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