Action redesign: use the new ActionInfo Genius classes
[netvirt.git] / vpnservice / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / AbstractEgressAclServiceImpl.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.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;
48
49 /**
50  * Provides abstract implementation for egress (w.r.t VM) ACL service.
51  *
52  * <p>
53  * Note: Table names used are w.r.t switch. Hence, switch ingress is VM egress
54  * and vice versa.
55  */
56 public abstract class AbstractEgressAclServiceImpl extends AbstractAclServiceImpl {
57
58     private static final Logger LOG = LoggerFactory.getLogger(AbstractEgressAclServiceImpl.class);
59
60     /**
61      * Initialize the member variables.
62      *
63      * @param dataBroker the data broker instance.
64      * @param mdsalManager the mdsal manager instance.
65      * @param aclDataUtil
66      *            the acl data util.
67      * @param aclServiceUtils
68      *            the acl service util.
69      */
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);
74     }
75
76     /**
77      * Bind service.
78      *
79      * @param interfaceName the interface name
80      */
81     @Override
82     protected void bindService(String interfaceName) {
83         int flowPriority = AclConstants.EGRESS_ACL_DEFAULT_FLOW_PRIORITY;
84
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);
95     }
96
97     /**
98      * Unbind service.
99      *
100      * @param interfaceName the interface name
101      */
102     @Override
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);
109     }
110
111     @Override
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.");
115
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);
123         }
124         programArpRule(dpid, allowedAddresses, lportTag, addOrRemove);
125     }
126
127     @Override
128     protected boolean programAclRules(List<Uuid> aclUuidList, BigInteger dpId, int lportTag, int addOrRemove, String
129             portId) {
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 {}",
133                     aclUuidList, dpId);
134             return false;
135         }
136         for (Uuid sgUuid :aclUuidList ) {
137             Acl acl = AclServiceUtils.getAcl(dataBroker, sgUuid.getValue());
138             if (null == acl) {
139                 LOG.warn("The ACL is empty");
140                 continue;
141             }
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);
146             }
147         }
148         return true;
149     }
150
151     @Override
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)) {
156             return;
157         }
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,
167                     false);
168             }
169         }
170         if (null == flowMap) {
171             LOG.error("Failed to apply ACL {} lportTag {}", ace.getKey(), lportTag);
172             return;
173         }
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);
178         }
179     }
180
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);
183
184     /**
185      * Anti-spoofing rule to block the Ipv4 DHCP server traffic from the port.
186      *
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.
191      */
192     protected void egressAclDhcpDropServerTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
193             int addOrRemove) {
194         List<MatchInfoBase> matches = AclServiceUtils.buildDhcpMatches(AclConstants.DHCP_SERVER_PORT_IPV4,
195                 AclConstants.DHCP_CLIENT_PORT_IPV4, lportTag);
196
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);
204     }
205
206     /**
207      * Anti-spoofing rule to block the Ipv6 DHCP server traffic from the port.
208      *
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.
213      */
214     protected void egressAclDhcpv6DropServerTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
215             int addOrRemove) {
216         List<MatchInfoBase> matches = AclServiceUtils.buildDhcpV6Matches(AclConstants.DHCP_SERVER_PORT_IPV6,
217                 AclConstants.DHCP_CLIENT_PORT_IPV6, lportTag);
218
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);
226     }
227
228     /**
229      * Anti-spoofing rule to block the Ipv6 Router Advts from the VM port.
230      *
231      * @param dpId the dpId
232      * @param lportTag the lport tag
233      * @param addOrRemove add/remove the flow.
234      */
235     private void egressAclIcmpv6DropRouterAdvts(BigInteger dpId, int lportTag, int addOrRemove) {
236         List<MatchInfoBase> matches = AclServiceUtils.buildIcmpV6Matches(AclConstants.ICMPV6_TYPE_RA, 0, lportTag);
237
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);
244     }
245
246     /**
247      * Add rule to allow certain ICMPv6 traffic from VM ports.
248      *
249      * @param dpId the dpId
250      * @param lportTag the lport tag
251      * @param addOrRemove add/remove the flow.
252      */
253     private void egressAclIcmpv6AllowedList(BigInteger dpId, int lportTag, int addOrRemove) {
254         List<ActionInfo> actionsInfos = new ArrayList<>();
255         List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
256
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);
262         }
263     }
264
265     /**
266      * Add rule to ensure only DHCP server traffic from the specified mac is
267      * allowed.
268      *
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
273      */
274     private void egressAclDhcpAllowClientTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
275             int addOrRemove) {
276         final List<MatchInfoBase> matches = AclServiceUtils.buildDhcpMatches(AclConstants.DHCP_CLIENT_PORT_IPV4,
277                 AclConstants.DHCP_SERVER_PORT_IPV4, lportTag);
278
279         List<ActionInfo> actionsInfos = new ArrayList<>();
280         List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
281
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);
285     }
286
287     /**
288      * Add rule to ensure only DHCPv6 server traffic from the specified mac is
289      * allowed.
290      *
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
295      */
296     private void egressAclDhcpv6AllowClientTraffic(BigInteger dpId, String dhcpMacAddress, int lportTag,
297             int addOrRemove) {
298         final List<MatchInfoBase> matches = AclServiceUtils.buildDhcpV6Matches(AclConstants.DHCP_CLIENT_PORT_IPV6,
299                 AclConstants.DHCP_SERVER_PORT_IPV6, lportTag);
300
301         List<ActionInfo> actionsInfos = new ArrayList<>();
302         List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(actionsInfos);
303
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);
307     }
308
309     /**
310      * Adds the rule to allow arp packets.
311      *
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
316      */
317     protected void programArpRule(BigInteger dpId, List<AllowedAddressPairs> allowedAddresses, int lportTag,
318             int addOrRemove) {
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));
325
326             List<InstructionInfo> instructions = getDispatcherTableResubmitInstructions(new ArrayList<>());
327
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);
332         }
333     }
334 }