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.actions.ActionDrop;
25 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
26 import org.opendaylight.genius.utils.ServiceIndex;
27 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
28 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
29 import org.opendaylight.netvirt.aclservice.utils.AclDataUtil;
30 import org.opendaylight.netvirt.aclservice.utils.AclServiceOFFlowBuilder;
31 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.AccessListEntries;
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;
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;
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.AceType;
37 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;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeIngress;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionEgress;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttr;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * Provides abstract implementation for egress (w.r.t VM) ACL service.
53 * Note: Table names used are w.r.t switch. Hence, switch ingress is VM egress
56 public abstract class AbstractEgressAclServiceImpl extends AbstractAclServiceImpl {
58 private static final Logger LOG = LoggerFactory.getLogger(AbstractEgressAclServiceImpl.class);
61 * Initialize the member variables.
63 * @param dataBroker the data broker instance.
64 * @param mdsalManager the mdsal manager instance.
67 * @param aclServiceUtils
68 * the acl service util.
70 public AbstractEgressAclServiceImpl(DataBroker dataBroker, IMdsalApiManager mdsalManager, AclDataUtil aclDataUtil,
71 AclServiceUtils aclServiceUtils) {
72 // Service mode is w.rt. switch
73 super(ServiceModeIngress.class, dataBroker, mdsalManager, aclDataUtil, aclServiceUtils);
79 * @param interfaceName the interface name
82 protected void bindService(String interfaceName) {
83 int flowPriority = AclConstants.EGRESS_ACL_DEFAULT_FLOW_PRIORITY;
85 int instructionKey = 0;
86 List<Instruction> instructions = new ArrayList<>();
87 instructions.add(MDSALUtil.buildAndGetGotoTableInstruction(NwConstants.INGRESS_ACL_TABLE, ++instructionKey));
88 short serviceIndex = ServiceIndex.getIndex(NwConstants.ACL_SERVICE_NAME, NwConstants.ACL_SERVICE_INDEX);
89 BoundServices serviceInfo =
90 AclServiceUtils.getBoundServices(String.format("%s.%s.%s", "vpn", "egressacl", interfaceName),
91 serviceIndex, flowPriority, AclConstants.COOKIE_ACL_BASE, instructions);
92 InstanceIdentifier<BoundServices> path =
93 AclServiceUtils.buildServiceId(interfaceName, serviceIndex, ServiceModeIngress.class);
94 MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, path, serviceInfo);
100 * @param interfaceName the interface name
103 protected void unbindService(String interfaceName) {
104 InstanceIdentifier<BoundServices> path =
105 AclServiceUtils.buildServiceId(interfaceName,
106 ServiceIndex.getIndex(NwConstants.ACL_SERVICE_NAME, NwConstants.ACL_SERVICE_INDEX),
107 ServiceModeIngress.class);
108 MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION, path);
112 protected void programGeneralFixedRules(BigInteger dpid, String dhcpMacAddress,
113 List<AllowedAddressPairs> allowedAddresses, int lportTag, Action action, int addOrRemove) {
114 LOG.info("programFixedRules : adding default rules.");
116 if (action == Action.ADD || action == Action.REMOVE) {
117 egressAclDhcpAllowClientTraffic(dpid, dhcpMacAddress, lportTag, addOrRemove);
118 egressAclDhcpv6AllowClientTraffic(dpid, dhcpMacAddress, lportTag, addOrRemove);
119 egressAclDhcpDropServerTraffic(dpid, dhcpMacAddress, lportTag, addOrRemove);
120 egressAclDhcpv6DropServerTraffic(dpid, dhcpMacAddress, lportTag, addOrRemove);
121 egressAclIcmpv6DropRouterAdvts(dpid, lportTag, addOrRemove);
122 egressAclIcmpv6AllowedList(dpid, lportTag, addOrRemove);
124 programArpRule(dpid, allowedAddresses, lportTag, addOrRemove);
128 protected boolean programAclRules(List<Uuid> aclUuidList, BigInteger dpId, int lportTag, int addOrRemove, String
130 LOG.trace("Applying custom rules DpId {}, lportTag {}", dpId, lportTag);
131 if (aclUuidList == null || dpId == null) {
132 LOG.warn("one of the egress acl parameters can not be null. sg {}, dpId {}",
136 for (Uuid sgUuid :aclUuidList ) {
137 Acl acl = AclServiceUtils.getAcl(dataBroker, sgUuid.getValue());
139 LOG.warn("The ACL is empty");
142 AccessListEntries accessListEntries = acl.getAccessListEntries();
143 List<Ace> aceList = accessListEntries.getAce();
144 for (Ace ace: aceList) {
145 programAceRule(dpId, lportTag, addOrRemove, acl.getAclName(), ace, portId, null);
152 protected void programAceRule(BigInteger dpId, int lportTag, int addOrRemove, String aclName, Ace ace,
153 String portId, List<AllowedAddressPairs> syncAllowedAddresses) {
154 SecurityRuleAttr aceAttr = AclServiceUtils.getAccesssListAttributes(ace);
155 if (!aceAttr.getDirection().equals(DirectionEgress.class)) {
158 Matches matches = ace.getMatches();
159 AceType aceType = matches.getAceType();
160 Map<String,List<MatchInfoBase>> flowMap = null;
161 if (aceType instanceof AceIp) {
162 flowMap = AclServiceOFFlowBuilder.programIpFlow(matches);
163 if (syncAllowedAddresses != null) {
164 flowMap = AclServiceUtils.getFlowForAllowedAddresses(syncAllowedAddresses, flowMap, false);
165 } else if (aceAttr.getRemoteGroupId() != null) {
166 flowMap = aclServiceUtils.getFlowForRemoteAcl(aceAttr.getRemoteGroupId(), portId, flowMap,
170 if (null == flowMap) {
171 LOG.error("Failed to apply ACL {} lportTag {}", ace.getKey(), lportTag);
174 int priority = this.aclDataUtil.getAclFlowPriority(aclName);
175 // The flow map contains list of flows if port range is selected.
176 for (String flowName : flowMap.keySet()) {
177 flowName = syncSpecificAclFlow(dpId, lportTag, addOrRemove, priority, ace, portId, flowMap, flowName);
181 protected abstract String syncSpecificAclFlow(BigInteger dpId, int lportTag, int addOrRemove, int priority, Ace ace,
182 String portId, Map<String, List<MatchInfoBase>> flowMap, String flowName);
185 * Anti-spoofing rule to block the Ipv4 DHCP server traffic from the port.
187 * @param dpId the dpId
188 * @param dhcpMacAddress the Dhcp mac address
189 * @param lportTag the lport tag
190 * @param addOrRemove add/remove the flow.
192 protected void egressAclDhcpDropServerTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
194 List<MatchInfoBase> matches = AclServiceUtils.buildDhcpMatches(AclConstants.DHCP_SERVER_PORT_IPV4,
195 AclConstants.DHCP_CLIENT_PORT_IPV4, lportTag);
197 List<InstructionInfo> instructions = new ArrayList<>();
198 List<ActionInfo> actionsInfos = new ArrayList<>();
199 actionsInfos.add(new ActionDrop());
200 String flowName = "Egress_DHCP_Server_v4" + dpId + "_" + lportTag + "_" + dhcpMacAddress + "_Drop_";
201 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName,
202 AclConstants.PROTO_DHCP_CLIENT_TRAFFIC_MATCH_PRIORITY, "ACL", 0,
203 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
207 * Anti-spoofing rule to block the Ipv6 DHCP server traffic from the port.
209 * @param dpId the dpId
210 * @param dhcpMacAddress the Dhcp mac address
211 * @param lportTag the lport tag
212 * @param addOrRemove add/remove the flow.
214 protected void egressAclDhcpv6DropServerTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
216 List<MatchInfoBase> matches = AclServiceUtils.buildDhcpV6Matches(AclConstants.DHCP_SERVER_PORT_IPV6,
217 AclConstants.DHCP_CLIENT_PORT_IPV6, lportTag);
219 List<InstructionInfo> instructions = new ArrayList<>();
220 List<ActionInfo> actionsInfos = new ArrayList<>();
221 actionsInfos.add(new ActionDrop());
222 String flowName = "Egress_DHCP_Server_v6" + "_" + dpId + "_" + lportTag + "_" + dhcpMacAddress + "_Drop_";
223 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName,
224 AclConstants.PROTO_DHCP_CLIENT_TRAFFIC_MATCH_PRIORITY, "ACL", 0,
225 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
229 * Anti-spoofing rule to block the Ipv6 Router Advts from the VM port.
231 * @param dpId the dpId
232 * @param lportTag the lport tag
233 * @param addOrRemove add/remove the flow.
235 private void egressAclIcmpv6DropRouterAdvts(BigInteger dpId, int lportTag, int addOrRemove) {
236 List<MatchInfoBase> matches = AclServiceUtils.buildIcmpV6Matches(AclConstants.ICMPV6_TYPE_RA, 0, lportTag);
238 List<InstructionInfo> instructions = new ArrayList<>();
239 List<ActionInfo> actionsInfos = new ArrayList<>();
240 actionsInfos.add(new ActionDrop());
241 String flowName = "Egress_ICMPv6" + "_" + dpId + "_" + lportTag + "_" + AclConstants.ICMPV6_TYPE_RA + "_Drop_";
242 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName, AclConstants.PROTO_IPV6_DROP_PRIORITY, "ACL", 0,
243 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
247 * Add rule to allow certain ICMPv6 traffic from VM ports.
249 * @param dpId the dpId
250 * @param lportTag the lport tag
251 * @param addOrRemove add/remove the flow.
253 private void egressAclIcmpv6AllowedList(BigInteger dpId, int lportTag, int addOrRemove) {
254 List<ActionInfo> actionsInfos = new ArrayList<>();
255 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
257 for (Integer icmpv6Type: AclConstants.allowedIcmpv6NdList()) {
258 List<MatchInfoBase> matches = AclServiceUtils.buildIcmpV6Matches(icmpv6Type, 0, lportTag);
259 String flowName = "Egress_ICMPv6" + "_" + dpId + "_" + lportTag + "_" + icmpv6Type + "_Permit_";
260 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName, AclConstants.PROTO_IPV6_ALLOWED_PRIORITY,
261 "ACL", 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
266 * Add rule to ensure only DHCP server traffic from the specified mac is
269 * @param dpId the dpid
270 * @param dhcpMacAddress the DHCP server mac address
271 * @param lportTag the lport tag
272 * @param addOrRemove whether to add or remove the flow
274 private void egressAclDhcpAllowClientTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
276 final List<MatchInfoBase> matches = AclServiceUtils.buildDhcpMatches(AclConstants.DHCP_CLIENT_PORT_IPV4,
277 AclConstants.DHCP_SERVER_PORT_IPV4, lportTag);
279 List<ActionInfo> actionsInfos = new ArrayList<>();
280 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
282 String flowName = "Egress_DHCP_Client_v4" + dpId + "_" + lportTag + "_" + dhcpMacAddress + "_Permit_";
283 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName, AclConstants.PROTO_DHCP_CLIENT_TRAFFIC_MATCH_PRIORITY,
284 "ACL", 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
288 * Add rule to ensure only DHCPv6 server traffic from the specified mac is
291 * @param dpId the dpid
292 * @param dhcpMacAddress the DHCP server mac address
293 * @param lportTag the lport tag
294 * @param addOrRemove whether to add or remove the flow
296 private void egressAclDhcpv6AllowClientTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
298 final List<MatchInfoBase> matches = AclServiceUtils.buildDhcpV6Matches(AclConstants.DHCP_CLIENT_PORT_IPV6,
299 AclConstants.DHCP_SERVER_PORT_IPV6, lportTag);
301 List<ActionInfo> actionsInfos = new ArrayList<>();
302 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
304 String flowName = "Egress_DHCP_Client_v6" + "_" + dpId + "_" + lportTag + "_" + dhcpMacAddress + "_Permit_";
305 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName, AclConstants.PROTO_DHCP_CLIENT_TRAFFIC_MATCH_PRIORITY,
306 "ACL", 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
310 * Adds the rule to allow arp packets.
312 * @param dpId the dpId
313 * @param allowedAddresses the allowed addresses
314 * @param lportTag the lport tag
315 * @param addOrRemove whether to add or remove the flow
317 protected void programArpRule(BigInteger dpId, List<AllowedAddressPairs> allowedAddresses, int lportTag,
319 for (AllowedAddressPairs allowedAddress : allowedAddresses) {
320 String attachMac = allowedAddress.getMacAddress().getValue();
321 List<MatchInfo> matches = new ArrayList<>();
322 matches.add(new MatchInfo(MatchFieldType.eth_type, new long[] {NwConstants.ETHTYPE_ARP}));
323 matches.add(new MatchInfo(MatchFieldType.arp_sha, new String[] {attachMac}));
324 matches.add(AclServiceUtils.buildLPortTagMatch(lportTag));
326 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(new ArrayList<>());
328 String flowName = "Egress_ARP_" + dpId + "_" + attachMac;
329 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName,
330 AclConstants.PROTO_ARP_TRAFFIC_MATCH_PRIORITY, "ACL", 0, 0,
331 AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);