]> git.ipfire.org Git - thirdparty/bird.git/blob - proto/bgp/attrs.c
Prints full community lists during 'show route all'.
[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 static byte bgp_mandatory_attrs[] = { BA_ORIGIN, BA_AS_PATH
62 #ifndef IPV6
63 ,BA_NEXT_HOP
64 #endif
65 };
66
67 struct attr_desc {
68 char *name;
69 int expected_length;
70 int expected_flags;
71 int type;
72 int allow_in_ebgp;
73 int (*validate)(struct bgp_proto *p, byte *attr, int len);
74 void (*format)(eattr *ea, byte *buf, int buflen);
75 };
76
77 #define IGNORE -1
78 #define WITHDRAW -2
79
80 static int
81 bgp_check_origin(struct bgp_proto *p UNUSED, byte *a, int len UNUSED)
82 {
83 if (*a > 2)
84 return 6;
85 return 0;
86 }
87
88 static void
89 bgp_format_origin(eattr *a, byte *buf, int buflen UNUSED)
90 {
91 static char *bgp_origin_names[] = { "IGP", "EGP", "Incomplete" };
92
93 bsprintf(buf, bgp_origin_names[a->u.data]);
94 }
95
96 static int
97 path_segment_contains(byte *p, int bs, u32 asn)
98 {
99 int i;
100 int len = p[1];
101 p += 2;
102
103 for(i=0; i<len; i++)
104 {
105 u32 asn2 = (bs == 4) ? get_u32(p) : get_u16(p);
106 if (asn2 == asn)
107 return 1;
108 p += bs;
109 }
110
111 return 0;
112 }
113
114 /* Validates path attribute, removes AS_CONFED_* segments, and also returns path length */
115 static int
116 validate_path(struct bgp_proto *p, int as_path, int bs, byte *idata, unsigned int *ilength)
117 {
118 int res = 0;
119 u8 *a, *dst;
120 int len, plen, copy;
121
122 dst = a = idata;
123 len = *ilength;
124
125 while (len)
126 {
127 if (len < 2)
128 return -1;
129
130 plen = 2 + bs * a[1];
131 if (len < plen)
132 return -1;
133
134 switch (a[0])
135 {
136 case AS_PATH_SET:
137 copy = 1;
138 res++;
139 break;
140
141 case AS_PATH_SEQUENCE:
142 copy = 1;
143 res += a[1];
144 break;
145
146 case AS_PATH_CONFED_SEQUENCE:
147 case AS_PATH_CONFED_SET:
148 if (as_path && path_segment_contains(a, bs, p->remote_as))
149 {
150 log(L_WARN "%s: AS_CONFED_* segment with peer ASN found, misconfigured confederation?", p->p.name);
151 return -1;
152 }
153
154 log(L_WARN "%s: %s_PATH attribute contains AS_CONFED_* segment, skipping segment",
155 p->p.name, as_path ? "AS" : "AS4");
156 copy = 0;
157 break;
158
159 default:
160 return -1;
161 }
162
163 if (copy)
164 {
165 if (dst != a)
166 memmove(dst, a, plen);
167 dst += plen;
168 }
169
170 len -= plen;
171 a += plen;
172 }
173
174 *ilength = dst - idata;
175 return res;
176 }
177
178 static inline int
179 validate_as_path(struct bgp_proto *p, byte *a, int *len)
180 {
181 return validate_path(p, 1, p->as4_session ? 4 : 2, a, len);
182 }
183
184 static inline int
185 validate_as4_path(struct bgp_proto *p, struct adata *path)
186 {
187 return validate_path(p, 0, 4, path->data, &path->length);
188 }
189
190 static int
191 bgp_check_next_hop(struct bgp_proto *p UNUSED, byte *a, int len)
192 {
193 #ifdef IPV6
194 return IGNORE;
195 #else
196 ip_addr addr;
197
198 memcpy(&addr, a, len);
199 ipa_ntoh(addr);
200 if (ipa_classify(addr) & IADDR_HOST)
201 return 0;
202 else
203 return 8;
204 #endif
205 }
206
207 static void
208 bgp_format_next_hop(eattr *a, byte *buf, int buflen UNUSED)
209 {
210 ip_addr *ipp = (ip_addr *) a->u.ptr->data;
211 #ifdef IPV6
212 /* in IPv6, we might have two addresses in NEXT HOP */
213 if ((a->u.ptr->length == NEXT_HOP_LENGTH) && ipa_nonzero(ipp[1]))
214 {
215 bsprintf(buf, "%I %I", ipp[0], ipp[1]);
216 return;
217 }
218 #endif
219
220 bsprintf(buf, "%I", ipp[0]);
221 }
222
223 static int
224 bgp_check_aggregator(struct bgp_proto *p, byte *a UNUSED, int len)
225 {
226 int exp_len = p->as4_session ? 8 : 6;
227
228 return (len == exp_len) ? 0 : WITHDRAW;
229 }
230
231 static void
232 bgp_format_aggregator(eattr *a, byte *buf, int buflen UNUSED)
233 {
234 struct adata *ad = a->u.ptr;
235 byte *data = ad->data;
236 u32 as;
237
238 as = get_u32(data);
239 data += 4;
240
241 bsprintf(buf, "%d.%d.%d.%d AS%d", data[0], data[1], data[2], data[3], as);
242 }
243
244 static int
245 bgp_check_community(struct bgp_proto *p UNUSED, byte *a UNUSED, int len)
246 {
247 return ((len % 4) == 0) ? 0 : WITHDRAW;
248 }
249
250
251 static int
252 bgp_check_cluster_list(struct bgp_proto *p UNUSED, byte *a UNUSED, int len)
253 {
254 return ((len % 4) == 0) ? 0 : 5;
255 }
256
257 static void
258 bgp_format_cluster_list(eattr *a, byte *buf, int buflen)
259 {
260 /* Truncates cluster lists larger than buflen, probably not a problem */
261 int_set_format(a->u.ptr, 0, -1, buf, buflen);
262 }
263
264 static int
265 bgp_check_reach_nlri(struct bgp_proto *p UNUSED, byte *a UNUSED, int len UNUSED)
266 {
267 #ifdef IPV6
268 p->mp_reach_start = a;
269 p->mp_reach_len = len;
270 #endif
271 return IGNORE;
272 }
273
274 static int
275 bgp_check_unreach_nlri(struct bgp_proto *p UNUSED, byte *a UNUSED, int len UNUSED)
276 {
277 #ifdef IPV6
278 p->mp_unreach_start = a;
279 p->mp_unreach_len = len;
280 #endif
281 return IGNORE;
282 }
283
284 static struct attr_desc bgp_attr_table[] = {
285 { NULL, -1, 0, 0, 0, /* Undefined */
286 NULL, NULL },
287 { "origin", 1, BAF_TRANSITIVE, EAF_TYPE_INT, 1, /* BA_ORIGIN */
288 bgp_check_origin, bgp_format_origin },
289 { "as_path", -1, BAF_TRANSITIVE, EAF_TYPE_AS_PATH, 1, /* BA_AS_PATH */
290 NULL, NULL }, /* is checked by validate_as_path() as a special case */
291 { "next_hop", 4, BAF_TRANSITIVE, EAF_TYPE_IP_ADDRESS, 1, /* BA_NEXT_HOP */
292 bgp_check_next_hop, bgp_format_next_hop },
293 { "med", 4, BAF_OPTIONAL, EAF_TYPE_INT, 1, /* BA_MULTI_EXIT_DISC */
294 NULL, NULL },
295 { "local_pref", 4, BAF_TRANSITIVE, EAF_TYPE_INT, 0, /* BA_LOCAL_PREF */
296 NULL, NULL },
297 { "atomic_aggr", 0, BAF_TRANSITIVE, EAF_TYPE_OPAQUE, 1, /* BA_ATOMIC_AGGR */
298 NULL, NULL },
299 { "aggregator", -1, BAF_OPTIONAL | BAF_TRANSITIVE, EAF_TYPE_OPAQUE, 1, /* BA_AGGREGATOR */
300 bgp_check_aggregator, bgp_format_aggregator },
301 { "community", -1, BAF_OPTIONAL | BAF_TRANSITIVE, EAF_TYPE_INT_SET, 1, /* BA_COMMUNITY */
302 bgp_check_community, NULL },
303 { "originator_id", 4, BAF_OPTIONAL, EAF_TYPE_ROUTER_ID, 0, /* BA_ORIGINATOR_ID */
304 NULL, NULL },
305 { "cluster_list", -1, BAF_OPTIONAL, EAF_TYPE_INT_SET, 0, /* BA_CLUSTER_LIST */
306 bgp_check_cluster_list, bgp_format_cluster_list },
307 { .name = NULL }, /* BA_DPA */
308 { .name = NULL }, /* BA_ADVERTISER */
309 { .name = NULL }, /* BA_RCID_PATH */
310 { "mp_reach_nlri", -1, BAF_OPTIONAL, EAF_TYPE_OPAQUE, 1, /* BA_MP_REACH_NLRI */
311 bgp_check_reach_nlri, NULL },
312 { "mp_unreach_nlri", -1, BAF_OPTIONAL, EAF_TYPE_OPAQUE, 1, /* BA_MP_UNREACH_NLRI */
313 bgp_check_unreach_nlri, NULL },
314 { .name = NULL }, /* BA_EXTENDED_COMM */
315 { "as4_path", -1, BAF_OPTIONAL | BAF_TRANSITIVE, EAF_TYPE_OPAQUE, 1, /* BA_AS4_PATH */
316 NULL, NULL },
317 { "as4_aggregator", -1, BAF_OPTIONAL | BAF_TRANSITIVE, EAF_TYPE_OPAQUE, 1, /* BA_AS4_PATH */
318 NULL, NULL }
319 };
320
321 /* BA_AS4_PATH is type EAF_TYPE_OPAQUE and not type EAF_TYPE_AS_PATH.
322 * It does not matter as this attribute does not appear on routes in the routing table.
323 */
324
325 #define ATTR_KNOWN(code) ((code) < ARRAY_SIZE(bgp_attr_table) && bgp_attr_table[code].name)
326
327 static inline struct adata *
328 bgp_alloc_adata(struct linpool *pool, unsigned len)
329 {
330 struct adata *ad = lp_alloc(pool, sizeof(struct adata) + len);
331 ad->length = len;
332 return ad;
333 }
334
335 static void
336 bgp_set_attr(eattr *e, unsigned attr, uintptr_t val)
337 {
338 ASSERT(ATTR_KNOWN(attr));
339 e->id = EA_CODE(EAP_BGP, attr);
340 e->type = bgp_attr_table[attr].type;
341 e->flags = bgp_attr_table[attr].expected_flags;
342 if (e->type & EAF_EMBEDDED)
343 e->u.data = val;
344 else
345 e->u.ptr = (struct adata *) val;
346 }
347
348 static byte *
349 bgp_set_attr_wa(eattr *e, struct linpool *pool, unsigned attr, unsigned len)
350 {
351 struct adata *ad = bgp_alloc_adata(pool, len);
352 bgp_set_attr(e, attr, (uintptr_t) ad);
353 return ad->data;
354 }
355
356 void
357 bgp_attach_attr(ea_list **to, struct linpool *pool, unsigned attr, uintptr_t val)
358 {
359 ea_list *a = lp_alloc(pool, sizeof(ea_list) + sizeof(eattr));
360 a->next = *to;
361 *to = a;
362 a->flags = EALF_SORTED;
363 a->count = 1;
364 bgp_set_attr(a->attrs, attr, val);
365 }
366
367 byte *
368 bgp_attach_attr_wa(ea_list **to, struct linpool *pool, unsigned attr, unsigned len)
369 {
370 struct adata *ad = bgp_alloc_adata(pool, len);
371 bgp_attach_attr(to, pool, attr, (uintptr_t) ad);
372 return ad->data;
373 }
374
375 static int
376 bgp_encode_attr_hdr(byte *dst, unsigned int flags, unsigned code, int len)
377 {
378 int wlen;
379
380 DBG("\tAttribute %02x (%d bytes, flags %02x)\n", code, len, flags);
381
382 if (len < 256)
383 {
384 *dst++ = flags;
385 *dst++ = code;
386 *dst++ = len;
387 wlen = 3;
388 }
389 else
390 {
391 *dst++ = flags | BAF_EXT_LEN;
392 *dst++ = code;
393 put_u16(dst, len);
394 wlen = 4;
395 }
396
397 return wlen;
398 }
399
400 static void
401 aggregator_convert_to_old(struct adata *aggr, byte *dst, int *new_used)
402 {
403 byte *src = aggr->data;
404 *new_used = 0;
405
406 u32 as = get_u32(src);
407 if (as > 0xFFFF)
408 {
409 as = AS_TRANS;
410 *new_used = 1;
411 }
412 put_u16(dst, as);
413
414 /* Copy IPv4 address */
415 memcpy(dst + 2, src + 4, 4);
416 }
417
418 static void
419 aggregator_convert_to_new(struct adata *aggr, byte *dst)
420 {
421 byte *src = aggr->data;
422
423 u32 as = get_u16(src);
424 put_u32(dst, as);
425
426 /* Copy IPv4 address */
427 memcpy(dst + 4, src + 2, 4);
428 }
429
430 static int
431 bgp_get_attr_len(eattr *a)
432 {
433 int len;
434 if (ATTR_KNOWN(EA_ID(a->id)))
435 {
436 int code = EA_ID(a->id);
437 struct attr_desc *desc = &bgp_attr_table[code];
438 len = desc->expected_length;
439 if (len < 0)
440 {
441 ASSERT(!(a->type & EAF_EMBEDDED));
442 len = a->u.ptr->length;
443 }
444 }
445 else
446 {
447 ASSERT((a->type & EAF_TYPE_MASK) == EAF_TYPE_OPAQUE);
448 len = a->u.ptr->length;
449 }
450
451 return len;
452 }
453
454 #define ADVANCE(w, r, l) do { r -= l; w += l; } while (0)
455
456 /**
457 * bgp_encode_attrs - encode BGP attributes
458 * @p: BGP instance
459 * @w: buffer
460 * @attrs: a list of extended attributes
461 * @remains: remaining space in the buffer
462 *
463 * The bgp_encode_attrs() function takes a list of extended attributes
464 * and converts it to its BGP representation (a part of an Update message).
465 *
466 * Result: Length of the attribute block generated or -1 if not enough space.
467 */
468 unsigned int
469 bgp_encode_attrs(struct bgp_proto *p, byte *w, ea_list *attrs, int remains)
470 {
471 unsigned int i, code, flags;
472 byte *start = w;
473 int len, rv;
474
475 for(i=0; i<attrs->count; i++)
476 {
477 eattr *a = &attrs->attrs[i];
478 ASSERT(EA_PROTO(a->id) == EAP_BGP);
479 code = EA_ID(a->id);
480 #ifdef IPV6
481 /* When talking multiprotocol BGP, the NEXT_HOP attributes are used only temporarily. */
482 if (code == BA_NEXT_HOP)
483 continue;
484 #endif
485
486 /* When AS4-aware BGP speaker is talking to non-AS4-aware BGP speaker,
487 * we have to convert our 4B AS_PATH to 2B AS_PATH and send our AS_PATH
488 * as optional AS4_PATH attribute.
489 */
490 if ((code == BA_AS_PATH) && (! p->as4_session))
491 {
492 len = a->u.ptr->length;
493
494 if (remains < (len + 4))
495 goto err_no_buffer;
496
497 /* Using temporary buffer because don't know a length of created attr
498 * and therefore a length of a header. Perhaps i should better always
499 * use BAF_EXT_LEN. */
500
501 byte buf[len];
502 int new_used;
503 int nl = as_path_convert_to_old(a->u.ptr, buf, &new_used);
504
505 DBG("BGP: Encoding old AS_PATH\n");
506 rv = bgp_encode_attr_hdr(w, BAF_TRANSITIVE, BA_AS_PATH, nl);
507 ADVANCE(w, remains, rv);
508 memcpy(w, buf, nl);
509 ADVANCE(w, remains, nl);
510
511 if (! new_used)
512 continue;
513
514 if (remains < (len + 4))
515 goto err_no_buffer;
516
517 /* We should discard AS_CONFED_SEQUENCE or AS_CONFED_SET path segments
518 * here but we don't support confederations and such paths we already
519 * discarded in bgp_check_as_path().
520 */
521
522 DBG("BGP: Encoding AS4_PATH\n");
523 rv = bgp_encode_attr_hdr(w, BAF_OPTIONAL | BAF_TRANSITIVE, BA_AS4_PATH, len);
524 ADVANCE(w, remains, rv);
525 memcpy(w, a->u.ptr->data, len);
526 ADVANCE(w, remains, len);
527
528 continue;
529 }
530
531 /* The same issue with AGGREGATOR attribute */
532 if ((code == BA_AGGREGATOR) && (! p->as4_session))
533 {
534 int new_used;
535
536 len = 6;
537 if (remains < (len + 3))
538 goto err_no_buffer;
539
540 rv = bgp_encode_attr_hdr(w, BAF_OPTIONAL | BAF_TRANSITIVE, BA_AGGREGATOR, len);
541 ADVANCE(w, remains, rv);
542 aggregator_convert_to_old(a->u.ptr, w, &new_used);
543 ADVANCE(w, remains, len);
544
545 if (! new_used)
546 continue;
547
548 len = 8;
549 if (remains < (len + 3))
550 goto err_no_buffer;
551
552 rv = bgp_encode_attr_hdr(w, BAF_OPTIONAL | BAF_TRANSITIVE, BA_AS4_AGGREGATOR, len);
553 ADVANCE(w, remains, rv);
554 memcpy(w, a->u.ptr->data, len);
555 ADVANCE(w, remains, len);
556
557 continue;
558 }
559
560 /* Standard path continues here ... */
561
562 flags = a->flags & (BAF_OPTIONAL | BAF_TRANSITIVE | BAF_PARTIAL);
563 len = bgp_get_attr_len(a);
564
565 /* Skip empty int sets */
566 if (((a->type & EAF_TYPE_MASK) == EAF_TYPE_INT_SET) && (len == 0))
567 continue;
568
569 if (remains < len + 4)
570 goto err_no_buffer;
571
572 rv = bgp_encode_attr_hdr(w, flags, code, len);
573 ADVANCE(w, remains, rv);
574
575 switch (a->type & EAF_TYPE_MASK)
576 {
577 case EAF_TYPE_INT:
578 case EAF_TYPE_ROUTER_ID:
579 if (len == 4)
580 put_u32(w, a->u.data);
581 else
582 *w = a->u.data;
583 break;
584 case EAF_TYPE_IP_ADDRESS:
585 {
586 ip_addr ip = *(ip_addr *)a->u.ptr->data;
587 ipa_hton(ip);
588 memcpy(w, &ip, len);
589 break;
590 }
591 case EAF_TYPE_INT_SET:
592 {
593 u32 *z = (u32 *)a->u.ptr->data;
594 int i;
595 for(i=0; i<len; i+=4)
596 put_u32(w+i, *z++);
597 break;
598 }
599 case EAF_TYPE_OPAQUE:
600 case EAF_TYPE_AS_PATH:
601 memcpy(w, a->u.ptr->data, len);
602 break;
603 default:
604 bug("bgp_encode_attrs: unknown attribute type %02x", a->type);
605 }
606 ADVANCE(w, remains, len);
607 }
608 return w - start;
609
610 err_no_buffer:
611 return -1;
612 }
613
614 static void
615 bgp_init_prefix(struct fib_node *N)
616 {
617 struct bgp_prefix *p = (struct bgp_prefix *) N;
618 p->bucket_node.next = NULL;
619 }
620
621 static int
622 bgp_compare_u32(const u32 *x, const u32 *y)
623 {
624 return (*x < *y) ? -1 : (*x > *y) ? 1 : 0;
625 }
626
627 static void
628 bgp_normalize_set(u32 *dest, u32 *src, unsigned cnt)
629 {
630 memcpy(dest, src, sizeof(u32) * cnt);
631 qsort(dest, cnt, sizeof(u32), (int(*)(const void *, const void *)) bgp_compare_u32);
632 }
633
634 static void
635 bgp_rehash_buckets(struct bgp_proto *p)
636 {
637 struct bgp_bucket **old = p->bucket_hash;
638 struct bgp_bucket **new;
639 unsigned oldn = p->hash_size;
640 unsigned i, e, mask;
641 struct bgp_bucket *b;
642
643 p->hash_size = p->hash_limit;
644 DBG("BGP: Rehashing bucket table from %d to %d\n", oldn, p->hash_size);
645 p->hash_limit *= 4;
646 if (p->hash_limit >= 65536)
647 p->hash_limit = ~0;
648 new = p->bucket_hash = mb_allocz(p->p.pool, p->hash_size * sizeof(struct bgp_bucket *));
649 mask = p->hash_size - 1;
650 for (i=0; i<oldn; i++)
651 while (b = old[i])
652 {
653 old[i] = b->hash_next;
654 e = b->hash & mask;
655 b->hash_next = new[e];
656 if (b->hash_next)
657 b->hash_next->hash_prev = b;
658 b->hash_prev = NULL;
659 new[e] = b;
660 }
661 mb_free(old);
662 }
663
664 static struct bgp_bucket *
665 bgp_new_bucket(struct bgp_proto *p, ea_list *new, unsigned hash)
666 {
667 struct bgp_bucket *b;
668 unsigned ea_size = sizeof(ea_list) + new->count * sizeof(eattr);
669 unsigned ea_size_aligned = BIRD_ALIGN(ea_size, CPU_STRUCT_ALIGN);
670 unsigned size = sizeof(struct bgp_bucket) + ea_size;
671 unsigned i;
672 byte *dest;
673 unsigned index = hash & (p->hash_size - 1);
674
675 /* Gather total size of non-inline attributes */
676 for (i=0; i<new->count; i++)
677 {
678 eattr *a = &new->attrs[i];
679 if (!(a->type & EAF_EMBEDDED))
680 size += BIRD_ALIGN(sizeof(struct adata) + a->u.ptr->length, CPU_STRUCT_ALIGN);
681 }
682
683 /* Create the bucket and hash it */
684 b = mb_alloc(p->p.pool, size);
685 b->hash_next = p->bucket_hash[index];
686 if (b->hash_next)
687 b->hash_next->hash_prev = b;
688 p->bucket_hash[index] = b;
689 b->hash_prev = NULL;
690 b->hash = hash;
691 add_tail(&p->bucket_queue, &b->send_node);
692 init_list(&b->prefixes);
693 memcpy(b->eattrs, new, ea_size);
694 dest = ((byte *)b->eattrs) + ea_size_aligned;
695
696 /* Copy values of non-inline attributes */
697 for (i=0; i<new->count; i++)
698 {
699 eattr *a = &b->eattrs->attrs[i];
700 if (!(a->type & EAF_EMBEDDED))
701 {
702 struct adata *oa = a->u.ptr;
703 struct adata *na = (struct adata *) dest;
704 memcpy(na, oa, sizeof(struct adata) + oa->length);
705 a->u.ptr = na;
706 dest += BIRD_ALIGN(sizeof(struct adata) + na->length, CPU_STRUCT_ALIGN);
707 }
708 }
709
710 /* If needed, rehash */
711 p->hash_count++;
712 if (p->hash_count > p->hash_limit)
713 bgp_rehash_buckets(p);
714
715 return b;
716 }
717
718 static struct bgp_bucket *
719 bgp_get_bucket(struct bgp_proto *p, net *n, ea_list *attrs, int originate)
720 {
721 ea_list *new;
722 unsigned i, cnt, hash, code;
723 eattr *a, *d;
724 u32 seen = 0;
725 struct bgp_bucket *b;
726
727 /* Merge the attribute list */
728 new = alloca(ea_scan(attrs));
729 ea_merge(attrs, new);
730 ea_sort(new);
731
732 /* Normalize attributes */
733 d = new->attrs;
734 cnt = new->count;
735 new->count = 0;
736 for(i=0; i<cnt; i++)
737 {
738 a = &new->attrs[i];
739 if (EA_PROTO(a->id) != EAP_BGP)
740 continue;
741 code = EA_ID(a->id);
742 if (ATTR_KNOWN(code))
743 {
744 if (!bgp_attr_table[code].allow_in_ebgp && !p->is_internal)
745 continue;
746 /* The flags might have been zero if the attr was added by filters */
747 a->flags = (a->flags & BAF_PARTIAL) | bgp_attr_table[code].expected_flags;
748 if (code < 32)
749 seen |= 1 << code;
750 }
751 else
752 {
753 /* Don't re-export unknown non-transitive attributes */
754 if (!(a->flags & BAF_TRANSITIVE))
755 continue;
756 }
757 *d = *a;
758 if ((d->type & EAF_ORIGINATED) && !originate && (d->flags & BAF_TRANSITIVE) && (d->flags & BAF_OPTIONAL))
759 d->flags |= BAF_PARTIAL;
760 switch (d->type & EAF_TYPE_MASK)
761 {
762 case EAF_TYPE_INT_SET:
763 {
764 struct adata *z = alloca(sizeof(struct adata) + d->u.ptr->length);
765 z->length = d->u.ptr->length;
766 bgp_normalize_set((u32 *) z->data, (u32 *) d->u.ptr->data, z->length / 4);
767 d->u.ptr = z;
768 break;
769 }
770 default: ;
771 }
772 d++;
773 new->count++;
774 }
775
776 /* Hash */
777 hash = ea_hash(new);
778 for(b=p->bucket_hash[hash & (p->hash_size - 1)]; b; b=b->hash_next)
779 if (b->hash == hash && ea_same(b->eattrs, new))
780 {
781 DBG("Found bucket.\n");
782 return b;
783 }
784
785 /* Ensure that there are all mandatory attributes */
786 for(i=0; i<ARRAY_SIZE(bgp_mandatory_attrs); i++)
787 if (!(seen & (1 << bgp_mandatory_attrs[i])))
788 {
789 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);
790 return NULL;
791 }
792
793 /* Check if next hop is valid */
794 a = ea_find(new, EA_CODE(EAP_BGP, BA_NEXT_HOP));
795 if (!a || ipa_equal(p->cf->remote_ip, *(ip_addr *)a->u.ptr->data))
796 {
797 log(L_ERR "%s: Invalid NEXT_HOP attribute in route %I/%d", p->p.name, n->n.prefix, n->n.pxlen);
798 return NULL;
799 }
800
801 /* Create new bucket */
802 DBG("Creating bucket.\n");
803 return bgp_new_bucket(p, new, hash);
804 }
805
806 void
807 bgp_free_bucket(struct bgp_proto *p, struct bgp_bucket *buck)
808 {
809 if (buck->hash_next)
810 buck->hash_next->hash_prev = buck->hash_prev;
811 if (buck->hash_prev)
812 buck->hash_prev->hash_next = buck->hash_next;
813 else
814 p->bucket_hash[buck->hash & (p->hash_size-1)] = buck->hash_next;
815 mb_free(buck);
816 }
817
818 void
819 bgp_rt_notify(struct proto *P, rtable *tbl UNUSED, net *n, rte *new, rte *old UNUSED, ea_list *attrs)
820 {
821 struct bgp_proto *p = (struct bgp_proto *) P;
822 struct bgp_bucket *buck;
823 struct bgp_prefix *px;
824
825 DBG("BGP: Got route %I/%d %s\n", n->n.prefix, n->n.pxlen, new ? "up" : "down");
826
827 if (new)
828 {
829 buck = bgp_get_bucket(p, n, attrs, new->attrs->source != RTS_BGP);
830 if (!buck) /* Inconsistent attribute list */
831 return;
832 }
833 else
834 {
835 if (!(buck = p->withdraw_bucket))
836 {
837 buck = p->withdraw_bucket = mb_alloc(P->pool, sizeof(struct bgp_bucket));
838 init_list(&buck->prefixes);
839 }
840 }
841 px = fib_get(&p->prefix_fib, &n->n.prefix, n->n.pxlen);
842 if (px->bucket_node.next)
843 {
844 DBG("\tRemoving old entry.\n");
845 rem_node(&px->bucket_node);
846 }
847 add_tail(&buck->prefixes, &px->bucket_node);
848 bgp_schedule_packet(p->conn, PKT_UPDATE);
849 }
850
851 static int
852 bgp_create_attrs(struct bgp_proto *p, rte *e, ea_list **attrs, struct linpool *pool)
853 {
854 ea_list *ea = lp_alloc(pool, sizeof(ea_list) + 4*sizeof(eattr));
855 rta *rta = e->attrs;
856 byte *z;
857
858 ea->next = *attrs;
859 *attrs = ea;
860 ea->flags = EALF_SORTED;
861 ea->count = 4;
862
863 bgp_set_attr(ea->attrs, BA_ORIGIN,
864 ((rta->source == RTS_OSPF_EXT1) || (rta->source == RTS_OSPF_EXT2)) ? ORIGIN_INCOMPLETE : ORIGIN_IGP);
865
866 if (p->is_internal)
867 bgp_set_attr_wa(ea->attrs+1, pool, BA_AS_PATH, 0);
868 else
869 {
870 z = bgp_set_attr_wa(ea->attrs+1, pool, BA_AS_PATH, 6);
871 z[0] = AS_PATH_SEQUENCE;
872 z[1] = 1; /* 1 AS */
873 put_u32(z+2, p->local_as);
874 }
875
876 /* iBGP -> use gw, eBGP multi-hop -> use source_addr,
877 eBGP single-hop -> use gw if on the same iface */
878 z = bgp_set_attr_wa(ea->attrs+2, pool, BA_NEXT_HOP, NEXT_HOP_LENGTH);
879 if (p->cf->next_hop_self ||
880 rta->dest != RTD_ROUTER ||
881 ipa_equal(rta->gw, IPA_NONE) ||
882 ipa_has_link_scope(rta->gw) ||
883 (!p->is_internal && (!p->neigh || (rta->iface != p->neigh->iface))))
884 set_next_hop(z, p->source_addr);
885 else
886 set_next_hop(z, rta->gw);
887
888 bgp_set_attr(ea->attrs+3, BA_LOCAL_PREF, p->cf->default_local_pref);
889
890 return 0; /* Leave decision to the filters */
891 }
892
893
894 static inline int
895 bgp_as_path_loopy(struct bgp_proto *p, rta *a)
896 {
897 eattr *e = ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
898 return (e && as_path_is_member(e->u.ptr, p->local_as));
899 }
900
901 static inline int
902 bgp_originator_id_loopy(struct bgp_proto *p, rta *a)
903 {
904 eattr *e = ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
905 return (e && (e->u.data == p->local_id));
906 }
907
908 static inline int
909 bgp_cluster_list_loopy(struct bgp_proto *p, rta *a)
910 {
911 eattr *e = ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
912 return (e && p->rr_client && int_set_contains(e->u.ptr, p->rr_cluster_id));
913 }
914
915
916 static inline void
917 bgp_path_prepend(rte *e, ea_list **attrs, struct linpool *pool, u32 as)
918 {
919 eattr *a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
920 bgp_attach_attr(attrs, pool, BA_AS_PATH, (uintptr_t) as_path_prepend(pool, a->u.ptr, as));
921 }
922
923 static inline void
924 bgp_cluster_list_prepend(rte *e, ea_list **attrs, struct linpool *pool, u32 cid)
925 {
926 eattr *a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
927 bgp_attach_attr(attrs, pool, BA_CLUSTER_LIST, (uintptr_t) int_set_add(pool, a ? a->u.ptr : NULL, cid));
928 }
929
930 static int
931 bgp_update_attrs(struct bgp_proto *p, rte *e, ea_list **attrs, struct linpool *pool, int rr)
932 {
933 eattr *a;
934
935 if (!p->is_internal && !p->rs_client)
936 {
937 bgp_path_prepend(e, attrs, pool, p->local_as);
938
939 /* The MULTI_EXIT_DISC attribute received from a neighboring AS MUST NOT be
940 * propagated to other neighboring ASes.
941 * Perhaps it would be better to undefine it.
942 */
943 a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
944 if (a)
945 bgp_attach_attr(attrs, pool, BA_MULTI_EXIT_DISC, 0);
946 }
947
948 /* iBGP -> keep next_hop, eBGP multi-hop -> use source_addr,
949 eBGP single-hop -> keep next_hop if on the same iface */
950 a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_NEXT_HOP));
951 if (a && !p->cf->next_hop_self &&
952 (p->is_internal || (p->neigh && (e->attrs->iface == p->neigh->iface))))
953 {
954 /* Leave the original next hop attribute, will check later where does it point */
955 }
956 else
957 {
958 /* Need to create new one */
959 byte *b = bgp_attach_attr_wa(attrs, pool, BA_NEXT_HOP, NEXT_HOP_LENGTH);
960 set_next_hop(b, p->source_addr);
961 }
962
963 if (rr)
964 {
965 /* Handling route reflection, RFC 4456 */
966 struct bgp_proto *src = (struct bgp_proto *) e->attrs->proto;
967
968 a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
969 if (!a)
970 bgp_attach_attr(attrs, pool, BA_ORIGINATOR_ID, src->remote_id);
971
972 /* We attach proper cluster ID according to whether the route is entering or leaving the cluster */
973 bgp_cluster_list_prepend(e, attrs, pool, src->rr_client ? src->rr_cluster_id : p->rr_cluster_id);
974
975 /* Two RR clients with different cluster ID, hmmm */
976 if (src->rr_client && p->rr_client && (src->rr_cluster_id != p->rr_cluster_id))
977 bgp_cluster_list_prepend(e, attrs, pool, p->rr_cluster_id);
978 }
979
980 return 0; /* Leave decision to the filters */
981 }
982
983 static int
984 bgp_community_filter(struct bgp_proto *p, rte *e)
985 {
986 eattr *a;
987 struct adata *d;
988
989 /* Check if we aren't forbidden to export the route by communities */
990 a = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_COMMUNITY));
991 if (a)
992 {
993 d = a->u.ptr;
994 if (int_set_contains(d, BGP_COMM_NO_ADVERTISE))
995 {
996 DBG("\tNO_ADVERTISE\n");
997 return 1;
998 }
999 if (!p->is_internal &&
1000 (int_set_contains(d, BGP_COMM_NO_EXPORT) ||
1001 int_set_contains(d, BGP_COMM_NO_EXPORT_SUBCONFED)))
1002 {
1003 DBG("\tNO_EXPORT\n");
1004 return 1;
1005 }
1006 }
1007
1008 return 0;
1009 }
1010
1011 int
1012 bgp_import_control(struct proto *P, rte **new, ea_list **attrs, struct linpool *pool)
1013 {
1014 rte *e = *new;
1015 struct bgp_proto *p = (struct bgp_proto *) P;
1016 struct bgp_proto *new_bgp = (e->attrs->proto->proto == &proto_bgp) ? (struct bgp_proto *) e->attrs->proto : NULL;
1017
1018 if (p == new_bgp) /* Poison reverse updates */
1019 return -1;
1020 if (new_bgp)
1021 {
1022 /* We should check here for cluster list loop, because the receiving BGP instance
1023 might have different cluster ID */
1024 if (bgp_cluster_list_loopy(p, e->attrs))
1025 return -1;
1026
1027 if (p->cf->interpret_communities && bgp_community_filter(p, e))
1028 return -1;
1029
1030 if (p->local_as == new_bgp->local_as && p->is_internal && new_bgp->is_internal)
1031 {
1032 /* Redistribution of internal routes with IBGP */
1033 if (p->rr_client || new_bgp->rr_client)
1034 /* Route reflection, RFC 4456 */
1035 return bgp_update_attrs(p, e, attrs, pool, 1);
1036 else
1037 return -1;
1038 }
1039 else
1040 return bgp_update_attrs(p, e, attrs, pool, 0);
1041 }
1042 else
1043 return bgp_create_attrs(p, e, attrs, pool);
1044 }
1045
1046 static inline u32
1047 bgp_get_neighbor(rte *r)
1048 {
1049 eattr *e = ea_find(r->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1050 u32 as;
1051
1052 if (e && as_path_get_first(e->u.ptr, &as))
1053 return as;
1054 else
1055 return ((struct bgp_proto *) r->attrs->proto)->remote_as;
1056 }
1057
1058 static inline int
1059 rte_resolvable(rte *rt)
1060 {
1061 int rd = rt->attrs->dest;
1062 return (rd == RTD_ROUTER) || (rd == RTD_DEVICE) || (rd == RTD_MULTIPATH);
1063 }
1064
1065 int
1066 bgp_rte_better(rte *new, rte *old)
1067 {
1068 struct bgp_proto *new_bgp = (struct bgp_proto *) new->attrs->proto;
1069 struct bgp_proto *old_bgp = (struct bgp_proto *) old->attrs->proto;
1070 eattr *x, *y;
1071 u32 n, o;
1072
1073 /* RFC 4271 9.1.2.1. Route resolvability test */
1074 n = rte_resolvable(new);
1075 o = rte_resolvable(old);
1076 if (n > o)
1077 return 1;
1078 if (n < o)
1079 return 0;
1080
1081 /* Start with local preferences */
1082 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_LOCAL_PREF));
1083 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_LOCAL_PREF));
1084 n = x ? x->u.data : new_bgp->cf->default_local_pref;
1085 o = y ? y->u.data : old_bgp->cf->default_local_pref;
1086 if (n > o)
1087 return 1;
1088 if (n < o)
1089 return 0;
1090
1091 /* RFC 4271 9.1.2.2. a) Use AS path lengths */
1092 if (new_bgp->cf->compare_path_lengths || old_bgp->cf->compare_path_lengths)
1093 {
1094 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1095 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1096 n = x ? as_path_getlen(x->u.ptr) : AS_PATH_MAXLEN;
1097 o = y ? as_path_getlen(y->u.ptr) : AS_PATH_MAXLEN;
1098 if (n < o)
1099 return 1;
1100 if (n > o)
1101 return 0;
1102 }
1103
1104 /* RFC 4271 9.1.2.2. b) Use origins */
1105 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1106 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1107 n = x ? x->u.data : ORIGIN_INCOMPLETE;
1108 o = y ? y->u.data : ORIGIN_INCOMPLETE;
1109 if (n < o)
1110 return 1;
1111 if (n > o)
1112 return 0;
1113
1114 /* RFC 4271 9.1.2.2. c) Compare MED's */
1115 /* This is noncompliant. Proper RFC 4271 path selection cannot be
1116 * interpreted as finding the best path in some ordering.
1117 * Therefore, it cannot be implemented in BIRD without some ugly
1118 * hacks. This is just an approximation, which in specific
1119 * situations may lead to persistent routing loops, because it is
1120 * nondeterministic - it depends on the order in which routes
1121 * appeared. But it is also the same behavior as used by default in
1122 * Cisco routers, so it is probably not a big issue.
1123 */
1124 if (new_bgp->cf->med_metric || old_bgp->cf->med_metric ||
1125 (bgp_get_neighbor(new) == bgp_get_neighbor(old)))
1126 {
1127 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
1128 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
1129 n = x ? x->u.data : new_bgp->cf->default_med;
1130 o = y ? y->u.data : old_bgp->cf->default_med;
1131 if (n < o)
1132 return 1;
1133 if (n > o)
1134 return 0;
1135 }
1136
1137 /* RFC 4271 9.1.2.2. d) Prefer external peers */
1138 if (new_bgp->is_internal > old_bgp->is_internal)
1139 return 0;
1140 if (new_bgp->is_internal < old_bgp->is_internal)
1141 return 1;
1142
1143 /* RFC 4271 9.1.2.2. e) Compare IGP metrics */
1144 n = new_bgp->cf->igp_metric ? new->attrs->igp_metric : 0;
1145 o = old_bgp->cf->igp_metric ? old->attrs->igp_metric : 0;
1146 if (n < o)
1147 return 1;
1148 if (n > o)
1149 return 0;
1150
1151 /* RFC 4271 9.1.2.2. f) Compare BGP identifiers */
1152 /* RFC 4456 9. a) Use ORIGINATOR_ID instead of local neighor ID */
1153 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
1154 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
1155 n = x ? x->u.data : new_bgp->remote_id;
1156 o = y ? y->u.data : old_bgp->remote_id;
1157
1158 /* RFC 5004 - prefer older routes */
1159 /* (if both are external and from different peer) */
1160 if ((new_bgp->cf->prefer_older || old_bgp->cf->prefer_older) &&
1161 !new_bgp->is_internal && n != o)
1162 return 0;
1163
1164 /* rest of RFC 4271 9.1.2.2. f) */
1165 if (n < o)
1166 return 1;
1167 if (n > o)
1168 return 0;
1169
1170 /* RFC 4456 9. b) Compare cluster list lengths */
1171 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
1172 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
1173 n = x ? int_set_get_size(x->u.ptr) : 0;
1174 o = y ? int_set_get_size(y->u.ptr) : 0;
1175 if (n < o)
1176 return 1;
1177 if (n > o)
1178 return 0;
1179
1180 /* RFC 4271 9.1.2.2. g) Compare peer IP adresses */
1181 return (ipa_compare(new_bgp->cf->remote_ip, old_bgp->cf->remote_ip) < 0);
1182 }
1183
1184 static struct adata *
1185 bgp_aggregator_convert_to_new(struct adata *old, struct linpool *pool)
1186 {
1187 struct adata *newa = lp_alloc(pool, sizeof(struct adata) + 8);
1188 newa->length = 8;
1189 aggregator_convert_to_new(old, newa->data);
1190 return newa;
1191 }
1192
1193
1194 /* Take last req_as ASNs from path old2 (in 2B format), convert to 4B format
1195 * and append path old4 (in 4B format).
1196 */
1197 static struct adata *
1198 bgp_merge_as_paths(struct adata *old2, struct adata *old4, int req_as, struct linpool *pool)
1199 {
1200 byte buf[old2->length * 2];
1201
1202 int ol = as_path_convert_to_new(old2, buf, req_as);
1203 int nl = ol + (old4 ? old4->length : 0);
1204
1205 struct adata *newa = lp_alloc(pool, sizeof(struct adata) + nl);
1206 newa->length = nl;
1207 memcpy(newa->data, buf, ol);
1208 if (old4) memcpy(newa->data + ol, old4->data, old4->length);
1209
1210 return newa;
1211 }
1212
1213 static int
1214 as4_aggregator_valid(struct adata *aggr)
1215 {
1216 return aggr->length == 8;
1217 }
1218
1219
1220 /* Reconstruct 4B AS_PATH and AGGREGATOR according to RFC 4893 4.2.3 */
1221 static void
1222 bgp_reconstruct_4b_atts(struct bgp_proto *p, rta *a, struct linpool *pool)
1223 {
1224 eattr *p2 =ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1225 eattr *p4 =ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AS4_PATH));
1226 eattr *a2 =ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AGGREGATOR));
1227 eattr *a4 =ea_find(a->eattrs, EA_CODE(EAP_BGP, BA_AS4_AGGREGATOR));
1228 int a4_removed = 0;
1229
1230 if (a4 && !as4_aggregator_valid(a4->u.ptr))
1231 {
1232 log(L_WARN "%s: AS4_AGGREGATOR attribute is invalid, skipping attribute", p->p.name);
1233 a4 = NULL;
1234 a4_removed = 1;
1235 }
1236
1237 if (a2)
1238 {
1239 u32 a2_as = get_u16(a2->u.ptr->data);
1240
1241 if (a4)
1242 {
1243 if (a2_as != AS_TRANS)
1244 {
1245 /* Routes were aggregated by old router and therefore AS4_PATH
1246 * and AS4_AGGREGATOR is invalid
1247 *
1248 * Convert AS_PATH and AGGREGATOR to 4B format and finish.
1249 */
1250
1251 a2->u.ptr = bgp_aggregator_convert_to_new(a2->u.ptr, pool);
1252 p2->u.ptr = bgp_merge_as_paths(p2->u.ptr, NULL, AS_PATH_MAXLEN, pool);
1253
1254 return;
1255 }
1256 else
1257 {
1258 /* Common case, use AS4_AGGREGATOR attribute */
1259 a2->u.ptr = a4->u.ptr;
1260 }
1261 }
1262 else
1263 {
1264 /* Common case, use old AGGREGATOR attribute */
1265 a2->u.ptr = bgp_aggregator_convert_to_new(a2->u.ptr, pool);
1266
1267 if ((a2_as == AS_TRANS) && !a4_removed)
1268 log(L_WARN "%s: AGGREGATOR attribute contain AS_TRANS, but AS4_AGGREGATOR is missing", p->p.name);
1269 }
1270 }
1271 else
1272 if (a4)
1273 log(L_WARN "%s: AS4_AGGREGATOR attribute received, but AGGREGATOR attribute is missing", p->p.name);
1274
1275 int p2_len = as_path_getlen_int(p2->u.ptr, 2);
1276 int p4_len = p4 ? validate_as4_path(p, p4->u.ptr) : -1;
1277
1278 if (p4 && (p4_len < 0))
1279 log(L_WARN "%s: AS4_PATH attribute is malformed, skipping attribute", p->p.name);
1280
1281 if ((p4_len <= 0) || (p2_len < p4_len))
1282 p2->u.ptr = bgp_merge_as_paths(p2->u.ptr, NULL, AS_PATH_MAXLEN, pool);
1283 else
1284 p2->u.ptr = bgp_merge_as_paths(p2->u.ptr, p4->u.ptr, p2_len - p4_len, pool);
1285 }
1286
1287 static void
1288 bgp_remove_as4_attrs(struct bgp_proto *p, rta *a)
1289 {
1290 unsigned id1 = EA_CODE(EAP_BGP, BA_AS4_PATH);
1291 unsigned id2 = EA_CODE(EAP_BGP, BA_AS4_AGGREGATOR);
1292 ea_list **el = &(a->eattrs);
1293
1294 /* We know that ea_lists constructed in bgp_decode attrs have one attribute per ea_list struct */
1295 while (*el != NULL)
1296 {
1297 unsigned fid = (*el)->attrs[0].id;
1298
1299 if ((fid == id1) || (fid == id2))
1300 {
1301 *el = (*el)->next;
1302 if (p->as4_session)
1303 log(L_WARN "%s: Unexpected AS4_* attributes received", p->p.name);
1304 }
1305 else
1306 el = &((*el)->next);
1307 }
1308 }
1309
1310 /**
1311 * bgp_decode_attrs - check and decode BGP attributes
1312 * @conn: connection
1313 * @attr: start of attribute block
1314 * @len: length of attribute block
1315 * @pool: linear pool to make all the allocations in
1316 * @mandatory: 1 iff presence of mandatory attributes has to be checked
1317 *
1318 * This function takes a BGP attribute block (a part of an Update message), checks
1319 * its consistency and converts it to a list of BIRD route attributes represented
1320 * by a &rta.
1321 */
1322 struct rta *
1323 bgp_decode_attrs(struct bgp_conn *conn, byte *attr, unsigned int len, struct linpool *pool, int mandatory)
1324 {
1325 struct bgp_proto *bgp = conn->bgp;
1326 rta *a = lp_alloc(pool, sizeof(struct rta));
1327 unsigned int flags, code, l, i, type;
1328 int errcode;
1329 byte *z, *attr_start;
1330 byte seen[256/8];
1331 ea_list *ea;
1332 struct adata *ad;
1333 int withdraw = 0;
1334
1335 bzero(a, sizeof(rta));
1336 a->proto = &bgp->p;
1337 a->source = RTS_BGP;
1338 a->scope = SCOPE_UNIVERSE;
1339 a->cast = RTC_UNICAST;
1340 /* a->dest = RTD_ROUTER; -- set in bgp_set_next_hop() */
1341 a->from = bgp->cf->remote_ip;
1342
1343 /* Parse the attributes */
1344 bzero(seen, sizeof(seen));
1345 DBG("BGP: Parsing attributes\n");
1346 while (len)
1347 {
1348 if (len < 2)
1349 goto malformed;
1350 attr_start = attr;
1351 flags = *attr++;
1352 code = *attr++;
1353 len -= 2;
1354 if (flags & BAF_EXT_LEN)
1355 {
1356 if (len < 2)
1357 goto malformed;
1358 l = get_u16(attr);
1359 attr += 2;
1360 len -= 2;
1361 }
1362 else
1363 {
1364 if (len < 1)
1365 goto malformed;
1366 l = *attr++;
1367 len--;
1368 }
1369 if (l > len)
1370 goto malformed;
1371 len -= l;
1372 z = attr;
1373 attr += l;
1374 DBG("Attr %02x %02x %d\n", code, flags, l);
1375 if (seen[code/8] & (1 << (code%8)))
1376 goto malformed;
1377 if (ATTR_KNOWN(code))
1378 {
1379 struct attr_desc *desc = &bgp_attr_table[code];
1380 if (desc->expected_length >= 0 && desc->expected_length != (int) l)
1381 { errcode = 5; goto err; }
1382 if ((desc->expected_flags ^ flags) & (BAF_OPTIONAL | BAF_TRANSITIVE))
1383 { errcode = 4; goto err; }
1384 if (!desc->allow_in_ebgp && !bgp->is_internal)
1385 continue;
1386 if (desc->validate)
1387 {
1388 errcode = desc->validate(bgp, z, l);
1389 if (errcode > 0)
1390 goto err;
1391 if (errcode == IGNORE)
1392 continue;
1393 if (errcode <= WITHDRAW)
1394 {
1395 log(L_WARN "%s: Attribute %s is malformed, withdrawing update",
1396 bgp->p.name, desc->name);
1397 withdraw = 1;
1398 }
1399 }
1400 else if (code == BA_AS_PATH)
1401 {
1402 /* Special case as it might also trim the attribute */
1403 if (validate_as_path(bgp, z, &l) < 0)
1404 { errcode = 11; goto err; }
1405 }
1406 type = desc->type;
1407 }
1408 else /* Unknown attribute */
1409 {
1410 if (!(flags & BAF_OPTIONAL))
1411 { errcode = 2; goto err; }
1412 type = EAF_TYPE_OPAQUE;
1413 }
1414
1415 // Only OPTIONAL and TRANSITIVE attributes may have non-zero PARTIAL flag
1416 // if (!((flags & BAF_OPTIONAL) && (flags & BAF_TRANSITIVE)) && (flags & BAF_PARTIAL))
1417 // { errcode = 4; goto err; }
1418
1419 seen[code/8] |= (1 << (code%8));
1420 ea = lp_alloc(pool, sizeof(ea_list) + sizeof(eattr));
1421 ea->next = a->eattrs;
1422 a->eattrs = ea;
1423 ea->flags = 0;
1424 ea->count = 1;
1425 ea->attrs[0].id = EA_CODE(EAP_BGP, code);
1426 ea->attrs[0].flags = flags;
1427 ea->attrs[0].type = type;
1428 if (type & EAF_EMBEDDED)
1429 ad = NULL;
1430 else
1431 {
1432 ad = lp_alloc(pool, sizeof(struct adata) + l);
1433 ea->attrs[0].u.ptr = ad;
1434 ad->length = l;
1435 memcpy(ad->data, z, l);
1436 }
1437 switch (type)
1438 {
1439 case EAF_TYPE_ROUTER_ID:
1440 case EAF_TYPE_INT:
1441 if (l == 1)
1442 ea->attrs[0].u.data = *z;
1443 else
1444 ea->attrs[0].u.data = get_u32(z);
1445 break;
1446 case EAF_TYPE_IP_ADDRESS:
1447 ipa_ntoh(*(ip_addr *)ad->data);
1448 break;
1449 case EAF_TYPE_INT_SET:
1450 {
1451 u32 *z = (u32 *) ad->data;
1452 for(i=0; i<ad->length/4; i++)
1453 z[i] = ntohl(z[i]);
1454 break;
1455 }
1456 }
1457 }
1458
1459 if (withdraw)
1460 goto withdraw;
1461
1462 #ifdef IPV6
1463 /* If we received MP_REACH_NLRI we should check mandatory attributes */
1464 if (bgp->mp_reach_len != 0)
1465 mandatory = 1;
1466 #endif
1467
1468 /* If there is no (reachability) NLRI, we should exit now */
1469 if (! mandatory)
1470 return a;
1471
1472 /* Check if all mandatory attributes are present */
1473 for(i=0; i < ARRAY_SIZE(bgp_mandatory_attrs); i++)
1474 {
1475 code = bgp_mandatory_attrs[i];
1476 if (!(seen[code/8] & (1 << (code%8))))
1477 {
1478 bgp_error(conn, 3, 3, &bgp_mandatory_attrs[i], 1);
1479 return NULL;
1480 }
1481 }
1482
1483 /* When receiving attributes from non-AS4-aware BGP speaker,
1484 * we have to reconstruct 4B AS_PATH and AGGREGATOR attributes
1485 */
1486 if (! bgp->as4_session)
1487 bgp_reconstruct_4b_atts(bgp, a, pool);
1488
1489 bgp_remove_as4_attrs(bgp, a);
1490
1491 /* If the AS path attribute contains our AS, reject the routes */
1492 if (bgp_as_path_loopy(bgp, a))
1493 goto withdraw;
1494
1495 /* Two checks for IBGP loops caused by route reflection, RFC 4456 */
1496 if (bgp_originator_id_loopy(bgp, a) ||
1497 bgp_cluster_list_loopy(bgp, a))
1498 goto withdraw;
1499
1500 /* If there's no local preference, define one */
1501 if (!(seen[0] & (1 << BA_LOCAL_PREF)))
1502 bgp_attach_attr(&a->eattrs, pool, BA_LOCAL_PREF, bgp->cf->default_local_pref);
1503
1504 return a;
1505
1506 withdraw:
1507 return NULL;
1508
1509 malformed:
1510 bgp_error(conn, 3, 1, NULL, 0);
1511 return NULL;
1512
1513 err:
1514 bgp_error(conn, 3, errcode, attr_start, z+l-attr_start);
1515 return NULL;
1516 }
1517
1518 int
1519 bgp_get_attr(eattr *a, byte *buf, int buflen)
1520 {
1521 unsigned int i = EA_ID(a->id);
1522 struct attr_desc *d;
1523
1524 if (ATTR_KNOWN(i))
1525 {
1526 d = &bgp_attr_table[i];
1527 buf += bsprintf(buf, "%s", d->name);
1528 if (d->format)
1529 {
1530 *buf++ = ':';
1531 *buf++ = ' ';
1532 d->format(a, buf, buflen);
1533 return GA_FULL;
1534 }
1535 return GA_NAME;
1536 }
1537 bsprintf(buf, "%02x%s", i, (a->flags & BAF_TRANSITIVE) ? " [t]" : "");
1538 return GA_NAME;
1539 }
1540
1541 void
1542 bgp_attr_init(struct bgp_proto *p)
1543 {
1544 p->hash_size = 256;
1545 p->hash_limit = p->hash_size * 4;
1546 p->bucket_hash = mb_allocz(p->p.pool, p->hash_size * sizeof(struct bgp_bucket *));
1547 init_list(&p->bucket_queue);
1548 p->withdraw_bucket = NULL;
1549 fib_init(&p->prefix_fib, p->p.pool, sizeof(struct bgp_prefix), 0, bgp_init_prefix);
1550 }
1551
1552 void
1553 bgp_get_route_info(rte *e, byte *buf, ea_list *attrs)
1554 {
1555 eattr *p = ea_find(attrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1556 eattr *o = ea_find(attrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1557 u32 origas;
1558
1559 buf += bsprintf(buf, " (%d", e->pref);
1560 if (e->attrs->hostentry)
1561 {
1562 if (!rte_resolvable(e))
1563 buf += bsprintf(buf, "/-");
1564 else if (e->attrs->igp_metric >= IGP_METRIC_UNKNOWN)
1565 buf += bsprintf(buf, "/?");
1566 else
1567 buf += bsprintf(buf, "/%d", e->attrs->igp_metric);
1568 }
1569 buf += bsprintf(buf, ") [");
1570
1571 if (p && as_path_get_last(p->u.ptr, &origas))
1572 buf += bsprintf(buf, "AS%u", origas);
1573 if (o)
1574 buf += bsprintf(buf, "%c", "ie?"[o->u.data]);
1575 strcpy(buf, "]");
1576 }