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