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
)
385 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
));
388 case BGP_ATTR_NHLEN_IPV4
:
390 case BGP_ATTR_NHLEN_VPNV4
:
394 /* TODO: handle IPv6 nexthops */
396 EC_BGP_INVALID_NEXTHOP_LENGTH
,
397 "%s: %s: invalid MP nexthop length (AFI IP): %u",
398 __func__
, peer
->host
, nhlen
);
403 stream_get_from(&v4nh
, s
, offset_nh
, IPV4_MAX_BYTELEN
);
407 * If route-map has set the nexthop, that is normally
408 * used; if it is specified as peer-address, the peering
409 * address is picked up. Otherwise, if NH is unavailable
410 * from attribute, the peering addr is picked up; the
411 * "NH unavailable" case also covers next-hop-self and
412 * some other scenarios - see subgroup_announce_check().
413 * In all other cases, use the nexthop carried in the
414 * attribute unless it is EBGP non-multiaccess and there
415 * is no next-hop-unchanged setting or the peer is EBGP
416 * and the route-map that changed the next-hop value
417 * was applied inbound rather than outbound. Updates to
418 * an EBGP peer should only modify the next-hop if it
419 * was set in an outbound route-map to that peer.
420 * Note: It is assumed route-map cannot set the nexthop
421 * to an invalid value.
423 if (route_map_sets_nh
424 && ((peer
->sort
!= BGP_PEER_EBGP
)
425 || ROUTE_MAP_OUT(filter
))) {
428 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
)) {
429 mod_v4nh
= &peer
->nexthop
.v4
;
432 } else if (v4nh
.s_addr
== INADDR_ANY
) {
433 mod_v4nh
= &peer
->nexthop
.v4
;
435 } else if (peer
->sort
== BGP_PEER_EBGP
436 && (bgp_multiaccess_check_v4(v4nh
, peer
) == 0)
437 && !CHECK_FLAG(vec
->flags
,
438 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
)
439 && !peer_af_flag_check(
440 peer
, paf
->afi
, paf
->safi
,
441 PEER_FLAG_NEXTHOP_UNCHANGED
)) {
442 /* NOTE: not handling case where NH has new AFI
444 mod_v4nh
= &peer
->nexthop
.v4
;
448 if (nh_modified
) /* allow for VPN RD */
449 stream_put_in_addr_at(s
, offset_nh
, mod_v4nh
);
451 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
452 zlog_debug("u%" PRIu64
":s%" PRIu64
453 " %s send UPDATE w/ nexthop %pI4%s",
454 PAF_SUBGRP(paf
)->update_group
->id
,
455 PAF_SUBGRP(paf
)->id
, peer
->host
, mod_v4nh
,
456 (nhlen
== BGP_ATTR_NHLEN_VPNV4
? " and RD"
458 } else if (nhafi
== AFI_IP6
) {
459 struct in6_addr v6nhglobal
, *mod_v6nhg
;
460 struct in6_addr v6nhlocal
, *mod_v6nhl
;
461 int gnh_modified
, lnh_modified
;
462 size_t offset_nhglobal
= vec
->offset
+ 1;
463 size_t offset_nhlocal
= vec
->offset
+ 1;
465 gnh_modified
= lnh_modified
= 0;
466 mod_v6nhg
= &v6nhglobal
;
467 mod_v6nhl
= &v6nhlocal
;
470 (CHECK_FLAG(vec
->flags
,
471 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED
)
474 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
));
477 * The logic here is rather similar to that for IPv4, the
478 * additional work being to handle 1 or 2 nexthops.
479 * Also, 3rd party nexthop is not propagated for EBGP
483 case BGP_ATTR_NHLEN_IPV6_GLOBAL
:
485 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
:
486 offset_nhlocal
+= IPV6_MAX_BYTELEN
;
488 case BGP_ATTR_NHLEN_VPNV6_GLOBAL
:
489 offset_nhglobal
+= 8;
491 case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
:
492 offset_nhglobal
+= 8;
493 offset_nhlocal
+= 8 * 2 + IPV6_MAX_BYTELEN
;
496 /* TODO: handle IPv4 nexthops */
498 EC_BGP_INVALID_NEXTHOP_LENGTH
,
499 "%s: %s: invalid MP nexthop length (AFI IP6): %u",
500 __func__
, peer
->host
, nhlen
);
505 stream_get_from(&v6nhglobal
, s
, offset_nhglobal
,
509 * Updates to an EBGP peer should only modify the
510 * next-hop if it was set in an outbound route-map
513 if (route_map_sets_nh
514 && ((peer
->sort
!= BGP_PEER_EBGP
)
515 || ROUTE_MAP_OUT(filter
))) {
518 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
)) {
519 mod_v6nhg
= &peer
->nexthop
.v6_global
;
522 } else if (IN6_IS_ADDR_UNSPECIFIED(&v6nhglobal
)) {
523 mod_v6nhg
= &peer
->nexthop
.v6_global
;
525 } else if ((peer
->sort
== BGP_PEER_EBGP
)
526 && (!bgp_multiaccess_check_v6(v6nhglobal
, peer
))
527 && !CHECK_FLAG(vec
->flags
,
528 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
)
529 && !peer_af_flag_check(
530 peer
, paf
->afi
, paf
->safi
,
531 PEER_FLAG_NEXTHOP_UNCHANGED
)) {
532 /* NOTE: not handling case where NH has new AFI
534 mod_v6nhg
= &peer
->nexthop
.v6_global
;
538 if (IN6_IS_ADDR_UNSPECIFIED(mod_v6nhg
)) {
539 if (peer
->nexthop
.v4
.s_addr
!= INADDR_ANY
) {
540 ipv4_to_ipv4_mapped_ipv6(mod_v6nhg
,
545 if (IS_MAPPED_IPV6(&peer
->nexthop
.v6_global
)) {
546 mod_v6nhg
= &peer
->nexthop
.v6_global
;
550 if (nhlen
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
551 || nhlen
== BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
552 stream_get_from(&v6nhlocal
, s
, offset_nhlocal
,
554 if (IN6_IS_ADDR_UNSPECIFIED(&v6nhlocal
)) {
555 mod_v6nhl
= &peer
->nexthop
.v6_local
;
561 stream_put_in6_addr_at(s
, offset_nhglobal
, mod_v6nhg
);
563 stream_put_in6_addr_at(s
, offset_nhlocal
, mod_v6nhl
);
565 if (bgp_debug_update(peer
, NULL
, NULL
, 0)) {
566 if (nhlen
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
567 || nhlen
== BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
)
569 "u%" PRIu64
":s%" PRIu64
570 " %s send UPDATE w/ mp_nexthops %pI6, %pI6%s",
571 PAF_SUBGRP(paf
)->update_group
->id
,
572 PAF_SUBGRP(paf
)->id
, peer
->host
,
573 mod_v6nhg
, mod_v6nhl
,
574 (nhlen
== BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
579 "u%" PRIu64
":s%" PRIu64
580 " %s send UPDATE w/ mp_nexthop %pI6%s",
581 PAF_SUBGRP(paf
)->update_group
->id
,
582 PAF_SUBGRP(paf
)->id
, peer
->host
,
584 (nhlen
== BGP_ATTR_NHLEN_VPNV6_GLOBAL
588 } else if (paf
->afi
== AFI_L2VPN
) {
589 struct in_addr v4nh
, *mod_v4nh
;
592 stream_get_from(&v4nh
, s
, vec
->offset
+ 1, 4);
595 /* No route-map changes allowed for EVPN nexthops. */
596 if (v4nh
.s_addr
== INADDR_ANY
) {
597 mod_v4nh
= &peer
->nexthop
.v4
;
602 stream_put_in_addr_at(s
, vec
->offset
+ 1, mod_v4nh
);
604 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
605 zlog_debug("u%" PRIu64
":s%" PRIu64
606 " %s send UPDATE w/ nexthop %pI4",
607 PAF_SUBGRP(paf
)->update_group
->id
,
608 PAF_SUBGRP(paf
)->id
, peer
->host
, mod_v4nh
);
615 * Update the vecarr offsets to go beyond 'pos' bytes, i.e. add 'pos'
618 static void bpacket_attr_vec_arr_update(struct bpacket_attr_vec_arr
*vecarr
,
626 for (i
= 0; i
< BGP_ATTR_VEC_MAX
; i
++)
627 vecarr
->entries
[i
].offset
+= pos
;
631 * Return if there are packets to build for this subgroup.
633 bool subgroup_packets_to_build(struct update_subgroup
*subgrp
)
635 struct bgp_advertise
*adv
;
640 adv
= bgp_adv_fifo_first(&subgrp
->sync
->withdraw
);
644 adv
= bgp_adv_fifo_first(&subgrp
->sync
->update
);
651 /* Make BGP update packet. */
652 struct bpacket
*subgroup_update_packet(struct update_subgroup
*subgrp
)
654 struct bpacket_attr_vec_arr vecarr
;
658 struct stream
*snlri
;
659 struct stream
*packet
;
660 struct bgp_adj_out
*adj
;
661 struct bgp_advertise
*adv
;
662 struct bgp_dest
*dest
= NULL
;
663 struct bgp_path_info
*path
= NULL
;
664 bgp_size_t total_attr_len
= 0;
665 unsigned long attrlen_pos
= 0;
666 size_t mpattrlen_pos
= 0;
667 size_t mpattr_pos
= 0;
670 int space_remaining
= 0;
671 int space_needed
= 0;
672 char send_attr_str
[BUFSIZ
];
673 int send_attr_printed
= 0;
675 bool addpath_capable
= false;
676 int addpath_overhead
= 0;
677 uint32_t addpath_tx_id
= 0;
678 struct prefix_rd
*prd
= NULL
;
679 mpls_label_t label
= MPLS_INVALID_LABEL
, *label_pnt
= NULL
;
680 uint32_t num_labels
= 0;
685 if (bpacket_queue_is_full(SUBGRP_INST(subgrp
), SUBGRP_PKTQ(subgrp
)))
688 peer
= SUBGRP_PEER(subgrp
);
689 afi
= SUBGRP_AFI(subgrp
);
690 safi
= SUBGRP_SAFI(subgrp
);
693 snlri
= subgrp
->scratch
;
696 bpacket_attr_vec_arr_reset(&vecarr
);
698 addpath_capable
= bgp_addpath_encode_tx(peer
, afi
, safi
);
699 addpath_overhead
= addpath_capable
? BGP_ADDPATH_ID_LEN
: 0;
701 adv
= bgp_adv_fifo_first(&subgrp
->sync
->update
);
703 const struct prefix
*dest_p
;
707 dest_p
= bgp_dest_get_prefix(dest
);
709 addpath_tx_id
= adj
->addpath_tx_id
;
712 space_remaining
= STREAM_CONCAT_REMAIN(s
, snlri
, STREAM_SIZE(s
))
713 - BGP_MAX_PACKET_SIZE_OVERFLOW
;
715 BGP_NLRI_LENGTH
+ addpath_overhead
716 + bgp_packet_mpattr_prefix_size(afi
, safi
, dest_p
);
718 /* When remaining space can't include NLRI and it's length. */
719 if (space_remaining
< space_needed
)
722 /* If packet is empty, set attribute. */
723 if (stream_empty(s
)) {
724 struct peer
*from
= NULL
;
729 /* 1: Write the BGP message header - 16 bytes marker, 2
731 * one byte message type.
733 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
735 /* 2: withdrawn routes length */
738 /* 3: total attributes length - attrlen_pos stores the
740 attrlen_pos
= stream_get_endp(s
);
743 /* 4: if there is MP_REACH_NLRI attribute, that should
745 * attribute, according to
746 * draft-ietf-idr-error-handling. Save the
749 mpattr_pos
= stream_get_endp(s
);
751 /* 5: Encode all the attributes, except MP_REACH_NLRI
753 total_attr_len
= bgp_packet_attribute(
754 NULL
, peer
, s
, adv
->baa
->attr
, &vecarr
, NULL
,
755 afi
, safi
, from
, NULL
, NULL
, 0, 0, 0, path
);
758 STREAM_CONCAT_REMAIN(s
, snlri
, STREAM_SIZE(s
))
759 - BGP_MAX_PACKET_SIZE_OVERFLOW
;
760 space_needed
= BGP_NLRI_LENGTH
+ addpath_overhead
761 + bgp_packet_mpattr_prefix_size(
764 /* If the attributes alone do not leave any room for
767 if (space_remaining
< space_needed
) {
769 EC_BGP_UPDGRP_ATTR_LEN
,
770 "u%" PRIu64
":s%" PRIu64
" attributes too long, cannot send UPDATE",
771 subgrp
->update_group
->id
, subgrp
->id
);
773 /* Flush the FIFO update queue */
775 adv
= bgp_advertise_clean_subgroup(
780 if (BGP_DEBUG(update
, UPDATE_OUT
)
781 || BGP_DEBUG(update
, UPDATE_PREFIX
)) {
782 memset(send_attr_str
, 0, BUFSIZ
);
783 send_attr_printed
= 0;
784 bgp_dump_attr(adv
->baa
->attr
, send_attr_str
,
785 sizeof(send_attr_str
));
789 if ((afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
790 && !peer_cap_enhe(peer
, afi
, safi
))
791 stream_put_prefix_addpath(s
, dest_p
, addpath_capable
,
794 /* Encode the prefix in MP_REACH_NLRI attribute */
796 prd
= (struct prefix_rd
*)bgp_dest_get_prefix(
799 if (safi
== SAFI_LABELED_UNICAST
) {
800 label
= bgp_adv_label(dest
, path
, peer
, afi
,
804 } else if (path
&& path
->extra
) {
805 label_pnt
= &path
->extra
->label
[0];
806 num_labels
= path
->extra
->num_labels
;
809 if (stream_empty(snlri
))
810 mpattrlen_pos
= bgp_packet_mpattr_start(
811 snlri
, peer
, afi
, safi
, &vecarr
,
814 bgp_packet_mpattr_prefix(snlri
, afi
, safi
, dest_p
, prd
,
815 label_pnt
, num_labels
,
816 addpath_capable
, addpath_tx_id
,
822 if (bgp_debug_update(NULL
, dest_p
, subgrp
->update_group
, 0)) {
823 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
825 if (!send_attr_printed
) {
826 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE w/ attr: %s",
827 subgrp
->update_group
->id
, subgrp
->id
,
829 if (!stream_empty(snlri
)) {
831 iana_safi_t pkt_safi
;
833 pkt_afi
= afi_int2iana(afi
);
834 pkt_safi
= safi_int2iana(safi
);
836 "u%" PRIu64
":s%" PRIu64
837 " send MP_REACH for afi/safi %s/%s",
838 subgrp
->update_group
->id
,
840 iana_afi2str(pkt_afi
),
841 iana_safi2str(pkt_safi
));
844 send_attr_printed
= 1;
847 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, dest_p
,
848 label_pnt
, num_labels
,
849 addpath_capable
, addpath_tx_id
,
850 &adv
->baa
->attr
->evpn_overlay
,
851 pfx_buf
, sizeof(pfx_buf
));
852 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE %s",
853 subgrp
->update_group
->id
, subgrp
->id
,
857 /* Synchnorize attribute. */
859 bgp_attr_unintern(&adj
->attr
);
863 adj
->attr
= bgp_attr_intern(adv
->baa
->attr
);
864 adv
= bgp_advertise_clean_subgroup(subgrp
, adj
);
867 if (!stream_empty(s
)) {
868 if (!stream_empty(snlri
)) {
869 bgp_packet_mpattr_end(snlri
, mpattrlen_pos
);
870 total_attr_len
+= stream_get_endp(snlri
);
873 /* set the total attribute length correctly */
874 stream_putw_at(s
, attrlen_pos
, total_attr_len
);
876 if (!stream_empty(snlri
)) {
877 packet
= stream_dupcat(s
, snlri
, mpattr_pos
);
878 bpacket_attr_vec_arr_update(&vecarr
, mpattr_pos
);
880 packet
= stream_dup(s
);
881 bgp_packet_set_size(packet
);
882 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
884 "u%" PRIu64
":s%" PRIu64
885 " send UPDATE len %zd (max message len: %hu) numpfx %d",
886 subgrp
->update_group
->id
, subgrp
->id
,
887 (stream_get_endp(packet
)
888 - stream_get_getp(packet
)),
889 peer
->max_packet_size
, num_pfx
);
890 pkt
= bpacket_queue_add(SUBGRP_PKTQ(subgrp
), packet
, &vecarr
);
898 /* Make BGP withdraw packet. */
900 16-octet marker | 2-octet length | 1-octet type |
901 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0)
903 /* For other afi/safis:
904 16-octet marker | 2-octet length | 1-octet type |
905 2-octet withdrawn route length (=0) | 2-octet attrlen |
906 mp_unreach attr type | attr len | afi | safi | withdrawn prefixes
908 struct bpacket
*subgroup_withdraw_packet(struct update_subgroup
*subgrp
)
912 struct bgp_adj_out
*adj
;
913 struct bgp_advertise
*adv
;
915 struct bgp_dest
*dest
;
916 bgp_size_t unfeasible_len
;
917 bgp_size_t total_attr_len
;
919 size_t attrlen_pos
= 0;
920 size_t mplen_pos
= 0;
921 uint8_t first_time
= 1;
924 int space_remaining
= 0;
925 int space_needed
= 0;
927 bool addpath_capable
= false;
928 int addpath_overhead
= 0;
929 uint32_t addpath_tx_id
= 0;
930 const struct prefix_rd
*prd
= NULL
;
936 if (bpacket_queue_is_full(SUBGRP_INST(subgrp
), SUBGRP_PKTQ(subgrp
)))
939 peer
= SUBGRP_PEER(subgrp
);
940 afi
= SUBGRP_AFI(subgrp
);
941 safi
= SUBGRP_SAFI(subgrp
);
944 addpath_capable
= bgp_addpath_encode_tx(peer
, afi
, safi
);
945 addpath_overhead
= addpath_capable
? BGP_ADDPATH_ID_LEN
: 0;
947 while ((adv
= bgp_adv_fifo_first(&subgrp
->sync
->withdraw
)) != NULL
) {
948 const struct prefix
*dest_p
;
953 dest_p
= bgp_dest_get_prefix(dest
);
954 addpath_tx_id
= adj
->addpath_tx_id
;
957 STREAM_WRITEABLE(s
) - BGP_MAX_PACKET_SIZE_OVERFLOW
;
959 BGP_NLRI_LENGTH
+ addpath_overhead
+ BGP_TOTAL_ATTR_LEN
960 + bgp_packet_mpattr_prefix_size(afi
, safi
, dest_p
);
962 if (space_remaining
< space_needed
)
965 if (stream_empty(s
)) {
966 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
967 stream_putw(s
, 0); /* unfeasible routes length */
971 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
972 && !peer_cap_enhe(peer
, afi
, safi
))
973 stream_put_prefix_addpath(s
, dest_p
, addpath_capable
,
977 prd
= (struct prefix_rd
*)bgp_dest_get_prefix(
980 /* If first time, format the MP_UNREACH header
984 iana_safi_t pkt_safi
;
986 pkt_afi
= afi_int2iana(afi
);
987 pkt_safi
= safi_int2iana(safi
);
989 attrlen_pos
= stream_get_endp(s
);
990 /* total attr length = 0 for now.
991 * reevaluate later */
993 mp_start
= stream_get_endp(s
);
994 mplen_pos
= bgp_packet_mpunreach_start(s
, afi
,
996 if (bgp_debug_update(NULL
, NULL
,
997 subgrp
->update_group
, 0))
999 "u%" PRIu64
":s%" PRIu64
1000 " send MP_UNREACH for afi/safi %s/%s",
1001 subgrp
->update_group
->id
,
1003 iana_afi2str(pkt_afi
),
1004 iana_safi2str(pkt_safi
));
1007 bgp_packet_mpunreach_prefix(s
, dest_p
, afi
, safi
, prd
,
1008 NULL
, 0, addpath_capable
,
1009 addpath_tx_id
, NULL
);
1014 if (bgp_debug_update(NULL
, dest_p
, subgrp
->update_group
, 0)) {
1015 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
1017 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, dest_p
, NULL
, 0,
1018 addpath_capable
, addpath_tx_id
,
1019 NULL
, pfx_buf
, sizeof(pfx_buf
));
1020 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE %s -- unreachable",
1021 subgrp
->update_group
->id
, subgrp
->id
,
1027 bgp_adj_out_remove_subgroup(dest
, adj
, subgrp
);
1030 if (!stream_empty(s
)) {
1031 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
1032 && !peer_cap_enhe(peer
, afi
, safi
)) {
1033 unfeasible_len
= stream_get_endp(s
) - BGP_HEADER_SIZE
1034 - BGP_UNFEASIBLE_LEN
;
1035 stream_putw_at(s
, BGP_HEADER_SIZE
, unfeasible_len
);
1038 /* Set the mp_unreach attr's length */
1039 bgp_packet_mpunreach_end(s
, mplen_pos
);
1041 /* Set total path attribute length. */
1042 total_attr_len
= stream_get_endp(s
) - mp_start
;
1043 stream_putw_at(s
, attrlen_pos
, total_attr_len
);
1045 bgp_packet_set_size(s
);
1046 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
1047 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE (withdraw) len %zd numpfx %d",
1048 subgrp
->update_group
->id
, subgrp
->id
,
1049 (stream_get_endp(s
) - stream_get_getp(s
)),
1051 pkt
= bpacket_queue_add(SUBGRP_PKTQ(subgrp
), stream_dup(s
),
1060 void subgroup_default_update_packet(struct update_subgroup
*subgrp
,
1061 struct attr
*attr
, struct peer
*from
)
1067 bgp_size_t total_attr_len
;
1070 struct bpacket_attr_vec_arr vecarr
;
1071 bool addpath_capable
= false;
1073 if (DISABLE_BGP_ANNOUNCE
)
1079 peer
= SUBGRP_PEER(subgrp
);
1080 afi
= SUBGRP_AFI(subgrp
);
1081 safi
= SUBGRP_SAFI(subgrp
);
1082 bpacket_attr_vec_arr_reset(&vecarr
);
1083 addpath_capable
= bgp_addpath_encode_tx(peer
, afi
, safi
);
1085 memset(&p
, 0, sizeof(p
));
1086 p
.family
= afi2family(afi
);
1089 /* Logging the attribute. */
1090 if (bgp_debug_update(NULL
, &p
, subgrp
->update_group
, 0)) {
1091 char attrstr
[BUFSIZ
];
1092 /* ' with addpath ID ' 17
1093 * max strlen of uint32 + 10
1094 * +/- (just in case) + 1
1095 * null terminator + 1
1096 * ============================ 29 */
1101 bgp_dump_attr(attr
, attrstr
, sizeof(attrstr
));
1103 if (addpath_capable
)
1104 snprintf(tx_id_buf
, sizeof(tx_id_buf
),
1105 " with addpath ID %u",
1106 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1108 tx_id_buf
[0] = '\0';
1110 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE %pFX%s %s",
1111 (SUBGRP_UPDGRP(subgrp
))->id
, subgrp
->id
, &p
,
1112 tx_id_buf
, attrstr
);
1115 s
= stream_new(peer
->max_packet_size
);
1117 /* Make BGP update packet. */
1118 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
1120 /* Unfeasible Routes Length. */
1123 /* Make place for total attribute length. */
1124 pos
= stream_get_endp(s
);
1126 total_attr_len
= bgp_packet_attribute(
1127 NULL
, peer
, s
, attr
, &vecarr
, &p
, afi
, safi
, from
, NULL
, NULL
,
1128 0, addpath_capable
, BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
,
1131 /* Set Total Path Attribute Length. */
1132 stream_putw_at(s
, pos
, total_attr_len
);
1135 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
1136 && !peer_cap_enhe(peer
, afi
, safi
))
1137 stream_put_prefix_addpath(
1138 s
, &p
, addpath_capable
,
1139 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1142 bgp_packet_set_size(s
);
1144 (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp
), s
, &vecarr
);
1145 subgroup_trigger_write(subgrp
);
1147 if (!CHECK_FLAG(subgrp
->sflags
,
1148 SUBGRP_STATUS_PEER_DEFAULT_ORIGINATED
)) {
1150 SET_FLAG(subgrp
->sflags
, SUBGRP_STATUS_PEER_DEFAULT_ORIGINATED
);
1154 void subgroup_default_withdraw_packet(struct update_subgroup
*subgrp
)
1159 unsigned long attrlen_pos
= 0;
1161 bgp_size_t unfeasible_len
;
1162 bgp_size_t total_attr_len
= 0;
1163 size_t mp_start
= 0;
1164 size_t mplen_pos
= 0;
1167 bool addpath_capable
= false;
1169 if (DISABLE_BGP_ANNOUNCE
)
1172 peer
= SUBGRP_PEER(subgrp
);
1173 afi
= SUBGRP_AFI(subgrp
);
1174 safi
= SUBGRP_SAFI(subgrp
);
1175 addpath_capable
= bgp_addpath_encode_tx(peer
, afi
, safi
);
1177 memset(&p
, 0, sizeof(p
));
1178 p
.family
= afi2family(afi
);
1181 if (bgp_debug_update(NULL
, &p
, subgrp
->update_group
, 0)) {
1182 /* ' with addpath ID ' 17
1183 * max strlen of uint32 + 10
1184 * +/- (just in case) + 1
1185 * null terminator + 1
1186 * ============================ 29 */
1189 if (addpath_capable
)
1190 snprintf(tx_id_buf
, sizeof(tx_id_buf
),
1191 " with addpath ID %u",
1192 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1194 zlog_debug("u%" PRIu64
":s%" PRIu64
1195 " send UPDATE %pFX%s -- unreachable",
1196 (SUBGRP_UPDGRP(subgrp
))->id
, subgrp
->id
, &p
,
1200 s
= stream_new(peer
->max_packet_size
);
1202 /* Make BGP update packet. */
1203 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
1205 /* Unfeasible Routes Length. */;
1206 cp
= stream_get_endp(s
);
1209 /* Withdrawn Routes. */
1210 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
1211 && !peer_cap_enhe(peer
, afi
, safi
)) {
1212 stream_put_prefix_addpath(
1213 s
, &p
, addpath_capable
,
1214 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1216 unfeasible_len
= stream_get_endp(s
) - cp
- 2;
1218 /* Set unfeasible len. */
1219 stream_putw_at(s
, cp
, unfeasible_len
);
1221 /* Set total path attribute length. */
1224 attrlen_pos
= stream_get_endp(s
);
1226 mp_start
= stream_get_endp(s
);
1227 mplen_pos
= bgp_packet_mpunreach_start(s
, afi
, safi
);
1228 bgp_packet_mpunreach_prefix(
1229 s
, &p
, afi
, safi
, NULL
, NULL
, 0, addpath_capable
,
1230 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
, NULL
);
1232 /* Set the mp_unreach attr's length */
1233 bgp_packet_mpunreach_end(s
, mplen_pos
);
1235 /* Set total path attribute length. */
1236 total_attr_len
= stream_get_endp(s
) - mp_start
;
1237 stream_putw_at(s
, attrlen_pos
, total_attr_len
);
1240 bgp_packet_set_size(s
);
1242 (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp
), s
, NULL
);
1243 subgroup_trigger_write(subgrp
);
1245 if (CHECK_FLAG(subgrp
->sflags
, SUBGRP_STATUS_PEER_DEFAULT_ORIGINATED
)) {
1247 UNSET_FLAG(subgrp
->sflags
,
1248 SUBGRP_STATUS_PEER_DEFAULT_ORIGINATED
);
1253 bpacket_vec_arr_inherit_attr_flags(struct bpacket_attr_vec_arr
*vecarr
,
1254 enum bpacket_attr_vec_type type
,
1257 if (CHECK_FLAG(attr
->rmap_change_flags
,
1258 BATTR_RMAP_NEXTHOP_PEER_ADDRESS
))
1259 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1260 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
);
1262 if (CHECK_FLAG(attr
->rmap_change_flags
, BATTR_REFLECTED
))
1263 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1264 BPKT_ATTRVEC_FLAGS_REFLECTED
);
1266 if (CHECK_FLAG(attr
->rmap_change_flags
, BATTR_RMAP_NEXTHOP_UNCHANGED
))
1267 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1268 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
);
1270 if (CHECK_FLAG(attr
->rmap_change_flags
, BATTR_RMAP_IPV4_NHOP_CHANGED
))
1271 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1272 BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED
);
1274 if (CHECK_FLAG(attr
->rmap_change_flags
,
1275 BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED
))
1276 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1277 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED
);
1279 if (CHECK_FLAG(attr
->rmap_change_flags
,
1280 BATTR_RMAP_IPV6_LL_NHOP_CHANGED
))
1281 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1282 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_LNH_CHANGED
);
1285 /* Reset the Attributes vector array. The vector array is used to override
1286 * certain output parameters in the packet for a particular peer
1288 void bpacket_attr_vec_arr_reset(struct bpacket_attr_vec_arr
*vecarr
)
1296 while (i
< BGP_ATTR_VEC_MAX
) {
1297 vecarr
->entries
[i
].flags
= 0;
1298 vecarr
->entries
[i
].offset
= 0;
1303 /* Setup a particular node entry in the vecarr */
1304 void bpacket_attr_vec_arr_set_vec(struct bpacket_attr_vec_arr
*vecarr
,
1305 enum bpacket_attr_vec_type type
,
1306 struct stream
*s
, struct attr
*attr
)
1310 assert(type
< BGP_ATTR_VEC_MAX
);
1312 SET_FLAG(vecarr
->entries
[type
].flags
, BPKT_ATTRVEC_FLAGS_UPDATED
);
1313 vecarr
->entries
[type
].offset
= stream_get_endp(s
);
1315 bpacket_vec_arr_inherit_attr_flags(vecarr
, type
, attr
);