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 | { |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 325 | return XCALLOC (MTYPE_AS_PATH, sizeof (struct aspath)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /* Free AS path structure. */ |
| 329 | void |
| 330 | aspath_free (struct aspath *aspath) |
| 331 | { |
| 332 | if (!aspath) |
| 333 | return; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 334 | if (aspath->segments) |
| 335 | assegment_free_all (aspath->segments); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 336 | if (aspath->str) |
| 337 | XFREE (MTYPE_AS_STR, aspath->str); |
| 338 | XFREE (MTYPE_AS_PATH, aspath); |
| 339 | } |
| 340 | |
| 341 | /* Unintern aspath from AS path bucket. */ |
| 342 | void |
| 343 | aspath_unintern (struct aspath *aspath) |
| 344 | { |
| 345 | struct aspath *ret; |
| 346 | |
| 347 | if (aspath->refcnt) |
| 348 | aspath->refcnt--; |
| 349 | |
| 350 | if (aspath->refcnt == 0) |
| 351 | { |
| 352 | /* This aspath must exist in aspath hash table. */ |
| 353 | ret = hash_release (ashash, aspath); |
| 354 | assert (ret != NULL); |
| 355 | aspath_free (aspath); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /* Return the start or end delimiters for a particular Segment type */ |
| 360 | #define AS_SEG_START 0 |
| 361 | #define AS_SEG_END 1 |
| 362 | static char |
| 363 | aspath_delimiter_char (u_char type, u_char which) |
| 364 | { |
| 365 | int i; |
| 366 | struct |
| 367 | { |
| 368 | int type; |
| 369 | char start; |
| 370 | char end; |
| 371 | } aspath_delim_char [] = |
| 372 | { |
| 373 | { AS_SET, '{', '}' }, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 374 | { AS_CONFED_SET, '[', ']' }, |
| 375 | { AS_CONFED_SEQUENCE, '(', ')' }, |
| 376 | { 0 } |
| 377 | }; |
| 378 | |
| 379 | for (i = 0; aspath_delim_char[i].type != 0; i++) |
| 380 | { |
| 381 | if (aspath_delim_char[i].type == type) |
| 382 | { |
| 383 | if (which == AS_SEG_START) |
| 384 | return aspath_delim_char[i].start; |
| 385 | else if (which == AS_SEG_END) |
| 386 | return aspath_delim_char[i].end; |
| 387 | } |
| 388 | } |
| 389 | return ' '; |
| 390 | } |
| 391 | |
Denis Ovsienko | 014b670 | 2009-06-23 21:10:45 +0400 | [diff] [blame] | 392 | /* countup asns from this segment and index onward */ |
| 393 | static int |
| 394 | assegment_count_asns (struct assegment *seg, int from) |
| 395 | { |
| 396 | int count = 0; |
| 397 | while (seg) |
| 398 | { |
| 399 | if (!from) |
| 400 | count += seg->length; |
| 401 | else |
| 402 | { |
| 403 | count += (seg->length - from); |
| 404 | from = 0; |
| 405 | } |
| 406 | seg = seg->next; |
| 407 | } |
| 408 | return count; |
| 409 | } |
| 410 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 411 | unsigned int |
| 412 | aspath_count_confeds (struct aspath *aspath) |
| 413 | { |
| 414 | int count = 0; |
| 415 | struct assegment *seg = aspath->segments; |
| 416 | |
| 417 | while (seg) |
| 418 | { |
| 419 | if (seg->type == AS_CONFED_SEQUENCE) |
| 420 | count += seg->length; |
| 421 | else if (seg->type == AS_CONFED_SET) |
| 422 | count++; |
| 423 | |
| 424 | seg = seg->next; |
| 425 | } |
| 426 | return count; |
| 427 | } |
| 428 | |
| 429 | unsigned int |
| 430 | aspath_count_hops (struct aspath *aspath) |
| 431 | { |
| 432 | int count = 0; |
| 433 | struct assegment *seg = aspath->segments; |
| 434 | |
| 435 | while (seg) |
| 436 | { |
| 437 | if (seg->type == AS_SEQUENCE) |
| 438 | count += seg->length; |
| 439 | else if (seg->type == AS_SET) |
| 440 | count++; |
| 441 | |
| 442 | seg = seg->next; |
| 443 | } |
| 444 | return count; |
| 445 | } |
| 446 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 447 | /* Estimate size aspath /might/ take if encoded into an |
| 448 | * ASPATH attribute. |
| 449 | * |
| 450 | * This is a quick estimate, not definitive! aspath_put() |
| 451 | * may return a different number!! |
| 452 | */ |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 453 | unsigned int |
| 454 | aspath_size (struct aspath *aspath) |
| 455 | { |
| 456 | int size = 0; |
| 457 | struct assegment *seg = aspath->segments; |
| 458 | |
| 459 | while (seg) |
| 460 | { |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 461 | size += ASSEGMENT_SIZE(seg->length, 1); |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 462 | seg = seg->next; |
| 463 | } |
| 464 | return size; |
| 465 | } |
| 466 | |
Paul Jakma | 2815e61 | 2006-09-14 02:56:07 +0000 | [diff] [blame] | 467 | /* Return highest public ASN in path */ |
| 468 | as_t |
| 469 | aspath_highest (struct aspath *aspath) |
| 470 | { |
| 471 | struct assegment *seg = aspath->segments; |
| 472 | as_t highest = 0; |
| 473 | unsigned int i; |
| 474 | |
| 475 | while (seg) |
| 476 | { |
| 477 | for (i = 0; i < seg->length; i++) |
| 478 | if (seg->as[i] > highest |
| 479 | && (seg->as[i] < BGP_PRIVATE_AS_MIN |
| 480 | || seg->as[i] > BGP_PRIVATE_AS_MAX)) |
| 481 | highest = seg->as[i]; |
| 482 | seg = seg->next; |
| 483 | } |
| 484 | return highest; |
| 485 | } |
| 486 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 487 | /* Return 1 if there are any 4-byte ASes in the path */ |
| 488 | unsigned int |
| 489 | aspath_has_as4 (struct aspath *aspath) |
| 490 | { |
| 491 | struct assegment *seg = aspath->segments; |
| 492 | unsigned int i; |
| 493 | |
| 494 | while (seg) |
| 495 | { |
| 496 | for (i = 0; i < seg->length; i++) |
| 497 | if (seg->as[i] > BGP_AS_MAX) |
| 498 | return 1; |
| 499 | seg = seg->next; |
| 500 | } |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | /* Return number of as numbers in in path */ |
| 505 | unsigned int |
| 506 | aspath_count_numas (struct aspath *aspath) |
| 507 | { |
| 508 | struct assegment *seg = aspath->segments; |
| 509 | unsigned int num; |
| 510 | |
| 511 | num=0; |
| 512 | while (seg) |
| 513 | { |
| 514 | num += seg->length; |
| 515 | seg = seg->next; |
| 516 | } |
| 517 | return num; |
| 518 | } |
| 519 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 520 | /* Convert aspath structure to string expression. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 521 | static char * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 522 | aspath_make_str_count (struct aspath *as) |
| 523 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 524 | struct assegment *seg; |
| 525 | int str_size; |
| 526 | int len = 0; |
hasso | c9e52be | 2004-09-26 16:09:34 +0000 | [diff] [blame] | 527 | char *str_buf; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 528 | |
| 529 | /* Empty aspath. */ |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 530 | if (!as->segments) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 531 | { |
| 532 | str_buf = XMALLOC (MTYPE_AS_STR, 1); |
| 533 | str_buf[0] = '\0'; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 534 | return str_buf; |
| 535 | } |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 536 | |
| 537 | seg = as->segments; |
| 538 | |
Denis Ovsienko | 014b670 | 2009-06-23 21:10:45 +0400 | [diff] [blame] | 539 | /* ASN takes 5 to 10 chars plus seperator, see below. |
| 540 | * If there is one differing segment type, we need an additional |
| 541 | * 2 chars for segment delimiters, and the final '\0'. |
| 542 | * Hopefully this is large enough to avoid hitting the realloc |
| 543 | * code below for most common sequences. |
| 544 | * |
| 545 | * This was changed to 10 after the well-known BGP assertion, which |
| 546 | * had hit some parts of the Internet in May of 2009. |
| 547 | */ |
| 548 | #define ASN_STR_LEN (10 + 1) |
| 549 | str_size = MAX (assegment_count_asns (seg, 0) * ASN_STR_LEN + 2 + 1, |
| 550 | ASPATH_STR_DEFAULT_LEN); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 551 | str_buf = XMALLOC (MTYPE_AS_STR, str_size); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 552 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 553 | while (seg) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 554 | { |
| 555 | int i; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 556 | char seperator; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 557 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 558 | /* Check AS type validity. Set seperator for segment */ |
| 559 | switch (seg->type) |
| 560 | { |
| 561 | case AS_SET: |
| 562 | case AS_CONFED_SET: |
| 563 | seperator = ','; |
| 564 | break; |
| 565 | case AS_SEQUENCE: |
| 566 | case AS_CONFED_SEQUENCE: |
| 567 | seperator = ' '; |
| 568 | break; |
| 569 | default: |
| 570 | XFREE (MTYPE_AS_STR, str_buf); |
| 571 | return NULL; |
| 572 | } |
| 573 | |
Denis Ovsienko | 014b670 | 2009-06-23 21:10:45 +0400 | [diff] [blame] | 574 | /* We might need to increase str_buf, particularly if path has |
| 575 | * differing segments types, our initial guesstimate above will |
| 576 | * have been wrong. Need 10 chars for ASN, a seperator each and |
| 577 | * potentially two segment delimiters, plus a space between each |
| 578 | * segment and trailing zero. |
| 579 | * |
| 580 | * This definitely didn't work with the value of 5 bytes and |
| 581 | * 32-bit ASNs. |
| 582 | */ |
| 583 | #define SEGMENT_STR_LEN(X) (((X)->length * ASN_STR_LEN) + 2 + 1 + 1) |
| 584 | if ( (len + SEGMENT_STR_LEN(seg)) > str_size) |
| 585 | { |
| 586 | str_size = len + SEGMENT_STR_LEN(seg); |
| 587 | str_buf = XREALLOC (MTYPE_AS_STR, str_buf, str_size); |
| 588 | } |
| 589 | #undef ASN_STR_LEN |
| 590 | #undef SEGMENT_STR_LEN |
| 591 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 592 | if (seg->type != AS_SEQUENCE) |
Denis Ovsienko | 014b670 | 2009-06-23 21:10:45 +0400 | [diff] [blame] | 593 | len += snprintf (str_buf + len, str_size - len, |
| 594 | "%c", |
| 595 | aspath_delimiter_char (seg->type, AS_SEG_START)); |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 596 | |
| 597 | /* write out the ASNs, with their seperators, bar the last one*/ |
| 598 | for (i = 0; i < seg->length; i++) |
| 599 | { |
| 600 | len += snprintf (str_buf + len, str_size - len, "%u", seg->as[i]); |
| 601 | |
| 602 | if (i < (seg->length - 1)) |
| 603 | len += snprintf (str_buf + len, str_size - len, "%c", seperator); |
| 604 | } |
| 605 | |
| 606 | if (seg->type != AS_SEQUENCE) |
| 607 | len += snprintf (str_buf + len, str_size - len, "%c", |
| 608 | aspath_delimiter_char (seg->type, AS_SEG_END)); |
| 609 | if (seg->next) |
| 610 | len += snprintf (str_buf + len, str_size - len, " "); |
| 611 | |
| 612 | seg = seg->next; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 613 | } |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 614 | |
| 615 | assert (len < str_size); |
| 616 | |
| 617 | str_buf[len] = '\0'; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 618 | |
| 619 | return str_buf; |
| 620 | } |
| 621 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 622 | static void |
| 623 | aspath_str_update (struct aspath *as) |
| 624 | { |
| 625 | if (as->str) |
| 626 | XFREE (MTYPE_AS_STR, as->str); |
| 627 | as->str = aspath_make_str_count (as); |
| 628 | } |
| 629 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 630 | /* Intern allocated AS path. */ |
| 631 | struct aspath * |
| 632 | aspath_intern (struct aspath *aspath) |
| 633 | { |
| 634 | struct aspath *find; |
| 635 | |
| 636 | /* Assert this AS path structure is not interned. */ |
| 637 | assert (aspath->refcnt == 0); |
| 638 | |
| 639 | /* Check AS path hash. */ |
| 640 | find = hash_get (ashash, aspath, hash_alloc_intern); |
| 641 | |
| 642 | if (find != aspath) |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 643 | aspath_free (aspath); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 644 | |
| 645 | find->refcnt++; |
| 646 | |
| 647 | if (! find->str) |
| 648 | find->str = aspath_make_str_count (find); |
| 649 | |
| 650 | return find; |
| 651 | } |
| 652 | |
| 653 | /* Duplicate aspath structure. Created same aspath structure but |
| 654 | reference count and AS path string is cleared. */ |
| 655 | struct aspath * |
| 656 | aspath_dup (struct aspath *aspath) |
| 657 | { |
| 658 | struct aspath *new; |
| 659 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 660 | new = XCALLOC (MTYPE_AS_PATH, sizeof (struct aspath)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 661 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 662 | if (aspath->segments) |
| 663 | new->segments = assegment_dup_all (aspath->segments); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 664 | else |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 665 | new->segments = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 666 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 667 | new->str = aspath_make_str_count (aspath); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 668 | |
| 669 | return new; |
| 670 | } |
| 671 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 672 | static void * |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 673 | aspath_hash_alloc (void *arg) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 674 | { |
| 675 | struct aspath *aspath; |
| 676 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 677 | /* New aspath structure is needed. */ |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 678 | aspath = aspath_dup (arg); |
| 679 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 680 | /* Malformed AS path value. */ |
| 681 | if (! aspath->str) |
| 682 | { |
| 683 | aspath_free (aspath); |
| 684 | return NULL; |
| 685 | } |
| 686 | |
| 687 | return aspath; |
| 688 | } |
| 689 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 690 | /* parse as-segment byte stream in struct assegment */ |
paul | ad72740 | 2005-11-23 02:47:02 +0000 | [diff] [blame] | 691 | static struct assegment * |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 692 | assegments_parse (struct stream *s, size_t length, int use32bit) |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 693 | { |
| 694 | struct assegment_header segh; |
| 695 | struct assegment *seg, *prev = NULL, *head = NULL; |
| 696 | size_t bytes = 0; |
| 697 | |
| 698 | /* empty aspath (ie iBGP or somesuch) */ |
| 699 | if (length == 0) |
| 700 | return NULL; |
| 701 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 702 | if (BGP_DEBUG (as4, AS4_SEGMENT)) |
| 703 | zlog_debug ("[AS4SEG] Parse aspath segment: got total byte length %lu", |
| 704 | (unsigned long) length); |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 705 | /* basic checks */ |
| 706 | if ( (STREAM_READABLE(s) < length) |
| 707 | || (STREAM_READABLE(s) < AS_HEADER_SIZE) |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 708 | || (length % AS16_VALUE_SIZE )) |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 709 | return NULL; |
| 710 | |
| 711 | while ( (STREAM_READABLE(s) > AS_HEADER_SIZE) |
| 712 | && (bytes < length)) |
| 713 | { |
| 714 | int i; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 715 | int seg_size; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 716 | |
| 717 | /* softly softly, get the header first on its own */ |
| 718 | segh.type = stream_getc (s); |
| 719 | segh.length = stream_getc (s); |
| 720 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 721 | seg_size = ASSEGMENT_SIZE(segh.length, use32bit); |
| 722 | |
| 723 | if (BGP_DEBUG (as4, AS4_SEGMENT)) |
| 724 | zlog_debug ("[AS4SEG] Parse aspath segment: got type %d, length %d", |
| 725 | segh.type, segh.length); |
| 726 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 727 | /* check it.. */ |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 728 | if ( ((bytes + seg_size) > length) |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 729 | /* 1771bis 4.3b: seg length contains one or more */ |
| 730 | || (segh.length == 0) |
| 731 | /* Paranoia in case someone changes type of segment length */ |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 732 | || ((sizeof segh.length > 1) && (segh.length > AS_SEGMENT_MAX)) ) |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 733 | { |
| 734 | if (head) |
| 735 | assegment_free_all (head); |
| 736 | return NULL; |
| 737 | } |
| 738 | |
| 739 | /* now its safe to trust lengths */ |
| 740 | seg = assegment_new (segh.type, segh.length); |
| 741 | |
| 742 | if (head) |
| 743 | prev->next = seg; |
| 744 | else /* it's the first segment */ |
| 745 | head = prev = seg; |
| 746 | |
| 747 | for (i = 0; i < segh.length; i++) |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 748 | seg->as[i] = (use32bit) ? stream_getl (s) : stream_getw (s); |
| 749 | |
| 750 | bytes += seg_size; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 751 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 752 | if (BGP_DEBUG (as4, AS4_SEGMENT)) |
| 753 | zlog_debug ("[AS4SEG] Parse aspath segment: Bytes now: %lu", |
| 754 | (unsigned long) bytes); |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 755 | |
| 756 | prev = seg; |
| 757 | } |
| 758 | |
| 759 | return assegment_normalise (head); |
| 760 | } |
| 761 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 762 | /* AS path parse function. pnt is a pointer to byte stream and length |
| 763 | is length of byte stream. If there is same AS path in the the AS |
| 764 | path hash then return it else make new AS path structure. */ |
| 765 | struct aspath * |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 766 | aspath_parse (struct stream *s, size_t length, int use32bit) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 767 | { |
| 768 | struct aspath as; |
| 769 | struct aspath *find; |
| 770 | |
| 771 | /* If length is odd it's malformed AS path. */ |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 772 | /* Nit-picking: if (use32bit == 0) it is malformed if odd, |
| 773 | * otherwise its malformed when length is larger than 2 and (length-2) |
| 774 | * is not dividable by 4. |
| 775 | * But... this time we're lazy |
| 776 | */ |
| 777 | if (length % AS16_VALUE_SIZE ) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 778 | return NULL; |
| 779 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 780 | memset (&as, 0, sizeof (struct aspath)); |
| 781 | as.segments = assegments_parse (s, length, use32bit); |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 782 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 783 | /* If already same aspath exist then return it. */ |
| 784 | find = hash_get (ashash, &as, aspath_hash_alloc); |
paul | 0233542 | 2006-01-16 11:13:27 +0000 | [diff] [blame] | 785 | |
| 786 | /* aspath_hash_alloc dupes segments too. that probably could be |
| 787 | * optimised out. |
| 788 | */ |
| 789 | assegment_free_all (as.segments); |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 790 | if (as.str) |
| 791 | XFREE (MTYPE_AS_STR, as.str); |
paul | 0233542 | 2006-01-16 11:13:27 +0000 | [diff] [blame] | 792 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 793 | if (! find) |
| 794 | return NULL; |
| 795 | find->refcnt++; |
| 796 | |
| 797 | return find; |
| 798 | } |
| 799 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 800 | static inline void |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 801 | assegment_data_put (struct stream *s, as_t *as, int num, int use32bit) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 802 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 803 | int i; |
| 804 | assert (num <= AS_SEGMENT_MAX); |
| 805 | |
| 806 | for (i = 0; i < num; i++) |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 807 | if ( use32bit ) |
| 808 | stream_putl (s, as[i]); |
| 809 | else |
| 810 | { |
| 811 | if ( as[i] <= BGP_AS_MAX ) |
| 812 | stream_putw(s, as[i]); |
| 813 | else |
| 814 | stream_putw(s, BGP_AS_TRANS); |
| 815 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 816 | } |
| 817 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 818 | static inline size_t |
| 819 | assegment_header_put (struct stream *s, u_char type, int length) |
| 820 | { |
| 821 | size_t lenp; |
| 822 | assert (length <= AS_SEGMENT_MAX); |
| 823 | stream_putc (s, type); |
| 824 | lenp = stream_get_endp (s); |
| 825 | stream_putc (s, length); |
| 826 | return lenp; |
| 827 | } |
| 828 | |
| 829 | /* write aspath data to stream */ |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 830 | size_t |
| 831 | aspath_put (struct stream *s, struct aspath *as, int use32bit ) |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 832 | { |
| 833 | struct assegment *seg = as->segments; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 834 | size_t bytes = 0; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 835 | |
| 836 | if (!seg || seg->length == 0) |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 837 | return 0; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 838 | |
| 839 | if (seg) |
| 840 | { |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 841 | /* |
| 842 | * Hey, what do we do when we have > STREAM_WRITABLE(s) here? |
| 843 | * At the moment, we would write out a partial aspath, and our peer |
| 844 | * will complain and drop the session :-/ |
| 845 | * |
| 846 | * The general assumption here is that many things tested will |
| 847 | * never happen. And, in real live, up to now, they have not. |
| 848 | */ |
| 849 | while (seg && (ASSEGMENT_LEN(seg, use32bit) <= STREAM_WRITEABLE(s))) |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 850 | { |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 851 | struct assegment *next = seg->next; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 852 | int written = 0; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 853 | int asns_packed = 0; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 854 | size_t lenp; |
| 855 | |
| 856 | /* Overlength segments have to be split up */ |
| 857 | while ( (seg->length - written) > AS_SEGMENT_MAX) |
| 858 | { |
| 859 | assegment_header_put (s, seg->type, AS_SEGMENT_MAX); |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 860 | assegment_data_put (s, seg->as, AS_SEGMENT_MAX, use32bit); |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 861 | written += AS_SEGMENT_MAX; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 862 | bytes += ASSEGMENT_SIZE (written, use32bit); |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | /* write the final segment, probably is also the first */ |
| 866 | lenp = assegment_header_put (s, seg->type, seg->length - written); |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 867 | assegment_data_put (s, (seg->as + written), seg->length - written, |
| 868 | use32bit); |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 869 | |
| 870 | /* Sequence-type segments can be 'packed' together |
| 871 | * Case of a segment which was overlength and split up |
| 872 | * will be missed here, but that doesn't matter. |
| 873 | */ |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 874 | while (next && ASSEGMENTS_PACKABLE (seg, next)) |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 875 | { |
| 876 | /* NB: We should never normally get here given we |
| 877 | * normalise aspath data when parse them. However, better |
| 878 | * safe than sorry. We potentially could call |
| 879 | * assegment_normalise here instead, but it's cheaper and |
| 880 | * easier to do it on the fly here rather than go through |
| 881 | * the segment list twice every time we write out |
| 882 | * aspath's. |
| 883 | */ |
| 884 | |
| 885 | /* Next segment's data can fit in this one */ |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 886 | assegment_data_put (s, next->as, next->length, use32bit); |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 887 | |
| 888 | /* update the length of the segment header */ |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 889 | stream_putc_at (s, lenp, seg->length - written + next->length); |
| 890 | asns_packed += next->length; |
| 891 | |
| 892 | next = next->next; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 893 | } |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 894 | |
| 895 | bytes += ASSEGMENT_SIZE (seg->length - written + asns_packed, |
| 896 | use32bit); |
| 897 | seg = next; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 898 | } |
| 899 | } |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 900 | return bytes; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | /* This is for SNMP BGP4PATHATTRASPATHSEGMENT |
| 904 | * We have no way to manage the storage, so we use a static stream |
| 905 | * wrapper around aspath_put. |
| 906 | */ |
| 907 | u_char * |
| 908 | aspath_snmp_pathseg (struct aspath *as, size_t *varlen) |
| 909 | { |
| 910 | #define SNMP_PATHSEG_MAX 1024 |
paul | 8fdc32a | 2006-01-16 12:01:29 +0000 | [diff] [blame] | 911 | |
| 912 | if (!snmp_stream) |
| 913 | snmp_stream = stream_new (SNMP_PATHSEG_MAX); |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 914 | else |
paul | 8fdc32a | 2006-01-16 12:01:29 +0000 | [diff] [blame] | 915 | stream_reset (snmp_stream); |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 916 | |
| 917 | if (!as) |
| 918 | { |
| 919 | *varlen = 0; |
| 920 | return NULL; |
| 921 | } |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 922 | aspath_put (snmp_stream, as, 0); /* use 16 bit for now here */ |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 923 | |
paul | 8fdc32a | 2006-01-16 12:01:29 +0000 | [diff] [blame] | 924 | *varlen = stream_get_endp (snmp_stream); |
| 925 | return stream_pnt(snmp_stream); |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| 928 | #define min(A,B) ((A) < (B) ? (A) : (B)) |
| 929 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 930 | static struct assegment * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 931 | aspath_aggregate_as_set_add (struct aspath *aspath, struct assegment *asset, |
| 932 | as_t as) |
| 933 | { |
| 934 | int i; |
| 935 | |
| 936 | /* If this is first AS set member, create new as-set segment. */ |
| 937 | if (asset == NULL) |
| 938 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 939 | asset = assegment_new (AS_SET, 1); |
| 940 | if (! aspath->segments) |
| 941 | aspath->segments = asset; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 942 | else |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 943 | { |
| 944 | struct assegment *seg = aspath->segments; |
| 945 | while (seg->next) |
| 946 | seg = seg->next; |
| 947 | seg->next = asset; |
| 948 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 949 | asset->type = AS_SET; |
| 950 | asset->length = 1; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 951 | asset->as[0] = as; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 952 | } |
| 953 | else |
| 954 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 955 | /* Check this AS value already exists or not. */ |
| 956 | for (i = 0; i < asset->length; i++) |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 957 | if (asset->as[i] == as) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 958 | return asset; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 959 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 960 | asset->length++; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 961 | asset->as = XREALLOC (MTYPE_AS_SEG_DATA, asset->as, |
| 962 | asset->length * AS_VALUE_SIZE); |
| 963 | asset->as[asset->length - 1] = as; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 964 | } |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 965 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 966 | |
| 967 | return asset; |
| 968 | } |
| 969 | |
| 970 | /* Modify as1 using as2 for aggregation. */ |
| 971 | struct aspath * |
| 972 | aspath_aggregate (struct aspath *as1, struct aspath *as2) |
| 973 | { |
| 974 | int i; |
| 975 | int minlen; |
| 976 | int match; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 977 | int from; |
| 978 | struct assegment *seg1 = as1->segments; |
| 979 | struct assegment *seg2 = as2->segments; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 980 | struct aspath *aspath = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 981 | struct assegment *asset; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 982 | struct assegment *prevseg = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 983 | |
| 984 | match = 0; |
| 985 | minlen = 0; |
| 986 | aspath = NULL; |
| 987 | asset = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 988 | |
| 989 | /* First of all check common leading sequence. */ |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 990 | while (seg1 && seg2) |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 991 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 992 | /* Check segment type. */ |
| 993 | if (seg1->type != seg2->type) |
| 994 | break; |
| 995 | |
| 996 | /* Minimum segment length. */ |
| 997 | minlen = min (seg1->length, seg2->length); |
| 998 | |
| 999 | for (match = 0; match < minlen; match++) |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1000 | if (seg1->as[match] != seg2->as[match]) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1001 | break; |
| 1002 | |
| 1003 | if (match) |
| 1004 | { |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1005 | struct assegment *seg = assegment_new (seg1->type, 0); |
| 1006 | |
| 1007 | seg = assegment_append_asns (seg, seg1->as, match); |
| 1008 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1009 | if (! aspath) |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1010 | { |
| 1011 | aspath = aspath_new (); |
| 1012 | aspath->segments = seg; |
| 1013 | } |
| 1014 | else |
| 1015 | prevseg->next = seg; |
| 1016 | |
| 1017 | prevseg = seg; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | if (match != minlen || match != seg1->length |
| 1021 | || seg1->length != seg2->length) |
| 1022 | break; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1023 | |
| 1024 | seg1 = seg1->next; |
| 1025 | seg2 = seg2->next; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | if (! aspath) |
| 1029 | aspath = aspath_new(); |
| 1030 | |
| 1031 | /* Make as-set using rest of all information. */ |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1032 | from = match; |
| 1033 | while (seg1) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1034 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1035 | for (i = from; i < seg1->length; i++) |
| 1036 | asset = aspath_aggregate_as_set_add (aspath, asset, seg1->as[i]); |
| 1037 | |
| 1038 | from = 0; |
| 1039 | seg1 = seg1->next; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1042 | from = match; |
| 1043 | while (seg2) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1044 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1045 | for (i = from; i < seg2->length; i++) |
| 1046 | asset = aspath_aggregate_as_set_add (aspath, asset, seg2->as[i]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1047 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1048 | from = 0; |
| 1049 | seg2 = seg2->next; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1050 | } |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1051 | |
| 1052 | assegment_normalise (aspath->segments); |
| 1053 | aspath_str_update (aspath); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1054 | return aspath; |
| 1055 | } |
| 1056 | |
| 1057 | /* When a BGP router receives an UPDATE with an MP_REACH_NLRI |
| 1058 | attribute, check the leftmost AS number in the AS_PATH attribute is |
| 1059 | or not the peer's AS number. */ |
| 1060 | int |
| 1061 | aspath_firstas_check (struct aspath *aspath, as_t asno) |
| 1062 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1063 | if ( (aspath == NULL) || (aspath->segments == NULL) ) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1064 | return 0; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1065 | |
| 1066 | if (aspath->segments |
| 1067 | && (aspath->segments->type == AS_SEQUENCE) |
| 1068 | && (aspath->segments->as[0] == asno )) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1069 | return 1; |
| 1070 | |
| 1071 | return 0; |
| 1072 | } |
| 1073 | |
Paul Jakma | 1f742f2 | 2006-08-06 15:52:11 +0000 | [diff] [blame] | 1074 | /* AS path loop check. If aspath contains asno then return >= 1. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1075 | int |
| 1076 | aspath_loop_check (struct aspath *aspath, as_t asno) |
| 1077 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1078 | struct assegment *seg; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1079 | int count = 0; |
| 1080 | |
Paul Jakma | 1f742f2 | 2006-08-06 15:52:11 +0000 | [diff] [blame] | 1081 | if ( (aspath == NULL) || (aspath->segments == NULL) ) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1082 | return 0; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1083 | |
| 1084 | seg = aspath->segments; |
| 1085 | |
| 1086 | while (seg) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1087 | { |
| 1088 | int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1089 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1090 | for (i = 0; i < seg->length; i++) |
| 1091 | if (seg->as[i] == asno) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1092 | count++; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1093 | |
| 1094 | seg = seg->next; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1095 | } |
| 1096 | return count; |
| 1097 | } |
| 1098 | |
| 1099 | /* When all of AS path is private AS return 1. */ |
| 1100 | int |
| 1101 | aspath_private_as_check (struct aspath *aspath) |
| 1102 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1103 | struct assegment *seg; |
| 1104 | |
| 1105 | if ( !(aspath && aspath->segments) ) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1106 | return 0; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1107 | |
| 1108 | seg = aspath->segments; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1109 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1110 | while (seg) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1111 | { |
| 1112 | int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1113 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1114 | for (i = 0; i < seg->length; i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1115 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1116 | if ( (seg->as[i] < BGP_PRIVATE_AS_MIN) |
| 1117 | || (seg->as[i] > BGP_PRIVATE_AS_MAX) ) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1118 | return 0; |
| 1119 | } |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1120 | seg = seg->next; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1121 | } |
| 1122 | return 1; |
| 1123 | } |
| 1124 | |
| 1125 | /* Merge as1 to as2. as2 should be uninterned aspath. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1126 | static struct aspath * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1127 | aspath_merge (struct aspath *as1, struct aspath *as2) |
| 1128 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1129 | struct assegment *last, *new; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1130 | |
| 1131 | if (! as1 || ! as2) |
| 1132 | return NULL; |
| 1133 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1134 | last = new = assegment_dup_all (as1->segments); |
| 1135 | |
| 1136 | /* find the last valid segment */ |
| 1137 | while (last && last->next) |
| 1138 | last = last->next; |
| 1139 | |
| 1140 | last->next = as2->segments; |
| 1141 | as2->segments = new; |
| 1142 | aspath_str_update (as2); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1143 | return as2; |
| 1144 | } |
| 1145 | |
| 1146 | /* Prepend as1 to as2. as2 should be uninterned aspath. */ |
| 1147 | struct aspath * |
| 1148 | aspath_prepend (struct aspath *as1, struct aspath *as2) |
| 1149 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1150 | struct assegment *seg1; |
| 1151 | struct assegment *seg2; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1152 | |
| 1153 | if (! as1 || ! as2) |
| 1154 | return NULL; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1155 | |
| 1156 | seg1 = as1->segments; |
| 1157 | seg2 = as2->segments; |
| 1158 | |
| 1159 | /* If as2 is empty, only need to dupe as1's chain onto as2 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1160 | if (seg2 == NULL) |
| 1161 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1162 | as2->segments = assegment_dup_all (as1->segments); |
| 1163 | aspath_str_update (as2); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1164 | return as2; |
| 1165 | } |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1166 | |
| 1167 | /* If as1 is empty AS, no prepending to do. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1168 | if (seg1 == NULL) |
| 1169 | return as2; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1170 | |
| 1171 | /* find the tail as1's segment chain. */ |
| 1172 | while (seg1 && seg1->next) |
| 1173 | seg1 = seg1->next; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1174 | |
| 1175 | /* Compare last segment type of as1 and first segment type of as2. */ |
| 1176 | if (seg1->type != seg2->type) |
| 1177 | return aspath_merge (as1, as2); |
| 1178 | |
| 1179 | if (seg1->type == AS_SEQUENCE) |
| 1180 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1181 | /* We have two chains of segments, as1->segments and seg2, |
| 1182 | * and we have to attach them together, merging the attaching |
| 1183 | * segments together into one. |
| 1184 | * |
| 1185 | * 1. dupe as1->segments onto head of as2 |
| 1186 | * 2. merge seg2's asns onto last segment of this new chain |
| 1187 | * 3. attach chain after seg2 |
| 1188 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1189 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1190 | /* dupe as1 onto as2's head */ |
| 1191 | seg1 = as2->segments = assegment_dup_all (as1->segments); |
| 1192 | |
| 1193 | /* refind the tail of as2, reusing seg1 */ |
| 1194 | while (seg1 && seg1->next) |
| 1195 | seg1 = seg1->next; |
| 1196 | |
| 1197 | /* merge the old head, seg2, into tail, seg1 */ |
| 1198 | seg1 = assegment_append_asns (seg1, seg2->as, seg2->length); |
| 1199 | |
| 1200 | /* bypass the merged seg2, and attach any chain after it to |
| 1201 | * chain descending from as2's head |
| 1202 | */ |
| 1203 | seg1->next = seg2->next; |
| 1204 | |
| 1205 | /* seg2 is now referenceless and useless*/ |
| 1206 | assegment_free (seg2); |
| 1207 | |
| 1208 | /* we've now prepended as1's segment chain to as2, merging |
| 1209 | * the inbetween AS_SEQUENCE of seg2 in the process |
| 1210 | */ |
| 1211 | aspath_str_update (as2); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1212 | return as2; |
| 1213 | } |
| 1214 | else |
| 1215 | { |
| 1216 | /* AS_SET merge code is needed at here. */ |
| 1217 | return aspath_merge (as1, as2); |
| 1218 | } |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1219 | /* XXX: Ermmm, what if as1 has multiple segments?? */ |
| 1220 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1221 | /* Not reached */ |
| 1222 | } |
| 1223 | |
Denis Ovsienko | 841f7a5 | 2008-04-10 11:47:45 +0000 | [diff] [blame] | 1224 | /* Iterate over AS_PATH segments and wipe all occurences of the |
| 1225 | * listed AS numbers. Hence some segments may lose some or even |
| 1226 | * all data on the way, the operation is implemented as a smarter |
| 1227 | * version of aspath_dup(), which allocates memory to hold the new |
| 1228 | * data, not the original. The new AS path is returned. |
| 1229 | */ |
| 1230 | struct aspath * |
| 1231 | aspath_filter_exclude (struct aspath * source, struct aspath * exclude_list) |
| 1232 | { |
| 1233 | struct assegment * srcseg, * exclseg, * lastseg; |
| 1234 | struct aspath * newpath; |
| 1235 | |
| 1236 | newpath = aspath_new(); |
| 1237 | lastseg = NULL; |
| 1238 | |
| 1239 | for (srcseg = source->segments; srcseg; srcseg = srcseg->next) |
| 1240 | { |
| 1241 | unsigned i, y, newlen = 0, done = 0, skip_as; |
| 1242 | struct assegment * newseg; |
| 1243 | |
| 1244 | /* Find out, how much ASns are we going to pick from this segment. |
| 1245 | * We can't perform filtering right inline, because the size of |
| 1246 | * the new segment isn't known at the moment yet. |
| 1247 | */ |
| 1248 | for (i = 0; i < srcseg->length; i++) |
| 1249 | { |
| 1250 | skip_as = 0; |
| 1251 | for (exclseg = exclude_list->segments; exclseg && !skip_as; exclseg = exclseg->next) |
| 1252 | for (y = 0; y < exclseg->length; y++) |
| 1253 | if (srcseg->as[i] == exclseg->as[y]) |
| 1254 | { |
| 1255 | skip_as = 1; |
| 1256 | // There's no sense in testing the rest of exclusion list, bail out. |
| 1257 | break; |
| 1258 | } |
| 1259 | if (!skip_as) |
| 1260 | newlen++; |
| 1261 | } |
| 1262 | /* newlen is now the number of ASns to copy */ |
| 1263 | if (!newlen) |
| 1264 | continue; |
| 1265 | |
| 1266 | /* Actual copying. Allocate memory and iterate once more, performing filtering. */ |
| 1267 | newseg = assegment_new (srcseg->type, newlen); |
| 1268 | for (i = 0; i < srcseg->length; i++) |
| 1269 | { |
| 1270 | skip_as = 0; |
| 1271 | for (exclseg = exclude_list->segments; exclseg && !skip_as; exclseg = exclseg->next) |
| 1272 | for (y = 0; y < exclseg->length; y++) |
| 1273 | if (srcseg->as[i] == exclseg->as[y]) |
| 1274 | { |
| 1275 | skip_as = 1; |
| 1276 | break; |
| 1277 | } |
| 1278 | if (skip_as) |
| 1279 | continue; |
| 1280 | newseg->as[done++] = srcseg->as[i]; |
| 1281 | } |
| 1282 | /* At his point newlen must be equal to done, and both must be positive. Append |
| 1283 | * the filtered segment to the gross result. */ |
| 1284 | if (!lastseg) |
| 1285 | newpath->segments = newseg; |
| 1286 | else |
| 1287 | lastseg->next = newseg; |
| 1288 | lastseg = newseg; |
| 1289 | } |
| 1290 | aspath_str_update (newpath); |
| 1291 | /* We are happy returning even an empty AS_PATH, because the administrator |
| 1292 | * might expect this very behaviour. There's a mean to avoid this, if necessary, |
| 1293 | * by having a match rule against certain AS_PATH regexps in the route-map index. |
| 1294 | */ |
| 1295 | aspath_free (source); |
| 1296 | return newpath; |
| 1297 | } |
| 1298 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1299 | /* Add specified AS to the leftmost of aspath. */ |
| 1300 | static struct aspath * |
| 1301 | aspath_add_one_as (struct aspath *aspath, as_t asno, u_char type) |
| 1302 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1303 | struct assegment *assegment = aspath->segments; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1304 | |
| 1305 | /* In case of empty aspath. */ |
| 1306 | if (assegment == NULL || assegment->length == 0) |
| 1307 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1308 | aspath->segments = assegment_new (type, 1); |
| 1309 | aspath->segments->as[0] = asno; |
| 1310 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1311 | if (assegment) |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1312 | assegment_free (assegment); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1313 | |
| 1314 | return aspath; |
| 1315 | } |
| 1316 | |
| 1317 | if (assegment->type == type) |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1318 | aspath->segments = assegment_prepend_asns (aspath->segments, asno, 1); |
| 1319 | else |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1320 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1321 | /* create new segment |
| 1322 | * push it onto head of aspath's segment chain |
| 1323 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1324 | struct assegment *newsegment; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1325 | |
| 1326 | newsegment = assegment_new (type, 1); |
| 1327 | newsegment->as[0] = asno; |
| 1328 | |
| 1329 | newsegment->next = assegment; |
| 1330 | aspath->segments = newsegment; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | return aspath; |
| 1334 | } |
| 1335 | |
| 1336 | /* Add specified AS to the leftmost of aspath. */ |
| 1337 | struct aspath * |
| 1338 | aspath_add_seq (struct aspath *aspath, as_t asno) |
| 1339 | { |
| 1340 | return aspath_add_one_as (aspath, asno, AS_SEQUENCE); |
| 1341 | } |
| 1342 | |
| 1343 | /* Compare leftmost AS value for MED check. If as1's leftmost AS and |
| 1344 | as2's leftmost AS is same return 1. */ |
| 1345 | int |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 1346 | aspath_cmp_left (const struct aspath *aspath1, const struct aspath *aspath2) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1347 | { |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 1348 | const struct assegment *seg1 = NULL; |
| 1349 | const struct assegment *seg2 = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1350 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1351 | if (!(aspath1 && aspath2)) |
| 1352 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1353 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1354 | seg1 = aspath1->segments; |
| 1355 | seg2 = aspath2->segments; |
| 1356 | |
| 1357 | /* find first non-confed segments for each */ |
| 1358 | while (seg1 && ((seg1->type == AS_CONFED_SEQUENCE) |
| 1359 | || (seg1->type == AS_CONFED_SET))) |
| 1360 | seg1 = seg1->next; |
| 1361 | |
| 1362 | while (seg2 && ((seg2->type == AS_CONFED_SEQUENCE) |
| 1363 | || (seg2->type == AS_CONFED_SET))) |
| 1364 | seg2 = seg2->next; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1365 | |
| 1366 | /* Check as1's */ |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1367 | if (!(seg1 && seg2 |
| 1368 | && (seg1->type == AS_SEQUENCE) && (seg2->type == AS_SEQUENCE))) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1369 | return 0; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1370 | |
| 1371 | if (seg1->as[0] == seg2->as[0]) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1372 | return 1; |
| 1373 | |
| 1374 | return 0; |
| 1375 | } |
| 1376 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1377 | /* Truncate an aspath after a number of hops, and put the hops remaining |
| 1378 | * at the front of another aspath. Needed for AS4 compat. |
| 1379 | * |
| 1380 | * Returned aspath is a /new/ aspath, which should either by free'd or |
| 1381 | * interned by the caller, as desired. |
| 1382 | */ |
| 1383 | struct aspath * |
| 1384 | aspath_reconcile_as4 ( struct aspath *aspath, struct aspath *as4path) |
| 1385 | { |
| 1386 | struct assegment *seg, *newseg, *prevseg = NULL; |
| 1387 | struct aspath *newpath = NULL, *mergedpath; |
| 1388 | int hops, cpasns = 0; |
| 1389 | |
| 1390 | if (!aspath) |
| 1391 | return NULL; |
| 1392 | |
| 1393 | seg = aspath->segments; |
| 1394 | |
| 1395 | /* CONFEDs should get reconciled too.. */ |
| 1396 | hops = (aspath_count_hops (aspath) + aspath_count_confeds (aspath)) |
| 1397 | - aspath_count_hops (as4path); |
| 1398 | |
| 1399 | if (hops < 0) |
| 1400 | { |
| 1401 | if (BGP_DEBUG (as4, AS4)) |
| 1402 | zlog_warn ("[AS4] Fewer hops in AS_PATH than NEW_AS_PATH"); |
| 1403 | /* Something's gone wrong. The RFC says we should now ignore AS4_PATH, |
| 1404 | * which is daft behaviour - it contains vital loop-detection |
| 1405 | * information which must have been removed from AS_PATH. |
| 1406 | */ |
| 1407 | hops = aspath_count_hops (aspath); |
| 1408 | } |
| 1409 | |
| 1410 | if (!hops) |
| 1411 | return aspath_dup (as4path); |
| 1412 | |
| 1413 | if ( BGP_DEBUG(as4, AS4)) |
| 1414 | zlog_debug("[AS4] got AS_PATH %s and AS4_PATH %s synthesizing now", |
| 1415 | aspath->str, as4path->str); |
| 1416 | |
| 1417 | while (seg && hops > 0) |
| 1418 | { |
| 1419 | switch (seg->type) |
| 1420 | { |
| 1421 | case AS_SET: |
| 1422 | case AS_CONFED_SET: |
| 1423 | hops--; |
| 1424 | cpasns = seg->length; |
| 1425 | break; |
| 1426 | case AS_CONFED_SEQUENCE: |
| 1427 | /* Should never split a confed-sequence, if hop-count |
| 1428 | * suggests we must then something's gone wrong somewhere. |
| 1429 | * |
| 1430 | * Most important goal is to preserve AS_PATHs prime function |
| 1431 | * as loop-detector, so we fudge the numbers so that the entire |
| 1432 | * confed-sequence is merged in. |
| 1433 | */ |
| 1434 | if (hops < seg->length) |
| 1435 | { |
| 1436 | if (BGP_DEBUG (as4, AS4)) |
| 1437 | zlog_debug ("[AS4] AS4PATHmangle: AS_CONFED_SEQUENCE falls" |
| 1438 | " across 2/4 ASN boundary somewhere, broken.."); |
| 1439 | hops = seg->length; |
| 1440 | } |
| 1441 | case AS_SEQUENCE: |
| 1442 | cpasns = MIN(seg->length, hops); |
| 1443 | hops -= seg->length; |
| 1444 | } |
| 1445 | |
| 1446 | assert (cpasns <= seg->length); |
| 1447 | |
| 1448 | newseg = assegment_new (seg->type, 0); |
| 1449 | newseg = assegment_append_asns (newseg, seg->as, cpasns); |
| 1450 | |
| 1451 | if (!newpath) |
| 1452 | { |
| 1453 | newpath = aspath_new (); |
| 1454 | newpath->segments = newseg; |
| 1455 | } |
| 1456 | else |
| 1457 | prevseg->next = newseg; |
| 1458 | |
| 1459 | prevseg = newseg; |
| 1460 | seg = seg->next; |
| 1461 | } |
| 1462 | |
| 1463 | /* We may be able to join some segments here, and we must |
| 1464 | * do this because... we want normalised aspaths in out hash |
| 1465 | * and we do not want to stumble in aspath_put. |
| 1466 | */ |
| 1467 | mergedpath = aspath_merge (newpath, aspath_dup(as4path)); |
| 1468 | aspath_free (newpath); |
| 1469 | mergedpath->segments = assegment_normalise (mergedpath->segments); |
| 1470 | aspath_str_update (mergedpath); |
| 1471 | |
| 1472 | if ( BGP_DEBUG(as4, AS4)) |
| 1473 | zlog_debug ("[AS4] result of synthesizing is %s", |
| 1474 | mergedpath->str); |
| 1475 | |
| 1476 | return mergedpath; |
| 1477 | } |
| 1478 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1479 | /* Compare leftmost AS value for MED check. If as1's leftmost AS and |
| 1480 | as2's leftmost AS is same return 1. (confederation as-path |
| 1481 | only). */ |
| 1482 | int |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 1483 | aspath_cmp_left_confed (const struct aspath *aspath1, const struct aspath *aspath2) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1484 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1485 | if (! (aspath1 && aspath2) ) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1486 | return 0; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1487 | |
paul | ad72740 | 2005-11-23 02:47:02 +0000 | [diff] [blame] | 1488 | if ( !(aspath1->segments && aspath2->segments) ) |
| 1489 | return 0; |
| 1490 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1491 | if ( (aspath1->segments->type != AS_CONFED_SEQUENCE) |
| 1492 | || (aspath2->segments->type != AS_CONFED_SEQUENCE) ) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1493 | return 0; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1494 | |
| 1495 | if (aspath1->segments->as[0] == aspath2->segments->as[0]) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1496 | return 1; |
| 1497 | |
| 1498 | return 0; |
| 1499 | } |
| 1500 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1501 | /* Delete all leading AS_CONFED_SEQUENCE/SET segments from aspath. |
| 1502 | * See RFC3065, 6.1 c1 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1503 | struct aspath * |
| 1504 | aspath_delete_confed_seq (struct aspath *aspath) |
| 1505 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1506 | struct assegment *seg; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1507 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1508 | if (!(aspath && aspath->segments)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1509 | return aspath; |
| 1510 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1511 | seg = aspath->segments; |
| 1512 | |
| 1513 | /* "if the first path segment of the AS_PATH is |
| 1514 | * of type AS_CONFED_SEQUENCE," |
| 1515 | */ |
| 1516 | if (aspath->segments->type != AS_CONFED_SEQUENCE) |
| 1517 | return aspath; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1518 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1519 | /* "... that segment and any immediately following segments |
| 1520 | * of the type AS_CONFED_SET or AS_CONFED_SEQUENCE are removed |
| 1521 | * from the AS_PATH attribute," |
| 1522 | */ |
| 1523 | while (seg && |
| 1524 | (seg->type == AS_CONFED_SEQUENCE || seg->type == AS_CONFED_SET)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1525 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1526 | aspath->segments = seg->next; |
| 1527 | assegment_free (seg); |
| 1528 | seg = aspath->segments; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1529 | } |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1530 | aspath_str_update (aspath); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1531 | return aspath; |
| 1532 | } |
| 1533 | |
| 1534 | /* Add new AS number to the leftmost part of the aspath as |
| 1535 | AS_CONFED_SEQUENCE. */ |
| 1536 | struct aspath* |
| 1537 | aspath_add_confed_seq (struct aspath *aspath, as_t asno) |
| 1538 | { |
| 1539 | return aspath_add_one_as (aspath, asno, AS_CONFED_SEQUENCE); |
| 1540 | } |
| 1541 | |
| 1542 | /* Add new as value to as path structure. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1543 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1544 | aspath_as_add (struct aspath *as, as_t asno) |
| 1545 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1546 | struct assegment *seg = as->segments; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1547 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1548 | if (!seg) |
| 1549 | return; |
| 1550 | |
Andrew J. Schorr | 93c1749 | 2007-04-15 19:17:24 +0000 | [diff] [blame] | 1551 | /* Last segment search procedure. */ |
| 1552 | while (seg->next) |
| 1553 | seg = seg->next; |
| 1554 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1555 | assegment_append_asns (seg, &asno, 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | /* Add new as segment to the as path. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1559 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1560 | aspath_segment_add (struct aspath *as, int type) |
| 1561 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1562 | struct assegment *seg = as->segments; |
| 1563 | struct assegment *new = assegment_new (type, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1564 | |
Andrew J. Schorr | 93c1749 | 2007-04-15 19:17:24 +0000 | [diff] [blame] | 1565 | if (seg) |
| 1566 | { |
| 1567 | while (seg->next) |
| 1568 | seg = seg->next; |
| 1569 | seg->next = new; |
| 1570 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1571 | else |
Andrew J. Schorr | 93c1749 | 2007-04-15 19:17:24 +0000 | [diff] [blame] | 1572 | as->segments = new; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | struct aspath * |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1576 | aspath_empty (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1577 | { |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1578 | return aspath_parse (NULL, 0, 1); /* 32Bit ;-) */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | struct aspath * |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1582 | aspath_empty_get (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1583 | { |
| 1584 | struct aspath *aspath; |
| 1585 | |
| 1586 | aspath = aspath_new (); |
| 1587 | aspath->str = aspath_make_str_count (aspath); |
| 1588 | return aspath; |
| 1589 | } |
| 1590 | |
| 1591 | unsigned long |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1592 | aspath_count (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1593 | { |
| 1594 | return ashash->count; |
| 1595 | } |
| 1596 | |
| 1597 | /* |
| 1598 | Theoretically, one as path can have: |
| 1599 | |
| 1600 | One BGP packet size should be less than 4096. |
| 1601 | One BGP attribute size should be less than 4096 - BGP header size. |
| 1602 | One BGP aspath size should be less than 4096 - BGP header size - |
| 1603 | BGP mandantry attribute size. |
| 1604 | */ |
| 1605 | |
| 1606 | /* AS path string lexical token enum. */ |
| 1607 | enum as_token |
| 1608 | { |
| 1609 | as_token_asval, |
| 1610 | as_token_set_start, |
| 1611 | as_token_set_end, |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1612 | as_token_confed_seq_start, |
| 1613 | as_token_confed_seq_end, |
| 1614 | as_token_confed_set_start, |
| 1615 | as_token_confed_set_end, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1616 | as_token_unknown |
| 1617 | }; |
| 1618 | |
| 1619 | /* Return next token and point for string parse. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1620 | static const char * |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1621 | aspath_gettoken (const char *buf, enum as_token *token, u_long *asno) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1622 | { |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 1623 | const char *p = buf; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1624 | |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1625 | /* Skip seperators (space for sequences, ',' for sets). */ |
| 1626 | while (isspace ((int) *p) || *p == ',') |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1627 | p++; |
| 1628 | |
| 1629 | /* Check the end of the string and type specify characters |
| 1630 | (e.g. {}()). */ |
| 1631 | switch (*p) |
| 1632 | { |
| 1633 | case '\0': |
| 1634 | return NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1635 | case '{': |
| 1636 | *token = as_token_set_start; |
| 1637 | p++; |
| 1638 | return p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1639 | case '}': |
| 1640 | *token = as_token_set_end; |
| 1641 | p++; |
| 1642 | return p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1643 | case '(': |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1644 | *token = as_token_confed_seq_start; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1645 | p++; |
| 1646 | return p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1647 | case ')': |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1648 | *token = as_token_confed_seq_end; |
| 1649 | p++; |
| 1650 | return p; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1651 | case '[': |
| 1652 | *token = as_token_confed_set_start; |
| 1653 | p++; |
| 1654 | return p; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1655 | case ']': |
| 1656 | *token = as_token_confed_set_end; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1657 | p++; |
| 1658 | return p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | /* Check actual AS value. */ |
| 1662 | if (isdigit ((int) *p)) |
| 1663 | { |
Denis Ovsienko | 10819ec | 2009-06-09 15:15:33 +0400 | [diff] [blame] | 1664 | as_t asval; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1665 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1666 | *token = as_token_asval; |
| 1667 | asval = (*p - '0'); |
| 1668 | p++; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1669 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1670 | while (isdigit ((int) *p)) |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1671 | { |
| 1672 | asval *= 10; |
| 1673 | asval += (*p - '0'); |
| 1674 | p++; |
| 1675 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1676 | *asno = asval; |
| 1677 | return p; |
| 1678 | } |
| 1679 | |
| 1680 | /* There is no match then return unknown token. */ |
| 1681 | *token = as_token_unknown; |
| 1682 | return p++; |
| 1683 | } |
| 1684 | |
| 1685 | struct aspath * |
paul | fd79ac9 | 2004-10-13 05:06:08 +0000 | [diff] [blame] | 1686 | aspath_str2aspath (const char *str) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1687 | { |
paul | 3fff6ff | 2006-02-05 17:55:35 +0000 | [diff] [blame] | 1688 | enum as_token token = as_token_unknown; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1689 | u_short as_type; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1690 | u_long asno = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1691 | struct aspath *aspath; |
| 1692 | int needtype; |
| 1693 | |
| 1694 | aspath = aspath_new (); |
| 1695 | |
| 1696 | /* We start default type as AS_SEQUENCE. */ |
| 1697 | as_type = AS_SEQUENCE; |
| 1698 | needtype = 1; |
| 1699 | |
| 1700 | while ((str = aspath_gettoken (str, &token, &asno)) != NULL) |
| 1701 | { |
| 1702 | switch (token) |
| 1703 | { |
| 1704 | case as_token_asval: |
| 1705 | if (needtype) |
| 1706 | { |
| 1707 | aspath_segment_add (aspath, as_type); |
| 1708 | needtype = 0; |
| 1709 | } |
| 1710 | aspath_as_add (aspath, asno); |
| 1711 | break; |
| 1712 | case as_token_set_start: |
| 1713 | as_type = AS_SET; |
| 1714 | aspath_segment_add (aspath, as_type); |
| 1715 | needtype = 0; |
| 1716 | break; |
| 1717 | case as_token_set_end: |
| 1718 | as_type = AS_SEQUENCE; |
| 1719 | needtype = 1; |
| 1720 | break; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1721 | case as_token_confed_seq_start: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1722 | as_type = AS_CONFED_SEQUENCE; |
| 1723 | aspath_segment_add (aspath, as_type); |
| 1724 | needtype = 0; |
| 1725 | break; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1726 | case as_token_confed_seq_end: |
| 1727 | as_type = AS_SEQUENCE; |
| 1728 | needtype = 1; |
| 1729 | break; |
| 1730 | case as_token_confed_set_start: |
| 1731 | as_type = AS_CONFED_SET; |
| 1732 | aspath_segment_add (aspath, as_type); |
| 1733 | needtype = 0; |
| 1734 | break; |
| 1735 | case as_token_confed_set_end: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1736 | as_type = AS_SEQUENCE; |
| 1737 | needtype = 1; |
| 1738 | break; |
| 1739 | case as_token_unknown: |
| 1740 | default: |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1741 | aspath_free (aspath); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1742 | return NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | aspath->str = aspath_make_str_count (aspath); |
| 1747 | |
| 1748 | return aspath; |
| 1749 | } |
| 1750 | |
| 1751 | /* Make hash value by raw aspath data. */ |
| 1752 | unsigned int |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 1753 | aspath_key_make (void *p) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1754 | { |
Paul Jakma | 923de65 | 2007-04-29 18:25:17 +0000 | [diff] [blame] | 1755 | struct aspath * aspath = (struct aspath *) p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1756 | unsigned int key = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1757 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1758 | if (!aspath->str) |
| 1759 | aspath_str_update (aspath); |
| 1760 | |
| 1761 | key = jhash (aspath->str, strlen(aspath->str), 2334325); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1762 | |
| 1763 | return key; |
| 1764 | } |
| 1765 | |
| 1766 | /* If two aspath have same value then return 1 else return 0 */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1767 | static int |
Stephen Hemminger | ffe11cf | 2008-08-14 16:25:25 +0100 | [diff] [blame] | 1768 | aspath_cmp (const void *arg1, const void *arg2) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1769 | { |
Denis Ovsienko | aea339f | 2009-04-30 17:16:22 +0400 | [diff] [blame] | 1770 | const struct assegment *seg1 = ((const struct aspath *)arg1)->segments; |
| 1771 | const struct assegment *seg2 = ((const struct aspath *)arg2)->segments; |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1772 | |
| 1773 | while (seg1 || seg2) |
| 1774 | { |
| 1775 | int i; |
| 1776 | if ((!seg1 && seg2) || (seg1 && !seg2)) |
| 1777 | return 0; |
| 1778 | if (seg1->type != seg2->type) |
| 1779 | return 0; |
| 1780 | if (seg1->length != seg2->length) |
| 1781 | return 0; |
| 1782 | for (i = 0; i < seg1->length; i++) |
| 1783 | if (seg1->as[i] != seg2->as[i]) |
| 1784 | return 0; |
| 1785 | seg1 = seg1->next; |
| 1786 | seg2 = seg2->next; |
| 1787 | } |
| 1788 | return 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | /* AS path hash initialize. */ |
| 1792 | void |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1793 | aspath_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1794 | { |
| 1795 | ashash = hash_create_size (32767, aspath_key_make, aspath_cmp); |
| 1796 | } |
paul | 8fdc32a | 2006-01-16 12:01:29 +0000 | [diff] [blame] | 1797 | |
| 1798 | void |
| 1799 | aspath_finish (void) |
| 1800 | { |
| 1801 | hash_free (ashash); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame^] | 1802 | ashash = NULL; |
paul | 8fdc32a | 2006-01-16 12:01:29 +0000 | [diff] [blame] | 1803 | |
| 1804 | if (snmp_stream) |
| 1805 | stream_free (snmp_stream); |
| 1806 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1807 | |
| 1808 | /* return and as path value */ |
| 1809 | const char * |
| 1810 | aspath_print (struct aspath *as) |
| 1811 | { |
paul | fe69a50 | 2005-09-10 16:55:02 +0000 | [diff] [blame] | 1812 | return (as ? as->str : NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | /* Printing functions */ |
Denis Ovsienko | 841f7a5 | 2008-04-10 11:47:45 +0000 | [diff] [blame] | 1816 | /* Feed the AS_PATH to the vty; the suffix string follows it only in case |
| 1817 | * AS_PATH wasn't empty. |
| 1818 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1819 | void |
Denis Ovsienko | 841f7a5 | 2008-04-10 11:47:45 +0000 | [diff] [blame] | 1820 | aspath_print_vty (struct vty *vty, const char *format, struct aspath *as, const char * suffix) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1821 | { |
Paul Jakma | b2518c1 | 2006-05-12 23:48:40 +0000 | [diff] [blame] | 1822 | assert (format); |
| 1823 | vty_out (vty, format, as->str); |
Denis Ovsienko | 841f7a5 | 2008-04-10 11:47:45 +0000 | [diff] [blame] | 1824 | if (strlen (as->str) && strlen (suffix)) |
| 1825 | vty_out (vty, "%s", suffix); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1828 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1829 | aspath_show_all_iterator (struct hash_backet *backet, struct vty *vty) |
| 1830 | { |
| 1831 | struct aspath *as; |
| 1832 | |
| 1833 | as = (struct aspath *) backet->data; |
| 1834 | |
paul | efa9f83 | 2002-12-13 21:47:59 +0000 | [diff] [blame] | 1835 | vty_out (vty, "[%p:%u] (%ld) ", backet, backet->key, as->refcnt); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1836 | vty_out (vty, "%s%s", as->str, VTY_NEWLINE); |
| 1837 | } |
| 1838 | |
| 1839 | /* Print all aspath and hash information. This function is used from |
| 1840 | `show ip bgp paths' command. */ |
| 1841 | void |
| 1842 | aspath_print_all_vty (struct vty *vty) |
| 1843 | { |
| 1844 | hash_iterate (ashash, |
| 1845 | (void (*) (struct hash_backet *, void *)) |
| 1846 | aspath_show_all_iterator, |
| 1847 | vty); |
| 1848 | } |