]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_updgrp_packet.c
Merge pull request #12603 from opensourcerouting/fix/deprecate_bgp_stuff_some
[mirror_frr.git] / bgpd / bgp_updgrp_packet.c
CommitLineData
3f9c7369
DS
1/**
2 * bgp_updgrp_packet.c: BGP update group packet handling routines
3 *
4 * @copyright Copyright (C) 2014 Cumulus Networks, Inc.
5 *
6 * @author Avneesh Sachdev <avneesh@sproute.net>
7 * @author Rajesh Varadarajan <rajesh@sproute.net>
8 * @author Pradosh Mohapatra <pradosh@sproute.net>
9 *
10 * This file is part of GNU Zebra.
11 *
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
15 * later version.
16 *
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.
21 *
896014f4
DL
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
3f9c7369
DS
25 */
26
27#include <zebra.h>
28
29#include "prefix.h"
30#include "thread.h"
31#include "buffer.h"
32#include "stream.h"
33#include "command.h"
34#include "sockunion.h"
35#include "network.h"
36#include "memory.h"
37#include "filter.h"
38#include "routemap.h"
3f9c7369
DS
39#include "log.h"
40#include "plist.h"
41#include "linklist.h"
42#include "workqueue.h"
43#include "hash.h"
44#include "queue.h"
cd1964ff 45#include "mpls.h"
3f9c7369
DS
46
47#include "bgpd/bgpd.h"
48#include "bgpd/bgp_debug.h"
14454c9f 49#include "bgpd/bgp_errors.h"
3f9c7369
DS
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"
906ad49b 57#include "bgpd/bgp_mplsvpn.h"
cd1964ff 58#include "bgpd/bgp_label.h"
dcc68b5e 59#include "bgpd/bgp_addpath.h"
3f9c7369
DS
60
61/********************
62 * PRIVATE FUNCTIONS
63 ********************/
64
65/********************
66 * PUBLIC FUNCTIONS
67 ********************/
4d762f26 68struct bpacket *bpacket_alloc(void)
3f9c7369 69{
d62a17ae 70 struct bpacket *pkt;
3f9c7369 71
9f5dc319 72 pkt = XCALLOC(MTYPE_BGP_PACKET, sizeof(struct bpacket));
3f9c7369 73
d62a17ae 74 return pkt;
3f9c7369
DS
75}
76
d62a17ae 77void bpacket_free(struct bpacket *pkt)
3f9c7369 78{
d62a17ae 79 if (pkt->buffer)
80 stream_free(pkt->buffer);
81 pkt->buffer = NULL;
82 XFREE(MTYPE_BGP_PACKET, pkt);
3f9c7369
DS
83}
84
d62a17ae 85void bpacket_queue_init(struct bpacket_queue *q)
3f9c7369 86{
d62a17ae 87 TAILQ_INIT(&(q->pkts));
3f9c7369
DS
88}
89
3f9c7369
DS
90/*
91 * bpacket_queue_add_packet
92 *
93 * Internal function of bpacket_queue - and adds a
94 * packet entry to the end of the list.
95 *
96 * Users of bpacket_queue should use bpacket_queue_add instead.
97 */
d62a17ae 98static void bpacket_queue_add_packet(struct bpacket_queue *q,
99 struct bpacket *pkt)
3f9c7369 100{
d62a17ae 101 struct bpacket *last_pkt;
3f9c7369 102
d62a17ae 103 if (TAILQ_EMPTY(&(q->pkts)))
104 TAILQ_INSERT_TAIL(&(q->pkts), pkt, pkt_train);
105 else {
106 last_pkt = bpacket_queue_last(q);
107 TAILQ_INSERT_AFTER(&(q->pkts), last_pkt, pkt, pkt_train);
108 }
109 q->curr_count++;
110 if (q->hwm_count < q->curr_count)
111 q->hwm_count = q->curr_count;
3f9c7369
DS
112}
113
114/*
115 * Adds a packet to the bpacket_queue.
116 *
117 * The stream passed is consumed by this function. So, the caller should
118 * not free or use the stream after
119 * invoking this function.
120 */
d62a17ae 121struct bpacket *bpacket_queue_add(struct bpacket_queue *q, struct stream *s,
122 struct bpacket_attr_vec_arr *vecarrp)
3f9c7369 123{
d62a17ae 124 struct bpacket *pkt;
125 struct bpacket *last_pkt;
126
127
128 pkt = bpacket_alloc();
129 if (TAILQ_EMPTY(&(q->pkts))) {
130 pkt->ver = 1;
131 pkt->buffer = s;
132 if (vecarrp)
133 memcpy(&pkt->arr, vecarrp,
134 sizeof(struct bpacket_attr_vec_arr));
135 else
136 bpacket_attr_vec_arr_reset(&pkt->arr);
137 bpacket_queue_add_packet(q, pkt);
d62a17ae 138 return pkt;
139 }
3f9c7369 140
d62a17ae 141 /*
142 * Fill in the new information into the current sentinel and create a
143 * new sentinel.
144 */
d62a17ae 145 last_pkt = bpacket_queue_last(q);
146 assert(last_pkt->buffer == NULL);
147 last_pkt->buffer = s;
148 if (vecarrp)
149 memcpy(&last_pkt->arr, vecarrp,
150 sizeof(struct bpacket_attr_vec_arr));
151 else
152 bpacket_attr_vec_arr_reset(&last_pkt->arr);
153
154 pkt->ver = last_pkt->ver;
155 pkt->ver++;
156 bpacket_queue_add_packet(q, pkt);
157
d62a17ae 158 return last_pkt;
3f9c7369
DS
159}
160
d62a17ae 161struct bpacket *bpacket_queue_first(struct bpacket_queue *q)
3f9c7369 162{
d62a17ae 163 return (TAILQ_FIRST(&(q->pkts)));
3f9c7369
DS
164}
165
d62a17ae 166struct bpacket *bpacket_queue_last(struct bpacket_queue *q)
3f9c7369 167{
d62a17ae 168 return TAILQ_LAST(&(q->pkts), pkt_queue);
3f9c7369
DS
169}
170
d62a17ae 171struct bpacket *bpacket_queue_remove(struct bpacket_queue *q)
3f9c7369 172{
d62a17ae 173 struct bpacket *first;
3f9c7369 174
d62a17ae 175 first = bpacket_queue_first(q);
176 if (first) {
177 TAILQ_REMOVE(&(q->pkts), first, pkt_train);
178 q->curr_count--;
179 }
180 return first;
3f9c7369
DS
181}
182
d62a17ae 183unsigned int bpacket_queue_length(struct bpacket_queue *q)
3f9c7369 184{
d62a17ae 185 return q->curr_count - 1;
3f9c7369
DS
186}
187
d62a17ae 188unsigned int bpacket_queue_hwm_length(struct bpacket_queue *q)
3f9c7369 189{
d62a17ae 190 return q->hwm_count - 1;
3f9c7369
DS
191}
192
3dc339cd 193bool bpacket_queue_is_full(struct bgp *bgp, struct bpacket_queue *q)
3f9c7369 194{
d62a17ae 195 if (q->curr_count >= bgp->default_subgroup_pkt_queue_max)
3dc339cd
DA
196 return true;
197 return false;
3f9c7369
DS
198}
199
d62a17ae 200void bpacket_add_peer(struct bpacket *pkt, struct peer_af *paf)
3f9c7369 201{
d62a17ae 202 if (!pkt || !paf)
203 return;
3f9c7369 204
d62a17ae 205 LIST_INSERT_HEAD(&(pkt->peers), paf, pkt_train);
206 paf->next_pkt_to_send = pkt;
3f9c7369
DS
207}
208
209/*
210 * bpacket_queue_cleanup
211 */
d62a17ae 212void bpacket_queue_cleanup(struct bpacket_queue *q)
3f9c7369 213{
d62a17ae 214 struct bpacket *pkt;
3f9c7369 215
d62a17ae 216 while ((pkt = bpacket_queue_remove(q))) {
217 bpacket_free(pkt);
218 }
3f9c7369
DS
219}
220
221/*
222 * bpacket_queue_compact
223 *
224 * Delete packets that do not need to be transmitted to any peer from
225 * the queue.
226 *
227 * @return the number of packets deleted.
228 */
d62a17ae 229static int bpacket_queue_compact(struct bpacket_queue *q)
3f9c7369 230{
d62a17ae 231 int num_deleted;
232 struct bpacket *pkt, *removed_pkt;
3f9c7369 233
d62a17ae 234 num_deleted = 0;
3f9c7369 235
d62a17ae 236 while (1) {
237 pkt = bpacket_queue_first(q);
238 if (!pkt)
239 break;
3f9c7369 240
d62a17ae 241 /*
242 * Don't delete the sentinel.
243 */
244 if (!pkt->buffer)
245 break;
3f9c7369 246
d62a17ae 247 if (!LIST_EMPTY(&(pkt->peers)))
248 break;
3f9c7369 249
d62a17ae 250 removed_pkt = bpacket_queue_remove(q);
251 assert(pkt == removed_pkt);
252 bpacket_free(removed_pkt);
3f9c7369 253
d62a17ae 254 num_deleted++;
255 }
3f9c7369 256
d62a17ae 257 return num_deleted;
3f9c7369
DS
258}
259
d62a17ae 260void bpacket_queue_advance_peer(struct peer_af *paf)
3f9c7369 261{
d62a17ae 262 struct bpacket *pkt;
263 struct bpacket *old_pkt;
264
265 old_pkt = paf->next_pkt_to_send;
266 if (old_pkt->buffer == NULL)
267 /* Already at end of list */
268 return;
269
270 LIST_REMOVE(paf, pkt_train);
271 pkt = TAILQ_NEXT(old_pkt, pkt_train);
272 bpacket_add_peer(pkt, paf);
273
274 if (!bpacket_queue_compact(PAF_PKTQ(paf)))
275 return;
276
277 /*
278 * Deleted one or more packets. Check if we can now merge this
279 * peer's subgroup into another subgroup.
280 */
281 update_subgroup_check_merge(paf->subgroup, "advanced peer in queue");
3f9c7369
DS
282}
283
284/*
285 * bpacket_queue_remove_peer
286 *
287 * Remove the peer from the packet queue of the subgroup it belongs
288 * to.
289 */
d62a17ae 290void bpacket_queue_remove_peer(struct peer_af *paf)
3f9c7369 291{
d62a17ae 292 struct bpacket_queue *q;
3f9c7369 293
d62a17ae 294 q = PAF_PKTQ(paf);
295 assert(q);
3f9c7369 296
d62a17ae 297 LIST_REMOVE(paf, pkt_train);
298 paf->next_pkt_to_send = NULL;
3f9c7369 299
d62a17ae 300 bpacket_queue_compact(q);
3f9c7369
DS
301}
302
d62a17ae 303unsigned int bpacket_queue_virtual_length(struct peer_af *paf)
3f9c7369 304{
d62a17ae 305 struct bpacket *pkt;
306 struct bpacket *last;
307 struct bpacket_queue *q;
3f9c7369 308
d62a17ae 309 pkt = paf->next_pkt_to_send;
310 if (!pkt || (pkt->buffer == NULL))
311 /* Already at end of list */
312 return 0;
3f9c7369 313
d62a17ae 314 q = PAF_PKTQ(paf);
315 if (TAILQ_EMPTY(&(q->pkts)))
316 return 0;
3f9c7369 317
d62a17ae 318 last = TAILQ_LAST(&(q->pkts), pkt_queue);
319 if (last->ver >= pkt->ver)
320 return last->ver - pkt->ver;
3f9c7369 321
d62a17ae 322 /* sequence # rolled over */
323 return (UINT_MAX - pkt->ver + 1) + last->ver;
3f9c7369
DS
324}
325
326/*
327 * Dump the bpacket queue
328 */
d62a17ae 329void bpacket_queue_show_vty(struct bpacket_queue *q, struct vty *vty)
3f9c7369 330{
d62a17ae 331 struct bpacket *pkt;
332 struct peer_af *paf;
333
334 pkt = bpacket_queue_first(q);
335 while (pkt) {
336 vty_out(vty, " Packet %p ver %u buffer %p\n", pkt, pkt->ver,
337 pkt->buffer);
338
a2addae8 339 LIST_FOREACH (paf, &(pkt->peers), pkt_train) {
d62a17ae 340 vty_out(vty, " - %s\n", paf->peer->host);
341 }
342 pkt = bpacket_next(pkt);
343 }
344 return;
3f9c7369
DS
345}
346
d62a17ae 347struct stream *bpacket_reformat_for_peer(struct bpacket *pkt,
348 struct peer_af *paf)
3f9c7369 349{
d62a17ae 350 struct stream *s = NULL;
351 bpacket_attr_vec *vec;
352 struct peer *peer;
1c875ddb 353 struct bgp_filter *filter;
d62a17ae 354
355 s = stream_dup(pkt->buffer);
356 peer = PAF_PEER(paf);
357
358 vec = &pkt->arr.entries[BGP_ATTR_VEC_NH];
d62a17ae 359
f041034e
DS
360 if (!CHECK_FLAG(vec->flags, BPKT_ATTRVEC_FLAGS_UPDATED))
361 return s;
362
363 uint8_t nhlen;
364 afi_t nhafi;
365 int route_map_sets_nh;
366
367 nhlen = stream_getc_from(s, vec->offset);
368 filter = &peer->filter[paf->afi][paf->safi];
369
370 if (peer_cap_enhe(peer, paf->afi, paf->safi))
371 nhafi = AFI_IP6;
372 else
373 nhafi = BGP_NEXTHOP_AFI_FROM_NHLEN(nhlen);
374
375 if (nhafi == AFI_IP) {
376 struct in_addr v4nh, *mod_v4nh;
377 int nh_modified = 0;
378 size_t offset_nh = vec->offset + 1;
379
380 route_map_sets_nh =
381 (CHECK_FLAG(vec->flags,
6eeb9255
DA
382 BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED) ||
383 CHECK_FLAG(vec->flags,
384 BPKT_ATTRVEC_FLAGS_RMAP_VPNV4_NH_CHANGED) ||
385 CHECK_FLAG(vec->flags,
386 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS));
f041034e
DS
387
388 switch (nhlen) {
389 case BGP_ATTR_NHLEN_IPV4:
390 break;
391 case BGP_ATTR_NHLEN_VPNV4:
392 offset_nh += 8;
393 break;
394 default:
395 /* TODO: handle IPv6 nexthops */
396 flog_warn(
397 EC_BGP_INVALID_NEXTHOP_LENGTH,
398 "%s: %s: invalid MP nexthop length (AFI IP): %u",
399 __func__, peer->host, nhlen);
400 stream_free(s);
401 return NULL;
402 }
403
404 stream_get_from(&v4nh, s, offset_nh, IPV4_MAX_BYTELEN);
405 mod_v4nh = &v4nh;
406
407 /*
408 * If route-map has set the nexthop, that is normally
409 * used; if it is specified as peer-address, the peering
410 * address is picked up. Otherwise, if NH is unavailable
411 * from attribute, the peering addr is picked up; the
412 * "NH unavailable" case also covers next-hop-self and
413 * some other scenarios - see subgroup_announce_check().
414 * In all other cases, use the nexthop carried in the
415 * attribute unless it is EBGP non-multiaccess and there
416 * is no next-hop-unchanged setting or the peer is EBGP
417 * and the route-map that changed the next-hop value
418 * was applied inbound rather than outbound. Updates to
419 * an EBGP peer should only modify the next-hop if it
420 * was set in an outbound route-map to that peer.
421 * Note: It is assumed route-map cannot set the nexthop
422 * to an invalid value.
423 */
424 if (route_map_sets_nh
425 && ((peer->sort != BGP_PEER_EBGP)
426 || ROUTE_MAP_OUT(filter))) {
427 if (CHECK_FLAG(
428 vec->flags,
429 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS)) {
d62a17ae 430 mod_v4nh = &peer->nexthop.v4;
431 nh_modified = 1;
432 }
f041034e
DS
433 } else if (v4nh.s_addr == INADDR_ANY) {
434 mod_v4nh = &peer->nexthop.v4;
435 nh_modified = 1;
436 } else if (peer->sort == BGP_PEER_EBGP
437 && (bgp_multiaccess_check_v4(v4nh, peer) == 0)
438 && !CHECK_FLAG(vec->flags,
439 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED)
440 && !peer_af_flag_check(
441 peer, paf->afi, paf->safi,
442 PEER_FLAG_NEXTHOP_UNCHANGED)) {
443 /* NOTE: not handling case where NH has new AFI
444 */
445 mod_v4nh = &peer->nexthop.v4;
446 nh_modified = 1;
447 }
d62a17ae 448
f041034e
DS
449 if (nh_modified) /* allow for VPN RD */
450 stream_put_in_addr_at(s, offset_nh, mod_v4nh);
451
452 if (bgp_debug_update(peer, NULL, NULL, 0))
23d0a753
DA
453 zlog_debug("u%" PRIu64 ":s%" PRIu64
454 " %s send UPDATE w/ nexthop %pI4%s",
f041034e 455 PAF_SUBGRP(paf)->update_group->id,
23d0a753 456 PAF_SUBGRP(paf)->id, peer->host, mod_v4nh,
3ddd6994
DA
457 (nhlen == BGP_ATTR_NHLEN_VPNV4 ? " and RD"
458 : ""));
f041034e
DS
459 } else if (nhafi == AFI_IP6) {
460 struct in6_addr v6nhglobal, *mod_v6nhg;
461 struct in6_addr v6nhlocal, *mod_v6nhl;
462 int gnh_modified, lnh_modified;
463 size_t offset_nhglobal = vec->offset + 1;
464 size_t offset_nhlocal = vec->offset + 1;
465
466 gnh_modified = lnh_modified = 0;
467 mod_v6nhg = &v6nhglobal;
468 mod_v6nhl = &v6nhlocal;
469
470 route_map_sets_nh =
471 (CHECK_FLAG(vec->flags,
6eeb9255
DA
472 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED) ||
473 CHECK_FLAG(
f041034e 474 vec->flags,
6eeb9255
DA
475 BPKT_ATTRVEC_FLAGS_RMAP_VPNV6_GNH_CHANGED) ||
476 CHECK_FLAG(vec->flags,
477 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS));
d62a17ae 478
f041034e
DS
479 /*
480 * The logic here is rather similar to that for IPv4, the
481 * additional work being to handle 1 or 2 nexthops.
482 * Also, 3rd party nexthop is not propagated for EBGP
483 * right now.
484 */
485 switch (nhlen) {
486 case BGP_ATTR_NHLEN_IPV6_GLOBAL:
487 break;
488 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL:
489 offset_nhlocal += IPV6_MAX_BYTELEN;
490 break;
491 case BGP_ATTR_NHLEN_VPNV6_GLOBAL:
492 offset_nhglobal += 8;
493 break;
494 case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL:
495 offset_nhglobal += 8;
496 offset_nhlocal += 8 * 2 + IPV6_MAX_BYTELEN;
497 break;
498 default:
499 /* TODO: handle IPv4 nexthops */
500 flog_warn(
501 EC_BGP_INVALID_NEXTHOP_LENGTH,
502 "%s: %s: invalid MP nexthop length (AFI IP6): %u",
503 __func__, peer->host, nhlen);
504 stream_free(s);
505 return NULL;
506 }
d62a17ae 507
f041034e
DS
508 stream_get_from(&v6nhglobal, s, offset_nhglobal,
509 IPV6_MAX_BYTELEN);
1c875ddb 510
f041034e
DS
511 /*
512 * Updates to an EBGP peer should only modify the
513 * next-hop if it was set in an outbound route-map
514 * to that peer.
515 */
516 if (route_map_sets_nh
517 && ((peer->sort != BGP_PEER_EBGP)
518 || ROUTE_MAP_OUT(filter))) {
519 if (CHECK_FLAG(
520 vec->flags,
521 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS)) {
d62a17ae 522 mod_v6nhg = &peer->nexthop.v6_global;
523 gnh_modified = 1;
524 }
f041034e
DS
525 } else if (IN6_IS_ADDR_UNSPECIFIED(&v6nhglobal)) {
526 mod_v6nhg = &peer->nexthop.v6_global;
527 gnh_modified = 1;
528 } else if ((peer->sort == BGP_PEER_EBGP)
529 && (!bgp_multiaccess_check_v6(v6nhglobal, peer))
530 && !CHECK_FLAG(vec->flags,
531 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED)
532 && !peer_af_flag_check(
5b786189 533 peer, paf->afi, paf->safi,
f041034e
DS
534 PEER_FLAG_NEXTHOP_UNCHANGED)) {
535 /* NOTE: not handling case where NH has new AFI
536 */
537 mod_v6nhg = &peer->nexthop.v6_global;
538 gnh_modified = 1;
539 }
d62a17ae 540
92d6f769 541 if (IN6_IS_ADDR_UNSPECIFIED(mod_v6nhg)) {
3a6290bd 542 if (peer->nexthop.v4.s_addr != INADDR_ANY) {
92d6f769
K
543 ipv4_to_ipv4_mapped_ipv6(mod_v6nhg,
544 peer->nexthop.v4);
545 }
546 }
547
548 if (IS_MAPPED_IPV6(&peer->nexthop.v6_global)) {
549 mod_v6nhg = &peer->nexthop.v6_global;
550 gnh_modified = 1;
551 }
552
f041034e
DS
553 if (nhlen == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
554 || nhlen == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL) {
555 stream_get_from(&v6nhlocal, s, offset_nhlocal,
556 IPV6_MAX_BYTELEN);
557 if (IN6_IS_ADDR_UNSPECIFIED(&v6nhlocal)) {
558 mod_v6nhl = &peer->nexthop.v6_local;
559 lnh_modified = 1;
d62a17ae 560 }
f041034e 561 }
d62a17ae 562
f041034e
DS
563 if (gnh_modified)
564 stream_put_in6_addr_at(s, offset_nhglobal, mod_v6nhg);
565 if (lnh_modified)
566 stream_put_in6_addr_at(s, offset_nhlocal, mod_v6nhl);
567
568 if (bgp_debug_update(peer, NULL, NULL, 0)) {
3ddd6994
DA
569 if (nhlen == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
570 || nhlen == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL)
f041034e 571 zlog_debug(
c0d72166
DS
572 "u%" PRIu64 ":s%" PRIu64
573 " %s send UPDATE w/ mp_nexthops %pI6, %pI6%s",
f041034e
DS
574 PAF_SUBGRP(paf)->update_group->id,
575 PAF_SUBGRP(paf)->id, peer->host,
c0d72166 576 mod_v6nhg, mod_v6nhl,
3ddd6994
DA
577 (nhlen == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
578 ? " and RD"
579 : ""));
f041034e 580 else
c0d72166
DS
581 zlog_debug(
582 "u%" PRIu64 ":s%" PRIu64
583 " %s send UPDATE w/ mp_nexthop %pI6%s",
584 PAF_SUBGRP(paf)->update_group->id,
585 PAF_SUBGRP(paf)->id, peer->host,
586 mod_v6nhg,
587 (nhlen == BGP_ATTR_NHLEN_VPNV6_GLOBAL
588 ? " and RD"
589 : ""));
f041034e
DS
590 }
591 } else if (paf->afi == AFI_L2VPN) {
592 struct in_addr v4nh, *mod_v4nh;
593 int nh_modified = 0;
594
595 stream_get_from(&v4nh, s, vec->offset + 1, 4);
596 mod_v4nh = &v4nh;
597
598 /* No route-map changes allowed for EVPN nexthops. */
599 if (v4nh.s_addr == INADDR_ANY) {
600 mod_v4nh = &peer->nexthop.v4;
601 nh_modified = 1;
d62a17ae 602 }
f041034e
DS
603
604 if (nh_modified)
605 stream_put_in_addr_at(s, vec->offset + 1, mod_v4nh);
606
607 if (bgp_debug_update(peer, NULL, NULL, 0))
23d0a753
DA
608 zlog_debug("u%" PRIu64 ":s%" PRIu64
609 " %s send UPDATE w/ nexthop %pI4",
f041034e 610 PAF_SUBGRP(paf)->update_group->id,
23d0a753 611 PAF_SUBGRP(paf)->id, peer->host, mod_v4nh);
3f9c7369 612 }
3f9c7369 613
d62a17ae 614 return s;
3f9c7369
DS
615}
616
617/*
618 * Update the vecarr offsets to go beyond 'pos' bytes, i.e. add 'pos'
619 * to each offset.
620 */
d62a17ae 621static void bpacket_attr_vec_arr_update(struct bpacket_attr_vec_arr *vecarr,
622 size_t pos)
3f9c7369 623{
d62a17ae 624 int i;
3f9c7369 625
d62a17ae 626 if (!vecarr)
627 return;
3f9c7369 628
d62a17ae 629 for (i = 0; i < BGP_ATTR_VEC_MAX; i++)
630 vecarr->entries[i].offset += pos;
3f9c7369
DS
631}
632
633/*
634 * Return if there are packets to build for this subgroup.
635 */
3dc339cd 636bool subgroup_packets_to_build(struct update_subgroup *subgrp)
3f9c7369 637{
d62a17ae 638 struct bgp_advertise *adv;
3f9c7369 639
d62a17ae 640 if (!subgrp)
3dc339cd 641 return false;
3f9c7369 642
a274fef8 643 adv = bgp_adv_fifo_first(&subgrp->sync->withdraw);
d62a17ae 644 if (adv)
3dc339cd 645 return true;
3f9c7369 646
a274fef8 647 adv = bgp_adv_fifo_first(&subgrp->sync->update);
d62a17ae 648 if (adv)
3dc339cd 649 return true;
3f9c7369 650
3dc339cd 651 return false;
3f9c7369
DS
652}
653
654/* Make BGP update packet. */
d62a17ae 655struct bpacket *subgroup_update_packet(struct update_subgroup *subgrp)
3f9c7369 656{
d62a17ae 657 struct bpacket_attr_vec_arr vecarr;
658 struct bpacket *pkt;
659 struct peer *peer;
660 struct stream *s;
661 struct stream *snlri;
662 struct stream *packet;
663 struct bgp_adj_out *adj;
664 struct bgp_advertise *adv;
9bcb3eef 665 struct bgp_dest *dest = NULL;
9b6d8fcf 666 struct bgp_path_info *path = NULL;
d62a17ae 667 bgp_size_t total_attr_len = 0;
668 unsigned long attrlen_pos = 0;
669 size_t mpattrlen_pos = 0;
670 size_t mpattr_pos = 0;
671 afi_t afi;
672 safi_t safi;
673 int space_remaining = 0;
674 int space_needed = 0;
675 char send_attr_str[BUFSIZ];
676 int send_attr_printed = 0;
677 int num_pfx = 0;
be92fc9f 678 bool addpath_capable = false;
e386c1d5 679 int addpath_overhead = 0;
d7c0a89a 680 uint32_t addpath_tx_id = 0;
d62a17ae 681 struct prefix_rd *prd = NULL;
b57ba6d2 682 mpls_label_t label = MPLS_INVALID_LABEL, *label_pnt = NULL;
d7c0a89a 683 uint32_t num_labels = 0;
d62a17ae 684
685 if (!subgrp)
686 return NULL;
3f9c7369 687
d62a17ae 688 if (bpacket_queue_is_full(SUBGRP_INST(subgrp), SUBGRP_PKTQ(subgrp)))
689 return NULL;
3f9c7369 690
d62a17ae 691 peer = SUBGRP_PEER(subgrp);
692 afi = SUBGRP_AFI(subgrp);
693 safi = SUBGRP_SAFI(subgrp);
694 s = subgrp->work;
695 stream_reset(s);
696 snlri = subgrp->scratch;
697 stream_reset(snlri);
698
699 bpacket_attr_vec_arr_reset(&vecarr);
700
be92fc9f
DA
701 addpath_capable = bgp_addpath_encode_tx(peer, afi, safi);
702 addpath_overhead = addpath_capable ? BGP_ADDPATH_ID_LEN : 0;
d62a17ae 703
a274fef8 704 adv = bgp_adv_fifo_first(&subgrp->sync->update);
d62a17ae 705 while (adv) {
9bcb3eef 706 const struct prefix *dest_p;
b54892e0 707
9bcb3eef
DS
708 assert(adv->dest);
709 dest = adv->dest;
710 dest_p = bgp_dest_get_prefix(dest);
d62a17ae 711 adj = adv->adj;
712 addpath_tx_id = adj->addpath_tx_id;
9b6d8fcf 713 path = adv->pathi;
d62a17ae 714
715 space_remaining = STREAM_CONCAT_REMAIN(s, snlri, STREAM_SIZE(s))
716 - BGP_MAX_PACKET_SIZE_OVERFLOW;
9bcb3eef
DS
717 space_needed =
718 BGP_NLRI_LENGTH + addpath_overhead
719 + bgp_packet_mpattr_prefix_size(afi, safi, dest_p);
d62a17ae 720
721 /* When remaining space can't include NLRI and it's length. */
722 if (space_remaining < space_needed)
723 break;
724
725 /* If packet is empty, set attribute. */
726 if (stream_empty(s)) {
727 struct peer *from = NULL;
728
9b6d8fcf
DS
729 if (path)
730 from = path->peer;
d62a17ae 731
732 /* 1: Write the BGP message header - 16 bytes marker, 2
733 * bytes length,
734 * one byte message type.
735 */
736 bgp_packet_set_marker(s, BGP_MSG_UPDATE);
737
738 /* 2: withdrawn routes length */
739 stream_putw(s, 0);
740
741 /* 3: total attributes length - attrlen_pos stores the
742 * position */
743 attrlen_pos = stream_get_endp(s);
744 stream_putw(s, 0);
745
746 /* 4: if there is MP_REACH_NLRI attribute, that should
747 * be the first
748 * attribute, according to
749 * draft-ietf-idr-error-handling. Save the
750 * position.
751 */
752 mpattr_pos = stream_get_endp(s);
753
754 /* 5: Encode all the attributes, except MP_REACH_NLRI
755 * attr. */
756 total_attr_len = bgp_packet_attribute(
757 NULL, peer, s, adv->baa->attr, &vecarr, NULL,
97a52c82 758 afi, safi, from, NULL, NULL, 0, 0, 0, path);
d62a17ae 759
760 space_remaining =
761 STREAM_CONCAT_REMAIN(s, snlri, STREAM_SIZE(s))
762 - BGP_MAX_PACKET_SIZE_OVERFLOW;
996c9314
LB
763 space_needed = BGP_NLRI_LENGTH + addpath_overhead
764 + bgp_packet_mpattr_prefix_size(
9bcb3eef 765 afi, safi, dest_p);
d62a17ae 766
767 /* If the attributes alone do not leave any room for
768 * NLRI then
769 * return */
770 if (space_remaining < space_needed) {
af4c2728 771 flog_err(
e50f7cfd 772 EC_BGP_UPDGRP_ATTR_LEN,
6cde4b45 773 "u%" PRIu64 ":s%" PRIu64" attributes too long, cannot send UPDATE",
d62a17ae 774 subgrp->update_group->id, subgrp->id);
775
776 /* Flush the FIFO update queue */
777 while (adv)
778 adv = bgp_advertise_clean_subgroup(
779 subgrp, adj);
780 return NULL;
781 }
782
783 if (BGP_DEBUG(update, UPDATE_OUT)
784 || BGP_DEBUG(update, UPDATE_PREFIX)) {
785 memset(send_attr_str, 0, BUFSIZ);
786 send_attr_printed = 0;
787 bgp_dump_attr(adv->baa->attr, send_attr_str,
5022c833 788 sizeof(send_attr_str));
d62a17ae 789 }
790 }
791
792 if ((afi == AFI_IP && safi == SAFI_UNICAST)
793 && !peer_cap_enhe(peer, afi, safi))
be92fc9f 794 stream_put_prefix_addpath(s, dest_p, addpath_capable,
d62a17ae 795 addpath_tx_id);
796 else {
797 /* Encode the prefix in MP_REACH_NLRI attribute */
9bcb3eef
DS
798 if (dest->pdest)
799 prd = (struct prefix_rd *)bgp_dest_get_prefix(
800 dest->pdest);
d62a17ae 801
b57ba6d2 802 if (safi == SAFI_LABELED_UNICAST) {
9bcb3eef 803 label = bgp_adv_label(dest, path, peer, afi,
d62a17ae 804 safi);
b57ba6d2
MK
805 label_pnt = &label;
806 num_labels = 1;
9b6d8fcf
DS
807 } else if (path && path->extra) {
808 label_pnt = &path->extra->label[0];
809 num_labels = path->extra->num_labels;
b57ba6d2 810 }
d62a17ae 811
812 if (stream_empty(snlri))
813 mpattrlen_pos = bgp_packet_mpattr_start(
814 snlri, peer, afi, safi, &vecarr,
815 adv->baa->attr);
816
9bcb3eef 817 bgp_packet_mpattr_prefix(snlri, afi, safi, dest_p, prd,
b57ba6d2 818 label_pnt, num_labels,
be92fc9f 819 addpath_capable, addpath_tx_id,
b57ba6d2 820 adv->baa->attr);
d62a17ae 821 }
822
823 num_pfx++;
824
9bcb3eef 825 if (bgp_debug_update(NULL, dest_p, subgrp->update_group, 0)) {
d62a17ae 826 char pfx_buf[BGP_PRD_PATH_STRLEN];
827
828 if (!send_attr_printed) {
6cde4b45 829 zlog_debug("u%" PRIu64 ":s%" PRIu64" send UPDATE w/ attr: %s",
d62a17ae 830 subgrp->update_group->id, subgrp->id,
831 send_attr_str);
832 if (!stream_empty(snlri)) {
833 iana_afi_t pkt_afi;
5c525538 834 iana_safi_t pkt_safi;
d62a17ae 835
836 pkt_afi = afi_int2iana(afi);
837 pkt_safi = safi_int2iana(safi);
838 zlog_debug(
adf086ec
DA
839 "u%" PRIu64 ":s%" PRIu64
840 " send MP_REACH for afi/safi %s/%s",
d62a17ae 841 subgrp->update_group->id,
adf086ec
DA
842 subgrp->id,
843 iana_afi2str(pkt_afi),
844 iana_safi2str(pkt_safi));
d62a17ae 845 }
846
847 send_attr_printed = 1;
848 }
849
9bcb3eef
DS
850 bgp_debug_rdpfxpath2str(afi, safi, prd, dest_p,
851 label_pnt, num_labels,
be92fc9f 852 addpath_capable, addpath_tx_id,
6c995628 853 &adv->baa->attr->evpn_overlay,
9bcb3eef 854 pfx_buf, sizeof(pfx_buf));
d62a17ae 855 zlog_debug("u%" PRIu64 ":s%" PRIu64 " send UPDATE %s",
856 subgrp->update_group->id, subgrp->id,
857 pfx_buf);
858 }
859
860 /* Synchnorize attribute. */
861 if (adj->attr)
862 bgp_attr_unintern(&adj->attr);
863 else
864 subgrp->scount++;
865
866 adj->attr = bgp_attr_intern(adv->baa->attr);
d62a17ae 867 adv = bgp_advertise_clean_subgroup(subgrp, adj);
3f9c7369
DS
868 }
869
d62a17ae 870 if (!stream_empty(s)) {
871 if (!stream_empty(snlri)) {
872 bgp_packet_mpattr_end(snlri, mpattrlen_pos);
873 total_attr_len += stream_get_endp(snlri);
874 }
875
876 /* set the total attribute length correctly */
877 stream_putw_at(s, attrlen_pos, total_attr_len);
878
879 if (!stream_empty(snlri)) {
880 packet = stream_dupcat(s, snlri, mpattr_pos);
881 bpacket_attr_vec_arr_update(&vecarr, mpattr_pos);
882 } else
883 packet = stream_dup(s);
884 bgp_packet_set_size(packet);
885 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
ef56aee4
DA
886 zlog_debug(
887 "u%" PRIu64 ":s%" PRIu64
888 " send UPDATE len %zd (max message len: %hu) numpfx %d",
889 subgrp->update_group->id, subgrp->id,
890 (stream_get_endp(packet)
891 - stream_get_getp(packet)),
892 peer->max_packet_size, num_pfx);
d62a17ae 893 pkt = bpacket_queue_add(SUBGRP_PKTQ(subgrp), packet, &vecarr);
894 stream_reset(s);
895 stream_reset(snlri);
896 return pkt;
3f9c7369 897 }
d62a17ae 898 return NULL;
3f9c7369
DS
899}
900
901/* Make BGP withdraw packet. */
902/* For ipv4 unicast:
903 16-octet marker | 2-octet length | 1-octet type |
904 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0)
905*/
906/* For other afi/safis:
907 16-octet marker | 2-octet length | 1-octet type |
908 2-octet withdrawn route length (=0) | 2-octet attrlen |
909 mp_unreach attr type | attr len | afi | safi | withdrawn prefixes
910*/
d62a17ae 911struct bpacket *subgroup_withdraw_packet(struct update_subgroup *subgrp)
3f9c7369 912{
d62a17ae 913 struct bpacket *pkt;
914 struct stream *s;
915 struct bgp_adj_out *adj;
916 struct bgp_advertise *adv;
917 struct peer *peer;
9bcb3eef 918 struct bgp_dest *dest;
d62a17ae 919 bgp_size_t unfeasible_len;
920 bgp_size_t total_attr_len;
921 size_t mp_start = 0;
922 size_t attrlen_pos = 0;
923 size_t mplen_pos = 0;
d7c0a89a 924 uint8_t first_time = 1;
d62a17ae 925 afi_t afi;
926 safi_t safi;
927 int space_remaining = 0;
928 int space_needed = 0;
929 int num_pfx = 0;
be92fc9f 930 bool addpath_capable = false;
e386c1d5 931 int addpath_overhead = 0;
d7c0a89a 932 uint32_t addpath_tx_id = 0;
b54892e0 933 const struct prefix_rd *prd = NULL;
d62a17ae 934
935
936 if (!subgrp)
937 return NULL;
3f9c7369 938
d62a17ae 939 if (bpacket_queue_is_full(SUBGRP_INST(subgrp), SUBGRP_PKTQ(subgrp)))
940 return NULL;
906ad49b 941
d62a17ae 942 peer = SUBGRP_PEER(subgrp);
943 afi = SUBGRP_AFI(subgrp);
944 safi = SUBGRP_SAFI(subgrp);
945 s = subgrp->work;
946 stream_reset(s);
be92fc9f
DA
947 addpath_capable = bgp_addpath_encode_tx(peer, afi, safi);
948 addpath_overhead = addpath_capable ? BGP_ADDPATH_ID_LEN : 0;
d62a17ae 949
a274fef8 950 while ((adv = bgp_adv_fifo_first(&subgrp->sync->withdraw)) != NULL) {
9bcb3eef 951 const struct prefix *dest_p;
b54892e0 952
9bcb3eef 953 assert(adv->dest);
d62a17ae 954 adj = adv->adj;
9bcb3eef
DS
955 dest = adv->dest;
956 dest_p = bgp_dest_get_prefix(dest);
d62a17ae 957 addpath_tx_id = adj->addpath_tx_id;
958
959 space_remaining =
2d34fb80 960 STREAM_WRITEABLE(s) - BGP_MAX_PACKET_SIZE_OVERFLOW;
9bcb3eef
DS
961 space_needed =
962 BGP_NLRI_LENGTH + addpath_overhead + BGP_TOTAL_ATTR_LEN
963 + bgp_packet_mpattr_prefix_size(afi, safi, dest_p);
d62a17ae 964
965 if (space_remaining < space_needed)
966 break;
967
968 if (stream_empty(s)) {
969 bgp_packet_set_marker(s, BGP_MSG_UPDATE);
970 stream_putw(s, 0); /* unfeasible routes length */
971 } else
972 first_time = 0;
973
974 if (afi == AFI_IP && safi == SAFI_UNICAST
975 && !peer_cap_enhe(peer, afi, safi))
be92fc9f 976 stream_put_prefix_addpath(s, dest_p, addpath_capable,
d62a17ae 977 addpath_tx_id);
978 else {
9bcb3eef
DS
979 if (dest->pdest)
980 prd = (struct prefix_rd *)bgp_dest_get_prefix(
981 dest->pdest);
d62a17ae 982
b54892e0
DS
983 /* If first time, format the MP_UNREACH header
984 */
d62a17ae 985 if (first_time) {
986 iana_afi_t pkt_afi;
5c525538 987 iana_safi_t pkt_safi;
d62a17ae 988
989 pkt_afi = afi_int2iana(afi);
990 pkt_safi = safi_int2iana(safi);
991
992 attrlen_pos = stream_get_endp(s);
b54892e0
DS
993 /* total attr length = 0 for now.
994 * reevaluate later */
d62a17ae 995 stream_putw(s, 0);
996 mp_start = stream_get_endp(s);
997 mplen_pos = bgp_packet_mpunreach_start(s, afi,
998 safi);
999 if (bgp_debug_update(NULL, NULL,
1000 subgrp->update_group, 0))
1001 zlog_debug(
adf086ec
DA
1002 "u%" PRIu64 ":s%" PRIu64
1003 " send MP_UNREACH for afi/safi %s/%s",
d62a17ae 1004 subgrp->update_group->id,
adf086ec
DA
1005 subgrp->id,
1006 iana_afi2str(pkt_afi),
1007 iana_safi2str(pkt_safi));
d62a17ae 1008 }
1009
9bcb3eef 1010 bgp_packet_mpunreach_prefix(s, dest_p, afi, safi, prd,
be92fc9f 1011 NULL, 0, addpath_capable,
d62a17ae 1012 addpath_tx_id, NULL);
1013 }
1014
1015 num_pfx++;
1016
9bcb3eef 1017 if (bgp_debug_update(NULL, dest_p, subgrp->update_group, 0)) {
d62a17ae 1018 char pfx_buf[BGP_PRD_PATH_STRLEN];
1019
9bcb3eef 1020 bgp_debug_rdpfxpath2str(afi, safi, prd, dest_p, NULL, 0,
be92fc9f 1021 addpath_capable, addpath_tx_id,
6c995628 1022 NULL, pfx_buf, sizeof(pfx_buf));
6cde4b45 1023 zlog_debug("u%" PRIu64 ":s%" PRIu64" send UPDATE %s -- unreachable",
d62a17ae 1024 subgrp->update_group->id, subgrp->id,
1025 pfx_buf);
1026 }
1027
1028 subgrp->scount--;
1029
9bcb3eef 1030 bgp_adj_out_remove_subgroup(dest, adj, subgrp);
3f9c7369
DS
1031 }
1032
d62a17ae 1033 if (!stream_empty(s)) {
1034 if (afi == AFI_IP && safi == SAFI_UNICAST
1035 && !peer_cap_enhe(peer, afi, safi)) {
1036 unfeasible_len = stream_get_endp(s) - BGP_HEADER_SIZE
1037 - BGP_UNFEASIBLE_LEN;
1038 stream_putw_at(s, BGP_HEADER_SIZE, unfeasible_len);
1039 stream_putw(s, 0);
1040 } else {
1041 /* Set the mp_unreach attr's length */
1042 bgp_packet_mpunreach_end(s, mplen_pos);
1043
1044 /* Set total path attribute length. */
1045 total_attr_len = stream_get_endp(s) - mp_start;
1046 stream_putw_at(s, attrlen_pos, total_attr_len);
1047 }
1048 bgp_packet_set_size(s);
1049 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
6cde4b45 1050 zlog_debug("u%" PRIu64 ":s%" PRIu64" send UPDATE (withdraw) len %zd numpfx %d",
d62a17ae 1051 subgrp->update_group->id, subgrp->id,
1052 (stream_get_endp(s) - stream_get_getp(s)),
1053 num_pfx);
1054 pkt = bpacket_queue_add(SUBGRP_PKTQ(subgrp), stream_dup(s),
1055 NULL);
1056 stream_reset(s);
1057 return pkt;
3f9c7369 1058 }
3f9c7369 1059
d62a17ae 1060 return NULL;
3f9c7369
DS
1061}
1062
d62a17ae 1063void subgroup_default_update_packet(struct update_subgroup *subgrp,
1064 struct attr *attr, struct peer *from)
3f9c7369 1065{
d62a17ae 1066 struct stream *s;
1067 struct peer *peer;
1068 struct prefix p;
1069 unsigned long pos;
1070 bgp_size_t total_attr_len;
1071 afi_t afi;
1072 safi_t safi;
1073 struct bpacket_attr_vec_arr vecarr;
be92fc9f 1074 bool addpath_capable = false;
2de5f5b5
DA
1075 uint8_t default_originate_label[4] = {0x80, 0x00, 0x00};
1076 mpls_label_t *label = NULL;
1077 uint32_t num_labels = 0;
d62a17ae 1078
1079 if (DISABLE_BGP_ANNOUNCE)
1080 return;
1081
1082 if (!subgrp)
1083 return;
1084
1085 peer = SUBGRP_PEER(subgrp);
1086 afi = SUBGRP_AFI(subgrp);
1087 safi = SUBGRP_SAFI(subgrp);
1088 bpacket_attr_vec_arr_reset(&vecarr);
be92fc9f 1089 addpath_capable = bgp_addpath_encode_tx(peer, afi, safi);
d62a17ae 1090
2de5f5b5
DA
1091 if (safi == SAFI_LABELED_UNICAST) {
1092 label = (mpls_label_t *)default_originate_label;
1093 num_labels = 1;
1094 }
1095
cbb65f5e
RW
1096 memset(&p, 0, sizeof(p));
1097 p.family = afi2family(afi);
1098 p.prefixlen = 0;
d62a17ae 1099
1100 /* Logging the attribute. */
1101 if (bgp_debug_update(NULL, &p, subgrp->update_group, 0)) {
1102 char attrstr[BUFSIZ];
d62a17ae 1103 /* ' with addpath ID ' 17
1104 * max strlen of uint32 + 10
1105 * +/- (just in case) + 1
1106 * null terminator + 1
1107 * ============================ 29 */
1108 char tx_id_buf[30];
1109
1110 attrstr[0] = '\0';
1111
5022c833 1112 bgp_dump_attr(attr, attrstr, sizeof(attrstr));
d62a17ae 1113
be92fc9f 1114 if (addpath_capable)
d62a17ae 1115 snprintf(tx_id_buf, sizeof(tx_id_buf),
1116 " with addpath ID %u",
1117 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
cbb263cf
DS
1118 else
1119 tx_id_buf[0] = '\0';
d62a17ae 1120
2dbe669b
DA
1121 zlog_debug("u%" PRIu64 ":s%" PRIu64 " send UPDATE %pFX%s %s",
1122 (SUBGRP_UPDGRP(subgrp))->id, subgrp->id, &p,
1123 tx_id_buf, attrstr);
d62a17ae 1124 }
3f9c7369 1125
ef56aee4 1126 s = stream_new(peer->max_packet_size);
3f9c7369 1127
d62a17ae 1128 /* Make BGP update packet. */
1129 bgp_packet_set_marker(s, BGP_MSG_UPDATE);
3f9c7369 1130
d62a17ae 1131 /* Unfeasible Routes Length. */
1132 stream_putw(s, 0);
3f9c7369 1133
d62a17ae 1134 /* Make place for total attribute length. */
1135 pos = stream_get_endp(s);
1136 stream_putw(s, 0);
1137 total_attr_len = bgp_packet_attribute(
2de5f5b5
DA
1138 NULL, peer, s, attr, &vecarr, &p, afi, safi, from, NULL, label,
1139 num_labels, addpath_capable,
1140 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE, NULL);
3f9c7369 1141
d62a17ae 1142 /* Set Total Path Attribute Length. */
1143 stream_putw_at(s, pos, total_attr_len);
3f9c7369 1144
d62a17ae 1145 /* NLRI set. */
1146 if (p.family == AF_INET && safi == SAFI_UNICAST
1147 && !peer_cap_enhe(peer, afi, safi))
1148 stream_put_prefix_addpath(
be92fc9f 1149 s, &p, addpath_capable,
d62a17ae 1150 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
3f9c7369 1151
d62a17ae 1152 /* Set size. */
1153 bgp_packet_set_size(s);
3f9c7369 1154
d62a17ae 1155 (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp), s, &vecarr);
2fc102e1 1156 subgroup_trigger_write(subgrp);
46c4f05b
IS
1157
1158 if (!CHECK_FLAG(subgrp->sflags,
1159 SUBGRP_STATUS_PEER_DEFAULT_ORIGINATED)) {
1160 subgrp->scount++;
1161 SET_FLAG(subgrp->sflags, SUBGRP_STATUS_PEER_DEFAULT_ORIGINATED);
1162 }
3f9c7369
DS
1163}
1164
d62a17ae 1165void subgroup_default_withdraw_packet(struct update_subgroup *subgrp)
3f9c7369 1166{
d62a17ae 1167 struct peer *peer;
1168 struct stream *s;
1169 struct prefix p;
1170 unsigned long attrlen_pos = 0;
1171 unsigned long cp;
1172 bgp_size_t unfeasible_len;
1173 bgp_size_t total_attr_len = 0;
1174 size_t mp_start = 0;
1175 size_t mplen_pos = 0;
1176 afi_t afi;
1177 safi_t safi;
be92fc9f 1178 bool addpath_capable = false;
d62a17ae 1179
1180 if (DISABLE_BGP_ANNOUNCE)
1181 return;
1182
1183 peer = SUBGRP_PEER(subgrp);
1184 afi = SUBGRP_AFI(subgrp);
1185 safi = SUBGRP_SAFI(subgrp);
be92fc9f 1186 addpath_capable = bgp_addpath_encode_tx(peer, afi, safi);
d62a17ae 1187
cbb65f5e
RW
1188 memset(&p, 0, sizeof(p));
1189 p.family = afi2family(afi);
1190 p.prefixlen = 0;
d62a17ae 1191
1192 if (bgp_debug_update(NULL, &p, subgrp->update_group, 0)) {
d62a17ae 1193 /* ' with addpath ID ' 17
1194 * max strlen of uint32 + 10
1195 * +/- (just in case) + 1
1196 * null terminator + 1
1197 * ============================ 29 */
1198 char tx_id_buf[30];
1199
be92fc9f 1200 if (addpath_capable)
d62a17ae 1201 snprintf(tx_id_buf, sizeof(tx_id_buf),
1202 " with addpath ID %u",
1203 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
1204
2dbe669b
DA
1205 zlog_debug("u%" PRIu64 ":s%" PRIu64
1206 " send UPDATE %pFX%s -- unreachable",
1207 (SUBGRP_UPDGRP(subgrp))->id, subgrp->id, &p,
1208 tx_id_buf);
d62a17ae 1209 }
3f9c7369 1210
ef56aee4 1211 s = stream_new(peer->max_packet_size);
3f9c7369 1212
d62a17ae 1213 /* Make BGP update packet. */
1214 bgp_packet_set_marker(s, BGP_MSG_UPDATE);
3f9c7369 1215
d62a17ae 1216 /* Unfeasible Routes Length. */;
1217 cp = stream_get_endp(s);
1218 stream_putw(s, 0);
3f9c7369 1219
d62a17ae 1220 /* Withdrawn Routes. */
1221 if (p.family == AF_INET && safi == SAFI_UNICAST
1222 && !peer_cap_enhe(peer, afi, safi)) {
1223 stream_put_prefix_addpath(
be92fc9f 1224 s, &p, addpath_capable,
d62a17ae 1225 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
3f9c7369 1226
d62a17ae 1227 unfeasible_len = stream_get_endp(s) - cp - 2;
3f9c7369 1228
d62a17ae 1229 /* Set unfeasible len. */
1230 stream_putw_at(s, cp, unfeasible_len);
3f9c7369 1231
d62a17ae 1232 /* Set total path attribute length. */
1233 stream_putw(s, 0);
1234 } else {
1235 attrlen_pos = stream_get_endp(s);
1236 stream_putw(s, 0);
1237 mp_start = stream_get_endp(s);
1238 mplen_pos = bgp_packet_mpunreach_start(s, afi, safi);
1239 bgp_packet_mpunreach_prefix(
be92fc9f 1240 s, &p, afi, safi, NULL, NULL, 0, addpath_capable,
d62a17ae 1241 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE, NULL);
3f9c7369 1242
d62a17ae 1243 /* Set the mp_unreach attr's length */
1244 bgp_packet_mpunreach_end(s, mplen_pos);
3f9c7369 1245
d62a17ae 1246 /* Set total path attribute length. */
1247 total_attr_len = stream_get_endp(s) - mp_start;
1248 stream_putw_at(s, attrlen_pos, total_attr_len);
1249 }
1250
1251 bgp_packet_set_size(s);
1252
1253 (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp), s, NULL);
2fc102e1 1254 subgroup_trigger_write(subgrp);
46c4f05b
IS
1255
1256 if (CHECK_FLAG(subgrp->sflags, SUBGRP_STATUS_PEER_DEFAULT_ORIGINATED)) {
1257 subgrp->scount--;
1258 UNSET_FLAG(subgrp->sflags,
1259 SUBGRP_STATUS_PEER_DEFAULT_ORIGINATED);
1260 }
3f9c7369
DS
1261}
1262
1263static void
d62a17ae 1264bpacket_vec_arr_inherit_attr_flags(struct bpacket_attr_vec_arr *vecarr,
e8e36ff3 1265 enum bpacket_attr_vec_type type,
d62a17ae 1266 struct attr *attr)
3f9c7369 1267{
d62a17ae 1268 if (CHECK_FLAG(attr->rmap_change_flags,
1269 BATTR_RMAP_NEXTHOP_PEER_ADDRESS))
1270 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1271 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS);
1272
1273 if (CHECK_FLAG(attr->rmap_change_flags, BATTR_REFLECTED))
1274 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1275 BPKT_ATTRVEC_FLAGS_REFLECTED);
1276
1277 if (CHECK_FLAG(attr->rmap_change_flags, BATTR_RMAP_NEXTHOP_UNCHANGED))
1278 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1279 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED);
1280
1281 if (CHECK_FLAG(attr->rmap_change_flags, BATTR_RMAP_IPV4_NHOP_CHANGED))
1282 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1283 BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED);
1284
1285 if (CHECK_FLAG(attr->rmap_change_flags,
1286 BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED))
1287 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1288 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED);
6eeb9255
DA
1289
1290 if (CHECK_FLAG(attr->rmap_change_flags, BATTR_RMAP_VPNV4_NHOP_CHANGED))
1291 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1292 BPKT_ATTRVEC_FLAGS_RMAP_VPNV4_NH_CHANGED);
1293
1294 if (CHECK_FLAG(attr->rmap_change_flags,
1295 BATTR_RMAP_VPNV6_GLOBAL_NHOP_CHANGED))
1296 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1297 BPKT_ATTRVEC_FLAGS_RMAP_VPNV6_GNH_CHANGED);
d62a17ae 1298
1299 if (CHECK_FLAG(attr->rmap_change_flags,
1300 BATTR_RMAP_IPV6_LL_NHOP_CHANGED))
1301 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1302 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_LNH_CHANGED);
3f9c7369
DS
1303}
1304
1305/* Reset the Attributes vector array. The vector array is used to override
1306 * certain output parameters in the packet for a particular peer
1307 */
d62a17ae 1308void bpacket_attr_vec_arr_reset(struct bpacket_attr_vec_arr *vecarr)
3f9c7369 1309{
d62a17ae 1310 int i;
3f9c7369 1311
d62a17ae 1312 if (!vecarr)
1313 return;
3f9c7369 1314
d62a17ae 1315 i = 0;
1316 while (i < BGP_ATTR_VEC_MAX) {
1317 vecarr->entries[i].flags = 0;
1318 vecarr->entries[i].offset = 0;
1319 i++;
1320 }
3f9c7369
DS
1321}
1322
1323/* Setup a particular node entry in the vecarr */
d62a17ae 1324void bpacket_attr_vec_arr_set_vec(struct bpacket_attr_vec_arr *vecarr,
e8e36ff3
DA
1325 enum bpacket_attr_vec_type type,
1326 struct stream *s, struct attr *attr)
3f9c7369 1327{
d62a17ae 1328 if (!vecarr)
1329 return;
1330 assert(type < BGP_ATTR_VEC_MAX);
1331
1332 SET_FLAG(vecarr->entries[type].flags, BPKT_ATTRVEC_FLAGS_UPDATED);
1333 vecarr->entries[type].offset = stream_get_endp(s);
1334 if (attr)
1335 bpacket_vec_arr_inherit_attr_flags(vecarr, type, attr);
3f9c7369 1336}