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_fsm.h"
50 #include "bgpd/bgp_route.h"
51 #include "bgpd/bgp_packet.h"
52 #include "bgpd/bgp_advertise.h"
53 #include "bgpd/bgp_updgrp.h"
54 #include "bgpd/bgp_nexthop.h"
55 #include "bgpd/bgp_nht.h"
56 #include "bgpd/bgp_mplsvpn.h"
57 #include "bgpd/bgp_label.h"
66 struct bpacket
*bpacket_alloc()
70 pkt
= (struct bpacket
*)XCALLOC(MTYPE_BGP_PACKET
,
71 sizeof(struct bpacket
));
76 void bpacket_free(struct bpacket
*pkt
)
79 stream_free(pkt
->buffer
);
81 XFREE(MTYPE_BGP_PACKET
, pkt
);
84 void bpacket_queue_init(struct bpacket_queue
*q
)
86 TAILQ_INIT(&(q
->pkts
));
90 * bpacket_queue_sanity_check
92 void bpacket_queue_sanity_check(struct bpacket_queue
__attribute__((__unused__
))
98 pkt
= bpacket_queue_last (q
);
100 assert (!pkt
->buffer
);
103 * Make sure the count of packets is correct.
107 pkt
= bpacket_queue_first (q
);
112 if (num_pkts
> q
->curr_count
)
115 pkt
= TAILQ_NEXT (pkt
, pkt_train
);
118 assert (num_pkts
== q
->curr_count
);
123 * bpacket_queue_add_packet
125 * Internal function of bpacket_queue - and adds a
126 * packet entry to the end of the list.
128 * Users of bpacket_queue should use bpacket_queue_add instead.
130 static void bpacket_queue_add_packet(struct bpacket_queue
*q
,
133 struct bpacket
*last_pkt
;
135 if (TAILQ_EMPTY(&(q
->pkts
)))
136 TAILQ_INSERT_TAIL(&(q
->pkts
), pkt
, pkt_train
);
138 last_pkt
= bpacket_queue_last(q
);
139 TAILQ_INSERT_AFTER(&(q
->pkts
), last_pkt
, pkt
, pkt_train
);
142 if (q
->hwm_count
< q
->curr_count
)
143 q
->hwm_count
= q
->curr_count
;
147 * Adds a packet to the bpacket_queue.
149 * The stream passed is consumed by this function. So, the caller should
150 * not free or use the stream after
151 * invoking this function.
153 struct bpacket
*bpacket_queue_add(struct bpacket_queue
*q
, struct stream
*s
,
154 struct bpacket_attr_vec_arr
*vecarrp
)
157 struct bpacket
*last_pkt
;
160 pkt
= bpacket_alloc();
161 if (TAILQ_EMPTY(&(q
->pkts
))) {
165 memcpy(&pkt
->arr
, vecarrp
,
166 sizeof(struct bpacket_attr_vec_arr
));
168 bpacket_attr_vec_arr_reset(&pkt
->arr
);
169 bpacket_queue_add_packet(q
, pkt
);
170 bpacket_queue_sanity_check(q
);
175 * Fill in the new information into the current sentinel and create a
178 bpacket_queue_sanity_check(q
);
179 last_pkt
= bpacket_queue_last(q
);
180 assert(last_pkt
->buffer
== NULL
);
181 last_pkt
->buffer
= s
;
183 memcpy(&last_pkt
->arr
, vecarrp
,
184 sizeof(struct bpacket_attr_vec_arr
));
186 bpacket_attr_vec_arr_reset(&last_pkt
->arr
);
188 pkt
->ver
= last_pkt
->ver
;
190 bpacket_queue_add_packet(q
, pkt
);
192 bpacket_queue_sanity_check(q
);
196 struct bpacket
*bpacket_queue_first(struct bpacket_queue
*q
)
198 return (TAILQ_FIRST(&(q
->pkts
)));
201 struct bpacket
*bpacket_queue_last(struct bpacket_queue
*q
)
203 return TAILQ_LAST(&(q
->pkts
), pkt_queue
);
206 struct bpacket
*bpacket_queue_remove(struct bpacket_queue
*q
)
208 struct bpacket
*first
;
210 first
= bpacket_queue_first(q
);
212 TAILQ_REMOVE(&(q
->pkts
), first
, pkt_train
);
218 unsigned int bpacket_queue_length(struct bpacket_queue
*q
)
220 return q
->curr_count
- 1;
223 unsigned int bpacket_queue_hwm_length(struct bpacket_queue
*q
)
225 return q
->hwm_count
- 1;
228 int bpacket_queue_is_full(struct bgp
*bgp
, struct bpacket_queue
*q
)
230 if (q
->curr_count
>= bgp
->default_subgroup_pkt_queue_max
)
235 void bpacket_add_peer(struct bpacket
*pkt
, struct peer_af
*paf
)
240 LIST_INSERT_HEAD(&(pkt
->peers
), paf
, pkt_train
);
241 paf
->next_pkt_to_send
= pkt
;
245 * bpacket_queue_cleanup
247 void bpacket_queue_cleanup(struct bpacket_queue
*q
)
251 while ((pkt
= bpacket_queue_remove(q
))) {
257 * bpacket_queue_compact
259 * Delete packets that do not need to be transmitted to any peer from
262 * @return the number of packets deleted.
264 static int bpacket_queue_compact(struct bpacket_queue
*q
)
267 struct bpacket
*pkt
, *removed_pkt
;
272 pkt
= bpacket_queue_first(q
);
277 * Don't delete the sentinel.
282 if (!LIST_EMPTY(&(pkt
->peers
)))
285 removed_pkt
= bpacket_queue_remove(q
);
286 assert(pkt
== removed_pkt
);
287 bpacket_free(removed_pkt
);
292 bpacket_queue_sanity_check(q
);
296 void bpacket_queue_advance_peer(struct peer_af
*paf
)
299 struct bpacket
*old_pkt
;
301 old_pkt
= paf
->next_pkt_to_send
;
302 if (old_pkt
->buffer
== NULL
)
303 /* Already at end of list */
306 LIST_REMOVE(paf
, pkt_train
);
307 pkt
= TAILQ_NEXT(old_pkt
, pkt_train
);
308 bpacket_add_peer(pkt
, paf
);
310 if (!bpacket_queue_compact(PAF_PKTQ(paf
)))
314 * Deleted one or more packets. Check if we can now merge this
315 * peer's subgroup into another subgroup.
317 update_subgroup_check_merge(paf
->subgroup
, "advanced peer in queue");
321 * bpacket_queue_remove_peer
323 * Remove the peer from the packet queue of the subgroup it belongs
326 void bpacket_queue_remove_peer(struct peer_af
*paf
)
328 struct bpacket_queue
*q
;
335 LIST_REMOVE(paf
, pkt_train
);
336 paf
->next_pkt_to_send
= NULL
;
338 bpacket_queue_compact(q
);
341 unsigned int bpacket_queue_virtual_length(struct peer_af
*paf
)
344 struct bpacket
*last
;
345 struct bpacket_queue
*q
;
347 pkt
= paf
->next_pkt_to_send
;
348 if (!pkt
|| (pkt
->buffer
== NULL
))
349 /* Already at end of list */
353 if (TAILQ_EMPTY(&(q
->pkts
)))
356 last
= TAILQ_LAST(&(q
->pkts
), pkt_queue
);
357 if (last
->ver
>= pkt
->ver
)
358 return last
->ver
- pkt
->ver
;
360 /* sequence # rolled over */
361 return (UINT_MAX
- pkt
->ver
+ 1) + last
->ver
;
365 * Dump the bpacket queue
367 void bpacket_queue_show_vty(struct bpacket_queue
*q
, struct vty
*vty
)
372 pkt
= bpacket_queue_first(q
);
374 vty_out(vty
, " Packet %p ver %u buffer %p\n", pkt
, pkt
->ver
,
377 LIST_FOREACH (paf
, &(pkt
->peers
), pkt_train
) {
378 vty_out(vty
, " - %s\n", paf
->peer
->host
);
380 pkt
= bpacket_next(pkt
);
385 struct stream
*bpacket_reformat_for_peer(struct bpacket
*pkt
,
388 struct stream
*s
= NULL
;
389 bpacket_attr_vec
*vec
;
394 s
= stream_dup(pkt
->buffer
);
395 peer
= PAF_PEER(paf
);
397 vec
= &pkt
->arr
.entries
[BGP_ATTR_VEC_NH
];
398 if (CHECK_FLAG(vec
->flags
, BPKT_ATTRVEC_FLAGS_UPDATED
)) {
400 afi_t nhafi
= AFI_MAX
; /* NH AFI is based on nhlen! */
401 int route_map_sets_nh
;
402 nhlen
= stream_getc_from(s
, vec
->offset
);
403 if (peer_cap_enhe(peer
, paf
->afi
, paf
->safi
))
406 nhafi
= BGP_NEXTHOP_AFI_FROM_NHLEN(nhlen
);
408 if (nhafi
== AFI_IP
) {
409 struct in_addr v4nh
, *mod_v4nh
;
411 size_t offset_nh
= vec
->offset
+ 1;
416 BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED
)
419 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
));
422 case BGP_ATTR_NHLEN_IPV4
:
424 case BGP_ATTR_NHLEN_VPNV4
:
428 /* TODO: handle IPv6 nexthops */
430 "%s: %s: invalid MP nexthop length (AFI IP): %u",
431 __func__
, peer
->host
, nhlen
);
436 stream_get_from(&v4nh
, s
, offset_nh
, IPV4_MAX_BYTELEN
);
440 * If route-map has set the nexthop, that is always
442 * specified as peer-address, the peering address is
444 * Otherwise, if NH is unavailable from attribute, the
446 * is picked up; the "NH unavailable" case also covers
448 * and some other scenarios -- see
449 * subgroup_announce_check(). In
450 * all other cases, use the nexthop carried in the
452 * it is EBGP non-multiaccess and there is no
453 * next-hop-unchanged setting.
454 * Note: It is assumed route-map cannot set the nexthop
458 if (route_map_sets_nh
) {
461 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
)) {
462 mod_v4nh
= &peer
->nexthop
.v4
;
465 } else if (!v4nh
.s_addr
) {
466 mod_v4nh
= &peer
->nexthop
.v4
;
469 peer
->sort
== BGP_PEER_EBGP
470 && paf
->safi
!= SAFI_EVPN
471 && (bgp_multiaccess_check_v4(v4nh
, peer
) == 0)
474 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
)
475 && !peer_af_flag_check(
476 peer
, nhafi
, paf
->safi
,
477 PEER_FLAG_NEXTHOP_UNCHANGED
)) {
478 /* NOTE: not handling case where NH has new AFI
480 mod_v4nh
= &peer
->nexthop
.v4
;
484 if (nh_modified
) /* allow for VPN RD */
485 stream_put_in_addr_at(s
, offset_nh
, mod_v4nh
);
487 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
488 zlog_debug("u%" PRIu64
":s%" PRIu64
489 " %s send UPDATE w/ nexthop %s%s",
490 PAF_SUBGRP(paf
)->update_group
->id
,
491 PAF_SUBGRP(paf
)->id
, peer
->host
,
492 inet_ntoa(*mod_v4nh
),
493 (nhlen
== 12 ? " and RD" : ""));
494 } else if (nhafi
== AFI_IP6
) {
495 struct in6_addr v6nhglobal
, *mod_v6nhg
;
496 struct in6_addr v6nhlocal
, *mod_v6nhl
;
497 int gnh_modified
, lnh_modified
;
498 size_t offset_nhglobal
= vec
->offset
+ 1;
499 size_t offset_nhlocal
= vec
->offset
+ 1;
501 gnh_modified
= lnh_modified
= 0;
502 mod_v6nhg
= &v6nhglobal
;
503 mod_v6nhl
= &v6nhlocal
;
508 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED
)
511 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
));
514 * The logic here is rather similar to that for IPv4,
516 * additional work being to handle 1 or 2 nexthops.
518 * party nexthop is not propagated for EBGP right now.
521 case BGP_ATTR_NHLEN_IPV6_GLOBAL
:
523 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
:
524 offset_nhlocal
+= IPV6_MAX_BYTELEN
;
526 case BGP_ATTR_NHLEN_VPNV6_GLOBAL
:
527 offset_nhglobal
+= 8;
529 case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
:
530 offset_nhglobal
+= 8;
531 offset_nhlocal
+= 8 * 2 + IPV6_MAX_BYTELEN
;
534 /* TODO: handle IPv4 nexthops */
536 "%s: %s: invalid MP nexthop length (AFI IP6): %u",
537 __func__
, peer
->host
, nhlen
);
542 stream_get_from(&v6nhglobal
, s
, offset_nhglobal
,
544 if (route_map_sets_nh
) {
547 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
)) {
548 mod_v6nhg
= &peer
->nexthop
.v6_global
;
551 } else if (IN6_IS_ADDR_UNSPECIFIED(&v6nhglobal
)) {
552 mod_v6nhg
= &peer
->nexthop
.v6_global
;
555 peer
->sort
== BGP_PEER_EBGP
558 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
)
559 && !peer_af_flag_check(
560 peer
, nhafi
, paf
->safi
,
561 PEER_FLAG_NEXTHOP_UNCHANGED
)) {
562 /* NOTE: not handling case where NH has new AFI
564 mod_v6nhg
= &peer
->nexthop
.v6_global
;
569 if (nhlen
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
570 || nhlen
== BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
571 stream_get_from(&v6nhlocal
, s
, offset_nhlocal
,
573 if (IN6_IS_ADDR_UNSPECIFIED(&v6nhlocal
)) {
574 mod_v6nhl
= &peer
->nexthop
.v6_local
;
580 stream_put_in6_addr_at(s
, offset_nhglobal
,
583 stream_put_in6_addr_at(s
, offset_nhlocal
,
586 if (bgp_debug_update(peer
, NULL
, NULL
, 0)) {
587 if (nhlen
== 32 || nhlen
== 48)
589 "u%" PRIu64
":s%" PRIu64
590 " %s send UPDATE w/ mp_nexthops %s, %s%s",
593 PAF_SUBGRP(paf
)->id
, peer
->host
,
594 inet_ntop(AF_INET6
, mod_v6nhg
,
596 inet_ntop(AF_INET6
, mod_v6nhl
,
598 (nhlen
== 48 ? " and RD" : ""));
601 "u%" PRIu64
":s%" PRIu64
602 " %s send UPDATE w/ mp_nexthop %s%s",
605 PAF_SUBGRP(paf
)->id
, peer
->host
,
606 inet_ntop(AF_INET6
, mod_v6nhg
,
608 (nhlen
== 24 ? " and RD" : ""));
610 } else if (paf
->afi
== AFI_L2VPN
) {
611 struct in_addr v4nh
, *mod_v4nh
;
614 stream_get_from(&v4nh
, s
, vec
->offset
+ 1, 4);
617 /* No route-map changes allowed for EVPN nexthops. */
619 mod_v4nh
= &peer
->nexthop
.v4
;
624 stream_put_in_addr_at(s
, vec
->offset
+ 1,
627 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
628 zlog_debug("u%" PRIu64
":s%" PRIu64
629 " %s send UPDATE w/ nexthop %s",
630 PAF_SUBGRP(paf
)->update_group
->id
,
631 PAF_SUBGRP(paf
)->id
, peer
->host
,
632 inet_ntoa(*mod_v4nh
));
640 * Update the vecarr offsets to go beyond 'pos' bytes, i.e. add 'pos'
643 static void bpacket_attr_vec_arr_update(struct bpacket_attr_vec_arr
*vecarr
,
651 for (i
= 0; i
< BGP_ATTR_VEC_MAX
; i
++)
652 vecarr
->entries
[i
].offset
+= pos
;
656 * Return if there are packets to build for this subgroup.
658 int subgroup_packets_to_build(struct update_subgroup
*subgrp
)
660 struct bgp_advertise
*adv
;
665 adv
= BGP_ADV_FIFO_HEAD(&subgrp
->sync
->withdraw
);
669 adv
= BGP_ADV_FIFO_HEAD(&subgrp
->sync
->update
);
676 /* Make BGP update packet. */
677 struct bpacket
*subgroup_update_packet(struct update_subgroup
*subgrp
)
679 struct bpacket_attr_vec_arr vecarr
;
683 struct stream
*snlri
;
684 struct stream
*packet
;
685 struct bgp_adj_out
*adj
;
686 struct bgp_advertise
*adv
;
687 struct bgp_node
*rn
= NULL
;
688 struct bgp_info
*binfo
= NULL
;
689 bgp_size_t total_attr_len
= 0;
690 unsigned long attrlen_pos
= 0;
691 size_t mpattrlen_pos
= 0;
692 size_t mpattr_pos
= 0;
695 int space_remaining
= 0;
696 int space_needed
= 0;
697 char send_attr_str
[BUFSIZ
];
698 int send_attr_printed
= 0;
700 int addpath_encode
= 0;
701 int addpath_overhead
= 0;
702 u_int32_t addpath_tx_id
= 0;
703 struct prefix_rd
*prd
= NULL
;
704 mpls_label_t label
= MPLS_INVALID_LABEL
;
709 if (bpacket_queue_is_full(SUBGRP_INST(subgrp
), SUBGRP_PKTQ(subgrp
)))
712 peer
= SUBGRP_PEER(subgrp
);
713 afi
= SUBGRP_AFI(subgrp
);
714 safi
= SUBGRP_SAFI(subgrp
);
717 snlri
= subgrp
->scratch
;
720 bpacket_attr_vec_arr_reset(&vecarr
);
722 addpath_encode
= bgp_addpath_encode_tx(peer
, afi
, safi
);
723 addpath_overhead
= addpath_encode
? BGP_ADDPATH_ID_LEN
: 0;
725 adv
= BGP_ADV_FIFO_HEAD(&subgrp
->sync
->update
);
730 addpath_tx_id
= adj
->addpath_tx_id
;
733 space_remaining
= STREAM_CONCAT_REMAIN(s
, snlri
, STREAM_SIZE(s
))
734 - BGP_MAX_PACKET_SIZE_OVERFLOW
;
735 space_needed
= BGP_NLRI_LENGTH
+ addpath_overhead
+
736 bgp_packet_mpattr_prefix_size(afi
, safi
, &rn
->p
);
738 /* When remaining space can't include NLRI and it's length. */
739 if (space_remaining
< space_needed
)
742 /* If packet is empty, set attribute. */
743 if (stream_empty(s
)) {
744 struct peer
*from
= NULL
;
749 /* 1: Write the BGP message header - 16 bytes marker, 2
751 * one byte message type.
753 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
755 /* 2: withdrawn routes length */
758 /* 3: total attributes length - attrlen_pos stores the
760 attrlen_pos
= stream_get_endp(s
);
763 /* 4: if there is MP_REACH_NLRI attribute, that should
765 * attribute, according to
766 * draft-ietf-idr-error-handling. Save the
769 mpattr_pos
= stream_get_endp(s
);
771 /* 5: Encode all the attributes, except MP_REACH_NLRI
773 total_attr_len
= bgp_packet_attribute(
774 NULL
, peer
, s
, adv
->baa
->attr
, &vecarr
, NULL
,
775 afi
, safi
, from
, NULL
, NULL
, 0, 0);
778 STREAM_CONCAT_REMAIN(s
, snlri
, STREAM_SIZE(s
))
779 - BGP_MAX_PACKET_SIZE_OVERFLOW
;
780 space_needed
= BGP_NLRI_LENGTH
+ addpath_overhead
+
781 bgp_packet_mpattr_prefix_size(afi
, safi
,
784 /* If the attributes alone do not leave any room for
787 if (space_remaining
< space_needed
) {
789 "u%" PRIu64
":s%" PRIu64
790 " attributes too long, cannot send UPDATE",
791 subgrp
->update_group
->id
, subgrp
->id
);
793 /* Flush the FIFO update queue */
795 adv
= bgp_advertise_clean_subgroup(
800 if (BGP_DEBUG(update
, UPDATE_OUT
)
801 || BGP_DEBUG(update
, UPDATE_PREFIX
)) {
802 memset(send_attr_str
, 0, BUFSIZ
);
803 send_attr_printed
= 0;
804 bgp_dump_attr(adv
->baa
->attr
, send_attr_str
,
809 if ((afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
810 && !peer_cap_enhe(peer
, afi
, safi
))
811 stream_put_prefix_addpath(s
, &rn
->p
, addpath_encode
,
814 /* Encode the prefix in MP_REACH_NLRI attribute */
816 prd
= (struct prefix_rd
*)&rn
->prn
->p
;
818 if (safi
== SAFI_LABELED_UNICAST
)
819 label
= bgp_adv_label(rn
, binfo
, peer
, afi
,
821 else if (binfo
&& binfo
->extra
)
822 label
= binfo
->extra
->label
;
824 if (stream_empty(snlri
))
825 mpattrlen_pos
= bgp_packet_mpattr_start(
826 snlri
, peer
, afi
, safi
, &vecarr
,
829 bgp_packet_mpattr_prefix(snlri
, afi
, safi
, &rn
->p
, prd
,
830 &label
, addpath_encode
,
831 addpath_tx_id
, adv
->baa
->attr
);
836 if (bgp_debug_update(NULL
, &rn
->p
, subgrp
->update_group
, 0)) {
837 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
839 if (!send_attr_printed
) {
840 zlog_debug("u%" PRIu64
":s%" PRIu64
841 " send UPDATE w/ attr: %s",
842 subgrp
->update_group
->id
, subgrp
->id
,
844 if (!stream_empty(snlri
)) {
846 iana_safi_t pkt_safi
;
848 pkt_afi
= afi_int2iana(afi
);
849 pkt_safi
= safi_int2iana(safi
);
851 "u%" PRIu64
":s%" PRIu64
852 " send MP_REACH for afi/safi %d/%d",
853 subgrp
->update_group
->id
,
854 subgrp
->id
, pkt_afi
, pkt_safi
);
857 send_attr_printed
= 1;
860 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, &rn
->p
, &label
,
861 addpath_encode
, addpath_tx_id
,
862 pfx_buf
, sizeof(pfx_buf
));
863 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE %s",
864 subgrp
->update_group
->id
, subgrp
->id
,
868 /* Synchnorize attribute. */
870 bgp_attr_unintern(&adj
->attr
);
874 adj
->attr
= bgp_attr_intern(adv
->baa
->attr
);
876 adv
= bgp_advertise_clean_subgroup(subgrp
, adj
);
879 if (!stream_empty(s
)) {
880 if (!stream_empty(snlri
)) {
881 bgp_packet_mpattr_end(snlri
, mpattrlen_pos
);
882 total_attr_len
+= stream_get_endp(snlri
);
885 /* set the total attribute length correctly */
886 stream_putw_at(s
, attrlen_pos
, total_attr_len
);
888 if (!stream_empty(snlri
)) {
889 packet
= stream_dupcat(s
, snlri
, mpattr_pos
);
890 bpacket_attr_vec_arr_update(&vecarr
, mpattr_pos
);
892 packet
= stream_dup(s
);
893 bgp_packet_set_size(packet
);
894 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
895 zlog_debug("u%" PRIu64
":s%" PRIu64
896 " send UPDATE len %zd numpfx %d",
897 subgrp
->update_group
->id
, subgrp
->id
,
898 (stream_get_endp(packet
)
899 - stream_get_getp(packet
)),
901 pkt
= bpacket_queue_add(SUBGRP_PKTQ(subgrp
), packet
, &vecarr
);
909 /* Make BGP withdraw packet. */
911 16-octet marker | 2-octet length | 1-octet type |
912 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0)
914 /* For other afi/safis:
915 16-octet marker | 2-octet length | 1-octet type |
916 2-octet withdrawn route length (=0) | 2-octet attrlen |
917 mp_unreach attr type | attr len | afi | safi | withdrawn prefixes
919 struct bpacket
*subgroup_withdraw_packet(struct update_subgroup
*subgrp
)
923 struct bgp_adj_out
*adj
;
924 struct bgp_advertise
*adv
;
927 bgp_size_t unfeasible_len
;
928 bgp_size_t total_attr_len
;
930 size_t attrlen_pos
= 0;
931 size_t mplen_pos
= 0;
932 u_char first_time
= 1;
935 int space_remaining
= 0;
936 int space_needed
= 0;
938 int addpath_encode
= 0;
939 int addpath_overhead
= 0;
940 u_int32_t addpath_tx_id
= 0;
941 struct prefix_rd
*prd
= NULL
;
947 if (bpacket_queue_is_full(SUBGRP_INST(subgrp
), SUBGRP_PKTQ(subgrp
)))
950 peer
= SUBGRP_PEER(subgrp
);
951 afi
= SUBGRP_AFI(subgrp
);
952 safi
= SUBGRP_SAFI(subgrp
);
955 addpath_encode
= bgp_addpath_encode_tx(peer
, afi
, safi
);
956 addpath_overhead
= addpath_encode
? BGP_ADDPATH_ID_LEN
: 0;
958 while ((adv
= BGP_ADV_FIFO_HEAD(&subgrp
->sync
->withdraw
)) != NULL
) {
962 addpath_tx_id
= adj
->addpath_tx_id
;
965 STREAM_WRITEABLE(s
) - BGP_MAX_PACKET_SIZE_OVERFLOW
;
967 BGP_NLRI_LENGTH
+ addpath_overhead
+ BGP_TOTAL_ATTR_LEN
968 + bgp_packet_mpattr_prefix_size(afi
, safi
, &rn
->p
);
970 if (space_remaining
< space_needed
)
973 if (stream_empty(s
)) {
974 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
975 stream_putw(s
, 0); /* unfeasible routes length */
979 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
980 && !peer_cap_enhe(peer
, afi
, safi
))
981 stream_put_prefix_addpath(s
, &rn
->p
, addpath_encode
,
985 prd
= (struct prefix_rd
*)&rn
->prn
->p
;
987 /* If first time, format the MP_UNREACH header */
990 iana_safi_t pkt_safi
;
992 pkt_afi
= afi_int2iana(afi
);
993 pkt_safi
= safi_int2iana(safi
);
995 attrlen_pos
= stream_get_endp(s
);
996 /* total attr length = 0 for now. reevaluate
999 mp_start
= stream_get_endp(s
);
1000 mplen_pos
= bgp_packet_mpunreach_start(s
, afi
,
1002 if (bgp_debug_update(NULL
, NULL
,
1003 subgrp
->update_group
, 0))
1005 "u%" PRIu64
":s%" PRIu64
1006 " send MP_UNREACH for afi/safi %d/%d",
1007 subgrp
->update_group
->id
,
1008 subgrp
->id
, pkt_afi
, pkt_safi
);
1011 bgp_packet_mpunreach_prefix(s
, &rn
->p
, afi
, safi
, prd
,
1012 NULL
, addpath_encode
,
1013 addpath_tx_id
, NULL
);
1018 if (bgp_debug_update(NULL
, &rn
->p
, subgrp
->update_group
, 0)) {
1019 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
1021 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, &rn
->p
, NULL
,
1022 addpath_encode
, addpath_tx_id
,
1023 pfx_buf
, sizeof(pfx_buf
));
1024 zlog_debug("u%" PRIu64
":s%" PRIu64
1025 " send UPDATE %s -- unreachable",
1026 subgrp
->update_group
->id
, subgrp
->id
,
1032 bgp_adj_out_remove_subgroup(rn
, adj
, subgrp
);
1033 bgp_unlock_node(rn
);
1036 if (!stream_empty(s
)) {
1037 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
1038 && !peer_cap_enhe(peer
, afi
, safi
)) {
1039 unfeasible_len
= stream_get_endp(s
) - BGP_HEADER_SIZE
1040 - BGP_UNFEASIBLE_LEN
;
1041 stream_putw_at(s
, BGP_HEADER_SIZE
, unfeasible_len
);
1044 /* Set the mp_unreach attr's length */
1045 bgp_packet_mpunreach_end(s
, mplen_pos
);
1047 /* Set total path attribute length. */
1048 total_attr_len
= stream_get_endp(s
) - mp_start
;
1049 stream_putw_at(s
, attrlen_pos
, total_attr_len
);
1051 bgp_packet_set_size(s
);
1052 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
1053 zlog_debug("u%" PRIu64
":s%" PRIu64
1054 " send UPDATE (withdraw) len %zd numpfx %d",
1055 subgrp
->update_group
->id
, subgrp
->id
,
1056 (stream_get_endp(s
) - stream_get_getp(s
)),
1058 pkt
= bpacket_queue_add(SUBGRP_PKTQ(subgrp
), stream_dup(s
),
1067 void subgroup_default_update_packet(struct update_subgroup
*subgrp
,
1068 struct attr
*attr
, struct peer
*from
)
1074 bgp_size_t total_attr_len
;
1077 struct bpacket_attr_vec_arr vecarr
;
1078 int addpath_encode
= 0;
1080 if (DISABLE_BGP_ANNOUNCE
)
1086 peer
= SUBGRP_PEER(subgrp
);
1087 afi
= SUBGRP_AFI(subgrp
);
1088 safi
= SUBGRP_SAFI(subgrp
);
1089 bpacket_attr_vec_arr_reset(&vecarr
);
1090 addpath_encode
= bgp_addpath_encode_tx(peer
, afi
, safi
);
1092 memset(&p
, 0, sizeof(p
));
1093 p
.family
= afi2family(afi
);
1096 /* Logging the attribute. */
1097 if (bgp_debug_update(NULL
, &p
, subgrp
->update_group
, 0)) {
1098 char attrstr
[BUFSIZ
];
1099 char buf
[PREFIX_STRLEN
];
1100 /* ' with addpath ID ' 17
1101 * max strlen of uint32 + 10
1102 * +/- (just in case) + 1
1103 * null terminator + 1
1104 * ============================ 29 */
1109 bgp_dump_attr(attr
, attrstr
, BUFSIZ
);
1112 snprintf(tx_id_buf
, sizeof(tx_id_buf
),
1113 " with addpath ID %u",
1114 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1116 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE %s%s %s",
1117 (SUBGRP_UPDGRP(subgrp
))->id
, subgrp
->id
,
1118 prefix2str(&p
, buf
, sizeof(buf
)), tx_id_buf
,
1122 s
= stream_new(BGP_MAX_PACKET_SIZE
);
1124 /* Make BGP update packet. */
1125 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
1127 /* Unfeasible Routes Length. */
1130 /* Make place for total attribute length. */
1131 pos
= stream_get_endp(s
);
1133 total_attr_len
= bgp_packet_attribute(
1134 NULL
, peer
, s
, attr
, &vecarr
, &p
, afi
, safi
, from
, NULL
, NULL
,
1135 addpath_encode
, BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1137 /* Set Total Path Attribute Length. */
1138 stream_putw_at(s
, pos
, total_attr_len
);
1141 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
1142 && !peer_cap_enhe(peer
, afi
, safi
))
1143 stream_put_prefix_addpath(
1144 s
, &p
, addpath_encode
,
1145 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1148 bgp_packet_set_size(s
);
1150 (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp
), s
, &vecarr
);
1151 subgroup_trigger_write(subgrp
);
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 int addpath_encode
= 0;
1169 if (DISABLE_BGP_ANNOUNCE
)
1172 peer
= SUBGRP_PEER(subgrp
);
1173 afi
= SUBGRP_AFI(subgrp
);
1174 safi
= SUBGRP_SAFI(subgrp
);
1175 addpath_encode
= 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 char buf
[PREFIX_STRLEN
];
1183 /* ' with addpath ID ' 17
1184 * max strlen of uint32 + 10
1185 * +/- (just in case) + 1
1186 * null terminator + 1
1187 * ============================ 29 */
1191 snprintf(tx_id_buf
, sizeof(tx_id_buf
),
1192 " with addpath ID %u",
1193 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1195 zlog_debug("u%" PRIu64
":s%" PRIu64
1196 " send UPDATE %s%s -- unreachable",
1197 (SUBGRP_UPDGRP(subgrp
))->id
, subgrp
->id
,
1198 prefix2str(&p
, buf
, sizeof(buf
)), tx_id_buf
);
1201 s
= stream_new(BGP_MAX_PACKET_SIZE
);
1203 /* Make BGP update packet. */
1204 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
1206 /* Unfeasible Routes Length. */;
1207 cp
= stream_get_endp(s
);
1210 /* Withdrawn Routes. */
1211 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
1212 && !peer_cap_enhe(peer
, afi
, safi
)) {
1213 stream_put_prefix_addpath(
1214 s
, &p
, addpath_encode
,
1215 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1217 unfeasible_len
= stream_get_endp(s
) - cp
- 2;
1219 /* Set unfeasible len. */
1220 stream_putw_at(s
, cp
, unfeasible_len
);
1222 /* Set total path attribute length. */
1225 attrlen_pos
= stream_get_endp(s
);
1227 mp_start
= stream_get_endp(s
);
1228 mplen_pos
= bgp_packet_mpunreach_start(s
, afi
, safi
);
1229 bgp_packet_mpunreach_prefix(
1230 s
, &p
, afi
, safi
, NULL
, NULL
, addpath_encode
,
1231 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
, NULL
);
1233 /* Set the mp_unreach attr's length */
1234 bgp_packet_mpunreach_end(s
, mplen_pos
);
1236 /* Set total path attribute length. */
1237 total_attr_len
= stream_get_endp(s
) - mp_start
;
1238 stream_putw_at(s
, attrlen_pos
, total_attr_len
);
1241 bgp_packet_set_size(s
);
1243 (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp
), s
, NULL
);
1244 subgroup_trigger_write(subgrp
);
1248 bpacket_vec_arr_inherit_attr_flags(struct bpacket_attr_vec_arr
*vecarr
,
1249 bpacket_attr_vec_type type
,
1252 if (CHECK_FLAG(attr
->rmap_change_flags
,
1253 BATTR_RMAP_NEXTHOP_PEER_ADDRESS
))
1254 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1255 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
);
1257 if (CHECK_FLAG(attr
->rmap_change_flags
, BATTR_REFLECTED
))
1258 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1259 BPKT_ATTRVEC_FLAGS_REFLECTED
);
1261 if (CHECK_FLAG(attr
->rmap_change_flags
, BATTR_RMAP_NEXTHOP_UNCHANGED
))
1262 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1263 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
);
1265 if (CHECK_FLAG(attr
->rmap_change_flags
, BATTR_RMAP_IPV4_NHOP_CHANGED
))
1266 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1267 BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED
);
1269 if (CHECK_FLAG(attr
->rmap_change_flags
,
1270 BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED
))
1271 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1272 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED
);
1274 if (CHECK_FLAG(attr
->rmap_change_flags
,
1275 BATTR_RMAP_IPV6_LL_NHOP_CHANGED
))
1276 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1277 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_LNH_CHANGED
);
1280 /* Reset the Attributes vector array. The vector array is used to override
1281 * certain output parameters in the packet for a particular peer
1283 void bpacket_attr_vec_arr_reset(struct bpacket_attr_vec_arr
*vecarr
)
1291 while (i
< BGP_ATTR_VEC_MAX
) {
1292 vecarr
->entries
[i
].flags
= 0;
1293 vecarr
->entries
[i
].offset
= 0;
1298 /* Setup a particular node entry in the vecarr */
1299 void bpacket_attr_vec_arr_set_vec(struct bpacket_attr_vec_arr
*vecarr
,
1300 bpacket_attr_vec_type type
, struct stream
*s
,
1305 assert(type
< BGP_ATTR_VEC_MAX
);
1307 SET_FLAG(vecarr
->entries
[type
].flags
, BPKT_ATTRVEC_FLAGS_UPDATED
);
1308 vecarr
->entries
[type
].offset
= stream_get_endp(s
);
1310 bpacket_vec_arr_inherit_attr_flags(vecarr
, type
, attr
);