forked from free5gc/gtp5g
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtp5g.c
4392 lines (3672 loc) · 130 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 */
/* GTP5G according to 3GPP TS 29.281 / 3GPP TS 29.244
*
* Author: Muthuraman Elangovan <[email protected]>
* Yao-Wen Chang <[email protected]>
* Chi Chang <[email protected]>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/kernel.h>
#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/inetdevice.h>
#include <linux/file.h>
#include <linux/gtp.h>
#include <linux/range.h>
#include <linux/un.h>
#include <linux/proc_fs.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 <stdbool.h>
#include "gtp5g.h"
#define DRV_VERSION "0.5.4"
/* used to compatible with api with/without seid */
#define MSG_SEID_IOV_LEN 3
#define MSG_NO_SEID_IOV_LEN 2
bool api_with_seid = false;
int dbg_trace_lvl = 1;
#define DBG(level, dev, fmt, args...) do { \
if (level <= dbg_trace_lvl) { \
if (dev) \
printk_ratelimited("%s:[gtp5g] %s: "fmt, netdev_name(dev), __func__, ##args); \
else \
printk_ratelimited("[gtp5g] %s: " fmt, __func__, ##args); \
} \
} while(0)
#define GTP5G_LOG(dev, fmt, args...) DBG(0, dev, fmt, ##args)
#define GTP5G_ERR(dev, fmt, args...) DBG(1, dev, fmt, ##args)
#define GTP5G_WAR(dev, fmt, args...) DBG(2, dev, fmt, ##args)
#define GTP5G_INF(dev, fmt, args...) DBG(3, dev, fmt, ##args)
#define GTP5G_TRC(dev, fmt, args...) DBG(4, dev, fmt, ##args)
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 "any" -> 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 gtp5g_qer {
struct hlist_node hlist_id;
u64 seid;
u32 id; /* 8.2.75 QER_ID */
uint8_t ul_dl_gate; /* 8.2.7 Gate Status */
struct {
uint32_t ul_high;
uint8_t ul_low;
uint32_t dl_high;
uint8_t dl_low;
} mbr; /* 8.2.8 MBR */
struct {
uint32_t ul_high;
uint8_t ul_low;
uint32_t dl_high;
uint8_t dl_low;
} gbr; /* 8.2.9 GBR */
uint32_t qer_corr_id; /* 8.2.10 QER Correlation ID */
uint8_t rqi; /* 8.2.88 RQI */
uint8_t qfi; /* 8.2.89 QFI */
/* 8.2.115 Averaging Window (Optional) */
uint8_t ppi; /* 8.2.116 Paging Policy Indicator */
/* 8.2.139 Packet Rate Status */
/* Rate Control Status Reporting */
uint8_t rcsr; /* 8.2.174 QER Control Indications */
struct net_device *dev;
struct rcu_head rcu_head;
};
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_policy {
int len;
char identifier[0xff + 1];
/* Exact value to handle forwarding policy */
u32 mark;
};
struct forwarding_parameter {
//uint8_t dest_int;
//char *network_instance;
struct outer_header_creation *hdr_creation;
struct forwarding_policy *fwd_policy;
};
struct gtp5g_far {
struct hlist_node hlist_id;
u64 seid;
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;
struct hlist_node hlist_related_qer;
u64 seid;
u16 id;
u32 precedence;
u8 *outer_header_removal;
struct gtp5g_pdi *pdi;
u32 *far_id;
struct gtp5g_far *far;
u32 *qer_id;
struct gtp5g_qer *qer;
// 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;
/* Drop Count */
u64 ul_drop_cnt;
u64 dl_drop_cnt;
};
/* 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 *qer_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
struct hlist_head *related_qer_hash; // PDR list waiting the QER to handle
/* Used by proc interface */
struct list_head proc_list;
};
struct gtp5g_pktinfo {
struct sock *sk;
struct iphdr *iph;
struct flowi4 fl4;
struct rtable *rt;
struct outer_header_creation *hdr_creation;
struct gtp5g_qer *qer;
struct net_device *dev;
__be16 gtph_port;
};
struct gtp5g_emark_pktinfo {
u32 teid;
u32 peer_addr;
u32 local_addr;
u32 role_addr;
struct sock *sk;
struct flowi4 fl4;
struct rtable *rt;
struct net_device *dev;
__be16 gtph_port;
};
static unsigned int gtp5g_net_id __read_mostly;
struct gtp5g_net {
struct list_head gtp5g_dev_list;
};
struct list_head proc_gtp5g_dev;
struct proc_gtp5g_pdr {
u16 id;
u64 seid;
u32 precedence;
u8 ohr;
u32 role_addr4;
u32 pdi_ue_addr4;
u32 pdi_fteid;
u32 pdi_gtpu_addr4;
u32 far_id;
u32 qer_id;
u64 ul_drop_cnt;
u64 dl_drop_cnt;
};
struct proc_gtp5g_far {
u32 id;
u64 seid;
u8 action;
//OHC
u16 description;
u32 teid;
u32 peer_addr4;
};
struct proc_gtp5g_qer {
u32 id;
u64 seid;
u8 qfi;
};
static struct gtp5g_qer *gtp5g_find_qer(struct net *net, struct nlattr *nla[]);
static struct gtp5g_qer *find_qer_by_id(struct gtp5g_dev *gtp, u64 seid, u32 qer_id);
static void qer_context_delete(struct gtp5g_qer *qer);
#define SEID_U32ID_HEX_STR_LEN 24
static void seid_and_u32id_to_hex_str(u64 seid_int, u32 id, char *seid_u32id_hexstr)
{
#define SEID_HEX_STR_LEN 16
#define U32_ID_HEX_STR_LEN 8
char seid_hexstr[SEID_HEX_STR_LEN];
char id_hexstr[U32_ID_HEX_STR_LEN];
snprintf(seid_hexstr, SEID_HEX_STR_LEN, "%llx", seid_int);
snprintf(id_hexstr, U32_ID_HEX_STR_LEN, "%x", id);
strcpy(seid_u32id_hexstr, seid_hexstr);
strcat(seid_u32id_hexstr, id_hexstr);
}
static void seid_pdr_id_to_hex_str(u64 seid_int, u16 pdr_id, char *buff)
{
seid_and_u32id_to_hex_str(seid_int, (u32)(pdr_id), buff);
}
static void seid_far_id_to_hex_str(u64 seid_int, u32 far_id, char *buff)
{
seid_and_u32id_to_hex_str(seid_int, far_id, buff);
}
static void seid_qer_id_to_hex_str(u64 seid_int, u32 qer_id, char *buff)
{
seid_and_u32id_to_hex_str(seid_int, qer_id, buff);
}
/* 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;
mm_segment_t oldfs;
int msg_iovlen;
int total_iov_len = 0;
int i, rt;
u64 self_seid_hdr[1] = {pdr->seid};
u16 self_hdr[2] = {pdr->id, pdr->far->action};
if (!pdr->sock_for_buf) {
GTP5G_ERR(NULL, "Failed Socket buffer is NULL\n");
return -EINVAL;
}
memset(&msg, 0, sizeof(msg));
if (api_with_seid) {
msg_iovlen = MSG_SEID_IOV_LEN;
iov = kmalloc_array(msg_iovlen, sizeof(struct iovec),
GFP_KERNEL);
memset(iov, 0, sizeof(struct iovec) * msg_iovlen);
iov[0].iov_base = self_seid_hdr;
iov[0].iov_len = sizeof(self_seid_hdr);
iov[1].iov_base = self_hdr;
iov[1].iov_len = sizeof(self_hdr);
iov[2].iov_base = buf;
iov[2].iov_len = len;
} else {
// for backward compatible
msg_iovlen = MSG_NO_SEID_IOV_LEN;
iov = kmalloc_array(msg_iovlen, sizeof(struct iovec),
GFP_KERNEL);
memset(iov, 0, sizeof(struct iovec) * msg_iovlen);
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 = MSG_DONTWAIT;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0)
oldfs = force_uaccess_begin();
#else
oldfs = get_fs();
set_fs(KERNEL_DS);
#endif
rt = sock_sendmsg(pdr->sock_for_buf, &msg);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0)
force_uaccess_end(oldfs);
#else
set_fs(oldfs);
#endif
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)) {
GTP5G_ERR(NULL, "Failed sun_path length is 0\n");
return -EINVAL;
}
rt = sock_create(AF_UNIX, SOCK_DGRAM, 0, psock);
if (rt) {
GTP5G_ERR(NULL, "Failed to create dgram socket: %s\n", addr->sun_path);
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);
GTP5G_ERR(NULL, "Failed to connect the socket: %s\n", addr->sun_path);
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 str_hashfn(char *str)
{
return jhash(str, strlen(str), 0);
}
static inline u32 ipv4_hashfn(__be32 ip)
{
return jhash_1word((__force u32)ip, gtp5g_h_initval);
}
static struct gtp5g_far *find_far_by_id(struct gtp5g_dev *gtp, u64 seid, u32 far_id)
{
struct hlist_head *head;
struct gtp5g_far *far;
char seid_far_id_hexstr[SEID_U32ID_HEX_STR_LEN] = {0};
seid_far_id_to_hex_str(seid, far_id, seid_far_id_hexstr);
head = >p->far_id_hash[str_hashfn(seid_far_id_hexstr) % gtp->hash_size];
hlist_for_each_entry_rcu(far, head, hlist_id) {
if (far->seid == seid && far->id == far_id)
return far;
}
return NULL;
}
static int far_fill(struct gtp5g_far *far, struct gtp5g_dev *gtp, struct genl_info *info,
u8 *flag, struct gtp5g_emark_pktinfo *epkt_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;
struct forwarding_policy *fwd_policy;
struct gtp5g_pdr *pdr;
struct hlist_head *head;
char seid_far_id_hexstr[SEID_U32ID_HEX_STR_LEN] = {0};
if (!far) {
GTP5G_ERR(NULL, "Far is null\n");
return -EINVAL;
}
far->id = nla_get_u32(info->attrs[GTP5G_FAR_ID]);
if (info->attrs[GTP5G_FAR_SEID])
far->seid = nla_get_u64(info->attrs[GTP5G_FAR_SEID]);
else
far->seid = 0;
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) {
GTP5G_ERR(NULL, "Failed to allocate memory for FAR fwd param\n");
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 (!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]) {
GTP5G_ERR(NULL, "Header createion attributes error\n");
return -EINVAL;
}
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) {
GTP5G_ERR(NULL, "Failed to allocate FAR fwd Hdr creation\n");
return -ENOMEM;
}
hdr_creation = far->fwd_param->hdr_creation;
hdr_creation->description = nla_get_u16(hdr_creation_attrs[GTP5G_OUTER_HEADER_CREATION_DESCRIPTION]);
hdr_creation->teid = htonl(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 = htons(nla_get_u16(hdr_creation_attrs[GTP5G_OUTER_HEADER_CREATION_PORT]));
} else {
u32 old_teid, old_peer_addr;
u16 old_port;
hdr_creation = far->fwd_param->hdr_creation;
old_teid = hdr_creation->teid;
old_peer_addr = hdr_creation->peer_addr_ipv4.s_addr;
old_port = hdr_creation->port;
hdr_creation->description = nla_get_u16(hdr_creation_attrs[GTP5G_OUTER_HEADER_CREATION_DESCRIPTION]);
hdr_creation->teid = htonl(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 = htons(nla_get_u16(hdr_creation_attrs[GTP5G_OUTER_HEADER_CREATION_PORT]));
/* For Downlink traffic from UPF to gNB
* In some cases,
* 1) SMF will send PFCP Msg filled with FAR's TEID and gNB N3 addr as 0
* 2) Later time, SMF will send PFCP Msg filled with right value in 1)
* 2.a) We should send the GTP-U EndMarker to gNB
* 2.b) SHOULD not set the flag as 1
* 3) Xn Handover in b/w gNB then
* 3.a) SMF will send modification of PDR, FAR(TEID and GTP-U)
* 3.b) SHOULD set the flag as 1 and send GTP-U Marker for old gNB
* */
/* R15.3 29.281
* 5.1 General format
* When setting up a GTP-U tunnel, the GTP-U entity shall not assign the value 'all zeros' to its own TEID.
* However, for backward compatibility, if a GTP-U entity receives (via respective control plane message) a peer's
* TEID that is set to the value 'all zeros', the GTP-U entity shall accept this value as valid and send the subsequent
* G-PDU with the TEID field in the header set to the value 'all zeros'.
* */
if ((flag != NULL && epkt_info != NULL)) {
if (((old_peer_addr & hdr_creation->peer_addr_ipv4.s_addr) != 0) &&
((old_teid != hdr_creation->teid ) ||
(old_peer_addr != hdr_creation->peer_addr_ipv4.s_addr))) {
*flag = 1;
epkt_info->teid = old_teid;
epkt_info->peer_addr = old_peer_addr;
epkt_info->gtph_port = old_port;
}
}
}
}
if (fwd_param_attrs[GTP5G_FORWARDING_PARAMETER_FORWARDING_POLICY]) {
if (!far->fwd_param->fwd_policy) {
far->fwd_param->fwd_policy = kzalloc(sizeof(*far->fwd_param->fwd_policy),
GFP_ATOMIC);
if (!far->fwd_param->fwd_policy) {
GTP5G_ERR(NULL, "Failed to allocate FAR fwd policy\n");
return -ENOMEM;
}
}
fwd_policy = far->fwd_param->fwd_policy;
fwd_policy->len = nla_len(fwd_param_attrs[GTP5G_FORWARDING_PARAMETER_FORWARDING_POLICY]);
if (fwd_policy->len >= sizeof(fwd_policy->identifier)) {
GTP5G_ERR(NULL, "Failed FAR fwd policy length is bigger\n");
return -EINVAL;
}
strncpy(fwd_policy->identifier,
nla_data(fwd_param_attrs[GTP5G_FORWARDING_PARAMETER_FORWARDING_POLICY]),
fwd_policy->len);
/* Exact value to handle forwarding policy */
if (!(fwd_policy->mark = simple_strtol(fwd_policy->identifier, NULL, 10))) {
GTP5G_ERR(NULL, "Failed FAR fwd policy mark not set\n");
return -EINVAL;
}
}
}
/* Update PDRs which has not linked to this FAR */
seid_far_id_to_hex_str(far->seid, far->id, seid_far_id_hexstr);
head = >p->related_far_hash[str_hashfn(seid_far_id_hexstr) % gtp->hash_size];
hlist_for_each_entry_rcu(pdr, head, hlist_related_far) {
if (pdr->seid == far->seid && *pdr->far_id == far->id) {
if (flag != NULL && *flag == 1) {
epkt_info->role_addr = pdr->role_addr_ipv4.s_addr;
epkt_info->sk = pdr->sk;
GTP5G_INF(NULL, "Flag is set, role_addr %#x\n", epkt_info->role_addr);
}
pdr->far = far;
if (unix_sock_client_update(pdr) < 0)
GTP5G_ERR(NULL, "PDR(%u) update fail when FAR(%u) apply action is changed",
pdr->id, far->id);
}
}
return 0;
}
static struct gtp5g_pdr *find_pdr_by_id(struct gtp5g_dev *gtp, u64 seid, u16 pdr_id)
{
struct hlist_head *head;
struct gtp5g_pdr *pdr;
char seid_pdr_id[SEID_U32ID_HEX_STR_LEN] = {0};
seid_pdr_id_to_hex_str(seid, pdr_id, seid_pdr_id);
head = >p->pdr_id_hash[str_hashfn(seid_pdr_id) % gtp->hash_size];
hlist_for_each_entry_rcu(pdr, head, hlist_id) {
if (pdr->seid == seid && pdr->id == pdr_id)
return pdr;
}
return NULL;
}
static int ipv4_match(__be32 target_addr, __be32 ifa_addr, __be32 ifa_mask) {
return !((target_addr ^ ifa_addr) & ifa_mask);
}
static bool ports_match(struct range *match_list, int list_len, __be16 port) {
int i;
bool match = false;
if (!list_len)
return true;
for (i = 0; i < list_len; i++) {
if (match_list[i].start <= port && match_list[i].end >= port){
match = true;
}
}
return match;
}
static int sdf_filter_match(struct sdf_filter *sdf, struct sk_buff *skb,
unsigned int hdrlen, u8 direction)
{
struct iphdr *iph;
struct ip_filter_rule *rule;
const __be16 *pptr;
__be16 _ports[2];
if (!sdf) {
GTP5G_ERR(NULL, "SDF is null\n");
return 1;
}
if (!pskb_may_pull(skb, hdrlen + sizeof(struct iphdr))) {
GTP5G_ERR(NULL, "skb pull fail in sdf filter match\n");
goto mismatch;
}
iph = (struct iphdr *)(skb->data + hdrlen);
if (sdf->rule) {
rule = sdf->rule;
if (rule->direction != direction)
goto mismatch;
if (rule->proto != 0xff && rule->proto != iph->protocol)
goto mismatch;
if (!ipv4_match(iph->saddr, rule->src.s_addr, rule->smask.s_addr))
goto mismatch;
if (!ipv4_match(iph->daddr, rule->dest.s_addr, rule->dmask.s_addr))
goto mismatch;
if (rule->sport_num + rule->dport_num > 0) {
if (!(pptr = skb_header_pointer(skb, hdrlen + sizeof(struct iphdr),
sizeof(_ports), _ports)))
goto mismatch;
if (!ports_match(rule->sport, rule->sport_num, ntohs(pptr[0])))
goto mismatch;
if (!ports_match(rule->dport, rule->dport_num, ntohs(pptr[1])))
goto mismatch;
}
}
if (sdf->tos_traffic_class)
GTP5G_ERR(NULL, "TODO: SDF's ToS traffic class\n");
if (sdf->security_param_idx)
GTP5G_ERR(NULL, "TODO: SDF's Security parameter index\n");
if (sdf->flow_label)
GTP5G_ERR(NULL, "TODO: SDF's Flow label\n");
if (sdf->bi_id)
GTP5G_ERR(NULL, "TODO: SDF's SDF filter id\n");
return 1;
mismatch:
return 0;
}
static struct gtp5g_pdr *pdr_find_by_ipv4(struct gtp5g_dev *gtp, struct sk_buff *skb,
unsigned int hdrlen, __be32 addr)
{
struct hlist_head *head;
struct gtp5g_pdr *pdr;
struct gtp5g_pdi *pdi;
head = >p->addr_hash[ipv4_hashfn(addr) % gtp->hash_size];
hlist_for_each_entry_rcu(pdr, head, hlist_addr) {
pdi = pdr->pdi;
// TODO: Move the value we check into first level
if (!(pdr->af == AF_INET && pdi->ue_addr_ipv4->s_addr == addr))
continue;
if (pdi->sdf)
if (!sdf_filter_match(pdi->sdf, skb, hdrlen, GTP5G_SDF_FILTER_OUT))
continue;
GTP5G_INF(NULL, "Match PDR ID:%d\n", pdr->id);
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;
char seid_far_id_hexstr[SEID_U32ID_HEX_STR_LEN] = {0};
char seid_qer_id_hexstr[SEID_U32ID_HEX_STR_LEN] = {0};
if (!pdr) {
GTP5G_ERR(NULL, "PDR is NULL\n");
return -EINVAL;
}
pdr->af = AF_INET;
pdr->id = nla_get_u16(info->attrs[GTP5G_PDR_ID]);
if (info->attrs[GTP5G_PDR_SEID]) {
api_with_seid = true;
pdr->seid = nla_get_u64(info->attrs[GTP5G_PDR_SEID]);
}
else {
api_with_seid = false;
pdr->seid = 0;
}
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) {
GTP5G_ERR(NULL, "Failed to allocate OHC\n");
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]));
}
/* FAR */
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) {
GTP5G_ERR(NULL, "Failed to allocate FAR ID\n");
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);
seid_far_id_to_hex_str(pdr->seid, *pdr->far_id, seid_far_id_hexstr);
hlist_add_head_rcu(&pdr->hlist_related_far,
>p->related_far_hash[str_hashfn(seid_far_id_hexstr) % gtp->hash_size]);
pdr->far = find_far_by_id(gtp, pdr->seid, *pdr->far_id);
} else {
GTP5G_ERR(NULL, "FAR ID not exist\n");
}
/* QER */
if (info->attrs[GTP5G_PDR_QER_ID]) {
if (!pdr->qer_id) {
pdr->qer_id = kzalloc(sizeof(*pdr->qer_id), GFP_ATOMIC);
if (!pdr->qer_id) {
GTP5G_ERR(NULL, "Failed to allocate memory qer id\n");
return -ENOMEM;
}
}
*pdr->qer_id = nla_get_u32(info->attrs[GTP5G_PDR_QER_ID]);
if (!hlist_unhashed(&pdr->hlist_related_qer))
hlist_del_rcu(&pdr->hlist_related_qer);
seid_qer_id_to_hex_str(pdr->seid, *pdr->qer_id, seid_qer_id_hexstr);
hlist_add_head_rcu(&pdr->hlist_related_qer,
>p->related_qer_hash[str_hashfn(seid_qer_id_hexstr) % gtp->hash_size]);
pdr->qer = find_qer_by_id(gtp, pdr->seid, *pdr->qer_id);
if (!pdr->qer)
GTP5G_ERR(NULL, "Failed to find QER id(%u)\n", *pdr->qer_id);
}
if (unix_sock_client_update(pdr) < 0) {
GTP5G_ERR(NULL, "PDR sock client update fail\n");
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) {
GTP5G_ERR(NULL, "Failed to allocate PDI\n");
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) {
GTP5G_ERR(NULL, "Failed to allocate UE IPv4 address\n");
return -ENOMEM;
}
}
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]) {
GTP5G_ERR(NULL, "TEID is not preset\n");
return -EINVAL;
}
if (!pdi->f_teid) {
pdi->f_teid = kzalloc(sizeof(*pdi->f_teid), GFP_ATOMIC);
if (!pdi->f_teid) {
GTP5G_ERR(NULL, "Failed to allocate UE IPv4 address\n");
return -ENOMEM;
}
}
f_teid = pdi->f_teid;
f_teid->teid = htonl(nla_get_u32(f_teid_attrs[GTP5G_F_TEID_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) {
GTP5G_ERR(NULL, "Failed to allocate SDF\n");
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) {
GTP5G_ERR(NULL, "Failed to allocate SDF's Rule\n");
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]);