Search in sources :

Example 1 with BgpFlowSpecNlri

use of org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecNlri in project onos by opennetworkinglab.

the class MpReachNlri method read.

/**
 * Reads from ChannelBuffer and parses MpReachNlri.
 *
 * @param cb channelBuffer
 * @return object of MpReachNlri
 * @throws BgpParseException while parsing MpReachNlri
 */
public static MpReachNlri read(ChannelBuffer cb) throws BgpParseException {
    ChannelBuffer tempBuf = cb.copy();
    Validation parseFlags = Validation.parseAttributeHeader(cb);
    int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
    ChannelBuffer data = tempBuf.readBytes(len);
    if (cb.readableBytes() < parseFlags.getLength()) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, parseFlags.getLength());
    }
    if (!parseFlags.getFirstBit() && parseFlags.getSecondBit() && parseFlags.getThirdBit()) {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
    }
    BgpLSNlri bgpLSNlri = null;
    List<BgpLSNlri> mpReachNlri = new LinkedList<>();
    ChannelBuffer tempCb = cb.readBytes(parseFlags.getLength());
    short afi = 0;
    byte safi = 0;
    IpAddress ipNextHop = null;
    while (tempCb.readableBytes() > 0) {
        afi = tempCb.readShort();
        safi = tempCb.readByte();
        // Supporting for AFI 16388 / SAFI 71 and VPN AFI 16388 / SAFI 128
        if ((afi == Constants.AFI_VALUE) && (safi == Constants.SAFI_VALUE) || (afi == Constants.AFI_VALUE) && (safi == Constants.VPN_SAFI_VALUE)) {
            byte nextHopLen = tempCb.readByte();
            InetAddress ipAddress = Validation.toInetAddress(nextHopLen, tempCb);
            if (ipAddress.isMulticastAddress()) {
                throw new BgpParseException("Multicast not supported");
            }
            ipNextHop = IpAddress.valueOf(ipAddress);
            byte reserved = tempCb.readByte();
            while (tempCb.readableBytes() > 0) {
                short nlriType = tempCb.readShort();
                short totNlriLen = tempCb.readShort();
                if (tempCb.readableBytes() < totNlriLen) {
                    Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, totNlriLen);
                }
                tempBuf = tempCb.readBytes(totNlriLen);
                switch(nlriType) {
                    case BgpNodeLSNlriVer4.NODE_NLRITYPE:
                        bgpLSNlri = BgpNodeLSNlriVer4.read(tempBuf, afi, safi);
                        break;
                    case BgpLinkLsNlriVer4.LINK_NLRITYPE:
                        bgpLSNlri = BgpLinkLsNlriVer4.read(tempBuf, afi, safi);
                        break;
                    case BgpPrefixIPv4LSNlriVer4.PREFIX_IPV4_NLRITYPE:
                    case BgpPrefixIPv4LSNlriVer4.PREFIX_IPV6_NLRITYPE:
                        // RFC 7752 : Structure of IPv4 and IPv6 is same
                        bgpLSNlri = BgpPrefixIPv4LSNlriVer4.read(tempBuf, afi, safi);
                        break;
                    default:
                        log.debug("nlriType not supported" + nlriType);
                }
                mpReachNlri.add(bgpLSNlri);
            }
        } else if ((afi == Constants.AFI_FLOWSPEC_VALUE) && ((safi == Constants.SAFI_FLOWSPEC_VALUE) || (safi == Constants.VPN_SAFI_FLOWSPEC_VALUE))) {
            List<BgpValueType> flowSpecComponents = new LinkedList<>();
            RouteDistinguisher routeDistinguisher = null;
            if (tempCb.readableBytes() > 0) {
                BgpValueType flowSpecComponent = null;
                byte nextHopLen = tempCb.readByte();
                if (nextHopLen > 0) {
                    InetAddress ipAddress = Validation.toInetAddress(nextHopLen, tempCb);
                    if (ipAddress.isMulticastAddress()) {
                        throw new BgpParseException("Multicast not supported");
                    }
                    ipNextHop = IpAddress.valueOf(ipAddress);
                }
                byte reserved = tempCb.readByte();
                if (safi == Constants.VPN_SAFI_FLOWSPEC_VALUE) {
                    routeDistinguisher = new RouteDistinguisher();
                    routeDistinguisher = RouteDistinguisher.read(tempCb);
                }
                while (tempCb.readableBytes() > 0) {
                    short totNlriLen = tempCb.getUnsignedByte(tempCb.readerIndex());
                    if (totNlriLen >= BgpFlowSpecNlri.FLOW_SPEC_LEN) {
                        if (tempCb.readableBytes() < 2) {
                            Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, totNlriLen);
                        }
                        totNlriLen = tempCb.readShort();
                    } else {
                        totNlriLen = tempCb.readByte();
                    }
                    if (tempCb.readableBytes() < totNlriLen) {
                        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, totNlriLen);
                    }
                    tempBuf = tempCb.readBytes(totNlriLen);
                    while (tempBuf.readableBytes() > 0) {
                        short type = tempBuf.readByte();
                        switch(type) {
                            case Constants.BGP_FLOWSPEC_DST_PREFIX:
                                flowSpecComponent = BgpFsDestinationPrefix.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_SRC_PREFIX:
                                flowSpecComponent = BgpFsSourcePrefix.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_IP_PROTO:
                                flowSpecComponent = BgpFsIpProtocol.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_PORT:
                                flowSpecComponent = BgpFsPortNum.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_DST_PORT:
                                flowSpecComponent = BgpFsDestinationPortNum.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_SRC_PORT:
                                flowSpecComponent = BgpFsSourcePortNum.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_ICMP_TP:
                                flowSpecComponent = BgpFsIcmpType.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_ICMP_CD:
                                flowSpecComponent = BgpFsIcmpCode.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_TCP_FLAGS:
                                flowSpecComponent = BgpFsTcpFlags.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_PCK_LEN:
                                flowSpecComponent = BgpFsPacketLength.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_DSCP:
                                flowSpecComponent = BgpFsDscpValue.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_FRAGMENT:
                                flowSpecComponent = BgpFsFragment.read(tempBuf);
                                break;
                            default:
                                log.debug("flow spec type not supported" + type);
                                break;
                        }
                        flowSpecComponents.add(flowSpecComponent);
                    }
                }
            }
            BgpFlowSpecNlri flowSpecDetails = new BgpFlowSpecNlri(flowSpecComponents);
            flowSpecDetails.setRouteDistinguiher(routeDistinguisher);
            return new MpReachNlri(flowSpecDetails, afi, safi);
        } else if ((afi == Constants.AFI_EVPN_VALUE) && (safi == Constants.SAFI_EVPN_VALUE)) {
            List<BgpEvpnNlri> eVpnComponents = null;
            byte nextHopLen = tempCb.readByte();
            InetAddress ipAddress = Validation.toInetAddress(nextHopLen, tempCb);
            if (ipAddress.isMulticastAddress()) {
                throw new BgpParseException("Multicast not supported");
            }
            ipNextHop = IpAddress.valueOf(ipAddress);
            byte reserved = tempCb.readByte();
            while (tempCb.readableBytes() > 0) {
                BgpEvpnNlri eVpnComponent = BgpEvpnNlriImpl.read(tempCb);
                eVpnComponents = new LinkedList<>();
                eVpnComponents.add(eVpnComponent);
                log.info("=====evpn Component is {} ======", eVpnComponent);
            }
            return new MpReachNlri(eVpnComponents, afi, safi, ipNextHop);
        } else if ((afi == Constants.AFI_IPV4_UNICAST && safi == Constants.SAFI_UNICAST) || (afi == Constants.AFI_IPV6_UNICAST && safi == Constants.SAFI_UNICAST)) {
            byte nextHopLen = tempCb.readByte();
            InetAddress ipAddress = Validation.toInetAddress(nextHopLen, tempCb);
            if (ipAddress.isMulticastAddress()) {
                throw new BgpParseException("Multicast not supported");
            }
            ipNextHop = IpAddress.valueOf(ipAddress);
            byte snpa = tempCb.readByte();
            // TODO : Use this info, if required
            if (tempCb.readableBytes() > 0) {
                // Convert bits to bytes
                int mpReachNlriPrefixLength = tempCb.readByte() / ONE_BYTE_LENGTH;
                tempCb.skipBytes(mpReachNlriPrefixLength);
            }
            return new MpReachNlri(mpReachNlri, afi, safi, ipNextHop, parseFlags.getLength());
        } else {
            throw new BgpParseException("Not Supporting afi " + afi + "safi " + safi);
        }
    }
    return new MpReachNlri(mpReachNlri, afi, safi, ipNextHop, parseFlags.getLength());
}
Also used : Validation(org.onosproject.bgpio.util.Validation) BgpParseException(org.onosproject.bgpio.exceptions.BgpParseException) BgpFlowSpecNlri(org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecNlri) LinkedList(java.util.LinkedList) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) BgpLSNlri(org.onosproject.bgpio.protocol.BgpLSNlri) IpAddress(org.onlab.packet.IpAddress) List(java.util.List) LinkedList(java.util.LinkedList) BgpEvpnNlri(org.onosproject.bgpio.protocol.BgpEvpnNlri) InetAddress(java.net.InetAddress)

Example 2 with BgpFlowSpecNlri

use of org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecNlri in project onos by opennetworkinglab.

the class MpUnReachNlri method read.

/**
 * Reads from ChannelBuffer and parses MpUnReachNlri.
 *
 * @param cb ChannelBuffer
 * @return object of MpUnReachNlri
 * @throws BgpParseException while parsing MpUnReachNlri
 */
public static MpUnReachNlri read(ChannelBuffer cb) throws BgpParseException {
    ChannelBuffer tempBuf = cb.copy();
    Validation parseFlags = Validation.parseAttributeHeader(cb);
    int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
    ChannelBuffer data = tempBuf.readBytes(len);
    if (!parseFlags.getFirstBit() && parseFlags.getSecondBit() && parseFlags.getThirdBit()) {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
    }
    if (cb.readableBytes() < parseFlags.getLength()) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, parseFlags.getLength());
    }
    LinkedList<BgpLSNlri> mpUnReachNlri = new LinkedList<>();
    BgpLSNlri bgpLSNlri = null;
    short afi = 0;
    byte safi = 0;
    ChannelBuffer tempCb = cb.readBytes(parseFlags.getLength());
    while (tempCb.readableBytes() > 0) {
        afi = tempCb.readShort();
        safi = tempCb.readByte();
        // Supporting only for AFI 16388 / SAFI 71
        if ((afi == Constants.AFI_VALUE) && (safi == Constants.SAFI_VALUE) || (afi == Constants.AFI_VALUE) && (safi == Constants.VPN_SAFI_VALUE)) {
            while (tempCb.readableBytes() > 0) {
                short nlriType = tempCb.readShort();
                short totNlriLen = tempCb.readShort();
                if (tempCb.readableBytes() < totNlriLen) {
                    Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, totNlriLen);
                }
                tempBuf = tempCb.readBytes(totNlriLen);
                switch(nlriType) {
                    case BgpNodeLSNlriVer4.NODE_NLRITYPE:
                        bgpLSNlri = BgpNodeLSNlriVer4.read(tempBuf, afi, safi);
                        break;
                    case BgpLinkLsNlriVer4.LINK_NLRITYPE:
                        bgpLSNlri = BgpLinkLsNlriVer4.read(tempBuf, afi, safi);
                        break;
                    case BgpPrefixIPv4LSNlriVer4.PREFIX_IPV4_NLRITYPE:
                    case BgpPrefixIPv4LSNlriVer4.PREFIX_IPV6_NLRITYPE:
                        // RFC 7752 : Structure of IPv4 and IPv6 is same
                        bgpLSNlri = BgpPrefixIPv4LSNlriVer4.read(tempBuf, afi, safi);
                        break;
                    default:
                        log.debug("nlriType not supported" + nlriType);
                        break;
                }
                mpUnReachNlri.add(bgpLSNlri);
            }
        } else if ((afi == Constants.AFI_FLOWSPEC_VALUE) && ((safi == Constants.SAFI_FLOWSPEC_VALUE) || (safi == Constants.VPN_SAFI_FLOWSPEC_VALUE))) {
            List<BgpValueType> flowSpecComponents = new LinkedList<>();
            RouteDistinguisher routeDistinguisher = null;
            if (tempCb.readableBytes() > 0) {
                BgpValueType flowSpecComponent = null;
                if (safi == Constants.VPN_SAFI_FLOWSPEC_VALUE) {
                    routeDistinguisher = new RouteDistinguisher();
                    routeDistinguisher = RouteDistinguisher.read(tempCb);
                }
                while (tempCb.readableBytes() > 0) {
                    short totNlriLen = tempCb.getUnsignedByte(tempCb.readerIndex());
                    if (totNlriLen >= BgpFlowSpecNlri.FLOW_SPEC_LEN) {
                        if (tempCb.readableBytes() < 2) {
                            Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, totNlriLen);
                        }
                        totNlriLen = tempCb.readShort();
                    } else {
                        totNlriLen = tempCb.readByte();
                    }
                    if (tempCb.readableBytes() < totNlriLen) {
                        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, totNlriLen);
                    }
                    tempBuf = tempCb.readBytes(totNlriLen);
                    while (tempBuf.readableBytes() > 0) {
                        short type = tempBuf.readByte();
                        switch(type) {
                            case Constants.BGP_FLOWSPEC_DST_PREFIX:
                                flowSpecComponent = BgpFsDestinationPrefix.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_SRC_PREFIX:
                                flowSpecComponent = BgpFsSourcePrefix.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_IP_PROTO:
                                flowSpecComponent = BgpFsIpProtocol.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_PORT:
                                flowSpecComponent = BgpFsPortNum.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_DST_PORT:
                                flowSpecComponent = BgpFsDestinationPortNum.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_SRC_PORT:
                                flowSpecComponent = BgpFsSourcePortNum.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_ICMP_TP:
                                flowSpecComponent = BgpFsIcmpType.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_ICMP_CD:
                                flowSpecComponent = BgpFsIcmpCode.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_TCP_FLAGS:
                                flowSpecComponent = BgpFsTcpFlags.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_PCK_LEN:
                                flowSpecComponent = BgpFsPacketLength.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_DSCP:
                                flowSpecComponent = BgpFsDscpValue.read(tempBuf);
                                break;
                            case Constants.BGP_FLOWSPEC_FRAGMENT:
                                flowSpecComponent = BgpFsFragment.read(tempBuf);
                                break;
                            default:
                                log.debug("flow spec type not supported" + type);
                                break;
                        }
                        flowSpecComponents.add(flowSpecComponent);
                    }
                }
            }
            BgpFlowSpecNlri flowSpecDetails = new BgpFlowSpecNlri(flowSpecComponents);
            flowSpecDetails.setRouteDistinguiher(routeDistinguisher);
            return new MpUnReachNlri(flowSpecDetails, afi, safi);
        } else if ((afi == Constants.AFI_EVPN_VALUE) && (safi == Constants.SAFI_EVPN_VALUE)) {
            List<BgpEvpnNlri> eVpnComponents = null;
            while (tempCb.readableBytes() > 0) {
                BgpEvpnNlri eVpnComponent = BgpEvpnNlriImpl.read(tempCb);
                eVpnComponents = new LinkedList<>();
                eVpnComponents.add(eVpnComponent);
                log.info("=====evpn Component is {} ======", eVpnComponent);
            }
            return new MpUnReachNlri(eVpnComponents, afi, safi);
        } else if ((afi == Constants.AFI_IPV4_UNICAST && safi == Constants.SAFI_UNICAST) || (afi == Constants.AFI_IPV6_UNICAST && safi == Constants.SAFI_UNICAST)) {
            // Right now, we do not know where to use these bytes so we skip them
            // If we do not skip, there are errors in reading subsequent TLVs
            // TODO : Use this info, if required
            // Short for AFI, byte for SAFI
            int remainingLen = parseFlags.getLength() - 3;
            if (remainingLen > 0) {
                tempCb.skipBytes(remainingLen);
            }
        } else {
            // TODO: check with the values got from capability
            throw new BgpParseException("Not Supporting afi " + afi + "safi " + safi);
        }
    }
    return new MpUnReachNlri(mpUnReachNlri, afi, safi, parseFlags.getLength());
}
Also used : Validation(org.onosproject.bgpio.util.Validation) BgpParseException(org.onosproject.bgpio.exceptions.BgpParseException) BgpFlowSpecNlri(org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecNlri) LinkedList(java.util.LinkedList) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) BgpLSNlri(org.onosproject.bgpio.protocol.BgpLSNlri) List(java.util.List) LinkedList(java.util.LinkedList) BgpEvpnNlri(org.onosproject.bgpio.protocol.BgpEvpnNlri)

Aggregations

LinkedList (java.util.LinkedList)2 List (java.util.List)2 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)2 BgpParseException (org.onosproject.bgpio.exceptions.BgpParseException)2 BgpEvpnNlri (org.onosproject.bgpio.protocol.BgpEvpnNlri)2 BgpLSNlri (org.onosproject.bgpio.protocol.BgpLSNlri)2 BgpFlowSpecNlri (org.onosproject.bgpio.protocol.flowspec.BgpFlowSpecNlri)2 Validation (org.onosproject.bgpio.util.Validation)2 InetAddress (java.net.InetAddress)1 IpAddress (org.onlab.packet.IpAddress)1