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