Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [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 | */ |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 16 | package org.onosproject.routing.bgp; |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 17 | |
| 18 | import org.jboss.netty.buffer.ChannelBuffer; |
| 19 | import org.jboss.netty.buffer.ChannelBuffers; |
| 20 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 21 | import org.onlab.packet.Ip4Address; |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 22 | import org.slf4j.Logger; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
| 25 | /** |
| 26 | * A class for handling BGP OPEN messages. |
| 27 | */ |
| 28 | final class BgpOpen { |
| 29 | private static final Logger log = LoggerFactory.getLogger(BgpOpen.class); |
| 30 | |
| 31 | /** |
| 32 | * Default constructor. |
| 33 | * <p> |
| 34 | * The constructor is private to prevent creating an instance of |
| 35 | * this utility class. |
| 36 | */ |
| 37 | private BgpOpen() { |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Processes BGP OPEN message. |
| 42 | * |
| 43 | * @param bgpSession the BGP Session to use |
| 44 | * @param ctx the Channel Handler Context |
| 45 | * @param message the message to process |
| 46 | */ |
| 47 | static void processBgpOpen(BgpSession bgpSession, |
| 48 | ChannelHandlerContext ctx, |
| 49 | ChannelBuffer message) { |
| 50 | int minLength = |
| 51 | BgpConstants.BGP_OPEN_MIN_LENGTH - BgpConstants.BGP_HEADER_LENGTH; |
| 52 | if (message.readableBytes() < minLength) { |
| 53 | log.debug("BGP RX OPEN Error from {}: " + |
| 54 | "Message length {} too short. Must be at least {}", |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 55 | bgpSession.remoteInfo().address(), |
| 56 | message.readableBytes(), minLength); |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 57 | // |
| 58 | // ERROR: Bad Message Length |
| 59 | // |
| 60 | // Send NOTIFICATION and close the connection |
| 61 | ChannelBuffer txMessage = |
| 62 | BgpNotification.prepareBgpNotificationBadMessageLength( |
| 63 | message.readableBytes() + BgpConstants.BGP_HEADER_LENGTH); |
| 64 | ctx.getChannel().write(txMessage); |
| 65 | bgpSession.closeSession(ctx); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | // |
| 70 | // Parse the OPEN message |
| 71 | // |
| 72 | // Remote BGP version |
| 73 | int remoteBgpVersion = message.readUnsignedByte(); |
| 74 | if (remoteBgpVersion != BgpConstants.BGP_VERSION) { |
| 75 | log.debug("BGP RX OPEN Error from {}: " + |
| 76 | "Unsupported BGP version {}. Should be {}", |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 77 | bgpSession.remoteInfo().address(), remoteBgpVersion, |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 78 | BgpConstants.BGP_VERSION); |
| 79 | // |
| 80 | // ERROR: Unsupported Version Number |
| 81 | // |
| 82 | // Send NOTIFICATION and close the connection |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 83 | int errorCode = BgpConstants.Notifications.OpenMessageError.ERROR_CODE; |
| 84 | int errorSubcode = BgpConstants.Notifications.OpenMessageError.UNSUPPORTED_VERSION_NUMBER; |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 85 | ChannelBuffer data = ChannelBuffers.buffer(2); |
| 86 | data.writeShort(BgpConstants.BGP_VERSION); |
| 87 | ChannelBuffer txMessage = |
| 88 | BgpNotification.prepareBgpNotification(errorCode, errorSubcode, |
| 89 | data); |
| 90 | ctx.getChannel().write(txMessage); |
| 91 | bgpSession.closeSession(ctx); |
| 92 | return; |
| 93 | } |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 94 | bgpSession.remoteInfo().setBgpVersion(remoteBgpVersion); |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 95 | |
| 96 | // Remote AS number |
| 97 | long remoteAs = message.readUnsignedShort(); |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 98 | bgpSession.remoteInfo().setAsNumber(remoteAs); |
| 99 | // |
| 100 | // NOTE: Currently, the local AS number is always set to the remote AS. |
| 101 | // This is done, because the peer setup is always iBGP. |
| 102 | // In the future, the local AS number should be configured as part |
| 103 | // of an explicit BGP peering configuration. |
| 104 | // |
| 105 | bgpSession.localInfo().setAsNumber(remoteAs); |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 106 | |
| 107 | // Remote Hold Time |
| 108 | long remoteHoldtime = message.readUnsignedShort(); |
| 109 | if ((remoteHoldtime != 0) && |
| 110 | (remoteHoldtime < BgpConstants.BGP_KEEPALIVE_MIN_HOLDTIME)) { |
| 111 | log.debug("BGP RX OPEN Error from {}: " + |
| 112 | "Unacceptable Hold Time field {}. " + |
| 113 | "Should be 0 or at least {}", |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 114 | bgpSession.remoteInfo().address(), remoteHoldtime, |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 115 | BgpConstants.BGP_KEEPALIVE_MIN_HOLDTIME); |
| 116 | // |
| 117 | // ERROR: Unacceptable Hold Time |
| 118 | // |
| 119 | // Send NOTIFICATION and close the connection |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 120 | int errorCode = BgpConstants.Notifications.OpenMessageError.ERROR_CODE; |
| 121 | int errorSubcode = BgpConstants.Notifications.OpenMessageError.UNACCEPTABLE_HOLD_TIME; |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 122 | ChannelBuffer txMessage = |
| 123 | BgpNotification.prepareBgpNotification(errorCode, errorSubcode, |
| 124 | null); |
| 125 | ctx.getChannel().write(txMessage); |
| 126 | bgpSession.closeSession(ctx); |
| 127 | return; |
| 128 | } |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 129 | bgpSession.remoteInfo().setHoldtime(remoteHoldtime); |
| 130 | // |
| 131 | // NOTE: Currently. the local BGP Holdtime is always set to the remote |
| 132 | // BGP holdtime. |
| 133 | // In the future, the local BGP Holdtime should be configured as part |
| 134 | // of an explicit BGP peering configuration. |
| 135 | // |
| 136 | bgpSession.localInfo().setHoldtime(remoteHoldtime); |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 137 | |
| 138 | // Remote BGP Identifier |
| 139 | Ip4Address remoteBgpId = |
| 140 | Ip4Address.valueOf((int) message.readUnsignedInt()); |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 141 | bgpSession.remoteInfo().setBgpId(remoteBgpId); |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 142 | |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 143 | // Parse the Optional Parameters |
| 144 | try { |
| 145 | parseOptionalParameters(bgpSession, ctx, message); |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 146 | } catch (BgpMessage.BgpParseException e) { |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 147 | // ERROR: Error parsing optional parameters |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 148 | log.debug("BGP RX OPEN Error from {}: " + |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 149 | "Exception parsing Optional Parameters: {}", |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 150 | bgpSession.remoteInfo().address(), e); |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 151 | // |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 152 | // ERROR: Invalid Optional Parameters: Unspecific |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 153 | // |
| 154 | // Send NOTIFICATION and close the connection |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 155 | int errorCode = BgpConstants.Notifications.OpenMessageError.ERROR_CODE; |
| 156 | int errorSubcode = BgpConstants.Notifications.ERROR_SUBCODE_UNSPECIFIC; |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 157 | ChannelBuffer txMessage = |
| 158 | BgpNotification.prepareBgpNotification(errorCode, errorSubcode, |
| 159 | null); |
| 160 | ctx.getChannel().write(txMessage); |
| 161 | bgpSession.closeSession(ctx); |
| 162 | return; |
| 163 | } |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 164 | |
Pavlin Radoslavov | 4b5acae | 2015-01-28 17:09:45 -0800 | [diff] [blame] | 165 | // |
| 166 | // NOTE: Prepare the BGP OPEN message before the original local AS |
| 167 | // is overwritten by the 4-octet AS number |
| 168 | // |
| 169 | ChannelBuffer txOpenMessage = prepareBgpOpen(bgpSession.localInfo()); |
| 170 | |
| 171 | // |
| 172 | // Use the 4-octet AS number in lieu of the "My AS" field |
| 173 | // See RFC 6793, Section 4.1, second paragraph. |
| 174 | // |
| 175 | if (bgpSession.remoteInfo().as4OctetCapability()) { |
| 176 | long as4Number = bgpSession.remoteInfo().as4Number(); |
| 177 | bgpSession.remoteInfo().setAsNumber(as4Number); |
| 178 | bgpSession.localInfo().setAsNumber(as4Number); |
| 179 | } |
| 180 | |
| 181 | // |
| 182 | // Verify that the AS number is same for all other BGP Sessions |
| 183 | // NOTE: This check applies only for our use-case where all BGP |
| 184 | // sessions are iBGP. |
| 185 | // |
| 186 | for (BgpSession bs : bgpSession.getBgpSessionManager().getBgpSessions()) { |
| 187 | if ((bs.remoteInfo().asNumber() != 0) && |
| 188 | (bgpSession.remoteInfo().asNumber() != |
| 189 | bs.remoteInfo().asNumber())) { |
| 190 | log.debug("BGP RX OPEN Error from {}: Bad Peer AS {}. " + |
| 191 | "Expected {}", |
| 192 | bgpSession.remoteInfo().address(), |
| 193 | bgpSession.remoteInfo().asNumber(), |
| 194 | bs.remoteInfo().asNumber()); |
| 195 | // |
| 196 | // ERROR: Bad Peer AS |
| 197 | // |
| 198 | // Send NOTIFICATION and close the connection |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 199 | int errorCode = BgpConstants.Notifications.OpenMessageError.ERROR_CODE; |
| 200 | int errorSubcode = BgpConstants.Notifications.OpenMessageError.BAD_PEER_AS; |
Pavlin Radoslavov | 4b5acae | 2015-01-28 17:09:45 -0800 | [diff] [blame] | 201 | ChannelBuffer txMessage = |
| 202 | BgpNotification.prepareBgpNotification(errorCode, |
| 203 | errorSubcode, null); |
| 204 | ctx.getChannel().write(txMessage); |
| 205 | bgpSession.closeSession(ctx); |
| 206 | return; |
| 207 | } |
| 208 | } |
| 209 | |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 210 | log.debug("BGP RX OPEN message from {}: " + |
| 211 | "BGPv{} AS {} BGP-ID {} Holdtime {}", |
Pavlin Radoslavov | 4b5acae | 2015-01-28 17:09:45 -0800 | [diff] [blame] | 212 | bgpSession.remoteInfo().address(), |
| 213 | bgpSession.remoteInfo().bgpVersion(), |
| 214 | bgpSession.remoteInfo().asNumber(), |
| 215 | bgpSession.remoteInfo().bgpId(), |
| 216 | bgpSession.remoteInfo().holdtime()); |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 217 | |
| 218 | // Send my OPEN followed by KEEPALIVE |
Pavlin Radoslavov | 4b5acae | 2015-01-28 17:09:45 -0800 | [diff] [blame] | 219 | ctx.getChannel().write(txOpenMessage); |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 220 | // |
Pavlin Radoslavov | 4b5acae | 2015-01-28 17:09:45 -0800 | [diff] [blame] | 221 | ChannelBuffer txMessage = BgpKeepalive.prepareBgpKeepalive(); |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 222 | ctx.getChannel().write(txMessage); |
| 223 | |
| 224 | // Start the KEEPALIVE timer |
| 225 | bgpSession.restartKeepaliveTimer(ctx); |
| 226 | |
| 227 | // Start the Session Timeout timer |
| 228 | bgpSession.restartSessionTimeoutTimer(ctx); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Prepares BGP OPEN message. |
| 233 | * |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 234 | * @param localInfo the BGP Session local information to use |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 235 | * @return the message to transmit (BGP header included) |
| 236 | */ |
Pavlin Radoslavov | 4b5acae | 2015-01-28 17:09:45 -0800 | [diff] [blame] | 237 | static ChannelBuffer prepareBgpOpen(BgpSessionInfo localInfo) { |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 238 | ChannelBuffer message = |
| 239 | ChannelBuffers.buffer(BgpConstants.BGP_MESSAGE_MAX_LENGTH); |
| 240 | |
| 241 | // |
| 242 | // Prepare the OPEN message payload |
| 243 | // |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 244 | message.writeByte(localInfo.bgpVersion()); |
| 245 | message.writeShort((int) localInfo.asNumber()); |
| 246 | message.writeShort((int) localInfo.holdtime()); |
| 247 | message.writeInt(localInfo.bgpId().toInt()); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 248 | |
| 249 | // Prepare the optional BGP Capabilities |
| 250 | ChannelBuffer capabilitiesMessage = |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 251 | prepareBgpOpenCapabilities(localInfo); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 252 | message.writeByte(capabilitiesMessage.readableBytes()); |
| 253 | message.writeBytes(capabilitiesMessage); |
| 254 | |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 255 | return BgpMessage.prepareBgpMessage(BgpConstants.BGP_TYPE_OPEN, |
| 256 | message); |
| 257 | } |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 258 | |
| 259 | /** |
| 260 | * Parses BGP OPEN Optional Parameters. |
| 261 | * |
| 262 | * @param bgpSession the BGP Session to use |
| 263 | * @param ctx the Channel Handler Context |
| 264 | * @param message the message to process |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 265 | * @throws BgpMessage.BgpParseException |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 266 | */ |
| 267 | private static void parseOptionalParameters(BgpSession bgpSession, |
| 268 | ChannelHandlerContext ctx, |
| 269 | ChannelBuffer message) |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 270 | throws BgpMessage.BgpParseException { |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 271 | |
| 272 | // |
| 273 | // Get and verify the Optional Parameters Length |
| 274 | // |
| 275 | int optParamLength = message.readUnsignedByte(); |
| 276 | if (optParamLength > message.readableBytes()) { |
| 277 | // ERROR: Invalid Optional Parameter Length |
| 278 | String errorMsg = "Invalid Optional Parameter Length field " + |
| 279 | optParamLength + ". Remaining Optional Parameters " + |
| 280 | message.readableBytes(); |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 281 | throw new BgpMessage.BgpParseException(errorMsg); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 282 | } |
| 283 | if (optParamLength == 0) { |
| 284 | return; // No Optional Parameters |
| 285 | } |
| 286 | |
| 287 | // |
| 288 | // Parse the Optional Parameters |
| 289 | // |
| 290 | int optParamEnd = message.readerIndex() + optParamLength; |
| 291 | while (message.readerIndex() < optParamEnd) { |
| 292 | int paramType = message.readUnsignedByte(); |
| 293 | if (message.readerIndex() >= optParamEnd) { |
| 294 | // ERROR: Malformed Optional Parameters |
| 295 | String errorMsg = "Malformed Optional Parameters"; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 296 | throw new BgpMessage.BgpParseException(errorMsg); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 297 | } |
| 298 | int paramLen = message.readUnsignedByte(); |
| 299 | if (message.readerIndex() + paramLen > optParamEnd) { |
| 300 | // ERROR: Malformed Optional Parameters |
| 301 | String errorMsg = "Malformed Optional Parameters"; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 302 | throw new BgpMessage.BgpParseException(errorMsg); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | // |
| 306 | // Extract the Optional Parameter Value based on the Parameter Type |
| 307 | // |
| 308 | switch (paramType) { |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 309 | case BgpConstants.Open.Capabilities.TYPE: |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 310 | // Optional Parameter Type: Capabilities |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 311 | if (paramLen < BgpConstants.Open.Capabilities.MIN_LENGTH) { |
Pingping Lin | 816c480 | 2015-11-10 15:28:54 -0800 | [diff] [blame] | 312 | // ERROR: Malformed Param Type |
| 313 | String errorMsg = "Malformed Capabilities Optional " |
| 314 | + "Parameter Type " + paramType; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 315 | throw new BgpMessage.BgpParseException(errorMsg); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 316 | } |
Pingping Lin | 816c480 | 2015-11-10 15:28:54 -0800 | [diff] [blame] | 317 | int paramEnd = message.readerIndex() + paramLen; |
| 318 | // Parse Capabilities |
| 319 | while (message.readerIndex() < paramEnd) { |
| 320 | if (paramEnd - message.readerIndex() < |
| 321 | BgpConstants.Open.Capabilities.MIN_LENGTH) { |
| 322 | String errorMsg = "Malformed Capabilities"; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 323 | throw new BgpMessage.BgpParseException(errorMsg); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 324 | } |
Pingping Lin | 816c480 | 2015-11-10 15:28:54 -0800 | [diff] [blame] | 325 | int capabCode = message.readUnsignedByte(); |
| 326 | int capabLen = message.readUnsignedByte(); |
| 327 | if (message.readerIndex() + capabLen > paramEnd) { |
| 328 | // ERROR: Malformed Capability |
| 329 | String errorMsg = "Malformed Capability instance with " |
| 330 | + "code " + capabCode; |
| 331 | throw new BgpMessage.BgpParseException(errorMsg); |
| 332 | } |
| 333 | |
| 334 | switch (capabCode) { |
| 335 | case BgpConstants.Open.Capabilities.MultiprotocolExtensions.CODE: |
| 336 | // Multiprotocol Extensions Capabilities (RFC 4760) |
| 337 | if (capabLen != BgpConstants.Open.Capabilities.MultiprotocolExtensions.LENGTH) { |
| 338 | // ERROR: Multiprotocol Extension Length Error |
| 339 | String errorMsg = "Multiprotocol Extension Length Error"; |
| 340 | throw new BgpMessage.BgpParseException(errorMsg); |
| 341 | } |
| 342 | // Decode the AFI (2 octets) and SAFI (1 octet) |
| 343 | int afi = message.readUnsignedShort(); |
| 344 | int reserved = message.readUnsignedByte(); |
| 345 | int safi = message.readUnsignedByte(); |
| 346 | log.debug("BGP RX OPEN Capability: AFI = {} SAFI = {}", |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 347 | afi, safi); |
Pingping Lin | 816c480 | 2015-11-10 15:28:54 -0800 | [diff] [blame] | 348 | // |
| 349 | // Setup the AFI/SAFI in the BgpSession |
| 350 | // |
| 351 | // NOTE: For now we just copy the remote AFI/SAFI setting |
| 352 | // to the local configuration. |
| 353 | // |
| 354 | if (afi == BgpConstants.Open.Capabilities.MultiprotocolExtensions.AFI_IPV4 && |
| 355 | safi == BgpConstants.Open.Capabilities.MultiprotocolExtensions.SAFI_UNICAST) { |
| 356 | bgpSession.remoteInfo().setIpv4Unicast(); |
| 357 | bgpSession.localInfo().setIpv4Unicast(); |
| 358 | } else if (afi == BgpConstants.Open.Capabilities.MultiprotocolExtensions.AFI_IPV4 && |
| 359 | safi == BgpConstants.Open.Capabilities.MultiprotocolExtensions.SAFI_MULTICAST) { |
| 360 | bgpSession.remoteInfo().setIpv4Multicast(); |
| 361 | bgpSession.localInfo().setIpv4Multicast(); |
| 362 | } else if (afi == BgpConstants.Open.Capabilities.MultiprotocolExtensions.AFI_IPV6 && |
| 363 | safi == BgpConstants.Open.Capabilities.MultiprotocolExtensions.SAFI_UNICAST) { |
| 364 | bgpSession.remoteInfo().setIpv6Unicast(); |
| 365 | bgpSession.localInfo().setIpv6Unicast(); |
| 366 | } else if (afi == BgpConstants.Open.Capabilities.MultiprotocolExtensions.AFI_IPV6 && |
| 367 | safi == BgpConstants.Open.Capabilities.MultiprotocolExtensions.SAFI_MULTICAST) { |
| 368 | bgpSession.remoteInfo().setIpv6Multicast(); |
| 369 | bgpSession.localInfo().setIpv6Multicast(); |
| 370 | } else { |
| 371 | log.debug("BGP RX OPEN Capability: Unknown AFI = {} SAFI = {}", |
| 372 | afi, safi); |
| 373 | } |
| 374 | break; |
| 375 | |
| 376 | case BgpConstants.Open.Capabilities.As4Octet.CODE: |
| 377 | // Support for 4-octet AS Number Capabilities (RFC 6793) |
| 378 | if (capabLen != BgpConstants.Open.Capabilities.As4Octet.LENGTH) { |
| 379 | // ERROR: 4-octet AS Number Capability Length Error |
| 380 | String errorMsg = "4-octet AS Number Capability Length Error"; |
| 381 | throw new BgpMessage.BgpParseException(errorMsg); |
| 382 | } |
| 383 | long as4Number = message.readUnsignedInt(); |
| 384 | |
| 385 | bgpSession.remoteInfo().setAs4OctetCapability(); |
| 386 | bgpSession.remoteInfo().setAs4Number(as4Number); |
| 387 | |
| 388 | // |
| 389 | // Copy remote 4-octet AS Number Capabilities and AS |
| 390 | // Number. This is a temporary setting until local AS |
| 391 | // number configuration is supported. |
| 392 | // |
| 393 | bgpSession.localInfo().setAs4OctetCapability(); |
| 394 | bgpSession.localInfo().setAs4Number(as4Number); |
| 395 | log.debug("BGP RX OPEN Capability: AS4 Number = {}", |
| 396 | as4Number); |
| 397 | break; |
| 398 | |
| 399 | default: |
| 400 | // Unknown Capability: ignore it |
| 401 | log.debug("BGP RX OPEN Capability Code = {} Length = {}", |
| 402 | capabCode, capabLen); |
| 403 | message.readBytes(capabLen); |
| 404 | break; |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 405 | } |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 406 | |
Kunihiro Ishiguro | 923d9d8 | 2014-12-21 15:47:28 +0900 | [diff] [blame] | 407 | |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 408 | } |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 409 | break; |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 410 | default: |
| 411 | // Unknown Parameter Type: ignore it |
| 412 | log.debug("BGP RX OPEN Parameter Type = {} Length = {}", |
| 413 | paramType, paramLen); |
| 414 | message.readBytes(paramLen); |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Prepares the Capabilities for the BGP OPEN message. |
| 422 | * |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 423 | * @param localInfo the BGP Session local information to use |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 424 | * @return the buffer with the BGP Capabilities to transmit |
| 425 | */ |
| 426 | private static ChannelBuffer prepareBgpOpenCapabilities( |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 427 | BgpSessionInfo localInfo) { |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 428 | ChannelBuffer message = |
| 429 | ChannelBuffers.buffer(BgpConstants.BGP_MESSAGE_MAX_LENGTH); |
| 430 | |
| 431 | // |
| 432 | // Write the Multiprotocol Extensions Capabilities |
| 433 | // |
| 434 | |
| 435 | // IPv4 unicast |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 436 | if (localInfo.ipv4Unicast()) { |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 437 | message.writeByte(BgpConstants.Open.Capabilities.TYPE); // Param type |
| 438 | message.writeByte(BgpConstants.Open.Capabilities.MIN_LENGTH + |
| 439 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.LENGTH); // Param len |
| 440 | message.writeByte( |
| 441 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.CODE); // Capab. code |
| 442 | message.writeByte( |
| 443 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.LENGTH); // Capab. len |
| 444 | message.writeShort( |
| 445 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.AFI_IPV4); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 446 | message.writeByte(0); // Reserved field |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 447 | message.writeByte( |
| 448 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.SAFI_UNICAST); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 449 | } |
| 450 | // IPv4 multicast |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 451 | if (localInfo.ipv4Multicast()) { |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 452 | message.writeByte(BgpConstants.Open.Capabilities.TYPE); // Param type |
| 453 | message.writeByte(BgpConstants.Open.Capabilities.MIN_LENGTH + |
| 454 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.LENGTH); // Param len |
| 455 | message.writeByte( |
| 456 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.CODE); // Capab. code |
| 457 | message.writeByte( |
| 458 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.LENGTH); // Capab. len |
| 459 | message.writeShort( |
| 460 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.AFI_IPV4); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 461 | message.writeByte(0); // Reserved field |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 462 | message.writeByte( |
| 463 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.SAFI_MULTICAST); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 464 | } |
| 465 | // IPv6 unicast |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 466 | if (localInfo.ipv6Unicast()) { |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 467 | message.writeByte(BgpConstants.Open.Capabilities.TYPE); // Param type |
| 468 | message.writeByte(BgpConstants.Open.Capabilities.MIN_LENGTH + |
| 469 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.LENGTH); // Param len |
| 470 | message.writeByte( |
| 471 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.CODE); // Capab. code |
| 472 | message.writeByte( |
| 473 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.LENGTH); // Capab. len |
| 474 | message.writeShort( |
| 475 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.AFI_IPV6); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 476 | message.writeByte(0); // Reserved field |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 477 | message.writeByte( |
| 478 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.SAFI_UNICAST); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 479 | } |
| 480 | // IPv6 multicast |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 481 | if (localInfo.ipv6Multicast()) { |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 482 | message.writeByte(BgpConstants.Open.Capabilities.TYPE); // Param type |
| 483 | message.writeByte(BgpConstants.Open.Capabilities.MIN_LENGTH + |
| 484 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.LENGTH); // Param len |
| 485 | message.writeByte( |
| 486 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.CODE); // Capab. code |
| 487 | message.writeByte( |
| 488 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.LENGTH); // Capab. len |
| 489 | message.writeShort( |
| 490 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.AFI_IPV6); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 491 | message.writeByte(0); // Reserved field |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 492 | message.writeByte( |
| 493 | BgpConstants.Open.Capabilities.MultiprotocolExtensions.SAFI_MULTICAST); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Kunihiro Ishiguro | 923d9d8 | 2014-12-21 15:47:28 +0900 | [diff] [blame] | 496 | // 4 octet AS path capability |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 497 | if (localInfo.as4OctetCapability()) { |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 498 | message.writeByte(BgpConstants.Open.Capabilities.TYPE); // Param type |
| 499 | message.writeByte(BgpConstants.Open.Capabilities.MIN_LENGTH + |
| 500 | BgpConstants.Open.Capabilities.As4Octet.LENGTH); // Param len |
| 501 | message.writeByte(BgpConstants.Open.Capabilities.As4Octet.CODE); // Capab. code |
| 502 | message.writeByte(BgpConstants.Open.Capabilities.As4Octet.LENGTH); // Capab. len |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 503 | message.writeInt((int) localInfo.as4Number()); |
Kunihiro Ishiguro | 923d9d8 | 2014-12-21 15:47:28 +0900 | [diff] [blame] | 504 | } |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 505 | return message; |
| 506 | } |
Pavlin Radoslavov | 80f3e18 | 2014-12-15 10:46:18 -0800 | [diff] [blame] | 507 | } |