Charles M.C. Chan
Committed by Gerrit Code Review

Trace IPv6 hosts

ONOS-511: Implement NeighborSolicitation

Change-Id: I9aaf35d499cfc7885c74f9c4bf281210ef9f3969

ONOS-507: Trace NeighborSolicitation/NeighborAdvertisement/IPv6 packets in HostLocationProvider
	* Complete Javadoc of IPv6, ICMP6, NeighborAdvertisement and NeighborSolicitation
		- The Javadoc for serialize() is removed since the one in its superclass just works fine.
	* Change 'diffServ' in IPv6 to 'trafficClass' to meet the field name in RFC.
		- The setter method, getter method and unit test are also updated accordingly.
	* Add IpAddress.isZero() to determine if this address is zero.
		- The unit test is also updated accordingly.
	* Fix misuse of IpAddress.valueOf(int) in HostLocationProvider

Change-Id: Id0d873aeb1bc61bf26d4964e7aab4bb06ccd0a38
......@@ -46,6 +46,10 @@ import org.onosproject.net.topology.TopologyService;
import org.onlab.packet.ARP;
import org.onlab.packet.Ethernet;
import org.onlab.packet.IpAddress;
import org.onlab.packet.IPacket;
import org.onlab.packet.IPv6;
import org.onlab.packet.NeighborAdvertisement;
import org.onlab.packet.NeighborSolicitation;
import org.onlab.packet.VlanId;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
......@@ -155,22 +159,43 @@ public class HostLocationProvider extends AbstractProvider implements HostProvid
HostId hid = HostId.hostId(eth.getSourceMAC(), vlan);
// Potentially a new or moved host
// ARP: possible new hosts, update both location and IP
if (eth.getEtherType() == Ethernet.TYPE_ARP) {
ARP arp = (ARP) eth.getPayload();
IpAddress ip =
IpAddress.valueOf(IpAddress.Version.INET,
arp.getSenderProtocolAddress());
IpAddress ip = IpAddress.valueOf(IpAddress.Version.INET, arp.getSenderProtocolAddress());
HostDescription hdescr =
new DefaultHostDescription(eth.getSourceMAC(), vlan, hloc, ip);
providerService.hostDetected(hid, hdescr);
// IPv4: update location only
} else if (eth.getEtherType() == Ethernet.TYPE_IPV4) {
//Do not learn new ip from ip packet.
HostDescription hdescr =
new DefaultHostDescription(eth.getSourceMAC(), vlan, hloc);
providerService.hostDetected(hid, hdescr);
// NeighborAdvertisement and NeighborSolicitation: possible new hosts, update both location and IP
// IPv6: update location only
} else if (eth.getEtherType() == Ethernet.TYPE_IPV6) {
IpAddress ip = null;
IPv6 ipv6 = (IPv6) eth.getPayload();
IPacket iPkt = ipv6;
while (iPkt != null) {
if (iPkt instanceof NeighborAdvertisement || iPkt instanceof NeighborSolicitation) {
IpAddress sourceAddress =
IpAddress.valueOf(IpAddress.Version.INET6, ipv6.getSourceAddress());
// Ignore DAD packets, in which source address is all zeros.
if (!sourceAddress.isZero()) {
ip = sourceAddress;
break;
}
}
iPkt = iPkt.getPayload();
}
HostDescription hdescr = (ip == null) ?
new DefaultHostDescription(eth.getSourceMAC(), vlan, hloc) :
new DefaultHostDescription(eth.getSourceMAC(), vlan, hloc, ip);
providerService.hostDetected(hid, hdescr);
}
}
}
......
......@@ -23,8 +23,7 @@ import java.util.HashMap;
import java.util.Map;
/**
* Implements ICMPv6 packet format.
*
* Implements ICMPv6 packet format. (RFC 4443)
*/
public class ICMP6 extends BasePacket {
public static final byte HEADER_LENGTH = 4; // bytes
......@@ -37,6 +36,7 @@ public class ICMP6 extends BasePacket {
new HashMap<>();
static {
ICMP6.PROTOCOL_CLASS_MAP.put(ICMP6.NEIGHBOR_SOLICITATION, NeighborSolicitation.class);
ICMP6.PROTOCOL_CLASS_MAP.put(ICMP6.NEIGHBOR_ADVERTISEMENT, NeighborAdvertisement.class);
}
......@@ -45,15 +45,18 @@ public class ICMP6 extends BasePacket {
protected short checksum;
/**
* @return the icmpType
* Gets ICMP6 type.
*
* @return the ICMP6 type
*/
public byte getIcmpType() {
return this.icmpType;
}
/**
* @param icmpType
* to set
* Sets ICMP6 type.
*
* @param icmpType the ICMP type to set
* @return this
*/
public ICMP6 setIcmpType(final byte icmpType) {
......@@ -62,15 +65,18 @@ public class ICMP6 extends BasePacket {
}
/**
* @return the icmp code
* Gets ICMP6 code.
*
* @return the ICMP6 code
*/
public byte getIcmpCode() {
return this.icmpCode;
}
/**
* @param icmpCode
* code to set
* Sets ICMP6 code.
*
* @param icmpCode the ICMP6 code to set
* @return this
*/
public ICMP6 setIcmpCode(final byte icmpCode) {
......@@ -79,6 +85,8 @@ public class ICMP6 extends BasePacket {
}
/**
* Gets checksum.
*
* @return the checksum
*/
public short getChecksum() {
......@@ -86,8 +94,9 @@ public class ICMP6 extends BasePacket {
}
/**
* @param checksum
* the checksum to set
* Sets checksum.
*
* @param checksum the checksum to set
* @return this
*/
public ICMP6 setChecksum(final short checksum) {
......@@ -95,11 +104,6 @@ public class ICMP6 extends BasePacket {
return this;
}
/**
* Serializes the packet. Will compute and set the following fields if they
* are set to specific values at the time serialize is called: -checksum : 0
* -length : 0
*/
@Override
public byte[] serialize() {
byte[] payloadData = null;
......
......@@ -24,7 +24,7 @@ import java.util.HashMap;
import java.util.Map;
/**
*
* Implements IPv6 packet format. (RFC 2460)
*/
public class IPv6 extends BasePacket {
public static final byte FIXED_HEADER_LENGTH = 40; // bytes
......@@ -43,7 +43,7 @@ public class IPv6 extends BasePacket {
}
protected byte version;
protected byte diffServ;
protected byte trafficClass;
protected int flowLabel;
protected short payloadLength;
protected byte nextHeader;
......@@ -52,7 +52,7 @@ public class IPv6 extends BasePacket {
protected byte[] destinationAddress = new byte[Ip6Address.BYTE_LENGTH];
/**
* Default constructor that sets the version to 4.
* Default constructor that sets the version to 6.
*/
public IPv6() {
super();
......@@ -60,15 +60,18 @@ public class IPv6 extends BasePacket {
}
/**
* @return the version
* Gets IP version.
*
* @return the IP version
*/
public byte getVersion() {
return this.version;
}
/**
* @param version
* the version to set
* Sets IP version.
*
* @param version the IP version to set
* @return this
*/
public IPv6 setVersion(final byte version) {
......@@ -77,32 +80,38 @@ public class IPv6 extends BasePacket {
}
/**
* @return the diffServ
* Gets traffic class.
*
* @return the traffic class
*/
public byte getDiffServ() {
return this.diffServ;
public byte getTrafficClass() {
return this.trafficClass;
}
/**
* @param diffServ
* the diffServ to set
* Sets traffic class.
*
* @param trafficClass the traffic class to set
* @return this
*/
public IPv6 setDiffServ(final byte diffServ) {
this.diffServ = diffServ;
public IPv6 setTrafficClass(final byte trafficClass) {
this.trafficClass = trafficClass;
return this;
}
/**
* @return the flowLabel
* Gets flow label.
*
* @return the flow label
*/
public int getFlowLabel() {
return this.flowLabel;
}
/**
* @param flowLabel
* the flowLabel to set
* Sets flow label.
*
* @param flowLabel the flow label to set
* @return this
*/
public IPv6 setFlowLabel(final int flowLabel) {
......@@ -111,15 +120,18 @@ public class IPv6 extends BasePacket {
}
/**
* @return the nextHeader
* Gets next header.
*
* @return the next header
*/
public byte getNextHeader() {
return this.nextHeader;
}
/**
* @param nextHeader
* the nextHeader to set
* Sets next header.
*
* @param nextHeader the next header to set
* @return this
*/
public IPv6 setNextHeader(final byte nextHeader) {
......@@ -128,15 +140,18 @@ public class IPv6 extends BasePacket {
}
/**
* @return the hopLimit
* Gets hop limit.
*
* @return the hop limit
*/
public byte getHopLimit() {
return this.hopLimit;
}
/**
* @param hopLimit
* the hopLimit to set
* Sets hop limit.
*
* @param hopLimit the hop limit to set
* @return this
*/
public IPv6 setHopLimit(final byte hopLimit) {
......@@ -145,15 +160,18 @@ public class IPv6 extends BasePacket {
}
/**
* @return the sourceAddress
* Gets source address.
*
* @return the IPv6 source address
*/
public byte[] getSourceAddress() {
return this.sourceAddress;
}
/**
* @param sourceAddress
* the sourceAddress to set
* Sets source address.
*
* @param sourceAddress the IPv6 source address to set
* @return this
*/
public IPv6 setSourceAddress(final byte[] sourceAddress) {
......@@ -162,15 +180,18 @@ public class IPv6 extends BasePacket {
}
/**
* @return the destinationAddress
* Gets destination address.
*
* @return the IPv6 destination address
*/
public byte[] getDestinationAddress() {
return this.destinationAddress;
}
/**
* @param destinationAddress
* the destinationAddress to set
* Sets destination address.
*
* @param destinationAddress the IPv6 destination address to set
* @return this
*/
public IPv6 setDestinationAddress(final byte[] destinationAddress) {
......@@ -191,7 +212,7 @@ public class IPv6 extends BasePacket {
final byte[] data = new byte[FIXED_HEADER_LENGTH + payloadLength];
final ByteBuffer bb = ByteBuffer.wrap(data);
bb.putInt((this.version & 0xf) << 28 | (this.diffServ & 0xff) << 20 | this.flowLabel & 0xfffff);
bb.putInt((this.version & 0xf) << 28 | (this.trafficClass & 0xff) << 20 | this.flowLabel & 0xfffff);
bb.putShort(this.payloadLength);
bb.put(this.nextHeader);
bb.put(this.hopLimit);
......@@ -213,7 +234,7 @@ public class IPv6 extends BasePacket {
iscratch = bb.getInt();
this.version = (byte) (iscratch >> 28 & 0xf);
this.diffServ = (byte) (iscratch >> 20 & 0xff);
this.trafficClass = (byte) (iscratch >> 20 & 0xff);
this.flowLabel = iscratch & 0xfffff;
this.payloadLength = bb.getShort();
this.nextHeader = bb.get();
......@@ -255,7 +276,7 @@ public class IPv6 extends BasePacket {
for (int i = 0; i < 4; i++) {
result = prime * result + bb.getInt();
}
result = prime * result + this.diffServ;
result = prime * result + this.trafficClass;
result = prime * result + this.flowLabel;
result = prime * result + this.hopLimit;
result = prime * result + this.nextHeader;
......@@ -288,7 +309,7 @@ public class IPv6 extends BasePacket {
if (!Arrays.equals(this.destinationAddress, other.destinationAddress)) {
return false;
}
if (this.diffServ != other.diffServ) {
if (this.trafficClass != other.trafficClass) {
return false;
}
if (this.flowLabel != other.flowLabel) {
......
......@@ -274,6 +274,20 @@ public class IpAddress implements Comparable<IpAddress> {
}
}
/**
* Check if this IP address is zero.
*
* @return true if this address is zero.
*/
public boolean isZero() {
for (byte b : octets) {
if (b != 0) {
return false;
}
}
return true;
}
@Override
public int compareTo(IpAddress o) {
// Compare first the version
......
......@@ -22,7 +22,7 @@ import java.nio.ByteBuffer;
import java.util.Arrays;
/**
* Implements ICMPv6 Neighbor Solicitation packet format.
* Implements ICMPv6 Neighbor Solicitation packet format. (RFC 4861)
*/
public class NeighborAdvertisement extends BasePacket {
public static final byte HEADER_LENGTH = 20; // bytes
......@@ -33,16 +33,18 @@ public class NeighborAdvertisement extends BasePacket {
protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH];
/**
* Gets router flag.
*
* @return true if router flag is set
* @return the router flag
*/
public byte getRouterFlag() {
return this.routerFlag;
}
/**
* @param routerFlag
* the routerFlag to set
* Sets router flag.
*
* @param routerFlag the router flag to set
* @return this
*/
public NeighborAdvertisement setRouterFlag(final byte routerFlag) {
......@@ -51,16 +53,18 @@ public class NeighborAdvertisement extends BasePacket {
}
/**
* Gets solicited flag.
*
* @return true if solicited flag is set
* @return the solicited flag
*/
public byte getSolicitedFlag() {
return this.solicitedFlag;
}
/**
* @param solicitedFlag
* the routerFlag to set
* Sets solicited flag.
*
* @param solicitedFlag the solicited flag to set
* @return this
*/
public NeighborAdvertisement setSolicitedFlag(final byte solicitedFlag) {
......@@ -69,16 +73,18 @@ public class NeighborAdvertisement extends BasePacket {
}
/**
* Gets override flag.
*
* @return true if override flag is set
* @return the override flag
*/
public byte getOverrideFlag() {
return this.overrideFlag;
}
/**
* @param overrideFlag
* the routerFlag to set
* Sets override flag.
*
* @param overrideFlag the override flag to set
* @return this
*/
public NeighborAdvertisement setOverrideFlag(final byte overrideFlag) {
......@@ -87,6 +93,7 @@ public class NeighborAdvertisement extends BasePacket {
}
/**
* Gets target address.
*
* @return the target IPv6 address
*/
......@@ -95,8 +102,9 @@ public class NeighborAdvertisement extends BasePacket {
}
/**
* @param targetAddress
* the sourceAddress to set
* Sets target address.
*
* @param targetAddress the target IPv6 address to set
* @return this
*/
public NeighborAdvertisement setTargetAddress(final byte[] targetAddress) {
......@@ -104,11 +112,6 @@ public class NeighborAdvertisement extends BasePacket {
return this;
}
/**
* Serializes the packet. Will compute and set the following fields if they
* are set to specific values at the time serialize is called: -routerFlag : 0
* -solicitedFlag : 0 -overrideFlag : 0
*/
@Override
public byte[] serialize() {
byte[] payloadData = null;
......@@ -151,10 +154,10 @@ public class NeighborAdvertisement extends BasePacket {
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 5807;
......
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onlab.packet;
import java.nio.ByteBuffer;
import java.util.Arrays;
/**
* Implements ICMPv6 Neighbor Solicitation packet format. (RFC 4861)
*/
public class NeighborSolicitation extends BasePacket {
public static final byte HEADER_LENGTH = 20; // bytes
protected byte[] targetAddress = new byte[Ip6Address.BYTE_LENGTH];
/**
* Gets target address.
*
* @return the target IPv6 address
*/
public byte[] getTargetAddress() {
return this.targetAddress;
}
/**
* Sets target address.
*
* @param targetAddress the target IPv6 address to set
* @return this
*/
public NeighborSolicitation setTargetAddress(final byte[] targetAddress) {
this.targetAddress = Arrays.copyOfRange(targetAddress, 0, Ip6Address.BYTE_LENGTH);
return this;
}
@Override
public byte[] serialize() {
byte[] payloadData = null;
if (this.payload != null) {
this.payload.setParent(this);
payloadData = this.payload.serialize();
}
int payloadLength = payloadData == null ? 0 : (short) payloadData.length;
final byte[] data = new byte[HEADER_LENGTH + payloadLength];
final ByteBuffer bb = ByteBuffer.wrap(data);
bb.putInt(0);
bb.put(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
if (payloadData != null) {
bb.put(payloadData);
}
return data;
}
@Override
public IPacket deserialize(byte[] data, int offset, int length) {
final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
bb.getInt();
bb.get(this.targetAddress, 0, Ip6Address.BYTE_LENGTH);
this.payload = new Data();
this.payload = this.payload.deserialize(data, bb.position(), bb.limit()
- bb.position());
this.payload.setParent(this);
return this;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 5807;
int result = super.hashCode();
ByteBuffer bb;
bb = ByteBuffer.wrap(this.targetAddress);
for (int i = 0; i < 4; i++) {
result = prime * result + bb.getInt();
}
return result;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (!(obj instanceof NeighborSolicitation)) {
return false;
}
final NeighborSolicitation other = (NeighborSolicitation) obj;
if (!Arrays.equals(this.targetAddress, other.targetAddress)) {
return false;
}
return true;
}
}
......@@ -73,7 +73,7 @@ public class IPv6Test {
IPv6 ipv6 = new IPv6();
ipv6.setPayload(udp);
ipv6.setVersion((byte) 6);
ipv6.setDiffServ((byte) 0x93);
ipv6.setTrafficClass((byte) 0x93);
ipv6.setFlowLabel(0x13579);
ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
ipv6.setHopLimit((byte) 32);
......@@ -92,7 +92,7 @@ public class IPv6Test {
ipv6.deserialize(bytePacket, 0, bytePacket.length);
assertThat(ipv6.getVersion(), is((byte) 6));
assertThat(ipv6.getDiffServ(), is((byte) 0x93));
assertThat(ipv6.getTrafficClass(), is((byte) 0x93));
assertThat(ipv6.getFlowLabel(), is(0x13579));
assertThat(ipv6.getNextHeader(), is(IPv6.PROTOCOL_UDP));
assertThat(ipv6.getHopLimit(), is((byte) 32));
......@@ -108,7 +108,7 @@ public class IPv6Test {
IPv6 packet1 = new IPv6();
packet1.setPayload(udp);
packet1.setVersion((byte) 6);
packet1.setDiffServ((byte) 0x93);
packet1.setTrafficClass((byte) 0x93);
packet1.setFlowLabel(0x13579);
packet1.setNextHeader(IPv6.PROTOCOL_UDP);
packet1.setHopLimit((byte) 32);
......@@ -118,7 +118,7 @@ public class IPv6Test {
IPv6 packet2 = new IPv6();
packet2.setPayload(udp);
packet2.setVersion((byte) 6);
packet2.setDiffServ((byte) 0x93);
packet2.setTrafficClass((byte) 0x93);
packet2.setFlowLabel(0x13579);
packet2.setNextHeader(IPv6.PROTOCOL_UDP);
packet2.setHopLimit((byte) 32);
......
......@@ -26,6 +26,7 @@ import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
/**
......@@ -703,6 +704,28 @@ public class IpAddressTest {
}
/**
* Tests if address is zero for IPv4.
*/
@Test
public void testIsZeroIPv4() {
IpAddress normalIP = IpAddress.valueOf("10.0.0.1");
IpAddress zeroIP = IpAddress.valueOf("0.0.0.0");
assertFalse(normalIP.isZero());
assertTrue(zeroIP.isZero());
}
/**
* Tests if address is zero for IPv6.
*/
@Test
public void testIsZeroIPv6() {
IpAddress normalIP = IpAddress.valueOf("fe80::1");
IpAddress zeroIP = IpAddress.valueOf("::");
assertFalse(normalIP.isZero());
assertTrue(zeroIP.isZero());
}
/**
* Tests comparison of {@link IpAddress} for IPv4.
*/
@Test
......
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onlab.packet;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Tests for class {@link org.onlab.packet.NeighborSolicitation}.
*/
public class NeighborSolicitationTest {
private static final byte[] TARGET_ADDRESS = {
(byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
(byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
};
private static final byte[] TARGET_ADDRESS2 = {
(byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
(byte) 0xe6, (byte) 0xce, (byte) 0x8f, (byte) 0xff, (byte) 0xfe, (byte) 0x54, (byte) 0x37, (byte) 0xc8
};
private static Data data;
private static byte[] bytePacket;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
data = new Data();
data.setData("".getBytes());
byte[] bytePayload = data.serialize();
byte[] byteHeader = {
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x20, (byte) 0x01, (byte) 0x0f, (byte) 0x18, (byte) 0x01, (byte) 0x13, (byte) 0x02, (byte) 0x15,
(byte) 0xca, (byte) 0x2a, (byte) 0x14, (byte) 0xff, (byte) 0xfe, (byte) 0x35, (byte) 0x26, (byte) 0xce
};
bytePacket = new byte[byteHeader.length + bytePayload.length];
System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
}
/**
* Tests serialize and setters.
*/
@Test
public void testSerialize() {
NeighborSolicitation ns = new NeighborSolicitation();
ns.setTargetAddress(TARGET_ADDRESS);
assertArrayEquals(ns.serialize(), bytePacket);
}
/**
* Tests deserialize and getters.
*/
@Test
public void testDeserialize() {
NeighborSolicitation ns = new NeighborSolicitation();
ns.deserialize(bytePacket, 0, bytePacket.length);
assertArrayEquals(ns.getTargetAddress(), TARGET_ADDRESS);
}
/**
* Tests comparator.
*/
@Test
public void testEqual() {
NeighborSolicitation ns1 = new NeighborSolicitation();
ns1.setTargetAddress(TARGET_ADDRESS);
NeighborSolicitation ns2 = new NeighborSolicitation();
ns2.setTargetAddress(TARGET_ADDRESS2);
assertTrue(ns1.equals(ns1));
assertFalse(ns1.equals(ns2));
}
}