-
Notifications
You must be signed in to change notification settings - Fork 3
/
gtp.c
1620 lines (1325 loc) · 36.8 KB
/
gtp.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
/* GTP according to GSM TS 09.60 / 3GPP TS 29.060 */
/* (C) 2012-2014 by sysmocom - s.f.m.c. GmbH
* Author: Harald Welte <[email protected]>
* Pablo Neira Ayuso <[email protected]>
* Andreas Schultz <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*/
#include <linux/module.h>
#include <linux/version.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 <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 "gtp.h"
#include "gtp_nl.h"
static u32 gtp_h_initval;
struct pdp_ctx {
struct hlist_node hlist_tid;
struct hlist_node hlist_addr;
union {
uint64_t tid;
struct {
uint64_t tid;
u16 flow;
} v0;
struct {
uint32_t i_tei;
uint32_t o_tei;
} v1;
} u;
u8 gtp_version;
u16 af;
union {
struct in6_addr ip6;
struct in_addr ip4;
} ms_addr;
union {
struct in6_addr ip6;
struct in_addr ip4;
} sgsn_addr;
atomic_t tx_seq;
struct rcu_head rcu_head;
};
/* One local instance of the GTP code base */
struct gtp_instance {
struct list_head list;
/* the socket */
struct socket *sock0;
struct socket *sock1u;
struct net_device *dev;
unsigned int hash_size;
struct hlist_head *tid_hash;
struct hlist_head *addr_hash;
};
static int gtp_net_id __read_mostly;
struct gtp_net {
struct list_head gtp_instance_list;
};
static void gtp_encap_disable(struct gtp_instance *gti);
static inline u32 gtp0_hashfn(u64 tid)
{
u32 *tid32 = (u32 *) &tid;
return jhash_2words(tid32[0], tid32[1], gtp_h_initval);
}
static inline u32 gtp1u_hashfn(u32 tid)
{
return jhash_1word(tid, gtp_h_initval);
}
static inline u32 ipv4_hashfn(u32 ip)
{
return jhash_1word(ip, gtp_h_initval);
}
static inline u32 ipv6_hashfn(struct in6_addr *ip6)
{
return jhash2((const u32 *) &ip6->s6_addr32, sizeof(*ip6)/sizeof(u32),
gtp_h_initval);
}
/* resolve a PDP context structure based on the 64bit TID */
static struct pdp_ctx *gtp0_pdp_find(struct gtp_instance *gti, u64 tid)
{
struct hlist_head *head;
struct pdp_ctx *pdp;
head = >i->tid_hash[gtp0_hashfn(tid) % gti->hash_size];
hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
if (pdp->gtp_version == GTP_V0 && pdp->u.v0.tid == tid)
return pdp;
}
return NULL;
}
/* resolve a PDP context structure based on the 32bit TEI */
static struct pdp_ctx *gtp1_pdp_find(struct gtp_instance *gti, u32 tid)
{
struct hlist_head *head;
struct pdp_ctx *pdp;
head = >i->tid_hash[gtp1u_hashfn(tid) % gti->hash_size];
hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
if (pdp->gtp_version == GTP_V1 && pdp->u.v1.i_tei == tid)
return pdp;
}
return NULL;
}
/* resolve a PDP context based on IPv4 address of MS */
static struct pdp_ctx *ipv4_pdp_find(struct gtp_instance *gti,
u32 ms_addr)
{
struct hlist_head *head;
struct pdp_ctx *pdp;
head = >i->addr_hash[ipv4_hashfn(ms_addr) % gti->hash_size];
hlist_for_each_entry_rcu(pdp, head, hlist_addr) {
if (pdp->af == AF_INET && pdp->ms_addr.ip4.s_addr == ms_addr)
return pdp;
}
return NULL;
}
/* resolve a PDP context based on IPv6 address of MS */
static struct pdp_ctx *ipv6_pdp_find(struct gtp_instance *gti,
struct in6_addr *ms_addr)
{
struct hlist_head *head;
struct pdp_ctx *pdp;
head = >i->addr_hash[ipv6_hashfn(ms_addr) % gti->hash_size];
hlist_for_each_entry_rcu(pdp, head, hlist_addr) {
if (pdp->af == AF_INET6 &&
!memcmp(&pdp->ms_addr.ip6, ms_addr, sizeof(*ms_addr)))
return pdp;
}
return NULL;
}
/* resolve the GTP instance for a given sock */
static inline struct gtp_instance *sk_to_gti(struct sock *sk)
{
struct gtp_instance *gti;
if (!sk)
return NULL;
sock_hold(sk);
gti = (struct gtp_instance *) sk->sk_user_data;
if (!gti) {
sock_put(sk);
return NULL;
}
return gti;
}
/* Check if the inner IP header has the source address assigned to the
* current MS.
*/
static bool gtp_check_src_ms(struct sk_buff *skb, struct pdp_ctx *pctx,
unsigned int hdrlen)
{
bool ret = false;
if (skb->protocol == ntohs(ETH_P_IP)) {
struct iphdr *iph;
if (!pskb_may_pull(skb, hdrlen + sizeof(struct iphdr)))
return false;
iph = (struct iphdr *)
(skb->data + hdrlen + sizeof(struct iphdr));
ret = (iph->saddr != pctx->ms_addr.ip4.s_addr);
} else if (skb->protocol == ntohs(ETH_P_IPV6)) {
struct ipv6hdr *ip6h;
if (!pskb_may_pull(skb, hdrlen + sizeof(struct ipv6hdr)))
return false;
ip6h = (struct ipv6hdr *)
(skb->data + hdrlen + sizeof(struct ipv6hdr));
ret = memcmp(&ip6h->saddr, &pctx->ms_addr.ip6,
sizeof(struct in6_addr)) == 0;
}
return ret;
}
/* 1 means pass up to the stack, -1 means drop and 0 means decapsulated */
static int gtp0_udp_encap_recv(struct gtp_instance *gti, struct sk_buff *skb)
{
struct gtp0_header *gtp0;
struct pdp_ctx *pctx;
unsigned int hdrlen = sizeof(struct udphdr) + sizeof(*gtp0);
int ret = 0;
/* check for sufficient header size */
if (!pskb_may_pull(skb, hdrlen))
return -1;
gtp0 = (struct gtp0_header *)(skb->data + sizeof(struct udphdr));
/* check for GTP Version 0 */
if ((gtp0->flags >> 5) != GTP_V0)
return 1;
/* check if it is T-PDU. if not -> userspace */
if (gtp0->type != GTP_TPDU)
return 1;
rcu_read_lock();
/* look-up the PDP context for the Tunnel ID */
pctx = gtp0_pdp_find(gti, be64_to_cpu(gtp0->tid));
if (!pctx) {
ret = -1;
goto out_rcu;
}
if (!gtp_check_src_ms(skb, pctx, hdrlen)) {
ret = -1;
goto out_rcu;
}
/* get rid of the GTP + UDP headers */
__skb_pull(skb, hdrlen);
out_rcu:
rcu_read_unlock();
return ret;
}
static int gtp1u_udp_encap_recv(struct gtp_instance *gti, struct sk_buff *skb)
{
struct gtp1_header *gtp1;
struct pdp_ctx *pctx;
unsigned int hdrlen = sizeof(struct udphdr) + sizeof(*gtp1);
int ret = 0;
/* check for sufficient header size */
if (!pskb_may_pull(skb, hdrlen))
return -1;
gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
/* check for GTP Version 1 */
if ((gtp1->flags >> 5) != GTP_V1)
return 1;
/* check if it is T-PDU. */
if (gtp1->type != GTP_TPDU)
return 1;
/* From 29.060: "This field shall be present if and only if any one or
* more of the S, PN and E flags are set.".
*
* If any of the bit is set, then the remaining ones also have to be
* set.
*/
if (gtp1->flags & GTP1_F_MASK)
hdrlen += 4;
/* check for sufficient header size for extension */
if (!pskb_may_pull(skb, hdrlen))
return -1;
/* look-up the PDP context for the Tunnel ID */
rcu_read_lock();
pctx = gtp1_pdp_find(gti, ntohl(gtp1->tid));
if (!pctx) {
ret = -1;
goto out_rcu;
}
if (!gtp_check_src_ms(skb, pctx, hdrlen)) {
ret = -1;
goto out_rcu;
}
/* get rid of the UDP + GTP header + extensions */
__skb_pull(skb, hdrlen);
out_rcu:
rcu_read_unlock();
return ret;
}
static void gtp_udp_encap_destroy(struct sock *sk)
{
struct gtp_instance *gti = sk_to_gti(sk);
if (gti) {
gtp_encap_disable(gti);
sock_put(sk);
}
}
/* UDP encapsulation receive handler. See net/ipv4/udp.c.
* Return codes: 0: success, <0: error, >0: passed up to userspace UDP.
*/
static int gtp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
{
struct gtp_instance *gti;
int ret;
/* resolve the GTP instance to which the socket belongs */
gti = sk_to_gti(sk);
if (!gti)
goto user;
netdev_dbg(gti->dev, "encap_recv %p\n", sk);
switch (udp_sk(sk)->encap_type) {
case UDP_ENCAP_GTP0:
netdev_dbg(gti->dev, "received GTP0 packet\n");
ret = gtp0_udp_encap_recv(gti, skb);
break;
case UDP_ENCAP_GTP1U:
netdev_dbg(gti->dev, "received GTP1U packet\n");
ret = gtp1u_udp_encap_recv(gti, skb);
break;
default:
ret = -1; /* shouldn't happen */
}
switch (ret) {
case 1:
netdev_dbg(gti->dev, "pass up to the process\n");
goto user_put;
case 0:
netdev_dbg(gti->dev, "forwarding packet from GGSN to uplink\n");
break;
case -1:
netdev_dbg(gti->dev, "GTP packet has been dropped\n");
goto drop;
}
/* Now that the UDP and the GTP header have been removed, set up the
* new network header. This is required by the upper later to
* calculate the transport header.
*/
skb_reset_network_header(skb);
/* re-submit via virtual tunnel device into regular network stack */
secpath_reset(skb);
skb_dst_drop(skb);
nf_reset(skb);
skb->dev = gti->dev;
/* Force the upper layers to verify it. */
skb->ip_summed = CHECKSUM_NONE;
netif_rx(skb);
sock_put(sk);
return 0;
drop:
kfree_skb(skb);
return 0;
user_put:
sock_put(sk);
user:
return 1;
}
static int gtp_dev_init(struct net_device *dev)
{
struct gtp_instance *gti = netdev_priv(dev);
gti->dev = dev;
dev->tstats = alloc_percpu(struct pcpu_sw_netstats);
if (!dev->tstats)
return -ENOMEM;
return 0;
}
static void gtp_dev_uninit(struct net_device *dev)
{
struct gtp_instance *gti = netdev_priv(dev);
gtp_encap_disable(gti);
free_percpu(dev->tstats);
}
#define IP_UDP_LEN (sizeof(struct iphdr) + sizeof(struct udphdr))
static inline void init_gtp_flow(struct flowi4 *fl4,
const struct sock *sk,
__be32 daddr)
{
memset(fl4, 0, sizeof(*fl4));
fl4->flowi4_oif = sk->sk_bound_dev_if;
fl4->daddr = daddr;
fl4->saddr = inet_sk(sk)->inet_saddr;
fl4->flowi4_tos = RT_CONN_FLAGS(sk);
fl4->flowi4_proto = sk->sk_protocol;
}
static struct rtable *
ip4_route_output_gtp(struct net *net, struct flowi4 *fl4,
const struct sock *sk,
__be32 daddr)
{
init_gtp_flow(fl4, sk, daddr);
return ip_route_output_key(net, fl4);
}
static struct dst_entry *
ip6_route_output_gtp(struct net *net, struct flowi6 *fl6,
const struct sock *sk,
struct in6_addr *daddr)
{
memset(fl6, 0, sizeof(*fl6));
fl6->flowi6_oif = sk->sk_bound_dev_if;
fl6->daddr = *daddr;
fl6->saddr = inet6_sk(sk)->saddr;
fl6->flowi6_tos = RT_CONN_FLAGS(sk);
fl6->flowi6_proto = sk->sk_protocol;
return ip6_route_output(net, NULL, fl6);
}
static inline void
gtp0_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
{
struct gtp0_header *gtp0;
int payload_len = skb->len;
/* ensure there is sufficient headroom */
gtp0 = (struct gtp0_header *) skb_push(skb, sizeof(*gtp0));
gtp0->flags = 0x1e; /* V0, GTP-non-prime */
gtp0->type = GTP_TPDU;
gtp0->length = htons(payload_len);
gtp0->seq = htons((atomic_inc_return(&pctx->tx_seq)-1) % 0xffff);
gtp0->flow = htons(pctx->u.v0.flow);
gtp0->number = 0xFF;
gtp0->spare[0] = gtp0->spare[1] = gtp0->spare[2] = 0xFF;
gtp0->tid = cpu_to_be64(pctx->u.v0.tid);
}
static inline void
gtp1_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
{
struct gtp1_header *gtp1;
int payload_len = skb->len;
/* ensure there is sufficient headroom */
gtp1 = (struct gtp1_header *) skb_push(skb, sizeof(*gtp1));
/* Bits 8 7 6 5 4 3 2 1
* +--+--+--+--+--+--+--+--+
* |version |PT| 1| E| S|PN|
* +--+--+--+--+--+--+--+--+
* 0 0 1 1 1 0 0 0
*/
gtp1->flags = 0x38; /* V1, GTP-non-prime */
gtp1->type = GTP_TPDU;
gtp1->length = htons(payload_len);
gtp1->tid = htonl(pctx->u.v1.o_tei);
/* TODO: Suppport for extension header, sequence number and N-PDU.
* Update the length field if any of them is available.
*/
}
/* From Linux kernel 3.13: iptunnel_xmit_stats() */
static inline void
gtp_iptunnel_xmit_stats(int err, struct net_device_stats *err_stats,
struct pcpu_sw_netstats __percpu *stats)
{
if (err > 0) {
struct pcpu_sw_netstats *tstats = this_cpu_ptr(stats);
u64_stats_update_begin(&tstats->syncp);
tstats->tx_bytes += err;
tstats->tx_packets++;
u64_stats_update_end(&tstats->syncp);
} else if (err < 0) {
err_stats->tx_errors++;
err_stats->tx_aborted_errors++;
} else {
err_stats->tx_dropped++;
}
}
struct gtp_pktinfo {
struct sock *sk;
union {
struct iphdr *iph;
struct ipv6hdr *ip6h;
};
union {
struct flowi4 fl4;
struct flowi6 fl6;
};
union {
struct rtable *rt;
struct dst_entry *ndst;
};
struct pdp_ctx *pctx;
struct net_device *dev;
};
static inline void
gtp_set_pktinfo_ipv4(struct gtp_pktinfo *pktinfo, struct sock *sk,
struct iphdr *iph,
struct pdp_ctx *pctx, struct rtable *rt,
struct flowi4 *fl4, struct net_device *dev)
{
pktinfo->sk = sk;
pktinfo->iph = iph;
pktinfo->pctx = pctx;
pktinfo->rt = rt;
pktinfo->fl4 = *fl4;
pktinfo->dev = dev;
}
static int gtp_ip4_prepare_xmit(struct sk_buff *skb, struct net_device *dev,
struct gtp_pktinfo *pktinfo)
{
struct gtp_instance *gti = netdev_priv(dev);
struct sock *sk;
struct iphdr *iph;
struct pdp_ctx *pctx;
struct rtable *rt;
struct flowi4 fl4;
int df, mtu;
/* Read the IP destination address and resolve the PDP context.
* Prepend PDP header with TEI/TID from PDP ctx.
*/
iph = ip_hdr(skb);
pctx = ipv4_pdp_find(gti, iph->daddr);
if (!pctx) {
netdev_dbg(dev, "no PDP ctx found for this packet, skip\n");
return -ENOENT;
}
netdev_dbg(dev, "found PDP context %p\n", pctx);
/* Obtain route for the new encapsulated GTP packet */
switch (pctx->gtp_version) {
case GTP_V0:
sk = gti->sock0->sk;
break;
case GTP_V1:
sk = gti->sock1u->sk;
break;
default:
return -ENOENT;
}
rt = ip4_route_output_gtp(sock_net(sk), &fl4,
gti->sock0->sk,
pctx->sgsn_addr.ip4.s_addr);
if (IS_ERR(rt)) {
netdev_dbg(dev, "no route to SSGN %pI4\n",
&pctx->sgsn_addr.ip4.s_addr);
dev->stats.tx_carrier_errors++;
goto err;
}
/* There is a routing loop */
if (rt->dst.dev == dev) {
netdev_dbg(dev, "circular route to SSGN %pI4\n",
&pctx->sgsn_addr.ip4.s_addr);
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) - dev->hard_header_len -
sizeof(struct iphdr) - sizeof(struct udphdr);
switch (pctx->gtp_version) {
case GTP_V0:
mtu -= sizeof(struct gtp0_header);
break;
case GTP_V1:
mtu -= sizeof(struct gtp1_header);
break;
}
} else
mtu = dst_mtu(&rt->dst);
rt->dst.ops->update_pmtu(&rt->dst, NULL, skb, mtu);
if (!skb_is_gso(skb) && (iph->frag_off & htons(IP_DF)) &&
mtu < ntohs(iph->tot_len)) {
netdev_dbg(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;
}
gtp_set_pktinfo_ipv4(pktinfo, sk, iph, pctx, rt, &fl4, dev);
return 0;
err_rt:
ip_rt_put(rt);
err:
return -EBADMSG;
}
static inline void
gtp_set_pktinfo_ipv6(struct gtp_pktinfo *pktinfo, struct sock *sk,
struct ipv6hdr *ip6h,
struct pdp_ctx *pctx, struct dst_entry *ndst,
struct flowi6 *fl6, struct net_device *dev)
{
pktinfo->sk = sk;
pktinfo->ip6h = ip6h;
pktinfo->pctx = pctx;
pktinfo->ndst = ndst;
pktinfo->fl6 = *fl6;
pktinfo->dev = dev;
}
static int gtp_ip6_prepare_xmit(struct sk_buff *skb, struct net_device *dev,
struct gtp_pktinfo *pktinfo)
{
struct gtp_instance *gti = netdev_priv(dev);
struct sock *sk;
struct ipv6hdr *ipv6h;
struct pdp_ctx *pctx;
struct dst_entry *ndst;
struct flowi6 fl6;
int mtu;
/* Read the IP destination address and resolve the PDP context.
* Prepend PDP header with TEI/TID from PDP ctx.
*/
ipv6h = ipv6_hdr(skb);
pctx = ipv6_pdp_find(gti, &ipv6h->daddr);
if (!pctx) {
netdev_dbg(dev, "no PDP ctx found for this packet, skip\n");
return -ENOENT;
}
netdev_dbg(dev, "found PDP context %p\n", pctx);
/* Obtain route for the new encapsulated GTP packet */
switch (pctx->gtp_version) {
case GTP_V0:
sk = gti->sock0->sk;
break;
case GTP_V1:
sk = gti->sock1u->sk;
break;
default:
return -ENOENT;
}
ndst = ip6_route_output_gtp(sock_net(sk), &fl6,
gti->sock0->sk,
&pctx->sgsn_addr.ip6);
if (IS_ERR(ndst)) {
netdev_dbg(dev, "no route to SSGN %pI6\n",
&pctx->sgsn_addr.ip6.s6_addr);
dev->stats.tx_carrier_errors++;
goto err;
}
/* There is a routing loop */
if (ndst->dev == dev) {
netdev_dbg(dev, "circular route to SSGN %pI6\n",
&pctx->sgsn_addr.ip6);
dev->stats.collisions++;
goto err_dst;
}
skb_dst_drop(skb);
mtu = dst_mtu(ndst) - dev->hard_header_len -
sizeof(struct ipv6hdr) - sizeof(struct udphdr);
switch (pctx->gtp_version) {
case GTP_V0:
mtu -= sizeof(struct gtp0_header);
break;
case GTP_V1:
mtu -= sizeof(struct gtp1_header);
break;
}
ndst->ops->update_pmtu(ndst, NULL, skb, mtu);
if (!skb_is_gso(skb) && skb->len > mtu) {
netdev_dbg(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_dst;
}
gtp_set_pktinfo_ipv6(pktinfo, sk, ipv6h, pctx, ndst, &fl6, dev);
return 0;
err_dst:
dst_release(ndst);
err:
return -EBADMSG;
}
static inline int
gtp_udp_tunnel_xmit(struct sk_buff *skb, __be16 port,
struct gtp_pktinfo *pktinfo)
{
netdev_dbg(pktinfo->dev, "gtp -> IP src: %pI4 dst: %pI4\n",
&pktinfo->iph->saddr, &pktinfo->iph->daddr);
return udp_tunnel_xmit_skb(pktinfo->rt, pktinfo->sk, skb,
pktinfo->fl4.saddr,
pktinfo->fl4.daddr,
pktinfo->iph->tos,
ip4_dst_hoplimit(&pktinfo->rt->dst),
htons(IP_DF), port, port, true, false);
}
static inline int
gtp_ip6tunnel_xmit(struct sk_buff *skb, __be16 port,
struct gtp_pktinfo *pktinfo)
{
netdev_dbg(pktinfo->dev, "gtp -> IP src: %pI6 dst: %pI6\n",
&pktinfo->ip6h->saddr, &pktinfo->ip6h->daddr);
return udp_tunnel6_xmit_skb(pktinfo->ndst, pktinfo->sk, skb,
pktinfo->ndst->dev,
&pktinfo->fl6.saddr,
&pktinfo->fl6.daddr,
0,
ip6_dst_hoplimit(pktinfo->ndst),
port, port, false);
}
static netdev_tx_t gtp_dev_xmit(struct sk_buff *skb, struct net_device *dev)
{
struct gtp_pktinfo pktinfo;
unsigned int proto = ntohs(skb->protocol);
int gtph_len, err = -EINVAL;
__be16 gtph_port;
rcu_read_lock();
/* ensure there is sufficient headroom */
if (skb_cow_head(skb, dev->needed_headroom))
goto tx_error;
skb_reset_inner_headers(skb);
switch (proto) {
case ETH_P_IP:
err = gtp_ip4_prepare_xmit(skb, dev, &pktinfo);
break;
case ETH_P_IPV6:
err = gtp_ip6_prepare_xmit(skb, dev, &pktinfo);
break;
}
if (err < 0)
goto tx_error;
/* Push down GTP header */
switch (pktinfo.pctx->gtp_version) {
case GTP_V0:
gtph_port = htons(GTP0_PORT);
gtph_len = sizeof(struct gtp0_header);
gtp0_push_header(skb, pktinfo.pctx);
break;
case GTP_V1:
gtph_port = htons(GTP1U_PORT);
gtph_len = sizeof(struct gtp1_header);
gtp1_push_header(skb, pktinfo.pctx);
break;
default:
goto tx_error;
}
switch (proto) {
case ETH_P_IP:
err = gtp_udp_tunnel_xmit(skb, gtph_port, &pktinfo);
break;
case ETH_P_IPV6:
err = gtp_ip6tunnel_xmit(skb, gtph_port, &pktinfo);
break;
}
rcu_read_unlock();
gtp_iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
return NETDEV_TX_OK;
tx_error:
rcu_read_unlock();
dev->stats.tx_errors++;
dev_kfree_skb(skb);
return NETDEV_TX_OK;
}
static const struct net_device_ops gtp_netdev_ops = {
.ndo_init = gtp_dev_init,
.ndo_uninit = gtp_dev_uninit,
.ndo_start_xmit = gtp_dev_xmit,
};
static void gtp_link_setup(struct net_device *dev)
{
dev->netdev_ops = >p_netdev_ops;
dev->destructor = free_netdev;
dev->hard_header_len = 0;
dev->addr_len = 0;
/* Zero header length */
dev->type = ARPHRD_NONE;
dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
dev->tx_queue_len = 1000;
dev->priv_flags |= IFF_NO_QUEUE;
dev->features |= NETIF_F_LLTX;
netif_keep_dst(dev);
dev->needed_headroom = LL_MAX_HEADER
+ sizeof(struct iphdr)
+ sizeof(struct udphdr)
+ sizeof(struct gtp0_header);
}
static int gtp_hashtable_new(struct gtp_instance *gti, int hsize);
static void gtp_hashtable_free(struct gtp_instance *gti);
static int gtp_encap_enable(struct net_device *dev, struct gtp_instance *gti,
int fd_gtp0, int fd_gtp1);
static int gtp_newlink(struct net *src_net, struct net_device *dev,
struct nlattr *tb[], struct nlattr *data[])
{
struct gtp_net *gn;
struct gtp_instance *gti;
int hashsize, err, fd0, fd1;
if (!tb[IFLA_MTU])
dev->mtu = 1500;
gti = netdev_priv(dev);
fd0 = nla_get_u32(data[IFLA_GTP_FD0]);
fd1 = nla_get_u32(data[IFLA_GTP_FD1]);
err = gtp_encap_enable(dev, gti, fd0, fd1);
if (err < 0)
goto out_err;
if (!data[IFLA_GTP_HASHSIZE])
hashsize = 1024;
else
hashsize = nla_get_u32(data[IFLA_GTP_HASHSIZE]);
err = gtp_hashtable_new(gti, hashsize);
if (err < 0)
goto out_encap;
err = register_netdevice(dev);
if (err < 0) {
netdev_dbg(dev, "failed to register new netdev %d\n", err);
goto out_hashtable;
}
gn = net_generic(dev_net(dev), gtp_net_id);
list_add_rcu(>i->list, &gn->gtp_instance_list);
netdev_dbg(dev, "registered new GTP interface\n");
return 0;
out_hashtable:
gtp_hashtable_free(gti);
out_encap:
gtp_encap_disable(gti);
out_err:
return err;
}
static void gtp_dellink(struct net_device *dev, struct list_head *head)
{
struct gtp_instance *gti = netdev_priv(dev);
gtp_encap_disable(gti);
gtp_hashtable_free(gti);
list_del_rcu(>i->list);
unregister_netdevice_queue(dev, head);
}
static const struct nla_policy gtp_policy[IFLA_GTP_MAX + 1] = {
[IFLA_GTP_FD0] = { .type = NLA_U32 },
[IFLA_GTP_FD1] = { .type = NLA_U32 },
[IFLA_GTP_HASHSIZE] = { .type = NLA_U32 },
};
static int gtp_validate(struct nlattr *tb[], struct nlattr *data[])
{
if (!data || !data[IFLA_GTP_FD0] || !data[IFLA_GTP_FD1])
return -EINVAL;
return 0;
}
static size_t gtp_get_size(const struct net_device *dev)
{
return nla_total_size(sizeof(__u32)); /* IFLA_GTP_HASHSIZE */
}
static int gtp_fill_info(struct sk_buff *skb, const struct net_device *dev)
{
struct gtp_instance *gti = netdev_priv(dev);
if (nla_put_u32(skb, IFLA_GTP_HASHSIZE, gti->hash_size))
goto nla_put_failure;
return 0;
nla_put_failure:
return -EMSGSIZE;
}
static struct rtnl_link_ops gtp_link_ops __read_mostly = {
.kind = "gtp",
.maxtype = IFLA_GTP_MAX,
.policy = gtp_policy,
.priv_size = sizeof(struct gtp_instance),
.setup = gtp_link_setup,
.validate = gtp_validate,
.newlink = gtp_newlink,
.dellink = gtp_dellink,
.get_size = gtp_get_size,
.fill_info = gtp_fill_info,
};
static struct net *gtp_genl_get_net(struct net *src_net, struct nlattr *tb[])
{
struct net *net;
/* Examine the link attributes and figure out which
* network namespace we are talking about.
*/
if (tb[GTPA_NET_NS_FD])
net = get_net_ns_by_fd(nla_get_u32(tb[GTPA_NET_NS_FD]));