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