blob: 7b1f2a49bd52dab157ed4a9057ffe0693f2e28d0 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.proxyarp.impl;
alshabibb5522ff2014-09-29 19:20:00 -070017
Jonathan Hart6cd2f352015-01-13 17:44:45 -080018import com.google.common.collect.HashMultimap;
19import com.google.common.collect.Lists;
20import com.google.common.collect.Multimap;
alshabibb5522ff2014-09-29 19:20:00 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.apache.felix.scr.annotations.Service;
Jonathan Harte8600eb2015-01-12 10:30:45 -080027import org.onlab.packet.ARP;
28import org.onlab.packet.Ethernet;
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -080029import org.onlab.packet.ICMP6;
30import org.onlab.packet.IPv6;
Jonathan Harte8600eb2015-01-12 10:30:45 -080031import org.onlab.packet.Ip4Address;
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -080032import org.onlab.packet.Ip6Address;
Jonathan Harte8600eb2015-01-12 10:30:45 -080033import org.onlab.packet.IpAddress;
34import org.onlab.packet.MacAddress;
35import org.onlab.packet.VlanId;
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -080036import org.onlab.packet.ndp.NeighborAdvertisement;
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -080037import org.onlab.packet.ndp.NeighborDiscoveryOptions;
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -080038import org.onlab.packet.ndp.NeighborSolicitation;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.ConnectPoint;
40import org.onosproject.net.Device;
41import org.onosproject.net.Host;
42import org.onosproject.net.HostId;
43import org.onosproject.net.Link;
44import org.onosproject.net.Port;
45import org.onosproject.net.PortNumber;
46import org.onosproject.net.device.DeviceEvent;
47import org.onosproject.net.device.DeviceListener;
48import org.onosproject.net.device.DeviceService;
49import org.onosproject.net.flow.DefaultTrafficTreatment;
50import org.onosproject.net.flow.TrafficTreatment;
51import org.onosproject.net.host.HostService;
52import org.onosproject.net.host.InterfaceIpAddress;
53import org.onosproject.net.host.PortAddresses;
54import org.onosproject.net.link.LinkEvent;
55import org.onosproject.net.link.LinkListener;
56import org.onosproject.net.link.LinkService;
57import org.onosproject.net.packet.DefaultOutboundPacket;
58import org.onosproject.net.packet.InboundPacket;
59import org.onosproject.net.packet.PacketContext;
60import org.onosproject.net.packet.PacketService;
61import org.onosproject.net.proxyarp.ProxyArpService;
alshabibb5522ff2014-09-29 19:20:00 -070062import org.slf4j.Logger;
63
Jonathan Hart6cd2f352015-01-13 17:44:45 -080064import java.nio.ByteBuffer;
65import java.util.HashSet;
66import java.util.List;
67import java.util.Map.Entry;
68import java.util.Set;
69
70import static com.google.common.base.Preconditions.checkArgument;
71import static com.google.common.base.Preconditions.checkNotNull;
72import static org.slf4j.LoggerFactory.getLogger;
alshabibb5522ff2014-09-29 19:20:00 -070073
alshabibb5522ff2014-09-29 19:20:00 -070074@Component(immediate = true)
75@Service
76public class ProxyArpManager implements ProxyArpService {
77
78 private final Logger log = getLogger(getClass());
79
80 private static final String MAC_ADDR_NULL = "Mac address cannot be null.";
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -080081 private static final String REQUEST_NULL = "ARP or NDP request cannot be null.";
alshabibb5522ff2014-09-29 19:20:00 -070082 private static final String REQUEST_NOT_ARP = "Ethernet frame does not contain ARP request.";
83 private static final String NOT_ARP_REQUEST = "ARP is not a request.";
Jonathan Hart704ca142014-10-09 09:34:39 -070084 private static final String NOT_ARP_REPLY = "ARP is not a reply.";
alshabibb5522ff2014-09-29 19:20:00 -070085
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected HostService hostService;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected PacketService packetService;
91
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected LinkService linkService;
94
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected DeviceService deviceService;
97
98 private final Multimap<Device, PortNumber> internalPorts =
99 HashMultimap.<Device, PortNumber>create();
100
101 private final Multimap<Device, PortNumber> externalPorts =
102 HashMultimap.<Device, PortNumber>create();
103
104 /**
105 * Listens to both device service and link service to determine
106 * whether a port is internal or external.
107 */
108 @Activate
109 public void activate() {
110 deviceService.addListener(new InternalDeviceListener());
111 linkService.addListener(new InternalLinkListener());
112 determinePortLocations();
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800113
alshabibb5522ff2014-09-29 19:20:00 -0700114 log.info("Started");
115 }
116
117
118 @Deactivate
119 public void deactivate() {
120 log.info("Stopped");
121 }
122
123 @Override
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800124 public boolean isKnown(IpAddress addr) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700125 checkNotNull(addr, MAC_ADDR_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700126 Set<Host> hosts = hostService.getHostsByIp(addr);
127 return !hosts.isEmpty();
128 }
129
130 @Override
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700131 public void reply(Ethernet eth, ConnectPoint inPort) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700132 checkNotNull(eth, REQUEST_NULL);
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800133
134 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
135 replyArp(eth, inPort);
136 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
137 replyNdp(eth, inPort);
138 }
139 }
140
141 private void replyArp(Ethernet eth, ConnectPoint inPort) {
alshabibb5522ff2014-09-29 19:20:00 -0700142 ARP arp = (ARP) eth.getPayload();
143 checkArgument(arp.getOpCode() == ARP.OP_REQUEST, NOT_ARP_REQUEST);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700144 checkNotNull(inPort);
Dusan Pajina22b9702015-02-12 16:25:23 +0100145 Ip4Address targetAddress = Ip4Address.valueOf(arp.getTargetProtocolAddress());
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700146
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800147 VlanId vlan = VlanId.vlanId(eth.getVlanID());
148
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700149 // If the request came from outside the network, only reply if it was
150 // for one of our external addresses.
151 if (isOutsidePort(inPort)) {
Jonathan Harta887ba82014-11-03 15:20:52 -0800152 Set<PortAddresses> addressSet =
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700153 hostService.getAddressBindingsForPort(inPort);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700154
Jonathan Harta887ba82014-11-03 15:20:52 -0800155 for (PortAddresses addresses : addressSet) {
156 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
Dusan Pajina22b9702015-02-12 16:25:23 +0100157 if (ia.ipAddress().equals(targetAddress)) {
Jonathan Harta887ba82014-11-03 15:20:52 -0800158 Ethernet arpReply =
Dusan Pajina22b9702015-02-12 16:25:23 +0100159 buildArpReply(targetAddress, addresses.mac(), eth);
Jonathan Harta887ba82014-11-03 15:20:52 -0800160 sendTo(arpReply, inPort);
161 }
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700162 }
163 }
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700164 return;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700165 } else {
166 // If the source address matches one of our external addresses
167 // it could be a request from an internal host to an external
Jonathan Hart1f793a72014-11-12 23:22:02 -0800168 // address. Forward it over to the correct ports.
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800169 Ip4Address source =
170 Ip4Address.valueOf(arp.getSenderProtocolAddress());
Jonathan Hart1f793a72014-11-12 23:22:02 -0800171 Set<PortAddresses> sourceAddresses = findPortsInSubnet(source);
172 boolean matched = false;
173 for (PortAddresses pa : sourceAddresses) {
174 for (InterfaceIpAddress ia : pa.ipAddresses()) {
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800175 if (ia.ipAddress().equals(source) &&
176 pa.vlan().equals(vlan)) {
Jonathan Hart1f793a72014-11-12 23:22:02 -0800177 matched = true;
178 sendTo(eth, pa.connectPoint());
Pavlin Radoslavovcef52062015-02-12 16:57:17 -0800179 break;
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700180 }
181 }
182 }
Jonathan Hart1f793a72014-11-12 23:22:02 -0800183
184 if (matched) {
185 return;
186 }
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700187 }
188
189 // Continue with normal proxy ARP case
alshabibb5522ff2014-09-29 19:20:00 -0700190
Dusan Pajina22b9702015-02-12 16:25:23 +0100191 Set<Host> hosts = hostService.getHostsByIp(targetAddress);
alshabibb5522ff2014-09-29 19:20:00 -0700192
193 Host dst = null;
194 Host src = hostService.getHost(HostId.hostId(eth.getSourceMAC(),
195 VlanId.vlanId(eth.getVlanID())));
196
197 for (Host host : hosts) {
198 if (host.vlan().equals(vlan)) {
199 dst = host;
200 break;
201 }
202 }
203
204 if (src == null || dst == null) {
Dusan Pajina22b9702015-02-12 16:25:23 +0100205 //
206 // The request couldn't be resolved.
207 // Flood the request on all ports except the incoming ports.
208 //
Jonathan Hartf84591d2015-01-16 14:33:43 -0800209 flood(eth, inPort);
alshabibb5522ff2014-09-29 19:20:00 -0700210 return;
211 }
212
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800213 //
Dusan Pajina22b9702015-02-12 16:25:23 +0100214 // Reply on the port the request was received on
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800215 //
Dusan Pajina22b9702015-02-12 16:25:23 +0100216 Ethernet arpReply = buildArpReply(targetAddress, dst.mac(), eth);
217 sendTo(arpReply, inPort);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700218 }
219
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800220 private void replyNdp(Ethernet eth, ConnectPoint inPort) {
221
222 IPv6 ipv6 = (IPv6) eth.getPayload();
223 ICMP6 icmpv6 = (ICMP6) ipv6.getPayload();
224 NeighborSolicitation nsol = (NeighborSolicitation) icmpv6.getPayload();
Dusan Pajina22b9702015-02-12 16:25:23 +0100225 Ip6Address targetAddress = Ip6Address.valueOf(nsol.getTargetAddress());
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800226
227 VlanId vlan = VlanId.vlanId(eth.getVlanID());
228
229 // If the request came from outside the network, only reply if it was
230 // for one of our external addresses.
231 if (isOutsidePort(inPort)) {
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800232 Set<PortAddresses> addressSet =
233 hostService.getAddressBindingsForPort(inPort);
234
235 for (PortAddresses addresses : addressSet) {
236 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
Dusan Pajina22b9702015-02-12 16:25:23 +0100237 if (ia.ipAddress().equals(targetAddress)) {
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800238 Ethernet ndpReply =
Pavlin Radoslavovcef52062015-02-12 16:57:17 -0800239 buildNdpReply(targetAddress, addresses.mac(), eth);
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800240 sendTo(ndpReply, inPort);
241 }
242 }
243 }
244 return;
245 } else {
246 // If the source address matches one of our external addresses
247 // it could be a request from an internal host to an external
248 // address. Forward it over to the correct ports.
249 Ip6Address source =
Pavlin Radoslavovcef52062015-02-12 16:57:17 -0800250 Ip6Address.valueOf(ipv6.getSourceAddress());
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800251 Set<PortAddresses> sourceAddresses = findPortsInSubnet(source);
252 boolean matched = false;
253 for (PortAddresses pa : sourceAddresses) {
254 for (InterfaceIpAddress ia : pa.ipAddresses()) {
255 if (ia.ipAddress().equals(source) &&
256 pa.vlan().equals(vlan)) {
257 matched = true;
258 sendTo(eth, pa.connectPoint());
Pavlin Radoslavovcef52062015-02-12 16:57:17 -0800259 break;
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800260 }
261 }
262 }
263
264 if (matched) {
265 return;
266 }
267 }
268
269 // Continue with normal proxy ARP case
270
Dusan Pajina22b9702015-02-12 16:25:23 +0100271 Set<Host> hosts = hostService.getHostsByIp(targetAddress);
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800272
273 Host dst = null;
274 Host src = hostService.getHost(HostId.hostId(eth.getSourceMAC(),
275 VlanId.vlanId(eth.getVlanID())));
276
277 for (Host host : hosts) {
278 if (host.vlan().equals(vlan)) {
279 dst = host;
280 break;
281 }
282 }
283
284 if (src == null || dst == null) {
Dusan Pajina22b9702015-02-12 16:25:23 +0100285 //
286 // The request couldn't be resolved.
287 // Flood the request on all ports except the incoming ports.
288 //
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800289 flood(eth, inPort);
290 return;
291 }
292
293 //
Dusan Pajina22b9702015-02-12 16:25:23 +0100294 // Reply on the port the request was received on
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800295 //
Dusan Pajina22b9702015-02-12 16:25:23 +0100296 Ethernet ndpReply = buildNdpReply(targetAddress, dst.mac(), eth);
297 sendTo(ndpReply, inPort);
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800298 }
299
300
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700301 /**
302 * Outputs the given packet out the given port.
303 *
304 * @param packet the packet to send
305 * @param outPort the port to send it out
306 */
307 private void sendTo(Ethernet packet, ConnectPoint outPort) {
308 if (internalPorts.containsEntry(
309 deviceService.getDevice(outPort.deviceId()), outPort.port())) {
310 // Sanity check to make sure we don't send the packet out an
311 // internal port and create a loop (could happen due to
312 // misconfiguration).
313 return;
314 }
315
tom9a693fd2014-10-03 11:32:19 -0700316 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700317 builder.setOutput(outPort.port());
318 packetService.emit(new DefaultOutboundPacket(outPort.deviceId(),
319 builder.build(), ByteBuffer.wrap(packet.serialize())));
320 }
321
322 /**
Jonathan Hart1f793a72014-11-12 23:22:02 -0800323 * Finds ports with an address in the subnet of the target address.
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700324 *
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700325 * @param target the target address to find a matching port for
Jonathan Hart1f793a72014-11-12 23:22:02 -0800326 * @return a set of PortAddresses describing ports in the subnet
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700327 */
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800328 private Set<PortAddresses> findPortsInSubnet(IpAddress target) {
Jonathan Hart1f793a72014-11-12 23:22:02 -0800329 Set<PortAddresses> result = new HashSet<PortAddresses>();
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700330 for (PortAddresses addresses : hostService.getAddressBindings()) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700331 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
332 if (ia.subnetAddress().contains(target)) {
Jonathan Hart1f793a72014-11-12 23:22:02 -0800333 result.add(addresses);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700334 }
335 }
336 }
Jonathan Hart1f793a72014-11-12 23:22:02 -0800337 return result;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700338 }
339
340 /**
341 * Returns whether the given port is an outside-facing port with an IP
342 * address configured.
343 *
344 * @param port the port to check
345 * @return true if the port is an outside-facing port, otherwise false
346 */
347 private boolean isOutsidePort(ConnectPoint port) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700348 //
349 // TODO: Is this sufficient to identify outside-facing ports: just
350 // having IP addresses on a port?
351 //
Jonathan Harta887ba82014-11-03 15:20:52 -0800352 return !hostService.getAddressBindingsForPort(port).isEmpty();
alshabibb5522ff2014-09-29 19:20:00 -0700353 }
354
355 @Override
Jonathan Hartf84591d2015-01-16 14:33:43 -0800356 public void forward(Ethernet eth, ConnectPoint inPort) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700357 checkNotNull(eth, REQUEST_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700358
359 Host h = hostService.getHost(HostId.hostId(eth.getDestinationMAC(),
360 VlanId.vlanId(eth.getVlanID())));
361
362 if (h == null) {
Jonathan Hartf84591d2015-01-16 14:33:43 -0800363 flood(eth, inPort);
alshabibb5522ff2014-09-29 19:20:00 -0700364 } else {
tom9a693fd2014-10-03 11:32:19 -0700365 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700366 builder.setOutput(h.location().port());
367 packetService.emit(new DefaultOutboundPacket(h.location().deviceId(),
368 builder.build(), ByteBuffer.wrap(eth.serialize())));
369 }
370
371 }
372
alshabibc274c902014-10-03 14:58:27 -0700373 @Override
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800374 public boolean handlePacket(PacketContext context) {
alshabibc274c902014-10-03 14:58:27 -0700375 InboundPacket pkt = context.inPacket();
376 Ethernet ethPkt = pkt.parsed();
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800377
378 if (ethPkt == null) {
379 return false;
380 }
381 if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) {
382 return handleArp(context, ethPkt);
383 } else if (ethPkt.getEtherType() == Ethernet.TYPE_IPV6) {
384 return handleNdp(context, ethPkt);
alshabibc274c902014-10-03 14:58:27 -0700385 }
386 return false;
387 }
388
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800389 private boolean handleArp(PacketContext context, Ethernet ethPkt) {
390 ARP arp = (ARP) ethPkt.getPayload();
391
392 if (arp.getOpCode() == ARP.OP_REPLY) {
393 forward(ethPkt, context.inPacket().receivedFrom());
394 } else if (arp.getOpCode() == ARP.OP_REQUEST) {
395 reply(ethPkt, context.inPacket().receivedFrom());
396 } else {
397 return false;
398 }
399 context.block();
400 return true;
401 }
402
403 private boolean handleNdp(PacketContext context, Ethernet ethPkt) {
404 IPv6 ipv6 = (IPv6) ethPkt.getPayload();
405
406 if (ipv6.getNextHeader() != IPv6.PROTOCOL_ICMP6) {
407 return false;
408 }
409 ICMP6 icmpv6 = (ICMP6) ipv6.getPayload();
410 if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_ADVERTISEMENT) {
411 forward(ethPkt, context.inPacket().receivedFrom());
412 } else if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION) {
413 reply(ethPkt, context.inPacket().receivedFrom());
414 } else {
415 return false;
416 }
417 context.block();
418 return true;
419 }
420
alshabibb5522ff2014-09-29 19:20:00 -0700421 /**
422 * Flood the arp request at all edges in the network.
Dusan Pajina22b9702015-02-12 16:25:23 +0100423 *
424 * @param request the arp request
425 * @param inPort the connect point the arp request was received on
alshabibb5522ff2014-09-29 19:20:00 -0700426 */
Jonathan Hartf84591d2015-01-16 14:33:43 -0800427 private void flood(Ethernet request, ConnectPoint inPort) {
alshabibb5522ff2014-09-29 19:20:00 -0700428 TrafficTreatment.Builder builder = null;
429 ByteBuffer buf = ByteBuffer.wrap(request.serialize());
430
431 synchronized (externalPorts) {
432 for (Entry<Device, PortNumber> entry : externalPorts.entries()) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700433 ConnectPoint cp = new ConnectPoint(entry.getKey().id(), entry.getValue());
Jonathan Hartf84591d2015-01-16 14:33:43 -0800434 if (isOutsidePort(cp) || cp.equals(inPort)) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700435 continue;
436 }
437
tom9a693fd2014-10-03 11:32:19 -0700438 builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700439 builder.setOutput(entry.getValue());
440 packetService.emit(new DefaultOutboundPacket(entry.getKey().id(),
441 builder.build(), buf));
442 }
alshabibb5522ff2014-09-29 19:20:00 -0700443 }
444 }
445
446 /**
447 * Determines the location of all known ports in the system.
448 */
449 private void determinePortLocations() {
450 Iterable<Device> devices = deviceService.getDevices();
451 Iterable<Link> links = null;
452 List<PortNumber> ports = null;
453 for (Device d : devices) {
454 ports = buildPortNumberList(deviceService.getPorts(d.id()));
455 links = linkService.getLinks();
456 for (Link l : links) {
457 // for each link, mark the concerned ports as internal
458 // and the remaining ports are therefore external.
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700459 if (l.src().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700460 && ports.contains(l.src().port())) {
461 ports.remove(l.src().port());
462 internalPorts.put(d, l.src().port());
463 }
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700464 if (l.dst().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700465 && ports.contains(l.dst().port())) {
466 ports.remove(l.dst().port());
467 internalPorts.put(d, l.dst().port());
468 }
469 }
470 synchronized (externalPorts) {
471 externalPorts.putAll(d, ports);
472 }
473 }
474
475 }
476
477 private List<PortNumber> buildPortNumberList(List<Port> ports) {
478 List<PortNumber> portNumbers = Lists.newLinkedList();
479 for (Port p : ports) {
480 portNumbers.add(p.number());
481 }
482 return portNumbers;
483 }
484
485 /**
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700486 * Builds an ARP reply based on a request.
487 *
488 * @param srcIp the IP address to use as the reply source
489 * @param srcMac the MAC address to use as the reply source
490 * @param request the ARP request we got
491 * @return an Ethernet frame containing the ARP reply
alshabibb5522ff2014-09-29 19:20:00 -0700492 */
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800493 private Ethernet buildArpReply(Ip4Address srcIp, MacAddress srcMac,
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700494 Ethernet request) {
495
alshabibb5522ff2014-09-29 19:20:00 -0700496 Ethernet eth = new Ethernet();
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800497 eth.setDestinationMACAddress(request.getSourceMAC());
498 eth.setSourceMACAddress(srcMac);
alshabibb5522ff2014-09-29 19:20:00 -0700499 eth.setEtherType(Ethernet.TYPE_ARP);
500 eth.setVlanID(request.getVlanID());
501
502 ARP arp = new ARP();
503 arp.setOpCode(ARP.OP_REPLY);
504 arp.setProtocolType(ARP.PROTO_TYPE_IP);
505 arp.setHardwareType(ARP.HW_TYPE_ETHERNET);
alshabib6eb438a2014-10-01 16:39:37 -0700506
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800507 arp.setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH);
alshabibb5522ff2014-09-29 19:20:00 -0700508 arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH);
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800509 arp.setSenderHardwareAddress(srcMac.toBytes());
alshabibb5522ff2014-09-29 19:20:00 -0700510 arp.setTargetHardwareAddress(request.getSourceMACAddress());
511
512 arp.setTargetProtocolAddress(((ARP) request.getPayload())
513 .getSenderProtocolAddress());
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700514 arp.setSenderProtocolAddress(srcIp.toInt());
Dusan Pajina22b9702015-02-12 16:25:23 +0100515
alshabibb5522ff2014-09-29 19:20:00 -0700516 eth.setPayload(arp);
517 return eth;
518 }
519
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800520 /**
521 * Builds an Neighbor Discovery reply based on a request.
522 *
523 * @param srcIp the IP address to use as the reply source
524 * @param srcMac the MAC address to use as the reply source
525 * @param request the Neighbor Solicitation request we got
526 * @return an Ethernet frame containing the Neighbor Advertisement reply
527 */
528 private Ethernet buildNdpReply(Ip6Address srcIp, MacAddress srcMac,
529 Ethernet request) {
530
531 Ethernet eth = new Ethernet();
532 eth.setDestinationMACAddress(request.getSourceMAC());
533 eth.setSourceMACAddress(srcMac);
534 eth.setEtherType(Ethernet.TYPE_IPV6);
535 eth.setVlanID(request.getVlanID());
536
537 IPv6 requestIp = (IPv6) request.getPayload();
538 IPv6 ipv6 = new IPv6();
539 ipv6.setSourceAddress(srcIp.toOctets());
540 ipv6.setDestinationAddress(requestIp.getSourceAddress());
541 ipv6.setHopLimit((byte) 255);
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800542
543 ICMP6 icmp6 = new ICMP6();
544 icmp6.setIcmpType(ICMP6.NEIGHBOR_ADVERTISEMENT);
545 icmp6.setIcmpCode((byte) 0);
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800546
547 NeighborAdvertisement nadv = new NeighborAdvertisement();
Dusan Pajina22b9702015-02-12 16:25:23 +0100548 nadv.setTargetAddress(srcIp.toOctets());
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800549 nadv.setSolicitedFlag((byte) 1);
550 nadv.setOverrideFlag((byte) 1);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800551 nadv.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
552 srcMac.toBytes());
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800553
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800554 icmp6.setPayload(nadv);
Dusan Pajina22b9702015-02-12 16:25:23 +0100555 ipv6.setPayload(icmp6);
556 eth.setPayload(ipv6);
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800557 return eth;
558 }
559
alshabibb5522ff2014-09-29 19:20:00 -0700560 public class InternalLinkListener implements LinkListener {
561
562 @Override
563 public void event(LinkEvent event) {
564 Link link = event.subject();
565 Device src = deviceService.getDevice(link.src().deviceId());
566 Device dst = deviceService.getDevice(link.dst().deviceId());
567 switch (event.type()) {
568 case LINK_ADDED:
569 synchronized (externalPorts) {
570 externalPorts.remove(src, link.src().port());
571 externalPorts.remove(dst, link.dst().port());
572 internalPorts.put(src, link.src().port());
573 internalPorts.put(dst, link.dst().port());
574 }
575
576 break;
577 case LINK_REMOVED:
578 synchronized (externalPorts) {
579 externalPorts.put(src, link.src().port());
580 externalPorts.put(dst, link.dst().port());
581 internalPorts.remove(src, link.src().port());
582 internalPorts.remove(dst, link.dst().port());
583 }
584
585 break;
586 case LINK_UPDATED:
587 // don't care about links being updated.
588 break;
589 default:
590 break;
591 }
592
593 }
594
595 }
596
597 public class InternalDeviceListener implements DeviceListener {
598
599 @Override
600 public void event(DeviceEvent event) {
601 Device device = event.subject();
602 switch (event.type()) {
603 case DEVICE_ADDED:
604 case DEVICE_AVAILABILITY_CHANGED:
alshabibb5522ff2014-09-29 19:20:00 -0700605 case DEVICE_SUSPENDED:
606 case DEVICE_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700607 // nothing to do in these cases; handled when links get reported
608 break;
609 case DEVICE_REMOVED:
610 synchronized (externalPorts) {
611 externalPorts.removeAll(device);
612 internalPorts.removeAll(device);
613 }
614 break;
615 case PORT_ADDED:
alshabib6eb438a2014-10-01 16:39:37 -0700616 case PORT_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700617 synchronized (externalPorts) {
alshabib6eb438a2014-10-01 16:39:37 -0700618 if (event.port().isEnabled()) {
619 externalPorts.put(device, event.port().number());
620 internalPorts.remove(device, event.port().number());
621 }
alshabibb5522ff2014-09-29 19:20:00 -0700622 }
623 break;
624 case PORT_REMOVED:
625 synchronized (externalPorts) {
626 externalPorts.remove(device, event.port().number());
627 internalPorts.remove(device, event.port().number());
628 }
629 break;
630 default:
631 break;
632
633 }
634
635 }
636
alshabibc274c902014-10-03 14:58:27 -0700637 }
alshabibb5522ff2014-09-29 19:20:00 -0700638
639}