blob: 35e0e3115551e47cc6399b8e5a6aad9c1907ce7a [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;
37import org.onlab.packet.ndp.NeighborSolicitation;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.ConnectPoint;
39import org.onosproject.net.Device;
40import org.onosproject.net.Host;
41import org.onosproject.net.HostId;
42import org.onosproject.net.Link;
43import org.onosproject.net.Port;
44import org.onosproject.net.PortNumber;
45import org.onosproject.net.device.DeviceEvent;
46import org.onosproject.net.device.DeviceListener;
47import org.onosproject.net.device.DeviceService;
48import org.onosproject.net.flow.DefaultTrafficTreatment;
49import org.onosproject.net.flow.TrafficTreatment;
50import org.onosproject.net.host.HostService;
51import org.onosproject.net.host.InterfaceIpAddress;
52import org.onosproject.net.host.PortAddresses;
53import org.onosproject.net.link.LinkEvent;
54import org.onosproject.net.link.LinkListener;
55import org.onosproject.net.link.LinkService;
56import org.onosproject.net.packet.DefaultOutboundPacket;
57import org.onosproject.net.packet.InboundPacket;
58import org.onosproject.net.packet.PacketContext;
59import org.onosproject.net.packet.PacketService;
60import org.onosproject.net.proxyarp.ProxyArpService;
alshabibb5522ff2014-09-29 19:20:00 -070061import org.slf4j.Logger;
62
Jonathan Hart6cd2f352015-01-13 17:44:45 -080063import java.nio.ByteBuffer;
64import java.util.HashSet;
65import java.util.List;
66import java.util.Map.Entry;
67import java.util.Set;
68
69import static com.google.common.base.Preconditions.checkArgument;
70import static com.google.common.base.Preconditions.checkNotNull;
71import static org.slf4j.LoggerFactory.getLogger;
alshabibb5522ff2014-09-29 19:20:00 -070072
alshabibb5522ff2014-09-29 19:20:00 -070073@Component(immediate = true)
74@Service
75public class ProxyArpManager implements ProxyArpService {
76
77 private final Logger log = getLogger(getClass());
78
79 private static final String MAC_ADDR_NULL = "Mac address cannot be null.";
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -080080 private static final String REQUEST_NULL = "ARP or NDP request cannot be null.";
alshabibb5522ff2014-09-29 19:20:00 -070081 private static final String REQUEST_NOT_ARP = "Ethernet frame does not contain ARP request.";
82 private static final String NOT_ARP_REQUEST = "ARP is not a request.";
Jonathan Hart704ca142014-10-09 09:34:39 -070083 private static final String NOT_ARP_REPLY = "ARP is not a reply.";
alshabibb5522ff2014-09-29 19:20:00 -070084
85 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected HostService hostService;
87
88 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
89 protected PacketService packetService;
90
91 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
92 protected LinkService linkService;
93
94 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
95 protected DeviceService deviceService;
96
97 private final Multimap<Device, PortNumber> internalPorts =
98 HashMultimap.<Device, PortNumber>create();
99
100 private final Multimap<Device, PortNumber> externalPorts =
101 HashMultimap.<Device, PortNumber>create();
102
103 /**
104 * Listens to both device service and link service to determine
105 * whether a port is internal or external.
106 */
107 @Activate
108 public void activate() {
109 deviceService.addListener(new InternalDeviceListener());
110 linkService.addListener(new InternalLinkListener());
111 determinePortLocations();
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800112
alshabibb5522ff2014-09-29 19:20:00 -0700113 log.info("Started");
114 }
115
116
117 @Deactivate
118 public void deactivate() {
119 log.info("Stopped");
120 }
121
122 @Override
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800123 public boolean isKnown(IpAddress addr) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700124 checkNotNull(addr, MAC_ADDR_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700125 Set<Host> hosts = hostService.getHostsByIp(addr);
126 return !hosts.isEmpty();
127 }
128
129 @Override
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700130 public void reply(Ethernet eth, ConnectPoint inPort) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700131 checkNotNull(eth, REQUEST_NULL);
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800132
133 if (eth.getEtherType() == Ethernet.TYPE_ARP) {
134 replyArp(eth, inPort);
135 } else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
136 replyNdp(eth, inPort);
137 }
138 }
139
140 private void replyArp(Ethernet eth, ConnectPoint inPort) {
alshabibb5522ff2014-09-29 19:20:00 -0700141 ARP arp = (ARP) eth.getPayload();
142 checkArgument(arp.getOpCode() == ARP.OP_REQUEST, NOT_ARP_REQUEST);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700143 checkNotNull(inPort);
144
Jonathan Hart6cd2f352015-01-13 17:44:45 -0800145 VlanId vlan = VlanId.vlanId(eth.getVlanID());
146
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700147 // If the request came from outside the network, only reply if it was
148 // for one of our external addresses.
149 if (isOutsidePort(inPort)) {
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800150 Ip4Address target =
151 Ip4Address.valueOf(arp.getTargetProtocolAddress());
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()) {
157 if (ia.ipAddress().equals(target)) {
158 Ethernet arpReply =
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800159 buildArpReply(target, 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
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800190 Set<Host> hosts = hostService.getHostsByIp(
191 Ip4Address.valueOf(arp.getTargetProtocolAddress()));
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) {
Jonathan Hartf84591d2015-01-16 14:33:43 -0800205 flood(eth, inPort);
alshabibb5522ff2014-09-29 19:20:00 -0700206 return;
207 }
208
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800209 //
210 // TODO find the correct IP address.
211 // Right now we use the first IPv4 address that is found.
212 //
213 for (IpAddress ipAddress : dst.ipAddresses()) {
214 Ip4Address ip4Address = ipAddress.getIp4Address();
215 if (ip4Address != null) {
216 Ethernet arpReply = buildArpReply(ip4Address, dst.mac(), eth);
217 // TODO: check send status with host service.
218 sendTo(arpReply, src.location());
219 break;
220 }
221 }
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700222 }
223
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800224 private void replyNdp(Ethernet eth, ConnectPoint inPort) {
225
226 IPv6 ipv6 = (IPv6) eth.getPayload();
227 ICMP6 icmpv6 = (ICMP6) ipv6.getPayload();
228 NeighborSolicitation nsol = (NeighborSolicitation) icmpv6.getPayload();
229
230 VlanId vlan = VlanId.vlanId(eth.getVlanID());
231
232 // If the request came from outside the network, only reply if it was
233 // for one of our external addresses.
234 if (isOutsidePort(inPort)) {
235 Ip6Address target =
236 Ip6Address.valueOf(nsol.getTargetAddress());
237 Set<PortAddresses> addressSet =
238 hostService.getAddressBindingsForPort(inPort);
239
240 for (PortAddresses addresses : addressSet) {
241 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
242 if (ia.ipAddress().equals(target)) {
243 Ethernet ndpReply =
244 buildNdpReply(target, addresses.mac(), eth);
245 sendTo(ndpReply, inPort);
246 }
247 }
248 }
249 return;
250 } else {
251 // If the source address matches one of our external addresses
252 // it could be a request from an internal host to an external
253 // address. Forward it over to the correct ports.
254 Ip6Address source =
255 Ip6Address.valueOf(nsol.getTargetAddress());
256 Set<PortAddresses> sourceAddresses = findPortsInSubnet(source);
257 boolean matched = false;
258 for (PortAddresses pa : sourceAddresses) {
259 for (InterfaceIpAddress ia : pa.ipAddresses()) {
260 if (ia.ipAddress().equals(source) &&
261 pa.vlan().equals(vlan)) {
262 matched = true;
263 sendTo(eth, pa.connectPoint());
264 }
265 }
266 }
267
268 if (matched) {
269 return;
270 }
271 }
272
273 // Continue with normal proxy ARP case
274
275 Set<Host> hosts = hostService.getHostsByIp(
276 Ip6Address.valueOf(nsol.getTargetAddress()));
277
278 Host dst = null;
279 Host src = hostService.getHost(HostId.hostId(eth.getSourceMAC(),
280 VlanId.vlanId(eth.getVlanID())));
281
282 for (Host host : hosts) {
283 if (host.vlan().equals(vlan)) {
284 dst = host;
285 break;
286 }
287 }
288
289 if (src == null || dst == null) {
290 flood(eth, inPort);
291 return;
292 }
293
294 //
295 // TODO find the correct IP address.
296 // Right now we use the first IPv4 address that is found.
297 //
298 for (IpAddress ipAddress : dst.ipAddresses()) {
299 Ip6Address ip6Address = ipAddress.getIp6Address();
300 if (ip6Address != null) {
301 Ethernet arpReply = buildNdpReply(ip6Address, dst.mac(), eth);
302 // TODO: check send status with host service.
303 sendTo(arpReply, src.location());
304 break;
305 }
306 }
307 }
308
309
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700310 /**
311 * Outputs the given packet out the given port.
312 *
313 * @param packet the packet to send
314 * @param outPort the port to send it out
315 */
316 private void sendTo(Ethernet packet, ConnectPoint outPort) {
317 if (internalPorts.containsEntry(
318 deviceService.getDevice(outPort.deviceId()), outPort.port())) {
319 // Sanity check to make sure we don't send the packet out an
320 // internal port and create a loop (could happen due to
321 // misconfiguration).
322 return;
323 }
324
tom9a693fd2014-10-03 11:32:19 -0700325 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700326 builder.setOutput(outPort.port());
327 packetService.emit(new DefaultOutboundPacket(outPort.deviceId(),
328 builder.build(), ByteBuffer.wrap(packet.serialize())));
329 }
330
331 /**
Jonathan Hart1f793a72014-11-12 23:22:02 -0800332 * Finds ports with an address in the subnet of the target address.
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700333 *
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700334 * @param target the target address to find a matching port for
Jonathan Hart1f793a72014-11-12 23:22:02 -0800335 * @return a set of PortAddresses describing ports in the subnet
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700336 */
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800337 private Set<PortAddresses> findPortsInSubnet(IpAddress target) {
Jonathan Hart1f793a72014-11-12 23:22:02 -0800338 Set<PortAddresses> result = new HashSet<PortAddresses>();
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700339 for (PortAddresses addresses : hostService.getAddressBindings()) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700340 for (InterfaceIpAddress ia : addresses.ipAddresses()) {
341 if (ia.subnetAddress().contains(target)) {
Jonathan Hart1f793a72014-11-12 23:22:02 -0800342 result.add(addresses);
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700343 }
344 }
345 }
Jonathan Hart1f793a72014-11-12 23:22:02 -0800346 return result;
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700347 }
348
349 /**
350 * Returns whether the given port is an outside-facing port with an IP
351 * address configured.
352 *
353 * @param port the port to check
354 * @return true if the port is an outside-facing port, otherwise false
355 */
356 private boolean isOutsidePort(ConnectPoint port) {
Pavlin Radoslavov76b0ae22014-10-27 15:33:19 -0700357 //
358 // TODO: Is this sufficient to identify outside-facing ports: just
359 // having IP addresses on a port?
360 //
Jonathan Harta887ba82014-11-03 15:20:52 -0800361 return !hostService.getAddressBindingsForPort(port).isEmpty();
alshabibb5522ff2014-09-29 19:20:00 -0700362 }
363
364 @Override
Jonathan Hartf84591d2015-01-16 14:33:43 -0800365 public void forward(Ethernet eth, ConnectPoint inPort) {
Yuta HIGUCHI59718042014-10-04 22:04:56 -0700366 checkNotNull(eth, REQUEST_NULL);
alshabibb5522ff2014-09-29 19:20:00 -0700367
368 Host h = hostService.getHost(HostId.hostId(eth.getDestinationMAC(),
369 VlanId.vlanId(eth.getVlanID())));
370
371 if (h == null) {
Jonathan Hartf84591d2015-01-16 14:33:43 -0800372 flood(eth, inPort);
alshabibb5522ff2014-09-29 19:20:00 -0700373 } else {
tom9a693fd2014-10-03 11:32:19 -0700374 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700375 builder.setOutput(h.location().port());
376 packetService.emit(new DefaultOutboundPacket(h.location().deviceId(),
377 builder.build(), ByteBuffer.wrap(eth.serialize())));
378 }
379
380 }
381
alshabibc274c902014-10-03 14:58:27 -0700382 @Override
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800383 public boolean handlePacket(PacketContext context) {
alshabibc274c902014-10-03 14:58:27 -0700384 InboundPacket pkt = context.inPacket();
385 Ethernet ethPkt = pkt.parsed();
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800386
387 if (ethPkt == null) {
388 return false;
389 }
390 if (ethPkt.getEtherType() == Ethernet.TYPE_ARP) {
391 return handleArp(context, ethPkt);
392 } else if (ethPkt.getEtherType() == Ethernet.TYPE_IPV6) {
393 return handleNdp(context, ethPkt);
alshabibc274c902014-10-03 14:58:27 -0700394 }
395 return false;
396 }
397
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800398 private boolean handleArp(PacketContext context, Ethernet ethPkt) {
399 ARP arp = (ARP) ethPkt.getPayload();
400
401 if (arp.getOpCode() == ARP.OP_REPLY) {
402 forward(ethPkt, context.inPacket().receivedFrom());
403 } else if (arp.getOpCode() == ARP.OP_REQUEST) {
404 reply(ethPkt, context.inPacket().receivedFrom());
405 } else {
406 return false;
407 }
408 context.block();
409 return true;
410 }
411
412 private boolean handleNdp(PacketContext context, Ethernet ethPkt) {
413 IPv6 ipv6 = (IPv6) ethPkt.getPayload();
414
415 if (ipv6.getNextHeader() != IPv6.PROTOCOL_ICMP6) {
416 return false;
417 }
418 ICMP6 icmpv6 = (ICMP6) ipv6.getPayload();
419 if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_ADVERTISEMENT) {
420 forward(ethPkt, context.inPacket().receivedFrom());
421 } else if (icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION) {
422 reply(ethPkt, context.inPacket().receivedFrom());
423 } else {
424 return false;
425 }
426 context.block();
427 return true;
428 }
429
alshabibb5522ff2014-09-29 19:20:00 -0700430 /**
431 * Flood the arp request at all edges in the network.
432 * @param request the arp request.
433 */
Jonathan Hartf84591d2015-01-16 14:33:43 -0800434 private void flood(Ethernet request, ConnectPoint inPort) {
alshabibb5522ff2014-09-29 19:20:00 -0700435 TrafficTreatment.Builder builder = null;
436 ByteBuffer buf = ByteBuffer.wrap(request.serialize());
437
438 synchronized (externalPorts) {
439 for (Entry<Device, PortNumber> entry : externalPorts.entries()) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700440 ConnectPoint cp = new ConnectPoint(entry.getKey().id(), entry.getValue());
Jonathan Hartf84591d2015-01-16 14:33:43 -0800441 if (isOutsidePort(cp) || cp.equals(inPort)) {
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700442 continue;
443 }
444
tom9a693fd2014-10-03 11:32:19 -0700445 builder = DefaultTrafficTreatment.builder();
alshabibb5522ff2014-09-29 19:20:00 -0700446 builder.setOutput(entry.getValue());
447 packetService.emit(new DefaultOutboundPacket(entry.getKey().id(),
448 builder.build(), buf));
449 }
alshabibb5522ff2014-09-29 19:20:00 -0700450 }
451 }
452
453 /**
454 * Determines the location of all known ports in the system.
455 */
456 private void determinePortLocations() {
457 Iterable<Device> devices = deviceService.getDevices();
458 Iterable<Link> links = null;
459 List<PortNumber> ports = null;
460 for (Device d : devices) {
461 ports = buildPortNumberList(deviceService.getPorts(d.id()));
462 links = linkService.getLinks();
463 for (Link l : links) {
464 // for each link, mark the concerned ports as internal
465 // and the remaining ports are therefore external.
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700466 if (l.src().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700467 && ports.contains(l.src().port())) {
468 ports.remove(l.src().port());
469 internalPorts.put(d, l.src().port());
470 }
Yuta HIGUCHI3541bf22014-10-04 22:06:19 -0700471 if (l.dst().deviceId().equals(d.id())
alshabibb5522ff2014-09-29 19:20:00 -0700472 && ports.contains(l.dst().port())) {
473 ports.remove(l.dst().port());
474 internalPorts.put(d, l.dst().port());
475 }
476 }
477 synchronized (externalPorts) {
478 externalPorts.putAll(d, ports);
479 }
480 }
481
482 }
483
484 private List<PortNumber> buildPortNumberList(List<Port> ports) {
485 List<PortNumber> portNumbers = Lists.newLinkedList();
486 for (Port p : ports) {
487 portNumbers.add(p.number());
488 }
489 return portNumbers;
490 }
491
492 /**
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700493 * Builds an ARP reply based on a request.
494 *
495 * @param srcIp the IP address to use as the reply source
496 * @param srcMac the MAC address to use as the reply source
497 * @param request the ARP request we got
498 * @return an Ethernet frame containing the ARP reply
alshabibb5522ff2014-09-29 19:20:00 -0700499 */
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800500 private Ethernet buildArpReply(Ip4Address srcIp, MacAddress srcMac,
Jonathan Hartdbdbdbb2014-10-06 18:35:30 -0700501 Ethernet request) {
502
alshabibb5522ff2014-09-29 19:20:00 -0700503 Ethernet eth = new Ethernet();
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800504 eth.setDestinationMACAddress(request.getSourceMAC());
505 eth.setSourceMACAddress(srcMac);
alshabibb5522ff2014-09-29 19:20:00 -0700506 eth.setEtherType(Ethernet.TYPE_ARP);
507 eth.setVlanID(request.getVlanID());
508
509 ARP arp = new ARP();
510 arp.setOpCode(ARP.OP_REPLY);
511 arp.setProtocolType(ARP.PROTO_TYPE_IP);
512 arp.setHardwareType(ARP.HW_TYPE_ETHERNET);
alshabib6eb438a2014-10-01 16:39:37 -0700513
Pavlin Radoslavov5b5dc482014-11-05 14:48:08 -0800514 arp.setProtocolAddressLength((byte) Ip4Address.BYTE_LENGTH);
alshabibb5522ff2014-09-29 19:20:00 -0700515 arp.setHardwareAddressLength((byte) Ethernet.DATALAYER_ADDRESS_LENGTH);
Yuta HIGUCHI3e848a82014-11-02 20:19:42 -0800516 arp.setSenderHardwareAddress(srcMac.toBytes());
alshabibb5522ff2014-09-29 19:20:00 -0700517 arp.setTargetHardwareAddress(request.getSourceMACAddress());
518
519 arp.setTargetProtocolAddress(((ARP) request.getPayload())
520 .getSenderProtocolAddress());
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700521 arp.setSenderProtocolAddress(srcIp.toInt());
alshabibb5522ff2014-09-29 19:20:00 -0700522 eth.setPayload(arp);
523 return eth;
524 }
525
Kunihiro Ishigurof1bff502015-01-30 15:47:06 -0800526 /**
527 * Builds an Neighbor Discovery reply based on a request.
528 *
529 * @param srcIp the IP address to use as the reply source
530 * @param srcMac the MAC address to use as the reply source
531 * @param request the Neighbor Solicitation request we got
532 * @return an Ethernet frame containing the Neighbor Advertisement reply
533 */
534 private Ethernet buildNdpReply(Ip6Address srcIp, MacAddress srcMac,
535 Ethernet request) {
536
537 Ethernet eth = new Ethernet();
538 eth.setDestinationMACAddress(request.getSourceMAC());
539 eth.setSourceMACAddress(srcMac);
540 eth.setEtherType(Ethernet.TYPE_IPV6);
541 eth.setVlanID(request.getVlanID());
542
543 IPv6 requestIp = (IPv6) request.getPayload();
544 IPv6 ipv6 = new IPv6();
545 ipv6.setSourceAddress(srcIp.toOctets());
546 ipv6.setDestinationAddress(requestIp.getSourceAddress());
547 ipv6.setHopLimit((byte) 255);
548 eth.setPayload(ipv6);
549
550 ICMP6 icmp6 = new ICMP6();
551 icmp6.setIcmpType(ICMP6.NEIGHBOR_ADVERTISEMENT);
552 icmp6.setIcmpCode((byte) 0);
553 ipv6.setPayload(icmp6);
554
555 NeighborAdvertisement nadv = new NeighborAdvertisement();
556 nadv.setTargetAddress(srcMac.toBytes());
557 nadv.setSolicitedFlag((byte) 1);
558 nadv.setOverrideFlag((byte) 1);
559 icmp6.setPayload(nadv);
560
561 return eth;
562 }
563
alshabibb5522ff2014-09-29 19:20:00 -0700564 public class InternalLinkListener implements LinkListener {
565
566 @Override
567 public void event(LinkEvent event) {
568 Link link = event.subject();
569 Device src = deviceService.getDevice(link.src().deviceId());
570 Device dst = deviceService.getDevice(link.dst().deviceId());
571 switch (event.type()) {
572 case LINK_ADDED:
573 synchronized (externalPorts) {
574 externalPorts.remove(src, link.src().port());
575 externalPorts.remove(dst, link.dst().port());
576 internalPorts.put(src, link.src().port());
577 internalPorts.put(dst, link.dst().port());
578 }
579
580 break;
581 case LINK_REMOVED:
582 synchronized (externalPorts) {
583 externalPorts.put(src, link.src().port());
584 externalPorts.put(dst, link.dst().port());
585 internalPorts.remove(src, link.src().port());
586 internalPorts.remove(dst, link.dst().port());
587 }
588
589 break;
590 case LINK_UPDATED:
591 // don't care about links being updated.
592 break;
593 default:
594 break;
595 }
596
597 }
598
599 }
600
601 public class InternalDeviceListener implements DeviceListener {
602
603 @Override
604 public void event(DeviceEvent event) {
605 Device device = event.subject();
606 switch (event.type()) {
607 case DEVICE_ADDED:
608 case DEVICE_AVAILABILITY_CHANGED:
alshabibb5522ff2014-09-29 19:20:00 -0700609 case DEVICE_SUSPENDED:
610 case DEVICE_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700611 // nothing to do in these cases; handled when links get reported
612 break;
613 case DEVICE_REMOVED:
614 synchronized (externalPorts) {
615 externalPorts.removeAll(device);
616 internalPorts.removeAll(device);
617 }
618 break;
619 case PORT_ADDED:
alshabib6eb438a2014-10-01 16:39:37 -0700620 case PORT_UPDATED:
alshabibb5522ff2014-09-29 19:20:00 -0700621 synchronized (externalPorts) {
alshabib6eb438a2014-10-01 16:39:37 -0700622 if (event.port().isEnabled()) {
623 externalPorts.put(device, event.port().number());
624 internalPorts.remove(device, event.port().number());
625 }
alshabibb5522ff2014-09-29 19:20:00 -0700626 }
627 break;
628 case PORT_REMOVED:
629 synchronized (externalPorts) {
630 externalPorts.remove(device, event.port().number());
631 internalPorts.remove(device, event.port().number());
632 }
633 break;
634 default:
635 break;
636
637 }
638
639 }
640
alshabibc274c902014-10-03 14:58:27 -0700641 }
alshabibb5522ff2014-09-29 19:20:00 -0700642
643}