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 bool 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
;
334 LIST_REMOVE(paf
, pkt_train
);
335 paf
->next_pkt_to_send
= NULL
;
337 bpacket_queue_compact(q
);
340 unsigned int bpacket_queue_virtual_length(struct peer_af
*paf
)
343 struct bpacket
*last
;
344 struct bpacket_queue
*q
;
346 pkt
= paf
->next_pkt_to_send
;
347 if (!pkt
|| (pkt
->buffer
== NULL
))
348 /* Already at end of list */
352 if (TAILQ_EMPTY(&(q
->pkts
)))
355 last
= TAILQ_LAST(&(q
->pkts
), pkt_queue
);
356 if (last
->ver
>= pkt
->ver
)
357 return last
->ver
- pkt
->ver
;
359 /* sequence # rolled over */
360 return (UINT_MAX
- pkt
->ver
+ 1) + last
->ver
;
364 * Dump the bpacket queue
366 void bpacket_queue_show_vty(struct bpacket_queue
*q
, struct vty
*vty
)
371 pkt
= bpacket_queue_first(q
);
373 vty_out(vty
, " Packet %p ver %u buffer %p\n", pkt
, pkt
->ver
,
376 LIST_FOREACH (paf
, &(pkt
->peers
), pkt_train
) {
377 vty_out(vty
, " - %s\n", paf
->peer
->host
);
379 pkt
= bpacket_next(pkt
);
384 struct stream
*bpacket_reformat_for_peer(struct bpacket
*pkt
,
387 struct stream
*s
= NULL
;
388 bpacket_attr_vec
*vec
;
392 struct bgp_filter
*filter
;
394 s
= stream_dup(pkt
->buffer
);
395 peer
= PAF_PEER(paf
);
397 vec
= &pkt
->arr
.entries
[BGP_ATTR_VEC_NH
];
399 if (!CHECK_FLAG(vec
->flags
, BPKT_ATTRVEC_FLAGS_UPDATED
))
404 int route_map_sets_nh
;
406 nhlen
= stream_getc_from(s
, vec
->offset
);
407 filter
= &peer
->filter
[paf
->afi
][paf
->safi
];
409 if (peer_cap_enhe(peer
, paf
->afi
, paf
->safi
))
412 nhafi
= BGP_NEXTHOP_AFI_FROM_NHLEN(nhlen
);
414 if (nhafi
== AFI_IP
) {
415 struct in_addr v4nh
, *mod_v4nh
;
417 size_t offset_nh
= vec
->offset
+ 1;
420 (CHECK_FLAG(vec
->flags
,
421 BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED
)
424 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
));
427 case BGP_ATTR_NHLEN_IPV4
:
429 case BGP_ATTR_NHLEN_VPNV4
:
433 /* TODO: handle IPv6 nexthops */
435 EC_BGP_INVALID_NEXTHOP_LENGTH
,
436 "%s: %s: invalid MP nexthop length (AFI IP): %u",
437 __func__
, peer
->host
, nhlen
);
442 stream_get_from(&v4nh
, s
, offset_nh
, IPV4_MAX_BYTELEN
);
446 * If route-map has set the nexthop, that is normally
447 * used; if it is specified as peer-address, the peering
448 * address is picked up. Otherwise, if NH is unavailable
449 * from attribute, the peering addr is picked up; the
450 * "NH unavailable" case also covers next-hop-self and
451 * some other scenarios - see subgroup_announce_check().
452 * In all other cases, use the nexthop carried in the
453 * attribute unless it is EBGP non-multiaccess and there
454 * is no next-hop-unchanged setting or the peer is EBGP
455 * and the route-map that changed the next-hop value
456 * was applied inbound rather than outbound. Updates to
457 * an EBGP peer should only modify the next-hop if it
458 * was set in an outbound route-map to that peer.
459 * Note: It is assumed route-map cannot set the nexthop
460 * to an invalid value.
462 if (route_map_sets_nh
463 && ((peer
->sort
!= BGP_PEER_EBGP
)
464 || ROUTE_MAP_OUT(filter
))) {
467 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
)) {
468 mod_v4nh
= &peer
->nexthop
.v4
;
471 } else if (v4nh
.s_addr
== INADDR_ANY
) {
472 mod_v4nh
= &peer
->nexthop
.v4
;
474 } else if (peer
->sort
== BGP_PEER_EBGP
475 && (bgp_multiaccess_check_v4(v4nh
, peer
) == 0)
476 && !CHECK_FLAG(vec
->flags
,
477 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
)
478 && !peer_af_flag_check(
479 peer
, paf
->afi
, paf
->safi
,
480 PEER_FLAG_NEXTHOP_UNCHANGED
)) {
481 /* NOTE: not handling case where NH has new AFI
483 mod_v4nh
= &peer
->nexthop
.v4
;
487 if (nh_modified
) /* allow for VPN RD */
488 stream_put_in_addr_at(s
, offset_nh
, mod_v4nh
);
490 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
491 zlog_debug("u%" PRIu64
":s%" PRIu64
" %s send UPDATE w/ nexthop %s%s",
492 PAF_SUBGRP(paf
)->update_group
->id
,
493 PAF_SUBGRP(paf
)->id
, peer
->host
,
494 inet_ntoa(*mod_v4nh
),
495 (nhlen
== BGP_ATTR_NHLEN_VPNV4
? " and RD"
497 } else if (nhafi
== AFI_IP6
) {
498 struct in6_addr v6nhglobal
, *mod_v6nhg
;
499 struct in6_addr v6nhlocal
, *mod_v6nhl
;
500 int gnh_modified
, lnh_modified
;
501 size_t offset_nhglobal
= vec
->offset
+ 1;
502 size_t offset_nhlocal
= vec
->offset
+ 1;
504 gnh_modified
= lnh_modified
= 0;
505 mod_v6nhg
= &v6nhglobal
;
506 mod_v6nhl
= &v6nhlocal
;
509 (CHECK_FLAG(vec
->flags
,
510 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED
)
513 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
));
516 * The logic here is rather similar to that for IPv4, the
517 * additional work being to handle 1 or 2 nexthops.
518 * Also, 3rd party nexthop is not propagated for EBGP
522 case BGP_ATTR_NHLEN_IPV6_GLOBAL
:
524 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
:
525 offset_nhlocal
+= IPV6_MAX_BYTELEN
;
527 case BGP_ATTR_NHLEN_VPNV6_GLOBAL
:
528 offset_nhglobal
+= 8;
530 case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
:
531 offset_nhglobal
+= 8;
532 offset_nhlocal
+= 8 * 2 + IPV6_MAX_BYTELEN
;
535 /* TODO: handle IPv4 nexthops */
537 EC_BGP_INVALID_NEXTHOP_LENGTH
,
538 "%s: %s: invalid MP nexthop length (AFI IP6): %u",
539 __func__
, peer
->host
, nhlen
);
544 stream_get_from(&v6nhglobal
, s
, offset_nhglobal
,
548 * Updates to an EBGP peer should only modify the
549 * next-hop if it was set in an outbound route-map
552 if (route_map_sets_nh
553 && ((peer
->sort
!= BGP_PEER_EBGP
)
554 || ROUTE_MAP_OUT(filter
))) {
557 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
)) {
558 mod_v6nhg
= &peer
->nexthop
.v6_global
;
561 } else if (IN6_IS_ADDR_UNSPECIFIED(&v6nhglobal
)) {
562 mod_v6nhg
= &peer
->nexthop
.v6_global
;
564 } else if ((peer
->sort
== BGP_PEER_EBGP
)
565 && (!bgp_multiaccess_check_v6(v6nhglobal
, peer
))
566 && !CHECK_FLAG(vec
->flags
,
567 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
)
568 && !peer_af_flag_check(
569 peer
, nhafi
, paf
->safi
,
570 PEER_FLAG_NEXTHOP_UNCHANGED
)) {
571 /* NOTE: not handling case where NH has new AFI
573 mod_v6nhg
= &peer
->nexthop
.v6_global
;
577 if (IN6_IS_ADDR_UNSPECIFIED(mod_v6nhg
)) {
578 if (peer
->nexthop
.v4
.s_addr
) {
579 ipv4_to_ipv4_mapped_ipv6(mod_v6nhg
,
584 if (IS_MAPPED_IPV6(&peer
->nexthop
.v6_global
)) {
585 mod_v6nhg
= &peer
->nexthop
.v6_global
;
589 if (nhlen
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
590 || nhlen
== BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
591 stream_get_from(&v6nhlocal
, s
, offset_nhlocal
,
593 if (IN6_IS_ADDR_UNSPECIFIED(&v6nhlocal
)) {
594 mod_v6nhl
= &peer
->nexthop
.v6_local
;
600 stream_put_in6_addr_at(s
, offset_nhglobal
, mod_v6nhg
);
602 stream_put_in6_addr_at(s
, offset_nhlocal
, mod_v6nhl
);
604 if (bgp_debug_update(peer
, NULL
, NULL
, 0)) {
605 if (nhlen
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
606 || nhlen
== BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
)
608 "u%" PRIu64
":s%" PRIu64
" %s send UPDATE w/ mp_nexthops %s, %s%s",
609 PAF_SUBGRP(paf
)->update_group
->id
,
610 PAF_SUBGRP(paf
)->id
, peer
->host
,
611 inet_ntop(AF_INET6
, mod_v6nhg
, buf
,
613 inet_ntop(AF_INET6
, mod_v6nhl
, buf2
,
615 (nhlen
== BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
619 zlog_debug("u%" PRIu64
":s%" PRIu64
" %s send UPDATE w/ mp_nexthop %s%s",
620 PAF_SUBGRP(paf
)->update_group
->id
,
621 PAF_SUBGRP(paf
)->id
, peer
->host
,
622 inet_ntop(AF_INET6
, mod_v6nhg
, buf
,
624 (nhlen
== BGP_ATTR_NHLEN_VPNV6_GLOBAL
628 } else if (paf
->afi
== AFI_L2VPN
) {
629 struct in_addr v4nh
, *mod_v4nh
;
632 stream_get_from(&v4nh
, s
, vec
->offset
+ 1, 4);
635 /* No route-map changes allowed for EVPN nexthops. */
636 if (v4nh
.s_addr
== INADDR_ANY
) {
637 mod_v4nh
= &peer
->nexthop
.v4
;
642 stream_put_in_addr_at(s
, vec
->offset
+ 1, mod_v4nh
);
644 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
645 zlog_debug("u%" PRIu64
":s%" PRIu64
" %s send UPDATE w/ nexthop %s",
646 PAF_SUBGRP(paf
)->update_group
->id
,
647 PAF_SUBGRP(paf
)->id
, peer
->host
,
648 inet_ntoa(*mod_v4nh
));
655 * Update the vecarr offsets to go beyond 'pos' bytes, i.e. add 'pos'
658 static void bpacket_attr_vec_arr_update(struct bpacket_attr_vec_arr
*vecarr
,
666 for (i
= 0; i
< BGP_ATTR_VEC_MAX
; i
++)
667 vecarr
->entries
[i
].offset
+= pos
;
671 * Return if there are packets to build for this subgroup.
673 bool subgroup_packets_to_build(struct update_subgroup
*subgrp
)
675 struct bgp_advertise
*adv
;
680 adv
= bgp_adv_fifo_first(&subgrp
->sync
->withdraw
);
684 adv
= bgp_adv_fifo_first(&subgrp
->sync
->update
);
691 /* Make BGP update packet. */
692 struct bpacket
*subgroup_update_packet(struct update_subgroup
*subgrp
)
694 struct bpacket_attr_vec_arr vecarr
;
698 struct stream
*snlri
;
699 struct stream
*packet
;
700 struct bgp_adj_out
*adj
;
701 struct bgp_advertise
*adv
;
702 struct bgp_dest
*dest
= NULL
;
703 struct bgp_path_info
*path
= NULL
;
704 bgp_size_t total_attr_len
= 0;
705 unsigned long attrlen_pos
= 0;
706 size_t mpattrlen_pos
= 0;
707 size_t mpattr_pos
= 0;
710 int space_remaining
= 0;
711 int space_needed
= 0;
712 char send_attr_str
[BUFSIZ
];
713 int send_attr_printed
= 0;
715 int addpath_encode
= 0;
716 int addpath_overhead
= 0;
717 uint32_t addpath_tx_id
= 0;
718 struct prefix_rd
*prd
= NULL
;
719 mpls_label_t label
= MPLS_INVALID_LABEL
, *label_pnt
= NULL
;
720 uint32_t num_labels
= 0;
725 if (bpacket_queue_is_full(SUBGRP_INST(subgrp
), SUBGRP_PKTQ(subgrp
)))
728 peer
= SUBGRP_PEER(subgrp
);
729 afi
= SUBGRP_AFI(subgrp
);
730 safi
= SUBGRP_SAFI(subgrp
);
733 snlri
= subgrp
->scratch
;
736 bpacket_attr_vec_arr_reset(&vecarr
);
738 addpath_encode
= bgp_addpath_encode_tx(peer
, afi
, safi
);
739 addpath_overhead
= addpath_encode
? BGP_ADDPATH_ID_LEN
: 0;
741 adv
= bgp_adv_fifo_first(&subgrp
->sync
->update
);
743 const struct prefix
*dest_p
;
747 dest_p
= bgp_dest_get_prefix(dest
);
749 addpath_tx_id
= adj
->addpath_tx_id
;
752 /* Check if we need to add a prefix to the packet if
753 * maximum-prefix-out is set for the peer.
755 if (CHECK_FLAG(peer
->af_flags
[afi
][safi
],
756 PEER_FLAG_MAX_PREFIX_OUT
)
757 && subgrp
->scount
>= peer
->pmax_out
[afi
][safi
]) {
758 if (BGP_DEBUG(update
, UPDATE_OUT
)
759 || BGP_DEBUG(update
, UPDATE_PREFIX
)) {
761 "%s reached maximum prefix to be send (%u)",
762 peer
->host
, peer
->pmax_out
[afi
][safi
]);
767 space_remaining
= STREAM_CONCAT_REMAIN(s
, snlri
, STREAM_SIZE(s
))
768 - BGP_MAX_PACKET_SIZE_OVERFLOW
;
770 BGP_NLRI_LENGTH
+ addpath_overhead
771 + bgp_packet_mpattr_prefix_size(afi
, safi
, dest_p
);
773 /* When remaining space can't include NLRI and it's length. */
774 if (space_remaining
< space_needed
)
777 /* If packet is empty, set attribute. */
778 if (stream_empty(s
)) {
779 struct peer
*from
= NULL
;
784 /* 1: Write the BGP message header - 16 bytes marker, 2
786 * one byte message type.
788 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
790 /* 2: withdrawn routes length */
793 /* 3: total attributes length - attrlen_pos stores the
795 attrlen_pos
= stream_get_endp(s
);
798 /* 4: if there is MP_REACH_NLRI attribute, that should
800 * attribute, according to
801 * draft-ietf-idr-error-handling. Save the
804 mpattr_pos
= stream_get_endp(s
);
806 /* 5: Encode all the attributes, except MP_REACH_NLRI
808 total_attr_len
= bgp_packet_attribute(
809 NULL
, peer
, s
, adv
->baa
->attr
, &vecarr
, NULL
,
810 afi
, safi
, from
, NULL
, NULL
, 0, 0, 0);
813 STREAM_CONCAT_REMAIN(s
, snlri
, STREAM_SIZE(s
))
814 - BGP_MAX_PACKET_SIZE_OVERFLOW
;
815 space_needed
= BGP_NLRI_LENGTH
+ addpath_overhead
816 + bgp_packet_mpattr_prefix_size(
819 /* If the attributes alone do not leave any room for
822 if (space_remaining
< space_needed
) {
824 EC_BGP_UPDGRP_ATTR_LEN
,
825 "u%" PRIu64
":s%" PRIu64
" attributes too long, cannot send UPDATE",
826 subgrp
->update_group
->id
, subgrp
->id
);
828 /* Flush the FIFO update queue */
830 adv
= bgp_advertise_clean_subgroup(
835 if (BGP_DEBUG(update
, UPDATE_OUT
)
836 || BGP_DEBUG(update
, UPDATE_PREFIX
)) {
837 memset(send_attr_str
, 0, BUFSIZ
);
838 send_attr_printed
= 0;
839 bgp_dump_attr(adv
->baa
->attr
, send_attr_str
,
840 sizeof(send_attr_str
));
844 if ((afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
845 && !peer_cap_enhe(peer
, afi
, safi
))
846 stream_put_prefix_addpath(s
, dest_p
, addpath_encode
,
849 /* Encode the prefix in MP_REACH_NLRI attribute */
851 prd
= (struct prefix_rd
*)bgp_dest_get_prefix(
854 if (safi
== SAFI_LABELED_UNICAST
) {
855 label
= bgp_adv_label(dest
, path
, peer
, afi
,
859 } else if (path
&& path
->extra
) {
860 label_pnt
= &path
->extra
->label
[0];
861 num_labels
= path
->extra
->num_labels
;
864 if (stream_empty(snlri
))
865 mpattrlen_pos
= bgp_packet_mpattr_start(
866 snlri
, peer
, afi
, safi
, &vecarr
,
869 bgp_packet_mpattr_prefix(snlri
, afi
, safi
, dest_p
, prd
,
870 label_pnt
, num_labels
,
871 addpath_encode
, addpath_tx_id
,
877 if (bgp_debug_update(NULL
, dest_p
, subgrp
->update_group
, 0)) {
878 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
880 if (!send_attr_printed
) {
881 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE w/ attr: %s",
882 subgrp
->update_group
->id
, subgrp
->id
,
884 if (!stream_empty(snlri
)) {
886 iana_safi_t pkt_safi
;
888 pkt_afi
= afi_int2iana(afi
);
889 pkt_safi
= safi_int2iana(safi
);
891 "u%" PRIu64
":s%" PRIu64
" send MP_REACH for afi/safi %d/%d",
892 subgrp
->update_group
->id
,
893 subgrp
->id
, pkt_afi
, pkt_safi
);
896 send_attr_printed
= 1;
899 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, dest_p
,
900 label_pnt
, num_labels
,
901 addpath_encode
, addpath_tx_id
,
902 pfx_buf
, sizeof(pfx_buf
));
903 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE %s",
904 subgrp
->update_group
->id
, subgrp
->id
,
908 /* Synchnorize attribute. */
910 bgp_attr_unintern(&adj
->attr
);
914 adj
->attr
= bgp_attr_intern(adv
->baa
->attr
);
916 adv
= bgp_advertise_clean_subgroup(subgrp
, adj
);
919 if (!stream_empty(s
)) {
920 if (!stream_empty(snlri
)) {
921 bgp_packet_mpattr_end(snlri
, mpattrlen_pos
);
922 total_attr_len
+= stream_get_endp(snlri
);
925 /* set the total attribute length correctly */
926 stream_putw_at(s
, attrlen_pos
, total_attr_len
);
928 if (!stream_empty(snlri
)) {
929 packet
= stream_dupcat(s
, snlri
, mpattr_pos
);
930 bpacket_attr_vec_arr_update(&vecarr
, mpattr_pos
);
932 packet
= stream_dup(s
);
933 bgp_packet_set_size(packet
);
934 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
935 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE len %zd numpfx %d",
936 subgrp
->update_group
->id
, subgrp
->id
,
937 (stream_get_endp(packet
)
938 - stream_get_getp(packet
)),
940 pkt
= bpacket_queue_add(SUBGRP_PKTQ(subgrp
), packet
, &vecarr
);
948 /* Make BGP withdraw packet. */
950 16-octet marker | 2-octet length | 1-octet type |
951 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0)
953 /* For other afi/safis:
954 16-octet marker | 2-octet length | 1-octet type |
955 2-octet withdrawn route length (=0) | 2-octet attrlen |
956 mp_unreach attr type | attr len | afi | safi | withdrawn prefixes
958 struct bpacket
*subgroup_withdraw_packet(struct update_subgroup
*subgrp
)
962 struct bgp_adj_out
*adj
;
963 struct bgp_advertise
*adv
;
965 struct bgp_dest
*dest
;
966 bgp_size_t unfeasible_len
;
967 bgp_size_t total_attr_len
;
969 size_t attrlen_pos
= 0;
970 size_t mplen_pos
= 0;
971 uint8_t first_time
= 1;
974 int space_remaining
= 0;
975 int space_needed
= 0;
977 int addpath_encode
= 0;
978 int addpath_overhead
= 0;
979 uint32_t addpath_tx_id
= 0;
980 const struct prefix_rd
*prd
= NULL
;
986 if (bpacket_queue_is_full(SUBGRP_INST(subgrp
), SUBGRP_PKTQ(subgrp
)))
989 peer
= SUBGRP_PEER(subgrp
);
990 afi
= SUBGRP_AFI(subgrp
);
991 safi
= SUBGRP_SAFI(subgrp
);
994 addpath_encode
= bgp_addpath_encode_tx(peer
, afi
, safi
);
995 addpath_overhead
= addpath_encode
? BGP_ADDPATH_ID_LEN
: 0;
997 while ((adv
= bgp_adv_fifo_first(&subgrp
->sync
->withdraw
)) != NULL
) {
998 const struct prefix
*dest_p
;
1003 dest_p
= bgp_dest_get_prefix(dest
);
1004 addpath_tx_id
= adj
->addpath_tx_id
;
1007 STREAM_WRITEABLE(s
) - BGP_MAX_PACKET_SIZE_OVERFLOW
;
1009 BGP_NLRI_LENGTH
+ addpath_overhead
+ BGP_TOTAL_ATTR_LEN
1010 + bgp_packet_mpattr_prefix_size(afi
, safi
, dest_p
);
1012 if (space_remaining
< space_needed
)
1015 if (stream_empty(s
)) {
1016 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
1017 stream_putw(s
, 0); /* unfeasible routes length */
1021 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
1022 && !peer_cap_enhe(peer
, afi
, safi
))
1023 stream_put_prefix_addpath(s
, dest_p
, addpath_encode
,
1027 prd
= (struct prefix_rd
*)bgp_dest_get_prefix(
1030 /* If first time, format the MP_UNREACH header
1034 iana_safi_t pkt_safi
;
1036 pkt_afi
= afi_int2iana(afi
);
1037 pkt_safi
= safi_int2iana(safi
);
1039 attrlen_pos
= stream_get_endp(s
);
1040 /* total attr length = 0 for now.
1041 * reevaluate later */
1043 mp_start
= stream_get_endp(s
);
1044 mplen_pos
= bgp_packet_mpunreach_start(s
, afi
,
1046 if (bgp_debug_update(NULL
, NULL
,
1047 subgrp
->update_group
, 0))
1049 "u%" PRIu64
":s%" PRIu64
" send MP_UNREACH for afi/safi %d/%d",
1050 subgrp
->update_group
->id
,
1051 subgrp
->id
, pkt_afi
, pkt_safi
);
1054 bgp_packet_mpunreach_prefix(s
, dest_p
, afi
, safi
, prd
,
1055 NULL
, 0, addpath_encode
,
1056 addpath_tx_id
, NULL
);
1061 if (bgp_debug_update(NULL
, dest_p
, subgrp
->update_group
, 0)) {
1062 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
1064 bgp_debug_rdpfxpath2str(afi
, safi
, prd
, dest_p
, NULL
, 0,
1065 addpath_encode
, addpath_tx_id
,
1066 pfx_buf
, sizeof(pfx_buf
));
1067 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE %s -- unreachable",
1068 subgrp
->update_group
->id
, subgrp
->id
,
1074 bgp_adj_out_remove_subgroup(dest
, adj
, subgrp
);
1075 bgp_dest_unlock_node(dest
);
1078 if (!stream_empty(s
)) {
1079 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
1080 && !peer_cap_enhe(peer
, afi
, safi
)) {
1081 unfeasible_len
= stream_get_endp(s
) - BGP_HEADER_SIZE
1082 - BGP_UNFEASIBLE_LEN
;
1083 stream_putw_at(s
, BGP_HEADER_SIZE
, unfeasible_len
);
1086 /* Set the mp_unreach attr's length */
1087 bgp_packet_mpunreach_end(s
, mplen_pos
);
1089 /* Set total path attribute length. */
1090 total_attr_len
= stream_get_endp(s
) - mp_start
;
1091 stream_putw_at(s
, attrlen_pos
, total_attr_len
);
1093 bgp_packet_set_size(s
);
1094 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
1095 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE (withdraw) len %zd numpfx %d",
1096 subgrp
->update_group
->id
, subgrp
->id
,
1097 (stream_get_endp(s
) - stream_get_getp(s
)),
1099 pkt
= bpacket_queue_add(SUBGRP_PKTQ(subgrp
), stream_dup(s
),
1108 void subgroup_default_update_packet(struct update_subgroup
*subgrp
,
1109 struct attr
*attr
, struct peer
*from
)
1115 bgp_size_t total_attr_len
;
1118 struct bpacket_attr_vec_arr vecarr
;
1119 int addpath_encode
= 0;
1121 if (DISABLE_BGP_ANNOUNCE
)
1127 peer
= SUBGRP_PEER(subgrp
);
1128 afi
= SUBGRP_AFI(subgrp
);
1129 safi
= SUBGRP_SAFI(subgrp
);
1130 bpacket_attr_vec_arr_reset(&vecarr
);
1131 addpath_encode
= bgp_addpath_encode_tx(peer
, afi
, safi
);
1133 memset(&p
, 0, sizeof(p
));
1134 p
.family
= afi2family(afi
);
1137 /* Logging the attribute. */
1138 if (bgp_debug_update(NULL
, &p
, subgrp
->update_group
, 0)) {
1139 char attrstr
[BUFSIZ
];
1140 char buf
[PREFIX_STRLEN
];
1141 /* ' with addpath ID ' 17
1142 * max strlen of uint32 + 10
1143 * +/- (just in case) + 1
1144 * null terminator + 1
1145 * ============================ 29 */
1150 bgp_dump_attr(attr
, attrstr
, sizeof(attrstr
));
1153 snprintf(tx_id_buf
, sizeof(tx_id_buf
),
1154 " with addpath ID %u",
1155 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1157 tx_id_buf
[0] = '\0';
1159 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE %s%s %s",
1160 (SUBGRP_UPDGRP(subgrp
))->id
, subgrp
->id
,
1161 prefix2str(&p
, buf
, sizeof(buf
)), tx_id_buf
,
1165 s
= stream_new(BGP_MAX_PACKET_SIZE
);
1167 /* Make BGP update packet. */
1168 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
1170 /* Unfeasible Routes Length. */
1173 /* Make place for total attribute length. */
1174 pos
= stream_get_endp(s
);
1176 total_attr_len
= bgp_packet_attribute(
1177 NULL
, peer
, s
, attr
, &vecarr
, &p
, afi
, safi
, from
, NULL
, NULL
,
1178 0, addpath_encode
, BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1180 /* Set Total Path Attribute Length. */
1181 stream_putw_at(s
, pos
, total_attr_len
);
1184 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
1185 && !peer_cap_enhe(peer
, afi
, safi
))
1186 stream_put_prefix_addpath(
1187 s
, &p
, addpath_encode
,
1188 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1191 bgp_packet_set_size(s
);
1193 (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp
), s
, &vecarr
);
1194 subgroup_trigger_write(subgrp
);
1197 void subgroup_default_withdraw_packet(struct update_subgroup
*subgrp
)
1202 unsigned long attrlen_pos
= 0;
1204 bgp_size_t unfeasible_len
;
1205 bgp_size_t total_attr_len
= 0;
1206 size_t mp_start
= 0;
1207 size_t mplen_pos
= 0;
1210 int addpath_encode
= 0;
1212 if (DISABLE_BGP_ANNOUNCE
)
1215 peer
= SUBGRP_PEER(subgrp
);
1216 afi
= SUBGRP_AFI(subgrp
);
1217 safi
= SUBGRP_SAFI(subgrp
);
1218 addpath_encode
= bgp_addpath_encode_tx(peer
, afi
, safi
);
1220 memset(&p
, 0, sizeof(p
));
1221 p
.family
= afi2family(afi
);
1224 if (bgp_debug_update(NULL
, &p
, subgrp
->update_group
, 0)) {
1225 char buf
[PREFIX_STRLEN
];
1226 /* ' with addpath ID ' 17
1227 * max strlen of uint32 + 10
1228 * +/- (just in case) + 1
1229 * null terminator + 1
1230 * ============================ 29 */
1234 snprintf(tx_id_buf
, sizeof(tx_id_buf
),
1235 " with addpath ID %u",
1236 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1238 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE %s%s -- unreachable",
1239 (SUBGRP_UPDGRP(subgrp
))->id
, subgrp
->id
,
1240 prefix2str(&p
, buf
, sizeof(buf
)), tx_id_buf
);
1243 s
= stream_new(BGP_MAX_PACKET_SIZE
);
1245 /* Make BGP update packet. */
1246 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
1248 /* Unfeasible Routes Length. */;
1249 cp
= stream_get_endp(s
);
1252 /* Withdrawn Routes. */
1253 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
1254 && !peer_cap_enhe(peer
, afi
, safi
)) {
1255 stream_put_prefix_addpath(
1256 s
, &p
, addpath_encode
,
1257 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1259 unfeasible_len
= stream_get_endp(s
) - cp
- 2;
1261 /* Set unfeasible len. */
1262 stream_putw_at(s
, cp
, unfeasible_len
);
1264 /* Set total path attribute length. */
1267 attrlen_pos
= stream_get_endp(s
);
1269 mp_start
= stream_get_endp(s
);
1270 mplen_pos
= bgp_packet_mpunreach_start(s
, afi
, safi
);
1271 bgp_packet_mpunreach_prefix(
1272 s
, &p
, afi
, safi
, NULL
, NULL
, 0, addpath_encode
,
1273 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
, NULL
);
1275 /* Set the mp_unreach attr's length */
1276 bgp_packet_mpunreach_end(s
, mplen_pos
);
1278 /* Set total path attribute length. */
1279 total_attr_len
= stream_get_endp(s
) - mp_start
;
1280 stream_putw_at(s
, attrlen_pos
, total_attr_len
);
1283 bgp_packet_set_size(s
);
1285 (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp
), s
, NULL
);
1286 subgroup_trigger_write(subgrp
);
1290 bpacket_vec_arr_inherit_attr_flags(struct bpacket_attr_vec_arr
*vecarr
,
1291 bpacket_attr_vec_type type
,
1294 if (CHECK_FLAG(attr
->rmap_change_flags
,
1295 BATTR_RMAP_NEXTHOP_PEER_ADDRESS
))
1296 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1297 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
);
1299 if (CHECK_FLAG(attr
->rmap_change_flags
, BATTR_REFLECTED
))
1300 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1301 BPKT_ATTRVEC_FLAGS_REFLECTED
);
1303 if (CHECK_FLAG(attr
->rmap_change_flags
, BATTR_RMAP_NEXTHOP_UNCHANGED
))
1304 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1305 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
);
1307 if (CHECK_FLAG(attr
->rmap_change_flags
, BATTR_RMAP_IPV4_NHOP_CHANGED
))
1308 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1309 BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED
);
1311 if (CHECK_FLAG(attr
->rmap_change_flags
,
1312 BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED
))
1313 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1314 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED
);
1316 if (CHECK_FLAG(attr
->rmap_change_flags
,
1317 BATTR_RMAP_IPV6_LL_NHOP_CHANGED
))
1318 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1319 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_LNH_CHANGED
);
1322 /* Reset the Attributes vector array. The vector array is used to override
1323 * certain output parameters in the packet for a particular peer
1325 void bpacket_attr_vec_arr_reset(struct bpacket_attr_vec_arr
*vecarr
)
1333 while (i
< BGP_ATTR_VEC_MAX
) {
1334 vecarr
->entries
[i
].flags
= 0;
1335 vecarr
->entries
[i
].offset
= 0;
1340 /* Setup a particular node entry in the vecarr */
1341 void bpacket_attr_vec_arr_set_vec(struct bpacket_attr_vec_arr
*vecarr
,
1342 bpacket_attr_vec_type type
, struct stream
*s
,
1347 assert(type
< BGP_ATTR_VEC_MAX
);
1349 SET_FLAG(vecarr
->entries
[type
].flags
, BPKT_ATTRVEC_FLAGS_UPDATED
);
1350 vecarr
->entries
[type
].offset
= stream_get_endp(s
);
1352 bpacket_vec_arr_inherit_attr_flags(vecarr
, type
, attr
);