use of org.onlab.packet.UDP in project onos by opennetworkinglab.
the class Dhcp6Test method serializeAdvertise.
/**
* Test serialize advertise message.
*
* @throws Exception exception while serialize the DHCPv6 payload
*/
@Test
public void serializeAdvertise() throws Exception {
DHCP6 dhcp6 = new DHCP6();
dhcp6.setMsgType(DHCP6.MsgType.ADVERTISE.value());
dhcp6.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);
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(ADVERTISE)), eth.serialize());
}
use of org.onlab.packet.UDP in project onos by opennetworkinglab.
the class Dhcp6Test method serializeRequest.
/**
* Test serialize request message.
*
* @throws Exception exception while serialize the DHCPv6 payload
*/
@Test
public void serializeRequest() throws Exception {
DHCP6 dhcp6 = new DHCP6();
dhcp6.setMsgType(DHCP6.MsgType.REQUEST.value());
dhcp6.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);
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) 0xffc1);
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(REQUEST)), eth.serialize());
}
use of org.onlab.packet.UDP in project onos by opennetworkinglab.
the class AuthenticationTest method setUpBeforeClass.
@BeforeClass
public static void setUpBeforeClass() throws Exception {
data = new Data();
data.setData("testSerialize".getBytes());
udp = new UDP();
udp.setPayload(data);
byte[] bytePayload = udp.serialize();
byte[] byteHeader = { (byte) 0x11, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x13, (byte) 0x57, (byte) 0x24, (byte) 0x68, (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0x00, (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44 };
bytePacket = new byte[byteHeader.length + bytePayload.length];
System.arraycopy(byteHeader, 0, bytePacket, 0, byteHeader.length);
System.arraycopy(bytePayload, 0, bytePacket, byteHeader.length, bytePayload.length);
}
use of org.onlab.packet.UDP in project onos by opennetworkinglab.
the class OpenstackRoutingSnatHandler method setStatelessSnatDownstreamRules.
private void setStatelessSnatDownstreamRules(InstancePort srcInstPort, String segmentId, Type networkType, IpAddress externalIp, ExternalPeerRouter externalPeerRouter, TpPort patPort, InboundPacket packetIn) {
IPv4 iPacket = (IPv4) packetIn.parsed().getPayload();
IpAddress internalIp = IpAddress.valueOf(iPacket.getSourceAddress());
TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder().matchEthType(Ethernet.TYPE_IPV4).matchIPProtocol(iPacket.getProtocol()).matchIPDst(IpPrefix.valueOf(externalIp.getIp4Address(), VM_PREFIX)).matchIPSrc(IpPrefix.valueOf(iPacket.getDestinationAddress(), VM_PREFIX));
TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder().setEthDst(packetIn.parsed().getSourceMAC()).setIpDst(internalIp);
if (!externalPeerRouter.vlanId().equals(VlanId.NONE)) {
sBuilder.matchVlanId(externalPeerRouter.vlanId());
tBuilder.popVlan();
}
switch(networkType) {
case VXLAN:
case GRE:
case GENEVE:
tBuilder.setTunnelId(Long.parseLong(segmentId));
break;
case VLAN:
tBuilder.pushVlan().setVlanId(VlanId.vlanId(segmentId));
break;
default:
final String error = String.format("%s %s", ERR_UNSUPPORTED_NET_TYPE, networkType.toString());
throw new IllegalStateException(error);
}
switch(iPacket.getProtocol()) {
case IPv4.PROTOCOL_TCP:
TCP tcpPacket = (TCP) iPacket.getPayload();
sBuilder.matchTcpSrc(TpPort.tpPort(tcpPacket.getDestinationPort())).matchTcpDst(patPort);
tBuilder.setTcpDst(TpPort.tpPort(tcpPacket.getSourcePort()));
break;
case IPv4.PROTOCOL_UDP:
UDP udpPacket = (UDP) iPacket.getPayload();
sBuilder.matchUdpSrc(TpPort.tpPort(udpPacket.getDestinationPort())).matchUdpDst(patPort);
tBuilder.setUdpDst(TpPort.tpPort(udpPacket.getSourcePort()));
break;
default:
break;
}
OpenstackNode srcNode = osNodeService.node(srcInstPort.deviceId());
osNodeService.completeNodes(GATEWAY).forEach(gNode -> {
TrafficTreatment treatment = getDownstreamTreatment(networkType, tBuilder, gNode, srcNode);
osFlowRuleService.setRule(appId, gNode.intgBridge(), sBuilder.build(), treatment, PRIORITY_SNAT_RULE, GW_COMMON_TABLE, true);
});
}
use of org.onlab.packet.UDP in project onos by opennetworkinglab.
the class OpenstackSwitchingDhcpHandlerTest method constructDhcpPacket.
/**
* Constructs an Ethernet packet containing a DHCP payload.
*
* @param msgType DHCP message type
* @return Ethernet packet
*/
private Ethernet constructDhcpPacket(DHCP.MsgType msgType) {
// Ethernet frame
Ethernet ethFrame = new Ethernet();
ethFrame.setSourceMACAddress(CLIENT_HOST.mac());
ethFrame.setDestinationMACAddress(MacAddress.BROADCAST);
ethFrame.setEtherType(Ethernet.TYPE_IPV4);
ethFrame.setVlanID((short) 2);
// IP packet
IPv4 ipv4Pkt = new IPv4();
ipv4Pkt.setSourceAddress(0);
ipv4Pkt.setDestinationAddress(BROADCAST.toInt());
ipv4Pkt.setTtl((byte) 127);
// UDP datagram
UDP udpDatagram = new UDP();
udpDatagram.setSourcePort((byte) UDP.DHCP_CLIENT_PORT);
udpDatagram.setDestinationPort((byte) UDP.DHCP_SERVER_PORT);
// DHCP payload
DHCP dhcp = new DHCP();
dhcp.setOpCode(DHCP.OPCODE_REQUEST);
dhcp.setYourIPAddress(0);
dhcp.setServerIPAddress(0);
dhcp.setTransactionId(TRANSACTION_ID);
dhcp.setClientHardwareAddress(CLIENT_HOST.mac().toBytes());
dhcp.setHardwareType(DHCP.HWTYPE_ETHERNET);
dhcp.setHardwareAddressLength((byte) 6);
// DHCP options start...
DhcpOption option = new DhcpOption();
List<DhcpOption> optionList = new ArrayList<>();
// DHCP message type
option.setCode(OptionCode_MessageType.getValue());
option.setLength((byte) 1);
byte[] optionData = { (byte) msgType.getValue() };
option.setData(optionData);
optionList.add(option);
// DHCP requested IP address
option = new DhcpOption();
option.setCode(OptionCode_RequestedIP.getValue());
option.setLength((byte) 4);
optionData = Ip4Address.valueOf(EXPECTED_IP).toOctets();
option.setData(optionData);
optionList.add(option);
// DHCP domain server
Subnet subnet = dhcpHandler.osNetworkService.subnet("subnet");
option = new DhcpOption();
option.setCode(OptionCode_DomainServer.getValue());
option.setLength((byte) 8);
ByteBuffer dnsByteBuf = ByteBuffer.allocate(8);
dnsByteBuf.put(DEFAULT_PRIMARY_DNS.toOctets());
dnsByteBuf.put(DEFAULT_SECONDARY_DNS.toOctets());
option.setData(dnsByteBuf.array());
optionList.add(option);
// MTU
option = new DhcpOption();
option.setCode(DHCP_OPTION_MTU);
option.setLength((byte) 2);
option.setData(ByteBuffer.allocate(2).putShort(MTU).array());
optionList.add(option);
// classless static route
option = new DhcpOption();
option.setCode(OptionCode_Classless_Static_Route.getValue());
option.setLength((byte) HOST_ROUTES_SIZE);
ByteBuffer hostRouteByteBuf = ByteBuffer.allocate(HOST_ROUTES_SIZE);
subnet.getHostRoutes().forEach(h -> {
IpPrefix ipPrefix = IpPrefix.valueOf(h.getDestination());
hostRouteByteBuf.put(bytesDestinationDescriptor(ipPrefix));
hostRouteByteBuf.put(Ip4Address.valueOf(h.getNexthop()).toOctets());
});
option.setData(hostRouteByteBuf.array());
optionList.add(option);
// default router address setup
option = new DhcpOption();
option.setCode(OptionCode_RouterAddress.getValue());
option.setLength((byte) 4);
option.setData(Ip4Address.valueOf(subnet.getGateway()).toOctets());
optionList.add(option);
// DHCP options end...
option = new DhcpOption();
option.setCode(OptionCode_END.getValue());
option.setLength((byte) 1);
optionList.add(option);
dhcp.setOptions(optionList);
udpDatagram.setPayload(dhcp);
ipv4Pkt.setPayload(udpDatagram);
ethFrame.setPayload(ipv4Pkt);
return ethFrame;
}
Aggregations