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