]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_updgrp_packet.c
bgpd, zebra: rfc-5549-generic.patch
[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;
411
412 s = stream_dup (pkt->buffer);
413
414 vec = &pkt->arr.entries[BGP_ATTR_VEC_NH];
415 if (CHECK_FLAG (vec->flags, BPACKET_ATTRVEC_FLAGS_UPDATED))
416 {
417 u_int8_t nhlen;
418 int route_map_sets_nh;
419 nhlen = stream_getc_from (s, vec->offset);
420
421 route_map_sets_nh = CHECK_FLAG (vec->flags,
422 BPACKET_ATTRVEC_FLAGS_RMAP_CHANGED);
423
8a92a8a0 424 if (paf->afi == AFI_IP && !peer_cap_enhe(paf->peer))
3f9c7369
DS
425 {
426 struct in_addr v4nh;
427
428 stream_get_from (&v4nh, s, vec->offset + 1, 4);
429
430 /* If NH unavailable from attribute or the route-map has set it to
431 * be the peering address, use peer's NH. The "NH unavailable" case
432 * also covers next-hop-self and some other scenarios -- see
433 * subgroup_announce_check(). The only other case where we use the
434 * peer's NH is if it is an EBGP multiaccess scenario and there is
435 * no next-hop-unchanged setting.
436 */
437 if (!v4nh.s_addr ||
438 (route_map_sets_nh &&
439 CHECK_FLAG(vec->flags,
440 BPACKET_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS)))
441 stream_put_in_addr_at (s, vec->offset + 1, &paf->peer->nexthop.v4);
316e074d
DS
442 else if (!CHECK_FLAG(vec->flags,
443 BPACKET_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED) &&
444 paf->peer->sort == BGP_PEER_EBGP &&
3f9c7369
DS
445 !peer_af_flag_check (paf->peer, paf->afi, paf->safi,
446 PEER_FLAG_NEXTHOP_UNCHANGED))
447 {
448 if (bgp_multiaccess_check_v4 (v4nh, paf->peer) == 0)
449 stream_put_in_addr_at (s, vec->offset + 1,
450 &paf->peer->nexthop.v4);
451 }
452
453#if 0
454 if (!v4nh.s_addr)
455 nhtouse = paf->peer->nexthop.v4;
456
457 /*
458 * If NH is available from attribute (which is after outbound
459 * policy application), always use it if it has been specified
460 * by the policy. Otherwise, the decision to make is whether
461 * we need to set ourselves as the next-hop or not. Here are
462 * the conditions for that (1 OR 2):
463 *
464 * (1) if the configuration says: 'next-hop-self'
465 * (2) if the peer is EBGP AND not a third-party-nexthop type
466 *
467 * There are some exceptions even if the above conditions apply.
468 * Those are:
469 * (a) if the configuration says: 'next-hop-unchanged'. Honor that
470 * always. Not set 'self' as next-hop.
471 * (b) if we are reflecting the routes (IBGP->IBGP) and the config
472 * is _not_ forcing next-hop-self. We should pass on the
473 * next-hop unchanged for reflected routes.
474 */
475 if (route_map_sets_nh)
476 {
477 /*
478 * If address is specified, nothing to do; if specified as
479 * 'peer-address', compute the value to use.
480 *
481 * NOTE: If we are reflecting routes, the policy could have set
482 * this only if outbound policy has been allowed for route
483 * reflection -- handled in announce_check().
484 */
485 if (CHECK_FLAG(vec->flags,
486 BPACKET_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS))
487 nhtouse = paf->peer->nexthop.v4;
488 }
489 else if (peer_af_flag_check (paf->peer, paf->afi, paf->safi,
490 PEER_FLAG_NEXTHOP_SELF)
491 || (paf->peer->sort == BGP_PEER_EBGP &&
492 (bgp_multiaccess_check_v4 (v4nh, paf->peer) == 0)))
493 {
494 if (!(peer_af_flag_check (paf->peer, paf->afi, paf->safi,
495 PEER_FLAG_NEXTHOP_UNCHANGED)
496 || (CHECK_FLAG(vec->flags, BPACKET_ATTRVEC_FLAGS_REFLECTED) &&
497 !peer_af_flag_check(paf->peer, paf->afi, paf->safi,
498 PEER_FLAG_FORCE_NEXTHOP_SELF))))
499 nhtouse = paf->peer->nexthop.v4;
500 }
501#endif
502
503 }
8a92a8a0 504 else if (paf->afi == AFI_IP6 || peer_cap_enhe(paf->peer))
3f9c7369
DS
505 {
506 struct in6_addr v6nhglobal;
507 struct in6_addr v6nhlocal;
508
509 /*
510 * The logic here is rather similar to that for IPv4, the
511 * additional work being to handle 1 or 2 nexthops.
512 */
513 stream_get_from (&v6nhglobal, s, vec->offset + 1, 16);
514 if (IN6_IS_ADDR_UNSPECIFIED (&v6nhglobal) ||
515 (route_map_sets_nh &&
516 CHECK_FLAG(vec->flags,
517 BPACKET_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS)))
518 stream_put_in6_addr_at (s, vec->offset + 1,
519 &paf->peer->nexthop.v6_global);
316e074d
DS
520 else if (!CHECK_FLAG(vec->flags,
521 BPACKET_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED) &&
522 paf->peer->sort == BGP_PEER_EBGP &&
3f9c7369
DS
523 !peer_af_flag_check (paf->peer, paf->afi, paf->safi,
524 PEER_FLAG_NEXTHOP_UNCHANGED))
525 {
526 stream_put_in6_addr_at (s, vec->offset + 1,
527 &paf->peer->nexthop.v6_global);
528 }
529
530 if (nhlen == 32)
531 {
532 stream_get_from (&v6nhlocal, s, vec->offset + 1 + 16, 16);
533 if (IN6_IS_ADDR_UNSPECIFIED (&v6nhlocal))
534 stream_put_in6_addr_at (s, vec->offset + 1 + 16,
535 &paf->peer->nexthop.v6_local);
536 }
537 }
538 }
539
540 bgp_packet_add (paf->peer, s);
541 return s;
542}
543
544/*
545 * Update the vecarr offsets to go beyond 'pos' bytes, i.e. add 'pos'
546 * to each offset.
547 */
548static void
549bpacket_attr_vec_arr_update (struct bpacket_attr_vec_arr *vecarr, size_t pos)
550{
551 int i;
552
553 if (!vecarr)
554 return;
555
556 for (i = 0; i < BGP_ATTR_VEC_MAX; i++)
557 vecarr->entries[i].offset += pos;
558}
559
560/*
561 * Return if there are packets to build for this subgroup.
562 */
563int
564subgroup_packets_to_build (struct update_subgroup *subgrp)
565{
566 struct bgp_advertise *adv;
567
568 if (!subgrp)
569 return 0;
570
571 adv = BGP_ADV_FIFO_HEAD (&subgrp->sync->withdraw);
572 if (adv)
573 return 1;
574
575 adv = BGP_ADV_FIFO_HEAD (&subgrp->sync->update);
576 if (adv)
577 return 1;
578
579 return 0;
580}
581
582/* Make BGP update packet. */
583struct bpacket *
584subgroup_update_packet (struct update_subgroup *subgrp)
585{
586 struct bpacket_attr_vec_arr vecarr;
587 struct bpacket *pkt;
588 struct peer *peer;
589 struct stream *s;
590 struct stream *snlri;
591 struct stream *packet;
592 struct bgp_adj_out *adj;
593 struct bgp_advertise *adv;
594 struct bgp_node *rn = NULL;
595 struct bgp_info *binfo = NULL;
596 bgp_size_t total_attr_len = 0;
597 unsigned long attrlen_pos = 0;
598 size_t mpattrlen_pos = 0;
599 size_t mpattr_pos = 0;
600 afi_t afi;
601 safi_t safi;
602 int space_remaining = 0;
603 int space_needed = 0;
604 char send_attr_str[BUFSIZ];
ffd0c037 605 int send_attr_printed = 0;
3f9c7369
DS
606 int num_pfx = 0;
607
608
609 if (!subgrp)
610 return NULL;
611
612 if (bpacket_queue_is_full (SUBGRP_INST (subgrp), SUBGRP_PKTQ (subgrp)))
613 return NULL;
614
615
616 peer = SUBGRP_PEER (subgrp);
617 afi = SUBGRP_AFI (subgrp);
618 safi = SUBGRP_SAFI (subgrp);
619 s = subgrp->work;
620 stream_reset (s);
621 snlri = subgrp->scratch;
622 stream_reset (snlri);
623
624 bpacket_attr_vec_arr_reset (&vecarr);
625
626 adv = BGP_ADV_FIFO_HEAD (&subgrp->sync->update);
627 while (adv)
628 {
629 assert (adv->rn);
630 rn = adv->rn;
631 adj = adv->adj;
632 if (adv->binfo)
633 binfo = adv->binfo;
634
635 space_remaining = STREAM_CONCAT_REMAIN (s, snlri, STREAM_SIZE(s)) -
636 BGP_MAX_PACKET_SIZE_OVERFLOW;
637 space_needed = BGP_NLRI_LENGTH + PSIZE (rn->p.prefixlen);
638
639 /* When remaining space can't include NLRI and it's length. */
640 if (space_remaining < space_needed)
641 break;
642
643 /* If packet is empty, set attribute. */
644 if (stream_empty (s))
645 {
646 struct peer *from = NULL;
647
648 if (binfo)
649 from = binfo->peer;
650
651 /* 1: Write the BGP message header - 16 bytes marker, 2 bytes length,
652 * one byte message type.
653 */
654 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
655
656 /* 2: withdrawn routes length */
657 stream_putw (s, 0);
658
659 /* 3: total attributes length - attrlen_pos stores the position */
660 attrlen_pos = stream_get_endp (s);
661 stream_putw (s, 0);
662
663 /* 4: if there is MP_REACH_NLRI attribute, that should be the first
664 * attribute, according to draft-ietf-idr-error-handling. Save the
665 * position.
666 */
667 mpattr_pos = stream_get_endp (s);
668
669 /* 5: Encode all the attributes, except MP_REACH_NLRI attr. */
670 total_attr_len = bgp_packet_attribute (NULL, peer, s,
671 adv->baa->attr, &vecarr,
672 NULL, afi, safi,
673 from, NULL, NULL);
674
675 space_remaining = STREAM_CONCAT_REMAIN (s, snlri, STREAM_SIZE(s)) -
676 BGP_MAX_PACKET_SIZE_OVERFLOW;
677 space_needed = BGP_NLRI_LENGTH + PSIZE (rn->p.prefixlen);
678
679 /* If the attributes alone do not leave any room for NLRI then
680 * return */
681 if (space_remaining < space_needed)
682 {
ffd0c037 683 zlog_err ("u%" PRIu64 ":s%" PRIu64 " attributes too long, cannot send UPDATE",
3f9c7369
DS
684 subgrp->update_group->id, subgrp->id);
685
686 /* Flush the FIFO update queue */
687 while (adv)
688 adv = bgp_advertise_clean_subgroup (subgrp, adj);
689 return NULL;
690 }
691
692 if (BGP_DEBUG (update, UPDATE_OUT) ||
693 BGP_DEBUG (update, UPDATE_PREFIX))
694 {
695 memset (send_attr_str, 0, BUFSIZ);
696 send_attr_printed = 0;
697 bgp_dump_attr (peer, adv->baa->attr, send_attr_str, BUFSIZ);
698 }
699 }
700
8a92a8a0
DS
701 if ((afi == AFI_IP && safi == SAFI_UNICAST) &&
702 !peer_cap_enhe(peer))
3f9c7369
DS
703 stream_put_prefix (s, &rn->p);
704 else
705 {
706 /* Encode the prefix in MP_REACH_NLRI attribute */
707 struct prefix_rd *prd = NULL;
708 u_char *tag = NULL;
709
710 if (rn->prn)
711 prd = (struct prefix_rd *) &rn->prn->p;
712 if (binfo && binfo->extra)
713 tag = binfo->extra->tag;
714
715 if (stream_empty (snlri))
716 mpattrlen_pos = bgp_packet_mpattr_start (snlri, afi, safi,
8a92a8a0
DS
717 (peer_cap_enhe(peer) ? AFI_IP6 : afi),
718 &vecarr, adv->baa->attr);
3f9c7369
DS
719 bgp_packet_mpattr_prefix (snlri, afi, safi, &rn->p, prd, tag);
720 }
721
722 num_pfx++;
723
724 if (bgp_debug_update(NULL, &rn->p, subgrp->update_group, 0))
725 {
726 char buf[INET6_BUFSIZ];
727
728 if (!send_attr_printed)
729 {
ffd0c037 730 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " send UPDATE w/ attr: %s",
3f9c7369
DS
731 subgrp->update_group->id, subgrp->id, send_attr_str);
732 send_attr_printed = 1;
733 }
734
ffd0c037 735 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " send UPDATE %s/%d",
3f9c7369
DS
736 subgrp->update_group->id, subgrp->id,
737 inet_ntop (rn->p.family, &(rn->p.u.prefix), buf,
738 INET6_BUFSIZ), rn->p.prefixlen);
739 }
740
741 /* Synchnorize attribute. */
742 if (adj->attr)
743 bgp_attr_unintern (&adj->attr);
744 else
745 subgrp->scount++;
746
747 adj->attr = bgp_attr_intern (adv->baa->attr);
748
749 adv = bgp_advertise_clean_subgroup (subgrp, adj);
750 }
751
752 if (!stream_empty (s))
753 {
754 if (!stream_empty (snlri))
755 {
756 bgp_packet_mpattr_end (snlri, mpattrlen_pos);
757 total_attr_len += stream_get_endp (snlri);
758 }
759
760 /* set the total attribute length correctly */
761 stream_putw_at (s, attrlen_pos, total_attr_len);
762
763 if (!stream_empty (snlri))
764 {
765 packet = stream_dupcat (s, snlri, mpattr_pos);
766 bpacket_attr_vec_arr_update (&vecarr, mpattr_pos);
767 }
768 else
769 packet = stream_dup (s);
770 bgp_packet_set_size (packet);
771 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
ffd0c037 772 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " UPDATE len %zd numpfx %d",
3f9c7369
DS
773 subgrp->update_group->id, subgrp->id,
774 (stream_get_endp(packet) - stream_get_getp(packet)), num_pfx);
775 pkt = bpacket_queue_add (SUBGRP_PKTQ (subgrp), packet, &vecarr);
776 stream_reset (s);
777 stream_reset (snlri);
778 return pkt;
779 }
780 return NULL;
781}
782
783/* Make BGP withdraw packet. */
784/* For ipv4 unicast:
785 16-octet marker | 2-octet length | 1-octet type |
786 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0)
787*/
788/* For other afi/safis:
789 16-octet marker | 2-octet length | 1-octet type |
790 2-octet withdrawn route length (=0) | 2-octet attrlen |
791 mp_unreach attr type | attr len | afi | safi | withdrawn prefixes
792*/
793struct bpacket *
794subgroup_withdraw_packet (struct update_subgroup *subgrp)
795{
796 struct bpacket *pkt;
797 struct stream *s;
798 struct bgp_adj_out *adj;
799 struct bgp_advertise *adv;
8a92a8a0 800 struct peer *peer;
3f9c7369
DS
801 struct bgp_node *rn;
802 bgp_size_t unfeasible_len;
803 bgp_size_t total_attr_len;
804 size_t mp_start = 0;
805 size_t attrlen_pos = 0;
806 size_t mplen_pos = 0;
807 u_char first_time = 1;
808 afi_t afi;
809 safi_t safi;
810 int space_remaining = 0;
811 int space_needed = 0;
812 int num_pfx = 0;
813
814 if (!subgrp)
815 return NULL;
816
817 if (bpacket_queue_is_full (SUBGRP_INST (subgrp), SUBGRP_PKTQ (subgrp)))
818 return NULL;
819
8a92a8a0 820 peer = SUBGRP_PEER (subgrp);
3f9c7369
DS
821 afi = SUBGRP_AFI (subgrp);
822 safi = SUBGRP_SAFI (subgrp);
823 s = subgrp->work;
824 stream_reset (s);
825
826 while ((adv = BGP_ADV_FIFO_HEAD (&subgrp->sync->withdraw)) != NULL)
827 {
828 assert (adv->rn);
829 adj = adv->adj;
830 rn = adv->rn;
831
832 space_remaining = STREAM_REMAIN (s) -
833 BGP_MAX_PACKET_SIZE_OVERFLOW;
834 space_needed = (BGP_NLRI_LENGTH + BGP_TOTAL_ATTR_LEN +
835 PSIZE (rn->p.prefixlen));
836
837 if (space_remaining < space_needed)
838 break;
839
840 if (stream_empty (s))
841 {
842 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
843 stream_putw (s, 0); /* unfeasible routes length */
844 }
845 else
846 first_time = 0;
847
8a92a8a0
DS
848 if (afi == AFI_IP && safi == SAFI_UNICAST &&
849 !peer_cap_enhe(peer))
3f9c7369
DS
850 stream_put_prefix (s, &rn->p);
851 else
852 {
853 struct prefix_rd *prd = NULL;
854
855 if (rn->prn)
856 prd = (struct prefix_rd *) &rn->prn->p;
857
858 /* If first time, format the MP_UNREACH header */
859 if (first_time)
860 {
861 attrlen_pos = stream_get_endp (s);
862 /* total attr length = 0 for now. reevaluate later */
863 stream_putw (s, 0);
864 mp_start = stream_get_endp (s);
865 mplen_pos = bgp_packet_mpunreach_start (s, afi, safi);
866 }
867
868 bgp_packet_mpunreach_prefix (s, &rn->p, afi, safi, prd, NULL);
869 }
870
871 num_pfx++;
872
873 if (bgp_debug_update(NULL, &rn->p, subgrp->update_group, 0))
874 {
875 char buf[INET6_BUFSIZ];
876
ffd0c037 877 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " send UPDATE %s/%d -- unreachable",
3f9c7369
DS
878 subgrp->update_group->id, subgrp->id,
879 inet_ntop (rn->p.family, &(rn->p.u.prefix), buf,
880 INET6_BUFSIZ), rn->p.prefixlen);
881 }
882
883 subgrp->scount--;
884
885 bgp_adj_out_remove_subgroup (rn, adj, subgrp);
886 bgp_unlock_node (rn);
887 }
888
889 if (!stream_empty (s))
890 {
8a92a8a0
DS
891 if (afi == AFI_IP && safi == SAFI_UNICAST &&
892 !peer_cap_enhe(peer))
3f9c7369
DS
893 {
894 unfeasible_len
895 = stream_get_endp (s) - BGP_HEADER_SIZE - BGP_UNFEASIBLE_LEN;
896 stream_putw_at (s, BGP_HEADER_SIZE, unfeasible_len);
897 stream_putw (s, 0);
898 }
899 else
900 {
901 /* Set the mp_unreach attr's length */
902 bgp_packet_mpunreach_end (s, mplen_pos);
903
904 /* Set total path attribute length. */
905 total_attr_len = stream_get_endp (s) - mp_start;
906 stream_putw_at (s, attrlen_pos, total_attr_len);
907 }
908 bgp_packet_set_size (s);
909 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
ffd0c037 910 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " UPDATE (withdraw) len %zd numpfx %d",
3f9c7369
DS
911 subgrp->update_group->id, subgrp->id,
912 (stream_get_endp(s) - stream_get_getp(s)), num_pfx);
913 pkt = bpacket_queue_add (SUBGRP_PKTQ (subgrp), stream_dup (s), NULL);
914 stream_reset (s);
915 return pkt;
916 }
917
918 return NULL;
919}
920
921void
922subgroup_default_update_packet (struct update_subgroup *subgrp,
923 struct attr *attr, struct peer *from)
924{
925 struct stream *s;
926 struct stream *packet;
927 struct peer *peer;
928 struct prefix p;
929 unsigned long pos;
930 bgp_size_t total_attr_len;
931 afi_t afi;
932 safi_t safi;
933 struct bpacket_attr_vec_arr vecarr;
934
935 if (DISABLE_BGP_ANNOUNCE)
936 return;
937
938 if (!subgrp)
939 return;
940
941 peer = SUBGRP_PEER (subgrp);
942 afi = SUBGRP_AFI (subgrp);
943 safi = SUBGRP_SAFI (subgrp);
944 bpacket_attr_vec_arr_reset (&vecarr);
945
946 if (afi == AFI_IP)
947 str2prefix ("0.0.0.0/0", &p);
948#ifdef HAVE_IPV6
949 else
950 str2prefix ("::/0", &p);
951#endif /* HAVE_IPV6 */
952
953 /* Logging the attribute. */
954 if (bgp_debug_update(NULL, &p, subgrp->update_group, 0))
955 {
956 char attrstr[BUFSIZ];
957 char buf[INET6_BUFSIZ];
958 attrstr[0] = '\0';
959
960 bgp_dump_attr (peer, attr, attrstr, BUFSIZ);
ffd0c037 961 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " send UPDATE %s/%d %s",
3f9c7369
DS
962 (SUBGRP_UPDGRP (subgrp))->id, subgrp->id,
963 inet_ntop (p.family, &(p.u.prefix), buf, INET6_BUFSIZ),
964 p.prefixlen, attrstr);
965 }
966
967 s = stream_new (BGP_MAX_PACKET_SIZE);
968
969 /* Make BGP update packet. */
970 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
971
972 /* Unfeasible Routes Length. */
973 stream_putw (s, 0);
974
975 /* Make place for total attribute length. */
976 pos = stream_get_endp (s);
977 stream_putw (s, 0);
978 total_attr_len = bgp_packet_attribute (NULL, peer, s, attr, &vecarr, &p,
979 afi, safi, from, NULL, NULL);
980
981 /* Set Total Path Attribute Length. */
982 stream_putw_at (s, pos, total_attr_len);
983
984 /* NLRI set. */
8a92a8a0
DS
985 if (p.family == AF_INET && safi == SAFI_UNICAST &&
986 !peer_cap_enhe(peer))
3f9c7369
DS
987 stream_put_prefix (s, &p);
988
989 /* Set size. */
990 bgp_packet_set_size (s);
991
992 packet = stream_dup (s);
993 stream_free (s);
994 (void) bpacket_queue_add (SUBGRP_PKTQ (subgrp), packet, &vecarr);
995 subgroup_trigger_write(subgrp);
996}
997
998void
999subgroup_default_withdraw_packet (struct update_subgroup *subgrp)
1000{
8a92a8a0 1001 struct peer *peer;
3f9c7369
DS
1002 struct stream *s;
1003 struct stream *packet;
1004 struct prefix p;
1005 unsigned long attrlen_pos = 0;
1006 unsigned long cp;
1007 bgp_size_t unfeasible_len;
1008 bgp_size_t total_attr_len;
1009 size_t mp_start = 0;
1010 size_t mplen_pos = 0;
1011 afi_t afi;
1012 safi_t safi;
1013
1014 if (DISABLE_BGP_ANNOUNCE)
1015 return;
1016
8a92a8a0 1017 peer = SUBGRP_PEER (subgrp);
3f9c7369
DS
1018 afi = SUBGRP_AFI (subgrp);
1019 safi = SUBGRP_SAFI (subgrp);
1020
1021 if (afi == AFI_IP)
1022 str2prefix ("0.0.0.0/0", &p);
1023#ifdef HAVE_IPV6
1024 else
1025 str2prefix ("::/0", &p);
1026#endif /* HAVE_IPV6 */
1027
1028 total_attr_len = 0;
1029
1030 if (bgp_debug_update(NULL, &p, subgrp->update_group, 0))
1031 {
1032 char buf[INET6_BUFSIZ];
1033
ffd0c037 1034 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " send UPDATE %s/%d -- unreachable",
3f9c7369
DS
1035 (SUBGRP_UPDGRP (subgrp))->id, subgrp->id, inet_ntop (p.family,
1036 &(p.u.
1037 prefix),
1038 buf,
1039 INET6_BUFSIZ),
1040 p.prefixlen);
1041 }
1042
1043 s = stream_new (BGP_MAX_PACKET_SIZE);
1044
1045 /* Make BGP update packet. */
1046 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
1047
1048 /* Unfeasible Routes Length. */ ;
1049 cp = stream_get_endp (s);
1050 stream_putw (s, 0);
1051
1052 /* Withdrawn Routes. */
8a92a8a0
DS
1053 if (p.family == AF_INET && safi == SAFI_UNICAST &&
1054 !peer_cap_enhe(peer))
3f9c7369
DS
1055 {
1056 stream_put_prefix (s, &p);
1057
1058 unfeasible_len = stream_get_endp (s) - cp - 2;
1059
1060 /* Set unfeasible len. */
1061 stream_putw_at (s, cp, unfeasible_len);
1062
1063 /* Set total path attribute length. */
1064 stream_putw (s, 0);
1065 }
1066 else
1067 {
1068 attrlen_pos = stream_get_endp (s);
1069 stream_putw (s, 0);
1070 mp_start = stream_get_endp (s);
1071 mplen_pos = bgp_packet_mpunreach_start (s, afi, safi);
1072 bgp_packet_mpunreach_prefix (s, &p, afi, safi, NULL, NULL);
1073
1074 /* Set the mp_unreach attr's length */
1075 bgp_packet_mpunreach_end (s, mplen_pos);
1076
1077 /* Set total path attribute length. */
1078 total_attr_len = stream_get_endp (s) - mp_start;
1079 stream_putw_at (s, attrlen_pos, total_attr_len);
1080 }
1081
1082 bgp_packet_set_size (s);
1083
1084 packet = stream_dup (s);
1085 stream_free (s);
1086
1087 (void) bpacket_queue_add (SUBGRP_PKTQ (subgrp), packet, NULL);
1088 subgroup_trigger_write(subgrp);
1089}
1090
1091static void
1092bpacket_vec_arr_inherit_attr_flags (struct bpacket_attr_vec_arr *vecarr,
1093 bpacket_attr_vec_type type,
1094 struct attr *attr)
1095{
1096 if (CHECK_FLAG (attr->rmap_change_flags,
1097 BATTR_RMAP_NEXTHOP_CHANGED))
1098 SET_FLAG (vecarr->entries[BGP_ATTR_VEC_NH].flags,
1099 BPACKET_ATTRVEC_FLAGS_RMAP_CHANGED);
1100
1101 if (CHECK_FLAG (attr->rmap_change_flags,
1102 BATTR_RMAP_NEXTHOP_PEER_ADDRESS))
1103 SET_FLAG (vecarr->entries[BGP_ATTR_VEC_NH].flags,
1104 BPACKET_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS);
1105
1106 if (CHECK_FLAG (attr->rmap_change_flags, BATTR_REFLECTED))
1107 SET_FLAG (vecarr->entries[BGP_ATTR_VEC_NH].flags,
1108 BPACKET_ATTRVEC_FLAGS_REFLECTED);
316e074d
DS
1109
1110 if (CHECK_FLAG (attr->rmap_change_flags,
1111 BATTR_RMAP_NEXTHOP_UNCHANGED))
1112 SET_FLAG (vecarr->entries[BGP_ATTR_VEC_NH].flags,
1113 BPACKET_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED);
3f9c7369
DS
1114}
1115
1116/* Reset the Attributes vector array. The vector array is used to override
1117 * certain output parameters in the packet for a particular peer
1118 */
1119void
1120bpacket_attr_vec_arr_reset (struct bpacket_attr_vec_arr *vecarr)
1121{
1122 int i;
1123
1124 if (!vecarr)
1125 return;
1126
1127 i = 0;
1128 while (i < BGP_ATTR_VEC_MAX)
1129 {
1130 vecarr->entries[i].flags = 0;
1131 vecarr->entries[i].offset = 0;
1132 i++;
1133 }
1134}
1135
1136/* Setup a particular node entry in the vecarr */
1137void
1138bpacket_attr_vec_arr_set_vec (struct bpacket_attr_vec_arr *vecarr,
1139 bpacket_attr_vec_type type, struct stream *s,
1140 struct attr *attr)
1141{
1142 if (!vecarr)
1143 return;
1144 assert (type < BGP_ATTR_VEC_MAX);
1145
1146 SET_FLAG (vecarr->entries[type].flags, BPACKET_ATTRVEC_FLAGS_UPDATED);
1147 vecarr->entries[type].offset = stream_get_endp (s);
1148 if (attr)
1149 bpacket_vec_arr_inherit_attr_flags(vecarr, type, attr);
1150}