]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_updgrp_packet.c
Merge pull request #3007 from pacovn/static_analysis__shadow_variables2
[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"
3f9c7369
DS
59
60/********************
61 * PRIVATE FUNCTIONS
62 ********************/
63
64/********************
65 * PUBLIC FUNCTIONS
66 ********************/
d62a17ae 67struct bpacket *bpacket_alloc()
3f9c7369 68{
d62a17ae 69 struct bpacket *pkt;
3f9c7369 70
d62a17ae 71 pkt = (struct bpacket *)XCALLOC(MTYPE_BGP_PACKET,
72 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];
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)) {
d7c0a89a 400 uint8_t nhlen;
a2b6e694 401 afi_t nhafi;
d62a17ae 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 */
ade6974d
QY
430 flog_warn(
431 BGP_WARN_INVALID_NEXTHOP_LENGTH,
432 "%s: %s: invalid MP nexthop length (AFI IP): %u",
433 __func__, peer->host, nhlen);
d62a17ae 434 stream_free(s);
435 return NULL;
436 }
437
438 stream_get_from(&v4nh, s, offset_nh, IPV4_MAX_BYTELEN);
439 mod_v4nh = &v4nh;
440
441 /*
442 * If route-map has set the nexthop, that is always
443 * used; if it is
444 * specified as peer-address, the peering address is
445 * picked up.
446 * Otherwise, if NH is unavailable from attribute, the
447 * peering addr
448 * is picked up; the "NH unavailable" case also covers
449 * next-hop-self
450 * and some other scenarios -- see
451 * subgroup_announce_check(). In
452 * all other cases, use the nexthop carried in the
453 * attribute unless
454 * it is EBGP non-multiaccess and there is no
455 * next-hop-unchanged setting.
456 * Note: It is assumed route-map cannot set the nexthop
457 * to an
458 * invalid value.
459 */
460 if (route_map_sets_nh) {
461 if (CHECK_FLAG(
462 vec->flags,
463 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS)) {
464 mod_v4nh = &peer->nexthop.v4;
465 nh_modified = 1;
466 }
467 } else if (!v4nh.s_addr) {
468 mod_v4nh = &peer->nexthop.v4;
469 nh_modified = 1;
470 } else if (
471 peer->sort == BGP_PEER_EBGP
d62a17ae 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(
ace295a9 477 peer, paf->afi, paf->safi,
d62a17ae 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 */
ade6974d
QY
536 flog_warn(
537 BGP_WARN_INVALID_NEXTHOP_LENGTH,
538 "%s: %s: invalid MP nexthop length (AFI IP6): %u",
539 __func__, peer->host, nhlen);
d62a17ae 540 stream_free(s);
541 return NULL;
542 }
543
544 stream_get_from(&v6nhglobal, s, offset_nhglobal,
545 IPV6_MAX_BYTELEN);
546 if (route_map_sets_nh) {
547 if (CHECK_FLAG(
548 vec->flags,
549 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS)) {
550 mod_v6nhg = &peer->nexthop.v6_global;
551 gnh_modified = 1;
552 }
553 } else if (IN6_IS_ADDR_UNSPECIFIED(&v6nhglobal)) {
554 mod_v6nhg = &peer->nexthop.v6_global;
555 gnh_modified = 1;
556 } else if (
557 peer->sort == BGP_PEER_EBGP
558 && !CHECK_FLAG(
559 vec->flags,
560 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED)
561 && !peer_af_flag_check(
562 peer, nhafi, paf->safi,
563 PEER_FLAG_NEXTHOP_UNCHANGED)) {
564 /* NOTE: not handling case where NH has new AFI
565 */
566 mod_v6nhg = &peer->nexthop.v6_global;
567 gnh_modified = 1;
568 }
569
570
571 if (nhlen == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL
572 || nhlen == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL) {
573 stream_get_from(&v6nhlocal, s, offset_nhlocal,
574 IPV6_MAX_BYTELEN);
575 if (IN6_IS_ADDR_UNSPECIFIED(&v6nhlocal)) {
576 mod_v6nhl = &peer->nexthop.v6_local;
577 lnh_modified = 1;
578 }
579 }
580
581 if (gnh_modified)
582 stream_put_in6_addr_at(s, offset_nhglobal,
583 mod_v6nhg);
584 if (lnh_modified)
585 stream_put_in6_addr_at(s, offset_nhlocal,
586 mod_v6nhl);
587
588 if (bgp_debug_update(peer, NULL, NULL, 0)) {
589 if (nhlen == 32 || nhlen == 48)
590 zlog_debug(
591 "u%" PRIu64 ":s%" PRIu64
592 " %s send UPDATE w/ mp_nexthops %s, %s%s",
593 PAF_SUBGRP(paf)
594 ->update_group->id,
595 PAF_SUBGRP(paf)->id, peer->host,
596 inet_ntop(AF_INET6, mod_v6nhg,
597 buf, BUFSIZ),
598 inet_ntop(AF_INET6, mod_v6nhl,
599 buf2, BUFSIZ),
600 (nhlen == 48 ? " and RD" : ""));
601 else
602 zlog_debug(
603 "u%" PRIu64 ":s%" PRIu64
604 " %s send UPDATE w/ mp_nexthop %s%s",
605 PAF_SUBGRP(paf)
606 ->update_group->id,
607 PAF_SUBGRP(paf)->id, peer->host,
608 inet_ntop(AF_INET6, mod_v6nhg,
609 buf, BUFSIZ),
610 (nhlen == 24 ? " and RD" : ""));
611 }
612 } else if (paf->afi == AFI_L2VPN) {
613 struct in_addr v4nh, *mod_v4nh;
614 int nh_modified = 0;
615
616 stream_get_from(&v4nh, s, vec->offset + 1, 4);
617 mod_v4nh = &v4nh;
618
619 /* No route-map changes allowed for EVPN nexthops. */
620 if (!v4nh.s_addr) {
621 mod_v4nh = &peer->nexthop.v4;
622 nh_modified = 1;
623 }
624
625 if (nh_modified)
626 stream_put_in_addr_at(s, vec->offset + 1,
627 mod_v4nh);
628
629 if (bgp_debug_update(peer, NULL, NULL, 0))
630 zlog_debug("u%" PRIu64 ":s%" PRIu64
631 " %s send UPDATE w/ nexthop %s",
632 PAF_SUBGRP(paf)->update_group->id,
633 PAF_SUBGRP(paf)->id, peer->host,
634 inet_ntoa(*mod_v4nh));
635 }
3f9c7369 636 }
3f9c7369 637
d62a17ae 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;
d7c0a89a 704 uint32_t addpath_tx_id = 0;
d62a17ae 705 struct prefix_rd *prd = NULL;
b57ba6d2 706 mpls_label_t label = MPLS_INVALID_LABEL, *label_pnt = NULL;
d7c0a89a 707 uint32_t num_labels = 0;
d62a17ae 708
709 if (!subgrp)
710 return NULL;
3f9c7369 711
d62a17ae 712 if (bpacket_queue_is_full(SUBGRP_INST(subgrp), SUBGRP_PKTQ(subgrp)))
713 return NULL;
3f9c7369 714
d62a17ae 715 peer = SUBGRP_PEER(subgrp);
716 afi = SUBGRP_AFI(subgrp);
717 safi = SUBGRP_SAFI(subgrp);
718 s = subgrp->work;
719 stream_reset(s);
720 snlri = subgrp->scratch;
721 stream_reset(snlri);
722
723 bpacket_attr_vec_arr_reset(&vecarr);
724
725 addpath_encode = bgp_addpath_encode_tx(peer, afi, safi);
e386c1d5 726 addpath_overhead = addpath_encode ? BGP_ADDPATH_ID_LEN : 0;
d62a17ae 727
728 adv = BGP_ADV_FIFO_HEAD(&subgrp->sync->update);
729 while (adv) {
730 assert(adv->rn);
731 rn = adv->rn;
732 adj = adv->adj;
733 addpath_tx_id = adj->addpath_tx_id;
734 binfo = adv->binfo;
735
736 space_remaining = STREAM_CONCAT_REMAIN(s, snlri, STREAM_SIZE(s))
737 - BGP_MAX_PACKET_SIZE_OVERFLOW;
996c9314
LB
738 space_needed =
739 BGP_NLRI_LENGTH + addpath_overhead
740 + bgp_packet_mpattr_prefix_size(afi, safi, &rn->p);
d62a17ae 741
742 /* When remaining space can't include NLRI and it's length. */
743 if (space_remaining < space_needed)
744 break;
745
746 /* If packet is empty, set attribute. */
747 if (stream_empty(s)) {
748 struct peer *from = NULL;
749
750 if (binfo)
751 from = binfo->peer;
752
753 /* 1: Write the BGP message header - 16 bytes marker, 2
754 * bytes length,
755 * one byte message type.
756 */
757 bgp_packet_set_marker(s, BGP_MSG_UPDATE);
758
759 /* 2: withdrawn routes length */
760 stream_putw(s, 0);
761
762 /* 3: total attributes length - attrlen_pos stores the
763 * position */
764 attrlen_pos = stream_get_endp(s);
765 stream_putw(s, 0);
766
767 /* 4: if there is MP_REACH_NLRI attribute, that should
768 * be the first
769 * attribute, according to
770 * draft-ietf-idr-error-handling. Save the
771 * position.
772 */
773 mpattr_pos = stream_get_endp(s);
774
775 /* 5: Encode all the attributes, except MP_REACH_NLRI
776 * attr. */
777 total_attr_len = bgp_packet_attribute(
778 NULL, peer, s, adv->baa->attr, &vecarr, NULL,
b57ba6d2 779 afi, safi, from, NULL, NULL, 0, 0, 0);
d62a17ae 780
781 space_remaining =
782 STREAM_CONCAT_REMAIN(s, snlri, STREAM_SIZE(s))
783 - BGP_MAX_PACKET_SIZE_OVERFLOW;
996c9314
LB
784 space_needed = BGP_NLRI_LENGTH + addpath_overhead
785 + bgp_packet_mpattr_prefix_size(
786 afi, safi, &rn->p);
d62a17ae 787
788 /* If the attributes alone do not leave any room for
789 * NLRI then
790 * return */
791 if (space_remaining < space_needed) {
af4c2728 792 flog_err(
14454c9f 793 BGP_ERR_UPDGRP_ATTR_LEN,
d62a17ae 794 "u%" PRIu64 ":s%" PRIu64
795 " attributes too long, cannot send UPDATE",
796 subgrp->update_group->id, subgrp->id);
797
798 /* Flush the FIFO update queue */
799 while (adv)
800 adv = bgp_advertise_clean_subgroup(
801 subgrp, adj);
802 return NULL;
803 }
804
805 if (BGP_DEBUG(update, UPDATE_OUT)
806 || BGP_DEBUG(update, UPDATE_PREFIX)) {
807 memset(send_attr_str, 0, BUFSIZ);
808 send_attr_printed = 0;
809 bgp_dump_attr(adv->baa->attr, send_attr_str,
810 BUFSIZ);
811 }
812 }
813
814 if ((afi == AFI_IP && safi == SAFI_UNICAST)
815 && !peer_cap_enhe(peer, afi, safi))
816 stream_put_prefix_addpath(s, &rn->p, addpath_encode,
817 addpath_tx_id);
818 else {
819 /* Encode the prefix in MP_REACH_NLRI attribute */
820 if (rn->prn)
821 prd = (struct prefix_rd *)&rn->prn->p;
822
b57ba6d2 823 if (safi == SAFI_LABELED_UNICAST) {
d62a17ae 824 label = bgp_adv_label(rn, binfo, peer, afi,
825 safi);
b57ba6d2
MK
826 label_pnt = &label;
827 num_labels = 1;
828 } else if (binfo && binfo->extra) {
829 label_pnt = &binfo->extra->label[0];
830 num_labels = binfo->extra->num_labels;
831 }
d62a17ae 832
833 if (stream_empty(snlri))
834 mpattrlen_pos = bgp_packet_mpattr_start(
835 snlri, peer, afi, safi, &vecarr,
836 adv->baa->attr);
837
838 bgp_packet_mpattr_prefix(snlri, afi, safi, &rn->p, prd,
b57ba6d2
MK
839 label_pnt, num_labels,
840 addpath_encode, addpath_tx_id,
841 adv->baa->attr);
d62a17ae 842 }
843
844 num_pfx++;
845
846 if (bgp_debug_update(NULL, &rn->p, subgrp->update_group, 0)) {
847 char pfx_buf[BGP_PRD_PATH_STRLEN];
848
849 if (!send_attr_printed) {
850 zlog_debug("u%" PRIu64 ":s%" PRIu64
851 " send UPDATE w/ attr: %s",
852 subgrp->update_group->id, subgrp->id,
853 send_attr_str);
854 if (!stream_empty(snlri)) {
855 iana_afi_t pkt_afi;
5c525538 856 iana_safi_t pkt_safi;
d62a17ae 857
858 pkt_afi = afi_int2iana(afi);
859 pkt_safi = safi_int2iana(safi);
860 zlog_debug(
861 "u%" PRIu64 ":s%" PRIu64
862 " send MP_REACH for afi/safi %d/%d",
863 subgrp->update_group->id,
864 subgrp->id, pkt_afi, pkt_safi);
865 }
866
867 send_attr_printed = 1;
868 }
869
b57ba6d2
MK
870 bgp_debug_rdpfxpath2str(afi, safi, prd, &rn->p,
871 label_pnt, num_labels,
d62a17ae 872 addpath_encode, addpath_tx_id,
873 pfx_buf, sizeof(pfx_buf));
874 zlog_debug("u%" PRIu64 ":s%" PRIu64 " send UPDATE %s",
875 subgrp->update_group->id, subgrp->id,
876 pfx_buf);
877 }
878
879 /* Synchnorize attribute. */
880 if (adj->attr)
881 bgp_attr_unintern(&adj->attr);
882 else
883 subgrp->scount++;
884
885 adj->attr = bgp_attr_intern(adv->baa->attr);
886
887 adv = bgp_advertise_clean_subgroup(subgrp, adj);
3f9c7369
DS
888 }
889
d62a17ae 890 if (!stream_empty(s)) {
891 if (!stream_empty(snlri)) {
892 bgp_packet_mpattr_end(snlri, mpattrlen_pos);
893 total_attr_len += stream_get_endp(snlri);
894 }
895
896 /* set the total attribute length correctly */
897 stream_putw_at(s, attrlen_pos, total_attr_len);
898
899 if (!stream_empty(snlri)) {
900 packet = stream_dupcat(s, snlri, mpattr_pos);
901 bpacket_attr_vec_arr_update(&vecarr, mpattr_pos);
902 } else
903 packet = stream_dup(s);
904 bgp_packet_set_size(packet);
905 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
906 zlog_debug("u%" PRIu64 ":s%" PRIu64
907 " send UPDATE len %zd numpfx %d",
908 subgrp->update_group->id, subgrp->id,
909 (stream_get_endp(packet)
910 - stream_get_getp(packet)),
911 num_pfx);
912 pkt = bpacket_queue_add(SUBGRP_PKTQ(subgrp), packet, &vecarr);
913 stream_reset(s);
914 stream_reset(snlri);
915 return pkt;
3f9c7369 916 }
d62a17ae 917 return NULL;
3f9c7369
DS
918}
919
920/* Make BGP withdraw packet. */
921/* For ipv4 unicast:
922 16-octet marker | 2-octet length | 1-octet type |
923 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0)
924*/
925/* For other afi/safis:
926 16-octet marker | 2-octet length | 1-octet type |
927 2-octet withdrawn route length (=0) | 2-octet attrlen |
928 mp_unreach attr type | attr len | afi | safi | withdrawn prefixes
929*/
d62a17ae 930struct bpacket *subgroup_withdraw_packet(struct update_subgroup *subgrp)
3f9c7369 931{
d62a17ae 932 struct bpacket *pkt;
933 struct stream *s;
934 struct bgp_adj_out *adj;
935 struct bgp_advertise *adv;
936 struct peer *peer;
937 struct bgp_node *rn;
938 bgp_size_t unfeasible_len;
939 bgp_size_t total_attr_len;
940 size_t mp_start = 0;
941 size_t attrlen_pos = 0;
942 size_t mplen_pos = 0;
d7c0a89a 943 uint8_t first_time = 1;
d62a17ae 944 afi_t afi;
945 safi_t safi;
946 int space_remaining = 0;
947 int space_needed = 0;
948 int num_pfx = 0;
949 int addpath_encode = 0;
e386c1d5 950 int addpath_overhead = 0;
d7c0a89a 951 uint32_t addpath_tx_id = 0;
d62a17ae 952 struct prefix_rd *prd = NULL;
953
954
955 if (!subgrp)
956 return NULL;
3f9c7369 957
d62a17ae 958 if (bpacket_queue_is_full(SUBGRP_INST(subgrp), SUBGRP_PKTQ(subgrp)))
959 return NULL;
906ad49b 960
d62a17ae 961 peer = SUBGRP_PEER(subgrp);
962 afi = SUBGRP_AFI(subgrp);
963 safi = SUBGRP_SAFI(subgrp);
964 s = subgrp->work;
965 stream_reset(s);
966 addpath_encode = bgp_addpath_encode_tx(peer, afi, safi);
e386c1d5 967 addpath_overhead = addpath_encode ? BGP_ADDPATH_ID_LEN : 0;
d62a17ae 968
969 while ((adv = BGP_ADV_FIFO_HEAD(&subgrp->sync->withdraw)) != NULL) {
970 assert(adv->rn);
971 adj = adv->adj;
972 rn = adv->rn;
973 addpath_tx_id = adj->addpath_tx_id;
974
975 space_remaining =
2d34fb80 976 STREAM_WRITEABLE(s) - BGP_MAX_PACKET_SIZE_OVERFLOW;
d62a17ae 977 space_needed =
e386c1d5 978 BGP_NLRI_LENGTH + addpath_overhead + BGP_TOTAL_ATTR_LEN
d62a17ae 979 + bgp_packet_mpattr_prefix_size(afi, safi, &rn->p);
980
981 if (space_remaining < space_needed)
982 break;
983
984 if (stream_empty(s)) {
985 bgp_packet_set_marker(s, BGP_MSG_UPDATE);
986 stream_putw(s, 0); /* unfeasible routes length */
987 } else
988 first_time = 0;
989
990 if (afi == AFI_IP && safi == SAFI_UNICAST
991 && !peer_cap_enhe(peer, afi, safi))
992 stream_put_prefix_addpath(s, &rn->p, addpath_encode,
993 addpath_tx_id);
994 else {
995 if (rn->prn)
996 prd = (struct prefix_rd *)&rn->prn->p;
997
998 /* If first time, format the MP_UNREACH header */
999 if (first_time) {
1000 iana_afi_t pkt_afi;
5c525538 1001 iana_safi_t pkt_safi;
d62a17ae 1002
1003 pkt_afi = afi_int2iana(afi);
1004 pkt_safi = safi_int2iana(safi);
1005
1006 attrlen_pos = stream_get_endp(s);
1007 /* total attr length = 0 for now. reevaluate
1008 * later */
1009 stream_putw(s, 0);
1010 mp_start = stream_get_endp(s);
1011 mplen_pos = bgp_packet_mpunreach_start(s, afi,
1012 safi);
1013 if (bgp_debug_update(NULL, NULL,
1014 subgrp->update_group, 0))
1015 zlog_debug(
1016 "u%" PRIu64 ":s%" PRIu64
1017 " send MP_UNREACH for afi/safi %d/%d",
1018 subgrp->update_group->id,
1019 subgrp->id, pkt_afi, pkt_safi);
1020 }
1021
1022 bgp_packet_mpunreach_prefix(s, &rn->p, afi, safi, prd,
b57ba6d2 1023 NULL, 0, addpath_encode,
d62a17ae 1024 addpath_tx_id, NULL);
1025 }
1026
1027 num_pfx++;
1028
1029 if (bgp_debug_update(NULL, &rn->p, subgrp->update_group, 0)) {
1030 char pfx_buf[BGP_PRD_PATH_STRLEN];
1031
b57ba6d2 1032 bgp_debug_rdpfxpath2str(afi, safi, prd, &rn->p, NULL, 0,
d62a17ae 1033 addpath_encode, addpath_tx_id,
1034 pfx_buf, sizeof(pfx_buf));
1035 zlog_debug("u%" PRIu64 ":s%" PRIu64
1036 " send UPDATE %s -- unreachable",
1037 subgrp->update_group->id, subgrp->id,
1038 pfx_buf);
1039 }
1040
1041 subgrp->scount--;
1042
1043 bgp_adj_out_remove_subgroup(rn, adj, subgrp);
1044 bgp_unlock_node(rn);
3f9c7369
DS
1045 }
1046
d62a17ae 1047 if (!stream_empty(s)) {
1048 if (afi == AFI_IP && safi == SAFI_UNICAST
1049 && !peer_cap_enhe(peer, afi, safi)) {
1050 unfeasible_len = stream_get_endp(s) - BGP_HEADER_SIZE
1051 - BGP_UNFEASIBLE_LEN;
1052 stream_putw_at(s, BGP_HEADER_SIZE, unfeasible_len);
1053 stream_putw(s, 0);
1054 } else {
1055 /* Set the mp_unreach attr's length */
1056 bgp_packet_mpunreach_end(s, mplen_pos);
1057
1058 /* Set total path attribute length. */
1059 total_attr_len = stream_get_endp(s) - mp_start;
1060 stream_putw_at(s, attrlen_pos, total_attr_len);
1061 }
1062 bgp_packet_set_size(s);
1063 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
1064 zlog_debug("u%" PRIu64 ":s%" PRIu64
1065 " send UPDATE (withdraw) len %zd numpfx %d",
1066 subgrp->update_group->id, subgrp->id,
1067 (stream_get_endp(s) - stream_get_getp(s)),
1068 num_pfx);
1069 pkt = bpacket_queue_add(SUBGRP_PKTQ(subgrp), stream_dup(s),
1070 NULL);
1071 stream_reset(s);
1072 return pkt;
3f9c7369 1073 }
3f9c7369 1074
d62a17ae 1075 return NULL;
3f9c7369
DS
1076}
1077
d62a17ae 1078void subgroup_default_update_packet(struct update_subgroup *subgrp,
1079 struct attr *attr, struct peer *from)
3f9c7369 1080{
d62a17ae 1081 struct stream *s;
1082 struct peer *peer;
1083 struct prefix p;
1084 unsigned long pos;
1085 bgp_size_t total_attr_len;
1086 afi_t afi;
1087 safi_t safi;
1088 struct bpacket_attr_vec_arr vecarr;
1089 int addpath_encode = 0;
1090
1091 if (DISABLE_BGP_ANNOUNCE)
1092 return;
1093
1094 if (!subgrp)
1095 return;
1096
1097 peer = SUBGRP_PEER(subgrp);
1098 afi = SUBGRP_AFI(subgrp);
1099 safi = SUBGRP_SAFI(subgrp);
1100 bpacket_attr_vec_arr_reset(&vecarr);
1101 addpath_encode = bgp_addpath_encode_tx(peer, afi, safi);
1102
cbb65f5e
RW
1103 memset(&p, 0, sizeof(p));
1104 p.family = afi2family(afi);
1105 p.prefixlen = 0;
d62a17ae 1106
1107 /* Logging the attribute. */
1108 if (bgp_debug_update(NULL, &p, subgrp->update_group, 0)) {
1109 char attrstr[BUFSIZ];
1110 char buf[PREFIX_STRLEN];
1111 /* ' with addpath ID ' 17
1112 * max strlen of uint32 + 10
1113 * +/- (just in case) + 1
1114 * null terminator + 1
1115 * ============================ 29 */
1116 char tx_id_buf[30];
1117
1118 attrstr[0] = '\0';
1119
1120 bgp_dump_attr(attr, attrstr, BUFSIZ);
1121
1122 if (addpath_encode)
1123 snprintf(tx_id_buf, sizeof(tx_id_buf),
1124 " with addpath ID %u",
1125 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
1126
1127 zlog_debug("u%" PRIu64 ":s%" PRIu64 " send UPDATE %s%s %s",
1128 (SUBGRP_UPDGRP(subgrp))->id, subgrp->id,
1129 prefix2str(&p, buf, sizeof(buf)), tx_id_buf,
1130 attrstr);
1131 }
3f9c7369 1132
d62a17ae 1133 s = stream_new(BGP_MAX_PACKET_SIZE);
3f9c7369 1134
d62a17ae 1135 /* Make BGP update packet. */
1136 bgp_packet_set_marker(s, BGP_MSG_UPDATE);
3f9c7369 1137
d62a17ae 1138 /* Unfeasible Routes Length. */
1139 stream_putw(s, 0);
3f9c7369 1140
d62a17ae 1141 /* Make place for total attribute length. */
1142 pos = stream_get_endp(s);
1143 stream_putw(s, 0);
1144 total_attr_len = bgp_packet_attribute(
1145 NULL, peer, s, attr, &vecarr, &p, afi, safi, from, NULL, NULL,
b57ba6d2 1146 0, addpath_encode, BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
3f9c7369 1147
d62a17ae 1148 /* Set Total Path Attribute Length. */
1149 stream_putw_at(s, pos, total_attr_len);
3f9c7369 1150
d62a17ae 1151 /* NLRI set. */
1152 if (p.family == AF_INET && safi == SAFI_UNICAST
1153 && !peer_cap_enhe(peer, afi, safi))
1154 stream_put_prefix_addpath(
1155 s, &p, addpath_encode,
1156 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
3f9c7369 1157
d62a17ae 1158 /* Set size. */
1159 bgp_packet_set_size(s);
3f9c7369 1160
d62a17ae 1161 (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp), s, &vecarr);
2fc102e1 1162 subgroup_trigger_write(subgrp);
3f9c7369
DS
1163}
1164
d62a17ae 1165void subgroup_default_withdraw_packet(struct update_subgroup *subgrp)
3f9c7369 1166{
d62a17ae 1167 struct peer *peer;
1168 struct stream *s;
1169 struct prefix p;
1170 unsigned long attrlen_pos = 0;
1171 unsigned long cp;
1172 bgp_size_t unfeasible_len;
1173 bgp_size_t total_attr_len = 0;
1174 size_t mp_start = 0;
1175 size_t mplen_pos = 0;
1176 afi_t afi;
1177 safi_t safi;
1178 int addpath_encode = 0;
1179
1180 if (DISABLE_BGP_ANNOUNCE)
1181 return;
1182
1183 peer = SUBGRP_PEER(subgrp);
1184 afi = SUBGRP_AFI(subgrp);
1185 safi = SUBGRP_SAFI(subgrp);
1186 addpath_encode = bgp_addpath_encode_tx(peer, afi, safi);
1187
cbb65f5e
RW
1188 memset(&p, 0, sizeof(p));
1189 p.family = afi2family(afi);
1190 p.prefixlen = 0;
d62a17ae 1191
1192 if (bgp_debug_update(NULL, &p, subgrp->update_group, 0)) {
1193 char buf[PREFIX_STRLEN];
1194 /* ' with addpath ID ' 17
1195 * max strlen of uint32 + 10
1196 * +/- (just in case) + 1
1197 * null terminator + 1
1198 * ============================ 29 */
1199 char tx_id_buf[30];
1200
1201 if (addpath_encode)
1202 snprintf(tx_id_buf, sizeof(tx_id_buf),
1203 " with addpath ID %u",
1204 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
1205
1206 zlog_debug("u%" PRIu64 ":s%" PRIu64
1207 " send UPDATE %s%s -- unreachable",
1208 (SUBGRP_UPDGRP(subgrp))->id, subgrp->id,
1209 prefix2str(&p, buf, sizeof(buf)), tx_id_buf);
1210 }
3f9c7369 1211
d62a17ae 1212 s = stream_new(BGP_MAX_PACKET_SIZE);
3f9c7369 1213
d62a17ae 1214 /* Make BGP update packet. */
1215 bgp_packet_set_marker(s, BGP_MSG_UPDATE);
3f9c7369 1216
d62a17ae 1217 /* Unfeasible Routes Length. */;
1218 cp = stream_get_endp(s);
1219 stream_putw(s, 0);
3f9c7369 1220
d62a17ae 1221 /* Withdrawn Routes. */
1222 if (p.family == AF_INET && safi == SAFI_UNICAST
1223 && !peer_cap_enhe(peer, afi, safi)) {
1224 stream_put_prefix_addpath(
1225 s, &p, addpath_encode,
1226 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
3f9c7369 1227
d62a17ae 1228 unfeasible_len = stream_get_endp(s) - cp - 2;
3f9c7369 1229
d62a17ae 1230 /* Set unfeasible len. */
1231 stream_putw_at(s, cp, unfeasible_len);
3f9c7369 1232
d62a17ae 1233 /* Set total path attribute length. */
1234 stream_putw(s, 0);
1235 } else {
1236 attrlen_pos = stream_get_endp(s);
1237 stream_putw(s, 0);
1238 mp_start = stream_get_endp(s);
1239 mplen_pos = bgp_packet_mpunreach_start(s, afi, safi);
1240 bgp_packet_mpunreach_prefix(
b57ba6d2 1241 s, &p, afi, safi, NULL, NULL, 0, addpath_encode,
d62a17ae 1242 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE, NULL);
3f9c7369 1243
d62a17ae 1244 /* Set the mp_unreach attr's length */
1245 bgp_packet_mpunreach_end(s, mplen_pos);
3f9c7369 1246
d62a17ae 1247 /* Set total path attribute length. */
1248 total_attr_len = stream_get_endp(s) - mp_start;
1249 stream_putw_at(s, attrlen_pos, total_attr_len);
1250 }
1251
1252 bgp_packet_set_size(s);
1253
1254 (void)bpacket_queue_add(SUBGRP_PKTQ(subgrp), s, NULL);
2fc102e1 1255 subgroup_trigger_write(subgrp);
3f9c7369
DS
1256}
1257
1258static void
d62a17ae 1259bpacket_vec_arr_inherit_attr_flags(struct bpacket_attr_vec_arr *vecarr,
1260 bpacket_attr_vec_type type,
1261 struct attr *attr)
3f9c7369 1262{
d62a17ae 1263 if (CHECK_FLAG(attr->rmap_change_flags,
1264 BATTR_RMAP_NEXTHOP_PEER_ADDRESS))
1265 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1266 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS);
1267
1268 if (CHECK_FLAG(attr->rmap_change_flags, BATTR_REFLECTED))
1269 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1270 BPKT_ATTRVEC_FLAGS_REFLECTED);
1271
1272 if (CHECK_FLAG(attr->rmap_change_flags, BATTR_RMAP_NEXTHOP_UNCHANGED))
1273 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1274 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED);
1275
1276 if (CHECK_FLAG(attr->rmap_change_flags, BATTR_RMAP_IPV4_NHOP_CHANGED))
1277 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1278 BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED);
1279
1280 if (CHECK_FLAG(attr->rmap_change_flags,
1281 BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED))
1282 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1283 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED);
1284
1285 if (CHECK_FLAG(attr->rmap_change_flags,
1286 BATTR_RMAP_IPV6_LL_NHOP_CHANGED))
1287 SET_FLAG(vecarr->entries[BGP_ATTR_VEC_NH].flags,
1288 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_LNH_CHANGED);
3f9c7369
DS
1289}
1290
1291/* Reset the Attributes vector array. The vector array is used to override
1292 * certain output parameters in the packet for a particular peer
1293 */
d62a17ae 1294void bpacket_attr_vec_arr_reset(struct bpacket_attr_vec_arr *vecarr)
3f9c7369 1295{
d62a17ae 1296 int i;
3f9c7369 1297
d62a17ae 1298 if (!vecarr)
1299 return;
3f9c7369 1300
d62a17ae 1301 i = 0;
1302 while (i < BGP_ATTR_VEC_MAX) {
1303 vecarr->entries[i].flags = 0;
1304 vecarr->entries[i].offset = 0;
1305 i++;
1306 }
3f9c7369
DS
1307}
1308
1309/* Setup a particular node entry in the vecarr */
d62a17ae 1310void bpacket_attr_vec_arr_set_vec(struct bpacket_attr_vec_arr *vecarr,
1311 bpacket_attr_vec_type type, struct stream *s,
1312 struct attr *attr)
3f9c7369 1313{
d62a17ae 1314 if (!vecarr)
1315 return;
1316 assert(type < BGP_ATTR_VEC_MAX);
1317
1318 SET_FLAG(vecarr->entries[type].flags, BPKT_ATTRVEC_FLAGS_UPDATED);
1319 vecarr->entries[type].offset = stream_get_endp(s);
1320 if (attr)
1321 bpacket_vec_arr_inherit_attr_flags(vecarr, type, attr);
3f9c7369 1322}