paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* BGP packet management routine. |
| 2 | Copyright (C) 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 "thread.h" |
| 24 | #include "stream.h" |
| 25 | #include "network.h" |
| 26 | #include "prefix.h" |
| 27 | #include "command.h" |
| 28 | #include "log.h" |
| 29 | #include "memory.h" |
| 30 | #include "sockunion.h" /* for inet_ntop () */ |
| 31 | #include "linklist.h" |
| 32 | #include "plist.h" |
| 33 | |
| 34 | #include "bgpd/bgpd.h" |
| 35 | #include "bgpd/bgp_table.h" |
| 36 | #include "bgpd/bgp_dump.h" |
| 37 | #include "bgpd/bgp_attr.h" |
| 38 | #include "bgpd/bgp_debug.h" |
| 39 | #include "bgpd/bgp_fsm.h" |
| 40 | #include "bgpd/bgp_route.h" |
| 41 | #include "bgpd/bgp_packet.h" |
| 42 | #include "bgpd/bgp_open.h" |
| 43 | #include "bgpd/bgp_aspath.h" |
| 44 | #include "bgpd/bgp_community.h" |
| 45 | #include "bgpd/bgp_ecommunity.h" |
| 46 | #include "bgpd/bgp_network.h" |
| 47 | #include "bgpd/bgp_mplsvpn.h" |
| 48 | #include "bgpd/bgp_advertise.h" |
| 49 | |
| 50 | int stream_put_prefix (struct stream *, struct prefix *); |
| 51 | |
| 52 | /* Set up BGP packet marker and packet type. */ |
| 53 | static int |
| 54 | bgp_packet_set_marker (struct stream *s, u_char type) |
| 55 | { |
| 56 | int i; |
| 57 | |
| 58 | /* Fill in marker. */ |
| 59 | for (i = 0; i < BGP_MARKER_SIZE; i++) |
| 60 | stream_putc (s, 0xff); |
| 61 | |
| 62 | /* Dummy total length. This field is should be filled in later on. */ |
| 63 | stream_putw (s, 0); |
| 64 | |
| 65 | /* BGP packet type. */ |
| 66 | stream_putc (s, type); |
| 67 | |
| 68 | /* Return current stream size. */ |
| 69 | return stream_get_putp (s); |
| 70 | } |
| 71 | |
| 72 | /* Set BGP packet header size entry. If size is zero then use current |
| 73 | stream size. */ |
| 74 | static int |
| 75 | bgp_packet_set_size (struct stream *s) |
| 76 | { |
| 77 | int cp; |
| 78 | |
| 79 | /* Preserve current pointer. */ |
| 80 | cp = stream_get_putp (s); |
| 81 | stream_set_putp (s, BGP_MARKER_SIZE); |
| 82 | stream_putw (s, cp); |
| 83 | |
| 84 | /* Write back current pointer. */ |
| 85 | stream_set_putp (s, cp); |
| 86 | |
| 87 | return cp; |
| 88 | } |
| 89 | |
| 90 | /* Add new packet to the peer. */ |
| 91 | void |
| 92 | bgp_packet_add (struct peer *peer, struct stream *s) |
| 93 | { |
| 94 | /* Add packet to the end of list. */ |
| 95 | stream_fifo_push (peer->obuf, s); |
| 96 | } |
| 97 | |
| 98 | /* Free first packet. */ |
| 99 | void |
| 100 | bgp_packet_delete (struct peer *peer) |
| 101 | { |
| 102 | stream_free (stream_fifo_pop (peer->obuf)); |
| 103 | } |
| 104 | |
| 105 | /* Duplicate packet. */ |
| 106 | struct stream * |
| 107 | bgp_packet_dup (struct stream *s) |
| 108 | { |
| 109 | struct stream *new; |
| 110 | |
| 111 | new = stream_new (stream_get_endp (s)); |
| 112 | |
| 113 | new->endp = s->endp; |
| 114 | new->putp = s->putp; |
| 115 | new->getp = s->getp; |
| 116 | |
| 117 | memcpy (new->data, s->data, stream_get_endp (s)); |
| 118 | |
| 119 | return new; |
| 120 | } |
| 121 | |
| 122 | /* Check file descriptor whether connect is established. */ |
| 123 | static void |
| 124 | bgp_connect_check (struct peer *peer) |
| 125 | { |
| 126 | int status; |
| 127 | int slen; |
| 128 | int ret; |
| 129 | |
| 130 | /* Anyway I have to reset read and write thread. */ |
| 131 | BGP_READ_OFF (peer->t_read); |
| 132 | BGP_WRITE_OFF (peer->t_write); |
| 133 | |
| 134 | /* Check file descriptor. */ |
| 135 | slen = sizeof (status); |
| 136 | ret = getsockopt(peer->fd, SOL_SOCKET, SO_ERROR, (void *) &status, &slen); |
| 137 | |
| 138 | /* If getsockopt is fail, this is fatal error. */ |
| 139 | if (ret < 0) |
| 140 | { |
| 141 | zlog (peer->log, LOG_INFO, "can't get sockopt for nonblocking connect"); |
| 142 | BGP_EVENT_ADD (peer, TCP_fatal_error); |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | /* When status is 0 then TCP connection is established. */ |
| 147 | if (status == 0) |
| 148 | { |
| 149 | BGP_EVENT_ADD (peer, TCP_connection_open); |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | if (BGP_DEBUG (events, EVENTS)) |
| 154 | plog_info (peer->log, "%s [Event] Connect failed (%s)", |
| 155 | peer->host, strerror (errno)); |
| 156 | BGP_EVENT_ADD (peer, TCP_connection_open_failed); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /* Make BGP update packet. */ |
| 161 | struct stream * |
| 162 | bgp_update_packet (struct peer *peer, afi_t afi, safi_t safi) |
| 163 | { |
| 164 | struct stream *s; |
| 165 | struct bgp_adj_out *adj; |
| 166 | struct bgp_advertise *adv; |
| 167 | struct stream *packet; |
| 168 | struct bgp_node *rn = NULL; |
| 169 | struct bgp_info *binfo = NULL; |
| 170 | bgp_size_t total_attr_len = 0; |
| 171 | unsigned long pos; |
| 172 | char buf[BUFSIZ]; |
| 173 | struct prefix_rd *prd = NULL; |
| 174 | char *tag = NULL; |
| 175 | |
| 176 | s = peer->work; |
| 177 | stream_reset (s); |
| 178 | |
| 179 | adv = FIFO_HEAD (&peer->sync[afi][safi]->update); |
| 180 | |
| 181 | while (adv) |
| 182 | { |
| 183 | if (adv->rn) |
| 184 | rn = adv->rn; |
| 185 | adj = adv->adj; |
| 186 | if (adv->binfo) |
| 187 | binfo = adv->binfo; |
| 188 | #ifdef MPLS_VPN |
| 189 | if (rn) |
| 190 | prd = (struct prefix_rd *) &rn->prn->p; |
| 191 | if (binfo) |
| 192 | tag = binfo->tag; |
| 193 | #endif /* MPLS_VPN */ |
| 194 | |
| 195 | /* When remaining space can't include NLRI and it's length. */ |
| 196 | if (rn && STREAM_REMAIN (s) <= BGP_NLRI_LENGTH + PSIZE (rn->p.prefixlen)) |
| 197 | break; |
| 198 | |
| 199 | /* If packet is empty, set attribute. */ |
| 200 | if (stream_empty (s)) |
| 201 | { |
| 202 | bgp_packet_set_marker (s, BGP_MSG_UPDATE); |
| 203 | stream_putw (s, 0); |
| 204 | pos = stream_get_putp (s); |
| 205 | stream_putw (s, 0); |
| 206 | total_attr_len = bgp_packet_attribute (NULL, peer, s, |
| 207 | adv->baa->attr, |
| 208 | &rn->p, afi, safi, |
| 209 | binfo->peer, prd, tag); |
| 210 | stream_putw_at (s, pos, total_attr_len); |
| 211 | } |
| 212 | |
| 213 | if (afi == AFI_IP && safi == SAFI_UNICAST) |
| 214 | stream_put_prefix (s, &rn->p); |
| 215 | |
| 216 | if (BGP_DEBUG (update, UPDATE_OUT)) |
| 217 | zlog (peer->log, LOG_INFO, "%s send UPDATE %s/%d", |
| 218 | peer->host, |
| 219 | inet_ntop (rn->p.family, &(rn->p.u.prefix), buf, BUFSIZ), |
| 220 | rn->p.prefixlen); |
| 221 | |
| 222 | /* Synchnorize attribute. */ |
| 223 | if (adj->attr) |
| 224 | bgp_attr_unintern (adj->attr); |
| 225 | else |
| 226 | peer->scount[afi][safi]++; |
| 227 | |
| 228 | adj->attr = bgp_attr_intern (adv->baa->attr); |
| 229 | |
| 230 | adv = bgp_advertise_clean (peer, adj, afi, safi); |
| 231 | |
| 232 | if (! (afi == AFI_IP && safi == SAFI_UNICAST)) |
| 233 | break; |
| 234 | } |
| 235 | |
| 236 | if (! stream_empty (s)) |
| 237 | { |
| 238 | bgp_packet_set_size (s); |
| 239 | packet = bgp_packet_dup (s); |
| 240 | bgp_packet_add (peer, packet); |
| 241 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
| 242 | stream_reset (s); |
| 243 | return packet; |
| 244 | } |
| 245 | return NULL; |
| 246 | |
| 247 | } |
| 248 | |
| 249 | /* Make BGP withdraw packet. */ |
| 250 | struct stream * |
| 251 | bgp_withdraw_packet (struct peer *peer, afi_t afi, safi_t safi) |
| 252 | { |
| 253 | struct stream *s; |
| 254 | struct stream *packet; |
| 255 | struct bgp_adj_out *adj; |
| 256 | struct bgp_advertise *adv; |
| 257 | struct bgp_node *rn; |
| 258 | unsigned long pos; |
| 259 | bgp_size_t unfeasible_len; |
| 260 | bgp_size_t total_attr_len; |
| 261 | char buf[BUFSIZ]; |
| 262 | struct prefix_rd *prd = NULL; |
| 263 | |
| 264 | s = peer->work; |
| 265 | stream_reset (s); |
| 266 | |
| 267 | while ((adv = FIFO_HEAD (&peer->sync[afi][safi]->withdraw)) != NULL) |
| 268 | { |
| 269 | adj = adv->adj; |
| 270 | rn = adv->rn; |
| 271 | #ifdef MPLS_VPN |
| 272 | prd = (struct prefix_rd *) &rn->prn->p; |
| 273 | #endif /* MPLS_VPN */ |
| 274 | |
| 275 | if (STREAM_REMAIN (s) |
| 276 | <= (BGP_NLRI_LENGTH + BGP_TOTAL_ATTR_LEN + PSIZE (rn->p.prefixlen))) |
| 277 | break; |
| 278 | |
| 279 | if (stream_empty (s)) |
| 280 | { |
| 281 | bgp_packet_set_marker (s, BGP_MSG_UPDATE); |
| 282 | stream_putw (s, 0); |
| 283 | } |
| 284 | |
| 285 | if (afi == AFI_IP && safi == SAFI_UNICAST) |
| 286 | stream_put_prefix (s, &rn->p); |
| 287 | else |
| 288 | { |
| 289 | pos = stream_get_putp (s); |
| 290 | stream_putw (s, 0); |
| 291 | total_attr_len |
| 292 | = bgp_packet_withdraw (peer, s, &rn->p, afi, safi, prd, NULL); |
| 293 | |
| 294 | /* Set total path attribute length. */ |
| 295 | stream_putw_at (s, pos, total_attr_len); |
| 296 | } |
| 297 | |
| 298 | if (BGP_DEBUG (update, UPDATE_OUT)) |
| 299 | zlog (peer->log, LOG_INFO, "%s send UPDATE %s/%d -- unreachable", |
| 300 | peer->host, |
| 301 | inet_ntop (rn->p.family, &(rn->p.u.prefix), buf, BUFSIZ), |
| 302 | rn->p.prefixlen); |
| 303 | |
| 304 | peer->scount[afi][safi]--; |
| 305 | |
| 306 | bgp_adj_out_remove (rn, adj, peer, afi, safi); |
| 307 | bgp_unlock_node (rn); |
| 308 | |
| 309 | if (! (afi == AFI_IP && safi == SAFI_UNICAST)) |
| 310 | break; |
| 311 | } |
| 312 | |
| 313 | if (! stream_empty (s)) |
| 314 | { |
| 315 | if (afi == AFI_IP && safi == SAFI_UNICAST) |
| 316 | { |
| 317 | unfeasible_len |
| 318 | = stream_get_putp (s) - BGP_HEADER_SIZE - BGP_UNFEASIBLE_LEN; |
| 319 | stream_putw_at (s, BGP_HEADER_SIZE, unfeasible_len); |
| 320 | stream_putw (s, 0); |
| 321 | } |
| 322 | bgp_packet_set_size (s); |
| 323 | packet = bgp_packet_dup (s); |
| 324 | bgp_packet_add (peer, packet); |
| 325 | stream_reset (s); |
| 326 | return packet; |
| 327 | } |
| 328 | |
| 329 | return NULL; |
| 330 | } |
| 331 | |
| 332 | void |
| 333 | bgp_default_update_send (struct peer *peer, struct attr *attr, |
| 334 | afi_t afi, safi_t safi, struct peer *from) |
| 335 | { |
| 336 | struct stream *s; |
| 337 | struct stream *packet; |
| 338 | struct prefix p; |
| 339 | unsigned long pos; |
| 340 | bgp_size_t total_attr_len; |
| 341 | char attrstr[BUFSIZ]; |
| 342 | char buf[BUFSIZ]; |
| 343 | |
| 344 | #ifdef DISABLE_BGP_ANNOUNCE |
| 345 | return; |
| 346 | #endif /* DISABLE_BGP_ANNOUNCE */ |
| 347 | |
| 348 | if (afi == AFI_IP) |
| 349 | str2prefix ("0.0.0.0/0", &p); |
| 350 | #ifdef HAVE_IPV6 |
| 351 | else |
| 352 | str2prefix ("::/0", &p); |
| 353 | #endif /* HAVE_IPV6 */ |
| 354 | |
| 355 | /* Logging the attribute. */ |
| 356 | if (BGP_DEBUG (update, UPDATE_OUT)) |
| 357 | { |
| 358 | bgp_dump_attr (peer, attr, attrstr, BUFSIZ); |
| 359 | zlog (peer->log, LOG_INFO, "%s send UPDATE %s/%d %s", |
| 360 | peer->host, inet_ntop(p.family, &(p.u.prefix), buf, BUFSIZ), |
| 361 | p.prefixlen, attrstr); |
| 362 | } |
| 363 | |
| 364 | s = stream_new (BGP_MAX_PACKET_SIZE); |
| 365 | |
| 366 | /* Make BGP update packet. */ |
| 367 | bgp_packet_set_marker (s, BGP_MSG_UPDATE); |
| 368 | |
| 369 | /* Unfeasible Routes Length. */ |
| 370 | stream_putw (s, 0); |
| 371 | |
| 372 | /* Make place for total attribute length. */ |
| 373 | pos = stream_get_putp (s); |
| 374 | stream_putw (s, 0); |
| 375 | total_attr_len = bgp_packet_attribute (NULL, peer, s, attr, &p, afi, safi, from, NULL, NULL); |
| 376 | |
| 377 | /* Set Total Path Attribute Length. */ |
| 378 | stream_putw_at (s, pos, total_attr_len); |
| 379 | |
| 380 | /* NLRI set. */ |
| 381 | if (p.family == AF_INET && safi == SAFI_UNICAST) |
| 382 | stream_put_prefix (s, &p); |
| 383 | |
| 384 | /* Set size. */ |
| 385 | bgp_packet_set_size (s); |
| 386 | |
| 387 | packet = bgp_packet_dup (s); |
| 388 | stream_free (s); |
| 389 | |
| 390 | /* Dump packet if debug option is set. */ |
| 391 | #ifdef DEBUG |
| 392 | bgp_packet_dump (packet); |
| 393 | #endif /* DEBUG */ |
| 394 | |
| 395 | /* Add packet to the peer. */ |
| 396 | bgp_packet_add (peer, packet); |
| 397 | |
| 398 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
| 399 | } |
| 400 | |
| 401 | void |
| 402 | bgp_default_withdraw_send (struct peer *peer, afi_t afi, safi_t safi) |
| 403 | { |
| 404 | struct stream *s; |
| 405 | struct stream *packet; |
| 406 | struct prefix p; |
| 407 | unsigned long pos; |
| 408 | unsigned long cp; |
| 409 | bgp_size_t unfeasible_len; |
| 410 | bgp_size_t total_attr_len; |
| 411 | char buf[BUFSIZ]; |
| 412 | |
| 413 | #ifdef DISABLE_BGP_ANNOUNCE |
| 414 | return; |
| 415 | #endif /* DISABLE_BGP_ANNOUNCE */ |
| 416 | |
| 417 | if (afi == AFI_IP) |
| 418 | str2prefix ("0.0.0.0/0", &p); |
| 419 | #ifdef HAVE_IPV6 |
| 420 | else |
| 421 | str2prefix ("::/0", &p); |
| 422 | #endif /* HAVE_IPV6 */ |
| 423 | |
| 424 | total_attr_len = 0; |
| 425 | pos = 0; |
| 426 | |
| 427 | if (BGP_DEBUG (update, UPDATE_OUT)) |
| 428 | zlog (peer->log, LOG_INFO, "%s send UPDATE %s/%d -- unreachable", |
| 429 | peer->host, inet_ntop(p.family, &(p.u.prefix), buf, BUFSIZ), |
| 430 | p.prefixlen); |
| 431 | |
| 432 | s = stream_new (BGP_MAX_PACKET_SIZE); |
| 433 | |
| 434 | /* Make BGP update packet. */ |
| 435 | bgp_packet_set_marker (s, BGP_MSG_UPDATE); |
| 436 | |
| 437 | /* Unfeasible Routes Length. */; |
| 438 | cp = stream_get_putp (s); |
| 439 | stream_putw (s, 0); |
| 440 | |
| 441 | /* Withdrawn Routes. */ |
| 442 | if (p.family == AF_INET && safi == SAFI_UNICAST) |
| 443 | { |
| 444 | stream_put_prefix (s, &p); |
| 445 | |
| 446 | unfeasible_len = stream_get_putp (s) - cp - 2; |
| 447 | |
| 448 | /* Set unfeasible len. */ |
| 449 | stream_putw_at (s, cp, unfeasible_len); |
| 450 | |
| 451 | /* Set total path attribute length. */ |
| 452 | stream_putw (s, 0); |
| 453 | } |
| 454 | else |
| 455 | { |
| 456 | pos = stream_get_putp (s); |
| 457 | stream_putw (s, 0); |
| 458 | total_attr_len = bgp_packet_withdraw (peer, s, &p, afi, safi, NULL, NULL); |
| 459 | |
| 460 | /* Set total path attribute length. */ |
| 461 | stream_putw_at (s, pos, total_attr_len); |
| 462 | } |
| 463 | |
| 464 | bgp_packet_set_size (s); |
| 465 | |
| 466 | packet = bgp_packet_dup (s); |
| 467 | stream_free (s); |
| 468 | |
| 469 | /* Add packet to the peer. */ |
| 470 | bgp_packet_add (peer, packet); |
| 471 | |
| 472 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
| 473 | } |
| 474 | |
| 475 | /* Get next packet to be written. */ |
| 476 | struct stream * |
| 477 | bgp_write_packet (struct peer *peer) |
| 478 | { |
| 479 | afi_t afi; |
| 480 | safi_t safi; |
| 481 | struct stream *s = NULL; |
| 482 | struct bgp_advertise *adv; |
| 483 | |
| 484 | s = stream_fifo_head (peer->obuf); |
| 485 | if (s) |
| 486 | return s; |
| 487 | |
| 488 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 489 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) |
| 490 | { |
| 491 | adv = FIFO_HEAD (&peer->sync[afi][safi]->withdraw); |
| 492 | if (adv) |
| 493 | { |
| 494 | s = bgp_withdraw_packet (peer, afi, safi); |
| 495 | if (s) |
| 496 | return s; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 501 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) |
| 502 | { |
| 503 | adv = FIFO_HEAD (&peer->sync[afi][safi]->update); |
| 504 | if (adv) |
| 505 | { |
| 506 | if (adv->binfo && adv->binfo->uptime < peer->synctime) |
| 507 | s = bgp_update_packet (peer, afi, safi); |
| 508 | |
| 509 | if (s) |
| 510 | return s; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | return NULL; |
| 515 | } |
| 516 | |
| 517 | /* Is there partially written packet or updates we can send right |
| 518 | now. */ |
| 519 | int |
| 520 | bgp_write_proceed (struct peer *peer) |
| 521 | { |
| 522 | afi_t afi; |
| 523 | safi_t safi; |
| 524 | struct bgp_advertise *adv; |
| 525 | |
| 526 | if (stream_fifo_head (peer->obuf)) |
| 527 | return 1; |
| 528 | |
| 529 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 530 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) |
| 531 | if (FIFO_HEAD (&peer->sync[afi][safi]->withdraw)) |
| 532 | return 1; |
| 533 | |
| 534 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 535 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) |
| 536 | if ((adv = FIFO_HEAD (&peer->sync[afi][safi]->update)) != NULL) |
| 537 | if (adv->binfo->uptime < peer->synctime) |
| 538 | return 1; |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | /* Write packet to the peer. */ |
| 544 | int |
| 545 | bgp_write (struct thread *thread) |
| 546 | { |
| 547 | struct peer *peer; |
| 548 | u_char type; |
| 549 | struct stream *s; |
| 550 | int num; |
| 551 | int count = 0; |
| 552 | int write_errno; |
| 553 | |
| 554 | /* Yes first of all get peer pointer. */ |
| 555 | peer = THREAD_ARG (thread); |
| 556 | peer->t_write = NULL; |
| 557 | |
| 558 | /* For non-blocking IO check. */ |
| 559 | if (peer->status == Connect) |
| 560 | { |
| 561 | bgp_connect_check (peer); |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | /* Nonblocking write until TCP output buffer is full. */ |
| 566 | while (1) |
| 567 | { |
| 568 | int writenum; |
| 569 | |
| 570 | s = bgp_write_packet (peer); |
| 571 | if (! s) |
| 572 | return 0; |
| 573 | |
| 574 | /* Number of bytes to be sent. */ |
| 575 | writenum = stream_get_endp (s) - stream_get_getp (s); |
| 576 | |
| 577 | /* Call write() system call. */ |
| 578 | num = write (peer->fd, STREAM_PNT (s), writenum); |
| 579 | write_errno = errno; |
| 580 | if (num <= 0) |
| 581 | { |
| 582 | /* Partial write. */ |
| 583 | if (write_errno == EWOULDBLOCK || write_errno == EAGAIN) |
| 584 | break; |
| 585 | |
| 586 | bgp_stop (peer); |
| 587 | peer->status = Idle; |
| 588 | bgp_timer_set (peer); |
| 589 | return 0; |
| 590 | } |
| 591 | if (num != writenum) |
| 592 | { |
| 593 | stream_forward (s, num); |
| 594 | |
| 595 | if (write_errno == EAGAIN) |
| 596 | break; |
| 597 | |
| 598 | continue; |
| 599 | } |
| 600 | |
| 601 | /* Retrieve BGP packet type. */ |
| 602 | stream_set_getp (s, BGP_MARKER_SIZE + 2); |
| 603 | type = stream_getc (s); |
| 604 | |
| 605 | switch (type) |
| 606 | { |
| 607 | case BGP_MSG_OPEN: |
| 608 | peer->open_out++; |
| 609 | break; |
| 610 | case BGP_MSG_UPDATE: |
| 611 | peer->update_out++; |
| 612 | break; |
| 613 | case BGP_MSG_NOTIFY: |
| 614 | peer->notify_out++; |
| 615 | /* Double start timer. */ |
| 616 | peer->v_start *= 2; |
| 617 | |
| 618 | /* Overflow check. */ |
| 619 | if (peer->v_start >= (60 * 2)) |
| 620 | peer->v_start = (60 * 2); |
| 621 | |
| 622 | /* BGP_EVENT_ADD (peer, BGP_Stop); */ |
| 623 | bgp_stop (peer); |
| 624 | peer->status = Idle; |
| 625 | bgp_timer_set (peer); |
| 626 | return 0; |
| 627 | break; |
| 628 | case BGP_MSG_KEEPALIVE: |
| 629 | peer->keepalive_out++; |
| 630 | break; |
| 631 | case BGP_MSG_ROUTE_REFRESH_NEW: |
| 632 | case BGP_MSG_ROUTE_REFRESH_OLD: |
| 633 | peer->refresh_out++; |
| 634 | break; |
| 635 | case BGP_MSG_CAPABILITY: |
| 636 | peer->dynamic_cap_out++; |
| 637 | break; |
| 638 | } |
| 639 | |
| 640 | /* OK we send packet so delete it. */ |
| 641 | bgp_packet_delete (peer); |
| 642 | |
| 643 | if (++count >= BGP_WRITE_PACKET_MAX) |
| 644 | break; |
| 645 | } |
| 646 | |
| 647 | if (bgp_write_proceed (peer)) |
| 648 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
| 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | /* This is only for sending NOTIFICATION message to neighbor. */ |
| 654 | int |
| 655 | bgp_write_notify (struct peer *peer) |
| 656 | { |
| 657 | int ret; |
| 658 | u_char type; |
| 659 | struct stream *s; |
| 660 | |
| 661 | /* There should be at least one packet. */ |
| 662 | s = stream_fifo_head (peer->obuf); |
| 663 | if (!s) |
| 664 | return 0; |
| 665 | assert (stream_get_endp (s) >= BGP_HEADER_SIZE); |
| 666 | |
| 667 | /* I'm not sure fd is writable. */ |
| 668 | ret = writen (peer->fd, STREAM_DATA (s), stream_get_endp (s)); |
| 669 | if (ret <= 0) |
| 670 | { |
| 671 | bgp_stop (peer); |
| 672 | peer->status = Idle; |
| 673 | bgp_timer_set (peer); |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | /* Retrieve BGP packet type. */ |
| 678 | stream_set_getp (s, BGP_MARKER_SIZE + 2); |
| 679 | type = stream_getc (s); |
| 680 | |
| 681 | assert (type == BGP_MSG_NOTIFY); |
| 682 | |
| 683 | /* Type should be notify. */ |
| 684 | peer->notify_out++; |
| 685 | |
| 686 | /* Double start timer. */ |
| 687 | peer->v_start *= 2; |
| 688 | |
| 689 | /* Overflow check. */ |
| 690 | if (peer->v_start >= (60 * 2)) |
| 691 | peer->v_start = (60 * 2); |
| 692 | |
| 693 | /* We don't call event manager at here for avoiding other events. */ |
| 694 | bgp_stop (peer); |
| 695 | peer->status = Idle; |
| 696 | bgp_timer_set (peer); |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | /* Make keepalive packet and send it to the peer. */ |
| 702 | void |
| 703 | bgp_keepalive_send (struct peer *peer) |
| 704 | { |
| 705 | struct stream *s; |
| 706 | int length; |
| 707 | |
| 708 | s = stream_new (BGP_MAX_PACKET_SIZE); |
| 709 | |
| 710 | /* Make keepalive packet. */ |
| 711 | bgp_packet_set_marker (s, BGP_MSG_KEEPALIVE); |
| 712 | |
| 713 | /* Set packet size. */ |
| 714 | length = bgp_packet_set_size (s); |
| 715 | |
| 716 | /* Dump packet if debug option is set. */ |
| 717 | /* bgp_packet_dump (s); */ |
| 718 | |
| 719 | if (BGP_DEBUG (keepalive, KEEPALIVE)) |
| 720 | zlog_info ("%s sending KEEPALIVE", peer->host); |
| 721 | if (BGP_DEBUG (normal, NORMAL)) |
| 722 | zlog_info ("%s send message type %d, length (incl. header) %d", |
| 723 | peer->host, BGP_MSG_KEEPALIVE, length); |
| 724 | |
| 725 | /* Add packet to the peer. */ |
| 726 | bgp_packet_add (peer, s); |
| 727 | |
| 728 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
| 729 | } |
| 730 | |
| 731 | /* Make open packet and send it to the peer. */ |
| 732 | void |
| 733 | bgp_open_send (struct peer *peer) |
| 734 | { |
| 735 | struct stream *s; |
| 736 | int length; |
| 737 | u_int16_t send_holdtime; |
| 738 | as_t local_as; |
| 739 | |
| 740 | if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER)) |
| 741 | send_holdtime = peer->holdtime; |
| 742 | else |
| 743 | send_holdtime = peer->bgp->default_holdtime; |
| 744 | |
| 745 | /* local-as Change */ |
| 746 | if (peer->change_local_as) |
| 747 | local_as = peer->change_local_as; |
| 748 | else |
| 749 | local_as = peer->local_as; |
| 750 | |
| 751 | s = stream_new (BGP_MAX_PACKET_SIZE); |
| 752 | |
| 753 | /* Make open packet. */ |
| 754 | bgp_packet_set_marker (s, BGP_MSG_OPEN); |
| 755 | |
| 756 | /* Set open packet values. */ |
| 757 | stream_putc (s, BGP_VERSION_4); /* BGP version */ |
| 758 | stream_putw (s, local_as); /* My Autonomous System*/ |
| 759 | stream_putw (s, send_holdtime); /* Hold Time */ |
| 760 | stream_put_in_addr (s, &peer->local_id); /* BGP Identifier */ |
| 761 | |
| 762 | /* Set capability code. */ |
| 763 | bgp_open_capability (s, peer); |
| 764 | |
| 765 | /* Set BGP packet length. */ |
| 766 | length = bgp_packet_set_size (s); |
| 767 | |
| 768 | if (BGP_DEBUG (normal, NORMAL)) |
| 769 | zlog_info ("%s sending OPEN, version %d, my as %d, holdtime %d, id %s", |
| 770 | peer->host, BGP_VERSION_4, local_as, |
| 771 | send_holdtime, inet_ntoa (peer->local_id)); |
| 772 | |
| 773 | if (BGP_DEBUG (normal, NORMAL)) |
| 774 | zlog_info ("%s send message type %d, length (incl. header) %d", |
| 775 | peer->host, BGP_MSG_OPEN, length); |
| 776 | |
| 777 | /* Dump packet if debug option is set. */ |
| 778 | /* bgp_packet_dump (s); */ |
| 779 | |
| 780 | /* Add packet to the peer. */ |
| 781 | bgp_packet_add (peer, s); |
| 782 | |
| 783 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
| 784 | } |
| 785 | |
| 786 | /* Send BGP notify packet with data potion. */ |
| 787 | void |
| 788 | bgp_notify_send_with_data (struct peer *peer, u_char code, u_char sub_code, |
| 789 | u_char *data, size_t datalen) |
| 790 | { |
| 791 | struct stream *s; |
| 792 | int length; |
| 793 | |
| 794 | /* Allocate new stream. */ |
| 795 | s = stream_new (BGP_MAX_PACKET_SIZE); |
| 796 | |
| 797 | /* Make nitify packet. */ |
| 798 | bgp_packet_set_marker (s, BGP_MSG_NOTIFY); |
| 799 | |
| 800 | /* Set notify packet values. */ |
| 801 | stream_putc (s, code); /* BGP notify code */ |
| 802 | stream_putc (s, sub_code); /* BGP notify sub_code */ |
| 803 | |
| 804 | /* If notify data is present. */ |
| 805 | if (data) |
| 806 | stream_write (s, data, datalen); |
| 807 | |
| 808 | /* Set BGP packet length. */ |
| 809 | length = bgp_packet_set_size (s); |
| 810 | |
| 811 | /* Add packet to the peer. */ |
| 812 | stream_fifo_clean (peer->obuf); |
| 813 | bgp_packet_add (peer, s); |
| 814 | |
| 815 | /* For debug */ |
| 816 | { |
| 817 | struct bgp_notify bgp_notify; |
| 818 | int first = 0; |
| 819 | int i; |
| 820 | char c[4]; |
| 821 | |
| 822 | bgp_notify.code = code; |
| 823 | bgp_notify.subcode = sub_code; |
| 824 | bgp_notify.data = NULL; |
| 825 | bgp_notify.length = length - BGP_MSG_NOTIFY_MIN_SIZE; |
| 826 | |
| 827 | if (bgp_notify.length) |
| 828 | { |
| 829 | bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3); |
| 830 | for (i = 0; i < bgp_notify.length; i++) |
| 831 | if (first) |
| 832 | { |
| 833 | sprintf (c, " %02x", data[i]); |
| 834 | strcat (bgp_notify.data, c); |
| 835 | } |
| 836 | else |
| 837 | { |
| 838 | first = 1; |
| 839 | sprintf (c, "%02x", data[i]); |
| 840 | strcpy (bgp_notify.data, c); |
| 841 | } |
| 842 | } |
| 843 | bgp_notify_print (peer, &bgp_notify, "sending"); |
| 844 | if (bgp_notify.data) |
| 845 | XFREE (MTYPE_TMP, bgp_notify.data); |
| 846 | } |
| 847 | |
| 848 | if (BGP_DEBUG (normal, NORMAL)) |
| 849 | zlog_info ("%s send message type %d, length (incl. header) %d", |
| 850 | peer->host, BGP_MSG_NOTIFY, length); |
| 851 | |
| 852 | /* Call imidiately. */ |
| 853 | BGP_WRITE_OFF (peer->t_write); |
| 854 | |
| 855 | bgp_write_notify (peer); |
| 856 | } |
| 857 | |
| 858 | /* Send BGP notify packet. */ |
| 859 | void |
| 860 | bgp_notify_send (struct peer *peer, u_char code, u_char sub_code) |
| 861 | { |
| 862 | bgp_notify_send_with_data (peer, code, sub_code, NULL, 0); |
| 863 | } |
| 864 | |
| 865 | char * |
| 866 | afi2str (afi_t afi) |
| 867 | { |
| 868 | if (afi == AFI_IP) |
| 869 | return "AFI_IP"; |
| 870 | else if (afi == AFI_IP6) |
| 871 | return "AFI_IP6"; |
| 872 | else |
| 873 | return "Unknown AFI"; |
| 874 | } |
| 875 | |
| 876 | char * |
| 877 | safi2str (safi_t safi) |
| 878 | { |
| 879 | if (safi == SAFI_UNICAST) |
| 880 | return "SAFI_UNICAST"; |
| 881 | else if (safi == SAFI_MULTICAST) |
| 882 | return "SAFI_MULTICAST"; |
| 883 | else if (safi == SAFI_MPLS_VPN || safi == BGP_SAFI_VPNV4) |
| 884 | return "SAFI_MPLS_VPN"; |
| 885 | else |
| 886 | return "Unknown SAFI"; |
| 887 | } |
| 888 | |
| 889 | /* Send route refresh message to the peer. */ |
| 890 | void |
| 891 | bgp_route_refresh_send (struct peer *peer, afi_t afi, safi_t safi, |
| 892 | u_char orf_type, u_char when_to_refresh, int remove) |
| 893 | { |
| 894 | struct stream *s; |
| 895 | struct stream *packet; |
| 896 | int length; |
| 897 | struct bgp_filter *filter; |
| 898 | int orf_refresh = 0; |
| 899 | |
| 900 | #ifdef DISABLE_BGP_ANNOUNCE |
| 901 | return; |
| 902 | #endif /* DISABLE_BGP_ANNOUNCE */ |
| 903 | |
| 904 | filter = &peer->filter[afi][safi]; |
| 905 | |
| 906 | /* Adjust safi code. */ |
| 907 | if (safi == SAFI_MPLS_VPN) |
| 908 | safi = BGP_SAFI_VPNV4; |
| 909 | |
| 910 | s = stream_new (BGP_MAX_PACKET_SIZE); |
| 911 | |
| 912 | /* Make BGP update packet. */ |
| 913 | if (CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV)) |
| 914 | bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_NEW); |
| 915 | else |
| 916 | bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_OLD); |
| 917 | |
| 918 | /* Encode Route Refresh message. */ |
| 919 | stream_putw (s, afi); |
| 920 | stream_putc (s, 0); |
| 921 | stream_putc (s, safi); |
| 922 | |
| 923 | if (orf_type == ORF_TYPE_PREFIX |
| 924 | || orf_type == ORF_TYPE_PREFIX_OLD) |
| 925 | if (remove || filter->plist[FILTER_IN].plist) |
| 926 | { |
| 927 | u_int16_t orf_len; |
| 928 | unsigned long orfp; |
| 929 | |
| 930 | orf_refresh = 1; |
| 931 | stream_putc (s, when_to_refresh); |
| 932 | stream_putc (s, orf_type); |
| 933 | orfp = stream_get_putp (s); |
| 934 | stream_putw (s, 0); |
| 935 | |
| 936 | if (remove) |
| 937 | { |
| 938 | UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND); |
| 939 | stream_putc (s, ORF_COMMON_PART_REMOVE_ALL); |
| 940 | if (BGP_DEBUG (normal, NORMAL)) |
| 941 | zlog_info ("%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d", |
| 942 | peer->host, orf_type, |
| 943 | (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"), |
| 944 | afi, safi); |
| 945 | } |
| 946 | else |
| 947 | { |
| 948 | SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND); |
| 949 | prefix_bgp_orf_entry (s, filter->plist[FILTER_IN].plist, |
| 950 | ORF_COMMON_PART_ADD, ORF_COMMON_PART_PERMIT, |
| 951 | ORF_COMMON_PART_DENY); |
| 952 | if (BGP_DEBUG (normal, NORMAL)) |
| 953 | zlog_info ("%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d", |
| 954 | peer->host, orf_type, |
| 955 | (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"), |
| 956 | afi, safi); |
| 957 | } |
| 958 | |
| 959 | /* Total ORF Entry Len. */ |
| 960 | orf_len = stream_get_putp (s) - orfp - 2; |
| 961 | stream_putw_at (s, orfp, orf_len); |
| 962 | } |
| 963 | |
| 964 | /* Set packet size. */ |
| 965 | length = bgp_packet_set_size (s); |
| 966 | |
| 967 | if (BGP_DEBUG (normal, NORMAL)) |
| 968 | { |
| 969 | if (! orf_refresh) |
| 970 | zlog_info ("%s sending REFRESH_REQ for afi/safi: %d/%d", |
| 971 | peer->host, afi, safi); |
| 972 | zlog_info ("%s send message type %d, length (incl. header) %d", |
| 973 | peer->host, CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV) ? |
| 974 | BGP_MSG_ROUTE_REFRESH_NEW : BGP_MSG_ROUTE_REFRESH_OLD, length); |
| 975 | } |
| 976 | |
| 977 | /* Make real packet. */ |
| 978 | packet = bgp_packet_dup (s); |
| 979 | stream_free (s); |
| 980 | |
| 981 | /* Add packet to the peer. */ |
| 982 | bgp_packet_add (peer, packet); |
| 983 | |
| 984 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
| 985 | } |
| 986 | |
| 987 | /* Send capability message to the peer. */ |
| 988 | void |
| 989 | bgp_capability_send (struct peer *peer, afi_t afi, safi_t safi, |
| 990 | int capability_code, int action) |
| 991 | { |
| 992 | struct stream *s; |
| 993 | struct stream *packet; |
| 994 | int length; |
| 995 | |
| 996 | /* Adjust safi code. */ |
| 997 | if (safi == SAFI_MPLS_VPN) |
| 998 | safi = BGP_SAFI_VPNV4; |
| 999 | |
| 1000 | s = stream_new (BGP_MAX_PACKET_SIZE); |
| 1001 | |
| 1002 | /* Make BGP update packet. */ |
| 1003 | bgp_packet_set_marker (s, BGP_MSG_CAPABILITY); |
| 1004 | |
| 1005 | /* Encode MP_EXT capability. */ |
| 1006 | if (capability_code == CAPABILITY_CODE_MP) |
| 1007 | { |
| 1008 | stream_putc (s, action); |
| 1009 | stream_putc (s, CAPABILITY_CODE_MP); |
| 1010 | stream_putc (s, CAPABILITY_CODE_MP_LEN); |
| 1011 | stream_putw (s, afi); |
| 1012 | stream_putc (s, 0); |
| 1013 | stream_putc (s, safi); |
| 1014 | |
| 1015 | if (BGP_DEBUG (normal, NORMAL)) |
| 1016 | zlog_info ("%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d", |
| 1017 | peer->host, action == CAPABILITY_ACTION_SET ? |
| 1018 | "Advertising" : "Removing", afi, safi); |
| 1019 | } |
| 1020 | |
| 1021 | /* Encode Route Refresh capability. */ |
| 1022 | if (capability_code == CAPABILITY_CODE_REFRESH) |
| 1023 | { |
| 1024 | stream_putc (s, action); |
| 1025 | stream_putc (s, CAPABILITY_CODE_REFRESH); |
| 1026 | stream_putc (s, CAPABILITY_CODE_REFRESH_LEN); |
| 1027 | stream_putc (s, action); |
| 1028 | stream_putc (s, CAPABILITY_CODE_REFRESH_OLD); |
| 1029 | stream_putc (s, CAPABILITY_CODE_REFRESH_LEN); |
| 1030 | |
| 1031 | if (BGP_DEBUG (normal, NORMAL)) |
| 1032 | zlog_info ("%s sending CAPABILITY has %s ROUTE-REFRESH capability", |
| 1033 | peer->host, action == CAPABILITY_ACTION_SET ? |
| 1034 | "Advertising" : "Removing"); |
| 1035 | } |
| 1036 | |
| 1037 | /* Set packet size. */ |
| 1038 | length = bgp_packet_set_size (s); |
| 1039 | |
| 1040 | /* Make real packet. */ |
| 1041 | packet = bgp_packet_dup (s); |
| 1042 | stream_free (s); |
| 1043 | |
| 1044 | /* Add packet to the peer. */ |
| 1045 | bgp_packet_add (peer, packet); |
| 1046 | |
| 1047 | if (BGP_DEBUG (normal, NORMAL)) |
| 1048 | zlog_info ("%s send message type %d, length (incl. header) %d", |
| 1049 | peer->host, BGP_MSG_CAPABILITY, length); |
| 1050 | |
| 1051 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
| 1052 | } |
| 1053 | |
| 1054 | /* RFC1771 6.8 Connection collision detection. */ |
| 1055 | int |
| 1056 | bgp_collision_detect (struct peer *new, struct in_addr remote_id) |
| 1057 | { |
| 1058 | struct peer *peer; |
| 1059 | struct listnode *nn; |
| 1060 | struct bgp *bgp; |
| 1061 | |
| 1062 | bgp = bgp_get_default (); |
| 1063 | if (! bgp) |
| 1064 | return 0; |
| 1065 | |
| 1066 | /* Upon receipt of an OPEN message, the local system must examine |
| 1067 | all of its connections that are in the OpenConfirm state. A BGP |
| 1068 | speaker may also examine connections in an OpenSent state if it |
| 1069 | knows the BGP Identifier of the peer by means outside of the |
| 1070 | protocol. If among these connections there is a connection to a |
| 1071 | remote BGP speaker whose BGP Identifier equals the one in the |
| 1072 | OPEN message, then the local system performs the following |
| 1073 | collision resolution procedure: */ |
| 1074 | |
| 1075 | LIST_LOOP (bgp->peer, peer, nn) |
| 1076 | { |
| 1077 | /* Under OpenConfirm status, local peer structure already hold |
| 1078 | remote router ID. */ |
| 1079 | |
| 1080 | if (peer != new |
| 1081 | && (peer->status == OpenConfirm || peer->status == OpenSent) |
| 1082 | && sockunion_same (&peer->su, &new->su)) |
| 1083 | { |
| 1084 | /* 1. The BGP Identifier of the local system is compared to |
| 1085 | the BGP Identifier of the remote system (as specified in |
| 1086 | the OPEN message). */ |
| 1087 | |
| 1088 | if (ntohl (peer->local_id.s_addr) < ntohl (remote_id.s_addr)) |
| 1089 | { |
| 1090 | /* 2. If the value of the local BGP Identifier is less |
| 1091 | than the remote one, the local system closes BGP |
| 1092 | connection that already exists (the one that is |
| 1093 | already in the OpenConfirm state), and accepts BGP |
| 1094 | connection initiated by the remote system. */ |
| 1095 | |
| 1096 | if (peer->fd >= 0) |
| 1097 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 1098 | return 1; |
| 1099 | } |
| 1100 | else |
| 1101 | { |
| 1102 | /* 3. Otherwise, the local system closes newly created |
| 1103 | BGP connection (the one associated with the newly |
| 1104 | received OPEN message), and continues to use the |
| 1105 | existing one (the one that is already in the |
| 1106 | OpenConfirm state). */ |
| 1107 | |
| 1108 | if (new->fd >= 0) |
| 1109 | bgp_notify_send (new, BGP_NOTIFY_CEASE, 0); |
| 1110 | return -1; |
| 1111 | } |
| 1112 | } |
| 1113 | } |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
| 1117 | int |
| 1118 | bgp_open_receive (struct peer *peer, bgp_size_t size) |
| 1119 | { |
| 1120 | int ret; |
| 1121 | u_char version; |
| 1122 | u_char optlen; |
| 1123 | u_int16_t holdtime; |
| 1124 | u_int16_t send_holdtime; |
| 1125 | as_t remote_as; |
| 1126 | struct peer *realpeer; |
| 1127 | struct in_addr remote_id; |
| 1128 | int capability; |
| 1129 | char notify_data_remote_as[2]; |
| 1130 | char notify_data_remote_id[4]; |
| 1131 | |
| 1132 | realpeer = NULL; |
| 1133 | |
| 1134 | /* Parse open packet. */ |
| 1135 | version = stream_getc (peer->ibuf); |
| 1136 | memcpy (notify_data_remote_as, stream_pnt (peer->ibuf), 2); |
| 1137 | remote_as = stream_getw (peer->ibuf); |
| 1138 | holdtime = stream_getw (peer->ibuf); |
| 1139 | memcpy (notify_data_remote_id, stream_pnt (peer->ibuf), 4); |
| 1140 | remote_id.s_addr = stream_get_ipv4 (peer->ibuf); |
| 1141 | |
| 1142 | /* Receive OPEN message log */ |
| 1143 | if (BGP_DEBUG (normal, NORMAL)) |
| 1144 | zlog_info ("%s rcv OPEN, version %d, remote-as %d, holdtime %d, id %s", |
| 1145 | peer->host, version, remote_as, holdtime, |
| 1146 | inet_ntoa (remote_id)); |
| 1147 | |
| 1148 | /* Lookup peer from Open packet. */ |
| 1149 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 1150 | { |
| 1151 | int as = 0; |
| 1152 | |
| 1153 | realpeer = peer_lookup_with_open (&peer->su, remote_as, &remote_id, &as); |
| 1154 | |
| 1155 | if (! realpeer) |
| 1156 | { |
| 1157 | /* Peer's source IP address is check in bgp_accept(), so this |
| 1158 | must be AS number mismatch or remote-id configuration |
| 1159 | mismatch. */ |
| 1160 | if (as) |
| 1161 | { |
| 1162 | if (BGP_DEBUG (normal, NORMAL)) |
| 1163 | zlog_info ("%s bad OPEN, wrong router identifier %s", |
| 1164 | peer->host, inet_ntoa (remote_id)); |
| 1165 | bgp_notify_send_with_data (peer, |
| 1166 | BGP_NOTIFY_OPEN_ERR, |
| 1167 | BGP_NOTIFY_OPEN_BAD_BGP_IDENT, |
| 1168 | notify_data_remote_id, 4); |
| 1169 | } |
| 1170 | else |
| 1171 | { |
| 1172 | if (BGP_DEBUG (normal, NORMAL)) |
| 1173 | zlog_info ("%s bad OPEN, remote AS is %d, expected %d", |
| 1174 | peer->host, remote_as, peer->as); |
| 1175 | bgp_notify_send_with_data (peer, |
| 1176 | BGP_NOTIFY_OPEN_ERR, |
| 1177 | BGP_NOTIFY_OPEN_BAD_PEER_AS, |
| 1178 | notify_data_remote_as, 2); |
| 1179 | } |
| 1180 | return -1; |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | /* When collision is detected and this peer is closed. Retrun |
| 1185 | immidiately. */ |
| 1186 | ret = bgp_collision_detect (peer, remote_id); |
| 1187 | if (ret < 0) |
| 1188 | return ret; |
| 1189 | |
| 1190 | /* Hack part. */ |
| 1191 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 1192 | { |
| 1193 | if (ret == 0 && realpeer->status != Active |
| 1194 | && realpeer->status != OpenSent |
| 1195 | && realpeer->status != OpenConfirm) |
| 1196 | { |
| 1197 | if (BGP_DEBUG (events, EVENTS)) |
| 1198 | zlog_info ("%s [Event] peer's status is %s close connection", |
| 1199 | realpeer->host, LOOKUP (bgp_status_msg, peer->status)); |
| 1200 | return -1; |
| 1201 | } |
| 1202 | |
| 1203 | if (BGP_DEBUG (events, EVENTS)) |
| 1204 | zlog_info ("%s [Event] Transfer temporary BGP peer to existing one", |
| 1205 | peer->host); |
| 1206 | |
| 1207 | bgp_stop (realpeer); |
| 1208 | |
| 1209 | /* Transfer file descriptor. */ |
| 1210 | realpeer->fd = peer->fd; |
| 1211 | peer->fd = -1; |
| 1212 | |
| 1213 | /* Transfer input buffer. */ |
| 1214 | stream_free (realpeer->ibuf); |
| 1215 | realpeer->ibuf = peer->ibuf; |
| 1216 | realpeer->packet_size = peer->packet_size; |
| 1217 | peer->ibuf = NULL; |
| 1218 | |
| 1219 | /* Transfer status. */ |
| 1220 | realpeer->status = peer->status; |
| 1221 | bgp_stop (peer); |
| 1222 | |
| 1223 | /* peer pointer change. Open packet send to neighbor. */ |
| 1224 | peer = realpeer; |
| 1225 | bgp_open_send (peer); |
| 1226 | if (peer->fd < 0) |
| 1227 | { |
| 1228 | zlog_err ("bgp_open_receive peer's fd is negative value %d", |
| 1229 | peer->fd); |
| 1230 | return -1; |
| 1231 | } |
| 1232 | BGP_READ_ON (peer->t_read, bgp_read, peer->fd); |
| 1233 | } |
| 1234 | |
| 1235 | /* remote router-id check. */ |
| 1236 | if (remote_id.s_addr == 0 |
| 1237 | || ntohl (remote_id.s_addr) >= 0xe0000000 |
| 1238 | || ntohl (peer->local_id.s_addr) == ntohl (remote_id.s_addr)) |
| 1239 | { |
| 1240 | if (BGP_DEBUG (normal, NORMAL)) |
| 1241 | zlog_info ("%s bad OPEN, wrong router identifier %s", |
| 1242 | peer->host, inet_ntoa (remote_id)); |
| 1243 | bgp_notify_send_with_data (peer, |
| 1244 | BGP_NOTIFY_OPEN_ERR, |
| 1245 | BGP_NOTIFY_OPEN_BAD_BGP_IDENT, |
| 1246 | notify_data_remote_id, 4); |
| 1247 | return -1; |
| 1248 | } |
| 1249 | |
| 1250 | /* Set remote router-id */ |
| 1251 | peer->remote_id = remote_id; |
| 1252 | |
| 1253 | /* Peer BGP version check. */ |
| 1254 | if (version != BGP_VERSION_4) |
| 1255 | { |
| 1256 | if (BGP_DEBUG (normal, NORMAL)) |
| 1257 | zlog_info ("%s bad protocol version, remote requested %d, local request %d", |
| 1258 | peer->host, version, BGP_VERSION_4); |
| 1259 | bgp_notify_send_with_data (peer, |
| 1260 | BGP_NOTIFY_OPEN_ERR, |
| 1261 | BGP_NOTIFY_OPEN_UNSUP_VERSION, |
| 1262 | "\x04", 1); |
| 1263 | return -1; |
| 1264 | } |
| 1265 | |
| 1266 | /* Check neighbor as number. */ |
| 1267 | if (remote_as != peer->as) |
| 1268 | { |
| 1269 | if (BGP_DEBUG (normal, NORMAL)) |
| 1270 | zlog_info ("%s bad OPEN, remote AS is %d, expected %d", |
| 1271 | peer->host, remote_as, peer->as); |
| 1272 | bgp_notify_send_with_data (peer, |
| 1273 | BGP_NOTIFY_OPEN_ERR, |
| 1274 | BGP_NOTIFY_OPEN_BAD_PEER_AS, |
| 1275 | notify_data_remote_as, 2); |
| 1276 | return -1; |
| 1277 | } |
| 1278 | |
| 1279 | /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST |
| 1280 | calculate the value of the Hold Timer by using the smaller of its |
| 1281 | configured Hold Time and the Hold Time received in the OPEN message. |
| 1282 | The Hold Time MUST be either zero or at least three seconds. An |
| 1283 | implementation may reject connections on the basis of the Hold Time. */ |
| 1284 | |
| 1285 | if (holdtime < 3 && holdtime != 0) |
| 1286 | { |
| 1287 | bgp_notify_send (peer, |
| 1288 | BGP_NOTIFY_OPEN_ERR, |
| 1289 | BGP_NOTIFY_OPEN_UNACEP_HOLDTIME); |
| 1290 | return -1; |
| 1291 | } |
| 1292 | |
| 1293 | /* From the rfc: A reasonable maximum time between KEEPALIVE messages |
| 1294 | would be one third of the Hold Time interval. KEEPALIVE messages |
| 1295 | MUST NOT be sent more frequently than one per second. An |
| 1296 | implementation MAY adjust the rate at which it sends KEEPALIVE |
| 1297 | messages as a function of the Hold Time interval. */ |
| 1298 | |
| 1299 | if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER)) |
| 1300 | send_holdtime = peer->holdtime; |
| 1301 | else |
| 1302 | send_holdtime = peer->bgp->default_holdtime; |
| 1303 | |
| 1304 | if (holdtime < send_holdtime) |
| 1305 | peer->v_holdtime = holdtime; |
| 1306 | else |
| 1307 | peer->v_holdtime = send_holdtime; |
| 1308 | |
| 1309 | peer->v_keepalive = peer->v_holdtime / 3; |
| 1310 | |
| 1311 | /* Open option part parse. */ |
| 1312 | capability = 0; |
| 1313 | optlen = stream_getc (peer->ibuf); |
| 1314 | if (optlen != 0) |
| 1315 | { |
| 1316 | ret = bgp_open_option_parse (peer, optlen, &capability); |
| 1317 | if (ret < 0) |
| 1318 | return ret; |
| 1319 | |
| 1320 | stream_forward (peer->ibuf, optlen); |
| 1321 | } |
| 1322 | else |
| 1323 | { |
| 1324 | if (BGP_DEBUG (normal, NORMAL)) |
| 1325 | zlog_info ("%s rcvd OPEN w/ OPTION parameter len: 0", |
| 1326 | peer->host); |
| 1327 | } |
| 1328 | |
| 1329 | /* Override capability. */ |
| 1330 | if (! capability || CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) |
| 1331 | { |
| 1332 | peer->afc_nego[AFI_IP][SAFI_UNICAST] = peer->afc[AFI_IP][SAFI_UNICAST]; |
| 1333 | peer->afc_nego[AFI_IP][SAFI_MULTICAST] = peer->afc[AFI_IP][SAFI_MULTICAST]; |
| 1334 | peer->afc_nego[AFI_IP6][SAFI_UNICAST] = peer->afc[AFI_IP6][SAFI_UNICAST]; |
| 1335 | peer->afc_nego[AFI_IP6][SAFI_MULTICAST] = peer->afc[AFI_IP6][SAFI_MULTICAST]; |
| 1336 | } |
| 1337 | |
| 1338 | /* Get sockname. */ |
| 1339 | bgp_getsockname (peer); |
| 1340 | |
| 1341 | BGP_EVENT_ADD (peer, Receive_OPEN_message); |
| 1342 | |
| 1343 | peer->packet_size = 0; |
| 1344 | if (peer->ibuf) |
| 1345 | stream_reset (peer->ibuf); |
| 1346 | |
| 1347 | return 0; |
| 1348 | } |
| 1349 | |
| 1350 | /* Parse BGP Update packet and make attribute object. */ |
| 1351 | int |
| 1352 | bgp_update_receive (struct peer *peer, bgp_size_t size) |
| 1353 | { |
| 1354 | int ret; |
| 1355 | u_char *end; |
| 1356 | struct stream *s; |
| 1357 | struct attr attr; |
| 1358 | bgp_size_t attribute_len; |
| 1359 | bgp_size_t update_len; |
| 1360 | bgp_size_t withdraw_len; |
| 1361 | struct bgp_nlri update; |
| 1362 | struct bgp_nlri withdraw; |
| 1363 | struct bgp_nlri mp_update; |
| 1364 | struct bgp_nlri mp_withdraw; |
| 1365 | char attrstr[BUFSIZ]; |
| 1366 | |
| 1367 | /* Status must be Established. */ |
| 1368 | if (peer->status != Established) |
| 1369 | { |
| 1370 | zlog_err ("%s [FSM] Update packet received under status %s", |
| 1371 | peer->host, LOOKUP (bgp_status_msg, peer->status)); |
| 1372 | bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0); |
| 1373 | return -1; |
| 1374 | } |
| 1375 | |
| 1376 | /* Set initial values. */ |
| 1377 | memset (&attr, 0, sizeof (struct attr)); |
| 1378 | memset (&update, 0, sizeof (struct bgp_nlri)); |
| 1379 | memset (&withdraw, 0, sizeof (struct bgp_nlri)); |
| 1380 | memset (&mp_update, 0, sizeof (struct bgp_nlri)); |
| 1381 | memset (&mp_withdraw, 0, sizeof (struct bgp_nlri)); |
| 1382 | |
| 1383 | s = peer->ibuf; |
| 1384 | end = stream_pnt (s) + size; |
| 1385 | |
| 1386 | /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute |
| 1387 | Length is too large (i.e., if Unfeasible Routes Length + Total |
| 1388 | Attribute Length + 23 exceeds the message Length), then the Error |
| 1389 | Subcode is set to Malformed Attribute List. */ |
| 1390 | if (stream_pnt (s) + 2 > end) |
| 1391 | { |
| 1392 | zlog_err ("%s [Error] Update packet error" |
| 1393 | " (packet length is short for unfeasible length)", |
| 1394 | peer->host); |
| 1395 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, |
| 1396 | BGP_NOTIFY_UPDATE_MAL_ATTR); |
| 1397 | return -1; |
| 1398 | } |
| 1399 | |
| 1400 | /* Unfeasible Route Length. */ |
| 1401 | withdraw_len = stream_getw (s); |
| 1402 | |
| 1403 | /* Unfeasible Route Length check. */ |
| 1404 | if (stream_pnt (s) + withdraw_len > end) |
| 1405 | { |
| 1406 | zlog_err ("%s [Error] Update packet error" |
| 1407 | " (packet unfeasible length overflow %d)", |
| 1408 | peer->host, withdraw_len); |
| 1409 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, |
| 1410 | BGP_NOTIFY_UPDATE_MAL_ATTR); |
| 1411 | return -1; |
| 1412 | } |
| 1413 | |
| 1414 | /* Unfeasible Route packet format check. */ |
| 1415 | if (withdraw_len > 0) |
| 1416 | { |
| 1417 | ret = bgp_nlri_sanity_check (peer, AFI_IP, stream_pnt (s), withdraw_len); |
| 1418 | if (ret < 0) |
| 1419 | return -1; |
| 1420 | |
| 1421 | if (BGP_DEBUG (packet, PACKET_RECV)) |
| 1422 | zlog_info ("%s [Update:RECV] Unfeasible NLRI received", peer->host); |
| 1423 | |
| 1424 | withdraw.afi = AFI_IP; |
| 1425 | withdraw.safi = SAFI_UNICAST; |
| 1426 | withdraw.nlri = stream_pnt (s); |
| 1427 | withdraw.length = withdraw_len; |
| 1428 | stream_forward (s, withdraw_len); |
| 1429 | } |
| 1430 | |
| 1431 | /* Attribute total length check. */ |
| 1432 | if (stream_pnt (s) + 2 > end) |
| 1433 | { |
| 1434 | zlog_warn ("%s [Error] Packet Error" |
| 1435 | " (update packet is short for attribute length)", |
| 1436 | peer->host); |
| 1437 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, |
| 1438 | BGP_NOTIFY_UPDATE_MAL_ATTR); |
| 1439 | return -1; |
| 1440 | } |
| 1441 | |
| 1442 | /* Fetch attribute total length. */ |
| 1443 | attribute_len = stream_getw (s); |
| 1444 | |
| 1445 | /* Attribute length check. */ |
| 1446 | if (stream_pnt (s) + attribute_len > end) |
| 1447 | { |
| 1448 | zlog_warn ("%s [Error] Packet Error" |
| 1449 | " (update packet attribute length overflow %d)", |
| 1450 | peer->host, attribute_len); |
| 1451 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, |
| 1452 | BGP_NOTIFY_UPDATE_MAL_ATTR); |
| 1453 | return -1; |
| 1454 | } |
| 1455 | |
| 1456 | /* Parse attribute when it exists. */ |
| 1457 | if (attribute_len) |
| 1458 | { |
| 1459 | ret = bgp_attr_parse (peer, &attr, attribute_len, |
| 1460 | &mp_update, &mp_withdraw); |
| 1461 | if (ret < 0) |
| 1462 | return -1; |
| 1463 | } |
| 1464 | |
| 1465 | /* Logging the attribute. */ |
| 1466 | if (BGP_DEBUG (update, UPDATE_IN)) |
| 1467 | { |
| 1468 | bgp_dump_attr (peer, &attr, attrstr, BUFSIZ); |
| 1469 | zlog (peer->log, LOG_INFO, "%s rcvd UPDATE w/ attr: %s", |
| 1470 | peer->host, attrstr); |
| 1471 | } |
| 1472 | |
| 1473 | /* Network Layer Reachability Information. */ |
| 1474 | update_len = end - stream_pnt (s); |
| 1475 | |
| 1476 | if (update_len) |
| 1477 | { |
| 1478 | /* Check NLRI packet format and prefix length. */ |
| 1479 | ret = bgp_nlri_sanity_check (peer, AFI_IP, stream_pnt (s), update_len); |
| 1480 | if (ret < 0) |
| 1481 | return -1; |
| 1482 | |
| 1483 | /* Set NLRI portion to structure. */ |
| 1484 | update.afi = AFI_IP; |
| 1485 | update.safi = SAFI_UNICAST; |
| 1486 | update.nlri = stream_pnt (s); |
| 1487 | update.length = update_len; |
| 1488 | stream_forward (s, update_len); |
| 1489 | } |
| 1490 | |
| 1491 | /* NLRI is processed only when the peer is configured specific |
| 1492 | Address Family and Subsequent Address Family. */ |
| 1493 | if (peer->afc[AFI_IP][SAFI_UNICAST]) |
| 1494 | { |
| 1495 | if (withdraw.length) |
| 1496 | bgp_nlri_parse (peer, NULL, &withdraw); |
| 1497 | |
| 1498 | if (update.length) |
| 1499 | { |
| 1500 | /* We check well-known attribute only for IPv4 unicast |
| 1501 | update. */ |
| 1502 | ret = bgp_attr_check (peer, &attr); |
| 1503 | if (ret < 0) |
| 1504 | return -1; |
| 1505 | |
| 1506 | bgp_nlri_parse (peer, &attr, &update); |
| 1507 | } |
| 1508 | } |
| 1509 | if (peer->afc[AFI_IP][SAFI_MULTICAST]) |
| 1510 | { |
| 1511 | if (mp_update.length |
| 1512 | && mp_update.afi == AFI_IP |
| 1513 | && mp_update.safi == SAFI_MULTICAST) |
| 1514 | bgp_nlri_parse (peer, &attr, &mp_update); |
| 1515 | |
| 1516 | if (mp_withdraw.length |
| 1517 | && mp_withdraw.afi == AFI_IP |
| 1518 | && mp_withdraw.safi == SAFI_MULTICAST) |
| 1519 | bgp_nlri_parse (peer, NULL, &mp_withdraw); |
| 1520 | } |
| 1521 | if (peer->afc[AFI_IP6][SAFI_UNICAST]) |
| 1522 | { |
| 1523 | if (mp_update.length |
| 1524 | && mp_update.afi == AFI_IP6 |
| 1525 | && mp_update.safi == SAFI_UNICAST) |
| 1526 | bgp_nlri_parse (peer, &attr, &mp_update); |
| 1527 | |
| 1528 | if (mp_withdraw.length |
| 1529 | && mp_withdraw.afi == AFI_IP6 |
| 1530 | && mp_withdraw.safi == SAFI_UNICAST) |
| 1531 | bgp_nlri_parse (peer, NULL, &mp_withdraw); |
| 1532 | } |
| 1533 | if (peer->afc[AFI_IP6][SAFI_MULTICAST]) |
| 1534 | { |
| 1535 | if (mp_update.length |
| 1536 | && mp_update.afi == AFI_IP6 |
| 1537 | && mp_update.safi == SAFI_MULTICAST) |
| 1538 | bgp_nlri_parse (peer, &attr, &mp_update); |
| 1539 | |
| 1540 | if (mp_withdraw.length |
| 1541 | && mp_withdraw.afi == AFI_IP6 |
| 1542 | && mp_withdraw.safi == SAFI_MULTICAST) |
| 1543 | bgp_nlri_parse (peer, NULL, &mp_withdraw); |
| 1544 | } |
| 1545 | if (peer->afc[AFI_IP][SAFI_MPLS_VPN]) |
| 1546 | { |
| 1547 | if (mp_update.length |
| 1548 | && mp_update.afi == AFI_IP |
| 1549 | && mp_update.safi == BGP_SAFI_VPNV4) |
| 1550 | bgp_nlri_parse_vpnv4 (peer, &attr, &mp_update); |
| 1551 | |
| 1552 | if (mp_withdraw.length |
| 1553 | && mp_withdraw.afi == AFI_IP |
| 1554 | && mp_withdraw.safi == BGP_SAFI_VPNV4) |
| 1555 | bgp_nlri_parse_vpnv4 (peer, NULL, &mp_withdraw); |
| 1556 | } |
| 1557 | |
| 1558 | /* Everything is done. We unintern temporary structures which |
| 1559 | interned in bgp_attr_parse(). */ |
| 1560 | if (attr.aspath) |
| 1561 | aspath_unintern (attr.aspath); |
| 1562 | if (attr.community) |
| 1563 | community_unintern (attr.community); |
| 1564 | if (attr.ecommunity) |
| 1565 | ecommunity_unintern (attr.ecommunity); |
| 1566 | if (attr.cluster) |
| 1567 | cluster_unintern (attr.cluster); |
| 1568 | if (attr.transit) |
| 1569 | transit_unintern (attr.transit); |
| 1570 | |
| 1571 | /* If peering is stopped due to some reason, do not generate BGP |
| 1572 | event. */ |
| 1573 | if (peer->status != Established) |
| 1574 | return 0; |
| 1575 | |
| 1576 | /* Increment packet counter. */ |
| 1577 | peer->update_in++; |
| 1578 | peer->update_time = time (NULL); |
| 1579 | |
| 1580 | /* Generate BGP event. */ |
| 1581 | BGP_EVENT_ADD (peer, Receive_UPDATE_message); |
| 1582 | |
| 1583 | return 0; |
| 1584 | } |
| 1585 | |
| 1586 | /* Notify message treatment function. */ |
| 1587 | void |
| 1588 | bgp_notify_receive (struct peer *peer, bgp_size_t size) |
| 1589 | { |
| 1590 | struct bgp_notify bgp_notify; |
| 1591 | |
| 1592 | if (peer->notify.data) |
| 1593 | { |
| 1594 | XFREE (MTYPE_TMP, peer->notify.data); |
| 1595 | peer->notify.data = NULL; |
| 1596 | peer->notify.length = 0; |
| 1597 | } |
| 1598 | |
| 1599 | bgp_notify.code = stream_getc (peer->ibuf); |
| 1600 | bgp_notify.subcode = stream_getc (peer->ibuf); |
| 1601 | bgp_notify.length = size - 2; |
| 1602 | bgp_notify.data = NULL; |
| 1603 | |
| 1604 | /* Preserv notify code and sub code. */ |
| 1605 | peer->notify.code = bgp_notify.code; |
| 1606 | peer->notify.subcode = bgp_notify.subcode; |
| 1607 | /* For further diagnostic record returned Data. */ |
| 1608 | if (bgp_notify.length) |
| 1609 | { |
| 1610 | peer->notify.length = size - 2; |
| 1611 | peer->notify.data = XMALLOC (MTYPE_TMP, size - 2); |
| 1612 | memcpy (peer->notify.data, stream_pnt (peer->ibuf), size - 2); |
| 1613 | } |
| 1614 | |
| 1615 | /* For debug */ |
| 1616 | { |
| 1617 | int i; |
| 1618 | int first = 0; |
| 1619 | char c[4]; |
| 1620 | |
| 1621 | if (bgp_notify.length) |
| 1622 | { |
| 1623 | bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3); |
| 1624 | for (i = 0; i < bgp_notify.length; i++) |
| 1625 | if (first) |
| 1626 | { |
| 1627 | sprintf (c, " %02x", stream_getc (peer->ibuf)); |
| 1628 | strcat (bgp_notify.data, c); |
| 1629 | } |
| 1630 | else |
| 1631 | { |
| 1632 | first = 1; |
| 1633 | sprintf (c, "%02x", stream_getc (peer->ibuf)); |
| 1634 | strcpy (bgp_notify.data, c); |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | bgp_notify_print(peer, &bgp_notify, "received"); |
| 1639 | if (bgp_notify.data) |
| 1640 | XFREE (MTYPE_TMP, bgp_notify.data); |
| 1641 | } |
| 1642 | |
| 1643 | /* peer count update */ |
| 1644 | peer->notify_in++; |
| 1645 | |
| 1646 | /* We have to check for Notify with Unsupported Optional Parameter. |
| 1647 | in that case we fallback to open without the capability option. |
| 1648 | But this done in bgp_stop. We just mark it here to avoid changing |
| 1649 | the fsm tables. */ |
| 1650 | if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR && |
| 1651 | bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM ) |
| 1652 | UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN); |
| 1653 | |
| 1654 | /* Also apply to Unsupported Capability until remote router support |
| 1655 | capability. */ |
| 1656 | if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR && |
| 1657 | bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_CAPBL) |
| 1658 | UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN); |
| 1659 | |
| 1660 | BGP_EVENT_ADD (peer, Receive_NOTIFICATION_message); |
| 1661 | } |
| 1662 | |
| 1663 | /* Keepalive treatment function -- get keepalive send keepalive */ |
| 1664 | void |
| 1665 | bgp_keepalive_receive (struct peer *peer, bgp_size_t size) |
| 1666 | { |
| 1667 | if (BGP_DEBUG (keepalive, KEEPALIVE)) |
| 1668 | zlog_info ("%s KEEPALIVE rcvd", peer->host); |
| 1669 | |
| 1670 | BGP_EVENT_ADD (peer, Receive_KEEPALIVE_message); |
| 1671 | } |
| 1672 | |
| 1673 | /* Route refresh message is received. */ |
| 1674 | void |
| 1675 | bgp_route_refresh_receive (struct peer *peer, bgp_size_t size) |
| 1676 | { |
| 1677 | afi_t afi; |
| 1678 | safi_t safi; |
| 1679 | u_char reserved; |
| 1680 | struct stream *s; |
| 1681 | |
| 1682 | /* If peer does not have the capability, send notification. */ |
| 1683 | if (! CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_ADV)) |
| 1684 | { |
| 1685 | plog_err (peer->log, "%s [Error] BGP route refresh is not enabled", |
| 1686 | peer->host); |
| 1687 | bgp_notify_send (peer, |
| 1688 | BGP_NOTIFY_HEADER_ERR, |
| 1689 | BGP_NOTIFY_HEADER_BAD_MESTYPE); |
| 1690 | return; |
| 1691 | } |
| 1692 | |
| 1693 | /* Status must be Established. */ |
| 1694 | if (peer->status != Established) |
| 1695 | { |
| 1696 | plog_err (peer->log, |
| 1697 | "%s [Error] Route refresh packet received under status %s", |
| 1698 | peer->host, LOOKUP (bgp_status_msg, peer->status)); |
| 1699 | bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0); |
| 1700 | return; |
| 1701 | } |
| 1702 | |
| 1703 | s = peer->ibuf; |
| 1704 | |
| 1705 | /* Parse packet. */ |
| 1706 | afi = stream_getw (s); |
| 1707 | reserved = stream_getc (s); |
| 1708 | safi = stream_getc (s); |
| 1709 | |
| 1710 | if (BGP_DEBUG (normal, NORMAL)) |
| 1711 | zlog_info ("%s rcvd REFRESH_REQ for afi/safi: %d/%d", |
| 1712 | peer->host, afi, safi); |
| 1713 | |
| 1714 | /* Check AFI and SAFI. */ |
| 1715 | if ((afi != AFI_IP && afi != AFI_IP6) |
| 1716 | || (safi != SAFI_UNICAST && safi != SAFI_MULTICAST |
| 1717 | && safi != BGP_SAFI_VPNV4)) |
| 1718 | { |
| 1719 | if (BGP_DEBUG (normal, NORMAL)) |
| 1720 | { |
| 1721 | zlog_info ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored", |
| 1722 | peer->host, afi, safi); |
| 1723 | } |
| 1724 | return; |
| 1725 | } |
| 1726 | |
| 1727 | /* Adjust safi code. */ |
| 1728 | if (safi == BGP_SAFI_VPNV4) |
| 1729 | safi = SAFI_MPLS_VPN; |
| 1730 | |
| 1731 | if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) |
| 1732 | { |
| 1733 | u_char *end; |
| 1734 | u_char when_to_refresh; |
| 1735 | u_char orf_type; |
| 1736 | u_int16_t orf_len; |
| 1737 | |
| 1738 | if (size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) < 5) |
| 1739 | { |
| 1740 | zlog_info ("%s ORF route refresh length error", peer->host); |
| 1741 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 1742 | return; |
| 1743 | } |
| 1744 | |
| 1745 | when_to_refresh = stream_getc (s); |
| 1746 | end = stream_pnt (s) + (size - 5); |
| 1747 | |
| 1748 | while (stream_pnt (s) < end) |
| 1749 | { |
| 1750 | orf_type = stream_getc (s); |
| 1751 | orf_len = stream_getw (s); |
| 1752 | |
| 1753 | if (orf_type == ORF_TYPE_PREFIX |
| 1754 | || orf_type == ORF_TYPE_PREFIX_OLD) |
| 1755 | { |
| 1756 | u_char *p_pnt = stream_pnt (s); |
| 1757 | u_char *p_end = stream_pnt (s) + orf_len; |
| 1758 | struct orf_prefix orfp; |
| 1759 | u_char common = 0; |
| 1760 | u_int32_t seq; |
| 1761 | int psize; |
| 1762 | char name[BUFSIZ]; |
| 1763 | char buf[BUFSIZ]; |
| 1764 | int ret; |
| 1765 | |
| 1766 | if (BGP_DEBUG (normal, NORMAL)) |
| 1767 | { |
| 1768 | zlog_info ("%s rcvd Prefixlist ORF(%d) length %d", |
| 1769 | peer->host, orf_type, orf_len); |
| 1770 | } |
| 1771 | |
| 1772 | /* ORF prefix-list name */ |
| 1773 | sprintf (name, "%s.%d.%d", peer->host, afi, safi); |
| 1774 | |
| 1775 | while (p_pnt < p_end) |
| 1776 | { |
| 1777 | memset (&orfp, 0, sizeof (struct orf_prefix)); |
| 1778 | common = *p_pnt++; |
| 1779 | if (common & ORF_COMMON_PART_REMOVE_ALL) |
| 1780 | { |
| 1781 | if (BGP_DEBUG (normal, NORMAL)) |
| 1782 | zlog_info ("%s rcvd Remove-All pfxlist ORF request", peer->host); |
| 1783 | prefix_bgp_orf_remove_all (name); |
| 1784 | break; |
| 1785 | } |
| 1786 | memcpy (&seq, p_pnt, sizeof (u_int32_t)); |
| 1787 | p_pnt += sizeof (u_int32_t); |
| 1788 | orfp.seq = ntohl (seq); |
| 1789 | orfp.ge = *p_pnt++; |
| 1790 | orfp.le = *p_pnt++; |
| 1791 | orfp.p.prefixlen = *p_pnt++; |
| 1792 | orfp.p.family = afi2family (afi); |
| 1793 | psize = PSIZE (orfp.p.prefixlen); |
| 1794 | memcpy (&orfp.p.u.prefix, p_pnt, psize); |
| 1795 | p_pnt += psize; |
| 1796 | |
| 1797 | if (BGP_DEBUG (normal, NORMAL)) |
| 1798 | zlog_info ("%s rcvd %s %s seq %u %s/%d ge %d le %d", |
| 1799 | peer->host, |
| 1800 | (common & ORF_COMMON_PART_REMOVE ? "Remove" : "Add"), |
| 1801 | (common & ORF_COMMON_PART_DENY ? "deny" : "permit"), |
| 1802 | orfp.seq, |
| 1803 | inet_ntop (orfp.p.family, &orfp.p.u.prefix, buf, BUFSIZ), |
| 1804 | orfp.p.prefixlen, orfp.ge, orfp.le); |
| 1805 | |
| 1806 | ret = prefix_bgp_orf_set (name, afi, &orfp, |
| 1807 | (common & ORF_COMMON_PART_DENY ? 0 : 1 ), |
| 1808 | (common & ORF_COMMON_PART_REMOVE ? 0 : 1)); |
| 1809 | |
| 1810 | if (ret != CMD_SUCCESS) |
| 1811 | { |
| 1812 | if (BGP_DEBUG (normal, NORMAL)) |
| 1813 | zlog_info ("%s Received misformatted prefixlist ORF. Remove All pfxlist", peer->host); |
| 1814 | prefix_bgp_orf_remove_all (name); |
| 1815 | break; |
| 1816 | } |
| 1817 | } |
| 1818 | peer->orf_plist[afi][safi] = |
| 1819 | prefix_list_lookup (AFI_ORF_PREFIX, name); |
| 1820 | } |
| 1821 | stream_forward (s, orf_len); |
| 1822 | } |
| 1823 | if (BGP_DEBUG (normal, NORMAL)) |
| 1824 | zlog_info ("%s rcvd Refresh %s ORF request", peer->host, |
| 1825 | when_to_refresh == REFRESH_DEFER ? "Defer" : "Immediate"); |
| 1826 | if (when_to_refresh == REFRESH_DEFER) |
| 1827 | return; |
| 1828 | } |
| 1829 | |
| 1830 | /* First update is deferred until ORF or ROUTE-REFRESH is received */ |
| 1831 | if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH)) |
| 1832 | UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH); |
| 1833 | |
| 1834 | /* Perform route refreshment to the peer */ |
| 1835 | bgp_announce_route (peer, afi, safi); |
| 1836 | } |
| 1837 | |
| 1838 | int |
| 1839 | bgp_capability_msg_parse (struct peer *peer, u_char *pnt, bgp_size_t length) |
| 1840 | { |
| 1841 | u_char *end; |
| 1842 | struct capability cap; |
| 1843 | u_char action; |
| 1844 | struct bgp *bgp; |
| 1845 | afi_t afi; |
| 1846 | safi_t safi; |
| 1847 | |
| 1848 | bgp = peer->bgp; |
| 1849 | end = pnt + length; |
| 1850 | |
| 1851 | while (pnt < end) |
| 1852 | { |
| 1853 | /* We need at least action, capability code and capability length. */ |
| 1854 | if (pnt + 3 > end) |
| 1855 | { |
| 1856 | zlog_info ("%s Capability length error", peer->host); |
| 1857 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 1858 | return -1; |
| 1859 | } |
| 1860 | |
| 1861 | action = *pnt; |
| 1862 | |
| 1863 | /* Fetch structure to the byte stream. */ |
| 1864 | memcpy (&cap, pnt + 1, sizeof (struct capability)); |
| 1865 | |
| 1866 | /* Action value check. */ |
| 1867 | if (action != CAPABILITY_ACTION_SET |
| 1868 | && action != CAPABILITY_ACTION_UNSET) |
| 1869 | { |
| 1870 | zlog_info ("%s Capability Action Value error %d", |
| 1871 | peer->host, action); |
| 1872 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 1873 | return -1; |
| 1874 | } |
| 1875 | |
| 1876 | if (BGP_DEBUG (normal, NORMAL)) |
| 1877 | zlog_info ("%s CAPABILITY has action: %d, code: %u, length %u", |
| 1878 | peer->host, action, cap.code, cap.length); |
| 1879 | |
| 1880 | /* Capability length check. */ |
| 1881 | if (pnt + (cap.length + 3) > end) |
| 1882 | { |
| 1883 | zlog_info ("%s Capability length error", peer->host); |
| 1884 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 1885 | return -1; |
| 1886 | } |
| 1887 | |
| 1888 | /* We know MP Capability Code. */ |
| 1889 | if (cap.code == CAPABILITY_CODE_MP) |
| 1890 | { |
| 1891 | afi = ntohs (cap.mpc.afi); |
| 1892 | safi = cap.mpc.safi; |
| 1893 | |
| 1894 | /* Ignore capability when override-capability is set. */ |
| 1895 | if (CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) |
| 1896 | continue; |
| 1897 | |
| 1898 | /* Address family check. */ |
| 1899 | if ((afi == AFI_IP |
| 1900 | || afi == AFI_IP6) |
| 1901 | && (safi == SAFI_UNICAST |
| 1902 | || safi == SAFI_MULTICAST |
| 1903 | || safi == BGP_SAFI_VPNV4)) |
| 1904 | { |
| 1905 | if (BGP_DEBUG (normal, NORMAL)) |
| 1906 | zlog_info ("%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u", |
| 1907 | peer->host, |
| 1908 | action == CAPABILITY_ACTION_SET |
| 1909 | ? "Advertising" : "Removing", |
| 1910 | ntohs(cap.mpc.afi) , cap.mpc.safi); |
| 1911 | |
| 1912 | /* Adjust safi code. */ |
| 1913 | if (safi == BGP_SAFI_VPNV4) |
| 1914 | safi = SAFI_MPLS_VPN; |
| 1915 | |
| 1916 | if (action == CAPABILITY_ACTION_SET) |
| 1917 | { |
| 1918 | peer->afc_recv[afi][safi] = 1; |
| 1919 | if (peer->afc[afi][safi]) |
| 1920 | { |
| 1921 | peer->afc_nego[afi][safi] = 1; |
| 1922 | bgp_announce_route (peer, afi, safi); |
| 1923 | } |
| 1924 | } |
| 1925 | else |
| 1926 | { |
| 1927 | peer->afc_recv[afi][safi] = 0; |
| 1928 | peer->afc_nego[afi][safi] = 0; |
| 1929 | |
| 1930 | if (peer_active_nego (peer)) |
| 1931 | bgp_clear_route (peer, afi, safi); |
| 1932 | else |
| 1933 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 1934 | } |
| 1935 | } |
| 1936 | } |
| 1937 | else if (cap.code == CAPABILITY_CODE_REFRESH |
| 1938 | || cap.code == CAPABILITY_CODE_REFRESH_OLD) |
| 1939 | { |
| 1940 | /* Check length. */ |
| 1941 | if (cap.length != 0) |
| 1942 | { |
| 1943 | zlog_info ("%s Route Refresh Capability length error %d", |
| 1944 | peer->host, cap.length); |
| 1945 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 1946 | return -1; |
| 1947 | } |
| 1948 | |
| 1949 | if (BGP_DEBUG (normal, NORMAL)) |
| 1950 | zlog_info ("%s CAPABILITY has %s ROUTE-REFRESH capability(%s) for all address-families", |
| 1951 | peer->host, |
| 1952 | action == CAPABILITY_ACTION_SET |
| 1953 | ? "Advertising" : "Removing", |
| 1954 | cap.code == CAPABILITY_CODE_REFRESH_OLD |
| 1955 | ? "old" : "new"); |
| 1956 | |
| 1957 | /* BGP refresh capability */ |
| 1958 | if (action == CAPABILITY_ACTION_SET) |
| 1959 | { |
| 1960 | if (cap.code == CAPABILITY_CODE_REFRESH_OLD) |
| 1961 | SET_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV); |
| 1962 | else |
| 1963 | SET_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV); |
| 1964 | } |
| 1965 | else |
| 1966 | { |
| 1967 | if (cap.code == CAPABILITY_CODE_REFRESH_OLD) |
| 1968 | UNSET_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV); |
| 1969 | else |
| 1970 | UNSET_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV); |
| 1971 | } |
| 1972 | } |
| 1973 | else |
| 1974 | { |
| 1975 | zlog_warn ("%s unrecognized capability code: %d - ignored", |
| 1976 | peer->host, cap.code); |
| 1977 | } |
| 1978 | pnt += cap.length + 3; |
| 1979 | } |
| 1980 | return 0; |
| 1981 | } |
| 1982 | |
| 1983 | /* Dynamic Capability is received. */ |
| 1984 | void |
| 1985 | bgp_capability_receive (struct peer *peer, bgp_size_t size) |
| 1986 | { |
| 1987 | u_char *pnt; |
| 1988 | int ret; |
| 1989 | |
| 1990 | /* Fetch pointer. */ |
| 1991 | pnt = stream_pnt (peer->ibuf); |
| 1992 | |
| 1993 | if (BGP_DEBUG (normal, NORMAL)) |
| 1994 | zlog_info ("%s rcv CAPABILITY", peer->host); |
| 1995 | |
| 1996 | /* If peer does not have the capability, send notification. */ |
| 1997 | if (! CHECK_FLAG (peer->cap, PEER_CAP_DYNAMIC_ADV)) |
| 1998 | { |
| 1999 | plog_err (peer->log, "%s [Error] BGP dynamic capability is not enabled", |
| 2000 | peer->host); |
| 2001 | bgp_notify_send (peer, |
| 2002 | BGP_NOTIFY_HEADER_ERR, |
| 2003 | BGP_NOTIFY_HEADER_BAD_MESTYPE); |
| 2004 | return; |
| 2005 | } |
| 2006 | |
| 2007 | /* Status must be Established. */ |
| 2008 | if (peer->status != Established) |
| 2009 | { |
| 2010 | plog_err (peer->log, |
| 2011 | "%s [Error] Dynamic capability packet received under status %s", peer->host, LOOKUP (bgp_status_msg, peer->status)); |
| 2012 | bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0); |
| 2013 | return; |
| 2014 | } |
| 2015 | |
| 2016 | /* Parse packet. */ |
| 2017 | ret = bgp_capability_msg_parse (peer, pnt, size); |
| 2018 | } |
| 2019 | |
| 2020 | /* BGP read utility function. */ |
| 2021 | int |
| 2022 | bgp_read_packet (struct peer *peer) |
| 2023 | { |
| 2024 | int nbytes; |
| 2025 | int readsize; |
| 2026 | |
| 2027 | readsize = peer->packet_size - peer->ibuf->putp; |
| 2028 | |
| 2029 | /* If size is zero then return. */ |
| 2030 | if (! readsize) |
| 2031 | return 0; |
| 2032 | |
| 2033 | /* Read packet from fd. */ |
| 2034 | nbytes = stream_read_unblock (peer->ibuf, peer->fd, readsize); |
| 2035 | |
| 2036 | /* If read byte is smaller than zero then error occured. */ |
| 2037 | if (nbytes < 0) |
| 2038 | { |
| 2039 | if (errno == EAGAIN) |
| 2040 | return -1; |
| 2041 | |
| 2042 | plog_err (peer->log, "%s [Error] bgp_read_packet error: %s", |
| 2043 | peer->host, strerror (errno)); |
| 2044 | BGP_EVENT_ADD (peer, TCP_fatal_error); |
| 2045 | return -1; |
| 2046 | } |
| 2047 | |
| 2048 | /* When read byte is zero : clear bgp peer and return */ |
| 2049 | if (nbytes == 0) |
| 2050 | { |
| 2051 | if (BGP_DEBUG (events, EVENTS)) |
| 2052 | plog_info (peer->log, "%s [Event] BGP connection closed fd %d", |
| 2053 | peer->host, peer->fd); |
| 2054 | BGP_EVENT_ADD (peer, TCP_connection_closed); |
| 2055 | return -1; |
| 2056 | } |
| 2057 | |
| 2058 | /* We read partial packet. */ |
| 2059 | if (peer->ibuf->putp != peer->packet_size) |
| 2060 | return -1; |
| 2061 | |
| 2062 | return 0; |
| 2063 | } |
| 2064 | |
| 2065 | /* Marker check. */ |
| 2066 | int |
| 2067 | bgp_marker_all_one (struct stream *s, int length) |
| 2068 | { |
| 2069 | int i; |
| 2070 | |
| 2071 | for (i = 0; i < length; i++) |
| 2072 | if (s->data[i] != 0xff) |
| 2073 | return 0; |
| 2074 | |
| 2075 | return 1; |
| 2076 | } |
| 2077 | |
| 2078 | /* Starting point of packet process function. */ |
| 2079 | int |
| 2080 | bgp_read (struct thread *thread) |
| 2081 | { |
| 2082 | int ret; |
| 2083 | u_char type = 0; |
| 2084 | struct peer *peer; |
| 2085 | bgp_size_t size; |
| 2086 | char notify_data_length[2]; |
| 2087 | |
| 2088 | /* Yes first of all get peer pointer. */ |
| 2089 | peer = THREAD_ARG (thread); |
| 2090 | peer->t_read = NULL; |
| 2091 | |
| 2092 | /* For non-blocking IO check. */ |
| 2093 | if (peer->status == Connect) |
| 2094 | { |
| 2095 | bgp_connect_check (peer); |
| 2096 | goto done; |
| 2097 | } |
| 2098 | else |
| 2099 | { |
| 2100 | if (peer->fd < 0) |
| 2101 | { |
| 2102 | zlog_err ("bgp_read peer's fd is negative value %d", peer->fd); |
| 2103 | return -1; |
| 2104 | } |
| 2105 | BGP_READ_ON (peer->t_read, bgp_read, peer->fd); |
| 2106 | } |
| 2107 | |
| 2108 | /* Read packet header to determine type of the packet */ |
| 2109 | if (peer->packet_size == 0) |
| 2110 | peer->packet_size = BGP_HEADER_SIZE; |
| 2111 | |
| 2112 | if (peer->ibuf->putp < BGP_HEADER_SIZE) |
| 2113 | { |
| 2114 | ret = bgp_read_packet (peer); |
| 2115 | |
| 2116 | /* Header read error or partial read packet. */ |
| 2117 | if (ret < 0) |
| 2118 | goto done; |
| 2119 | |
| 2120 | /* Get size and type. */ |
| 2121 | stream_forward (peer->ibuf, BGP_MARKER_SIZE); |
| 2122 | memcpy (notify_data_length, stream_pnt (peer->ibuf), 2); |
| 2123 | size = stream_getw (peer->ibuf); |
| 2124 | type = stream_getc (peer->ibuf); |
| 2125 | |
| 2126 | if (BGP_DEBUG (normal, NORMAL) && type != 2 && type != 0) |
| 2127 | zlog_info ("%s rcv message type %d, length (excl. header) %d", |
| 2128 | peer->host, type, size - BGP_HEADER_SIZE); |
| 2129 | |
| 2130 | /* Marker check */ |
| 2131 | if (type == BGP_MSG_OPEN |
| 2132 | && ! bgp_marker_all_one (peer->ibuf, BGP_MARKER_SIZE)) |
| 2133 | { |
| 2134 | bgp_notify_send (peer, |
| 2135 | BGP_NOTIFY_HEADER_ERR, |
| 2136 | BGP_NOTIFY_HEADER_NOT_SYNC); |
| 2137 | goto done; |
| 2138 | } |
| 2139 | |
| 2140 | /* BGP type check. */ |
| 2141 | if (type != BGP_MSG_OPEN && type != BGP_MSG_UPDATE |
| 2142 | && type != BGP_MSG_NOTIFY && type != BGP_MSG_KEEPALIVE |
| 2143 | && type != BGP_MSG_ROUTE_REFRESH_NEW |
| 2144 | && type != BGP_MSG_ROUTE_REFRESH_OLD |
| 2145 | && type != BGP_MSG_CAPABILITY) |
| 2146 | { |
| 2147 | if (BGP_DEBUG (normal, NORMAL)) |
| 2148 | plog_err (peer->log, |
| 2149 | "%s unknown message type 0x%02x", |
| 2150 | peer->host, type); |
| 2151 | bgp_notify_send_with_data (peer, |
| 2152 | BGP_NOTIFY_HEADER_ERR, |
| 2153 | BGP_NOTIFY_HEADER_BAD_MESTYPE, |
| 2154 | &type, 1); |
| 2155 | goto done; |
| 2156 | } |
| 2157 | /* Mimimum packet length check. */ |
| 2158 | if ((size < BGP_HEADER_SIZE) |
| 2159 | || (size > BGP_MAX_PACKET_SIZE) |
| 2160 | || (type == BGP_MSG_OPEN && size < BGP_MSG_OPEN_MIN_SIZE) |
| 2161 | || (type == BGP_MSG_UPDATE && size < BGP_MSG_UPDATE_MIN_SIZE) |
| 2162 | || (type == BGP_MSG_NOTIFY && size < BGP_MSG_NOTIFY_MIN_SIZE) |
| 2163 | || (type == BGP_MSG_KEEPALIVE && size != BGP_MSG_KEEPALIVE_MIN_SIZE) |
| 2164 | || (type == BGP_MSG_ROUTE_REFRESH_NEW && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE) |
| 2165 | || (type == BGP_MSG_ROUTE_REFRESH_OLD && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE) |
| 2166 | || (type == BGP_MSG_CAPABILITY && size < BGP_MSG_CAPABILITY_MIN_SIZE)) |
| 2167 | { |
| 2168 | if (BGP_DEBUG (normal, NORMAL)) |
| 2169 | plog_err (peer->log, |
| 2170 | "%s bad message length - %d for %s", |
| 2171 | peer->host, size, |
| 2172 | type == 128 ? "ROUTE-REFRESH" : |
| 2173 | bgp_type_str[(int) type]); |
| 2174 | bgp_notify_send_with_data (peer, |
| 2175 | BGP_NOTIFY_HEADER_ERR, |
| 2176 | BGP_NOTIFY_HEADER_BAD_MESLEN, |
| 2177 | notify_data_length, 2); |
| 2178 | goto done; |
| 2179 | } |
| 2180 | |
| 2181 | /* Adjust size to message length. */ |
| 2182 | peer->packet_size = size; |
| 2183 | } |
| 2184 | |
| 2185 | ret = bgp_read_packet (peer); |
| 2186 | if (ret < 0) |
| 2187 | goto done; |
| 2188 | |
| 2189 | /* Get size and type again. */ |
| 2190 | size = stream_getw_from (peer->ibuf, BGP_MARKER_SIZE); |
| 2191 | type = stream_getc_from (peer->ibuf, BGP_MARKER_SIZE + 2); |
| 2192 | |
| 2193 | /* BGP packet dump function. */ |
| 2194 | bgp_dump_packet (peer, type, peer->ibuf); |
| 2195 | |
| 2196 | size = (peer->packet_size - BGP_HEADER_SIZE); |
| 2197 | |
| 2198 | /* Read rest of the packet and call each sort of packet routine */ |
| 2199 | switch (type) |
| 2200 | { |
| 2201 | case BGP_MSG_OPEN: |
| 2202 | peer->open_in++; |
| 2203 | bgp_open_receive (peer, size); |
| 2204 | break; |
| 2205 | case BGP_MSG_UPDATE: |
| 2206 | peer->readtime = time(NULL); /* Last read timer reset */ |
| 2207 | bgp_update_receive (peer, size); |
| 2208 | break; |
| 2209 | case BGP_MSG_NOTIFY: |
| 2210 | bgp_notify_receive (peer, size); |
| 2211 | break; |
| 2212 | case BGP_MSG_KEEPALIVE: |
| 2213 | peer->readtime = time(NULL); /* Last read timer reset */ |
| 2214 | bgp_keepalive_receive (peer, size); |
| 2215 | break; |
| 2216 | case BGP_MSG_ROUTE_REFRESH_NEW: |
| 2217 | case BGP_MSG_ROUTE_REFRESH_OLD: |
| 2218 | peer->refresh_in++; |
| 2219 | bgp_route_refresh_receive (peer, size); |
| 2220 | break; |
| 2221 | case BGP_MSG_CAPABILITY: |
| 2222 | peer->dynamic_cap_in++; |
| 2223 | bgp_capability_receive (peer, size); |
| 2224 | break; |
| 2225 | } |
| 2226 | |
| 2227 | /* Clear input buffer. */ |
| 2228 | peer->packet_size = 0; |
| 2229 | if (peer->ibuf) |
| 2230 | stream_reset (peer->ibuf); |
| 2231 | |
| 2232 | done: |
| 2233 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 2234 | { |
| 2235 | if (BGP_DEBUG (events, EVENTS)) |
| 2236 | zlog_info ("%s [Event] Accepting BGP peer delete", peer->host); |
| 2237 | peer_delete (peer); |
| 2238 | } |
| 2239 | return 0; |
| 2240 | } |