blob: e1b5af377fbcda6a875d014ec2692e506e6be7b8 [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
262static void gtp_encap_disable(struct gtp_dev *gtp)
263{
Andreas Schultz17886c472017-03-09 17:42:56 +0100264 if (gtp->sk0) {
265 udp_sk(gtp->sk0)->encap_type = 0;
266 rcu_assign_sk_user_data(gtp->sk0, NULL);
267 sock_put(gtp->sk0);
Pablo Neira459aa662016-05-09 00:55:48 +0200268 }
Andreas Schultz17886c472017-03-09 17:42:56 +0100269 if (gtp->sk1u) {
270 udp_sk(gtp->sk1u)->encap_type = 0;
271 rcu_assign_sk_user_data(gtp->sk1u, NULL);
272 sock_put(gtp->sk1u);
Pablo Neira459aa662016-05-09 00:55:48 +0200273 }
274
Andreas Schultz17886c472017-03-09 17:42:56 +0100275 gtp->sk0 = NULL;
276 gtp->sk1u = NULL;
Pablo Neira459aa662016-05-09 00:55:48 +0200277}
278
279static void gtp_encap_destroy(struct sock *sk)
280{
281 struct gtp_dev *gtp;
282
283 gtp = rcu_dereference_sk_user_data(sk);
284 if (gtp)
285 gtp_encap_disable(gtp);
286}
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);
645static int gtp_encap_enable(struct net_device *dev, struct gtp_dev *gtp,
Andreas Schultz3ab1b462017-01-27 10:40:58 +0100646 int fd_gtp0, int fd_gtp1);
Pablo Neira459aa662016-05-09 00:55:48 +0200647
648static int gtp_newlink(struct net *src_net, struct net_device *dev,
649 struct nlattr *tb[], struct nlattr *data[])
650{
651 int hashsize, err, fd0, fd1;
652 struct gtp_dev *gtp;
653 struct gtp_net *gn;
654
655 if (!data[IFLA_GTP_FD0] || !data[IFLA_GTP_FD1])
656 return -EINVAL;
657
658 gtp = netdev_priv(dev);
659
660 fd0 = nla_get_u32(data[IFLA_GTP_FD0]);
661 fd1 = nla_get_u32(data[IFLA_GTP_FD1]);
662
Andreas Schultz3ab1b462017-01-27 10:40:58 +0100663 err = gtp_encap_enable(dev, gtp, fd0, fd1);
Pablo Neira459aa662016-05-09 00:55:48 +0200664 if (err < 0)
665 goto out_err;
666
667 if (!data[IFLA_GTP_PDP_HASHSIZE])
668 hashsize = 1024;
669 else
670 hashsize = nla_get_u32(data[IFLA_GTP_PDP_HASHSIZE]);
671
672 err = gtp_hashtable_new(gtp, hashsize);
673 if (err < 0)
674 goto out_encap;
675
676 err = register_netdevice(dev);
677 if (err < 0) {
678 netdev_dbg(dev, "failed to register new netdev %d\n", err);
679 goto out_hashtable;
680 }
681
682 gn = net_generic(dev_net(dev), gtp_net_id);
683 list_add_rcu(&gtp->list, &gn->gtp_dev_list);
684
685 netdev_dbg(dev, "registered new GTP interface\n");
686
687 return 0;
688
689out_hashtable:
690 gtp_hashtable_free(gtp);
691out_encap:
692 gtp_encap_disable(gtp);
693out_err:
694 return err;
695}
696
697static void gtp_dellink(struct net_device *dev, struct list_head *head)
698{
699 struct gtp_dev *gtp = netdev_priv(dev);
700
701 gtp_encap_disable(gtp);
702 gtp_hashtable_free(gtp);
703 list_del_rcu(&gtp->list);
704 unregister_netdevice_queue(dev, head);
705}
706
707static const struct nla_policy gtp_policy[IFLA_GTP_MAX + 1] = {
708 [IFLA_GTP_FD0] = { .type = NLA_U32 },
709 [IFLA_GTP_FD1] = { .type = NLA_U32 },
710 [IFLA_GTP_PDP_HASHSIZE] = { .type = NLA_U32 },
711};
712
713static int gtp_validate(struct nlattr *tb[], struct nlattr *data[])
714{
715 if (!data)
716 return -EINVAL;
717
718 return 0;
719}
720
721static size_t gtp_get_size(const struct net_device *dev)
722{
723 return nla_total_size(sizeof(__u32)); /* IFLA_GTP_PDP_HASHSIZE */
724}
725
726static int gtp_fill_info(struct sk_buff *skb, const struct net_device *dev)
727{
728 struct gtp_dev *gtp = netdev_priv(dev);
729
730 if (nla_put_u32(skb, IFLA_GTP_PDP_HASHSIZE, gtp->hash_size))
731 goto nla_put_failure;
732
733 return 0;
734
735nla_put_failure:
736 return -EMSGSIZE;
737}
738
739static struct rtnl_link_ops gtp_link_ops __read_mostly = {
740 .kind = "gtp",
741 .maxtype = IFLA_GTP_MAX,
742 .policy = gtp_policy,
743 .priv_size = sizeof(struct gtp_dev),
744 .setup = gtp_link_setup,
745 .validate = gtp_validate,
746 .newlink = gtp_newlink,
747 .dellink = gtp_dellink,
748 .get_size = gtp_get_size,
749 .fill_info = gtp_fill_info,
750};
751
752static struct net *gtp_genl_get_net(struct net *src_net, struct nlattr *tb[])
753{
754 struct net *net;
755
756 /* Examine the link attributes and figure out which network namespace
757 * we are talking about.
758 */
759 if (tb[GTPA_NET_NS_FD])
760 net = get_net_ns_by_fd(nla_get_u32(tb[GTPA_NET_NS_FD]));
761 else
762 net = get_net(src_net);
763
764 return net;
765}
766
767static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize)
768{
769 int i;
770
771 gtp->addr_hash = kmalloc(sizeof(struct hlist_head) * hsize, GFP_KERNEL);
772 if (gtp->addr_hash == NULL)
773 return -ENOMEM;
774
775 gtp->tid_hash = kmalloc(sizeof(struct hlist_head) * hsize, GFP_KERNEL);
776 if (gtp->tid_hash == NULL)
777 goto err1;
778
779 gtp->hash_size = hsize;
780
781 for (i = 0; i < hsize; i++) {
782 INIT_HLIST_HEAD(&gtp->addr_hash[i]);
783 INIT_HLIST_HEAD(&gtp->tid_hash[i]);
784 }
785 return 0;
786err1:
787 kfree(gtp->addr_hash);
788 return -ENOMEM;
789}
790
791static void gtp_hashtable_free(struct gtp_dev *gtp)
792{
793 struct pdp_ctx *pctx;
794 int i;
795
796 for (i = 0; i < gtp->hash_size; i++) {
797 hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid) {
798 hlist_del_rcu(&pctx->hlist_tid);
799 hlist_del_rcu(&pctx->hlist_addr);
800 kfree_rcu(pctx, rcu_head);
801 }
802 }
803 synchronize_rcu();
804 kfree(gtp->addr_hash);
805 kfree(gtp->tid_hash);
806}
807
808static int gtp_encap_enable(struct net_device *dev, struct gtp_dev *gtp,
Andreas Schultz3ab1b462017-01-27 10:40:58 +0100809 int fd_gtp0, int fd_gtp1)
Pablo Neira459aa662016-05-09 00:55:48 +0200810{
811 struct udp_tunnel_sock_cfg tuncfg = {NULL};
812 struct socket *sock0, *sock1u;
813 int err;
814
815 netdev_dbg(dev, "enable gtp on %d, %d\n", fd_gtp0, fd_gtp1);
816
817 sock0 = sockfd_lookup(fd_gtp0, &err);
818 if (sock0 == NULL) {
819 netdev_dbg(dev, "socket fd=%d not found (gtp0)\n", fd_gtp0);
820 return -ENOENT;
821 }
822
823 if (sock0->sk->sk_protocol != IPPROTO_UDP) {
824 netdev_dbg(dev, "socket fd=%d not UDP\n", fd_gtp0);
825 err = -EINVAL;
826 goto err1;
827 }
828
829 sock1u = sockfd_lookup(fd_gtp1, &err);
830 if (sock1u == NULL) {
831 netdev_dbg(dev, "socket fd=%d not found (gtp1u)\n", fd_gtp1);
832 err = -ENOENT;
833 goto err1;
834 }
835
836 if (sock1u->sk->sk_protocol != IPPROTO_UDP) {
837 netdev_dbg(dev, "socket fd=%d not UDP\n", fd_gtp1);
838 err = -EINVAL;
839 goto err2;
840 }
841
842 netdev_dbg(dev, "enable gtp on %p, %p\n", sock0, sock1u);
843
Andreas Schultz17886c472017-03-09 17:42:56 +0100844 sock_hold(sock0->sk);
845 gtp->sk0 = sock0->sk;
846 sock_hold(sock1u->sk);
847 gtp->sk1u = sock1u->sk;
Pablo Neira459aa662016-05-09 00:55:48 +0200848
849 tuncfg.sk_user_data = gtp;
850 tuncfg.encap_rcv = gtp_encap_recv;
851 tuncfg.encap_destroy = gtp_encap_destroy;
852
853 tuncfg.encap_type = UDP_ENCAP_GTP0;
Andreas Schultz17886c472017-03-09 17:42:56 +0100854 setup_udp_tunnel_sock(sock_net(gtp->sk0), sock0, &tuncfg);
Pablo Neira459aa662016-05-09 00:55:48 +0200855
856 tuncfg.encap_type = UDP_ENCAP_GTP1U;
Andreas Schultz17886c472017-03-09 17:42:56 +0100857 setup_udp_tunnel_sock(sock_net(gtp->sk1u), sock1u, &tuncfg);
Pablo Neira459aa662016-05-09 00:55:48 +0200858
859 err = 0;
860err2:
861 sockfd_put(sock1u);
862err1:
863 sockfd_put(sock0);
864 return err;
865}
866
867static struct net_device *gtp_find_dev(struct net *net, int ifindex)
868{
869 struct gtp_net *gn = net_generic(net, gtp_net_id);
870 struct gtp_dev *gtp;
871
872 list_for_each_entry_rcu(gtp, &gn->gtp_dev_list, list) {
873 if (ifindex == gtp->dev->ifindex)
874 return gtp->dev;
875 }
876 return NULL;
877}
878
879static void ipv4_pdp_fill(struct pdp_ctx *pctx, struct genl_info *info)
880{
881 pctx->gtp_version = nla_get_u32(info->attrs[GTPA_VERSION]);
882 pctx->af = AF_INET;
883 pctx->sgsn_addr_ip4.s_addr =
884 nla_get_be32(info->attrs[GTPA_SGSN_ADDRESS]);
885 pctx->ms_addr_ip4.s_addr =
886 nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
887
888 switch (pctx->gtp_version) {
889 case GTP_V0:
890 /* According to TS 09.60, sections 7.5.1 and 7.5.2, the flow
891 * label needs to be the same for uplink and downlink packets,
892 * so let's annotate this.
893 */
894 pctx->u.v0.tid = nla_get_u64(info->attrs[GTPA_TID]);
895 pctx->u.v0.flow = nla_get_u16(info->attrs[GTPA_FLOW]);
896 break;
897 case GTP_V1:
898 pctx->u.v1.i_tei = nla_get_u32(info->attrs[GTPA_I_TEI]);
899 pctx->u.v1.o_tei = nla_get_u32(info->attrs[GTPA_O_TEI]);
900 break;
901 default:
902 break;
903 }
904}
905
906static int ipv4_pdp_add(struct net_device *dev, struct genl_info *info)
907{
908 struct gtp_dev *gtp = netdev_priv(dev);
909 u32 hash_ms, hash_tid = 0;
910 struct pdp_ctx *pctx;
911 bool found = false;
912 __be32 ms_addr;
913
914 ms_addr = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
915 hash_ms = ipv4_hashfn(ms_addr) % gtp->hash_size;
916
917 hlist_for_each_entry_rcu(pctx, &gtp->addr_hash[hash_ms], hlist_addr) {
918 if (pctx->ms_addr_ip4.s_addr == ms_addr) {
919 found = true;
920 break;
921 }
922 }
923
924 if (found) {
925 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
926 return -EEXIST;
927 if (info->nlhdr->nlmsg_flags & NLM_F_REPLACE)
928 return -EOPNOTSUPP;
929
930 ipv4_pdp_fill(pctx, info);
931
932 if (pctx->gtp_version == GTP_V0)
933 netdev_dbg(dev, "GTPv0-U: update tunnel id = %llx (pdp %p)\n",
934 pctx->u.v0.tid, pctx);
935 else if (pctx->gtp_version == GTP_V1)
936 netdev_dbg(dev, "GTPv1-U: update tunnel id = %x/%x (pdp %p)\n",
937 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
938
939 return 0;
940
941 }
942
943 pctx = kmalloc(sizeof(struct pdp_ctx), GFP_KERNEL);
944 if (pctx == NULL)
945 return -ENOMEM;
946
947 ipv4_pdp_fill(pctx, info);
948 atomic_set(&pctx->tx_seq, 0);
949
950 switch (pctx->gtp_version) {
951 case GTP_V0:
952 /* TS 09.60: "The flow label identifies unambiguously a GTP
953 * flow.". We use the tid for this instead, I cannot find a
954 * situation in which this doesn't unambiguosly identify the
955 * PDP context.
956 */
957 hash_tid = gtp0_hashfn(pctx->u.v0.tid) % gtp->hash_size;
958 break;
959 case GTP_V1:
960 hash_tid = gtp1u_hashfn(pctx->u.v1.i_tei) % gtp->hash_size;
961 break;
962 }
963
964 hlist_add_head_rcu(&pctx->hlist_addr, &gtp->addr_hash[hash_ms]);
965 hlist_add_head_rcu(&pctx->hlist_tid, &gtp->tid_hash[hash_tid]);
966
967 switch (pctx->gtp_version) {
968 case GTP_V0:
969 netdev_dbg(dev, "GTPv0-U: new PDP ctx id=%llx ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
970 pctx->u.v0.tid, &pctx->sgsn_addr_ip4,
971 &pctx->ms_addr_ip4, pctx);
972 break;
973 case GTP_V1:
974 netdev_dbg(dev, "GTPv1-U: new PDP ctx id=%x/%x ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
975 pctx->u.v1.i_tei, pctx->u.v1.o_tei,
976 &pctx->sgsn_addr_ip4, &pctx->ms_addr_ip4, pctx);
977 break;
978 }
979
980 return 0;
981}
982
983static int gtp_genl_new_pdp(struct sk_buff *skb, struct genl_info *info)
984{
985 struct net_device *dev;
986 struct net *net;
987
988 if (!info->attrs[GTPA_VERSION] ||
989 !info->attrs[GTPA_LINK] ||
990 !info->attrs[GTPA_SGSN_ADDRESS] ||
991 !info->attrs[GTPA_MS_ADDRESS])
992 return -EINVAL;
993
994 switch (nla_get_u32(info->attrs[GTPA_VERSION])) {
995 case GTP_V0:
996 if (!info->attrs[GTPA_TID] ||
997 !info->attrs[GTPA_FLOW])
998 return -EINVAL;
999 break;
1000 case GTP_V1:
1001 if (!info->attrs[GTPA_I_TEI] ||
1002 !info->attrs[GTPA_O_TEI])
1003 return -EINVAL;
1004 break;
1005
1006 default:
1007 return -EINVAL;
1008 }
1009
1010 net = gtp_genl_get_net(sock_net(skb->sk), info->attrs);
1011 if (IS_ERR(net))
1012 return PTR_ERR(net);
1013
1014 /* Check if there's an existing gtpX device to configure */
1015 dev = gtp_find_dev(net, nla_get_u32(info->attrs[GTPA_LINK]));
Pablo Neira27ee4412016-05-12 17:16:31 +02001016 if (dev == NULL) {
1017 put_net(net);
Pablo Neira459aa662016-05-09 00:55:48 +02001018 return -ENODEV;
Pablo Neira27ee4412016-05-12 17:16:31 +02001019 }
1020 put_net(net);
Pablo Neira459aa662016-05-09 00:55:48 +02001021
1022 return ipv4_pdp_add(dev, info);
1023}
1024
1025static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info)
1026{
1027 struct net_device *dev;
1028 struct pdp_ctx *pctx;
1029 struct gtp_dev *gtp;
1030 struct net *net;
1031
1032 if (!info->attrs[GTPA_VERSION] ||
1033 !info->attrs[GTPA_LINK])
1034 return -EINVAL;
1035
1036 net = gtp_genl_get_net(sock_net(skb->sk), info->attrs);
1037 if (IS_ERR(net))
1038 return PTR_ERR(net);
1039
1040 /* Check if there's an existing gtpX device to configure */
1041 dev = gtp_find_dev(net, nla_get_u32(info->attrs[GTPA_LINK]));
Pablo Neira27ee4412016-05-12 17:16:31 +02001042 if (dev == NULL) {
1043 put_net(net);
Pablo Neira459aa662016-05-09 00:55:48 +02001044 return -ENODEV;
Pablo Neira27ee4412016-05-12 17:16:31 +02001045 }
1046 put_net(net);
Pablo Neira459aa662016-05-09 00:55:48 +02001047
1048 gtp = netdev_priv(dev);
1049
1050 switch (nla_get_u32(info->attrs[GTPA_VERSION])) {
1051 case GTP_V0:
1052 if (!info->attrs[GTPA_TID])
1053 return -EINVAL;
1054 pctx = gtp0_pdp_find(gtp, nla_get_u64(info->attrs[GTPA_TID]));
1055 break;
1056 case GTP_V1:
1057 if (!info->attrs[GTPA_I_TEI])
1058 return -EINVAL;
1059 pctx = gtp1_pdp_find(gtp, nla_get_u64(info->attrs[GTPA_I_TEI]));
1060 break;
1061
1062 default:
1063 return -EINVAL;
1064 }
1065
1066 if (pctx == NULL)
1067 return -ENOENT;
1068
1069 if (pctx->gtp_version == GTP_V0)
1070 netdev_dbg(dev, "GTPv0-U: deleting tunnel id = %llx (pdp %p)\n",
1071 pctx->u.v0.tid, pctx);
1072 else if (pctx->gtp_version == GTP_V1)
1073 netdev_dbg(dev, "GTPv1-U: deleting tunnel id = %x/%x (pdp %p)\n",
1074 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
1075
1076 hlist_del_rcu(&pctx->hlist_tid);
1077 hlist_del_rcu(&pctx->hlist_addr);
1078 kfree_rcu(pctx, rcu_head);
1079
1080 return 0;
1081}
1082
Johannes Berg489111e2016-10-24 14:40:03 +02001083static struct genl_family gtp_genl_family;
Pablo Neira459aa662016-05-09 00:55:48 +02001084
1085static int gtp_genl_fill_info(struct sk_buff *skb, u32 snd_portid, u32 snd_seq,
1086 u32 type, struct pdp_ctx *pctx)
1087{
1088 void *genlh;
1089
1090 genlh = genlmsg_put(skb, snd_portid, snd_seq, &gtp_genl_family, 0,
1091 type);
1092 if (genlh == NULL)
1093 goto nlmsg_failure;
1094
1095 if (nla_put_u32(skb, GTPA_VERSION, pctx->gtp_version) ||
1096 nla_put_be32(skb, GTPA_SGSN_ADDRESS, pctx->sgsn_addr_ip4.s_addr) ||
1097 nla_put_be32(skb, GTPA_MS_ADDRESS, pctx->ms_addr_ip4.s_addr))
1098 goto nla_put_failure;
1099
1100 switch (pctx->gtp_version) {
1101 case GTP_V0:
1102 if (nla_put_u64_64bit(skb, GTPA_TID, pctx->u.v0.tid, GTPA_PAD) ||
1103 nla_put_u16(skb, GTPA_FLOW, pctx->u.v0.flow))
1104 goto nla_put_failure;
1105 break;
1106 case GTP_V1:
1107 if (nla_put_u32(skb, GTPA_I_TEI, pctx->u.v1.i_tei) ||
1108 nla_put_u32(skb, GTPA_O_TEI, pctx->u.v1.o_tei))
1109 goto nla_put_failure;
1110 break;
1111 }
1112 genlmsg_end(skb, genlh);
1113 return 0;
1114
1115nlmsg_failure:
1116nla_put_failure:
1117 genlmsg_cancel(skb, genlh);
1118 return -EMSGSIZE;
1119}
1120
1121static int gtp_genl_get_pdp(struct sk_buff *skb, struct genl_info *info)
1122{
1123 struct pdp_ctx *pctx = NULL;
1124 struct net_device *dev;
1125 struct sk_buff *skb2;
1126 struct gtp_dev *gtp;
1127 u32 gtp_version;
1128 struct net *net;
1129 int err;
1130
1131 if (!info->attrs[GTPA_VERSION] ||
1132 !info->attrs[GTPA_LINK])
1133 return -EINVAL;
1134
1135 gtp_version = nla_get_u32(info->attrs[GTPA_VERSION]);
1136 switch (gtp_version) {
1137 case GTP_V0:
1138 case GTP_V1:
1139 break;
1140 default:
1141 return -EINVAL;
1142 }
1143
1144 net = gtp_genl_get_net(sock_net(skb->sk), info->attrs);
1145 if (IS_ERR(net))
1146 return PTR_ERR(net);
1147
1148 /* Check if there's an existing gtpX device to configure */
1149 dev = gtp_find_dev(net, nla_get_u32(info->attrs[GTPA_LINK]));
Pablo Neira27ee4412016-05-12 17:16:31 +02001150 if (dev == NULL) {
1151 put_net(net);
Pablo Neira459aa662016-05-09 00:55:48 +02001152 return -ENODEV;
Pablo Neira27ee4412016-05-12 17:16:31 +02001153 }
1154 put_net(net);
Pablo Neira459aa662016-05-09 00:55:48 +02001155
1156 gtp = netdev_priv(dev);
1157
1158 rcu_read_lock();
1159 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");