forked from PrinzOwO/gtp5g
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtp5g.c
2555 lines (2039 loc) · 71.7 KB
/
gtp5g.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 */
/* GTP according to 3GPP TS 29.281 / 3GPP TS 29.244
*
* Author: Yao-Wen Chang <[email protected]>
* Chi Chang <[email protected]>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/version.h>
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/udp.h>
#include <linux/rculist.h>
#include <linux/jhash.h>
#include <linux/if_tunnel.h>
#include <linux/net.h>
#include <linux/file.h>
#include <linux/gtp.h>
#include <linux/range.h>
#include <linux/un.h>
#include <net/net_namespace.h>
#include <net/protocol.h>
#include <net/ip.h>
#include <net/udp.h>
#include <net/udp_tunnel.h>
#include <net/icmp.h>
#include <net/xfrm.h>
#include <net/genetlink.h>
#include <net/netns/generic.h>
#include "gtp5g.h"
struct local_f_teid {
u32 teid; // i_teid
struct in_addr gtpu_addr_ipv4; // self upf ip
};
struct ip_filter_rule {
uint8_t action; // permit only
uint8_t direction; // in/out
uint8_t proto; // number or "ip" which is not used for matching
struct in_addr src, smask; // ip addr or "any" -> 0.0.0.0
struct in_addr dest, dmask; // ip addr or "assigned" -> 0.0.0.0
int sport_num; // Conut for sport
struct range *sport; // one value, range or not existed -> [0, 0]
int dport_num; // Counter for dport
struct range *dport; // one value, range or not existed -> [0, 0]
};
struct sdf_filter {
struct ip_filter_rule *rule;
uint16_t *tos_traffic_class;
u32 *security_param_idx;
u32 *flow_label; // exactly 3 Octets
u32 *bi_id;
};
struct gtp5g_pdi {
// u8 src_iface; // 0: Access, 1: Core, 2: SGi-LAN/N6-LAN, 3: CP-function
struct in_addr *ue_addr_ipv4;
// char *network_instance
struct local_f_teid *f_teid;
struct sdf_filter *sdf;
};
struct outer_header_creation {
u16 description;
u32 teid; // o_teid
struct in_addr peer_addr_ipv4;
u16 port;
};
struct forwarding_parameter {
// uint8_t dest_int;
// char *network_instance;
struct outer_header_creation *hdr_creation;
};
struct gtp5g_far {
struct hlist_node hlist_id;
u32 id;
// u8 dest_iface;
u8 action; // apply action
struct forwarding_parameter *fwd_param;
struct net_device *dev;
struct rcu_head rcu_head;
};
struct gtp5g_pdr {
struct hlist_node hlist_id;
struct hlist_node hlist_i_teid;
struct hlist_node hlist_addr;
struct hlist_node hlist_related_far;
u16 id;
u32 precedence;
u8 *outer_header_removal;
struct gtp5g_pdi *pdi;
u32 *far_id;
struct gtp5g_far *far;
// AF_UNIX socket for buffer
struct sockaddr_un addr_unix;
struct socket *sock_for_buf;
u16 af;
struct in_addr role_addr_ipv4;
struct sock *sk;
struct net_device *dev;
struct rcu_head rcu_head;
};
/* One instance of the GTP device. */
struct gtp5g_dev {
struct list_head list;
struct sock *sk1u;
struct net_device *dev;
unsigned int role;
unsigned int hash_size;
struct hlist_head *pdr_id_hash;
struct hlist_head *far_id_hash;
struct hlist_head *i_teid_hash; // Used for GTP-U packet detect
struct hlist_head *addr_hash; // Used for IPv4 packet detect
/* IEs list related to PDR*/
struct hlist_head *related_far_hash; // PDR list waiting the FAR to handle
};
static unsigned int gtp5g_net_id __read_mostly;
struct gtp5g_net {
struct list_head gtp5g_dev_list;
};
/* Function unix_sock_{...} are used to handle buffering */
// Send PDR ID, FAR action and buffered packet to user space
static int unix_sock_send(struct gtp5g_pdr *pdr, void *buf, u32 len)
{
struct msghdr msg;
struct iovec iov[2];
mm_segment_t oldfs;
int msg_iovlen = sizeof(iov) / sizeof(struct iovec);
int total_iov_len = 0;
int i, rt;
u16 self_hdr[2] = {pdr->id, pdr->far->action};
if (!pdr->sock_for_buf)
return -EINVAL;
memset(&msg, 0, sizeof(msg));
memset(iov, 0, sizeof(iov));
iov[0].iov_base = self_hdr;
iov[0].iov_len = sizeof(self_hdr);
iov[1].iov_base = buf;
iov[1].iov_len = len;
for (i = 0; i < msg_iovlen; i++)
total_iov_len += iov[i].iov_len;
msg.msg_name = 0;
msg.msg_namelen = 0;
iov_iter_init(&msg.msg_iter, WRITE, iov, msg_iovlen, total_iov_len);
msg.msg_control = NULL;
msg.msg_controllen = 0;
msg.msg_flags = 0;
oldfs = get_fs();
set_fs(KERNEL_DS);
rt = sock_sendmsg(pdr->sock_for_buf, &msg);
set_fs(oldfs);
return rt;
}
// Delete the AF_UNIX client
static void unix_sock_client_delete(struct gtp5g_pdr *pdr)
{
if (pdr->sock_for_buf)
sock_release(pdr->sock_for_buf);
pdr->sock_for_buf = NULL;
}
// Create a AF_UNIX client by specific name sent from user space
static int unix_sock_client_new(struct gtp5g_pdr *pdr)
{
int rt;
struct socket **psock = &pdr->sock_for_buf;
struct sockaddr_un *addr = &pdr->addr_unix;
if (!strlen(addr->sun_path))
return -EINVAL;
rt = sock_create(AF_UNIX, SOCK_DGRAM, 0, psock);
if (rt) {
pr_err("Sock create fail\n");
return rt;
}
rt = (*psock)->ops->connect(*psock, (struct sockaddr *) addr,
sizeof(addr->sun_family) + strlen(addr->sun_path), 0);
if (rt) {
unix_sock_client_delete(pdr);
pr_err("Unix sock connect fail\n");
return rt;
}
return 0;
}
// Handle PDR/FAR changed and affect buffering
static int unix_sock_client_update(struct gtp5g_pdr *pdr)
{
struct gtp5g_far *far = pdr->far;
unix_sock_client_delete(pdr);
if (far && (far->action & FAR_ACTION_BUFF))
return unix_sock_client_new(pdr);
return 0;
}
static u32 gtp5g_h_initval;
static inline u32 u32_hashfn(u32 val)
{
return jhash_1word(val, gtp5g_h_initval);
}
static inline u32 ipv4_hashfn(__be32 ip)
{
return jhash_1word((__force u32)ip, gtp5g_h_initval);
}
static struct gtp5g_far *far_find_by_id(struct gtp5g_dev *gtp, u32 id)
{
struct hlist_head *head;
struct gtp5g_far *far;
head = >p->far_id_hash[u32_hashfn(id) % gtp->hash_size];
hlist_for_each_entry_rcu(far, head, hlist_id) {
if (far->id == id)
return far;
}
return NULL;
}
static int far_fill(struct gtp5g_far *far, struct gtp5g_dev *gtp, struct genl_info *info)
{
struct nlattr *fwd_param_attrs[GTP5G_FORWARDING_PARAMETER_ATTR_MAX + 1];
struct nlattr *hdr_creation_attrs[GTP5G_OUTER_HEADER_CREATION_ATTR_MAX + 1];
struct outer_header_creation *hdr_creation;
// Update related PDR for buffering
struct gtp5g_pdr *pdr;
struct hlist_head *head;
if (!far)
return -EINVAL;
far->id = nla_get_u32(info->attrs[GTP5G_FAR_ID]);
if (info->attrs[GTP5G_FAR_APPLY_ACTION]) {
far->action = nla_get_u8(info->attrs[GTP5G_FAR_APPLY_ACTION]);
}
if (info->attrs[GTP5G_FAR_FORWARDING_PARAMETER] &&
!nla_parse_nested(fwd_param_attrs, GTP5G_FORWARDING_PARAMETER_ATTR_MAX, info->attrs[GTP5G_FAR_FORWARDING_PARAMETER], NULL, NULL)) {
if (!far->fwd_param) {
far->fwd_param = kzalloc(sizeof(*far->fwd_param), GFP_ATOMIC);
if (!far->fwd_param)
return -ENOMEM;
}
if (fwd_param_attrs[GTP5G_FORWARDING_PARAMETER_OUTER_HEADER_CREATION] &&
!nla_parse_nested(hdr_creation_attrs, GTP5G_OUTER_HEADER_CREATION_ATTR_MAX, fwd_param_attrs[GTP5G_FORWARDING_PARAMETER_OUTER_HEADER_CREATION], NULL, NULL)) {
if (!far->fwd_param->hdr_creation) {
far->fwd_param->hdr_creation = kzalloc(sizeof(*far->fwd_param->hdr_creation), GFP_ATOMIC);
if (!far->fwd_param->hdr_creation)
return -ENOMEM;
}
hdr_creation = far->fwd_param->hdr_creation;
if (!hdr_creation_attrs[GTP5G_OUTER_HEADER_CREATION_DESCRIPTION] ||
!hdr_creation_attrs[GTP5G_OUTER_HEADER_CREATION_O_TEID] ||
!hdr_creation_attrs[GTP5G_OUTER_HEADER_CREATION_PEER_ADDR_IPV4] ||
!hdr_creation_attrs[GTP5G_OUTER_HEADER_CREATION_PORT])
return -EINVAL;
hdr_creation->description = nla_get_u16(hdr_creation_attrs[GTP5G_OUTER_HEADER_CREATION_DESCRIPTION]);
hdr_creation->teid = nla_get_u32(hdr_creation_attrs[GTP5G_OUTER_HEADER_CREATION_O_TEID]);
hdr_creation->peer_addr_ipv4.s_addr = nla_get_be32(hdr_creation_attrs[GTP5G_OUTER_HEADER_CREATION_PEER_ADDR_IPV4]);
hdr_creation->port = nla_get_u16(hdr_creation_attrs[GTP5G_OUTER_HEADER_CREATION_PORT]);
}
}
/* Update PDRs which has not linked to this FAR */
head = >p->related_far_hash[u32_hashfn(far->id) % gtp->hash_size];
hlist_for_each_entry_rcu(pdr, head, hlist_related_far) {
if (*pdr->far_id == far->id) {
pdr->far = far;
if (unix_sock_client_update(pdr) < 0)
pr_warn("PDR[%u] update fail when FAR[%u] apply action is changed",
pdr->id, far->id);
}
}
return 0;
}
static struct gtp5g_pdr *pdr_find_by_id(struct gtp5g_dev *gtp, u16 id)
{
struct hlist_head *head;
struct gtp5g_pdr *pdr;
head = >p->pdr_id_hash[u32_hashfn(id) % gtp->hash_size];
hlist_for_each_entry_rcu(pdr, head, hlist_id) {
if (pdr->id == id)
return pdr;
}
return NULL;
}
static struct gtp5g_pdr *pdr_find_by_ipv4(struct gtp5g_dev *gtp, __be32 addr)
{
struct hlist_head *head;
struct gtp5g_pdr *pdr;
head = >p->addr_hash[ipv4_hashfn(addr) % gtp->hash_size];
hlist_for_each_entry_rcu(pdr, head, hlist_addr) {
// TODO: Move the value we check into first level
if (pdr->af == AF_INET && pdr->pdi->ue_addr_ipv4->s_addr == addr)
return pdr;
}
return NULL;
}
static int pdr_fill(struct gtp5g_pdr *pdr, struct gtp5g_dev *gtp, struct genl_info *info)
{
struct nlattr *pdi_attrs[GTP5G_PDI_ATTR_MAX + 1];
struct nlattr *f_teid_attrs[GTP5G_F_TEID_ATTR_MAX + 1];
struct nlattr *sdf_attrs[GTP5G_SDF_FILTER_ATTR_MAX + 1];
struct nlattr *rule_attrs[GTP5G_FLOW_DESCRIPTION_ATTR_MAX + 1];
struct hlist_head *head;
struct gtp5g_pdr *ppdr, *last_ppdr;
struct gtp5g_pdi *pdi = NULL;
struct local_f_teid *f_teid = NULL;
struct sdf_filter *sdf;
struct ip_filter_rule *rule;
int i;
char *str;
if (!pdr)
return -EINVAL;
pdr->af = AF_INET;
pdr->id = nla_get_u16(info->attrs[GTP5G_PDR_ID]);
if (info->attrs[GTP5G_PDR_PRECEDENCE]) {
pdr->precedence = nla_get_u32(info->attrs[GTP5G_PDR_PRECEDENCE]);
}
if (info->attrs[GTP5G_OUTER_HEADER_REMOVAL]) {
if (!pdr->outer_header_removal) {
pdr->outer_header_removal = kzalloc(sizeof(*pdr->outer_header_removal), GFP_ATOMIC);
if (!pdr->outer_header_removal)
return -ENOMEM;
}
*pdr->outer_header_removal = nla_get_u8(info->attrs[GTP5G_OUTER_HEADER_REMOVAL]);
}
/* Not in 3GPP spec, just used for routing */
if (info->attrs[GTP5G_PDR_ROLE_ADDR_IPV4])
pdr->role_addr_ipv4.s_addr = nla_get_u32(info->attrs[GTP5G_PDR_ROLE_ADDR_IPV4]);
/* Not in 3GPP spec, just used for buffering */
if (info->attrs[GTP5G_PDR_UNIX_SOCKET_PATH]) {
str = nla_data(info->attrs[GTP5G_PDR_UNIX_SOCKET_PATH]);
pdr->addr_unix.sun_family = AF_UNIX;
strncpy(pdr->addr_unix.sun_path, str, nla_len(info->attrs[GTP5G_PDR_UNIX_SOCKET_PATH]));
}
if (info->attrs[GTP5G_PDR_FAR_ID]) {
if (!pdr->far_id) {
pdr->far_id = kzalloc(sizeof(*pdr->far_id), GFP_ATOMIC);
if (!pdr->far_id)
return -ENOMEM;
}
*pdr->far_id = nla_get_u32(info->attrs[GTP5G_PDR_FAR_ID]);
if (!hlist_unhashed(&pdr->hlist_related_far))
hlist_del_rcu(&pdr->hlist_related_far);
hlist_add_head_rcu(&pdr->hlist_related_far, >p->related_far_hash[u32_hashfn(*pdr->far_id) % gtp->hash_size]);
pdr->far = far_find_by_id(gtp, *pdr->far_id);
}
if (unix_sock_client_update(pdr) < 0)
return -EINVAL;
/* Parse PDI in PDR */
if (info->attrs[GTP5G_PDR_PDI] &&
!nla_parse_nested(pdi_attrs, GTP5G_PDI_ATTR_MAX, info->attrs[GTP5G_PDR_PDI], NULL, NULL)) {
if (!pdr->pdi) {
pdr->pdi = kzalloc(sizeof(*pdr->pdi), GFP_ATOMIC);
if (!pdr->pdi)
return -ENOMEM;
}
pdi = pdr->pdi;
if (pdi_attrs[GTP5G_PDI_UE_ADDR_IPV4]) {
if (!pdi->ue_addr_ipv4) {
pdi->ue_addr_ipv4 = kzalloc(sizeof(*pdi->ue_addr_ipv4), GFP_ATOMIC);
if (!pdi->ue_addr_ipv4)
return -ENOMEM;
}
if (!hlist_unhashed(&pdr->hlist_addr))
hlist_del_rcu(&pdr->hlist_addr);
pdi->ue_addr_ipv4->s_addr = nla_get_be32(pdi_attrs[GTP5G_PDI_UE_ADDR_IPV4]);
}
/* Parse F-TEID in PDI */
if (pdi_attrs[GTP5G_PDI_F_TEID] &&
!nla_parse_nested(f_teid_attrs, GTP5G_F_TEID_ATTR_MAX, pdi_attrs[GTP5G_PDI_F_TEID], NULL, NULL)) {
if (!f_teid_attrs[GTP5G_F_TEID_I_TEID] || !f_teid_attrs[GTP5G_F_TEID_GTPU_ADDR_IPV4])
return -EINVAL;
if (!pdi->f_teid) {
pdi->f_teid = kzalloc(sizeof(*pdi->f_teid), GFP_ATOMIC);
if (!pdi->f_teid)
return -ENOMEM;
}
f_teid = pdi->f_teid;
if (!hlist_unhashed(&pdr->hlist_i_teid)){
hlist_del_rcu(&pdr->hlist_i_teid);
}
f_teid->teid = nla_get_u32(f_teid_attrs[GTP5G_F_TEID_I_TEID]);
last_ppdr = NULL;
head = >p->i_teid_hash[u32_hashfn(f_teid->teid) % gtp->hash_size];
hlist_for_each_entry_rcu(ppdr, head, hlist_i_teid) {
if (pdr->precedence > ppdr->precedence)
last_ppdr = ppdr;
else
break;
}
if(!last_ppdr)
hlist_add_head_rcu(&pdr->hlist_i_teid, head);
else
hlist_add_behind_rcu(&pdr->hlist_i_teid, &last_ppdr->hlist_i_teid);
f_teid->gtpu_addr_ipv4.s_addr = nla_get_be32(f_teid_attrs[GTP5G_F_TEID_GTPU_ADDR_IPV4]);
}
/* Parse SDF Filter in PDI */
if (pdi_attrs[GTP5G_PDI_SDF_FILTER] &&
!nla_parse_nested(sdf_attrs, GTP5G_SDF_FILTER_ATTR_MAX, pdi_attrs[GTP5G_PDI_SDF_FILTER], NULL, NULL)) {
if (!pdi->sdf) {
pdi->sdf = kzalloc(sizeof(*pdi->sdf), GFP_ATOMIC);
if (!pdi->sdf)
return -ENOMEM;
}
sdf = pdi->sdf;
if (sdf_attrs[GTP5G_SDF_FILTER_FLOW_DESCRIPTION] &&
!nla_parse_nested(rule_attrs, GTP5G_FLOW_DESCRIPTION_ATTR_MAX, sdf_attrs[GTP5G_SDF_FILTER_FLOW_DESCRIPTION], NULL, NULL)) {
if (!rule_attrs[GTP5G_FLOW_DESCRIPTION_ACTION] ||
!rule_attrs[GTP5G_FLOW_DESCRIPTION_DIRECTION] ||
!rule_attrs[GTP5G_FLOW_DESCRIPTION_PROTOCOL] ||
!rule_attrs[GTP5G_FLOW_DESCRIPTION_SRC_IPV4] ||
!rule_attrs[GTP5G_FLOW_DESCRIPTION_DEST_IPV4])
return -EINVAL;
if (!sdf->rule) {
sdf->rule = kzalloc(sizeof(*sdf->rule), GFP_ATOMIC);
if (!sdf->rule)
return -ENOMEM;
}
rule = sdf->rule;
rule->action = nla_get_u8(rule_attrs[GTP5G_FLOW_DESCRIPTION_ACTION]);
rule->direction = nla_get_u8(rule_attrs[GTP5G_FLOW_DESCRIPTION_DIRECTION]);
rule->proto = nla_get_u8(rule_attrs[GTP5G_FLOW_DESCRIPTION_PROTOCOL]);
rule->src.s_addr = nla_get_be32(rule_attrs[GTP5G_FLOW_DESCRIPTION_SRC_IPV4]);
rule->dest.s_addr = nla_get_be32(rule_attrs[GTP5G_FLOW_DESCRIPTION_DEST_IPV4]);
if (rule_attrs[GTP5G_FLOW_DESCRIPTION_SRC_MASK])
rule->smask.s_addr = nla_get_be32(rule_attrs[GTP5G_FLOW_DESCRIPTION_SRC_MASK]);
else
rule->smask.s_addr = -1;
if (rule_attrs[GTP5G_FLOW_DESCRIPTION_DEST_MASK])
rule->dmask.s_addr = nla_get_be32(rule_attrs[GTP5G_FLOW_DESCRIPTION_DEST_MASK]);
else
rule->dmask.s_addr = -1;
if (rule_attrs[GTP5G_FLOW_DESCRIPTION_SRC_PORT]) {
u32 *sport_encode = nla_data(rule_attrs[GTP5G_FLOW_DESCRIPTION_SRC_PORT]);
rule->sport_num = nla_len(rule_attrs[GTP5G_FLOW_DESCRIPTION_SRC_PORT]) / sizeof(u32);
if (rule->sport)
kfree(rule->sport);
rule->sport = kzalloc(rule->sport_num * sizeof(*rule->sport), GFP_ATOMIC);
for (i = 0; i < rule->sport_num; i++) {
if ((sport_encode[i] & 0xFFFF) <= (sport_encode[i] >> 16)) {
rule->sport[i].start = (sport_encode[i] & 0xFFFF);
rule->sport[i].end = (sport_encode[i] >> 16);
}
else {
rule->sport[i].start = (sport_encode[i] >> 16);
rule->sport[i].end = (sport_encode[i] & 0xFFFF);
}
}
}
if (rule_attrs[GTP5G_FLOW_DESCRIPTION_DEST_PORT]) {
u32 *dport_encode = nla_data(rule_attrs[GTP5G_FLOW_DESCRIPTION_DEST_PORT]);
rule->dport_num = nla_len(rule_attrs[GTP5G_FLOW_DESCRIPTION_DEST_PORT]) / sizeof(u32);
if (rule->dport)
kfree(rule->dport);
rule->dport = kzalloc(rule->dport_num * sizeof(*rule->dport), GFP_ATOMIC);
for (i = 0; i < rule->dport_num; i++) {
if ((dport_encode[i] & 0xFFFF) <= (dport_encode[i] >> 16)) {
rule->dport[i].start = (dport_encode[i] & 0xFFFF);
rule->dport[i].end = (dport_encode[i] >> 16);
}
else {
rule->dport[i].start = (dport_encode[i] >> 16);
rule->dport[i].end = (dport_encode[i] & 0xFFFF);
}
}
}
}
if (sdf_attrs[GTP5G_SDF_FILTER_TOS_TRAFFIC_CLASS]) {
if (!sdf->tos_traffic_class) {
sdf->tos_traffic_class = kzalloc(sizeof(*sdf->tos_traffic_class), GFP_ATOMIC);
if (!sdf->tos_traffic_class)
return -ENOMEM;
}
*sdf->tos_traffic_class = nla_get_u16(sdf_attrs[GTP5G_SDF_FILTER_TOS_TRAFFIC_CLASS]);
}
if (sdf_attrs[GTP5G_SDF_FILTER_SECURITY_PARAMETER_INDEX]) {
if (!sdf->security_param_idx) {
sdf->security_param_idx = kzalloc(sizeof(*sdf->security_param_idx), GFP_ATOMIC);
if (!sdf->security_param_idx)
return -ENOMEM;
}
*sdf->security_param_idx = nla_get_u32(sdf_attrs[GTP5G_SDF_FILTER_SECURITY_PARAMETER_INDEX]);
}
if (sdf_attrs[GTP5G_SDF_FILTER_FLOW_LABEL]) {
if (!sdf->flow_label) {
sdf->flow_label = kzalloc(sizeof(*sdf->flow_label), GFP_ATOMIC);
if (!sdf->flow_label)
return -ENOMEM;
}
*sdf->flow_label = nla_get_u32(sdf_attrs[GTP5G_SDF_FILTER_FLOW_LABEL]);
}
if (sdf_attrs[GTP5G_SDF_FILTER_SDF_FILTER_ID]) {
if (!sdf->bi_id) {
sdf->bi_id = kzalloc(sizeof(*sdf->bi_id), GFP_ATOMIC);
if (!sdf->bi_id)
return -ENOMEM;
}
*sdf->bi_id = nla_get_u32(sdf_attrs[GTP5G_SDF_FILTER_SDF_FILTER_ID]);
}
}
// Add into hash table only for downlink
if (pdi->ue_addr_ipv4 && !f_teid) {
last_ppdr = NULL;
head = >p->addr_hash[u32_hashfn(pdi->ue_addr_ipv4->s_addr) % gtp->hash_size];
hlist_for_each_entry_rcu(ppdr, head, hlist_addr) {
if (pdr->precedence > ppdr->precedence)
last_ppdr = ppdr;
else
break;
}
if (!last_ppdr)
hlist_add_head_rcu(&pdr->hlist_addr, head);
else
hlist_add_behind_rcu(&pdr->hlist_addr, &last_ppdr->hlist_addr);
}
}
return 0;
}
static int gtp5g_dev_init(struct net_device *dev)
{
struct gtp5g_dev *gtp = netdev_priv(dev);
gtp->dev = dev;
dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
if (!dev->tstats)
return -ENOMEM;
return 0;
}
static void __gtp5g_encap_destroy(struct sock *sk)
{
struct gtp5g_dev *gtp;
lock_sock(sk);
gtp = sk->sk_user_data;
if (gtp) {
gtp->sk1u = NULL;
udp_sk(sk)->encap_type = 0;
rcu_assign_sk_user_data(sk, NULL);
sock_put(sk);
}
release_sock(sk);
}
static void gtp5g_encap_disable_sock(struct sock *sk)
{
if (!sk)
return;
__gtp5g_encap_destroy(sk);
}
static void gtp5g_encap_disable(struct gtp5g_dev *gtp)
{
gtp5g_encap_disable_sock(gtp->sk1u);
}
static void gtp5g_dev_uninit(struct net_device *dev)
{
struct gtp5g_dev *gtp = netdev_priv(dev);
gtp5g_encap_disable(gtp);
free_percpu(dev->tstats);
}
struct gtp5g_pktinfo {
struct sock *sk;
struct iphdr *iph;
struct flowi4 fl4;
struct rtable *rt;
struct outer_header_creation *hdr_creation;
struct net_device *dev;
__be16 gtph_port;
};
static void gtp5g_push_header(struct sk_buff *skb, struct gtp5g_pktinfo *pktinfo)
{
int payload_len = skb->len;
struct gtp1_header *gtp1;
pktinfo->gtph_port = pktinfo->hdr_creation->port;
gtp1 = skb_push(skb, sizeof(*gtp1));
/* Bits 8 7 6 5 4 3 2 1
* +--+--+--+--+--+--+--+--+
* |version |PT| 0| E| S|PN|
* +--+--+--+--+--+--+--+--+
* 0 0 1 1 0 0 0 0
*/
gtp1->flags = 0x30; /* v1, GTP-non-prime. */
gtp1->type = GTP_TPDU;
gtp1->length = htons(payload_len);
gtp1->tid = pktinfo->hdr_creation->teid;
/* TODO: Suppport for extension header, sequence number and N-PDU.
* Update the length field if any of them is available.
*/
}
static inline void gtp5g_set_pktinfo_ipv4(struct gtp5g_pktinfo *pktinfo,
struct sock *sk, struct iphdr *iph,
struct outer_header_creation *hdr_creation,
struct rtable *rt,
struct flowi4 *fl4,
struct net_device *dev)
{
pktinfo->sk = sk;
pktinfo->iph = iph;
pktinfo->hdr_creation = hdr_creation;
pktinfo->rt = rt;
pktinfo->fl4 = *fl4;
pktinfo->dev = dev;
}
static struct rtable *ip4_find_route(struct sk_buff *skb, struct iphdr *iph,
struct sock *sk, struct net_device *gtp_dev,
__be32 saddr, __be32 daddr, struct flowi4 *fl4)
{
struct rtable *rt;
__be16 df;
int mtu;
memset(fl4, 0, sizeof(*fl4));
fl4->flowi4_oif = sk->sk_bound_dev_if;
fl4->daddr = daddr;
fl4->saddr = (saddr ? saddr : inet_sk(sk)->inet_saddr);
fl4->flowi4_tos = RT_CONN_FLAGS(sk);
fl4->flowi4_proto = sk->sk_protocol;
rt = ip_route_output_key(dev_net(gtp_dev), fl4);
if (IS_ERR(rt)) {
pr_warn("no route to %pI4\n", &iph->daddr);
gtp_dev->stats.tx_carrier_errors++;
goto err;
}
if (rt->dst.dev == gtp_dev) {
pr_warn("circular route to %pI4\n", &iph->daddr);
gtp_dev->stats.collisions++;
goto err_rt;
}
skb_dst_drop(skb);
/* This is similar to tnl_update_pmtu(). */
df = iph->frag_off;
if (df) {
mtu = dst_mtu(&rt->dst) - gtp_dev->hard_header_len -
sizeof(struct iphdr) - sizeof(struct udphdr);
// GTPv1
mtu -= sizeof(struct gtp1_header);
}
else {
mtu = dst_mtu(&rt->dst);
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 4, 0)
rt->dst.ops->update_pmtu(&rt->dst, NULL, skb, mtu, false);
#else
rt->dst.ops->update_pmtu(&rt->dst, NULL, skb, mtu);
#endif
if (!skb_is_gso(skb) && (iph->frag_off & htons(IP_DF)) &&
mtu < ntohs(iph->tot_len)) {
netdev_dbg(gtp_dev, "packet too big, fragmentation needed\n");
memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
htonl(mtu));
goto err_rt;
}
return rt;
err_rt:
ip_rt_put(rt);
err:
return ERR_PTR(-ENOENT);
}
static int ip_xmit(struct sk_buff *skb, struct sock *sk, struct net_device *gtp_dev)
{
struct iphdr *iph = ip_hdr(skb);
struct flowi4 fl4;
struct rtable *rt;
rt = ip4_find_route(skb, iph, sk, gtp_dev, 0, iph->daddr, &fl4);
if (IS_ERR(rt))
return -EBADMSG;
skb_dst_set(skb, &rt->dst);
if (ip_local_out(dev_net(gtp_dev), sk, skb) < 0) {
pr_err("dev error\n");
return -1;
}
return 0;
}
static int gtp5g_drop_skb_ipv4(struct sk_buff *skb, struct net_device *dev)
{
dev->stats.tx_dropped++;
dev_kfree_skb(skb);
return 0;
}
static int gtp5g_fwd_skb_ipv4(struct sk_buff *skb, struct net_device *dev,
struct gtp5g_pktinfo *pktinfo, struct gtp5g_pdr *pdr)
{
struct rtable *rt;
struct flowi4 fl4;
struct iphdr *iph = ip_hdr(skb);
struct outer_header_creation *hdr_creation;
if (!(pdr->far && pdr->far->fwd_param && pdr->far->fwd_param->hdr_creation)) {
netdev_dbg(dev, "Unknown RAN address\n");
dev->stats.tx_carrier_errors++;
goto err;
}
hdr_creation = pdr->far->fwd_param->hdr_creation;
rt = ip4_find_route(skb, iph, pdr->sk, dev,
pdr->role_addr_ipv4.s_addr, hdr_creation->peer_addr_ipv4.s_addr, &fl4);
if (IS_ERR(rt))
goto err;
gtp5g_set_pktinfo_ipv4(pktinfo, pdr->sk, iph, hdr_creation, rt, &fl4, dev);
gtp5g_push_header(skb, pktinfo);
return FAR_ACTION_FORW;
err:
return -EBADMSG;
}
static int gtp5g_buf_skb_ipv4(struct sk_buff *skb, struct net_device *dev,
struct gtp5g_pdr *pdr)
{
int rt = 0;
// TODO: handle nonlinear part
if (unix_sock_send(pdr, skb->data, skb_headlen(skb)) < 0)
rt = -EBADMSG;
return rt;
}
static int gtp5g_handle_skb_ipv4(struct sk_buff *skb, struct net_device *dev,
struct gtp5g_pktinfo *pktinfo)
{
struct gtp5g_dev *gtp = netdev_priv(dev);
struct gtp5g_pdr *pdr;
struct gtp5g_far *far;
struct iphdr *iph;
/* Read the IP destination address and resolve the PDR.
* Prepend PDR header with TEI/TID from PDR.
*/
iph = ip_hdr(skb);
if (gtp->role == GTP5G_ROLE_UPF)
pdr = pdr_find_by_ipv4(gtp, iph->daddr);
else
pdr = pdr_find_by_ipv4(gtp, iph->saddr);
if (!pdr) {
netdev_dbg(dev, "no PDR found for %pI4, skip\n",
&iph->daddr);
return -ENOENT;
}
netdev_dbg(dev, "found PDR %p\n", pdr);
far = pdr->far;
if (far) {
// One and only one of the DROP, FORW and BUFF flags shall be set to 1.
// The NOCP flag may only be set if the BUFF flag is set.
// The DUPL flag may be set with any of the DROP, FORW, BUFF and NOCP flags.
switch (far->action & FAR_ACTION_MASK) {
case FAR_ACTION_DROP:
return gtp5g_drop_skb_ipv4(skb, dev);
case FAR_ACTION_FORW:
return gtp5g_fwd_skb_ipv4(skb, dev, pktinfo, pdr);
case FAR_ACTION_BUFF:
return gtp5g_buf_skb_ipv4(skb, dev, pdr);
default:
pr_err("Unspec apply action[%u] in FAR[%u] and related to PDR[%u]",
far->action, far->id, pdr->id);
}
}
return -ENOENT;
}
static void gtp5g_xmit_skb_ipv4(struct sk_buff *skb, struct gtp5g_pktinfo *pktinfo, u32 action)
{
if (action & FAR_ACTION_FORW) {
netdev_dbg(pktinfo->dev, "gtp -> IP src: %pI4 dst: %pI4\n",
&pktinfo->iph->saddr, &pktinfo->iph->daddr);
udp_tunnel_xmit_skb(pktinfo->rt, pktinfo->sk, skb,
pktinfo->fl4.saddr, pktinfo->fl4.daddr,
pktinfo->iph->tos,
ip4_dst_hoplimit(&pktinfo->rt->dst),
0,
pktinfo->gtph_port, pktinfo->gtph_port,
true, true);
}
/* TODO: Need to implement with gtp5g_handle_skb_ipv4
if (action & FAR_ACTION_BUFF) {
}
if (action & FAR_ACTION_NOCP) {
}
if (action & FAR_ACTION_DUPL) {
}
*/
}
static netdev_tx_t gtp5g_dev_xmit(struct sk_buff *skb, struct net_device *dev)
{
unsigned int proto = ntohs(skb->protocol);
struct gtp5g_pktinfo pktinfo;
int ret;
/* Ensure there is sufficient headroom. */
if (skb_cow_head(skb, dev->needed_headroom))
goto tx_err;
skb_reset_inner_headers(skb);
/* PDR lookups in gtp5g_build_skb_*() need rcu read-side lock. */
rcu_read_lock();
switch (proto) {
case ETH_P_IP:
ret = gtp5g_handle_skb_ipv4(skb, dev, &pktinfo);
break;
default:
ret = -EOPNOTSUPP;
}
rcu_read_unlock();
if (ret < 0)
goto tx_err;
switch (proto) {
case ETH_P_IP:
gtp5g_xmit_skb_ipv4(skb, &pktinfo, ret);
break;
}
return NETDEV_TX_OK;
tx_err:
dev->stats.tx_errors++;
dev_kfree_skb(skb);
return NETDEV_TX_OK;
}
static const struct net_device_ops gtp5g_netdev_ops = {
.ndo_init = gtp5g_dev_init,
.ndo_uninit = gtp5g_dev_uninit,
.ndo_start_xmit = gtp5g_dev_xmit,
.ndo_get_stats64 = ip_tunnel_get_stats64,
};
static void pdr_context_free(struct rcu_head *head)
{
struct gtp5g_pdr *pdr = container_of(head, struct gtp5g_pdr, rcu_head);
struct gtp5g_pdi *pdi;
struct sdf_filter *sdf;
if (!pdr)
return;
sock_put(pdr->sk);
kfree(pdr->outer_header_removal);
pdi = pdr->pdi;
if (pdi) {
kfree(pdi->ue_addr_ipv4);
kfree(pdi->f_teid);
kfree(pdr->pdi);
kfree(pdr->far_id);
sdf = pdi->sdf;
if (pdi->sdf) {
if (sdf->rule) {
kfree(sdf->rule->sport);
kfree(sdf->rule->dport);
kfree(sdf->rule);
}