forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbgp_ecommunity.c
2082 lines (1817 loc) · 53.3 KB
/
bgp_ecommunity.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
// SPDX-License-Identifier: GPL-2.0-or-later
/* BGP Extended Communities Attribute
* Copyright (C) 2000 Kunihiro Ishiguro <kunihiro@zebra.org>
*/
#include <zebra.h>
#include "hash.h"
#include "memory.h"
#include "prefix.h"
#include "command.h"
#include "queue.h"
#include "filter.h"
#include "jhash.h"
#include "stream.h"
#include "lib/printfrr.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_ecommunity.h"
#include "bgpd/bgp_lcommunity.h"
#include "bgpd/bgp_aspath.h"
#include "bgpd/bgp_flowspec_private.h"
#include "bgpd/bgp_pbr.h"
/* struct used to dump the rate contained in FS set traffic-rate EC */
union traffic_rate {
float rate_float;
uint8_t rate_byte[4];
};
/* Hash of community attribute. */
static struct hash *ecomhash;
/* Allocate a new ecommunities. */
struct ecommunity *ecommunity_new(void)
{
struct ecommunity *ecom;
ecom = (struct ecommunity *)XCALLOC(MTYPE_ECOMMUNITY,
sizeof(struct ecommunity));
ecom->unit_size = ECOMMUNITY_SIZE;
return ecom;
}
void ecommunity_strfree(char **s)
{
XFREE(MTYPE_ECOMMUNITY_STR, *s);
}
/* Free ecommunities. */
void ecommunity_free(struct ecommunity **ecom)
{
if (!(*ecom))
return;
XFREE(MTYPE_ECOMMUNITY_VAL, (*ecom)->val);
XFREE(MTYPE_ECOMMUNITY_STR, (*ecom)->str);
XFREE(MTYPE_ECOMMUNITY, *ecom);
}
static void ecommunity_hash_free(struct ecommunity *ecom)
{
ecommunity_free(&ecom);
}
/* Add a new Extended Communities value to Extended Communities
Attribute structure. When the value is already exists in the
structure, we don't add the value. Newly added value is sorted by
numerical order. When the value is added to the structure return 1
else return 0.
The additional parameters 'unique' and 'overwrite' ensure a particular
extended community (based on type and sub-type) is present only
once and whether the new value should replace what is existing or
not.
*/
static bool ecommunity_add_val_internal(struct ecommunity *ecom,
const void *eval,
bool unique, bool overwrite,
uint8_t ecom_size)
{
uint32_t c, ins_idx;
const struct ecommunity_val *eval4 = (struct ecommunity_val *)eval;
const struct ecommunity_val_ipv6 *eval6 =
(struct ecommunity_val_ipv6 *)eval;
/* When this is fist value, just add it. */
if (ecom->val == NULL) {
ecom->size = 1;
ecom->val = XMALLOC(MTYPE_ECOMMUNITY_VAL,
ecom_length_size(ecom, ecom_size));
memcpy(ecom->val, eval, ecom_size);
return true;
}
/* If the value already exists in the structure return 0. */
/* check also if the extended community itself exists. */
c = 0;
ins_idx = UINT32_MAX;
for (uint8_t *p = ecom->val; c < ecom->size;
p += ecom_size, c++) {
if (unique) {
if (ecom_size == ECOMMUNITY_SIZE) {
if (p[0] == eval4->val[0] &&
p[1] == eval4->val[1]) {
if (overwrite) {
memcpy(p, eval4->val,
ecom_size);
return true;
}
return false;
}
} else {
if (p[0] == eval6->val[0] &&
p[1] == eval6->val[1]) {
if (overwrite) {
memcpy(p, eval6->val,
ecom_size);
return true;
}
return false;
}
}
}
int ret = memcmp(p, eval, ecom_size);
if (ret == 0)
return false;
if (ret > 0) {
if (!unique)
break;
if (ins_idx == UINT32_MAX)
ins_idx = c;
}
}
if (ins_idx == UINT32_MAX)
ins_idx = c;
/* Add the value to the structure with numerical sorting. */
ecom->size++;
ecom->val = XREALLOC(MTYPE_ECOMMUNITY_VAL, ecom->val,
ecom_length_size(ecom, ecom_size));
memmove(ecom->val + ((ins_idx + 1) * ecom_size),
ecom->val + (ins_idx * ecom_size),
(ecom->size - 1 - ins_idx) * ecom_size);
memcpy(ecom->val + (ins_idx * ecom_size),
eval, ecom_size);
return true;
}
/* Add a new Extended Communities value to Extended Communities
* Attribute structure. When the value is already exists in the
* structure, we don't add the value. Newly added value is sorted by
* numerical order. When the value is added to the structure return 1
* else return 0.
*/
bool ecommunity_add_val(struct ecommunity *ecom, struct ecommunity_val *eval,
bool unique, bool overwrite)
{
return ecommunity_add_val_internal(ecom, (const void *)eval, unique,
overwrite, ECOMMUNITY_SIZE);
}
bool ecommunity_add_val_ipv6(struct ecommunity *ecom,
struct ecommunity_val_ipv6 *eval,
bool unique, bool overwrite)
{
return ecommunity_add_val_internal(ecom, (const void *)eval, unique,
overwrite, IPV6_ECOMMUNITY_SIZE);
}
static struct ecommunity *
ecommunity_uniq_sort_internal(struct ecommunity *ecom,
unsigned short ecom_size)
{
uint32_t i;
struct ecommunity *new;
const void *eval;
if (!ecom)
return NULL;
new = ecommunity_new();
new->unit_size = ecom_size;
new->disable_ieee_floating = ecom->disable_ieee_floating;
for (i = 0; i < ecom->size; i++) {
eval = (void *)(ecom->val + (i * ecom_size));
ecommunity_add_val_internal(new, eval, false, false, ecom_size);
}
return new;
}
/* This function takes pointer to Extended Communites structure then
* create a new Extended Communities structure by uniq and sort each
* Extended Communities value.
*/
struct ecommunity *ecommunity_uniq_sort(struct ecommunity *ecom)
{
return ecommunity_uniq_sort_internal(ecom, ECOMMUNITY_SIZE);
}
/* Parse Extended Communites Attribute in BGP packet. */
static struct ecommunity *ecommunity_parse_internal(uint8_t *pnt,
unsigned short length,
unsigned short size_ecom,
bool disable_ieee_floating)
{
struct ecommunity tmp;
struct ecommunity *new;
/* Length check. */
if (length % size_ecom)
return NULL;
/* Prepare tmporary structure for making a new Extended Communities
Attribute. */
tmp.size = length / size_ecom;
tmp.val = pnt;
tmp.disable_ieee_floating = disable_ieee_floating;
/* Create a new Extended Communities Attribute by uniq and sort each
Extended Communities value */
new = ecommunity_uniq_sort_internal(&tmp, size_ecom);
return ecommunity_intern(new);
}
struct ecommunity *ecommunity_parse(uint8_t *pnt, unsigned short length,
bool disable_ieee_floating)
{
return ecommunity_parse_internal(pnt, length, ECOMMUNITY_SIZE,
disable_ieee_floating);
}
struct ecommunity *ecommunity_parse_ipv6(uint8_t *pnt, unsigned short length)
{
return ecommunity_parse_internal(pnt, length, IPV6_ECOMMUNITY_SIZE,
false);
}
/* Duplicate the Extended Communities Attribute structure. */
struct ecommunity *ecommunity_dup(struct ecommunity *ecom)
{
struct ecommunity *new;
new = XCALLOC(MTYPE_ECOMMUNITY, sizeof(struct ecommunity));
new->size = ecom->size;
new->unit_size = ecom->unit_size;
if (new->size) {
new->val = XMALLOC(MTYPE_ECOMMUNITY_VAL,
ecom->size * ecom->unit_size);
memcpy(new->val, ecom->val,
(size_t)ecom->size * (size_t)ecom->unit_size);
} else
new->val = NULL;
return new;
}
/* Return string representation of ecommunities attribute. */
const char *ecommunity_str(struct ecommunity *ecom)
{
if (!ecom)
return "(null)";
if (!ecom->str)
ecom->str =
ecommunity_ecom2str(ecom, ECOMMUNITY_FORMAT_DISPLAY, 0);
return ecom->str;
}
/* Merge two Extended Communities Attribute structure. */
struct ecommunity *ecommunity_merge(struct ecommunity *ecom1,
struct ecommunity *ecom2)
{
ecom1->val = XREALLOC(MTYPE_ECOMMUNITY_VAL, ecom1->val,
(size_t)(ecom1->size + ecom2->size)
* (size_t)ecom1->unit_size);
memcpy(ecom1->val + (ecom1->size * ecom1->unit_size), ecom2->val,
(size_t)ecom2->size * (size_t)ecom1->unit_size);
ecom1->size += ecom2->size;
return ecom1;
}
/* Intern Extended Communities Attribute. */
struct ecommunity *ecommunity_intern(struct ecommunity *ecom)
{
struct ecommunity *find;
assert(ecom->refcnt == 0);
find = (struct ecommunity *)hash_get(ecomhash, ecom, hash_alloc_intern);
if (find != ecom)
ecommunity_free(&ecom);
find->refcnt++;
if (!find->str)
find->str =
ecommunity_ecom2str(find, ECOMMUNITY_FORMAT_DISPLAY, 0);
return find;
}
/* Unintern Extended Communities Attribute. */
void ecommunity_unintern(struct ecommunity **ecom)
{
struct ecommunity *ret;
if (!*ecom)
return;
if ((*ecom)->refcnt)
(*ecom)->refcnt--;
/* Pull off from hash. */
if ((*ecom)->refcnt == 0) {
/* Extended community must be in the hash. */
ret = (struct ecommunity *)hash_release(ecomhash, *ecom);
assert(ret != NULL);
ecommunity_free(ecom);
}
}
/* Utinity function to make hash key. */
unsigned int ecommunity_hash_make(const void *arg)
{
const struct ecommunity *ecom = arg;
int size = ecom->size * ecom->unit_size;
return jhash(ecom->val, size, 0x564321ab);
}
/* Compare two Extended Communities Attribute structure. */
bool ecommunity_cmp(const void *arg1, const void *arg2)
{
const struct ecommunity *ecom1 = arg1;
const struct ecommunity *ecom2 = arg2;
if (ecom1 == NULL && ecom2 == NULL)
return true;
if (ecom1 == NULL || ecom2 == NULL)
return false;
if (ecom1->unit_size != ecom2->unit_size)
return false;
return (ecom1->size == ecom2->size
&& memcmp(ecom1->val, ecom2->val, ecom1->size *
ecom1->unit_size) == 0);
}
static void ecommunity_color_str(char *buf, size_t bufsz, uint8_t *ptr)
{
/*
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | 0x03 | Sub-Type(0x0b) | CO| Flags |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Color Value |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* https://datatracker.ietf.org/doc/rfc9256/, Section 8.8.1
* The CO bits can have 4 different values: 00 01 10 11
*/
uint32_t colorid;
uint8_t color_type;
/* get the color type */
ptr++;
color_type = (*ptr) >> 6;
memcpy(&colorid, ptr + 2, 4);
colorid = ntohl(colorid);
snprintf(buf, bufsz, "Color:%d%d:%d", (color_type & 0x2) >> 1, color_type & 0x1, colorid);
}
/* Initialize Extended Comminities related hash. */
void ecommunity_init(void)
{
ecomhash = hash_create(ecommunity_hash_make, ecommunity_cmp,
"BGP ecommunity hash");
}
void ecommunity_finish(void)
{
hash_clean_and_free(&ecomhash, (void (*)(void *))ecommunity_hash_free);
}
/* Extended Communities token enum. */
enum ecommunity_token {
ecommunity_token_unknown = 0,
ecommunity_token_rt,
ecommunity_token_nt,
ecommunity_token_soo,
ecommunity_token_color,
ecommunity_token_val,
ecommunity_token_rt6,
ecommunity_token_val6,
};
static const char *ecommunity_origin_validation_state2str(
enum ecommunity_origin_validation_states state)
{
switch (state) {
case ECOMMUNITY_ORIGIN_VALIDATION_STATE_VALID:
return "valid";
case ECOMMUNITY_ORIGIN_VALIDATION_STATE_NOTFOUND:
return "not-found";
case ECOMMUNITY_ORIGIN_VALIDATION_STATE_INVALID:
return "invalid";
case ECOMMUNITY_ORIGIN_VALIDATION_STATE_NOTUSED:
return "not-used";
}
return "ERROR";
}
static void ecommunity_origin_validation_state_str(char *buf, size_t bufsz,
uint8_t *ptr)
{
/* Origin Validation State is encoded in the last octet
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | 0x43 | 0x00 | Reserved |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Reserved |validationstate|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
uint8_t state = *(ptr + ECOMMUNITY_SIZE - 3);
snprintf(buf, bufsz, "OVS:%s",
ecommunity_origin_validation_state2str(state));
(void)ptr; /* consume value */
}
bool ecommunity_node_target_match(struct ecommunity *ecom,
struct in_addr *local_id)
{
uint32_t i;
bool match = false;
if (!ecom || !ecom->size)
return NULL;
for (i = 0; i < ecom->size; i++) {
const uint8_t *pnt;
uint8_t type, sub_type;
pnt = (ecom->val + (i * ECOMMUNITY_SIZE));
type = *pnt++;
sub_type = *pnt++;
if (type == ECOMMUNITY_ENCODE_IP &&
sub_type == ECOMMUNITY_NODE_TARGET) {
/* Node Target ID is encoded as A.B.C.D:0 */
if (IPV4_ADDR_SAME((struct in_addr *)pnt, local_id))
match = true;
(void)pnt;
}
}
return match;
}
static void ecommunity_node_target_str(char *buf, size_t bufsz, uint8_t *ptr,
int format)
{
/*
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | 0x01 or 0x41 | Sub-Type(0x09) | Target BGP Identifier |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Target BGP Identifier (cont.) | Reserved |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
struct in_addr node_id = {};
IPV4_ADDR_COPY(&node_id, (struct in_addr *)ptr);
snprintfrr(buf, bufsz, "%s%pI4%s",
format == ECOMMUNITY_FORMAT_COMMUNITY_LIST ? "nt " : "NT:",
&node_id,
format == ECOMMUNITY_FORMAT_COMMUNITY_LIST ? ":0" : "");
(void)ptr; /* consume value */
}
static int ecommunity_encode_internal(uint8_t type, uint8_t sub_type,
int trans, as_t as,
struct in_addr *ip,
struct in6_addr *ip6,
uint32_t val,
void *eval_ptr)
{
struct ecommunity_val *eval = (struct ecommunity_val *)eval_ptr;
struct ecommunity_val_ipv6 *eval6 =
(struct ecommunity_val_ipv6 *)eval_ptr;
assert(eval);
if (type == ECOMMUNITY_ENCODE_AS) {
if (as > BGP_AS_MAX)
return -1;
} else if (type == ECOMMUNITY_ENCODE_IP
|| type == ECOMMUNITY_ENCODE_AS4) {
if (val > UINT16_MAX)
return -1;
} else if (type == ECOMMUNITY_ENCODE_TRANS_EXP &&
sub_type == ECOMMUNITY_FLOWSPEC_REDIRECT_IPV6 &&
(!ip6 || val > UINT16_MAX)) {
return -1;
}
/* Fill in the values. */
eval->val[0] = type;
if (!trans)
SET_FLAG(eval->val[0], ECOMMUNITY_FLAG_NON_TRANSITIVE);
eval->val[1] = sub_type;
if (type == ECOMMUNITY_ENCODE_AS) {
encode_route_target_as(as, val, eval, trans);
} else if (type == ECOMMUNITY_ENCODE_IP) {
if (sub_type == ECOMMUNITY_NODE_TARGET)
encode_node_target(ip, eval, trans);
else
encode_route_target_ip(ip, val, eval, trans);
} else if (type == ECOMMUNITY_ENCODE_TRANS_EXP &&
sub_type == ECOMMUNITY_FLOWSPEC_REDIRECT_IPV6) {
memcpy(&eval6->val[2], ip6, sizeof(struct in6_addr));
eval6->val[18] = (val >> 8) & 0xff;
eval6->val[19] = val & 0xff;
} else if (type == ECOMMUNITY_ENCODE_OPAQUE &&
sub_type == ECOMMUNITY_COLOR) {
encode_color(val, as, eval);
} else {
encode_route_target_as4(as, val, eval, trans);
}
return 0;
}
/*
* Encode BGP extended community from passed values. Supports types
* defined in RFC 4360 and well-known sub-types.
*/
static int ecommunity_encode(uint8_t type, uint8_t sub_type, int trans, as_t as,
struct in_addr ip, uint32_t val,
struct ecommunity_val *eval)
{
return ecommunity_encode_internal(type, sub_type, trans, as,
&ip, NULL, val, (void *)eval);
}
/* Get next Extended Communities token from the string. */
static const char *ecommunity_gettoken(const char *str, void *eval_ptr,
enum ecommunity_token *token, int type)
{
int ret;
int dot = 0;
int digit = 0;
int separator = 0;
const char *p = str;
char *endptr;
struct in_addr ip;
struct in6_addr ip6;
as_t as = 0;
uint32_t val = 0;
uint32_t val_color = 0;
uint8_t ecomm_type = 0;
uint8_t sub_type = 0;
char buf[INET_ADDRSTRLEN + 1];
struct ecommunity_val *eval = (struct ecommunity_val *)eval_ptr;
uint64_t tmp_as = 0;
static const char str_color[5] = "color";
const char *ptr_color;
bool val_color_set = false;
/* Skip white space. */
while (isspace((unsigned char)*p)) {
p++;
str++;
}
/* Check the end of the line. */
if (*p == '\0')
return NULL;
/* "rt", "nt", "soo", and "color" keyword parse. */
/* "rt" */
if (tolower((unsigned char)*p) == 'r') {
p++;
if (tolower((unsigned char)*p) == 't') {
p++;
if (*p != '\0' && tolower((int)*p) == '6')
*token = ecommunity_token_rt6;
else
*token = ecommunity_token_rt;
return p;
}
if (isspace((unsigned char)*p) || *p == '\0') {
*token = ecommunity_token_rt;
return p;
}
goto error;
}
/* "nt" */
if (tolower((unsigned char)*p) == 'n') {
p++;
if (tolower((unsigned char)*p) == 't') {
p++;
*token = ecommunity_token_nt;
return p;
}
if (isspace((unsigned char)*p) || *p == '\0') {
*token = ecommunity_token_nt;
return p;
}
goto error;
}
/* "soo" */
if (tolower((unsigned char)*p) == 's') {
p++;
if (tolower((unsigned char)*p) == 'o') {
p++;
if (tolower((unsigned char)*p) == 'o') {
p++;
*token = ecommunity_token_soo;
return p;
}
if (isspace((unsigned char)*p) || *p == '\0') {
*token = ecommunity_token_soo;
return p;
}
goto error;
}
if (isspace((unsigned char)*p) || *p == '\0') {
*token = ecommunity_token_soo;
return p;
}
goto error;
}
/* "color" */
if (tolower((unsigned char)*p) == 'c') {
ptr_color = &str_color[0];
for (unsigned int i = 0; i < 5; i++) {
if (tolower((unsigned char)*p) != *ptr_color)
break;
p++;
ptr_color++;
}
if (isspace((unsigned char)*p) || *p == '\0') {
*token = ecommunity_token_color;
return p;
}
goto error;
}
/* What a mess, there are several possibilities:
*
* a) A.B.C.D:MN
* b) EF:OPQR
* c) GHJK:MN
* d) <IPV6>:MN (only with rt6)
*
* A.B.C.D: Four Byte IP
* EF: Two byte ASN
* GHJK: Four-byte ASN
* MN: Two byte value
* OPQR: Four byte value
*
*/
/* IPv6 case : look for last ':' */
if (*token == ecommunity_token_rt6 ||
*token == ecommunity_token_val6) {
char *limit;
limit = endptr = strrchr(p, ':');
if (!endptr)
goto error;
endptr++;
errno = 0;
tmp_as = strtoul(endptr, &endptr, 10);
/* 'unsigned long' is a uint64 on 64-bit
* systems, and uint32 on 32-bit systems. So for
* 64-bit we can just directly check the value
* against BGP_AS4_MAX/UINT32_MAX, and for
* 32-bit we can check for errno (set to ERANGE
* upon overflow).
*/
if (*endptr != '\0' || tmp_as == BGP_AS4_MAX || errno)
goto error;
as = (as_t)tmp_as;
memcpy(buf, p, (limit - p));
buf[limit - p] = '\0';
ret = inet_pton(AF_INET6, buf, &ip6);
if (ret == 0)
goto error;
ecomm_type = ECOMMUNITY_ENCODE_TRANS_EXP;
if (ecommunity_encode_internal(ecomm_type,
ECOMMUNITY_FLOWSPEC_REDIRECT_IPV6,
1, 0, NULL, &ip6, as, eval_ptr))
goto error;
*token = ecommunity_token_val6;
while (isdigit((int)*p) || *p == ':' || *p == '.') {
p++;
}
return p;
}
while (isdigit((unsigned char)*p) || *p == ':' || *p == '.') {
if (*p == ':') {
if (separator)
goto error;
separator = 1;
digit = 0;
if ((p - str) > INET_ADDRSTRLEN)
goto error;
memset(buf, 0, INET_ADDRSTRLEN + 1);
memcpy(buf, str, p - str);
if (dot == 3) {
/* Parsing A.B.C.D in:
* A.B.C.D:MN
*/
ret = inet_aton(buf, &ip);
if (ret == 0)
goto error;
} else if (dot == 1) {
/* Parsing A.B AS number in:
* A.B:MN
*/
if (!asn_str2asn(buf, &as))
goto error;
} else if (type == ECOMMUNITY_COLOR) {
/* If extcommunity is color, only support 00/01/10/11, max value is 3 */
/* color value */
as = strtoul(buf, &endptr, 2);
if (*endptr != '\0' || as > 3)
goto error;
val_color = 0;
} else {
/* Parsing A AS number in A:MN */
errno = 0;
tmp_as = strtoul(buf, &endptr, 10);
/* 'unsigned long' is a uint64 on 64-bit
* systems, and uint32 on 32-bit systems. So for
* 64-bit we can just directly check the value
* against BGP_AS4_MAX/UINT32_MAX, and for
* 32-bit we can check for errno (set to ERANGE
* upon overflow).
*/
if (*endptr != '\0' || tmp_as > BGP_AS4_MAX ||
errno)
goto error;
if (*token == ecommunity_token_color && tmp_as > 3)
goto error;
as = (as_t)tmp_as;
}
} else if (*p == '.') {
if (separator)
goto error;
/* either IP or AS format */
dot++;
if (dot > 1)
ecomm_type = ECOMMUNITY_ENCODE_IP;
if (dot >= 4)
goto error;
} else {
digit = 1;
/* We're past the IP/ASN part,
* or we have a color
*/
if (separator) {
val *= 10;
val += (*p - '0');
val_color_set = false;
} else {
val_color *= 10;
val_color += (*p - '0');
val_color_set = true;
}
}
p++;
}
/* Low digit part must be there. */
if (!digit && (!separator || !val_color_set))
goto error;
if (ecomm_type != ECOMMUNITY_ENCODE_IP) {
/* Encode result into extended community for AS format or color. */
if (as > BGP_AS_MAX)
ecomm_type = ECOMMUNITY_ENCODE_AS4;
else if (type == ECOMMUNITY_COLOR) {
ecomm_type = ECOMMUNITY_ENCODE_OPAQUE;
sub_type = ECOMMUNITY_COLOR;
if (val_color) {
val = val_color;
as = 1;
}
} else if (as > 0)
ecomm_type = ECOMMUNITY_ENCODE_AS;
}
if (ecommunity_encode(ecomm_type, sub_type, 1, as, ip, val, eval))
goto error;
*token = ecommunity_token_val;
return p;
error:
*token = ecommunity_token_unknown;
return p;
}
static struct ecommunity *ecommunity_str2com_internal(const char *str, int type,
int keyword_included,
bool is_ipv6_extcomm)
{
struct ecommunity *ecom = NULL;
enum ecommunity_token token = ecommunity_token_unknown;
struct ecommunity_val_ipv6 eval;
int keyword = 0;
if (is_ipv6_extcomm)
token = ecommunity_token_rt6;
while ((str = ecommunity_gettoken(str, (void *)&eval, &token, type))) {
switch (token) {
case ecommunity_token_rt:
case ecommunity_token_nt:
case ecommunity_token_rt6:
case ecommunity_token_soo:
case ecommunity_token_color:
if (!keyword_included || keyword) {
if (ecom)
ecommunity_free(&ecom);
return NULL;
}
keyword = 1;
if (token == ecommunity_token_rt ||
token == ecommunity_token_rt6)
type = ECOMMUNITY_ROUTE_TARGET;
if (token == ecommunity_token_soo)
type = ECOMMUNITY_SITE_ORIGIN;
if (token == ecommunity_token_nt)
type = ECOMMUNITY_NODE_TARGET;
if (token == ecommunity_token_color)
type = ECOMMUNITY_COLOR;
break;
case ecommunity_token_val:
if (keyword_included) {
if (!keyword) {
ecommunity_free(&ecom);
return NULL;
}
keyword = 0;
}
if (ecom == NULL)
ecom = ecommunity_new();
eval.val[1] = type;
ecommunity_add_val_internal(ecom, (void *)&eval,
false, false,
ecom->unit_size);
break;
case ecommunity_token_val6:
if (keyword_included) {
if (!keyword) {
ecommunity_free(&ecom);
return NULL;
}
keyword = 0;
}
if (ecom == NULL)
ecom = ecommunity_new();
ecom->unit_size = IPV6_ECOMMUNITY_SIZE;
eval.val[1] = type;
ecommunity_add_val_internal(ecom, (void *)&eval, false, false,
ecom->unit_size);
break;
case ecommunity_token_unknown:
if (ecom)
ecommunity_free(&ecom);
return NULL;
}
}
return ecom;
}
/* Convert string to extended community attribute.
*
* When type is already known, please specify both str and type. str
* should not include keyword such as "rt" and "soo". Type is
* ECOMMUNITY_ROUTE_TARGET or ECOMMUNITY_SITE_ORIGIN.
* keyword_included should be zero.
*
* For example route-map's "set extcommunity" command case:
*
* "rt 100:1 100:2 100:3" -> str = "100:1 100:2 100:3"
* type = ECOMMUNITY_ROUTE_TARGET
* keyword_included = 0
*
* "soo 100:1" -> str = "100:1"
* type = ECOMMUNITY_SITE_ORIGIN
* keyword_included = 0
*
* When string includes keyword for each extended community value.
* Please specify keyword_included as non-zero value.
*
* For example standard extcommunity-list case:
*
* "rt 100:1 rt 100:2 soo 100:1" -> str = "rt 100:1 rt 100:2 soo 100:1"
* type = 0
* keyword_include = 1
*/
struct ecommunity *ecommunity_str2com(const char *str, int type,
int keyword_included)
{
return ecommunity_str2com_internal(str, type,
keyword_included, false);
}
struct ecommunity *ecommunity_str2com_ipv6(const char *str, int type,
int keyword_included)
{
return ecommunity_str2com_internal(str, type,
keyword_included, true);
}
static int ecommunity_rt_soo_str_internal(char *buf, size_t bufsz,
const uint8_t *pnt, int type,
int sub_type, int format,
unsigned short ecom_size)
{
int len = 0;
const char *prefix;
char buf_local[INET6_ADDRSTRLEN];
/* For parse Extended Community attribute tupple. */
struct ecommunity_as eas;
struct ecommunity_ip eip;
struct ecommunity_ip6 eip6;
/* Determine prefix for string, if any. */
switch (format) {
case ECOMMUNITY_FORMAT_COMMUNITY_LIST:
prefix = (sub_type == ECOMMUNITY_ROUTE_TARGET ? "rt " : "soo ");
break;
case ECOMMUNITY_FORMAT_DISPLAY:
prefix = (sub_type == ECOMMUNITY_ROUTE_TARGET ? "RT:" : "SoO:");
break;
case ECOMMUNITY_FORMAT_ROUTE_MAP:
prefix = "";
break;
default:
prefix = "";
break;
}
/* Put string into buffer. */
if (type == ECOMMUNITY_ENCODE_AS4) {
pnt = ptr_get_be32(pnt, &eas.as);
eas.val = (*pnt++ << 8);
eas.val |= (*pnt++);
len = snprintf(buf, bufsz, "%s%u:%u", prefix, eas.as, eas.val);
} else if (type == ECOMMUNITY_ENCODE_AS) {
if (ecom_size == ECOMMUNITY_SIZE) {
eas.as = (*pnt++ << 8);
eas.as |= (*pnt++);
pnt = ptr_get_be32(pnt, &eas.val);
len = snprintf(buf, bufsz, "%s%u:%u", prefix, eas.as,
eas.val);
} else {
/* this is an IPv6 ext community
* first 16 bytes stands for IPv6 addres
*/
memcpy(&eip6.ip, pnt, 16);
pnt += 16;
eip6.val = (*pnt++ << 8);
eip6.val |= (*pnt++);
inet_ntop(AF_INET6, &eip6.ip, buf_local,
sizeof(buf_local));
len = snprintf(buf, bufsz, "%s%s:%u", prefix,
buf_local, eip6.val);