Bug 7725: AAP with prefix 0.0.0.0/0 not supported in ACL
[netvirt.git] / vpnservice / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / AbstractIngressAclServiceImpl.java
1 /*
2  * Copyright (c) 2016 HPE, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netvirt.aclservice;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.Map;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
20 import org.opendaylight.genius.mdsalutil.ActionInfo;
21 import org.opendaylight.genius.mdsalutil.InstructionInfo;
22 import org.opendaylight.genius.mdsalutil.MDSALUtil;
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.interfaces.IMdsalApiManager;
27 import org.opendaylight.genius.mdsalutil.matches.MatchEthernetType;
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.AclDataUtil;
32 import org.opendaylight.netvirt.aclservice.utils.AclServiceOFFlowBuilder;
33 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.Acl;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.access.control.list.rev160218.access.lists.acl.AccessListEntries;
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;
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;
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.AceType;
39 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;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.ServiceModeEgress;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionIngress;
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;
50
51 /**
52  * Provides abstract implementation for ingress (w.r.t VM) ACL service.
53  *
54  * <p>
55  * Note: Table names used are w.r.t switch. Hence, switch ingress is VM egress
56  * and vice versa.
57  */
58 public abstract class AbstractIngressAclServiceImpl extends AbstractAclServiceImpl {
59
60     private static final Logger LOG = LoggerFactory.getLogger(AbstractIngressAclServiceImpl.class);
61
62     /**
63      * Initialize the member variables.
64      *
65      * @param dataBroker the data broker instance.
66      * @param mdsalManager the mdsal manager.
67      * @param aclDataUtil
68      *            the acl data util.
69      * @param aclServiceUtils
70      *            the acl service util.
71      */
72     public AbstractIngressAclServiceImpl(DataBroker dataBroker, IMdsalApiManager mdsalManager, AclDataUtil aclDataUtil,
73             AclServiceUtils aclServiceUtils) {
74         // Service mode is w.rt. switch
75         super(ServiceModeEgress.class, dataBroker, mdsalManager, aclDataUtil, aclServiceUtils);
76     }
77
78     /**
79      * Bind service.
80      *
81      * @param interfaceName the interface name
82      */
83     @Override
84     protected void bindService(String interfaceName) {
85         int flowPriority = AclConstants.INGRESS_ACL_DEFAULT_FLOW_PRIORITY;
86
87         int instructionKey = 0;
88         List<Instruction> instructions = new ArrayList<>();
89         instructions.add(MDSALUtil.buildAndGetGotoTableInstruction(NwConstants.EGRESS_ACL_TABLE, ++instructionKey));
90         BoundServices serviceInfo = AclServiceUtils.getBoundServices(
91                 String.format("%s.%s.%s", "vpn", "ingressacl", interfaceName),
92                 ServiceIndex.getIndex(NwConstants.EGRESS_ACL_SERVICE_NAME, NwConstants.EGRESS_ACL_SERVICE_INDEX),
93                 flowPriority, AclConstants.COOKIE_ACL_BASE, instructions);
94         InstanceIdentifier<BoundServices> path = AclServiceUtils.buildServiceId(interfaceName,
95                 ServiceIndex.getIndex(NwConstants.EGRESS_ACL_SERVICE_NAME,
96                         NwConstants.EGRESS_ACL_SERVICE_INDEX), ServiceModeEgress.class);
97
98         DataStoreJobCoordinator dataStoreCoordinator = DataStoreJobCoordinator.getInstance();
99         dataStoreCoordinator.enqueueJob(interfaceName,
100             () -> {
101                 WriteTransaction writeTxn = dataBroker.newWriteOnlyTransaction();
102                 writeTxn.put(LogicalDatastoreType.CONFIGURATION, path, serviceInfo, true);
103
104                 List<ListenableFuture<Void>> futures = new ArrayList<>();
105                 futures.add(writeTxn.submit());
106                 return futures;
107             });
108     }
109
110     /**
111      * Unbind service.
112      *
113      * @param interfaceName the interface name
114      */
115     @Override
116     protected void unbindService(String interfaceName) {
117         InstanceIdentifier<BoundServices> path = AclServiceUtils.buildServiceId(interfaceName,
118                 ServiceIndex.getIndex(NwConstants.EGRESS_ACL_SERVICE_NAME, NwConstants.EGRESS_ACL_SERVICE_INDEX),
119                 ServiceModeEgress.class);
120
121         DataStoreJobCoordinator dataStoreCoordinator = DataStoreJobCoordinator.getInstance();
122         dataStoreCoordinator.enqueueJob(interfaceName,
123             () -> {
124                 WriteTransaction writeTxn = dataBroker.newWriteOnlyTransaction();
125                 writeTxn.delete(LogicalDatastoreType.CONFIGURATION, path);
126
127                 List<ListenableFuture<Void>> futures = new ArrayList<>();
128                 futures.add(writeTxn.submit());
129                 return futures;
130             });
131     }
132
133     /**
134      * Program conntrack rules.
135      *
136      * @param dpid the dpid
137      * @param dhcpMacAddress the dhcp mac address.
138      * @param allowedAddresses the allowed addresses
139      * @param lportTag the lport tag
140      * @param addOrRemove add or remove the flow
141      */
142     @Override
143     protected abstract void programSpecificFixedRules(BigInteger dpid, String dhcpMacAddress,
144             List<AllowedAddressPairs> allowedAddresses, int lportTag, String portId, Action action, int addOrRemove);
145
146     @Override
147     protected void programGeneralFixedRules(BigInteger dpid, String dhcpMacAddress,
148             List<AllowedAddressPairs> allowedAddresses, int lportTag, Action action, int addOrRemove) {
149         LOG.info("programFixedRules : {} default rules.", action == Action.ADD ? "adding" : "removing");
150
151         if (action == Action.ADD || action == Action.REMOVE) {
152             ingressAclDhcpAllowServerTraffic(dpid, dhcpMacAddress, lportTag, addOrRemove,
153                     AclConstants.PROTO_PREFIX_MATCH_PRIORITY);
154             ingressAclDhcpv6AllowServerTraffic(dpid, dhcpMacAddress, lportTag, addOrRemove,
155                     AclConstants.PROTO_PREFIX_MATCH_PRIORITY);
156             ingressAclIcmpv6AllowedTraffic(dpid, lportTag, addOrRemove);
157         }
158         programArpRule(dpid, lportTag, addOrRemove);
159     }
160
161     @Override
162     protected boolean programAclRules(List<Uuid> aclUuidList, BigInteger dpId, int lportTag, int addOrRemove, String
163             portId) {
164         if (aclUuidList == null || dpId == null) {
165             LOG.warn("one of the ingress acl parameters can not be null. sg {}, dpId {}",
166                     aclUuidList, dpId);
167             return false;
168         }
169
170         for (Uuid sgUuid :aclUuidList) {
171             Acl acl = AclServiceUtils.getAcl(dataBroker, sgUuid.getValue());
172             if (null == acl) {
173                 LOG.warn("The ACL is empty");
174                 continue;
175             }
176             AccessListEntries accessListEntries = acl.getAccessListEntries();
177             List<Ace> aceList = accessListEntries.getAce();
178             for (Ace ace : aceList) {
179                 programAceRule(dpId, lportTag, addOrRemove, acl.getAclName(), ace, portId, null);
180             }
181         }
182         return true;
183     }
184
185     @Override
186     protected void programAceRule(BigInteger dpId, int lportTag, int addOrRemove, String aclName, Ace ace,
187             String portId, List<AllowedAddressPairs> syncAllowedAddresses) {
188         SecurityRuleAttr aceAttr = AclServiceUtils.getAccesssListAttributes(ace);
189         if (!aceAttr.getDirection().equals(DirectionIngress.class)) {
190             return;
191         }
192         Matches matches = ace.getMatches();
193         AceType aceType = matches.getAceType();
194         Map<String,List<MatchInfoBase>> flowMap = null;
195         if (aceType instanceof AceIp) {
196             flowMap = AclServiceOFFlowBuilder.programIpFlow(matches);
197             if (syncAllowedAddresses != null) {
198                 flowMap = AclServiceUtils.getFlowForAllowedAddresses(syncAllowedAddresses, flowMap, true);
199             } else if (aceAttr.getRemoteGroupId() != null) {
200                 flowMap = aclServiceUtils.getFlowForRemoteAcl(aceAttr.getRemoteGroupId(), portId, flowMap, true);
201             }
202         }
203         if (null == flowMap) {
204             LOG.error("Failed to apply ACL {} lportTag {}", ace.getKey(), lportTag);
205             return;
206         }
207         for (String flowName : flowMap.keySet()) {
208             syncSpecificAclFlow(dpId, lportTag, addOrRemove, ace, portId, flowMap, flowName);
209         }
210     }
211
212     protected abstract String syncSpecificAclFlow(BigInteger dpId, int lportTag, int addOrRemove, Ace ace,
213             String portId, Map<String, List<MatchInfoBase>> flowMap, String flowName);
214
215     /**
216      * Add rule to ensure only DHCP server traffic from the specified mac is
217      * allowed.
218      *
219      * @param dpId the dpid
220      * @param dhcpMacAddress the DHCP server mac address
221      * @param lportTag the lport tag
222      * @param addOrRemove is write or delete
223      * @param protoPortMatchPriority the priority
224      */
225     protected void ingressAclDhcpAllowServerTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
226             int addOrRemove, int protoPortMatchPriority) {
227         final List<MatchInfoBase> matches = AclServiceUtils.buildDhcpMatches(AclConstants.DHCP_SERVER_PORT_IPV4,
228                 AclConstants.DHCP_CLIENT_PORT_IPV4, lportTag);
229
230         List<ActionInfo> actionsInfos = new ArrayList<>();
231         List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
232
233         String flowName = "Ingress_DHCP_Server_v4" + dpId + "_" + lportTag + "_" + dhcpMacAddress + "_Permit_";
234         syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName, AclConstants.PROTO_DHCP_SERVER_MATCH_PRIORITY, "ACL", 0,
235                 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
236     }
237
238     /**
239      * Add rule to ensure only DHCPv6 server traffic from the specified mac is
240      * allowed.
241      *
242      * @param dpId the dpid
243      * @param dhcpMacAddress the DHCP server mac address
244      * @param lportTag the lport tag
245      * @param addOrRemove is write or delete
246      * @param protoPortMatchPriority the priority
247      */
248     protected void ingressAclDhcpv6AllowServerTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
249             int addOrRemove, Integer protoPortMatchPriority) {
250         final List<MatchInfoBase> matches = AclServiceUtils.buildDhcpV6Matches(AclConstants.DHCP_SERVER_PORT_IPV6,
251                 AclConstants.DHCP_CLIENT_PORT_IPV6, lportTag);
252
253         List<ActionInfo> actionsInfos = new ArrayList<>();
254         List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
255
256         String flowName =
257                 "Ingress_DHCP_Server_v6" + "_" + dpId + "_" + lportTag + "_" + "_" + dhcpMacAddress + "_Permit_";
258         syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName, AclConstants.PROTO_DHCP_SERVER_MATCH_PRIORITY, "ACL", 0,
259                 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
260     }
261
262     /**
263      * Add rules to ensure that certain ICMPv6 like MLD_QUERY (130), NS (135), NA (136) are allowed into the VM.
264      *
265      * @param dpId the dpid
266      * @param lportTag the lport tag
267      * @param addOrRemove is write or delete
268      */
269     private void ingressAclIcmpv6AllowedTraffic(BigInteger dpId, int lportTag, int addOrRemove) {
270         List<ActionInfo> actionsInfos = new ArrayList<>();
271         List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
272
273         // Allow ICMPv6 Multicast Listener Query packets.
274         List<MatchInfoBase> matches = AclServiceUtils.buildIcmpV6Matches(AclConstants.ICMPV6_TYPE_MLD_QUERY,
275                 0, lportTag);
276
277         String flowName =
278                 "Ingress_ICMPv6" + "_" + dpId + "_" + lportTag + "_" + AclConstants.ICMPV6_TYPE_MLD_QUERY + "_Permit_";
279         syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName, AclConstants.PROTO_IPV6_ALLOWED_PRIORITY, "ACL", 0,
280                 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
281
282         // Allow ICMPv6 Neighbor Solicitation packets.
283         matches = AclServiceUtils.buildIcmpV6Matches(AclConstants.ICMPV6_TYPE_NS, 0, lportTag);
284
285         flowName =
286                 "Ingress_ICMPv6" + "_" + dpId + "_" + lportTag + "_" + AclConstants.ICMPV6_TYPE_NS + "_Permit_";
287         syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName, AclConstants.PROTO_IPV6_ALLOWED_PRIORITY, "ACL", 0,
288                 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
289
290         // Allow ICMPv6 Neighbor Advertisement packets.
291         matches = AclServiceUtils.buildIcmpV6Matches(AclConstants.ICMPV6_TYPE_NA, 0, lportTag);
292
293         flowName =
294                 "Ingress_ICMPv6" + "_" + dpId + "_" + lportTag + "_" + AclConstants.ICMPV6_TYPE_NA + "_Permit_";
295         syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName, AclConstants.PROTO_IPV6_ALLOWED_PRIORITY, "ACL", 0,
296                 0, AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
297     }
298
299     /**
300      * Adds the rule to allow arp packets.
301      *
302      * @param dpId the dpId
303      * @param lportTag the lport tag
304      * @param addOrRemove whether to add or remove the flow
305      */
306     protected void programArpRule(BigInteger dpId, int lportTag, int addOrRemove) {
307         List<MatchInfo> matches = new ArrayList<>();
308         matches.add(MatchEthernetType.ARP);
309         matches.add(AclServiceUtils.buildLPortTagMatch(lportTag));
310
311         List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(new ArrayList<>());
312         String flowName = "Ingress_ARP_" + dpId + "_" + lportTag;
313         syncFlow(dpId, NwConstants.EGRESS_ACL_TABLE, flowName,
314                 AclConstants.PROTO_ARP_TRAFFIC_MATCH_PRIORITY, "ACL", 0, 0,
315                 AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
316     }
317 }