]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_updgrp_packet.c
Merge remote-tracking branch 'origin/master' into evpn_plus_struct_attr
[mirror_frr.git] / bgpd / bgp_updgrp_packet.c
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 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
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"
39 #include "log.h"
40 #include "plist.h"
41 #include "linklist.h"
42 #include "workqueue.h"
43 #include "hash.h"
44 #include "queue.h"
45 #include "mpls.h"
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"
56 #include "bgpd/bgp_mplsvpn.h"
57 #include "bgpd/bgp_label.h"
58
59 /********************
60 * PRIVATE FUNCTIONS
61 ********************/
62
63 /********************
64 * PUBLIC FUNCTIONS
65 ********************/
66 struct bpacket *
67 bpacket_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
77 void
78 bpacket_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
86 void
87 bpacket_queue_init (struct bpacket_queue *q)
88 {
89 TAILQ_INIT (&(q->pkts));
90 }
91
92 /*
93 * bpacket_queue_sanity_check
94 */
95 void
96 bpacket_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 */
133 static void
134 bpacket_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 */
157 struct bpacket *
158 bpacket_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
200 struct bpacket *
201 bpacket_queue_first (struct bpacket_queue *q)
202 {
203 return (TAILQ_FIRST (&(q->pkts)));
204 }
205
206 struct bpacket *
207 bpacket_queue_last (struct bpacket_queue *q)
208 {
209 return TAILQ_LAST (&(q->pkts), pkt_queue);
210 }
211
212 struct bpacket *
213 bpacket_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
226 unsigned int
227 bpacket_queue_length (struct bpacket_queue *q)
228 {
229 return q->curr_count - 1;
230 }
231
232 unsigned int
233 bpacket_queue_hwm_length (struct bpacket_queue *q)
234 {
235 return q->hwm_count - 1;
236 }
237
238 int
239 bpacket_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
246 void
247 bpacket_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 */
259 void
260 bpacket_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 */
278 static int
279 bpacket_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
312 void
313 bpacket_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 */
343 void
344 bpacket_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
359 unsigned int
360 bpacket_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 */
386 void
387 bpacket_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 {
395 vty_out (vty, " Packet %p ver %u buffer %p\n", pkt, pkt->ver,
396 pkt->buffer);
397
398 LIST_FOREACH (paf, &(pkt->peers), pkt_train)
399 {
400 vty_out (vty, " - %s\n", paf->peer->host);
401 }
402 pkt = bpacket_next (pkt);
403 }
404 return;
405 }
406
407 struct stream *
408 bpacket_reformat_for_peer (struct bpacket *pkt, struct peer_af *paf)
409 {
410 struct stream *s = NULL;
411 bpacket_attr_vec *vec;
412 struct peer *peer;
413 char buf[BUFSIZ];
414 char buf2[BUFSIZ];
415
416 s = stream_dup (pkt->buffer);
417 peer = PAF_PEER(paf);
418
419 vec = &pkt->arr.entries[BGP_ATTR_VEC_NH];
420 if (CHECK_FLAG (vec->flags, BPKT_ATTRVEC_FLAGS_UPDATED))
421 {
422 u_int8_t nhlen;
423 afi_t nhafi = AFI_MAX; /* NH AFI is based on nhlen! */
424 int route_map_sets_nh;
425 nhlen = stream_getc_from (s, vec->offset);
426 if (peer_cap_enhe(peer, paf->afi, paf->safi))
427 nhafi = AFI_IP6;
428 else
429 nhafi = BGP_NEXTHOP_AFI_FROM_NHLEN(nhlen);
430
431 if (nhafi == AFI_IP)
432 {
433 struct in_addr v4nh, *mod_v4nh;
434 int nh_modified = 0;
435 size_t offset_nh = vec->offset + 1;
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));
440
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);
452 stream_free (s);
453 return NULL;
454 }
455
456 stream_get_from (&v4nh, s, offset_nh, IPV4_MAX_BYTELEN);
457 mod_v4nh = &v4nh;
458
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.
469 */
470 if (route_map_sets_nh)
471 {
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 paf->safi != SAFI_EVPN &&
486 (bgp_multiaccess_check_v4 (v4nh, peer) == 0) &&
487 !CHECK_FLAG(vec->flags,
488 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED) &&
489 !peer_af_flag_check (peer, nhafi, paf->safi,
490 PEER_FLAG_NEXTHOP_UNCHANGED))
491 {
492 /* NOTE: not handling case where NH has new AFI */
493 mod_v4nh = &peer->nexthop.v4;
494 nh_modified = 1;
495 }
496
497 if (nh_modified) /* allow for VPN RD */
498 stream_put_in_addr_at (s, offset_nh, mod_v4nh);
499
500 if (bgp_debug_update(peer, NULL, NULL, 0))
501 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " %s send UPDATE w/ nexthop %s%s",
502 PAF_SUBGRP(paf)->update_group->id, PAF_SUBGRP(paf)->id,
503 peer->host, inet_ntoa (*mod_v4nh),
504 (nhlen == 12 ? " and RD" : ""));
505 }
506 else if (nhafi == AFI_IP6)
507 {
508 struct in6_addr v6nhglobal, *mod_v6nhg;
509 struct in6_addr v6nhlocal, *mod_v6nhl;
510 int gnh_modified, lnh_modified;
511 size_t offset_nhglobal = vec->offset + 1;
512 size_t offset_nhlocal = vec->offset + 1;
513
514 gnh_modified = lnh_modified = 0;
515 mod_v6nhg = &v6nhglobal;
516 mod_v6nhl = &v6nhlocal;
517
518 route_map_sets_nh =
519 (CHECK_FLAG (vec->flags, BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED) ||
520 CHECK_FLAG (vec->flags, BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS));
521
522 /*
523 * The logic here is rather similar to that for IPv4, the
524 * additional work being to handle 1 or 2 nexthops. Also, 3rd
525 * party nexthop is not propagated for EBGP right now.
526 */
527 switch (nhlen)
528 {
529 case BGP_ATTR_NHLEN_IPV6_GLOBAL:
530 break;
531 case BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL:
532 offset_nhlocal += IPV6_MAX_BYTELEN;
533 break;
534 case BGP_ATTR_NHLEN_VPNV6_GLOBAL:
535 offset_nhglobal += 8;
536 break;
537 case BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL:
538 offset_nhglobal += 8;
539 offset_nhlocal += 8 * 2 + IPV6_MAX_BYTELEN;
540 break;
541 default:
542 /* TODO: handle IPv4 nexthops */
543 zlog_warn ("%s: %s: invalid MP nexthop length (AFI IP6): %u",
544 __func__, peer->host, nhlen);
545 stream_free (s);
546 return NULL;
547 }
548
549 stream_get_from (&v6nhglobal, s, offset_nhglobal, IPV6_MAX_BYTELEN);
550 if (route_map_sets_nh)
551 {
552 if (CHECK_FLAG(vec->flags,
553 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS))
554 {
555 mod_v6nhg = &peer->nexthop.v6_global;
556 gnh_modified = 1;
557 }
558 }
559 else if (IN6_IS_ADDR_UNSPECIFIED (&v6nhglobal))
560 {
561 mod_v6nhg = &peer->nexthop.v6_global;
562 gnh_modified = 1;
563 }
564 else if (peer->sort == BGP_PEER_EBGP &&
565 !CHECK_FLAG(vec->flags,
566 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED) &&
567 !peer_af_flag_check (peer, nhafi, paf->safi,
568 PEER_FLAG_NEXTHOP_UNCHANGED))
569 {
570 /* NOTE: not handling case where NH has new AFI */
571 mod_v6nhg = &peer->nexthop.v6_global;
572 gnh_modified = 1;
573 }
574
575
576 if (nhlen == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL ||
577 nhlen == BGP_ATTR_NHLEN_VPNV6_GLOBAL_AND_LL)
578 {
579 stream_get_from (&v6nhlocal, s, offset_nhlocal, IPV6_MAX_BYTELEN);
580 if (IN6_IS_ADDR_UNSPECIFIED (&v6nhlocal))
581 {
582 mod_v6nhl = &peer->nexthop.v6_local;
583 lnh_modified = 1;
584 }
585 }
586
587 if (gnh_modified)
588 stream_put_in6_addr_at (s, offset_nhglobal, mod_v6nhg);
589 if (lnh_modified)
590 stream_put_in6_addr_at (s, offset_nhlocal, mod_v6nhl);
591
592 if (bgp_debug_update(peer, NULL, NULL, 0))
593 {
594 if (nhlen == 32 || nhlen == 48)
595 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " %s send UPDATE w/ mp_nexthops %s, %s%s",
596 PAF_SUBGRP(paf)->update_group->id,
597 PAF_SUBGRP(paf)->id,
598 peer->host,
599 inet_ntop (AF_INET6, mod_v6nhg, buf, BUFSIZ),
600 inet_ntop (AF_INET6, mod_v6nhl, buf2, BUFSIZ),
601 (nhlen == 48 ? " and RD" : ""));
602 else
603 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " %s send UPDATE w/ mp_nexthop %s%s",
604 PAF_SUBGRP(paf)->update_group->id,
605 PAF_SUBGRP(paf)->id,
606 peer->host,
607 inet_ntop (AF_INET6, mod_v6nhg, buf, BUFSIZ),
608 (nhlen == 24 ? " and RD" : ""));
609 }
610 }
611 else if (paf->afi == AFI_L2VPN)
612 {
613 struct in_addr v4nh, *mod_v4nh;
614 int nh_modified = 0;
615
616 stream_get_from (&v4nh, s, vec->offset + 1, 4);
617 mod_v4nh = &v4nh;
618
619 /* No route-map changes allowed for EVPN nexthops. */
620 if (!v4nh.s_addr)
621 {
622 mod_v4nh = &peer->nexthop.v4;
623 nh_modified = 1;
624 }
625
626 if (nh_modified)
627 stream_put_in_addr_at (s, vec->offset + 1, mod_v4nh);
628
629 if (bgp_debug_update(peer, NULL, NULL, 0))
630 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " %s send UPDATE w/ nexthop %s",
631 PAF_SUBGRP(paf)->update_group->id, PAF_SUBGRP(paf)->id,
632 peer->host, inet_ntoa (*mod_v4nh));
633
634 }
635 }
636
637 bgp_packet_add (peer, s);
638 return s;
639 }
640
641 /*
642 * Update the vecarr offsets to go beyond 'pos' bytes, i.e. add 'pos'
643 * to each offset.
644 */
645 static void
646 bpacket_attr_vec_arr_update (struct bpacket_attr_vec_arr *vecarr, size_t pos)
647 {
648 int i;
649
650 if (!vecarr)
651 return;
652
653 for (i = 0; i < BGP_ATTR_VEC_MAX; i++)
654 vecarr->entries[i].offset += pos;
655 }
656
657 /*
658 * Return if there are packets to build for this subgroup.
659 */
660 int
661 subgroup_packets_to_build (struct update_subgroup *subgrp)
662 {
663 struct bgp_advertise *adv;
664
665 if (!subgrp)
666 return 0;
667
668 adv = BGP_ADV_FIFO_HEAD (&subgrp->sync->withdraw);
669 if (adv)
670 return 1;
671
672 adv = BGP_ADV_FIFO_HEAD (&subgrp->sync->update);
673 if (adv)
674 return 1;
675
676 return 0;
677 }
678
679 /* Make BGP update packet. */
680 struct bpacket *
681 subgroup_update_packet (struct update_subgroup *subgrp)
682 {
683 struct bpacket_attr_vec_arr vecarr;
684 struct bpacket *pkt;
685 struct peer *peer;
686 struct stream *s;
687 struct stream *snlri;
688 struct stream *packet;
689 struct bgp_adj_out *adj;
690 struct bgp_advertise *adv;
691 struct bgp_node *rn = NULL;
692 struct bgp_info *binfo = NULL;
693 bgp_size_t total_attr_len = 0;
694 unsigned long attrlen_pos = 0;
695 size_t mpattrlen_pos = 0;
696 size_t mpattr_pos = 0;
697 afi_t afi;
698 safi_t safi;
699 int space_remaining = 0;
700 int space_needed = 0;
701 char send_attr_str[BUFSIZ];
702 int send_attr_printed = 0;
703 int num_pfx = 0;
704 int addpath_encode = 0;
705 u_int32_t addpath_tx_id = 0;
706 struct prefix_rd *prd = NULL;
707 mpls_label_t label = MPLS_INVALID_LABEL;
708
709 if (!subgrp)
710 return NULL;
711
712 if (bpacket_queue_is_full (SUBGRP_INST (subgrp), SUBGRP_PKTQ (subgrp)))
713 return NULL;
714
715 peer = SUBGRP_PEER (subgrp);
716 afi = SUBGRP_AFI (subgrp);
717 safi = SUBGRP_SAFI (subgrp);
718 s = subgrp->work;
719 stream_reset (s);
720 snlri = subgrp->scratch;
721 stream_reset (snlri);
722
723 bpacket_attr_vec_arr_reset (&vecarr);
724
725 addpath_encode = bgp_addpath_encode_tx (peer, afi, safi);
726
727 adv = BGP_ADV_FIFO_HEAD (&subgrp->sync->update);
728 while (adv)
729 {
730 assert (adv->rn);
731 rn = adv->rn;
732 adj = adv->adj;
733 addpath_tx_id = adj->addpath_tx_id;
734 binfo = adv->binfo;
735
736 space_remaining = STREAM_CONCAT_REMAIN (s, snlri, STREAM_SIZE(s)) -
737 BGP_MAX_PACKET_SIZE_OVERFLOW;
738 space_needed = BGP_NLRI_LENGTH +
739 bgp_packet_mpattr_prefix_size (afi, safi, &rn->p);
740
741 /* When remaining space can't include NLRI and it's length. */
742 if (space_remaining < space_needed)
743 break;
744
745 /* If packet is empty, set attribute. */
746 if (stream_empty (s))
747 {
748 struct peer *from = NULL;
749
750 if (binfo)
751 from = binfo->peer;
752
753 /* 1: Write the BGP message header - 16 bytes marker, 2 bytes length,
754 * one byte message type.
755 */
756 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
757
758 /* 2: withdrawn routes length */
759 stream_putw (s, 0);
760
761 /* 3: total attributes length - attrlen_pos stores the position */
762 attrlen_pos = stream_get_endp (s);
763 stream_putw (s, 0);
764
765 /* 4: if there is MP_REACH_NLRI attribute, that should be the first
766 * attribute, according to draft-ietf-idr-error-handling. Save the
767 * position.
768 */
769 mpattr_pos = stream_get_endp (s);
770
771 /* 5: Encode all the attributes, except MP_REACH_NLRI attr. */
772 total_attr_len = bgp_packet_attribute (NULL, peer, s,
773 adv->baa->attr, &vecarr,
774 NULL, afi, safi,
775 from, NULL, NULL, 0, 0);
776
777 space_remaining = STREAM_CONCAT_REMAIN (s, snlri, STREAM_SIZE(s)) -
778 BGP_MAX_PACKET_SIZE_OVERFLOW;
779 space_needed = BGP_NLRI_LENGTH +
780 bgp_packet_mpattr_prefix_size (afi, safi, &rn->p);
781
782 /* If the attributes alone do not leave any room for NLRI then
783 * return */
784 if (space_remaining < space_needed)
785 {
786 zlog_err ("u%" PRIu64 ":s%" PRIu64 " attributes too long, cannot send UPDATE",
787 subgrp->update_group->id, subgrp->id);
788
789 /* Flush the FIFO update queue */
790 while (adv)
791 adv = bgp_advertise_clean_subgroup (subgrp, adj);
792 return NULL;
793 }
794
795 if (BGP_DEBUG (update, UPDATE_OUT) ||
796 BGP_DEBUG (update, UPDATE_PREFIX))
797 {
798 memset (send_attr_str, 0, BUFSIZ);
799 send_attr_printed = 0;
800 bgp_dump_attr (adv->baa->attr, send_attr_str, BUFSIZ);
801 }
802 }
803
804 if ((afi == AFI_IP && safi == SAFI_UNICAST) &&
805 !peer_cap_enhe(peer, afi, safi))
806 stream_put_prefix_addpath (s, &rn->p, addpath_encode, addpath_tx_id);
807 else
808 {
809 /* Encode the prefix in MP_REACH_NLRI attribute */
810 if (rn->prn)
811 prd = (struct prefix_rd *) &rn->prn->p;
812
813 if (safi == SAFI_LABELED_UNICAST)
814 label = bgp_adv_label(rn, binfo, peer, afi, safi);
815 else
816 if (binfo && binfo->extra)
817 label = binfo->extra->label;
818
819 if (stream_empty (snlri))
820 mpattrlen_pos = bgp_packet_mpattr_start (snlri, peer, afi, safi,
821 &vecarr, adv->baa->attr);
822
823 bgp_packet_mpattr_prefix (snlri, afi, safi, &rn->p, prd,
824 &label, addpath_encode, addpath_tx_id, adv->baa->attr);
825 }
826
827 num_pfx++;
828
829 if (bgp_debug_update(NULL, &rn->p, subgrp->update_group, 0))
830 {
831 char pfx_buf[BGP_PRD_PATH_STRLEN];
832
833 if (!send_attr_printed)
834 {
835 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " send UPDATE w/ attr: %s",
836 subgrp->update_group->id, subgrp->id, send_attr_str);
837 if (!stream_empty (snlri))
838 {
839 iana_afi_t pkt_afi;
840 safi_t pkt_safi;
841
842 pkt_afi = afi_int2iana (afi);
843 pkt_safi = safi_int2iana (safi);
844 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " send MP_REACH for afi/safi %d/%d",
845 subgrp->update_group->id, subgrp->id, pkt_afi, pkt_safi);
846 }
847
848 send_attr_printed = 1;
849 }
850
851 bgp_debug_rdpfxpath2str (afi, safi, prd, &rn->p, &label,
852 addpath_encode, addpath_tx_id,
853 pfx_buf, sizeof (pfx_buf));
854 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " send UPDATE %s",
855 subgrp->update_group->id, subgrp->id, pfx_buf);
856 }
857
858 /* Synchnorize attribute. */
859 if (adj->attr)
860 bgp_attr_unintern (&adj->attr);
861 else
862 subgrp->scount++;
863
864 adj->attr = bgp_attr_intern (adv->baa->attr);
865
866 adv = bgp_advertise_clean_subgroup (subgrp, adj);
867 }
868
869 if (!stream_empty (s))
870 {
871 if (!stream_empty (snlri))
872 {
873 bgp_packet_mpattr_end (snlri, mpattrlen_pos);
874 total_attr_len += stream_get_endp (snlri);
875 }
876
877 /* set the total attribute length correctly */
878 stream_putw_at (s, attrlen_pos, total_attr_len);
879
880 if (!stream_empty (snlri))
881 {
882 packet = stream_dupcat (s, snlri, mpattr_pos);
883 bpacket_attr_vec_arr_update (&vecarr, mpattr_pos);
884 }
885 else
886 packet = stream_dup (s);
887 bgp_packet_set_size (packet);
888 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
889 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " send UPDATE len %zd numpfx %d",
890 subgrp->update_group->id, subgrp->id,
891 (stream_get_endp(packet) - stream_get_getp(packet)), num_pfx);
892 pkt = bpacket_queue_add (SUBGRP_PKTQ (subgrp), packet, &vecarr);
893 stream_reset (s);
894 stream_reset (snlri);
895 return pkt;
896 }
897 return NULL;
898 }
899
900 /* Make BGP withdraw packet. */
901 /* For ipv4 unicast:
902 16-octet marker | 2-octet length | 1-octet type |
903 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0)
904 */
905 /* For other afi/safis:
906 16-octet marker | 2-octet length | 1-octet type |
907 2-octet withdrawn route length (=0) | 2-octet attrlen |
908 mp_unreach attr type | attr len | afi | safi | withdrawn prefixes
909 */
910 struct bpacket *
911 subgroup_withdraw_packet (struct update_subgroup *subgrp)
912 {
913 struct bpacket *pkt;
914 struct stream *s;
915 struct bgp_adj_out *adj;
916 struct bgp_advertise *adv;
917 struct peer *peer;
918 struct bgp_node *rn;
919 bgp_size_t unfeasible_len;
920 bgp_size_t total_attr_len;
921 size_t mp_start = 0;
922 size_t attrlen_pos = 0;
923 size_t mplen_pos = 0;
924 u_char first_time = 1;
925 afi_t afi;
926 safi_t safi;
927 int space_remaining = 0;
928 int space_needed = 0;
929 int num_pfx = 0;
930 int addpath_encode = 0;
931 u_int32_t addpath_tx_id = 0;
932 struct prefix_rd *prd = NULL;
933
934
935 if (!subgrp)
936 return NULL;
937
938 if (bpacket_queue_is_full (SUBGRP_INST (subgrp), SUBGRP_PKTQ (subgrp)))
939 return NULL;
940
941 peer = SUBGRP_PEER (subgrp);
942 afi = SUBGRP_AFI (subgrp);
943 safi = SUBGRP_SAFI (subgrp);
944 s = subgrp->work;
945 stream_reset (s);
946 addpath_encode = bgp_addpath_encode_tx (peer, afi, safi);
947
948 while ((adv = BGP_ADV_FIFO_HEAD (&subgrp->sync->withdraw)) != NULL)
949 {
950 assert (adv->rn);
951 adj = adv->adj;
952 rn = adv->rn;
953 addpath_tx_id = adj->addpath_tx_id;
954
955 space_remaining = STREAM_REMAIN (s) -
956 BGP_MAX_PACKET_SIZE_OVERFLOW;
957 space_needed = BGP_NLRI_LENGTH + BGP_TOTAL_ATTR_LEN +
958 bgp_packet_mpattr_prefix_size (afi, safi, &rn->p);
959
960 if (space_remaining < space_needed)
961 break;
962
963 if (stream_empty (s))
964 {
965 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
966 stream_putw (s, 0); /* unfeasible routes length */
967 }
968 else
969 first_time = 0;
970
971 if (afi == AFI_IP && safi == SAFI_UNICAST &&
972 !peer_cap_enhe(peer, afi, safi))
973 stream_put_prefix_addpath (s, &rn->p, addpath_encode, addpath_tx_id);
974 else
975 {
976 if (rn->prn)
977 prd = (struct prefix_rd *) &rn->prn->p;
978
979 /* If first time, format the MP_UNREACH header */
980 if (first_time)
981 {
982 iana_afi_t pkt_afi;
983 safi_t pkt_safi;
984
985 pkt_afi = afi_int2iana (afi);
986 pkt_safi = safi_int2iana (safi);
987
988 attrlen_pos = stream_get_endp (s);
989 /* total attr length = 0 for now. reevaluate later */
990 stream_putw (s, 0);
991 mp_start = stream_get_endp (s);
992 mplen_pos = bgp_packet_mpunreach_start (s, afi, safi);
993 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
994 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " send MP_UNREACH for afi/safi %d/%d",
995 subgrp->update_group->id, subgrp->id, pkt_afi, pkt_safi);
996 }
997
998 bgp_packet_mpunreach_prefix (s, &rn->p, afi, safi, prd, NULL,
999 addpath_encode, addpath_tx_id, NULL);
1000 }
1001
1002 num_pfx++;
1003
1004 if (bgp_debug_update(NULL, &rn->p, subgrp->update_group, 0))
1005 {
1006 char pfx_buf[BGP_PRD_PATH_STRLEN];
1007
1008 bgp_debug_rdpfxpath2str (afi, safi, prd, &rn->p, NULL,
1009 addpath_encode, addpath_tx_id,
1010 pfx_buf, sizeof (pfx_buf));
1011 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " send UPDATE %s -- unreachable",
1012 subgrp->update_group->id, subgrp->id, pfx_buf);
1013 }
1014
1015 subgrp->scount--;
1016
1017 bgp_adj_out_remove_subgroup (rn, adj, subgrp);
1018 bgp_unlock_node (rn);
1019 }
1020
1021 if (!stream_empty (s))
1022 {
1023 if (afi == AFI_IP && safi == SAFI_UNICAST &&
1024 !peer_cap_enhe(peer, afi, safi))
1025 {
1026 unfeasible_len
1027 = stream_get_endp (s) - BGP_HEADER_SIZE - BGP_UNFEASIBLE_LEN;
1028 stream_putw_at (s, BGP_HEADER_SIZE, unfeasible_len);
1029 stream_putw (s, 0);
1030 }
1031 else
1032 {
1033 /* Set the mp_unreach attr's length */
1034 bgp_packet_mpunreach_end (s, mplen_pos);
1035
1036 /* Set total path attribute length. */
1037 total_attr_len = stream_get_endp (s) - mp_start;
1038 stream_putw_at (s, attrlen_pos, total_attr_len);
1039 }
1040 bgp_packet_set_size (s);
1041 if (bgp_debug_update(NULL, NULL, subgrp->update_group, 0))
1042 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " send UPDATE (withdraw) len %zd numpfx %d",
1043 subgrp->update_group->id, subgrp->id,
1044 (stream_get_endp(s) - stream_get_getp(s)), num_pfx);
1045 pkt = bpacket_queue_add (SUBGRP_PKTQ (subgrp), stream_dup (s), NULL);
1046 stream_reset (s);
1047 return pkt;
1048 }
1049
1050 return NULL;
1051 }
1052
1053 void
1054 subgroup_default_update_packet (struct update_subgroup *subgrp,
1055 struct attr *attr, struct peer *from)
1056 {
1057 struct stream *s;
1058 struct peer *peer;
1059 struct prefix p;
1060 unsigned long pos;
1061 bgp_size_t total_attr_len;
1062 afi_t afi;
1063 safi_t safi;
1064 struct bpacket_attr_vec_arr vecarr;
1065 int addpath_encode = 0;
1066
1067 if (DISABLE_BGP_ANNOUNCE)
1068 return;
1069
1070 if (!subgrp)
1071 return;
1072
1073 peer = SUBGRP_PEER (subgrp);
1074 afi = SUBGRP_AFI (subgrp);
1075 safi = SUBGRP_SAFI (subgrp);
1076 bpacket_attr_vec_arr_reset (&vecarr);
1077 addpath_encode = bgp_addpath_encode_tx (peer, afi, safi);
1078
1079 if (afi == AFI_IP)
1080 str2prefix ("0.0.0.0/0", &p);
1081 else
1082 str2prefix ("::/0", &p);
1083
1084 /* Logging the attribute. */
1085 if (bgp_debug_update(NULL, &p, subgrp->update_group, 0))
1086 {
1087 char attrstr[BUFSIZ];
1088 char buf[PREFIX_STRLEN];
1089 /* ' with addpath ID ' 17
1090 * max strlen of uint32 + 10
1091 * +/- (just in case) + 1
1092 * null terminator + 1
1093 * ============================ 29 */
1094 char tx_id_buf[30];
1095
1096 attrstr[0] = '\0';
1097
1098 bgp_dump_attr (attr, attrstr, BUFSIZ);
1099
1100 if (addpath_encode)
1101 snprintf(tx_id_buf, sizeof (tx_id_buf), " with addpath ID %u",
1102 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
1103
1104 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " send UPDATE %s%s %s",
1105 (SUBGRP_UPDGRP (subgrp))->id, subgrp->id,
1106 prefix2str (&p, buf, sizeof (buf)),
1107 tx_id_buf, attrstr);
1108 }
1109
1110 s = stream_new (BGP_MAX_PACKET_SIZE);
1111
1112 /* Make BGP update packet. */
1113 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
1114
1115 /* Unfeasible Routes Length. */
1116 stream_putw (s, 0);
1117
1118 /* Make place for total attribute length. */
1119 pos = stream_get_endp (s);
1120 stream_putw (s, 0);
1121 total_attr_len = bgp_packet_attribute (NULL, peer, s, attr, &vecarr, &p,
1122 afi, safi, from, NULL, NULL,
1123 addpath_encode,
1124 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
1125
1126 /* Set Total Path Attribute Length. */
1127 stream_putw_at (s, pos, total_attr_len);
1128
1129 /* NLRI set. */
1130 if (p.family == AF_INET && safi == SAFI_UNICAST &&
1131 !peer_cap_enhe(peer, afi, safi))
1132 stream_put_prefix_addpath (s, &p, addpath_encode, BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
1133
1134 /* Set size. */
1135 bgp_packet_set_size (s);
1136
1137 (void) bpacket_queue_add (SUBGRP_PKTQ (subgrp), s, &vecarr);
1138 subgroup_trigger_write(subgrp);
1139 }
1140
1141 void
1142 subgroup_default_withdraw_packet (struct update_subgroup *subgrp)
1143 {
1144 struct peer *peer;
1145 struct stream *s;
1146 struct prefix p;
1147 unsigned long attrlen_pos = 0;
1148 unsigned long cp;
1149 bgp_size_t unfeasible_len;
1150 bgp_size_t total_attr_len = 0;
1151 size_t mp_start = 0;
1152 size_t mplen_pos = 0;
1153 afi_t afi;
1154 safi_t safi;
1155 int addpath_encode = 0;
1156
1157 if (DISABLE_BGP_ANNOUNCE)
1158 return;
1159
1160 peer = SUBGRP_PEER (subgrp);
1161 afi = SUBGRP_AFI (subgrp);
1162 safi = SUBGRP_SAFI (subgrp);
1163 addpath_encode = bgp_addpath_encode_tx (peer, afi, safi);
1164
1165 if (afi == AFI_IP)
1166 str2prefix ("0.0.0.0/0", &p);
1167 else
1168 str2prefix ("::/0", &p);
1169
1170 if (bgp_debug_update(NULL, &p, subgrp->update_group, 0))
1171 {
1172 char buf[PREFIX_STRLEN];
1173 /* ' with addpath ID ' 17
1174 * max strlen of uint32 + 10
1175 * +/- (just in case) + 1
1176 * null terminator + 1
1177 * ============================ 29 */
1178 char tx_id_buf[30];
1179
1180 if (addpath_encode)
1181 snprintf(tx_id_buf, sizeof (tx_id_buf), " with addpath ID %u",
1182 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
1183
1184 zlog_debug ("u%" PRIu64 ":s%" PRIu64 " send UPDATE %s%s -- unreachable",
1185 (SUBGRP_UPDGRP (subgrp))->id, subgrp->id,
1186 prefix2str (&p, buf, sizeof (buf)), tx_id_buf);
1187 }
1188
1189 s = stream_new (BGP_MAX_PACKET_SIZE);
1190
1191 /* Make BGP update packet. */
1192 bgp_packet_set_marker (s, BGP_MSG_UPDATE);
1193
1194 /* Unfeasible Routes Length. */ ;
1195 cp = stream_get_endp (s);
1196 stream_putw (s, 0);
1197
1198 /* Withdrawn Routes. */
1199 if (p.family == AF_INET && safi == SAFI_UNICAST &&
1200 !peer_cap_enhe(peer, afi, safi))
1201 {
1202 stream_put_prefix_addpath (s, &p, addpath_encode,
1203 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE);
1204
1205 unfeasible_len = stream_get_endp (s) - cp - 2;
1206
1207 /* Set unfeasible len. */
1208 stream_putw_at (s, cp, unfeasible_len);
1209
1210 /* Set total path attribute length. */
1211 stream_putw (s, 0);
1212 }
1213 else
1214 {
1215 attrlen_pos = stream_get_endp (s);
1216 stream_putw (s, 0);
1217 mp_start = stream_get_endp (s);
1218 mplen_pos = bgp_packet_mpunreach_start (s, afi, safi);
1219 bgp_packet_mpunreach_prefix (s, &p, afi, safi, NULL, NULL,
1220 addpath_encode,
1221 BGP_ADDPATH_TX_ID_FOR_DEFAULT_ORIGINATE, NULL);
1222
1223 /* Set the mp_unreach attr's length */
1224 bgp_packet_mpunreach_end (s, mplen_pos);
1225
1226 /* Set total path attribute length. */
1227 total_attr_len = stream_get_endp (s) - mp_start;
1228 stream_putw_at (s, attrlen_pos, total_attr_len);
1229 }
1230
1231 bgp_packet_set_size (s);
1232
1233 (void) bpacket_queue_add (SUBGRP_PKTQ (subgrp), s, NULL);
1234 subgroup_trigger_write(subgrp);
1235 }
1236
1237 static void
1238 bpacket_vec_arr_inherit_attr_flags (struct bpacket_attr_vec_arr *vecarr,
1239 bpacket_attr_vec_type type,
1240 struct attr *attr)
1241 {
1242 if (CHECK_FLAG (attr->rmap_change_flags,
1243 BATTR_RMAP_NEXTHOP_PEER_ADDRESS))
1244 SET_FLAG (vecarr->entries[BGP_ATTR_VEC_NH].flags,
1245 BPKT_ATTRVEC_FLAGS_RMAP_NH_PEER_ADDRESS);
1246
1247 if (CHECK_FLAG (attr->rmap_change_flags, BATTR_REFLECTED))
1248 SET_FLAG (vecarr->entries[BGP_ATTR_VEC_NH].flags,
1249 BPKT_ATTRVEC_FLAGS_REFLECTED);
1250
1251 if (CHECK_FLAG (attr->rmap_change_flags,
1252 BATTR_RMAP_NEXTHOP_UNCHANGED))
1253 SET_FLAG (vecarr->entries[BGP_ATTR_VEC_NH].flags,
1254 BPKT_ATTRVEC_FLAGS_RMAP_NH_UNCHANGED);
1255
1256 if (CHECK_FLAG (attr->rmap_change_flags,
1257 BATTR_RMAP_IPV4_NHOP_CHANGED))
1258 SET_FLAG (vecarr->entries[BGP_ATTR_VEC_NH].flags,
1259 BPKT_ATTRVEC_FLAGS_RMAP_IPV4_NH_CHANGED);
1260
1261 if (CHECK_FLAG (attr->rmap_change_flags,
1262 BATTR_RMAP_IPV6_GLOBAL_NHOP_CHANGED))
1263 SET_FLAG (vecarr->entries[BGP_ATTR_VEC_NH].flags,
1264 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_GNH_CHANGED);
1265
1266 if (CHECK_FLAG (attr->rmap_change_flags,
1267 BATTR_RMAP_IPV6_LL_NHOP_CHANGED))
1268 SET_FLAG (vecarr->entries[BGP_ATTR_VEC_NH].flags,
1269 BPKT_ATTRVEC_FLAGS_RMAP_IPV6_LNH_CHANGED);
1270 }
1271
1272 /* Reset the Attributes vector array. The vector array is used to override
1273 * certain output parameters in the packet for a particular peer
1274 */
1275 void
1276 bpacket_attr_vec_arr_reset (struct bpacket_attr_vec_arr *vecarr)
1277 {
1278 int i;
1279
1280 if (!vecarr)
1281 return;
1282
1283 i = 0;
1284 while (i < BGP_ATTR_VEC_MAX)
1285 {
1286 vecarr->entries[i].flags = 0;
1287 vecarr->entries[i].offset = 0;
1288 i++;
1289 }
1290 }
1291
1292 /* Setup a particular node entry in the vecarr */
1293 void
1294 bpacket_attr_vec_arr_set_vec (struct bpacket_attr_vec_arr *vecarr,
1295 bpacket_attr_vec_type type, struct stream *s,
1296 struct attr *attr)
1297 {
1298 if (!vecarr)
1299 return;
1300 assert (type < BGP_ATTR_VEC_MAX);
1301
1302 SET_FLAG (vecarr->entries[type].flags, BPKT_ATTRVEC_FLAGS_UPDATED);
1303 vecarr->entries[type].offset = stream_get_endp (s);
1304 if (attr)
1305 bpacket_vec_arr_inherit_attr_flags(vecarr, type, attr);
1306 }