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