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