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; |
paul | 5228ad2 | 2004-06-04 17:58:18 +0000 | [diff] [blame] | 127 | socklen_t slen; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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); |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 136 | ret = getsockopt(peer->fd, SOL_SOCKET, SO_ERROR, (void *) &status, &slen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 154 | plog_debug (peer->log, "%s [Event] Connect failed (%s)", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 155 | peer->host, safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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); |
paul | 5228ad2 | 2004-06-04 17:58:18 +0000 | [diff] [blame] | 206 | total_attr_len = bgp_packet_attribute (NULL, peer, s, |
| 207 | adv->baa->attr, |
| 208 | &rn->p, afi, safi, |
| 209 | binfo->peer, prd, tag); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 217 | zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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); |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 241 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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) |
hasso | 4372df7 | 2004-05-20 10:20:02 +0000 | [diff] [blame] | 276 | < (BGP_NLRI_LENGTH + BGP_TOTAL_ATTR_LEN + PSIZE (rn->p.prefixlen))) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 299 | zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d -- unreachable", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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); |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 359 | zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d %s", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 398 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 428 | zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d -- unreachable", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 472 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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; |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 551 | unsigned int count = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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; |
paul | a24a7e1 | 2005-01-05 08:14:13 +0000 | [diff] [blame] | 569 | int val; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 570 | |
| 571 | s = bgp_write_packet (peer); |
| 572 | if (! s) |
| 573 | return 0; |
paul | a24a7e1 | 2005-01-05 08:14:13 +0000 | [diff] [blame] | 574 | |
| 575 | /* XXX: FIXME, the socket should be NONBLOCK from the start |
| 576 | * status shouldnt need to be toggled on each write |
| 577 | */ |
| 578 | val = fcntl (peer->fd, F_GETFL, 0); |
| 579 | fcntl (peer->fd, F_SETFL, val|O_NONBLOCK); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 580 | |
| 581 | /* Number of bytes to be sent. */ |
| 582 | writenum = stream_get_endp (s) - stream_get_getp (s); |
| 583 | |
| 584 | /* Call write() system call. */ |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 585 | num = write (peer->fd, STREAM_PNT (s), writenum); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 586 | write_errno = errno; |
paul | a24a7e1 | 2005-01-05 08:14:13 +0000 | [diff] [blame] | 587 | fcntl (peer->fd, F_SETFL, val); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 588 | if (num <= 0) |
| 589 | { |
| 590 | /* Partial write. */ |
| 591 | if (write_errno == EWOULDBLOCK || write_errno == EAGAIN) |
| 592 | break; |
| 593 | |
| 594 | bgp_stop (peer); |
| 595 | peer->status = Idle; |
| 596 | bgp_timer_set (peer); |
| 597 | return 0; |
| 598 | } |
| 599 | if (num != writenum) |
| 600 | { |
| 601 | stream_forward (s, num); |
| 602 | |
| 603 | if (write_errno == EAGAIN) |
| 604 | break; |
| 605 | |
| 606 | continue; |
| 607 | } |
| 608 | |
| 609 | /* Retrieve BGP packet type. */ |
| 610 | stream_set_getp (s, BGP_MARKER_SIZE + 2); |
| 611 | type = stream_getc (s); |
| 612 | |
| 613 | switch (type) |
| 614 | { |
| 615 | case BGP_MSG_OPEN: |
| 616 | peer->open_out++; |
| 617 | break; |
| 618 | case BGP_MSG_UPDATE: |
| 619 | peer->update_out++; |
| 620 | break; |
| 621 | case BGP_MSG_NOTIFY: |
| 622 | peer->notify_out++; |
| 623 | /* Double start timer. */ |
| 624 | peer->v_start *= 2; |
| 625 | |
| 626 | /* Overflow check. */ |
| 627 | if (peer->v_start >= (60 * 2)) |
| 628 | peer->v_start = (60 * 2); |
| 629 | |
| 630 | /* BGP_EVENT_ADD (peer, BGP_Stop); */ |
| 631 | bgp_stop (peer); |
| 632 | peer->status = Idle; |
| 633 | bgp_timer_set (peer); |
| 634 | return 0; |
| 635 | break; |
| 636 | case BGP_MSG_KEEPALIVE: |
| 637 | peer->keepalive_out++; |
| 638 | break; |
| 639 | case BGP_MSG_ROUTE_REFRESH_NEW: |
| 640 | case BGP_MSG_ROUTE_REFRESH_OLD: |
| 641 | peer->refresh_out++; |
| 642 | break; |
| 643 | case BGP_MSG_CAPABILITY: |
| 644 | peer->dynamic_cap_out++; |
| 645 | break; |
| 646 | } |
| 647 | |
| 648 | /* OK we send packet so delete it. */ |
| 649 | bgp_packet_delete (peer); |
| 650 | |
| 651 | if (++count >= BGP_WRITE_PACKET_MAX) |
| 652 | break; |
| 653 | } |
| 654 | |
| 655 | if (bgp_write_proceed (peer)) |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 656 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 657 | |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | /* This is only for sending NOTIFICATION message to neighbor. */ |
| 662 | int |
| 663 | bgp_write_notify (struct peer *peer) |
| 664 | { |
| 665 | int ret; |
| 666 | u_char type; |
| 667 | struct stream *s; |
| 668 | |
| 669 | /* There should be at least one packet. */ |
| 670 | s = stream_fifo_head (peer->obuf); |
| 671 | if (!s) |
| 672 | return 0; |
| 673 | assert (stream_get_endp (s) >= BGP_HEADER_SIZE); |
| 674 | |
| 675 | /* I'm not sure fd is writable. */ |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 676 | ret = writen (peer->fd, STREAM_DATA (s), stream_get_endp (s)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 677 | if (ret <= 0) |
| 678 | { |
| 679 | bgp_stop (peer); |
| 680 | peer->status = Idle; |
| 681 | bgp_timer_set (peer); |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | /* Retrieve BGP packet type. */ |
| 686 | stream_set_getp (s, BGP_MARKER_SIZE + 2); |
| 687 | type = stream_getc (s); |
| 688 | |
| 689 | assert (type == BGP_MSG_NOTIFY); |
| 690 | |
| 691 | /* Type should be notify. */ |
| 692 | peer->notify_out++; |
| 693 | |
| 694 | /* Double start timer. */ |
| 695 | peer->v_start *= 2; |
| 696 | |
| 697 | /* Overflow check. */ |
| 698 | if (peer->v_start >= (60 * 2)) |
| 699 | peer->v_start = (60 * 2); |
| 700 | |
| 701 | /* We don't call event manager at here for avoiding other events. */ |
| 702 | bgp_stop (peer); |
| 703 | peer->status = Idle; |
| 704 | bgp_timer_set (peer); |
| 705 | |
| 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | /* Make keepalive packet and send it to the peer. */ |
| 710 | void |
| 711 | bgp_keepalive_send (struct peer *peer) |
| 712 | { |
| 713 | struct stream *s; |
| 714 | int length; |
| 715 | |
| 716 | s = stream_new (BGP_MAX_PACKET_SIZE); |
| 717 | |
| 718 | /* Make keepalive packet. */ |
| 719 | bgp_packet_set_marker (s, BGP_MSG_KEEPALIVE); |
| 720 | |
| 721 | /* Set packet size. */ |
| 722 | length = bgp_packet_set_size (s); |
| 723 | |
| 724 | /* Dump packet if debug option is set. */ |
| 725 | /* bgp_packet_dump (s); */ |
| 726 | |
| 727 | if (BGP_DEBUG (keepalive, KEEPALIVE)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 728 | zlog_debug ("%s sending KEEPALIVE", peer->host); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 729 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 730 | zlog_debug ("%s send message type %d, length (incl. header) %d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 731 | peer->host, BGP_MSG_KEEPALIVE, length); |
| 732 | |
| 733 | /* Add packet to the peer. */ |
| 734 | bgp_packet_add (peer, s); |
| 735 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 736 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | /* Make open packet and send it to the peer. */ |
| 740 | void |
| 741 | bgp_open_send (struct peer *peer) |
| 742 | { |
| 743 | struct stream *s; |
| 744 | int length; |
| 745 | u_int16_t send_holdtime; |
| 746 | as_t local_as; |
| 747 | |
| 748 | if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER)) |
| 749 | send_holdtime = peer->holdtime; |
| 750 | else |
| 751 | send_holdtime = peer->bgp->default_holdtime; |
| 752 | |
| 753 | /* local-as Change */ |
| 754 | if (peer->change_local_as) |
| 755 | local_as = peer->change_local_as; |
| 756 | else |
| 757 | local_as = peer->local_as; |
| 758 | |
| 759 | s = stream_new (BGP_MAX_PACKET_SIZE); |
| 760 | |
| 761 | /* Make open packet. */ |
| 762 | bgp_packet_set_marker (s, BGP_MSG_OPEN); |
| 763 | |
| 764 | /* Set open packet values. */ |
| 765 | stream_putc (s, BGP_VERSION_4); /* BGP version */ |
| 766 | stream_putw (s, local_as); /* My Autonomous System*/ |
| 767 | stream_putw (s, send_holdtime); /* Hold Time */ |
| 768 | stream_put_in_addr (s, &peer->local_id); /* BGP Identifier */ |
| 769 | |
| 770 | /* Set capability code. */ |
| 771 | bgp_open_capability (s, peer); |
| 772 | |
| 773 | /* Set BGP packet length. */ |
| 774 | length = bgp_packet_set_size (s); |
| 775 | |
| 776 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 777 | zlog_debug ("%s sending OPEN, version %d, my as %d, holdtime %d, id %s", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 778 | peer->host, BGP_VERSION_4, local_as, |
| 779 | send_holdtime, inet_ntoa (peer->local_id)); |
| 780 | |
| 781 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 782 | zlog_debug ("%s send message type %d, length (incl. header) %d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 783 | peer->host, BGP_MSG_OPEN, length); |
| 784 | |
| 785 | /* Dump packet if debug option is set. */ |
| 786 | /* bgp_packet_dump (s); */ |
| 787 | |
| 788 | /* Add packet to the peer. */ |
| 789 | bgp_packet_add (peer, s); |
| 790 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 791 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | /* Send BGP notify packet with data potion. */ |
| 795 | void |
| 796 | bgp_notify_send_with_data (struct peer *peer, u_char code, u_char sub_code, |
| 797 | u_char *data, size_t datalen) |
| 798 | { |
| 799 | struct stream *s; |
| 800 | int length; |
| 801 | |
| 802 | /* Allocate new stream. */ |
| 803 | s = stream_new (BGP_MAX_PACKET_SIZE); |
| 804 | |
| 805 | /* Make nitify packet. */ |
| 806 | bgp_packet_set_marker (s, BGP_MSG_NOTIFY); |
| 807 | |
| 808 | /* Set notify packet values. */ |
| 809 | stream_putc (s, code); /* BGP notify code */ |
| 810 | stream_putc (s, sub_code); /* BGP notify sub_code */ |
| 811 | |
| 812 | /* If notify data is present. */ |
| 813 | if (data) |
| 814 | stream_write (s, data, datalen); |
| 815 | |
| 816 | /* Set BGP packet length. */ |
| 817 | length = bgp_packet_set_size (s); |
| 818 | |
| 819 | /* Add packet to the peer. */ |
| 820 | stream_fifo_clean (peer->obuf); |
| 821 | bgp_packet_add (peer, s); |
| 822 | |
| 823 | /* For debug */ |
| 824 | { |
| 825 | struct bgp_notify bgp_notify; |
| 826 | int first = 0; |
| 827 | int i; |
| 828 | char c[4]; |
| 829 | |
| 830 | bgp_notify.code = code; |
| 831 | bgp_notify.subcode = sub_code; |
| 832 | bgp_notify.data = NULL; |
| 833 | bgp_notify.length = length - BGP_MSG_NOTIFY_MIN_SIZE; |
| 834 | |
| 835 | if (bgp_notify.length) |
| 836 | { |
| 837 | bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3); |
| 838 | for (i = 0; i < bgp_notify.length; i++) |
| 839 | if (first) |
| 840 | { |
| 841 | sprintf (c, " %02x", data[i]); |
| 842 | strcat (bgp_notify.data, c); |
| 843 | } |
| 844 | else |
| 845 | { |
| 846 | first = 1; |
| 847 | sprintf (c, "%02x", data[i]); |
| 848 | strcpy (bgp_notify.data, c); |
| 849 | } |
| 850 | } |
| 851 | bgp_notify_print (peer, &bgp_notify, "sending"); |
| 852 | if (bgp_notify.data) |
| 853 | XFREE (MTYPE_TMP, bgp_notify.data); |
| 854 | } |
| 855 | |
| 856 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 857 | zlog_debug ("%s send message type %d, length (incl. header) %d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 858 | peer->host, BGP_MSG_NOTIFY, length); |
| 859 | |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 860 | /* peer reset cause */ |
| 861 | if (sub_code != BGP_NOTIFY_CEASE_CONFIG_CHANGE) |
| 862 | { |
| 863 | if (sub_code == BGP_NOTIFY_CEASE_ADMIN_RESET) |
| 864 | peer->last_reset = PEER_DOWN_USER_RESET; |
| 865 | else if (sub_code == BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN) |
| 866 | peer->last_reset = PEER_DOWN_USER_SHUTDOWN; |
| 867 | else |
| 868 | peer->last_reset = PEER_DOWN_NOTIFY_SEND; |
| 869 | } |
| 870 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 871 | /* Call imidiately. */ |
| 872 | BGP_WRITE_OFF (peer->t_write); |
| 873 | |
| 874 | bgp_write_notify (peer); |
| 875 | } |
| 876 | |
| 877 | /* Send BGP notify packet. */ |
| 878 | void |
| 879 | bgp_notify_send (struct peer *peer, u_char code, u_char sub_code) |
| 880 | { |
| 881 | bgp_notify_send_with_data (peer, code, sub_code, NULL, 0); |
| 882 | } |
| 883 | |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 884 | const char * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 885 | afi2str (afi_t afi) |
| 886 | { |
| 887 | if (afi == AFI_IP) |
| 888 | return "AFI_IP"; |
| 889 | else if (afi == AFI_IP6) |
| 890 | return "AFI_IP6"; |
| 891 | else |
| 892 | return "Unknown AFI"; |
| 893 | } |
| 894 | |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 895 | const char * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 896 | safi2str (safi_t safi) |
| 897 | { |
| 898 | if (safi == SAFI_UNICAST) |
| 899 | return "SAFI_UNICAST"; |
| 900 | else if (safi == SAFI_MULTICAST) |
| 901 | return "SAFI_MULTICAST"; |
| 902 | else if (safi == SAFI_MPLS_VPN || safi == BGP_SAFI_VPNV4) |
| 903 | return "SAFI_MPLS_VPN"; |
| 904 | else |
| 905 | return "Unknown SAFI"; |
| 906 | } |
| 907 | |
| 908 | /* Send route refresh message to the peer. */ |
| 909 | void |
| 910 | bgp_route_refresh_send (struct peer *peer, afi_t afi, safi_t safi, |
| 911 | u_char orf_type, u_char when_to_refresh, int remove) |
| 912 | { |
| 913 | struct stream *s; |
| 914 | struct stream *packet; |
| 915 | int length; |
| 916 | struct bgp_filter *filter; |
| 917 | int orf_refresh = 0; |
| 918 | |
| 919 | #ifdef DISABLE_BGP_ANNOUNCE |
| 920 | return; |
| 921 | #endif /* DISABLE_BGP_ANNOUNCE */ |
| 922 | |
| 923 | filter = &peer->filter[afi][safi]; |
| 924 | |
| 925 | /* Adjust safi code. */ |
| 926 | if (safi == SAFI_MPLS_VPN) |
| 927 | safi = BGP_SAFI_VPNV4; |
| 928 | |
| 929 | s = stream_new (BGP_MAX_PACKET_SIZE); |
| 930 | |
| 931 | /* Make BGP update packet. */ |
| 932 | if (CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV)) |
| 933 | bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_NEW); |
| 934 | else |
| 935 | bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_OLD); |
| 936 | |
| 937 | /* Encode Route Refresh message. */ |
| 938 | stream_putw (s, afi); |
| 939 | stream_putc (s, 0); |
| 940 | stream_putc (s, safi); |
| 941 | |
| 942 | if (orf_type == ORF_TYPE_PREFIX |
| 943 | || orf_type == ORF_TYPE_PREFIX_OLD) |
| 944 | if (remove || filter->plist[FILTER_IN].plist) |
| 945 | { |
| 946 | u_int16_t orf_len; |
| 947 | unsigned long orfp; |
| 948 | |
| 949 | orf_refresh = 1; |
| 950 | stream_putc (s, when_to_refresh); |
| 951 | stream_putc (s, orf_type); |
| 952 | orfp = stream_get_putp (s); |
| 953 | stream_putw (s, 0); |
| 954 | |
| 955 | if (remove) |
| 956 | { |
| 957 | UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND); |
| 958 | stream_putc (s, ORF_COMMON_PART_REMOVE_ALL); |
| 959 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 960 | zlog_debug ("%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 961 | peer->host, orf_type, |
| 962 | (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"), |
| 963 | afi, safi); |
| 964 | } |
| 965 | else |
| 966 | { |
| 967 | SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND); |
| 968 | prefix_bgp_orf_entry (s, filter->plist[FILTER_IN].plist, |
| 969 | ORF_COMMON_PART_ADD, ORF_COMMON_PART_PERMIT, |
| 970 | ORF_COMMON_PART_DENY); |
| 971 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 972 | zlog_debug ("%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 973 | peer->host, orf_type, |
| 974 | (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"), |
| 975 | afi, safi); |
| 976 | } |
| 977 | |
| 978 | /* Total ORF Entry Len. */ |
| 979 | orf_len = stream_get_putp (s) - orfp - 2; |
| 980 | stream_putw_at (s, orfp, orf_len); |
| 981 | } |
| 982 | |
| 983 | /* Set packet size. */ |
| 984 | length = bgp_packet_set_size (s); |
| 985 | |
| 986 | if (BGP_DEBUG (normal, NORMAL)) |
| 987 | { |
| 988 | if (! orf_refresh) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 989 | zlog_debug ("%s sending REFRESH_REQ for afi/safi: %d/%d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 990 | peer->host, afi, safi); |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 991 | zlog_debug ("%s send message type %d, length (incl. header) %d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 992 | peer->host, CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV) ? |
| 993 | BGP_MSG_ROUTE_REFRESH_NEW : BGP_MSG_ROUTE_REFRESH_OLD, length); |
| 994 | } |
| 995 | |
| 996 | /* Make real packet. */ |
| 997 | packet = bgp_packet_dup (s); |
| 998 | stream_free (s); |
| 999 | |
| 1000 | /* Add packet to the peer. */ |
| 1001 | bgp_packet_add (peer, packet); |
| 1002 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 1003 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | /* Send capability message to the peer. */ |
| 1007 | void |
| 1008 | bgp_capability_send (struct peer *peer, afi_t afi, safi_t safi, |
| 1009 | int capability_code, int action) |
| 1010 | { |
| 1011 | struct stream *s; |
| 1012 | struct stream *packet; |
| 1013 | int length; |
| 1014 | |
| 1015 | /* Adjust safi code. */ |
| 1016 | if (safi == SAFI_MPLS_VPN) |
| 1017 | safi = BGP_SAFI_VPNV4; |
| 1018 | |
| 1019 | s = stream_new (BGP_MAX_PACKET_SIZE); |
| 1020 | |
| 1021 | /* Make BGP update packet. */ |
| 1022 | bgp_packet_set_marker (s, BGP_MSG_CAPABILITY); |
| 1023 | |
| 1024 | /* Encode MP_EXT capability. */ |
| 1025 | if (capability_code == CAPABILITY_CODE_MP) |
| 1026 | { |
| 1027 | stream_putc (s, action); |
| 1028 | stream_putc (s, CAPABILITY_CODE_MP); |
| 1029 | stream_putc (s, CAPABILITY_CODE_MP_LEN); |
| 1030 | stream_putw (s, afi); |
| 1031 | stream_putc (s, 0); |
| 1032 | stream_putc (s, safi); |
| 1033 | |
| 1034 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1035 | zlog_debug ("%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1036 | peer->host, action == CAPABILITY_ACTION_SET ? |
| 1037 | "Advertising" : "Removing", afi, safi); |
| 1038 | } |
| 1039 | |
| 1040 | /* Encode Route Refresh capability. */ |
| 1041 | if (capability_code == CAPABILITY_CODE_REFRESH) |
| 1042 | { |
| 1043 | stream_putc (s, action); |
| 1044 | stream_putc (s, CAPABILITY_CODE_REFRESH); |
| 1045 | stream_putc (s, CAPABILITY_CODE_REFRESH_LEN); |
| 1046 | stream_putc (s, action); |
| 1047 | stream_putc (s, CAPABILITY_CODE_REFRESH_OLD); |
| 1048 | stream_putc (s, CAPABILITY_CODE_REFRESH_LEN); |
| 1049 | |
| 1050 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1051 | zlog_debug ("%s sending CAPABILITY has %s ROUTE-REFRESH capability", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1052 | peer->host, action == CAPABILITY_ACTION_SET ? |
| 1053 | "Advertising" : "Removing"); |
| 1054 | } |
| 1055 | |
| 1056 | /* Set packet size. */ |
| 1057 | length = bgp_packet_set_size (s); |
| 1058 | |
| 1059 | /* Make real packet. */ |
| 1060 | packet = bgp_packet_dup (s); |
| 1061 | stream_free (s); |
| 1062 | |
| 1063 | /* Add packet to the peer. */ |
| 1064 | bgp_packet_add (peer, packet); |
| 1065 | |
| 1066 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1067 | zlog_debug ("%s send message type %d, length (incl. header) %d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1068 | peer->host, BGP_MSG_CAPABILITY, length); |
| 1069 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 1070 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | /* RFC1771 6.8 Connection collision detection. */ |
| 1074 | int |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 1075 | bgp_collision_detect (struct peer *new, struct in_addr remote_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1076 | { |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 1077 | struct peer *peer; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1078 | struct listnode *nn; |
| 1079 | struct bgp *bgp; |
| 1080 | |
| 1081 | bgp = bgp_get_default (); |
| 1082 | if (! bgp) |
| 1083 | return 0; |
| 1084 | |
| 1085 | /* Upon receipt of an OPEN message, the local system must examine |
| 1086 | all of its connections that are in the OpenConfirm state. A BGP |
| 1087 | speaker may also examine connections in an OpenSent state if it |
| 1088 | knows the BGP Identifier of the peer by means outside of the |
| 1089 | protocol. If among these connections there is a connection to a |
| 1090 | remote BGP speaker whose BGP Identifier equals the one in the |
| 1091 | OPEN message, then the local system performs the following |
| 1092 | collision resolution procedure: */ |
| 1093 | |
| 1094 | LIST_LOOP (bgp->peer, peer, nn) |
| 1095 | { |
| 1096 | /* Under OpenConfirm status, local peer structure already hold |
| 1097 | remote router ID. */ |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 1098 | |
| 1099 | if (peer != new |
| 1100 | && (peer->status == OpenConfirm || peer->status == OpenSent) |
| 1101 | && sockunion_same (&peer->su, &new->su)) |
| 1102 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1103 | /* 1. The BGP Identifier of the local system is compared to |
| 1104 | the BGP Identifier of the remote system (as specified in |
| 1105 | the OPEN message). */ |
| 1106 | |
| 1107 | if (ntohl (peer->local_id.s_addr) < ntohl (remote_id.s_addr)) |
| 1108 | { |
| 1109 | /* 2. If the value of the local BGP Identifier is less |
| 1110 | than the remote one, the local system closes BGP |
| 1111 | connection that already exists (the one that is |
| 1112 | already in the OpenConfirm state), and accepts BGP |
| 1113 | connection initiated by the remote system. */ |
| 1114 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 1115 | if (peer->fd >= 0) |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 1116 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1117 | return 1; |
| 1118 | } |
| 1119 | else |
| 1120 | { |
| 1121 | /* 3. Otherwise, the local system closes newly created |
| 1122 | BGP connection (the one associated with the newly |
| 1123 | received OPEN message), and continues to use the |
| 1124 | existing one (the one that is already in the |
| 1125 | OpenConfirm state). */ |
| 1126 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 1127 | if (new->fd >= 0) |
paul | f5ba387 | 2004-07-09 12:11:31 +0000 | [diff] [blame] | 1128 | bgp_notify_send (new, BGP_NOTIFY_CEASE, |
| 1129 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1130 | return -1; |
| 1131 | } |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 1132 | } |
| 1133 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1134 | return 0; |
| 1135 | } |
| 1136 | |
| 1137 | int |
| 1138 | bgp_open_receive (struct peer *peer, bgp_size_t size) |
| 1139 | { |
| 1140 | int ret; |
| 1141 | u_char version; |
| 1142 | u_char optlen; |
| 1143 | u_int16_t holdtime; |
| 1144 | u_int16_t send_holdtime; |
| 1145 | as_t remote_as; |
| 1146 | struct peer *realpeer; |
| 1147 | struct in_addr remote_id; |
| 1148 | int capability; |
paul | 5228ad2 | 2004-06-04 17:58:18 +0000 | [diff] [blame] | 1149 | u_int8_t notify_data_remote_as[2]; |
| 1150 | u_int8_t notify_data_remote_id[4]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1151 | |
| 1152 | realpeer = NULL; |
| 1153 | |
| 1154 | /* Parse open packet. */ |
| 1155 | version = stream_getc (peer->ibuf); |
| 1156 | memcpy (notify_data_remote_as, stream_pnt (peer->ibuf), 2); |
| 1157 | remote_as = stream_getw (peer->ibuf); |
| 1158 | holdtime = stream_getw (peer->ibuf); |
| 1159 | memcpy (notify_data_remote_id, stream_pnt (peer->ibuf), 4); |
| 1160 | remote_id.s_addr = stream_get_ipv4 (peer->ibuf); |
| 1161 | |
| 1162 | /* Receive OPEN message log */ |
| 1163 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1164 | zlog_debug ("%s rcv OPEN, version %d, remote-as %d, holdtime %d, id %s", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1165 | peer->host, version, remote_as, holdtime, |
| 1166 | inet_ntoa (remote_id)); |
| 1167 | |
| 1168 | /* Lookup peer from Open packet. */ |
| 1169 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 1170 | { |
| 1171 | int as = 0; |
| 1172 | |
| 1173 | realpeer = peer_lookup_with_open (&peer->su, remote_as, &remote_id, &as); |
| 1174 | |
| 1175 | if (! realpeer) |
| 1176 | { |
| 1177 | /* Peer's source IP address is check in bgp_accept(), so this |
| 1178 | must be AS number mismatch or remote-id configuration |
| 1179 | mismatch. */ |
| 1180 | if (as) |
| 1181 | { |
| 1182 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1183 | zlog_debug ("%s bad OPEN, wrong router identifier %s", |
| 1184 | peer->host, inet_ntoa (remote_id)); |
| 1185 | bgp_notify_send_with_data (peer, BGP_NOTIFY_OPEN_ERR, |
| 1186 | BGP_NOTIFY_OPEN_BAD_BGP_IDENT, |
| 1187 | notify_data_remote_id, 4); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1188 | } |
| 1189 | else |
| 1190 | { |
| 1191 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1192 | zlog_debug ("%s bad OPEN, remote AS is %d, expected %d", |
| 1193 | peer->host, remote_as, peer->as); |
| 1194 | bgp_notify_send_with_data (peer, BGP_NOTIFY_OPEN_ERR, |
| 1195 | BGP_NOTIFY_OPEN_BAD_PEER_AS, |
| 1196 | notify_data_remote_as, 2); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1197 | } |
| 1198 | return -1; |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | /* When collision is detected and this peer is closed. Retrun |
| 1203 | immidiately. */ |
| 1204 | ret = bgp_collision_detect (peer, remote_id); |
| 1205 | if (ret < 0) |
| 1206 | return ret; |
| 1207 | |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 1208 | /* Hack part. */ |
| 1209 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 1210 | { |
| 1211 | if (ret == 0 && realpeer->status != Active |
| 1212 | && realpeer->status != OpenSent |
| 1213 | && realpeer->status != OpenConfirm) |
| 1214 | { |
| 1215 | if (BGP_DEBUG (events, EVENTS)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1216 | zlog_debug ("%s [Event] peer's status is %s close connection", |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 1217 | realpeer->host, LOOKUP (bgp_status_msg, peer->status)); |
| 1218 | return -1; |
| 1219 | } |
| 1220 | |
| 1221 | if (BGP_DEBUG (events, EVENTS)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1222 | zlog_debug ("%s [Event] Transfer temporary BGP peer to existing one", |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 1223 | peer->host); |
| 1224 | |
| 1225 | bgp_stop (realpeer); |
| 1226 | |
| 1227 | /* Transfer file descriptor. */ |
| 1228 | realpeer->fd = peer->fd; |
| 1229 | peer->fd = -1; |
| 1230 | |
| 1231 | /* Transfer input buffer. */ |
| 1232 | stream_free (realpeer->ibuf); |
| 1233 | realpeer->ibuf = peer->ibuf; |
| 1234 | realpeer->packet_size = peer->packet_size; |
| 1235 | peer->ibuf = NULL; |
| 1236 | |
| 1237 | /* Transfer status. */ |
| 1238 | realpeer->status = peer->status; |
| 1239 | bgp_stop (peer); |
| 1240 | |
| 1241 | /* peer pointer change. Open packet send to neighbor. */ |
| 1242 | peer = realpeer; |
| 1243 | bgp_open_send (peer); |
| 1244 | if (peer->fd < 0) |
| 1245 | { |
| 1246 | zlog_err ("bgp_open_receive peer's fd is negative value %d", |
| 1247 | peer->fd); |
| 1248 | return -1; |
| 1249 | } |
| 1250 | BGP_READ_ON (peer->t_read, bgp_read, peer->fd); |
| 1251 | } |
| 1252 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1253 | /* remote router-id check. */ |
| 1254 | if (remote_id.s_addr == 0 |
| 1255 | || ntohl (remote_id.s_addr) >= 0xe0000000 |
| 1256 | || ntohl (peer->local_id.s_addr) == ntohl (remote_id.s_addr)) |
| 1257 | { |
| 1258 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1259 | zlog_debug ("%s bad OPEN, wrong router identifier %s", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1260 | peer->host, inet_ntoa (remote_id)); |
| 1261 | bgp_notify_send_with_data (peer, |
| 1262 | BGP_NOTIFY_OPEN_ERR, |
| 1263 | BGP_NOTIFY_OPEN_BAD_BGP_IDENT, |
| 1264 | notify_data_remote_id, 4); |
| 1265 | return -1; |
| 1266 | } |
| 1267 | |
| 1268 | /* Set remote router-id */ |
| 1269 | peer->remote_id = remote_id; |
| 1270 | |
| 1271 | /* Peer BGP version check. */ |
| 1272 | if (version != BGP_VERSION_4) |
| 1273 | { |
paul | 5228ad2 | 2004-06-04 17:58:18 +0000 | [diff] [blame] | 1274 | u_int8_t maxver = BGP_VERSION_4; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1275 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1276 | zlog_debug ("%s bad protocol version, remote requested %d, local request %d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1277 | peer->host, version, BGP_VERSION_4); |
| 1278 | bgp_notify_send_with_data (peer, |
| 1279 | BGP_NOTIFY_OPEN_ERR, |
| 1280 | BGP_NOTIFY_OPEN_UNSUP_VERSION, |
paul | 5228ad2 | 2004-06-04 17:58:18 +0000 | [diff] [blame] | 1281 | &maxver, 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1282 | return -1; |
| 1283 | } |
| 1284 | |
| 1285 | /* Check neighbor as number. */ |
| 1286 | if (remote_as != peer->as) |
| 1287 | { |
| 1288 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1289 | zlog_debug ("%s bad OPEN, remote AS is %d, expected %d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1290 | peer->host, remote_as, peer->as); |
| 1291 | bgp_notify_send_with_data (peer, |
| 1292 | BGP_NOTIFY_OPEN_ERR, |
| 1293 | BGP_NOTIFY_OPEN_BAD_PEER_AS, |
| 1294 | notify_data_remote_as, 2); |
| 1295 | return -1; |
| 1296 | } |
| 1297 | |
| 1298 | /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST |
| 1299 | calculate the value of the Hold Timer by using the smaller of its |
| 1300 | configured Hold Time and the Hold Time received in the OPEN message. |
| 1301 | The Hold Time MUST be either zero or at least three seconds. An |
| 1302 | implementation may reject connections on the basis of the Hold Time. */ |
| 1303 | |
| 1304 | if (holdtime < 3 && holdtime != 0) |
| 1305 | { |
| 1306 | bgp_notify_send (peer, |
| 1307 | BGP_NOTIFY_OPEN_ERR, |
| 1308 | BGP_NOTIFY_OPEN_UNACEP_HOLDTIME); |
| 1309 | return -1; |
| 1310 | } |
| 1311 | |
| 1312 | /* From the rfc: A reasonable maximum time between KEEPALIVE messages |
| 1313 | would be one third of the Hold Time interval. KEEPALIVE messages |
| 1314 | MUST NOT be sent more frequently than one per second. An |
| 1315 | implementation MAY adjust the rate at which it sends KEEPALIVE |
| 1316 | messages as a function of the Hold Time interval. */ |
| 1317 | |
| 1318 | if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER)) |
| 1319 | send_holdtime = peer->holdtime; |
| 1320 | else |
| 1321 | send_holdtime = peer->bgp->default_holdtime; |
| 1322 | |
| 1323 | if (holdtime < send_holdtime) |
| 1324 | peer->v_holdtime = holdtime; |
| 1325 | else |
| 1326 | peer->v_holdtime = send_holdtime; |
| 1327 | |
| 1328 | peer->v_keepalive = peer->v_holdtime / 3; |
| 1329 | |
| 1330 | /* Open option part parse. */ |
| 1331 | capability = 0; |
| 1332 | optlen = stream_getc (peer->ibuf); |
| 1333 | if (optlen != 0) |
| 1334 | { |
| 1335 | ret = bgp_open_option_parse (peer, optlen, &capability); |
| 1336 | if (ret < 0) |
| 1337 | return ret; |
| 1338 | |
| 1339 | stream_forward (peer->ibuf, optlen); |
| 1340 | } |
| 1341 | else |
| 1342 | { |
| 1343 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1344 | zlog_debug ("%s rcvd OPEN w/ OPTION parameter len: 0", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1345 | peer->host); |
| 1346 | } |
| 1347 | |
| 1348 | /* Override capability. */ |
| 1349 | if (! capability || CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) |
| 1350 | { |
| 1351 | peer->afc_nego[AFI_IP][SAFI_UNICAST] = peer->afc[AFI_IP][SAFI_UNICAST]; |
| 1352 | peer->afc_nego[AFI_IP][SAFI_MULTICAST] = peer->afc[AFI_IP][SAFI_MULTICAST]; |
| 1353 | peer->afc_nego[AFI_IP6][SAFI_UNICAST] = peer->afc[AFI_IP6][SAFI_UNICAST]; |
| 1354 | peer->afc_nego[AFI_IP6][SAFI_MULTICAST] = peer->afc[AFI_IP6][SAFI_MULTICAST]; |
| 1355 | } |
| 1356 | |
| 1357 | /* Get sockname. */ |
| 1358 | bgp_getsockname (peer); |
| 1359 | |
| 1360 | BGP_EVENT_ADD (peer, Receive_OPEN_message); |
| 1361 | |
| 1362 | peer->packet_size = 0; |
| 1363 | if (peer->ibuf) |
| 1364 | stream_reset (peer->ibuf); |
| 1365 | |
| 1366 | return 0; |
| 1367 | } |
| 1368 | |
| 1369 | /* Parse BGP Update packet and make attribute object. */ |
| 1370 | int |
| 1371 | bgp_update_receive (struct peer *peer, bgp_size_t size) |
| 1372 | { |
| 1373 | int ret; |
| 1374 | u_char *end; |
| 1375 | struct stream *s; |
| 1376 | struct attr attr; |
| 1377 | bgp_size_t attribute_len; |
| 1378 | bgp_size_t update_len; |
| 1379 | bgp_size_t withdraw_len; |
| 1380 | struct bgp_nlri update; |
| 1381 | struct bgp_nlri withdraw; |
| 1382 | struct bgp_nlri mp_update; |
| 1383 | struct bgp_nlri mp_withdraw; |
paul | e01f9cb | 2004-07-09 17:48:53 +0000 | [diff] [blame] | 1384 | char attrstr[BUFSIZ] = ""; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1385 | |
| 1386 | /* Status must be Established. */ |
| 1387 | if (peer->status != Established) |
| 1388 | { |
| 1389 | zlog_err ("%s [FSM] Update packet received under status %s", |
| 1390 | peer->host, LOOKUP (bgp_status_msg, peer->status)); |
| 1391 | bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0); |
| 1392 | return -1; |
| 1393 | } |
| 1394 | |
| 1395 | /* Set initial values. */ |
| 1396 | memset (&attr, 0, sizeof (struct attr)); |
| 1397 | memset (&update, 0, sizeof (struct bgp_nlri)); |
| 1398 | memset (&withdraw, 0, sizeof (struct bgp_nlri)); |
| 1399 | memset (&mp_update, 0, sizeof (struct bgp_nlri)); |
| 1400 | memset (&mp_withdraw, 0, sizeof (struct bgp_nlri)); |
| 1401 | |
| 1402 | s = peer->ibuf; |
| 1403 | end = stream_pnt (s) + size; |
| 1404 | |
| 1405 | /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute |
| 1406 | Length is too large (i.e., if Unfeasible Routes Length + Total |
| 1407 | Attribute Length + 23 exceeds the message Length), then the Error |
| 1408 | Subcode is set to Malformed Attribute List. */ |
| 1409 | if (stream_pnt (s) + 2 > end) |
| 1410 | { |
| 1411 | zlog_err ("%s [Error] Update packet error" |
| 1412 | " (packet length is short for unfeasible length)", |
| 1413 | peer->host); |
| 1414 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, |
| 1415 | BGP_NOTIFY_UPDATE_MAL_ATTR); |
| 1416 | return -1; |
| 1417 | } |
| 1418 | |
| 1419 | /* Unfeasible Route Length. */ |
| 1420 | withdraw_len = stream_getw (s); |
| 1421 | |
| 1422 | /* Unfeasible Route Length check. */ |
| 1423 | if (stream_pnt (s) + withdraw_len > end) |
| 1424 | { |
| 1425 | zlog_err ("%s [Error] Update packet error" |
| 1426 | " (packet unfeasible length overflow %d)", |
| 1427 | peer->host, withdraw_len); |
| 1428 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, |
| 1429 | BGP_NOTIFY_UPDATE_MAL_ATTR); |
| 1430 | return -1; |
| 1431 | } |
| 1432 | |
| 1433 | /* Unfeasible Route packet format check. */ |
| 1434 | if (withdraw_len > 0) |
| 1435 | { |
| 1436 | ret = bgp_nlri_sanity_check (peer, AFI_IP, stream_pnt (s), withdraw_len); |
| 1437 | if (ret < 0) |
| 1438 | return -1; |
| 1439 | |
| 1440 | if (BGP_DEBUG (packet, PACKET_RECV)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1441 | zlog_debug ("%s [Update:RECV] Unfeasible NLRI received", peer->host); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1442 | |
| 1443 | withdraw.afi = AFI_IP; |
| 1444 | withdraw.safi = SAFI_UNICAST; |
| 1445 | withdraw.nlri = stream_pnt (s); |
| 1446 | withdraw.length = withdraw_len; |
| 1447 | stream_forward (s, withdraw_len); |
| 1448 | } |
| 1449 | |
| 1450 | /* Attribute total length check. */ |
| 1451 | if (stream_pnt (s) + 2 > end) |
| 1452 | { |
| 1453 | zlog_warn ("%s [Error] Packet Error" |
| 1454 | " (update packet is short for attribute length)", |
| 1455 | peer->host); |
| 1456 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, |
| 1457 | BGP_NOTIFY_UPDATE_MAL_ATTR); |
| 1458 | return -1; |
| 1459 | } |
| 1460 | |
| 1461 | /* Fetch attribute total length. */ |
| 1462 | attribute_len = stream_getw (s); |
| 1463 | |
| 1464 | /* Attribute length check. */ |
| 1465 | if (stream_pnt (s) + attribute_len > end) |
| 1466 | { |
| 1467 | zlog_warn ("%s [Error] Packet Error" |
| 1468 | " (update packet attribute length overflow %d)", |
| 1469 | peer->host, attribute_len); |
| 1470 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, |
| 1471 | BGP_NOTIFY_UPDATE_MAL_ATTR); |
| 1472 | return -1; |
| 1473 | } |
| 1474 | |
| 1475 | /* Parse attribute when it exists. */ |
| 1476 | if (attribute_len) |
| 1477 | { |
| 1478 | ret = bgp_attr_parse (peer, &attr, attribute_len, |
| 1479 | &mp_update, &mp_withdraw); |
| 1480 | if (ret < 0) |
| 1481 | return -1; |
| 1482 | } |
| 1483 | |
| 1484 | /* Logging the attribute. */ |
| 1485 | if (BGP_DEBUG (update, UPDATE_IN)) |
| 1486 | { |
paul | e01f9cb | 2004-07-09 17:48:53 +0000 | [diff] [blame] | 1487 | ret= bgp_dump_attr (peer, &attr, attrstr, BUFSIZ); |
| 1488 | |
| 1489 | if (ret) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1490 | zlog (peer->log, LOG_DEBUG, "%s rcvd UPDATE w/ attr: %s", |
paul | e01f9cb | 2004-07-09 17:48:53 +0000 | [diff] [blame] | 1491 | peer->host, attrstr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | /* Network Layer Reachability Information. */ |
| 1495 | update_len = end - stream_pnt (s); |
| 1496 | |
| 1497 | if (update_len) |
| 1498 | { |
| 1499 | /* Check NLRI packet format and prefix length. */ |
| 1500 | ret = bgp_nlri_sanity_check (peer, AFI_IP, stream_pnt (s), update_len); |
| 1501 | if (ret < 0) |
| 1502 | return -1; |
| 1503 | |
| 1504 | /* Set NLRI portion to structure. */ |
| 1505 | update.afi = AFI_IP; |
| 1506 | update.safi = SAFI_UNICAST; |
| 1507 | update.nlri = stream_pnt (s); |
| 1508 | update.length = update_len; |
| 1509 | stream_forward (s, update_len); |
| 1510 | } |
| 1511 | |
| 1512 | /* NLRI is processed only when the peer is configured specific |
| 1513 | Address Family and Subsequent Address Family. */ |
| 1514 | if (peer->afc[AFI_IP][SAFI_UNICAST]) |
| 1515 | { |
| 1516 | if (withdraw.length) |
| 1517 | bgp_nlri_parse (peer, NULL, &withdraw); |
| 1518 | |
| 1519 | if (update.length) |
| 1520 | { |
| 1521 | /* We check well-known attribute only for IPv4 unicast |
| 1522 | update. */ |
| 1523 | ret = bgp_attr_check (peer, &attr); |
| 1524 | if (ret < 0) |
| 1525 | return -1; |
| 1526 | |
| 1527 | bgp_nlri_parse (peer, &attr, &update); |
| 1528 | } |
paul | e01f9cb | 2004-07-09 17:48:53 +0000 | [diff] [blame] | 1529 | |
| 1530 | if (! attribute_len && ! withdraw_len) |
| 1531 | { |
| 1532 | /* End-of-RIB received */ |
| 1533 | |
| 1534 | if (BGP_DEBUG (update, UPDATE_IN)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1535 | zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv4 Unicast from %s", |
paul | e01f9cb | 2004-07-09 17:48:53 +0000 | [diff] [blame] | 1536 | peer->host); |
| 1537 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1538 | } |
| 1539 | if (peer->afc[AFI_IP][SAFI_MULTICAST]) |
| 1540 | { |
| 1541 | if (mp_update.length |
| 1542 | && mp_update.afi == AFI_IP |
| 1543 | && mp_update.safi == SAFI_MULTICAST) |
| 1544 | bgp_nlri_parse (peer, &attr, &mp_update); |
| 1545 | |
| 1546 | if (mp_withdraw.length |
| 1547 | && mp_withdraw.afi == AFI_IP |
| 1548 | && mp_withdraw.safi == SAFI_MULTICAST) |
| 1549 | bgp_nlri_parse (peer, NULL, &mp_withdraw); |
paul | e01f9cb | 2004-07-09 17:48:53 +0000 | [diff] [blame] | 1550 | |
| 1551 | if (attribute_len == 6 && ! withdraw_len |
| 1552 | && mp_withdraw.afi == AFI_IP |
| 1553 | && mp_withdraw.safi == SAFI_MULTICAST |
| 1554 | && mp_withdraw.length == 0) |
| 1555 | { |
| 1556 | /* End-of-RIB received */ |
| 1557 | |
| 1558 | if (BGP_DEBUG (update, UPDATE_IN)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1559 | zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv4 Multicast from %s", |
paul | e01f9cb | 2004-07-09 17:48:53 +0000 | [diff] [blame] | 1560 | peer->host); |
| 1561 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1562 | } |
| 1563 | if (peer->afc[AFI_IP6][SAFI_UNICAST]) |
| 1564 | { |
| 1565 | if (mp_update.length |
| 1566 | && mp_update.afi == AFI_IP6 |
| 1567 | && mp_update.safi == SAFI_UNICAST) |
| 1568 | bgp_nlri_parse (peer, &attr, &mp_update); |
| 1569 | |
| 1570 | if (mp_withdraw.length |
| 1571 | && mp_withdraw.afi == AFI_IP6 |
| 1572 | && mp_withdraw.safi == SAFI_UNICAST) |
| 1573 | bgp_nlri_parse (peer, NULL, &mp_withdraw); |
paul | e01f9cb | 2004-07-09 17:48:53 +0000 | [diff] [blame] | 1574 | |
| 1575 | if (attribute_len == 6 && ! withdraw_len |
| 1576 | && mp_withdraw.afi == AFI_IP6 |
| 1577 | && mp_withdraw.safi == SAFI_UNICAST |
| 1578 | && mp_withdraw.length == 0) |
| 1579 | { |
| 1580 | /* End-of-RIB received */ |
| 1581 | |
| 1582 | if (BGP_DEBUG (update, UPDATE_IN)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1583 | zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv6 Unicast from %s", |
paul | e01f9cb | 2004-07-09 17:48:53 +0000 | [diff] [blame] | 1584 | peer->host); |
| 1585 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1586 | } |
| 1587 | if (peer->afc[AFI_IP6][SAFI_MULTICAST]) |
| 1588 | { |
| 1589 | if (mp_update.length |
| 1590 | && mp_update.afi == AFI_IP6 |
| 1591 | && mp_update.safi == SAFI_MULTICAST) |
| 1592 | bgp_nlri_parse (peer, &attr, &mp_update); |
| 1593 | |
| 1594 | if (mp_withdraw.length |
| 1595 | && mp_withdraw.afi == AFI_IP6 |
| 1596 | && mp_withdraw.safi == SAFI_MULTICAST) |
| 1597 | bgp_nlri_parse (peer, NULL, &mp_withdraw); |
paul | e01f9cb | 2004-07-09 17:48:53 +0000 | [diff] [blame] | 1598 | |
| 1599 | if (attribute_len == 6 && ! withdraw_len |
| 1600 | && mp_withdraw.afi == AFI_IP6 |
| 1601 | && mp_withdraw.safi == SAFI_MULTICAST |
| 1602 | && mp_withdraw.length == 0) |
| 1603 | { |
| 1604 | /* End-of-RIB received */ |
| 1605 | |
| 1606 | if (BGP_DEBUG (update, UPDATE_IN)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1607 | zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv6 Multicast from %s", |
paul | e01f9cb | 2004-07-09 17:48:53 +0000 | [diff] [blame] | 1608 | peer->host); |
| 1609 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1610 | } |
| 1611 | if (peer->afc[AFI_IP][SAFI_MPLS_VPN]) |
| 1612 | { |
| 1613 | if (mp_update.length |
| 1614 | && mp_update.afi == AFI_IP |
| 1615 | && mp_update.safi == BGP_SAFI_VPNV4) |
| 1616 | bgp_nlri_parse_vpnv4 (peer, &attr, &mp_update); |
| 1617 | |
| 1618 | if (mp_withdraw.length |
| 1619 | && mp_withdraw.afi == AFI_IP |
| 1620 | && mp_withdraw.safi == BGP_SAFI_VPNV4) |
| 1621 | bgp_nlri_parse_vpnv4 (peer, NULL, &mp_withdraw); |
paul | e01f9cb | 2004-07-09 17:48:53 +0000 | [diff] [blame] | 1622 | |
| 1623 | if (attribute_len == 6 && ! withdraw_len |
| 1624 | && mp_withdraw.afi == AFI_IP |
| 1625 | && mp_withdraw.safi == BGP_SAFI_VPNV4 |
| 1626 | && mp_withdraw.length == 0) |
| 1627 | { |
| 1628 | /* End-of-RIB received */ |
| 1629 | |
| 1630 | if (BGP_DEBUG (update, UPDATE_IN)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1631 | zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for VPNv4 Unicast from %s", |
paul | e01f9cb | 2004-07-09 17:48:53 +0000 | [diff] [blame] | 1632 | peer->host); |
| 1633 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | /* Everything is done. We unintern temporary structures which |
| 1637 | interned in bgp_attr_parse(). */ |
| 1638 | if (attr.aspath) |
| 1639 | aspath_unintern (attr.aspath); |
| 1640 | if (attr.community) |
| 1641 | community_unintern (attr.community); |
| 1642 | if (attr.ecommunity) |
| 1643 | ecommunity_unintern (attr.ecommunity); |
| 1644 | if (attr.cluster) |
| 1645 | cluster_unintern (attr.cluster); |
| 1646 | if (attr.transit) |
| 1647 | transit_unintern (attr.transit); |
| 1648 | |
| 1649 | /* If peering is stopped due to some reason, do not generate BGP |
| 1650 | event. */ |
| 1651 | if (peer->status != Established) |
| 1652 | return 0; |
| 1653 | |
| 1654 | /* Increment packet counter. */ |
| 1655 | peer->update_in++; |
| 1656 | peer->update_time = time (NULL); |
| 1657 | |
| 1658 | /* Generate BGP event. */ |
| 1659 | BGP_EVENT_ADD (peer, Receive_UPDATE_message); |
| 1660 | |
| 1661 | return 0; |
| 1662 | } |
| 1663 | |
| 1664 | /* Notify message treatment function. */ |
| 1665 | void |
| 1666 | bgp_notify_receive (struct peer *peer, bgp_size_t size) |
| 1667 | { |
| 1668 | struct bgp_notify bgp_notify; |
| 1669 | |
| 1670 | if (peer->notify.data) |
| 1671 | { |
| 1672 | XFREE (MTYPE_TMP, peer->notify.data); |
| 1673 | peer->notify.data = NULL; |
| 1674 | peer->notify.length = 0; |
| 1675 | } |
| 1676 | |
| 1677 | bgp_notify.code = stream_getc (peer->ibuf); |
| 1678 | bgp_notify.subcode = stream_getc (peer->ibuf); |
| 1679 | bgp_notify.length = size - 2; |
| 1680 | bgp_notify.data = NULL; |
| 1681 | |
| 1682 | /* Preserv notify code and sub code. */ |
| 1683 | peer->notify.code = bgp_notify.code; |
| 1684 | peer->notify.subcode = bgp_notify.subcode; |
| 1685 | /* For further diagnostic record returned Data. */ |
| 1686 | if (bgp_notify.length) |
| 1687 | { |
| 1688 | peer->notify.length = size - 2; |
| 1689 | peer->notify.data = XMALLOC (MTYPE_TMP, size - 2); |
| 1690 | memcpy (peer->notify.data, stream_pnt (peer->ibuf), size - 2); |
| 1691 | } |
| 1692 | |
| 1693 | /* For debug */ |
| 1694 | { |
| 1695 | int i; |
| 1696 | int first = 0; |
| 1697 | char c[4]; |
| 1698 | |
| 1699 | if (bgp_notify.length) |
| 1700 | { |
| 1701 | bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3); |
| 1702 | for (i = 0; i < bgp_notify.length; i++) |
| 1703 | if (first) |
| 1704 | { |
| 1705 | sprintf (c, " %02x", stream_getc (peer->ibuf)); |
| 1706 | strcat (bgp_notify.data, c); |
| 1707 | } |
| 1708 | else |
| 1709 | { |
| 1710 | first = 1; |
| 1711 | sprintf (c, "%02x", stream_getc (peer->ibuf)); |
| 1712 | strcpy (bgp_notify.data, c); |
| 1713 | } |
| 1714 | } |
| 1715 | |
| 1716 | bgp_notify_print(peer, &bgp_notify, "received"); |
| 1717 | if (bgp_notify.data) |
| 1718 | XFREE (MTYPE_TMP, bgp_notify.data); |
| 1719 | } |
| 1720 | |
| 1721 | /* peer count update */ |
| 1722 | peer->notify_in++; |
| 1723 | |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 1724 | if (peer->status == Established) |
| 1725 | peer->last_reset = PEER_DOWN_NOTIFY_RECEIVED; |
| 1726 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1727 | /* We have to check for Notify with Unsupported Optional Parameter. |
| 1728 | in that case we fallback to open without the capability option. |
| 1729 | But this done in bgp_stop. We just mark it here to avoid changing |
| 1730 | the fsm tables. */ |
| 1731 | if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR && |
| 1732 | bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM ) |
| 1733 | UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN); |
| 1734 | |
| 1735 | /* Also apply to Unsupported Capability until remote router support |
| 1736 | capability. */ |
| 1737 | if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR && |
| 1738 | bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_CAPBL) |
| 1739 | UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN); |
| 1740 | |
| 1741 | BGP_EVENT_ADD (peer, Receive_NOTIFICATION_message); |
| 1742 | } |
| 1743 | |
| 1744 | /* Keepalive treatment function -- get keepalive send keepalive */ |
| 1745 | void |
| 1746 | bgp_keepalive_receive (struct peer *peer, bgp_size_t size) |
| 1747 | { |
| 1748 | if (BGP_DEBUG (keepalive, KEEPALIVE)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1749 | zlog_debug ("%s KEEPALIVE rcvd", peer->host); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1750 | |
| 1751 | BGP_EVENT_ADD (peer, Receive_KEEPALIVE_message); |
| 1752 | } |
| 1753 | |
| 1754 | /* Route refresh message is received. */ |
| 1755 | void |
| 1756 | bgp_route_refresh_receive (struct peer *peer, bgp_size_t size) |
| 1757 | { |
| 1758 | afi_t afi; |
| 1759 | safi_t safi; |
| 1760 | u_char reserved; |
| 1761 | struct stream *s; |
| 1762 | |
| 1763 | /* If peer does not have the capability, send notification. */ |
| 1764 | if (! CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_ADV)) |
| 1765 | { |
| 1766 | plog_err (peer->log, "%s [Error] BGP route refresh is not enabled", |
| 1767 | peer->host); |
| 1768 | bgp_notify_send (peer, |
| 1769 | BGP_NOTIFY_HEADER_ERR, |
| 1770 | BGP_NOTIFY_HEADER_BAD_MESTYPE); |
| 1771 | return; |
| 1772 | } |
| 1773 | |
| 1774 | /* Status must be Established. */ |
| 1775 | if (peer->status != Established) |
| 1776 | { |
| 1777 | plog_err (peer->log, |
| 1778 | "%s [Error] Route refresh packet received under status %s", |
| 1779 | peer->host, LOOKUP (bgp_status_msg, peer->status)); |
| 1780 | bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0); |
| 1781 | return; |
| 1782 | } |
| 1783 | |
| 1784 | s = peer->ibuf; |
| 1785 | |
| 1786 | /* Parse packet. */ |
| 1787 | afi = stream_getw (s); |
| 1788 | reserved = stream_getc (s); |
| 1789 | safi = stream_getc (s); |
| 1790 | |
| 1791 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1792 | zlog_debug ("%s rcvd REFRESH_REQ for afi/safi: %d/%d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1793 | peer->host, afi, safi); |
| 1794 | |
| 1795 | /* Check AFI and SAFI. */ |
| 1796 | if ((afi != AFI_IP && afi != AFI_IP6) |
| 1797 | || (safi != SAFI_UNICAST && safi != SAFI_MULTICAST |
| 1798 | && safi != BGP_SAFI_VPNV4)) |
| 1799 | { |
| 1800 | if (BGP_DEBUG (normal, NORMAL)) |
| 1801 | { |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1802 | zlog_debug ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1803 | peer->host, afi, safi); |
| 1804 | } |
| 1805 | return; |
| 1806 | } |
| 1807 | |
| 1808 | /* Adjust safi code. */ |
| 1809 | if (safi == BGP_SAFI_VPNV4) |
| 1810 | safi = SAFI_MPLS_VPN; |
| 1811 | |
| 1812 | if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) |
| 1813 | { |
| 1814 | u_char *end; |
| 1815 | u_char when_to_refresh; |
| 1816 | u_char orf_type; |
| 1817 | u_int16_t orf_len; |
| 1818 | |
| 1819 | if (size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) < 5) |
| 1820 | { |
| 1821 | zlog_info ("%s ORF route refresh length error", peer->host); |
| 1822 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 1823 | return; |
| 1824 | } |
| 1825 | |
| 1826 | when_to_refresh = stream_getc (s); |
| 1827 | end = stream_pnt (s) + (size - 5); |
| 1828 | |
| 1829 | while (stream_pnt (s) < end) |
| 1830 | { |
| 1831 | orf_type = stream_getc (s); |
| 1832 | orf_len = stream_getw (s); |
| 1833 | |
| 1834 | if (orf_type == ORF_TYPE_PREFIX |
| 1835 | || orf_type == ORF_TYPE_PREFIX_OLD) |
| 1836 | { |
| 1837 | u_char *p_pnt = stream_pnt (s); |
| 1838 | u_char *p_end = stream_pnt (s) + orf_len; |
| 1839 | struct orf_prefix orfp; |
| 1840 | u_char common = 0; |
| 1841 | u_int32_t seq; |
| 1842 | int psize; |
| 1843 | char name[BUFSIZ]; |
| 1844 | char buf[BUFSIZ]; |
| 1845 | int ret; |
| 1846 | |
| 1847 | if (BGP_DEBUG (normal, NORMAL)) |
| 1848 | { |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1849 | zlog_debug ("%s rcvd Prefixlist ORF(%d) length %d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1850 | peer->host, orf_type, orf_len); |
| 1851 | } |
| 1852 | |
| 1853 | /* ORF prefix-list name */ |
| 1854 | sprintf (name, "%s.%d.%d", peer->host, afi, safi); |
| 1855 | |
| 1856 | while (p_pnt < p_end) |
| 1857 | { |
| 1858 | memset (&orfp, 0, sizeof (struct orf_prefix)); |
| 1859 | common = *p_pnt++; |
| 1860 | if (common & ORF_COMMON_PART_REMOVE_ALL) |
| 1861 | { |
| 1862 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1863 | zlog_debug ("%s rcvd Remove-All pfxlist ORF request", peer->host); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1864 | prefix_bgp_orf_remove_all (name); |
| 1865 | break; |
| 1866 | } |
| 1867 | memcpy (&seq, p_pnt, sizeof (u_int32_t)); |
| 1868 | p_pnt += sizeof (u_int32_t); |
| 1869 | orfp.seq = ntohl (seq); |
| 1870 | orfp.ge = *p_pnt++; |
| 1871 | orfp.le = *p_pnt++; |
| 1872 | orfp.p.prefixlen = *p_pnt++; |
| 1873 | orfp.p.family = afi2family (afi); |
| 1874 | psize = PSIZE (orfp.p.prefixlen); |
| 1875 | memcpy (&orfp.p.u.prefix, p_pnt, psize); |
| 1876 | p_pnt += psize; |
| 1877 | |
| 1878 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1879 | zlog_debug ("%s rcvd %s %s seq %u %s/%d ge %d le %d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1880 | peer->host, |
| 1881 | (common & ORF_COMMON_PART_REMOVE ? "Remove" : "Add"), |
| 1882 | (common & ORF_COMMON_PART_DENY ? "deny" : "permit"), |
| 1883 | orfp.seq, |
| 1884 | inet_ntop (orfp.p.family, &orfp.p.u.prefix, buf, BUFSIZ), |
| 1885 | orfp.p.prefixlen, orfp.ge, orfp.le); |
| 1886 | |
| 1887 | ret = prefix_bgp_orf_set (name, afi, &orfp, |
| 1888 | (common & ORF_COMMON_PART_DENY ? 0 : 1 ), |
| 1889 | (common & ORF_COMMON_PART_REMOVE ? 0 : 1)); |
| 1890 | |
| 1891 | if (ret != CMD_SUCCESS) |
| 1892 | { |
| 1893 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1894 | zlog_debug ("%s Received misformatted prefixlist ORF. Remove All pfxlist", peer->host); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1895 | prefix_bgp_orf_remove_all (name); |
| 1896 | break; |
| 1897 | } |
| 1898 | } |
| 1899 | peer->orf_plist[afi][safi] = |
| 1900 | prefix_list_lookup (AFI_ORF_PREFIX, name); |
| 1901 | } |
| 1902 | stream_forward (s, orf_len); |
| 1903 | } |
| 1904 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1905 | zlog_debug ("%s rcvd Refresh %s ORF request", peer->host, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1906 | when_to_refresh == REFRESH_DEFER ? "Defer" : "Immediate"); |
| 1907 | if (when_to_refresh == REFRESH_DEFER) |
| 1908 | return; |
| 1909 | } |
| 1910 | |
| 1911 | /* First update is deferred until ORF or ROUTE-REFRESH is received */ |
| 1912 | if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH)) |
| 1913 | UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH); |
| 1914 | |
| 1915 | /* Perform route refreshment to the peer */ |
| 1916 | bgp_announce_route (peer, afi, safi); |
| 1917 | } |
| 1918 | |
| 1919 | int |
| 1920 | bgp_capability_msg_parse (struct peer *peer, u_char *pnt, bgp_size_t length) |
| 1921 | { |
| 1922 | u_char *end; |
| 1923 | struct capability cap; |
| 1924 | u_char action; |
| 1925 | struct bgp *bgp; |
| 1926 | afi_t afi; |
| 1927 | safi_t safi; |
| 1928 | |
| 1929 | bgp = peer->bgp; |
| 1930 | end = pnt + length; |
| 1931 | |
| 1932 | while (pnt < end) |
| 1933 | { |
| 1934 | /* We need at least action, capability code and capability length. */ |
| 1935 | if (pnt + 3 > end) |
| 1936 | { |
| 1937 | zlog_info ("%s Capability length error", peer->host); |
| 1938 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 1939 | return -1; |
| 1940 | } |
| 1941 | |
| 1942 | action = *pnt; |
| 1943 | |
| 1944 | /* Fetch structure to the byte stream. */ |
| 1945 | memcpy (&cap, pnt + 1, sizeof (struct capability)); |
| 1946 | |
| 1947 | /* Action value check. */ |
| 1948 | if (action != CAPABILITY_ACTION_SET |
| 1949 | && action != CAPABILITY_ACTION_UNSET) |
| 1950 | { |
| 1951 | zlog_info ("%s Capability Action Value error %d", |
| 1952 | peer->host, action); |
| 1953 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 1954 | return -1; |
| 1955 | } |
| 1956 | |
| 1957 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1958 | zlog_debug ("%s CAPABILITY has action: %d, code: %u, length %u", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1959 | peer->host, action, cap.code, cap.length); |
| 1960 | |
| 1961 | /* Capability length check. */ |
| 1962 | if (pnt + (cap.length + 3) > end) |
| 1963 | { |
| 1964 | zlog_info ("%s Capability length error", peer->host); |
| 1965 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 1966 | return -1; |
| 1967 | } |
| 1968 | |
| 1969 | /* We know MP Capability Code. */ |
| 1970 | if (cap.code == CAPABILITY_CODE_MP) |
| 1971 | { |
| 1972 | afi = ntohs (cap.mpc.afi); |
| 1973 | safi = cap.mpc.safi; |
| 1974 | |
| 1975 | /* Ignore capability when override-capability is set. */ |
| 1976 | if (CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) |
| 1977 | continue; |
| 1978 | |
| 1979 | /* Address family check. */ |
| 1980 | if ((afi == AFI_IP |
| 1981 | || afi == AFI_IP6) |
| 1982 | && (safi == SAFI_UNICAST |
| 1983 | || safi == SAFI_MULTICAST |
| 1984 | || safi == BGP_SAFI_VPNV4)) |
| 1985 | { |
| 1986 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1987 | zlog_debug ("%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1988 | peer->host, |
| 1989 | action == CAPABILITY_ACTION_SET |
| 1990 | ? "Advertising" : "Removing", |
| 1991 | ntohs(cap.mpc.afi) , cap.mpc.safi); |
| 1992 | |
| 1993 | /* Adjust safi code. */ |
| 1994 | if (safi == BGP_SAFI_VPNV4) |
| 1995 | safi = SAFI_MPLS_VPN; |
| 1996 | |
| 1997 | if (action == CAPABILITY_ACTION_SET) |
| 1998 | { |
| 1999 | peer->afc_recv[afi][safi] = 1; |
| 2000 | if (peer->afc[afi][safi]) |
| 2001 | { |
| 2002 | peer->afc_nego[afi][safi] = 1; |
| 2003 | bgp_announce_route (peer, afi, safi); |
| 2004 | } |
| 2005 | } |
| 2006 | else |
| 2007 | { |
| 2008 | peer->afc_recv[afi][safi] = 0; |
| 2009 | peer->afc_nego[afi][safi] = 0; |
| 2010 | |
| 2011 | if (peer_active_nego (peer)) |
| 2012 | bgp_clear_route (peer, afi, safi); |
| 2013 | else |
| 2014 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 2015 | } |
| 2016 | } |
| 2017 | } |
| 2018 | else if (cap.code == CAPABILITY_CODE_REFRESH |
| 2019 | || cap.code == CAPABILITY_CODE_REFRESH_OLD) |
| 2020 | { |
| 2021 | /* Check length. */ |
| 2022 | if (cap.length != 0) |
| 2023 | { |
| 2024 | zlog_info ("%s Route Refresh Capability length error %d", |
| 2025 | peer->host, cap.length); |
| 2026 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 2027 | return -1; |
| 2028 | } |
| 2029 | |
| 2030 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 2031 | zlog_debug ("%s CAPABILITY has %s ROUTE-REFRESH capability(%s) for all address-families", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2032 | peer->host, |
| 2033 | action == CAPABILITY_ACTION_SET |
| 2034 | ? "Advertising" : "Removing", |
| 2035 | cap.code == CAPABILITY_CODE_REFRESH_OLD |
| 2036 | ? "old" : "new"); |
| 2037 | |
| 2038 | /* BGP refresh capability */ |
| 2039 | if (action == CAPABILITY_ACTION_SET) |
| 2040 | { |
| 2041 | if (cap.code == CAPABILITY_CODE_REFRESH_OLD) |
| 2042 | SET_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV); |
| 2043 | else |
| 2044 | SET_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV); |
| 2045 | } |
| 2046 | else |
| 2047 | { |
| 2048 | if (cap.code == CAPABILITY_CODE_REFRESH_OLD) |
| 2049 | UNSET_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV); |
| 2050 | else |
| 2051 | UNSET_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV); |
| 2052 | } |
| 2053 | } |
| 2054 | else |
| 2055 | { |
| 2056 | zlog_warn ("%s unrecognized capability code: %d - ignored", |
| 2057 | peer->host, cap.code); |
| 2058 | } |
| 2059 | pnt += cap.length + 3; |
| 2060 | } |
| 2061 | return 0; |
| 2062 | } |
| 2063 | |
| 2064 | /* Dynamic Capability is received. */ |
| 2065 | void |
| 2066 | bgp_capability_receive (struct peer *peer, bgp_size_t size) |
| 2067 | { |
| 2068 | u_char *pnt; |
| 2069 | int ret; |
| 2070 | |
| 2071 | /* Fetch pointer. */ |
| 2072 | pnt = stream_pnt (peer->ibuf); |
| 2073 | |
| 2074 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 2075 | zlog_debug ("%s rcv CAPABILITY", peer->host); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2076 | |
| 2077 | /* If peer does not have the capability, send notification. */ |
| 2078 | if (! CHECK_FLAG (peer->cap, PEER_CAP_DYNAMIC_ADV)) |
| 2079 | { |
| 2080 | plog_err (peer->log, "%s [Error] BGP dynamic capability is not enabled", |
| 2081 | peer->host); |
| 2082 | bgp_notify_send (peer, |
| 2083 | BGP_NOTIFY_HEADER_ERR, |
| 2084 | BGP_NOTIFY_HEADER_BAD_MESTYPE); |
| 2085 | return; |
| 2086 | } |
| 2087 | |
| 2088 | /* Status must be Established. */ |
| 2089 | if (peer->status != Established) |
| 2090 | { |
| 2091 | plog_err (peer->log, |
| 2092 | "%s [Error] Dynamic capability packet received under status %s", peer->host, LOOKUP (bgp_status_msg, peer->status)); |
| 2093 | bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0); |
| 2094 | return; |
| 2095 | } |
| 2096 | |
| 2097 | /* Parse packet. */ |
| 2098 | ret = bgp_capability_msg_parse (peer, pnt, size); |
| 2099 | } |
| 2100 | |
| 2101 | /* BGP read utility function. */ |
| 2102 | int |
| 2103 | bgp_read_packet (struct peer *peer) |
| 2104 | { |
| 2105 | int nbytes; |
| 2106 | int readsize; |
| 2107 | |
| 2108 | readsize = peer->packet_size - peer->ibuf->putp; |
| 2109 | |
| 2110 | /* If size is zero then return. */ |
| 2111 | if (! readsize) |
| 2112 | return 0; |
| 2113 | |
| 2114 | /* Read packet from fd. */ |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 2115 | nbytes = stream_read_unblock (peer->ibuf, peer->fd, readsize); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2116 | |
| 2117 | /* If read byte is smaller than zero then error occured. */ |
| 2118 | if (nbytes < 0) |
| 2119 | { |
| 2120 | if (errno == EAGAIN) |
| 2121 | return -1; |
| 2122 | |
| 2123 | plog_err (peer->log, "%s [Error] bgp_read_packet error: %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 2124 | peer->host, safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2125 | BGP_EVENT_ADD (peer, TCP_fatal_error); |
| 2126 | return -1; |
| 2127 | } |
| 2128 | |
| 2129 | /* When read byte is zero : clear bgp peer and return */ |
| 2130 | if (nbytes == 0) |
| 2131 | { |
| 2132 | if (BGP_DEBUG (events, EVENTS)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 2133 | plog_debug (peer->log, "%s [Event] BGP connection closed fd %d", |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 2134 | peer->host, peer->fd); |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 2135 | |
| 2136 | if (peer->status == Established) |
| 2137 | peer->last_reset = PEER_DOWN_CLOSE_SESSION; |
| 2138 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2139 | BGP_EVENT_ADD (peer, TCP_connection_closed); |
| 2140 | return -1; |
| 2141 | } |
| 2142 | |
| 2143 | /* We read partial packet. */ |
| 2144 | if (peer->ibuf->putp != peer->packet_size) |
| 2145 | return -1; |
| 2146 | |
| 2147 | return 0; |
| 2148 | } |
| 2149 | |
| 2150 | /* Marker check. */ |
| 2151 | int |
| 2152 | bgp_marker_all_one (struct stream *s, int length) |
| 2153 | { |
| 2154 | int i; |
| 2155 | |
| 2156 | for (i = 0; i < length; i++) |
| 2157 | if (s->data[i] != 0xff) |
| 2158 | return 0; |
| 2159 | |
| 2160 | return 1; |
| 2161 | } |
| 2162 | |
| 2163 | /* Starting point of packet process function. */ |
| 2164 | int |
| 2165 | bgp_read (struct thread *thread) |
| 2166 | { |
| 2167 | int ret; |
| 2168 | u_char type = 0; |
| 2169 | struct peer *peer; |
| 2170 | bgp_size_t size; |
| 2171 | char notify_data_length[2]; |
| 2172 | |
| 2173 | /* Yes first of all get peer pointer. */ |
| 2174 | peer = THREAD_ARG (thread); |
| 2175 | peer->t_read = NULL; |
| 2176 | |
| 2177 | /* For non-blocking IO check. */ |
| 2178 | if (peer->status == Connect) |
| 2179 | { |
| 2180 | bgp_connect_check (peer); |
| 2181 | goto done; |
| 2182 | } |
| 2183 | else |
| 2184 | { |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 2185 | if (peer->fd < 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2186 | { |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 2187 | zlog_err ("bgp_read peer's fd is negative value %d", peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2188 | return -1; |
| 2189 | } |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 2190 | BGP_READ_ON (peer->t_read, bgp_read, peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2191 | } |
| 2192 | |
| 2193 | /* Read packet header to determine type of the packet */ |
| 2194 | if (peer->packet_size == 0) |
| 2195 | peer->packet_size = BGP_HEADER_SIZE; |
| 2196 | |
| 2197 | if (peer->ibuf->putp < BGP_HEADER_SIZE) |
| 2198 | { |
| 2199 | ret = bgp_read_packet (peer); |
| 2200 | |
| 2201 | /* Header read error or partial read packet. */ |
| 2202 | if (ret < 0) |
| 2203 | goto done; |
| 2204 | |
| 2205 | /* Get size and type. */ |
| 2206 | stream_forward (peer->ibuf, BGP_MARKER_SIZE); |
| 2207 | memcpy (notify_data_length, stream_pnt (peer->ibuf), 2); |
| 2208 | size = stream_getw (peer->ibuf); |
| 2209 | type = stream_getc (peer->ibuf); |
| 2210 | |
| 2211 | if (BGP_DEBUG (normal, NORMAL) && type != 2 && type != 0) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 2212 | zlog_debug ("%s rcv message type %d, length (excl. header) %d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2213 | peer->host, type, size - BGP_HEADER_SIZE); |
| 2214 | |
| 2215 | /* Marker check */ |
paul | f5ba387 | 2004-07-09 12:11:31 +0000 | [diff] [blame] | 2216 | if (((type == BGP_MSG_OPEN) || (type == BGP_MSG_KEEPALIVE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2217 | && ! bgp_marker_all_one (peer->ibuf, BGP_MARKER_SIZE)) |
| 2218 | { |
| 2219 | bgp_notify_send (peer, |
| 2220 | BGP_NOTIFY_HEADER_ERR, |
| 2221 | BGP_NOTIFY_HEADER_NOT_SYNC); |
| 2222 | goto done; |
| 2223 | } |
| 2224 | |
| 2225 | /* BGP type check. */ |
| 2226 | if (type != BGP_MSG_OPEN && type != BGP_MSG_UPDATE |
| 2227 | && type != BGP_MSG_NOTIFY && type != BGP_MSG_KEEPALIVE |
| 2228 | && type != BGP_MSG_ROUTE_REFRESH_NEW |
| 2229 | && type != BGP_MSG_ROUTE_REFRESH_OLD |
| 2230 | && type != BGP_MSG_CAPABILITY) |
| 2231 | { |
| 2232 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 2233 | plog_debug (peer->log, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2234 | "%s unknown message type 0x%02x", |
| 2235 | peer->host, type); |
| 2236 | bgp_notify_send_with_data (peer, |
| 2237 | BGP_NOTIFY_HEADER_ERR, |
| 2238 | BGP_NOTIFY_HEADER_BAD_MESTYPE, |
| 2239 | &type, 1); |
| 2240 | goto done; |
| 2241 | } |
| 2242 | /* Mimimum packet length check. */ |
| 2243 | if ((size < BGP_HEADER_SIZE) |
| 2244 | || (size > BGP_MAX_PACKET_SIZE) |
| 2245 | || (type == BGP_MSG_OPEN && size < BGP_MSG_OPEN_MIN_SIZE) |
| 2246 | || (type == BGP_MSG_UPDATE && size < BGP_MSG_UPDATE_MIN_SIZE) |
| 2247 | || (type == BGP_MSG_NOTIFY && size < BGP_MSG_NOTIFY_MIN_SIZE) |
| 2248 | || (type == BGP_MSG_KEEPALIVE && size != BGP_MSG_KEEPALIVE_MIN_SIZE) |
| 2249 | || (type == BGP_MSG_ROUTE_REFRESH_NEW && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE) |
| 2250 | || (type == BGP_MSG_ROUTE_REFRESH_OLD && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE) |
| 2251 | || (type == BGP_MSG_CAPABILITY && size < BGP_MSG_CAPABILITY_MIN_SIZE)) |
| 2252 | { |
| 2253 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 2254 | plog_debug (peer->log, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2255 | "%s bad message length - %d for %s", |
| 2256 | peer->host, size, |
| 2257 | type == 128 ? "ROUTE-REFRESH" : |
| 2258 | bgp_type_str[(int) type]); |
| 2259 | bgp_notify_send_with_data (peer, |
| 2260 | BGP_NOTIFY_HEADER_ERR, |
| 2261 | BGP_NOTIFY_HEADER_BAD_MESLEN, |
hasso | c9e52be | 2004-09-26 16:09:34 +0000 | [diff] [blame] | 2262 | (u_char *) notify_data_length, 2); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2263 | goto done; |
| 2264 | } |
| 2265 | |
| 2266 | /* Adjust size to message length. */ |
| 2267 | peer->packet_size = size; |
| 2268 | } |
| 2269 | |
| 2270 | ret = bgp_read_packet (peer); |
| 2271 | if (ret < 0) |
| 2272 | goto done; |
| 2273 | |
| 2274 | /* Get size and type again. */ |
| 2275 | size = stream_getw_from (peer->ibuf, BGP_MARKER_SIZE); |
| 2276 | type = stream_getc_from (peer->ibuf, BGP_MARKER_SIZE + 2); |
| 2277 | |
| 2278 | /* BGP packet dump function. */ |
| 2279 | bgp_dump_packet (peer, type, peer->ibuf); |
| 2280 | |
| 2281 | size = (peer->packet_size - BGP_HEADER_SIZE); |
| 2282 | |
| 2283 | /* Read rest of the packet and call each sort of packet routine */ |
| 2284 | switch (type) |
| 2285 | { |
| 2286 | case BGP_MSG_OPEN: |
| 2287 | peer->open_in++; |
paul | f5ba387 | 2004-07-09 12:11:31 +0000 | [diff] [blame] | 2288 | bgp_open_receive (peer, size); /* XXX return value ignored! */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2289 | break; |
| 2290 | case BGP_MSG_UPDATE: |
| 2291 | peer->readtime = time(NULL); /* Last read timer reset */ |
| 2292 | bgp_update_receive (peer, size); |
| 2293 | break; |
| 2294 | case BGP_MSG_NOTIFY: |
| 2295 | bgp_notify_receive (peer, size); |
| 2296 | break; |
| 2297 | case BGP_MSG_KEEPALIVE: |
| 2298 | peer->readtime = time(NULL); /* Last read timer reset */ |
| 2299 | bgp_keepalive_receive (peer, size); |
| 2300 | break; |
| 2301 | case BGP_MSG_ROUTE_REFRESH_NEW: |
| 2302 | case BGP_MSG_ROUTE_REFRESH_OLD: |
| 2303 | peer->refresh_in++; |
| 2304 | bgp_route_refresh_receive (peer, size); |
| 2305 | break; |
| 2306 | case BGP_MSG_CAPABILITY: |
| 2307 | peer->dynamic_cap_in++; |
| 2308 | bgp_capability_receive (peer, size); |
| 2309 | break; |
| 2310 | } |
| 2311 | |
| 2312 | /* Clear input buffer. */ |
| 2313 | peer->packet_size = 0; |
| 2314 | if (peer->ibuf) |
| 2315 | stream_reset (peer->ibuf); |
| 2316 | |
| 2317 | done: |
| 2318 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 2319 | { |
| 2320 | if (BGP_DEBUG (events, EVENTS)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 2321 | zlog_debug ("%s [Event] Accepting BGP peer delete", peer->host); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2322 | peer_delete (peer); |
| 2323 | } |
| 2324 | return 0; |
| 2325 | } |