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 | |
hasso | f418446 | 2005-02-01 20:13:16 +0000 | [diff] [blame^] | 1530 | if (mp_update.length |
| 1531 | && mp_update.afi == AFI_IP |
| 1532 | && mp_update.safi == SAFI_UNICAST) |
| 1533 | bgp_nlri_parse (peer, &attr, &mp_update); |
| 1534 | |
| 1535 | if (mp_withdraw.length |
| 1536 | && mp_withdraw.afi == AFI_IP |
| 1537 | && mp_withdraw.safi == SAFI_UNICAST) |
| 1538 | bgp_nlri_parse (peer, NULL, &mp_withdraw); |
| 1539 | |
paul | e01f9cb | 2004-07-09 17:48:53 +0000 | [diff] [blame] | 1540 | if (! attribute_len && ! withdraw_len) |
| 1541 | { |
| 1542 | /* End-of-RIB received */ |
| 1543 | |
| 1544 | if (BGP_DEBUG (update, UPDATE_IN)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1545 | 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] | 1546 | peer->host); |
| 1547 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1548 | } |
| 1549 | if (peer->afc[AFI_IP][SAFI_MULTICAST]) |
| 1550 | { |
| 1551 | if (mp_update.length |
| 1552 | && mp_update.afi == AFI_IP |
| 1553 | && mp_update.safi == SAFI_MULTICAST) |
| 1554 | bgp_nlri_parse (peer, &attr, &mp_update); |
| 1555 | |
| 1556 | if (mp_withdraw.length |
| 1557 | && mp_withdraw.afi == AFI_IP |
| 1558 | && mp_withdraw.safi == SAFI_MULTICAST) |
| 1559 | bgp_nlri_parse (peer, NULL, &mp_withdraw); |
paul | e01f9cb | 2004-07-09 17:48:53 +0000 | [diff] [blame] | 1560 | |
| 1561 | if (attribute_len == 6 && ! withdraw_len |
| 1562 | && mp_withdraw.afi == AFI_IP |
| 1563 | && mp_withdraw.safi == SAFI_MULTICAST |
| 1564 | && mp_withdraw.length == 0) |
| 1565 | { |
| 1566 | /* End-of-RIB received */ |
| 1567 | |
| 1568 | if (BGP_DEBUG (update, UPDATE_IN)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1569 | 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] | 1570 | peer->host); |
| 1571 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1572 | } |
| 1573 | if (peer->afc[AFI_IP6][SAFI_UNICAST]) |
| 1574 | { |
| 1575 | if (mp_update.length |
| 1576 | && mp_update.afi == AFI_IP6 |
| 1577 | && mp_update.safi == SAFI_UNICAST) |
| 1578 | bgp_nlri_parse (peer, &attr, &mp_update); |
| 1579 | |
| 1580 | if (mp_withdraw.length |
| 1581 | && mp_withdraw.afi == AFI_IP6 |
| 1582 | && mp_withdraw.safi == SAFI_UNICAST) |
| 1583 | bgp_nlri_parse (peer, NULL, &mp_withdraw); |
paul | e01f9cb | 2004-07-09 17:48:53 +0000 | [diff] [blame] | 1584 | |
| 1585 | if (attribute_len == 6 && ! withdraw_len |
| 1586 | && mp_withdraw.afi == AFI_IP6 |
| 1587 | && mp_withdraw.safi == SAFI_UNICAST |
| 1588 | && mp_withdraw.length == 0) |
| 1589 | { |
| 1590 | /* End-of-RIB received */ |
| 1591 | |
| 1592 | if (BGP_DEBUG (update, UPDATE_IN)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1593 | 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] | 1594 | peer->host); |
| 1595 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1596 | } |
| 1597 | if (peer->afc[AFI_IP6][SAFI_MULTICAST]) |
| 1598 | { |
| 1599 | if (mp_update.length |
| 1600 | && mp_update.afi == AFI_IP6 |
| 1601 | && mp_update.safi == SAFI_MULTICAST) |
| 1602 | bgp_nlri_parse (peer, &attr, &mp_update); |
| 1603 | |
| 1604 | if (mp_withdraw.length |
| 1605 | && mp_withdraw.afi == AFI_IP6 |
| 1606 | && mp_withdraw.safi == SAFI_MULTICAST) |
| 1607 | bgp_nlri_parse (peer, NULL, &mp_withdraw); |
paul | e01f9cb | 2004-07-09 17:48:53 +0000 | [diff] [blame] | 1608 | |
| 1609 | if (attribute_len == 6 && ! withdraw_len |
| 1610 | && mp_withdraw.afi == AFI_IP6 |
| 1611 | && mp_withdraw.safi == SAFI_MULTICAST |
| 1612 | && mp_withdraw.length == 0) |
| 1613 | { |
| 1614 | /* End-of-RIB received */ |
| 1615 | |
| 1616 | if (BGP_DEBUG (update, UPDATE_IN)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1617 | 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] | 1618 | peer->host); |
| 1619 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1620 | } |
| 1621 | if (peer->afc[AFI_IP][SAFI_MPLS_VPN]) |
| 1622 | { |
| 1623 | if (mp_update.length |
| 1624 | && mp_update.afi == AFI_IP |
| 1625 | && mp_update.safi == BGP_SAFI_VPNV4) |
| 1626 | bgp_nlri_parse_vpnv4 (peer, &attr, &mp_update); |
| 1627 | |
| 1628 | if (mp_withdraw.length |
| 1629 | && mp_withdraw.afi == AFI_IP |
| 1630 | && mp_withdraw.safi == BGP_SAFI_VPNV4) |
| 1631 | bgp_nlri_parse_vpnv4 (peer, NULL, &mp_withdraw); |
paul | e01f9cb | 2004-07-09 17:48:53 +0000 | [diff] [blame] | 1632 | |
| 1633 | if (attribute_len == 6 && ! withdraw_len |
| 1634 | && mp_withdraw.afi == AFI_IP |
| 1635 | && mp_withdraw.safi == BGP_SAFI_VPNV4 |
| 1636 | && mp_withdraw.length == 0) |
| 1637 | { |
| 1638 | /* End-of-RIB received */ |
| 1639 | |
| 1640 | if (BGP_DEBUG (update, UPDATE_IN)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1641 | 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] | 1642 | peer->host); |
| 1643 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | /* Everything is done. We unintern temporary structures which |
| 1647 | interned in bgp_attr_parse(). */ |
| 1648 | if (attr.aspath) |
| 1649 | aspath_unintern (attr.aspath); |
| 1650 | if (attr.community) |
| 1651 | community_unintern (attr.community); |
| 1652 | if (attr.ecommunity) |
| 1653 | ecommunity_unintern (attr.ecommunity); |
| 1654 | if (attr.cluster) |
| 1655 | cluster_unintern (attr.cluster); |
| 1656 | if (attr.transit) |
| 1657 | transit_unintern (attr.transit); |
| 1658 | |
| 1659 | /* If peering is stopped due to some reason, do not generate BGP |
| 1660 | event. */ |
| 1661 | if (peer->status != Established) |
| 1662 | return 0; |
| 1663 | |
| 1664 | /* Increment packet counter. */ |
| 1665 | peer->update_in++; |
| 1666 | peer->update_time = time (NULL); |
| 1667 | |
| 1668 | /* Generate BGP event. */ |
| 1669 | BGP_EVENT_ADD (peer, Receive_UPDATE_message); |
| 1670 | |
| 1671 | return 0; |
| 1672 | } |
| 1673 | |
| 1674 | /* Notify message treatment function. */ |
| 1675 | void |
| 1676 | bgp_notify_receive (struct peer *peer, bgp_size_t size) |
| 1677 | { |
| 1678 | struct bgp_notify bgp_notify; |
| 1679 | |
| 1680 | if (peer->notify.data) |
| 1681 | { |
| 1682 | XFREE (MTYPE_TMP, peer->notify.data); |
| 1683 | peer->notify.data = NULL; |
| 1684 | peer->notify.length = 0; |
| 1685 | } |
| 1686 | |
| 1687 | bgp_notify.code = stream_getc (peer->ibuf); |
| 1688 | bgp_notify.subcode = stream_getc (peer->ibuf); |
| 1689 | bgp_notify.length = size - 2; |
| 1690 | bgp_notify.data = NULL; |
| 1691 | |
| 1692 | /* Preserv notify code and sub code. */ |
| 1693 | peer->notify.code = bgp_notify.code; |
| 1694 | peer->notify.subcode = bgp_notify.subcode; |
| 1695 | /* For further diagnostic record returned Data. */ |
| 1696 | if (bgp_notify.length) |
| 1697 | { |
| 1698 | peer->notify.length = size - 2; |
| 1699 | peer->notify.data = XMALLOC (MTYPE_TMP, size - 2); |
| 1700 | memcpy (peer->notify.data, stream_pnt (peer->ibuf), size - 2); |
| 1701 | } |
| 1702 | |
| 1703 | /* For debug */ |
| 1704 | { |
| 1705 | int i; |
| 1706 | int first = 0; |
| 1707 | char c[4]; |
| 1708 | |
| 1709 | if (bgp_notify.length) |
| 1710 | { |
| 1711 | bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3); |
| 1712 | for (i = 0; i < bgp_notify.length; i++) |
| 1713 | if (first) |
| 1714 | { |
| 1715 | sprintf (c, " %02x", stream_getc (peer->ibuf)); |
| 1716 | strcat (bgp_notify.data, c); |
| 1717 | } |
| 1718 | else |
| 1719 | { |
| 1720 | first = 1; |
| 1721 | sprintf (c, "%02x", stream_getc (peer->ibuf)); |
| 1722 | strcpy (bgp_notify.data, c); |
| 1723 | } |
| 1724 | } |
| 1725 | |
| 1726 | bgp_notify_print(peer, &bgp_notify, "received"); |
| 1727 | if (bgp_notify.data) |
| 1728 | XFREE (MTYPE_TMP, bgp_notify.data); |
| 1729 | } |
| 1730 | |
| 1731 | /* peer count update */ |
| 1732 | peer->notify_in++; |
| 1733 | |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 1734 | if (peer->status == Established) |
| 1735 | peer->last_reset = PEER_DOWN_NOTIFY_RECEIVED; |
| 1736 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1737 | /* We have to check for Notify with Unsupported Optional Parameter. |
| 1738 | in that case we fallback to open without the capability option. |
| 1739 | But this done in bgp_stop. We just mark it here to avoid changing |
| 1740 | the fsm tables. */ |
| 1741 | if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR && |
| 1742 | bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM ) |
| 1743 | UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN); |
| 1744 | |
| 1745 | /* Also apply to Unsupported Capability until remote router support |
| 1746 | capability. */ |
| 1747 | if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR && |
| 1748 | bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_CAPBL) |
| 1749 | UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN); |
| 1750 | |
| 1751 | BGP_EVENT_ADD (peer, Receive_NOTIFICATION_message); |
| 1752 | } |
| 1753 | |
| 1754 | /* Keepalive treatment function -- get keepalive send keepalive */ |
| 1755 | void |
| 1756 | bgp_keepalive_receive (struct peer *peer, bgp_size_t size) |
| 1757 | { |
| 1758 | if (BGP_DEBUG (keepalive, KEEPALIVE)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1759 | zlog_debug ("%s KEEPALIVE rcvd", peer->host); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1760 | |
| 1761 | BGP_EVENT_ADD (peer, Receive_KEEPALIVE_message); |
| 1762 | } |
| 1763 | |
| 1764 | /* Route refresh message is received. */ |
| 1765 | void |
| 1766 | bgp_route_refresh_receive (struct peer *peer, bgp_size_t size) |
| 1767 | { |
| 1768 | afi_t afi; |
| 1769 | safi_t safi; |
| 1770 | u_char reserved; |
| 1771 | struct stream *s; |
| 1772 | |
| 1773 | /* If peer does not have the capability, send notification. */ |
| 1774 | if (! CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_ADV)) |
| 1775 | { |
| 1776 | plog_err (peer->log, "%s [Error] BGP route refresh is not enabled", |
| 1777 | peer->host); |
| 1778 | bgp_notify_send (peer, |
| 1779 | BGP_NOTIFY_HEADER_ERR, |
| 1780 | BGP_NOTIFY_HEADER_BAD_MESTYPE); |
| 1781 | return; |
| 1782 | } |
| 1783 | |
| 1784 | /* Status must be Established. */ |
| 1785 | if (peer->status != Established) |
| 1786 | { |
| 1787 | plog_err (peer->log, |
| 1788 | "%s [Error] Route refresh packet received under status %s", |
| 1789 | peer->host, LOOKUP (bgp_status_msg, peer->status)); |
| 1790 | bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0); |
| 1791 | return; |
| 1792 | } |
| 1793 | |
| 1794 | s = peer->ibuf; |
| 1795 | |
| 1796 | /* Parse packet. */ |
| 1797 | afi = stream_getw (s); |
| 1798 | reserved = stream_getc (s); |
| 1799 | safi = stream_getc (s); |
| 1800 | |
| 1801 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1802 | zlog_debug ("%s rcvd REFRESH_REQ for afi/safi: %d/%d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1803 | peer->host, afi, safi); |
| 1804 | |
| 1805 | /* Check AFI and SAFI. */ |
| 1806 | if ((afi != AFI_IP && afi != AFI_IP6) |
| 1807 | || (safi != SAFI_UNICAST && safi != SAFI_MULTICAST |
| 1808 | && safi != BGP_SAFI_VPNV4)) |
| 1809 | { |
| 1810 | if (BGP_DEBUG (normal, NORMAL)) |
| 1811 | { |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1812 | zlog_debug ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1813 | peer->host, afi, safi); |
| 1814 | } |
| 1815 | return; |
| 1816 | } |
| 1817 | |
| 1818 | /* Adjust safi code. */ |
| 1819 | if (safi == BGP_SAFI_VPNV4) |
| 1820 | safi = SAFI_MPLS_VPN; |
| 1821 | |
| 1822 | if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) |
| 1823 | { |
| 1824 | u_char *end; |
| 1825 | u_char when_to_refresh; |
| 1826 | u_char orf_type; |
| 1827 | u_int16_t orf_len; |
| 1828 | |
| 1829 | if (size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) < 5) |
| 1830 | { |
| 1831 | zlog_info ("%s ORF route refresh length error", peer->host); |
| 1832 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 1833 | return; |
| 1834 | } |
| 1835 | |
| 1836 | when_to_refresh = stream_getc (s); |
| 1837 | end = stream_pnt (s) + (size - 5); |
| 1838 | |
| 1839 | while (stream_pnt (s) < end) |
| 1840 | { |
| 1841 | orf_type = stream_getc (s); |
| 1842 | orf_len = stream_getw (s); |
| 1843 | |
| 1844 | if (orf_type == ORF_TYPE_PREFIX |
| 1845 | || orf_type == ORF_TYPE_PREFIX_OLD) |
| 1846 | { |
| 1847 | u_char *p_pnt = stream_pnt (s); |
| 1848 | u_char *p_end = stream_pnt (s) + orf_len; |
| 1849 | struct orf_prefix orfp; |
| 1850 | u_char common = 0; |
| 1851 | u_int32_t seq; |
| 1852 | int psize; |
| 1853 | char name[BUFSIZ]; |
| 1854 | char buf[BUFSIZ]; |
| 1855 | int ret; |
| 1856 | |
| 1857 | if (BGP_DEBUG (normal, NORMAL)) |
| 1858 | { |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1859 | zlog_debug ("%s rcvd Prefixlist ORF(%d) length %d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1860 | peer->host, orf_type, orf_len); |
| 1861 | } |
| 1862 | |
| 1863 | /* ORF prefix-list name */ |
| 1864 | sprintf (name, "%s.%d.%d", peer->host, afi, safi); |
| 1865 | |
| 1866 | while (p_pnt < p_end) |
| 1867 | { |
| 1868 | memset (&orfp, 0, sizeof (struct orf_prefix)); |
| 1869 | common = *p_pnt++; |
| 1870 | if (common & ORF_COMMON_PART_REMOVE_ALL) |
| 1871 | { |
| 1872 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1873 | zlog_debug ("%s rcvd Remove-All pfxlist ORF request", peer->host); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1874 | prefix_bgp_orf_remove_all (name); |
| 1875 | break; |
| 1876 | } |
| 1877 | memcpy (&seq, p_pnt, sizeof (u_int32_t)); |
| 1878 | p_pnt += sizeof (u_int32_t); |
| 1879 | orfp.seq = ntohl (seq); |
| 1880 | orfp.ge = *p_pnt++; |
| 1881 | orfp.le = *p_pnt++; |
| 1882 | orfp.p.prefixlen = *p_pnt++; |
| 1883 | orfp.p.family = afi2family (afi); |
| 1884 | psize = PSIZE (orfp.p.prefixlen); |
| 1885 | memcpy (&orfp.p.u.prefix, p_pnt, psize); |
| 1886 | p_pnt += psize; |
| 1887 | |
| 1888 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1889 | 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] | 1890 | peer->host, |
| 1891 | (common & ORF_COMMON_PART_REMOVE ? "Remove" : "Add"), |
| 1892 | (common & ORF_COMMON_PART_DENY ? "deny" : "permit"), |
| 1893 | orfp.seq, |
| 1894 | inet_ntop (orfp.p.family, &orfp.p.u.prefix, buf, BUFSIZ), |
| 1895 | orfp.p.prefixlen, orfp.ge, orfp.le); |
| 1896 | |
| 1897 | ret = prefix_bgp_orf_set (name, afi, &orfp, |
| 1898 | (common & ORF_COMMON_PART_DENY ? 0 : 1 ), |
| 1899 | (common & ORF_COMMON_PART_REMOVE ? 0 : 1)); |
| 1900 | |
| 1901 | if (ret != CMD_SUCCESS) |
| 1902 | { |
| 1903 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1904 | zlog_debug ("%s Received misformatted prefixlist ORF. Remove All pfxlist", peer->host); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1905 | prefix_bgp_orf_remove_all (name); |
| 1906 | break; |
| 1907 | } |
| 1908 | } |
| 1909 | peer->orf_plist[afi][safi] = |
| 1910 | prefix_list_lookup (AFI_ORF_PREFIX, name); |
| 1911 | } |
| 1912 | stream_forward (s, orf_len); |
| 1913 | } |
| 1914 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1915 | zlog_debug ("%s rcvd Refresh %s ORF request", peer->host, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1916 | when_to_refresh == REFRESH_DEFER ? "Defer" : "Immediate"); |
| 1917 | if (when_to_refresh == REFRESH_DEFER) |
| 1918 | return; |
| 1919 | } |
| 1920 | |
| 1921 | /* First update is deferred until ORF or ROUTE-REFRESH is received */ |
| 1922 | if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH)) |
| 1923 | UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH); |
| 1924 | |
| 1925 | /* Perform route refreshment to the peer */ |
| 1926 | bgp_announce_route (peer, afi, safi); |
| 1927 | } |
| 1928 | |
| 1929 | int |
| 1930 | bgp_capability_msg_parse (struct peer *peer, u_char *pnt, bgp_size_t length) |
| 1931 | { |
| 1932 | u_char *end; |
| 1933 | struct capability cap; |
| 1934 | u_char action; |
| 1935 | struct bgp *bgp; |
| 1936 | afi_t afi; |
| 1937 | safi_t safi; |
| 1938 | |
| 1939 | bgp = peer->bgp; |
| 1940 | end = pnt + length; |
| 1941 | |
| 1942 | while (pnt < end) |
| 1943 | { |
| 1944 | /* We need at least action, capability code and capability length. */ |
| 1945 | if (pnt + 3 > end) |
| 1946 | { |
| 1947 | zlog_info ("%s Capability length error", peer->host); |
| 1948 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 1949 | return -1; |
| 1950 | } |
| 1951 | |
| 1952 | action = *pnt; |
| 1953 | |
| 1954 | /* Fetch structure to the byte stream. */ |
| 1955 | memcpy (&cap, pnt + 1, sizeof (struct capability)); |
| 1956 | |
| 1957 | /* Action value check. */ |
| 1958 | if (action != CAPABILITY_ACTION_SET |
| 1959 | && action != CAPABILITY_ACTION_UNSET) |
| 1960 | { |
| 1961 | zlog_info ("%s Capability Action Value error %d", |
| 1962 | peer->host, action); |
| 1963 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 1964 | return -1; |
| 1965 | } |
| 1966 | |
| 1967 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1968 | zlog_debug ("%s CAPABILITY has action: %d, code: %u, length %u", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1969 | peer->host, action, cap.code, cap.length); |
| 1970 | |
| 1971 | /* Capability length check. */ |
| 1972 | if (pnt + (cap.length + 3) > end) |
| 1973 | { |
| 1974 | zlog_info ("%s Capability length error", peer->host); |
| 1975 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 1976 | return -1; |
| 1977 | } |
| 1978 | |
| 1979 | /* We know MP Capability Code. */ |
| 1980 | if (cap.code == CAPABILITY_CODE_MP) |
| 1981 | { |
| 1982 | afi = ntohs (cap.mpc.afi); |
| 1983 | safi = cap.mpc.safi; |
| 1984 | |
| 1985 | /* Ignore capability when override-capability is set. */ |
| 1986 | if (CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) |
| 1987 | continue; |
| 1988 | |
| 1989 | /* Address family check. */ |
| 1990 | if ((afi == AFI_IP |
| 1991 | || afi == AFI_IP6) |
| 1992 | && (safi == SAFI_UNICAST |
| 1993 | || safi == SAFI_MULTICAST |
| 1994 | || safi == BGP_SAFI_VPNV4)) |
| 1995 | { |
| 1996 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 1997 | 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] | 1998 | peer->host, |
| 1999 | action == CAPABILITY_ACTION_SET |
| 2000 | ? "Advertising" : "Removing", |
| 2001 | ntohs(cap.mpc.afi) , cap.mpc.safi); |
| 2002 | |
| 2003 | /* Adjust safi code. */ |
| 2004 | if (safi == BGP_SAFI_VPNV4) |
| 2005 | safi = SAFI_MPLS_VPN; |
| 2006 | |
| 2007 | if (action == CAPABILITY_ACTION_SET) |
| 2008 | { |
| 2009 | peer->afc_recv[afi][safi] = 1; |
| 2010 | if (peer->afc[afi][safi]) |
| 2011 | { |
| 2012 | peer->afc_nego[afi][safi] = 1; |
| 2013 | bgp_announce_route (peer, afi, safi); |
| 2014 | } |
| 2015 | } |
| 2016 | else |
| 2017 | { |
| 2018 | peer->afc_recv[afi][safi] = 0; |
| 2019 | peer->afc_nego[afi][safi] = 0; |
| 2020 | |
| 2021 | if (peer_active_nego (peer)) |
| 2022 | bgp_clear_route (peer, afi, safi); |
| 2023 | else |
| 2024 | BGP_EVENT_ADD (peer, BGP_Stop); |
| 2025 | } |
| 2026 | } |
| 2027 | } |
| 2028 | else if (cap.code == CAPABILITY_CODE_REFRESH |
| 2029 | || cap.code == CAPABILITY_CODE_REFRESH_OLD) |
| 2030 | { |
| 2031 | /* Check length. */ |
| 2032 | if (cap.length != 0) |
| 2033 | { |
| 2034 | zlog_info ("%s Route Refresh Capability length error %d", |
| 2035 | peer->host, cap.length); |
| 2036 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 2037 | return -1; |
| 2038 | } |
| 2039 | |
| 2040 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 2041 | 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] | 2042 | peer->host, |
| 2043 | action == CAPABILITY_ACTION_SET |
| 2044 | ? "Advertising" : "Removing", |
| 2045 | cap.code == CAPABILITY_CODE_REFRESH_OLD |
| 2046 | ? "old" : "new"); |
| 2047 | |
| 2048 | /* BGP refresh capability */ |
| 2049 | if (action == CAPABILITY_ACTION_SET) |
| 2050 | { |
| 2051 | if (cap.code == CAPABILITY_CODE_REFRESH_OLD) |
| 2052 | SET_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV); |
| 2053 | else |
| 2054 | SET_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV); |
| 2055 | } |
| 2056 | else |
| 2057 | { |
| 2058 | if (cap.code == CAPABILITY_CODE_REFRESH_OLD) |
| 2059 | UNSET_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV); |
| 2060 | else |
| 2061 | UNSET_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV); |
| 2062 | } |
| 2063 | } |
| 2064 | else |
| 2065 | { |
| 2066 | zlog_warn ("%s unrecognized capability code: %d - ignored", |
| 2067 | peer->host, cap.code); |
| 2068 | } |
| 2069 | pnt += cap.length + 3; |
| 2070 | } |
| 2071 | return 0; |
| 2072 | } |
| 2073 | |
| 2074 | /* Dynamic Capability is received. */ |
| 2075 | void |
| 2076 | bgp_capability_receive (struct peer *peer, bgp_size_t size) |
| 2077 | { |
| 2078 | u_char *pnt; |
| 2079 | int ret; |
| 2080 | |
| 2081 | /* Fetch pointer. */ |
| 2082 | pnt = stream_pnt (peer->ibuf); |
| 2083 | |
| 2084 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 2085 | zlog_debug ("%s rcv CAPABILITY", peer->host); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2086 | |
| 2087 | /* If peer does not have the capability, send notification. */ |
| 2088 | if (! CHECK_FLAG (peer->cap, PEER_CAP_DYNAMIC_ADV)) |
| 2089 | { |
| 2090 | plog_err (peer->log, "%s [Error] BGP dynamic capability is not enabled", |
| 2091 | peer->host); |
| 2092 | bgp_notify_send (peer, |
| 2093 | BGP_NOTIFY_HEADER_ERR, |
| 2094 | BGP_NOTIFY_HEADER_BAD_MESTYPE); |
| 2095 | return; |
| 2096 | } |
| 2097 | |
| 2098 | /* Status must be Established. */ |
| 2099 | if (peer->status != Established) |
| 2100 | { |
| 2101 | plog_err (peer->log, |
| 2102 | "%s [Error] Dynamic capability packet received under status %s", peer->host, LOOKUP (bgp_status_msg, peer->status)); |
| 2103 | bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0); |
| 2104 | return; |
| 2105 | } |
| 2106 | |
| 2107 | /* Parse packet. */ |
| 2108 | ret = bgp_capability_msg_parse (peer, pnt, size); |
| 2109 | } |
| 2110 | |
| 2111 | /* BGP read utility function. */ |
| 2112 | int |
| 2113 | bgp_read_packet (struct peer *peer) |
| 2114 | { |
| 2115 | int nbytes; |
| 2116 | int readsize; |
| 2117 | |
| 2118 | readsize = peer->packet_size - peer->ibuf->putp; |
| 2119 | |
| 2120 | /* If size is zero then return. */ |
| 2121 | if (! readsize) |
| 2122 | return 0; |
| 2123 | |
| 2124 | /* Read packet from fd. */ |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 2125 | nbytes = stream_read_unblock (peer->ibuf, peer->fd, readsize); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2126 | |
| 2127 | /* If read byte is smaller than zero then error occured. */ |
| 2128 | if (nbytes < 0) |
| 2129 | { |
| 2130 | if (errno == EAGAIN) |
| 2131 | return -1; |
| 2132 | |
| 2133 | plog_err (peer->log, "%s [Error] bgp_read_packet error: %s", |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 2134 | peer->host, safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2135 | BGP_EVENT_ADD (peer, TCP_fatal_error); |
| 2136 | return -1; |
| 2137 | } |
| 2138 | |
| 2139 | /* When read byte is zero : clear bgp peer and return */ |
| 2140 | if (nbytes == 0) |
| 2141 | { |
| 2142 | if (BGP_DEBUG (events, EVENTS)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 2143 | plog_debug (peer->log, "%s [Event] BGP connection closed fd %d", |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 2144 | peer->host, peer->fd); |
hasso | e0701b7 | 2004-05-20 09:19:34 +0000 | [diff] [blame] | 2145 | |
| 2146 | if (peer->status == Established) |
| 2147 | peer->last_reset = PEER_DOWN_CLOSE_SESSION; |
| 2148 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2149 | BGP_EVENT_ADD (peer, TCP_connection_closed); |
| 2150 | return -1; |
| 2151 | } |
| 2152 | |
| 2153 | /* We read partial packet. */ |
| 2154 | if (peer->ibuf->putp != peer->packet_size) |
| 2155 | return -1; |
| 2156 | |
| 2157 | return 0; |
| 2158 | } |
| 2159 | |
| 2160 | /* Marker check. */ |
| 2161 | int |
| 2162 | bgp_marker_all_one (struct stream *s, int length) |
| 2163 | { |
| 2164 | int i; |
| 2165 | |
| 2166 | for (i = 0; i < length; i++) |
| 2167 | if (s->data[i] != 0xff) |
| 2168 | return 0; |
| 2169 | |
| 2170 | return 1; |
| 2171 | } |
| 2172 | |
| 2173 | /* Starting point of packet process function. */ |
| 2174 | int |
| 2175 | bgp_read (struct thread *thread) |
| 2176 | { |
| 2177 | int ret; |
| 2178 | u_char type = 0; |
| 2179 | struct peer *peer; |
| 2180 | bgp_size_t size; |
| 2181 | char notify_data_length[2]; |
| 2182 | |
| 2183 | /* Yes first of all get peer pointer. */ |
| 2184 | peer = THREAD_ARG (thread); |
| 2185 | peer->t_read = NULL; |
| 2186 | |
| 2187 | /* For non-blocking IO check. */ |
| 2188 | if (peer->status == Connect) |
| 2189 | { |
| 2190 | bgp_connect_check (peer); |
| 2191 | goto done; |
| 2192 | } |
| 2193 | else |
| 2194 | { |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 2195 | if (peer->fd < 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2196 | { |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 2197 | zlog_err ("bgp_read peer's fd is negative value %d", peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2198 | return -1; |
| 2199 | } |
paul | eb82118 | 2004-05-01 08:44:08 +0000 | [diff] [blame] | 2200 | BGP_READ_ON (peer->t_read, bgp_read, peer->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
| 2203 | /* Read packet header to determine type of the packet */ |
| 2204 | if (peer->packet_size == 0) |
| 2205 | peer->packet_size = BGP_HEADER_SIZE; |
| 2206 | |
| 2207 | if (peer->ibuf->putp < BGP_HEADER_SIZE) |
| 2208 | { |
| 2209 | ret = bgp_read_packet (peer); |
| 2210 | |
| 2211 | /* Header read error or partial read packet. */ |
| 2212 | if (ret < 0) |
| 2213 | goto done; |
| 2214 | |
| 2215 | /* Get size and type. */ |
| 2216 | stream_forward (peer->ibuf, BGP_MARKER_SIZE); |
| 2217 | memcpy (notify_data_length, stream_pnt (peer->ibuf), 2); |
| 2218 | size = stream_getw (peer->ibuf); |
| 2219 | type = stream_getc (peer->ibuf); |
| 2220 | |
| 2221 | if (BGP_DEBUG (normal, NORMAL) && type != 2 && type != 0) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 2222 | zlog_debug ("%s rcv message type %d, length (excl. header) %d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2223 | peer->host, type, size - BGP_HEADER_SIZE); |
| 2224 | |
| 2225 | /* Marker check */ |
paul | f5ba387 | 2004-07-09 12:11:31 +0000 | [diff] [blame] | 2226 | if (((type == BGP_MSG_OPEN) || (type == BGP_MSG_KEEPALIVE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2227 | && ! bgp_marker_all_one (peer->ibuf, BGP_MARKER_SIZE)) |
| 2228 | { |
| 2229 | bgp_notify_send (peer, |
| 2230 | BGP_NOTIFY_HEADER_ERR, |
| 2231 | BGP_NOTIFY_HEADER_NOT_SYNC); |
| 2232 | goto done; |
| 2233 | } |
| 2234 | |
| 2235 | /* BGP type check. */ |
| 2236 | if (type != BGP_MSG_OPEN && type != BGP_MSG_UPDATE |
| 2237 | && type != BGP_MSG_NOTIFY && type != BGP_MSG_KEEPALIVE |
| 2238 | && type != BGP_MSG_ROUTE_REFRESH_NEW |
| 2239 | && type != BGP_MSG_ROUTE_REFRESH_OLD |
| 2240 | && type != BGP_MSG_CAPABILITY) |
| 2241 | { |
| 2242 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 2243 | plog_debug (peer->log, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2244 | "%s unknown message type 0x%02x", |
| 2245 | peer->host, type); |
| 2246 | bgp_notify_send_with_data (peer, |
| 2247 | BGP_NOTIFY_HEADER_ERR, |
| 2248 | BGP_NOTIFY_HEADER_BAD_MESTYPE, |
| 2249 | &type, 1); |
| 2250 | goto done; |
| 2251 | } |
| 2252 | /* Mimimum packet length check. */ |
| 2253 | if ((size < BGP_HEADER_SIZE) |
| 2254 | || (size > BGP_MAX_PACKET_SIZE) |
| 2255 | || (type == BGP_MSG_OPEN && size < BGP_MSG_OPEN_MIN_SIZE) |
| 2256 | || (type == BGP_MSG_UPDATE && size < BGP_MSG_UPDATE_MIN_SIZE) |
| 2257 | || (type == BGP_MSG_NOTIFY && size < BGP_MSG_NOTIFY_MIN_SIZE) |
| 2258 | || (type == BGP_MSG_KEEPALIVE && size != BGP_MSG_KEEPALIVE_MIN_SIZE) |
| 2259 | || (type == BGP_MSG_ROUTE_REFRESH_NEW && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE) |
| 2260 | || (type == BGP_MSG_ROUTE_REFRESH_OLD && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE) |
| 2261 | || (type == BGP_MSG_CAPABILITY && size < BGP_MSG_CAPABILITY_MIN_SIZE)) |
| 2262 | { |
| 2263 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 2264 | plog_debug (peer->log, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2265 | "%s bad message length - %d for %s", |
| 2266 | peer->host, size, |
| 2267 | type == 128 ? "ROUTE-REFRESH" : |
| 2268 | bgp_type_str[(int) type]); |
| 2269 | bgp_notify_send_with_data (peer, |
| 2270 | BGP_NOTIFY_HEADER_ERR, |
| 2271 | BGP_NOTIFY_HEADER_BAD_MESLEN, |
hasso | c9e52be | 2004-09-26 16:09:34 +0000 | [diff] [blame] | 2272 | (u_char *) notify_data_length, 2); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2273 | goto done; |
| 2274 | } |
| 2275 | |
| 2276 | /* Adjust size to message length. */ |
| 2277 | peer->packet_size = size; |
| 2278 | } |
| 2279 | |
| 2280 | ret = bgp_read_packet (peer); |
| 2281 | if (ret < 0) |
| 2282 | goto done; |
| 2283 | |
| 2284 | /* Get size and type again. */ |
| 2285 | size = stream_getw_from (peer->ibuf, BGP_MARKER_SIZE); |
| 2286 | type = stream_getc_from (peer->ibuf, BGP_MARKER_SIZE + 2); |
| 2287 | |
| 2288 | /* BGP packet dump function. */ |
| 2289 | bgp_dump_packet (peer, type, peer->ibuf); |
| 2290 | |
| 2291 | size = (peer->packet_size - BGP_HEADER_SIZE); |
| 2292 | |
| 2293 | /* Read rest of the packet and call each sort of packet routine */ |
| 2294 | switch (type) |
| 2295 | { |
| 2296 | case BGP_MSG_OPEN: |
| 2297 | peer->open_in++; |
paul | f5ba387 | 2004-07-09 12:11:31 +0000 | [diff] [blame] | 2298 | bgp_open_receive (peer, size); /* XXX return value ignored! */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2299 | break; |
| 2300 | case BGP_MSG_UPDATE: |
| 2301 | peer->readtime = time(NULL); /* Last read timer reset */ |
| 2302 | bgp_update_receive (peer, size); |
| 2303 | break; |
| 2304 | case BGP_MSG_NOTIFY: |
| 2305 | bgp_notify_receive (peer, size); |
| 2306 | break; |
| 2307 | case BGP_MSG_KEEPALIVE: |
| 2308 | peer->readtime = time(NULL); /* Last read timer reset */ |
| 2309 | bgp_keepalive_receive (peer, size); |
| 2310 | break; |
| 2311 | case BGP_MSG_ROUTE_REFRESH_NEW: |
| 2312 | case BGP_MSG_ROUTE_REFRESH_OLD: |
| 2313 | peer->refresh_in++; |
| 2314 | bgp_route_refresh_receive (peer, size); |
| 2315 | break; |
| 2316 | case BGP_MSG_CAPABILITY: |
| 2317 | peer->dynamic_cap_in++; |
| 2318 | bgp_capability_receive (peer, size); |
| 2319 | break; |
| 2320 | } |
| 2321 | |
| 2322 | /* Clear input buffer. */ |
| 2323 | peer->packet_size = 0; |
| 2324 | if (peer->ibuf) |
| 2325 | stream_reset (peer->ibuf); |
| 2326 | |
| 2327 | done: |
| 2328 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
| 2329 | { |
| 2330 | if (BGP_DEBUG (events, EVENTS)) |
ajs | 6b51474 | 2004-12-08 21:03:23 +0000 | [diff] [blame] | 2331 | zlog_debug ("%s [Event] Accepting BGP peer delete", peer->host); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2332 | peer_delete (peer); |
| 2333 | } |
| 2334 | return 0; |
| 2335 | } |