blob: 74018ebcaf3af018efdeb032b26450bae38b2bc0 [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
61 atomic_t tx_seq;
62 struct rcu_head rcu_head;
63};
64
65/* One instance of the GTP device. */
66struct gtp_dev {
67 struct list_head list;
68
Andreas Schultz17886c472017-03-09 17:42:56 +010069 struct sock *sk0;
70 struct sock *sk1u;
Pablo Neira459aa662016-05-09 00:55:48 +020071
Pablo Neira459aa662016-05-09 00:55:48 +020072 struct net_device *dev;
73
74 unsigned int hash_size;
75 struct hlist_head *tid_hash;
76 struct hlist_head *addr_hash;
77};
78
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030079static unsigned int gtp_net_id __read_mostly;
Pablo Neira459aa662016-05-09 00:55:48 +020080
81struct gtp_net {
82 struct list_head gtp_dev_list;
83};
84
85static u32 gtp_h_initval;
86
87static inline u32 gtp0_hashfn(u64 tid)
88{
89 u32 *tid32 = (u32 *) &tid;
90 return jhash_2words(tid32[0], tid32[1], gtp_h_initval);
91}
92
93static inline u32 gtp1u_hashfn(u32 tid)
94{
95 return jhash_1word(tid, gtp_h_initval);
96}
97
98static inline u32 ipv4_hashfn(__be32 ip)
99{
100 return jhash_1word((__force u32)ip, gtp_h_initval);
101}
102
103/* Resolve a PDP context structure based on the 64bit TID. */
104static struct pdp_ctx *gtp0_pdp_find(struct gtp_dev *gtp, u64 tid)
105{
106 struct hlist_head *head;
107 struct pdp_ctx *pdp;
108
109 head = &gtp->tid_hash[gtp0_hashfn(tid) % gtp->hash_size];
110
111 hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
112 if (pdp->gtp_version == GTP_V0 &&
113 pdp->u.v0.tid == tid)
114 return pdp;
115 }
116 return NULL;
117}
118
119/* Resolve a PDP context structure based on the 32bit TEI. */
120static struct pdp_ctx *gtp1_pdp_find(struct gtp_dev *gtp, u32 tid)
121{
122 struct hlist_head *head;
123 struct pdp_ctx *pdp;
124
125 head = &gtp->tid_hash[gtp1u_hashfn(tid) % gtp->hash_size];
126
127 hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
128 if (pdp->gtp_version == GTP_V1 &&
129 pdp->u.v1.i_tei == tid)
130 return pdp;
131 }
132 return NULL;
133}
134
135/* Resolve a PDP context based on IPv4 address of MS. */
136static struct pdp_ctx *ipv4_pdp_find(struct gtp_dev *gtp, __be32 ms_addr)
137{
138 struct hlist_head *head;
139 struct pdp_ctx *pdp;
140
141 head = &gtp->addr_hash[ipv4_hashfn(ms_addr) % gtp->hash_size];
142
143 hlist_for_each_entry_rcu(pdp, head, hlist_addr) {
144 if (pdp->af == AF_INET &&
145 pdp->ms_addr_ip4.s_addr == ms_addr)
146 return pdp;
147 }
148
149 return NULL;
150}
151
152static bool gtp_check_src_ms_ipv4(struct sk_buff *skb, struct pdp_ctx *pctx,
153 unsigned int hdrlen)
154{
155 struct iphdr *iph;
156
157 if (!pskb_may_pull(skb, hdrlen + sizeof(struct iphdr)))
158 return false;
159
Lionel Gauthier88edf102016-12-15 22:35:52 +0100160 iph = (struct iphdr *)(skb->data + hdrlen);
Pablo Neira459aa662016-05-09 00:55:48 +0200161
Lionel Gauthier88edf102016-12-15 22:35:52 +0100162 return iph->saddr == pctx->ms_addr_ip4.s_addr;
Pablo Neira459aa662016-05-09 00:55:48 +0200163}
164
165/* Check if the inner IP source address in this packet is assigned to any
166 * existing mobile subscriber.
167 */
168static bool gtp_check_src_ms(struct sk_buff *skb, struct pdp_ctx *pctx,
169 unsigned int hdrlen)
170{
171 switch (ntohs(skb->protocol)) {
172 case ETH_P_IP:
173 return gtp_check_src_ms_ipv4(skb, pctx, hdrlen);
174 }
175 return false;
176}
177
178/* 1 means pass up to the stack, -1 means drop and 0 means decapsulated. */
179static int gtp0_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb,
180 bool xnet)
181{
182 unsigned int hdrlen = sizeof(struct udphdr) +
183 sizeof(struct gtp0_header);
184 struct gtp0_header *gtp0;
185 struct pdp_ctx *pctx;
Pablo Neira459aa662016-05-09 00:55:48 +0200186
187 if (!pskb_may_pull(skb, hdrlen))
188 return -1;
189
190 gtp0 = (struct gtp0_header *)(skb->data + sizeof(struct udphdr));
191
192 if ((gtp0->flags >> 5) != GTP_V0)
193 return 1;
194
195 if (gtp0->type != GTP_TPDU)
196 return 1;
197
Pablo Neira459aa662016-05-09 00:55:48 +0200198 pctx = gtp0_pdp_find(gtp, be64_to_cpu(gtp0->tid));
199 if (!pctx) {
200 netdev_dbg(gtp->dev, "No PDP ctx to decap skb=%p\n", skb);
Andreas Schultz1796a812017-01-26 16:21:49 +0100201 return 1;
Pablo Neira459aa662016-05-09 00:55:48 +0200202 }
203
204 if (!gtp_check_src_ms(skb, pctx, hdrlen)) {
205 netdev_dbg(gtp->dev, "No PDP ctx for this MS\n");
Andreas Schultz1796a812017-01-26 16:21:49 +0100206 return 1;
Pablo Neira459aa662016-05-09 00:55:48 +0200207 }
Pablo Neira459aa662016-05-09 00:55:48 +0200208
209 /* Get rid of the GTP + UDP headers. */
210 return iptunnel_pull_header(skb, hdrlen, skb->protocol, xnet);
Pablo Neira459aa662016-05-09 00:55:48 +0200211}
212
213static int gtp1u_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb,
214 bool xnet)
215{
216 unsigned int hdrlen = sizeof(struct udphdr) +
217 sizeof(struct gtp1_header);
218 struct gtp1_header *gtp1;
219 struct pdp_ctx *pctx;
Pablo Neira459aa662016-05-09 00:55:48 +0200220
221 if (!pskb_may_pull(skb, hdrlen))
222 return -1;
223
224 gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
225
226 if ((gtp1->flags >> 5) != GTP_V1)
227 return 1;
228
229 if (gtp1->type != GTP_TPDU)
230 return 1;
231
232 /* From 29.060: "This field shall be present if and only if any one or
233 * more of the S, PN and E flags are set.".
234 *
235 * If any of the bit is set, then the remaining ones also have to be
236 * set.
237 */
238 if (gtp1->flags & GTP1_F_MASK)
239 hdrlen += 4;
240
241 /* Make sure the header is larger enough, including extensions. */
242 if (!pskb_may_pull(skb, hdrlen))
243 return -1;
244
Pablo Neira93edb8c2016-05-10 21:33:38 +0200245 gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
246
Pablo Neira459aa662016-05-09 00:55:48 +0200247 pctx = gtp1_pdp_find(gtp, ntohl(gtp1->tid));
248 if (!pctx) {
249 netdev_dbg(gtp->dev, "No PDP ctx to decap skb=%p\n", skb);
Andreas Schultz1796a812017-01-26 16:21:49 +0100250 return 1;
Pablo Neira459aa662016-05-09 00:55:48 +0200251 }
252
253 if (!gtp_check_src_ms(skb, pctx, hdrlen)) {
254 netdev_dbg(gtp->dev, "No PDP ctx for this MS\n");
Andreas Schultz1796a812017-01-26 16:21:49 +0100255 return 1;
Pablo Neira459aa662016-05-09 00:55:48 +0200256 }
Pablo Neira459aa662016-05-09 00:55:48 +0200257
258 /* Get rid of the GTP + UDP headers. */
259 return iptunnel_pull_header(skb, hdrlen, skb->protocol, xnet);
Pablo Neira459aa662016-05-09 00:55:48 +0200260}
261
Pablo Neira459aa662016-05-09 00:55:48 +0200262static void gtp_encap_destroy(struct sock *sk)
263{
264 struct gtp_dev *gtp;
265
266 gtp = rcu_dereference_sk_user_data(sk);
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100267 if (gtp) {
268 udp_sk(sk)->encap_type = 0;
269 rcu_assign_sk_user_data(sk, NULL);
270 sock_put(sk);
271 }
272}
273
274static void gtp_encap_disable_sock(struct sock *sk)
275{
276 if (!sk)
277 return;
278
279 gtp_encap_destroy(sk);
280}
281
282static void gtp_encap_disable(struct gtp_dev *gtp)
283{
284 gtp_encap_disable_sock(gtp->sk0);
285 gtp_encap_disable_sock(gtp->sk1u);
Pablo Neira459aa662016-05-09 00:55:48 +0200286}
287
288/* UDP encapsulation receive handler. See net/ipv4/udp.c.
289 * Return codes: 0: success, <0: error, >0: pass up to userspace UDP socket.
290 */
291static int gtp_encap_recv(struct sock *sk, struct sk_buff *skb)
292{
293 struct pcpu_sw_netstats *stats;
294 struct gtp_dev *gtp;
295 bool xnet;
296 int ret;
297
298 gtp = rcu_dereference_sk_user_data(sk);
299 if (!gtp)
300 return 1;
301
302 netdev_dbg(gtp->dev, "encap_recv sk=%p\n", sk);
303
Andreas Schultz3ab1b462017-01-27 10:40:58 +0100304 xnet = !net_eq(sock_net(sk), dev_net(gtp->dev));
Pablo Neira459aa662016-05-09 00:55:48 +0200305
306 switch (udp_sk(sk)->encap_type) {
307 case UDP_ENCAP_GTP0:
308 netdev_dbg(gtp->dev, "received GTP0 packet\n");
309 ret = gtp0_udp_encap_recv(gtp, skb, xnet);
310 break;
311 case UDP_ENCAP_GTP1U:
312 netdev_dbg(gtp->dev, "received GTP1U packet\n");
313 ret = gtp1u_udp_encap_recv(gtp, skb, xnet);
314 break;
315 default:
316 ret = -1; /* Shouldn't happen. */
317 }
318
319 switch (ret) {
320 case 1:
321 netdev_dbg(gtp->dev, "pass up to the process\n");
322 return 1;
323 case 0:
324 netdev_dbg(gtp->dev, "forwarding packet from GGSN to uplink\n");
325 break;
326 case -1:
327 netdev_dbg(gtp->dev, "GTP packet has been dropped\n");
328 kfree_skb(skb);
329 return 0;
330 }
331
332 /* Now that the UDP and the GTP header have been removed, set up the
333 * new network header. This is required by the upper layer to
334 * calculate the transport header.
335 */
336 skb_reset_network_header(skb);
337
338 skb->dev = gtp->dev;
339
340 stats = this_cpu_ptr(gtp->dev->tstats);
341 u64_stats_update_begin(&stats->syncp);
342 stats->rx_packets++;
343 stats->rx_bytes += skb->len;
344 u64_stats_update_end(&stats->syncp);
345
346 netif_rx(skb);
347
348 return 0;
349}
350
351static int gtp_dev_init(struct net_device *dev)
352{
353 struct gtp_dev *gtp = netdev_priv(dev);
354
355 gtp->dev = dev;
356
357 dev->tstats = alloc_percpu(struct pcpu_sw_netstats);
358 if (!dev->tstats)
359 return -ENOMEM;
360
361 return 0;
362}
363
364static void gtp_dev_uninit(struct net_device *dev)
365{
366 struct gtp_dev *gtp = netdev_priv(dev);
367
368 gtp_encap_disable(gtp);
369 free_percpu(dev->tstats);
370}
371
372static struct rtable *ip4_route_output_gtp(struct net *net, struct flowi4 *fl4,
373 const struct sock *sk, __be32 daddr)
374{
375 memset(fl4, 0, sizeof(*fl4));
376 fl4->flowi4_oif = sk->sk_bound_dev_if;
377 fl4->daddr = daddr;
378 fl4->saddr = inet_sk(sk)->inet_saddr;
379 fl4->flowi4_tos = RT_CONN_FLAGS(sk);
380 fl4->flowi4_proto = sk->sk_protocol;
381
382 return ip_route_output_key(net, fl4);
383}
384
385static inline void gtp0_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
386{
387 int payload_len = skb->len;
388 struct gtp0_header *gtp0;
389
390 gtp0 = (struct gtp0_header *) skb_push(skb, sizeof(*gtp0));
391
392 gtp0->flags = 0x1e; /* v0, GTP-non-prime. */
393 gtp0->type = GTP_TPDU;
394 gtp0->length = htons(payload_len);
395 gtp0->seq = htons((atomic_inc_return(&pctx->tx_seq) - 1) % 0xffff);
396 gtp0->flow = htons(pctx->u.v0.flow);
397 gtp0->number = 0xff;
398 gtp0->spare[0] = gtp0->spare[1] = gtp0->spare[2] = 0xff;
399 gtp0->tid = cpu_to_be64(pctx->u.v0.tid);
400}
401
402static inline void gtp1_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
403{
404 int payload_len = skb->len;
405 struct gtp1_header *gtp1;
406
407 gtp1 = (struct gtp1_header *) skb_push(skb, sizeof(*gtp1));
408
409 /* Bits 8 7 6 5 4 3 2 1
410 * +--+--+--+--+--+--+--+--+
Harald Welted928be82016-12-15 22:35:53 +0100411 * |version |PT| 0| E| S|PN|
Pablo Neira459aa662016-05-09 00:55:48 +0200412 * +--+--+--+--+--+--+--+--+
413 * 0 0 1 1 1 0 0 0
414 */
Harald Welted928be82016-12-15 22:35:53 +0100415 gtp1->flags = 0x30; /* v1, GTP-non-prime. */
Pablo Neira459aa662016-05-09 00:55:48 +0200416 gtp1->type = GTP_TPDU;
417 gtp1->length = htons(payload_len);
418 gtp1->tid = htonl(pctx->u.v1.o_tei);
419
420 /* TODO: Suppport for extension header, sequence number and N-PDU.
421 * Update the length field if any of them is available.
422 */
423}
424
425struct gtp_pktinfo {
426 struct sock *sk;
427 struct iphdr *iph;
428 struct flowi4 fl4;
429 struct rtable *rt;
430 struct pdp_ctx *pctx;
431 struct net_device *dev;
432 __be16 gtph_port;
433};
434
435static void gtp_push_header(struct sk_buff *skb, struct gtp_pktinfo *pktinfo)
436{
437 switch (pktinfo->pctx->gtp_version) {
438 case GTP_V0:
439 pktinfo->gtph_port = htons(GTP0_PORT);
440 gtp0_push_header(skb, pktinfo->pctx);
441 break;
442 case GTP_V1:
443 pktinfo->gtph_port = htons(GTP1U_PORT);
444 gtp1_push_header(skb, pktinfo->pctx);
445 break;
446 }
447}
448
449static inline void gtp_set_pktinfo_ipv4(struct gtp_pktinfo *pktinfo,
450 struct sock *sk, struct iphdr *iph,
451 struct pdp_ctx *pctx, struct rtable *rt,
452 struct flowi4 *fl4,
453 struct net_device *dev)
454{
455 pktinfo->sk = sk;
456 pktinfo->iph = iph;
457 pktinfo->pctx = pctx;
458 pktinfo->rt = rt;
459 pktinfo->fl4 = *fl4;
460 pktinfo->dev = dev;
461}
462
463static int gtp_build_skb_ip4(struct sk_buff *skb, struct net_device *dev,
464 struct gtp_pktinfo *pktinfo)
465{
466 struct gtp_dev *gtp = netdev_priv(dev);
467 struct pdp_ctx *pctx;
468 struct rtable *rt;
469 struct flowi4 fl4;
470 struct iphdr *iph;
471 struct sock *sk;
472 __be16 df;
473 int mtu;
474
475 /* Read the IP destination address and resolve the PDP context.
476 * Prepend PDP header with TEI/TID from PDP ctx.
477 */
478 iph = ip_hdr(skb);
479 pctx = ipv4_pdp_find(gtp, iph->daddr);
480 if (!pctx) {
481 netdev_dbg(dev, "no PDP ctx found for %pI4, skip\n",
482 &iph->daddr);
483 return -ENOENT;
484 }
485 netdev_dbg(dev, "found PDP context %p\n", pctx);
486
487 switch (pctx->gtp_version) {
488 case GTP_V0:
Andreas Schultz17886c472017-03-09 17:42:56 +0100489 if (gtp->sk0)
490 sk = gtp->sk0;
Pablo Neira459aa662016-05-09 00:55:48 +0200491 else
492 sk = NULL;
493 break;
494 case GTP_V1:
Andreas Schultz17886c472017-03-09 17:42:56 +0100495 if (gtp->sk1u)
496 sk = gtp->sk1u;
Pablo Neira459aa662016-05-09 00:55:48 +0200497 else
498 sk = NULL;
499 break;
500 default:
501 return -ENOENT;
502 }
503
504 if (!sk) {
505 netdev_dbg(dev, "no userspace socket is available, skip\n");
506 return -ENOENT;
507 }
508
Andreas Schultz17886c472017-03-09 17:42:56 +0100509 rt = ip4_route_output_gtp(sock_net(sk), &fl4, gtp->sk0,
Pablo Neira459aa662016-05-09 00:55:48 +0200510 pctx->sgsn_addr_ip4.s_addr);
511 if (IS_ERR(rt)) {
512 netdev_dbg(dev, "no route to SSGN %pI4\n",
513 &pctx->sgsn_addr_ip4.s_addr);
514 dev->stats.tx_carrier_errors++;
515 goto err;
516 }
517
518 if (rt->dst.dev == dev) {
519 netdev_dbg(dev, "circular route to SSGN %pI4\n",
520 &pctx->sgsn_addr_ip4.s_addr);
521 dev->stats.collisions++;
522 goto err_rt;
523 }
524
525 skb_dst_drop(skb);
526
527 /* This is similar to tnl_update_pmtu(). */
528 df = iph->frag_off;
529 if (df) {
530 mtu = dst_mtu(&rt->dst) - dev->hard_header_len -
531 sizeof(struct iphdr) - sizeof(struct udphdr);
532 switch (pctx->gtp_version) {
533 case GTP_V0:
534 mtu -= sizeof(struct gtp0_header);
535 break;
536 case GTP_V1:
537 mtu -= sizeof(struct gtp1_header);
538 break;
539 }
540 } else {
541 mtu = dst_mtu(&rt->dst);
542 }
543
544 rt->dst.ops->update_pmtu(&rt->dst, NULL, skb, mtu);
545
546 if (!skb_is_gso(skb) && (iph->frag_off & htons(IP_DF)) &&
547 mtu < ntohs(iph->tot_len)) {
548 netdev_dbg(dev, "packet too big, fragmentation needed\n");
549 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
550 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
551 htonl(mtu));
552 goto err_rt;
553 }
554
555 gtp_set_pktinfo_ipv4(pktinfo, sk, iph, pctx, rt, &fl4, dev);
556 gtp_push_header(skb, pktinfo);
557
558 return 0;
559err_rt:
560 ip_rt_put(rt);
561err:
562 return -EBADMSG;
563}
564
565static netdev_tx_t gtp_dev_xmit(struct sk_buff *skb, struct net_device *dev)
566{
567 unsigned int proto = ntohs(skb->protocol);
568 struct gtp_pktinfo pktinfo;
569 int err;
570
571 /* Ensure there is sufficient headroom. */
572 if (skb_cow_head(skb, dev->needed_headroom))
573 goto tx_err;
574
575 skb_reset_inner_headers(skb);
576
577 /* PDP context lookups in gtp_build_skb_*() need rcu read-side lock. */
578 rcu_read_lock();
579 switch (proto) {
580 case ETH_P_IP:
581 err = gtp_build_skb_ip4(skb, dev, &pktinfo);
582 break;
583 default:
584 err = -EOPNOTSUPP;
585 break;
586 }
587 rcu_read_unlock();
588
589 if (err < 0)
590 goto tx_err;
591
592 switch (proto) {
593 case ETH_P_IP:
594 netdev_dbg(pktinfo.dev, "gtp -> IP src: %pI4 dst: %pI4\n",
595 &pktinfo.iph->saddr, &pktinfo.iph->daddr);
596 udp_tunnel_xmit_skb(pktinfo.rt, pktinfo.sk, skb,
597 pktinfo.fl4.saddr, pktinfo.fl4.daddr,
598 pktinfo.iph->tos,
599 ip4_dst_hoplimit(&pktinfo.rt->dst),
Andreas Schultzc6ce1d02017-01-27 10:40:57 +0100600 0,
Pablo Neira459aa662016-05-09 00:55:48 +0200601 pktinfo.gtph_port, pktinfo.gtph_port,
602 true, false);
603 break;
604 }
605
606 return NETDEV_TX_OK;
607tx_err:
608 dev->stats.tx_errors++;
609 dev_kfree_skb(skb);
610 return NETDEV_TX_OK;
611}
612
613static const struct net_device_ops gtp_netdev_ops = {
614 .ndo_init = gtp_dev_init,
615 .ndo_uninit = gtp_dev_uninit,
616 .ndo_start_xmit = gtp_dev_xmit,
617 .ndo_get_stats64 = ip_tunnel_get_stats64,
618};
619
620static void gtp_link_setup(struct net_device *dev)
621{
622 dev->netdev_ops = &gtp_netdev_ops;
623 dev->destructor = free_netdev;
624
625 dev->hard_header_len = 0;
626 dev->addr_len = 0;
627
628 /* Zero header length. */
629 dev->type = ARPHRD_NONE;
630 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
631
632 dev->priv_flags |= IFF_NO_QUEUE;
633 dev->features |= NETIF_F_LLTX;
634 netif_keep_dst(dev);
635
636 /* Assume largest header, ie. GTPv0. */
637 dev->needed_headroom = LL_MAX_HEADER +
638 sizeof(struct iphdr) +
639 sizeof(struct udphdr) +
640 sizeof(struct gtp0_header);
641}
642
643static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize);
644static void gtp_hashtable_free(struct gtp_dev *gtp);
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100645static int gtp_encap_enable(struct gtp_dev *gtp, struct nlattr *data[]);
Pablo Neira459aa662016-05-09 00:55:48 +0200646
647static int gtp_newlink(struct net *src_net, struct net_device *dev,
648 struct nlattr *tb[], struct nlattr *data[])
649{
Pablo Neira459aa662016-05-09 00:55:48 +0200650 struct gtp_dev *gtp;
651 struct gtp_net *gn;
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100652 int hashsize, err;
Pablo Neira459aa662016-05-09 00:55:48 +0200653
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100654 if (!data[IFLA_GTP_FD0] && !data[IFLA_GTP_FD1])
Pablo Neira459aa662016-05-09 00:55:48 +0200655 return -EINVAL;
656
657 gtp = netdev_priv(dev);
658
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100659 err = gtp_encap_enable(gtp, data);
Pablo Neira459aa662016-05-09 00:55:48 +0200660 if (err < 0)
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100661 return err;
Pablo Neira459aa662016-05-09 00:55:48 +0200662
663 if (!data[IFLA_GTP_PDP_HASHSIZE])
664 hashsize = 1024;
665 else
666 hashsize = nla_get_u32(data[IFLA_GTP_PDP_HASHSIZE]);
667
668 err = gtp_hashtable_new(gtp, hashsize);
669 if (err < 0)
670 goto out_encap;
671
672 err = register_netdevice(dev);
673 if (err < 0) {
674 netdev_dbg(dev, "failed to register new netdev %d\n", err);
675 goto out_hashtable;
676 }
677
678 gn = net_generic(dev_net(dev), gtp_net_id);
679 list_add_rcu(&gtp->list, &gn->gtp_dev_list);
680
681 netdev_dbg(dev, "registered new GTP interface\n");
682
683 return 0;
684
685out_hashtable:
686 gtp_hashtable_free(gtp);
687out_encap:
688 gtp_encap_disable(gtp);
Pablo Neira459aa662016-05-09 00:55:48 +0200689 return err;
690}
691
692static void gtp_dellink(struct net_device *dev, struct list_head *head)
693{
694 struct gtp_dev *gtp = netdev_priv(dev);
695
696 gtp_encap_disable(gtp);
697 gtp_hashtable_free(gtp);
698 list_del_rcu(&gtp->list);
699 unregister_netdevice_queue(dev, head);
700}
701
702static const struct nla_policy gtp_policy[IFLA_GTP_MAX + 1] = {
703 [IFLA_GTP_FD0] = { .type = NLA_U32 },
704 [IFLA_GTP_FD1] = { .type = NLA_U32 },
705 [IFLA_GTP_PDP_HASHSIZE] = { .type = NLA_U32 },
706};
707
708static int gtp_validate(struct nlattr *tb[], struct nlattr *data[])
709{
710 if (!data)
711 return -EINVAL;
712
713 return 0;
714}
715
716static size_t gtp_get_size(const struct net_device *dev)
717{
718 return nla_total_size(sizeof(__u32)); /* IFLA_GTP_PDP_HASHSIZE */
719}
720
721static int gtp_fill_info(struct sk_buff *skb, const struct net_device *dev)
722{
723 struct gtp_dev *gtp = netdev_priv(dev);
724
725 if (nla_put_u32(skb, IFLA_GTP_PDP_HASHSIZE, gtp->hash_size))
726 goto nla_put_failure;
727
728 return 0;
729
730nla_put_failure:
731 return -EMSGSIZE;
732}
733
734static struct rtnl_link_ops gtp_link_ops __read_mostly = {
735 .kind = "gtp",
736 .maxtype = IFLA_GTP_MAX,
737 .policy = gtp_policy,
738 .priv_size = sizeof(struct gtp_dev),
739 .setup = gtp_link_setup,
740 .validate = gtp_validate,
741 .newlink = gtp_newlink,
742 .dellink = gtp_dellink,
743 .get_size = gtp_get_size,
744 .fill_info = gtp_fill_info,
745};
746
Pablo Neira459aa662016-05-09 00:55:48 +0200747static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize)
748{
749 int i;
750
751 gtp->addr_hash = kmalloc(sizeof(struct hlist_head) * hsize, GFP_KERNEL);
752 if (gtp->addr_hash == NULL)
753 return -ENOMEM;
754
755 gtp->tid_hash = kmalloc(sizeof(struct hlist_head) * hsize, GFP_KERNEL);
756 if (gtp->tid_hash == NULL)
757 goto err1;
758
759 gtp->hash_size = hsize;
760
761 for (i = 0; i < hsize; i++) {
762 INIT_HLIST_HEAD(&gtp->addr_hash[i]);
763 INIT_HLIST_HEAD(&gtp->tid_hash[i]);
764 }
765 return 0;
766err1:
767 kfree(gtp->addr_hash);
768 return -ENOMEM;
769}
770
771static void gtp_hashtable_free(struct gtp_dev *gtp)
772{
773 struct pdp_ctx *pctx;
774 int i;
775
776 for (i = 0; i < gtp->hash_size; i++) {
777 hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid) {
778 hlist_del_rcu(&pctx->hlist_tid);
779 hlist_del_rcu(&pctx->hlist_addr);
780 kfree_rcu(pctx, rcu_head);
781 }
782 }
783 synchronize_rcu();
784 kfree(gtp->addr_hash);
785 kfree(gtp->tid_hash);
786}
787
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100788static struct sock *gtp_encap_enable_socket(int fd, int type,
789 struct gtp_dev *gtp)
Pablo Neira459aa662016-05-09 00:55:48 +0200790{
791 struct udp_tunnel_sock_cfg tuncfg = {NULL};
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100792 struct socket *sock;
793 struct sock *sk;
Pablo Neira459aa662016-05-09 00:55:48 +0200794 int err;
795
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100796 pr_debug("enable gtp on %d, %d\n", fd, type);
Pablo Neira459aa662016-05-09 00:55:48 +0200797
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100798 sock = sockfd_lookup(fd, &err);
799 if (!sock) {
800 pr_debug("gtp socket fd=%d not found\n", fd);
801 return NULL;
Pablo Neira459aa662016-05-09 00:55:48 +0200802 }
803
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100804 if (sock->sk->sk_protocol != IPPROTO_UDP) {
805 pr_debug("socket fd=%d not UDP\n", fd);
806 sk = ERR_PTR(-EINVAL);
807 goto out_sock;
Pablo Neira459aa662016-05-09 00:55:48 +0200808 }
809
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100810 if (rcu_dereference_sk_user_data(sock->sk)) {
811 sk = ERR_PTR(-EBUSY);
812 goto out_sock;
Pablo Neira459aa662016-05-09 00:55:48 +0200813 }
814
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100815 sk = sock->sk;
816 sock_hold(sk);
Pablo Neira459aa662016-05-09 00:55:48 +0200817
818 tuncfg.sk_user_data = gtp;
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100819 tuncfg.encap_type = type;
Pablo Neira459aa662016-05-09 00:55:48 +0200820 tuncfg.encap_rcv = gtp_encap_recv;
821 tuncfg.encap_destroy = gtp_encap_destroy;
822
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100823 setup_udp_tunnel_sock(sock_net(sock->sk), sock, &tuncfg);
Pablo Neira459aa662016-05-09 00:55:48 +0200824
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100825out_sock:
826 sockfd_put(sock);
827 return sk;
828}
Pablo Neira459aa662016-05-09 00:55:48 +0200829
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100830static int gtp_encap_enable(struct gtp_dev *gtp, struct nlattr *data[])
831{
832 struct sock *sk1u = NULL;
833 struct sock *sk0 = NULL;
834
835 if (data[IFLA_GTP_FD0]) {
836 u32 fd0 = nla_get_u32(data[IFLA_GTP_FD0]);
837
838 sk0 = gtp_encap_enable_socket(fd0, UDP_ENCAP_GTP0, gtp);
839 if (IS_ERR(sk0))
840 return PTR_ERR(sk0);
841 }
842
843 if (data[IFLA_GTP_FD1]) {
844 u32 fd1 = nla_get_u32(data[IFLA_GTP_FD1]);
845
846 sk1u = gtp_encap_enable_socket(fd1, UDP_ENCAP_GTP1U, gtp);
847 if (IS_ERR(sk1u)) {
848 if (sk0)
849 gtp_encap_disable_sock(sk0);
850 return PTR_ERR(sk1u);
851 }
852 }
853
854 gtp->sk0 = sk0;
855 gtp->sk1u = sk1u;
856
857 return 0;
Pablo Neira459aa662016-05-09 00:55:48 +0200858}
859
Andreas Schultz3fb94612017-03-09 17:42:58 +0100860static struct gtp_dev *gtp_find_dev(struct net *src_net, struct nlattr *nla[])
Pablo Neira459aa662016-05-09 00:55:48 +0200861{
Andreas Schultz3fb94612017-03-09 17:42:58 +0100862 struct gtp_dev *gtp = NULL;
863 struct net_device *dev;
864 struct net *net;
Pablo Neira459aa662016-05-09 00:55:48 +0200865
Andreas Schultz3fb94612017-03-09 17:42:58 +0100866 /* Examine the link attributes and figure out which network namespace
867 * we are talking about.
868 */
869 if (nla[GTPA_NET_NS_FD])
870 net = get_net_ns_by_fd(nla_get_u32(nla[GTPA_NET_NS_FD]));
871 else
872 net = get_net(src_net);
873
874 if (IS_ERR(net))
875 return NULL;
876
877 /* Check if there's an existing gtpX device to configure */
878 dev = dev_get_by_index_rcu(net, nla_get_u32(nla[GTPA_LINK]));
879 if (dev->netdev_ops == &gtp_netdev_ops)
880 gtp = netdev_priv(dev);
881
882 put_net(net);
883 return gtp;
Pablo Neira459aa662016-05-09 00:55:48 +0200884}
885
886static void ipv4_pdp_fill(struct pdp_ctx *pctx, struct genl_info *info)
887{
888 pctx->gtp_version = nla_get_u32(info->attrs[GTPA_VERSION]);
889 pctx->af = AF_INET;
890 pctx->sgsn_addr_ip4.s_addr =
891 nla_get_be32(info->attrs[GTPA_SGSN_ADDRESS]);
892 pctx->ms_addr_ip4.s_addr =
893 nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
894
895 switch (pctx->gtp_version) {
896 case GTP_V0:
897 /* According to TS 09.60, sections 7.5.1 and 7.5.2, the flow
898 * label needs to be the same for uplink and downlink packets,
899 * so let's annotate this.
900 */
901 pctx->u.v0.tid = nla_get_u64(info->attrs[GTPA_TID]);
902 pctx->u.v0.flow = nla_get_u16(info->attrs[GTPA_FLOW]);
903 break;
904 case GTP_V1:
905 pctx->u.v1.i_tei = nla_get_u32(info->attrs[GTPA_I_TEI]);
906 pctx->u.v1.o_tei = nla_get_u32(info->attrs[GTPA_O_TEI]);
907 break;
908 default:
909 break;
910 }
911}
912
Andreas Schultz3fb94612017-03-09 17:42:58 +0100913static int ipv4_pdp_add(struct gtp_dev *gtp, struct genl_info *info)
Pablo Neira459aa662016-05-09 00:55:48 +0200914{
Andreas Schultz3fb94612017-03-09 17:42:58 +0100915 struct net_device *dev = gtp->dev;
Pablo Neira459aa662016-05-09 00:55:48 +0200916 u32 hash_ms, hash_tid = 0;
917 struct pdp_ctx *pctx;
918 bool found = false;
919 __be32 ms_addr;
920
921 ms_addr = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
922 hash_ms = ipv4_hashfn(ms_addr) % gtp->hash_size;
923
924 hlist_for_each_entry_rcu(pctx, &gtp->addr_hash[hash_ms], hlist_addr) {
925 if (pctx->ms_addr_ip4.s_addr == ms_addr) {
926 found = true;
927 break;
928 }
929 }
930
931 if (found) {
932 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
933 return -EEXIST;
934 if (info->nlhdr->nlmsg_flags & NLM_F_REPLACE)
935 return -EOPNOTSUPP;
936
937 ipv4_pdp_fill(pctx, info);
938
939 if (pctx->gtp_version == GTP_V0)
940 netdev_dbg(dev, "GTPv0-U: update tunnel id = %llx (pdp %p)\n",
941 pctx->u.v0.tid, pctx);
942 else if (pctx->gtp_version == GTP_V1)
943 netdev_dbg(dev, "GTPv1-U: update tunnel id = %x/%x (pdp %p)\n",
944 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
945
946 return 0;
947
948 }
949
950 pctx = kmalloc(sizeof(struct pdp_ctx), GFP_KERNEL);
951 if (pctx == NULL)
952 return -ENOMEM;
953
954 ipv4_pdp_fill(pctx, info);
955 atomic_set(&pctx->tx_seq, 0);
956
957 switch (pctx->gtp_version) {
958 case GTP_V0:
959 /* TS 09.60: "The flow label identifies unambiguously a GTP
960 * flow.". We use the tid for this instead, I cannot find a
961 * situation in which this doesn't unambiguosly identify the
962 * PDP context.
963 */
964 hash_tid = gtp0_hashfn(pctx->u.v0.tid) % gtp->hash_size;
965 break;
966 case GTP_V1:
967 hash_tid = gtp1u_hashfn(pctx->u.v1.i_tei) % gtp->hash_size;
968 break;
969 }
970
971 hlist_add_head_rcu(&pctx->hlist_addr, &gtp->addr_hash[hash_ms]);
972 hlist_add_head_rcu(&pctx->hlist_tid, &gtp->tid_hash[hash_tid]);
973
974 switch (pctx->gtp_version) {
975 case GTP_V0:
976 netdev_dbg(dev, "GTPv0-U: new PDP ctx id=%llx ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
977 pctx->u.v0.tid, &pctx->sgsn_addr_ip4,
978 &pctx->ms_addr_ip4, pctx);
979 break;
980 case GTP_V1:
981 netdev_dbg(dev, "GTPv1-U: new PDP ctx id=%x/%x ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
982 pctx->u.v1.i_tei, pctx->u.v1.o_tei,
983 &pctx->sgsn_addr_ip4, &pctx->ms_addr_ip4, pctx);
984 break;
985 }
986
987 return 0;
988}
989
990static int gtp_genl_new_pdp(struct sk_buff *skb, struct genl_info *info)
991{
Andreas Schultz3fb94612017-03-09 17:42:58 +0100992 struct gtp_dev *gtp;
993 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
1001 switch (nla_get_u32(info->attrs[GTPA_VERSION])) {
1002 case GTP_V0:
1003 if (!info->attrs[GTPA_TID] ||
1004 !info->attrs[GTPA_FLOW])
1005 return -EINVAL;
1006 break;
1007 case GTP_V1:
1008 if (!info->attrs[GTPA_I_TEI] ||
1009 !info->attrs[GTPA_O_TEI])
1010 return -EINVAL;
1011 break;
1012
1013 default:
1014 return -EINVAL;
1015 }
1016
Andreas Schultz3fb94612017-03-09 17:42:58 +01001017 rcu_read_lock();
Pablo Neira459aa662016-05-09 00:55:48 +02001018
Andreas Schultz3fb94612017-03-09 17:42:58 +01001019 gtp = gtp_find_dev(sock_net(skb->sk), info->attrs);
1020 if (!gtp) {
1021 err = -ENODEV;
1022 goto out_unlock;
Pablo Neira27ee4412016-05-12 17:16:31 +02001023 }
Pablo Neira459aa662016-05-09 00:55:48 +02001024
Andreas Schultz3fb94612017-03-09 17:42:58 +01001025 err = ipv4_pdp_add(gtp, info);
1026
1027out_unlock:
1028 rcu_read_unlock();
1029 return err;
Pablo Neira459aa662016-05-09 00:55:48 +02001030}
1031
1032static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info)
1033{
Pablo Neira459aa662016-05-09 00:55:48 +02001034 struct pdp_ctx *pctx;
1035 struct gtp_dev *gtp;
Andreas Schultz3fb94612017-03-09 17:42:58 +01001036 int err = 0;
Pablo Neira459aa662016-05-09 00:55:48 +02001037
1038 if (!info->attrs[GTPA_VERSION] ||
1039 !info->attrs[GTPA_LINK])
1040 return -EINVAL;
1041
Andreas Schultz3fb94612017-03-09 17:42:58 +01001042 rcu_read_lock();
Pablo Neira459aa662016-05-09 00:55:48 +02001043
Andreas Schultz3fb94612017-03-09 17:42:58 +01001044 gtp = gtp_find_dev(sock_net(skb->sk), info->attrs);
1045 if (!gtp) {
1046 err = -ENODEV;
1047 goto out_unlock;
Pablo Neira27ee4412016-05-12 17:16:31 +02001048 }
Pablo Neira459aa662016-05-09 00:55:48 +02001049
1050 switch (nla_get_u32(info->attrs[GTPA_VERSION])) {
1051 case GTP_V0:
Andreas Schultz3fb94612017-03-09 17:42:58 +01001052 if (!info->attrs[GTPA_TID]) {
1053 err = -EINVAL;
1054 goto out_unlock;
1055 }
Pablo Neira459aa662016-05-09 00:55:48 +02001056 pctx = gtp0_pdp_find(gtp, nla_get_u64(info->attrs[GTPA_TID]));
1057 break;
1058 case GTP_V1:
Andreas Schultz3fb94612017-03-09 17:42:58 +01001059 if (!info->attrs[GTPA_I_TEI]) {
1060 err = -EINVAL;
1061 goto out_unlock;
1062 }
Pablo Neira459aa662016-05-09 00:55:48 +02001063 pctx = gtp1_pdp_find(gtp, nla_get_u64(info->attrs[GTPA_I_TEI]));
1064 break;
1065
1066 default:
Andreas Schultz3fb94612017-03-09 17:42:58 +01001067 err = -EINVAL;
1068 goto out_unlock;
Pablo Neira459aa662016-05-09 00:55:48 +02001069 }
1070
Andreas Schultz3fb94612017-03-09 17:42:58 +01001071 if (!pctx) {
1072 err = -ENOENT;
1073 goto out_unlock;
1074 }
Pablo Neira459aa662016-05-09 00:55:48 +02001075
1076 if (pctx->gtp_version == GTP_V0)
Andreas Schultz3fb94612017-03-09 17:42:58 +01001077 netdev_dbg(gtp->dev, "GTPv0-U: deleting tunnel id = %llx (pdp %p)\n",
Pablo Neira459aa662016-05-09 00:55:48 +02001078 pctx->u.v0.tid, pctx);
1079 else if (pctx->gtp_version == GTP_V1)
Andreas Schultz3fb94612017-03-09 17:42:58 +01001080 netdev_dbg(gtp->dev, "GTPv1-U: deleting tunnel id = %x/%x (pdp %p)\n",
Pablo Neira459aa662016-05-09 00:55:48 +02001081 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
1082
1083 hlist_del_rcu(&pctx->hlist_tid);
1084 hlist_del_rcu(&pctx->hlist_addr);
1085 kfree_rcu(pctx, rcu_head);
1086
Andreas Schultz3fb94612017-03-09 17:42:58 +01001087out_unlock:
1088 rcu_read_unlock();
1089 return err;
Pablo Neira459aa662016-05-09 00:55:48 +02001090}
1091
Johannes Berg489111e2016-10-24 14:40:03 +02001092static struct genl_family gtp_genl_family;
Pablo Neira459aa662016-05-09 00:55:48 +02001093
1094static int gtp_genl_fill_info(struct sk_buff *skb, u32 snd_portid, u32 snd_seq,
1095 u32 type, struct pdp_ctx *pctx)
1096{
1097 void *genlh;
1098
1099 genlh = genlmsg_put(skb, snd_portid, snd_seq, &gtp_genl_family, 0,
1100 type);
1101 if (genlh == NULL)
1102 goto nlmsg_failure;
1103
1104 if (nla_put_u32(skb, GTPA_VERSION, pctx->gtp_version) ||
1105 nla_put_be32(skb, GTPA_SGSN_ADDRESS, pctx->sgsn_addr_ip4.s_addr) ||
1106 nla_put_be32(skb, GTPA_MS_ADDRESS, pctx->ms_addr_ip4.s_addr))
1107 goto nla_put_failure;
1108
1109 switch (pctx->gtp_version) {
1110 case GTP_V0:
1111 if (nla_put_u64_64bit(skb, GTPA_TID, pctx->u.v0.tid, GTPA_PAD) ||
1112 nla_put_u16(skb, GTPA_FLOW, pctx->u.v0.flow))
1113 goto nla_put_failure;
1114 break;
1115 case GTP_V1:
1116 if (nla_put_u32(skb, GTPA_I_TEI, pctx->u.v1.i_tei) ||
1117 nla_put_u32(skb, GTPA_O_TEI, pctx->u.v1.o_tei))
1118 goto nla_put_failure;
1119 break;
1120 }
1121 genlmsg_end(skb, genlh);
1122 return 0;
1123
1124nlmsg_failure:
1125nla_put_failure:
1126 genlmsg_cancel(skb, genlh);
1127 return -EMSGSIZE;
1128}
1129
1130static int gtp_genl_get_pdp(struct sk_buff *skb, struct genl_info *info)
1131{
1132 struct pdp_ctx *pctx = NULL;
Pablo Neira459aa662016-05-09 00:55:48 +02001133 struct sk_buff *skb2;
1134 struct gtp_dev *gtp;
1135 u32 gtp_version;
Pablo Neira459aa662016-05-09 00:55:48 +02001136 int err;
1137
1138 if (!info->attrs[GTPA_VERSION] ||
1139 !info->attrs[GTPA_LINK])
1140 return -EINVAL;
1141
1142 gtp_version = nla_get_u32(info->attrs[GTPA_VERSION]);
1143 switch (gtp_version) {
1144 case GTP_V0:
1145 case GTP_V1:
1146 break;
1147 default:
1148 return -EINVAL;
1149 }
1150
Pablo Neira459aa662016-05-09 00:55:48 +02001151 rcu_read_lock();
Andreas Schultz3fb94612017-03-09 17:42:58 +01001152
1153 gtp = gtp_find_dev(sock_net(skb->sk), info->attrs);
1154 if (!gtp) {
1155 err = -ENODEV;
1156 goto err_unlock;
1157 }
1158
Pablo Neira459aa662016-05-09 00:55:48 +02001159 if (gtp_version == GTP_V0 &&
1160 info->attrs[GTPA_TID]) {
1161 u64 tid = nla_get_u64(info->attrs[GTPA_TID]);
1162
1163 pctx = gtp0_pdp_find(gtp, tid);
1164 } else if (gtp_version == GTP_V1 &&
1165 info->attrs[GTPA_I_TEI]) {
1166 u32 tid = nla_get_u32(info->attrs[GTPA_I_TEI]);
1167
1168 pctx = gtp1_pdp_find(gtp, tid);
1169 } else if (info->attrs[GTPA_MS_ADDRESS]) {
1170 __be32 ip = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
1171
1172 pctx = ipv4_pdp_find(gtp, ip);
1173 }
1174
1175 if (pctx == NULL) {
1176 err = -ENOENT;
1177 goto err_unlock;
1178 }
1179
1180 skb2 = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
1181 if (skb2 == NULL) {
1182 err = -ENOMEM;
1183 goto err_unlock;
1184 }
1185
1186 err = gtp_genl_fill_info(skb2, NETLINK_CB(skb).portid,
1187 info->snd_seq, info->nlhdr->nlmsg_type, pctx);
1188 if (err < 0)
1189 goto err_unlock_free;
1190
1191 rcu_read_unlock();
1192 return genlmsg_unicast(genl_info_net(info), skb2, info->snd_portid);
1193
1194err_unlock_free:
1195 kfree_skb(skb2);
1196err_unlock:
1197 rcu_read_unlock();
1198 return err;
1199}
1200
1201static int gtp_genl_dump_pdp(struct sk_buff *skb,
1202 struct netlink_callback *cb)
1203{
1204 struct gtp_dev *last_gtp = (struct gtp_dev *)cb->args[2], *gtp;
1205 struct net *net = sock_net(skb->sk);
1206 struct gtp_net *gn = net_generic(net, gtp_net_id);
1207 unsigned long tid = cb->args[1];
1208 int i, k = cb->args[0], ret;
1209 struct pdp_ctx *pctx;
1210
1211 if (cb->args[4])
1212 return 0;
1213
1214 list_for_each_entry_rcu(gtp, &gn->gtp_dev_list, list) {
1215 if (last_gtp && last_gtp != gtp)
1216 continue;
1217 else
1218 last_gtp = NULL;
1219
1220 for (i = k; i < gtp->hash_size; i++) {
1221 hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid) {
1222 if (tid && tid != pctx->u.tid)
1223 continue;
1224 else
1225 tid = 0;
1226
1227 ret = gtp_genl_fill_info(skb,
1228 NETLINK_CB(cb->skb).portid,
1229 cb->nlh->nlmsg_seq,
1230 cb->nlh->nlmsg_type, pctx);
1231 if (ret < 0) {
1232 cb->args[0] = i;
1233 cb->args[1] = pctx->u.tid;
1234 cb->args[2] = (unsigned long)gtp;
1235 goto out;
1236 }
1237 }
1238 }
1239 }
1240 cb->args[4] = 1;
1241out:
1242 return skb->len;
1243}
1244
1245static struct nla_policy gtp_genl_policy[GTPA_MAX + 1] = {
1246 [GTPA_LINK] = { .type = NLA_U32, },
1247 [GTPA_VERSION] = { .type = NLA_U32, },
1248 [GTPA_TID] = { .type = NLA_U64, },
1249 [GTPA_SGSN_ADDRESS] = { .type = NLA_U32, },
1250 [GTPA_MS_ADDRESS] = { .type = NLA_U32, },
1251 [GTPA_FLOW] = { .type = NLA_U16, },
1252 [GTPA_NET_NS_FD] = { .type = NLA_U32, },
1253 [GTPA_I_TEI] = { .type = NLA_U32, },
1254 [GTPA_O_TEI] = { .type = NLA_U32, },
1255};
1256
1257static const struct genl_ops gtp_genl_ops[] = {
1258 {
1259 .cmd = GTP_CMD_NEWPDP,
1260 .doit = gtp_genl_new_pdp,
1261 .policy = gtp_genl_policy,
1262 .flags = GENL_ADMIN_PERM,
1263 },
1264 {
1265 .cmd = GTP_CMD_DELPDP,
1266 .doit = gtp_genl_del_pdp,
1267 .policy = gtp_genl_policy,
1268 .flags = GENL_ADMIN_PERM,
1269 },
1270 {
1271 .cmd = GTP_CMD_GETPDP,
1272 .doit = gtp_genl_get_pdp,
1273 .dumpit = gtp_genl_dump_pdp,
1274 .policy = gtp_genl_policy,
1275 .flags = GENL_ADMIN_PERM,
1276 },
1277};
1278
Johannes Berg56989f62016-10-24 14:40:05 +02001279static struct genl_family gtp_genl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001280 .name = "gtp",
1281 .version = 0,
1282 .hdrsize = 0,
1283 .maxattr = GTPA_MAX,
1284 .netnsok = true,
1285 .module = THIS_MODULE,
1286 .ops = gtp_genl_ops,
1287 .n_ops = ARRAY_SIZE(gtp_genl_ops),
1288};
1289
Pablo Neira459aa662016-05-09 00:55:48 +02001290static int __net_init gtp_net_init(struct net *net)
1291{
1292 struct gtp_net *gn = net_generic(net, gtp_net_id);
1293
1294 INIT_LIST_HEAD(&gn->gtp_dev_list);
1295 return 0;
1296}
1297
1298static void __net_exit gtp_net_exit(struct net *net)
1299{
1300 struct gtp_net *gn = net_generic(net, gtp_net_id);
1301 struct gtp_dev *gtp;
1302 LIST_HEAD(list);
1303
1304 rtnl_lock();
1305 list_for_each_entry(gtp, &gn->gtp_dev_list, list)
1306 gtp_dellink(gtp->dev, &list);
1307
1308 unregister_netdevice_many(&list);
1309 rtnl_unlock();
1310}
1311
1312static struct pernet_operations gtp_net_ops = {
1313 .init = gtp_net_init,
1314 .exit = gtp_net_exit,
1315 .id = &gtp_net_id,
1316 .size = sizeof(struct gtp_net),
1317};
1318
1319static int __init gtp_init(void)
1320{
1321 int err;
1322
1323 get_random_bytes(&gtp_h_initval, sizeof(gtp_h_initval));
1324
1325 err = rtnl_link_register(&gtp_link_ops);
1326 if (err < 0)
1327 goto error_out;
1328
Johannes Berg489111e2016-10-24 14:40:03 +02001329 err = genl_register_family(&gtp_genl_family);
Pablo Neira459aa662016-05-09 00:55:48 +02001330 if (err < 0)
1331 goto unreg_rtnl_link;
1332
1333 err = register_pernet_subsys(&gtp_net_ops);
1334 if (err < 0)
1335 goto unreg_genl_family;
1336
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001337 pr_info("GTP module loaded (pdp ctx size %zd bytes)\n",
Pablo Neira459aa662016-05-09 00:55:48 +02001338 sizeof(struct pdp_ctx));
1339 return 0;
1340
1341unreg_genl_family:
1342 genl_unregister_family(&gtp_genl_family);
1343unreg_rtnl_link:
1344 rtnl_link_unregister(&gtp_link_ops);
1345error_out:
1346 pr_err("error loading GTP module loaded\n");
1347 return err;
1348}
1349late_initcall(gtp_init);
1350
1351static void __exit gtp_fini(void)
1352{
1353 unregister_pernet_subsys(&gtp_net_ops);
1354 genl_unregister_family(&gtp_genl_family);
1355 rtnl_link_unregister(&gtp_link_ops);
1356
1357 pr_info("GTP module unloaded\n");
1358}
1359module_exit(gtp_fini);
1360
1361MODULE_LICENSE("GPL");
1362MODULE_AUTHOR("Harald Welte <hwelte@sysmocom.de>");
1363MODULE_DESCRIPTION("Interface driver for GTP encapsulated traffic");
1364MODULE_ALIAS_RTNL_LINK("gtp");
Andreas Schultzab729822017-01-27 10:40:56 +01001365MODULE_ALIAS_GENL_FAMILY("gtp");