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