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