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.utils.AclConstants;
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.rpcs.rev160406.OdlInterfaceRpcService;
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.yangtools.yang.binding.InstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class EgressAclServiceImpl extends AbstractAclServiceImpl {
52 private static final Logger LOG = LoggerFactory.getLogger(EgressAclServiceImpl.class);
53 private final DataBroker dataBroker;
56 * Initialize the member variables.
57 * @param dataBroker the data broker instance.
58 * @param interfaceManager the interface manager instance.
59 * @param mdsalManager the mdsal manager instance.
61 public EgressAclServiceImpl(DataBroker dataBroker, OdlInterfaceRpcService interfaceManager,
62 IMdsalApiManager mdsalManager) {
63 super(dataBroker,interfaceManager,mdsalManager);
64 this.dataBroker = dataBroker;
70 * @param interfaceName the interface name
72 protected void bindService(String interfaceName) {
73 int flowPriority = AclConstants.EGRESS_ACL_DEFAULT_FLOW_PRIORITY;
75 int instructionKey = 0;
76 List<Instruction> instructions = new ArrayList<>();
77 instructions.add(MDSALUtil.buildAndGetGotoTableInstruction(AclConstants.EGRESS_ACL_TABLE_ID, ++instructionKey));
78 BoundServices serviceInfo = AclServiceUtils.getBoundServices(
79 String.format("%s.%s.%s", "vpn", "egressacl", interfaceName), AclConstants.EGRESS_ACL_SERVICE_PRIORITY,
80 flowPriority, AclConstants.COOKIE_ACL_BASE, instructions);
81 InstanceIdentifier<BoundServices> path = AclServiceUtils.buildServiceId(interfaceName,
82 AclConstants.EGRESS_ACL_SERVICE_PRIORITY, ServiceModeIngress.class);
83 MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, path, serviceInfo);
89 * @param interfaceName the interface name
91 protected void unbindService(String interfaceName) {
92 InstanceIdentifier<BoundServices> path = AclServiceUtils.buildServiceId(interfaceName,
93 AclConstants.EGRESS_ACL_SERVICE_PRIORITY, ServiceModeIngress.class);
94 MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION, path);
98 * Program the default anti-spoofing rule and the conntrack rules.
100 * @param dpid the dpid
101 * @param dhcpMacAddress the dhcp mac address.
102 * @param attachMac The vm mac address
103 * @param addOrRemove addorRemove
105 protected void programFixedRules(BigInteger dpid, String dhcpMacAddress,
106 String attachMac, int addOrRemove) {
107 LOG.info("programFixedRules : adding default rules.");
108 egressAclDhcpAllowClientTraffic(dpid, dhcpMacAddress, attachMac, addOrRemove);
109 egressAclDhcpv6AllowClientTraffic(dpid, dhcpMacAddress, attachMac, addOrRemove);
110 egressAclDhcpDropServerTraffic(dpid, dhcpMacAddress, attachMac, addOrRemove);
111 egressAclDhcpv6DropServerTraffic(dpid, dhcpMacAddress, attachMac, addOrRemove);
113 //if (securityServicesManager.isConntrackEnabled()) {
114 programEgressAclFixedConntrackRule(dpid, attachMac, addOrRemove);
116 programArpRule(dpid,attachMac, addOrRemove);
120 * Programs the custom flows.
122 * @param aclUuidList the list of acl uuid to be applied
123 * @param dpId the dpId
124 * @param attachMac the attached mac
125 * @param addOrRemove whether to delete or add flow
127 protected void programAclRules(List<Uuid> aclUuidList, BigInteger dpId, String attachMac,
128 IpPrefixOrAddress attachIp, int addOrRemove) {
129 LOG.trace("Applying custom rules DpId {}, vmMacAddress {}", dpId, attachMac );
130 for (Uuid sgUuid :aclUuidList ) {
131 Acl acl = AclServiceUtils.getAcl(dataBroker, sgUuid.getValue());
132 AccessListEntries accessListEntries = acl.getAccessListEntries();
133 List<Ace> aceList = accessListEntries.getAce();
134 for (Ace ace: aceList) {
135 programAceRule(dpId, attachMac, attachIp, addOrRemove, ace);
141 protected void programAceRule(BigInteger dpId, String attachMac, IpPrefixOrAddress attachIp, int addOrRemove,
143 SecurityRuleAttr aceAttr = AclServiceUtils.getAccesssListAttributes(ace);
144 if (!aceAttr.getDirection().equals(DirectionEgress.class)) {
147 Matches matches = ace.getMatches();
148 AceType aceType = matches.getAceType();
149 Map<String,List<MatchInfoBase>> flowMap = null;
150 if (aceType instanceof AceIp) {
151 flowMap = AclServiceOFFlowBuilder.programIpFlow(matches);
153 if (null == flowMap) {
154 LOG.error("Failed to apply ACL {} vmMacAddress {}", ace.getKey(), attachMac);
157 //The flow map contains list of flows if port range is selected.
158 for ( String flowName : flowMap.keySet()) {
159 List<MatchInfoBase> flows = flowMap.get(flowName);
160 flowName += "Egress" + attachMac + String.valueOf(attachIp.getValue()) + ace.getKey().getRuleName();
161 flows .add(new MatchInfo(MatchFieldType.eth_src,
162 new String[] { attachMac }));
163 flows.addAll(AclServiceUtils.getAllowedIpMatches(attachIp, MatchFieldType.ipv4_source));
164 List<InstructionInfo> instructions = new ArrayList<>();
165 List<ActionInfo> actionsInfos = new ArrayList<>();
166 actionsInfos.add(new ActionInfo(ActionType.nx_conntrack,
167 new String[] {"1", "0", "0", "255"}, 2));
168 instructions.add(new InstructionInfo(InstructionType.apply_actions,
170 instructions.add(new InstructionInfo(InstructionType.goto_table,
171 new long[] { AclConstants.EGRESS_ACL_NEXT_TABLE_ID }));
172 syncFlow(dpId, AclConstants.EGRESS_ACL_TABLE_ID, flowName, AclConstants.PROTO_MATCH_PRIORITY,
173 "ACL", 0, 0, AclConstants.COOKIE_ACL_BASE, flows, instructions, addOrRemove);
178 * Anti-spoofing rule to block the Ipv4 DHCP server traffic from the port.
179 * @param dpId the dpId
180 * @param dhcpMacAddress the Dhcp mac address
181 * @param attachMac the attached mac address
182 * @param addOrRemove add/remove the flow.
184 private void egressAclDhcpDropServerTraffic(BigInteger dpId, String dhcpMacAddress,
185 String attachMac, int addOrRemove) {
186 List<MatchInfoBase> matches = AclServiceUtils.buildDhcpSourceMatches(AclConstants.DHCP_SERVER_PORT_IPV4,
187 AclConstants.DHCP_CLIENT_PORT_IPV4, attachMac);
189 List<InstructionInfo> instructions = new ArrayList<>();
191 List<ActionInfo> actionsInfos = new ArrayList<>();
193 actionsInfos.add(new ActionInfo(ActionType.drop_action,
195 String flowName = "Egress_DHCP_Server_v4" + dpId + "_" + attachMac + "_" + dhcpMacAddress + "_Drop_";
196 syncFlow(dpId, AclConstants.EGRESS_ACL_TABLE_ID, flowName, AclConstants.PROTO_MATCH_PRIORITY, "ACL", 0, 0,
197 AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
201 * Anti-spoofing rule to block the Ipv6 DHCP server traffic from the port.
202 * @param dpId the dpId
203 * @param dhcpMacAddress the Dhcp mac address
204 * @param attachMac the attached mac address
205 * @param addOrRemove add/remove the flow.
207 private void egressAclDhcpv6DropServerTraffic(BigInteger dpId, String dhcpMacAddress,
208 String attachMac, int addOrRemove) {
209 List<MatchInfoBase> matches = AclServiceUtils.buildDhcpSourceMatches(AclConstants.DHCP_SERVER_PORT_IPV6,
210 AclConstants.DHCP_CLIENT_PORT_IPV6, attachMac);
212 List<InstructionInfo> instructions = new ArrayList<>();
214 List<ActionInfo> actionsInfos = new ArrayList<>();
216 actionsInfos.add(new ActionInfo(ActionType.drop_action,
218 String flowName = "Egress_DHCP_Server_v6" + "_" + dpId + "_" + attachMac + "_" + dhcpMacAddress + "_Drop_";
219 syncFlow(dpId, AclConstants.EGRESS_ACL_TABLE_ID, flowName, AclConstants.PROTO_MATCH_PRIORITY, "ACL", 0, 0,
220 AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
224 * Add rule to ensure only DHCP server traffic from the specified mac is allowed.
226 * @param dpId the dpid
227 * @param dhcpMacAddress the DHCP server mac address
228 * @param attachMac the mac address of the port
229 * @param addOrRemove whether to add or remove the flow
231 private void egressAclDhcpAllowClientTraffic(BigInteger dpId, String dhcpMacAddress,
232 String attachMac, int addOrRemove) {
233 final List<MatchInfoBase> matches =
234 AclServiceUtils.buildDhcpSourceMatches(AclConstants.DHCP_CLIENT_PORT_IPV4,
235 AclConstants.DHCP_SERVER_PORT_IPV4, attachMac);
237 List<InstructionInfo> instructions = new ArrayList<>();
239 List<ActionInfo> actionsInfos = new ArrayList<>();
241 actionsInfos.add(new ActionInfo(ActionType.nx_conntrack,
242 new String[] {"1", "0", "0", "255"}, 2));
243 instructions.add(new InstructionInfo(InstructionType.apply_actions,
247 instructions.add(new InstructionInfo(InstructionType.goto_table,
248 new long[] { AclConstants.EGRESS_ACL_NEXT_TABLE_ID }));
249 String flowName = "Egress_DHCP_Client_v4" + dpId + "_" + attachMac + "_" + dhcpMacAddress + "_Permit_";
250 syncFlow(dpId, AclConstants.EGRESS_ACL_TABLE_ID, flowName, AclConstants.PROTO_MATCH_PRIORITY, "ACL", 0, 0,
251 AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
255 * Add rule to ensure only DHCPv6 server traffic from the specified mac is allowed.
257 * @param dpId the dpid
258 * @param dhcpMacAddress the DHCP server mac address
259 * @param attachMac the mac address of the port
260 * @param addOrRemove whether to add or remove the flow
262 private void egressAclDhcpv6AllowClientTraffic(BigInteger dpId, String dhcpMacAddress,
263 String attachMac, int addOrRemove) {
264 final List<MatchInfoBase> matches =
265 AclServiceUtils.buildDhcpSourceMatches(AclConstants.DHCP_CLIENT_PORT_IPV6,
266 AclConstants.DHCP_SERVER_PORT_IPV6, attachMac);
268 List<InstructionInfo> instructions = new ArrayList<>();
270 List<ActionInfo> actionsInfos = new ArrayList<>();
272 actionsInfos.add(new ActionInfo(ActionType.nx_conntrack,
273 new String[] {"1", "0", "0", "255"}, 2));
274 instructions.add(new InstructionInfo(InstructionType.apply_actions,
277 instructions.add(new InstructionInfo(InstructionType.goto_table,
278 new long[] { AclConstants.EGRESS_ACL_NEXT_TABLE_ID }));
279 String flowName = "Egress_DHCP_Client_v6" + "_" + dpId + "_" + attachMac + "_" + dhcpMacAddress + "_Permit_";
280 syncFlow(dpId, AclConstants.EGRESS_ACL_TABLE_ID, flowName, AclConstants.PROTO_MATCH_PRIORITY, "ACL", 0, 0,
281 AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
285 * Adds the rule to send the packet to the netfilter to check whether it is a known packet.
286 * @param dpId the dpId
287 * @param attachMac the attached mac address
288 * @param priority the priority of the flow
289 * @param flowId the flowId
290 * @param conntrackState the conntrack state of the packets thats should be send
291 * @param conntrackMask the conntrack mask
292 * @param addOrRemove whether to add or remove the flow
294 private void programConntrackRecircRule(BigInteger dpId, String attachMac, Integer priority, String flowId,
295 int conntrackState, int conntrackMask, int addOrRemove) {
296 List<MatchInfoBase> matches = new ArrayList<>();
297 matches.add(new MatchInfo(MatchFieldType.eth_type,
298 new long[] { NwConstants.ETHTYPE_IPV4 }));
299 matches.add(new NxMatchInfo(NxMatchFieldType.ct_state,
300 new long[] {conntrackState, conntrackMask}));
301 matches.add(new MatchInfo(MatchFieldType.eth_src,
302 new String[] { attachMac }));
303 List<InstructionInfo> instructions = new ArrayList<>();
305 List<ActionInfo> actionsInfos = new ArrayList<>();
307 actionsInfos.add(new ActionInfo(ActionType.nx_conntrack,
308 new String[] {"0", "0", "0", Short.toString(AclConstants.EGRESS_ACL_TABLE_ID)}, 2));
309 instructions.add(new InstructionInfo(InstructionType.apply_actions,
311 String flowName = "Egress_Fixed_Conntrk_Untrk_" + dpId + "_" + attachMac + "_" + flowId;
312 syncFlow(dpId, AclConstants.EGRESS_ACL_TABLE_ID, flowName, AclConstants.PROTO_MATCH_PRIORITY, "ACL", 0, 0,
313 AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
317 * Adds the rule to forward the packets known packets.
318 * @param dpId the dpId
319 * @param attachMac the attached mac address
320 * @param priority the priority of the flow
321 * @param flowId the flowId
322 * @param conntrackState the conntrack state of the packets thats should be send
323 * @param conntrackMask the conntrack mask
324 * @param addOrRemove whether to add or remove the flow
326 private void programConntrackForwardRule(BigInteger dpId, String attachMac, Integer priority, String flowId,
327 int conntrackState, int conntrackMask, int addOrRemove) {
328 List<MatchInfoBase> matches = new ArrayList<>();
329 matches.add(new MatchInfo(MatchFieldType.eth_type,
330 new long[] { NwConstants.ETHTYPE_IPV4 }));
331 matches.add(new NxMatchInfo(NxMatchFieldType.ct_state,
332 new long[] {conntrackState, conntrackMask}));
333 matches.add(new MatchInfo(MatchFieldType.eth_src,
334 new String[] { attachMac }));
335 List<InstructionInfo> instructions = new ArrayList<>();
337 List<ActionInfo> actionsInfos = new ArrayList<>();
339 actionsInfos.add(new ActionInfo(ActionType.goto_table,
342 instructions.add(new InstructionInfo(InstructionType.goto_table,
343 new long[] { AclConstants.EGRESS_ACL_NEXT_TABLE_ID }));
344 String flowName = "Egress_Fixed_Conntrk_Untrk_" + dpId + "_" + attachMac + "_" + flowId;
345 syncFlow(dpId, AclConstants.EGRESS_ACL_TABLE_ID, flowName, priority, "ACL", 0, 0,
346 AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
350 * Adds the rule to drop the unknown/invalid packets .
351 * @param dpId the dpId
352 * @param attachMac the attached mac address
353 * @param priority the priority of the flow
354 * @param flowId the flowId
355 * @param conntrackState the conntrack state of the packets thats should be send
356 * @param conntrackMask the conntrack mask
357 * @param addOrRemove whether to add or remove the flow
359 private void programConntrackDropRule(BigInteger dpId, String attachMac, Integer priority, String flowId,
360 int conntrackState, int conntrackMask, int addOrRemove) {
361 List<MatchInfoBase> matches = new ArrayList<>();
362 matches.add(new MatchInfo(MatchFieldType.eth_type,
363 new long[] { NwConstants.ETHTYPE_IPV4 }));
364 matches.add(new NxMatchInfo(NxMatchFieldType.ct_state,
365 new long[] { conntrackState, conntrackMask}));
366 matches.add(new MatchInfo(MatchFieldType.eth_src,
367 new String[] { attachMac }));
368 List<InstructionInfo> instructions = new ArrayList<>();
370 List<ActionInfo> actionsInfos = new ArrayList<>();
372 actionsInfos.add(new ActionInfo(ActionType.drop_action,
374 String flowName = "Egress_Fixed_Conntrk_NewDrop_" + dpId + "_" + attachMac + "_" + flowId;
375 syncFlow(dpId, AclConstants.EGRESS_ACL_TABLE_ID, flowName, priority, "ACL", 0, 0,
376 AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
380 * Adds the rule to allow arp packets.
381 * @param dpId the dpId
382 * @param attachMac the attached mac address
383 * @param addOrRemove whether to add or remove the flow
385 private void programArpRule(BigInteger dpId, String attachMac, int addOrRemove) {
386 List<MatchInfo> matches = new ArrayList<>();
387 matches.add(new MatchInfo(MatchFieldType.eth_type,
388 new long[] { NwConstants.ETHTYPE_ARP }));
389 matches.add(new MatchInfo(MatchFieldType.arp_sha,
390 new String[] { attachMac }));
392 List<InstructionInfo> instructions = new ArrayList<>();
394 List<ActionInfo> actionsInfos = new ArrayList<>();
396 actionsInfos.add(new ActionInfo(ActionType.goto_table,
399 instructions.add(new InstructionInfo(InstructionType.goto_table,
400 new long[] { AclConstants.EGRESS_ACL_NEXT_TABLE_ID }));
401 String flowName = "Egress_ARP_" + dpId + "_" + attachMac ;
402 syncFlow(dpId, AclConstants.EGRESS_ACL_TABLE_ID, flowName, AclConstants.PROTO_MATCH_PRIORITY, "ACL", 0, 0,
403 AclConstants.COOKIE_ACL_BASE, matches, instructions, addOrRemove);
407 * Programs the default connection tracking rules.
408 * @param dpid the dp id
409 * @param attachMac the attached mac address
410 * @param write whether to add or remove the flow.
412 private void programEgressAclFixedConntrackRule(BigInteger dpid, String attachMac, int write) {
413 programConntrackRecircRule(dpid, attachMac,AclConstants.CT_STATE_UNTRACKED_PRIORITY,
414 "Untracked",AclConstants.UNTRACKED_CT_STATE,AclConstants.UNTRACKED_CT_STATE_MASK, write );
415 programConntrackForwardRule(dpid, attachMac, AclConstants.CT_STATE_TRACKED_EXIST_PRIORITY,
416 "Tracked_Established", AclConstants.TRACKED_EST_CT_STATE, AclConstants.TRACKED_CT_STATE_MASK,
418 programConntrackForwardRule(dpid, attachMac, AclConstants.CT_STATE_TRACKED_EXIST_PRIORITY,
419 "Tracked_Related", AclConstants.TRACKED_REL_CT_STATE, AclConstants.TRACKED_CT_STATE_MASK, write );
420 programConntrackDropRule(dpid, attachMac, AclConstants.CT_STATE_NEW_PRIORITY_DROP,
421 "Tracked_New", AclConstants.TRACKED_NEW_CT_STATE, AclConstants.TRACKED_NEW_CT_STATE_MASK, write );
422 programConntrackDropRule(dpid, attachMac, AclConstants.CT_STATE_NEW_PRIORITY_DROP,
423 "Tracked_Invalid",AclConstants.TRACKED_INV_CT_STATE, AclConstants.TRACKED_INV_CT_STATE_MASK,
425 LOG.info("programEgressAclFixedConntrackRule : default connection tracking rule are added.");