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