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