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