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"
38 #include "bgpd/bgp_errors.h"
40 /* Attr. Flags and Attr. Type Code. */
41 #define AS_HEADER_SIZE 2
43 /* Now FOUR octets are used for AS value. */
44 #define AS_VALUE_SIZE sizeof(as_t)
45 /* This is the old one */
46 #define AS16_VALUE_SIZE sizeof(as16_t)
48 /* Maximum protocol segment length value */
49 #define AS_SEGMENT_MAX 255
51 /* The following length and size macros relate specifically to Quagga's
52 * internal representation of AS-Segments, not per se to the on-wire
53 * sizes and lengths. At present (200508) they sort of match, however
54 * the ONLY functions which should now about the on-wire syntax are
55 * aspath_put, assegment_put and assegment_parse.
57 * aspath_put returns bytes written, the only definitive record of
58 * size of wire-format attribute..
61 /* Calculated size in bytes of ASN segment data to hold N ASN's */
62 #define ASSEGMENT_DATA_SIZE(N, S) \
63 ((N) * ((S) ? AS_VALUE_SIZE : AS16_VALUE_SIZE))
65 /* Calculated size of segment struct to hold N ASN's */
66 #define ASSEGMENT_SIZE(N,S) (AS_HEADER_SIZE + ASSEGMENT_DATA_SIZE (N,S))
68 /* AS segment octet length. */
69 #define ASSEGMENT_LEN(X,S) ASSEGMENT_SIZE((X)->length,S)
71 /* AS_SEQUENCE segments can be packed together */
72 /* Can the types of X and Y be considered for packing? */
73 #define ASSEGMENT_TYPES_PACKABLE(X, Y) \
74 (((X)->type == (Y)->type) && ((X)->type == AS_SEQUENCE))
75 /* Types and length of X,Y suitable for packing? */
76 #define ASSEGMENTS_PACKABLE(X, Y) \
77 (ASSEGMENT_TYPES_PACKABLE((X), (Y)) \
78 && (((X)->length + (Y)->length) <= AS_SEGMENT_MAX))
80 /* As segment header - the on-wire representation
81 * NOT the internal representation!
83 struct assegment_header
{
88 /* Hash for aspath. This is the top level structure of AS path. */
89 static struct hash
*ashash
;
91 /* Stream for SNMP. See aspath_snmp_pathseg */
92 static struct stream
*snmp_stream
;
94 /* Callers are required to initialize the memory */
95 static as_t
*assegment_data_new(int num
)
97 return (XMALLOC(MTYPE_AS_SEG_DATA
, ASSEGMENT_DATA_SIZE(num
, 1)));
100 static void assegment_data_free(as_t
*asdata
)
102 XFREE(MTYPE_AS_SEG_DATA
, asdata
);
105 const char *const aspath_segment_type_str
[] = {
106 "as-invalid", "as-set", "as-sequence", "as-confed-sequence",
110 /* Get a new segment. Note that 0 is an allowed length,
111 * and will result in a segment with no allocated data segment.
112 * the caller should immediately assign data to the segment, as the segment
113 * otherwise is not generally valid
115 static struct assegment
*assegment_new(uint8_t type
, unsigned short length
)
117 struct assegment
*new;
119 new = XCALLOC(MTYPE_AS_SEG
, sizeof(struct assegment
));
122 new->as
= assegment_data_new(length
);
124 new->length
= length
;
130 static void assegment_free(struct assegment
*seg
)
135 assegment_data_free(seg
->as
);
136 memset(seg
, 0xfe, sizeof(struct assegment
));
137 XFREE(MTYPE_AS_SEG
, seg
);
142 /* free entire chain of segments */
143 static void assegment_free_all(struct assegment
*seg
)
145 struct assegment
*prev
;
150 assegment_free(prev
);
154 /* Duplicate just the given assegment and its data */
155 static struct assegment
*assegment_dup(struct assegment
*seg
)
157 struct assegment
*new;
159 new = assegment_new(seg
->type
, seg
->length
);
160 memcpy(new->as
, seg
->as
, ASSEGMENT_DATA_SIZE(new->length
, 1));
165 /* Duplicate entire chain of assegments, return the head */
166 static struct assegment
*assegment_dup_all(struct assegment
*seg
)
168 struct assegment
*new = NULL
;
169 struct assegment
*head
= NULL
;
173 new->next
= assegment_dup(seg
);
176 head
= new = assegment_dup(seg
);
183 /* prepend the as number to given segment, given num of times */
184 static struct assegment
*assegment_prepend_asns(struct assegment
*seg
,
193 if (num
>= AS_SEGMENT_MAX
)
194 return seg
; /* we don't do huge prepends */
196 newas
= assegment_data_new(seg
->length
+ num
);
200 for (i
= 0; i
< num
; i
++)
203 memcpy(newas
+ num
, seg
->as
, ASSEGMENT_DATA_SIZE(seg
->length
, 1));
204 assegment_data_free(seg
->as
);
211 /* append given array of as numbers to the segment */
212 static struct assegment
*assegment_append_asns(struct assegment
*seg
,
213 as_t
*asnos
, int num
)
220 newas
= XREALLOC(MTYPE_AS_SEG_DATA
, seg
->as
,
221 ASSEGMENT_DATA_SIZE(seg
->length
+ num
, 1));
224 memcpy(seg
->as
+ seg
->length
, asnos
,
225 ASSEGMENT_DATA_SIZE(num
, 1));
230 static int int_cmp(const void *p1
, const void *p2
)
232 const as_t
*as1
= p1
;
233 const as_t
*as2
= p2
;
235 return (*as1
== *as2
) ? 0 : ((*as1
> *as2
) ? 1 : -1);
238 /* normalise the segment.
239 * In particular, merge runs of AS_SEQUENCEs into one segment
240 * Internally, we do not care about the wire segment length limit, and
241 * we want each distinct AS_PATHs to have the exact same internal
242 * representation - eg, so that our hashing actually works..
244 static struct assegment
*assegment_normalise(struct assegment
*head
)
246 struct assegment
*seg
= head
, *pin
;
247 struct assegment
*tmp
;
255 /* Sort values SET segments, for determinism in paths to aid
256 * creation of hash values / path comparisons
257 * and because it helps other lesser implementations ;)
259 if (seg
->type
== AS_SET
|| seg
->type
== AS_CONFED_SET
) {
263 qsort(seg
->as
, seg
->length
, sizeof(as_t
), int_cmp
);
266 for (i
= 1; i
< seg
->length
; i
++) {
267 if (seg
->as
[tail
] == seg
->as
[i
])
272 seg
->as
[tail
] = seg
->as
[i
];
274 /* seg->length can be 0.. */
276 seg
->length
= tail
+ 1;
279 /* read ahead from the current, pinned segment while the
281 * are packable/mergeable. Append all following packable
283 * to the segment we have pinned and remove these appended
286 while (pin
->next
&& ASSEGMENT_TYPES_PACKABLE(pin
, pin
->next
)) {
290 /* append the next sequence to the pinned sequence */
291 pin
= assegment_append_asns(pin
, seg
->as
, seg
->length
);
293 /* bypass the next sequence */
294 pin
->next
= seg
->next
;
296 /* get rid of the now referenceless segment */
305 static struct aspath
*aspath_new(void)
307 return XCALLOC(MTYPE_AS_PATH
, sizeof(struct aspath
));
310 /* Free AS path structure. */
311 void aspath_free(struct aspath
*aspath
)
315 if (aspath
->segments
)
316 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
)
341 if (asp
->refcnt
== 0) {
342 /* This aspath must exist in aspath hash table. */
343 ret
= hash_release(ashash
, asp
);
350 /* Return the start or end delimiters for a particular Segment type */
351 #define AS_SEG_START 0
353 static char aspath_delimiter_char(uint8_t type
, uint8_t which
)
360 } aspath_delim_char
[] = {{AS_SET
, '{', '}'},
361 {AS_CONFED_SET
, '[', ']'},
362 {AS_CONFED_SEQUENCE
, '(', ')'},
365 for (i
= 0; aspath_delim_char
[i
].type
!= 0; i
++) {
366 if (aspath_delim_char
[i
].type
== type
) {
367 if (which
== AS_SEG_START
)
368 return aspath_delim_char
[i
].start
;
369 else if (which
== AS_SEG_END
)
370 return aspath_delim_char
[i
].end
;
376 /* countup asns from this segment and index onward */
377 static int assegment_count_asns(struct assegment
*seg
, int from
)
382 count
+= seg
->length
;
384 count
+= (seg
->length
- from
);
392 unsigned int aspath_count_confeds(struct aspath
*aspath
)
395 struct assegment
*seg
= aspath
->segments
;
398 if (seg
->type
== AS_CONFED_SEQUENCE
)
399 count
+= seg
->length
;
400 else if (seg
->type
== AS_CONFED_SET
)
408 unsigned int aspath_count_hops(const struct aspath
*aspath
)
411 struct assegment
*seg
= aspath
->segments
;
414 if (seg
->type
== AS_SEQUENCE
)
415 count
+= seg
->length
;
416 else if (seg
->type
== AS_SET
)
424 /* Check if aspath has AS_SET or AS_CONFED_SET */
425 bool aspath_check_as_sets(struct aspath
*aspath
)
427 struct assegment
*seg
= aspath
->segments
;
430 if (seg
->type
== AS_SET
|| seg
->type
== AS_CONFED_SET
)
437 /* Check if aspath has BGP_AS_ZERO */
438 bool aspath_check_as_zero(struct aspath
*aspath
)
440 struct assegment
*seg
= aspath
->segments
;
444 for (i
= 0; i
< seg
->length
; i
++)
445 if (seg
->as
[i
] == BGP_AS_ZERO
)
453 /* Estimate size aspath /might/ take if encoded into an
456 * This is a quick estimate, not definitive! aspath_put()
457 * may return a different number!!
459 unsigned int aspath_size(struct aspath
*aspath
)
462 struct assegment
*seg
= aspath
->segments
;
465 size
+= ASSEGMENT_SIZE(seg
->length
, 1);
471 /* Return highest public ASN in path */
472 as_t
aspath_highest(struct aspath
*aspath
)
474 struct assegment
*seg
= aspath
->segments
;
479 for (i
= 0; i
< seg
->length
; i
++)
480 if (seg
->as
[i
] > highest
481 && !BGP_AS_IS_PRIVATE(seg
->as
[i
]))
482 highest
= seg
->as
[i
];
488 /* Return the left-most ASN in path */
489 as_t
aspath_leftmost(struct aspath
*aspath
)
491 struct assegment
*seg
= aspath
->segments
;
494 if (seg
&& seg
->length
&& seg
->type
== AS_SEQUENCE
)
495 leftmost
= seg
->as
[0];
500 /* Return 1 if there are any 4-byte ASes in the path */
501 bool aspath_has_as4(struct aspath
*aspath
)
503 struct assegment
*seg
= aspath
->segments
;
507 for (i
= 0; i
< seg
->length
; i
++)
508 if (seg
->as
[i
] > BGP_AS_MAX
)
515 /* Convert aspath structure to string expression. */
516 static void aspath_make_str_count(struct aspath
*as
, bool make_json
)
518 struct assegment
*seg
;
522 json_object
*jaspath_segments
= NULL
;
523 json_object
*jseg
= NULL
;
524 json_object
*jseg_list
= NULL
;
527 as
->json
= json_object_new_object();
528 jaspath_segments
= json_object_new_array();
534 json_object_string_add(as
->json
, "string", "Local");
535 json_object_object_add(as
->json
, "segments",
537 json_object_int_add(as
->json
, "length", 0);
539 as
->str
= XMALLOC(MTYPE_AS_STR
, 1);
547 /* ASN takes 5 to 10 chars plus separator, see below.
548 * If there is one differing segment type, we need an additional
549 * 2 chars for segment delimiters, and the final '\0'.
550 * Hopefully this is large enough to avoid hitting the realloc
551 * code below for most common sequences.
553 * This was changed to 10 after the well-known BGP assertion, which
554 * had hit some parts of the Internet in May of 2009.
556 #define ASN_STR_LEN (10 + 1)
557 str_size
= MAX(assegment_count_asns(seg
, 0) * ASN_STR_LEN
+ 2 + 1,
558 ASPATH_STR_DEFAULT_LEN
);
559 str_buf
= XMALLOC(MTYPE_AS_STR
, str_size
);
565 /* Check AS type validity. Set separator for segment */
572 case AS_CONFED_SEQUENCE
:
576 XFREE(MTYPE_AS_STR
, str_buf
);
579 json_object_free(as
->json
);
585 /* We might need to increase str_buf, particularly if path has
586 * differing segments types, our initial guesstimate above will
587 * have been wrong. Need 10 chars for ASN, a separator each and
588 * potentially two segment delimiters, plus a space between each
589 * segment and trailing zero.
591 * This definitely didn't work with the value of 5 bytes and
594 #define SEGMENT_STR_LEN(X) (((X)->length * ASN_STR_LEN) + 2 + 1 + 1)
595 if ((len
+ SEGMENT_STR_LEN(seg
)) > str_size
) {
596 str_size
= len
+ SEGMENT_STR_LEN(seg
);
597 str_buf
= XREALLOC(MTYPE_AS_STR
, str_buf
, str_size
);
600 #undef SEGMENT_STR_LEN
602 if (seg
->type
!= AS_SEQUENCE
)
604 str_buf
+ len
, str_size
- len
, "%c",
605 aspath_delimiter_char(seg
->type
, AS_SEG_START
));
608 jseg_list
= json_object_new_array();
610 /* write out the ASNs, with their separators, bar the last one*/
611 for (i
= 0; i
< seg
->length
; i
++) {
613 json_object_array_add(
615 json_object_new_int64(seg
->as
[i
]));
617 len
+= snprintf(str_buf
+ len
, str_size
- len
, "%u",
620 if (i
< (seg
->length
- 1))
621 len
+= snprintf(str_buf
+ len
, str_size
- len
,
626 jseg
= json_object_new_object();
627 json_object_string_add(
629 aspath_segment_type_str
[seg
->type
]);
630 json_object_object_add(jseg
, "list", jseg_list
);
631 json_object_array_add(jaspath_segments
, jseg
);
634 if (seg
->type
!= AS_SEQUENCE
)
636 str_buf
+ len
, str_size
- len
, "%c",
637 aspath_delimiter_char(seg
->type
, AS_SEG_END
));
639 len
+= snprintf(str_buf
+ len
, str_size
- len
, " ");
644 assert(len
< str_size
);
651 json_object_string_add(as
->json
, "string", str_buf
);
652 json_object_object_add(as
->json
, "segments", jaspath_segments
);
653 json_object_int_add(as
->json
, "length", aspath_count_hops(as
));
659 void aspath_str_update(struct aspath
*as
, bool make_json
)
661 XFREE(MTYPE_AS_STR
, as
->str
);
664 json_object_free(as
->json
);
668 aspath_make_str_count(as
, make_json
);
671 /* Intern allocated AS path. */
672 struct aspath
*aspath_intern(struct aspath
*aspath
)
676 /* Assert this AS path structure is not interned and has the string
677 representation built. */
678 assert(aspath
->refcnt
== 0);
681 /* Check AS path hash. */
682 find
= hash_get(ashash
, aspath
, hash_alloc_intern
);
691 /* Duplicate aspath structure. Created same aspath structure but
692 reference count and AS path string is cleared. */
693 struct aspath
*aspath_dup(struct aspath
*aspath
)
695 unsigned short buflen
= aspath
->str_len
+ 1;
698 new = XCALLOC(MTYPE_AS_PATH
, sizeof(struct aspath
));
701 if (aspath
->segments
)
702 new->segments
= assegment_dup_all(aspath
->segments
);
707 new->str
= XMALLOC(MTYPE_AS_STR
, buflen
);
708 new->str_len
= aspath
->str_len
;
710 /* copy the string data */
711 if (aspath
->str_len
> 0)
712 memcpy(new->str
, aspath
->str
, buflen
);
719 static void *aspath_hash_alloc(void *arg
)
721 const struct aspath
*aspath
= arg
;
724 /* Malformed AS path value. */
727 /* New aspath structure is needed. */
728 new = XMALLOC(MTYPE_AS_PATH
, sizeof(struct aspath
));
730 /* Reuse segments and string representation */
732 new->segments
= aspath
->segments
;
733 new->str
= aspath
->str
;
734 new->str_len
= aspath
->str_len
;
735 new->json
= aspath
->json
;
740 /* parse as-segment byte stream in struct assegment */
741 static int assegments_parse(struct stream
*s
, size_t length
,
742 struct assegment
**result
, int use32bit
)
744 struct assegment_header segh
;
745 struct assegment
*seg
, *prev
= NULL
, *head
= NULL
;
748 /* empty aspath (ie iBGP or somesuch) */
752 if (BGP_DEBUG(as4
, AS4_SEGMENT
))
754 "[AS4SEG] Parse aspath segment: got total byte length %lu",
755 (unsigned long)length
);
757 if ((STREAM_READABLE(s
) < length
)
758 || (STREAM_READABLE(s
) < AS_HEADER_SIZE
)
759 || (length
% AS16_VALUE_SIZE
))
762 while (bytes
< length
) {
766 if ((length
- bytes
) <= AS_HEADER_SIZE
) {
768 assegment_free_all(head
);
772 /* softly softly, get the header first on its own */
773 segh
.type
= stream_getc(s
);
774 segh
.length
= stream_getc(s
);
776 seg_size
= ASSEGMENT_SIZE(segh
.length
, use32bit
);
778 if (BGP_DEBUG(as4
, AS4_SEGMENT
))
780 "[AS4SEG] Parse aspath segment: got type %d, length %d",
781 segh
.type
, segh
.length
);
784 if (((bytes
+ seg_size
) > length
)
785 /* 1771bis 4.3b: seg length contains one or more */
786 || (segh
.length
== 0)
787 /* Paranoia in case someone changes type of segment length.
788 * Shift both values by 0x10 to make the comparison operate
789 * on more, than 8 bits (otherwise it's a warning, bug
792 || ((sizeof(segh
.length
) > 1)
793 && (0x10 + segh
.length
> 0x10 + AS_SEGMENT_MAX
))) {
795 assegment_free_all(head
);
802 case AS_CONFED_SEQUENCE
:
807 assegment_free_all(head
);
811 /* now its safe to trust lengths */
812 seg
= assegment_new(segh
.type
, segh
.length
);
816 else /* it's the first segment */
819 for (i
= 0; i
< segh
.length
; i
++)
821 (use32bit
) ? stream_getl(s
) : stream_getw(s
);
825 if (BGP_DEBUG(as4
, AS4_SEGMENT
))
827 "[AS4SEG] Parse aspath segment: Bytes now: %lu",
828 (unsigned long)bytes
);
833 *result
= assegment_normalise(head
);
837 /* AS path parse function. pnt is a pointer to byte stream and length
838 is length of byte stream. If there is same AS path in the the AS
839 path hash then return it else make new AS path structure.
841 On error NULL is returned.
843 struct aspath
*aspath_parse(struct stream
*s
, size_t length
, int use32bit
)
848 /* If length is odd it's malformed AS path. */
849 /* Nit-picking: if (use32bit == 0) it is malformed if odd,
850 * otherwise its malformed when length is larger than 2 and (length-2)
851 * is not dividable by 4.
852 * But... this time we're lazy
854 if (length
% AS16_VALUE_SIZE
)
857 memset(&as
, 0, sizeof(struct aspath
));
858 if (assegments_parse(s
, length
, &as
.segments
, use32bit
) < 0)
861 /* If already same aspath exist then return it. */
862 find
= hash_get(ashash
, &as
, aspath_hash_alloc
);
864 /* bug! should not happen, let the daemon crash below */
867 /* if the aspath was already hashed free temporary memory. */
869 assegment_free_all(as
.segments
);
870 /* aspath_key_make() always updates the string */
871 XFREE(MTYPE_AS_STR
, as
.str
);
873 json_object_free(as
.json
);
883 static void assegment_data_put(struct stream
*s
, as_t
*as
, int num
,
887 assert(num
<= AS_SEGMENT_MAX
);
889 for (i
= 0; i
< num
; i
++)
891 stream_putl(s
, as
[i
]);
893 if (as
[i
] <= BGP_AS_MAX
)
894 stream_putw(s
, as
[i
]);
896 stream_putw(s
, BGP_AS_TRANS
);
900 static size_t assegment_header_put(struct stream
*s
, uint8_t type
, int length
)
903 assert(length
<= AS_SEGMENT_MAX
);
904 stream_putc(s
, type
);
905 lenp
= stream_get_endp(s
);
906 stream_putc(s
, length
);
910 /* write aspath data to stream */
911 size_t aspath_put(struct stream
*s
, struct aspath
*as
, int use32bit
)
913 struct assegment
*seg
= as
->segments
;
916 if (!seg
|| seg
->length
== 0)
920 * Hey, what do we do when we have > STREAM_WRITABLE(s) here?
921 * At the moment, we would write out a partial aspath, and our
923 * will complain and drop the session :-/
925 * The general assumption here is that many things tested will
926 * never happen. And, in real live, up to now, they have not.
928 while (seg
&& (ASSEGMENT_LEN(seg
, use32bit
) <= STREAM_WRITEABLE(s
))) {
929 struct assegment
*next
= seg
->next
;
934 /* Overlength segments have to be split up */
935 while ((seg
->length
- written
) > AS_SEGMENT_MAX
) {
936 assegment_header_put(s
, seg
->type
, AS_SEGMENT_MAX
);
937 assegment_data_put(s
, (seg
->as
+ written
),
938 AS_SEGMENT_MAX
, use32bit
);
939 written
+= AS_SEGMENT_MAX
;
940 bytes
+= ASSEGMENT_SIZE(AS_SEGMENT_MAX
, use32bit
);
943 /* write the final segment, probably is also the first
945 lenp
= assegment_header_put(s
, seg
->type
,
946 seg
->length
- written
);
947 assegment_data_put(s
, (seg
->as
+ written
),
948 seg
->length
- written
, use32bit
);
950 /* Sequence-type segments can be 'packed' together
951 * Case of a segment which was overlength and split up
952 * will be missed here, but that doesn't matter.
954 while (next
&& ASSEGMENTS_PACKABLE(seg
, next
)) {
955 /* NB: We should never normally get here given
957 * normalise aspath data when parse them.
959 * safe than sorry. We potentially could call
960 * assegment_normalise here instead, but it's
962 * easier to do it on the fly here rather than
964 * the segment list twice every time we write
969 /* Next segment's data can fit in this one */
970 assegment_data_put(s
, next
->as
, next
->length
, use32bit
);
972 /* update the length of the segment header */
973 stream_putc_at(s
, lenp
,
974 seg
->length
- written
+ next
->length
);
975 asns_packed
+= next
->length
;
980 bytes
+= ASSEGMENT_SIZE(seg
->length
- written
+ asns_packed
,
987 /* This is for SNMP BGP4PATHATTRASPATHSEGMENT
988 * We have no way to manage the storage, so we use a static stream
989 * wrapper around aspath_put.
991 uint8_t *aspath_snmp_pathseg(struct aspath
*as
, size_t *varlen
)
993 #define SNMP_PATHSEG_MAX 1024
996 snmp_stream
= stream_new(SNMP_PATHSEG_MAX
);
998 stream_reset(snmp_stream
);
1004 aspath_put(snmp_stream
, as
, 0); /* use 16 bit for now here */
1006 *varlen
= stream_get_endp(snmp_stream
);
1007 return stream_pnt(snmp_stream
);
1010 static struct assegment
*aspath_aggregate_as_set_add(struct aspath
*aspath
,
1011 struct assegment
*asset
,
1016 /* If this is first AS set member, create new as-set segment. */
1017 if (asset
== NULL
) {
1018 asset
= assegment_new(AS_SET
, 1);
1019 if (!aspath
->segments
)
1020 aspath
->segments
= asset
;
1022 struct assegment
*seg
= aspath
->segments
;
1027 asset
->type
= AS_SET
;
1031 /* Check this AS value already exists or not. */
1032 for (i
= 0; i
< asset
->length
; i
++)
1033 if (asset
->as
[i
] == as
)
1037 asset
->as
= XREALLOC(MTYPE_AS_SEG_DATA
, asset
->as
,
1038 asset
->length
* AS_VALUE_SIZE
);
1039 asset
->as
[asset
->length
- 1] = as
;
1046 /* Modify as1 using as2 for aggregation. */
1047 struct aspath
*aspath_aggregate(struct aspath
*as1
, struct aspath
*as2
)
1053 struct assegment
*seg1
= as1
->segments
;
1054 struct assegment
*seg2
= as2
->segments
;
1055 struct aspath
*aspath
= NULL
;
1056 struct assegment
*asset
= NULL
;
1057 struct assegment
*prevseg
= NULL
;
1059 /* First of all check common leading sequence. */
1060 while (seg1
&& seg2
) {
1061 /* Check segment type. */
1062 if (seg1
->type
!= seg2
->type
)
1065 /* Minimum segment length. */
1066 minlen
= MIN(seg1
->length
, seg2
->length
);
1068 for (match
= 0; match
< minlen
; match
++)
1069 if (seg1
->as
[match
] != seg2
->as
[match
])
1073 struct assegment
*seg
= assegment_new(seg1
->type
, 0);
1075 seg
= assegment_append_asns(seg
, seg1
->as
, match
);
1078 aspath
= aspath_new();
1079 aspath
->segments
= seg
;
1081 prevseg
->next
= seg
;
1086 if (match
!= minlen
|| match
!= seg1
->length
1087 || seg1
->length
!= seg2
->length
)
1089 /* We are moving on to the next segment to reset match */
1098 aspath
= aspath_new();
1100 /* Make as-set using rest of all information. */
1103 for (i
= from
; i
< seg1
->length
; i
++)
1104 asset
= aspath_aggregate_as_set_add(aspath
, asset
,
1113 for (i
= from
; i
< seg2
->length
; i
++)
1114 asset
= aspath_aggregate_as_set_add(aspath
, asset
,
1121 assegment_normalise(aspath
->segments
);
1122 aspath_str_update(aspath
, false);
1126 /* When a BGP router receives an UPDATE with an MP_REACH_NLRI
1127 attribute, check the leftmost AS number in the AS_PATH attribute is
1128 or not the peer's AS number. */
1129 bool aspath_firstas_check(struct aspath
*aspath
, as_t asno
)
1131 if ((aspath
== NULL
) || (aspath
->segments
== NULL
))
1134 if (aspath
->segments
&& (aspath
->segments
->type
== AS_SEQUENCE
)
1135 && (aspath
->segments
->as
[0] == asno
))
1141 unsigned int aspath_get_first_as(struct aspath
*aspath
)
1143 if (aspath
== NULL
|| aspath
->segments
== NULL
)
1146 return aspath
->segments
->as
[0];
1149 unsigned int aspath_get_last_as(struct aspath
*aspath
)
1152 unsigned int last_as
= 0;
1153 const struct assegment
*seg
;
1155 if (aspath
== NULL
|| aspath
->segments
== NULL
)
1158 seg
= aspath
->segments
;
1161 if (seg
->type
== AS_SEQUENCE
|| seg
->type
== AS_CONFED_SEQUENCE
)
1162 for (i
= 0; i
< seg
->length
; i
++)
1163 last_as
= seg
->as
[i
];
1170 /* AS path loop check. If aspath contains asno then return >= 1. */
1171 int aspath_loop_check(struct aspath
*aspath
, as_t asno
)
1173 struct assegment
*seg
;
1176 if ((aspath
== NULL
) || (aspath
->segments
== NULL
))
1179 seg
= aspath
->segments
;
1184 for (i
= 0; i
< seg
->length
; i
++)
1185 if (seg
->as
[i
] == asno
)
1193 /* When all of AS path is private AS return 1. */
1194 bool aspath_private_as_check(struct aspath
*aspath
)
1196 struct assegment
*seg
;
1198 if (!(aspath
&& aspath
->segments
))
1201 seg
= aspath
->segments
;
1206 for (i
= 0; i
< seg
->length
; i
++) {
1207 if (!BGP_AS_IS_PRIVATE(seg
->as
[i
]))
1215 /* Return True if the entire ASPATH consist of the specified ASN */
1216 bool aspath_single_asn_check(struct aspath
*aspath
, as_t asn
)
1218 struct assegment
*seg
;
1220 if (!(aspath
&& aspath
->segments
))
1223 seg
= aspath
->segments
;
1228 for (i
= 0; i
< seg
->length
; i
++) {
1229 if (seg
->as
[i
] != asn
)
1237 /* Replace all instances of the target ASN with our own ASN */
1238 struct aspath
*aspath_replace_specific_asn(struct aspath
*aspath
,
1239 as_t target_asn
, as_t our_asn
)
1242 struct assegment
*seg
;
1244 new = aspath_dup(aspath
);
1245 seg
= new->segments
;
1250 for (i
= 0; i
< seg
->length
; i
++) {
1251 if (seg
->as
[i
] == target_asn
)
1252 seg
->as
[i
] = our_asn
;
1257 aspath_str_update(new, false);
1261 /* Replace all private ASNs with our own ASN */
1262 struct aspath
*aspath_replace_private_asns(struct aspath
*aspath
, as_t asn
,
1266 struct assegment
*seg
;
1268 new = aspath_dup(aspath
);
1269 seg
= new->segments
;
1274 for (i
= 0; i
< seg
->length
; i
++) {
1275 /* Don't replace if public ASN or peer's ASN */
1276 if (BGP_AS_IS_PRIVATE(seg
->as
[i
])
1277 && (seg
->as
[i
] != peer_asn
))
1283 aspath_str_update(new, false);
1287 /* Remove all private ASNs */
1288 struct aspath
*aspath_remove_private_asns(struct aspath
*aspath
, as_t peer_asn
)
1291 struct assegment
*seg
;
1292 struct assegment
*new_seg
;
1293 struct assegment
*last_new_seg
;
1299 new = XCALLOC(MTYPE_AS_PATH
, sizeof(struct aspath
));
1303 last_new_seg
= NULL
;
1304 seg
= aspath
->segments
;
1308 for (i
= 0; i
< seg
->length
; i
++) {
1310 if (!BGP_AS_IS_PRIVATE(seg
->as
[i
]))
1312 /* ASN matches peer's.
1313 * Don't double-count if peer_asn is public.
1315 else if (seg
->as
[i
] == peer_asn
)
1319 // The entire segment is public so copy it
1320 if (public == seg
->length
)
1321 new_seg
= assegment_dup(seg
);
1323 // The segment is a mix of public and private ASNs. Copy as many
1325 // there are public ASNs then come back and fill in only the
1328 /* length needs to account for all retained ASNs
1329 * (public or peer_asn), not just public
1331 new_seg
= assegment_new(seg
->type
, (public + peer
));
1333 for (i
= 0; i
< seg
->length
; i
++) {
1334 // keep ASN if public or matches peer's ASN
1335 if (!BGP_AS_IS_PRIVATE(seg
->as
[i
])
1336 || (seg
->as
[i
] == peer_asn
)) {
1337 new_seg
->as
[j
] = seg
->as
[i
];
1343 // This is the first segment so set the aspath segments pointer
1346 new->segments
= new_seg
;
1348 last_new_seg
->next
= new_seg
;
1350 last_new_seg
= new_seg
;
1354 aspath_str_update(new, false);
1358 /* AS path confed check. If aspath contains confed set or sequence then return
1360 bool aspath_confed_check(struct aspath
*aspath
)
1362 struct assegment
*seg
;
1364 if (!(aspath
&& aspath
->segments
))
1367 seg
= aspath
->segments
;
1370 if (seg
->type
== AS_CONFED_SET
1371 || seg
->type
== AS_CONFED_SEQUENCE
)
1378 /* Leftmost AS path segment confed check. If leftmost AS segment is of type
1379 AS_CONFED_SEQUENCE or AS_CONFED_SET then return 1. */
1380 bool aspath_left_confed_check(struct aspath
*aspath
)
1383 if (!(aspath
&& aspath
->segments
))
1386 if ((aspath
->segments
->type
== AS_CONFED_SEQUENCE
)
1387 || (aspath
->segments
->type
== AS_CONFED_SET
))
1393 /* Merge as1 to as2. as2 should be uninterned aspath. */
1394 static struct aspath
*aspath_merge(struct aspath
*as1
, struct aspath
*as2
)
1396 struct assegment
*last
, *new;
1401 last
= new = assegment_dup_all(as1
->segments
);
1403 /* find the last valid segment */
1404 while (last
&& last
->next
)
1408 last
->next
= as2
->segments
;
1409 as2
->segments
= new;
1410 aspath_str_update(as2
, false);
1414 /* Prepend as1 to as2. as2 should be uninterned aspath. */
1415 struct aspath
*aspath_prepend(struct aspath
*as1
, struct aspath
*as2
)
1417 struct assegment
*as1segtail
;
1418 struct assegment
*as2segtail
;
1419 struct assegment
*as2seghead
;
1424 /* If as2 is empty, only need to dupe as1's chain onto as2 */
1425 if (as2
->segments
== NULL
) {
1426 as2
->segments
= assegment_dup_all(as1
->segments
);
1427 aspath_str_update(as2
, false);
1431 /* If as1 is empty AS, no prepending to do. */
1432 if (as1
->segments
== NULL
)
1435 /* find the tail as1's segment chain. */
1436 as1segtail
= as1
->segments
;
1437 while (as1segtail
&& as1segtail
->next
)
1438 as1segtail
= as1segtail
->next
;
1440 /* Delete any AS_CONFED_SEQUENCE segment from as2. */
1441 if (as1segtail
->type
== AS_SEQUENCE
1442 && as2
->segments
->type
== AS_CONFED_SEQUENCE
)
1443 as2
= aspath_delete_confed_seq(as2
);
1445 if (!as2
->segments
) {
1446 as2
->segments
= assegment_dup_all(as1
->segments
);
1447 aspath_str_update(as2
, false);
1451 /* Compare last segment type of as1 and first segment type of as2. */
1452 if (as1segtail
->type
!= as2
->segments
->type
)
1453 return aspath_merge(as1
, as2
);
1455 if (as1segtail
->type
== AS_SEQUENCE
) {
1456 /* We have two chains of segments, as1->segments and seg2,
1457 * and we have to attach them together, merging the attaching
1458 * segments together into one.
1460 * 1. dupe as1->segments onto head of as2
1461 * 2. merge seg2's asns onto last segment of this new chain
1462 * 3. attach chain after seg2
1466 as2seghead
= as2
->segments
;
1468 /* dupe as1 onto as2's head */
1469 as2segtail
= as2
->segments
= assegment_dup_all(as1
->segments
);
1471 /* refind the tail of as2 */
1472 while (as2segtail
&& as2segtail
->next
)
1473 as2segtail
= as2segtail
->next
;
1475 /* merge the old head, seg2, into tail, seg1 */
1476 assegment_append_asns(as2segtail
, as2seghead
->as
,
1477 as2seghead
->length
);
1480 * bypass the merged seg2, and attach any chain after it
1481 * to chain descending from as2's head
1484 as2segtail
->next
= as2seghead
->next
;
1486 /* as2->segments is now referenceless and useless */
1487 assegment_free(as2seghead
);
1489 /* we've now prepended as1's segment chain to as2, merging
1490 * the inbetween AS_SEQUENCE of seg2 in the process
1492 aspath_str_update(as2
, false);
1495 /* AS_SET merge code is needed at here. */
1496 return aspath_merge(as1
, as2
);
1498 /* XXX: Ermmm, what if as1 has multiple segments?? */
1503 /* Iterate over AS_PATH segments and wipe all occurrences of the
1504 * listed AS numbers. Hence some segments may lose some or even
1505 * all data on the way, the operation is implemented as a smarter
1506 * version of aspath_dup(), which allocates memory to hold the new
1507 * data, not the original. The new AS path is returned.
1509 struct aspath
*aspath_filter_exclude(struct aspath
*source
,
1510 struct aspath
*exclude_list
)
1512 struct assegment
*srcseg
, *exclseg
, *lastseg
;
1513 struct aspath
*newpath
;
1515 newpath
= aspath_new();
1518 for (srcseg
= source
->segments
; srcseg
; srcseg
= srcseg
->next
) {
1519 unsigned i
, y
, newlen
= 0, done
= 0, skip_as
;
1520 struct assegment
*newseg
;
1522 /* Find out, how much ASns are we going to pick from this
1524 * We can't perform filtering right inline, because the size of
1525 * the new segment isn't known at the moment yet.
1527 for (i
= 0; i
< srcseg
->length
; i
++) {
1529 for (exclseg
= exclude_list
->segments
;
1530 exclseg
&& !skip_as
; exclseg
= exclseg
->next
)
1531 for (y
= 0; y
< exclseg
->length
; y
++)
1532 if (srcseg
->as
[i
] == exclseg
->as
[y
]) {
1534 // There's no sense in testing
1535 // the rest of exclusion list,
1542 /* newlen is now the number of ASns to copy */
1546 /* Actual copying. Allocate memory and iterate once more,
1547 * performing filtering. */
1548 newseg
= assegment_new(srcseg
->type
, newlen
);
1549 for (i
= 0; i
< srcseg
->length
; i
++) {
1551 for (exclseg
= exclude_list
->segments
;
1552 exclseg
&& !skip_as
; exclseg
= exclseg
->next
)
1553 for (y
= 0; y
< exclseg
->length
; y
++)
1554 if (srcseg
->as
[i
] == exclseg
->as
[y
]) {
1560 newseg
->as
[done
++] = srcseg
->as
[i
];
1562 /* At his point newlen must be equal to done, and both must be
1564 * the filtered segment to the gross result. */
1566 newpath
->segments
= newseg
;
1568 lastseg
->next
= newseg
;
1571 aspath_str_update(newpath
, false);
1572 /* We are happy returning even an empty AS_PATH, because the
1574 * might expect this very behaviour. There's a mean to avoid this, if
1576 * by having a match rule against certain AS_PATH regexps in the
1579 aspath_free(source
);
1583 /* Add specified AS to the leftmost of aspath. */
1584 static struct aspath
*aspath_add_asns(struct aspath
*aspath
, as_t asno
,
1585 uint8_t type
, unsigned num
)
1587 struct assegment
*assegment
= aspath
->segments
;
1590 if (assegment
&& assegment
->type
== type
) {
1591 /* extend existing segment */
1593 assegment_prepend_asns(aspath
->segments
, asno
, num
);
1595 /* prepend with new segment */
1596 struct assegment
*newsegment
= assegment_new(type
, num
);
1597 for (i
= 0; i
< num
; i
++)
1598 newsegment
->as
[i
] = asno
;
1600 /* insert potentially replacing empty segment */
1601 if (assegment
&& assegment
->length
== 0) {
1602 newsegment
->next
= assegment
->next
;
1603 assegment_free(assegment
);
1605 newsegment
->next
= assegment
;
1606 aspath
->segments
= newsegment
;
1609 aspath_str_update(aspath
, false);
1613 /* Add specified AS to the leftmost of aspath num times. */
1614 struct aspath
*aspath_add_seq_n(struct aspath
*aspath
, as_t asno
, unsigned num
)
1616 return aspath_add_asns(aspath
, asno
, AS_SEQUENCE
, num
);
1619 /* Add specified AS to the leftmost of aspath. */
1620 struct aspath
*aspath_add_seq(struct aspath
*aspath
, as_t asno
)
1622 return aspath_add_asns(aspath
, asno
, AS_SEQUENCE
, 1);
1625 /* Compare leftmost AS value for MED check. If as1's leftmost AS and
1626 as2's leftmost AS is same return 1. */
1627 bool aspath_cmp_left(const struct aspath
*aspath1
, const struct aspath
*aspath2
)
1629 const struct assegment
*seg1
;
1630 const struct assegment
*seg2
;
1632 if (!(aspath1
&& aspath2
))
1635 seg1
= aspath1
->segments
;
1636 seg2
= aspath2
->segments
;
1638 /* If both paths are originated in this AS then we do want to compare
1643 /* find first non-confed segments for each */
1644 while (seg1
&& ((seg1
->type
== AS_CONFED_SEQUENCE
)
1645 || (seg1
->type
== AS_CONFED_SET
)))
1648 while (seg2
&& ((seg2
->type
== AS_CONFED_SEQUENCE
)
1649 || (seg2
->type
== AS_CONFED_SET
)))
1653 if (!(seg1
&& seg2
&& (seg1
->type
== AS_SEQUENCE
)
1654 && (seg2
->type
== AS_SEQUENCE
)))
1657 if (seg1
->as
[0] == seg2
->as
[0])
1663 /* Truncate an aspath after a number of hops, and put the hops remaining
1664 * at the front of another aspath. Needed for AS4 compat.
1666 * Returned aspath is a /new/ aspath, which should either by free'd or
1667 * interned by the caller, as desired.
1669 struct aspath
*aspath_reconcile_as4(struct aspath
*aspath
,
1670 struct aspath
*as4path
)
1672 struct assegment
*seg
, *newseg
, *prevseg
= NULL
;
1673 struct aspath
*newpath
= NULL
, *mergedpath
;
1674 int hops
, cpasns
= 0;
1676 if (!aspath
|| !as4path
)
1679 seg
= aspath
->segments
;
1681 /* CONFEDs should get reconciled too.. */
1682 hops
= (aspath_count_hops(aspath
) + aspath_count_confeds(aspath
))
1683 - aspath_count_hops(as4path
);
1686 if (BGP_DEBUG(as4
, AS4
))
1688 EC_BGP_ASPATH_FEWER_HOPS
,
1689 "[AS4] Fewer hops in AS_PATH than NEW_AS_PATH");
1690 /* Something's gone wrong. The RFC says we should now ignore
1692 * which is daft behaviour - it contains vital loop-detection
1693 * information which must have been removed from AS_PATH.
1695 hops
= aspath_count_hops(aspath
);
1699 newpath
= aspath_dup(as4path
);
1700 aspath_str_update(newpath
, false);
1704 if (BGP_DEBUG(as4
, AS4
))
1706 "[AS4] got AS_PATH %s and AS4_PATH %s synthesizing now",
1707 aspath
->str
, as4path
->str
);
1709 while (seg
&& hops
> 0) {
1710 switch (seg
->type
) {
1714 cpasns
= seg
->length
;
1716 case AS_CONFED_SEQUENCE
:
1717 /* Should never split a confed-sequence, if hop-count
1718 * suggests we must then something's gone wrong
1721 * Most important goal is to preserve AS_PATHs prime
1723 * as loop-detector, so we fudge the numbers so that the
1725 * confed-sequence is merged in.
1727 if (hops
< seg
->length
) {
1728 if (BGP_DEBUG(as4
, AS4
))
1730 "[AS4] AS4PATHmangle: AS_CONFED_SEQUENCE falls across 2/4 ASN boundary somewhere, broken..");
1735 cpasns
= MIN(seg
->length
, hops
);
1736 hops
-= seg
->length
;
1739 assert(cpasns
<= seg
->length
);
1741 newseg
= assegment_new(seg
->type
, 0);
1742 newseg
= assegment_append_asns(newseg
, seg
->as
, cpasns
);
1745 newpath
= aspath_new();
1746 newpath
->segments
= newseg
;
1748 prevseg
->next
= newseg
;
1754 /* We may be able to join some segments here, and we must
1755 * do this because... we want normalised aspaths in out hash
1756 * and we do not want to stumble in aspath_put.
1758 mergedpath
= aspath_merge(newpath
, aspath_dup(as4path
));
1759 aspath_free(newpath
);
1760 mergedpath
->segments
= assegment_normalise(mergedpath
->segments
);
1761 aspath_str_update(mergedpath
, false);
1763 if (BGP_DEBUG(as4
, AS4
))
1764 zlog_debug("[AS4] result of synthesizing is %s",
1770 /* Compare leftmost AS value for MED check. If as1's leftmost AS and
1771 as2's leftmost AS is same return 1. (confederation as-path
1773 bool aspath_cmp_left_confed(const struct aspath
*aspath1
,
1774 const struct aspath
*aspath2
)
1776 if (!(aspath1
&& aspath2
))
1779 if (!(aspath1
->segments
&& aspath2
->segments
))
1782 if ((aspath1
->segments
->type
!= AS_CONFED_SEQUENCE
)
1783 || (aspath2
->segments
->type
!= AS_CONFED_SEQUENCE
))
1786 if (aspath1
->segments
->as
[0] == aspath2
->segments
->as
[0])
1792 /* Delete all AS_CONFED_SEQUENCE/SET segments from aspath.
1793 * RFC 5065 section 4.1.c.1
1795 * 1) if any path segments of the AS_PATH are of the type
1796 * AS_CONFED_SEQUENCE or AS_CONFED_SET, those segments MUST be
1797 * removed from the AS_PATH attribute, leaving the sanitized
1798 * AS_PATH attribute to be operated on by steps 2, 3 or 4.
1800 struct aspath
*aspath_delete_confed_seq(struct aspath
*aspath
)
1802 struct assegment
*seg
, *prev
, *next
;
1803 char removed_confed_segment
;
1805 if (!(aspath
&& aspath
->segments
))
1808 seg
= aspath
->segments
;
1809 removed_confed_segment
= 0;
1816 if (seg
->type
== AS_CONFED_SEQUENCE
1817 || seg
->type
== AS_CONFED_SET
) {
1818 /* This is the first segment in the aspath */
1819 if (aspath
->segments
== seg
)
1820 aspath
->segments
= seg
->next
;
1822 prev
->next
= seg
->next
;
1824 assegment_free(seg
);
1825 removed_confed_segment
= 1;
1832 if (removed_confed_segment
)
1833 aspath_str_update(aspath
, false);
1838 /* Add new AS number to the leftmost part of the aspath as
1839 AS_CONFED_SEQUENCE. */
1840 struct aspath
*aspath_add_confed_seq(struct aspath
*aspath
, as_t asno
)
1842 return aspath_add_asns(aspath
, asno
, AS_CONFED_SEQUENCE
, 1);
1845 /* Add new as value to as path structure. */
1846 static void aspath_as_add(struct aspath
*as
, as_t asno
)
1848 struct assegment
*seg
= as
->segments
;
1853 /* Last segment search procedure. */
1857 assegment_append_asns(seg
, &asno
, 1);
1860 /* Add new as segment to the as path. */
1861 static void aspath_segment_add(struct aspath
*as
, int type
)
1863 struct assegment
*seg
= as
->segments
;
1864 struct assegment
*new = assegment_new(type
, 0);
1874 struct aspath
*aspath_empty(void)
1876 return aspath_parse(NULL
, 0, 1); /* 32Bit ;-) */
1879 struct aspath
*aspath_empty_get(void)
1881 struct aspath
*aspath
;
1883 aspath
= aspath_new();
1884 aspath_make_str_count(aspath
, false);
1888 unsigned long aspath_count(void)
1890 return ashash
->count
;
1894 Theoretically, one as path can have:
1896 One BGP packet size should be less than 4096.
1897 One BGP attribute size should be less than 4096 - BGP header size.
1898 One BGP aspath size should be less than 4096 - BGP header size -
1899 BGP mandantry attribute size.
1902 /* AS path string lexical token enum. */
1907 as_token_confed_seq_start
,
1908 as_token_confed_seq_end
,
1909 as_token_confed_set_start
,
1910 as_token_confed_set_end
,
1914 /* Return next token and point for string parse. */
1915 static const char *aspath_gettoken(const char *buf
, enum as_token
*token
,
1916 unsigned long *asno
)
1918 const char *p
= buf
;
1920 /* Skip separators (space for sequences, ',' for sets). */
1921 while (isspace((unsigned char)*p
) || *p
== ',')
1924 /* Check the end of the string and type specify characters
1930 *token
= as_token_set_start
;
1934 *token
= as_token_set_end
;
1938 *token
= as_token_confed_seq_start
;
1942 *token
= as_token_confed_seq_end
;
1946 *token
= as_token_confed_set_start
;
1950 *token
= as_token_confed_set_end
;
1955 /* Check actual AS value. */
1956 if (isdigit((unsigned char)*p
)) {
1959 *token
= as_token_asval
;
1963 while (isdigit((unsigned char)*p
)) {
1965 asval
+= (*p
- '0');
1972 /* There is no match then return unknown token. */
1973 *token
= as_token_unknown
;
1978 struct aspath
*aspath_str2aspath(const char *str
)
1980 enum as_token token
= as_token_unknown
;
1981 unsigned short as_type
;
1982 unsigned long asno
= 0;
1983 struct aspath
*aspath
;
1986 aspath
= aspath_new();
1988 /* We start default type as AS_SEQUENCE. */
1989 as_type
= AS_SEQUENCE
;
1992 while ((str
= aspath_gettoken(str
, &token
, &asno
)) != NULL
) {
1994 case as_token_asval
:
1996 aspath_segment_add(aspath
, as_type
);
1999 aspath_as_add(aspath
, asno
);
2001 case as_token_set_start
:
2003 aspath_segment_add(aspath
, as_type
);
2006 case as_token_set_end
:
2007 as_type
= AS_SEQUENCE
;
2010 case as_token_confed_seq_start
:
2011 as_type
= AS_CONFED_SEQUENCE
;
2012 aspath_segment_add(aspath
, as_type
);
2015 case as_token_confed_seq_end
:
2016 as_type
= AS_SEQUENCE
;
2019 case as_token_confed_set_start
:
2020 as_type
= AS_CONFED_SET
;
2021 aspath_segment_add(aspath
, as_type
);
2024 case as_token_confed_set_end
:
2025 as_type
= AS_SEQUENCE
;
2028 case as_token_unknown
:
2030 aspath_free(aspath
);
2035 aspath_make_str_count(aspath
, false);
2040 /* Make hash value by raw aspath data. */
2041 unsigned int aspath_key_make(const void *p
)
2043 const struct aspath
*aspath
= p
;
2044 unsigned int key
= 0;
2047 aspath_str_update((struct aspath
*)aspath
, false);
2049 key
= jhash(aspath
->str
, aspath
->str_len
, 2334325);
2054 /* If two aspath have same value then return 1 else return 0 */
2055 bool aspath_cmp(const void *arg1
, const void *arg2
)
2057 const struct assegment
*seg1
= ((const struct aspath
*)arg1
)->segments
;
2058 const struct assegment
*seg2
= ((const struct aspath
*)arg2
)->segments
;
2060 while (seg1
|| seg2
) {
2062 if ((!seg1
&& seg2
) || (seg1
&& !seg2
))
2064 if (seg1
->type
!= seg2
->type
)
2066 if (seg1
->length
!= seg2
->length
)
2068 for (i
= 0; i
< seg1
->length
; i
++)
2069 if (seg1
->as
[i
] != seg2
->as
[i
])
2077 /* AS path hash initialize. */
2078 void aspath_init(void)
2080 ashash
= hash_create_size(32768, aspath_key_make
, aspath_cmp
,
2084 void aspath_finish(void)
2086 hash_clean(ashash
, (void (*)(void *))aspath_free
);
2091 stream_free(snmp_stream
);
2094 /* return and as path value */
2095 const char *aspath_print(struct aspath
*as
)
2097 return (as
? as
->str
: NULL
);
2100 /* Printing functions */
2101 /* Feed the AS_PATH to the vty; the suffix string follows it only in case
2102 * AS_PATH wasn't empty.
2104 void aspath_print_vty(struct vty
*vty
, const char *format
, struct aspath
*as
,
2108 vty_out(vty
, format
, as
->str
);
2109 if (as
->str_len
&& strlen(suffix
))
2110 vty_out(vty
, "%s", suffix
);
2113 static void aspath_show_all_iterator(struct hash_bucket
*bucket
,
2118 as
= (struct aspath
*)bucket
->data
;
2120 vty_out(vty
, "[%p:%u] (%ld) ", (void *)bucket
, bucket
->key
, as
->refcnt
);
2121 vty_out(vty
, "%s\n", as
->str
);
2124 /* Print all aspath and hash information. This function is used from
2125 `show [ip] bgp paths' command. */
2126 void aspath_print_all_vty(struct vty
*vty
)
2128 hash_iterate(ashash
, (void (*)(struct hash_bucket
*,
2129 void *))aspath_show_all_iterator
,
2133 static struct aspath
*bgp_aggr_aspath_lookup(struct bgp_aggregate
*aggregate
,
2134 struct aspath
*aspath
)
2136 return hash_lookup(aggregate
->aspath_hash
, aspath
);
2139 static void *bgp_aggr_aspath_hash_alloc(void *p
)
2141 struct aspath
*ref
= (struct aspath
*)p
;
2142 struct aspath
*aspath
= NULL
;
2144 aspath
= aspath_dup(ref
);
2148 static void bgp_aggr_aspath_prepare(struct hash_bucket
*hb
, void *arg
)
2150 struct aspath
*hb_aspath
= hb
->data
;
2151 struct aspath
**aggr_aspath
= arg
;
2154 *aggr_aspath
= aspath_aggregate(*aggr_aspath
, hb_aspath
);
2156 *aggr_aspath
= aspath_dup(hb_aspath
);
2159 void bgp_aggr_aspath_remove(void *arg
)
2161 struct aspath
*aspath
= arg
;
2163 aspath_free(aspath
);
2166 void bgp_compute_aggregate_aspath(struct bgp_aggregate
*aggregate
,
2167 struct aspath
*aspath
)
2169 bgp_compute_aggregate_aspath_hash(aggregate
, aspath
);
2171 bgp_compute_aggregate_aspath_val(aggregate
);
2175 void bgp_compute_aggregate_aspath_hash(struct bgp_aggregate
*aggregate
,
2176 struct aspath
*aspath
)
2178 struct aspath
*aggr_aspath
= NULL
;
2180 if ((aggregate
== NULL
) || (aspath
== NULL
))
2183 /* Create hash if not already created.
2185 if (aggregate
->aspath_hash
== NULL
)
2186 aggregate
->aspath_hash
= hash_create(
2187 aspath_key_make
, aspath_cmp
,
2188 "BGP Aggregator as-path hash");
2190 aggr_aspath
= bgp_aggr_aspath_lookup(aggregate
, aspath
);
2191 if (aggr_aspath
== NULL
) {
2192 /* Insert as-path into hash.
2194 aggr_aspath
= hash_get(aggregate
->aspath_hash
, aspath
,
2195 bgp_aggr_aspath_hash_alloc
);
2198 /* Increment reference counter.
2200 aggr_aspath
->refcnt
++;
2203 void bgp_compute_aggregate_aspath_val(struct bgp_aggregate
*aggregate
)
2205 if (aggregate
== NULL
)
2207 /* Re-compute aggregate's as-path.
2209 if (aggregate
->aspath
) {
2210 aspath_free(aggregate
->aspath
);
2211 aggregate
->aspath
= NULL
;
2213 if (aggregate
->aspath_hash
2214 && aggregate
->aspath_hash
->count
) {
2215 hash_iterate(aggregate
->aspath_hash
,
2216 bgp_aggr_aspath_prepare
,
2217 &aggregate
->aspath
);
2221 void bgp_remove_aspath_from_aggregate(struct bgp_aggregate
*aggregate
,
2222 struct aspath
*aspath
)
2224 struct aspath
*aggr_aspath
= NULL
;
2225 struct aspath
*ret_aspath
= NULL
;
2228 || (!aggregate
->aspath_hash
)
2232 /* Look-up the aspath in the hash.
2234 aggr_aspath
= bgp_aggr_aspath_lookup(aggregate
, aspath
);
2236 aggr_aspath
->refcnt
--;
2238 if (aggr_aspath
->refcnt
== 0) {
2239 ret_aspath
= hash_release(aggregate
->aspath_hash
,
2241 aspath_free(ret_aspath
);
2244 /* Remove aggregate's old as-path.
2246 aspath_free(aggregate
->aspath
);
2247 aggregate
->aspath
= NULL
;
2249 bgp_compute_aggregate_aspath_val(aggregate
);
2254 void bgp_remove_aspath_from_aggregate_hash(struct bgp_aggregate
*aggregate
,
2255 struct aspath
*aspath
)
2257 struct aspath
*aggr_aspath
= NULL
;
2258 struct aspath
*ret_aspath
= NULL
;
2261 || (!aggregate
->aspath_hash
)
2265 /* Look-up the aspath in the hash.
2267 aggr_aspath
= bgp_aggr_aspath_lookup(aggregate
, aspath
);
2269 aggr_aspath
->refcnt
--;
2271 if (aggr_aspath
->refcnt
== 0) {
2272 ret_aspath
= hash_release(aggregate
->aspath_hash
,
2274 aspath_free(ret_aspath
);