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
23 * along with GNU Zebra; see the file COPYING. If not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
35 #include "sockunion.h"
43 #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"
65 struct bpacket
*bpacket_alloc()
69 pkt
= (struct bpacket
*)XCALLOC(MTYPE_BGP_PACKET
,
70 sizeof(struct bpacket
));
75 void bpacket_free(struct bpacket
*pkt
)
78 stream_free(pkt
->buffer
);
80 XFREE(MTYPE_BGP_PACKET
, pkt
);
83 void bpacket_queue_init(struct bpacket_queue
*q
)
85 TAILQ_INIT(&(q
->pkts
));
89 * bpacket_queue_sanity_check
91 void bpacket_queue_sanity_check(struct bpacket_queue
__attribute__((__unused__
))
97 pkt
= bpacket_queue_last (q
);
99 assert (!pkt
->buffer
);
102 * Make sure the count of packets is correct.
106 pkt
= bpacket_queue_first (q
);
111 if (num_pkts
> q
->curr_count
)
114 pkt
= TAILQ_NEXT (pkt
, pkt_train
);
117 assert (num_pkts
== q
->curr_count
);
122 * bpacket_queue_add_packet
124 * Internal function of bpacket_queue - and adds a
125 * packet entry to the end of the list.
127 * Users of bpacket_queue should use bpacket_queue_add instead.
129 static void bpacket_queue_add_packet(struct bpacket_queue
*q
,
132 struct bpacket
*last_pkt
;
134 if (TAILQ_EMPTY(&(q
->pkts
)))
135 TAILQ_INSERT_TAIL(&(q
->pkts
), pkt
, pkt_train
);
137 last_pkt
= bpacket_queue_last(q
);
138 TAILQ_INSERT_AFTER(&(q
->pkts
), last_pkt
, pkt
, pkt_train
);
141 if (q
->hwm_count
< q
->curr_count
)
142 q
->hwm_count
= q
->curr_count
;
146 * Adds a packet to the bpacket_queue.
148 * The stream passed is consumed by this function. So, the caller should
149 * not free or use the stream after
150 * invoking this function.
152 struct bpacket
*bpacket_queue_add(struct bpacket_queue
*q
, struct stream
*s
,
153 struct bpacket_attr_vec_arr
*vecarrp
)
156 struct bpacket
*last_pkt
;
159 pkt
= bpacket_alloc();
160 if (TAILQ_EMPTY(&(q
->pkts
))) {
164 memcpy(&pkt
->arr
, vecarrp
,
165 sizeof(struct bpacket_attr_vec_arr
));
167 bpacket_attr_vec_arr_reset(&pkt
->arr
);
168 bpacket_queue_add_packet(q
, pkt
);
169 bpacket_queue_sanity_check(q
);
174 * Fill in the new information into the current sentinel and create a
177 bpacket_queue_sanity_check(q
);
178 last_pkt
= bpacket_queue_last(q
);
179 assert(last_pkt
->buffer
== NULL
);
180 last_pkt
->buffer
= s
;
182 memcpy(&last_pkt
->arr
, vecarrp
,
183 sizeof(struct bpacket_attr_vec_arr
));
185 bpacket_attr_vec_arr_reset(&last_pkt
->arr
);
187 pkt
->ver
= last_pkt
->ver
;
189 bpacket_queue_add_packet(q
, pkt
);
191 bpacket_queue_sanity_check(q
);
195 struct bpacket
*bpacket_queue_first(struct bpacket_queue
*q
)
197 return (TAILQ_FIRST(&(q
->pkts
)));
200 struct bpacket
*bpacket_queue_last(struct bpacket_queue
*q
)
202 return TAILQ_LAST(&(q
->pkts
), pkt_queue
);
205 struct bpacket
*bpacket_queue_remove(struct bpacket_queue
*q
)
207 struct bpacket
*first
;
209 first
= bpacket_queue_first(q
);
211 TAILQ_REMOVE(&(q
->pkts
), first
, pkt_train
);
217 unsigned int bpacket_queue_length(struct bpacket_queue
*q
)
219 return q
->curr_count
- 1;
222 unsigned int bpacket_queue_hwm_length(struct bpacket_queue
*q
)
224 return q
->hwm_count
- 1;
227 int bpacket_queue_is_full(struct bgp
*bgp
, struct bpacket_queue
*q
)
229 if (q
->curr_count
>= bgp
->default_subgroup_pkt_queue_max
)
234 void bpacket_add_peer(struct bpacket
*pkt
, struct peer_af
*paf
)
239 LIST_INSERT_HEAD(&(pkt
->peers
), paf
, pkt_train
);
240 paf
->next_pkt_to_send
= pkt
;
244 * bpacket_queue_cleanup
246 void bpacket_queue_cleanup(struct bpacket_queue
*q
)
250 while ((pkt
= bpacket_queue_remove(q
))) {
256 * bpacket_queue_compact
258 * Delete packets that do not need to be transmitted to any peer from
261 * @return the number of packets deleted.
263 static int bpacket_queue_compact(struct bpacket_queue
*q
)
266 struct bpacket
*pkt
, *removed_pkt
;
271 pkt
= bpacket_queue_first(q
);
276 * Don't delete the sentinel.
281 if (!LIST_EMPTY(&(pkt
->peers
)))
284 removed_pkt
= bpacket_queue_remove(q
);
285 assert(pkt
== removed_pkt
);
286 bpacket_free(removed_pkt
);
291 bpacket_queue_sanity_check(q
);
295 void bpacket_queue_advance_peer(struct peer_af
*paf
)
298 struct bpacket
*old_pkt
;
300 old_pkt
= paf
->next_pkt_to_send
;
301 if (old_pkt
->buffer
== NULL
)
302 /* Already at end of list */
305 LIST_REMOVE(paf
, pkt_train
);
306 pkt
= TAILQ_NEXT(old_pkt
, pkt_train
);
307 bpacket_add_peer(pkt
, paf
);
309 if (!bpacket_queue_compact(PAF_PKTQ(paf
)))
313 * Deleted one or more packets. Check if we can now merge this
314 * peer's subgroup into another subgroup.
316 update_subgroup_check_merge(paf
->subgroup
, "advanced peer in queue");
320 * bpacket_queue_remove_peer
322 * Remove the peer from the packet queue of the subgroup it belongs
325 void bpacket_queue_remove_peer(struct peer_af
*paf
)
327 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%s", pkt
, pkt
->ver
,
374 pkt
->buffer
, VTY_NEWLINE
);
376 LIST_FOREACH(paf
, &(pkt
->peers
), pkt_train
)
378 vty_out(vty
, " - %s%s", 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
;
395 s
= stream_dup(pkt
->buffer
);
396 peer
= PAF_PEER(paf
);
398 vec
= &pkt
->arr
.entries
[BGP_ATTR_VEC_NH
];
399 if (CHECK_FLAG(vec
->flags
, BPKT_ATTRVEC_FLAGS_UPDATED
)) {
401 afi_t nhafi
= AFI_MAX
; /* NH AFI is based on nhlen! */
402 int route_map_sets_nh
;
403 nhlen
= stream_getc_from(s
, vec
->offset
);
404 if (paf
->afi
== AFI_IP
|| paf
->afi
== AFI_IP6
) {
405 nhafi
= BGP_NEXTHOP_AFI_FROM_NHLEN(nhlen
);
406 if (peer_cap_enhe(peer
))
410 if (nhafi
== AFI_IP
) {
411 struct in_addr v4nh
, *mod_v4nh
;
413 size_t offset_nh
= vec
->offset
+ 1;
418 BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED
)
421 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
));
424 case BGP_ATTR_NHLEN_IPV4
:
426 case BGP_ATTR_NHLEN_VPNV4
:
430 /* TODO: handle IPv6 nexthops */
432 "%s: %s: invalid MP nexthop length (AFI IP): %u",
433 __func__
, peer
->host
, nhlen
);
437 stream_get_from(&v4nh
, s
, offset_nh
, IPV4_MAX_BYTELEN
);
441 * If route-map has set the nexthop, that is always
443 * specified as peer-address, the peering address is
445 * Otherwise, if NH is unavailable from attribute, the
447 * is picked up; the "NH unavailable" case also covers
449 * and some other scenarios -- see
450 * subgroup_announce_check(). In
451 * all other cases, use the nexthop carried in the
453 * it is EBGP non-multiaccess and there is no
454 * next-hop-unchanged setting.
455 * Note: It is assumed route-map cannot set the nexthop
459 if (route_map_sets_nh
) {
462 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
)) {
463 mod_v4nh
= &peer
->nexthop
.v4
;
466 } else if (!v4nh
.s_addr
) {
467 mod_v4nh
= &peer
->nexthop
.v4
;
470 peer
->sort
== BGP_PEER_EBGP
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
);
541 stream_get_from(&v6nhglobal
, s
, offset_nhglobal
,
543 if (route_map_sets_nh
) {
546 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
)) {
547 mod_v6nhg
= &peer
->nexthop
.v6_global
;
550 } else if (IN6_IS_ADDR_UNSPECIFIED(&v6nhglobal
)) {
551 mod_v6nhg
= &peer
->nexthop
.v6_global
;
554 peer
->sort
== BGP_PEER_EBGP
557 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
)
558 && !peer_af_flag_check(
559 peer
, nhafi
, paf
->safi
,
560 PEER_FLAG_NEXTHOP_UNCHANGED
)) {
561 /* NOTE: not handling case where NH has new AFI
563 mod_v6nhg
= &peer
->nexthop
.v6_global
;
568 if (nhlen
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
569 || nhlen
== BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
) {
570 stream_get_from(&v6nhlocal
, s
, offset_nhlocal
,
572 if (IN6_IS_ADDR_UNSPECIFIED(&v6nhlocal
)) {
573 mod_v6nhl
= &peer
->nexthop
.v6_local
;
579 stream_put_in6_addr_at(s
, offset_nhglobal
,
582 stream_put_in6_addr_at(s
, offset_nhlocal
,
585 if (bgp_debug_update(peer
, NULL
, NULL
, 0)) {
586 if (nhlen
== 32 || nhlen
== 48)
588 "u%" PRIu64
":s%" PRIu64
589 " %s send UPDATE w/ mp_nexthops %s, %s%s",
592 PAF_SUBGRP(paf
)->id
, peer
->host
,
593 inet_ntop(AF_INET6
, mod_v6nhg
,
595 inet_ntop(AF_INET6
, mod_v6nhl
,
597 (nhlen
== 48 ? " and RD" : ""));
600 "u%" PRIu64
":s%" PRIu64
601 " %s send UPDATE w/ mp_nexthop %s%s",
604 PAF_SUBGRP(paf
)->id
, peer
->host
,
605 inet_ntop(AF_INET6
, mod_v6nhg
,
607 (nhlen
== 24 ? " and RD" : ""));
612 bgp_packet_add(peer
, s
);
617 * Update the vecarr offsets to go beyond 'pos' bytes, i.e. add 'pos'
620 static void bpacket_attr_vec_arr_update(struct bpacket_attr_vec_arr
*vecarr
,
628 for (i
= 0; i
< BGP_ATTR_VEC_MAX
; i
++)
629 vecarr
->entries
[i
].offset
+= pos
;
633 * Return if there are packets to build for this subgroup.
635 int subgroup_packets_to_build(struct update_subgroup
*subgrp
)
637 struct bgp_advertise
*adv
;
642 adv
= BGP_ADV_FIFO_HEAD(&subgrp
->sync
->withdraw
);
646 adv
= BGP_ADV_FIFO_HEAD(&subgrp
->sync
->update
);
653 /* Make BGP update packet. */
654 struct bpacket
*subgroup_update_packet(struct update_subgroup
*subgrp
)
656 struct bpacket_attr_vec_arr vecarr
;
660 struct stream
*snlri
;
661 struct stream
*packet
;
662 struct bgp_adj_out
*adj
;
663 struct bgp_advertise
*adv
;
664 struct bgp_node
*rn
= NULL
;
665 struct bgp_info
*binfo
= NULL
;
666 bgp_size_t total_attr_len
= 0;
667 unsigned long attrlen_pos
= 0;
668 size_t mpattrlen_pos
= 0;
669 size_t mpattr_pos
= 0;
672 int space_remaining
= 0;
673 int space_needed
= 0;
674 char send_attr_str
[BUFSIZ
];
675 int send_attr_printed
= 0;
677 int addpath_encode
= 0;
678 u_int32_t addpath_tx_id
= 0;
679 struct prefix_rd
*prd
= NULL
;
684 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_encode
= bgp_addpath_encode_tx(peer
, afi
, safi
);
700 adv
= BGP_ADV_FIFO_HEAD(&subgrp
->sync
->update
);
705 addpath_tx_id
= adj
->addpath_tx_id
;
708 space_remaining
= STREAM_CONCAT_REMAIN(s
, snlri
, STREAM_SIZE(s
))
709 - BGP_MAX_PACKET_SIZE_OVERFLOW
;
710 space_needed
= BGP_NLRI_LENGTH
+ bgp_packet_mpattr_prefix_size(
713 /* When remaining space can't include NLRI and it's length. */
714 if (space_remaining
< space_needed
)
717 /* If packet is empty, set attribute. */
718 if (stream_empty(s
)) {
719 struct peer
*from
= NULL
;
724 /* 1: Write the BGP message header - 16 bytes marker, 2
726 * one byte message type.
728 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
730 /* 2: withdrawn routes length */
733 /* 3: total attributes length - attrlen_pos stores the
735 attrlen_pos
= stream_get_endp(s
);
738 /* 4: if there is MP_REACH_NLRI attribute, that should
740 * attribute, according to
741 * draft-ietf-idr-error-handling. Save the
744 mpattr_pos
= stream_get_endp(s
);
746 /* 5: Encode all the attributes, except MP_REACH_NLRI
748 total_attr_len
= bgp_packet_attribute(
749 NULL
, peer
, s
, adv
->baa
->attr
, &vecarr
, NULL
,
750 afi
, safi
, from
, NULL
, NULL
, 0, 0);
753 STREAM_CONCAT_REMAIN(s
, snlri
, STREAM_SIZE(s
))
754 - BGP_MAX_PACKET_SIZE_OVERFLOW
;
756 BGP_NLRI_LENGTH
+ bgp_packet_mpattr_prefix_size(
759 /* If the attributes alone do not leave any room for
762 if (space_remaining
< space_needed
) {
764 "u%" PRIu64
":s%" PRIu64
765 " attributes too long, cannot send UPDATE",
766 subgrp
->update_group
->id
, subgrp
->id
);
768 /* Flush the FIFO update queue */
770 adv
= bgp_advertise_clean_subgroup(
775 if (BGP_DEBUG(update
, UPDATE_OUT
)
776 || BGP_DEBUG(update
, UPDATE_PREFIX
)) {
777 memset(send_attr_str
, 0, BUFSIZ
);
778 send_attr_printed
= 0;
779 bgp_dump_attr(peer
, adv
->baa
->attr
,
780 send_attr_str
, BUFSIZ
);
784 if ((afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
785 && !peer_cap_enhe(peer
))
786 stream_put_prefix_addpath(s
, &rn
->p
, addpath_encode
,
789 /* Encode the prefix in MP_REACH_NLRI attribute */
793 prd
= (struct prefix_rd
*)&rn
->prn
->p
;
794 if (binfo
&& binfo
->extra
)
795 tag
= binfo
->extra
->tag
;
797 if (stream_empty(snlri
))
798 mpattrlen_pos
= bgp_packet_mpattr_start(
802 : AFI_MAX
), /* get from NH */
803 &vecarr
, adv
->baa
->attr
);
805 bgp_packet_mpattr_prefix(snlri
, afi
, safi
, &rn
->p
, prd
,
807 addpath_tx_id
, adv
->baa
->attr
);
812 if (bgp_debug_update(NULL
, &rn
->p
, subgrp
->update_group
, 0)) {
813 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
815 if (!send_attr_printed
) {
816 zlog_debug("u%" PRIu64
":s%" PRIu64
817 " send UPDATE w/ attr: %s",
818 subgrp
->update_group
->id
, subgrp
->id
,
820 send_attr_printed
= 1;
823 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE %s",
824 subgrp
->update_group
->id
, subgrp
->id
,
825 bgp_debug_rdpfxpath2str(
826 prd
, &rn
->p
, addpath_encode
,
827 addpath_tx_id
, pfx_buf
,
831 /* Synchnorize attribute. */
833 bgp_attr_unintern(&adj
->attr
);
837 adj
->attr
= bgp_attr_intern(adv
->baa
->attr
);
839 adv
= bgp_advertise_clean_subgroup(subgrp
, adj
);
842 if (!stream_empty(s
)) {
843 if (!stream_empty(snlri
)) {
844 bgp_packet_mpattr_end(snlri
, mpattrlen_pos
);
845 total_attr_len
+= stream_get_endp(snlri
);
848 /* set the total attribute length correctly */
849 stream_putw_at(s
, attrlen_pos
, total_attr_len
);
851 if (!stream_empty(snlri
)) {
852 packet
= stream_dupcat(s
, snlri
, mpattr_pos
);
853 bpacket_attr_vec_arr_update(&vecarr
, mpattr_pos
);
855 packet
= stream_dup(s
);
856 bgp_packet_set_size(packet
);
857 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
858 zlog_debug("u%" PRIu64
":s%" PRIu64
859 " UPDATE len %zd numpfx %d",
860 subgrp
->update_group
->id
, subgrp
->id
,
861 (stream_get_endp(packet
)
862 - stream_get_getp(packet
)),
864 pkt
= bpacket_queue_add(SUBGRP_PKTQ(subgrp
), packet
, &vecarr
);
872 /* Make BGP withdraw packet. */
874 16-octet marker | 2-octet length | 1-octet type |
875 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0)
877 /* For other afi/safis:
878 16-octet marker | 2-octet length | 1-octet type |
879 2-octet withdrawn route length (=0) | 2-octet attrlen |
880 mp_unreach attr type | attr len | afi | safi | withdrawn prefixes
882 struct bpacket
*subgroup_withdraw_packet(struct update_subgroup
*subgrp
)
886 struct bgp_adj_out
*adj
;
887 struct bgp_advertise
*adv
;
890 bgp_size_t unfeasible_len
;
891 bgp_size_t total_attr_len
;
893 size_t attrlen_pos
= 0;
894 size_t mplen_pos
= 0;
895 u_char first_time
= 1;
898 int space_remaining
= 0;
899 int space_needed
= 0;
901 int addpath_encode
= 0;
902 u_int32_t addpath_tx_id
= 0;
903 struct prefix_rd
*prd
= NULL
;
909 if (bpacket_queue_is_full(SUBGRP_INST(subgrp
), SUBGRP_PKTQ(subgrp
)))
912 peer
= SUBGRP_PEER(subgrp
);
913 afi
= SUBGRP_AFI(subgrp
);
914 safi
= SUBGRP_SAFI(subgrp
);
917 addpath_encode
= bgp_addpath_encode_tx(peer
, afi
, safi
);
919 while ((adv
= BGP_ADV_FIFO_HEAD(&subgrp
->sync
->withdraw
)) != NULL
) {
923 addpath_tx_id
= adj
->addpath_tx_id
;
926 STREAM_REMAIN(s
) - BGP_MAX_PACKET_SIZE_OVERFLOW
;
928 BGP_NLRI_LENGTH
+ BGP_TOTAL_ATTR_LEN
929 + bgp_packet_mpattr_prefix_size(afi
, safi
, &rn
->p
);
931 if (space_remaining
< space_needed
)
934 if (stream_empty(s
)) {
935 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
936 stream_putw(s
, 0); /* unfeasible routes length */
940 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
941 && !peer_cap_enhe(peer
))
942 stream_put_prefix_addpath(s
, &rn
->p
, addpath_encode
,
946 prd
= (struct prefix_rd
*)&rn
->prn
->p
;
948 /* If first time, format the MP_UNREACH header */
950 attrlen_pos
= stream_get_endp(s
);
951 /* total attr length = 0 for now. reevaluate
954 mp_start
= stream_get_endp(s
);
955 mplen_pos
= bgp_packet_mpunreach_start(s
, afi
,
959 bgp_packet_mpunreach_prefix(s
, &rn
->p
, afi
, safi
, prd
,
960 NULL
, addpath_encode
,
961 addpath_tx_id
, NULL
);
966 if (bgp_debug_update(NULL
, &rn
->p
, subgrp
->update_group
, 0)) {
967 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
969 zlog_debug("u%" PRIu64
":s%" PRIu64
970 " send UPDATE %s -- unreachable",
971 subgrp
->update_group
->id
, subgrp
->id
,
972 bgp_debug_rdpfxpath2str(
973 prd
, &rn
->p
, addpath_encode
,
974 addpath_tx_id
, pfx_buf
,
980 bgp_adj_out_remove_subgroup(rn
, adj
, subgrp
);
984 if (!stream_empty(s
)) {
985 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
986 && !peer_cap_enhe(peer
)) {
987 unfeasible_len
= stream_get_endp(s
) - BGP_HEADER_SIZE
988 - BGP_UNFEASIBLE_LEN
;
989 stream_putw_at(s
, BGP_HEADER_SIZE
, unfeasible_len
);
992 /* Set the mp_unreach attr's length */
993 bgp_packet_mpunreach_end(s
, mplen_pos
);
995 /* Set total path attribute length. */
996 total_attr_len
= stream_get_endp(s
) - mp_start
;
997 stream_putw_at(s
, attrlen_pos
, total_attr_len
);
999 bgp_packet_set_size(s
);
1000 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
1001 zlog_debug("u%" PRIu64
":s%" PRIu64
1002 " UPDATE (withdraw) len %zd numpfx %d",
1003 subgrp
->update_group
->id
, subgrp
->id
,
1004 (stream_get_endp(s
) - stream_get_getp(s
)),
1006 pkt
= bpacket_queue_add(SUBGRP_PKTQ(subgrp
), stream_dup(s
),
1015 void subgroup_default_update_packet(struct update_subgroup
*subgrp
,
1016 struct attr
*attr
, struct peer
*from
)
1022 bgp_size_t total_attr_len
;
1025 struct bpacket_attr_vec_arr vecarr
;
1026 int addpath_encode
= 0;
1028 if (DISABLE_BGP_ANNOUNCE
)
1034 peer
= SUBGRP_PEER(subgrp
);
1035 afi
= SUBGRP_AFI(subgrp
);
1036 safi
= SUBGRP_SAFI(subgrp
);
1037 bpacket_attr_vec_arr_reset(&vecarr
);
1038 addpath_encode
= bgp_addpath_encode_tx(peer
, afi
, safi
);
1041 str2prefix("0.0.0.0/0", &p
);
1043 str2prefix("::/0", &p
);
1045 /* Logging the attribute. */
1046 if (bgp_debug_update(NULL
, &p
, subgrp
->update_group
, 0)) {
1047 char attrstr
[BUFSIZ
];
1048 char buf
[PREFIX_STRLEN
];
1049 /* ' with addpath ID ' 17
1050 * max strlen of uint32 + 10
1051 * +/- (in case of idiocy) + 1
1052 * null terminator + 1
1053 * ============================ 29 */
1058 bgp_dump_attr(peer
, attr
, attrstr
, BUFSIZ
);
1061 snprintf(tx_id_buf
, sizeof(tx_id_buf
),
1062 " with addpath ID %u",
1063 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1065 zlog_debug("u%" PRIu64
":s%" PRIu64
" send UPDATE %s%s %s",
1066 (SUBGRP_UPDGRP(subgrp
))->id
, subgrp
->id
,
1067 prefix2str(&p
, buf
, sizeof(buf
)), tx_id_buf
,
1071 s
= stream_new(BGP_MAX_PACKET_SIZE
);
1073 /* Make BGP update packet. */
1074 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
1076 /* Unfeasible Routes Length. */
1079 /* Make place for total attribute length. */
1080 pos
= stream_get_endp(s
);
1082 total_attr_len
= bgp_packet_attribute(
1083 NULL
, peer
, s
, attr
, &vecarr
, &p
, afi
, safi
, from
, NULL
, NULL
,
1084 addpath_encode
, BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1086 /* Set Total Path Attribute Length. */
1087 stream_putw_at(s
, pos
, total_attr_len
);
1090 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
&& !peer_cap_enhe(peer
))
1091 stream_put_prefix_addpath(
1092 s
, &p
, addpath_encode
,
1093 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1096 bgp_packet_set_size(s
);
1098 (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp
), s
, &vecarr
);
1099 subgroup_trigger_write(subgrp
);
1102 void subgroup_default_withdraw_packet(struct update_subgroup
*subgrp
)
1107 unsigned long attrlen_pos
= 0;
1109 bgp_size_t unfeasible_len
;
1110 bgp_size_t total_attr_len
= 0;
1111 size_t mp_start
= 0;
1112 size_t mplen_pos
= 0;
1115 int addpath_encode
= 0;
1117 if (DISABLE_BGP_ANNOUNCE
)
1120 peer
= SUBGRP_PEER(subgrp
);
1121 afi
= SUBGRP_AFI(subgrp
);
1122 safi
= SUBGRP_SAFI(subgrp
);
1123 addpath_encode
= bgp_addpath_encode_tx(peer
, afi
, safi
);
1126 str2prefix("0.0.0.0/0", &p
);
1128 str2prefix("::/0", &p
);
1130 if (bgp_debug_update(NULL
, &p
, subgrp
->update_group
, 0)) {
1131 char buf
[PREFIX_STRLEN
];
1132 /* ' with addpath ID ' 17
1133 * max strlen of uint32 + 10
1134 * +/- (in case of idiocy) + 1
1135 * null terminator + 1
1136 * ============================ 29 */
1140 snprintf(tx_id_buf
, sizeof(tx_id_buf
),
1141 " with addpath ID %u",
1142 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1144 zlog_debug("u%" PRIu64
":s%" PRIu64
1145 " send UPDATE %s%s -- unreachable",
1146 (SUBGRP_UPDGRP(subgrp
))->id
, subgrp
->id
,
1147 prefix2str(&p
, buf
, sizeof(buf
)), tx_id_buf
);
1150 s
= stream_new(BGP_MAX_PACKET_SIZE
);
1152 /* Make BGP update packet. */
1153 bgp_packet_set_marker(s
, BGP_MSG_UPDATE
);
1155 /* Unfeasible Routes Length. */;
1156 cp
= stream_get_endp(s
);
1159 /* Withdrawn Routes. */
1160 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
1161 && !peer_cap_enhe(peer
)) {
1162 stream_put_prefix_addpath(
1163 s
, &p
, addpath_encode
,
1164 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1166 unfeasible_len
= stream_get_endp(s
) - cp
- 2;
1168 /* Set unfeasible len. */
1169 stream_putw_at(s
, cp
, unfeasible_len
);
1171 /* Set total path attribute length. */
1174 attrlen_pos
= stream_get_endp(s
);
1176 mp_start
= stream_get_endp(s
);
1177 mplen_pos
= bgp_packet_mpunreach_start(s
, afi
, safi
);
1178 bgp_packet_mpunreach_prefix(
1179 s
, &p
, afi
, safi
, NULL
, NULL
, addpath_encode
,
1180 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
, NULL
);
1182 /* Set the mp_unreach attr's length */
1183 bgp_packet_mpunreach_end(s
, mplen_pos
);
1185 /* Set total path attribute length. */
1186 total_attr_len
= stream_get_endp(s
) - mp_start
;
1187 stream_putw_at(s
, attrlen_pos
, total_attr_len
);
1190 bgp_packet_set_size(s
);
1192 (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp
), s
, NULL
);
1193 subgroup_trigger_write(subgrp
);
1197 bpacket_vec_arr_inherit_attr_flags(struct bpacket_attr_vec_arr
*vecarr
,
1198 bpacket_attr_vec_type type
,
1201 if (CHECK_FLAG(attr
->rmap_change_flags
,
1202 BATTR_RMAP_NEXTHOP_PEER_ADDRESS
))
1203 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1204 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
);
1206 if (CHECK_FLAG(attr
->rmap_change_flags
, BATTR_REFLECTED
))
1207 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1208 BPKT_ATTRVEC_FLAGS_REFLECTED
);
1210 if (CHECK_FLAG(attr
->rmap_change_flags
, BATTR_RMAP_NEXTHOP_UNCHANGED
))
1211 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1212 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
);
1214 if (CHECK_FLAG(attr
->rmap_change_flags
, BATTR_RMAP_IPV4_NHOP_CHANGED
))
1215 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1216 BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED
);
1218 if (CHECK_FLAG(attr
->rmap_change_flags
,
1219 BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED
))
1220 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1221 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED
);
1223 if (CHECK_FLAG(attr
->rmap_change_flags
,
1224 BATTR_RMAP_IPV6_LL_NHOP_CHANGED
))
1225 SET_FLAG(vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1226 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_LNH_CHANGED
);
1229 /* Reset the Attributes vector array. The vector array is used to override
1230 * certain output parameters in the packet for a particular peer
1232 void bpacket_attr_vec_arr_reset(struct bpacket_attr_vec_arr
*vecarr
)
1240 while (i
< BGP_ATTR_VEC_MAX
) {
1241 vecarr
->entries
[i
].flags
= 0;
1242 vecarr
->entries
[i
].offset
= 0;
1247 /* Setup a particular node entry in the vecarr */
1248 void bpacket_attr_vec_arr_set_vec(struct bpacket_attr_vec_arr
*vecarr
,
1249 bpacket_attr_vec_type type
, struct stream
*s
,
1254 assert(type
< BGP_ATTR_VEC_MAX
);
1256 SET_FLAG(vecarr
->entries
[type
].flags
, BPKT_ATTRVEC_FLAGS_UPDATED
);
1257 vecarr
->entries
[type
].offset
= stream_get_endp(s
);
1259 bpacket_vec_arr_inherit_attr_flags(vecarr
, type
, attr
);