blob: 161518c957b51ded626bf88f73200202bb97485e [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 (isOutsidePort(inPort)) {
Jonathan Hart7d1496b2015-03-10 22:00:48 -0700150 // If the request came from outside the network, only reply if it was
151 // for one of our external addresses.
Jonathan Harta887ba82014-11-03 15:20:52 -0800152 Set<PortAddresses> addressSet =
Jonathan Hart7d1496b2015-03-10 22:00:48 -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 =
Jonathan Hart7d1496b2015-03-10 22:00:48 -0700159 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;
165 }
166
Jonathan Hart7d1496b2015-03-10 22:00:48 -0700167 // See if we have the target host in the host store
alshabibb5522ff2014-09-29 19:20:00 -0700168
Dusan Pajina22b9702015-02-12 16:25:23 +0100169 Set<Host> hosts = hostService.getHostsByIp(targetAddress);
alshabibb5522ff2014-09-29 19:20:00 -0700170
171 Host dst = null;
172 Host src = hostService.getHost(HostId.hostId(eth.getSourceMAC(),
173 VlanId.vlanId(eth.getVlanID())));
174
175 for (Host host : hosts) {
176 if (host.vlan().equals(vlan)) {
177 dst = host;
178 break;
179 }
180 }
181
Jonathan Hart7d1496b2015-03-10 22:00:48 -0700182 if (src != null && dst != null) {
183 // We know the target host so we can respond
184 Ethernet arpReply = buildArpReply(targetAddress, dst.mac(), eth);
185 sendTo(arpReply, inPort);
186 return;
187 }
188
189 // If the source address matches one of our external addresses
190 // it could be a request from an internal host to an external
191 // address. Forward it over to the correct port.
192 Ip4Address source =
193 Ip4Address.valueOf(arp.getSenderProtocolAddress());
194 Set<PortAddresses> sourceAddresses = findPortsInSubnet(source);
195 boolean matched = false;
196 for (PortAddresses pa : sourceAddresses) {
197 for (InterfaceIpAddress ia : pa.ipAddresses()) {
198 if (ia.ipAddress().equals(source) &&
199 pa.vlan().equals(vlan)) {
200 matched = true;
201 sendTo(eth, pa.connectPoint());
202 break;
203 }
204 }
205 }
206
207 if (matched) {
alshabibb5522ff2014-09-29 19:20:00 -0700208 return;
209 }
210
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800211 //
Jonathan Hart7d1496b2015-03-10 22:00:48 -0700212 // The request couldn't be resolved.
213 // Flood the request on all ports except the incoming port.
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800214 //
Jonathan Hart7d1496b2015-03-10 22:00:48 -0700215 flood(eth, inPort);
216 return;
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 =
Pavlin Radoslavovcef52062015-02-12 16:57:17 -0800238 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 =
Pavlin Radoslavovcef52062015-02-12 16:57:17 -0800249 Ip6Address.valueOf(ipv6.getSourceAddress());
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800250 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());
Pavlin Radoslavovcef52062015-02-12 16:57:17 -0800258 break;
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800259 }
260 }
261 }
262
263 if (matched) {
264 return;
265 }
266 }
267
268 // Continue with normal proxy ARP case
269
Dusan Pajina22b9702015-02-12 16:25:23 +0100270 Set<Host> hosts = hostService.getHostsByIp(targetAddress);
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800271
272 Host dst = null;
273 Host src = hostService.getHost(HostId.hostId(eth.getSourceMAC(),
274 VlanId.vlanId(eth.getVlanID())));
275
276 for (Host host : hosts) {
277 if (host.vlan().equals(vlan)) {
278 dst = host;
279 break;
280 }
281 }
282
283 if (src == null || dst == null) {
Dusan Pajina22b9702015-02-12 16:25:23 +0100284 //
285 // The request couldn't be resolved.
286 // Flood the request on all ports except the incoming ports.
287 //
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800288 flood(eth, inPort);
289 return;
290 }
291
292 //
Dusan Pajina22b9702015-02-12 16:25:23 +0100293 // Reply on the port the request was received on
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800294 //
Dusan Pajina22b9702015-02-12 16:25:23 +0100295 Ethernet ndpReply = buildNdpReply(targetAddress, dst.mac(), eth);
296 sendTo(ndpReply, inPort);
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800297 }
298
299
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700300 /**
301 * Outputs the given packet out the given port.
302 *
303 * @param packet the packet to send
304 * @param outPort the port to send it out
305 */
306 private void sendTo(Ethernet packet, ConnectPoint outPort) {
307 if (internalPorts.containsEntry(
308 deviceService.getDevice(outPort.deviceId()), outPort.port())) {
309 // Sanity check to make sure we don't send the packet out an
310 // internal port and create a loop (could happen due to
311 // misconfiguration).
312 return;
313 }
314
tom9a693fd2014-10-03 11:32:19 -0700315 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700316 builder.setOutput(outPort.port());
317 packetService.emit(new DefaultOutboundPacket(outPort.deviceId(),
318 builder.build(), ByteBuffer.wrap(packet.serialize())));
319 }
320
321 /**
Jonathan Hart1f793a72014-11-12 23:22:02 -0800322 * Finds ports with an address in the subnet of the target address.
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700323 *
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700324 * @param target the target address to find a matching port for
Jonathan Hart1f793a72014-11-12 23:22:02 -0800325 * @return a set of PortAddresses describing ports in the subnet
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700326 */
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800327 private Set<PortAddresses> findPortsInSubnet(IpAddress target) {
Jonathan Hart1f793a72014-11-12 23:22:02 -0800328 Set<PortAddresses> result = new HashSet<PortAddresses>();
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700329 for (PortAddresses addresses : hostService.getAddressBindings()) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700330 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
331 if (ia.subnetAddress().contains(target)) {
Jonathan Hart1f793a72014-11-12 23:22:02 -0800332 result.add(addresses);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700333 }
334 }
335 }
Jonathan Hart1f793a72014-11-12 23:22:02 -0800336 return result;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700337 }
338
339 /**
340 * Returns whether the given port is an outside-facing port with an IP
341 * address configured.
342 *
343 * @param port the port to check
344 * @return true if the port is an outside-facing port, otherwise false
345 */
346 private boolean isOutsidePort(ConnectPoint port) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700347 //
348 // TODO: Is this sufficient to identify outside-facing ports: just
349 // having IP addresses on a port?
350 //
Jonathan Harta887ba82014-11-03 15:20:52 -0800351 return !hostService.getAddressBindingsForPort(port).isEmpty();
alshabibb5522ff2014-09-29 19:20:00 -0700352 }
353
354 @Override
Jonathan Hartf84591d2015-01-16 14:33:43 -0800355 public void forward(Ethernet eth, ConnectPoint inPort) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700356 checkNotNull(eth, REQUEST_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700357
358 Host h = hostService.getHost(HostId.hostId(eth.getDestinationMAC(),
359 VlanId.vlanId(eth.getVlanID())));
360
361 if (h == null) {
Jonathan Hartf84591d2015-01-16 14:33:43 -0800362 flood(eth, inPort);
alshabibb5522ff2014-09-29 19:20:00 -0700363 } else {
tom9a693fd2014-10-03 11:32:19 -0700364 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700365 builder.setOutput(h.location().port());
366 packetService.emit(new DefaultOutboundPacket(h.location().deviceId(),
367 builder.build(), ByteBuffer.wrap(eth.serialize())));
368 }
369
370 }
371
alshabibc274c902014-10-03 14:58:27 -0700372 @Override
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800373 public boolean handlePacket(PacketContext context) {
alshabibc274c902014-10-03 14:58:27 -0700374 InboundPacket pkt = context.inPacket();
375 Ethernet ethPkt = pkt.parsed();
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800376
377 if (ethPkt == null) {
378 return false;
379 }
380 if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) {
381 return handleArp(context, ethPkt);
382 } else if (ethPkt.getEtherType() == Ethernet.TYPE_IPV6) {
383 return handleNdp(context, ethPkt);
alshabibc274c902014-10-03 14:58:27 -0700384 }
385 return false;
386 }
387
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800388 private boolean handleArp(PacketContext context, Ethernet ethPkt) {
389 ARP arp = (ARP) ethPkt.getPayload();
390
391 if (arp.getOpCode() == ARP.OP_REPLY) {
392 forward(ethPkt, context.inPacket().receivedFrom());
393 } else if (arp.getOpCode() == ARP.OP_REQUEST) {
394 reply(ethPkt, context.inPacket().receivedFrom());
395 } else {
396 return false;
397 }
398 context.block();
399 return true;
400 }
401
402 private boolean handleNdp(PacketContext context, Ethernet ethPkt) {
403 IPv6 ipv6 = (IPv6) ethPkt.getPayload();
404
405 if (ipv6.getNextHeader() != IPv6.PROTOCOL_ICMP6) {
406 return false;
407 }
408 ICMP6 icmpv6 = (ICMP6) ipv6.getPayload();
409 if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_ADVERTISEMENT) {
410 forward(ethPkt, context.inPacket().receivedFrom());
411 } else if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION) {
412 reply(ethPkt, context.inPacket().receivedFrom());
413 } else {
414 return false;
415 }
416 context.block();
417 return true;
418 }
419
alshabibb5522ff2014-09-29 19:20:00 -0700420 /**
421 * Flood the arp request at all edges in the network.
Dusan Pajina22b9702015-02-12 16:25:23 +0100422 *
423 * @param request the arp request
424 * @param inPort the connect point the arp request was received on
alshabibb5522ff2014-09-29 19:20:00 -0700425 */
Jonathan Hartf84591d2015-01-16 14:33:43 -0800426 private void flood(Ethernet request, ConnectPoint inPort) {
alshabibb5522ff2014-09-29 19:20:00 -0700427 TrafficTreatment.Builder builder = null;
428 ByteBuffer buf = ByteBuffer.wrap(request.serialize());
429
430 synchronized (externalPorts) {
431 for (Entry<Device, PortNumber> entry : externalPorts.entries()) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700432 ConnectPoint cp = new ConnectPoint(entry.getKey().id(), entry.getValue());
Jonathan Hartf84591d2015-01-16 14:33:43 -0800433 if (isOutsidePort(cp) || cp.equals(inPort)) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700434 continue;
435 }
436
tom9a693fd2014-10-03 11:32:19 -0700437 builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700438 builder.setOutput(entry.getValue());
439 packetService.emit(new DefaultOutboundPacket(entry.getKey().id(),
440 builder.build(), buf));
441 }
alshabibb5522ff2014-09-29 19:20:00 -0700442 }
443 }
444
445 /**
446 * Determines the location of all known ports in the system.
447 */
448 private void determinePortLocations() {
449 Iterable<Device> devices = deviceService.getDevices();
450 Iterable<Link> links = null;
451 List<PortNumber> ports = null;
452 for (Device d : devices) {
453 ports = buildPortNumberList(deviceService.getPorts(d.id()));
454 links = linkService.getLinks();
455 for (Link l : links) {
456 // for each link, mark the concerned ports as internal
457 // and the remaining ports are therefore external.
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700458 if (l.src().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700459 && ports.contains(l.src().port())) {
460 ports.remove(l.src().port());
461 internalPorts.put(d, l.src().port());
462 }
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700463 if (l.dst().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700464 && ports.contains(l.dst().port())) {
465 ports.remove(l.dst().port());
466 internalPorts.put(d, l.dst().port());
467 }
468 }
469 synchronized (externalPorts) {
470 externalPorts.putAll(d, ports);
471 }
472 }
473
474 }
475
476 private List<PortNumber> buildPortNumberList(List<Port> ports) {
477 List<PortNumber> portNumbers = Lists.newLinkedList();
478 for (Port p : ports) {
479 portNumbers.add(p.number());
480 }
481 return portNumbers;
482 }
483
484 /**
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700485 * Builds an ARP reply based on a request.
486 *
487 * @param srcIp the IP address to use as the reply source
488 * @param srcMac the MAC address to use as the reply source
489 * @param request the ARP request we got
490 * @return an Ethernet frame containing the ARP reply
alshabibb5522ff2014-09-29 19:20:00 -0700491 */
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800492 private Ethernet buildArpReply(Ip4Address srcIp, MacAddress srcMac,
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700493 Ethernet request) {
494
alshabibb5522ff2014-09-29 19:20:00 -0700495 Ethernet eth = new Ethernet();
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800496 eth.setDestinationMACAddress(request.getSourceMAC());
497 eth.setSourceMACAddress(srcMac);
alshabibb5522ff2014-09-29 19:20:00 -0700498 eth.setEtherType(Ethernet.TYPE_ARP);
499 eth.setVlanID(request.getVlanID());
500
501 ARP arp = new ARP();
502 arp.setOpCode(ARP.OP_REPLY);
503 arp.setProtocolType(ARP.PROTO_TYPE_IP);
504 arp.setHardwareType(ARP.HW_TYPE_ETHERNET);
alshabib6eb438a2014-10-01 16:39:37 -0700505
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800506 arp.setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH);
alshabibb5522ff2014-09-29 19:20:00 -0700507 arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH);
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800508 arp.setSenderHardwareAddress(srcMac.toBytes());
alshabibb5522ff2014-09-29 19:20:00 -0700509 arp.setTargetHardwareAddress(request.getSourceMACAddress());
510
511 arp.setTargetProtocolAddress(((ARP) request.getPayload())
512 .getSenderProtocolAddress());
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700513 arp.setSenderProtocolAddress(srcIp.toInt());
Dusan Pajina22b9702015-02-12 16:25:23 +0100514
alshabibb5522ff2014-09-29 19:20:00 -0700515 eth.setPayload(arp);
516 return eth;
517 }
518
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800519 /**
520 * Builds an Neighbor Discovery reply based on a request.
521 *
522 * @param srcIp the IP address to use as the reply source
523 * @param srcMac the MAC address to use as the reply source
524 * @param request the Neighbor Solicitation request we got
525 * @return an Ethernet frame containing the Neighbor Advertisement reply
526 */
527 private Ethernet buildNdpReply(Ip6Address srcIp, MacAddress srcMac,
528 Ethernet request) {
529
530 Ethernet eth = new Ethernet();
531 eth.setDestinationMACAddress(request.getSourceMAC());
532 eth.setSourceMACAddress(srcMac);
533 eth.setEtherType(Ethernet.TYPE_IPV6);
534 eth.setVlanID(request.getVlanID());
535
536 IPv6 requestIp = (IPv6) request.getPayload();
537 IPv6 ipv6 = new IPv6();
538 ipv6.setSourceAddress(srcIp.toOctets());
539 ipv6.setDestinationAddress(requestIp.getSourceAddress());
540 ipv6.setHopLimit((byte) 255);
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800541
542 ICMP6 icmp6 = new ICMP6();
543 icmp6.setIcmpType(ICMP6.NEIGHBOR_ADVERTISEMENT);
544 icmp6.setIcmpCode((byte) 0);
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800545
546 NeighborAdvertisement nadv = new NeighborAdvertisement();
Dusan Pajina22b9702015-02-12 16:25:23 +0100547 nadv.setTargetAddress(srcIp.toOctets());
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800548 nadv.setSolicitedFlag((byte) 1);
549 nadv.setOverrideFlag((byte) 1);
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800550 nadv.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS,
551 srcMac.toBytes());
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800552
Pavlin Radoslavova2626ef2015-02-18 18:33:25 -0800553 icmp6.setPayload(nadv);
Dusan Pajina22b9702015-02-12 16:25:23 +0100554 ipv6.setPayload(icmp6);
555 eth.setPayload(ipv6);
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800556 return eth;
557 }
558
alshabibb5522ff2014-09-29 19:20:00 -0700559 public class InternalLinkListener implements LinkListener {
560
561 @Override
562 public void event(LinkEvent event) {
563 Link link = event.subject();
564 Device src = deviceService.getDevice(link.src().deviceId());
565 Device dst = deviceService.getDevice(link.dst().deviceId());
566 switch (event.type()) {
567 case LINK_ADDED:
568 synchronized (externalPorts) {
569 externalPorts.remove(src, link.src().port());
570 externalPorts.remove(dst, link.dst().port());
571 internalPorts.put(src, link.src().port());
572 internalPorts.put(dst, link.dst().port());
573 }
574
575 break;
576 case LINK_REMOVED:
577 synchronized (externalPorts) {
578 externalPorts.put(src, link.src().port());
579 externalPorts.put(dst, link.dst().port());
580 internalPorts.remove(src, link.src().port());
581 internalPorts.remove(dst, link.dst().port());
582 }
583
584 break;
585 case LINK_UPDATED:
586 // don't care about links being updated.
587 break;
588 default:
589 break;
590 }
591
592 }
593
594 }
595
596 public class InternalDeviceListener implements DeviceListener {
597
598 @Override
599 public void event(DeviceEvent event) {
600 Device device = event.subject();
601 switch (event.type()) {
602 case DEVICE_ADDED:
603 case DEVICE_AVAILABILITY_CHANGED:
alshabibb5522ff2014-09-29 19:20:00 -0700604 case DEVICE_SUSPENDED:
605 case DEVICE_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700606 // nothing to do in these cases; handled when links get reported
607 break;
608 case DEVICE_REMOVED:
609 synchronized (externalPorts) {
610 externalPorts.removeAll(device);
611 internalPorts.removeAll(device);
612 }
613 break;
614 case PORT_ADDED:
alshabib6eb438a2014-10-01 16:39:37 -0700615 case PORT_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700616 synchronized (externalPorts) {
alshabib6eb438a2014-10-01 16:39:37 -0700617 if (event.port().isEnabled()) {
618 externalPorts.put(device, event.port().number());
619 internalPorts.remove(device, event.port().number());
620 }
alshabibb5522ff2014-09-29 19:20:00 -0700621 }
622 break;
623 case PORT_REMOVED:
624 synchronized (externalPorts) {
625 externalPorts.remove(device, event.port().number());
626 internalPorts.remove(device, event.port().number());
627 }
628 break;
629 default:
630 break;
631
632 }
633
634 }
635
alshabibc274c902014-10-03 14:58:27 -0700636 }
alshabibb5522ff2014-09-29 19:20:00 -0700637
638}