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