1 /* AS path management routines.
2 * Copyright (C) 1996, 97, 98, 99 Kunihiro Ishiguro
3 * Copyright (C) 2005 Sun Microsystems, Inc.
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "bgpd/bgpd.h"
35 #include "bgpd/bgp_aspath.h"
36 #include "bgpd/bgp_debug.h"
37 #include "bgpd/bgp_attr.h"
39 /* Attr. Flags and Attr. Type Code. */
40 #define AS_HEADER_SIZE 2
42 /* Now FOUR octets are used for AS value. */
43 #define AS_VALUE_SIZE sizeof (as_t)
44 /* This is the old one */
45 #define AS16_VALUE_SIZE sizeof (as16_t)
47 /* Maximum protocol segment length value */
48 #define AS_SEGMENT_MAX 255
50 /* The following length and size macros relate specifically to Quagga's
51 * internal representation of AS-Segments, not per se to the on-wire
52 * sizes and lengths. At present (200508) they sort of match, however
53 * the ONLY functions which should now about the on-wire syntax are
54 * aspath_put, assegment_put and assegment_parse.
56 * aspath_put returns bytes written, the only definitive record of
57 * size of wire-format attribute..
60 /* Calculated size in bytes of ASN segment data to hold N ASN's */
61 #define ASSEGMENT_DATA_SIZE(N, S) \
62 ((N) * ((S) ? AS_VALUE_SIZE : AS16_VALUE_SIZE))
64 /* Calculated size of segment struct to hold N ASN's */
65 #define ASSEGMENT_SIZE(N,S) (AS_HEADER_SIZE + ASSEGMENT_DATA_SIZE (N,S))
67 /* AS segment octet length. */
68 #define ASSEGMENT_LEN(X,S) ASSEGMENT_SIZE((X)->length,S)
70 /* AS_SEQUENCE segments can be packed together */
71 /* Can the types of X and Y be considered for packing? */
72 #define ASSEGMENT_TYPES_PACKABLE(X, Y) \
73 (((X)->type == (Y)->type) && ((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))
79 /* As segment header - the on-wire representation
80 * NOT the internal representation!
82 struct assegment_header
{
87 /* Hash for aspath. This is the top level structure of AS path. */
88 static struct hash
*ashash
;
90 /* Stream for SNMP. See aspath_snmp_pathseg */
91 static struct stream
*snmp_stream
;
93 /* Callers are required to initialize the memory */
94 static as_t
*assegment_data_new(int num
)
96 return (XMALLOC(MTYPE_AS_SEG_DATA
, ASSEGMENT_DATA_SIZE(num
, 1)));
99 static void assegment_data_free(as_t
*asdata
)
101 XFREE(MTYPE_AS_SEG_DATA
, asdata
);
104 const char *aspath_segment_type_str
[] = {"as-invalid", "as-set", "as-sequence",
105 "as-confed-sequence", "as-confed-set"};
107 /* Get a new segment. Note that 0 is an allowed length,
108 * and will result in a segment with no allocated data segment.
109 * the caller should immediately assign data to the segment, as the segment
110 * otherwise is not generally valid
112 static struct assegment
*assegment_new(u_char type
, u_short length
)
114 struct assegment
*new;
116 new = XCALLOC(MTYPE_AS_SEG
, sizeof(struct assegment
));
119 new->as
= assegment_data_new(length
);
121 new->length
= length
;
127 static void assegment_free(struct assegment
*seg
)
133 assegment_data_free(seg
->as
);
134 memset(seg
, 0xfe, sizeof(struct assegment
));
135 XFREE(MTYPE_AS_SEG
, seg
);
140 /* free entire chain of segments */
141 static void assegment_free_all(struct assegment
*seg
)
143 struct assegment
*prev
;
148 assegment_free(prev
);
152 /* Duplicate just the given assegment and its data */
153 static struct assegment
*assegment_dup(struct assegment
*seg
)
155 struct assegment
*new;
157 new = assegment_new(seg
->type
, seg
->length
);
158 memcpy(new->as
, seg
->as
, ASSEGMENT_DATA_SIZE(new->length
, 1));
163 /* Duplicate entire chain of assegments, return the head */
164 static struct assegment
*assegment_dup_all(struct assegment
*seg
)
166 struct assegment
*new = NULL
;
167 struct assegment
*head
= NULL
;
171 new->next
= assegment_dup(seg
);
174 head
= new = assegment_dup(seg
);
181 /* prepend the as number to given segment, given num of times */
182 static struct assegment
*assegment_prepend_asns(struct assegment
*seg
,
191 if (num
>= AS_SEGMENT_MAX
)
192 return seg
; /* we don't do huge prepends */
194 if ((newas
= assegment_data_new(seg
->length
+ num
)) == NULL
)
197 for (i
= 0; i
< num
; i
++)
200 memcpy(newas
+ num
, seg
->as
, ASSEGMENT_DATA_SIZE(seg
->length
, 1));
201 assegment_data_free(seg
->as
);
208 /* append given array of as numbers to the segment */
209 static struct assegment
*assegment_append_asns(struct assegment
*seg
,
210 as_t
*asnos
, int num
)
214 newas
= XREALLOC(MTYPE_AS_SEG_DATA
, seg
->as
,
215 ASSEGMENT_DATA_SIZE(seg
->length
+ num
, 1));
219 memcpy(seg
->as
+ seg
->length
, asnos
,
220 ASSEGMENT_DATA_SIZE(num
, 1));
225 assegment_free_all(seg
);
229 static int int_cmp(const void *p1
, const void *p2
)
231 const as_t
*as1
= p1
;
232 const as_t
*as2
= p2
;
234 return (*as1
== *as2
) ? 0 : ((*as1
> *as2
) ? 1 : -1);
237 /* normalise the segment.
238 * In particular, merge runs of AS_SEQUENCEs into one segment
239 * Internally, we do not care about the wire segment length limit, and
240 * we want each distinct AS_PATHs to have the exact same internal
241 * representation - eg, so that our hashing actually works..
243 static struct assegment
*assegment_normalise(struct assegment
*head
)
245 struct assegment
*seg
= head
, *pin
;
246 struct assegment
*tmp
;
254 /* Sort values SET segments, for determinism in paths to aid
255 * creation of hash values / path comparisons
256 * and because it helps other lesser implementations ;)
258 if (seg
->type
== AS_SET
|| seg
->type
== AS_CONFED_SET
) {
262 qsort(seg
->as
, seg
->length
, sizeof(as_t
), int_cmp
);
265 for (i
= 1; i
< seg
->length
; i
++) {
266 if (seg
->as
[tail
] == seg
->as
[i
])
271 seg
->as
[tail
] = seg
->as
[i
];
273 /* seg->length can be 0.. */
275 seg
->length
= tail
+ 1;
278 /* read ahead from the current, pinned segment while the
280 * are packable/mergeable. Append all following packable
282 * to the segment we have pinned and remove these appended
285 while (pin
->next
&& ASSEGMENT_TYPES_PACKABLE(pin
, pin
->next
)) {
289 /* append the next sequence to the pinned sequence */
290 pin
= assegment_append_asns(pin
, seg
->as
, seg
->length
);
292 /* bypass the next sequence */
293 pin
->next
= seg
->next
;
295 /* get rid of the now referenceless segment */
304 static struct aspath
*aspath_new(void)
306 return XCALLOC(MTYPE_AS_PATH
, sizeof(struct aspath
));
309 /* Free AS path structure. */
310 void aspath_free(struct aspath
*aspath
)
314 if (aspath
->segments
)
315 assegment_free_all(aspath
->segments
);
317 XFREE(MTYPE_AS_STR
, aspath
->str
);
320 json_object_free(aspath
->json
);
324 XFREE(MTYPE_AS_PATH
, aspath
);
327 /* Unintern aspath from AS path bucket. */
328 void aspath_unintern(struct aspath
**aspath
)
331 struct aspath
*asp
= *aspath
;
336 if (asp
->refcnt
== 0) {
337 /* This aspath must exist in aspath hash table. */
338 ret
= hash_release(ashash
, asp
);
345 /* Return the start or end delimiters for a particular Segment type */
346 #define AS_SEG_START 0
348 static char aspath_delimiter_char(u_char type
, u_char which
)
355 } aspath_delim_char
[] = {{AS_SET
, '{', '}'},
356 {AS_CONFED_SET
, '[', ']'},
357 {AS_CONFED_SEQUENCE
, '(', ')'},
360 for (i
= 0; aspath_delim_char
[i
].type
!= 0; i
++) {
361 if (aspath_delim_char
[i
].type
== type
) {
362 if (which
== AS_SEG_START
)
363 return aspath_delim_char
[i
].start
;
364 else if (which
== AS_SEG_END
)
365 return aspath_delim_char
[i
].end
;
371 /* countup asns from this segment and index onward */
372 static int assegment_count_asns(struct assegment
*seg
, int from
)
377 count
+= seg
->length
;
379 count
+= (seg
->length
- from
);
387 unsigned int aspath_count_confeds(struct aspath
*aspath
)
390 struct assegment
*seg
= aspath
->segments
;
393 if (seg
->type
== AS_CONFED_SEQUENCE
)
394 count
+= seg
->length
;
395 else if (seg
->type
== AS_CONFED_SET
)
403 unsigned int aspath_count_hops(const struct aspath
*aspath
)
406 struct assegment
*seg
= aspath
->segments
;
409 if (seg
->type
== AS_SEQUENCE
)
410 count
+= seg
->length
;
411 else if (seg
->type
== AS_SET
)
419 /* Estimate size aspath /might/ take if encoded into an
422 * This is a quick estimate, not definitive! aspath_put()
423 * may return a different number!!
425 unsigned int aspath_size(struct aspath
*aspath
)
428 struct assegment
*seg
= aspath
->segments
;
431 size
+= ASSEGMENT_SIZE(seg
->length
, 1);
437 /* Return highest public ASN in path */
438 as_t
aspath_highest(struct aspath
*aspath
)
440 struct assegment
*seg
= aspath
->segments
;
445 for (i
= 0; i
< seg
->length
; i
++)
446 if (seg
->as
[i
] > highest
447 && !BGP_AS_IS_PRIVATE(seg
->as
[i
]))
448 highest
= seg
->as
[i
];
454 /* Return the left-most ASN in path */
455 as_t
aspath_leftmost(struct aspath
*aspath
)
457 struct assegment
*seg
= aspath
->segments
;
460 if (seg
&& seg
->length
&& seg
->type
== AS_SEQUENCE
)
461 leftmost
= seg
->as
[0];
466 /* Return 1 if there are any 4-byte ASes in the path */
467 unsigned int aspath_has_as4(struct aspath
*aspath
)
469 struct assegment
*seg
= aspath
->segments
;
473 for (i
= 0; i
< seg
->length
; i
++)
474 if (seg
->as
[i
] > BGP_AS_MAX
)
481 /* Convert aspath structure to string expression. */
482 static void aspath_make_str_count(struct aspath
*as
)
484 struct assegment
*seg
;
488 json_object
*jaspath_segments
= NULL
;
489 json_object
*jseg
= NULL
;
490 json_object
*jseg_list
= NULL
;
492 as
->json
= json_object_new_object();
493 jaspath_segments
= json_object_new_array();
497 json_object_string_add(as
->json
, "string", "Local");
498 json_object_object_add(as
->json
, "segments", jaspath_segments
);
499 json_object_int_add(as
->json
, "length", 0);
500 as
->str
= XMALLOC(MTYPE_AS_STR
, 1);
508 /* ASN takes 5 to 10 chars plus seperator, see below.
509 * If there is one differing segment type, we need an additional
510 * 2 chars for segment delimiters, and the final '\0'.
511 * Hopefully this is large enough to avoid hitting the realloc
512 * code below for most common sequences.
514 * This was changed to 10 after the well-known BGP assertion, which
515 * had hit some parts of the Internet in May of 2009.
517 #define ASN_STR_LEN (10 + 1)
518 str_size
= MAX(assegment_count_asns(seg
, 0) * ASN_STR_LEN
+ 2 + 1,
519 ASPATH_STR_DEFAULT_LEN
);
520 str_buf
= XMALLOC(MTYPE_AS_STR
, str_size
);
526 /* Check AS type validity. Set seperator for segment */
533 case AS_CONFED_SEQUENCE
:
537 XFREE(MTYPE_AS_STR
, str_buf
);
540 json_object_free(as
->json
);
545 /* We might need to increase str_buf, particularly if path has
546 * differing segments types, our initial guesstimate above will
547 * have been wrong. Need 10 chars for ASN, a seperator each and
548 * potentially two segment delimiters, plus a space between each
549 * segment and trailing zero.
551 * This definitely didn't work with the value of 5 bytes and
554 #define SEGMENT_STR_LEN(X) (((X)->length * ASN_STR_LEN) + 2 + 1 + 1)
555 if ((len
+ SEGMENT_STR_LEN(seg
)) > str_size
) {
556 str_size
= len
+ SEGMENT_STR_LEN(seg
);
557 str_buf
= XREALLOC(MTYPE_AS_STR
, str_buf
, str_size
);
560 #undef SEGMENT_STR_LEN
562 if (seg
->type
!= AS_SEQUENCE
)
564 str_buf
+ len
, str_size
- len
, "%c",
565 aspath_delimiter_char(seg
->type
, AS_SEG_START
));
567 jseg_list
= json_object_new_array();
569 /* write out the ASNs, with their seperators, bar the last one*/
570 for (i
= 0; i
< seg
->length
; i
++) {
571 json_object_array_add(jseg_list
,
572 json_object_new_int(seg
->as
[i
]));
574 len
+= snprintf(str_buf
+ len
, str_size
- len
, "%u",
577 if (i
< (seg
->length
- 1))
578 len
+= snprintf(str_buf
+ len
, str_size
- len
,
582 jseg
= json_object_new_object();
583 json_object_string_add(jseg
, "type",
584 aspath_segment_type_str
[seg
->type
]);
585 json_object_object_add(jseg
, "list", jseg_list
);
586 json_object_array_add(jaspath_segments
, jseg
);
588 if (seg
->type
!= AS_SEQUENCE
)
590 str_buf
+ len
, str_size
- len
, "%c",
591 aspath_delimiter_char(seg
->type
, AS_SEG_END
));
593 len
+= snprintf(str_buf
+ len
, str_size
- len
, " ");
598 assert(len
< str_size
);
604 json_object_string_add(as
->json
, "string", str_buf
);
605 json_object_object_add(as
->json
, "segments", jaspath_segments
);
606 json_object_int_add(as
->json
, "length", aspath_count_hops(as
));
610 static void aspath_str_update(struct aspath
*as
)
613 XFREE(MTYPE_AS_STR
, as
->str
);
616 json_object_free(as
->json
);
620 aspath_make_str_count(as
);
623 /* Intern allocated AS path. */
624 struct aspath
*aspath_intern(struct aspath
*aspath
)
628 /* Assert this AS path structure is not interned and has the string
629 representation built. */
630 assert(aspath
->refcnt
== 0);
633 /* Check AS path hash. */
634 find
= hash_get(ashash
, aspath
, hash_alloc_intern
);
643 /* Duplicate aspath structure. Created same aspath structure but
644 reference count and AS path string is cleared. */
645 struct aspath
*aspath_dup(struct aspath
*aspath
)
647 unsigned short buflen
= aspath
->str_len
+ 1;
650 new = XCALLOC(MTYPE_AS_PATH
, sizeof(struct aspath
));
653 if (aspath
->segments
)
654 new->segments
= assegment_dup_all(aspath
->segments
);
659 new->str
= XMALLOC(MTYPE_AS_STR
, buflen
);
660 new->str_len
= aspath
->str_len
;
662 /* copy the string data */
663 if (aspath
->str_len
> 0)
664 memcpy(new->str
, aspath
->str
, buflen
);
671 static void *aspath_hash_alloc(void *arg
)
673 const struct aspath
*aspath
= arg
;
676 /* Malformed AS path value. */
679 /* New aspath structure is needed. */
680 new = XMALLOC(MTYPE_AS_PATH
, sizeof(struct aspath
));
682 /* Reuse segments and string representation */
684 new->segments
= aspath
->segments
;
685 new->str
= aspath
->str
;
686 new->str_len
= aspath
->str_len
;
687 new->json
= aspath
->json
;
692 /* parse as-segment byte stream in struct assegment */
693 static int assegments_parse(struct stream
*s
, size_t length
,
694 struct assegment
**result
, int use32bit
)
696 struct assegment_header segh
;
697 struct assegment
*seg
, *prev
= NULL
, *head
= NULL
;
700 /* empty aspath (ie iBGP or somesuch) */
704 if (BGP_DEBUG(as4
, AS4_SEGMENT
))
706 "[AS4SEG] Parse aspath segment: got total byte length %lu",
707 (unsigned long)length
);
709 if ((STREAM_READABLE(s
) < length
)
710 || (STREAM_READABLE(s
) < AS_HEADER_SIZE
)
711 || (length
% AS16_VALUE_SIZE
))
714 while (bytes
< length
) {
718 if ((length
- bytes
) <= AS_HEADER_SIZE
) {
720 assegment_free_all(head
);
724 /* softly softly, get the header first on its own */
725 segh
.type
= stream_getc(s
);
726 segh
.length
= stream_getc(s
);
728 seg_size
= ASSEGMENT_SIZE(segh
.length
, use32bit
);
730 if (BGP_DEBUG(as4
, AS4_SEGMENT
))
732 "[AS4SEG] Parse aspath segment: got type %d, length %d",
733 segh
.type
, segh
.length
);
736 if (((bytes
+ seg_size
) > length
)
737 /* 1771bis 4.3b: seg length contains one or more */
738 || (segh
.length
== 0)
739 /* Paranoia in case someone changes type of segment length.
740 * Shift both values by 0x10 to make the comparison operate
741 * on more, than 8 bits (otherwise it's a warning, bug
744 || ((sizeof segh
.length
> 1)
745 && (0x10 + segh
.length
> 0x10 + AS_SEGMENT_MAX
))) {
747 assegment_free_all(head
);
754 case AS_CONFED_SEQUENCE
:
759 assegment_free_all(head
);
763 /* now its safe to trust lengths */
764 seg
= assegment_new(segh
.type
, segh
.length
);
768 else /* it's the first segment */
771 for (i
= 0; i
< segh
.length
; i
++)
773 (use32bit
) ? stream_getl(s
) : stream_getw(s
);
777 if (BGP_DEBUG(as4
, AS4_SEGMENT
))
779 "[AS4SEG] Parse aspath segment: Bytes now: %lu",
780 (unsigned long)bytes
);
785 *result
= assegment_normalise(head
);
789 /* AS path parse function. pnt is a pointer to byte stream and length
790 is length of byte stream. If there is same AS path in the the AS
791 path hash then return it else make new AS path structure.
793 On error NULL is returned.
795 struct aspath
*aspath_parse(struct stream
*s
, size_t length
, int use32bit
)
800 /* If length is odd it's malformed AS path. */
801 /* Nit-picking: if (use32bit == 0) it is malformed if odd,
802 * otherwise its malformed when length is larger than 2 and (length-2)
803 * is not dividable by 4.
804 * But... this time we're lazy
806 if (length
% AS16_VALUE_SIZE
)
809 memset(&as
, 0, sizeof(struct aspath
));
810 if (assegments_parse(s
, length
, &as
.segments
, use32bit
) < 0)
813 /* If already same aspath exist then return it. */
814 find
= hash_get(ashash
, &as
, aspath_hash_alloc
);
816 /* bug! should not happen, let the daemon crash below */
819 /* if the aspath was already hashed free temporary memory. */
821 assegment_free_all(as
.segments
);
822 /* aspath_key_make() always updates the string */
823 XFREE(MTYPE_AS_STR
, as
.str
);
825 json_object_free(as
.json
);
835 static void assegment_data_put(struct stream
*s
, as_t
*as
, int num
,
839 assert(num
<= AS_SEGMENT_MAX
);
841 for (i
= 0; i
< num
; i
++)
843 stream_putl(s
, as
[i
]);
845 if (as
[i
] <= BGP_AS_MAX
)
846 stream_putw(s
, as
[i
]);
848 stream_putw(s
, BGP_AS_TRANS
);
852 static size_t assegment_header_put(struct stream
*s
, u_char type
, int length
)
855 assert(length
<= AS_SEGMENT_MAX
);
856 stream_putc(s
, type
);
857 lenp
= stream_get_endp(s
);
858 stream_putc(s
, length
);
862 /* write aspath data to stream */
863 size_t aspath_put(struct stream
*s
, struct aspath
*as
, int use32bit
)
865 struct assegment
*seg
= as
->segments
;
868 if (!seg
|| seg
->length
== 0)
873 * Hey, what do we do when we have > STREAM_WRITABLE(s) here?
874 * At the moment, we would write out a partial aspath, and our
876 * will complain and drop the session :-/
878 * The general assumption here is that many things tested will
879 * never happen. And, in real live, up to now, they have not.
881 while (seg
&& (ASSEGMENT_LEN(seg
, use32bit
)
882 <= STREAM_WRITEABLE(s
))) {
883 struct assegment
*next
= seg
->next
;
888 /* Overlength segments have to be split up */
889 while ((seg
->length
- written
) > AS_SEGMENT_MAX
) {
890 assegment_header_put(s
, seg
->type
,
892 assegment_data_put(s
, seg
->as
, AS_SEGMENT_MAX
,
894 written
+= AS_SEGMENT_MAX
;
895 bytes
+= ASSEGMENT_SIZE(AS_SEGMENT_MAX
, use32bit
);
898 /* write the final segment, probably is also the first
900 lenp
= assegment_header_put(s
, seg
->type
,
901 seg
->length
- written
);
902 assegment_data_put(s
, (seg
->as
+ written
),
903 seg
->length
- written
, use32bit
);
905 /* Sequence-type segments can be 'packed' together
906 * Case of a segment which was overlength and split up
907 * will be missed here, but that doesn't matter.
909 while (next
&& ASSEGMENTS_PACKABLE(seg
, next
)) {
910 /* NB: We should never normally get here given
912 * normalise aspath data when parse them.
914 * safe than sorry. We potentially could call
915 * assegment_normalise here instead, but it's
917 * easier to do it on the fly here rather than
919 * the segment list twice every time we write
924 /* Next segment's data can fit in this one */
925 assegment_data_put(s
, next
->as
, next
->length
,
928 /* update the length of the segment header */
929 stream_putc_at(s
, lenp
,
930 seg
->length
- written
932 asns_packed
+= next
->length
;
937 bytes
+= ASSEGMENT_SIZE(
938 seg
->length
- written
+ asns_packed
, use32bit
);
945 /* This is for SNMP BGP4PATHATTRASPATHSEGMENT
946 * We have no way to manage the storage, so we use a static stream
947 * wrapper around aspath_put.
949 u_char
*aspath_snmp_pathseg(struct aspath
*as
, size_t *varlen
)
951 #define SNMP_PATHSEG_MAX 1024
954 snmp_stream
= stream_new(SNMP_PATHSEG_MAX
);
956 stream_reset(snmp_stream
);
962 aspath_put(snmp_stream
, as
, 0); /* use 16 bit for now here */
964 *varlen
= stream_get_endp(snmp_stream
);
965 return stream_pnt(snmp_stream
);
968 #define min(A,B) ((A) < (B) ? (A) : (B))
970 static struct assegment
*aspath_aggregate_as_set_add(struct aspath
*aspath
,
971 struct assegment
*asset
,
976 /* If this is first AS set member, create new as-set segment. */
978 asset
= assegment_new(AS_SET
, 1);
979 if (!aspath
->segments
)
980 aspath
->segments
= asset
;
982 struct assegment
*seg
= aspath
->segments
;
987 asset
->type
= AS_SET
;
991 /* Check this AS value already exists or not. */
992 for (i
= 0; i
< asset
->length
; i
++)
993 if (asset
->as
[i
] == as
)
997 asset
->as
= XREALLOC(MTYPE_AS_SEG_DATA
, asset
->as
,
998 asset
->length
* AS_VALUE_SIZE
);
999 asset
->as
[asset
->length
- 1] = as
;
1006 /* Modify as1 using as2 for aggregation. */
1007 struct aspath
*aspath_aggregate(struct aspath
*as1
, struct aspath
*as2
)
1013 struct assegment
*seg1
= as1
->segments
;
1014 struct assegment
*seg2
= as2
->segments
;
1015 struct aspath
*aspath
= NULL
;
1016 struct assegment
*asset
= NULL
;
1017 struct assegment
*prevseg
= NULL
;
1019 /* First of all check common leading sequence. */
1020 while (seg1
&& seg2
) {
1021 /* Check segment type. */
1022 if (seg1
->type
!= seg2
->type
)
1025 /* Minimum segment length. */
1026 minlen
= min(seg1
->length
, seg2
->length
);
1028 for (match
= 0; match
< minlen
; match
++)
1029 if (seg1
->as
[match
] != seg2
->as
[match
])
1033 struct assegment
*seg
= assegment_new(seg1
->type
, 0);
1035 seg
= assegment_append_asns(seg
, seg1
->as
, match
);
1038 aspath
= aspath_new();
1039 aspath
->segments
= seg
;
1041 prevseg
->next
= seg
;
1046 if (match
!= minlen
|| match
!= seg1
->length
1047 || seg1
->length
!= seg2
->length
)
1049 /* We are moving on to the next segment to reset match */
1058 aspath
= aspath_new();
1060 /* Make as-set using rest of all information. */
1063 for (i
= from
; i
< seg1
->length
; i
++)
1064 asset
= aspath_aggregate_as_set_add(aspath
, asset
,
1073 for (i
= from
; i
< seg2
->length
; i
++)
1074 asset
= aspath_aggregate_as_set_add(aspath
, asset
,
1081 assegment_normalise(aspath
->segments
);
1082 aspath_str_update(aspath
);
1086 /* When a BGP router receives an UPDATE with an MP_REACH_NLRI
1087 attribute, check the leftmost AS number in the AS_PATH attribute is
1088 or not the peer's AS number. */
1089 int aspath_firstas_check(struct aspath
*aspath
, as_t asno
)
1091 if ((aspath
== NULL
) || (aspath
->segments
== NULL
))
1094 if (aspath
->segments
&& (aspath
->segments
->type
== AS_SEQUENCE
)
1095 && (aspath
->segments
->as
[0] == asno
))
1101 unsigned int aspath_get_first_as(struct aspath
*aspath
)
1103 if (aspath
== NULL
|| aspath
->segments
== NULL
)
1106 return aspath
->segments
->as
[0];
1109 unsigned int aspath_get_last_as(struct aspath
*aspath
)
1112 unsigned int last_as
= 0;
1113 const struct assegment
*seg
;
1115 if (aspath
== NULL
|| aspath
->segments
== NULL
)
1118 seg
= aspath
->segments
;
1121 if (seg
->type
== AS_SEQUENCE
|| seg
->type
== AS_CONFED_SEQUENCE
)
1122 for (i
= 0; i
< seg
->length
; i
++)
1123 last_as
= seg
->as
[i
];
1130 /* AS path loop check. If aspath contains asno then return >= 1. */
1131 int aspath_loop_check(struct aspath
*aspath
, as_t asno
)
1133 struct assegment
*seg
;
1136 if ((aspath
== NULL
) || (aspath
->segments
== NULL
))
1139 seg
= aspath
->segments
;
1144 for (i
= 0; i
< seg
->length
; i
++)
1145 if (seg
->as
[i
] == asno
)
1153 /* When all of AS path is private AS return 1. */
1154 int aspath_private_as_check(struct aspath
*aspath
)
1156 struct assegment
*seg
;
1158 if (!(aspath
&& aspath
->segments
))
1161 seg
= aspath
->segments
;
1166 for (i
= 0; i
< seg
->length
; i
++) {
1167 if (!BGP_AS_IS_PRIVATE(seg
->as
[i
]))
1175 /* Return True if the entire ASPATH consist of the specified ASN */
1176 int aspath_single_asn_check(struct aspath
*aspath
, as_t asn
)
1178 struct assegment
*seg
;
1180 if (!(aspath
&& aspath
->segments
))
1183 seg
= aspath
->segments
;
1188 for (i
= 0; i
< seg
->length
; i
++) {
1189 if (seg
->as
[i
] != asn
)
1197 /* Replace all instances of the target ASN with our own ASN */
1198 struct aspath
*aspath_replace_specific_asn(struct aspath
*aspath
,
1199 as_t target_asn
, as_t our_asn
)
1202 struct assegment
*seg
;
1204 new = aspath_dup(aspath
);
1205 seg
= new->segments
;
1210 for (i
= 0; i
< seg
->length
; i
++) {
1211 if (seg
->as
[i
] == target_asn
)
1212 seg
->as
[i
] = our_asn
;
1217 aspath_str_update(new);
1221 /* Replace all private ASNs with our own ASN */
1222 struct aspath
*aspath_replace_private_asns(struct aspath
*aspath
, as_t asn
)
1225 struct assegment
*seg
;
1227 new = aspath_dup(aspath
);
1228 seg
= new->segments
;
1233 for (i
= 0; i
< seg
->length
; i
++) {
1234 if (BGP_AS_IS_PRIVATE(seg
->as
[i
]))
1240 aspath_str_update(new);
1244 /* Remove all private ASNs */
1245 struct aspath
*aspath_remove_private_asns(struct aspath
*aspath
)
1248 struct assegment
*seg
;
1249 struct assegment
*new_seg
;
1250 struct assegment
*last_new_seg
;
1255 new = XCALLOC(MTYPE_AS_PATH
, sizeof(struct aspath
));
1259 last_new_seg
= NULL
;
1260 seg
= aspath
->segments
;
1264 for (i
= 0; i
< seg
->length
; i
++) {
1266 if (!BGP_AS_IS_PRIVATE(seg
->as
[i
])) {
1272 // The entire segment is private so skip it
1278 // The entire segment is public so copy it
1279 else if (public == seg
->length
) {
1280 new_seg
= assegment_dup(seg
);
1283 // The segment is a mix of public and private ASNs. Copy as many
1285 // there are public ASNs then come back and fill in only the
1288 new_seg
= assegment_new(seg
->type
, public);
1290 for (i
= 0; i
< seg
->length
; i
++) {
1292 if (!BGP_AS_IS_PRIVATE(seg
->as
[i
])) {
1293 new_seg
->as
[j
] = seg
->as
[i
];
1299 // This is the first segment so set the aspath segments pointer
1302 new->segments
= new_seg
;
1304 last_new_seg
->next
= new_seg
;
1306 last_new_seg
= new_seg
;
1310 aspath_str_update(new);
1314 /* AS path confed check. If aspath contains confed set or sequence then return
1316 int aspath_confed_check(struct aspath
*aspath
)
1318 struct assegment
*seg
;
1320 if (!(aspath
&& aspath
->segments
))
1323 seg
= aspath
->segments
;
1326 if (seg
->type
== AS_CONFED_SET
1327 || seg
->type
== AS_CONFED_SEQUENCE
)
1334 /* Leftmost AS path segment confed check. If leftmost AS segment is of type
1335 AS_CONFED_SEQUENCE or AS_CONFED_SET then return 1. */
1336 int aspath_left_confed_check(struct aspath
*aspath
)
1339 if (!(aspath
&& aspath
->segments
))
1342 if ((aspath
->segments
->type
== AS_CONFED_SEQUENCE
)
1343 || (aspath
->segments
->type
== AS_CONFED_SET
))
1349 /* Merge as1 to as2. as2 should be uninterned aspath. */
1350 static struct aspath
*aspath_merge(struct aspath
*as1
, struct aspath
*as2
)
1352 struct assegment
*last
, *new;
1357 last
= new = assegment_dup_all(as1
->segments
);
1359 /* find the last valid segment */
1360 while (last
&& last
->next
)
1363 last
->next
= as2
->segments
;
1364 as2
->segments
= new;
1365 aspath_str_update(as2
);
1369 /* Prepend as1 to as2. as2 should be uninterned aspath. */
1370 struct aspath
*aspath_prepend(struct aspath
*as1
, struct aspath
*as2
)
1372 struct assegment
*seg1
;
1373 struct assegment
*seg2
;
1378 seg1
= as1
->segments
;
1379 seg2
= as2
->segments
;
1381 /* If as2 is empty, only need to dupe as1's chain onto as2 */
1383 as2
->segments
= assegment_dup_all(as1
->segments
);
1384 aspath_str_update(as2
);
1388 /* If as1 is empty AS, no prepending to do. */
1392 /* find the tail as1's segment chain. */
1393 while (seg1
&& seg1
->next
)
1396 /* Delete any AS_CONFED_SEQUENCE segment from as2. */
1397 if (seg1
->type
== AS_SEQUENCE
&& seg2
->type
== AS_CONFED_SEQUENCE
)
1398 as2
= aspath_delete_confed_seq(as2
);
1400 /* Compare last segment type of as1 and first segment type of as2. */
1401 if (seg1
->type
!= seg2
->type
)
1402 return aspath_merge(as1
, as2
);
1404 if (seg1
->type
== AS_SEQUENCE
) {
1405 /* We have two chains of segments, as1->segments and seg2,
1406 * and we have to attach them together, merging the attaching
1407 * segments together into one.
1409 * 1. dupe as1->segments onto head of as2
1410 * 2. merge seg2's asns onto last segment of this new chain
1411 * 3. attach chain after seg2
1414 /* dupe as1 onto as2's head */
1415 seg1
= as2
->segments
= assegment_dup_all(as1
->segments
);
1417 /* refind the tail of as2, reusing seg1 */
1418 while (seg1
&& seg1
->next
)
1421 /* merge the old head, seg2, into tail, seg1 */
1422 seg1
= assegment_append_asns(seg1
, seg2
->as
, seg2
->length
);
1424 /* bypass the merged seg2, and attach any chain after it to
1425 * chain descending from as2's head
1427 seg1
->next
= seg2
->next
;
1429 /* seg2 is now referenceless and useless*/
1430 assegment_free(seg2
);
1432 /* we've now prepended as1's segment chain to as2, merging
1433 * the inbetween AS_SEQUENCE of seg2 in the process
1435 aspath_str_update(as2
);
1438 /* AS_SET merge code is needed at here. */
1439 return aspath_merge(as1
, as2
);
1441 /* XXX: Ermmm, what if as1 has multiple segments?? */
1446 /* Iterate over AS_PATH segments and wipe all occurences of the
1447 * listed AS numbers. Hence some segments may lose some or even
1448 * all data on the way, the operation is implemented as a smarter
1449 * version of aspath_dup(), which allocates memory to hold the new
1450 * data, not the original. The new AS path is returned.
1452 struct aspath
*aspath_filter_exclude(struct aspath
*source
,
1453 struct aspath
*exclude_list
)
1455 struct assegment
*srcseg
, *exclseg
, *lastseg
;
1456 struct aspath
*newpath
;
1458 newpath
= aspath_new();
1461 for (srcseg
= source
->segments
; srcseg
; srcseg
= srcseg
->next
) {
1462 unsigned i
, y
, newlen
= 0, done
= 0, skip_as
;
1463 struct assegment
*newseg
;
1465 /* Find out, how much ASns are we going to pick from this
1467 * We can't perform filtering right inline, because the size of
1468 * the new segment isn't known at the moment yet.
1470 for (i
= 0; i
< srcseg
->length
; i
++) {
1472 for (exclseg
= exclude_list
->segments
;
1473 exclseg
&& !skip_as
; exclseg
= exclseg
->next
)
1474 for (y
= 0; y
< exclseg
->length
; y
++)
1475 if (srcseg
->as
[i
] == exclseg
->as
[y
]) {
1477 // There's no sense in testing
1478 // the rest of exclusion list,
1485 /* newlen is now the number of ASns to copy */
1489 /* Actual copying. Allocate memory and iterate once more,
1490 * performing filtering. */
1491 newseg
= assegment_new(srcseg
->type
, newlen
);
1492 for (i
= 0; i
< srcseg
->length
; i
++) {
1494 for (exclseg
= exclude_list
->segments
;
1495 exclseg
&& !skip_as
; exclseg
= exclseg
->next
)
1496 for (y
= 0; y
< exclseg
->length
; y
++)
1497 if (srcseg
->as
[i
] == exclseg
->as
[y
]) {
1503 newseg
->as
[done
++] = srcseg
->as
[i
];
1505 /* At his point newlen must be equal to done, and both must be
1507 * the filtered segment to the gross result. */
1509 newpath
->segments
= newseg
;
1511 lastseg
->next
= newseg
;
1514 aspath_str_update(newpath
);
1515 /* We are happy returning even an empty AS_PATH, because the
1517 * might expect this very behaviour. There's a mean to avoid this, if
1519 * by having a match rule against certain AS_PATH regexps in the
1522 aspath_free(source
);
1526 /* Add specified AS to the leftmost of aspath. */
1527 static struct aspath
*aspath_add_asns(struct aspath
*aspath
, as_t asno
,
1528 u_char type
, unsigned num
)
1530 struct assegment
*assegment
= aspath
->segments
;
1533 if (assegment
&& assegment
->type
== type
) {
1534 /* extend existing segment */
1536 assegment_prepend_asns(aspath
->segments
, asno
, num
);
1538 /* prepend with new segment */
1539 struct assegment
*newsegment
= assegment_new(type
, num
);
1540 for (i
= 0; i
< num
; i
++)
1541 newsegment
->as
[i
] = asno
;
1543 /* insert potentially replacing empty segment */
1544 if (assegment
&& assegment
->length
== 0) {
1545 newsegment
->next
= assegment
->next
;
1546 assegment_free(assegment
);
1548 newsegment
->next
= assegment
;
1549 aspath
->segments
= newsegment
;
1552 aspath_str_update(aspath
);
1556 /* Add specified AS to the leftmost of aspath num times. */
1557 struct aspath
*aspath_add_seq_n(struct aspath
*aspath
, as_t asno
, unsigned num
)
1559 return aspath_add_asns(aspath
, asno
, AS_SEQUENCE
, num
);
1562 /* Add specified AS to the leftmost of aspath. */
1563 struct aspath
*aspath_add_seq(struct aspath
*aspath
, as_t asno
)
1565 return aspath_add_asns(aspath
, asno
, AS_SEQUENCE
, 1);
1568 /* Compare leftmost AS value for MED check. If as1's leftmost AS and
1569 as2's leftmost AS is same return 1. */
1570 int aspath_cmp_left(const struct aspath
*aspath1
, const struct aspath
*aspath2
)
1572 const struct assegment
*seg1
;
1573 const struct assegment
*seg2
;
1575 if (!(aspath1
&& aspath2
))
1578 seg1
= aspath1
->segments
;
1579 seg2
= aspath2
->segments
;
1581 /* If both paths are originated in this AS then we do want to compare
1586 /* find first non-confed segments for each */
1587 while (seg1
&& ((seg1
->type
== AS_CONFED_SEQUENCE
)
1588 || (seg1
->type
== AS_CONFED_SET
)))
1591 while (seg2
&& ((seg2
->type
== AS_CONFED_SEQUENCE
)
1592 || (seg2
->type
== AS_CONFED_SET
)))
1596 if (!(seg1
&& seg2
&& (seg1
->type
== AS_SEQUENCE
)
1597 && (seg2
->type
== AS_SEQUENCE
)))
1600 if (seg1
->as
[0] == seg2
->as
[0])
1606 /* Truncate an aspath after a number of hops, and put the hops remaining
1607 * at the front of another aspath. Needed for AS4 compat.
1609 * Returned aspath is a /new/ aspath, which should either by free'd or
1610 * interned by the caller, as desired.
1612 struct aspath
*aspath_reconcile_as4(struct aspath
*aspath
,
1613 struct aspath
*as4path
)
1615 struct assegment
*seg
, *newseg
, *prevseg
= NULL
;
1616 struct aspath
*newpath
= NULL
, *mergedpath
;
1617 int hops
, cpasns
= 0;
1622 seg
= aspath
->segments
;
1624 /* CONFEDs should get reconciled too.. */
1625 hops
= (aspath_count_hops(aspath
) + aspath_count_confeds(aspath
))
1626 - aspath_count_hops(as4path
);
1629 if (BGP_DEBUG(as4
, AS4
))
1631 "[AS4] Fewer hops in AS_PATH than NEW_AS_PATH");
1632 /* Something's gone wrong. The RFC says we should now ignore
1634 * which is daft behaviour - it contains vital loop-detection
1635 * information which must have been removed from AS_PATH.
1637 hops
= aspath_count_hops(aspath
);
1641 newpath
= aspath_dup(as4path
);
1642 aspath_str_update(newpath
);
1646 if (BGP_DEBUG(as4
, AS4
))
1648 "[AS4] got AS_PATH %s and AS4_PATH %s synthesizing now",
1649 aspath
->str
, as4path
->str
);
1651 while (seg
&& hops
> 0) {
1652 switch (seg
->type
) {
1656 cpasns
= seg
->length
;
1658 case AS_CONFED_SEQUENCE
:
1659 /* Should never split a confed-sequence, if hop-count
1660 * suggests we must then something's gone wrong
1663 * Most important goal is to preserve AS_PATHs prime
1665 * as loop-detector, so we fudge the numbers so that the
1667 * confed-sequence is merged in.
1669 if (hops
< seg
->length
) {
1670 if (BGP_DEBUG(as4
, AS4
))
1672 "[AS4] AS4PATHmangle: AS_CONFED_SEQUENCE falls"
1673 " across 2/4 ASN boundary somewhere, broken..");
1678 cpasns
= MIN(seg
->length
, hops
);
1679 hops
-= seg
->length
;
1682 assert(cpasns
<= seg
->length
);
1684 newseg
= assegment_new(seg
->type
, 0);
1685 newseg
= assegment_append_asns(newseg
, seg
->as
, cpasns
);
1688 newpath
= aspath_new();
1689 newpath
->segments
= newseg
;
1691 prevseg
->next
= newseg
;
1697 /* We may be able to join some segments here, and we must
1698 * do this because... we want normalised aspaths in out hash
1699 * and we do not want to stumble in aspath_put.
1701 mergedpath
= aspath_merge(newpath
, aspath_dup(as4path
));
1702 aspath_free(newpath
);
1703 mergedpath
->segments
= assegment_normalise(mergedpath
->segments
);
1704 aspath_str_update(mergedpath
);
1706 if (BGP_DEBUG(as4
, AS4
))
1707 zlog_debug("[AS4] result of synthesizing is %s",
1713 /* Compare leftmost AS value for MED check. If as1's leftmost AS and
1714 as2's leftmost AS is same return 1. (confederation as-path
1716 int aspath_cmp_left_confed(const struct aspath
*aspath1
,
1717 const struct aspath
*aspath2
)
1719 if (!(aspath1
&& aspath2
))
1722 if (!(aspath1
->segments
&& aspath2
->segments
))
1725 if ((aspath1
->segments
->type
!= AS_CONFED_SEQUENCE
)
1726 || (aspath2
->segments
->type
!= AS_CONFED_SEQUENCE
))
1729 if (aspath1
->segments
->as
[0] == aspath2
->segments
->as
[0])
1735 /* Delete all AS_CONFED_SEQUENCE/SET segments from aspath.
1736 * RFC 5065 section 4.1.c.1
1738 * 1) if any path segments of the AS_PATH are of the type
1739 * AS_CONFED_SEQUENCE or AS_CONFED_SET, those segments MUST be
1740 * removed from the AS_PATH attribute, leaving the sanitized
1741 * AS_PATH attribute to be operated on by steps 2, 3 or 4.
1743 struct aspath
*aspath_delete_confed_seq(struct aspath
*aspath
)
1745 struct assegment
*seg
, *prev
, *next
;
1746 char removed_confed_segment
;
1748 if (!(aspath
&& aspath
->segments
))
1751 seg
= aspath
->segments
;
1752 removed_confed_segment
= 0;
1759 if (seg
->type
== AS_CONFED_SEQUENCE
1760 || seg
->type
== AS_CONFED_SET
) {
1761 /* This is the first segment in the aspath */
1762 if (aspath
->segments
== seg
)
1763 aspath
->segments
= seg
->next
;
1765 prev
->next
= seg
->next
;
1767 assegment_free(seg
);
1768 removed_confed_segment
= 1;
1775 if (removed_confed_segment
)
1776 aspath_str_update(aspath
);
1781 /* Add new AS number to the leftmost part of the aspath as
1782 AS_CONFED_SEQUENCE. */
1783 struct aspath
*aspath_add_confed_seq(struct aspath
*aspath
, as_t asno
)
1785 return aspath_add_asns(aspath
, asno
, AS_CONFED_SEQUENCE
, 1);
1788 /* Add new as value to as path structure. */
1789 static void aspath_as_add(struct aspath
*as
, as_t asno
)
1791 struct assegment
*seg
= as
->segments
;
1796 /* Last segment search procedure. */
1800 assegment_append_asns(seg
, &asno
, 1);
1803 /* Add new as segment to the as path. */
1804 static void aspath_segment_add(struct aspath
*as
, int type
)
1806 struct assegment
*seg
= as
->segments
;
1807 struct assegment
*new = assegment_new(type
, 0);
1817 struct aspath
*aspath_empty(void)
1819 return aspath_parse(NULL
, 0, 1); /* 32Bit ;-) */
1822 struct aspath
*aspath_empty_get(void)
1824 struct aspath
*aspath
;
1826 aspath
= aspath_new();
1827 aspath_make_str_count(aspath
);
1831 unsigned long aspath_count(void)
1833 return ashash
->count
;
1837 Theoretically, one as path can have:
1839 One BGP packet size should be less than 4096.
1840 One BGP attribute size should be less than 4096 - BGP header size.
1841 One BGP aspath size should be less than 4096 - BGP header size -
1842 BGP mandantry attribute size.
1845 /* AS path string lexical token enum. */
1850 as_token_confed_seq_start
,
1851 as_token_confed_seq_end
,
1852 as_token_confed_set_start
,
1853 as_token_confed_set_end
,
1857 /* Return next token and point for string parse. */
1858 static const char *aspath_gettoken(const char *buf
, enum as_token
*token
,
1861 const char *p
= buf
;
1863 /* Skip seperators (space for sequences, ',' for sets). */
1864 while (isspace((int)*p
) || *p
== ',')
1867 /* Check the end of the string and type specify characters
1873 *token
= as_token_set_start
;
1877 *token
= as_token_set_end
;
1881 *token
= as_token_confed_seq_start
;
1885 *token
= as_token_confed_seq_end
;
1889 *token
= as_token_confed_set_start
;
1893 *token
= as_token_confed_set_end
;
1898 /* Check actual AS value. */
1899 if (isdigit((int)*p
)) {
1902 *token
= as_token_asval
;
1906 while (isdigit((int)*p
)) {
1908 asval
+= (*p
- '0');
1915 /* There is no match then return unknown token. */
1916 *token
= as_token_unknown
;
1920 struct aspath
*aspath_str2aspath(const char *str
)
1922 enum as_token token
= as_token_unknown
;
1925 struct aspath
*aspath
;
1928 aspath
= aspath_new();
1930 /* We start default type as AS_SEQUENCE. */
1931 as_type
= AS_SEQUENCE
;
1934 while ((str
= aspath_gettoken(str
, &token
, &asno
)) != NULL
) {
1936 case as_token_asval
:
1938 aspath_segment_add(aspath
, as_type
);
1941 aspath_as_add(aspath
, asno
);
1943 case as_token_set_start
:
1945 aspath_segment_add(aspath
, as_type
);
1948 case as_token_set_end
:
1949 as_type
= AS_SEQUENCE
;
1952 case as_token_confed_seq_start
:
1953 as_type
= AS_CONFED_SEQUENCE
;
1954 aspath_segment_add(aspath
, as_type
);
1957 case as_token_confed_seq_end
:
1958 as_type
= AS_SEQUENCE
;
1961 case as_token_confed_set_start
:
1962 as_type
= AS_CONFED_SET
;
1963 aspath_segment_add(aspath
, as_type
);
1966 case as_token_confed_set_end
:
1967 as_type
= AS_SEQUENCE
;
1970 case as_token_unknown
:
1972 aspath_free(aspath
);
1977 aspath_make_str_count(aspath
);
1982 /* Make hash value by raw aspath data. */
1983 unsigned int aspath_key_make(void *p
)
1985 struct aspath
*aspath
= (struct aspath
*)p
;
1986 unsigned int key
= 0;
1989 aspath_str_update(aspath
);
1991 key
= jhash(aspath
->str
, aspath
->str_len
, 2334325);
1996 /* If two aspath have same value then return 1 else return 0 */
1997 int aspath_cmp(const void *arg1
, const void *arg2
)
1999 const struct assegment
*seg1
= ((const struct aspath
*)arg1
)->segments
;
2000 const struct assegment
*seg2
= ((const struct aspath
*)arg2
)->segments
;
2002 while (seg1
|| seg2
) {
2004 if ((!seg1
&& seg2
) || (seg1
&& !seg2
))
2006 if (seg1
->type
!= seg2
->type
)
2008 if (seg1
->length
!= seg2
->length
)
2010 for (i
= 0; i
< seg1
->length
; i
++)
2011 if (seg1
->as
[i
] != seg2
->as
[i
])
2019 /* AS path hash initialize. */
2020 void aspath_init(void)
2022 ashash
= hash_create_size(32768,
2028 void aspath_finish(void)
2030 hash_clean(ashash
, (void (*)(void *))aspath_free
);
2035 stream_free(snmp_stream
);
2038 /* return and as path value */
2039 const char *aspath_print(struct aspath
*as
)
2041 return (as
? as
->str
: NULL
);
2044 /* Printing functions */
2045 /* Feed the AS_PATH to the vty; the suffix string follows it only in case
2046 * AS_PATH wasn't empty.
2048 void aspath_print_vty(struct vty
*vty
, const char *format
, struct aspath
*as
,
2052 vty_out(vty
, format
, as
->str
);
2053 if (as
->str_len
&& strlen(suffix
))
2054 vty_out(vty
, "%s", suffix
);
2057 static void aspath_show_all_iterator(struct hash_backet
*backet
,
2062 as
= (struct aspath
*)backet
->data
;
2064 vty_out(vty
, "[%p:%u] (%ld) ", (void *)backet
, backet
->key
, as
->refcnt
);
2065 vty_out(vty
, "%s\n", as
->str
);
2068 /* Print all aspath and hash information. This function is used from
2069 `show [ip] bgp paths' command. */
2070 void aspath_print_all_vty(struct vty
*vty
)
2072 hash_iterate(ashash
, (void (*)(struct hash_backet
*,
2073 void *))aspath_show_all_iterator
,