]> git.ipfire.org Git - thirdparty/bird.git/blob - proto/bgp/attrs.c
Merge tag 'v1.6.2' into int-new
[thirdparty/bird.git] / proto / bgp / attrs.c
1 /*
2 * BIRD -- BGP Attributes
3 *
4 * (c) 2000 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 #undef LOCAL_DEBUG
10
11 #include <stdlib.h>
12
13 #include "nest/bird.h"
14 #include "nest/iface.h"
15 #include "nest/protocol.h"
16 #include "nest/route.h"
17 #include "nest/attrs.h"
18 #include "conf/conf.h"
19 #include "lib/resource.h"
20 #include "lib/string.h"
21 #include "lib/unaligned.h"
22
23 #include "bgp.h"
24
25 /*
26 * UPDATE message error handling
27 *
28 * All checks from RFC 4271 6.3 are done as specified with these exceptions:
29 * - The semantic check of an IP address from NEXT_HOP attribute is missing.
30 * - Checks of some optional attribute values are missing.
31 * - Syntactic and semantic checks of NLRIs (done in DECODE_PREFIX())
32 * are probably inadequate.
33 *
34 * Loop detection based on AS_PATH causes updates to be withdrawn. RFC
35 * 4271 does not explicitly specifiy the behavior in that case.
36 *
37 * Loop detection related to route reflection (based on ORIGINATOR_ID
38 * and CLUSTER_LIST) causes updates to be withdrawn. RFC 4456 8
39 * specifies that such updates should be ignored, but that is generally
40 * a bad idea.
41 *
42 * Error checking of optional transitive attributes is done according to
43 * draft-ietf-idr-optional-transitive-03, but errors are handled always
44 * as withdraws.
45 *
46 * Unexpected AS_CONFED_* segments in AS_PATH are logged and removed,
47 * but unknown segments cause a session drop with Malformed AS_PATH
48 * error (see validate_path()). The behavior in such case is not
49 * explicitly specified by RFC 4271. RFC 5065 specifies that
50 * inconsistent AS_CONFED_* segments should cause a session drop, but
51 * implementations that pass invalid AS_CONFED_* segments are
52 * widespread.
53 *
54 * Error handling of AS4_* attributes is done as specified by
55 * draft-ietf-idr-rfc4893bis-03. There are several possible
56 * inconsistencies between AGGREGATOR and AS4_AGGREGATOR that are not
57 * handled by that draft, these are logged and ignored (see
58 * bgp_reconstruct_4b_attrs()).
59 */
60
61
62 static byte bgp_mandatory_attrs[] = { BA_ORIGIN, BA_AS_PATH
63 #ifndef IPV6
64 ,BA_NEXT_HOP
65 #endif
66 };
67
68 struct attr_desc {
69 char *name;
70 int expected_length;
71 int expected_flags;
72 int type;
73 int allow_in_ebgp;
74 int (*validate)(struct bgp_proto *p, byte *attr, int len);
75 void (*format)(eattr *ea, byte *buf, int buflen);
76 };
77
78 #define IGNORE -1
79 #define WITHDRAW -2
80
81 static int
82 bgp_check_origin(struct bgp_proto *p UNUSED, byte *a, int len UNUSED)
83 {
84 if (*a > 2)
85 return 6;
86 return 0;
87 }
88
89 static void
90 bgp_format_origin(eattr *a, byte *buf, int buflen UNUSED)
91 {
92 static char *bgp_origin_names[] = { "IGP", "EGP", "Incomplete" };
93
94 bsprintf(buf, bgp_origin_names[a->u.data]);
95 }
96
97 static int
98 path_segment_contains(byte *p, int bs, u32 asn)
99 {
100 int i;
101 int len = p[1];
102 p += 2;
103
104 for(i=0; i<len; i++)
105 {
106 u32 asn2 = (bs == 4) ? get_u32(p) : get_u16(p);
107 if (asn2 == asn)
108 return 1;
109 p += bs;
110 }
111
112 return 0;
113 }
114
115 /* Validates path attribute, removes AS_CONFED_* segments, and also returns path length */
116 static int
117 validate_path(struct bgp_proto *p, int as_path, int bs, byte *idata, uint *ilength)
118 {
119 int res = 0;
120 u8 *a, *dst;
121 int len, plen;
122
123 dst = a = idata;
124 len = *ilength;
125
126 while (len)
127 {
128 if (len < 2)
129 return -1;
130
131 plen = 2 + bs * a[1];
132 if (len < plen)
133 return -1;
134
135 if (a[1] == 0)
136 {
137 log(L_WARN "%s: %s_PATH attribute contains empty segment, skipping it",
138 p->p.name, as_path ? "AS" : "AS4");
139 goto skip;
140 }
141
142 switch (a[0])
143 {
144 case AS_PATH_SET:
145 res++;
146 break;
147
148 case AS_PATH_SEQUENCE:
149 res += a[1];
150 break;
151
152 case AS_PATH_CONFED_SEQUENCE:
153 case AS_PATH_CONFED_SET:
154 if (as_path && path_segment_contains(a, bs, p->remote_as))
155 {
156 log(L_WARN "%s: AS_CONFED_* segment with peer ASN found, misconfigured confederation?", p->p.name);
157 return -1;
158 }
159
160 log(L_WARN "%s: %s_PATH attribute contains AS_CONFED_* segment, skipping segment",
161 p->p.name, as_path ? "AS" : "AS4");
162 goto skip;
163
164 default:
165 return -1;
166 }
167
168 if (dst != a)
169 memmove(dst, a, plen);
170 dst += plen;
171
172 skip:
173 len -= plen;
174 a += plen;
175 }
176
177 *ilength = dst - idata;
178 return res;
179 }
180
181 static inline int
182 validate_as_path(struct bgp_proto *p, byte *a, int *len)
183 {
184 return validate_path(p, 1, p->as4_session ? 4 : 2, a, len);
185 }
186
187 static inline int
188 validate_as4_path(struct bgp_proto *p, struct adata *path)
189 {
190 return validate_path(p, 0, 4, path->data, &path->length);
191 }
192
193 static int
194 bgp_check_next_hop(struct bgp_proto *p UNUSED, byte *a, int len)
195 {
196 #ifdef IPV6
197 return IGNORE;
198 #else
199 ip_addr addr;
200
201 memcpy(&addr, a, len);
202 ipa_ntoh(addr);
203 if (ipa_classify(addr) & IADDR_HOST)
204 return 0;
205 else
206 return 8;
207 #endif
208 }
209
210 static void
211 bgp_format_next_hop(eattr *a, byte *buf, int buflen UNUSED)
212 {
213 ip_addr *ipp = (ip_addr *) a->u.ptr->data;
214 #ifdef IPV6
215 /* in IPv6, we might have two addresses in NEXT HOP */
216 if ((a->u.ptr->length == NEXT_HOP_LENGTH) && ipa_nonzero(ipp[1]))
217 {
218 bsprintf(buf, "%I %I", ipp[0], ipp[1]);
219 return;
220 }
221 #endif
222
223 bsprintf(buf, "%I", ipp[0]);
224 }
225
226 static int
227 bgp_check_aggregator(struct bgp_proto *p, byte *a UNUSED, int len)
228 {
229 int exp_len = p->as4_session ? 8 : 6;
230
231 return (len == exp_len) ? 0 : WITHDRAW;
232 }
233
234 static void
235 bgp_format_aggregator(eattr *a, byte *buf, int buflen UNUSED)
236 {
237 struct adata *ad = a->u.ptr;
238 byte *data = ad->data;
239 u32 as;
240
241 as = get_u32(data);
242 data += 4;
243
244 bsprintf(buf, "%d.%d.%d.%d AS%u", data[0], data[1], data[2], data[3], as);
245 }
246
247 static int
248 bgp_check_community(struct bgp_proto *p UNUSED, byte *a UNUSED, int len)
249 {
250 return ((len % 4) == 0) ? 0 : WITHDRAW;
251 }
252
253 static int
254 bgp_check_cluster_list(struct bgp_proto *p UNUSED, byte *a UNUSED, int len)
255 {
256 return ((len % 4) == 0) ? 0 : 5;
257 }
258
259 static void
260 bgp_format_cluster_list(eattr *a, byte *buf, int buflen)
261 {
262 /* Truncates cluster lists larger than buflen, probably not a problem */
263 int_set_format(a->u.ptr, 0, -1, buf, buflen);
264 }
265
266 static int
267 bgp_check_reach_nlri(struct bgp_proto *p UNUSED, byte *a UNUSED, int len UNUSED)
268 {
269 #ifdef IPV6
270 p->mp_reach_start = a;
271 p->mp_reach_len = len;
272 #endif
273 return IGNORE;
274 }
275
276 static int
277 bgp_check_unreach_nlri(struct bgp_proto *p UNUSED, byte *a UNUSED, int len UNUSED)
278 {
279 #ifdef IPV6
280 p->mp_unreach_start = a;
281 p->mp_unreach_len = len;
282 #endif
283 return IGNORE;
284 }
285
286 static int
287 bgp_check_ext_community(struct bgp_proto *p UNUSED, byte *a UNUSED, int len)
288 {
289 return ((len % 8) == 0) ? 0 : WITHDRAW;
290 }
291
292
293 static struct attr_desc bgp_attr_table[] = {
294 { NULL, -1, 0, 0, 0, /* Undefined */
295 NULL, NULL },
296 { "origin", 1, BAF_TRANSITIVE, EAF_TYPE_INT, 1, /* BA_ORIGIN */
297 bgp_check_origin, bgp_format_origin },
298 { "as_path", -1, BAF_TRANSITIVE, EAF_TYPE_AS_PATH, 1, /* BA_AS_PATH */
299 NULL, NULL }, /* is checked by validate_as_path() as a special case */
300 { "next_hop", 4, BAF_TRANSITIVE, EAF_TYPE_IP_ADDRESS, 1, /* BA_NEXT_HOP */
301 bgp_check_next_hop, bgp_format_next_hop },
302 { "med", 4, BAF_OPTIONAL, EAF_TYPE_INT, 1, /* BA_MULTI_EXIT_DISC */
303 NULL, NULL },
304 { "local_pref", 4, BAF_TRANSITIVE, EAF_TYPE_INT, 0, /* BA_LOCAL_PREF */
305 NULL, NULL },
306 { "atomic_aggr", 0, BAF_TRANSITIVE, EAF_TYPE_OPAQUE, 1, /* BA_ATOMIC_AGGR */
307 NULL, NULL },
308 { "aggregator", -1, BAF_OPTIONAL | BAF_TRANSITIVE, EAF_TYPE_OPAQUE, 1, /* BA_AGGREGATOR */
309 bgp_check_aggregator, bgp_format_aggregator },
310 { "community", -1, BAF_OPTIONAL | BAF_TRANSITIVE, EAF_TYPE_INT_SET, 1, /* BA_COMMUNITY */
311 bgp_check_community, NULL },
312 { "originator_id", 4, BAF_OPTIONAL, EAF_TYPE_ROUTER_ID, 0, /* BA_ORIGINATOR_ID */
313 NULL, NULL },
314 { "cluster_list", -1, BAF_OPTIONAL, EAF_TYPE_INT_SET, 0, /* BA_CLUSTER_LIST */
315 bgp_check_cluster_list, bgp_format_cluster_list },
316 { .name = NULL }, /* BA_DPA */
317 { .name = NULL }, /* BA_ADVERTISER */
318 { .name = NULL }, /* BA_RCID_PATH */
319 { "mp_reach_nlri", -1, BAF_OPTIONAL, EAF_TYPE_OPAQUE, 1, /* BA_MP_REACH_NLRI */
320 bgp_check_reach_nlri, NULL },
321 { "mp_unreach_nlri", -1, BAF_OPTIONAL, EAF_TYPE_OPAQUE, 1, /* BA_MP_UNREACH_NLRI */
322 bgp_check_unreach_nlri, NULL },
323 { "ext_community", -1, BAF_OPTIONAL | BAF_TRANSITIVE, EAF_TYPE_EC_SET, 1, /* BA_EXT_COMMUNITY */
324 bgp_check_ext_community, NULL },
325 { "as4_path", -1, BAF_OPTIONAL | BAF_TRANSITIVE, EAF_TYPE_OPAQUE, 1, /* BA_AS4_PATH */
326 NULL, NULL },
327 { "as4_aggregator", -1, BAF_OPTIONAL | BAF_TRANSITIVE, EAF_TYPE_OPAQUE, 1, /* BA_AS4_PATH */
328 NULL, NULL }
329 };
330
331 /* BA_AS4_PATH is type EAF_TYPE_OPAQUE and not type EAF_TYPE_AS_PATH.
332 * It does not matter as this attribute does not appear on routes in the routing table.
333 */
334
335 #define ATTR_KNOWN(code) ((code) < ARRAY_SIZE(bgp_attr_table) && bgp_attr_table[code].name)
336
337 static inline struct adata *
338 bgp_alloc_adata(struct linpool *pool, unsigned len)
339 {
340 struct adata *ad = lp_alloc(pool, sizeof(struct adata) + len);
341 ad->length = len;
342 return ad;
343 }
344
345 static void
346 bgp_set_attr(eattr *e, unsigned attr, uintptr_t val)
347 {
348 ASSERT(ATTR_KNOWN(attr));
349 e->id = EA_CODE(EAP_BGP, attr);
350 e->type = bgp_attr_table[attr].type;
351 e->flags = bgp_attr_table[attr].expected_flags;
352 if (e->type & EAF_EMBEDDED)
353 e->u.data = val;
354 else
355 e->u.ptr = (struct adata *) val;
356 }
357
358 static byte *
359 bgp_set_attr_wa(eattr *e, struct linpool *pool, unsigned attr, unsigned len)
360 {
361 struct adata *ad = bgp_alloc_adata(pool, len);
362 bgp_set_attr(e, attr, (uintptr_t) ad);
363 return ad->data;
364 }
365
366 void
367 bgp_attach_attr(ea_list **to, struct linpool *pool, unsigned attr, uintptr_t val)
368 {
369 ea_list *a = lp_alloc(pool, sizeof(ea_list) + sizeof(eattr));
370 a->next = *to;
371 *to = a;
372 a->flags = EALF_SORTED;
373 a->count = 1;
374 bgp_set_attr(a->attrs, attr, val);
375 }
376
377 byte *
378 bgp_attach_attr_wa(ea_list **to, struct linpool *pool, unsigned attr, unsigned len)
379 {
380 struct adata *ad = bgp_alloc_adata(pool, len);
381 bgp_attach_attr(to, pool, attr, (uintptr_t) ad);
382 return ad->data;
383 }
384
385 static int
386 bgp_encode_attr_hdr(byte *dst, uint flags, unsigned code, int len)
387 {
388 int wlen;
389
390 DBG("\tAttribute %02x (%d bytes, flags %02x)\n", code, len, flags);
391
392 if (len < 256)
393 {
394 *dst++ = flags;
395 *dst++ = code;
396 *dst++ = len;
397 wlen = 3;
398 }
399 else
400 {
401 *dst++ = flags | BAF_EXT_LEN;
402 *dst++ = code;
403 put_u16(dst, len);
404 wlen = 4;
405 }
406
407 return wlen;
408 }
409
410 static void
411 aggregator_convert_to_old(struct adata *aggr, byte *dst, int *new_used)
412 {
413 byte *src = aggr->data;
414 *new_used = 0;
415
416 u32 as = get_u32(src);
417 if (as > 0xFFFF)
418 {
419 as = AS_TRANS;
420 *new_used = 1;
421 }
422 put_u16(dst, as);
423
424 /* Copy IPv4 address */
425 memcpy(dst + 2, src + 4, 4);
426 }
427
428 static void
429 aggregator_convert_to_new(struct adata *aggr, byte *dst)
430 {
431 byte *src = aggr->data;
432
433 u32 as = get_u16(src);
434 put_u32(dst, as);
435
436 /* Copy IPv4 address */
437 memcpy(dst + 4, src + 2, 4);
438 }
439
440 static int
441 bgp_get_attr_len(eattr *a)
442 {
443 int len;
444 if (ATTR_KNOWN(EA_ID(a->id)))
445 {
446 int code = EA_ID(a->id);
447 struct attr_desc *desc = &bgp_attr_table[code];
448 len = desc->expected_length;
449 if (len < 0)
450 {
451 ASSERT(!(a->type & EAF_EMBEDDED));
452 len = a->u.ptr->length;
453 }
454 }
455 else
456 {
457 ASSERT((a->type & EAF_TYPE_MASK) == EAF_TYPE_OPAQUE);
458 len = a->u.ptr->length;
459 }
460
461 return len;
462 }
463
464 #define ADVANCE(w, r, l) do { r -= l; w += l; } while (0)
465
466 /**
467 * bgp_encode_attrs - encode BGP attributes
468 * @p: BGP instance
469 * @w: buffer
470 * @attrs: a list of extended attributes
471 * @remains: remaining space in the buffer
472 *
473 * The bgp_encode_attrs() function takes a list of extended attributes
474 * and converts it to its BGP representation (a part of an Update message).
475 *
476 * Result: Length of the attribute block generated or -1 if not enough space.
477 */
478 uint
479 bgp_encode_attrs(struct bgp_proto *p, byte *w, ea_list *attrs, int remains)
480 {
481 uint i, code, type, flags;
482 byte *start = w;
483 int len, rv;
484
485 for(i=0; i<attrs->count; i++)
486 {
487 eattr *a = &attrs->attrs[i];
488 ASSERT(EA_PROTO(a->id) == EAP_BGP);
489 code = EA_ID(a->id);
490
491 #ifdef IPV6
492 /* When talking multiprotocol BGP, the NEXT_HOP attributes are used only temporarily. */
493 if (code == BA_NEXT_HOP)
494 continue;
495 #endif
496
497 /* When AS4-aware BGP speaker is talking to non-AS4-aware BGP speaker,
498 * we have to convert our 4B AS_PATH to 2B AS_PATH and send our AS_PATH
499 * as optional AS4_PATH attribute.
500 */
501 if ((code == BA_AS_PATH) && (! p->as4_session))
502 {
503 len = a->u.ptr->length;
504
505 if (remains < (len + 4))
506 goto err_no_buffer;
507
508 /* Using temporary buffer because don't know a length of created attr
509 * and therefore a length of a header. Perhaps i should better always
510 * use BAF_EXT_LEN. */
511
512 byte buf[len];
513 int new_used;
514 int nl = as_path_convert_to_old(a->u.ptr, buf, &new_used);
515
516 DBG("BGP: Encoding old AS_PATH\n");
517 rv = bgp_encode_attr_hdr(w, BAF_TRANSITIVE, BA_AS_PATH, nl);
518 ADVANCE(w, remains, rv);
519 memcpy(w, buf, nl);
520 ADVANCE(w, remains, nl);
521
522 if (! new_used)
523 continue;
524
525 if (remains < (len + 4))
526 goto err_no_buffer;
527
528 /* We should discard AS_CONFED_SEQUENCE or AS_CONFED_SET path segments
529 * here but we don't support confederations and such paths we already
530 * discarded in bgp_check_as_path().
531 */
532
533 DBG("BGP: Encoding AS4_PATH\n");
534 rv = bgp_encode_attr_hdr(w, BAF_OPTIONAL | BAF_TRANSITIVE, BA_AS4_PATH, len);
535 ADVANCE(w, remains, rv);
536 memcpy(w, a->u.ptr->data, len);
537 ADVANCE(w, remains, len);
538
539 continue;
540 }
541
542 /* The same issue with AGGREGATOR attribute */
543 if ((code == BA_AGGREGATOR) && (! p->as4_session))
544 {
545 int new_used;
546
547 len = 6;
548 if (remains < (len + 3))
549 goto err_no_buffer;
550
551 rv = bgp_encode_attr_hdr(w, BAF_OPTIONAL | BAF_TRANSITIVE, BA_AGGREGATOR, len);
552 ADVANCE(w, remains, rv);
553 aggregator_convert_to_old(a->u.ptr, w, &new_used);
554 ADVANCE(w, remains, len);
555
556 if (! new_used)
557 continue;
558
559 len = 8;
560 if (remains < (len + 3))
561 goto err_no_buffer;
562
563 rv = bgp_encode_attr_hdr(w, BAF_OPTIONAL | BAF_TRANSITIVE, BA_AS4_AGGREGATOR, len);
564 ADVANCE(w, remains, rv);
565 memcpy(w, a->u.ptr->data, len);
566 ADVANCE(w, remains, len);
567
568 continue;
569 }
570
571 /* Standard path continues here ... */
572
573 type = a->type & EAF_TYPE_MASK;
574 flags = a->flags & (BAF_OPTIONAL | BAF_TRANSITIVE | BAF_PARTIAL);
575 len = bgp_get_attr_len(a);
576
577 /* Skip empty sets */
578 if (((type == EAF_TYPE_INT_SET) || (type == EAF_TYPE_EC_SET)) && (len == 0))
579 continue;
580
581 if (remains < len + 4)
582 goto err_no_buffer;
583
584 rv = bgp_encode_attr_hdr(w, flags, code, len);
585 ADVANCE(w, remains, rv);
586
587 switch (type)
588 {
589 case EAF_TYPE_INT:
590 case EAF_TYPE_ROUTER_ID:
591 if (len == 4)
592 put_u32(w, a->u.data);
593 else
594 *w = a->u.data;
595 break;
596 case EAF_TYPE_IP_ADDRESS:
597 {
598 ip_addr ip = *(ip_addr *)a->u.ptr->data;
599 ipa_hton(ip);
600 memcpy(w, &ip, len);
601 break;
602 }
603 case EAF_TYPE_INT_SET:
604 case EAF_TYPE_EC_SET:
605 {
606 u32 *z = int_set_get_data(a->u.ptr);
607 int i;
608 for(i=0; i<len; i+=4)
609 put_u32(w+i, *z++);
610 break;
611 }
612 case EAF_TYPE_OPAQUE:
613 case EAF_TYPE_AS_PATH:
614 memcpy(w, a->u.ptr->data, len);
615 break;
616 default:
617 bug("bgp_encode_attrs: unknown attribute type %02x", a->type);
618 }
619 ADVANCE(w, remains, len);
620 }
621 return w - start;
622
623 err_no_buffer:
624 return -1;
625 }
626
627 /*
628 static void
629 bgp_init_prefix(struct fib_node *N)
630 {
631 struct bgp_prefix *p = (struct bgp_prefix *) N;
632 p->bucket_node.next = NULL;
633 }
634 */
635
636 static int
637 bgp_compare_u32(const u32 *x, const u32 *y)
638 {
639 return (*x < *y) ? -1 : (*x > *y) ? 1 : 0;
640 }
641
642 static inline void
643 bgp_normalize_int_set(u32 *dest, u32 *src, unsigned cnt)
644 {
645 memcpy(dest, src, sizeof(u32) * cnt);
646 qsort(dest, cnt, sizeof(u32), (int(*)(const void *, const void *)) bgp_compare_u32);
647 }
648
649 static int
650 bgp_compare_ec(const u32 *xp, const u32 *yp)
651 {
652 u64 x = ec_get(xp, 0);
653 u64 y = ec_get(yp, 0);
654 return (x < y) ? -1 : (x > y) ? 1 : 0;
655 }
656
657 static inline void
658 bgp_normalize_ec_set(struct adata *ad, u32 *src, int internal)
659 {
660 u32 *dst = int_set_get_data(ad);
661
662 /* Remove non-transitive communities (EC_TBIT active) on external sessions */
663 if (! internal)
664 {
665 int len = int_set_get_size(ad);
666 u32 *t = dst;
667 int i;
668
669 for (i=0; i < len; i += 2)
670 {
671 if (src[i] & EC_TBIT)
672 continue;
673
674 *t++ = src[i];
675 *t++ = src[i+1];
676 }
677
678 ad->length = (t - dst) * 4;
679 }
680 else
681 memcpy(dst, src, ad->length);
682
683 qsort(dst, ad->length / 8, 8, (int(*)(const void *, const void *)) bgp_compare_ec);
684 }
685
686 static void
687 bgp_rehash_buckets(struct bgp_proto *p)
688 {
689 struct bgp_bucket **old = p->bucket_hash;
690 struct bgp_bucket **new;
691 unsigned oldn = p->hash_size;
692 unsigned i, e, mask;
693 struct bgp_bucket *b;
694
695 p->hash_size = p->hash_limit;
696 DBG("BGP: Rehashing bucket table from %d to %d\n", oldn, p->hash_size);
697 p->hash_limit *= 4;
698 if (p->hash_limit >= 65536)
699 p->hash_limit = ~0;
700 new = p->bucket_hash = mb_allocz(p->p.pool, p->hash_size * sizeof(struct bgp_bucket *));
701 mask = p->hash_size - 1;
702 for (i=0; i<oldn; i++)
703 while (b = old[i])
704 {
705 old[i] = b->hash_next;
706 e = b->hash & mask;
707 b->hash_next = new[e];
708 if (b->hash_next)
709 b->hash_next->hash_prev = b;
710 b->hash_prev = NULL;
711 new[e] = b;
712 }
713 mb_free(old);
714 }
715
716 static struct bgp_bucket *
717 bgp_new_bucket(struct bgp_proto *p, ea_list *new, unsigned hash)
718 {
719 struct bgp_bucket *b;
720 unsigned ea_size = sizeof(ea_list) + new->count * sizeof(eattr);
721 unsigned ea_size_aligned = BIRD_ALIGN(ea_size, CPU_STRUCT_ALIGN);
722 unsigned size = sizeof(struct bgp_bucket) + ea_size_aligned;
723 unsigned i;
724 byte *dest;
725 unsigned index = hash & (p->hash_size - 1);
726
727 /* Gather total size of non-inline attributes */
728 for (i=0; i<new->count; i++)
729 {
730 eattr *a = &new->attrs[i];
731 if (!(a->type & EAF_EMBEDDED))
732 size += BIRD_ALIGN(sizeof(struct adata) + a->u.ptr->length, CPU_STRUCT_ALIGN);
733 }
734
735 /* Create the bucket and hash it */
736 b = mb_alloc(p->p.pool, size);
737 b->hash_next = p->bucket_hash[index];
738 if (b->hash_next)
739 b->hash_next->hash_prev = b;
740 p->bucket_hash[index] = b;
741 b->hash_prev = NULL;
742 b->hash = hash;
743 add_tail(&p->bucket_queue, &b->send_node);
744 init_list(&b->prefixes);
745 memcpy(b->eattrs, new, ea_size);
746 dest = ((byte *)b->eattrs) + ea_size_aligned;
747
748 /* Copy values of non-inline attributes */
749 for (i=0; i<new->count; i++)
750 {
751 eattr *a = &b->eattrs->attrs[i];
752 if (!(a->type & EAF_EMBEDDED))
753 {
754 struct adata *oa = a->u.ptr;
755 struct adata *na = (struct adata *) dest;
756 memcpy(na, oa, sizeof(struct adata) + oa->length);
757 a->u.ptr = na;
758 dest += BIRD_ALIGN(sizeof(struct adata) + na->length, CPU_STRUCT_ALIGN);
759 }
760 }
761
762 /* If needed, rehash */
763 p->hash_count++;
764 if (p->hash_count > p->hash_limit)
765 bgp_rehash_buckets(p);
766
767 return b;
768 }
769
770 static struct bgp_bucket *
771 bgp_get_bucket(struct bgp_proto *p, net *n, ea_list *attrs, int originate)
772 {
773 ea_list *new;
774 unsigned i, cnt, hash, code;
775 eattr *a, *d;
776 u32 seen = 0;
777 struct bgp_bucket *b;
778
779 /* Merge the attribute list */
780 new = alloca(ea_scan(attrs));
781 ea_merge(attrs, new);
782 ea_sort(new);
783
784 /* Normalize attributes */
785 d = new->attrs;
786 cnt = new->count;
787 new->count = 0;
788 for(i=0; i<cnt; i++)
789 {
790 a = &new->attrs[i];
791 if (EA_PROTO(a->id) != EAP_BGP)
792 continue;
793 code = EA_ID(a->id);
794 if (ATTR_KNOWN(code))
795 {
796 if (!bgp_attr_table[code].allow_in_ebgp && !p->is_internal)
797 continue;
798 /* The flags might have been zero if the attr was added by filters */
799 a->flags = (a->flags & BAF_PARTIAL) | bgp_attr_table[code].expected_flags;
800 if (code < 32)
801 seen |= 1 << code;
802 }
803 else
804 {
805 /* Don't re-export unknown non-transitive attributes */
806 if (!(a->flags & BAF_TRANSITIVE))
807 continue;
808 }
809 *d = *a;
810 if ((d->type & EAF_ORIGINATED) && !originate && (d->flags & BAF_TRANSITIVE) && (d->flags & BAF_OPTIONAL))
811 d->flags |= BAF_PARTIAL;
812 switch (d->type & EAF_TYPE_MASK)
813 {
814 case EAF_TYPE_INT_SET:
815 {
816 struct adata *z = alloca(sizeof(struct adata) + d->u.ptr->length);
817 z->length = d->u.ptr->length;
818 bgp_normalize_int_set((u32 *) z->data, (u32 *) d->u.ptr->data, z->length / 4);
819 d->u.ptr = z;
820 break;
821 }
822 case EAF_TYPE_EC_SET:
823 {
824 struct adata *z = alloca(sizeof(struct adata) + d->u.ptr->length);
825 z->length = d->u.ptr->length;
826 bgp_normalize_ec_set(z, (u32 *) d->u.ptr->data, p->is_internal);
827 d->u.ptr = z;
828 break;
829 }
830 default: ;
831 }
832 d++;
833 new->count++;
834 }
835
836 /* Hash */
837 hash = ea_hash(new);
838 for(b=p->bucket_hash[hash & (p->hash_size - 1)]; b; b=b->hash_next)
839 if (b->hash == hash && ea_same(b->eattrs, new))
840 {
841 DBG("Found bucket.\n");
842 return b;
843 }
844
845 /* Ensure that there are all mandatory attributes */
846 for(i=0; i<ARRAY_SIZE(bgp_mandatory_attrs); i++)
847 if (!(seen & (1 << bgp_mandatory_attrs[i])))
848 {
849 log(L_ERR "%s: Mandatory attribute %s missing in route %I/%d", p->p.name, bgp_attr_table[bgp_mandatory_attrs[i]].name, n->n.prefix, n->n.pxlen);
850 return NULL;
851 }
852
853 /* Check if next hop is valid */
854 a = ea_find(new, EA_CODE(EAP_BGP, BA_NEXT_HOP));
855 if (!a || ipa_equal(p->cf->remote_ip, *(ip_addr *)a->u.ptr->data))
856 {
857 log(L_ERR "%s: Invalid NEXT_HOP attribute in route %I/%d", p->p.name, n->n.prefix, n->n.pxlen);
858 return NULL;
859 }
860
861 /* Create new bucket */
862 DBG("Creating bucket.\n");
863 return bgp_new_bucket(p, new, hash);
864 }
865
866 void
867 bgp_free_bucket(struct bgp_proto *p, struct bgp_bucket *buck)
868 {
869 if (buck->hash_next)
870 buck->hash_next->hash_prev = buck->hash_prev;
871 if (buck->hash_prev)
872 buck->hash_prev->hash_next = buck->hash_next;
873 else
874 p->bucket_hash[buck->hash & (p->hash_size-1)] = buck->hash_next;
875 mb_free(buck);
876 }
877
878
879 /* Prefix hash table */
880
881 #define PXH_KEY(n1) n1->n.prefix, n1->n.pxlen, n1->path_id
882 #define PXH_NEXT(n) n->next
883 #define PXH_EQ(p1,l1,i1,p2,l2,i2) ipa_equal(p1, p2) && l1 == l2 && i1 == i2
884 #define PXH_FN(p,l,i) ipa_hash32(p) ^ u32_hash((l << 16) ^ i)
885
886 #define PXH_REHASH bgp_pxh_rehash
887 #define PXH_PARAMS /8, *2, 2, 2, 8, 20
888
889
890 HASH_DEFINE_REHASH_FN(PXH, struct bgp_prefix)
891
892 void
893 bgp_init_prefix_table(struct bgp_proto *p, u32 order)
894 {
895 HASH_INIT(p->prefix_hash, p->p.pool, order);
896
897 p->prefix_slab = sl_new(p->p.pool, sizeof(struct bgp_prefix));
898 }
899
900 static struct bgp_prefix *
901 bgp_get_prefix(struct bgp_proto *p, ip_addr prefix, int pxlen, u32 path_id)
902 {
903 struct bgp_prefix *bp = HASH_FIND(p->prefix_hash, PXH, prefix, pxlen, path_id);
904
905 if (bp)
906 return bp;
907
908 bp = sl_alloc(p->prefix_slab);
909 bp->n.prefix = prefix;
910 bp->n.pxlen = pxlen;
911 bp->path_id = path_id;
912 bp->bucket_node.next = NULL;
913
914 HASH_INSERT2(p->prefix_hash, PXH, p->p.pool, bp);
915
916 return bp;
917 }
918
919 void
920 bgp_free_prefix(struct bgp_proto *p, struct bgp_prefix *bp)
921 {
922 HASH_REMOVE2(p->prefix_hash, PXH, p->p.pool, bp);
923 sl_free(p->prefix_slab, bp);
924 }
925
926
927 void
928 bgp_rt_notify(struct proto *P, rtable *tbl UNUSED, net *n, rte *new, rte *old UNUSED, ea_list *attrs)
929 {
930 struct bgp_proto *p = (struct bgp_proto *) P;
931 struct bgp_bucket *buck;
932 struct bgp_prefix *px;
933 rte *key;
934 u32 path_id;
935
936 DBG("BGP: Got route %I/%d %s\n", n->n.prefix, n->n.pxlen, new ? "up" : "down");
937
938 if (new)
939 {
940 key = new;
941 buck = bgp_get_bucket(p, n, attrs, new->attrs->source != RTS_BGP);
942 if (!buck) /* Inconsistent attribute list */
943 return;
944 }
945 else
946 {
947 key = old;
948 if (!(buck = p->withdraw_bucket))
949 {
950 buck = p->withdraw_bucket = mb_alloc(P->pool, sizeof(struct bgp_bucket));
951 init_list(&buck->prefixes);
952 }
953 }
954 path_id = p->add_path_tx ? key->attrs->src->global_id : 0;
955 px = bgp_get_prefix(p, n->n.prefix, n->n.pxlen, path_id);
956 if (px->bucket_node.next)
957 {
958 DBG("\tRemoving old entry.\n");
959 rem_node(&px->bucket_node);
960 }
961 add_tail(&buck->prefixes, &px->bucket_node);
962 bgp_schedule_packet(p->conn, PKT_UPDATE);
963 }
964
965 static int
966 bgp_create_attrs(struct bgp_proto *p, rte *e, ea_list **attrs, struct linpool *pool)
967 {
968 ea_list *ea = lp_alloc(pool, sizeof(ea_list) + 4*sizeof(eattr));
969 rta *rta = e->attrs;
970 byte *z;
971
972 ea->next = *attrs;
973 *attrs = ea;
974 ea->flags = EALF_SORTED;
975 ea->count = 4;
976
977 bgp_set_attr(ea->attrs, BA_ORIGIN,
978 ((rta->source == RTS_OSPF_EXT1) || (rta->source == RTS_OSPF_EXT2)) ? ORIGIN_INCOMPLETE : ORIGIN_IGP);
979
980 if (p->is_internal)
981 bgp_set_attr_wa(ea->attrs+1, pool, BA_AS_PATH, 0);
982 else
983 {
984 z = bgp_set_attr_wa(ea->attrs+1, pool, BA_AS_PATH, 6);
985 z[0] = AS_PATH_SEQUENCE;
986 z[1] = 1; /* 1 AS */
987 put_u32(z+2, p->local_as);
988 }
989
990 /* iBGP -> use gw, eBGP multi-hop -> use source_addr,
991 eBGP single-hop -> use gw if on the same iface */
992 z = bgp_set_attr_wa(ea->attrs+2, pool, BA_NEXT_HOP, NEXT_HOP_LENGTH);
993 if (p->cf->next_hop_self ||
994 rta->dest != RTD_ROUTER ||
995 ipa_equal(rta->gw, IPA_NONE) ||
996 ipa_is_link_local(rta->gw) ||
997 (!p->is_internal && !p->cf->next_hop_keep &&
998 (!p->neigh || (rta->iface != p->neigh->iface))))
999 set_next_hop(z, p->source_addr);
1000 else
1001 set_next_hop(z, rta->gw);
1002
1003 bgp_set_attr(ea->attrs+3, BA_LOCAL_PREF, p->cf->default_local_pref);
1004
1005 return 0; /* Leave decision to the filters */
1006 }
1007
1008
1009 static inline int
1010 bgp_as_path_loopy(struct bgp_proto *p, rta *a)
1011 {
1012 int num = p->cf->allow_local_as + 1;
1013 eattr *e = ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1014 return (e && (num > 0) && as_path_contains(e->u.ptr, p->local_as, num));
1015 }
1016
1017 static inline int
1018 bgp_originator_id_loopy(struct bgp_proto *p, rta *a)
1019 {
1020 eattr *e = ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
1021 return (e && (e->u.data == p->local_id));
1022 }
1023
1024 static inline int
1025 bgp_cluster_list_loopy(struct bgp_proto *p, rta *a)
1026 {
1027 eattr *e = ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
1028 return (e && p->rr_client && int_set_contains(e->u.ptr, p->rr_cluster_id));
1029 }
1030
1031
1032 static inline void
1033 bgp_path_prepend(rte *e, ea_list **attrs, struct linpool *pool, u32 as)
1034 {
1035 eattr *a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1036 bgp_attach_attr(attrs, pool, BA_AS_PATH, (uintptr_t) as_path_prepend(pool, a->u.ptr, as));
1037 }
1038
1039 static inline void
1040 bgp_cluster_list_prepend(rte *e, ea_list **attrs, struct linpool *pool, u32 cid)
1041 {
1042 eattr *a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
1043 bgp_attach_attr(attrs, pool, BA_CLUSTER_LIST, (uintptr_t) int_set_add(pool, a ? a->u.ptr : NULL, cid));
1044 }
1045
1046 static int
1047 bgp_update_attrs(struct bgp_proto *p, rte *e, ea_list **attrs, struct linpool *pool, int rr)
1048 {
1049 eattr *a;
1050
1051 if (!p->is_internal && !p->rs_client)
1052 {
1053 bgp_path_prepend(e, attrs, pool, p->local_as);
1054
1055 /* The MULTI_EXIT_DISC attribute received from a neighboring AS MUST NOT be
1056 * propagated to other neighboring ASes.
1057 * Perhaps it would be better to undefine it.
1058 */
1059 a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
1060 if (a)
1061 bgp_attach_attr(attrs, pool, BA_MULTI_EXIT_DISC, 0);
1062 }
1063
1064 /* iBGP -> keep next_hop, eBGP multi-hop -> use source_addr,
1065 * eBGP single-hop -> keep next_hop if on the same iface.
1066 * If the next_hop is zero (i.e. link-local), keep only if on the same iface.
1067 *
1068 * Note that same-iface-check uses iface from route, which is based on gw.
1069 */
1070 a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_NEXT_HOP));
1071 if (a && !p->cf->next_hop_self &&
1072 (p->cf->next_hop_keep ||
1073 (p->is_internal && ipa_nonzero(*((ip_addr *) a->u.ptr->data))) ||
1074 (p->neigh && (e->attrs->iface == p->neigh->iface))))
1075 {
1076 /* Leave the original next hop attribute, will check later where does it point */
1077 }
1078 else
1079 {
1080 /* Need to create new one */
1081 byte *b = bgp_attach_attr_wa(attrs, pool, BA_NEXT_HOP, NEXT_HOP_LENGTH);
1082 set_next_hop(b, p->source_addr);
1083 }
1084
1085 if (rr)
1086 {
1087 /* Handling route reflection, RFC 4456 */
1088 struct bgp_proto *src = (struct bgp_proto *) e->attrs->src->proto;
1089
1090 a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
1091 if (!a)
1092 bgp_attach_attr(attrs, pool, BA_ORIGINATOR_ID, src->remote_id);
1093
1094 /* We attach proper cluster ID according to whether the route is entering or leaving the cluster */
1095 bgp_cluster_list_prepend(e, attrs, pool, src->rr_client ? src->rr_cluster_id : p->rr_cluster_id);
1096
1097 /* Two RR clients with different cluster ID, hmmm */
1098 if (src->rr_client && p->rr_client && (src->rr_cluster_id != p->rr_cluster_id))
1099 bgp_cluster_list_prepend(e, attrs, pool, p->rr_cluster_id);
1100 }
1101
1102 return 0; /* Leave decision to the filters */
1103 }
1104
1105 static int
1106 bgp_community_filter(struct bgp_proto *p, rte *e)
1107 {
1108 eattr *a;
1109 struct adata *d;
1110
1111 /* Check if we aren't forbidden to export the route by communities */
1112 a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_COMMUNITY));
1113 if (a)
1114 {
1115 d = a->u.ptr;
1116 if (int_set_contains(d, BGP_COMM_NO_ADVERTISE))
1117 {
1118 DBG("\tNO_ADVERTISE\n");
1119 return 1;
1120 }
1121 if (!p->is_internal &&
1122 (int_set_contains(d, BGP_COMM_NO_EXPORT) ||
1123 int_set_contains(d, BGP_COMM_NO_EXPORT_SUBCONFED)))
1124 {
1125 DBG("\tNO_EXPORT\n");
1126 return 1;
1127 }
1128 }
1129
1130 return 0;
1131 }
1132
1133 int
1134 bgp_import_control(struct proto *P, rte **new, ea_list **attrs, struct linpool *pool)
1135 {
1136 rte *e = *new;
1137 struct bgp_proto *p = (struct bgp_proto *) P;
1138 struct bgp_proto *new_bgp = (e->attrs->src->proto->proto == &proto_bgp) ?
1139 (struct bgp_proto *) e->attrs->src->proto : NULL;
1140
1141 if (p == new_bgp) /* Poison reverse updates */
1142 return -1;
1143 if (new_bgp)
1144 {
1145 /* We should check here for cluster list loop, because the receiving BGP instance
1146 might have different cluster ID */
1147 if (bgp_cluster_list_loopy(p, e->attrs))
1148 return -1;
1149
1150 if (p->cf->interpret_communities && bgp_community_filter(p, e))
1151 return -1;
1152
1153 if (p->local_as == new_bgp->local_as && p->is_internal && new_bgp->is_internal)
1154 {
1155 /* Redistribution of internal routes with IBGP */
1156 if (p->rr_client || new_bgp->rr_client)
1157 /* Route reflection, RFC 4456 */
1158 return bgp_update_attrs(p, e, attrs, pool, 1);
1159 else
1160 return -1;
1161 }
1162 else
1163 return bgp_update_attrs(p, e, attrs, pool, 0);
1164 }
1165 else
1166 return bgp_create_attrs(p, e, attrs, pool);
1167 }
1168
1169 static inline u32
1170 bgp_get_neighbor(rte *r)
1171 {
1172 eattr *e = ea_find(r->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1173 u32 as;
1174
1175 if (e && as_path_get_first(e->u.ptr, &as))
1176 return as;
1177 else
1178 return ((struct bgp_proto *) r->attrs->src->proto)->remote_as;
1179 }
1180
1181 static inline int
1182 rte_resolvable(rte *rt)
1183 {
1184 int rd = rt->attrs->dest;
1185 return (rd == RTD_ROUTER) || (rd == RTD_DEVICE) || (rd == RTD_MULTIPATH);
1186 }
1187
1188 int
1189 bgp_rte_better(rte *new, rte *old)
1190 {
1191 struct bgp_proto *new_bgp = (struct bgp_proto *) new->attrs->src->proto;
1192 struct bgp_proto *old_bgp = (struct bgp_proto *) old->attrs->src->proto;
1193 eattr *x, *y;
1194 u32 n, o;
1195
1196 /* Skip suppressed routes (see bgp_rte_recalculate()) */
1197 n = new->u.bgp.suppressed;
1198 o = old->u.bgp.suppressed;
1199 if (n > o)
1200 return 0;
1201 if (n < o)
1202 return 1;
1203
1204 /* RFC 4271 9.1.2.1. Route resolvability test */
1205 n = rte_resolvable(new);
1206 o = rte_resolvable(old);
1207 if (n > o)
1208 return 1;
1209 if (n < o)
1210 return 0;
1211
1212 /* Start with local preferences */
1213 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_LOCAL_PREF));
1214 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_LOCAL_PREF));
1215 n = x ? x->u.data : new_bgp->cf->default_local_pref;
1216 o = y ? y->u.data : old_bgp->cf->default_local_pref;
1217 if (n > o)
1218 return 1;
1219 if (n < o)
1220 return 0;
1221
1222 /* RFC 4271 9.1.2.2. a) Use AS path lengths */
1223 if (new_bgp->cf->compare_path_lengths || old_bgp->cf->compare_path_lengths)
1224 {
1225 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1226 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1227 n = x ? as_path_getlen(x->u.ptr) : AS_PATH_MAXLEN;
1228 o = y ? as_path_getlen(y->u.ptr) : AS_PATH_MAXLEN;
1229 if (n < o)
1230 return 1;
1231 if (n > o)
1232 return 0;
1233 }
1234
1235 /* RFC 4271 9.1.2.2. b) Use origins */
1236 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1237 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1238 n = x ? x->u.data : ORIGIN_INCOMPLETE;
1239 o = y ? y->u.data : ORIGIN_INCOMPLETE;
1240 if (n < o)
1241 return 1;
1242 if (n > o)
1243 return 0;
1244
1245 /* RFC 4271 9.1.2.2. c) Compare MED's */
1246 /* Proper RFC 4271 path selection cannot be interpreted as finding
1247 * the best path in some ordering. It is implemented partially in
1248 * bgp_rte_recalculate() when deterministic_med option is
1249 * active. Without that option, the behavior is just an
1250 * approximation, which in specific situations may lead to
1251 * persistent routing loops, because it is nondeterministic - it
1252 * depends on the order in which routes appeared. But it is also the
1253 * same behavior as used by default in Cisco routers, so it is
1254 * probably not a big issue.
1255 */
1256 if (new_bgp->cf->med_metric || old_bgp->cf->med_metric ||
1257 (bgp_get_neighbor(new) == bgp_get_neighbor(old)))
1258 {
1259 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
1260 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
1261 n = x ? x->u.data : new_bgp->cf->default_med;
1262 o = y ? y->u.data : old_bgp->cf->default_med;
1263 if (n < o)
1264 return 1;
1265 if (n > o)
1266 return 0;
1267 }
1268
1269 /* RFC 4271 9.1.2.2. d) Prefer external peers */
1270 if (new_bgp->is_internal > old_bgp->is_internal)
1271 return 0;
1272 if (new_bgp->is_internal < old_bgp->is_internal)
1273 return 1;
1274
1275 /* RFC 4271 9.1.2.2. e) Compare IGP metrics */
1276 n = new_bgp->cf->igp_metric ? new->attrs->igp_metric : 0;
1277 o = old_bgp->cf->igp_metric ? old->attrs->igp_metric : 0;
1278 if (n < o)
1279 return 1;
1280 if (n > o)
1281 return 0;
1282
1283 /* RFC 4271 9.1.2.2. f) Compare BGP identifiers */
1284 /* RFC 4456 9. a) Use ORIGINATOR_ID instead of local neighor ID */
1285 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
1286 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
1287 n = x ? x->u.data : new_bgp->remote_id;
1288 o = y ? y->u.data : old_bgp->remote_id;
1289
1290 /* RFC 5004 - prefer older routes */
1291 /* (if both are external and from different peer) */
1292 if ((new_bgp->cf->prefer_older || old_bgp->cf->prefer_older) &&
1293 !new_bgp->is_internal && n != o)
1294 return 0;
1295
1296 /* rest of RFC 4271 9.1.2.2. f) */
1297 if (n < o)
1298 return 1;
1299 if (n > o)
1300 return 0;
1301
1302 /* RFC 4456 9. b) Compare cluster list lengths */
1303 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
1304 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
1305 n = x ? int_set_get_size(x->u.ptr) : 0;
1306 o = y ? int_set_get_size(y->u.ptr) : 0;
1307 if (n < o)
1308 return 1;
1309 if (n > o)
1310 return 0;
1311
1312 /* RFC 4271 9.1.2.2. g) Compare peer IP adresses */
1313 return (ipa_compare(new_bgp->cf->remote_ip, old_bgp->cf->remote_ip) < 0);
1314 }
1315
1316
1317 int
1318 bgp_rte_mergable(rte *pri, rte *sec)
1319 {
1320 struct bgp_proto *pri_bgp = (struct bgp_proto *) pri->attrs->src->proto;
1321 struct bgp_proto *sec_bgp = (struct bgp_proto *) sec->attrs->src->proto;
1322 eattr *x, *y;
1323 u32 p, s;
1324
1325 /* Skip suppressed routes (see bgp_rte_recalculate()) */
1326 if (pri->u.bgp.suppressed != sec->u.bgp.suppressed)
1327 return 0;
1328
1329 /* RFC 4271 9.1.2.1. Route resolvability test */
1330 if (!rte_resolvable(sec))
1331 return 0;
1332
1333 /* Start with local preferences */
1334 x = ea_find(pri->attrs->eattrs, EA_CODE(EAP_BGP, BA_LOCAL_PREF));
1335 y = ea_find(sec->attrs->eattrs, EA_CODE(EAP_BGP, BA_LOCAL_PREF));
1336 p = x ? x->u.data : pri_bgp->cf->default_local_pref;
1337 s = y ? y->u.data : sec_bgp->cf->default_local_pref;
1338 if (p != s)
1339 return 0;
1340
1341 /* RFC 4271 9.1.2.2. a) Use AS path lengths */
1342 if (pri_bgp->cf->compare_path_lengths || sec_bgp->cf->compare_path_lengths)
1343 {
1344 x = ea_find(pri->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1345 y = ea_find(sec->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1346 p = x ? as_path_getlen(x->u.ptr) : AS_PATH_MAXLEN;
1347 s = y ? as_path_getlen(y->u.ptr) : AS_PATH_MAXLEN;
1348
1349 if (p != s)
1350 return 0;
1351
1352 // if (DELTA(p, s) > pri_bgp->cf->relax_multipath)
1353 // return 0;
1354 }
1355
1356 /* RFC 4271 9.1.2.2. b) Use origins */
1357 x = ea_find(pri->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1358 y = ea_find(sec->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1359 p = x ? x->u.data : ORIGIN_INCOMPLETE;
1360 s = y ? y->u.data : ORIGIN_INCOMPLETE;
1361 if (p != s)
1362 return 0;
1363
1364 /* RFC 4271 9.1.2.2. c) Compare MED's */
1365 if (pri_bgp->cf->med_metric || sec_bgp->cf->med_metric ||
1366 (bgp_get_neighbor(pri) == bgp_get_neighbor(sec)))
1367 {
1368 x = ea_find(pri->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
1369 y = ea_find(sec->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
1370 p = x ? x->u.data : pri_bgp->cf->default_med;
1371 s = y ? y->u.data : sec_bgp->cf->default_med;
1372 if (p != s)
1373 return 0;
1374 }
1375
1376 /* RFC 4271 9.1.2.2. d) Prefer external peers */
1377 if (pri_bgp->is_internal != sec_bgp->is_internal)
1378 return 0;
1379
1380 /* RFC 4271 9.1.2.2. e) Compare IGP metrics */
1381 p = pri_bgp->cf->igp_metric ? pri->attrs->igp_metric : 0;
1382 s = sec_bgp->cf->igp_metric ? sec->attrs->igp_metric : 0;
1383 if (p != s)
1384 return 0;
1385
1386 /* Remaining criteria are ignored */
1387
1388 return 1;
1389 }
1390
1391
1392
1393 static inline int
1394 same_group(rte *r, u32 lpref, u32 lasn)
1395 {
1396 return (r->pref == lpref) && (bgp_get_neighbor(r) == lasn);
1397 }
1398
1399 static inline int
1400 use_deterministic_med(rte *r)
1401 {
1402 struct proto *P = r->attrs->src->proto;
1403 return (P->proto == &proto_bgp) && ((struct bgp_proto *) P)->cf->deterministic_med;
1404 }
1405
1406 int
1407 bgp_rte_recalculate(rtable *table, net *net, rte *new, rte *old, rte *old_best)
1408 {
1409 rte *r, *s;
1410 rte *key = new ? new : old;
1411 u32 lpref = key->pref;
1412 u32 lasn = bgp_get_neighbor(key);
1413 int old_is_group_best = 0;
1414
1415 /*
1416 * Proper RFC 4271 path selection is a bit complicated, it cannot be
1417 * implemented just by rte_better(), because it is not a linear
1418 * ordering. But it can be splitted to two levels, where the lower
1419 * level chooses the best routes in each group of routes from the
1420 * same neighboring AS and higher level chooses the best route (with
1421 * a slightly different ordering) between the best-in-group routes.
1422 *
1423 * When deterministic_med is disabled, we just ignore this issue and
1424 * choose the best route by bgp_rte_better() alone. If enabled, the
1425 * lower level of the route selection is done here (for the group
1426 * to which the changed route belongs), all routes in group are
1427 * marked as suppressed, just chosen best-in-group is not.
1428 *
1429 * Global best route selection then implements higher level by
1430 * choosing between non-suppressed routes (as they are always
1431 * preferred over suppressed routes). Routes from BGP protocols
1432 * that do not set deterministic_med are just never suppressed. As
1433 * they do not participate in the lower level selection, it is OK
1434 * that this fn is not called for them.
1435 *
1436 * The idea is simple, the implementation is more problematic,
1437 * mostly because of optimizations in rte_recalculate() that
1438 * avoids full recalculation in most cases.
1439 *
1440 * We can assume that at least one of new, old is non-NULL and both
1441 * are from the same protocol with enabled deterministic_med. We
1442 * group routes by both neighbor AS (lasn) and preference (lpref),
1443 * because bgp_rte_better() does not handle preference itself.
1444 */
1445
1446 /* If new and old are from different groups, we just process that
1447 as two independent events */
1448 if (new && old && !same_group(old, lpref, lasn))
1449 {
1450 int i1, i2;
1451 i1 = bgp_rte_recalculate(table, net, NULL, old, old_best);
1452 i2 = bgp_rte_recalculate(table, net, new, NULL, old_best);
1453 return i1 || i2;
1454 }
1455
1456 /*
1457 * We could find the best-in-group and then make some shortcuts like
1458 * in rte_recalculate, but as we would have to walk through all
1459 * net->routes just to find it, it is probably not worth. So we
1460 * just have two simpler fast cases that use just the old route.
1461 * We also set suppressed flag to avoid using it in bgp_rte_better().
1462 */
1463
1464 if (new)
1465 new->u.bgp.suppressed = 1;
1466
1467 if (old)
1468 {
1469 old_is_group_best = !old->u.bgp.suppressed;
1470 old->u.bgp.suppressed = 1;
1471 int new_is_better = new && bgp_rte_better(new, old);
1472
1473 /* The first case - replace not best with worse (or remove not best) */
1474 if (!old_is_group_best && !new_is_better)
1475 return 0;
1476
1477 /* The second case - replace the best with better */
1478 if (old_is_group_best && new_is_better)
1479 {
1480 /* new is best-in-group, the see discussion below - this is
1481 a special variant of NBG && OBG. From OBG we can deduce
1482 that same_group(old_best) iff (old == old_best) */
1483 new->u.bgp.suppressed = 0;
1484 return (old == old_best);
1485 }
1486 }
1487
1488 /* The default case - find a new best-in-group route */
1489 r = new; /* new may not be in the list */
1490 for (s=net->routes; rte_is_valid(s); s=s->next)
1491 if (use_deterministic_med(s) && same_group(s, lpref, lasn))
1492 {
1493 s->u.bgp.suppressed = 1;
1494 if (!r || bgp_rte_better(s, r))
1495 r = s;
1496 }
1497
1498 /* Simple case - the last route in group disappears */
1499 if (!r)
1500 return 0;
1501
1502 /* Found best-in-group */
1503 r->u.bgp.suppressed = 0;
1504
1505 /*
1506 * There are generally two reasons why we have to force
1507 * recalculation (return 1): First, the new route may be wrongfully
1508 * chosen to be the best in the first case check in
1509 * rte_recalculate(), this may happen only if old_best is from the
1510 * same group. Second, another (different than new route)
1511 * best-in-group is chosen and that may be the proper best (although
1512 * rte_recalculate() without ignore that possibility).
1513 *
1514 * There are three possible cases according to whether the old route
1515 * was the best in group (OBG, stored in old_is_group_best) and
1516 * whether the new route is the best in group (NBG, tested by r == new).
1517 * These cases work even if old or new is NULL.
1518 *
1519 * NBG -> new is a possible candidate for the best route, so we just
1520 * check for the first reason using same_group().
1521 *
1522 * !NBG && OBG -> Second reason applies, return 1
1523 *
1524 * !NBG && !OBG -> Best in group does not change, old != old_best,
1525 * rte_better(new, old_best) is false and therefore
1526 * the first reason does not apply, return 0
1527 */
1528
1529 if (r == new)
1530 return old_best && same_group(old_best, lpref, lasn);
1531 else
1532 return old_is_group_best;
1533 }
1534
1535 static struct adata *
1536 bgp_aggregator_convert_to_new(struct adata *old, struct linpool *pool)
1537 {
1538 struct adata *newa = lp_alloc(pool, sizeof(struct adata) + 8);
1539 newa->length = 8;
1540 aggregator_convert_to_new(old, newa->data);
1541 return newa;
1542 }
1543
1544
1545 /* Take last req_as ASNs from path old2 (in 2B format), convert to 4B format
1546 * and append path old4 (in 4B format).
1547 */
1548 static struct adata *
1549 bgp_merge_as_paths(struct adata *old2, struct adata *old4, int req_as, struct linpool *pool)
1550 {
1551 byte buf[old2->length * 2];
1552
1553 int ol = as_path_convert_to_new(old2, buf, req_as);
1554 int nl = ol + (old4 ? old4->length : 0);
1555
1556 struct adata *newa = lp_alloc(pool, sizeof(struct adata) + nl);
1557 newa->length = nl;
1558 memcpy(newa->data, buf, ol);
1559 if (old4) memcpy(newa->data + ol, old4->data, old4->length);
1560
1561 return newa;
1562 }
1563
1564 static int
1565 as4_aggregator_valid(struct adata *aggr)
1566 {
1567 return aggr->length == 8;
1568 }
1569
1570
1571 /* Reconstruct 4B AS_PATH and AGGREGATOR according to RFC 4893 4.2.3 */
1572 static void
1573 bgp_reconstruct_4b_atts(struct bgp_proto *p, rta *a, struct linpool *pool)
1574 {
1575 eattr *p2 =ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1576 eattr *p4 =ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AS4_PATH));
1577 eattr *a2 =ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AGGREGATOR));
1578 eattr *a4 =ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AS4_AGGREGATOR));
1579 int a4_removed = 0;
1580
1581 if (a4 && !as4_aggregator_valid(a4->u.ptr))
1582 {
1583 log(L_WARN "%s: AS4_AGGREGATOR attribute is invalid, skipping attribute", p->p.name);
1584 a4 = NULL;
1585 a4_removed = 1;
1586 }
1587
1588 if (a2)
1589 {
1590 u32 a2_as = get_u16(a2->u.ptr->data);
1591
1592 if (a4)
1593 {
1594 if (a2_as != AS_TRANS)
1595 {
1596 /* Routes were aggregated by old router and therefore AS4_PATH
1597 * and AS4_AGGREGATOR is invalid
1598 *
1599 * Convert AS_PATH and AGGREGATOR to 4B format and finish.
1600 */
1601
1602 a2->u.ptr = bgp_aggregator_convert_to_new(a2->u.ptr, pool);
1603 p2->u.ptr = bgp_merge_as_paths(p2->u.ptr, NULL, AS_PATH_MAXLEN, pool);
1604
1605 return;
1606 }
1607 else
1608 {
1609 /* Common case, use AS4_AGGREGATOR attribute */
1610 a2->u.ptr = a4->u.ptr;
1611 }
1612 }
1613 else
1614 {
1615 /* Common case, use old AGGREGATOR attribute */
1616 a2->u.ptr = bgp_aggregator_convert_to_new(a2->u.ptr, pool);
1617
1618 if ((a2_as == AS_TRANS) && !a4_removed)
1619 log(L_WARN "%s: AGGREGATOR attribute contain AS_TRANS, but AS4_AGGREGATOR is missing", p->p.name);
1620 }
1621 }
1622 else
1623 if (a4)
1624 log(L_WARN "%s: AS4_AGGREGATOR attribute received, but AGGREGATOR attribute is missing", p->p.name);
1625
1626 int p2_len = as_path_getlen_int(p2->u.ptr, 2);
1627 int p4_len = p4 ? validate_as4_path(p, p4->u.ptr) : -1;
1628
1629 if (p4 && (p4_len < 0))
1630 log(L_WARN "%s: AS4_PATH attribute is malformed, skipping attribute", p->p.name);
1631
1632 if ((p4_len <= 0) || (p2_len < p4_len))
1633 p2->u.ptr = bgp_merge_as_paths(p2->u.ptr, NULL, AS_PATH_MAXLEN, pool);
1634 else
1635 p2->u.ptr = bgp_merge_as_paths(p2->u.ptr, p4->u.ptr, p2_len - p4_len, pool);
1636 }
1637
1638 static void
1639 bgp_remove_as4_attrs(struct bgp_proto *p, rta *a)
1640 {
1641 unsigned id1 = EA_CODE(EAP_BGP, BA_AS4_PATH);
1642 unsigned id2 = EA_CODE(EAP_BGP, BA_AS4_AGGREGATOR);
1643 ea_list **el = &(a->eattrs);
1644
1645 /* We know that ea_lists constructed in bgp_decode attrs have one attribute per ea_list struct */
1646 while (*el != NULL)
1647 {
1648 unsigned fid = (*el)->attrs[0].id;
1649
1650 if ((fid == id1) || (fid == id2))
1651 {
1652 *el = (*el)->next;
1653 if (p->as4_session)
1654 log(L_WARN "%s: Unexpected AS4_* attributes received", p->p.name);
1655 }
1656 else
1657 el = &((*el)->next);
1658 }
1659 }
1660
1661 /**
1662 * bgp_decode_attrs - check and decode BGP attributes
1663 * @conn: connection
1664 * @attr: start of attribute block
1665 * @len: length of attribute block
1666 * @pool: linear pool to make all the allocations in
1667 * @mandatory: 1 iff presence of mandatory attributes has to be checked
1668 *
1669 * This function takes a BGP attribute block (a part of an Update message), checks
1670 * its consistency and converts it to a list of BIRD route attributes represented
1671 * by a &rta.
1672 */
1673 struct rta *
1674 bgp_decode_attrs(struct bgp_conn *conn, byte *attr, uint len, struct linpool *pool, int mandatory)
1675 {
1676 struct bgp_proto *bgp = conn->bgp;
1677 rta *a = lp_alloc(pool, sizeof(struct rta));
1678 uint flags, code, l, i, type;
1679 int errcode;
1680 byte *z, *attr_start;
1681 byte seen[256/8];
1682 ea_list *ea;
1683 struct adata *ad;
1684 int withdraw = 0;
1685
1686 bzero(a, sizeof(rta));
1687 a->source = RTS_BGP;
1688 a->scope = SCOPE_UNIVERSE;
1689 a->cast = RTC_UNICAST;
1690 /* a->dest = RTD_ROUTER; -- set in bgp_set_next_hop() */
1691 a->from = bgp->cf->remote_ip;
1692
1693 /* Parse the attributes */
1694 bzero(seen, sizeof(seen));
1695 DBG("BGP: Parsing attributes\n");
1696 while (len)
1697 {
1698 if (len < 2)
1699 goto malformed;
1700 attr_start = attr;
1701 flags = *attr++;
1702 code = *attr++;
1703 len -= 2;
1704 if (flags & BAF_EXT_LEN)
1705 {
1706 if (len < 2)
1707 goto malformed;
1708 l = get_u16(attr);
1709 attr += 2;
1710 len -= 2;
1711 }
1712 else
1713 {
1714 if (len < 1)
1715 goto malformed;
1716 l = *attr++;
1717 len--;
1718 }
1719 if (l > len)
1720 goto malformed;
1721 len -= l;
1722 z = attr;
1723 attr += l;
1724 DBG("Attr %02x %02x %d\n", code, flags, l);
1725 if (seen[code/8] & (1 << (code%8)))
1726 goto malformed;
1727 if (ATTR_KNOWN(code))
1728 {
1729 struct attr_desc *desc = &bgp_attr_table[code];
1730 if (desc->expected_length >= 0 && desc->expected_length != (int) l)
1731 { errcode = 5; goto err; }
1732 if ((desc->expected_flags ^ flags) & (BAF_OPTIONAL | BAF_TRANSITIVE))
1733 { errcode = 4; goto err; }
1734 if (!desc->allow_in_ebgp && !bgp->is_internal)
1735 continue;
1736 if (desc->validate)
1737 {
1738 errcode = desc->validate(bgp, z, l);
1739 if (errcode > 0)
1740 goto err;
1741 if (errcode == IGNORE)
1742 continue;
1743 if (errcode <= WITHDRAW)
1744 {
1745 log(L_WARN "%s: Attribute %s is malformed, withdrawing update",
1746 bgp->p.name, desc->name);
1747 withdraw = 1;
1748 }
1749 }
1750 else if (code == BA_AS_PATH)
1751 {
1752 /* Special case as it might also trim the attribute */
1753 if (validate_as_path(bgp, z, &l) < 0)
1754 { errcode = 11; goto err; }
1755 }
1756 type = desc->type;
1757 }
1758 else /* Unknown attribute */
1759 {
1760 if (!(flags & BAF_OPTIONAL))
1761 { errcode = 2; goto err; }
1762 type = EAF_TYPE_OPAQUE;
1763 }
1764
1765 // Only OPTIONAL and TRANSITIVE attributes may have non-zero PARTIAL flag
1766 // if (!((flags & BAF_OPTIONAL) && (flags & BAF_TRANSITIVE)) && (flags & BAF_PARTIAL))
1767 // { errcode = 4; goto err; }
1768
1769 seen[code/8] |= (1 << (code%8));
1770 ea = lp_alloc(pool, sizeof(ea_list) + sizeof(eattr));
1771 ea->next = a->eattrs;
1772 a->eattrs = ea;
1773 ea->flags = 0;
1774 ea->count = 1;
1775 ea->attrs[0].id = EA_CODE(EAP_BGP, code);
1776 ea->attrs[0].flags = flags;
1777 ea->attrs[0].type = type;
1778 if (type & EAF_EMBEDDED)
1779 ad = NULL;
1780 else
1781 {
1782 ad = lp_alloc(pool, sizeof(struct adata) + l);
1783 ea->attrs[0].u.ptr = ad;
1784 ad->length = l;
1785 memcpy(ad->data, z, l);
1786 }
1787 switch (type)
1788 {
1789 case EAF_TYPE_ROUTER_ID:
1790 case EAF_TYPE_INT:
1791 if (l == 1)
1792 ea->attrs[0].u.data = *z;
1793 else
1794 ea->attrs[0].u.data = get_u32(z);
1795 break;
1796 case EAF_TYPE_IP_ADDRESS:
1797 ipa_ntoh(*(ip_addr *)ad->data);
1798 break;
1799 case EAF_TYPE_INT_SET:
1800 case EAF_TYPE_EC_SET:
1801 {
1802 u32 *z = (u32 *) ad->data;
1803 for(i=0; i<ad->length/4; i++)
1804 z[i] = ntohl(z[i]);
1805 break;
1806 }
1807 }
1808 }
1809
1810 if (withdraw)
1811 goto withdraw;
1812
1813 #ifdef IPV6
1814 /* If we received MP_REACH_NLRI we should check mandatory attributes */
1815 if (bgp->mp_reach_len != 0)
1816 mandatory = 1;
1817 #endif
1818
1819 /* If there is no (reachability) NLRI, we should exit now */
1820 if (! mandatory)
1821 return a;
1822
1823 /* Check if all mandatory attributes are present */
1824 for(i=0; i < ARRAY_SIZE(bgp_mandatory_attrs); i++)
1825 {
1826 code = bgp_mandatory_attrs[i];
1827 if (!(seen[code/8] & (1 << (code%8))))
1828 {
1829 bgp_error(conn, 3, 3, &bgp_mandatory_attrs[i], 1);
1830 return NULL;
1831 }
1832 }
1833
1834 /* When receiving attributes from non-AS4-aware BGP speaker,
1835 * we have to reconstruct 4B AS_PATH and AGGREGATOR attributes
1836 */
1837 if (! bgp->as4_session)
1838 bgp_reconstruct_4b_atts(bgp, a, pool);
1839
1840 bgp_remove_as4_attrs(bgp, a);
1841
1842 /* If the AS path attribute contains our AS, reject the routes */
1843 if (bgp_as_path_loopy(bgp, a))
1844 goto withdraw;
1845
1846 /* Two checks for IBGP loops caused by route reflection, RFC 4456 */
1847 if (bgp_originator_id_loopy(bgp, a) ||
1848 bgp_cluster_list_loopy(bgp, a))
1849 goto withdraw;
1850
1851 /* If there's no local preference, define one */
1852 if (!(seen[0] & (1 << BA_LOCAL_PREF)))
1853 bgp_attach_attr(&a->eattrs, pool, BA_LOCAL_PREF, bgp->cf->default_local_pref);
1854
1855 return a;
1856
1857 withdraw:
1858 return NULL;
1859
1860 malformed:
1861 bgp_error(conn, 3, 1, NULL, 0);
1862 return NULL;
1863
1864 err:
1865 bgp_error(conn, 3, errcode, attr_start, z+l-attr_start);
1866 return NULL;
1867 }
1868
1869 int
1870 bgp_get_attr(eattr *a, byte *buf, int buflen)
1871 {
1872 uint i = EA_ID(a->id);
1873 struct attr_desc *d;
1874 int len;
1875
1876 if (ATTR_KNOWN(i))
1877 {
1878 d = &bgp_attr_table[i];
1879 len = bsprintf(buf, "%s", d->name);
1880 buf += len;
1881 if (d->format)
1882 {
1883 *buf++ = ':';
1884 *buf++ = ' ';
1885 d->format(a, buf, buflen - len - 2);
1886 return GA_FULL;
1887 }
1888 return GA_NAME;
1889 }
1890 bsprintf(buf, "%02x%s", i, (a->flags & BAF_TRANSITIVE) ? " [t]" : "");
1891 return GA_NAME;
1892 }
1893
1894 void
1895 bgp_init_bucket_table(struct bgp_proto *p)
1896 {
1897 p->hash_size = 256;
1898 p->hash_limit = p->hash_size * 4;
1899 p->bucket_hash = mb_allocz(p->p.pool, p->hash_size * sizeof(struct bgp_bucket *));
1900 init_list(&p->bucket_queue);
1901 p->withdraw_bucket = NULL;
1902 // fib_init(&p->prefix_fib, p->p.pool, sizeof(struct bgp_prefix), 0, bgp_init_prefix);
1903 }
1904
1905 void
1906 bgp_get_route_info(rte *e, byte *buf, ea_list *attrs)
1907 {
1908 eattr *p = ea_find(attrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1909 eattr *o = ea_find(attrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1910 u32 origas;
1911
1912 buf += bsprintf(buf, " (%d", e->pref);
1913
1914 if (e->u.bgp.suppressed)
1915 buf += bsprintf(buf, "-");
1916
1917 if (e->attrs->hostentry)
1918 {
1919 if (!rte_resolvable(e))
1920 buf += bsprintf(buf, "/-");
1921 else if (e->attrs->igp_metric >= IGP_METRIC_UNKNOWN)
1922 buf += bsprintf(buf, "/?");
1923 else
1924 buf += bsprintf(buf, "/%d", e->attrs->igp_metric);
1925 }
1926 buf += bsprintf(buf, ") [");
1927
1928 if (p && as_path_get_last(p->u.ptr, &origas))
1929 buf += bsprintf(buf, "AS%u", origas);
1930 if (o)
1931 buf += bsprintf(buf, "%c", "ie?"[o->u.data]);
1932 strcpy(buf, "]");
1933 }