]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/net/gtp.c
gtp: consolidate gtp socket rx path
[mirror_ubuntu-jammy-kernel.git] / drivers / net / gtp.c
CommitLineData
459aa660
PN
1/* 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>
459aa660
PN
19#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
5b171f9c
AS
61 struct net_device *dev;
62
459aa660
PN
63 atomic_t tx_seq;
64 struct rcu_head rcu_head;
65};
66
67/* One instance of the GTP device. */
68struct gtp_dev {
69 struct list_head list;
70
17886c47
AS
71 struct sock *sk0;
72 struct sock *sk1u;
459aa660 73
459aa660
PN
74 struct net_device *dev;
75
76 unsigned int hash_size;
77 struct hlist_head *tid_hash;
78 struct hlist_head *addr_hash;
79};
80
c7d03a00 81static unsigned int gtp_net_id __read_mostly;
459aa660
PN
82
83struct gtp_net {
84 struct list_head gtp_dev_list;
85};
86
87static u32 gtp_h_initval;
88
89static inline u32 gtp0_hashfn(u64 tid)
90{
91 u32 *tid32 = (u32 *) &tid;
92 return jhash_2words(tid32[0], tid32[1], gtp_h_initval);
93}
94
95static inline u32 gtp1u_hashfn(u32 tid)
96{
97 return jhash_1word(tid, gtp_h_initval);
98}
99
100static inline u32 ipv4_hashfn(__be32 ip)
101{
102 return jhash_1word((__force u32)ip, gtp_h_initval);
103}
104
105/* Resolve a PDP context structure based on the 64bit TID. */
106static struct pdp_ctx *gtp0_pdp_find(struct gtp_dev *gtp, u64 tid)
107{
108 struct hlist_head *head;
109 struct pdp_ctx *pdp;
110
111 head = &gtp->tid_hash[gtp0_hashfn(tid) % gtp->hash_size];
112
113 hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
114 if (pdp->gtp_version == GTP_V0 &&
115 pdp->u.v0.tid == tid)
116 return pdp;
117 }
118 return NULL;
119}
120
121/* Resolve a PDP context structure based on the 32bit TEI. */
122static struct pdp_ctx *gtp1_pdp_find(struct gtp_dev *gtp, u32 tid)
123{
124 struct hlist_head *head;
125 struct pdp_ctx *pdp;
126
127 head = &gtp->tid_hash[gtp1u_hashfn(tid) % gtp->hash_size];
128
129 hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
130 if (pdp->gtp_version == GTP_V1 &&
131 pdp->u.v1.i_tei == tid)
132 return pdp;
133 }
134 return NULL;
135}
136
137/* Resolve a PDP context based on IPv4 address of MS. */
138static struct pdp_ctx *ipv4_pdp_find(struct gtp_dev *gtp, __be32 ms_addr)
139{
140 struct hlist_head *head;
141 struct pdp_ctx *pdp;
142
143 head = &gtp->addr_hash[ipv4_hashfn(ms_addr) % gtp->hash_size];
144
145 hlist_for_each_entry_rcu(pdp, head, hlist_addr) {
146 if (pdp->af == AF_INET &&
147 pdp->ms_addr_ip4.s_addr == ms_addr)
148 return pdp;
149 }
150
151 return NULL;
152}
153
154static bool gtp_check_src_ms_ipv4(struct sk_buff *skb, struct pdp_ctx *pctx,
155 unsigned int hdrlen)
156{
157 struct iphdr *iph;
158
159 if (!pskb_may_pull(skb, hdrlen + sizeof(struct iphdr)))
160 return false;
161
88edf103 162 iph = (struct iphdr *)(skb->data + hdrlen);
459aa660 163
88edf103 164 return iph->saddr == pctx->ms_addr_ip4.s_addr;
459aa660
PN
165}
166
167/* Check if the inner IP source address in this packet is assigned to any
168 * existing mobile subscriber.
169 */
170static bool gtp_check_src_ms(struct sk_buff *skb, struct pdp_ctx *pctx,
171 unsigned int hdrlen)
172{
173 switch (ntohs(skb->protocol)) {
174 case ETH_P_IP:
175 return gtp_check_src_ms_ipv4(skb, pctx, hdrlen);
176 }
177 return false;
178}
179
5b171f9c
AS
180static int gtp_rx(struct pdp_ctx *pctx, struct sk_buff *skb, unsigned int hdrlen,
181 bool xnet)
182{
183 struct pcpu_sw_netstats *stats;
184
185 if (!gtp_check_src_ms(skb, pctx, hdrlen)) {
186 netdev_dbg(pctx->dev, "No PDP ctx for this MS\n");
187 return 1;
188 }
189
190 /* Get rid of the GTP + UDP headers. */
191 if (iptunnel_pull_header(skb, hdrlen, skb->protocol, xnet))
192 return -1;
193
194 netdev_dbg(pctx->dev, "forwarding packet from GGSN to uplink\n");
195
196 /* Now that the UDP and the GTP header have been removed, set up the
197 * new network header. This is required by the upper layer to
198 * calculate the transport header.
199 */
200 skb_reset_network_header(skb);
201
202 skb->dev = pctx->dev;
203
204 stats = this_cpu_ptr(pctx->dev->tstats);
205 u64_stats_update_begin(&stats->syncp);
206 stats->rx_packets++;
207 stats->rx_bytes += skb->len;
208 u64_stats_update_end(&stats->syncp);
209
210 netif_rx(skb);
211 return 0;
212}
213
459aa660
PN
214/* 1 means pass up to the stack, -1 means drop and 0 means decapsulated. */
215static int gtp0_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb,
216 bool xnet)
217{
218 unsigned int hdrlen = sizeof(struct udphdr) +
219 sizeof(struct gtp0_header);
220 struct gtp0_header *gtp0;
221 struct pdp_ctx *pctx;
459aa660
PN
222
223 if (!pskb_may_pull(skb, hdrlen))
224 return -1;
225
226 gtp0 = (struct gtp0_header *)(skb->data + sizeof(struct udphdr));
227
228 if ((gtp0->flags >> 5) != GTP_V0)
229 return 1;
230
231 if (gtp0->type != GTP_TPDU)
232 return 1;
233
459aa660
PN
234 pctx = gtp0_pdp_find(gtp, be64_to_cpu(gtp0->tid));
235 if (!pctx) {
236 netdev_dbg(gtp->dev, "No PDP ctx to decap skb=%p\n", skb);
1796a81d 237 return 1;
459aa660
PN
238 }
239
5b171f9c 240 return gtp_rx(pctx, skb, hdrlen, xnet);
459aa660
PN
241}
242
243static int gtp1u_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb,
244 bool xnet)
245{
246 unsigned int hdrlen = sizeof(struct udphdr) +
247 sizeof(struct gtp1_header);
248 struct gtp1_header *gtp1;
249 struct pdp_ctx *pctx;
459aa660
PN
250
251 if (!pskb_may_pull(skb, hdrlen))
252 return -1;
253
254 gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
255
256 if ((gtp1->flags >> 5) != GTP_V1)
257 return 1;
258
259 if (gtp1->type != GTP_TPDU)
260 return 1;
261
262 /* From 29.060: "This field shall be present if and only if any one or
263 * more of the S, PN and E flags are set.".
264 *
265 * If any of the bit is set, then the remaining ones also have to be
266 * set.
267 */
268 if (gtp1->flags & GTP1_F_MASK)
269 hdrlen += 4;
270
271 /* Make sure the header is larger enough, including extensions. */
272 if (!pskb_may_pull(skb, hdrlen))
273 return -1;
274
93edb8c7
PN
275 gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
276
459aa660
PN
277 pctx = gtp1_pdp_find(gtp, ntohl(gtp1->tid));
278 if (!pctx) {
279 netdev_dbg(gtp->dev, "No PDP ctx to decap skb=%p\n", skb);
1796a81d 280 return 1;
459aa660
PN
281 }
282
5b171f9c 283 return gtp_rx(pctx, skb, hdrlen, xnet);
459aa660
PN
284}
285
1e3a3abd 286static void gtp_encap_destroy(struct sock *sk)
459aa660 287{
1e3a3abd 288 struct gtp_dev *gtp;
459aa660 289
1e3a3abd
AS
290 gtp = rcu_dereference_sk_user_data(sk);
291 if (gtp) {
292 udp_sk(sk)->encap_type = 0;
293 rcu_assign_sk_user_data(sk, NULL);
294 sock_put(sk);
295 }
459aa660
PN
296}
297
1e3a3abd 298static void gtp_encap_disable_sock(struct sock *sk)
459aa660 299{
1e3a3abd
AS
300 if (!sk)
301 return;
459aa660 302
1e3a3abd
AS
303 gtp_encap_destroy(sk);
304}
305
306static void gtp_encap_disable(struct gtp_dev *gtp)
307{
308 gtp_encap_disable_sock(gtp->sk0);
309 gtp_encap_disable_sock(gtp->sk1u);
459aa660
PN
310}
311
312/* UDP encapsulation receive handler. See net/ipv4/udp.c.
313 * Return codes: 0: success, <0: error, >0: pass up to userspace UDP socket.
314 */
315static int gtp_encap_recv(struct sock *sk, struct sk_buff *skb)
316{
459aa660 317 struct gtp_dev *gtp;
5b171f9c 318 int ret = 0;
459aa660 319 bool xnet;
459aa660
PN
320
321 gtp = rcu_dereference_sk_user_data(sk);
322 if (!gtp)
323 return 1;
324
325 netdev_dbg(gtp->dev, "encap_recv sk=%p\n", sk);
326
3ab1b469 327 xnet = !net_eq(sock_net(sk), dev_net(gtp->dev));
459aa660
PN
328
329 switch (udp_sk(sk)->encap_type) {
330 case UDP_ENCAP_GTP0:
331 netdev_dbg(gtp->dev, "received GTP0 packet\n");
332 ret = gtp0_udp_encap_recv(gtp, skb, xnet);
333 break;
334 case UDP_ENCAP_GTP1U:
335 netdev_dbg(gtp->dev, "received GTP1U packet\n");
336 ret = gtp1u_udp_encap_recv(gtp, skb, xnet);
337 break;
338 default:
339 ret = -1; /* Shouldn't happen. */
340 }
341
342 switch (ret) {
343 case 1:
344 netdev_dbg(gtp->dev, "pass up to the process\n");
5b171f9c 345 break;
459aa660 346 case 0:
459aa660
PN
347 break;
348 case -1:
349 netdev_dbg(gtp->dev, "GTP packet has been dropped\n");
350 kfree_skb(skb);
5b171f9c
AS
351 ret = 0;
352 break;
459aa660
PN
353 }
354
5b171f9c 355 return ret;
459aa660
PN
356}
357
358static int gtp_dev_init(struct net_device *dev)
359{
360 struct gtp_dev *gtp = netdev_priv(dev);
361
362 gtp->dev = dev;
363
364 dev->tstats = alloc_percpu(struct pcpu_sw_netstats);
365 if (!dev->tstats)
366 return -ENOMEM;
367
368 return 0;
369}
370
371static void gtp_dev_uninit(struct net_device *dev)
372{
373 struct gtp_dev *gtp = netdev_priv(dev);
374
375 gtp_encap_disable(gtp);
376 free_percpu(dev->tstats);
377}
378
379static struct rtable *ip4_route_output_gtp(struct net *net, struct flowi4 *fl4,
380 const struct sock *sk, __be32 daddr)
381{
382 memset(fl4, 0, sizeof(*fl4));
383 fl4->flowi4_oif = sk->sk_bound_dev_if;
384 fl4->daddr = daddr;
385 fl4->saddr = inet_sk(sk)->inet_saddr;
386 fl4->flowi4_tos = RT_CONN_FLAGS(sk);
387 fl4->flowi4_proto = sk->sk_protocol;
388
389 return ip_route_output_key(net, fl4);
390}
391
392static inline void gtp0_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
393{
394 int payload_len = skb->len;
395 struct gtp0_header *gtp0;
396
397 gtp0 = (struct gtp0_header *) skb_push(skb, sizeof(*gtp0));
398
399 gtp0->flags = 0x1e; /* v0, GTP-non-prime. */
400 gtp0->type = GTP_TPDU;
401 gtp0->length = htons(payload_len);
402 gtp0->seq = htons((atomic_inc_return(&pctx->tx_seq) - 1) % 0xffff);
403 gtp0->flow = htons(pctx->u.v0.flow);
404 gtp0->number = 0xff;
405 gtp0->spare[0] = gtp0->spare[1] = gtp0->spare[2] = 0xff;
406 gtp0->tid = cpu_to_be64(pctx->u.v0.tid);
407}
408
409static inline void gtp1_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
410{
411 int payload_len = skb->len;
412 struct gtp1_header *gtp1;
413
414 gtp1 = (struct gtp1_header *) skb_push(skb, sizeof(*gtp1));
415
416 /* Bits 8 7 6 5 4 3 2 1
417 * +--+--+--+--+--+--+--+--+
d928be81 418 * |version |PT| 0| E| S|PN|
459aa660
PN
419 * +--+--+--+--+--+--+--+--+
420 * 0 0 1 1 1 0 0 0
421 */
d928be81 422 gtp1->flags = 0x30; /* v1, GTP-non-prime. */
459aa660
PN
423 gtp1->type = GTP_TPDU;
424 gtp1->length = htons(payload_len);
425 gtp1->tid = htonl(pctx->u.v1.o_tei);
426
427 /* TODO: Suppport for extension header, sequence number and N-PDU.
428 * Update the length field if any of them is available.
429 */
430}
431
432struct gtp_pktinfo {
433 struct sock *sk;
434 struct iphdr *iph;
435 struct flowi4 fl4;
436 struct rtable *rt;
437 struct pdp_ctx *pctx;
438 struct net_device *dev;
439 __be16 gtph_port;
440};
441
442static void gtp_push_header(struct sk_buff *skb, struct gtp_pktinfo *pktinfo)
443{
444 switch (pktinfo->pctx->gtp_version) {
445 case GTP_V0:
446 pktinfo->gtph_port = htons(GTP0_PORT);
447 gtp0_push_header(skb, pktinfo->pctx);
448 break;
449 case GTP_V1:
450 pktinfo->gtph_port = htons(GTP1U_PORT);
451 gtp1_push_header(skb, pktinfo->pctx);
452 break;
453 }
454}
455
456static inline void gtp_set_pktinfo_ipv4(struct gtp_pktinfo *pktinfo,
457 struct sock *sk, struct iphdr *iph,
458 struct pdp_ctx *pctx, struct rtable *rt,
459 struct flowi4 *fl4,
460 struct net_device *dev)
461{
462 pktinfo->sk = sk;
463 pktinfo->iph = iph;
464 pktinfo->pctx = pctx;
465 pktinfo->rt = rt;
466 pktinfo->fl4 = *fl4;
467 pktinfo->dev = dev;
468}
469
470static int gtp_build_skb_ip4(struct sk_buff *skb, struct net_device *dev,
471 struct gtp_pktinfo *pktinfo)
472{
473 struct gtp_dev *gtp = netdev_priv(dev);
474 struct pdp_ctx *pctx;
475 struct rtable *rt;
476 struct flowi4 fl4;
477 struct iphdr *iph;
478 struct sock *sk;
479 __be16 df;
480 int mtu;
481
482 /* Read the IP destination address and resolve the PDP context.
483 * Prepend PDP header with TEI/TID from PDP ctx.
484 */
485 iph = ip_hdr(skb);
486 pctx = ipv4_pdp_find(gtp, iph->daddr);
487 if (!pctx) {
488 netdev_dbg(dev, "no PDP ctx found for %pI4, skip\n",
489 &iph->daddr);
490 return -ENOENT;
491 }
492 netdev_dbg(dev, "found PDP context %p\n", pctx);
493
494 switch (pctx->gtp_version) {
495 case GTP_V0:
17886c47
AS
496 if (gtp->sk0)
497 sk = gtp->sk0;
459aa660
PN
498 else
499 sk = NULL;
500 break;
501 case GTP_V1:
17886c47
AS
502 if (gtp->sk1u)
503 sk = gtp->sk1u;
459aa660
PN
504 else
505 sk = NULL;
506 break;
507 default:
508 return -ENOENT;
509 }
510
511 if (!sk) {
512 netdev_dbg(dev, "no userspace socket is available, skip\n");
513 return -ENOENT;
514 }
515
17886c47 516 rt = ip4_route_output_gtp(sock_net(sk), &fl4, gtp->sk0,
459aa660
PN
517 pctx->sgsn_addr_ip4.s_addr);
518 if (IS_ERR(rt)) {
519 netdev_dbg(dev, "no route to SSGN %pI4\n",
520 &pctx->sgsn_addr_ip4.s_addr);
521 dev->stats.tx_carrier_errors++;
522 goto err;
523 }
524
525 if (rt->dst.dev == dev) {
526 netdev_dbg(dev, "circular route to SSGN %pI4\n",
527 &pctx->sgsn_addr_ip4.s_addr);
528 dev->stats.collisions++;
529 goto err_rt;
530 }
531
532 skb_dst_drop(skb);
533
534 /* This is similar to tnl_update_pmtu(). */
535 df = iph->frag_off;
536 if (df) {
537 mtu = dst_mtu(&rt->dst) - dev->hard_header_len -
538 sizeof(struct iphdr) - sizeof(struct udphdr);
539 switch (pctx->gtp_version) {
540 case GTP_V0:
541 mtu -= sizeof(struct gtp0_header);
542 break;
543 case GTP_V1:
544 mtu -= sizeof(struct gtp1_header);
545 break;
546 }
547 } else {
548 mtu = dst_mtu(&rt->dst);
549 }
550
551 rt->dst.ops->update_pmtu(&rt->dst, NULL, skb, mtu);
552
553 if (!skb_is_gso(skb) && (iph->frag_off & htons(IP_DF)) &&
554 mtu < ntohs(iph->tot_len)) {
555 netdev_dbg(dev, "packet too big, fragmentation needed\n");
556 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
557 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
558 htonl(mtu));
559 goto err_rt;
560 }
561
562 gtp_set_pktinfo_ipv4(pktinfo, sk, iph, pctx, rt, &fl4, dev);
563 gtp_push_header(skb, pktinfo);
564
565 return 0;
566err_rt:
567 ip_rt_put(rt);
568err:
569 return -EBADMSG;
570}
571
572static netdev_tx_t gtp_dev_xmit(struct sk_buff *skb, struct net_device *dev)
573{
574 unsigned int proto = ntohs(skb->protocol);
575 struct gtp_pktinfo pktinfo;
576 int err;
577
578 /* Ensure there is sufficient headroom. */
579 if (skb_cow_head(skb, dev->needed_headroom))
580 goto tx_err;
581
582 skb_reset_inner_headers(skb);
583
584 /* PDP context lookups in gtp_build_skb_*() need rcu read-side lock. */
585 rcu_read_lock();
586 switch (proto) {
587 case ETH_P_IP:
588 err = gtp_build_skb_ip4(skb, dev, &pktinfo);
589 break;
590 default:
591 err = -EOPNOTSUPP;
592 break;
593 }
594 rcu_read_unlock();
595
596 if (err < 0)
597 goto tx_err;
598
599 switch (proto) {
600 case ETH_P_IP:
601 netdev_dbg(pktinfo.dev, "gtp -> IP src: %pI4 dst: %pI4\n",
602 &pktinfo.iph->saddr, &pktinfo.iph->daddr);
603 udp_tunnel_xmit_skb(pktinfo.rt, pktinfo.sk, skb,
604 pktinfo.fl4.saddr, pktinfo.fl4.daddr,
605 pktinfo.iph->tos,
606 ip4_dst_hoplimit(&pktinfo.rt->dst),
c6ce1d08 607 0,
459aa660
PN
608 pktinfo.gtph_port, pktinfo.gtph_port,
609 true, false);
610 break;
611 }
612
613 return NETDEV_TX_OK;
614tx_err:
615 dev->stats.tx_errors++;
616 dev_kfree_skb(skb);
617 return NETDEV_TX_OK;
618}
619
620static const struct net_device_ops gtp_netdev_ops = {
621 .ndo_init = gtp_dev_init,
622 .ndo_uninit = gtp_dev_uninit,
623 .ndo_start_xmit = gtp_dev_xmit,
624 .ndo_get_stats64 = ip_tunnel_get_stats64,
625};
626
627static void gtp_link_setup(struct net_device *dev)
628{
629 dev->netdev_ops = &gtp_netdev_ops;
630 dev->destructor = free_netdev;
631
632 dev->hard_header_len = 0;
633 dev->addr_len = 0;
634
635 /* Zero header length. */
636 dev->type = ARPHRD_NONE;
637 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
638
639 dev->priv_flags |= IFF_NO_QUEUE;
640 dev->features |= NETIF_F_LLTX;
641 netif_keep_dst(dev);
642
643 /* Assume largest header, ie. GTPv0. */
644 dev->needed_headroom = LL_MAX_HEADER +
645 sizeof(struct iphdr) +
646 sizeof(struct udphdr) +
647 sizeof(struct gtp0_header);
648}
649
650static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize);
651static void gtp_hashtable_free(struct gtp_dev *gtp);
1e3a3abd 652static int gtp_encap_enable(struct gtp_dev *gtp, struct nlattr *data[]);
459aa660
PN
653
654static int gtp_newlink(struct net *src_net, struct net_device *dev,
655 struct nlattr *tb[], struct nlattr *data[])
656{
459aa660
PN
657 struct gtp_dev *gtp;
658 struct gtp_net *gn;
1e3a3abd 659 int hashsize, err;
459aa660 660
1e3a3abd 661 if (!data[IFLA_GTP_FD0] && !data[IFLA_GTP_FD1])
459aa660
PN
662 return -EINVAL;
663
664 gtp = netdev_priv(dev);
665
1e3a3abd 666 err = gtp_encap_enable(gtp, data);
459aa660 667 if (err < 0)
1e3a3abd 668 return err;
459aa660
PN
669
670 if (!data[IFLA_GTP_PDP_HASHSIZE])
671 hashsize = 1024;
672 else
673 hashsize = nla_get_u32(data[IFLA_GTP_PDP_HASHSIZE]);
674
675 err = gtp_hashtable_new(gtp, hashsize);
676 if (err < 0)
677 goto out_encap;
678
679 err = register_netdevice(dev);
680 if (err < 0) {
681 netdev_dbg(dev, "failed to register new netdev %d\n", err);
682 goto out_hashtable;
683 }
684
685 gn = net_generic(dev_net(dev), gtp_net_id);
686 list_add_rcu(&gtp->list, &gn->gtp_dev_list);
687
688 netdev_dbg(dev, "registered new GTP interface\n");
689
690 return 0;
691
692out_hashtable:
693 gtp_hashtable_free(gtp);
694out_encap:
695 gtp_encap_disable(gtp);
459aa660
PN
696 return err;
697}
698
699static void gtp_dellink(struct net_device *dev, struct list_head *head)
700{
701 struct gtp_dev *gtp = netdev_priv(dev);
702
703 gtp_encap_disable(gtp);
704 gtp_hashtable_free(gtp);
705 list_del_rcu(&gtp->list);
706 unregister_netdevice_queue(dev, head);
707}
708
709static const struct nla_policy gtp_policy[IFLA_GTP_MAX + 1] = {
710 [IFLA_GTP_FD0] = { .type = NLA_U32 },
711 [IFLA_GTP_FD1] = { .type = NLA_U32 },
712 [IFLA_GTP_PDP_HASHSIZE] = { .type = NLA_U32 },
713};
714
715static int gtp_validate(struct nlattr *tb[], struct nlattr *data[])
716{
717 if (!data)
718 return -EINVAL;
719
720 return 0;
721}
722
723static size_t gtp_get_size(const struct net_device *dev)
724{
725 return nla_total_size(sizeof(__u32)); /* IFLA_GTP_PDP_HASHSIZE */
726}
727
728static int gtp_fill_info(struct sk_buff *skb, const struct net_device *dev)
729{
730 struct gtp_dev *gtp = netdev_priv(dev);
731
732 if (nla_put_u32(skb, IFLA_GTP_PDP_HASHSIZE, gtp->hash_size))
733 goto nla_put_failure;
734
735 return 0;
736
737nla_put_failure:
738 return -EMSGSIZE;
739}
740
741static struct rtnl_link_ops gtp_link_ops __read_mostly = {
742 .kind = "gtp",
743 .maxtype = IFLA_GTP_MAX,
744 .policy = gtp_policy,
745 .priv_size = sizeof(struct gtp_dev),
746 .setup = gtp_link_setup,
747 .validate = gtp_validate,
748 .newlink = gtp_newlink,
749 .dellink = gtp_dellink,
750 .get_size = gtp_get_size,
751 .fill_info = gtp_fill_info,
752};
753
459aa660
PN
754static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize)
755{
756 int i;
757
758 gtp->addr_hash = kmalloc(sizeof(struct hlist_head) * hsize, GFP_KERNEL);
759 if (gtp->addr_hash == NULL)
760 return -ENOMEM;
761
762 gtp->tid_hash = kmalloc(sizeof(struct hlist_head) * hsize, GFP_KERNEL);
763 if (gtp->tid_hash == NULL)
764 goto err1;
765
766 gtp->hash_size = hsize;
767
768 for (i = 0; i < hsize; i++) {
769 INIT_HLIST_HEAD(&gtp->addr_hash[i]);
770 INIT_HLIST_HEAD(&gtp->tid_hash[i]);
771 }
772 return 0;
773err1:
774 kfree(gtp->addr_hash);
775 return -ENOMEM;
776}
777
778static void gtp_hashtable_free(struct gtp_dev *gtp)
779{
780 struct pdp_ctx *pctx;
781 int i;
782
783 for (i = 0; i < gtp->hash_size; i++) {
784 hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid) {
785 hlist_del_rcu(&pctx->hlist_tid);
786 hlist_del_rcu(&pctx->hlist_addr);
787 kfree_rcu(pctx, rcu_head);
788 }
789 }
790 synchronize_rcu();
791 kfree(gtp->addr_hash);
792 kfree(gtp->tid_hash);
793}
794
1e3a3abd
AS
795static struct sock *gtp_encap_enable_socket(int fd, int type,
796 struct gtp_dev *gtp)
459aa660
PN
797{
798 struct udp_tunnel_sock_cfg tuncfg = {NULL};
1e3a3abd
AS
799 struct socket *sock;
800 struct sock *sk;
459aa660
PN
801 int err;
802
1e3a3abd 803 pr_debug("enable gtp on %d, %d\n", fd, type);
459aa660 804
1e3a3abd
AS
805 sock = sockfd_lookup(fd, &err);
806 if (!sock) {
807 pr_debug("gtp socket fd=%d not found\n", fd);
808 return NULL;
459aa660
PN
809 }
810
1e3a3abd
AS
811 if (sock->sk->sk_protocol != IPPROTO_UDP) {
812 pr_debug("socket fd=%d not UDP\n", fd);
813 sk = ERR_PTR(-EINVAL);
814 goto out_sock;
459aa660
PN
815 }
816
1e3a3abd
AS
817 if (rcu_dereference_sk_user_data(sock->sk)) {
818 sk = ERR_PTR(-EBUSY);
819 goto out_sock;
459aa660
PN
820 }
821
1e3a3abd
AS
822 sk = sock->sk;
823 sock_hold(sk);
459aa660
PN
824
825 tuncfg.sk_user_data = gtp;
1e3a3abd 826 tuncfg.encap_type = type;
459aa660
PN
827 tuncfg.encap_rcv = gtp_encap_recv;
828 tuncfg.encap_destroy = gtp_encap_destroy;
829
1e3a3abd 830 setup_udp_tunnel_sock(sock_net(sock->sk), sock, &tuncfg);
459aa660 831
1e3a3abd
AS
832out_sock:
833 sockfd_put(sock);
834 return sk;
835}
459aa660 836
1e3a3abd
AS
837static int gtp_encap_enable(struct gtp_dev *gtp, struct nlattr *data[])
838{
839 struct sock *sk1u = NULL;
840 struct sock *sk0 = NULL;
841
842 if (data[IFLA_GTP_FD0]) {
843 u32 fd0 = nla_get_u32(data[IFLA_GTP_FD0]);
844
845 sk0 = gtp_encap_enable_socket(fd0, UDP_ENCAP_GTP0, gtp);
846 if (IS_ERR(sk0))
847 return PTR_ERR(sk0);
848 }
849
850 if (data[IFLA_GTP_FD1]) {
851 u32 fd1 = nla_get_u32(data[IFLA_GTP_FD1]);
852
853 sk1u = gtp_encap_enable_socket(fd1, UDP_ENCAP_GTP1U, gtp);
854 if (IS_ERR(sk1u)) {
855 if (sk0)
856 gtp_encap_disable_sock(sk0);
857 return PTR_ERR(sk1u);
858 }
859 }
860
861 gtp->sk0 = sk0;
862 gtp->sk1u = sk1u;
863
864 return 0;
459aa660
PN
865}
866
3fb94617 867static struct gtp_dev *gtp_find_dev(struct net *src_net, struct nlattr *nla[])
459aa660 868{
3fb94617
AS
869 struct gtp_dev *gtp = NULL;
870 struct net_device *dev;
871 struct net *net;
459aa660 872
3fb94617
AS
873 /* Examine the link attributes and figure out which network namespace
874 * we are talking about.
875 */
876 if (nla[GTPA_NET_NS_FD])
877 net = get_net_ns_by_fd(nla_get_u32(nla[GTPA_NET_NS_FD]));
878 else
879 net = get_net(src_net);
880
881 if (IS_ERR(net))
882 return NULL;
883
884 /* Check if there's an existing gtpX device to configure */
885 dev = dev_get_by_index_rcu(net, nla_get_u32(nla[GTPA_LINK]));
886 if (dev->netdev_ops == &gtp_netdev_ops)
887 gtp = netdev_priv(dev);
888
889 put_net(net);
890 return gtp;
459aa660
PN
891}
892
893static void ipv4_pdp_fill(struct pdp_ctx *pctx, struct genl_info *info)
894{
895 pctx->gtp_version = nla_get_u32(info->attrs[GTPA_VERSION]);
896 pctx->af = AF_INET;
897 pctx->sgsn_addr_ip4.s_addr =
898 nla_get_be32(info->attrs[GTPA_SGSN_ADDRESS]);
899 pctx->ms_addr_ip4.s_addr =
900 nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
901
902 switch (pctx->gtp_version) {
903 case GTP_V0:
904 /* According to TS 09.60, sections 7.5.1 and 7.5.2, the flow
905 * label needs to be the same for uplink and downlink packets,
906 * so let's annotate this.
907 */
908 pctx->u.v0.tid = nla_get_u64(info->attrs[GTPA_TID]);
909 pctx->u.v0.flow = nla_get_u16(info->attrs[GTPA_FLOW]);
910 break;
911 case GTP_V1:
912 pctx->u.v1.i_tei = nla_get_u32(info->attrs[GTPA_I_TEI]);
913 pctx->u.v1.o_tei = nla_get_u32(info->attrs[GTPA_O_TEI]);
914 break;
915 default:
916 break;
917 }
918}
919
3fb94617 920static int ipv4_pdp_add(struct gtp_dev *gtp, struct genl_info *info)
459aa660 921{
3fb94617 922 struct net_device *dev = gtp->dev;
459aa660
PN
923 u32 hash_ms, hash_tid = 0;
924 struct pdp_ctx *pctx;
925 bool found = false;
926 __be32 ms_addr;
927
928 ms_addr = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
929 hash_ms = ipv4_hashfn(ms_addr) % gtp->hash_size;
930
931 hlist_for_each_entry_rcu(pctx, &gtp->addr_hash[hash_ms], hlist_addr) {
932 if (pctx->ms_addr_ip4.s_addr == ms_addr) {
933 found = true;
934 break;
935 }
936 }
937
938 if (found) {
939 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
940 return -EEXIST;
941 if (info->nlhdr->nlmsg_flags & NLM_F_REPLACE)
942 return -EOPNOTSUPP;
943
944 ipv4_pdp_fill(pctx, info);
945
946 if (pctx->gtp_version == GTP_V0)
947 netdev_dbg(dev, "GTPv0-U: update tunnel id = %llx (pdp %p)\n",
948 pctx->u.v0.tid, pctx);
949 else if (pctx->gtp_version == GTP_V1)
950 netdev_dbg(dev, "GTPv1-U: update tunnel id = %x/%x (pdp %p)\n",
951 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
952
953 return 0;
954
955 }
956
957 pctx = kmalloc(sizeof(struct pdp_ctx), GFP_KERNEL);
958 if (pctx == NULL)
959 return -ENOMEM;
960
5b171f9c 961 pctx->dev = gtp->dev;
459aa660
PN
962 ipv4_pdp_fill(pctx, info);
963 atomic_set(&pctx->tx_seq, 0);
964
965 switch (pctx->gtp_version) {
966 case GTP_V0:
967 /* TS 09.60: "The flow label identifies unambiguously a GTP
968 * flow.". We use the tid for this instead, I cannot find a
969 * situation in which this doesn't unambiguosly identify the
970 * PDP context.
971 */
972 hash_tid = gtp0_hashfn(pctx->u.v0.tid) % gtp->hash_size;
973 break;
974 case GTP_V1:
975 hash_tid = gtp1u_hashfn(pctx->u.v1.i_tei) % gtp->hash_size;
976 break;
977 }
978
979 hlist_add_head_rcu(&pctx->hlist_addr, &gtp->addr_hash[hash_ms]);
980 hlist_add_head_rcu(&pctx->hlist_tid, &gtp->tid_hash[hash_tid]);
981
982 switch (pctx->gtp_version) {
983 case GTP_V0:
984 netdev_dbg(dev, "GTPv0-U: new PDP ctx id=%llx ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
985 pctx->u.v0.tid, &pctx->sgsn_addr_ip4,
986 &pctx->ms_addr_ip4, pctx);
987 break;
988 case GTP_V1:
989 netdev_dbg(dev, "GTPv1-U: new PDP ctx id=%x/%x ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
990 pctx->u.v1.i_tei, pctx->u.v1.o_tei,
991 &pctx->sgsn_addr_ip4, &pctx->ms_addr_ip4, pctx);
992 break;
993 }
994
995 return 0;
996}
997
998static int gtp_genl_new_pdp(struct sk_buff *skb, struct genl_info *info)
999{
3fb94617
AS
1000 struct gtp_dev *gtp;
1001 int err;
459aa660
PN
1002
1003 if (!info->attrs[GTPA_VERSION] ||
1004 !info->attrs[GTPA_LINK] ||
1005 !info->attrs[GTPA_SGSN_ADDRESS] ||
1006 !info->attrs[GTPA_MS_ADDRESS])
1007 return -EINVAL;
1008
1009 switch (nla_get_u32(info->attrs[GTPA_VERSION])) {
1010 case GTP_V0:
1011 if (!info->attrs[GTPA_TID] ||
1012 !info->attrs[GTPA_FLOW])
1013 return -EINVAL;
1014 break;
1015 case GTP_V1:
1016 if (!info->attrs[GTPA_I_TEI] ||
1017 !info->attrs[GTPA_O_TEI])
1018 return -EINVAL;
1019 break;
1020
1021 default:
1022 return -EINVAL;
1023 }
1024
3fb94617 1025 rcu_read_lock();
459aa660 1026
3fb94617
AS
1027 gtp = gtp_find_dev(sock_net(skb->sk), info->attrs);
1028 if (!gtp) {
1029 err = -ENODEV;
1030 goto out_unlock;
27ee441a 1031 }
459aa660 1032
3fb94617
AS
1033 err = ipv4_pdp_add(gtp, info);
1034
1035out_unlock:
1036 rcu_read_unlock();
1037 return err;
459aa660
PN
1038}
1039
1040static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info)
1041{
459aa660
PN
1042 struct pdp_ctx *pctx;
1043 struct gtp_dev *gtp;
3fb94617 1044 int err = 0;
459aa660
PN
1045
1046 if (!info->attrs[GTPA_VERSION] ||
1047 !info->attrs[GTPA_LINK])
1048 return -EINVAL;
1049
3fb94617 1050 rcu_read_lock();
459aa660 1051
3fb94617
AS
1052 gtp = gtp_find_dev(sock_net(skb->sk), info->attrs);
1053 if (!gtp) {
1054 err = -ENODEV;
1055 goto out_unlock;
27ee441a 1056 }
459aa660
PN
1057
1058 switch (nla_get_u32(info->attrs[GTPA_VERSION])) {
1059 case GTP_V0:
3fb94617
AS
1060 if (!info->attrs[GTPA_TID]) {
1061 err = -EINVAL;
1062 goto out_unlock;
1063 }
459aa660
PN
1064 pctx = gtp0_pdp_find(gtp, nla_get_u64(info->attrs[GTPA_TID]));
1065 break;
1066 case GTP_V1:
3fb94617
AS
1067 if (!info->attrs[GTPA_I_TEI]) {
1068 err = -EINVAL;
1069 goto out_unlock;
1070 }
459aa660
PN
1071 pctx = gtp1_pdp_find(gtp, nla_get_u64(info->attrs[GTPA_I_TEI]));
1072 break;
1073
1074 default:
3fb94617
AS
1075 err = -EINVAL;
1076 goto out_unlock;
459aa660
PN
1077 }
1078
3fb94617
AS
1079 if (!pctx) {
1080 err = -ENOENT;
1081 goto out_unlock;
1082 }
459aa660
PN
1083
1084 if (pctx->gtp_version == GTP_V0)
3fb94617 1085 netdev_dbg(gtp->dev, "GTPv0-U: deleting tunnel id = %llx (pdp %p)\n",
459aa660
PN
1086 pctx->u.v0.tid, pctx);
1087 else if (pctx->gtp_version == GTP_V1)
3fb94617 1088 netdev_dbg(gtp->dev, "GTPv1-U: deleting tunnel id = %x/%x (pdp %p)\n",
459aa660
PN
1089 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
1090
1091 hlist_del_rcu(&pctx->hlist_tid);
1092 hlist_del_rcu(&pctx->hlist_addr);
1093 kfree_rcu(pctx, rcu_head);
1094
3fb94617
AS
1095out_unlock:
1096 rcu_read_unlock();
1097 return err;
459aa660
PN
1098}
1099
489111e5 1100static struct genl_family gtp_genl_family;
459aa660
PN
1101
1102static int gtp_genl_fill_info(struct sk_buff *skb, u32 snd_portid, u32 snd_seq,
1103 u32 type, struct pdp_ctx *pctx)
1104{
1105 void *genlh;
1106
1107 genlh = genlmsg_put(skb, snd_portid, snd_seq, &gtp_genl_family, 0,
1108 type);
1109 if (genlh == NULL)
1110 goto nlmsg_failure;
1111
1112 if (nla_put_u32(skb, GTPA_VERSION, pctx->gtp_version) ||
1113 nla_put_be32(skb, GTPA_SGSN_ADDRESS, pctx->sgsn_addr_ip4.s_addr) ||
1114 nla_put_be32(skb, GTPA_MS_ADDRESS, pctx->ms_addr_ip4.s_addr))
1115 goto nla_put_failure;
1116
1117 switch (pctx->gtp_version) {
1118 case GTP_V0:
1119 if (nla_put_u64_64bit(skb, GTPA_TID, pctx->u.v0.tid, GTPA_PAD) ||
1120 nla_put_u16(skb, GTPA_FLOW, pctx->u.v0.flow))
1121 goto nla_put_failure;
1122 break;
1123 case GTP_V1:
1124 if (nla_put_u32(skb, GTPA_I_TEI, pctx->u.v1.i_tei) ||
1125 nla_put_u32(skb, GTPA_O_TEI, pctx->u.v1.o_tei))
1126 goto nla_put_failure;
1127 break;
1128 }
1129 genlmsg_end(skb, genlh);
1130 return 0;
1131
1132nlmsg_failure:
1133nla_put_failure:
1134 genlmsg_cancel(skb, genlh);
1135 return -EMSGSIZE;
1136}
1137
1138static int gtp_genl_get_pdp(struct sk_buff *skb, struct genl_info *info)
1139{
1140 struct pdp_ctx *pctx = NULL;
459aa660
PN
1141 struct sk_buff *skb2;
1142 struct gtp_dev *gtp;
1143 u32 gtp_version;
459aa660
PN
1144 int err;
1145
1146 if (!info->attrs[GTPA_VERSION] ||
1147 !info->attrs[GTPA_LINK])
1148 return -EINVAL;
1149
1150 gtp_version = nla_get_u32(info->attrs[GTPA_VERSION]);
1151 switch (gtp_version) {
1152 case GTP_V0:
1153 case GTP_V1:
1154 break;
1155 default:
1156 return -EINVAL;
1157 }
1158
3fb94617 1159 rcu_read_lock();
459aa660 1160
3fb94617
AS
1161 gtp = gtp_find_dev(sock_net(skb->sk), info->attrs);
1162 if (!gtp) {
1163 err = -ENODEV;
1164 goto err_unlock;
27ee441a 1165 }
459aa660 1166
459aa660
PN
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
56989f6d 1287static struct genl_family gtp_genl_family __ro_after_init = {
489111e5
JB
1288 .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
459aa660
PN
1298static 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
489111e5 1337 err = genl_register_family(&gtp_genl_family);
459aa660
PN
1338 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
5b5e0928 1345 pr_info("GTP module loaded (pdp ctx size %zd bytes)\n",
459aa660
PN
1346 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");
ab729823 1373MODULE_ALIAS_GENL_FAMILY("gtp");