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