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;
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.ActionType;
19 import org.opendaylight.genius.mdsalutil.InstructionInfo;
20 import org.opendaylight.genius.mdsalutil.InstructionType;
21 import org.opendaylight.genius.mdsalutil.MDSALUtil;
22 import org.opendaylight.genius.mdsalutil.MatchFieldType;
23 import org.opendaylight.genius.mdsalutil.MatchInfo;
24 import org.opendaylight.genius.mdsalutil.MatchInfoBase;
25 import org.opendaylight.genius.mdsalutil.NwConstants;
26 import org.opendaylight.genius.mdsalutil.NxMatchFieldType;
27 import org.opendaylight.genius.mdsalutil.NxMatchInfo;
28 import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
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.ServiceModeEgress;
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.DirectionIngress;
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 ingress (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 IngressAclServiceImpl extends AbstractAclServiceImpl {
60 private static final Logger LOG = LoggerFactory.getLogger(IngressAclServiceImpl.class);
63 * Initialize the member variables.
65 * @param dataBroker the data broker instance.
66 * @param mdsalManager the mdsal manager.
68 public IngressAclServiceImpl(DataBroker dataBroker, IMdsalApiManager mdsalManager) {
69 // Service mode is w.rt. switch
70 super(ServiceModeEgress.class, dataBroker, mdsalManager);
76 * @param interfaceName the interface name
79 protected void bindService(String interfaceName) {
80 int flowPriority = AclConstants.INGRESS_ACL_DEFAULT_FLOW_PRIORITY;
82 int instructionKey = 0;
83 List<Instruction> instructions = new ArrayList<>();
84 instructions.add(MDSALUtil.buildAndGetGotoTableInstruction(NwConstants.EGRESS_ACL_TABLE, ++instructionKey));
85 BoundServices serviceInfo = AclServiceUtils.getBoundServices(
86 String.format("%s.%s.%s", "vpn", "ingressacl", interfaceName), NwConstants.EGRESS_ACL_SERVICE_INDEX,
87 flowPriority, AclConstants.COOKIE_ACL_BASE, instructions);
88 InstanceIdentifier<BoundServices> path = AclServiceUtils.buildServiceId(interfaceName,
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 NwConstants.EGRESS_ACL_SERVICE_INDEX, ServiceModeEgress.class);
102 MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION, path);
106 * Program conntrack rules.
108 * @param dpid the dpid
109 * @param dhcpMacAddress the dhcp mac address.
110 * @param allowedAddresses the allowed addresses
111 * @param lportTag the lport tag
112 * @param addOrRemove add or remove the flow
115 protected void programSpecificFixedRules(BigInteger dpid, String dhcpMacAddress,
116 List<AllowedAddressPairs> allowedAddresses, int lportTag, String portId, Action action, int addOrRemove) {
117 programIngressAclFixedConntrackRule(dpid, allowedAddresses, portId, action, addOrRemove);
121 protected void programGeneralFixedRules(BigInteger dpid, String dhcpMacAddress,
122 List<AllowedAddressPairs> allowedAddresses, int lportTag, Action action, int addOrRemove) {
123 LOG.info("programFixedRules : adding default rules.");
125 if (action == Action.ADD || action == Action.REMOVE) {
126 ingressAclDhcpAllowServerTraffic(dpid, dhcpMacAddress, lportTag, addOrRemove,
127 AclConstants.PROTO_PREFIX_MATCH_PRIORITY);
128 ingressAclDhcpv6AllowServerTraffic(dpid, dhcpMacAddress, lportTag, addOrRemove,
129 AclConstants.PROTO_PREFIX_MATCH_PRIORITY);
130 ingressAclIcmpv6AllowedTraffic(dpid, lportTag, addOrRemove);
132 programArpRule(dpid, lportTag, addOrRemove);
136 protected boolean programAclRules(List<Uuid> aclUuidList, BigInteger dpId, int lportTag, int addOrRemove, String
138 if (aclUuidList == null || dpId == null) {
139 LOG.warn("one of the ingress acl parameters can not be null. sg {}, dpId {}",
144 for (Uuid sgUuid :aclUuidList ) {
145 Acl acl = AclServiceUtils.getAcl(dataBroker, sgUuid.getValue());
147 LOG.warn("The ACL is empty");
150 AccessListEntries accessListEntries = acl.getAccessListEntries();
151 List<Ace> aceList = accessListEntries.getAce();
152 for (Ace ace : aceList) {
153 programAceRule(dpId, lportTag, addOrRemove, ace, portId, null);
160 protected void programAceRule(BigInteger dpId, int lportTag, int addOrRemove, Ace ace, String portId,
161 List<AllowedAddressPairs> syncAllowedAddresses) {
162 SecurityRuleAttr aceAttr = AclServiceUtils.getAccesssListAttributes(ace);
163 if (!aceAttr.getDirection().equals(DirectionIngress.class)) {
166 Matches matches = ace.getMatches();
167 AceType aceType = matches.getAceType();
168 Map<String,List<MatchInfoBase>> flowMap = null;
169 if (aceType instanceof AceIp) {
170 flowMap = AclServiceOFFlowBuilder.programIpFlow(matches);
171 if (syncAllowedAddresses != null) {
172 flowMap = AclServiceUtils.getFlowForAllowedAddresses(syncAllowedAddresses, flowMap, true);
173 } else if (aceAttr.getRemoteGroupId() != null) {
174 flowMap = AclServiceUtils.getFlowForRemoteAcl(aceAttr.getRemoteGroupId(), portId, flowMap,
178 if (null == flowMap) {
179 LOG.error("Failed to apply ACL {} lportTag {}", ace.getKey(), lportTag);
182 for ( String flowName : flowMap.keySet()) {
183 List<MatchInfoBase> flows = flowMap.get(flowName);
184 flowName += "Ingress" + 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 elanTag = AclServiceUtils.getElanIdFromInterface(portId, dataBroker);
190 List<ActionInfo> actionsInfos = new ArrayList<>();
191 actionsInfos.add(new ActionInfo(ActionType.nx_conntrack,
192 new String[] {"1", "0", elanTag.toString(), "255"}, 2));
193 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
195 syncFlow(dpId, NwConstants.EGRESS_ACL_FILTER_TABLE, flowName, AclConstants.PROTO_MATCH_PRIORITY,
196 "ACL", 0, 0, AclConstants.COOKIE_ACL_BASE, flows, instructions, addOrRemove);
201 * Add rule to ensure only DHCP server traffic from the specified mac is
204 * @param dpId the dpid
205 * @param dhcpMacAddress the DHCP server mac address
206 * @param lportTag the lport tag
207 * @param addOrRemove is write or delete
208 * @param protoPortMatchPriority the priority
210 protected void ingressAclDhcpAllowServerTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
211 int addOrRemove, int protoPortMatchPriority) {
212 final List<MatchInfoBase> matches = AclServiceUtils.buildDhcpMatches(AclConstants.DHCP_SERVER_PORT_IPV4,
213 AclConstants.DHCP_CLIENT_PORT_IPV4, lportTag);
215 List<ActionInfo> actionsInfos = new ArrayList<>();
216 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
218 String flowName = "Ingress_DHCP_Server_v4" + dpId + "_" + lportTag + "_" + dhcpMacAddress + "_Permit_";
219 syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName, AclConstants.PROTO_DHCP_SERVER_MATCH_PRIORITY, "ACL", 0,
220 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
224 * Add rule to ensure only DHCPv6 server traffic from the specified mac is
227 * @param dpId the dpid
228 * @param dhcpMacAddress the DHCP server mac address
229 * @param lportTag the lport tag
230 * @param addOrRemove is write or delete
231 * @param protoPortMatchPriority the priority
233 protected void ingressAclDhcpv6AllowServerTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
234 int addOrRemove, Integer protoPortMatchPriority) {
235 final List<MatchInfoBase> matches = AclServiceUtils.buildDhcpV6Matches(AclConstants.DHCP_SERVER_PORT_IPV6,
236 AclConstants.DHCP_CLIENT_PORT_IPV6, lportTag);
238 List<ActionInfo> actionsInfos = new ArrayList<>();
239 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
242 "Ingress_DHCP_Server_v6" + "_" + dpId + "_" + lportTag + "_" + "_" + dhcpMacAddress + "_Permit_";
243 syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName, AclConstants.PROTO_DHCP_SERVER_MATCH_PRIORITY, "ACL", 0,
244 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
248 * Add rules to ensure that certain ICMPv6 like MLD_QUERY (130), NS (135), NA (136) are allowed into the VM.
250 * @param dpId the dpid
251 * @param lportTag the lport tag
252 * @param addOrRemove is write or delete
254 private void ingressAclIcmpv6AllowedTraffic(BigInteger dpId, int lportTag, int addOrRemove) {
255 List<ActionInfo> actionsInfos = new ArrayList<>();
256 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
258 // Allow ICMPv6 Multicast Listener Query packets.
259 List<MatchInfoBase> matches = AclServiceUtils.buildIcmpV6Matches(AclConstants.ICMPV6_TYPE_MLD_QUERY,
263 "Ingress_ICMPv6" + "_" + dpId + "_" + lportTag + "_" + AclConstants.ICMPV6_TYPE_MLD_QUERY + "_Permit_";
264 syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName, AclConstants.PROTO_IPV6_ALLOWED_PRIORITY, "ACL", 0,
265 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
267 // Allow ICMPv6 Neighbor Solicitation packets.
268 matches = AclServiceUtils.buildIcmpV6Matches(AclConstants.ICMPV6_TYPE_NS, 0, lportTag);
271 "Ingress_ICMPv6" + "_" + dpId + "_" + lportTag + "_" + AclConstants.ICMPV6_TYPE_NS + "_Permit_";
272 syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName, AclConstants.PROTO_IPV6_ALLOWED_PRIORITY, "ACL", 0,
273 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
275 // Allow ICMPv6 Neighbor Advertisement packets.
276 matches = AclServiceUtils.buildIcmpV6Matches(AclConstants.ICMPV6_TYPE_NA, 0, lportTag);
279 "Ingress_ICMPv6" + "_" + dpId + "_" + lportTag + "_" + AclConstants.ICMPV6_TYPE_NA + "_Permit_";
280 syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName, AclConstants.PROTO_IPV6_ALLOWED_PRIORITY, "ACL", 0,
281 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
285 * Adds the rule to send the packet to the netfilter to check whether it is
288 * @param dpId the dpId
289 * @param allowedAddresses the allowed addresses
290 * @param priority the priority of the flow
291 * @param flowId the flowId
292 * @param conntrackState the conntrack state of the packets thats should be
294 * @param conntrackMask the conntrack mask
295 * @param portId the portId
296 * @param addOrRemove whether to add or remove the flow
298 private void programConntrackRecircRules(BigInteger dpId, List<AllowedAddressPairs> allowedAddresses,
299 Integer priority, String flowId, int conntrackState, int conntrackMask, String portId, int addOrRemove) {
300 for (AllowedAddressPairs allowedAddress : allowedAddresses) {
301 IpPrefixOrAddress attachIp = allowedAddress.getIpAddress();
302 String attachMac = allowedAddress.getMacAddress().getValue();
304 List<MatchInfoBase> matches = new ArrayList<>();
305 matches.add(new MatchInfo(MatchFieldType.eth_type, new long[] { NwConstants.ETHTYPE_IPV4 }));
306 matches.add(new NxMatchInfo(NxMatchFieldType.ct_state, new long[] {conntrackState, conntrackMask}));
307 matches.add(new MatchInfo(MatchFieldType.eth_dst, new String[] { attachMac }));
308 matches.addAll(AclServiceUtils.buildIpMatches(attachIp, MatchFieldType.ipv4_destination));
310 List<InstructionInfo> instructions = new ArrayList<>();
311 List<ActionInfo> actionsInfos = new ArrayList<>();
313 Long elanTag = AclServiceUtils.getElanIdFromInterface(portId, dataBroker);
314 actionsInfos.add(new ActionInfo(ActionType.nx_conntrack,
315 new String[] {"0", "0", elanTag.toString(), Short.toString(
316 NwConstants.EGRESS_ACL_FILTER_TABLE)}, 2));
317 instructions.add(new InstructionInfo(InstructionType.apply_actions, actionsInfos));
318 String flowName = "Ingress_Fixed_Conntrk_Untrk_" + dpId + "_" + attachMac + "_"
319 + String.valueOf(attachIp.getValue()) + "_" + flowId;
320 syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName, AclConstants.PROTO_MATCH_PRIORITY, "ACL", 0, 0,
321 AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
326 * Program conntrack tracked rule.
328 * @param dpId the dp id
329 * @param allowedAddresses the allowed addresses
330 * @param priority the priority
331 * @param flowId the flow id
332 * @param conntrackState the conntrack state
333 * @param conntrackMask the conntrack mask
334 * @param addOrRemove the add or remove
336 private void programConntrackTrackedRule(BigInteger dpId, List<AllowedAddressPairs> allowedAddresses,
337 Integer priority, String flowId, int conntrackState, int conntrackMask, int addOrRemove) {
338 for (AllowedAddressPairs allowedAddress : allowedAddresses) {
339 IpPrefixOrAddress attachIp = allowedAddress.getIpAddress();
340 String attachMac = allowedAddress.getMacAddress().getValue();
342 List<MatchInfoBase> matches = new ArrayList<>();
343 matches.add(new MatchInfo(MatchFieldType.eth_type, new long[] {NwConstants.ETHTYPE_IPV4}));
344 matches.add(new NxMatchInfo(NxMatchFieldType.ct_state, new long[] {conntrackState, conntrackMask}));
345 matches.add(new MatchInfo(MatchFieldType.eth_dst, new String[] {attachMac}));
346 matches.addAll(AclServiceUtils.buildIpMatches(attachIp, MatchFieldType.ipv4_destination));
348 List<ActionInfo> actionsInfos = new ArrayList<>();
349 actionsInfos.add(new ActionInfo(ActionType.goto_table, new String[] {}));
351 List<InstructionInfo> instructions = new ArrayList<>();
352 instructions.add(new InstructionInfo(InstructionType.goto_table,
353 new long[] {NwConstants.EGRESS_ACL_FILTER_TABLE}));
355 String flowName = "Ingress_Fixed_Conntrk_Trk_" + dpId + "_" + attachMac + "_"
356 + String.valueOf(attachIp.getValue()) + "_" + flowId;
357 syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName, priority, "ACL", 0, 0,
358 AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
363 * Adds the rule to allow arp packets.
365 * @param dpId the dpId
366 * @param lportTag the lport tag
367 * @param addOrRemove whether to add or remove the flow
369 protected void programArpRule(BigInteger dpId, int lportTag, int addOrRemove) {
370 List<MatchInfo> matches = new ArrayList<>();
371 matches.add(new MatchInfo(MatchFieldType.eth_type, new long[] {NwConstants.ETHTYPE_ARP}));
372 matches.add(AclServiceUtils.buildLPortTagMatch(lportTag));
374 List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(new ArrayList<>());
375 String flowName = "Ingress_ARP_" + dpId + "_" + lportTag;
376 syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName,
377 AclConstants.PROTO_ARP_TRAFFIC_MATCH_PRIORITY, "ACL", 0, 0,
378 AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
382 * Programs the default connection tracking rules.
384 * @param dpid the dp id
385 * @param allowedAddresses the allowed addresses
386 * @param portId the portId
387 * @param write whether to add or remove the flow.
389 private void programIngressAclFixedConntrackRule(BigInteger dpid, List<AllowedAddressPairs> allowedAddresses,
390 String portId, Action action, int write) {
391 programConntrackRecircRules(dpid, allowedAddresses, AclConstants.CT_STATE_UNTRACKED_PRIORITY,
392 "Untracked", AclConstants.UNTRACKED_CT_STATE, AclConstants.UNTRACKED_CT_STATE_MASK, portId, write);
393 programConntrackTrackedRule(dpid, allowedAddresses, AclConstants.CT_STATE_TRACKED_EXIST_PRIORITY, "Tracked",
394 AclConstants.TRACKED_CT_STATE, AclConstants.TRACKED_CT_STATE_MASK, write);
395 LOG.info("programIngressAclFixedConntrackRule : default connection tracking rule are added.");