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