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"
25 static byte bgp_mandatory_attrs
[] = { BA_ORIGIN
, BA_AS_PATH
37 int (*validate
)(struct bgp_proto
*p
, byte
*attr
, int len
);
38 void (*format
)(eattr
*ea
, byte
*buf
, int buflen
);
42 bgp_check_origin(struct bgp_proto
*p UNUSED
, byte
*a
, int len UNUSED
)
50 bgp_format_origin(eattr
*a
, byte
*buf
, int buflen
)
52 static char *bgp_origin_names
[] = { "IGP", "EGP", "Incomplete" };
54 bsprintf(buf
, bgp_origin_names
[a
->u
.data
]);
58 path_segment_contains(byte
*p
, int bs
, u32 asn
)
66 u32 asn2
= (bs
== 4) ? get_u32(p
) : get_u16(p
);
75 /* Validates path attribute, removes AS_CONFED_* segments, and also returns path length */
77 validate_path(struct bgp_proto
*p
, int as_path
, int bs
, byte
*idata
, unsigned int *ilength
)
102 case AS_PATH_SEQUENCE
:
107 case AS_PATH_CONFED_SEQUENCE
:
108 case AS_PATH_CONFED_SET
:
109 if (as_path
&& path_segment_contains(a
, bs
, p
->remote_as
))
111 log(L_WARN
"%s: AS_CONFED_* segment with peer ASN found, misconfigured confederation?", p
->p
.name
);
115 log(L_WARN
"%s: %s_PATH attribute contains AS_CONFED_* segment, skipping segment",
116 p
->p
.name
, as_path
? "AS" : "AS4");
127 memmove(dst
, a
, plen
);
135 *ilength
= dst
- idata
;
140 validate_as_path(struct bgp_proto
*p
, byte
*a
, int *len
)
142 return validate_path(p
, 1, p
->as4_session
? 4 : 2, a
, len
);
146 validate_as4_path(struct bgp_proto
*p
, struct adata
*path
)
148 return validate_path(p
, 0, 4, path
->data
, &path
->length
);
152 bgp_check_next_hop(struct bgp_proto
*p UNUSED
, byte
*a
, int len
)
159 memcpy(&addr
, a
, len
);
161 if (ipa_classify(addr
) & IADDR_HOST
)
169 bgp_check_aggregator(struct bgp_proto
*p
, byte
*a UNUSED
, int len
)
171 int exp_len
= p
->as4_session
? 8 : 6;
173 return (len
== exp_len
) ? 0 : 5;
177 bgp_format_aggregator(eattr
*a
, byte
*buf
, int buflen UNUSED
)
179 struct adata
*ad
= a
->u
.ptr
;
180 byte
*data
= ad
->data
;
194 bsprintf(buf
, "%d.%d.%d.%d AS%d", data
[0], data
[1], data
[2], data
[3], as
);
198 bgp_check_cluster_list(struct bgp_proto
*p UNUSED
, byte
*a UNUSED
, int len
)
200 return ((len
% 4) == 0) ? 0 : 5;
204 bgp_format_cluster_list(eattr
*a
, byte
*buf
, int buflen UNUSED
)
206 int_set_format(a
->u
.ptr
, 0, buf
, buflen
);
210 bgp_check_reach_nlri(struct bgp_proto
*p UNUSED
, byte
*a UNUSED
, int len UNUSED
)
213 p
->mp_reach_start
= a
;
214 p
->mp_reach_len
= len
;
220 bgp_check_unreach_nlri(struct bgp_proto
*p UNUSED
, byte
*a UNUSED
, int len UNUSED
)
223 p
->mp_unreach_start
= a
;
224 p
->mp_unreach_len
= len
;
229 static struct attr_desc bgp_attr_table
[] = {
230 { NULL
, -1, 0, 0, 0, /* Undefined */
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 */
240 { "local_pref", 4, BAF_TRANSITIVE
, EAF_TYPE_INT
, 0, /* BA_LOCAL_PREF */
242 { "atomic_aggr", 0, BAF_TRANSITIVE
, EAF_TYPE_OPAQUE
, 1, /* BA_ATOMIC_AGGR */
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 */
248 { "originator_id", 4, BAF_OPTIONAL
, EAF_TYPE_ROUTER_ID
, 0, /* BA_ORIGINATOR_ID */
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 */
262 { "as4_aggregator", -1, BAF_OPTIONAL
| BAF_TRANSITIVE
, EAF_TYPE_OPAQUE
, 1, /* BA_AS4_PATH */
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.
271 #define ATTR_KNOWN(code) ((code) < ARRAY_SIZE(bgp_attr_table) && bgp_attr_table[code].name)
273 static inline struct adata
*
274 bgp_alloc_adata(struct linpool
*pool
, unsigned len
)
276 struct adata
*ad
= lp_alloc(pool
, sizeof(struct adata
) + len
);
282 bgp_set_attr(eattr
*e
, unsigned attr
, uintptr_t val
)
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
)
291 e
->u
.ptr
= (struct adata
*) val
;
295 bgp_set_attr_wa(eattr
*e
, struct linpool
*pool
, unsigned attr
, unsigned len
)
297 struct adata
*ad
= bgp_alloc_adata(pool
, len
);
298 bgp_set_attr(e
, attr
, (uintptr_t) ad
);
303 bgp_attach_attr(ea_list
**to
, struct linpool
*pool
, unsigned attr
, uintptr_t val
)
305 ea_list
*a
= lp_alloc(pool
, sizeof(ea_list
) + sizeof(eattr
));
308 a
->flags
= EALF_SORTED
;
310 bgp_set_attr(a
->attrs
, attr
, val
);
314 bgp_attach_attr_wa(ea_list
**to
, struct linpool
*pool
, unsigned attr
, unsigned len
)
316 struct adata
*ad
= bgp_alloc_adata(pool
, len
);
317 bgp_attach_attr(to
, pool
, attr
, (uintptr_t) ad
);
322 bgp_encode_attr_hdr(byte
*dst
, unsigned int flags
, unsigned code
, int len
)
326 DBG("\tAttribute %02x (%d bytes, flags %02x)\n", code
, len
, flags
);
337 *dst
++ = flags
| BAF_EXT_LEN
;
347 aggregator_convert_to_old(struct adata
*aggr
, byte
*dst
, int *new_used
)
349 byte
*src
= aggr
->data
;
352 u32 as
= get_u32(src
);
360 /* Copy IPv4 address */
361 memcpy(dst
+ 2, src
+ 4, 4);
365 aggregator_convert_to_new(struct adata
*aggr
, byte
*dst
)
367 byte
*src
= aggr
->data
;
369 u32 as
= get_u16(src
);
372 /* Copy IPv4 address */
373 memcpy(dst
+ 4, src
+ 2, 4);
377 bgp_get_attr_len(eattr
*a
)
380 if (ATTR_KNOWN(EA_ID(a
->id
)))
382 int code
= EA_ID(a
->id
);
383 struct attr_desc
*desc
= &bgp_attr_table
[code
];
384 len
= desc
->expected_length
;
387 ASSERT(!(a
->type
& EAF_EMBEDDED
));
388 len
= a
->u
.ptr
->length
;
393 ASSERT((a
->type
& EAF_TYPE_MASK
) == EAF_TYPE_OPAQUE
);
394 len
= a
->u
.ptr
->length
;
400 #define ADVANCE(w, r, l) do { r -= l; w += l; } while (0)
403 * bgp_encode_attrs - encode BGP attributes
406 * @attrs: a list of extended attributes
407 * @remains: remaining space in the buffer
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).
412 * Result: Length of the attribute block generated or -1 if not enough space.
415 bgp_encode_attrs(struct bgp_proto
*p
, byte
*w
, ea_list
*attrs
, int remains
)
417 unsigned int i
, code
, flags
;
421 for(i
=0; i
<attrs
->count
; i
++)
423 eattr
*a
= &attrs
->attrs
[i
];
424 ASSERT(EA_PROTO(a
->id
) == EAP_BGP
);
427 /* When talking multiprotocol BGP, the NEXT_HOP attributes are used only temporarily. */
428 if (code
== BA_NEXT_HOP
)
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.
436 if ((code
== BA_AS_PATH
) && bgp_as4_support
&& (! p
->as4_session
))
438 len
= a
->u
.ptr
->length
;
440 if (remains
< (len
+ 4))
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. */
449 int nl
= as_path_convert_to_old(a
->u
.ptr
, buf
, &new_used
);
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
);
455 ADVANCE(w
, remains
, nl
);
460 if (remains
< (len
+ 4))
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().
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
);
477 /* The same issue with AGGREGATOR attribute */
478 if ((code
== BA_AGGREGATOR
) && bgp_as4_support
&& (! p
->as4_session
))
483 if (remains
< (len
+ 3))
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
);
495 if (remains
< (len
+ 3))
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
);
506 /* Standard path continues here ... */
508 flags
= a
->flags
& (BAF_OPTIONAL
| BAF_TRANSITIVE
| BAF_PARTIAL
);
509 len
= bgp_get_attr_len(a
);
511 if (remains
< len
+ 4)
514 rv
= bgp_encode_attr_hdr(w
, flags
, code
, len
);
515 ADVANCE(w
, remains
, rv
);
517 switch (a
->type
& EAF_TYPE_MASK
)
520 case EAF_TYPE_ROUTER_ID
:
522 put_u32(w
, a
->u
.data
);
526 case EAF_TYPE_IP_ADDRESS
:
528 ip_addr ip
= *(ip_addr
*)a
->u
.ptr
->data
;
533 case EAF_TYPE_INT_SET
:
535 u32
*z
= (u32
*)a
->u
.ptr
->data
;
537 for(i
=0; i
<len
; i
+=4)
541 case EAF_TYPE_OPAQUE
:
542 case EAF_TYPE_AS_PATH
:
543 memcpy(w
, a
->u
.ptr
->data
, len
);
546 bug("bgp_encode_attrs: unknown attribute type %02x", a
->type
);
548 ADVANCE(w
, remains
, len
);
557 bgp_init_prefix(struct fib_node
*N
)
559 struct bgp_prefix
*p
= (struct bgp_prefix
*) N
;
560 p
->bucket_node
.next
= NULL
;
564 bgp_compare_u32(const u32
*x
, const u32
*y
)
566 return (*x
< *y
) ? -1 : (*x
> *y
) ? 1 : 0;
570 bgp_normalize_set(u32
*dest
, u32
*src
, unsigned cnt
)
572 memcpy(dest
, src
, sizeof(u32
) * cnt
);
573 qsort(dest
, cnt
, sizeof(u32
), (int(*)(const void *, const void *)) bgp_compare_u32
);
577 bgp_rehash_buckets(struct bgp_proto
*p
)
579 struct bgp_bucket
**old
= p
->bucket_hash
;
580 struct bgp_bucket
**new;
581 unsigned oldn
= p
->hash_size
;
583 struct bgp_bucket
*b
;
585 p
->hash_size
= p
->hash_limit
;
586 DBG("BGP: Rehashing bucket table from %d to %d\n", oldn
, p
->hash_size
);
588 if (p
->hash_limit
>= 65536)
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
++)
595 old
[i
] = b
->hash_next
;
597 b
->hash_next
= new[e
];
599 b
->hash_next
->hash_prev
= b
;
606 static struct bgp_bucket
*
607 bgp_new_bucket(struct bgp_proto
*p
, ea_list
*new, unsigned hash
)
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
;
615 unsigned index
= hash
& (p
->hash_size
- 1);
617 /* Gather total size of non-inline attributes */
618 for (i
=0; i
<new->count
; i
++)
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
);
625 /* Create the bucket and hash it */
626 b
= mb_alloc(p
->p
.pool
, size
);
627 b
->hash_next
= p
->bucket_hash
[index
];
629 b
->hash_next
->hash_prev
= b
;
630 p
->bucket_hash
[index
] = b
;
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
;
638 /* Copy values of non-inline attributes */
639 for (i
=0; i
<new->count
; i
++)
641 eattr
*a
= &b
->eattrs
->attrs
[i
];
642 if (!(a
->type
& EAF_EMBEDDED
))
644 struct adata
*oa
= a
->u
.ptr
;
645 struct adata
*na
= (struct adata
*) dest
;
646 memcpy(na
, oa
, sizeof(struct adata
) + oa
->length
);
648 dest
+= BIRD_ALIGN(sizeof(struct adata
) + na
->length
, CPU_STRUCT_ALIGN
);
652 /* If needed, rehash */
654 if (p
->hash_count
> p
->hash_limit
)
655 bgp_rehash_buckets(p
);
661 bgp_export_check(struct bgp_proto
*p
, ea_list
*new)
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
))
670 DBG("\tInvalid NEXT_HOP\n");
674 /* Check if we aren't forbidden to export the route by communities */
675 a
= ea_find(new, EA_CODE(EAP_BGP
, BA_COMMUNITY
));
679 if (int_set_contains(d
, BGP_COMM_NO_ADVERTISE
))
681 DBG("\tNO_ADVERTISE\n");
684 if (!p
->is_internal
&&
685 (int_set_contains(d
, BGP_COMM_NO_EXPORT
) ||
686 int_set_contains(d
, BGP_COMM_NO_EXPORT_SUBCONFED
)))
688 DBG("\tNO_EXPORT\n");
696 static struct bgp_bucket
*
697 bgp_get_bucket(struct bgp_proto
*p
, ea_list
*attrs
, int originate
)
700 unsigned i
, cnt
, hash
, code
;
703 struct bgp_bucket
*b
;
705 /* Merge the attribute list */
706 new = alloca(ea_scan(attrs
));
707 ea_merge(attrs
, new);
710 /* Normalize attributes */
719 byte buf
[EA_FORMAT_BUF_SIZE
];
724 if (EA_PROTO(a
->id
) != EAP_BGP
)
727 if (ATTR_KNOWN(code
))
729 if (!bgp_attr_table
[code
].allow_in_ebgp
&& !p
->is_internal
)
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
;
738 /* Don't re-export unknown non-transitive attributes */
739 if (!(a
->flags
& BAF_TRANSITIVE
))
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
)
747 case EAF_TYPE_INT_SET
:
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);
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))
766 DBG("Found bucket.\n");
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
])))
774 log(L_ERR
"%s: Mandatory attribute %s missing", p
->p
.name
, bgp_attr_table
[bgp_mandatory_attrs
[i
]].name
);
778 if (!bgp_export_check(p
, new))
781 /* Create new bucket */
782 DBG("Creating bucket.\n");
783 return bgp_new_bucket(p
, new, hash
);
787 bgp_free_bucket(struct bgp_proto
*p
, struct bgp_bucket
*buck
)
790 buck
->hash_next
->hash_prev
= buck
->hash_prev
;
792 buck
->hash_prev
->hash_next
= buck
->hash_next
;
794 p
->bucket_hash
[buck
->hash
& (p
->hash_size
-1)] = buck
->hash_next
;
799 bgp_rt_notify(struct proto
*P
, net
*n
, rte
*new, rte
*old UNUSED
, ea_list
*attrs
)
801 struct bgp_proto
*p
= (struct bgp_proto
*) P
;
802 struct bgp_bucket
*buck
;
803 struct bgp_prefix
*px
;
805 DBG("BGP: Got route %I/%d %s\n", n
->n
.prefix
, n
->n
.pxlen
, new ? "up" : "down");
809 buck
= bgp_get_bucket(p
, attrs
, new->attrs
->source
!= RTS_BGP
);
810 if (!buck
) /* Inconsistent attribute list */
815 if (!(buck
= p
->withdraw_bucket
))
817 buck
= p
->withdraw_bucket
= mb_alloc(P
->pool
, sizeof(struct bgp_bucket
));
818 init_list(&buck
->prefixes
);
821 px
= fib_get(&p
->prefix_fib
, &n
->n
.prefix
, n
->n
.pxlen
);
822 if (px
->bucket_node
.next
)
824 DBG("\tRemoving old entry.\n");
825 rem_node(&px
->bucket_node
);
827 add_tail(&buck
->prefixes
, &px
->bucket_node
);
828 bgp_schedule_packet(p
->conn
, PKT_UPDATE
);
833 bgp_create_attrs(struct bgp_proto
*p
, rte
*e
, ea_list
**attrs
, struct linpool
*pool
)
835 ea_list
*ea
= lp_alloc(pool
, sizeof(ea_list
) + 4*sizeof(eattr
));
841 ea
->flags
= EALF_SORTED
;
844 bgp_set_attr(ea
->attrs
, BA_ORIGIN
,
845 ((rta
->source
== RTS_OSPF_EXT1
) || (rta
->source
== RTS_OSPF_EXT2
)) ? ORIGIN_INCOMPLETE
: ORIGIN_IGP
);
848 bgp_set_attr_wa(ea
->attrs
+1, pool
, BA_AS_PATH
, 0);
851 z
= bgp_set_attr_wa(ea
->attrs
+1, pool
, BA_AS_PATH
, bgp_as4_support
? 6 : 4);
852 z
[0] = AS_PATH_SEQUENCE
;
856 put_u32(z
+2, p
->local_as
);
858 put_u16(z
+2, p
->local_as
);
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
);
867 set_next_hop(z
, e
->attrs
->gw
);
869 bgp_set_attr(ea
->attrs
+3, BA_LOCAL_PREF
, 0);
871 return 0; /* Leave decision to the filters */
876 bgp_as_path_loopy(struct bgp_proto
*p
, rta
*a
)
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
));
883 bgp_originator_id_loopy(struct bgp_proto
*p
, rta
*a
)
885 eattr
*e
= ea_find(a
->eattrs
, EA_CODE(EAP_BGP
, BA_ORIGINATOR_ID
));
886 return (e
&& (e
->u
.data
== p
->local_id
));
890 bgp_cluster_list_loopy(struct bgp_proto
*p
, rta
*a
)
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
));
898 bgp_path_prepend(rte
*e
, ea_list
**attrs
, struct linpool
*pool
, u32 as
)
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
));
905 bgp_cluster_list_prepend(rte
*e
, ea_list
**attrs
, struct linpool
*pool
, u32 cid
)
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
));
912 bgp_update_attrs(struct bgp_proto
*p
, rte
*e
, ea_list
**attrs
, struct linpool
*pool
, int rr
)
916 if (!p
->is_internal
&& !p
->rs_client
)
918 bgp_path_prepend(e
, attrs
, pool
, p
->local_as
);
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.
924 a
= ea_find(e
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_MULTI_EXIT_DISC
));
926 bgp_attach_attr(attrs
, pool
, BA_MULTI_EXIT_DISC
, 0);
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
)))
932 /* Leave the original next hop attribute, will check later where does it point */
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
);
943 /* Handling route reflection, RFC 4456 */
944 struct bgp_proto
*src
= (struct bgp_proto
*) e
->attrs
->proto
;
946 a
= ea_find(e
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_ORIGINATOR_ID
));
948 bgp_attach_attr(attrs
, pool
, BA_ORIGINATOR_ID
, src
->remote_id
);
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
);
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
);
958 return 0; /* Leave decision to the filters */
962 bgp_import_control(struct proto
*P
, rte
**new, ea_list
**attrs
, struct linpool
*pool
)
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
;
968 if (p
== new_bgp
) /* Poison reverse updates */
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
))
977 if (p
->local_as
== new_bgp
->local_as
&& p
->is_internal
&& new_bgp
->is_internal
)
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);
987 return bgp_update_attrs(p
, e
, attrs
, pool
, 0);
990 return bgp_create_attrs(p
, e
, attrs
, pool
);
994 bgp_get_neighbor(rte
*r
)
996 eattr
*e
= ea_find(r
->attrs
->eattrs
, EA_CODE(EAP_BGP
, BA_AS_PATH
));
999 if (e
&& as_path_get_first(e
->u
.ptr
, &as
))
1002 return ((struct bgp_proto
*) r
->attrs
->proto
)->remote_as
;
1006 bgp_rte_better(rte
*new, rte
*old
)
1008 struct bgp_proto
*new_bgp
= (struct bgp_proto
*) new->attrs
->proto
;
1009 struct bgp_proto
*old_bgp
= (struct bgp_proto
*) old
->attrs
->proto
;
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
;
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
)
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
;
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
;
1046 /* RFC 4271 9.1.2.2. c) Compare MED's */
1047 if (bgp_get_neighbor(new) == bgp_get_neighbor(old
))
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
;
1059 /* RFC 4271 9.1.2.2. d) Prefer external peers */
1060 if (new_bgp
->is_internal
> old_bgp
->is_internal
)
1062 if (new_bgp
->is_internal
< old_bgp
->is_internal
)
1065 /* Skipping RFC 4271 9.1.2.2. e) */
1066 /* We don't have interior distances */
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;
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
;
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
)
1091 /* rest of RFC 4271 9.1.2.2. f) */
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);
1101 static struct adata
*
1102 bgp_aggregator_convert_to_new(struct adata
*old
, struct linpool
*pool
)
1104 struct adata
*newa
= lp_alloc(pool
, sizeof(struct adata
) + 8);
1106 aggregator_convert_to_new(old
, newa
->data
);
1111 /* Take last req_as ASNs from path old2 (in 2B format), convert to 4B format
1112 * and append path old4 (in 4B format).
1114 static struct adata
*
1115 bgp_merge_as_paths(struct adata
*old2
, struct adata
*old4
, int req_as
, struct linpool
*pool
)
1117 byte buf
[old2
->length
* 2];
1119 int ol
= as_path_convert_to_new(old2
, buf
, req_as
);
1120 int nl
= ol
+ (old4
? old4
->length
: 0);
1122 struct adata
*newa
= lp_alloc(pool
, sizeof(struct adata
) + nl
);
1124 memcpy(newa
->data
, buf
, ol
);
1125 if (old4
) memcpy(newa
->data
+ ol
, old4
->data
, old4
->length
);
1131 as4_aggregator_valid(struct adata
*aggr
)
1133 if (aggr
->length
!= 8)
1136 u32
*a
= (u32
*) aggr
->data
;
1138 if ((a
[0] == 0) || (a
[1] == 0))
1145 /* Reconstruct 4B AS_PATH and AGGREGATOR according to RFC 4893 4.2.3 */
1147 bgp_reconstruct_4b_atts(struct bgp_proto
*p
, rta
*a
, struct linpool
*pool
)
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
));
1155 if (a4
&& !as4_aggregator_valid(a4
->u
.ptr
))
1157 log(L_WARN
"%s: AS4_AGGREGATOR attribute is invalid, skipping attribute", p
->p
.name
);
1164 u32 a2_as
= get_u16(a2
->u
.ptr
->data
);
1168 if (a2_as
!= AS_TRANS
)
1170 /* Routes were aggregated by old router and therefore AS4_PATH
1171 * and AS4_AGGREGATOR is invalid
1173 * Convert AS_PATH and AGGREGATOR to 4B format and finish.
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
);
1183 /* Common case, use AS4_AGGREGATOR attribute */
1184 a2
->u
.ptr
= a4
->u
.ptr
;
1189 /* Common case, use old AGGREGATOR attribute */
1190 a2
->u
.ptr
= bgp_aggregator_convert_to_new(a2
->u
.ptr
, pool
);
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
);
1198 log(L_WARN
"%s: AS4_AGGREGATOR attribute received, but AGGREGATOR attribute is missing", p
->p
.name
);
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;
1203 if (p4
&& (p4_len
< 0))
1204 log(L_WARN
"%s: AS4_PATH attribute is malformed, skipping attribute", p
->p
.name
);
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
);
1209 p2
->u
.ptr
= bgp_merge_as_paths(p2
->u
.ptr
, p4
->u
.ptr
, p2_len
- p4_len
, pool
);
1213 bgp_remove_as4_attrs(struct bgp_proto
*p
, rta
*a
)
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
);
1219 /* We know that ea_lists constructed in bgp_decode attrs have one attribute per ea_list struct */
1222 unsigned fid
= (*el
)->attrs
[0].id
;
1224 if ((fid
== id1
) || (fid
== id2
))
1228 log(L_WARN
"BGP: Unexpected AS4_* attributes received");
1231 el
= &((*el
)->next
);
1236 * bgp_decode_attrs - check and decode BGP attributes
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
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
1248 bgp_decode_attrs(struct bgp_conn
*conn
, byte
*attr
, unsigned int len
, struct linpool
*pool
, int mandatory
)
1250 struct bgp_proto
*bgp
= conn
->bgp
;
1251 rta
*a
= lp_alloc(pool
, sizeof(struct rta
));
1252 unsigned int flags
, code
, l
, i
, type
;
1254 byte
*z
, *attr_start
;
1260 a
->source
= RTS_BGP
;
1261 a
->scope
= SCOPE_UNIVERSE
;
1262 a
->cast
= RTC_UNICAST
;
1263 a
->dest
= RTD_ROUTER
;
1266 a
->from
= bgp
->cf
->remote_ip
;
1269 /* Parse the attributes */
1270 bzero(seen
, sizeof(seen
));
1271 DBG("BGP: Parsing attributes\n");
1280 if (flags
& BAF_EXT_LEN
)
1300 DBG("Attr %02x %02x %d\n", code
, flags
, l
);
1301 if (seen
[code
/8] & (1 << (code
%8)))
1303 if (ATTR_KNOWN(code
))
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
)
1314 errcode
= desc
->validate(bgp
, z
, l
);
1320 else if (code
== BA_AS_PATH
)
1322 /* Special case as it might also trim the attribute */
1323 if (validate_as_path(bgp
, z
, &l
) < 0)
1324 { errcode
= 11; goto err
; }
1328 else /* Unknown attribute */
1330 if (!(flags
& BAF_OPTIONAL
))
1331 { errcode
= 2; goto err
; }
1332 type
= EAF_TYPE_OPAQUE
;
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; }
1339 seen
[code
/8] |= (1 << (code
%8));
1340 ea
= lp_alloc(pool
, sizeof(ea_list
) + sizeof(eattr
));
1341 ea
->next
= a
->eattrs
;
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
)
1352 ad
= lp_alloc(pool
, sizeof(struct adata
) + l
);
1353 ea
->attrs
[0].u
.ptr
= ad
;
1355 memcpy(ad
->data
, z
, l
);
1359 case EAF_TYPE_ROUTER_ID
:
1362 ea
->attrs
[0].u
.data
= *z
;
1364 ea
->attrs
[0].u
.data
= get_u32(z
);
1366 case EAF_TYPE_IP_ADDRESS
:
1367 ipa_ntoh(*(ip_addr
*)ad
->data
);
1369 case EAF_TYPE_INT_SET
:
1371 u32
*z
= (u32
*) ad
->data
;
1372 for(i
=0; i
<ad
->length
/4; i
++)
1380 /* If we received MP_REACH_NLRI we should check mandatory attributes */
1381 if (bgp
->mp_reach_len
!= 0)
1385 /* If there is no (reachability) NLRI, we should exit now */
1389 /* Check if all mandatory attributes are present */
1390 for(i
=0; i
< ARRAY_SIZE(bgp_mandatory_attrs
); i
++)
1392 code
= bgp_mandatory_attrs
[i
];
1393 if (!(seen
[code
/8] & (1 << (code
%8))))
1395 bgp_error(conn
, 3, 3, &bgp_mandatory_attrs
[i
], 1);
1400 /* When receiving attributes from non-AS4-aware BGP speaker,
1401 * we have to reconstruct 4B AS_PATH and AGGREGATOR attributes
1403 if (bgp_as4_support
&& (! bgp
->as4_session
))
1404 bgp_reconstruct_4b_atts(bgp
, a
, pool
);
1406 if (bgp_as4_support
)
1407 bgp_remove_as4_attrs(bgp
, a
);
1409 /* If the AS path attribute contains our AS, reject the routes */
1410 if (bgp_as_path_loopy(bgp
, a
))
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
))
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);
1425 DBG("BGP: Path loop!\n");
1429 bgp_error(conn
, 3, 1, NULL
, 0);
1433 bgp_error(conn
, 3, errcode
, attr_start
, z
+l
-attr_start
);
1438 bgp_get_attr(eattr
*a
, byte
*buf
, int buflen
)
1440 unsigned int i
= EA_ID(a
->id
);
1441 struct attr_desc
*d
;
1445 d
= &bgp_attr_table
[i
];
1446 buf
+= bsprintf(buf
, "%s", d
->name
);
1451 d
->format(a
, buf
, buflen
);
1456 bsprintf(buf
, "%02x%s", i
, (a
->flags
& BAF_TRANSITIVE
) ? " [t]" : "");
1461 bgp_attr_init(struct bgp_proto
*p
)
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
);
1472 bgp_get_route_info(rte
*e
, byte
*buf
, ea_list
*attrs
)
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
));
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
);
1482 buf
+= bsprintf(buf
, "%c", "ie?"[o
->u
.data
]);