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