Search in sources :

Example 6 with Validation

use of org.onosproject.bgpio.util.Validation in project onos by opennetworkinglab.

the class LocalPref method read.

/**
 * Reads the channel buffer and returns object of LocalPref.
 *
 * @param cb channelBuffer
 * @return object of LocalPref
 * @throws BgpParseException while parsing localPref attribute
 */
public static LocalPref read(ChannelBuffer cb) throws BgpParseException {
    int localPref;
    ChannelBuffer tempCb = cb.copy();
    Validation parseFlags = Validation.parseAttributeHeader(cb);
    if ((parseFlags.getLength() > LOCAL_PREF_MAX_LEN) || cb.readableBytes() < parseFlags.getLength()) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, parseFlags.getLength());
    }
    int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
    ChannelBuffer data = tempCb.readBytes(len);
    if (parseFlags.getFirstBit()) {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
    }
    localPref = cb.readInt();
    return new LocalPref(localPref);
}
Also used : Validation(org.onosproject.bgpio.util.Validation) BgpParseException(org.onosproject.bgpio.exceptions.BgpParseException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 7 with Validation

use of org.onosproject.bgpio.util.Validation 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)

Example 8 with Validation

use of org.onosproject.bgpio.util.Validation in project onos by opennetworkinglab.

the class NextHop method read.

/**
 * Reads from ChannelBuffer and parses NextHop.
 *
 * @param cb ChannelBuffer
 * @return object of NextHop
 * @throws BgpParseException while parsing nexthop attribute
 */
public static NextHop read(ChannelBuffer cb) throws BgpParseException {
    Ip4Address nextHop;
    ChannelBuffer tempCb = cb.copy();
    Validation parseFlags = Validation.parseAttributeHeader(cb);
    if (cb.readableBytes() < parseFlags.getLength()) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, parseFlags.getLength());
    }
    int len = parseFlags.isShort() ? parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : parseFlags.getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
    ChannelBuffer data = tempCb.readBytes(len);
    if (parseFlags.getFirstBit() && !parseFlags.getSecondBit() && parseFlags.getThirdBit()) {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
    }
    InetAddress ipAddress = Validation.toInetAddress(parseFlags.getLength(), cb);
    if (ipAddress.isMulticastAddress()) {
        throw new BgpParseException("Multicast address is not supported");
    }
    nextHop = Ip4Address.valueOf(ipAddress);
    return new NextHop(nextHop);
}
Also used : Validation(org.onosproject.bgpio.util.Validation) BgpParseException(org.onosproject.bgpio.exceptions.BgpParseException) Ip4Address(org.onlab.packet.Ip4Address) InetAddress(java.net.InetAddress) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 9 with Validation

use of org.onosproject.bgpio.util.Validation in project onos by opennetworkinglab.

the class Origin method read.

/**
 * Reads from ChannelBuffer and parses Origin.
 *
 * @param cb ChannelBuffer
 * @return object of Origin
 * @throws BgpParseException while parsing Origin path attribute
 */
public static Origin read(ChannelBuffer cb) throws BgpParseException {
    ChannelBuffer tempCb = 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 = tempCb.readBytes(len);
    if ((parseFlags.getLength() > ORIGIN_VALUE_LEN) || (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);
    }
    byte originValue;
    originValue = cb.readByte();
    if ((originValue != OriginType.INCOMPLETE.value) && (originValue != OriginType.IGP.value) && (originValue != OriginType.EGP.value)) {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.INVALID_ORIGIN_ATTRIBUTE, data);
    }
    return new Origin(originValue);
}
Also used : Validation(org.onosproject.bgpio.util.Validation) BgpParseException(org.onosproject.bgpio.exceptions.BgpParseException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 10 with Validation

use of org.onosproject.bgpio.util.Validation in project onos by opennetworkinglab.

the class AsPath method read.

/**
 * Reads from the channel buffer and parses AsPath.
 *
 * @param cb ChannelBuffer
 * @return object of AsPath
 * @throws BgpParseException while parsing AsPath
 */
public static AsPath read(ChannelBuffer cb) throws BgpParseException {
    List<Short> aspathSet = new ArrayList<>();
    List<Short> aspathSeq = new ArrayList<>();
    ChannelBuffer tempCb = cb.copy();
    Validation validation = Validation.parseAttributeHeader(cb);
    if (cb.readableBytes() < validation.getLength()) {
        Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, validation.getLength());
    }
    // if fourth bit is set, length is read as short otherwise as byte , len includes type, length and value
    int len = validation.isShort() ? validation.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : validation.getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
    ChannelBuffer data = tempCb.readBytes(len);
    if (validation.getFirstBit() && !validation.getSecondBit() && validation.getThirdBit()) {
        throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
    }
    ChannelBuffer tempBuf = cb.readBytes(validation.getLength());
    while (tempBuf.readableBytes() > 0) {
        byte pathSegType = tempBuf.readByte();
        // no of ASes
        byte pathSegLen = tempBuf.readByte();
        int length = pathSegLen * ASNUM_SIZE;
        if (tempBuf.readableBytes() < length) {
            Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR, length);
        }
        ChannelBuffer aspathBuf = tempBuf.readBytes(length);
        while (aspathBuf.readableBytes() > 0) {
            short asNum;
            asNum = aspathBuf.readShort();
            switch(pathSegType) {
                case ASPATH_SET_TYPE:
                    aspathSet.add(asNum);
                    break;
                case ASPATH_SEQ_TYPE:
                    aspathSeq.add(asNum);
                    break;
                default:
                    log.debug("Other type Not Supported:" + pathSegType);
            }
        }
    }
    return new AsPath(aspathSet, aspathSeq);
}
Also used : Validation(org.onosproject.bgpio.util.Validation) BgpParseException(org.onosproject.bgpio.exceptions.BgpParseException) ArrayList(java.util.ArrayList) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Aggregations

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