forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbgp_pbr.c
2916 lines (2657 loc) · 80.4 KB
/
bgp_pbr.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 pbr
* Copyright (C) 6WIND
*/
#include "zebra.h"
#include "prefix.h"
#include "zclient.h"
#include "jhash.h"
#include "pbr.h"
#include "lib/printfrr.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_pbr.h"
#include "bgpd/bgp_debug.h"
#include "bgpd/bgp_flowspec_util.h"
#include "bgpd/bgp_ecommunity.h"
#include "bgpd/bgp_route.h"
#include "bgpd/bgp_attr.h"
#include "bgpd/bgp_zebra.h"
#include "bgpd/bgp_mplsvpn.h"
#include "bgpd/bgp_flowspec_private.h"
#include "bgpd/bgp_errors.h"
DEFINE_MTYPE_STATIC(BGPD, PBR_MATCH_ENTRY, "PBR match entry");
DEFINE_MTYPE_STATIC(BGPD, PBR_MATCH, "PBR match");
DEFINE_MTYPE_STATIC(BGPD, PBR_ACTION, "PBR action");
DEFINE_MTYPE_STATIC(BGPD, PBR_RULE, "PBR rule");
DEFINE_MTYPE_STATIC(BGPD, PBR, "BGP PBR Context");
DEFINE_MTYPE_STATIC(BGPD, PBR_VALMASK, "BGP PBR Val Mask Value");
/* chain strings too long to fit in one line */
#define FSPEC_ACTION_EXCEED_LIMIT "flowspec actions exceeds limit"
#define IPV6_FRAGMENT_INVALID "fragment not valid for IPv6 for this implementation"
RB_GENERATE(bgp_pbr_interface_head, bgp_pbr_interface,
id_entry, bgp_pbr_interface_compare);
struct bgp_pbr_interface_head ifaces_by_name_ipv4 =
RB_INITIALIZER(&ifaces_by_name_ipv4);
static int bgp_pbr_match_counter_unique;
static int bgp_pbr_match_entry_counter_unique;
static int bgp_pbr_action_counter_unique;
static int bgp_pbr_match_iptable_counter_unique;
struct bgp_pbr_match_iptable_unique {
uint32_t unique;
struct bgp_pbr_match *bpm_found;
};
struct bgp_pbr_match_entry_unique {
uint32_t unique;
struct bgp_pbr_match_entry *bpme_found;
};
struct bgp_pbr_action_unique {
uint32_t unique;
struct bgp_pbr_action *bpa_found;
};
struct bgp_pbr_rule_unique {
uint32_t unique;
struct bgp_pbr_rule *bpr_found;
};
static int bgp_pbr_rule_walkcb(struct hash_bucket *bucket, void *arg)
{
struct bgp_pbr_rule *bpr = (struct bgp_pbr_rule *)bucket->data;
struct bgp_pbr_rule_unique *bpru = (struct bgp_pbr_rule_unique *)
arg;
uint32_t unique = bpru->unique;
if (bpr->unique == unique) {
bpru->bpr_found = bpr;
return HASHWALK_ABORT;
}
return HASHWALK_CONTINUE;
}
static int bgp_pbr_action_walkcb(struct hash_bucket *bucket, void *arg)
{
struct bgp_pbr_action *bpa = (struct bgp_pbr_action *)bucket->data;
struct bgp_pbr_action_unique *bpau = (struct bgp_pbr_action_unique *)
arg;
uint32_t unique = bpau->unique;
if (bpa->unique == unique) {
bpau->bpa_found = bpa;
return HASHWALK_ABORT;
}
return HASHWALK_CONTINUE;
}
static int bgp_pbr_match_entry_walkcb(struct hash_bucket *bucket, void *arg)
{
struct bgp_pbr_match_entry *bpme =
(struct bgp_pbr_match_entry *)bucket->data;
struct bgp_pbr_match_entry_unique *bpmeu =
(struct bgp_pbr_match_entry_unique *)arg;
uint32_t unique = bpmeu->unique;
if (bpme->unique == unique) {
bpmeu->bpme_found = bpme;
return HASHWALK_ABORT;
}
return HASHWALK_CONTINUE;
}
struct bgp_pbr_match_ipsetname {
char *ipsetname;
struct bgp_pbr_match *bpm_found;
};
static int bgp_pbr_match_pername_walkcb(struct hash_bucket *bucket, void *arg)
{
struct bgp_pbr_match *bpm = (struct bgp_pbr_match *)bucket->data;
struct bgp_pbr_match_ipsetname *bpmi =
(struct bgp_pbr_match_ipsetname *)arg;
char *ipset_name = bpmi->ipsetname;
if (!strncmp(ipset_name, bpm->ipset_name,
ZEBRA_IPSET_NAME_SIZE)) {
bpmi->bpm_found = bpm;
return HASHWALK_ABORT;
}
return HASHWALK_CONTINUE;
}
static int bgp_pbr_match_iptable_walkcb(struct hash_bucket *bucket, void *arg)
{
struct bgp_pbr_match *bpm = (struct bgp_pbr_match *)bucket->data;
struct bgp_pbr_match_iptable_unique *bpmiu =
(struct bgp_pbr_match_iptable_unique *)arg;
uint32_t unique = bpmiu->unique;
if (bpm->unique2 == unique) {
bpmiu->bpm_found = bpm;
return HASHWALK_ABORT;
}
return HASHWALK_CONTINUE;
}
struct bgp_pbr_match_unique {
uint32_t unique;
struct bgp_pbr_match *bpm_found;
};
static int bgp_pbr_match_walkcb(struct hash_bucket *bucket, void *arg)
{
struct bgp_pbr_match *bpm = (struct bgp_pbr_match *)bucket->data;
struct bgp_pbr_match_unique *bpmu = (struct bgp_pbr_match_unique *)
arg;
uint32_t unique = bpmu->unique;
if (bpm->unique == unique) {
bpmu->bpm_found = bpm;
return HASHWALK_ABORT;
}
return HASHWALK_CONTINUE;
}
static int snprintf_bgp_pbr_match_val(char *str, int len,
struct bgp_pbr_match_val *mval,
const char *prepend)
{
char *ptr = str;
int delta;
if (prepend) {
delta = snprintf(ptr, len, "%s", prepend);
ptr += delta;
len -= delta;
} else {
if (CHECK_FLAG(mval->unary_operator, OPERATOR_UNARY_OR)) {
delta = snprintf(ptr, len, ", or ");
ptr += delta;
len -= delta;
}
if (CHECK_FLAG(mval->unary_operator, OPERATOR_UNARY_AND)) {
delta = snprintf(ptr, len, ", and ");
ptr += delta;
len -= delta;
}
}
if (CHECK_FLAG(mval->compare_operator, OPERATOR_COMPARE_LESS_THAN)) {
delta = snprintf(ptr, len, "<");
ptr += delta;
len -= delta;
}
if (CHECK_FLAG(mval->compare_operator, OPERATOR_COMPARE_GREATER_THAN)) {
delta = snprintf(ptr, len, ">");
ptr += delta;
len -= delta;
}
if (CHECK_FLAG(mval->compare_operator, OPERATOR_COMPARE_EQUAL_TO)) {
delta = snprintf(ptr, len, "=");
ptr += delta;
len -= delta;
}
if (CHECK_FLAG(mval->compare_operator, OPERATOR_COMPARE_EXACT_MATCH)) {
delta = snprintf(ptr, len, "match");
ptr += delta;
len -= delta;
}
ptr += snprintf(ptr, len, " %u", mval->value);
return (int)(ptr - str);
}
#define INCREMENT_DISPLAY(_ptr, _cnt, _len) do { \
int sn_delta; \
\
if (_cnt) { \
sn_delta = snprintf((_ptr), (_len), "; ");\
(_len) -= sn_delta; \
(_ptr) += sn_delta; \
} \
(_cnt)++; \
} while (0)
/* this structure can be used for port range,
* but also for other values range like packet length range
*/
struct bgp_pbr_range_port {
uint16_t min_port;
uint16_t max_port;
};
/* this structure can be used to filter with a mask
* for instance it supports not instructions like for
* tcpflags
*/
struct bgp_pbr_val_mask {
uint16_t val;
uint16_t mask;
};
/* this structure is used to pass instructs
* so that BGP can create pbr instructions to ZEBRA
*/
struct bgp_pbr_filter {
uint8_t type;
vrf_id_t vrf_id;
uint8_t family;
struct prefix *src;
struct prefix *dst;
uint8_t bitmask_iprule;
uint8_t protocol;
struct bgp_pbr_range_port *pkt_len;
struct bgp_pbr_range_port *src_port;
struct bgp_pbr_range_port *dst_port;
struct bgp_pbr_val_mask *tcp_flags;
struct bgp_pbr_val_mask *dscp;
struct bgp_pbr_val_mask *flow_label;
struct bgp_pbr_val_mask *pkt_len_val;
struct bgp_pbr_val_mask *fragment;
};
/* this structure is used to contain OR instructions
* so that BGP can create multiple pbr instructions
* to ZEBRA
*/
struct bgp_pbr_or_filter {
struct list *tcpflags;
struct list *dscp;
struct list *flowlabel;
struct list *pkt_len;
struct list *fragment;
struct list *icmp_type;
struct list *icmp_code;
};
static void bgp_pbr_policyroute_add_to_zebra_unit(struct bgp *bgp,
struct bgp_path_info *path,
struct bgp_pbr_filter *bpf,
struct nexthop *nh,
float *rate);
static void bgp_pbr_dump_entry(struct bgp_pbr_filter *bpf, bool add);
static bool bgp_pbr_extract_enumerate_unary_opposite(
uint8_t unary_operator,
struct bgp_pbr_val_mask *and_valmask,
struct list *or_valmask, uint32_t value,
uint8_t type_entry)
{
if (unary_operator == OPERATOR_UNARY_AND && and_valmask) {
if (type_entry == FLOWSPEC_TCP_FLAGS) {
SET_FLAG(and_valmask->mask, CHECK_FLAG(TCP_HEADER_ALL_FLAGS, ~(value)));
} else if (type_entry == FLOWSPEC_DSCP ||
type_entry == FLOWSPEC_FLOW_LABEL ||
type_entry == FLOWSPEC_PKT_LEN ||
type_entry == FLOWSPEC_FRAGMENT) {
and_valmask->val = value;
and_valmask->mask = 1; /* inverse */
}
} else if (unary_operator == OPERATOR_UNARY_OR && or_valmask) {
and_valmask = XCALLOC(MTYPE_PBR_VALMASK,
sizeof(struct bgp_pbr_val_mask));
if (type_entry == FLOWSPEC_TCP_FLAGS) {
and_valmask->val = TCP_HEADER_ALL_FLAGS;
SET_FLAG(and_valmask->mask, CHECK_FLAG(TCP_HEADER_ALL_FLAGS, ~(value)));
} else if (type_entry == FLOWSPEC_DSCP ||
type_entry == FLOWSPEC_FLOW_LABEL ||
type_entry == FLOWSPEC_FRAGMENT ||
type_entry == FLOWSPEC_PKT_LEN) {
and_valmask->val = value;
and_valmask->mask = 1; /* inverse */
}
listnode_add(or_valmask, and_valmask);
} else if (type_entry == FLOWSPEC_ICMP_CODE ||
type_entry == FLOWSPEC_ICMP_TYPE)
return false;
return true;
}
/* TCP : FIN and SYN -> val = ALL; mask = 3
* TCP : not (FIN and SYN) -> val = ALL; mask = ALL & ~(FIN|RST)
* other variables type: dscp, pkt len, fragment, flow label
* - value is copied in bgp_pbr_val_mask->val value
* - if negate form is identifierd, bgp_pbr_val_mask->mask set to 1
*/
static bool bgp_pbr_extract_enumerate_unary(struct bgp_pbr_match_val list[],
int num, uint8_t unary_operator,
void *valmask, uint8_t type_entry)
{
int i = 0;
struct bgp_pbr_val_mask *and_valmask = NULL;
struct list *or_valmask = NULL;
bool ret;
if (valmask) {
if (unary_operator == OPERATOR_UNARY_AND) {
and_valmask = (struct bgp_pbr_val_mask *)valmask;
memset(and_valmask, 0, sizeof(struct bgp_pbr_val_mask));
} else if (unary_operator == OPERATOR_UNARY_OR) {
or_valmask = (struct list *)valmask;
}
}
for (i = 0; i < num; i++) {
if (i != 0 && list[i].unary_operator !=
unary_operator)
return false;
if (!CHECK_FLAG(list[i].compare_operator, OPERATOR_COMPARE_EQUAL_TO) &&
!CHECK_FLAG(list[i].compare_operator, OPERATOR_COMPARE_EXACT_MATCH)) {
if (CHECK_FLAG(list[i].compare_operator, OPERATOR_COMPARE_LESS_THAN) &&
CHECK_FLAG(list[i].compare_operator, OPERATOR_COMPARE_GREATER_THAN)) {
ret = bgp_pbr_extract_enumerate_unary_opposite(
unary_operator, and_valmask,
or_valmask, list[i].value,
type_entry);
if (!ret)
return ret;
continue;
}
return false;
}
if (unary_operator == OPERATOR_UNARY_AND && and_valmask) {
if (type_entry == FLOWSPEC_TCP_FLAGS)
SET_FLAG(and_valmask->mask,
CHECK_FLAG(TCP_HEADER_ALL_FLAGS, list[i].value));
} else if (unary_operator == OPERATOR_UNARY_OR && or_valmask) {
and_valmask = XCALLOC(MTYPE_PBR_VALMASK,
sizeof(struct bgp_pbr_val_mask));
if (type_entry == FLOWSPEC_TCP_FLAGS) {
and_valmask->val = TCP_HEADER_ALL_FLAGS;
SET_FLAG(and_valmask->mask,
CHECK_FLAG(TCP_HEADER_ALL_FLAGS, list[i].value));
} else if (type_entry == FLOWSPEC_DSCP ||
type_entry == FLOWSPEC_FLOW_LABEL ||
type_entry == FLOWSPEC_ICMP_TYPE ||
type_entry == FLOWSPEC_ICMP_CODE ||
type_entry == FLOWSPEC_FRAGMENT ||
type_entry == FLOWSPEC_PKT_LEN)
and_valmask->val = list[i].value;
listnode_add(or_valmask, and_valmask);
}
}
if (unary_operator == OPERATOR_UNARY_AND && and_valmask
&& type_entry == FLOWSPEC_TCP_FLAGS)
and_valmask->val = TCP_HEADER_ALL_FLAGS;
return true;
}
/* if unary operator can either be UNARY_OR/AND/OR-AND.
* in the latter case, combinationf of both is not handled
*/
static bool bgp_pbr_extract_enumerate(struct bgp_pbr_match_val list[],
int num, uint8_t unary_operator,
void *valmask, uint8_t type_entry)
{
bool ret;
uint8_t unary_operator_val;
bool double_check = false;
if (CHECK_FLAG(unary_operator, OPERATOR_UNARY_OR) &&
CHECK_FLAG(unary_operator, OPERATOR_UNARY_AND)) {
unary_operator_val = OPERATOR_UNARY_AND;
double_check = true;
} else
unary_operator_val = unary_operator;
ret = bgp_pbr_extract_enumerate_unary(list, num, unary_operator_val,
valmask, type_entry);
if (!ret && double_check)
ret = bgp_pbr_extract_enumerate_unary(list, num,
OPERATOR_UNARY_OR,
valmask,
type_entry);
return ret;
}
/* returns the unary operator that is in the list
* return 0 if both operators are used
*/
static uint8_t bgp_pbr_match_val_get_operator(struct bgp_pbr_match_val list[],
int num)
{
int i;
uint8_t unary_operator = OPERATOR_UNARY_AND;
for (i = 0; i < num; i++) {
if (i == 0)
continue;
if (CHECK_FLAG(list[i].unary_operator, OPERATOR_UNARY_OR))
unary_operator = OPERATOR_UNARY_OR;
if ((CHECK_FLAG(list[i].unary_operator, OPERATOR_UNARY_AND) &&
unary_operator == OPERATOR_UNARY_OR) ||
(CHECK_FLAG(list[i].unary_operator, OPERATOR_UNARY_OR) &&
unary_operator == OPERATOR_UNARY_AND))
return 0;
}
return unary_operator;
}
/* return true if extraction ok
*/
static bool bgp_pbr_extract(struct bgp_pbr_match_val list[],
int num,
struct bgp_pbr_range_port *range)
{
int i = 0;
bool exact_match = false;
if (range)
memset(range, 0, sizeof(struct bgp_pbr_range_port));
if (num > 2)
return false;
for (i = 0; i < num; i++) {
if (i != 0 && (list[i].compare_operator ==
OPERATOR_COMPARE_EQUAL_TO))
return false;
if (i == 0 && (list[i].compare_operator ==
OPERATOR_COMPARE_EQUAL_TO)) {
if (range)
range->min_port = list[i].value;
exact_match = true;
}
if (exact_match && i > 0)
return false;
if (list[i].compare_operator ==
(OPERATOR_COMPARE_GREATER_THAN +
OPERATOR_COMPARE_EQUAL_TO)) {
if (range)
range->min_port = list[i].value;
} else if (list[i].compare_operator ==
(OPERATOR_COMPARE_LESS_THAN +
OPERATOR_COMPARE_EQUAL_TO)) {
if (range)
range->max_port = list[i].value;
} else if (list[i].compare_operator ==
OPERATOR_COMPARE_LESS_THAN) {
if (range)
range->max_port = list[i].value - 1;
} else if (list[i].compare_operator ==
OPERATOR_COMPARE_GREATER_THAN) {
if (range)
range->min_port = list[i].value + 1;
}
}
return true;
}
static int bgp_pbr_validate_policy_route(struct bgp_pbr_entry_main *api)
{
bool enumerate_icmp = false;
if (api->type == BGP_PBR_UNDEFINED) {
if (BGP_DEBUG(pbr, PBR))
zlog_debug("BGP: pbr entry undefined. cancel.");
return 0;
}
/* because bgp pbr entry may contain unsupported
* combinations, a message will be displayed here if
* not supported.
* for now, only match/set supported is
* - combination src/dst => redirect nexthop [ + rate]
* - combination src/dst => redirect VRF [ + rate]
* - combination src/dst => drop
* - combination srcport + @IP
*/
if (api->match_protocol_num > 1) {
if (BGP_DEBUG(pbr, PBR))
zlog_debug("BGP: match protocol operations:multiple protocols ( %d). ignoring.",
api->match_protocol_num);
return 0;
}
if (api->src_prefix_offset > 0 ||
api->dst_prefix_offset > 0) {
if (BGP_DEBUG(pbr, PBR))
zlog_debug("BGP: match prefix offset:"
"implementation does not support it.");
return 0;
}
if (api->match_protocol_num == 1 &&
api->protocol[0].value != PROTOCOL_UDP &&
api->protocol[0].value != PROTOCOL_ICMP &&
api->protocol[0].value != PROTOCOL_ICMPV6 &&
api->protocol[0].value != PROTOCOL_TCP) {
if (BGP_DEBUG(pbr, PBR))
zlog_debug("BGP: match protocol operations:protocol (%d) not supported. ignoring",
api->match_protocol_num);
return 0;
}
if (!bgp_pbr_extract(api->src_port, api->match_src_port_num, NULL)) {
if (BGP_DEBUG(pbr, PBR))
zlog_debug("BGP: match src port operations:too complex. ignoring.");
return 0;
}
if (!bgp_pbr_extract(api->dst_port, api->match_dst_port_num, NULL)) {
if (BGP_DEBUG(pbr, PBR))
zlog_debug("BGP: match dst port operations:too complex. ignoring.");
return 0;
}
if (!bgp_pbr_extract_enumerate(api->tcpflags,
api->match_tcpflags_num,
OPERATOR_UNARY_AND |
OPERATOR_UNARY_OR, NULL,
FLOWSPEC_TCP_FLAGS)) {
if (BGP_DEBUG(pbr, PBR))
zlog_debug("BGP: match tcp flags:too complex. ignoring.");
return 0;
}
if (!bgp_pbr_extract(api->icmp_type, api->match_icmp_type_num, NULL)) {
if (!bgp_pbr_extract_enumerate(api->icmp_type,
api->match_icmp_type_num,
OPERATOR_UNARY_OR, NULL,
FLOWSPEC_ICMP_TYPE)) {
if (BGP_DEBUG(pbr, PBR))
zlog_debug("BGP: match icmp type operations:too complex. ignoring.");
return 0;
}
enumerate_icmp = true;
}
if (!bgp_pbr_extract(api->icmp_code, api->match_icmp_code_num, NULL)) {
if (!bgp_pbr_extract_enumerate(api->icmp_code,
api->match_icmp_code_num,
OPERATOR_UNARY_OR, NULL,
FLOWSPEC_ICMP_CODE)) {
if (BGP_DEBUG(pbr, PBR))
zlog_debug("BGP: match icmp code operations:too complex. ignoring.");
return 0;
} else if (api->match_icmp_type_num > 1 &&
!enumerate_icmp) {
if (BGP_DEBUG(pbr, PBR))
zlog_debug("BGP: match icmp code is enumerate, and icmp type is not. too complex. ignoring.");
return 0;
}
}
if (!bgp_pbr_extract(api->port, api->match_port_num, NULL)) {
if (BGP_DEBUG(pbr, PBR))
zlog_debug("BGP: match port operations:too complex. ignoring.");
return 0;
}
if (api->match_packet_length_num) {
bool ret;
ret = bgp_pbr_extract(api->packet_length,
api->match_packet_length_num, NULL);
if (!ret)
ret = bgp_pbr_extract_enumerate(api->packet_length,
api->match_packet_length_num,
OPERATOR_UNARY_OR
| OPERATOR_UNARY_AND,
NULL, FLOWSPEC_PKT_LEN);
if (!ret) {
if (BGP_DEBUG(pbr, PBR))
zlog_debug("BGP: match packet length operations:too complex. ignoring.");
return 0;
}
}
if (api->match_dscp_num) {
if (!bgp_pbr_extract_enumerate(api->dscp, api->match_dscp_num,
OPERATOR_UNARY_OR | OPERATOR_UNARY_AND,
NULL, FLOWSPEC_DSCP)) {
if (BGP_DEBUG(pbr, PBR))
zlog_debug("BGP: match DSCP operations:too complex. ignoring.");
return 0;
}
}
if (api->match_flowlabel_num) {
if (api->afi == AFI_IP) {
if (BGP_DEBUG(pbr, PBR))
zlog_debug("BGP: match Flow Label operations:"
"Not for IPv4.");
return 0;
}
if (!bgp_pbr_extract_enumerate(api->flow_label,
api->match_flowlabel_num,
OPERATOR_UNARY_OR | OPERATOR_UNARY_AND,
NULL, FLOWSPEC_FLOW_LABEL)) {
if (BGP_DEBUG(pbr, PBR))
zlog_debug("BGP: match FlowLabel operations:"
"too complex. ignoring.");
return 0;
}
if (BGP_DEBUG(pbr, PBR))
zlog_debug("BGP: match FlowLabel operations "
"not supported. ignoring.");
return 0;
}
if (api->match_fragment_num) {
char fail_str[64];
bool success;
success = bgp_pbr_extract_enumerate(api->fragment,
api->match_fragment_num,
OPERATOR_UNARY_OR
| OPERATOR_UNARY_AND,
NULL, FLOWSPEC_FRAGMENT);
if (success) {
int i;
for (i = 0; i < api->match_fragment_num; i++) {
if (api->fragment[i].value != 1 &&
api->fragment[i].value != 2 &&
api->fragment[i].value != 4 &&
api->fragment[i].value != 8) {
success = false;
snprintf(
fail_str, sizeof(fail_str),
"Value not valid (%d) for this implementation",
api->fragment[i].value);
}
if (api->afi == AFI_IP6 &&
api->fragment[i].value == 1) {
success = false;
snprintf(fail_str, sizeof(fail_str),
"IPv6 dont fragment match invalid (%d)",
api->fragment[i].value);
}
}
if (api->afi == AFI_IP6) {
success = false;
snprintf(fail_str, sizeof(fail_str),
"%s", IPV6_FRAGMENT_INVALID);
}
} else
snprintf(fail_str, sizeof(fail_str),
"too complex. ignoring");
if (!success) {
if (BGP_DEBUG(pbr, PBR))
zlog_debug("BGP: match fragment operation (%d) %s",
api->match_fragment_num,
fail_str);
return 0;
}
}
/* no combinations with both src_port and dst_port
* or port with src_port and dst_port
*/
if (api->match_src_port_num + api->match_dst_port_num +
api->match_port_num > 3) {
if (BGP_DEBUG(pbr, PBR))
zlog_debug("BGP: match multiple port operations: too complex. ignoring.");
return 0;
}
if ((api->match_src_port_num || api->match_dst_port_num
|| api->match_port_num) && (api->match_icmp_type_num
|| api->match_icmp_code_num)) {
if (BGP_DEBUG(pbr, PBR))
zlog_debug("BGP: match multiple port/imcp operations: too complex. ignoring.");
return 0;
}
/* iprule only supports redirect IP */
if (api->type == BGP_PBR_IPRULE) {
int i;
for (i = 0; i < api->action_num; i++) {
if (api->actions[i].action == ACTION_TRAFFICRATE &&
api->actions[i].u.r.rate == 0) {
if (BGP_DEBUG(pbr, PBR)) {
bgp_pbr_print_policy_route(api);
zlog_debug("BGP: iprule match actions drop not supported");
}
return 0;
}
if (api->actions[i].action == ACTION_MARKING) {
if (BGP_DEBUG(pbr, PBR)) {
bgp_pbr_print_policy_route(api);
zlog_warn("PBR: iprule set DSCP/Flow Label %u not supported",
api->actions[i].u.marking_dscp);
}
}
if (api->actions[i].action == ACTION_REDIRECT) {
if (BGP_DEBUG(pbr, PBR)) {
bgp_pbr_print_policy_route(api);
zlog_warn("PBR: iprule redirect VRF %u not supported",
api->actions[i].u.redirect_vrf);
}
}
}
} else if (!CHECK_FLAG(api->match_bitmask, PREFIX_SRC_PRESENT) &&
!CHECK_FLAG(api->match_bitmask, PREFIX_DST_PRESENT)) {
if (BGP_DEBUG(pbr, PBR)) {
bgp_pbr_print_policy_route(api);
zlog_debug("BGP: match actions without src or dst address can not operate. ignoring.");
}
return 0;
}
return 1;
}
/* return -1 if build or validation failed */
int bgp_pbr_build_and_validate_entry(const struct prefix *p,
struct bgp_path_info *path,
struct bgp_pbr_entry_main *api)
{
int ret;
uint32_t i, action_count = 0;
struct ecommunity *ecom;
struct ecommunity_val *ecom_eval;
struct bgp_pbr_entry_action *api_action;
struct prefix *src = NULL, *dst = NULL;
int valid_prefix = 0;
struct bgp_pbr_entry_action *api_action_redirect_ip = NULL;
bool discard_action_found = false;
afi_t afi = family2afi(p->u.prefix_flowspec.family);
/* extract match from flowspec entries */
ret = bgp_flowspec_match_rules_fill((uint8_t *)p->u.prefix_flowspec.ptr,
p->u.prefix_flowspec.prefixlen, api, afi);
if (ret < 0)
return -1;
/* extract actiosn from flowspec ecom list */
if (path && bgp_attr_get_ecommunity(path->attr)) {
ecom = bgp_attr_get_ecommunity(path->attr);
for (i = 0; i < ecom->size; i++) {
ecom_eval = (struct ecommunity_val *)
(ecom->val + (i * ECOMMUNITY_SIZE));
action_count++;
if (action_count > ACTIONS_MAX_NUM) {
if (BGP_DEBUG(pbr, PBR_ERROR))
flog_err(
EC_BGP_FLOWSPEC_PACKET,
"%s: %s (max %u)",
__func__,
FSPEC_ACTION_EXCEED_LIMIT,
action_count);
break;
}
api_action = &api->actions[action_count - 1];
if ((ecom_eval->val[1] == ECOMMUNITY_REDIRECT_VRF) &&
(ecom_eval->val[0] == ECOMMUNITY_ENCODE_TRANS_EXP ||
ecom_eval->val[0] ==
ECOMMUNITY_EXTENDED_COMMUNITY_PART_2 ||
ecom_eval->val[0] ==
ECOMMUNITY_EXTENDED_COMMUNITY_PART_3)) {
struct ecommunity *eckey = ecommunity_new();
struct ecommunity_val ecom_copy;
memcpy(&ecom_copy, ecom_eval,
sizeof(struct ecommunity_val));
UNSET_FLAG(ecom_copy.val[0], ECOMMUNITY_ENCODE_TRANS_EXP);
ecom_copy.val[1] = ECOMMUNITY_ROUTE_TARGET;
ecommunity_add_val(eckey, &ecom_copy,
false, false);
api_action->action = ACTION_REDIRECT;
api_action->u.redirect_vrf =
get_first_vrf_for_redirect_with_rt(
eckey);
ecommunity_free(&eckey);
} else if ((ecom_eval->val[0] ==
ECOMMUNITY_ENCODE_REDIRECT_IP_NH) &&
(ecom_eval->val[1] ==
ECOMMUNITY_REDIRECT_IP_NH)) {
/* in case the 2 ecom present,
* do not overwrite
* draft-ietf-idr-flowspec-redirect
*/
if (api_action_redirect_ip &&
p->u.prefix_flowspec.family == AF_INET) {
if (api_action_redirect_ip->u
.zr.redirect_ip_v4.s_addr
!= INADDR_ANY)
continue;
if (path->attr->nexthop.s_addr
== INADDR_ANY)
continue;
api_action_redirect_ip->u.zr
.redirect_ip_v4.s_addr =
path->attr->nexthop.s_addr;
api_action_redirect_ip->u.zr.duplicate
= ecom_eval->val[7];
continue;
} else if (api_action_redirect_ip &&
p->u.prefix_flowspec.family == AF_INET6) {
if (memcmp(&api_action_redirect_ip->u
.zr.redirect_ip_v6,
&in6addr_any,
sizeof(struct in6_addr)))
continue;
if (path->attr->mp_nexthop_len == 0 ||
path->attr->mp_nexthop_len ==
BGP_ATTR_NHLEN_IPV4 ||
path->attr->mp_nexthop_len ==
BGP_ATTR_NHLEN_VPNV4)
continue;
memcpy(&api_action_redirect_ip->u
.zr.redirect_ip_v6,
&path->attr->mp_nexthop_global,
sizeof(struct in6_addr));
api_action_redirect_ip->u.zr.duplicate
= ecom_eval->val[7];
continue;
} else if (p->u.prefix_flowspec.family ==
AF_INET) {
api_action->action = ACTION_REDIRECT_IP;
api_action->u.zr.redirect_ip_v4.s_addr =
path->attr->nexthop.s_addr;
api_action->u.zr.duplicate =
ecom_eval->val[7];
api_action_redirect_ip = api_action;
} else if (p->u.prefix_flowspec.family ==
AF_INET6) {
api_action->action = ACTION_REDIRECT_IP;
memcpy(&api_action->u
.zr.redirect_ip_v6,
&path->attr->mp_nexthop_global,
sizeof(struct in6_addr));
api_action->u.zr.duplicate
= ecom_eval->val[7];
api_action_redirect_ip = api_action;
}
} else if ((ecom_eval->val[0] == ECOMMUNITY_ENCODE_IP) &&
(ecom_eval->val[1] ==
ECOMMUNITY_FLOWSPEC_REDIRECT_IPV4)) {
/* in case the 2 ecom present,
* overwrite simpson draft
* update redirect ip fields
*/
if (api_action_redirect_ip) {
memcpy(&(api_action_redirect_ip->u
.zr.redirect_ip_v4.s_addr),
(ecom_eval->val+2), 4);
api_action_redirect_ip->u
.zr.duplicate =
ecom_eval->val[7];
continue;
} else {
api_action->action = ACTION_REDIRECT_IP;
memcpy(&(api_action->u
.zr.redirect_ip_v4.s_addr),
(ecom_eval->val+2), 4);
api_action->u.zr.duplicate =
ecom_eval->val[7];
api_action_redirect_ip = api_action;
}
} else {
if (ecom_eval->val[0] !=
ECOMMUNITY_ENCODE_TRANS_EXP)
continue;
ret = ecommunity_fill_pbr_action(ecom_eval,
api_action,
afi);
if (ret != 0)
continue;
if ((api_action->action == ACTION_TRAFFICRATE)
&& api->actions[i].u.r.rate == 0)
discard_action_found = true;
}
api->action_num++;
}
}
if (path && path->attr && bgp_attr_get_ipv6_ecommunity(path->attr)) {
struct ecommunity_val_ipv6 *ipv6_ecom_eval;
ecom = bgp_attr_get_ipv6_ecommunity(path->attr);
for (i = 0; i < ecom->size; i++) {
ipv6_ecom_eval = (struct ecommunity_val_ipv6 *)
(ecom->val + (i * ecom->unit_size));
action_count++;
if (action_count > ACTIONS_MAX_NUM) {
if (BGP_DEBUG(pbr, PBR_ERROR))
flog_err(
EC_BGP_FLOWSPEC_PACKET,
"%s: flowspec actions exceeds limit (max %u)",
__func__, action_count);
break;
}
api_action = &api->actions[action_count - 1];
if ((ipv6_ecom_eval->val[1] ==
ECOMMUNITY_FLOWSPEC_REDIRECT_IPV6) &&
(ipv6_ecom_eval->val[0] ==
ECOMMUNITY_ENCODE_TRANS_EXP)) {
struct ecommunity *eckey = ecommunity_new();
struct ecommunity_val_ipv6 ecom_copy;
eckey->unit_size = IPV6_ECOMMUNITY_SIZE;
memcpy(&ecom_copy, ipv6_ecom_eval,
sizeof(struct ecommunity_val_ipv6));
ecom_copy.val[1] = ECOMMUNITY_ROUTE_TARGET;
ecommunity_add_val_ipv6(eckey, &ecom_copy,
false, false);
api_action->action = ACTION_REDIRECT;
api_action->u.redirect_vrf =
get_first_vrf_for_redirect_with_rt(
eckey);
ecommunity_free(&eckey);
api->action_num++;
}
}
}
/* if ECOMMUNITY_TRAFFIC_RATE = 0 as action
* then reduce the API action list to that action
*/
if (api->action_num > 1 && discard_action_found) {
api->action_num = 1;
memset(&api->actions[0], 0,
sizeof(struct bgp_pbr_entry_action));
api->actions[0].action = ACTION_TRAFFICRATE;
}
/* validate if incoming matc/action is compatible
* with our policy routing engine
*/
if (!bgp_pbr_validate_policy_route(api))
return -1;
/* check inconsistency in the match rule */
if (CHECK_FLAG(api->match_bitmask, PREFIX_SRC_PRESENT)) {
src = &api->src_prefix;
afi = family2afi(src->family);
valid_prefix = 1;
}
if (CHECK_FLAG(api->match_bitmask, PREFIX_DST_PRESENT)) {
dst = &api->dst_prefix;
if (valid_prefix && afi != family2afi(dst->family)) {
if (BGP_DEBUG(pbr, PBR)) {
bgp_pbr_print_policy_route(api);
zlog_debug("%s: inconsistency: no match for afi src and dst (%u/%u)",
__func__, afi, family2afi(dst->family));
}
return -1;
}
}
return 0;
}
static void bgp_pbr_match_entry_free(void *arg)
{
struct bgp_pbr_match_entry *bpme;
bpme = (struct bgp_pbr_match_entry *)arg;
if (bpme->installed) {
bgp_send_pbr_ipset_entry_match(bpme, false);
bpme->installed = false;
bpme->backpointer = NULL;
}
XFREE(MTYPE_PBR_MATCH_ENTRY, bpme);
}
static void bgp_pbr_match_free(void *arg)
{
struct bgp_pbr_match *bpm;
bpm = (struct bgp_pbr_match *)arg;
hash_clean(bpm->entry_hash, bgp_pbr_match_entry_free);
if (hashcount(bpm->entry_hash) == 0) {
/* delete iptable entry first */
/* then delete ipset match */
if (bpm->installed) {
if (bpm->installed_in_iptable) {
bgp_send_pbr_iptable(bpm->action,
bpm, false);
bpm->installed_in_iptable = false;
bpm->action->refcnt--;
}
bgp_send_pbr_ipset_match(bpm, false);