]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_aspath.c
[zebra] IRDP: Move stream_free to where its created, probably fixing a leak
[mirror_frr.git] / bgpd / bgp_aspath.c
CommitLineData
718e3744 1/* AS path management routines.
2 Copyright (C) 1996, 97, 98, 99 Kunihiro Ishiguro
fe69a505 3 Copyright (C) 2005 Sun Microsystems, Inc.
718e3744 4
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"
fe69a505 30#include "stream.h"
718e3744 31
32#include "bgpd/bgpd.h"
33#include "bgpd/bgp_aspath.h"
34\f
35/* Attr. Flags and Attr. Type Code. */
36#define AS_HEADER_SIZE 2
37
38/* Two octet is used for AS value. */
39#define AS_VALUE_SIZE sizeof (as_t)
40
fe69a505 41/* Maximum protocol segment length value */
42#define AS_SEGMENT_MAX 255
43
44/* The following length and size macros relate specifically to Quagga's
45 * internal representation of AS-Segments, not per se to the on-wire
46 * sizes and lengths. At present (200508) they sort of match, however
47 * the ONLY functions which should now about the on-wire syntax are
48 * aspath_put, assegment_put and assegment_parse.
49 */
50
51/* Calculated size in bytes of ASN segment data to hold N ASN's */
52#define ASSEGMENT_DATA_SIZE(N) ((N) * AS_VALUE_SIZE)
718e3744 53
fe69a505 54/* Calculated size of segment struct to hold N ASN's */
55#define ASSEGMENT_SIZE(N) (AS_HEADER_SIZE + ASSEGMENT_DATA_SIZE (N))
56
57/* AS segment octet length. */
58#define ASSEGMENT_LEN(X) ASSEGMENT_SIZE((X)->length)
59
60/* AS_SEQUENCE segments can be packed together */
61/* Can the types of X and Y be considered for packing? */
62#define ASSEGMENT_TYPES_PACKABLE(X,Y) \
63 ( ((X)->type == (Y)->type) \
64 && ((X)->type == AS_SEQUENCE))
65/* Types and length of X,Y suitable for packing? */
66#define ASSEGMENTS_PACKABLE(X,Y) \
67 ( ASSEGMENT_TYPES_PACKABLE( (X), (Y)) \
68 && ( ((X)->length + (Y)->length) <= AS_SEGMENT_MAX ) )
69
70/* As segment header - the on-wire representation
71 * NOT the internal representation!
72 */
73struct assegment_header
718e3744 74{
75 u_char type;
76 u_char length;
718e3744 77};
78
79/* Hash for aspath. This is the top level structure of AS path. */
80struct hash *ashash;
8fdc32ab 81
82/* Stream for SNMP. See aspath_snmp_pathseg */
83static struct stream *snmp_stream;
718e3744 84\f
fe69a505 85static inline as_t *
86assegment_data_new (int num)
87{
88 return (XCALLOC (MTYPE_AS_SEG_DATA, ASSEGMENT_DATA_SIZE (num)));
89}
90
91static inline void
92assegment_data_free (as_t *asdata)
93{
94 XFREE (MTYPE_AS_SEG_DATA,asdata);
95}
96
97/* Get a new segment. Note that 0 is an allowed length,
98 * and will result in a segment with no allocated data segment.
99 * the caller should immediately assign data to the segment, as the segment
100 * otherwise is not generally valid
101 */
102static struct assegment *
103assegment_new (u_char type, u_short length)
104{
105 struct assegment *new;
106
107 new = XCALLOC (MTYPE_AS_SEG, sizeof (struct assegment));
108
109 if (length)
110 new->as = assegment_data_new (length);
111
112 new->length = length;
113 new->type = type;
114
115 return new;
116}
117
118static void
119assegment_free (struct assegment *seg)
120{
121 if (!seg)
122 return;
123
124 if (seg->as)
125 XFREE (MTYPE_AS_SEG_DATA, seg->as);
126 memset (seg, 0xfe, sizeof(struct assegment));
127 XFREE (MTYPE_AS_SEG, seg);
128
129 return;
130}
131
132/* free entire chain of segments */
133static void
134assegment_free_all (struct assegment *seg)
135{
136 struct assegment *prev;
137
138 while (seg)
139 {
140 prev = seg;
141 seg = seg->next;
142 assegment_free (prev);
143 }
144}
145
146/* Duplicate just the given assegment and its data */
147static struct assegment *
148assegment_dup (struct assegment *seg)
149{
150 struct assegment *new;
151
152 new = assegment_new (seg->type, seg->length);
153 memcpy (new->as, seg->as, ASSEGMENT_DATA_SIZE (new->length) );
154
155 return new;
156}
157
158/* Duplicate entire chain of assegments, return the head */
159static struct assegment *
160assegment_dup_all (struct assegment *seg)
161{
162 struct assegment *new = NULL;
163 struct assegment *head = NULL;
164
165 while (seg)
166 {
167 if (head)
168 {
169 new->next = assegment_dup (seg);
170 new = new->next;
171 }
172 else
173 head = new = assegment_dup (seg);
174
175 seg = seg->next;
176 }
177 return head;
178}
179
180/* prepend the as number to given segment, given num of times */
181static struct assegment *
182assegment_prepend_asns (struct assegment *seg, as_t asnum, int num)
183{
184 as_t *newas;
185
186 if (!num)
187 return seg;
188
189 if (num >= AS_SEGMENT_MAX)
190 return seg; /* we don't do huge prepends */
191
192 newas = assegment_data_new (seg->length + num);
193
194 if (newas)
195 {
196 int i;
197 for (i = 0; i < num; i++)
198 newas[i] = asnum;
199
200 memcpy (newas + num, seg->as, ASSEGMENT_DATA_SIZE (seg->length));
201 XFREE (MTYPE_AS_SEG_DATA, seg->as);
202 seg->as = newas;
203 seg->length += num;
204 return seg;
205 }
206
207 assegment_free_all (seg);
208 return NULL;
209}
210
211/* append given array of as numbers to the segment */
212static struct assegment *
213assegment_append_asns (struct assegment *seg, as_t *asnos, int num)
214{
02335429 215 as_t *newas;
216
217 newas = XREALLOC (MTYPE_AS_SEG_DATA, seg->as,
fe69a505 218 ASSEGMENT_DATA_SIZE (seg->length + num));
219
02335429 220 if (newas)
fe69a505 221 {
02335429 222 seg->as = newas;
fe69a505 223 memcpy (seg->as + seg->length, asnos, ASSEGMENT_DATA_SIZE(num));
224 seg->length += num;
225 return seg;
226 }
227
228 assegment_free_all (seg);
229 return NULL;
230}
231
232static int
233int_cmp (const void *p1, const void *p2)
234{
235 const as_t *as1 = p1;
236 const as_t *as2 = p2;
237
238 return (*as1 == *as2)
239 ? 0 : ( (*as1 > *as2) ? 1 : -1);
240}
241
242/* normalise the segment.
243 * In particular, merge runs of AS_SEQUENCEs into one segment
244 * Internally, we do not care about the wire segment length limit, and
245 * we want each distinct AS_PATHs to have the exact same internal
246 * representation - eg, so that our hashing actually works..
247 */
248static struct assegment *
249assegment_normalise (struct assegment *head)
250{
251 struct assegment *seg = head, *pin;
252 struct assegment *tmp;
253
254 if (!head)
255 return head;
256
257 while (seg)
258 {
259 pin = seg;
260
261 /* Sort values SET segments, for determinism in paths to aid
262 * creation of hash values / path comparisons
263 * and because it helps other lesser implementations ;)
264 */
265 if (seg->type == AS_SET || seg->type == AS_CONFED_SET)
266 qsort (seg->as, seg->length, sizeof(as_t), int_cmp);
267
268 /* read ahead from the current, pinned segment while the segments
269 * are packable/mergeable. Append all following packable segments
270 * to the segment we have pinned and remove these appended
271 * segments.
272 */
273 while (pin->next && ASSEGMENT_TYPES_PACKABLE(pin, pin->next))
274 {
275 tmp = pin->next;
276 seg = pin->next;
277
278 /* append the next sequence to the pinned sequence */
279 pin = assegment_append_asns (pin, seg->as, seg->length);
280
281 /* bypass the next sequence */
282 pin->next = seg->next;
283
284 /* get rid of the now referenceless segment */
285 assegment_free (tmp);
286
287 }
288
289 seg = pin->next;
290 }
291 return head;
292}
293\f
718e3744 294static struct aspath *
fe69a505 295aspath_new (void)
718e3744 296{
297 struct aspath *aspath;
298
299 aspath = XMALLOC (MTYPE_AS_PATH, sizeof (struct aspath));
300 memset (aspath, 0, sizeof (struct aspath));
301 return aspath;
302}
303
304/* Free AS path structure. */
305void
306aspath_free (struct aspath *aspath)
307{
308 if (!aspath)
309 return;
fe69a505 310 if (aspath->segments)
311 assegment_free_all (aspath->segments);
718e3744 312 if (aspath->str)
313 XFREE (MTYPE_AS_STR, aspath->str);
314 XFREE (MTYPE_AS_PATH, aspath);
315}
316
317/* Unintern aspath from AS path bucket. */
318void
319aspath_unintern (struct aspath *aspath)
320{
321 struct aspath *ret;
322
323 if (aspath->refcnt)
324 aspath->refcnt--;
325
326 if (aspath->refcnt == 0)
327 {
328 /* This aspath must exist in aspath hash table. */
329 ret = hash_release (ashash, aspath);
330 assert (ret != NULL);
331 aspath_free (aspath);
332 }
333}
334
335/* Return the start or end delimiters for a particular Segment type */
336#define AS_SEG_START 0
337#define AS_SEG_END 1
338static char
339aspath_delimiter_char (u_char type, u_char which)
340{
341 int i;
342 struct
343 {
344 int type;
345 char start;
346 char end;
347 } aspath_delim_char [] =
348 {
349 { AS_SET, '{', '}' },
718e3744 350 { AS_CONFED_SET, '[', ']' },
351 { AS_CONFED_SEQUENCE, '(', ')' },
352 { 0 }
353 };
354
355 for (i = 0; aspath_delim_char[i].type != 0; i++)
356 {
357 if (aspath_delim_char[i].type == type)
358 {
359 if (which == AS_SEG_START)
360 return aspath_delim_char[i].start;
361 else if (which == AS_SEG_END)
362 return aspath_delim_char[i].end;
363 }
364 }
365 return ' ';
366}
367
fe69a505 368/* countup asns from this segment and index onward */
369static int
370assegment_count_asns (struct assegment *seg, int from)
371{
372 int count = 0;
373 while (seg)
374 {
375 if (!from)
376 count += seg->length;
377 else
378 {
379 count += (seg->length - from);
380 from = 0;
381 }
382 seg = seg->next;
383 }
384 return count;
385}
386
387unsigned int
388aspath_count_confeds (struct aspath *aspath)
389{
390 int count = 0;
391 struct assegment *seg = aspath->segments;
392
393 while (seg)
394 {
395 if (seg->type == AS_CONFED_SEQUENCE)
396 count += seg->length;
397 else if (seg->type == AS_CONFED_SET)
398 count++;
399
400 seg = seg->next;
401 }
402 return count;
403}
404
405unsigned int
406aspath_count_hops (struct aspath *aspath)
407{
408 int count = 0;
409 struct assegment *seg = aspath->segments;
410
411 while (seg)
412 {
413 if (seg->type == AS_SEQUENCE)
414 count += seg->length;
415 else if (seg->type == AS_SET)
416 count++;
417
418 seg = seg->next;
419 }
420 return count;
421}
422
423unsigned int
424aspath_size (struct aspath *aspath)
425{
426 int size = 0;
427 struct assegment *seg = aspath->segments;
428
429 while (seg)
430 {
431 size += ASSEGMENT_SIZE(seg->length);
432 seg = seg->next;
433 }
434 return size;
435}
436
718e3744 437/* Convert aspath structure to string expression. */
94f2b392 438static char *
718e3744 439aspath_make_str_count (struct aspath *as)
440{
fe69a505 441 struct assegment *seg;
442 int str_size;
443 int len = 0;
c9e52be3 444 char *str_buf;
718e3744 445
446 /* Empty aspath. */
fe69a505 447 if (!as->segments)
718e3744 448 {
449 str_buf = XMALLOC (MTYPE_AS_STR, 1);
450 str_buf[0] = '\0';
718e3744 451 return str_buf;
452 }
fe69a505 453
454 seg = as->segments;
455
456 /* ASN takes 5 chars at least, plus seperator, see below.
457 * If there is one differing segment type, we need an additional
458 * 2 chars for segment delimiters, and the final '\0'.
459 * Hopefully this is large enough to avoid hitting the realloc
460 * code below for most common sequences.
461 */
462#define ASN_STR_LEN (5 + 1)
463 str_size = MAX (assegment_count_asns (seg, 0) * ASN_STR_LEN + 2 + 1,
464 ASPATH_STR_DEFAULT_LEN);
718e3744 465 str_buf = XMALLOC (MTYPE_AS_STR, str_size);
718e3744 466
fe69a505 467 while (seg)
718e3744 468 {
469 int i;
fe69a505 470 char seperator;
718e3744 471
fe69a505 472 /* Check AS type validity. Set seperator for segment */
473 switch (seg->type)
474 {
475 case AS_SET:
476 case AS_CONFED_SET:
477 seperator = ',';
478 break;
479 case AS_SEQUENCE:
480 case AS_CONFED_SEQUENCE:
481 seperator = ' ';
482 break;
483 default:
484 XFREE (MTYPE_AS_STR, str_buf);
485 return NULL;
486 }
487
488 /* We might need to increase str_buf, particularly if path has
489 * differing segments types, our initial guesstimate above will
490 * have been wrong. need 5 chars for ASN, a seperator each and
491 * potentially two segment delimiters, plus a space between each
492 * segment and trailing zero.
493 */
494#define SEGMENT_STR_LEN(X) (((X)->length * ASN_STR_LEN) + 2 + 1 + 1)
495 if ( (len + SEGMENT_STR_LEN(seg)) > str_size)
496 {
497 str_size = len + SEGMENT_STR_LEN(seg);
498 str_buf = XREALLOC (MTYPE_AS_STR, str_buf, str_size);
499 }
500#undef ASN_STR_LEN
501#undef SEGMENT_STR_LEN
502
503 if (seg->type != AS_SEQUENCE)
504 len += snprintf (str_buf + len, str_size - len,
505 "%c",
506 aspath_delimiter_char (seg->type, AS_SEG_START));
507
508 /* write out the ASNs, with their seperators, bar the last one*/
509 for (i = 0; i < seg->length; i++)
510 {
511 len += snprintf (str_buf + len, str_size - len, "%u", seg->as[i]);
512
513 if (i < (seg->length - 1))
514 len += snprintf (str_buf + len, str_size - len, "%c", seperator);
515 }
516
517 if (seg->type != AS_SEQUENCE)
518 len += snprintf (str_buf + len, str_size - len, "%c",
519 aspath_delimiter_char (seg->type, AS_SEG_END));
520 if (seg->next)
521 len += snprintf (str_buf + len, str_size - len, " ");
522
523 seg = seg->next;
718e3744 524 }
fe69a505 525
526 assert (len < str_size);
527
528 str_buf[len] = '\0';
718e3744 529
530 return str_buf;
531}
532
fe69a505 533static void
534aspath_str_update (struct aspath *as)
535{
536 if (as->str)
537 XFREE (MTYPE_AS_STR, as->str);
538 as->str = aspath_make_str_count (as);
539}
540
718e3744 541/* Intern allocated AS path. */
542struct aspath *
543aspath_intern (struct aspath *aspath)
544{
545 struct aspath *find;
546
547 /* Assert this AS path structure is not interned. */
548 assert (aspath->refcnt == 0);
549
550 /* Check AS path hash. */
551 find = hash_get (ashash, aspath, hash_alloc_intern);
552
553 if (find != aspath)
fe69a505 554 aspath_free (aspath);
718e3744 555
556 find->refcnt++;
557
558 if (! find->str)
559 find->str = aspath_make_str_count (find);
560
561 return find;
562}
563
564/* Duplicate aspath structure. Created same aspath structure but
565 reference count and AS path string is cleared. */
566struct aspath *
567aspath_dup (struct aspath *aspath)
568{
569 struct aspath *new;
570
fe69a505 571 new = XCALLOC (MTYPE_AS_PATH, sizeof (struct aspath));
718e3744 572
fe69a505 573 if (aspath->segments)
574 new->segments = assegment_dup_all (aspath->segments);
718e3744 575 else
fe69a505 576 new->segments = NULL;
718e3744 577
fe69a505 578 new->str = aspath_make_str_count (aspath);
718e3744 579
580 return new;
581}
582
94f2b392 583static void *
fe69a505 584aspath_hash_alloc (void *arg)
718e3744 585{
586 struct aspath *aspath;
587
588 /* New aspath strucutre is needed. */
fe69a505 589 aspath = aspath_dup (arg);
590
718e3744 591 /* Malformed AS path value. */
592 if (! aspath->str)
593 {
594 aspath_free (aspath);
595 return NULL;
596 }
597
598 return aspath;
599}
600
fe69a505 601/* parse as-segment byte stream in struct assegment */
ad72740e 602static struct assegment *
fe69a505 603assegments_parse (struct stream *s, size_t length)
604{
605 struct assegment_header segh;
606 struct assegment *seg, *prev = NULL, *head = NULL;
607 size_t bytes = 0;
608
609 /* empty aspath (ie iBGP or somesuch) */
610 if (length == 0)
611 return NULL;
612
613 /* basic checks */
614 if ( (STREAM_READABLE(s) < length)
615 || (STREAM_READABLE(s) < AS_HEADER_SIZE)
616 || (length % AS_VALUE_SIZE))
617 return NULL;
618
619 while ( (STREAM_READABLE(s) > AS_HEADER_SIZE)
620 && (bytes < length))
621 {
622 int i;
623
624 /* softly softly, get the header first on its own */
625 segh.type = stream_getc (s);
626 segh.length = stream_getc (s);
627
628 /* check it.. */
629 if ( ((bytes + ASSEGMENT_SIZE(segh.length)) > length)
630 /* 1771bis 4.3b: seg length contains one or more */
631 || (segh.length == 0)
632 /* Paranoia in case someone changes type of segment length */
633 || ((sizeof segh.length > 1) && segh.length > AS_SEGMENT_MAX))
634 {
635 if (head)
636 assegment_free_all (head);
637 return NULL;
638 }
639
640 /* now its safe to trust lengths */
641 seg = assegment_new (segh.type, segh.length);
642
643 if (head)
644 prev->next = seg;
645 else /* it's the first segment */
646 head = prev = seg;
647
648 for (i = 0; i < segh.length; i++)
649 seg->as[i] = stream_getw (s);
650
651 bytes += ASSEGMENT_SIZE(segh.length);
652
653 prev = seg;
654 }
655
656 return assegment_normalise (head);
657}
658
718e3744 659/* AS path parse function. pnt is a pointer to byte stream and length
660 is length of byte stream. If there is same AS path in the the AS
661 path hash then return it else make new AS path structure. */
662struct aspath *
fe69a505 663aspath_parse (struct stream *s, size_t length)
718e3744 664{
665 struct aspath as;
666 struct aspath *find;
667
668 /* If length is odd it's malformed AS path. */
fe69a505 669 if (length % AS_VALUE_SIZE)
718e3744 670 return NULL;
671
fe69a505 672 as.segments = assegments_parse (s, length);
673
718e3744 674 /* If already same aspath exist then return it. */
675 find = hash_get (ashash, &as, aspath_hash_alloc);
02335429 676
677 /* aspath_hash_alloc dupes segments too. that probably could be
678 * optimised out.
679 */
680 assegment_free_all (as.segments);
681
718e3744 682 if (! find)
683 return NULL;
684 find->refcnt++;
685
686 return find;
687}
688
fe69a505 689static inline void
690assegment_data_put (struct stream *s, as_t *as, int num)
691{
692 int i;
693 assert (num <= AS_SEGMENT_MAX);
694
695 for (i = 0; i < num; i++)
696 stream_putw (s, as[i]);
697}
718e3744 698
fe69a505 699static inline size_t
700assegment_header_put (struct stream *s, u_char type, int length)
718e3744 701{
fe69a505 702 size_t lenp;
703 assert (length <= AS_SEGMENT_MAX);
704 stream_putc (s, type);
705 lenp = stream_get_endp (s);
706 stream_putc (s, length);
707 return lenp;
708}
718e3744 709
fe69a505 710/* write aspath data to stream */
711void
712aspath_put (struct stream *s, struct aspath *as)
713{
714 struct assegment *seg = as->segments;
715
716 if (!seg || seg->length == 0)
717 return;
718
719 if (seg)
718e3744 720 {
fe69a505 721 while (seg && (ASSEGMENT_LEN (seg) <= STREAM_WRITEABLE(s)))
722 {
723 int written = 0;
724 size_t lenp;
725
726 /* Overlength segments have to be split up */
727 while ( (seg->length - written) > AS_SEGMENT_MAX)
728 {
729 assegment_header_put (s, seg->type, AS_SEGMENT_MAX);
730 assegment_data_put (s, seg->as, AS_SEGMENT_MAX);
731 written += AS_SEGMENT_MAX;
732 }
733
734 /* write the final segment, probably is also the first */
735 lenp = assegment_header_put (s, seg->type, seg->length - written);
736 assegment_data_put (s, (seg->as + written), seg->length - written);
737
738 /* Sequence-type segments can be 'packed' together
739 * Case of a segment which was overlength and split up
740 * will be missed here, but that doesn't matter.
741 */
742 if (seg->next && ASSEGMENTS_PACKABLE (seg, seg->next))
743 {
744 /* NB: We should never normally get here given we
745 * normalise aspath data when parse them. However, better
746 * safe than sorry. We potentially could call
747 * assegment_normalise here instead, but it's cheaper and
748 * easier to do it on the fly here rather than go through
749 * the segment list twice every time we write out
750 * aspath's.
751 */
752
753 /* Next segment's data can fit in this one */
754 assegment_data_put (s, seg->next->as, seg->next->length);
755
756 /* update the length of the segment header */
757 stream_putc_at (s, lenp,
758 seg->length - written + seg->next->length);
759 seg = seg->next->next; /* skip to past next */
760 }
761 else
762 seg = seg->next;
763 }
718e3744 764 }
fe69a505 765}
766
767/* This is for SNMP BGP4PATHATTRASPATHSEGMENT
768 * We have no way to manage the storage, so we use a static stream
769 * wrapper around aspath_put.
770 */
771u_char *
772aspath_snmp_pathseg (struct aspath *as, size_t *varlen)
773{
774#define SNMP_PATHSEG_MAX 1024
8fdc32ab 775
776 if (!snmp_stream)
777 snmp_stream = stream_new (SNMP_PATHSEG_MAX);
718e3744 778 else
8fdc32ab 779 stream_reset (snmp_stream);
fe69a505 780
781 if (!as)
718e3744 782 {
fe69a505 783 *varlen = 0;
784 return NULL;
718e3744 785 }
8fdc32ab 786 aspath_put (snmp_stream, as);
fe69a505 787
8fdc32ab 788 *varlen = stream_get_endp (snmp_stream);
789 return stream_pnt(snmp_stream);
718e3744 790}
fe69a505 791
792#define min(A,B) ((A) < (B) ? (A) : (B))
718e3744 793
94f2b392 794static struct assegment *
718e3744 795aspath_aggregate_as_set_add (struct aspath *aspath, struct assegment *asset,
796 as_t as)
797{
798 int i;
799
800 /* If this is first AS set member, create new as-set segment. */
801 if (asset == NULL)
802 {
fe69a505 803 asset = assegment_new (AS_SET, 1);
804 if (! aspath->segments)
805 aspath->segments = asset;
718e3744 806 else
fe69a505 807 {
808 struct assegment *seg = aspath->segments;
809 while (seg->next)
810 seg = seg->next;
811 seg->next = asset;
812 }
718e3744 813 asset->type = AS_SET;
814 asset->length = 1;
fe69a505 815 asset->as[0] = as;
718e3744 816 }
817 else
818 {
718e3744 819 /* Check this AS value already exists or not. */
820 for (i = 0; i < asset->length; i++)
fe69a505 821 if (asset->as[i] == as)
718e3744 822 return asset;
fe69a505 823
718e3744 824 asset->length++;
fe69a505 825 asset->as = XREALLOC (MTYPE_AS_SEG_DATA, asset->as,
826 asset->length * AS_VALUE_SIZE);
827 asset->as[asset->length - 1] = as;
718e3744 828 }
fe69a505 829
718e3744 830
831 return asset;
832}
833
834/* Modify as1 using as2 for aggregation. */
835struct aspath *
836aspath_aggregate (struct aspath *as1, struct aspath *as2)
837{
838 int i;
839 int minlen;
840 int match;
fe69a505 841 int from;
842 struct assegment *seg1 = as1->segments;
843 struct assegment *seg2 = as2->segments;
718e3744 844 struct aspath *aspath;
845 struct assegment *asset;
846
847 match = 0;
848 minlen = 0;
849 aspath = NULL;
850 asset = NULL;
718e3744 851
852 /* First of all check common leading sequence. */
fe69a505 853 while (seg1 && seg2)
718e3744 854 {
855 /* Check segment type. */
856 if (seg1->type != seg2->type)
857 break;
858
859 /* Minimum segment length. */
860 minlen = min (seg1->length, seg2->length);
861
862 for (match = 0; match < minlen; match++)
fe69a505 863 if (seg1->as[match] != seg2->as[match])
718e3744 864 break;
865
866 if (match)
867 {
868 if (! aspath)
fe69a505 869 aspath = aspath_new ();
870 aspath->segments = assegment_new (seg1->type, 0);
871 aspath->segments = assegment_append_asns (aspath->segments,
872 seg1->as, match);
718e3744 873 }
874
875 if (match != minlen || match != seg1->length
876 || seg1->length != seg2->length)
877 break;
fe69a505 878
879 seg1 = seg1->next;
880 seg2 = seg2->next;
718e3744 881 }
882
883 if (! aspath)
884 aspath = aspath_new();
885
886 /* Make as-set using rest of all information. */
fe69a505 887 from = match;
888 while (seg1)
718e3744 889 {
fe69a505 890 for (i = from; i < seg1->length; i++)
891 asset = aspath_aggregate_as_set_add (aspath, asset, seg1->as[i]);
892
893 from = 0;
894 seg1 = seg1->next;
718e3744 895 }
896
fe69a505 897 from = match;
898 while (seg2)
718e3744 899 {
fe69a505 900 for (i = from; i < seg2->length; i++)
901 asset = aspath_aggregate_as_set_add (aspath, asset, seg2->as[i]);
718e3744 902
fe69a505 903 from = 0;
904 seg2 = seg2->next;
718e3744 905 }
fe69a505 906
907 assegment_normalise (aspath->segments);
908 aspath_str_update (aspath);
718e3744 909 return aspath;
910}
911
912/* When a BGP router receives an UPDATE with an MP_REACH_NLRI
913 attribute, check the leftmost AS number in the AS_PATH attribute is
914 or not the peer's AS number. */
915int
916aspath_firstas_check (struct aspath *aspath, as_t asno)
917{
fe69a505 918 if ( (aspath == NULL) || (aspath->segments == NULL) )
718e3744 919 return 0;
fe69a505 920
921 if (aspath->segments
922 && (aspath->segments->type == AS_SEQUENCE)
923 && (aspath->segments->as[0] == asno ))
718e3744 924 return 1;
925
926 return 0;
927}
928
929/* AS path loop check. If aspath contains asno then return 1. */
930int
931aspath_loop_check (struct aspath *aspath, as_t asno)
932{
fe69a505 933 struct assegment *seg;
718e3744 934 int count = 0;
935
fe69a505 936 if ( (aspath == NULL) || (aspath->segments) )
718e3744 937 return 0;
fe69a505 938
939 seg = aspath->segments;
940
941 while (seg)
718e3744 942 {
943 int i;
718e3744 944
fe69a505 945 for (i = 0; i < seg->length; i++)
946 if (seg->as[i] == asno)
718e3744 947 count++;
fe69a505 948
949 seg = seg->next;
718e3744 950 }
951 return count;
952}
953
954/* When all of AS path is private AS return 1. */
955int
956aspath_private_as_check (struct aspath *aspath)
957{
fe69a505 958 struct assegment *seg;
959
960 if ( !(aspath && aspath->segments) )
718e3744 961 return 0;
fe69a505 962
963 seg = aspath->segments;
718e3744 964
fe69a505 965 while (seg)
718e3744 966 {
967 int i;
718e3744 968
fe69a505 969 for (i = 0; i < seg->length; i++)
718e3744 970 {
fe69a505 971 if ( (seg->as[i] < BGP_PRIVATE_AS_MIN)
972 || (seg->as[i] > BGP_PRIVATE_AS_MAX) )
718e3744 973 return 0;
974 }
fe69a505 975 seg = seg->next;
718e3744 976 }
977 return 1;
978}
979
980/* Merge as1 to as2. as2 should be uninterned aspath. */
94f2b392 981static struct aspath *
718e3744 982aspath_merge (struct aspath *as1, struct aspath *as2)
983{
fe69a505 984 struct assegment *last, *new;
718e3744 985
986 if (! as1 || ! as2)
987 return NULL;
988
fe69a505 989 last = new = assegment_dup_all (as1->segments);
990
991 /* find the last valid segment */
992 while (last && last->next)
993 last = last->next;
994
995 last->next = as2->segments;
996 as2->segments = new;
997 aspath_str_update (as2);
718e3744 998 return as2;
999}
1000
1001/* Prepend as1 to as2. as2 should be uninterned aspath. */
1002struct aspath *
1003aspath_prepend (struct aspath *as1, struct aspath *as2)
1004{
fe69a505 1005 struct assegment *seg1;
1006 struct assegment *seg2;
718e3744 1007
1008 if (! as1 || ! as2)
1009 return NULL;
fe69a505 1010
1011 seg1 = as1->segments;
1012 seg2 = as2->segments;
1013
1014 /* If as2 is empty, only need to dupe as1's chain onto as2 */
718e3744 1015 if (seg2 == NULL)
1016 {
fe69a505 1017 as2->segments = assegment_dup_all (as1->segments);
1018 aspath_str_update (as2);
718e3744 1019 return as2;
1020 }
fe69a505 1021
1022 /* If as1 is empty AS, no prepending to do. */
718e3744 1023 if (seg1 == NULL)
1024 return as2;
fe69a505 1025
1026 /* find the tail as1's segment chain. */
1027 while (seg1 && seg1->next)
1028 seg1 = seg1->next;
718e3744 1029
1030 /* Compare last segment type of as1 and first segment type of as2. */
1031 if (seg1->type != seg2->type)
1032 return aspath_merge (as1, as2);
1033
1034 if (seg1->type == AS_SEQUENCE)
1035 {
fe69a505 1036 /* We have two chains of segments, as1->segments and seg2,
1037 * and we have to attach them together, merging the attaching
1038 * segments together into one.
1039 *
1040 * 1. dupe as1->segments onto head of as2
1041 * 2. merge seg2's asns onto last segment of this new chain
1042 * 3. attach chain after seg2
1043 */
718e3744 1044
fe69a505 1045 /* dupe as1 onto as2's head */
1046 seg1 = as2->segments = assegment_dup_all (as1->segments);
1047
1048 /* refind the tail of as2, reusing seg1 */
1049 while (seg1 && seg1->next)
1050 seg1 = seg1->next;
1051
1052 /* merge the old head, seg2, into tail, seg1 */
1053 seg1 = assegment_append_asns (seg1, seg2->as, seg2->length);
1054
1055 /* bypass the merged seg2, and attach any chain after it to
1056 * chain descending from as2's head
1057 */
1058 seg1->next = seg2->next;
1059
1060 /* seg2 is now referenceless and useless*/
1061 assegment_free (seg2);
1062
1063 /* we've now prepended as1's segment chain to as2, merging
1064 * the inbetween AS_SEQUENCE of seg2 in the process
1065 */
1066 aspath_str_update (as2);
718e3744 1067 return as2;
1068 }
1069 else
1070 {
1071 /* AS_SET merge code is needed at here. */
1072 return aspath_merge (as1, as2);
1073 }
fe69a505 1074 /* XXX: Ermmm, what if as1 has multiple segments?? */
1075
718e3744 1076 /* Not reached */
1077}
1078
1079/* Add specified AS to the leftmost of aspath. */
1080static struct aspath *
1081aspath_add_one_as (struct aspath *aspath, as_t asno, u_char type)
1082{
fe69a505 1083 struct assegment *assegment = aspath->segments;
718e3744 1084
1085 /* In case of empty aspath. */
1086 if (assegment == NULL || assegment->length == 0)
1087 {
fe69a505 1088 aspath->segments = assegment_new (type, 1);
1089 aspath->segments->as[0] = asno;
1090
718e3744 1091 if (assegment)
fe69a505 1092 assegment_free (assegment);
718e3744 1093
1094 return aspath;
1095 }
1096
1097 if (assegment->type == type)
fe69a505 1098 aspath->segments = assegment_prepend_asns (aspath->segments, asno, 1);
1099 else
718e3744 1100 {
fe69a505 1101 /* create new segment
1102 * push it onto head of aspath's segment chain
1103 */
718e3744 1104 struct assegment *newsegment;
fe69a505 1105
1106 newsegment = assegment_new (type, 1);
1107 newsegment->as[0] = asno;
1108
1109 newsegment->next = assegment;
1110 aspath->segments = newsegment;
718e3744 1111 }
1112
1113 return aspath;
1114}
1115
1116/* Add specified AS to the leftmost of aspath. */
1117struct aspath *
1118aspath_add_seq (struct aspath *aspath, as_t asno)
1119{
1120 return aspath_add_one_as (aspath, asno, AS_SEQUENCE);
1121}
1122
1123/* Compare leftmost AS value for MED check. If as1's leftmost AS and
1124 as2's leftmost AS is same return 1. */
1125int
1126aspath_cmp_left (struct aspath *aspath1, struct aspath *aspath2)
1127{
fe69a505 1128 struct assegment *seg1 = NULL;
1129 struct assegment *seg2 = NULL;
718e3744 1130
fe69a505 1131 if (!(aspath1 && aspath2))
1132 return 0;
718e3744 1133
fe69a505 1134 seg1 = aspath1->segments;
1135 seg2 = aspath2->segments;
718e3744 1136
fe69a505 1137 /* find first non-confed segments for each */
1138 while (seg1 && ((seg1->type == AS_CONFED_SEQUENCE)
1139 || (seg1->type == AS_CONFED_SET)))
1140 seg1 = seg1->next;
718e3744 1141
fe69a505 1142 while (seg2 && ((seg2->type == AS_CONFED_SEQUENCE)
1143 || (seg2->type == AS_CONFED_SET)))
1144 seg2 = seg2->next;
718e3744 1145
fe69a505 1146 /* Check as1's */
1147 if (!(seg1 && seg2
1148 && (seg1->type == AS_SEQUENCE) && (seg2->type == AS_SEQUENCE)))
1149 return 0;
1150
1151 if (seg1->as[0] == seg2->as[0])
718e3744 1152 return 1;
1153
1154 return 0;
1155}
1156
1157/* Compare leftmost AS value for MED check. If as1's leftmost AS and
1158 as2's leftmost AS is same return 1. (confederation as-path
1159 only). */
1160int
1161aspath_cmp_left_confed (struct aspath *aspath1, struct aspath *aspath2)
1162{
fe69a505 1163 if (! (aspath1 && aspath2) )
718e3744 1164 return 0;
fe69a505 1165
ad72740e 1166 if ( !(aspath1->segments && aspath2->segments) )
1167 return 0;
1168
fe69a505 1169 if ( (aspath1->segments->type != AS_CONFED_SEQUENCE)
1170 || (aspath2->segments->type != AS_CONFED_SEQUENCE) )
718e3744 1171 return 0;
fe69a505 1172
1173 if (aspath1->segments->as[0] == aspath2->segments->as[0])
718e3744 1174 return 1;
1175
1176 return 0;
1177}
1178
fe69a505 1179/* Delete all leading AS_CONFED_SEQUENCE/SET segments from aspath.
1180 * See RFC3065, 6.1 c1 */
718e3744 1181struct aspath *
1182aspath_delete_confed_seq (struct aspath *aspath)
1183{
fe69a505 1184 struct assegment *seg;
718e3744 1185
fe69a505 1186 if (!(aspath && aspath->segments))
718e3744 1187 return aspath;
1188
fe69a505 1189 seg = aspath->segments;
1190
1191 /* "if the first path segment of the AS_PATH is
1192 * of type AS_CONFED_SEQUENCE,"
1193 */
1194 if (aspath->segments->type != AS_CONFED_SEQUENCE)
1195 return aspath;
718e3744 1196
fe69a505 1197 /* "... that segment and any immediately following segments
1198 * of the type AS_CONFED_SET or AS_CONFED_SEQUENCE are removed
1199 * from the AS_PATH attribute,"
1200 */
1201 while (seg &&
1202 (seg->type == AS_CONFED_SEQUENCE || seg->type == AS_CONFED_SET))
718e3744 1203 {
fe69a505 1204 aspath->segments = seg->next;
1205 assegment_free (seg);
1206 seg = aspath->segments;
718e3744 1207 }
fe69a505 1208 aspath_str_update (aspath);
718e3744 1209 return aspath;
1210}
1211
1212/* Add new AS number to the leftmost part of the aspath as
1213 AS_CONFED_SEQUENCE. */
1214struct aspath*
1215aspath_add_confed_seq (struct aspath *aspath, as_t asno)
1216{
1217 return aspath_add_one_as (aspath, asno, AS_CONFED_SEQUENCE);
1218}
1219
1220/* Add new as value to as path structure. */
94f2b392 1221static void
718e3744 1222aspath_as_add (struct aspath *as, as_t asno)
1223{
fe69a505 1224 struct assegment *seg = as->segments;
718e3744 1225
1226 /* Last segment search procedure. */
fe69a505 1227 while (seg && seg->next)
1228 seg = seg->next;
1229
1230 if (!seg)
1231 return;
1232
1233 assegment_append_asns (seg, &asno, 1);
718e3744 1234}
1235
1236/* Add new as segment to the as path. */
94f2b392 1237static void
718e3744 1238aspath_segment_add (struct aspath *as, int type)
1239{
fe69a505 1240 struct assegment *seg = as->segments;
1241 struct assegment *new = assegment_new (type, 0);
718e3744 1242
fe69a505 1243 while (seg && seg->next)
1244 seg = seg->next;
1245
1246 if (seg == NULL)
1247 as->segments = new;
718e3744 1248 else
fe69a505 1249 seg->next = new;
718e3744 1250}
1251
1252struct aspath *
94f2b392 1253aspath_empty (void)
718e3744 1254{
1255 return aspath_parse (NULL, 0);
1256}
1257
1258struct aspath *
94f2b392 1259aspath_empty_get (void)
718e3744 1260{
1261 struct aspath *aspath;
1262
1263 aspath = aspath_new ();
1264 aspath->str = aspath_make_str_count (aspath);
1265 return aspath;
1266}
1267
1268unsigned long
fe69a505 1269aspath_count (void)
718e3744 1270{
1271 return ashash->count;
1272}
1273\f
1274/*
1275 Theoretically, one as path can have:
1276
1277 One BGP packet size should be less than 4096.
1278 One BGP attribute size should be less than 4096 - BGP header size.
1279 One BGP aspath size should be less than 4096 - BGP header size -
1280 BGP mandantry attribute size.
1281*/
1282
1283/* AS path string lexical token enum. */
1284enum as_token
1285{
1286 as_token_asval,
1287 as_token_set_start,
1288 as_token_set_end,
fe69a505 1289 as_token_confed_seq_start,
1290 as_token_confed_seq_end,
1291 as_token_confed_set_start,
1292 as_token_confed_set_end,
718e3744 1293 as_token_unknown
1294};
1295
1296/* Return next token and point for string parse. */
94f2b392 1297static const char *
fd79ac91 1298aspath_gettoken (const char *buf, enum as_token *token, u_short *asno)
718e3744 1299{
fd79ac91 1300 const char *p = buf;
718e3744 1301
fe69a505 1302 /* Skip seperators (space for sequences, ',' for sets). */
1303 while (isspace ((int) *p) || *p == ',')
718e3744 1304 p++;
1305
1306 /* Check the end of the string and type specify characters
1307 (e.g. {}()). */
1308 switch (*p)
1309 {
1310 case '\0':
1311 return NULL;
718e3744 1312 case '{':
1313 *token = as_token_set_start;
1314 p++;
1315 return p;
718e3744 1316 case '}':
1317 *token = as_token_set_end;
1318 p++;
1319 return p;
718e3744 1320 case '(':
fe69a505 1321 *token = as_token_confed_seq_start;
718e3744 1322 p++;
1323 return p;
718e3744 1324 case ')':
fe69a505 1325 *token = as_token_confed_seq_end;
1326 p++;
1327 return p;
fe69a505 1328 case '[':
1329 *token = as_token_confed_set_start;
1330 p++;
1331 return p;
fe69a505 1332 case ']':
1333 *token = as_token_confed_set_end;
718e3744 1334 p++;
1335 return p;
718e3744 1336 }
1337
1338 /* Check actual AS value. */
1339 if (isdigit ((int) *p))
1340 {
1341 u_short asval;
1342
1343 *token = as_token_asval;
1344 asval = (*p - '0');
1345 p++;
1346 while (isdigit ((int) *p))
1347 {
1348 asval *= 10;
1349 asval += (*p - '0');
1350 p++;
1351 }
1352 *asno = asval;
1353 return p;
1354 }
1355
1356 /* There is no match then return unknown token. */
1357 *token = as_token_unknown;
1358 return p++;
1359}
1360
1361struct aspath *
fd79ac91 1362aspath_str2aspath (const char *str)
718e3744 1363{
3fff6ffc 1364 enum as_token token = as_token_unknown;
718e3744 1365 u_short as_type;
3fff6ffc 1366 u_short asno = NULL;
718e3744 1367 struct aspath *aspath;
1368 int needtype;
1369
1370 aspath = aspath_new ();
1371
1372 /* We start default type as AS_SEQUENCE. */
1373 as_type = AS_SEQUENCE;
1374 needtype = 1;
1375
1376 while ((str = aspath_gettoken (str, &token, &asno)) != NULL)
1377 {
1378 switch (token)
1379 {
1380 case as_token_asval:
1381 if (needtype)
1382 {
1383 aspath_segment_add (aspath, as_type);
1384 needtype = 0;
1385 }
1386 aspath_as_add (aspath, asno);
1387 break;
1388 case as_token_set_start:
1389 as_type = AS_SET;
1390 aspath_segment_add (aspath, as_type);
1391 needtype = 0;
1392 break;
1393 case as_token_set_end:
1394 as_type = AS_SEQUENCE;
1395 needtype = 1;
1396 break;
fe69a505 1397 case as_token_confed_seq_start:
718e3744 1398 as_type = AS_CONFED_SEQUENCE;
1399 aspath_segment_add (aspath, as_type);
1400 needtype = 0;
1401 break;
fe69a505 1402 case as_token_confed_seq_end:
1403 as_type = AS_SEQUENCE;
1404 needtype = 1;
1405 break;
1406 case as_token_confed_set_start:
1407 as_type = AS_CONFED_SET;
1408 aspath_segment_add (aspath, as_type);
1409 needtype = 0;
1410 break;
1411 case as_token_confed_set_end:
718e3744 1412 as_type = AS_SEQUENCE;
1413 needtype = 1;
1414 break;
1415 case as_token_unknown:
1416 default:
fe69a505 1417 aspath_free (aspath);
718e3744 1418 return NULL;
718e3744 1419 }
1420 }
1421
1422 aspath->str = aspath_make_str_count (aspath);
1423
1424 return aspath;
1425}
1426\f
1427/* Make hash value by raw aspath data. */
1428unsigned int
1429aspath_key_make (struct aspath *aspath)
1430{
1431 unsigned int key = 0;
fe69a505 1432 unsigned int i;
1433 struct assegment *seg = aspath->segments;
1434 struct assegment *prev = NULL;
718e3744 1435
fe69a505 1436 while (seg)
718e3744 1437 {
fe69a505 1438 /* segment types should be part of the hash
1439 * otherwise seq(1) and set(1) will hash to same value
1440 */
1441 if (!(prev && seg->type == AS_SEQUENCE && seg->type == prev->type))
1442 key += seg->type;
1443
1444 for (i = 0; i < seg->length; i++)
1445 key += seg->as[i];
1446
1447 prev = seg;
1448 seg = seg->next;
718e3744 1449 }
1450
1451 return key;
1452}
1453
1454/* If two aspath have same value then return 1 else return 0 */
94f2b392 1455static int
fe69a505 1456aspath_cmp (void *arg1, void *arg2)
718e3744 1457{
fe69a505 1458 struct assegment *seg1 = ((struct aspath *)arg1)->segments;
1459 struct assegment *seg2 = ((struct aspath *)arg2)->segments;
1460
1461 while (seg1 || seg2)
1462 {
1463 int i;
1464 if ((!seg1 && seg2) || (seg1 && !seg2))
1465 return 0;
1466 if (seg1->type != seg2->type)
1467 return 0;
1468 if (seg1->length != seg2->length)
1469 return 0;
1470 for (i = 0; i < seg1->length; i++)
1471 if (seg1->as[i] != seg2->as[i])
1472 return 0;
1473 seg1 = seg1->next;
1474 seg2 = seg2->next;
1475 }
1476 return 1;
718e3744 1477}
1478
1479/* AS path hash initialize. */
1480void
94f2b392 1481aspath_init (void)
718e3744 1482{
1483 ashash = hash_create_size (32767, aspath_key_make, aspath_cmp);
1484}
8fdc32ab 1485
1486void
1487aspath_finish (void)
1488{
1489 hash_free (ashash);
1490
1491 if (snmp_stream)
1492 stream_free (snmp_stream);
1493}
718e3744 1494\f
1495/* return and as path value */
1496const char *
1497aspath_print (struct aspath *as)
1498{
fe69a505 1499 return (as ? as->str : NULL);
718e3744 1500}
1501
1502/* Printing functions */
1503void
b2518c1e 1504aspath_print_vty (struct vty *vty, const char *format, struct aspath *as)
718e3744 1505{
b2518c1e
PJ
1506 assert (format);
1507 vty_out (vty, format, as->str);
718e3744 1508}
1509
94f2b392 1510static void
718e3744 1511aspath_show_all_iterator (struct hash_backet *backet, struct vty *vty)
1512{
1513 struct aspath *as;
1514
1515 as = (struct aspath *) backet->data;
1516
efa9f830 1517 vty_out (vty, "[%p:%u] (%ld) ", backet, backet->key, as->refcnt);
718e3744 1518 vty_out (vty, "%s%s", as->str, VTY_NEWLINE);
1519}
1520
1521/* Print all aspath and hash information. This function is used from
1522 `show ip bgp paths' command. */
1523void
1524aspath_print_all_vty (struct vty *vty)
1525{
1526 hash_iterate (ashash,
1527 (void (*) (struct hash_backet *, void *))
1528 aspath_show_all_iterator,
1529 vty);
1530}