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