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_sanity_check
93 void bpacket_queue_sanity_check(struct bpacket_queue
__attribute__((__unused__
))
99 pkt
= bpacket_queue_last (q
);
101 assert (!pkt
->buffer
);
104 * Make sure the count of packets is correct.
108 pkt
= bpacket_queue_first (q
);
113 if (num_pkts
> q
->curr_count
)
116 pkt
= TAILQ_NEXT (pkt
, pkt_train
);
119 assert (num_pkts
== q
->curr_count
);
124 * bpacket_queue_add_packet
126 * Internal function of bpacket_queue - and adds a
127 * packet entry to the end of the list.
129 * Users of bpacket_queue should use bpacket_queue_add instead.
131 static void bpacket_queue_add_packet(struct bpacket_queue
*q
,
134 struct bpacket
*last_pkt
;
136 if (TAILQ_EMPTY(&(q
->pkts
)))
137 TAILQ_INSERT_TAIL(&(q
->pkts
), pkt
, pkt_train
);
139 last_pkt
= bpacket_queue_last(q
);
140 TAILQ_INSERT_AFTER(&(q
->pkts
), last_pkt
, pkt
, pkt_train
);
143 if (q
->hwm_count
< q
->curr_count
)
144 q
->hwm_count
= q
->curr_count
;
148 * Adds a packet to the bpacket_queue.
150 * The stream passed is consumed by this function. So, the caller should
151 * not free or use the stream after
152 * invoking this function.
154 struct bpacket
*bpacket_queue_add(struct bpacket_queue
*q
, struct stream
*s
,
155 struct bpacket_attr_vec_arr
*vecarrp
)
158 struct bpacket
*last_pkt
;
161 pkt
= bpacket_alloc();
162 if (TAILQ_EMPTY(&(q
->pkts
))) {
166 memcpy(&pkt
->arr
, vecarrp
,
167 sizeof(struct bpacket_attr_vec_arr
));
169 bpacket_attr_vec_arr_reset(&pkt
->arr
);
170 bpacket_queue_add_packet(q
, pkt
);
171 bpacket_queue_sanity_check(q
);
176 * Fill in the new information into the current sentinel and create a
179 bpacket_queue_sanity_check(q
);
180 last_pkt
= bpacket_queue_last(q
);
181 assert(last_pkt
->buffer
== NULL
);
182 last_pkt
->buffer
= s
;
184 memcpy(&last_pkt
->arr
, vecarrp
,
185 sizeof(struct bpacket_attr_vec_arr
));
187 bpacket_attr_vec_arr_reset(&last_pkt
->arr
);
189 pkt
->ver
= last_pkt
->ver
;
191 bpacket_queue_add_packet(q
, pkt
);
193 bpacket_queue_sanity_check(q
);
197 struct bpacket
*bpacket_queue_first(struct bpacket_queue
*q
)
199 return (TAILQ_FIRST(&(q
->pkts
)));
202 struct bpacket
*bpacket_queue_last(struct bpacket_queue
*q
)
204 return TAILQ_LAST(&(q
->pkts
), pkt_queue
);
207 struct bpacket
*bpacket_queue_remove(struct bpacket_queue
*q
)
209 struct bpacket
*first
;
211 first
= bpacket_queue_first(q
);
213 TAILQ_REMOVE(&(q
->pkts
), first
, pkt_train
);
219 unsigned int bpacket_queue_length(struct bpacket_queue
*q
)
221 return q
->curr_count
- 1;
224 unsigned int bpacket_queue_hwm_length(struct bpacket_queue
*q
)
226 return q
->hwm_count
- 1;
229 int bpacket_queue_is_full(struct bgp
*bgp
, struct bpacket_queue
*q
)
231 if (q
->curr_count
>= bgp
->default_subgroup_pkt_queue_max
)
236 void bpacket_add_peer(struct bpacket
*pkt
, struct peer_af
*paf
)
241 LIST_INSERT_HEAD(&(pkt
->peers
), paf
, pkt_train
);
242 paf
->next_pkt_to_send
= pkt
;
246 * bpacket_queue_cleanup
248 void bpacket_queue_cleanup(struct bpacket_queue
*q
)
252 while ((pkt
= bpacket_queue_remove(q
))) {
258 * bpacket_queue_compact
260 * Delete packets that do not need to be transmitted to any peer from
263 * @return the number of packets deleted.
265 static int bpacket_queue_compact(struct bpacket_queue
*q
)
268 struct bpacket
*pkt
, *removed_pkt
;
273 pkt
= bpacket_queue_first(q
);
278 * Don't delete the sentinel.
283 if (!LIST_EMPTY(&(pkt
->peers
)))
286 removed_pkt
= bpacket_queue_remove(q
);
287 assert(pkt
== removed_pkt
);
288 bpacket_free(removed_pkt
);
293 bpacket_queue_sanity_check(q
);
297 void bpacket_queue_advance_peer(struct peer_af
*paf
)
300 struct bpacket
*old_pkt
;
302 old_pkt
= paf
->next_pkt_to_send
;
303 if (old_pkt
->buffer
== NULL
)
304 /* Already at end of list */
307 LIST_REMOVE(paf
, pkt_train
);
308 pkt
= TAILQ_NEXT(old_pkt
, pkt_train
);
309 bpacket_add_peer(pkt
, paf
);
311 if (!bpacket_queue_compact(PAF_PKTQ(paf
)))
315 * Deleted one or more packets. Check if we can now merge this
316 * peer's subgroup into another subgroup.
318 update_subgroup_check_merge(paf
->subgroup
, "advanced peer in queue");
322 * bpacket_queue_remove_peer
324 * Remove the peer from the packet queue of the subgroup it belongs
327 void bpacket_queue_remove_peer(struct peer_af
*paf
)
329 struct bpacket_queue
*q
;
336 LIST_REMOVE(paf
, pkt_train
);
337 paf
->next_pkt_to_send
= NULL
;
339 bpacket_queue_compact(q
);
342 unsigned int bpacket_queue_virtual_length(struct peer_af
*paf
)
345 struct bpacket
*last
;
346 struct bpacket_queue
*q
;
348 pkt
= paf
->next_pkt_to_send
;
349 if (!pkt
|| (pkt
->buffer
== NULL
))
350 /* Already at end of list */
354 if (TAILQ_EMPTY(&(q
->pkts
)))
357 last
= TAILQ_LAST(&(q
->pkts
), pkt_queue
);
358 if (last
->ver
>= pkt
->ver
)
359 return last
->ver
- pkt
->ver
;
361 /* sequence # rolled over */
362 return (UINT_MAX
- pkt
->ver
+ 1) + last
->ver
;
366 * Dump the bpacket queue
368 void bpacket_queue_show_vty(struct bpacket_queue
*q
, struct vty
*vty
)
373 pkt
= bpacket_queue_first(q
);
375 vty_out(vty
, " Packet %p ver %u buffer %p\n", pkt
, pkt
->ver
,
378 LIST_FOREACH (paf
, &(pkt
->peers
), pkt_train
) {
379 vty_out(vty
, " - %s\n", paf
->peer
->host
);
381 pkt
= bpacket_next(pkt
);
386 struct stream
*bpacket_reformat_for_peer(struct bpacket
*pkt
,
389 struct stream
*s
= NULL
;
390 bpacket_attr_vec
*vec
;
394 struct bgp_filter
*filter
;
396 s
= stream_dup(pkt
->buffer
);
397 peer
= PAF_PEER(paf
);
399 vec
= &pkt
->arr
.entries
[BGP_ATTR_VEC_NH
];
400 if (CHECK_FLAG(vec
->flags
, BPKT_ATTRVEC_FLAGS_UPDATED
)) {
403 int route_map_sets_nh
;
404 nhlen
= stream_getc_from(s
, vec
->offset
);
405 filter
= &peer
->filter
[paf
->afi
][paf
->safi
];
407 if (peer_cap_enhe(peer
, paf
->afi
, paf
->safi
))
410 nhafi
= BGP_NEXTHOP_AFI_FROM_NHLEN(nhlen
);
412 if (nhafi
== AFI_IP
) {
413 struct in_addr v4nh
, *mod_v4nh
;
415 size_t offset_nh
= vec
->offset
+ 1;
420 BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED
)
423 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
));
426 case BGP_ATTR_NHLEN_IPV4
:
428 case BGP_ATTR_NHLEN_VPNV4
:
432 /* TODO: handle IPv6 nexthops */
434 EC_BGP_INVALID_NEXTHOP_LENGTH
,
435 "%s: %s: invalid MP nexthop length (AFI IP): %u",
436 __func__
, peer
->host
, nhlen
);
441 stream_get_from(&v4nh
, s
, offset_nh
, IPV4_MAX_BYTELEN
);
445 * If route-map has set the nexthop, that is normally
446 * used; if it is specified as peer-address, the peering
447 * address is picked up. Otherwise, if NH is unavailable
448 * from attribute, the peering addr is picked up; the
449 * "NH unavailable" case also covers next-hop-self and
450 * some other scenarios - see subgroup_announce_check().
451 * In all other cases, use the nexthop carried in the
452 * attribute unless it is EBGP non-multiaccess and there
453 * is no next-hop-unchanged setting or the peer is EBGP
454 * and the route-map that changed the next-hop value
455 * was applied inbound rather than outbound. Updates to
456 * an EBGP peer should only modify the next-hop if it
457 * was set in an outbound route-map to that peer.
458 * Note: It is assumed route-map cannot set the nexthop
459 * to an invalid value.
461 if (route_map_sets_nh
462 && ((peer
->sort
!= BGP_PEER_EBGP
)
463 || ROUTE_MAP_OUT(filter
))) {
466 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
)) {
467 mod_v4nh
= &peer
->nexthop
.v4
;
470 } else if (!v4nh
.s_addr
) {
471 mod_v4nh
= &peer
->nexthop
.v4
;
474 peer
->sort
== BGP_PEER_EBGP
475 && (bgp_multiaccess_check_v4(v4nh
, peer
) == 0)
478 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
)
479 && !peer_af_flag_check(
480 peer
, paf
->afi
, paf
->safi
,
481 PEER_FLAG_NEXTHOP_UNCHANGED
)) {
482 /* NOTE: not handling case where NH has new AFI
484 mod_v4nh
= &peer
->nexthop
.v4
;
488 if (nh_modified
) /* allow for VPN RD */
489 stream_put_in_addr_at(s
, offset_nh
, mod_v4nh
);
491 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
492 zlog_debug("u%" PRIu64
":s%" PRIu64
493 " %s send UPDATE w/ nexthop %s%s",
494 PAF_SUBGRP(paf
)->update_group
->id
,
495 PAF_SUBGRP(paf
)->id
, peer
->host
,
496 inet_ntoa(*mod_v4nh
),
497 (nhlen
== 12 ? " and RD" : ""));
498 } else if (nhafi
== AFI_IP6
) {
499 struct in6_addr v6nhglobal
, *mod_v6nhg
;
500 struct in6_addr v6nhlocal
, *mod_v6nhl
;
501 int gnh_modified
, lnh_modified
;
502 size_t offset_nhglobal
= vec
->offset
+ 1;
503 size_t offset_nhlocal
= vec
->offset
+ 1;
505 gnh_modified
= lnh_modified
= 0;
506 mod_v6nhg
= &v6nhglobal
;
507 mod_v6nhl
= &v6nhlocal
;
512 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED
)
515 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
));
518 * The logic here is rather similar to that for IPv4,
520 * additional work being to handle 1 or 2 nexthops.
522 * party nexthop is not propagated for EBGP right now.
525 case BGP_ATTR_NHLEN_IPV6_GLOBAL
:
527 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
:
528 offset_nhlocal
+= IPV6_MAX_BYTELEN
;
530 case BGP_ATTR_NHLEN_VPNV6_GLOBAL
:
531 offset_nhglobal
+= 8;
533 case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
:
534 offset_nhglobal
+= 8;
535 offset_nhlocal
+= 8 * 2 + IPV6_MAX_BYTELEN
;
538 /* TODO: handle IPv4 nexthops */
540 EC_BGP_INVALID_NEXTHOP_LENGTH
,
541 "%s: %s: invalid MP nexthop length (AFI IP6): %u",
542 __func__
, peer
->host
, nhlen
);
547 stream_get_from(&v6nhglobal
, s
, offset_nhglobal
,
551 * Updates to an EBGP peer should only modify the
552 * next-hop if it was set in an outbound route-map
555 if (route_map_sets_nh
556 && ((peer
->sort
!= BGP_PEER_EBGP
)
557 || ROUTE_MAP_OUT(filter
))) {
560 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
)) {
561 mod_v6nhg
= &peer
->nexthop
.v6_global
;
564 } else if (IN6_IS_ADDR_UNSPECIFIED(&v6nhglobal
)) {
565 mod_v6nhg
= &peer
->nexthop
.v6_global
;
568 (peer
->sort
== BGP_PEER_EBGP
)
569 && (!bgp_multiaccess_check_v6(v6nhglobal
, peer
))
570 && !CHECK_FLAG(vec
->flags
,
571 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
)
572 && !peer_af_flag_check(
573 peer
, nhafi
, paf
->safi
,
574 PEER_FLAG_NEXTHOP_UNCHANGED
)) {
575 /* NOTE: not handling case where NH has new AFI
577 mod_v6nhg
= &peer
->nexthop
.v6_global
;
582 if (nhlen
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
583 || nhlen
== BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
584 stream_get_from(&v6nhlocal
, s
, offset_nhlocal
,
586 if (IN6_IS_ADDR_UNSPECIFIED(&v6nhlocal
)) {
587 mod_v6nhl
= &peer
->nexthop
.v6_local
;
593 stream_put_in6_addr_at(s
, offset_nhglobal
,
596 stream_put_in6_addr_at(s
, offset_nhlocal
,
599 if (bgp_debug_update(peer
, NULL
, NULL
, 0)) {
600 if (nhlen
== 32 || nhlen
== 48)
602 "u%" PRIu64
":s%" PRIu64
603 " %s send UPDATE w/ mp_nexthops %s, %s%s",
606 PAF_SUBGRP(paf
)->id
, peer
->host
,
607 inet_ntop(AF_INET6
, mod_v6nhg
,
609 inet_ntop(AF_INET6
, mod_v6nhl
,
611 (nhlen
== 48 ? " and RD" : ""));
614 "u%" PRIu64
":s%" PRIu64
615 " %s send UPDATE w/ mp_nexthop %s%s",
618 PAF_SUBGRP(paf
)->id
, peer
->host
,
619 inet_ntop(AF_INET6
, mod_v6nhg
,
621 (nhlen
== 24 ? " and RD" : ""));
623 } else if (paf
->afi
== AFI_L2VPN
) {
624 struct in_addr v4nh
, *mod_v4nh
;
627 stream_get_from(&v4nh
, s
, vec
->offset
+ 1, 4);
630 /* No route-map changes allowed for EVPN nexthops. */
632 mod_v4nh
= &peer
->nexthop
.v4
;
637 stream_put_in_addr_at(s
, vec
->offset
+ 1,
640 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
641 zlog_debug("u%" PRIu64
":s%" PRIu64
642 " %s send UPDATE w/ nexthop %s",
643 PAF_SUBGRP(paf
)->update_group
->id
,
644 PAF_SUBGRP(paf
)->id
, peer
->host
,
645 inet_ntoa(*mod_v4nh
));
653 * Update the vecarr offsets to go beyond 'pos' bytes, i.e. add 'pos'
656 static void bpacket_attr_vec_arr_update(struct bpacket_attr_vec_arr
*vecarr
,
664 for (i
= 0; i
< BGP_ATTR_VEC_MAX
; i
++)
665 vecarr
->entries
[i
].offset
+= pos
;
669 * Return if there are packets to build for this subgroup.
671 int subgroup_packets_to_build(struct update_subgroup
*subgrp
)
673 struct bgp_advertise
*adv
;
678 adv
= bgp_adv_fifo_first(&subgrp
->sync
->withdraw
);
682 adv
= bgp_adv_fifo_first(&subgrp
->sync
->update
);
689 /* Make BGP update packet. */
690 struct bpacket
*subgroup_update_packet(struct update_subgroup
*subgrp
)
692 struct bpacket_attr_vec_arr vecarr
;
696 struct stream
*snlri
;
697 struct stream
*packet
;
698 struct bgp_adj_out
*adj
;
699 struct bgp_advertise
*adv
;
700 struct bgp_node
*rn
= NULL
;
701 struct bgp_path_info
*path
= NULL
;
702 bgp_size_t total_attr_len
= 0;
703 unsigned long attrlen_pos
= 0;
704 size_t mpattrlen_pos
= 0;
705 size_t mpattr_pos
= 0;
708 int space_remaining
= 0;
709 int space_needed
= 0;
710 char send_attr_str
[BUFSIZ
];
711 int send_attr_printed
= 0;
713 int addpath_encode
= 0;
714 int addpath_overhead
= 0;
715 uint32_t addpath_tx_id
= 0;
716 struct prefix_rd
*prd
= NULL
;
717 mpls_label_t label
= MPLS_INVALID_LABEL
, *label_pnt
= NULL
;
718 uint32_t num_labels
= 0;
723 if (bpacket_queue_is_full(SUBGRP_INST(subgrp
), SUBGRP_PKTQ(subgrp
)))
726 peer
= SUBGRP_PEER(subgrp
);
727 afi
= SUBGRP_AFI(subgrp
);
728 safi
= SUBGRP_SAFI(subgrp
);
731 snlri
= subgrp
->scratch
;
734 bpacket_attr_vec_arr_reset(&vecarr
);
736 addpath_encode
= bgp_addpath_encode_tx(peer
, afi
, safi
);
737 addpath_overhead
= addpath_encode
? BGP_ADDPATH_ID_LEN
: 0;
739 adv
= bgp_adv_fifo_first(&subgrp
->sync
->update
);
744 addpath_tx_id
= adj
->addpath_tx_id
;
747 space_remaining
= STREAM_CONCAT_REMAIN(s
, snlri
, STREAM_SIZE(s
))
748 - BGP_MAX_PACKET_SIZE_OVERFLOW
;
750 BGP_NLRI_LENGTH
+ addpath_overhead
751 + bgp_packet_mpattr_prefix_size(afi
, safi
, &rn
->p
);
753 /* When remaining space can't include NLRI and it's length. */
754 if (space_remaining
< space_needed
)
757 /* If packet is empty, set attribute. */
758 if (stream_empty(s
)) {
759 struct peer
*from
= NULL
;
764 /* 1: Write the BGP message header - 16 bytes marker, 2
766 * one byte message type.
768 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
770 /* 2: withdrawn routes length */
773 /* 3: total attributes length - attrlen_pos stores the
775 attrlen_pos
= stream_get_endp(s
);
778 /* 4: if there is MP_REACH_NLRI attribute, that should
780 * attribute, according to
781 * draft-ietf-idr-error-handling. Save the
784 mpattr_pos
= stream_get_endp(s
);
786 /* 5: Encode all the attributes, except MP_REACH_NLRI
788 total_attr_len
= bgp_packet_attribute(
789 NULL
, peer
, s
, adv
->baa
->attr
, &vecarr
, NULL
,
790 afi
, safi
, from
, NULL
, NULL
, 0, 0, 0);
793 STREAM_CONCAT_REMAIN(s
, snlri
, STREAM_SIZE(s
))
794 - BGP_MAX_PACKET_SIZE_OVERFLOW
;
795 space_needed
= BGP_NLRI_LENGTH
+ addpath_overhead
796 + bgp_packet_mpattr_prefix_size(
799 /* If the attributes alone do not leave any room for
802 if (space_remaining
< space_needed
) {
804 EC_BGP_UPDGRP_ATTR_LEN
,
805 "u%" PRIu64
":s%" PRIu64
806 " attributes too long, cannot send UPDATE",
807 subgrp
->update_group
->id
, subgrp
->id
);
809 /* Flush the FIFO update queue */
811 adv
= bgp_advertise_clean_subgroup(
816 if (BGP_DEBUG(update
, UPDATE_OUT
)
817 || BGP_DEBUG(update
, UPDATE_PREFIX
)) {
818 memset(send_attr_str
, 0, BUFSIZ
);
819 send_attr_printed
= 0;
820 bgp_dump_attr(adv
->baa
->attr
, send_attr_str
,
825 if ((afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
826 && !peer_cap_enhe(peer
, afi
, safi
))
827 stream_put_prefix_addpath(s
, &rn
->p
, addpath_encode
,
830 /* Encode the prefix in MP_REACH_NLRI attribute */
832 prd
= (struct prefix_rd
*)&rn
->prn
->p
;
834 if (safi
== SAFI_LABELED_UNICAST
) {
835 label
= bgp_adv_label(rn
, path
, peer
, afi
,
839 } else if (path
&& path
->extra
) {
840 label_pnt
= &path
->extra
->label
[0];
841 num_labels
= path
->extra
->num_labels
;
844 if (stream_empty(snlri
))
845 mpattrlen_pos
= bgp_packet_mpattr_start(
846 snlri
, peer
, afi
, safi
, &vecarr
,
849 bgp_packet_mpattr_prefix(snlri
, afi
, safi
, &rn
->p
, prd
,
850 label_pnt
, num_labels
,
851 addpath_encode
, addpath_tx_id
,
857 if (bgp_debug_update(NULL
, &rn
->p
, subgrp
->update_group
, 0)) {
858 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
860 if (!send_attr_printed
) {
861 zlog_debug("u%" PRIu64
":s%" PRIu64
862 " send UPDATE w/ attr: %s",
863 subgrp
->update_group
->id
, subgrp
->id
,
865 if (!stream_empty(snlri
)) {
867 iana_safi_t pkt_safi
;
869 pkt_afi
= afi_int2iana(afi
);
870 pkt_safi
= safi_int2iana(safi
);
872 "u%" PRIu64
":s%" PRIu64
873 " send MP_REACH for afi/safi %d/%d",
874 subgrp
->update_group
->id
,
875 subgrp
->id
, pkt_afi
, pkt_safi
);
878 send_attr_printed
= 1;
881 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, &rn
->p
,
882 label_pnt
, num_labels
,
883 addpath_encode
, addpath_tx_id
,
884 pfx_buf
, sizeof(pfx_buf
));
885 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE %s",
886 subgrp
->update_group
->id
, subgrp
->id
,
890 /* Synchnorize attribute. */
892 bgp_attr_unintern(&adj
->attr
);
896 adj
->attr
= bgp_attr_intern(adv
->baa
->attr
);
898 adv
= bgp_advertise_clean_subgroup(subgrp
, adj
);
901 if (!stream_empty(s
)) {
902 if (!stream_empty(snlri
)) {
903 bgp_packet_mpattr_end(snlri
, mpattrlen_pos
);
904 total_attr_len
+= stream_get_endp(snlri
);
907 /* set the total attribute length correctly */
908 stream_putw_at(s
, attrlen_pos
, total_attr_len
);
910 if (!stream_empty(snlri
)) {
911 packet
= stream_dupcat(s
, snlri
, mpattr_pos
);
912 bpacket_attr_vec_arr_update(&vecarr
, mpattr_pos
);
914 packet
= stream_dup(s
);
915 bgp_packet_set_size(packet
);
916 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
917 zlog_debug("u%" PRIu64
":s%" PRIu64
918 " send UPDATE len %zd numpfx %d",
919 subgrp
->update_group
->id
, subgrp
->id
,
920 (stream_get_endp(packet
)
921 - stream_get_getp(packet
)),
923 pkt
= bpacket_queue_add(SUBGRP_PKTQ(subgrp
), packet
, &vecarr
);
931 /* Make BGP withdraw packet. */
933 16-octet marker | 2-octet length | 1-octet type |
934 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0)
936 /* For other afi/safis:
937 16-octet marker | 2-octet length | 1-octet type |
938 2-octet withdrawn route length (=0) | 2-octet attrlen |
939 mp_unreach attr type | attr len | afi | safi | withdrawn prefixes
941 struct bpacket
*subgroup_withdraw_packet(struct update_subgroup
*subgrp
)
945 struct bgp_adj_out
*adj
;
946 struct bgp_advertise
*adv
;
949 bgp_size_t unfeasible_len
;
950 bgp_size_t total_attr_len
;
952 size_t attrlen_pos
= 0;
953 size_t mplen_pos
= 0;
954 uint8_t first_time
= 1;
957 int space_remaining
= 0;
958 int space_needed
= 0;
960 int addpath_encode
= 0;
961 int addpath_overhead
= 0;
962 uint32_t addpath_tx_id
= 0;
963 struct prefix_rd
*prd
= NULL
;
969 if (bpacket_queue_is_full(SUBGRP_INST(subgrp
), SUBGRP_PKTQ(subgrp
)))
972 peer
= SUBGRP_PEER(subgrp
);
973 afi
= SUBGRP_AFI(subgrp
);
974 safi
= SUBGRP_SAFI(subgrp
);
977 addpath_encode
= bgp_addpath_encode_tx(peer
, afi
, safi
);
978 addpath_overhead
= addpath_encode
? BGP_ADDPATH_ID_LEN
: 0;
980 while ((adv
= bgp_adv_fifo_first(&subgrp
->sync
->withdraw
)) != NULL
) {
984 addpath_tx_id
= adj
->addpath_tx_id
;
987 STREAM_WRITEABLE(s
) - BGP_MAX_PACKET_SIZE_OVERFLOW
;
989 BGP_NLRI_LENGTH
+ addpath_overhead
+ BGP_TOTAL_ATTR_LEN
990 + bgp_packet_mpattr_prefix_size(afi
, safi
, &rn
->p
);
992 if (space_remaining
< space_needed
)
995 if (stream_empty(s
)) {
996 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
997 stream_putw(s
, 0); /* unfeasible routes length */
1001 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
1002 && !peer_cap_enhe(peer
, afi
, safi
))
1003 stream_put_prefix_addpath(s
, &rn
->p
, addpath_encode
,
1007 prd
= (struct prefix_rd
*)&rn
->prn
->p
;
1009 /* If first time, format the MP_UNREACH header */
1012 iana_safi_t pkt_safi
;
1014 pkt_afi
= afi_int2iana(afi
);
1015 pkt_safi
= safi_int2iana(safi
);
1017 attrlen_pos
= stream_get_endp(s
);
1018 /* total attr length = 0 for now. reevaluate
1021 mp_start
= stream_get_endp(s
);
1022 mplen_pos
= bgp_packet_mpunreach_start(s
, afi
,
1024 if (bgp_debug_update(NULL
, NULL
,
1025 subgrp
->update_group
, 0))
1027 "u%" PRIu64
":s%" PRIu64
1028 " send MP_UNREACH for afi/safi %d/%d",
1029 subgrp
->update_group
->id
,
1030 subgrp
->id
, pkt_afi
, pkt_safi
);
1033 bgp_packet_mpunreach_prefix(s
, &rn
->p
, afi
, safi
, prd
,
1034 NULL
, 0, addpath_encode
,
1035 addpath_tx_id
, NULL
);
1040 if (bgp_debug_update(NULL
, &rn
->p
, subgrp
->update_group
, 0)) {
1041 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
1043 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, &rn
->p
, NULL
, 0,
1044 addpath_encode
, addpath_tx_id
,
1045 pfx_buf
, sizeof(pfx_buf
));
1046 zlog_debug("u%" PRIu64
":s%" PRIu64
1047 " send UPDATE %s -- unreachable",
1048 subgrp
->update_group
->id
, subgrp
->id
,
1054 bgp_adj_out_remove_subgroup(rn
, adj
, subgrp
);
1055 bgp_unlock_node(rn
);
1058 if (!stream_empty(s
)) {
1059 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
1060 && !peer_cap_enhe(peer
, afi
, safi
)) {
1061 unfeasible_len
= stream_get_endp(s
) - BGP_HEADER_SIZE
1062 - BGP_UNFEASIBLE_LEN
;
1063 stream_putw_at(s
, BGP_HEADER_SIZE
, unfeasible_len
);
1066 /* Set the mp_unreach attr's length */
1067 bgp_packet_mpunreach_end(s
, mplen_pos
);
1069 /* Set total path attribute length. */
1070 total_attr_len
= stream_get_endp(s
) - mp_start
;
1071 stream_putw_at(s
, attrlen_pos
, total_attr_len
);
1073 bgp_packet_set_size(s
);
1074 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
1075 zlog_debug("u%" PRIu64
":s%" PRIu64
1076 " send UPDATE (withdraw) len %zd numpfx %d",
1077 subgrp
->update_group
->id
, subgrp
->id
,
1078 (stream_get_endp(s
) - stream_get_getp(s
)),
1080 pkt
= bpacket_queue_add(SUBGRP_PKTQ(subgrp
), stream_dup(s
),
1089 void subgroup_default_update_packet(struct update_subgroup
*subgrp
,
1090 struct attr
*attr
, struct peer
*from
)
1096 bgp_size_t total_attr_len
;
1099 struct bpacket_attr_vec_arr vecarr
;
1100 int addpath_encode
= 0;
1102 if (DISABLE_BGP_ANNOUNCE
)
1108 peer
= SUBGRP_PEER(subgrp
);
1109 afi
= SUBGRP_AFI(subgrp
);
1110 safi
= SUBGRP_SAFI(subgrp
);
1111 bpacket_attr_vec_arr_reset(&vecarr
);
1112 addpath_encode
= bgp_addpath_encode_tx(peer
, afi
, safi
);
1114 memset(&p
, 0, sizeof(p
));
1115 p
.family
= afi2family(afi
);
1118 /* Logging the attribute. */
1119 if (bgp_debug_update(NULL
, &p
, subgrp
->update_group
, 0)) {
1120 char attrstr
[BUFSIZ
];
1121 char buf
[PREFIX_STRLEN
];
1122 /* ' with addpath ID ' 17
1123 * max strlen of uint32 + 10
1124 * +/- (just in case) + 1
1125 * null terminator + 1
1126 * ============================ 29 */
1131 bgp_dump_attr(attr
, attrstr
, BUFSIZ
);
1134 snprintf(tx_id_buf
, sizeof(tx_id_buf
),
1135 " with addpath ID %u",
1136 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1138 tx_id_buf
[0] = '\0';
1140 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE %s%s %s",
1141 (SUBGRP_UPDGRP(subgrp
))->id
, subgrp
->id
,
1142 prefix2str(&p
, buf
, sizeof(buf
)), tx_id_buf
,
1146 s
= stream_new(BGP_MAX_PACKET_SIZE
);
1148 /* Make BGP update packet. */
1149 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
1151 /* Unfeasible Routes Length. */
1154 /* Make place for total attribute length. */
1155 pos
= stream_get_endp(s
);
1157 total_attr_len
= bgp_packet_attribute(
1158 NULL
, peer
, s
, attr
, &vecarr
, &p
, afi
, safi
, from
, NULL
, NULL
,
1159 0, addpath_encode
, BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1161 /* Set Total Path Attribute Length. */
1162 stream_putw_at(s
, pos
, total_attr_len
);
1165 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
1166 && !peer_cap_enhe(peer
, afi
, safi
))
1167 stream_put_prefix_addpath(
1168 s
, &p
, addpath_encode
,
1169 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1172 bgp_packet_set_size(s
);
1174 (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp
), s
, &vecarr
);
1175 subgroup_trigger_write(subgrp
);
1178 void subgroup_default_withdraw_packet(struct update_subgroup
*subgrp
)
1183 unsigned long attrlen_pos
= 0;
1185 bgp_size_t unfeasible_len
;
1186 bgp_size_t total_attr_len
= 0;
1187 size_t mp_start
= 0;
1188 size_t mplen_pos
= 0;
1191 int addpath_encode
= 0;
1193 if (DISABLE_BGP_ANNOUNCE
)
1196 peer
= SUBGRP_PEER(subgrp
);
1197 afi
= SUBGRP_AFI(subgrp
);
1198 safi
= SUBGRP_SAFI(subgrp
);
1199 addpath_encode
= bgp_addpath_encode_tx(peer
, afi
, safi
);
1201 memset(&p
, 0, sizeof(p
));
1202 p
.family
= afi2family(afi
);
1205 if (bgp_debug_update(NULL
, &p
, subgrp
->update_group
, 0)) {
1206 char buf
[PREFIX_STRLEN
];
1207 /* ' with addpath ID ' 17
1208 * max strlen of uint32 + 10
1209 * +/- (just in case) + 1
1210 * null terminator + 1
1211 * ============================ 29 */
1215 snprintf(tx_id_buf
, sizeof(tx_id_buf
),
1216 " with addpath ID %u",
1217 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1219 zlog_debug("u%" PRIu64
":s%" PRIu64
1220 " send UPDATE %s%s -- unreachable",
1221 (SUBGRP_UPDGRP(subgrp
))->id
, subgrp
->id
,
1222 prefix2str(&p
, buf
, sizeof(buf
)), tx_id_buf
);
1225 s
= stream_new(BGP_MAX_PACKET_SIZE
);
1227 /* Make BGP update packet. */
1228 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
1230 /* Unfeasible Routes Length. */;
1231 cp
= stream_get_endp(s
);
1234 /* Withdrawn Routes. */
1235 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
1236 && !peer_cap_enhe(peer
, afi
, safi
)) {
1237 stream_put_prefix_addpath(
1238 s
, &p
, addpath_encode
,
1239 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1241 unfeasible_len
= stream_get_endp(s
) - cp
- 2;
1243 /* Set unfeasible len. */
1244 stream_putw_at(s
, cp
, unfeasible_len
);
1246 /* Set total path attribute length. */
1249 attrlen_pos
= stream_get_endp(s
);
1251 mp_start
= stream_get_endp(s
);
1252 mplen_pos
= bgp_packet_mpunreach_start(s
, afi
, safi
);
1253 bgp_packet_mpunreach_prefix(
1254 s
, &p
, afi
, safi
, NULL
, NULL
, 0, addpath_encode
,
1255 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
, NULL
);
1257 /* Set the mp_unreach attr's length */
1258 bgp_packet_mpunreach_end(s
, mplen_pos
);
1260 /* Set total path attribute length. */
1261 total_attr_len
= stream_get_endp(s
) - mp_start
;
1262 stream_putw_at(s
, attrlen_pos
, total_attr_len
);
1265 bgp_packet_set_size(s
);
1267 (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp
), s
, NULL
);
1268 subgroup_trigger_write(subgrp
);
1272 bpacket_vec_arr_inherit_attr_flags(struct bpacket_attr_vec_arr
*vecarr
,
1273 bpacket_attr_vec_type type
,
1276 if (CHECK_FLAG(attr
->rmap_change_flags
,
1277 BATTR_RMAP_NEXTHOP_PEER_ADDRESS
))
1278 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1279 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
);
1281 if (CHECK_FLAG(attr
->rmap_change_flags
, BATTR_REFLECTED
))
1282 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1283 BPKT_ATTRVEC_FLAGS_REFLECTED
);
1285 if (CHECK_FLAG(attr
->rmap_change_flags
, BATTR_RMAP_NEXTHOP_UNCHANGED
))
1286 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1287 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
);
1289 if (CHECK_FLAG(attr
->rmap_change_flags
, BATTR_RMAP_IPV4_NHOP_CHANGED
))
1290 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1291 BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED
);
1293 if (CHECK_FLAG(attr
->rmap_change_flags
,
1294 BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED
))
1295 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1296 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED
);
1298 if (CHECK_FLAG(attr
->rmap_change_flags
,
1299 BATTR_RMAP_IPV6_LL_NHOP_CHANGED
))
1300 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1301 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_LNH_CHANGED
);
1304 /* Reset the Attributes vector array. The vector array is used to override
1305 * certain output parameters in the packet for a particular peer
1307 void bpacket_attr_vec_arr_reset(struct bpacket_attr_vec_arr
*vecarr
)
1315 while (i
< BGP_ATTR_VEC_MAX
) {
1316 vecarr
->entries
[i
].flags
= 0;
1317 vecarr
->entries
[i
].offset
= 0;
1322 /* Setup a particular node entry in the vecarr */
1323 void bpacket_attr_vec_arr_set_vec(struct bpacket_attr_vec_arr
*vecarr
,
1324 bpacket_attr_vec_type type
, struct stream
*s
,
1329 assert(type
< BGP_ATTR_VEC_MAX
);
1331 SET_FLAG(vecarr
->entries
[type
].flags
, BPKT_ATTRVEC_FLAGS_UPDATED
);
1332 vecarr
->entries
[type
].offset
= stream_get_endp(s
);
1334 bpacket_vec_arr_inherit_attr_flags(vecarr
, type
, attr
);