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