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