blob: 3fd359cc50dd327c1023e6f2637daf5273493e50 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* AS path management routines.
2 Copyright (C) 1996, 97, 98, 99 Kunihiro Ishiguro
paulfe69a502005-09-10 16:55:02 +00003 Copyright (C) 2005 Sun Microsystems, Inc.
paul718e3742002-12-13 20:15:29 +00004
5This file is part of GNU Zebra.
6
7GNU Zebra is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by the
9Free Software Foundation; either version 2, or (at your option) any
10later version.
11
12GNU Zebra is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Zebra; see the file COPYING. If not, write to the Free
19Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
21
22#include <zebra.h>
23
24#include "hash.h"
25#include "memory.h"
26#include "vector.h"
27#include "vty.h"
28#include "str.h"
29#include "log.h"
paulfe69a502005-09-10 16:55:02 +000030#include "stream.h"
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000031#include "jhash.h"
Donald Sharp04907292016-01-07 10:03:01 -050032#include "filter.h"
paul718e3742002-12-13 20:15:29 +000033
34#include "bgpd/bgpd.h"
35#include "bgpd/bgp_aspath.h"
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000036#include "bgpd/bgp_debug.h"
37#include "bgpd/bgp_attr.h"
David Lamparter6b0655a2014-06-04 06:53:35 +020038
paul718e3742002-12-13 20:15:29 +000039/* Attr. Flags and Attr. Type Code. */
40#define AS_HEADER_SIZE 2
41
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000042/* Now FOUR octets are used for AS value. */
paul718e3742002-12-13 20:15:29 +000043#define AS_VALUE_SIZE sizeof (as_t)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000044/* This is the old one */
45#define AS16_VALUE_SIZE sizeof (as16_t)
paul718e3742002-12-13 20:15:29 +000046
paulfe69a502005-09-10 16:55:02 +000047/* Maximum protocol segment length value */
48#define AS_SEGMENT_MAX 255
paul718e3742002-12-13 20:15:29 +000049
paulfe69a502005-09-10 16:55:02 +000050/* 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.
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000055 *
56 * aspath_put returns bytes written, the only definitive record of
57 * size of wire-format attribute..
paulfe69a502005-09-10 16:55:02 +000058 */
59
60/* Calculated size in bytes of ASN segment data to hold N ASN's */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000061#define ASSEGMENT_DATA_SIZE(N,S) \
62 ((N) * ( (S) ? AS_VALUE_SIZE : AS16_VALUE_SIZE) )
paulfe69a502005-09-10 16:55:02 +000063
64/* Calculated size of segment struct to hold N ASN's */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000065#define ASSEGMENT_SIZE(N,S) (AS_HEADER_SIZE + ASSEGMENT_DATA_SIZE (N,S))
paulfe69a502005-09-10 16:55:02 +000066
67/* AS segment octet length. */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000068#define ASSEGMENT_LEN(X,S) ASSEGMENT_SIZE((X)->length,S)
paulfe69a502005-09-10 16:55:02 +000069
70/* AS_SEQUENCE segments can be packed together */
71/* Can the types of X and Y be considered for packing? */
72#define ASSEGMENT_TYPES_PACKABLE(X,Y) \
73 ( ((X)->type == (Y)->type) \
74 && ((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 */
83struct assegment_header
paul718e3742002-12-13 20:15:29 +000084{
85 u_char type;
86 u_char length;
paul718e3742002-12-13 20:15:29 +000087};
88
89/* Hash for aspath. This is the top level structure of AS path. */
Stephen Hemmingerda88ea82009-12-17 13:14:28 +030090static struct hash *ashash;
paul8fdc32a2006-01-16 12:01:29 +000091
92/* Stream for SNMP. See aspath_snmp_pathseg */
93static struct stream *snmp_stream;
David Lamparter6b0655a2014-06-04 06:53:35 +020094
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +000095/* Callers are required to initialize the memory */
Paul Jakmaf63f06d2011-04-08 12:44:43 +010096static as_t *
paulfe69a502005-09-10 16:55:02 +000097assegment_data_new (int num)
98{
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +000099 return (XMALLOC (MTYPE_AS_SEG_DATA, ASSEGMENT_DATA_SIZE (num, 1)));
paulfe69a502005-09-10 16:55:02 +0000100}
101
Lou Berger056f3762013-04-10 12:30:04 -0700102static void
103assegment_data_free (as_t *asdata)
104{
105 XFREE (MTYPE_AS_SEG_DATA, asdata);
106}
107
paulfe69a502005-09-10 16:55:02 +0000108/* Get a new segment. Note that 0 is an allowed length,
109 * and will result in a segment with no allocated data segment.
110 * the caller should immediately assign data to the segment, as the segment
111 * otherwise is not generally valid
112 */
113static struct assegment *
114assegment_new (u_char type, u_short length)
115{
116 struct assegment *new;
117
118 new = XCALLOC (MTYPE_AS_SEG, sizeof (struct assegment));
119
120 if (length)
121 new->as = assegment_data_new (length);
122
123 new->length = length;
124 new->type = type;
125
126 return new;
127}
128
129static void
130assegment_free (struct assegment *seg)
131{
132 if (!seg)
133 return;
134
135 if (seg->as)
Lou Berger056f3762013-04-10 12:30:04 -0700136 assegment_data_free (seg->as);
paulfe69a502005-09-10 16:55:02 +0000137 memset (seg, 0xfe, sizeof(struct assegment));
138 XFREE (MTYPE_AS_SEG, seg);
139
140 return;
141}
142
143/* free entire chain of segments */
144static void
145assegment_free_all (struct assegment *seg)
146{
147 struct assegment *prev;
148
149 while (seg)
150 {
151 prev = seg;
152 seg = seg->next;
153 assegment_free (prev);
154 }
155}
156
157/* Duplicate just the given assegment and its data */
158static struct assegment *
159assegment_dup (struct assegment *seg)
160{
161 struct assegment *new;
162
163 new = assegment_new (seg->type, seg->length);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000164 memcpy (new->as, seg->as, ASSEGMENT_DATA_SIZE (new->length, 1) );
paulfe69a502005-09-10 16:55:02 +0000165
166 return new;
167}
168
169/* Duplicate entire chain of assegments, return the head */
170static struct assegment *
171assegment_dup_all (struct assegment *seg)
172{
173 struct assegment *new = NULL;
174 struct assegment *head = NULL;
175
176 while (seg)
177 {
178 if (head)
179 {
180 new->next = assegment_dup (seg);
181 new = new->next;
182 }
183 else
184 head = new = assegment_dup (seg);
185
186 seg = seg->next;
187 }
188 return head;
189}
190
191/* prepend the as number to given segment, given num of times */
192static struct assegment *
193assegment_prepend_asns (struct assegment *seg, as_t asnum, int num)
194{
195 as_t *newas;
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000196 int i;
paulfe69a502005-09-10 16:55:02 +0000197
198 if (!num)
199 return seg;
200
201 if (num >= AS_SEGMENT_MAX)
202 return seg; /* we don't do huge prepends */
203
Lou Berger056f3762013-04-10 12:30:04 -0700204 if ((newas = assegment_data_new (seg->length + num)) == NULL)
205 return seg;
206
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000207 for (i = 0; i < num; i++)
208 newas[i] = asnum;
209
210 memcpy (newas + num, seg->as, ASSEGMENT_DATA_SIZE (seg->length, 1));
Lou Berger056f3762013-04-10 12:30:04 -0700211 assegment_data_free (seg->as);
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000212 seg->as = newas;
213 seg->length += num;
214
215 return seg;
paulfe69a502005-09-10 16:55:02 +0000216}
217
218/* append given array of as numbers to the segment */
219static struct assegment *
220assegment_append_asns (struct assegment *seg, as_t *asnos, int num)
221{
paul02335422006-01-16 11:13:27 +0000222 as_t *newas;
223
224 newas = XREALLOC (MTYPE_AS_SEG_DATA, seg->as,
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000225 ASSEGMENT_DATA_SIZE (seg->length + num, 1));
paulfe69a502005-09-10 16:55:02 +0000226
paul02335422006-01-16 11:13:27 +0000227 if (newas)
paulfe69a502005-09-10 16:55:02 +0000228 {
paul02335422006-01-16 11:13:27 +0000229 seg->as = newas;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000230 memcpy (seg->as + seg->length, asnos, ASSEGMENT_DATA_SIZE(num, 1));
paulfe69a502005-09-10 16:55:02 +0000231 seg->length += num;
232 return seg;
233 }
234
235 assegment_free_all (seg);
236 return NULL;
237}
238
239static int
240int_cmp (const void *p1, const void *p2)
241{
242 const as_t *as1 = p1;
243 const as_t *as2 = p2;
244
245 return (*as1 == *as2)
246 ? 0 : ( (*as1 > *as2) ? 1 : -1);
247}
248
249/* normalise the segment.
250 * In particular, merge runs of AS_SEQUENCEs into one segment
251 * Internally, we do not care about the wire segment length limit, and
252 * we want each distinct AS_PATHs to have the exact same internal
253 * representation - eg, so that our hashing actually works..
254 */
255static struct assegment *
256assegment_normalise (struct assegment *head)
257{
258 struct assegment *seg = head, *pin;
259 struct assegment *tmp;
260
261 if (!head)
262 return head;
263
264 while (seg)
265 {
266 pin = seg;
267
268 /* Sort values SET segments, for determinism in paths to aid
269 * creation of hash values / path comparisons
270 * and because it helps other lesser implementations ;)
271 */
272 if (seg->type == AS_SET || seg->type == AS_CONFED_SET)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000273 {
274 int tail = 0;
275 int i;
276
277 qsort (seg->as, seg->length, sizeof(as_t), int_cmp);
278
279 /* weed out dupes */
280 for (i=1; i < seg->length; i++)
281 {
282 if (seg->as[tail] == seg->as[i])
283 continue;
284
285 tail++;
286 if (tail < i)
287 seg->as[tail] = seg->as[i];
288 }
289 /* seg->length can be 0.. */
290 if (seg->length)
291 seg->length = tail + 1;
292 }
paulfe69a502005-09-10 16:55:02 +0000293
294 /* read ahead from the current, pinned segment while the segments
295 * are packable/mergeable. Append all following packable segments
296 * to the segment we have pinned and remove these appended
297 * segments.
298 */
299 while (pin->next && ASSEGMENT_TYPES_PACKABLE(pin, pin->next))
300 {
301 tmp = pin->next;
302 seg = pin->next;
303
304 /* append the next sequence to the pinned sequence */
305 pin = assegment_append_asns (pin, seg->as, seg->length);
306
307 /* bypass the next sequence */
308 pin->next = seg->next;
309
310 /* get rid of the now referenceless segment */
311 assegment_free (tmp);
312
313 }
314
315 seg = pin->next;
316 }
317 return head;
318}
David Lamparter6b0655a2014-06-04 06:53:35 +0200319
paul718e3742002-12-13 20:15:29 +0000320static struct aspath *
paulfe69a502005-09-10 16:55:02 +0000321aspath_new (void)
paul718e3742002-12-13 20:15:29 +0000322{
Stephen Hemminger393deb92008-08-18 14:13:29 -0700323 return XCALLOC (MTYPE_AS_PATH, sizeof (struct aspath));
paul718e3742002-12-13 20:15:29 +0000324}
325
326/* Free AS path structure. */
327void
328aspath_free (struct aspath *aspath)
329{
330 if (!aspath)
331 return;
paulfe69a502005-09-10 16:55:02 +0000332 if (aspath->segments)
333 assegment_free_all (aspath->segments);
paul718e3742002-12-13 20:15:29 +0000334 if (aspath->str)
335 XFREE (MTYPE_AS_STR, aspath->str);
336 XFREE (MTYPE_AS_PATH, aspath);
337}
338
339/* Unintern aspath from AS path bucket. */
340void
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000341aspath_unintern (struct aspath **aspath)
paul718e3742002-12-13 20:15:29 +0000342{
343 struct aspath *ret;
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000344 struct aspath *asp = *aspath;
345
346 if (asp->refcnt)
347 asp->refcnt--;
paul718e3742002-12-13 20:15:29 +0000348
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000349 if (asp->refcnt == 0)
paul718e3742002-12-13 20:15:29 +0000350 {
351 /* This aspath must exist in aspath hash table. */
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000352 ret = hash_release (ashash, asp);
paul718e3742002-12-13 20:15:29 +0000353 assert (ret != NULL);
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000354 aspath_free (asp);
355 *aspath = NULL;
paul718e3742002-12-13 20:15:29 +0000356 }
357}
358
359/* Return the start or end delimiters for a particular Segment type */
360#define AS_SEG_START 0
361#define AS_SEG_END 1
362static char
363aspath_delimiter_char (u_char type, u_char which)
364{
365 int i;
366 struct
367 {
368 int type;
369 char start;
370 char end;
371 } aspath_delim_char [] =
372 {
373 { AS_SET, '{', '}' },
paul718e3742002-12-13 20:15:29 +0000374 { AS_CONFED_SET, '[', ']' },
375 { AS_CONFED_SEQUENCE, '(', ')' },
376 { 0 }
377 };
378
379 for (i = 0; aspath_delim_char[i].type != 0; i++)
380 {
381 if (aspath_delim_char[i].type == type)
382 {
383 if (which == AS_SEG_START)
384 return aspath_delim_char[i].start;
385 else if (which == AS_SEG_END)
386 return aspath_delim_char[i].end;
387 }
388 }
389 return ' ';
390}
391
Denis Ovsienko014b6702009-06-23 21:10:45 +0400392/* countup asns from this segment and index onward */
393static int
394assegment_count_asns (struct assegment *seg, int from)
395{
396 int count = 0;
397 while (seg)
398 {
399 if (!from)
400 count += seg->length;
401 else
402 {
403 count += (seg->length - from);
404 from = 0;
405 }
406 seg = seg->next;
407 }
408 return count;
409}
410
paulfe69a502005-09-10 16:55:02 +0000411unsigned int
412aspath_count_confeds (struct aspath *aspath)
413{
414 int count = 0;
415 struct assegment *seg = aspath->segments;
416
417 while (seg)
418 {
419 if (seg->type == AS_CONFED_SEQUENCE)
420 count += seg->length;
421 else if (seg->type == AS_CONFED_SET)
422 count++;
423
424 seg = seg->next;
425 }
426 return count;
427}
428
429unsigned int
430aspath_count_hops (struct aspath *aspath)
431{
432 int count = 0;
433 struct assegment *seg = aspath->segments;
434
435 while (seg)
436 {
437 if (seg->type == AS_SEQUENCE)
438 count += seg->length;
439 else if (seg->type == AS_SET)
440 count++;
441
442 seg = seg->next;
443 }
444 return count;
445}
446
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000447/* 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 */
paulfe69a502005-09-10 16:55:02 +0000453unsigned int
454aspath_size (struct aspath *aspath)
455{
456 int size = 0;
457 struct assegment *seg = aspath->segments;
458
459 while (seg)
460 {
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000461 size += ASSEGMENT_SIZE(seg->length, 1);
paulfe69a502005-09-10 16:55:02 +0000462 seg = seg->next;
463 }
464 return size;
465}
466
Paul Jakma2815e612006-09-14 02:56:07 +0000467/* Return highest public ASN in path */
468as_t
469aspath_highest (struct aspath *aspath)
470{
471 struct assegment *seg = aspath->segments;
472 as_t highest = 0;
473 unsigned int i;
474
475 while (seg)
476 {
477 for (i = 0; i < seg->length; i++)
Daniel Waltond0aa6e82016-06-17 14:45:42 +0100478 if (seg->as[i] > highest
479 && !BGP_AS_IS_PRIVATE(seg->as[i]))
Paul Jakma2815e612006-09-14 02:56:07 +0000480 highest = seg->as[i];
481 seg = seg->next;
482 }
483 return highest;
484}
485
Timo Teräs85c854a2014-09-30 11:31:53 +0300486/* Return the left-most ASN in path */
487as_t
488aspath_leftmost (struct aspath *aspath)
489{
490 struct assegment *seg = aspath->segments;
491 as_t leftmost = 0;
492
493 if (seg && seg->length && seg->type == AS_SEQUENCE)
494 leftmost = seg->as[0];
495
496 return leftmost;
497}
498
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000499/* Return 1 if there are any 4-byte ASes in the path */
500unsigned int
501aspath_has_as4 (struct aspath *aspath)
502{
503 struct assegment *seg = aspath->segments;
504 unsigned int i;
505
506 while (seg)
507 {
508 for (i = 0; i < seg->length; i++)
509 if (seg->as[i] > BGP_AS_MAX)
510 return 1;
511 seg = seg->next;
512 }
513 return 0;
514}
515
paul718e3742002-12-13 20:15:29 +0000516/* Convert aspath structure to string expression. */
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000517static void
paul718e3742002-12-13 20:15:29 +0000518aspath_make_str_count (struct aspath *as)
519{
paulfe69a502005-09-10 16:55:02 +0000520 struct assegment *seg;
521 int str_size;
522 int len = 0;
hassoc9e52be2004-09-26 16:09:34 +0000523 char *str_buf;
paul718e3742002-12-13 20:15:29 +0000524
525 /* Empty aspath. */
paulfe69a502005-09-10 16:55:02 +0000526 if (!as->segments)
paul718e3742002-12-13 20:15:29 +0000527 {
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000528 as->str = XMALLOC (MTYPE_AS_STR, 1);
529 as->str[0] = '\0';
530 as->str_len = 0;
531 return;
paul718e3742002-12-13 20:15:29 +0000532 }
paulfe69a502005-09-10 16:55:02 +0000533
534 seg = as->segments;
535
Denis Ovsienko014b6702009-06-23 21:10:45 +0400536 /* ASN takes 5 to 10 chars plus seperator, see below.
537 * If there is one differing segment type, we need an additional
538 * 2 chars for segment delimiters, and the final '\0'.
539 * Hopefully this is large enough to avoid hitting the realloc
540 * code below for most common sequences.
541 *
542 * This was changed to 10 after the well-known BGP assertion, which
543 * had hit some parts of the Internet in May of 2009.
544 */
545#define ASN_STR_LEN (10 + 1)
546 str_size = MAX (assegment_count_asns (seg, 0) * ASN_STR_LEN + 2 + 1,
547 ASPATH_STR_DEFAULT_LEN);
paul718e3742002-12-13 20:15:29 +0000548 str_buf = XMALLOC (MTYPE_AS_STR, str_size);
paul718e3742002-12-13 20:15:29 +0000549
paulfe69a502005-09-10 16:55:02 +0000550 while (seg)
paul718e3742002-12-13 20:15:29 +0000551 {
552 int i;
paulfe69a502005-09-10 16:55:02 +0000553 char seperator;
paul718e3742002-12-13 20:15:29 +0000554
paulfe69a502005-09-10 16:55:02 +0000555 /* Check AS type validity. Set seperator for segment */
556 switch (seg->type)
557 {
558 case AS_SET:
559 case AS_CONFED_SET:
560 seperator = ',';
561 break;
562 case AS_SEQUENCE:
563 case AS_CONFED_SEQUENCE:
564 seperator = ' ';
565 break;
566 default:
567 XFREE (MTYPE_AS_STR, str_buf);
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000568 as->str = NULL;
569 as->str_len = 0;
570 return;
paulfe69a502005-09-10 16:55:02 +0000571 }
572
Denis Ovsienko014b6702009-06-23 21:10:45 +0400573 /* We might need to increase str_buf, particularly if path has
574 * differing segments types, our initial guesstimate above will
575 * have been wrong. Need 10 chars for ASN, a seperator each and
576 * potentially two segment delimiters, plus a space between each
577 * segment and trailing zero.
578 *
579 * This definitely didn't work with the value of 5 bytes and
580 * 32-bit ASNs.
581 */
582#define SEGMENT_STR_LEN(X) (((X)->length * ASN_STR_LEN) + 2 + 1 + 1)
583 if ( (len + SEGMENT_STR_LEN(seg)) > str_size)
584 {
585 str_size = len + SEGMENT_STR_LEN(seg);
586 str_buf = XREALLOC (MTYPE_AS_STR, str_buf, str_size);
587 }
588#undef ASN_STR_LEN
589#undef SEGMENT_STR_LEN
590
paulfe69a502005-09-10 16:55:02 +0000591 if (seg->type != AS_SEQUENCE)
Denis Ovsienko014b6702009-06-23 21:10:45 +0400592 len += snprintf (str_buf + len, str_size - len,
593 "%c",
594 aspath_delimiter_char (seg->type, AS_SEG_START));
paulfe69a502005-09-10 16:55:02 +0000595
596 /* write out the ASNs, with their seperators, bar the last one*/
597 for (i = 0; i < seg->length; i++)
598 {
599 len += snprintf (str_buf + len, str_size - len, "%u", seg->as[i]);
600
601 if (i < (seg->length - 1))
602 len += snprintf (str_buf + len, str_size - len, "%c", seperator);
603 }
604
605 if (seg->type != AS_SEQUENCE)
606 len += snprintf (str_buf + len, str_size - len, "%c",
607 aspath_delimiter_char (seg->type, AS_SEG_END));
608 if (seg->next)
609 len += snprintf (str_buf + len, str_size - len, " ");
610
611 seg = seg->next;
paul718e3742002-12-13 20:15:29 +0000612 }
paulfe69a502005-09-10 16:55:02 +0000613
614 assert (len < str_size);
615
616 str_buf[len] = '\0';
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000617 as->str = str_buf;
618 as->str_len = len;
paul718e3742002-12-13 20:15:29 +0000619
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000620 return;
paul718e3742002-12-13 20:15:29 +0000621}
622
paulfe69a502005-09-10 16:55:02 +0000623static void
624aspath_str_update (struct aspath *as)
625{
626 if (as->str)
627 XFREE (MTYPE_AS_STR, as->str);
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000628 aspath_make_str_count (as);
paulfe69a502005-09-10 16:55:02 +0000629}
630
paul718e3742002-12-13 20:15:29 +0000631/* Intern allocated AS path. */
632struct aspath *
633aspath_intern (struct aspath *aspath)
634{
635 struct aspath *find;
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000636
637 /* Assert this AS path structure is not interned and has the string
638 representation built. */
paul718e3742002-12-13 20:15:29 +0000639 assert (aspath->refcnt == 0);
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000640 assert (aspath->str);
paul718e3742002-12-13 20:15:29 +0000641
642 /* Check AS path hash. */
643 find = hash_get (ashash, aspath, hash_alloc_intern);
paul718e3742002-12-13 20:15:29 +0000644 if (find != aspath)
paulfe69a502005-09-10 16:55:02 +0000645 aspath_free (aspath);
paul718e3742002-12-13 20:15:29 +0000646
647 find->refcnt++;
648
paul718e3742002-12-13 20:15:29 +0000649 return find;
650}
651
652/* Duplicate aspath structure. Created same aspath structure but
653 reference count and AS path string is cleared. */
654struct aspath *
655aspath_dup (struct aspath *aspath)
656{
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000657 unsigned short buflen = aspath->str_len + 1;
paul718e3742002-12-13 20:15:29 +0000658 struct aspath *new;
659
paulfe69a502005-09-10 16:55:02 +0000660 new = XCALLOC (MTYPE_AS_PATH, sizeof (struct aspath));
paul718e3742002-12-13 20:15:29 +0000661
paulfe69a502005-09-10 16:55:02 +0000662 if (aspath->segments)
663 new->segments = assegment_dup_all (aspath->segments);
paul718e3742002-12-13 20:15:29 +0000664
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000665 if (!aspath->str)
666 return new;
667
668 new->str = XMALLOC (MTYPE_AS_STR, buflen);
669 new->str_len = aspath->str_len;
670
671 /* copy the string data */
672 if (aspath->str_len > 0)
673 memcpy (new->str, aspath->str, buflen);
674 else
675 new->str[0] = '\0';
paul718e3742002-12-13 20:15:29 +0000676
677 return new;
678}
679
paul94f2b392005-06-28 12:44:16 +0000680static void *
paulfe69a502005-09-10 16:55:02 +0000681aspath_hash_alloc (void *arg)
paul718e3742002-12-13 20:15:29 +0000682{
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000683 const struct aspath *aspath = arg;
684 struct aspath *new;
685
686 /* Malformed AS path value. */
687 assert (aspath->str);
688 if (! aspath->str)
689 return NULL;
paul718e3742002-12-13 20:15:29 +0000690
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000691 /* New aspath structure is needed. */
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000692 new = XMALLOC (MTYPE_AS_PATH, sizeof (struct aspath));
paul718e3742002-12-13 20:15:29 +0000693
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000694 /* 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
700 return new;
paul718e3742002-12-13 20:15:29 +0000701}
702
Paul Jakmaab005292010-11-27 22:48:34 +0000703/* parse as-segment byte stream in struct assegment */
Paul Jakmab881c702010-11-23 16:35:42 +0000704static int
705assegments_parse (struct stream *s, size_t length,
706 struct assegment **result, int use32bit)
paulfe69a502005-09-10 16:55:02 +0000707{
708 struct assegment_header segh;
709 struct assegment *seg, *prev = NULL, *head = NULL;
Paul Jakmaab005292010-11-27 22:48:34 +0000710 size_t bytes = 0;
paulfe69a502005-09-10 16:55:02 +0000711
Paul Jakmaab005292010-11-27 22:48:34 +0000712 /* empty aspath (ie iBGP or somesuch) */
713 if (length == 0)
Paul Jakmab881c702010-11-23 16:35:42 +0000714 return 0;
paulfe69a502005-09-10 16:55:02 +0000715
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000716 if (BGP_DEBUG (as4, AS4_SEGMENT))
717 zlog_debug ("[AS4SEG] Parse aspath segment: got total byte length %lu",
718 (unsigned long) length);
Paul Jakmaab005292010-11-27 22:48:34 +0000719 /* basic checks */
720 if ((STREAM_READABLE(s) < length)
721 || (STREAM_READABLE(s) < AS_HEADER_SIZE)
722 || (length % AS16_VALUE_SIZE ))
Paul Jakmab881c702010-11-23 16:35:42 +0000723 return -1;
paulfe69a502005-09-10 16:55:02 +0000724
Paul Jakmaab005292010-11-27 22:48:34 +0000725 while (bytes < length)
paulfe69a502005-09-10 16:55:02 +0000726 {
727 int i;
Chris Hallcddb8112010-08-09 22:31:37 +0400728 size_t seg_size;
paulfe69a502005-09-10 16:55:02 +0000729
Paul Jakmaab005292010-11-27 22:48:34 +0000730 if ((length - bytes) <= AS_HEADER_SIZE)
Chris Hallcddb8112010-08-09 22:31:37 +0400731 {
Paul Jakmaab005292010-11-27 22:48:34 +0000732 if (head)
733 assegment_free_all (head);
Paul Jakmab881c702010-11-23 16:35:42 +0000734 return -1;
Paul Jakmafdbc8e72011-04-11 16:31:43 +0100735 }
736
Paul Jakmaab005292010-11-27 22:48:34 +0000737 /* softly softly, get the header first on its own */
paulfe69a502005-09-10 16:55:02 +0000738 segh.type = stream_getc (s);
739 segh.length = stream_getc (s);
740
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000741 seg_size = ASSEGMENT_SIZE(segh.length, use32bit);
742
743 if (BGP_DEBUG (as4, AS4_SEGMENT))
744 zlog_debug ("[AS4SEG] Parse aspath segment: got type %d, length %d",
745 segh.type, segh.length);
746
Paul Jakmaab005292010-11-27 22:48:34 +0000747 /* 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 #564).
Denis Ovsienko2eb445e2009-12-04 17:32:54 +0300754 */
Paul Jakmaab005292010-11-27 22:48:34 +0000755 || ((sizeof segh.length > 1)
756 && (0x10 + segh.length > 0x10 + AS_SEGMENT_MAX)))
paulfe69a502005-09-10 16:55:02 +0000757 {
Paul Jakmaab005292010-11-27 22:48:34 +0000758 if (head)
paulfe69a502005-09-10 16:55:02 +0000759 assegment_free_all (head);
Paul Jakmab881c702010-11-23 16:35:42 +0000760 return -1;
paulfe69a502005-09-10 16:55:02 +0000761 }
762
Paul Jakmafdbc8e72011-04-11 16:31:43 +0100763 switch (segh.type)
764 {
765 case AS_SEQUENCE:
766 case AS_SET:
Paul Jakmafdbc8e72011-04-11 16:31:43 +0100767 case AS_CONFED_SEQUENCE:
768 case AS_CONFED_SET:
Paul Jakmaab005292010-11-27 22:48:34 +0000769 break;
770 default:
771 if (head)
772 assegment_free_all (head);
Paul Jakmab881c702010-11-23 16:35:42 +0000773 return -1;
paulfe69a502005-09-10 16:55:02 +0000774 }
Chris Hallcddb8112010-08-09 22:31:37 +0400775
paulfe69a502005-09-10 16:55:02 +0000776 /* now its safe to trust lengths */
777 seg = assegment_new (segh.type, segh.length);
778
779 if (head)
780 prev->next = seg;
781 else /* it's the first segment */
782 head = prev = seg;
783
784 for (i = 0; i < segh.length; i++)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000785 seg->as[i] = (use32bit) ? stream_getl (s) : stream_getw (s);
786
Paul Jakmaab005292010-11-27 22:48:34 +0000787 bytes += seg_size;
788
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000789 if (BGP_DEBUG (as4, AS4_SEGMENT))
Paul Jakmaab005292010-11-27 22:48:34 +0000790 zlog_debug ("[AS4SEG] Parse aspath segment: Bytes now: %lu",
791 (unsigned long) bytes);
paulfe69a502005-09-10 16:55:02 +0000792
793 prev = seg;
794 }
795
Paul Jakmab881c702010-11-23 16:35:42 +0000796 *result = assegment_normalise (head);
797 return 0;
paulfe69a502005-09-10 16:55:02 +0000798}
799
Paul Jakmaab005292010-11-27 22:48:34 +0000800/* AS path parse function. pnt is a pointer to byte stream and length
801 is length of byte stream. If there is same AS path in the the AS
Paul Jakmab881c702010-11-23 16:35:42 +0000802 path hash then return it else make new AS path structure.
803
804 On error NULL is returned.
Chris Hallcddb8112010-08-09 22:31:37 +0400805 */
paul718e3742002-12-13 20:15:29 +0000806struct aspath *
Paul Jakmaab005292010-11-27 22:48:34 +0000807aspath_parse (struct stream *s, size_t length, int use32bit)
paul718e3742002-12-13 20:15:29 +0000808{
809 struct aspath as;
810 struct aspath *find;
811
Paul Jakmaab005292010-11-27 22:48:34 +0000812 /* 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;
Chris Hallcddb8112010-08-09 22:31:37 +0400820
Paul Jakmaab005292010-11-27 22:48:34 +0000821 memset (&as, 0, sizeof (struct aspath));
Paul Jakmab881c702010-11-23 16:35:42 +0000822 if (assegments_parse (s, length, &as.segments, use32bit) < 0)
823 return NULL;
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000824
paul718e3742002-12-13 20:15:29 +0000825 /* If already same aspath exist then return it. */
826 find = hash_get (ashash, &as, aspath_hash_alloc);
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000827
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 {
834 assegment_free_all (as.segments);
835 /* aspath_key_make() always updates the string */
836 XFREE (MTYPE_AS_STR, as.str);
837 }
838
paul718e3742002-12-13 20:15:29 +0000839 find->refcnt++;
840
841 return find;
842}
843
Paul Jakmaf63f06d2011-04-08 12:44:43 +0100844static void
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000845assegment_data_put (struct stream *s, as_t *as, int num, int use32bit)
paul718e3742002-12-13 20:15:29 +0000846{
paulfe69a502005-09-10 16:55:02 +0000847 int i;
848 assert (num <= AS_SEGMENT_MAX);
849
850 for (i = 0; i < num; i++)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000851 if ( use32bit )
852 stream_putl (s, as[i]);
853 else
854 {
855 if ( as[i] <= BGP_AS_MAX )
856 stream_putw(s, as[i]);
857 else
858 stream_putw(s, BGP_AS_TRANS);
859 }
paul718e3742002-12-13 20:15:29 +0000860}
861
Paul Jakmaf63f06d2011-04-08 12:44:43 +0100862static size_t
paulfe69a502005-09-10 16:55:02 +0000863assegment_header_put (struct stream *s, u_char type, int length)
864{
865 size_t lenp;
866 assert (length <= AS_SEGMENT_MAX);
867 stream_putc (s, type);
868 lenp = stream_get_endp (s);
869 stream_putc (s, length);
870 return lenp;
871}
872
873/* write aspath data to stream */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000874size_t
875aspath_put (struct stream *s, struct aspath *as, int use32bit )
paulfe69a502005-09-10 16:55:02 +0000876{
877 struct assegment *seg = as->segments;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000878 size_t bytes = 0;
paulfe69a502005-09-10 16:55:02 +0000879
880 if (!seg || seg->length == 0)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000881 return 0;
paulfe69a502005-09-10 16:55:02 +0000882
883 if (seg)
884 {
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000885 /*
886 * Hey, what do we do when we have > STREAM_WRITABLE(s) here?
887 * At the moment, we would write out a partial aspath, and our 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 */
893 while (seg && (ASSEGMENT_LEN(seg, use32bit) <= STREAM_WRITEABLE(s)))
paulfe69a502005-09-10 16:55:02 +0000894 {
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000895 struct assegment *next = seg->next;
paulfe69a502005-09-10 16:55:02 +0000896 int written = 0;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000897 int asns_packed = 0;
paulfe69a502005-09-10 16:55:02 +0000898 size_t lenp;
899
900 /* Overlength segments have to be split up */
901 while ( (seg->length - written) > AS_SEGMENT_MAX)
902 {
903 assegment_header_put (s, seg->type, AS_SEGMENT_MAX);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000904 assegment_data_put (s, seg->as, AS_SEGMENT_MAX, use32bit);
paulfe69a502005-09-10 16:55:02 +0000905 written += AS_SEGMENT_MAX;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000906 bytes += ASSEGMENT_SIZE (written, use32bit);
paulfe69a502005-09-10 16:55:02 +0000907 }
908
909 /* write the final segment, probably is also the first */
910 lenp = assegment_header_put (s, seg->type, seg->length - written);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000911 assegment_data_put (s, (seg->as + written), seg->length - written,
912 use32bit);
paulfe69a502005-09-10 16:55:02 +0000913
914 /* Sequence-type segments can be 'packed' together
915 * Case of a segment which was overlength and split up
916 * will be missed here, but that doesn't matter.
917 */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000918 while (next && ASSEGMENTS_PACKABLE (seg, next))
paulfe69a502005-09-10 16:55:02 +0000919 {
920 /* NB: We should never normally get here given we
921 * normalise aspath data when parse them. However, better
922 * safe than sorry. We potentially could call
923 * assegment_normalise here instead, but it's cheaper and
924 * easier to do it on the fly here rather than go through
925 * the segment list twice every time we write out
926 * aspath's.
927 */
928
929 /* Next segment's data can fit in this one */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000930 assegment_data_put (s, next->as, next->length, use32bit);
paulfe69a502005-09-10 16:55:02 +0000931
932 /* update the length of the segment header */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000933 stream_putc_at (s, lenp, seg->length - written + next->length);
934 asns_packed += next->length;
935
936 next = next->next;
paulfe69a502005-09-10 16:55:02 +0000937 }
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000938
939 bytes += ASSEGMENT_SIZE (seg->length - written + asns_packed,
940 use32bit);
941 seg = next;
paulfe69a502005-09-10 16:55:02 +0000942 }
943 }
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000944 return bytes;
paulfe69a502005-09-10 16:55:02 +0000945}
946
947/* This is for SNMP BGP4PATHATTRASPATHSEGMENT
948 * We have no way to manage the storage, so we use a static stream
949 * wrapper around aspath_put.
950 */
951u_char *
952aspath_snmp_pathseg (struct aspath *as, size_t *varlen)
953{
954#define SNMP_PATHSEG_MAX 1024
paul8fdc32a2006-01-16 12:01:29 +0000955
956 if (!snmp_stream)
957 snmp_stream = stream_new (SNMP_PATHSEG_MAX);
paulfe69a502005-09-10 16:55:02 +0000958 else
paul8fdc32a2006-01-16 12:01:29 +0000959 stream_reset (snmp_stream);
paulfe69a502005-09-10 16:55:02 +0000960
961 if (!as)
962 {
963 *varlen = 0;
964 return NULL;
965 }
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000966 aspath_put (snmp_stream, as, 0); /* use 16 bit for now here */
paulfe69a502005-09-10 16:55:02 +0000967
paul8fdc32a2006-01-16 12:01:29 +0000968 *varlen = stream_get_endp (snmp_stream);
969 return stream_pnt(snmp_stream);
paulfe69a502005-09-10 16:55:02 +0000970}
971
972#define min(A,B) ((A) < (B) ? (A) : (B))
973
paul94f2b392005-06-28 12:44:16 +0000974static struct assegment *
paul718e3742002-12-13 20:15:29 +0000975aspath_aggregate_as_set_add (struct aspath *aspath, struct assegment *asset,
976 as_t as)
977{
978 int i;
979
980 /* If this is first AS set member, create new as-set segment. */
981 if (asset == NULL)
982 {
paulfe69a502005-09-10 16:55:02 +0000983 asset = assegment_new (AS_SET, 1);
984 if (! aspath->segments)
985 aspath->segments = asset;
paul718e3742002-12-13 20:15:29 +0000986 else
paulfe69a502005-09-10 16:55:02 +0000987 {
988 struct assegment *seg = aspath->segments;
989 while (seg->next)
990 seg = seg->next;
991 seg->next = asset;
992 }
paul718e3742002-12-13 20:15:29 +0000993 asset->type = AS_SET;
994 asset->length = 1;
paulfe69a502005-09-10 16:55:02 +0000995 asset->as[0] = as;
paul718e3742002-12-13 20:15:29 +0000996 }
997 else
998 {
paul718e3742002-12-13 20:15:29 +0000999 /* Check this AS value already exists or not. */
1000 for (i = 0; i < asset->length; i++)
paulfe69a502005-09-10 16:55:02 +00001001 if (asset->as[i] == as)
paul718e3742002-12-13 20:15:29 +00001002 return asset;
paulfe69a502005-09-10 16:55:02 +00001003
paul718e3742002-12-13 20:15:29 +00001004 asset->length++;
paulfe69a502005-09-10 16:55:02 +00001005 asset->as = XREALLOC (MTYPE_AS_SEG_DATA, asset->as,
1006 asset->length * AS_VALUE_SIZE);
1007 asset->as[asset->length - 1] = as;
paul718e3742002-12-13 20:15:29 +00001008 }
paulfe69a502005-09-10 16:55:02 +00001009
paul718e3742002-12-13 20:15:29 +00001010
1011 return asset;
1012}
1013
1014/* Modify as1 using as2 for aggregation. */
1015struct aspath *
1016aspath_aggregate (struct aspath *as1, struct aspath *as2)
1017{
1018 int i;
1019 int minlen;
1020 int match;
paulfe69a502005-09-10 16:55:02 +00001021 int from;
1022 struct assegment *seg1 = as1->segments;
1023 struct assegment *seg2 = as2->segments;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001024 struct aspath *aspath = NULL;
paul718e3742002-12-13 20:15:29 +00001025 struct assegment *asset;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001026 struct assegment *prevseg = NULL;
paul718e3742002-12-13 20:15:29 +00001027
1028 match = 0;
1029 minlen = 0;
1030 aspath = NULL;
1031 asset = NULL;
paul718e3742002-12-13 20:15:29 +00001032
1033 /* First of all check common leading sequence. */
paulfe69a502005-09-10 16:55:02 +00001034 while (seg1 && seg2)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001035 {
paul718e3742002-12-13 20:15:29 +00001036 /* Check segment type. */
1037 if (seg1->type != seg2->type)
1038 break;
1039
1040 /* Minimum segment length. */
1041 minlen = min (seg1->length, seg2->length);
1042
1043 for (match = 0; match < minlen; match++)
paulfe69a502005-09-10 16:55:02 +00001044 if (seg1->as[match] != seg2->as[match])
paul718e3742002-12-13 20:15:29 +00001045 break;
1046
1047 if (match)
1048 {
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001049 struct assegment *seg = assegment_new (seg1->type, 0);
1050
1051 seg = assegment_append_asns (seg, seg1->as, match);
1052
paul718e3742002-12-13 20:15:29 +00001053 if (! aspath)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001054 {
1055 aspath = aspath_new ();
1056 aspath->segments = seg;
1057 }
1058 else
1059 prevseg->next = seg;
1060
1061 prevseg = seg;
paul718e3742002-12-13 20:15:29 +00001062 }
1063
1064 if (match != minlen || match != seg1->length
1065 || seg1->length != seg2->length)
1066 break;
paulfe69a502005-09-10 16:55:02 +00001067
1068 seg1 = seg1->next;
1069 seg2 = seg2->next;
paul718e3742002-12-13 20:15:29 +00001070 }
1071
1072 if (! aspath)
1073 aspath = aspath_new();
1074
1075 /* Make as-set using rest of all information. */
paulfe69a502005-09-10 16:55:02 +00001076 from = match;
1077 while (seg1)
paul718e3742002-12-13 20:15:29 +00001078 {
paulfe69a502005-09-10 16:55:02 +00001079 for (i = from; i < seg1->length; i++)
1080 asset = aspath_aggregate_as_set_add (aspath, asset, seg1->as[i]);
1081
1082 from = 0;
1083 seg1 = seg1->next;
paul718e3742002-12-13 20:15:29 +00001084 }
1085
paulfe69a502005-09-10 16:55:02 +00001086 from = match;
1087 while (seg2)
paul718e3742002-12-13 20:15:29 +00001088 {
paulfe69a502005-09-10 16:55:02 +00001089 for (i = from; i < seg2->length; i++)
1090 asset = aspath_aggregate_as_set_add (aspath, asset, seg2->as[i]);
paul718e3742002-12-13 20:15:29 +00001091
paulfe69a502005-09-10 16:55:02 +00001092 from = 0;
1093 seg2 = seg2->next;
paul718e3742002-12-13 20:15:29 +00001094 }
paulfe69a502005-09-10 16:55:02 +00001095
1096 assegment_normalise (aspath->segments);
1097 aspath_str_update (aspath);
paul718e3742002-12-13 20:15:29 +00001098 return aspath;
1099}
1100
Boian Boneva3936d02014-06-25 20:26:44 +03001101/* Modify as1 using as2 for aggregation for multipath, keeping the
1102 * AS-Path length the same, and so minimising change to the preference
1103 * of the mpath aggregrate route.
1104 */
1105struct aspath *
1106aspath_aggregate_mpath (struct aspath *as1, struct aspath *as2)
1107{
1108 int i;
1109 int minlen;
1110 int match;
1111 int from1,from2;
1112 struct assegment *seg1 = as1->segments;
1113 struct assegment *seg2 = as2->segments;
1114 struct aspath *aspath = NULL;
1115 struct assegment *asset;
1116 struct assegment *prevseg = NULL;
1117
1118 match = 0;
1119 minlen = 0;
1120 aspath = NULL;
1121 asset = NULL;
1122
1123 /* First of all check common leading sequence. */
1124 while (seg1 && seg2)
1125 {
1126 /* Check segment type. */
1127 if (seg1->type != seg2->type)
1128 break;
1129
1130 /* Minimum segment length. */
1131 minlen = min (seg1->length, seg2->length);
1132
1133 for (match = 0; match < minlen; match++)
1134 if (seg1->as[match] != seg2->as[match])
1135 break;
1136
1137 if (match)
1138 {
1139 struct assegment *seg = assegment_new (seg1->type, 0);
1140
1141 seg = assegment_append_asns (seg, seg1->as, match);
1142
1143 if (! aspath)
1144 {
1145 aspath = aspath_new ();
1146 aspath->segments = seg;
1147 }
1148 else
1149 prevseg->next = seg;
1150
1151 prevseg = seg;
1152 }
1153
1154 if (match != minlen || match != seg1->length
1155 || seg1->length != seg2->length)
1156 break;
1157
1158 seg1 = seg1->next;
1159 seg2 = seg2->next;
1160 }
1161
1162 if (! aspath)
1163 aspath = aspath_new();
1164
1165 /* Make as-set using rest of all information. */
1166 from1 = from2 = match;
1167 while (seg1 || seg2)
1168 {
1169 if (seg1)
1170 {
1171 if (seg1->type == AS_SEQUENCE)
1172 {
1173 asset = aspath_aggregate_as_set_add (aspath, asset, seg1->as[from1]);
1174 from1++;
1175 if (from1 >= seg1->length)
1176 {
1177 from1 = 0;
1178 seg1 = seg1->next;
1179 }
1180 }
1181 else
1182 {
1183 for (i = from1; i < seg1->length; i++)
1184 asset = aspath_aggregate_as_set_add (aspath, asset, seg1->as[i]);
1185
1186 from1 = 0;
1187 seg1 = seg1->next;
1188 }
1189 }
1190
1191 if (seg2)
1192 {
1193 if (seg2->type == AS_SEQUENCE)
1194 {
1195 asset = aspath_aggregate_as_set_add (aspath, asset, seg2->as[from2]);
1196 from2++;
1197 if (from2 >= seg2->length)
1198 {
1199 from2 = 0;
1200 seg2 = seg2->next;
1201 }
1202 }
1203 else
1204 {
1205 for (i = from2; i < seg2->length; i++)
1206 asset = aspath_aggregate_as_set_add (aspath, asset, seg2->as[i]);
1207
1208 from2 = 0;
1209 seg2 = seg2->next;
1210 }
1211 }
1212
1213 if (asset->length == 1)
1214 asset->type = AS_SEQUENCE;
1215 asset = NULL;
1216 }
1217
1218 assegment_normalise (aspath->segments);
1219 aspath_str_update (aspath);
1220 return aspath;
1221}
1222
paul718e3742002-12-13 20:15:29 +00001223/* When a BGP router receives an UPDATE with an MP_REACH_NLRI
1224 attribute, check the leftmost AS number in the AS_PATH attribute is
1225 or not the peer's AS number. */
1226int
1227aspath_firstas_check (struct aspath *aspath, as_t asno)
1228{
paulfe69a502005-09-10 16:55:02 +00001229 if ( (aspath == NULL) || (aspath->segments == NULL) )
paul718e3742002-12-13 20:15:29 +00001230 return 0;
paulfe69a502005-09-10 16:55:02 +00001231
1232 if (aspath->segments
1233 && (aspath->segments->type == AS_SEQUENCE)
1234 && (aspath->segments->as[0] == asno ))
paul718e3742002-12-13 20:15:29 +00001235 return 1;
1236
1237 return 0;
1238}
1239
Paul Jakma1f742f22006-08-06 15:52:11 +00001240/* AS path loop check. If aspath contains asno then return >= 1. */
paul718e3742002-12-13 20:15:29 +00001241int
1242aspath_loop_check (struct aspath *aspath, as_t asno)
1243{
paulfe69a502005-09-10 16:55:02 +00001244 struct assegment *seg;
paul718e3742002-12-13 20:15:29 +00001245 int count = 0;
1246
Paul Jakma1f742f22006-08-06 15:52:11 +00001247 if ( (aspath == NULL) || (aspath->segments == NULL) )
paul718e3742002-12-13 20:15:29 +00001248 return 0;
paulfe69a502005-09-10 16:55:02 +00001249
1250 seg = aspath->segments;
1251
1252 while (seg)
paul718e3742002-12-13 20:15:29 +00001253 {
1254 int i;
paul718e3742002-12-13 20:15:29 +00001255
paulfe69a502005-09-10 16:55:02 +00001256 for (i = 0; i < seg->length; i++)
1257 if (seg->as[i] == asno)
paul718e3742002-12-13 20:15:29 +00001258 count++;
paulfe69a502005-09-10 16:55:02 +00001259
1260 seg = seg->next;
paul718e3742002-12-13 20:15:29 +00001261 }
1262 return count;
1263}
1264
1265/* When all of AS path is private AS return 1. */
1266int
1267aspath_private_as_check (struct aspath *aspath)
1268{
paulfe69a502005-09-10 16:55:02 +00001269 struct assegment *seg;
1270
1271 if ( !(aspath && aspath->segments) )
paul718e3742002-12-13 20:15:29 +00001272 return 0;
paulfe69a502005-09-10 16:55:02 +00001273
1274 seg = aspath->segments;
paul718e3742002-12-13 20:15:29 +00001275
paulfe69a502005-09-10 16:55:02 +00001276 while (seg)
paul718e3742002-12-13 20:15:29 +00001277 {
1278 int i;
paul718e3742002-12-13 20:15:29 +00001279
paulfe69a502005-09-10 16:55:02 +00001280 for (i = 0; i < seg->length; i++)
paul718e3742002-12-13 20:15:29 +00001281 {
Daniel Waltond0aa6e82016-06-17 14:45:42 +01001282 if (!BGP_AS_IS_PRIVATE(seg->as[i]))
paul718e3742002-12-13 20:15:29 +00001283 return 0;
1284 }
paulfe69a502005-09-10 16:55:02 +00001285 seg = seg->next;
paul718e3742002-12-13 20:15:29 +00001286 }
1287 return 1;
1288}
1289
Vasilis Tsiligiannisca87e1d2009-07-20 01:28:35 +03001290/* AS path confed check. If aspath contains confed set or sequence then return 1. */
1291int
1292aspath_confed_check (struct aspath *aspath)
1293{
1294 struct assegment *seg;
1295
1296 if ( !(aspath && aspath->segments) )
1297 return 0;
1298
1299 seg = aspath->segments;
1300
1301 while (seg)
1302 {
1303 if (seg->type == AS_CONFED_SET || seg->type == AS_CONFED_SEQUENCE)
1304 return 1;
1305 seg = seg->next;
1306 }
1307 return 0;
1308}
1309
1310/* Leftmost AS path segment confed check. If leftmost AS segment is of type
1311 AS_CONFED_SEQUENCE or AS_CONFED_SET then return 1. */
1312int
1313aspath_left_confed_check (struct aspath *aspath)
1314{
1315
1316 if ( !(aspath && aspath->segments) )
1317 return 0;
1318
1319 if ( (aspath->segments->type == AS_CONFED_SEQUENCE)
1320 || (aspath->segments->type == AS_CONFED_SET) )
1321 return 1;
1322
1323 return 0;
1324}
1325
paul718e3742002-12-13 20:15:29 +00001326/* Merge as1 to as2. as2 should be uninterned aspath. */
paul94f2b392005-06-28 12:44:16 +00001327static struct aspath *
paul718e3742002-12-13 20:15:29 +00001328aspath_merge (struct aspath *as1, struct aspath *as2)
1329{
paulfe69a502005-09-10 16:55:02 +00001330 struct assegment *last, *new;
paul718e3742002-12-13 20:15:29 +00001331
1332 if (! as1 || ! as2)
1333 return NULL;
1334
paulfe69a502005-09-10 16:55:02 +00001335 last = new = assegment_dup_all (as1->segments);
1336
1337 /* find the last valid segment */
1338 while (last && last->next)
1339 last = last->next;
1340
1341 last->next = as2->segments;
1342 as2->segments = new;
1343 aspath_str_update (as2);
paul718e3742002-12-13 20:15:29 +00001344 return as2;
1345}
1346
1347/* Prepend as1 to as2. as2 should be uninterned aspath. */
1348struct aspath *
1349aspath_prepend (struct aspath *as1, struct aspath *as2)
1350{
paulfe69a502005-09-10 16:55:02 +00001351 struct assegment *seg1;
1352 struct assegment *seg2;
paul718e3742002-12-13 20:15:29 +00001353
1354 if (! as1 || ! as2)
1355 return NULL;
paulfe69a502005-09-10 16:55:02 +00001356
1357 seg1 = as1->segments;
1358 seg2 = as2->segments;
1359
1360 /* If as2 is empty, only need to dupe as1's chain onto as2 */
paul718e3742002-12-13 20:15:29 +00001361 if (seg2 == NULL)
1362 {
paulfe69a502005-09-10 16:55:02 +00001363 as2->segments = assegment_dup_all (as1->segments);
1364 aspath_str_update (as2);
paul718e3742002-12-13 20:15:29 +00001365 return as2;
1366 }
paulfe69a502005-09-10 16:55:02 +00001367
1368 /* If as1 is empty AS, no prepending to do. */
paul718e3742002-12-13 20:15:29 +00001369 if (seg1 == NULL)
1370 return as2;
paulfe69a502005-09-10 16:55:02 +00001371
1372 /* find the tail as1's segment chain. */
1373 while (seg1 && seg1->next)
1374 seg1 = seg1->next;
paul718e3742002-12-13 20:15:29 +00001375
Vasilis Tsiligiannis736d4402009-07-20 01:59:10 +03001376 /* Delete any AS_CONFED_SEQUENCE segment from as2. */
1377 if (seg1->type == AS_SEQUENCE && seg2->type == AS_CONFED_SEQUENCE)
1378 as2 = aspath_delete_confed_seq (as2);
Paul Jakma5552da82016-06-17 11:36:59 +01001379
1380 /* as2 may have been updated */
1381 seg2 = as2->segments;
1382
1383 /* as2 may be empty now due to aspath_delete_confed_seq, recheck */
1384 if (seg2 == NULL)
1385 {
1386 as2->segments = assegment_dup_all (as1->segments);
1387 aspath_str_update (as2);
1388 return as2;
1389 }
1390
paul718e3742002-12-13 20:15:29 +00001391 /* Compare last segment type of as1 and first segment type of as2. */
1392 if (seg1->type != seg2->type)
1393 return aspath_merge (as1, as2);
1394
1395 if (seg1->type == AS_SEQUENCE)
1396 {
paulfe69a502005-09-10 16:55:02 +00001397 /* We have two chains of segments, as1->segments and seg2,
1398 * and we have to attach them together, merging the attaching
1399 * segments together into one.
1400 *
1401 * 1. dupe as1->segments onto head of as2
1402 * 2. merge seg2's asns onto last segment of this new chain
1403 * 3. attach chain after seg2
1404 */
paul718e3742002-12-13 20:15:29 +00001405
paulfe69a502005-09-10 16:55:02 +00001406 /* dupe as1 onto as2's head */
1407 seg1 = as2->segments = assegment_dup_all (as1->segments);
1408
1409 /* refind the tail of as2, reusing seg1 */
1410 while (seg1 && seg1->next)
1411 seg1 = seg1->next;
1412
1413 /* merge the old head, seg2, into tail, seg1 */
1414 seg1 = assegment_append_asns (seg1, seg2->as, seg2->length);
1415
1416 /* bypass the merged seg2, and attach any chain after it to
1417 * chain descending from as2's head
1418 */
1419 seg1->next = seg2->next;
1420
1421 /* seg2 is now referenceless and useless*/
1422 assegment_free (seg2);
1423
1424 /* we've now prepended as1's segment chain to as2, merging
1425 * the inbetween AS_SEQUENCE of seg2 in the process
1426 */
1427 aspath_str_update (as2);
paul718e3742002-12-13 20:15:29 +00001428 return as2;
1429 }
1430 else
1431 {
1432 /* AS_SET merge code is needed at here. */
1433 return aspath_merge (as1, as2);
1434 }
paulfe69a502005-09-10 16:55:02 +00001435 /* XXX: Ermmm, what if as1 has multiple segments?? */
1436
paul718e3742002-12-13 20:15:29 +00001437 /* Not reached */
1438}
1439
Denis Ovsienko841f7a52008-04-10 11:47:45 +00001440/* Iterate over AS_PATH segments and wipe all occurences of the
1441 * listed AS numbers. Hence some segments may lose some or even
1442 * all data on the way, the operation is implemented as a smarter
1443 * version of aspath_dup(), which allocates memory to hold the new
1444 * data, not the original. The new AS path is returned.
1445 */
1446struct aspath *
1447aspath_filter_exclude (struct aspath * source, struct aspath * exclude_list)
1448{
1449 struct assegment * srcseg, * exclseg, * lastseg;
1450 struct aspath * newpath;
1451
1452 newpath = aspath_new();
1453 lastseg = NULL;
1454
1455 for (srcseg = source->segments; srcseg; srcseg = srcseg->next)
1456 {
1457 unsigned i, y, newlen = 0, done = 0, skip_as;
1458 struct assegment * newseg;
1459
1460 /* Find out, how much ASns are we going to pick from this segment.
1461 * We can't perform filtering right inline, because the size of
1462 * the new segment isn't known at the moment yet.
1463 */
1464 for (i = 0; i < srcseg->length; i++)
1465 {
1466 skip_as = 0;
1467 for (exclseg = exclude_list->segments; exclseg && !skip_as; exclseg = exclseg->next)
1468 for (y = 0; y < exclseg->length; y++)
1469 if (srcseg->as[i] == exclseg->as[y])
1470 {
1471 skip_as = 1;
1472 // There's no sense in testing the rest of exclusion list, bail out.
1473 break;
1474 }
1475 if (!skip_as)
1476 newlen++;
1477 }
1478 /* newlen is now the number of ASns to copy */
1479 if (!newlen)
1480 continue;
1481
1482 /* Actual copying. Allocate memory and iterate once more, performing filtering. */
1483 newseg = assegment_new (srcseg->type, newlen);
1484 for (i = 0; i < srcseg->length; i++)
1485 {
1486 skip_as = 0;
1487 for (exclseg = exclude_list->segments; exclseg && !skip_as; exclseg = exclseg->next)
1488 for (y = 0; y < exclseg->length; y++)
1489 if (srcseg->as[i] == exclseg->as[y])
1490 {
1491 skip_as = 1;
1492 break;
1493 }
1494 if (skip_as)
1495 continue;
1496 newseg->as[done++] = srcseg->as[i];
1497 }
1498 /* At his point newlen must be equal to done, and both must be positive. Append
1499 * the filtered segment to the gross result. */
1500 if (!lastseg)
1501 newpath->segments = newseg;
1502 else
1503 lastseg->next = newseg;
1504 lastseg = newseg;
1505 }
1506 aspath_str_update (newpath);
1507 /* We are happy returning even an empty AS_PATH, because the administrator
1508 * might expect this very behaviour. There's a mean to avoid this, if necessary,
1509 * by having a match rule against certain AS_PATH regexps in the route-map index.
1510 */
1511 aspath_free (source);
1512 return newpath;
1513}
1514
paul718e3742002-12-13 20:15:29 +00001515/* Add specified AS to the leftmost of aspath. */
1516static struct aspath *
Timo Teräs85c854a2014-09-30 11:31:53 +03001517aspath_add_asns (struct aspath *aspath, as_t asno, u_char type, unsigned num)
paul718e3742002-12-13 20:15:29 +00001518{
paulfe69a502005-09-10 16:55:02 +00001519 struct assegment *assegment = aspath->segments;
David Lamparter21401f32015-03-03 08:55:26 +01001520 unsigned i;
paul718e3742002-12-13 20:15:29 +00001521
Timo Teräs85c854a2014-09-30 11:31:53 +03001522 if (assegment && assegment->type == type)
paul718e3742002-12-13 20:15:29 +00001523 {
Timo Teräs85c854a2014-09-30 11:31:53 +03001524 /* extend existing segment */
1525 aspath->segments = assegment_prepend_asns (aspath->segments, asno, num);
paul718e3742002-12-13 20:15:29 +00001526 }
paulfe69a502005-09-10 16:55:02 +00001527 else
paul718e3742002-12-13 20:15:29 +00001528 {
Timo Teräs85c854a2014-09-30 11:31:53 +03001529 /* prepend with new segment */
1530 struct assegment *newsegment = assegment_new (type, num);
1531 for (i = 0; i < num; i++)
1532 newsegment->as[i] = asno;
1533
1534 /* insert potentially replacing empty segment */
1535 if (assegment && assegment->length == 0)
1536 {
1537 newsegment->next = assegment->next;
1538 assegment_free (assegment);
1539 }
1540 else
1541 newsegment->next = assegment;
paulfe69a502005-09-10 16:55:02 +00001542 aspath->segments = newsegment;
paul718e3742002-12-13 20:15:29 +00001543 }
1544
Timo Teräs85c854a2014-09-30 11:31:53 +03001545 aspath_str_update (aspath);
paul718e3742002-12-13 20:15:29 +00001546 return aspath;
1547}
1548
Timo Teräs85c854a2014-09-30 11:31:53 +03001549/* Add specified AS to the leftmost of aspath num times. */
1550struct aspath *
1551aspath_add_seq_n (struct aspath *aspath, as_t asno, unsigned num)
1552{
1553 return aspath_add_asns (aspath, asno, AS_SEQUENCE, num);
1554}
1555
paul718e3742002-12-13 20:15:29 +00001556/* Add specified AS to the leftmost of aspath. */
1557struct aspath *
1558aspath_add_seq (struct aspath *aspath, as_t asno)
1559{
Timo Teräs85c854a2014-09-30 11:31:53 +03001560 return aspath_add_asns (aspath, asno, AS_SEQUENCE, 1);
paul718e3742002-12-13 20:15:29 +00001561}
1562
1563/* Compare leftmost AS value for MED check. If as1's leftmost AS and
1564 as2's leftmost AS is same return 1. */
1565int
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +01001566aspath_cmp_left (const struct aspath *aspath1, const struct aspath *aspath2)
paul718e3742002-12-13 20:15:29 +00001567{
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +00001568 const struct assegment *seg1;
1569 const struct assegment *seg2;
paul718e3742002-12-13 20:15:29 +00001570
paulfe69a502005-09-10 16:55:02 +00001571 if (!(aspath1 && aspath2))
1572 return 0;
paul718e3742002-12-13 20:15:29 +00001573
paulfe69a502005-09-10 16:55:02 +00001574 seg1 = aspath1->segments;
1575 seg2 = aspath2->segments;
1576
1577 /* find first non-confed segments for each */
1578 while (seg1 && ((seg1->type == AS_CONFED_SEQUENCE)
1579 || (seg1->type == AS_CONFED_SET)))
1580 seg1 = seg1->next;
1581
1582 while (seg2 && ((seg2->type == AS_CONFED_SEQUENCE)
1583 || (seg2->type == AS_CONFED_SET)))
1584 seg2 = seg2->next;
paul718e3742002-12-13 20:15:29 +00001585
1586 /* Check as1's */
paulfe69a502005-09-10 16:55:02 +00001587 if (!(seg1 && seg2
1588 && (seg1->type == AS_SEQUENCE) && (seg2->type == AS_SEQUENCE)))
paul718e3742002-12-13 20:15:29 +00001589 return 0;
paulfe69a502005-09-10 16:55:02 +00001590
1591 if (seg1->as[0] == seg2->as[0])
paul718e3742002-12-13 20:15:29 +00001592 return 1;
1593
1594 return 0;
1595}
1596
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001597/* Truncate an aspath after a number of hops, and put the hops remaining
1598 * at the front of another aspath. Needed for AS4 compat.
1599 *
1600 * Returned aspath is a /new/ aspath, which should either by free'd or
1601 * interned by the caller, as desired.
1602 */
1603struct aspath *
1604aspath_reconcile_as4 ( struct aspath *aspath, struct aspath *as4path)
1605{
1606 struct assegment *seg, *newseg, *prevseg = NULL;
1607 struct aspath *newpath = NULL, *mergedpath;
1608 int hops, cpasns = 0;
1609
1610 if (!aspath)
1611 return NULL;
1612
1613 seg = aspath->segments;
1614
1615 /* CONFEDs should get reconciled too.. */
1616 hops = (aspath_count_hops (aspath) + aspath_count_confeds (aspath))
1617 - aspath_count_hops (as4path);
1618
1619 if (hops < 0)
1620 {
1621 if (BGP_DEBUG (as4, AS4))
1622 zlog_warn ("[AS4] Fewer hops in AS_PATH than NEW_AS_PATH");
1623 /* Something's gone wrong. The RFC says we should now ignore AS4_PATH,
1624 * which is daft behaviour - it contains vital loop-detection
1625 * information which must have been removed from AS_PATH.
1626 */
1627 hops = aspath_count_hops (aspath);
1628 }
1629
1630 if (!hops)
1631 return aspath_dup (as4path);
1632
1633 if ( BGP_DEBUG(as4, AS4))
1634 zlog_debug("[AS4] got AS_PATH %s and AS4_PATH %s synthesizing now",
1635 aspath->str, as4path->str);
1636
1637 while (seg && hops > 0)
1638 {
1639 switch (seg->type)
1640 {
1641 case AS_SET:
1642 case AS_CONFED_SET:
1643 hops--;
1644 cpasns = seg->length;
1645 break;
1646 case AS_CONFED_SEQUENCE:
1647 /* Should never split a confed-sequence, if hop-count
1648 * suggests we must then something's gone wrong somewhere.
1649 *
1650 * Most important goal is to preserve AS_PATHs prime function
1651 * as loop-detector, so we fudge the numbers so that the entire
1652 * confed-sequence is merged in.
1653 */
1654 if (hops < seg->length)
1655 {
1656 if (BGP_DEBUG (as4, AS4))
1657 zlog_debug ("[AS4] AS4PATHmangle: AS_CONFED_SEQUENCE falls"
1658 " across 2/4 ASN boundary somewhere, broken..");
1659 hops = seg->length;
1660 }
1661 case AS_SEQUENCE:
1662 cpasns = MIN(seg->length, hops);
1663 hops -= seg->length;
1664 }
1665
1666 assert (cpasns <= seg->length);
1667
1668 newseg = assegment_new (seg->type, 0);
1669 newseg = assegment_append_asns (newseg, seg->as, cpasns);
1670
1671 if (!newpath)
1672 {
1673 newpath = aspath_new ();
1674 newpath->segments = newseg;
1675 }
1676 else
1677 prevseg->next = newseg;
1678
1679 prevseg = newseg;
1680 seg = seg->next;
1681 }
1682
1683 /* We may be able to join some segments here, and we must
1684 * do this because... we want normalised aspaths in out hash
1685 * and we do not want to stumble in aspath_put.
1686 */
1687 mergedpath = aspath_merge (newpath, aspath_dup(as4path));
1688 aspath_free (newpath);
1689 mergedpath->segments = assegment_normalise (mergedpath->segments);
1690 aspath_str_update (mergedpath);
1691
1692 if ( BGP_DEBUG(as4, AS4))
1693 zlog_debug ("[AS4] result of synthesizing is %s",
1694 mergedpath->str);
1695
1696 return mergedpath;
1697}
1698
paul718e3742002-12-13 20:15:29 +00001699/* Compare leftmost AS value for MED check. If as1's leftmost AS and
1700 as2's leftmost AS is same return 1. (confederation as-path
1701 only). */
1702int
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +01001703aspath_cmp_left_confed (const struct aspath *aspath1, const struct aspath *aspath2)
paul718e3742002-12-13 20:15:29 +00001704{
paulfe69a502005-09-10 16:55:02 +00001705 if (! (aspath1 && aspath2) )
paul718e3742002-12-13 20:15:29 +00001706 return 0;
paulfe69a502005-09-10 16:55:02 +00001707
paulad727402005-11-23 02:47:02 +00001708 if ( !(aspath1->segments && aspath2->segments) )
1709 return 0;
1710
paulfe69a502005-09-10 16:55:02 +00001711 if ( (aspath1->segments->type != AS_CONFED_SEQUENCE)
1712 || (aspath2->segments->type != AS_CONFED_SEQUENCE) )
paul718e3742002-12-13 20:15:29 +00001713 return 0;
paulfe69a502005-09-10 16:55:02 +00001714
1715 if (aspath1->segments->as[0] == aspath2->segments->as[0])
paul718e3742002-12-13 20:15:29 +00001716 return 1;
1717
1718 return 0;
1719}
1720
paulfe69a502005-09-10 16:55:02 +00001721/* Delete all leading AS_CONFED_SEQUENCE/SET segments from aspath.
1722 * See RFC3065, 6.1 c1 */
paul718e3742002-12-13 20:15:29 +00001723struct aspath *
1724aspath_delete_confed_seq (struct aspath *aspath)
1725{
paulfe69a502005-09-10 16:55:02 +00001726 struct assegment *seg;
paul718e3742002-12-13 20:15:29 +00001727
paulfe69a502005-09-10 16:55:02 +00001728 if (!(aspath && aspath->segments))
paul718e3742002-12-13 20:15:29 +00001729 return aspath;
1730
paulfe69a502005-09-10 16:55:02 +00001731 seg = aspath->segments;
1732
1733 /* "if the first path segment of the AS_PATH is
1734 * of type AS_CONFED_SEQUENCE,"
1735 */
1736 if (aspath->segments->type != AS_CONFED_SEQUENCE)
1737 return aspath;
paul718e3742002-12-13 20:15:29 +00001738
paulfe69a502005-09-10 16:55:02 +00001739 /* "... that segment and any immediately following segments
1740 * of the type AS_CONFED_SET or AS_CONFED_SEQUENCE are removed
1741 * from the AS_PATH attribute,"
1742 */
1743 while (seg &&
1744 (seg->type == AS_CONFED_SEQUENCE || seg->type == AS_CONFED_SET))
paul718e3742002-12-13 20:15:29 +00001745 {
paulfe69a502005-09-10 16:55:02 +00001746 aspath->segments = seg->next;
1747 assegment_free (seg);
1748 seg = aspath->segments;
paul718e3742002-12-13 20:15:29 +00001749 }
paulfe69a502005-09-10 16:55:02 +00001750 aspath_str_update (aspath);
paul718e3742002-12-13 20:15:29 +00001751 return aspath;
1752}
1753
1754/* Add new AS number to the leftmost part of the aspath as
1755 AS_CONFED_SEQUENCE. */
1756struct aspath*
1757aspath_add_confed_seq (struct aspath *aspath, as_t asno)
1758{
Timo Teräs85c854a2014-09-30 11:31:53 +03001759 return aspath_add_asns (aspath, asno, AS_CONFED_SEQUENCE, 1);
paul718e3742002-12-13 20:15:29 +00001760}
1761
1762/* Add new as value to as path structure. */
paul94f2b392005-06-28 12:44:16 +00001763static void
paul718e3742002-12-13 20:15:29 +00001764aspath_as_add (struct aspath *as, as_t asno)
1765{
paulfe69a502005-09-10 16:55:02 +00001766 struct assegment *seg = as->segments;
paul718e3742002-12-13 20:15:29 +00001767
paulfe69a502005-09-10 16:55:02 +00001768 if (!seg)
1769 return;
1770
Andrew J. Schorr93c17492007-04-15 19:17:24 +00001771 /* Last segment search procedure. */
1772 while (seg->next)
1773 seg = seg->next;
1774
paulfe69a502005-09-10 16:55:02 +00001775 assegment_append_asns (seg, &asno, 1);
paul718e3742002-12-13 20:15:29 +00001776}
1777
1778/* Add new as segment to the as path. */
paul94f2b392005-06-28 12:44:16 +00001779static void
paul718e3742002-12-13 20:15:29 +00001780aspath_segment_add (struct aspath *as, int type)
1781{
paulfe69a502005-09-10 16:55:02 +00001782 struct assegment *seg = as->segments;
1783 struct assegment *new = assegment_new (type, 0);
paul718e3742002-12-13 20:15:29 +00001784
Andrew J. Schorr93c17492007-04-15 19:17:24 +00001785 if (seg)
1786 {
1787 while (seg->next)
1788 seg = seg->next;
1789 seg->next = new;
1790 }
paul718e3742002-12-13 20:15:29 +00001791 else
Andrew J. Schorr93c17492007-04-15 19:17:24 +00001792 as->segments = new;
paul718e3742002-12-13 20:15:29 +00001793}
1794
1795struct aspath *
paul94f2b392005-06-28 12:44:16 +00001796aspath_empty (void)
paul718e3742002-12-13 20:15:29 +00001797{
Paul Jakmaab005292010-11-27 22:48:34 +00001798 return aspath_parse (NULL, 0, 1); /* 32Bit ;-) */
paul718e3742002-12-13 20:15:29 +00001799}
1800
1801struct aspath *
paul94f2b392005-06-28 12:44:16 +00001802aspath_empty_get (void)
paul718e3742002-12-13 20:15:29 +00001803{
1804 struct aspath *aspath;
1805
1806 aspath = aspath_new ();
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +00001807 aspath_make_str_count (aspath);
paul718e3742002-12-13 20:15:29 +00001808 return aspath;
1809}
1810
1811unsigned long
paulfe69a502005-09-10 16:55:02 +00001812aspath_count (void)
paul718e3742002-12-13 20:15:29 +00001813{
1814 return ashash->count;
1815}
David Lamparter6b0655a2014-06-04 06:53:35 +02001816
paul718e3742002-12-13 20:15:29 +00001817/*
1818 Theoretically, one as path can have:
1819
1820 One BGP packet size should be less than 4096.
1821 One BGP attribute size should be less than 4096 - BGP header size.
1822 One BGP aspath size should be less than 4096 - BGP header size -
1823 BGP mandantry attribute size.
1824*/
1825
1826/* AS path string lexical token enum. */
1827enum as_token
1828{
1829 as_token_asval,
1830 as_token_set_start,
1831 as_token_set_end,
paulfe69a502005-09-10 16:55:02 +00001832 as_token_confed_seq_start,
1833 as_token_confed_seq_end,
1834 as_token_confed_set_start,
1835 as_token_confed_set_end,
paul718e3742002-12-13 20:15:29 +00001836 as_token_unknown
1837};
1838
1839/* Return next token and point for string parse. */
paul94f2b392005-06-28 12:44:16 +00001840static const char *
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001841aspath_gettoken (const char *buf, enum as_token *token, u_long *asno)
paul718e3742002-12-13 20:15:29 +00001842{
paulfd79ac92004-10-13 05:06:08 +00001843 const char *p = buf;
paul718e3742002-12-13 20:15:29 +00001844
paulfe69a502005-09-10 16:55:02 +00001845 /* Skip seperators (space for sequences, ',' for sets). */
1846 while (isspace ((int) *p) || *p == ',')
paul718e3742002-12-13 20:15:29 +00001847 p++;
1848
1849 /* Check the end of the string and type specify characters
1850 (e.g. {}()). */
1851 switch (*p)
1852 {
1853 case '\0':
1854 return NULL;
paul718e3742002-12-13 20:15:29 +00001855 case '{':
1856 *token = as_token_set_start;
1857 p++;
1858 return p;
paul718e3742002-12-13 20:15:29 +00001859 case '}':
1860 *token = as_token_set_end;
1861 p++;
1862 return p;
paul718e3742002-12-13 20:15:29 +00001863 case '(':
paulfe69a502005-09-10 16:55:02 +00001864 *token = as_token_confed_seq_start;
paul718e3742002-12-13 20:15:29 +00001865 p++;
1866 return p;
paul718e3742002-12-13 20:15:29 +00001867 case ')':
paulfe69a502005-09-10 16:55:02 +00001868 *token = as_token_confed_seq_end;
1869 p++;
1870 return p;
paulfe69a502005-09-10 16:55:02 +00001871 case '[':
1872 *token = as_token_confed_set_start;
1873 p++;
1874 return p;
paulfe69a502005-09-10 16:55:02 +00001875 case ']':
1876 *token = as_token_confed_set_end;
paul718e3742002-12-13 20:15:29 +00001877 p++;
1878 return p;
paul718e3742002-12-13 20:15:29 +00001879 }
1880
1881 /* Check actual AS value. */
1882 if (isdigit ((int) *p))
1883 {
Denis Ovsienko10819ec2009-06-09 15:15:33 +04001884 as_t asval;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001885
paul718e3742002-12-13 20:15:29 +00001886 *token = as_token_asval;
1887 asval = (*p - '0');
1888 p++;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001889
paul718e3742002-12-13 20:15:29 +00001890 while (isdigit ((int) *p))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001891 {
1892 asval *= 10;
1893 asval += (*p - '0');
1894 p++;
1895 }
paul718e3742002-12-13 20:15:29 +00001896 *asno = asval;
1897 return p;
1898 }
1899
1900 /* There is no match then return unknown token. */
1901 *token = as_token_unknown;
1902 return p++;
1903}
1904
1905struct aspath *
paulfd79ac92004-10-13 05:06:08 +00001906aspath_str2aspath (const char *str)
paul718e3742002-12-13 20:15:29 +00001907{
paul3fff6ff2006-02-05 17:55:35 +00001908 enum as_token token = as_token_unknown;
paul718e3742002-12-13 20:15:29 +00001909 u_short as_type;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001910 u_long asno = 0;
paul718e3742002-12-13 20:15:29 +00001911 struct aspath *aspath;
1912 int needtype;
1913
1914 aspath = aspath_new ();
1915
1916 /* We start default type as AS_SEQUENCE. */
1917 as_type = AS_SEQUENCE;
1918 needtype = 1;
1919
1920 while ((str = aspath_gettoken (str, &token, &asno)) != NULL)
1921 {
1922 switch (token)
1923 {
1924 case as_token_asval:
1925 if (needtype)
1926 {
1927 aspath_segment_add (aspath, as_type);
1928 needtype = 0;
1929 }
1930 aspath_as_add (aspath, asno);
1931 break;
1932 case as_token_set_start:
1933 as_type = AS_SET;
1934 aspath_segment_add (aspath, as_type);
1935 needtype = 0;
1936 break;
1937 case as_token_set_end:
1938 as_type = AS_SEQUENCE;
1939 needtype = 1;
1940 break;
paulfe69a502005-09-10 16:55:02 +00001941 case as_token_confed_seq_start:
paul718e3742002-12-13 20:15:29 +00001942 as_type = AS_CONFED_SEQUENCE;
1943 aspath_segment_add (aspath, as_type);
1944 needtype = 0;
1945 break;
paulfe69a502005-09-10 16:55:02 +00001946 case as_token_confed_seq_end:
1947 as_type = AS_SEQUENCE;
1948 needtype = 1;
1949 break;
1950 case as_token_confed_set_start:
1951 as_type = AS_CONFED_SET;
1952 aspath_segment_add (aspath, as_type);
1953 needtype = 0;
1954 break;
1955 case as_token_confed_set_end:
paul718e3742002-12-13 20:15:29 +00001956 as_type = AS_SEQUENCE;
1957 needtype = 1;
1958 break;
1959 case as_token_unknown:
1960 default:
paulfe69a502005-09-10 16:55:02 +00001961 aspath_free (aspath);
paul718e3742002-12-13 20:15:29 +00001962 return NULL;
paul718e3742002-12-13 20:15:29 +00001963 }
1964 }
1965
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +00001966 aspath_make_str_count (aspath);
paul718e3742002-12-13 20:15:29 +00001967
1968 return aspath;
1969}
David Lamparter6b0655a2014-06-04 06:53:35 +02001970
paul718e3742002-12-13 20:15:29 +00001971/* Make hash value by raw aspath data. */
1972unsigned int
Paul Jakma923de652007-04-29 18:25:17 +00001973aspath_key_make (void *p)
paul718e3742002-12-13 20:15:29 +00001974{
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +00001975 struct aspath *aspath = (struct aspath *) p;
paul718e3742002-12-13 20:15:29 +00001976 unsigned int key = 0;
paul718e3742002-12-13 20:15:29 +00001977
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001978 if (!aspath->str)
1979 aspath_str_update (aspath);
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +00001980
1981 key = jhash (aspath->str, aspath->str_len, 2334325);
paul718e3742002-12-13 20:15:29 +00001982
1983 return key;
1984}
1985
1986/* If two aspath have same value then return 1 else return 0 */
Josh Bailey96450fa2011-07-20 20:45:12 -07001987int
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +01001988aspath_cmp (const void *arg1, const void *arg2)
paul718e3742002-12-13 20:15:29 +00001989{
Denis Ovsienkoaea339f2009-04-30 17:16:22 +04001990 const struct assegment *seg1 = ((const struct aspath *)arg1)->segments;
1991 const struct assegment *seg2 = ((const struct aspath *)arg2)->segments;
paulfe69a502005-09-10 16:55:02 +00001992
1993 while (seg1 || seg2)
1994 {
1995 int i;
1996 if ((!seg1 && seg2) || (seg1 && !seg2))
1997 return 0;
1998 if (seg1->type != seg2->type)
1999 return 0;
2000 if (seg1->length != seg2->length)
2001 return 0;
2002 for (i = 0; i < seg1->length; i++)
2003 if (seg1->as[i] != seg2->as[i])
2004 return 0;
2005 seg1 = seg1->next;
2006 seg2 = seg2->next;
2007 }
2008 return 1;
paul718e3742002-12-13 20:15:29 +00002009}
2010
2011/* AS path hash initialize. */
2012void
paul94f2b392005-06-28 12:44:16 +00002013aspath_init (void)
paul718e3742002-12-13 20:15:29 +00002014{
Stephen Hemminger90645f52013-01-04 22:29:21 +00002015 ashash = hash_create_size (32768, aspath_key_make, aspath_cmp);
paul718e3742002-12-13 20:15:29 +00002016}
paul8fdc32a2006-01-16 12:01:29 +00002017
2018void
2019aspath_finish (void)
2020{
Lou Berger056f3762013-04-10 12:30:04 -07002021 hash_clean (ashash, (void (*)(void *))aspath_free);
paul8fdc32a2006-01-16 12:01:29 +00002022 hash_free (ashash);
Chris Caputo228da422009-07-18 05:44:03 +00002023 ashash = NULL;
paul8fdc32a2006-01-16 12:01:29 +00002024
2025 if (snmp_stream)
2026 stream_free (snmp_stream);
2027}
David Lamparter6b0655a2014-06-04 06:53:35 +02002028
paul718e3742002-12-13 20:15:29 +00002029/* return and as path value */
2030const char *
2031aspath_print (struct aspath *as)
2032{
paulfe69a502005-09-10 16:55:02 +00002033 return (as ? as->str : NULL);
paul718e3742002-12-13 20:15:29 +00002034}
2035
2036/* Printing functions */
Denis Ovsienko841f7a52008-04-10 11:47:45 +00002037/* Feed the AS_PATH to the vty; the suffix string follows it only in case
2038 * AS_PATH wasn't empty.
2039 */
paul718e3742002-12-13 20:15:29 +00002040void
Denis Ovsienko841f7a52008-04-10 11:47:45 +00002041aspath_print_vty (struct vty *vty, const char *format, struct aspath *as, const char * suffix)
paul718e3742002-12-13 20:15:29 +00002042{
Paul Jakmab2518c12006-05-12 23:48:40 +00002043 assert (format);
2044 vty_out (vty, format, as->str);
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +00002045 if (as->str_len && strlen (suffix))
Denis Ovsienko841f7a52008-04-10 11:47:45 +00002046 vty_out (vty, "%s", suffix);
paul718e3742002-12-13 20:15:29 +00002047}
2048
paul94f2b392005-06-28 12:44:16 +00002049static void
paul718e3742002-12-13 20:15:29 +00002050aspath_show_all_iterator (struct hash_backet *backet, struct vty *vty)
2051{
2052 struct aspath *as;
2053
2054 as = (struct aspath *) backet->data;
2055
David Lampartereed3c482015-03-03 08:51:53 +01002056 vty_out (vty, "[%p:%u] (%ld) ", (void *)backet, backet->key, as->refcnt);
paul718e3742002-12-13 20:15:29 +00002057 vty_out (vty, "%s%s", as->str, VTY_NEWLINE);
2058}
2059
2060/* Print all aspath and hash information. This function is used from
2061 `show ip bgp paths' command. */
2062void
2063aspath_print_all_vty (struct vty *vty)
2064{
2065 hash_iterate (ashash,
2066 (void (*) (struct hash_backet *, void *))
2067 aspath_show_all_iterator,
2068 vty);
2069}