2 * BIRD -- BGP Attributes
4 * (c) 2000 Martin Mares <mj@ucw.cz>
5 * (c) 2008--2016 Ondrej Zajicek <santiago@crfreenet.org>
6 * (c) 2008--2016 CZ.NIC z.s.p.o.
8 * Can be freely distributed and used under the terms of the GNU GPL.
15 #include "nest/bird.h"
16 #include "nest/iface.h"
17 #include "nest/protocol.h"
18 #include "nest/route.h"
19 #include "nest/attrs.h"
20 #include "conf/conf.h"
21 #include "lib/resource.h"
22 #include "lib/string.h"
23 #include "lib/unaligned.h"
28 * UPDATE message error handling
30 * All checks from RFC 4271 6.3 are done as specified with these exceptions:
31 * - The semantic check of an IP address from NEXT_HOP attribute is missing.
32 * - Checks of some optional attribute values are missing.
33 * - Syntactic and semantic checks of NLRIs (done in DECODE_PREFIX())
34 * are probably inadequate.
36 * Loop detection based on AS_PATH causes updates to be withdrawn. RFC
37 * 4271 does not explicitly specifiy the behavior in that case.
39 * Loop detection related to route reflection (based on ORIGINATOR_ID
40 * and CLUSTER_LIST) causes updates to be withdrawn. RFC 4456 8
41 * specifies that such updates should be ignored, but that is generally
44 * BGP attribute table has several hooks:
46 * export - Hook that validates and normalizes attribute during export phase.
47 * Receives eattr, may modify it (e.g., sort community lists for canonical
48 * representation), UNSET() it (e.g., skip empty lists), or WITHDRAW() it if
49 * necessary. May assume that eattr has value valid w.r.t. its type, but may be
50 * invalid w.r.t. BGP constraints. Optional.
52 * encode - Hook that converts internal representation to external one during
53 * packet writing. Receives eattr and puts it in the buffer (including attribute
54 * header). Returns number of bytes, or -1 if not enough space. May assume that
55 * eattr has value valid w.r.t. its type and validated by export hook. Mandatory
56 * for all known attributes that exist internally after export phase (i.e., all
57 * except pseudoattributes MP_(UN)REACH_NLRI).
59 * decode - Hook that converts external representation to internal one during
60 * packet parsing. Receives attribute data in buffer, validates it and adds
61 * attribute to ea_list. If data are invalid, steps DISCARD(), WITHDRAW() or
62 * bgp_parse_error() may be used to escape. Mandatory for all known attributes.
64 * format - Optional hook that converts eattr to textual representation.
68 struct bgp_attr_desc
{
72 void (*export
)(struct bgp_export_state
*s
, eattr
*a
);
73 int (*encode
)(struct bgp_write_state
*s
, eattr
*a
, byte
*buf
, uint size
);
74 void (*decode
)(struct bgp_parse_state
*s
, uint code
, uint flags
, byte
*data
, uint len
, ea_list
**to
);
75 void (*format
)(eattr
*ea
, byte
*buf
, uint size
);
78 static const struct bgp_attr_desc bgp_attr_table
[];
80 static inline int bgp_attr_known(uint code
);
83 bgp_set_attr(ea_list
**attrs
, struct linpool
*pool
, uint code
, uint flags
, uintptr_t val
)
85 ASSERT(bgp_attr_known(code
));
90 EA_CODE(PROTOCOL_BGP
, code
),
92 bgp_attr_table
[code
].type
,
99 #define REPORT(msg, args...) \
100 ({ log(L_REMOTE "%s: " msg, s->proto->p.name, ## args); })
102 #define DISCARD(msg, args...) \
103 ({ REPORT(msg, ## args); return; })
105 #define WITHDRAW(msg, args...) \
106 ({ REPORT(msg, ## args); s->err_withdraw = 1; return; })
109 ({ a->type = EAF_TYPE_UNDEF; return; })
111 #define NEW_BGP "Discarding %s attribute received from AS4-aware neighbor"
112 #define BAD_EBGP "Discarding %s attribute received from EBGP neighbor"
113 #define BAD_LENGTH "Malformed %s attribute - invalid length (%u)"
114 #define BAD_VALUE "Malformed %s attribute - invalid value (%u)"
115 #define NO_MANDATORY "Missing mandatory %s attribute"
119 bgp_put_attr_hdr3(byte
*buf
, uint code
, uint flags
, uint len
)
128 bgp_put_attr_hdr4(byte
*buf
, uint code
, uint flags
, uint len
)
130 *buf
++ = flags
| BAF_EXT_LEN
;
137 bgp_put_attr_hdr(byte
*buf
, uint code
, uint flags
, uint len
)
140 return bgp_put_attr_hdr3(buf
, code
, flags
, len
);
142 return bgp_put_attr_hdr4(buf
, code
, flags
, len
);
146 bgp_encode_u8(struct bgp_write_state
*s UNUSED
, eattr
*a
, byte
*buf
, uint size
)
151 bgp_put_attr_hdr3(buf
, EA_ID(a
->id
), a
->flags
, 1);
158 bgp_encode_u32(struct bgp_write_state
*s UNUSED
, eattr
*a
, byte
*buf
, uint size
)
163 bgp_put_attr_hdr3(buf
, EA_ID(a
->id
), a
->flags
, 4);
164 put_u32(buf
+3, a
->u
.data
);
170 bgp_encode_u32s(struct bgp_write_state
*s UNUSED
, eattr
*a
, byte
*buf
, uint size
)
172 uint len
= a
->u
.ptr
->length
;
177 uint hdr
= bgp_put_attr_hdr(buf
, EA_ID(a
->id
), a
->flags
, len
);
178 put_u32s(buf
+ hdr
, (u32
*) a
->u
.ptr
->data
, len
/ 4);
184 bgp_put_attr(byte
*buf
, uint size
, uint code
, uint flags
, byte
*data
, uint len
)
189 uint hdr
= bgp_put_attr_hdr(buf
, code
, flags
, len
);
190 memcpy(buf
+ hdr
, data
, len
);
196 bgp_encode_raw(struct bgp_write_state
*s UNUSED
, eattr
*a
, byte
*buf
, uint size
)
198 return bgp_put_attr(buf
, size
, EA_ID(a
->id
), a
->flags
, a
->u
.ptr
->data
, a
->u
.ptr
->length
);
207 bgp_export_origin(struct bgp_export_state
*s
, eattr
*a
)
210 WITHDRAW(BAD_VALUE
, "ORIGIN", a
->u
.data
);
214 bgp_decode_origin(struct bgp_parse_state
*s
, uint code UNUSED
, uint flags
, byte
*data
, uint len
, ea_list
**to
)
217 WITHDRAW(BAD_LENGTH
, "ORIGIN", len
);
220 WITHDRAW(BAD_VALUE
, "ORIGIN", data
[0]);
222 bgp_set_attr_u32(to
, s
->pool
, BA_ORIGIN
, flags
, data
[0]);
226 bgp_format_origin(eattr
*a
, byte
*buf
, uint size UNUSED
)
228 static const char *bgp_origin_names
[] = { "IGP", "EGP", "Incomplete" };
230 bsprintf(buf
, (a
->u
.data
<= 2) ? bgp_origin_names
[a
->u
.data
] : "?");
235 bgp_encode_as_path(struct bgp_write_state
*s
, eattr
*a
, byte
*buf
, uint size
)
237 byte
*data
= a
->u
.ptr
->data
;
238 uint len
= a
->u
.ptr
->length
;
242 /* Prepare 16-bit AS_PATH (from 32-bit one) in a temporary buffer */
245 len
= as_path_32to16(data
, src
, len
);
248 return bgp_put_attr(buf
, size
, BA_AS_PATH
, a
->flags
, data
, len
);
252 bgp_decode_as_path(struct bgp_parse_state
*s
, uint code UNUSED
, uint flags
, byte
*data
, uint len
, ea_list
**to
)
254 struct bgp_proto
*p
= s
->proto
;
255 int as_length
= s
->as4_session
? 4 : 2;
256 int as_confed
= p
->cf
->confederation
&& p
->is_interior
;
259 if (!as_path_valid(data
, len
, as_length
, as_confed
, err
, sizeof(err
)))
260 WITHDRAW("Malformed AS_PATH attribute - %s", err
);
262 /* In some circumstances check for initial AS_CONFED_SEQUENCE; RFC 5065 5.0 */
263 if (p
->is_interior
&& !p
->is_internal
&&
264 ((len
< 2) || (data
[0] != AS_PATH_CONFED_SEQUENCE
)))
265 WITHDRAW("Malformed AS_PATH attribute - %s", "missing initial AS_CONFED_SEQUENCE");
269 /* Prepare 32-bit AS_PATH (from 16-bit one) in a temporary buffer */
271 data
= alloca(2*len
);
272 len
= as_path_16to32(data
, src
, len
);
275 bgp_set_attr_data(to
, s
->pool
, BA_AS_PATH
, flags
, data
, len
);
280 bgp_encode_next_hop(struct bgp_write_state
*s
, eattr
*a
, byte
*buf
, uint size
)
283 * The NEXT_HOP attribute is used only in traditional (IPv4) BGP. In MP-BGP,
284 * the next hop is encoded as a part of the MP_REACH_NLRI attribute, so we
285 * store it and encode it later by AFI-specific hooks.
288 if ((s
->channel
->afi
== BGP_AF_IPV4
) && !s
->channel
->ext_next_hop
)
290 ASSERT(a
->u
.ptr
->length
== sizeof(ip_addr
));
295 bgp_put_attr_hdr3(buf
, BA_NEXT_HOP
, a
->flags
, 4);
296 put_ip4(buf
+3, ipa_to_ip4( *(ip_addr
*) a
->u
.ptr
->data
));
308 bgp_decode_next_hop(struct bgp_parse_state
*s
, uint code UNUSED
, uint flags UNUSED
, byte
*data
, uint len
, ea_list
**to UNUSED
)
311 WITHDRAW(BAD_LENGTH
, "NEXT_HOP", len
);
313 /* Semantic checks are done later */
314 s
->ip_next_hop_len
= len
;
315 s
->ip_next_hop_data
= data
;
318 /* TODO: This function should use AF-specific hook */
320 bgp_format_next_hop(eattr
*a
, byte
*buf
, uint size UNUSED
)
322 ip_addr
*nh
= (void *) a
->u
.ptr
->data
;
323 uint len
= a
->u
.ptr
->length
;
325 ASSERT((len
== 16) || (len
== 32));
327 /* in IPv6, we may have two addresses in NEXT HOP */
328 if ((len
== 16) || ipa_zero(nh
[1]))
329 bsprintf(buf
, "%I", nh
[0]);
331 bsprintf(buf
, "%I %I", nh
[0], nh
[1]);
336 bgp_decode_med(struct bgp_parse_state
*s
, uint code UNUSED
, uint flags
, byte
*data
, uint len
, ea_list
**to
)
339 WITHDRAW(BAD_LENGTH
, "MULTI_EXIT_DISC", len
);
341 u32 val
= get_u32(data
);
342 bgp_set_attr_u32(to
, s
->pool
, BA_MULTI_EXIT_DISC
, flags
, val
);
347 bgp_export_local_pref(struct bgp_export_state
*s
, eattr
*a
)
349 if (!s
->proto
->is_interior
&& !s
->proto
->cf
->allow_local_pref
)
354 bgp_decode_local_pref(struct bgp_parse_state
*s
, uint code UNUSED
, uint flags
, byte
*data
, uint len
, ea_list
**to
)
356 if (!s
->proto
->is_interior
&& !s
->proto
->cf
->allow_local_pref
)
357 DISCARD(BAD_EBGP
, "LOCAL_PREF");
360 WITHDRAW(BAD_LENGTH
, "LOCAL_PREF", len
);
362 u32 val
= get_u32(data
);
363 bgp_set_attr_u32(to
, s
->pool
, BA_LOCAL_PREF
, flags
, val
);
368 bgp_decode_atomic_aggr(struct bgp_parse_state
*s
, uint code UNUSED
, uint flags
, byte
*data UNUSED
, uint len
, ea_list
**to
)
371 DISCARD(BAD_LENGTH
, "ATOMIC_AGGR", len
);
373 bgp_set_attr_data(to
, s
->pool
, BA_ATOMIC_AGGR
, flags
, NULL
, 0);
377 bgp_encode_aggregator(struct bgp_write_state
*s
, eattr
*a
, byte
*buf
, uint size
)
379 byte
*data
= a
->u
.ptr
->data
;
380 uint len
= a
->u
.ptr
->length
;
384 /* Prepare 16-bit AGGREGATOR (from 32-bit one) in a temporary buffer */
387 len
= aggregator_32to16(data
, src
);
390 return bgp_put_attr(buf
, size
, BA_AGGREGATOR
, a
->flags
, data
, len
);
394 bgp_decode_aggregator(struct bgp_parse_state
*s
, uint code UNUSED
, uint flags
, byte
*data
, uint len
, ea_list
**to
)
396 if (len
!= (s
->as4_session
? 8 : 6))
397 DISCARD(BAD_LENGTH
, "AGGREGATOR", len
);
401 /* Prepare 32-bit AGGREGATOR (from 16-bit one) in a temporary buffer */
404 len
= aggregator_16to32(data
, src
);
407 bgp_set_attr_data(to
, s
->pool
, BA_AGGREGATOR
, flags
, data
, len
);
411 bgp_format_aggregator(eattr
*a
, byte
*buf
, uint size UNUSED
)
413 byte
*data
= a
->u
.ptr
->data
;
415 bsprintf(buf
, "%I4 AS%u", get_ip4(data
+4), get_u32(data
+0));
420 bgp_export_community(struct bgp_export_state
*s
, eattr
*a
)
422 if (a
->u
.ptr
->length
== 0)
425 a
->u
.ptr
= int_set_sort(s
->pool
, a
->u
.ptr
);
429 bgp_decode_community(struct bgp_parse_state
*s
, uint code UNUSED
, uint flags
, byte
*data
, uint len
, ea_list
**to
)
431 if (!len
|| (len
% 4))
432 WITHDRAW(BAD_LENGTH
, "COMMUNITY", len
);
434 struct adata
*ad
= lp_alloc_adata(s
->pool
, len
);
435 get_u32s(data
, (u32
*) ad
->data
, len
/ 4);
436 bgp_set_attr_ptr(to
, s
->pool
, BA_COMMUNITY
, flags
, ad
);
441 bgp_export_originator_id(struct bgp_export_state
*s
, eattr
*a
)
443 if (!s
->proto
->is_internal
)
448 bgp_decode_originator_id(struct bgp_parse_state
*s
, uint code UNUSED
, uint flags
, byte
*data
, uint len
, ea_list
**to
)
450 if (!s
->proto
->is_internal
)
451 DISCARD(BAD_EBGP
, "ORIGINATOR_ID");
454 WITHDRAW(BAD_LENGTH
, "ORIGINATOR_ID", len
);
456 u32 val
= get_u32(data
);
457 bgp_set_attr_u32(to
, s
->pool
, BA_ORIGINATOR_ID
, flags
, val
);
462 bgp_export_cluster_list(struct bgp_export_state
*s UNUSED
, eattr
*a
)
464 if (!s
->proto
->is_internal
)
467 if (a
->u
.ptr
->length
== 0)
472 bgp_decode_cluster_list(struct bgp_parse_state
*s
, uint code UNUSED
, uint flags
, byte
*data
, uint len
, ea_list
**to
)
474 if (!s
->proto
->is_internal
)
475 DISCARD(BAD_EBGP
, "CLUSTER_LIST");
477 if (!len
|| (len
% 4))
478 WITHDRAW(BAD_LENGTH
, "CLUSTER_LIST", len
);
480 struct adata
*ad
= lp_alloc_adata(s
->pool
, len
);
481 get_u32s(data
, (u32
*) ad
->data
, len
/ 4);
482 bgp_set_attr_ptr(to
, s
->pool
, BA_CLUSTER_LIST
, flags
, ad
);
486 bgp_format_cluster_list(eattr
*a
, byte
*buf
, uint size
)
488 /* Truncates cluster lists larger than buflen, probably not a problem */
489 int_set_format(a
->u
.ptr
, 0, -1, buf
, size
);
496 return (get_u16(buf
) << 16) | buf
[2];
500 bgp_decode_mp_reach_nlri(struct bgp_parse_state
*s
, uint code UNUSED
, uint flags UNUSED
, byte
*data
, uint len
, ea_list
**to UNUSED
)
503 * 2 B MP_REACH_NLRI data - Address Family Identifier
504 * 1 B MP_REACH_NLRI data - Subsequent Address Family Identifier
505 * 1 B MP_REACH_NLRI data - Length of Next Hop Network Address
506 * var MP_REACH_NLRI data - Network Address of Next Hop
507 * 1 B MP_REACH_NLRI data - Reserved (zero)
508 * var MP_REACH_NLRI data - Network Layer Reachability Information
511 if ((len
< 5) || (len
< (5 + (uint
) data
[3])))
512 bgp_parse_error(s
, 9);
514 s
->mp_reach_af
= get_af3(data
);
515 s
->mp_next_hop_len
= data
[3];
516 s
->mp_next_hop_data
= data
+ 4;
517 s
->mp_reach_len
= len
- 5 - s
->mp_next_hop_len
;
518 s
->mp_reach_nlri
= data
+ 5 + s
->mp_next_hop_len
;
523 bgp_decode_mp_unreach_nlri(struct bgp_parse_state
*s
, uint code UNUSED
, uint flags UNUSED
, byte
*data
, uint len
, ea_list
**to UNUSED
)
526 * 2 B MP_UNREACH_NLRI data - Address Family Identifier
527 * 1 B MP_UNREACH_NLRI data - Subsequent Address Family Identifier
528 * var MP_UNREACH_NLRI data - Network Layer Reachability Information
532 bgp_parse_error(s
, 9);
534 s
->mp_unreach_af
= get_af3(data
);
535 s
->mp_unreach_len
= len
- 3;
536 s
->mp_unreach_nlri
= data
+ 3;
541 bgp_export_ext_community(struct bgp_export_state
*s
, eattr
*a
)
543 a
->u
.ptr
= ec_set_del_nontrans(s
->pool
, a
->u
.ptr
);
545 if (a
->u
.ptr
->length
== 0)
548 ec_set_sort_x(a
->u
.ptr
);
552 bgp_decode_ext_community(struct bgp_parse_state
*s
, uint code UNUSED
, uint flags
, byte
*data
, uint len
, ea_list
**to
)
554 if (!len
|| (len
% 8))
555 WITHDRAW(BAD_LENGTH
, "EXT_COMMUNITY", len
);
557 struct adata
*ad
= lp_alloc_adata(s
->pool
, len
);
558 get_u32s(data
, (u32
*) ad
->data
, len
/ 4);
559 bgp_set_attr_ptr(to
, s
->pool
, BA_EXT_COMMUNITY
, flags
, ad
);
564 bgp_decode_as4_aggregator(struct bgp_parse_state
*s
, uint code UNUSED
, uint flags
, byte
*data
, uint len
, ea_list
**to
)
567 DISCARD(NEW_BGP
, "AS4_AGGREGATOR");
570 DISCARD(BAD_LENGTH
, "AS4_AGGREGATOR", len
);
572 bgp_set_attr_data(to
, s
->pool
, BA_AS4_AGGREGATOR
, flags
, data
, len
);
576 bgp_decode_as4_path(struct bgp_parse_state
*s
, uint code UNUSED
, uint flags
, byte
*data
, uint len
, ea_list
**to
)
581 DISCARD(NEW_BGP
, "AS4_PATH");
584 DISCARD(BAD_LENGTH
, "AS4_PATH", len
);
586 if (!as_path_valid(data
, len
, 4, 1, err
, sizeof(err
)))
587 DISCARD("Malformed AS4_PATH attribute - %s", err
);
589 struct adata
*a
= lp_alloc_adata(s
->pool
, len
);
590 memcpy(a
->data
, data
, len
);
592 /* AS_CONFED* segments are invalid in AS4_PATH; RFC 6793 6 */
593 if (as_path_contains_confed(a
))
595 REPORT("Discarding AS_CONFED* segment from AS4_PATH attribute");
596 a
= as_path_strip_confed(s
->pool
, a
);
599 bgp_set_attr_ptr(to
, s
->pool
, BA_AS4_PATH
, flags
, a
);
603 bgp_export_large_community(struct bgp_export_state
*s
, eattr
*a
)
605 if (a
->u
.ptr
->length
== 0)
608 a
->u
.ptr
= lc_set_sort(s
->pool
, a
->u
.ptr
);
612 bgp_decode_large_community(struct bgp_parse_state
*s
, uint code UNUSED
, uint flags
, byte
*data
, uint len
, ea_list
**to
)
614 if (!len
|| (len
% 12))
615 WITHDRAW(BAD_LENGTH
, "LARGE_COMMUNITY", len
);
617 struct adata
*ad
= lp_alloc_adata(s
->pool
, len
);
618 get_u32s(data
, (u32
*) ad
->data
, len
/ 4);
619 bgp_set_attr_ptr(to
, s
->pool
, BA_LARGE_COMMUNITY
, flags
, ad
);
623 bgp_export_mpls_label_stack(struct bgp_export_state
*s
, eattr
*a
)
625 net_addr
*n
= s
->route
->net
->n
.addr
;
626 u32
*labels
= (u32
*) a
->u
.ptr
->data
;
627 uint lnum
= a
->u
.ptr
->length
/ 4;
629 /* Perhaps we should just ignore it? */
631 WITHDRAW("Unexpected MPLS stack");
633 /* Empty MPLS stack is not allowed */
635 WITHDRAW("Malformed MPLS stack - empty");
637 /* This is ugly, but we must ensure that labels fit into NLRI field */
638 if ((24*lnum
+ (net_is_vpn(n
) ? 64 : 0) + net_pxlen(n
)) > 255)
639 WITHDRAW("Malformed MPLS stack - too many labels (%u)", lnum
);
641 for (uint i
= 0; i
< lnum
; i
++)
643 if (labels
[i
] > 0xfffff)
644 WITHDRAW("Malformed MPLS stack - invalid label (%u)", labels
[i
]);
646 /* TODO: Check for special-purpose label values? */
651 bgp_encode_mpls_label_stack(struct bgp_write_state
*s
, eattr
*a
, byte
*buf UNUSED
, uint size UNUSED
)
654 * MPLS labels are encoded as a part of the NLRI in MP_REACH_NLRI attribute,
655 * so we store MPLS_LABEL_STACK and encode it later by AFI-specific hooks.
658 s
->mpls_labels
= a
->u
.ptr
;
663 bgp_decode_mpls_label_stack(struct bgp_parse_state
*s
, uint code UNUSED
, uint flags UNUSED
, byte
*data UNUSED
, uint len UNUSED
, ea_list
**to UNUSED
)
665 DISCARD("Discarding received attribute #0");
669 bgp_format_mpls_label_stack(eattr
*a
, byte
*buf
, uint size
)
671 u32
*labels
= (u32
*) a
->u
.ptr
->data
;
672 uint lnum
= a
->u
.ptr
->length
/ 4;
675 for (uint i
= 0; i
< lnum
; i
++)
679 bsprintf(pos
, "...");
683 uint l
= bsprintf(pos
, "%d/", labels
[i
]);
684 ADVANCE(pos
, size
, l
);
687 /* Clear last slash or terminate empty string */
688 pos
[lnum
? -1 : 0] = 0;
692 bgp_decode_unknown(struct bgp_parse_state
*s
, uint code
, uint flags
, byte
*data
, uint len
, ea_list
**to
)
694 /* Cannot use bgp_set_attr_data() as it works on known attributes only */
695 ea_set_attr_data(to
, s
->pool
, EA_CODE(PROTOCOL_BGP
, code
), flags
, EAF_TYPE_OPAQUE
, data
, len
);
703 static const struct bgp_attr_desc bgp_attr_table
[] = {
706 .type
= EAF_TYPE_INT
,
707 .flags
= BAF_TRANSITIVE
,
708 .export
= bgp_export_origin
,
709 .encode
= bgp_encode_u8
,
710 .decode
= bgp_decode_origin
,
711 .format
= bgp_format_origin
,
715 .type
= EAF_TYPE_AS_PATH
,
716 .flags
= BAF_TRANSITIVE
,
717 .encode
= bgp_encode_as_path
,
718 .decode
= bgp_decode_as_path
,
722 .type
= EAF_TYPE_IP_ADDRESS
,
723 .flags
= BAF_TRANSITIVE
,
724 .encode
= bgp_encode_next_hop
,
725 .decode
= bgp_decode_next_hop
,
726 .format
= bgp_format_next_hop
,
728 [BA_MULTI_EXIT_DISC
] = {
730 .type
= EAF_TYPE_INT
,
731 .flags
= BAF_OPTIONAL
,
732 .encode
= bgp_encode_u32
,
733 .decode
= bgp_decode_med
,
736 .name
= "local_pref",
737 .type
= EAF_TYPE_INT
,
738 .flags
= BAF_TRANSITIVE
,
739 .export
= bgp_export_local_pref
,
740 .encode
= bgp_encode_u32
,
741 .decode
= bgp_decode_local_pref
,
744 .name
= "atomic_aggr",
745 .type
= EAF_TYPE_OPAQUE
,
746 .flags
= BAF_TRANSITIVE
,
747 .encode
= bgp_encode_raw
,
748 .decode
= bgp_decode_atomic_aggr
,
751 .name
= "aggregator",
752 .type
= EAF_TYPE_OPAQUE
,
753 .flags
= BAF_OPTIONAL
| BAF_TRANSITIVE
,
754 .encode
= bgp_encode_aggregator
,
755 .decode
= bgp_decode_aggregator
,
756 .format
= bgp_format_aggregator
,
760 .type
= EAF_TYPE_INT_SET
,
761 .flags
= BAF_OPTIONAL
| BAF_TRANSITIVE
,
762 .export
= bgp_export_community
,
763 .encode
= bgp_encode_u32s
,
764 .decode
= bgp_decode_community
,
766 [BA_ORIGINATOR_ID
] = {
767 .name
= "originator_id",
768 .type
= EAF_TYPE_ROUTER_ID
,
769 .flags
= BAF_OPTIONAL
,
770 .export
= bgp_export_originator_id
,
771 .encode
= bgp_encode_u32
,
772 .decode
= bgp_decode_originator_id
,
774 [BA_CLUSTER_LIST
] = {
775 .name
= "cluster_list",
776 .type
= EAF_TYPE_INT_SET
,
777 .flags
= BAF_OPTIONAL
,
778 .export
= bgp_export_cluster_list
,
779 .encode
= bgp_encode_u32s
,
780 .decode
= bgp_decode_cluster_list
,
781 .format
= bgp_format_cluster_list
,
783 [BA_MP_REACH_NLRI
] = {
784 .name
= "mp_reach_nlri",
785 .type
= EAF_TYPE_OPAQUE
,
786 .flags
= BAF_OPTIONAL
,
787 .decode
= bgp_decode_mp_reach_nlri
,
789 [BA_MP_UNREACH_NLRI
] = {
790 .name
= "mp_unreach_nlri",
791 .type
= EAF_TYPE_OPAQUE
,
792 .flags
= BAF_OPTIONAL
,
793 .decode
= bgp_decode_mp_unreach_nlri
,
795 [BA_EXT_COMMUNITY
] = {
796 .name
= "ext_community",
797 .type
= EAF_TYPE_EC_SET
,
798 .flags
= BAF_OPTIONAL
| BAF_TRANSITIVE
,
799 .export
= bgp_export_ext_community
,
800 .encode
= bgp_encode_u32s
,
801 .decode
= bgp_decode_ext_community
,
805 .type
= EAF_TYPE_AS_PATH
,
806 .flags
= BAF_OPTIONAL
| BAF_TRANSITIVE
,
807 .encode
= bgp_encode_raw
,
808 .decode
= bgp_decode_as4_path
,
810 [BA_AS4_AGGREGATOR
] = {
811 .name
= "as4_aggregator",
812 .type
= EAF_TYPE_OPAQUE
,
813 .flags
= BAF_OPTIONAL
| BAF_TRANSITIVE
,
814 .encode
= bgp_encode_raw
,
815 .decode
= bgp_decode_as4_aggregator
,
816 .format
= bgp_format_aggregator
,
818 [BA_LARGE_COMMUNITY
] = {
819 .name
= "large_community",
820 .type
= EAF_TYPE_LC_SET
,
821 .flags
= BAF_OPTIONAL
| BAF_TRANSITIVE
,
822 .export
= bgp_export_large_community
,
823 .encode
= bgp_encode_u32s
,
824 .decode
= bgp_decode_large_community
,
826 [BA_MPLS_LABEL_STACK
] = {
827 .name
= "mpls_label_stack",
828 .type
= EAF_TYPE_INT_SET
,
829 .export
= bgp_export_mpls_label_stack
,
830 .encode
= bgp_encode_mpls_label_stack
,
831 .decode
= bgp_decode_mpls_label_stack
,
832 .format
= bgp_format_mpls_label_stack
,
837 bgp_attr_known(uint code
)
839 return (code
< ARRAY_SIZE(bgp_attr_table
)) && bgp_attr_table
[code
].name
;
848 bgp_export_attr(struct bgp_export_state
*s
, eattr
*a
, ea_list
*to
)
850 if (EA_PROTO(a
->id
) != PROTOCOL_BGP
)
853 uint code
= EA_ID(a
->id
);
855 if (bgp_attr_known(code
))
857 const struct bgp_attr_desc
*desc
= &bgp_attr_table
[code
];
859 /* The flags might have been zero if the attr was added by filters */
860 a
->flags
= (a
->flags
& BAF_PARTIAL
) | desc
->flags
;
862 /* Set partial bit if new opt-trans attribute is attached to non-local route */
863 if ((s
->src
!= NULL
) && (a
->type
& EAF_ORIGINATED
) &&
864 (a
->flags
& BAF_OPTIONAL
) && (a
->flags
& BAF_TRANSITIVE
))
865 a
->flags
|= BAF_PARTIAL
;
867 /* Call specific hook */
868 CALL(desc
->export
, s
, a
);
870 /* Attribute might become undefined in hook */
871 if ((a
->type
& EAF_TYPE_MASK
) == EAF_TYPE_UNDEF
)
876 /* Don't re-export unknown non-transitive attributes */
877 if (!(a
->flags
& BAF_TRANSITIVE
))
880 a
->flags
|= BAF_PARTIAL
;
883 /* Append updated attribute */
884 to
->attrs
[to
->count
++] = *a
;
888 * bgp_export_attrs - export BGP attributes
889 * @s: BGP export state
890 * @attrs: a list of extended attributes
892 * The bgp_export_attrs() function takes a list of attributes and merges it to
893 * one newly allocated and sorted segment. Attributes are validated and
894 * normalized by type-specific export hooks and attribute flags are updated.
895 * Some attributes may be eliminated (e.g. unknown non-tranitive attributes, or
896 * empty community sets).
898 * Result: one sorted attribute list segment, or NULL if attributes are unsuitable.
900 static inline ea_list
*
901 bgp_export_attrs(struct bgp_export_state
*s
, ea_list
*attrs
)
903 /* Merge the attribute list */
904 ea_list
*new = lp_alloc(s
->pool
, ea_scan(attrs
));
905 ea_merge(attrs
, new);
912 /* Export each attribute */
913 for (i
= 0; i
< count
; i
++)
914 bgp_export_attr(s
, &new->attrs
[i
], new);
928 bgp_encode_attr(struct bgp_write_state
*s
, eattr
*a
, byte
*buf
, uint size
)
930 ASSERT(EA_PROTO(a
->id
) == PROTOCOL_BGP
);
932 uint code
= EA_ID(a
->id
);
934 if (bgp_attr_known(code
))
935 return bgp_attr_table
[code
].encode(s
, a
, buf
, size
);
937 return bgp_encode_raw(s
, a
, buf
, size
);
941 * bgp_encode_attrs - encode BGP attributes
942 * @s: BGP write state
943 * @attrs: a list of extended attributes
947 * The bgp_encode_attrs() function takes a list of extended attributes
948 * and converts it to its BGP representation (a part of an Update message).
950 * Result: Length of the attribute block generated or -1 if not enough space.
953 bgp_encode_attrs(struct bgp_write_state
*s
, ea_list
*attrs
, byte
*buf
, byte
*end
)
958 for (i
= 0; i
< attrs
->count
; i
++)
960 len
= bgp_encode_attr(s
, &attrs
->attrs
[i
], pos
, end
- pos
);
976 static void bgp_process_as4_attrs(ea_list
**attrs
, struct linpool
*pool
);
979 bgp_as_path_loopy(struct bgp_proto
*p
, ea_list
*attrs
, u32 asn
)
981 eattr
*e
= bgp_find_attr(attrs
, BA_AS_PATH
);
982 int num
= p
->cf
->allow_local_as
+ 1;
983 return (e
&& (num
> 0) && as_path_contains(e
->u
.ptr
, asn
, num
));
987 bgp_originator_id_loopy(struct bgp_proto
*p
, ea_list
*attrs
)
989 eattr
*e
= bgp_find_attr(attrs
, BA_ORIGINATOR_ID
);
990 return (e
&& (e
->u
.data
== p
->local_id
));
994 bgp_cluster_list_loopy(struct bgp_proto
*p
, ea_list
*attrs
)
996 eattr
*e
= bgp_find_attr(attrs
, BA_CLUSTER_LIST
);
997 return (e
&& int_set_contains(e
->u
.ptr
, p
->rr_cluster_id
));
1001 bgp_decode_attr(struct bgp_parse_state
*s
, uint code
, uint flags
, byte
*data
, uint len
, ea_list
**to
)
1003 /* Handle duplicate attributes; RFC 7606 3 (g) */
1004 if (BIT32_TEST(s
->attrs_seen
, code
))
1006 if ((code
== BA_MP_REACH_NLRI
) || (code
== BA_MP_UNREACH_NLRI
))
1007 bgp_parse_error(s
, 1);
1009 DISCARD("Discarding duplicate attribute (code %u)", code
);
1011 BIT32_SET(s
->attrs_seen
, code
);
1013 if (bgp_attr_known(code
))
1015 const struct bgp_attr_desc
*desc
= &bgp_attr_table
[code
];
1017 /* Handle conflicting flags; RFC 7606 3 (c) */
1018 if ((flags
^ desc
->flags
) & (BAF_OPTIONAL
| BAF_TRANSITIVE
))
1019 WITHDRAW("Malformed %s attribute - conflicting flags (%02x)", desc
->name
, flags
);
1021 desc
->decode(s
, code
, flags
, data
, len
, to
);
1023 else /* Unknown attribute */
1025 if (!(flags
& BAF_OPTIONAL
))
1026 WITHDRAW("Unknown attribute (code %u) - conflicting flags (%02x)", code
, flags
);
1028 bgp_decode_unknown(s
, code
, flags
, data
, len
, to
);
1033 * bgp_decode_attrs - check and decode BGP attributes
1034 * @s: BGP parse state
1035 * @data: start of attribute block
1036 * @len: length of attribute block
1038 * This function takes a BGP attribute block (a part of an Update message), checks
1039 * its consistency and converts it to a list of BIRD route attributes represented
1040 * by an (uncached) &rta.
1043 bgp_decode_attrs(struct bgp_parse_state
*s
, byte
*data
, uint len
)
1045 struct bgp_proto
*p
= s
->proto
;
1046 ea_list
*attrs
= NULL
;
1047 uint code
, flags
, alen
;
1050 /* Parse the attributes */
1055 /* Read attribute type */
1060 ADVANCE(pos
, len
, 2);
1062 /* Read attribute length */
1063 if (flags
& BAF_EXT_LEN
)
1067 alen
= get_u16(pos
);
1068 ADVANCE(pos
, len
, 2);
1075 ADVANCE(pos
, len
, 1);
1081 DBG("Attr %02x %02x %u\n", code
, flags
, alen
);
1083 bgp_decode_attr(s
, code
, flags
, pos
, alen
, &attrs
);
1084 ADVANCE(pos
, len
, alen
);
1087 if (s
->err_withdraw
)
1090 /* If there is no reachability NLRI, we are finished */
1091 if (!s
->ip_reach_len
&& !s
->mp_reach_len
)
1095 /* Handle missing mandatory attributes; RFC 7606 3 (d) */
1096 if (!BIT32_TEST(s
->attrs_seen
, BA_ORIGIN
))
1097 { REPORT(NO_MANDATORY
, "ORIGIN"); goto withdraw
; }
1099 if (!BIT32_TEST(s
->attrs_seen
, BA_AS_PATH
))
1100 { REPORT(NO_MANDATORY
, "AS_PATH"); goto withdraw
; }
1102 if (s
->ip_reach_len
&& !BIT32_TEST(s
->attrs_seen
, BA_NEXT_HOP
))
1103 { REPORT(NO_MANDATORY
, "NEXT_HOP"); goto withdraw
; }
1105 /* When receiving attributes from non-AS4-aware BGP speaker, we have to
1106 reconstruct AS_PATH and AGGREGATOR attributes; RFC 6793 4.2.3 */
1107 if (!p
->as4_session
)
1108 bgp_process_as4_attrs(&attrs
, s
->pool
);
1110 /* Reject routes with our ASN in AS_PATH attribute */
1111 if (bgp_as_path_loopy(p
, attrs
, p
->local_as
))
1114 /* Reject routes with our Confederation ID in AS_PATH attribute; RFC 5065 4.0 */
1115 if ((p
->public_as
!= p
->local_as
) && bgp_as_path_loopy(p
, attrs
, p
->public_as
))
1118 /* Reject routes with our Router ID in ORIGINATOR_ID attribute; RFC 4456 8 */
1119 if (p
->is_internal
&& bgp_originator_id_loopy(p
, attrs
))
1122 /* Reject routes with our Cluster ID in CLUSTER_LIST attribute; RFC 4456 8 */
1123 if (p
->rr_client
&& bgp_cluster_list_loopy(p
, attrs
))
1126 /* If there is no local preference, define one */
1127 if (!BIT32_TEST(s
->attrs_seen
, BA_LOCAL_PREF
))
1128 bgp_set_attr_u32(&attrs
, s
->pool
, BA_LOCAL_PREF
, 0, p
->cf
->default_local_pref
);
1134 /* RFC 7606 4 - handle attribute framing errors */
1135 REPORT("Malformed attribute list - framing error (%u/%u) at %d",
1136 alen
, len
, (int) (pos
- s
->attrs
));
1139 /* RFC 7606 5.2 - handle missing NLRI during errors */
1140 if (!s
->ip_reach_len
&& !s
->mp_reach_len
)
1141 bgp_parse_error(s
, 1);
1143 s
->err_withdraw
= 1;
1149 * Route bucket hash table
1152 #define RBH_KEY(b) b->eattrs, b->hash
1153 #define RBH_NEXT(b) b->next
1154 #define RBH_EQ(a1,h1,a2,h2) h1 == h2 && ea_same(a1, a2)
1155 #define RBH_FN(a,h) h
1157 #define RBH_REHASH bgp_rbh_rehash
1158 #define RBH_PARAMS /8, *2, 2, 2, 8, 20
1161 HASH_DEFINE_REHASH_FN(RBH
, struct bgp_bucket
)
1164 bgp_init_bucket_table(struct bgp_channel
*c
)
1166 HASH_INIT(c
->bucket_hash
, c
->pool
, 8);
1168 init_list(&c
->bucket_queue
);
1169 c
->withdraw_bucket
= NULL
;
1173 bgp_free_bucket_table(struct bgp_channel
*c
)
1175 HASH_FREE(c
->bucket_hash
);
1177 struct bgp_bucket
*b
;
1178 WALK_LIST_FIRST(b
, c
->bucket_queue
)
1180 rem_node(&b
->send_node
);
1184 mb_free(c
->withdraw_bucket
);
1185 c
->withdraw_bucket
= NULL
;
1188 static struct bgp_bucket
*
1189 bgp_get_bucket(struct bgp_channel
*c
, ea_list
*new)
1191 /* Hash and lookup */
1192 u32 hash
= ea_hash(new);
1193 struct bgp_bucket
*b
= HASH_FIND(c
->bucket_hash
, RBH
, new, hash
);
1198 uint ea_size
= sizeof(ea_list
) + new->count
* sizeof(eattr
);
1199 uint ea_size_aligned
= BIRD_ALIGN(ea_size
, CPU_STRUCT_ALIGN
);
1200 uint size
= sizeof(struct bgp_bucket
) + ea_size_aligned
;
1204 /* Gather total size of non-inline attributes */
1205 for (i
= 0; i
< new->count
; i
++)
1207 eattr
*a
= &new->attrs
[i
];
1209 if (!(a
->type
& EAF_EMBEDDED
))
1210 size
+= BIRD_ALIGN(sizeof(struct adata
) + a
->u
.ptr
->length
, CPU_STRUCT_ALIGN
);
1213 /* Create the bucket */
1214 b
= mb_alloc(c
->pool
, size
);
1215 init_list(&b
->prefixes
);
1218 /* Copy list of extended attributes */
1219 memcpy(b
->eattrs
, new, ea_size
);
1220 dest
= ((byte
*) b
->eattrs
) + ea_size_aligned
;
1222 /* Copy values of non-inline attributes */
1223 for (i
= 0; i
< new->count
; i
++)
1225 eattr
*a
= &b
->eattrs
->attrs
[i
];
1227 if (!(a
->type
& EAF_EMBEDDED
))
1229 struct adata
*oa
= a
->u
.ptr
;
1230 struct adata
*na
= (struct adata
*) dest
;
1231 memcpy(na
, oa
, sizeof(struct adata
) + oa
->length
);
1233 dest
+= BIRD_ALIGN(sizeof(struct adata
) + na
->length
, CPU_STRUCT_ALIGN
);
1237 /* Insert the bucket to send queue and bucket hash */
1238 add_tail(&c
->bucket_queue
, &b
->send_node
);
1239 HASH_INSERT2(c
->bucket_hash
, RBH
, c
->pool
, b
);
1244 static struct bgp_bucket
*
1245 bgp_get_withdraw_bucket(struct bgp_channel
*c
)
1247 if (!c
->withdraw_bucket
)
1249 c
->withdraw_bucket
= mb_allocz(c
->pool
, sizeof(struct bgp_bucket
));
1250 init_list(&c
->withdraw_bucket
->prefixes
);
1253 return c
->withdraw_bucket
;
1257 bgp_free_bucket(struct bgp_channel
*c
, struct bgp_bucket
*b
)
1259 rem_node(&b
->send_node
);
1260 HASH_REMOVE2(c
->bucket_hash
, RBH
, c
->pool
, b
);
1265 bgp_defer_bucket(struct bgp_channel
*c
, struct bgp_bucket
*b
)
1267 rem_node(&b
->send_node
);
1268 add_tail(&c
->bucket_queue
, &b
->send_node
);
1272 bgp_withdraw_bucket(struct bgp_channel
*c
, struct bgp_bucket
*b
)
1274 struct bgp_proto
*p
= (void *) c
->c
.proto
;
1275 struct bgp_bucket
*wb
= bgp_get_withdraw_bucket(c
);
1277 log(L_ERR
"%s: Attribute list too long", p
->p
.name
);
1278 while (!EMPTY_LIST(b
->prefixes
))
1280 struct bgp_prefix
*px
= HEAD(b
->prefixes
);
1282 log(L_ERR
"%s: - withdrawing %N", p
->p
.name
, &px
->net
);
1283 rem_node(&px
->buck_node
);
1284 add_tail(&wb
->prefixes
, &px
->buck_node
);
1293 #define PXH_KEY(px) px->net, px->path_id, px->hash
1294 #define PXH_NEXT(px) px->next
1295 #define PXH_EQ(n1,i1,h1,n2,i2,h2) h1 == h2 && i1 == i2 && net_equal(n1, n2)
1296 #define PXH_FN(n,i,h) h
1298 #define PXH_REHASH bgp_pxh_rehash
1299 #define PXH_PARAMS /8, *2, 2, 2, 8, 20
1302 HASH_DEFINE_REHASH_FN(PXH
, struct bgp_prefix
)
1305 bgp_init_prefix_table(struct bgp_channel
*c
)
1307 HASH_INIT(c
->prefix_hash
, c
->pool
, 8);
1309 uint alen
= net_addr_length
[c
->c
.net_type
];
1310 c
->prefix_slab
= alen
? sl_new(c
->pool
, sizeof(struct bgp_prefix
) + alen
) : NULL
;
1314 bgp_free_prefix_table(struct bgp_channel
*c
)
1316 HASH_FREE(c
->prefix_hash
);
1318 rfree(c
->prefix_slab
);
1319 c
->prefix_slab
= NULL
;
1322 static struct bgp_prefix
*
1323 bgp_get_prefix(struct bgp_channel
*c
, net_addr
*net
, u32 path_id
)
1325 u32 hash
= net_hash(net
) ^ u32_hash(path_id
);
1326 struct bgp_prefix
*px
= HASH_FIND(c
->prefix_hash
, PXH
, net
, path_id
, hash
);
1330 rem_node(&px
->buck_node
);
1335 px
= sl_alloc(c
->prefix_slab
);
1337 px
= mb_alloc(c
->pool
, sizeof(struct bgp_prefix
) + net
->length
);
1339 px
->buck_node
.next
= NULL
;
1340 px
->buck_node
.prev
= NULL
;
1342 px
->path_id
= path_id
;
1343 net_copy(px
->net
, net
);
1345 HASH_INSERT2(c
->prefix_hash
, PXH
, c
->pool
, px
);
1351 bgp_free_prefix(struct bgp_channel
*c
, struct bgp_prefix
*px
)
1353 rem_node(&px
->buck_node
);
1354 HASH_REMOVE2(c
->prefix_hash
, PXH
, c
->pool
, px
);
1357 sl_free(c
->prefix_slab
, px
);
1368 bgp_import_control(struct proto
*P
, rte
**new, struct linpool
*pool UNUSED
)
1371 struct proto
*SRC
= e
->attrs
->src
->proto
;
1372 struct bgp_proto
*p
= (struct bgp_proto
*) P
;
1373 struct bgp_proto
*src
= (SRC
->proto
== &proto_bgp
) ? (struct bgp_proto
*) SRC
: NULL
;
1375 /* Reject our routes */
1379 /* Accept non-BGP routes */
1383 // XXXX: Check next hop AF
1385 /* IBGP route reflection, RFC 4456 */
1386 if (p
->is_internal
&& src
->is_internal
&& (p
->local_as
== src
->local_as
))
1388 /* Rejected unless configured as route reflector */
1389 if (!p
->rr_client
&& !src
->rr_client
)
1392 /* Generally, this should be handled when path is received, but we check it
1393 also here as rr_cluster_id may be undefined or different in src. */
1394 if (p
->rr_cluster_id
&& bgp_cluster_list_loopy(p
, e
->attrs
->eattrs
))
1398 /* Handle well-known communities, RFC 1997 */
1400 if (p
->cf
->interpret_communities
&&
1401 (c
= ea_find(e
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_COMMUNITY
))))
1403 struct adata
*d
= c
->u
.ptr
;
1405 /* Do not export anywhere */
1406 if (int_set_contains(d
, BGP_COMM_NO_ADVERTISE
))
1409 /* Do not export outside of AS (or member-AS) */
1410 if (!p
->is_internal
&& int_set_contains(d
, BGP_COMM_NO_EXPORT_SUBCONFED
))
1413 /* Do not export outside of AS (or confederation) */
1414 if (!p
->is_interior
&& int_set_contains(d
, BGP_COMM_NO_EXPORT
))
1417 /* Do not export LLGR_STALE routes to LLGR-ignorant peers */
1418 if (!p
->conn
->remote_caps
->llgr_aware
&& int_set_contains(d
, BGP_COMM_LLGR_STALE
))
1426 static adata null_adata
; /* adata of length 0 */
1429 bgp_update_attrs(struct bgp_proto
*p
, struct bgp_channel
*c
, rte
*e
, ea_list
*attrs0
, struct linpool
*pool
)
1431 struct proto
*SRC
= e
->attrs
->src
->proto
;
1432 struct bgp_proto
*src
= (SRC
->proto
== &proto_bgp
) ? (void *) SRC
: NULL
;
1433 struct bgp_export_state s
= { .proto
= p
, .channel
= c
, .pool
= pool
, .src
= src
, .route
= e
, .mpls
= c
->desc
->mpls
};
1434 ea_list
*attrs
= attrs0
;
1438 /* ORIGIN attribute - mandatory, attach if missing */
1439 if (! bgp_find_attr(attrs0
, BA_ORIGIN
))
1440 bgp_set_attr_u32(&attrs
, pool
, BA_ORIGIN
, 0, src
? ORIGIN_INCOMPLETE
: ORIGIN_IGP
);
1442 /* AS_PATH attribute - mandatory */
1443 a
= bgp_find_attr(attrs0
, BA_AS_PATH
);
1444 ad
= a
? a
->u
.ptr
: &null_adata
;
1446 /* AS_PATH attribute - strip AS_CONFED* segments outside confederation */
1447 if ((!p
->cf
->confederation
|| !p
->is_interior
) && as_path_contains_confed(ad
))
1448 ad
= as_path_strip_confed(pool
, ad
);
1450 /* AS_PATH attribute - keep or prepend ASN */
1451 if (p
->is_internal
||
1452 (p
->rs_client
&& src
&& src
->rs_client
))
1454 /* IBGP or route server -> just ensure there is one */
1456 bgp_set_attr_ptr(&attrs
, pool
, BA_AS_PATH
, 0, &null_adata
);
1458 else if (p
->is_interior
)
1460 /* Confederation -> prepend ASN as AS_CONFED_SEQUENCE */
1461 ad
= as_path_prepend2(pool
, ad
, AS_PATH_CONFED_SEQUENCE
, p
->public_as
);
1462 bgp_set_attr_ptr(&attrs
, pool
, BA_AS_PATH
, 0, ad
);
1464 else /* Regular EBGP (no RS, no confederation) */
1466 /* Regular EBGP -> prepend ASN as regular sequence */
1467 ad
= as_path_prepend2(pool
, ad
, AS_PATH_SEQUENCE
, p
->public_as
);
1468 bgp_set_attr_ptr(&attrs
, pool
, BA_AS_PATH
, 0, ad
);
1470 /* MULTI_EXIT_DESC attribute - accept only if set in export filter */
1471 a
= bgp_find_attr(attrs0
, BA_MULTI_EXIT_DISC
);
1472 if (a
&& !(a
->type
& EAF_FRESH
))
1473 bgp_unset_attr(&attrs
, pool
, BA_MULTI_EXIT_DISC
);
1476 /* NEXT_HOP attribute - delegated to AF-specific hook */
1477 a
= bgp_find_attr(attrs0
, BA_NEXT_HOP
);
1478 bgp_update_next_hop(&s
, a
, &attrs
);
1480 /* LOCAL_PREF attribute - required for IBGP, attach if missing */
1481 if (p
->is_interior
&& ! bgp_find_attr(attrs0
, BA_LOCAL_PREF
))
1482 bgp_set_attr_u32(&attrs
, pool
, BA_LOCAL_PREF
, 0, p
->cf
->default_local_pref
);
1484 /* IBGP route reflection, RFC 4456 */
1485 if (src
&& src
->is_internal
&& p
->is_internal
&& (src
->local_as
== p
->local_as
))
1487 /* ORIGINATOR_ID attribute - attach if not already set */
1488 if (! bgp_find_attr(attrs0
, BA_ORIGINATOR_ID
))
1489 bgp_set_attr_u32(&attrs
, pool
, BA_ORIGINATOR_ID
, 0, src
->remote_id
);
1491 /* CLUSTER_LIST attribute - prepend cluster ID */
1492 a
= bgp_find_attr(attrs0
, BA_CLUSTER_LIST
);
1493 ad
= a
? a
->u
.ptr
: NULL
;
1495 /* Prepend src cluster ID */
1496 if (src
->rr_cluster_id
)
1497 ad
= int_set_prepend(pool
, ad
, src
->rr_cluster_id
);
1499 /* Prepend dst cluster ID if src and dst clusters are different */
1500 if (p
->rr_cluster_id
&& (src
->rr_cluster_id
!= p
->rr_cluster_id
))
1501 ad
= int_set_prepend(pool
, ad
, p
->rr_cluster_id
);
1503 /* Should be at least one prepended cluster ID */
1504 bgp_set_attr_ptr(&attrs
, pool
, BA_CLUSTER_LIST
, 0, ad
);
1507 /* AS4_* transition attributes, RFC 6793 4.2.2 */
1508 if (! p
->as4_session
)
1510 a
= bgp_find_attr(attrs
, BA_AS_PATH
);
1511 if (a
&& as_path_contains_as4(a
->u
.ptr
))
1513 bgp_set_attr_ptr(&attrs
, pool
, BA_AS_PATH
, 0, as_path_to_old(pool
, a
->u
.ptr
));
1514 bgp_set_attr_ptr(&attrs
, pool
, BA_AS4_PATH
, 0, as_path_strip_confed(pool
, a
->u
.ptr
));
1517 a
= bgp_find_attr(attrs
, BA_AGGREGATOR
);
1518 if (a
&& aggregator_contains_as4(a
->u
.ptr
))
1520 bgp_set_attr_ptr(&attrs
, pool
, BA_AGGREGATOR
, 0, aggregator_to_old(pool
, a
->u
.ptr
));
1521 bgp_set_attr_ptr(&attrs
, pool
, BA_AS4_AGGREGATOR
, 0, a
->u
.ptr
);
1526 * Presence of mandatory attributes ORIGIN and AS_PATH is ensured by above
1527 * conditions. Presence and validity of quasi-mandatory NEXT_HOP attribute
1528 * should be checked in AF-specific hooks.
1531 /* Apply per-attribute export hooks for validatation and normalization */
1532 return bgp_export_attrs(&s
, attrs
);
1536 bgp_rt_notify(struct proto
*P
, struct channel
*C
, net
*n
, rte
*new, rte
*old
)
1538 struct bgp_proto
*p
= (void *) P
;
1539 struct bgp_channel
*c
= (void *) C
;
1540 struct bgp_bucket
*buck
;
1541 struct bgp_prefix
*px
;
1546 struct ea_list
*attrs
= bgp_update_attrs(p
, c
, new, new->attrs
->eattrs
, bgp_linpool2
);
1548 /* If attributes are invalid, we fail back to withdraw */
1549 buck
= attrs
? bgp_get_bucket(c
, attrs
) : bgp_get_withdraw_bucket(c
);
1550 path
= new->attrs
->src
->global_id
;
1552 lp_flush(bgp_linpool2
);
1556 buck
= bgp_get_withdraw_bucket(c
);
1557 path
= old
->attrs
->src
->global_id
;
1560 px
= bgp_get_prefix(c
, n
->n
.addr
, c
->add_path_tx
? path
: 0);
1561 add_tail(&buck
->prefixes
, &px
->buck_node
);
1563 bgp_schedule_packet(p
->conn
, c
, PKT_UPDATE
);
1568 bgp_get_neighbor(rte
*r
)
1570 eattr
*e
= ea_find(r
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_AS_PATH
));
1573 if (e
&& as_path_get_first_regular(e
->u
.ptr
, &as
))
1576 /* If AS_PATH is not defined, we treat rte as locally originated */
1577 struct bgp_proto
*p
= (void *) r
->attrs
->src
->proto
;
1578 return p
->cf
->confederation
?: p
->local_as
;
1582 rte_resolvable(rte
*rt
)
1584 return rt
->attrs
->dest
== RTD_UNICAST
;
1590 if (r
->u
.bgp
.stale
< 0)
1592 /* If staleness is unknown, compute and cache it */
1593 eattr
*a
= ea_find(r
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_COMMUNITY
));
1594 r
->u
.bgp
.stale
= a
&& int_set_contains(a
->u
.ptr
, BGP_COMM_LLGR_STALE
);
1597 return r
->u
.bgp
.stale
;
1601 bgp_rte_better(rte
*new, rte
*old
)
1603 struct bgp_proto
*new_bgp
= (struct bgp_proto
*) new->attrs
->src
->proto
;
1604 struct bgp_proto
*old_bgp
= (struct bgp_proto
*) old
->attrs
->src
->proto
;
1608 /* Skip suppressed routes (see bgp_rte_recalculate()) */
1609 n
= new->u
.bgp
.suppressed
;
1610 o
= old
->u
.bgp
.suppressed
;
1616 /* RFC 4271 9.1.2.1. Route resolvability test */
1617 n
= rte_resolvable(new);
1618 o
= rte_resolvable(old
);
1624 /* LLGR draft - depreference stale routes */
1632 /* Start with local preferences */
1633 x
= ea_find(new->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_LOCAL_PREF
));
1634 y
= ea_find(old
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_LOCAL_PREF
));
1635 n
= x
? x
->u
.data
: new_bgp
->cf
->default_local_pref
;
1636 o
= y
? y
->u
.data
: old_bgp
->cf
->default_local_pref
;
1642 /* RFC 4271 9.1.2.2. a) Use AS path lengths */
1643 if (new_bgp
->cf
->compare_path_lengths
|| old_bgp
->cf
->compare_path_lengths
)
1645 x
= ea_find(new->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_AS_PATH
));
1646 y
= ea_find(old
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_AS_PATH
));
1647 n
= x
? as_path_getlen(x
->u
.ptr
) : AS_PATH_MAXLEN
;
1648 o
= y
? as_path_getlen(y
->u
.ptr
) : AS_PATH_MAXLEN
;
1655 /* RFC 4271 9.1.2.2. b) Use origins */
1656 x
= ea_find(new->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_ORIGIN
));
1657 y
= ea_find(old
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_ORIGIN
));
1658 n
= x
? x
->u
.data
: ORIGIN_INCOMPLETE
;
1659 o
= y
? y
->u
.data
: ORIGIN_INCOMPLETE
;
1665 /* RFC 4271 9.1.2.2. c) Compare MED's */
1666 /* Proper RFC 4271 path selection cannot be interpreted as finding
1667 * the best path in some ordering. It is implemented partially in
1668 * bgp_rte_recalculate() when deterministic_med option is
1669 * active. Without that option, the behavior is just an
1670 * approximation, which in specific situations may lead to
1671 * persistent routing loops, because it is nondeterministic - it
1672 * depends on the order in which routes appeared. But it is also the
1673 * same behavior as used by default in Cisco routers, so it is
1674 * probably not a big issue.
1676 if (new_bgp
->cf
->med_metric
|| old_bgp
->cf
->med_metric
||
1677 (bgp_get_neighbor(new) == bgp_get_neighbor(old
)))
1679 x
= ea_find(new->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_MULTI_EXIT_DISC
));
1680 y
= ea_find(old
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_MULTI_EXIT_DISC
));
1681 n
= x
? x
->u
.data
: new_bgp
->cf
->default_med
;
1682 o
= y
? y
->u
.data
: old_bgp
->cf
->default_med
;
1689 /* RFC 4271 9.1.2.2. d) Prefer external peers */
1690 if (new_bgp
->is_interior
> old_bgp
->is_interior
)
1692 if (new_bgp
->is_interior
< old_bgp
->is_interior
)
1695 /* RFC 4271 9.1.2.2. e) Compare IGP metrics */
1696 n
= new_bgp
->cf
->igp_metric
? new->attrs
->igp_metric
: 0;
1697 o
= old_bgp
->cf
->igp_metric
? old
->attrs
->igp_metric
: 0;
1703 /* RFC 4271 9.1.2.2. f) Compare BGP identifiers */
1704 /* RFC 4456 9. a) Use ORIGINATOR_ID instead of local neighbor ID */
1705 x
= ea_find(new->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_ORIGINATOR_ID
));
1706 y
= ea_find(old
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_ORIGINATOR_ID
));
1707 n
= x
? x
->u
.data
: new_bgp
->remote_id
;
1708 o
= y
? y
->u
.data
: old_bgp
->remote_id
;
1710 /* RFC 5004 - prefer older routes */
1711 /* (if both are external and from different peer) */
1712 if ((new_bgp
->cf
->prefer_older
|| old_bgp
->cf
->prefer_older
) &&
1713 !new_bgp
->is_internal
&& n
!= o
)
1716 /* rest of RFC 4271 9.1.2.2. f) */
1722 /* RFC 4456 9. b) Compare cluster list lengths */
1723 x
= ea_find(new->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_CLUSTER_LIST
));
1724 y
= ea_find(old
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_CLUSTER_LIST
));
1725 n
= x
? int_set_get_size(x
->u
.ptr
) : 0;
1726 o
= y
? int_set_get_size(y
->u
.ptr
) : 0;
1732 /* RFC 4271 9.1.2.2. g) Compare peer IP adresses */
1733 return (ipa_compare(new_bgp
->cf
->remote_ip
, old_bgp
->cf
->remote_ip
) < 0);
1738 bgp_rte_mergable(rte
*pri
, rte
*sec
)
1740 struct bgp_proto
*pri_bgp
= (struct bgp_proto
*) pri
->attrs
->src
->proto
;
1741 struct bgp_proto
*sec_bgp
= (struct bgp_proto
*) sec
->attrs
->src
->proto
;
1745 /* Skip suppressed routes (see bgp_rte_recalculate()) */
1746 if (pri
->u
.bgp
.suppressed
!= sec
->u
.bgp
.suppressed
)
1749 /* RFC 4271 9.1.2.1. Route resolvability test */
1750 if (!rte_resolvable(sec
))
1753 /* LLGR draft - depreference stale routes */
1754 if (rte_stale(pri
) != rte_stale(sec
))
1757 /* Start with local preferences */
1758 x
= ea_find(pri
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_LOCAL_PREF
));
1759 y
= ea_find(sec
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_LOCAL_PREF
));
1760 p
= x
? x
->u
.data
: pri_bgp
->cf
->default_local_pref
;
1761 s
= y
? y
->u
.data
: sec_bgp
->cf
->default_local_pref
;
1765 /* RFC 4271 9.1.2.2. a) Use AS path lengths */
1766 if (pri_bgp
->cf
->compare_path_lengths
|| sec_bgp
->cf
->compare_path_lengths
)
1768 x
= ea_find(pri
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_AS_PATH
));
1769 y
= ea_find(sec
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_AS_PATH
));
1770 p
= x
? as_path_getlen(x
->u
.ptr
) : AS_PATH_MAXLEN
;
1771 s
= y
? as_path_getlen(y
->u
.ptr
) : AS_PATH_MAXLEN
;
1776 // if (DELTA(p, s) > pri_bgp->cf->relax_multipath)
1780 /* RFC 4271 9.1.2.2. b) Use origins */
1781 x
= ea_find(pri
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_ORIGIN
));
1782 y
= ea_find(sec
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_ORIGIN
));
1783 p
= x
? x
->u
.data
: ORIGIN_INCOMPLETE
;
1784 s
= y
? y
->u
.data
: ORIGIN_INCOMPLETE
;
1788 /* RFC 4271 9.1.2.2. c) Compare MED's */
1789 if (pri_bgp
->cf
->med_metric
|| sec_bgp
->cf
->med_metric
||
1790 (bgp_get_neighbor(pri
) == bgp_get_neighbor(sec
)))
1792 x
= ea_find(pri
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_MULTI_EXIT_DISC
));
1793 y
= ea_find(sec
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_MULTI_EXIT_DISC
));
1794 p
= x
? x
->u
.data
: pri_bgp
->cf
->default_med
;
1795 s
= y
? y
->u
.data
: sec_bgp
->cf
->default_med
;
1800 /* RFC 4271 9.1.2.2. d) Prefer external peers */
1801 if (pri_bgp
->is_interior
!= sec_bgp
->is_interior
)
1804 /* RFC 4271 9.1.2.2. e) Compare IGP metrics */
1805 p
= pri_bgp
->cf
->igp_metric
? pri
->attrs
->igp_metric
: 0;
1806 s
= sec_bgp
->cf
->igp_metric
? sec
->attrs
->igp_metric
: 0;
1810 /* Remaining criteria are ignored */
1817 same_group(rte
*r
, u32 lpref
, u32 lasn
)
1819 return (r
->pref
== lpref
) && (bgp_get_neighbor(r
) == lasn
);
1823 use_deterministic_med(rte
*r
)
1825 struct proto
*P
= r
->attrs
->src
->proto
;
1826 return (P
->proto
== &proto_bgp
) && ((struct bgp_proto
*) P
)->cf
->deterministic_med
;
1830 bgp_rte_recalculate(rtable
*table
, net
*net
, rte
*new, rte
*old
, rte
*old_best
)
1833 rte
*key
= new ? new : old
;
1834 u32 lpref
= key
->pref
;
1835 u32 lasn
= bgp_get_neighbor(key
);
1836 int old_is_group_best
= 0;
1839 * Proper RFC 4271 path selection is a bit complicated, it cannot be
1840 * implemented just by rte_better(), because it is not a linear
1841 * ordering. But it can be splitted to two levels, where the lower
1842 * level chooses the best routes in each group of routes from the
1843 * same neighboring AS and higher level chooses the best route (with
1844 * a slightly different ordering) between the best-in-group routes.
1846 * When deterministic_med is disabled, we just ignore this issue and
1847 * choose the best route by bgp_rte_better() alone. If enabled, the
1848 * lower level of the route selection is done here (for the group
1849 * to which the changed route belongs), all routes in group are
1850 * marked as suppressed, just chosen best-in-group is not.
1852 * Global best route selection then implements higher level by
1853 * choosing between non-suppressed routes (as they are always
1854 * preferred over suppressed routes). Routes from BGP protocols
1855 * that do not set deterministic_med are just never suppressed. As
1856 * they do not participate in the lower level selection, it is OK
1857 * that this fn is not called for them.
1859 * The idea is simple, the implementation is more problematic,
1860 * mostly because of optimizations in rte_recalculate() that
1861 * avoids full recalculation in most cases.
1863 * We can assume that at least one of new, old is non-NULL and both
1864 * are from the same protocol with enabled deterministic_med. We
1865 * group routes by both neighbor AS (lasn) and preference (lpref),
1866 * because bgp_rte_better() does not handle preference itself.
1869 /* If new and old are from different groups, we just process that
1870 as two independent events */
1871 if (new && old
&& !same_group(old
, lpref
, lasn
))
1874 i1
= bgp_rte_recalculate(table
, net
, NULL
, old
, old_best
);
1875 i2
= bgp_rte_recalculate(table
, net
, new, NULL
, old_best
);
1880 * We could find the best-in-group and then make some shortcuts like
1881 * in rte_recalculate, but as we would have to walk through all
1882 * net->routes just to find it, it is probably not worth. So we
1883 * just have two simpler fast cases that use just the old route.
1884 * We also set suppressed flag to avoid using it in bgp_rte_better().
1888 new->u
.bgp
.suppressed
= 1;
1892 old_is_group_best
= !old
->u
.bgp
.suppressed
;
1893 old
->u
.bgp
.suppressed
= 1;
1894 int new_is_better
= new && bgp_rte_better(new, old
);
1896 /* The first case - replace not best with worse (or remove not best) */
1897 if (!old_is_group_best
&& !new_is_better
)
1900 /* The second case - replace the best with better */
1901 if (old_is_group_best
&& new_is_better
)
1903 /* new is best-in-group, the see discussion below - this is
1904 a special variant of NBG && OBG. From OBG we can deduce
1905 that same_group(old_best) iff (old == old_best) */
1906 new->u
.bgp
.suppressed
= 0;
1907 return (old
== old_best
);
1911 /* The default case - find a new best-in-group route */
1912 r
= new; /* new may not be in the list */
1913 for (s
=net
->routes
; rte_is_valid(s
); s
=s
->next
)
1914 if (use_deterministic_med(s
) && same_group(s
, lpref
, lasn
))
1916 s
->u
.bgp
.suppressed
= 1;
1917 if (!r
|| bgp_rte_better(s
, r
))
1921 /* Simple case - the last route in group disappears */
1925 /* Found best-in-group */
1926 r
->u
.bgp
.suppressed
= 0;
1929 * There are generally two reasons why we have to force
1930 * recalculation (return 1): First, the new route may be wrongfully
1931 * chosen to be the best in the first case check in
1932 * rte_recalculate(), this may happen only if old_best is from the
1933 * same group. Second, another (different than new route)
1934 * best-in-group is chosen and that may be the proper best (although
1935 * rte_recalculate() without ignore that possibility).
1937 * There are three possible cases according to whether the old route
1938 * was the best in group (OBG, stored in old_is_group_best) and
1939 * whether the new route is the best in group (NBG, tested by r == new).
1940 * These cases work even if old or new is NULL.
1942 * NBG -> new is a possible candidate for the best route, so we just
1943 * check for the first reason using same_group().
1945 * !NBG && OBG -> Second reason applies, return 1
1947 * !NBG && !OBG -> Best in group does not change, old != old_best,
1948 * rte_better(new, old_best) is false and therefore
1949 * the first reason does not apply, return 0
1953 return old_best
&& same_group(old_best
, lpref
, lasn
);
1955 return old_is_group_best
;
1959 bgp_rte_modify_stale(struct rte
*r
, struct linpool
*pool
)
1961 eattr
*a
= ea_find(r
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_COMMUNITY
));
1962 struct adata
*ad
= a
? a
->u
.ptr
: NULL
;
1963 uint flags
= a
? a
->flags
: BAF_PARTIAL
;
1965 if (ad
&& int_set_contains(ad
, BGP_COMM_NO_LLGR
))
1968 if (ad
&& int_set_contains(ad
, BGP_COMM_LLGR_STALE
))
1971 r
= rte_cow_rta(r
, pool
);
1972 bgp_set_attr_ptr(&(r
->attrs
->eattrs
), pool
, BA_COMMUNITY
, flags
,
1973 int_set_add(pool
, ad
, BGP_COMM_LLGR_STALE
));
1981 * Reconstruct AS_PATH and AGGREGATOR according to RFC 6793 4.2.3
1984 bgp_process_as4_attrs(ea_list
**attrs
, struct linpool
*pool
)
1986 eattr
*p2
= bgp_find_attr(*attrs
, BA_AS_PATH
);
1987 eattr
*p4
= bgp_find_attr(*attrs
, BA_AS4_PATH
);
1988 eattr
*a2
= bgp_find_attr(*attrs
, BA_AGGREGATOR
);
1989 eattr
*a4
= bgp_find_attr(*attrs
, BA_AS4_AGGREGATOR
);
1991 /* First, unset AS4_* attributes */
1992 if (p4
) bgp_unset_attr(attrs
, pool
, BA_AS4_PATH
);
1993 if (a4
) bgp_unset_attr(attrs
, pool
, BA_AS4_AGGREGATOR
);
1995 /* Handle AGGREGATOR attribute */
1998 u32 a2_asn
= get_u32(a2
->u
.ptr
->data
);
2000 /* If routes were aggregated by an old router, then AS4_PATH and
2001 AS4_AGGREGATOR are invalid. In that case we give up. */
2002 if (a2_asn
!= AS_TRANS
)
2005 /* Use AS4_AGGREGATOR instead of AGGREGATOR */
2006 a2
->u
.ptr
= a4
->u
.ptr
;
2009 /* Handle AS_PATH attribute */
2012 /* Both as_path_getlen() and as_path_cut() take AS_CONFED* as zero length */
2013 int p2_len
= as_path_getlen(p2
->u
.ptr
);
2014 int p4_len
= as_path_getlen(p4
->u
.ptr
);
2016 /* AS_PATH is too short, give up */
2017 if (p2_len
< p4_len
)
2020 /* Merge AS_PATH and AS4_PATH */
2021 as_path_cut(p2
->u
.ptr
, p2_len
- p4_len
);
2022 p2
->u
.ptr
= as_path_merge(pool
, p2
->u
.ptr
, p4
->u
.ptr
);
2027 bgp_get_attr(eattr
*a
, byte
*buf
, int buflen
)
2029 uint i
= EA_ID(a
->id
);
2030 const struct bgp_attr_desc
*d
;
2033 if (bgp_attr_known(i
))
2035 d
= &bgp_attr_table
[i
];
2036 len
= bsprintf(buf
, "%s", d
->name
);
2042 d
->format(a
, buf
, buflen
- len
- 2);
2048 bsprintf(buf
, "%02x%s", i
, (a
->flags
& BAF_TRANSITIVE
) ? " [t]" : "");
2053 bgp_get_route_info(rte
*e
, byte
*buf
)
2055 eattr
*p
= ea_find(e
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_AS_PATH
));
2056 eattr
*o
= ea_find(e
->attrs
->eattrs
, EA_CODE(PROTOCOL_BGP
, BA_ORIGIN
));
2059 buf
+= bsprintf(buf
, " (%d", e
->pref
);
2061 if (e
->u
.bgp
.suppressed
)
2062 buf
+= bsprintf(buf
, "-");
2065 buf
+= bsprintf(buf
, "s");
2067 if (e
->attrs
->hostentry
)
2069 if (!rte_resolvable(e
))
2070 buf
+= bsprintf(buf
, "/-");
2071 else if (e
->attrs
->igp_metric
>= IGP_METRIC_UNKNOWN
)
2072 buf
+= bsprintf(buf
, "/?");
2074 buf
+= bsprintf(buf
, "/%d", e
->attrs
->igp_metric
);
2076 buf
+= bsprintf(buf
, ") [");
2078 if (p
&& as_path_get_last(p
->u
.ptr
, &origas
))
2079 buf
+= bsprintf(buf
, "AS%u", origas
);
2081 buf
+= bsprintf(buf
, "%c", "ie?"[o
->u
.data
]);