]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_aspath.c
Merge pull request #3856 from donaldsharp/dplane_use_after_free
[mirror_frr.git] / bgpd / bgp_aspath.c
CommitLineData
718e3744 1/* AS path management routines.
896014f4
DL
2 * Copyright (C) 1996, 97, 98, 99 Kunihiro Ishiguro
3 * Copyright (C) 2005 Sun Microsystems, Inc.
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
718e3744 21
22#include <zebra.h>
23
24#include "hash.h"
25#include "memory.h"
26#include "vector.h"
718e3744 27#include "log.h"
fe69a505 28#include "stream.h"
4dcadbef 29#include "command.h"
0b2aa3a0 30#include "jhash.h"
3f9c7369 31#include "queue.h"
039f3a34 32#include "filter.h"
718e3744 33
34#include "bgpd/bgpd.h"
35#include "bgpd/bgp_aspath.h"
0b2aa3a0
PJ
36#include "bgpd/bgp_debug.h"
37#include "bgpd/bgp_attr.h"
559aaa30 38#include "bgpd/bgp_errors.h"
6b0655a2 39
718e3744 40/* Attr. Flags and Attr. Type Code. */
ade6974d 41#define AS_HEADER_SIZE 2
718e3744 42
0b2aa3a0 43/* Now FOUR octets are used for AS value. */
718e3744 44#define AS_VALUE_SIZE sizeof (as_t)
0b2aa3a0
PJ
45/* This is the old one */
46#define AS16_VALUE_SIZE sizeof (as16_t)
718e3744 47
fe69a505 48/* Maximum protocol segment length value */
49#define AS_SEGMENT_MAX 255
50
51/* The following length and size macros relate specifically to Quagga's
52 * internal representation of AS-Segments, not per se to the on-wire
53 * sizes and lengths. At present (200508) they sort of match, however
54 * the ONLY functions which should now about the on-wire syntax are
55 * aspath_put, assegment_put and assegment_parse.
0b2aa3a0
PJ
56 *
57 * aspath_put returns bytes written, the only definitive record of
58 * size of wire-format attribute..
fe69a505 59 */
60
61/* Calculated size in bytes of ASN segment data to hold N ASN's */
d62a17ae 62#define ASSEGMENT_DATA_SIZE(N, S) \
63 ((N) * ((S) ? AS_VALUE_SIZE : AS16_VALUE_SIZE))
718e3744 64
fe69a505 65/* Calculated size of segment struct to hold N ASN's */
0b2aa3a0 66#define ASSEGMENT_SIZE(N,S) (AS_HEADER_SIZE + ASSEGMENT_DATA_SIZE (N,S))
fe69a505 67
68/* AS segment octet length. */
0b2aa3a0 69#define ASSEGMENT_LEN(X,S) ASSEGMENT_SIZE((X)->length,S)
fe69a505 70
71/* AS_SEQUENCE segments can be packed together */
72/* Can the types of X and Y be considered for packing? */
d62a17ae 73#define ASSEGMENT_TYPES_PACKABLE(X, Y) \
74 (((X)->type == (Y)->type) && ((X)->type == AS_SEQUENCE))
fe69a505 75/* Types and length of X,Y suitable for packing? */
d62a17ae 76#define ASSEGMENTS_PACKABLE(X, Y) \
77 (ASSEGMENT_TYPES_PACKABLE((X), (Y)) \
78 && (((X)->length + (Y)->length) <= AS_SEGMENT_MAX))
fe69a505 79
d62a17ae 80/* As segment header - the on-wire representation
fe69a505 81 * NOT the internal representation!
82 */
d62a17ae 83struct assegment_header {
d7c0a89a
QY
84 uint8_t type;
85 uint8_t length;
718e3744 86};
87
88/* Hash for aspath. This is the top level structure of AS path. */
da88ea82 89static struct hash *ashash;
8fdc32ab 90
91/* Stream for SNMP. See aspath_snmp_pathseg */
92static struct stream *snmp_stream;
6b0655a2 93
f669f7d2 94/* Callers are required to initialize the memory */
d62a17ae 95static as_t *assegment_data_new(int num)
fe69a505 96{
d62a17ae 97 return (XMALLOC(MTYPE_AS_SEG_DATA, ASSEGMENT_DATA_SIZE(num, 1)));
fe69a505 98}
99
d62a17ae 100static void assegment_data_free(as_t *asdata)
289d2501 101{
d62a17ae 102 XFREE(MTYPE_AS_SEG_DATA, asdata);
289d2501
LB
103}
104
d62a17ae 105const char *aspath_segment_type_str[] = {"as-invalid", "as-set", "as-sequence",
106 "as-confed-sequence", "as-confed-set"};
f1aa5d8a 107
fe69a505 108/* Get a new segment. Note that 0 is an allowed length,
109 * and will result in a segment with no allocated data segment.
110 * the caller should immediately assign data to the segment, as the segment
111 * otherwise is not generally valid
112 */
d7c0a89a 113static struct assegment *assegment_new(uint8_t type, unsigned short length)
d62a17ae 114{
115 struct assegment *new;
116
117 new = XCALLOC(MTYPE_AS_SEG, sizeof(struct assegment));
118
119 if (length)
120 new->as = assegment_data_new(length);
121
122 new->length = length;
123 new->type = type;
124
125 return new;
126}
127
128static void assegment_free(struct assegment *seg)
129{
130 if (!seg)
131 return;
132
133 if (seg->as)
134 assegment_data_free(seg->as);
135 memset(seg, 0xfe, sizeof(struct assegment));
136 XFREE(MTYPE_AS_SEG, seg);
137
138 return;
fe69a505 139}
140
141/* free entire chain of segments */
d62a17ae 142static void assegment_free_all(struct assegment *seg)
fe69a505 143{
d62a17ae 144 struct assegment *prev;
145
146 while (seg) {
147 prev = seg;
148 seg = seg->next;
149 assegment_free(prev);
150 }
fe69a505 151}
152
153/* Duplicate just the given assegment and its data */
d62a17ae 154static struct assegment *assegment_dup(struct assegment *seg)
fe69a505 155{
d62a17ae 156 struct assegment *new;
157
158 new = assegment_new(seg->type, seg->length);
159 memcpy(new->as, seg->as, ASSEGMENT_DATA_SIZE(new->length, 1));
160
161 return new;
fe69a505 162}
163
164/* Duplicate entire chain of assegments, return the head */
d62a17ae 165static struct assegment *assegment_dup_all(struct assegment *seg)
166{
167 struct assegment *new = NULL;
168 struct assegment *head = NULL;
169
170 while (seg) {
171 if (head) {
172 new->next = assegment_dup(seg);
173 new = new->next;
174 } else
175 head = new = assegment_dup(seg);
176
177 seg = seg->next;
178 }
179 return head;
fe69a505 180}
181
182/* prepend the as number to given segment, given num of times */
d62a17ae 183static struct assegment *assegment_prepend_asns(struct assegment *seg,
184 as_t asnum, int num)
fe69a505 185{
d62a17ae 186 as_t *newas;
187 int i;
fe69a505 188
d62a17ae 189 if (!num)
190 return seg;
f669f7d2 191
d62a17ae 192 if (num >= AS_SEGMENT_MAX)
193 return seg; /* we don't do huge prepends */
f669f7d2 194
d62a17ae 195 if ((newas = assegment_data_new(seg->length + num)) == NULL)
196 return seg;
197
198 for (i = 0; i < num; i++)
199 newas[i] = asnum;
200
201 memcpy(newas + num, seg->as, ASSEGMENT_DATA_SIZE(seg->length, 1));
202 assegment_data_free(seg->as);
203 seg->as = newas;
204 seg->length += num;
205
206 return seg;
fe69a505 207}
208
209/* append given array of as numbers to the segment */
d62a17ae 210static struct assegment *assegment_append_asns(struct assegment *seg,
211 as_t *asnos, int num)
fe69a505 212{
d62a17ae 213 as_t *newas;
fe69a505 214
d62a17ae 215 newas = XREALLOC(MTYPE_AS_SEG_DATA, seg->as,
216 ASSEGMENT_DATA_SIZE(seg->length + num, 1));
217
0ce1ca80
DS
218 seg->as = newas;
219 memcpy(seg->as + seg->length, asnos,
220 ASSEGMENT_DATA_SIZE(num, 1));
221 seg->length += num;
222 return seg;
fe69a505 223}
224
d62a17ae 225static int int_cmp(const void *p1, const void *p2)
fe69a505 226{
d62a17ae 227 const as_t *as1 = p1;
228 const as_t *as2 = p2;
229
230 return (*as1 == *as2) ? 0 : ((*as1 > *as2) ? 1 : -1);
fe69a505 231}
232
233/* normalise the segment.
234 * In particular, merge runs of AS_SEQUENCEs into one segment
235 * Internally, we do not care about the wire segment length limit, and
236 * we want each distinct AS_PATHs to have the exact same internal
237 * representation - eg, so that our hashing actually works..
238 */
d62a17ae 239static struct assegment *assegment_normalise(struct assegment *head)
240{
241 struct assegment *seg = head, *pin;
242 struct assegment *tmp;
243
244 if (!head)
245 return head;
246
247 while (seg) {
248 pin = seg;
249
250 /* Sort values SET segments, for determinism in paths to aid
251 * creation of hash values / path comparisons
252 * and because it helps other lesser implementations ;)
253 */
254 if (seg->type == AS_SET || seg->type == AS_CONFED_SET) {
255 int tail = 0;
256 int i;
257
258 qsort(seg->as, seg->length, sizeof(as_t), int_cmp);
259
260 /* weed out dupes */
261 for (i = 1; i < seg->length; i++) {
262 if (seg->as[tail] == seg->as[i])
263 continue;
264
265 tail++;
266 if (tail < i)
267 seg->as[tail] = seg->as[i];
268 }
269 /* seg->length can be 0.. */
270 if (seg->length)
271 seg->length = tail + 1;
272 }
273
274 /* read ahead from the current, pinned segment while the
275 * segments
276 * are packable/mergeable. Append all following packable
277 * segments
278 * to the segment we have pinned and remove these appended
279 * segments.
280 */
281 while (pin->next && ASSEGMENT_TYPES_PACKABLE(pin, pin->next)) {
282 tmp = pin->next;
283 seg = pin->next;
284
285 /* append the next sequence to the pinned sequence */
286 pin = assegment_append_asns(pin, seg->as, seg->length);
287
288 /* bypass the next sequence */
289 pin->next = seg->next;
290
291 /* get rid of the now referenceless segment */
292 assegment_free(tmp);
293 }
294
295 seg = pin->next;
0b2aa3a0 296 }
d62a17ae 297 return head;
718e3744 298}
299
d62a17ae 300static struct aspath *aspath_new(void)
718e3744 301{
d62a17ae 302 return XCALLOC(MTYPE_AS_PATH, sizeof(struct aspath));
303}
f1aa5d8a 304
d62a17ae 305/* Free AS path structure. */
306void aspath_free(struct aspath *aspath)
307{
308 if (!aspath)
309 return;
310 if (aspath->segments)
311 assegment_free_all(aspath->segments);
312 if (aspath->str)
313 XFREE(MTYPE_AS_STR, aspath->str);
314
315 if (aspath->json) {
316 json_object_free(aspath->json);
317 aspath->json = NULL;
318 }
f1aa5d8a 319
d62a17ae 320 XFREE(MTYPE_AS_PATH, aspath);
718e3744 321}
322
323/* Unintern aspath from AS path bucket. */
d62a17ae 324void aspath_unintern(struct aspath **aspath)
718e3744 325{
d62a17ae 326 struct aspath *ret;
327 struct aspath *asp = *aspath;
328
329 if (asp->refcnt)
330 asp->refcnt--;
718e3744 331
d62a17ae 332 if (asp->refcnt == 0) {
333 /* This aspath must exist in aspath hash table. */
334 ret = hash_release(ashash, asp);
335 assert(ret != NULL);
336 aspath_free(asp);
337 *aspath = NULL;
338 }
718e3744 339}
340
341/* Return the start or end delimiters for a particular Segment type */
342#define AS_SEG_START 0
343#define AS_SEG_END 1
d7c0a89a 344static char aspath_delimiter_char(uint8_t type, uint8_t which)
d62a17ae 345{
346 int i;
347 struct {
348 int type;
349 char start;
350 char end;
351 } aspath_delim_char[] = {{AS_SET, '{', '}'},
352 {AS_CONFED_SET, '[', ']'},
353 {AS_CONFED_SEQUENCE, '(', ')'},
354 {0}};
355
356 for (i = 0; aspath_delim_char[i].type != 0; i++) {
357 if (aspath_delim_char[i].type == type) {
358 if (which == AS_SEG_START)
359 return aspath_delim_char[i].start;
360 else if (which == AS_SEG_END)
361 return aspath_delim_char[i].end;
362 }
718e3744 363 }
d62a17ae 364 return ' ';
718e3744 365}
366
014b670e 367/* countup asns from this segment and index onward */
d62a17ae 368static int assegment_count_asns(struct assegment *seg, int from)
369{
370 int count = 0;
371 while (seg) {
372 if (!from)
373 count += seg->length;
374 else {
375 count += (seg->length - from);
376 from = 0;
377 }
378 seg = seg->next;
379 }
380 return count;
381}
382
383unsigned int aspath_count_confeds(struct aspath *aspath)
384{
385 int count = 0;
386 struct assegment *seg = aspath->segments;
387
388 while (seg) {
389 if (seg->type == AS_CONFED_SEQUENCE)
390 count += seg->length;
391 else if (seg->type == AS_CONFED_SET)
392 count++;
393
394 seg = seg->next;
395 }
396 return count;
397}
398
399unsigned int aspath_count_hops(const struct aspath *aspath)
400{
401 int count = 0;
402 struct assegment *seg = aspath->segments;
403
404 while (seg) {
405 if (seg->type == AS_SEQUENCE)
406 count += seg->length;
407 else if (seg->type == AS_SET)
408 count++;
409
410 seg = seg->next;
411 }
412 return count;
fe69a505 413}
414
0b2aa3a0
PJ
415/* Estimate size aspath /might/ take if encoded into an
416 * ASPATH attribute.
417 *
418 * This is a quick estimate, not definitive! aspath_put()
419 * may return a different number!!
420 */
d62a17ae 421unsigned int aspath_size(struct aspath *aspath)
fe69a505 422{
d62a17ae 423 int size = 0;
424 struct assegment *seg = aspath->segments;
425
426 while (seg) {
427 size += ASSEGMENT_SIZE(seg->length, 1);
428 seg = seg->next;
429 }
430 return size;
fe69a505 431}
432
2815e61f 433/* Return highest public ASN in path */
d62a17ae 434as_t aspath_highest(struct aspath *aspath)
435{
436 struct assegment *seg = aspath->segments;
437 as_t highest = 0;
438 unsigned int i;
439
440 while (seg) {
441 for (i = 0; i < seg->length; i++)
442 if (seg->as[i] > highest
443 && !BGP_AS_IS_PRIVATE(seg->as[i]))
444 highest = seg->as[i];
445 seg = seg->next;
446 }
447 return highest;
2815e61f
PJ
448}
449
bc3dd427 450/* Return the left-most ASN in path */
d62a17ae 451as_t aspath_leftmost(struct aspath *aspath)
bc3dd427 452{
d62a17ae 453 struct assegment *seg = aspath->segments;
454 as_t leftmost = 0;
bc3dd427 455
d62a17ae 456 if (seg && seg->length && seg->type == AS_SEQUENCE)
457 leftmost = seg->as[0];
bc3dd427 458
d62a17ae 459 return leftmost;
bc3dd427
DW
460}
461
0b2aa3a0 462/* Return 1 if there are any 4-byte ASes in the path */
d62a17ae 463unsigned int aspath_has_as4(struct aspath *aspath)
464{
465 struct assegment *seg = aspath->segments;
466 unsigned int i;
467
468 while (seg) {
469 for (i = 0; i < seg->length; i++)
470 if (seg->as[i] > BGP_AS_MAX)
471 return 1;
472 seg = seg->next;
473 }
474 return 0;
0b2aa3a0
PJ
475}
476
718e3744 477/* Convert aspath structure to string expression. */
68e1a55b 478static void aspath_make_str_count(struct aspath *as, bool make_json)
d62a17ae 479{
480 struct assegment *seg;
481 int str_size;
482 int len = 0;
483 char *str_buf;
484 json_object *jaspath_segments = NULL;
485 json_object *jseg = NULL;
486 json_object *jseg_list = NULL;
487
68e1a55b
DS
488 if (make_json) {
489 as->json = json_object_new_object();
490 jaspath_segments = json_object_new_array();
491 }
d62a17ae 492
493 /* Empty aspath. */
494 if (!as->segments) {
68e1a55b
DS
495 if (make_json) {
496 json_object_string_add(as->json, "string", "Local");
996c9314
LB
497 json_object_object_add(as->json, "segments",
498 jaspath_segments);
68e1a55b
DS
499 json_object_int_add(as->json, "length", 0);
500 }
d62a17ae 501 as->str = XMALLOC(MTYPE_AS_STR, 1);
502 as->str[0] = '\0';
503 as->str_len = 0;
504 return;
505 }
506
507 seg = as->segments;
508
509/* ASN takes 5 to 10 chars plus seperator, see below.
510 * If there is one differing segment type, we need an additional
511 * 2 chars for segment delimiters, and the final '\0'.
512 * Hopefully this is large enough to avoid hitting the realloc
513 * code below for most common sequences.
514 *
515 * This was changed to 10 after the well-known BGP assertion, which
516 * had hit some parts of the Internet in May of 2009.
517 */
014b670e 518#define ASN_STR_LEN (10 + 1)
d62a17ae 519 str_size = MAX(assegment_count_asns(seg, 0) * ASN_STR_LEN + 2 + 1,
520 ASPATH_STR_DEFAULT_LEN);
521 str_buf = XMALLOC(MTYPE_AS_STR, str_size);
522
523 while (seg) {
524 int i;
525 char seperator;
526
527 /* Check AS type validity. Set seperator for segment */
528 switch (seg->type) {
529 case AS_SET:
530 case AS_CONFED_SET:
531 seperator = ',';
532 break;
533 case AS_SEQUENCE:
534 case AS_CONFED_SEQUENCE:
535 seperator = ' ';
536 break;
537 default:
538 XFREE(MTYPE_AS_STR, str_buf);
539 as->str = NULL;
540 as->str_len = 0;
541 json_object_free(as->json);
542 as->json = NULL;
68e1a55b 543
d62a17ae 544 return;
545 }
546
547/* We might need to increase str_buf, particularly if path has
548 * differing segments types, our initial guesstimate above will
549 * have been wrong. Need 10 chars for ASN, a seperator each and
550 * potentially two segment delimiters, plus a space between each
551 * segment and trailing zero.
552 *
553 * This definitely didn't work with the value of 5 bytes and
554 * 32-bit ASNs.
555 */
014b670e 556#define SEGMENT_STR_LEN(X) (((X)->length * ASN_STR_LEN) + 2 + 1 + 1)
d62a17ae 557 if ((len + SEGMENT_STR_LEN(seg)) > str_size) {
558 str_size = len + SEGMENT_STR_LEN(seg);
559 str_buf = XREALLOC(MTYPE_AS_STR, str_buf, str_size);
560 }
014b670e
DO
561#undef ASN_STR_LEN
562#undef SEGMENT_STR_LEN
d62a17ae 563
564 if (seg->type != AS_SEQUENCE)
565 len += snprintf(
566 str_buf + len, str_size - len, "%c",
567 aspath_delimiter_char(seg->type, AS_SEG_START));
568
68e1a55b
DS
569 if (make_json)
570 jseg_list = json_object_new_array();
d62a17ae 571
572 /* write out the ASNs, with their seperators, bar the last one*/
573 for (i = 0; i < seg->length; i++) {
68e1a55b 574 if (make_json)
996c9314
LB
575 json_object_array_add(
576 jseg_list,
577 json_object_new_int(seg->as[i]));
d62a17ae 578
579 len += snprintf(str_buf + len, str_size - len, "%u",
580 seg->as[i]);
581
582 if (i < (seg->length - 1))
583 len += snprintf(str_buf + len, str_size - len,
584 "%c", seperator);
585 }
586
68e1a55b
DS
587 if (make_json) {
588 jseg = json_object_new_object();
996c9314
LB
589 json_object_string_add(
590 jseg, "type",
591 aspath_segment_type_str[seg->type]);
68e1a55b
DS
592 json_object_object_add(jseg, "list", jseg_list);
593 json_object_array_add(jaspath_segments, jseg);
594 }
d62a17ae 595
596 if (seg->type != AS_SEQUENCE)
597 len += snprintf(
598 str_buf + len, str_size - len, "%c",
599 aspath_delimiter_char(seg->type, AS_SEG_END));
600 if (seg->next)
601 len += snprintf(str_buf + len, str_size - len, " ");
602
603 seg = seg->next;
604 }
605
606 assert(len < str_size);
607
608 str_buf[len] = '\0';
609 as->str = str_buf;
610 as->str_len = len;
611
68e1a55b
DS
612 if (make_json) {
613 json_object_string_add(as->json, "string", str_buf);
614 json_object_object_add(as->json, "segments", jaspath_segments);
615 json_object_int_add(as->json, "length", aspath_count_hops(as));
616 }
617
d62a17ae 618 return;
619}
620
68e1a55b 621void aspath_str_update(struct aspath *as, bool make_json)
d62a17ae 622{
623 if (as->str)
624 XFREE(MTYPE_AS_STR, as->str);
625
626 if (as->json) {
627 json_object_free(as->json);
628 as->json = NULL;
629 }
630
68e1a55b 631 aspath_make_str_count(as, make_json);
fe69a505 632}
633
718e3744 634/* Intern allocated AS path. */
d62a17ae 635struct aspath *aspath_intern(struct aspath *aspath)
718e3744 636{
d62a17ae 637 struct aspath *find;
f669f7d2 638
d62a17ae 639 /* Assert this AS path structure is not interned and has the string
640 representation built. */
641 assert(aspath->refcnt == 0);
642 assert(aspath->str);
718e3744 643
d62a17ae 644 /* Check AS path hash. */
645 find = hash_get(ashash, aspath, hash_alloc_intern);
646 if (find != aspath)
647 aspath_free(aspath);
718e3744 648
d62a17ae 649 find->refcnt++;
718e3744 650
d62a17ae 651 return find;
718e3744 652}
653
654/* Duplicate aspath structure. Created same aspath structure but
655 reference count and AS path string is cleared. */
d62a17ae 656struct aspath *aspath_dup(struct aspath *aspath)
718e3744 657{
d62a17ae 658 unsigned short buflen = aspath->str_len + 1;
659 struct aspath *new;
718e3744 660
d62a17ae 661 new = XCALLOC(MTYPE_AS_PATH, sizeof(struct aspath));
662 new->json = NULL;
718e3744 663
d62a17ae 664 if (aspath->segments)
665 new->segments = assegment_dup_all(aspath->segments);
718e3744 666
d62a17ae 667 if (!aspath->str)
668 return new;
f669f7d2 669
d62a17ae 670 new->str = XMALLOC(MTYPE_AS_STR, buflen);
671 new->str_len = aspath->str_len;
f669f7d2 672
d62a17ae 673 /* copy the string data */
674 if (aspath->str_len > 0)
675 memcpy(new->str, aspath->str, buflen);
676 else
677 new->str[0] = '\0';
718e3744 678
d62a17ae 679 return new;
718e3744 680}
681
d62a17ae 682static void *aspath_hash_alloc(void *arg)
718e3744 683{
d62a17ae 684 const struct aspath *aspath = arg;
685 struct aspath *new;
718e3744 686
d62a17ae 687 /* Malformed AS path value. */
688 assert(aspath->str);
718e3744 689
d62a17ae 690 /* New aspath structure is needed. */
691 new = XMALLOC(MTYPE_AS_PATH, sizeof(struct aspath));
f669f7d2 692
d62a17ae 693 /* Reuse segments and string representation */
694 new->refcnt = 0;
695 new->segments = aspath->segments;
696 new->str = aspath->str;
697 new->str_len = aspath->str_len;
698 new->json = aspath->json;
f669f7d2 699
d62a17ae 700 return new;
718e3744 701}
702
ab005298 703/* parse as-segment byte stream in struct assegment */
d62a17ae 704static int assegments_parse(struct stream *s, size_t length,
705 struct assegment **result, int use32bit)
706{
707 struct assegment_header segh;
708 struct assegment *seg, *prev = NULL, *head = NULL;
709 size_t bytes = 0;
710
711 /* empty aspath (ie iBGP or somesuch) */
712 if (length == 0)
713 return 0;
714
715 if (BGP_DEBUG(as4, AS4_SEGMENT))
716 zlog_debug(
717 "[AS4SEG] Parse aspath segment: got total byte length %lu",
718 (unsigned long)length);
719 /* basic checks */
720 if ((STREAM_READABLE(s) < length)
721 || (STREAM_READABLE(s) < AS_HEADER_SIZE)
722 || (length % AS16_VALUE_SIZE))
723 return -1;
724
725 while (bytes < length) {
726 int i;
727 size_t seg_size;
728
729 if ((length - bytes) <= AS_HEADER_SIZE) {
730 if (head)
731 assegment_free_all(head);
732 return -1;
733 }
734
735 /* softly softly, get the header first on its own */
736 segh.type = stream_getc(s);
737 segh.length = stream_getc(s);
738
739 seg_size = ASSEGMENT_SIZE(segh.length, use32bit);
740
741 if (BGP_DEBUG(as4, AS4_SEGMENT))
742 zlog_debug(
743 "[AS4SEG] Parse aspath segment: got type %d, length %d",
744 segh.type, segh.length);
745
746 /* check it.. */
747 if (((bytes + seg_size) > length)
748 /* 1771bis 4.3b: seg length contains one or more */
749 || (segh.length == 0)
750 /* Paranoia in case someone changes type of segment length.
751 * Shift both values by 0x10 to make the comparison operate
752 * on more, than 8 bits (otherwise it's a warning, bug
753 * #564).
754 */
755 || ((sizeof segh.length > 1)
756 && (0x10 + segh.length > 0x10 + AS_SEGMENT_MAX))) {
757 if (head)
758 assegment_free_all(head);
759 return -1;
760 }
761
762 switch (segh.type) {
763 case AS_SEQUENCE:
764 case AS_SET:
765 case AS_CONFED_SEQUENCE:
766 case AS_CONFED_SET:
767 break;
768 default:
769 if (head)
770 assegment_free_all(head);
771 return -1;
772 }
773
774 /* now its safe to trust lengths */
775 seg = assegment_new(segh.type, segh.length);
776
777 if (head)
778 prev->next = seg;
779 else /* it's the first segment */
780 head = prev = seg;
781
782 for (i = 0; i < segh.length; i++)
783 seg->as[i] =
784 (use32bit) ? stream_getl(s) : stream_getw(s);
785
786 bytes += seg_size;
787
788 if (BGP_DEBUG(as4, AS4_SEGMENT))
789 zlog_debug(
790 "[AS4SEG] Parse aspath segment: Bytes now: %lu",
791 (unsigned long)bytes);
792
793 prev = seg;
794 }
795
796 *result = assegment_normalise(head);
797 return 0;
fe69a505 798}
799
ab005298
PJ
800/* AS path parse function. pnt is a pointer to byte stream and length
801 is length of byte stream. If there is same AS path in the the AS
d62a17ae 802 path hash then return it else make new AS path structure.
803
b881c707 804 On error NULL is returned.
cddb8112 805 */
d62a17ae 806struct aspath *aspath_parse(struct stream *s, size_t length, int use32bit)
807{
808 struct aspath as;
809 struct aspath *find;
810
811 /* If length is odd it's malformed AS path. */
812 /* Nit-picking: if (use32bit == 0) it is malformed if odd,
813 * otherwise its malformed when length is larger than 2 and (length-2)
814 * is not dividable by 4.
815 * But... this time we're lazy
816 */
817 if (length % AS16_VALUE_SIZE)
818 return NULL;
819
820 memset(&as, 0, sizeof(struct aspath));
821 if (assegments_parse(s, length, &as.segments, use32bit) < 0)
822 return NULL;
823
824 /* If already same aspath exist then return it. */
825 find = hash_get(ashash, &as, aspath_hash_alloc);
826
827 /* bug! should not happen, let the daemon crash below */
828 assert(find);
829
830 /* if the aspath was already hashed free temporary memory. */
831 if (find->refcnt) {
832 assegment_free_all(as.segments);
833 /* aspath_key_make() always updates the string */
834 XFREE(MTYPE_AS_STR, as.str);
835 if (as.json) {
836 json_object_free(as.json);
837 as.json = NULL;
838 }
4390a8ee 839 }
f669f7d2 840
d62a17ae 841 find->refcnt++;
718e3744 842
d62a17ae 843 return find;
718e3744 844}
845
d62a17ae 846static void assegment_data_put(struct stream *s, as_t *as, int num,
847 int use32bit)
fe69a505 848{
d62a17ae 849 int i;
850 assert(num <= AS_SEGMENT_MAX);
851
852 for (i = 0; i < num; i++)
853 if (use32bit)
854 stream_putl(s, as[i]);
855 else {
856 if (as[i] <= BGP_AS_MAX)
857 stream_putw(s, as[i]);
858 else
859 stream_putw(s, BGP_AS_TRANS);
860 }
fe69a505 861}
718e3744 862
d7c0a89a 863static size_t assegment_header_put(struct stream *s, uint8_t type, int length)
718e3744 864{
d62a17ae 865 size_t lenp;
866 assert(length <= AS_SEGMENT_MAX);
867 stream_putc(s, type);
868 lenp = stream_get_endp(s);
869 stream_putc(s, length);
870 return lenp;
fe69a505 871}
718e3744 872
fe69a505 873/* write aspath data to stream */
d62a17ae 874size_t aspath_put(struct stream *s, struct aspath *as, int use32bit)
875{
876 struct assegment *seg = as->segments;
877 size_t bytes = 0;
878
879 if (!seg || seg->length == 0)
880 return 0;
881
882 if (seg) {
883 /*
884 * Hey, what do we do when we have > STREAM_WRITABLE(s) here?
885 * At the moment, we would write out a partial aspath, and our
886 * peer
887 * will complain and drop the session :-/
888 *
889 * The general assumption here is that many things tested will
890 * never happen. And, in real live, up to now, they have not.
891 */
9d303b37
DL
892 while (seg && (ASSEGMENT_LEN(seg, use32bit)
893 <= STREAM_WRITEABLE(s))) {
d62a17ae 894 struct assegment *next = seg->next;
895 int written = 0;
896 int asns_packed = 0;
897 size_t lenp;
898
899 /* Overlength segments have to be split up */
900 while ((seg->length - written) > AS_SEGMENT_MAX) {
901 assegment_header_put(s, seg->type,
902 AS_SEGMENT_MAX);
248c86da 903 assegment_data_put(s, (seg->as + written), AS_SEGMENT_MAX,
d62a17ae 904 use32bit);
905 written += AS_SEGMENT_MAX;
996c9314
LB
906 bytes += ASSEGMENT_SIZE(AS_SEGMENT_MAX,
907 use32bit);
d62a17ae 908 }
909
910 /* write the final segment, probably is also the first
911 */
912 lenp = assegment_header_put(s, seg->type,
913 seg->length - written);
914 assegment_data_put(s, (seg->as + written),
915 seg->length - written, use32bit);
916
917 /* Sequence-type segments can be 'packed' together
918 * Case of a segment which was overlength and split up
919 * will be missed here, but that doesn't matter.
920 */
921 while (next && ASSEGMENTS_PACKABLE(seg, next)) {
922 /* NB: We should never normally get here given
923 * we
924 * normalise aspath data when parse them.
925 * However, better
926 * safe than sorry. We potentially could call
927 * assegment_normalise here instead, but it's
928 * cheaper and
929 * easier to do it on the fly here rather than
930 * go through
931 * the segment list twice every time we write
932 * out
933 * aspath's.
934 */
935
936 /* Next segment's data can fit in this one */
937 assegment_data_put(s, next->as, next->length,
938 use32bit);
939
940 /* update the length of the segment header */
941 stream_putc_at(s, lenp,
942 seg->length - written
943 + next->length);
944 asns_packed += next->length;
945
946 next = next->next;
947 }
948
949 bytes += ASSEGMENT_SIZE(
950 seg->length - written + asns_packed, use32bit);
951 seg = next;
952 }
953 }
954 return bytes;
fe69a505 955}
956
957/* This is for SNMP BGP4PATHATTRASPATHSEGMENT
958 * We have no way to manage the storage, so we use a static stream
959 * wrapper around aspath_put.
960 */
d7c0a89a 961uint8_t *aspath_snmp_pathseg(struct aspath *as, size_t *varlen)
fe69a505 962{
963#define SNMP_PATHSEG_MAX 1024
8fdc32ab 964
d62a17ae 965 if (!snmp_stream)
966 snmp_stream = stream_new(SNMP_PATHSEG_MAX);
967 else
968 stream_reset(snmp_stream);
969
970 if (!as) {
971 *varlen = 0;
972 return NULL;
973 }
974 aspath_put(snmp_stream, as, 0); /* use 16 bit for now here */
975
976 *varlen = stream_get_endp(snmp_stream);
977 return stream_pnt(snmp_stream);
978}
979
fe69a505 980#define min(A,B) ((A) < (B) ? (A) : (B))
718e3744 981
d62a17ae 982static struct assegment *aspath_aggregate_as_set_add(struct aspath *aspath,
983 struct assegment *asset,
984 as_t as)
985{
986 int i;
987
988 /* If this is first AS set member, create new as-set segment. */
989 if (asset == NULL) {
990 asset = assegment_new(AS_SET, 1);
991 if (!aspath->segments)
992 aspath->segments = asset;
993 else {
994 struct assegment *seg = aspath->segments;
995 while (seg->next)
996 seg = seg->next;
997 seg->next = asset;
998 }
999 asset->type = AS_SET;
1000 asset->length = 1;
1001 asset->as[0] = as;
1002 } else {
1003 /* Check this AS value already exists or not. */
1004 for (i = 0; i < asset->length; i++)
1005 if (asset->as[i] == as)
1006 return asset;
1007
1008 asset->length++;
1009 asset->as = XREALLOC(MTYPE_AS_SEG_DATA, asset->as,
1010 asset->length * AS_VALUE_SIZE);
1011 asset->as[asset->length - 1] = as;
1012 }
1013
1014
1015 return asset;
718e3744 1016}
1017
1018/* Modify as1 using as2 for aggregation. */
d62a17ae 1019struct aspath *aspath_aggregate(struct aspath *as1, struct aspath *as2)
1020{
1021 int i;
1022 int minlen = 0;
1023 int match = 0;
1024 int from;
1025 struct assegment *seg1 = as1->segments;
1026 struct assegment *seg2 = as2->segments;
1027 struct aspath *aspath = NULL;
1028 struct assegment *asset = NULL;
1029 struct assegment *prevseg = NULL;
1030
1031 /* First of all check common leading sequence. */
1032 while (seg1 && seg2) {
1033 /* Check segment type. */
1034 if (seg1->type != seg2->type)
1035 break;
1036
1037 /* Minimum segment length. */
1038 minlen = min(seg1->length, seg2->length);
1039
1040 for (match = 0; match < minlen; match++)
1041 if (seg1->as[match] != seg2->as[match])
1042 break;
1043
1044 if (match) {
1045 struct assegment *seg = assegment_new(seg1->type, 0);
1046
1047 seg = assegment_append_asns(seg, seg1->as, match);
1048
1049 if (!aspath) {
1050 aspath = aspath_new();
1051 aspath->segments = seg;
1052 } else
1053 prevseg->next = seg;
1054
1055 prevseg = seg;
1056 }
1057
1058 if (match != minlen || match != seg1->length
1059 || seg1->length != seg2->length)
1060 break;
1061 /* We are moving on to the next segment to reset match */
1062 else
1063 match = 0;
1064
1065 seg1 = seg1->next;
1066 seg2 = seg2->next;
1067 }
1068
1069 if (!aspath)
1070 aspath = aspath_new();
1071
1072 /* Make as-set using rest of all information. */
1073 from = match;
1074 while (seg1) {
1075 for (i = from; i < seg1->length; i++)
1076 asset = aspath_aggregate_as_set_add(aspath, asset,
1077 seg1->as[i]);
1078
1079 from = 0;
1080 seg1 = seg1->next;
718e3744 1081 }
1082
d62a17ae 1083 from = match;
1084 while (seg2) {
1085 for (i = from; i < seg2->length; i++)
1086 asset = aspath_aggregate_as_set_add(aspath, asset,
1087 seg2->as[i]);
1088
1089 from = 0;
1090 seg2 = seg2->next;
1091 }
1092
1093 assegment_normalise(aspath->segments);
68e1a55b 1094 aspath_str_update(aspath, false);
d62a17ae 1095 return aspath;
718e3744 1096}
1097
1098/* When a BGP router receives an UPDATE with an MP_REACH_NLRI
1099 attribute, check the leftmost AS number in the AS_PATH attribute is
d62a17ae 1100 or not the peer's AS number. */
1101int aspath_firstas_check(struct aspath *aspath, as_t asno)
718e3744 1102{
d62a17ae 1103 if ((aspath == NULL) || (aspath->segments == NULL))
1104 return 0;
1105
1106 if (aspath->segments && (aspath->segments->type == AS_SEQUENCE)
1107 && (aspath->segments->as[0] == asno))
1108 return 1;
718e3744 1109
d62a17ae 1110 return 0;
718e3744 1111}
1112
d62a17ae 1113unsigned int aspath_get_first_as(struct aspath *aspath)
06370dac 1114{
d62a17ae 1115 if (aspath == NULL || aspath->segments == NULL)
1116 return 0;
06370dac 1117
d62a17ae 1118 return aspath->segments->as[0];
06370dac
DW
1119}
1120
d62a17ae 1121unsigned int aspath_get_last_as(struct aspath *aspath)
aac9ef6c 1122{
d62a17ae 1123 int i;
1124 unsigned int last_as = 0;
1125 const struct assegment *seg;
aac9ef6c 1126
d62a17ae 1127 if (aspath == NULL || aspath->segments == NULL)
1128 return last_as;
aac9ef6c 1129
d62a17ae 1130 seg = aspath->segments;
aac9ef6c 1131
d62a17ae 1132 while (seg) {
1133 if (seg->type == AS_SEQUENCE || seg->type == AS_CONFED_SEQUENCE)
1134 for (i = 0; i < seg->length; i++)
1135 last_as = seg->as[i];
1136 seg = seg->next;
1137 }
aac9ef6c 1138
d62a17ae 1139 return last_as;
aac9ef6c
DW
1140}
1141
1f742f21 1142/* AS path loop check. If aspath contains asno then return >= 1. */
d62a17ae 1143int aspath_loop_check(struct aspath *aspath, as_t asno)
1144{
1145 struct assegment *seg;
1146 int count = 0;
1147
1148 if ((aspath == NULL) || (aspath->segments == NULL))
1149 return 0;
1150
1151 seg = aspath->segments;
1152
1153 while (seg) {
1154 int i;
1155
1156 for (i = 0; i < seg->length; i++)
1157 if (seg->as[i] == asno)
1158 count++;
1159
1160 seg = seg->next;
1161 }
1162 return count;
718e3744 1163}
1164
1165/* When all of AS path is private AS return 1. */
d62a17ae 1166int aspath_private_as_check(struct aspath *aspath)
718e3744 1167{
d62a17ae 1168 struct assegment *seg;
5000f21c 1169
d62a17ae 1170 if (!(aspath && aspath->segments))
1171 return 0;
5000f21c 1172
d62a17ae 1173 seg = aspath->segments;
718e3744 1174
d62a17ae 1175 while (seg) {
1176 int i;
5000f21c 1177
d62a17ae 1178 for (i = 0; i < seg->length; i++) {
1179 if (!BGP_AS_IS_PRIVATE(seg->as[i]))
1180 return 0;
1181 }
1182 seg = seg->next;
718e3744 1183 }
d62a17ae 1184 return 1;
718e3744 1185}
1186
c7122e14 1187/* Return True if the entire ASPATH consist of the specified ASN */
d62a17ae 1188int aspath_single_asn_check(struct aspath *aspath, as_t asn)
c7122e14 1189{
d62a17ae 1190 struct assegment *seg;
c7122e14 1191
d62a17ae 1192 if (!(aspath && aspath->segments))
1193 return 0;
c7122e14 1194
d62a17ae 1195 seg = aspath->segments;
c7122e14 1196
d62a17ae 1197 while (seg) {
1198 int i;
c7122e14 1199
d62a17ae 1200 for (i = 0; i < seg->length; i++) {
1201 if (seg->as[i] != asn)
1202 return 0;
1203 }
1204 seg = seg->next;
c7122e14 1205 }
d62a17ae 1206 return 1;
c7122e14
DS
1207}
1208
1209/* Replace all instances of the target ASN with our own ASN */
d62a17ae 1210struct aspath *aspath_replace_specific_asn(struct aspath *aspath,
1211 as_t target_asn, as_t our_asn)
c7122e14 1212{
d62a17ae 1213 struct aspath *new;
1214 struct assegment *seg;
c7122e14 1215
d62a17ae 1216 new = aspath_dup(aspath);
1217 seg = new->segments;
c7122e14 1218
d62a17ae 1219 while (seg) {
1220 int i;
c7122e14 1221
d62a17ae 1222 for (i = 0; i < seg->length; i++) {
1223 if (seg->as[i] == target_asn)
1224 seg->as[i] = our_asn;
1225 }
1226 seg = seg->next;
c7122e14 1227 }
c7122e14 1228
68e1a55b 1229 aspath_str_update(new, false);
d62a17ae 1230 return new;
c7122e14
DS
1231}
1232
5000f21c 1233/* Replace all private ASNs with our own ASN */
d62a17ae 1234struct aspath *aspath_replace_private_asns(struct aspath *aspath, as_t asn)
5000f21c 1235{
d62a17ae 1236 struct aspath *new;
1237 struct assegment *seg;
5000f21c 1238
d62a17ae 1239 new = aspath_dup(aspath);
1240 seg = new->segments;
5000f21c 1241
d62a17ae 1242 while (seg) {
1243 int i;
5000f21c 1244
d62a17ae 1245 for (i = 0; i < seg->length; i++) {
1246 if (BGP_AS_IS_PRIVATE(seg->as[i]))
1247 seg->as[i] = asn;
1248 }
1249 seg = seg->next;
5000f21c 1250 }
5000f21c 1251
68e1a55b 1252 aspath_str_update(new, false);
d62a17ae 1253 return new;
5000f21c
DS
1254}
1255
1256/* Remove all private ASNs */
d62a17ae 1257struct aspath *aspath_remove_private_asns(struct aspath *aspath)
1258{
1259 struct aspath *new;
1260 struct assegment *seg;
1261 struct assegment *new_seg;
1262 struct assegment *last_new_seg;
1263 int i;
1264 int j;
1265 int public = 0;
1266
1267 new = XCALLOC(MTYPE_AS_PATH, sizeof(struct aspath));
1268
1269 new->json = NULL;
1270 new_seg = NULL;
1271 last_new_seg = NULL;
1272 seg = aspath->segments;
1273 while (seg) {
1274 public
1275 = 0;
1276 for (i = 0; i < seg->length; i++) {
1277 // ASN is public
1278 if (!BGP_AS_IS_PRIVATE(seg->as[i])) {
1279 public
1280 ++;
1281 }
1282 }
1283
1284 // The entire segment is private so skip it
1285 if (!public) {
1286 seg = seg->next;
1287 continue;
1288 }
1289
1290 // The entire segment is public so copy it
1291 else if (public == seg->length) {
1292 new_seg = assegment_dup(seg);
1293 }
1294
1295 // The segment is a mix of public and private ASNs. Copy as many
1296 // spots as
1297 // there are public ASNs then come back and fill in only the
1298 // public ASNs.
1299 else {
1300 new_seg = assegment_new(seg->type, public);
1301 j = 0;
1302 for (i = 0; i < seg->length; i++) {
1303 // ASN is public
1304 if (!BGP_AS_IS_PRIVATE(seg->as[i])) {
1305 new_seg->as[j] = seg->as[i];
1306 j++;
1307 }
1308 }
1309 }
1310
1311 // This is the first segment so set the aspath segments pointer
1312 // to this one
1313 if (!last_new_seg)
1314 new->segments = new_seg;
1315 else
1316 last_new_seg->next = new_seg;
1317
1318 last_new_seg = new_seg;
1319 seg = seg->next;
1320 }
1321
68e1a55b 1322 aspath_str_update(new, false);
d62a17ae 1323 return new;
1324}
1325
1326/* AS path confed check. If aspath contains confed set or sequence then return
1327 * 1. */
1328int aspath_confed_check(struct aspath *aspath)
1329{
1330 struct assegment *seg;
1331
1332 if (!(aspath && aspath->segments))
1333 return 0;
1334
1335 seg = aspath->segments;
1336
1337 while (seg) {
1338 if (seg->type == AS_CONFED_SET
1339 || seg->type == AS_CONFED_SEQUENCE)
1340 return 1;
1341 seg = seg->next;
1342 }
1343 return 0;
ca87e1d3
VT
1344}
1345
1346/* Leftmost AS path segment confed check. If leftmost AS segment is of type
1347 AS_CONFED_SEQUENCE or AS_CONFED_SET then return 1. */
d62a17ae 1348int aspath_left_confed_check(struct aspath *aspath)
ca87e1d3
VT
1349{
1350
d62a17ae 1351 if (!(aspath && aspath->segments))
1352 return 0;
ca87e1d3 1353
d62a17ae 1354 if ((aspath->segments->type == AS_CONFED_SEQUENCE)
1355 || (aspath->segments->type == AS_CONFED_SET))
1356 return 1;
ca87e1d3 1357
d62a17ae 1358 return 0;
ca87e1d3
VT
1359}
1360
718e3744 1361/* Merge as1 to as2. as2 should be uninterned aspath. */
d62a17ae 1362static struct aspath *aspath_merge(struct aspath *as1, struct aspath *as2)
718e3744 1363{
d62a17ae 1364 struct assegment *last, *new;
718e3744 1365
d62a17ae 1366 if (!as1 || !as2)
1367 return NULL;
718e3744 1368
d62a17ae 1369 last = new = assegment_dup_all(as1->segments);
1370
1371 /* find the last valid segment */
1372 while (last && last->next)
1373 last = last->next;
1374
1375 last->next = as2->segments;
1376 as2->segments = new;
68e1a55b 1377 aspath_str_update(as2, false);
d62a17ae 1378 return as2;
718e3744 1379}
1380
1381/* Prepend as1 to as2. as2 should be uninterned aspath. */
d62a17ae 1382struct aspath *aspath_prepend(struct aspath *as1, struct aspath *as2)
1383{
3c510881
QY
1384 struct assegment *as1segtail;
1385 struct assegment *as2segtail;
1386 struct assegment *as2seghead;
d62a17ae 1387
1388 if (!as1 || !as2)
1389 return NULL;
1390
d62a17ae 1391 /* If as2 is empty, only need to dupe as1's chain onto as2 */
3c510881 1392 if (as2->segments == NULL) {
d62a17ae 1393 as2->segments = assegment_dup_all(as1->segments);
68e1a55b 1394 aspath_str_update(as2, false);
d62a17ae 1395 return as2;
1396 }
1397
1398 /* If as1 is empty AS, no prepending to do. */
3c510881 1399 if (as1->segments == NULL)
d62a17ae 1400 return as2;
1401
1402 /* find the tail as1's segment chain. */
3c510881
QY
1403 as1segtail = as1->segments;
1404 while (as1segtail && as1segtail->next)
1405 as1segtail = as1segtail->next;
d62a17ae 1406
1407 /* Delete any AS_CONFED_SEQUENCE segment from as2. */
3c510881
QY
1408 if (as1segtail->type == AS_SEQUENCE
1409 && as2->segments->type == AS_CONFED_SEQUENCE)
d62a17ae 1410 as2 = aspath_delete_confed_seq(as2);
1411
3c510881
QY
1412 if (!as2->segments) {
1413 as2->segments = assegment_dup_all(as1->segments);
1414 aspath_str_update(as2, false);
1415 return as2;
1416 }
1417
d62a17ae 1418 /* Compare last segment type of as1 and first segment type of as2. */
3c510881 1419 if (as1segtail->type != as2->segments->type)
d62a17ae 1420 return aspath_merge(as1, as2);
1421
3c510881 1422 if (as1segtail->type == AS_SEQUENCE) {
d62a17ae 1423 /* We have two chains of segments, as1->segments and seg2,
1424 * and we have to attach them together, merging the attaching
1425 * segments together into one.
1426 *
1427 * 1. dupe as1->segments onto head of as2
1428 * 2. merge seg2's asns onto last segment of this new chain
1429 * 3. attach chain after seg2
1430 */
1431
3c510881
QY
1432 /* save as2 head */
1433 as2seghead = as2->segments;
1434
d62a17ae 1435 /* dupe as1 onto as2's head */
3c510881 1436 as2segtail = as2->segments = assegment_dup_all(as1->segments);
d62a17ae 1437
3c510881
QY
1438 /* refind the tail of as2 */
1439 while (as2segtail && as2segtail->next)
1440 as2segtail = as2segtail->next;
d62a17ae 1441
1442 /* merge the old head, seg2, into tail, seg1 */
3c510881
QY
1443 assegment_append_asns(as2segtail, as2seghead->as,
1444 as2seghead->length);
d62a17ae 1445
3c510881
QY
1446 /*
1447 * bypass the merged seg2, and attach any chain after it
1448 * to chain descending from as2's head
d62a17ae 1449 */
3c510881 1450 as2segtail->next = as2seghead->next;
d62a17ae 1451
3c510881
QY
1452 /* as2->segments is now referenceless and useless */
1453 assegment_free(as2seghead);
d62a17ae 1454
1455 /* we've now prepended as1's segment chain to as2, merging
1456 * the inbetween AS_SEQUENCE of seg2 in the process
1457 */
68e1a55b 1458 aspath_str_update(as2, false);
d62a17ae 1459 return as2;
1460 } else {
1461 /* AS_SET merge code is needed at here. */
1462 return aspath_merge(as1, as2);
1463 }
1464 /* XXX: Ermmm, what if as1 has multiple segments?? */
1465
1466 /* Not reached */
718e3744 1467}
1468
841f7a57
DO
1469/* Iterate over AS_PATH segments and wipe all occurences of the
1470 * listed AS numbers. Hence some segments may lose some or even
1471 * all data on the way, the operation is implemented as a smarter
1472 * version of aspath_dup(), which allocates memory to hold the new
1473 * data, not the original. The new AS path is returned.
1474 */
d62a17ae 1475struct aspath *aspath_filter_exclude(struct aspath *source,
1476 struct aspath *exclude_list)
1477{
1478 struct assegment *srcseg, *exclseg, *lastseg;
1479 struct aspath *newpath;
1480
1481 newpath = aspath_new();
1482 lastseg = NULL;
1483
1484 for (srcseg = source->segments; srcseg; srcseg = srcseg->next) {
1485 unsigned i, y, newlen = 0, done = 0, skip_as;
1486 struct assegment *newseg;
1487
1488 /* Find out, how much ASns are we going to pick from this
1489 * segment.
1490 * We can't perform filtering right inline, because the size of
1491 * the new segment isn't known at the moment yet.
1492 */
1493 for (i = 0; i < srcseg->length; i++) {
1494 skip_as = 0;
1495 for (exclseg = exclude_list->segments;
1496 exclseg && !skip_as; exclseg = exclseg->next)
1497 for (y = 0; y < exclseg->length; y++)
1498 if (srcseg->as[i] == exclseg->as[y]) {
1499 skip_as = 1;
1500 // There's no sense in testing
1501 // the rest of exclusion list,
1502 // bail out.
1503 break;
1504 }
1505 if (!skip_as)
1506 newlen++;
1507 }
1508 /* newlen is now the number of ASns to copy */
1509 if (!newlen)
1510 continue;
1511
1512 /* Actual copying. Allocate memory and iterate once more,
1513 * performing filtering. */
1514 newseg = assegment_new(srcseg->type, newlen);
1515 for (i = 0; i < srcseg->length; i++) {
1516 skip_as = 0;
1517 for (exclseg = exclude_list->segments;
1518 exclseg && !skip_as; exclseg = exclseg->next)
1519 for (y = 0; y < exclseg->length; y++)
1520 if (srcseg->as[i] == exclseg->as[y]) {
1521 skip_as = 1;
1522 break;
1523 }
1524 if (skip_as)
1525 continue;
1526 newseg->as[done++] = srcseg->as[i];
1527 }
1528 /* At his point newlen must be equal to done, and both must be
1529 * positive. Append
1530 * the filtered segment to the gross result. */
1531 if (!lastseg)
1532 newpath->segments = newseg;
1533 else
1534 lastseg->next = newseg;
1535 lastseg = newseg;
1536 }
68e1a55b 1537 aspath_str_update(newpath, false);
d62a17ae 1538 /* We are happy returning even an empty AS_PATH, because the
1539 * administrator
1540 * might expect this very behaviour. There's a mean to avoid this, if
1541 * necessary,
1542 * by having a match rule against certain AS_PATH regexps in the
1543 * route-map index.
1544 */
1545 aspath_free(source);
1546 return newpath;
841f7a57
DO
1547}
1548
718e3744 1549/* Add specified AS to the leftmost of aspath. */
d62a17ae 1550static struct aspath *aspath_add_asns(struct aspath *aspath, as_t asno,
d7c0a89a 1551 uint8_t type, unsigned num)
d62a17ae 1552{
1553 struct assegment *assegment = aspath->segments;
1554 unsigned i;
1555
1556 if (assegment && assegment->type == type) {
1557 /* extend existing segment */
1558 aspath->segments =
1559 assegment_prepend_asns(aspath->segments, asno, num);
1560 } else {
1561 /* prepend with new segment */
1562 struct assegment *newsegment = assegment_new(type, num);
1563 for (i = 0; i < num; i++)
1564 newsegment->as[i] = asno;
1565
1566 /* insert potentially replacing empty segment */
1567 if (assegment && assegment->length == 0) {
1568 newsegment->next = assegment->next;
1569 assegment_free(assegment);
1570 } else
1571 newsegment->next = assegment;
1572 aspath->segments = newsegment;
bc3dd427 1573 }
718e3744 1574
68e1a55b 1575 aspath_str_update(aspath, false);
d62a17ae 1576 return aspath;
718e3744 1577}
1578
bc3dd427 1579/* Add specified AS to the leftmost of aspath num times. */
d62a17ae 1580struct aspath *aspath_add_seq_n(struct aspath *aspath, as_t asno, unsigned num)
bc3dd427 1581{
d62a17ae 1582 return aspath_add_asns(aspath, asno, AS_SEQUENCE, num);
bc3dd427
DW
1583}
1584
718e3744 1585/* Add specified AS to the leftmost of aspath. */
d62a17ae 1586struct aspath *aspath_add_seq(struct aspath *aspath, as_t asno)
718e3744 1587{
d62a17ae 1588 return aspath_add_asns(aspath, asno, AS_SEQUENCE, 1);
718e3744 1589}
1590
1591/* Compare leftmost AS value for MED check. If as1's leftmost AS and
1592 as2's leftmost AS is same return 1. */
d62a17ae 1593int aspath_cmp_left(const struct aspath *aspath1, const struct aspath *aspath2)
718e3744 1594{
d62a17ae 1595 const struct assegment *seg1;
1596 const struct assegment *seg2;
1597
1598 if (!(aspath1 && aspath2))
1599 return 0;
718e3744 1600
d62a17ae 1601 seg1 = aspath1->segments;
1602 seg2 = aspath2->segments;
718e3744 1603
d62a17ae 1604 /* If both paths are originated in this AS then we do want to compare
1605 * MED */
1606 if (!seg1 && !seg2)
1607 return 1;
718e3744 1608
d62a17ae 1609 /* find first non-confed segments for each */
9d303b37
DL
1610 while (seg1 && ((seg1->type == AS_CONFED_SEQUENCE)
1611 || (seg1->type == AS_CONFED_SET)))
d62a17ae 1612 seg1 = seg1->next;
2a3fa5d7 1613
9d303b37
DL
1614 while (seg2 && ((seg2->type == AS_CONFED_SEQUENCE)
1615 || (seg2->type == AS_CONFED_SET)))
d62a17ae 1616 seg2 = seg2->next;
718e3744 1617
d62a17ae 1618 /* Check as1's */
1619 if (!(seg1 && seg2 && (seg1->type == AS_SEQUENCE)
1620 && (seg2->type == AS_SEQUENCE)))
1621 return 0;
718e3744 1622
d62a17ae 1623 if (seg1->as[0] == seg2->as[0])
1624 return 1;
718e3744 1625
d62a17ae 1626 return 0;
718e3744 1627}
1628
0b2aa3a0
PJ
1629/* Truncate an aspath after a number of hops, and put the hops remaining
1630 * at the front of another aspath. Needed for AS4 compat.
1631 *
1632 * Returned aspath is a /new/ aspath, which should either by free'd or
1633 * interned by the caller, as desired.
1634 */
d62a17ae 1635struct aspath *aspath_reconcile_as4(struct aspath *aspath,
1636 struct aspath *as4path)
1637{
1638 struct assegment *seg, *newseg, *prevseg = NULL;
1639 struct aspath *newpath = NULL, *mergedpath;
1640 int hops, cpasns = 0;
1641
e8a3a0a0 1642 if (!aspath || !as4path)
d62a17ae 1643 return NULL;
1644
1645 seg = aspath->segments;
1646
1647 /* CONFEDs should get reconciled too.. */
1648 hops = (aspath_count_hops(aspath) + aspath_count_confeds(aspath))
1649 - aspath_count_hops(as4path);
1650
1651 if (hops < 0) {
1652 if (BGP_DEBUG(as4, AS4))
ade6974d 1653 flog_warn(
e50f7cfd 1654 EC_BGP_ASPATH_FEWER_HOPS,
ade6974d 1655 "[AS4] Fewer hops in AS_PATH than NEW_AS_PATH");
d62a17ae 1656 /* Something's gone wrong. The RFC says we should now ignore
1657 * AS4_PATH,
1658 * which is daft behaviour - it contains vital loop-detection
1659 * information which must have been removed from AS_PATH.
1660 */
1661 hops = aspath_count_hops(aspath);
1662 }
1663
1664 if (!hops) {
1665 newpath = aspath_dup(as4path);
68e1a55b 1666 aspath_str_update(newpath, false);
d62a17ae 1667 return newpath;
0b2aa3a0 1668 }
d62a17ae 1669
1670 if (BGP_DEBUG(as4, AS4))
1671 zlog_debug(
1672 "[AS4] got AS_PATH %s and AS4_PATH %s synthesizing now",
1673 aspath->str, as4path->str);
1674
1675 while (seg && hops > 0) {
1676 switch (seg->type) {
1677 case AS_SET:
1678 case AS_CONFED_SET:
1679 hops--;
1680 cpasns = seg->length;
1681 break;
1682 case AS_CONFED_SEQUENCE:
1683 /* Should never split a confed-sequence, if hop-count
1684 * suggests we must then something's gone wrong
1685 * somewhere.
1686 *
1687 * Most important goal is to preserve AS_PATHs prime
1688 * function
1689 * as loop-detector, so we fudge the numbers so that the
1690 * entire
1691 * confed-sequence is merged in.
1692 */
1693 if (hops < seg->length) {
1694 if (BGP_DEBUG(as4, AS4))
1695 zlog_debug(
1696 "[AS4] AS4PATHmangle: AS_CONFED_SEQUENCE falls"
1697 " across 2/4 ASN boundary somewhere, broken..");
1698 hops = seg->length;
1699 }
1700 /* fallthru */
1701 case AS_SEQUENCE:
1702 cpasns = MIN(seg->length, hops);
1703 hops -= seg->length;
1704 }
1705
1706 assert(cpasns <= seg->length);
1707
1708 newseg = assegment_new(seg->type, 0);
1709 newseg = assegment_append_asns(newseg, seg->as, cpasns);
1710
1711 if (!newpath) {
1712 newpath = aspath_new();
1713 newpath->segments = newseg;
1714 } else
1715 prevseg->next = newseg;
1716
1717 prevseg = newseg;
1718 seg = seg->next;
1719 }
1720
1721 /* We may be able to join some segments here, and we must
1722 * do this because... we want normalised aspaths in out hash
1723 * and we do not want to stumble in aspath_put.
1724 */
1725 mergedpath = aspath_merge(newpath, aspath_dup(as4path));
1726 aspath_free(newpath);
1727 mergedpath->segments = assegment_normalise(mergedpath->segments);
68e1a55b 1728 aspath_str_update(mergedpath, false);
d62a17ae 1729
1730 if (BGP_DEBUG(as4, AS4))
1731 zlog_debug("[AS4] result of synthesizing is %s",
1732 mergedpath->str);
1733
1734 return mergedpath;
0b2aa3a0
PJ
1735}
1736
718e3744 1737/* Compare leftmost AS value for MED check. If as1's leftmost AS and
1738 as2's leftmost AS is same return 1. (confederation as-path
1739 only). */
74df8d6d
DS
1740bool aspath_cmp_left_confed(const struct aspath *aspath1,
1741 const struct aspath *aspath2)
718e3744 1742{
d62a17ae 1743 if (!(aspath1 && aspath2))
74df8d6d 1744 return false;
718e3744 1745
d62a17ae 1746 if (!(aspath1->segments && aspath2->segments))
74df8d6d 1747 return false;
d62a17ae 1748
1749 if ((aspath1->segments->type != AS_CONFED_SEQUENCE)
1750 || (aspath2->segments->type != AS_CONFED_SEQUENCE))
74df8d6d 1751 return false;
d62a17ae 1752
1753 if (aspath1->segments->as[0] == aspath2->segments->as[0])
74df8d6d 1754 return true;
d62a17ae 1755
74df8d6d 1756 return false;
718e3744 1757}
1758
66b199b2
DS
1759/* Delete all AS_CONFED_SEQUENCE/SET segments from aspath.
1760 * RFC 5065 section 4.1.c.1
1761 *
1762 * 1) if any path segments of the AS_PATH are of the type
1763 * AS_CONFED_SEQUENCE or AS_CONFED_SET, those segments MUST be
1764 * removed from the AS_PATH attribute, leaving the sanitized
1765 * AS_PATH attribute to be operated on by steps 2, 3 or 4.
1766 */
d62a17ae 1767struct aspath *aspath_delete_confed_seq(struct aspath *aspath)
718e3744 1768{
d62a17ae 1769 struct assegment *seg, *prev, *next;
1770 char removed_confed_segment;
1771
1772 if (!(aspath && aspath->segments))
1773 return aspath;
718e3744 1774
d62a17ae 1775 seg = aspath->segments;
1776 removed_confed_segment = 0;
1777 next = NULL;
1778 prev = NULL;
718e3744 1779
d62a17ae 1780 while (seg) {
1781 next = seg->next;
66b199b2 1782
d62a17ae 1783 if (seg->type == AS_CONFED_SEQUENCE
1784 || seg->type == AS_CONFED_SET) {
1785 /* This is the first segment in the aspath */
1786 if (aspath->segments == seg)
1787 aspath->segments = seg->next;
1788 else
1789 prev->next = seg->next;
66b199b2 1790
d62a17ae 1791 assegment_free(seg);
1792 removed_confed_segment = 1;
1793 } else
1794 prev = seg;
66b199b2 1795
d62a17ae 1796 seg = next;
1797 }
66b199b2 1798
d62a17ae 1799 if (removed_confed_segment)
68e1a55b 1800 aspath_str_update(aspath, false);
66b199b2 1801
d62a17ae 1802 return aspath;
718e3744 1803}
1804
1805/* Add new AS number to the leftmost part of the aspath as
1806 AS_CONFED_SEQUENCE. */
d62a17ae 1807struct aspath *aspath_add_confed_seq(struct aspath *aspath, as_t asno)
718e3744 1808{
d62a17ae 1809 return aspath_add_asns(aspath, asno, AS_CONFED_SEQUENCE, 1);
718e3744 1810}
1811
1812/* Add new as value to as path structure. */
d62a17ae 1813static void aspath_as_add(struct aspath *as, as_t asno)
718e3744 1814{
d62a17ae 1815 struct assegment *seg = as->segments;
1816
1817 if (!seg)
1818 return;
718e3744 1819
d62a17ae 1820 /* Last segment search procedure. */
1821 while (seg->next)
1822 seg = seg->next;
93c1749c 1823
d62a17ae 1824 assegment_append_asns(seg, &asno, 1);
718e3744 1825}
1826
1827/* Add new as segment to the as path. */
d62a17ae 1828static void aspath_segment_add(struct aspath *as, int type)
718e3744 1829{
d62a17ae 1830 struct assegment *seg = as->segments;
1831 struct assegment *new = assegment_new(type, 0);
718e3744 1832
d62a17ae 1833 if (seg) {
1834 while (seg->next)
1835 seg = seg->next;
1836 seg->next = new;
1837 } else
1838 as->segments = new;
718e3744 1839}
1840
d62a17ae 1841struct aspath *aspath_empty(void)
718e3744 1842{
d62a17ae 1843 return aspath_parse(NULL, 0, 1); /* 32Bit ;-) */
718e3744 1844}
1845
d62a17ae 1846struct aspath *aspath_empty_get(void)
718e3744 1847{
d62a17ae 1848 struct aspath *aspath;
718e3744 1849
d62a17ae 1850 aspath = aspath_new();
68e1a55b 1851 aspath_make_str_count(aspath, false);
d62a17ae 1852 return aspath;
718e3744 1853}
1854
d62a17ae 1855unsigned long aspath_count(void)
718e3744 1856{
d62a17ae 1857 return ashash->count;
1858}
6b0655a2 1859
d62a17ae 1860/*
718e3744 1861 Theoretically, one as path can have:
1862
1863 One BGP packet size should be less than 4096.
1864 One BGP attribute size should be less than 4096 - BGP header size.
1865 One BGP aspath size should be less than 4096 - BGP header size -
1866 BGP mandantry attribute size.
1867*/
1868
1869/* AS path string lexical token enum. */
d62a17ae 1870enum as_token {
1871 as_token_asval,
1872 as_token_set_start,
1873 as_token_set_end,
1874 as_token_confed_seq_start,
1875 as_token_confed_seq_end,
1876 as_token_confed_set_start,
1877 as_token_confed_set_end,
1878 as_token_unknown
718e3744 1879};
1880
1881/* Return next token and point for string parse. */
d62a17ae 1882static const char *aspath_gettoken(const char *buf, enum as_token *token,
d7c0a89a 1883 unsigned long *asno)
d62a17ae 1884{
1885 const char *p = buf;
1886
1887 /* Skip seperators (space for sequences, ',' for sets). */
1888 while (isspace((int)*p) || *p == ',')
1889 p++;
1890
1891 /* Check the end of the string and type specify characters
1892 (e.g. {}()). */
1893 switch (*p) {
1894 case '\0':
1895 return NULL;
1896 case '{':
1897 *token = as_token_set_start;
1898 p++;
1899 return p;
1900 case '}':
1901 *token = as_token_set_end;
1902 p++;
1903 return p;
1904 case '(':
1905 *token = as_token_confed_seq_start;
1906 p++;
1907 return p;
1908 case ')':
1909 *token = as_token_confed_seq_end;
1910 p++;
1911 return p;
1912 case '[':
1913 *token = as_token_confed_set_start;
1914 p++;
1915 return p;
1916 case ']':
1917 *token = as_token_confed_set_end;
1918 p++;
1919 return p;
1920 }
1921
1922 /* Check actual AS value. */
1923 if (isdigit((int)*p)) {
1924 as_t asval;
1925
1926 *token = as_token_asval;
1927 asval = (*p - '0');
1928 p++;
1929
1930 while (isdigit((int)*p)) {
1931 asval *= 10;
1932 asval += (*p - '0');
1933 p++;
1934 }
1935 *asno = asval;
1936 return p;
718e3744 1937 }
718e3744 1938
d62a17ae 1939 /* There is no match then return unknown token. */
1940 *token = as_token_unknown;
b42d80dd
VJ
1941 p++;
1942 return p;
d62a17ae 1943}
1944
1945struct aspath *aspath_str2aspath(const char *str)
1946{
1947 enum as_token token = as_token_unknown;
d7c0a89a
QY
1948 unsigned short as_type;
1949 unsigned long asno = 0;
d62a17ae 1950 struct aspath *aspath;
1951 int needtype;
1952
1953 aspath = aspath_new();
1954
1955 /* We start default type as AS_SEQUENCE. */
1956 as_type = AS_SEQUENCE;
1957 needtype = 1;
1958
1959 while ((str = aspath_gettoken(str, &token, &asno)) != NULL) {
1960 switch (token) {
1961 case as_token_asval:
1962 if (needtype) {
1963 aspath_segment_add(aspath, as_type);
1964 needtype = 0;
1965 }
1966 aspath_as_add(aspath, asno);
1967 break;
1968 case as_token_set_start:
1969 as_type = AS_SET;
1970 aspath_segment_add(aspath, as_type);
1971 needtype = 0;
1972 break;
1973 case as_token_set_end:
1974 as_type = AS_SEQUENCE;
1975 needtype = 1;
1976 break;
1977 case as_token_confed_seq_start:
1978 as_type = AS_CONFED_SEQUENCE;
1979 aspath_segment_add(aspath, as_type);
1980 needtype = 0;
1981 break;
1982 case as_token_confed_seq_end:
1983 as_type = AS_SEQUENCE;
1984 needtype = 1;
1985 break;
1986 case as_token_confed_set_start:
1987 as_type = AS_CONFED_SET;
1988 aspath_segment_add(aspath, as_type);
1989 needtype = 0;
1990 break;
1991 case as_token_confed_set_end:
1992 as_type = AS_SEQUENCE;
1993 needtype = 1;
1994 break;
1995 case as_token_unknown:
1996 default:
1997 aspath_free(aspath);
1998 return NULL;
1999 }
2000 }
2001
68e1a55b 2002 aspath_make_str_count(aspath, false);
718e3744 2003
d62a17ae 2004 return aspath;
718e3744 2005}
6b0655a2 2006
718e3744 2007/* Make hash value by raw aspath data. */
d62a17ae 2008unsigned int aspath_key_make(void *p)
718e3744 2009{
d62a17ae 2010 struct aspath *aspath = (struct aspath *)p;
2011 unsigned int key = 0;
718e3744 2012
d62a17ae 2013 if (!aspath->str)
68e1a55b 2014 aspath_str_update(aspath, false);
f669f7d2 2015
d62a17ae 2016 key = jhash(aspath->str, aspath->str_len, 2334325);
718e3744 2017
d62a17ae 2018 return key;
718e3744 2019}
2020
2021/* If two aspath have same value then return 1 else return 0 */
74df8d6d 2022bool aspath_cmp(const void *arg1, const void *arg2)
d62a17ae 2023{
2024 const struct assegment *seg1 = ((const struct aspath *)arg1)->segments;
2025 const struct assegment *seg2 = ((const struct aspath *)arg2)->segments;
2026
2027 while (seg1 || seg2) {
2028 int i;
2029 if ((!seg1 && seg2) || (seg1 && !seg2))
74df8d6d 2030 return false;
d62a17ae 2031 if (seg1->type != seg2->type)
74df8d6d 2032 return false;
d62a17ae 2033 if (seg1->length != seg2->length)
74df8d6d 2034 return false;
d62a17ae 2035 for (i = 0; i < seg1->length; i++)
2036 if (seg1->as[i] != seg2->as[i])
74df8d6d 2037 return false;
d62a17ae 2038 seg1 = seg1->next;
2039 seg2 = seg2->next;
2040 }
74df8d6d 2041 return true;
718e3744 2042}
2043
2044/* AS path hash initialize. */
d62a17ae 2045void aspath_init(void)
718e3744 2046{
996c9314 2047 ashash = hash_create_size(32768, aspath_key_make, aspath_cmp,
3f65c5b1 2048 "BGP AS Path");
718e3744 2049}
8fdc32ab 2050
d62a17ae 2051void aspath_finish(void)
8fdc32ab 2052{
d62a17ae 2053 hash_clean(ashash, (void (*)(void *))aspath_free);
2054 hash_free(ashash);
2055 ashash = NULL;
2056
2057 if (snmp_stream)
2058 stream_free(snmp_stream);
8fdc32ab 2059}
6b0655a2 2060
718e3744 2061/* return and as path value */
d62a17ae 2062const char *aspath_print(struct aspath *as)
718e3744 2063{
d62a17ae 2064 return (as ? as->str : NULL);
718e3744 2065}
2066
2067/* Printing functions */
841f7a57
DO
2068/* Feed the AS_PATH to the vty; the suffix string follows it only in case
2069 * AS_PATH wasn't empty.
2070 */
d62a17ae 2071void aspath_print_vty(struct vty *vty, const char *format, struct aspath *as,
2072 const char *suffix)
718e3744 2073{
d62a17ae 2074 assert(format);
2075 vty_out(vty, format, as->str);
2076 if (as->str_len && strlen(suffix))
2077 vty_out(vty, "%s", suffix);
718e3744 2078}
2079
d62a17ae 2080static void aspath_show_all_iterator(struct hash_backet *backet,
2081 struct vty *vty)
718e3744 2082{
d62a17ae 2083 struct aspath *as;
718e3744 2084
d62a17ae 2085 as = (struct aspath *)backet->data;
718e3744 2086
d62a17ae 2087 vty_out(vty, "[%p:%u] (%ld) ", (void *)backet, backet->key, as->refcnt);
2088 vty_out(vty, "%s\n", as->str);
718e3744 2089}
2090
2091/* Print all aspath and hash information. This function is used from
716b2d8a 2092 `show [ip] bgp paths' command. */
d62a17ae 2093void aspath_print_all_vty(struct vty *vty)
718e3744 2094{
9d303b37
DL
2095 hash_iterate(ashash, (void (*)(struct hash_backet *,
2096 void *))aspath_show_all_iterator,
d62a17ae 2097 vty);
718e3744 2098}