]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_packet.c
Merge pull request #10081 from ckishimo/ospf6d_type4
[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;
135 iana_safi_t pkt_safi;
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 int 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 0;
411
412 if ((peer->bgp->main_peers_update_hold)
413 || bgp_update_delay_active(peer->bgp))
414 return 0;
415
416 if (peer->t_routeadv)
417 return 0;
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 "%s sending route-refresh (EoRR) for %s/%s",
487 peer->host,
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 return 0;
546 }
547
548 /*
549 * Creates a BGP Keepalive packet and appends it to the peer's output queue.
550 */
551 void bgp_keepalive_send(struct peer *peer)
552 {
553 struct stream *s;
554
555 s = stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE);
556
557 /* Make keepalive packet. */
558 bgp_packet_set_marker(s, BGP_MSG_KEEPALIVE);
559
560 /* Set packet size. */
561 bgp_packet_set_size(s);
562
563 /* Dump packet if debug option is set. */
564 /* bgp_packet_dump (s); */
565
566 if (bgp_debug_keepalive(peer))
567 zlog_debug("%s sending KEEPALIVE", peer->host);
568
569 /* Add packet to the peer. */
570 bgp_packet_add(peer, s);
571
572 bgp_writes_on(peer);
573 }
574
575 /*
576 * Creates a BGP Open packet and appends it to the peer's output queue.
577 * Sets capabilities as necessary.
578 */
579 void bgp_open_send(struct peer *peer)
580 {
581 struct stream *s;
582 uint16_t send_holdtime;
583 as_t local_as;
584
585 if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER))
586 send_holdtime = peer->holdtime;
587 else
588 send_holdtime = peer->bgp->default_holdtime;
589
590 /* local-as Change */
591 if (peer->change_local_as)
592 local_as = peer->change_local_as;
593 else
594 local_as = peer->local_as;
595
596 s = stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE);
597
598 /* Make open packet. */
599 bgp_packet_set_marker(s, BGP_MSG_OPEN);
600
601 /* Set open packet values. */
602 stream_putc(s, BGP_VERSION_4); /* BGP version */
603 stream_putw(s, (local_as <= BGP_AS_MAX) ? (uint16_t)local_as
604 : BGP_AS_TRANS);
605 stream_putw(s, send_holdtime); /* Hold Time */
606 stream_put_in_addr(s, &peer->local_id); /* BGP Identifier */
607
608 /* Set capabilities */
609 if (CHECK_FLAG(peer->flags, PEER_FLAG_EXTENDED_OPT_PARAMS)) {
610 (void)bgp_open_capability(s, peer, true);
611 } else {
612 struct stream *tmp = stream_new(STREAM_SIZE(s));
613
614 stream_copy(tmp, s);
615 if (bgp_open_capability(tmp, peer, false)
616 > BGP_OPEN_NON_EXT_OPT_LEN) {
617 stream_free(tmp);
618 (void)bgp_open_capability(s, peer, true);
619 } else {
620 stream_copy(s, tmp);
621 stream_free(tmp);
622 }
623 }
624
625 /* Set BGP packet length. */
626 bgp_packet_set_size(s);
627
628 if (bgp_debug_neighbor_events(peer))
629 zlog_debug(
630 "%s sending OPEN, version %d, my as %u, holdtime %d, id %pI4",
631 peer->host, BGP_VERSION_4, local_as, send_holdtime,
632 &peer->local_id);
633
634 /* Dump packet if debug option is set. */
635 /* bgp_packet_dump (s); */
636 hook_call(bgp_packet_send, peer, BGP_MSG_OPEN, stream_get_endp(s), s);
637
638 /* Add packet to the peer. */
639 bgp_packet_add(peer, s);
640
641 bgp_writes_on(peer);
642 }
643
644 /*
645 * Writes NOTIFICATION message directly to a peer socket without waiting for
646 * the I/O thread.
647 *
648 * There must be exactly one stream on the peer->obuf FIFO, and the data within
649 * this stream must match the format of a BGP NOTIFICATION message.
650 * Transmission is best-effort.
651 *
652 * @requires peer->io_mtx
653 * @param peer
654 * @return 0
655 */
656 static void bgp_write_notify(struct peer *peer)
657 {
658 int ret, val;
659 uint8_t type;
660 struct stream *s;
661
662 /* There should be at least one packet. */
663 s = stream_fifo_pop(peer->obuf);
664
665 if (!s)
666 return;
667
668 assert(stream_get_endp(s) >= BGP_HEADER_SIZE);
669
670 /* Stop collecting data within the socket */
671 sockopt_cork(peer->fd, 0);
672
673 /*
674 * socket is in nonblocking mode, if we can't deliver the NOTIFY, well,
675 * we only care about getting a clean shutdown at this point.
676 */
677 ret = write(peer->fd, STREAM_DATA(s), stream_get_endp(s));
678
679 /*
680 * only connection reset/close gets counted as TCP_fatal_error, failure
681 * to write the entire NOTIFY doesn't get different FSM treatment
682 */
683 if (ret <= 0) {
684 stream_free(s);
685 BGP_EVENT_ADD(peer, TCP_fatal_error);
686 return;
687 }
688
689 /* Disable Nagle, make NOTIFY packet go out right away */
690 val = 1;
691 (void)setsockopt(peer->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val,
692 sizeof(val));
693
694 /* Retrieve BGP packet type. */
695 stream_set_getp(s, BGP_MARKER_SIZE + 2);
696 type = stream_getc(s);
697
698 assert(type == BGP_MSG_NOTIFY);
699
700 /* Type should be notify. */
701 atomic_fetch_add_explicit(&peer->notify_out, 1, memory_order_relaxed);
702 peer->notify_out++;
703
704 /* Double start timer. */
705 peer->v_start *= 2;
706
707 /* Overflow check. */
708 if (peer->v_start >= (60 * 2))
709 peer->v_start = (60 * 2);
710
711 /*
712 * Handle Graceful Restart case where the state changes to
713 * Connect instead of Idle
714 */
715 BGP_EVENT_ADD(peer, BGP_Stop);
716
717 stream_free(s);
718 }
719
720 /*
721 * Creates a BGP Notify and appends it to the peer's output queue.
722 *
723 * This function attempts to write the packet from the thread it is called
724 * from, to ensure the packet gets out ASAP.
725 *
726 * This function may be called from multiple threads. Since the function
727 * modifies I/O buffer(s) in the peer, these are locked for the duration of the
728 * call to prevent tampering from other threads.
729 *
730 * Delivery of the NOTIFICATION is attempted once and is best-effort. After
731 * return, the peer structure *must* be reset; no assumptions about session
732 * state are valid.
733 *
734 * @param peer
735 * @param code BGP error code
736 * @param sub_code BGP error subcode
737 * @param data Data portion
738 * @param datalen length of data portion
739 */
740 void bgp_notify_send_with_data(struct peer *peer, uint8_t code,
741 uint8_t sub_code, uint8_t *data, size_t datalen)
742 {
743 struct stream *s;
744
745 /* Lock I/O mutex to prevent other threads from pushing packets */
746 frr_mutex_lock_autounlock(&peer->io_mtx);
747 /* ============================================== */
748
749 /* Allocate new stream. */
750 s = stream_new(peer->max_packet_size);
751
752 /* Make notify packet. */
753 bgp_packet_set_marker(s, BGP_MSG_NOTIFY);
754
755 /* Set notify packet values. */
756 stream_putc(s, code); /* BGP notify code */
757 stream_putc(s, sub_code); /* BGP notify sub_code */
758
759 /* If notify data is present. */
760 if (data)
761 stream_write(s, data, datalen);
762
763 /* Set BGP packet length. */
764 bgp_packet_set_size(s);
765
766 /* wipe output buffer */
767 stream_fifo_clean(peer->obuf);
768
769 /*
770 * If possible, store last packet for debugging purposes. This check is
771 * in place because we are sometimes called with a doppelganger peer,
772 * who tends to have a plethora of fields nulled out.
773 */
774 if (peer->curr) {
775 size_t packetsize = stream_get_endp(peer->curr);
776 assert(packetsize <= peer->max_packet_size);
777 memcpy(peer->last_reset_cause, peer->curr->data, packetsize);
778 peer->last_reset_cause_size = packetsize;
779 }
780
781 /* For debug */
782 {
783 struct bgp_notify bgp_notify;
784 int first = 0;
785 int i;
786 char c[4];
787
788 bgp_notify.code = code;
789 bgp_notify.subcode = sub_code;
790 bgp_notify.data = NULL;
791 bgp_notify.length = datalen;
792 bgp_notify.raw_data = data;
793
794 peer->notify.code = bgp_notify.code;
795 peer->notify.subcode = bgp_notify.subcode;
796
797 if (bgp_notify.length && data) {
798 bgp_notify.data =
799 XMALLOC(MTYPE_TMP, bgp_notify.length * 3);
800 for (i = 0; i < bgp_notify.length; i++)
801 if (first) {
802 snprintf(c, sizeof(c), " %02x",
803 data[i]);
804
805 strlcat(bgp_notify.data, c,
806 bgp_notify.length);
807
808 } else {
809 first = 1;
810 snprintf(c, sizeof(c), "%02x", data[i]);
811
812 strlcpy(bgp_notify.data, c,
813 bgp_notify.length);
814 }
815 }
816 bgp_notify_print(peer, &bgp_notify, "sending");
817
818 if (bgp_notify.data) {
819 XFREE(MTYPE_TMP, bgp_notify.data);
820 bgp_notify.length = 0;
821 }
822 }
823
824 /* peer reset cause */
825 if (code == BGP_NOTIFY_CEASE) {
826 if (sub_code == BGP_NOTIFY_CEASE_ADMIN_RESET)
827 peer->last_reset = PEER_DOWN_USER_RESET;
828 else if (sub_code == BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN)
829 peer->last_reset = PEER_DOWN_USER_SHUTDOWN;
830 else
831 peer->last_reset = PEER_DOWN_NOTIFY_SEND;
832 } else
833 peer->last_reset = PEER_DOWN_NOTIFY_SEND;
834
835 /* Add packet to peer's output queue */
836 stream_fifo_push(peer->obuf, s);
837
838 bgp_peer_gr_flags_update(peer);
839 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp,
840 peer->bgp->peer);
841
842 bgp_write_notify(peer);
843 }
844
845 /*
846 * Creates a BGP Notify and appends it to the peer's output queue.
847 *
848 * This function attempts to write the packet from the thread it is called
849 * from, to ensure the packet gets out ASAP.
850 *
851 * @param peer
852 * @param code BGP error code
853 * @param sub_code BGP error subcode
854 */
855 void bgp_notify_send(struct peer *peer, uint8_t code, uint8_t sub_code)
856 {
857 bgp_notify_send_with_data(peer, code, sub_code, NULL, 0);
858 }
859
860 /*
861 * Creates BGP Route Refresh packet and appends it to the peer's output queue.
862 *
863 * @param peer
864 * @param afi Address Family Identifier
865 * @param safi Subsequent Address Family Identifier
866 * @param orf_type Outbound Route Filtering type
867 * @param when_to_refresh Whether to refresh immediately or defer
868 * @param remove Whether to remove ORF for specified AFI/SAFI
869 */
870 void bgp_route_refresh_send(struct peer *peer, afi_t afi, safi_t safi,
871 uint8_t orf_type, uint8_t when_to_refresh,
872 int remove, uint8_t subtype)
873 {
874 struct stream *s;
875 struct bgp_filter *filter;
876 int orf_refresh = 0;
877 iana_afi_t pkt_afi;
878 iana_safi_t pkt_safi;
879
880 if (DISABLE_BGP_ANNOUNCE)
881 return;
882
883 filter = &peer->filter[afi][safi];
884
885 /* Convert AFI, SAFI to values for packet. */
886 bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi);
887
888 s = stream_new(peer->max_packet_size);
889
890 /* Make BGP update packet. */
891 if (CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_NEW_RCV))
892 bgp_packet_set_marker(s, BGP_MSG_ROUTE_REFRESH_NEW);
893 else
894 bgp_packet_set_marker(s, BGP_MSG_ROUTE_REFRESH_OLD);
895
896 /* Encode Route Refresh message. */
897 stream_putw(s, pkt_afi);
898 if (subtype)
899 stream_putc(s, subtype);
900 else
901 stream_putc(s, 0);
902 stream_putc(s, pkt_safi);
903
904 if (orf_type == ORF_TYPE_PREFIX || orf_type == ORF_TYPE_PREFIX_OLD)
905 if (remove || filter->plist[FILTER_IN].plist) {
906 uint16_t orf_len;
907 unsigned long orfp;
908
909 orf_refresh = 1;
910 stream_putc(s, when_to_refresh);
911 stream_putc(s, orf_type);
912 orfp = stream_get_endp(s);
913 stream_putw(s, 0);
914
915 if (remove) {
916 UNSET_FLAG(peer->af_sflags[afi][safi],
917 PEER_STATUS_ORF_PREFIX_SEND);
918 stream_putc(s, ORF_COMMON_PART_REMOVE_ALL);
919 if (bgp_debug_neighbor_events(peer))
920 zlog_debug(
921 "%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %s/%s",
922 peer->host, orf_type,
923 (when_to_refresh == REFRESH_DEFER
924 ? "defer"
925 : "immediate"),
926 iana_afi2str(pkt_afi),
927 iana_safi2str(pkt_safi));
928 } else {
929 SET_FLAG(peer->af_sflags[afi][safi],
930 PEER_STATUS_ORF_PREFIX_SEND);
931 prefix_bgp_orf_entry(
932 s, filter->plist[FILTER_IN].plist,
933 ORF_COMMON_PART_ADD,
934 ORF_COMMON_PART_PERMIT,
935 ORF_COMMON_PART_DENY);
936 if (bgp_debug_neighbor_events(peer))
937 zlog_debug(
938 "%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %s/%s",
939 peer->host, orf_type,
940 (when_to_refresh == REFRESH_DEFER
941 ? "defer"
942 : "immediate"),
943 iana_afi2str(pkt_afi),
944 iana_safi2str(pkt_safi));
945 }
946
947 /* Total ORF Entry Len. */
948 orf_len = stream_get_endp(s) - orfp - 2;
949 stream_putw_at(s, orfp, orf_len);
950 }
951
952 /* Set packet size. */
953 bgp_packet_set_size(s);
954
955 if (bgp_debug_neighbor_events(peer)) {
956 if (!orf_refresh)
957 zlog_debug("%s sending REFRESH_REQ for afi/safi: %s/%s",
958 peer->host, iana_afi2str(pkt_afi),
959 iana_safi2str(pkt_safi));
960 }
961
962 /* Add packet to the peer. */
963 bgp_packet_add(peer, s);
964
965 bgp_writes_on(peer);
966 }
967
968 /*
969 * Create a BGP Capability packet and append 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 capability_code BGP Capability Code
975 * @param action Set or Remove capability
976 */
977 void bgp_capability_send(struct peer *peer, afi_t afi, safi_t safi,
978 int capability_code, int action)
979 {
980 struct stream *s;
981 iana_afi_t pkt_afi;
982 iana_safi_t pkt_safi;
983
984 /* Convert AFI, SAFI to values for packet. */
985 bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi);
986
987 s = stream_new(peer->max_packet_size);
988
989 /* Make BGP update packet. */
990 bgp_packet_set_marker(s, BGP_MSG_CAPABILITY);
991
992 /* Encode MP_EXT capability. */
993 if (capability_code == CAPABILITY_CODE_MP) {
994 stream_putc(s, action);
995 stream_putc(s, CAPABILITY_CODE_MP);
996 stream_putc(s, CAPABILITY_CODE_MP_LEN);
997 stream_putw(s, pkt_afi);
998 stream_putc(s, 0);
999 stream_putc(s, pkt_safi);
1000
1001 if (bgp_debug_neighbor_events(peer))
1002 zlog_debug(
1003 "%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %s/%s",
1004 peer->host,
1005 action == CAPABILITY_ACTION_SET ? "Advertising"
1006 : "Removing",
1007 iana_afi2str(pkt_afi), iana_safi2str(pkt_safi));
1008 }
1009
1010 /* Set packet size. */
1011 bgp_packet_set_size(s);
1012
1013 /* Add packet to the peer. */
1014 bgp_packet_add(peer, s);
1015
1016 bgp_writes_on(peer);
1017 }
1018
1019 /* RFC1771 6.8 Connection collision detection. */
1020 static int bgp_collision_detect(struct peer *new, struct in_addr remote_id)
1021 {
1022 struct peer *peer;
1023
1024 /*
1025 * Upon receipt of an OPEN message, the local system must examine
1026 * all of its connections that are in the OpenConfirm state. A BGP
1027 * speaker may also examine connections in an OpenSent state if it
1028 * knows the BGP Identifier of the peer by means outside of the
1029 * protocol. If among these connections there is a connection to a
1030 * remote BGP speaker whose BGP Identifier equals the one in the
1031 * OPEN message, then the local system performs the following
1032 * collision resolution procedure:
1033 */
1034 peer = new->doppelganger;
1035 if (peer == NULL)
1036 return 0;
1037
1038 /*
1039 * Do not accept the new connection in Established or Clearing
1040 * states. Note that a peer GR is handled by closing the existing
1041 * connection upon receipt of new one.
1042 */
1043 if (peer_established(peer) || peer->status == Clearing) {
1044 bgp_notify_send(new, BGP_NOTIFY_CEASE,
1045 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1046 return -1;
1047 }
1048
1049 if ((peer->status != OpenConfirm) && (peer->status != OpenSent))
1050 return 0;
1051
1052 /*
1053 * 1. The BGP Identifier of the local system is
1054 * compared to the BGP Identifier of the remote
1055 * system (as specified in the OPEN message).
1056 *
1057 * If the BGP Identifiers of the peers
1058 * involved in the connection collision
1059 * are identical, then the connection
1060 * initiated by the BGP speaker with the
1061 * larger AS number is preserved.
1062 */
1063 if (ntohl(peer->local_id.s_addr) < ntohl(remote_id.s_addr)
1064 || (ntohl(peer->local_id.s_addr) == ntohl(remote_id.s_addr)
1065 && peer->local_as < peer->as))
1066 if (!CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) {
1067 /*
1068 * 2. If the value of the local BGP
1069 * Identifier is less than the remote one,
1070 * the local system closes BGP connection
1071 * that already exists (the one that is
1072 * already in the OpenConfirm state),
1073 * and accepts BGP connection initiated by
1074 * the remote system.
1075 */
1076 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
1077 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1078 return 1;
1079 } else {
1080 bgp_notify_send(new, BGP_NOTIFY_CEASE,
1081 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1082 return -1;
1083 }
1084 else {
1085 if (ntohl(peer->local_id.s_addr) == ntohl(remote_id.s_addr)
1086 && peer->local_as == peer->as)
1087 flog_err(EC_BGP_ROUTER_ID_SAME,
1088 "Peer's router-id %pI4 is the same as ours",
1089 &remote_id);
1090
1091 /*
1092 * 3. Otherwise, the local system closes newly
1093 * created BGP connection (the one associated with the
1094 * newly received OPEN message), and continues to use
1095 * the existing one (the one that is already in the
1096 * OpenConfirm state).
1097 */
1098 if (CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) {
1099 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
1100 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1101 return 1;
1102 } else {
1103 bgp_notify_send(new, BGP_NOTIFY_CEASE,
1104 BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
1105 return -1;
1106 }
1107 }
1108 }
1109
1110 /* Packet processing routines ---------------------------------------------- */
1111 /*
1112 * This is a family of functions designed to be called from
1113 * bgp_process_packet(). These functions all share similar behavior and should
1114 * adhere to the following invariants and restrictions:
1115 *
1116 * Return codes
1117 * ------------
1118 * The return code of any one of those functions should be one of the FSM event
1119 * codes specified in bgpd.h. If a NOTIFY was sent, this event code MUST be
1120 * BGP_Stop. Otherwise, the code SHOULD correspond to the function's expected
1121 * packet type. For example, bgp_open_receive() should return BGP_Stop upon
1122 * error and Receive_OPEN_message otherwise.
1123 *
1124 * If no action is necessary, the correct return code is BGP_PACKET_NOOP as
1125 * defined below.
1126 *
1127 * Side effects
1128 * ------------
1129 * - May send NOTIFY messages
1130 * - May not modify peer->status
1131 * - May not call bgp_event_update()
1132 */
1133
1134 #define BGP_PACKET_NOOP 0
1135
1136 /**
1137 * Process BGP OPEN message for peer.
1138 *
1139 * If any errors are encountered in the OPEN message, immediately sends NOTIFY
1140 * and returns BGP_Stop.
1141 *
1142 * @param peer
1143 * @param size size of the packet
1144 * @return as in summary
1145 */
1146 static int bgp_open_receive(struct peer *peer, bgp_size_t size)
1147 {
1148 int ret;
1149 uint8_t version;
1150 uint16_t optlen;
1151 uint16_t holdtime;
1152 uint16_t send_holdtime;
1153 as_t remote_as;
1154 as_t as4 = 0, as4_be;
1155 struct in_addr remote_id;
1156 int mp_capability;
1157 uint8_t notify_data_remote_as[2];
1158 uint8_t notify_data_remote_as4[4];
1159 uint8_t notify_data_remote_id[4];
1160 uint16_t *holdtime_ptr;
1161
1162 /* Parse open packet. */
1163 version = stream_getc(peer->curr);
1164 memcpy(notify_data_remote_as, stream_pnt(peer->curr), 2);
1165 remote_as = stream_getw(peer->curr);
1166 holdtime_ptr = (uint16_t *)stream_pnt(peer->curr);
1167 holdtime = stream_getw(peer->curr);
1168 memcpy(notify_data_remote_id, stream_pnt(peer->curr), 4);
1169 remote_id.s_addr = stream_get_ipv4(peer->curr);
1170
1171 /* BEGIN to read the capability here, but dont do it yet */
1172 mp_capability = 0;
1173 optlen = stream_getc(peer->curr);
1174
1175 /* Extended Optional Parameters Length for BGP OPEN Message */
1176 if (optlen == BGP_OPEN_NON_EXT_OPT_LEN
1177 || CHECK_FLAG(peer->flags, PEER_FLAG_EXTENDED_OPT_PARAMS)) {
1178 uint8_t opttype;
1179
1180 opttype = stream_getc(peer->curr);
1181 if (opttype == BGP_OPEN_NON_EXT_OPT_TYPE_EXTENDED_LENGTH) {
1182 optlen = stream_getw(peer->curr);
1183 SET_FLAG(peer->sflags,
1184 PEER_STATUS_EXT_OPT_PARAMS_LENGTH);
1185 }
1186 }
1187
1188 /* Receive OPEN message log */
1189 if (bgp_debug_neighbor_events(peer))
1190 zlog_debug(
1191 "%s rcv OPEN%s, version %d, remote-as (in open) %u, holdtime %d, id %pI4",
1192 peer->host,
1193 CHECK_FLAG(peer->sflags,
1194 PEER_STATUS_EXT_OPT_PARAMS_LENGTH)
1195 ? " (Extended)"
1196 : "",
1197 version, remote_as, holdtime, &remote_id);
1198
1199 if (optlen != 0) {
1200 /* If not enough bytes, it is an error. */
1201 if (STREAM_READABLE(peer->curr) < optlen) {
1202 flog_err(EC_BGP_PKT_OPEN,
1203 "%s: stream has not enough bytes (%u)",
1204 peer->host, optlen);
1205 bgp_notify_send(peer, BGP_NOTIFY_OPEN_ERR,
1206 BGP_NOTIFY_OPEN_MALFORMED_ATTR);
1207 return BGP_Stop;
1208 }
1209
1210 /* We need the as4 capability value *right now* because
1211 * if it is there, we have not got the remote_as yet, and
1212 * without
1213 * that we do not know which peer is connecting to us now.
1214 */
1215 as4 = peek_for_as4_capability(peer, optlen);
1216 }
1217
1218 as4_be = htonl(as4);
1219 memcpy(notify_data_remote_as4, &as4_be, 4);
1220
1221 /* Just in case we have a silly peer who sends AS4 capability set to 0
1222 */
1223 if (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV) && !as4) {
1224 flog_err(EC_BGP_PKT_OPEN,
1225 "%s bad OPEN, got AS4 capability, but AS4 set to 0",
1226 peer->host);
1227 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1228 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1229 notify_data_remote_as4, 4);
1230 return BGP_Stop;
1231 }
1232
1233 /* Codification of AS 0 Processing */
1234 if (remote_as == BGP_AS_ZERO) {
1235 flog_err(EC_BGP_PKT_OPEN, "%s bad OPEN, got AS set to 0",
1236 peer->host);
1237 bgp_notify_send(peer, BGP_NOTIFY_OPEN_ERR,
1238 BGP_NOTIFY_OPEN_BAD_PEER_AS);
1239 return BGP_Stop;
1240 }
1241
1242 if (remote_as == BGP_AS_TRANS) {
1243 /* Take the AS4 from the capability. We must have received the
1244 * capability now! Otherwise we have a asn16 peer who uses
1245 * BGP_AS_TRANS, for some unknown reason.
1246 */
1247 if (as4 == BGP_AS_TRANS) {
1248 flog_err(
1249 EC_BGP_PKT_OPEN,
1250 "%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
1251 peer->host);
1252 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1253 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1254 notify_data_remote_as4, 4);
1255 return BGP_Stop;
1256 }
1257
1258 if (!as4 && BGP_DEBUG(as4, AS4))
1259 zlog_debug(
1260 "%s [AS4] OPEN remote_as is AS_TRANS, but no AS4. Odd, but proceeding.",
1261 peer->host);
1262 else if (as4 < BGP_AS_MAX && BGP_DEBUG(as4, AS4))
1263 zlog_debug(
1264 "%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits in 2-bytes, very odd peer.",
1265 peer->host, as4);
1266 if (as4)
1267 remote_as = as4;
1268 } else {
1269 /* We may have a partner with AS4 who has an asno < BGP_AS_MAX
1270 */
1271 /* If we have got the capability, peer->as4cap must match
1272 * remote_as */
1273 if (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV)
1274 && as4 != remote_as) {
1275 /* raise error, log this, close session */
1276 flog_err(
1277 EC_BGP_PKT_OPEN,
1278 "%s bad OPEN, got AS4 capability, but remote_as %u mismatch with 16bit 'myasn' %u in open",
1279 peer->host, as4, remote_as);
1280 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1281 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1282 notify_data_remote_as4, 4);
1283 return BGP_Stop;
1284 }
1285 }
1286
1287 /* rfc6286:
1288 * If the BGP Identifier field of the OPEN message
1289 * is zero, or if it is the same as the BGP Identifier
1290 * of the local BGP speaker and the message is from an
1291 * internal peer, then the Error Subcode is set to
1292 * "Bad BGP Identifier".
1293 */
1294 if (remote_id.s_addr == INADDR_ANY
1295 || (peer->sort == BGP_PEER_IBGP
1296 && ntohl(peer->local_id.s_addr) == ntohl(remote_id.s_addr))) {
1297 if (bgp_debug_neighbor_events(peer))
1298 zlog_debug("%s bad OPEN, wrong router identifier %pI4",
1299 peer->host, &remote_id);
1300 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1301 BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
1302 notify_data_remote_id, 4);
1303 return BGP_Stop;
1304 }
1305
1306 /* Peer BGP version check. */
1307 if (version != BGP_VERSION_4) {
1308 uint16_t maxver = htons(BGP_VERSION_4);
1309 /* XXX this reply may not be correct if version < 4 XXX */
1310 if (bgp_debug_neighbor_events(peer))
1311 zlog_debug(
1312 "%s bad protocol version, remote requested %d, local request %d",
1313 peer->host, version, BGP_VERSION_4);
1314 /* Data must be in network byte order here */
1315 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1316 BGP_NOTIFY_OPEN_UNSUP_VERSION,
1317 (uint8_t *)&maxver, 2);
1318 return BGP_Stop;
1319 }
1320
1321 /* Check neighbor as number. */
1322 if (peer->as_type == AS_UNSPECIFIED) {
1323 if (bgp_debug_neighbor_events(peer))
1324 zlog_debug(
1325 "%s bad OPEN, remote AS is unspecified currently",
1326 peer->host);
1327 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1328 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1329 notify_data_remote_as, 2);
1330 return BGP_Stop;
1331 } else if (peer->as_type == AS_INTERNAL) {
1332 if (remote_as != peer->bgp->as) {
1333 if (bgp_debug_neighbor_events(peer))
1334 zlog_debug(
1335 "%s bad OPEN, remote AS is %u, internal specified",
1336 peer->host, remote_as);
1337 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1338 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1339 notify_data_remote_as, 2);
1340 return BGP_Stop;
1341 }
1342 peer->as = peer->local_as;
1343 } else if (peer->as_type == AS_EXTERNAL) {
1344 if (remote_as == peer->bgp->as) {
1345 if (bgp_debug_neighbor_events(peer))
1346 zlog_debug(
1347 "%s bad OPEN, remote AS is %u, external specified",
1348 peer->host, remote_as);
1349 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1350 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1351 notify_data_remote_as, 2);
1352 return BGP_Stop;
1353 }
1354 peer->as = remote_as;
1355 } else if ((peer->as_type == AS_SPECIFIED) && (remote_as != peer->as)) {
1356 if (bgp_debug_neighbor_events(peer))
1357 zlog_debug("%s bad OPEN, remote AS is %u, expected %u",
1358 peer->host, remote_as, peer->as);
1359 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1360 BGP_NOTIFY_OPEN_BAD_PEER_AS,
1361 notify_data_remote_as, 2);
1362 return BGP_Stop;
1363 }
1364
1365 /*
1366 * When collision is detected and this peer is closed.
1367 * Return immediately.
1368 */
1369 ret = bgp_collision_detect(peer, remote_id);
1370 if (ret < 0)
1371 return BGP_Stop;
1372
1373 /* Get sockname. */
1374 if (bgp_getsockname(peer) < 0) {
1375 flog_err_sys(EC_LIB_SOCKET,
1376 "%s: bgp_getsockname() failed for peer: %s",
1377 __func__, peer->host);
1378 return BGP_Stop;
1379 }
1380
1381 /* Set remote router-id */
1382 peer->remote_id = remote_id;
1383
1384 /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
1385 calculate the value of the Hold Timer by using the smaller of its
1386 configured Hold Time and the Hold Time received in the OPEN message.
1387 The Hold Time MUST be either zero or at least three seconds. An
1388 implementation may reject connections on the basis of the Hold Time.
1389 */
1390
1391 if (holdtime < 3 && holdtime != 0) {
1392 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1393 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME,
1394 (uint8_t *)holdtime_ptr, 2);
1395 return BGP_Stop;
1396 }
1397
1398 /* Send notification message when Hold Time received in the OPEN message
1399 * is smaller than configured minimum Hold Time. */
1400 if (holdtime < peer->bgp->default_min_holdtime
1401 && peer->bgp->default_min_holdtime != 0) {
1402 bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR,
1403 BGP_NOTIFY_OPEN_UNACEP_HOLDTIME,
1404 (uint8_t *)holdtime_ptr, 2);
1405 return BGP_Stop;
1406 }
1407
1408 /* From the rfc: A reasonable maximum time between KEEPALIVE messages
1409 would be one third of the Hold Time interval. KEEPALIVE messages
1410 MUST NOT be sent more frequently than one per second. An
1411 implementation MAY adjust the rate at which it sends KEEPALIVE
1412 messages as a function of the Hold Time interval. */
1413
1414 if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER))
1415 send_holdtime = peer->holdtime;
1416 else
1417 send_holdtime = peer->bgp->default_holdtime;
1418
1419 if (holdtime < send_holdtime)
1420 peer->v_holdtime = holdtime;
1421 else
1422 peer->v_holdtime = send_holdtime;
1423
1424 /* Set effective keepalive to 1/3 the effective holdtime.
1425 * Use configured keeplive when < effective keepalive.
1426 */
1427 peer->v_keepalive = peer->v_holdtime / 3;
1428 if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER)) {
1429 if (peer->keepalive && peer->keepalive < peer->v_keepalive)
1430 peer->v_keepalive = peer->keepalive;
1431 } else {
1432 if (peer->bgp->default_keepalive
1433 && peer->bgp->default_keepalive < peer->v_keepalive)
1434 peer->v_keepalive = peer->bgp->default_keepalive;
1435 }
1436
1437 /* Open option part parse. */
1438 if (optlen != 0) {
1439 if (bgp_open_option_parse(peer, optlen, &mp_capability) < 0)
1440 return BGP_Stop;
1441 } else {
1442 if (bgp_debug_neighbor_events(peer))
1443 zlog_debug("%s rcvd OPEN w/ OPTION parameter len: 0",
1444 peer->host);
1445 }
1446
1447 /*
1448 * Assume that the peer supports the locally configured set of
1449 * AFI/SAFIs if the peer did not send us any Mulitiprotocol
1450 * capabilities, or if 'override-capability' is configured.
1451 */
1452 if (!mp_capability
1453 || CHECK_FLAG(peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) {
1454 peer->afc_nego[AFI_IP][SAFI_UNICAST] =
1455 peer->afc[AFI_IP][SAFI_UNICAST];
1456 peer->afc_nego[AFI_IP][SAFI_MULTICAST] =
1457 peer->afc[AFI_IP][SAFI_MULTICAST];
1458 peer->afc_nego[AFI_IP][SAFI_LABELED_UNICAST] =
1459 peer->afc[AFI_IP][SAFI_LABELED_UNICAST];
1460 peer->afc_nego[AFI_IP][SAFI_FLOWSPEC] =
1461 peer->afc[AFI_IP][SAFI_FLOWSPEC];
1462 peer->afc_nego[AFI_IP6][SAFI_UNICAST] =
1463 peer->afc[AFI_IP6][SAFI_UNICAST];
1464 peer->afc_nego[AFI_IP6][SAFI_MULTICAST] =
1465 peer->afc[AFI_IP6][SAFI_MULTICAST];
1466 peer->afc_nego[AFI_IP6][SAFI_LABELED_UNICAST] =
1467 peer->afc[AFI_IP6][SAFI_LABELED_UNICAST];
1468 peer->afc_nego[AFI_L2VPN][SAFI_EVPN] =
1469 peer->afc[AFI_L2VPN][SAFI_EVPN];
1470 peer->afc_nego[AFI_IP6][SAFI_FLOWSPEC] =
1471 peer->afc[AFI_IP6][SAFI_FLOWSPEC];
1472 }
1473
1474 /* Verify valid local address present based on negotiated
1475 * address-families. */
1476 if (peer->afc_nego[AFI_IP][SAFI_UNICAST]
1477 || peer->afc_nego[AFI_IP][SAFI_LABELED_UNICAST]
1478 || peer->afc_nego[AFI_IP][SAFI_MULTICAST]
1479 || peer->afc_nego[AFI_IP][SAFI_MPLS_VPN]
1480 || peer->afc_nego[AFI_IP][SAFI_ENCAP]) {
1481 if (peer->nexthop.v4.s_addr == INADDR_ANY) {
1482 #if defined(HAVE_CUMULUS)
1483 zlog_warn("%s: No local IPv4 addr, BGP routing may not work",
1484 peer->host);
1485 #endif
1486 }
1487 }
1488 if (peer->afc_nego[AFI_IP6][SAFI_UNICAST]
1489 || peer->afc_nego[AFI_IP6][SAFI_LABELED_UNICAST]
1490 || peer->afc_nego[AFI_IP6][SAFI_MULTICAST]
1491 || peer->afc_nego[AFI_IP6][SAFI_MPLS_VPN]
1492 || peer->afc_nego[AFI_IP6][SAFI_ENCAP]) {
1493 if (IN6_IS_ADDR_UNSPECIFIED(&peer->nexthop.v6_global)) {
1494 #if defined(HAVE_CUMULUS)
1495 zlog_warn("%s: No local IPv6 address, BGP routing may not work",
1496 peer->host);
1497 #endif
1498 }
1499 }
1500 peer->rtt = sockopt_tcp_rtt(peer->fd);
1501
1502 return Receive_OPEN_message;
1503 }
1504
1505 /**
1506 * Process BGP KEEPALIVE message for peer.
1507 *
1508 * @param peer
1509 * @param size size of the packet
1510 * @return as in summary
1511 */
1512 static int bgp_keepalive_receive(struct peer *peer, bgp_size_t size)
1513 {
1514 if (bgp_debug_keepalive(peer))
1515 zlog_debug("%s KEEPALIVE rcvd", peer->host);
1516
1517 bgp_update_implicit_eors(peer);
1518
1519 peer->rtt = sockopt_tcp_rtt(peer->fd);
1520
1521 /* If the peer's RTT is higher than expected, shutdown
1522 * the peer automatically.
1523 */
1524 if (CHECK_FLAG(peer->flags, PEER_FLAG_RTT_SHUTDOWN)
1525 && peer->rtt > peer->rtt_expected) {
1526
1527 peer->rtt_keepalive_rcv++;
1528
1529 if (peer->rtt_keepalive_rcv > peer->rtt_keepalive_conf) {
1530 zlog_warn(
1531 "%s shutdown due to high round-trip-time (%dms > %dms)",
1532 peer->host, peer->rtt, peer->rtt_expected);
1533 peer_flag_set(peer, PEER_FLAG_SHUTDOWN);
1534 }
1535 } else {
1536 if (peer->rtt_keepalive_rcv)
1537 peer->rtt_keepalive_rcv--;
1538 }
1539
1540 return Receive_KEEPALIVE_message;
1541 }
1542
1543 static int bgp_refresh_stalepath_timer_expire(struct thread *thread)
1544 {
1545 struct peer_af *paf;
1546
1547 paf = THREAD_ARG(thread);
1548
1549 afi_t afi = paf->afi;
1550 safi_t safi = paf->safi;
1551 struct peer *peer = paf->peer;
1552
1553 peer->t_refresh_stalepath = NULL;
1554
1555 if (peer->nsf[afi][safi])
1556 bgp_clear_stale_route(peer, afi, safi);
1557
1558 if (bgp_debug_neighbor_events(peer))
1559 zlog_debug("%s: route-refresh (BoRR) timer for %s/%s expired",
1560 peer->host, afi2str(afi), safi2str(safi));
1561
1562 bgp_timer_set(peer);
1563
1564 return 0;
1565 }
1566
1567 /**
1568 * Process BGP UPDATE message for peer.
1569 *
1570 * Parses UPDATE and creates attribute object.
1571 *
1572 * @param peer
1573 * @param size size of the packet
1574 * @return as in summary
1575 */
1576 static int bgp_update_receive(struct peer *peer, bgp_size_t size)
1577 {
1578 int ret, nlri_ret;
1579 uint8_t *end;
1580 struct stream *s;
1581 struct attr attr;
1582 bgp_size_t attribute_len;
1583 bgp_size_t update_len;
1584 bgp_size_t withdraw_len;
1585 bool restart = false;
1586
1587 enum NLRI_TYPES {
1588 NLRI_UPDATE,
1589 NLRI_WITHDRAW,
1590 NLRI_MP_UPDATE,
1591 NLRI_MP_WITHDRAW,
1592 NLRI_TYPE_MAX
1593 };
1594 struct bgp_nlri nlris[NLRI_TYPE_MAX];
1595
1596 /* Status must be Established. */
1597 if (!peer_established(peer)) {
1598 flog_err(EC_BGP_INVALID_STATUS,
1599 "%s [FSM] Update packet received under status %s",
1600 peer->host,
1601 lookup_msg(bgp_status_msg, peer->status, NULL));
1602 bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR,
1603 bgp_fsm_error_subcode(peer->status));
1604 return BGP_Stop;
1605 }
1606
1607 /* Set initial values. */
1608 memset(&attr, 0, sizeof(struct attr));
1609 attr.label_index = BGP_INVALID_LABEL_INDEX;
1610 attr.label = MPLS_INVALID_LABEL;
1611 memset(&nlris, 0, sizeof(nlris));
1612 memset(peer->rcvd_attr_str, 0, BUFSIZ);
1613 peer->rcvd_attr_printed = 0;
1614
1615 s = peer->curr;
1616 end = stream_pnt(s) + size;
1617
1618 /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
1619 Length is too large (i.e., if Unfeasible Routes Length + Total
1620 Attribute Length + 23 exceeds the message Length), then the Error
1621 Subcode is set to Malformed Attribute List. */
1622 if (stream_pnt(s) + 2 > end) {
1623 flog_err(EC_BGP_UPDATE_RCV,
1624 "%s [Error] Update packet error (packet length is short for unfeasible length)",
1625 peer->host);
1626 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
1627 BGP_NOTIFY_UPDATE_MAL_ATTR);
1628 return BGP_Stop;
1629 }
1630
1631 /* Unfeasible Route Length. */
1632 withdraw_len = stream_getw(s);
1633
1634 /* Unfeasible Route Length check. */
1635 if (stream_pnt(s) + withdraw_len > end) {
1636 flog_err(EC_BGP_UPDATE_RCV,
1637 "%s [Error] Update packet error (packet unfeasible length overflow %d)",
1638 peer->host, withdraw_len);
1639 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
1640 BGP_NOTIFY_UPDATE_MAL_ATTR);
1641 return BGP_Stop;
1642 }
1643
1644 /* Unfeasible Route packet format check. */
1645 if (withdraw_len > 0) {
1646 nlris[NLRI_WITHDRAW].afi = AFI_IP;
1647 nlris[NLRI_WITHDRAW].safi = SAFI_UNICAST;
1648 nlris[NLRI_WITHDRAW].nlri = stream_pnt(s);
1649 nlris[NLRI_WITHDRAW].length = withdraw_len;
1650 stream_forward_getp(s, withdraw_len);
1651 }
1652
1653 /* Attribute total length check. */
1654 if (stream_pnt(s) + 2 > end) {
1655 flog_warn(
1656 EC_BGP_UPDATE_PACKET_SHORT,
1657 "%s [Error] Packet Error (update packet is short for attribute length)",
1658 peer->host);
1659 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
1660 BGP_NOTIFY_UPDATE_MAL_ATTR);
1661 return BGP_Stop;
1662 }
1663
1664 /* Fetch attribute total length. */
1665 attribute_len = stream_getw(s);
1666
1667 /* Attribute length check. */
1668 if (stream_pnt(s) + attribute_len > end) {
1669 flog_warn(
1670 EC_BGP_UPDATE_PACKET_LONG,
1671 "%s [Error] Packet Error (update packet attribute length overflow %d)",
1672 peer->host, attribute_len);
1673 bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR,
1674 BGP_NOTIFY_UPDATE_MAL_ATTR);
1675 return BGP_Stop;
1676 }
1677
1678 /* Certain attribute parsing errors should not be considered bad enough
1679 * to reset the session for, most particularly any partial/optional
1680 * attributes that have 'tunneled' over speakers that don't understand
1681 * them. Instead we withdraw only the prefix concerned.
1682 *
1683 * Complicates the flow a little though..
1684 */
1685 bgp_attr_parse_ret_t attr_parse_ret = BGP_ATTR_PARSE_PROCEED;
1686 /* This define morphs the update case into a withdraw when lower levels
1687 * have signalled an error condition where this is best.
1688 */
1689 #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
1690
1691 /* Parse attribute when it exists. */
1692 if (attribute_len) {
1693 attr_parse_ret = bgp_attr_parse(peer, &attr, attribute_len,
1694 &nlris[NLRI_MP_UPDATE],
1695 &nlris[NLRI_MP_WITHDRAW]);
1696 if (attr_parse_ret == BGP_ATTR_PARSE_ERROR) {
1697 bgp_attr_unintern_sub(&attr);
1698 return BGP_Stop;
1699 }
1700 }
1701
1702 /* Logging the attribute. */
1703 if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW
1704 || BGP_DEBUG(update, UPDATE_IN)
1705 || BGP_DEBUG(update, UPDATE_PREFIX)) {
1706 ret = bgp_dump_attr(&attr, peer->rcvd_attr_str,
1707 sizeof(peer->rcvd_attr_str));
1708
1709 peer->stat_upd_7606++;
1710
1711 if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW)
1712 flog_err(
1713 EC_BGP_UPDATE_RCV,
1714 "%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
1715 peer->host);
1716
1717 if (ret && bgp_debug_update(peer, NULL, NULL, 1)) {
1718 zlog_debug("%s rcvd UPDATE w/ attr: %s", peer->host,
1719 peer->rcvd_attr_str);
1720 peer->rcvd_attr_printed = 1;
1721 }
1722 }
1723
1724 /* Network Layer Reachability Information. */
1725 update_len = end - stream_pnt(s);
1726
1727 if (update_len) {
1728 /* Set NLRI portion to structure. */
1729 nlris[NLRI_UPDATE].afi = AFI_IP;
1730 nlris[NLRI_UPDATE].safi = SAFI_UNICAST;
1731 nlris[NLRI_UPDATE].nlri = stream_pnt(s);
1732 nlris[NLRI_UPDATE].length = update_len;
1733 stream_forward_getp(s, update_len);
1734
1735 if (CHECK_FLAG(attr.flag, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI))) {
1736 /*
1737 * We skipped nexthop attribute validation earlier so
1738 * validate the nexthop now.
1739 */
1740 if (bgp_attr_nexthop_valid(peer, &attr) < 0) {
1741 bgp_attr_unintern_sub(&attr);
1742 return BGP_Stop;
1743 }
1744 }
1745 }
1746
1747 if (BGP_DEBUG(update, UPDATE_IN))
1748 zlog_debug("%s rcvd UPDATE wlen %d attrlen %d alen %d",
1749 peer->host, withdraw_len, attribute_len, update_len);
1750
1751 /* Parse any given NLRIs */
1752 for (int i = NLRI_UPDATE; i < NLRI_TYPE_MAX; i++) {
1753 if (!nlris[i].nlri)
1754 continue;
1755
1756 /* NLRI is processed iff the peer if configured for the specific
1757 * afi/safi */
1758 if (!peer->afc[nlris[i].afi][nlris[i].safi]) {
1759 zlog_info(
1760 "%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u",
1761 peer->host, nlris[i].afi, nlris[i].safi);
1762 continue;
1763 }
1764
1765 /* EoR handled later */
1766 if (nlris[i].length == 0)
1767 continue;
1768
1769 switch (i) {
1770 case NLRI_UPDATE:
1771 case NLRI_MP_UPDATE:
1772 nlri_ret = bgp_nlri_parse(peer, NLRI_ATTR_ARG,
1773 &nlris[i], 0);
1774 break;
1775 case NLRI_WITHDRAW:
1776 case NLRI_MP_WITHDRAW:
1777 nlri_ret = bgp_nlri_parse(peer, &attr, &nlris[i], 1);
1778 break;
1779 default:
1780 nlri_ret = BGP_NLRI_PARSE_ERROR;
1781 }
1782
1783 if (nlri_ret < BGP_NLRI_PARSE_OK
1784 && nlri_ret != BGP_NLRI_PARSE_ERROR_PREFIX_OVERFLOW) {
1785 flog_err(EC_BGP_UPDATE_RCV,
1786 "%s [Error] Error parsing NLRI", peer->host);
1787 if (peer_established(peer))
1788 bgp_notify_send(
1789 peer, BGP_NOTIFY_UPDATE_ERR,
1790 i <= NLRI_WITHDRAW
1791 ? BGP_NOTIFY_UPDATE_INVAL_NETWORK
1792 : BGP_NOTIFY_UPDATE_OPT_ATTR_ERR);
1793 bgp_attr_unintern_sub(&attr);
1794 return BGP_Stop;
1795 }
1796 }
1797
1798 /* EoR checks
1799 *
1800 * Non-MP IPv4/Unicast EoR is a completely empty UPDATE
1801 * and MP EoR should have only an empty MP_UNREACH
1802 */
1803 if ((!update_len && !withdraw_len && nlris[NLRI_MP_UPDATE].length == 0)
1804 || (attr_parse_ret == BGP_ATTR_PARSE_EOR)) {
1805 afi_t afi = 0;
1806 safi_t safi;
1807 struct graceful_restart_info *gr_info;
1808
1809 /* Restarting router */
1810 if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer)
1811 && BGP_PEER_RESTARTING_MODE(peer))
1812 restart = true;
1813
1814 /* Non-MP IPv4/Unicast is a completely emtpy UPDATE - already
1815 * checked
1816 * update and withdraw NLRI lengths are 0.
1817 */
1818 if (!attribute_len) {
1819 afi = AFI_IP;
1820 safi = SAFI_UNICAST;
1821 } else if (attr.flag & ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI)
1822 && nlris[NLRI_MP_WITHDRAW].length == 0) {
1823 afi = nlris[NLRI_MP_WITHDRAW].afi;
1824 safi = nlris[NLRI_MP_WITHDRAW].safi;
1825 } else if (attr_parse_ret == BGP_ATTR_PARSE_EOR) {
1826 afi = nlris[NLRI_MP_UPDATE].afi;
1827 safi = nlris[NLRI_MP_UPDATE].safi;
1828 }
1829
1830 if (afi && peer->afc[afi][safi]) {
1831 struct vrf *vrf = vrf_lookup_by_id(peer->bgp->vrf_id);
1832
1833 /* End-of-RIB received */
1834 if (!CHECK_FLAG(peer->af_sflags[afi][safi],
1835 PEER_STATUS_EOR_RECEIVED)) {
1836 SET_FLAG(peer->af_sflags[afi][safi],
1837 PEER_STATUS_EOR_RECEIVED);
1838 bgp_update_explicit_eors(peer);
1839 /* Update graceful restart information */
1840 gr_info = &(peer->bgp->gr_info[afi][safi]);
1841 if (restart)
1842 gr_info->eor_received++;
1843 /* If EOR received from all peers and selection
1844 * deferral timer is running, cancel the timer
1845 * and invoke the best path calculation
1846 */
1847 if (gr_info->eor_required
1848 == gr_info->eor_received) {
1849 if (bgp_debug_neighbor_events(peer))
1850 zlog_debug(
1851 "%s %d, %s %d",
1852 "EOR REQ",
1853 gr_info->eor_required,
1854 "EOR RCV",
1855 gr_info->eor_received);
1856 BGP_TIMER_OFF(
1857 gr_info->t_select_deferral);
1858 gr_info->eor_required = 0;
1859 gr_info->eor_received = 0;
1860 /* Best path selection */
1861 if (bgp_best_path_select_defer(
1862 peer->bgp, afi, safi)
1863 < 0)
1864 return BGP_Stop;
1865 }
1866 }
1867
1868 /* NSF delete stale route */
1869 if (peer->nsf[afi][safi])
1870 bgp_clear_stale_route(peer, afi, safi);
1871
1872 zlog_info(
1873 "%s: rcvd End-of-RIB for %s from %s in vrf %s",
1874 __func__, get_afi_safi_str(afi, safi, false),
1875 peer->host, vrf ? vrf->name : VRF_DEFAULT_NAME);
1876 }
1877 }
1878
1879 /* Everything is done. We unintern temporary structures which
1880 interned in bgp_attr_parse(). */
1881 bgp_attr_unintern_sub(&attr);
1882
1883 peer->update_time = bgp_clock();
1884
1885 /* Notify BGP Conditional advertisement scanner process */
1886 peer->advmap_table_change = true;
1887
1888 return Receive_UPDATE_message;
1889 }
1890
1891 /**
1892 * Process BGP NOTIFY message for peer.
1893 *
1894 * @param peer
1895 * @param size size of the packet
1896 * @return as in summary
1897 */
1898 static int bgp_notify_receive(struct peer *peer, bgp_size_t size)
1899 {
1900 struct bgp_notify bgp_notify;
1901
1902 if (peer->notify.data) {
1903 XFREE(MTYPE_TMP, peer->notify.data);
1904 peer->notify.length = 0;
1905 }
1906
1907 bgp_notify.code = stream_getc(peer->curr);
1908 bgp_notify.subcode = stream_getc(peer->curr);
1909 bgp_notify.length = size - 2;
1910 bgp_notify.data = NULL;
1911 bgp_notify.raw_data = NULL;
1912
1913 /* Preserv notify code and sub code. */
1914 peer->notify.code = bgp_notify.code;
1915 peer->notify.subcode = bgp_notify.subcode;
1916 /* For further diagnostic record returned Data. */
1917 if (bgp_notify.length) {
1918 peer->notify.length = size - 2;
1919 peer->notify.data = XMALLOC(MTYPE_TMP, size - 2);
1920 memcpy(peer->notify.data, stream_pnt(peer->curr), size - 2);
1921 }
1922
1923 /* For debug */
1924 {
1925 int i;
1926 int first = 0;
1927 char c[4];
1928
1929 if (bgp_notify.length) {
1930 bgp_notify.data =
1931 XMALLOC(MTYPE_TMP, bgp_notify.length * 3);
1932 for (i = 0; i < bgp_notify.length; i++)
1933 if (first) {
1934 snprintf(c, sizeof(c), " %02x",
1935 stream_getc(peer->curr));
1936
1937 strlcat(bgp_notify.data, c,
1938 bgp_notify.length * 3);
1939
1940 } else {
1941 first = 1;
1942 snprintf(c, sizeof(c), "%02x",
1943 stream_getc(peer->curr));
1944
1945 strlcpy(bgp_notify.data, c,
1946 bgp_notify.length * 3);
1947 }
1948 bgp_notify.raw_data = (uint8_t *)peer->notify.data;
1949 }
1950
1951 bgp_notify_print(peer, &bgp_notify, "received");
1952 if (bgp_notify.data) {
1953 XFREE(MTYPE_TMP, bgp_notify.data);
1954 bgp_notify.length = 0;
1955 }
1956 }
1957
1958 /* peer count update */
1959 atomic_fetch_add_explicit(&peer->notify_in, 1, memory_order_relaxed);
1960
1961 peer->last_reset = PEER_DOWN_NOTIFY_RECEIVED;
1962
1963 /* We have to check for Notify with Unsupported Optional Parameter.
1964 in that case we fallback to open without the capability option.
1965 But this done in bgp_stop. We just mark it here to avoid changing
1966 the fsm tables. */
1967 if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR
1968 && bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM)
1969 UNSET_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
1970
1971 bgp_peer_gr_flags_update(peer);
1972 BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp,
1973 peer->bgp->peer);
1974
1975 return Receive_NOTIFICATION_message;
1976 }
1977
1978 /**
1979 * Process BGP ROUTEREFRESH message for peer.
1980 *
1981 * @param peer
1982 * @param size size of the packet
1983 * @return as in summary
1984 */
1985 static int bgp_route_refresh_receive(struct peer *peer, bgp_size_t size)
1986 {
1987 iana_afi_t pkt_afi;
1988 afi_t afi;
1989 iana_safi_t pkt_safi;
1990 safi_t safi;
1991 struct stream *s;
1992 struct peer_af *paf;
1993 struct update_group *updgrp;
1994 struct peer *updgrp_peer;
1995 uint8_t subtype;
1996 bool force_update = false;
1997 bgp_size_t msg_length =
1998 size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE);
1999
2000 /* If peer does not have the capability, send notification. */
2001 if (!CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_ADV)) {
2002 flog_err(EC_BGP_NO_CAP,
2003 "%s [Error] BGP route refresh is not enabled",
2004 peer->host);
2005 bgp_notify_send(peer, BGP_NOTIFY_HEADER_ERR,
2006 BGP_NOTIFY_HEADER_BAD_MESTYPE);
2007 return BGP_Stop;
2008 }
2009
2010 /* Status must be Established. */
2011 if (!peer_established(peer)) {
2012 flog_err(
2013 EC_BGP_INVALID_STATUS,
2014 "%s [Error] Route refresh packet received under status %s",
2015 peer->host,
2016 lookup_msg(bgp_status_msg, peer->status, NULL));
2017 bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR,
2018 bgp_fsm_error_subcode(peer->status));
2019 return BGP_Stop;
2020 }
2021
2022 s = peer->curr;
2023
2024 /* Parse packet. */
2025 pkt_afi = stream_getw(s);
2026 subtype = stream_getc(s);
2027 pkt_safi = stream_getc(s);
2028
2029 /* Convert AFI, SAFI to internal values and check. */
2030 if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi, &safi)) {
2031 zlog_info(
2032 "%s REFRESH_REQ for unrecognized afi/safi: %s/%s - ignored",
2033 peer->host, iana_afi2str(pkt_afi),
2034 iana_safi2str(pkt_safi));
2035 return BGP_PACKET_NOOP;
2036 }
2037
2038 if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) {
2039 uint8_t *end;
2040 uint8_t when_to_refresh;
2041 uint8_t orf_type;
2042 uint16_t orf_len;
2043
2044 if (subtype) {
2045 /* If the length, excluding the fixed-size message
2046 * header, of the received ROUTE-REFRESH message with
2047 * Message Subtype 1 and 2 is not 4, then the BGP
2048 * speaker MUST send a NOTIFICATION message with the
2049 * Error Code of "ROUTE-REFRESH Message Error" and the
2050 * subcode of "Invalid Message Length".
2051 */
2052 if (msg_length != 4) {
2053 zlog_err(
2054 "%s Enhanced Route Refresh message length error",
2055 peer->host);
2056 bgp_notify_send(
2057 peer, BGP_NOTIFY_ROUTE_REFRESH_ERR,
2058 BGP_NOTIFY_ROUTE_REFRESH_INVALID_MSG_LEN);
2059 }
2060
2061 /* When the BGP speaker receives a ROUTE-REFRESH message
2062 * with a "Message Subtype" field other than 0, 1, or 2,
2063 * it MUST ignore the received ROUTE-REFRESH message.
2064 */
2065 if (subtype > 2)
2066 zlog_err(
2067 "%s Enhanced Route Refresh invalid subtype",
2068 peer->host);
2069 }
2070
2071 if (msg_length < 5) {
2072 zlog_info("%s ORF route refresh length error",
2073 peer->host);
2074 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2075 BGP_NOTIFY_SUBCODE_UNSPECIFIC);
2076 return BGP_Stop;
2077 }
2078
2079 when_to_refresh = stream_getc(s);
2080 end = stream_pnt(s) + (size - 5);
2081
2082 while ((stream_pnt(s) + 2) < end) {
2083 orf_type = stream_getc(s);
2084 orf_len = stream_getw(s);
2085
2086 /* orf_len in bounds? */
2087 if ((stream_pnt(s) + orf_len) > end)
2088 break; /* XXX: Notify instead?? */
2089 if (orf_type == ORF_TYPE_PREFIX
2090 || orf_type == ORF_TYPE_PREFIX_OLD) {
2091 uint8_t *p_pnt = stream_pnt(s);
2092 uint8_t *p_end = stream_pnt(s) + orf_len;
2093 struct orf_prefix orfp;
2094 uint8_t common = 0;
2095 uint32_t seq;
2096 int psize;
2097 char name[BUFSIZ];
2098 int ret = CMD_SUCCESS;
2099
2100 if (bgp_debug_neighbor_events(peer)) {
2101 zlog_debug(
2102 "%s rcvd Prefixlist ORF(%d) length %d",
2103 peer->host, orf_type, orf_len);
2104 }
2105
2106 /* we're going to read at least 1 byte of common
2107 * ORF header,
2108 * and 7 bytes of ORF Address-filter entry from
2109 * the stream
2110 */
2111 if (orf_len < 7)
2112 break;
2113
2114 /* ORF prefix-list name */
2115 snprintf(name, sizeof(name), "%s.%d.%d",
2116 peer->host, afi, safi);
2117
2118 while (p_pnt < p_end) {
2119 /* If the ORF entry is malformed, want
2120 * to read as much of it
2121 * as possible without going beyond the
2122 * bounds of the entry,
2123 * to maximise debug information.
2124 */
2125 int ok;
2126 memset(&orfp, 0,
2127 sizeof(struct orf_prefix));
2128 common = *p_pnt++;
2129 /* after ++: p_pnt <= p_end */
2130 if (common
2131 & ORF_COMMON_PART_REMOVE_ALL) {
2132 if (bgp_debug_neighbor_events(
2133 peer))
2134 zlog_debug(
2135 "%s rcvd Remove-All pfxlist ORF request",
2136 peer->host);
2137 prefix_bgp_orf_remove_all(afi,
2138 name);
2139 break;
2140 }
2141 ok = ((uint32_t)(p_end - p_pnt)
2142 >= sizeof(uint32_t));
2143 if (ok) {
2144 memcpy(&seq, p_pnt,
2145 sizeof(uint32_t));
2146 p_pnt += sizeof(uint32_t);
2147 orfp.seq = ntohl(seq);
2148 } else
2149 p_pnt = p_end;
2150
2151 /* val checked in prefix_bgp_orf_set */
2152 if (p_pnt < p_end)
2153 orfp.ge = *p_pnt++;
2154
2155 /* val checked in prefix_bgp_orf_set */
2156 if (p_pnt < p_end)
2157 orfp.le = *p_pnt++;
2158
2159 if ((ok = (p_pnt < p_end)))
2160 orfp.p.prefixlen = *p_pnt++;
2161
2162 /* afi checked already */
2163 orfp.p.family = afi2family(afi);
2164
2165 /* 0 if not ok */
2166 psize = PSIZE(orfp.p.prefixlen);
2167 /* valid for family ? */
2168 if (psize > prefix_blen(&orfp.p)) {
2169 ok = 0;
2170 psize = prefix_blen(&orfp.p);
2171 }
2172 /* valid for packet ? */
2173 if (psize > (p_end - p_pnt)) {
2174 ok = 0;
2175 psize = p_end - p_pnt;
2176 }
2177
2178 if (psize > 0)
2179 memcpy(&orfp.p.u.prefix, p_pnt,
2180 psize);
2181 p_pnt += psize;
2182
2183 if (bgp_debug_neighbor_events(peer)) {
2184 char buf[INET6_BUFSIZ];
2185
2186 zlog_debug(
2187 "%s rcvd %s %s seq %u %s/%d ge %d le %d%s",
2188 peer->host,
2189 (common & ORF_COMMON_PART_REMOVE
2190 ? "Remove"
2191 : "Add"),
2192 (common & ORF_COMMON_PART_DENY
2193 ? "deny"
2194 : "permit"),
2195 orfp.seq,
2196 inet_ntop(
2197 orfp.p.family,
2198 &orfp.p.u.prefix,
2199 buf,
2200 INET6_BUFSIZ),
2201 orfp.p.prefixlen,
2202 orfp.ge, orfp.le,
2203 ok ? "" : " MALFORMED");
2204 }
2205
2206 if (ok)
2207 ret = prefix_bgp_orf_set(
2208 name, afi, &orfp,
2209 (common & ORF_COMMON_PART_DENY
2210 ? 0
2211 : 1),
2212 (common & ORF_COMMON_PART_REMOVE
2213 ? 0
2214 : 1));
2215
2216 if (!ok || (ok && ret != CMD_SUCCESS)) {
2217 zlog_info(
2218 "%s Received misformatted prefixlist ORF. Remove All pfxlist",
2219 peer->host);
2220 prefix_bgp_orf_remove_all(afi,
2221 name);
2222 break;
2223 }
2224 }
2225
2226 peer->orf_plist[afi][safi] =
2227 prefix_bgp_orf_lookup(afi, name);
2228 }
2229 stream_forward_getp(s, orf_len);
2230 }
2231 if (bgp_debug_neighbor_events(peer))
2232 zlog_debug("%s rcvd Refresh %s ORF request", peer->host,
2233 when_to_refresh == REFRESH_DEFER
2234 ? "Defer"
2235 : "Immediate");
2236 if (when_to_refresh == REFRESH_DEFER)
2237 return BGP_PACKET_NOOP;
2238 }
2239
2240 /* First update is deferred until ORF or ROUTE-REFRESH is received */
2241 if (CHECK_FLAG(peer->af_sflags[afi][safi],
2242 PEER_STATUS_ORF_WAIT_REFRESH))
2243 UNSET_FLAG(peer->af_sflags[afi][safi],
2244 PEER_STATUS_ORF_WAIT_REFRESH);
2245
2246 paf = peer_af_find(peer, afi, safi);
2247 if (paf && paf->subgroup) {
2248 if (peer->orf_plist[afi][safi]) {
2249 updgrp = PAF_UPDGRP(paf);
2250 updgrp_peer = UPDGRP_PEER(updgrp);
2251 updgrp_peer->orf_plist[afi][safi] =
2252 peer->orf_plist[afi][safi];
2253 }
2254
2255 /* Avoid supressing duplicate routes later
2256 * when processing in subgroup_announce_table().
2257 */
2258 force_update = true;
2259
2260 /* If the peer is configured for default-originate clear the
2261 * SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will
2262 * re-advertise the
2263 * default
2264 */
2265 if (CHECK_FLAG(paf->subgroup->sflags,
2266 SUBGRP_STATUS_DEFAULT_ORIGINATE))
2267 UNSET_FLAG(paf->subgroup->sflags,
2268 SUBGRP_STATUS_DEFAULT_ORIGINATE);
2269 }
2270
2271 if (subtype == BGP_ROUTE_REFRESH_BORR) {
2272 /* A BGP speaker that has received the Graceful Restart
2273 * Capability from its neighbor MUST ignore any BoRRs for
2274 * an <AFI, SAFI> from the neighbor before the speaker
2275 * receives the EoR for the given <AFI, SAFI> from the
2276 * neighbor.
2277 */
2278 if (CHECK_FLAG(peer->cap, PEER_CAP_RESTART_RCV)
2279 && !CHECK_FLAG(peer->af_sflags[afi][safi],
2280 PEER_STATUS_EOR_RECEIVED)) {
2281 if (bgp_debug_neighbor_events(peer))
2282 zlog_debug(
2283 "%s rcvd route-refresh (BoRR) for %s/%s before EoR",
2284 peer->host, afi2str(afi),
2285 safi2str(safi));
2286 return BGP_PACKET_NOOP;
2287 }
2288
2289 if (peer->t_refresh_stalepath) {
2290 if (bgp_debug_neighbor_events(peer))
2291 zlog_debug(
2292 "%s rcvd route-refresh (BoRR) for %s/%s, whereas BoRR already received",
2293 peer->host, afi2str(afi),
2294 safi2str(safi));
2295 return BGP_PACKET_NOOP;
2296 }
2297
2298 SET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_BORR_RECEIVED);
2299 UNSET_FLAG(peer->af_sflags[afi][safi],
2300 PEER_STATUS_EORR_RECEIVED);
2301
2302 /* When a BGP speaker receives a BoRR message from
2303 * a peer, it MUST mark all the routes with the given
2304 * Address Family Identifier and Subsequent Address
2305 * Family Identifier, <AFI, SAFI> [RFC2918], from
2306 * that peer as stale.
2307 */
2308 if (peer_active_nego(peer)) {
2309 SET_FLAG(peer->af_sflags[afi][safi],
2310 PEER_STATUS_ENHANCED_REFRESH);
2311 bgp_set_stale_route(peer, afi, safi);
2312 }
2313
2314 if (peer_established(peer))
2315 thread_add_timer(bm->master,
2316 bgp_refresh_stalepath_timer_expire,
2317 paf, peer->bgp->stalepath_time,
2318 &peer->t_refresh_stalepath);
2319
2320 if (bgp_debug_neighbor_events(peer))
2321 zlog_debug(
2322 "%s rcvd route-refresh (BoRR) for %s/%s, triggering timer for %u seconds",
2323 peer->host, afi2str(afi), safi2str(safi),
2324 peer->bgp->stalepath_time);
2325 } else if (subtype == BGP_ROUTE_REFRESH_EORR) {
2326 if (!peer->t_refresh_stalepath) {
2327 zlog_err(
2328 "%s rcvd route-refresh (EoRR) for %s/%s, whereas no BoRR received",
2329 peer->host, afi2str(afi), safi2str(safi));
2330 return BGP_PACKET_NOOP;
2331 }
2332
2333 BGP_TIMER_OFF(peer->t_refresh_stalepath);
2334
2335 SET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_EORR_RECEIVED);
2336 UNSET_FLAG(peer->af_sflags[afi][safi],
2337 PEER_STATUS_BORR_RECEIVED);
2338
2339 if (bgp_debug_neighbor_events(peer))
2340 zlog_debug(
2341 "%s rcvd route-refresh (EoRR) for %s/%s, stopping BoRR timer",
2342 peer->host, afi2str(afi), safi2str(safi));
2343
2344 if (peer->nsf[afi][safi])
2345 bgp_clear_stale_route(peer, afi, safi);
2346 } else {
2347 if (bgp_debug_neighbor_events(peer))
2348 zlog_debug("%s rcvd route-refresh (REQUEST) for %s/%s",
2349 peer->host, afi2str(afi), safi2str(safi));
2350
2351 /* In response to a "normal route refresh request" from the
2352 * peer, the speaker MUST send a BoRR message.
2353 */
2354 if (CHECK_FLAG(peer->cap, PEER_CAP_ENHANCED_RR_RCV)) {
2355 /* For a BGP speaker that supports the BGP Graceful
2356 * Restart, it MUST NOT send a BoRR for an <AFI, SAFI>
2357 * to a neighbor before it sends the EoR for the
2358 * <AFI, SAFI> to the neighbor.
2359 */
2360 if (!CHECK_FLAG(peer->af_sflags[afi][safi],
2361 PEER_STATUS_EOR_SEND)) {
2362 if (bgp_debug_neighbor_events(peer))
2363 zlog_debug(
2364 "%s rcvd route-refresh (REQUEST) for %s/%s before EoR",
2365 peer->host, afi2str(afi),
2366 safi2str(safi));
2367 return BGP_PACKET_NOOP;
2368 }
2369
2370 bgp_route_refresh_send(peer, afi, safi, 0, 0, 0,
2371 BGP_ROUTE_REFRESH_BORR);
2372
2373 if (bgp_debug_neighbor_events(peer))
2374 zlog_debug(
2375 "%s sending route-refresh (BoRR) for %s/%s",
2376 peer->host, afi2str(afi),
2377 safi2str(safi));
2378
2379 /* Set flag Ready-To-Send to know when we can send EoRR
2380 * message.
2381 */
2382 SET_FLAG(peer->af_sflags[afi][safi],
2383 PEER_STATUS_BORR_SEND);
2384 UNSET_FLAG(peer->af_sflags[afi][safi],
2385 PEER_STATUS_EORR_SEND);
2386 }
2387 }
2388
2389 /* Perform route refreshment to the peer */
2390 bgp_announce_route(peer, afi, safi, force_update);
2391
2392 /* No FSM action necessary */
2393 return BGP_PACKET_NOOP;
2394 }
2395
2396 /**
2397 * Parse BGP CAPABILITY message for peer.
2398 *
2399 * @param peer
2400 * @param size size of the packet
2401 * @return as in summary
2402 */
2403 static int bgp_capability_msg_parse(struct peer *peer, uint8_t *pnt,
2404 bgp_size_t length)
2405 {
2406 uint8_t *end;
2407 struct capability_mp_data mpc;
2408 struct capability_header *hdr;
2409 uint8_t action;
2410 iana_afi_t pkt_afi;
2411 afi_t afi;
2412 iana_safi_t pkt_safi;
2413 safi_t safi;
2414
2415 end = pnt + length;
2416
2417 while (pnt < end) {
2418 /* We need at least action, capability code and capability
2419 * length. */
2420 if (pnt + 3 > end) {
2421 zlog_info("%s Capability length error", peer->host);
2422 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2423 BGP_NOTIFY_SUBCODE_UNSPECIFIC);
2424 return BGP_Stop;
2425 }
2426 action = *pnt;
2427 hdr = (struct capability_header *)(pnt + 1);
2428
2429 /* Action value check. */
2430 if (action != CAPABILITY_ACTION_SET
2431 && action != CAPABILITY_ACTION_UNSET) {
2432 zlog_info("%s Capability Action Value error %d",
2433 peer->host, action);
2434 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2435 BGP_NOTIFY_SUBCODE_UNSPECIFIC);
2436 return BGP_Stop;
2437 }
2438
2439 if (bgp_debug_neighbor_events(peer))
2440 zlog_debug(
2441 "%s CAPABILITY has action: %d, code: %u, length %u",
2442 peer->host, action, hdr->code, hdr->length);
2443
2444 /* Capability length check. */
2445 if ((pnt + hdr->length + 3) > end) {
2446 zlog_info("%s Capability length error", peer->host);
2447 bgp_notify_send(peer, BGP_NOTIFY_CEASE,
2448 BGP_NOTIFY_SUBCODE_UNSPECIFIC);
2449 return BGP_Stop;
2450 }
2451
2452 /* Fetch structure to the byte stream. */
2453 memcpy(&mpc, pnt + 3, sizeof(struct capability_mp_data));
2454 pnt += hdr->length + 3;
2455
2456 /* We know MP Capability Code. */
2457 if (hdr->code == CAPABILITY_CODE_MP) {
2458 pkt_afi = ntohs(mpc.afi);
2459 pkt_safi = mpc.safi;
2460
2461 /* Ignore capability when override-capability is set. */
2462 if (CHECK_FLAG(peer->flags,
2463 PEER_FLAG_OVERRIDE_CAPABILITY))
2464 continue;
2465
2466 /* Convert AFI, SAFI to internal values. */
2467 if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi,
2468 &safi)) {
2469 if (bgp_debug_neighbor_events(peer))
2470 zlog_debug(
2471 "%s Dynamic Capability MP_EXT afi/safi invalid (%s/%s)",
2472 peer->host,
2473 iana_afi2str(pkt_afi),
2474 iana_safi2str(pkt_safi));
2475 continue;
2476 }
2477
2478 /* Address family check. */
2479 if (bgp_debug_neighbor_events(peer))
2480 zlog_debug(
2481 "%s CAPABILITY has %s MP_EXT CAP for afi/safi: %s/%s",
2482 peer->host,
2483 action == CAPABILITY_ACTION_SET
2484 ? "Advertising"
2485 : "Removing",
2486 iana_afi2str(pkt_afi),
2487 iana_safi2str(pkt_safi));
2488
2489 if (action == CAPABILITY_ACTION_SET) {
2490 peer->afc_recv[afi][safi] = 1;
2491 if (peer->afc[afi][safi]) {
2492 peer->afc_nego[afi][safi] = 1;
2493 bgp_announce_route(peer, afi, safi,
2494 false);
2495 }
2496 } else {
2497 peer->afc_recv[afi][safi] = 0;
2498 peer->afc_nego[afi][safi] = 0;
2499
2500 if (peer_active_nego(peer))
2501 bgp_clear_route(peer, afi, safi);
2502 else
2503 return BGP_Stop;
2504 }
2505 } else {
2506 flog_warn(
2507 EC_BGP_UNRECOGNIZED_CAPABILITY,
2508 "%s unrecognized capability code: %d - ignored",
2509 peer->host, hdr->code);
2510 }
2511 }
2512
2513 /* No FSM action necessary */
2514 return BGP_PACKET_NOOP;
2515 }
2516
2517 /**
2518 * Parse BGP CAPABILITY message for peer.
2519 *
2520 * Exported for unit testing.
2521 *
2522 * @param peer
2523 * @param size size of the packet
2524 * @return as in summary
2525 */
2526 int bgp_capability_receive(struct peer *peer, bgp_size_t size)
2527 {
2528 uint8_t *pnt;
2529
2530 /* Fetch pointer. */
2531 pnt = stream_pnt(peer->curr);
2532
2533 if (bgp_debug_neighbor_events(peer))
2534 zlog_debug("%s rcv CAPABILITY", peer->host);
2535
2536 /* If peer does not have the capability, send notification. */
2537 if (!CHECK_FLAG(peer->cap, PEER_CAP_DYNAMIC_ADV)) {
2538 flog_err(EC_BGP_NO_CAP,
2539 "%s [Error] BGP dynamic capability is not enabled",
2540 peer->host);
2541 bgp_notify_send(peer, BGP_NOTIFY_HEADER_ERR,
2542 BGP_NOTIFY_HEADER_BAD_MESTYPE);
2543 return BGP_Stop;
2544 }
2545
2546 /* Status must be Established. */
2547 if (!peer_established(peer)) {
2548 flog_err(
2549 EC_BGP_NO_CAP,
2550 "%s [Error] Dynamic capability packet received under status %s",
2551 peer->host,
2552 lookup_msg(bgp_status_msg, peer->status, NULL));
2553 bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR,
2554 bgp_fsm_error_subcode(peer->status));
2555 return BGP_Stop;
2556 }
2557
2558 /* Parse packet. */
2559 return bgp_capability_msg_parse(peer, pnt, size);
2560 }
2561
2562 /**
2563 * Processes a peer's input buffer.
2564 *
2565 * This function sidesteps the event loop and directly calls bgp_event_update()
2566 * after processing each BGP message. This is necessary to ensure proper
2567 * ordering of FSM events and unifies the behavior that was present previously,
2568 * whereby some of the packet handling functions would update the FSM and some
2569 * would not, making event flow difficult to understand. Please think twice
2570 * before hacking this.
2571 *
2572 * Thread type: THREAD_EVENT
2573 * @param thread
2574 * @return 0
2575 */
2576 int bgp_process_packet(struct thread *thread)
2577 {
2578 /* Yes first of all get peer pointer. */
2579 struct peer *peer; // peer
2580 uint32_t rpkt_quanta_old; // how many packets to read
2581 int fsm_update_result; // return code of bgp_event_update()
2582 int mprc; // message processing return code
2583
2584 peer = THREAD_ARG(thread);
2585 rpkt_quanta_old = atomic_load_explicit(&peer->bgp->rpkt_quanta,
2586 memory_order_relaxed);
2587 fsm_update_result = 0;
2588
2589 /* Guard against scheduled events that occur after peer deletion. */
2590 if (peer->status == Deleted || peer->status == Clearing)
2591 return 0;
2592
2593 unsigned int processed = 0;
2594
2595 while (processed < rpkt_quanta_old) {
2596 uint8_t type = 0;
2597 bgp_size_t size;
2598 char notify_data_length[2];
2599
2600 frr_with_mutex(&peer->io_mtx) {
2601 peer->curr = stream_fifo_pop(peer->ibuf);
2602 }
2603
2604 if (peer->curr == NULL) // no packets to process, hmm...
2605 return 0;
2606
2607 /* skip the marker and copy the packet length */
2608 stream_forward_getp(peer->curr, BGP_MARKER_SIZE);
2609 memcpy(notify_data_length, stream_pnt(peer->curr), 2);
2610
2611 /* read in the packet length and type */
2612 size = stream_getw(peer->curr);
2613 type = stream_getc(peer->curr);
2614
2615 hook_call(bgp_packet_dump, peer, type, size, peer->curr);
2616
2617 /* adjust size to exclude the marker + length + type */
2618 size -= BGP_HEADER_SIZE;
2619
2620 /* Read rest of the packet and call each sort of packet routine
2621 */
2622 switch (type) {
2623 case BGP_MSG_OPEN:
2624 frrtrace(2, frr_bgp, open_process, peer, size);
2625 atomic_fetch_add_explicit(&peer->open_in, 1,
2626 memory_order_relaxed);
2627 mprc = bgp_open_receive(peer, size);
2628 if (mprc == BGP_Stop)
2629 flog_err(
2630 EC_BGP_PKT_OPEN,
2631 "%s: BGP OPEN receipt failed for peer: %s",
2632 __func__, peer->host);
2633 break;
2634 case BGP_MSG_UPDATE:
2635 frrtrace(2, frr_bgp, update_process, peer, size);
2636 atomic_fetch_add_explicit(&peer->update_in, 1,
2637 memory_order_relaxed);
2638 peer->readtime = monotime(NULL);
2639 mprc = bgp_update_receive(peer, size);
2640 if (mprc == BGP_Stop)
2641 flog_err(
2642 EC_BGP_UPDATE_RCV,
2643 "%s: BGP UPDATE receipt failed for peer: %s",
2644 __func__, peer->host);
2645 break;
2646 case BGP_MSG_NOTIFY:
2647 frrtrace(2, frr_bgp, notification_process, peer, size);
2648 atomic_fetch_add_explicit(&peer->notify_in, 1,
2649 memory_order_relaxed);
2650 mprc = bgp_notify_receive(peer, size);
2651 if (mprc == BGP_Stop)
2652 flog_err(
2653 EC_BGP_NOTIFY_RCV,
2654 "%s: BGP NOTIFY receipt failed for peer: %s",
2655 __func__, peer->host);
2656 break;
2657 case BGP_MSG_KEEPALIVE:
2658 frrtrace(2, frr_bgp, keepalive_process, peer, size);
2659 peer->readtime = monotime(NULL);
2660 atomic_fetch_add_explicit(&peer->keepalive_in, 1,
2661 memory_order_relaxed);
2662 mprc = bgp_keepalive_receive(peer, size);
2663 if (mprc == BGP_Stop)
2664 flog_err(
2665 EC_BGP_KEEP_RCV,
2666 "%s: BGP KEEPALIVE receipt failed for peer: %s",
2667 __func__, peer->host);
2668 break;
2669 case BGP_MSG_ROUTE_REFRESH_NEW:
2670 case BGP_MSG_ROUTE_REFRESH_OLD:
2671 frrtrace(2, frr_bgp, refresh_process, peer, size);
2672 atomic_fetch_add_explicit(&peer->refresh_in, 1,
2673 memory_order_relaxed);
2674 mprc = bgp_route_refresh_receive(peer, size);
2675 if (mprc == BGP_Stop)
2676 flog_err(
2677 EC_BGP_RFSH_RCV,
2678 "%s: BGP ROUTEREFRESH receipt failed for peer: %s",
2679 __func__, peer->host);
2680 break;
2681 case BGP_MSG_CAPABILITY:
2682 frrtrace(2, frr_bgp, capability_process, peer, size);
2683 atomic_fetch_add_explicit(&peer->dynamic_cap_in, 1,
2684 memory_order_relaxed);
2685 mprc = bgp_capability_receive(peer, size);
2686 if (mprc == BGP_Stop)
2687 flog_err(
2688 EC_BGP_CAP_RCV,
2689 "%s: BGP CAPABILITY receipt failed for peer: %s",
2690 __func__, peer->host);
2691 break;
2692 default:
2693 /* Suppress uninitialized variable warning */
2694 mprc = 0;
2695 (void)mprc;
2696 /*
2697 * The message type should have been sanitized before
2698 * we ever got here. Receipt of a message with an
2699 * invalid header at this point is indicative of a
2700 * security issue.
2701 */
2702 assert (!"Message of invalid type received during input processing");
2703 }
2704
2705 /* delete processed packet */
2706 stream_free(peer->curr);
2707 peer->curr = NULL;
2708 processed++;
2709
2710 /* Update FSM */
2711 if (mprc != BGP_PACKET_NOOP)
2712 fsm_update_result = bgp_event_update(peer, mprc);
2713 else
2714 continue;
2715
2716 /*
2717 * If peer was deleted, do not process any more packets. This
2718 * is usually due to executing BGP_Stop or a stub deletion.
2719 */
2720 if (fsm_update_result == FSM_PEER_TRANSFERRED
2721 || fsm_update_result == FSM_PEER_STOPPED)
2722 break;
2723 }
2724
2725 if (fsm_update_result != FSM_PEER_TRANSFERRED
2726 && fsm_update_result != FSM_PEER_STOPPED) {
2727 frr_with_mutex(&peer->io_mtx) {
2728 // more work to do, come back later
2729 if (peer->ibuf->count > 0)
2730 thread_add_event(
2731 bm->master, bgp_process_packet, peer, 0,
2732 &peer->t_process_packet);
2733 }
2734 }
2735
2736 return 0;
2737 }
2738
2739 /* Send EOR when routes are processed by selection deferral timer */
2740 void bgp_send_delayed_eor(struct bgp *bgp)
2741 {
2742 struct peer *peer;
2743 struct listnode *node, *nnode;
2744
2745 /* EOR message sent in bgp_write_proceed_actions */
2746 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer))
2747 bgp_write_proceed_actions(peer);
2748 }
2749
2750 /*
2751 * Task callback to handle socket error encountered in the io pthread. We avoid
2752 * having the io pthread try to enqueue fsm events or mess with the peer
2753 * struct.
2754 */
2755 int bgp_packet_process_error(struct thread *thread)
2756 {
2757 struct peer *peer;
2758 int code;
2759
2760 peer = THREAD_ARG(thread);
2761 code = THREAD_VAL(thread);
2762
2763 if (bgp_debug_neighbor_events(peer))
2764 zlog_debug("%s [Event] BGP error %d on fd %d",
2765 peer->host, code, peer->fd);
2766
2767 /* Closed connection or error on the socket */
2768 if (peer_established(peer)) {
2769 if ((CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART)
2770 || CHECK_FLAG(peer->flags,
2771 PEER_FLAG_GRACEFUL_RESTART_HELPER))
2772 && CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_MODE)) {
2773 peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
2774 SET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT);
2775 } else
2776 peer->last_reset = PEER_DOWN_CLOSE_SESSION;
2777 }
2778
2779 bgp_event_update(peer, code);
2780
2781 return 0;
2782 }