paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* BGP open message handling |
| 2 | Copyright (C) 1998, 1999 Kunihiro Ishiguro |
| 3 | |
| 4 | This file is part of GNU Zebra. |
| 5 | |
| 6 | GNU Zebra is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by the |
| 8 | Free Software Foundation; either version 2, or (at your option) any |
| 9 | later version. |
| 10 | |
| 11 | GNU Zebra is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 18 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | 02111-1307, USA. */ |
| 20 | |
| 21 | #include <zebra.h> |
| 22 | |
| 23 | #include "linklist.h" |
| 24 | #include "prefix.h" |
| 25 | #include "stream.h" |
| 26 | #include "thread.h" |
| 27 | #include "log.h" |
| 28 | #include "command.h" |
| 29 | |
| 30 | #include "bgpd/bgpd.h" |
| 31 | #include "bgpd/bgp_attr.h" |
| 32 | #include "bgpd/bgp_debug.h" |
| 33 | #include "bgpd/bgp_fsm.h" |
| 34 | #include "bgpd/bgp_packet.h" |
| 35 | #include "bgpd/bgp_open.h" |
| 36 | |
| 37 | /* BGP-4 Multiprotocol Extentions lead us to the complex world. We can |
| 38 | negotiate remote peer supports extentions or not. But if |
| 39 | remote-peer doesn't supports negotiation process itself. We would |
| 40 | like to do manual configuration. |
| 41 | |
| 42 | So there is many configurable point. First of all we want set each |
| 43 | peer whether we send capability negotiation to the peer or not. |
| 44 | Next, if we send capability to the peer we want to set my capabilty |
| 45 | inforation at each peer. */ |
| 46 | |
| 47 | void |
| 48 | bgp_capability_vty_out (struct vty *vty, struct peer *peer) |
| 49 | { |
| 50 | u_char *pnt; |
| 51 | u_char *end; |
| 52 | struct capability cap; |
| 53 | |
| 54 | pnt = peer->notify.data; |
| 55 | end = pnt + peer->notify.length; |
| 56 | |
| 57 | while (pnt < end) |
| 58 | { |
| 59 | memcpy(&cap, pnt, sizeof(struct capability)); |
| 60 | |
| 61 | if (pnt + 2 > end) |
| 62 | return; |
| 63 | if (pnt + (cap.length + 2) > end) |
| 64 | return; |
| 65 | |
| 66 | if (cap.code == CAPABILITY_CODE_MP) |
| 67 | { |
| 68 | vty_out (vty, " Capability error for: Multi protocol "); |
| 69 | |
| 70 | switch (ntohs (cap.mpc.afi)) |
| 71 | { |
| 72 | case AFI_IP: |
| 73 | vty_out (vty, "AFI IPv4, "); |
| 74 | break; |
| 75 | case AFI_IP6: |
| 76 | vty_out (vty, "AFI IPv6, "); |
| 77 | break; |
| 78 | default: |
| 79 | vty_out (vty, "AFI Unknown %d, ", ntohs (cap.mpc.afi)); |
| 80 | break; |
| 81 | } |
| 82 | switch (cap.mpc.safi) |
| 83 | { |
| 84 | case SAFI_UNICAST: |
| 85 | vty_out (vty, "SAFI Unicast"); |
| 86 | break; |
| 87 | case SAFI_MULTICAST: |
| 88 | vty_out (vty, "SAFI Multicast"); |
| 89 | break; |
| 90 | case SAFI_UNICAST_MULTICAST: |
| 91 | vty_out (vty, "SAFI Unicast Multicast"); |
| 92 | break; |
| 93 | case BGP_SAFI_VPNV4: |
| 94 | vty_out (vty, "SAFI MPLS-VPN"); |
| 95 | break; |
| 96 | default: |
| 97 | vty_out (vty, "SAFI Unknown %d ", cap.mpc.safi); |
| 98 | break; |
| 99 | } |
| 100 | vty_out (vty, "%s", VTY_NEWLINE); |
| 101 | } |
| 102 | else if (cap.code >= 128) |
| 103 | vty_out (vty, " Capability error: vendor specific capability code %d", |
| 104 | cap.code); |
| 105 | else |
| 106 | vty_out (vty, " Capability error: unknown capability code %d", |
| 107 | cap.code); |
| 108 | |
| 109 | pnt += cap.length + 2; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* Set negotiated capability value. */ |
| 114 | int |
| 115 | bgp_capability_mp (struct peer *peer, struct capability *cap) |
| 116 | { |
| 117 | if (ntohs (cap->mpc.afi) == AFI_IP) |
| 118 | { |
| 119 | if (cap->mpc.safi == SAFI_UNICAST) |
| 120 | { |
| 121 | peer->afc_recv[AFI_IP][SAFI_UNICAST] = 1; |
| 122 | |
| 123 | if (peer->afc[AFI_IP][SAFI_UNICAST]) |
| 124 | peer->afc_nego[AFI_IP][SAFI_UNICAST] = 1; |
| 125 | else |
| 126 | return -1; |
| 127 | } |
| 128 | else if (cap->mpc.safi == SAFI_MULTICAST) |
| 129 | { |
| 130 | peer->afc_recv[AFI_IP][SAFI_MULTICAST] = 1; |
| 131 | |
| 132 | if (peer->afc[AFI_IP][SAFI_MULTICAST]) |
| 133 | peer->afc_nego[AFI_IP][SAFI_MULTICAST] = 1; |
| 134 | else |
| 135 | return -1; |
| 136 | } |
| 137 | else if (cap->mpc.safi == BGP_SAFI_VPNV4) |
| 138 | { |
| 139 | peer->afc_recv[AFI_IP][SAFI_MPLS_VPN] = 1; |
| 140 | |
| 141 | if (peer->afc[AFI_IP][SAFI_MPLS_VPN]) |
| 142 | peer->afc_nego[AFI_IP][SAFI_MPLS_VPN] = 1; |
| 143 | else |
| 144 | return -1; |
| 145 | } |
| 146 | else |
| 147 | return -1; |
| 148 | } |
| 149 | #ifdef HAVE_IPV6 |
| 150 | else if (ntohs (cap->mpc.afi) == AFI_IP6) |
| 151 | { |
| 152 | if (cap->mpc.safi == SAFI_UNICAST) |
| 153 | { |
| 154 | peer->afc_recv[AFI_IP6][SAFI_UNICAST] = 1; |
| 155 | |
| 156 | if (peer->afc[AFI_IP6][SAFI_UNICAST]) |
| 157 | peer->afc_nego[AFI_IP6][SAFI_UNICAST] = 1; |
| 158 | else |
| 159 | return -1; |
| 160 | } |
| 161 | else if (cap->mpc.safi == SAFI_MULTICAST) |
| 162 | { |
| 163 | peer->afc_recv[AFI_IP6][SAFI_MULTICAST] = 1; |
| 164 | |
| 165 | if (peer->afc[AFI_IP6][SAFI_MULTICAST]) |
| 166 | peer->afc_nego[AFI_IP6][SAFI_MULTICAST] = 1; |
| 167 | else |
| 168 | return -1; |
| 169 | } |
| 170 | else |
| 171 | return -1; |
| 172 | } |
| 173 | #endif /* HAVE_IPV6 */ |
| 174 | else |
| 175 | { |
| 176 | /* Unknown Address Family. */ |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | bgp_capability_orf_not_support (struct peer *peer, afi_t afi, safi_t safi, |
| 185 | u_char type, u_char mode) |
| 186 | { |
| 187 | if (BGP_DEBUG (normal, NORMAL)) |
| 188 | zlog_info ("%s Addr-family %d/%d has ORF type/mode %d/%d not supported", |
| 189 | peer->host, afi, safi, type, mode); |
| 190 | } |
| 191 | |
| 192 | int |
| 193 | bgp_capability_orf (struct peer *peer, struct capability *cap, |
| 194 | u_char *pnt) |
| 195 | { |
| 196 | afi_t afi = ntohs(cap->mpc.afi); |
| 197 | safi_t safi = cap->mpc.safi; |
| 198 | u_char number_of_orfs; |
| 199 | u_char type; |
| 200 | u_char mode; |
| 201 | u_int16_t sm_cap = 0; /* capability send-mode receive */ |
| 202 | u_int16_t rm_cap = 0; /* capability receive-mode receive */ |
| 203 | int i; |
| 204 | |
| 205 | /* Check length. */ |
| 206 | if (cap->length < 7) |
| 207 | { |
| 208 | zlog_info ("%s ORF Capability length error %d", |
| 209 | peer->host, cap->length); |
| 210 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 211 | return -1; |
| 212 | } |
| 213 | |
| 214 | if (BGP_DEBUG (normal, NORMAL)) |
| 215 | zlog_info ("%s OPEN has ORF CAP(%s) for afi/safi: %u/%u", |
| 216 | peer->host, (cap->code == CAPABILITY_CODE_ORF ? |
| 217 | "new" : "old"), afi, safi); |
| 218 | |
| 219 | /* Check AFI and SAFI. */ |
| 220 | if ((afi != AFI_IP && afi != AFI_IP6) |
| 221 | || (safi != SAFI_UNICAST && safi != SAFI_MULTICAST |
| 222 | && safi != BGP_SAFI_VPNV4)) |
| 223 | { |
| 224 | zlog_info ("%s Addr-family %d/%d not supported. Ignoring the ORF capability", |
| 225 | peer->host, afi, safi); |
| 226 | return -1; |
| 227 | } |
| 228 | |
| 229 | number_of_orfs = *pnt++; |
| 230 | |
| 231 | for (i = 0 ; i < number_of_orfs ; i++) |
| 232 | { |
| 233 | type = *pnt++; |
| 234 | mode = *pnt++; |
| 235 | |
| 236 | /* ORF Mode error check */ |
| 237 | if (mode != ORF_MODE_BOTH && mode != ORF_MODE_SEND |
| 238 | && mode != ORF_MODE_RECEIVE) |
| 239 | { |
| 240 | bgp_capability_orf_not_support (peer, afi, safi, type, mode); |
| 241 | continue; |
| 242 | } |
| 243 | |
| 244 | /* ORF Type and afi/safi error check */ |
| 245 | if (cap->code == CAPABILITY_CODE_ORF) |
| 246 | { |
| 247 | if (type == ORF_TYPE_PREFIX && |
| 248 | ((afi == AFI_IP && safi == SAFI_UNICAST) |
| 249 | || (afi == AFI_IP && safi == SAFI_MULTICAST) |
| 250 | || (afi == AFI_IP6 && safi == SAFI_UNICAST))) |
| 251 | { |
| 252 | sm_cap = PEER_CAP_ORF_PREFIX_SM_RCV; |
| 253 | rm_cap = PEER_CAP_ORF_PREFIX_RM_RCV; |
| 254 | if (BGP_DEBUG (normal, NORMAL)) |
| 255 | zlog_info ("%s OPEN has Prefixlist ORF(%d) capability as %s for afi/safi: %d/%d", |
| 256 | peer->host, ORF_TYPE_PREFIX, (mode == ORF_MODE_SEND ? "SEND" : |
| 257 | mode == ORF_MODE_RECEIVE ? "RECEIVE" : "BOTH") , afi, safi); |
| 258 | } |
| 259 | else |
| 260 | { |
| 261 | bgp_capability_orf_not_support (peer, afi, safi, type, mode); |
| 262 | continue; |
| 263 | } |
| 264 | } |
| 265 | else if (cap->code == CAPABILITY_CODE_ORF_OLD) |
| 266 | { |
| 267 | if (type == ORF_TYPE_PREFIX_OLD && |
| 268 | ((afi == AFI_IP && safi == SAFI_UNICAST) |
| 269 | || (afi == AFI_IP && safi == SAFI_MULTICAST) |
| 270 | || (afi == AFI_IP6 && safi == SAFI_UNICAST))) |
| 271 | { |
| 272 | sm_cap = PEER_CAP_ORF_PREFIX_SM_OLD_RCV; |
| 273 | rm_cap = PEER_CAP_ORF_PREFIX_RM_OLD_RCV; |
| 274 | if (BGP_DEBUG (normal, NORMAL)) |
| 275 | zlog_info ("%s OPEN has Prefixlist ORF(%d) capability as %s for afi/safi: %d/%d", |
| 276 | peer->host, ORF_TYPE_PREFIX_OLD, (mode == ORF_MODE_SEND ? "SEND" : |
| 277 | mode == ORF_MODE_RECEIVE ? "RECEIVE" : "BOTH") , afi, safi); |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | bgp_capability_orf_not_support (peer, afi, safi, type, mode); |
| 282 | continue; |
| 283 | } |
| 284 | } |
| 285 | else |
| 286 | { |
| 287 | bgp_capability_orf_not_support (peer, afi, safi, type, mode); |
| 288 | continue; |
| 289 | } |
| 290 | |
| 291 | switch (mode) |
| 292 | { |
| 293 | case ORF_MODE_BOTH: |
| 294 | SET_FLAG (peer->af_cap[afi][safi], sm_cap); |
| 295 | SET_FLAG (peer->af_cap[afi][safi], rm_cap); |
| 296 | break; |
| 297 | case ORF_MODE_SEND: |
| 298 | SET_FLAG (peer->af_cap[afi][safi], sm_cap); |
| 299 | break; |
| 300 | case ORF_MODE_RECEIVE: |
| 301 | SET_FLAG (peer->af_cap[afi][safi], rm_cap); |
| 302 | break; |
| 303 | } |
| 304 | } |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | /* Parse given capability. */ |
| 309 | int |
| 310 | bgp_capability_parse (struct peer *peer, u_char *pnt, u_char length, |
| 311 | u_char **error) |
| 312 | { |
| 313 | int ret; |
| 314 | u_char *end; |
| 315 | struct capability cap; |
| 316 | |
| 317 | end = pnt + length; |
| 318 | |
| 319 | while (pnt < end) |
| 320 | { |
| 321 | afi_t afi; |
| 322 | safi_t safi; |
| 323 | |
| 324 | /* Fetch structure to the byte stream. */ |
| 325 | memcpy (&cap, pnt, sizeof (struct capability)); |
| 326 | |
| 327 | afi = ntohs(cap.mpc.afi); |
| 328 | safi = cap.mpc.safi; |
| 329 | |
| 330 | if (BGP_DEBUG (normal, NORMAL)) |
| 331 | zlog_info ("%s OPEN has CAPABILITY code: %d, length %d", |
| 332 | peer->host, cap.code, cap.length); |
| 333 | |
| 334 | /* We need at least capability code and capability length. */ |
| 335 | if (pnt + 2 > end) |
| 336 | { |
| 337 | zlog_info ("%s Capability length error", peer->host); |
| 338 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 339 | return -1; |
| 340 | } |
| 341 | |
| 342 | /* Capability length check. */ |
| 343 | if (pnt + (cap.length + 2) > end) |
| 344 | { |
| 345 | zlog_info ("%s Capability length error", peer->host); |
| 346 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 347 | return -1; |
| 348 | } |
| 349 | |
| 350 | /* We know MP Capability Code. */ |
| 351 | if (cap.code == CAPABILITY_CODE_MP) |
| 352 | { |
| 353 | if (BGP_DEBUG (normal, NORMAL)) |
| 354 | zlog_info ("%s OPEN has MP_EXT CAP for afi/safi: %u/%u", |
| 355 | peer->host, afi, safi); |
| 356 | |
| 357 | /* Ignore capability when override-capability is set. */ |
| 358 | if (! CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) |
| 359 | { |
| 360 | /* Set negotiated value. */ |
| 361 | ret = bgp_capability_mp (peer, &cap); |
| 362 | |
| 363 | /* Unsupported Capability. */ |
| 364 | if (ret < 0) |
| 365 | { |
| 366 | /* Store return data. */ |
| 367 | memcpy (*error, &cap, cap.length + 2); |
| 368 | *error += cap.length + 2; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | else if (cap.code == CAPABILITY_CODE_REFRESH |
| 373 | || cap.code == CAPABILITY_CODE_REFRESH_OLD) |
| 374 | { |
| 375 | /* Check length. */ |
| 376 | if (cap.length != 0) |
| 377 | { |
| 378 | zlog_info ("%s Route Refresh Capability length error %d", |
| 379 | peer->host, cap.length); |
| 380 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 381 | return -1; |
| 382 | } |
| 383 | |
| 384 | if (BGP_DEBUG (normal, NORMAL)) |
| 385 | zlog_info ("%s OPEN has ROUTE-REFRESH capability(%s) for all address-families", |
| 386 | peer->host, |
| 387 | cap.code == CAPABILITY_CODE_REFRESH_OLD ? "old" : "new"); |
| 388 | |
| 389 | /* BGP refresh capability */ |
| 390 | if (cap.code == CAPABILITY_CODE_REFRESH_OLD) |
| 391 | SET_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV); |
| 392 | else |
| 393 | SET_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV); |
| 394 | } |
| 395 | else if (cap.code == CAPABILITY_CODE_ORF |
| 396 | || cap.code == CAPABILITY_CODE_ORF_OLD) |
| 397 | bgp_capability_orf (peer, &cap, pnt + sizeof (struct capability)); |
| 398 | else if (cap.code == CAPABILITY_CODE_DYNAMIC) |
| 399 | { |
| 400 | /* Check length. */ |
| 401 | if (cap.length != 0) |
| 402 | { |
| 403 | zlog_info ("%s Dynamic Capability length error %d", |
| 404 | peer->host, cap.length); |
| 405 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 406 | return -1; |
| 407 | } |
| 408 | |
| 409 | if (BGP_DEBUG (normal, NORMAL)) |
| 410 | zlog_info ("%s OPEN has DYNAMIC capability", peer->host); |
| 411 | |
| 412 | SET_FLAG (peer->cap, PEER_CAP_DYNAMIC_RCV); |
| 413 | } |
| 414 | |
| 415 | else if (cap.code > 128) |
| 416 | { |
| 417 | /* We don't send Notification for unknown vendor specific |
| 418 | capabilities. It seems reasonable for now... */ |
| 419 | zlog_warn ("%s Vendor specific capability %d", |
| 420 | peer->host, cap.code); |
| 421 | } |
| 422 | else |
| 423 | { |
| 424 | zlog_warn ("%s unrecognized capability code: %d - ignored", |
| 425 | peer->host, cap.code); |
| 426 | memcpy (*error, &cap, cap.length + 2); |
| 427 | *error += cap.length + 2; |
| 428 | } |
| 429 | |
| 430 | pnt += cap.length + 2; |
| 431 | } |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | int |
| 436 | bgp_auth_parse (struct peer *peer, u_char *pnt, size_t length) |
| 437 | { |
| 438 | bgp_notify_send (peer, |
| 439 | BGP_NOTIFY_OPEN_ERR, |
| 440 | BGP_NOTIFY_OPEN_AUTH_FAILURE); |
| 441 | return -1; |
| 442 | } |
| 443 | |
| 444 | int |
| 445 | strict_capability_same (struct peer *peer) |
| 446 | { |
| 447 | int i, j; |
| 448 | |
| 449 | for (i = AFI_IP; i < AFI_MAX; i++) |
| 450 | for (j = SAFI_UNICAST; j < SAFI_MAX; j++) |
| 451 | if (peer->afc[i][j] != peer->afc_nego[i][j]) |
| 452 | return 0; |
| 453 | return 1; |
| 454 | } |
| 455 | |
| 456 | /* Parse open option */ |
| 457 | int |
| 458 | bgp_open_option_parse (struct peer *peer, u_char length, int *capability) |
| 459 | { |
| 460 | int ret; |
| 461 | u_char *end; |
| 462 | u_char opt_type; |
| 463 | u_char opt_length; |
| 464 | u_char *pnt; |
| 465 | u_char *error; |
| 466 | u_char error_data[BGP_MAX_PACKET_SIZE]; |
| 467 | |
| 468 | /* Fetch pointer. */ |
| 469 | pnt = stream_pnt (peer->ibuf); |
| 470 | |
| 471 | ret = 0; |
| 472 | opt_type = 0; |
| 473 | opt_length = 0; |
| 474 | end = pnt + length; |
| 475 | error = error_data; |
| 476 | |
| 477 | if (BGP_DEBUG (normal, NORMAL)) |
| 478 | zlog_info ("%s rcv OPEN w/ OPTION parameter len: %u", |
| 479 | peer->host, length); |
| 480 | |
| 481 | while (pnt < end) |
| 482 | { |
| 483 | /* Check the length. */ |
| 484 | if (pnt + 2 > end) |
| 485 | { |
| 486 | zlog_info ("%s Option length error", peer->host); |
| 487 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 488 | return -1; |
| 489 | } |
| 490 | |
| 491 | /* Fetch option type and length. */ |
| 492 | opt_type = *pnt++; |
| 493 | opt_length = *pnt++; |
| 494 | |
| 495 | /* Option length check. */ |
| 496 | if (pnt + opt_length > end) |
| 497 | { |
| 498 | zlog_info ("%s Option length error", peer->host); |
| 499 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 500 | return -1; |
| 501 | } |
| 502 | |
| 503 | if (BGP_DEBUG (normal, NORMAL)) |
| 504 | zlog_info ("%s rcvd OPEN w/ optional parameter type %u (%s) len %u", |
| 505 | peer->host, opt_type, |
| 506 | opt_type == BGP_OPEN_OPT_AUTH ? "Authentication" : |
| 507 | opt_type == BGP_OPEN_OPT_CAP ? "Capability" : "Unknown", |
| 508 | opt_length); |
| 509 | |
| 510 | switch (opt_type) |
| 511 | { |
| 512 | case BGP_OPEN_OPT_AUTH: |
| 513 | ret = bgp_auth_parse (peer, pnt, opt_length); |
| 514 | break; |
| 515 | case BGP_OPEN_OPT_CAP: |
| 516 | ret = bgp_capability_parse (peer, pnt, opt_length, &error); |
| 517 | *capability = 1; |
| 518 | break; |
| 519 | default: |
| 520 | bgp_notify_send (peer, |
| 521 | BGP_NOTIFY_OPEN_ERR, |
| 522 | BGP_NOTIFY_OPEN_UNSUP_PARAM); |
| 523 | ret = -1; |
| 524 | break; |
| 525 | } |
| 526 | |
| 527 | /* Parse error. To accumulate all unsupported capability codes, |
| 528 | bgp_capability_parse does not return -1 when encounter |
| 529 | unsupported capability code. To detect that, please check |
| 530 | error and erro_data pointer, like below. */ |
| 531 | if (ret < 0) |
| 532 | return -1; |
| 533 | |
| 534 | /* Forward pointer. */ |
| 535 | pnt += opt_length; |
| 536 | } |
| 537 | |
| 538 | /* All OPEN option is parsed. Check capability when strict compare |
| 539 | flag is enabled.*/ |
| 540 | if (CHECK_FLAG (peer->flags, PEER_FLAG_STRICT_CAP_MATCH)) |
| 541 | { |
| 542 | /* If Unsupported Capability exists. */ |
| 543 | if (error != error_data) |
| 544 | { |
| 545 | bgp_notify_send_with_data (peer, |
| 546 | BGP_NOTIFY_OPEN_ERR, |
| 547 | BGP_NOTIFY_OPEN_UNSUP_CAPBL, |
| 548 | error_data, error - error_data); |
| 549 | return -1; |
| 550 | } |
| 551 | |
| 552 | /* Check local capability does not negotiated with remote |
| 553 | peer. */ |
| 554 | if (! strict_capability_same (peer)) |
| 555 | { |
| 556 | bgp_notify_send (peer, |
| 557 | BGP_NOTIFY_OPEN_ERR, |
| 558 | BGP_NOTIFY_OPEN_UNSUP_CAPBL); |
| 559 | return -1; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | /* Check there is no common capability send Unsupported Capability |
| 564 | error. */ |
| 565 | if (*capability && ! CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) |
| 566 | { |
| 567 | if (! peer->afc_nego[AFI_IP][SAFI_UNICAST] |
| 568 | && ! peer->afc_nego[AFI_IP][SAFI_MULTICAST] |
| 569 | && ! peer->afc_nego[AFI_IP][SAFI_MPLS_VPN] |
| 570 | && ! peer->afc_nego[AFI_IP6][SAFI_UNICAST] |
| 571 | && ! peer->afc_nego[AFI_IP6][SAFI_MULTICAST]) |
| 572 | { |
| 573 | plog_err (peer->log, "%s [Error] No common capability", peer->host); |
| 574 | |
| 575 | if (error != error_data) |
| 576 | |
| 577 | bgp_notify_send_with_data (peer, |
| 578 | BGP_NOTIFY_OPEN_ERR, |
| 579 | BGP_NOTIFY_OPEN_UNSUP_CAPBL, |
| 580 | error_data, error - error_data); |
| 581 | else |
| 582 | bgp_notify_send (peer, |
| 583 | BGP_NOTIFY_OPEN_ERR, |
| 584 | BGP_NOTIFY_OPEN_UNSUP_CAPBL); |
| 585 | return -1; |
| 586 | } |
| 587 | } |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | void |
| 592 | bgp_open_capability_orf (struct stream *s, struct peer *peer, |
| 593 | afi_t afi, safi_t safi, u_char code) |
| 594 | { |
| 595 | u_char cap_len; |
| 596 | u_char orf_len; |
| 597 | unsigned long capp; |
| 598 | unsigned long orfp; |
| 599 | unsigned long numberp; |
| 600 | int number_of_orfs = 0; |
| 601 | |
| 602 | if (safi == SAFI_MPLS_VPN) |
| 603 | safi = BGP_SAFI_VPNV4; |
| 604 | |
| 605 | stream_putc (s, BGP_OPEN_OPT_CAP); |
| 606 | capp = stream_get_putp (s); /* Set Capability Len Pointer */ |
| 607 | stream_putc (s, 0); /* Capability Length */ |
| 608 | stream_putc (s, code); /* Capability Code */ |
| 609 | orfp = stream_get_putp (s); /* Set ORF Len Pointer */ |
| 610 | stream_putc (s, 0); /* ORF Length */ |
| 611 | stream_putw (s, afi); |
| 612 | stream_putc (s, 0); |
| 613 | stream_putc (s, safi); |
| 614 | numberp = stream_get_putp (s); /* Set Number Pointer */ |
| 615 | stream_putc (s, 0); /* Number of ORFs */ |
| 616 | |
| 617 | /* Address Prefix ORF */ |
| 618 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM) |
| 619 | || CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_RM)) |
| 620 | { |
| 621 | stream_putc (s, (code == CAPABILITY_CODE_ORF ? |
| 622 | ORF_TYPE_PREFIX : ORF_TYPE_PREFIX_OLD)); |
| 623 | |
| 624 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM) |
| 625 | && CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_RM)) |
| 626 | { |
| 627 | SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV); |
| 628 | SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV); |
| 629 | stream_putc (s, ORF_MODE_BOTH); |
| 630 | } |
| 631 | else if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM)) |
| 632 | { |
| 633 | SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV); |
| 634 | stream_putc (s, ORF_MODE_SEND); |
| 635 | } |
| 636 | else |
| 637 | { |
| 638 | SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV); |
| 639 | stream_putc (s, ORF_MODE_RECEIVE); |
| 640 | } |
| 641 | number_of_orfs++; |
| 642 | } |
| 643 | |
| 644 | /* Total Number of ORFs. */ |
| 645 | stream_putc_at (s, numberp, number_of_orfs); |
| 646 | |
| 647 | /* Total ORF Len. */ |
| 648 | orf_len = stream_get_putp (s) - orfp - 1; |
| 649 | stream_putc_at (s, orfp, orf_len); |
| 650 | |
| 651 | /* Total Capability Len. */ |
| 652 | cap_len = stream_get_putp (s) - capp - 1; |
| 653 | stream_putc_at (s, capp, cap_len); |
| 654 | } |
| 655 | |
| 656 | /* Fill in capability open option to the packet. */ |
| 657 | void |
| 658 | bgp_open_capability (struct stream *s, struct peer *peer) |
| 659 | { |
| 660 | u_char len; |
| 661 | unsigned long cp; |
| 662 | afi_t afi; |
| 663 | safi_t safi; |
| 664 | |
| 665 | /* Remember current pointer for Opt Parm Len. */ |
| 666 | cp = stream_get_putp (s); |
| 667 | |
| 668 | /* Opt Parm Len. */ |
| 669 | stream_putc (s, 0); |
| 670 | |
| 671 | /* Do not send capability. */ |
| 672 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN) |
| 673 | || CHECK_FLAG (peer->flags, PEER_FLAG_DONT_CAPABILITY)) |
| 674 | return; |
| 675 | |
| 676 | /* When the peer is IPv4 unicast only, do not send capability. */ |
| 677 | if (! peer->afc[AFI_IP][SAFI_MULTICAST] |
| 678 | && ! peer->afc[AFI_IP][SAFI_MPLS_VPN] |
| 679 | && ! peer->afc[AFI_IP6][SAFI_UNICAST] |
| 680 | && ! peer->afc[AFI_IP6][SAFI_MULTICAST] |
| 681 | && CHECK_FLAG (peer->flags, PEER_FLAG_NO_ROUTE_REFRESH_CAP) |
| 682 | && ! CHECK_FLAG (peer->af_flags[AFI_IP][SAFI_UNICAST], |
| 683 | PEER_FLAG_ORF_PREFIX_SM) |
| 684 | && ! CHECK_FLAG (peer->af_flags[AFI_IP][SAFI_UNICAST], |
| 685 | PEER_FLAG_ORF_PREFIX_RM) |
| 686 | && ! CHECK_FLAG (peer->af_flags[AFI_IP][SAFI_MULTICAST], |
| 687 | PEER_FLAG_ORF_PREFIX_SM) |
| 688 | && ! CHECK_FLAG (peer->af_flags[AFI_IP][SAFI_MULTICAST], |
| 689 | PEER_FLAG_ORF_PREFIX_RM) |
| 690 | && ! CHECK_FLAG (peer->flags, PEER_FLAG_DYNAMIC_CAPABILITY)) |
| 691 | return; |
| 692 | |
| 693 | /* IPv4 unicast. */ |
| 694 | if (peer->afc[AFI_IP][SAFI_UNICAST]) |
| 695 | { |
| 696 | peer->afc_adv[AFI_IP][SAFI_UNICAST] = 1; |
| 697 | stream_putc (s, BGP_OPEN_OPT_CAP); |
| 698 | stream_putc (s, CAPABILITY_CODE_MP_LEN + 2); |
| 699 | stream_putc (s, CAPABILITY_CODE_MP); |
| 700 | stream_putc (s, CAPABILITY_CODE_MP_LEN); |
| 701 | stream_putw (s, AFI_IP); |
| 702 | stream_putc (s, 0); |
| 703 | stream_putc (s, SAFI_UNICAST); |
| 704 | } |
| 705 | /* IPv4 multicast. */ |
| 706 | if (peer->afc[AFI_IP][SAFI_MULTICAST]) |
| 707 | { |
| 708 | peer->afc_adv[AFI_IP][SAFI_MULTICAST] = 1; |
| 709 | stream_putc (s, BGP_OPEN_OPT_CAP); |
| 710 | stream_putc (s, CAPABILITY_CODE_MP_LEN + 2); |
| 711 | stream_putc (s, CAPABILITY_CODE_MP); |
| 712 | stream_putc (s, CAPABILITY_CODE_MP_LEN); |
| 713 | stream_putw (s, AFI_IP); |
| 714 | stream_putc (s, 0); |
| 715 | stream_putc (s, SAFI_MULTICAST); |
| 716 | } |
| 717 | /* IPv4 VPN */ |
| 718 | if (peer->afc[AFI_IP][SAFI_MPLS_VPN]) |
| 719 | { |
| 720 | peer->afc_adv[AFI_IP][SAFI_MPLS_VPN] = 1; |
| 721 | stream_putc (s, BGP_OPEN_OPT_CAP); |
| 722 | stream_putc (s, CAPABILITY_CODE_MP_LEN + 2); |
| 723 | stream_putc (s, CAPABILITY_CODE_MP); |
| 724 | stream_putc (s, CAPABILITY_CODE_MP_LEN); |
| 725 | stream_putw (s, AFI_IP); |
| 726 | stream_putc (s, 0); |
| 727 | stream_putc (s, BGP_SAFI_VPNV4); |
| 728 | } |
| 729 | #ifdef HAVE_IPV6 |
| 730 | /* IPv6 unicast. */ |
| 731 | if (peer->afc[AFI_IP6][SAFI_UNICAST]) |
| 732 | { |
| 733 | peer->afc_adv[AFI_IP6][SAFI_UNICAST] = 1; |
| 734 | stream_putc (s, BGP_OPEN_OPT_CAP); |
| 735 | stream_putc (s, CAPABILITY_CODE_MP_LEN + 2); |
| 736 | stream_putc (s, CAPABILITY_CODE_MP); |
| 737 | stream_putc (s, CAPABILITY_CODE_MP_LEN); |
| 738 | stream_putw (s, AFI_IP6); |
| 739 | stream_putc (s, 0); |
| 740 | stream_putc (s, SAFI_UNICAST); |
| 741 | } |
| 742 | /* IPv6 multicast. */ |
| 743 | if (peer->afc[AFI_IP6][SAFI_MULTICAST]) |
| 744 | { |
| 745 | peer->afc_adv[AFI_IP6][SAFI_MULTICAST] = 1; |
| 746 | stream_putc (s, BGP_OPEN_OPT_CAP); |
| 747 | stream_putc (s, CAPABILITY_CODE_MP_LEN + 2); |
| 748 | stream_putc (s, CAPABILITY_CODE_MP); |
| 749 | stream_putc (s, CAPABILITY_CODE_MP_LEN); |
| 750 | stream_putw (s, AFI_IP6); |
| 751 | stream_putc (s, 0); |
| 752 | stream_putc (s, SAFI_MULTICAST); |
| 753 | } |
| 754 | #endif /* HAVE_IPV6 */ |
| 755 | |
| 756 | /* Route refresh. */ |
| 757 | if (! CHECK_FLAG (peer->flags, PEER_FLAG_NO_ROUTE_REFRESH_CAP)) |
| 758 | { |
| 759 | SET_FLAG (peer->cap, PEER_CAP_REFRESH_ADV); |
| 760 | stream_putc (s, BGP_OPEN_OPT_CAP); |
| 761 | stream_putc (s, CAPABILITY_CODE_REFRESH_LEN + 2); |
| 762 | stream_putc (s, CAPABILITY_CODE_REFRESH_OLD); |
| 763 | stream_putc (s, CAPABILITY_CODE_REFRESH_LEN); |
| 764 | stream_putc (s, BGP_OPEN_OPT_CAP); |
| 765 | stream_putc (s, CAPABILITY_CODE_REFRESH_LEN + 2); |
| 766 | stream_putc (s, CAPABILITY_CODE_REFRESH); |
| 767 | stream_putc (s, CAPABILITY_CODE_REFRESH_LEN); |
| 768 | } |
| 769 | |
| 770 | /* ORF capability. */ |
| 771 | for (afi = AFI_IP ; afi < AFI_MAX ; afi++) |
| 772 | for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++) |
| 773 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM) |
| 774 | || CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_RM)) |
| 775 | { |
| 776 | bgp_open_capability_orf (s, peer, afi, safi, CAPABILITY_CODE_ORF_OLD); |
| 777 | bgp_open_capability_orf (s, peer, afi, safi, CAPABILITY_CODE_ORF); |
| 778 | } |
| 779 | |
| 780 | /* Dynamic capability. */ |
| 781 | if (CHECK_FLAG (peer->flags, PEER_FLAG_DYNAMIC_CAPABILITY)) |
| 782 | { |
| 783 | SET_FLAG (peer->cap, PEER_CAP_DYNAMIC_ADV); |
| 784 | stream_putc (s, BGP_OPEN_OPT_CAP); |
| 785 | stream_putc (s, CAPABILITY_CODE_DYNAMIC_LEN + 2); |
| 786 | stream_putc (s, CAPABILITY_CODE_DYNAMIC); |
| 787 | stream_putc (s, CAPABILITY_CODE_DYNAMIC_LEN); |
| 788 | } |
| 789 | |
| 790 | /* Total Opt Parm Len. */ |
| 791 | len = stream_get_putp (s) - cp - 1; |
| 792 | stream_putc_at (s, cp, len); |
| 793 | } |