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