2 * bgp_updgrp_packet.c: BGP update group packet handling routines
4 * @copyright Copyright (C) 2014 Cumulus Networks, Inc.
6 * @author Avneesh Sachdev <avneesh@sproute.net>
7 * @author Rajesh Varadarajan <rajesh@sproute.net>
8 * @author Pradosh Mohapatra <pradosh@sproute.net>
10 * This file is part of GNU Zebra.
12 * GNU Zebra is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2, or (at your option) any
17 * GNU Zebra is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; see the file COPYING; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "sockunion.h"
42 #include "workqueue.h"
47 #include "bgpd/bgpd.h"
48 #include "bgpd/bgp_debug.h"
49 #include "bgpd/bgp_errors.h"
50 #include "bgpd/bgp_fsm.h"
51 #include "bgpd/bgp_route.h"
52 #include "bgpd/bgp_packet.h"
53 #include "bgpd/bgp_advertise.h"
54 #include "bgpd/bgp_updgrp.h"
55 #include "bgpd/bgp_nexthop.h"
56 #include "bgpd/bgp_nht.h"
57 #include "bgpd/bgp_mplsvpn.h"
58 #include "bgpd/bgp_label.h"
59 #include "bgpd/bgp_addpath.h"
68 struct bpacket
*bpacket_alloc(void)
72 pkt
= XCALLOC(MTYPE_BGP_PACKET
, sizeof(struct bpacket
));
77 void bpacket_free(struct bpacket
*pkt
)
80 stream_free(pkt
->buffer
);
82 XFREE(MTYPE_BGP_PACKET
, pkt
);
85 void bpacket_queue_init(struct bpacket_queue
*q
)
87 TAILQ_INIT(&(q
->pkts
));
91 * bpacket_queue_add_packet
93 * Internal function of bpacket_queue - and adds a
94 * packet entry to the end of the list.
96 * Users of bpacket_queue should use bpacket_queue_add instead.
98 static void bpacket_queue_add_packet(struct bpacket_queue
*q
,
101 struct bpacket
*last_pkt
;
103 if (TAILQ_EMPTY(&(q
->pkts
)))
104 TAILQ_INSERT_TAIL(&(q
->pkts
), pkt
, pkt_train
);
106 last_pkt
= bpacket_queue_last(q
);
107 TAILQ_INSERT_AFTER(&(q
->pkts
), last_pkt
, pkt
, pkt_train
);
110 if (q
->hwm_count
< q
->curr_count
)
111 q
->hwm_count
= q
->curr_count
;
115 * Adds a packet to the bpacket_queue.
117 * The stream passed is consumed by this function. So, the caller should
118 * not free or use the stream after
119 * invoking this function.
121 struct bpacket
*bpacket_queue_add(struct bpacket_queue
*q
, struct stream
*s
,
122 struct bpacket_attr_vec_arr
*vecarrp
)
125 struct bpacket
*last_pkt
;
128 pkt
= bpacket_alloc();
129 if (TAILQ_EMPTY(&(q
->pkts
))) {
133 memcpy(&pkt
->arr
, vecarrp
,
134 sizeof(struct bpacket_attr_vec_arr
));
136 bpacket_attr_vec_arr_reset(&pkt
->arr
);
137 bpacket_queue_add_packet(q
, pkt
);
142 * Fill in the new information into the current sentinel and create a
145 last_pkt
= bpacket_queue_last(q
);
146 assert(last_pkt
->buffer
== NULL
);
147 last_pkt
->buffer
= s
;
149 memcpy(&last_pkt
->arr
, vecarrp
,
150 sizeof(struct bpacket_attr_vec_arr
));
152 bpacket_attr_vec_arr_reset(&last_pkt
->arr
);
154 pkt
->ver
= last_pkt
->ver
;
156 bpacket_queue_add_packet(q
, pkt
);
161 struct bpacket
*bpacket_queue_first(struct bpacket_queue
*q
)
163 return (TAILQ_FIRST(&(q
->pkts
)));
166 struct bpacket
*bpacket_queue_last(struct bpacket_queue
*q
)
168 return TAILQ_LAST(&(q
->pkts
), pkt_queue
);
171 struct bpacket
*bpacket_queue_remove(struct bpacket_queue
*q
)
173 struct bpacket
*first
;
175 first
= bpacket_queue_first(q
);
177 TAILQ_REMOVE(&(q
->pkts
), first
, pkt_train
);
183 unsigned int bpacket_queue_length(struct bpacket_queue
*q
)
185 return q
->curr_count
- 1;
188 unsigned int bpacket_queue_hwm_length(struct bpacket_queue
*q
)
190 return q
->hwm_count
- 1;
193 bool bpacket_queue_is_full(struct bgp
*bgp
, struct bpacket_queue
*q
)
195 if (q
->curr_count
>= bgp
->default_subgroup_pkt_queue_max
)
200 void bpacket_add_peer(struct bpacket
*pkt
, struct peer_af
*paf
)
205 LIST_INSERT_HEAD(&(pkt
->peers
), paf
, pkt_train
);
206 paf
->next_pkt_to_send
= pkt
;
210 * bpacket_queue_cleanup
212 void bpacket_queue_cleanup(struct bpacket_queue
*q
)
216 while ((pkt
= bpacket_queue_remove(q
))) {
222 * bpacket_queue_compact
224 * Delete packets that do not need to be transmitted to any peer from
227 * @return the number of packets deleted.
229 static int bpacket_queue_compact(struct bpacket_queue
*q
)
232 struct bpacket
*pkt
, *removed_pkt
;
237 pkt
= bpacket_queue_first(q
);
242 * Don't delete the sentinel.
247 if (!LIST_EMPTY(&(pkt
->peers
)))
250 removed_pkt
= bpacket_queue_remove(q
);
251 assert(pkt
== removed_pkt
);
252 bpacket_free(removed_pkt
);
260 void bpacket_queue_advance_peer(struct peer_af
*paf
)
263 struct bpacket
*old_pkt
;
265 old_pkt
= paf
->next_pkt_to_send
;
266 if (old_pkt
->buffer
== NULL
)
267 /* Already at end of list */
270 LIST_REMOVE(paf
, pkt_train
);
271 pkt
= TAILQ_NEXT(old_pkt
, pkt_train
);
272 bpacket_add_peer(pkt
, paf
);
274 if (!bpacket_queue_compact(PAF_PKTQ(paf
)))
278 * Deleted one or more packets. Check if we can now merge this
279 * peer's subgroup into another subgroup.
281 update_subgroup_check_merge(paf
->subgroup
, "advanced peer in queue");
285 * bpacket_queue_remove_peer
287 * Remove the peer from the packet queue of the subgroup it belongs
290 void bpacket_queue_remove_peer(struct peer_af
*paf
)
292 struct bpacket_queue
*q
;
297 LIST_REMOVE(paf
, pkt_train
);
298 paf
->next_pkt_to_send
= NULL
;
300 bpacket_queue_compact(q
);
303 unsigned int bpacket_queue_virtual_length(struct peer_af
*paf
)
306 struct bpacket
*last
;
307 struct bpacket_queue
*q
;
309 pkt
= paf
->next_pkt_to_send
;
310 if (!pkt
|| (pkt
->buffer
== NULL
))
311 /* Already at end of list */
315 if (TAILQ_EMPTY(&(q
->pkts
)))
318 last
= TAILQ_LAST(&(q
->pkts
), pkt_queue
);
319 if (last
->ver
>= pkt
->ver
)
320 return last
->ver
- pkt
->ver
;
322 /* sequence # rolled over */
323 return (UINT_MAX
- pkt
->ver
+ 1) + last
->ver
;
327 * Dump the bpacket queue
329 void bpacket_queue_show_vty(struct bpacket_queue
*q
, struct vty
*vty
)
334 pkt
= bpacket_queue_first(q
);
336 vty_out(vty
, " Packet %p ver %u buffer %p\n", pkt
, pkt
->ver
,
339 LIST_FOREACH (paf
, &(pkt
->peers
), pkt_train
) {
340 vty_out(vty
, " - %s\n", paf
->peer
->host
);
342 pkt
= bpacket_next(pkt
);
347 struct stream
*bpacket_reformat_for_peer(struct bpacket
*pkt
,
350 struct stream
*s
= NULL
;
351 bpacket_attr_vec
*vec
;
353 struct bgp_filter
*filter
;
355 s
= stream_dup(pkt
->buffer
);
356 peer
= PAF_PEER(paf
);
358 vec
= &pkt
->arr
.entries
[BGP_ATTR_VEC_NH
];
360 if (!CHECK_FLAG(vec
->flags
, BPKT_ATTRVEC_FLAGS_UPDATED
))
365 int route_map_sets_nh
;
367 nhlen
= stream_getc_from(s
, vec
->offset
);
368 filter
= &peer
->filter
[paf
->afi
][paf
->safi
];
370 if (peer_cap_enhe(peer
, paf
->afi
, paf
->safi
))
373 nhafi
= BGP_NEXTHOP_AFI_FROM_NHLEN(nhlen
);
375 if (nhafi
== AFI_IP
) {
376 struct in_addr v4nh
, *mod_v4nh
;
378 size_t offset_nh
= vec
->offset
+ 1;
381 (CHECK_FLAG(vec
->flags
,
382 BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED
) ||
383 CHECK_FLAG(vec
->flags
,
384 BPKT_ATTRVEC_FLAGS_RMAP_VPNV4_NH_CHANGED
) ||
385 CHECK_FLAG(vec
->flags
,
386 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
));
389 case BGP_ATTR_NHLEN_IPV4
:
391 case BGP_ATTR_NHLEN_VPNV4
:
395 /* TODO: handle IPv6 nexthops */
397 EC_BGP_INVALID_NEXTHOP_LENGTH
,
398 "%s: %s: invalid MP nexthop length (AFI IP): %u",
399 __func__
, peer
->host
, nhlen
);
404 stream_get_from(&v4nh
, s
, offset_nh
, IPV4_MAX_BYTELEN
);
408 * If route-map has set the nexthop, that is normally
409 * used; if it is specified as peer-address, the peering
410 * address is picked up. Otherwise, if NH is unavailable
411 * from attribute, the peering addr is picked up; the
412 * "NH unavailable" case also covers next-hop-self and
413 * some other scenarios - see subgroup_announce_check().
414 * In all other cases, use the nexthop carried in the
415 * attribute unless it is EBGP non-multiaccess and there
416 * is no next-hop-unchanged setting or the peer is EBGP
417 * and the route-map that changed the next-hop value
418 * was applied inbound rather than outbound. Updates to
419 * an EBGP peer should only modify the next-hop if it
420 * was set in an outbound route-map to that peer.
421 * Note: It is assumed route-map cannot set the nexthop
422 * to an invalid value.
424 if (route_map_sets_nh
425 && ((peer
->sort
!= BGP_PEER_EBGP
)
426 || ROUTE_MAP_OUT(filter
))) {
429 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
)) {
430 mod_v4nh
= &peer
->nexthop
.v4
;
433 } else if (v4nh
.s_addr
== INADDR_ANY
) {
434 mod_v4nh
= &peer
->nexthop
.v4
;
436 } else if (peer
->sort
== BGP_PEER_EBGP
437 && (bgp_multiaccess_check_v4(v4nh
, peer
) == 0)
438 && !CHECK_FLAG(vec
->flags
,
439 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
)
440 && !peer_af_flag_check(
441 peer
, paf
->afi
, paf
->safi
,
442 PEER_FLAG_NEXTHOP_UNCHANGED
)) {
443 /* NOTE: not handling case where NH has new AFI
445 mod_v4nh
= &peer
->nexthop
.v4
;
449 if (nh_modified
) /* allow for VPN RD */
450 stream_put_in_addr_at(s
, offset_nh
, mod_v4nh
);
452 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
453 zlog_debug("u%" PRIu64
":s%" PRIu64
454 " %s send UPDATE w/ nexthop %pI4%s",
455 PAF_SUBGRP(paf
)->update_group
->id
,
456 PAF_SUBGRP(paf
)->id
, peer
->host
, mod_v4nh
,
457 (nhlen
== BGP_ATTR_NHLEN_VPNV4
? " and RD"
459 } else if (nhafi
== AFI_IP6
) {
460 struct in6_addr v6nhglobal
, *mod_v6nhg
;
461 struct in6_addr v6nhlocal
, *mod_v6nhl
;
462 int gnh_modified
, lnh_modified
;
463 size_t offset_nhglobal
= vec
->offset
+ 1;
464 size_t offset_nhlocal
= vec
->offset
+ 1;
466 gnh_modified
= lnh_modified
= 0;
467 mod_v6nhg
= &v6nhglobal
;
468 mod_v6nhl
= &v6nhlocal
;
471 (CHECK_FLAG(vec
->flags
,
472 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED
) ||
475 BPKT_ATTRVEC_FLAGS_RMAP_VPNV6_GNH_CHANGED
) ||
476 CHECK_FLAG(vec
->flags
,
477 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
));
480 * The logic here is rather similar to that for IPv4, the
481 * additional work being to handle 1 or 2 nexthops.
482 * Also, 3rd party nexthop is not propagated for EBGP
486 case BGP_ATTR_NHLEN_IPV6_GLOBAL
:
488 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
:
489 offset_nhlocal
+= IPV6_MAX_BYTELEN
;
491 case BGP_ATTR_NHLEN_VPNV6_GLOBAL
:
492 offset_nhglobal
+= 8;
494 case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
:
495 offset_nhglobal
+= 8;
496 offset_nhlocal
+= 8 * 2 + IPV6_MAX_BYTELEN
;
499 /* TODO: handle IPv4 nexthops */
501 EC_BGP_INVALID_NEXTHOP_LENGTH
,
502 "%s: %s: invalid MP nexthop length (AFI IP6): %u",
503 __func__
, peer
->host
, nhlen
);
508 stream_get_from(&v6nhglobal
, s
, offset_nhglobal
,
512 * Updates to an EBGP peer should only modify the
513 * next-hop if it was set in an outbound route-map
516 if (route_map_sets_nh
517 && ((peer
->sort
!= BGP_PEER_EBGP
)
518 || ROUTE_MAP_OUT(filter
))) {
521 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
)) {
522 mod_v6nhg
= &peer
->nexthop
.v6_global
;
525 } else if (IN6_IS_ADDR_UNSPECIFIED(&v6nhglobal
)) {
526 mod_v6nhg
= &peer
->nexthop
.v6_global
;
528 } else if ((peer
->sort
== BGP_PEER_EBGP
)
529 && (!bgp_multiaccess_check_v6(v6nhglobal
, peer
))
530 && !CHECK_FLAG(vec
->flags
,
531 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
)
532 && !peer_af_flag_check(
533 peer
, paf
->afi
, paf
->safi
,
534 PEER_FLAG_NEXTHOP_UNCHANGED
)) {
535 /* NOTE: not handling case where NH has new AFI
537 mod_v6nhg
= &peer
->nexthop
.v6_global
;
541 if (IN6_IS_ADDR_UNSPECIFIED(mod_v6nhg
)) {
542 if (peer
->nexthop
.v4
.s_addr
!= INADDR_ANY
) {
543 ipv4_to_ipv4_mapped_ipv6(mod_v6nhg
,
548 if (IS_MAPPED_IPV6(&peer
->nexthop
.v6_global
)) {
549 mod_v6nhg
= &peer
->nexthop
.v6_global
;
553 if (nhlen
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
554 || nhlen
== BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
555 stream_get_from(&v6nhlocal
, s
, offset_nhlocal
,
557 if (IN6_IS_ADDR_UNSPECIFIED(&v6nhlocal
)) {
558 mod_v6nhl
= &peer
->nexthop
.v6_local
;
564 stream_put_in6_addr_at(s
, offset_nhglobal
, mod_v6nhg
);
566 stream_put_in6_addr_at(s
, offset_nhlocal
, mod_v6nhl
);
568 if (bgp_debug_update(peer
, NULL
, NULL
, 0)) {
569 if (nhlen
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
570 || nhlen
== BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
)
572 "u%" PRIu64
":s%" PRIu64
573 " %s send UPDATE w/ mp_nexthops %pI6, %pI6%s",
574 PAF_SUBGRP(paf
)->update_group
->id
,
575 PAF_SUBGRP(paf
)->id
, peer
->host
,
576 mod_v6nhg
, mod_v6nhl
,
577 (nhlen
== BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
582 "u%" PRIu64
":s%" PRIu64
583 " %s send UPDATE w/ mp_nexthop %pI6%s",
584 PAF_SUBGRP(paf
)->update_group
->id
,
585 PAF_SUBGRP(paf
)->id
, peer
->host
,
587 (nhlen
== BGP_ATTR_NHLEN_VPNV6_GLOBAL
591 } else if (paf
->afi
== AFI_L2VPN
) {
592 struct in_addr v4nh
, *mod_v4nh
;
595 stream_get_from(&v4nh
, s
, vec
->offset
+ 1, 4);
598 /* No route-map changes allowed for EVPN nexthops. */
599 if (v4nh
.s_addr
== INADDR_ANY
) {
600 mod_v4nh
= &peer
->nexthop
.v4
;
605 stream_put_in_addr_at(s
, vec
->offset
+ 1, mod_v4nh
);
607 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
608 zlog_debug("u%" PRIu64
":s%" PRIu64
609 " %s send UPDATE w/ nexthop %pI4",
610 PAF_SUBGRP(paf
)->update_group
->id
,
611 PAF_SUBGRP(paf
)->id
, peer
->host
, mod_v4nh
);
618 * Update the vecarr offsets to go beyond 'pos' bytes, i.e. add 'pos'
621 static void bpacket_attr_vec_arr_update(struct bpacket_attr_vec_arr
*vecarr
,
629 for (i
= 0; i
< BGP_ATTR_VEC_MAX
; i
++)
630 vecarr
->entries
[i
].offset
+= pos
;
634 * Return if there are packets to build for this subgroup.
636 bool subgroup_packets_to_build(struct update_subgroup
*subgrp
)
638 struct bgp_advertise
*adv
;
643 adv
= bgp_adv_fifo_first(&subgrp
->sync
->withdraw
);
647 adv
= bgp_adv_fifo_first(&subgrp
->sync
->update
);
654 /* Make BGP update packet. */
655 struct bpacket
*subgroup_update_packet(struct update_subgroup
*subgrp
)
657 struct bpacket_attr_vec_arr vecarr
;
661 struct stream
*snlri
;
662 struct stream
*packet
;
663 struct bgp_adj_out
*adj
;
664 struct bgp_advertise
*adv
;
665 struct bgp_dest
*dest
= NULL
;
666 struct bgp_path_info
*path
= NULL
;
667 bgp_size_t total_attr_len
= 0;
668 unsigned long attrlen_pos
= 0;
669 size_t mpattrlen_pos
= 0;
670 size_t mpattr_pos
= 0;
673 int space_remaining
= 0;
674 int space_needed
= 0;
675 char send_attr_str
[BUFSIZ
];
676 int send_attr_printed
= 0;
678 bool addpath_capable
= false;
679 int addpath_overhead
= 0;
680 uint32_t addpath_tx_id
= 0;
681 struct prefix_rd
*prd
= NULL
;
682 mpls_label_t label
= MPLS_INVALID_LABEL
, *label_pnt
= NULL
;
683 uint32_t num_labels
= 0;
688 if (bpacket_queue_is_full(SUBGRP_INST(subgrp
), SUBGRP_PKTQ(subgrp
)))
691 peer
= SUBGRP_PEER(subgrp
);
692 afi
= SUBGRP_AFI(subgrp
);
693 safi
= SUBGRP_SAFI(subgrp
);
696 snlri
= subgrp
->scratch
;
699 bpacket_attr_vec_arr_reset(&vecarr
);
701 addpath_capable
= bgp_addpath_encode_tx(peer
, afi
, safi
);
702 addpath_overhead
= addpath_capable
? BGP_ADDPATH_ID_LEN
: 0;
704 adv
= bgp_adv_fifo_first(&subgrp
->sync
->update
);
706 const struct prefix
*dest_p
;
710 dest_p
= bgp_dest_get_prefix(dest
);
712 addpath_tx_id
= adj
->addpath_tx_id
;
715 space_remaining
= STREAM_CONCAT_REMAIN(s
, snlri
, STREAM_SIZE(s
))
716 - BGP_MAX_PACKET_SIZE_OVERFLOW
;
718 BGP_NLRI_LENGTH
+ addpath_overhead
719 + bgp_packet_mpattr_prefix_size(afi
, safi
, dest_p
);
721 /* When remaining space can't include NLRI and it's length. */
722 if (space_remaining
< space_needed
)
725 /* If packet is empty, set attribute. */
726 if (stream_empty(s
)) {
727 struct peer
*from
= NULL
;
732 /* 1: Write the BGP message header - 16 bytes marker, 2
734 * one byte message type.
736 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
738 /* 2: withdrawn routes length */
741 /* 3: total attributes length - attrlen_pos stores the
743 attrlen_pos
= stream_get_endp(s
);
746 /* 4: if there is MP_REACH_NLRI attribute, that should
748 * attribute, according to
749 * draft-ietf-idr-error-handling. Save the
752 mpattr_pos
= stream_get_endp(s
);
754 /* 5: Encode all the attributes, except MP_REACH_NLRI
756 total_attr_len
= bgp_packet_attribute(
757 NULL
, peer
, s
, adv
->baa
->attr
, &vecarr
, NULL
,
758 afi
, safi
, from
, NULL
, NULL
, 0, 0, 0, path
);
761 STREAM_CONCAT_REMAIN(s
, snlri
, STREAM_SIZE(s
))
762 - BGP_MAX_PACKET_SIZE_OVERFLOW
;
763 space_needed
= BGP_NLRI_LENGTH
+ addpath_overhead
764 + bgp_packet_mpattr_prefix_size(
767 /* If the attributes alone do not leave any room for
770 if (space_remaining
< space_needed
) {
772 EC_BGP_UPDGRP_ATTR_LEN
,
773 "u%" PRIu64
":s%" PRIu64
" attributes too long, cannot send UPDATE",
774 subgrp
->update_group
->id
, subgrp
->id
);
776 /* Flush the FIFO update queue */
778 adv
= bgp_advertise_clean_subgroup(
783 if (BGP_DEBUG(update
, UPDATE_OUT
)
784 || BGP_DEBUG(update
, UPDATE_PREFIX
)) {
785 memset(send_attr_str
, 0, BUFSIZ
);
786 send_attr_printed
= 0;
787 bgp_dump_attr(adv
->baa
->attr
, send_attr_str
,
788 sizeof(send_attr_str
));
792 if ((afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
793 && !peer_cap_enhe(peer
, afi
, safi
))
794 stream_put_prefix_addpath(s
, dest_p
, addpath_capable
,
797 /* Encode the prefix in MP_REACH_NLRI attribute */
799 prd
= (struct prefix_rd
*)bgp_dest_get_prefix(
802 if (safi
== SAFI_LABELED_UNICAST
) {
803 label
= bgp_adv_label(dest
, path
, peer
, afi
,
807 } else if (path
&& path
->extra
) {
808 label_pnt
= &path
->extra
->label
[0];
809 num_labels
= path
->extra
->num_labels
;
812 if (stream_empty(snlri
))
813 mpattrlen_pos
= bgp_packet_mpattr_start(
814 snlri
, peer
, afi
, safi
, &vecarr
,
817 bgp_packet_mpattr_prefix(snlri
, afi
, safi
, dest_p
, prd
,
818 label_pnt
, num_labels
,
819 addpath_capable
, addpath_tx_id
,
825 if (bgp_debug_update(NULL
, dest_p
, subgrp
->update_group
, 0)) {
826 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
828 if (!send_attr_printed
) {
829 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE w/ attr: %s",
830 subgrp
->update_group
->id
, subgrp
->id
,
832 if (!stream_empty(snlri
)) {
834 iana_safi_t pkt_safi
;
836 pkt_afi
= afi_int2iana(afi
);
837 pkt_safi
= safi_int2iana(safi
);
839 "u%" PRIu64
":s%" PRIu64
840 " send MP_REACH for afi/safi %s/%s",
841 subgrp
->update_group
->id
,
843 iana_afi2str(pkt_afi
),
844 iana_safi2str(pkt_safi
));
847 send_attr_printed
= 1;
850 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, dest_p
,
851 label_pnt
, num_labels
,
852 addpath_capable
, addpath_tx_id
,
853 &adv
->baa
->attr
->evpn_overlay
,
854 pfx_buf
, sizeof(pfx_buf
));
855 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE %s",
856 subgrp
->update_group
->id
, subgrp
->id
,
860 /* Synchnorize attribute. */
862 bgp_attr_unintern(&adj
->attr
);
866 adj
->attr
= bgp_attr_intern(adv
->baa
->attr
);
867 adv
= bgp_advertise_clean_subgroup(subgrp
, adj
);
870 if (!stream_empty(s
)) {
871 if (!stream_empty(snlri
)) {
872 bgp_packet_mpattr_end(snlri
, mpattrlen_pos
);
873 total_attr_len
+= stream_get_endp(snlri
);
876 /* set the total attribute length correctly */
877 stream_putw_at(s
, attrlen_pos
, total_attr_len
);
879 if (!stream_empty(snlri
)) {
880 packet
= stream_dupcat(s
, snlri
, mpattr_pos
);
881 bpacket_attr_vec_arr_update(&vecarr
, mpattr_pos
);
883 packet
= stream_dup(s
);
884 bgp_packet_set_size(packet
);
885 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
887 "u%" PRIu64
":s%" PRIu64
888 " send UPDATE len %zd (max message len: %hu) numpfx %d",
889 subgrp
->update_group
->id
, subgrp
->id
,
890 (stream_get_endp(packet
)
891 - stream_get_getp(packet
)),
892 peer
->max_packet_size
, num_pfx
);
893 pkt
= bpacket_queue_add(SUBGRP_PKTQ(subgrp
), packet
, &vecarr
);
901 /* Make BGP withdraw packet. */
903 16-octet marker | 2-octet length | 1-octet type |
904 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0)
906 /* For other afi/safis:
907 16-octet marker | 2-octet length | 1-octet type |
908 2-octet withdrawn route length (=0) | 2-octet attrlen |
909 mp_unreach attr type | attr len | afi | safi | withdrawn prefixes
911 struct bpacket
*subgroup_withdraw_packet(struct update_subgroup
*subgrp
)
915 struct bgp_adj_out
*adj
;
916 struct bgp_advertise
*adv
;
918 struct bgp_dest
*dest
;
919 bgp_size_t unfeasible_len
;
920 bgp_size_t total_attr_len
;
922 size_t attrlen_pos
= 0;
923 size_t mplen_pos
= 0;
924 uint8_t first_time
= 1;
927 int space_remaining
= 0;
928 int space_needed
= 0;
930 bool addpath_capable
= false;
931 int addpath_overhead
= 0;
932 uint32_t addpath_tx_id
= 0;
933 const struct prefix_rd
*prd
= NULL
;
939 if (bpacket_queue_is_full(SUBGRP_INST(subgrp
), SUBGRP_PKTQ(subgrp
)))
942 peer
= SUBGRP_PEER(subgrp
);
943 afi
= SUBGRP_AFI(subgrp
);
944 safi
= SUBGRP_SAFI(subgrp
);
947 addpath_capable
= bgp_addpath_encode_tx(peer
, afi
, safi
);
948 addpath_overhead
= addpath_capable
? BGP_ADDPATH_ID_LEN
: 0;
950 while ((adv
= bgp_adv_fifo_first(&subgrp
->sync
->withdraw
)) != NULL
) {
951 const struct prefix
*dest_p
;
956 dest_p
= bgp_dest_get_prefix(dest
);
957 addpath_tx_id
= adj
->addpath_tx_id
;
960 STREAM_WRITEABLE(s
) - BGP_MAX_PACKET_SIZE_OVERFLOW
;
962 BGP_NLRI_LENGTH
+ addpath_overhead
+ BGP_TOTAL_ATTR_LEN
963 + bgp_packet_mpattr_prefix_size(afi
, safi
, dest_p
);
965 if (space_remaining
< space_needed
)
968 if (stream_empty(s
)) {
969 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
970 stream_putw(s
, 0); /* unfeasible routes length */
974 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
975 && !peer_cap_enhe(peer
, afi
, safi
))
976 stream_put_prefix_addpath(s
, dest_p
, addpath_capable
,
980 prd
= (struct prefix_rd
*)bgp_dest_get_prefix(
983 /* If first time, format the MP_UNREACH header
987 iana_safi_t pkt_safi
;
989 pkt_afi
= afi_int2iana(afi
);
990 pkt_safi
= safi_int2iana(safi
);
992 attrlen_pos
= stream_get_endp(s
);
993 /* total attr length = 0 for now.
994 * reevaluate later */
996 mp_start
= stream_get_endp(s
);
997 mplen_pos
= bgp_packet_mpunreach_start(s
, afi
,
999 if (bgp_debug_update(NULL
, NULL
,
1000 subgrp
->update_group
, 0))
1002 "u%" PRIu64
":s%" PRIu64
1003 " send MP_UNREACH for afi/safi %s/%s",
1004 subgrp
->update_group
->id
,
1006 iana_afi2str(pkt_afi
),
1007 iana_safi2str(pkt_safi
));
1010 bgp_packet_mpunreach_prefix(s
, dest_p
, afi
, safi
, prd
,
1011 NULL
, 0, addpath_capable
,
1012 addpath_tx_id
, NULL
);
1017 if (bgp_debug_update(NULL
, dest_p
, subgrp
->update_group
, 0)) {
1018 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
1020 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, dest_p
, NULL
, 0,
1021 addpath_capable
, addpath_tx_id
,
1022 NULL
, pfx_buf
, sizeof(pfx_buf
));
1023 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE %s -- unreachable",
1024 subgrp
->update_group
->id
, subgrp
->id
,
1030 bgp_adj_out_remove_subgroup(dest
, adj
, subgrp
);
1033 if (!stream_empty(s
)) {
1034 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
1035 && !peer_cap_enhe(peer
, afi
, safi
)) {
1036 unfeasible_len
= stream_get_endp(s
) - BGP_HEADER_SIZE
1037 - BGP_UNFEASIBLE_LEN
;
1038 stream_putw_at(s
, BGP_HEADER_SIZE
, unfeasible_len
);
1041 /* Set the mp_unreach attr's length */
1042 bgp_packet_mpunreach_end(s
, mplen_pos
);
1044 /* Set total path attribute length. */
1045 total_attr_len
= stream_get_endp(s
) - mp_start
;
1046 stream_putw_at(s
, attrlen_pos
, total_attr_len
);
1048 bgp_packet_set_size(s
);
1049 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
1050 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE (withdraw) len %zd numpfx %d",
1051 subgrp
->update_group
->id
, subgrp
->id
,
1052 (stream_get_endp(s
) - stream_get_getp(s
)),
1054 pkt
= bpacket_queue_add(SUBGRP_PKTQ(subgrp
), stream_dup(s
),
1063 void subgroup_default_update_packet(struct update_subgroup
*subgrp
,
1064 struct attr
*attr
, struct peer
*from
)
1070 bgp_size_t total_attr_len
;
1073 struct bpacket_attr_vec_arr vecarr
;
1074 bool addpath_capable
= false;
1075 uint8_t default_originate_label
[4] = {0x80, 0x00, 0x00};
1076 mpls_label_t
*label
= NULL
;
1077 uint32_t num_labels
= 0;
1079 if (DISABLE_BGP_ANNOUNCE
)
1085 peer
= SUBGRP_PEER(subgrp
);
1086 afi
= SUBGRP_AFI(subgrp
);
1087 safi
= SUBGRP_SAFI(subgrp
);
1088 bpacket_attr_vec_arr_reset(&vecarr
);
1089 addpath_capable
= bgp_addpath_encode_tx(peer
, afi
, safi
);
1091 if (safi
== SAFI_LABELED_UNICAST
) {
1092 label
= (mpls_label_t
*)default_originate_label
;
1096 memset(&p
, 0, sizeof(p
));
1097 p
.family
= afi2family(afi
);
1100 /* Logging the attribute. */
1101 if (bgp_debug_update(NULL
, &p
, subgrp
->update_group
, 0)) {
1102 char attrstr
[BUFSIZ
];
1103 /* ' with addpath ID ' 17
1104 * max strlen of uint32 + 10
1105 * +/- (just in case) + 1
1106 * null terminator + 1
1107 * ============================ 29 */
1112 bgp_dump_attr(attr
, attrstr
, sizeof(attrstr
));
1114 if (addpath_capable
)
1115 snprintf(tx_id_buf
, sizeof(tx_id_buf
),
1116 " with addpath ID %u",
1117 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1119 tx_id_buf
[0] = '\0';
1121 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE %pFX%s %s",
1122 (SUBGRP_UPDGRP(subgrp
))->id
, subgrp
->id
, &p
,
1123 tx_id_buf
, attrstr
);
1126 s
= stream_new(peer
->max_packet_size
);
1128 /* Make BGP update packet. */
1129 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
1131 /* Unfeasible Routes Length. */
1134 /* Make place for total attribute length. */
1135 pos
= stream_get_endp(s
);
1137 total_attr_len
= bgp_packet_attribute(
1138 NULL
, peer
, s
, attr
, &vecarr
, &p
, afi
, safi
, from
, NULL
, label
,
1139 num_labels
, addpath_capable
,
1140 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
, NULL
);
1142 /* Set Total Path Attribute Length. */
1143 stream_putw_at(s
, pos
, total_attr_len
);
1146 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
1147 && !peer_cap_enhe(peer
, afi
, safi
))
1148 stream_put_prefix_addpath(
1149 s
, &p
, addpath_capable
,
1150 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1153 bgp_packet_set_size(s
);
1155 (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp
), s
, &vecarr
);
1156 subgroup_trigger_write(subgrp
);
1158 if (!CHECK_FLAG(subgrp
->sflags
,
1159 SUBGRP_STATUS_PEER_DEFAULT_ORIGINATED
)) {
1161 SET_FLAG(subgrp
->sflags
, SUBGRP_STATUS_PEER_DEFAULT_ORIGINATED
);
1165 void subgroup_default_withdraw_packet(struct update_subgroup
*subgrp
)
1170 unsigned long attrlen_pos
= 0;
1172 bgp_size_t unfeasible_len
;
1173 bgp_size_t total_attr_len
= 0;
1174 size_t mp_start
= 0;
1175 size_t mplen_pos
= 0;
1178 bool addpath_capable
= false;
1180 if (DISABLE_BGP_ANNOUNCE
)
1183 peer
= SUBGRP_PEER(subgrp
);
1184 afi
= SUBGRP_AFI(subgrp
);
1185 safi
= SUBGRP_SAFI(subgrp
);
1186 addpath_capable
= bgp_addpath_encode_tx(peer
, afi
, safi
);
1188 memset(&p
, 0, sizeof(p
));
1189 p
.family
= afi2family(afi
);
1192 if (bgp_debug_update(NULL
, &p
, subgrp
->update_group
, 0)) {
1193 /* ' with addpath ID ' 17
1194 * max strlen of uint32 + 10
1195 * +/- (just in case) + 1
1196 * null terminator + 1
1197 * ============================ 29 */
1200 if (addpath_capable
)
1201 snprintf(tx_id_buf
, sizeof(tx_id_buf
),
1202 " with addpath ID %u",
1203 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1205 zlog_debug("u%" PRIu64
":s%" PRIu64
1206 " send UPDATE %pFX%s -- unreachable",
1207 (SUBGRP_UPDGRP(subgrp
))->id
, subgrp
->id
, &p
,
1211 s
= stream_new(peer
->max_packet_size
);
1213 /* Make BGP update packet. */
1214 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
1216 /* Unfeasible Routes Length. */;
1217 cp
= stream_get_endp(s
);
1220 /* Withdrawn Routes. */
1221 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
1222 && !peer_cap_enhe(peer
, afi
, safi
)) {
1223 stream_put_prefix_addpath(
1224 s
, &p
, addpath_capable
,
1225 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1227 unfeasible_len
= stream_get_endp(s
) - cp
- 2;
1229 /* Set unfeasible len. */
1230 stream_putw_at(s
, cp
, unfeasible_len
);
1232 /* Set total path attribute length. */
1235 attrlen_pos
= stream_get_endp(s
);
1237 mp_start
= stream_get_endp(s
);
1238 mplen_pos
= bgp_packet_mpunreach_start(s
, afi
, safi
);
1239 bgp_packet_mpunreach_prefix(
1240 s
, &p
, afi
, safi
, NULL
, NULL
, 0, addpath_capable
,
1241 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
, NULL
);
1243 /* Set the mp_unreach attr's length */
1244 bgp_packet_mpunreach_end(s
, mplen_pos
);
1246 /* Set total path attribute length. */
1247 total_attr_len
= stream_get_endp(s
) - mp_start
;
1248 stream_putw_at(s
, attrlen_pos
, total_attr_len
);
1251 bgp_packet_set_size(s
);
1253 (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp
), s
, NULL
);
1254 subgroup_trigger_write(subgrp
);
1256 if (CHECK_FLAG(subgrp
->sflags
, SUBGRP_STATUS_PEER_DEFAULT_ORIGINATED
)) {
1258 UNSET_FLAG(subgrp
->sflags
,
1259 SUBGRP_STATUS_PEER_DEFAULT_ORIGINATED
);
1264 bpacket_vec_arr_inherit_attr_flags(struct bpacket_attr_vec_arr
*vecarr
,
1265 enum bpacket_attr_vec_type type
,
1268 if (CHECK_FLAG(attr
->rmap_change_flags
,
1269 BATTR_RMAP_NEXTHOP_PEER_ADDRESS
))
1270 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1271 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
);
1273 if (CHECK_FLAG(attr
->rmap_change_flags
, BATTR_REFLECTED
))
1274 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1275 BPKT_ATTRVEC_FLAGS_REFLECTED
);
1277 if (CHECK_FLAG(attr
->rmap_change_flags
, BATTR_RMAP_NEXTHOP_UNCHANGED
))
1278 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1279 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
);
1281 if (CHECK_FLAG(attr
->rmap_change_flags
, BATTR_RMAP_IPV4_NHOP_CHANGED
))
1282 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1283 BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED
);
1285 if (CHECK_FLAG(attr
->rmap_change_flags
,
1286 BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED
))
1287 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1288 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED
);
1290 if (CHECK_FLAG(attr
->rmap_change_flags
, BATTR_RMAP_VPNV4_NHOP_CHANGED
))
1291 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1292 BPKT_ATTRVEC_FLAGS_RMAP_VPNV4_NH_CHANGED
);
1294 if (CHECK_FLAG(attr
->rmap_change_flags
,
1295 BATTR_RMAP_VPNV6_GLOBAL_NHOP_CHANGED
))
1296 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1297 BPKT_ATTRVEC_FLAGS_RMAP_VPNV6_GNH_CHANGED
);
1299 if (CHECK_FLAG(attr
->rmap_change_flags
,
1300 BATTR_RMAP_IPV6_LL_NHOP_CHANGED
))
1301 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1302 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_LNH_CHANGED
);
1305 /* Reset the Attributes vector array. The vector array is used to override
1306 * certain output parameters in the packet for a particular peer
1308 void bpacket_attr_vec_arr_reset(struct bpacket_attr_vec_arr
*vecarr
)
1316 while (i
< BGP_ATTR_VEC_MAX
) {
1317 vecarr
->entries
[i
].flags
= 0;
1318 vecarr
->entries
[i
].offset
= 0;
1323 /* Setup a particular node entry in the vecarr */
1324 void bpacket_attr_vec_arr_set_vec(struct bpacket_attr_vec_arr
*vecarr
,
1325 enum bpacket_attr_vec_type type
,
1326 struct stream
*s
, struct attr
*attr
)
1330 assert(type
< BGP_ATTR_VEC_MAX
);
1332 SET_FLAG(vecarr
->entries
[type
].flags
, BPKT_ATTRVEC_FLAGS_UPDATED
);
1333 vecarr
->entries
[type
].offset
= stream_get_endp(s
);
1335 bpacket_vec_arr_inherit_attr_flags(vecarr
, type
, attr
);