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