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