blob: 9aa2b355f2be0e1b2ade7046579fc01c13d5499d [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
747static struct net *gtp_genl_get_net(struct net *src_net, struct nlattr *tb[])
748{
749 struct net *net;
750
751 /* Examine the link attributes and figure out which network namespace
752 * we are talking about.
753 */
754 if (tb[GTPA_NET_NS_FD])
755 net = get_net_ns_by_fd(nla_get_u32(tb[GTPA_NET_NS_FD]));
756 else
757 net = get_net(src_net);
758
759 return net;
760}
761
762static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize)
763{
764 int i;
765
766 gtp->addr_hash = kmalloc(sizeof(struct hlist_head) * hsize, GFP_KERNEL);
767 if (gtp->addr_hash == NULL)
768 return -ENOMEM;
769
770 gtp->tid_hash = kmalloc(sizeof(struct hlist_head) * hsize, GFP_KERNEL);
771 if (gtp->tid_hash == NULL)
772 goto err1;
773
774 gtp->hash_size = hsize;
775
776 for (i = 0; i < hsize; i++) {
777 INIT_HLIST_HEAD(&gtp->addr_hash[i]);
778 INIT_HLIST_HEAD(&gtp->tid_hash[i]);
779 }
780 return 0;
781err1:
782 kfree(gtp->addr_hash);
783 return -ENOMEM;
784}
785
786static void gtp_hashtable_free(struct gtp_dev *gtp)
787{
788 struct pdp_ctx *pctx;
789 int i;
790
791 for (i = 0; i < gtp->hash_size; i++) {
792 hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid) {
793 hlist_del_rcu(&pctx->hlist_tid);
794 hlist_del_rcu(&pctx->hlist_addr);
795 kfree_rcu(pctx, rcu_head);
796 }
797 }
798 synchronize_rcu();
799 kfree(gtp->addr_hash);
800 kfree(gtp->tid_hash);
801}
802
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100803static struct sock *gtp_encap_enable_socket(int fd, int type,
804 struct gtp_dev *gtp)
Pablo Neira459aa662016-05-09 00:55:48 +0200805{
806 struct udp_tunnel_sock_cfg tuncfg = {NULL};
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100807 struct socket *sock;
808 struct sock *sk;
Pablo Neira459aa662016-05-09 00:55:48 +0200809 int err;
810
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100811 pr_debug("enable gtp on %d, %d\n", fd, type);
Pablo Neira459aa662016-05-09 00:55:48 +0200812
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100813 sock = sockfd_lookup(fd, &err);
814 if (!sock) {
815 pr_debug("gtp socket fd=%d not found\n", fd);
816 return NULL;
Pablo Neira459aa662016-05-09 00:55:48 +0200817 }
818
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100819 if (sock->sk->sk_protocol != IPPROTO_UDP) {
820 pr_debug("socket fd=%d not UDP\n", fd);
821 sk = ERR_PTR(-EINVAL);
822 goto out_sock;
Pablo Neira459aa662016-05-09 00:55:48 +0200823 }
824
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100825 if (rcu_dereference_sk_user_data(sock->sk)) {
826 sk = ERR_PTR(-EBUSY);
827 goto out_sock;
Pablo Neira459aa662016-05-09 00:55:48 +0200828 }
829
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100830 sk = sock->sk;
831 sock_hold(sk);
Pablo Neira459aa662016-05-09 00:55:48 +0200832
833 tuncfg.sk_user_data = gtp;
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100834 tuncfg.encap_type = type;
Pablo Neira459aa662016-05-09 00:55:48 +0200835 tuncfg.encap_rcv = gtp_encap_recv;
836 tuncfg.encap_destroy = gtp_encap_destroy;
837
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100838 setup_udp_tunnel_sock(sock_net(sock->sk), sock, &tuncfg);
Pablo Neira459aa662016-05-09 00:55:48 +0200839
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100840out_sock:
841 sockfd_put(sock);
842 return sk;
843}
Pablo Neira459aa662016-05-09 00:55:48 +0200844
Andreas Schultz1e3a3ab2017-03-09 17:42:57 +0100845static int gtp_encap_enable(struct gtp_dev *gtp, struct nlattr *data[])
846{
847 struct sock *sk1u = NULL;
848 struct sock *sk0 = NULL;
849
850 if (data[IFLA_GTP_FD0]) {
851 u32 fd0 = nla_get_u32(data[IFLA_GTP_FD0]);
852
853 sk0 = gtp_encap_enable_socket(fd0, UDP_ENCAP_GTP0, gtp);
854 if (IS_ERR(sk0))
855 return PTR_ERR(sk0);
856 }
857
858 if (data[IFLA_GTP_FD1]) {
859 u32 fd1 = nla_get_u32(data[IFLA_GTP_FD1]);
860
861 sk1u = gtp_encap_enable_socket(fd1, UDP_ENCAP_GTP1U, gtp);
862 if (IS_ERR(sk1u)) {
863 if (sk0)
864 gtp_encap_disable_sock(sk0);
865 return PTR_ERR(sk1u);
866 }
867 }
868
869 gtp->sk0 = sk0;
870 gtp->sk1u = sk1u;
871
872 return 0;
Pablo Neira459aa662016-05-09 00:55:48 +0200873}
874
875static struct net_device *gtp_find_dev(struct net *net, int ifindex)
876{
877 struct gtp_net *gn = net_generic(net, gtp_net_id);
878 struct gtp_dev *gtp;
879
880 list_for_each_entry_rcu(gtp, &gn->gtp_dev_list, list) {
881 if (ifindex == gtp->dev->ifindex)
882 return gtp->dev;
883 }
884 return NULL;
885}
886
887static void ipv4_pdp_fill(struct pdp_ctx *pctx, struct genl_info *info)
888{
889 pctx->gtp_version = nla_get_u32(info->attrs[GTPA_VERSION]);
890 pctx->af = AF_INET;
891 pctx->sgsn_addr_ip4.s_addr =
892 nla_get_be32(info->attrs[GTPA_SGSN_ADDRESS]);
893 pctx->ms_addr_ip4.s_addr =
894 nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
895
896 switch (pctx->gtp_version) {
897 case GTP_V0:
898 /* According to TS 09.60, sections 7.5.1 and 7.5.2, the flow
899 * label needs to be the same for uplink and downlink packets,
900 * so let's annotate this.
901 */
902 pctx->u.v0.tid = nla_get_u64(info->attrs[GTPA_TID]);
903 pctx->u.v0.flow = nla_get_u16(info->attrs[GTPA_FLOW]);
904 break;
905 case GTP_V1:
906 pctx->u.v1.i_tei = nla_get_u32(info->attrs[GTPA_I_TEI]);
907 pctx->u.v1.o_tei = nla_get_u32(info->attrs[GTPA_O_TEI]);
908 break;
909 default:
910 break;
911 }
912}
913
914static int ipv4_pdp_add(struct net_device *dev, struct genl_info *info)
915{
916 struct gtp_dev *gtp = netdev_priv(dev);
917 u32 hash_ms, hash_tid = 0;
918 struct pdp_ctx *pctx;
919 bool found = false;
920 __be32 ms_addr;
921
922 ms_addr = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
923 hash_ms = ipv4_hashfn(ms_addr) % gtp->hash_size;
924
925 hlist_for_each_entry_rcu(pctx, &gtp->addr_hash[hash_ms], hlist_addr) {
926 if (pctx->ms_addr_ip4.s_addr == ms_addr) {
927 found = true;
928 break;
929 }
930 }
931
932 if (found) {
933 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
934 return -EEXIST;
935 if (info->nlhdr->nlmsg_flags & NLM_F_REPLACE)
936 return -EOPNOTSUPP;
937
938 ipv4_pdp_fill(pctx, info);
939
940 if (pctx->gtp_version == GTP_V0)
941 netdev_dbg(dev, "GTPv0-U: update tunnel id = %llx (pdp %p)\n",
942 pctx->u.v0.tid, pctx);
943 else if (pctx->gtp_version == GTP_V1)
944 netdev_dbg(dev, "GTPv1-U: update tunnel id = %x/%x (pdp %p)\n",
945 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
946
947 return 0;
948
949 }
950
951 pctx = kmalloc(sizeof(struct pdp_ctx), GFP_KERNEL);
952 if (pctx == NULL)
953 return -ENOMEM;
954
955 ipv4_pdp_fill(pctx, info);
956 atomic_set(&pctx->tx_seq, 0);
957
958 switch (pctx->gtp_version) {
959 case GTP_V0:
960 /* TS 09.60: "The flow label identifies unambiguously a GTP
961 * flow.". We use the tid for this instead, I cannot find a
962 * situation in which this doesn't unambiguosly identify the
963 * PDP context.
964 */
965 hash_tid = gtp0_hashfn(pctx->u.v0.tid) % gtp->hash_size;
966 break;
967 case GTP_V1:
968 hash_tid = gtp1u_hashfn(pctx->u.v1.i_tei) % gtp->hash_size;
969 break;
970 }
971
972 hlist_add_head_rcu(&pctx->hlist_addr, &gtp->addr_hash[hash_ms]);
973 hlist_add_head_rcu(&pctx->hlist_tid, &gtp->tid_hash[hash_tid]);
974
975 switch (pctx->gtp_version) {
976 case GTP_V0:
977 netdev_dbg(dev, "GTPv0-U: new PDP ctx id=%llx ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
978 pctx->u.v0.tid, &pctx->sgsn_addr_ip4,
979 &pctx->ms_addr_ip4, pctx);
980 break;
981 case GTP_V1:
982 netdev_dbg(dev, "GTPv1-U: new PDP ctx id=%x/%x ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
983 pctx->u.v1.i_tei, pctx->u.v1.o_tei,
984 &pctx->sgsn_addr_ip4, &pctx->ms_addr_ip4, pctx);
985 break;
986 }
987
988 return 0;
989}
990
991static int gtp_genl_new_pdp(struct sk_buff *skb, struct genl_info *info)
992{
993 struct net_device *dev;
994 struct net *net;
995
996 if (!info->attrs[GTPA_VERSION] ||
997 !info->attrs[GTPA_LINK] ||
998 !info->attrs[GTPA_SGSN_ADDRESS] ||
999 !info->attrs[GTPA_MS_ADDRESS])
1000 return -EINVAL;
1001
1002 switch (nla_get_u32(info->attrs[GTPA_VERSION])) {
1003 case GTP_V0:
1004 if (!info->attrs[GTPA_TID] ||
1005 !info->attrs[GTPA_FLOW])
1006 return -EINVAL;
1007 break;
1008 case GTP_V1:
1009 if (!info->attrs[GTPA_I_TEI] ||
1010 !info->attrs[GTPA_O_TEI])
1011 return -EINVAL;
1012 break;
1013
1014 default:
1015 return -EINVAL;
1016 }
1017
1018 net = gtp_genl_get_net(sock_net(skb->sk), info->attrs);
1019 if (IS_ERR(net))
1020 return PTR_ERR(net);
1021
1022 /* Check if there's an existing gtpX device to configure */
1023 dev = gtp_find_dev(net, nla_get_u32(info->attrs[GTPA_LINK]));
Pablo Neira27ee4412016-05-12 17:16:31 +02001024 if (dev == NULL) {
1025 put_net(net);
Pablo Neira459aa662016-05-09 00:55:48 +02001026 return -ENODEV;
Pablo Neira27ee4412016-05-12 17:16:31 +02001027 }
1028 put_net(net);
Pablo Neira459aa662016-05-09 00:55:48 +02001029
1030 return ipv4_pdp_add(dev, info);
1031}
1032
1033static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info)
1034{
1035 struct net_device *dev;
1036 struct pdp_ctx *pctx;
1037 struct gtp_dev *gtp;
1038 struct net *net;
1039
1040 if (!info->attrs[GTPA_VERSION] ||
1041 !info->attrs[GTPA_LINK])
1042 return -EINVAL;
1043
1044 net = gtp_genl_get_net(sock_net(skb->sk), info->attrs);
1045 if (IS_ERR(net))
1046 return PTR_ERR(net);
1047
1048 /* Check if there's an existing gtpX device to configure */
1049 dev = gtp_find_dev(net, nla_get_u32(info->attrs[GTPA_LINK]));
Pablo Neira27ee4412016-05-12 17:16:31 +02001050 if (dev == NULL) {
1051 put_net(net);
Pablo Neira459aa662016-05-09 00:55:48 +02001052 return -ENODEV;
Pablo Neira27ee4412016-05-12 17:16:31 +02001053 }
1054 put_net(net);
Pablo Neira459aa662016-05-09 00:55:48 +02001055
1056 gtp = netdev_priv(dev);
1057
1058 switch (nla_get_u32(info->attrs[GTPA_VERSION])) {
1059 case GTP_V0:
1060 if (!info->attrs[GTPA_TID])
1061 return -EINVAL;
1062 pctx = gtp0_pdp_find(gtp, nla_get_u64(info->attrs[GTPA_TID]));
1063 break;
1064 case GTP_V1:
1065 if (!info->attrs[GTPA_I_TEI])
1066 return -EINVAL;
1067 pctx = gtp1_pdp_find(gtp, nla_get_u64(info->attrs[GTPA_I_TEI]));
1068 break;
1069
1070 default:
1071 return -EINVAL;
1072 }
1073
1074 if (pctx == NULL)
1075 return -ENOENT;
1076
1077 if (pctx->gtp_version == GTP_V0)
1078 netdev_dbg(dev, "GTPv0-U: deleting tunnel id = %llx (pdp %p)\n",
1079 pctx->u.v0.tid, pctx);
1080 else if (pctx->gtp_version == GTP_V1)
1081 netdev_dbg(dev, "GTPv1-U: deleting tunnel id = %x/%x (pdp %p)\n",
1082 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
1083
1084 hlist_del_rcu(&pctx->hlist_tid);
1085 hlist_del_rcu(&pctx->hlist_addr);
1086 kfree_rcu(pctx, rcu_head);
1087
1088 return 0;
1089}
1090
Johannes Berg489111e2016-10-24 14:40:03 +02001091static struct genl_family gtp_genl_family;
Pablo Neira459aa662016-05-09 00:55:48 +02001092
1093static int gtp_genl_fill_info(struct sk_buff *skb, u32 snd_portid, u32 snd_seq,
1094 u32 type, struct pdp_ctx *pctx)
1095{
1096 void *genlh;
1097
1098 genlh = genlmsg_put(skb, snd_portid, snd_seq, &gtp_genl_family, 0,
1099 type);
1100 if (genlh == NULL)
1101 goto nlmsg_failure;
1102
1103 if (nla_put_u32(skb, GTPA_VERSION, pctx->gtp_version) ||
1104 nla_put_be32(skb, GTPA_SGSN_ADDRESS, pctx->sgsn_addr_ip4.s_addr) ||
1105 nla_put_be32(skb, GTPA_MS_ADDRESS, pctx->ms_addr_ip4.s_addr))
1106 goto nla_put_failure;
1107
1108 switch (pctx->gtp_version) {
1109 case GTP_V0:
1110 if (nla_put_u64_64bit(skb, GTPA_TID, pctx->u.v0.tid, GTPA_PAD) ||
1111 nla_put_u16(skb, GTPA_FLOW, pctx->u.v0.flow))
1112 goto nla_put_failure;
1113 break;
1114 case GTP_V1:
1115 if (nla_put_u32(skb, GTPA_I_TEI, pctx->u.v1.i_tei) ||
1116 nla_put_u32(skb, GTPA_O_TEI, pctx->u.v1.o_tei))
1117 goto nla_put_failure;
1118 break;
1119 }
1120 genlmsg_end(skb, genlh);
1121 return 0;
1122
1123nlmsg_failure:
1124nla_put_failure:
1125 genlmsg_cancel(skb, genlh);
1126 return -EMSGSIZE;
1127}
1128
1129static int gtp_genl_get_pdp(struct sk_buff *skb, struct genl_info *info)
1130{
1131 struct pdp_ctx *pctx = NULL;
1132 struct net_device *dev;
1133 struct sk_buff *skb2;
1134 struct gtp_dev *gtp;
1135 u32 gtp_version;
1136 struct net *net;
1137 int err;
1138
1139 if (!info->attrs[GTPA_VERSION] ||
1140 !info->attrs[GTPA_LINK])
1141 return -EINVAL;
1142
1143 gtp_version = nla_get_u32(info->attrs[GTPA_VERSION]);
1144 switch (gtp_version) {
1145 case GTP_V0:
1146 case GTP_V1:
1147 break;
1148 default:
1149 return -EINVAL;
1150 }
1151
1152 net = gtp_genl_get_net(sock_net(skb->sk), info->attrs);
1153 if (IS_ERR(net))
1154 return PTR_ERR(net);
1155
1156 /* Check if there's an existing gtpX device to configure */
1157 dev = gtp_find_dev(net, nla_get_u32(info->attrs[GTPA_LINK]));
Pablo Neira27ee4412016-05-12 17:16:31 +02001158 if (dev == NULL) {
1159 put_net(net);
Pablo Neira459aa662016-05-09 00:55:48 +02001160 return -ENODEV;
Pablo Neira27ee4412016-05-12 17:16:31 +02001161 }
1162 put_net(net);
Pablo Neira459aa662016-05-09 00:55:48 +02001163
1164 gtp = netdev_priv(dev);
1165
1166 rcu_read_lock();
1167 if (gtp_version == GTP_V0 &&
1168 info->attrs[GTPA_TID]) {
1169 u64 tid = nla_get_u64(info->attrs[GTPA_TID]);
1170
1171 pctx = gtp0_pdp_find(gtp, tid);
1172 } else if (gtp_version == GTP_V1 &&
1173 info->attrs[GTPA_I_TEI]) {
1174 u32 tid = nla_get_u32(info->attrs[GTPA_I_TEI]);
1175
1176 pctx = gtp1_pdp_find(gtp, tid);
1177 } else if (info->attrs[GTPA_MS_ADDRESS]) {
1178 __be32 ip = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
1179
1180 pctx = ipv4_pdp_find(gtp, ip);
1181 }
1182
1183 if (pctx == NULL) {
1184 err = -ENOENT;
1185 goto err_unlock;
1186 }
1187
1188 skb2 = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
1189 if (skb2 == NULL) {
1190 err = -ENOMEM;
1191 goto err_unlock;
1192 }
1193
1194 err = gtp_genl_fill_info(skb2, NETLINK_CB(skb).portid,
1195 info->snd_seq, info->nlhdr->nlmsg_type, pctx);
1196 if (err < 0)
1197 goto err_unlock_free;
1198
1199 rcu_read_unlock();
1200 return genlmsg_unicast(genl_info_net(info), skb2, info->snd_portid);
1201
1202err_unlock_free:
1203 kfree_skb(skb2);
1204err_unlock:
1205 rcu_read_unlock();
1206 return err;
1207}
1208
1209static int gtp_genl_dump_pdp(struct sk_buff *skb,
1210 struct netlink_callback *cb)
1211{
1212 struct gtp_dev *last_gtp = (struct gtp_dev *)cb->args[2], *gtp;
1213 struct net *net = sock_net(skb->sk);
1214 struct gtp_net *gn = net_generic(net, gtp_net_id);
1215 unsigned long tid = cb->args[1];
1216 int i, k = cb->args[0], ret;
1217 struct pdp_ctx *pctx;
1218
1219 if (cb->args[4])
1220 return 0;
1221
1222 list_for_each_entry_rcu(gtp, &gn->gtp_dev_list, list) {
1223 if (last_gtp && last_gtp != gtp)
1224 continue;
1225 else
1226 last_gtp = NULL;
1227
1228 for (i = k; i < gtp->hash_size; i++) {
1229 hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid) {
1230 if (tid && tid != pctx->u.tid)
1231 continue;
1232 else
1233 tid = 0;
1234
1235 ret = gtp_genl_fill_info(skb,
1236 NETLINK_CB(cb->skb).portid,
1237 cb->nlh->nlmsg_seq,
1238 cb->nlh->nlmsg_type, pctx);
1239 if (ret < 0) {
1240 cb->args[0] = i;
1241 cb->args[1] = pctx->u.tid;
1242 cb->args[2] = (unsigned long)gtp;
1243 goto out;
1244 }
1245 }
1246 }
1247 }
1248 cb->args[4] = 1;
1249out:
1250 return skb->len;
1251}
1252
1253static struct nla_policy gtp_genl_policy[GTPA_MAX + 1] = {
1254 [GTPA_LINK] = { .type = NLA_U32, },
1255 [GTPA_VERSION] = { .type = NLA_U32, },
1256 [GTPA_TID] = { .type = NLA_U64, },
1257 [GTPA_SGSN_ADDRESS] = { .type = NLA_U32, },
1258 [GTPA_MS_ADDRESS] = { .type = NLA_U32, },
1259 [GTPA_FLOW] = { .type = NLA_U16, },
1260 [GTPA_NET_NS_FD] = { .type = NLA_U32, },
1261 [GTPA_I_TEI] = { .type = NLA_U32, },
1262 [GTPA_O_TEI] = { .type = NLA_U32, },
1263};
1264
1265static const struct genl_ops gtp_genl_ops[] = {
1266 {
1267 .cmd = GTP_CMD_NEWPDP,
1268 .doit = gtp_genl_new_pdp,
1269 .policy = gtp_genl_policy,
1270 .flags = GENL_ADMIN_PERM,
1271 },
1272 {
1273 .cmd = GTP_CMD_DELPDP,
1274 .doit = gtp_genl_del_pdp,
1275 .policy = gtp_genl_policy,
1276 .flags = GENL_ADMIN_PERM,
1277 },
1278 {
1279 .cmd = GTP_CMD_GETPDP,
1280 .doit = gtp_genl_get_pdp,
1281 .dumpit = gtp_genl_dump_pdp,
1282 .policy = gtp_genl_policy,
1283 .flags = GENL_ADMIN_PERM,
1284 },
1285};
1286
Johannes Berg56989f62016-10-24 14:40:05 +02001287static struct genl_family gtp_genl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +02001288 .name = "gtp",
1289 .version = 0,
1290 .hdrsize = 0,
1291 .maxattr = GTPA_MAX,
1292 .netnsok = true,
1293 .module = THIS_MODULE,
1294 .ops = gtp_genl_ops,
1295 .n_ops = ARRAY_SIZE(gtp_genl_ops),
1296};
1297
Pablo Neira459aa662016-05-09 00:55:48 +02001298static int __net_init gtp_net_init(struct net *net)
1299{
1300 struct gtp_net *gn = net_generic(net, gtp_net_id);
1301
1302 INIT_LIST_HEAD(&gn->gtp_dev_list);
1303 return 0;
1304}
1305
1306static void __net_exit gtp_net_exit(struct net *net)
1307{
1308 struct gtp_net *gn = net_generic(net, gtp_net_id);
1309 struct gtp_dev *gtp;
1310 LIST_HEAD(list);
1311
1312 rtnl_lock();
1313 list_for_each_entry(gtp, &gn->gtp_dev_list, list)
1314 gtp_dellink(gtp->dev, &list);
1315
1316 unregister_netdevice_many(&list);
1317 rtnl_unlock();
1318}
1319
1320static struct pernet_operations gtp_net_ops = {
1321 .init = gtp_net_init,
1322 .exit = gtp_net_exit,
1323 .id = &gtp_net_id,
1324 .size = sizeof(struct gtp_net),
1325};
1326
1327static int __init gtp_init(void)
1328{
1329 int err;
1330
1331 get_random_bytes(&gtp_h_initval, sizeof(gtp_h_initval));
1332
1333 err = rtnl_link_register(&gtp_link_ops);
1334 if (err < 0)
1335 goto error_out;
1336
Johannes Berg489111e2016-10-24 14:40:03 +02001337 err = genl_register_family(&gtp_genl_family);
Pablo Neira459aa662016-05-09 00:55:48 +02001338 if (err < 0)
1339 goto unreg_rtnl_link;
1340
1341 err = register_pernet_subsys(&gtp_net_ops);
1342 if (err < 0)
1343 goto unreg_genl_family;
1344
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08001345 pr_info("GTP module loaded (pdp ctx size %zd bytes)\n",
Pablo Neira459aa662016-05-09 00:55:48 +02001346 sizeof(struct pdp_ctx));
1347 return 0;
1348
1349unreg_genl_family:
1350 genl_unregister_family(&gtp_genl_family);
1351unreg_rtnl_link:
1352 rtnl_link_unregister(&gtp_link_ops);
1353error_out:
1354 pr_err("error loading GTP module loaded\n");
1355 return err;
1356}
1357late_initcall(gtp_init);
1358
1359static void __exit gtp_fini(void)
1360{
1361 unregister_pernet_subsys(&gtp_net_ops);
1362 genl_unregister_family(&gtp_genl_family);
1363 rtnl_link_unregister(&gtp_link_ops);
1364
1365 pr_info("GTP module unloaded\n");
1366}
1367module_exit(gtp_fini);
1368
1369MODULE_LICENSE("GPL");
1370MODULE_AUTHOR("Harald Welte <hwelte@sysmocom.de>");
1371MODULE_DESCRIPTION("Interface driver for GTP encapsulated traffic");
1372MODULE_ALIAS_RTNL_LINK("gtp");
Andreas Schultzab729822017-01-27 10:40:56 +01001373MODULE_ALIAS_GENL_FAMILY("gtp");