use of org.onlab.packet.DHCP6 in project onos by opennetworkinglab.
the class Dhcp6Test method deserializeReply.
/**
* Test deserialize relay message with reply message.
*
* @throws Exception exception while deserialize the DHCPv6 payload
*/
@Test
public void deserializeReply() throws Exception {
byte[] data = Resources.toByteArray(getClass().getResource(REPLY));
Ethernet eth = Ethernet.deserializer().deserialize(data, 0, data.length);
DHCP6 dhcp6 = (DHCP6) eth.getPayload().getPayload().getPayload();
assertEquals(dhcp6.getMsgType(), DHCP6.MsgType.REPLY.value());
assertEquals(dhcp6.getTransactionId(), XID_2);
assertEquals(dhcp6.getOptions().size(), 3);
// IA NA
Dhcp6Option option = dhcp6.getOptions().get(0);
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_SERVER);
assertEquals(iaNaOption.getT2(), T2_SERVER);
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_SERVER);
assertEquals(iaAddressOption.getValidLifetime(), VALID_LT_SERVER);
assertNull(iaAddressOption.getOptions());
// Client ID
option = dhcp6.getOptions().get(1);
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 = dhcp6.getOptions().get(2);
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());
assertArrayEquals(data, eth.serialize());
}
use of org.onlab.packet.DHCP6 in project onos by opennetworkinglab.
the class Dhcp6Test method serializeSolicit.
/**
* Test serialize solicit message.
*
* @throws Exception exception while serialize the DHCPv6 payload
*/
@Test
public void serializeSolicit() throws Exception {
DHCP6 dhcp6 = new DHCP6();
dhcp6.setMsgType(DHCP6.MsgType.SOLICIT.value());
dhcp6.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);
dhcp6.setOptions(options);
Dhcp6RelayOption relayOption = new Dhcp6RelayOption();
relayOption.setPayload(dhcp6);
UDP udp = new UDP();
udp.setSourcePort(UDP.DHCP_V6_CLIENT_PORT);
udp.setDestinationPort(UDP.DHCP_V6_SERVER_PORT);
udp.setPayload(dhcp6);
udp.setChecksum((short) 0xffaf);
IPv6 ipv6 = new IPv6();
ipv6.setHopLimit((byte) 1);
ipv6.setSourceAddress(CLIENT_LL.toOctets());
ipv6.setDestinationAddress(DHCP6_BRC.toOctets());
ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
ipv6.setTrafficClass((byte) 0);
ipv6.setFlowLabel(0x000322ad);
ipv6.setPayload(udp);
Ethernet eth = new Ethernet();
eth.setDestinationMACAddress(IPV6_MCAST);
eth.setSourceMACAddress(CLIENT_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 Dhcp6Test method serializeReply.
@Test
public void serializeReply() throws Exception {
DHCP6 dhcp6 = new DHCP6();
dhcp6.setMsgType(DHCP6.MsgType.REPLY.value());
dhcp6.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);
dhcp6.setOptions(options);
Dhcp6RelayOption relayOption = new Dhcp6RelayOption();
relayOption.setPayload(dhcp6);
UDP udp = new UDP();
udp.setSourcePort(UDP.DHCP_V6_SERVER_PORT);
udp.setDestinationPort(UDP.DHCP_V6_CLIENT_PORT);
udp.setPayload(dhcp6);
udp.setChecksum((short) 0xcb5a);
IPv6 ipv6 = new IPv6();
ipv6.setHopLimit((byte) 64);
ipv6.setSourceAddress(UPSTREAM_LL.toOctets());
ipv6.setDestinationAddress(CLIENT_LL.toOctets());
ipv6.setNextHeader(IPv6.PROTOCOL_UDP);
ipv6.setTrafficClass((byte) 0);
ipv6.setFlowLabel(0x000d935f);
ipv6.setPayload(udp);
Ethernet eth = new Ethernet();
eth.setDestinationMACAddress(CLIENT_MAC);
eth.setSourceMACAddress(UPSTREAM_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 Dhcp6HandlerImpl method processDhcpPacket.
@Override
public void processDhcpPacket(PacketContext context, BasePacket payload) {
checkNotNull(payload, "DHCP6 payload can't be null");
checkState(payload instanceof DHCP6, "Payload is not a DHCP6");
DHCP6 dhcp6Payload = (DHCP6) payload;
Ethernet receivedPacket = context.inPacket().parsed();
if (!configured()) {
log.warn("Missing DHCP6 relay server config. " + "Abort packet processing dhcp6 payload {}", dhcp6Payload);
return;
}
byte msgTypeVal = dhcp6Payload.getMsgType();
MsgType msgType = DHCP6.MsgType.getType(msgTypeVal);
log.debug("msgType is {}", msgType);
ConnectPoint inPort = context.inPacket().receivedFrom();
if (inPort == null) {
log.warn("incoming ConnectPoint is null");
}
Set<Interface> receivingInterfaces = interfaceService.getInterfacesByPort(inPort);
// ignore the packets if dhcp client interface is not configured on onos.
if (receivingInterfaces.isEmpty()) {
log.warn("Virtual interface is not configured on {}", inPort);
return;
}
if (msgTypeVal == DHCP6.MsgType.LEASEQUERY.value()) {
List<InternalPacket> ethernetClientPackets = learnRouteFromLeasequery ? processLQ6PacketFromClient(context, receivedPacket, receivingInterfaces, dhcp6Payload) : processDhcp6ForwardOnly(context, receivedPacket, receivingInterfaces, dhcp6Payload);
for (InternalPacket internalPacket : ethernetClientPackets) {
forwardPacket(internalPacket);
}
} else if (msgTypeVal == DHCP6.MsgType.LEASEQUERY_REPLY.value() && learnRouteFromLeasequery) {
IPv6 clientIpv6 = (IPv6) receivedPacket.getPayload();
UDP clientUdp = (UDP) clientIpv6.getPayload();
DHCP6 clientDhcp6 = (DHCP6) clientUdp.getPayload();
Interface serverInterface = Dhcp6HandlerUtil.directlyConnected(clientDhcp6) ? getServerInterface() : getIndirectServerInterface();
InternalPacket ethernetPacketReply = Dhcp6HandlerUtil.processLQ6PacketFromServer(defaultServerInfoList, indirectServerInfoList, serverInterface, interfaceService, hostService, context, receivedPacket, receivingInterfaces);
if (ethernetPacketReply != null) {
forwardPacket(ethernetPacketReply);
}
handleLeaseQuery6ReplyMsg(context, dhcp6Payload);
} else if (MSG_TYPE_FROM_CLIENT.contains(msgTypeVal)) {
List<InternalPacket> ethernetClientPacket = processDhcp6PacketFromClient(context, receivedPacket, receivingInterfaces);
for (InternalPacket internalPacket : ethernetClientPacket) {
forwardPacket(internalPacket);
}
} else if (MSG_TYPE_FROM_SERVER.contains(msgTypeVal)) {
log.debug("calling processDhcp6PacketFromServer with RELAY_REPL {}, {}", receivedPacket, dhcp6Payload);
InternalPacket ethernetPacketReply = processDhcp6PacketFromServer(context, receivedPacket, receivingInterfaces);
if (ethernetPacketReply != null) {
forwardPacket(ethernetPacketReply);
}
} else {
log.warn("Not so fast, packet type {} not supported yet", msgTypeVal);
}
}
use of org.onlab.packet.DHCP6 in project onos by opennetworkinglab.
the class Dhcp6HandlerImpl method processDhcp6PacketFromServer.
/**
* process the DHCP6 relay-reply packet from dhcp server.
*
* @param context packet context
* @param receivedPacket server ethernet packet
* @param recevingInterfaces set of server side interfaces
* @return internalPacket toward client
*/
private InternalPacket processDhcp6PacketFromServer(PacketContext context, Ethernet receivedPacket, Set<Interface> recevingInterfaces) {
// get dhcp6 header.
Ethernet etherReply = receivedPacket.duplicate();
IPv6 ipv6Packet = (IPv6) etherReply.getPayload();
UDP udpPacket = (UDP) ipv6Packet.getPayload();
DHCP6 dhcp6Relay = (DHCP6) udpPacket.getPayload();
Boolean directConnFlag = Dhcp6HandlerUtil.directlyConnected(dhcp6Relay);
DHCP6 embeddedDhcp6 = dhcp6Relay.getOptions().stream().filter(opt -> opt instanceof Dhcp6RelayOption).map(BasePacket::getPayload).map(pld -> (DHCP6) pld).findFirst().orElse(null);
ConnectPoint inPort = context.inPacket().receivedFrom();
DhcpServerInfo foundServerInfo = findServerInfoFromServer(directConnFlag, inPort);
if (foundServerInfo == null) {
log.warn("Cannot find server info for {} server, inPort {}", directConnFlag ? "direct" : "indirect", inPort);
// dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_SERVER_INFO);
return null;
} else {
if (Dhcp6HandlerUtil.isServerIpEmpty(foundServerInfo)) {
log.warn("Cannot find server info's ipaddress");
// dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_SERVER_IP6ADDR);
return null;
}
}
Dhcp6InterfaceIdOption interfaceIdOption = dhcp6Relay.getOptions().stream().filter(opt -> opt instanceof Dhcp6InterfaceIdOption).map(opt -> (Dhcp6InterfaceIdOption) opt).findFirst().orElse(null);
if (interfaceIdOption == null) {
log.warn("Interface Id option is not present, abort packet...");
// dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.OPTION_MISSING_FAIL);
return null;
}
MacAddress peerMac = interfaceIdOption.getMacAddress();
String clientConnectionPointStr = new String(interfaceIdOption.getInPort());
ConnectPoint clientConnectionPoint = ConnectPoint.deviceConnectPoint(clientConnectionPointStr);
VlanId vlanIdInUse = VlanId.vlanId(interfaceIdOption.getVlanId());
log.debug("processDhcp6PacketFromServer Interface Id Mac {}, port{}, vlan {}", peerMac, clientConnectionPointStr, vlanIdInUse);
Interface clientInterface = interfaceService.getInterfacesByPort(clientConnectionPoint).stream().filter(iface -> Dhcp6HandlerUtil.interfaceContainsVlan(iface, vlanIdInUse)).findFirst().orElse(null);
if (clientInterface == null) {
log.warn("Cannot get client interface for from packet, abort... vlan {}", vlanIdInUse.toString());
// dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_MATCHING_INTF);
return null;
}
etherReply.setVlanID(vlanIdInUse.toShort());
MacAddress relayAgentMac = clientInterface.mac();
if (relayAgentMac == null) {
log.warn("Can not get client interface mac, abort packet..");
// dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_CLIENT_INTF_MAC);
return null;
}
etherReply.setSourceMACAddress(relayAgentMac);
// find destMac
MacAddress clientMac;
Ip6Address peerAddress = Ip6Address.valueOf(dhcp6Relay.getPeerAddress());
Set<Host> clients = hostService.getHostsByIp(peerAddress);
if (clients.isEmpty()) {
log.trace("There's no host found for this address {}", HexString.toHexString(dhcp6Relay.getPeerAddress(), ":"));
log.trace("Let's look up interfaceId {}", HexString.toHexString(peerMac.toBytes(), ":"));
clientMac = peerMac;
} else {
clientMac = clients.iterator().next().mac();
if (clientMac == null) {
log.warn("No client mac address found, abort packet...");
// dhcpRelayCountersStore.incrementCounter(gCount, DhcpRelayCounters.NO_CLIENT_INTF_MAC);
return null;
}
log.trace("Client mac address found from getHostByIp");
}
etherReply.setDestinationMACAddress(clientMac);
// ip header
ipv6Packet.setSourceAddress(dhcp6Relay.getLinkAddress());
ipv6Packet.setDestinationAddress(dhcp6Relay.getPeerAddress());
// udp header
udpPacket.setSourcePort(UDP.DHCP_V6_SERVER_PORT);
if (directConnFlag) {
udpPacket.setDestinationPort(UDP.DHCP_V6_CLIENT_PORT);
} else {
udpPacket.setDestinationPort(UDP.DHCP_V6_SERVER_PORT);
}
boolean hostOrRouteAllowed = learnRouteFromLeasequery || Dhcp6HandlerUtil.getDhcp6LeafMessageType(dhcp6Relay) != MsgType.LEASEQUERY_REPLY;
log.debug("Can add host or route: {}", hostOrRouteAllowed);
if (hostOrRouteAllowed) {
// add host or route
addHostOrRoute(directConnFlag, clientConnectionPoint, dhcp6Relay, embeddedDhcp6, clientMac, clientInterface, vlanIdInUse);
}
udpPacket.setPayload(embeddedDhcp6);
udpPacket.resetChecksum();
ipv6Packet.setPayload(udpPacket);
etherReply.setPayload(ipv6Packet);
return InternalPacket.internalPacket(etherReply, clientConnectionPoint);
}
Aggregations