2 * BIRD -- BGP Attributes
4 * (c) 2000 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
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"
26 * UPDATE message error handling
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.
34 * Loop detection based on AS_PATH causes updates to be withdrawn. RFC
35 * 4271 does not explicitly specifiy the behavior in that case.
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
42 * Error checking of optional transitive attributes is done according to
43 * draft-ietf-idr-optional-transitive-03, but errors are handled always
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
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()).
62 static byte bgp_mandatory_attrs
[] = { BA_ORIGIN
, BA_AS_PATH
74 int (*validate
)(struct bgp_proto
*p
, byte
*attr
, int len
);
75 void (*format
)(eattr
*ea
, byte
*buf
, int buflen
);
82 bgp_check_origin(struct bgp_proto
*p UNUSED
, byte
*a
, int len UNUSED
)
90 bgp_format_origin(eattr
*a
, byte
*buf
, int buflen UNUSED
)
92 static char *bgp_origin_names
[] = { "IGP", "EGP", "Incomplete" };
94 bsprintf(buf
, bgp_origin_names
[a
->u
.data
]);
98 path_segment_contains(byte
*p
, int bs
, u32 asn
)
106 u32 asn2
= (bs
== 4) ? get_u32(p
) : get_u16(p
);
115 /* Validates path attribute, removes AS_CONFED_* segments, and also returns path length */
117 validate_path(struct bgp_proto
*p
, int as_path
, int bs
, byte
*idata
, uint
*ilength
)
131 plen
= 2 + bs
* a
[1];
137 log(L_WARN
"%s: %s_PATH attribute contains empty segment, skipping it",
138 p
->p
.name
, as_path
? "AS" : "AS4");
148 case AS_PATH_SEQUENCE
:
152 case AS_PATH_CONFED_SEQUENCE
:
153 case AS_PATH_CONFED_SET
:
154 if (as_path
&& path_segment_contains(a
, bs
, p
->remote_as
))
156 log(L_WARN
"%s: AS_CONFED_* segment with peer ASN found, misconfigured confederation?", p
->p
.name
);
160 log(L_WARN
"%s: %s_PATH attribute contains AS_CONFED_* segment, skipping segment",
161 p
->p
.name
, as_path
? "AS" : "AS4");
169 memmove(dst
, a
, plen
);
177 *ilength
= dst
- idata
;
182 validate_as_path(struct bgp_proto
*p
, byte
*a
, int *len
)
184 return validate_path(p
, 1, p
->as4_session
? 4 : 2, a
, len
);
188 validate_as4_path(struct bgp_proto
*p
, struct adata
*path
)
190 return validate_path(p
, 0, 4, path
->data
, &path
->length
);
194 bgp_check_next_hop(struct bgp_proto
*p UNUSED
, byte
*a UNUSED6
, int len UNUSED6
)
201 memcpy(&addr
, a
, len
);
203 if (ipa_classify(addr
) & IADDR_HOST
)
211 bgp_format_next_hop(eattr
*a
, byte
*buf
, int buflen UNUSED
)
213 ip_addr
*ipp
= (ip_addr
*) a
->u
.ptr
->data
;
215 /* in IPv6, we might have two addresses in NEXT HOP */
216 if ((a
->u
.ptr
->length
== NEXT_HOP_LENGTH
) && ipa_nonzero(ipp
[1]))
218 bsprintf(buf
, "%I %I", ipp
[0], ipp
[1]);
223 bsprintf(buf
, "%I", ipp
[0]);
227 bgp_check_aggregator(struct bgp_proto
*p
, byte
*a UNUSED
, int len
)
229 int exp_len
= p
->as4_session
? 8 : 6;
231 return (len
== exp_len
) ? 0 : WITHDRAW
;
235 bgp_format_aggregator(eattr
*a
, byte
*buf
, int buflen UNUSED
)
237 struct adata
*ad
= a
->u
.ptr
;
238 byte
*data
= ad
->data
;
244 bsprintf(buf
, "%d.%d.%d.%d AS%u", data
[0], data
[1], data
[2], data
[3], as
);
248 bgp_check_community(struct bgp_proto
*p UNUSED
, byte
*a UNUSED
, int len
)
250 return ((len
% 4) == 0) ? 0 : WITHDRAW
;
254 bgp_check_cluster_list(struct bgp_proto
*p UNUSED
, byte
*a UNUSED
, int len
)
256 return ((len
% 4) == 0) ? 0 : 5;
260 bgp_format_cluster_list(eattr
*a
, byte
*buf
, int buflen
)
262 /* Truncates cluster lists larger than buflen, probably not a problem */
263 int_set_format(a
->u
.ptr
, 0, -1, buf
, buflen
);
267 bgp_check_reach_nlri(struct bgp_proto
*p UNUSED
, byte
*a UNUSED
, int len UNUSED
)
270 p
->mp_reach_start
= a
;
271 p
->mp_reach_len
= len
;
277 bgp_check_unreach_nlri(struct bgp_proto
*p UNUSED
, byte
*a UNUSED
, int len UNUSED
)
280 p
->mp_unreach_start
= a
;
281 p
->mp_unreach_len
= len
;
287 bgp_check_ext_community(struct bgp_proto
*p UNUSED
, byte
*a UNUSED
, int len
)
289 return ((len
% 8) == 0) ? 0 : WITHDRAW
;
293 bgp_check_large_community(struct bgp_proto
*p UNUSED
, byte
*a UNUSED
, int len
)
295 return ((len
% 12) == 0) ? 0 : WITHDRAW
;
299 static struct attr_desc bgp_attr_table
[] = {
300 { NULL
, -1, 0, 0, 0, /* Undefined */
302 { "origin", 1, BAF_TRANSITIVE
, EAF_TYPE_INT
, 1, /* BA_ORIGIN */
303 bgp_check_origin
, bgp_format_origin
},
304 { "as_path", -1, BAF_TRANSITIVE
, EAF_TYPE_AS_PATH
, 1, /* BA_AS_PATH */
305 NULL
, NULL
}, /* is checked by validate_as_path() as a special case */
306 { "next_hop", 4, BAF_TRANSITIVE
, EAF_TYPE_IP_ADDRESS
, 1, /* BA_NEXT_HOP */
307 bgp_check_next_hop
, bgp_format_next_hop
},
308 { "med", 4, BAF_OPTIONAL
, EAF_TYPE_INT
, 1, /* BA_MULTI_EXIT_DISC */
310 { "local_pref", 4, BAF_TRANSITIVE
, EAF_TYPE_INT
, 0, /* BA_LOCAL_PREF */
312 { "atomic_aggr", 0, BAF_TRANSITIVE
, EAF_TYPE_OPAQUE
, 1, /* BA_ATOMIC_AGGR */
314 { "aggregator", -1, BAF_OPTIONAL
| BAF_TRANSITIVE
, EAF_TYPE_OPAQUE
, 1, /* BA_AGGREGATOR */
315 bgp_check_aggregator
, bgp_format_aggregator
},
316 { "community", -1, BAF_OPTIONAL
| BAF_TRANSITIVE
, EAF_TYPE_INT_SET
, 1, /* BA_COMMUNITY */
317 bgp_check_community
, NULL
},
318 { "originator_id", 4, BAF_OPTIONAL
, EAF_TYPE_ROUTER_ID
, 0, /* BA_ORIGINATOR_ID */
320 { "cluster_list", -1, BAF_OPTIONAL
, EAF_TYPE_INT_SET
, 0, /* BA_CLUSTER_LIST */
321 bgp_check_cluster_list
, bgp_format_cluster_list
},
322 { .name
= NULL
}, /* BA_DPA */
323 { .name
= NULL
}, /* BA_ADVERTISER */
324 { .name
= NULL
}, /* BA_RCID_PATH */
325 { "mp_reach_nlri", -1, BAF_OPTIONAL
, EAF_TYPE_OPAQUE
, 1, /* BA_MP_REACH_NLRI */
326 bgp_check_reach_nlri
, NULL
},
327 { "mp_unreach_nlri", -1, BAF_OPTIONAL
, EAF_TYPE_OPAQUE
, 1, /* BA_MP_UNREACH_NLRI */
328 bgp_check_unreach_nlri
, NULL
},
329 { "ext_community", -1, BAF_OPTIONAL
| BAF_TRANSITIVE
, EAF_TYPE_EC_SET
, 1, /* BA_EXT_COMMUNITY */
330 bgp_check_ext_community
, NULL
},
331 { "as4_path", -1, BAF_OPTIONAL
| BAF_TRANSITIVE
, EAF_TYPE_OPAQUE
, 1, /* BA_AS4_PATH */
333 { "as4_aggregator", -1, BAF_OPTIONAL
| BAF_TRANSITIVE
, EAF_TYPE_OPAQUE
, 1, /* BA_AS4_PATH */
335 [BA_LARGE_COMMUNITY
] =
336 { "large_community", -1, BAF_OPTIONAL
| BAF_TRANSITIVE
, EAF_TYPE_LC_SET
, 1,
337 bgp_check_large_community
, NULL
}
340 /* BA_AS4_PATH is type EAF_TYPE_OPAQUE and not type EAF_TYPE_AS_PATH.
341 * It does not matter as this attribute does not appear on routes in the routing table.
344 #define ATTR_KNOWN(code) ((code) < ARRAY_SIZE(bgp_attr_table) && bgp_attr_table[code].name)
346 static inline struct adata
*
347 bgp_alloc_adata(struct linpool
*pool
, unsigned len
)
349 struct adata
*ad
= lp_alloc(pool
, sizeof(struct adata
) + len
);
355 bgp_set_attr(eattr
*e
, unsigned attr
, uintptr_t val
)
357 ASSERT(ATTR_KNOWN(attr
));
358 e
->id
= EA_CODE(EAP_BGP
, attr
);
359 e
->type
= bgp_attr_table
[attr
].type
;
360 e
->flags
= bgp_attr_table
[attr
].expected_flags
;
361 if (e
->type
& EAF_EMBEDDED
)
364 e
->u
.ptr
= (struct adata
*) val
;
368 bgp_set_attr_wa(eattr
*e
, struct linpool
*pool
, unsigned attr
, unsigned len
)
370 struct adata
*ad
= bgp_alloc_adata(pool
, len
);
371 bgp_set_attr(e
, attr
, (uintptr_t) ad
);
376 bgp_attach_attr(ea_list
**to
, struct linpool
*pool
, unsigned attr
, uintptr_t val
)
378 ea_list
*a
= lp_alloc(pool
, sizeof(ea_list
) + sizeof(eattr
));
381 a
->flags
= EALF_SORTED
;
383 bgp_set_attr(a
->attrs
, attr
, val
);
387 bgp_attach_attr_wa(ea_list
**to
, struct linpool
*pool
, unsigned attr
, unsigned len
)
389 struct adata
*ad
= bgp_alloc_adata(pool
, len
);
390 bgp_attach_attr(to
, pool
, attr
, (uintptr_t) ad
);
395 bgp_encode_attr_hdr(byte
*dst
, uint flags
, unsigned code
, int len
)
399 DBG("\tAttribute %02x (%d bytes, flags %02x)\n", code
, len
, flags
);
410 *dst
++ = flags
| BAF_EXT_LEN
;
420 aggregator_convert_to_old(struct adata
*aggr
, byte
*dst
, int *new_used
)
422 byte
*src
= aggr
->data
;
425 u32 as
= get_u32(src
);
433 /* Copy IPv4 address */
434 memcpy(dst
+ 2, src
+ 4, 4);
438 aggregator_convert_to_new(struct adata
*aggr
, byte
*dst
)
440 byte
*src
= aggr
->data
;
442 u32 as
= get_u16(src
);
445 /* Copy IPv4 address */
446 memcpy(dst
+ 4, src
+ 2, 4);
450 bgp_get_attr_len(eattr
*a
)
453 if (ATTR_KNOWN(EA_ID(a
->id
)))
455 int code
= EA_ID(a
->id
);
456 struct attr_desc
*desc
= &bgp_attr_table
[code
];
457 len
= desc
->expected_length
;
460 ASSERT(!(a
->type
& EAF_EMBEDDED
));
461 len
= a
->u
.ptr
->length
;
466 ASSERT((a
->type
& EAF_TYPE_MASK
) == EAF_TYPE_OPAQUE
);
467 len
= a
->u
.ptr
->length
;
473 #define ADVANCE(w, r, l) do { r -= l; w += l; } while (0)
476 * bgp_encode_attrs - encode BGP attributes
479 * @attrs: a list of extended attributes
480 * @remains: remaining space in the buffer
482 * The bgp_encode_attrs() function takes a list of extended attributes
483 * and converts it to its BGP representation (a part of an Update message).
485 * Result: Length of the attribute block generated or -1 if not enough space.
488 bgp_encode_attrs(struct bgp_proto
*p
, byte
*w
, ea_list
*attrs
, int remains
)
490 uint i
, code
, type
, flags
;
494 for(i
=0; i
<attrs
->count
; i
++)
496 eattr
*a
= &attrs
->attrs
[i
];
497 ASSERT(EA_PROTO(a
->id
) == EAP_BGP
);
501 /* When talking multiprotocol BGP, the NEXT_HOP attributes are used only temporarily. */
502 if (code
== BA_NEXT_HOP
)
506 /* When AS4-aware BGP speaker is talking to non-AS4-aware BGP speaker,
507 * we have to convert our 4B AS_PATH to 2B AS_PATH and send our AS_PATH
508 * as optional AS4_PATH attribute.
510 if ((code
== BA_AS_PATH
) && (! p
->as4_session
))
512 len
= a
->u
.ptr
->length
;
514 if (remains
< (len
+ 4))
517 /* Using temporary buffer because don't know a length of created attr
518 * and therefore a length of a header. Perhaps i should better always
519 * use BAF_EXT_LEN. */
523 int nl
= as_path_convert_to_old(a
->u
.ptr
, buf
, &new_used
);
525 DBG("BGP: Encoding old AS_PATH\n");
526 rv
= bgp_encode_attr_hdr(w
, BAF_TRANSITIVE
, BA_AS_PATH
, nl
);
527 ADVANCE(w
, remains
, rv
);
529 ADVANCE(w
, remains
, nl
);
534 if (remains
< (len
+ 4))
537 /* We should discard AS_CONFED_SEQUENCE or AS_CONFED_SET path segments
538 * here but we don't support confederations and such paths we already
539 * discarded in bgp_check_as_path().
542 DBG("BGP: Encoding AS4_PATH\n");
543 rv
= bgp_encode_attr_hdr(w
, BAF_OPTIONAL
| BAF_TRANSITIVE
, BA_AS4_PATH
, len
);
544 ADVANCE(w
, remains
, rv
);
545 memcpy(w
, a
->u
.ptr
->data
, len
);
546 ADVANCE(w
, remains
, len
);
551 /* The same issue with AGGREGATOR attribute */
552 if ((code
== BA_AGGREGATOR
) && (! p
->as4_session
))
557 if (remains
< (len
+ 3))
560 rv
= bgp_encode_attr_hdr(w
, BAF_OPTIONAL
| BAF_TRANSITIVE
, BA_AGGREGATOR
, len
);
561 ADVANCE(w
, remains
, rv
);
562 aggregator_convert_to_old(a
->u
.ptr
, w
, &new_used
);
563 ADVANCE(w
, remains
, len
);
569 if (remains
< (len
+ 3))
572 rv
= bgp_encode_attr_hdr(w
, BAF_OPTIONAL
| BAF_TRANSITIVE
, BA_AS4_AGGREGATOR
, len
);
573 ADVANCE(w
, remains
, rv
);
574 memcpy(w
, a
->u
.ptr
->data
, len
);
575 ADVANCE(w
, remains
, len
);
580 /* Standard path continues here ... */
582 type
= a
->type
& EAF_TYPE_MASK
;
583 flags
= a
->flags
& (BAF_OPTIONAL
| BAF_TRANSITIVE
| BAF_PARTIAL
);
584 len
= bgp_get_attr_len(a
);
586 /* Skip empty sets */
587 if (((type
== EAF_TYPE_INT_SET
) || (type
== EAF_TYPE_EC_SET
) || (type
== EAF_TYPE_LC_SET
)) && (len
== 0))
590 if (remains
< len
+ 4)
593 rv
= bgp_encode_attr_hdr(w
, flags
, code
, len
);
594 ADVANCE(w
, remains
, rv
);
599 case EAF_TYPE_ROUTER_ID
:
601 put_u32(w
, a
->u
.data
);
605 case EAF_TYPE_IP_ADDRESS
:
607 ip_addr ip
= *(ip_addr
*)a
->u
.ptr
->data
;
612 case EAF_TYPE_INT_SET
:
613 case EAF_TYPE_LC_SET
:
614 case EAF_TYPE_EC_SET
:
616 u32
*z
= int_set_get_data(a
->u
.ptr
);
618 for(i
=0; i
<len
; i
+=4)
622 case EAF_TYPE_OPAQUE
:
623 case EAF_TYPE_AS_PATH
:
624 memcpy(w
, a
->u
.ptr
->data
, len
);
627 bug("bgp_encode_attrs: unknown attribute type %02x", a
->type
);
629 ADVANCE(w
, remains
, len
);
639 bgp_init_prefix(struct fib_node *N)
641 struct bgp_prefix *p = (struct bgp_prefix *) N;
642 p->bucket_node.next = NULL;
647 bgp_compare_u32(const u32
*x
, const u32
*y
)
649 return (*x
< *y
) ? -1 : (*x
> *y
) ? 1 : 0;
653 bgp_normalize_int_set(u32
*dest
, u32
*src
, unsigned cnt
)
655 memcpy(dest
, src
, sizeof(u32
) * cnt
);
656 qsort(dest
, cnt
, sizeof(u32
), (int(*)(const void *, const void *)) bgp_compare_u32
);
660 bgp_compare_ec(const u32
*xp
, const u32
*yp
)
662 u64 x
= ec_get(xp
, 0);
663 u64 y
= ec_get(yp
, 0);
664 return (x
< y
) ? -1 : (x
> y
) ? 1 : 0;
668 bgp_normalize_ec_set(struct adata
*ad
, u32
*src
, int internal
)
670 u32
*dst
= int_set_get_data(ad
);
672 /* Remove non-transitive communities (EC_TBIT active) on external sessions */
675 int len
= int_set_get_size(ad
);
679 for (i
=0; i
< len
; i
+= 2)
681 if (src
[i
] & EC_TBIT
)
688 ad
->length
= (t
- dst
) * 4;
691 memcpy(dst
, src
, ad
->length
);
693 qsort(dst
, ad
->length
/ 8, 8, (int(*)(const void *, const void *)) bgp_compare_ec
);
697 bgp_compare_lc(const u32
*x
, const u32
*y
)
700 return (x
[0] > y
[0]) ? 1 : -1;
702 return (x
[1] > y
[1]) ? 1 : -1;
704 return (x
[2] > y
[2]) ? 1 : -1;
709 bgp_normalize_lc_set(u32
*dest
, u32
*src
, unsigned cnt
)
711 memcpy(dest
, src
, LCOMM_LENGTH
* cnt
);
712 qsort(dest
, cnt
, LCOMM_LENGTH
, (int(*)(const void *, const void *)) bgp_compare_lc
);
716 bgp_rehash_buckets(struct bgp_proto
*p
)
718 struct bgp_bucket
**old
= p
->bucket_hash
;
719 struct bgp_bucket
**new;
720 unsigned oldn
= p
->hash_size
;
722 struct bgp_bucket
*b
;
724 p
->hash_size
= p
->hash_limit
;
725 DBG("BGP: Rehashing bucket table from %d to %d\n", oldn
, p
->hash_size
);
727 if (p
->hash_limit
>= 65536)
729 new = p
->bucket_hash
= mb_allocz(p
->p
.pool
, p
->hash_size
* sizeof(struct bgp_bucket
*));
730 mask
= p
->hash_size
- 1;
731 for (i
=0; i
<oldn
; i
++)
734 old
[i
] = b
->hash_next
;
736 b
->hash_next
= new[e
];
738 b
->hash_next
->hash_prev
= b
;
745 static struct bgp_bucket
*
746 bgp_new_bucket(struct bgp_proto
*p
, ea_list
*new, unsigned hash
)
748 struct bgp_bucket
*b
;
749 unsigned ea_size
= sizeof(ea_list
) + new->count
* sizeof(eattr
);
750 unsigned ea_size_aligned
= BIRD_ALIGN(ea_size
, CPU_STRUCT_ALIGN
);
751 unsigned size
= sizeof(struct bgp_bucket
) + ea_size_aligned
;
754 unsigned index
= hash
& (p
->hash_size
- 1);
756 /* Gather total size of non-inline attributes */
757 for (i
=0; i
<new->count
; i
++)
759 eattr
*a
= &new->attrs
[i
];
760 if (!(a
->type
& EAF_EMBEDDED
))
761 size
+= BIRD_ALIGN(sizeof(struct adata
) + a
->u
.ptr
->length
, CPU_STRUCT_ALIGN
);
764 /* Create the bucket and hash it */
765 b
= mb_alloc(p
->p
.pool
, size
);
766 b
->hash_next
= p
->bucket_hash
[index
];
768 b
->hash_next
->hash_prev
= b
;
769 p
->bucket_hash
[index
] = b
;
772 add_tail(&p
->bucket_queue
, &b
->send_node
);
773 init_list(&b
->prefixes
);
774 memcpy(b
->eattrs
, new, ea_size
);
775 dest
= ((byte
*)b
->eattrs
) + ea_size_aligned
;
777 /* Copy values of non-inline attributes */
778 for (i
=0; i
<new->count
; i
++)
780 eattr
*a
= &b
->eattrs
->attrs
[i
];
781 if (!(a
->type
& EAF_EMBEDDED
))
783 struct adata
*oa
= a
->u
.ptr
;
784 struct adata
*na
= (struct adata
*) dest
;
785 memcpy(na
, oa
, sizeof(struct adata
) + oa
->length
);
787 dest
+= BIRD_ALIGN(sizeof(struct adata
) + na
->length
, CPU_STRUCT_ALIGN
);
791 /* If needed, rehash */
793 if (p
->hash_count
> p
->hash_limit
)
794 bgp_rehash_buckets(p
);
799 static struct bgp_bucket
*
800 bgp_get_bucket(struct bgp_proto
*p
, net
*n
, ea_list
*attrs
, int originate
)
803 unsigned i
, cnt
, hash
, code
;
806 struct bgp_bucket
*b
;
808 /* Merge the attribute list */
809 new = alloca(ea_scan(attrs
));
810 ea_merge(attrs
, new);
813 /* Normalize attributes */
820 if (EA_PROTO(a
->id
) != EAP_BGP
)
823 if (ATTR_KNOWN(code
))
825 if (!bgp_attr_table
[code
].allow_in_ebgp
&& !p
->is_internal
)
827 /* The flags might have been zero if the attr was added by filters */
828 a
->flags
= (a
->flags
& BAF_PARTIAL
) | bgp_attr_table
[code
].expected_flags
;
834 /* Don't re-export unknown non-transitive attributes */
835 if (!(a
->flags
& BAF_TRANSITIVE
))
839 if ((d
->type
& EAF_ORIGINATED
) && !originate
&& (d
->flags
& BAF_TRANSITIVE
) && (d
->flags
& BAF_OPTIONAL
))
840 d
->flags
|= BAF_PARTIAL
;
841 switch (d
->type
& EAF_TYPE_MASK
)
843 case EAF_TYPE_INT_SET
:
845 struct adata
*z
= alloca(sizeof(struct adata
) + d
->u
.ptr
->length
);
846 z
->length
= d
->u
.ptr
->length
;
847 bgp_normalize_int_set((u32
*) z
->data
, (u32
*) d
->u
.ptr
->data
, z
->length
/ 4);
851 case EAF_TYPE_EC_SET
:
853 struct adata
*z
= alloca(sizeof(struct adata
) + d
->u
.ptr
->length
);
854 z
->length
= d
->u
.ptr
->length
;
855 bgp_normalize_ec_set(z
, (u32
*) d
->u
.ptr
->data
, p
->is_internal
);
859 case EAF_TYPE_LC_SET
:
861 struct adata
*z
= alloca(sizeof(struct adata
) + d
->u
.ptr
->length
);
862 z
->length
= d
->u
.ptr
->length
;
863 bgp_normalize_lc_set((u32
*) z
->data
, (u32
*) d
->u
.ptr
->data
, z
->length
/ LCOMM_LENGTH
);
875 for(b
=p
->bucket_hash
[hash
& (p
->hash_size
- 1)]; b
; b
=b
->hash_next
)
876 if (b
->hash
== hash
&& ea_same(b
->eattrs
, new))
878 DBG("Found bucket.\n");
882 /* Ensure that there are all mandatory attributes */
883 for(i
=0; i
<ARRAY_SIZE(bgp_mandatory_attrs
); i
++)
884 if (!(seen
& (1 << bgp_mandatory_attrs
[i
])))
886 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
);
890 /* Check if next hop is valid */
891 a
= ea_find(new, EA_CODE(EAP_BGP
, BA_NEXT_HOP
));
892 if (!a
|| ipa_equal(p
->cf
->remote_ip
, *(ip_addr
*)a
->u
.ptr
->data
))
894 log(L_ERR
"%s: Invalid NEXT_HOP attribute in route %I/%d", p
->p
.name
, n
->n
.prefix
, n
->n
.pxlen
);
898 /* Create new bucket */
899 DBG("Creating bucket.\n");
900 return bgp_new_bucket(p
, new, hash
);
904 bgp_free_bucket(struct bgp_proto
*p
, struct bgp_bucket
*buck
)
907 buck
->hash_next
->hash_prev
= buck
->hash_prev
;
909 buck
->hash_prev
->hash_next
= buck
->hash_next
;
911 p
->bucket_hash
[buck
->hash
& (p
->hash_size
-1)] = buck
->hash_next
;
916 /* Prefix hash table */
918 #define PXH_KEY(n1) n1->n.prefix, n1->n.pxlen, n1->path_id
919 #define PXH_NEXT(n) n->next
920 #define PXH_EQ(p1,l1,i1,p2,l2,i2) ipa_equal(p1, p2) && l1 == l2 && i1 == i2
921 #define PXH_FN(p,l,i) ipa_hash32(p) ^ u32_hash((l << 16) ^ i)
923 #define PXH_REHASH bgp_pxh_rehash
924 #define PXH_PARAMS /8, *2, 2, 2, 8, 20
927 HASH_DEFINE_REHASH_FN(PXH
, struct bgp_prefix
)
930 bgp_init_prefix_table(struct bgp_proto
*p
, u32 order
)
932 HASH_INIT(p
->prefix_hash
, p
->p
.pool
, order
);
934 p
->prefix_slab
= sl_new(p
->p
.pool
, sizeof(struct bgp_prefix
));
937 static struct bgp_prefix
*
938 bgp_get_prefix(struct bgp_proto
*p
, ip_addr prefix
, int pxlen
, u32 path_id
)
940 struct bgp_prefix
*bp
= HASH_FIND(p
->prefix_hash
, PXH
, prefix
, pxlen
, path_id
);
945 bp
= sl_alloc(p
->prefix_slab
);
946 bp
->n
.prefix
= prefix
;
948 bp
->path_id
= path_id
;
949 bp
->bucket_node
.next
= NULL
;
951 HASH_INSERT2(p
->prefix_hash
, PXH
, p
->p
.pool
, bp
);
957 bgp_free_prefix(struct bgp_proto
*p
, struct bgp_prefix
*bp
)
959 HASH_REMOVE2(p
->prefix_hash
, PXH
, p
->p
.pool
, bp
);
960 sl_free(p
->prefix_slab
, bp
);
965 bgp_rt_notify(struct proto
*P
, rtable
*tbl UNUSED
, net
*n
, rte
*new, rte
*old UNUSED
, ea_list
*attrs
)
967 struct bgp_proto
*p
= (struct bgp_proto
*) P
;
968 struct bgp_bucket
*buck
;
969 struct bgp_prefix
*px
;
973 DBG("BGP: Got route %I/%d %s\n", n
->n
.prefix
, n
->n
.pxlen
, new ? "up" : "down");
978 buck
= bgp_get_bucket(p
, n
, attrs
, new->attrs
->source
!= RTS_BGP
);
979 if (!buck
) /* Inconsistent attribute list */
985 if (!(buck
= p
->withdraw_bucket
))
987 buck
= p
->withdraw_bucket
= mb_alloc(P
->pool
, sizeof(struct bgp_bucket
));
988 init_list(&buck
->prefixes
);
991 path_id
= p
->add_path_tx
? key
->attrs
->src
->global_id
: 0;
992 px
= bgp_get_prefix(p
, n
->n
.prefix
, n
->n
.pxlen
, path_id
);
993 if (px
->bucket_node
.next
)
995 DBG("\tRemoving old entry.\n");
996 rem_node(&px
->bucket_node
);
998 add_tail(&buck
->prefixes
, &px
->bucket_node
);
999 bgp_schedule_packet(p
->conn
, PKT_UPDATE
);
1003 bgp_create_attrs(struct bgp_proto
*p
, rte
*e
, ea_list
**attrs
, struct linpool
*pool
)
1005 ea_list
*ea
= lp_alloc(pool
, sizeof(ea_list
) + 4*sizeof(eattr
));
1006 rta
*rta
= e
->attrs
;
1011 ea
->flags
= EALF_SORTED
;
1014 bgp_set_attr(ea
->attrs
, BA_ORIGIN
,
1015 ((rta
->source
== RTS_OSPF_EXT1
) || (rta
->source
== RTS_OSPF_EXT2
)) ? ORIGIN_INCOMPLETE
: ORIGIN_IGP
);
1018 bgp_set_attr_wa(ea
->attrs
+1, pool
, BA_AS_PATH
, 0);
1021 z
= bgp_set_attr_wa(ea
->attrs
+1, pool
, BA_AS_PATH
, 6);
1022 z
[0] = AS_PATH_SEQUENCE
;
1023 z
[1] = 1; /* 1 AS */
1024 put_u32(z
+2, p
->local_as
);
1027 /* iBGP -> use gw, eBGP multi-hop -> use source_addr,
1028 eBGP single-hop -> use gw if on the same iface */
1029 z
= bgp_set_attr_wa(ea
->attrs
+2, pool
, BA_NEXT_HOP
, NEXT_HOP_LENGTH
);
1030 if (p
->cf
->next_hop_self
||
1031 rta
->dest
!= RTD_ROUTER
||
1032 ipa_equal(rta
->gw
, IPA_NONE
) ||
1033 ipa_is_link_local(rta
->gw
) ||
1034 (!p
->is_internal
&& !p
->cf
->next_hop_keep
&&
1035 (!p
->neigh
|| (rta
->iface
!= p
->neigh
->iface
))))
1036 set_next_hop(z
, p
->source_addr
);
1038 set_next_hop(z
, rta
->gw
);
1040 bgp_set_attr(ea
->attrs
+3, BA_LOCAL_PREF
, p
->cf
->default_local_pref
);
1042 return 0; /* Leave decision to the filters */
1047 bgp_as_path_loopy(struct bgp_proto
*p
, rta
*a
)
1049 int num
= p
->cf
->allow_local_as
+ 1;
1050 eattr
*e
= ea_find(a
->eattrs
, EA_CODE(EAP_BGP
, BA_AS_PATH
));
1051 return (e
&& (num
> 0) && as_path_contains(e
->u
.ptr
, p
->local_as
, num
));
1055 bgp_originator_id_loopy(struct bgp_proto
*p
, rta
*a
)
1057 eattr
*e
= ea_find(a
->eattrs
, EA_CODE(EAP_BGP
, BA_ORIGINATOR_ID
));
1058 return (e
&& (e
->u
.data
== p
->local_id
));
1062 bgp_cluster_list_loopy(struct bgp_proto
*p
, rta
*a
)
1064 eattr
*e
= ea_find(a
->eattrs
, EA_CODE(EAP_BGP
, BA_CLUSTER_LIST
));
1065 return (e
&& p
->rr_client
&& int_set_contains(e
->u
.ptr
, p
->rr_cluster_id
));
1070 bgp_path_prepend(rte
*e
, ea_list
**attrs
, struct linpool
*pool
, u32 as
)
1072 eattr
*a
= ea_find(e
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_AS_PATH
));
1073 bgp_attach_attr(attrs
, pool
, BA_AS_PATH
, (uintptr_t) as_path_prepend(pool
, a
->u
.ptr
, as
));
1077 bgp_cluster_list_prepend(rte
*e
, ea_list
**attrs
, struct linpool
*pool
, u32 cid
)
1079 eattr
*a
= ea_find(e
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_CLUSTER_LIST
));
1080 bgp_attach_attr(attrs
, pool
, BA_CLUSTER_LIST
, (uintptr_t) int_set_add(pool
, a
? a
->u
.ptr
: NULL
, cid
));
1084 bgp_update_attrs(struct bgp_proto
*p
, rte
*e
, ea_list
**attrs
, struct linpool
*pool
, int rr
)
1088 if (!p
->is_internal
&& !p
->rs_client
)
1090 bgp_path_prepend(e
, attrs
, pool
, p
->local_as
);
1092 /* The MULTI_EXIT_DISC attribute received from a neighboring AS MUST NOT be
1093 * propagated to other neighboring ASes.
1094 * Perhaps it would be better to undefine it.
1096 a
= ea_find(e
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_MULTI_EXIT_DISC
));
1098 bgp_attach_attr(attrs
, pool
, BA_MULTI_EXIT_DISC
, 0);
1101 /* iBGP -> keep next_hop, eBGP multi-hop -> use source_addr,
1102 * eBGP single-hop -> keep next_hop if on the same iface.
1103 * If the next_hop is zero (i.e. link-local), keep only if on the same iface.
1105 * Note that same-iface-check uses iface from route, which is based on gw.
1107 a
= ea_find(e
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_NEXT_HOP
));
1108 if (a
&& !p
->cf
->next_hop_self
&&
1109 (p
->cf
->next_hop_keep
||
1110 (p
->is_internal
&& ipa_nonzero(*((ip_addr
*) a
->u
.ptr
->data
))) ||
1111 (p
->neigh
&& (e
->attrs
->iface
== p
->neigh
->iface
))))
1113 /* Leave the original next hop attribute, will check later where does it point */
1117 /* Need to create new one */
1118 byte
*b
= bgp_attach_attr_wa(attrs
, pool
, BA_NEXT_HOP
, NEXT_HOP_LENGTH
);
1119 set_next_hop(b
, p
->source_addr
);
1124 /* Handling route reflection, RFC 4456 */
1125 struct bgp_proto
*src
= (struct bgp_proto
*) e
->attrs
->src
->proto
;
1127 a
= ea_find(e
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_ORIGINATOR_ID
));
1129 bgp_attach_attr(attrs
, pool
, BA_ORIGINATOR_ID
, src
->remote_id
);
1131 /* We attach proper cluster ID according to whether the route is entering or leaving the cluster */
1132 bgp_cluster_list_prepend(e
, attrs
, pool
, src
->rr_client
? src
->rr_cluster_id
: p
->rr_cluster_id
);
1134 /* Two RR clients with different cluster ID, hmmm */
1135 if (src
->rr_client
&& p
->rr_client
&& (src
->rr_cluster_id
!= p
->rr_cluster_id
))
1136 bgp_cluster_list_prepend(e
, attrs
, pool
, p
->rr_cluster_id
);
1139 return 0; /* Leave decision to the filters */
1143 bgp_community_filter(struct bgp_proto
*p
, rte
*e
)
1148 /* Check if we aren't forbidden to export the route by communities */
1149 a
= ea_find(e
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_COMMUNITY
));
1153 if (int_set_contains(d
, BGP_COMM_NO_ADVERTISE
))
1155 DBG("\tNO_ADVERTISE\n");
1158 if (!p
->is_internal
&&
1159 (int_set_contains(d
, BGP_COMM_NO_EXPORT
) ||
1160 int_set_contains(d
, BGP_COMM_NO_EXPORT_SUBCONFED
)))
1162 DBG("\tNO_EXPORT\n");
1171 bgp_import_control(struct proto
*P
, rte
**new, ea_list
**attrs
, struct linpool
*pool
)
1174 struct bgp_proto
*p
= (struct bgp_proto
*) P
;
1175 struct bgp_proto
*new_bgp
= (e
->attrs
->src
->proto
->proto
== &proto_bgp
) ?
1176 (struct bgp_proto
*) e
->attrs
->src
->proto
: NULL
;
1178 if (p
== new_bgp
) /* Poison reverse updates */
1182 /* We should check here for cluster list loop, because the receiving BGP instance
1183 might have different cluster ID */
1184 if (bgp_cluster_list_loopy(p
, e
->attrs
))
1187 if (p
->cf
->interpret_communities
&& bgp_community_filter(p
, e
))
1190 if (p
->local_as
== new_bgp
->local_as
&& p
->is_internal
&& new_bgp
->is_internal
)
1192 /* Redistribution of internal routes with IBGP */
1193 if (p
->rr_client
|| new_bgp
->rr_client
)
1194 /* Route reflection, RFC 4456 */
1195 return bgp_update_attrs(p
, e
, attrs
, pool
, 1);
1200 return bgp_update_attrs(p
, e
, attrs
, pool
, 0);
1203 return bgp_create_attrs(p
, e
, attrs
, pool
);
1207 bgp_get_neighbor(rte
*r
)
1209 eattr
*e
= ea_find(r
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_AS_PATH
));
1212 if (e
&& as_path_get_first(e
->u
.ptr
, &as
))
1215 return ((struct bgp_proto
*) r
->attrs
->src
->proto
)->remote_as
;
1219 rte_resolvable(rte
*rt
)
1221 int rd
= rt
->attrs
->dest
;
1222 return (rd
== RTD_ROUTER
) || (rd
== RTD_DEVICE
) || (rd
== RTD_MULTIPATH
);
1226 bgp_rte_better(rte
*new, rte
*old
)
1228 struct bgp_proto
*new_bgp
= (struct bgp_proto
*) new->attrs
->src
->proto
;
1229 struct bgp_proto
*old_bgp
= (struct bgp_proto
*) old
->attrs
->src
->proto
;
1233 /* Skip suppressed routes (see bgp_rte_recalculate()) */
1234 n
= new->u
.bgp
.suppressed
;
1235 o
= old
->u
.bgp
.suppressed
;
1241 /* RFC 4271 9.1.2.1. Route resolvability test */
1242 n
= rte_resolvable(new);
1243 o
= rte_resolvable(old
);
1249 /* Start with local preferences */
1250 x
= ea_find(new->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_LOCAL_PREF
));
1251 y
= ea_find(old
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_LOCAL_PREF
));
1252 n
= x
? x
->u
.data
: new_bgp
->cf
->default_local_pref
;
1253 o
= y
? y
->u
.data
: old_bgp
->cf
->default_local_pref
;
1259 /* RFC 4271 9.1.2.2. a) Use AS path lengths */
1260 if (new_bgp
->cf
->compare_path_lengths
|| old_bgp
->cf
->compare_path_lengths
)
1262 x
= ea_find(new->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_AS_PATH
));
1263 y
= ea_find(old
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_AS_PATH
));
1264 n
= x
? as_path_getlen(x
->u
.ptr
) : AS_PATH_MAXLEN
;
1265 o
= y
? as_path_getlen(y
->u
.ptr
) : AS_PATH_MAXLEN
;
1272 /* RFC 4271 9.1.2.2. b) Use origins */
1273 x
= ea_find(new->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_ORIGIN
));
1274 y
= ea_find(old
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_ORIGIN
));
1275 n
= x
? x
->u
.data
: ORIGIN_INCOMPLETE
;
1276 o
= y
? y
->u
.data
: ORIGIN_INCOMPLETE
;
1282 /* RFC 4271 9.1.2.2. c) Compare MED's */
1283 /* Proper RFC 4271 path selection cannot be interpreted as finding
1284 * the best path in some ordering. It is implemented partially in
1285 * bgp_rte_recalculate() when deterministic_med option is
1286 * active. Without that option, the behavior is just an
1287 * approximation, which in specific situations may lead to
1288 * persistent routing loops, because it is nondeterministic - it
1289 * depends on the order in which routes appeared. But it is also the
1290 * same behavior as used by default in Cisco routers, so it is
1291 * probably not a big issue.
1293 if (new_bgp
->cf
->med_metric
|| old_bgp
->cf
->med_metric
||
1294 (bgp_get_neighbor(new) == bgp_get_neighbor(old
)))
1296 x
= ea_find(new->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_MULTI_EXIT_DISC
));
1297 y
= ea_find(old
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_MULTI_EXIT_DISC
));
1298 n
= x
? x
->u
.data
: new_bgp
->cf
->default_med
;
1299 o
= y
? y
->u
.data
: old_bgp
->cf
->default_med
;
1306 /* RFC 4271 9.1.2.2. d) Prefer external peers */
1307 if (new_bgp
->is_internal
> old_bgp
->is_internal
)
1309 if (new_bgp
->is_internal
< old_bgp
->is_internal
)
1312 /* RFC 4271 9.1.2.2. e) Compare IGP metrics */
1313 n
= new_bgp
->cf
->igp_metric
? new->attrs
->igp_metric
: 0;
1314 o
= old_bgp
->cf
->igp_metric
? old
->attrs
->igp_metric
: 0;
1320 /* RFC 4271 9.1.2.2. f) Compare BGP identifiers */
1321 /* RFC 4456 9. a) Use ORIGINATOR_ID instead of local neighor ID */
1322 x
= ea_find(new->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_ORIGINATOR_ID
));
1323 y
= ea_find(old
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_ORIGINATOR_ID
));
1324 n
= x
? x
->u
.data
: new_bgp
->remote_id
;
1325 o
= y
? y
->u
.data
: old_bgp
->remote_id
;
1327 /* RFC 5004 - prefer older routes */
1328 /* (if both are external and from different peer) */
1329 if ((new_bgp
->cf
->prefer_older
|| old_bgp
->cf
->prefer_older
) &&
1330 !new_bgp
->is_internal
&& n
!= o
)
1333 /* rest of RFC 4271 9.1.2.2. f) */
1339 /* RFC 4456 9. b) Compare cluster list lengths */
1340 x
= ea_find(new->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_CLUSTER_LIST
));
1341 y
= ea_find(old
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_CLUSTER_LIST
));
1342 n
= x
? int_set_get_size(x
->u
.ptr
) : 0;
1343 o
= y
? int_set_get_size(y
->u
.ptr
) : 0;
1349 /* RFC 4271 9.1.2.2. g) Compare peer IP adresses */
1350 return (ipa_compare(new_bgp
->cf
->remote_ip
, old_bgp
->cf
->remote_ip
) < 0);
1355 bgp_rte_mergable(rte
*pri
, rte
*sec
)
1357 struct bgp_proto
*pri_bgp
= (struct bgp_proto
*) pri
->attrs
->src
->proto
;
1358 struct bgp_proto
*sec_bgp
= (struct bgp_proto
*) sec
->attrs
->src
->proto
;
1362 /* Skip suppressed routes (see bgp_rte_recalculate()) */
1363 if (pri
->u
.bgp
.suppressed
!= sec
->u
.bgp
.suppressed
)
1366 /* RFC 4271 9.1.2.1. Route resolvability test */
1367 if (!rte_resolvable(sec
))
1370 /* Start with local preferences */
1371 x
= ea_find(pri
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_LOCAL_PREF
));
1372 y
= ea_find(sec
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_LOCAL_PREF
));
1373 p
= x
? x
->u
.data
: pri_bgp
->cf
->default_local_pref
;
1374 s
= y
? y
->u
.data
: sec_bgp
->cf
->default_local_pref
;
1378 /* RFC 4271 9.1.2.2. a) Use AS path lengths */
1379 if (pri_bgp
->cf
->compare_path_lengths
|| sec_bgp
->cf
->compare_path_lengths
)
1381 x
= ea_find(pri
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_AS_PATH
));
1382 y
= ea_find(sec
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_AS_PATH
));
1383 p
= x
? as_path_getlen(x
->u
.ptr
) : AS_PATH_MAXLEN
;
1384 s
= y
? as_path_getlen(y
->u
.ptr
) : AS_PATH_MAXLEN
;
1389 // if (DELTA(p, s) > pri_bgp->cf->relax_multipath)
1393 /* RFC 4271 9.1.2.2. b) Use origins */
1394 x
= ea_find(pri
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_ORIGIN
));
1395 y
= ea_find(sec
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_ORIGIN
));
1396 p
= x
? x
->u
.data
: ORIGIN_INCOMPLETE
;
1397 s
= y
? y
->u
.data
: ORIGIN_INCOMPLETE
;
1401 /* RFC 4271 9.1.2.2. c) Compare MED's */
1402 if (pri_bgp
->cf
->med_metric
|| sec_bgp
->cf
->med_metric
||
1403 (bgp_get_neighbor(pri
) == bgp_get_neighbor(sec
)))
1405 x
= ea_find(pri
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_MULTI_EXIT_DISC
));
1406 y
= ea_find(sec
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_MULTI_EXIT_DISC
));
1407 p
= x
? x
->u
.data
: pri_bgp
->cf
->default_med
;
1408 s
= y
? y
->u
.data
: sec_bgp
->cf
->default_med
;
1413 /* RFC 4271 9.1.2.2. d) Prefer external peers */
1414 if (pri_bgp
->is_internal
!= sec_bgp
->is_internal
)
1417 /* RFC 4271 9.1.2.2. e) Compare IGP metrics */
1418 p
= pri_bgp
->cf
->igp_metric
? pri
->attrs
->igp_metric
: 0;
1419 s
= sec_bgp
->cf
->igp_metric
? sec
->attrs
->igp_metric
: 0;
1423 /* Remaining criteria are ignored */
1431 same_group(rte
*r
, u32 lpref
, u32 lasn
)
1433 return (r
->pref
== lpref
) && (bgp_get_neighbor(r
) == lasn
);
1437 use_deterministic_med(rte
*r
)
1439 struct proto
*P
= r
->attrs
->src
->proto
;
1440 return (P
->proto
== &proto_bgp
) && ((struct bgp_proto
*) P
)->cf
->deterministic_med
;
1444 bgp_rte_recalculate(rtable
*table
, net
*net
, rte
*new, rte
*old
, rte
*old_best
)
1447 rte
*key
= new ? new : old
;
1448 u32 lpref
= key
->pref
;
1449 u32 lasn
= bgp_get_neighbor(key
);
1450 int old_is_group_best
= 0;
1453 * Proper RFC 4271 path selection is a bit complicated, it cannot be
1454 * implemented just by rte_better(), because it is not a linear
1455 * ordering. But it can be splitted to two levels, where the lower
1456 * level chooses the best routes in each group of routes from the
1457 * same neighboring AS and higher level chooses the best route (with
1458 * a slightly different ordering) between the best-in-group routes.
1460 * When deterministic_med is disabled, we just ignore this issue and
1461 * choose the best route by bgp_rte_better() alone. If enabled, the
1462 * lower level of the route selection is done here (for the group
1463 * to which the changed route belongs), all routes in group are
1464 * marked as suppressed, just chosen best-in-group is not.
1466 * Global best route selection then implements higher level by
1467 * choosing between non-suppressed routes (as they are always
1468 * preferred over suppressed routes). Routes from BGP protocols
1469 * that do not set deterministic_med are just never suppressed. As
1470 * they do not participate in the lower level selection, it is OK
1471 * that this fn is not called for them.
1473 * The idea is simple, the implementation is more problematic,
1474 * mostly because of optimizations in rte_recalculate() that
1475 * avoids full recalculation in most cases.
1477 * We can assume that at least one of new, old is non-NULL and both
1478 * are from the same protocol with enabled deterministic_med. We
1479 * group routes by both neighbor AS (lasn) and preference (lpref),
1480 * because bgp_rte_better() does not handle preference itself.
1483 /* If new and old are from different groups, we just process that
1484 as two independent events */
1485 if (new && old
&& !same_group(old
, lpref
, lasn
))
1488 i1
= bgp_rte_recalculate(table
, net
, NULL
, old
, old_best
);
1489 i2
= bgp_rte_recalculate(table
, net
, new, NULL
, old_best
);
1494 * We could find the best-in-group and then make some shortcuts like
1495 * in rte_recalculate, but as we would have to walk through all
1496 * net->routes just to find it, it is probably not worth. So we
1497 * just have two simpler fast cases that use just the old route.
1498 * We also set suppressed flag to avoid using it in bgp_rte_better().
1502 new->u
.bgp
.suppressed
= 1;
1506 old_is_group_best
= !old
->u
.bgp
.suppressed
;
1507 old
->u
.bgp
.suppressed
= 1;
1508 int new_is_better
= new && bgp_rte_better(new, old
);
1510 /* The first case - replace not best with worse (or remove not best) */
1511 if (!old_is_group_best
&& !new_is_better
)
1514 /* The second case - replace the best with better */
1515 if (old_is_group_best
&& new_is_better
)
1517 /* new is best-in-group, the see discussion below - this is
1518 a special variant of NBG && OBG. From OBG we can deduce
1519 that same_group(old_best) iff (old == old_best) */
1520 new->u
.bgp
.suppressed
= 0;
1521 return (old
== old_best
);
1525 /* The default case - find a new best-in-group route */
1526 r
= new; /* new may not be in the list */
1527 for (s
=net
->routes
; rte_is_valid(s
); s
=s
->next
)
1528 if (use_deterministic_med(s
) && same_group(s
, lpref
, lasn
))
1530 s
->u
.bgp
.suppressed
= 1;
1531 if (!r
|| bgp_rte_better(s
, r
))
1535 /* Simple case - the last route in group disappears */
1539 /* Found best-in-group */
1540 r
->u
.bgp
.suppressed
= 0;
1543 * There are generally two reasons why we have to force
1544 * recalculation (return 1): First, the new route may be wrongfully
1545 * chosen to be the best in the first case check in
1546 * rte_recalculate(), this may happen only if old_best is from the
1547 * same group. Second, another (different than new route)
1548 * best-in-group is chosen and that may be the proper best (although
1549 * rte_recalculate() without ignore that possibility).
1551 * There are three possible cases according to whether the old route
1552 * was the best in group (OBG, stored in old_is_group_best) and
1553 * whether the new route is the best in group (NBG, tested by r == new).
1554 * These cases work even if old or new is NULL.
1556 * NBG -> new is a possible candidate for the best route, so we just
1557 * check for the first reason using same_group().
1559 * !NBG && OBG -> Second reason applies, return 1
1561 * !NBG && !OBG -> Best in group does not change, old != old_best,
1562 * rte_better(new, old_best) is false and therefore
1563 * the first reason does not apply, return 0
1567 return old_best
&& same_group(old_best
, lpref
, lasn
);
1569 return old_is_group_best
;
1572 static struct adata
*
1573 bgp_aggregator_convert_to_new(struct adata
*old
, struct linpool
*pool
)
1575 struct adata
*newa
= lp_alloc(pool
, sizeof(struct adata
) + 8);
1577 aggregator_convert_to_new(old
, newa
->data
);
1582 /* Take last req_as ASNs from path old2 (in 2B format), convert to 4B format
1583 * and append path old4 (in 4B format).
1585 static struct adata
*
1586 bgp_merge_as_paths(struct adata
*old2
, struct adata
*old4
, int req_as
, struct linpool
*pool
)
1588 byte buf
[old2
->length
* 2];
1590 int ol
= as_path_convert_to_new(old2
, buf
, req_as
);
1591 int nl
= ol
+ (old4
? old4
->length
: 0);
1593 struct adata
*newa
= lp_alloc(pool
, sizeof(struct adata
) + nl
);
1595 memcpy(newa
->data
, buf
, ol
);
1596 if (old4
) memcpy(newa
->data
+ ol
, old4
->data
, old4
->length
);
1602 as4_aggregator_valid(struct adata
*aggr
)
1604 return aggr
->length
== 8;
1608 /* Reconstruct 4B AS_PATH and AGGREGATOR according to RFC 4893 4.2.3 */
1610 bgp_reconstruct_4b_atts(struct bgp_proto
*p
, rta
*a
, struct linpool
*pool
)
1612 eattr
*p2
=ea_find(a
->eattrs
, EA_CODE(EAP_BGP
, BA_AS_PATH
));
1613 eattr
*p4
=ea_find(a
->eattrs
, EA_CODE(EAP_BGP
, BA_AS4_PATH
));
1614 eattr
*a2
=ea_find(a
->eattrs
, EA_CODE(EAP_BGP
, BA_AGGREGATOR
));
1615 eattr
*a4
=ea_find(a
->eattrs
, EA_CODE(EAP_BGP
, BA_AS4_AGGREGATOR
));
1618 if (a4
&& !as4_aggregator_valid(a4
->u
.ptr
))
1620 log(L_WARN
"%s: AS4_AGGREGATOR attribute is invalid, skipping attribute", p
->p
.name
);
1627 u32 a2_as
= get_u16(a2
->u
.ptr
->data
);
1631 if (a2_as
!= AS_TRANS
)
1633 /* Routes were aggregated by old router and therefore AS4_PATH
1634 * and AS4_AGGREGATOR is invalid
1636 * Convert AS_PATH and AGGREGATOR to 4B format and finish.
1639 a2
->u
.ptr
= bgp_aggregator_convert_to_new(a2
->u
.ptr
, pool
);
1640 p2
->u
.ptr
= bgp_merge_as_paths(p2
->u
.ptr
, NULL
, AS_PATH_MAXLEN
, pool
);
1646 /* Common case, use AS4_AGGREGATOR attribute */
1647 a2
->u
.ptr
= a4
->u
.ptr
;
1652 /* Common case, use old AGGREGATOR attribute */
1653 a2
->u
.ptr
= bgp_aggregator_convert_to_new(a2
->u
.ptr
, pool
);
1655 if ((a2_as
== AS_TRANS
) && !a4_removed
)
1656 log(L_WARN
"%s: AGGREGATOR attribute contain AS_TRANS, but AS4_AGGREGATOR is missing", p
->p
.name
);
1661 log(L_WARN
"%s: AS4_AGGREGATOR attribute received, but AGGREGATOR attribute is missing", p
->p
.name
);
1663 int p2_len
= as_path_getlen_int(p2
->u
.ptr
, 2);
1664 int p4_len
= p4
? validate_as4_path(p
, p4
->u
.ptr
) : -1;
1666 if (p4
&& (p4_len
< 0))
1667 log(L_WARN
"%s: AS4_PATH attribute is malformed, skipping attribute", p
->p
.name
);
1669 if ((p4_len
<= 0) || (p2_len
< p4_len
))
1670 p2
->u
.ptr
= bgp_merge_as_paths(p2
->u
.ptr
, NULL
, AS_PATH_MAXLEN
, pool
);
1672 p2
->u
.ptr
= bgp_merge_as_paths(p2
->u
.ptr
, p4
->u
.ptr
, p2_len
- p4_len
, pool
);
1676 bgp_remove_as4_attrs(struct bgp_proto
*p
, rta
*a
)
1678 unsigned id1
= EA_CODE(EAP_BGP
, BA_AS4_PATH
);
1679 unsigned id2
= EA_CODE(EAP_BGP
, BA_AS4_AGGREGATOR
);
1680 ea_list
**el
= &(a
->eattrs
);
1682 /* We know that ea_lists constructed in bgp_decode attrs have one attribute per ea_list struct */
1685 unsigned fid
= (*el
)->attrs
[0].id
;
1687 if ((fid
== id1
) || (fid
== id2
))
1691 log(L_WARN
"%s: Unexpected AS4_* attributes received", p
->p
.name
);
1694 el
= &((*el
)->next
);
1699 * bgp_decode_attrs - check and decode BGP attributes
1701 * @attr: start of attribute block
1702 * @len: length of attribute block
1703 * @pool: linear pool to make all the allocations in
1704 * @mandatory: 1 iff presence of mandatory attributes has to be checked
1706 * This function takes a BGP attribute block (a part of an Update message), checks
1707 * its consistency and converts it to a list of BIRD route attributes represented
1711 bgp_decode_attrs(struct bgp_conn
*conn
, byte
*attr
, uint len
, struct linpool
*pool
, int mandatory
)
1713 struct bgp_proto
*bgp
= conn
->bgp
;
1714 rta
*a
= lp_alloc(pool
, sizeof(struct rta
));
1715 uint flags
, code
, l
, i
, type
;
1717 byte
*z
, *attr_start
;
1723 bzero(a
, sizeof(rta
));
1724 a
->source
= RTS_BGP
;
1725 a
->scope
= SCOPE_UNIVERSE
;
1726 a
->cast
= RTC_UNICAST
;
1727 /* a->dest = RTD_ROUTER; -- set in bgp_set_next_hop() */
1728 a
->from
= bgp
->cf
->remote_ip
;
1730 /* Parse the attributes */
1731 bzero(seen
, sizeof(seen
));
1732 DBG("BGP: Parsing attributes\n");
1741 if (flags
& BAF_EXT_LEN
)
1761 DBG("Attr %02x %02x %d\n", code
, flags
, l
);
1762 if (seen
[code
/8] & (1 << (code
%8)))
1764 if (ATTR_KNOWN(code
))
1766 struct attr_desc
*desc
= &bgp_attr_table
[code
];
1767 if (desc
->expected_length
>= 0 && desc
->expected_length
!= (int) l
)
1768 { errcode
= 5; goto err
; }
1769 if ((desc
->expected_flags
^ flags
) & (BAF_OPTIONAL
| BAF_TRANSITIVE
))
1770 { errcode
= 4; goto err
; }
1771 if (!desc
->allow_in_ebgp
&& !bgp
->is_internal
)
1775 errcode
= desc
->validate(bgp
, z
, l
);
1778 if (errcode
== IGNORE
)
1780 if (errcode
<= WITHDRAW
)
1782 log(L_WARN
"%s: Attribute %s is malformed, withdrawing update",
1783 bgp
->p
.name
, desc
->name
);
1787 else if (code
== BA_AS_PATH
)
1789 /* Special case as it might also trim the attribute */
1790 if (validate_as_path(bgp
, z
, &l
) < 0)
1791 { errcode
= 11; goto err
; }
1795 else /* Unknown attribute */
1797 if (!(flags
& BAF_OPTIONAL
))
1798 { errcode
= 2; goto err
; }
1799 type
= EAF_TYPE_OPAQUE
;
1802 // Only OPTIONAL and TRANSITIVE attributes may have non-zero PARTIAL flag
1803 // if (!((flags & BAF_OPTIONAL) && (flags & BAF_TRANSITIVE)) && (flags & BAF_PARTIAL))
1804 // { errcode = 4; goto err; }
1806 seen
[code
/8] |= (1 << (code
%8));
1807 ea
= lp_alloc(pool
, sizeof(ea_list
) + sizeof(eattr
));
1808 ea
->next
= a
->eattrs
;
1812 ea
->attrs
[0].id
= EA_CODE(EAP_BGP
, code
);
1813 ea
->attrs
[0].flags
= flags
;
1814 ea
->attrs
[0].type
= type
;
1815 if (type
& EAF_EMBEDDED
)
1819 ad
= lp_alloc(pool
, sizeof(struct adata
) + l
);
1820 ea
->attrs
[0].u
.ptr
= ad
;
1822 memcpy(ad
->data
, z
, l
);
1826 case EAF_TYPE_ROUTER_ID
:
1829 ea
->attrs
[0].u
.data
= *z
;
1831 ea
->attrs
[0].u
.data
= get_u32(z
);
1833 case EAF_TYPE_IP_ADDRESS
:
1834 ipa_ntoh(*(ip_addr
*)ad
->data
);
1836 case EAF_TYPE_INT_SET
:
1837 case EAF_TYPE_LC_SET
:
1838 case EAF_TYPE_EC_SET
:
1840 u32
*z
= (u32
*) ad
->data
;
1841 for(i
=0; i
<ad
->length
/4; i
++)
1852 /* If we received MP_REACH_NLRI we should check mandatory attributes */
1853 if (bgp
->mp_reach_len
!= 0)
1857 /* If there is no (reachability) NLRI, we should exit now */
1861 /* Check if all mandatory attributes are present */
1862 for(i
=0; i
< ARRAY_SIZE(bgp_mandatory_attrs
); i
++)
1864 code
= bgp_mandatory_attrs
[i
];
1865 if (!(seen
[code
/8] & (1 << (code
%8))))
1867 bgp_error(conn
, 3, 3, &bgp_mandatory_attrs
[i
], 1);
1872 /* When receiving attributes from non-AS4-aware BGP speaker,
1873 * we have to reconstruct 4B AS_PATH and AGGREGATOR attributes
1875 if (! bgp
->as4_session
)
1876 bgp_reconstruct_4b_atts(bgp
, a
, pool
);
1878 bgp_remove_as4_attrs(bgp
, a
);
1880 /* If the AS path attribute contains our AS, reject the routes */
1881 if (bgp_as_path_loopy(bgp
, a
))
1884 /* Two checks for IBGP loops caused by route reflection, RFC 4456 */
1885 if (bgp_originator_id_loopy(bgp
, a
) ||
1886 bgp_cluster_list_loopy(bgp
, a
))
1889 /* If there's no local preference, define one */
1890 if (!(seen
[0] & (1 << BA_LOCAL_PREF
)))
1891 bgp_attach_attr(&a
->eattrs
, pool
, BA_LOCAL_PREF
, bgp
->cf
->default_local_pref
);
1899 bgp_error(conn
, 3, 1, NULL
, 0);
1903 bgp_error(conn
, 3, errcode
, attr_start
, z
+l
-attr_start
);
1908 bgp_get_attr(eattr
*a
, byte
*buf
, int buflen
)
1910 uint i
= EA_ID(a
->id
);
1911 struct attr_desc
*d
;
1916 d
= &bgp_attr_table
[i
];
1917 len
= bsprintf(buf
, "%s", d
->name
);
1923 d
->format(a
, buf
, buflen
- len
- 2);
1928 bsprintf(buf
, "%02x%s", i
, (a
->flags
& BAF_TRANSITIVE
) ? " [t]" : "");
1933 bgp_init_bucket_table(struct bgp_proto
*p
)
1936 p
->hash_limit
= p
->hash_size
* 4;
1937 p
->bucket_hash
= mb_allocz(p
->p
.pool
, p
->hash_size
* sizeof(struct bgp_bucket
*));
1938 init_list(&p
->bucket_queue
);
1939 p
->withdraw_bucket
= NULL
;
1940 // fib_init(&p->prefix_fib, p->p.pool, sizeof(struct bgp_prefix), 0, bgp_init_prefix);
1944 bgp_get_route_info(rte
*e
, byte
*buf
, ea_list
*attrs
)
1946 eattr
*p
= ea_find(attrs
, EA_CODE(EAP_BGP
, BA_AS_PATH
));
1947 eattr
*o
= ea_find(attrs
, EA_CODE(EAP_BGP
, BA_ORIGIN
));
1950 buf
+= bsprintf(buf
, " (%d", e
->pref
);
1952 if (e
->u
.bgp
.suppressed
)
1953 buf
+= bsprintf(buf
, "-");
1955 if (e
->attrs
->hostentry
)
1957 if (!rte_resolvable(e
))
1958 buf
+= bsprintf(buf
, "/-");
1959 else if (e
->attrs
->igp_metric
>= IGP_METRIC_UNKNOWN
)
1960 buf
+= bsprintf(buf
, "/?");
1962 buf
+= bsprintf(buf
, "/%d", e
->attrs
->igp_metric
);
1964 buf
+= bsprintf(buf
, ") [");
1966 if (p
&& as_path_get_last(p
->u
.ptr
, &origas
))
1967 buf
+= bsprintf(buf
, "AS%u", origas
);
1969 buf
+= bsprintf(buf
, "%c", "ie?"[o
->u
.data
]);