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