use of org.opendaylight.genius.mdsalutil.packet.Ethernet in project netvirt by opendaylight.
the class DhcpPktHandler method getDhcpPacketOut.
// "Consider returning a zero length array rather than null" - the eventual user of the returned byte[] likely
// expects null and it's unclear what the behavior would be if empty array was returned.
@SuppressFBWarnings("PZLA_PREFER_ZERO_LENGTH_ARRAYS")
protected byte[] getDhcpPacketOut(DHCP reply, Ethernet etherPkt, String phyAddrees) {
if (reply == null) {
/*
* DECLINE or RELEASE don't result in reply packet
*/
return null;
}
LOG.trace("Sending DHCP Pkt {}", reply);
InetAddress serverIp = reply.getOptionInetAddr(DHCPConstants.OPT_SERVER_IDENTIFIER);
// create UDP pkt
UDP udpPkt = new UDP();
byte[] rawPkt;
try {
rawPkt = reply.serialize();
} catch (PacketException e) {
LOG.warn("Failed to serialize packet", e);
return null;
}
udpPkt.setRawPayload(rawPkt);
udpPkt.setDestinationPort(DhcpMConstants.DHCP_CLIENT_PORT);
udpPkt.setSourcePort(DhcpMConstants.DHCP_SERVER_PORT);
udpPkt.setLength((short) (rawPkt.length + 8));
// Create IP Pkt
try {
rawPkt = udpPkt.serialize();
} catch (PacketException e) {
LOG.warn("Failed to serialize packet", e);
return null;
}
short checkSum = 0;
boolean computeUdpChecksum = true;
if (computeUdpChecksum) {
checkSum = computeChecksum(rawPkt, serverIp.getAddress(), NetUtils.intToByteArray4(DhcpMConstants.BCAST_IP));
}
udpPkt.setChecksum(checkSum);
IPv4 ip4Reply = new IPv4();
ip4Reply.setPayload(udpPkt);
ip4Reply.setProtocol(IPProtocols.UDP.byteValue());
ip4Reply.setSourceAddress(serverIp);
ip4Reply.setDestinationAddress(DhcpMConstants.BCAST_IP);
ip4Reply.setTotalLength((short) (rawPkt.length + 20));
ip4Reply.setTtl((byte) 32);
// create Ethernet Frame
Ethernet ether = new Ethernet();
if (etherPkt.getEtherType() == (short) NwConstants.ETHTYPE_802_1Q) {
IEEE8021Q vlanPacket = (IEEE8021Q) etherPkt.getPayload();
IEEE8021Q vlanTagged = new IEEE8021Q();
vlanTagged.setCFI(vlanPacket.getCfi());
vlanTagged.setPriority(vlanPacket.getPriority());
vlanTagged.setVlanId(vlanPacket.getVlanId());
vlanTagged.setPayload(ip4Reply);
vlanTagged.setEtherType(EtherTypes.IPv4.shortValue());
ether.setPayload(vlanTagged);
ether.setEtherType((short) NwConstants.ETHTYPE_802_1Q);
} else {
ether.setEtherType(EtherTypes.IPv4.shortValue());
ether.setPayload(ip4Reply);
}
ether.setSourceMACAddress(getServerMacAddress(phyAddrees));
ether.setDestinationMACAddress(etherPkt.getSourceMACAddress());
try {
rawPkt = ether.serialize();
} catch (PacketException e) {
LOG.warn("Failed to serialize ethernet reply", e);
return null;
}
return rawPkt;
}
use of org.opendaylight.genius.mdsalutil.packet.Ethernet in project netvirt by opendaylight.
the class ArpUtils method createEthernetPacket.
private static byte[] createEthernetPacket(byte[] sourceMAC, byte[] targetMAC, byte[] arp) {
Ethernet ethernet = new Ethernet();
byte[] rawEthPkt = null;
try {
ethernet.setSourceMACAddress(sourceMAC);
ethernet.setDestinationMACAddress(targetMAC);
ethernet.setEtherType(EtherTypes.ARP.shortValue());
ethernet.setRawPayload(arp);
rawEthPkt = ethernet.serialize();
} catch (PacketException ex) {
LOG.error("VPNUtil: Serialized Ethernet packet with sourceMacAddress {} targetMacAddress {} exception ", sourceMAC, targetMAC, ex);
}
return rawEthPkt;
}
use of org.opendaylight.genius.mdsalutil.packet.Ethernet in project genius by opendaylight.
the class ArpPacketUtil method getPayload.
static byte[] getPayload(short opCode, byte[] senderMacAddress, byte[] senderIP, byte[] targetMacAddress, byte[] targetIP) throws PacketException {
ARP arp = createARPPacket(opCode, senderMacAddress, senderIP, targetMacAddress, targetIP);
Ethernet ethernet = createEthernetPacket(senderMacAddress, targetMacAddress, arp);
return ethernet.serialize();
}
use of org.opendaylight.genius.mdsalutil.packet.Ethernet in project genius by opendaylight.
the class AlivenessProtocolHandlerLLDP method makeLLDPPacket.
public Ethernet makeLLDPPacket(String nodeId, long portNum, byte[] srcMac, String sourceInterface) {
// Create LLDP TTL TLV
LLDPTLV lldpTlvTTL = buildLLDTLV(LLDPTLV.TLVType.TTL, new byte[] { (byte) 0, (byte) 120 });
LLDPTLV lldpTlvChassisId = buildLLDTLV(LLDPTLV.TLVType.ChassisID, LLDPTLV.createChassisIDTLVValue(colonize(StringUtils.leftPad(Long.toHexString(MDSALUtil.getDpnIdFromNodeName(nodeId).longValue()), 16, "0"))));
LLDPTLV lldpTlvSystemName = buildLLDTLV(TLVType.SystemName, LLDPTLV.createSystemNameTLVValue(nodeId));
LLDPTLV lldpTlvPortId = buildLLDTLV(TLVType.PortID, LLDPTLV.createPortIDTLVValue(Long.toHexString(portNum)));
String customValue = sourceInterface + "#" + getPacketId();
LOG.debug("Sending LLDP packet, custome value {}", customValue);
LLDPTLV lldpTlvCustom = buildLLDTLV(TLVType.Custom, customValue.getBytes(LLDPTLV_CHARSET));
@SuppressWarnings("AbbreviationAsWordInName") List<LLDPTLV> lstLLDPTLVCustom = new ArrayList<>();
lstLLDPTLVCustom.add(lldpTlvCustom);
LLDP lldpDiscoveryPacket = new LLDP();
lldpDiscoveryPacket.setChassisId(lldpTlvChassisId).setPortId(lldpTlvPortId).setTtl(lldpTlvTTL).setSystemNameId(lldpTlvSystemName).setOptionalTLVList(lstLLDPTLVCustom);
byte[] destMac = LLDP.LLDP_MULTICAST_MAC;
Ethernet ethernetPacket = new Ethernet();
ethernetPacket.setSourceMACAddress(srcMac).setDestinationMACAddress(destMac).setEtherType(EtherTypes.LLDP.shortValue()).setPayload(lldpDiscoveryPacket);
return ethernetPacket;
}
Aggregations