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