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