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