]> git.ipfire.org Git - thirdparty/bird.git/blob - proto/bgp/attrs.c
Merge branch 'int-new' into nexthop-merged
[thirdparty/bird.git] / proto / bgp / attrs.c
1 /*
2 * BIRD -- BGP Attributes
3 *
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.
7 *
8 * Can be freely distributed and used under the terms of the GNU GPL.
9 */
10
11 #undef LOCAL_DEBUG
12
13 #include <stdlib.h>
14
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"
24
25 #include "bgp.h"
26
27 /*
28 * UPDATE message error handling
29 *
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.
35 *
36 * Loop detection based on AS_PATH causes updates to be withdrawn. RFC
37 * 4271 does not explicitly specifiy the behavior in that case.
38 *
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
42 * a bad idea.
43 *
44 * BGP attribute table has several hooks:
45 *
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.
51 *
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).
58 *
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.
63 *
64 * format - Optional hook that converts eattr to textual representation.
65 */
66
67 // XXXX review pool usage : c->c.proto->pool
68
69
70 struct bgp_attr_desc {
71 const char *name;
72 uint type;
73 uint flags;
74 void (*export)(struct bgp_export_state *s, eattr *a);
75 int (*encode)(struct bgp_write_state *s, eattr *a, byte *buf, uint size);
76 void (*decode)(struct bgp_parse_state *s, uint code, uint flags, byte *data, uint len, ea_list **to);
77 void (*format)(eattr *ea, byte *buf, uint size);
78 };
79
80 static const struct bgp_attr_desc bgp_attr_table[];
81
82 static inline int bgp_attr_known(uint code);
83
84 eattr *
85 bgp_set_attr(ea_list **attrs, struct linpool *pool, uint code, uint flags, uintptr_t val)
86 {
87 ASSERT(bgp_attr_known(code));
88
89 ea_list *a = lp_alloc(pool, sizeof(ea_list) + sizeof(eattr));
90 eattr *e = &a->attrs[0];
91
92 a->flags = EALF_SORTED;
93 a->count = 1;
94 a->next = *attrs;
95 *attrs = a;
96
97 e->id = EA_CODE(EAP_BGP, code);
98 e->type = bgp_attr_table[code].type;
99 e->flags = flags;
100
101 if (e->type & EAF_EMBEDDED)
102 e->u.data = (u32) val;
103 else
104 e->u.ptr = (struct adata *) val;
105
106 return e;
107 }
108
109
110
111 #define REPORT(msg, args...) \
112 ({ log(L_REMOTE "%s: " msg, s->proto->p.name, ## args); })
113
114 #define DISCARD(msg, args...) \
115 ({ REPORT(msg, ## args); return; })
116
117 #define WITHDRAW(msg, args...) \
118 ({ REPORT(msg, ## args); s->err_withdraw = 1; return; })
119
120 #define UNSET(a) \
121 ({ a->type = EAF_TYPE_UNDEF; return; })
122
123 #define NEW_BGP "Discarding %s attribute received from AS4-aware neighbor"
124 #define BAD_EBGP "Discarding %s attribute received from EBGP neighbor"
125 #define BAD_LENGTH "Malformed %s attribute - invalid length (%u)"
126 #define BAD_VALUE "Malformed %s attribute - invalid value (%u)"
127 #define NO_MANDATORY "Missing mandatory %s attribute"
128
129
130 static inline int
131 bgp_put_attr_hdr3(byte *buf, uint code, uint flags, uint len)
132 {
133 *buf++ = flags;
134 *buf++ = code;
135 *buf++ = len;
136 return 3;
137 }
138
139 static inline int
140 bgp_put_attr_hdr4(byte *buf, uint code, uint flags, uint len)
141 {
142 *buf++ = flags | BAF_EXT_LEN;
143 *buf++ = code;
144 put_u16(buf, len);
145 return 4;
146 }
147
148 static inline int
149 bgp_put_attr_hdr(byte *buf, uint code, uint flags, uint len)
150 {
151 if (len < 256)
152 return bgp_put_attr_hdr3(buf, code, flags, len);
153 else
154 return bgp_put_attr_hdr4(buf, code, flags, len);
155 }
156
157 static int
158 bgp_encode_u8(struct bgp_write_state *s UNUSED, eattr *a, byte *buf, uint size)
159 {
160 if (size < (3+1))
161 return -1;
162
163 bgp_put_attr_hdr3(buf, EA_ID(a->id), a->flags, 1);
164 buf[3] = a->u.data;
165
166 return 3+1;
167 }
168
169 static int
170 bgp_encode_u32(struct bgp_write_state *s UNUSED, eattr *a, byte *buf, uint size)
171 {
172 if (size < (3+4))
173 return -1;
174
175 bgp_put_attr_hdr3(buf, EA_ID(a->id), a->flags, 4);
176 put_u32(buf+3, a->u.data);
177
178 return 3+4;
179 }
180
181 static int
182 bgp_encode_u32s(struct bgp_write_state *s UNUSED, eattr *a, byte *buf, uint size)
183 {
184 uint len = a->u.ptr->length;
185
186 if (size < (4+len))
187 return -1;
188
189 uint hdr = bgp_put_attr_hdr(buf, EA_ID(a->id), a->flags, len);
190 put_u32s(buf + hdr, (u32 *) a->u.ptr->data, len / 4);
191
192 return hdr + len;
193 }
194
195 static int
196 bgp_put_attr(byte *buf, uint size, uint code, uint flags, byte *data, uint len)
197 {
198 if (size < (4+len))
199 return -1;
200
201 uint hdr = bgp_put_attr_hdr(buf, code, flags, len);
202 memcpy(buf + hdr, data, len);
203
204 return hdr + len;
205 }
206
207 static int
208 bgp_encode_raw(struct bgp_write_state *s UNUSED, eattr *a, byte *buf, uint size)
209 {
210 return bgp_put_attr(buf, size, EA_ID(a->id), a->flags, a->u.ptr->data, a->u.ptr->length);
211 }
212
213
214 /*
215 * Attribute hooks
216 */
217
218 static void
219 bgp_export_origin(struct bgp_export_state *s, eattr *a)
220 {
221 if (a->u.data > 2)
222 WITHDRAW(BAD_VALUE, "ORIGIN", a->u.data);
223 }
224
225 static void
226 bgp_decode_origin(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
227 {
228 if (len != 1)
229 WITHDRAW(BAD_LENGTH, "ORIGIN", len);
230
231 if (data[0] > 2)
232 WITHDRAW(BAD_VALUE, "ORIGIN", data[0]);
233
234 bgp_set_attr_u32(to, s->pool, BA_ORIGIN, flags, data[0]);
235 }
236
237 static void
238 bgp_format_origin(eattr *a, byte *buf, uint size UNUSED)
239 {
240 static const char *bgp_origin_names[] = { "IGP", "EGP", "Incomplete" };
241
242 bsprintf(buf, (a->u.data <= 2) ? bgp_origin_names[a->u.data] : "?");
243 }
244
245
246 static int
247 bgp_encode_as_path(struct bgp_write_state *s, eattr *a, byte *buf, uint size)
248 {
249 byte *data = a->u.ptr->data;
250 uint len = a->u.ptr->length;
251
252 if (!s->as4_session)
253 {
254 /* Prepare 16-bit AS_PATH (from 32-bit one) in a temporary buffer */
255 byte *src = data;
256 data = alloca(len);
257 len = as_path_32to16(data, src, len);
258 }
259
260 return bgp_put_attr(buf, size, BA_AS_PATH, a->flags, data, len);
261 }
262
263 static void
264 bgp_decode_as_path(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
265 {
266 struct bgp_proto *p = s->proto;
267 int as_length = s->as4_session ? 4 : 2;
268 int as_confed = p->cf->confederation && p->is_interior;
269 char err[128];
270
271 if (!as_path_valid(data, len, as_length, as_confed, err, sizeof(err)))
272 WITHDRAW("Malformed AS_PATH attribute - %s", err);
273
274 /* In some circumstances check for initial AS_CONFED_SEQUENCE; RFC 5065 5.0 */
275 if (p->is_interior && !p->is_internal &&
276 ((len < 2) || (data[0] != AS_PATH_CONFED_SEQUENCE)))
277 WITHDRAW("Malformed AS_PATH attribute - %s", "missing initial AS_CONFED_SEQUENCE");
278
279 if (!s->as4_session)
280 {
281 /* Prepare 32-bit AS_PATH (from 16-bit one) in a temporary buffer */
282 byte *src = data;
283 data = alloca(2*len);
284 len = as_path_16to32(data, src, len);
285 }
286
287 bgp_set_attr_data(to, s->pool, BA_AS_PATH, flags, data, len);
288 }
289
290
291 static int
292 bgp_encode_next_hop(struct bgp_write_state *s, eattr *a, byte *buf, uint size)
293 {
294 /*
295 * The NEXT_HOP attribute is used only in traditional (IPv4) BGP. In MP-BGP,
296 * the next hop is encoded as a part of the MP_REACH_NLRI attribute, so we
297 * store it and encode it later by AFI-specific hooks.
298 */
299
300 if (s->channel->afi == BGP_AF_IPV4)
301 {
302 ASSERT(a->u.ptr->length == sizeof(ip_addr));
303
304 if (size < (3+4))
305 return -1;
306
307 bgp_put_attr_hdr3(buf, BA_NEXT_HOP, a->flags, 4);
308 put_ip4(buf+3, ipa_to_ip4( *(ip_addr *) a->u.ptr->data ));
309
310 return 3+4;
311 }
312 else
313 {
314 s->mp_next_hop = a;
315 return 0;
316 }
317 }
318
319 static void
320 bgp_decode_next_hop(struct bgp_parse_state *s, uint code UNUSED, uint flags UNUSED, byte *data, uint len, ea_list **to UNUSED)
321 {
322 if (len != 4)
323 WITHDRAW(BAD_LENGTH, "NEXT_HOP", len);
324
325 /* Semantic checks are done later */
326 s->ip_next_hop_len = len;
327 s->ip_next_hop_data = data;
328 }
329
330 /* TODO: This function should use AF-specific hook */
331 static void
332 bgp_format_next_hop(eattr *a, byte *buf, uint size UNUSED)
333 {
334 ip_addr *nh = (void *) a->u.ptr->data;
335 uint len = a->u.ptr->length;
336
337 ASSERT((len == 16) || (len == 32));
338
339 /* in IPv6, we may have two addresses in NEXT HOP */
340 if ((len == 16) || ipa_zero(nh[1]))
341 bsprintf(buf, "%I", nh[0]);
342 else
343 bsprintf(buf, "%I %I", nh[0], nh[1]);
344 }
345
346
347 static void
348 bgp_decode_med(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
349 {
350 if (len != 4)
351 WITHDRAW(BAD_LENGTH, "MULTI_EXIT_DISC", len);
352
353 u32 val = get_u32(data);
354 bgp_set_attr_u32(to, s->pool, BA_MULTI_EXIT_DISC, flags, val);
355 }
356
357
358 static void
359 bgp_export_local_pref(struct bgp_export_state *s, eattr *a)
360 {
361 if (!s->proto->is_interior)
362 UNSET(a);
363 }
364
365 static void
366 bgp_decode_local_pref(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
367 {
368 if (!s->proto->is_interior)
369 DISCARD(BAD_EBGP, "LOCAL_PREF");
370
371 if (len != 4)
372 WITHDRAW(BAD_LENGTH, "LOCAL_PREF", len);
373
374 u32 val = get_u32(data);
375 bgp_set_attr_u32(to, s->pool, BA_LOCAL_PREF, flags, val);
376 }
377
378
379 static void
380 bgp_decode_atomic_aggr(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data UNUSED, uint len, ea_list **to)
381 {
382 if (len != 0)
383 DISCARD(BAD_LENGTH, "ATOMIC_AGGR", len);
384
385 bgp_set_attr_data(to, s->pool, BA_ATOMIC_AGGR, flags, NULL, 0);
386 }
387
388 static int
389 bgp_encode_aggregator(struct bgp_write_state *s, eattr *a, byte *buf, uint size)
390 {
391 byte *data = a->u.ptr->data;
392 uint len = a->u.ptr->length;
393
394 if (!s->as4_session)
395 {
396 /* Prepare 16-bit AGGREGATOR (from 32-bit one) in a temporary buffer */
397 byte *src = data;
398 data = alloca(6);
399 len = aggregator_32to16(data, src);
400 }
401
402 return bgp_put_attr(buf, size, BA_AGGREGATOR, a->flags, data, len);
403 }
404
405 static void
406 bgp_decode_aggregator(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
407 {
408 if (len != (s->as4_session ? 8 : 6))
409 DISCARD(BAD_LENGTH, "AGGREGATOR", len);
410
411 if (!s->as4_session)
412 {
413 /* Prepare 32-bit AGGREGATOR (from 16-bit one) in a temporary buffer */
414 byte *src = data;
415 data = alloca(8);
416 len = aggregator_16to32(data, src);
417 }
418
419 bgp_set_attr_data(to, s->pool, BA_AGGREGATOR, flags, data, len);
420 }
421
422 static void
423 bgp_format_aggregator(eattr *a, byte *buf, uint size UNUSED)
424 {
425 byte *data = a->u.ptr->data;
426
427 bsprintf(buf, "%I4 AS%u", get_ip4(data+4), get_u32(data+0));
428 }
429
430
431 static void
432 bgp_export_community(struct bgp_export_state *s, eattr *a)
433 {
434 if (a->u.ptr->length == 0)
435 UNSET(a);
436
437 a->u.ptr = int_set_sort(s->pool, a->u.ptr);
438 }
439
440 static void
441 bgp_decode_community(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
442 {
443 if (!len || (len % 4))
444 WITHDRAW(BAD_LENGTH, "COMMUNITY", len);
445
446 struct adata *ad = lp_alloc_adata(s->pool, len);
447 get_u32s(data, (u32 *) ad->data, len / 4);
448 bgp_set_attr_ptr(to, s->pool, BA_COMMUNITY, flags, ad);
449 }
450
451
452 static void
453 bgp_export_originator_id(struct bgp_export_state *s, eattr *a)
454 {
455 if (!s->proto->is_internal)
456 UNSET(a);
457 }
458
459 static void
460 bgp_decode_originator_id(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
461 {
462 if (!s->proto->is_internal)
463 DISCARD(BAD_EBGP, "ORIGINATOR_ID");
464
465 if (len != 4)
466 WITHDRAW(BAD_LENGTH, "ORIGINATOR_ID", len);
467
468 u32 val = get_u32(data);
469 bgp_set_attr_u32(to, s->pool, BA_ORIGINATOR_ID, flags, val);
470 }
471
472
473 static void
474 bgp_export_cluster_list(struct bgp_export_state *s UNUSED, eattr *a)
475 {
476 if (!s->proto->is_internal)
477 UNSET(a);
478
479 if (a->u.ptr->length == 0)
480 UNSET(a);
481 }
482
483 static void
484 bgp_decode_cluster_list(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
485 {
486 if (!s->proto->is_internal)
487 DISCARD(BAD_EBGP, "CLUSTER_LIST");
488
489 if (!len || (len % 4))
490 WITHDRAW(BAD_LENGTH, "CLUSTER_LIST", len);
491
492 struct adata *ad = lp_alloc_adata(s->pool, len);
493 get_u32s(data, (u32 *) ad->data, len / 4);
494 bgp_set_attr_ptr(to, s->pool, BA_CLUSTER_LIST, flags, ad);
495 }
496
497 static void
498 bgp_format_cluster_list(eattr *a, byte *buf, uint size)
499 {
500 /* Truncates cluster lists larger than buflen, probably not a problem */
501 int_set_format(a->u.ptr, 0, -1, buf, size);
502 }
503
504
505 static inline u32
506 get_af3(byte *buf)
507 {
508 return (get_u16(buf) << 16) | buf[2];
509 }
510
511 static void
512 bgp_decode_mp_reach_nlri(struct bgp_parse_state *s, uint code UNUSED, uint flags UNUSED, byte *data, uint len, ea_list **to UNUSED)
513 {
514 /*
515 * 2 B MP_REACH_NLRI data - Address Family Identifier
516 * 1 B MP_REACH_NLRI data - Subsequent Address Family Identifier
517 * 1 B MP_REACH_NLRI data - Length of Next Hop Network Address
518 * var MP_REACH_NLRI data - Network Address of Next Hop
519 * 1 B MP_REACH_NLRI data - Reserved (zero)
520 * var MP_REACH_NLRI data - Network Layer Reachability Information
521 */
522
523 if ((len < 5) || (len < (5 + (uint) data[3])))
524 bgp_parse_error(s, 9);
525
526 s->mp_reach_af = get_af3(data);
527 s->mp_next_hop_len = data[3];
528 s->mp_next_hop_data = data + 4;
529 s->mp_reach_len = len - 5 - s->mp_next_hop_len;
530 s->mp_reach_nlri = data + 5 + s->mp_next_hop_len;
531 }
532
533
534 static void
535 bgp_decode_mp_unreach_nlri(struct bgp_parse_state *s, uint code UNUSED, uint flags UNUSED, byte *data, uint len, ea_list **to UNUSED)
536 {
537 /*
538 * 2 B MP_UNREACH_NLRI data - Address Family Identifier
539 * 1 B MP_UNREACH_NLRI data - Subsequent Address Family Identifier
540 * var MP_UNREACH_NLRI data - Network Layer Reachability Information
541 */
542
543 if (len < 3)
544 bgp_parse_error(s, 9);
545
546 s->mp_unreach_af = get_af3(data);
547 s->mp_unreach_len = len - 3;
548 s->mp_unreach_nlri = data + 3;
549 }
550
551
552 static void
553 bgp_export_ext_community(struct bgp_export_state *s, eattr *a)
554 {
555 if (a->u.ptr->length == 0)
556 UNSET(a);
557
558 a->u.ptr = ec_set_sort(s->pool, a->u.ptr);
559 }
560
561 static void
562 bgp_decode_ext_community(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
563 {
564 if (!len || (len % 8))
565 WITHDRAW(BAD_LENGTH, "EXT_COMMUNITY", len);
566
567 struct adata *ad = lp_alloc_adata(s->pool, len);
568 get_u32s(data, (u32 *) ad->data, len / 4);
569 bgp_set_attr_ptr(to, s->pool, BA_EXT_COMMUNITY, flags, ad);
570 }
571
572
573 static void
574 bgp_decode_as4_aggregator(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
575 {
576 if (s->as4_session)
577 DISCARD(NEW_BGP, "AS4_AGGREGATOR");
578
579 if (len != 8)
580 DISCARD(BAD_LENGTH, "AS4_AGGREGATOR", len);
581
582 bgp_set_attr_data(to, s->pool, BA_AS4_AGGREGATOR, flags, data, len);
583 }
584
585 static void
586 bgp_decode_as4_path(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
587 {
588 char err[128];
589
590 if (s->as4_session)
591 DISCARD(NEW_BGP, "AS4_PATH");
592
593 if (len < 6)
594 DISCARD(BAD_LENGTH, "AS4_PATH", len);
595
596 if (!as_path_valid(data, len, 4, 1, err, sizeof(err)))
597 DISCARD("Malformed AS4_PATH attribute - %s", err);
598
599 struct adata *a = lp_alloc_adata(s->pool, len);
600 memcpy(a->data, data, len);
601
602 /* AS_CONFED* segments are invalid in AS4_PATH; RFC 6793 6 */
603 if (as_path_contains_confed(a))
604 {
605 REPORT("Discarding AS_CONFED* segment from AS4_PATH attribute");
606 a = as_path_strip_confed(s->pool, a);
607 }
608
609 bgp_set_attr_ptr(to, s->pool, BA_AS4_PATH, flags, a);
610 }
611
612 static void
613 bgp_export_large_community(struct bgp_export_state *s, eattr *a)
614 {
615 if (a->u.ptr->length == 0)
616 UNSET(a);
617
618 a->u.ptr = lc_set_sort(s->pool, a->u.ptr);
619 }
620
621 static void
622 bgp_decode_large_community(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
623 {
624 if (!len || (len % 12))
625 WITHDRAW(BAD_LENGTH, "LARGE_COMMUNITY", len);
626
627 struct adata *ad = lp_alloc_adata(s->pool, len);
628 get_u32s(data, (u32 *) ad->data, len / 4);
629 bgp_set_attr_ptr(to, s->pool, BA_LARGE_COMMUNITY, flags, ad);
630 }
631
632 static inline void
633 bgp_decode_unknown(struct bgp_parse_state *s, uint code, uint flags, byte *data, uint len, ea_list **to)
634 {
635 bgp_set_attr_data(to, s->pool, code, flags, data, len);
636 }
637
638
639 /*
640 * Attribute table
641 */
642
643 static const struct bgp_attr_desc bgp_attr_table[] = {
644 [BA_ORIGIN] = {
645 .name = "origin",
646 .type = EAF_TYPE_INT,
647 .flags = BAF_TRANSITIVE,
648 .export = bgp_export_origin,
649 .encode = bgp_encode_u8,
650 .decode = bgp_decode_origin,
651 .format = bgp_format_origin,
652 },
653 [BA_AS_PATH] = {
654 .name = "as_path",
655 .type = EAF_TYPE_AS_PATH,
656 .flags = BAF_TRANSITIVE,
657 .encode = bgp_encode_as_path,
658 .decode = bgp_decode_as_path,
659 },
660 [BA_NEXT_HOP] = {
661 .name = "next_hop",
662 .type = EAF_TYPE_IP_ADDRESS,
663 .flags = BAF_TRANSITIVE,
664 .encode = bgp_encode_next_hop,
665 .decode = bgp_decode_next_hop,
666 .format = bgp_format_next_hop,
667 },
668 [BA_MULTI_EXIT_DISC] = {
669 .name = "med",
670 .type = EAF_TYPE_INT,
671 .flags = BAF_OPTIONAL,
672 .encode = bgp_encode_u32,
673 .decode = bgp_decode_med,
674 },
675 [BA_LOCAL_PREF] = {
676 .name = "local_pref",
677 .type = EAF_TYPE_INT,
678 .flags = BAF_TRANSITIVE,
679 .export = bgp_export_local_pref,
680 .encode = bgp_encode_u32,
681 .decode = bgp_decode_local_pref,
682 },
683 [BA_ATOMIC_AGGR] = {
684 .name = "atomic_aggr",
685 .type = EAF_TYPE_OPAQUE,
686 .flags = BAF_TRANSITIVE,
687 .encode = bgp_encode_raw,
688 .decode = bgp_decode_atomic_aggr,
689 },
690 [BA_AGGREGATOR] = {
691 .name = "aggregator",
692 .type = EAF_TYPE_OPAQUE,
693 .flags = BAF_OPTIONAL | BAF_TRANSITIVE,
694 .encode = bgp_encode_aggregator,
695 .decode = bgp_decode_aggregator,
696 .format = bgp_format_aggregator,
697 },
698 [BA_COMMUNITY] = {
699 .name = "community",
700 .type = EAF_TYPE_INT_SET,
701 .flags = BAF_OPTIONAL | BAF_TRANSITIVE,
702 .export = bgp_export_community,
703 .encode = bgp_encode_u32s,
704 .decode = bgp_decode_community,
705 },
706 [BA_ORIGINATOR_ID] = {
707 .name = "originator_id",
708 .type = EAF_TYPE_ROUTER_ID,
709 .flags = BAF_OPTIONAL,
710 .export = bgp_export_originator_id,
711 .encode = bgp_encode_u32,
712 .decode = bgp_decode_originator_id,
713 },
714 [BA_CLUSTER_LIST] = {
715 .name = "cluster_list",
716 .type = EAF_TYPE_INT_SET,
717 .flags = BAF_OPTIONAL,
718 .export = bgp_export_cluster_list,
719 .encode = bgp_encode_u32s,
720 .decode = bgp_decode_cluster_list,
721 .format = bgp_format_cluster_list,
722 },
723 [BA_MP_REACH_NLRI] = {
724 .name = "mp_reach_nlri",
725 .type = EAF_TYPE_OPAQUE,
726 .flags = BAF_OPTIONAL,
727 .decode = bgp_decode_mp_reach_nlri,
728 },
729 [BA_MP_UNREACH_NLRI] = {
730 .name = "mp_unreach_nlri",
731 .type = EAF_TYPE_OPAQUE,
732 .flags = BAF_OPTIONAL,
733 .decode = bgp_decode_mp_unreach_nlri,
734 },
735 [BA_EXT_COMMUNITY] = {
736 .name = "ext_community",
737 .type = EAF_TYPE_EC_SET,
738 .flags = BAF_OPTIONAL | BAF_TRANSITIVE,
739 .export = bgp_export_ext_community,
740 .encode = bgp_encode_u32s,
741 .decode = bgp_decode_ext_community,
742 },
743 [BA_AS4_PATH] = {
744 .name = "as4_path",
745 .type = EAF_TYPE_AS_PATH,
746 .flags = BAF_OPTIONAL | BAF_TRANSITIVE,
747 .encode = bgp_encode_raw,
748 .decode = bgp_decode_as4_path,
749 },
750 [BA_AS4_AGGREGATOR] = {
751 .name = "as4_aggregator",
752 .type = EAF_TYPE_OPAQUE,
753 .flags = BAF_OPTIONAL | BAF_TRANSITIVE,
754 .encode = bgp_encode_raw,
755 .decode = bgp_decode_as4_aggregator,
756 .format = bgp_format_aggregator,
757 },
758 [BA_LARGE_COMMUNITY] = {
759 .name = "large_community",
760 .type = EAF_TYPE_LC_SET,
761 .flags = BAF_OPTIONAL | BAF_TRANSITIVE,
762 .export = bgp_export_large_community,
763 .encode = bgp_encode_u32s,
764 .decode = bgp_decode_large_community,
765 },
766 };
767
768 static inline int
769 bgp_attr_known(uint code)
770 {
771 return (code < ARRAY_SIZE(bgp_attr_table)) && bgp_attr_table[code].name;
772 }
773
774
775 /*
776 * Attribute export
777 */
778
779 static inline void
780 bgp_export_attr(struct bgp_export_state *s, eattr *a, ea_list *to)
781 {
782 if (EA_PROTO(a->id) != EAP_BGP)
783 return;
784
785 uint code = EA_ID(a->id);
786
787 if (bgp_attr_known(code))
788 {
789 const struct bgp_attr_desc *desc = &bgp_attr_table[code];
790
791 /* The flags might have been zero if the attr was added by filters */
792 a->flags = (a->flags & BAF_PARTIAL) | desc->flags;
793
794 /* Set partial bit if new opt-trans attribute is attached to non-local route */
795 if ((s->src != NULL) && (a->type & EAF_ORIGINATED) &&
796 (a->flags & BAF_OPTIONAL) && (a->flags & BAF_TRANSITIVE))
797 a->flags |= BAF_PARTIAL;
798
799 /* Call specific hook */
800 CALL(desc->export, s, a);
801
802 /* Attribute might become undefined in hook */
803 if ((a->type & EAF_TYPE_MASK) == EAF_TYPE_UNDEF)
804 return;
805 }
806 else
807 {
808 /* Don't re-export unknown non-transitive attributes */
809 if (!(a->flags & BAF_TRANSITIVE))
810 return;
811
812 a->flags |= BAF_PARTIAL;
813 }
814
815 /* Append updated attribute */
816 to->attrs[to->count++] = *a;
817 }
818
819 /**
820 * bgp_export_attrs - export BGP attributes
821 * @s: BGP export state
822 * @attrs: a list of extended attributes
823 *
824 * The bgp_export_attrs() function takes a list of attributes and merges it to
825 * one newly allocated and sorted segment. Attributes are validated and
826 * normalized by type-specific export hooks and attribute flags are updated.
827 * Some attributes may be eliminated (e.g. unknown non-tranitive attributes, or
828 * empty community sets).
829 *
830 * Result: one sorted attribute list segment, or NULL if attributes are unsuitable.
831 */
832 static inline ea_list *
833 bgp_export_attrs(struct bgp_export_state *s, ea_list *attrs)
834 {
835 /* Merge the attribute list */
836 ea_list *new = lp_alloc(s->pool, ea_scan(attrs));
837 ea_merge(attrs, new);
838 ea_sort(new);
839
840 uint i, count;
841 count = new->count;
842 new->count = 0;
843
844 /* Export each attribute */
845 for (i = 0; i < count; i++)
846 bgp_export_attr(s, &new->attrs[i], new);
847
848 if (s->err_withdraw)
849 return NULL;
850
851 return new;
852
853 }
854
855
856 /*
857 * Attribute encoding
858 */
859
860 static inline int
861 bgp_encode_attr(struct bgp_write_state *s, eattr *a, byte *buf, uint size)
862 {
863 ASSERT(EA_PROTO(a->id) == EAP_BGP);
864
865 uint code = EA_ID(a->id);
866
867 if (bgp_attr_known(code))
868 return bgp_attr_table[code].encode(s, a, buf, size);
869 else
870 return bgp_encode_raw(s, a, buf, size);
871 }
872
873 /**
874 * bgp_encode_attrs - encode BGP attributes
875 * @s: BGP write state
876 * @attrs: a list of extended attributes
877 * @buf: buffer
878 * @end: buffer end
879 *
880 * The bgp_encode_attrs() function takes a list of extended attributes
881 * and converts it to its BGP representation (a part of an Update message).
882 *
883 * Result: Length of the attribute block generated or -1 if not enough space.
884 */
885 int
886 bgp_encode_attrs(struct bgp_write_state *s, ea_list *attrs, byte *buf, byte *end)
887 {
888 byte *pos = buf;
889 int i, len;
890
891 for (i = 0; i < attrs->count; i++)
892 {
893 len = bgp_encode_attr(s, &attrs->attrs[i], pos, end - pos);
894
895 if (len < 0)
896 return -1;
897
898 pos += len;
899 }
900
901 return pos - buf;
902 }
903
904
905 /*
906 * Attribute decoding
907 */
908
909 static void bgp_process_as4_attrs(ea_list **attrs, struct linpool *pool);
910
911 static inline int
912 bgp_as_path_loopy(struct bgp_proto *p, ea_list *attrs, u32 asn)
913 {
914 eattr *e = bgp_find_attr(attrs, BA_AS_PATH);
915 int num = p->cf->allow_local_as + 1;
916 return (e && (num > 0) && as_path_contains(e->u.ptr, asn, num));
917 }
918
919 static inline int
920 bgp_originator_id_loopy(struct bgp_proto *p, ea_list *attrs)
921 {
922 eattr *e = bgp_find_attr(attrs, BA_ORIGINATOR_ID);
923 return (e && (e->u.data == p->local_id));
924 }
925
926 static inline int
927 bgp_cluster_list_loopy(struct bgp_proto *p, ea_list *attrs)
928 {
929 eattr *e = bgp_find_attr(attrs, BA_CLUSTER_LIST);
930 return (e && int_set_contains(e->u.ptr, p->rr_cluster_id));
931 }
932
933 static inline void
934 bgp_decode_attr(struct bgp_parse_state *s, uint code, uint flags, byte *data, uint len, ea_list **to)
935 {
936 /* Handle duplicate attributes; RFC 7606 3 (g) */
937 if (BIT32_TEST(s->attrs_seen, code))
938 {
939 if ((code == BA_MP_REACH_NLRI) || (code == BA_MP_UNREACH_NLRI))
940 bgp_parse_error(s, 1);
941 else
942 DISCARD("Discarding duplicate attribute (code %u)", code);
943 }
944 BIT32_SET(s->attrs_seen, code);
945
946 if (bgp_attr_known(code))
947 {
948 const struct bgp_attr_desc *desc = &bgp_attr_table[code];
949
950 /* Handle conflicting flags; RFC 7606 3 (c) */
951 if ((flags ^ desc->flags) & (BAF_OPTIONAL | BAF_TRANSITIVE))
952 WITHDRAW("Malformed %s attribute - conflicting flags (%02x)", desc->name, flags);
953
954 desc->decode(s, code, flags, data, len, to);
955 }
956 else /* Unknown attribute */
957 {
958 if (!(flags & BAF_OPTIONAL))
959 WITHDRAW("Unknown attribute (code %u) - conflicting flags (%02x)", code, flags);
960
961 bgp_decode_unknown(s, code, flags, data, len, to);
962 }
963 }
964
965 /**
966 * bgp_decode_attrs - check and decode BGP attributes
967 * @s: BGP parse state
968 * @data: start of attribute block
969 * @len: length of attribute block
970 *
971 * This function takes a BGP attribute block (a part of an Update message), checks
972 * its consistency and converts it to a list of BIRD route attributes represented
973 * by an (uncached) &rta.
974 */
975 ea_list *
976 bgp_decode_attrs(struct bgp_parse_state *s, byte *data, uint len)
977 {
978 struct bgp_proto *p = s->proto;
979 ea_list *attrs = NULL;
980 uint code, flags, alen;
981 byte *pos = data;
982
983 /* Parse the attributes */
984 while (len)
985 {
986 alen = 0;
987
988 /* Read attribute type */
989 if (len < 2)
990 goto framing_error;
991 flags = pos[0];
992 code = pos[1];
993 ADVANCE(pos, len, 2);
994
995 /* Read attribute length */
996 if (flags & BAF_EXT_LEN)
997 {
998 if (len < 2)
999 goto framing_error;
1000 alen = get_u16(pos);
1001 ADVANCE(pos, len, 2);
1002 }
1003 else
1004 {
1005 if (len < 1)
1006 goto framing_error;
1007 alen = *pos;
1008 ADVANCE(pos, len, 1);
1009 }
1010
1011 if (alen > len)
1012 goto framing_error;
1013
1014 DBG("Attr %02x %02x %u\n", code, flags, alen);
1015
1016 bgp_decode_attr(s, code, flags, pos, alen, &attrs);
1017 ADVANCE(pos, len, alen);
1018 }
1019
1020 if (s->err_withdraw)
1021 goto withdraw;
1022
1023 /* If there is no reachability NLRI, we are finished */
1024 if (!s->ip_reach_len && !s->mp_reach_len)
1025 return NULL;
1026
1027
1028 /* Handle missing mandatory attributes; RFC 7606 3 (d) */
1029 if (!BIT32_TEST(s->attrs_seen, BA_ORIGIN))
1030 { REPORT(NO_MANDATORY, "ORIGIN"); goto withdraw; }
1031
1032 if (!BIT32_TEST(s->attrs_seen, BA_AS_PATH))
1033 { REPORT(NO_MANDATORY, "AS_PATH"); goto withdraw; }
1034
1035 /* When receiving attributes from non-AS4-aware BGP speaker, we have to
1036 reconstruct AS_PATH and AGGREGATOR attributes; RFC 6793 4.2.3 */
1037 if (!p->as4_session)
1038 bgp_process_as4_attrs(&attrs, s->pool);
1039
1040 /* Reject routes with our ASN in AS_PATH attribute */
1041 if (bgp_as_path_loopy(p, attrs, p->local_as))
1042 goto withdraw;
1043
1044 /* Reject routes with our Confederation ID in AS_PATH attribute; RFC 5065 4.0 */
1045 if ((p->public_as != p->local_as) && bgp_as_path_loopy(p, attrs, p->public_as))
1046 goto withdraw;
1047
1048 /* Reject routes with our Router ID in ORIGINATOR_ID attribute; RFC 4456 8 */
1049 if (p->is_internal && bgp_originator_id_loopy(p, attrs))
1050 goto withdraw;
1051
1052 /* Reject routes with our Cluster ID in CLUSTER_LIST attribute; RFC 4456 8 */
1053 if (p->rr_client && bgp_cluster_list_loopy(p, attrs))
1054 goto withdraw;
1055
1056 /* If there is no local preference, define one */
1057 if (!BIT32_TEST(s->attrs_seen, BA_LOCAL_PREF))
1058 bgp_set_attr_u32(&attrs, s->pool, BA_LOCAL_PREF, 0, p->cf->default_local_pref);
1059
1060 return attrs;
1061
1062
1063 framing_error:
1064 /* RFC 7606 4 - handle attribute framing errors */
1065 REPORT("Malformed attribute list - framing error (%u/%u) at %d",
1066 alen, len, (int) (pos - s->attrs));
1067
1068 withdraw:
1069 /* RFC 7606 5.2 - handle missing NLRI during errors */
1070 if (!s->ip_reach_len && !s->mp_reach_len)
1071 bgp_parse_error(s, 1);
1072
1073 s->err_withdraw = 1;
1074 return NULL;
1075 }
1076
1077
1078 /*
1079 * Route bucket hash table
1080 */
1081
1082 #define RBH_KEY(b) b->eattrs, b->hash
1083 #define RBH_NEXT(b) b->next
1084 #define RBH_EQ(a1,h1,a2,h2) h1 == h2 && ea_same(a1, a2)
1085 #define RBH_FN(a,h) h
1086
1087 #define RBH_REHASH bgp_rbh_rehash
1088 #define RBH_PARAMS /8, *2, 2, 2, 8, 20
1089
1090
1091 HASH_DEFINE_REHASH_FN(RBH, struct bgp_bucket)
1092
1093 void
1094 bgp_init_bucket_table(struct bgp_channel *c)
1095 {
1096 HASH_INIT(c->bucket_hash, c->pool, 8);
1097
1098 init_list(&c->bucket_queue);
1099 c->withdraw_bucket = NULL;
1100 }
1101
1102 static struct bgp_bucket *
1103 bgp_get_bucket(struct bgp_channel *c, ea_list *new)
1104 {
1105 /* Hash and lookup */
1106 u32 hash = ea_hash(new);
1107 struct bgp_bucket *b = HASH_FIND(c->bucket_hash, RBH, new, hash);
1108
1109 if (b)
1110 return b;
1111
1112 uint ea_size = sizeof(ea_list) + new->count * sizeof(eattr);
1113 uint ea_size_aligned = BIRD_ALIGN(ea_size, CPU_STRUCT_ALIGN);
1114 uint size = sizeof(struct bgp_bucket) + ea_size_aligned;
1115 uint i;
1116 byte *dest;
1117
1118 /* Gather total size of non-inline attributes */
1119 for (i = 0; i < new->count; i++)
1120 {
1121 eattr *a = &new->attrs[i];
1122
1123 if (!(a->type & EAF_EMBEDDED))
1124 size += BIRD_ALIGN(sizeof(struct adata) + a->u.ptr->length, CPU_STRUCT_ALIGN);
1125 }
1126
1127 /* Create the bucket */
1128 b = mb_alloc(c->pool, size);
1129 init_list(&b->prefixes);
1130 b->hash = hash;
1131
1132 /* Copy list of extended attributes */
1133 memcpy(b->eattrs, new, ea_size);
1134 dest = ((byte *) b->eattrs) + ea_size_aligned;
1135
1136 /* Copy values of non-inline attributes */
1137 for (i = 0; i < new->count; i++)
1138 {
1139 eattr *a = &b->eattrs->attrs[i];
1140
1141 if (!(a->type & EAF_EMBEDDED))
1142 {
1143 struct adata *oa = a->u.ptr;
1144 struct adata *na = (struct adata *) dest;
1145 memcpy(na, oa, sizeof(struct adata) + oa->length);
1146 a->u.ptr = na;
1147 dest += BIRD_ALIGN(sizeof(struct adata) + na->length, CPU_STRUCT_ALIGN);
1148 }
1149 }
1150
1151 /* Insert the bucket to send queue and bucket hash */
1152 add_tail(&c->bucket_queue, &b->send_node);
1153 HASH_INSERT2(c->bucket_hash, RBH, c->pool, b);
1154
1155 return b;
1156 }
1157
1158 static struct bgp_bucket *
1159 bgp_get_withdraw_bucket(struct bgp_channel *c)
1160 {
1161 if (!c->withdraw_bucket)
1162 {
1163 c->withdraw_bucket = mb_allocz(c->pool, sizeof(struct bgp_bucket));
1164 init_list(&c->withdraw_bucket->prefixes);
1165 }
1166
1167 return c->withdraw_bucket;
1168 }
1169
1170 void
1171 bgp_free_bucket(struct bgp_channel *c, struct bgp_bucket *b)
1172 {
1173 rem_node(&b->send_node);
1174 HASH_REMOVE2(c->bucket_hash, RBH, c->pool, b);
1175 mb_free(b);
1176 }
1177
1178 void
1179 bgp_defer_bucket(struct bgp_channel *c, struct bgp_bucket *b)
1180 {
1181 rem_node(&b->send_node);
1182 add_tail(&c->bucket_queue, &b->send_node);
1183 }
1184
1185 void
1186 bgp_withdraw_bucket(struct bgp_channel *c, struct bgp_bucket *b)
1187 {
1188 struct bgp_proto *p = (void *) c->c.proto;
1189 struct bgp_bucket *wb = bgp_get_withdraw_bucket(c);
1190
1191 log(L_ERR "%s: Attribute list too long", p->p.name);
1192 while (!EMPTY_LIST(b->prefixes))
1193 {
1194 struct bgp_prefix *px = HEAD(b->prefixes);
1195
1196 log(L_ERR "%s: - withdrawing %N", p->p.name, &px->net);
1197 rem_node(&px->buck_node);
1198 add_tail(&wb->prefixes, &px->buck_node);
1199 }
1200 }
1201
1202
1203 /*
1204 * Prefix hash table
1205 */
1206
1207 #define PXH_KEY(px) px->net, px->path_id, px->hash
1208 #define PXH_NEXT(px) px->next
1209 #define PXH_EQ(n1,i1,h1,n2,i2,h2) h1 == h2 && i1 == i2 && net_equal(n1, n2)
1210 #define PXH_FN(n,i,h) h
1211
1212 #define PXH_REHASH bgp_pxh_rehash
1213 #define PXH_PARAMS /8, *2, 2, 2, 8, 20
1214
1215
1216 HASH_DEFINE_REHASH_FN(PXH, struct bgp_prefix)
1217
1218 void
1219 bgp_init_prefix_table(struct bgp_channel *c)
1220 {
1221 HASH_INIT(c->prefix_hash, c->pool, 8);
1222
1223 uint alen = net_addr_length[c->c.net_type];
1224 c->prefix_slab = alen ? sl_new(c->pool, sizeof(struct bgp_prefix) + alen) : NULL;
1225 }
1226
1227 void
1228 bgp_free_prefix_table(struct bgp_channel *c)
1229 {
1230 HASH_FREE(c->prefix_hash);
1231
1232 rfree(c->prefix_slab);
1233 c->prefix_slab = NULL;
1234 }
1235
1236 static struct bgp_prefix *
1237 bgp_get_prefix(struct bgp_channel *c, net_addr *net, u32 path_id)
1238 {
1239 u32 hash = net_hash(net) ^ u32_hash(path_id);
1240 struct bgp_prefix *px = HASH_FIND(c->prefix_hash, PXH, net, path_id, hash);
1241
1242 if (px)
1243 {
1244 rem_node(&px->buck_node);
1245 return px;
1246 }
1247
1248 if (c->prefix_slab)
1249 px = sl_alloc(c->prefix_slab);
1250 else
1251 px = mb_alloc(c->pool, sizeof(struct bgp_prefix) + net->length);
1252
1253 px->buck_node.next = NULL;
1254 px->buck_node.prev = NULL;
1255 px->hash = hash;
1256 px->path_id = path_id;
1257 net_copy(px->net, net);
1258
1259 HASH_INSERT2(c->prefix_hash, PXH, c->pool, px);
1260
1261 return px;
1262 }
1263
1264 void
1265 bgp_free_prefix(struct bgp_channel *c, struct bgp_prefix *px)
1266 {
1267 rem_node(&px->buck_node);
1268 HASH_REMOVE2(c->prefix_hash, PXH, c->pool, px);
1269
1270 if (c->prefix_slab)
1271 sl_free(c->prefix_slab, px);
1272 else
1273 mb_free(px);
1274 }
1275
1276
1277 /*
1278 * BGP protocol glue
1279 */
1280
1281 int
1282 bgp_import_control(struct proto *P, rte **new, ea_list **attrs UNUSED, struct linpool *pool UNUSED)
1283 {
1284 rte *e = *new;
1285 struct proto *SRC = e->attrs->src->proto;
1286 struct bgp_proto *p = (struct bgp_proto *) P;
1287 struct bgp_proto *src = (SRC->proto == &proto_bgp) ? (struct bgp_proto *) SRC : NULL;
1288
1289 /* Reject our routes */
1290 if (src == p)
1291 return -1;
1292
1293 /* Accept non-BGP routes */
1294 if (src == NULL)
1295 return 0;
1296
1297 // XXXX: Check next hop AF
1298
1299 /* IBGP route reflection, RFC 4456 */
1300 if (p->is_internal && src->is_internal && (p->local_as == src->local_as))
1301 {
1302 /* Rejected unless configured as route reflector */
1303 if (!p->rr_client && !src->rr_client)
1304 return -1;
1305
1306 /* Generally, this should be handled when path is received, but we check it
1307 also here as rr_cluster_id may be undefined or different in src. */
1308 if (p->rr_cluster_id && bgp_cluster_list_loopy(p, e->attrs->eattrs))
1309 return -1;
1310 }
1311
1312 /* Handle well-known communities, RFC 1997 */
1313 struct eattr *c;
1314 if (p->cf->interpret_communities &&
1315 (c = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_COMMUNITY))))
1316 {
1317 struct adata *d = c->u.ptr;
1318
1319 /* Do not export anywhere */
1320 if (int_set_contains(d, BGP_COMM_NO_ADVERTISE))
1321 return -1;
1322
1323 /* Do not export outside of AS (or member-AS) */
1324 if (!p->is_internal && int_set_contains(d, BGP_COMM_NO_EXPORT_SUBCONFED))
1325 return -1;
1326
1327 /* Do not export outside of AS (or confederation) */
1328 if (!p->is_interior && int_set_contains(d, BGP_COMM_NO_EXPORT))
1329 return -1;
1330 }
1331
1332 return 0;
1333 }
1334
1335
1336 static adata null_adata; /* adata of length 0 */
1337
1338 static ea_list *
1339 bgp_update_attrs(struct bgp_proto *p, struct bgp_channel *c, rte *e, ea_list *attrs0, struct linpool *pool)
1340 {
1341 struct proto *SRC = e->attrs->src->proto;
1342 struct bgp_proto *src = (SRC->proto == &proto_bgp) ? (void *) SRC : NULL;
1343 struct bgp_export_state s = { .proto = p, .channel =c, .pool = pool, .src = src, .route = e };
1344 ea_list *attrs = attrs0;
1345 eattr *a;
1346 adata *ad;
1347
1348 /* ORIGIN attribute - mandatory, attach if missing */
1349 if (! bgp_find_attr(attrs0, BA_ORIGIN))
1350 bgp_set_attr_u32(&attrs, pool, BA_ORIGIN, 0, src ? ORIGIN_INCOMPLETE : ORIGIN_IGP);
1351
1352 /* AS_PATH attribute - mandatory */
1353 a = bgp_find_attr(attrs0, BA_AS_PATH);
1354 ad = a ? a->u.ptr : &null_adata;
1355
1356 /* AS_PATH attribute - strip AS_CONFED* segments outside confederation */
1357 if ((!p->cf->confederation || !p->is_interior) && as_path_contains_confed(ad))
1358 ad = as_path_strip_confed(pool, ad);
1359
1360 /* AS_PATH attribute - keep or prepend ASN */
1361 if (p->is_internal ||
1362 (p->rs_client && src && src->rs_client))
1363 {
1364 /* IBGP or route server -> just ensure there is one */
1365 if (!a)
1366 bgp_set_attr_ptr(&attrs, pool, BA_AS_PATH, 0, &null_adata);
1367 }
1368 else if (p->is_interior)
1369 {
1370 /* Confederation -> prepend ASN as AS_CONFED_SEQUENCE */
1371 ad = as_path_prepend2(pool, ad, AS_PATH_CONFED_SEQUENCE, p->public_as);
1372 bgp_set_attr_ptr(&attrs, pool, BA_AS_PATH, 0, ad);
1373 }
1374 else /* Regular EBGP (no RS, no confederation) */
1375 {
1376 /* Regular EBGP -> prepend ASN as regular sequence */
1377 ad = as_path_prepend2(pool, ad, AS_PATH_SEQUENCE, p->public_as);
1378 bgp_set_attr_ptr(&attrs, pool, BA_AS_PATH, 0, ad);
1379
1380 /* MULTI_EXIT_DESC attribute - accept only if set in export filter */
1381 a = bgp_find_attr(attrs0, BA_MULTI_EXIT_DISC);
1382 if (a && !(a->type & EAF_FRESH))
1383 bgp_unset_attr(&attrs, pool, BA_MULTI_EXIT_DISC);
1384 }
1385
1386 /* NEXT_HOP attribute - delegated to AF-specific hook */
1387 a = bgp_find_attr(attrs0, BA_NEXT_HOP);
1388 bgp_update_next_hop(&s, a, &attrs);
1389
1390 /* LOCAL_PREF attribute - required for IBGP, attach if missing */
1391 if (p->is_interior && ! bgp_find_attr(attrs0, BA_LOCAL_PREF))
1392 bgp_set_attr_u32(&attrs, pool, BA_LOCAL_PREF, 0, p->cf->default_local_pref);
1393
1394 /* IBGP route reflection, RFC 4456 */
1395 if (src && src->is_internal && p->is_internal && (src->local_as == p->local_as))
1396 {
1397 /* ORIGINATOR_ID attribute - attach if not already set */
1398 if (! bgp_find_attr(attrs0, BA_ORIGINATOR_ID))
1399 bgp_set_attr_u32(&attrs, pool, BA_ORIGINATOR_ID, 0, src->remote_id);
1400
1401 /* CLUSTER_LIST attribute - prepend cluster ID */
1402 a = bgp_find_attr(attrs0, BA_CLUSTER_LIST);
1403 ad = a ? a->u.ptr : NULL;
1404
1405 /* Prepend src cluster ID */
1406 if (src->rr_cluster_id)
1407 ad = int_set_prepend(pool, ad, src->rr_cluster_id);
1408
1409 /* Prepend dst cluster ID if src and dst clusters are different */
1410 if (p->rr_cluster_id && (src->rr_cluster_id != p->rr_cluster_id))
1411 ad = int_set_prepend(pool, ad, p->rr_cluster_id);
1412
1413 /* Should be at least one prepended cluster ID */
1414 bgp_set_attr_ptr(&attrs, pool, BA_CLUSTER_LIST, 0, ad);
1415 }
1416
1417 /* AS4_* transition attributes, RFC 6793 4.2.2 */
1418 if (! p->as4_session)
1419 {
1420 a = bgp_find_attr(attrs, BA_AS_PATH);
1421 if (a && as_path_contains_as4(a->u.ptr))
1422 {
1423 bgp_set_attr_ptr(&attrs, pool, BA_AS_PATH, 0, as_path_to_old(pool, a->u.ptr));
1424 bgp_set_attr_ptr(&attrs, pool, BA_AS4_PATH, 0, as_path_strip_confed(pool, a->u.ptr));
1425 }
1426
1427 a = bgp_find_attr(attrs, BA_AGGREGATOR);
1428 if (a && aggregator_contains_as4(a->u.ptr))
1429 {
1430 bgp_set_attr_ptr(&attrs, pool, BA_AGGREGATOR, 0, aggregator_to_old(pool, a->u.ptr));
1431 bgp_set_attr_ptr(&attrs, pool, BA_AS4_AGGREGATOR, 0, a->u.ptr);
1432 }
1433 }
1434
1435 /*
1436 * Presence of mandatory attributes ORIGIN and AS_PATH is ensured by above
1437 * conditions. Presence and validity of quasi-mandatory NEXT_HOP attribute
1438 * should be checked in AF-specific hooks.
1439 */
1440
1441 /* Apply per-attribute export hooks for validatation and normalization */
1442 return bgp_export_attrs(&s, attrs);
1443 }
1444
1445 void
1446 bgp_rt_notify(struct proto *P, struct channel *C, net *n, rte *new, rte *old, ea_list *attrs)
1447 {
1448 struct bgp_proto *p = (void *) P;
1449 struct bgp_channel *c = (void *) C;
1450 struct bgp_bucket *buck;
1451 struct bgp_prefix *px;
1452 u32 path;
1453
1454 if (new)
1455 {
1456 attrs = bgp_update_attrs(p, c, new, attrs, bgp_linpool);
1457
1458 /* If attributes are invalid, we fail back to withdraw */
1459 buck = attrs ? bgp_get_bucket(c, attrs) : bgp_get_withdraw_bucket(c);
1460 path = new->attrs->src->global_id;
1461
1462 lp_flush(bgp_linpool);
1463 }
1464 else
1465 {
1466 buck = bgp_get_withdraw_bucket(c);
1467 path = old->attrs->src->global_id;
1468 }
1469
1470 px = bgp_get_prefix(c, n->n.addr, c->add_path_tx ? path : 0);
1471 add_tail(&buck->prefixes, &px->buck_node);
1472
1473 bgp_schedule_packet(p->conn, c, PKT_UPDATE);
1474 }
1475
1476
1477 static inline u32
1478 bgp_get_neighbor(rte *r)
1479 {
1480 eattr *e = ea_find(r->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1481 u32 as;
1482
1483 if (e && as_path_get_first_regular(e->u.ptr, &as))
1484 return as;
1485
1486 /* If AS_PATH is not defined, we treat rte as locally originated */
1487 struct bgp_proto *p = (void *) r->attrs->src->proto;
1488 return p->cf->confederation ?: p->local_as;
1489 }
1490
1491 static inline int
1492 rte_resolvable(rte *rt)
1493 {
1494 return rt->attrs->dest == RTD_UNICAST;
1495 }
1496
1497 int
1498 bgp_rte_better(rte *new, rte *old)
1499 {
1500 struct bgp_proto *new_bgp = (struct bgp_proto *) new->attrs->src->proto;
1501 struct bgp_proto *old_bgp = (struct bgp_proto *) old->attrs->src->proto;
1502 eattr *x, *y;
1503 u32 n, o;
1504
1505 /* Skip suppressed routes (see bgp_rte_recalculate()) */
1506 n = new->u.bgp.suppressed;
1507 o = old->u.bgp.suppressed;
1508 if (n > o)
1509 return 0;
1510 if (n < o)
1511 return 1;
1512
1513 /* RFC 4271 9.1.2.1. Route resolvability test */
1514 n = rte_resolvable(new);
1515 o = rte_resolvable(old);
1516 if (n > o)
1517 return 1;
1518 if (n < o)
1519 return 0;
1520
1521 /* Start with local preferences */
1522 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_LOCAL_PREF));
1523 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_LOCAL_PREF));
1524 n = x ? x->u.data : new_bgp->cf->default_local_pref;
1525 o = y ? y->u.data : old_bgp->cf->default_local_pref;
1526 if (n > o)
1527 return 1;
1528 if (n < o)
1529 return 0;
1530
1531 /* RFC 4271 9.1.2.2. a) Use AS path lengths */
1532 if (new_bgp->cf->compare_path_lengths || old_bgp->cf->compare_path_lengths)
1533 {
1534 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1535 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1536 n = x ? as_path_getlen(x->u.ptr) : AS_PATH_MAXLEN;
1537 o = y ? as_path_getlen(y->u.ptr) : AS_PATH_MAXLEN;
1538 if (n < o)
1539 return 1;
1540 if (n > o)
1541 return 0;
1542 }
1543
1544 /* RFC 4271 9.1.2.2. b) Use origins */
1545 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1546 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1547 n = x ? x->u.data : ORIGIN_INCOMPLETE;
1548 o = y ? y->u.data : ORIGIN_INCOMPLETE;
1549 if (n < o)
1550 return 1;
1551 if (n > o)
1552 return 0;
1553
1554 /* RFC 4271 9.1.2.2. c) Compare MED's */
1555 /* Proper RFC 4271 path selection cannot be interpreted as finding
1556 * the best path in some ordering. It is implemented partially in
1557 * bgp_rte_recalculate() when deterministic_med option is
1558 * active. Without that option, the behavior is just an
1559 * approximation, which in specific situations may lead to
1560 * persistent routing loops, because it is nondeterministic - it
1561 * depends on the order in which routes appeared. But it is also the
1562 * same behavior as used by default in Cisco routers, so it is
1563 * probably not a big issue.
1564 */
1565 if (new_bgp->cf->med_metric || old_bgp->cf->med_metric ||
1566 (bgp_get_neighbor(new) == bgp_get_neighbor(old)))
1567 {
1568 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
1569 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
1570 n = x ? x->u.data : new_bgp->cf->default_med;
1571 o = y ? y->u.data : old_bgp->cf->default_med;
1572 if (n < o)
1573 return 1;
1574 if (n > o)
1575 return 0;
1576 }
1577
1578 /* RFC 4271 9.1.2.2. d) Prefer external peers */
1579 if (new_bgp->is_interior > old_bgp->is_interior)
1580 return 0;
1581 if (new_bgp->is_interior < old_bgp->is_interior)
1582 return 1;
1583
1584 /* RFC 4271 9.1.2.2. e) Compare IGP metrics */
1585 n = new_bgp->cf->igp_metric ? new->attrs->igp_metric : 0;
1586 o = old_bgp->cf->igp_metric ? old->attrs->igp_metric : 0;
1587 if (n < o)
1588 return 1;
1589 if (n > o)
1590 return 0;
1591
1592 /* RFC 4271 9.1.2.2. f) Compare BGP identifiers */
1593 /* RFC 4456 9. a) Use ORIGINATOR_ID instead of local neighbor ID */
1594 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
1595 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
1596 n = x ? x->u.data : new_bgp->remote_id;
1597 o = y ? y->u.data : old_bgp->remote_id;
1598
1599 /* RFC 5004 - prefer older routes */
1600 /* (if both are external and from different peer) */
1601 if ((new_bgp->cf->prefer_older || old_bgp->cf->prefer_older) &&
1602 !new_bgp->is_internal && n != o)
1603 return 0;
1604
1605 /* rest of RFC 4271 9.1.2.2. f) */
1606 if (n < o)
1607 return 1;
1608 if (n > o)
1609 return 0;
1610
1611 /* RFC 4456 9. b) Compare cluster list lengths */
1612 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
1613 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
1614 n = x ? int_set_get_size(x->u.ptr) : 0;
1615 o = y ? int_set_get_size(y->u.ptr) : 0;
1616 if (n < o)
1617 return 1;
1618 if (n > o)
1619 return 0;
1620
1621 /* RFC 4271 9.1.2.2. g) Compare peer IP adresses */
1622 return (ipa_compare(new_bgp->cf->remote_ip, old_bgp->cf->remote_ip) < 0);
1623 }
1624
1625
1626 int
1627 bgp_rte_mergable(rte *pri, rte *sec)
1628 {
1629 struct bgp_proto *pri_bgp = (struct bgp_proto *) pri->attrs->src->proto;
1630 struct bgp_proto *sec_bgp = (struct bgp_proto *) sec->attrs->src->proto;
1631 eattr *x, *y;
1632 u32 p, s;
1633
1634 /* Skip suppressed routes (see bgp_rte_recalculate()) */
1635 if (pri->u.bgp.suppressed != sec->u.bgp.suppressed)
1636 return 0;
1637
1638 /* RFC 4271 9.1.2.1. Route resolvability test */
1639 if (!rte_resolvable(sec))
1640 return 0;
1641
1642 /* Start with local preferences */
1643 x = ea_find(pri->attrs->eattrs, EA_CODE(EAP_BGP, BA_LOCAL_PREF));
1644 y = ea_find(sec->attrs->eattrs, EA_CODE(EAP_BGP, BA_LOCAL_PREF));
1645 p = x ? x->u.data : pri_bgp->cf->default_local_pref;
1646 s = y ? y->u.data : sec_bgp->cf->default_local_pref;
1647 if (p != s)
1648 return 0;
1649
1650 /* RFC 4271 9.1.2.2. a) Use AS path lengths */
1651 if (pri_bgp->cf->compare_path_lengths || sec_bgp->cf->compare_path_lengths)
1652 {
1653 x = ea_find(pri->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1654 y = ea_find(sec->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1655 p = x ? as_path_getlen(x->u.ptr) : AS_PATH_MAXLEN;
1656 s = y ? as_path_getlen(y->u.ptr) : AS_PATH_MAXLEN;
1657
1658 if (p != s)
1659 return 0;
1660
1661 // if (DELTA(p, s) > pri_bgp->cf->relax_multipath)
1662 // return 0;
1663 }
1664
1665 /* RFC 4271 9.1.2.2. b) Use origins */
1666 x = ea_find(pri->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1667 y = ea_find(sec->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1668 p = x ? x->u.data : ORIGIN_INCOMPLETE;
1669 s = y ? y->u.data : ORIGIN_INCOMPLETE;
1670 if (p != s)
1671 return 0;
1672
1673 /* RFC 4271 9.1.2.2. c) Compare MED's */
1674 if (pri_bgp->cf->med_metric || sec_bgp->cf->med_metric ||
1675 (bgp_get_neighbor(pri) == bgp_get_neighbor(sec)))
1676 {
1677 x = ea_find(pri->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
1678 y = ea_find(sec->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
1679 p = x ? x->u.data : pri_bgp->cf->default_med;
1680 s = y ? y->u.data : sec_bgp->cf->default_med;
1681 if (p != s)
1682 return 0;
1683 }
1684
1685 /* RFC 4271 9.1.2.2. d) Prefer external peers */
1686 if (pri_bgp->is_interior != sec_bgp->is_interior)
1687 return 0;
1688
1689 /* RFC 4271 9.1.2.2. e) Compare IGP metrics */
1690 p = pri_bgp->cf->igp_metric ? pri->attrs->igp_metric : 0;
1691 s = sec_bgp->cf->igp_metric ? sec->attrs->igp_metric : 0;
1692 if (p != s)
1693 return 0;
1694
1695 /* Remaining criteria are ignored */
1696
1697 return 1;
1698 }
1699
1700
1701 static inline int
1702 same_group(rte *r, u32 lpref, u32 lasn)
1703 {
1704 return (r->pref == lpref) && (bgp_get_neighbor(r) == lasn);
1705 }
1706
1707 static inline int
1708 use_deterministic_med(rte *r)
1709 {
1710 struct proto *P = r->attrs->src->proto;
1711 return (P->proto == &proto_bgp) && ((struct bgp_proto *) P)->cf->deterministic_med;
1712 }
1713
1714 int
1715 bgp_rte_recalculate(rtable *table, net *net, rte *new, rte *old, rte *old_best)
1716 {
1717 rte *r, *s;
1718 rte *key = new ? new : old;
1719 u32 lpref = key->pref;
1720 u32 lasn = bgp_get_neighbor(key);
1721 int old_is_group_best = 0;
1722
1723 /*
1724 * Proper RFC 4271 path selection is a bit complicated, it cannot be
1725 * implemented just by rte_better(), because it is not a linear
1726 * ordering. But it can be splitted to two levels, where the lower
1727 * level chooses the best routes in each group of routes from the
1728 * same neighboring AS and higher level chooses the best route (with
1729 * a slightly different ordering) between the best-in-group routes.
1730 *
1731 * When deterministic_med is disabled, we just ignore this issue and
1732 * choose the best route by bgp_rte_better() alone. If enabled, the
1733 * lower level of the route selection is done here (for the group
1734 * to which the changed route belongs), all routes in group are
1735 * marked as suppressed, just chosen best-in-group is not.
1736 *
1737 * Global best route selection then implements higher level by
1738 * choosing between non-suppressed routes (as they are always
1739 * preferred over suppressed routes). Routes from BGP protocols
1740 * that do not set deterministic_med are just never suppressed. As
1741 * they do not participate in the lower level selection, it is OK
1742 * that this fn is not called for them.
1743 *
1744 * The idea is simple, the implementation is more problematic,
1745 * mostly because of optimizations in rte_recalculate() that
1746 * avoids full recalculation in most cases.
1747 *
1748 * We can assume that at least one of new, old is non-NULL and both
1749 * are from the same protocol with enabled deterministic_med. We
1750 * group routes by both neighbor AS (lasn) and preference (lpref),
1751 * because bgp_rte_better() does not handle preference itself.
1752 */
1753
1754 /* If new and old are from different groups, we just process that
1755 as two independent events */
1756 if (new && old && !same_group(old, lpref, lasn))
1757 {
1758 int i1, i2;
1759 i1 = bgp_rte_recalculate(table, net, NULL, old, old_best);
1760 i2 = bgp_rte_recalculate(table, net, new, NULL, old_best);
1761 return i1 || i2;
1762 }
1763
1764 /*
1765 * We could find the best-in-group and then make some shortcuts like
1766 * in rte_recalculate, but as we would have to walk through all
1767 * net->routes just to find it, it is probably not worth. So we
1768 * just have two simpler fast cases that use just the old route.
1769 * We also set suppressed flag to avoid using it in bgp_rte_better().
1770 */
1771
1772 if (new)
1773 new->u.bgp.suppressed = 1;
1774
1775 if (old)
1776 {
1777 old_is_group_best = !old->u.bgp.suppressed;
1778 old->u.bgp.suppressed = 1;
1779 int new_is_better = new && bgp_rte_better(new, old);
1780
1781 /* The first case - replace not best with worse (or remove not best) */
1782 if (!old_is_group_best && !new_is_better)
1783 return 0;
1784
1785 /* The second case - replace the best with better */
1786 if (old_is_group_best && new_is_better)
1787 {
1788 /* new is best-in-group, the see discussion below - this is
1789 a special variant of NBG && OBG. From OBG we can deduce
1790 that same_group(old_best) iff (old == old_best) */
1791 new->u.bgp.suppressed = 0;
1792 return (old == old_best);
1793 }
1794 }
1795
1796 /* The default case - find a new best-in-group route */
1797 r = new; /* new may not be in the list */
1798 for (s=net->routes; rte_is_valid(s); s=s->next)
1799 if (use_deterministic_med(s) && same_group(s, lpref, lasn))
1800 {
1801 s->u.bgp.suppressed = 1;
1802 if (!r || bgp_rte_better(s, r))
1803 r = s;
1804 }
1805
1806 /* Simple case - the last route in group disappears */
1807 if (!r)
1808 return 0;
1809
1810 /* Found best-in-group */
1811 r->u.bgp.suppressed = 0;
1812
1813 /*
1814 * There are generally two reasons why we have to force
1815 * recalculation (return 1): First, the new route may be wrongfully
1816 * chosen to be the best in the first case check in
1817 * rte_recalculate(), this may happen only if old_best is from the
1818 * same group. Second, another (different than new route)
1819 * best-in-group is chosen and that may be the proper best (although
1820 * rte_recalculate() without ignore that possibility).
1821 *
1822 * There are three possible cases according to whether the old route
1823 * was the best in group (OBG, stored in old_is_group_best) and
1824 * whether the new route is the best in group (NBG, tested by r == new).
1825 * These cases work even if old or new is NULL.
1826 *
1827 * NBG -> new is a possible candidate for the best route, so we just
1828 * check for the first reason using same_group().
1829 *
1830 * !NBG && OBG -> Second reason applies, return 1
1831 *
1832 * !NBG && !OBG -> Best in group does not change, old != old_best,
1833 * rte_better(new, old_best) is false and therefore
1834 * the first reason does not apply, return 0
1835 */
1836
1837 if (r == new)
1838 return old_best && same_group(old_best, lpref, lasn);
1839 else
1840 return old_is_group_best;
1841 }
1842
1843
1844 /*
1845 * Reconstruct AS_PATH and AGGREGATOR according to RFC 6793 4.2.3
1846 */
1847 static void
1848 bgp_process_as4_attrs(ea_list **attrs, struct linpool *pool)
1849 {
1850 eattr *p2 = bgp_find_attr(*attrs, BA_AS_PATH);
1851 eattr *p4 = bgp_find_attr(*attrs, BA_AS4_PATH);
1852 eattr *a2 = bgp_find_attr(*attrs, BA_AGGREGATOR);
1853 eattr *a4 = bgp_find_attr(*attrs, BA_AS4_AGGREGATOR);
1854
1855 /* First, unset AS4_* attributes */
1856 if (p4) bgp_unset_attr(attrs, pool, BA_AS4_PATH);
1857 if (a4) bgp_unset_attr(attrs, pool, BA_AS4_AGGREGATOR);
1858
1859 /* Handle AGGREGATOR attribute */
1860 if (a2 && a4)
1861 {
1862 u32 a2_asn = get_u32(a2->u.ptr->data);
1863
1864 /* If routes were aggregated by an old router, then AS4_PATH and
1865 AS4_AGGREGATOR are invalid. In that case we give up. */
1866 if (a2_asn != AS_TRANS)
1867 return;
1868
1869 /* Use AS4_AGGREGATOR instead of AGGREGATOR */
1870 a2->u.ptr = a4->u.ptr;
1871 }
1872
1873 /* Handle AS_PATH attribute */
1874 if (p2 && p4)
1875 {
1876 /* Both as_path_getlen() and as_path_cut() take AS_CONFED* as zero length */
1877 int p2_len = as_path_getlen(p2->u.ptr);
1878 int p4_len = as_path_getlen(p4->u.ptr);
1879
1880 /* AS_PATH is too short, give up */
1881 if (p2_len < p4_len)
1882 return;
1883
1884 /* Merge AS_PATH and AS4_PATH */
1885 as_path_cut(p2->u.ptr, p2_len - p4_len);
1886 p2->u.ptr = as_path_merge(pool, p2->u.ptr, p4->u.ptr);
1887 }
1888 }
1889
1890 int
1891 bgp_get_attr(eattr *a, byte *buf, int buflen)
1892 {
1893 uint i = EA_ID(a->id);
1894 const struct bgp_attr_desc *d;
1895 int len;
1896
1897 if (bgp_attr_known(i))
1898 {
1899 d = &bgp_attr_table[i];
1900 len = bsprintf(buf, "%s", d->name);
1901 buf += len;
1902 if (d->format)
1903 {
1904 *buf++ = ':';
1905 *buf++ = ' ';
1906 d->format(a, buf, buflen - len - 2);
1907 return GA_FULL;
1908 }
1909 return GA_NAME;
1910 }
1911
1912 bsprintf(buf, "%02x%s", i, (a->flags & BAF_TRANSITIVE) ? " [t]" : "");
1913 return GA_NAME;
1914 }
1915
1916 void
1917 bgp_get_route_info(rte *e, byte *buf, ea_list *attrs)
1918 {
1919 eattr *p = ea_find(attrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1920 eattr *o = ea_find(attrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1921 u32 origas;
1922
1923 buf += bsprintf(buf, " (%d", e->pref);
1924
1925 if (e->u.bgp.suppressed)
1926 buf += bsprintf(buf, "-");
1927
1928 if (e->attrs->hostentry)
1929 {
1930 if (!rte_resolvable(e))
1931 buf += bsprintf(buf, "/-");
1932 else if (e->attrs->igp_metric >= IGP_METRIC_UNKNOWN)
1933 buf += bsprintf(buf, "/?");
1934 else
1935 buf += bsprintf(buf, "/%d", e->attrs->igp_metric);
1936 }
1937 buf += bsprintf(buf, ") [");
1938
1939 if (p && as_path_get_last(p->u.ptr, &origas))
1940 buf += bsprintf(buf, "AS%u", origas);
1941 if (o)
1942 buf += bsprintf(buf, "%c", "ie?"[o->u.data]);
1943 strcpy(buf, "]");
1944 }