]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_packet.c
Merge pull request #11183 from opensourcerouting/feature/handle_bgp_gr_notification
[mirror_frr.git] / bgpd / bgp_packet.c
1 /* BGP packet management routine.
2 * Contains utility functions for constructing and consuming BGP messages.
3 * Copyright (C) 2017 Cumulus Networks
4 * Copyright (C) 1999 Kunihiro Ishiguro
5 *
6 * This file is part of GNU Zebra.
7 *
8 * GNU Zebra is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * GNU Zebra is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; see the file COPYING; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <zebra.h>
24 #include <sys/time.h>
25
26 #include "thread.h"
27 #include "stream.h"
28 #include "network.h"
29 #include "prefix.h"
30 #include "command.h"
31 #include "log.h"
32 #include "memory.h"
33 #include "sockunion.h" /* for inet_ntop () */
34 #include "sockopt.h"
35 #include "linklist.h"
36 #include "plist.h"
37 #include "queue.h"
38 #include "filter.h"
39 #include "lib_errors.h"
40
41 #include "bgpd/bgpd.h"
42 #include "bgpd/bgp_table.h"
43 #include "bgpd/bgp_dump.h"
44 #include "bgpd/bgp_bmp.h"
45 #include "bgpd/bgp_attr.h"
46 #include "bgpd/bgp_debug.h"
47 #include "bgpd/bgp_errors.h"
48 #include "bgpd/bgp_fsm.h"
49 #include "bgpd/bgp_route.h"
50 #include "bgpd/bgp_packet.h"
51 #include "bgpd/bgp_open.h"
52 #include "bgpd/bgp_aspath.h"
53 #include "bgpd/bgp_community.h"
54 #include "bgpd/bgp_ecommunity.h"
55 #include "bgpd/bgp_lcommunity.h"
56 #include "bgpd/bgp_network.h"
57 #include "bgpd/bgp_mplsvpn.h"
58 #include "bgpd/bgp_evpn.h"
59 #include "bgpd/bgp_advertise.h"
60 #include "bgpd/bgp_vty.h"
61 #include "bgpd/bgp_updgrp.h"
62 #include "bgpd/bgp_label.h"
63 #include "bgpd/bgp_io.h"
64 #include "bgpd/bgp_keepalives.h"
65 #include "bgpd/bgp_flowspec.h"
66 #include "bgpd/bgp_trace.h"
67
68 DEFINE_HOOK(bgp_packet_dump,
69 (struct peer *peer, uint8_t type, bgp_size_t size,
70 struct stream *s),
71 (peer, type, size, s));
72
73 DEFINE_HOOK(bgp_packet_send,
74 (struct peer *peer, uint8_t type, bgp_size_t size,
75 struct stream *s),
76 (peer, type, size, s));
77
78 /**
79 * Sets marker and type fields for a BGP message.
80 *
81 * @param s the stream containing the packet
82 * @param type the packet type
83 * @return the size of the stream
84 */
85 int bgp_packet_set_marker(struct stream *s, uint8_t type)
86 {
87 int i;
88
89 /* Fill in marker. */
90 for (i = 0; i < BGP_MARKER_SIZE; i++)
91 stream_putc(s, 0xff);
92
93 /* Dummy total length. This field is should be filled in later on. */
94 stream_putw(s, 0);
95
96 /* BGP packet type. */
97 stream_putc(s, type);
98
99 /* Return current stream size. */
100 return stream_get_endp(s);
101 }
102
103 /**
104 * Sets size field for a BGP message.
105 *
106 * Size field is set to the size of the stream passed.
107 *
108 * @param s the stream containing the packet
109 */
110 void bgp_packet_set_size(struct stream *s)
111 {
112 int cp;
113
114 /* Preserve current pointer. */
115 cp = stream_get_endp(s);
116 stream_putw_at(s, BGP_MARKER_SIZE, cp);
117 }
118
119 /*
120 * Push a packet onto the beginning of the peer's output queue.
121 * This function acquires the peer's write mutex before proceeding.
122 */
123 static void bgp_packet_add(struct peer *peer, struct stream *s)
124 {
125 frr_with_mutex(&peer->io_mtx) {
126 stream_fifo_push(peer->obuf, s);
127 }
128 }
129
130 static struct stream *bgp_update_packet_eor(struct peer *peer, afi_t afi,
131 safi_t safi)
132 {
133 struct stream *s;
134 iana_afi_t pkt_afi = IANA_AFI_IPV4;
135 iana_safi_t pkt_safi = IANA_SAFI_UNICAST;
136
137 if (DISABLE_BGP_ANNOUNCE)
138 return NULL;
139
140 if (bgp_debug_neighbor_events(peer))
141 zlog_debug("send End-of-RIB for %s to %s",
142 get_afi_safi_str(afi, safi, false), peer->host);
143
144 s = stream_new(peer->max_packet_size);
145
146 /* Make BGP update packet. */
147 bgp_packet_set_marker(s, BGP_MSG_UPDATE);
148
149 /* Unfeasible Routes Length */
150 stream_putw(s, 0);
151
152 if (afi == AFI_IP && safi == SAFI_UNICAST) {
153 /* Total Path Attribute Length */
154 stream_putw(s, 0);
155 } else {
156 /* Convert AFI, SAFI to values for packet. */
157 bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi);
158
159 /* Total Path Attribute Length */
160 stream_putw(s, 6);
161 stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
162 stream_putc(s, BGP_ATTR_MP_UNREACH_NLRI);
163 stream_putc(s, 3);
164 stream_putw(s, pkt_afi);
165 stream_putc(s, pkt_safi);
166 }
167
168 bgp_packet_set_size(s);
169 return s;
170 }
171
172 /* Called when there is a change in the EOR(implicit or explicit) status of a
173 * peer. Ends the update-delay if all expected peers are done with EORs. */
174 void bgp_check_update_delay(struct bgp *bgp)
175 {
176 struct listnode *node, *nnode;
177 struct peer *peer = NULL;
178
179 if (bgp_debug_neighbor_events(peer))
180 zlog_debug("Checking update delay, T: %d R: %d I:%d E: %d",
181 bgp->established, bgp->restarted_peers,
182 bgp->implicit_eors, bgp->explicit_eors);
183
184 if (bgp->established
185 <= bgp->restarted_peers + bgp->implicit_eors + bgp->explicit_eors) {
186 /*
187 * This is an extra sanity check to make sure we wait for all
188 * the eligible configured peers. This check is performed if
189 * establish wait timer is on, or establish wait option is not
190 * given with the update-delay command
191 */
192 if (bgp->t_establish_wait
193 || (bgp->v_establish_wait == bgp->v_update_delay))
194 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
195 if (CHECK_FLAG(peer->flags,
196 PEER_FLAG_CONFIG_NODE)
197 && !CHECK_FLAG(peer->flags,
198 PEER_FLAG_SHUTDOWN)
199 && !CHECK_FLAG(peer->bgp->flags,
200 BGP_FLAG_SHUTDOWN)
201 && !peer->update_delay_over) {
202 if (bgp_debug_neighbor_events(peer))
203 zlog_debug(
204 " Peer %s pending, continuing read-only mode",
205 peer->host);
206 return;
207 }
208 }
209
210 zlog_info(
211 "Update delay ended, restarted: %d, EORs implicit: %d, explicit: %d",
212 bgp->restarted_peers, bgp->implicit_eors,
213 bgp->explicit_eors);
214 bgp_update_delay_end(bgp);
215 }
216 }
217
218 /*
219 * Called if peer is known to have restarted. The restart-state bit in
220 * Graceful-Restart capability is used for that
221 */
222 void bgp_update_restarted_peers(struct peer *peer)
223 {
224 if (!bgp_update_delay_active(peer->bgp))
225 return; /* BGP update delay has ended */
226 if (peer->update_delay_over)
227 return; /* This peer has already been considered */
228
229 if (bgp_debug_neighbor_events(peer))
230 zlog_debug("Peer %s: Checking restarted", peer->host);
231
232 if (peer_established(peer)) {
233 peer->update_delay_over = 1;
234 peer->bgp->restarted_peers++;
235 bgp_check_update_delay(peer->bgp);
236 }
237 }
238
239 /*
240 * Called as peer receives a keep-alive. Determines if this occurence can be
241 * taken as an implicit EOR for this peer.
242 * NOTE: The very first keep-alive after the Established state of a peer is
243 * considered implicit EOR for the update-delay purposes
244 */
245 void bgp_update_implicit_eors(struct peer *peer)
246 {
247 if (!bgp_update_delay_active(peer->bgp))
248 return; /* BGP update delay has ended */
249 if (peer->update_delay_over)
250 return; /* This peer has already been considered */
251
252 if (bgp_debug_neighbor_events(peer))
253 zlog_debug("Peer %s: Checking implicit EORs", peer->host);
254
255 if (peer_established(peer)) {
256 peer->update_delay_over = 1;
257 peer->bgp->implicit_eors++;
258 bgp_check_update_delay(peer->bgp);
259 }
260 }
261
262 /*
263 * Should be called only when there is a change in the EOR_RECEIVED status
264 * for any afi/safi on a peer.
265 */
266 static void bgp_update_explicit_eors(struct peer *peer)
267 {
268 afi_t afi;
269 safi_t safi;
270
271 if (!bgp_update_delay_active(peer->bgp))
272 return; /* BGP update delay has ended */
273 if (peer->update_delay_over)
274 return; /* This peer has already been considered */
275
276 if (bgp_debug_neighbor_events(peer))
277 zlog_debug("Peer %s: Checking explicit EORs", peer->host);
278
279 FOREACH_AFI_SAFI (afi, safi) {
280 if (peer->afc_nego[afi][safi]
281 && !CHECK_FLAG(peer->af_sflags[afi][safi],
282 PEER_STATUS_EOR_RECEIVED)) {
283 if (bgp_debug_neighbor_events(peer))
284 zlog_debug(
285 " afi %d safi %d didn't receive EOR",
286 afi, safi);
287 return;
288 }
289 }
290
291 peer->update_delay_over = 1;
292 peer->bgp->explicit_eors++;
293 bgp_check_update_delay(peer->bgp);
294 }
295
296 /**
297 * Frontend for NLRI parsing, to fan-out to AFI/SAFI specific parsers.
298 *
299 * mp_withdraw, if set, is used to nullify attr structure on most of the
300 * calling safi function and for evpn, passed as parameter
301 */
302 int bgp_nlri_parse(struct peer *peer, struct attr *attr,
303 struct bgp_nlri *packet, int mp_withdraw)
304 {
305 switch (packet->safi) {
306 case SAFI_UNICAST:
307 case SAFI_MULTICAST:
308 return bgp_nlri_parse_ip(peer, mp_withdraw ? NULL : attr,
309 packet);
310 case SAFI_LABELED_UNICAST:
311 return bgp_nlri_parse_label(peer, mp_withdraw ? NULL : attr,
312 packet);
313 case SAFI_MPLS_VPN:
314 return bgp_nlri_parse_vpn(peer, mp_withdraw ? NULL : attr,
315 packet);
316 case SAFI_EVPN:
317 return bgp_nlri_parse_evpn(peer, attr, packet, mp_withdraw);
318 case SAFI_FLOWSPEC:
319 return bgp_nlri_parse_flowspec(peer, attr, packet, mp_withdraw);
320 }
321 return BGP_NLRI_PARSE_ERROR;
322 }
323
324 /*
325 * Checks a variety of conditions to determine whether the peer needs to be
326 * rescheduled for packet generation again, and does so if necessary.
327 *
328 * @param peer to check for rescheduling
329 */
330 static void bgp_write_proceed_actions(struct peer *peer)
331 {
332 afi_t afi;
333 safi_t safi;
334 struct peer_af *paf;
335 struct bpacket *next_pkt;
336 struct update_subgroup *subgrp;
337 enum bgp_af_index index;
338
339 for (index = BGP_AF_START; index < BGP_AF_MAX; index++) {
340 paf = peer->peer_af_array[index];
341 if (!paf)
342 continue;
343
344 subgrp = paf->subgroup;
345 if (!subgrp)
346 continue;
347
348 next_pkt = paf->next_pkt_to_send;
349 if (next_pkt && next_pkt->buffer) {
350 BGP_TIMER_ON(peer->t_generate_updgrp_packets,
351 bgp_generate_updgrp_packets, 0);
352 return;
353 }
354
355 /* No packets readily available for AFI/SAFI, are there
356 * subgroup packets
357 * that need to be generated? */
358 if (bpacket_queue_is_full(SUBGRP_INST(subgrp),
359 SUBGRP_PKTQ(subgrp))
360 || subgroup_packets_to_build(subgrp)) {
361 BGP_TIMER_ON(peer->t_generate_updgrp_packets,
362 bgp_generate_updgrp_packets, 0);
363 return;
364 }
365
366 afi = paf->afi;
367 safi = paf->safi;
368
369 /* No packets to send, see if EOR is pending */
370 if (CHECK_FLAG(peer->cap, PEER_CAP_RESTART_RCV)) {
371 if (!subgrp->t_coalesce && peer->afc_nego[afi][safi]
372 && peer->synctime
373 && !CHECK_FLAG(peer->af_sflags[afi][safi],
374 PEER_STATUS_EOR_SEND)
375 && safi != SAFI_MPLS_VPN) {
376 BGP_TIMER_ON(peer->t_generate_updgrp_packets,
377 bgp_generate_updgrp_packets, 0);
378 return;
379 }
380 }
381 }
382 }
383
384 /*
385 * Generate advertisement information (withdraws, updates, EOR) from each
386 * update group a peer belongs to, encode this information into packets, and
387 * enqueue the packets onto the peer's output buffer.
388 */
389 void bgp_generate_updgrp_packets(struct thread *thread)
390 {
391 struct peer *peer = THREAD_ARG(thread);
392
393 struct stream *s;
394 struct peer_af *paf;
395 struct bpacket *next_pkt;
396 uint32_t wpq;
397 uint32_t generated = 0;
398 afi_t afi;
399 safi_t safi;
400
401 wpq = atomic_load_explicit(&peer->bgp->wpkt_quanta,
402 memory_order_relaxed);
403
404 /*
405 * The code beyond this part deals with update packets, proceed only
406 * if peer is Established and updates are not on hold (as part of
407 * update-delay processing).
408 */
409 if (!peer_established(peer))
410 return;
411
412 if ((peer->bgp->main_peers_update_hold)
413 || bgp_update_delay_active(peer->bgp))
414 return;
415
416 if (peer->t_routeadv)
417 return;
418
419 do {
420 enum bgp_af_index index;
421
422 s = NULL;
423 for (index = BGP_AF_START; index < BGP_AF_MAX; index++) {
424 paf = peer->peer_af_array[index];
425 if (!paf || !PAF_SUBGRP(paf))
426 continue;
427
428 afi = paf->afi;
429 safi = paf->safi;
430 next_pkt = paf->next_pkt_to_send;
431
432 /*
433 * Try to generate a packet for the peer if we are at
434 * the end of the list. Always try to push out
435 * WITHDRAWs first.
436 */
437 if (!next_pkt || !next_pkt->buffer) {
438 next_pkt = subgroup_withdraw_packet(
439 PAF_SUBGRP(paf));
440 if (!next_pkt || !next_pkt->buffer)
441 subgroup_update_packet(PAF_SUBGRP(paf));
442 next_pkt = paf->next_pkt_to_send;
443 }
444
445 /*
446 * If we still don't have a packet to send to the peer,
447 * then try to find out out if we have to send eor or
448 * if not, skip to the next AFI, SAFI. Don't send the
449 * EOR prematurely; if the subgroup's coalesce timer is
450 * running, the adjacency-out structure is not created
451 * yet.
452 */
453 if (!next_pkt || !next_pkt->buffer) {
454 if (!paf->t_announce_route) {
455 /* Make sure we supress BGP UPDATES
456 * for normal processing later again.
457 */
458 UNSET_FLAG(paf->subgroup->sflags,
459 SUBGRP_STATUS_FORCE_UPDATES);
460
461 /* If route-refresh BoRR message was
462 * already sent and we are done with
463 * re-announcing tables for a decent
464 * afi/safi, we ready to send
465 * EoRR request.
466 */
467 if (CHECK_FLAG(
468 peer->af_sflags[afi][safi],
469 PEER_STATUS_BORR_SEND)) {
470 bgp_route_refresh_send(
471 peer, afi, safi, 0, 0,
472 0,
473 BGP_ROUTE_REFRESH_EORR);
474
475 SET_FLAG(peer->af_sflags[afi]
476 [safi],
477 PEER_STATUS_EORR_SEND);
478 UNSET_FLAG(
479 peer->af_sflags[afi]
480 [safi],
481 PEER_STATUS_BORR_SEND);
482
483 if (bgp_debug_neighbor_events(
484 peer))
485 zlog_debug(
486 "%pBP sending route-refresh (EoRR) for %s/%s",
487 peer,
488 afi2str(afi),
489 safi2str(safi));
490 }
491 }
492
493 if (CHECK_FLAG(peer->cap,
494 PEER_CAP_RESTART_RCV)) {
495 if (!(PAF_SUBGRP(paf))->t_coalesce
496 && peer->afc_nego[afi][safi]
497 && peer->synctime
498 && !CHECK_FLAG(
499 peer->af_sflags[afi][safi],
500 PEER_STATUS_EOR_SEND)) {
501 /* If EOR is disabled,
502 * the message is not sent
503 */
504 if (BGP_SEND_EOR(peer->bgp, afi,
505 safi)) {
506 SET_FLAG(
507 peer->af_sflags
508 [afi]
509 [safi],
510 PEER_STATUS_EOR_SEND);
511
512 /* Update EOR
513 * send time
514 */
515 peer->eor_stime[afi]
516 [safi] =
517 monotime(NULL);
518
519 BGP_UPDATE_EOR_PKT(
520 peer, afi, safi,
521 s);
522 }
523 }
524 }
525 continue;
526 }
527
528 /* Update packet send time */
529 peer->pkt_stime[afi][safi] = monotime(NULL);
530
531 /* Found a packet template to send, overwrite
532 * packet with appropriate attributes from peer
533 * and advance peer */
534 s = bpacket_reformat_for_peer(next_pkt, paf);
535 bgp_packet_add(peer, s);
536 bpacket_queue_advance_peer(paf);
537 }
538 } while (s && (++generated < wpq));
539
540 if (generated)
541 bgp_writes_on(peer);
542
543 bgp_write_proceed_actions(peer);
544 }
545
546 /*
547 * Creates a BGP Keepalive packet and appends it to the peer's output queue.
548 */
549 void bgp_keepalive_send(struct peer *peer)
550 {
551 struct stream *s;
552
553 s = stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE);
554
555 /* Make keepalive packet. */
556 bgp_packet_set_marker(s, BGP_MSG_KEEPALIVE);
557
558 /* Set packet size. */
559 bgp_packet_set_size(s);
560
561 /* Dump packet if debug option is set. */
562 /* bgp_packet_dump (s); */
563
564 if (bgp_debug_keepalive(peer))
565 zlog_debug("%s sending KEEPALIVE", peer->host);
566
567 /* Add packet to the peer. */
568 bgp_packet_add(peer, s);
569
570 bgp_writes_on(peer);
571 }
572
573 /*
574 * Creates a BGP Open packet and appends it to the peer's output queue.
575 * Sets capabilities as necessary.
576 */
577 void bgp_open_send(struct peer *peer)
578 {
579 struct stream *s;
580 uint16_t send_holdtime;
581 as_t local_as;
582
583 if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER))
584 send_holdtime = peer->holdtime;
585 else
586 send_holdtime = peer->bgp->default_holdtime;
587
588 /* local-as Change */
589 if (peer->change_local_as)
590 local_as = peer->change_local_as;
591 else
592 local_as = peer->local_as;
593
594 s = stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE);
595
596 /* Make open packet. */
597 bgp_packet_set_marker(s, BGP_MSG_OPEN);
598
599 /* Set open packet values. */
600 stream_putc(s, BGP_VERSION_4); /* BGP version */
601 stream_putw(s, (local_as <= BGP_AS_MAX) ? (uint16_t)local_as
602 : BGP_AS_TRANS);
603 stream_putw(s, send_holdtime); /* Hold Time */
604 stream_put_in_addr(s, &peer->local_id); /* BGP Identifier */
605
606 /* Set capabilities */
607 if (CHECK_FLAG(peer->flags, PEER_FLAG_EXTENDED_OPT_PARAMS)) {
608 (void)bgp_open_capability(s, peer, true);
609 } else {
610 struct stream *tmp = stream_new(STREAM_SIZE(s));
611
612 stream_copy(tmp, s);
613 if (bgp_open_capability(tmp, peer, false)
614 > BGP_OPEN_NON_EXT_OPT_LEN) {
615 stream_free(tmp);
616 (void)bgp_open_capability(s, peer, true);
617 } else {
618 stream_copy(s, tmp);
619 stream_free(tmp);
620 }
621 }
622
623 /* Set BGP packet length. */
624 bgp_packet_set_size(s);
625
626 if (bgp_debug_neighbor_events(peer))
627 zlog_debug(
628 "%s sending OPEN, version %d, my as %u, holdtime %d, id %pI4",
629 peer->host, BGP_VERSION_4, local_as, send_holdtime,
630 &peer->local_id);
631
632 /* Dump packet if debug option is set. */
633 /* bgp_packet_dump (s); */
634 hook_call(bgp_packet_send, peer, BGP_MSG_OPEN, stream_get_endp(s), s);
635
636 /* Add packet to the peer. */
637 bgp_packet_add(peer, s);
638
639 bgp_writes_on(peer);
640 }
641
642 /*
643 * Writes NOTIFICATION message directly to a peer socket without waiting for
644 * the I/O thread.
645 *
646 * There must be exactly one stream on the peer->obuf FIFO, and the data within
647 * this stream must match the format of a BGP NOTIFICATION message.
648 * Transmission is best-effort.
649 *
650 * @requires peer->io_mtx
651 * @param peer
652 * @return 0
653 */
654 static void bgp_write_notify(struct peer *peer)
655 {
656 int ret, val;
657 uint8_t type;
658 struct stream *s;
659
660 /* There should be at least one packet. */
661 s = stream_fifo_pop(peer->obuf);
662
663 if (!s)
664 return;
665
666 assert(stream_get_endp(s) >= BGP_HEADER_SIZE);
667
668 /*
669 * socket is in nonblocking mode, if we can't deliver the NOTIFY, well,
670 * we only care about getting a clean shutdown at this point.
671 */
672 ret = write(peer->fd, STREAM_DATA(s), stream_get_endp(s));
673
674 /*
675 * only connection reset/close gets counted as TCP_fatal_error, failure
676 * to write the entire NOTIFY doesn't get different FSM treatment
677 */
678 if (ret <= 0) {
679 stream_free(s);
680 BGP_EVENT_ADD(peer, TCP_fatal_error);
681 return;
682 }
683
684 /* Disable Nagle, make NOTIFY packet go out right away */
685 val = 1;
686 (void)setsockopt(peer->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val,
687 sizeof(val));
688
689 /* Retrieve BGP packet type. */
690 stream_set_getp(s, BGP_MARKER_SIZE + 2);
691 type = stream_getc(s);
692
693 assert(type == BGP_MSG_NOTIFY);
694
695 /* Type should be notify. */
696 atomic_fetch_add_explicit(&peer->notify_out, 1, memory_order_relaxed);
697 peer->notify_out++;
698
699 /* Double start timer. */
700 peer->v_start *= 2;
701
702 /* Overflow check. */
703 if (peer->v_start >= (60 * 2))
704 peer->v_start = (60 * 2);
705
706 /*
707 * Handle Graceful Restart case where the state changes to
708 * Connect instead of Idle
709 */
710 BGP_EVENT_ADD(peer, BGP_Stop);
711
712 stream_free(s);
713 }
714
715 /*
716 * Encapsulate an original BGP CEASE Notification into Hard Reset
717 */
718 static uint8_t *bgp_notify_encapsulate_hard_reset(uint8_t code, uint8_t subcode,
719 uint8_t *data, size_t datalen)
720 {
721 uint8_t *message = XCALLOC(MTYPE_BGP_NOTIFICATION, datalen + 2);
722
723 /* ErrCode */
724 message[0] = code;
725 /* Subcode */
726 message[1] = subcode;
727 /* Data */
728 if (datalen)
729 memcpy(message + 2, data, datalen);
730
731 return message;
732 }
733
734 /*
735 * Decapsulate an original BGP CEASE Notification from Hard Reset
736 */
737 struct bgp_notify bgp_notify_decapsulate_hard_reset(struct bgp_notify *notify)
738 {
739 struct bgp_notify bn = {};
740
741 bn.code = notify->raw_data[0];
742 bn.subcode = notify->raw_data[1];
743 bn.length = notify->length - 2;
744
745 bn.raw_data = XCALLOC(MTYPE_BGP_NOTIFICATION, bn.length);
746 memcpy(bn.raw_data, notify->raw_data + 2, bn.length);
747
748 return bn;
749 }
750
751 /* Check if Graceful-Restart N-bit is exchanged */
752 bool bgp_has_graceful_restart_notification(struct peer *peer)
753 {
754 return CHECK_FLAG(peer->cap, PEER_CAP_GRACEFUL_RESTART_N_BIT_RCV) &&
755 CHECK_FLAG(peer->cap, PEER_CAP_GRACEFUL_RESTART_N_BIT_ADV);
756 }
757
758 /*
759 * Check if to send BGP CEASE Notification/Hard Reset?
760 */
761 bool bgp_notify_send_hard_reset(struct peer *peer, uint8_t code,
762 uint8_t subcode)
763 {
764 /* When the "N" bit has been exchanged, a Hard Reset message is used to
765 * indicate to the peer that the session is to be fully terminated.
766 */
767 if (!bgp_has_graceful_restart_notification(peer))
768 return false;
769
770 /*
771 * https://datatracker.ietf.org/doc/html/rfc8538#section-5.1
772 */
773 if (code == BGP_NOTIFY_CEASE) {
774 switch (subcode) {
775 case BGP_NOTIFY_CEASE_MAX_PREFIX:
776 case BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN:
777 case BGP_NOTIFY_CEASE_PEER_UNCONFIG:
778 case BGP_NOTIFY_CEASE_HARD_RESET:
779 return true;
780 case BGP_NOTIFY_CEASE_ADMIN_RESET:
781 /* Provide user control:
782 * `bgp hard-adminstrative-reset`
783 */
784 if (CHECK_FLAG(peer->bgp->flags,
785 BGP_FLAG_HARD_ADMIN_RESET))
786 return true;
787 else
788 return false;
789 default:
790 break;
791 }
792 }
793
794 return false;
795 }
796
797 /*
798 * Check if received BGP CEASE Notification/Hard Reset?
799 */
800 bool bgp_notify_received_hard_reset(struct peer *peer, uint8_t code,
801 uint8_t subcode)
802 {
803 /* When the "N" bit has been exchanged, a Hard Reset message is used to
804 * indicate to the peer that the session is to be fully terminated.
805 */
806 if (!bgp_has_graceful_restart_notification(peer))
807 return false;
808
809 if (code == BGP_NOTIFY_CEASE && subcode == BGP_NOTIFY_CEASE_HARD_RESET)
810 return true;
811
812 return false;
813 }
814
815 /*
816 * Creates a BGP Notify and appends it to the peer's output queue.
817 *
818 * This function attempts to write the packet from the thread it is called
819 * from, to ensure the packet gets out ASAP.
820 *
821 * This function may be called from multiple threads. Since the function
822 * modifies I/O buffer(s) in the peer, these are locked for the duration of the
823 * call to prevent tampering from other threads.
824 *
825 * Delivery of the NOTIFICATION is attempted once and is best-effort. After
826 * return, the peer structure *must* be reset; no assumptions about session
827 * state are valid.
828 *
829 * @param peer
830 * @param code BGP error code
831 * @param sub_code BGP error subcode
832 * @param data Data portion
833 * @param datalen length of data portion
834 */
835 void bgp_notify_send_with_data(struct peer *peer, uint8_t code,
836 uint8_t sub_code, uint8_t *data, size_t datalen)
837 {
838 struct stream *s;
839 bool hard_reset = bgp_notify_send_hard_reset(peer, code, sub_code);
840
841 /* Lock I/O mutex to prevent other threads from pushing packets */
842 frr_mutex_lock_autounlock(&peer->io_mtx);
843 /* ============================================== */
844
845 /* Allocate new stream. */
846 s = stream_new(peer->max_packet_size);
847
848 /* Make notify packet. */
849 bgp_packet_set_marker(s, BGP_MSG_NOTIFY);
850
851 /* Check if we should send Hard Reset Notification or not */
852 if (hard_reset) {
853 uint8_t *hard_reset_message = bgp_notify_encapsulate_hard_reset(
854 code, sub_code, data, datalen);
855
856 /* Hard Reset encapsulates another NOTIFICATION message
857 * in its data portion.
858 */
859 stream_putc(s, BGP_NOTIFY_CEASE);
860 stream_putc(s, BGP_NOTIFY_CEASE_HARD_RESET);
861 stream_write(s, hard_reset_message, datalen + 2);
862
863 XFREE(MTYPE_BGP_NOTIFICATION, hard_reset_message);
864 } else {
865 stream_putc(s, code);
866 stream_putc(s, sub_code);
867 if (data)
868 stream_write(s, data, datalen);
869 }
870
871 /* Set BGP packet length. */
872 bgp_packet_set_size(s);
873
874 /* wipe output buffer */
875 stream_fifo_clean(peer->obuf);
876
877 /*
878 * If possible, store last packet for debugging purposes. This check is
879 * in place because we are sometimes called with a doppelganger peer,
880 * who tends to have a plethora of fields nulled out.
881 */
882 if (peer->curr) {
883 size_t packetsize = stream_get_endp(peer->curr);
884 assert(packetsize <= peer->max_packet_size);
885 memcpy(peer->last_reset_cause, peer->curr->data, packetsize);
886 peer->last_reset_cause_size = packetsize;
887 }
888
889 /* For debug */
890 {
891 struct bgp_notify bgp_notify;
892 int first = 0;
893 int i;
894 char c[4];
895
896 bgp_notify.code = code;
897 bgp_notify.subcode = sub_code;
898 bgp_notify.data = NULL;
899 bgp_notify.length = datalen;
900 bgp_notify.raw_data = data;
901
902 peer->notify.code = bgp_notify.code;
903 peer->notify.subcode = bgp_notify.subcode;
904
905 if (bgp_notify.length && data) {
906 bgp_notify.data =
907 XMALLOC(MTYPE_TMP, bgp_notify.length * 3);
908 for (i = 0; i < bgp_notify.length; i++)
909 if (first) {
910 snprintf(c, sizeof(c), " %02x",
911 data[i]);
912
913 strlcat(bgp_notify.data, c,
914 bgp_notify.length);
915
916 } else {
917 first = 1;
918 snprintf(c, sizeof(c), "%02x", data[i]);
919
920 strlcpy(bgp_notify.data, c,
921 bgp_notify.length);
922 }
923 }
924 bgp_notify_print(peer, &bgp_notify, "sending", hard_reset);
925
926 if (bgp_notify.data) {
927 XFREE(MTYPE_TMP, bgp_notify.data);
928 bgp_notify.length = 0;
929 }
930 }
931
932 /* peer reset cause */
933 if (code == BGP_NOTIFY_CEASE) {
934 if (sub_code == BGP_NOTIFY_CEASE_ADMIN_RESET)
935 peer->last_reset = PEER_DOWN_USER_RESET;
936 else if (sub_code == BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN)
937 peer->last_reset = PEER_DOWN_USER_SHUTDOWN;
938 else
939 peer->last_reset = PEER_DOWN_NOTIFY_SEND;
940 } else
941 peer->last_reset = PEER_DOWN_NOTIFY_SEND;
942
943 /* Add packet to peer's output queue */
944 stream_fifo_push(peer->obuf, s);
945
946 bgp_peer_gr_flags_update(peer);
947 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp,
948 peer->bgp->peer);
949
950 bgp_write_notify(peer);
951 }
952
953 /*
954 * Creates a BGP Notify and appends it to the peer's output queue.
955 *
956 * This function attempts to write the packet from the thread it is called
957 * from, to ensure the packet gets out ASAP.
958 *
959 * @param peer
960 * @param code BGP error code
961 * @param sub_code BGP error subcode
962 */
963 void bgp_notify_send(struct peer *peer, uint8_t code, uint8_t sub_code)
964 {
965 bgp_notify_send_with_data(peer, code, sub_code, NULL, 0);
966 }
967
968 /*
969 * Creates BGP Route Refresh packet and appends it to the peer's output queue.
970 *
971 * @param peer
972 * @param afi Address Family Identifier
973 * @param safi Subsequent Address Family Identifier
974 * @param orf_type Outbound Route Filtering type
975 * @param when_to_refresh Whether to refresh immediately or defer
976 * @param remove Whether to remove ORF for specified AFI/SAFI
977 */
978 void bgp_route_refresh_send(struct peer *peer, afi_t afi, safi_t safi,
979 uint8_t orf_type, uint8_t when_to_refresh,
980 int remove, uint8_t subtype)
981 {
982 struct stream *s;
983 struct bgp_filter *filter;
984 int orf_refresh = 0;
985 iana_afi_t pkt_afi = IANA_AFI_IPV4;
986 iana_safi_t pkt_safi = IANA_SAFI_UNICAST;
987
988 if (DISABLE_BGP_ANNOUNCE)
989 return;
990
991 filter = &peer->filter[afi][safi];
992
993 /* Convert AFI, SAFI to values for packet. */
994 bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi);
995
996 s = stream_new(peer->max_packet_size);
997
998 /* Make BGP update packet. */
999 if (CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_NEW_RCV))
1000 bgp_packet_set_marker(s, BGP_MSG_ROUTE_REFRESH_NEW);
1001 else
1002 bgp_packet_set_marker(s, BGP_MSG_ROUTE_REFRESH_OLD);
1003
1004 /* Encode Route Refresh message. */
1005 stream_putw(s, pkt_afi);
1006 if (subtype)
1007 stream_putc(s, subtype);
1008 else
1009 stream_putc(s, 0);
1010 stream_putc(s, pkt_safi);
1011
1012 if (orf_type == ORF_TYPE_PREFIX || orf_type == ORF_TYPE_PREFIX_OLD)
1013 if (remove || filter->plist[FILTER_IN].plist) {
1014 uint16_t orf_len;
1015 unsigned long orfp;
1016
1017 orf_refresh = 1;
1018 stream_putc(s, when_to_refresh);
1019 stream_putc(s, orf_type);
1020 orfp = stream_get_endp(s);
1021 stream_putw(s, 0);
1022
1023 if (remove) {
1024 UNSET_FLAG(peer->af_sflags[afi][safi],
1025 PEER_STATUS_ORF_PREFIX_SEND);
1026 stream_putc(s, ORF_COMMON_PART_REMOVE_ALL);
1027 if (bgp_debug_neighbor_events(peer))
1028 zlog_debug(
1029 "%pBP sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %s/%s",
1030 peer, orf_type,
1031 (when_to_refresh ==
1032 REFRESH_DEFER
1033 ? "defer"
1034 : "immediate"),
1035 iana_afi2str(pkt_afi),
1036 iana_safi2str(pkt_safi));
1037 } else {
1038 SET_FLAG(peer->af_sflags[afi][safi],
1039 PEER_STATUS_ORF_PREFIX_SEND);
1040 prefix_bgp_orf_entry(
1041 s, filter->plist[FILTER_IN].plist,
1042 ORF_COMMON_PART_ADD,
1043 ORF_COMMON_PART_PERMIT,
1044 ORF_COMMON_PART_DENY);
1045 if (bgp_debug_neighbor_events(peer))
1046 zlog_debug(
1047 "%pBP sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %s/%s",
1048 peer, orf_type,
1049 (when_to_refresh ==
1050 REFRESH_DEFER
1051 ? "defer"
1052 : "immediate"),
1053 iana_afi2str(pkt_afi),
1054 iana_safi2str(pkt_safi));
1055 }
1056
1057 /* Total ORF Entry Len. */
1058 orf_len = stream_get_endp(s) - orfp - 2;
1059 stream_putw_at(s, orfp, orf_len);
1060 }
1061
1062 /* Set packet size. */
1063 bgp_packet_set_size(s);
1064
1065 if (bgp_debug_neighbor_events(peer)) {
1066 if (!orf_refresh)
1067 zlog_debug(
1068 "%pBP sending REFRESH_REQ for afi/safi: %s/%s",
1069 peer, iana_afi2str(pkt_afi),
1070 iana_safi2str(pkt_safi));
1071 }
1072
1073 /* Add packet to the peer. */
1074 bgp_packet_add(peer, s);
1075
1076 bgp_writes_on(peer);
1077 }
1078
1079 /*
1080 * Create a BGP Capability packet and append it to the peer's output queue.
1081 *
1082 * @param peer
1083 * @param afi Address Family Identifier
1084 * @param safi Subsequent Address Family Identifier
1085 * @param capability_code BGP Capability Code
1086 * @param action Set or Remove capability
1087 */
1088 void bgp_capability_send(struct peer *peer, afi_t afi, safi_t safi,
1089 int capability_code, int action)
1090 {
1091 struct stream *s;
1092 iana_afi_t pkt_afi = IANA_AFI_IPV4;
1093 iana_safi_t pkt_safi = IANA_SAFI_UNICAST;
1094
1095 /* Convert AFI, SAFI to values for packet. */
1096 bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi);
1097
1098 s = stream_new(peer->max_packet_size);
1099
1100 /* Make BGP update packet. */
1101 bgp_packet_set_marker(s, BGP_MSG_CAPABILITY);
1102
1103 /* Encode MP_EXT capability. */
1104 if (capability_code == CAPABILITY_CODE_MP) {
1105 stream_putc(s, action);
1106 stream_putc(s, CAPABILITY_CODE_MP);
1107 stream_putc(s, CAPABILITY_CODE_MP_LEN);
1108 stream_putw(s, pkt_afi);
1109 stream_putc(s, 0);
1110 stream_putc(s, pkt_safi);
1111
1112 if (bgp_debug_neighbor_events(peer))
1113 zlog_debug(
1114 "%pBP sending CAPABILITY has %s MP_EXT CAP for afi/safi: %s/%s",
1115 peer,
1116 action == CAPABILITY_ACTION_SET ? "Advertising"
1117 : "Removing",
1118 iana_afi2str(pkt_afi), iana_safi2str(pkt_safi));
1119 }
1120
1121 /* Set packet size. */
1122 bgp_packet_set_size(s);
1123
1124 /* Add packet to the peer. */
1125 bgp_packet_add(peer, s);
1126
1127 bgp_writes_on(peer);
1128 }
1129
1130 /* RFC1771 6.8 Connection collision detection. */
1131 static int bgp_collision_detect(struct peer *new, struct in_addr remote_id)
1132 {
1133 struct peer *peer;
1134
1135 /*
1136 * Upon receipt of an OPEN message, the local system must examine
1137 * all of its connections that are in the OpenConfirm state. A BGP
1138 * speaker may also examine connections in an OpenSent state if it
1139 * knows the BGP Identifier of the peer by means outside of the
1140 * protocol. If among these connections there is a connection to a
1141 * remote BGP speaker whose BGP Identifier equals the one in the
1142 * OPEN message, then the local system performs the following
1143 * collision resolution procedure:
1144 */
1145 peer = new->doppelganger;
1146 if (peer == NULL)
1147 return 0;
1148
1149 /*
1150 * Do not accept the new connection in Established or Clearing
1151 * states. Note that a peer GR is handled by closing the existing
1152 * connection upon receipt of new one.
1153 */
1154 if (peer_established(peer) || peer->status == Clearing) {
1155 bgp_notify_send(new, BGP_NOTIFY_CEASE,
1156 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1157 return -1;
1158 }
1159
1160 if ((peer->status != OpenConfirm) && (peer->status != OpenSent))
1161 return 0;
1162
1163 /*
1164 * 1. The BGP Identifier of the local system is
1165 * compared to the BGP Identifier of the remote
1166 * system (as specified in the OPEN message).
1167 *
1168 * If the BGP Identifiers of the peers
1169 * involved in the connection collision
1170 * are identical, then the connection
1171 * initiated by the BGP speaker with the
1172 * larger AS number is preserved.
1173 */
1174 if (ntohl(peer->local_id.s_addr) < ntohl(remote_id.s_addr)
1175 || (ntohl(peer->local_id.s_addr) == ntohl(remote_id.s_addr)
1176 && peer->local_as < peer->as))
1177 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) {
1178 /*
1179 * 2. If the value of the local BGP
1180 * Identifier is less than the remote one,
1181 * the local system closes BGP connection
1182 * that already exists (the one that is
1183 * already in the OpenConfirm state),
1184 * and accepts BGP connection initiated by
1185 * the remote system.
1186 */
1187 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
1188 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1189 return 1;
1190 } else {
1191 bgp_notify_send(new, BGP_NOTIFY_CEASE,
1192 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1193 return -1;
1194 }
1195 else {
1196 if (ntohl(peer->local_id.s_addr) == ntohl(remote_id.s_addr)
1197 && peer->local_as == peer->as)
1198 flog_err(EC_BGP_ROUTER_ID_SAME,
1199 "Peer's router-id %pI4 is the same as ours",
1200 &remote_id);
1201
1202 /*
1203 * 3. Otherwise, the local system closes newly
1204 * created BGP connection (the one associated with the
1205 * newly received OPEN message), and continues to use
1206 * the existing one (the one that is already in the
1207 * OpenConfirm state).
1208 */
1209 if (CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) {
1210 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
1211 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1212 return 1;
1213 } else {
1214 bgp_notify_send(new, BGP_NOTIFY_CEASE,
1215 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1216 return -1;
1217 }
1218 }
1219 }
1220
1221 /* Packet processing routines ---------------------------------------------- */
1222 /*
1223 * This is a family of functions designed to be called from
1224 * bgp_process_packet(). These functions all share similar behavior and should
1225 * adhere to the following invariants and restrictions:
1226 *
1227 * Return codes
1228 * ------------
1229 * The return code of any one of those functions should be one of the FSM event
1230 * codes specified in bgpd.h. If a NOTIFY was sent, this event code MUST be
1231 * BGP_Stop. Otherwise, the code SHOULD correspond to the function's expected
1232 * packet type. For example, bgp_open_receive() should return BGP_Stop upon
1233 * error and Receive_OPEN_message otherwise.
1234 *
1235 * If no action is necessary, the correct return code is BGP_PACKET_NOOP as
1236 * defined below.
1237 *
1238 * Side effects
1239 * ------------
1240 * - May send NOTIFY messages
1241 * - May not modify peer->status
1242 * - May not call bgp_event_update()
1243 */
1244
1245 #define BGP_PACKET_NOOP 0
1246
1247 /**
1248 * Process BGP OPEN message for peer.
1249 *
1250 * If any errors are encountered in the OPEN message, immediately sends NOTIFY
1251 * and returns BGP_Stop.
1252 *
1253 * @param peer
1254 * @param size size of the packet
1255 * @return as in summary
1256 */
1257 static int bgp_open_receive(struct peer *peer, bgp_size_t size)
1258 {
1259 int ret;
1260 uint8_t version;
1261 uint16_t optlen;
1262 uint16_t holdtime;
1263 uint16_t send_holdtime;
1264 as_t remote_as;
1265 as_t as4 = 0, as4_be;
1266 struct in_addr remote_id;
1267 int mp_capability;
1268 uint8_t notify_data_remote_as[2];
1269 uint8_t notify_data_remote_as4[4];
1270 uint8_t notify_data_remote_id[4];
1271 uint16_t *holdtime_ptr;
1272
1273 /* Parse open packet. */
1274 version = stream_getc(peer->curr);
1275 memcpy(notify_data_remote_as, stream_pnt(peer->curr), 2);
1276 remote_as = stream_getw(peer->curr);
1277 holdtime_ptr = (uint16_t *)stream_pnt(peer->curr);
1278 holdtime = stream_getw(peer->curr);
1279 memcpy(notify_data_remote_id, stream_pnt(peer->curr), 4);
1280 remote_id.s_addr = stream_get_ipv4(peer->curr);
1281
1282 /* BEGIN to read the capability here, but dont do it yet */
1283 mp_capability = 0;
1284 optlen = stream_getc(peer->curr);
1285
1286 /* Extended Optional Parameters Length for BGP OPEN Message */
1287 if (optlen == BGP_OPEN_NON_EXT_OPT_LEN
1288 || CHECK_FLAG(peer->flags, PEER_FLAG_EXTENDED_OPT_PARAMS)) {
1289 uint8_t opttype;
1290
1291 opttype = stream_getc(peer->curr);
1292 if (opttype == BGP_OPEN_NON_EXT_OPT_TYPE_EXTENDED_LENGTH) {
1293 optlen = stream_getw(peer->curr);
1294 SET_FLAG(peer->sflags,
1295 PEER_STATUS_EXT_OPT_PARAMS_LENGTH);
1296 }
1297 }
1298
1299 /* Receive OPEN message log */
1300 if (bgp_debug_neighbor_events(peer))
1301 zlog_debug(
1302 "%s rcv OPEN%s, version %d, remote-as (in open) %u, holdtime %d, id %pI4",
1303 peer->host,
1304 CHECK_FLAG(peer->sflags,
1305 PEER_STATUS_EXT_OPT_PARAMS_LENGTH)
1306 ? " (Extended)"
1307 : "",
1308 version, remote_as, holdtime, &remote_id);
1309
1310 if (optlen != 0) {
1311 /* If not enough bytes, it is an error. */
1312 if (STREAM_READABLE(peer->curr) < optlen) {
1313 flog_err(EC_BGP_PKT_OPEN,
1314 "%s: stream has not enough bytes (%u)",
1315 peer->host, optlen);
1316 bgp_notify_send(peer, BGP_NOTIFY_OPEN_ERR,
1317 BGP_NOTIFY_OPEN_MALFORMED_ATTR);
1318 return BGP_Stop;
1319 }
1320
1321 /* We need the as4 capability value *right now* because
1322 * if it is there, we have not got the remote_as yet, and
1323 * without
1324 * that we do not know which peer is connecting to us now.
1325 */
1326 as4 = peek_for_as4_capability(peer, optlen);
1327 }
1328
1329 as4_be = htonl(as4);
1330 memcpy(notify_data_remote_as4, &as4_be, 4);
1331
1332 /* Just in case we have a silly peer who sends AS4 capability set to 0
1333 */
1334 if (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV) && !as4) {
1335 flog_err(EC_BGP_PKT_OPEN,
1336 "%s bad OPEN, got AS4 capability, but AS4 set to 0",
1337 peer->host);
1338 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1339 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1340 notify_data_remote_as4, 4);
1341 return BGP_Stop;
1342 }
1343
1344 /* Codification of AS 0 Processing */
1345 if (remote_as == BGP_AS_ZERO) {
1346 flog_err(EC_BGP_PKT_OPEN, "%s bad OPEN, got AS set to 0",
1347 peer->host);
1348 bgp_notify_send(peer, BGP_NOTIFY_OPEN_ERR,
1349 BGP_NOTIFY_OPEN_BAD_PEER_AS);
1350 return BGP_Stop;
1351 }
1352
1353 if (remote_as == BGP_AS_TRANS) {
1354 /* Take the AS4 from the capability. We must have received the
1355 * capability now! Otherwise we have a asn16 peer who uses
1356 * BGP_AS_TRANS, for some unknown reason.
1357 */
1358 if (as4 == BGP_AS_TRANS) {
1359 flog_err(
1360 EC_BGP_PKT_OPEN,
1361 "%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
1362 peer->host);
1363 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1364 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1365 notify_data_remote_as4, 4);
1366 return BGP_Stop;
1367 }
1368
1369 if (!as4 && BGP_DEBUG(as4, AS4))
1370 zlog_debug(
1371 "%s [AS4] OPEN remote_as is AS_TRANS, but no AS4. Odd, but proceeding.",
1372 peer->host);
1373 else if (as4 < BGP_AS_MAX && BGP_DEBUG(as4, AS4))
1374 zlog_debug(
1375 "%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits in 2-bytes, very odd peer.",
1376 peer->host, as4);
1377 if (as4)
1378 remote_as = as4;
1379 } else {
1380 /* We may have a partner with AS4 who has an asno < BGP_AS_MAX
1381 */
1382 /* If we have got the capability, peer->as4cap must match
1383 * remote_as */
1384 if (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV)
1385 && as4 != remote_as) {
1386 /* raise error, log this, close session */
1387 flog_err(
1388 EC_BGP_PKT_OPEN,
1389 "%s bad OPEN, got AS4 capability, but remote_as %u mismatch with 16bit 'myasn' %u in open",
1390 peer->host, as4, remote_as);
1391 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1392 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1393 notify_data_remote_as4, 4);
1394 return BGP_Stop;
1395 }
1396 }
1397
1398 /* rfc6286:
1399 * If the BGP Identifier field of the OPEN message
1400 * is zero, or if it is the same as the BGP Identifier
1401 * of the local BGP speaker and the message is from an
1402 * internal peer, then the Error Subcode is set to
1403 * "Bad BGP Identifier".
1404 */
1405 if (remote_id.s_addr == INADDR_ANY
1406 || (peer->sort == BGP_PEER_IBGP
1407 && ntohl(peer->local_id.s_addr) == ntohl(remote_id.s_addr))) {
1408 if (bgp_debug_neighbor_events(peer))
1409 zlog_debug("%s bad OPEN, wrong router identifier %pI4",
1410 peer->host, &remote_id);
1411 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1412 BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
1413 notify_data_remote_id, 4);
1414 return BGP_Stop;
1415 }
1416
1417 /* Peer BGP version check. */
1418 if (version != BGP_VERSION_4) {
1419 uint16_t maxver = htons(BGP_VERSION_4);
1420 /* XXX this reply may not be correct if version < 4 XXX */
1421 if (bgp_debug_neighbor_events(peer))
1422 zlog_debug(
1423 "%s bad protocol version, remote requested %d, local request %d",
1424 peer->host, version, BGP_VERSION_4);
1425 /* Data must be in network byte order here */
1426 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1427 BGP_NOTIFY_OPEN_UNSUP_VERSION,
1428 (uint8_t *)&maxver, 2);
1429 return BGP_Stop;
1430 }
1431
1432 /* Check neighbor as number. */
1433 if (peer->as_type == AS_UNSPECIFIED) {
1434 if (bgp_debug_neighbor_events(peer))
1435 zlog_debug(
1436 "%s bad OPEN, remote AS is unspecified currently",
1437 peer->host);
1438 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1439 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1440 notify_data_remote_as, 2);
1441 return BGP_Stop;
1442 } else if (peer->as_type == AS_INTERNAL) {
1443 if (remote_as != peer->bgp->as) {
1444 if (bgp_debug_neighbor_events(peer))
1445 zlog_debug(
1446 "%s bad OPEN, remote AS is %u, internal specified",
1447 peer->host, remote_as);
1448 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1449 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1450 notify_data_remote_as, 2);
1451 return BGP_Stop;
1452 }
1453 peer->as = peer->local_as;
1454 } else if (peer->as_type == AS_EXTERNAL) {
1455 if (remote_as == peer->bgp->as) {
1456 if (bgp_debug_neighbor_events(peer))
1457 zlog_debug(
1458 "%s bad OPEN, remote AS is %u, external specified",
1459 peer->host, remote_as);
1460 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1461 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1462 notify_data_remote_as, 2);
1463 return BGP_Stop;
1464 }
1465 peer->as = remote_as;
1466 } else if ((peer->as_type == AS_SPECIFIED) && (remote_as != peer->as)) {
1467 if (bgp_debug_neighbor_events(peer))
1468 zlog_debug("%s bad OPEN, remote AS is %u, expected %u",
1469 peer->host, remote_as, peer->as);
1470 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1471 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1472 notify_data_remote_as, 2);
1473 return BGP_Stop;
1474 }
1475
1476 /*
1477 * When collision is detected and this peer is closed.
1478 * Return immediately.
1479 */
1480 ret = bgp_collision_detect(peer, remote_id);
1481 if (ret < 0)
1482 return BGP_Stop;
1483
1484 /* Get sockname. */
1485 if (bgp_getsockname(peer) < 0) {
1486 flog_err_sys(EC_LIB_SOCKET,
1487 "%s: bgp_getsockname() failed for peer: %s",
1488 __func__, peer->host);
1489 return BGP_Stop;
1490 }
1491
1492 /* Set remote router-id */
1493 peer->remote_id = remote_id;
1494
1495 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1496 calculate the value of the Hold Timer by using the smaller of its
1497 configured Hold Time and the Hold Time received in the OPEN message.
1498 The Hold Time MUST be either zero or at least three seconds. An
1499 implementation may reject connections on the basis of the Hold Time.
1500 */
1501
1502 if (holdtime < 3 && holdtime != 0) {
1503 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1504 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME,
1505 (uint8_t *)holdtime_ptr, 2);
1506 return BGP_Stop;
1507 }
1508
1509 /* Send notification message when Hold Time received in the OPEN message
1510 * is smaller than configured minimum Hold Time. */
1511 if (holdtime < peer->bgp->default_min_holdtime
1512 && peer->bgp->default_min_holdtime != 0) {
1513 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1514 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME,
1515 (uint8_t *)holdtime_ptr, 2);
1516 return BGP_Stop;
1517 }
1518
1519 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1520 would be one third of the Hold Time interval. KEEPALIVE messages
1521 MUST NOT be sent more frequently than one per second. An
1522 implementation MAY adjust the rate at which it sends KEEPALIVE
1523 messages as a function of the Hold Time interval. */
1524
1525 if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER))
1526 send_holdtime = peer->holdtime;
1527 else
1528 send_holdtime = peer->bgp->default_holdtime;
1529
1530 if (holdtime < send_holdtime)
1531 peer->v_holdtime = holdtime;
1532 else
1533 peer->v_holdtime = send_holdtime;
1534
1535 /* Set effective keepalive to 1/3 the effective holdtime.
1536 * Use configured keeplive when < effective keepalive.
1537 */
1538 peer->v_keepalive = peer->v_holdtime / 3;
1539 if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER)) {
1540 if (peer->keepalive && peer->keepalive < peer->v_keepalive)
1541 peer->v_keepalive = peer->keepalive;
1542 } else {
1543 if (peer->bgp->default_keepalive
1544 && peer->bgp->default_keepalive < peer->v_keepalive)
1545 peer->v_keepalive = peer->bgp->default_keepalive;
1546 }
1547
1548 /* Open option part parse. */
1549 if (optlen != 0) {
1550 if (bgp_open_option_parse(peer, optlen, &mp_capability) < 0)
1551 return BGP_Stop;
1552 } else {
1553 if (bgp_debug_neighbor_events(peer))
1554 zlog_debug("%s rcvd OPEN w/ OPTION parameter len: 0",
1555 peer->host);
1556 }
1557
1558 /*
1559 * Assume that the peer supports the locally configured set of
1560 * AFI/SAFIs if the peer did not send us any Mulitiprotocol
1561 * capabilities, or if 'override-capability' is configured.
1562 */
1563 if (!mp_capability
1564 || CHECK_FLAG(peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) {
1565 peer->afc_nego[AFI_IP][SAFI_UNICAST] =
1566 peer->afc[AFI_IP][SAFI_UNICAST];
1567 peer->afc_nego[AFI_IP][SAFI_MULTICAST] =
1568 peer->afc[AFI_IP][SAFI_MULTICAST];
1569 peer->afc_nego[AFI_IP][SAFI_LABELED_UNICAST] =
1570 peer->afc[AFI_IP][SAFI_LABELED_UNICAST];
1571 peer->afc_nego[AFI_IP][SAFI_FLOWSPEC] =
1572 peer->afc[AFI_IP][SAFI_FLOWSPEC];
1573 peer->afc_nego[AFI_IP6][SAFI_UNICAST] =
1574 peer->afc[AFI_IP6][SAFI_UNICAST];
1575 peer->afc_nego[AFI_IP6][SAFI_MULTICAST] =
1576 peer->afc[AFI_IP6][SAFI_MULTICAST];
1577 peer->afc_nego[AFI_IP6][SAFI_LABELED_UNICAST] =
1578 peer->afc[AFI_IP6][SAFI_LABELED_UNICAST];
1579 peer->afc_nego[AFI_L2VPN][SAFI_EVPN] =
1580 peer->afc[AFI_L2VPN][SAFI_EVPN];
1581 peer->afc_nego[AFI_IP6][SAFI_FLOWSPEC] =
1582 peer->afc[AFI_IP6][SAFI_FLOWSPEC];
1583 }
1584
1585 /* Verify valid local address present based on negotiated
1586 * address-families. */
1587 if (peer->afc_nego[AFI_IP][SAFI_UNICAST]
1588 || peer->afc_nego[AFI_IP][SAFI_LABELED_UNICAST]
1589 || peer->afc_nego[AFI_IP][SAFI_MULTICAST]
1590 || peer->afc_nego[AFI_IP][SAFI_MPLS_VPN]
1591 || peer->afc_nego[AFI_IP][SAFI_ENCAP]) {
1592 if (peer->nexthop.v4.s_addr == INADDR_ANY) {
1593 #if defined(HAVE_CUMULUS)
1594 zlog_warn("%s: No local IPv4 addr, BGP routing may not work",
1595 peer->host);
1596 #endif
1597 }
1598 }
1599 if (peer->afc_nego[AFI_IP6][SAFI_UNICAST]
1600 || peer->afc_nego[AFI_IP6][SAFI_LABELED_UNICAST]
1601 || peer->afc_nego[AFI_IP6][SAFI_MULTICAST]
1602 || peer->afc_nego[AFI_IP6][SAFI_MPLS_VPN]
1603 || peer->afc_nego[AFI_IP6][SAFI_ENCAP]) {
1604 if (IN6_IS_ADDR_UNSPECIFIED(&peer->nexthop.v6_global)) {
1605 #if defined(HAVE_CUMULUS)
1606 zlog_warn("%s: No local IPv6 address, BGP routing may not work",
1607 peer->host);
1608 #endif
1609 }
1610 }
1611 peer->rtt = sockopt_tcp_rtt(peer->fd);
1612
1613 return Receive_OPEN_message;
1614 }
1615
1616 /**
1617 * Process BGP KEEPALIVE message for peer.
1618 *
1619 * @param peer
1620 * @param size size of the packet
1621 * @return as in summary
1622 */
1623 static int bgp_keepalive_receive(struct peer *peer, bgp_size_t size)
1624 {
1625 if (bgp_debug_keepalive(peer))
1626 zlog_debug("%s KEEPALIVE rcvd", peer->host);
1627
1628 bgp_update_implicit_eors(peer);
1629
1630 peer->rtt = sockopt_tcp_rtt(peer->fd);
1631
1632 /* If the peer's RTT is higher than expected, shutdown
1633 * the peer automatically.
1634 */
1635 if (CHECK_FLAG(peer->flags, PEER_FLAG_RTT_SHUTDOWN)
1636 && peer->rtt > peer->rtt_expected) {
1637
1638 peer->rtt_keepalive_rcv++;
1639
1640 if (peer->rtt_keepalive_rcv > peer->rtt_keepalive_conf) {
1641 zlog_warn(
1642 "%s shutdown due to high round-trip-time (%dms > %dms)",
1643 peer->host, peer->rtt, peer->rtt_expected);
1644 peer_flag_set(peer, PEER_FLAG_SHUTDOWN);
1645 }
1646 } else {
1647 if (peer->rtt_keepalive_rcv)
1648 peer->rtt_keepalive_rcv--;
1649 }
1650
1651 return Receive_KEEPALIVE_message;
1652 }
1653
1654 static void bgp_refresh_stalepath_timer_expire(struct thread *thread)
1655 {
1656 struct peer_af *paf;
1657
1658 paf = THREAD_ARG(thread);
1659
1660 afi_t afi = paf->afi;
1661 safi_t safi = paf->safi;
1662 struct peer *peer = paf->peer;
1663
1664 peer->t_refresh_stalepath = NULL;
1665
1666 if (peer->nsf[afi][safi])
1667 bgp_clear_stale_route(peer, afi, safi);
1668
1669 if (bgp_debug_neighbor_events(peer))
1670 zlog_debug(
1671 "%pBP route-refresh (BoRR) timer expired for afi/safi: %d/%d",
1672 peer, afi, safi);
1673
1674 bgp_timer_set(peer);
1675 }
1676
1677 /**
1678 * Process BGP UPDATE message for peer.
1679 *
1680 * Parses UPDATE and creates attribute object.
1681 *
1682 * @param peer
1683 * @param size size of the packet
1684 * @return as in summary
1685 */
1686 static int bgp_update_receive(struct peer *peer, bgp_size_t size)
1687 {
1688 int ret, nlri_ret;
1689 uint8_t *end;
1690 struct stream *s;
1691 struct attr attr;
1692 bgp_size_t attribute_len;
1693 bgp_size_t update_len;
1694 bgp_size_t withdraw_len;
1695 bool restart = false;
1696
1697 enum NLRI_TYPES {
1698 NLRI_UPDATE,
1699 NLRI_WITHDRAW,
1700 NLRI_MP_UPDATE,
1701 NLRI_MP_WITHDRAW,
1702 NLRI_TYPE_MAX
1703 };
1704 struct bgp_nlri nlris[NLRI_TYPE_MAX];
1705
1706 /* Status must be Established. */
1707 if (!peer_established(peer)) {
1708 flog_err(EC_BGP_INVALID_STATUS,
1709 "%s [FSM] Update packet received under status %s",
1710 peer->host,
1711 lookup_msg(bgp_status_msg, peer->status, NULL));
1712 bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR,
1713 bgp_fsm_error_subcode(peer->status));
1714 return BGP_Stop;
1715 }
1716
1717 /* Set initial values. */
1718 memset(&attr, 0, sizeof(attr));
1719 attr.label_index = BGP_INVALID_LABEL_INDEX;
1720 attr.label = MPLS_INVALID_LABEL;
1721 memset(&nlris, 0, sizeof(nlris));
1722 memset(peer->rcvd_attr_str, 0, BUFSIZ);
1723 peer->rcvd_attr_printed = 0;
1724
1725 s = peer->curr;
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 flog_err(EC_BGP_UPDATE_RCV,
1734 "%s [Error] Update packet error (packet length is short for unfeasible length)",
1735 peer->host);
1736 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
1737 BGP_NOTIFY_UPDATE_MAL_ATTR);
1738 return BGP_Stop;
1739 }
1740
1741 /* Unfeasible Route Length. */
1742 withdraw_len = stream_getw(s);
1743
1744 /* Unfeasible Route Length check. */
1745 if (stream_pnt(s) + withdraw_len > end) {
1746 flog_err(EC_BGP_UPDATE_RCV,
1747 "%s [Error] Update packet error (packet unfeasible length overflow %d)",
1748 peer->host, withdraw_len);
1749 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
1750 BGP_NOTIFY_UPDATE_MAL_ATTR);
1751 return BGP_Stop;
1752 }
1753
1754 /* Unfeasible Route packet format check. */
1755 if (withdraw_len > 0) {
1756 nlris[NLRI_WITHDRAW].afi = AFI_IP;
1757 nlris[NLRI_WITHDRAW].safi = SAFI_UNICAST;
1758 nlris[NLRI_WITHDRAW].nlri = stream_pnt(s);
1759 nlris[NLRI_WITHDRAW].length = withdraw_len;
1760 stream_forward_getp(s, withdraw_len);
1761 }
1762
1763 /* Attribute total length check. */
1764 if (stream_pnt(s) + 2 > end) {
1765 flog_warn(
1766 EC_BGP_UPDATE_PACKET_SHORT,
1767 "%s [Error] Packet Error (update packet is short for attribute length)",
1768 peer->host);
1769 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
1770 BGP_NOTIFY_UPDATE_MAL_ATTR);
1771 return BGP_Stop;
1772 }
1773
1774 /* Fetch attribute total length. */
1775 attribute_len = stream_getw(s);
1776
1777 /* Attribute length check. */
1778 if (stream_pnt(s) + attribute_len > end) {
1779 flog_warn(
1780 EC_BGP_UPDATE_PACKET_LONG,
1781 "%s [Error] Packet Error (update packet attribute length overflow %d)",
1782 peer->host, attribute_len);
1783 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
1784 BGP_NOTIFY_UPDATE_MAL_ATTR);
1785 return BGP_Stop;
1786 }
1787
1788 /* Certain attribute parsing errors should not be considered bad enough
1789 * to reset the session for, most particularly any partial/optional
1790 * attributes that have 'tunneled' over speakers that don't understand
1791 * them. Instead we withdraw only the prefix concerned.
1792 *
1793 * Complicates the flow a little though..
1794 */
1795 enum bgp_attr_parse_ret attr_parse_ret = BGP_ATTR_PARSE_PROCEED;
1796 /* This define morphs the update case into a withdraw when lower levels
1797 * have signalled an error condition where this is best.
1798 */
1799 #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
1800
1801 /* Parse attribute when it exists. */
1802 if (attribute_len) {
1803 attr_parse_ret = bgp_attr_parse(peer, &attr, attribute_len,
1804 &nlris[NLRI_MP_UPDATE],
1805 &nlris[NLRI_MP_WITHDRAW]);
1806 if (attr_parse_ret == BGP_ATTR_PARSE_ERROR) {
1807 bgp_attr_unintern_sub(&attr);
1808 return BGP_Stop;
1809 }
1810 }
1811
1812 /* Logging the attribute. */
1813 if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW
1814 || BGP_DEBUG(update, UPDATE_IN)
1815 || BGP_DEBUG(update, UPDATE_PREFIX)) {
1816 ret = bgp_dump_attr(&attr, peer->rcvd_attr_str,
1817 sizeof(peer->rcvd_attr_str));
1818
1819 peer->stat_upd_7606++;
1820
1821 if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW)
1822 flog_err(
1823 EC_BGP_UPDATE_RCV,
1824 "%pBP rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
1825 peer);
1826
1827 if (ret && bgp_debug_update(peer, NULL, NULL, 1)) {
1828 zlog_debug("%pBP rcvd UPDATE w/ attr: %s", peer,
1829 peer->rcvd_attr_str);
1830 peer->rcvd_attr_printed = 1;
1831 }
1832 }
1833
1834 /* Network Layer Reachability Information. */
1835 update_len = end - stream_pnt(s);
1836
1837 if (update_len) {
1838 /* Set NLRI portion to structure. */
1839 nlris[NLRI_UPDATE].afi = AFI_IP;
1840 nlris[NLRI_UPDATE].safi = SAFI_UNICAST;
1841 nlris[NLRI_UPDATE].nlri = stream_pnt(s);
1842 nlris[NLRI_UPDATE].length = update_len;
1843 stream_forward_getp(s, update_len);
1844
1845 if (CHECK_FLAG(attr.flag, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI))) {
1846 /*
1847 * We skipped nexthop attribute validation earlier so
1848 * validate the nexthop now.
1849 */
1850 if (bgp_attr_nexthop_valid(peer, &attr) < 0) {
1851 bgp_attr_unintern_sub(&attr);
1852 return BGP_Stop;
1853 }
1854 }
1855 }
1856
1857 if (BGP_DEBUG(update, UPDATE_IN))
1858 zlog_debug("%pBP rcvd UPDATE wlen %d attrlen %d alen %d", peer,
1859 withdraw_len, attribute_len, update_len);
1860
1861 /* Parse any given NLRIs */
1862 for (int i = NLRI_UPDATE; i < NLRI_TYPE_MAX; i++) {
1863 if (!nlris[i].nlri)
1864 continue;
1865
1866 /* NLRI is processed iff the peer if configured for the specific
1867 * afi/safi */
1868 if (!peer->afc[nlris[i].afi][nlris[i].safi]) {
1869 zlog_info(
1870 "%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u",
1871 peer->host, nlris[i].afi, nlris[i].safi);
1872 continue;
1873 }
1874
1875 /* EoR handled later */
1876 if (nlris[i].length == 0)
1877 continue;
1878
1879 switch (i) {
1880 case NLRI_UPDATE:
1881 case NLRI_MP_UPDATE:
1882 nlri_ret = bgp_nlri_parse(peer, NLRI_ATTR_ARG,
1883 &nlris[i], 0);
1884 break;
1885 case NLRI_WITHDRAW:
1886 case NLRI_MP_WITHDRAW:
1887 nlri_ret = bgp_nlri_parse(peer, &attr, &nlris[i], 1);
1888 break;
1889 default:
1890 nlri_ret = BGP_NLRI_PARSE_ERROR;
1891 }
1892
1893 if (nlri_ret < BGP_NLRI_PARSE_OK
1894 && nlri_ret != BGP_NLRI_PARSE_ERROR_PREFIX_OVERFLOW) {
1895 flog_err(EC_BGP_UPDATE_RCV,
1896 "%s [Error] Error parsing NLRI", peer->host);
1897 if (peer_established(peer))
1898 bgp_notify_send(
1899 peer, BGP_NOTIFY_UPDATE_ERR,
1900 i <= NLRI_WITHDRAW
1901 ? BGP_NOTIFY_UPDATE_INVAL_NETWORK
1902 : BGP_NOTIFY_UPDATE_OPT_ATTR_ERR);
1903 bgp_attr_unintern_sub(&attr);
1904 return BGP_Stop;
1905 }
1906 }
1907
1908 /* EoR checks
1909 *
1910 * Non-MP IPv4/Unicast EoR is a completely empty UPDATE
1911 * and MP EoR should have only an empty MP_UNREACH
1912 */
1913 if ((!update_len && !withdraw_len && nlris[NLRI_MP_UPDATE].length == 0)
1914 || (attr_parse_ret == BGP_ATTR_PARSE_EOR)) {
1915 afi_t afi = 0;
1916 safi_t safi;
1917 struct graceful_restart_info *gr_info;
1918
1919 /* Restarting router */
1920 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer)
1921 && BGP_PEER_RESTARTING_MODE(peer))
1922 restart = true;
1923
1924 /* Non-MP IPv4/Unicast is a completely emtpy UPDATE - already
1925 * checked
1926 * update and withdraw NLRI lengths are 0.
1927 */
1928 if (!attribute_len) {
1929 afi = AFI_IP;
1930 safi = SAFI_UNICAST;
1931 } else if (attr.flag & ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI)
1932 && nlris[NLRI_MP_WITHDRAW].length == 0) {
1933 afi = nlris[NLRI_MP_WITHDRAW].afi;
1934 safi = nlris[NLRI_MP_WITHDRAW].safi;
1935 } else if (attr_parse_ret == BGP_ATTR_PARSE_EOR) {
1936 afi = nlris[NLRI_MP_UPDATE].afi;
1937 safi = nlris[NLRI_MP_UPDATE].safi;
1938 }
1939
1940 if (afi && peer->afc[afi][safi]) {
1941 struct vrf *vrf = vrf_lookup_by_id(peer->bgp->vrf_id);
1942
1943 /* End-of-RIB received */
1944 if (!CHECK_FLAG(peer->af_sflags[afi][safi],
1945 PEER_STATUS_EOR_RECEIVED)) {
1946 SET_FLAG(peer->af_sflags[afi][safi],
1947 PEER_STATUS_EOR_RECEIVED);
1948 bgp_update_explicit_eors(peer);
1949 /* Update graceful restart information */
1950 gr_info = &(peer->bgp->gr_info[afi][safi]);
1951 if (restart)
1952 gr_info->eor_received++;
1953 /* If EOR received from all peers and selection
1954 * deferral timer is running, cancel the timer
1955 * and invoke the best path calculation
1956 */
1957 if (gr_info->eor_required
1958 == gr_info->eor_received) {
1959 if (bgp_debug_neighbor_events(peer))
1960 zlog_debug(
1961 "%s %d, %s %d",
1962 "EOR REQ",
1963 gr_info->eor_required,
1964 "EOR RCV",
1965 gr_info->eor_received);
1966 BGP_TIMER_OFF(
1967 gr_info->t_select_deferral);
1968 gr_info->eor_required = 0;
1969 gr_info->eor_received = 0;
1970 /* Best path selection */
1971 if (bgp_best_path_select_defer(
1972 peer->bgp, afi, safi)
1973 < 0)
1974 return BGP_Stop;
1975 }
1976 }
1977
1978 /* NSF delete stale route */
1979 if (peer->nsf[afi][safi])
1980 bgp_clear_stale_route(peer, afi, safi);
1981
1982 zlog_info(
1983 "%s: rcvd End-of-RIB for %s from %s in vrf %s",
1984 __func__, get_afi_safi_str(afi, safi, false),
1985 peer->host, vrf ? vrf->name : VRF_DEFAULT_NAME);
1986 }
1987 }
1988
1989 /* Everything is done. We unintern temporary structures which
1990 interned in bgp_attr_parse(). */
1991 bgp_attr_unintern_sub(&attr);
1992
1993 peer->update_time = bgp_clock();
1994
1995 /* Notify BGP Conditional advertisement scanner process */
1996 peer->advmap_table_change = true;
1997
1998 return Receive_UPDATE_message;
1999 }
2000
2001 /**
2002 * Process BGP NOTIFY message for peer.
2003 *
2004 * @param peer
2005 * @param size size of the packet
2006 * @return as in summary
2007 */
2008 static int bgp_notify_receive(struct peer *peer, bgp_size_t size)
2009 {
2010 struct bgp_notify outer = {};
2011 struct bgp_notify inner = {};
2012 bool hard_reset = false;
2013
2014 if (peer->notify.data) {
2015 XFREE(MTYPE_BGP_NOTIFICATION, peer->notify.data);
2016 peer->notify.length = 0;
2017 peer->notify.hard_reset = false;
2018 }
2019
2020 outer.code = stream_getc(peer->curr);
2021 outer.subcode = stream_getc(peer->curr);
2022 outer.length = size - 2;
2023 outer.data = NULL;
2024 outer.raw_data = NULL;
2025 if (outer.length) {
2026 outer.raw_data = XMALLOC(MTYPE_BGP_NOTIFICATION, outer.length);
2027 memcpy(outer.raw_data, stream_pnt(peer->curr), outer.length);
2028 }
2029
2030 hard_reset =
2031 bgp_notify_received_hard_reset(peer, outer.code, outer.subcode);
2032 if (hard_reset && outer.length) {
2033 inner = bgp_notify_decapsulate_hard_reset(&outer);
2034 peer->notify.hard_reset = true;
2035 } else {
2036 inner = outer;
2037 }
2038
2039 /* Preserv notify code and sub code. */
2040 peer->notify.code = inner.code;
2041 peer->notify.subcode = inner.subcode;
2042 /* For further diagnostic record returned Data. */
2043 if (inner.length) {
2044 peer->notify.length = inner.length;
2045 peer->notify.data =
2046 XMALLOC(MTYPE_BGP_NOTIFICATION, inner.length);
2047 memcpy(peer->notify.data, inner.raw_data, inner.length);
2048 }
2049
2050 /* For debug */
2051 {
2052 int i;
2053 int first = 0;
2054 char c[4];
2055
2056 if (inner.length) {
2057 inner.data = XMALLOC(MTYPE_BGP_NOTIFICATION,
2058 inner.length * 3);
2059 for (i = 0; i < inner.length; i++)
2060 if (first) {
2061 snprintf(c, sizeof(c), " %02x",
2062 stream_getc(peer->curr));
2063
2064 strlcat(inner.data, c,
2065 inner.length * 3);
2066
2067 } else {
2068 first = 1;
2069 snprintf(c, sizeof(c), "%02x",
2070 stream_getc(peer->curr));
2071
2072 strlcpy(inner.data, c,
2073 inner.length * 3);
2074 }
2075 }
2076
2077 bgp_notify_print(peer, &inner, "received", hard_reset);
2078 if (inner.length) {
2079 XFREE(MTYPE_BGP_NOTIFICATION, inner.data);
2080 inner.length = 0;
2081 }
2082 if (outer.length) {
2083 XFREE(MTYPE_BGP_NOTIFICATION, outer.data);
2084 XFREE(MTYPE_BGP_NOTIFICATION, outer.raw_data);
2085 outer.length = 0;
2086 }
2087 }
2088
2089 /* peer count update */
2090 atomic_fetch_add_explicit(&peer->notify_in, 1, memory_order_relaxed);
2091
2092 peer->last_reset = PEER_DOWN_NOTIFY_RECEIVED;
2093
2094 /* We have to check for Notify with Unsupported Optional Parameter.
2095 in that case we fallback to open without the capability option.
2096 But this done in bgp_stop. We just mark it here to avoid changing
2097 the fsm tables. */
2098 if (inner.code == BGP_NOTIFY_OPEN_ERR &&
2099 inner.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM)
2100 UNSET_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
2101
2102 /* If Graceful-Restart N-bit (Notification) is exchanged,
2103 * and it's not a Hard Reset, let's retain the routes.
2104 */
2105 if (bgp_has_graceful_restart_notification(peer) && !hard_reset &&
2106 CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_MODE))
2107 SET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT);
2108
2109 bgp_peer_gr_flags_update(peer);
2110 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp,
2111 peer->bgp->peer);
2112
2113 return Receive_NOTIFICATION_message;
2114 }
2115
2116 /**
2117 * Process BGP ROUTEREFRESH message for peer.
2118 *
2119 * @param peer
2120 * @param size size of the packet
2121 * @return as in summary
2122 */
2123 static int bgp_route_refresh_receive(struct peer *peer, bgp_size_t size)
2124 {
2125 iana_afi_t pkt_afi;
2126 afi_t afi;
2127 iana_safi_t pkt_safi;
2128 safi_t safi;
2129 struct stream *s;
2130 struct peer_af *paf;
2131 struct update_group *updgrp;
2132 struct peer *updgrp_peer;
2133 uint8_t subtype;
2134 bool force_update = false;
2135 bgp_size_t msg_length =
2136 size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE);
2137
2138 /* If peer does not have the capability, send notification. */
2139 if (!CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_ADV)) {
2140 flog_err(EC_BGP_NO_CAP,
2141 "%s [Error] BGP route refresh is not enabled",
2142 peer->host);
2143 bgp_notify_send(peer, BGP_NOTIFY_HEADER_ERR,
2144 BGP_NOTIFY_HEADER_BAD_MESTYPE);
2145 return BGP_Stop;
2146 }
2147
2148 /* Status must be Established. */
2149 if (!peer_established(peer)) {
2150 flog_err(
2151 EC_BGP_INVALID_STATUS,
2152 "%s [Error] Route refresh packet received under status %s",
2153 peer->host,
2154 lookup_msg(bgp_status_msg, peer->status, NULL));
2155 bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR,
2156 bgp_fsm_error_subcode(peer->status));
2157 return BGP_Stop;
2158 }
2159
2160 s = peer->curr;
2161
2162 /* Parse packet. */
2163 pkt_afi = stream_getw(s);
2164 subtype = stream_getc(s);
2165 pkt_safi = stream_getc(s);
2166
2167 /* Convert AFI, SAFI to internal values and check. */
2168 if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi, &safi)) {
2169 zlog_info(
2170 "%s REFRESH_REQ for unrecognized afi/safi: %s/%s - ignored",
2171 peer->host, iana_afi2str(pkt_afi),
2172 iana_safi2str(pkt_safi));
2173 return BGP_PACKET_NOOP;
2174 }
2175
2176 if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) {
2177 uint8_t *end;
2178 uint8_t when_to_refresh;
2179 uint8_t orf_type;
2180 uint16_t orf_len;
2181
2182 if (subtype) {
2183 /* If the length, excluding the fixed-size message
2184 * header, of the received ROUTE-REFRESH message with
2185 * Message Subtype 1 and 2 is not 4, then the BGP
2186 * speaker MUST send a NOTIFICATION message with the
2187 * Error Code of "ROUTE-REFRESH Message Error" and the
2188 * subcode of "Invalid Message Length".
2189 */
2190 if (msg_length != 4) {
2191 zlog_err(
2192 "%s Enhanced Route Refresh message length error",
2193 peer->host);
2194 bgp_notify_send(
2195 peer, BGP_NOTIFY_ROUTE_REFRESH_ERR,
2196 BGP_NOTIFY_ROUTE_REFRESH_INVALID_MSG_LEN);
2197 }
2198
2199 /* When the BGP speaker receives a ROUTE-REFRESH message
2200 * with a "Message Subtype" field other than 0, 1, or 2,
2201 * it MUST ignore the received ROUTE-REFRESH message.
2202 */
2203 if (subtype > 2)
2204 zlog_err(
2205 "%s Enhanced Route Refresh invalid subtype",
2206 peer->host);
2207 }
2208
2209 if (msg_length < 5) {
2210 zlog_info("%s ORF route refresh length error",
2211 peer->host);
2212 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2213 BGP_NOTIFY_SUBCODE_UNSPECIFIC);
2214 return BGP_Stop;
2215 }
2216
2217 when_to_refresh = stream_getc(s);
2218 end = stream_pnt(s) + (size - 5);
2219
2220 while ((stream_pnt(s) + 2) < end) {
2221 orf_type = stream_getc(s);
2222 orf_len = stream_getw(s);
2223
2224 /* orf_len in bounds? */
2225 if ((stream_pnt(s) + orf_len) > end)
2226 break; /* XXX: Notify instead?? */
2227 if (orf_type == ORF_TYPE_PREFIX
2228 || orf_type == ORF_TYPE_PREFIX_OLD) {
2229 uint8_t *p_pnt = stream_pnt(s);
2230 uint8_t *p_end = stream_pnt(s) + orf_len;
2231 struct orf_prefix orfp;
2232 uint8_t common = 0;
2233 uint32_t seq;
2234 int psize;
2235 char name[BUFSIZ];
2236 int ret = CMD_SUCCESS;
2237
2238 if (bgp_debug_neighbor_events(peer)) {
2239 zlog_debug(
2240 "%pBP rcvd Prefixlist ORF(%d) length %d",
2241 peer, orf_type, orf_len);
2242 }
2243
2244 /* we're going to read at least 1 byte of common
2245 * ORF header,
2246 * and 7 bytes of ORF Address-filter entry from
2247 * the stream
2248 */
2249 if (orf_len < 7)
2250 break;
2251
2252 /* ORF prefix-list name */
2253 snprintf(name, sizeof(name), "%s.%d.%d",
2254 peer->host, afi, safi);
2255
2256 while (p_pnt < p_end) {
2257 /* If the ORF entry is malformed, want
2258 * to read as much of it
2259 * as possible without going beyond the
2260 * bounds of the entry,
2261 * to maximise debug information.
2262 */
2263 int ok;
2264 memset(&orfp, 0, sizeof(orfp));
2265 common = *p_pnt++;
2266 /* after ++: p_pnt <= p_end */
2267 if (common
2268 & ORF_COMMON_PART_REMOVE_ALL) {
2269 if (bgp_debug_neighbor_events(
2270 peer))
2271 zlog_debug(
2272 "%pBP rcvd Remove-All pfxlist ORF request",
2273 peer);
2274 prefix_bgp_orf_remove_all(afi,
2275 name);
2276 break;
2277 }
2278 ok = ((uint32_t)(p_end - p_pnt)
2279 >= sizeof(uint32_t));
2280 if (ok) {
2281 memcpy(&seq, p_pnt,
2282 sizeof(uint32_t));
2283 p_pnt += sizeof(uint32_t);
2284 orfp.seq = ntohl(seq);
2285 } else
2286 p_pnt = p_end;
2287
2288 /* val checked in prefix_bgp_orf_set */
2289 if (p_pnt < p_end)
2290 orfp.ge = *p_pnt++;
2291
2292 /* val checked in prefix_bgp_orf_set */
2293 if (p_pnt < p_end)
2294 orfp.le = *p_pnt++;
2295
2296 if ((ok = (p_pnt < p_end)))
2297 orfp.p.prefixlen = *p_pnt++;
2298
2299 /* afi checked already */
2300 orfp.p.family = afi2family(afi);
2301
2302 /* 0 if not ok */
2303 psize = PSIZE(orfp.p.prefixlen);
2304 /* valid for family ? */
2305 if (psize > prefix_blen(&orfp.p)) {
2306 ok = 0;
2307 psize = prefix_blen(&orfp.p);
2308 }
2309 /* valid for packet ? */
2310 if (psize > (p_end - p_pnt)) {
2311 ok = 0;
2312 psize = p_end - p_pnt;
2313 }
2314
2315 if (psize > 0)
2316 memcpy(&orfp.p.u.prefix, p_pnt,
2317 psize);
2318 p_pnt += psize;
2319
2320 if (bgp_debug_neighbor_events(peer)) {
2321 char buf[INET6_BUFSIZ];
2322
2323 zlog_debug(
2324 "%pBP rcvd %s %s seq %u %s/%d ge %d le %d%s",
2325 peer,
2326 (common & ORF_COMMON_PART_REMOVE
2327 ? "Remove"
2328 : "Add"),
2329 (common & ORF_COMMON_PART_DENY
2330 ? "deny"
2331 : "permit"),
2332 orfp.seq,
2333 inet_ntop(
2334 orfp.p.family,
2335 &orfp.p.u.prefix,
2336 buf,
2337 INET6_BUFSIZ),
2338 orfp.p.prefixlen,
2339 orfp.ge, orfp.le,
2340 ok ? "" : " MALFORMED");
2341 }
2342
2343 if (ok)
2344 ret = prefix_bgp_orf_set(
2345 name, afi, &orfp,
2346 (common & ORF_COMMON_PART_DENY
2347 ? 0
2348 : 1),
2349 (common & ORF_COMMON_PART_REMOVE
2350 ? 0
2351 : 1));
2352
2353 if (!ok || (ok && ret != CMD_SUCCESS)) {
2354 zlog_info(
2355 "%pBP Received misformatted prefixlist ORF. Remove All pfxlist",
2356 peer);
2357 prefix_bgp_orf_remove_all(afi,
2358 name);
2359 break;
2360 }
2361 }
2362
2363 peer->orf_plist[afi][safi] =
2364 prefix_bgp_orf_lookup(afi, name);
2365 }
2366 stream_forward_getp(s, orf_len);
2367 }
2368 if (bgp_debug_neighbor_events(peer))
2369 zlog_debug("%pBP rcvd Refresh %s ORF request", peer,
2370 when_to_refresh == REFRESH_DEFER
2371 ? "Defer"
2372 : "Immediate");
2373 if (when_to_refresh == REFRESH_DEFER)
2374 return BGP_PACKET_NOOP;
2375 }
2376
2377 /* First update is deferred until ORF or ROUTE-REFRESH is received */
2378 if (CHECK_FLAG(peer->af_sflags[afi][safi],
2379 PEER_STATUS_ORF_WAIT_REFRESH))
2380 UNSET_FLAG(peer->af_sflags[afi][safi],
2381 PEER_STATUS_ORF_WAIT_REFRESH);
2382
2383 paf = peer_af_find(peer, afi, safi);
2384 if (paf && paf->subgroup) {
2385 if (peer->orf_plist[afi][safi]) {
2386 updgrp = PAF_UPDGRP(paf);
2387 updgrp_peer = UPDGRP_PEER(updgrp);
2388 updgrp_peer->orf_plist[afi][safi] =
2389 peer->orf_plist[afi][safi];
2390 }
2391
2392 /* Avoid supressing duplicate routes later
2393 * when processing in subgroup_announce_table().
2394 */
2395 force_update = true;
2396
2397 /* If the peer is configured for default-originate clear the
2398 * SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will
2399 * re-advertise the
2400 * default
2401 */
2402 if (CHECK_FLAG(paf->subgroup->sflags,
2403 SUBGRP_STATUS_DEFAULT_ORIGINATE))
2404 UNSET_FLAG(paf->subgroup->sflags,
2405 SUBGRP_STATUS_DEFAULT_ORIGINATE);
2406 }
2407
2408 if (subtype == BGP_ROUTE_REFRESH_BORR) {
2409 /* A BGP speaker that has received the Graceful Restart
2410 * Capability from its neighbor MUST ignore any BoRRs for
2411 * an <AFI, SAFI> from the neighbor before the speaker
2412 * receives the EoR for the given <AFI, SAFI> from the
2413 * neighbor.
2414 */
2415 if (CHECK_FLAG(peer->cap, PEER_CAP_RESTART_RCV)
2416 && !CHECK_FLAG(peer->af_sflags[afi][safi],
2417 PEER_STATUS_EOR_RECEIVED)) {
2418 if (bgp_debug_neighbor_events(peer))
2419 zlog_debug(
2420 "%pBP rcvd route-refresh (BoRR) for %s/%s before EoR",
2421 peer, afi2str(afi), safi2str(safi));
2422 return BGP_PACKET_NOOP;
2423 }
2424
2425 if (peer->t_refresh_stalepath) {
2426 if (bgp_debug_neighbor_events(peer))
2427 zlog_debug(
2428 "%pBP rcvd route-refresh (BoRR) for %s/%s, whereas BoRR already received",
2429 peer, afi2str(afi), safi2str(safi));
2430 return BGP_PACKET_NOOP;
2431 }
2432
2433 SET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_BORR_RECEIVED);
2434 UNSET_FLAG(peer->af_sflags[afi][safi],
2435 PEER_STATUS_EORR_RECEIVED);
2436
2437 /* When a BGP speaker receives a BoRR message from
2438 * a peer, it MUST mark all the routes with the given
2439 * Address Family Identifier and Subsequent Address
2440 * Family Identifier, <AFI, SAFI> [RFC2918], from
2441 * that peer as stale.
2442 */
2443 if (peer_active_nego(peer)) {
2444 SET_FLAG(peer->af_sflags[afi][safi],
2445 PEER_STATUS_ENHANCED_REFRESH);
2446 bgp_set_stale_route(peer, afi, safi);
2447 }
2448
2449 if (peer_established(peer))
2450 thread_add_timer(bm->master,
2451 bgp_refresh_stalepath_timer_expire,
2452 paf, peer->bgp->stalepath_time,
2453 &peer->t_refresh_stalepath);
2454
2455 if (bgp_debug_neighbor_events(peer))
2456 zlog_debug(
2457 "%pBP rcvd route-refresh (BoRR) for %s/%s, triggering timer for %u seconds",
2458 peer, afi2str(afi), safi2str(safi),
2459 peer->bgp->stalepath_time);
2460 } else if (subtype == BGP_ROUTE_REFRESH_EORR) {
2461 if (!peer->t_refresh_stalepath) {
2462 zlog_err(
2463 "%pBP rcvd route-refresh (EoRR) for %s/%s, whereas no BoRR received",
2464 peer, afi2str(afi), safi2str(safi));
2465 return BGP_PACKET_NOOP;
2466 }
2467
2468 BGP_TIMER_OFF(peer->t_refresh_stalepath);
2469
2470 SET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_EORR_RECEIVED);
2471 UNSET_FLAG(peer->af_sflags[afi][safi],
2472 PEER_STATUS_BORR_RECEIVED);
2473
2474 if (bgp_debug_neighbor_events(peer))
2475 zlog_debug(
2476 "%pBP rcvd route-refresh (EoRR) for %s/%s, stopping BoRR timer",
2477 peer, afi2str(afi), safi2str(safi));
2478
2479 if (peer->nsf[afi][safi])
2480 bgp_clear_stale_route(peer, afi, safi);
2481 } else {
2482 if (bgp_debug_neighbor_events(peer))
2483 zlog_debug(
2484 "%pBP rcvd route-refresh (REQUEST) for %s/%s",
2485 peer, afi2str(afi), safi2str(safi));
2486
2487 /* In response to a "normal route refresh request" from the
2488 * peer, the speaker MUST send a BoRR message.
2489 */
2490 if (CHECK_FLAG(peer->cap, PEER_CAP_ENHANCED_RR_RCV)) {
2491 /* For a BGP speaker that supports the BGP Graceful
2492 * Restart, it MUST NOT send a BoRR for an <AFI, SAFI>
2493 * to a neighbor before it sends the EoR for the
2494 * <AFI, SAFI> to the neighbor.
2495 */
2496 if (!CHECK_FLAG(peer->af_sflags[afi][safi],
2497 PEER_STATUS_EOR_SEND)) {
2498 if (bgp_debug_neighbor_events(peer))
2499 zlog_debug(
2500 "%pBP rcvd route-refresh (REQUEST) for %s/%s before EoR",
2501 peer, afi2str(afi),
2502 safi2str(safi));
2503 return BGP_PACKET_NOOP;
2504 }
2505
2506 bgp_route_refresh_send(peer, afi, safi, 0, 0, 0,
2507 BGP_ROUTE_REFRESH_BORR);
2508
2509 if (bgp_debug_neighbor_events(peer))
2510 zlog_debug(
2511 "%pBP sending route-refresh (BoRR) for %s/%s",
2512 peer, afi2str(afi), safi2str(safi));
2513
2514 /* Set flag Ready-To-Send to know when we can send EoRR
2515 * message.
2516 */
2517 SET_FLAG(peer->af_sflags[afi][safi],
2518 PEER_STATUS_BORR_SEND);
2519 UNSET_FLAG(peer->af_sflags[afi][safi],
2520 PEER_STATUS_EORR_SEND);
2521 }
2522 }
2523
2524 /* Perform route refreshment to the peer */
2525 bgp_announce_route(peer, afi, safi, force_update);
2526
2527 /* No FSM action necessary */
2528 return BGP_PACKET_NOOP;
2529 }
2530
2531 /**
2532 * Parse BGP CAPABILITY message for peer.
2533 *
2534 * @param peer
2535 * @param size size of the packet
2536 * @return as in summary
2537 */
2538 static int bgp_capability_msg_parse(struct peer *peer, uint8_t *pnt,
2539 bgp_size_t length)
2540 {
2541 uint8_t *end;
2542 struct capability_mp_data mpc;
2543 struct capability_header *hdr;
2544 uint8_t action;
2545 iana_afi_t pkt_afi;
2546 afi_t afi;
2547 iana_safi_t pkt_safi;
2548 safi_t safi;
2549
2550 end = pnt + length;
2551
2552 while (pnt < end) {
2553 /* We need at least action, capability code and capability
2554 * length. */
2555 if (pnt + 3 > end) {
2556 zlog_info("%s Capability length error", peer->host);
2557 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2558 BGP_NOTIFY_SUBCODE_UNSPECIFIC);
2559 return BGP_Stop;
2560 }
2561 action = *pnt;
2562 hdr = (struct capability_header *)(pnt + 1);
2563
2564 /* Action value check. */
2565 if (action != CAPABILITY_ACTION_SET
2566 && action != CAPABILITY_ACTION_UNSET) {
2567 zlog_info("%s Capability Action Value error %d",
2568 peer->host, action);
2569 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2570 BGP_NOTIFY_SUBCODE_UNSPECIFIC);
2571 return BGP_Stop;
2572 }
2573
2574 if (bgp_debug_neighbor_events(peer))
2575 zlog_debug(
2576 "%s CAPABILITY has action: %d, code: %u, length %u",
2577 peer->host, action, hdr->code, hdr->length);
2578
2579 /* Capability length check. */
2580 if ((pnt + hdr->length + 3) > end) {
2581 zlog_info("%s Capability length error", peer->host);
2582 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2583 BGP_NOTIFY_SUBCODE_UNSPECIFIC);
2584 return BGP_Stop;
2585 }
2586
2587 /* Fetch structure to the byte stream. */
2588 memcpy(&mpc, pnt + 3, sizeof(struct capability_mp_data));
2589 pnt += hdr->length + 3;
2590
2591 /* We know MP Capability Code. */
2592 if (hdr->code == CAPABILITY_CODE_MP) {
2593 pkt_afi = ntohs(mpc.afi);
2594 pkt_safi = mpc.safi;
2595
2596 /* Ignore capability when override-capability is set. */
2597 if (CHECK_FLAG(peer->flags,
2598 PEER_FLAG_OVERRIDE_CAPABILITY))
2599 continue;
2600
2601 /* Convert AFI, SAFI to internal values. */
2602 if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi,
2603 &safi)) {
2604 if (bgp_debug_neighbor_events(peer))
2605 zlog_debug(
2606 "%s Dynamic Capability MP_EXT afi/safi invalid (%s/%s)",
2607 peer->host,
2608 iana_afi2str(pkt_afi),
2609 iana_safi2str(pkt_safi));
2610 continue;
2611 }
2612
2613 /* Address family check. */
2614 if (bgp_debug_neighbor_events(peer))
2615 zlog_debug(
2616 "%s CAPABILITY has %s MP_EXT CAP for afi/safi: %s/%s",
2617 peer->host,
2618 action == CAPABILITY_ACTION_SET
2619 ? "Advertising"
2620 : "Removing",
2621 iana_afi2str(pkt_afi),
2622 iana_safi2str(pkt_safi));
2623
2624 if (action == CAPABILITY_ACTION_SET) {
2625 peer->afc_recv[afi][safi] = 1;
2626 if (peer->afc[afi][safi]) {
2627 peer->afc_nego[afi][safi] = 1;
2628 bgp_announce_route(peer, afi, safi,
2629 false);
2630 }
2631 } else {
2632 peer->afc_recv[afi][safi] = 0;
2633 peer->afc_nego[afi][safi] = 0;
2634
2635 if (peer_active_nego(peer))
2636 bgp_clear_route(peer, afi, safi);
2637 else
2638 return BGP_Stop;
2639 }
2640 } else {
2641 flog_warn(
2642 EC_BGP_UNRECOGNIZED_CAPABILITY,
2643 "%s unrecognized capability code: %d - ignored",
2644 peer->host, hdr->code);
2645 }
2646 }
2647
2648 /* No FSM action necessary */
2649 return BGP_PACKET_NOOP;
2650 }
2651
2652 /**
2653 * Parse BGP CAPABILITY message for peer.
2654 *
2655 * Exported for unit testing.
2656 *
2657 * @param peer
2658 * @param size size of the packet
2659 * @return as in summary
2660 */
2661 int bgp_capability_receive(struct peer *peer, bgp_size_t size)
2662 {
2663 uint8_t *pnt;
2664
2665 /* Fetch pointer. */
2666 pnt = stream_pnt(peer->curr);
2667
2668 if (bgp_debug_neighbor_events(peer))
2669 zlog_debug("%s rcv CAPABILITY", peer->host);
2670
2671 /* If peer does not have the capability, send notification. */
2672 if (!CHECK_FLAG(peer->cap, PEER_CAP_DYNAMIC_ADV)) {
2673 flog_err(EC_BGP_NO_CAP,
2674 "%s [Error] BGP dynamic capability is not enabled",
2675 peer->host);
2676 bgp_notify_send(peer, BGP_NOTIFY_HEADER_ERR,
2677 BGP_NOTIFY_HEADER_BAD_MESTYPE);
2678 return BGP_Stop;
2679 }
2680
2681 /* Status must be Established. */
2682 if (!peer_established(peer)) {
2683 flog_err(
2684 EC_BGP_NO_CAP,
2685 "%s [Error] Dynamic capability packet received under status %s",
2686 peer->host,
2687 lookup_msg(bgp_status_msg, peer->status, NULL));
2688 bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR,
2689 bgp_fsm_error_subcode(peer->status));
2690 return BGP_Stop;
2691 }
2692
2693 /* Parse packet. */
2694 return bgp_capability_msg_parse(peer, pnt, size);
2695 }
2696
2697 /**
2698 * Processes a peer's input buffer.
2699 *
2700 * This function sidesteps the event loop and directly calls bgp_event_update()
2701 * after processing each BGP message. This is necessary to ensure proper
2702 * ordering of FSM events and unifies the behavior that was present previously,
2703 * whereby some of the packet handling functions would update the FSM and some
2704 * would not, making event flow difficult to understand. Please think twice
2705 * before hacking this.
2706 *
2707 * Thread type: THREAD_EVENT
2708 * @param thread
2709 * @return 0
2710 */
2711 void bgp_process_packet(struct thread *thread)
2712 {
2713 /* Yes first of all get peer pointer. */
2714 struct peer *peer; // peer
2715 uint32_t rpkt_quanta_old; // how many packets to read
2716 int fsm_update_result; // return code of bgp_event_update()
2717 int mprc; // message processing return code
2718
2719 peer = THREAD_ARG(thread);
2720 rpkt_quanta_old = atomic_load_explicit(&peer->bgp->rpkt_quanta,
2721 memory_order_relaxed);
2722 fsm_update_result = 0;
2723
2724 /* Guard against scheduled events that occur after peer deletion. */
2725 if (peer->status == Deleted || peer->status == Clearing)
2726 return;
2727
2728 unsigned int processed = 0;
2729
2730 while (processed < rpkt_quanta_old) {
2731 uint8_t type = 0;
2732 bgp_size_t size;
2733 char notify_data_length[2];
2734
2735 frr_with_mutex(&peer->io_mtx) {
2736 peer->curr = stream_fifo_pop(peer->ibuf);
2737 }
2738
2739 if (peer->curr == NULL) // no packets to process, hmm...
2740 return;
2741
2742 /* skip the marker and copy the packet length */
2743 stream_forward_getp(peer->curr, BGP_MARKER_SIZE);
2744 memcpy(notify_data_length, stream_pnt(peer->curr), 2);
2745
2746 /* read in the packet length and type */
2747 size = stream_getw(peer->curr);
2748 type = stream_getc(peer->curr);
2749
2750 hook_call(bgp_packet_dump, peer, type, size, peer->curr);
2751
2752 /* adjust size to exclude the marker + length + type */
2753 size -= BGP_HEADER_SIZE;
2754
2755 /* Read rest of the packet and call each sort of packet routine
2756 */
2757 switch (type) {
2758 case BGP_MSG_OPEN:
2759 frrtrace(2, frr_bgp, open_process, peer, size);
2760 atomic_fetch_add_explicit(&peer->open_in, 1,
2761 memory_order_relaxed);
2762 mprc = bgp_open_receive(peer, size);
2763 if (mprc == BGP_Stop)
2764 flog_err(
2765 EC_BGP_PKT_OPEN,
2766 "%s: BGP OPEN receipt failed for peer: %s",
2767 __func__, peer->host);
2768 break;
2769 case BGP_MSG_UPDATE:
2770 frrtrace(2, frr_bgp, update_process, peer, size);
2771 atomic_fetch_add_explicit(&peer->update_in, 1,
2772 memory_order_relaxed);
2773 peer->readtime = monotime(NULL);
2774 mprc = bgp_update_receive(peer, size);
2775 if (mprc == BGP_Stop)
2776 flog_err(
2777 EC_BGP_UPDATE_RCV,
2778 "%s: BGP UPDATE receipt failed for peer: %s",
2779 __func__, peer->host);
2780 break;
2781 case BGP_MSG_NOTIFY:
2782 frrtrace(2, frr_bgp, notification_process, peer, size);
2783 atomic_fetch_add_explicit(&peer->notify_in, 1,
2784 memory_order_relaxed);
2785 mprc = bgp_notify_receive(peer, size);
2786 if (mprc == BGP_Stop)
2787 flog_err(
2788 EC_BGP_NOTIFY_RCV,
2789 "%s: BGP NOTIFY receipt failed for peer: %s",
2790 __func__, peer->host);
2791 break;
2792 case BGP_MSG_KEEPALIVE:
2793 frrtrace(2, frr_bgp, keepalive_process, peer, size);
2794 peer->readtime = monotime(NULL);
2795 atomic_fetch_add_explicit(&peer->keepalive_in, 1,
2796 memory_order_relaxed);
2797 mprc = bgp_keepalive_receive(peer, size);
2798 if (mprc == BGP_Stop)
2799 flog_err(
2800 EC_BGP_KEEP_RCV,
2801 "%s: BGP KEEPALIVE receipt failed for peer: %s",
2802 __func__, peer->host);
2803 break;
2804 case BGP_MSG_ROUTE_REFRESH_NEW:
2805 case BGP_MSG_ROUTE_REFRESH_OLD:
2806 frrtrace(2, frr_bgp, refresh_process, peer, size);
2807 atomic_fetch_add_explicit(&peer->refresh_in, 1,
2808 memory_order_relaxed);
2809 mprc = bgp_route_refresh_receive(peer, size);
2810 if (mprc == BGP_Stop)
2811 flog_err(
2812 EC_BGP_RFSH_RCV,
2813 "%s: BGP ROUTEREFRESH receipt failed for peer: %s",
2814 __func__, peer->host);
2815 break;
2816 case BGP_MSG_CAPABILITY:
2817 frrtrace(2, frr_bgp, capability_process, peer, size);
2818 atomic_fetch_add_explicit(&peer->dynamic_cap_in, 1,
2819 memory_order_relaxed);
2820 mprc = bgp_capability_receive(peer, size);
2821 if (mprc == BGP_Stop)
2822 flog_err(
2823 EC_BGP_CAP_RCV,
2824 "%s: BGP CAPABILITY receipt failed for peer: %s",
2825 __func__, peer->host);
2826 break;
2827 default:
2828 /* Suppress uninitialized variable warning */
2829 mprc = 0;
2830 (void)mprc;
2831 /*
2832 * The message type should have been sanitized before
2833 * we ever got here. Receipt of a message with an
2834 * invalid header at this point is indicative of a
2835 * security issue.
2836 */
2837 assert (!"Message of invalid type received during input processing");
2838 }
2839
2840 /* delete processed packet */
2841 stream_free(peer->curr);
2842 peer->curr = NULL;
2843 processed++;
2844
2845 /* Update FSM */
2846 if (mprc != BGP_PACKET_NOOP)
2847 fsm_update_result = bgp_event_update(peer, mprc);
2848 else
2849 continue;
2850
2851 /*
2852 * If peer was deleted, do not process any more packets. This
2853 * is usually due to executing BGP_Stop or a stub deletion.
2854 */
2855 if (fsm_update_result == FSM_PEER_TRANSFERRED
2856 || fsm_update_result == FSM_PEER_STOPPED)
2857 break;
2858 }
2859
2860 if (fsm_update_result != FSM_PEER_TRANSFERRED
2861 && fsm_update_result != FSM_PEER_STOPPED) {
2862 frr_with_mutex(&peer->io_mtx) {
2863 // more work to do, come back later
2864 if (peer->ibuf->count > 0)
2865 thread_add_event(
2866 bm->master, bgp_process_packet, peer, 0,
2867 &peer->t_process_packet);
2868 }
2869 }
2870 }
2871
2872 /* Send EOR when routes are processed by selection deferral timer */
2873 void bgp_send_delayed_eor(struct bgp *bgp)
2874 {
2875 struct peer *peer;
2876 struct listnode *node, *nnode;
2877
2878 /* EOR message sent in bgp_write_proceed_actions */
2879 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer))
2880 bgp_write_proceed_actions(peer);
2881 }
2882
2883 /*
2884 * Task callback to handle socket error encountered in the io pthread. We avoid
2885 * having the io pthread try to enqueue fsm events or mess with the peer
2886 * struct.
2887 */
2888 void bgp_packet_process_error(struct thread *thread)
2889 {
2890 struct peer *peer;
2891 int code;
2892
2893 peer = THREAD_ARG(thread);
2894 code = THREAD_VAL(thread);
2895
2896 if (bgp_debug_neighbor_events(peer))
2897 zlog_debug("%s [Event] BGP error %d on fd %d",
2898 peer->host, code, peer->fd);
2899
2900 /* Closed connection or error on the socket */
2901 if (peer_established(peer)) {
2902 if ((CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART)
2903 || CHECK_FLAG(peer->flags,
2904 PEER_FLAG_GRACEFUL_RESTART_HELPER))
2905 && CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_MODE)) {
2906 peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
2907 SET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT);
2908 } else
2909 peer->last_reset = PEER_DOWN_CLOSE_SESSION;
2910 }
2911
2912 bgp_event_update(peer, code);
2913 }