blob: 4ef470d2b7a037aa1791720e9853f24905135ee5 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* BGP packet management routine.
2 Copyright (C) 1999 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-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äsef757702015-04-29 09:43:04 +030031#include "sockopt.h"
paul718e3742002-12-13 20:15:29 +000032#include "linklist.h"
33#include "plist.h"
Donald Sharp04907292016-01-07 10:03:01 -050034#include "filter.h"
paul718e3742002-12-13 20:15:29 +000035
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 Berger298cc2f2016-01-12 13:42:02 -050050#include "bgpd/bgp_encap.h"
paul718e3742002-12-13 20:15:29 +000051#include "bgpd/bgp_advertise.h"
hasso93406d82005-02-02 14:40:33 +000052#include "bgpd/bgp_vty.h"
paul718e3742002-12-13 20:15:29 +000053
54int stream_put_prefix (struct stream *, struct prefix *);
David Lamparter6b0655a2014-06-04 06:53:35 +020055
paul718e3742002-12-13 20:15:29 +000056/* Set up BGP packet marker and packet type. */
57static int
58bgp_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. */
paul9985f832005-02-09 15:51:56 +000073 return stream_get_endp (s);
paul718e3742002-12-13 20:15:29 +000074}
75
76/* Set BGP packet header size entry. If size is zero then use current
77 stream size. */
78static int
79bgp_packet_set_size (struct stream *s)
80{
81 int cp;
82
83 /* Preserve current pointer. */
paul9985f832005-02-09 15:51:56 +000084 cp = stream_get_endp (s);
85 stream_putw_at (s, BGP_MARKER_SIZE, cp);
paul718e3742002-12-13 20:15:29 +000086
87 return cp;
88}
89
90/* Add new packet to the peer. */
paul94f2b392005-06-28 12:44:16 +000091static void
paul718e3742002-12-13 20:15:29 +000092bgp_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. */
paul94f2b392005-06-28 12:44:16 +000099static void
paul718e3742002-12-13 20:15:29 +0000100bgp_packet_delete (struct peer *peer)
101{
102 stream_free (stream_fifo_pop (peer->obuf));
103}
104
paul718e3742002-12-13 20:15:29 +0000105/* Check file descriptor whether connect is established. */
Paul Jakma743dd422016-09-30 13:55:47 +0100106static void
107bgp_connect_check (struct peer *peer)
paul718e3742002-12-13 20:15:29 +0000108{
109 int status;
paul5228ad22004-06-04 17:58:18 +0000110 socklen_t slen;
paul718e3742002-12-13 20:15:29 +0000111 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);
pauleb821182004-05-01 08:44:08 +0000119 ret = getsockopt(peer->fd, SOL_SOCKET, SO_ERROR, (void *) &status, &slen);
paul718e3742002-12-13 20:15:29 +0000120
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 Jakma743dd422016-09-30 13:55:47 +0100126 return;
paul718e3742002-12-13 20:15:29 +0000127 }
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))
ajs6b514742004-12-08 21:03:23 +0000137 plog_debug (peer->log, "%s [Event] Connect failed (%s)",
ajs6099b3b2004-11-20 02:06:59 +0000138 peer->host, safe_strerror (errno));
Paul Jakma743dd422016-09-30 13:55:47 +0100139 BGP_EVENT_ADD (peer, TCP_connection_open_failed);
paul718e3742002-12-13 20:15:29 +0000140 }
141}
142
143/* Make BGP update packet. */
paul94f2b392005-06-28 12:44:16 +0000144static struct stream *
paul718e3742002-12-13 20:15:29 +0000145bgp_update_packet (struct peer *peer, afi_t afi, safi_t safi)
146{
147 struct stream *s;
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000148 struct stream *snlri;
paul718e3742002-12-13 20:15:29 +0000149 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 Mohapatra8c71e482014-01-15 06:57:57 +0000155 unsigned long attrlen_pos = 0;
Daniel Walton5bcd7542015-05-19 17:58:10 -0700156 int space_remaining = 0;
157 int space_needed = 0;
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000158 size_t mpattrlen_pos = 0;
159 size_t mpattr_pos = 0;
paul718e3742002-12-13 20:15:29 +0000160
161 s = peer->work;
162 stream_reset (s);
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000163 snlri = peer->scratch;
164 stream_reset (snlri);
paul718e3742002-12-13 20:15:29 +0000165
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100166 adv = BGP_ADV_FIFO_HEAD (&peer->sync[afi][safi]->update);
paul718e3742002-12-13 20:15:29 +0000167
168 while (adv)
169 {
Paul Jakmaed3ebfa2006-10-15 23:50:16 +0000170 assert (adv->rn);
171 rn = adv->rn;
paul718e3742002-12-13 20:15:29 +0000172 adj = adv->adj;
173 if (adv->binfo)
174 binfo = adv->binfo;
paul718e3742002-12-13 20:15:29 +0000175
Daniel Walton5bcd7542015-05-19 17:58:10 -0700176 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
paul718e3742002-12-13 20:15:29 +0000180 /* When remaining space can't include NLRI and it's length. */
Daniel Walton5bcd7542015-05-19 17:58:10 -0700181 if (space_remaining < space_needed)
paul718e3742002-12-13 20:15:29 +0000182 break;
183
184 /* If packet is empty, set attribute. */
185 if (stream_empty (s))
186 {
Lou Berger050defe2016-01-12 13:41:59 -0500187 struct prefix_rd *prd = NULL;
188 u_char *tag = NULL;
Paul Jakmaed3ebfa2006-10-15 23:50:16 +0000189 struct peer *from = NULL;
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000190
Lou Berger050defe2016-01-12 13:41:59 -0500191 if (rn->prn)
192 prd = (struct prefix_rd *) &rn->prn->p;
Greg Troxeld3ddb222010-09-17 10:47:49 -0400193 if (binfo)
Lou Berger050defe2016-01-12 13:41:59 -0500194 {
195 from = binfo->peer;
196 if (binfo->extra)
197 tag = binfo->extra->tag;
198 }
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000199
200 /* 1: Write the BGP message header - 16 bytes marker, 2 bytes length,
201 * one byte message type.
202 */
paul718e3742002-12-13 20:15:29 +0000203 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000204
205 /* 2: withdrawn routes length */
paul718e3742002-12-13 20:15:29 +0000206 stream_putw (s, 0);
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000207
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,
paul5228ad22004-06-04 17:58:18 +0000220 adv->baa->attr,
Lou Berger298cc2f2016-01-12 13:42:02 -0500221 ((afi == AFI_IP && safi == SAFI_UNICAST) ?
222 &rn->p : NULL),
223 afi, safi,
Lou Berger050defe2016-01-12 13:41:59 -0500224 from, prd, tag);
Daniel Walton5bcd7542015-05-19 17:58:10 -0700225 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
paul718e3742002-12-13 20:15:29 +0000241 }
242
243 if (afi == AFI_IP && safi == SAFI_UNICAST)
244 stream_put_prefix (s, &rn->p);
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000245 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 }
paul718e3742002-12-13 20:15:29 +0000261 if (BGP_DEBUG (update, UPDATE_OUT))
Jorge Boncompte [DTI2]14542f32012-05-07 16:52:53 +0000262 {
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 }
paul718e3742002-12-13 20:15:29 +0000270
271 /* Synchnorize attribute. */
272 if (adj->attr)
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000273 bgp_attr_unintern (&adj->attr);
paul718e3742002-12-13 20:15:29 +0000274 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);
paul718e3742002-12-13 20:15:29 +0000280 }
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000281
paul718e3742002-12-13 20:15:29 +0000282 if (! stream_empty (s))
283 {
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000284 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);
paul718e3742002-12-13 20:15:29 +0000298 bgp_packet_add (peer, packet);
pauleb821182004-05-01 08:44:08 +0000299 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000300 stream_reset (s);
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000301 stream_reset (snlri);
paul718e3742002-12-13 20:15:29 +0000302 return packet;
303 }
304 return NULL;
hasso93406d82005-02-02 14:40:33 +0000305}
paul718e3742002-12-13 20:15:29 +0000306
paul94f2b392005-06-28 12:44:16 +0000307static struct stream *
hasso93406d82005-02-02 14:40:33 +0000308bgp_update_packet_eor (struct peer *peer, afi_t afi, safi_t safi)
309{
310 struct stream *s;
hasso93406d82005-02-02 14:40:33 +0000311
Paul Jakma750e8142008-07-22 21:11:48 +0000312 if (DISABLE_BGP_ANNOUNCE)
313 return NULL;
hasso93406d82005-02-02 14:40:33 +0000314
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 Sharpa752c3b2015-08-18 08:48:53 -0400343 bgp_packet_add (peer, s);
344 return s;
paul718e3742002-12-13 20:15:29 +0000345}
346
347/* Make BGP withdraw packet. */
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000348/* 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*/
paul94f2b392005-06-28 12:44:16 +0000357static struct stream *
paul718e3742002-12-13 20:15:29 +0000358bgp_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;
paul718e3742002-12-13 20:15:29 +0000365 bgp_size_t unfeasible_len;
366 bgp_size_t total_attr_len;
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000367 size_t mp_start = 0;
368 size_t attrlen_pos = 0;
369 size_t mplen_pos = 0;
370 u_char first_time = 1;
Daniel Walton5bcd7542015-05-19 17:58:10 -0700371 int space_remaining = 0;
372 int space_needed = 0;
paul718e3742002-12-13 20:15:29 +0000373
374 s = peer->work;
375 stream_reset (s);
376
Paul Jakma7aa9dce2014-09-19 14:42:23 +0100377 while ((adv = BGP_ADV_FIFO_HEAD (&peer->sync[afi][safi]->withdraw)) != NULL)
paul718e3742002-12-13 20:15:29 +0000378 {
Paul Jakmaed3ebfa2006-10-15 23:50:16 +0000379 assert (adv->rn);
paul718e3742002-12-13 20:15:29 +0000380 adj = adv->adj;
381 rn = adv->rn;
paul718e3742002-12-13 20:15:29 +0000382
Daniel Walton5bcd7542015-05-19 17:58:10 -0700383 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)
paul718e3742002-12-13 20:15:29 +0000389 break;
390
391 if (stream_empty (s))
392 {
393 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000394 stream_putw (s, 0); /* unfeasible routes length */
paul718e3742002-12-13 20:15:29 +0000395 }
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000396 else
397 first_time = 0;
paul718e3742002-12-13 20:15:29 +0000398
399 if (afi == AFI_IP && safi == SAFI_UNICAST)
400 stream_put_prefix (s, &rn->p);
401 else
402 {
Paul Jakmaa3b6ea52006-05-04 07:52:12 +0000403 struct prefix_rd *prd = NULL;
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000404
Paul Jakmaa3b6ea52006-05-04 07:52:12 +0000405 if (rn->prn)
406 prd = (struct prefix_rd *) &rn->prn->p;
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000407
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);
paul718e3742002-12-13 20:15:29 +0000419 }
420
421 if (BGP_DEBUG (update, UPDATE_OUT))
Jorge Boncompte [DTI2]14542f32012-05-07 16:52:53 +0000422 {
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 }
paul718e3742002-12-13 20:15:29 +0000430
431 peer->scount[afi][safi]--;
432
433 bgp_adj_out_remove (rn, adj, peer, afi, safi);
434 bgp_unlock_node (rn);
paul718e3742002-12-13 20:15:29 +0000435 }
436
437 if (! stream_empty (s))
438 {
439 if (afi == AFI_IP && safi == SAFI_UNICAST)
440 {
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000441 unfeasible_len
paul9985f832005-02-09 15:51:56 +0000442 = stream_get_endp (s) - BGP_HEADER_SIZE - BGP_UNFEASIBLE_LEN;
paul718e3742002-12-13 20:15:29 +0000443 stream_putw_at (s, BGP_HEADER_SIZE, unfeasible_len);
444 stream_putw (s, 0);
445 }
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000446 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 }
paul718e3742002-12-13 20:15:29 +0000455 bgp_packet_set_size (s);
paule83e2082005-05-19 02:12:25 +0000456 packet = stream_dup (s);
paul718e3742002-12-13 20:15:29 +0000457 bgp_packet_add (peer, packet);
458 stream_reset (s);
459 return packet;
460 }
461
462 return NULL;
463}
464
465void
466bgp_default_update_send (struct peer *peer, struct attr *attr,
467 afi_t afi, safi_t safi, struct peer *from)
468{
469 struct stream *s;
paul718e3742002-12-13 20:15:29 +0000470 struct prefix p;
471 unsigned long pos;
472 bgp_size_t total_attr_len;
paul718e3742002-12-13 20:15:29 +0000473
Paul Jakma750e8142008-07-22 21:11:48 +0000474 if (DISABLE_BGP_ANNOUNCE)
475 return;
paul718e3742002-12-13 20:15:29 +0000476
477 if (afi == AFI_IP)
478 str2prefix ("0.0.0.0/0", &p);
paul718e3742002-12-13 20:15:29 +0000479 else
480 str2prefix ("::/0", &p);
paul718e3742002-12-13 20:15:29 +0000481
482 /* Logging the attribute. */
483 if (BGP_DEBUG (update, UPDATE_OUT))
484 {
Jorge Boncompte [DTI2]14542f32012-05-07 16:52:53 +0000485 char attrstr[BUFSIZ];
486 char buf[INET6_BUFSIZ];
487 attrstr[0] = '\0';
488
paul718e3742002-12-13 20:15:29 +0000489 bgp_dump_attr (peer, attr, attrstr, BUFSIZ);
ajs6b514742004-12-08 21:03:23 +0000490 zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d %s",
Jorge Boncompte [DTI2]14542f32012-05-07 16:52:53 +0000491 peer->host, inet_ntop(p.family, &(p.u.prefix), buf, INET6_BUFSIZ),
paul718e3742002-12-13 20:15:29 +0000492 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. */
paul9985f832005-02-09 15:51:56 +0000504 pos = stream_get_endp (s);
paul718e3742002-12-13 20:15:29 +0000505 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
paul718e3742002-12-13 20:15:29 +0000518 /* Dump packet if debug option is set. */
519#ifdef DEBUG
jardin2d74db52005-10-01 00:07:50 +0000520 /* bgp_packet_dump (packet); */
paul718e3742002-12-13 20:15:29 +0000521#endif /* DEBUG */
522
523 /* Add packet to the peer. */
Donald Sharpa752c3b2015-08-18 08:48:53 -0400524 bgp_packet_add (peer, s);
paul718e3742002-12-13 20:15:29 +0000525
pauleb821182004-05-01 08:44:08 +0000526 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000527}
528
529void
530bgp_default_withdraw_send (struct peer *peer, afi_t afi, safi_t safi)
531{
532 struct stream *s;
paul718e3742002-12-13 20:15:29 +0000533 struct prefix p;
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000534 unsigned long attrlen_pos = 0;
paul718e3742002-12-13 20:15:29 +0000535 unsigned long cp;
536 bgp_size_t unfeasible_len;
537 bgp_size_t total_attr_len;
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000538 size_t mp_start = 0;
539 size_t mplen_pos = 0;
paul718e3742002-12-13 20:15:29 +0000540
Paul Jakma750e8142008-07-22 21:11:48 +0000541 if (DISABLE_BGP_ANNOUNCE)
542 return;
paul718e3742002-12-13 20:15:29 +0000543
544 if (afi == AFI_IP)
545 str2prefix ("0.0.0.0/0", &p);
paul718e3742002-12-13 20:15:29 +0000546 else
547 str2prefix ("::/0", &p);
paul718e3742002-12-13 20:15:29 +0000548
549 total_attr_len = 0;
paul718e3742002-12-13 20:15:29 +0000550
551 if (BGP_DEBUG (update, UPDATE_OUT))
Jorge Boncompte [DTI2]14542f32012-05-07 16:52:53 +0000552 {
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 }
paul718e3742002-12-13 20:15:29 +0000559
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. */;
paul9985f832005-02-09 15:51:56 +0000566 cp = stream_get_endp (s);
paul718e3742002-12-13 20:15:29 +0000567 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
paul9985f832005-02-09 15:51:56 +0000574 unfeasible_len = stream_get_endp (s) - cp - 2;
paul718e3742002-12-13 20:15:29 +0000575
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 Mohapatra8c71e482014-01-15 06:57:57 +0000584 attrlen_pos = stream_get_endp (s);
paul718e3742002-12-13 20:15:29 +0000585 stream_putw (s, 0);
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000586 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);
paul718e3742002-12-13 20:15:29 +0000592
593 /* Set total path attribute length. */
Pradosh Mohapatra8c71e482014-01-15 06:57:57 +0000594 total_attr_len = stream_get_endp(s) - mp_start;
595 stream_putw_at (s, attrlen_pos, total_attr_len);
paul718e3742002-12-13 20:15:29 +0000596 }
597
598 bgp_packet_set_size (s);
599
paul718e3742002-12-13 20:15:29 +0000600 /* Add packet to the peer. */
Donald Sharpa752c3b2015-08-18 08:48:53 -0400601 bgp_packet_add (peer, s);
paul718e3742002-12-13 20:15:29 +0000602
pauleb821182004-05-01 08:44:08 +0000603 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000604}
605
606/* Get next packet to be written. */
paul94f2b392005-06-28 12:44:16 +0000607static struct stream *
paul718e3742002-12-13 20:15:29 +0000608bgp_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 Jakma7aa9dce2014-09-19 14:42:23 +0100622 adv = BGP_ADV_FIFO_HEAD (&peer->sync[afi][safi]->withdraw);
paul718e3742002-12-13 20:15:29 +0000623 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 Jakma7aa9dce2014-09-19 14:42:23 +0100634 adv = BGP_ADV_FIFO_HEAD (&peer->sync[afi][safi]->update);
paul718e3742002-12-13 20:15:29 +0000635 if (adv)
636 {
637 if (adv->binfo && adv->binfo->uptime < peer->synctime)
hasso93406d82005-02-02 14:40:33 +0000638 {
639 if (CHECK_FLAG (adv->binfo->peer->cap, PEER_CAP_RESTART_RCV)
640 && CHECK_FLAG (adv->binfo->peer->cap, PEER_CAP_RESTART_ADV)
Vipin Kumardd49eb12014-09-30 14:36:38 -0700641 && ! (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))
hasso93406d82005-02-02 14:40:33 +0000645 && ! 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 }
paul718e3742002-12-13 20:15:29 +0000655
656 if (s)
657 return s;
658 }
hasso93406d82005-02-02 14:40:33 +0000659
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 }
paul718e3742002-12-13 20:15:29 +0000670 }
671
672 return NULL;
673}
674
675/* Is there partially written packet or updates we can send right
676 now. */
paul94f2b392005-06-28 12:44:16 +0000677static int
paul718e3742002-12-13 20:15:29 +0000678bgp_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 Jakma7aa9dce2014-09-19 14:42:23 +0100694 if ((adv = BGP_ADV_FIFO_HEAD (&peer->sync[afi][safi]->update)) != NULL)
paul718e3742002-12-13 20:15:29 +0000695 if (adv->binfo->uptime < peer->synctime)
696 return 1;
697
698 return 0;
699}
700
701/* Write packet to the peer. */
702int
703bgp_write (struct thread *thread)
704{
705 struct peer *peer;
706 u_char type;
707 struct stream *s;
708 int num;
paulfd79ac92004-10-13 05:06:08 +0000709 unsigned int count = 0;
paul718e3742002-12-13 20:15:29 +0000710
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 Jakma743dd422016-09-30 13:55:47 +0100718 bgp_connect_check (peer);
paul718e3742002-12-13 20:15:29 +0000719 return 0;
720 }
721
Stephen Hemmingereac57022010-08-05 10:26:25 -0700722 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
paul718e3742002-12-13 20:15:29 +0000730 {
731 int writenum;
paul718e3742002-12-13 20:15:29 +0000732
733 /* Number of bytes to be sent. */
734 writenum = stream_get_endp (s) - stream_get_getp (s);
735
736 /* Call write() system call. */
pauleb821182004-05-01 08:44:08 +0000737 num = write (peer->fd, STREAM_PNT (s), writenum);
Stephen Hemminger35398582010-08-05 10:26:23 -0700738 if (num < 0)
paul718e3742002-12-13 20:15:29 +0000739 {
Stephen Hemmingereac57022010-08-05 10:26:25 -0700740 /* write failed either retry needed or error */
741 if (ERRNO_IO_RETRY(errno))
742 break;
743
744 BGP_EVENT_ADD (peer, TCP_fatal_error);
paul718e3742002-12-13 20:15:29 +0000745 return 0;
746 }
Stephen Hemminger35398582010-08-05 10:26:23 -0700747
paul718e3742002-12-13 20:15:29 +0000748 if (num != writenum)
749 {
Stephen Hemminger35398582010-08-05 10:26:23 -0700750 /* Partial write */
paul9985f832005-02-09 15:51:56 +0000751 stream_forward_getp (s, num);
Stephen Hemmingereac57022010-08-05 10:26:25 -0700752 break;
paul718e3742002-12-13 20:15:29 +0000753 }
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++;
paul718e3742002-12-13 20:15:29 +0000769
Paul Jakmaca058a32006-09-14 02:58:49 +0000770 /* Flush any existing events */
Paul Jakmab1b15792016-09-21 18:50:23 +0100771 BGP_EVENT_ADD (peer, BGP_Stop_with_error);
Stephen Hemminger3a69f742013-01-11 18:27:23 +0000772 goto done;
773
paul718e3742002-12-13 20:15:29 +0000774 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);
paul718e3742002-12-13 20:15:29 +0000788 }
Stephen Hemmingereac57022010-08-05 10:26:25 -0700789 while (++count < BGP_WRITE_PACKET_MAX &&
790 (s = bgp_write_packet (peer)) != NULL);
paul718e3742002-12-13 20:15:29 +0000791
792 if (bgp_write_proceed (peer))
pauleb821182004-05-01 08:44:08 +0000793 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
Stephen Hemminger3a69f742013-01-11 18:27:23 +0000794
795 done:
796 sockopt_cork (peer->fd, 0);
paul718e3742002-12-13 20:15:29 +0000797 return 0;
798}
799
800/* This is only for sending NOTIFICATION message to neighbor. */
paul94f2b392005-06-28 12:44:16 +0000801static int
paul718e3742002-12-13 20:15:29 +0000802bgp_write_notify (struct peer *peer)
803{
Stephen Hemminger35398582010-08-05 10:26:23 -0700804 int ret, val;
paul718e3742002-12-13 20:15:29 +0000805 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 Rosenboim86998bc2012-12-14 19:12:17 +0000814 /* Stop collecting data within the socket */
815 sockopt_cork (peer->fd, 0);
816
David Lamparter8ff202e2013-07-31 14:39:41 +0200817 /* 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 Rosenboim86998bc2012-12-14 19:12:17 +0000819 ret = write (peer->fd, STREAM_DATA (s), stream_get_endp (s));
David Lamparter8ff202e2013-07-31 14:39:41 +0200820
821 /* only connection reset/close gets counted as TCP_fatal_error, failure
822 * to write the entire NOTIFY doesn't get different FSM treatment */
paul718e3742002-12-13 20:15:29 +0000823 if (ret <= 0)
824 {
Paul Jakmadcdf3992006-10-15 23:39:59 +0000825 BGP_EVENT_ADD (peer, TCP_fatal_error);
paul718e3742002-12-13 20:15:29 +0000826 return 0;
827 }
828
Leonid Rosenboim86998bc2012-12-14 19:12:17 +0000829 /* 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
paul718e3742002-12-13 20:15:29 +0000834 /* 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 Jakmab1b15792016-09-21 18:50:23 +0100843 BGP_EVENT_ADD (peer, BGP_Stop_with_error);
paul718e3742002-12-13 20:15:29 +0000844
845 return 0;
846}
847
848/* Make keepalive packet and send it to the peer. */
849void
850bgp_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))
ajs6b514742004-12-08 21:03:23 +0000867 zlog_debug ("%s sending KEEPALIVE", peer->host);
paul718e3742002-12-13 20:15:29 +0000868 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +0000869 zlog_debug ("%s send message type %d, length (incl. header) %d",
paul718e3742002-12-13 20:15:29 +0000870 peer->host, BGP_MSG_KEEPALIVE, length);
871
872 /* Add packet to the peer. */
873 bgp_packet_add (peer, s);
874
pauleb821182004-05-01 08:44:08 +0000875 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000876}
877
878/* Make open packet and send it to the peer. */
879void
880bgp_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 Jakma0b2aa3a2007-10-14 22:32:21 +0000905 stream_putw (s, (local_as <= BGP_AS_MAX) ? (u_int16_t) local_as
906 : BGP_AS_TRANS);
paul718e3742002-12-13 20:15:29 +0000907 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 Ovsienkoaea339f2009-04-30 17:16:22 +0400917 zlog_debug ("%s sending OPEN, version %d, my as %u, holdtime %d, id %s",
paul718e3742002-12-13 20:15:29 +0000918 peer->host, BGP_VERSION_4, local_as,
919 send_holdtime, inet_ntoa (peer->local_id));
920
921 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +0000922 zlog_debug ("%s send message type %d, length (incl. header) %d",
paul718e3742002-12-13 20:15:29 +0000923 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
pauleb821182004-05-01 08:44:08 +0000931 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +0000932}
933
934/* Send BGP notify packet with data potion. */
935void
936bgp_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 Walton363c9032015-10-21 06:42:54 -0700992
paul718e3742002-12-13 20:15:29 +0000993 if (bgp_notify.data)
Daniel Walton363c9032015-10-21 06:42:54 -0700994 {
995 XFREE (MTYPE_TMP, bgp_notify.data);
996 bgp_notify.data = NULL;
997 bgp_notify.length = 0;
998 }
paul718e3742002-12-13 20:15:29 +0000999 }
1000
1001 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001002 zlog_debug ("%s send message type %d, length (incl. header) %d",
paul718e3742002-12-13 20:15:29 +00001003 peer->host, BGP_MSG_NOTIFY, length);
1004
hassoe0701b72004-05-20 09:19:34 +00001005 /* peer reset cause */
1006 if (sub_code != BGP_NOTIFY_CEASE_CONFIG_CHANGE)
1007 {
1008 if (sub_code == BGP_NOTIFY_CEASE_ADMIN_RESET)
heasley1212dc12011-09-12 13:27:52 +04001009 {
1010 peer->last_reset = PEER_DOWN_USER_RESET;
1011 zlog_info ("Notification sent to neighbor %s: User reset", peer->host);
1012 }
hassoe0701b72004-05-20 09:19:34 +00001013 else if (sub_code == BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN)
heasley1212dc12011-09-12 13:27:52 +04001014 {
1015 peer->last_reset = PEER_DOWN_USER_SHUTDOWN;
1016 zlog_info ("Notification sent to neighbor %s: shutdown", peer->host);
1017 }
hassoe0701b72004-05-20 09:19:34 +00001018 else
heasley1212dc12011-09-12 13:27:52 +04001019 {
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 }
hassoe0701b72004-05-20 09:19:34 +00001024 }
heasley1212dc12011-09-12 13:27:52 +04001025 else
1026 zlog_info ("Notification sent to neighbor %s: configuration change",
1027 peer->host);
hassoe0701b72004-05-20 09:19:34 +00001028
Denis Ovsienko7ccf5e52011-09-10 16:53:30 +04001029 /* Call immediately. */
paul718e3742002-12-13 20:15:29 +00001030 BGP_WRITE_OFF (peer->t_write);
1031
1032 bgp_write_notify (peer);
1033}
1034
1035/* Send BGP notify packet. */
1036void
1037bgp_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
paul718e3742002-12-13 20:15:29 +00001042/* Send route refresh message to the peer. */
1043void
1044bgp_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;
paul718e3742002-12-13 20:15:29 +00001048 int length;
1049 struct bgp_filter *filter;
1050 int orf_refresh = 0;
1051
Paul Jakma750e8142008-07-22 21:11:48 +00001052 if (DISABLE_BGP_ANNOUNCE)
1053 return;
paul718e3742002-12-13 20:15:29 +00001054
1055 filter = &peer->filter[afi][safi];
1056
1057 /* Adjust safi code. */
1058 if (safi == SAFI_MPLS_VPN)
Denis Ovsienko42e6d742011-07-14 12:36:19 +04001059 safi = SAFI_MPLS_LABELED_VPN;
paul718e3742002-12-13 20:15:29 +00001060
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);
paul9985f832005-02-09 15:51:56 +00001084 orfp = stream_get_endp (s);
paul718e3742002-12-13 20:15:29 +00001085 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))
ajs6b514742004-12-08 21:03:23 +00001092 zlog_debug ("%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
paul718e3742002-12-13 20:15:29 +00001093 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))
ajs6b514742004-12-08 21:03:23 +00001104 zlog_debug ("%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
paul718e3742002-12-13 20:15:29 +00001105 peer->host, orf_type,
1106 (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"),
1107 afi, safi);
1108 }
1109
1110 /* Total ORF Entry Len. */
paul9985f832005-02-09 15:51:56 +00001111 orf_len = stream_get_endp (s) - orfp - 2;
paul718e3742002-12-13 20:15:29 +00001112 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)
ajs6b514742004-12-08 21:03:23 +00001121 zlog_debug ("%s sending REFRESH_REQ for afi/safi: %d/%d",
paul718e3742002-12-13 20:15:29 +00001122 peer->host, afi, safi);
ajs6b514742004-12-08 21:03:23 +00001123 zlog_debug ("%s send message type %d, length (incl. header) %d",
paul718e3742002-12-13 20:15:29 +00001124 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
paul718e3742002-12-13 20:15:29 +00001128 /* Add packet to the peer. */
Donald Sharpa752c3b2015-08-18 08:48:53 -04001129 bgp_packet_add (peer, s);
paul718e3742002-12-13 20:15:29 +00001130
pauleb821182004-05-01 08:44:08 +00001131 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +00001132}
1133
1134/* Send capability message to the peer. */
1135void
1136bgp_capability_send (struct peer *peer, afi_t afi, safi_t safi,
1137 int capability_code, int action)
1138{
1139 struct stream *s;
paul718e3742002-12-13 20:15:29 +00001140 int length;
1141
1142 /* Adjust safi code. */
1143 if (safi == SAFI_MPLS_VPN)
Denis Ovsienko42e6d742011-07-14 12:36:19 +04001144 safi = SAFI_MPLS_LABELED_VPN;
paul718e3742002-12-13 20:15:29 +00001145
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))
ajs6b514742004-12-08 21:03:23 +00001162 zlog_debug ("%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
paul718e3742002-12-13 20:15:29 +00001163 peer->host, action == CAPABILITY_ACTION_SET ?
1164 "Advertising" : "Removing", afi, safi);
1165 }
1166
paul718e3742002-12-13 20:15:29 +00001167 /* Set packet size. */
1168 length = bgp_packet_set_size (s);
1169
paul718e3742002-12-13 20:15:29 +00001170
1171 /* Add packet to the peer. */
Donald Sharpa752c3b2015-08-18 08:48:53 -04001172 bgp_packet_add (peer, s);
paul718e3742002-12-13 20:15:29 +00001173
1174 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001175 zlog_debug ("%s send message type %d, length (incl. header) %d",
paul718e3742002-12-13 20:15:29 +00001176 peer->host, BGP_MSG_CAPABILITY, length);
1177
pauleb821182004-05-01 08:44:08 +00001178 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
paul718e3742002-12-13 20:15:29 +00001179}
David Lamparter6b0655a2014-06-04 06:53:35 +02001180
paul718e3742002-12-13 20:15:29 +00001181/* RFC1771 6.8 Connection collision detection. */
paul94f2b392005-06-28 12:44:16 +00001182static int
pauleb821182004-05-01 08:44:08 +00001183bgp_collision_detect (struct peer *new, struct in_addr remote_id)
paul718e3742002-12-13 20:15:29 +00001184{
pauleb821182004-05-01 08:44:08 +00001185 struct peer *peer;
paul1eb8ef22005-04-07 07:30:20 +00001186 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001187 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
paul1eb8ef22005-04-07 07:30:20 +00001202 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
paul718e3742002-12-13 20:15:29 +00001203 {
Paul Jakmaac278ea2016-04-26 11:46:34 +01001204 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
Paul Jakma1ebafb62016-04-27 09:55:21 +01001226 /* Note: Quagga historically orders explicitly only on the processing
1227 * of the Opens, treating 'new' as the passive, inbound and connection
1228 * and 'peer' as the active outbound connection.
Paul Jakmaac278ea2016-04-26 11:46:34 +01001229 */
1230
1231 /* The local_id is always set, so we can match the given remote-ID
1232 * from the OPEN against both OpenConfirm and OpenSent peers.
1233 */
1234 if (peer->status == OpenConfirm || peer->status == OpenSent)
pauleb821182004-05-01 08:44:08 +00001235 {
Paul Jakma1ebafb62016-04-27 09:55:21 +01001236 struct peer *out = peer;
1237 struct peer *in = new;
1238 int ret_close_out = 1, ret_close_in = -1;
1239
1240 if (!CHECK_FLAG (new->sflags, PEER_STATUS_ACCEPT_PEER))
1241 {
1242 out = new;
1243 ret_close_out = -1;
1244 in = peer;
1245 ret_close_in = 1;
1246 }
1247
paul718e3742002-12-13 20:15:29 +00001248 /* 1. The BGP Identifier of the local system is compared to
1249 the BGP Identifier of the remote system (as specified in
1250 the OPEN message). */
1251
1252 if (ntohl (peer->local_id.s_addr) < ntohl (remote_id.s_addr))
1253 {
1254 /* 2. If the value of the local BGP Identifier is less
1255 than the remote one, the local system closes BGP
1256 connection that already exists (the one that is
1257 already in the OpenConfirm state), and accepts BGP
1258 connection initiated by the remote system. */
1259
Paul Jakma1ebafb62016-04-27 09:55:21 +01001260 if (out->fd >= 0)
1261 bgp_notify_send (out, BGP_NOTIFY_CEASE,
Paul Jakmaac278ea2016-04-26 11:46:34 +01001262 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
Paul Jakma1ebafb62016-04-27 09:55:21 +01001263 return ret_close_out;
paul718e3742002-12-13 20:15:29 +00001264 }
1265 else
1266 {
1267 /* 3. Otherwise, the local system closes newly created
1268 BGP connection (the one associated with the newly
1269 received OPEN message), and continues to use the
1270 existing one (the one that is already in the
1271 OpenConfirm state). */
1272
Paul Jakma1ebafb62016-04-27 09:55:21 +01001273 if (in->fd >= 0)
1274 bgp_notify_send (in, BGP_NOTIFY_CEASE,
paulf5ba3872004-07-09 12:11:31 +00001275 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
Paul Jakma1ebafb62016-04-27 09:55:21 +01001276 return ret_close_in;
paul718e3742002-12-13 20:15:29 +00001277 }
pauleb821182004-05-01 08:44:08 +00001278 }
1279 }
paul718e3742002-12-13 20:15:29 +00001280 return 0;
1281}
1282
paul94f2b392005-06-28 12:44:16 +00001283static int
paul718e3742002-12-13 20:15:29 +00001284bgp_open_receive (struct peer *peer, bgp_size_t size)
1285{
1286 int ret;
1287 u_char version;
1288 u_char optlen;
1289 u_int16_t holdtime;
1290 u_int16_t send_holdtime;
1291 as_t remote_as;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001292 as_t as4 = 0;
paul718e3742002-12-13 20:15:29 +00001293 struct peer *realpeer;
1294 struct in_addr remote_id;
Avneesh Sachdev3b381c32012-02-19 10:19:52 -08001295 int mp_capability;
paul5228ad22004-06-04 17:58:18 +00001296 u_int8_t notify_data_remote_as[2];
1297 u_int8_t notify_data_remote_id[4];
Daniel Waltonc6969872015-05-19 18:03:43 -07001298 u_int16_t *holdtime_ptr;
paul718e3742002-12-13 20:15:29 +00001299
1300 realpeer = NULL;
1301
1302 /* Parse open packet. */
1303 version = stream_getc (peer->ibuf);
1304 memcpy (notify_data_remote_as, stream_pnt (peer->ibuf), 2);
1305 remote_as = stream_getw (peer->ibuf);
Daniel Waltonc6969872015-05-19 18:03:43 -07001306 holdtime_ptr = (u_int16_t *)stream_pnt (peer->ibuf);
paul718e3742002-12-13 20:15:29 +00001307 holdtime = stream_getw (peer->ibuf);
1308 memcpy (notify_data_remote_id, stream_pnt (peer->ibuf), 4);
1309 remote_id.s_addr = stream_get_ipv4 (peer->ibuf);
1310
1311 /* Receive OPEN message log */
1312 if (BGP_DEBUG (normal, NORMAL))
Denis Ovsienkoaea339f2009-04-30 17:16:22 +04001313 zlog_debug ("%s rcv OPEN, version %d, remote-as (in open) %u,"
Paul Jakmaac278ea2016-04-26 11:46:34 +01001314 " holdtime %d, id %s, %sbound connection",
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001315 peer->host, version, remote_as, holdtime,
Paul Jakmaac278ea2016-04-26 11:46:34 +01001316 inet_ntoa (remote_id),
1317 CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)
1318 ? "in" : "out");
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001319
1320 /* BEGIN to read the capability here, but dont do it yet */
Avneesh Sachdev3b381c32012-02-19 10:19:52 -08001321 mp_capability = 0;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001322 optlen = stream_getc (peer->ibuf);
1323
1324 if (optlen != 0)
1325 {
1326 /* We need the as4 capability value *right now* because
1327 * if it is there, we have not got the remote_as yet, and without
1328 * that we do not know which peer is connecting to us now.
1329 */
1330 as4 = peek_for_as4_capability (peer, optlen);
1331 }
1332
1333 /* Just in case we have a silly peer who sends AS4 capability set to 0 */
1334 if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) && !as4)
1335 {
1336 zlog_err ("%s bad OPEN, got AS4 capability, but AS4 set to 0",
1337 peer->host);
1338 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
1339 BGP_NOTIFY_OPEN_BAD_PEER_AS);
1340 return -1;
1341 }
1342
1343 if (remote_as == BGP_AS_TRANS)
1344 {
1345 /* Take the AS4 from the capability. We must have received the
1346 * capability now! Otherwise we have a asn16 peer who uses
1347 * BGP_AS_TRANS, for some unknown reason.
1348 */
1349 if (as4 == BGP_AS_TRANS)
1350 {
1351 zlog_err ("%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
1352 peer->host);
1353 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
1354 BGP_NOTIFY_OPEN_BAD_PEER_AS);
1355 return -1;
1356 }
1357
1358 if (!as4 && BGP_DEBUG (as4, AS4))
1359 zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but no AS4."
1360 " Odd, but proceeding.", peer->host);
1361 else if (as4 < BGP_AS_MAX && BGP_DEBUG (as4, AS4))
Paul Jakma0df7c912008-07-21 21:02:49 +00001362 zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits "
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001363 "in 2-bytes, very odd peer.", peer->host, as4);
1364 if (as4)
1365 remote_as = as4;
1366 }
1367 else
1368 {
1369 /* We may have a partner with AS4 who has an asno < BGP_AS_MAX */
1370 /* If we have got the capability, peer->as4cap must match remote_as */
1371 if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV)
1372 && as4 != remote_as)
1373 {
1374 /* raise error, log this, close session */
1375 zlog_err ("%s bad OPEN, got AS4 capability, but remote_as %u"
1376 " mismatch with 16bit 'myasn' %u in open",
1377 peer->host, as4, remote_as);
1378 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
1379 BGP_NOTIFY_OPEN_BAD_PEER_AS);
1380 return -1;
1381 }
1382 }
1383
paul718e3742002-12-13 20:15:29 +00001384 /* Lookup peer from Open packet. */
1385 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
1386 {
1387 int as = 0;
1388
1389 realpeer = peer_lookup_with_open (&peer->su, remote_as, &remote_id, &as);
1390
1391 if (! realpeer)
1392 {
1393 /* Peer's source IP address is check in bgp_accept(), so this
1394 must be AS number mismatch or remote-id configuration
1395 mismatch. */
1396 if (as)
1397 {
1398 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001399 zlog_debug ("%s bad OPEN, wrong router identifier %s",
1400 peer->host, inet_ntoa (remote_id));
1401 bgp_notify_send_with_data (peer, BGP_NOTIFY_OPEN_ERR,
1402 BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
1403 notify_data_remote_id, 4);
paul718e3742002-12-13 20:15:29 +00001404 }
1405 else
1406 {
1407 if (BGP_DEBUG (normal, NORMAL))
Denis Ovsienkoaea339f2009-04-30 17:16:22 +04001408 zlog_debug ("%s bad OPEN, remote AS is %u, expected %u",
ajs6b514742004-12-08 21:03:23 +00001409 peer->host, remote_as, peer->as);
1410 bgp_notify_send_with_data (peer, BGP_NOTIFY_OPEN_ERR,
1411 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1412 notify_data_remote_as, 2);
paul718e3742002-12-13 20:15:29 +00001413 }
1414 return -1;
1415 }
1416 }
1417
1418 /* When collision is detected and this peer is closed. Retrun
1419 immidiately. */
1420 ret = bgp_collision_detect (peer, remote_id);
1421 if (ret < 0)
1422 return ret;
1423
Paul Jakmaac278ea2016-04-26 11:46:34 +01001424 /* Bit hacky */
pauleb821182004-05-01 08:44:08 +00001425 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
Paul Jakmaac278ea2016-04-26 11:46:34 +01001426 {
1427 /* Connection FSM state is intertwined with our peer configuration
1428 * (the RFC encourages this a bit). At _this_ point we have a
1429 * 'realpeer' which represents the configuration and any earlier FSM
1430 * (outbound, unless the remote side has opened two connections to
1431 * us), and a 'peer' which here represents an inbound connection that
1432 * has not yet been reconciled with a 'realpeer'.
1433 *
1434 * As 'peer' has just sent an OPEN that reconciliation must now
1435 * happen, as only the 'realpeer' can ever proceed to Established.
1436 *
1437 * bgp_collision_detect should have resolved any collisions with
1438 * realpeers that are in states OpenSent, OpenConfirm or Established,
1439 * and may have sent a notify on the 'realpeer' connection.
1440 * bgp_accept will have rejected any connections where the 'realpeer'
1441 * is in Idle or >Established (though, that status may have changed
1442 * since).
1443 *
1444 * Need to finish off any reconciliation here, and ensure that
1445 * 'realpeer' is left holding any needed state from the appropriate
1446 * connection (fd, buffers, etc.), and any state from the other
1447 * connection is cleaned up.
1448 */
1449
1450 /* Is realpeer in some globally-down state, that precludes any and all
1451 * connections (Idle, Clearing, Deleted, etc.)?
1452 */
1453 if (realpeer->status == Idle || realpeer->status > Established)
1454 {
1455 if (BGP_DEBUG (events, EVENTS))
1456 zlog_debug ("%s peer status is %s, closing the new connection",
1457 realpeer->host,
1458 LOOKUP (bgp_status_msg, realpeer->status));
1459 return -1;
1460 }
1461
1462 /* GR does things differently, and prefers any new connection attempts
1463 * over an Established one (why not just rely on KEEPALIVE and avoid
1464 * having to special case this?) */
1465 if (realpeer->status == Established
hasso93406d82005-02-02 14:40:33 +00001466 && CHECK_FLAG (realpeer->sflags, PEER_STATUS_NSF_MODE))
1467 {
1468 realpeer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
1469 SET_FLAG (realpeer->sflags, PEER_STATUS_NSF_WAIT);
1470 }
Paul Jakmaac278ea2016-04-26 11:46:34 +01001471 else if (ret == 0)
pauleb821182004-05-01 08:44:08 +00001472 {
Paul Jakmaac278ea2016-04-26 11:46:34 +01001473 /* If we're here, RFC collision-detect did not reconcile the
1474 * connections, and the 'realpeer' is still available. So
1475 * 'realpeer' must be 'Active' or 'Connect'.
Paul Jakma2b2fc562008-09-06 13:09:35 +01001476 *
1477 * According to the RFC we should just let this connection (of the
1478 * accepted 'peer') continue on to Established if the other
Paul Jakmaac278ea2016-04-26 11:46:34 +01001479 * onnection (the 'realpeer') is in a more larval state, and
1480 * reconcile them when OPEN is sent on the 'realpeer'.
Paul Jakma2b2fc562008-09-06 13:09:35 +01001481 *
Paul Jakmaac278ea2016-04-26 11:46:34 +01001482 * However, the accepted 'peer' must be reconciled with 'peer' at
1483 * this point, due to the implementation, if 'peer' is to be able
1484 * to proceed. So it should be allowed to go to Established, as
1485 * long as the 'realpeer' was in Active or Connect state - which
1486 * /should/ be the case if we're here.
Paul Jakma2b2fc562008-09-06 13:09:35 +01001487 *
Paul Jakmaac278ea2016-04-26 11:46:34 +01001488 * So we should only need to sanity check that that is the case
1489 * here, and allow the code to get on with transferring the 'peer'
1490 * connection state over.
Paul Jakma2b2fc562008-09-06 13:09:35 +01001491 */
Paul Jakmaac278ea2016-04-26 11:46:34 +01001492 if (realpeer->status != Active && realpeer->status != Connect)
1493 {
1494 if (BGP_DEBUG (events, EVENTS))
1495 zlog_warn ("%s real peer status should be Active or Connect,"
1496 " but is %s",
1497 realpeer->host,
1498 LOOKUP (bgp_status_msg, realpeer->status));
1499 bgp_notify_send (realpeer, BGP_NOTIFY_CEASE,
1500 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1501 }
pauleb821182004-05-01 08:44:08 +00001502 }
1503
1504 if (BGP_DEBUG (events, EVENTS))
Paul Jakmaac278ea2016-04-26 11:46:34 +01001505 zlog_debug ("%s:%u [Event] Transfer accept BGP peer to real (state %s)",
1506 peer->host, sockunion_get_port (&peer->su),
Paul Jakma6e199262008-09-09 17:14:33 +01001507 LOOKUP (bgp_status_msg, realpeer->status));
pauleb821182004-05-01 08:44:08 +00001508
1509 bgp_stop (realpeer);
1510
1511 /* Transfer file descriptor. */
1512 realpeer->fd = peer->fd;
1513 peer->fd = -1;
1514
1515 /* Transfer input buffer. */
1516 stream_free (realpeer->ibuf);
1517 realpeer->ibuf = peer->ibuf;
1518 realpeer->packet_size = peer->packet_size;
1519 peer->ibuf = NULL;
Paul Jakmaac278ea2016-04-26 11:46:34 +01001520
Paul Jakma2d81a7a2016-04-20 14:05:20 +01001521 /* Transfer output buffer, there may be an OPEN queued to send */
1522 stream_fifo_free (realpeer->obuf);
1523 realpeer->obuf = peer->obuf;
1524 peer->obuf = NULL;
Paul Jakmaac278ea2016-04-26 11:46:34 +01001525
pauleb821182004-05-01 08:44:08 +00001526 /* Transfer status. */
1527 realpeer->status = peer->status;
1528 bgp_stop (peer);
paul200df112005-06-01 11:17:05 +00001529
Paul Jakmaac278ea2016-04-26 11:46:34 +01001530 /* peer pointer change */
pauleb821182004-05-01 08:44:08 +00001531 peer = realpeer;
pauleb821182004-05-01 08:44:08 +00001532 if (peer->fd < 0)
1533 {
1534 zlog_err ("bgp_open_receive peer's fd is negative value %d",
1535 peer->fd);
1536 return -1;
1537 }
1538 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
Paul Jakma2d81a7a2016-04-20 14:05:20 +01001539 if (stream_fifo_head (peer->obuf))
1540 BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
pauleb821182004-05-01 08:44:08 +00001541 }
1542
paul718e3742002-12-13 20:15:29 +00001543 /* remote router-id check. */
1544 if (remote_id.s_addr == 0
Denis Ovsienko733cd9e2011-12-17 19:39:30 +04001545 || IPV4_CLASS_DE (ntohl (remote_id.s_addr))
paul718e3742002-12-13 20:15:29 +00001546 || ntohl (peer->local_id.s_addr) == ntohl (remote_id.s_addr))
1547 {
1548 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001549 zlog_debug ("%s bad OPEN, wrong router identifier %s",
paul718e3742002-12-13 20:15:29 +00001550 peer->host, inet_ntoa (remote_id));
1551 bgp_notify_send_with_data (peer,
1552 BGP_NOTIFY_OPEN_ERR,
1553 BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
1554 notify_data_remote_id, 4);
1555 return -1;
1556 }
1557
1558 /* Set remote router-id */
1559 peer->remote_id = remote_id;
1560
1561 /* Peer BGP version check. */
1562 if (version != BGP_VERSION_4)
1563 {
Leonid Rosenboima689e6a2012-12-07 21:25:00 +00001564 u_int16_t maxver = htons(BGP_VERSION_4);
1565 /* XXX this reply may not be correct if version < 4 XXX */
paul718e3742002-12-13 20:15:29 +00001566 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001567 zlog_debug ("%s bad protocol version, remote requested %d, local request %d",
paul718e3742002-12-13 20:15:29 +00001568 peer->host, version, BGP_VERSION_4);
Leonid Rosenboima689e6a2012-12-07 21:25:00 +00001569 /* Data must be in network byte order here */
paul718e3742002-12-13 20:15:29 +00001570 bgp_notify_send_with_data (peer,
1571 BGP_NOTIFY_OPEN_ERR,
1572 BGP_NOTIFY_OPEN_UNSUP_VERSION,
Leonid Rosenboima689e6a2012-12-07 21:25:00 +00001573 (u_int8_t *) &maxver, 2);
paul718e3742002-12-13 20:15:29 +00001574 return -1;
1575 }
1576
1577 /* Check neighbor as number. */
1578 if (remote_as != peer->as)
1579 {
1580 if (BGP_DEBUG (normal, NORMAL))
Denis Ovsienkoaea339f2009-04-30 17:16:22 +04001581 zlog_debug ("%s bad OPEN, remote AS is %u, expected %u",
paul718e3742002-12-13 20:15:29 +00001582 peer->host, remote_as, peer->as);
1583 bgp_notify_send_with_data (peer,
1584 BGP_NOTIFY_OPEN_ERR,
1585 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1586 notify_data_remote_as, 2);
1587 return -1;
1588 }
1589
1590 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1591 calculate the value of the Hold Timer by using the smaller of its
1592 configured Hold Time and the Hold Time received in the OPEN message.
1593 The Hold Time MUST be either zero or at least three seconds. An
1594 implementation may reject connections on the basis of the Hold Time. */
1595
1596 if (holdtime < 3 && holdtime != 0)
1597 {
Daniel Waltonc6969872015-05-19 18:03:43 -07001598 bgp_notify_send_with_data (peer,
1599 BGP_NOTIFY_OPEN_ERR,
1600 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME,
1601 (u_int8_t *)holdtime_ptr, 2);
paul718e3742002-12-13 20:15:29 +00001602 return -1;
1603 }
1604
1605 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1606 would be one third of the Hold Time interval. KEEPALIVE messages
1607 MUST NOT be sent more frequently than one per second. An
1608 implementation MAY adjust the rate at which it sends KEEPALIVE
1609 messages as a function of the Hold Time interval. */
1610
1611 if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
1612 send_holdtime = peer->holdtime;
1613 else
1614 send_holdtime = peer->bgp->default_holdtime;
1615
1616 if (holdtime < send_holdtime)
1617 peer->v_holdtime = holdtime;
1618 else
1619 peer->v_holdtime = send_holdtime;
1620
1621 peer->v_keepalive = peer->v_holdtime / 3;
1622
1623 /* Open option part parse. */
paul718e3742002-12-13 20:15:29 +00001624 if (optlen != 0)
1625 {
Avneesh Sachdev3b381c32012-02-19 10:19:52 -08001626 if ((ret = bgp_open_option_parse (peer, optlen, &mp_capability)) < 0)
Paul Jakma58617392012-01-09 20:59:26 +00001627 {
1628 bgp_notify_send (peer,
1629 BGP_NOTIFY_OPEN_ERR,
Paul Jakma68ec4242015-11-25 17:14:34 +00001630 BGP_NOTIFY_OPEN_UNSPECIFIC);
Paul Jakma58617392012-01-09 20:59:26 +00001631 return ret;
1632 }
paul718e3742002-12-13 20:15:29 +00001633 }
1634 else
1635 {
1636 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00001637 zlog_debug ("%s rcvd OPEN w/ OPTION parameter len: 0",
paul718e3742002-12-13 20:15:29 +00001638 peer->host);
1639 }
1640
Avneesh Sachdev3b381c32012-02-19 10:19:52 -08001641 /*
1642 * Assume that the peer supports the locally configured set of
1643 * AFI/SAFIs if the peer did not send us any Mulitiprotocol
1644 * capabilities, or if 'override-capability' is configured.
1645 */
1646 if (! mp_capability ||
1647 CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
paul718e3742002-12-13 20:15:29 +00001648 {
1649 peer->afc_nego[AFI_IP][SAFI_UNICAST] = peer->afc[AFI_IP][SAFI_UNICAST];
1650 peer->afc_nego[AFI_IP][SAFI_MULTICAST] = peer->afc[AFI_IP][SAFI_MULTICAST];
1651 peer->afc_nego[AFI_IP6][SAFI_UNICAST] = peer->afc[AFI_IP6][SAFI_UNICAST];
1652 peer->afc_nego[AFI_IP6][SAFI_MULTICAST] = peer->afc[AFI_IP6][SAFI_MULTICAST];
1653 }
1654
1655 /* Get sockname. */
1656 bgp_getsockname (peer);
Timo Teräs0edba8b2015-10-22 11:35:17 +03001657 peer->rtt = sockopt_tcp_rtt (peer->fd);
paul718e3742002-12-13 20:15:29 +00001658
1659 BGP_EVENT_ADD (peer, Receive_OPEN_message);
1660
1661 peer->packet_size = 0;
1662 if (peer->ibuf)
1663 stream_reset (peer->ibuf);
1664
1665 return 0;
1666}
1667
Paul Jakma518a4b72016-02-04 13:27:04 +00001668/* Frontend for NLRI parsing, to fan-out to AFI/SAFI specific parsers */
1669int
1670bgp_nlri_parse (struct peer *peer, struct attr *attr, struct bgp_nlri *packet)
1671{
1672 switch (packet->safi)
1673 {
1674 case SAFI_UNICAST:
1675 case SAFI_MULTICAST:
1676 return bgp_nlri_parse_ip (peer, attr, packet);
1677 case SAFI_MPLS_VPN:
1678 case SAFI_MPLS_LABELED_VPN:
1679 return bgp_nlri_parse_vpn (peer, attr, packet);
1680 case SAFI_ENCAP:
1681 return bgp_nlri_parse_encap (peer, attr, packet);
1682 }
1683 return -1;
1684}
1685
paul718e3742002-12-13 20:15:29 +00001686/* Parse BGP Update packet and make attribute object. */
paul94f2b392005-06-28 12:44:16 +00001687static int
paul718e3742002-12-13 20:15:29 +00001688bgp_update_receive (struct peer *peer, bgp_size_t size)
1689{
Paul Jakma518a4b72016-02-04 13:27:04 +00001690 int ret, nlri_ret;
paul718e3742002-12-13 20:15:29 +00001691 u_char *end;
1692 struct stream *s;
1693 struct attr attr;
Jorge Boncompte [DTI2]489d0052012-05-07 16:53:03 +00001694 struct attr_extra extra;
paul718e3742002-12-13 20:15:29 +00001695 bgp_size_t attribute_len;
1696 bgp_size_t update_len;
1697 bgp_size_t withdraw_len;
Paul Jakma518a4b72016-02-04 13:27:04 +00001698 int i;
1699
1700 enum NLRI_TYPES {
1701 NLRI_UPDATE,
1702 NLRI_WITHDRAW,
1703 NLRI_MP_UPDATE,
1704 NLRI_MP_WITHDRAW,
1705 NLRI_TYPE_MAX,
1706 };
1707 struct bgp_nlri nlris[NLRI_TYPE_MAX];
paul718e3742002-12-13 20:15:29 +00001708
1709 /* Status must be Established. */
1710 if (peer->status != Established)
1711 {
1712 zlog_err ("%s [FSM] Update packet received under status %s",
1713 peer->host, LOOKUP (bgp_status_msg, peer->status));
1714 bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
1715 return -1;
1716 }
1717
1718 /* Set initial values. */
1719 memset (&attr, 0, sizeof (struct attr));
Jorge Boncompte [DTI2]489d0052012-05-07 16:53:03 +00001720 memset (&extra, 0, sizeof (struct attr_extra));
Paul Jakma518a4b72016-02-04 13:27:04 +00001721 memset (&nlris, 0, sizeof nlris);
Paul Jakma3b847ef2016-04-22 12:48:49 +01001722
Jorge Boncompte [DTI2]489d0052012-05-07 16:53:03 +00001723 attr.extra = &extra;
paul718e3742002-12-13 20:15:29 +00001724
1725 s = peer->ibuf;
1726 end = stream_pnt (s) + size;
1727
1728 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1729 Length is too large (i.e., if Unfeasible Routes Length + Total
1730 Attribute Length + 23 exceeds the message Length), then the Error
1731 Subcode is set to Malformed Attribute List. */
1732 if (stream_pnt (s) + 2 > end)
1733 {
1734 zlog_err ("%s [Error] Update packet error"
1735 " (packet length is short for unfeasible length)",
1736 peer->host);
1737 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1738 BGP_NOTIFY_UPDATE_MAL_ATTR);
1739 return -1;
1740 }
1741
1742 /* Unfeasible Route Length. */
1743 withdraw_len = stream_getw (s);
1744
1745 /* Unfeasible Route Length check. */
1746 if (stream_pnt (s) + withdraw_len > end)
1747 {
1748 zlog_err ("%s [Error] Update packet error"
1749 " (packet unfeasible length overflow %d)",
1750 peer->host, withdraw_len);
1751 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1752 BGP_NOTIFY_UPDATE_MAL_ATTR);
1753 return -1;
1754 }
1755
1756 /* Unfeasible Route packet format check. */
1757 if (withdraw_len > 0)
1758 {
Paul Jakma518a4b72016-02-04 13:27:04 +00001759 nlris[NLRI_WITHDRAW].afi = AFI_IP;
1760 nlris[NLRI_WITHDRAW].safi = SAFI_UNICAST;
1761 nlris[NLRI_WITHDRAW].nlri = stream_pnt (s);
1762 nlris[NLRI_WITHDRAW].length = withdraw_len;
1763
paul718e3742002-12-13 20:15:29 +00001764 if (BGP_DEBUG (packet, PACKET_RECV))
ajs6b514742004-12-08 21:03:23 +00001765 zlog_debug ("%s [Update:RECV] Unfeasible NLRI received", peer->host);
paul718e3742002-12-13 20:15:29 +00001766
paul9985f832005-02-09 15:51:56 +00001767 stream_forward_getp (s, withdraw_len);
paul718e3742002-12-13 20:15:29 +00001768 }
1769
1770 /* Attribute total length check. */
1771 if (stream_pnt (s) + 2 > end)
1772 {
1773 zlog_warn ("%s [Error] Packet Error"
1774 " (update packet is short for attribute length)",
1775 peer->host);
1776 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1777 BGP_NOTIFY_UPDATE_MAL_ATTR);
1778 return -1;
1779 }
1780
1781 /* Fetch attribute total length. */
1782 attribute_len = stream_getw (s);
1783
1784 /* Attribute length check. */
1785 if (stream_pnt (s) + attribute_len > end)
1786 {
1787 zlog_warn ("%s [Error] Packet Error"
1788 " (update packet attribute length overflow %d)",
1789 peer->host, attribute_len);
1790 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1791 BGP_NOTIFY_UPDATE_MAL_ATTR);
1792 return -1;
1793 }
Paul Jakmab881c702010-11-23 16:35:42 +00001794
1795 /* Certain attribute parsing errors should not be considered bad enough
1796 * to reset the session for, most particularly any partial/optional
1797 * attributes that have 'tunneled' over speakers that don't understand
1798 * them. Instead we withdraw only the prefix concerned.
1799 *
1800 * Complicates the flow a little though..
1801 */
1802 bgp_attr_parse_ret_t attr_parse_ret = BGP_ATTR_PARSE_PROCEED;
1803 /* This define morphs the update case into a withdraw when lower levels
1804 * have signalled an error condition where this is best.
1805 */
1806#define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
paul718e3742002-12-13 20:15:29 +00001807
1808 /* Parse attribute when it exists. */
1809 if (attribute_len)
1810 {
Paul Jakmab881c702010-11-23 16:35:42 +00001811 attr_parse_ret = bgp_attr_parse (peer, &attr, attribute_len,
Paul Jakma518a4b72016-02-04 13:27:04 +00001812 &nlris[NLRI_MP_UPDATE], &nlris[NLRI_MP_WITHDRAW]);
Paul Jakmab881c702010-11-23 16:35:42 +00001813 if (attr_parse_ret == BGP_ATTR_PARSE_ERROR)
David Lamparterf80f8382014-06-04 01:00:51 +02001814 {
1815 bgp_attr_unintern_sub (&attr);
Lou Berger050defe2016-01-12 13:41:59 -05001816 bgp_attr_flush (&attr);
David Lamparterf80f8382014-06-04 01:00:51 +02001817 return -1;
1818 }
paul718e3742002-12-13 20:15:29 +00001819 }
Paul Jakmab881c702010-11-23 16:35:42 +00001820
paul718e3742002-12-13 20:15:29 +00001821 /* Logging the attribute. */
Paul Jakmab881c702010-11-23 16:35:42 +00001822 if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW
1823 || BGP_DEBUG (update, UPDATE_IN))
paul718e3742002-12-13 20:15:29 +00001824 {
Jorge Boncompte [DTI2]14542f32012-05-07 16:52:53 +00001825 char attrstr[BUFSIZ];
1826 attrstr[0] = '\0';
1827
paule01f9cb2004-07-09 17:48:53 +00001828 ret= bgp_dump_attr (peer, &attr, attrstr, BUFSIZ);
Paul Jakmab881c702010-11-23 16:35:42 +00001829 int lvl = (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW)
1830 ? LOG_ERR : LOG_DEBUG;
1831
1832 if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW)
1833 zlog (peer->log, LOG_ERR,
1834 "%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
1835 peer->host);
paule01f9cb2004-07-09 17:48:53 +00001836
1837 if (ret)
Paul Jakmab881c702010-11-23 16:35:42 +00001838 zlog (peer->log, lvl, "%s rcvd UPDATE w/ attr: %s",
paule01f9cb2004-07-09 17:48:53 +00001839 peer->host, attrstr);
paul718e3742002-12-13 20:15:29 +00001840 }
Paul Jakmab881c702010-11-23 16:35:42 +00001841
paul718e3742002-12-13 20:15:29 +00001842 /* Network Layer Reachability Information. */
1843 update_len = end - stream_pnt (s);
1844
1845 if (update_len)
1846 {
Paul Jakma18ab08b2016-01-27 16:37:33 +00001847 /* Set NLRI portion to structure. */
Paul Jakma518a4b72016-02-04 13:27:04 +00001848 nlris[NLRI_UPDATE].afi = AFI_IP;
1849 nlris[NLRI_UPDATE].safi = SAFI_UNICAST;
1850 nlris[NLRI_UPDATE].nlri = stream_pnt (s);
1851 nlris[NLRI_UPDATE].length = update_len;
Paul Jakma18ab08b2016-01-27 16:37:33 +00001852
paul9985f832005-02-09 15:51:56 +00001853 stream_forward_getp (s, update_len);
paul718e3742002-12-13 20:15:29 +00001854 }
Paul Jakma518a4b72016-02-04 13:27:04 +00001855
1856 /* Parse any given NLRIs */
1857 for (i = NLRI_UPDATE; i < NLRI_TYPE_MAX; i++)
paul718e3742002-12-13 20:15:29 +00001858 {
Paul Jakma3b847ef2016-04-22 12:48:49 +01001859 if (!nlris[i].nlri) continue;
1860
Paul Jakma518a4b72016-02-04 13:27:04 +00001861 /* We use afi and safi as indices into tables and what not. It would
1862 * be impossible, at this time, to support unknown afi/safis. And
1863 * anyway, the peer needs to be configured to enable the afi/safi
1864 * explicitly which requires UI support.
1865 *
1866 * Ignore unknown afi/safi NLRIs.
1867 *
1868 * Note: this means nlri[x].afi/safi still can not be trusted for
1869 * indexing later in this function!
1870 *
1871 * Note2: This will also remap the wire code-point for VPN safi to the
1872 * internal safi_t point, as needs be.
1873 */
1874 if (!bgp_afi_safi_valid_indices (nlris[i].afi, &nlris[i].safi))
1875 {
1876 plog_info (peer->log,
1877 "%s [Info] UPDATE with unsupported AFI/SAFI %u/%u",
1878 peer->host, nlris[i].afi, nlris[i].safi);
1879 continue;
1880 }
1881
1882 /* NLRI is processed only when the peer is configured specific
1883 Address Family and Subsequent Address Family. */
1884 if (!peer->afc[nlris[i].afi][nlris[i].safi])
1885 {
1886 plog_info (peer->log,
1887 "%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u",
1888 peer->host, nlris[i].afi, nlris[i].safi);
1889 continue;
1890 }
1891
1892 /* EoR handled later */
1893 if (nlris[i].length == 0)
1894 continue;
1895
1896 switch (i)
1897 {
1898 case NLRI_UPDATE:
1899 case NLRI_MP_UPDATE:
1900 nlri_ret = bgp_nlri_parse (peer, NLRI_ATTR_ARG, &nlris[i]);
1901 break;
1902 case NLRI_WITHDRAW:
1903 case NLRI_MP_WITHDRAW:
1904 nlri_ret = bgp_nlri_parse (peer, NULL, &nlris[i]);
1905 }
1906
1907 if (nlri_ret < 0)
1908 {
1909 plog_err (peer->log,
1910 "%s [Error] Error parsing NLRI", peer->host);
1911 if (peer->status == Established)
1912 bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
1913 i <= NLRI_WITHDRAW
1914 ? BGP_NOTIFY_UPDATE_INVAL_NETWORK
1915 : BGP_NOTIFY_UPDATE_OPT_ATTR_ERR);
1916 bgp_attr_unintern_sub (&attr);
1917 return -1;
1918 }
1919 }
1920
1921 /* EoR checks.
1922 *
1923 * Non-MP IPv4/Unicast EoR is a completely empty UPDATE
1924 * and MP EoR should have only an empty MP_UNREACH
1925 */
1926 if (!update_len && !withdraw_len
1927 && nlris[NLRI_MP_UPDATE].length == 0)
1928 {
1929 afi_t afi = 0;
1930 safi_t safi;
1931
1932 /* Non-MP IPv4/Unicast is a completely empty UPDATE - already
1933 * checked update and withdraw NLRI lengths are 0.
1934 */
1935 if (!attribute_len)
1936 {
1937 afi = AFI_IP;
1938 safi = SAFI_UNICAST;
1939 }
1940 /* otherwise MP AFI/SAFI is an empty update, other than an empty
1941 * MP_UNREACH_NLRI attr (with an AFI/SAFI we recognise).
1942 */
1943 else if (attr.flag == BGP_ATTR_MP_UNREACH_NLRI
1944 && nlris[NLRI_MP_WITHDRAW].length == 0
1945 && bgp_afi_safi_valid_indices (nlris[NLRI_MP_WITHDRAW].afi,
1946 &nlris[NLRI_MP_WITHDRAW].safi))
1947 {
1948 afi = nlris[NLRI_MP_WITHDRAW].afi;
1949 safi = nlris[NLRI_MP_WITHDRAW].safi;
1950 }
1951
1952 if (afi && peer->afc[afi][safi])
1953 {
paule01f9cb2004-07-09 17:48:53 +00001954 /* End-of-RIB received */
Paul Jakma518a4b72016-02-04 13:27:04 +00001955 SET_FLAG (peer->af_sflags[afi][safi],
hasso93406d82005-02-02 14:40:33 +00001956 PEER_STATUS_EOR_RECEIVED);
paule01f9cb2004-07-09 17:48:53 +00001957
hasso93406d82005-02-02 14:40:33 +00001958 /* NSF delete stale route */
Paul Jakma518a4b72016-02-04 13:27:04 +00001959 if (peer->nsf[afi][safi])
1960 bgp_clear_stale_route (peer, afi, safi);
hasso93406d82005-02-02 14:40:33 +00001961
1962 if (BGP_DEBUG (normal, NORMAL))
Paul Jakma518a4b72016-02-04 13:27:04 +00001963 zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for %s from %s",
1964 peer->host, afi_safi_print (afi, safi));
1965 }
paul718e3742002-12-13 20:15:29 +00001966 }
Paul Jakma518a4b72016-02-04 13:27:04 +00001967
paul718e3742002-12-13 20:15:29 +00001968 /* Everything is done. We unintern temporary structures which
1969 interned in bgp_attr_parse(). */
Paul Jakmab881c702010-11-23 16:35:42 +00001970 bgp_attr_unintern_sub (&attr);
Lou Berger050defe2016-01-12 13:41:59 -05001971 bgp_attr_flush (&attr);
Jorge Boncompte [DTI2]489d0052012-05-07 16:53:03 +00001972
paul718e3742002-12-13 20:15:29 +00001973 /* If peering is stopped due to some reason, do not generate BGP
1974 event. */
1975 if (peer->status != Established)
1976 return 0;
1977
1978 /* Increment packet counter. */
1979 peer->update_in++;
Stephen Hemminger65957882010-01-15 16:22:10 +03001980 peer->update_time = bgp_clock ();
paul718e3742002-12-13 20:15:29 +00001981
Jorge Boncompte [DTI2]e2c38e62012-06-20 17:45:50 +02001982 /* Rearm holdtime timer */
Jorge Boncompte [DTI2]6a4677b2012-05-07 16:53:07 +00001983 BGP_TIMER_OFF (peer->t_holdtime);
Jorge Boncompte [DTI2]e2c38e62012-06-20 17:45:50 +02001984 bgp_timer_set (peer);
paul718e3742002-12-13 20:15:29 +00001985
1986 return 0;
1987}
1988
1989/* Notify message treatment function. */
paul94f2b392005-06-28 12:44:16 +00001990static void
paul718e3742002-12-13 20:15:29 +00001991bgp_notify_receive (struct peer *peer, bgp_size_t size)
1992{
1993 struct bgp_notify bgp_notify;
1994
1995 if (peer->notify.data)
1996 {
1997 XFREE (MTYPE_TMP, peer->notify.data);
1998 peer->notify.data = NULL;
1999 peer->notify.length = 0;
2000 }
2001
2002 bgp_notify.code = stream_getc (peer->ibuf);
2003 bgp_notify.subcode = stream_getc (peer->ibuf);
2004 bgp_notify.length = size - 2;
2005 bgp_notify.data = NULL;
2006
2007 /* Preserv notify code and sub code. */
2008 peer->notify.code = bgp_notify.code;
2009 peer->notify.subcode = bgp_notify.subcode;
2010 /* For further diagnostic record returned Data. */
2011 if (bgp_notify.length)
2012 {
2013 peer->notify.length = size - 2;
2014 peer->notify.data = XMALLOC (MTYPE_TMP, size - 2);
2015 memcpy (peer->notify.data, stream_pnt (peer->ibuf), size - 2);
2016 }
2017
2018 /* For debug */
2019 {
2020 int i;
2021 int first = 0;
2022 char c[4];
2023
2024 if (bgp_notify.length)
2025 {
2026 bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3);
2027 for (i = 0; i < bgp_notify.length; i++)
2028 if (first)
2029 {
2030 sprintf (c, " %02x", stream_getc (peer->ibuf));
2031 strcat (bgp_notify.data, c);
2032 }
2033 else
2034 {
2035 first = 1;
2036 sprintf (c, "%02x", stream_getc (peer->ibuf));
2037 strcpy (bgp_notify.data, c);
2038 }
2039 }
2040
2041 bgp_notify_print(peer, &bgp_notify, "received");
2042 if (bgp_notify.data)
Daniel Walton363c9032015-10-21 06:42:54 -07002043 {
2044 XFREE (MTYPE_TMP, bgp_notify.data);
2045 bgp_notify.data = NULL;
2046 bgp_notify.length = 0;
2047 }
paul718e3742002-12-13 20:15:29 +00002048 }
2049
2050 /* peer count update */
2051 peer->notify_in++;
2052
hassoe0701b72004-05-20 09:19:34 +00002053 if (peer->status == Established)
2054 peer->last_reset = PEER_DOWN_NOTIFY_RECEIVED;
2055
paul718e3742002-12-13 20:15:29 +00002056 /* We have to check for Notify with Unsupported Optional Parameter.
2057 in that case we fallback to open without the capability option.
2058 But this done in bgp_stop. We just mark it here to avoid changing
2059 the fsm tables. */
2060 if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR &&
2061 bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM )
2062 UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
2063
paul718e3742002-12-13 20:15:29 +00002064 BGP_EVENT_ADD (peer, Receive_NOTIFICATION_message);
2065}
2066
2067/* Keepalive treatment function -- get keepalive send keepalive */
paul94f2b392005-06-28 12:44:16 +00002068static void
paul718e3742002-12-13 20:15:29 +00002069bgp_keepalive_receive (struct peer *peer, bgp_size_t size)
2070{
2071 if (BGP_DEBUG (keepalive, KEEPALIVE))
ajs6b514742004-12-08 21:03:23 +00002072 zlog_debug ("%s KEEPALIVE rcvd", peer->host);
paul718e3742002-12-13 20:15:29 +00002073
2074 BGP_EVENT_ADD (peer, Receive_KEEPALIVE_message);
2075}
2076
2077/* Route refresh message is received. */
paul94f2b392005-06-28 12:44:16 +00002078static void
paul718e3742002-12-13 20:15:29 +00002079bgp_route_refresh_receive (struct peer *peer, bgp_size_t size)
2080{
2081 afi_t afi;
2082 safi_t safi;
paul718e3742002-12-13 20:15:29 +00002083 struct stream *s;
2084
2085 /* If peer does not have the capability, send notification. */
2086 if (! CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_ADV))
2087 {
2088 plog_err (peer->log, "%s [Error] BGP route refresh is not enabled",
2089 peer->host);
2090 bgp_notify_send (peer,
2091 BGP_NOTIFY_HEADER_ERR,
2092 BGP_NOTIFY_HEADER_BAD_MESTYPE);
2093 return;
2094 }
2095
2096 /* Status must be Established. */
2097 if (peer->status != Established)
2098 {
2099 plog_err (peer->log,
2100 "%s [Error] Route refresh packet received under status %s",
2101 peer->host, LOOKUP (bgp_status_msg, peer->status));
2102 bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
2103 return;
2104 }
2105
2106 s = peer->ibuf;
2107
2108 /* Parse packet. */
2109 afi = stream_getw (s);
Paul Jakma7aa9dce2014-09-19 14:42:23 +01002110 /* reserved byte */
2111 stream_getc (s);
paul718e3742002-12-13 20:15:29 +00002112 safi = stream_getc (s);
2113
2114 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00002115 zlog_debug ("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
paul718e3742002-12-13 20:15:29 +00002116 peer->host, afi, safi);
2117
2118 /* Check AFI and SAFI. */
2119 if ((afi != AFI_IP && afi != AFI_IP6)
2120 || (safi != SAFI_UNICAST && safi != SAFI_MULTICAST
Denis Ovsienko42e6d742011-07-14 12:36:19 +04002121 && safi != SAFI_MPLS_LABELED_VPN))
paul718e3742002-12-13 20:15:29 +00002122 {
2123 if (BGP_DEBUG (normal, NORMAL))
2124 {
ajs6b514742004-12-08 21:03:23 +00002125 zlog_debug ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
paul718e3742002-12-13 20:15:29 +00002126 peer->host, afi, safi);
2127 }
2128 return;
2129 }
2130
2131 /* Adjust safi code. */
Denis Ovsienko42e6d742011-07-14 12:36:19 +04002132 if (safi == SAFI_MPLS_LABELED_VPN)
paul718e3742002-12-13 20:15:29 +00002133 safi = SAFI_MPLS_VPN;
2134
2135 if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE)
2136 {
2137 u_char *end;
2138 u_char when_to_refresh;
2139 u_char orf_type;
2140 u_int16_t orf_len;
2141
2142 if (size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) < 5)
2143 {
2144 zlog_info ("%s ORF route refresh length error", peer->host);
2145 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
2146 return;
2147 }
2148
2149 when_to_refresh = stream_getc (s);
2150 end = stream_pnt (s) + (size - 5);
2151
Paul Jakma370b64a2007-12-22 16:49:52 +00002152 while ((stream_pnt (s) + 2) < end)
paul718e3742002-12-13 20:15:29 +00002153 {
2154 orf_type = stream_getc (s);
2155 orf_len = stream_getw (s);
Paul Jakma370b64a2007-12-22 16:49:52 +00002156
2157 /* orf_len in bounds? */
2158 if ((stream_pnt (s) + orf_len) > end)
2159 break; /* XXX: Notify instead?? */
paul718e3742002-12-13 20:15:29 +00002160 if (orf_type == ORF_TYPE_PREFIX
2161 || orf_type == ORF_TYPE_PREFIX_OLD)
2162 {
Paul Jakma7aa9dce2014-09-19 14:42:23 +01002163 uint8_t *p_pnt = stream_pnt (s);
2164 uint8_t *p_end = stream_pnt (s) + orf_len;
paul718e3742002-12-13 20:15:29 +00002165 struct orf_prefix orfp;
2166 u_char common = 0;
2167 u_int32_t seq;
2168 int psize;
2169 char name[BUFSIZ];
paul718e3742002-12-13 20:15:29 +00002170 int ret;
2171
2172 if (BGP_DEBUG (normal, NORMAL))
2173 {
ajs6b514742004-12-08 21:03:23 +00002174 zlog_debug ("%s rcvd Prefixlist ORF(%d) length %d",
paul718e3742002-12-13 20:15:29 +00002175 peer->host, orf_type, orf_len);
2176 }
2177
Paul Jakma370b64a2007-12-22 16:49:52 +00002178 /* we're going to read at least 1 byte of common ORF header,
2179 * and 7 bytes of ORF Address-filter entry from the stream
2180 */
2181 if (orf_len < 7)
2182 break;
2183
paul718e3742002-12-13 20:15:29 +00002184 /* ORF prefix-list name */
2185 sprintf (name, "%s.%d.%d", peer->host, afi, safi);
2186
2187 while (p_pnt < p_end)
2188 {
Chris Halld64379e2010-05-14 16:38:39 +04002189 /* If the ORF entry is malformed, want to read as much of it
2190 * as possible without going beyond the bounds of the entry,
2191 * to maximise debug information.
2192 */
Paul Jakmafdbc8e72011-04-11 16:31:43 +01002193 int ok;
paul718e3742002-12-13 20:15:29 +00002194 memset (&orfp, 0, sizeof (struct orf_prefix));
2195 common = *p_pnt++;
Chris Halld64379e2010-05-14 16:38:39 +04002196 /* after ++: p_pnt <= p_end */
paul718e3742002-12-13 20:15:29 +00002197 if (common & ORF_COMMON_PART_REMOVE_ALL)
2198 {
2199 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00002200 zlog_debug ("%s rcvd Remove-All pfxlist ORF request", peer->host);
David Lamparterc9c06d02015-04-13 10:21:35 +02002201 prefix_bgp_orf_remove_all (afi, name);
paul718e3742002-12-13 20:15:29 +00002202 break;
2203 }
Paul Jakma7aa9dce2014-09-19 14:42:23 +01002204 ok = ((size_t)(p_end - p_pnt) >= sizeof(u_int32_t)) ;
Denis Ovsienkobb915f52011-12-13 21:11:39 +04002205 if (ok)
Chris Halld64379e2010-05-14 16:38:39 +04002206 {
Paul Jakmafdbc8e72011-04-11 16:31:43 +01002207 memcpy (&seq, p_pnt, sizeof (u_int32_t));
2208 p_pnt += sizeof (u_int32_t);
2209 orfp.seq = ntohl (seq);
Chris Halld64379e2010-05-14 16:38:39 +04002210 }
2211 else
2212 p_pnt = p_end ;
2213
2214 if ((ok = (p_pnt < p_end)))
2215 orfp.ge = *p_pnt++ ; /* value checked in prefix_bgp_orf_set() */
2216 if ((ok = (p_pnt < p_end)))
2217 orfp.le = *p_pnt++ ; /* value checked in prefix_bgp_orf_set() */
2218 if ((ok = (p_pnt < p_end)))
2219 orfp.p.prefixlen = *p_pnt++ ;
2220 orfp.p.family = afi2family (afi); /* afi checked already */
2221
2222 psize = PSIZE (orfp.p.prefixlen); /* 0 if not ok */
2223 if (psize > prefix_blen(&orfp.p)) /* valid for family ? */
2224 {
2225 ok = 0 ;
2226 psize = prefix_blen(&orfp.p) ;
2227 }
2228 if (psize > (p_end - p_pnt)) /* valid for packet ? */
2229 {
2230 ok = 0 ;
2231 psize = p_end - p_pnt ;
2232 }
2233
2234 if (psize > 0)
2235 memcpy (&orfp.p.u.prefix, p_pnt, psize);
paul718e3742002-12-13 20:15:29 +00002236 p_pnt += psize;
2237
2238 if (BGP_DEBUG (normal, NORMAL))
Jorge Boncompte [DTI2]14542f32012-05-07 16:52:53 +00002239 {
2240 char buf[INET6_BUFSIZ];
2241
2242 zlog_debug ("%s rcvd %s %s seq %u %s/%d ge %d le %d%s",
2243 peer->host,
2244 (common & ORF_COMMON_PART_REMOVE ? "Remove" : "Add"),
2245 (common & ORF_COMMON_PART_DENY ? "deny" : "permit"),
2246 orfp.seq,
2247 inet_ntop (orfp.p.family, &orfp.p.u.prefix, buf, INET6_BUFSIZ),
2248 orfp.p.prefixlen, orfp.ge, orfp.le,
2249 ok ? "" : " MALFORMED");
2250 }
2251
Chris Halld64379e2010-05-14 16:38:39 +04002252 if (ok)
Paul Jakmafdbc8e72011-04-11 16:31:43 +01002253 ret = prefix_bgp_orf_set (name, afi, &orfp,
2254 (common & ORF_COMMON_PART_DENY ? 0 : 1 ),
2255 (common & ORF_COMMON_PART_REMOVE ? 0 : 1));
Paul Jakma7aa9dce2014-09-19 14:42:23 +01002256
2257 if (!ok || (ok && ret != CMD_SUCCESS))
paul718e3742002-12-13 20:15:29 +00002258 {
2259 if (BGP_DEBUG (normal, NORMAL))
Paul Jakmafdbc8e72011-04-11 16:31:43 +01002260 zlog_debug ("%s Received misformatted prefixlist ORF."
2261 " Remove All pfxlist", peer->host);
David Lamparterc9c06d02015-04-13 10:21:35 +02002262 prefix_bgp_orf_remove_all (afi, name);
paul718e3742002-12-13 20:15:29 +00002263 break;
2264 }
2265 }
2266 peer->orf_plist[afi][safi] =
David Lamparterc9c06d02015-04-13 10:21:35 +02002267 prefix_bgp_orf_lookup (afi, name);
paul718e3742002-12-13 20:15:29 +00002268 }
paul9985f832005-02-09 15:51:56 +00002269 stream_forward_getp (s, orf_len);
paul718e3742002-12-13 20:15:29 +00002270 }
2271 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00002272 zlog_debug ("%s rcvd Refresh %s ORF request", peer->host,
paul718e3742002-12-13 20:15:29 +00002273 when_to_refresh == REFRESH_DEFER ? "Defer" : "Immediate");
2274 if (when_to_refresh == REFRESH_DEFER)
2275 return;
2276 }
2277
2278 /* First update is deferred until ORF or ROUTE-REFRESH is received */
2279 if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH))
2280 UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH);
2281
2282 /* Perform route refreshment to the peer */
2283 bgp_announce_route (peer, afi, safi);
2284}
2285
paul94f2b392005-06-28 12:44:16 +00002286static int
paul718e3742002-12-13 20:15:29 +00002287bgp_capability_msg_parse (struct peer *peer, u_char *pnt, bgp_size_t length)
2288{
2289 u_char *end;
Paul Jakma6d582722007-08-06 15:21:45 +00002290 struct capability_mp_data mpc;
2291 struct capability_header *hdr;
paul718e3742002-12-13 20:15:29 +00002292 u_char action;
paul718e3742002-12-13 20:15:29 +00002293 afi_t afi;
2294 safi_t safi;
2295
paul718e3742002-12-13 20:15:29 +00002296 end = pnt + length;
2297
2298 while (pnt < end)
Paul Jakma6d582722007-08-06 15:21:45 +00002299 {
paul718e3742002-12-13 20:15:29 +00002300 /* We need at least action, capability code and capability length. */
2301 if (pnt + 3 > end)
2302 {
2303 zlog_info ("%s Capability length error", peer->host);
2304 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
2305 return -1;
2306 }
paul718e3742002-12-13 20:15:29 +00002307 action = *pnt;
Paul Jakma6d582722007-08-06 15:21:45 +00002308 hdr = (struct capability_header *)(pnt + 1);
2309
paul718e3742002-12-13 20:15:29 +00002310 /* Action value check. */
2311 if (action != CAPABILITY_ACTION_SET
2312 && action != CAPABILITY_ACTION_UNSET)
2313 {
2314 zlog_info ("%s Capability Action Value error %d",
2315 peer->host, action);
2316 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
2317 return -1;
2318 }
2319
2320 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00002321 zlog_debug ("%s CAPABILITY has action: %d, code: %u, length %u",
Paul Jakma6d582722007-08-06 15:21:45 +00002322 peer->host, action, hdr->code, hdr->length);
paul718e3742002-12-13 20:15:29 +00002323
2324 /* Capability length check. */
Paul Jakma6d582722007-08-06 15:21:45 +00002325 if ((pnt + hdr->length + 3) > end)
paul718e3742002-12-13 20:15:29 +00002326 {
2327 zlog_info ("%s Capability length error", peer->host);
2328 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
2329 return -1;
2330 }
2331
Paul Jakma6d582722007-08-06 15:21:45 +00002332 /* Fetch structure to the byte stream. */
2333 memcpy (&mpc, pnt + 3, sizeof (struct capability_mp_data));
2334
paul718e3742002-12-13 20:15:29 +00002335 /* We know MP Capability Code. */
Paul Jakma6d582722007-08-06 15:21:45 +00002336 if (hdr->code == CAPABILITY_CODE_MP)
paul718e3742002-12-13 20:15:29 +00002337 {
Paul Jakma6d582722007-08-06 15:21:45 +00002338 afi = ntohs (mpc.afi);
2339 safi = mpc.safi;
paul718e3742002-12-13 20:15:29 +00002340
2341 /* Ignore capability when override-capability is set. */
2342 if (CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
2343 continue;
Paul Jakma6d582722007-08-06 15:21:45 +00002344
2345 if (!bgp_afi_safi_valid_indices (afi, &safi))
2346 {
2347 if (BGP_DEBUG (normal, NORMAL))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002348 zlog_debug ("%s Dynamic Capability MP_EXT afi/safi invalid "
2349 "(%u/%u)", peer->host, afi, safi);
Paul Jakma6d582722007-08-06 15:21:45 +00002350 continue;
2351 }
2352
paul718e3742002-12-13 20:15:29 +00002353 /* Address family check. */
Paul Jakma6d582722007-08-06 15:21:45 +00002354 if (BGP_DEBUG (normal, NORMAL))
2355 zlog_debug ("%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
2356 peer->host,
2357 action == CAPABILITY_ACTION_SET
2358 ? "Advertising" : "Removing",
2359 ntohs(mpc.afi) , mpc.safi);
2360
2361 if (action == CAPABILITY_ACTION_SET)
2362 {
2363 peer->afc_recv[afi][safi] = 1;
2364 if (peer->afc[afi][safi])
2365 {
2366 peer->afc_nego[afi][safi] = 1;
2367 bgp_announce_route (peer, afi, safi);
2368 }
2369 }
2370 else
2371 {
2372 peer->afc_recv[afi][safi] = 0;
2373 peer->afc_nego[afi][safi] = 0;
paul718e3742002-12-13 20:15:29 +00002374
Paul Jakma6d582722007-08-06 15:21:45 +00002375 if (peer_active_nego (peer))
Chris Caputo228da422009-07-18 05:44:03 +00002376 bgp_clear_route (peer, afi, safi, BGP_CLEAR_ROUTE_NORMAL);
Paul Jakma6d582722007-08-06 15:21:45 +00002377 else
2378 BGP_EVENT_ADD (peer, BGP_Stop);
2379 }
paul718e3742002-12-13 20:15:29 +00002380 }
paul718e3742002-12-13 20:15:29 +00002381 else
2382 {
2383 zlog_warn ("%s unrecognized capability code: %d - ignored",
Paul Jakma6d582722007-08-06 15:21:45 +00002384 peer->host, hdr->code);
paul718e3742002-12-13 20:15:29 +00002385 }
Paul Jakma6d582722007-08-06 15:21:45 +00002386 pnt += hdr->length + 3;
paul718e3742002-12-13 20:15:29 +00002387 }
2388 return 0;
2389}
2390
Paul Jakma01b7ce22009-06-18 12:34:43 +01002391/* Dynamic Capability is received.
2392 *
2393 * This is exported for unit-test purposes
2394 */
Paul Jakma6d582722007-08-06 15:21:45 +00002395int
paul718e3742002-12-13 20:15:29 +00002396bgp_capability_receive (struct peer *peer, bgp_size_t size)
2397{
2398 u_char *pnt;
paul718e3742002-12-13 20:15:29 +00002399
2400 /* Fetch pointer. */
2401 pnt = stream_pnt (peer->ibuf);
2402
2403 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00002404 zlog_debug ("%s rcv CAPABILITY", peer->host);
paul718e3742002-12-13 20:15:29 +00002405
2406 /* If peer does not have the capability, send notification. */
2407 if (! CHECK_FLAG (peer->cap, PEER_CAP_DYNAMIC_ADV))
2408 {
2409 plog_err (peer->log, "%s [Error] BGP dynamic capability is not enabled",
2410 peer->host);
2411 bgp_notify_send (peer,
2412 BGP_NOTIFY_HEADER_ERR,
2413 BGP_NOTIFY_HEADER_BAD_MESTYPE);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002414 return -1;
paul718e3742002-12-13 20:15:29 +00002415 }
2416
2417 /* Status must be Established. */
2418 if (peer->status != Established)
2419 {
2420 plog_err (peer->log,
2421 "%s [Error] Dynamic capability packet received under status %s", peer->host, LOOKUP (bgp_status_msg, peer->status));
2422 bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00002423 return -1;
paul718e3742002-12-13 20:15:29 +00002424 }
2425
2426 /* Parse packet. */
Paul Jakma6d582722007-08-06 15:21:45 +00002427 return bgp_capability_msg_parse (peer, pnt, size);
paul718e3742002-12-13 20:15:29 +00002428}
David Lamparter6b0655a2014-06-04 06:53:35 +02002429
paul718e3742002-12-13 20:15:29 +00002430/* BGP read utility function. */
paul94f2b392005-06-28 12:44:16 +00002431static int
paul718e3742002-12-13 20:15:29 +00002432bgp_read_packet (struct peer *peer)
2433{
2434 int nbytes;
2435 int readsize;
2436
paul9985f832005-02-09 15:51:56 +00002437 readsize = peer->packet_size - stream_get_endp (peer->ibuf);
paul718e3742002-12-13 20:15:29 +00002438
2439 /* If size is zero then return. */
2440 if (! readsize)
2441 return 0;
2442
2443 /* Read packet from fd. */
Stephen Hemminger35398582010-08-05 10:26:23 -07002444 nbytes = stream_read_try (peer->ibuf, peer->fd, readsize);
paul718e3742002-12-13 20:15:29 +00002445
2446 /* If read byte is smaller than zero then error occured. */
2447 if (nbytes < 0)
2448 {
Stephen Hemminger35398582010-08-05 10:26:23 -07002449 /* Transient error should retry */
2450 if (nbytes == -2)
paul718e3742002-12-13 20:15:29 +00002451 return -1;
2452
2453 plog_err (peer->log, "%s [Error] bgp_read_packet error: %s",
ajs6099b3b2004-11-20 02:06:59 +00002454 peer->host, safe_strerror (errno));
hasso93406d82005-02-02 14:40:33 +00002455
2456 if (peer->status == Established)
2457 {
2458 if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_MODE))
2459 {
2460 peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
2461 SET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
2462 }
2463 else
2464 peer->last_reset = PEER_DOWN_CLOSE_SESSION;
2465 }
2466
paul718e3742002-12-13 20:15:29 +00002467 BGP_EVENT_ADD (peer, TCP_fatal_error);
2468 return -1;
2469 }
2470
2471 /* When read byte is zero : clear bgp peer and return */
2472 if (nbytes == 0)
2473 {
2474 if (BGP_DEBUG (events, EVENTS))
ajs6b514742004-12-08 21:03:23 +00002475 plog_debug (peer->log, "%s [Event] BGP connection closed fd %d",
pauleb821182004-05-01 08:44:08 +00002476 peer->host, peer->fd);
hassoe0701b72004-05-20 09:19:34 +00002477
2478 if (peer->status == Established)
hasso93406d82005-02-02 14:40:33 +00002479 {
2480 if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_MODE))
2481 {
2482 peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
2483 SET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
2484 }
2485 else
2486 peer->last_reset = PEER_DOWN_CLOSE_SESSION;
2487 }
hassoe0701b72004-05-20 09:19:34 +00002488
paul718e3742002-12-13 20:15:29 +00002489 BGP_EVENT_ADD (peer, TCP_connection_closed);
2490 return -1;
2491 }
2492
2493 /* We read partial packet. */
paul9985f832005-02-09 15:51:56 +00002494 if (stream_get_endp (peer->ibuf) != peer->packet_size)
paul718e3742002-12-13 20:15:29 +00002495 return -1;
2496
2497 return 0;
2498}
2499
2500/* Marker check. */
paul94f2b392005-06-28 12:44:16 +00002501static int
paul718e3742002-12-13 20:15:29 +00002502bgp_marker_all_one (struct stream *s, int length)
2503{
2504 int i;
2505
2506 for (i = 0; i < length; i++)
2507 if (s->data[i] != 0xff)
2508 return 0;
2509
2510 return 1;
2511}
2512
Stephen Hemmingerd61c1bb2013-01-04 22:29:23 +00002513/* Recent thread time.
2514 On same clock base as bgp_clock (MONOTONIC)
2515 but can be time of last context switch to bgp_read thread. */
2516static time_t
2517bgp_recent_clock (void)
2518{
2519 return recent_relative_time().tv_sec;
2520}
2521
paul718e3742002-12-13 20:15:29 +00002522/* Starting point of packet process function. */
2523int
2524bgp_read (struct thread *thread)
2525{
2526 int ret;
2527 u_char type = 0;
2528 struct peer *peer;
2529 bgp_size_t size;
2530 char notify_data_length[2];
2531
2532 /* Yes first of all get peer pointer. */
2533 peer = THREAD_ARG (thread);
2534 peer->t_read = NULL;
2535
2536 /* For non-blocking IO check. */
2537 if (peer->status == Connect)
2538 {
Paul Jakma743dd422016-09-30 13:55:47 +01002539 bgp_connect_check (peer);
paul718e3742002-12-13 20:15:29 +00002540 goto done;
2541 }
2542 else
2543 {
pauleb821182004-05-01 08:44:08 +00002544 if (peer->fd < 0)
paul718e3742002-12-13 20:15:29 +00002545 {
pauleb821182004-05-01 08:44:08 +00002546 zlog_err ("bgp_read peer's fd is negative value %d", peer->fd);
paul718e3742002-12-13 20:15:29 +00002547 return -1;
2548 }
pauleb821182004-05-01 08:44:08 +00002549 BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
paul718e3742002-12-13 20:15:29 +00002550 }
2551
2552 /* Read packet header to determine type of the packet */
2553 if (peer->packet_size == 0)
2554 peer->packet_size = BGP_HEADER_SIZE;
2555
paul9985f832005-02-09 15:51:56 +00002556 if (stream_get_endp (peer->ibuf) < BGP_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00002557 {
2558 ret = bgp_read_packet (peer);
2559
2560 /* Header read error or partial read packet. */
2561 if (ret < 0)
2562 goto done;
2563
2564 /* Get size and type. */
paul9985f832005-02-09 15:51:56 +00002565 stream_forward_getp (peer->ibuf, BGP_MARKER_SIZE);
paul718e3742002-12-13 20:15:29 +00002566 memcpy (notify_data_length, stream_pnt (peer->ibuf), 2);
2567 size = stream_getw (peer->ibuf);
2568 type = stream_getc (peer->ibuf);
2569
2570 if (BGP_DEBUG (normal, NORMAL) && type != 2 && type != 0)
ajs6b514742004-12-08 21:03:23 +00002571 zlog_debug ("%s rcv message type %d, length (excl. header) %d",
paul718e3742002-12-13 20:15:29 +00002572 peer->host, type, size - BGP_HEADER_SIZE);
2573
2574 /* Marker check */
paulf5ba3872004-07-09 12:11:31 +00002575 if (((type == BGP_MSG_OPEN) || (type == BGP_MSG_KEEPALIVE))
paul718e3742002-12-13 20:15:29 +00002576 && ! bgp_marker_all_one (peer->ibuf, BGP_MARKER_SIZE))
2577 {
2578 bgp_notify_send (peer,
2579 BGP_NOTIFY_HEADER_ERR,
2580 BGP_NOTIFY_HEADER_NOT_SYNC);
2581 goto done;
2582 }
2583
2584 /* BGP type check. */
2585 if (type != BGP_MSG_OPEN && type != BGP_MSG_UPDATE
2586 && type != BGP_MSG_NOTIFY && type != BGP_MSG_KEEPALIVE
2587 && type != BGP_MSG_ROUTE_REFRESH_NEW
2588 && type != BGP_MSG_ROUTE_REFRESH_OLD
2589 && type != BGP_MSG_CAPABILITY)
2590 {
2591 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00002592 plog_debug (peer->log,
paul718e3742002-12-13 20:15:29 +00002593 "%s unknown message type 0x%02x",
2594 peer->host, type);
2595 bgp_notify_send_with_data (peer,
2596 BGP_NOTIFY_HEADER_ERR,
2597 BGP_NOTIFY_HEADER_BAD_MESTYPE,
2598 &type, 1);
2599 goto done;
2600 }
2601 /* Mimimum packet length check. */
2602 if ((size < BGP_HEADER_SIZE)
2603 || (size > BGP_MAX_PACKET_SIZE)
2604 || (type == BGP_MSG_OPEN && size < BGP_MSG_OPEN_MIN_SIZE)
2605 || (type == BGP_MSG_UPDATE && size < BGP_MSG_UPDATE_MIN_SIZE)
2606 || (type == BGP_MSG_NOTIFY && size < BGP_MSG_NOTIFY_MIN_SIZE)
2607 || (type == BGP_MSG_KEEPALIVE && size != BGP_MSG_KEEPALIVE_MIN_SIZE)
2608 || (type == BGP_MSG_ROUTE_REFRESH_NEW && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE)
2609 || (type == BGP_MSG_ROUTE_REFRESH_OLD && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE)
2610 || (type == BGP_MSG_CAPABILITY && size < BGP_MSG_CAPABILITY_MIN_SIZE))
2611 {
2612 if (BGP_DEBUG (normal, NORMAL))
ajs6b514742004-12-08 21:03:23 +00002613 plog_debug (peer->log,
paul718e3742002-12-13 20:15:29 +00002614 "%s bad message length - %d for %s",
2615 peer->host, size,
2616 type == 128 ? "ROUTE-REFRESH" :
2617 bgp_type_str[(int) type]);
2618 bgp_notify_send_with_data (peer,
2619 BGP_NOTIFY_HEADER_ERR,
2620 BGP_NOTIFY_HEADER_BAD_MESLEN,
hassoc9e52be2004-09-26 16:09:34 +00002621 (u_char *) notify_data_length, 2);
paul718e3742002-12-13 20:15:29 +00002622 goto done;
2623 }
2624
2625 /* Adjust size to message length. */
2626 peer->packet_size = size;
2627 }
2628
2629 ret = bgp_read_packet (peer);
2630 if (ret < 0)
2631 goto done;
2632
2633 /* Get size and type again. */
2634 size = stream_getw_from (peer->ibuf, BGP_MARKER_SIZE);
2635 type = stream_getc_from (peer->ibuf, BGP_MARKER_SIZE + 2);
2636
2637 /* BGP packet dump function. */
2638 bgp_dump_packet (peer, type, peer->ibuf);
2639
2640 size = (peer->packet_size - BGP_HEADER_SIZE);
2641
2642 /* Read rest of the packet and call each sort of packet routine */
2643 switch (type)
2644 {
2645 case BGP_MSG_OPEN:
2646 peer->open_in++;
paulf5ba3872004-07-09 12:11:31 +00002647 bgp_open_receive (peer, size); /* XXX return value ignored! */
paul718e3742002-12-13 20:15:29 +00002648 break;
2649 case BGP_MSG_UPDATE:
Stephen Hemmingerd61c1bb2013-01-04 22:29:23 +00002650 peer->readtime = bgp_recent_clock ();
paul718e3742002-12-13 20:15:29 +00002651 bgp_update_receive (peer, size);
2652 break;
2653 case BGP_MSG_NOTIFY:
2654 bgp_notify_receive (peer, size);
2655 break;
2656 case BGP_MSG_KEEPALIVE:
Stephen Hemmingerd61c1bb2013-01-04 22:29:23 +00002657 peer->readtime = bgp_recent_clock ();
paul718e3742002-12-13 20:15:29 +00002658 bgp_keepalive_receive (peer, size);
2659 break;
2660 case BGP_MSG_ROUTE_REFRESH_NEW:
2661 case BGP_MSG_ROUTE_REFRESH_OLD:
2662 peer->refresh_in++;
2663 bgp_route_refresh_receive (peer, size);
2664 break;
2665 case BGP_MSG_CAPABILITY:
2666 peer->dynamic_cap_in++;
2667 bgp_capability_receive (peer, size);
2668 break;
2669 }
2670
2671 /* Clear input buffer. */
2672 peer->packet_size = 0;
2673 if (peer->ibuf)
2674 stream_reset (peer->ibuf);
2675
2676 done:
2677 if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
2678 {
2679 if (BGP_DEBUG (events, EVENTS))
ajs6b514742004-12-08 21:03:23 +00002680 zlog_debug ("%s [Event] Accepting BGP peer delete", peer->host);
paul718e3742002-12-13 20:15:29 +00002681 peer_delete (peer);
paul718e3742002-12-13 20:15:29 +00002682 }
2683 return 0;
2684}