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%s", pkt
, pkt
->ver
,
396 pkt
->buffer
, VTY_NEWLINE
);
398 LIST_FOREACH (paf
, &(pkt
->peers
), pkt_train
)
400 vty_out (vty
, " - %s%s", paf
->peer
->host
, VTY_NEWLINE
);
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 (paf
->afi
== AFI_IP
|| paf
->afi
== AFI_IP6
)
428 nhafi
= BGP_NEXTHOP_AFI_FROM_NHLEN(nhlen
);
429 if (peer_cap_enhe(peer
, paf
->afi
, paf
->safi
))
435 struct in_addr v4nh
, *mod_v4nh
;
437 size_t offset_nh
= vec
->offset
+ 1;
440 (CHECK_FLAG (vec
->flags
, BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED
) ||
441 CHECK_FLAG (vec
->flags
, BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
));
445 case BGP_ATTR_NHLEN_IPV4
:
447 case BGP_ATTR_NHLEN_VPNV4
:
451 /* TODO: handle IPv6 nexthops */
452 zlog_warn ("%s: %s: invalid MP nexthop length (AFI IP): %u",
453 __func__
, peer
->host
, nhlen
);
458 stream_get_from (&v4nh
, s
, offset_nh
, IPV4_MAX_BYTELEN
);
462 * If route-map has set the nexthop, that is always used; if it is
463 * specified as peer-address, the peering address is picked up.
464 * Otherwise, if NH is unavailable from attribute, the peering addr
465 * is picked up; the "NH unavailable" case also covers next-hop-self
466 * and some other scenarios -- see subgroup_announce_check(). In
467 * all other cases, use the nexthop carried in the attribute unless
468 * it is EBGP non-multiaccess and there is no next-hop-unchanged setting.
469 * Note: It is assumed route-map cannot set the nexthop to an
472 if (route_map_sets_nh
)
474 if (CHECK_FLAG(vec
->flags
,
475 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
))
477 mod_v4nh
= &peer
->nexthop
.v4
;
481 else if (!v4nh
.s_addr
)
483 mod_v4nh
= &peer
->nexthop
.v4
;
486 else if (peer
->sort
== BGP_PEER_EBGP
&&
487 (bgp_multiaccess_check_v4 (v4nh
, peer
) == 0) &&
488 !CHECK_FLAG(vec
->flags
,
489 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
) &&
490 !peer_af_flag_check (peer
, nhafi
, paf
->safi
,
491 PEER_FLAG_NEXTHOP_UNCHANGED
))
493 /* NOTE: not handling case where NH has new AFI */
494 mod_v4nh
= &peer
->nexthop
.v4
;
498 if (nh_modified
) /* allow for VPN RD */
499 stream_put_in_addr_at (s
, offset_nh
, mod_v4nh
);
501 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
502 zlog_debug ("u%" PRIu64
":s%" PRIu64
" %s send UPDATE w/ nexthop %s%s",
503 PAF_SUBGRP(paf
)->update_group
->id
, PAF_SUBGRP(paf
)->id
,
504 peer
->host
, inet_ntoa (*mod_v4nh
),
505 (nhlen
== 12 ? " and RD" : ""));
507 else if (nhafi
== AFI_IP6
)
509 struct in6_addr v6nhglobal
, *mod_v6nhg
;
510 struct in6_addr v6nhlocal
, *mod_v6nhl
;
511 int gnh_modified
, lnh_modified
;
512 size_t offset_nhglobal
= vec
->offset
+ 1;
513 size_t offset_nhlocal
= vec
->offset
+ 1;
515 gnh_modified
= lnh_modified
= 0;
516 mod_v6nhg
= &v6nhglobal
;
517 mod_v6nhl
= &v6nhlocal
;
520 (CHECK_FLAG (vec
->flags
, BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED
) ||
521 CHECK_FLAG (vec
->flags
, BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
));
524 * The logic here is rather similar to that for IPv4, the
525 * additional work being to handle 1 or 2 nexthops. Also, 3rd
526 * party nexthop is not propagated for EBGP right now.
530 case BGP_ATTR_NHLEN_IPV6_GLOBAL
:
532 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
:
533 offset_nhlocal
+= IPV6_MAX_BYTELEN
;
535 case BGP_ATTR_NHLEN_VPNV6_GLOBAL
:
536 offset_nhglobal
+= 8;
538 case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
:
539 offset_nhglobal
+= 8;
540 offset_nhlocal
+= 8 * 2 + IPV6_MAX_BYTELEN
;
543 /* TODO: handle IPv4 nexthops */
544 zlog_warn ("%s: %s: invalid MP nexthop length (AFI IP6): %u",
545 __func__
, peer
->host
, nhlen
);
550 stream_get_from (&v6nhglobal
, s
, offset_nhglobal
, IPV6_MAX_BYTELEN
);
551 if (route_map_sets_nh
)
553 if (CHECK_FLAG(vec
->flags
,
554 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
))
556 mod_v6nhg
= &peer
->nexthop
.v6_global
;
560 else if (IN6_IS_ADDR_UNSPECIFIED (&v6nhglobal
))
562 mod_v6nhg
= &peer
->nexthop
.v6_global
;
565 else if (peer
->sort
== BGP_PEER_EBGP
&&
566 !CHECK_FLAG(vec
->flags
,
567 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
) &&
568 !peer_af_flag_check (peer
, nhafi
, paf
->safi
,
569 PEER_FLAG_NEXTHOP_UNCHANGED
))
571 /* NOTE: not handling case where NH has new AFI */
572 mod_v6nhg
= &peer
->nexthop
.v6_global
;
577 if (nhlen
== BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
||
578 nhlen
== BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
)
580 stream_get_from (&v6nhlocal
, s
, offset_nhlocal
, IPV6_MAX_BYTELEN
);
581 if (IN6_IS_ADDR_UNSPECIFIED (&v6nhlocal
))
583 mod_v6nhl
= &peer
->nexthop
.v6_local
;
589 stream_put_in6_addr_at (s
, offset_nhglobal
, mod_v6nhg
);
591 stream_put_in6_addr_at (s
, offset_nhlocal
, mod_v6nhl
);
593 if (bgp_debug_update(peer
, NULL
, NULL
, 0))
595 if (nhlen
== 32 || nhlen
== 48)
596 zlog_debug ("u%" PRIu64
":s%" PRIu64
" %s send UPDATE w/ mp_nexthops %s, %s%s",
597 PAF_SUBGRP(paf
)->update_group
->id
,
600 inet_ntop (AF_INET6
, mod_v6nhg
, buf
, BUFSIZ
),
601 inet_ntop (AF_INET6
, mod_v6nhl
, buf2
, BUFSIZ
),
602 (nhlen
== 48 ? " and RD" : ""));
604 zlog_debug ("u%" PRIu64
":s%" PRIu64
" %s send UPDATE w/ mp_nexthop %s%s",
605 PAF_SUBGRP(paf
)->update_group
->id
,
608 inet_ntop (AF_INET6
, mod_v6nhg
, buf
, BUFSIZ
),
609 (nhlen
== 24 ? " and RD" : ""));
614 bgp_packet_add (peer
, s
);
619 * Update the vecarr offsets to go beyond 'pos' bytes, i.e. add 'pos'
623 bpacket_attr_vec_arr_update (struct bpacket_attr_vec_arr
*vecarr
, size_t pos
)
630 for (i
= 0; i
< BGP_ATTR_VEC_MAX
; i
++)
631 vecarr
->entries
[i
].offset
+= pos
;
635 * Return if there are packets to build for this subgroup.
638 subgroup_packets_to_build (struct update_subgroup
*subgrp
)
640 struct bgp_advertise
*adv
;
645 adv
= BGP_ADV_FIFO_HEAD (&subgrp
->sync
->withdraw
);
649 adv
= BGP_ADV_FIFO_HEAD (&subgrp
->sync
->update
);
656 /* Make BGP update packet. */
658 subgroup_update_packet (struct update_subgroup
*subgrp
)
660 struct bpacket_attr_vec_arr vecarr
;
664 struct stream
*snlri
;
665 struct stream
*packet
;
666 struct bgp_adj_out
*adj
;
667 struct bgp_advertise
*adv
;
668 struct bgp_node
*rn
= NULL
;
669 struct bgp_info
*binfo
= NULL
;
670 bgp_size_t total_attr_len
= 0;
671 unsigned long attrlen_pos
= 0;
672 size_t mpattrlen_pos
= 0;
673 size_t mpattr_pos
= 0;
676 int space_remaining
= 0;
677 int space_needed
= 0;
678 char send_attr_str
[BUFSIZ
];
679 int send_attr_printed
= 0;
681 int addpath_encode
= 0;
682 u_int32_t addpath_tx_id
= 0;
683 struct prefix_rd
*prd
= NULL
;
689 if (bpacket_queue_is_full (SUBGRP_INST (subgrp
), SUBGRP_PKTQ (subgrp
)))
692 peer
= SUBGRP_PEER (subgrp
);
693 afi
= SUBGRP_AFI (subgrp
);
694 safi
= SUBGRP_SAFI (subgrp
);
697 snlri
= subgrp
->scratch
;
698 stream_reset (snlri
);
701 bpacket_attr_vec_arr_reset (&vecarr
);
703 addpath_encode
= bgp_addpath_encode_tx (peer
, afi
, safi
);
705 adv
= BGP_ADV_FIFO_HEAD (&subgrp
->sync
->update
);
711 addpath_tx_id
= adj
->addpath_tx_id
;
714 space_remaining
= STREAM_CONCAT_REMAIN (s
, snlri
, STREAM_SIZE(s
)) -
715 BGP_MAX_PACKET_SIZE_OVERFLOW
;
716 space_needed
= BGP_NLRI_LENGTH
+
717 bgp_packet_mpattr_prefix_size (afi
, safi
, &rn
->p
);
719 /* When remaining space can't include NLRI and it's length. */
720 if (space_remaining
< space_needed
)
723 /* If packet is empty, set attribute. */
724 if (stream_empty (s
))
726 struct peer
*from
= NULL
;
731 /* 1: Write the BGP message header - 16 bytes marker, 2 bytes length,
732 * one byte message type.
734 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
736 /* 2: withdrawn routes length */
739 /* 3: total attributes length - attrlen_pos stores the position */
740 attrlen_pos
= stream_get_endp (s
);
743 /* 4: if there is MP_REACH_NLRI attribute, that should be the first
744 * attribute, according to draft-ietf-idr-error-handling. Save the
747 mpattr_pos
= stream_get_endp (s
);
749 /* 5: Encode all the attributes, except MP_REACH_NLRI attr. */
750 total_attr_len
= bgp_packet_attribute (NULL
, peer
, s
,
751 adv
->baa
->attr
, &vecarr
,
753 from
, NULL
, NULL
, 0, 0);
755 space_remaining
= STREAM_CONCAT_REMAIN (s
, snlri
, STREAM_SIZE(s
)) -
756 BGP_MAX_PACKET_SIZE_OVERFLOW
;
757 space_needed
= BGP_NLRI_LENGTH
+
758 bgp_packet_mpattr_prefix_size (afi
, safi
, &rn
->p
);
760 /* If the attributes alone do not leave any room for NLRI then
762 if (space_remaining
< space_needed
)
764 zlog_err ("u%" PRIu64
":s%" PRIu64
" attributes too long, cannot send UPDATE",
765 subgrp
->update_group
->id
, subgrp
->id
);
767 /* Flush the FIFO update queue */
769 adv
= bgp_advertise_clean_subgroup (subgrp
, adj
);
773 if (BGP_DEBUG (update
, UPDATE_OUT
) ||
774 BGP_DEBUG (update
, UPDATE_PREFIX
))
776 memset (send_attr_str
, 0, BUFSIZ
);
777 send_attr_printed
= 0;
778 bgp_dump_attr (adv
->baa
->attr
, send_attr_str
, BUFSIZ
);
782 if ((afi
== AFI_IP
&& safi
== SAFI_UNICAST
) &&
783 !peer_cap_enhe(peer
, afi
, safi
))
784 stream_put_prefix_addpath (s
, &rn
->p
, addpath_encode
, addpath_tx_id
);
787 /* Encode the prefix in MP_REACH_NLRI attribute */
791 prd
= (struct prefix_rd
*) &rn
->prn
->p
;
793 if (safi
== SAFI_LABELED_UNICAST
)
794 tag
= bgp_adv_label(rn
, binfo
, peer
, afi
, safi
);
796 if (binfo
&& binfo
->extra
)
797 tag
= binfo
->extra
->tag
;
799 if (bgp_labeled_safi(safi
))
800 sprintf (label_buf
, "label %u", label_pton(tag
));
802 if (stream_empty (snlri
))
803 mpattrlen_pos
= bgp_packet_mpattr_start (snlri
, peer
, afi
, safi
,
804 &vecarr
, adv
->baa
->attr
);
806 bgp_packet_mpattr_prefix (snlri
, afi
, safi
, &rn
->p
, prd
,
807 tag
, addpath_encode
, addpath_tx_id
, adv
->baa
->attr
);
812 if (bgp_debug_update(NULL
, &rn
->p
, subgrp
->update_group
, 0))
814 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
816 if (!send_attr_printed
)
818 zlog_debug ("u%" PRIu64
":s%" PRIu64
" send UPDATE w/ attr: %s",
819 subgrp
->update_group
->id
, subgrp
->id
, send_attr_str
);
820 if (!stream_empty (snlri
))
825 pkt_afi
= afi_int2iana (afi
);
826 pkt_safi
= safi_int2iana (safi
);
827 zlog_debug ("u%" PRIu64
":s%" PRIu64
" send MP_REACH for afi/safi %d/%d",
828 subgrp
->update_group
->id
, subgrp
->id
, pkt_afi
, pkt_safi
);
831 send_attr_printed
= 1;
834 zlog_debug ("u%" PRIu64
":s%" PRIu64
" send UPDATE %s %s",
835 subgrp
->update_group
->id
, subgrp
->id
,
836 bgp_debug_rdpfxpath2str (prd
, &rn
->p
, addpath_encode
,
838 pfx_buf
, sizeof (pfx_buf
)),
842 /* Synchnorize attribute. */
844 bgp_attr_unintern (&adj
->attr
);
848 adj
->attr
= bgp_attr_intern (adv
->baa
->attr
);
850 adv
= bgp_advertise_clean_subgroup (subgrp
, adj
);
853 if (!stream_empty (s
))
855 if (!stream_empty (snlri
))
857 bgp_packet_mpattr_end (snlri
, mpattrlen_pos
);
858 total_attr_len
+= stream_get_endp (snlri
);
861 /* set the total attribute length correctly */
862 stream_putw_at (s
, attrlen_pos
, total_attr_len
);
864 if (!stream_empty (snlri
))
866 packet
= stream_dupcat (s
, snlri
, mpattr_pos
);
867 bpacket_attr_vec_arr_update (&vecarr
, mpattr_pos
);
870 packet
= stream_dup (s
);
871 bgp_packet_set_size (packet
);
872 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
873 zlog_debug ("u%" PRIu64
":s%" PRIu64
" send UPDATE len %zd numpfx %d",
874 subgrp
->update_group
->id
, subgrp
->id
,
875 (stream_get_endp(packet
) - stream_get_getp(packet
)), num_pfx
);
876 pkt
= bpacket_queue_add (SUBGRP_PKTQ (subgrp
), packet
, &vecarr
);
878 stream_reset (snlri
);
884 /* Make BGP withdraw packet. */
886 16-octet marker | 2-octet length | 1-octet type |
887 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0)
889 /* For other afi/safis:
890 16-octet marker | 2-octet length | 1-octet type |
891 2-octet withdrawn route length (=0) | 2-octet attrlen |
892 mp_unreach attr type | attr len | afi | safi | withdrawn prefixes
895 subgroup_withdraw_packet (struct update_subgroup
*subgrp
)
899 struct bgp_adj_out
*adj
;
900 struct bgp_advertise
*adv
;
903 bgp_size_t unfeasible_len
;
904 bgp_size_t total_attr_len
;
906 size_t attrlen_pos
= 0;
907 size_t mplen_pos
= 0;
908 u_char first_time
= 1;
911 int space_remaining
= 0;
912 int space_needed
= 0;
914 int addpath_encode
= 0;
915 u_int32_t addpath_tx_id
= 0;
916 struct prefix_rd
*prd
= NULL
;
922 if (bpacket_queue_is_full (SUBGRP_INST (subgrp
), SUBGRP_PKTQ (subgrp
)))
925 peer
= SUBGRP_PEER (subgrp
);
926 afi
= SUBGRP_AFI (subgrp
);
927 safi
= SUBGRP_SAFI (subgrp
);
930 addpath_encode
= bgp_addpath_encode_tx (peer
, afi
, safi
);
932 while ((adv
= BGP_ADV_FIFO_HEAD (&subgrp
->sync
->withdraw
)) != NULL
)
937 addpath_tx_id
= adj
->addpath_tx_id
;
939 space_remaining
= STREAM_REMAIN (s
) -
940 BGP_MAX_PACKET_SIZE_OVERFLOW
;
941 space_needed
= BGP_NLRI_LENGTH
+ BGP_TOTAL_ATTR_LEN
+
942 bgp_packet_mpattr_prefix_size (afi
, safi
, &rn
->p
);
944 if (space_remaining
< space_needed
)
947 if (stream_empty (s
))
949 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
950 stream_putw (s
, 0); /* unfeasible routes length */
955 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
&&
956 !peer_cap_enhe(peer
, afi
, safi
))
957 stream_put_prefix_addpath (s
, &rn
->p
, addpath_encode
, addpath_tx_id
);
961 prd
= (struct prefix_rd
*) &rn
->prn
->p
;
963 /* If first time, format the MP_UNREACH header */
969 pkt_afi
= afi_int2iana (afi
);
970 pkt_safi
= safi_int2iana (safi
);
972 attrlen_pos
= stream_get_endp (s
);
973 /* total attr length = 0 for now. reevaluate later */
975 mp_start
= stream_get_endp (s
);
976 mplen_pos
= bgp_packet_mpunreach_start (s
, afi
, safi
);
977 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
978 zlog_debug ("u%" PRIu64
":s%" PRIu64
" send MP_UNREACH for afi/safi %d/%d",
979 subgrp
->update_group
->id
, subgrp
->id
, pkt_afi
, pkt_safi
);
982 bgp_packet_mpunreach_prefix (s
, &rn
->p
, afi
, safi
, prd
, NULL
,
983 addpath_encode
, addpath_tx_id
, NULL
);
988 if (bgp_debug_update(NULL
, &rn
->p
, subgrp
->update_group
, 0))
990 char pfx_buf
[BGP_PRD_PATH_STRLEN
];
992 zlog_debug ("u%" PRIu64
":s%" PRIu64
" send UPDATE %s -- unreachable",
993 subgrp
->update_group
->id
, subgrp
->id
,
994 bgp_debug_rdpfxpath2str (prd
, &rn
->p
,
995 addpath_encode
, addpath_tx_id
,
996 pfx_buf
, sizeof (pfx_buf
)));
1001 bgp_adj_out_remove_subgroup (rn
, adj
, subgrp
);
1002 bgp_unlock_node (rn
);
1005 if (!stream_empty (s
))
1007 if (afi
== AFI_IP
&& safi
== SAFI_UNICAST
&&
1008 !peer_cap_enhe(peer
, afi
, safi
))
1011 = stream_get_endp (s
) - BGP_HEADER_SIZE
- BGP_UNFEASIBLE_LEN
;
1012 stream_putw_at (s
, BGP_HEADER_SIZE
, unfeasible_len
);
1017 /* Set the mp_unreach attr's length */
1018 bgp_packet_mpunreach_end (s
, mplen_pos
);
1020 /* Set total path attribute length. */
1021 total_attr_len
= stream_get_endp (s
) - mp_start
;
1022 stream_putw_at (s
, attrlen_pos
, total_attr_len
);
1024 bgp_packet_set_size (s
);
1025 if (bgp_debug_update(NULL
, NULL
, subgrp
->update_group
, 0))
1026 zlog_debug ("u%" PRIu64
":s%" PRIu64
" send UPDATE (withdraw) len %zd numpfx %d",
1027 subgrp
->update_group
->id
, subgrp
->id
,
1028 (stream_get_endp(s
) - stream_get_getp(s
)), num_pfx
);
1029 pkt
= bpacket_queue_add (SUBGRP_PKTQ (subgrp
), stream_dup (s
), NULL
);
1038 subgroup_default_update_packet (struct update_subgroup
*subgrp
,
1039 struct attr
*attr
, struct peer
*from
)
1045 bgp_size_t total_attr_len
;
1048 struct bpacket_attr_vec_arr vecarr
;
1049 int addpath_encode
= 0;
1051 if (DISABLE_BGP_ANNOUNCE
)
1057 peer
= SUBGRP_PEER (subgrp
);
1058 afi
= SUBGRP_AFI (subgrp
);
1059 safi
= SUBGRP_SAFI (subgrp
);
1060 bpacket_attr_vec_arr_reset (&vecarr
);
1061 addpath_encode
= bgp_addpath_encode_tx (peer
, afi
, safi
);
1064 str2prefix ("0.0.0.0/0", &p
);
1066 str2prefix ("::/0", &p
);
1068 /* Logging the attribute. */
1069 if (bgp_debug_update(NULL
, &p
, subgrp
->update_group
, 0))
1071 char attrstr
[BUFSIZ
];
1072 char buf
[PREFIX_STRLEN
];
1073 /* ' with addpath ID ' 17
1074 * max strlen of uint32 + 10
1075 * +/- (just in case) + 1
1076 * null terminator + 1
1077 * ============================ 29 */
1082 bgp_dump_attr (attr
, attrstr
, BUFSIZ
);
1085 snprintf(tx_id_buf
, sizeof (tx_id_buf
), " with addpath ID %u",
1086 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1088 zlog_debug ("u%" PRIu64
":s%" PRIu64
" send UPDATE %s%s %s",
1089 (SUBGRP_UPDGRP (subgrp
))->id
, subgrp
->id
,
1090 prefix2str (&p
, buf
, sizeof (buf
)),
1091 tx_id_buf
, attrstr
);
1094 s
= stream_new (BGP_MAX_PACKET_SIZE
);
1096 /* Make BGP update packet. */
1097 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
1099 /* Unfeasible Routes Length. */
1102 /* Make place for total attribute length. */
1103 pos
= stream_get_endp (s
);
1105 total_attr_len
= bgp_packet_attribute (NULL
, peer
, s
, attr
, &vecarr
, &p
,
1106 afi
, safi
, from
, NULL
, NULL
,
1108 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1110 /* Set Total Path Attribute Length. */
1111 stream_putw_at (s
, pos
, total_attr_len
);
1114 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
&&
1115 !peer_cap_enhe(peer
, afi
, safi
))
1116 stream_put_prefix_addpath (s
, &p
, addpath_encode
, BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1119 bgp_packet_set_size (s
);
1121 (void) bpacket_queue_add (SUBGRP_PKTQ (subgrp
), s
, &vecarr
);
1122 subgroup_trigger_write(subgrp
);
1126 subgroup_default_withdraw_packet (struct update_subgroup
*subgrp
)
1131 unsigned long attrlen_pos
= 0;
1133 bgp_size_t unfeasible_len
;
1134 bgp_size_t total_attr_len
= 0;
1135 size_t mp_start
= 0;
1136 size_t mplen_pos
= 0;
1139 int addpath_encode
= 0;
1141 if (DISABLE_BGP_ANNOUNCE
)
1144 peer
= SUBGRP_PEER (subgrp
);
1145 afi
= SUBGRP_AFI (subgrp
);
1146 safi
= SUBGRP_SAFI (subgrp
);
1147 addpath_encode
= bgp_addpath_encode_tx (peer
, afi
, safi
);
1150 str2prefix ("0.0.0.0/0", &p
);
1152 str2prefix ("::/0", &p
);
1154 if (bgp_debug_update(NULL
, &p
, subgrp
->update_group
, 0))
1156 char buf
[PREFIX_STRLEN
];
1157 /* ' with addpath ID ' 17
1158 * max strlen of uint32 + 10
1159 * +/- (just in case) + 1
1160 * null terminator + 1
1161 * ============================ 29 */
1165 snprintf(tx_id_buf
, sizeof (tx_id_buf
), " with addpath ID %u",
1166 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1168 zlog_debug ("u%" PRIu64
":s%" PRIu64
" send UPDATE %s%s -- unreachable",
1169 (SUBGRP_UPDGRP (subgrp
))->id
, subgrp
->id
,
1170 prefix2str (&p
, buf
, sizeof (buf
)), tx_id_buf
);
1173 s
= stream_new (BGP_MAX_PACKET_SIZE
);
1175 /* Make BGP update packet. */
1176 bgp_packet_set_marker (s
, BGP_MSG_UPDATE
);
1178 /* Unfeasible Routes Length. */ ;
1179 cp
= stream_get_endp (s
);
1182 /* Withdrawn Routes. */
1183 if (p
.family
== AF_INET
&& safi
== SAFI_UNICAST
&&
1184 !peer_cap_enhe(peer
, afi
, safi
))
1186 stream_put_prefix_addpath (s
, &p
, addpath_encode
,
1187 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
);
1189 unfeasible_len
= stream_get_endp (s
) - cp
- 2;
1191 /* Set unfeasible len. */
1192 stream_putw_at (s
, cp
, unfeasible_len
);
1194 /* Set total path attribute length. */
1199 attrlen_pos
= stream_get_endp (s
);
1201 mp_start
= stream_get_endp (s
);
1202 mplen_pos
= bgp_packet_mpunreach_start (s
, afi
, safi
);
1203 bgp_packet_mpunreach_prefix (s
, &p
, afi
, safi
, NULL
, NULL
,
1205 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE
, NULL
);
1207 /* Set the mp_unreach attr's length */
1208 bgp_packet_mpunreach_end (s
, mplen_pos
);
1210 /* Set total path attribute length. */
1211 total_attr_len
= stream_get_endp (s
) - mp_start
;
1212 stream_putw_at (s
, attrlen_pos
, total_attr_len
);
1215 bgp_packet_set_size (s
);
1217 (void) bpacket_queue_add (SUBGRP_PKTQ (subgrp
), s
, NULL
);
1218 subgroup_trigger_write(subgrp
);
1222 bpacket_vec_arr_inherit_attr_flags (struct bpacket_attr_vec_arr
*vecarr
,
1223 bpacket_attr_vec_type type
,
1226 if (CHECK_FLAG (attr
->rmap_change_flags
,
1227 BATTR_RMAP_NEXTHOP_PEER_ADDRESS
))
1228 SET_FLAG (vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1229 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS
);
1231 if (CHECK_FLAG (attr
->rmap_change_flags
, BATTR_REFLECTED
))
1232 SET_FLAG (vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1233 BPKT_ATTRVEC_FLAGS_REFLECTED
);
1235 if (CHECK_FLAG (attr
->rmap_change_flags
,
1236 BATTR_RMAP_NEXTHOP_UNCHANGED
))
1237 SET_FLAG (vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1238 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED
);
1240 if (CHECK_FLAG (attr
->rmap_change_flags
,
1241 BATTR_RMAP_IPV4_NHOP_CHANGED
))
1242 SET_FLAG (vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1243 BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED
);
1245 if (CHECK_FLAG (attr
->rmap_change_flags
,
1246 BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED
))
1247 SET_FLAG (vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1248 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED
);
1250 if (CHECK_FLAG (attr
->rmap_change_flags
,
1251 BATTR_RMAP_IPV6_LL_NHOP_CHANGED
))
1252 SET_FLAG (vecarr
->entries
[BGP_ATTR_VEC_NH
].flags
,
1253 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_LNH_CHANGED
);
1256 /* Reset the Attributes vector array. The vector array is used to override
1257 * certain output parameters in the packet for a particular peer
1260 bpacket_attr_vec_arr_reset (struct bpacket_attr_vec_arr
*vecarr
)
1268 while (i
< BGP_ATTR_VEC_MAX
)
1270 vecarr
->entries
[i
].flags
= 0;
1271 vecarr
->entries
[i
].offset
= 0;
1276 /* Setup a particular node entry in the vecarr */
1278 bpacket_attr_vec_arr_set_vec (struct bpacket_attr_vec_arr
*vecarr
,
1279 bpacket_attr_vec_type type
, struct stream
*s
,
1284 assert (type
< BGP_ATTR_VEC_MAX
);
1286 SET_FLAG (vecarr
->entries
[type
].flags
, BPKT_ATTRVEC_FLAGS_UPDATED
);
1287 vecarr
->entries
[type
].offset
= stream_get_endp (s
);
1289 bpacket_vec_arr_inherit_attr_flags(vecarr
, type
, attr
);