]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* AS path management routines. |
896014f4 DL |
2 | * Copyright (C) 1996, 97, 98, 99 Kunihiro Ishiguro |
3 | * Copyright (C) 2005 Sun Microsystems, Inc. | |
4 | * | |
5 | * This file is part of GNU Zebra. | |
6 | * | |
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 | |
10 | * later version. | |
11 | * | |
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. | |
16 | * | |
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 | |
20 | */ | |
718e3744 | 21 | |
22 | #include <zebra.h> | |
23 | ||
24 | #include "hash.h" | |
25 | #include "memory.h" | |
26 | #include "vector.h" | |
718e3744 | 27 | #include "log.h" |
fe69a505 | 28 | #include "stream.h" |
4dcadbef | 29 | #include "command.h" |
0b2aa3a0 | 30 | #include "jhash.h" |
3f9c7369 | 31 | #include "queue.h" |
039f3a34 | 32 | #include "filter.h" |
718e3744 | 33 | |
34 | #include "bgpd/bgpd.h" | |
35 | #include "bgpd/bgp_aspath.h" | |
0b2aa3a0 PJ |
36 | #include "bgpd/bgp_debug.h" |
37 | #include "bgpd/bgp_attr.h" | |
6b0655a2 | 38 | |
718e3744 | 39 | /* Attr. Flags and Attr. Type Code. */ |
40 | #define AS_HEADER_SIZE 2 | |
41 | ||
0b2aa3a0 | 42 | /* Now FOUR octets are used for AS value. */ |
718e3744 | 43 | #define AS_VALUE_SIZE sizeof (as_t) |
0b2aa3a0 PJ |
44 | /* This is the old one */ |
45 | #define AS16_VALUE_SIZE sizeof (as16_t) | |
718e3744 | 46 | |
fe69a505 | 47 | /* Maximum protocol segment length value */ |
48 | #define AS_SEGMENT_MAX 255 | |
49 | ||
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. | |
0b2aa3a0 PJ |
55 | * |
56 | * aspath_put returns bytes written, the only definitive record of | |
57 | * size of wire-format attribute.. | |
fe69a505 | 58 | */ |
59 | ||
60 | /* Calculated size in bytes of ASN segment data to hold N ASN's */ | |
d62a17ae | 61 | #define ASSEGMENT_DATA_SIZE(N, S) \ |
62 | ((N) * ((S) ? AS_VALUE_SIZE : AS16_VALUE_SIZE)) | |
718e3744 | 63 | |
fe69a505 | 64 | /* Calculated size of segment struct to hold N ASN's */ |
0b2aa3a0 | 65 | #define ASSEGMENT_SIZE(N,S) (AS_HEADER_SIZE + ASSEGMENT_DATA_SIZE (N,S)) |
fe69a505 | 66 | |
67 | /* AS segment octet length. */ | |
0b2aa3a0 | 68 | #define ASSEGMENT_LEN(X,S) ASSEGMENT_SIZE((X)->length,S) |
fe69a505 | 69 | |
70 | /* AS_SEQUENCE segments can be packed together */ | |
71 | /* Can the types of X and Y be considered for packing? */ | |
d62a17ae | 72 | #define ASSEGMENT_TYPES_PACKABLE(X, Y) \ |
73 | (((X)->type == (Y)->type) && ((X)->type == AS_SEQUENCE)) | |
fe69a505 | 74 | /* Types and length of X,Y suitable for packing? */ |
d62a17ae | 75 | #define ASSEGMENTS_PACKABLE(X, Y) \ |
76 | (ASSEGMENT_TYPES_PACKABLE((X), (Y)) \ | |
77 | && (((X)->length + (Y)->length) <= AS_SEGMENT_MAX)) | |
fe69a505 | 78 | |
d62a17ae | 79 | /* As segment header - the on-wire representation |
fe69a505 | 80 | * NOT the internal representation! |
81 | */ | |
d62a17ae | 82 | struct assegment_header { |
d7c0a89a QY |
83 | uint8_t type; |
84 | uint8_t length; | |
718e3744 | 85 | }; |
86 | ||
87 | /* Hash for aspath. This is the top level structure of AS path. */ | |
da88ea82 | 88 | static struct hash *ashash; |
8fdc32ab | 89 | |
90 | /* Stream for SNMP. See aspath_snmp_pathseg */ | |
91 | static struct stream *snmp_stream; | |
6b0655a2 | 92 | |
f669f7d2 | 93 | /* Callers are required to initialize the memory */ |
d62a17ae | 94 | static as_t *assegment_data_new(int num) |
fe69a505 | 95 | { |
d62a17ae | 96 | return (XMALLOC(MTYPE_AS_SEG_DATA, ASSEGMENT_DATA_SIZE(num, 1))); |
fe69a505 | 97 | } |
98 | ||
d62a17ae | 99 | static void assegment_data_free(as_t *asdata) |
289d2501 | 100 | { |
d62a17ae | 101 | XFREE(MTYPE_AS_SEG_DATA, asdata); |
289d2501 LB |
102 | } |
103 | ||
d62a17ae | 104 | const char *aspath_segment_type_str[] = {"as-invalid", "as-set", "as-sequence", |
105 | "as-confed-sequence", "as-confed-set"}; | |
f1aa5d8a | 106 | |
fe69a505 | 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 | |
111 | */ | |
d7c0a89a | 112 | static struct assegment *assegment_new(uint8_t type, unsigned short length) |
d62a17ae | 113 | { |
114 | struct assegment *new; | |
115 | ||
116 | new = XCALLOC(MTYPE_AS_SEG, sizeof(struct assegment)); | |
117 | ||
118 | if (length) | |
119 | new->as = assegment_data_new(length); | |
120 | ||
121 | new->length = length; | |
122 | new->type = type; | |
123 | ||
124 | return new; | |
125 | } | |
126 | ||
127 | static void assegment_free(struct assegment *seg) | |
128 | { | |
129 | if (!seg) | |
130 | return; | |
131 | ||
132 | if (seg->as) | |
133 | assegment_data_free(seg->as); | |
134 | memset(seg, 0xfe, sizeof(struct assegment)); | |
135 | XFREE(MTYPE_AS_SEG, seg); | |
136 | ||
137 | return; | |
fe69a505 | 138 | } |
139 | ||
140 | /* free entire chain of segments */ | |
d62a17ae | 141 | static void assegment_free_all(struct assegment *seg) |
fe69a505 | 142 | { |
d62a17ae | 143 | struct assegment *prev; |
144 | ||
145 | while (seg) { | |
146 | prev = seg; | |
147 | seg = seg->next; | |
148 | assegment_free(prev); | |
149 | } | |
fe69a505 | 150 | } |
151 | ||
152 | /* Duplicate just the given assegment and its data */ | |
d62a17ae | 153 | static struct assegment *assegment_dup(struct assegment *seg) |
fe69a505 | 154 | { |
d62a17ae | 155 | struct assegment *new; |
156 | ||
157 | new = assegment_new(seg->type, seg->length); | |
158 | memcpy(new->as, seg->as, ASSEGMENT_DATA_SIZE(new->length, 1)); | |
159 | ||
160 | return new; | |
fe69a505 | 161 | } |
162 | ||
163 | /* Duplicate entire chain of assegments, return the head */ | |
d62a17ae | 164 | static struct assegment *assegment_dup_all(struct assegment *seg) |
165 | { | |
166 | struct assegment *new = NULL; | |
167 | struct assegment *head = NULL; | |
168 | ||
169 | while (seg) { | |
170 | if (head) { | |
171 | new->next = assegment_dup(seg); | |
172 | new = new->next; | |
173 | } else | |
174 | head = new = assegment_dup(seg); | |
175 | ||
176 | seg = seg->next; | |
177 | } | |
178 | return head; | |
fe69a505 | 179 | } |
180 | ||
181 | /* prepend the as number to given segment, given num of times */ | |
d62a17ae | 182 | static struct assegment *assegment_prepend_asns(struct assegment *seg, |
183 | as_t asnum, int num) | |
fe69a505 | 184 | { |
d62a17ae | 185 | as_t *newas; |
186 | int i; | |
fe69a505 | 187 | |
d62a17ae | 188 | if (!num) |
189 | return seg; | |
f669f7d2 | 190 | |
d62a17ae | 191 | if (num >= AS_SEGMENT_MAX) |
192 | return seg; /* we don't do huge prepends */ | |
f669f7d2 | 193 | |
d62a17ae | 194 | if ((newas = assegment_data_new(seg->length + num)) == NULL) |
195 | return seg; | |
196 | ||
197 | for (i = 0; i < num; i++) | |
198 | newas[i] = asnum; | |
199 | ||
200 | memcpy(newas + num, seg->as, ASSEGMENT_DATA_SIZE(seg->length, 1)); | |
201 | assegment_data_free(seg->as); | |
202 | seg->as = newas; | |
203 | seg->length += num; | |
204 | ||
205 | return seg; | |
fe69a505 | 206 | } |
207 | ||
208 | /* append given array of as numbers to the segment */ | |
d62a17ae | 209 | static struct assegment *assegment_append_asns(struct assegment *seg, |
210 | as_t *asnos, int num) | |
fe69a505 | 211 | { |
d62a17ae | 212 | as_t *newas; |
fe69a505 | 213 | |
d62a17ae | 214 | newas = XREALLOC(MTYPE_AS_SEG_DATA, seg->as, |
215 | ASSEGMENT_DATA_SIZE(seg->length + num, 1)); | |
216 | ||
217 | if (newas) { | |
218 | seg->as = newas; | |
219 | memcpy(seg->as + seg->length, asnos, | |
220 | ASSEGMENT_DATA_SIZE(num, 1)); | |
221 | seg->length += num; | |
222 | return seg; | |
223 | } | |
fe69a505 | 224 | |
d62a17ae | 225 | assegment_free_all(seg); |
226 | return NULL; | |
fe69a505 | 227 | } |
228 | ||
d62a17ae | 229 | static int int_cmp(const void *p1, const void *p2) |
fe69a505 | 230 | { |
d62a17ae | 231 | const as_t *as1 = p1; |
232 | const as_t *as2 = p2; | |
233 | ||
234 | return (*as1 == *as2) ? 0 : ((*as1 > *as2) ? 1 : -1); | |
fe69a505 | 235 | } |
236 | ||
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.. | |
242 | */ | |
d62a17ae | 243 | static struct assegment *assegment_normalise(struct assegment *head) |
244 | { | |
245 | struct assegment *seg = head, *pin; | |
246 | struct assegment *tmp; | |
247 | ||
248 | if (!head) | |
249 | return head; | |
250 | ||
251 | while (seg) { | |
252 | pin = seg; | |
253 | ||
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 ;) | |
257 | */ | |
258 | if (seg->type == AS_SET || seg->type == AS_CONFED_SET) { | |
259 | int tail = 0; | |
260 | int i; | |
261 | ||
262 | qsort(seg->as, seg->length, sizeof(as_t), int_cmp); | |
263 | ||
264 | /* weed out dupes */ | |
265 | for (i = 1; i < seg->length; i++) { | |
266 | if (seg->as[tail] == seg->as[i]) | |
267 | continue; | |
268 | ||
269 | tail++; | |
270 | if (tail < i) | |
271 | seg->as[tail] = seg->as[i]; | |
272 | } | |
273 | /* seg->length can be 0.. */ | |
274 | if (seg->length) | |
275 | seg->length = tail + 1; | |
276 | } | |
277 | ||
278 | /* read ahead from the current, pinned segment while the | |
279 | * segments | |
280 | * are packable/mergeable. Append all following packable | |
281 | * segments | |
282 | * to the segment we have pinned and remove these appended | |
283 | * segments. | |
284 | */ | |
285 | while (pin->next && ASSEGMENT_TYPES_PACKABLE(pin, pin->next)) { | |
286 | tmp = pin->next; | |
287 | seg = pin->next; | |
288 | ||
289 | /* append the next sequence to the pinned sequence */ | |
290 | pin = assegment_append_asns(pin, seg->as, seg->length); | |
291 | ||
292 | /* bypass the next sequence */ | |
293 | pin->next = seg->next; | |
294 | ||
295 | /* get rid of the now referenceless segment */ | |
296 | assegment_free(tmp); | |
297 | } | |
298 | ||
299 | seg = pin->next; | |
0b2aa3a0 | 300 | } |
d62a17ae | 301 | return head; |
718e3744 | 302 | } |
303 | ||
d62a17ae | 304 | static struct aspath *aspath_new(void) |
718e3744 | 305 | { |
d62a17ae | 306 | return XCALLOC(MTYPE_AS_PATH, sizeof(struct aspath)); |
307 | } | |
f1aa5d8a | 308 | |
d62a17ae | 309 | /* Free AS path structure. */ |
310 | void aspath_free(struct aspath *aspath) | |
311 | { | |
312 | if (!aspath) | |
313 | return; | |
314 | if (aspath->segments) | |
315 | assegment_free_all(aspath->segments); | |
316 | if (aspath->str) | |
317 | XFREE(MTYPE_AS_STR, aspath->str); | |
318 | ||
319 | if (aspath->json) { | |
320 | json_object_free(aspath->json); | |
321 | aspath->json = NULL; | |
322 | } | |
f1aa5d8a | 323 | |
d62a17ae | 324 | XFREE(MTYPE_AS_PATH, aspath); |
718e3744 | 325 | } |
326 | ||
327 | /* Unintern aspath from AS path bucket. */ | |
d62a17ae | 328 | void aspath_unintern(struct aspath **aspath) |
718e3744 | 329 | { |
d62a17ae | 330 | struct aspath *ret; |
331 | struct aspath *asp = *aspath; | |
332 | ||
333 | if (asp->refcnt) | |
334 | asp->refcnt--; | |
718e3744 | 335 | |
d62a17ae | 336 | if (asp->refcnt == 0) { |
337 | /* This aspath must exist in aspath hash table. */ | |
338 | ret = hash_release(ashash, asp); | |
339 | assert(ret != NULL); | |
340 | aspath_free(asp); | |
341 | *aspath = NULL; | |
342 | } | |
718e3744 | 343 | } |
344 | ||
345 | /* Return the start or end delimiters for a particular Segment type */ | |
346 | #define AS_SEG_START 0 | |
347 | #define AS_SEG_END 1 | |
d7c0a89a | 348 | static char aspath_delimiter_char(uint8_t type, uint8_t which) |
d62a17ae | 349 | { |
350 | int i; | |
351 | struct { | |
352 | int type; | |
353 | char start; | |
354 | char end; | |
355 | } aspath_delim_char[] = {{AS_SET, '{', '}'}, | |
356 | {AS_CONFED_SET, '[', ']'}, | |
357 | {AS_CONFED_SEQUENCE, '(', ')'}, | |
358 | {0}}; | |
359 | ||
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; | |
366 | } | |
718e3744 | 367 | } |
d62a17ae | 368 | return ' '; |
718e3744 | 369 | } |
370 | ||
014b670e | 371 | /* countup asns from this segment and index onward */ |
d62a17ae | 372 | static int assegment_count_asns(struct assegment *seg, int from) |
373 | { | |
374 | int count = 0; | |
375 | while (seg) { | |
376 | if (!from) | |
377 | count += seg->length; | |
378 | else { | |
379 | count += (seg->length - from); | |
380 | from = 0; | |
381 | } | |
382 | seg = seg->next; | |
383 | } | |
384 | return count; | |
385 | } | |
386 | ||
387 | unsigned int aspath_count_confeds(struct aspath *aspath) | |
388 | { | |
389 | int count = 0; | |
390 | struct assegment *seg = aspath->segments; | |
391 | ||
392 | while (seg) { | |
393 | if (seg->type == AS_CONFED_SEQUENCE) | |
394 | count += seg->length; | |
395 | else if (seg->type == AS_CONFED_SET) | |
396 | count++; | |
397 | ||
398 | seg = seg->next; | |
399 | } | |
400 | return count; | |
401 | } | |
402 | ||
403 | unsigned int aspath_count_hops(const struct aspath *aspath) | |
404 | { | |
405 | int count = 0; | |
406 | struct assegment *seg = aspath->segments; | |
407 | ||
408 | while (seg) { | |
409 | if (seg->type == AS_SEQUENCE) | |
410 | count += seg->length; | |
411 | else if (seg->type == AS_SET) | |
412 | count++; | |
413 | ||
414 | seg = seg->next; | |
415 | } | |
416 | return count; | |
fe69a505 | 417 | } |
418 | ||
0b2aa3a0 PJ |
419 | /* Estimate size aspath /might/ take if encoded into an |
420 | * ASPATH attribute. | |
421 | * | |
422 | * This is a quick estimate, not definitive! aspath_put() | |
423 | * may return a different number!! | |
424 | */ | |
d62a17ae | 425 | unsigned int aspath_size(struct aspath *aspath) |
fe69a505 | 426 | { |
d62a17ae | 427 | int size = 0; |
428 | struct assegment *seg = aspath->segments; | |
429 | ||
430 | while (seg) { | |
431 | size += ASSEGMENT_SIZE(seg->length, 1); | |
432 | seg = seg->next; | |
433 | } | |
434 | return size; | |
fe69a505 | 435 | } |
436 | ||
2815e61f | 437 | /* Return highest public ASN in path */ |
d62a17ae | 438 | as_t aspath_highest(struct aspath *aspath) |
439 | { | |
440 | struct assegment *seg = aspath->segments; | |
441 | as_t highest = 0; | |
442 | unsigned int i; | |
443 | ||
444 | while (seg) { | |
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]; | |
449 | seg = seg->next; | |
450 | } | |
451 | return highest; | |
2815e61f PJ |
452 | } |
453 | ||
bc3dd427 | 454 | /* Return the left-most ASN in path */ |
d62a17ae | 455 | as_t aspath_leftmost(struct aspath *aspath) |
bc3dd427 | 456 | { |
d62a17ae | 457 | struct assegment *seg = aspath->segments; |
458 | as_t leftmost = 0; | |
bc3dd427 | 459 | |
d62a17ae | 460 | if (seg && seg->length && seg->type == AS_SEQUENCE) |
461 | leftmost = seg->as[0]; | |
bc3dd427 | 462 | |
d62a17ae | 463 | return leftmost; |
bc3dd427 DW |
464 | } |
465 | ||
0b2aa3a0 | 466 | /* Return 1 if there are any 4-byte ASes in the path */ |
d62a17ae | 467 | unsigned int aspath_has_as4(struct aspath *aspath) |
468 | { | |
469 | struct assegment *seg = aspath->segments; | |
470 | unsigned int i; | |
471 | ||
472 | while (seg) { | |
473 | for (i = 0; i < seg->length; i++) | |
474 | if (seg->as[i] > BGP_AS_MAX) | |
475 | return 1; | |
476 | seg = seg->next; | |
477 | } | |
478 | return 0; | |
0b2aa3a0 PJ |
479 | } |
480 | ||
718e3744 | 481 | /* Convert aspath structure to string expression. */ |
68e1a55b | 482 | static void aspath_make_str_count(struct aspath *as, bool make_json) |
d62a17ae | 483 | { |
484 | struct assegment *seg; | |
485 | int str_size; | |
486 | int len = 0; | |
487 | char *str_buf; | |
488 | json_object *jaspath_segments = NULL; | |
489 | json_object *jseg = NULL; | |
490 | json_object *jseg_list = NULL; | |
491 | ||
68e1a55b DS |
492 | if (make_json) { |
493 | as->json = json_object_new_object(); | |
494 | jaspath_segments = json_object_new_array(); | |
495 | } | |
d62a17ae | 496 | |
497 | /* Empty aspath. */ | |
498 | if (!as->segments) { | |
68e1a55b DS |
499 | if (make_json) { |
500 | json_object_string_add(as->json, "string", "Local"); | |
996c9314 LB |
501 | json_object_object_add(as->json, "segments", |
502 | jaspath_segments); | |
68e1a55b DS |
503 | json_object_int_add(as->json, "length", 0); |
504 | } | |
d62a17ae | 505 | as->str = XMALLOC(MTYPE_AS_STR, 1); |
506 | as->str[0] = '\0'; | |
507 | as->str_len = 0; | |
508 | return; | |
509 | } | |
510 | ||
511 | seg = as->segments; | |
512 | ||
513 | /* ASN takes 5 to 10 chars plus seperator, see below. | |
514 | * If there is one differing segment type, we need an additional | |
515 | * 2 chars for segment delimiters, and the final '\0'. | |
516 | * Hopefully this is large enough to avoid hitting the realloc | |
517 | * code below for most common sequences. | |
518 | * | |
519 | * This was changed to 10 after the well-known BGP assertion, which | |
520 | * had hit some parts of the Internet in May of 2009. | |
521 | */ | |
014b670e | 522 | #define ASN_STR_LEN (10 + 1) |
d62a17ae | 523 | str_size = MAX(assegment_count_asns(seg, 0) * ASN_STR_LEN + 2 + 1, |
524 | ASPATH_STR_DEFAULT_LEN); | |
525 | str_buf = XMALLOC(MTYPE_AS_STR, str_size); | |
526 | ||
527 | while (seg) { | |
528 | int i; | |
529 | char seperator; | |
530 | ||
531 | /* Check AS type validity. Set seperator for segment */ | |
532 | switch (seg->type) { | |
533 | case AS_SET: | |
534 | case AS_CONFED_SET: | |
535 | seperator = ','; | |
536 | break; | |
537 | case AS_SEQUENCE: | |
538 | case AS_CONFED_SEQUENCE: | |
539 | seperator = ' '; | |
540 | break; | |
541 | default: | |
542 | XFREE(MTYPE_AS_STR, str_buf); | |
543 | as->str = NULL; | |
544 | as->str_len = 0; | |
545 | json_object_free(as->json); | |
546 | as->json = NULL; | |
68e1a55b | 547 | |
d62a17ae | 548 | return; |
549 | } | |
550 | ||
551 | /* We might need to increase str_buf, particularly if path has | |
552 | * differing segments types, our initial guesstimate above will | |
553 | * have been wrong. Need 10 chars for ASN, a seperator each and | |
554 | * potentially two segment delimiters, plus a space between each | |
555 | * segment and trailing zero. | |
556 | * | |
557 | * This definitely didn't work with the value of 5 bytes and | |
558 | * 32-bit ASNs. | |
559 | */ | |
014b670e | 560 | #define SEGMENT_STR_LEN(X) (((X)->length * ASN_STR_LEN) + 2 + 1 + 1) |
d62a17ae | 561 | if ((len + SEGMENT_STR_LEN(seg)) > str_size) { |
562 | str_size = len + SEGMENT_STR_LEN(seg); | |
563 | str_buf = XREALLOC(MTYPE_AS_STR, str_buf, str_size); | |
564 | } | |
014b670e DO |
565 | #undef ASN_STR_LEN |
566 | #undef SEGMENT_STR_LEN | |
d62a17ae | 567 | |
568 | if (seg->type != AS_SEQUENCE) | |
569 | len += snprintf( | |
570 | str_buf + len, str_size - len, "%c", | |
571 | aspath_delimiter_char(seg->type, AS_SEG_START)); | |
572 | ||
68e1a55b DS |
573 | if (make_json) |
574 | jseg_list = json_object_new_array(); | |
d62a17ae | 575 | |
576 | /* write out the ASNs, with their seperators, bar the last one*/ | |
577 | for (i = 0; i < seg->length; i++) { | |
68e1a55b | 578 | if (make_json) |
996c9314 LB |
579 | json_object_array_add( |
580 | jseg_list, | |
581 | json_object_new_int(seg->as[i])); | |
d62a17ae | 582 | |
583 | len += snprintf(str_buf + len, str_size - len, "%u", | |
584 | seg->as[i]); | |
585 | ||
586 | if (i < (seg->length - 1)) | |
587 | len += snprintf(str_buf + len, str_size - len, | |
588 | "%c", seperator); | |
589 | } | |
590 | ||
68e1a55b DS |
591 | if (make_json) { |
592 | jseg = json_object_new_object(); | |
996c9314 LB |
593 | json_object_string_add( |
594 | jseg, "type", | |
595 | aspath_segment_type_str[seg->type]); | |
68e1a55b DS |
596 | json_object_object_add(jseg, "list", jseg_list); |
597 | json_object_array_add(jaspath_segments, jseg); | |
598 | } | |
d62a17ae | 599 | |
600 | if (seg->type != AS_SEQUENCE) | |
601 | len += snprintf( | |
602 | str_buf + len, str_size - len, "%c", | |
603 | aspath_delimiter_char(seg->type, AS_SEG_END)); | |
604 | if (seg->next) | |
605 | len += snprintf(str_buf + len, str_size - len, " "); | |
606 | ||
607 | seg = seg->next; | |
608 | } | |
609 | ||
610 | assert(len < str_size); | |
611 | ||
612 | str_buf[len] = '\0'; | |
613 | as->str = str_buf; | |
614 | as->str_len = len; | |
615 | ||
68e1a55b DS |
616 | if (make_json) { |
617 | json_object_string_add(as->json, "string", str_buf); | |
618 | json_object_object_add(as->json, "segments", jaspath_segments); | |
619 | json_object_int_add(as->json, "length", aspath_count_hops(as)); | |
620 | } | |
621 | ||
d62a17ae | 622 | return; |
623 | } | |
624 | ||
68e1a55b | 625 | void aspath_str_update(struct aspath *as, bool make_json) |
d62a17ae | 626 | { |
627 | if (as->str) | |
628 | XFREE(MTYPE_AS_STR, as->str); | |
629 | ||
630 | if (as->json) { | |
631 | json_object_free(as->json); | |
632 | as->json = NULL; | |
633 | } | |
634 | ||
68e1a55b | 635 | aspath_make_str_count(as, make_json); |
fe69a505 | 636 | } |
637 | ||
718e3744 | 638 | /* Intern allocated AS path. */ |
d62a17ae | 639 | struct aspath *aspath_intern(struct aspath *aspath) |
718e3744 | 640 | { |
d62a17ae | 641 | struct aspath *find; |
f669f7d2 | 642 | |
d62a17ae | 643 | /* Assert this AS path structure is not interned and has the string |
644 | representation built. */ | |
645 | assert(aspath->refcnt == 0); | |
646 | assert(aspath->str); | |
718e3744 | 647 | |
d62a17ae | 648 | /* Check AS path hash. */ |
649 | find = hash_get(ashash, aspath, hash_alloc_intern); | |
650 | if (find != aspath) | |
651 | aspath_free(aspath); | |
718e3744 | 652 | |
d62a17ae | 653 | find->refcnt++; |
718e3744 | 654 | |
d62a17ae | 655 | return find; |
718e3744 | 656 | } |
657 | ||
658 | /* Duplicate aspath structure. Created same aspath structure but | |
659 | reference count and AS path string is cleared. */ | |
d62a17ae | 660 | struct aspath *aspath_dup(struct aspath *aspath) |
718e3744 | 661 | { |
d62a17ae | 662 | unsigned short buflen = aspath->str_len + 1; |
663 | struct aspath *new; | |
718e3744 | 664 | |
d62a17ae | 665 | new = XCALLOC(MTYPE_AS_PATH, sizeof(struct aspath)); |
666 | new->json = NULL; | |
718e3744 | 667 | |
d62a17ae | 668 | if (aspath->segments) |
669 | new->segments = assegment_dup_all(aspath->segments); | |
718e3744 | 670 | |
d62a17ae | 671 | if (!aspath->str) |
672 | return new; | |
f669f7d2 | 673 | |
d62a17ae | 674 | new->str = XMALLOC(MTYPE_AS_STR, buflen); |
675 | new->str_len = aspath->str_len; | |
f669f7d2 | 676 | |
d62a17ae | 677 | /* copy the string data */ |
678 | if (aspath->str_len > 0) | |
679 | memcpy(new->str, aspath->str, buflen); | |
680 | else | |
681 | new->str[0] = '\0'; | |
718e3744 | 682 | |
d62a17ae | 683 | return new; |
718e3744 | 684 | } |
685 | ||
d62a17ae | 686 | static void *aspath_hash_alloc(void *arg) |
718e3744 | 687 | { |
d62a17ae | 688 | const struct aspath *aspath = arg; |
689 | struct aspath *new; | |
718e3744 | 690 | |
d62a17ae | 691 | /* Malformed AS path value. */ |
692 | assert(aspath->str); | |
718e3744 | 693 | |
d62a17ae | 694 | /* New aspath structure is needed. */ |
695 | new = XMALLOC(MTYPE_AS_PATH, sizeof(struct aspath)); | |
f669f7d2 | 696 | |
d62a17ae | 697 | /* Reuse segments and string representation */ |
698 | new->refcnt = 0; | |
699 | new->segments = aspath->segments; | |
700 | new->str = aspath->str; | |
701 | new->str_len = aspath->str_len; | |
702 | new->json = aspath->json; | |
f669f7d2 | 703 | |
d62a17ae | 704 | return new; |
718e3744 | 705 | } |
706 | ||
ab005298 | 707 | /* parse as-segment byte stream in struct assegment */ |
d62a17ae | 708 | static int assegments_parse(struct stream *s, size_t length, |
709 | struct assegment **result, int use32bit) | |
710 | { | |
711 | struct assegment_header segh; | |
712 | struct assegment *seg, *prev = NULL, *head = NULL; | |
713 | size_t bytes = 0; | |
714 | ||
715 | /* empty aspath (ie iBGP or somesuch) */ | |
716 | if (length == 0) | |
717 | return 0; | |
718 | ||
719 | if (BGP_DEBUG(as4, AS4_SEGMENT)) | |
720 | zlog_debug( | |
721 | "[AS4SEG] Parse aspath segment: got total byte length %lu", | |
722 | (unsigned long)length); | |
723 | /* basic checks */ | |
724 | if ((STREAM_READABLE(s) < length) | |
725 | || (STREAM_READABLE(s) < AS_HEADER_SIZE) | |
726 | || (length % AS16_VALUE_SIZE)) | |
727 | return -1; | |
728 | ||
729 | while (bytes < length) { | |
730 | int i; | |
731 | size_t seg_size; | |
732 | ||
733 | if ((length - bytes) <= AS_HEADER_SIZE) { | |
734 | if (head) | |
735 | assegment_free_all(head); | |
736 | return -1; | |
737 | } | |
738 | ||
739 | /* softly softly, get the header first on its own */ | |
740 | segh.type = stream_getc(s); | |
741 | segh.length = stream_getc(s); | |
742 | ||
743 | seg_size = ASSEGMENT_SIZE(segh.length, use32bit); | |
744 | ||
745 | if (BGP_DEBUG(as4, AS4_SEGMENT)) | |
746 | zlog_debug( | |
747 | "[AS4SEG] Parse aspath segment: got type %d, length %d", | |
748 | segh.type, segh.length); | |
749 | ||
750 | /* check it.. */ | |
751 | if (((bytes + seg_size) > length) | |
752 | /* 1771bis 4.3b: seg length contains one or more */ | |
753 | || (segh.length == 0) | |
754 | /* Paranoia in case someone changes type of segment length. | |
755 | * Shift both values by 0x10 to make the comparison operate | |
756 | * on more, than 8 bits (otherwise it's a warning, bug | |
757 | * #564). | |
758 | */ | |
759 | || ((sizeof segh.length > 1) | |
760 | && (0x10 + segh.length > 0x10 + AS_SEGMENT_MAX))) { | |
761 | if (head) | |
762 | assegment_free_all(head); | |
763 | return -1; | |
764 | } | |
765 | ||
766 | switch (segh.type) { | |
767 | case AS_SEQUENCE: | |
768 | case AS_SET: | |
769 | case AS_CONFED_SEQUENCE: | |
770 | case AS_CONFED_SET: | |
771 | break; | |
772 | default: | |
773 | if (head) | |
774 | assegment_free_all(head); | |
775 | return -1; | |
776 | } | |
777 | ||
778 | /* now its safe to trust lengths */ | |
779 | seg = assegment_new(segh.type, segh.length); | |
780 | ||
781 | if (head) | |
782 | prev->next = seg; | |
783 | else /* it's the first segment */ | |
784 | head = prev = seg; | |
785 | ||
786 | for (i = 0; i < segh.length; i++) | |
787 | seg->as[i] = | |
788 | (use32bit) ? stream_getl(s) : stream_getw(s); | |
789 | ||
790 | bytes += seg_size; | |
791 | ||
792 | if (BGP_DEBUG(as4, AS4_SEGMENT)) | |
793 | zlog_debug( | |
794 | "[AS4SEG] Parse aspath segment: Bytes now: %lu", | |
795 | (unsigned long)bytes); | |
796 | ||
797 | prev = seg; | |
798 | } | |
799 | ||
800 | *result = assegment_normalise(head); | |
801 | return 0; | |
fe69a505 | 802 | } |
803 | ||
ab005298 PJ |
804 | /* AS path parse function. pnt is a pointer to byte stream and length |
805 | is length of byte stream. If there is same AS path in the the AS | |
d62a17ae | 806 | path hash then return it else make new AS path structure. |
807 | ||
b881c707 | 808 | On error NULL is returned. |
cddb8112 | 809 | */ |
d62a17ae | 810 | struct aspath *aspath_parse(struct stream *s, size_t length, int use32bit) |
811 | { | |
812 | struct aspath as; | |
813 | struct aspath *find; | |
814 | ||
815 | /* If length is odd it's malformed AS path. */ | |
816 | /* Nit-picking: if (use32bit == 0) it is malformed if odd, | |
817 | * otherwise its malformed when length is larger than 2 and (length-2) | |
818 | * is not dividable by 4. | |
819 | * But... this time we're lazy | |
820 | */ | |
821 | if (length % AS16_VALUE_SIZE) | |
822 | return NULL; | |
823 | ||
824 | memset(&as, 0, sizeof(struct aspath)); | |
825 | if (assegments_parse(s, length, &as.segments, use32bit) < 0) | |
826 | return NULL; | |
827 | ||
828 | /* If already same aspath exist then return it. */ | |
829 | find = hash_get(ashash, &as, aspath_hash_alloc); | |
830 | ||
831 | /* bug! should not happen, let the daemon crash below */ | |
832 | assert(find); | |
833 | ||
834 | /* if the aspath was already hashed free temporary memory. */ | |
835 | if (find->refcnt) { | |
836 | assegment_free_all(as.segments); | |
837 | /* aspath_key_make() always updates the string */ | |
838 | XFREE(MTYPE_AS_STR, as.str); | |
839 | if (as.json) { | |
840 | json_object_free(as.json); | |
841 | as.json = NULL; | |
842 | } | |
4390a8ee | 843 | } |
f669f7d2 | 844 | |
d62a17ae | 845 | find->refcnt++; |
718e3744 | 846 | |
d62a17ae | 847 | return find; |
718e3744 | 848 | } |
849 | ||
d62a17ae | 850 | static void assegment_data_put(struct stream *s, as_t *as, int num, |
851 | int use32bit) | |
fe69a505 | 852 | { |
d62a17ae | 853 | int i; |
854 | assert(num <= AS_SEGMENT_MAX); | |
855 | ||
856 | for (i = 0; i < num; i++) | |
857 | if (use32bit) | |
858 | stream_putl(s, as[i]); | |
859 | else { | |
860 | if (as[i] <= BGP_AS_MAX) | |
861 | stream_putw(s, as[i]); | |
862 | else | |
863 | stream_putw(s, BGP_AS_TRANS); | |
864 | } | |
fe69a505 | 865 | } |
718e3744 | 866 | |
d7c0a89a | 867 | static size_t assegment_header_put(struct stream *s, uint8_t type, int length) |
718e3744 | 868 | { |
d62a17ae | 869 | size_t lenp; |
870 | assert(length <= AS_SEGMENT_MAX); | |
871 | stream_putc(s, type); | |
872 | lenp = stream_get_endp(s); | |
873 | stream_putc(s, length); | |
874 | return lenp; | |
fe69a505 | 875 | } |
718e3744 | 876 | |
fe69a505 | 877 | /* write aspath data to stream */ |
d62a17ae | 878 | size_t aspath_put(struct stream *s, struct aspath *as, int use32bit) |
879 | { | |
880 | struct assegment *seg = as->segments; | |
881 | size_t bytes = 0; | |
882 | ||
883 | if (!seg || seg->length == 0) | |
884 | return 0; | |
885 | ||
886 | if (seg) { | |
887 | /* | |
888 | * Hey, what do we do when we have > STREAM_WRITABLE(s) here? | |
889 | * At the moment, we would write out a partial aspath, and our | |
890 | * peer | |
891 | * will complain and drop the session :-/ | |
892 | * | |
893 | * The general assumption here is that many things tested will | |
894 | * never happen. And, in real live, up to now, they have not. | |
895 | */ | |
9d303b37 DL |
896 | while (seg && (ASSEGMENT_LEN(seg, use32bit) |
897 | <= STREAM_WRITEABLE(s))) { | |
d62a17ae | 898 | struct assegment *next = seg->next; |
899 | int written = 0; | |
900 | int asns_packed = 0; | |
901 | size_t lenp; | |
902 | ||
903 | /* Overlength segments have to be split up */ | |
904 | while ((seg->length - written) > AS_SEGMENT_MAX) { | |
905 | assegment_header_put(s, seg->type, | |
906 | AS_SEGMENT_MAX); | |
907 | assegment_data_put(s, seg->as, AS_SEGMENT_MAX, | |
908 | use32bit); | |
909 | written += AS_SEGMENT_MAX; | |
996c9314 LB |
910 | bytes += ASSEGMENT_SIZE(AS_SEGMENT_MAX, |
911 | use32bit); | |
d62a17ae | 912 | } |
913 | ||
914 | /* write the final segment, probably is also the first | |
915 | */ | |
916 | lenp = assegment_header_put(s, seg->type, | |
917 | seg->length - written); | |
918 | assegment_data_put(s, (seg->as + written), | |
919 | seg->length - written, use32bit); | |
920 | ||
921 | /* Sequence-type segments can be 'packed' together | |
922 | * Case of a segment which was overlength and split up | |
923 | * will be missed here, but that doesn't matter. | |
924 | */ | |
925 | while (next && ASSEGMENTS_PACKABLE(seg, next)) { | |
926 | /* NB: We should never normally get here given | |
927 | * we | |
928 | * normalise aspath data when parse them. | |
929 | * However, better | |
930 | * safe than sorry. We potentially could call | |
931 | * assegment_normalise here instead, but it's | |
932 | * cheaper and | |
933 | * easier to do it on the fly here rather than | |
934 | * go through | |
935 | * the segment list twice every time we write | |
936 | * out | |
937 | * aspath's. | |
938 | */ | |
939 | ||
940 | /* Next segment's data can fit in this one */ | |
941 | assegment_data_put(s, next->as, next->length, | |
942 | use32bit); | |
943 | ||
944 | /* update the length of the segment header */ | |
945 | stream_putc_at(s, lenp, | |
946 | seg->length - written | |
947 | + next->length); | |
948 | asns_packed += next->length; | |
949 | ||
950 | next = next->next; | |
951 | } | |
952 | ||
953 | bytes += ASSEGMENT_SIZE( | |
954 | seg->length - written + asns_packed, use32bit); | |
955 | seg = next; | |
956 | } | |
957 | } | |
958 | return bytes; | |
fe69a505 | 959 | } |
960 | ||
961 | /* This is for SNMP BGP4PATHATTRASPATHSEGMENT | |
962 | * We have no way to manage the storage, so we use a static stream | |
963 | * wrapper around aspath_put. | |
964 | */ | |
d7c0a89a | 965 | uint8_t *aspath_snmp_pathseg(struct aspath *as, size_t *varlen) |
fe69a505 | 966 | { |
967 | #define SNMP_PATHSEG_MAX 1024 | |
8fdc32ab | 968 | |
d62a17ae | 969 | if (!snmp_stream) |
970 | snmp_stream = stream_new(SNMP_PATHSEG_MAX); | |
971 | else | |
972 | stream_reset(snmp_stream); | |
973 | ||
974 | if (!as) { | |
975 | *varlen = 0; | |
976 | return NULL; | |
977 | } | |
978 | aspath_put(snmp_stream, as, 0); /* use 16 bit for now here */ | |
979 | ||
980 | *varlen = stream_get_endp(snmp_stream); | |
981 | return stream_pnt(snmp_stream); | |
982 | } | |
983 | ||
fe69a505 | 984 | #define min(A,B) ((A) < (B) ? (A) : (B)) |
718e3744 | 985 | |
d62a17ae | 986 | static struct assegment *aspath_aggregate_as_set_add(struct aspath *aspath, |
987 | struct assegment *asset, | |
988 | as_t as) | |
989 | { | |
990 | int i; | |
991 | ||
992 | /* If this is first AS set member, create new as-set segment. */ | |
993 | if (asset == NULL) { | |
994 | asset = assegment_new(AS_SET, 1); | |
995 | if (!aspath->segments) | |
996 | aspath->segments = asset; | |
997 | else { | |
998 | struct assegment *seg = aspath->segments; | |
999 | while (seg->next) | |
1000 | seg = seg->next; | |
1001 | seg->next = asset; | |
1002 | } | |
1003 | asset->type = AS_SET; | |
1004 | asset->length = 1; | |
1005 | asset->as[0] = as; | |
1006 | } else { | |
1007 | /* Check this AS value already exists or not. */ | |
1008 | for (i = 0; i < asset->length; i++) | |
1009 | if (asset->as[i] == as) | |
1010 | return asset; | |
1011 | ||
1012 | asset->length++; | |
1013 | asset->as = XREALLOC(MTYPE_AS_SEG_DATA, asset->as, | |
1014 | asset->length * AS_VALUE_SIZE); | |
1015 | asset->as[asset->length - 1] = as; | |
1016 | } | |
1017 | ||
1018 | ||
1019 | return asset; | |
718e3744 | 1020 | } |
1021 | ||
1022 | /* Modify as1 using as2 for aggregation. */ | |
d62a17ae | 1023 | struct aspath *aspath_aggregate(struct aspath *as1, struct aspath *as2) |
1024 | { | |
1025 | int i; | |
1026 | int minlen = 0; | |
1027 | int match = 0; | |
1028 | int from; | |
1029 | struct assegment *seg1 = as1->segments; | |
1030 | struct assegment *seg2 = as2->segments; | |
1031 | struct aspath *aspath = NULL; | |
1032 | struct assegment *asset = NULL; | |
1033 | struct assegment *prevseg = NULL; | |
1034 | ||
1035 | /* First of all check common leading sequence. */ | |
1036 | while (seg1 && seg2) { | |
1037 | /* Check segment type. */ | |
1038 | if (seg1->type != seg2->type) | |
1039 | break; | |
1040 | ||
1041 | /* Minimum segment length. */ | |
1042 | minlen = min(seg1->length, seg2->length); | |
1043 | ||
1044 | for (match = 0; match < minlen; match++) | |
1045 | if (seg1->as[match] != seg2->as[match]) | |
1046 | break; | |
1047 | ||
1048 | if (match) { | |
1049 | struct assegment *seg = assegment_new(seg1->type, 0); | |
1050 | ||
1051 | seg = assegment_append_asns(seg, seg1->as, match); | |
1052 | ||
1053 | if (!aspath) { | |
1054 | aspath = aspath_new(); | |
1055 | aspath->segments = seg; | |
1056 | } else | |
1057 | prevseg->next = seg; | |
1058 | ||
1059 | prevseg = seg; | |
1060 | } | |
1061 | ||
1062 | if (match != minlen || match != seg1->length | |
1063 | || seg1->length != seg2->length) | |
1064 | break; | |
1065 | /* We are moving on to the next segment to reset match */ | |
1066 | else | |
1067 | match = 0; | |
1068 | ||
1069 | seg1 = seg1->next; | |
1070 | seg2 = seg2->next; | |
1071 | } | |
1072 | ||
1073 | if (!aspath) | |
1074 | aspath = aspath_new(); | |
1075 | ||
1076 | /* Make as-set using rest of all information. */ | |
1077 | from = match; | |
1078 | while (seg1) { | |
1079 | for (i = from; i < seg1->length; i++) | |
1080 | asset = aspath_aggregate_as_set_add(aspath, asset, | |
1081 | seg1->as[i]); | |
1082 | ||
1083 | from = 0; | |
1084 | seg1 = seg1->next; | |
718e3744 | 1085 | } |
1086 | ||
d62a17ae | 1087 | from = match; |
1088 | while (seg2) { | |
1089 | for (i = from; i < seg2->length; i++) | |
1090 | asset = aspath_aggregate_as_set_add(aspath, asset, | |
1091 | seg2->as[i]); | |
1092 | ||
1093 | from = 0; | |
1094 | seg2 = seg2->next; | |
1095 | } | |
1096 | ||
1097 | assegment_normalise(aspath->segments); | |
68e1a55b | 1098 | aspath_str_update(aspath, false); |
d62a17ae | 1099 | return aspath; |
718e3744 | 1100 | } |
1101 | ||
1102 | /* When a BGP router receives an UPDATE with an MP_REACH_NLRI | |
1103 | attribute, check the leftmost AS number in the AS_PATH attribute is | |
d62a17ae | 1104 | or not the peer's AS number. */ |
1105 | int aspath_firstas_check(struct aspath *aspath, as_t asno) | |
718e3744 | 1106 | { |
d62a17ae | 1107 | if ((aspath == NULL) || (aspath->segments == NULL)) |
1108 | return 0; | |
1109 | ||
1110 | if (aspath->segments && (aspath->segments->type == AS_SEQUENCE) | |
1111 | && (aspath->segments->as[0] == asno)) | |
1112 | return 1; | |
718e3744 | 1113 | |
d62a17ae | 1114 | return 0; |
718e3744 | 1115 | } |
1116 | ||
d62a17ae | 1117 | unsigned int aspath_get_first_as(struct aspath *aspath) |
06370dac | 1118 | { |
d62a17ae | 1119 | if (aspath == NULL || aspath->segments == NULL) |
1120 | return 0; | |
06370dac | 1121 | |
d62a17ae | 1122 | return aspath->segments->as[0]; |
06370dac DW |
1123 | } |
1124 | ||
d62a17ae | 1125 | unsigned int aspath_get_last_as(struct aspath *aspath) |
aac9ef6c | 1126 | { |
d62a17ae | 1127 | int i; |
1128 | unsigned int last_as = 0; | |
1129 | const struct assegment *seg; | |
aac9ef6c | 1130 | |
d62a17ae | 1131 | if (aspath == NULL || aspath->segments == NULL) |
1132 | return last_as; | |
aac9ef6c | 1133 | |
d62a17ae | 1134 | seg = aspath->segments; |
aac9ef6c | 1135 | |
d62a17ae | 1136 | while (seg) { |
1137 | if (seg->type == AS_SEQUENCE || seg->type == AS_CONFED_SEQUENCE) | |
1138 | for (i = 0; i < seg->length; i++) | |
1139 | last_as = seg->as[i]; | |
1140 | seg = seg->next; | |
1141 | } | |
aac9ef6c | 1142 | |
d62a17ae | 1143 | return last_as; |
aac9ef6c DW |
1144 | } |
1145 | ||
1f742f21 | 1146 | /* AS path loop check. If aspath contains asno then return >= 1. */ |
d62a17ae | 1147 | int aspath_loop_check(struct aspath *aspath, as_t asno) |
1148 | { | |
1149 | struct assegment *seg; | |
1150 | int count = 0; | |
1151 | ||
1152 | if ((aspath == NULL) || (aspath->segments == NULL)) | |
1153 | return 0; | |
1154 | ||
1155 | seg = aspath->segments; | |
1156 | ||
1157 | while (seg) { | |
1158 | int i; | |
1159 | ||
1160 | for (i = 0; i < seg->length; i++) | |
1161 | if (seg->as[i] == asno) | |
1162 | count++; | |
1163 | ||
1164 | seg = seg->next; | |
1165 | } | |
1166 | return count; | |
718e3744 | 1167 | } |
1168 | ||
1169 | /* When all of AS path is private AS return 1. */ | |
d62a17ae | 1170 | int aspath_private_as_check(struct aspath *aspath) |
718e3744 | 1171 | { |
d62a17ae | 1172 | struct assegment *seg; |
5000f21c | 1173 | |
d62a17ae | 1174 | if (!(aspath && aspath->segments)) |
1175 | return 0; | |
5000f21c | 1176 | |
d62a17ae | 1177 | seg = aspath->segments; |
718e3744 | 1178 | |
d62a17ae | 1179 | while (seg) { |
1180 | int i; | |
5000f21c | 1181 | |
d62a17ae | 1182 | for (i = 0; i < seg->length; i++) { |
1183 | if (!BGP_AS_IS_PRIVATE(seg->as[i])) | |
1184 | return 0; | |
1185 | } | |
1186 | seg = seg->next; | |
718e3744 | 1187 | } |
d62a17ae | 1188 | return 1; |
718e3744 | 1189 | } |
1190 | ||
c7122e14 | 1191 | /* Return True if the entire ASPATH consist of the specified ASN */ |
d62a17ae | 1192 | int aspath_single_asn_check(struct aspath *aspath, as_t asn) |
c7122e14 | 1193 | { |
d62a17ae | 1194 | struct assegment *seg; |
c7122e14 | 1195 | |
d62a17ae | 1196 | if (!(aspath && aspath->segments)) |
1197 | return 0; | |
c7122e14 | 1198 | |
d62a17ae | 1199 | seg = aspath->segments; |
c7122e14 | 1200 | |
d62a17ae | 1201 | while (seg) { |
1202 | int i; | |
c7122e14 | 1203 | |
d62a17ae | 1204 | for (i = 0; i < seg->length; i++) { |
1205 | if (seg->as[i] != asn) | |
1206 | return 0; | |
1207 | } | |
1208 | seg = seg->next; | |
c7122e14 | 1209 | } |
d62a17ae | 1210 | return 1; |
c7122e14 DS |
1211 | } |
1212 | ||
1213 | /* Replace all instances of the target ASN with our own ASN */ | |
d62a17ae | 1214 | struct aspath *aspath_replace_specific_asn(struct aspath *aspath, |
1215 | as_t target_asn, as_t our_asn) | |
c7122e14 | 1216 | { |
d62a17ae | 1217 | struct aspath *new; |
1218 | struct assegment *seg; | |
c7122e14 | 1219 | |
d62a17ae | 1220 | new = aspath_dup(aspath); |
1221 | seg = new->segments; | |
c7122e14 | 1222 | |
d62a17ae | 1223 | while (seg) { |
1224 | int i; | |
c7122e14 | 1225 | |
d62a17ae | 1226 | for (i = 0; i < seg->length; i++) { |
1227 | if (seg->as[i] == target_asn) | |
1228 | seg->as[i] = our_asn; | |
1229 | } | |
1230 | seg = seg->next; | |
c7122e14 | 1231 | } |
c7122e14 | 1232 | |
68e1a55b | 1233 | aspath_str_update(new, false); |
d62a17ae | 1234 | return new; |
c7122e14 DS |
1235 | } |
1236 | ||
5000f21c | 1237 | /* Replace all private ASNs with our own ASN */ |
d62a17ae | 1238 | struct aspath *aspath_replace_private_asns(struct aspath *aspath, as_t asn) |
5000f21c | 1239 | { |
d62a17ae | 1240 | struct aspath *new; |
1241 | struct assegment *seg; | |
5000f21c | 1242 | |
d62a17ae | 1243 | new = aspath_dup(aspath); |
1244 | seg = new->segments; | |
5000f21c | 1245 | |
d62a17ae | 1246 | while (seg) { |
1247 | int i; | |
5000f21c | 1248 | |
d62a17ae | 1249 | for (i = 0; i < seg->length; i++) { |
1250 | if (BGP_AS_IS_PRIVATE(seg->as[i])) | |
1251 | seg->as[i] = asn; | |
1252 | } | |
1253 | seg = seg->next; | |
5000f21c | 1254 | } |
5000f21c | 1255 | |
68e1a55b | 1256 | aspath_str_update(new, false); |
d62a17ae | 1257 | return new; |
5000f21c DS |
1258 | } |
1259 | ||
1260 | /* Remove all private ASNs */ | |
d62a17ae | 1261 | struct aspath *aspath_remove_private_asns(struct aspath *aspath) |
1262 | { | |
1263 | struct aspath *new; | |
1264 | struct assegment *seg; | |
1265 | struct assegment *new_seg; | |
1266 | struct assegment *last_new_seg; | |
1267 | int i; | |
1268 | int j; | |
1269 | int public = 0; | |
1270 | ||
1271 | new = XCALLOC(MTYPE_AS_PATH, sizeof(struct aspath)); | |
1272 | ||
1273 | new->json = NULL; | |
1274 | new_seg = NULL; | |
1275 | last_new_seg = NULL; | |
1276 | seg = aspath->segments; | |
1277 | while (seg) { | |
1278 | public | |
1279 | = 0; | |
1280 | for (i = 0; i < seg->length; i++) { | |
1281 | // ASN is public | |
1282 | if (!BGP_AS_IS_PRIVATE(seg->as[i])) { | |
1283 | public | |
1284 | ++; | |
1285 | } | |
1286 | } | |
1287 | ||
1288 | // The entire segment is private so skip it | |
1289 | if (!public) { | |
1290 | seg = seg->next; | |
1291 | continue; | |
1292 | } | |
1293 | ||
1294 | // The entire segment is public so copy it | |
1295 | else if (public == seg->length) { | |
1296 | new_seg = assegment_dup(seg); | |
1297 | } | |
1298 | ||
1299 | // The segment is a mix of public and private ASNs. Copy as many | |
1300 | // spots as | |
1301 | // there are public ASNs then come back and fill in only the | |
1302 | // public ASNs. | |
1303 | else { | |
1304 | new_seg = assegment_new(seg->type, public); | |
1305 | j = 0; | |
1306 | for (i = 0; i < seg->length; i++) { | |
1307 | // ASN is public | |
1308 | if (!BGP_AS_IS_PRIVATE(seg->as[i])) { | |
1309 | new_seg->as[j] = seg->as[i]; | |
1310 | j++; | |
1311 | } | |
1312 | } | |
1313 | } | |
1314 | ||
1315 | // This is the first segment so set the aspath segments pointer | |
1316 | // to this one | |
1317 | if (!last_new_seg) | |
1318 | new->segments = new_seg; | |
1319 | else | |
1320 | last_new_seg->next = new_seg; | |
1321 | ||
1322 | last_new_seg = new_seg; | |
1323 | seg = seg->next; | |
1324 | } | |
1325 | ||
68e1a55b | 1326 | aspath_str_update(new, false); |
d62a17ae | 1327 | return new; |
1328 | } | |
1329 | ||
1330 | /* AS path confed check. If aspath contains confed set or sequence then return | |
1331 | * 1. */ | |
1332 | int aspath_confed_check(struct aspath *aspath) | |
1333 | { | |
1334 | struct assegment *seg; | |
1335 | ||
1336 | if (!(aspath && aspath->segments)) | |
1337 | return 0; | |
1338 | ||
1339 | seg = aspath->segments; | |
1340 | ||
1341 | while (seg) { | |
1342 | if (seg->type == AS_CONFED_SET | |
1343 | || seg->type == AS_CONFED_SEQUENCE) | |
1344 | return 1; | |
1345 | seg = seg->next; | |
1346 | } | |
1347 | return 0; | |
ca87e1d3 VT |
1348 | } |
1349 | ||
1350 | /* Leftmost AS path segment confed check. If leftmost AS segment is of type | |
1351 | AS_CONFED_SEQUENCE or AS_CONFED_SET then return 1. */ | |
d62a17ae | 1352 | int aspath_left_confed_check(struct aspath *aspath) |
ca87e1d3 VT |
1353 | { |
1354 | ||
d62a17ae | 1355 | if (!(aspath && aspath->segments)) |
1356 | return 0; | |
ca87e1d3 | 1357 | |
d62a17ae | 1358 | if ((aspath->segments->type == AS_CONFED_SEQUENCE) |
1359 | || (aspath->segments->type == AS_CONFED_SET)) | |
1360 | return 1; | |
ca87e1d3 | 1361 | |
d62a17ae | 1362 | return 0; |
ca87e1d3 VT |
1363 | } |
1364 | ||
718e3744 | 1365 | /* Merge as1 to as2. as2 should be uninterned aspath. */ |
d62a17ae | 1366 | static struct aspath *aspath_merge(struct aspath *as1, struct aspath *as2) |
718e3744 | 1367 | { |
d62a17ae | 1368 | struct assegment *last, *new; |
718e3744 | 1369 | |
d62a17ae | 1370 | if (!as1 || !as2) |
1371 | return NULL; | |
718e3744 | 1372 | |
d62a17ae | 1373 | last = new = assegment_dup_all(as1->segments); |
1374 | ||
1375 | /* find the last valid segment */ | |
1376 | while (last && last->next) | |
1377 | last = last->next; | |
1378 | ||
1379 | last->next = as2->segments; | |
1380 | as2->segments = new; | |
68e1a55b | 1381 | aspath_str_update(as2, false); |
d62a17ae | 1382 | return as2; |
718e3744 | 1383 | } |
1384 | ||
1385 | /* Prepend as1 to as2. as2 should be uninterned aspath. */ | |
d62a17ae | 1386 | struct aspath *aspath_prepend(struct aspath *as1, struct aspath *as2) |
1387 | { | |
1388 | struct assegment *seg1; | |
1389 | struct assegment *seg2; | |
1390 | ||
1391 | if (!as1 || !as2) | |
1392 | return NULL; | |
1393 | ||
1394 | seg1 = as1->segments; | |
1395 | seg2 = as2->segments; | |
1396 | ||
1397 | /* If as2 is empty, only need to dupe as1's chain onto as2 */ | |
1398 | if (seg2 == NULL) { | |
1399 | as2->segments = assegment_dup_all(as1->segments); | |
68e1a55b | 1400 | aspath_str_update(as2, false); |
d62a17ae | 1401 | return as2; |
1402 | } | |
1403 | ||
1404 | /* If as1 is empty AS, no prepending to do. */ | |
1405 | if (seg1 == NULL) | |
1406 | return as2; | |
1407 | ||
1408 | /* find the tail as1's segment chain. */ | |
1409 | while (seg1 && seg1->next) | |
1410 | seg1 = seg1->next; | |
1411 | ||
1412 | /* Delete any AS_CONFED_SEQUENCE segment from as2. */ | |
1413 | if (seg1->type == AS_SEQUENCE && seg2->type == AS_CONFED_SEQUENCE) | |
1414 | as2 = aspath_delete_confed_seq(as2); | |
1415 | ||
1416 | /* Compare last segment type of as1 and first segment type of as2. */ | |
1417 | if (seg1->type != seg2->type) | |
1418 | return aspath_merge(as1, as2); | |
1419 | ||
1420 | if (seg1->type == AS_SEQUENCE) { | |
1421 | /* We have two chains of segments, as1->segments and seg2, | |
1422 | * and we have to attach them together, merging the attaching | |
1423 | * segments together into one. | |
1424 | * | |
1425 | * 1. dupe as1->segments onto head of as2 | |
1426 | * 2. merge seg2's asns onto last segment of this new chain | |
1427 | * 3. attach chain after seg2 | |
1428 | */ | |
1429 | ||
1430 | /* dupe as1 onto as2's head */ | |
1431 | seg1 = as2->segments = assegment_dup_all(as1->segments); | |
1432 | ||
1433 | /* refind the tail of as2, reusing seg1 */ | |
1434 | while (seg1 && seg1->next) | |
1435 | seg1 = seg1->next; | |
1436 | ||
1437 | /* merge the old head, seg2, into tail, seg1 */ | |
1438 | seg1 = assegment_append_asns(seg1, seg2->as, seg2->length); | |
1439 | ||
1440 | /* bypass the merged seg2, and attach any chain after it to | |
1441 | * chain descending from as2's head | |
1442 | */ | |
1443 | seg1->next = seg2->next; | |
1444 | ||
1445 | /* seg2 is now referenceless and useless*/ | |
1446 | assegment_free(seg2); | |
1447 | ||
1448 | /* we've now prepended as1's segment chain to as2, merging | |
1449 | * the inbetween AS_SEQUENCE of seg2 in the process | |
1450 | */ | |
68e1a55b | 1451 | aspath_str_update(as2, false); |
d62a17ae | 1452 | return as2; |
1453 | } else { | |
1454 | /* AS_SET merge code is needed at here. */ | |
1455 | return aspath_merge(as1, as2); | |
1456 | } | |
1457 | /* XXX: Ermmm, what if as1 has multiple segments?? */ | |
1458 | ||
1459 | /* Not reached */ | |
718e3744 | 1460 | } |
1461 | ||
841f7a57 DO |
1462 | /* Iterate over AS_PATH segments and wipe all occurences of the |
1463 | * listed AS numbers. Hence some segments may lose some or even | |
1464 | * all data on the way, the operation is implemented as a smarter | |
1465 | * version of aspath_dup(), which allocates memory to hold the new | |
1466 | * data, not the original. The new AS path is returned. | |
1467 | */ | |
d62a17ae | 1468 | struct aspath *aspath_filter_exclude(struct aspath *source, |
1469 | struct aspath *exclude_list) | |
1470 | { | |
1471 | struct assegment *srcseg, *exclseg, *lastseg; | |
1472 | struct aspath *newpath; | |
1473 | ||
1474 | newpath = aspath_new(); | |
1475 | lastseg = NULL; | |
1476 | ||
1477 | for (srcseg = source->segments; srcseg; srcseg = srcseg->next) { | |
1478 | unsigned i, y, newlen = 0, done = 0, skip_as; | |
1479 | struct assegment *newseg; | |
1480 | ||
1481 | /* Find out, how much ASns are we going to pick from this | |
1482 | * segment. | |
1483 | * We can't perform filtering right inline, because the size of | |
1484 | * the new segment isn't known at the moment yet. | |
1485 | */ | |
1486 | for (i = 0; i < srcseg->length; i++) { | |
1487 | skip_as = 0; | |
1488 | for (exclseg = exclude_list->segments; | |
1489 | exclseg && !skip_as; exclseg = exclseg->next) | |
1490 | for (y = 0; y < exclseg->length; y++) | |
1491 | if (srcseg->as[i] == exclseg->as[y]) { | |
1492 | skip_as = 1; | |
1493 | // There's no sense in testing | |
1494 | // the rest of exclusion list, | |
1495 | // bail out. | |
1496 | break; | |
1497 | } | |
1498 | if (!skip_as) | |
1499 | newlen++; | |
1500 | } | |
1501 | /* newlen is now the number of ASns to copy */ | |
1502 | if (!newlen) | |
1503 | continue; | |
1504 | ||
1505 | /* Actual copying. Allocate memory and iterate once more, | |
1506 | * performing filtering. */ | |
1507 | newseg = assegment_new(srcseg->type, newlen); | |
1508 | for (i = 0; i < srcseg->length; i++) { | |
1509 | skip_as = 0; | |
1510 | for (exclseg = exclude_list->segments; | |
1511 | exclseg && !skip_as; exclseg = exclseg->next) | |
1512 | for (y = 0; y < exclseg->length; y++) | |
1513 | if (srcseg->as[i] == exclseg->as[y]) { | |
1514 | skip_as = 1; | |
1515 | break; | |
1516 | } | |
1517 | if (skip_as) | |
1518 | continue; | |
1519 | newseg->as[done++] = srcseg->as[i]; | |
1520 | } | |
1521 | /* At his point newlen must be equal to done, and both must be | |
1522 | * positive. Append | |
1523 | * the filtered segment to the gross result. */ | |
1524 | if (!lastseg) | |
1525 | newpath->segments = newseg; | |
1526 | else | |
1527 | lastseg->next = newseg; | |
1528 | lastseg = newseg; | |
1529 | } | |
68e1a55b | 1530 | aspath_str_update(newpath, false); |
d62a17ae | 1531 | /* We are happy returning even an empty AS_PATH, because the |
1532 | * administrator | |
1533 | * might expect this very behaviour. There's a mean to avoid this, if | |
1534 | * necessary, | |
1535 | * by having a match rule against certain AS_PATH regexps in the | |
1536 | * route-map index. | |
1537 | */ | |
1538 | aspath_free(source); | |
1539 | return newpath; | |
841f7a57 DO |
1540 | } |
1541 | ||
718e3744 | 1542 | /* Add specified AS to the leftmost of aspath. */ |
d62a17ae | 1543 | static struct aspath *aspath_add_asns(struct aspath *aspath, as_t asno, |
d7c0a89a | 1544 | uint8_t type, unsigned num) |
d62a17ae | 1545 | { |
1546 | struct assegment *assegment = aspath->segments; | |
1547 | unsigned i; | |
1548 | ||
1549 | if (assegment && assegment->type == type) { | |
1550 | /* extend existing segment */ | |
1551 | aspath->segments = | |
1552 | assegment_prepend_asns(aspath->segments, asno, num); | |
1553 | } else { | |
1554 | /* prepend with new segment */ | |
1555 | struct assegment *newsegment = assegment_new(type, num); | |
1556 | for (i = 0; i < num; i++) | |
1557 | newsegment->as[i] = asno; | |
1558 | ||
1559 | /* insert potentially replacing empty segment */ | |
1560 | if (assegment && assegment->length == 0) { | |
1561 | newsegment->next = assegment->next; | |
1562 | assegment_free(assegment); | |
1563 | } else | |
1564 | newsegment->next = assegment; | |
1565 | aspath->segments = newsegment; | |
bc3dd427 | 1566 | } |
718e3744 | 1567 | |
68e1a55b | 1568 | aspath_str_update(aspath, false); |
d62a17ae | 1569 | return aspath; |
718e3744 | 1570 | } |
1571 | ||
bc3dd427 | 1572 | /* Add specified AS to the leftmost of aspath num times. */ |
d62a17ae | 1573 | struct aspath *aspath_add_seq_n(struct aspath *aspath, as_t asno, unsigned num) |
bc3dd427 | 1574 | { |
d62a17ae | 1575 | return aspath_add_asns(aspath, asno, AS_SEQUENCE, num); |
bc3dd427 DW |
1576 | } |
1577 | ||
718e3744 | 1578 | /* Add specified AS to the leftmost of aspath. */ |
d62a17ae | 1579 | struct aspath *aspath_add_seq(struct aspath *aspath, as_t asno) |
718e3744 | 1580 | { |
d62a17ae | 1581 | return aspath_add_asns(aspath, asno, AS_SEQUENCE, 1); |
718e3744 | 1582 | } |
1583 | ||
1584 | /* Compare leftmost AS value for MED check. If as1's leftmost AS and | |
1585 | as2's leftmost AS is same return 1. */ | |
d62a17ae | 1586 | int aspath_cmp_left(const struct aspath *aspath1, const struct aspath *aspath2) |
718e3744 | 1587 | { |
d62a17ae | 1588 | const struct assegment *seg1; |
1589 | const struct assegment *seg2; | |
1590 | ||
1591 | if (!(aspath1 && aspath2)) | |
1592 | return 0; | |
718e3744 | 1593 | |
d62a17ae | 1594 | seg1 = aspath1->segments; |
1595 | seg2 = aspath2->segments; | |
718e3744 | 1596 | |
d62a17ae | 1597 | /* If both paths are originated in this AS then we do want to compare |
1598 | * MED */ | |
1599 | if (!seg1 && !seg2) | |
1600 | return 1; | |
718e3744 | 1601 | |
d62a17ae | 1602 | /* find first non-confed segments for each */ |
9d303b37 DL |
1603 | while (seg1 && ((seg1->type == AS_CONFED_SEQUENCE) |
1604 | || (seg1->type == AS_CONFED_SET))) | |
d62a17ae | 1605 | seg1 = seg1->next; |
2a3fa5d7 | 1606 | |
9d303b37 DL |
1607 | while (seg2 && ((seg2->type == AS_CONFED_SEQUENCE) |
1608 | || (seg2->type == AS_CONFED_SET))) | |
d62a17ae | 1609 | seg2 = seg2->next; |
718e3744 | 1610 | |
d62a17ae | 1611 | /* Check as1's */ |
1612 | if (!(seg1 && seg2 && (seg1->type == AS_SEQUENCE) | |
1613 | && (seg2->type == AS_SEQUENCE))) | |
1614 | return 0; | |
718e3744 | 1615 | |
d62a17ae | 1616 | if (seg1->as[0] == seg2->as[0]) |
1617 | return 1; | |
718e3744 | 1618 | |
d62a17ae | 1619 | return 0; |
718e3744 | 1620 | } |
1621 | ||
0b2aa3a0 PJ |
1622 | /* Truncate an aspath after a number of hops, and put the hops remaining |
1623 | * at the front of another aspath. Needed for AS4 compat. | |
1624 | * | |
1625 | * Returned aspath is a /new/ aspath, which should either by free'd or | |
1626 | * interned by the caller, as desired. | |
1627 | */ | |
d62a17ae | 1628 | struct aspath *aspath_reconcile_as4(struct aspath *aspath, |
1629 | struct aspath *as4path) | |
1630 | { | |
1631 | struct assegment *seg, *newseg, *prevseg = NULL; | |
1632 | struct aspath *newpath = NULL, *mergedpath; | |
1633 | int hops, cpasns = 0; | |
1634 | ||
1635 | if (!aspath) | |
1636 | return NULL; | |
1637 | ||
1638 | seg = aspath->segments; | |
1639 | ||
1640 | /* CONFEDs should get reconciled too.. */ | |
1641 | hops = (aspath_count_hops(aspath) + aspath_count_confeds(aspath)) | |
1642 | - aspath_count_hops(as4path); | |
1643 | ||
1644 | if (hops < 0) { | |
1645 | if (BGP_DEBUG(as4, AS4)) | |
1646 | zlog_warn( | |
1647 | "[AS4] Fewer hops in AS_PATH than NEW_AS_PATH"); | |
1648 | /* Something's gone wrong. The RFC says we should now ignore | |
1649 | * AS4_PATH, | |
1650 | * which is daft behaviour - it contains vital loop-detection | |
1651 | * information which must have been removed from AS_PATH. | |
1652 | */ | |
1653 | hops = aspath_count_hops(aspath); | |
1654 | } | |
1655 | ||
1656 | if (!hops) { | |
1657 | newpath = aspath_dup(as4path); | |
68e1a55b | 1658 | aspath_str_update(newpath, false); |
d62a17ae | 1659 | return newpath; |
0b2aa3a0 | 1660 | } |
d62a17ae | 1661 | |
1662 | if (BGP_DEBUG(as4, AS4)) | |
1663 | zlog_debug( | |
1664 | "[AS4] got AS_PATH %s and AS4_PATH %s synthesizing now", | |
1665 | aspath->str, as4path->str); | |
1666 | ||
1667 | while (seg && hops > 0) { | |
1668 | switch (seg->type) { | |
1669 | case AS_SET: | |
1670 | case AS_CONFED_SET: | |
1671 | hops--; | |
1672 | cpasns = seg->length; | |
1673 | break; | |
1674 | case AS_CONFED_SEQUENCE: | |
1675 | /* Should never split a confed-sequence, if hop-count | |
1676 | * suggests we must then something's gone wrong | |
1677 | * somewhere. | |
1678 | * | |
1679 | * Most important goal is to preserve AS_PATHs prime | |
1680 | * function | |
1681 | * as loop-detector, so we fudge the numbers so that the | |
1682 | * entire | |
1683 | * confed-sequence is merged in. | |
1684 | */ | |
1685 | if (hops < seg->length) { | |
1686 | if (BGP_DEBUG(as4, AS4)) | |
1687 | zlog_debug( | |
1688 | "[AS4] AS4PATHmangle: AS_CONFED_SEQUENCE falls" | |
1689 | " across 2/4 ASN boundary somewhere, broken.."); | |
1690 | hops = seg->length; | |
1691 | } | |
1692 | /* fallthru */ | |
1693 | case AS_SEQUENCE: | |
1694 | cpasns = MIN(seg->length, hops); | |
1695 | hops -= seg->length; | |
1696 | } | |
1697 | ||
1698 | assert(cpasns <= seg->length); | |
1699 | ||
1700 | newseg = assegment_new(seg->type, 0); | |
1701 | newseg = assegment_append_asns(newseg, seg->as, cpasns); | |
1702 | ||
1703 | if (!newpath) { | |
1704 | newpath = aspath_new(); | |
1705 | newpath->segments = newseg; | |
1706 | } else | |
1707 | prevseg->next = newseg; | |
1708 | ||
1709 | prevseg = newseg; | |
1710 | seg = seg->next; | |
1711 | } | |
1712 | ||
1713 | /* We may be able to join some segments here, and we must | |
1714 | * do this because... we want normalised aspaths in out hash | |
1715 | * and we do not want to stumble in aspath_put. | |
1716 | */ | |
1717 | mergedpath = aspath_merge(newpath, aspath_dup(as4path)); | |
1718 | aspath_free(newpath); | |
1719 | mergedpath->segments = assegment_normalise(mergedpath->segments); | |
68e1a55b | 1720 | aspath_str_update(mergedpath, false); |
d62a17ae | 1721 | |
1722 | if (BGP_DEBUG(as4, AS4)) | |
1723 | zlog_debug("[AS4] result of synthesizing is %s", | |
1724 | mergedpath->str); | |
1725 | ||
1726 | return mergedpath; | |
0b2aa3a0 PJ |
1727 | } |
1728 | ||
718e3744 | 1729 | /* Compare leftmost AS value for MED check. If as1's leftmost AS and |
1730 | as2's leftmost AS is same return 1. (confederation as-path | |
1731 | only). */ | |
d62a17ae | 1732 | int aspath_cmp_left_confed(const struct aspath *aspath1, |
1733 | const struct aspath *aspath2) | |
718e3744 | 1734 | { |
d62a17ae | 1735 | if (!(aspath1 && aspath2)) |
1736 | return 0; | |
718e3744 | 1737 | |
d62a17ae | 1738 | if (!(aspath1->segments && aspath2->segments)) |
1739 | return 0; | |
1740 | ||
1741 | if ((aspath1->segments->type != AS_CONFED_SEQUENCE) | |
1742 | || (aspath2->segments->type != AS_CONFED_SEQUENCE)) | |
1743 | return 0; | |
1744 | ||
1745 | if (aspath1->segments->as[0] == aspath2->segments->as[0]) | |
1746 | return 1; | |
1747 | ||
1748 | return 0; | |
718e3744 | 1749 | } |
1750 | ||
66b199b2 DS |
1751 | /* Delete all AS_CONFED_SEQUENCE/SET segments from aspath. |
1752 | * RFC 5065 section 4.1.c.1 | |
1753 | * | |
1754 | * 1) if any path segments of the AS_PATH are of the type | |
1755 | * AS_CONFED_SEQUENCE or AS_CONFED_SET, those segments MUST be | |
1756 | * removed from the AS_PATH attribute, leaving the sanitized | |
1757 | * AS_PATH attribute to be operated on by steps 2, 3 or 4. | |
1758 | */ | |
d62a17ae | 1759 | struct aspath *aspath_delete_confed_seq(struct aspath *aspath) |
718e3744 | 1760 | { |
d62a17ae | 1761 | struct assegment *seg, *prev, *next; |
1762 | char removed_confed_segment; | |
1763 | ||
1764 | if (!(aspath && aspath->segments)) | |
1765 | return aspath; | |
718e3744 | 1766 | |
d62a17ae | 1767 | seg = aspath->segments; |
1768 | removed_confed_segment = 0; | |
1769 | next = NULL; | |
1770 | prev = NULL; | |
718e3744 | 1771 | |
d62a17ae | 1772 | while (seg) { |
1773 | next = seg->next; | |
66b199b2 | 1774 | |
d62a17ae | 1775 | if (seg->type == AS_CONFED_SEQUENCE |
1776 | || seg->type == AS_CONFED_SET) { | |
1777 | /* This is the first segment in the aspath */ | |
1778 | if (aspath->segments == seg) | |
1779 | aspath->segments = seg->next; | |
1780 | else | |
1781 | prev->next = seg->next; | |
66b199b2 | 1782 | |
d62a17ae | 1783 | assegment_free(seg); |
1784 | removed_confed_segment = 1; | |
1785 | } else | |
1786 | prev = seg; | |
66b199b2 | 1787 | |
d62a17ae | 1788 | seg = next; |
1789 | } | |
66b199b2 | 1790 | |
d62a17ae | 1791 | if (removed_confed_segment) |
68e1a55b | 1792 | aspath_str_update(aspath, false); |
66b199b2 | 1793 | |
d62a17ae | 1794 | return aspath; |
718e3744 | 1795 | } |
1796 | ||
1797 | /* Add new AS number to the leftmost part of the aspath as | |
1798 | AS_CONFED_SEQUENCE. */ | |
d62a17ae | 1799 | struct aspath *aspath_add_confed_seq(struct aspath *aspath, as_t asno) |
718e3744 | 1800 | { |
d62a17ae | 1801 | return aspath_add_asns(aspath, asno, AS_CONFED_SEQUENCE, 1); |
718e3744 | 1802 | } |
1803 | ||
1804 | /* Add new as value to as path structure. */ | |
d62a17ae | 1805 | static void aspath_as_add(struct aspath *as, as_t asno) |
718e3744 | 1806 | { |
d62a17ae | 1807 | struct assegment *seg = as->segments; |
1808 | ||
1809 | if (!seg) | |
1810 | return; | |
718e3744 | 1811 | |
d62a17ae | 1812 | /* Last segment search procedure. */ |
1813 | while (seg->next) | |
1814 | seg = seg->next; | |
93c1749c | 1815 | |
d62a17ae | 1816 | assegment_append_asns(seg, &asno, 1); |
718e3744 | 1817 | } |
1818 | ||
1819 | /* Add new as segment to the as path. */ | |
d62a17ae | 1820 | static void aspath_segment_add(struct aspath *as, int type) |
718e3744 | 1821 | { |
d62a17ae | 1822 | struct assegment *seg = as->segments; |
1823 | struct assegment *new = assegment_new(type, 0); | |
718e3744 | 1824 | |
d62a17ae | 1825 | if (seg) { |
1826 | while (seg->next) | |
1827 | seg = seg->next; | |
1828 | seg->next = new; | |
1829 | } else | |
1830 | as->segments = new; | |
718e3744 | 1831 | } |
1832 | ||
d62a17ae | 1833 | struct aspath *aspath_empty(void) |
718e3744 | 1834 | { |
d62a17ae | 1835 | return aspath_parse(NULL, 0, 1); /* 32Bit ;-) */ |
718e3744 | 1836 | } |
1837 | ||
d62a17ae | 1838 | struct aspath *aspath_empty_get(void) |
718e3744 | 1839 | { |
d62a17ae | 1840 | struct aspath *aspath; |
718e3744 | 1841 | |
d62a17ae | 1842 | aspath = aspath_new(); |
68e1a55b | 1843 | aspath_make_str_count(aspath, false); |
d62a17ae | 1844 | return aspath; |
718e3744 | 1845 | } |
1846 | ||
d62a17ae | 1847 | unsigned long aspath_count(void) |
718e3744 | 1848 | { |
d62a17ae | 1849 | return ashash->count; |
1850 | } | |
6b0655a2 | 1851 | |
d62a17ae | 1852 | /* |
718e3744 | 1853 | Theoretically, one as path can have: |
1854 | ||
1855 | One BGP packet size should be less than 4096. | |
1856 | One BGP attribute size should be less than 4096 - BGP header size. | |
1857 | One BGP aspath size should be less than 4096 - BGP header size - | |
1858 | BGP mandantry attribute size. | |
1859 | */ | |
1860 | ||
1861 | /* AS path string lexical token enum. */ | |
d62a17ae | 1862 | enum as_token { |
1863 | as_token_asval, | |
1864 | as_token_set_start, | |
1865 | as_token_set_end, | |
1866 | as_token_confed_seq_start, | |
1867 | as_token_confed_seq_end, | |
1868 | as_token_confed_set_start, | |
1869 | as_token_confed_set_end, | |
1870 | as_token_unknown | |
718e3744 | 1871 | }; |
1872 | ||
1873 | /* Return next token and point for string parse. */ | |
d62a17ae | 1874 | static const char *aspath_gettoken(const char *buf, enum as_token *token, |
d7c0a89a | 1875 | unsigned long *asno) |
d62a17ae | 1876 | { |
1877 | const char *p = buf; | |
1878 | ||
1879 | /* Skip seperators (space for sequences, ',' for sets). */ | |
1880 | while (isspace((int)*p) || *p == ',') | |
1881 | p++; | |
1882 | ||
1883 | /* Check the end of the string and type specify characters | |
1884 | (e.g. {}()). */ | |
1885 | switch (*p) { | |
1886 | case '\0': | |
1887 | return NULL; | |
1888 | case '{': | |
1889 | *token = as_token_set_start; | |
1890 | p++; | |
1891 | return p; | |
1892 | case '}': | |
1893 | *token = as_token_set_end; | |
1894 | p++; | |
1895 | return p; | |
1896 | case '(': | |
1897 | *token = as_token_confed_seq_start; | |
1898 | p++; | |
1899 | return p; | |
1900 | case ')': | |
1901 | *token = as_token_confed_seq_end; | |
1902 | p++; | |
1903 | return p; | |
1904 | case '[': | |
1905 | *token = as_token_confed_set_start; | |
1906 | p++; | |
1907 | return p; | |
1908 | case ']': | |
1909 | *token = as_token_confed_set_end; | |
1910 | p++; | |
1911 | return p; | |
1912 | } | |
1913 | ||
1914 | /* Check actual AS value. */ | |
1915 | if (isdigit((int)*p)) { | |
1916 | as_t asval; | |
1917 | ||
1918 | *token = as_token_asval; | |
1919 | asval = (*p - '0'); | |
1920 | p++; | |
1921 | ||
1922 | while (isdigit((int)*p)) { | |
1923 | asval *= 10; | |
1924 | asval += (*p - '0'); | |
1925 | p++; | |
1926 | } | |
1927 | *asno = asval; | |
1928 | return p; | |
718e3744 | 1929 | } |
718e3744 | 1930 | |
d62a17ae | 1931 | /* There is no match then return unknown token. */ |
1932 | *token = as_token_unknown; | |
b42d80dd VJ |
1933 | p++; |
1934 | return p; | |
d62a17ae | 1935 | } |
1936 | ||
1937 | struct aspath *aspath_str2aspath(const char *str) | |
1938 | { | |
1939 | enum as_token token = as_token_unknown; | |
d7c0a89a QY |
1940 | unsigned short as_type; |
1941 | unsigned long asno = 0; | |
d62a17ae | 1942 | struct aspath *aspath; |
1943 | int needtype; | |
1944 | ||
1945 | aspath = aspath_new(); | |
1946 | ||
1947 | /* We start default type as AS_SEQUENCE. */ | |
1948 | as_type = AS_SEQUENCE; | |
1949 | needtype = 1; | |
1950 | ||
1951 | while ((str = aspath_gettoken(str, &token, &asno)) != NULL) { | |
1952 | switch (token) { | |
1953 | case as_token_asval: | |
1954 | if (needtype) { | |
1955 | aspath_segment_add(aspath, as_type); | |
1956 | needtype = 0; | |
1957 | } | |
1958 | aspath_as_add(aspath, asno); | |
1959 | break; | |
1960 | case as_token_set_start: | |
1961 | as_type = AS_SET; | |
1962 | aspath_segment_add(aspath, as_type); | |
1963 | needtype = 0; | |
1964 | break; | |
1965 | case as_token_set_end: | |
1966 | as_type = AS_SEQUENCE; | |
1967 | needtype = 1; | |
1968 | break; | |
1969 | case as_token_confed_seq_start: | |
1970 | as_type = AS_CONFED_SEQUENCE; | |
1971 | aspath_segment_add(aspath, as_type); | |
1972 | needtype = 0; | |
1973 | break; | |
1974 | case as_token_confed_seq_end: | |
1975 | as_type = AS_SEQUENCE; | |
1976 | needtype = 1; | |
1977 | break; | |
1978 | case as_token_confed_set_start: | |
1979 | as_type = AS_CONFED_SET; | |
1980 | aspath_segment_add(aspath, as_type); | |
1981 | needtype = 0; | |
1982 | break; | |
1983 | case as_token_confed_set_end: | |
1984 | as_type = AS_SEQUENCE; | |
1985 | needtype = 1; | |
1986 | break; | |
1987 | case as_token_unknown: | |
1988 | default: | |
1989 | aspath_free(aspath); | |
1990 | return NULL; | |
1991 | } | |
1992 | } | |
1993 | ||
68e1a55b | 1994 | aspath_make_str_count(aspath, false); |
718e3744 | 1995 | |
d62a17ae | 1996 | return aspath; |
718e3744 | 1997 | } |
6b0655a2 | 1998 | |
718e3744 | 1999 | /* Make hash value by raw aspath data. */ |
d62a17ae | 2000 | unsigned int aspath_key_make(void *p) |
718e3744 | 2001 | { |
d62a17ae | 2002 | struct aspath *aspath = (struct aspath *)p; |
2003 | unsigned int key = 0; | |
718e3744 | 2004 | |
d62a17ae | 2005 | if (!aspath->str) |
68e1a55b | 2006 | aspath_str_update(aspath, false); |
f669f7d2 | 2007 | |
d62a17ae | 2008 | key = jhash(aspath->str, aspath->str_len, 2334325); |
718e3744 | 2009 | |
d62a17ae | 2010 | return key; |
718e3744 | 2011 | } |
2012 | ||
2013 | /* If two aspath have same value then return 1 else return 0 */ | |
d62a17ae | 2014 | int aspath_cmp(const void *arg1, const void *arg2) |
2015 | { | |
2016 | const struct assegment *seg1 = ((const struct aspath *)arg1)->segments; | |
2017 | const struct assegment *seg2 = ((const struct aspath *)arg2)->segments; | |
2018 | ||
2019 | while (seg1 || seg2) { | |
2020 | int i; | |
2021 | if ((!seg1 && seg2) || (seg1 && !seg2)) | |
2022 | return 0; | |
2023 | if (seg1->type != seg2->type) | |
2024 | return 0; | |
2025 | if (seg1->length != seg2->length) | |
2026 | return 0; | |
2027 | for (i = 0; i < seg1->length; i++) | |
2028 | if (seg1->as[i] != seg2->as[i]) | |
2029 | return 0; | |
2030 | seg1 = seg1->next; | |
2031 | seg2 = seg2->next; | |
2032 | } | |
2033 | return 1; | |
718e3744 | 2034 | } |
2035 | ||
2036 | /* AS path hash initialize. */ | |
d62a17ae | 2037 | void aspath_init(void) |
718e3744 | 2038 | { |
996c9314 | 2039 | ashash = hash_create_size(32768, aspath_key_make, aspath_cmp, |
3f65c5b1 | 2040 | "BGP AS Path"); |
718e3744 | 2041 | } |
8fdc32ab | 2042 | |
d62a17ae | 2043 | void aspath_finish(void) |
8fdc32ab | 2044 | { |
d62a17ae | 2045 | hash_clean(ashash, (void (*)(void *))aspath_free); |
2046 | hash_free(ashash); | |
2047 | ashash = NULL; | |
2048 | ||
2049 | if (snmp_stream) | |
2050 | stream_free(snmp_stream); | |
8fdc32ab | 2051 | } |
6b0655a2 | 2052 | |
718e3744 | 2053 | /* return and as path value */ |
d62a17ae | 2054 | const char *aspath_print(struct aspath *as) |
718e3744 | 2055 | { |
d62a17ae | 2056 | return (as ? as->str : NULL); |
718e3744 | 2057 | } |
2058 | ||
2059 | /* Printing functions */ | |
841f7a57 DO |
2060 | /* Feed the AS_PATH to the vty; the suffix string follows it only in case |
2061 | * AS_PATH wasn't empty. | |
2062 | */ | |
d62a17ae | 2063 | void aspath_print_vty(struct vty *vty, const char *format, struct aspath *as, |
2064 | const char *suffix) | |
718e3744 | 2065 | { |
d62a17ae | 2066 | assert(format); |
2067 | vty_out(vty, format, as->str); | |
2068 | if (as->str_len && strlen(suffix)) | |
2069 | vty_out(vty, "%s", suffix); | |
718e3744 | 2070 | } |
2071 | ||
d62a17ae | 2072 | static void aspath_show_all_iterator(struct hash_backet *backet, |
2073 | struct vty *vty) | |
718e3744 | 2074 | { |
d62a17ae | 2075 | struct aspath *as; |
718e3744 | 2076 | |
d62a17ae | 2077 | as = (struct aspath *)backet->data; |
718e3744 | 2078 | |
d62a17ae | 2079 | vty_out(vty, "[%p:%u] (%ld) ", (void *)backet, backet->key, as->refcnt); |
2080 | vty_out(vty, "%s\n", as->str); | |
718e3744 | 2081 | } |
2082 | ||
2083 | /* Print all aspath and hash information. This function is used from | |
716b2d8a | 2084 | `show [ip] bgp paths' command. */ |
d62a17ae | 2085 | void aspath_print_all_vty(struct vty *vty) |
718e3744 | 2086 | { |
9d303b37 DL |
2087 | hash_iterate(ashash, (void (*)(struct hash_backet *, |
2088 | void *))aspath_show_all_iterator, | |
d62a17ae | 2089 | vty); |
718e3744 | 2090 | } |