use of org.onlab.packet.DHCP6 in project onos by opennetworkinglab.
the class Dhcp6RelayTest method serializeSolicit.
/**
* Test serialize relay message with solicit message.
*
* @throws Exception exception while serialize the DHCPv6 payload
*/
@Test
public void serializeSolicit() throws Exception {
DHCP6 relayMsg = new DHCP6();
relayMsg.setMsgType(DHCP6.MsgType.RELAY_FORW.value());
relayMsg.setHopCount((byte) HOP_COUNT);
relayMsg.setLinkAddress(LINK_ADDRESS.toOctets());
relayMsg.setPeerAddress(PEER_ADDRESS.toOctets());
DHCP6 relaiedDhcp6 = new DHCP6();
relaiedDhcp6.setMsgType(DHCP6.MsgType.SOLICIT.value());
relaiedDhcp6.setTransactionId(XID_1);
List<Dhcp6Option> options = Lists.newArrayList();
// Client ID
Dhcp6Duid duid = new Dhcp6Duid();
duid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
duid.setHardwareType((short) 1);
duid.setDuidTime(CLIENT_DUID_TIME);
duid.setLinkLayerAddress(CLIENT_MAC.toBytes());
Dhcp6ClientIdOption clientIdOption = new Dhcp6ClientIdOption();
clientIdOption.setDuid(duid);
options.add(clientIdOption);
// Option request
Dhcp6Option option = new Dhcp6Option();
option.setCode(DHCP6.OptionCode.ORO.value());
option.setLength((short) 8);
option.setData(new byte[] { 0, 23, 0, 24, 0, 39, 0, 31 });
options.add(option);
// Elapsed Time
option = new Dhcp6Option();
option.setCode(DHCP6.OptionCode.ELAPSED_TIME.value());
option.setLength((short) 2);
option.setData(new byte[] { 0, 0 });
options.add(option);
// IA NA
Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption();
iaNaOption.setIaId(IA_ID);
iaNaOption.setT1(T1_CLIENT);
iaNaOption.setT2(T2_CLIENT);
Dhcp6IaAddressOption iaAddressOption = new Dhcp6IaAddressOption();
iaAddressOption.setIp6Address(IA_ADDRESS);
iaAddressOption.setPreferredLifetime(PREFFERRED_LT_REQ);
iaAddressOption.setValidLifetime(VALID_LT_REQ);
iaNaOption.setOptions(ImmutableList.of(iaAddressOption));
options.add(iaNaOption);
relaiedDhcp6.setOptions(options);
Dhcp6RelayOption relayOption = new Dhcp6RelayOption();
relayOption.setPayload(relaiedDhcp6);
Dhcp6Option subscriberId = new Dhcp6Option();
subscriberId.setCode(DHCP6.OptionCode.SUBSCRIBER_ID.value());
subscriberId.setLength((short) 10);
subscriberId.setData(SERVER_IP.toString().getBytes(US_ASCII));
relayMsg.setOptions(ImmutableList.of(subscriberId, relayOption));
UDP udp = new UDP();
udp.setSourcePort(UDP.DHCP_V6_SERVER_PORT);
udp.setDestinationPort(UDP.DHCP_V6_SERVER_PORT);
udp.setPayload(relayMsg);
udp.setChecksum((short) 0x9a99);
IPv6 ipv6 = new IPv6();
ipv6.setHopLimit((byte) 32);
ipv6.setSourceAddress(DOWNSTREAM_LL.toOctets());
ipv6.setDestinationAddress(DHCP6_BRC.toOctets());
ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
ipv6.setTrafficClass((byte) 0);
ipv6.setFlowLabel(0x000cbf64);
ipv6.setPayload(udp);
Ethernet eth = new Ethernet();
eth.setDestinationMACAddress(IPV6_MCAST);
eth.setSourceMACAddress(DOWNSTREAM_MAC);
eth.setEtherType(Ethernet.TYPE_IPV6);
eth.setPayload(ipv6);
assertArrayEquals(Resources.toByteArray(Dhcp6RelayTest.class.getResource(SOLICIT)), eth.serialize());
}
use of org.onlab.packet.DHCP6 in project onos by opennetworkinglab.
the class Dhcp6RelayTest method serializeAdvertise.
/**
* Test serialize relay message with advertise message.
*
* @throws Exception exception while serialize the DHCPv6 payload
*/
@Test
public void serializeAdvertise() throws Exception {
DHCP6 relayMsg = new DHCP6();
relayMsg.setMsgType(DHCP6.MsgType.RELAY_REPL.value());
relayMsg.setHopCount((byte) HOP_COUNT);
relayMsg.setLinkAddress(LINK_ADDRESS.toOctets());
relayMsg.setPeerAddress(PEER_ADDRESS.toOctets());
DHCP6 relaiedDhcp6 = new DHCP6();
relaiedDhcp6.setMsgType(DHCP6.MsgType.ADVERTISE.value());
relaiedDhcp6.setTransactionId(XID_1);
List<Dhcp6Option> options = Lists.newArrayList();
// IA address
Dhcp6IaAddressOption iaAddressOption = new Dhcp6IaAddressOption();
iaAddressOption.setIp6Address(IA_ADDRESS);
iaAddressOption.setPreferredLifetime(PREFFERRED_LT_SERVER);
iaAddressOption.setValidLifetime(VALID_LT_SERVER);
// IA NA
Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption();
iaNaOption.setIaId(IA_ID);
iaNaOption.setT1(T1_SERVER);
iaNaOption.setT2(T2_SERVER);
iaNaOption.setOptions(ImmutableList.of(iaAddressOption));
options.add(iaNaOption);
// Client ID
Dhcp6Duid duid = new Dhcp6Duid();
duid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
duid.setHardwareType((short) 1);
duid.setDuidTime(CLIENT_DUID_TIME);
duid.setLinkLayerAddress(CLIENT_MAC.toBytes());
Dhcp6ClientIdOption clientIdOption = new Dhcp6ClientIdOption();
clientIdOption.setDuid(duid);
options.add(clientIdOption);
// Server ID
Dhcp6Option option = new Dhcp6Option();
option.setCode(DHCP6.OptionCode.SERVERID.value());
option.setLength((short) 14);
Dhcp6Duid serverDuid = new Dhcp6Duid();
serverDuid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
serverDuid.setLinkLayerAddress(SERVER_MAC.toBytes());
serverDuid.setHardwareType((short) 1);
serverDuid.setDuidTime(0x211e5340);
option.setData(serverDuid.serialize());
options.add(option);
relaiedDhcp6.setOptions(options);
Dhcp6RelayOption relayOption = new Dhcp6RelayOption();
relayOption.setPayload(relaiedDhcp6);
relayMsg.setOptions(ImmutableList.of(relayOption));
UDP udp = new UDP();
udp.setSourcePort(UDP.DHCP_V6_SERVER_PORT);
udp.setDestinationPort(UDP.DHCP_V6_SERVER_PORT);
udp.setPayload(relayMsg);
udp.setChecksum((short) 0x0000019d);
IPv6 ipv6 = new IPv6();
ipv6.setHopLimit((byte) 64);
ipv6.setSourceAddress(SERVER_LL.toOctets());
ipv6.setDestinationAddress(DOWNSTREAM_LL.toOctets());
ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
ipv6.setTrafficClass((byte) 0);
ipv6.setFlowLabel(0x000c72ef);
ipv6.setPayload(udp);
Ethernet eth = new Ethernet();
eth.setDestinationMACAddress(DOWNSTREAM_MAC);
eth.setSourceMACAddress(SERVER_MAC);
eth.setEtherType(Ethernet.TYPE_IPV6);
eth.setPayload(ipv6);
assertArrayEquals(Resources.toByteArray(Dhcp6RelayTest.class.getResource(ADVERTISE)), eth.serialize());
}
use of org.onlab.packet.DHCP6 in project onos by opennetworkinglab.
the class Dhcp6RelayTest method serializeReply.
@Test
public void serializeReply() throws Exception {
DHCP6 relayMsg = new DHCP6();
relayMsg.setMsgType(DHCP6.MsgType.RELAY_REPL.value());
relayMsg.setHopCount((byte) HOP_COUNT);
relayMsg.setLinkAddress(LINK_ADDRESS.toOctets());
relayMsg.setPeerAddress(PEER_ADDRESS.toOctets());
DHCP6 relaiedDhcp6 = new DHCP6();
relaiedDhcp6.setMsgType(DHCP6.MsgType.REPLY.value());
relaiedDhcp6.setTransactionId(XID_2);
List<Dhcp6Option> options = Lists.newArrayList();
// IA address
Dhcp6IaAddressOption iaAddressOption = new Dhcp6IaAddressOption();
iaAddressOption.setIp6Address(IA_ADDRESS);
iaAddressOption.setPreferredLifetime(PREFFERRED_LT_SERVER);
iaAddressOption.setValidLifetime(VALID_LT_SERVER);
// IA NA
Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption();
iaNaOption.setIaId(IA_ID);
iaNaOption.setT1(T1_SERVER);
iaNaOption.setT2(T2_SERVER);
iaNaOption.setOptions(ImmutableList.of(iaAddressOption));
options.add(iaNaOption);
// Client ID
Dhcp6Duid duid = new Dhcp6Duid();
duid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
duid.setHardwareType((short) 1);
duid.setDuidTime(CLIENT_DUID_TIME);
duid.setLinkLayerAddress(CLIENT_MAC.toBytes());
Dhcp6ClientIdOption clientIdOption = new Dhcp6ClientIdOption();
clientIdOption.setDuid(duid);
options.add(clientIdOption);
// Server ID
Dhcp6Option option = new Dhcp6Option();
option.setCode(DHCP6.OptionCode.SERVERID.value());
option.setLength((short) 14);
Dhcp6Duid serverDuid = new Dhcp6Duid();
serverDuid.setDuidType(Dhcp6Duid.DuidType.DUID_LLT);
serverDuid.setLinkLayerAddress(SERVER_MAC.toBytes());
serverDuid.setHardwareType((short) 1);
serverDuid.setDuidTime(0x211e5340);
option.setData(serverDuid.serialize());
options.add(option);
relaiedDhcp6.setOptions(options);
Dhcp6RelayOption relayOption = new Dhcp6RelayOption();
relayOption.setPayload(relaiedDhcp6);
relayMsg.setOptions(ImmutableList.of(relayOption));
UDP udp = new UDP();
udp.setSourcePort(UDP.DHCP_V6_SERVER_PORT);
udp.setDestinationPort(UDP.DHCP_V6_SERVER_PORT);
udp.setPayload(relayMsg);
udp.setChecksum((short) 0x019d);
IPv6 ipv6 = new IPv6();
ipv6.setHopLimit((byte) 64);
ipv6.setSourceAddress(SERVER_LL.toOctets());
ipv6.setDestinationAddress(DOWNSTREAM_LL.toOctets());
ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
ipv6.setTrafficClass((byte) 0);
ipv6.setFlowLabel(0x000c72ef);
ipv6.setPayload(udp);
Ethernet eth = new Ethernet();
eth.setDestinationMACAddress(DOWNSTREAM_MAC);
eth.setSourceMACAddress(SERVER_MAC);
eth.setEtherType(Ethernet.TYPE_IPV6);
eth.setPayload(ipv6);
assertArrayEquals(Resources.toByteArray(Dhcp6RelayTest.class.getResource(REPLY)), eth.serialize());
}
use of org.onlab.packet.DHCP6 in project onos by opennetworkinglab.
the class Dhcp6RelayTest method deserializeRequest.
/**
* Test deserialize relay message with request message.
*
* @throws Exception exception while deserialize the DHCPv6 payload
*/
@Test
public void deserializeRequest() throws Exception {
byte[] data = Resources.toByteArray(getClass().getResource(REQUEST));
Ethernet eth = Ethernet.deserializer().deserialize(data, 0, data.length);
DHCP6 relayMsg = (DHCP6) eth.getPayload().getPayload().getPayload();
assertEquals(relayMsg.getMsgType(), DHCP6.MsgType.RELAY_FORW.value());
assertEquals(relayMsg.getHopCount(), HOP_COUNT);
assertEquals(relayMsg.getIp6LinkAddress(), LINK_ADDRESS);
assertEquals(relayMsg.getIp6PeerAddress(), PEER_ADDRESS);
assertEquals(relayMsg.getOptions().size(), 2);
Dhcp6Option option = relayMsg.getOptions().get(0);
assertEquals(option.getCode(), DHCP6.OptionCode.SUBSCRIBER_ID.value());
assertEquals(option.getLength(), 10);
assertArrayEquals(option.getData(), SERVER_IP.toString().getBytes(US_ASCII));
option = relayMsg.getOptions().get(1);
assertEquals(option.getCode(), DHCP6.OptionCode.RELAY_MSG.value());
assertEquals(option.getLength(), 102);
assertTrue(option.getPayload() instanceof DHCP6);
DHCP6 relaiedDhcp6 = (DHCP6) option.getPayload();
assertEquals(relaiedDhcp6.getMsgType(), DHCP6.MsgType.REQUEST.value());
assertEquals(relaiedDhcp6.getTransactionId(), XID_2);
assertEquals(relaiedDhcp6.getOptions().size(), 5);
// Client ID
option = relaiedDhcp6.getOptions().get(0);
assertTrue(option instanceof Dhcp6ClientIdOption);
Dhcp6ClientIdOption clientIdOption = (Dhcp6ClientIdOption) option;
assertEquals(clientIdOption.getCode(), DHCP6.OptionCode.CLIENTID.value());
assertEquals(clientIdOption.getLength(), 14);
assertEquals(clientIdOption.getDuid().getDuidType(), Dhcp6Duid.DuidType.DUID_LLT);
assertEquals(clientIdOption.getDuid().getHardwareType(), 1);
assertEquals(clientIdOption.getDuid().getDuidTime(), CLIENT_DUID_TIME);
assertArrayEquals(clientIdOption.getDuid().getLinkLayerAddress(), CLIENT_MAC.toBytes());
// Server ID
option = relaiedDhcp6.getOptions().get(1);
assertEquals(option.getCode(), DHCP6.OptionCode.SERVERID.value());
assertEquals(option.getLength(), 14);
Dhcp6Duid serverDuid = Dhcp6Duid.deserializer().deserialize(option.getData(), 0, option.getData().length);
assertEquals(serverDuid.getDuidType(), Dhcp6Duid.DuidType.DUID_LLT);
assertEquals(serverDuid.getDuidTime(), 0x211e5340);
assertEquals(serverDuid.getHardwareType(), 1);
assertArrayEquals(serverDuid.getLinkLayerAddress(), SERVER_MAC.toBytes());
// Option Request
option = relaiedDhcp6.getOptions().get(2);
assertEquals(option.getCode(), DHCP6.OptionCode.ORO.value());
assertEquals(option.getLength(), 8);
assertArrayEquals(option.getData(), new byte[] { 0, 23, 0, 24, 0, 39, 0, 31 });
// ELAPSED_TIME
option = relaiedDhcp6.getOptions().get(3);
assertEquals(option.getCode(), DHCP6.OptionCode.ELAPSED_TIME.value());
assertEquals(option.getLength(), 2);
assertArrayEquals(option.getData(), new byte[] { 0, 0 });
// IA NA
option = relaiedDhcp6.getOptions().get(4);
assertTrue(option instanceof Dhcp6IaNaOption);
Dhcp6IaNaOption iaNaOption = (Dhcp6IaNaOption) option;
assertEquals(iaNaOption.getCode(), DHCP6.OptionCode.IA_NA.value());
assertEquals(iaNaOption.getLength(), 40);
assertEquals(iaNaOption.getIaId(), IA_ID);
assertEquals(iaNaOption.getT1(), T1_CLIENT);
assertEquals(iaNaOption.getT2(), T2_CLIENT);
assertEquals(iaNaOption.getOptions().size(), 1);
// IA Address (in IA NA)
assertTrue(iaNaOption.getOptions().get(0) instanceof Dhcp6IaAddressOption);
Dhcp6IaAddressOption iaAddressOption = (Dhcp6IaAddressOption) iaNaOption.getOptions().get(0);
assertEquals(iaAddressOption.getIp6Address(), IA_ADDRESS);
assertEquals(iaAddressOption.getPreferredLifetime(), PREFFERRED_LT_REQ);
assertEquals(iaAddressOption.getValidLifetime(), VALID_LT_REQ_2);
assertNull(iaAddressOption.getOptions());
assertArrayEquals(data, eth.serialize());
}
use of org.onlab.packet.DHCP6 in project onos by opennetworkinglab.
the class Dhcp6HandlerImpl method removeHostOrRoute.
/**
* remove host or route and update dhcp relay record attributes.
*
* @param directConnFlag flag to show that packet is from directly connected client
* @param location client side connect point
* @param dhcp6Packet the dhcp6 payload
* @param clientPacket client's ethernet packet
* @param clientIpv6 client's Ipv6 packet
* @param clientInterface client interfaces
*/
private void removeHostOrRoute(boolean directConnFlag, ConnectPoint location, DHCP6 dhcp6Packet, Ethernet clientPacket, IPv6 clientIpv6, Interface clientInterface) {
log.debug("removeHostOrRoute enters {}", dhcp6Packet);
VlanId vlanId = clientInterface.vlan();
// could be gw or host
MacAddress srcMac = clientPacket.getSourceMAC();
MacAddress leafClientMac;
byte leafMsgType;
log.debug("client mac {} client vlan {}", HexString.toHexString(srcMac.toBytes(), ":"), vlanId);
Dhcp6ClientIdOption clientIdOption = Dhcp6HandlerUtil.extractClientId(directConnFlag, dhcp6Packet);
if (clientIdOption != null) {
if ((clientIdOption.getDuid().getDuidType() == Dhcp6Duid.DuidType.DUID_LLT) || (clientIdOption.getDuid().getDuidType() == Dhcp6Duid.DuidType.DUID_LL)) {
leafClientMac = MacAddress.valueOf(clientIdOption.getDuid().getLinkLayerAddress());
} else {
log.warn("Link-Layer Address not supported in CLIENTID option. No DhcpRelay Record created.");
// dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_LINKLOCAL_FAIL);
return;
}
} else {
log.warn("CLIENTID option NOT found. Don't create DhcpRelay Record.");
// dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_CLIENTID_FAIL);
return;
}
HostId leafHostId = HostId.hostId(leafClientMac, vlanId);
DhcpRecord record = dhcpRelayStore.getDhcpRecord(leafHostId).orElse(null);
if (record == null) {
record = new DhcpRecord(leafHostId);
} else {
record = record.clone();
}
Boolean isMsgRelease = Dhcp6HandlerUtil.isDhcp6Release(dhcp6Packet);
IpAddressInfo ipInfo;
PdPrefixInfo pdInfo = null;
if (directConnFlag) {
// Add to host store if it is connected to network directly
ipInfo = extractIpAddress(dhcp6Packet);
if (ipInfo != null) {
if (isMsgRelease) {
HostId hostId = HostId.hostId(srcMac, vlanId);
log.debug("remove Host {} ip for directly connected.", hostId.toString());
providerService.removeIpFromHost(hostId, ipInfo.ip6Address);
}
} else {
log.debug("ipAddress not found. Do not remove Host {} for directly connected.", HostId.hostId(srcMac, vlanId).toString());
}
leafMsgType = dhcp6Packet.getMsgType();
} else {
// Remove from route store if it is not connected to network directly
// pick out the first link-local ip address
IpAddress nextHopIp = getFirstIpByHost(directConnFlag, srcMac, vlanId);
if (nextHopIp == null) {
log.warn("Can't find link-local IP address of gateway mac {} vlanId {}", srcMac, vlanId);
// dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_LINKLOCAL_GW);
return;
}
DHCP6 leafDhcp = Dhcp6HandlerUtil.getDhcp6Leaf(dhcp6Packet);
ipInfo = extractIpAddress(leafDhcp);
if (ipInfo == null) {
log.debug("ip is null");
} else {
if (isMsgRelease) {
Route routeForIP = new Route(Route.Source.DHCP, ipInfo.ip6Address.toIpPrefix(), nextHopIp);
log.debug("removing route of 128 address for indirectly connected.");
log.debug("128 ip {}, nexthop {}", HexString.toHexString(ipInfo.ip6Address.toOctets(), ":"), HexString.toHexString(nextHopIp.toOctets(), ":"));
routeStore.removeRoute(routeForIP);
}
}
pdInfo = extractPrefix(leafDhcp);
if (pdInfo == null) {
log.debug("ipPrefix is null ");
} else {
if (isMsgRelease) {
Route routeForPrefix = new Route(Route.Source.DHCP, pdInfo.pdPrefix, nextHopIp);
log.debug("removing route of PD for indirectly connected.");
log.debug("pd ip {}, nexthop {}", HexString.toHexString(pdInfo.pdPrefix.address().toOctets(), ":"), HexString.toHexString(nextHopIp.toOctets(), ":"));
routeStore.removeRoute(routeForPrefix);
if (this.dhcpFpmEnabled) {
dhcpFpmPrefixStore.removeFpmRecord(pdInfo.pdPrefix);
}
}
}
leafMsgType = leafDhcp.getMsgType();
}
if (isMsgRelease) {
log.debug("DHCP6 RELEASE msg.");
if (record != null) {
if (ipInfo != null) {
log.debug("DhcpRelay Record ip6Address is set to null.");
record.ip6Address(null);
}
if (pdInfo != null) {
log.debug("DhcpRelay Record pdPrefix is set to null.");
}
if (!record.ip6Address().isPresent() && !record.pdPrefix().isPresent()) {
log.warn("IP6 address and IP6 PD both are null. Remove record.");
// do not remove a record. Let timer task handler it.
// dhcpRelayStore.removeDhcpRecord(HostId.hostId(leafClientMac, vlanId));
}
}
}
if (record != null) {
record.getV6Counters().incrementCounter(Dhcp6HandlerUtil.getMsgTypeStr(leafMsgType));
record.addLocation(new HostLocation(location, System.currentTimeMillis()));
record.ip6Status(DHCP6.MsgType.getType(leafMsgType));
record.setDirectlyConnected(directConnFlag);
if (!directConnFlag) {
// Update gateway mac address if the host is not directly connected
record.nextHop(srcMac);
}
record.updateLastSeen();
}
dhcpRelayStore.updateDhcpRecord(leafHostId, record);
/*
// TODO Use AtomicInteger for the counters
try {
recordSemaphore.acquire();
try {
dhcpRelayCountersStore.incrementCounter(gCount, Dhcp6HandlerUtil.getMsgTypeStr(leafMsgType));
} finally {
// calling release() after a successful acquire()
recordSemaphore.release();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
*/
}
Aggregations