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;
13 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
14 import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfWriter;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3Address;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation.LocationType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Manage the table that enforces port security
40 public class PortSecurity extends FlowTable {
41 protected static final Logger LOG =
42 LoggerFactory.getLogger(PortSecurity.class);
44 public static short TABLE_ID;
46 public PortSecurity(OfContext ctx, short tableId) {
48 this.TABLE_ID=tableId;
52 public short getTableId() {
57 public void sync(NodeId nodeId, OfWriter ofWriter) {
59 // Allow traffic from tunnel ports
60 NodeConnectorId tunnelIf = ctx.getSwitchManager().getTunnelPort(nodeId, TunnelTypeVxlan.class);
62 ofWriter.writeFlow(nodeId, TABLE_ID, allowFromPort(tunnelIf));
64 // Allow traffic from tunnel ports
65 //TODO Bug 3546 - Difficult: External port is unrelated to Tenant, L3C, L2BD..
67 Set<NodeConnectorId> external =
68 ctx.getSwitchManager().getExternalPorts(nodeId);
69 for (NodeConnectorId extIf : external) {
70 ofWriter.writeFlow(nodeId, TABLE_ID, allowFromExternalPort(extIf));
74 ofWriter.writeFlow(nodeId, TABLE_ID, dropFlow(Integer.valueOf(1), null, TABLE_ID));
76 // Drop IP traffic that doesn't match a source IP rule
77 ofWriter.writeFlow(nodeId, TABLE_ID, dropFlow(Integer.valueOf(110), FlowUtils.ARP, TABLE_ID));
78 ofWriter.writeFlow(nodeId, TABLE_ID, dropFlow(Integer.valueOf(111), FlowUtils.IPv4, TABLE_ID));
79 ofWriter.writeFlow(nodeId, TABLE_ID, dropFlow(Integer.valueOf(112), FlowUtils.IPv6, TABLE_ID));
81 for (Endpoint ep : ctx.getEndpointManager().getEndpointsForNode(nodeId)) {
82 OfOverlayContext ofc = ep.getAugmentation(OfOverlayContext.class);
84 if (ofc != null && ofc.getNodeConnectorId() != null
85 && (ofc.getLocationType() == null || LocationType.Internal.equals(ofc.getLocationType()))) {
86 // Allow layer 3 traffic (ARP and IP) with the correct
87 // source IP, MAC, and source port
88 l3flow(ofWriter, nodeId, ep, ofc, 120, false);
89 l3flow(ofWriter, nodeId, ep, ofc, 121, true);
90 ofWriter.writeFlow(nodeId, TABLE_ID, l3DhcpDoraFlow(ep, ofc, 115));
92 // Allow layer 2 traffic with the correct source MAC and
93 // source port (note lower priority than drop IP rules)
94 ofWriter.writeFlow(nodeId, TABLE_ID, l2flow(ep, ofc, 100));
99 private Flow allowFromPort(NodeConnectorId port) {
100 Match match = new MatchBuilder()
103 FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "allow", match);
104 FlowBuilder flowb = base()
106 .setPriority(Integer.valueOf(200))
108 .setInstructions(FlowUtils.gotoTableInstructions(ctx.getPolicyManager().getTABLEID_SOURCE_MAPPER()));
109 return flowb.build();
112 private Flow allowFromExternalPort(NodeConnectorId port) {
113 Match match = new MatchBuilder()
116 FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "allowExternal", match);
117 FlowBuilder flowb = base()
119 .setPriority(Integer.valueOf(200))
121 .setInstructions(FlowUtils.gotoTableInstructions(ctx.getPolicyManager().getTABLEID_INGRESS_NAT()));
122 return flowb.build();
125 private Flow l2flow(Endpoint ep, OfOverlayContext ofc, Integer priority) {
126 Match match = new MatchBuilder()
128 FlowUtils.ethernetMatch(ep.getMacAddress(), null, null))
129 .setInPort(ofc.getNodeConnectorId())
131 FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "L2", match);
132 FlowBuilder flowb = base()
133 .setPriority(priority)
136 .setInstructions(FlowUtils.gotoTableInstructions(ctx.getPolicyManager().getTABLEID_SOURCE_MAPPER()));
138 return flowb.build();
141 private Flow l3DhcpDoraFlow(Endpoint ep, OfOverlayContext ofc, Integer priority) {
143 //TODO: Handle IPv6 DORA
144 Long etherType = FlowUtils.IPv4;
145 // DHCP DORA destination is broadcast
146 String ikey = "255.255.255.255/32";
147 Layer3Match m = new Ipv4MatchBuilder().setIpv4Destination(new Ipv4Prefix(ikey)).build();
149 Match match = new MatchBuilder()
151 FlowUtils.ethernetMatch(ep.getMacAddress(),
155 .setInPort(ofc.getNodeConnectorId())
157 FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "dhcp", match);
159 .setPriority(priority)
162 .setInstructions(FlowUtils.gotoTableInstructions(ctx.getPolicyManager().getTABLEID_SOURCE_MAPPER()))
168 private void l3flow(OfWriter ofWriter, NodeId nodeId,
169 Endpoint ep, OfOverlayContext ofc,
170 Integer priority, boolean arp) {
171 if (ep.getL3Address() == null)
173 for (L3Address l3 : ep.getL3Address()) {
174 if (l3.getIpAddress() == null)
176 Layer3Match m = null;
177 Long etherType = null;
179 if (l3.getIpAddress().getIpv4Address() != null) {
180 ikey = l3.getIpAddress().getIpv4Address().getValue() + "/32";
182 m = new ArpMatchBuilder()
183 .setArpSourceTransportAddress(new Ipv4Prefix(ikey))
185 etherType = FlowUtils.ARP;
187 m = new Ipv4MatchBuilder()
188 .setIpv4Source(new Ipv4Prefix(ikey))
190 etherType = FlowUtils.IPv4;
192 } else if (l3.getIpAddress().getIpv6Address() != null) {
195 ikey = l3.getIpAddress().getIpv6Address().getValue() + "/128";
196 m = new Ipv6MatchBuilder()
197 .setIpv6Source(new Ipv6Prefix(ikey))
199 etherType = FlowUtils.IPv6;
203 Match match = new MatchBuilder()
205 FlowUtils.ethernetMatch(ep.getMacAddress(),
209 .setInPort(ofc.getNodeConnectorId())
211 FlowId flowid = FlowIdUtils.newFlowId(TABLE_ID, "L3", match);
213 .setPriority(priority)
216 .setInstructions(FlowUtils.gotoTableInstructions(ctx.getPolicyManager().getTABLEID_SOURCE_MAPPER()))
219 ofWriter.writeFlow(nodeId, TABLE_ID,flow);