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"
72 (struct bpacket
*) XCALLOC (MTYPE_BGP_PACKET
, sizeof (struct bpacket
));
78 bpacket_free (struct bpacket
*pkt
)
81 stream_free (pkt
->buffer
);
83 XFREE (MTYPE_BGP_PACKET
, pkt
);
87 bpacket_queue_init (struct bpacket_queue
*q
)
89 TAILQ_INIT (&(q
->pkts
));
93 * bpacket_queue_sanity_check
96 bpacket_queue_sanity_check (struct bpacket_queue
__attribute__ ((__unused__
)) *q
)
101 pkt
= bpacket_queue_last (q
);
103 assert (!pkt
->buffer
);
106 * Make sure the count of packets is correct.
110 pkt
= bpacket_queue_first (q
);
115 if (num_pkts
> q
->curr_count
)
118 pkt
= TAILQ_NEXT (pkt
, pkt_train
);
121 assert (num_pkts
== q
->curr_count
);
126 * bpacket_queue_add_packet
128 * Internal function of bpacket_queue - and adds a
129 * packet entry to the end of the list.
131 * Users of bpacket_queue should use bpacket_queue_add instead.
134 bpacket_queue_add_packet (struct bpacket_queue
*q
, struct bpacket
*pkt
)
136 struct bpacket
*last_pkt
;
138 if (TAILQ_EMPTY (&(q
->pkts
)))
139 TAILQ_INSERT_TAIL (&(q
->pkts
), pkt
, pkt_train
);
142 last_pkt
= bpacket_queue_last (q
);
143 TAILQ_INSERT_AFTER (&(q
->pkts
), last_pkt
, pkt
, pkt_train
);
146 if (q
->hwm_count
< q
->curr_count
)
147 q
->hwm_count
= q
->curr_count
;
151 * Adds a packet to the bpacket_queue.
153 * The stream passed is consumed by this function. So, the caller should
154 * not free or use the stream after
155 * invoking this function.
158 bpacket_queue_add (struct bpacket_queue
*q
, struct stream
*s
,
159 struct bpacket_attr_vec_arr
*vecarrp
)
162 struct bpacket
*last_pkt
;
165 pkt
= bpacket_alloc ();
166 if (TAILQ_EMPTY (&(q
->pkts
)))
171 memcpy (&pkt
->arr
, vecarrp
, sizeof (struct bpacket_attr_vec_arr
));
173 bpacket_attr_vec_arr_reset (&pkt
->arr
);
174 bpacket_queue_add_packet (q
, pkt
);
175 bpacket_queue_sanity_check (q
);
180 * Fill in the new information into the current sentinel and create a
183 bpacket_queue_sanity_check (q
);
184 last_pkt
= bpacket_queue_last (q
);
185 assert (last_pkt
->buffer
== NULL
);
186 last_pkt
->buffer
= s
;
188 memcpy (&last_pkt
->arr
, vecarrp
, sizeof (struct bpacket_attr_vec_arr
));
190 bpacket_attr_vec_arr_reset (&last_pkt
->arr
);
192 pkt
->ver
= last_pkt
->ver
;
194 bpacket_queue_add_packet (q
, pkt
);
196 bpacket_queue_sanity_check (q
);
201 bpacket_queue_first (struct bpacket_queue
*q
)
203 return (TAILQ_FIRST (&(q
->pkts
)));
207 bpacket_queue_last (struct bpacket_queue
*q
)
209 return TAILQ_LAST (&(q
->pkts
), pkt_queue
);
213 bpacket_queue_remove (struct bpacket_queue
*q
)
215 struct bpacket
*first
;
217 first
= bpacket_queue_first (q
);
220 TAILQ_REMOVE (&(q
->pkts
), first
, pkt_train
);
227 bpacket_queue_length (struct bpacket_queue
*q
)
229 return q
->curr_count
- 1;
233 bpacket_queue_hwm_length (struct bpacket_queue
*q
)
235 return q
->hwm_count
- 1;
239 bpacket_queue_is_full (struct bgp
*bgp
, struct bpacket_queue
*q
)
241 if (q
->curr_count
>= bgp
->default_subgroup_pkt_queue_max
)
247 bpacket_add_peer (struct bpacket
*pkt
, struct peer_af
*paf
)
252 LIST_INSERT_HEAD (&(pkt
->peers
), paf
, pkt_train
);
253 paf
->next_pkt_to_send
= pkt
;
257 * bpacket_queue_cleanup
260 bpacket_queue_cleanup (struct bpacket_queue
*q
)
264 while ((pkt
= bpacket_queue_remove (q
)))
271 * bpacket_queue_compact
273 * Delete packets that do not need to be transmitted to any peer from
276 * @return the number of packets deleted.
279 bpacket_queue_compact (struct bpacket_queue
*q
)
282 struct bpacket
*pkt
, *removed_pkt
;
288 pkt
= bpacket_queue_first (q
);
293 * Don't delete the sentinel.
298 if (!LIST_EMPTY (&(pkt
->peers
)))
301 removed_pkt
= bpacket_queue_remove (q
);
302 assert (pkt
== removed_pkt
);
303 bpacket_free (removed_pkt
);
308 bpacket_queue_sanity_check (q
);
313 bpacket_queue_advance_peer (struct peer_af
*paf
)
316 struct bpacket
*old_pkt
;
318 old_pkt
= paf
->next_pkt_to_send
;
319 if (old_pkt
->buffer
== NULL
)
320 /* Already at end of list */
323 LIST_REMOVE (paf
, pkt_train
);
324 pkt
= TAILQ_NEXT (old_pkt
, pkt_train
);
325 bpacket_add_peer (pkt
, paf
);
327 if (!bpacket_queue_compact (PAF_PKTQ (paf
)))
331 * Deleted one or more packets. Check if we can now merge this
332 * peer's subgroup into another subgroup.
334 update_subgroup_check_merge (paf
->subgroup
, "advanced peer in queue");
338 * bpacket_queue_remove_peer
340 * Remove the peer from the packet queue of the subgroup it belongs
344 bpacket_queue_remove_peer (struct peer_af
*paf
)
346 struct bpacket_queue
*q
;
353 LIST_REMOVE (paf
, pkt_train
);
354 paf
->next_pkt_to_send
= NULL
;
356 bpacket_queue_compact (q
);
360 bpacket_queue_virtual_length (struct peer_af
*paf
)
363 struct bpacket
*last
;
364 struct bpacket_queue
*q
;
366 pkt
= paf
->next_pkt_to_send
;
367 if (!pkt
|| (pkt
->buffer
== NULL
))
368 /* Already at end of list */
372 if (TAILQ_EMPTY (&(q
->pkts
)))
375 last
= TAILQ_LAST (&(q
->pkts
), pkt_queue
);
376 if (last
->ver
>= pkt
->ver
)
377 return last
->ver
- pkt
->ver
;
379 /* sequence # rolled over */
380 return (UINT_MAX
- pkt
->ver
+ 1) + last
->ver
;
384 * Dump the bpacket queue
387 bpacket_queue_show_vty (struct bpacket_queue
*q
, struct vty
*vty
)
392 pkt
= bpacket_queue_first (q
);
395 vty_out (vty
, " Packet %p ver %u buffer %p\n", pkt
, pkt
->ver
,
398 LIST_FOREACH (paf
, &(pkt
->peers
), pkt_train
)
400 vty_out (vty
, " - %s\n", paf
->peer
->host
);
402 pkt
= bpacket_next (pkt
);
408 bpacket_reformat_for_peer (struct bpacket
*pkt
, struct peer_af
*paf
)
410 struct stream
*s
= NULL
;
411 bpacket_attr_vec
*vec
;
416 s
= stream_dup (pkt
->buffer
);
417 peer
= PAF_PEER(paf
);
419 vec
= &pkt
->arr
.entries
[BGP_ATTR_VEC_NH
];
420 if (CHECK_FLAG (vec
->flags
, BPKT_ATTRVEC_FLAGS_UPDATED
))
423 afi_t nhafi
= AFI_MAX
; /* NH AFI is based on nhlen! */
424 int route_map_sets_nh
;
425 nhlen
= stream_getc_from (s
, vec
->offset
);
426 if (peer_cap_enhe(peer
, paf
->afi
, paf
->safi
))
429 nhafi
= BGP_NEXTHOP_AFI_FROM_NHLEN(nhlen
);
433 struct in_addr v4nh
, *mod_v4nh
;
435 size_t offset_nh
= vec
->offset
+ 1;
438 (CHECK_FLAG (vec
->flags
, BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED
) ||
439 CHECK_FLAG (vec
->flags
, BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
));
443 case BGP_ATTR_NHLEN_IPV4
:
445 case BGP_ATTR_NHLEN_VPNV4
:
449 /* TODO: handle IPv6 nexthops */
450 zlog_warn ("%s: %s: invalid MP nexthop length (AFI IP): %u",
451 __func__
, peer
->host
, nhlen
);
456 stream_get_from (&v4nh
, s
, offset_nh
, IPV4_MAX_BYTELEN
);
460 * If route-map has set the nexthop, that is always used; if it is
461 * specified as peer-address, the peering address is picked up.
462 * Otherwise, if NH is unavailable from attribute, the peering addr
463 * is picked up; the "NH unavailable" case also covers next-hop-self
464 * and some other scenarios -- see subgroup_announce_check(). In
465 * all other cases, use the nexthop carried in the attribute unless
466 * it is EBGP non-multiaccess and there is no next-hop-unchanged setting.
467 * Note: It is assumed route-map cannot set the nexthop to an
470 if (route_map_sets_nh
)
472 if (CHECK_FLAG(vec
->flags
,
473 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
))
475 mod_v4nh
= &peer
->nexthop
.v4
;
479 else if (!v4nh
.s_addr
)
481 mod_v4nh
= &peer
->nexthop
.v4
;
484 else if (peer
->sort
== BGP_PEER_EBGP
&&
485 paf
->safi
!= SAFI_EVPN
&&
486 (bgp_multiaccess_check_v4 (v4nh
, peer
) == 0) &&
487 !CHECK_FLAG(vec
->flags
,
488 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
) &&
489 !peer_af_flag_check (peer
, nhafi
, paf
->safi
,
490 PEER_FLAG_NEXTHOP_UNCHANGED
))
492 /* NOTE: not handling case where NH has new AFI */
493 mod_v4nh
= &peer
->nexthop
.v4
;
497 if (nh_modified
) /* allow for VPN RD */
498 stream_put_in_addr_at (s
, offset_nh
, mod_v4nh
);
500 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
501 zlog_debug ("u%" PRIu64
":s%" PRIu64
" %s send UPDATE w/ nexthop %s%s",
502 PAF_SUBGRP(paf
)->update_group
->id
, PAF_SUBGRP(paf
)->id
,
503 peer
->host
, inet_ntoa (*mod_v4nh
),
504 (nhlen
== 12 ? " and RD" : ""));
506 else if (nhafi
== AFI_IP6
)
508 struct in6_addr v6nhglobal
, *mod_v6nhg
;
509 struct in6_addr v6nhlocal
, *mod_v6nhl
;
510 int gnh_modified
, lnh_modified
;
511 size_t offset_nhglobal
= vec
->offset
+ 1;
512 size_t offset_nhlocal
= vec
->offset
+ 1;
514 gnh_modified
= lnh_modified
= 0;
515 mod_v6nhg
= &v6nhglobal
;
516 mod_v6nhl
= &v6nhlocal
;
519 (CHECK_FLAG (vec
->flags
, BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED
) ||
520 CHECK_FLAG (vec
->flags
, BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
));
523 * The logic here is rather similar to that for IPv4, the
524 * additional work being to handle 1 or 2 nexthops. Also, 3rd
525 * party nexthop is not propagated for EBGP right now.
529 case BGP_ATTR_NHLEN_IPV6_GLOBAL
:
531 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
:
532 offset_nhlocal
+= IPV6_MAX_BYTELEN
;
534 case BGP_ATTR_NHLEN_VPNV6_GLOBAL
:
535 offset_nhglobal
+= 8;
537 case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
:
538 offset_nhglobal
+= 8;
539 offset_nhlocal
+= 8 * 2 + IPV6_MAX_BYTELEN
;
542 /* TODO: handle IPv4 nexthops */
543 zlog_warn ("%s: %s: invalid MP nexthop length (AFI IP6): %u",
544 __func__
, peer
->host
, nhlen
);
549 stream_get_from (&v6nhglobal
, s
, offset_nhglobal
, IPV6_MAX_BYTELEN
);
550 if (route_map_sets_nh
)
552 if (CHECK_FLAG(vec
->flags
,
553 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
))
555 mod_v6nhg
= &peer
->nexthop
.v6_global
;
559 else if (IN6_IS_ADDR_UNSPECIFIED (&v6nhglobal
))
561 mod_v6nhg
= &peer
->nexthop
.v6_global
;
564 else if (peer
->sort
== BGP_PEER_EBGP
&&
565 !CHECK_FLAG(vec
->flags
,
566 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
) &&
567 !peer_af_flag_check (peer
, nhafi
, paf
->safi
,
568 PEER_FLAG_NEXTHOP_UNCHANGED
))
570 /* NOTE: not handling case where NH has new AFI */
571 mod_v6nhg
= &peer
->nexthop
.v6_global
;
576 if (nhlen
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
||
577 nhlen
== BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
)
579 stream_get_from (&v6nhlocal
, s
, offset_nhlocal
, IPV6_MAX_BYTELEN
);
580 if (IN6_IS_ADDR_UNSPECIFIED (&v6nhlocal
))
582 mod_v6nhl
= &peer
->nexthop
.v6_local
;
588 stream_put_in6_addr_at (s
, offset_nhglobal
, mod_v6nhg
);
590 stream_put_in6_addr_at (s
, offset_nhlocal
, mod_v6nhl
);
592 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
594 if (nhlen
== 32 || nhlen
== 48)
595 zlog_debug ("u%" PRIu64
":s%" PRIu64
" %s send UPDATE w/ mp_nexthops %s, %s%s",
596 PAF_SUBGRP(paf
)->update_group
->id
,
599 inet_ntop (AF_INET6
, mod_v6nhg
, buf
, BUFSIZ
),
600 inet_ntop (AF_INET6
, mod_v6nhl
, buf2
, BUFSIZ
),
601 (nhlen
== 48 ? " and RD" : ""));
603 zlog_debug ("u%" PRIu64
":s%" PRIu64
" %s send UPDATE w/ mp_nexthop %s%s",
604 PAF_SUBGRP(paf
)->update_group
->id
,
607 inet_ntop (AF_INET6
, mod_v6nhg
, buf
, BUFSIZ
),
608 (nhlen
== 24 ? " and RD" : ""));
611 else if (paf
->afi
== AFI_L2VPN
)
613 struct in_addr v4nh
, *mod_v4nh
;
616 stream_get_from (&v4nh
, s
, vec
->offset
+ 1, 4);
619 /* No route-map changes allowed for EVPN nexthops. */
622 mod_v4nh
= &peer
->nexthop
.v4
;
627 stream_put_in_addr_at (s
, vec
->offset
+ 1, mod_v4nh
);
629 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
630 zlog_debug ("u%" PRIu64
":s%" PRIu64
" %s send UPDATE w/ nexthop %s",
631 PAF_SUBGRP(paf
)->update_group
->id
, PAF_SUBGRP(paf
)->id
,
632 peer
->host
, inet_ntoa (*mod_v4nh
));
637 bgp_packet_add (peer
, s
);
642 * Update the vecarr offsets to go beyond 'pos' bytes, i.e. add 'pos'
646 bpacket_attr_vec_arr_update (struct bpacket_attr_vec_arr
*vecarr
, size_t pos
)
653 for (i
= 0; i
< BGP_ATTR_VEC_MAX
; i
++)
654 vecarr
->entries
[i
].offset
+= pos
;
658 * Return if there are packets to build for this subgroup.
661 subgroup_packets_to_build (struct update_subgroup
*subgrp
)
663 struct bgp_advertise
*adv
;
668 adv
= BGP_ADV_FIFO_HEAD (&subgrp
->sync
->withdraw
);
672 adv
= BGP_ADV_FIFO_HEAD (&subgrp
->sync
->update
);
679 /* Make BGP update packet. */
681 subgroup_update_packet (struct update_subgroup
*subgrp
)
683 struct bpacket_attr_vec_arr vecarr
;
687 struct stream
*snlri
;
688 struct stream
*packet
;
689 struct bgp_adj_out
*adj
;
690 struct bgp_advertise
*adv
;
691 struct bgp_node
*rn
= NULL
;
692 struct bgp_info
*binfo
= NULL
;
693 bgp_size_t total_attr_len
= 0;
694 unsigned long attrlen_pos
= 0;
695 size_t mpattrlen_pos
= 0;
696 size_t mpattr_pos
= 0;
699 int space_remaining
= 0;
700 int space_needed
= 0;
701 char send_attr_str
[BUFSIZ
];
702 int send_attr_printed
= 0;
704 int addpath_encode
= 0;
705 u_int32_t addpath_tx_id
= 0;
706 struct prefix_rd
*prd
= NULL
;
707 mpls_label_t label
= MPLS_INVALID_LABEL
;
712 if (bpacket_queue_is_full (SUBGRP_INST (subgrp
), SUBGRP_PKTQ (subgrp
)))
715 peer
= SUBGRP_PEER (subgrp
);
716 afi
= SUBGRP_AFI (subgrp
);
717 safi
= SUBGRP_SAFI (subgrp
);
720 snlri
= subgrp
->scratch
;
721 stream_reset (snlri
);
723 bpacket_attr_vec_arr_reset (&vecarr
);
725 addpath_encode
= bgp_addpath_encode_tx (peer
, afi
, safi
);
727 adv
= BGP_ADV_FIFO_HEAD (&subgrp
->sync
->update
);
733 addpath_tx_id
= adj
->addpath_tx_id
;
736 space_remaining
= STREAM_CONCAT_REMAIN (s
, snlri
, STREAM_SIZE(s
)) -
737 BGP_MAX_PACKET_SIZE_OVERFLOW
;
738 space_needed
= BGP_NLRI_LENGTH
+
739 bgp_packet_mpattr_prefix_size (afi
, safi
, &rn
->p
);
741 /* When remaining space can't include NLRI and it's length. */
742 if (space_remaining
< space_needed
)
745 /* If packet is empty, set attribute. */
746 if (stream_empty (s
))
748 struct peer
*from
= NULL
;
753 /* 1: Write the BGP message header - 16 bytes marker, 2 bytes length,
754 * one byte message type.
756 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
758 /* 2: withdrawn routes length */
761 /* 3: total attributes length - attrlen_pos stores the position */
762 attrlen_pos
= stream_get_endp (s
);
765 /* 4: if there is MP_REACH_NLRI attribute, that should be the first
766 * attribute, according to draft-ietf-idr-error-handling. Save the
769 mpattr_pos
= stream_get_endp (s
);
771 /* 5: Encode all the attributes, except MP_REACH_NLRI attr. */
772 total_attr_len
= bgp_packet_attribute (NULL
, peer
, s
,
773 adv
->baa
->attr
, &vecarr
,
775 from
, NULL
, NULL
, 0, 0);
777 space_remaining
= STREAM_CONCAT_REMAIN (s
, snlri
, STREAM_SIZE(s
)) -
778 BGP_MAX_PACKET_SIZE_OVERFLOW
;
779 space_needed
= BGP_NLRI_LENGTH
+
780 bgp_packet_mpattr_prefix_size (afi
, safi
, &rn
->p
);
782 /* If the attributes alone do not leave any room for NLRI then
784 if (space_remaining
< space_needed
)
786 zlog_err ("u%" PRIu64
":s%" PRIu64
" attributes too long, cannot send UPDATE",
787 subgrp
->update_group
->id
, subgrp
->id
);
789 /* Flush the FIFO update queue */
791 adv
= bgp_advertise_clean_subgroup (subgrp
, adj
);
795 if (BGP_DEBUG (update
, UPDATE_OUT
) ||
796 BGP_DEBUG (update
, UPDATE_PREFIX
))
798 memset (send_attr_str
, 0, BUFSIZ
);
799 send_attr_printed
= 0;
800 bgp_dump_attr (adv
->baa
->attr
, send_attr_str
, BUFSIZ
);
804 if ((afi
== AFI_IP
&& safi
== SAFI_UNICAST
) &&
805 !peer_cap_enhe(peer
, afi
, safi
))
806 stream_put_prefix_addpath (s
, &rn
->p
, addpath_encode
, addpath_tx_id
);
809 /* Encode the prefix in MP_REACH_NLRI attribute */
811 prd
= (struct prefix_rd
*) &rn
->prn
->p
;
813 if (safi
== SAFI_LABELED_UNICAST
)
814 label
= bgp_adv_label(rn
, binfo
, peer
, afi
, safi
);
816 if (binfo
&& binfo
->extra
)
817 label
= binfo
->extra
->label
;
819 if (stream_empty (snlri
))
820 mpattrlen_pos
= bgp_packet_mpattr_start (snlri
, peer
, afi
, safi
,
821 &vecarr
, adv
->baa
->attr
);
823 bgp_packet_mpattr_prefix (snlri
, afi
, safi
, &rn
->p
, prd
,
824 &label
, addpath_encode
, addpath_tx_id
, adv
->baa
->attr
);
829 if (bgp_debug_update(NULL
, &rn
->p
, subgrp
->update_group
, 0))
831 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
833 if (!send_attr_printed
)
835 zlog_debug ("u%" PRIu64
":s%" PRIu64
" send UPDATE w/ attr: %s",
836 subgrp
->update_group
->id
, subgrp
->id
, send_attr_str
);
837 if (!stream_empty (snlri
))
842 pkt_afi
= afi_int2iana (afi
);
843 pkt_safi
= safi_int2iana (safi
);
844 zlog_debug ("u%" PRIu64
":s%" PRIu64
" send MP_REACH for afi/safi %d/%d",
845 subgrp
->update_group
->id
, subgrp
->id
, pkt_afi
, pkt_safi
);
848 send_attr_printed
= 1;
851 bgp_debug_rdpfxpath2str (afi
, safi
, prd
, &rn
->p
, &label
,
852 addpath_encode
, addpath_tx_id
,
853 pfx_buf
, sizeof (pfx_buf
));
854 zlog_debug ("u%" PRIu64
":s%" PRIu64
" send UPDATE %s",
855 subgrp
->update_group
->id
, subgrp
->id
, pfx_buf
);
858 /* Synchnorize attribute. */
860 bgp_attr_unintern (&adj
->attr
);
864 adj
->attr
= bgp_attr_intern (adv
->baa
->attr
);
866 adv
= bgp_advertise_clean_subgroup (subgrp
, adj
);
869 if (!stream_empty (s
))
871 if (!stream_empty (snlri
))
873 bgp_packet_mpattr_end (snlri
, mpattrlen_pos
);
874 total_attr_len
+= stream_get_endp (snlri
);
877 /* set the total attribute length correctly */
878 stream_putw_at (s
, attrlen_pos
, total_attr_len
);
880 if (!stream_empty (snlri
))
882 packet
= stream_dupcat (s
, snlri
, mpattr_pos
);
883 bpacket_attr_vec_arr_update (&vecarr
, mpattr_pos
);
886 packet
= stream_dup (s
);
887 bgp_packet_set_size (packet
);
888 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
889 zlog_debug ("u%" PRIu64
":s%" PRIu64
" send UPDATE len %zd numpfx %d",
890 subgrp
->update_group
->id
, subgrp
->id
,
891 (stream_get_endp(packet
) - stream_get_getp(packet
)), num_pfx
);
892 pkt
= bpacket_queue_add (SUBGRP_PKTQ (subgrp
), packet
, &vecarr
);
894 stream_reset (snlri
);
900 /* Make BGP withdraw packet. */
902 16-octet marker | 2-octet length | 1-octet type |
903 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0)
905 /* For other afi/safis:
906 16-octet marker | 2-octet length | 1-octet type |
907 2-octet withdrawn route length (=0) | 2-octet attrlen |
908 mp_unreach attr type | attr len | afi | safi | withdrawn prefixes
911 subgroup_withdraw_packet (struct update_subgroup
*subgrp
)
915 struct bgp_adj_out
*adj
;
916 struct bgp_advertise
*adv
;
919 bgp_size_t unfeasible_len
;
920 bgp_size_t total_attr_len
;
922 size_t attrlen_pos
= 0;
923 size_t mplen_pos
= 0;
924 u_char first_time
= 1;
927 int space_remaining
= 0;
928 int space_needed
= 0;
930 int addpath_encode
= 0;
931 u_int32_t addpath_tx_id
= 0;
932 struct prefix_rd
*prd
= NULL
;
938 if (bpacket_queue_is_full (SUBGRP_INST (subgrp
), SUBGRP_PKTQ (subgrp
)))
941 peer
= SUBGRP_PEER (subgrp
);
942 afi
= SUBGRP_AFI (subgrp
);
943 safi
= SUBGRP_SAFI (subgrp
);
946 addpath_encode
= bgp_addpath_encode_tx (peer
, afi
, safi
);
948 while ((adv
= BGP_ADV_FIFO_HEAD (&subgrp
->sync
->withdraw
)) != NULL
)
953 addpath_tx_id
= adj
->addpath_tx_id
;
955 space_remaining
= STREAM_REMAIN (s
) -
956 BGP_MAX_PACKET_SIZE_OVERFLOW
;
957 space_needed
= BGP_NLRI_LENGTH
+ BGP_TOTAL_ATTR_LEN
+
958 bgp_packet_mpattr_prefix_size (afi
, safi
, &rn
->p
);
960 if (space_remaining
< space_needed
)
963 if (stream_empty (s
))
965 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
966 stream_putw (s
, 0); /* unfeasible routes length */
971 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
&&
972 !peer_cap_enhe(peer
, afi
, safi
))
973 stream_put_prefix_addpath (s
, &rn
->p
, addpath_encode
, addpath_tx_id
);
977 prd
= (struct prefix_rd
*) &rn
->prn
->p
;
979 /* If first time, format the MP_UNREACH header */
985 pkt_afi
= afi_int2iana (afi
);
986 pkt_safi
= safi_int2iana (safi
);
988 attrlen_pos
= stream_get_endp (s
);
989 /* total attr length = 0 for now. reevaluate later */
991 mp_start
= stream_get_endp (s
);
992 mplen_pos
= bgp_packet_mpunreach_start (s
, afi
, safi
);
993 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
994 zlog_debug ("u%" PRIu64
":s%" PRIu64
" send MP_UNREACH for afi/safi %d/%d",
995 subgrp
->update_group
->id
, subgrp
->id
, pkt_afi
, pkt_safi
);
998 bgp_packet_mpunreach_prefix (s
, &rn
->p
, afi
, safi
, prd
, NULL
,
999 addpath_encode
, addpath_tx_id
, NULL
);
1004 if (bgp_debug_update(NULL
, &rn
->p
, subgrp
->update_group
, 0))
1006 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
1008 bgp_debug_rdpfxpath2str (afi
, safi
, prd
, &rn
->p
, NULL
,
1009 addpath_encode
, addpath_tx_id
,
1010 pfx_buf
, sizeof (pfx_buf
));
1011 zlog_debug ("u%" PRIu64
":s%" PRIu64
" send UPDATE %s -- unreachable",
1012 subgrp
->update_group
->id
, subgrp
->id
, pfx_buf
);
1017 bgp_adj_out_remove_subgroup (rn
, adj
, subgrp
);
1018 bgp_unlock_node (rn
);
1021 if (!stream_empty (s
))
1023 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
&&
1024 !peer_cap_enhe(peer
, afi
, safi
))
1027 = stream_get_endp (s
) - BGP_HEADER_SIZE
- BGP_UNFEASIBLE_LEN
;
1028 stream_putw_at (s
, BGP_HEADER_SIZE
, unfeasible_len
);
1033 /* Set the mp_unreach attr's length */
1034 bgp_packet_mpunreach_end (s
, mplen_pos
);
1036 /* Set total path attribute length. */
1037 total_attr_len
= stream_get_endp (s
) - mp_start
;
1038 stream_putw_at (s
, attrlen_pos
, total_attr_len
);
1040 bgp_packet_set_size (s
);
1041 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
1042 zlog_debug ("u%" PRIu64
":s%" PRIu64
" send UPDATE (withdraw) len %zd numpfx %d",
1043 subgrp
->update_group
->id
, subgrp
->id
,
1044 (stream_get_endp(s
) - stream_get_getp(s
)), num_pfx
);
1045 pkt
= bpacket_queue_add (SUBGRP_PKTQ (subgrp
), stream_dup (s
), NULL
);
1054 subgroup_default_update_packet (struct update_subgroup
*subgrp
,
1055 struct attr
*attr
, struct peer
*from
)
1061 bgp_size_t total_attr_len
;
1064 struct bpacket_attr_vec_arr vecarr
;
1065 int addpath_encode
= 0;
1067 if (DISABLE_BGP_ANNOUNCE
)
1073 peer
= SUBGRP_PEER (subgrp
);
1074 afi
= SUBGRP_AFI (subgrp
);
1075 safi
= SUBGRP_SAFI (subgrp
);
1076 bpacket_attr_vec_arr_reset (&vecarr
);
1077 addpath_encode
= bgp_addpath_encode_tx (peer
, afi
, safi
);
1080 str2prefix ("0.0.0.0/0", &p
);
1082 str2prefix ("::/0", &p
);
1084 /* Logging the attribute. */
1085 if (bgp_debug_update(NULL
, &p
, subgrp
->update_group
, 0))
1087 char attrstr
[BUFSIZ
];
1088 char buf
[PREFIX_STRLEN
];
1089 /* ' with addpath ID ' 17
1090 * max strlen of uint32 + 10
1091 * +/- (just in case) + 1
1092 * null terminator + 1
1093 * ============================ 29 */
1098 bgp_dump_attr (attr
, attrstr
, BUFSIZ
);
1101 snprintf(tx_id_buf
, sizeof (tx_id_buf
), " with addpath ID %u",
1102 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1104 zlog_debug ("u%" PRIu64
":s%" PRIu64
" send UPDATE %s%s %s",
1105 (SUBGRP_UPDGRP (subgrp
))->id
, subgrp
->id
,
1106 prefix2str (&p
, buf
, sizeof (buf
)),
1107 tx_id_buf
, attrstr
);
1110 s
= stream_new (BGP_MAX_PACKET_SIZE
);
1112 /* Make BGP update packet. */
1113 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
1115 /* Unfeasible Routes Length. */
1118 /* Make place for total attribute length. */
1119 pos
= stream_get_endp (s
);
1121 total_attr_len
= bgp_packet_attribute (NULL
, peer
, s
, attr
, &vecarr
, &p
,
1122 afi
, safi
, from
, NULL
, NULL
,
1124 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1126 /* Set Total Path Attribute Length. */
1127 stream_putw_at (s
, pos
, total_attr_len
);
1130 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
&&
1131 !peer_cap_enhe(peer
, afi
, safi
))
1132 stream_put_prefix_addpath (s
, &p
, addpath_encode
, BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1135 bgp_packet_set_size (s
);
1137 (void) bpacket_queue_add (SUBGRP_PKTQ (subgrp
), s
, &vecarr
);
1138 subgroup_trigger_write(subgrp
);
1142 subgroup_default_withdraw_packet (struct update_subgroup
*subgrp
)
1147 unsigned long attrlen_pos
= 0;
1149 bgp_size_t unfeasible_len
;
1150 bgp_size_t total_attr_len
= 0;
1151 size_t mp_start
= 0;
1152 size_t mplen_pos
= 0;
1155 int addpath_encode
= 0;
1157 if (DISABLE_BGP_ANNOUNCE
)
1160 peer
= SUBGRP_PEER (subgrp
);
1161 afi
= SUBGRP_AFI (subgrp
);
1162 safi
= SUBGRP_SAFI (subgrp
);
1163 addpath_encode
= bgp_addpath_encode_tx (peer
, afi
, safi
);
1166 str2prefix ("0.0.0.0/0", &p
);
1168 str2prefix ("::/0", &p
);
1170 if (bgp_debug_update(NULL
, &p
, subgrp
->update_group
, 0))
1172 char buf
[PREFIX_STRLEN
];
1173 /* ' with addpath ID ' 17
1174 * max strlen of uint32 + 10
1175 * +/- (just in case) + 1
1176 * null terminator + 1
1177 * ============================ 29 */
1181 snprintf(tx_id_buf
, sizeof (tx_id_buf
), " with addpath ID %u",
1182 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1184 zlog_debug ("u%" PRIu64
":s%" PRIu64
" send UPDATE %s%s -- unreachable",
1185 (SUBGRP_UPDGRP (subgrp
))->id
, subgrp
->id
,
1186 prefix2str (&p
, buf
, sizeof (buf
)), tx_id_buf
);
1189 s
= stream_new (BGP_MAX_PACKET_SIZE
);
1191 /* Make BGP update packet. */
1192 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
1194 /* Unfeasible Routes Length. */ ;
1195 cp
= stream_get_endp (s
);
1198 /* Withdrawn Routes. */
1199 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
&&
1200 !peer_cap_enhe(peer
, afi
, safi
))
1202 stream_put_prefix_addpath (s
, &p
, addpath_encode
,
1203 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1205 unfeasible_len
= stream_get_endp (s
) - cp
- 2;
1207 /* Set unfeasible len. */
1208 stream_putw_at (s
, cp
, unfeasible_len
);
1210 /* Set total path attribute length. */
1215 attrlen_pos
= stream_get_endp (s
);
1217 mp_start
= stream_get_endp (s
);
1218 mplen_pos
= bgp_packet_mpunreach_start (s
, afi
, safi
);
1219 bgp_packet_mpunreach_prefix (s
, &p
, afi
, safi
, NULL
, NULL
,
1221 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
, NULL
);
1223 /* Set the mp_unreach attr's length */
1224 bgp_packet_mpunreach_end (s
, mplen_pos
);
1226 /* Set total path attribute length. */
1227 total_attr_len
= stream_get_endp (s
) - mp_start
;
1228 stream_putw_at (s
, attrlen_pos
, total_attr_len
);
1231 bgp_packet_set_size (s
);
1233 (void) bpacket_queue_add (SUBGRP_PKTQ (subgrp
), s
, NULL
);
1234 subgroup_trigger_write(subgrp
);
1238 bpacket_vec_arr_inherit_attr_flags (struct bpacket_attr_vec_arr
*vecarr
,
1239 bpacket_attr_vec_type type
,
1242 if (CHECK_FLAG (attr
->rmap_change_flags
,
1243 BATTR_RMAP_NEXTHOP_PEER_ADDRESS
))
1244 SET_FLAG (vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1245 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
);
1247 if (CHECK_FLAG (attr
->rmap_change_flags
, BATTR_REFLECTED
))
1248 SET_FLAG (vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1249 BPKT_ATTRVEC_FLAGS_REFLECTED
);
1251 if (CHECK_FLAG (attr
->rmap_change_flags
,
1252 BATTR_RMAP_NEXTHOP_UNCHANGED
))
1253 SET_FLAG (vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1254 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
);
1256 if (CHECK_FLAG (attr
->rmap_change_flags
,
1257 BATTR_RMAP_IPV4_NHOP_CHANGED
))
1258 SET_FLAG (vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1259 BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED
);
1261 if (CHECK_FLAG (attr
->rmap_change_flags
,
1262 BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED
))
1263 SET_FLAG (vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1264 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED
);
1266 if (CHECK_FLAG (attr
->rmap_change_flags
,
1267 BATTR_RMAP_IPV6_LL_NHOP_CHANGED
))
1268 SET_FLAG (vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1269 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_LNH_CHANGED
);
1272 /* Reset the Attributes vector array. The vector array is used to override
1273 * certain output parameters in the packet for a particular peer
1276 bpacket_attr_vec_arr_reset (struct bpacket_attr_vec_arr
*vecarr
)
1284 while (i
< BGP_ATTR_VEC_MAX
)
1286 vecarr
->entries
[i
].flags
= 0;
1287 vecarr
->entries
[i
].offset
= 0;
1292 /* Setup a particular node entry in the vecarr */
1294 bpacket_attr_vec_arr_set_vec (struct bpacket_attr_vec_arr
*vecarr
,
1295 bpacket_attr_vec_type type
, struct stream
*s
,
1300 assert (type
< BGP_ATTR_VEC_MAX
);
1302 SET_FLAG (vecarr
->entries
[type
].flags
, BPKT_ATTRVEC_FLAGS_UPDATED
);
1303 vecarr
->entries
[type
].offset
= stream_get_endp (s
);
1305 bpacket_vec_arr_inherit_attr_flags(vecarr
, type
, attr
);