blob: 6ab293765c73d749102ba93e20115641e2a14586 [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++)
478 if (seg->as[i] > highest
479 && (seg->as[i] < BGP_PRIVATE_AS_MIN
480 || seg->as[i] > BGP_PRIVATE_AS_MAX))
481 highest = seg->as[i];
482 seg = seg->next;
483 }
484 return highest;
485}
486
Timo Teräs85c854a2014-09-30 11:31:53 +0300487/* Return the left-most ASN in path */
488as_t
489aspath_leftmost (struct aspath *aspath)
490{
491 struct assegment *seg = aspath->segments;
492 as_t leftmost = 0;
493
494 if (seg && seg->length && seg->type == AS_SEQUENCE)
495 leftmost = seg->as[0];
496
497 return leftmost;
498}
499
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000500/* Return 1 if there are any 4-byte ASes in the path */
501unsigned int
502aspath_has_as4 (struct aspath *aspath)
503{
504 struct assegment *seg = aspath->segments;
505 unsigned int i;
506
507 while (seg)
508 {
509 for (i = 0; i < seg->length; i++)
510 if (seg->as[i] > BGP_AS_MAX)
511 return 1;
512 seg = seg->next;
513 }
514 return 0;
515}
516
paul718e3742002-12-13 20:15:29 +0000517/* Convert aspath structure to string expression. */
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000518static void
paul718e3742002-12-13 20:15:29 +0000519aspath_make_str_count (struct aspath *as)
520{
paulfe69a502005-09-10 16:55:02 +0000521 struct assegment *seg;
522 int str_size;
523 int len = 0;
hassoc9e52be2004-09-26 16:09:34 +0000524 char *str_buf;
paul718e3742002-12-13 20:15:29 +0000525
526 /* Empty aspath. */
paulfe69a502005-09-10 16:55:02 +0000527 if (!as->segments)
paul718e3742002-12-13 20:15:29 +0000528 {
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000529 as->str = XMALLOC (MTYPE_AS_STR, 1);
530 as->str[0] = '\0';
531 as->str_len = 0;
532 return;
paul718e3742002-12-13 20:15:29 +0000533 }
paulfe69a502005-09-10 16:55:02 +0000534
535 seg = as->segments;
536
Denis Ovsienko014b6702009-06-23 21:10:45 +0400537 /* ASN takes 5 to 10 chars plus seperator, see below.
538 * If there is one differing segment type, we need an additional
539 * 2 chars for segment delimiters, and the final '\0'.
540 * Hopefully this is large enough to avoid hitting the realloc
541 * code below for most common sequences.
542 *
543 * This was changed to 10 after the well-known BGP assertion, which
544 * had hit some parts of the Internet in May of 2009.
545 */
546#define ASN_STR_LEN (10 + 1)
547 str_size = MAX (assegment_count_asns (seg, 0) * ASN_STR_LEN + 2 + 1,
548 ASPATH_STR_DEFAULT_LEN);
paul718e3742002-12-13 20:15:29 +0000549 str_buf = XMALLOC (MTYPE_AS_STR, str_size);
paul718e3742002-12-13 20:15:29 +0000550
paulfe69a502005-09-10 16:55:02 +0000551 while (seg)
paul718e3742002-12-13 20:15:29 +0000552 {
553 int i;
paulfe69a502005-09-10 16:55:02 +0000554 char seperator;
paul718e3742002-12-13 20:15:29 +0000555
paulfe69a502005-09-10 16:55:02 +0000556 /* Check AS type validity. Set seperator for segment */
557 switch (seg->type)
558 {
559 case AS_SET:
560 case AS_CONFED_SET:
561 seperator = ',';
562 break;
563 case AS_SEQUENCE:
564 case AS_CONFED_SEQUENCE:
565 seperator = ' ';
566 break;
567 default:
568 XFREE (MTYPE_AS_STR, str_buf);
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000569 as->str = NULL;
570 as->str_len = 0;
571 return;
paulfe69a502005-09-10 16:55:02 +0000572 }
573
Denis Ovsienko014b6702009-06-23 21:10:45 +0400574 /* We might need to increase str_buf, particularly if path has
575 * differing segments types, our initial guesstimate above will
576 * have been wrong. Need 10 chars for ASN, a seperator each and
577 * potentially two segment delimiters, plus a space between each
578 * segment and trailing zero.
579 *
580 * This definitely didn't work with the value of 5 bytes and
581 * 32-bit ASNs.
582 */
583#define SEGMENT_STR_LEN(X) (((X)->length * ASN_STR_LEN) + 2 + 1 + 1)
584 if ( (len + SEGMENT_STR_LEN(seg)) > str_size)
585 {
586 str_size = len + SEGMENT_STR_LEN(seg);
587 str_buf = XREALLOC (MTYPE_AS_STR, str_buf, str_size);
588 }
589#undef ASN_STR_LEN
590#undef SEGMENT_STR_LEN
591
paulfe69a502005-09-10 16:55:02 +0000592 if (seg->type != AS_SEQUENCE)
Denis Ovsienko014b6702009-06-23 21:10:45 +0400593 len += snprintf (str_buf + len, str_size - len,
594 "%c",
595 aspath_delimiter_char (seg->type, AS_SEG_START));
paulfe69a502005-09-10 16:55:02 +0000596
597 /* write out the ASNs, with their seperators, bar the last one*/
598 for (i = 0; i < seg->length; i++)
599 {
600 len += snprintf (str_buf + len, str_size - len, "%u", seg->as[i]);
601
602 if (i < (seg->length - 1))
603 len += snprintf (str_buf + len, str_size - len, "%c", seperator);
604 }
605
606 if (seg->type != AS_SEQUENCE)
607 len += snprintf (str_buf + len, str_size - len, "%c",
608 aspath_delimiter_char (seg->type, AS_SEG_END));
609 if (seg->next)
610 len += snprintf (str_buf + len, str_size - len, " ");
611
612 seg = seg->next;
paul718e3742002-12-13 20:15:29 +0000613 }
paulfe69a502005-09-10 16:55:02 +0000614
615 assert (len < str_size);
616
617 str_buf[len] = '\0';
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000618 as->str = str_buf;
619 as->str_len = len;
paul718e3742002-12-13 20:15:29 +0000620
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000621 return;
paul718e3742002-12-13 20:15:29 +0000622}
623
paulfe69a502005-09-10 16:55:02 +0000624static void
625aspath_str_update (struct aspath *as)
626{
627 if (as->str)
628 XFREE (MTYPE_AS_STR, as->str);
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000629 aspath_make_str_count (as);
paulfe69a502005-09-10 16:55:02 +0000630}
631
paul718e3742002-12-13 20:15:29 +0000632/* Intern allocated AS path. */
633struct aspath *
634aspath_intern (struct aspath *aspath)
635{
636 struct aspath *find;
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000637
638 /* Assert this AS path structure is not interned and has the string
639 representation built. */
paul718e3742002-12-13 20:15:29 +0000640 assert (aspath->refcnt == 0);
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000641 assert (aspath->str);
paul718e3742002-12-13 20:15:29 +0000642
643 /* Check AS path hash. */
644 find = hash_get (ashash, aspath, hash_alloc_intern);
paul718e3742002-12-13 20:15:29 +0000645 if (find != aspath)
paulfe69a502005-09-10 16:55:02 +0000646 aspath_free (aspath);
paul718e3742002-12-13 20:15:29 +0000647
648 find->refcnt++;
649
paul718e3742002-12-13 20:15:29 +0000650 return find;
651}
652
653/* Duplicate aspath structure. Created same aspath structure but
654 reference count and AS path string is cleared. */
655struct aspath *
656aspath_dup (struct aspath *aspath)
657{
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000658 unsigned short buflen = aspath->str_len + 1;
paul718e3742002-12-13 20:15:29 +0000659 struct aspath *new;
660
paulfe69a502005-09-10 16:55:02 +0000661 new = XCALLOC (MTYPE_AS_PATH, sizeof (struct aspath));
paul718e3742002-12-13 20:15:29 +0000662
paulfe69a502005-09-10 16:55:02 +0000663 if (aspath->segments)
664 new->segments = assegment_dup_all (aspath->segments);
paul718e3742002-12-13 20:15:29 +0000665
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000666 if (!aspath->str)
667 return new;
668
669 new->str = XMALLOC (MTYPE_AS_STR, buflen);
670 new->str_len = aspath->str_len;
671
672 /* copy the string data */
673 if (aspath->str_len > 0)
674 memcpy (new->str, aspath->str, buflen);
675 else
676 new->str[0] = '\0';
paul718e3742002-12-13 20:15:29 +0000677
678 return new;
679}
680
paul94f2b392005-06-28 12:44:16 +0000681static void *
paulfe69a502005-09-10 16:55:02 +0000682aspath_hash_alloc (void *arg)
paul718e3742002-12-13 20:15:29 +0000683{
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000684 const struct aspath *aspath = arg;
685 struct aspath *new;
686
687 /* Malformed AS path value. */
688 assert (aspath->str);
689 if (! aspath->str)
690 return NULL;
paul718e3742002-12-13 20:15:29 +0000691
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000692 /* New aspath structure is needed. */
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000693 new = XMALLOC (MTYPE_AS_PATH, sizeof (struct aspath));
paul718e3742002-12-13 20:15:29 +0000694
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000695 /* Reuse segments and string representation */
696 new->refcnt = 0;
697 new->segments = aspath->segments;
698 new->str = aspath->str;
699 new->str_len = aspath->str_len;
700
701 return new;
paul718e3742002-12-13 20:15:29 +0000702}
703
Paul Jakmaab005292010-11-27 22:48:34 +0000704/* parse as-segment byte stream in struct assegment */
Paul Jakmab881c702010-11-23 16:35:42 +0000705static int
706assegments_parse (struct stream *s, size_t length,
707 struct assegment **result, int use32bit)
paulfe69a502005-09-10 16:55:02 +0000708{
709 struct assegment_header segh;
710 struct assegment *seg, *prev = NULL, *head = NULL;
Paul Jakmaab005292010-11-27 22:48:34 +0000711 size_t bytes = 0;
paulfe69a502005-09-10 16:55:02 +0000712
Paul Jakmaab005292010-11-27 22:48:34 +0000713 /* empty aspath (ie iBGP or somesuch) */
714 if (length == 0)
Paul Jakmab881c702010-11-23 16:35:42 +0000715 return 0;
paulfe69a502005-09-10 16:55:02 +0000716
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000717 if (BGP_DEBUG (as4, AS4_SEGMENT))
718 zlog_debug ("[AS4SEG] Parse aspath segment: got total byte length %lu",
719 (unsigned long) length);
Paul Jakmaab005292010-11-27 22:48:34 +0000720 /* basic checks */
721 if ((STREAM_READABLE(s) < length)
722 || (STREAM_READABLE(s) < AS_HEADER_SIZE)
723 || (length % AS16_VALUE_SIZE ))
Paul Jakmab881c702010-11-23 16:35:42 +0000724 return -1;
paulfe69a502005-09-10 16:55:02 +0000725
Paul Jakmaab005292010-11-27 22:48:34 +0000726 while (bytes < length)
paulfe69a502005-09-10 16:55:02 +0000727 {
728 int i;
Chris Hallcddb8112010-08-09 22:31:37 +0400729 size_t seg_size;
paulfe69a502005-09-10 16:55:02 +0000730
Paul Jakmaab005292010-11-27 22:48:34 +0000731 if ((length - bytes) <= AS_HEADER_SIZE)
Chris Hallcddb8112010-08-09 22:31:37 +0400732 {
Paul Jakmaab005292010-11-27 22:48:34 +0000733 if (head)
734 assegment_free_all (head);
Paul Jakmab881c702010-11-23 16:35:42 +0000735 return -1;
Paul Jakmafdbc8e72011-04-11 16:31:43 +0100736 }
737
Paul Jakmaab005292010-11-27 22:48:34 +0000738 /* softly softly, get the header first on its own */
paulfe69a502005-09-10 16:55:02 +0000739 segh.type = stream_getc (s);
740 segh.length = stream_getc (s);
741
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000742 seg_size = ASSEGMENT_SIZE(segh.length, use32bit);
743
744 if (BGP_DEBUG (as4, AS4_SEGMENT))
745 zlog_debug ("[AS4SEG] Parse aspath segment: got type %d, length %d",
746 segh.type, segh.length);
747
Paul Jakmaab005292010-11-27 22:48:34 +0000748 /* check it.. */
749 if ( ((bytes + seg_size) > length)
750 /* 1771bis 4.3b: seg length contains one or more */
751 || (segh.length == 0)
752 /* Paranoia in case someone changes type of segment length.
753 * Shift both values by 0x10 to make the comparison operate
754 * on more, than 8 bits (otherwise it's a warning, bug #564).
Denis Ovsienko2eb445e2009-12-04 17:32:54 +0300755 */
Paul Jakmaab005292010-11-27 22:48:34 +0000756 || ((sizeof segh.length > 1)
757 && (0x10 + segh.length > 0x10 + AS_SEGMENT_MAX)))
paulfe69a502005-09-10 16:55:02 +0000758 {
Paul Jakmaab005292010-11-27 22:48:34 +0000759 if (head)
paulfe69a502005-09-10 16:55:02 +0000760 assegment_free_all (head);
Paul Jakmab881c702010-11-23 16:35:42 +0000761 return -1;
paulfe69a502005-09-10 16:55:02 +0000762 }
763
Paul Jakmafdbc8e72011-04-11 16:31:43 +0100764 switch (segh.type)
765 {
766 case AS_SEQUENCE:
767 case AS_SET:
Paul Jakmafdbc8e72011-04-11 16:31:43 +0100768 case AS_CONFED_SEQUENCE:
769 case AS_CONFED_SET:
Paul Jakmaab005292010-11-27 22:48:34 +0000770 break;
771 default:
772 if (head)
773 assegment_free_all (head);
Paul Jakmab881c702010-11-23 16:35:42 +0000774 return -1;
paulfe69a502005-09-10 16:55:02 +0000775 }
Chris Hallcddb8112010-08-09 22:31:37 +0400776
paulfe69a502005-09-10 16:55:02 +0000777 /* now its safe to trust lengths */
778 seg = assegment_new (segh.type, segh.length);
779
780 if (head)
781 prev->next = seg;
782 else /* it's the first segment */
783 head = prev = seg;
784
785 for (i = 0; i < segh.length; i++)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000786 seg->as[i] = (use32bit) ? stream_getl (s) : stream_getw (s);
787
Paul Jakmaab005292010-11-27 22:48:34 +0000788 bytes += seg_size;
789
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000790 if (BGP_DEBUG (as4, AS4_SEGMENT))
Paul Jakmaab005292010-11-27 22:48:34 +0000791 zlog_debug ("[AS4SEG] Parse aspath segment: Bytes now: %lu",
792 (unsigned long) bytes);
paulfe69a502005-09-10 16:55:02 +0000793
794 prev = seg;
795 }
796
Paul Jakmab881c702010-11-23 16:35:42 +0000797 *result = assegment_normalise (head);
798 return 0;
paulfe69a502005-09-10 16:55:02 +0000799}
800
Paul Jakmaab005292010-11-27 22:48:34 +0000801/* 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
Paul Jakmab881c702010-11-23 16:35:42 +0000803 path hash then return it else make new AS path structure.
804
805 On error NULL is returned.
Chris Hallcddb8112010-08-09 22:31:37 +0400806 */
paul718e3742002-12-13 20:15:29 +0000807struct aspath *
Paul Jakmaab005292010-11-27 22:48:34 +0000808aspath_parse (struct stream *s, size_t length, int use32bit)
paul718e3742002-12-13 20:15:29 +0000809{
810 struct aspath as;
811 struct aspath *find;
812
Paul Jakmaab005292010-11-27 22:48:34 +0000813 /* If length is odd it's malformed AS path. */
814 /* Nit-picking: if (use32bit == 0) it is malformed if odd,
815 * otherwise its malformed when length is larger than 2 and (length-2)
816 * is not dividable by 4.
817 * But... this time we're lazy
818 */
819 if (length % AS16_VALUE_SIZE )
820 return NULL;
Chris Hallcddb8112010-08-09 22:31:37 +0400821
Paul Jakmaab005292010-11-27 22:48:34 +0000822 memset (&as, 0, sizeof (struct aspath));
Paul Jakmab881c702010-11-23 16:35:42 +0000823 if (assegments_parse (s, length, &as.segments, use32bit) < 0)
824 return NULL;
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000825
paul718e3742002-12-13 20:15:29 +0000826 /* If already same aspath exist then return it. */
827 find = hash_get (ashash, &as, aspath_hash_alloc);
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000828
829 /* bug! should not happen, let the daemon crash below */
830 assert (find);
831
832 /* if the aspath was already hashed free temporary memory. */
833 if (find->refcnt)
834 {
835 assegment_free_all (as.segments);
836 /* aspath_key_make() always updates the string */
837 XFREE (MTYPE_AS_STR, as.str);
838 }
839
paul718e3742002-12-13 20:15:29 +0000840 find->refcnt++;
841
842 return find;
843}
844
Paul Jakmaf63f06d2011-04-08 12:44:43 +0100845static void
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000846assegment_data_put (struct stream *s, as_t *as, int num, int use32bit)
paul718e3742002-12-13 20:15:29 +0000847{
paulfe69a502005-09-10 16:55:02 +0000848 int i;
849 assert (num <= AS_SEGMENT_MAX);
850
851 for (i = 0; i < num; i++)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000852 if ( use32bit )
853 stream_putl (s, as[i]);
854 else
855 {
856 if ( as[i] <= BGP_AS_MAX )
857 stream_putw(s, as[i]);
858 else
859 stream_putw(s, BGP_AS_TRANS);
860 }
paul718e3742002-12-13 20:15:29 +0000861}
862
Paul Jakmaf63f06d2011-04-08 12:44:43 +0100863static size_t
paulfe69a502005-09-10 16:55:02 +0000864assegment_header_put (struct stream *s, u_char type, int length)
865{
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;
872}
873
874/* write aspath data to stream */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000875size_t
876aspath_put (struct stream *s, struct aspath *as, int use32bit )
paulfe69a502005-09-10 16:55:02 +0000877{
878 struct assegment *seg = as->segments;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000879 size_t bytes = 0;
paulfe69a502005-09-10 16:55:02 +0000880
881 if (!seg || seg->length == 0)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000882 return 0;
paulfe69a502005-09-10 16:55:02 +0000883
884 if (seg)
885 {
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000886 /*
887 * Hey, what do we do when we have > STREAM_WRITABLE(s) here?
888 * At the moment, we would write out a partial aspath, and our peer
889 * will complain and drop the session :-/
890 *
891 * The general assumption here is that many things tested will
892 * never happen. And, in real live, up to now, they have not.
893 */
894 while (seg && (ASSEGMENT_LEN(seg, use32bit) <= STREAM_WRITEABLE(s)))
paulfe69a502005-09-10 16:55:02 +0000895 {
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000896 struct assegment *next = seg->next;
paulfe69a502005-09-10 16:55:02 +0000897 int written = 0;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000898 int asns_packed = 0;
paulfe69a502005-09-10 16:55:02 +0000899 size_t lenp;
900
901 /* Overlength segments have to be split up */
902 while ( (seg->length - written) > AS_SEGMENT_MAX)
903 {
904 assegment_header_put (s, seg->type, AS_SEGMENT_MAX);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000905 assegment_data_put (s, seg->as, AS_SEGMENT_MAX, use32bit);
paulfe69a502005-09-10 16:55:02 +0000906 written += AS_SEGMENT_MAX;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000907 bytes += ASSEGMENT_SIZE (written, use32bit);
paulfe69a502005-09-10 16:55:02 +0000908 }
909
910 /* write the final segment, probably is also the first */
911 lenp = assegment_header_put (s, seg->type, seg->length - written);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000912 assegment_data_put (s, (seg->as + written), seg->length - written,
913 use32bit);
paulfe69a502005-09-10 16:55:02 +0000914
915 /* Sequence-type segments can be 'packed' together
916 * Case of a segment which was overlength and split up
917 * will be missed here, but that doesn't matter.
918 */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000919 while (next && ASSEGMENTS_PACKABLE (seg, next))
paulfe69a502005-09-10 16:55:02 +0000920 {
921 /* NB: We should never normally get here given we
922 * normalise aspath data when parse them. However, better
923 * safe than sorry. We potentially could call
924 * assegment_normalise here instead, but it's cheaper and
925 * easier to do it on the fly here rather than go through
926 * the segment list twice every time we write out
927 * aspath's.
928 */
929
930 /* Next segment's data can fit in this one */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000931 assegment_data_put (s, next->as, next->length, use32bit);
paulfe69a502005-09-10 16:55:02 +0000932
933 /* update the length of the segment header */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000934 stream_putc_at (s, lenp, seg->length - written + next->length);
935 asns_packed += next->length;
936
937 next = next->next;
paulfe69a502005-09-10 16:55:02 +0000938 }
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000939
940 bytes += ASSEGMENT_SIZE (seg->length - written + asns_packed,
941 use32bit);
942 seg = next;
paulfe69a502005-09-10 16:55:02 +0000943 }
944 }
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000945 return bytes;
paulfe69a502005-09-10 16:55:02 +0000946}
947
948/* This is for SNMP BGP4PATHATTRASPATHSEGMENT
949 * We have no way to manage the storage, so we use a static stream
950 * wrapper around aspath_put.
951 */
952u_char *
953aspath_snmp_pathseg (struct aspath *as, size_t *varlen)
954{
955#define SNMP_PATHSEG_MAX 1024
paul8fdc32a2006-01-16 12:01:29 +0000956
957 if (!snmp_stream)
958 snmp_stream = stream_new (SNMP_PATHSEG_MAX);
paulfe69a502005-09-10 16:55:02 +0000959 else
paul8fdc32a2006-01-16 12:01:29 +0000960 stream_reset (snmp_stream);
paulfe69a502005-09-10 16:55:02 +0000961
962 if (!as)
963 {
964 *varlen = 0;
965 return NULL;
966 }
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000967 aspath_put (snmp_stream, as, 0); /* use 16 bit for now here */
paulfe69a502005-09-10 16:55:02 +0000968
paul8fdc32a2006-01-16 12:01:29 +0000969 *varlen = stream_get_endp (snmp_stream);
970 return stream_pnt(snmp_stream);
paulfe69a502005-09-10 16:55:02 +0000971}
972
973#define min(A,B) ((A) < (B) ? (A) : (B))
974
paul94f2b392005-06-28 12:44:16 +0000975static struct assegment *
paul718e3742002-12-13 20:15:29 +0000976aspath_aggregate_as_set_add (struct aspath *aspath, struct assegment *asset,
977 as_t as)
978{
979 int i;
980
981 /* If this is first AS set member, create new as-set segment. */
982 if (asset == NULL)
983 {
paulfe69a502005-09-10 16:55:02 +0000984 asset = assegment_new (AS_SET, 1);
985 if (! aspath->segments)
986 aspath->segments = asset;
paul718e3742002-12-13 20:15:29 +0000987 else
paulfe69a502005-09-10 16:55:02 +0000988 {
989 struct assegment *seg = aspath->segments;
990 while (seg->next)
991 seg = seg->next;
992 seg->next = asset;
993 }
paul718e3742002-12-13 20:15:29 +0000994 asset->type = AS_SET;
995 asset->length = 1;
paulfe69a502005-09-10 16:55:02 +0000996 asset->as[0] = as;
paul718e3742002-12-13 20:15:29 +0000997 }
998 else
999 {
paul718e3742002-12-13 20:15:29 +00001000 /* Check this AS value already exists or not. */
1001 for (i = 0; i < asset->length; i++)
paulfe69a502005-09-10 16:55:02 +00001002 if (asset->as[i] == as)
paul718e3742002-12-13 20:15:29 +00001003 return asset;
paulfe69a502005-09-10 16:55:02 +00001004
paul718e3742002-12-13 20:15:29 +00001005 asset->length++;
paulfe69a502005-09-10 16:55:02 +00001006 asset->as = XREALLOC (MTYPE_AS_SEG_DATA, asset->as,
1007 asset->length * AS_VALUE_SIZE);
1008 asset->as[asset->length - 1] = as;
paul718e3742002-12-13 20:15:29 +00001009 }
paulfe69a502005-09-10 16:55:02 +00001010
paul718e3742002-12-13 20:15:29 +00001011
1012 return asset;
1013}
1014
1015/* Modify as1 using as2 for aggregation. */
1016struct aspath *
1017aspath_aggregate (struct aspath *as1, struct aspath *as2)
1018{
1019 int i;
1020 int minlen;
1021 int match;
paulfe69a502005-09-10 16:55:02 +00001022 int from;
1023 struct assegment *seg1 = as1->segments;
1024 struct assegment *seg2 = as2->segments;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001025 struct aspath *aspath = NULL;
paul718e3742002-12-13 20:15:29 +00001026 struct assegment *asset;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001027 struct assegment *prevseg = NULL;
paul718e3742002-12-13 20:15:29 +00001028
1029 match = 0;
1030 minlen = 0;
1031 aspath = NULL;
1032 asset = NULL;
paul718e3742002-12-13 20:15:29 +00001033
1034 /* First of all check common leading sequence. */
paulfe69a502005-09-10 16:55:02 +00001035 while (seg1 && seg2)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001036 {
paul718e3742002-12-13 20:15:29 +00001037 /* Check segment type. */
1038 if (seg1->type != seg2->type)
1039 break;
1040
1041 /* Minimum segment length. */
1042 minlen = min (seg1->length, seg2->length);
1043
1044 for (match = 0; match < minlen; match++)
paulfe69a502005-09-10 16:55:02 +00001045 if (seg1->as[match] != seg2->as[match])
paul718e3742002-12-13 20:15:29 +00001046 break;
1047
1048 if (match)
1049 {
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001050 struct assegment *seg = assegment_new (seg1->type, 0);
1051
1052 seg = assegment_append_asns (seg, seg1->as, match);
1053
paul718e3742002-12-13 20:15:29 +00001054 if (! aspath)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001055 {
1056 aspath = aspath_new ();
1057 aspath->segments = seg;
1058 }
1059 else
1060 prevseg->next = seg;
1061
1062 prevseg = seg;
paul718e3742002-12-13 20:15:29 +00001063 }
1064
1065 if (match != minlen || match != seg1->length
1066 || seg1->length != seg2->length)
1067 break;
paulfe69a502005-09-10 16:55:02 +00001068
1069 seg1 = seg1->next;
1070 seg2 = seg2->next;
paul718e3742002-12-13 20:15:29 +00001071 }
1072
1073 if (! aspath)
1074 aspath = aspath_new();
1075
1076 /* Make as-set using rest of all information. */
paulfe69a502005-09-10 16:55:02 +00001077 from = match;
1078 while (seg1)
paul718e3742002-12-13 20:15:29 +00001079 {
paulfe69a502005-09-10 16:55:02 +00001080 for (i = from; i < seg1->length; i++)
1081 asset = aspath_aggregate_as_set_add (aspath, asset, seg1->as[i]);
1082
1083 from = 0;
1084 seg1 = seg1->next;
paul718e3742002-12-13 20:15:29 +00001085 }
1086
paulfe69a502005-09-10 16:55:02 +00001087 from = match;
1088 while (seg2)
paul718e3742002-12-13 20:15:29 +00001089 {
paulfe69a502005-09-10 16:55:02 +00001090 for (i = from; i < seg2->length; i++)
1091 asset = aspath_aggregate_as_set_add (aspath, asset, seg2->as[i]);
paul718e3742002-12-13 20:15:29 +00001092
paulfe69a502005-09-10 16:55:02 +00001093 from = 0;
1094 seg2 = seg2->next;
paul718e3742002-12-13 20:15:29 +00001095 }
paulfe69a502005-09-10 16:55:02 +00001096
1097 assegment_normalise (aspath->segments);
1098 aspath_str_update (aspath);
paul718e3742002-12-13 20:15:29 +00001099 return aspath;
1100}
1101
1102/* When a BGP router receives an UPDATE with an MP_REACH_NLRI
1103 attribute, check the leftmost AS number in the AS_PATH attribute is
1104 or not the peer's AS number. */
1105int
1106aspath_firstas_check (struct aspath *aspath, as_t asno)
1107{
paulfe69a502005-09-10 16:55:02 +00001108 if ( (aspath == NULL) || (aspath->segments == NULL) )
paul718e3742002-12-13 20:15:29 +00001109 return 0;
paulfe69a502005-09-10 16:55:02 +00001110
1111 if (aspath->segments
1112 && (aspath->segments->type == AS_SEQUENCE)
1113 && (aspath->segments->as[0] == asno ))
paul718e3742002-12-13 20:15:29 +00001114 return 1;
1115
1116 return 0;
1117}
1118
Paul Jakma1f742f22006-08-06 15:52:11 +00001119/* AS path loop check. If aspath contains asno then return >= 1. */
paul718e3742002-12-13 20:15:29 +00001120int
1121aspath_loop_check (struct aspath *aspath, as_t asno)
1122{
paulfe69a502005-09-10 16:55:02 +00001123 struct assegment *seg;
paul718e3742002-12-13 20:15:29 +00001124 int count = 0;
1125
Paul Jakma1f742f22006-08-06 15:52:11 +00001126 if ( (aspath == NULL) || (aspath->segments == NULL) )
paul718e3742002-12-13 20:15:29 +00001127 return 0;
paulfe69a502005-09-10 16:55:02 +00001128
1129 seg = aspath->segments;
1130
1131 while (seg)
paul718e3742002-12-13 20:15:29 +00001132 {
1133 int i;
paul718e3742002-12-13 20:15:29 +00001134
paulfe69a502005-09-10 16:55:02 +00001135 for (i = 0; i < seg->length; i++)
1136 if (seg->as[i] == asno)
paul718e3742002-12-13 20:15:29 +00001137 count++;
paulfe69a502005-09-10 16:55:02 +00001138
1139 seg = seg->next;
paul718e3742002-12-13 20:15:29 +00001140 }
1141 return count;
1142}
1143
1144/* When all of AS path is private AS return 1. */
1145int
1146aspath_private_as_check (struct aspath *aspath)
1147{
paulfe69a502005-09-10 16:55:02 +00001148 struct assegment *seg;
1149
1150 if ( !(aspath && aspath->segments) )
paul718e3742002-12-13 20:15:29 +00001151 return 0;
paulfe69a502005-09-10 16:55:02 +00001152
1153 seg = aspath->segments;
paul718e3742002-12-13 20:15:29 +00001154
paulfe69a502005-09-10 16:55:02 +00001155 while (seg)
paul718e3742002-12-13 20:15:29 +00001156 {
1157 int i;
paul718e3742002-12-13 20:15:29 +00001158
paulfe69a502005-09-10 16:55:02 +00001159 for (i = 0; i < seg->length; i++)
paul718e3742002-12-13 20:15:29 +00001160 {
paulfe69a502005-09-10 16:55:02 +00001161 if ( (seg->as[i] < BGP_PRIVATE_AS_MIN)
1162 || (seg->as[i] > BGP_PRIVATE_AS_MAX) )
paul718e3742002-12-13 20:15:29 +00001163 return 0;
1164 }
paulfe69a502005-09-10 16:55:02 +00001165 seg = seg->next;
paul718e3742002-12-13 20:15:29 +00001166 }
1167 return 1;
1168}
1169
Vasilis Tsiligiannisca87e1d2009-07-20 01:28:35 +03001170/* AS path confed check. If aspath contains confed set or sequence then return 1. */
1171int
1172aspath_confed_check (struct aspath *aspath)
1173{
1174 struct assegment *seg;
1175
1176 if ( !(aspath && aspath->segments) )
1177 return 0;
1178
1179 seg = aspath->segments;
1180
1181 while (seg)
1182 {
1183 if (seg->type == AS_CONFED_SET || seg->type == AS_CONFED_SEQUENCE)
1184 return 1;
1185 seg = seg->next;
1186 }
1187 return 0;
1188}
1189
1190/* Leftmost AS path segment confed check. If leftmost AS segment is of type
1191 AS_CONFED_SEQUENCE or AS_CONFED_SET then return 1. */
1192int
1193aspath_left_confed_check (struct aspath *aspath)
1194{
1195
1196 if ( !(aspath && aspath->segments) )
1197 return 0;
1198
1199 if ( (aspath->segments->type == AS_CONFED_SEQUENCE)
1200 || (aspath->segments->type == AS_CONFED_SET) )
1201 return 1;
1202
1203 return 0;
1204}
1205
paul718e3742002-12-13 20:15:29 +00001206/* Merge as1 to as2. as2 should be uninterned aspath. */
paul94f2b392005-06-28 12:44:16 +00001207static struct aspath *
paul718e3742002-12-13 20:15:29 +00001208aspath_merge (struct aspath *as1, struct aspath *as2)
1209{
paulfe69a502005-09-10 16:55:02 +00001210 struct assegment *last, *new;
paul718e3742002-12-13 20:15:29 +00001211
1212 if (! as1 || ! as2)
1213 return NULL;
1214
paulfe69a502005-09-10 16:55:02 +00001215 last = new = assegment_dup_all (as1->segments);
1216
1217 /* find the last valid segment */
1218 while (last && last->next)
1219 last = last->next;
1220
1221 last->next = as2->segments;
1222 as2->segments = new;
1223 aspath_str_update (as2);
paul718e3742002-12-13 20:15:29 +00001224 return as2;
1225}
1226
1227/* Prepend as1 to as2. as2 should be uninterned aspath. */
1228struct aspath *
1229aspath_prepend (struct aspath *as1, struct aspath *as2)
1230{
paulfe69a502005-09-10 16:55:02 +00001231 struct assegment *seg1;
1232 struct assegment *seg2;
paul718e3742002-12-13 20:15:29 +00001233
1234 if (! as1 || ! as2)
1235 return NULL;
paulfe69a502005-09-10 16:55:02 +00001236
1237 seg1 = as1->segments;
1238 seg2 = as2->segments;
1239
1240 /* If as2 is empty, only need to dupe as1's chain onto as2 */
paul718e3742002-12-13 20:15:29 +00001241 if (seg2 == NULL)
1242 {
paulfe69a502005-09-10 16:55:02 +00001243 as2->segments = assegment_dup_all (as1->segments);
1244 aspath_str_update (as2);
paul718e3742002-12-13 20:15:29 +00001245 return as2;
1246 }
paulfe69a502005-09-10 16:55:02 +00001247
1248 /* If as1 is empty AS, no prepending to do. */
paul718e3742002-12-13 20:15:29 +00001249 if (seg1 == NULL)
1250 return as2;
paulfe69a502005-09-10 16:55:02 +00001251
1252 /* find the tail as1's segment chain. */
1253 while (seg1 && seg1->next)
1254 seg1 = seg1->next;
paul718e3742002-12-13 20:15:29 +00001255
Vasilis Tsiligiannis736d4402009-07-20 01:59:10 +03001256 /* Delete any AS_CONFED_SEQUENCE segment from as2. */
1257 if (seg1->type == AS_SEQUENCE && seg2->type == AS_CONFED_SEQUENCE)
1258 as2 = aspath_delete_confed_seq (as2);
1259
paul718e3742002-12-13 20:15:29 +00001260 /* Compare last segment type of as1 and first segment type of as2. */
1261 if (seg1->type != seg2->type)
1262 return aspath_merge (as1, as2);
1263
1264 if (seg1->type == AS_SEQUENCE)
1265 {
paulfe69a502005-09-10 16:55:02 +00001266 /* We have two chains of segments, as1->segments and seg2,
1267 * and we have to attach them together, merging the attaching
1268 * segments together into one.
1269 *
1270 * 1. dupe as1->segments onto head of as2
1271 * 2. merge seg2's asns onto last segment of this new chain
1272 * 3. attach chain after seg2
1273 */
paul718e3742002-12-13 20:15:29 +00001274
paulfe69a502005-09-10 16:55:02 +00001275 /* dupe as1 onto as2's head */
1276 seg1 = as2->segments = assegment_dup_all (as1->segments);
1277
1278 /* refind the tail of as2, reusing seg1 */
1279 while (seg1 && seg1->next)
1280 seg1 = seg1->next;
1281
1282 /* merge the old head, seg2, into tail, seg1 */
1283 seg1 = assegment_append_asns (seg1, seg2->as, seg2->length);
1284
1285 /* bypass the merged seg2, and attach any chain after it to
1286 * chain descending from as2's head
1287 */
1288 seg1->next = seg2->next;
1289
1290 /* seg2 is now referenceless and useless*/
1291 assegment_free (seg2);
1292
1293 /* we've now prepended as1's segment chain to as2, merging
1294 * the inbetween AS_SEQUENCE of seg2 in the process
1295 */
1296 aspath_str_update (as2);
paul718e3742002-12-13 20:15:29 +00001297 return as2;
1298 }
1299 else
1300 {
1301 /* AS_SET merge code is needed at here. */
1302 return aspath_merge (as1, as2);
1303 }
paulfe69a502005-09-10 16:55:02 +00001304 /* XXX: Ermmm, what if as1 has multiple segments?? */
1305
paul718e3742002-12-13 20:15:29 +00001306 /* Not reached */
1307}
1308
Denis Ovsienko841f7a52008-04-10 11:47:45 +00001309/* Iterate over AS_PATH segments and wipe all occurences of the
1310 * listed AS numbers. Hence some segments may lose some or even
1311 * all data on the way, the operation is implemented as a smarter
1312 * version of aspath_dup(), which allocates memory to hold the new
1313 * data, not the original. The new AS path is returned.
1314 */
1315struct aspath *
1316aspath_filter_exclude (struct aspath * source, struct aspath * exclude_list)
1317{
1318 struct assegment * srcseg, * exclseg, * lastseg;
1319 struct aspath * newpath;
1320
1321 newpath = aspath_new();
1322 lastseg = NULL;
1323
1324 for (srcseg = source->segments; srcseg; srcseg = srcseg->next)
1325 {
1326 unsigned i, y, newlen = 0, done = 0, skip_as;
1327 struct assegment * newseg;
1328
1329 /* Find out, how much ASns are we going to pick from this segment.
1330 * We can't perform filtering right inline, because the size of
1331 * the new segment isn't known at the moment yet.
1332 */
1333 for (i = 0; i < srcseg->length; i++)
1334 {
1335 skip_as = 0;
1336 for (exclseg = exclude_list->segments; exclseg && !skip_as; exclseg = exclseg->next)
1337 for (y = 0; y < exclseg->length; y++)
1338 if (srcseg->as[i] == exclseg->as[y])
1339 {
1340 skip_as = 1;
1341 // There's no sense in testing the rest of exclusion list, bail out.
1342 break;
1343 }
1344 if (!skip_as)
1345 newlen++;
1346 }
1347 /* newlen is now the number of ASns to copy */
1348 if (!newlen)
1349 continue;
1350
1351 /* Actual copying. Allocate memory and iterate once more, performing filtering. */
1352 newseg = assegment_new (srcseg->type, newlen);
1353 for (i = 0; i < srcseg->length; i++)
1354 {
1355 skip_as = 0;
1356 for (exclseg = exclude_list->segments; exclseg && !skip_as; exclseg = exclseg->next)
1357 for (y = 0; y < exclseg->length; y++)
1358 if (srcseg->as[i] == exclseg->as[y])
1359 {
1360 skip_as = 1;
1361 break;
1362 }
1363 if (skip_as)
1364 continue;
1365 newseg->as[done++] = srcseg->as[i];
1366 }
1367 /* At his point newlen must be equal to done, and both must be positive. Append
1368 * the filtered segment to the gross result. */
1369 if (!lastseg)
1370 newpath->segments = newseg;
1371 else
1372 lastseg->next = newseg;
1373 lastseg = newseg;
1374 }
1375 aspath_str_update (newpath);
1376 /* We are happy returning even an empty AS_PATH, because the administrator
1377 * might expect this very behaviour. There's a mean to avoid this, if necessary,
1378 * by having a match rule against certain AS_PATH regexps in the route-map index.
1379 */
1380 aspath_free (source);
1381 return newpath;
1382}
1383
paul718e3742002-12-13 20:15:29 +00001384/* Add specified AS to the leftmost of aspath. */
1385static struct aspath *
Timo Teräs85c854a2014-09-30 11:31:53 +03001386aspath_add_asns (struct aspath *aspath, as_t asno, u_char type, unsigned num)
paul718e3742002-12-13 20:15:29 +00001387{
paulfe69a502005-09-10 16:55:02 +00001388 struct assegment *assegment = aspath->segments;
David Lamparter21401f32015-03-03 08:55:26 +01001389 unsigned i;
paul718e3742002-12-13 20:15:29 +00001390
Timo Teräs85c854a2014-09-30 11:31:53 +03001391 if (assegment && assegment->type == type)
paul718e3742002-12-13 20:15:29 +00001392 {
Timo Teräs85c854a2014-09-30 11:31:53 +03001393 /* extend existing segment */
1394 aspath->segments = assegment_prepend_asns (aspath->segments, asno, num);
paul718e3742002-12-13 20:15:29 +00001395 }
paulfe69a502005-09-10 16:55:02 +00001396 else
paul718e3742002-12-13 20:15:29 +00001397 {
Timo Teräs85c854a2014-09-30 11:31:53 +03001398 /* prepend with new segment */
1399 struct assegment *newsegment = assegment_new (type, num);
1400 for (i = 0; i < num; i++)
1401 newsegment->as[i] = asno;
1402
1403 /* insert potentially replacing empty segment */
1404 if (assegment && assegment->length == 0)
1405 {
1406 newsegment->next = assegment->next;
1407 assegment_free (assegment);
1408 }
1409 else
1410 newsegment->next = assegment;
paulfe69a502005-09-10 16:55:02 +00001411 aspath->segments = newsegment;
paul718e3742002-12-13 20:15:29 +00001412 }
1413
Timo Teräs85c854a2014-09-30 11:31:53 +03001414 aspath_str_update (aspath);
paul718e3742002-12-13 20:15:29 +00001415 return aspath;
1416}
1417
Timo Teräs85c854a2014-09-30 11:31:53 +03001418/* Add specified AS to the leftmost of aspath num times. */
1419struct aspath *
1420aspath_add_seq_n (struct aspath *aspath, as_t asno, unsigned num)
1421{
1422 return aspath_add_asns (aspath, asno, AS_SEQUENCE, num);
1423}
1424
paul718e3742002-12-13 20:15:29 +00001425/* Add specified AS to the leftmost of aspath. */
1426struct aspath *
1427aspath_add_seq (struct aspath *aspath, as_t asno)
1428{
Timo Teräs85c854a2014-09-30 11:31:53 +03001429 return aspath_add_asns (aspath, asno, AS_SEQUENCE, 1);
paul718e3742002-12-13 20:15:29 +00001430}
1431
1432/* Compare leftmost AS value for MED check. If as1's leftmost AS and
1433 as2's leftmost AS is same return 1. */
1434int
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +01001435aspath_cmp_left (const struct aspath *aspath1, const struct aspath *aspath2)
paul718e3742002-12-13 20:15:29 +00001436{
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +00001437 const struct assegment *seg1;
1438 const struct assegment *seg2;
paul718e3742002-12-13 20:15:29 +00001439
paulfe69a502005-09-10 16:55:02 +00001440 if (!(aspath1 && aspath2))
1441 return 0;
paul718e3742002-12-13 20:15:29 +00001442
paulfe69a502005-09-10 16:55:02 +00001443 seg1 = aspath1->segments;
1444 seg2 = aspath2->segments;
1445
1446 /* find first non-confed segments for each */
1447 while (seg1 && ((seg1->type == AS_CONFED_SEQUENCE)
1448 || (seg1->type == AS_CONFED_SET)))
1449 seg1 = seg1->next;
1450
1451 while (seg2 && ((seg2->type == AS_CONFED_SEQUENCE)
1452 || (seg2->type == AS_CONFED_SET)))
1453 seg2 = seg2->next;
paul718e3742002-12-13 20:15:29 +00001454
1455 /* Check as1's */
paulfe69a502005-09-10 16:55:02 +00001456 if (!(seg1 && seg2
1457 && (seg1->type == AS_SEQUENCE) && (seg2->type == AS_SEQUENCE)))
paul718e3742002-12-13 20:15:29 +00001458 return 0;
paulfe69a502005-09-10 16:55:02 +00001459
1460 if (seg1->as[0] == seg2->as[0])
paul718e3742002-12-13 20:15:29 +00001461 return 1;
1462
1463 return 0;
1464}
1465
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001466/* Truncate an aspath after a number of hops, and put the hops remaining
1467 * at the front of another aspath. Needed for AS4 compat.
1468 *
1469 * Returned aspath is a /new/ aspath, which should either by free'd or
1470 * interned by the caller, as desired.
1471 */
1472struct aspath *
1473aspath_reconcile_as4 ( struct aspath *aspath, struct aspath *as4path)
1474{
1475 struct assegment *seg, *newseg, *prevseg = NULL;
1476 struct aspath *newpath = NULL, *mergedpath;
1477 int hops, cpasns = 0;
1478
1479 if (!aspath)
1480 return NULL;
1481
1482 seg = aspath->segments;
1483
1484 /* CONFEDs should get reconciled too.. */
1485 hops = (aspath_count_hops (aspath) + aspath_count_confeds (aspath))
1486 - aspath_count_hops (as4path);
1487
1488 if (hops < 0)
1489 {
1490 if (BGP_DEBUG (as4, AS4))
1491 zlog_warn ("[AS4] Fewer hops in AS_PATH than NEW_AS_PATH");
1492 /* Something's gone wrong. The RFC says we should now ignore AS4_PATH,
1493 * which is daft behaviour - it contains vital loop-detection
1494 * information which must have been removed from AS_PATH.
1495 */
1496 hops = aspath_count_hops (aspath);
1497 }
1498
1499 if (!hops)
1500 return aspath_dup (as4path);
1501
1502 if ( BGP_DEBUG(as4, AS4))
1503 zlog_debug("[AS4] got AS_PATH %s and AS4_PATH %s synthesizing now",
1504 aspath->str, as4path->str);
1505
1506 while (seg && hops > 0)
1507 {
1508 switch (seg->type)
1509 {
1510 case AS_SET:
1511 case AS_CONFED_SET:
1512 hops--;
1513 cpasns = seg->length;
1514 break;
1515 case AS_CONFED_SEQUENCE:
1516 /* Should never split a confed-sequence, if hop-count
1517 * suggests we must then something's gone wrong somewhere.
1518 *
1519 * Most important goal is to preserve AS_PATHs prime function
1520 * as loop-detector, so we fudge the numbers so that the entire
1521 * confed-sequence is merged in.
1522 */
1523 if (hops < seg->length)
1524 {
1525 if (BGP_DEBUG (as4, AS4))
1526 zlog_debug ("[AS4] AS4PATHmangle: AS_CONFED_SEQUENCE falls"
1527 " across 2/4 ASN boundary somewhere, broken..");
1528 hops = seg->length;
1529 }
1530 case AS_SEQUENCE:
1531 cpasns = MIN(seg->length, hops);
1532 hops -= seg->length;
1533 }
1534
1535 assert (cpasns <= seg->length);
1536
1537 newseg = assegment_new (seg->type, 0);
1538 newseg = assegment_append_asns (newseg, seg->as, cpasns);
1539
1540 if (!newpath)
1541 {
1542 newpath = aspath_new ();
1543 newpath->segments = newseg;
1544 }
1545 else
1546 prevseg->next = newseg;
1547
1548 prevseg = newseg;
1549 seg = seg->next;
1550 }
1551
1552 /* We may be able to join some segments here, and we must
1553 * do this because... we want normalised aspaths in out hash
1554 * and we do not want to stumble in aspath_put.
1555 */
1556 mergedpath = aspath_merge (newpath, aspath_dup(as4path));
1557 aspath_free (newpath);
1558 mergedpath->segments = assegment_normalise (mergedpath->segments);
1559 aspath_str_update (mergedpath);
1560
1561 if ( BGP_DEBUG(as4, AS4))
1562 zlog_debug ("[AS4] result of synthesizing is %s",
1563 mergedpath->str);
1564
1565 return mergedpath;
1566}
1567
paul718e3742002-12-13 20:15:29 +00001568/* Compare leftmost AS value for MED check. If as1's leftmost AS and
1569 as2's leftmost AS is same return 1. (confederation as-path
1570 only). */
1571int
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +01001572aspath_cmp_left_confed (const struct aspath *aspath1, const struct aspath *aspath2)
paul718e3742002-12-13 20:15:29 +00001573{
paulfe69a502005-09-10 16:55:02 +00001574 if (! (aspath1 && aspath2) )
paul718e3742002-12-13 20:15:29 +00001575 return 0;
paulfe69a502005-09-10 16:55:02 +00001576
paulad727402005-11-23 02:47:02 +00001577 if ( !(aspath1->segments && aspath2->segments) )
1578 return 0;
1579
paulfe69a502005-09-10 16:55:02 +00001580 if ( (aspath1->segments->type != AS_CONFED_SEQUENCE)
1581 || (aspath2->segments->type != AS_CONFED_SEQUENCE) )
paul718e3742002-12-13 20:15:29 +00001582 return 0;
paulfe69a502005-09-10 16:55:02 +00001583
1584 if (aspath1->segments->as[0] == aspath2->segments->as[0])
paul718e3742002-12-13 20:15:29 +00001585 return 1;
1586
1587 return 0;
1588}
1589
paulfe69a502005-09-10 16:55:02 +00001590/* Delete all leading AS_CONFED_SEQUENCE/SET segments from aspath.
1591 * See RFC3065, 6.1 c1 */
paul718e3742002-12-13 20:15:29 +00001592struct aspath *
1593aspath_delete_confed_seq (struct aspath *aspath)
1594{
paulfe69a502005-09-10 16:55:02 +00001595 struct assegment *seg;
paul718e3742002-12-13 20:15:29 +00001596
paulfe69a502005-09-10 16:55:02 +00001597 if (!(aspath && aspath->segments))
paul718e3742002-12-13 20:15:29 +00001598 return aspath;
1599
paulfe69a502005-09-10 16:55:02 +00001600 seg = aspath->segments;
1601
1602 /* "if the first path segment of the AS_PATH is
1603 * of type AS_CONFED_SEQUENCE,"
1604 */
1605 if (aspath->segments->type != AS_CONFED_SEQUENCE)
1606 return aspath;
paul718e3742002-12-13 20:15:29 +00001607
paulfe69a502005-09-10 16:55:02 +00001608 /* "... that segment and any immediately following segments
1609 * of the type AS_CONFED_SET or AS_CONFED_SEQUENCE are removed
1610 * from the AS_PATH attribute,"
1611 */
1612 while (seg &&
1613 (seg->type == AS_CONFED_SEQUENCE || seg->type == AS_CONFED_SET))
paul718e3742002-12-13 20:15:29 +00001614 {
paulfe69a502005-09-10 16:55:02 +00001615 aspath->segments = seg->next;
1616 assegment_free (seg);
1617 seg = aspath->segments;
paul718e3742002-12-13 20:15:29 +00001618 }
paulfe69a502005-09-10 16:55:02 +00001619 aspath_str_update (aspath);
paul718e3742002-12-13 20:15:29 +00001620 return aspath;
1621}
1622
1623/* Add new AS number to the leftmost part of the aspath as
1624 AS_CONFED_SEQUENCE. */
1625struct aspath*
1626aspath_add_confed_seq (struct aspath *aspath, as_t asno)
1627{
Timo Teräs85c854a2014-09-30 11:31:53 +03001628 return aspath_add_asns (aspath, asno, AS_CONFED_SEQUENCE, 1);
paul718e3742002-12-13 20:15:29 +00001629}
1630
1631/* Add new as value to as path structure. */
paul94f2b392005-06-28 12:44:16 +00001632static void
paul718e3742002-12-13 20:15:29 +00001633aspath_as_add (struct aspath *as, as_t asno)
1634{
paulfe69a502005-09-10 16:55:02 +00001635 struct assegment *seg = as->segments;
paul718e3742002-12-13 20:15:29 +00001636
paulfe69a502005-09-10 16:55:02 +00001637 if (!seg)
1638 return;
1639
Andrew J. Schorr93c17492007-04-15 19:17:24 +00001640 /* Last segment search procedure. */
1641 while (seg->next)
1642 seg = seg->next;
1643
paulfe69a502005-09-10 16:55:02 +00001644 assegment_append_asns (seg, &asno, 1);
paul718e3742002-12-13 20:15:29 +00001645}
1646
1647/* Add new as segment to the as path. */
paul94f2b392005-06-28 12:44:16 +00001648static void
paul718e3742002-12-13 20:15:29 +00001649aspath_segment_add (struct aspath *as, int type)
1650{
paulfe69a502005-09-10 16:55:02 +00001651 struct assegment *seg = as->segments;
1652 struct assegment *new = assegment_new (type, 0);
paul718e3742002-12-13 20:15:29 +00001653
Andrew J. Schorr93c17492007-04-15 19:17:24 +00001654 if (seg)
1655 {
1656 while (seg->next)
1657 seg = seg->next;
1658 seg->next = new;
1659 }
paul718e3742002-12-13 20:15:29 +00001660 else
Andrew J. Schorr93c17492007-04-15 19:17:24 +00001661 as->segments = new;
paul718e3742002-12-13 20:15:29 +00001662}
1663
1664struct aspath *
paul94f2b392005-06-28 12:44:16 +00001665aspath_empty (void)
paul718e3742002-12-13 20:15:29 +00001666{
Paul Jakmaab005292010-11-27 22:48:34 +00001667 return aspath_parse (NULL, 0, 1); /* 32Bit ;-) */
paul718e3742002-12-13 20:15:29 +00001668}
1669
1670struct aspath *
paul94f2b392005-06-28 12:44:16 +00001671aspath_empty_get (void)
paul718e3742002-12-13 20:15:29 +00001672{
1673 struct aspath *aspath;
1674
1675 aspath = aspath_new ();
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +00001676 aspath_make_str_count (aspath);
paul718e3742002-12-13 20:15:29 +00001677 return aspath;
1678}
1679
1680unsigned long
paulfe69a502005-09-10 16:55:02 +00001681aspath_count (void)
paul718e3742002-12-13 20:15:29 +00001682{
1683 return ashash->count;
1684}
David Lamparter6b0655a2014-06-04 06:53:35 +02001685
paul718e3742002-12-13 20:15:29 +00001686/*
1687 Theoretically, one as path can have:
1688
1689 One BGP packet size should be less than 4096.
1690 One BGP attribute size should be less than 4096 - BGP header size.
1691 One BGP aspath size should be less than 4096 - BGP header size -
1692 BGP mandantry attribute size.
1693*/
1694
1695/* AS path string lexical token enum. */
1696enum as_token
1697{
1698 as_token_asval,
1699 as_token_set_start,
1700 as_token_set_end,
paulfe69a502005-09-10 16:55:02 +00001701 as_token_confed_seq_start,
1702 as_token_confed_seq_end,
1703 as_token_confed_set_start,
1704 as_token_confed_set_end,
paul718e3742002-12-13 20:15:29 +00001705 as_token_unknown
1706};
1707
1708/* Return next token and point for string parse. */
paul94f2b392005-06-28 12:44:16 +00001709static const char *
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001710aspath_gettoken (const char *buf, enum as_token *token, u_long *asno)
paul718e3742002-12-13 20:15:29 +00001711{
paulfd79ac92004-10-13 05:06:08 +00001712 const char *p = buf;
paul718e3742002-12-13 20:15:29 +00001713
paulfe69a502005-09-10 16:55:02 +00001714 /* Skip seperators (space for sequences, ',' for sets). */
1715 while (isspace ((int) *p) || *p == ',')
paul718e3742002-12-13 20:15:29 +00001716 p++;
1717
1718 /* Check the end of the string and type specify characters
1719 (e.g. {}()). */
1720 switch (*p)
1721 {
1722 case '\0':
1723 return NULL;
paul718e3742002-12-13 20:15:29 +00001724 case '{':
1725 *token = as_token_set_start;
1726 p++;
1727 return p;
paul718e3742002-12-13 20:15:29 +00001728 case '}':
1729 *token = as_token_set_end;
1730 p++;
1731 return p;
paul718e3742002-12-13 20:15:29 +00001732 case '(':
paulfe69a502005-09-10 16:55:02 +00001733 *token = as_token_confed_seq_start;
paul718e3742002-12-13 20:15:29 +00001734 p++;
1735 return p;
paul718e3742002-12-13 20:15:29 +00001736 case ')':
paulfe69a502005-09-10 16:55:02 +00001737 *token = as_token_confed_seq_end;
1738 p++;
1739 return p;
paulfe69a502005-09-10 16:55:02 +00001740 case '[':
1741 *token = as_token_confed_set_start;
1742 p++;
1743 return p;
paulfe69a502005-09-10 16:55:02 +00001744 case ']':
1745 *token = as_token_confed_set_end;
paul718e3742002-12-13 20:15:29 +00001746 p++;
1747 return p;
paul718e3742002-12-13 20:15:29 +00001748 }
1749
1750 /* Check actual AS value. */
1751 if (isdigit ((int) *p))
1752 {
Denis Ovsienko10819ec2009-06-09 15:15:33 +04001753 as_t asval;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001754
paul718e3742002-12-13 20:15:29 +00001755 *token = as_token_asval;
1756 asval = (*p - '0');
1757 p++;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001758
paul718e3742002-12-13 20:15:29 +00001759 while (isdigit ((int) *p))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001760 {
1761 asval *= 10;
1762 asval += (*p - '0');
1763 p++;
1764 }
paul718e3742002-12-13 20:15:29 +00001765 *asno = asval;
1766 return p;
1767 }
1768
1769 /* There is no match then return unknown token. */
1770 *token = as_token_unknown;
1771 return p++;
1772}
1773
1774struct aspath *
paulfd79ac92004-10-13 05:06:08 +00001775aspath_str2aspath (const char *str)
paul718e3742002-12-13 20:15:29 +00001776{
paul3fff6ff2006-02-05 17:55:35 +00001777 enum as_token token = as_token_unknown;
paul718e3742002-12-13 20:15:29 +00001778 u_short as_type;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001779 u_long asno = 0;
paul718e3742002-12-13 20:15:29 +00001780 struct aspath *aspath;
1781 int needtype;
1782
1783 aspath = aspath_new ();
1784
1785 /* We start default type as AS_SEQUENCE. */
1786 as_type = AS_SEQUENCE;
1787 needtype = 1;
1788
1789 while ((str = aspath_gettoken (str, &token, &asno)) != NULL)
1790 {
1791 switch (token)
1792 {
1793 case as_token_asval:
1794 if (needtype)
1795 {
1796 aspath_segment_add (aspath, as_type);
1797 needtype = 0;
1798 }
1799 aspath_as_add (aspath, asno);
1800 break;
1801 case as_token_set_start:
1802 as_type = AS_SET;
1803 aspath_segment_add (aspath, as_type);
1804 needtype = 0;
1805 break;
1806 case as_token_set_end:
1807 as_type = AS_SEQUENCE;
1808 needtype = 1;
1809 break;
paulfe69a502005-09-10 16:55:02 +00001810 case as_token_confed_seq_start:
paul718e3742002-12-13 20:15:29 +00001811 as_type = AS_CONFED_SEQUENCE;
1812 aspath_segment_add (aspath, as_type);
1813 needtype = 0;
1814 break;
paulfe69a502005-09-10 16:55:02 +00001815 case as_token_confed_seq_end:
1816 as_type = AS_SEQUENCE;
1817 needtype = 1;
1818 break;
1819 case as_token_confed_set_start:
1820 as_type = AS_CONFED_SET;
1821 aspath_segment_add (aspath, as_type);
1822 needtype = 0;
1823 break;
1824 case as_token_confed_set_end:
paul718e3742002-12-13 20:15:29 +00001825 as_type = AS_SEQUENCE;
1826 needtype = 1;
1827 break;
1828 case as_token_unknown:
1829 default:
paulfe69a502005-09-10 16:55:02 +00001830 aspath_free (aspath);
paul718e3742002-12-13 20:15:29 +00001831 return NULL;
paul718e3742002-12-13 20:15:29 +00001832 }
1833 }
1834
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +00001835 aspath_make_str_count (aspath);
paul718e3742002-12-13 20:15:29 +00001836
1837 return aspath;
1838}
David Lamparter6b0655a2014-06-04 06:53:35 +02001839
paul718e3742002-12-13 20:15:29 +00001840/* Make hash value by raw aspath data. */
1841unsigned int
Paul Jakma923de652007-04-29 18:25:17 +00001842aspath_key_make (void *p)
paul718e3742002-12-13 20:15:29 +00001843{
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +00001844 struct aspath *aspath = (struct aspath *) p;
paul718e3742002-12-13 20:15:29 +00001845 unsigned int key = 0;
paul718e3742002-12-13 20:15:29 +00001846
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001847 if (!aspath->str)
1848 aspath_str_update (aspath);
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +00001849
1850 key = jhash (aspath->str, aspath->str_len, 2334325);
paul718e3742002-12-13 20:15:29 +00001851
1852 return key;
1853}
1854
1855/* If two aspath have same value then return 1 else return 0 */
Josh Bailey96450fa2011-07-20 20:45:12 -07001856int
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +01001857aspath_cmp (const void *arg1, const void *arg2)
paul718e3742002-12-13 20:15:29 +00001858{
Denis Ovsienkoaea339f2009-04-30 17:16:22 +04001859 const struct assegment *seg1 = ((const struct aspath *)arg1)->segments;
1860 const struct assegment *seg2 = ((const struct aspath *)arg2)->segments;
paulfe69a502005-09-10 16:55:02 +00001861
1862 while (seg1 || seg2)
1863 {
1864 int i;
1865 if ((!seg1 && seg2) || (seg1 && !seg2))
1866 return 0;
1867 if (seg1->type != seg2->type)
1868 return 0;
1869 if (seg1->length != seg2->length)
1870 return 0;
1871 for (i = 0; i < seg1->length; i++)
1872 if (seg1->as[i] != seg2->as[i])
1873 return 0;
1874 seg1 = seg1->next;
1875 seg2 = seg2->next;
1876 }
1877 return 1;
paul718e3742002-12-13 20:15:29 +00001878}
1879
1880/* AS path hash initialize. */
1881void
paul94f2b392005-06-28 12:44:16 +00001882aspath_init (void)
paul718e3742002-12-13 20:15:29 +00001883{
Stephen Hemminger90645f52013-01-04 22:29:21 +00001884 ashash = hash_create_size (32768, aspath_key_make, aspath_cmp);
paul718e3742002-12-13 20:15:29 +00001885}
paul8fdc32a2006-01-16 12:01:29 +00001886
1887void
1888aspath_finish (void)
1889{
Lou Berger056f3762013-04-10 12:30:04 -07001890 hash_clean (ashash, (void (*)(void *))aspath_free);
paul8fdc32a2006-01-16 12:01:29 +00001891 hash_free (ashash);
Chris Caputo228da422009-07-18 05:44:03 +00001892 ashash = NULL;
paul8fdc32a2006-01-16 12:01:29 +00001893
1894 if (snmp_stream)
1895 stream_free (snmp_stream);
1896}
David Lamparter6b0655a2014-06-04 06:53:35 +02001897
paul718e3742002-12-13 20:15:29 +00001898/* return and as path value */
1899const char *
1900aspath_print (struct aspath *as)
1901{
paulfe69a502005-09-10 16:55:02 +00001902 return (as ? as->str : NULL);
paul718e3742002-12-13 20:15:29 +00001903}
1904
1905/* Printing functions */
Denis Ovsienko841f7a52008-04-10 11:47:45 +00001906/* Feed the AS_PATH to the vty; the suffix string follows it only in case
1907 * AS_PATH wasn't empty.
1908 */
paul718e3742002-12-13 20:15:29 +00001909void
Denis Ovsienko841f7a52008-04-10 11:47:45 +00001910aspath_print_vty (struct vty *vty, const char *format, struct aspath *as, const char * suffix)
paul718e3742002-12-13 20:15:29 +00001911{
Paul Jakmab2518c12006-05-12 23:48:40 +00001912 assert (format);
1913 vty_out (vty, format, as->str);
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +00001914 if (as->str_len && strlen (suffix))
Denis Ovsienko841f7a52008-04-10 11:47:45 +00001915 vty_out (vty, "%s", suffix);
paul718e3742002-12-13 20:15:29 +00001916}
1917
paul94f2b392005-06-28 12:44:16 +00001918static void
paul718e3742002-12-13 20:15:29 +00001919aspath_show_all_iterator (struct hash_backet *backet, struct vty *vty)
1920{
1921 struct aspath *as;
1922
1923 as = (struct aspath *) backet->data;
1924
David Lampartereed3c482015-03-03 08:51:53 +01001925 vty_out (vty, "[%p:%u] (%ld) ", (void *)backet, backet->key, as->refcnt);
paul718e3742002-12-13 20:15:29 +00001926 vty_out (vty, "%s%s", as->str, VTY_NEWLINE);
1927}
1928
1929/* Print all aspath and hash information. This function is used from
1930 `show ip bgp paths' command. */
1931void
1932aspath_print_all_vty (struct vty *vty)
1933{
1934 hash_iterate (ashash,
1935 (void (*) (struct hash_backet *, void *))
1936 aspath_show_all_iterator,
1937 vty);
1938}