2 * Copyright (c) 2016 Red Hat, 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;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.genius.mdsalutil.ActionInfo;
17 import org.opendaylight.genius.mdsalutil.ActionType;
18 import org.opendaylight.genius.mdsalutil.InstructionInfo;
19 import org.opendaylight.genius.mdsalutil.InstructionType;
20 import org.opendaylight.genius.mdsalutil.MDSALUtil;
21 import org.opendaylight.genius.mdsalutil.MatchFieldType;
22 import org.opendaylight.genius.mdsalutil.MatchInfo;
23 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
24 import org.opendaylight.genius.mdsalutil.NwConstants;
25 import org.opendaylight.genius.mdsalutil.NxMatchFieldType;
26 import org.opendaylight.genius.mdsalutil.NxMatchInfo;
27 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
28 import org.opendaylight.genius.utils.ServiceIndex;
29 import org.opendaylight.netvirt.aclservice.api.AclServiceManager.Action;
30 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
31 import org.opendaylight.netvirt.aclservice.utils.AclServiceOFFlowBuilder;
32 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.AccessListEntries;
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;
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;
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.AceType;
38 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;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeIngress;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionEgress;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.IpPrefixOrAddress;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.SecurityRuleAttr;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.interfaces._interface.AllowedAddressPairs;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * Provides the stateful implementation for egress (w.r.t VM) ACL service.
55 * Note: Table names used are w.r.t switch. Hence, switch ingress is VM egress
58 public class EgressAclServiceImpl extends AbstractAclServiceImpl {
60 private static final Logger LOG = LoggerFactory.getLogger(EgressAclServiceImpl.class);
63 * Initialize the member variables.
65 * @param dataBroker the data broker instance.
66 * @param mdsalManager the mdsal manager instance.
68 public EgressAclServiceImpl(DataBroker dataBroker, IMdsalApiManager mdsalManager) {
69 // Service mode is w.rt. switch
70 super(ServiceModeIngress.class, dataBroker, mdsalManager);
76 * @param interfaceName the interface name
79 protected void bindService(String interfaceName) {
80 int flowPriority = AclConstants.EGRESS_ACL_DEFAULT_FLOW_PRIORITY;
82 int instructionKey = 0;
83 List<Instruction> instructions = new ArrayList<>();
84 instructions.add(MDSALUtil.buildAndGetGotoTableInstruction(NwConstants.INGRESS_ACL_TABLE, ++instructionKey));
85 short serviceIndex = ServiceIndex.getIndex(NwConstants.ACL_SERVICE_NAME, NwConstants.ACL_SERVICE_INDEX);
86 BoundServices serviceInfo =
87 AclServiceUtils.getBoundServices(String.format("%s.%s.%s", "vpn", "egressacl", interfaceName),
88 serviceIndex, flowPriority, AclConstants.COOKIE_ACL_BASE, instructions);
89 InstanceIdentifier<BoundServices> path =
90 AclServiceUtils.buildServiceId(interfaceName, serviceIndex, ServiceModeIngress.class);
91 MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, path, serviceInfo);
97 * @param interfaceName the interface name
100 protected void unbindService(String interfaceName) {
101 InstanceIdentifier<BoundServices> path =
102 AclServiceUtils.buildServiceId(interfaceName,
103 ServiceIndex.getIndex(NwConstants.ACL_SERVICE_NAME, NwConstants.ACL_SERVICE_INDEX),
104 ServiceModeIngress.class);
105 MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION, path);
109 * Program conntrack rules.
111 * @param dpid the dpid
112 * @param dhcpMacAddress the dhcp mac address.
113 * @param allowedAddresses the allowed addresses
114 * @param lportTag the lport tag
115 * @param addOrRemove addorRemove
118 protected void programSpecificFixedRules(BigInteger dpid, String dhcpMacAddress,
119 List<AllowedAddressPairs> allowedAddresses, int lportTag, String portId, Action action, int addOrRemove) {
120 programEgressAclFixedConntrackRule(dpid, allowedAddresses, lportTag, portId, action, addOrRemove);
124 protected void programGeneralFixedRules(BigInteger dpid, String dhcpMacAddress,
125 List<AllowedAddressPairs> allowedAddresses, int lportTag, Action action, int addOrRemove) {
126 LOG.info("programFixedRules : adding default rules.");
128 if (action == Action.ADD || action == Action.REMOVE) {
129 egressAclDhcpAllowClientTraffic(dpid, dhcpMacAddress, lportTag, addOrRemove);
130 egressAclDhcpv6AllowClientTraffic(dpid, dhcpMacAddress, lportTag, addOrRemove);
131 egressAclDhcpDropServerTraffic(dpid, dhcpMacAddress, lportTag, addOrRemove);
132 egressAclDhcpv6DropServerTraffic(dpid, dhcpMacAddress, lportTag, addOrRemove);
133 egressAclIcmpv6AllowAll(dpid, lportTag, addOrRemove);
134 egressAclIcmpv6DropRouterAdvts(dpid, lportTag, addOrRemove);
136 programArpRule(dpid, allowedAddresses, lportTag, addOrRemove);
140 protected boolean programAclRules(List<Uuid> aclUuidList, BigInteger dpId, int lportTag, int addOrRemove, String
142 LOG.trace("Applying custom rules DpId {}, lportTag {}", dpId, lportTag);
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(DirectionEgress.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, false);
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 //The flow map contains list of flows if port range is selected.
182 for ( String flowName : flowMap.keySet()) {
183 List<MatchInfoBase> flows = flowMap.get(flowName);
184 flowName += "Egress" + lportTag + ace.getKey().getRuleName();
185 flows.add(AclServiceUtils.buildLPortTagMatch(lportTag));
186 flows.add(new NxMatchInfo(NxMatchFieldType.ct_state,
187 new long[] {AclConstants.TRACKED_NEW_CT_STATE, AclConstants.TRACKED_NEW_CT_STATE_MASK}));
189 Long elanId = AclServiceUtils.getElanIdFromInterface(portId, dataBroker);
190 List<ActionInfo> actionsInfos = new ArrayList<>();
191 actionsInfos.add(new ActionInfo(ActionType.nx_conntrack,
192 new String[] {"1", "0", elanId.toString(), "255"}, 2));
193 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
195 syncFlow(dpId, NwConstants.INGRESS_ACL_FILTER_TABLE, flowName, AclConstants.PROTO_MATCH_PRIORITY,
196 "ACL", 0, 0, AclConstants.COOKIE_ACL_BASE, flows, instructions, addOrRemove);
201 * Anti-spoofing rule to block the Ipv4 DHCP server traffic from the port.
203 * @param dpId the dpId
204 * @param dhcpMacAddress the Dhcp mac address
205 * @param lportTag the lport tag
206 * @param addOrRemove add/remove the flow.
208 protected void egressAclDhcpDropServerTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
210 List<MatchInfoBase> matches = AclServiceUtils.buildDhcpMatches(AclConstants.DHCP_SERVER_PORT_IPV4,
211 AclConstants.DHCP_CLIENT_PORT_IPV4, lportTag);
213 List<InstructionInfo> instructions = new ArrayList<>();
214 List<ActionInfo> actionsInfos = new ArrayList<>();
215 actionsInfos.add(new ActionInfo(ActionType.drop_action, new String[] {}));
216 String flowName = "Egress_DHCP_Server_v4" + dpId + "_" + lportTag + "_" + dhcpMacAddress + "_Drop_";
217 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName,
218 AclConstants.PROTO_DHCP_CLIENT_TRAFFIC_MATCH_PRIORITY, "ACL", 0,
219 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
223 * Anti-spoofing rule to block the Ipv6 DHCP server traffic from the port.
225 * @param dpId the dpId
226 * @param dhcpMacAddress the Dhcp mac address
227 * @param lportTag the lport tag
228 * @param addOrRemove add/remove the flow.
230 protected void egressAclDhcpv6DropServerTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
232 List<MatchInfoBase> matches = AclServiceUtils.buildDhcpV6Matches(AclConstants.DHCP_SERVER_PORT_IPV6,
233 AclConstants.DHCP_CLIENT_PORT_IPV6, lportTag);
235 List<InstructionInfo> instructions = new ArrayList<>();
236 List<ActionInfo> actionsInfos = new ArrayList<>();
237 actionsInfos.add(new ActionInfo(ActionType.drop_action, new String[] {}));
238 String flowName = "Egress_DHCP_Server_v6" + "_" + dpId + "_" + lportTag + "_" + dhcpMacAddress + "_Drop_";
239 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName,
240 AclConstants.PROTO_DHCP_CLIENT_TRAFFIC_MATCH_PRIORITY, "ACL", 0,
241 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
245 * Add rule to allow all ICMPv6 traffic from the VM port.
247 * @param dpId the dpId
248 * @param lportTag the lport tag
249 * @param addOrRemove add/remove the flow.
251 private void egressAclIcmpv6AllowAll(BigInteger dpId, int lportTag, int addOrRemove) {
252 final List<MatchInfoBase> matches = AclServiceUtils.buildIcmpV6Matches(0, 0, lportTag);
254 List<ActionInfo> actionsInfos = new ArrayList<>();
255 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
257 String flowName = "Egress_ICMPv6_ALL" + dpId + "_" + lportTag + "_Permit_";
258 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName, AclConstants.PROTO_IPV6_ALLOWED_PRIORITY,
259 "ACL", 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
263 * Anti-spoofing rule to block the Ipv6 Router Advts from the VM port.
265 * @param dpId the dpId
266 * @param lportTag the lport tag
267 * @param addOrRemove add/remove the flow.
269 private void egressAclIcmpv6DropRouterAdvts(BigInteger dpId, int lportTag, int addOrRemove) {
270 List<MatchInfoBase> matches = AclServiceUtils.buildIcmpV6Matches(AclConstants.ICMPV6_TYPE_RA, 0, lportTag);
272 List<InstructionInfo> instructions = new ArrayList<>();
273 List<ActionInfo> actionsInfos = new ArrayList<>();
274 actionsInfos.add(new ActionInfo(ActionType.drop_action, new String[] {}));
275 String flowName = "Egress_ICMPv6" + "_" + dpId + "_" + lportTag + "_" + AclConstants.ICMPV6_TYPE_RA + "_Drop_";
276 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName, AclConstants.PROTO_IPV6_DROP_PRIORITY, "ACL", 0,
277 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
281 * Add rule to ensure only DHCP server traffic from the specified mac is
284 * @param dpId the dpid
285 * @param dhcpMacAddress the DHCP server mac address
286 * @param lportTag the lport tag
287 * @param addOrRemove whether to add or remove the flow
289 private void egressAclDhcpAllowClientTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
291 final List<MatchInfoBase> matches = AclServiceUtils.buildDhcpMatches(AclConstants.DHCP_CLIENT_PORT_IPV4,
292 AclConstants.DHCP_SERVER_PORT_IPV4, lportTag);
294 List<ActionInfo> actionsInfos = new ArrayList<>();
295 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
297 String flowName = "Egress_DHCP_Client_v4" + dpId + "_" + lportTag + "_" + dhcpMacAddress + "_Permit_";
298 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName, AclConstants.PROTO_DHCP_CLIENT_TRAFFIC_MATCH_PRIORITY,
299 "ACL", 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
303 * Add rule to ensure only DHCPv6 server traffic from the specified mac is
306 * @param dpId the dpid
307 * @param dhcpMacAddress the DHCP server mac address
308 * @param lportTag the lport tag
309 * @param addOrRemove whether to add or remove the flow
311 private void egressAclDhcpv6AllowClientTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
313 final List<MatchInfoBase> matches = AclServiceUtils.buildDhcpV6Matches(AclConstants.DHCP_CLIENT_PORT_IPV6,
314 AclConstants.DHCP_SERVER_PORT_IPV6, lportTag);
316 List<ActionInfo> actionsInfos = new ArrayList<>();
317 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
319 String flowName = "Egress_DHCP_Client_v6" + "_" + dpId + "_" + lportTag + "_" + dhcpMacAddress + "_Permit_";
320 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName, AclConstants.PROTO_DHCP_CLIENT_TRAFFIC_MATCH_PRIORITY,
321 "ACL", 0, 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
325 * Adds the rule to send the packet to the netfilter to check whether it is
328 * @param dpId the dpId
329 * @param allowedAddresses the allowed addresses
330 * @param priority the priority of the flow
331 * @param flowId the flowId
332 * @param conntrackState the conntrack state of the packets thats should be
334 * @param conntrackMask the conntrack mask
335 * @param portId the portId
336 * @param addOrRemove whether to add or remove the flow
338 private void programConntrackRecircRules(BigInteger dpId, List<AllowedAddressPairs> allowedAddresses,
339 Integer priority, String flowId, int conntrackState, int conntrackMask, String portId, int addOrRemove) {
340 for (AllowedAddressPairs allowedAddress : allowedAddresses) {
341 IpPrefixOrAddress attachIp = allowedAddress.getIpAddress();
342 String attachMac = allowedAddress.getMacAddress().getValue();
344 List<MatchInfoBase> matches = new ArrayList<>();
345 matches.add(new MatchInfo(MatchFieldType.eth_type, new long[] {NwConstants.ETHTYPE_IPV4}));
346 matches.add(new NxMatchInfo(NxMatchFieldType.ct_state, new long[] {conntrackState, conntrackMask}));
347 matches.add(new MatchInfo(MatchFieldType.eth_src, new String[] {attachMac}));
348 matches.addAll(AclServiceUtils.buildIpMatches(attachIp, MatchFieldType.ipv4_source));
350 Long elanTag = AclServiceUtils.getElanIdFromInterface(portId, dataBroker);
351 List<InstructionInfo> instructions = new ArrayList<>();
352 List<ActionInfo> actionsInfos = new ArrayList<>();
353 actionsInfos.add(new ActionInfo(ActionType.nx_conntrack,
354 new String[] {"0", "0", elanTag.toString(), Short.toString(
355 NwConstants.INGRESS_ACL_FILTER_TABLE)}, 2));
356 instructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
358 String flowName = "Egress_Fixed_Conntrk_Untrk_" + dpId + "_" + attachMac + "_"
359 + String.valueOf(attachIp.getValue()) + "_" + flowId;
360 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName, AclConstants.PROTO_MATCH_PRIORITY, "ACL", 0, 0,
361 AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
366 * Adds the rule to allow arp packets.
368 * @param dpId the dpId
369 * @param allowedAddresses the allowed addresses
370 * @param lportTag the lport tag
371 * @param addOrRemove whether to add or remove the flow
373 protected void programArpRule(BigInteger dpId, List<AllowedAddressPairs> allowedAddresses, int lportTag,
375 for (AllowedAddressPairs allowedAddress : allowedAddresses) {
376 String attachMac = allowedAddress.getMacAddress().getValue();
377 List<MatchInfo> matches = new ArrayList<>();
378 matches.add(new MatchInfo(MatchFieldType.eth_type, new long[] {NwConstants.ETHTYPE_ARP}));
379 matches.add(new MatchInfo(MatchFieldType.arp_sha, new String[] {attachMac}));
380 matches.add(AclServiceUtils.buildLPortTagMatch(lportTag));
382 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(new ArrayList<>());
384 String flowName = "Egress_ARP_" + dpId + "_" + attachMac;
385 syncFlow(dpId, NwConstants.INGRESS_ACL_TABLE, flowName,
386 AclConstants.PROTO_ARP_TRAFFIC_MATCH_PRIORITY, "ACL", 0, 0,
387 AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
392 * Programs the default connection tracking rules.
394 * @param dpid the dp id
395 * @param allowedAddresses the allowed addresses
396 * @param lportTag the lport tag
397 * @param portId the portId
398 * @param action the action
399 * @param write whether to add or remove the flow.
401 private void programEgressAclFixedConntrackRule(BigInteger dpid, List<AllowedAddressPairs> allowedAddresses,
402 int lportTag, String portId, Action action, int write) {
403 programConntrackRecircRules(dpid, allowedAddresses, AclConstants.CT_STATE_UNTRACKED_PRIORITY,
404 "Untracked",AclConstants.UNTRACKED_CT_STATE,AclConstants.UNTRACKED_CT_STATE_MASK, portId, write );
405 LOG.info("programEgressAclFixedConntrackRule : default connection tracking rule are added.");