forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathseg6_local.c
2712 lines (2182 loc) · 68 KB
/
seg6_local.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
/*
* SR-IPv6 implementation
*
* Authors:
* David Lebrun <david.lebrun@uclouvain.be>
* eBPF support: Mathieu Xhonneux <m.xhonneux@gmail.com>
*/
#include <linux/filter.h>
#include <linux/types.h>
#include <linux/skbuff.h>
#include <linux/net.h>
#include <linux/module.h>
#include <net/ip.h>
#include <net/lwtunnel.h>
#include <net/netevent.h>
#include <net/netns/generic.h>
#include <net/ip6_fib.h>
#include <net/route.h>
#include <net/seg6.h>
#include <linux/seg6.h>
#include <linux/seg6_local.h>
#include <net/addrconf.h>
#include <net/ip6_route.h>
#include <net/dst_cache.h>
#include <net/ip_tunnels.h>
#ifdef CONFIG_IPV6_SEG6_HMAC
#include <net/seg6_hmac.h>
#endif
#include <net/seg6_local.h>
#include <linux/etherdevice.h>
#include <linux/bpf.h>
#include <linux/netfilter.h>
#define SEG6_F_ATTR(i) BIT(i)
struct seg6_local_lwt;
/* callbacks used for customizing the creation and destruction of a behavior */
struct seg6_local_lwtunnel_ops {
int (*build_state)(struct seg6_local_lwt *slwt, const void *cfg,
struct netlink_ext_ack *extack);
void (*destroy_state)(struct seg6_local_lwt *slwt);
};
struct seg6_action_desc {
int action;
unsigned long attrs;
/* The optattrs field is used for specifying all the optional
* attributes supported by a specific behavior.
* It means that if one of these attributes is not provided in the
* netlink message during the behavior creation, no errors will be
* returned to the userspace.
*
* Each attribute can be only of two types (mutually exclusive):
* 1) required or 2) optional.
* Every user MUST obey to this rule! If you set an attribute as
* required the same attribute CANNOT be set as optional and vice
* versa.
*/
unsigned long optattrs;
int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt);
int static_headroom;
struct seg6_local_lwtunnel_ops slwt_ops;
};
struct bpf_lwt_prog {
struct bpf_prog *prog;
char *name;
};
/* default length values (expressed in bits) for both Locator-Block and
* Locator-Node Function.
*
* Both SEG6_LOCAL_LCBLOCK_DBITS and SEG6_LOCAL_LCNODE_FN_DBITS *must* be:
* i) greater than 0;
* ii) evenly divisible by 8. In other terms, the lengths of the
* Locator-Block and Locator-Node Function must be byte-aligned (we can
* relax this constraint in the future if really needed).
*
* Moreover, a third condition must hold:
* iii) SEG6_LOCAL_LCBLOCK_DBITS + SEG6_LOCAL_LCNODE_FN_DBITS <= 128.
*
* The correctness of SEG6_LOCAL_LCBLOCK_DBITS and SEG6_LOCAL_LCNODE_FN_DBITS
* values are checked during the kernel compilation. If the compilation stops,
* check the value of these parameters to see if they meet conditions (i), (ii)
* and (iii).
*/
#define SEG6_LOCAL_LCBLOCK_DBITS 32
#define SEG6_LOCAL_LCNODE_FN_DBITS 16
/* The following next_csid_chk_{cntr,lcblock,lcblock_fn}_bits macros can be
* used directly to check whether the lengths (in bits) of Locator-Block and
* Locator-Node Function are valid according to (i), (ii), (iii).
*/
#define next_csid_chk_cntr_bits(blen, flen) \
((blen) + (flen) > 128)
#define next_csid_chk_lcblock_bits(blen) \
({ \
typeof(blen) __tmp = blen; \
(!__tmp || __tmp > 120 || (__tmp & 0x07)); \
})
#define next_csid_chk_lcnode_fn_bits(flen) \
next_csid_chk_lcblock_bits(flen)
/* flag indicating that flavors are set up for a given End* behavior */
#define SEG6_F_LOCAL_FLAVORS SEG6_F_ATTR(SEG6_LOCAL_FLAVORS)
#define SEG6_F_LOCAL_FLV_OP(flvname) BIT(SEG6_LOCAL_FLV_OP_##flvname)
#define SEG6_F_LOCAL_FLV_NEXT_CSID SEG6_F_LOCAL_FLV_OP(NEXT_CSID)
#define SEG6_F_LOCAL_FLV_PSP SEG6_F_LOCAL_FLV_OP(PSP)
/* Supported RFC8986 Flavor operations are reported in this bitmask */
#define SEG6_LOCAL_FLV8986_SUPP_OPS SEG6_F_LOCAL_FLV_PSP
#define SEG6_LOCAL_END_FLV_SUPP_OPS (SEG6_F_LOCAL_FLV_NEXT_CSID | \
SEG6_LOCAL_FLV8986_SUPP_OPS)
#define SEG6_LOCAL_END_X_FLV_SUPP_OPS SEG6_F_LOCAL_FLV_NEXT_CSID
struct seg6_flavors_info {
/* Flavor operations */
__u32 flv_ops;
/* Locator-Block length, expressed in bits */
__u8 lcblock_bits;
/* Locator-Node Function length, expressed in bits*/
__u8 lcnode_func_bits;
};
enum seg6_end_dt_mode {
DT_INVALID_MODE = -EINVAL,
DT_LEGACY_MODE = 0,
DT_VRF_MODE = 1,
};
struct seg6_end_dt_info {
enum seg6_end_dt_mode mode;
struct net *net;
/* VRF device associated to the routing table used by the SRv6
* End.DT4/DT6 behavior for routing IPv4/IPv6 packets.
*/
int vrf_ifindex;
int vrf_table;
/* tunneled packet family (IPv4 or IPv6).
* Protocol and header length are inferred from family.
*/
u16 family;
};
struct pcpu_seg6_local_counters {
u64_stats_t packets;
u64_stats_t bytes;
u64_stats_t errors;
struct u64_stats_sync syncp;
};
/* This struct groups all the SRv6 Behavior counters supported so far.
*
* put_nla_counters() makes use of this data structure to collect all counter
* values after the per-CPU counter evaluation has been performed.
* Finally, each counter value (in seg6_local_counters) is stored in the
* corresponding netlink attribute and sent to user space.
*
* NB: we don't want to expose this structure to user space!
*/
struct seg6_local_counters {
__u64 packets;
__u64 bytes;
__u64 errors;
};
#define seg6_local_alloc_pcpu_counters(__gfp) \
__netdev_alloc_pcpu_stats(struct pcpu_seg6_local_counters, \
((__gfp) | __GFP_ZERO))
#define SEG6_F_LOCAL_COUNTERS SEG6_F_ATTR(SEG6_LOCAL_COUNTERS)
struct seg6_local_lwt {
int action;
struct ipv6_sr_hdr *srh;
int table;
struct in_addr nh4;
struct in6_addr nh6;
int iif;
int oif;
struct bpf_lwt_prog bpf;
#ifdef CONFIG_NET_L3_MASTER_DEV
struct seg6_end_dt_info dt_info;
#endif
struct seg6_flavors_info flv_info;
struct pcpu_seg6_local_counters __percpu *pcpu_counters;
int headroom;
struct seg6_action_desc *desc;
/* unlike the required attrs, we have to track the optional attributes
* that have been effectively parsed.
*/
unsigned long parsed_optattrs;
};
static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt)
{
return (struct seg6_local_lwt *)lwt->data;
}
static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb)
{
struct ipv6_sr_hdr *srh;
srh = seg6_get_srh(skb, IP6_FH_F_SKIP_RH);
if (!srh)
return NULL;
#ifdef CONFIG_IPV6_SEG6_HMAC
if (!seg6_hmac_validate_skb(skb))
return NULL;
#endif
return srh;
}
static bool decap_and_validate(struct sk_buff *skb, int proto)
{
struct ipv6_sr_hdr *srh;
unsigned int off = 0;
srh = seg6_get_srh(skb, 0);
if (srh && srh->segments_left > 0)
return false;
#ifdef CONFIG_IPV6_SEG6_HMAC
if (srh && !seg6_hmac_validate_skb(skb))
return false;
#endif
if (ipv6_find_hdr(skb, &off, proto, NULL, NULL) < 0)
return false;
if (!pskb_pull(skb, off))
return false;
skb_postpull_rcsum(skb, skb_network_header(skb), off);
skb_reset_network_header(skb);
skb_reset_transport_header(skb);
if (iptunnel_pull_offloads(skb))
return false;
return true;
}
static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr)
{
struct in6_addr *addr;
srh->segments_left--;
addr = srh->segments + srh->segments_left;
*daddr = *addr;
}
static int
seg6_lookup_any_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
u32 tbl_id, bool local_delivery)
{
struct net *net = dev_net(skb->dev);
struct ipv6hdr *hdr = ipv6_hdr(skb);
int flags = RT6_LOOKUP_F_HAS_SADDR;
struct dst_entry *dst = NULL;
struct rt6_info *rt;
struct flowi6 fl6;
int dev_flags = 0;
memset(&fl6, 0, sizeof(fl6));
fl6.flowi6_iif = skb->dev->ifindex;
fl6.daddr = nhaddr ? *nhaddr : hdr->daddr;
fl6.saddr = hdr->saddr;
fl6.flowlabel = ip6_flowinfo(hdr);
fl6.flowi6_mark = skb->mark;
fl6.flowi6_proto = hdr->nexthdr;
if (nhaddr)
fl6.flowi6_flags = FLOWI_FLAG_KNOWN_NH;
if (!tbl_id) {
dst = ip6_route_input_lookup(net, skb->dev, &fl6, skb, flags);
} else {
struct fib6_table *table;
table = fib6_get_table(net, tbl_id);
if (!table)
goto out;
rt = ip6_pol_route(net, table, 0, &fl6, skb, flags);
dst = &rt->dst;
}
/* we want to discard traffic destined for local packet processing,
* if @local_delivery is set to false.
*/
if (!local_delivery)
dev_flags |= IFF_LOOPBACK;
if (dst && (dst->dev->flags & dev_flags) && !dst->error) {
dst_release(dst);
dst = NULL;
}
out:
if (!dst) {
rt = net->ipv6.ip6_blk_hole_entry;
dst = &rt->dst;
dst_hold(dst);
}
skb_dst_drop(skb);
skb_dst_set(skb, dst);
return dst->error;
}
int seg6_lookup_nexthop(struct sk_buff *skb,
struct in6_addr *nhaddr, u32 tbl_id)
{
return seg6_lookup_any_nexthop(skb, nhaddr, tbl_id, false);
}
static __u8 seg6_flv_lcblock_octects(const struct seg6_flavors_info *finfo)
{
return finfo->lcblock_bits >> 3;
}
static __u8 seg6_flv_lcnode_func_octects(const struct seg6_flavors_info *finfo)
{
return finfo->lcnode_func_bits >> 3;
}
static bool seg6_next_csid_is_arg_zero(const struct in6_addr *addr,
const struct seg6_flavors_info *finfo)
{
__u8 fnc_octects = seg6_flv_lcnode_func_octects(finfo);
__u8 blk_octects = seg6_flv_lcblock_octects(finfo);
__u8 arg_octects;
int i;
arg_octects = 16 - blk_octects - fnc_octects;
for (i = 0; i < arg_octects; ++i) {
if (addr->s6_addr[blk_octects + fnc_octects + i] != 0x00)
return false;
}
return true;
}
/* assume that DA.Argument length > 0 */
static void seg6_next_csid_advance_arg(struct in6_addr *addr,
const struct seg6_flavors_info *finfo)
{
__u8 fnc_octects = seg6_flv_lcnode_func_octects(finfo);
__u8 blk_octects = seg6_flv_lcblock_octects(finfo);
/* advance DA.Argument */
memmove(&addr->s6_addr[blk_octects],
&addr->s6_addr[blk_octects + fnc_octects],
16 - blk_octects - fnc_octects);
memset(&addr->s6_addr[16 - fnc_octects], 0x00, fnc_octects);
}
static int input_action_end_finish(struct sk_buff *skb,
struct seg6_local_lwt *slwt)
{
seg6_lookup_nexthop(skb, NULL, 0);
return dst_input(skb);
}
static int input_action_end_core(struct sk_buff *skb,
struct seg6_local_lwt *slwt)
{
struct ipv6_sr_hdr *srh;
srh = get_and_validate_srh(skb);
if (!srh)
goto drop;
advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
return input_action_end_finish(skb, slwt);
drop:
kfree_skb(skb);
return -EINVAL;
}
static int end_next_csid_core(struct sk_buff *skb, struct seg6_local_lwt *slwt)
{
const struct seg6_flavors_info *finfo = &slwt->flv_info;
struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
if (seg6_next_csid_is_arg_zero(daddr, finfo))
return input_action_end_core(skb, slwt);
/* update DA */
seg6_next_csid_advance_arg(daddr, finfo);
return input_action_end_finish(skb, slwt);
}
static int input_action_end_x_finish(struct sk_buff *skb,
struct seg6_local_lwt *slwt)
{
seg6_lookup_nexthop(skb, &slwt->nh6, 0);
return dst_input(skb);
}
static int input_action_end_x_core(struct sk_buff *skb,
struct seg6_local_lwt *slwt)
{
struct ipv6_sr_hdr *srh;
srh = get_and_validate_srh(skb);
if (!srh)
goto drop;
advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
return input_action_end_x_finish(skb, slwt);
drop:
kfree_skb(skb);
return -EINVAL;
}
static int end_x_next_csid_core(struct sk_buff *skb,
struct seg6_local_lwt *slwt)
{
const struct seg6_flavors_info *finfo = &slwt->flv_info;
struct in6_addr *daddr = &ipv6_hdr(skb)->daddr;
if (seg6_next_csid_is_arg_zero(daddr, finfo))
return input_action_end_x_core(skb, slwt);
/* update DA */
seg6_next_csid_advance_arg(daddr, finfo);
return input_action_end_x_finish(skb, slwt);
}
static bool seg6_next_csid_enabled(__u32 fops)
{
return fops & SEG6_F_LOCAL_FLV_NEXT_CSID;
}
/* Processing of SRv6 End, End.X, and End.T behaviors can be extended through
* the flavors framework. These behaviors must report the subset of (flavor)
* operations they currently implement. In this way, if a user specifies a
* flavor combination that is not supported by a given End* behavior, the
* kernel refuses to instantiate the tunnel reporting the error.
*/
static int seg6_flv_supp_ops_by_action(int action, __u32 *fops)
{
switch (action) {
case SEG6_LOCAL_ACTION_END:
*fops = SEG6_LOCAL_END_FLV_SUPP_OPS;
break;
case SEG6_LOCAL_ACTION_END_X:
*fops = SEG6_LOCAL_END_X_FLV_SUPP_OPS;
break;
default:
return -EOPNOTSUPP;
}
return 0;
}
/* We describe the packet state in relation to the absence/presence of the SRH
* and the Segment Left (SL) field.
* For our purposes, it is not necessary to record the exact value of the SL
* when the SID List consists of two or more segments.
*/
enum seg6_local_pktinfo {
/* the order really matters! */
SEG6_LOCAL_PKTINFO_NOHDR = 0,
SEG6_LOCAL_PKTINFO_SL_ZERO,
SEG6_LOCAL_PKTINFO_SL_ONE,
SEG6_LOCAL_PKTINFO_SL_MORE,
__SEG6_LOCAL_PKTINFO_MAX,
};
#define SEG6_LOCAL_PKTINFO_MAX (__SEG6_LOCAL_PKTINFO_MAX - 1)
static enum seg6_local_pktinfo seg6_get_srh_pktinfo(struct ipv6_sr_hdr *srh)
{
__u8 sgl;
if (!srh)
return SEG6_LOCAL_PKTINFO_NOHDR;
sgl = srh->segments_left;
if (sgl < 2)
return SEG6_LOCAL_PKTINFO_SL_ZERO + sgl;
return SEG6_LOCAL_PKTINFO_SL_MORE;
}
enum seg6_local_flv_action {
SEG6_LOCAL_FLV_ACT_UNSPEC = 0,
SEG6_LOCAL_FLV_ACT_END,
SEG6_LOCAL_FLV_ACT_PSP,
SEG6_LOCAL_FLV_ACT_USP,
SEG6_LOCAL_FLV_ACT_USD,
__SEG6_LOCAL_FLV_ACT_MAX
};
#define SEG6_LOCAL_FLV_ACT_MAX (__SEG6_LOCAL_FLV_ACT_MAX - 1)
/* The action table for RFC8986 flavors (see the flv8986_act_tbl below)
* contains the actions (i.e. processing operations) to be applied on packets
* when flavors are configured for an End* behavior.
* By combining the pkinfo data and from the flavors mask, the macro
* computes the index used to access the elements (actions) stored in the
* action table. The index is structured as follows:
*
* index
* _______________/\________________
* / \
* +----------------+----------------+
* | pf | afm |
* +----------------+----------------+
* ph-1 ... p1 p0 fk-1 ... f1 f0
* MSB LSB
*
* where:
* - 'afm' (adjusted flavor mask) is the mask containing a combination of the
* RFC8986 flavors currently supported. 'afm' corresponds to the @fm
* argument of the macro whose value is righ-shifted by 1 bit. By doing so,
* we discard the SEG6_LOCAL_FLV_OP_UNSPEC flag (bit 0 in @fm) which is
* never used here;
* - 'pf' encodes the packet info (pktinfo) regarding the presence/absence of
* the SRH, SL = 0, etc. 'pf' is set with the value of @pf provided as
* argument to the macro.
*/
#define flv8986_act_tbl_idx(pf, fm) \
((((pf) << bits_per(SEG6_LOCAL_FLV8986_SUPP_OPS)) | \
((fm) & SEG6_LOCAL_FLV8986_SUPP_OPS)) >> SEG6_LOCAL_FLV_OP_PSP)
/* We compute the size of the action table by considering the RFC8986 flavors
* actually supported by the kernel. In this way, the size is automatically
* adjusted when new flavors are supported.
*/
#define FLV8986_ACT_TBL_SIZE \
roundup_pow_of_two(flv8986_act_tbl_idx(SEG6_LOCAL_PKTINFO_MAX, \
SEG6_LOCAL_FLV8986_SUPP_OPS))
/* tbl_cfg(act, pf, fm) macro is used to easily configure the action
* table; it accepts 3 arguments:
* i) @act, the suffix from SEG6_LOCAL_FLV_ACT_{act} representing
* the action that should be applied on the packet;
* ii) @pf, the suffix from SEG6_LOCAL_PKTINFO_{pf} reporting the packet
* info about the lack/presence of SRH, SRH with SL = 0, etc;
* iii) @fm, the mask of flavors.
*/
#define tbl_cfg(act, pf, fm) \
[flv8986_act_tbl_idx(SEG6_LOCAL_PKTINFO_##pf, \
(fm))] = SEG6_LOCAL_FLV_ACT_##act
/* shorthand for improving readability */
#define F_PSP SEG6_F_LOCAL_FLV_PSP
/* The table contains, for each combination of the pktinfo data and
* flavors, the action that should be taken on a packet (e.g.
* "standard" Endpoint processing, Penultimate Segment Pop, etc).
*
* By default, table entries not explicitly configured are initialized with the
* SEG6_LOCAL_FLV_ACT_UNSPEC action, which generally has the effect of
* discarding the processed packet.
*/
static const u8 flv8986_act_tbl[FLV8986_ACT_TBL_SIZE] = {
/* PSP variant for packet where SRH with SL = 1 */
tbl_cfg(PSP, SL_ONE, F_PSP),
/* End for packet where the SRH with SL > 1*/
tbl_cfg(END, SL_MORE, F_PSP),
};
#undef F_PSP
#undef tbl_cfg
/* For each flavor defined in RFC8986 (or a combination of them) an action is
* performed on the packet. The specific action depends on:
* - info extracted from the packet (i.e. pktinfo data) regarding the
* lack/presence of the SRH, and if the SRH is available, on the value of
* Segment Left field;
* - the mask of flavors configured for the specific SRv6 End* behavior.
*
* The function combines both the pkinfo and the flavors mask to evaluate the
* corresponding action to be taken on the packet.
*/
static enum seg6_local_flv_action
seg6_local_flv8986_act_lookup(enum seg6_local_pktinfo pinfo, __u32 flvmask)
{
unsigned long index;
/* check if the provided mask of flavors is supported */
if (unlikely(flvmask & ~SEG6_LOCAL_FLV8986_SUPP_OPS))
return SEG6_LOCAL_FLV_ACT_UNSPEC;
index = flv8986_act_tbl_idx(pinfo, flvmask);
if (unlikely(index >= FLV8986_ACT_TBL_SIZE))
return SEG6_LOCAL_FLV_ACT_UNSPEC;
return flv8986_act_tbl[index];
}
/* skb->data must be aligned with skb->network_header */
static bool seg6_pop_srh(struct sk_buff *skb, int srhoff)
{
struct ipv6_sr_hdr *srh;
struct ipv6hdr *iph;
__u8 srh_nexthdr;
int thoff = -1;
int srhlen;
int nhlen;
if (unlikely(srhoff < sizeof(*iph) ||
!pskb_may_pull(skb, srhoff + sizeof(*srh))))
return false;
srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
srhlen = ipv6_optlen(srh);
/* we are about to mangle the pkt, let's check if we can write on it */
if (unlikely(skb_ensure_writable(skb, srhoff + srhlen)))
return false;
/* skb_ensure_writable() may change skb pointers; evaluate srh again */
srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
srh_nexthdr = srh->nexthdr;
if (unlikely(!skb_transport_header_was_set(skb)))
goto pull;
nhlen = skb_network_header_len(skb);
/* we have to deal with the transport header: it could be set before
* the SRH, after the SRH, or within it (which is considered wrong,
* however).
*/
if (likely(nhlen <= srhoff))
thoff = nhlen;
else if (nhlen >= srhoff + srhlen)
/* transport_header is set after the SRH */
thoff = nhlen - srhlen;
else
/* transport_header falls inside the SRH; hence, we can't
* restore the transport_header pointer properly after
* SRH removing operation.
*/
return false;
pull:
/* we need to pop the SRH:
* 1) first of all, we pull out everything from IPv6 header up to SRH
* (included) evaluating also the rcsum;
* 2) we overwrite (and then remove) the SRH by properly moving the
* IPv6 along with any extension header that precedes the SRH;
* 3) At the end, we push back the pulled headers (except for SRH,
* obviously).
*/
skb_pull_rcsum(skb, srhoff + srhlen);
memmove(skb_network_header(skb) + srhlen, skb_network_header(skb),
srhoff);
skb_push(skb, srhoff);
skb_reset_network_header(skb);
skb_mac_header_rebuild(skb);
if (likely(thoff >= 0))
skb_set_transport_header(skb, thoff);
iph = ipv6_hdr(skb);
if (iph->nexthdr == NEXTHDR_ROUTING) {
iph->nexthdr = srh_nexthdr;
} else {
/* we must look for the extension header (EXTH, for short) that
* immediately precedes the SRH we have just removed.
* Then, we update the value of the EXTH nexthdr with the one
* contained in the SRH nexthdr.
*/
unsigned int off = sizeof(*iph);
struct ipv6_opt_hdr *hp, _hdr;
__u8 nexthdr = iph->nexthdr;
for (;;) {
if (unlikely(!ipv6_ext_hdr(nexthdr) ||
nexthdr == NEXTHDR_NONE))
return false;
hp = skb_header_pointer(skb, off, sizeof(_hdr), &_hdr);
if (unlikely(!hp))
return false;
if (hp->nexthdr == NEXTHDR_ROUTING) {
hp->nexthdr = srh_nexthdr;
break;
}
switch (nexthdr) {
case NEXTHDR_FRAGMENT:
fallthrough;
case NEXTHDR_AUTH:
/* we expect SRH before FRAG and AUTH */
return false;
default:
off += ipv6_optlen(hp);
break;
}
nexthdr = hp->nexthdr;
}
}
iph->payload_len = htons(skb->len - sizeof(struct ipv6hdr));
skb_postpush_rcsum(skb, iph, srhoff);
return true;
}
/* process the packet on the basis of the RFC8986 flavors set for the given
* SRv6 End behavior instance.
*/
static int end_flv8986_core(struct sk_buff *skb, struct seg6_local_lwt *slwt)
{
const struct seg6_flavors_info *finfo = &slwt->flv_info;
enum seg6_local_flv_action action;
enum seg6_local_pktinfo pinfo;
struct ipv6_sr_hdr *srh;
__u32 flvmask;
int srhoff;
srh = seg6_get_srh(skb, 0);
srhoff = srh ? ((unsigned char *)srh - skb->data) : 0;
pinfo = seg6_get_srh_pktinfo(srh);
#ifdef CONFIG_IPV6_SEG6_HMAC
if (srh && !seg6_hmac_validate_skb(skb))
goto drop;
#endif
flvmask = finfo->flv_ops;
if (unlikely(flvmask & ~SEG6_LOCAL_FLV8986_SUPP_OPS)) {
pr_warn_once("seg6local: invalid RFC8986 flavors\n");
goto drop;
}
/* retrieve the action triggered by the combination of pktinfo data and
* the flavors mask.
*/
action = seg6_local_flv8986_act_lookup(pinfo, flvmask);
switch (action) {
case SEG6_LOCAL_FLV_ACT_END:
/* process the packet as the "standard" End behavior */
advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
break;
case SEG6_LOCAL_FLV_ACT_PSP:
advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
if (unlikely(!seg6_pop_srh(skb, srhoff)))
goto drop;
break;
case SEG6_LOCAL_FLV_ACT_UNSPEC:
fallthrough;
default:
/* by default, we drop the packet since we could not find a
* suitable action.
*/
goto drop;
}
return input_action_end_finish(skb, slwt);
drop:
kfree_skb(skb);
return -EINVAL;
}
/* regular endpoint function */
static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)
{
const struct seg6_flavors_info *finfo = &slwt->flv_info;
__u32 fops = finfo->flv_ops;
if (!fops)
return input_action_end_core(skb, slwt);
/* check for the presence of NEXT-C-SID since it applies first */
if (seg6_next_csid_enabled(fops))
return end_next_csid_core(skb, slwt);
/* the specific processing function to be performed on the packet
* depends on the combination of flavors defined in RFC8986 and some
* information extracted from the packet, e.g. presence/absence of SRH,
* Segment Left = 0, etc.
*/
return end_flv8986_core(skb, slwt);
}
/* regular endpoint, and forward to specified nexthop */
static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)
{
const struct seg6_flavors_info *finfo = &slwt->flv_info;
__u32 fops = finfo->flv_ops;
/* check for the presence of NEXT-C-SID since it applies first */
if (seg6_next_csid_enabled(fops))
return end_x_next_csid_core(skb, slwt);
return input_action_end_x_core(skb, slwt);
}
static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)
{
struct ipv6_sr_hdr *srh;
srh = get_and_validate_srh(skb);
if (!srh)
goto drop;
advance_nextseg(srh, &ipv6_hdr(skb)->daddr);
seg6_lookup_nexthop(skb, NULL, slwt->table);
return dst_input(skb);
drop:
kfree_skb(skb);
return -EINVAL;
}
/* decapsulate and forward inner L2 frame on specified interface */
static int input_action_end_dx2(struct sk_buff *skb,
struct seg6_local_lwt *slwt)
{
struct net *net = dev_net(skb->dev);
struct net_device *odev;
struct ethhdr *eth;
if (!decap_and_validate(skb, IPPROTO_ETHERNET))
goto drop;
if (!pskb_may_pull(skb, ETH_HLEN))
goto drop;
skb_reset_mac_header(skb);
eth = (struct ethhdr *)skb->data;
/* To determine the frame's protocol, we assume it is 802.3. This avoids
* a call to eth_type_trans(), which is not really relevant for our
* use case.
*/
if (!eth_proto_is_802_3(eth->h_proto))
goto drop;
odev = dev_get_by_index_rcu(net, slwt->oif);
if (!odev)
goto drop;
/* As we accept Ethernet frames, make sure the egress device is of
* the correct type.
*/
if (odev->type != ARPHRD_ETHER)
goto drop;
if (!(odev->flags & IFF_UP) || !netif_carrier_ok(odev))
goto drop;
skb_orphan(skb);
if (skb_warn_if_lro(skb))
goto drop;
skb_forward_csum(skb);
if (skb->len - ETH_HLEN > odev->mtu)
goto drop;
skb->dev = odev;
skb->protocol = eth->h_proto;
return dev_queue_xmit(skb);
drop:
kfree_skb(skb);
return -EINVAL;
}
static int input_action_end_dx6_finish(struct net *net, struct sock *sk,
struct sk_buff *skb)
{
struct dst_entry *orig_dst = skb_dst(skb);
struct in6_addr *nhaddr = NULL;
struct seg6_local_lwt *slwt;
slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
/* The inner packet is not associated to any local interface,
* so we do not call netif_rx().
*
* If slwt->nh6 is set to ::, then lookup the nexthop for the
* inner packet's DA. Otherwise, use the specified nexthop.
*/
if (!ipv6_addr_any(&slwt->nh6))
nhaddr = &slwt->nh6;
seg6_lookup_nexthop(skb, nhaddr, 0);
return dst_input(skb);
}
/* decapsulate and forward to specified nexthop */
static int input_action_end_dx6(struct sk_buff *skb,
struct seg6_local_lwt *slwt)
{
/* this function accepts IPv6 encapsulated packets, with either
* an SRH with SL=0, or no SRH.
*/
if (!decap_and_validate(skb, IPPROTO_IPV6))
goto drop;
if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
goto drop;
skb_set_transport_header(skb, sizeof(struct ipv6hdr));
nf_reset_ct(skb);
if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING,
dev_net(skb->dev), NULL, skb, NULL,
skb_dst(skb)->dev, input_action_end_dx6_finish);
return input_action_end_dx6_finish(dev_net(skb->dev), NULL, skb);
drop:
kfree_skb(skb);
return -EINVAL;
}
static int input_action_end_dx4_finish(struct net *net, struct sock *sk,
struct sk_buff *skb)
{
struct dst_entry *orig_dst = skb_dst(skb);
struct seg6_local_lwt *slwt;
struct iphdr *iph;
__be32 nhaddr;
int err;
slwt = seg6_local_lwtunnel(orig_dst->lwtstate);
iph = ip_hdr(skb);
nhaddr = slwt->nh4.s_addr ?: iph->daddr;
skb_dst_drop(skb);
err = ip_route_input(skb, nhaddr, iph->saddr, 0, skb->dev);
if (err) {
kfree_skb(skb);
return -EINVAL;
}
return dst_input(skb);
}
static int input_action_end_dx4(struct sk_buff *skb,
struct seg6_local_lwt *slwt)
{
if (!decap_and_validate(skb, IPPROTO_IPIP))
goto drop;
if (!pskb_may_pull(skb, sizeof(struct iphdr)))
goto drop;
skb->protocol = htons(ETH_P_IP);
skb_set_transport_header(skb, sizeof(struct iphdr));
nf_reset_ct(skb);
if (static_branch_unlikely(&nf_hooks_lwtunnel_enabled))
return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING,
dev_net(skb->dev), NULL, skb, NULL,
skb_dst(skb)->dev, input_action_end_dx4_finish);
return input_action_end_dx4_finish(dev_net(skb->dev), NULL, skb);
drop:
kfree_skb(skb);
return -EINVAL;