Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 16 | package org.onosproject.bgpio.protocol.ver4; |
| 17 | |
| 18 | import java.util.LinkedList; |
| 19 | import java.util.ListIterator; |
| 20 | |
| 21 | import org.jboss.netty.buffer.ChannelBuffer; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 22 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 23 | import org.onosproject.bgpio.protocol.BgpMessageReader; |
| 24 | import org.onosproject.bgpio.protocol.BgpMessageWriter; |
| 25 | import org.onosproject.bgpio.protocol.BgpOpenMsg; |
| 26 | import org.onosproject.bgpio.protocol.BgpType; |
| 27 | import org.onosproject.bgpio.protocol.BgpVersion; |
| 28 | import org.onosproject.bgpio.types.BgpErrorType; |
| 29 | import org.onosproject.bgpio.types.BgpHeader; |
| 30 | import org.onosproject.bgpio.types.BgpValueType; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 31 | import org.onosproject.bgpio.types.FourOctetAsNumCapabilityTlv; |
| 32 | import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 33 | import org.onosproject.bgpio.util.Validation; |
Shashikanth VH | 44967ec | 2016-02-04 19:46:16 +0530 | [diff] [blame] | 34 | import org.onosproject.bgpio.util.Constants; |
Shashikanth VH | b58cfd5 | 2016-04-21 16:45:50 +0530 | [diff] [blame] | 35 | import org.onosproject.bgpio.types.RpdCapabilityTlv; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 36 | import org.slf4j.Logger; |
| 37 | import org.slf4j.LoggerFactory; |
| 38 | |
| 39 | import com.google.common.base.MoreObjects; |
| 40 | |
| 41 | /** |
| 42 | * Provides BGP open message. |
| 43 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 44 | public class BgpOpenMsgVer4 implements BgpOpenMsg { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | 0 1 2 3 |
| 48 | 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 49 | +-+-+-+-+-+-+-+-+ |
| 50 | | Version | |
| 51 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 52 | | My Autonomous System | |
| 53 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 54 | | Hold Time | |
| 55 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 56 | | BGP Identifier | |
| 57 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 58 | | Opt Parm Len | |
| 59 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 60 | | Optional Parameters (variable) | |
| 61 | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 62 | OPEN Message Format |
| 63 | REFERENCE : RFC 4271 |
| 64 | */ |
| 65 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 66 | protected static final Logger log = LoggerFactory.getLogger(BgpOpenMsgVer4.class); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 67 | |
| 68 | public static final byte PACKET_VERSION = 4; |
| 69 | public static final int OPEN_MSG_MINIMUM_LENGTH = 10; |
| 70 | public static final int MSG_HEADER_LENGTH = 19; |
| 71 | public static final int MARKER_LENGTH = 16; |
| 72 | public static final int DEFAULT_HOLD_TIME = 120; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 73 | public static final short AS_TRANS = 23456; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 74 | public static final int OPT_PARA_TYPE_CAPABILITY = 2; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 75 | public static final BgpType MSG_TYPE = BgpType.OPEN; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 76 | public static final short AFI = 16388; |
| 77 | public static final byte SAFI = 71; |
| 78 | public static final byte RES = 0; |
| 79 | public static final int FOUR_OCTET_AS_NUM_CAPA_TYPE = 65; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 80 | public static final byte[] MARKER = new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 81 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 82 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 83 | public static final BgpHeader DEFAULT_OPEN_HEADER = new BgpHeader(MARKER, |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 84 | (short) OPEN_MSG_MINIMUM_LENGTH, (byte) 0X01); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 85 | private BgpHeader bgpMsgHeader; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 86 | private byte version; |
Shashikanth VH | 09792f0 | 2016-05-10 16:59:57 +0530 | [diff] [blame] | 87 | private long asNumber; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 88 | private short holdTime; |
| 89 | private int bgpId; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 90 | private boolean isLargeAsCapabilitySet; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 91 | private LinkedList<BgpValueType> capabilityTlv; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 92 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 93 | public static final BgpOpenMsgVer4.Reader READER = new Reader(); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * reset variables. |
| 97 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 98 | public BgpOpenMsgVer4() { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 99 | this.bgpMsgHeader = null; |
| 100 | this.version = 0; |
| 101 | this.holdTime = 0; |
| 102 | this.asNumber = 0; |
| 103 | this.bgpId = 0; |
| 104 | this.capabilityTlv = null; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Constructor to initialize all variables of BGP Open message. |
| 109 | * |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 110 | * @param bgpMsgHeader BGP Header in open message |
| 111 | * @param version BGP version in open message |
| 112 | * @param holdTime hold time in open message |
| 113 | * @param asNumber AS number in open message |
| 114 | * @param bgpId BGP identifier in open message |
| 115 | * @param capabilityTlv capabilities in open message |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 116 | */ |
Shashikanth VH | 09792f0 | 2016-05-10 16:59:57 +0530 | [diff] [blame] | 117 | public BgpOpenMsgVer4(BgpHeader bgpMsgHeader, byte version, long asNumber, short holdTime, |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 118 | int bgpId, LinkedList<BgpValueType> capabilityTlv) { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 119 | this.bgpMsgHeader = bgpMsgHeader; |
| 120 | this.version = version; |
| 121 | this.asNumber = asNumber; |
| 122 | this.holdTime = holdTime; |
| 123 | this.bgpId = bgpId; |
| 124 | this.capabilityTlv = capabilityTlv; |
| 125 | } |
| 126 | |
| 127 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 128 | public BgpHeader getHeader() { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 129 | return this.bgpMsgHeader; |
| 130 | } |
| 131 | |
| 132 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 133 | public BgpVersion getVersion() { |
| 134 | return BgpVersion.BGP_4; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 138 | public BgpType getType() { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 139 | return MSG_TYPE; |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public short getHoldTime() { |
| 144 | return this.holdTime; |
| 145 | } |
| 146 | |
| 147 | @Override |
Shashikanth VH | 09792f0 | 2016-05-10 16:59:57 +0530 | [diff] [blame] | 148 | public long getAsNumber() { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 149 | return this.asNumber; |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public int getBgpId() { |
| 154 | return this.bgpId; |
| 155 | } |
| 156 | |
| 157 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 158 | public LinkedList<BgpValueType> getCapabilityTlv() { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 159 | return this.capabilityTlv; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Reader class for reading BGP open message from channel buffer. |
| 164 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 165 | public static class Reader implements BgpMessageReader<BgpOpenMsg> { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 166 | |
| 167 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 168 | public BgpOpenMsg readFrom(ChannelBuffer cb, BgpHeader bgpHeader) throws BgpParseException { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 169 | |
| 170 | byte version; |
| 171 | short holdTime; |
Shashikanth VH | 09792f0 | 2016-05-10 16:59:57 +0530 | [diff] [blame] | 172 | long asNumber; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 173 | int bgpId; |
| 174 | byte optParaLen = 0; |
| 175 | byte optParaType; |
| 176 | byte capParaLen = 0; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 177 | LinkedList<BgpValueType> capabilityTlv = new LinkedList<>(); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 178 | |
| 179 | if (cb.readableBytes() < OPEN_MSG_MINIMUM_LENGTH) { |
| 180 | log.error("[readFrom] Invalid length: Packet size is less than the minimum length "); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 181 | Validation.validateLen(BgpErrorType.OPEN_MESSAGE_ERROR, BgpErrorType.BAD_MESSAGE_LENGTH, |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 182 | cb.readableBytes()); |
| 183 | } |
| 184 | |
| 185 | // Read version |
| 186 | version = cb.readByte(); |
| 187 | if (version != PACKET_VERSION) { |
| 188 | log.error("[readFrom] Invalid version: " + version); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 189 | throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, |
| 190 | BgpErrorType.UNSUPPORTED_VERSION_NUMBER, null); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | // Read AS number |
Shashikanth VH | 09792f0 | 2016-05-10 16:59:57 +0530 | [diff] [blame] | 194 | asNumber = cb.getUnsignedShort(cb.readerIndex()); |
| 195 | cb.readShort(); |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 196 | log.debug("AS number read"); |
| 197 | |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 198 | // Read Hold timer |
| 199 | holdTime = cb.readShort(); |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 200 | log.debug("Holding time read"); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 201 | |
| 202 | // Read BGP Identifier |
| 203 | bgpId = cb.readInt(); |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 204 | log.debug("BGP identifier read"); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 205 | |
| 206 | // Read optional parameter length |
| 207 | optParaLen = cb.readByte(); |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 208 | log.debug("OPtional parameter length read"); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 209 | |
| 210 | // Read Capabilities if optional parameter length is greater than 0 |
| 211 | if (optParaLen != 0) { |
| 212 | // Read optional parameter type |
| 213 | optParaType = cb.readByte(); |
| 214 | |
| 215 | // Read optional parameter length |
| 216 | capParaLen = cb.readByte(); |
| 217 | |
| 218 | if (cb.readableBytes() < capParaLen) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 219 | throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, (byte) 0, null); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | ChannelBuffer capaCb = cb.readBytes(capParaLen); |
| 223 | |
| 224 | // Parse capabilities only if optional parameter type is 2 |
| 225 | if ((optParaType == OPT_PARA_TYPE_CAPABILITY) && (capParaLen != 0)) { |
| 226 | capabilityTlv = parseCapabilityTlv(capaCb); |
| 227 | } else { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 228 | throw new BgpParseException(BgpErrorType.OPEN_MESSAGE_ERROR, |
| 229 | BgpErrorType.UNSUPPORTED_OPTIONAL_PARAMETER, null); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 230 | } |
| 231 | } |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 232 | return new BgpOpenMsgVer4(bgpHeader, version, asNumber, holdTime, bgpId, capabilityTlv); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Parsing capabilities. |
| 238 | * |
| 239 | * @param cb of type channel buffer |
| 240 | * @return capabilityTlv of open message |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 241 | * @throws BgpParseException while parsing capabilities |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 242 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 243 | protected static LinkedList<BgpValueType> parseCapabilityTlv(ChannelBuffer cb) throws BgpParseException { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 244 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 245 | LinkedList<BgpValueType> capabilityTlv = new LinkedList<>(); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 246 | |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 247 | while (cb.readableBytes() > 0) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 248 | BgpValueType tlv; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 249 | short type = cb.readByte(); |
| 250 | short length = cb.readByte(); |
| 251 | |
| 252 | switch (type) { |
| 253 | case FourOctetAsNumCapabilityTlv.TYPE: |
| 254 | log.debug("FourOctetAsNumCapabilityTlv"); |
| 255 | if (FourOctetAsNumCapabilityTlv.LENGTH != length) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 256 | throw new BgpParseException("Invalid length received for FourOctetAsNumCapabilityTlv."); |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 257 | } |
| 258 | if (length > cb.readableBytes()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 259 | throw new BgpParseException("Four octet as num tlv length" |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 260 | + " is more than readableBytes."); |
| 261 | } |
| 262 | int as4Num = cb.readInt(); |
| 263 | tlv = new FourOctetAsNumCapabilityTlv(as4Num); |
| 264 | break; |
Shashikanth VH | b58cfd5 | 2016-04-21 16:45:50 +0530 | [diff] [blame] | 265 | case RpdCapabilityTlv.TYPE: |
| 266 | log.debug("RpdCapability"); |
| 267 | if (RpdCapabilityTlv.LENGTH != length) { |
| 268 | throw new BgpParseException("Invalid length received for RpdCapability."); |
| 269 | } |
| 270 | if (length > cb.readableBytes()) { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 271 | throw new BgpParseException("Four octet as num TLV length" |
Shashikanth VH | b58cfd5 | 2016-04-21 16:45:50 +0530 | [diff] [blame] | 272 | + " is more than readableBytes."); |
| 273 | } |
| 274 | short rpdAfi = cb.readShort(); |
| 275 | byte rpdAsafi = cb.readByte(); |
| 276 | byte sendReceive = cb.readByte(); |
| 277 | tlv = new RpdCapabilityTlv(sendReceive); |
| 278 | break; |
| 279 | |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 280 | case MultiProtocolExtnCapabilityTlv.TYPE: |
| 281 | log.debug("MultiProtocolExtnCapabilityTlv"); |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 282 | |
Shashikanth VH | b58cfd5 | 2016-04-21 16:45:50 +0530 | [diff] [blame] | 283 | if (MultiProtocolExtnCapabilityTlv.LENGTH != length) { |
| 284 | throw new BgpParseException("Invalid length received for MultiProtocolExtnCapabilityTlv."); |
| 285 | } |
| 286 | |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 287 | if (length > cb.readableBytes()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 288 | throw new BgpParseException("BGP LS tlv length is more than readableBytes."); |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 289 | } |
| 290 | short afi = cb.readShort(); |
| 291 | byte res = cb.readByte(); |
| 292 | byte safi = cb.readByte(); |
Shashikanth VH | b58cfd5 | 2016-04-21 16:45:50 +0530 | [diff] [blame] | 293 | tlv = new MultiProtocolExtnCapabilityTlv(afi, res, safi); |
| 294 | |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 295 | break; |
| 296 | default: |
| 297 | log.debug("Warning: Unsupported TLV: " + type); |
| 298 | cb.skipBytes(length); |
| 299 | continue; |
| 300 | } |
| 301 | capabilityTlv.add(tlv); |
| 302 | } |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 303 | return capabilityTlv; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Builder class for BGP open message. |
| 308 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 309 | static class Builder implements BgpOpenMsg.Builder { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 310 | |
| 311 | private boolean isHeaderSet = false; |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 312 | private BgpHeader bgpMsgHeader; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 313 | private boolean isHoldTimeSet = false; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 314 | private short holdTime; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 315 | private boolean isAsNumSet = false; |
| 316 | private short asNumber; |
| 317 | private boolean isBgpIdSet = false; |
| 318 | private int bgpId; |
Shashikanth VH | f04ab49 | 2016-02-10 11:35:11 +0530 | [diff] [blame] | 319 | private boolean isIpV4UnicastCapabilityTlvSet = true; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 320 | private boolean isLargeAsCapabilityTlvSet = false; |
| 321 | private boolean isLsCapabilityTlvSet = false; |
Shashikanth VH | 44967ec | 2016-02-04 19:46:16 +0530 | [diff] [blame] | 322 | private boolean isFlowSpecCapabilityTlvSet = false; |
| 323 | private boolean isVpnFlowSpecCapabilityTlvSet = false; |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 324 | private boolean isFlowSpecRpdCapabilityTlvSet = false; |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame^] | 325 | private boolean isEvpnCapabilityTlvSet = false; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 326 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 327 | LinkedList<BgpValueType> capabilityTlv = new LinkedList<>(); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 328 | |
| 329 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 330 | public BgpOpenMsg build() throws BgpParseException { |
| 331 | BgpHeader bgpMsgHeader = this.isHeaderSet ? this.bgpMsgHeader : DEFAULT_OPEN_HEADER; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 332 | short holdTime = this.isHoldTimeSet ? this.holdTime : DEFAULT_HOLD_TIME; |
| 333 | |
| 334 | if (!this.isAsNumSet) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 335 | throw new BgpParseException("BGP AS number is not set (mandatory)"); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | if (!this.isBgpIdSet) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 339 | throw new BgpParseException("BGPID is not set (mandatory)"); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 340 | } |
| 341 | |
Shashikanth VH | f04ab49 | 2016-02-10 11:35:11 +0530 | [diff] [blame] | 342 | if (this.isIpV4UnicastCapabilityTlvSet) { |
| 343 | BgpValueType tlv; |
| 344 | tlv = new MultiProtocolExtnCapabilityTlv((short) Constants.AFI_IPV4_UNICAST, RES, |
| 345 | (byte) Constants.SAFI_IPV4_UNICAST); |
| 346 | this.capabilityTlv.add(tlv); |
| 347 | } |
| 348 | |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 349 | if (this.isLargeAsCapabilityTlvSet) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 350 | BgpValueType tlv; |
Vidyashree Rama | afbbff9 | 2015-11-02 14:45:17 +0530 | [diff] [blame] | 351 | int value = this.asNumber; |
| 352 | tlv = new FourOctetAsNumCapabilityTlv(value); |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 353 | this.capabilityTlv.add(tlv); |
| 354 | } |
| 355 | |
| 356 | if (this.isLsCapabilityTlvSet) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 357 | BgpValueType tlv; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 358 | tlv = new MultiProtocolExtnCapabilityTlv(AFI, RES, SAFI); |
| 359 | this.capabilityTlv.add(tlv); |
| 360 | } |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 361 | |
Shashikanth VH | 44967ec | 2016-02-04 19:46:16 +0530 | [diff] [blame] | 362 | if (this.isFlowSpecCapabilityTlvSet) { |
| 363 | BgpValueType tlv; |
| 364 | tlv = new MultiProtocolExtnCapabilityTlv(Constants.AFI_FLOWSPEC_VALUE, |
| 365 | RES, Constants.SAFI_FLOWSPEC_VALUE); |
| 366 | this.capabilityTlv.add(tlv); |
| 367 | } |
| 368 | |
| 369 | if (this.isVpnFlowSpecCapabilityTlvSet) { |
| 370 | BgpValueType tlv; |
| 371 | tlv = new MultiProtocolExtnCapabilityTlv(Constants.AFI_FLOWSPEC_VALUE, |
| 372 | RES, Constants.VPN_SAFI_FLOWSPEC_VALUE); |
| 373 | this.capabilityTlv.add(tlv); |
| 374 | } |
| 375 | |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 376 | if (this.isFlowSpecRpdCapabilityTlvSet) { |
| 377 | BgpValueType tlv; |
Shashikanth VH | b58cfd5 | 2016-04-21 16:45:50 +0530 | [diff] [blame] | 378 | tlv = new RpdCapabilityTlv(Constants.RPD_CAPABILITY_SEND_VALUE); |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 379 | this.capabilityTlv.add(tlv); |
| 380 | } |
| 381 | |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame^] | 382 | if (this.isEvpnCapabilityTlvSet) { |
| 383 | BgpValueType tlv; |
| 384 | tlv = new MultiProtocolExtnCapabilityTlv(Constants.AFI_EVPN_VALUE, |
| 385 | RES, Constants.SAFI_EVPN_VALUE); |
| 386 | this.capabilityTlv.add(tlv); |
| 387 | } |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 388 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 389 | return new BgpOpenMsgVer4(bgpMsgHeader, PACKET_VERSION, this.asNumber, holdTime, this.bgpId, |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 390 | this.capabilityTlv); |
| 391 | } |
| 392 | |
| 393 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 394 | public Builder setHeader(BgpHeader bgpMsgHeader) { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 395 | this.bgpMsgHeader = bgpMsgHeader; |
| 396 | return this; |
| 397 | } |
| 398 | |
| 399 | @Override |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 400 | public Builder setHoldTime(short holdTime) { |
| 401 | this.holdTime = holdTime; |
| 402 | this.isHoldTimeSet = true; |
| 403 | return this; |
| 404 | } |
| 405 | |
| 406 | @Override |
| 407 | public Builder setAsNumber(short asNumber) { |
| 408 | this.asNumber = asNumber; |
| 409 | this.isAsNumSet = true; |
| 410 | return this; |
| 411 | } |
| 412 | |
| 413 | @Override |
| 414 | public Builder setBgpId(int bgpId) { |
| 415 | this.bgpId = bgpId; |
| 416 | this.isBgpIdSet = true; |
| 417 | return this; |
| 418 | } |
| 419 | |
| 420 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 421 | public Builder setCapabilityTlv(LinkedList<BgpValueType> capabilityTlv) { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 422 | this.capabilityTlv = capabilityTlv; |
| 423 | return this; |
| 424 | } |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 425 | |
| 426 | @Override |
| 427 | public Builder setLargeAsCapabilityTlv(boolean isLargeAsCapabilitySet) { |
| 428 | this.isLargeAsCapabilityTlvSet = isLargeAsCapabilitySet; |
| 429 | return this; |
| 430 | } |
| 431 | |
| 432 | @Override |
| 433 | public Builder setLsCapabilityTlv(boolean isLsCapabilitySet) { |
| 434 | this.isLsCapabilityTlvSet = isLsCapabilitySet; |
| 435 | return this; |
| 436 | } |
Shashikanth VH | 44967ec | 2016-02-04 19:46:16 +0530 | [diff] [blame] | 437 | |
| 438 | @Override |
| 439 | public Builder setFlowSpecCapabilityTlv(boolean isFlowSpecCapabilitySet) { |
| 440 | this.isFlowSpecCapabilityTlvSet = isFlowSpecCapabilitySet; |
| 441 | return this; |
| 442 | } |
| 443 | |
| 444 | @Override |
| 445 | public Builder setVpnFlowSpecCapabilityTlv(boolean isVpnFlowSpecCapabilitySet) { |
| 446 | this.isVpnFlowSpecCapabilityTlvSet = isVpnFlowSpecCapabilitySet; |
| 447 | return this; |
| 448 | } |
Shashikanth VH | b650bfa | 2016-04-18 12:54:03 +0530 | [diff] [blame] | 449 | |
| 450 | @Override |
| 451 | public Builder setFlowSpecRpdCapabilityTlv(boolean isFlowSpecRpdCapabilityTlvSet) { |
| 452 | this.isFlowSpecRpdCapabilityTlvSet = isFlowSpecRpdCapabilityTlvSet; |
| 453 | return this; |
| 454 | } |
Mohammad Shahid | 30fedc5 | 2017-08-09 11:49:40 +0530 | [diff] [blame^] | 455 | |
| 456 | @Override |
| 457 | public Builder setEvpnCapabilityTlv(boolean isEvpnCapabilitySet) { |
| 458 | this.isEvpnCapabilityTlvSet = isEvpnCapabilitySet; |
| 459 | return this; |
| 460 | } |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | @Override |
| 464 | public void writeTo(ChannelBuffer cb) { |
| 465 | try { |
| 466 | WRITER.write(cb, this); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 467 | } catch (BgpParseException e) { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 468 | log.debug("[writeTo] Error: " + e.toString()); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | public static final Writer WRITER = new Writer(); |
| 473 | |
| 474 | /** |
| 475 | * Writer class for writing BGP open message to channel buffer. |
| 476 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 477 | public static class Writer implements BgpMessageWriter<BgpOpenMsgVer4> { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 478 | |
| 479 | @Override |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 480 | public void write(ChannelBuffer cb, BgpOpenMsgVer4 message) throws BgpParseException { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 481 | |
| 482 | int optParaLen = 0; |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 483 | int as4num = 0; |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 484 | |
| 485 | int startIndex = cb.writerIndex(); |
| 486 | |
| 487 | // write common header and get msg length index |
| 488 | int msgLenIndex = message.bgpMsgHeader.write(cb); |
| 489 | |
| 490 | if (msgLenIndex <= 0) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 491 | throw new BgpParseException("Unable to write message header."); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | // write version in 1-octet |
| 495 | cb.writeByte(message.version); |
| 496 | |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 497 | // get as4num if LS Capability is set |
| 498 | if (message.isLargeAsCapabilitySet) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 499 | LinkedList<BgpValueType> capabilityTlv = message |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 500 | .getCapabilityTlv(); |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 501 | ListIterator<BgpValueType> listIterator = capabilityTlv |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 502 | .listIterator(); |
| 503 | |
| 504 | while (listIterator.hasNext()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 505 | BgpValueType tlv = listIterator.next(); |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 506 | if (tlv.getType() == FOUR_OCTET_AS_NUM_CAPA_TYPE) { |
| 507 | as4num = ((FourOctetAsNumCapabilityTlv) tlv).getInt(); |
| 508 | break; |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | if ((message.isLargeAsCapabilitySet) && (as4num > 65535)) { |
| 514 | // write As number as AS_TRANS |
| 515 | cb.writeShort(AS_TRANS); |
| 516 | } else { |
| 517 | // write AS number in next 2-octet |
Shashikanth VH | 09792f0 | 2016-05-10 16:59:57 +0530 | [diff] [blame] | 518 | cb.writeShort((short) message.asNumber); |
Vidyashree Rama | f49b22c | 2015-10-13 15:30:24 +0530 | [diff] [blame] | 519 | } |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 520 | |
| 521 | // write HoldTime in next 2-octet |
| 522 | cb.writeShort(message.holdTime); |
| 523 | |
| 524 | // write BGP Identifier in next 4-octet |
| 525 | cb.writeInt(message.bgpId); |
| 526 | |
| 527 | // store the index of Optional parameter length |
| 528 | int optParaLenIndex = cb.writerIndex(); |
| 529 | |
| 530 | // set optional parameter length as 0 |
| 531 | cb.writeByte(0); |
| 532 | |
| 533 | // Pack capability TLV |
| 534 | optParaLen = message.packCapabilityTlv(cb, message); |
| 535 | |
| 536 | if (optParaLen != 0) { |
| 537 | // Update optional parameter length |
| 538 | cb.setByte(optParaLenIndex, (byte) (optParaLen + 2)); //+2 for optional parameter type. |
| 539 | } |
| 540 | |
| 541 | // write OPEN Object Length |
| 542 | int length = cb.writerIndex() - startIndex; |
| 543 | cb.setShort(msgLenIndex, (short) length); |
| 544 | message.bgpMsgHeader.setLength((short) length); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * returns length of capability tlvs. |
| 550 | * |
| 551 | * @param cb of type channel buffer |
| 552 | * @param message of type BGPOpenMsgVer4 |
| 553 | * @return capParaLen of open message |
| 554 | */ |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 555 | protected int packCapabilityTlv(ChannelBuffer cb, BgpOpenMsgVer4 message) { |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 556 | int startIndex = cb.writerIndex(); |
| 557 | int capParaLen = 0; |
| 558 | int capParaLenIndex = 0; |
| 559 | |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 560 | LinkedList<BgpValueType> capabilityTlv = message.capabilityTlv; |
| 561 | ListIterator<BgpValueType> listIterator = capabilityTlv.listIterator(); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 562 | |
| 563 | if (listIterator.hasNext()) { |
| 564 | // Set optional parameter type as 2 |
| 565 | cb.writeByte(OPT_PARA_TYPE_CAPABILITY); |
| 566 | |
| 567 | // Store the index of capability parameter length and update length at the end |
| 568 | capParaLenIndex = cb.writerIndex(); |
| 569 | |
| 570 | // Set capability parameter length as 0 |
| 571 | cb.writeByte(0); |
| 572 | |
| 573 | // Update the startIndex to know the length of capability tlv |
| 574 | startIndex = cb.writerIndex(); |
| 575 | } |
| 576 | |
| 577 | while (listIterator.hasNext()) { |
Shashikanth VH | 5dd8dbe | 2015-11-26 13:22:18 +0530 | [diff] [blame] | 578 | BgpValueType tlv = listIterator.next(); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 579 | if (tlv == null) { |
mohamedrahil | 00f6f26 | 2016-11-24 20:20:41 +0530 | [diff] [blame] | 580 | log.debug("Warning: TLV is null from CapabilityTlv list"); |
Vidyashree Rama | 6d4f84e | 2015-09-21 20:01:02 +0530 | [diff] [blame] | 581 | continue; |
| 582 | } |
| 583 | tlv.write(cb); |
| 584 | } |
| 585 | |
| 586 | capParaLen = cb.writerIndex() - startIndex; |
| 587 | |
| 588 | if (capParaLen != 0) { |
| 589 | // Update capability parameter length |
| 590 | cb.setByte(capParaLenIndex, (byte) capParaLen); |
| 591 | } |
| 592 | return capParaLen; |
| 593 | } |
| 594 | |
| 595 | @Override |
| 596 | public String toString() { |
| 597 | return MoreObjects.toStringHelper(getClass()) |
| 598 | .add("bgpMsgHeader", bgpMsgHeader) |
| 599 | .add("version", version) |
| 600 | .add("holdTime", holdTime) |
| 601 | .add("asNumber", asNumber) |
| 602 | .add("bgpId", bgpId) |
| 603 | .add("capabilityTlv", capabilityTlv) |
| 604 | .toString(); |
| 605 | } |
| 606 | } |