Search in sources :

Example 1 with IEEE8021Q

use of org.opendaylight.genius.mdsalutil.packet.IEEE8021Q 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;
}
Also used : UDP(org.opendaylight.genius.mdsalutil.packet.UDP) IEEE8021Q(org.opendaylight.genius.mdsalutil.packet.IEEE8021Q) IPv4(org.opendaylight.genius.mdsalutil.packet.IPv4) Ethernet(org.opendaylight.genius.mdsalutil.packet.Ethernet) InetAddress(java.net.InetAddress) PacketException(org.opendaylight.openflowplugin.libraries.liblldp.PacketException) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Aggregations

SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 InetAddress (java.net.InetAddress)1 Ethernet (org.opendaylight.genius.mdsalutil.packet.Ethernet)1 IEEE8021Q (org.opendaylight.genius.mdsalutil.packet.IEEE8021Q)1 IPv4 (org.opendaylight.genius.mdsalutil.packet.IPv4)1 UDP (org.opendaylight.genius.mdsalutil.packet.UDP)1 PacketException (org.opendaylight.openflowplugin.libraries.liblldp.PacketException)1