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