-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathattrs.c
2508 lines (2057 loc) · 67.1 KB
/
attrs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* BIRD -- BGP Attributes
*
* (c) 2000 Martin Mares <mj@ucw.cz>
* (c) 2008--2016 Ondrej Zajicek <santiago@crfreenet.org>
* (c) 2008--2016 CZ.NIC z.s.p.o.
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
#undef LOCAL_DEBUG
#include <stdlib.h>
#include "nest/bird.h"
#include "nest/iface.h"
#include "nest/protocol.h"
#include "nest/route.h"
#include "nest/attrs.h"
#include "nest/mpls.h"
#include "conf/conf.h"
#include "lib/resource.h"
#include "lib/string.h"
#include "lib/unaligned.h"
#include "bgp.h"
/*
* UPDATE message error handling
*
* All checks from RFC 4271 6.3 are done as specified with these exceptions:
* - The semantic check of an IP address from NEXT_HOP attribute is missing.
* - Checks of some optional attribute values are missing.
* - Syntactic and semantic checks of NLRIs (done in DECODE_PREFIX())
* are probably inadequate.
*
* Loop detection based on AS_PATH causes updates to be withdrawn. RFC
* 4271 does not explicitly specify the behavior in that case.
*
* Loop detection related to route reflection (based on ORIGINATOR_ID
* and CLUSTER_LIST) causes updates to be withdrawn. RFC 4456 8
* specifies that such updates should be ignored, but that is generally
* a bad idea.
*
* BGP attribute table has several hooks:
*
* export - Hook that validates and normalizes attribute during export phase.
* Receives eattr, may modify it (e.g., sort community lists for canonical
* representation), UNSET() it (e.g., skip empty lists), or REJECT() the route
* if necessary. May assume that eattr has value valid w.r.t. its type, but may
* be invalid w.r.t. BGP constraints. Optional.
*
* encode - Hook that converts internal representation to external one during
* packet writing. Receives eattr and puts it in the buffer (including attribute
* header). Returns number of bytes, or -1 if not enough space. May assume that
* eattr has value valid w.r.t. its type and validated by export hook. Mandatory
* for all known attributes that exist internally after export phase (i.e., all
* except pseudoattributes MP_(UN)REACH_NLRI).
*
* decode - Hook that converts external representation to internal one during
* packet parsing. Receives attribute data in buffer, validates it and adds
* attribute to ea_list. If data are invalid, steps DISCARD(), WITHDRAW() or
* bgp_parse_error() may be used to escape. Mandatory for all known attributes.
*
* format - Optional hook that converts eattr to textual representation.
*/
struct bgp_attr_desc {
const char *name;
uint type;
uint flags;
void (*export)(struct bgp_export_state *s, eattr *a);
int (*encode)(struct bgp_write_state *s, eattr *a, byte *buf, uint size);
void (*decode)(struct bgp_parse_state *s, uint code, uint flags, byte *data, uint len, ea_list **to);
void (*format)(const eattr *ea, byte *buf, uint size);
};
static const struct bgp_attr_desc bgp_attr_table[];
static inline int bgp_attr_known(uint code);
eattr *
bgp_set_attr(ea_list **attrs, struct linpool *pool, uint code, uint flags, uintptr_t val)
{
ASSERT(bgp_attr_known(code));
return ea_set_attr(
attrs,
pool,
EA_CODE(PROTOCOL_BGP, code),
bgp_attr_table[code].flags | (flags & BAF_PARTIAL),
bgp_attr_table[code].type,
val
);
}
#define REPORT(msg, args...) \
({ log(L_REMOTE "%s: " msg, s->proto->p.name, ## args); })
#define DISCARD(msg, args...) \
({ REPORT(msg, ## args); return; })
#define WITHDRAW(msg, args...) \
({ REPORT(msg, ## args); s->err_withdraw = 1; return; })
#define UNSET(a) \
({ a->undef = 1; return; })
#define REJECT(msg, args...) \
({ log(L_ERR "%s: " msg, s->proto->p.name, ## args); s->err_reject = 1; return; })
#define NEW_BGP "Discarding %s attribute received from AS4-aware neighbor"
#define BAD_EBGP "Discarding %s attribute received from EBGP neighbor"
#define BAD_LENGTH "Malformed %s attribute - invalid length (%u)"
#define BAD_VALUE "Malformed %s attribute - invalid value (%u)"
#define NO_MANDATORY "Missing mandatory %s attribute"
static inline int
bgp_put_attr_hdr3(byte *buf, uint code, uint flags, uint len)
{
*buf++ = flags & ~BAF_EXT_LEN;
*buf++ = code;
*buf++ = len;
return 3;
}
static inline int
bgp_put_attr_hdr4(byte *buf, uint code, uint flags, uint len)
{
*buf++ = flags | BAF_EXT_LEN;
*buf++ = code;
put_u16(buf, len);
return 4;
}
static inline int
bgp_put_attr_hdr(byte *buf, uint code, uint flags, uint len)
{
if (len < 256)
return bgp_put_attr_hdr3(buf, code, flags, len);
else
return bgp_put_attr_hdr4(buf, code, flags, len);
}
static int
bgp_encode_u8(struct bgp_write_state *s UNUSED, eattr *a, byte *buf, uint size)
{
if (size < (3+1))
return -1;
bgp_put_attr_hdr3(buf, EA_ID(a->id), a->flags, 1);
buf[3] = a->u.data;
return 3+1;
}
static int
bgp_encode_u32(struct bgp_write_state *s UNUSED, eattr *a, byte *buf, uint size)
{
if (size < (3+4))
return -1;
bgp_put_attr_hdr3(buf, EA_ID(a->id), a->flags, 4);
put_u32(buf+3, a->u.data);
return 3+4;
}
static int
bgp_encode_u32s(struct bgp_write_state *s UNUSED, eattr *a, byte *buf, uint size)
{
uint len = a->u.ptr->length;
if (size < (4+len))
return -1;
uint hdr = bgp_put_attr_hdr(buf, EA_ID(a->id), a->flags, len);
put_u32s(buf + hdr, (u32 *) a->u.ptr->data, len / 4);
return hdr + len;
}
static int
bgp_put_attr(byte *buf, uint size, uint code, uint flags, const byte *data, uint len)
{
if (size < (4+len))
return -1;
uint hdr = bgp_put_attr_hdr(buf, code, flags, len);
memcpy(buf + hdr, data, len);
return hdr + len;
}
static int
bgp_encode_raw(struct bgp_write_state *s UNUSED, eattr *a, byte *buf, uint size)
{
return bgp_put_attr(buf, size, EA_ID(a->id), a->flags, a->u.ptr->data, a->u.ptr->length);
}
/*
* AIGP handling
*/
static int
bgp_aigp_valid(byte *data, uint len, char *err, uint elen)
{
byte *pos = data;
char *err_dsc = NULL;
uint err_val = 0;
#define BAD(DSC,VAL) ({ err_dsc = DSC; err_val = VAL; goto bad; })
while (len)
{
if (len < 3)
BAD("TLV framing error", len);
/* Process one TLV */
uint ptype = pos[0];
uint plen = get_u16(pos + 1);
if (len < plen)
BAD("TLV framing error", plen);
if (plen < 3)
BAD("Bad TLV length", plen);
if ((ptype == BGP_AIGP_METRIC) && (plen != 11))
BAD("Bad AIGP TLV length", plen);
ADVANCE(pos, len, plen);
}
#undef BAD
return 1;
bad:
if (err)
if (bsnprintf(err, elen, "%s (%u) at %d", err_dsc, err_val, (int) (pos - data)) < 0)
err[0] = 0;
return 0;
}
static const byte *
bgp_aigp_get_tlv(const struct adata *ad, uint type)
{
if (!ad)
return NULL;
uint len = ad->length;
const byte *pos = ad->data;
while (len)
{
uint ptype = pos[0];
uint plen = get_u16(pos + 1);
if (ptype == type)
return pos;
ADVANCE(pos, len, plen);
}
return NULL;
}
static const struct adata *
bgp_aigp_set_tlv(struct linpool *pool, const struct adata *ad, uint type, byte *data, uint dlen)
{
uint len = ad ? ad->length : 0;
const byte *pos = ad ? ad->data : NULL;
struct adata *res = lp_alloc_adata(pool, len + 3 + dlen);
byte *dst = res->data;
byte *tlv = NULL;
int del = 0;
while (len)
{
uint ptype = pos[0];
uint plen = get_u16(pos + 1);
/* Find position for new TLV */
if ((ptype >= type) && !tlv)
{
tlv = dst;
dst += 3 + dlen;
}
/* Skip first matching TLV, copy others */
if ((ptype == type) && !del)
del = 1;
else
{
memcpy(dst, pos, plen);
dst += plen;
}
ADVANCE(pos, len, plen);
}
if (!tlv)
{
tlv = dst;
dst += 3 + dlen;
}
/* Store the TLD */
put_u8(tlv + 0, type);
put_u16(tlv + 1, 3 + dlen);
memcpy(tlv + 3, data, dlen);
/* Update length */
res->length = dst - res->data;
return res;
}
static u64 UNUSED
bgp_aigp_get_metric(const struct adata *ad, u64 def)
{
const byte *b = bgp_aigp_get_tlv(ad, BGP_AIGP_METRIC);
return b ? get_u64(b + 3) : def;
}
static const struct adata *
bgp_aigp_set_metric(struct linpool *pool, const struct adata *ad, u64 metric)
{
byte data[8];
put_u64(data, metric);
return bgp_aigp_set_tlv(pool, ad, BGP_AIGP_METRIC, data, 8);
}
int
bgp_total_aigp_metric_(rte *e, u64 *metric, const struct adata **ad)
{
eattr *a = ea_find(e->attrs->eattrs, EA_CODE(PROTOCOL_BGP, BA_AIGP));
if (!a)
return 0;
const byte *b = bgp_aigp_get_tlv(a->u.ptr, BGP_AIGP_METRIC);
if (!b)
return 0;
u64 aigp = get_u64(b + 3);
u64 step = e->attrs->igp_metric;
if (!rte_resolvable(e) || (step >= IGP_METRIC_UNKNOWN))
step = BGP_AIGP_MAX;
if (!step)
step = 1;
*ad = a->u.ptr;
*metric = aigp + step;
if (*metric < aigp)
*metric = BGP_AIGP_MAX;
return 1;
}
static inline int
bgp_init_aigp_metric(rte *e, u64 *metric, const struct adata **ad)
{
if (e->attrs->source == RTS_BGP)
return 0;
*metric = rt_get_igp_metric(e);
*ad = NULL;
return *metric < IGP_METRIC_UNKNOWN;
}
u32
bgp_rte_igp_metric(struct rte *rt)
{
u64 metric = bgp_total_aigp_metric(rt);
return (u32) MIN(metric, (u64) IGP_METRIC_UNKNOWN);
}
/*
* Attribute hooks
*/
static void
bgp_export_origin(struct bgp_export_state *s, eattr *a)
{
if (a->u.data > 2)
REJECT(BAD_VALUE, "ORIGIN", a->u.data);
}
static void
bgp_decode_origin(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
{
if (len != 1)
WITHDRAW(BAD_LENGTH, "ORIGIN", len);
if (data[0] > 2)
WITHDRAW(BAD_VALUE, "ORIGIN", data[0]);
bgp_set_attr_u32(to, s->pool, BA_ORIGIN, flags, data[0]);
}
static void
bgp_format_origin(const eattr *a, byte *buf, uint size UNUSED)
{
static const char *bgp_origin_names[] = { "IGP", "EGP", "Incomplete" };
bsprintf(buf, (a->u.data <= 2) ? bgp_origin_names[a->u.data] : "?");
}
static inline int
bgp_as_path_first_as_equal(const byte *data, uint len, u32 asn)
{
return (len >= 6) &&
((data[0] == AS_PATH_SEQUENCE) || (data[0] == AS_PATH_CONFED_SEQUENCE)) &&
(data[1] > 0) &&
(get_u32(data+2) == asn);
}
static int
bgp_encode_as_path(struct bgp_write_state *s, eattr *a, byte *buf, uint size)
{
const byte *data = a->u.ptr->data;
uint len = a->u.ptr->length;
if (!s->as4_session)
{
/* Prepare 16-bit AS_PATH (from 32-bit one) in a temporary buffer */
byte *dst = alloca(len);
len = as_path_32to16(dst, data, len);
data = dst;
}
return bgp_put_attr(buf, size, BA_AS_PATH, a->flags, data, len);
}
static void
bgp_decode_as_path(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
{
struct bgp_proto *p = s->proto;
int as_length = s->as4_session ? 4 : 2;
int as_sets = p->cf->allow_as_sets;
int as_confed = p->cf->confederation && p->is_interior;
char err[128];
if (!as_path_valid(data, len, as_length, as_sets, as_confed, err, sizeof(err)))
WITHDRAW("Malformed AS_PATH attribute - %s", err);
if (!s->as4_session)
{
/* Prepare 32-bit AS_PATH (from 16-bit one) in a temporary buffer */
byte *src = data;
data = alloca(2*len);
len = as_path_16to32(data, src, len);
}
/* In some circumstances check for initial AS_CONFED_SEQUENCE; RFC 5065 5.0 */
if (p->is_interior && !p->is_internal &&
((len < 2) || (data[0] != AS_PATH_CONFED_SEQUENCE)))
WITHDRAW("Malformed AS_PATH attribute - %s", "missing initial AS_CONFED_SEQUENCE");
/* Reject routes with first AS in AS_PATH not matching neighbor AS; RFC 4271 6.3 */
if (!p->is_internal && p->cf->enforce_first_as &&
!bgp_as_path_first_as_equal(data, len, p->remote_as))
WITHDRAW("Malformed AS_PATH attribute - %s", "First AS differs from neigbor AS");
bgp_set_attr_data(to, s->pool, BA_AS_PATH, flags, data, len);
}
static int
bgp_encode_next_hop(struct bgp_write_state *s, eattr *a, byte *buf, uint size)
{
/*
* The NEXT_HOP attribute is used only in traditional (IPv4) BGP. In MP-BGP,
* the next hop is encoded as a part of the MP_REACH_NLRI attribute, so we
* store it and encode it later by AFI-specific hooks.
*/
if (!s->mp_reach)
{
// ASSERT(a->u.ptr->length == sizeof(ip_addr));
/* FIXME: skip IPv6 next hops for IPv4 routes during MRT dump */
ip_addr *addr = (void *) a->u.ptr->data;
if ((a->u.ptr->length != sizeof(ip_addr)) || !ipa_is_ip4(*addr))
return 0;
if (size < (3+4))
return -1;
bgp_put_attr_hdr3(buf, BA_NEXT_HOP, a->flags, 4);
put_ip4(buf+3, ipa_to_ip4(*addr));
return 3+4;
}
else
{
s->mp_next_hop = a;
return 0;
}
}
static void
bgp_decode_next_hop(struct bgp_parse_state *s, uint code UNUSED, uint flags UNUSED, byte *data, uint len, ea_list **to UNUSED)
{
if (len != 4)
WITHDRAW(BAD_LENGTH, "NEXT_HOP", len);
/* Semantic checks are done later */
s->ip_next_hop_len = len;
s->ip_next_hop_data = data;
}
/* TODO: This function should use AF-specific hook */
static void
bgp_format_next_hop(const eattr *a, byte *buf, uint size UNUSED)
{
ip_addr *nh = (void *) a->u.ptr->data;
uint len = a->u.ptr->length;
ASSERT((len == 16) || (len == 32));
/* in IPv6, we may have two addresses in NEXT HOP */
if ((len == 16) || ipa_zero(nh[1]))
bsprintf(buf, "%I", nh[0]);
else
bsprintf(buf, "%I %I", nh[0], nh[1]);
}
static void
bgp_decode_med(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
{
if (len != 4)
WITHDRAW(BAD_LENGTH, "MULTI_EXIT_DISC", len);
u32 val = get_u32(data);
bgp_set_attr_u32(to, s->pool, BA_MULTI_EXIT_DISC, flags, val);
}
static void
bgp_export_local_pref(struct bgp_export_state *s, eattr *a)
{
if (!s->proto->is_interior && !s->proto->cf->allow_local_pref)
UNSET(a);
}
static void
bgp_decode_local_pref(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
{
if (!s->proto->is_interior && !s->proto->cf->allow_local_pref)
DISCARD(BAD_EBGP, "LOCAL_PREF");
if (len != 4)
WITHDRAW(BAD_LENGTH, "LOCAL_PREF", len);
u32 val = get_u32(data);
bgp_set_attr_u32(to, s->pool, BA_LOCAL_PREF, flags, val);
}
static void
bgp_decode_atomic_aggr(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data UNUSED, uint len, ea_list **to)
{
if (len != 0)
DISCARD(BAD_LENGTH, "ATOMIC_AGGR", len);
bgp_set_attr_data(to, s->pool, BA_ATOMIC_AGGR, flags, NULL, 0);
}
static int
bgp_encode_aggregator(struct bgp_write_state *s, eattr *a, byte *buf, uint size)
{
const byte *data = a->u.ptr->data;
uint len = a->u.ptr->length;
if (!s->as4_session)
{
/* Prepare 16-bit AGGREGATOR (from 32-bit one) in a temporary buffer */
byte *dst = alloca(6);
len = aggregator_32to16(dst, data);
data = dst;
}
return bgp_put_attr(buf, size, BA_AGGREGATOR, a->flags, data, len);
}
static void
bgp_decode_aggregator(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
{
if (len != (s->as4_session ? 8 : 6))
DISCARD(BAD_LENGTH, "AGGREGATOR", len);
if (!s->as4_session)
{
/* Prepare 32-bit AGGREGATOR (from 16-bit one) in a temporary buffer */
byte *src = data;
data = alloca(8);
len = aggregator_16to32(data, src);
}
bgp_set_attr_data(to, s->pool, BA_AGGREGATOR, flags, data, len);
}
static void
bgp_format_aggregator(const eattr *a, byte *buf, uint size UNUSED)
{
const byte *data = a->u.ptr->data;
bsprintf(buf, "%I4 AS%u", get_ip4(data+4), get_u32(data+0));
}
static void
bgp_export_community(struct bgp_export_state *s, eattr *a)
{
if (a->u.ptr->length == 0)
UNSET(a);
a->u.ptr = int_set_sort(s->pool, a->u.ptr);
}
static void
bgp_decode_community(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
{
if (!len || (len % 4))
WITHDRAW(BAD_LENGTH, "COMMUNITY", len);
struct adata *ad = lp_alloc_adata(s->pool, len);
get_u32s(data, (u32 *) ad->data, len / 4);
bgp_set_attr_ptr(to, s->pool, BA_COMMUNITY, flags, ad);
}
static void
bgp_export_originator_id(struct bgp_export_state *s, eattr *a)
{
if (!s->proto->is_internal)
UNSET(a);
}
static void
bgp_decode_originator_id(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
{
if (!s->proto->is_internal)
DISCARD(BAD_EBGP, "ORIGINATOR_ID");
if (len != 4)
WITHDRAW(BAD_LENGTH, "ORIGINATOR_ID", len);
u32 val = get_u32(data);
bgp_set_attr_u32(to, s->pool, BA_ORIGINATOR_ID, flags, val);
}
static void
bgp_export_cluster_list(struct bgp_export_state *s UNUSED, eattr *a)
{
if (!s->proto->is_internal)
UNSET(a);
if (a->u.ptr->length == 0)
UNSET(a);
}
static void
bgp_decode_cluster_list(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
{
if (!s->proto->is_internal)
DISCARD(BAD_EBGP, "CLUSTER_LIST");
if (!len || (len % 4))
WITHDRAW(BAD_LENGTH, "CLUSTER_LIST", len);
struct adata *ad = lp_alloc_adata(s->pool, len);
get_u32s(data, (u32 *) ad->data, len / 4);
bgp_set_attr_ptr(to, s->pool, BA_CLUSTER_LIST, flags, ad);
}
static void
bgp_format_cluster_list(const eattr *a, byte *buf, uint size)
{
/* Truncates cluster lists larger than buflen, probably not a problem */
int_set_format(a->u.ptr, ISF_ROUTER_ID, -1, buf, size);
}
int
bgp_encode_mp_reach_mrt(struct bgp_write_state *s UNUSED, eattr *a, byte *buf, uint size)
{
/*
* Limited version of MP_REACH_NLRI used for MRT table dumps (IPv6 only):
*
* 3 B MP_REACH_NLRI header
* 1 B MP_REACH_NLRI data - Length of Next Hop Network Address
* var MP_REACH_NLRI data - Network Address of Next Hop
*/
ip_addr *nh = (void *) a->u.ptr->data;
uint len = a->u.ptr->length;
ASSERT((len == 16) || (len == 32));
if (size < (3+1+len))
return -1;
bgp_put_attr_hdr3(buf, BA_MP_REACH_NLRI, BAF_OPTIONAL, 1+len);
buf[3] = len;
buf += 4;
put_ip6(buf, ipa_to_ip6(nh[0]));
if (len == 32)
put_ip6(buf+16, ipa_to_ip6(nh[1]));
return 3+1+len;
}
static inline u32
get_af3(byte *buf)
{
return (get_u16(buf) << 16) | buf[2];
}
static void
bgp_decode_mp_reach_nlri(struct bgp_parse_state *s, uint code UNUSED, uint flags UNUSED, byte *data, uint len, ea_list **to UNUSED)
{
/*
* 2 B MP_REACH_NLRI data - Address Family Identifier
* 1 B MP_REACH_NLRI data - Subsequent Address Family Identifier
* 1 B MP_REACH_NLRI data - Length of Next Hop Network Address
* var MP_REACH_NLRI data - Network Address of Next Hop
* 1 B MP_REACH_NLRI data - Reserved (zero)
* var MP_REACH_NLRI data - Network Layer Reachability Information
*/
if ((len < 5) || (len < (5 + (uint) data[3])))
bgp_parse_error(s, 9);
s->mp_reach_af = get_af3(data);
s->mp_next_hop_len = data[3];
s->mp_next_hop_data = data + 4;
s->mp_reach_len = len - 5 - s->mp_next_hop_len;
s->mp_reach_nlri = data + 5 + s->mp_next_hop_len;
}
static void
bgp_decode_mp_unreach_nlri(struct bgp_parse_state *s, uint code UNUSED, uint flags UNUSED, byte *data, uint len, ea_list **to UNUSED)
{
/*
* 2 B MP_UNREACH_NLRI data - Address Family Identifier
* 1 B MP_UNREACH_NLRI data - Subsequent Address Family Identifier
* var MP_UNREACH_NLRI data - Network Layer Reachability Information
*/
if (len < 3)
bgp_parse_error(s, 9);
s->mp_unreach_af = get_af3(data);
s->mp_unreach_len = len - 3;
s->mp_unreach_nlri = data + 3;
}
static void
bgp_export_ext_community(struct bgp_export_state *s, eattr *a)
{
if (!s->proto->is_interior)
{
struct adata *ad = ec_set_del_nontrans(s->pool, a->u.ptr);
if (ad->length == 0)
UNSET(a);
ec_set_sort_x(ad);
a->u.ptr = ad;
}
else
{
if (a->u.ptr->length == 0)
UNSET(a);
a->u.ptr = ec_set_sort(s->pool, a->u.ptr);
}
}
static void
bgp_decode_ext_community(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
{
if (!len || (len % 8))
WITHDRAW(BAD_LENGTH, "EXT_COMMUNITY", len);
struct adata *ad = lp_alloc_adata(s->pool, len);
get_u32s(data, (u32 *) ad->data, len / 4);
bgp_set_attr_ptr(to, s->pool, BA_EXT_COMMUNITY, flags, ad);
}
static void
bgp_decode_as4_aggregator(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
{
if (s->as4_session)
DISCARD(NEW_BGP, "AS4_AGGREGATOR");
if (len != 8)
DISCARD(BAD_LENGTH, "AS4_AGGREGATOR", len);
bgp_set_attr_data(to, s->pool, BA_AS4_AGGREGATOR, flags, data, len);
}
static void
bgp_decode_as4_path(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
{
struct bgp_proto *p = s->proto;
int sets = p->cf->allow_as_sets;
char err[128];
if (s->as4_session)
DISCARD(NEW_BGP, "AS4_PATH");
if (len < 6)
DISCARD(BAD_LENGTH, "AS4_PATH", len);
if (!as_path_valid(data, len, 4, sets, 1, err, sizeof(err)))
DISCARD("Malformed AS4_PATH attribute - %s", err);
struct adata *a = lp_alloc_adata(s->pool, len);
memcpy(a->data, data, len);
/* AS_CONFED* segments are invalid in AS4_PATH; RFC 6793 6 */
if (as_path_contains_confed(a))
{
REPORT("Discarding AS_CONFED* segment from AS4_PATH attribute");
a = as_path_strip_confed(s->pool, a);
}
bgp_set_attr_ptr(to, s->pool, BA_AS4_PATH, flags, a);
}
static void
bgp_export_aigp(struct bgp_export_state *s, eattr *a)
{
if (!s->channel->cf->aigp)
UNSET(a);
}
static void
bgp_decode_aigp(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
{
char err[128];
/* Acceptability test postponed to bgp_finish_attrs() */
if ((flags ^ bgp_attr_table[BA_AIGP].flags) & (BAF_OPTIONAL | BAF_TRANSITIVE))
DISCARD("Malformed AIGP attribute - conflicting flags (%02x)", flags);
if (!bgp_aigp_valid(data, len, err, sizeof(err)))
DISCARD("Malformed AIGP attribute - %s", err);
bgp_set_attr_data(to, s->pool, BA_AIGP, flags, data, len);
}
static void
bgp_format_aigp(const eattr *a, byte *buf, uint size UNUSED)
{
const byte *b = bgp_aigp_get_tlv(a->u.ptr, BGP_AIGP_METRIC);
if (!b)
bsprintf(buf, "?");
else
bsprintf(buf, "%lu", get_u64(b + 3));
}
static void
bgp_export_large_community(struct bgp_export_state *s, eattr *a)
{
if (a->u.ptr->length == 0)
UNSET(a);
a->u.ptr = lc_set_sort(s->pool, a->u.ptr);
}
static void
bgp_decode_large_community(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data, uint len, ea_list **to)
{
if (!len || (len % 12))
WITHDRAW(BAD_LENGTH, "LARGE_COMMUNITY", len);
struct adata *ad = lp_alloc_adata(s->pool, len);
get_u32s(data, (u32 *) ad->data, len / 4);
bgp_set_attr_ptr(to, s->pool, BA_LARGE_COMMUNITY, flags, ad);
}
static void
bgp_decode_otc(struct bgp_parse_state *s, uint code UNUSED, uint flags, byte *data UNUSED, uint len, ea_list **to)
{
if (len != 4)
WITHDRAW(BAD_LENGTH, "OTC", len);
u32 val = get_u32(data);
bgp_set_attr_u32(to, s->pool, BA_ONLY_TO_CUSTOMER, flags, val);
}
static void
bgp_export_mpls_label_stack(struct bgp_export_state *s, eattr *a)
{
net_addr *n = s->route->net->n.addr;
u32 *labels = (u32 *) a->u.ptr->data;
uint lnum = a->u.ptr->length / 4;
/* Perhaps we should just ignore it? */
if (!s->mpls)
REJECT("Unexpected MPLS stack");
/* Empty MPLS stack is not allowed */
if (!lnum)
REJECT("Malformed MPLS stack - empty");
/* This is ugly, but we must ensure that labels fit into NLRI field */
if ((24*lnum + (net_is_vpn(n) ? 64 : 0) + net_pxlen(n)) > 255)
REJECT("Malformed MPLS stack - too many labels (%u)", lnum);
for (uint i = 0; i < lnum; i++)
{
if (labels[i] > 0xfffff)
REJECT("Malformed MPLS stack - invalid label (%u)", labels[i]);
/* TODO: Check for special-purpose label values? */
}
}
static int
bgp_encode_mpls_label_stack(struct bgp_write_state *s, eattr *a, byte *buf UNUSED, uint size UNUSED)
{
/*
* MPLS labels are encoded as a part of the NLRI in MP_REACH_NLRI attribute,
* so we store MPLS_LABEL_STACK and encode it later by AFI-specific hooks.
*/
s->mpls_labels = a->u.ptr;
return 0;
}
static void
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)
{
DISCARD("Discarding received attribute #0");
}
static void
bgp_format_mpls_label_stack(const eattr *a, byte *buf, uint size)
{
u32 *labels = (u32 *) a->u.ptr->data;
uint lnum = a->u.ptr->length / 4;
char *pos = buf;
for (uint i = 0; i < lnum; i++)
{
if (size < 20)
{
bsprintf(pos, "...");
return;
}
uint l = bsprintf(pos, "%d/", labels[i]);
ADVANCE(pos, size, l);
}
/* Clear last slash or terminate empty string */
pos[lnum ? -1 : 0] = 0;
}
static inline void
bgp_decode_unknown(struct bgp_parse_state *s, uint code, uint flags, byte *data, uint len, ea_list **to)
{
/* Cannot use bgp_set_attr_data() as it works on known attributes only */
ea_set_attr_data(to, s->pool, EA_CODE(PROTOCOL_BGP, code), flags, EAF_TYPE_OPAQUE, data, len);
}
/*
* Attribute table
*/
static const struct bgp_attr_desc bgp_attr_table[] = {