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