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