blob: c37a8897f2e84c9f1e30e511983ab30df3b46e5a [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"
paul718e3742002-12-13 20:15:29 +000032
33#include "bgpd/bgpd.h"
34#include "bgpd/bgp_aspath.h"
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000035#include "bgpd/bgp_debug.h"
36#include "bgpd/bgp_attr.h"
paul718e3742002-12-13 20:15:29 +000037
38/* Attr. Flags and Attr. Type Code. */
39#define AS_HEADER_SIZE 2
40
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000041/* Now FOUR octets are used for AS value. */
paul718e3742002-12-13 20:15:29 +000042#define AS_VALUE_SIZE sizeof (as_t)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000043/* This is the old one */
44#define AS16_VALUE_SIZE sizeof (as16_t)
paul718e3742002-12-13 20:15:29 +000045
paulfe69a502005-09-10 16:55:02 +000046/* Maximum protocol segment length value */
47#define AS_SEGMENT_MAX 255
paul718e3742002-12-13 20:15:29 +000048
paulfe69a502005-09-10 16:55:02 +000049/* The following length and size macros relate specifically to Quagga's
50 * internal representation of AS-Segments, not per se to the on-wire
51 * sizes and lengths. At present (200508) they sort of match, however
52 * the ONLY functions which should now about the on-wire syntax are
53 * aspath_put, assegment_put and assegment_parse.
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000054 *
55 * aspath_put returns bytes written, the only definitive record of
56 * size of wire-format attribute..
paulfe69a502005-09-10 16:55:02 +000057 */
58
59/* Calculated size in bytes of ASN segment data to hold N ASN's */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000060#define ASSEGMENT_DATA_SIZE(N,S) \
61 ((N) * ( (S) ? AS_VALUE_SIZE : AS16_VALUE_SIZE) )
paulfe69a502005-09-10 16:55:02 +000062
63/* Calculated size of segment struct to hold N ASN's */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000064#define ASSEGMENT_SIZE(N,S) (AS_HEADER_SIZE + ASSEGMENT_DATA_SIZE (N,S))
paulfe69a502005-09-10 16:55:02 +000065
66/* AS segment octet length. */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +000067#define ASSEGMENT_LEN(X,S) ASSEGMENT_SIZE((X)->length,S)
paulfe69a502005-09-10 16:55:02 +000068
69/* AS_SEQUENCE segments can be packed together */
70/* Can the types of X and Y be considered for packing? */
71#define ASSEGMENT_TYPES_PACKABLE(X,Y) \
72 ( ((X)->type == (Y)->type) \
73 && ((X)->type == AS_SEQUENCE))
74/* Types and length of X,Y suitable for packing? */
75#define ASSEGMENTS_PACKABLE(X,Y) \
76 ( ASSEGMENT_TYPES_PACKABLE( (X), (Y)) \
77 && ( ((X)->length + (Y)->length) <= AS_SEGMENT_MAX ) )
78
79/* As segment header - the on-wire representation
80 * NOT the internal representation!
81 */
82struct assegment_header
paul718e3742002-12-13 20:15:29 +000083{
84 u_char type;
85 u_char length;
paul718e3742002-12-13 20:15:29 +000086};
87
88/* Hash for aspath. This is the top level structure of AS path. */
Stephen Hemmingerda88ea82009-12-17 13:14:28 +030089static struct hash *ashash;
paul8fdc32a2006-01-16 12:01:29 +000090
91/* Stream for SNMP. See aspath_snmp_pathseg */
92static struct stream *snmp_stream;
paul718e3742002-12-13 20:15:29 +000093
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +000094/* Callers are required to initialize the memory */
Paul Jakmaf63f06d2011-04-08 12:44:43 +010095static as_t *
paulfe69a502005-09-10 16:55:02 +000096assegment_data_new (int num)
97{
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +000098 return (XMALLOC (MTYPE_AS_SEG_DATA, ASSEGMENT_DATA_SIZE (num, 1)));
paulfe69a502005-09-10 16:55:02 +000099}
100
101/* Get a new segment. Note that 0 is an allowed length,
102 * and will result in a segment with no allocated data segment.
103 * the caller should immediately assign data to the segment, as the segment
104 * otherwise is not generally valid
105 */
106static struct assegment *
107assegment_new (u_char type, u_short length)
108{
109 struct assegment *new;
110
111 new = XCALLOC (MTYPE_AS_SEG, sizeof (struct assegment));
112
113 if (length)
114 new->as = assegment_data_new (length);
115
116 new->length = length;
117 new->type = type;
118
119 return new;
120}
121
122static void
123assegment_free (struct assegment *seg)
124{
125 if (!seg)
126 return;
127
128 if (seg->as)
129 XFREE (MTYPE_AS_SEG_DATA, seg->as);
130 memset (seg, 0xfe, sizeof(struct assegment));
131 XFREE (MTYPE_AS_SEG, seg);
132
133 return;
134}
135
136/* free entire chain of segments */
137static void
138assegment_free_all (struct assegment *seg)
139{
140 struct assegment *prev;
141
142 while (seg)
143 {
144 prev = seg;
145 seg = seg->next;
146 assegment_free (prev);
147 }
148}
149
150/* Duplicate just the given assegment and its data */
151static struct assegment *
152assegment_dup (struct assegment *seg)
153{
154 struct assegment *new;
155
156 new = assegment_new (seg->type, seg->length);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000157 memcpy (new->as, seg->as, ASSEGMENT_DATA_SIZE (new->length, 1) );
paulfe69a502005-09-10 16:55:02 +0000158
159 return new;
160}
161
162/* Duplicate entire chain of assegments, return the head */
163static struct assegment *
164assegment_dup_all (struct assegment *seg)
165{
166 struct assegment *new = NULL;
167 struct assegment *head = NULL;
168
169 while (seg)
170 {
171 if (head)
172 {
173 new->next = assegment_dup (seg);
174 new = new->next;
175 }
176 else
177 head = new = assegment_dup (seg);
178
179 seg = seg->next;
180 }
181 return head;
182}
183
184/* prepend the as number to given segment, given num of times */
185static struct assegment *
186assegment_prepend_asns (struct assegment *seg, as_t asnum, int num)
187{
188 as_t *newas;
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000189 int i;
paulfe69a502005-09-10 16:55:02 +0000190
191 if (!num)
192 return seg;
193
194 if (num >= AS_SEGMENT_MAX)
195 return seg; /* we don't do huge prepends */
196
197 newas = assegment_data_new (seg->length + num);
paulfe69a502005-09-10 16:55:02 +0000198
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000199 for (i = 0; i < num; i++)
200 newas[i] = asnum;
201
202 memcpy (newas + num, seg->as, ASSEGMENT_DATA_SIZE (seg->length, 1));
203 XFREE (MTYPE_AS_SEG_DATA, seg->as);
204 seg->as = newas;
205 seg->length += num;
206
207 return seg;
paulfe69a502005-09-10 16:55:02 +0000208}
209
210/* append given array of as numbers to the segment */
211static struct assegment *
212assegment_append_asns (struct assegment *seg, as_t *asnos, int num)
213{
paul02335422006-01-16 11:13:27 +0000214 as_t *newas;
215
216 newas = XREALLOC (MTYPE_AS_SEG_DATA, seg->as,
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000217 ASSEGMENT_DATA_SIZE (seg->length + num, 1));
paulfe69a502005-09-10 16:55:02 +0000218
paul02335422006-01-16 11:13:27 +0000219 if (newas)
paulfe69a502005-09-10 16:55:02 +0000220 {
paul02335422006-01-16 11:13:27 +0000221 seg->as = newas;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000222 memcpy (seg->as + seg->length, asnos, ASSEGMENT_DATA_SIZE(num, 1));
paulfe69a502005-09-10 16:55:02 +0000223 seg->length += num;
224 return seg;
225 }
226
227 assegment_free_all (seg);
228 return NULL;
229}
230
231static int
232int_cmp (const void *p1, const void *p2)
233{
234 const as_t *as1 = p1;
235 const as_t *as2 = p2;
236
237 return (*as1 == *as2)
238 ? 0 : ( (*as1 > *as2) ? 1 : -1);
239}
240
241/* normalise the segment.
242 * In particular, merge runs of AS_SEQUENCEs into one segment
243 * Internally, we do not care about the wire segment length limit, and
244 * we want each distinct AS_PATHs to have the exact same internal
245 * representation - eg, so that our hashing actually works..
246 */
247static struct assegment *
248assegment_normalise (struct assegment *head)
249{
250 struct assegment *seg = head, *pin;
251 struct assegment *tmp;
252
253 if (!head)
254 return head;
255
256 while (seg)
257 {
258 pin = seg;
259
260 /* Sort values SET segments, for determinism in paths to aid
261 * creation of hash values / path comparisons
262 * and because it helps other lesser implementations ;)
263 */
264 if (seg->type == AS_SET || seg->type == AS_CONFED_SET)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000265 {
266 int tail = 0;
267 int i;
268
269 qsort (seg->as, seg->length, sizeof(as_t), int_cmp);
270
271 /* weed out dupes */
272 for (i=1; i < seg->length; i++)
273 {
274 if (seg->as[tail] == seg->as[i])
275 continue;
276
277 tail++;
278 if (tail < i)
279 seg->as[tail] = seg->as[i];
280 }
281 /* seg->length can be 0.. */
282 if (seg->length)
283 seg->length = tail + 1;
284 }
paulfe69a502005-09-10 16:55:02 +0000285
286 /* read ahead from the current, pinned segment while the segments
287 * are packable/mergeable. Append all following packable segments
288 * to the segment we have pinned and remove these appended
289 * segments.
290 */
291 while (pin->next && ASSEGMENT_TYPES_PACKABLE(pin, pin->next))
292 {
293 tmp = pin->next;
294 seg = pin->next;
295
296 /* append the next sequence to the pinned sequence */
297 pin = assegment_append_asns (pin, seg->as, seg->length);
298
299 /* bypass the next sequence */
300 pin->next = seg->next;
301
302 /* get rid of the now referenceless segment */
303 assegment_free (tmp);
304
305 }
306
307 seg = pin->next;
308 }
309 return head;
310}
311
paul718e3742002-12-13 20:15:29 +0000312static struct aspath *
paulfe69a502005-09-10 16:55:02 +0000313aspath_new (void)
paul718e3742002-12-13 20:15:29 +0000314{
Stephen Hemminger393deb92008-08-18 14:13:29 -0700315 return XCALLOC (MTYPE_AS_PATH, sizeof (struct aspath));
paul718e3742002-12-13 20:15:29 +0000316}
317
318/* Free AS path structure. */
319void
320aspath_free (struct aspath *aspath)
321{
322 if (!aspath)
323 return;
paulfe69a502005-09-10 16:55:02 +0000324 if (aspath->segments)
325 assegment_free_all (aspath->segments);
paul718e3742002-12-13 20:15:29 +0000326 if (aspath->str)
327 XFREE (MTYPE_AS_STR, aspath->str);
328 XFREE (MTYPE_AS_PATH, aspath);
329}
330
331/* Unintern aspath from AS path bucket. */
332void
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000333aspath_unintern (struct aspath **aspath)
paul718e3742002-12-13 20:15:29 +0000334{
335 struct aspath *ret;
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000336 struct aspath *asp = *aspath;
337
338 if (asp->refcnt)
339 asp->refcnt--;
paul718e3742002-12-13 20:15:29 +0000340
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000341 if (asp->refcnt == 0)
paul718e3742002-12-13 20:15:29 +0000342 {
343 /* This aspath must exist in aspath hash table. */
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000344 ret = hash_release (ashash, asp);
paul718e3742002-12-13 20:15:29 +0000345 assert (ret != NULL);
Paul Jakmaf6f434b2010-11-23 21:28:03 +0000346 aspath_free (asp);
347 *aspath = NULL;
paul718e3742002-12-13 20:15:29 +0000348 }
349}
350
351/* Return the start or end delimiters for a particular Segment type */
352#define AS_SEG_START 0
353#define AS_SEG_END 1
354static char
355aspath_delimiter_char (u_char type, u_char which)
356{
357 int i;
358 struct
359 {
360 int type;
361 char start;
362 char end;
363 } aspath_delim_char [] =
364 {
365 { AS_SET, '{', '}' },
paul718e3742002-12-13 20:15:29 +0000366 { AS_CONFED_SET, '[', ']' },
367 { AS_CONFED_SEQUENCE, '(', ')' },
368 { 0 }
369 };
370
371 for (i = 0; aspath_delim_char[i].type != 0; i++)
372 {
373 if (aspath_delim_char[i].type == type)
374 {
375 if (which == AS_SEG_START)
376 return aspath_delim_char[i].start;
377 else if (which == AS_SEG_END)
378 return aspath_delim_char[i].end;
379 }
380 }
381 return ' ';
382}
383
Denis Ovsienko014b6702009-06-23 21:10:45 +0400384/* countup asns from this segment and index onward */
385static int
386assegment_count_asns (struct assegment *seg, int from)
387{
388 int count = 0;
389 while (seg)
390 {
391 if (!from)
392 count += seg->length;
393 else
394 {
395 count += (seg->length - from);
396 from = 0;
397 }
398 seg = seg->next;
399 }
400 return count;
401}
402
paulfe69a502005-09-10 16:55:02 +0000403unsigned int
404aspath_count_confeds (struct aspath *aspath)
405{
406 int count = 0;
407 struct assegment *seg = aspath->segments;
408
409 while (seg)
410 {
411 if (seg->type == AS_CONFED_SEQUENCE)
412 count += seg->length;
413 else if (seg->type == AS_CONFED_SET)
414 count++;
415
416 seg = seg->next;
417 }
418 return count;
419}
420
421unsigned int
422aspath_count_hops (struct aspath *aspath)
423{
424 int count = 0;
425 struct assegment *seg = aspath->segments;
426
427 while (seg)
428 {
429 if (seg->type == AS_SEQUENCE)
430 count += seg->length;
431 else if (seg->type == AS_SET)
432 count++;
433
434 seg = seg->next;
435 }
436 return count;
437}
438
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000439/* Estimate size aspath /might/ take if encoded into an
440 * ASPATH attribute.
441 *
442 * This is a quick estimate, not definitive! aspath_put()
443 * may return a different number!!
444 */
paulfe69a502005-09-10 16:55:02 +0000445unsigned int
446aspath_size (struct aspath *aspath)
447{
448 int size = 0;
449 struct assegment *seg = aspath->segments;
450
451 while (seg)
452 {
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000453 size += ASSEGMENT_SIZE(seg->length, 1);
paulfe69a502005-09-10 16:55:02 +0000454 seg = seg->next;
455 }
456 return size;
457}
458
Paul Jakma2815e612006-09-14 02:56:07 +0000459/* Return highest public ASN in path */
460as_t
461aspath_highest (struct aspath *aspath)
462{
463 struct assegment *seg = aspath->segments;
464 as_t highest = 0;
465 unsigned int i;
466
467 while (seg)
468 {
469 for (i = 0; i < seg->length; i++)
470 if (seg->as[i] > highest
471 && (seg->as[i] < BGP_PRIVATE_AS_MIN
472 || seg->as[i] > BGP_PRIVATE_AS_MAX))
473 highest = seg->as[i];
474 seg = seg->next;
475 }
476 return highest;
477}
478
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000479/* Return 1 if there are any 4-byte ASes in the path */
480unsigned int
481aspath_has_as4 (struct aspath *aspath)
482{
483 struct assegment *seg = aspath->segments;
484 unsigned int i;
485
486 while (seg)
487 {
488 for (i = 0; i < seg->length; i++)
489 if (seg->as[i] > BGP_AS_MAX)
490 return 1;
491 seg = seg->next;
492 }
493 return 0;
494}
495
paul718e3742002-12-13 20:15:29 +0000496/* Convert aspath structure to string expression. */
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000497static void
paul718e3742002-12-13 20:15:29 +0000498aspath_make_str_count (struct aspath *as)
499{
paulfe69a502005-09-10 16:55:02 +0000500 struct assegment *seg;
501 int str_size;
502 int len = 0;
hassoc9e52be2004-09-26 16:09:34 +0000503 char *str_buf;
paul718e3742002-12-13 20:15:29 +0000504
505 /* Empty aspath. */
paulfe69a502005-09-10 16:55:02 +0000506 if (!as->segments)
paul718e3742002-12-13 20:15:29 +0000507 {
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000508 as->str = XMALLOC (MTYPE_AS_STR, 1);
509 as->str[0] = '\0';
510 as->str_len = 0;
511 return;
paul718e3742002-12-13 20:15:29 +0000512 }
paulfe69a502005-09-10 16:55:02 +0000513
514 seg = as->segments;
515
Denis Ovsienko014b6702009-06-23 21:10:45 +0400516 /* ASN takes 5 to 10 chars plus seperator, see below.
517 * If there is one differing segment type, we need an additional
518 * 2 chars for segment delimiters, and the final '\0'.
519 * Hopefully this is large enough to avoid hitting the realloc
520 * code below for most common sequences.
521 *
522 * This was changed to 10 after the well-known BGP assertion, which
523 * had hit some parts of the Internet in May of 2009.
524 */
525#define ASN_STR_LEN (10 + 1)
526 str_size = MAX (assegment_count_asns (seg, 0) * ASN_STR_LEN + 2 + 1,
527 ASPATH_STR_DEFAULT_LEN);
paul718e3742002-12-13 20:15:29 +0000528 str_buf = XMALLOC (MTYPE_AS_STR, str_size);
paul718e3742002-12-13 20:15:29 +0000529
paulfe69a502005-09-10 16:55:02 +0000530 while (seg)
paul718e3742002-12-13 20:15:29 +0000531 {
532 int i;
paulfe69a502005-09-10 16:55:02 +0000533 char seperator;
paul718e3742002-12-13 20:15:29 +0000534
paulfe69a502005-09-10 16:55:02 +0000535 /* Check AS type validity. Set seperator for segment */
536 switch (seg->type)
537 {
538 case AS_SET:
539 case AS_CONFED_SET:
540 seperator = ',';
541 break;
542 case AS_SEQUENCE:
543 case AS_CONFED_SEQUENCE:
544 seperator = ' ';
545 break;
546 default:
547 XFREE (MTYPE_AS_STR, str_buf);
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000548 as->str = NULL;
549 as->str_len = 0;
550 return;
paulfe69a502005-09-10 16:55:02 +0000551 }
552
Denis Ovsienko014b6702009-06-23 21:10:45 +0400553 /* We might need to increase str_buf, particularly if path has
554 * differing segments types, our initial guesstimate above will
555 * have been wrong. Need 10 chars for ASN, a seperator each and
556 * potentially two segment delimiters, plus a space between each
557 * segment and trailing zero.
558 *
559 * This definitely didn't work with the value of 5 bytes and
560 * 32-bit ASNs.
561 */
562#define SEGMENT_STR_LEN(X) (((X)->length * ASN_STR_LEN) + 2 + 1 + 1)
563 if ( (len + SEGMENT_STR_LEN(seg)) > str_size)
564 {
565 str_size = len + SEGMENT_STR_LEN(seg);
566 str_buf = XREALLOC (MTYPE_AS_STR, str_buf, str_size);
567 }
568#undef ASN_STR_LEN
569#undef SEGMENT_STR_LEN
570
paulfe69a502005-09-10 16:55:02 +0000571 if (seg->type != AS_SEQUENCE)
Denis Ovsienko014b6702009-06-23 21:10:45 +0400572 len += snprintf (str_buf + len, str_size - len,
573 "%c",
574 aspath_delimiter_char (seg->type, AS_SEG_START));
paulfe69a502005-09-10 16:55:02 +0000575
576 /* write out the ASNs, with their seperators, bar the last one*/
577 for (i = 0; i < seg->length; i++)
578 {
579 len += snprintf (str_buf + len, str_size - len, "%u", seg->as[i]);
580
581 if (i < (seg->length - 1))
582 len += snprintf (str_buf + len, str_size - len, "%c", seperator);
583 }
584
585 if (seg->type != AS_SEQUENCE)
586 len += snprintf (str_buf + len, str_size - len, "%c",
587 aspath_delimiter_char (seg->type, AS_SEG_END));
588 if (seg->next)
589 len += snprintf (str_buf + len, str_size - len, " ");
590
591 seg = seg->next;
paul718e3742002-12-13 20:15:29 +0000592 }
paulfe69a502005-09-10 16:55:02 +0000593
594 assert (len < str_size);
595
596 str_buf[len] = '\0';
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000597 as->str = str_buf;
598 as->str_len = len;
paul718e3742002-12-13 20:15:29 +0000599
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000600 return;
paul718e3742002-12-13 20:15:29 +0000601}
602
paulfe69a502005-09-10 16:55:02 +0000603static void
604aspath_str_update (struct aspath *as)
605{
606 if (as->str)
607 XFREE (MTYPE_AS_STR, as->str);
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000608 aspath_make_str_count (as);
paulfe69a502005-09-10 16:55:02 +0000609}
610
paul718e3742002-12-13 20:15:29 +0000611/* Intern allocated AS path. */
612struct aspath *
613aspath_intern (struct aspath *aspath)
614{
615 struct aspath *find;
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000616
617 /* Assert this AS path structure is not interned and has the string
618 representation built. */
paul718e3742002-12-13 20:15:29 +0000619 assert (aspath->refcnt == 0);
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000620 assert (aspath->str);
paul718e3742002-12-13 20:15:29 +0000621
622 /* Check AS path hash. */
623 find = hash_get (ashash, aspath, hash_alloc_intern);
paul718e3742002-12-13 20:15:29 +0000624 if (find != aspath)
paulfe69a502005-09-10 16:55:02 +0000625 aspath_free (aspath);
paul718e3742002-12-13 20:15:29 +0000626
627 find->refcnt++;
628
paul718e3742002-12-13 20:15:29 +0000629 return find;
630}
631
632/* Duplicate aspath structure. Created same aspath structure but
633 reference count and AS path string is cleared. */
634struct aspath *
635aspath_dup (struct aspath *aspath)
636{
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000637 unsigned short buflen = aspath->str_len + 1;
paul718e3742002-12-13 20:15:29 +0000638 struct aspath *new;
639
paulfe69a502005-09-10 16:55:02 +0000640 new = XCALLOC (MTYPE_AS_PATH, sizeof (struct aspath));
paul718e3742002-12-13 20:15:29 +0000641
paulfe69a502005-09-10 16:55:02 +0000642 if (aspath->segments)
643 new->segments = assegment_dup_all (aspath->segments);
paul718e3742002-12-13 20:15:29 +0000644
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000645 if (!aspath->str)
646 return new;
647
648 new->str = XMALLOC (MTYPE_AS_STR, buflen);
649 new->str_len = aspath->str_len;
650
651 /* copy the string data */
652 if (aspath->str_len > 0)
653 memcpy (new->str, aspath->str, buflen);
654 else
655 new->str[0] = '\0';
paul718e3742002-12-13 20:15:29 +0000656
657 return new;
658}
659
paul94f2b392005-06-28 12:44:16 +0000660static void *
paulfe69a502005-09-10 16:55:02 +0000661aspath_hash_alloc (void *arg)
paul718e3742002-12-13 20:15:29 +0000662{
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000663 const struct aspath *aspath = arg;
664 struct aspath *new;
665
666 /* Malformed AS path value. */
667 assert (aspath->str);
668 if (! aspath->str)
669 return NULL;
paul718e3742002-12-13 20:15:29 +0000670
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000671 /* New aspath structure is needed. */
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000672 new = XMALLOC (MTYPE_AS_PATH, sizeof (struct aspath));
paul718e3742002-12-13 20:15:29 +0000673
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000674 /* Reuse segments and string representation */
675 new->refcnt = 0;
676 new->segments = aspath->segments;
677 new->str = aspath->str;
678 new->str_len = aspath->str_len;
679
680 return new;
paul718e3742002-12-13 20:15:29 +0000681}
682
Paul Jakmaab005292010-11-27 22:48:34 +0000683/* parse as-segment byte stream in struct assegment */
Paul Jakmab881c702010-11-23 16:35:42 +0000684static int
685assegments_parse (struct stream *s, size_t length,
686 struct assegment **result, int use32bit)
paulfe69a502005-09-10 16:55:02 +0000687{
688 struct assegment_header segh;
689 struct assegment *seg, *prev = NULL, *head = NULL;
Paul Jakmaab005292010-11-27 22:48:34 +0000690 size_t bytes = 0;
paulfe69a502005-09-10 16:55:02 +0000691
Paul Jakmaab005292010-11-27 22:48:34 +0000692 /* empty aspath (ie iBGP or somesuch) */
693 if (length == 0)
Paul Jakmab881c702010-11-23 16:35:42 +0000694 return 0;
paulfe69a502005-09-10 16:55:02 +0000695
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000696 if (BGP_DEBUG (as4, AS4_SEGMENT))
697 zlog_debug ("[AS4SEG] Parse aspath segment: got total byte length %lu",
698 (unsigned long) length);
Paul Jakmaab005292010-11-27 22:48:34 +0000699 /* basic checks */
700 if ((STREAM_READABLE(s) < length)
701 || (STREAM_READABLE(s) < AS_HEADER_SIZE)
702 || (length % AS16_VALUE_SIZE ))
Paul Jakmab881c702010-11-23 16:35:42 +0000703 return -1;
paulfe69a502005-09-10 16:55:02 +0000704
Paul Jakmaab005292010-11-27 22:48:34 +0000705 while (bytes < length)
paulfe69a502005-09-10 16:55:02 +0000706 {
707 int i;
Chris Hallcddb8112010-08-09 22:31:37 +0400708 size_t seg_size;
paulfe69a502005-09-10 16:55:02 +0000709
Paul Jakmaab005292010-11-27 22:48:34 +0000710 if ((length - bytes) <= AS_HEADER_SIZE)
Chris Hallcddb8112010-08-09 22:31:37 +0400711 {
Paul Jakmaab005292010-11-27 22:48:34 +0000712 if (head)
713 assegment_free_all (head);
Paul Jakmab881c702010-11-23 16:35:42 +0000714 return -1;
Paul Jakmafdbc8e72011-04-11 16:31:43 +0100715 }
716
Paul Jakmaab005292010-11-27 22:48:34 +0000717 /* softly softly, get the header first on its own */
paulfe69a502005-09-10 16:55:02 +0000718 segh.type = stream_getc (s);
719 segh.length = stream_getc (s);
720
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000721 seg_size = ASSEGMENT_SIZE(segh.length, use32bit);
722
723 if (BGP_DEBUG (as4, AS4_SEGMENT))
724 zlog_debug ("[AS4SEG] Parse aspath segment: got type %d, length %d",
725 segh.type, segh.length);
726
Paul Jakmaab005292010-11-27 22:48:34 +0000727 /* check it.. */
728 if ( ((bytes + seg_size) > length)
729 /* 1771bis 4.3b: seg length contains one or more */
730 || (segh.length == 0)
731 /* Paranoia in case someone changes type of segment length.
732 * Shift both values by 0x10 to make the comparison operate
733 * on more, than 8 bits (otherwise it's a warning, bug #564).
Denis Ovsienko2eb445e2009-12-04 17:32:54 +0300734 */
Paul Jakmaab005292010-11-27 22:48:34 +0000735 || ((sizeof segh.length > 1)
736 && (0x10 + segh.length > 0x10 + AS_SEGMENT_MAX)))
paulfe69a502005-09-10 16:55:02 +0000737 {
Paul Jakmaab005292010-11-27 22:48:34 +0000738 if (head)
paulfe69a502005-09-10 16:55:02 +0000739 assegment_free_all (head);
Paul Jakmab881c702010-11-23 16:35:42 +0000740 return -1;
paulfe69a502005-09-10 16:55:02 +0000741 }
742
Paul Jakmafdbc8e72011-04-11 16:31:43 +0100743 switch (segh.type)
744 {
745 case AS_SEQUENCE:
746 case AS_SET:
Paul Jakmafdbc8e72011-04-11 16:31:43 +0100747 case AS_CONFED_SEQUENCE:
748 case AS_CONFED_SET:
Paul Jakmaab005292010-11-27 22:48:34 +0000749 break;
750 default:
751 if (head)
752 assegment_free_all (head);
Paul Jakmab881c702010-11-23 16:35:42 +0000753 return -1;
paulfe69a502005-09-10 16:55:02 +0000754 }
Chris Hallcddb8112010-08-09 22:31:37 +0400755
paulfe69a502005-09-10 16:55:02 +0000756 /* now its safe to trust lengths */
757 seg = assegment_new (segh.type, segh.length);
758
759 if (head)
760 prev->next = seg;
761 else /* it's the first segment */
762 head = prev = seg;
763
764 for (i = 0; i < segh.length; i++)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000765 seg->as[i] = (use32bit) ? stream_getl (s) : stream_getw (s);
766
Paul Jakmaab005292010-11-27 22:48:34 +0000767 bytes += seg_size;
768
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000769 if (BGP_DEBUG (as4, AS4_SEGMENT))
Paul Jakmaab005292010-11-27 22:48:34 +0000770 zlog_debug ("[AS4SEG] Parse aspath segment: Bytes now: %lu",
771 (unsigned long) bytes);
paulfe69a502005-09-10 16:55:02 +0000772
773 prev = seg;
774 }
775
Paul Jakmab881c702010-11-23 16:35:42 +0000776 *result = assegment_normalise (head);
777 return 0;
paulfe69a502005-09-10 16:55:02 +0000778}
779
Paul Jakmaab005292010-11-27 22:48:34 +0000780/* AS path parse function. pnt is a pointer to byte stream and length
781 is length of byte stream. If there is same AS path in the the AS
Paul Jakmab881c702010-11-23 16:35:42 +0000782 path hash then return it else make new AS path structure.
783
784 On error NULL is returned.
Chris Hallcddb8112010-08-09 22:31:37 +0400785 */
paul718e3742002-12-13 20:15:29 +0000786struct aspath *
Paul Jakmaab005292010-11-27 22:48:34 +0000787aspath_parse (struct stream *s, size_t length, int use32bit)
paul718e3742002-12-13 20:15:29 +0000788{
789 struct aspath as;
790 struct aspath *find;
791
Paul Jakmaab005292010-11-27 22:48:34 +0000792 /* If length is odd it's malformed AS path. */
793 /* Nit-picking: if (use32bit == 0) it is malformed if odd,
794 * otherwise its malformed when length is larger than 2 and (length-2)
795 * is not dividable by 4.
796 * But... this time we're lazy
797 */
798 if (length % AS16_VALUE_SIZE )
799 return NULL;
Chris Hallcddb8112010-08-09 22:31:37 +0400800
Paul Jakmaab005292010-11-27 22:48:34 +0000801 memset (&as, 0, sizeof (struct aspath));
Paul Jakmab881c702010-11-23 16:35:42 +0000802 if (assegments_parse (s, length, &as.segments, use32bit) < 0)
803 return NULL;
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000804
paul718e3742002-12-13 20:15:29 +0000805 /* If already same aspath exist then return it. */
806 find = hash_get (ashash, &as, aspath_hash_alloc);
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +0000807
808 /* bug! should not happen, let the daemon crash below */
809 assert (find);
810
811 /* if the aspath was already hashed free temporary memory. */
812 if (find->refcnt)
813 {
814 assegment_free_all (as.segments);
815 /* aspath_key_make() always updates the string */
816 XFREE (MTYPE_AS_STR, as.str);
817 }
818
paul718e3742002-12-13 20:15:29 +0000819 find->refcnt++;
820
821 return find;
822}
823
Paul Jakmaf63f06d2011-04-08 12:44:43 +0100824static void
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000825assegment_data_put (struct stream *s, as_t *as, int num, int use32bit)
paul718e3742002-12-13 20:15:29 +0000826{
paulfe69a502005-09-10 16:55:02 +0000827 int i;
828 assert (num <= AS_SEGMENT_MAX);
829
830 for (i = 0; i < num; i++)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000831 if ( use32bit )
832 stream_putl (s, as[i]);
833 else
834 {
835 if ( as[i] <= BGP_AS_MAX )
836 stream_putw(s, as[i]);
837 else
838 stream_putw(s, BGP_AS_TRANS);
839 }
paul718e3742002-12-13 20:15:29 +0000840}
841
Paul Jakmaf63f06d2011-04-08 12:44:43 +0100842static size_t
paulfe69a502005-09-10 16:55:02 +0000843assegment_header_put (struct stream *s, u_char type, int length)
844{
845 size_t lenp;
846 assert (length <= AS_SEGMENT_MAX);
847 stream_putc (s, type);
848 lenp = stream_get_endp (s);
849 stream_putc (s, length);
850 return lenp;
851}
852
853/* write aspath data to stream */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000854size_t
855aspath_put (struct stream *s, struct aspath *as, int use32bit )
paulfe69a502005-09-10 16:55:02 +0000856{
857 struct assegment *seg = as->segments;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000858 size_t bytes = 0;
paulfe69a502005-09-10 16:55:02 +0000859
860 if (!seg || seg->length == 0)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000861 return 0;
paulfe69a502005-09-10 16:55:02 +0000862
863 if (seg)
864 {
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000865 /*
866 * Hey, what do we do when we have > STREAM_WRITABLE(s) here?
867 * At the moment, we would write out a partial aspath, and our peer
868 * will complain and drop the session :-/
869 *
870 * The general assumption here is that many things tested will
871 * never happen. And, in real live, up to now, they have not.
872 */
873 while (seg && (ASSEGMENT_LEN(seg, use32bit) <= STREAM_WRITEABLE(s)))
paulfe69a502005-09-10 16:55:02 +0000874 {
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000875 struct assegment *next = seg->next;
paulfe69a502005-09-10 16:55:02 +0000876 int written = 0;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000877 int asns_packed = 0;
paulfe69a502005-09-10 16:55:02 +0000878 size_t lenp;
879
880 /* Overlength segments have to be split up */
881 while ( (seg->length - written) > AS_SEGMENT_MAX)
882 {
883 assegment_header_put (s, seg->type, AS_SEGMENT_MAX);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000884 assegment_data_put (s, seg->as, AS_SEGMENT_MAX, use32bit);
paulfe69a502005-09-10 16:55:02 +0000885 written += AS_SEGMENT_MAX;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000886 bytes += ASSEGMENT_SIZE (written, use32bit);
paulfe69a502005-09-10 16:55:02 +0000887 }
888
889 /* write the final segment, probably is also the first */
890 lenp = assegment_header_put (s, seg->type, seg->length - written);
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000891 assegment_data_put (s, (seg->as + written), seg->length - written,
892 use32bit);
paulfe69a502005-09-10 16:55:02 +0000893
894 /* Sequence-type segments can be 'packed' together
895 * Case of a segment which was overlength and split up
896 * will be missed here, but that doesn't matter.
897 */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000898 while (next && ASSEGMENTS_PACKABLE (seg, next))
paulfe69a502005-09-10 16:55:02 +0000899 {
900 /* NB: We should never normally get here given we
901 * normalise aspath data when parse them. However, better
902 * safe than sorry. We potentially could call
903 * assegment_normalise here instead, but it's cheaper and
904 * easier to do it on the fly here rather than go through
905 * the segment list twice every time we write out
906 * aspath's.
907 */
908
909 /* Next segment's data can fit in this one */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000910 assegment_data_put (s, next->as, next->length, use32bit);
paulfe69a502005-09-10 16:55:02 +0000911
912 /* update the length of the segment header */
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000913 stream_putc_at (s, lenp, seg->length - written + next->length);
914 asns_packed += next->length;
915
916 next = next->next;
paulfe69a502005-09-10 16:55:02 +0000917 }
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000918
919 bytes += ASSEGMENT_SIZE (seg->length - written + asns_packed,
920 use32bit);
921 seg = next;
paulfe69a502005-09-10 16:55:02 +0000922 }
923 }
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000924 return bytes;
paulfe69a502005-09-10 16:55:02 +0000925}
926
927/* This is for SNMP BGP4PATHATTRASPATHSEGMENT
928 * We have no way to manage the storage, so we use a static stream
929 * wrapper around aspath_put.
930 */
931u_char *
932aspath_snmp_pathseg (struct aspath *as, size_t *varlen)
933{
934#define SNMP_PATHSEG_MAX 1024
paul8fdc32a2006-01-16 12:01:29 +0000935
936 if (!snmp_stream)
937 snmp_stream = stream_new (SNMP_PATHSEG_MAX);
paulfe69a502005-09-10 16:55:02 +0000938 else
paul8fdc32a2006-01-16 12:01:29 +0000939 stream_reset (snmp_stream);
paulfe69a502005-09-10 16:55:02 +0000940
941 if (!as)
942 {
943 *varlen = 0;
944 return NULL;
945 }
Paul Jakma0b2aa3a2007-10-14 22:32:21 +0000946 aspath_put (snmp_stream, as, 0); /* use 16 bit for now here */
paulfe69a502005-09-10 16:55:02 +0000947
paul8fdc32a2006-01-16 12:01:29 +0000948 *varlen = stream_get_endp (snmp_stream);
949 return stream_pnt(snmp_stream);
paulfe69a502005-09-10 16:55:02 +0000950}
951
952#define min(A,B) ((A) < (B) ? (A) : (B))
953
paul94f2b392005-06-28 12:44:16 +0000954static struct assegment *
paul718e3742002-12-13 20:15:29 +0000955aspath_aggregate_as_set_add (struct aspath *aspath, struct assegment *asset,
956 as_t as)
957{
958 int i;
959
960 /* If this is first AS set member, create new as-set segment. */
961 if (asset == NULL)
962 {
paulfe69a502005-09-10 16:55:02 +0000963 asset = assegment_new (AS_SET, 1);
964 if (! aspath->segments)
965 aspath->segments = asset;
paul718e3742002-12-13 20:15:29 +0000966 else
paulfe69a502005-09-10 16:55:02 +0000967 {
968 struct assegment *seg = aspath->segments;
969 while (seg->next)
970 seg = seg->next;
971 seg->next = asset;
972 }
paul718e3742002-12-13 20:15:29 +0000973 asset->type = AS_SET;
974 asset->length = 1;
paulfe69a502005-09-10 16:55:02 +0000975 asset->as[0] = as;
paul718e3742002-12-13 20:15:29 +0000976 }
977 else
978 {
paul718e3742002-12-13 20:15:29 +0000979 /* Check this AS value already exists or not. */
980 for (i = 0; i < asset->length; i++)
paulfe69a502005-09-10 16:55:02 +0000981 if (asset->as[i] == as)
paul718e3742002-12-13 20:15:29 +0000982 return asset;
paulfe69a502005-09-10 16:55:02 +0000983
paul718e3742002-12-13 20:15:29 +0000984 asset->length++;
paulfe69a502005-09-10 16:55:02 +0000985 asset->as = XREALLOC (MTYPE_AS_SEG_DATA, asset->as,
986 asset->length * AS_VALUE_SIZE);
987 asset->as[asset->length - 1] = as;
paul718e3742002-12-13 20:15:29 +0000988 }
paulfe69a502005-09-10 16:55:02 +0000989
paul718e3742002-12-13 20:15:29 +0000990
991 return asset;
992}
993
994/* Modify as1 using as2 for aggregation. */
995struct aspath *
996aspath_aggregate (struct aspath *as1, struct aspath *as2)
997{
998 int i;
999 int minlen;
1000 int match;
paulfe69a502005-09-10 16:55:02 +00001001 int from;
1002 struct assegment *seg1 = as1->segments;
1003 struct assegment *seg2 = as2->segments;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001004 struct aspath *aspath = NULL;
paul718e3742002-12-13 20:15:29 +00001005 struct assegment *asset;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001006 struct assegment *prevseg = NULL;
paul718e3742002-12-13 20:15:29 +00001007
1008 match = 0;
1009 minlen = 0;
1010 aspath = NULL;
1011 asset = NULL;
paul718e3742002-12-13 20:15:29 +00001012
1013 /* First of all check common leading sequence. */
paulfe69a502005-09-10 16:55:02 +00001014 while (seg1 && seg2)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001015 {
paul718e3742002-12-13 20:15:29 +00001016 /* Check segment type. */
1017 if (seg1->type != seg2->type)
1018 break;
1019
1020 /* Minimum segment length. */
1021 minlen = min (seg1->length, seg2->length);
1022
1023 for (match = 0; match < minlen; match++)
paulfe69a502005-09-10 16:55:02 +00001024 if (seg1->as[match] != seg2->as[match])
paul718e3742002-12-13 20:15:29 +00001025 break;
1026
1027 if (match)
1028 {
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001029 struct assegment *seg = assegment_new (seg1->type, 0);
1030
1031 seg = assegment_append_asns (seg, seg1->as, match);
1032
paul718e3742002-12-13 20:15:29 +00001033 if (! aspath)
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001034 {
1035 aspath = aspath_new ();
1036 aspath->segments = seg;
1037 }
1038 else
1039 prevseg->next = seg;
1040
1041 prevseg = seg;
paul718e3742002-12-13 20:15:29 +00001042 }
1043
1044 if (match != minlen || match != seg1->length
1045 || seg1->length != seg2->length)
1046 break;
paulfe69a502005-09-10 16:55:02 +00001047
1048 seg1 = seg1->next;
1049 seg2 = seg2->next;
paul718e3742002-12-13 20:15:29 +00001050 }
1051
1052 if (! aspath)
1053 aspath = aspath_new();
1054
1055 /* Make as-set using rest of all information. */
paulfe69a502005-09-10 16:55:02 +00001056 from = match;
1057 while (seg1)
paul718e3742002-12-13 20:15:29 +00001058 {
paulfe69a502005-09-10 16:55:02 +00001059 for (i = from; i < seg1->length; i++)
1060 asset = aspath_aggregate_as_set_add (aspath, asset, seg1->as[i]);
1061
1062 from = 0;
1063 seg1 = seg1->next;
paul718e3742002-12-13 20:15:29 +00001064 }
1065
paulfe69a502005-09-10 16:55:02 +00001066 from = match;
1067 while (seg2)
paul718e3742002-12-13 20:15:29 +00001068 {
paulfe69a502005-09-10 16:55:02 +00001069 for (i = from; i < seg2->length; i++)
1070 asset = aspath_aggregate_as_set_add (aspath, asset, seg2->as[i]);
paul718e3742002-12-13 20:15:29 +00001071
paulfe69a502005-09-10 16:55:02 +00001072 from = 0;
1073 seg2 = seg2->next;
paul718e3742002-12-13 20:15:29 +00001074 }
paulfe69a502005-09-10 16:55:02 +00001075
1076 assegment_normalise (aspath->segments);
1077 aspath_str_update (aspath);
paul718e3742002-12-13 20:15:29 +00001078 return aspath;
1079}
1080
1081/* When a BGP router receives an UPDATE with an MP_REACH_NLRI
1082 attribute, check the leftmost AS number in the AS_PATH attribute is
1083 or not the peer's AS number. */
1084int
1085aspath_firstas_check (struct aspath *aspath, as_t asno)
1086{
paulfe69a502005-09-10 16:55:02 +00001087 if ( (aspath == NULL) || (aspath->segments == NULL) )
paul718e3742002-12-13 20:15:29 +00001088 return 0;
paulfe69a502005-09-10 16:55:02 +00001089
1090 if (aspath->segments
1091 && (aspath->segments->type == AS_SEQUENCE)
1092 && (aspath->segments->as[0] == asno ))
paul718e3742002-12-13 20:15:29 +00001093 return 1;
1094
1095 return 0;
1096}
1097
Paul Jakma1f742f22006-08-06 15:52:11 +00001098/* AS path loop check. If aspath contains asno then return >= 1. */
paul718e3742002-12-13 20:15:29 +00001099int
1100aspath_loop_check (struct aspath *aspath, as_t asno)
1101{
paulfe69a502005-09-10 16:55:02 +00001102 struct assegment *seg;
paul718e3742002-12-13 20:15:29 +00001103 int count = 0;
1104
Paul Jakma1f742f22006-08-06 15:52:11 +00001105 if ( (aspath == NULL) || (aspath->segments == NULL) )
paul718e3742002-12-13 20:15:29 +00001106 return 0;
paulfe69a502005-09-10 16:55:02 +00001107
1108 seg = aspath->segments;
1109
1110 while (seg)
paul718e3742002-12-13 20:15:29 +00001111 {
1112 int i;
paul718e3742002-12-13 20:15:29 +00001113
paulfe69a502005-09-10 16:55:02 +00001114 for (i = 0; i < seg->length; i++)
1115 if (seg->as[i] == asno)
paul718e3742002-12-13 20:15:29 +00001116 count++;
paulfe69a502005-09-10 16:55:02 +00001117
1118 seg = seg->next;
paul718e3742002-12-13 20:15:29 +00001119 }
1120 return count;
1121}
1122
1123/* When all of AS path is private AS return 1. */
1124int
1125aspath_private_as_check (struct aspath *aspath)
1126{
paulfe69a502005-09-10 16:55:02 +00001127 struct assegment *seg;
1128
1129 if ( !(aspath && aspath->segments) )
paul718e3742002-12-13 20:15:29 +00001130 return 0;
paulfe69a502005-09-10 16:55:02 +00001131
1132 seg = aspath->segments;
paul718e3742002-12-13 20:15:29 +00001133
paulfe69a502005-09-10 16:55:02 +00001134 while (seg)
paul718e3742002-12-13 20:15:29 +00001135 {
1136 int i;
paul718e3742002-12-13 20:15:29 +00001137
paulfe69a502005-09-10 16:55:02 +00001138 for (i = 0; i < seg->length; i++)
paul718e3742002-12-13 20:15:29 +00001139 {
paulfe69a502005-09-10 16:55:02 +00001140 if ( (seg->as[i] < BGP_PRIVATE_AS_MIN)
1141 || (seg->as[i] > BGP_PRIVATE_AS_MAX) )
paul718e3742002-12-13 20:15:29 +00001142 return 0;
1143 }
paulfe69a502005-09-10 16:55:02 +00001144 seg = seg->next;
paul718e3742002-12-13 20:15:29 +00001145 }
1146 return 1;
1147}
1148
Vasilis Tsiligiannisca87e1d2009-07-20 01:28:35 +03001149/* AS path confed check. If aspath contains confed set or sequence then return 1. */
1150int
1151aspath_confed_check (struct aspath *aspath)
1152{
1153 struct assegment *seg;
1154
1155 if ( !(aspath && aspath->segments) )
1156 return 0;
1157
1158 seg = aspath->segments;
1159
1160 while (seg)
1161 {
1162 if (seg->type == AS_CONFED_SET || seg->type == AS_CONFED_SEQUENCE)
1163 return 1;
1164 seg = seg->next;
1165 }
1166 return 0;
1167}
1168
1169/* Leftmost AS path segment confed check. If leftmost AS segment is of type
1170 AS_CONFED_SEQUENCE or AS_CONFED_SET then return 1. */
1171int
1172aspath_left_confed_check (struct aspath *aspath)
1173{
1174
1175 if ( !(aspath && aspath->segments) )
1176 return 0;
1177
1178 if ( (aspath->segments->type == AS_CONFED_SEQUENCE)
1179 || (aspath->segments->type == AS_CONFED_SET) )
1180 return 1;
1181
1182 return 0;
1183}
1184
paul718e3742002-12-13 20:15:29 +00001185/* Merge as1 to as2. as2 should be uninterned aspath. */
paul94f2b392005-06-28 12:44:16 +00001186static struct aspath *
paul718e3742002-12-13 20:15:29 +00001187aspath_merge (struct aspath *as1, struct aspath *as2)
1188{
paulfe69a502005-09-10 16:55:02 +00001189 struct assegment *last, *new;
paul718e3742002-12-13 20:15:29 +00001190
1191 if (! as1 || ! as2)
1192 return NULL;
1193
paulfe69a502005-09-10 16:55:02 +00001194 last = new = assegment_dup_all (as1->segments);
1195
1196 /* find the last valid segment */
1197 while (last && last->next)
1198 last = last->next;
1199
1200 last->next = as2->segments;
1201 as2->segments = new;
1202 aspath_str_update (as2);
paul718e3742002-12-13 20:15:29 +00001203 return as2;
1204}
1205
1206/* Prepend as1 to as2. as2 should be uninterned aspath. */
1207struct aspath *
1208aspath_prepend (struct aspath *as1, struct aspath *as2)
1209{
paulfe69a502005-09-10 16:55:02 +00001210 struct assegment *seg1;
1211 struct assegment *seg2;
paul718e3742002-12-13 20:15:29 +00001212
1213 if (! as1 || ! as2)
1214 return NULL;
paulfe69a502005-09-10 16:55:02 +00001215
1216 seg1 = as1->segments;
1217 seg2 = as2->segments;
1218
1219 /* If as2 is empty, only need to dupe as1's chain onto as2 */
paul718e3742002-12-13 20:15:29 +00001220 if (seg2 == NULL)
1221 {
paulfe69a502005-09-10 16:55:02 +00001222 as2->segments = assegment_dup_all (as1->segments);
1223 aspath_str_update (as2);
paul718e3742002-12-13 20:15:29 +00001224 return as2;
1225 }
paulfe69a502005-09-10 16:55:02 +00001226
1227 /* If as1 is empty AS, no prepending to do. */
paul718e3742002-12-13 20:15:29 +00001228 if (seg1 == NULL)
1229 return as2;
paulfe69a502005-09-10 16:55:02 +00001230
1231 /* find the tail as1's segment chain. */
1232 while (seg1 && seg1->next)
1233 seg1 = seg1->next;
paul718e3742002-12-13 20:15:29 +00001234
Vasilis Tsiligiannis736d4402009-07-20 01:59:10 +03001235 /* Delete any AS_CONFED_SEQUENCE segment from as2. */
1236 if (seg1->type == AS_SEQUENCE && seg2->type == AS_CONFED_SEQUENCE)
1237 as2 = aspath_delete_confed_seq (as2);
1238
paul718e3742002-12-13 20:15:29 +00001239 /* Compare last segment type of as1 and first segment type of as2. */
1240 if (seg1->type != seg2->type)
1241 return aspath_merge (as1, as2);
1242
1243 if (seg1->type == AS_SEQUENCE)
1244 {
paulfe69a502005-09-10 16:55:02 +00001245 /* We have two chains of segments, as1->segments and seg2,
1246 * and we have to attach them together, merging the attaching
1247 * segments together into one.
1248 *
1249 * 1. dupe as1->segments onto head of as2
1250 * 2. merge seg2's asns onto last segment of this new chain
1251 * 3. attach chain after seg2
1252 */
paul718e3742002-12-13 20:15:29 +00001253
paulfe69a502005-09-10 16:55:02 +00001254 /* dupe as1 onto as2's head */
1255 seg1 = as2->segments = assegment_dup_all (as1->segments);
1256
1257 /* refind the tail of as2, reusing seg1 */
1258 while (seg1 && seg1->next)
1259 seg1 = seg1->next;
1260
1261 /* merge the old head, seg2, into tail, seg1 */
1262 seg1 = assegment_append_asns (seg1, seg2->as, seg2->length);
1263
1264 /* bypass the merged seg2, and attach any chain after it to
1265 * chain descending from as2's head
1266 */
1267 seg1->next = seg2->next;
1268
1269 /* seg2 is now referenceless and useless*/
1270 assegment_free (seg2);
1271
1272 /* we've now prepended as1's segment chain to as2, merging
1273 * the inbetween AS_SEQUENCE of seg2 in the process
1274 */
1275 aspath_str_update (as2);
paul718e3742002-12-13 20:15:29 +00001276 return as2;
1277 }
1278 else
1279 {
1280 /* AS_SET merge code is needed at here. */
1281 return aspath_merge (as1, as2);
1282 }
paulfe69a502005-09-10 16:55:02 +00001283 /* XXX: Ermmm, what if as1 has multiple segments?? */
1284
paul718e3742002-12-13 20:15:29 +00001285 /* Not reached */
1286}
1287
Denis Ovsienko841f7a52008-04-10 11:47:45 +00001288/* Iterate over AS_PATH segments and wipe all occurences of the
1289 * listed AS numbers. Hence some segments may lose some or even
1290 * all data on the way, the operation is implemented as a smarter
1291 * version of aspath_dup(), which allocates memory to hold the new
1292 * data, not the original. The new AS path is returned.
1293 */
1294struct aspath *
1295aspath_filter_exclude (struct aspath * source, struct aspath * exclude_list)
1296{
1297 struct assegment * srcseg, * exclseg, * lastseg;
1298 struct aspath * newpath;
1299
1300 newpath = aspath_new();
1301 lastseg = NULL;
1302
1303 for (srcseg = source->segments; srcseg; srcseg = srcseg->next)
1304 {
1305 unsigned i, y, newlen = 0, done = 0, skip_as;
1306 struct assegment * newseg;
1307
1308 /* Find out, how much ASns are we going to pick from this segment.
1309 * We can't perform filtering right inline, because the size of
1310 * the new segment isn't known at the moment yet.
1311 */
1312 for (i = 0; i < srcseg->length; i++)
1313 {
1314 skip_as = 0;
1315 for (exclseg = exclude_list->segments; exclseg && !skip_as; exclseg = exclseg->next)
1316 for (y = 0; y < exclseg->length; y++)
1317 if (srcseg->as[i] == exclseg->as[y])
1318 {
1319 skip_as = 1;
1320 // There's no sense in testing the rest of exclusion list, bail out.
1321 break;
1322 }
1323 if (!skip_as)
1324 newlen++;
1325 }
1326 /* newlen is now the number of ASns to copy */
1327 if (!newlen)
1328 continue;
1329
1330 /* Actual copying. Allocate memory and iterate once more, performing filtering. */
1331 newseg = assegment_new (srcseg->type, newlen);
1332 for (i = 0; i < srcseg->length; i++)
1333 {
1334 skip_as = 0;
1335 for (exclseg = exclude_list->segments; exclseg && !skip_as; exclseg = exclseg->next)
1336 for (y = 0; y < exclseg->length; y++)
1337 if (srcseg->as[i] == exclseg->as[y])
1338 {
1339 skip_as = 1;
1340 break;
1341 }
1342 if (skip_as)
1343 continue;
1344 newseg->as[done++] = srcseg->as[i];
1345 }
1346 /* At his point newlen must be equal to done, and both must be positive. Append
1347 * the filtered segment to the gross result. */
1348 if (!lastseg)
1349 newpath->segments = newseg;
1350 else
1351 lastseg->next = newseg;
1352 lastseg = newseg;
1353 }
1354 aspath_str_update (newpath);
1355 /* We are happy returning even an empty AS_PATH, because the administrator
1356 * might expect this very behaviour. There's a mean to avoid this, if necessary,
1357 * by having a match rule against certain AS_PATH regexps in the route-map index.
1358 */
1359 aspath_free (source);
1360 return newpath;
1361}
1362
paul718e3742002-12-13 20:15:29 +00001363/* Add specified AS to the leftmost of aspath. */
1364static struct aspath *
1365aspath_add_one_as (struct aspath *aspath, as_t asno, u_char type)
1366{
paulfe69a502005-09-10 16:55:02 +00001367 struct assegment *assegment = aspath->segments;
paul718e3742002-12-13 20:15:29 +00001368
1369 /* In case of empty aspath. */
1370 if (assegment == NULL || assegment->length == 0)
1371 {
paulfe69a502005-09-10 16:55:02 +00001372 aspath->segments = assegment_new (type, 1);
1373 aspath->segments->as[0] = asno;
1374
paul718e3742002-12-13 20:15:29 +00001375 if (assegment)
paulfe69a502005-09-10 16:55:02 +00001376 assegment_free (assegment);
paul718e3742002-12-13 20:15:29 +00001377
1378 return aspath;
1379 }
1380
1381 if (assegment->type == type)
paulfe69a502005-09-10 16:55:02 +00001382 aspath->segments = assegment_prepend_asns (aspath->segments, asno, 1);
1383 else
paul718e3742002-12-13 20:15:29 +00001384 {
paulfe69a502005-09-10 16:55:02 +00001385 /* create new segment
1386 * push it onto head of aspath's segment chain
1387 */
paul718e3742002-12-13 20:15:29 +00001388 struct assegment *newsegment;
paulfe69a502005-09-10 16:55:02 +00001389
1390 newsegment = assegment_new (type, 1);
1391 newsegment->as[0] = asno;
1392
1393 newsegment->next = assegment;
1394 aspath->segments = newsegment;
paul718e3742002-12-13 20:15:29 +00001395 }
1396
1397 return aspath;
1398}
1399
1400/* Add specified AS to the leftmost of aspath. */
1401struct aspath *
1402aspath_add_seq (struct aspath *aspath, as_t asno)
1403{
1404 return aspath_add_one_as (aspath, asno, AS_SEQUENCE);
1405}
1406
1407/* Compare leftmost AS value for MED check. If as1's leftmost AS and
1408 as2's leftmost AS is same return 1. */
1409int
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +01001410aspath_cmp_left (const struct aspath *aspath1, const struct aspath *aspath2)
paul718e3742002-12-13 20:15:29 +00001411{
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +00001412 const struct assegment *seg1;
1413 const struct assegment *seg2;
paul718e3742002-12-13 20:15:29 +00001414
paulfe69a502005-09-10 16:55:02 +00001415 if (!(aspath1 && aspath2))
1416 return 0;
paul718e3742002-12-13 20:15:29 +00001417
paulfe69a502005-09-10 16:55:02 +00001418 seg1 = aspath1->segments;
1419 seg2 = aspath2->segments;
1420
1421 /* find first non-confed segments for each */
1422 while (seg1 && ((seg1->type == AS_CONFED_SEQUENCE)
1423 || (seg1->type == AS_CONFED_SET)))
1424 seg1 = seg1->next;
1425
1426 while (seg2 && ((seg2->type == AS_CONFED_SEQUENCE)
1427 || (seg2->type == AS_CONFED_SET)))
1428 seg2 = seg2->next;
paul718e3742002-12-13 20:15:29 +00001429
1430 /* Check as1's */
paulfe69a502005-09-10 16:55:02 +00001431 if (!(seg1 && seg2
1432 && (seg1->type == AS_SEQUENCE) && (seg2->type == AS_SEQUENCE)))
paul718e3742002-12-13 20:15:29 +00001433 return 0;
paulfe69a502005-09-10 16:55:02 +00001434
1435 if (seg1->as[0] == seg2->as[0])
paul718e3742002-12-13 20:15:29 +00001436 return 1;
1437
1438 return 0;
1439}
1440
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001441/* Truncate an aspath after a number of hops, and put the hops remaining
1442 * at the front of another aspath. Needed for AS4 compat.
1443 *
1444 * Returned aspath is a /new/ aspath, which should either by free'd or
1445 * interned by the caller, as desired.
1446 */
1447struct aspath *
1448aspath_reconcile_as4 ( struct aspath *aspath, struct aspath *as4path)
1449{
1450 struct assegment *seg, *newseg, *prevseg = NULL;
1451 struct aspath *newpath = NULL, *mergedpath;
1452 int hops, cpasns = 0;
1453
1454 if (!aspath)
1455 return NULL;
1456
1457 seg = aspath->segments;
1458
1459 /* CONFEDs should get reconciled too.. */
1460 hops = (aspath_count_hops (aspath) + aspath_count_confeds (aspath))
1461 - aspath_count_hops (as4path);
1462
1463 if (hops < 0)
1464 {
1465 if (BGP_DEBUG (as4, AS4))
1466 zlog_warn ("[AS4] Fewer hops in AS_PATH than NEW_AS_PATH");
1467 /* Something's gone wrong. The RFC says we should now ignore AS4_PATH,
1468 * which is daft behaviour - it contains vital loop-detection
1469 * information which must have been removed from AS_PATH.
1470 */
1471 hops = aspath_count_hops (aspath);
1472 }
1473
1474 if (!hops)
1475 return aspath_dup (as4path);
1476
1477 if ( BGP_DEBUG(as4, AS4))
1478 zlog_debug("[AS4] got AS_PATH %s and AS4_PATH %s synthesizing now",
1479 aspath->str, as4path->str);
1480
1481 while (seg && hops > 0)
1482 {
1483 switch (seg->type)
1484 {
1485 case AS_SET:
1486 case AS_CONFED_SET:
1487 hops--;
1488 cpasns = seg->length;
1489 break;
1490 case AS_CONFED_SEQUENCE:
1491 /* Should never split a confed-sequence, if hop-count
1492 * suggests we must then something's gone wrong somewhere.
1493 *
1494 * Most important goal is to preserve AS_PATHs prime function
1495 * as loop-detector, so we fudge the numbers so that the entire
1496 * confed-sequence is merged in.
1497 */
1498 if (hops < seg->length)
1499 {
1500 if (BGP_DEBUG (as4, AS4))
1501 zlog_debug ("[AS4] AS4PATHmangle: AS_CONFED_SEQUENCE falls"
1502 " across 2/4 ASN boundary somewhere, broken..");
1503 hops = seg->length;
1504 }
1505 case AS_SEQUENCE:
1506 cpasns = MIN(seg->length, hops);
1507 hops -= seg->length;
1508 }
1509
1510 assert (cpasns <= seg->length);
1511
1512 newseg = assegment_new (seg->type, 0);
1513 newseg = assegment_append_asns (newseg, seg->as, cpasns);
1514
1515 if (!newpath)
1516 {
1517 newpath = aspath_new ();
1518 newpath->segments = newseg;
1519 }
1520 else
1521 prevseg->next = newseg;
1522
1523 prevseg = newseg;
1524 seg = seg->next;
1525 }
1526
1527 /* We may be able to join some segments here, and we must
1528 * do this because... we want normalised aspaths in out hash
1529 * and we do not want to stumble in aspath_put.
1530 */
1531 mergedpath = aspath_merge (newpath, aspath_dup(as4path));
1532 aspath_free (newpath);
1533 mergedpath->segments = assegment_normalise (mergedpath->segments);
1534 aspath_str_update (mergedpath);
1535
1536 if ( BGP_DEBUG(as4, AS4))
1537 zlog_debug ("[AS4] result of synthesizing is %s",
1538 mergedpath->str);
1539
1540 return mergedpath;
1541}
1542
paul718e3742002-12-13 20:15:29 +00001543/* Compare leftmost AS value for MED check. If as1's leftmost AS and
1544 as2's leftmost AS is same return 1. (confederation as-path
1545 only). */
1546int
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +01001547aspath_cmp_left_confed (const struct aspath *aspath1, const struct aspath *aspath2)
paul718e3742002-12-13 20:15:29 +00001548{
paulfe69a502005-09-10 16:55:02 +00001549 if (! (aspath1 && aspath2) )
paul718e3742002-12-13 20:15:29 +00001550 return 0;
paulfe69a502005-09-10 16:55:02 +00001551
paulad727402005-11-23 02:47:02 +00001552 if ( !(aspath1->segments && aspath2->segments) )
1553 return 0;
1554
paulfe69a502005-09-10 16:55:02 +00001555 if ( (aspath1->segments->type != AS_CONFED_SEQUENCE)
1556 || (aspath2->segments->type != AS_CONFED_SEQUENCE) )
paul718e3742002-12-13 20:15:29 +00001557 return 0;
paulfe69a502005-09-10 16:55:02 +00001558
1559 if (aspath1->segments->as[0] == aspath2->segments->as[0])
paul718e3742002-12-13 20:15:29 +00001560 return 1;
1561
1562 return 0;
1563}
1564
paulfe69a502005-09-10 16:55:02 +00001565/* Delete all leading AS_CONFED_SEQUENCE/SET segments from aspath.
1566 * See RFC3065, 6.1 c1 */
paul718e3742002-12-13 20:15:29 +00001567struct aspath *
1568aspath_delete_confed_seq (struct aspath *aspath)
1569{
paulfe69a502005-09-10 16:55:02 +00001570 struct assegment *seg;
paul718e3742002-12-13 20:15:29 +00001571
paulfe69a502005-09-10 16:55:02 +00001572 if (!(aspath && aspath->segments))
paul718e3742002-12-13 20:15:29 +00001573 return aspath;
1574
paulfe69a502005-09-10 16:55:02 +00001575 seg = aspath->segments;
1576
1577 /* "if the first path segment of the AS_PATH is
1578 * of type AS_CONFED_SEQUENCE,"
1579 */
1580 if (aspath->segments->type != AS_CONFED_SEQUENCE)
1581 return aspath;
paul718e3742002-12-13 20:15:29 +00001582
paulfe69a502005-09-10 16:55:02 +00001583 /* "... that segment and any immediately following segments
1584 * of the type AS_CONFED_SET or AS_CONFED_SEQUENCE are removed
1585 * from the AS_PATH attribute,"
1586 */
1587 while (seg &&
1588 (seg->type == AS_CONFED_SEQUENCE || seg->type == AS_CONFED_SET))
paul718e3742002-12-13 20:15:29 +00001589 {
paulfe69a502005-09-10 16:55:02 +00001590 aspath->segments = seg->next;
1591 assegment_free (seg);
1592 seg = aspath->segments;
paul718e3742002-12-13 20:15:29 +00001593 }
paulfe69a502005-09-10 16:55:02 +00001594 aspath_str_update (aspath);
paul718e3742002-12-13 20:15:29 +00001595 return aspath;
1596}
1597
1598/* Add new AS number to the leftmost part of the aspath as
1599 AS_CONFED_SEQUENCE. */
1600struct aspath*
1601aspath_add_confed_seq (struct aspath *aspath, as_t asno)
1602{
1603 return aspath_add_one_as (aspath, asno, AS_CONFED_SEQUENCE);
1604}
1605
1606/* Add new as value to as path structure. */
paul94f2b392005-06-28 12:44:16 +00001607static void
paul718e3742002-12-13 20:15:29 +00001608aspath_as_add (struct aspath *as, as_t asno)
1609{
paulfe69a502005-09-10 16:55:02 +00001610 struct assegment *seg = as->segments;
paul718e3742002-12-13 20:15:29 +00001611
paulfe69a502005-09-10 16:55:02 +00001612 if (!seg)
1613 return;
1614
Andrew J. Schorr93c17492007-04-15 19:17:24 +00001615 /* Last segment search procedure. */
1616 while (seg->next)
1617 seg = seg->next;
1618
paulfe69a502005-09-10 16:55:02 +00001619 assegment_append_asns (seg, &asno, 1);
paul718e3742002-12-13 20:15:29 +00001620}
1621
1622/* Add new as segment to the as path. */
paul94f2b392005-06-28 12:44:16 +00001623static void
paul718e3742002-12-13 20:15:29 +00001624aspath_segment_add (struct aspath *as, int type)
1625{
paulfe69a502005-09-10 16:55:02 +00001626 struct assegment *seg = as->segments;
1627 struct assegment *new = assegment_new (type, 0);
paul718e3742002-12-13 20:15:29 +00001628
Andrew J. Schorr93c17492007-04-15 19:17:24 +00001629 if (seg)
1630 {
1631 while (seg->next)
1632 seg = seg->next;
1633 seg->next = new;
1634 }
paul718e3742002-12-13 20:15:29 +00001635 else
Andrew J. Schorr93c17492007-04-15 19:17:24 +00001636 as->segments = new;
paul718e3742002-12-13 20:15:29 +00001637}
1638
1639struct aspath *
paul94f2b392005-06-28 12:44:16 +00001640aspath_empty (void)
paul718e3742002-12-13 20:15:29 +00001641{
Paul Jakmaab005292010-11-27 22:48:34 +00001642 return aspath_parse (NULL, 0, 1); /* 32Bit ;-) */
paul718e3742002-12-13 20:15:29 +00001643}
1644
1645struct aspath *
paul94f2b392005-06-28 12:44:16 +00001646aspath_empty_get (void)
paul718e3742002-12-13 20:15:29 +00001647{
1648 struct aspath *aspath;
1649
1650 aspath = aspath_new ();
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +00001651 aspath_make_str_count (aspath);
paul718e3742002-12-13 20:15:29 +00001652 return aspath;
1653}
1654
1655unsigned long
paulfe69a502005-09-10 16:55:02 +00001656aspath_count (void)
paul718e3742002-12-13 20:15:29 +00001657{
1658 return ashash->count;
1659}
1660
1661/*
1662 Theoretically, one as path can have:
1663
1664 One BGP packet size should be less than 4096.
1665 One BGP attribute size should be less than 4096 - BGP header size.
1666 One BGP aspath size should be less than 4096 - BGP header size -
1667 BGP mandantry attribute size.
1668*/
1669
1670/* AS path string lexical token enum. */
1671enum as_token
1672{
1673 as_token_asval,
1674 as_token_set_start,
1675 as_token_set_end,
paulfe69a502005-09-10 16:55:02 +00001676 as_token_confed_seq_start,
1677 as_token_confed_seq_end,
1678 as_token_confed_set_start,
1679 as_token_confed_set_end,
paul718e3742002-12-13 20:15:29 +00001680 as_token_unknown
1681};
1682
1683/* Return next token and point for string parse. */
paul94f2b392005-06-28 12:44:16 +00001684static const char *
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001685aspath_gettoken (const char *buf, enum as_token *token, u_long *asno)
paul718e3742002-12-13 20:15:29 +00001686{
paulfd79ac92004-10-13 05:06:08 +00001687 const char *p = buf;
paul718e3742002-12-13 20:15:29 +00001688
paulfe69a502005-09-10 16:55:02 +00001689 /* Skip seperators (space for sequences, ',' for sets). */
1690 while (isspace ((int) *p) || *p == ',')
paul718e3742002-12-13 20:15:29 +00001691 p++;
1692
1693 /* Check the end of the string and type specify characters
1694 (e.g. {}()). */
1695 switch (*p)
1696 {
1697 case '\0':
1698 return NULL;
paul718e3742002-12-13 20:15:29 +00001699 case '{':
1700 *token = as_token_set_start;
1701 p++;
1702 return p;
paul718e3742002-12-13 20:15:29 +00001703 case '}':
1704 *token = as_token_set_end;
1705 p++;
1706 return p;
paul718e3742002-12-13 20:15:29 +00001707 case '(':
paulfe69a502005-09-10 16:55:02 +00001708 *token = as_token_confed_seq_start;
paul718e3742002-12-13 20:15:29 +00001709 p++;
1710 return p;
paul718e3742002-12-13 20:15:29 +00001711 case ')':
paulfe69a502005-09-10 16:55:02 +00001712 *token = as_token_confed_seq_end;
1713 p++;
1714 return p;
paulfe69a502005-09-10 16:55:02 +00001715 case '[':
1716 *token = as_token_confed_set_start;
1717 p++;
1718 return p;
paulfe69a502005-09-10 16:55:02 +00001719 case ']':
1720 *token = as_token_confed_set_end;
paul718e3742002-12-13 20:15:29 +00001721 p++;
1722 return p;
paul718e3742002-12-13 20:15:29 +00001723 }
1724
1725 /* Check actual AS value. */
1726 if (isdigit ((int) *p))
1727 {
Denis Ovsienko10819ec2009-06-09 15:15:33 +04001728 as_t asval;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001729
paul718e3742002-12-13 20:15:29 +00001730 *token = as_token_asval;
1731 asval = (*p - '0');
1732 p++;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001733
paul718e3742002-12-13 20:15:29 +00001734 while (isdigit ((int) *p))
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001735 {
1736 asval *= 10;
1737 asval += (*p - '0');
1738 p++;
1739 }
paul718e3742002-12-13 20:15:29 +00001740 *asno = asval;
1741 return p;
1742 }
1743
1744 /* There is no match then return unknown token. */
1745 *token = as_token_unknown;
1746 return p++;
1747}
1748
1749struct aspath *
paulfd79ac92004-10-13 05:06:08 +00001750aspath_str2aspath (const char *str)
paul718e3742002-12-13 20:15:29 +00001751{
paul3fff6ff2006-02-05 17:55:35 +00001752 enum as_token token = as_token_unknown;
paul718e3742002-12-13 20:15:29 +00001753 u_short as_type;
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001754 u_long asno = 0;
paul718e3742002-12-13 20:15:29 +00001755 struct aspath *aspath;
1756 int needtype;
1757
1758 aspath = aspath_new ();
1759
1760 /* We start default type as AS_SEQUENCE. */
1761 as_type = AS_SEQUENCE;
1762 needtype = 1;
1763
1764 while ((str = aspath_gettoken (str, &token, &asno)) != NULL)
1765 {
1766 switch (token)
1767 {
1768 case as_token_asval:
1769 if (needtype)
1770 {
1771 aspath_segment_add (aspath, as_type);
1772 needtype = 0;
1773 }
1774 aspath_as_add (aspath, asno);
1775 break;
1776 case as_token_set_start:
1777 as_type = AS_SET;
1778 aspath_segment_add (aspath, as_type);
1779 needtype = 0;
1780 break;
1781 case as_token_set_end:
1782 as_type = AS_SEQUENCE;
1783 needtype = 1;
1784 break;
paulfe69a502005-09-10 16:55:02 +00001785 case as_token_confed_seq_start:
paul718e3742002-12-13 20:15:29 +00001786 as_type = AS_CONFED_SEQUENCE;
1787 aspath_segment_add (aspath, as_type);
1788 needtype = 0;
1789 break;
paulfe69a502005-09-10 16:55:02 +00001790 case as_token_confed_seq_end:
1791 as_type = AS_SEQUENCE;
1792 needtype = 1;
1793 break;
1794 case as_token_confed_set_start:
1795 as_type = AS_CONFED_SET;
1796 aspath_segment_add (aspath, as_type);
1797 needtype = 0;
1798 break;
1799 case as_token_confed_set_end:
paul718e3742002-12-13 20:15:29 +00001800 as_type = AS_SEQUENCE;
1801 needtype = 1;
1802 break;
1803 case as_token_unknown:
1804 default:
paulfe69a502005-09-10 16:55:02 +00001805 aspath_free (aspath);
paul718e3742002-12-13 20:15:29 +00001806 return NULL;
paul718e3742002-12-13 20:15:29 +00001807 }
1808 }
1809
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +00001810 aspath_make_str_count (aspath);
paul718e3742002-12-13 20:15:29 +00001811
1812 return aspath;
1813}
1814
1815/* Make hash value by raw aspath data. */
1816unsigned int
Paul Jakma923de652007-04-29 18:25:17 +00001817aspath_key_make (void *p)
paul718e3742002-12-13 20:15:29 +00001818{
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +00001819 struct aspath *aspath = (struct aspath *) p;
paul718e3742002-12-13 20:15:29 +00001820 unsigned int key = 0;
paul718e3742002-12-13 20:15:29 +00001821
Paul Jakma0b2aa3a2007-10-14 22:32:21 +00001822 if (!aspath->str)
1823 aspath_str_update (aspath);
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +00001824
1825 key = jhash (aspath->str, aspath->str_len, 2334325);
paul718e3742002-12-13 20:15:29 +00001826
1827 return key;
1828}
1829
1830/* If two aspath have same value then return 1 else return 0 */
Josh Bailey96450fa2011-07-20 20:45:12 -07001831int
Stephen Hemmingerffe11cf2008-08-14 16:25:25 +01001832aspath_cmp (const void *arg1, const void *arg2)
paul718e3742002-12-13 20:15:29 +00001833{
Denis Ovsienkoaea339f2009-04-30 17:16:22 +04001834 const struct assegment *seg1 = ((const struct aspath *)arg1)->segments;
1835 const struct assegment *seg2 = ((const struct aspath *)arg2)->segments;
paulfe69a502005-09-10 16:55:02 +00001836
1837 while (seg1 || seg2)
1838 {
1839 int i;
1840 if ((!seg1 && seg2) || (seg1 && !seg2))
1841 return 0;
1842 if (seg1->type != seg2->type)
1843 return 0;
1844 if (seg1->length != seg2->length)
1845 return 0;
1846 for (i = 0; i < seg1->length; i++)
1847 if (seg1->as[i] != seg2->as[i])
1848 return 0;
1849 seg1 = seg1->next;
1850 seg2 = seg2->next;
1851 }
1852 return 1;
paul718e3742002-12-13 20:15:29 +00001853}
1854
1855/* AS path hash initialize. */
1856void
paul94f2b392005-06-28 12:44:16 +00001857aspath_init (void)
paul718e3742002-12-13 20:15:29 +00001858{
1859 ashash = hash_create_size (32767, aspath_key_make, aspath_cmp);
1860}
paul8fdc32a2006-01-16 12:01:29 +00001861
1862void
1863aspath_finish (void)
1864{
1865 hash_free (ashash);
Chris Caputo228da422009-07-18 05:44:03 +00001866 ashash = NULL;
paul8fdc32a2006-01-16 12:01:29 +00001867
1868 if (snmp_stream)
1869 stream_free (snmp_stream);
1870}
paul718e3742002-12-13 20:15:29 +00001871
1872/* return and as path value */
1873const char *
1874aspath_print (struct aspath *as)
1875{
paulfe69a502005-09-10 16:55:02 +00001876 return (as ? as->str : NULL);
paul718e3742002-12-13 20:15:29 +00001877}
1878
1879/* Printing functions */
Denis Ovsienko841f7a52008-04-10 11:47:45 +00001880/* Feed the AS_PATH to the vty; the suffix string follows it only in case
1881 * AS_PATH wasn't empty.
1882 */
paul718e3742002-12-13 20:15:29 +00001883void
Denis Ovsienko841f7a52008-04-10 11:47:45 +00001884aspath_print_vty (struct vty *vty, const char *format, struct aspath *as, const char * suffix)
paul718e3742002-12-13 20:15:29 +00001885{
Paul Jakmab2518c12006-05-12 23:48:40 +00001886 assert (format);
1887 vty_out (vty, format, as->str);
Jorge Boncompte [DTI2]f669f7d2012-05-07 16:52:51 +00001888 if (as->str_len && strlen (suffix))
Denis Ovsienko841f7a52008-04-10 11:47:45 +00001889 vty_out (vty, "%s", suffix);
paul718e3742002-12-13 20:15:29 +00001890}
1891
paul94f2b392005-06-28 12:44:16 +00001892static void
paul718e3742002-12-13 20:15:29 +00001893aspath_show_all_iterator (struct hash_backet *backet, struct vty *vty)
1894{
1895 struct aspath *as;
1896
1897 as = (struct aspath *) backet->data;
1898
paulefa9f832002-12-13 21:47:59 +00001899 vty_out (vty, "[%p:%u] (%ld) ", backet, backet->key, as->refcnt);
paul718e3742002-12-13 20:15:29 +00001900 vty_out (vty, "%s%s", as->str, VTY_NEWLINE);
1901}
1902
1903/* Print all aspath and hash information. This function is used from
1904 `show ip bgp paths' command. */
1905void
1906aspath_print_all_vty (struct vty *vty)
1907{
1908 hash_iterate (ashash,
1909 (void (*) (struct hash_backet *, void *))
1910 aspath_show_all_iterator,
1911 vty);
1912}