]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_updgrp_packet.c
bgpd: Convert bpacket_attr_vec_type to enum
[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,
382 BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED)
383 || CHECK_FLAG(
384 vec->flags,
385 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS));
386
387 switch (nhlen) {
388 case BGP_ATTR_NHLEN_IPV4:
389 break;
390 case BGP_ATTR_NHLEN_VPNV4:
391 offset_nh += 8;
392 break;
393 default:
394 /* TODO: handle IPv6 nexthops */
395 flog_warn(
396 EC_BGP_INVALID_NEXTHOP_LENGTH,
397 "%s: %s: invalid MP nexthop length (AFI IP): %u",
398 __func__, peer->host, nhlen);
399 stream_free(s);
400 return NULL;
401 }
402
403 stream_get_from(&v4nh, s, offset_nh, IPV4_MAX_BYTELEN);
404 mod_v4nh = &v4nh;
405
406 /*
407 * If route-map has set the nexthop, that is normally
408 * used; if it is specified as peer-address, the peering
409 * address is picked up. Otherwise, if NH is unavailable
410 * from attribute, the peering addr is picked up; the
411 * "NH unavailable" case also covers next-hop-self and
412 * some other scenarios - see subgroup_announce_check().
413 * In all other cases, use the nexthop carried in the
414 * attribute unless it is EBGP non-multiaccess and there
415 * is no next-hop-unchanged setting or the peer is EBGP
416 * and the route-map that changed the next-hop value
417 * was applied inbound rather than outbound. Updates to
418 * an EBGP peer should only modify the next-hop if it
419 * was set in an outbound route-map to that peer.
420 * Note: It is assumed route-map cannot set the nexthop
421 * to an invalid value.
422 */
423 if (route_map_sets_nh
424 && ((peer->sort != BGP_PEER_EBGP)
425 || ROUTE_MAP_OUT(filter))) {
426 if (CHECK_FLAG(
427 vec->flags,
428 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS)) {
d62a17ae 429 mod_v4nh = &peer->nexthop.v4;
430 nh_modified = 1;
431 }
f041034e
DS
432 } else if (v4nh.s_addr == INADDR_ANY) {
433 mod_v4nh = &peer->nexthop.v4;
434 nh_modified = 1;
435 } else if (peer->sort == BGP_PEER_EBGP
436 && (bgp_multiaccess_check_v4(v4nh, peer) == 0)
437 && !CHECK_FLAG(vec->flags,
438 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED)
439 && !peer_af_flag_check(
440 peer, paf->afi, paf->safi,
441 PEER_FLAG_NEXTHOP_UNCHANGED)) {
442 /* NOTE: not handling case where NH has new AFI
443 */
444 mod_v4nh = &peer->nexthop.v4;
445 nh_modified = 1;
446 }
d62a17ae 447
f041034e
DS
448 if (nh_modified) /* allow for VPN RD */
449 stream_put_in_addr_at(s, offset_nh, mod_v4nh);
450
451 if (bgp_debug_update(peer, NULL, NULL, 0))
23d0a753
DA
452 zlog_debug("u%" PRIu64 ":s%" PRIu64
453 " %s send UPDATE w/ nexthop %pI4%s",
f041034e 454 PAF_SUBGRP(paf)->update_group->id,
23d0a753 455 PAF_SUBGRP(paf)->id, peer->host, mod_v4nh,
3ddd6994
DA
456 (nhlen == BGP_ATTR_NHLEN_VPNV4 ? " and RD"
457 : ""));
f041034e
DS
458 } else if (nhafi == AFI_IP6) {
459 struct in6_addr v6nhglobal, *mod_v6nhg;
460 struct in6_addr v6nhlocal, *mod_v6nhl;
461 int gnh_modified, lnh_modified;
462 size_t offset_nhglobal = vec->offset + 1;
463 size_t offset_nhlocal = vec->offset + 1;
464
465 gnh_modified = lnh_modified = 0;
466 mod_v6nhg = &v6nhglobal;
467 mod_v6nhl = &v6nhlocal;
468
469 route_map_sets_nh =
470 (CHECK_FLAG(vec->flags,
471 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED)
472 || CHECK_FLAG(
473 vec->flags,
474 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS));
d62a17ae 475
f041034e
DS
476 /*
477 * The logic here is rather similar to that for IPv4, the
478 * additional work being to handle 1 or 2 nexthops.
479 * Also, 3rd party nexthop is not propagated for EBGP
480 * right now.
481 */
482 switch (nhlen) {
483 case BGP_ATTR_NHLEN_IPV6_GLOBAL:
484 break;
485 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL:
486 offset_nhlocal += IPV6_MAX_BYTELEN;
487 break;
488 case BGP_ATTR_NHLEN_VPNV6_GLOBAL:
489 offset_nhglobal += 8;
490 break;
491 case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL:
492 offset_nhglobal += 8;
493 offset_nhlocal += 8 * 2 + IPV6_MAX_BYTELEN;
494 break;
495 default:
496 /* TODO: handle IPv4 nexthops */
497 flog_warn(
498 EC_BGP_INVALID_NEXTHOP_LENGTH,
499 "%s: %s: invalid MP nexthop length (AFI IP6): %u",
500 __func__, peer->host, nhlen);
501 stream_free(s);
502 return NULL;
503 }
d62a17ae 504
f041034e
DS
505 stream_get_from(&v6nhglobal, s, offset_nhglobal,
506 IPV6_MAX_BYTELEN);
1c875ddb 507
f041034e
DS
508 /*
509 * Updates to an EBGP peer should only modify the
510 * next-hop if it was set in an outbound route-map
511 * to that peer.
512 */
513 if (route_map_sets_nh
514 && ((peer->sort != BGP_PEER_EBGP)
515 || ROUTE_MAP_OUT(filter))) {
516 if (CHECK_FLAG(
517 vec->flags,
518 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS)) {
d62a17ae 519 mod_v6nhg = &peer->nexthop.v6_global;
520 gnh_modified = 1;
521 }
f041034e
DS
522 } else if (IN6_IS_ADDR_UNSPECIFIED(&v6nhglobal)) {
523 mod_v6nhg = &peer->nexthop.v6_global;
524 gnh_modified = 1;
525 } else if ((peer->sort == BGP_PEER_EBGP)
526 && (!bgp_multiaccess_check_v6(v6nhglobal, peer))
527 && !CHECK_FLAG(vec->flags,
528 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED)
529 && !peer_af_flag_check(
530 peer, nhafi, paf->safi,
531 PEER_FLAG_NEXTHOP_UNCHANGED)) {
532 /* NOTE: not handling case where NH has new AFI
533 */
534 mod_v6nhg = &peer->nexthop.v6_global;
535 gnh_modified = 1;
536 }
d62a17ae 537
92d6f769 538 if (IN6_IS_ADDR_UNSPECIFIED(mod_v6nhg)) {
3a6290bd 539 if (peer->nexthop.v4.s_addr != INADDR_ANY) {
92d6f769
K
540 ipv4_to_ipv4_mapped_ipv6(mod_v6nhg,
541 peer->nexthop.v4);
542 }
543 }
544
545 if (IS_MAPPED_IPV6(&peer->nexthop.v6_global)) {
546 mod_v6nhg = &peer->nexthop.v6_global;
547 gnh_modified = 1;
548 }
549
f041034e
DS
550 if (nhlen == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
551 || nhlen == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL) {
552 stream_get_from(&v6nhlocal, s, offset_nhlocal,
553 IPV6_MAX_BYTELEN);
554 if (IN6_IS_ADDR_UNSPECIFIED(&v6nhlocal)) {
555 mod_v6nhl = &peer->nexthop.v6_local;
556 lnh_modified = 1;
d62a17ae 557 }
f041034e 558 }
d62a17ae 559
f041034e
DS
560 if (gnh_modified)
561 stream_put_in6_addr_at(s, offset_nhglobal, mod_v6nhg);
562 if (lnh_modified)
563 stream_put_in6_addr_at(s, offset_nhlocal, mod_v6nhl);
564
565 if (bgp_debug_update(peer, NULL, NULL, 0)) {
3ddd6994
DA
566 if (nhlen == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
567 || nhlen == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL)
f041034e 568 zlog_debug(
c0d72166
DS
569 "u%" PRIu64 ":s%" PRIu64
570 " %s send UPDATE w/ mp_nexthops %pI6, %pI6%s",
f041034e
DS
571 PAF_SUBGRP(paf)->update_group->id,
572 PAF_SUBGRP(paf)->id, peer->host,
c0d72166 573 mod_v6nhg, mod_v6nhl,
3ddd6994
DA
574 (nhlen == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL
575 ? " and RD"
576 : ""));
f041034e 577 else
c0d72166
DS
578 zlog_debug(
579 "u%" PRIu64 ":s%" PRIu64
580 " %s send UPDATE w/ mp_nexthop %pI6%s",
581 PAF_SUBGRP(paf)->update_group->id,
582 PAF_SUBGRP(paf)->id, peer->host,
583 mod_v6nhg,
584 (nhlen == BGP_ATTR_NHLEN_VPNV6_GLOBAL
585 ? " and RD"
586 : ""));
f041034e
DS
587 }
588 } else if (paf->afi == AFI_L2VPN) {
589 struct in_addr v4nh, *mod_v4nh;
590 int nh_modified = 0;
591
592 stream_get_from(&v4nh, s, vec->offset + 1, 4);
593 mod_v4nh = &v4nh;
594
595 /* No route-map changes allowed for EVPN nexthops. */
596 if (v4nh.s_addr == INADDR_ANY) {
597 mod_v4nh = &peer->nexthop.v4;
598 nh_modified = 1;
d62a17ae 599 }
f041034e
DS
600
601 if (nh_modified)
602 stream_put_in_addr_at(s, vec->offset + 1, mod_v4nh);
603
604 if (bgp_debug_update(peer, NULL, NULL, 0))
23d0a753
DA
605 zlog_debug("u%" PRIu64 ":s%" PRIu64
606 " %s send UPDATE w/ nexthop %pI4",
f041034e 607 PAF_SUBGRP(paf)->update_group->id,
23d0a753 608 PAF_SUBGRP(paf)->id, peer->host, mod_v4nh);
3f9c7369 609 }
3f9c7369 610
d62a17ae 611 return s;
3f9c7369
DS
612}
613
614/*
615 * Update the vecarr offsets to go beyond 'pos' bytes, i.e. add 'pos'
616 * to each offset.
617 */
d62a17ae 618static void bpacket_attr_vec_arr_update(struct bpacket_attr_vec_arr *vecarr,
619 size_t pos)
3f9c7369 620{
d62a17ae 621 int i;
3f9c7369 622
d62a17ae 623 if (!vecarr)
624 return;
3f9c7369 625
d62a17ae 626 for (i = 0; i < BGP_ATTR_VEC_MAX; i++)
627 vecarr->entries[i].offset += pos;
3f9c7369
DS
628}
629
630/*
631 * Return if there are packets to build for this subgroup.
632 */
3dc339cd 633bool subgroup_packets_to_build(struct update_subgroup *subgrp)
3f9c7369 634{
d62a17ae 635 struct bgp_advertise *adv;
3f9c7369 636
d62a17ae 637 if (!subgrp)
3dc339cd 638 return false;
3f9c7369 639
a274fef8 640 adv = bgp_adv_fifo_first(&subgrp->sync->withdraw);
d62a17ae 641 if (adv)
3dc339cd 642 return true;
3f9c7369 643
a274fef8 644 adv = bgp_adv_fifo_first(&subgrp->sync->update);
d62a17ae 645 if (adv)
3dc339cd 646 return true;
3f9c7369 647
3dc339cd 648 return false;
3f9c7369
DS
649}
650
651/* Make BGP update packet. */
d62a17ae 652struct bpacket *subgroup_update_packet(struct update_subgroup *subgrp)
3f9c7369 653{
d62a17ae 654 struct bpacket_attr_vec_arr vecarr;
655 struct bpacket *pkt;
656 struct peer *peer;
657 struct stream *s;
658 struct stream *snlri;
659 struct stream *packet;
660 struct bgp_adj_out *adj;
661 struct bgp_advertise *adv;
9bcb3eef 662 struct bgp_dest *dest = NULL;
9b6d8fcf 663 struct bgp_path_info *path = NULL;
d62a17ae 664 bgp_size_t total_attr_len = 0;
665 unsigned long attrlen_pos = 0;
666 size_t mpattrlen_pos = 0;
667 size_t mpattr_pos = 0;
668 afi_t afi;
669 safi_t safi;
670 int space_remaining = 0;
671 int space_needed = 0;
672 char send_attr_str[BUFSIZ];
673 int send_attr_printed = 0;
674 int num_pfx = 0;
be92fc9f 675 bool addpath_capable = false;
e386c1d5 676 int addpath_overhead = 0;
d7c0a89a 677 uint32_t addpath_tx_id = 0;
d62a17ae 678 struct prefix_rd *prd = NULL;
b57ba6d2 679 mpls_label_t label = MPLS_INVALID_LABEL, *label_pnt = NULL;
d7c0a89a 680 uint32_t num_labels = 0;
d62a17ae 681
682 if (!subgrp)
683 return NULL;
3f9c7369 684
d62a17ae 685 if (bpacket_queue_is_full(SUBGRP_INST(subgrp), SUBGRP_PKTQ(subgrp)))
686 return NULL;
3f9c7369 687
d62a17ae 688 peer = SUBGRP_PEER(subgrp);
689 afi = SUBGRP_AFI(subgrp);
690 safi = SUBGRP_SAFI(subgrp);
691 s = subgrp->work;
692 stream_reset(s);
693 snlri = subgrp->scratch;
694 stream_reset(snlri);
695
696 bpacket_attr_vec_arr_reset(&vecarr);
697
be92fc9f
DA
698 addpath_capable = bgp_addpath_encode_tx(peer, afi, safi);
699 addpath_overhead = addpath_capable ? BGP_ADDPATH_ID_LEN : 0;
d62a17ae 700
a274fef8 701 adv = bgp_adv_fifo_first(&subgrp->sync->update);
d62a17ae 702 while (adv) {
9bcb3eef 703 const struct prefix *dest_p;
b54892e0 704
9bcb3eef
DS
705 assert(adv->dest);
706 dest = adv->dest;
707 dest_p = bgp_dest_get_prefix(dest);
d62a17ae 708 adj = adv->adj;
709 addpath_tx_id = adj->addpath_tx_id;
9b6d8fcf 710 path = adv->pathi;
d62a17ae 711
712 space_remaining = STREAM_CONCAT_REMAIN(s, snlri, STREAM_SIZE(s))
713 - BGP_MAX_PACKET_SIZE_OVERFLOW;
9bcb3eef
DS
714 space_needed =
715 BGP_NLRI_LENGTH + addpath_overhead
716 + bgp_packet_mpattr_prefix_size(afi, safi, dest_p);
d62a17ae 717
718 /* When remaining space can't include NLRI and it's length. */
719 if (space_remaining < space_needed)
720 break;
721
722 /* If packet is empty, set attribute. */
723 if (stream_empty(s)) {
724 struct peer *from = NULL;
725
9b6d8fcf
DS
726 if (path)
727 from = path->peer;
d62a17ae 728
729 /* 1: Write the BGP message header - 16 bytes marker, 2
730 * bytes length,
731 * one byte message type.
732 */
733 bgp_packet_set_marker(s, BGP_MSG_UPDATE);
734
735 /* 2: withdrawn routes length */
736 stream_putw(s, 0);
737
738 /* 3: total attributes length - attrlen_pos stores the
739 * position */
740 attrlen_pos = stream_get_endp(s);
741 stream_putw(s, 0);
742
743 /* 4: if there is MP_REACH_NLRI attribute, that should
744 * be the first
745 * attribute, according to
746 * draft-ietf-idr-error-handling. Save the
747 * position.
748 */
749 mpattr_pos = stream_get_endp(s);
750
751 /* 5: Encode all the attributes, except MP_REACH_NLRI
752 * attr. */
753 total_attr_len = bgp_packet_attribute(
754 NULL, peer, s, adv->baa->attr, &vecarr, NULL,
b57ba6d2 755 afi, safi, from, NULL, NULL, 0, 0, 0);
d62a17ae 756
757 space_remaining =
758 STREAM_CONCAT_REMAIN(s, snlri, STREAM_SIZE(s))
759 - BGP_MAX_PACKET_SIZE_OVERFLOW;
996c9314
LB
760 space_needed = BGP_NLRI_LENGTH + addpath_overhead
761 + bgp_packet_mpattr_prefix_size(
9bcb3eef 762 afi, safi, dest_p);
d62a17ae 763
764 /* If the attributes alone do not leave any room for
765 * NLRI then
766 * return */
767 if (space_remaining < space_needed) {
af4c2728 768 flog_err(
e50f7cfd 769 EC_BGP_UPDGRP_ATTR_LEN,
6cde4b45 770 "u%" PRIu64 ":s%" PRIu64" attributes too long, cannot send UPDATE",
d62a17ae 771 subgrp->update_group->id, subgrp->id);
772
773 /* Flush the FIFO update queue */
774 while (adv)
775 adv = bgp_advertise_clean_subgroup(
776 subgrp, adj);
777 return NULL;
778 }
779
780 if (BGP_DEBUG(update, UPDATE_OUT)
781 || BGP_DEBUG(update, UPDATE_PREFIX)) {
782 memset(send_attr_str, 0, BUFSIZ);
783 send_attr_printed = 0;
784 bgp_dump_attr(adv->baa->attr, send_attr_str,
5022c833 785 sizeof(send_attr_str));
d62a17ae 786 }
787 }
788
789 if ((afi == AFI_IP && safi == SAFI_UNICAST)
790 && !peer_cap_enhe(peer, afi, safi))
be92fc9f 791 stream_put_prefix_addpath(s, dest_p, addpath_capable,
d62a17ae 792 addpath_tx_id);
793 else {
794 /* Encode the prefix in MP_REACH_NLRI attribute */
9bcb3eef
DS
795 if (dest->pdest)
796 prd = (struct prefix_rd *)bgp_dest_get_prefix(
797 dest->pdest);
d62a17ae 798
b57ba6d2 799 if (safi == SAFI_LABELED_UNICAST) {
9bcb3eef 800 label = bgp_adv_label(dest, path, peer, afi,
d62a17ae 801 safi);
b57ba6d2
MK
802 label_pnt = &label;
803 num_labels = 1;
9b6d8fcf
DS
804 } else if (path && path->extra) {
805 label_pnt = &path->extra->label[0];
806 num_labels = path->extra->num_labels;
b57ba6d2 807 }
d62a17ae 808
809 if (stream_empty(snlri))
810 mpattrlen_pos = bgp_packet_mpattr_start(
811 snlri, peer, afi, safi, &vecarr,
812 adv->baa->attr);
813
9bcb3eef 814 bgp_packet_mpattr_prefix(snlri, afi, safi, dest_p, prd,
b57ba6d2 815 label_pnt, num_labels,
be92fc9f 816 addpath_capable, addpath_tx_id,
b57ba6d2 817 adv->baa->attr);
d62a17ae 818 }
819
820 num_pfx++;
821
9bcb3eef 822 if (bgp_debug_update(NULL, dest_p, subgrp->update_group, 0)) {
d62a17ae 823 char pfx_buf[BGP_PRD_PATH_STRLEN];
824
825 if (!send_attr_printed) {
6cde4b45 826 zlog_debug("u%" PRIu64 ":s%" PRIu64" send UPDATE w/ attr: %s",
d62a17ae 827 subgrp->update_group->id, subgrp->id,
828 send_attr_str);
829 if (!stream_empty(snlri)) {
830 iana_afi_t pkt_afi;
5c525538 831 iana_safi_t pkt_safi;
d62a17ae 832
833 pkt_afi = afi_int2iana(afi);
834 pkt_safi = safi_int2iana(safi);
835 zlog_debug(
adf086ec
DA
836 "u%" PRIu64 ":s%" PRIu64
837 " send MP_REACH for afi/safi %s/%s",
d62a17ae 838 subgrp->update_group->id,
adf086ec
DA
839 subgrp->id,
840 iana_afi2str(pkt_afi),
841 iana_safi2str(pkt_safi));
d62a17ae 842 }
843
844 send_attr_printed = 1;
845 }
846
9bcb3eef
DS
847 bgp_debug_rdpfxpath2str(afi, safi, prd, dest_p,
848 label_pnt, num_labels,
be92fc9f 849 addpath_capable, addpath_tx_id,
6c995628 850 &adv->baa->attr->evpn_overlay,
9bcb3eef 851 pfx_buf, sizeof(pfx_buf));
d62a17ae 852 zlog_debug("u%" PRIu64 ":s%" PRIu64 " send UPDATE %s",
853 subgrp->update_group->id, subgrp->id,
854 pfx_buf);
855 }
856
857 /* Synchnorize attribute. */
858 if (adj->attr)
859 bgp_attr_unintern(&adj->attr);
860 else
861 subgrp->scount++;
862
863 adj->attr = bgp_attr_intern(adv->baa->attr);
d62a17ae 864 adv = bgp_advertise_clean_subgroup(subgrp, adj);
3f9c7369
DS
865 }
866
d62a17ae 867 if (!stream_empty(s)) {
868 if (!stream_empty(snlri)) {
869 bgp_packet_mpattr_end(snlri, mpattrlen_pos);
870 total_attr_len += stream_get_endp(snlri);
871 }
872
873 /* set the total attribute length correctly */
874 stream_putw_at(s, attrlen_pos, total_attr_len);
875
876 if (!stream_empty(snlri)) {
877 packet = stream_dupcat(s, snlri, mpattr_pos);
878 bpacket_attr_vec_arr_update(&vecarr, mpattr_pos);
879 } else
880 packet = stream_dup(s);
881 bgp_packet_set_size(packet);
882 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
ef56aee4
DA
883 zlog_debug(
884 "u%" PRIu64 ":s%" PRIu64
885 " send UPDATE len %zd (max message len: %hu) numpfx %d",
886 subgrp->update_group->id, subgrp->id,
887 (stream_get_endp(packet)
888 - stream_get_getp(packet)),
889 peer->max_packet_size, num_pfx);
d62a17ae 890 pkt = bpacket_queue_add(SUBGRP_PKTQ(subgrp), packet, &vecarr);
891 stream_reset(s);
892 stream_reset(snlri);
893 return pkt;
3f9c7369 894 }
d62a17ae 895 return NULL;
3f9c7369
DS
896}
897
898/* Make BGP withdraw packet. */
899/* For ipv4 unicast:
900 16-octet marker | 2-octet length | 1-octet type |
901 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0)
902*/
903/* For other afi/safis:
904 16-octet marker | 2-octet length | 1-octet type |
905 2-octet withdrawn route length (=0) | 2-octet attrlen |
906 mp_unreach attr type | attr len | afi | safi | withdrawn prefixes
907*/
d62a17ae 908struct bpacket *subgroup_withdraw_packet(struct update_subgroup *subgrp)
3f9c7369 909{
d62a17ae 910 struct bpacket *pkt;
911 struct stream *s;
912 struct bgp_adj_out *adj;
913 struct bgp_advertise *adv;
914 struct peer *peer;
9bcb3eef 915 struct bgp_dest *dest;
d62a17ae 916 bgp_size_t unfeasible_len;
917 bgp_size_t total_attr_len;
918 size_t mp_start = 0;
919 size_t attrlen_pos = 0;
920 size_t mplen_pos = 0;
d7c0a89a 921 uint8_t first_time = 1;
d62a17ae 922 afi_t afi;
923 safi_t safi;
924 int space_remaining = 0;
925 int space_needed = 0;
926 int num_pfx = 0;
be92fc9f 927 bool addpath_capable = false;
e386c1d5 928 int addpath_overhead = 0;
d7c0a89a 929 uint32_t addpath_tx_id = 0;
b54892e0 930 const struct prefix_rd *prd = NULL;
d62a17ae 931
932
933 if (!subgrp)
934 return NULL;
3f9c7369 935
d62a17ae 936 if (bpacket_queue_is_full(SUBGRP_INST(subgrp), SUBGRP_PKTQ(subgrp)))
937 return NULL;
906ad49b 938
d62a17ae 939 peer = SUBGRP_PEER(subgrp);
940 afi = SUBGRP_AFI(subgrp);
941 safi = SUBGRP_SAFI(subgrp);
942 s = subgrp->work;
943 stream_reset(s);
be92fc9f
DA
944 addpath_capable = bgp_addpath_encode_tx(peer, afi, safi);
945 addpath_overhead = addpath_capable ? BGP_ADDPATH_ID_LEN : 0;
d62a17ae 946
a274fef8 947 while ((adv = bgp_adv_fifo_first(&subgrp->sync->withdraw)) != NULL) {
9bcb3eef 948 const struct prefix *dest_p;
b54892e0 949
9bcb3eef 950 assert(adv->dest);
d62a17ae 951 adj = adv->adj;
9bcb3eef
DS
952 dest = adv->dest;
953 dest_p = bgp_dest_get_prefix(dest);
d62a17ae 954 addpath_tx_id = adj->addpath_tx_id;
955
956 space_remaining =
2d34fb80 957 STREAM_WRITEABLE(s) - BGP_MAX_PACKET_SIZE_OVERFLOW;
9bcb3eef
DS
958 space_needed =
959 BGP_NLRI_LENGTH + addpath_overhead + BGP_TOTAL_ATTR_LEN
960 + bgp_packet_mpattr_prefix_size(afi, safi, dest_p);
d62a17ae 961
962 if (space_remaining < space_needed)
963 break;
964
965 if (stream_empty(s)) {
966 bgp_packet_set_marker(s, BGP_MSG_UPDATE);
967 stream_putw(s, 0); /* unfeasible routes length */
968 } else
969 first_time = 0;
970
971 if (afi == AFI_IP && safi == SAFI_UNICAST
972 && !peer_cap_enhe(peer, afi, safi))
be92fc9f 973 stream_put_prefix_addpath(s, dest_p, addpath_capable,
d62a17ae 974 addpath_tx_id);
975 else {
9bcb3eef
DS
976 if (dest->pdest)
977 prd = (struct prefix_rd *)bgp_dest_get_prefix(
978 dest->pdest);
d62a17ae 979
b54892e0
DS
980 /* If first time, format the MP_UNREACH header
981 */
d62a17ae 982 if (first_time) {
983 iana_afi_t pkt_afi;
5c525538 984 iana_safi_t pkt_safi;
d62a17ae 985
986 pkt_afi = afi_int2iana(afi);
987 pkt_safi = safi_int2iana(safi);
988
989 attrlen_pos = stream_get_endp(s);
b54892e0
DS
990 /* total attr length = 0 for now.
991 * reevaluate later */
d62a17ae 992 stream_putw(s, 0);
993 mp_start = stream_get_endp(s);
994 mplen_pos = bgp_packet_mpunreach_start(s, afi,
995 safi);
996 if (bgp_debug_update(NULL, NULL,
997 subgrp->update_group, 0))
998 zlog_debug(
adf086ec
DA
999 "u%" PRIu64 ":s%" PRIu64
1000 " send MP_UNREACH for afi/safi %s/%s",
d62a17ae 1001 subgrp->update_group->id,
adf086ec
DA
1002 subgrp->id,
1003 iana_afi2str(pkt_afi),
1004 iana_safi2str(pkt_safi));
d62a17ae 1005 }
1006
9bcb3eef 1007 bgp_packet_mpunreach_prefix(s, dest_p, afi, safi, prd,
be92fc9f 1008 NULL, 0, addpath_capable,
d62a17ae 1009 addpath_tx_id, NULL);
1010 }
1011
1012 num_pfx++;
1013
9bcb3eef 1014 if (bgp_debug_update(NULL, dest_p, subgrp->update_group, 0)) {
d62a17ae 1015 char pfx_buf[BGP_PRD_PATH_STRLEN];
1016
9bcb3eef 1017 bgp_debug_rdpfxpath2str(afi, safi, prd, dest_p, NULL, 0,
be92fc9f 1018 addpath_capable, addpath_tx_id,
6c995628 1019 NULL, pfx_buf, sizeof(pfx_buf));
6cde4b45 1020 zlog_debug("u%" PRIu64 ":s%" PRIu64" send UPDATE %s -- unreachable",
d62a17ae 1021 subgrp->update_group->id, subgrp->id,
1022 pfx_buf);
1023 }
1024
1025 subgrp->scount--;
1026
9bcb3eef 1027 bgp_adj_out_remove_subgroup(dest, adj, subgrp);
3f9c7369
DS
1028 }
1029
d62a17ae 1030 if (!stream_empty(s)) {
1031 if (afi == AFI_IP && safi == SAFI_UNICAST
1032 && !peer_cap_enhe(peer, afi, safi)) {
1033 unfeasible_len = stream_get_endp(s) - BGP_HEADER_SIZE
1034 - BGP_UNFEASIBLE_LEN;
1035 stream_putw_at(s, BGP_HEADER_SIZE, unfeasible_len);
1036 stream_putw(s, 0);
1037 } else {
1038 /* Set the mp_unreach attr's length */
1039 bgp_packet_mpunreach_end(s, mplen_pos);
1040
1041 /* Set total path attribute length. */
1042 total_attr_len = stream_get_endp(s) - mp_start;
1043 stream_putw_at(s, attrlen_pos, total_attr_len);
1044 }
1045 bgp_packet_set_size(s);
1046 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
6cde4b45 1047 zlog_debug("u%" PRIu64 ":s%" PRIu64" send UPDATE (withdraw) len %zd numpfx %d",
d62a17ae 1048 subgrp->update_group->id, subgrp->id,
1049 (stream_get_endp(s) - stream_get_getp(s)),
1050 num_pfx);
1051 pkt = bpacket_queue_add(SUBGRP_PKTQ(subgrp), stream_dup(s),
1052 NULL);
1053 stream_reset(s);
1054 return pkt;
3f9c7369 1055 }
3f9c7369 1056
d62a17ae 1057 return NULL;
3f9c7369
DS
1058}
1059
d62a17ae 1060void subgroup_default_update_packet(struct update_subgroup *subgrp,
1061 struct attr *attr, struct peer *from)
3f9c7369 1062{
d62a17ae 1063 struct stream *s;
1064 struct peer *peer;
1065 struct prefix p;
1066 unsigned long pos;
1067 bgp_size_t total_attr_len;
1068 afi_t afi;
1069 safi_t safi;
1070 struct bpacket_attr_vec_arr vecarr;
be92fc9f 1071 bool addpath_capable = false;
d62a17ae 1072
1073 if (DISABLE_BGP_ANNOUNCE)
1074 return;
1075
1076 if (!subgrp)
1077 return;
1078
1079 peer = SUBGRP_PEER(subgrp);
1080 afi = SUBGRP_AFI(subgrp);
1081 safi = SUBGRP_SAFI(subgrp);
1082 bpacket_attr_vec_arr_reset(&vecarr);
be92fc9f 1083 addpath_capable = bgp_addpath_encode_tx(peer, afi, safi);
d62a17ae 1084
cbb65f5e
RW
1085 memset(&p, 0, sizeof(p));
1086 p.family = afi2family(afi);
1087 p.prefixlen = 0;
d62a17ae 1088
1089 /* Logging the attribute. */
1090 if (bgp_debug_update(NULL, &p, subgrp->update_group, 0)) {
1091 char attrstr[BUFSIZ];
d62a17ae 1092 /* ' with addpath ID ' 17
1093 * max strlen of uint32 + 10
1094 * +/- (just in case) + 1
1095 * null terminator + 1
1096 * ============================ 29 */
1097 char tx_id_buf[30];
1098
1099 attrstr[0] = '\0';
1100
5022c833 1101 bgp_dump_attr(attr, attrstr, sizeof(attrstr));
d62a17ae 1102
be92fc9f 1103 if (addpath_capable)
d62a17ae 1104 snprintf(tx_id_buf, sizeof(tx_id_buf),
1105 " with addpath ID %u",
1106 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
cbb263cf
DS
1107 else
1108 tx_id_buf[0] = '\0';
d62a17ae 1109
2dbe669b
DA
1110 zlog_debug("u%" PRIu64 ":s%" PRIu64 " send UPDATE %pFX%s %s",
1111 (SUBGRP_UPDGRP(subgrp))->id, subgrp->id, &p,
1112 tx_id_buf, attrstr);
d62a17ae 1113 }
3f9c7369 1114
ef56aee4 1115 s = stream_new(peer->max_packet_size);
3f9c7369 1116
d62a17ae 1117 /* Make BGP update packet. */
1118 bgp_packet_set_marker(s, BGP_MSG_UPDATE);
3f9c7369 1119
d62a17ae 1120 /* Unfeasible Routes Length. */
1121 stream_putw(s, 0);
3f9c7369 1122
d62a17ae 1123 /* Make place for total attribute length. */
1124 pos = stream_get_endp(s);
1125 stream_putw(s, 0);
1126 total_attr_len = bgp_packet_attribute(
1127 NULL, peer, s, attr, &vecarr, &p, afi, safi, from, NULL, NULL,
be92fc9f 1128 0, addpath_capable, BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
3f9c7369 1129
d62a17ae 1130 /* Set Total Path Attribute Length. */
1131 stream_putw_at(s, pos, total_attr_len);
3f9c7369 1132
d62a17ae 1133 /* NLRI set. */
1134 if (p.family == AF_INET && safi == SAFI_UNICAST
1135 && !peer_cap_enhe(peer, afi, safi))
1136 stream_put_prefix_addpath(
be92fc9f 1137 s, &p, addpath_capable,
d62a17ae 1138 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
3f9c7369 1139
d62a17ae 1140 /* Set size. */
1141 bgp_packet_set_size(s);
3f9c7369 1142
d62a17ae 1143 (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp), s, &vecarr);
2fc102e1 1144 subgroup_trigger_write(subgrp);
fea96c55 1145 subgrp->scount++;
3f9c7369
DS
1146}
1147
d62a17ae 1148void subgroup_default_withdraw_packet(struct update_subgroup *subgrp)
3f9c7369 1149{
d62a17ae 1150 struct peer *peer;
1151 struct stream *s;
1152 struct prefix p;
1153 unsigned long attrlen_pos = 0;
1154 unsigned long cp;
1155 bgp_size_t unfeasible_len;
1156 bgp_size_t total_attr_len = 0;
1157 size_t mp_start = 0;
1158 size_t mplen_pos = 0;
1159 afi_t afi;
1160 safi_t safi;
be92fc9f 1161 bool addpath_capable = false;
d62a17ae 1162
1163 if (DISABLE_BGP_ANNOUNCE)
1164 return;
1165
1166 peer = SUBGRP_PEER(subgrp);
1167 afi = SUBGRP_AFI(subgrp);
1168 safi = SUBGRP_SAFI(subgrp);
be92fc9f 1169 addpath_capable = bgp_addpath_encode_tx(peer, afi, safi);
d62a17ae 1170
cbb65f5e
RW
1171 memset(&p, 0, sizeof(p));
1172 p.family = afi2family(afi);
1173 p.prefixlen = 0;
d62a17ae 1174
1175 if (bgp_debug_update(NULL, &p, subgrp->update_group, 0)) {
d62a17ae 1176 /* ' with addpath ID ' 17
1177 * max strlen of uint32 + 10
1178 * +/- (just in case) + 1
1179 * null terminator + 1
1180 * ============================ 29 */
1181 char tx_id_buf[30];
1182
be92fc9f 1183 if (addpath_capable)
d62a17ae 1184 snprintf(tx_id_buf, sizeof(tx_id_buf),
1185 " with addpath ID %u",
1186 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
1187
2dbe669b
DA
1188 zlog_debug("u%" PRIu64 ":s%" PRIu64
1189 " send UPDATE %pFX%s -- unreachable",
1190 (SUBGRP_UPDGRP(subgrp))->id, subgrp->id, &p,
1191 tx_id_buf);
d62a17ae 1192 }
3f9c7369 1193
ef56aee4 1194 s = stream_new(peer->max_packet_size);
3f9c7369 1195
d62a17ae 1196 /* Make BGP update packet. */
1197 bgp_packet_set_marker(s, BGP_MSG_UPDATE);
3f9c7369 1198
d62a17ae 1199 /* Unfeasible Routes Length. */;
1200 cp = stream_get_endp(s);
1201 stream_putw(s, 0);
3f9c7369 1202
d62a17ae 1203 /* Withdrawn Routes. */
1204 if (p.family == AF_INET && safi == SAFI_UNICAST
1205 && !peer_cap_enhe(peer, afi, safi)) {
1206 stream_put_prefix_addpath(
be92fc9f 1207 s, &p, addpath_capable,
d62a17ae 1208 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
3f9c7369 1209
d62a17ae 1210 unfeasible_len = stream_get_endp(s) - cp - 2;
3f9c7369 1211
d62a17ae 1212 /* Set unfeasible len. */
1213 stream_putw_at(s, cp, unfeasible_len);
3f9c7369 1214
d62a17ae 1215 /* Set total path attribute length. */
1216 stream_putw(s, 0);
1217 } else {
1218 attrlen_pos = stream_get_endp(s);
1219 stream_putw(s, 0);
1220 mp_start = stream_get_endp(s);
1221 mplen_pos = bgp_packet_mpunreach_start(s, afi, safi);
1222 bgp_packet_mpunreach_prefix(
be92fc9f 1223 s, &p, afi, safi, NULL, NULL, 0, addpath_capable,
d62a17ae 1224 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE, NULL);
3f9c7369 1225
d62a17ae 1226 /* Set the mp_unreach attr's length */
1227 bgp_packet_mpunreach_end(s, mplen_pos);
3f9c7369 1228
d62a17ae 1229 /* Set total path attribute length. */
1230 total_attr_len = stream_get_endp(s) - mp_start;
1231 stream_putw_at(s, attrlen_pos, total_attr_len);
1232 }
1233
1234 bgp_packet_set_size(s);
1235
1236 (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp), s, NULL);
2fc102e1 1237 subgroup_trigger_write(subgrp);
fea96c55 1238 subgrp->scount--;
3f9c7369
DS
1239}
1240
1241static void
d62a17ae 1242bpacket_vec_arr_inherit_attr_flags(struct bpacket_attr_vec_arr *vecarr,
e8e36ff3 1243 enum bpacket_attr_vec_type type,
d62a17ae 1244 struct attr *attr)
3f9c7369 1245{
d62a17ae 1246 if (CHECK_FLAG(attr->rmap_change_flags,
1247 BATTR_RMAP_NEXTHOP_PEER_ADDRESS))
1248 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1249 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS);
1250
1251 if (CHECK_FLAG(attr->rmap_change_flags, BATTR_REFLECTED))
1252 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1253 BPKT_ATTRVEC_FLAGS_REFLECTED);
1254
1255 if (CHECK_FLAG(attr->rmap_change_flags, BATTR_RMAP_NEXTHOP_UNCHANGED))
1256 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1257 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED);
1258
1259 if (CHECK_FLAG(attr->rmap_change_flags, BATTR_RMAP_IPV4_NHOP_CHANGED))
1260 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1261 BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED);
1262
1263 if (CHECK_FLAG(attr->rmap_change_flags,
1264 BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED))
1265 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1266 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED);
1267
1268 if (CHECK_FLAG(attr->rmap_change_flags,
1269 BATTR_RMAP_IPV6_LL_NHOP_CHANGED))
1270 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1271 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_LNH_CHANGED);
3f9c7369
DS
1272}
1273
1274/* Reset the Attributes vector array. The vector array is used to override
1275 * certain output parameters in the packet for a particular peer
1276 */
d62a17ae 1277void bpacket_attr_vec_arr_reset(struct bpacket_attr_vec_arr *vecarr)
3f9c7369 1278{
d62a17ae 1279 int i;
3f9c7369 1280
d62a17ae 1281 if (!vecarr)
1282 return;
3f9c7369 1283
d62a17ae 1284 i = 0;
1285 while (i < BGP_ATTR_VEC_MAX) {
1286 vecarr->entries[i].flags = 0;
1287 vecarr->entries[i].offset = 0;
1288 i++;
1289 }
3f9c7369
DS
1290}
1291
1292/* Setup a particular node entry in the vecarr */
d62a17ae 1293void bpacket_attr_vec_arr_set_vec(struct bpacket_attr_vec_arr *vecarr,
e8e36ff3
DA
1294 enum bpacket_attr_vec_type type,
1295 struct stream *s, struct attr *attr)
3f9c7369 1296{
d62a17ae 1297 if (!vecarr)
1298 return;
1299 assert(type < BGP_ATTR_VEC_MAX);
1300
1301 SET_FLAG(vecarr->entries[type].flags, BPKT_ATTRVEC_FLAGS_UPDATED);
1302 vecarr->entries[type].offset = stream_get_endp(s);
1303 if (attr)
1304 bpacket_vec_arr_inherit_attr_flags(vecarr, type, attr);
3f9c7369 1305}