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