blob: 5369d2cd02ad3c092c2ff6a18f69734609b18ba9 [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;
Dusan Pajina22b9702015-02-12 16:25:23 +010028import org.onlab.packet.Data;
Jonathan Harte8600eb2015-01-12 10:30:45 -080029import org.onlab.packet.Ethernet;
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -080030import org.onlab.packet.ICMP6;
31import org.onlab.packet.IPv6;
Jonathan Harte8600eb2015-01-12 10:30:45 -080032import org.onlab.packet.Ip4Address;
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -080033import org.onlab.packet.Ip6Address;
Jonathan Harte8600eb2015-01-12 10:30:45 -080034import org.onlab.packet.IpAddress;
35import org.onlab.packet.MacAddress;
36import org.onlab.packet.VlanId;
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -080037import org.onlab.packet.ndp.NeighborAdvertisement;
38import 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 Radoslavov76b0ae22014-10-27 15:33:19 -0700179 }
180 }
181 }
Jonathan Hart1f793a72014-11-12 23:22:02 -0800182
183 if (matched) {
184 return;
185 }
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700186 }
187
188 // Continue with normal proxy ARP case
alshabibb5522ff2014-09-29 19:20:00 -0700189
Dusan Pajina22b9702015-02-12 16:25:23 +0100190 Set<Host> hosts = hostService.getHostsByIp(targetAddress);
alshabibb5522ff2014-09-29 19:20:00 -0700191
192 Host dst = null;
193 Host src = hostService.getHost(HostId.hostId(eth.getSourceMAC(),
194 VlanId.vlanId(eth.getVlanID())));
195
196 for (Host host : hosts) {
197 if (host.vlan().equals(vlan)) {
198 dst = host;
199 break;
200 }
201 }
202
203 if (src == null || dst == null) {
Dusan Pajina22b9702015-02-12 16:25:23 +0100204 //
205 // The request couldn't be resolved.
206 // Flood the request on all ports except the incoming ports.
207 //
Jonathan Hartf84591d2015-01-16 14:33:43 -0800208 flood(eth, inPort);
alshabibb5522ff2014-09-29 19:20:00 -0700209 return;
210 }
211
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800212 //
Dusan Pajina22b9702015-02-12 16:25:23 +0100213 // Reply on the port the request was received on
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800214 //
Dusan Pajina22b9702015-02-12 16:25:23 +0100215 Ethernet arpReply = buildArpReply(targetAddress, dst.mac(), eth);
216 sendTo(arpReply, inPort);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700217 }
218
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800219 private void replyNdp(Ethernet eth, ConnectPoint inPort) {
220
221 IPv6 ipv6 = (IPv6) eth.getPayload();
222 ICMP6 icmpv6 = (ICMP6) ipv6.getPayload();
223 NeighborSolicitation nsol = (NeighborSolicitation) icmpv6.getPayload();
Dusan Pajina22b9702015-02-12 16:25:23 +0100224 Ip6Address targetAddress = Ip6Address.valueOf(nsol.getTargetAddress());
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800225
226 VlanId vlan = VlanId.vlanId(eth.getVlanID());
227
228 // If the request came from outside the network, only reply if it was
229 // for one of our external addresses.
230 if (isOutsidePort(inPort)) {
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800231 Set<PortAddresses> addressSet =
232 hostService.getAddressBindingsForPort(inPort);
233
234 for (PortAddresses addresses : addressSet) {
235 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
Dusan Pajina22b9702015-02-12 16:25:23 +0100236 if (ia.ipAddress().equals(targetAddress)) {
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800237 Ethernet ndpReply =
Dusan Pajina22b9702015-02-12 16:25:23 +0100238 buildNdpReply(targetAddress, addresses.mac(), eth);
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800239 sendTo(ndpReply, inPort);
240 }
241 }
242 }
243 return;
244 } else {
245 // If the source address matches one of our external addresses
246 // it could be a request from an internal host to an external
247 // address. Forward it over to the correct ports.
248 Ip6Address source =
249 Ip6Address.valueOf(nsol.getTargetAddress());
250 Set<PortAddresses> sourceAddresses = findPortsInSubnet(source);
251 boolean matched = false;
252 for (PortAddresses pa : sourceAddresses) {
253 for (InterfaceIpAddress ia : pa.ipAddresses()) {
254 if (ia.ipAddress().equals(source) &&
255 pa.vlan().equals(vlan)) {
256 matched = true;
257 sendTo(eth, pa.connectPoint());
258 }
259 }
260 }
261
262 if (matched) {
263 return;
264 }
265 }
266
267 // Continue with normal proxy ARP case
268
Dusan Pajina22b9702015-02-12 16:25:23 +0100269 Set<Host> hosts = hostService.getHostsByIp(targetAddress);
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800270
271 Host dst = null;
272 Host src = hostService.getHost(HostId.hostId(eth.getSourceMAC(),
273 VlanId.vlanId(eth.getVlanID())));
274
275 for (Host host : hosts) {
276 if (host.vlan().equals(vlan)) {
277 dst = host;
278 break;
279 }
280 }
281
282 if (src == null || dst == null) {
Dusan Pajina22b9702015-02-12 16:25:23 +0100283 //
284 // The request couldn't be resolved.
285 // Flood the request on all ports except the incoming ports.
286 //
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800287 flood(eth, inPort);
288 return;
289 }
290
291 //
Dusan Pajina22b9702015-02-12 16:25:23 +0100292 // Reply on the port the request was received on
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800293 //
Dusan Pajina22b9702015-02-12 16:25:23 +0100294 Ethernet ndpReply = buildNdpReply(targetAddress, dst.mac(), eth);
295 sendTo(ndpReply, inPort);
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800296 }
297
298
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700299 /**
300 * Outputs the given packet out the given port.
301 *
302 * @param packet the packet to send
303 * @param outPort the port to send it out
304 */
305 private void sendTo(Ethernet packet, ConnectPoint outPort) {
306 if (internalPorts.containsEntry(
307 deviceService.getDevice(outPort.deviceId()), outPort.port())) {
308 // Sanity check to make sure we don't send the packet out an
309 // internal port and create a loop (could happen due to
310 // misconfiguration).
311 return;
312 }
313
tom9a693fd2014-10-03 11:32:19 -0700314 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700315 builder.setOutput(outPort.port());
316 packetService.emit(new DefaultOutboundPacket(outPort.deviceId(),
317 builder.build(), ByteBuffer.wrap(packet.serialize())));
318 }
319
320 /**
Jonathan Hart1f793a72014-11-12 23:22:02 -0800321 * Finds ports with an address in the subnet of the target address.
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700322 *
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700323 * @param target the target address to find a matching port for
Jonathan Hart1f793a72014-11-12 23:22:02 -0800324 * @return a set of PortAddresses describing ports in the subnet
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700325 */
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800326 private Set<PortAddresses> findPortsInSubnet(IpAddress target) {
Jonathan Hart1f793a72014-11-12 23:22:02 -0800327 Set<PortAddresses> result = new HashSet<PortAddresses>();
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700328 for (PortAddresses addresses : hostService.getAddressBindings()) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700329 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
330 if (ia.subnetAddress().contains(target)) {
Jonathan Hart1f793a72014-11-12 23:22:02 -0800331 result.add(addresses);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700332 }
333 }
334 }
Jonathan Hart1f793a72014-11-12 23:22:02 -0800335 return result;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700336 }
337
338 /**
339 * Returns whether the given port is an outside-facing port with an IP
340 * address configured.
341 *
342 * @param port the port to check
343 * @return true if the port is an outside-facing port, otherwise false
344 */
345 private boolean isOutsidePort(ConnectPoint port) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700346 //
347 // TODO: Is this sufficient to identify outside-facing ports: just
348 // having IP addresses on a port?
349 //
Jonathan Harta887ba82014-11-03 15:20:52 -0800350 return !hostService.getAddressBindingsForPort(port).isEmpty();
alshabibb5522ff2014-09-29 19:20:00 -0700351 }
352
353 @Override
Jonathan Hartf84591d2015-01-16 14:33:43 -0800354 public void forward(Ethernet eth, ConnectPoint inPort) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700355 checkNotNull(eth, REQUEST_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700356
357 Host h = hostService.getHost(HostId.hostId(eth.getDestinationMAC(),
358 VlanId.vlanId(eth.getVlanID())));
359
360 if (h == null) {
Jonathan Hartf84591d2015-01-16 14:33:43 -0800361 flood(eth, inPort);
alshabibb5522ff2014-09-29 19:20:00 -0700362 } else {
tom9a693fd2014-10-03 11:32:19 -0700363 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700364 builder.setOutput(h.location().port());
365 packetService.emit(new DefaultOutboundPacket(h.location().deviceId(),
366 builder.build(), ByteBuffer.wrap(eth.serialize())));
367 }
368
369 }
370
alshabibc274c902014-10-03 14:58:27 -0700371 @Override
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800372 public boolean handlePacket(PacketContext context) {
alshabibc274c902014-10-03 14:58:27 -0700373 InboundPacket pkt = context.inPacket();
374 Ethernet ethPkt = pkt.parsed();
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800375
376 if (ethPkt == null) {
377 return false;
378 }
379 if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) {
380 return handleArp(context, ethPkt);
381 } else if (ethPkt.getEtherType() == Ethernet.TYPE_IPV6) {
382 return handleNdp(context, ethPkt);
alshabibc274c902014-10-03 14:58:27 -0700383 }
384 return false;
385 }
386
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800387 private boolean handleArp(PacketContext context, Ethernet ethPkt) {
388 ARP arp = (ARP) ethPkt.getPayload();
389
390 if (arp.getOpCode() == ARP.OP_REPLY) {
391 forward(ethPkt, context.inPacket().receivedFrom());
392 } else if (arp.getOpCode() == ARP.OP_REQUEST) {
393 reply(ethPkt, context.inPacket().receivedFrom());
394 } else {
395 return false;
396 }
397 context.block();
398 return true;
399 }
400
401 private boolean handleNdp(PacketContext context, Ethernet ethPkt) {
402 IPv6 ipv6 = (IPv6) ethPkt.getPayload();
403
404 if (ipv6.getNextHeader() != IPv6.PROTOCOL_ICMP6) {
405 return false;
406 }
407 ICMP6 icmpv6 = (ICMP6) ipv6.getPayload();
408 if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_ADVERTISEMENT) {
409 forward(ethPkt, context.inPacket().receivedFrom());
410 } else if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION) {
411 reply(ethPkt, context.inPacket().receivedFrom());
412 } else {
413 return false;
414 }
415 context.block();
416 return true;
417 }
418
alshabibb5522ff2014-09-29 19:20:00 -0700419 /**
420 * Flood the arp request at all edges in the network.
Dusan Pajina22b9702015-02-12 16:25:23 +0100421 *
422 * @param request the arp request
423 * @param inPort the connect point the arp request was received on
alshabibb5522ff2014-09-29 19:20:00 -0700424 */
Jonathan Hartf84591d2015-01-16 14:33:43 -0800425 private void flood(Ethernet request, ConnectPoint inPort) {
alshabibb5522ff2014-09-29 19:20:00 -0700426 TrafficTreatment.Builder builder = null;
427 ByteBuffer buf = ByteBuffer.wrap(request.serialize());
428
429 synchronized (externalPorts) {
430 for (Entry<Device, PortNumber> entry : externalPorts.entries()) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700431 ConnectPoint cp = new ConnectPoint(entry.getKey().id(), entry.getValue());
Jonathan Hartf84591d2015-01-16 14:33:43 -0800432 if (isOutsidePort(cp) || cp.equals(inPort)) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700433 continue;
434 }
435
tom9a693fd2014-10-03 11:32:19 -0700436 builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700437 builder.setOutput(entry.getValue());
438 packetService.emit(new DefaultOutboundPacket(entry.getKey().id(),
439 builder.build(), buf));
440 }
alshabibb5522ff2014-09-29 19:20:00 -0700441 }
442 }
443
444 /**
445 * Determines the location of all known ports in the system.
446 */
447 private void determinePortLocations() {
448 Iterable<Device> devices = deviceService.getDevices();
449 Iterable<Link> links = null;
450 List<PortNumber> ports = null;
451 for (Device d : devices) {
452 ports = buildPortNumberList(deviceService.getPorts(d.id()));
453 links = linkService.getLinks();
454 for (Link l : links) {
455 // for each link, mark the concerned ports as internal
456 // and the remaining ports are therefore external.
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700457 if (l.src().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700458 && ports.contains(l.src().port())) {
459 ports.remove(l.src().port());
460 internalPorts.put(d, l.src().port());
461 }
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700462 if (l.dst().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700463 && ports.contains(l.dst().port())) {
464 ports.remove(l.dst().port());
465 internalPorts.put(d, l.dst().port());
466 }
467 }
468 synchronized (externalPorts) {
469 externalPorts.putAll(d, ports);
470 }
471 }
472
473 }
474
475 private List<PortNumber> buildPortNumberList(List<Port> ports) {
476 List<PortNumber> portNumbers = Lists.newLinkedList();
477 for (Port p : ports) {
478 portNumbers.add(p.number());
479 }
480 return portNumbers;
481 }
482
483 /**
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700484 * Builds an ARP reply based on a request.
485 *
486 * @param srcIp the IP address to use as the reply source
487 * @param srcMac the MAC address to use as the reply source
488 * @param request the ARP request we got
489 * @return an Ethernet frame containing the ARP reply
alshabibb5522ff2014-09-29 19:20:00 -0700490 */
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800491 private Ethernet buildArpReply(Ip4Address srcIp, MacAddress srcMac,
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700492 Ethernet request) {
493
alshabibb5522ff2014-09-29 19:20:00 -0700494 Ethernet eth = new Ethernet();
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800495 eth.setDestinationMACAddress(request.getSourceMAC());
496 eth.setSourceMACAddress(srcMac);
alshabibb5522ff2014-09-29 19:20:00 -0700497 eth.setEtherType(Ethernet.TYPE_ARP);
498 eth.setVlanID(request.getVlanID());
499
500 ARP arp = new ARP();
501 arp.setOpCode(ARP.OP_REPLY);
502 arp.setProtocolType(ARP.PROTO_TYPE_IP);
503 arp.setHardwareType(ARP.HW_TYPE_ETHERNET);
alshabib6eb438a2014-10-01 16:39:37 -0700504
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800505 arp.setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH);
alshabibb5522ff2014-09-29 19:20:00 -0700506 arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH);
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800507 arp.setSenderHardwareAddress(srcMac.toBytes());
alshabibb5522ff2014-09-29 19:20:00 -0700508 arp.setTargetHardwareAddress(request.getSourceMACAddress());
509
510 arp.setTargetProtocolAddress(((ARP) request.getPayload())
511 .getSenderProtocolAddress());
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700512 arp.setSenderProtocolAddress(srcIp.toInt());
Dusan Pajina22b9702015-02-12 16:25:23 +0100513
alshabibb5522ff2014-09-29 19:20:00 -0700514 eth.setPayload(arp);
515 return eth;
516 }
517
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800518 /**
519 * Builds an Neighbor Discovery reply based on a request.
520 *
521 * @param srcIp the IP address to use as the reply source
522 * @param srcMac the MAC address to use as the reply source
523 * @param request the Neighbor Solicitation request we got
524 * @return an Ethernet frame containing the Neighbor Advertisement reply
525 */
526 private Ethernet buildNdpReply(Ip6Address srcIp, MacAddress srcMac,
527 Ethernet request) {
528
529 Ethernet eth = new Ethernet();
530 eth.setDestinationMACAddress(request.getSourceMAC());
531 eth.setSourceMACAddress(srcMac);
532 eth.setEtherType(Ethernet.TYPE_IPV6);
533 eth.setVlanID(request.getVlanID());
534
535 IPv6 requestIp = (IPv6) request.getPayload();
536 IPv6 ipv6 = new IPv6();
537 ipv6.setSourceAddress(srcIp.toOctets());
538 ipv6.setDestinationAddress(requestIp.getSourceAddress());
539 ipv6.setHopLimit((byte) 255);
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800540
541 ICMP6 icmp6 = new ICMP6();
542 icmp6.setIcmpType(ICMP6.NEIGHBOR_ADVERTISEMENT);
543 icmp6.setIcmpCode((byte) 0);
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800544
545 NeighborAdvertisement nadv = new NeighborAdvertisement();
Dusan Pajina22b9702015-02-12 16:25:23 +0100546 nadv.setTargetAddress(srcIp.toOctets());
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800547 nadv.setSolicitedFlag((byte) 1);
548 nadv.setOverrideFlag((byte) 1);
Dusan Pajina22b9702015-02-12 16:25:23 +0100549 byte[] nadvData =
550 new byte[NeighborAdvertisement.OPTION_LENGTH_IEEE802_ADDRESS];
551 ByteBuffer bbNadv = ByteBuffer.wrap(nadvData);
552 byte nadvOptionType =
553 NeighborAdvertisement.OPTION_TYPE_TARGET_LL_ADDRESS;
554 // The Option length in 8-octets units
555 byte nadvOptionLength =
556 (NeighborAdvertisement.OPTION_LENGTH_IEEE802_ADDRESS + 7) / 8;
557 bbNadv.put(nadvOptionType);
558 bbNadv.put(nadvOptionLength);
559 bbNadv.put(srcMac.toBytes());
560 Data nadvPayload = new Data();
561 nadv.setPayload(nadvPayload.deserialize(nadvData, 0, nadvData.length));
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800562 icmp6.setPayload(nadv);
563
Dusan Pajina22b9702015-02-12 16:25:23 +0100564 ipv6.setPayload(icmp6);
565 eth.setPayload(ipv6);
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800566 return eth;
567 }
568
alshabibb5522ff2014-09-29 19:20:00 -0700569 public class InternalLinkListener implements LinkListener {
570
571 @Override
572 public void event(LinkEvent event) {
573 Link link = event.subject();
574 Device src = deviceService.getDevice(link.src().deviceId());
575 Device dst = deviceService.getDevice(link.dst().deviceId());
576 switch (event.type()) {
577 case LINK_ADDED:
578 synchronized (externalPorts) {
579 externalPorts.remove(src, link.src().port());
580 externalPorts.remove(dst, link.dst().port());
581 internalPorts.put(src, link.src().port());
582 internalPorts.put(dst, link.dst().port());
583 }
584
585 break;
586 case LINK_REMOVED:
587 synchronized (externalPorts) {
588 externalPorts.put(src, link.src().port());
589 externalPorts.put(dst, link.dst().port());
590 internalPorts.remove(src, link.src().port());
591 internalPorts.remove(dst, link.dst().port());
592 }
593
594 break;
595 case LINK_UPDATED:
596 // don't care about links being updated.
597 break;
598 default:
599 break;
600 }
601
602 }
603
604 }
605
606 public class InternalDeviceListener implements DeviceListener {
607
608 @Override
609 public void event(DeviceEvent event) {
610 Device device = event.subject();
611 switch (event.type()) {
612 case DEVICE_ADDED:
613 case DEVICE_AVAILABILITY_CHANGED:
alshabibb5522ff2014-09-29 19:20:00 -0700614 case DEVICE_SUSPENDED:
615 case DEVICE_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700616 // nothing to do in these cases; handled when links get reported
617 break;
618 case DEVICE_REMOVED:
619 synchronized (externalPorts) {
620 externalPorts.removeAll(device);
621 internalPorts.removeAll(device);
622 }
623 break;
624 case PORT_ADDED:
alshabib6eb438a2014-10-01 16:39:37 -0700625 case PORT_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700626 synchronized (externalPorts) {
alshabib6eb438a2014-10-01 16:39:37 -0700627 if (event.port().isEnabled()) {
628 externalPorts.put(device, event.port().number());
629 internalPorts.remove(device, event.port().number());
630 }
alshabibb5522ff2014-09-29 19:20:00 -0700631 }
632 break;
633 case PORT_REMOVED:
634 synchronized (externalPorts) {
635 externalPorts.remove(device, event.port().number());
636 internalPorts.remove(device, event.port().number());
637 }
638 break;
639 default:
640 break;
641
642 }
643
644 }
645
alshabibc274c902014-10-03 14:58:27 -0700646 }
alshabibb5522ff2014-09-29 19:20:00 -0700647
648}