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