2 * Copyright (c) 2014 Cisco Systems, 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
9 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;
14 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
15 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager.Dirty;
16 import org.opendaylight.groupbasedpolicy.resolver.EgKey;
17 import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3Address;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation.LocationType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Manage the table that enforces port security
43 public class PortSecurity extends FlowTable {
44 protected static final Logger LOG =
45 LoggerFactory.getLogger(PortSecurity.class);
47 public static final short TABLE_ID = 0;
49 public PortSecurity(OfTable.OfTableCtx ctx) {
54 public short getTableId() {
59 public void sync(ReadWriteTransaction t,
60 InstanceIdentifier<Table> tiid,
61 Map<String, FlowCtx> flowMap,
62 NodeId nodeId, PolicyInfo policyInfo, Dirty dirty) {
63 // Allow traffic from tunnel and external ports
64 NodeConnectorId tunnelIf = ctx.switchManager.getTunnelPort(nodeId);
66 allowFromPort(t, tiid, flowMap, tunnelIf);
67 Set<NodeConnectorId> external =
68 ctx.switchManager.getExternalPorts(nodeId);
69 for (NodeConnectorId extIf: external) {
70 allowFromPort(t, tiid, flowMap, extIf);
74 dropFlow(t, tiid, flowMap, 1, null);
76 // Drop IP traffic that doesn't match a source IP rule
77 dropFlow(t, tiid, flowMap, 110, FlowUtils.ARP);
78 dropFlow(t, tiid, flowMap, 111, FlowUtils.IPv4);
79 dropFlow(t, tiid, flowMap, 112, FlowUtils.IPv6);
81 for (EgKey sepg : ctx.epManager.getGroupsForNode(nodeId)) {
82 for (Endpoint e : ctx.epManager.getEPsForNode(nodeId, sepg)) {
83 OfOverlayContext ofc = e.getAugmentation(OfOverlayContext.class);
84 if (ofc != null && ofc.getNodeConnectorId() != null &&
85 (ofc.getLocationType() == null ||
86 LocationType.Internal.equals(ofc.getLocationType()))) {
87 // Allow layer 3 traffic (ARP and IP) with the correct
88 // source IP, MAC, and source port
89 l3flow(t, tiid, flowMap, e, ofc, 120, false);
90 l3flow(t, tiid, flowMap, e, ofc, 121, true);
91 l3DhcpDoraFlow(t, tiid, flowMap, e, ofc, 115);
93 // Allow layer 2 traffic with the correct source MAC and
94 // source port (note lower priority than drop IP rules)
95 l2flow(t, tiid, flowMap, e, ofc, 100);
101 private void allowFromPort(ReadWriteTransaction t,
102 InstanceIdentifier<Table> tiid,
103 Map<String, FlowCtx> flowMap,
104 NodeConnectorId port) {
105 FlowId flowid = new FlowId(new StringBuilder()
107 .append(port.getValue())
109 if (visit(flowMap, flowid.getValue())) {
110 FlowBuilder flowb = base()
112 .setPriority(Integer.valueOf(200))
113 .setMatch(new MatchBuilder()
116 .setInstructions(FlowUtils.gotoTableInstructions((short)(getTableId()+1)));
117 writeFlow(t, tiid, flowb.build());
121 private void l2flow(ReadWriteTransaction t,
122 InstanceIdentifier<Table> tiid,
123 Map<String, FlowCtx> flowMap,
124 Endpoint e, OfOverlayContext ofc,
126 FlowId flowid = new FlowId(new StringBuilder()
127 .append(ofc.getNodeConnectorId().getValue())
129 .append(e.getMacAddress().getValue())
131 if (visit(flowMap, flowid.getValue())) {
132 FlowBuilder flowb = base()
133 .setPriority(priority)
135 .setMatch(new MatchBuilder()
136 .setEthernetMatch(FlowUtils.ethernetMatch(e.getMacAddress(),
138 .setInPort(ofc.getNodeConnectorId())
140 .setInstructions(FlowUtils.gotoTableInstructions((short)(TABLE_ID + 1)));
142 writeFlow(t, tiid, flowb.build());
146 private void l3DhcpDoraFlow(ReadWriteTransaction t,
147 InstanceIdentifier<Table> tiid,
148 Map<String, FlowCtx> flowMap,
149 Endpoint e, OfOverlayContext ofc,
152 Long etherType= FlowUtils.IPv4;
153 // DHCP DORA destination is broadcast
154 String ikey="255.255.255.255/32";
155 Layer3Match m=new Ipv4MatchBuilder().setIpv4Destination(new Ipv4Prefix(ikey)).build();
157 FlowId flowid = new FlowId(new StringBuilder()
158 .append(ofc.getNodeConnectorId().getValue())
160 .append(e.getMacAddress().getValue())
164 if (visit(flowMap, flowid.getValue())) {
166 .setPriority(priority)
168 .setMatch(new MatchBuilder()
169 .setEthernetMatch(FlowUtils.ethernetMatch(e.getMacAddress(),
173 .setInPort(ofc.getNodeConnectorId())
175 .setInstructions(FlowUtils.gotoTableInstructions((short)(TABLE_ID + 1)))
178 writeFlow(t, tiid, flow);
182 private void l3flow(ReadWriteTransaction t,
183 InstanceIdentifier<Table> tiid,
184 Map<String, FlowCtx> flowMap,
185 Endpoint e, OfOverlayContext ofc,
188 if (e.getL3Address() == null) return;
189 for (L3Address l3 : e.getL3Address()) {
190 if (l3.getIpAddress() == null) continue;
191 Layer3Match m = null;
192 Long etherType = null;
194 if (l3.getIpAddress().getIpv4Address() != null) {
195 ikey = l3.getIpAddress().getIpv4Address().getValue()+"/32";
197 m = new ArpMatchBuilder()
198 .setArpSourceTransportAddress(new Ipv4Prefix(ikey))
200 etherType = FlowUtils.ARP;
202 m = new Ipv4MatchBuilder()
203 .setIpv4Source(new Ipv4Prefix(ikey))
205 etherType = FlowUtils.IPv4;
207 } else if (l3.getIpAddress().getIpv6Address() != null) {
209 ikey = l3.getIpAddress().getIpv6Address().getValue()+"/128";
210 m = new Ipv6MatchBuilder()
211 .setIpv6Source(new Ipv6Prefix(ikey))
213 etherType = FlowUtils.IPv6;
217 FlowId flowid = new FlowId(new StringBuilder()
218 .append(ofc.getNodeConnectorId().getValue())
220 .append(e.getMacAddress().getValue())
226 if (visit(flowMap, flowid.getValue())) {
228 .setPriority(priority)
230 .setMatch(new MatchBuilder()
231 .setEthernetMatch(FlowUtils.ethernetMatch(e.getMacAddress(),
235 .setInPort(ofc.getNodeConnectorId())
237 .setInstructions(FlowUtils.gotoTableInstructions((short)(TABLE_ID + 1)))
240 writeFlow(t, tiid, flow);