2 * Copyright (c) 2016 HPE, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.netvirt.aclservice;
10 import java.math.BigInteger;
11 import java.util.ArrayList;
12 import java.util.List;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.genius.mdsalutil.ActionInfo;
18 import org.opendaylight.genius.mdsalutil.InstructionInfo;
19 import org.opendaylight.genius.mdsalutil.MDSALUtil;
20 import org.opendaylight.genius.mdsalutil.MatchFieldType;
21 import org.opendaylight.genius.mdsalutil.MatchInfo;
22 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
23 import org.opendaylight.genius.mdsalutil.NwConstants;
24 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
25 import org.opendaylight.genius.utils.ServiceIndex;
26 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
27 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
28 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
29 import org.opendaylight.netvirt.aclservice.utils.AclServiceOFFlowBuilder;
30 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.AccessListEntries;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.Ace;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.Matches;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.AceType;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.access.list.entries.ace.matches.ace.type.AceIp;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeEgress;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionIngress;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttr;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * Provides abstract implementation for ingress (w.r.t VM) ACL service.
52 * Note: Table names used are w.r.t switch. Hence, switch ingress is VM egress
55 public abstract class AbstractIngressAclServiceImpl extends AbstractAclServiceImpl {
57 private static final Logger LOG = LoggerFactory.getLogger(AbstractIngressAclServiceImpl.class);
60 * Initialize the member variables.
62 * @param dataBroker the data broker instance.
63 * @param mdsalManager the mdsal manager.
65 public AbstractIngressAclServiceImpl(DataBroker dataBroker, IMdsalApiManager mdsalManager, AclDataUtil aclDataUtil,
66 AclServiceUtils aclServiceUtils) {
67 // Service mode is w.rt. switch
68 super(ServiceModeEgress.class, dataBroker, mdsalManager, aclDataUtil, aclServiceUtils);
74 * @param interfaceName the interface name
77 protected void bindService(String interfaceName) {
78 int flowPriority = AclConstants.INGRESS_ACL_DEFAULT_FLOW_PRIORITY;
80 int instructionKey = 0;
81 List<Instruction> instructions = new ArrayList<>();
82 instructions.add(MDSALUtil.buildAndGetGotoTableInstruction(NwConstants.EGRESS_ACL_TABLE, ++instructionKey));
83 BoundServices serviceInfo = AclServiceUtils.getBoundServices(
84 String.format("%s.%s.%s", "vpn", "ingressacl", interfaceName),
85 ServiceIndex.getIndex(NwConstants.EGRESS_ACL_SERVICE_NAME, NwConstants.EGRESS_ACL_SERVICE_INDEX),
86 flowPriority, AclConstants.COOKIE_ACL_BASE, instructions);
87 InstanceIdentifier<BoundServices> path = AclServiceUtils.buildServiceId(interfaceName,
88 ServiceIndex.getIndex(NwConstants.EGRESS_ACL_SERVICE_NAME,
89 NwConstants.EGRESS_ACL_SERVICE_INDEX), ServiceModeEgress.class);
90 MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, path, serviceInfo);
96 * @param interfaceName the interface name
99 protected void unbindService(String interfaceName) {
100 InstanceIdentifier<BoundServices> path = AclServiceUtils.buildServiceId(interfaceName,
101 ServiceIndex.getIndex(NwConstants.EGRESS_ACL_SERVICE_NAME, NwConstants.EGRESS_ACL_SERVICE_INDEX),
102 ServiceModeEgress.class);
103 MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION, path);
107 * Program conntrack rules.
109 * @param dpid the dpid
110 * @param dhcpMacAddress the dhcp mac address.
111 * @param allowedAddresses the allowed addresses
112 * @param lportTag the lport tag
113 * @param addOrRemove add or remove the flow
116 protected abstract void programSpecificFixedRules(BigInteger dpid, String dhcpMacAddress,
117 List<AllowedAddressPairs> allowedAddresses, int lportTag, String portId, Action action, int addOrRemove);
120 protected void programGeneralFixedRules(BigInteger dpid, String dhcpMacAddress,
121 List<AllowedAddressPairs> allowedAddresses, int lportTag, Action action, int addOrRemove) {
122 LOG.info("programFixedRules : {} default rules.", action == Action.ADD ? "adding" : "removing");
124 if (action == Action.ADD || action == Action.REMOVE) {
125 ingressAclDhcpAllowServerTraffic(dpid, dhcpMacAddress, lportTag, addOrRemove,
126 AclConstants.PROTO_PREFIX_MATCH_PRIORITY);
127 ingressAclDhcpv6AllowServerTraffic(dpid, dhcpMacAddress, lportTag, addOrRemove,
128 AclConstants.PROTO_PREFIX_MATCH_PRIORITY);
129 ingressAclIcmpv6AllowedTraffic(dpid, lportTag, addOrRemove);
131 programArpRule(dpid, lportTag, addOrRemove);
135 protected boolean programAclRules(List<Uuid> aclUuidList, BigInteger dpId, int lportTag, int addOrRemove, String
137 if (aclUuidList == null || dpId == null) {
138 LOG.warn("one of the ingress acl parameters can not be null. sg {}, dpId {}",
143 for (Uuid sgUuid :aclUuidList ) {
144 Acl acl = AclServiceUtils.getAcl(dataBroker, sgUuid.getValue());
146 LOG.warn("The ACL is empty");
149 AccessListEntries accessListEntries = acl.getAccessListEntries();
150 List<Ace> aceList = accessListEntries.getAce();
151 for (Ace ace : aceList) {
152 programAceRule(dpId, lportTag, addOrRemove, ace, portId, null);
159 protected void programAceRule(BigInteger dpId, int lportTag, int addOrRemove, Ace ace, String portId,
160 List<AllowedAddressPairs> syncAllowedAddresses) {
161 SecurityRuleAttr aceAttr = AclServiceUtils.getAccesssListAttributes(ace);
162 if (!aceAttr.getDirection().equals(DirectionIngress.class)) {
165 Matches matches = ace.getMatches();
166 AceType aceType = matches.getAceType();
167 Map<String,List<MatchInfoBase>> flowMap = null;
168 if (aceType instanceof AceIp) {
169 flowMap = AclServiceOFFlowBuilder.programIpFlow(matches);
170 if (syncAllowedAddresses != null) {
171 flowMap = AclServiceUtils.getFlowForAllowedAddresses(syncAllowedAddresses, flowMap, true);
172 } else if (aceAttr.getRemoteGroupId() != null) {
173 flowMap = aclServiceUtils.getFlowForRemoteAcl(aceAttr.getRemoteGroupId(), portId, flowMap,
177 if (null == flowMap) {
178 LOG.error("Failed to apply ACL {} lportTag {}", ace.getKey(), lportTag);
181 for ( String flowName : flowMap.keySet()) {
182 flowName = syncSpecificAclFlow(dpId, lportTag, addOrRemove, ace, portId, flowMap, flowName);
186 protected abstract String syncSpecificAclFlow(BigInteger dpId, int lportTag, int addOrRemove, Ace ace,
187 String portId, Map<String, List<MatchInfoBase>> flowMap, String flowName);
190 * Add rule to ensure only DHCP server traffic from the specified mac is
193 * @param dpId the dpid
194 * @param dhcpMacAddress the DHCP server mac address
195 * @param lportTag the lport tag
196 * @param addOrRemove is write or delete
197 * @param protoPortMatchPriority the priority
199 protected void ingressAclDhcpAllowServerTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
200 int addOrRemove, int protoPortMatchPriority) {
201 final List<MatchInfoBase> matches = AclServiceUtils.buildDhcpMatches(AclConstants.DHCP_SERVER_PORT_IPV4,
202 AclConstants.DHCP_CLIENT_PORT_IPV4, lportTag);
204 List<ActionInfo> actionsInfos = new ArrayList<>();
205 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
207 String flowName = "Ingress_DHCP_Server_v4" + dpId + "_" + lportTag + "_" + dhcpMacAddress + "_Permit_";
208 syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName, AclConstants.PROTO_DHCP_SERVER_MATCH_PRIORITY, "ACL", 0,
209 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
213 * Add rule to ensure only DHCPv6 server traffic from the specified mac is
216 * @param dpId the dpid
217 * @param dhcpMacAddress the DHCP server mac address
218 * @param lportTag the lport tag
219 * @param addOrRemove is write or delete
220 * @param protoPortMatchPriority the priority
222 protected void ingressAclDhcpv6AllowServerTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
223 int addOrRemove, Integer protoPortMatchPriority) {
224 final List<MatchInfoBase> matches = AclServiceUtils.buildDhcpV6Matches(AclConstants.DHCP_SERVER_PORT_IPV6,
225 AclConstants.DHCP_CLIENT_PORT_IPV6, lportTag);
227 List<ActionInfo> actionsInfos = new ArrayList<>();
228 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
231 "Ingress_DHCP_Server_v6" + "_" + dpId + "_" + lportTag + "_" + "_" + dhcpMacAddress + "_Permit_";
232 syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName, AclConstants.PROTO_DHCP_SERVER_MATCH_PRIORITY, "ACL", 0,
233 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
237 * Add rules to ensure that certain ICMPv6 like MLD_QUERY (130), NS (135), NA (136) are allowed into the VM.
239 * @param dpId the dpid
240 * @param lportTag the lport tag
241 * @param addOrRemove is write or delete
243 private void ingressAclIcmpv6AllowedTraffic(BigInteger dpId, int lportTag, int addOrRemove) {
244 List<ActionInfo> actionsInfos = new ArrayList<>();
245 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
247 // Allow ICMPv6 Multicast Listener Query packets.
248 List<MatchInfoBase> matches = AclServiceUtils.buildIcmpV6Matches(AclConstants.ICMPV6_TYPE_MLD_QUERY,
252 "Ingress_ICMPv6" + "_" + dpId + "_" + lportTag + "_" + AclConstants.ICMPV6_TYPE_MLD_QUERY + "_Permit_";
253 syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName, AclConstants.PROTO_IPV6_ALLOWED_PRIORITY, "ACL", 0,
254 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
256 // Allow ICMPv6 Neighbor Solicitation packets.
257 matches = AclServiceUtils.buildIcmpV6Matches(AclConstants.ICMPV6_TYPE_NS, 0, lportTag);
260 "Ingress_ICMPv6" + "_" + dpId + "_" + lportTag + "_" + AclConstants.ICMPV6_TYPE_NS + "_Permit_";
261 syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName, AclConstants.PROTO_IPV6_ALLOWED_PRIORITY, "ACL", 0,
262 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
264 // Allow ICMPv6 Neighbor Advertisement packets.
265 matches = AclServiceUtils.buildIcmpV6Matches(AclConstants.ICMPV6_TYPE_NA, 0, lportTag);
268 "Ingress_ICMPv6" + "_" + dpId + "_" + lportTag + "_" + AclConstants.ICMPV6_TYPE_NA + "_Permit_";
269 syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName, AclConstants.PROTO_IPV6_ALLOWED_PRIORITY, "ACL", 0,
270 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
274 * Adds the rule to allow arp packets.
276 * @param dpId the dpId
277 * @param lportTag the lport tag
278 * @param addOrRemove whether to add or remove the flow
280 protected void programArpRule(BigInteger dpId, int lportTag, int addOrRemove) {
281 List<MatchInfo> matches = new ArrayList<>();
282 matches.add(new MatchInfo(MatchFieldType.eth_type, new long[] {NwConstants.ETHTYPE_ARP}));
283 matches.add(AclServiceUtils.buildLPortTagMatch(lportTag));
285 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(new ArrayList<>());
286 String flowName = "Ingress_ARP_" + dpId + "_" + lportTag;
287 syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName,
288 AclConstants.PROTO_ARP_TRAFFIC_MATCH_PRIORITY, "ACL", 0, 0,
289 AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);