use of org.onlab.packet.Ethernet in project onos by opennetworkinglab.
the class OpenstackSwitchingDhcpHandlerTest method testDiscover.
/**
* Tests the response to a DHCP Discover Packet.
*/
@Test
public void testDiscover() {
Ethernet dhcpDiscover = constructDhcpPacket(DHCP.MsgType.DHCPDISCOVER);
testMsgType = DHCP.MsgType.DHCPDISCOVER;
sendPacket(dhcpDiscover);
}
use of org.onlab.packet.Ethernet in project onos by opennetworkinglab.
the class FabricInterpreter method mapInboundPacket.
@Override
public InboundPacket mapInboundPacket(PiPacketOperation packetIn, DeviceId deviceId) throws PiInterpreterException {
// Assuming that the packet is ethernet, which is fine since fabric.p4
// can deparse only ethernet packets.
Ethernet ethPkt;
try {
ethPkt = Ethernet.deserializer().deserialize(packetIn.data().asArray(), 0, packetIn.data().size());
} catch (DeserializationException dex) {
throw new PiInterpreterException(dex.getMessage());
}
// Returns the ingress port packet metadata.
Optional<PiPacketMetadata> packetMetadata = packetIn.metadatas().stream().filter(m -> m.id().equals(FabricConstants.INGRESS_PORT)).findFirst();
if (packetMetadata.isPresent()) {
ImmutableByteSequence portByteSequence = packetMetadata.get().value();
short s = portByteSequence.asReadOnlyBuffer().getShort();
ConnectPoint receivedFrom = new ConnectPoint(deviceId, PortNumber.portNumber(s));
if (!receivedFrom.port().hasName()) {
receivedFrom = translateSwitchPort(receivedFrom);
}
ByteBuffer rawData = ByteBuffer.wrap(packetIn.data().asArray());
return new DefaultInboundPacket(receivedFrom, ethPkt, rawData);
} else {
throw new PiInterpreterException(format("Missing metadata '%s' in packet-in received from '%s': %s", FabricConstants.INGRESS_PORT, deviceId, packetIn));
}
}
use of org.onlab.packet.Ethernet in project onos by opennetworkinglab.
the class NeighborAdvertisement method buildNdpAdv.
/**
* Builds an NDP reply based on a request.
*
* @param srcIp the IP address to use as the reply source
* @param srcMac the MAC address to use as the reply source
* @param request the Neighbor Solicitation request we got
* @return an Ethernet frame containing the Neighbor Advertisement reply
*/
public static Ethernet buildNdpAdv(Ip6Address srcIp, MacAddress srcMac, Ethernet request) {
checkNotNull(srcIp, "IP address cannot be null");
checkNotNull(srcMac, "MAC address cannot be null");
checkNotNull(request, "Request cannot be null");
checkArgument(request.getEtherType() == Ethernet.TYPE_IPV6, "EtherType must be IPv6");
final IPv6 ipv6Request = (IPv6) request.getPayload();
checkArgument(ipv6Request.getNextHeader() == IPv6.PROTOCOL_ICMP6, "Protocol must be ICMP6");
final ICMP6 icmpv6 = (ICMP6) ipv6Request.getPayload();
checkArgument(icmpv6.getIcmpType() == ICMP6.NEIGHBOR_SOLICITATION, "ICMP6 type must be NEIGHBOR_SOLICITATION");
Ethernet eth = new Ethernet();
eth.setDestinationMACAddress(request.getSourceMAC());
eth.setSourceMACAddress(srcMac);
eth.setEtherType(Ethernet.TYPE_IPV6);
eth.setVlanID(request.getVlanID());
IPv6 ipv6 = new IPv6();
ipv6.setSourceAddress(srcIp.toOctets());
ipv6.setDestinationAddress(ipv6Request.getSourceAddress());
ipv6.setHopLimit(NDP_HOP_LIMIT);
ipv6.setNextHeader(IPv6.PROTOCOL_ICMP6);
ICMP6 icmp6 = new ICMP6();
icmp6.setIcmpType(ICMP6.NEIGHBOR_ADVERTISEMENT);
icmp6.setIcmpCode(RESERVED_CODE);
NeighborAdvertisement nadv = new NeighborAdvertisement();
nadv.setTargetAddress(srcIp.toOctets());
nadv.setSolicitedFlag(NDP_SOLICITED_FLAG);
nadv.setOverrideFlag(NDP_OVERRIDE_FLAG);
nadv.addOption(NeighborDiscoveryOptions.TYPE_TARGET_LL_ADDRESS, srcMac.toBytes());
icmp6.setPayload(nadv);
ipv6.setPayload(icmp6);
eth.setPayload(ipv6);
return eth;
}
use of org.onlab.packet.Ethernet in project onos by opennetworkinglab.
the class Dhcp6RelayTest method serializeRequest.
/**
* Test serialize relay message with request message.
*
* @throws Exception exception while serialize the DHCPv6 payload
*/
@Test
public void serializeRequest() 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.REQUEST.value());
relaiedDhcp6.setTransactionId(XID_2);
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);
// 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);
// Option request
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 address
Dhcp6IaAddressOption iaAddressOption = new Dhcp6IaAddressOption();
iaAddressOption.setIp6Address(IA_ADDRESS);
iaAddressOption.setPreferredLifetime(PREFFERRED_LT_REQ);
iaAddressOption.setValidLifetime(VALID_LT_REQ_2);
// IA NA
Dhcp6IaNaOption iaNaOption = new Dhcp6IaNaOption();
iaNaOption.setIaId(IA_ID);
iaNaOption.setT1(T1_CLIENT);
iaNaOption.setT2(T2_CLIENT);
iaNaOption.setOptions(ImmutableList.of(iaAddressOption));
options.add(iaNaOption);
relaiedDhcp6.setOptions(options);
Dhcp6Option subscriberId = new Dhcp6Option();
subscriberId.setCode(DHCP6.OptionCode.SUBSCRIBER_ID.value());
subscriberId.setLength((short) 10);
subscriberId.setData(SERVER_IP.toString().getBytes(US_ASCII));
Dhcp6RelayOption relayOption = new Dhcp6RelayOption();
relayOption.setPayload(relaiedDhcp6);
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) 0x9aab);
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(REQUEST)), eth.serialize());
}
use of org.onlab.packet.Ethernet 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());
}
Aggregations