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