blob: 3e1854f34420b5a7ae1e7e13a9805156035dccc8 [file] [log] [blame]
Pablo Neira459aa662016-05-09 00:55:48 +02001/* GTP according to GSM TS 09.60 / 3GPP TS 29.060
2 *
3 * (C) 2012-2014 by sysmocom - s.f.m.c. GmbH
4 * (C) 2016 by Pablo Neira Ayuso <pablo@netfilter.org>
5 *
6 * Author: Harald Welte <hwelte@sysmocom.de>
7 * Pablo Neira Ayuso <pablo@netfilter.org>
8 * Andreas Schultz <aschultz@travelping.com>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17
18#include <linux/module.h>
Pablo Neira459aa662016-05-09 00:55:48 +020019#include <linux/skbuff.h>
20#include <linux/udp.h>
21#include <linux/rculist.h>
22#include <linux/jhash.h>
23#include <linux/if_tunnel.h>
24#include <linux/net.h>
25#include <linux/file.h>
26#include <linux/gtp.h>
27
28#include <net/net_namespace.h>
29#include <net/protocol.h>
30#include <net/ip.h>
31#include <net/udp.h>
32#include <net/udp_tunnel.h>
33#include <net/icmp.h>
34#include <net/xfrm.h>
35#include <net/genetlink.h>
36#include <net/netns/generic.h>
37#include <net/gtp.h>
38
39/* An active session for the subscriber. */
40struct pdp_ctx {
41 struct hlist_node hlist_tid;
42 struct hlist_node hlist_addr;
43
44 union {
45 u64 tid;
46 struct {
47 u64 tid;
48 u16 flow;
49 } v0;
50 struct {
51 u32 i_tei;
52 u32 o_tei;
53 } v1;
54 } u;
55 u8 gtp_version;
56 u16 af;
57
58 struct in_addr ms_addr_ip4;
59 struct in_addr sgsn_addr_ip4;
60
Andreas Schultz101cfbc2017-03-09 17:43:02 +010061 struct sock *sk;
Andreas Schultz5b171f92017-03-09 17:42:59 +010062 struct net_device *dev;
63
Pablo Neira459aa662016-05-09 00:55:48 +020064 atomic_t tx_seq;
65 struct rcu_head rcu_head;
66};
67
68/* One instance of the GTP device. */
69struct gtp_dev {
70 struct list_head list;
71
Andreas Schultz17886c472017-03-09 17:42:56 +010072 struct sock *sk0;
73 struct sock *sk1u;
Pablo Neira459aa662016-05-09 00:55:48 +020074
Pablo Neira459aa662016-05-09 00:55:48 +020075 struct net_device *dev;
76
77 unsigned int hash_size;
78 struct hlist_head *tid_hash;
79 struct hlist_head *addr_hash;
80};
81
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030082static unsigned int gtp_net_id __read_mostly;
Pablo Neira459aa662016-05-09 00:55:48 +020083
84struct gtp_net {
85 struct list_head gtp_dev_list;
86};
87
88static u32 gtp_h_initval;
89
Andreas Schultz6b5e2e72017-03-09 17:43:01 +010090static void pdp_context_delete(struct pdp_ctx *pctx);
91
Pablo Neira459aa662016-05-09 00:55:48 +020092static inline u32 gtp0_hashfn(u64 tid)
93{
94 u32 *tid32 = (u32 *) &tid;
95 return jhash_2words(tid32[0], tid32[1], gtp_h_initval);
96}
97
98static inline u32 gtp1u_hashfn(u32 tid)
99{
100 return jhash_1word(tid, gtp_h_initval);
101}
102
103static inline u32 ipv4_hashfn(__be32 ip)
104{
105 return jhash_1word((__force u32)ip, gtp_h_initval);
106}
107
108/* Resolve a PDP context structure based on the 64bit TID. */
109static struct pdp_ctx *gtp0_pdp_find(struct gtp_dev *gtp, u64 tid)
110{
111 struct hlist_head *head;
112 struct pdp_ctx *pdp;
113
114 head = &gtp->tid_hash[gtp0_hashfn(tid) % gtp->hash_size];
115
116 hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
117 if (pdp->gtp_version == GTP_V0 &&
118 pdp->u.v0.tid == tid)
119 return pdp;
120 }
121 return NULL;
122}
123
124/* Resolve a PDP context structure based on the 32bit TEI. */
125static struct pdp_ctx *gtp1_pdp_find(struct gtp_dev *gtp, u32 tid)
126{
127 struct hlist_head *head;
128 struct pdp_ctx *pdp;
129
130 head = &gtp->tid_hash[gtp1u_hashfn(tid) % gtp->hash_size];
131
132 hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
133 if (pdp->gtp_version == GTP_V1 &&
134 pdp->u.v1.i_tei == tid)
135 return pdp;
136 }
137 return NULL;
138}
139
140/* Resolve a PDP context based on IPv4 address of MS. */
141static struct pdp_ctx *ipv4_pdp_find(struct gtp_dev *gtp, __be32 ms_addr)
142{
143 struct hlist_head *head;
144 struct pdp_ctx *pdp;
145
146 head = &gtp->addr_hash[ipv4_hashfn(ms_addr) % gtp->hash_size];
147
148 hlist_for_each_entry_rcu(pdp, head, hlist_addr) {
149 if (pdp->af == AF_INET &&
150 pdp->ms_addr_ip4.s_addr == ms_addr)
151 return pdp;
152 }
153
154 return NULL;
155}
156
157static bool gtp_check_src_ms_ipv4(struct sk_buff *skb, struct pdp_ctx *pctx,
158 unsigned int hdrlen)
159{
160 struct iphdr *iph;
161
162 if (!pskb_may_pull(skb, hdrlen + sizeof(struct iphdr)))
163 return false;
164
Lionel Gauthier88edf102016-12-15 22:35:52 +0100165 iph = (struct iphdr *)(skb->data + hdrlen);
Pablo Neira459aa662016-05-09 00:55:48 +0200166
Lionel Gauthier88edf102016-12-15 22:35:52 +0100167 return iph->saddr == pctx->ms_addr_ip4.s_addr;
Pablo Neira459aa662016-05-09 00:55:48 +0200168}
169
170/* Check if the inner IP source address in this packet is assigned to any
171 * existing mobile subscriber.
172 */
173static bool gtp_check_src_ms(struct sk_buff *skb, struct pdp_ctx *pctx,
174 unsigned int hdrlen)
175{
176 switch (ntohs(skb->protocol)) {
177 case ETH_P_IP:
178 return gtp_check_src_ms_ipv4(skb, pctx, hdrlen);
179 }
180 return false;
181}
182
Andreas Schultz101cfbc2017-03-09 17:43:02 +0100183static int gtp_rx(struct pdp_ctx *pctx, struct sk_buff *skb, unsigned int hdrlen)
Andreas Schultz5b171f92017-03-09 17:42:59 +0100184{
185 struct pcpu_sw_netstats *stats;
186
187 if (!gtp_check_src_ms(skb, pctx, hdrlen)) {
188 netdev_dbg(pctx->dev, "No PDP ctx for this MS\n");
189 return 1;
190 }
191
192 /* Get rid of the GTP + UDP headers. */
Andreas Schultz101cfbc2017-03-09 17:43:02 +0100193 if (iptunnel_pull_header(skb, hdrlen, skb->protocol,
194 !net_eq(sock_net(pctx->sk), dev_net(pctx->dev))))
Andreas Schultz5b171f92017-03-09 17:42:59 +0100195 return -1;
196
197 netdev_dbg(pctx->dev, "forwarding packet from GGSN to uplink\n");
198
199 /* Now that the UDP and the GTP header have been removed, set up the
200 * new network header. This is required by the upper layer to
201 * calculate the transport header.
202 */
203 skb_reset_network_header(skb);
204
205 skb->dev = pctx->dev;
206
207 stats = this_cpu_ptr(pctx->dev->tstats);
208 u64_stats_update_begin(&stats->syncp);
209 stats->rx_packets++;
210 stats->rx_bytes += skb->len;
211 u64_stats_update_end(&stats->syncp);
212
213 netif_rx(skb);
214 return 0;
215}
216
Pablo Neira459aa662016-05-09 00:55:48 +0200217/* 1 means pass up to the stack, -1 means drop and 0 means decapsulated. */
Andreas Schultz101cfbc2017-03-09 17:43:02 +0100218static int gtp0_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb)
Pablo Neira459aa662016-05-09 00:55:48 +0200219{
220 unsigned int hdrlen = sizeof(struct udphdr) +
221 sizeof(struct gtp0_header);
222 struct gtp0_header *gtp0;
223 struct pdp_ctx *pctx;
Pablo Neira459aa662016-05-09 00:55:48 +0200224
225 if (!pskb_may_pull(skb, hdrlen))
226 return -1;
227
228 gtp0 = (struct gtp0_header *)(skb->data + sizeof(struct udphdr));
229
230 if ((gtp0->flags >> 5) != GTP_V0)
231 return 1;
232
233 if (gtp0->type != GTP_TPDU)
234 return 1;
235
Pablo Neira459aa662016-05-09 00:55:48 +0200236 pctx = gtp0_pdp_find(gtp, be64_to_cpu(gtp0->tid));
237 if (!pctx) {
238 netdev_dbg(gtp->dev, "No PDP ctx to decap skb=%p\n", skb);
Andreas Schultz1796a812017-01-26 16:21:49 +0100239 return 1;
Pablo Neira459aa662016-05-09 00:55:48 +0200240 }
241
Andreas Schultz101cfbc2017-03-09 17:43:02 +0100242 return gtp_rx(pctx, skb, hdrlen);
Pablo Neira459aa662016-05-09 00:55:48 +0200243}
244
Andreas Schultz101cfbc2017-03-09 17:43:02 +0100245static int gtp1u_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb)
Pablo Neira459aa662016-05-09 00:55:48 +0200246{
247 unsigned int hdrlen = sizeof(struct udphdr) +
248 sizeof(struct gtp1_header);
249 struct gtp1_header *gtp1;
250 struct pdp_ctx *pctx;
Pablo Neira459aa662016-05-09 00:55:48 +0200251
252 if (!pskb_may_pull(skb, hdrlen))
253 return -1;
254
255 gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
256
257 if ((gtp1->flags >> 5) != GTP_V1)
258 return 1;
259
260 if (gtp1->type != GTP_TPDU)
261 return 1;
262
263 /* From 29.060: "This field shall be present if and only if any one or
264 * more of the S, PN and E flags are set.".
265 *
266 * If any of the bit is set, then the remaining ones also have to be
267 * set.
268 */
269 if (gtp1->flags & GTP1_F_MASK)
270 hdrlen += 4;
271
272 /* Make sure the header is larger enough, including extensions. */
273 if (!pskb_may_pull(skb, hdrlen))
274 return -1;
275
Pablo Neira93edb8c2016-05-10 21:33:38 +0200276 gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
277
Pablo Neira459aa662016-05-09 00:55:48 +0200278 pctx = gtp1_pdp_find(gtp, ntohl(gtp1->tid));
279 if (!pctx) {
280 netdev_dbg(gtp->dev, "No PDP ctx to decap skb=%p\n", skb);
Andreas Schultz1796a812017-01-26 16:21:49 +0100281 return 1;
Pablo Neira459aa662016-05-09 00:55:48 +0200282 }
283
Andreas Schultz101cfbc2017-03-09 17:43:02 +0100284 return gtp_rx(pctx, skb, hdrlen);
Pablo Neira459aa662016-05-09 00:55:48 +0200285}
286
Pablo Neira459aa662016-05-09 00:55:48 +0200287static void gtp_encap_destroy(struct sock *sk)
288{
289 struct gtp_dev *gtp;
290
291 gtp = rcu_dereference_sk_user_data(sk);
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100292 if (gtp) {
293 udp_sk(sk)->encap_type = 0;
294 rcu_assign_sk_user_data(sk, NULL);
295 sock_put(sk);
296 }
297}
298
299static void gtp_encap_disable_sock(struct sock *sk)
300{
301 if (!sk)
302 return;
303
304 gtp_encap_destroy(sk);
305}
306
307static void gtp_encap_disable(struct gtp_dev *gtp)
308{
309 gtp_encap_disable_sock(gtp->sk0);
310 gtp_encap_disable_sock(gtp->sk1u);
Pablo Neira459aa662016-05-09 00:55:48 +0200311}
312
313/* UDP encapsulation receive handler. See net/ipv4/udp.c.
314 * Return codes: 0: success, <0: error, >0: pass up to userspace UDP socket.
315 */
316static int gtp_encap_recv(struct sock *sk, struct sk_buff *skb)
317{
Pablo Neira459aa662016-05-09 00:55:48 +0200318 struct gtp_dev *gtp;
Andreas Schultz5b171f92017-03-09 17:42:59 +0100319 int ret = 0;
Pablo Neira459aa662016-05-09 00:55:48 +0200320
321 gtp = rcu_dereference_sk_user_data(sk);
322 if (!gtp)
323 return 1;
324
325 netdev_dbg(gtp->dev, "encap_recv sk=%p\n", sk);
326
Pablo Neira459aa662016-05-09 00:55:48 +0200327 switch (udp_sk(sk)->encap_type) {
328 case UDP_ENCAP_GTP0:
329 netdev_dbg(gtp->dev, "received GTP0 packet\n");
Andreas Schultz101cfbc2017-03-09 17:43:02 +0100330 ret = gtp0_udp_encap_recv(gtp, skb);
Pablo Neira459aa662016-05-09 00:55:48 +0200331 break;
332 case UDP_ENCAP_GTP1U:
333 netdev_dbg(gtp->dev, "received GTP1U packet\n");
Andreas Schultz101cfbc2017-03-09 17:43:02 +0100334 ret = gtp1u_udp_encap_recv(gtp, skb);
Pablo Neira459aa662016-05-09 00:55:48 +0200335 break;
336 default:
337 ret = -1; /* Shouldn't happen. */
338 }
339
340 switch (ret) {
341 case 1:
342 netdev_dbg(gtp->dev, "pass up to the process\n");
Andreas Schultz5b171f92017-03-09 17:42:59 +0100343 break;
Pablo Neira459aa662016-05-09 00:55:48 +0200344 case 0:
Pablo Neira459aa662016-05-09 00:55:48 +0200345 break;
346 case -1:
347 netdev_dbg(gtp->dev, "GTP packet has been dropped\n");
348 kfree_skb(skb);
Andreas Schultz5b171f92017-03-09 17:42:59 +0100349 ret = 0;
350 break;
Pablo Neira459aa662016-05-09 00:55:48 +0200351 }
352
Andreas Schultz5b171f92017-03-09 17:42:59 +0100353 return ret;
Pablo Neira459aa662016-05-09 00:55:48 +0200354}
355
356static int gtp_dev_init(struct net_device *dev)
357{
358 struct gtp_dev *gtp = netdev_priv(dev);
359
360 gtp->dev = dev;
361
362 dev->tstats = alloc_percpu(struct pcpu_sw_netstats);
363 if (!dev->tstats)
364 return -ENOMEM;
365
366 return 0;
367}
368
369static void gtp_dev_uninit(struct net_device *dev)
370{
371 struct gtp_dev *gtp = netdev_priv(dev);
372
373 gtp_encap_disable(gtp);
374 free_percpu(dev->tstats);
375}
376
Andreas Schultz101cfbc2017-03-09 17:43:02 +0100377static struct rtable *ip4_route_output_gtp(struct flowi4 *fl4,
378 const struct sock *sk,
379 __be32 daddr)
Pablo Neira459aa662016-05-09 00:55:48 +0200380{
381 memset(fl4, 0, sizeof(*fl4));
382 fl4->flowi4_oif = sk->sk_bound_dev_if;
383 fl4->daddr = daddr;
384 fl4->saddr = inet_sk(sk)->inet_saddr;
385 fl4->flowi4_tos = RT_CONN_FLAGS(sk);
386 fl4->flowi4_proto = sk->sk_protocol;
387
Andreas Schultz101cfbc2017-03-09 17:43:02 +0100388 return ip_route_output_key(sock_net(sk), fl4);
Pablo Neira459aa662016-05-09 00:55:48 +0200389}
390
391static inline void gtp0_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
392{
393 int payload_len = skb->len;
394 struct gtp0_header *gtp0;
395
396 gtp0 = (struct gtp0_header *) skb_push(skb, sizeof(*gtp0));
397
398 gtp0->flags = 0x1e; /* v0, GTP-non-prime. */
399 gtp0->type = GTP_TPDU;
400 gtp0->length = htons(payload_len);
401 gtp0->seq = htons((atomic_inc_return(&pctx->tx_seq) - 1) % 0xffff);
402 gtp0->flow = htons(pctx->u.v0.flow);
403 gtp0->number = 0xff;
404 gtp0->spare[0] = gtp0->spare[1] = gtp0->spare[2] = 0xff;
405 gtp0->tid = cpu_to_be64(pctx->u.v0.tid);
406}
407
408static inline void gtp1_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
409{
410 int payload_len = skb->len;
411 struct gtp1_header *gtp1;
412
413 gtp1 = (struct gtp1_header *) skb_push(skb, sizeof(*gtp1));
414
415 /* Bits 8 7 6 5 4 3 2 1
416 * +--+--+--+--+--+--+--+--+
Harald Welted928be82016-12-15 22:35:53 +0100417 * |version |PT| 0| E| S|PN|
Pablo Neira459aa662016-05-09 00:55:48 +0200418 * +--+--+--+--+--+--+--+--+
419 * 0 0 1 1 1 0 0 0
420 */
Harald Welted928be82016-12-15 22:35:53 +0100421 gtp1->flags = 0x30; /* v1, GTP-non-prime. */
Pablo Neira459aa662016-05-09 00:55:48 +0200422 gtp1->type = GTP_TPDU;
423 gtp1->length = htons(payload_len);
424 gtp1->tid = htonl(pctx->u.v1.o_tei);
425
426 /* TODO: Suppport for extension header, sequence number and N-PDU.
427 * Update the length field if any of them is available.
428 */
429}
430
431struct gtp_pktinfo {
432 struct sock *sk;
433 struct iphdr *iph;
434 struct flowi4 fl4;
435 struct rtable *rt;
436 struct pdp_ctx *pctx;
437 struct net_device *dev;
438 __be16 gtph_port;
439};
440
441static void gtp_push_header(struct sk_buff *skb, struct gtp_pktinfo *pktinfo)
442{
443 switch (pktinfo->pctx->gtp_version) {
444 case GTP_V0:
445 pktinfo->gtph_port = htons(GTP0_PORT);
446 gtp0_push_header(skb, pktinfo->pctx);
447 break;
448 case GTP_V1:
449 pktinfo->gtph_port = htons(GTP1U_PORT);
450 gtp1_push_header(skb, pktinfo->pctx);
451 break;
452 }
453}
454
455static inline void gtp_set_pktinfo_ipv4(struct gtp_pktinfo *pktinfo,
456 struct sock *sk, struct iphdr *iph,
457 struct pdp_ctx *pctx, struct rtable *rt,
458 struct flowi4 *fl4,
459 struct net_device *dev)
460{
461 pktinfo->sk = sk;
462 pktinfo->iph = iph;
463 pktinfo->pctx = pctx;
464 pktinfo->rt = rt;
465 pktinfo->fl4 = *fl4;
466 pktinfo->dev = dev;
467}
468
469static int gtp_build_skb_ip4(struct sk_buff *skb, struct net_device *dev,
470 struct gtp_pktinfo *pktinfo)
471{
472 struct gtp_dev *gtp = netdev_priv(dev);
473 struct pdp_ctx *pctx;
474 struct rtable *rt;
475 struct flowi4 fl4;
476 struct iphdr *iph;
Pablo Neira459aa662016-05-09 00:55:48 +0200477 __be16 df;
478 int mtu;
479
480 /* Read the IP destination address and resolve the PDP context.
481 * Prepend PDP header with TEI/TID from PDP ctx.
482 */
483 iph = ip_hdr(skb);
484 pctx = ipv4_pdp_find(gtp, iph->daddr);
485 if (!pctx) {
486 netdev_dbg(dev, "no PDP ctx found for %pI4, skip\n",
487 &iph->daddr);
488 return -ENOENT;
489 }
490 netdev_dbg(dev, "found PDP context %p\n", pctx);
491
Andreas Schultz101cfbc2017-03-09 17:43:02 +0100492 rt = ip4_route_output_gtp(&fl4, pctx->sk, pctx->sgsn_addr_ip4.s_addr);
Pablo Neira459aa662016-05-09 00:55:48 +0200493 if (IS_ERR(rt)) {
494 netdev_dbg(dev, "no route to SSGN %pI4\n",
495 &pctx->sgsn_addr_ip4.s_addr);
496 dev->stats.tx_carrier_errors++;
497 goto err;
498 }
499
500 if (rt->dst.dev == dev) {
501 netdev_dbg(dev, "circular route to SSGN %pI4\n",
502 &pctx->sgsn_addr_ip4.s_addr);
503 dev->stats.collisions++;
504 goto err_rt;
505 }
506
507 skb_dst_drop(skb);
508
509 /* This is similar to tnl_update_pmtu(). */
510 df = iph->frag_off;
511 if (df) {
512 mtu = dst_mtu(&rt->dst) - dev->hard_header_len -
513 sizeof(struct iphdr) - sizeof(struct udphdr);
514 switch (pctx->gtp_version) {
515 case GTP_V0:
516 mtu -= sizeof(struct gtp0_header);
517 break;
518 case GTP_V1:
519 mtu -= sizeof(struct gtp1_header);
520 break;
521 }
522 } else {
523 mtu = dst_mtu(&rt->dst);
524 }
525
526 rt->dst.ops->update_pmtu(&rt->dst, NULL, skb, mtu);
527
528 if (!skb_is_gso(skb) && (iph->frag_off & htons(IP_DF)) &&
529 mtu < ntohs(iph->tot_len)) {
530 netdev_dbg(dev, "packet too big, fragmentation needed\n");
531 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
532 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
533 htonl(mtu));
534 goto err_rt;
535 }
536
Andreas Schultz101cfbc2017-03-09 17:43:02 +0100537 gtp_set_pktinfo_ipv4(pktinfo, pctx->sk, iph, pctx, rt, &fl4, dev);
Pablo Neira459aa662016-05-09 00:55:48 +0200538 gtp_push_header(skb, pktinfo);
539
540 return 0;
541err_rt:
542 ip_rt_put(rt);
543err:
544 return -EBADMSG;
545}
546
547static netdev_tx_t gtp_dev_xmit(struct sk_buff *skb, struct net_device *dev)
548{
549 unsigned int proto = ntohs(skb->protocol);
550 struct gtp_pktinfo pktinfo;
551 int err;
552
553 /* Ensure there is sufficient headroom. */
554 if (skb_cow_head(skb, dev->needed_headroom))
555 goto tx_err;
556
557 skb_reset_inner_headers(skb);
558
559 /* PDP context lookups in gtp_build_skb_*() need rcu read-side lock. */
560 rcu_read_lock();
561 switch (proto) {
562 case ETH_P_IP:
563 err = gtp_build_skb_ip4(skb, dev, &pktinfo);
564 break;
565 default:
566 err = -EOPNOTSUPP;
567 break;
568 }
569 rcu_read_unlock();
570
571 if (err < 0)
572 goto tx_err;
573
574 switch (proto) {
575 case ETH_P_IP:
576 netdev_dbg(pktinfo.dev, "gtp -> IP src: %pI4 dst: %pI4\n",
577 &pktinfo.iph->saddr, &pktinfo.iph->daddr);
578 udp_tunnel_xmit_skb(pktinfo.rt, pktinfo.sk, skb,
579 pktinfo.fl4.saddr, pktinfo.fl4.daddr,
580 pktinfo.iph->tos,
581 ip4_dst_hoplimit(&pktinfo.rt->dst),
Andreas Schultzc6ce1d02017-01-27 10:40:57 +0100582 0,
Pablo Neira459aa662016-05-09 00:55:48 +0200583 pktinfo.gtph_port, pktinfo.gtph_port,
584 true, false);
585 break;
586 }
587
588 return NETDEV_TX_OK;
589tx_err:
590 dev->stats.tx_errors++;
591 dev_kfree_skb(skb);
592 return NETDEV_TX_OK;
593}
594
595static const struct net_device_ops gtp_netdev_ops = {
596 .ndo_init = gtp_dev_init,
597 .ndo_uninit = gtp_dev_uninit,
598 .ndo_start_xmit = gtp_dev_xmit,
599 .ndo_get_stats64 = ip_tunnel_get_stats64,
600};
601
602static void gtp_link_setup(struct net_device *dev)
603{
604 dev->netdev_ops = &gtp_netdev_ops;
605 dev->destructor = free_netdev;
606
607 dev->hard_header_len = 0;
608 dev->addr_len = 0;
609
610 /* Zero header length. */
611 dev->type = ARPHRD_NONE;
612 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
613
614 dev->priv_flags |= IFF_NO_QUEUE;
615 dev->features |= NETIF_F_LLTX;
616 netif_keep_dst(dev);
617
618 /* Assume largest header, ie. GTPv0. */
619 dev->needed_headroom = LL_MAX_HEADER +
620 sizeof(struct iphdr) +
621 sizeof(struct udphdr) +
622 sizeof(struct gtp0_header);
623}
624
625static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize);
626static void gtp_hashtable_free(struct gtp_dev *gtp);
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100627static int gtp_encap_enable(struct gtp_dev *gtp, struct nlattr *data[]);
Pablo Neira459aa662016-05-09 00:55:48 +0200628
629static int gtp_newlink(struct net *src_net, struct net_device *dev,
630 struct nlattr *tb[], struct nlattr *data[])
631{
Pablo Neira459aa662016-05-09 00:55:48 +0200632 struct gtp_dev *gtp;
633 struct gtp_net *gn;
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100634 int hashsize, err;
Pablo Neira459aa662016-05-09 00:55:48 +0200635
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100636 if (!data[IFLA_GTP_FD0] && !data[IFLA_GTP_FD1])
Pablo Neira459aa662016-05-09 00:55:48 +0200637 return -EINVAL;
638
639 gtp = netdev_priv(dev);
640
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100641 err = gtp_encap_enable(gtp, data);
Pablo Neira459aa662016-05-09 00:55:48 +0200642 if (err < 0)
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100643 return err;
Pablo Neira459aa662016-05-09 00:55:48 +0200644
645 if (!data[IFLA_GTP_PDP_HASHSIZE])
646 hashsize = 1024;
647 else
648 hashsize = nla_get_u32(data[IFLA_GTP_PDP_HASHSIZE]);
649
650 err = gtp_hashtable_new(gtp, hashsize);
651 if (err < 0)
652 goto out_encap;
653
654 err = register_netdevice(dev);
655 if (err < 0) {
656 netdev_dbg(dev, "failed to register new netdev %d\n", err);
657 goto out_hashtable;
658 }
659
660 gn = net_generic(dev_net(dev), gtp_net_id);
661 list_add_rcu(&gtp->list, &gn->gtp_dev_list);
662
663 netdev_dbg(dev, "registered new GTP interface\n");
664
665 return 0;
666
667out_hashtable:
668 gtp_hashtable_free(gtp);
669out_encap:
670 gtp_encap_disable(gtp);
Pablo Neira459aa662016-05-09 00:55:48 +0200671 return err;
672}
673
674static void gtp_dellink(struct net_device *dev, struct list_head *head)
675{
676 struct gtp_dev *gtp = netdev_priv(dev);
677
678 gtp_encap_disable(gtp);
679 gtp_hashtable_free(gtp);
680 list_del_rcu(&gtp->list);
681 unregister_netdevice_queue(dev, head);
682}
683
684static const struct nla_policy gtp_policy[IFLA_GTP_MAX + 1] = {
685 [IFLA_GTP_FD0] = { .type = NLA_U32 },
686 [IFLA_GTP_FD1] = { .type = NLA_U32 },
687 [IFLA_GTP_PDP_HASHSIZE] = { .type = NLA_U32 },
688};
689
690static int gtp_validate(struct nlattr *tb[], struct nlattr *data[])
691{
692 if (!data)
693 return -EINVAL;
694
695 return 0;
696}
697
698static size_t gtp_get_size(const struct net_device *dev)
699{
700 return nla_total_size(sizeof(__u32)); /* IFLA_GTP_PDP_HASHSIZE */
701}
702
703static int gtp_fill_info(struct sk_buff *skb, const struct net_device *dev)
704{
705 struct gtp_dev *gtp = netdev_priv(dev);
706
707 if (nla_put_u32(skb, IFLA_GTP_PDP_HASHSIZE, gtp->hash_size))
708 goto nla_put_failure;
709
710 return 0;
711
712nla_put_failure:
713 return -EMSGSIZE;
714}
715
716static struct rtnl_link_ops gtp_link_ops __read_mostly = {
717 .kind = "gtp",
718 .maxtype = IFLA_GTP_MAX,
719 .policy = gtp_policy,
720 .priv_size = sizeof(struct gtp_dev),
721 .setup = gtp_link_setup,
722 .validate = gtp_validate,
723 .newlink = gtp_newlink,
724 .dellink = gtp_dellink,
725 .get_size = gtp_get_size,
726 .fill_info = gtp_fill_info,
727};
728
Pablo Neira459aa662016-05-09 00:55:48 +0200729static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize)
730{
731 int i;
732
733 gtp->addr_hash = kmalloc(sizeof(struct hlist_head) * hsize, GFP_KERNEL);
734 if (gtp->addr_hash == NULL)
735 return -ENOMEM;
736
737 gtp->tid_hash = kmalloc(sizeof(struct hlist_head) * hsize, GFP_KERNEL);
738 if (gtp->tid_hash == NULL)
739 goto err1;
740
741 gtp->hash_size = hsize;
742
743 for (i = 0; i < hsize; i++) {
744 INIT_HLIST_HEAD(&gtp->addr_hash[i]);
745 INIT_HLIST_HEAD(&gtp->tid_hash[i]);
746 }
747 return 0;
748err1:
749 kfree(gtp->addr_hash);
750 return -ENOMEM;
751}
752
753static void gtp_hashtable_free(struct gtp_dev *gtp)
754{
755 struct pdp_ctx *pctx;
756 int i;
757
Andreas Schultz6b5e2e72017-03-09 17:43:01 +0100758 for (i = 0; i < gtp->hash_size; i++)
759 hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid)
760 pdp_context_delete(pctx);
761
Pablo Neira459aa662016-05-09 00:55:48 +0200762 synchronize_rcu();
763 kfree(gtp->addr_hash);
764 kfree(gtp->tid_hash);
765}
766
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100767static struct sock *gtp_encap_enable_socket(int fd, int type,
768 struct gtp_dev *gtp)
Pablo Neira459aa662016-05-09 00:55:48 +0200769{
770 struct udp_tunnel_sock_cfg tuncfg = {NULL};
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100771 struct socket *sock;
772 struct sock *sk;
Pablo Neira459aa662016-05-09 00:55:48 +0200773 int err;
774
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100775 pr_debug("enable gtp on %d, %d\n", fd, type);
Pablo Neira459aa662016-05-09 00:55:48 +0200776
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100777 sock = sockfd_lookup(fd, &err);
778 if (!sock) {
779 pr_debug("gtp socket fd=%d not found\n", fd);
780 return NULL;
Pablo Neira459aa662016-05-09 00:55:48 +0200781 }
782
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100783 if (sock->sk->sk_protocol != IPPROTO_UDP) {
784 pr_debug("socket fd=%d not UDP\n", fd);
785 sk = ERR_PTR(-EINVAL);
786 goto out_sock;
Pablo Neira459aa662016-05-09 00:55:48 +0200787 }
788
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100789 if (rcu_dereference_sk_user_data(sock->sk)) {
790 sk = ERR_PTR(-EBUSY);
791 goto out_sock;
Pablo Neira459aa662016-05-09 00:55:48 +0200792 }
793
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100794 sk = sock->sk;
795 sock_hold(sk);
Pablo Neira459aa662016-05-09 00:55:48 +0200796
797 tuncfg.sk_user_data = gtp;
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100798 tuncfg.encap_type = type;
Pablo Neira459aa662016-05-09 00:55:48 +0200799 tuncfg.encap_rcv = gtp_encap_recv;
800 tuncfg.encap_destroy = gtp_encap_destroy;
801
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100802 setup_udp_tunnel_sock(sock_net(sock->sk), sock, &tuncfg);
Pablo Neira459aa662016-05-09 00:55:48 +0200803
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100804out_sock:
805 sockfd_put(sock);
806 return sk;
807}
Pablo Neira459aa662016-05-09 00:55:48 +0200808
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100809static int gtp_encap_enable(struct gtp_dev *gtp, struct nlattr *data[])
810{
811 struct sock *sk1u = NULL;
812 struct sock *sk0 = NULL;
813
814 if (data[IFLA_GTP_FD0]) {
815 u32 fd0 = nla_get_u32(data[IFLA_GTP_FD0]);
816
817 sk0 = gtp_encap_enable_socket(fd0, UDP_ENCAP_GTP0, gtp);
818 if (IS_ERR(sk0))
819 return PTR_ERR(sk0);
820 }
821
822 if (data[IFLA_GTP_FD1]) {
823 u32 fd1 = nla_get_u32(data[IFLA_GTP_FD1]);
824
825 sk1u = gtp_encap_enable_socket(fd1, UDP_ENCAP_GTP1U, gtp);
826 if (IS_ERR(sk1u)) {
827 if (sk0)
828 gtp_encap_disable_sock(sk0);
829 return PTR_ERR(sk1u);
830 }
831 }
832
833 gtp->sk0 = sk0;
834 gtp->sk1u = sk1u;
835
836 return 0;
Pablo Neira459aa662016-05-09 00:55:48 +0200837}
838
Andreas Schultz3fb94612017-03-09 17:42:58 +0100839static struct gtp_dev *gtp_find_dev(struct net *src_net, struct nlattr *nla[])
Pablo Neira459aa662016-05-09 00:55:48 +0200840{
Andreas Schultz3fb94612017-03-09 17:42:58 +0100841 struct gtp_dev *gtp = NULL;
842 struct net_device *dev;
843 struct net *net;
Pablo Neira459aa662016-05-09 00:55:48 +0200844
Andreas Schultz3fb94612017-03-09 17:42:58 +0100845 /* Examine the link attributes and figure out which network namespace
846 * we are talking about.
847 */
848 if (nla[GTPA_NET_NS_FD])
849 net = get_net_ns_by_fd(nla_get_u32(nla[GTPA_NET_NS_FD]));
850 else
851 net = get_net(src_net);
852
853 if (IS_ERR(net))
854 return NULL;
855
856 /* Check if there's an existing gtpX device to configure */
857 dev = dev_get_by_index_rcu(net, nla_get_u32(nla[GTPA_LINK]));
858 if (dev->netdev_ops == &gtp_netdev_ops)
859 gtp = netdev_priv(dev);
860
861 put_net(net);
862 return gtp;
Pablo Neira459aa662016-05-09 00:55:48 +0200863}
864
865static void ipv4_pdp_fill(struct pdp_ctx *pctx, struct genl_info *info)
866{
867 pctx->gtp_version = nla_get_u32(info->attrs[GTPA_VERSION]);
868 pctx->af = AF_INET;
869 pctx->sgsn_addr_ip4.s_addr =
870 nla_get_be32(info->attrs[GTPA_SGSN_ADDRESS]);
871 pctx->ms_addr_ip4.s_addr =
872 nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
873
874 switch (pctx->gtp_version) {
875 case GTP_V0:
876 /* According to TS 09.60, sections 7.5.1 and 7.5.2, the flow
877 * label needs to be the same for uplink and downlink packets,
878 * so let's annotate this.
879 */
880 pctx->u.v0.tid = nla_get_u64(info->attrs[GTPA_TID]);
881 pctx->u.v0.flow = nla_get_u16(info->attrs[GTPA_FLOW]);
882 break;
883 case GTP_V1:
884 pctx->u.v1.i_tei = nla_get_u32(info->attrs[GTPA_I_TEI]);
885 pctx->u.v1.o_tei = nla_get_u32(info->attrs[GTPA_O_TEI]);
886 break;
887 default:
888 break;
889 }
890}
891
Andreas Schultz101cfbc2017-03-09 17:43:02 +0100892static int ipv4_pdp_add(struct gtp_dev *gtp, struct sock *sk,
893 struct genl_info *info)
Pablo Neira459aa662016-05-09 00:55:48 +0200894{
Andreas Schultz3fb94612017-03-09 17:42:58 +0100895 struct net_device *dev = gtp->dev;
Pablo Neira459aa662016-05-09 00:55:48 +0200896 u32 hash_ms, hash_tid = 0;
897 struct pdp_ctx *pctx;
898 bool found = false;
899 __be32 ms_addr;
900
901 ms_addr = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
902 hash_ms = ipv4_hashfn(ms_addr) % gtp->hash_size;
903
904 hlist_for_each_entry_rcu(pctx, &gtp->addr_hash[hash_ms], hlist_addr) {
905 if (pctx->ms_addr_ip4.s_addr == ms_addr) {
906 found = true;
907 break;
908 }
909 }
910
911 if (found) {
912 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
913 return -EEXIST;
914 if (info->nlhdr->nlmsg_flags & NLM_F_REPLACE)
915 return -EOPNOTSUPP;
916
917 ipv4_pdp_fill(pctx, info);
918
919 if (pctx->gtp_version == GTP_V0)
920 netdev_dbg(dev, "GTPv0-U: update tunnel id = %llx (pdp %p)\n",
921 pctx->u.v0.tid, pctx);
922 else if (pctx->gtp_version == GTP_V1)
923 netdev_dbg(dev, "GTPv1-U: update tunnel id = %x/%x (pdp %p)\n",
924 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
925
926 return 0;
927
928 }
929
930 pctx = kmalloc(sizeof(struct pdp_ctx), GFP_KERNEL);
931 if (pctx == NULL)
932 return -ENOMEM;
933
Andreas Schultz101cfbc2017-03-09 17:43:02 +0100934 sock_hold(sk);
935 pctx->sk = sk;
Andreas Schultz5b171f92017-03-09 17:42:59 +0100936 pctx->dev = gtp->dev;
Pablo Neira459aa662016-05-09 00:55:48 +0200937 ipv4_pdp_fill(pctx, info);
938 atomic_set(&pctx->tx_seq, 0);
939
940 switch (pctx->gtp_version) {
941 case GTP_V0:
942 /* TS 09.60: "The flow label identifies unambiguously a GTP
943 * flow.". We use the tid for this instead, I cannot find a
944 * situation in which this doesn't unambiguosly identify the
945 * PDP context.
946 */
947 hash_tid = gtp0_hashfn(pctx->u.v0.tid) % gtp->hash_size;
948 break;
949 case GTP_V1:
950 hash_tid = gtp1u_hashfn(pctx->u.v1.i_tei) % gtp->hash_size;
951 break;
952 }
953
954 hlist_add_head_rcu(&pctx->hlist_addr, &gtp->addr_hash[hash_ms]);
955 hlist_add_head_rcu(&pctx->hlist_tid, &gtp->tid_hash[hash_tid]);
956
957 switch (pctx->gtp_version) {
958 case GTP_V0:
959 netdev_dbg(dev, "GTPv0-U: new PDP ctx id=%llx ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
960 pctx->u.v0.tid, &pctx->sgsn_addr_ip4,
961 &pctx->ms_addr_ip4, pctx);
962 break;
963 case GTP_V1:
964 netdev_dbg(dev, "GTPv1-U: new PDP ctx id=%x/%x ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
965 pctx->u.v1.i_tei, pctx->u.v1.o_tei,
966 &pctx->sgsn_addr_ip4, &pctx->ms_addr_ip4, pctx);
967 break;
968 }
969
970 return 0;
971}
972
Andreas Schultz101cfbc2017-03-09 17:43:02 +0100973static void pdp_context_free(struct rcu_head *head)
974{
975 struct pdp_ctx *pctx = container_of(head, struct pdp_ctx, rcu_head);
976
977 sock_put(pctx->sk);
978 kfree(pctx);
979}
980
Andreas Schultz6b5e2e72017-03-09 17:43:01 +0100981static void pdp_context_delete(struct pdp_ctx *pctx)
982{
983 hlist_del_rcu(&pctx->hlist_tid);
984 hlist_del_rcu(&pctx->hlist_addr);
Andreas Schultz101cfbc2017-03-09 17:43:02 +0100985 call_rcu(&pctx->rcu_head, pdp_context_free);
Andreas Schultz6b5e2e72017-03-09 17:43:01 +0100986}
987
Pablo Neira459aa662016-05-09 00:55:48 +0200988static int gtp_genl_new_pdp(struct sk_buff *skb, struct genl_info *info)
989{
Andreas Schultz101cfbc2017-03-09 17:43:02 +0100990 unsigned int version;
Andreas Schultz3fb94612017-03-09 17:42:58 +0100991 struct gtp_dev *gtp;
Andreas Schultz101cfbc2017-03-09 17:43:02 +0100992 struct sock *sk;
Andreas Schultz3fb94612017-03-09 17:42:58 +0100993 int err;
Pablo Neira459aa662016-05-09 00:55:48 +0200994
995 if (!info->attrs[GTPA_VERSION] ||
996 !info->attrs[GTPA_LINK] ||
997 !info->attrs[GTPA_SGSN_ADDRESS] ||
998 !info->attrs[GTPA_MS_ADDRESS])
999 return -EINVAL;
1000
Andreas Schultz101cfbc2017-03-09 17:43:02 +01001001 version = nla_get_u32(info->attrs[GTPA_VERSION]);
1002
1003 switch (version) {
Pablo Neira459aa662016-05-09 00:55:48 +02001004 case GTP_V0:
1005 if (!info->attrs[GTPA_TID] ||
1006 !info->attrs[GTPA_FLOW])
1007 return -EINVAL;
1008 break;
1009 case GTP_V1:
1010 if (!info->attrs[GTPA_I_TEI] ||
1011 !info->attrs[GTPA_O_TEI])
1012 return -EINVAL;
1013 break;
1014
1015 default:
1016 return -EINVAL;
1017 }
1018
Andreas Schultz3fb94612017-03-09 17:42:58 +01001019 rcu_read_lock();
Pablo Neira459aa662016-05-09 00:55:48 +02001020
Andreas Schultz3fb94612017-03-09 17:42:58 +01001021 gtp = gtp_find_dev(sock_net(skb->sk), info->attrs);
1022 if (!gtp) {
1023 err = -ENODEV;
1024 goto out_unlock;
Pablo Neira27ee4412016-05-12 17:16:31 +02001025 }
Pablo Neira459aa662016-05-09 00:55:48 +02001026
Andreas Schultz101cfbc2017-03-09 17:43:02 +01001027 if (version == GTP_V0)
1028 sk = gtp->sk0;
1029 else if (version == GTP_V1)
1030 sk = gtp->sk1u;
1031 else
1032 sk = NULL;
1033
1034 if (!sk) {
1035 err = -ENODEV;
1036 goto out_unlock;
1037 }
1038
1039 err = ipv4_pdp_add(gtp, sk, info);
Andreas Schultz3fb94612017-03-09 17:42:58 +01001040
1041out_unlock:
1042 rcu_read_unlock();
1043 return err;
Pablo Neira459aa662016-05-09 00:55:48 +02001044}
1045
Andreas Schultzd9e2dd12017-03-09 17:43:00 +01001046static struct pdp_ctx *gtp_find_pdp_by_link(struct net *net,
1047 struct nlattr *nla[])
1048{
1049 struct gtp_dev *gtp;
1050
1051 gtp = gtp_find_dev(net, nla);
1052 if (!gtp)
1053 return ERR_PTR(-ENODEV);
1054
1055 if (nla[GTPA_MS_ADDRESS]) {
1056 __be32 ip = nla_get_be32(nla[GTPA_MS_ADDRESS]);
1057
1058 return ipv4_pdp_find(gtp, ip);
1059 } else if (nla[GTPA_VERSION]) {
1060 u32 gtp_version = nla_get_u32(nla[GTPA_VERSION]);
1061
1062 if (gtp_version == GTP_V0 && nla[GTPA_TID])
1063 return gtp0_pdp_find(gtp, nla_get_u64(nla[GTPA_TID]));
1064 else if (gtp_version == GTP_V1 && nla[GTPA_I_TEI])
1065 return gtp1_pdp_find(gtp, nla_get_u32(nla[GTPA_I_TEI]));
1066 }
1067
1068 return ERR_PTR(-EINVAL);
1069}
1070
1071static struct pdp_ctx *gtp_find_pdp(struct net *net, struct nlattr *nla[])
1072{
1073 struct pdp_ctx *pctx;
1074
1075 if (nla[GTPA_LINK])
1076 pctx = gtp_find_pdp_by_link(net, nla);
1077 else
1078 pctx = ERR_PTR(-EINVAL);
1079
1080 if (!pctx)
1081 pctx = ERR_PTR(-ENOENT);
1082
1083 return pctx;
1084}
1085
Pablo Neira459aa662016-05-09 00:55:48 +02001086static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info)
1087{
Pablo Neira459aa662016-05-09 00:55:48 +02001088 struct pdp_ctx *pctx;
Andreas Schultz3fb94612017-03-09 17:42:58 +01001089 int err = 0;
Pablo Neira459aa662016-05-09 00:55:48 +02001090
Andreas Schultzd9e2dd12017-03-09 17:43:00 +01001091 if (!info->attrs[GTPA_VERSION])
Pablo Neira459aa662016-05-09 00:55:48 +02001092 return -EINVAL;
1093
Andreas Schultz3fb94612017-03-09 17:42:58 +01001094 rcu_read_lock();
Pablo Neira459aa662016-05-09 00:55:48 +02001095
Andreas Schultzd9e2dd12017-03-09 17:43:00 +01001096 pctx = gtp_find_pdp(sock_net(skb->sk), info->attrs);
1097 if (IS_ERR(pctx)) {
1098 err = PTR_ERR(pctx);
Andreas Schultz3fb94612017-03-09 17:42:58 +01001099 goto out_unlock;
1100 }
Pablo Neira459aa662016-05-09 00:55:48 +02001101
1102 if (pctx->gtp_version == GTP_V0)
Andreas Schultzd9e2dd12017-03-09 17:43:00 +01001103 netdev_dbg(pctx->dev, "GTPv0-U: deleting tunnel id = %llx (pdp %p)\n",
Pablo Neira459aa662016-05-09 00:55:48 +02001104 pctx->u.v0.tid, pctx);
1105 else if (pctx->gtp_version == GTP_V1)
Andreas Schultzd9e2dd12017-03-09 17:43:00 +01001106 netdev_dbg(pctx->dev, "GTPv1-U: deleting tunnel id = %x/%x (pdp %p)\n",
Pablo Neira459aa662016-05-09 00:55:48 +02001107 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
1108
Andreas Schultz6b5e2e72017-03-09 17:43:01 +01001109 pdp_context_delete(pctx);
Pablo Neira459aa662016-05-09 00:55:48 +02001110
Andreas Schultz3fb94612017-03-09 17:42:58 +01001111out_unlock:
1112 rcu_read_unlock();
1113 return err;
Pablo Neira459aa662016-05-09 00:55:48 +02001114}
1115
Johannes Berg489111e2016-10-24 14:40:03 +02001116static struct genl_family gtp_genl_family;
Pablo Neira459aa662016-05-09 00:55:48 +02001117
1118static int gtp_genl_fill_info(struct sk_buff *skb, u32 snd_portid, u32 snd_seq,
1119 u32 type, struct pdp_ctx *pctx)
1120{
1121 void *genlh;
1122
1123 genlh = genlmsg_put(skb, snd_portid, snd_seq, &gtp_genl_family, 0,
1124 type);
1125 if (genlh == NULL)
1126 goto nlmsg_failure;
1127
1128 if (nla_put_u32(skb, GTPA_VERSION, pctx->gtp_version) ||
1129 nla_put_be32(skb, GTPA_SGSN_ADDRESS, pctx->sgsn_addr_ip4.s_addr) ||
1130 nla_put_be32(skb, GTPA_MS_ADDRESS, pctx->ms_addr_ip4.s_addr))
1131 goto nla_put_failure;
1132
1133 switch (pctx->gtp_version) {
1134 case GTP_V0:
1135 if (nla_put_u64_64bit(skb, GTPA_TID, pctx->u.v0.tid, GTPA_PAD) ||
1136 nla_put_u16(skb, GTPA_FLOW, pctx->u.v0.flow))
1137 goto nla_put_failure;
1138 break;
1139 case GTP_V1:
1140 if (nla_put_u32(skb, GTPA_I_TEI, pctx->u.v1.i_tei) ||
1141 nla_put_u32(skb, GTPA_O_TEI, pctx->u.v1.o_tei))
1142 goto nla_put_failure;
1143 break;
1144 }
1145 genlmsg_end(skb, genlh);
1146 return 0;
1147
1148nlmsg_failure:
1149nla_put_failure:
1150 genlmsg_cancel(skb, genlh);
1151 return -EMSGSIZE;
1152}
1153
1154static int gtp_genl_get_pdp(struct sk_buff *skb, struct genl_info *info)
1155{
1156 struct pdp_ctx *pctx = NULL;
Pablo Neira459aa662016-05-09 00:55:48 +02001157 struct sk_buff *skb2;
Pablo Neira459aa662016-05-09 00:55:48 +02001158 int err;
1159
Andreas Schultzd9e2dd12017-03-09 17:43:00 +01001160 if (!info->attrs[GTPA_VERSION])
Pablo Neira459aa662016-05-09 00:55:48 +02001161 return -EINVAL;
1162
Pablo Neira459aa662016-05-09 00:55:48 +02001163 rcu_read_lock();
Andreas Schultz3fb94612017-03-09 17:42:58 +01001164
Andreas Schultzd9e2dd12017-03-09 17:43:00 +01001165 pctx = gtp_find_pdp(sock_net(skb->sk), info->attrs);
1166 if (IS_ERR(pctx)) {
1167 err = PTR_ERR(pctx);
Pablo Neira459aa662016-05-09 00:55:48 +02001168 goto err_unlock;
1169 }
1170
1171 skb2 = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
1172 if (skb2 == NULL) {
1173 err = -ENOMEM;
1174 goto err_unlock;
1175 }
1176
1177 err = gtp_genl_fill_info(skb2, NETLINK_CB(skb).portid,
1178 info->snd_seq, info->nlhdr->nlmsg_type, pctx);
1179 if (err < 0)
1180 goto err_unlock_free;
1181
1182 rcu_read_unlock();
1183 return genlmsg_unicast(genl_info_net(info), skb2, info->snd_portid);
1184
1185err_unlock_free:
1186 kfree_skb(skb2);
1187err_unlock:
1188 rcu_read_unlock();
1189 return err;
1190}
1191
1192static int gtp_genl_dump_pdp(struct sk_buff *skb,
1193 struct netlink_callback *cb)
1194{
1195 struct gtp_dev *last_gtp = (struct gtp_dev *)cb->args[2], *gtp;
1196 struct net *net = sock_net(skb->sk);
1197 struct gtp_net *gn = net_generic(net, gtp_net_id);
1198 unsigned long tid = cb->args[1];
1199 int i, k = cb->args[0], ret;
1200 struct pdp_ctx *pctx;
1201
1202 if (cb->args[4])
1203 return 0;
1204
1205 list_for_each_entry_rcu(gtp, &gn->gtp_dev_list, list) {
1206 if (last_gtp && last_gtp != gtp)
1207 continue;
1208 else
1209 last_gtp = NULL;
1210
1211 for (i = k; i < gtp->hash_size; i++) {
1212 hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid) {
1213 if (tid && tid != pctx->u.tid)
1214 continue;
1215 else
1216 tid = 0;
1217
1218 ret = gtp_genl_fill_info(skb,
1219 NETLINK_CB(cb->skb).portid,
1220 cb->nlh->nlmsg_seq,
1221 cb->nlh->nlmsg_type, pctx);
1222 if (ret < 0) {
1223 cb->args[0] = i;
1224 cb->args[1] = pctx->u.tid;
1225 cb->args[2] = (unsigned long)gtp;
1226 goto out;
1227 }
1228 }
1229 }
1230 }
1231 cb->args[4] = 1;
1232out:
1233 return skb->len;
1234}
1235
1236static struct nla_policy gtp_genl_policy[GTPA_MAX + 1] = {
1237 [GTPA_LINK] = { .type = NLA_U32, },
1238 [GTPA_VERSION] = { .type = NLA_U32, },
1239 [GTPA_TID] = { .type = NLA_U64, },
1240 [GTPA_SGSN_ADDRESS] = { .type = NLA_U32, },
1241 [GTPA_MS_ADDRESS] = { .type = NLA_U32, },
1242 [GTPA_FLOW] = { .type = NLA_U16, },
1243 [GTPA_NET_NS_FD] = { .type = NLA_U32, },
1244 [GTPA_I_TEI] = { .type = NLA_U32, },
1245 [GTPA_O_TEI] = { .type = NLA_U32, },
1246};
1247
1248static const struct genl_ops gtp_genl_ops[] = {
1249 {
1250 .cmd = GTP_CMD_NEWPDP,
1251 .doit = gtp_genl_new_pdp,
1252 .policy = gtp_genl_policy,
1253 .flags = GENL_ADMIN_PERM,
1254 },
1255 {
1256 .cmd = GTP_CMD_DELPDP,
1257 .doit = gtp_genl_del_pdp,
1258 .policy = gtp_genl_policy,
1259 .flags = GENL_ADMIN_PERM,
1260 },
1261 {
1262 .cmd = GTP_CMD_GETPDP,
1263 .doit = gtp_genl_get_pdp,
1264 .dumpit = gtp_genl_dump_pdp,
1265 .policy = gtp_genl_policy,
1266 .flags = GENL_ADMIN_PERM,
1267 },
1268};
1269
Johannes Berg56989f62016-10-24 14:40:05 +02001270static struct genl_family gtp_genl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001271 .name = "gtp",
1272 .version = 0,
1273 .hdrsize = 0,
1274 .maxattr = GTPA_MAX,
1275 .netnsok = true,
1276 .module = THIS_MODULE,
1277 .ops = gtp_genl_ops,
1278 .n_ops = ARRAY_SIZE(gtp_genl_ops),
1279};
1280
Pablo Neira459aa662016-05-09 00:55:48 +02001281static int __net_init gtp_net_init(struct net *net)
1282{
1283 struct gtp_net *gn = net_generic(net, gtp_net_id);
1284
1285 INIT_LIST_HEAD(&gn->gtp_dev_list);
1286 return 0;
1287}
1288
1289static void __net_exit gtp_net_exit(struct net *net)
1290{
1291 struct gtp_net *gn = net_generic(net, gtp_net_id);
1292 struct gtp_dev *gtp;
1293 LIST_HEAD(list);
1294
1295 rtnl_lock();
1296 list_for_each_entry(gtp, &gn->gtp_dev_list, list)
1297 gtp_dellink(gtp->dev, &list);
1298
1299 unregister_netdevice_many(&list);
1300 rtnl_unlock();
1301}
1302
1303static struct pernet_operations gtp_net_ops = {
1304 .init = gtp_net_init,
1305 .exit = gtp_net_exit,
1306 .id = &gtp_net_id,
1307 .size = sizeof(struct gtp_net),
1308};
1309
1310static int __init gtp_init(void)
1311{
1312 int err;
1313
1314 get_random_bytes(&gtp_h_initval, sizeof(gtp_h_initval));
1315
1316 err = rtnl_link_register(&gtp_link_ops);
1317 if (err < 0)
1318 goto error_out;
1319
Johannes Berg489111e2016-10-24 14:40:03 +02001320 err = genl_register_family(&gtp_genl_family);
Pablo Neira459aa662016-05-09 00:55:48 +02001321 if (err < 0)
1322 goto unreg_rtnl_link;
1323
1324 err = register_pernet_subsys(&gtp_net_ops);
1325 if (err < 0)
1326 goto unreg_genl_family;
1327
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001328 pr_info("GTP module loaded (pdp ctx size %zd bytes)\n",
Pablo Neira459aa662016-05-09 00:55:48 +02001329 sizeof(struct pdp_ctx));
1330 return 0;
1331
1332unreg_genl_family:
1333 genl_unregister_family(&gtp_genl_family);
1334unreg_rtnl_link:
1335 rtnl_link_unregister(&gtp_link_ops);
1336error_out:
1337 pr_err("error loading GTP module loaded\n");
1338 return err;
1339}
1340late_initcall(gtp_init);
1341
1342static void __exit gtp_fini(void)
1343{
1344 unregister_pernet_subsys(&gtp_net_ops);
1345 genl_unregister_family(&gtp_genl_family);
1346 rtnl_link_unregister(&gtp_link_ops);
1347
1348 pr_info("GTP module unloaded\n");
1349}
1350module_exit(gtp_fini);
1351
1352MODULE_LICENSE("GPL");
1353MODULE_AUTHOR("Harald Welte <hwelte@sysmocom.de>");
1354MODULE_DESCRIPTION("Interface driver for GTP encapsulated traffic");
1355MODULE_ALIAS_RTNL_LINK("gtp");
Andreas Schultzab729822017-01-27 10:40:56 +01001356MODULE_ALIAS_GENL_FAMILY("gtp");