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