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