]> git.ipfire.org Git - thirdparty/bird.git/blob - proto/bgp/attrs.c
Merge commit '1e8721e2aeccfbc3f533e8b8abc07582cee77e9a' into int-new
[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 && !s->proto->cf->allow_local_pref)
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 && !s->proto->cf->allow_local_pref)
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 void
633 bgp_export_mpls_label_stack(struct bgp_export_state *s, eattr *a)
634 {
635 net_addr *n = s->route->net->n.addr;
636 u32 *labels = (u32 *) a->u.ptr->data;
637 uint lnum = a->u.ptr->length / 4;
638
639 /* Perhaps we should just ignore it? */
640 if (!s->mpls)
641 WITHDRAW("Unexpected MPLS stack");
642
643 /* Empty MPLS stack is not allowed */
644 if (!lnum)
645 WITHDRAW("Malformed MPLS stack - empty");
646
647 /* This is ugly, but we must ensure that labels fit into NLRI field */
648 if ((24*lnum + (net_is_vpn(n) ? 64 : 0) + net_pxlen(n)) > 255)
649 WITHDRAW("Malformed MPLS stack - too many labels (%u)", lnum);
650
651 for (uint i = 0; i < lnum; i++)
652 {
653 if (labels[i] > 0xfffff)
654 WITHDRAW("Malformed MPLS stack - invalid label (%u)", labels[i]);
655
656 /* TODO: Check for special-purpose label values? */
657 }
658 }
659
660 static int
661 bgp_encode_mpls_label_stack(struct bgp_write_state *s, eattr *a, byte *buf UNUSED, uint size UNUSED)
662 {
663 /*
664 * MPLS labels are encoded as a part of the NLRI in MP_REACH_NLRI attribute,
665 * so we store MPLS_LABEL_STACK and encode it later by AFI-specific hooks.
666 */
667
668 s->mpls_labels = a->u.ptr;
669 return 0;
670 }
671
672 static void
673 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)
674 {
675 DISCARD("Discarding received attribute #0");
676 }
677
678 static void
679 bgp_format_mpls_label_stack(eattr *a, byte *buf, uint size)
680 {
681 u32 *labels = (u32 *) a->u.ptr->data;
682 uint lnum = a->u.ptr->length / 4;
683 char *pos = buf;
684
685 for (uint i = 0; i < lnum; i++)
686 {
687 if (size < 20)
688 {
689 bsprintf(pos, "...");
690 return;
691 }
692
693 uint l = bsprintf(pos, "%d/", labels[i]);
694 ADVANCE(pos, size, l);
695 }
696
697 /* Clear last slash or terminate empty string */
698 pos[lnum ? -1 : 0] = 0;
699 }
700
701 static inline void
702 bgp_decode_unknown(struct bgp_parse_state *s, uint code, uint flags, byte *data, uint len, ea_list **to)
703 {
704 bgp_set_attr_data(to, s->pool, code, flags, data, len);
705 }
706
707
708 /*
709 * Attribute table
710 */
711
712 static const struct bgp_attr_desc bgp_attr_table[] = {
713 [BA_ORIGIN] = {
714 .name = "origin",
715 .type = EAF_TYPE_INT,
716 .flags = BAF_TRANSITIVE,
717 .export = bgp_export_origin,
718 .encode = bgp_encode_u8,
719 .decode = bgp_decode_origin,
720 .format = bgp_format_origin,
721 },
722 [BA_AS_PATH] = {
723 .name = "as_path",
724 .type = EAF_TYPE_AS_PATH,
725 .flags = BAF_TRANSITIVE,
726 .encode = bgp_encode_as_path,
727 .decode = bgp_decode_as_path,
728 },
729 [BA_NEXT_HOP] = {
730 .name = "next_hop",
731 .type = EAF_TYPE_IP_ADDRESS,
732 .flags = BAF_TRANSITIVE,
733 .encode = bgp_encode_next_hop,
734 .decode = bgp_decode_next_hop,
735 .format = bgp_format_next_hop,
736 },
737 [BA_MULTI_EXIT_DISC] = {
738 .name = "med",
739 .type = EAF_TYPE_INT,
740 .flags = BAF_OPTIONAL,
741 .encode = bgp_encode_u32,
742 .decode = bgp_decode_med,
743 },
744 [BA_LOCAL_PREF] = {
745 .name = "local_pref",
746 .type = EAF_TYPE_INT,
747 .flags = BAF_TRANSITIVE,
748 .export = bgp_export_local_pref,
749 .encode = bgp_encode_u32,
750 .decode = bgp_decode_local_pref,
751 },
752 [BA_ATOMIC_AGGR] = {
753 .name = "atomic_aggr",
754 .type = EAF_TYPE_OPAQUE,
755 .flags = BAF_TRANSITIVE,
756 .encode = bgp_encode_raw,
757 .decode = bgp_decode_atomic_aggr,
758 },
759 [BA_AGGREGATOR] = {
760 .name = "aggregator",
761 .type = EAF_TYPE_OPAQUE,
762 .flags = BAF_OPTIONAL | BAF_TRANSITIVE,
763 .encode = bgp_encode_aggregator,
764 .decode = bgp_decode_aggregator,
765 .format = bgp_format_aggregator,
766 },
767 [BA_COMMUNITY] = {
768 .name = "community",
769 .type = EAF_TYPE_INT_SET,
770 .flags = BAF_OPTIONAL | BAF_TRANSITIVE,
771 .export = bgp_export_community,
772 .encode = bgp_encode_u32s,
773 .decode = bgp_decode_community,
774 },
775 [BA_ORIGINATOR_ID] = {
776 .name = "originator_id",
777 .type = EAF_TYPE_ROUTER_ID,
778 .flags = BAF_OPTIONAL,
779 .export = bgp_export_originator_id,
780 .encode = bgp_encode_u32,
781 .decode = bgp_decode_originator_id,
782 },
783 [BA_CLUSTER_LIST] = {
784 .name = "cluster_list",
785 .type = EAF_TYPE_INT_SET,
786 .flags = BAF_OPTIONAL,
787 .export = bgp_export_cluster_list,
788 .encode = bgp_encode_u32s,
789 .decode = bgp_decode_cluster_list,
790 .format = bgp_format_cluster_list,
791 },
792 [BA_MP_REACH_NLRI] = {
793 .name = "mp_reach_nlri",
794 .type = EAF_TYPE_OPAQUE,
795 .flags = BAF_OPTIONAL,
796 .decode = bgp_decode_mp_reach_nlri,
797 },
798 [BA_MP_UNREACH_NLRI] = {
799 .name = "mp_unreach_nlri",
800 .type = EAF_TYPE_OPAQUE,
801 .flags = BAF_OPTIONAL,
802 .decode = bgp_decode_mp_unreach_nlri,
803 },
804 [BA_EXT_COMMUNITY] = {
805 .name = "ext_community",
806 .type = EAF_TYPE_EC_SET,
807 .flags = BAF_OPTIONAL | BAF_TRANSITIVE,
808 .export = bgp_export_ext_community,
809 .encode = bgp_encode_u32s,
810 .decode = bgp_decode_ext_community,
811 },
812 [BA_AS4_PATH] = {
813 .name = "as4_path",
814 .type = EAF_TYPE_AS_PATH,
815 .flags = BAF_OPTIONAL | BAF_TRANSITIVE,
816 .encode = bgp_encode_raw,
817 .decode = bgp_decode_as4_path,
818 },
819 [BA_AS4_AGGREGATOR] = {
820 .name = "as4_aggregator",
821 .type = EAF_TYPE_OPAQUE,
822 .flags = BAF_OPTIONAL | BAF_TRANSITIVE,
823 .encode = bgp_encode_raw,
824 .decode = bgp_decode_as4_aggregator,
825 .format = bgp_format_aggregator,
826 },
827 [BA_LARGE_COMMUNITY] = {
828 .name = "large_community",
829 .type = EAF_TYPE_LC_SET,
830 .flags = BAF_OPTIONAL | BAF_TRANSITIVE,
831 .export = bgp_export_large_community,
832 .encode = bgp_encode_u32s,
833 .decode = bgp_decode_large_community,
834 },
835 [BA_MPLS_LABEL_STACK] = {
836 .name = "mpls_label_stack",
837 .type = EAF_TYPE_INT_SET,
838 .export = bgp_export_mpls_label_stack,
839 .encode = bgp_encode_mpls_label_stack,
840 .decode = bgp_decode_mpls_label_stack,
841 .format = bgp_format_mpls_label_stack,
842 },
843 };
844
845 static inline int
846 bgp_attr_known(uint code)
847 {
848 return (code < ARRAY_SIZE(bgp_attr_table)) && bgp_attr_table[code].name;
849 }
850
851
852 /*
853 * Attribute export
854 */
855
856 static inline void
857 bgp_export_attr(struct bgp_export_state *s, eattr *a, ea_list *to)
858 {
859 if (EA_PROTO(a->id) != EAP_BGP)
860 return;
861
862 uint code = EA_ID(a->id);
863
864 if (bgp_attr_known(code))
865 {
866 const struct bgp_attr_desc *desc = &bgp_attr_table[code];
867
868 /* The flags might have been zero if the attr was added by filters */
869 a->flags = (a->flags & BAF_PARTIAL) | desc->flags;
870
871 /* Set partial bit if new opt-trans attribute is attached to non-local route */
872 if ((s->src != NULL) && (a->type & EAF_ORIGINATED) &&
873 (a->flags & BAF_OPTIONAL) && (a->flags & BAF_TRANSITIVE))
874 a->flags |= BAF_PARTIAL;
875
876 /* Call specific hook */
877 CALL(desc->export, s, a);
878
879 /* Attribute might become undefined in hook */
880 if ((a->type & EAF_TYPE_MASK) == EAF_TYPE_UNDEF)
881 return;
882 }
883 else
884 {
885 /* Don't re-export unknown non-transitive attributes */
886 if (!(a->flags & BAF_TRANSITIVE))
887 return;
888
889 a->flags |= BAF_PARTIAL;
890 }
891
892 /* Append updated attribute */
893 to->attrs[to->count++] = *a;
894 }
895
896 /**
897 * bgp_export_attrs - export BGP attributes
898 * @s: BGP export state
899 * @attrs: a list of extended attributes
900 *
901 * The bgp_export_attrs() function takes a list of attributes and merges it to
902 * one newly allocated and sorted segment. Attributes are validated and
903 * normalized by type-specific export hooks and attribute flags are updated.
904 * Some attributes may be eliminated (e.g. unknown non-tranitive attributes, or
905 * empty community sets).
906 *
907 * Result: one sorted attribute list segment, or NULL if attributes are unsuitable.
908 */
909 static inline ea_list *
910 bgp_export_attrs(struct bgp_export_state *s, ea_list *attrs)
911 {
912 /* Merge the attribute list */
913 ea_list *new = lp_alloc(s->pool, ea_scan(attrs));
914 ea_merge(attrs, new);
915 ea_sort(new);
916
917 uint i, count;
918 count = new->count;
919 new->count = 0;
920
921 /* Export each attribute */
922 for (i = 0; i < count; i++)
923 bgp_export_attr(s, &new->attrs[i], new);
924
925 if (s->err_withdraw)
926 return NULL;
927
928 return new;
929 }
930
931
932 /*
933 * Attribute encoding
934 */
935
936 static inline int
937 bgp_encode_attr(struct bgp_write_state *s, eattr *a, byte *buf, uint size)
938 {
939 ASSERT(EA_PROTO(a->id) == EAP_BGP);
940
941 uint code = EA_ID(a->id);
942
943 if (bgp_attr_known(code))
944 return bgp_attr_table[code].encode(s, a, buf, size);
945 else
946 return bgp_encode_raw(s, a, buf, size);
947 }
948
949 /**
950 * bgp_encode_attrs - encode BGP attributes
951 * @s: BGP write state
952 * @attrs: a list of extended attributes
953 * @buf: buffer
954 * @end: buffer end
955 *
956 * The bgp_encode_attrs() function takes a list of extended attributes
957 * and converts it to its BGP representation (a part of an Update message).
958 *
959 * Result: Length of the attribute block generated or -1 if not enough space.
960 */
961 int
962 bgp_encode_attrs(struct bgp_write_state *s, ea_list *attrs, byte *buf, byte *end)
963 {
964 byte *pos = buf;
965 int i, len;
966
967 for (i = 0; i < attrs->count; i++)
968 {
969 len = bgp_encode_attr(s, &attrs->attrs[i], pos, end - pos);
970
971 if (len < 0)
972 return -1;
973
974 pos += len;
975 }
976
977 return pos - buf;
978 }
979
980
981 /*
982 * Attribute decoding
983 */
984
985 static void bgp_process_as4_attrs(ea_list **attrs, struct linpool *pool);
986
987 static inline int
988 bgp_as_path_loopy(struct bgp_proto *p, ea_list *attrs, u32 asn)
989 {
990 eattr *e = bgp_find_attr(attrs, BA_AS_PATH);
991 int num = p->cf->allow_local_as + 1;
992 return (e && (num > 0) && as_path_contains(e->u.ptr, asn, num));
993 }
994
995 static inline int
996 bgp_originator_id_loopy(struct bgp_proto *p, ea_list *attrs)
997 {
998 eattr *e = bgp_find_attr(attrs, BA_ORIGINATOR_ID);
999 return (e && (e->u.data == p->local_id));
1000 }
1001
1002 static inline int
1003 bgp_cluster_list_loopy(struct bgp_proto *p, ea_list *attrs)
1004 {
1005 eattr *e = bgp_find_attr(attrs, BA_CLUSTER_LIST);
1006 return (e && int_set_contains(e->u.ptr, p->rr_cluster_id));
1007 }
1008
1009 static inline void
1010 bgp_decode_attr(struct bgp_parse_state *s, uint code, uint flags, byte *data, uint len, ea_list **to)
1011 {
1012 /* Handle duplicate attributes; RFC 7606 3 (g) */
1013 if (BIT32_TEST(s->attrs_seen, code))
1014 {
1015 if ((code == BA_MP_REACH_NLRI) || (code == BA_MP_UNREACH_NLRI))
1016 bgp_parse_error(s, 1);
1017 else
1018 DISCARD("Discarding duplicate attribute (code %u)", code);
1019 }
1020 BIT32_SET(s->attrs_seen, code);
1021
1022 if (bgp_attr_known(code))
1023 {
1024 const struct bgp_attr_desc *desc = &bgp_attr_table[code];
1025
1026 /* Handle conflicting flags; RFC 7606 3 (c) */
1027 if ((flags ^ desc->flags) & (BAF_OPTIONAL | BAF_TRANSITIVE))
1028 WITHDRAW("Malformed %s attribute - conflicting flags (%02x)", desc->name, flags);
1029
1030 desc->decode(s, code, flags, data, len, to);
1031 }
1032 else /* Unknown attribute */
1033 {
1034 if (!(flags & BAF_OPTIONAL))
1035 WITHDRAW("Unknown attribute (code %u) - conflicting flags (%02x)", code, flags);
1036
1037 bgp_decode_unknown(s, code, flags, data, len, to);
1038 }
1039 }
1040
1041 /**
1042 * bgp_decode_attrs - check and decode BGP attributes
1043 * @s: BGP parse state
1044 * @data: start of attribute block
1045 * @len: length of attribute block
1046 *
1047 * This function takes a BGP attribute block (a part of an Update message), checks
1048 * its consistency and converts it to a list of BIRD route attributes represented
1049 * by an (uncached) &rta.
1050 */
1051 ea_list *
1052 bgp_decode_attrs(struct bgp_parse_state *s, byte *data, uint len)
1053 {
1054 struct bgp_proto *p = s->proto;
1055 ea_list *attrs = NULL;
1056 uint code, flags, alen;
1057 byte *pos = data;
1058
1059 /* Parse the attributes */
1060 while (len)
1061 {
1062 alen = 0;
1063
1064 /* Read attribute type */
1065 if (len < 2)
1066 goto framing_error;
1067 flags = pos[0];
1068 code = pos[1];
1069 ADVANCE(pos, len, 2);
1070
1071 /* Read attribute length */
1072 if (flags & BAF_EXT_LEN)
1073 {
1074 if (len < 2)
1075 goto framing_error;
1076 alen = get_u16(pos);
1077 ADVANCE(pos, len, 2);
1078 }
1079 else
1080 {
1081 if (len < 1)
1082 goto framing_error;
1083 alen = *pos;
1084 ADVANCE(pos, len, 1);
1085 }
1086
1087 if (alen > len)
1088 goto framing_error;
1089
1090 DBG("Attr %02x %02x %u\n", code, flags, alen);
1091
1092 bgp_decode_attr(s, code, flags, pos, alen, &attrs);
1093 ADVANCE(pos, len, alen);
1094 }
1095
1096 if (s->err_withdraw)
1097 goto withdraw;
1098
1099 /* If there is no reachability NLRI, we are finished */
1100 if (!s->ip_reach_len && !s->mp_reach_len)
1101 return NULL;
1102
1103
1104 /* Handle missing mandatory attributes; RFC 7606 3 (d) */
1105 if (!BIT32_TEST(s->attrs_seen, BA_ORIGIN))
1106 { REPORT(NO_MANDATORY, "ORIGIN"); goto withdraw; }
1107
1108 if (!BIT32_TEST(s->attrs_seen, BA_AS_PATH))
1109 { REPORT(NO_MANDATORY, "AS_PATH"); goto withdraw; }
1110
1111 /* When receiving attributes from non-AS4-aware BGP speaker, we have to
1112 reconstruct AS_PATH and AGGREGATOR attributes; RFC 6793 4.2.3 */
1113 if (!p->as4_session)
1114 bgp_process_as4_attrs(&attrs, s->pool);
1115
1116 /* Reject routes with our ASN in AS_PATH attribute */
1117 if (bgp_as_path_loopy(p, attrs, p->local_as))
1118 goto withdraw;
1119
1120 /* Reject routes with our Confederation ID in AS_PATH attribute; RFC 5065 4.0 */
1121 if ((p->public_as != p->local_as) && bgp_as_path_loopy(p, attrs, p->public_as))
1122 goto withdraw;
1123
1124 /* Reject routes with our Router ID in ORIGINATOR_ID attribute; RFC 4456 8 */
1125 if (p->is_internal && bgp_originator_id_loopy(p, attrs))
1126 goto withdraw;
1127
1128 /* Reject routes with our Cluster ID in CLUSTER_LIST attribute; RFC 4456 8 */
1129 if (p->rr_client && bgp_cluster_list_loopy(p, attrs))
1130 goto withdraw;
1131
1132 /* If there is no local preference, define one */
1133 if (!BIT32_TEST(s->attrs_seen, BA_LOCAL_PREF))
1134 bgp_set_attr_u32(&attrs, s->pool, BA_LOCAL_PREF, 0, p->cf->default_local_pref);
1135
1136 return attrs;
1137
1138
1139 framing_error:
1140 /* RFC 7606 4 - handle attribute framing errors */
1141 REPORT("Malformed attribute list - framing error (%u/%u) at %d",
1142 alen, len, (int) (pos - s->attrs));
1143
1144 withdraw:
1145 /* RFC 7606 5.2 - handle missing NLRI during errors */
1146 if (!s->ip_reach_len && !s->mp_reach_len)
1147 bgp_parse_error(s, 1);
1148
1149 s->err_withdraw = 1;
1150 return NULL;
1151 }
1152
1153
1154 /*
1155 * Route bucket hash table
1156 */
1157
1158 #define RBH_KEY(b) b->eattrs, b->hash
1159 #define RBH_NEXT(b) b->next
1160 #define RBH_EQ(a1,h1,a2,h2) h1 == h2 && ea_same(a1, a2)
1161 #define RBH_FN(a,h) h
1162
1163 #define RBH_REHASH bgp_rbh_rehash
1164 #define RBH_PARAMS /8, *2, 2, 2, 8, 20
1165
1166
1167 HASH_DEFINE_REHASH_FN(RBH, struct bgp_bucket)
1168
1169 void
1170 bgp_init_bucket_table(struct bgp_channel *c)
1171 {
1172 HASH_INIT(c->bucket_hash, c->pool, 8);
1173
1174 init_list(&c->bucket_queue);
1175 c->withdraw_bucket = NULL;
1176 }
1177
1178 static struct bgp_bucket *
1179 bgp_get_bucket(struct bgp_channel *c, ea_list *new)
1180 {
1181 /* Hash and lookup */
1182 u32 hash = ea_hash(new);
1183 struct bgp_bucket *b = HASH_FIND(c->bucket_hash, RBH, new, hash);
1184
1185 if (b)
1186 return b;
1187
1188 uint ea_size = sizeof(ea_list) + new->count * sizeof(eattr);
1189 uint ea_size_aligned = BIRD_ALIGN(ea_size, CPU_STRUCT_ALIGN);
1190 uint size = sizeof(struct bgp_bucket) + ea_size_aligned;
1191 uint i;
1192 byte *dest;
1193
1194 /* Gather total size of non-inline attributes */
1195 for (i = 0; i < new->count; i++)
1196 {
1197 eattr *a = &new->attrs[i];
1198
1199 if (!(a->type & EAF_EMBEDDED))
1200 size += BIRD_ALIGN(sizeof(struct adata) + a->u.ptr->length, CPU_STRUCT_ALIGN);
1201 }
1202
1203 /* Create the bucket */
1204 b = mb_alloc(c->pool, size);
1205 init_list(&b->prefixes);
1206 b->hash = hash;
1207
1208 /* Copy list of extended attributes */
1209 memcpy(b->eattrs, new, ea_size);
1210 dest = ((byte *) b->eattrs) + ea_size_aligned;
1211
1212 /* Copy values of non-inline attributes */
1213 for (i = 0; i < new->count; i++)
1214 {
1215 eattr *a = &b->eattrs->attrs[i];
1216
1217 if (!(a->type & EAF_EMBEDDED))
1218 {
1219 struct adata *oa = a->u.ptr;
1220 struct adata *na = (struct adata *) dest;
1221 memcpy(na, oa, sizeof(struct adata) + oa->length);
1222 a->u.ptr = na;
1223 dest += BIRD_ALIGN(sizeof(struct adata) + na->length, CPU_STRUCT_ALIGN);
1224 }
1225 }
1226
1227 /* Insert the bucket to send queue and bucket hash */
1228 add_tail(&c->bucket_queue, &b->send_node);
1229 HASH_INSERT2(c->bucket_hash, RBH, c->pool, b);
1230
1231 return b;
1232 }
1233
1234 static struct bgp_bucket *
1235 bgp_get_withdraw_bucket(struct bgp_channel *c)
1236 {
1237 if (!c->withdraw_bucket)
1238 {
1239 c->withdraw_bucket = mb_allocz(c->pool, sizeof(struct bgp_bucket));
1240 init_list(&c->withdraw_bucket->prefixes);
1241 }
1242
1243 return c->withdraw_bucket;
1244 }
1245
1246 void
1247 bgp_free_bucket(struct bgp_channel *c, struct bgp_bucket *b)
1248 {
1249 rem_node(&b->send_node);
1250 HASH_REMOVE2(c->bucket_hash, RBH, c->pool, b);
1251 mb_free(b);
1252 }
1253
1254 void
1255 bgp_defer_bucket(struct bgp_channel *c, struct bgp_bucket *b)
1256 {
1257 rem_node(&b->send_node);
1258 add_tail(&c->bucket_queue, &b->send_node);
1259 }
1260
1261 void
1262 bgp_withdraw_bucket(struct bgp_channel *c, struct bgp_bucket *b)
1263 {
1264 struct bgp_proto *p = (void *) c->c.proto;
1265 struct bgp_bucket *wb = bgp_get_withdraw_bucket(c);
1266
1267 log(L_ERR "%s: Attribute list too long", p->p.name);
1268 while (!EMPTY_LIST(b->prefixes))
1269 {
1270 struct bgp_prefix *px = HEAD(b->prefixes);
1271
1272 log(L_ERR "%s: - withdrawing %N", p->p.name, &px->net);
1273 rem_node(&px->buck_node);
1274 add_tail(&wb->prefixes, &px->buck_node);
1275 }
1276 }
1277
1278
1279 /*
1280 * Prefix hash table
1281 */
1282
1283 #define PXH_KEY(px) px->net, px->path_id, px->hash
1284 #define PXH_NEXT(px) px->next
1285 #define PXH_EQ(n1,i1,h1,n2,i2,h2) h1 == h2 && i1 == i2 && net_equal(n1, n2)
1286 #define PXH_FN(n,i,h) h
1287
1288 #define PXH_REHASH bgp_pxh_rehash
1289 #define PXH_PARAMS /8, *2, 2, 2, 8, 20
1290
1291
1292 HASH_DEFINE_REHASH_FN(PXH, struct bgp_prefix)
1293
1294 void
1295 bgp_init_prefix_table(struct bgp_channel *c)
1296 {
1297 HASH_INIT(c->prefix_hash, c->pool, 8);
1298
1299 uint alen = net_addr_length[c->c.net_type];
1300 c->prefix_slab = alen ? sl_new(c->pool, sizeof(struct bgp_prefix) + alen) : NULL;
1301 }
1302
1303 void
1304 bgp_free_prefix_table(struct bgp_channel *c)
1305 {
1306 HASH_FREE(c->prefix_hash);
1307
1308 rfree(c->prefix_slab);
1309 c->prefix_slab = NULL;
1310 }
1311
1312 static struct bgp_prefix *
1313 bgp_get_prefix(struct bgp_channel *c, net_addr *net, u32 path_id)
1314 {
1315 u32 hash = net_hash(net) ^ u32_hash(path_id);
1316 struct bgp_prefix *px = HASH_FIND(c->prefix_hash, PXH, net, path_id, hash);
1317
1318 if (px)
1319 {
1320 rem_node(&px->buck_node);
1321 return px;
1322 }
1323
1324 if (c->prefix_slab)
1325 px = sl_alloc(c->prefix_slab);
1326 else
1327 px = mb_alloc(c->pool, sizeof(struct bgp_prefix) + net->length);
1328
1329 px->buck_node.next = NULL;
1330 px->buck_node.prev = NULL;
1331 px->hash = hash;
1332 px->path_id = path_id;
1333 net_copy(px->net, net);
1334
1335 HASH_INSERT2(c->prefix_hash, PXH, c->pool, px);
1336
1337 return px;
1338 }
1339
1340 void
1341 bgp_free_prefix(struct bgp_channel *c, struct bgp_prefix *px)
1342 {
1343 rem_node(&px->buck_node);
1344 HASH_REMOVE2(c->prefix_hash, PXH, c->pool, px);
1345
1346 if (c->prefix_slab)
1347 sl_free(c->prefix_slab, px);
1348 else
1349 mb_free(px);
1350 }
1351
1352
1353 /*
1354 * BGP protocol glue
1355 */
1356
1357 int
1358 bgp_import_control(struct proto *P, rte **new, ea_list **attrs UNUSED, struct linpool *pool UNUSED)
1359 {
1360 rte *e = *new;
1361 struct proto *SRC = e->attrs->src->proto;
1362 struct bgp_proto *p = (struct bgp_proto *) P;
1363 struct bgp_proto *src = (SRC->proto == &proto_bgp) ? (struct bgp_proto *) SRC : NULL;
1364
1365 /* Reject our routes */
1366 if (src == p)
1367 return -1;
1368
1369 /* Accept non-BGP routes */
1370 if (src == NULL)
1371 return 0;
1372
1373 // XXXX: Check next hop AF
1374
1375 /* IBGP route reflection, RFC 4456 */
1376 if (p->is_internal && src->is_internal && (p->local_as == src->local_as))
1377 {
1378 /* Rejected unless configured as route reflector */
1379 if (!p->rr_client && !src->rr_client)
1380 return -1;
1381
1382 /* Generally, this should be handled when path is received, but we check it
1383 also here as rr_cluster_id may be undefined or different in src. */
1384 if (p->rr_cluster_id && bgp_cluster_list_loopy(p, e->attrs->eattrs))
1385 return -1;
1386 }
1387
1388 /* Handle well-known communities, RFC 1997 */
1389 struct eattr *c;
1390 if (p->cf->interpret_communities &&
1391 (c = ea_find(e->attrs->eattrs, EA_CODE(EAP_BGP, BA_COMMUNITY))))
1392 {
1393 struct adata *d = c->u.ptr;
1394
1395 /* Do not export anywhere */
1396 if (int_set_contains(d, BGP_COMM_NO_ADVERTISE))
1397 return -1;
1398
1399 /* Do not export outside of AS (or member-AS) */
1400 if (!p->is_internal && int_set_contains(d, BGP_COMM_NO_EXPORT_SUBCONFED))
1401 return -1;
1402
1403 /* Do not export outside of AS (or confederation) */
1404 if (!p->is_interior && int_set_contains(d, BGP_COMM_NO_EXPORT))
1405 return -1;
1406 }
1407
1408 return 0;
1409 }
1410
1411
1412 static adata null_adata; /* adata of length 0 */
1413
1414 static ea_list *
1415 bgp_update_attrs(struct bgp_proto *p, struct bgp_channel *c, rte *e, ea_list *attrs0, struct linpool *pool)
1416 {
1417 struct proto *SRC = e->attrs->src->proto;
1418 struct bgp_proto *src = (SRC->proto == &proto_bgp) ? (void *) SRC : NULL;
1419 struct bgp_export_state s = { .proto = p, .channel = c, .pool = pool, .src = src, .route = e, .mpls = c->desc->mpls };
1420 ea_list *attrs = attrs0;
1421 eattr *a;
1422 adata *ad;
1423
1424 /* ORIGIN attribute - mandatory, attach if missing */
1425 if (! bgp_find_attr(attrs0, BA_ORIGIN))
1426 bgp_set_attr_u32(&attrs, pool, BA_ORIGIN, 0, src ? ORIGIN_INCOMPLETE : ORIGIN_IGP);
1427
1428 /* AS_PATH attribute - mandatory */
1429 a = bgp_find_attr(attrs0, BA_AS_PATH);
1430 ad = a ? a->u.ptr : &null_adata;
1431
1432 /* AS_PATH attribute - strip AS_CONFED* segments outside confederation */
1433 if ((!p->cf->confederation || !p->is_interior) && as_path_contains_confed(ad))
1434 ad = as_path_strip_confed(pool, ad);
1435
1436 /* AS_PATH attribute - keep or prepend ASN */
1437 if (p->is_internal ||
1438 (p->rs_client && src && src->rs_client))
1439 {
1440 /* IBGP or route server -> just ensure there is one */
1441 if (!a)
1442 bgp_set_attr_ptr(&attrs, pool, BA_AS_PATH, 0, &null_adata);
1443 }
1444 else if (p->is_interior)
1445 {
1446 /* Confederation -> prepend ASN as AS_CONFED_SEQUENCE */
1447 ad = as_path_prepend2(pool, ad, AS_PATH_CONFED_SEQUENCE, p->public_as);
1448 bgp_set_attr_ptr(&attrs, pool, BA_AS_PATH, 0, ad);
1449 }
1450 else /* Regular EBGP (no RS, no confederation) */
1451 {
1452 /* Regular EBGP -> prepend ASN as regular sequence */
1453 ad = as_path_prepend2(pool, ad, AS_PATH_SEQUENCE, p->public_as);
1454 bgp_set_attr_ptr(&attrs, pool, BA_AS_PATH, 0, ad);
1455
1456 /* MULTI_EXIT_DESC attribute - accept only if set in export filter */
1457 a = bgp_find_attr(attrs0, BA_MULTI_EXIT_DISC);
1458 if (a && !(a->type & EAF_FRESH))
1459 bgp_unset_attr(&attrs, pool, BA_MULTI_EXIT_DISC);
1460 }
1461
1462 /* NEXT_HOP attribute - delegated to AF-specific hook */
1463 a = bgp_find_attr(attrs0, BA_NEXT_HOP);
1464 bgp_update_next_hop(&s, a, &attrs);
1465
1466 /* LOCAL_PREF attribute - required for IBGP, attach if missing */
1467 if (p->is_interior && ! bgp_find_attr(attrs0, BA_LOCAL_PREF))
1468 bgp_set_attr_u32(&attrs, pool, BA_LOCAL_PREF, 0, p->cf->default_local_pref);
1469
1470 /* IBGP route reflection, RFC 4456 */
1471 if (src && src->is_internal && p->is_internal && (src->local_as == p->local_as))
1472 {
1473 /* ORIGINATOR_ID attribute - attach if not already set */
1474 if (! bgp_find_attr(attrs0, BA_ORIGINATOR_ID))
1475 bgp_set_attr_u32(&attrs, pool, BA_ORIGINATOR_ID, 0, src->remote_id);
1476
1477 /* CLUSTER_LIST attribute - prepend cluster ID */
1478 a = bgp_find_attr(attrs0, BA_CLUSTER_LIST);
1479 ad = a ? a->u.ptr : NULL;
1480
1481 /* Prepend src cluster ID */
1482 if (src->rr_cluster_id)
1483 ad = int_set_prepend(pool, ad, src->rr_cluster_id);
1484
1485 /* Prepend dst cluster ID if src and dst clusters are different */
1486 if (p->rr_cluster_id && (src->rr_cluster_id != p->rr_cluster_id))
1487 ad = int_set_prepend(pool, ad, p->rr_cluster_id);
1488
1489 /* Should be at least one prepended cluster ID */
1490 bgp_set_attr_ptr(&attrs, pool, BA_CLUSTER_LIST, 0, ad);
1491 }
1492
1493 /* AS4_* transition attributes, RFC 6793 4.2.2 */
1494 if (! p->as4_session)
1495 {
1496 a = bgp_find_attr(attrs, BA_AS_PATH);
1497 if (a && as_path_contains_as4(a->u.ptr))
1498 {
1499 bgp_set_attr_ptr(&attrs, pool, BA_AS_PATH, 0, as_path_to_old(pool, a->u.ptr));
1500 bgp_set_attr_ptr(&attrs, pool, BA_AS4_PATH, 0, as_path_strip_confed(pool, a->u.ptr));
1501 }
1502
1503 a = bgp_find_attr(attrs, BA_AGGREGATOR);
1504 if (a && aggregator_contains_as4(a->u.ptr))
1505 {
1506 bgp_set_attr_ptr(&attrs, pool, BA_AGGREGATOR, 0, aggregator_to_old(pool, a->u.ptr));
1507 bgp_set_attr_ptr(&attrs, pool, BA_AS4_AGGREGATOR, 0, a->u.ptr);
1508 }
1509 }
1510
1511 /*
1512 * Presence of mandatory attributes ORIGIN and AS_PATH is ensured by above
1513 * conditions. Presence and validity of quasi-mandatory NEXT_HOP attribute
1514 * should be checked in AF-specific hooks.
1515 */
1516
1517 /* Apply per-attribute export hooks for validatation and normalization */
1518 return bgp_export_attrs(&s, attrs);
1519 }
1520
1521 void
1522 bgp_rt_notify(struct proto *P, struct channel *C, net *n, rte *new, rte *old, ea_list *attrs)
1523 {
1524 struct bgp_proto *p = (void *) P;
1525 struct bgp_channel *c = (void *) C;
1526 struct bgp_bucket *buck;
1527 struct bgp_prefix *px;
1528 u32 path;
1529
1530 if (new)
1531 {
1532 attrs = bgp_update_attrs(p, c, new, attrs, bgp_linpool2);
1533
1534 /* If attributes are invalid, we fail back to withdraw */
1535 buck = attrs ? bgp_get_bucket(c, attrs) : bgp_get_withdraw_bucket(c);
1536 path = new->attrs->src->global_id;
1537
1538 lp_flush(bgp_linpool2);
1539 }
1540 else
1541 {
1542 buck = bgp_get_withdraw_bucket(c);
1543 path = old->attrs->src->global_id;
1544 }
1545
1546 px = bgp_get_prefix(c, n->n.addr, c->add_path_tx ? path : 0);
1547 add_tail(&buck->prefixes, &px->buck_node);
1548
1549 bgp_schedule_packet(p->conn, c, PKT_UPDATE);
1550 }
1551
1552
1553 static inline u32
1554 bgp_get_neighbor(rte *r)
1555 {
1556 eattr *e = ea_find(r->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1557 u32 as;
1558
1559 if (e && as_path_get_first_regular(e->u.ptr, &as))
1560 return as;
1561
1562 /* If AS_PATH is not defined, we treat rte as locally originated */
1563 struct bgp_proto *p = (void *) r->attrs->src->proto;
1564 return p->cf->confederation ?: p->local_as;
1565 }
1566
1567 static inline int
1568 rte_resolvable(rte *rt)
1569 {
1570 return rt->attrs->dest == RTD_UNICAST;
1571 }
1572
1573 int
1574 bgp_rte_better(rte *new, rte *old)
1575 {
1576 struct bgp_proto *new_bgp = (struct bgp_proto *) new->attrs->src->proto;
1577 struct bgp_proto *old_bgp = (struct bgp_proto *) old->attrs->src->proto;
1578 eattr *x, *y;
1579 u32 n, o;
1580
1581 /* Skip suppressed routes (see bgp_rte_recalculate()) */
1582 n = new->u.bgp.suppressed;
1583 o = old->u.bgp.suppressed;
1584 if (n > o)
1585 return 0;
1586 if (n < o)
1587 return 1;
1588
1589 /* RFC 4271 9.1.2.1. Route resolvability test */
1590 n = rte_resolvable(new);
1591 o = rte_resolvable(old);
1592 if (n > o)
1593 return 1;
1594 if (n < o)
1595 return 0;
1596
1597 /* Start with local preferences */
1598 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_LOCAL_PREF));
1599 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_LOCAL_PREF));
1600 n = x ? x->u.data : new_bgp->cf->default_local_pref;
1601 o = y ? y->u.data : old_bgp->cf->default_local_pref;
1602 if (n > o)
1603 return 1;
1604 if (n < o)
1605 return 0;
1606
1607 /* RFC 4271 9.1.2.2. a) Use AS path lengths */
1608 if (new_bgp->cf->compare_path_lengths || old_bgp->cf->compare_path_lengths)
1609 {
1610 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1611 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1612 n = x ? as_path_getlen(x->u.ptr) : AS_PATH_MAXLEN;
1613 o = y ? as_path_getlen(y->u.ptr) : AS_PATH_MAXLEN;
1614 if (n < o)
1615 return 1;
1616 if (n > o)
1617 return 0;
1618 }
1619
1620 /* RFC 4271 9.1.2.2. b) Use origins */
1621 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1622 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1623 n = x ? x->u.data : ORIGIN_INCOMPLETE;
1624 o = y ? y->u.data : ORIGIN_INCOMPLETE;
1625 if (n < o)
1626 return 1;
1627 if (n > o)
1628 return 0;
1629
1630 /* RFC 4271 9.1.2.2. c) Compare MED's */
1631 /* Proper RFC 4271 path selection cannot be interpreted as finding
1632 * the best path in some ordering. It is implemented partially in
1633 * bgp_rte_recalculate() when deterministic_med option is
1634 * active. Without that option, the behavior is just an
1635 * approximation, which in specific situations may lead to
1636 * persistent routing loops, because it is nondeterministic - it
1637 * depends on the order in which routes appeared. But it is also the
1638 * same behavior as used by default in Cisco routers, so it is
1639 * probably not a big issue.
1640 */
1641 if (new_bgp->cf->med_metric || old_bgp->cf->med_metric ||
1642 (bgp_get_neighbor(new) == bgp_get_neighbor(old)))
1643 {
1644 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
1645 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
1646 n = x ? x->u.data : new_bgp->cf->default_med;
1647 o = y ? y->u.data : old_bgp->cf->default_med;
1648 if (n < o)
1649 return 1;
1650 if (n > o)
1651 return 0;
1652 }
1653
1654 /* RFC 4271 9.1.2.2. d) Prefer external peers */
1655 if (new_bgp->is_interior > old_bgp->is_interior)
1656 return 0;
1657 if (new_bgp->is_interior < old_bgp->is_interior)
1658 return 1;
1659
1660 /* RFC 4271 9.1.2.2. e) Compare IGP metrics */
1661 n = new_bgp->cf->igp_metric ? new->attrs->igp_metric : 0;
1662 o = old_bgp->cf->igp_metric ? old->attrs->igp_metric : 0;
1663 if (n < o)
1664 return 1;
1665 if (n > o)
1666 return 0;
1667
1668 /* RFC 4271 9.1.2.2. f) Compare BGP identifiers */
1669 /* RFC 4456 9. a) Use ORIGINATOR_ID instead of local neighbor ID */
1670 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
1671 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGINATOR_ID));
1672 n = x ? x->u.data : new_bgp->remote_id;
1673 o = y ? y->u.data : old_bgp->remote_id;
1674
1675 /* RFC 5004 - prefer older routes */
1676 /* (if both are external and from different peer) */
1677 if ((new_bgp->cf->prefer_older || old_bgp->cf->prefer_older) &&
1678 !new_bgp->is_internal && n != o)
1679 return 0;
1680
1681 /* rest of RFC 4271 9.1.2.2. f) */
1682 if (n < o)
1683 return 1;
1684 if (n > o)
1685 return 0;
1686
1687 /* RFC 4456 9. b) Compare cluster list lengths */
1688 x = ea_find(new->attrs->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
1689 y = ea_find(old->attrs->eattrs, EA_CODE(EAP_BGP, BA_CLUSTER_LIST));
1690 n = x ? int_set_get_size(x->u.ptr) : 0;
1691 o = y ? int_set_get_size(y->u.ptr) : 0;
1692 if (n < o)
1693 return 1;
1694 if (n > o)
1695 return 0;
1696
1697 /* RFC 4271 9.1.2.2. g) Compare peer IP adresses */
1698 return (ipa_compare(new_bgp->cf->remote_ip, old_bgp->cf->remote_ip) < 0);
1699 }
1700
1701
1702 int
1703 bgp_rte_mergable(rte *pri, rte *sec)
1704 {
1705 struct bgp_proto *pri_bgp = (struct bgp_proto *) pri->attrs->src->proto;
1706 struct bgp_proto *sec_bgp = (struct bgp_proto *) sec->attrs->src->proto;
1707 eattr *x, *y;
1708 u32 p, s;
1709
1710 /* Skip suppressed routes (see bgp_rte_recalculate()) */
1711 if (pri->u.bgp.suppressed != sec->u.bgp.suppressed)
1712 return 0;
1713
1714 /* RFC 4271 9.1.2.1. Route resolvability test */
1715 if (!rte_resolvable(sec))
1716 return 0;
1717
1718 /* Start with local preferences */
1719 x = ea_find(pri->attrs->eattrs, EA_CODE(EAP_BGP, BA_LOCAL_PREF));
1720 y = ea_find(sec->attrs->eattrs, EA_CODE(EAP_BGP, BA_LOCAL_PREF));
1721 p = x ? x->u.data : pri_bgp->cf->default_local_pref;
1722 s = y ? y->u.data : sec_bgp->cf->default_local_pref;
1723 if (p != s)
1724 return 0;
1725
1726 /* RFC 4271 9.1.2.2. a) Use AS path lengths */
1727 if (pri_bgp->cf->compare_path_lengths || sec_bgp->cf->compare_path_lengths)
1728 {
1729 x = ea_find(pri->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1730 y = ea_find(sec->attrs->eattrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1731 p = x ? as_path_getlen(x->u.ptr) : AS_PATH_MAXLEN;
1732 s = y ? as_path_getlen(y->u.ptr) : AS_PATH_MAXLEN;
1733
1734 if (p != s)
1735 return 0;
1736
1737 // if (DELTA(p, s) > pri_bgp->cf->relax_multipath)
1738 // return 0;
1739 }
1740
1741 /* RFC 4271 9.1.2.2. b) Use origins */
1742 x = ea_find(pri->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1743 y = ea_find(sec->attrs->eattrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1744 p = x ? x->u.data : ORIGIN_INCOMPLETE;
1745 s = y ? y->u.data : ORIGIN_INCOMPLETE;
1746 if (p != s)
1747 return 0;
1748
1749 /* RFC 4271 9.1.2.2. c) Compare MED's */
1750 if (pri_bgp->cf->med_metric || sec_bgp->cf->med_metric ||
1751 (bgp_get_neighbor(pri) == bgp_get_neighbor(sec)))
1752 {
1753 x = ea_find(pri->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
1754 y = ea_find(sec->attrs->eattrs, EA_CODE(EAP_BGP, BA_MULTI_EXIT_DISC));
1755 p = x ? x->u.data : pri_bgp->cf->default_med;
1756 s = y ? y->u.data : sec_bgp->cf->default_med;
1757 if (p != s)
1758 return 0;
1759 }
1760
1761 /* RFC 4271 9.1.2.2. d) Prefer external peers */
1762 if (pri_bgp->is_interior != sec_bgp->is_interior)
1763 return 0;
1764
1765 /* RFC 4271 9.1.2.2. e) Compare IGP metrics */
1766 p = pri_bgp->cf->igp_metric ? pri->attrs->igp_metric : 0;
1767 s = sec_bgp->cf->igp_metric ? sec->attrs->igp_metric : 0;
1768 if (p != s)
1769 return 0;
1770
1771 /* Remaining criteria are ignored */
1772
1773 return 1;
1774 }
1775
1776
1777 static inline int
1778 same_group(rte *r, u32 lpref, u32 lasn)
1779 {
1780 return (r->pref == lpref) && (bgp_get_neighbor(r) == lasn);
1781 }
1782
1783 static inline int
1784 use_deterministic_med(rte *r)
1785 {
1786 struct proto *P = r->attrs->src->proto;
1787 return (P->proto == &proto_bgp) && ((struct bgp_proto *) P)->cf->deterministic_med;
1788 }
1789
1790 int
1791 bgp_rte_recalculate(rtable *table, net *net, rte *new, rte *old, rte *old_best)
1792 {
1793 rte *r, *s;
1794 rte *key = new ? new : old;
1795 u32 lpref = key->pref;
1796 u32 lasn = bgp_get_neighbor(key);
1797 int old_is_group_best = 0;
1798
1799 /*
1800 * Proper RFC 4271 path selection is a bit complicated, it cannot be
1801 * implemented just by rte_better(), because it is not a linear
1802 * ordering. But it can be splitted to two levels, where the lower
1803 * level chooses the best routes in each group of routes from the
1804 * same neighboring AS and higher level chooses the best route (with
1805 * a slightly different ordering) between the best-in-group routes.
1806 *
1807 * When deterministic_med is disabled, we just ignore this issue and
1808 * choose the best route by bgp_rte_better() alone. If enabled, the
1809 * lower level of the route selection is done here (for the group
1810 * to which the changed route belongs), all routes in group are
1811 * marked as suppressed, just chosen best-in-group is not.
1812 *
1813 * Global best route selection then implements higher level by
1814 * choosing between non-suppressed routes (as they are always
1815 * preferred over suppressed routes). Routes from BGP protocols
1816 * that do not set deterministic_med are just never suppressed. As
1817 * they do not participate in the lower level selection, it is OK
1818 * that this fn is not called for them.
1819 *
1820 * The idea is simple, the implementation is more problematic,
1821 * mostly because of optimizations in rte_recalculate() that
1822 * avoids full recalculation in most cases.
1823 *
1824 * We can assume that at least one of new, old is non-NULL and both
1825 * are from the same protocol with enabled deterministic_med. We
1826 * group routes by both neighbor AS (lasn) and preference (lpref),
1827 * because bgp_rte_better() does not handle preference itself.
1828 */
1829
1830 /* If new and old are from different groups, we just process that
1831 as two independent events */
1832 if (new && old && !same_group(old, lpref, lasn))
1833 {
1834 int i1, i2;
1835 i1 = bgp_rte_recalculate(table, net, NULL, old, old_best);
1836 i2 = bgp_rte_recalculate(table, net, new, NULL, old_best);
1837 return i1 || i2;
1838 }
1839
1840 /*
1841 * We could find the best-in-group and then make some shortcuts like
1842 * in rte_recalculate, but as we would have to walk through all
1843 * net->routes just to find it, it is probably not worth. So we
1844 * just have two simpler fast cases that use just the old route.
1845 * We also set suppressed flag to avoid using it in bgp_rte_better().
1846 */
1847
1848 if (new)
1849 new->u.bgp.suppressed = 1;
1850
1851 if (old)
1852 {
1853 old_is_group_best = !old->u.bgp.suppressed;
1854 old->u.bgp.suppressed = 1;
1855 int new_is_better = new && bgp_rte_better(new, old);
1856
1857 /* The first case - replace not best with worse (or remove not best) */
1858 if (!old_is_group_best && !new_is_better)
1859 return 0;
1860
1861 /* The second case - replace the best with better */
1862 if (old_is_group_best && new_is_better)
1863 {
1864 /* new is best-in-group, the see discussion below - this is
1865 a special variant of NBG && OBG. From OBG we can deduce
1866 that same_group(old_best) iff (old == old_best) */
1867 new->u.bgp.suppressed = 0;
1868 return (old == old_best);
1869 }
1870 }
1871
1872 /* The default case - find a new best-in-group route */
1873 r = new; /* new may not be in the list */
1874 for (s=net->routes; rte_is_valid(s); s=s->next)
1875 if (use_deterministic_med(s) && same_group(s, lpref, lasn))
1876 {
1877 s->u.bgp.suppressed = 1;
1878 if (!r || bgp_rte_better(s, r))
1879 r = s;
1880 }
1881
1882 /* Simple case - the last route in group disappears */
1883 if (!r)
1884 return 0;
1885
1886 /* Found best-in-group */
1887 r->u.bgp.suppressed = 0;
1888
1889 /*
1890 * There are generally two reasons why we have to force
1891 * recalculation (return 1): First, the new route may be wrongfully
1892 * chosen to be the best in the first case check in
1893 * rte_recalculate(), this may happen only if old_best is from the
1894 * same group. Second, another (different than new route)
1895 * best-in-group is chosen and that may be the proper best (although
1896 * rte_recalculate() without ignore that possibility).
1897 *
1898 * There are three possible cases according to whether the old route
1899 * was the best in group (OBG, stored in old_is_group_best) and
1900 * whether the new route is the best in group (NBG, tested by r == new).
1901 * These cases work even if old or new is NULL.
1902 *
1903 * NBG -> new is a possible candidate for the best route, so we just
1904 * check for the first reason using same_group().
1905 *
1906 * !NBG && OBG -> Second reason applies, return 1
1907 *
1908 * !NBG && !OBG -> Best in group does not change, old != old_best,
1909 * rte_better(new, old_best) is false and therefore
1910 * the first reason does not apply, return 0
1911 */
1912
1913 if (r == new)
1914 return old_best && same_group(old_best, lpref, lasn);
1915 else
1916 return old_is_group_best;
1917 }
1918
1919
1920 /*
1921 * Reconstruct AS_PATH and AGGREGATOR according to RFC 6793 4.2.3
1922 */
1923 static void
1924 bgp_process_as4_attrs(ea_list **attrs, struct linpool *pool)
1925 {
1926 eattr *p2 = bgp_find_attr(*attrs, BA_AS_PATH);
1927 eattr *p4 = bgp_find_attr(*attrs, BA_AS4_PATH);
1928 eattr *a2 = bgp_find_attr(*attrs, BA_AGGREGATOR);
1929 eattr *a4 = bgp_find_attr(*attrs, BA_AS4_AGGREGATOR);
1930
1931 /* First, unset AS4_* attributes */
1932 if (p4) bgp_unset_attr(attrs, pool, BA_AS4_PATH);
1933 if (a4) bgp_unset_attr(attrs, pool, BA_AS4_AGGREGATOR);
1934
1935 /* Handle AGGREGATOR attribute */
1936 if (a2 && a4)
1937 {
1938 u32 a2_asn = get_u32(a2->u.ptr->data);
1939
1940 /* If routes were aggregated by an old router, then AS4_PATH and
1941 AS4_AGGREGATOR are invalid. In that case we give up. */
1942 if (a2_asn != AS_TRANS)
1943 return;
1944
1945 /* Use AS4_AGGREGATOR instead of AGGREGATOR */
1946 a2->u.ptr = a4->u.ptr;
1947 }
1948
1949 /* Handle AS_PATH attribute */
1950 if (p2 && p4)
1951 {
1952 /* Both as_path_getlen() and as_path_cut() take AS_CONFED* as zero length */
1953 int p2_len = as_path_getlen(p2->u.ptr);
1954 int p4_len = as_path_getlen(p4->u.ptr);
1955
1956 /* AS_PATH is too short, give up */
1957 if (p2_len < p4_len)
1958 return;
1959
1960 /* Merge AS_PATH and AS4_PATH */
1961 as_path_cut(p2->u.ptr, p2_len - p4_len);
1962 p2->u.ptr = as_path_merge(pool, p2->u.ptr, p4->u.ptr);
1963 }
1964 }
1965
1966 int
1967 bgp_get_attr(eattr *a, byte *buf, int buflen)
1968 {
1969 uint i = EA_ID(a->id);
1970 const struct bgp_attr_desc *d;
1971 int len;
1972
1973 if (bgp_attr_known(i))
1974 {
1975 d = &bgp_attr_table[i];
1976 len = bsprintf(buf, "%s", d->name);
1977 buf += len;
1978 if (d->format)
1979 {
1980 *buf++ = ':';
1981 *buf++ = ' ';
1982 d->format(a, buf, buflen - len - 2);
1983 return GA_FULL;
1984 }
1985 return GA_NAME;
1986 }
1987
1988 bsprintf(buf, "%02x%s", i, (a->flags & BAF_TRANSITIVE) ? " [t]" : "");
1989 return GA_NAME;
1990 }
1991
1992 void
1993 bgp_get_route_info(rte *e, byte *buf, ea_list *attrs)
1994 {
1995 eattr *p = ea_find(attrs, EA_CODE(EAP_BGP, BA_AS_PATH));
1996 eattr *o = ea_find(attrs, EA_CODE(EAP_BGP, BA_ORIGIN));
1997 u32 origas;
1998
1999 buf += bsprintf(buf, " (%d", e->pref);
2000
2001 if (e->u.bgp.suppressed)
2002 buf += bsprintf(buf, "-");
2003
2004 if (e->attrs->hostentry)
2005 {
2006 if (!rte_resolvable(e))
2007 buf += bsprintf(buf, "/-");
2008 else if (e->attrs->igp_metric >= IGP_METRIC_UNKNOWN)
2009 buf += bsprintf(buf, "/?");
2010 else
2011 buf += bsprintf(buf, "/%d", e->attrs->igp_metric);
2012 }
2013 buf += bsprintf(buf, ") [");
2014
2015 if (p && as_path_get_last(p->u.ptr, &origas))
2016 buf += bsprintf(buf, "AS%u", origas);
2017 if (o)
2018 buf += bsprintf(buf, "%c", "ie?"[o->u.data]);
2019 strcpy(buf, "]");
2020 }