]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_aspath.c
Merge pull request #6706 from donaldsharp/router_id_bullshit
[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 if (seg) {
914 /*
915 * Hey, what do we do when we have > STREAM_WRITABLE(s) here?
916 * At the moment, we would write out a partial aspath, and our
917 * peer
918 * will complain and drop the session :-/
919 *
920 * The general assumption here is that many things tested will
921 * never happen. And, in real live, up to now, they have not.
922 */
923 while (seg && (ASSEGMENT_LEN(seg, use32bit)
924 <= STREAM_WRITEABLE(s))) {
925 struct assegment *next = seg->next;
926 int written = 0;
927 int asns_packed = 0;
928 size_t lenp;
929
930 /* Overlength segments have to be split up */
931 while ((seg->length - written) > AS_SEGMENT_MAX) {
932 assegment_header_put(s, seg->type,
933 AS_SEGMENT_MAX);
934 assegment_data_put(s, (seg->as + written), AS_SEGMENT_MAX,
935 use32bit);
936 written += AS_SEGMENT_MAX;
937 bytes += ASSEGMENT_SIZE(AS_SEGMENT_MAX,
938 use32bit);
939 }
940
941 /* write the final segment, probably is also the first
942 */
943 lenp = assegment_header_put(s, seg->type,
944 seg->length - written);
945 assegment_data_put(s, (seg->as + written),
946 seg->length - written, use32bit);
947
948 /* Sequence-type segments can be 'packed' together
949 * Case of a segment which was overlength and split up
950 * will be missed here, but that doesn't matter.
951 */
952 while (next && ASSEGMENTS_PACKABLE(seg, next)) {
953 /* NB: We should never normally get here given
954 * we
955 * normalise aspath data when parse them.
956 * However, better
957 * safe than sorry. We potentially could call
958 * assegment_normalise here instead, but it's
959 * cheaper and
960 * easier to do it on the fly here rather than
961 * go through
962 * the segment list twice every time we write
963 * out
964 * aspath's.
965 */
966
967 /* Next segment's data can fit in this one */
968 assegment_data_put(s, next->as, next->length,
969 use32bit);
970
971 /* update the length of the segment header */
972 stream_putc_at(s, lenp,
973 seg->length - written
974 + next->length);
975 asns_packed += next->length;
976
977 next = next->next;
978 }
979
980 bytes += ASSEGMENT_SIZE(
981 seg->length - written + asns_packed, use32bit);
982 seg = next;
983 }
984 }
985 return bytes;
986 }
987
988 /* This is for SNMP BGP4PATHATTRASPATHSEGMENT
989 * We have no way to manage the storage, so we use a static stream
990 * wrapper around aspath_put.
991 */
992 uint8_t *aspath_snmp_pathseg(struct aspath *as, size_t *varlen)
993 {
994 #define SNMP_PATHSEG_MAX 1024
995
996 if (!snmp_stream)
997 snmp_stream = stream_new(SNMP_PATHSEG_MAX);
998 else
999 stream_reset(snmp_stream);
1000
1001 if (!as) {
1002 *varlen = 0;
1003 return NULL;
1004 }
1005 aspath_put(snmp_stream, as, 0); /* use 16 bit for now here */
1006
1007 *varlen = stream_get_endp(snmp_stream);
1008 return stream_pnt(snmp_stream);
1009 }
1010
1011 #define min(A,B) ((A) < (B) ? (A) : (B))
1012
1013 static struct assegment *aspath_aggregate_as_set_add(struct aspath *aspath,
1014 struct assegment *asset,
1015 as_t as)
1016 {
1017 int i;
1018
1019 /* If this is first AS set member, create new as-set segment. */
1020 if (asset == NULL) {
1021 asset = assegment_new(AS_SET, 1);
1022 if (!aspath->segments)
1023 aspath->segments = asset;
1024 else {
1025 struct assegment *seg = aspath->segments;
1026 while (seg->next)
1027 seg = seg->next;
1028 seg->next = asset;
1029 }
1030 asset->type = AS_SET;
1031 asset->length = 1;
1032 asset->as[0] = as;
1033 } else {
1034 /* Check this AS value already exists or not. */
1035 for (i = 0; i < asset->length; i++)
1036 if (asset->as[i] == as)
1037 return asset;
1038
1039 asset->length++;
1040 asset->as = XREALLOC(MTYPE_AS_SEG_DATA, asset->as,
1041 asset->length * AS_VALUE_SIZE);
1042 asset->as[asset->length - 1] = as;
1043 }
1044
1045
1046 return asset;
1047 }
1048
1049 /* Modify as1 using as2 for aggregation. */
1050 struct aspath *aspath_aggregate(struct aspath *as1, struct aspath *as2)
1051 {
1052 int i;
1053 int minlen = 0;
1054 int match = 0;
1055 int from;
1056 struct assegment *seg1 = as1->segments;
1057 struct assegment *seg2 = as2->segments;
1058 struct aspath *aspath = NULL;
1059 struct assegment *asset = NULL;
1060 struct assegment *prevseg = NULL;
1061
1062 /* First of all check common leading sequence. */
1063 while (seg1 && seg2) {
1064 /* Check segment type. */
1065 if (seg1->type != seg2->type)
1066 break;
1067
1068 /* Minimum segment length. */
1069 minlen = min(seg1->length, seg2->length);
1070
1071 for (match = 0; match < minlen; match++)
1072 if (seg1->as[match] != seg2->as[match])
1073 break;
1074
1075 if (match) {
1076 struct assegment *seg = assegment_new(seg1->type, 0);
1077
1078 seg = assegment_append_asns(seg, seg1->as, match);
1079
1080 if (!aspath) {
1081 aspath = aspath_new();
1082 aspath->segments = seg;
1083 } else
1084 prevseg->next = seg;
1085
1086 prevseg = seg;
1087 }
1088
1089 if (match != minlen || match != seg1->length
1090 || seg1->length != seg2->length)
1091 break;
1092 /* We are moving on to the next segment to reset match */
1093 else
1094 match = 0;
1095
1096 seg1 = seg1->next;
1097 seg2 = seg2->next;
1098 }
1099
1100 if (!aspath)
1101 aspath = aspath_new();
1102
1103 /* Make as-set using rest of all information. */
1104 from = match;
1105 while (seg1) {
1106 for (i = from; i < seg1->length; i++)
1107 asset = aspath_aggregate_as_set_add(aspath, asset,
1108 seg1->as[i]);
1109
1110 from = 0;
1111 seg1 = seg1->next;
1112 }
1113
1114 from = match;
1115 while (seg2) {
1116 for (i = from; i < seg2->length; i++)
1117 asset = aspath_aggregate_as_set_add(aspath, asset,
1118 seg2->as[i]);
1119
1120 from = 0;
1121 seg2 = seg2->next;
1122 }
1123
1124 assegment_normalise(aspath->segments);
1125 aspath_str_update(aspath, false);
1126 return aspath;
1127 }
1128
1129 /* When a BGP router receives an UPDATE with an MP_REACH_NLRI
1130 attribute, check the leftmost AS number in the AS_PATH attribute is
1131 or not the peer's AS number. */
1132 bool aspath_firstas_check(struct aspath *aspath, as_t asno)
1133 {
1134 if ((aspath == NULL) || (aspath->segments == NULL))
1135 return false;
1136
1137 if (aspath->segments && (aspath->segments->type == AS_SEQUENCE)
1138 && (aspath->segments->as[0] == asno))
1139 return true;
1140
1141 return false;
1142 }
1143
1144 unsigned int aspath_get_first_as(struct aspath *aspath)
1145 {
1146 if (aspath == NULL || aspath->segments == NULL)
1147 return 0;
1148
1149 return aspath->segments->as[0];
1150 }
1151
1152 unsigned int aspath_get_last_as(struct aspath *aspath)
1153 {
1154 int i;
1155 unsigned int last_as = 0;
1156 const struct assegment *seg;
1157
1158 if (aspath == NULL || aspath->segments == NULL)
1159 return last_as;
1160
1161 seg = aspath->segments;
1162
1163 while (seg) {
1164 if (seg->type == AS_SEQUENCE || seg->type == AS_CONFED_SEQUENCE)
1165 for (i = 0; i < seg->length; i++)
1166 last_as = seg->as[i];
1167 seg = seg->next;
1168 }
1169
1170 return last_as;
1171 }
1172
1173 /* AS path loop check. If aspath contains asno then return >= 1. */
1174 int aspath_loop_check(struct aspath *aspath, as_t asno)
1175 {
1176 struct assegment *seg;
1177 int count = 0;
1178
1179 if ((aspath == NULL) || (aspath->segments == NULL))
1180 return 0;
1181
1182 seg = aspath->segments;
1183
1184 while (seg) {
1185 int i;
1186
1187 for (i = 0; i < seg->length; i++)
1188 if (seg->as[i] == asno)
1189 count++;
1190
1191 seg = seg->next;
1192 }
1193 return count;
1194 }
1195
1196 /* When all of AS path is private AS return 1. */
1197 bool aspath_private_as_check(struct aspath *aspath)
1198 {
1199 struct assegment *seg;
1200
1201 if (!(aspath && aspath->segments))
1202 return false;
1203
1204 seg = aspath->segments;
1205
1206 while (seg) {
1207 int i;
1208
1209 for (i = 0; i < seg->length; i++) {
1210 if (!BGP_AS_IS_PRIVATE(seg->as[i]))
1211 return false;
1212 }
1213 seg = seg->next;
1214 }
1215 return true;
1216 }
1217
1218 /* Return True if the entire ASPATH consist of the specified ASN */
1219 bool aspath_single_asn_check(struct aspath *aspath, as_t asn)
1220 {
1221 struct assegment *seg;
1222
1223 if (!(aspath && aspath->segments))
1224 return false;
1225
1226 seg = aspath->segments;
1227
1228 while (seg) {
1229 int i;
1230
1231 for (i = 0; i < seg->length; i++) {
1232 if (seg->as[i] != asn)
1233 return false;
1234 }
1235 seg = seg->next;
1236 }
1237 return true;
1238 }
1239
1240 /* Replace all instances of the target ASN with our own ASN */
1241 struct aspath *aspath_replace_specific_asn(struct aspath *aspath,
1242 as_t target_asn, as_t our_asn)
1243 {
1244 struct aspath *new;
1245 struct assegment *seg;
1246
1247 new = aspath_dup(aspath);
1248 seg = new->segments;
1249
1250 while (seg) {
1251 int i;
1252
1253 for (i = 0; i < seg->length; i++) {
1254 if (seg->as[i] == target_asn)
1255 seg->as[i] = our_asn;
1256 }
1257 seg = seg->next;
1258 }
1259
1260 aspath_str_update(new, false);
1261 return new;
1262 }
1263
1264 /* Replace all private ASNs with our own ASN */
1265 struct aspath *aspath_replace_private_asns(struct aspath *aspath, as_t asn,
1266 as_t peer_asn)
1267 {
1268 struct aspath *new;
1269 struct assegment *seg;
1270
1271 new = aspath_dup(aspath);
1272 seg = new->segments;
1273
1274 while (seg) {
1275 int i;
1276
1277 for (i = 0; i < seg->length; i++) {
1278 /* Don't replace if public ASN or peer's ASN */
1279 if (BGP_AS_IS_PRIVATE(seg->as[i])
1280 && (seg->as[i] != peer_asn))
1281 seg->as[i] = asn;
1282 }
1283 seg = seg->next;
1284 }
1285
1286 aspath_str_update(new, false);
1287 return new;
1288 }
1289
1290 /* Remove all private ASNs */
1291 struct aspath *aspath_remove_private_asns(struct aspath *aspath, as_t peer_asn)
1292 {
1293 struct aspath *new;
1294 struct assegment *seg;
1295 struct assegment *new_seg;
1296 struct assegment *last_new_seg;
1297 int i;
1298 int j;
1299 int public = 0;
1300
1301 new = XCALLOC(MTYPE_AS_PATH, sizeof(struct aspath));
1302
1303 new->json = NULL;
1304 new_seg = NULL;
1305 last_new_seg = NULL;
1306 seg = aspath->segments;
1307 while (seg) {
1308 public
1309 = 0;
1310 for (i = 0; i < seg->length; i++) {
1311 // ASN is public
1312 if (!BGP_AS_IS_PRIVATE(seg->as[i])) {
1313 public
1314 ++;
1315 }
1316 }
1317
1318 // The entire segment is public so copy it
1319 if (public == seg->length)
1320 new_seg = assegment_dup(seg);
1321
1322 // The segment is a mix of public and private ASNs. Copy as many
1323 // spots as
1324 // there are public ASNs then come back and fill in only the
1325 // public ASNs.
1326 else {
1327 new_seg = assegment_new(seg->type, public);
1328 j = 0;
1329 for (i = 0; i < seg->length; i++) {
1330 // keep ASN if public or matches peer's ASN
1331 if (!BGP_AS_IS_PRIVATE(seg->as[i])
1332 || (seg->as[i] == peer_asn)) {
1333 new_seg->as[j] = seg->as[i];
1334 j++;
1335 }
1336 }
1337 }
1338
1339 // This is the first segment so set the aspath segments pointer
1340 // to this one
1341 if (!last_new_seg)
1342 new->segments = new_seg;
1343 else
1344 last_new_seg->next = new_seg;
1345
1346 last_new_seg = new_seg;
1347 seg = seg->next;
1348 }
1349
1350 aspath_str_update(new, false);
1351 return new;
1352 }
1353
1354 /* AS path confed check. If aspath contains confed set or sequence then return
1355 * 1. */
1356 bool aspath_confed_check(struct aspath *aspath)
1357 {
1358 struct assegment *seg;
1359
1360 if (!(aspath && aspath->segments))
1361 return false;
1362
1363 seg = aspath->segments;
1364
1365 while (seg) {
1366 if (seg->type == AS_CONFED_SET
1367 || seg->type == AS_CONFED_SEQUENCE)
1368 return true;
1369 seg = seg->next;
1370 }
1371 return false;
1372 }
1373
1374 /* Leftmost AS path segment confed check. If leftmost AS segment is of type
1375 AS_CONFED_SEQUENCE or AS_CONFED_SET then return 1. */
1376 bool aspath_left_confed_check(struct aspath *aspath)
1377 {
1378
1379 if (!(aspath && aspath->segments))
1380 return false;
1381
1382 if ((aspath->segments->type == AS_CONFED_SEQUENCE)
1383 || (aspath->segments->type == AS_CONFED_SET))
1384 return true;
1385
1386 return false;
1387 }
1388
1389 /* Merge as1 to as2. as2 should be uninterned aspath. */
1390 static struct aspath *aspath_merge(struct aspath *as1, struct aspath *as2)
1391 {
1392 struct assegment *last, *new;
1393
1394 if (!as1 || !as2)
1395 return NULL;
1396
1397 last = new = assegment_dup_all(as1->segments);
1398
1399 /* find the last valid segment */
1400 while (last && last->next)
1401 last = last->next;
1402
1403 if (last)
1404 last->next = as2->segments;
1405 as2->segments = new;
1406 aspath_str_update(as2, false);
1407 return as2;
1408 }
1409
1410 /* Prepend as1 to as2. as2 should be uninterned aspath. */
1411 struct aspath *aspath_prepend(struct aspath *as1, struct aspath *as2)
1412 {
1413 struct assegment *as1segtail;
1414 struct assegment *as2segtail;
1415 struct assegment *as2seghead;
1416
1417 if (!as1 || !as2)
1418 return NULL;
1419
1420 /* If as2 is empty, only need to dupe as1's chain onto as2 */
1421 if (as2->segments == NULL) {
1422 as2->segments = assegment_dup_all(as1->segments);
1423 aspath_str_update(as2, false);
1424 return as2;
1425 }
1426
1427 /* If as1 is empty AS, no prepending to do. */
1428 if (as1->segments == NULL)
1429 return as2;
1430
1431 /* find the tail as1's segment chain. */
1432 as1segtail = as1->segments;
1433 while (as1segtail && as1segtail->next)
1434 as1segtail = as1segtail->next;
1435
1436 /* Delete any AS_CONFED_SEQUENCE segment from as2. */
1437 if (as1segtail->type == AS_SEQUENCE
1438 && as2->segments->type == AS_CONFED_SEQUENCE)
1439 as2 = aspath_delete_confed_seq(as2);
1440
1441 if (!as2->segments) {
1442 as2->segments = assegment_dup_all(as1->segments);
1443 aspath_str_update(as2, false);
1444 return as2;
1445 }
1446
1447 /* Compare last segment type of as1 and first segment type of as2. */
1448 if (as1segtail->type != as2->segments->type)
1449 return aspath_merge(as1, as2);
1450
1451 if (as1segtail->type == AS_SEQUENCE) {
1452 /* We have two chains of segments, as1->segments and seg2,
1453 * and we have to attach them together, merging the attaching
1454 * segments together into one.
1455 *
1456 * 1. dupe as1->segments onto head of as2
1457 * 2. merge seg2's asns onto last segment of this new chain
1458 * 3. attach chain after seg2
1459 */
1460
1461 /* save as2 head */
1462 as2seghead = as2->segments;
1463
1464 /* dupe as1 onto as2's head */
1465 as2segtail = as2->segments = assegment_dup_all(as1->segments);
1466
1467 /* refind the tail of as2 */
1468 while (as2segtail && as2segtail->next)
1469 as2segtail = as2segtail->next;
1470
1471 /* merge the old head, seg2, into tail, seg1 */
1472 assegment_append_asns(as2segtail, as2seghead->as,
1473 as2seghead->length);
1474
1475 /*
1476 * bypass the merged seg2, and attach any chain after it
1477 * to chain descending from as2's head
1478 */
1479 if (as2segtail)
1480 as2segtail->next = as2seghead->next;
1481
1482 /* as2->segments is now referenceless and useless */
1483 assegment_free(as2seghead);
1484
1485 /* we've now prepended as1's segment chain to as2, merging
1486 * the inbetween AS_SEQUENCE of seg2 in the process
1487 */
1488 aspath_str_update(as2, false);
1489 return as2;
1490 } else {
1491 /* AS_SET merge code is needed at here. */
1492 return aspath_merge(as1, as2);
1493 }
1494 /* XXX: Ermmm, what if as1 has multiple segments?? */
1495
1496 /* Not reached */
1497 }
1498
1499 /* Iterate over AS_PATH segments and wipe all occurrences of the
1500 * listed AS numbers. Hence some segments may lose some or even
1501 * all data on the way, the operation is implemented as a smarter
1502 * version of aspath_dup(), which allocates memory to hold the new
1503 * data, not the original. The new AS path is returned.
1504 */
1505 struct aspath *aspath_filter_exclude(struct aspath *source,
1506 struct aspath *exclude_list)
1507 {
1508 struct assegment *srcseg, *exclseg, *lastseg;
1509 struct aspath *newpath;
1510
1511 newpath = aspath_new();
1512 lastseg = NULL;
1513
1514 for (srcseg = source->segments; srcseg; srcseg = srcseg->next) {
1515 unsigned i, y, newlen = 0, done = 0, skip_as;
1516 struct assegment *newseg;
1517
1518 /* Find out, how much ASns are we going to pick from this
1519 * segment.
1520 * We can't perform filtering right inline, because the size of
1521 * the new segment isn't known at the moment yet.
1522 */
1523 for (i = 0; i < srcseg->length; i++) {
1524 skip_as = 0;
1525 for (exclseg = exclude_list->segments;
1526 exclseg && !skip_as; exclseg = exclseg->next)
1527 for (y = 0; y < exclseg->length; y++)
1528 if (srcseg->as[i] == exclseg->as[y]) {
1529 skip_as = 1;
1530 // There's no sense in testing
1531 // the rest of exclusion list,
1532 // bail out.
1533 break;
1534 }
1535 if (!skip_as)
1536 newlen++;
1537 }
1538 /* newlen is now the number of ASns to copy */
1539 if (!newlen)
1540 continue;
1541
1542 /* Actual copying. Allocate memory and iterate once more,
1543 * performing filtering. */
1544 newseg = assegment_new(srcseg->type, newlen);
1545 for (i = 0; i < srcseg->length; i++) {
1546 skip_as = 0;
1547 for (exclseg = exclude_list->segments;
1548 exclseg && !skip_as; exclseg = exclseg->next)
1549 for (y = 0; y < exclseg->length; y++)
1550 if (srcseg->as[i] == exclseg->as[y]) {
1551 skip_as = 1;
1552 break;
1553 }
1554 if (skip_as)
1555 continue;
1556 newseg->as[done++] = srcseg->as[i];
1557 }
1558 /* At his point newlen must be equal to done, and both must be
1559 * positive. Append
1560 * the filtered segment to the gross result. */
1561 if (!lastseg)
1562 newpath->segments = newseg;
1563 else
1564 lastseg->next = newseg;
1565 lastseg = newseg;
1566 }
1567 aspath_str_update(newpath, false);
1568 /* We are happy returning even an empty AS_PATH, because the
1569 * administrator
1570 * might expect this very behaviour. There's a mean to avoid this, if
1571 * necessary,
1572 * by having a match rule against certain AS_PATH regexps in the
1573 * route-map index.
1574 */
1575 aspath_free(source);
1576 return newpath;
1577 }
1578
1579 /* Add specified AS to the leftmost of aspath. */
1580 static struct aspath *aspath_add_asns(struct aspath *aspath, as_t asno,
1581 uint8_t type, unsigned num)
1582 {
1583 struct assegment *assegment = aspath->segments;
1584 unsigned i;
1585
1586 if (assegment && assegment->type == type) {
1587 /* extend existing segment */
1588 aspath->segments =
1589 assegment_prepend_asns(aspath->segments, asno, num);
1590 } else {
1591 /* prepend with new segment */
1592 struct assegment *newsegment = assegment_new(type, num);
1593 for (i = 0; i < num; i++)
1594 newsegment->as[i] = asno;
1595
1596 /* insert potentially replacing empty segment */
1597 if (assegment && assegment->length == 0) {
1598 newsegment->next = assegment->next;
1599 assegment_free(assegment);
1600 } else
1601 newsegment->next = assegment;
1602 aspath->segments = newsegment;
1603 }
1604
1605 aspath_str_update(aspath, false);
1606 return aspath;
1607 }
1608
1609 /* Add specified AS to the leftmost of aspath num times. */
1610 struct aspath *aspath_add_seq_n(struct aspath *aspath, as_t asno, unsigned num)
1611 {
1612 return aspath_add_asns(aspath, asno, AS_SEQUENCE, num);
1613 }
1614
1615 /* Add specified AS to the leftmost of aspath. */
1616 struct aspath *aspath_add_seq(struct aspath *aspath, as_t asno)
1617 {
1618 return aspath_add_asns(aspath, asno, AS_SEQUENCE, 1);
1619 }
1620
1621 /* Compare leftmost AS value for MED check. If as1's leftmost AS and
1622 as2's leftmost AS is same return 1. */
1623 bool aspath_cmp_left(const struct aspath *aspath1, const struct aspath *aspath2)
1624 {
1625 const struct assegment *seg1;
1626 const struct assegment *seg2;
1627
1628 if (!(aspath1 && aspath2))
1629 return false;
1630
1631 seg1 = aspath1->segments;
1632 seg2 = aspath2->segments;
1633
1634 /* If both paths are originated in this AS then we do want to compare
1635 * MED */
1636 if (!seg1 && !seg2)
1637 return true;
1638
1639 /* find first non-confed segments for each */
1640 while (seg1 && ((seg1->type == AS_CONFED_SEQUENCE)
1641 || (seg1->type == AS_CONFED_SET)))
1642 seg1 = seg1->next;
1643
1644 while (seg2 && ((seg2->type == AS_CONFED_SEQUENCE)
1645 || (seg2->type == AS_CONFED_SET)))
1646 seg2 = seg2->next;
1647
1648 /* Check as1's */
1649 if (!(seg1 && seg2 && (seg1->type == AS_SEQUENCE)
1650 && (seg2->type == AS_SEQUENCE)))
1651 return false;
1652
1653 if (seg1->as[0] == seg2->as[0])
1654 return true;
1655
1656 return false;
1657 }
1658
1659 /* Truncate an aspath after a number of hops, and put the hops remaining
1660 * at the front of another aspath. Needed for AS4 compat.
1661 *
1662 * Returned aspath is a /new/ aspath, which should either by free'd or
1663 * interned by the caller, as desired.
1664 */
1665 struct aspath *aspath_reconcile_as4(struct aspath *aspath,
1666 struct aspath *as4path)
1667 {
1668 struct assegment *seg, *newseg, *prevseg = NULL;
1669 struct aspath *newpath = NULL, *mergedpath;
1670 int hops, cpasns = 0;
1671
1672 if (!aspath || !as4path)
1673 return NULL;
1674
1675 seg = aspath->segments;
1676
1677 /* CONFEDs should get reconciled too.. */
1678 hops = (aspath_count_hops(aspath) + aspath_count_confeds(aspath))
1679 - aspath_count_hops(as4path);
1680
1681 if (hops < 0) {
1682 if (BGP_DEBUG(as4, AS4))
1683 flog_warn(
1684 EC_BGP_ASPATH_FEWER_HOPS,
1685 "[AS4] Fewer hops in AS_PATH than NEW_AS_PATH");
1686 /* Something's gone wrong. The RFC says we should now ignore
1687 * AS4_PATH,
1688 * which is daft behaviour - it contains vital loop-detection
1689 * information which must have been removed from AS_PATH.
1690 */
1691 hops = aspath_count_hops(aspath);
1692 }
1693
1694 if (!hops) {
1695 newpath = aspath_dup(as4path);
1696 aspath_str_update(newpath, false);
1697 return newpath;
1698 }
1699
1700 if (BGP_DEBUG(as4, AS4))
1701 zlog_debug(
1702 "[AS4] got AS_PATH %s and AS4_PATH %s synthesizing now",
1703 aspath->str, as4path->str);
1704
1705 while (seg && hops > 0) {
1706 switch (seg->type) {
1707 case AS_SET:
1708 case AS_CONFED_SET:
1709 hops--;
1710 cpasns = seg->length;
1711 break;
1712 case AS_CONFED_SEQUENCE:
1713 /* Should never split a confed-sequence, if hop-count
1714 * suggests we must then something's gone wrong
1715 * somewhere.
1716 *
1717 * Most important goal is to preserve AS_PATHs prime
1718 * function
1719 * as loop-detector, so we fudge the numbers so that the
1720 * entire
1721 * confed-sequence is merged in.
1722 */
1723 if (hops < seg->length) {
1724 if (BGP_DEBUG(as4, AS4))
1725 zlog_debug(
1726 "[AS4] AS4PATHmangle: AS_CONFED_SEQUENCE falls across 2/4 ASN boundary somewhere, broken..");
1727 hops = seg->length;
1728 }
1729 /* fallthru */
1730 case AS_SEQUENCE:
1731 cpasns = MIN(seg->length, hops);
1732 hops -= seg->length;
1733 }
1734
1735 assert(cpasns <= seg->length);
1736
1737 newseg = assegment_new(seg->type, 0);
1738 newseg = assegment_append_asns(newseg, seg->as, cpasns);
1739
1740 if (!newpath) {
1741 newpath = aspath_new();
1742 newpath->segments = newseg;
1743 } else
1744 prevseg->next = newseg;
1745
1746 prevseg = newseg;
1747 seg = seg->next;
1748 }
1749
1750 /* We may be able to join some segments here, and we must
1751 * do this because... we want normalised aspaths in out hash
1752 * and we do not want to stumble in aspath_put.
1753 */
1754 mergedpath = aspath_merge(newpath, aspath_dup(as4path));
1755 aspath_free(newpath);
1756 mergedpath->segments = assegment_normalise(mergedpath->segments);
1757 aspath_str_update(mergedpath, false);
1758
1759 if (BGP_DEBUG(as4, AS4))
1760 zlog_debug("[AS4] result of synthesizing is %s",
1761 mergedpath->str);
1762
1763 return mergedpath;
1764 }
1765
1766 /* Compare leftmost AS value for MED check. If as1's leftmost AS and
1767 as2's leftmost AS is same return 1. (confederation as-path
1768 only). */
1769 bool aspath_cmp_left_confed(const struct aspath *aspath1,
1770 const struct aspath *aspath2)
1771 {
1772 if (!(aspath1 && aspath2))
1773 return false;
1774
1775 if (!(aspath1->segments && aspath2->segments))
1776 return false;
1777
1778 if ((aspath1->segments->type != AS_CONFED_SEQUENCE)
1779 || (aspath2->segments->type != AS_CONFED_SEQUENCE))
1780 return false;
1781
1782 if (aspath1->segments->as[0] == aspath2->segments->as[0])
1783 return true;
1784
1785 return false;
1786 }
1787
1788 /* Delete all AS_CONFED_SEQUENCE/SET segments from aspath.
1789 * RFC 5065 section 4.1.c.1
1790 *
1791 * 1) if any path segments of the AS_PATH are of the type
1792 * AS_CONFED_SEQUENCE or AS_CONFED_SET, those segments MUST be
1793 * removed from the AS_PATH attribute, leaving the sanitized
1794 * AS_PATH attribute to be operated on by steps 2, 3 or 4.
1795 */
1796 struct aspath *aspath_delete_confed_seq(struct aspath *aspath)
1797 {
1798 struct assegment *seg, *prev, *next;
1799 char removed_confed_segment;
1800
1801 if (!(aspath && aspath->segments))
1802 return aspath;
1803
1804 seg = aspath->segments;
1805 removed_confed_segment = 0;
1806 next = NULL;
1807 prev = NULL;
1808
1809 while (seg) {
1810 next = seg->next;
1811
1812 if (seg->type == AS_CONFED_SEQUENCE
1813 || seg->type == AS_CONFED_SET) {
1814 /* This is the first segment in the aspath */
1815 if (aspath->segments == seg)
1816 aspath->segments = seg->next;
1817 else
1818 prev->next = seg->next;
1819
1820 assegment_free(seg);
1821 removed_confed_segment = 1;
1822 } else
1823 prev = seg;
1824
1825 seg = next;
1826 }
1827
1828 if (removed_confed_segment)
1829 aspath_str_update(aspath, false);
1830
1831 return aspath;
1832 }
1833
1834 /* Add new AS number to the leftmost part of the aspath as
1835 AS_CONFED_SEQUENCE. */
1836 struct aspath *aspath_add_confed_seq(struct aspath *aspath, as_t asno)
1837 {
1838 return aspath_add_asns(aspath, asno, AS_CONFED_SEQUENCE, 1);
1839 }
1840
1841 /* Add new as value to as path structure. */
1842 static void aspath_as_add(struct aspath *as, as_t asno)
1843 {
1844 struct assegment *seg = as->segments;
1845
1846 if (!seg)
1847 return;
1848
1849 /* Last segment search procedure. */
1850 while (seg->next)
1851 seg = seg->next;
1852
1853 assegment_append_asns(seg, &asno, 1);
1854 }
1855
1856 /* Add new as segment to the as path. */
1857 static void aspath_segment_add(struct aspath *as, int type)
1858 {
1859 struct assegment *seg = as->segments;
1860 struct assegment *new = assegment_new(type, 0);
1861
1862 if (seg) {
1863 while (seg->next)
1864 seg = seg->next;
1865 seg->next = new;
1866 } else
1867 as->segments = new;
1868 }
1869
1870 struct aspath *aspath_empty(void)
1871 {
1872 return aspath_parse(NULL, 0, 1); /* 32Bit ;-) */
1873 }
1874
1875 struct aspath *aspath_empty_get(void)
1876 {
1877 struct aspath *aspath;
1878
1879 aspath = aspath_new();
1880 aspath_make_str_count(aspath, false);
1881 return aspath;
1882 }
1883
1884 unsigned long aspath_count(void)
1885 {
1886 return ashash->count;
1887 }
1888
1889 /*
1890 Theoretically, one as path can have:
1891
1892 One BGP packet size should be less than 4096.
1893 One BGP attribute size should be less than 4096 - BGP header size.
1894 One BGP aspath size should be less than 4096 - BGP header size -
1895 BGP mandantry attribute size.
1896 */
1897
1898 /* AS path string lexical token enum. */
1899 enum as_token {
1900 as_token_asval,
1901 as_token_set_start,
1902 as_token_set_end,
1903 as_token_confed_seq_start,
1904 as_token_confed_seq_end,
1905 as_token_confed_set_start,
1906 as_token_confed_set_end,
1907 as_token_unknown
1908 };
1909
1910 /* Return next token and point for string parse. */
1911 static const char *aspath_gettoken(const char *buf, enum as_token *token,
1912 unsigned long *asno)
1913 {
1914 const char *p = buf;
1915
1916 /* Skip seperators (space for sequences, ',' for sets). */
1917 while (isspace((unsigned char)*p) || *p == ',')
1918 p++;
1919
1920 /* Check the end of the string and type specify characters
1921 (e.g. {}()). */
1922 switch (*p) {
1923 case '\0':
1924 return NULL;
1925 case '{':
1926 *token = as_token_set_start;
1927 p++;
1928 return p;
1929 case '}':
1930 *token = as_token_set_end;
1931 p++;
1932 return p;
1933 case '(':
1934 *token = as_token_confed_seq_start;
1935 p++;
1936 return p;
1937 case ')':
1938 *token = as_token_confed_seq_end;
1939 p++;
1940 return p;
1941 case '[':
1942 *token = as_token_confed_set_start;
1943 p++;
1944 return p;
1945 case ']':
1946 *token = as_token_confed_set_end;
1947 p++;
1948 return p;
1949 }
1950
1951 /* Check actual AS value. */
1952 if (isdigit((unsigned char)*p)) {
1953 as_t asval;
1954
1955 *token = as_token_asval;
1956 asval = (*p - '0');
1957 p++;
1958
1959 while (isdigit((unsigned char)*p)) {
1960 asval *= 10;
1961 asval += (*p - '0');
1962 p++;
1963 }
1964 *asno = asval;
1965 return p;
1966 }
1967
1968 /* There is no match then return unknown token. */
1969 *token = as_token_unknown;
1970 p++;
1971 return p;
1972 }
1973
1974 struct aspath *aspath_str2aspath(const char *str)
1975 {
1976 enum as_token token = as_token_unknown;
1977 unsigned short as_type;
1978 unsigned long asno = 0;
1979 struct aspath *aspath;
1980 int needtype;
1981
1982 aspath = aspath_new();
1983
1984 /* We start default type as AS_SEQUENCE. */
1985 as_type = AS_SEQUENCE;
1986 needtype = 1;
1987
1988 while ((str = aspath_gettoken(str, &token, &asno)) != NULL) {
1989 switch (token) {
1990 case as_token_asval:
1991 if (needtype) {
1992 aspath_segment_add(aspath, as_type);
1993 needtype = 0;
1994 }
1995 aspath_as_add(aspath, asno);
1996 break;
1997 case as_token_set_start:
1998 as_type = AS_SET;
1999 aspath_segment_add(aspath, as_type);
2000 needtype = 0;
2001 break;
2002 case as_token_set_end:
2003 as_type = AS_SEQUENCE;
2004 needtype = 1;
2005 break;
2006 case as_token_confed_seq_start:
2007 as_type = AS_CONFED_SEQUENCE;
2008 aspath_segment_add(aspath, as_type);
2009 needtype = 0;
2010 break;
2011 case as_token_confed_seq_end:
2012 as_type = AS_SEQUENCE;
2013 needtype = 1;
2014 break;
2015 case as_token_confed_set_start:
2016 as_type = AS_CONFED_SET;
2017 aspath_segment_add(aspath, as_type);
2018 needtype = 0;
2019 break;
2020 case as_token_confed_set_end:
2021 as_type = AS_SEQUENCE;
2022 needtype = 1;
2023 break;
2024 case as_token_unknown:
2025 default:
2026 aspath_free(aspath);
2027 return NULL;
2028 }
2029 }
2030
2031 aspath_make_str_count(aspath, false);
2032
2033 return aspath;
2034 }
2035
2036 /* Make hash value by raw aspath data. */
2037 unsigned int aspath_key_make(const void *p)
2038 {
2039 const struct aspath *aspath = p;
2040 unsigned int key = 0;
2041
2042 if (!aspath->str)
2043 aspath_str_update((struct aspath *)aspath, false);
2044
2045 key = jhash(aspath->str, aspath->str_len, 2334325);
2046
2047 return key;
2048 }
2049
2050 /* If two aspath have same value then return 1 else return 0 */
2051 bool aspath_cmp(const void *arg1, const void *arg2)
2052 {
2053 const struct assegment *seg1 = ((const struct aspath *)arg1)->segments;
2054 const struct assegment *seg2 = ((const struct aspath *)arg2)->segments;
2055
2056 while (seg1 || seg2) {
2057 int i;
2058 if ((!seg1 && seg2) || (seg1 && !seg2))
2059 return false;
2060 if (seg1->type != seg2->type)
2061 return false;
2062 if (seg1->length != seg2->length)
2063 return false;
2064 for (i = 0; i < seg1->length; i++)
2065 if (seg1->as[i] != seg2->as[i])
2066 return false;
2067 seg1 = seg1->next;
2068 seg2 = seg2->next;
2069 }
2070 return true;
2071 }
2072
2073 /* AS path hash initialize. */
2074 void aspath_init(void)
2075 {
2076 ashash = hash_create_size(32768, aspath_key_make, aspath_cmp,
2077 "BGP AS Path");
2078 }
2079
2080 void aspath_finish(void)
2081 {
2082 hash_clean(ashash, (void (*)(void *))aspath_free);
2083 hash_free(ashash);
2084 ashash = NULL;
2085
2086 if (snmp_stream)
2087 stream_free(snmp_stream);
2088 }
2089
2090 /* return and as path value */
2091 const char *aspath_print(struct aspath *as)
2092 {
2093 return (as ? as->str : NULL);
2094 }
2095
2096 /* Printing functions */
2097 /* Feed the AS_PATH to the vty; the suffix string follows it only in case
2098 * AS_PATH wasn't empty.
2099 */
2100 void aspath_print_vty(struct vty *vty, const char *format, struct aspath *as,
2101 const char *suffix)
2102 {
2103 assert(format);
2104 vty_out(vty, format, as->str);
2105 if (as->str_len && strlen(suffix))
2106 vty_out(vty, "%s", suffix);
2107 }
2108
2109 static void aspath_show_all_iterator(struct hash_bucket *bucket,
2110 struct vty *vty)
2111 {
2112 struct aspath *as;
2113
2114 as = (struct aspath *)bucket->data;
2115
2116 vty_out(vty, "[%p:%u] (%ld) ", (void *)bucket, bucket->key, as->refcnt);
2117 vty_out(vty, "%s\n", as->str);
2118 }
2119
2120 /* Print all aspath and hash information. This function is used from
2121 `show [ip] bgp paths' command. */
2122 void aspath_print_all_vty(struct vty *vty)
2123 {
2124 hash_iterate(ashash, (void (*)(struct hash_bucket *,
2125 void *))aspath_show_all_iterator,
2126 vty);
2127 }
2128
2129 static struct aspath *bgp_aggr_aspath_lookup(struct bgp_aggregate *aggregate,
2130 struct aspath *aspath)
2131 {
2132 return hash_lookup(aggregate->aspath_hash, aspath);
2133 }
2134
2135 static void *bgp_aggr_aspath_hash_alloc(void *p)
2136 {
2137 struct aspath *ref = (struct aspath *)p;
2138 struct aspath *aspath = NULL;
2139
2140 aspath = aspath_dup(ref);
2141 return aspath;
2142 }
2143
2144 static void bgp_aggr_aspath_prepare(struct hash_bucket *hb, void *arg)
2145 {
2146 struct aspath *hb_aspath = hb->data;
2147 struct aspath **aggr_aspath = arg;
2148
2149 if (*aggr_aspath)
2150 *aggr_aspath = aspath_aggregate(*aggr_aspath, hb_aspath);
2151 else
2152 *aggr_aspath = aspath_dup(hb_aspath);
2153 }
2154
2155 void bgp_aggr_aspath_remove(void *arg)
2156 {
2157 struct aspath *aspath = arg;
2158
2159 aspath_free(aspath);
2160 }
2161
2162 void bgp_compute_aggregate_aspath(struct bgp_aggregate *aggregate,
2163 struct aspath *aspath)
2164 {
2165 bgp_compute_aggregate_aspath_hash(aggregate, aspath);
2166
2167 bgp_compute_aggregate_aspath_val(aggregate);
2168
2169 }
2170
2171 void bgp_compute_aggregate_aspath_hash(struct bgp_aggregate *aggregate,
2172 struct aspath *aspath)
2173 {
2174 struct aspath *aggr_aspath = NULL;
2175
2176 if ((aggregate == NULL) || (aspath == NULL))
2177 return;
2178
2179 /* Create hash if not already created.
2180 */
2181 if (aggregate->aspath_hash == NULL)
2182 aggregate->aspath_hash = hash_create(
2183 aspath_key_make, aspath_cmp,
2184 "BGP Aggregator as-path hash");
2185
2186 aggr_aspath = bgp_aggr_aspath_lookup(aggregate, aspath);
2187 if (aggr_aspath == NULL) {
2188 /* Insert as-path into hash.
2189 */
2190 aggr_aspath = hash_get(aggregate->aspath_hash, aspath,
2191 bgp_aggr_aspath_hash_alloc);
2192 }
2193
2194 /* Increment reference counter.
2195 */
2196 aggr_aspath->refcnt++;
2197 }
2198
2199 void bgp_compute_aggregate_aspath_val(struct bgp_aggregate *aggregate)
2200 {
2201 if (aggregate == NULL)
2202 return;
2203 /* Re-compute aggregate's as-path.
2204 */
2205 if (aggregate->aspath) {
2206 aspath_free(aggregate->aspath);
2207 aggregate->aspath = NULL;
2208 }
2209 if (aggregate->aspath_hash
2210 && aggregate->aspath_hash->count) {
2211 hash_iterate(aggregate->aspath_hash,
2212 bgp_aggr_aspath_prepare,
2213 &aggregate->aspath);
2214 }
2215 }
2216
2217 void bgp_remove_aspath_from_aggregate(struct bgp_aggregate *aggregate,
2218 struct aspath *aspath)
2219 {
2220 struct aspath *aggr_aspath = NULL;
2221 struct aspath *ret_aspath = NULL;
2222
2223 if ((!aggregate)
2224 || (!aggregate->aspath_hash)
2225 || (!aspath))
2226 return;
2227
2228 /* Look-up the aspath in the hash.
2229 */
2230 aggr_aspath = bgp_aggr_aspath_lookup(aggregate, aspath);
2231 if (aggr_aspath) {
2232 aggr_aspath->refcnt--;
2233
2234 if (aggr_aspath->refcnt == 0) {
2235 ret_aspath = hash_release(aggregate->aspath_hash,
2236 aggr_aspath);
2237 aspath_free(ret_aspath);
2238 ret_aspath = NULL;
2239
2240 /* Remove aggregate's old as-path.
2241 */
2242 aspath_free(aggregate->aspath);
2243 aggregate->aspath = NULL;
2244
2245 bgp_compute_aggregate_aspath_val(aggregate);
2246 }
2247 }
2248 }
2249
2250 void bgp_remove_aspath_from_aggregate_hash(struct bgp_aggregate *aggregate,
2251 struct aspath *aspath)
2252 {
2253 struct aspath *aggr_aspath = NULL;
2254 struct aspath *ret_aspath = NULL;
2255
2256 if ((!aggregate)
2257 || (!aggregate->aspath_hash)
2258 || (!aspath))
2259 return;
2260
2261 /* Look-up the aspath in the hash.
2262 */
2263 aggr_aspath = bgp_aggr_aspath_lookup(aggregate, aspath);
2264 if (aggr_aspath) {
2265 aggr_aspath->refcnt--;
2266
2267 if (aggr_aspath->refcnt == 0) {
2268 ret_aspath = hash_release(aggregate->aspath_hash,
2269 aggr_aspath);
2270 aspath_free(ret_aspath);
2271 ret_aspath = NULL;
2272 }
2273 }
2274 }
2275