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