blob: 97e0cbca0a08a15af666fdb17ebc0c87e2a09bda [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
69 struct socket *sock0;
70 struct socket *sock1u;
71
72 struct net *net;
73 struct net_device *dev;
74
75 unsigned int hash_size;
76 struct hlist_head *tid_hash;
77 struct hlist_head *addr_hash;
78};
79
80static int gtp_net_id __read_mostly;
81
82struct gtp_net {
83 struct list_head gtp_dev_list;
84};
85
86static u32 gtp_h_initval;
87
88static inline u32 gtp0_hashfn(u64 tid)
89{
90 u32 *tid32 = (u32 *) &tid;
91 return jhash_2words(tid32[0], tid32[1], gtp_h_initval);
92}
93
94static inline u32 gtp1u_hashfn(u32 tid)
95{
96 return jhash_1word(tid, gtp_h_initval);
97}
98
99static inline u32 ipv4_hashfn(__be32 ip)
100{
101 return jhash_1word((__force u32)ip, gtp_h_initval);
102}
103
104/* Resolve a PDP context structure based on the 64bit TID. */
105static struct pdp_ctx *gtp0_pdp_find(struct gtp_dev *gtp, u64 tid)
106{
107 struct hlist_head *head;
108 struct pdp_ctx *pdp;
109
110 head = &gtp->tid_hash[gtp0_hashfn(tid) % gtp->hash_size];
111
112 hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
113 if (pdp->gtp_version == GTP_V0 &&
114 pdp->u.v0.tid == tid)
115 return pdp;
116 }
117 return NULL;
118}
119
120/* Resolve a PDP context structure based on the 32bit TEI. */
121static struct pdp_ctx *gtp1_pdp_find(struct gtp_dev *gtp, u32 tid)
122{
123 struct hlist_head *head;
124 struct pdp_ctx *pdp;
125
126 head = &gtp->tid_hash[gtp1u_hashfn(tid) % gtp->hash_size];
127
128 hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
129 if (pdp->gtp_version == GTP_V1 &&
130 pdp->u.v1.i_tei == tid)
131 return pdp;
132 }
133 return NULL;
134}
135
136/* Resolve a PDP context based on IPv4 address of MS. */
137static struct pdp_ctx *ipv4_pdp_find(struct gtp_dev *gtp, __be32 ms_addr)
138{
139 struct hlist_head *head;
140 struct pdp_ctx *pdp;
141
142 head = &gtp->addr_hash[ipv4_hashfn(ms_addr) % gtp->hash_size];
143
144 hlist_for_each_entry_rcu(pdp, head, hlist_addr) {
145 if (pdp->af == AF_INET &&
146 pdp->ms_addr_ip4.s_addr == ms_addr)
147 return pdp;
148 }
149
150 return NULL;
151}
152
153static bool gtp_check_src_ms_ipv4(struct sk_buff *skb, struct pdp_ctx *pctx,
154 unsigned int hdrlen)
155{
156 struct iphdr *iph;
157
158 if (!pskb_may_pull(skb, hdrlen + sizeof(struct iphdr)))
159 return false;
160
161 iph = (struct iphdr *)(skb->data + hdrlen + sizeof(struct iphdr));
162
163 return iph->saddr != pctx->ms_addr_ip4.s_addr;
164}
165
166/* Check if the inner IP source address in this packet is assigned to any
167 * existing mobile subscriber.
168 */
169static bool gtp_check_src_ms(struct sk_buff *skb, struct pdp_ctx *pctx,
170 unsigned int hdrlen)
171{
172 switch (ntohs(skb->protocol)) {
173 case ETH_P_IP:
174 return gtp_check_src_ms_ipv4(skb, pctx, hdrlen);
175 }
176 return false;
177}
178
179/* 1 means pass up to the stack, -1 means drop and 0 means decapsulated. */
180static int gtp0_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb,
181 bool xnet)
182{
183 unsigned int hdrlen = sizeof(struct udphdr) +
184 sizeof(struct gtp0_header);
185 struct gtp0_header *gtp0;
186 struct pdp_ctx *pctx;
187 int ret = 0;
188
189 if (!pskb_may_pull(skb, hdrlen))
190 return -1;
191
192 gtp0 = (struct gtp0_header *)(skb->data + sizeof(struct udphdr));
193
194 if ((gtp0->flags >> 5) != GTP_V0)
195 return 1;
196
197 if (gtp0->type != GTP_TPDU)
198 return 1;
199
200 rcu_read_lock();
201 pctx = gtp0_pdp_find(gtp, be64_to_cpu(gtp0->tid));
202 if (!pctx) {
203 netdev_dbg(gtp->dev, "No PDP ctx to decap skb=%p\n", skb);
204 ret = -1;
205 goto out_rcu;
206 }
207
208 if (!gtp_check_src_ms(skb, pctx, hdrlen)) {
209 netdev_dbg(gtp->dev, "No PDP ctx for this MS\n");
210 ret = -1;
211 goto out_rcu;
212 }
213 rcu_read_unlock();
214
215 /* Get rid of the GTP + UDP headers. */
216 return iptunnel_pull_header(skb, hdrlen, skb->protocol, xnet);
217out_rcu:
218 rcu_read_unlock();
219 return ret;
220}
221
222static int gtp1u_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb,
223 bool xnet)
224{
225 unsigned int hdrlen = sizeof(struct udphdr) +
226 sizeof(struct gtp1_header);
227 struct gtp1_header *gtp1;
228 struct pdp_ctx *pctx;
229 int ret = 0;
230
231 if (!pskb_may_pull(skb, hdrlen))
232 return -1;
233
234 gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
235
236 if ((gtp1->flags >> 5) != GTP_V1)
237 return 1;
238
239 if (gtp1->type != GTP_TPDU)
240 return 1;
241
242 /* From 29.060: "This field shall be present if and only if any one or
243 * more of the S, PN and E flags are set.".
244 *
245 * If any of the bit is set, then the remaining ones also have to be
246 * set.
247 */
248 if (gtp1->flags & GTP1_F_MASK)
249 hdrlen += 4;
250
251 /* Make sure the header is larger enough, including extensions. */
252 if (!pskb_may_pull(skb, hdrlen))
253 return -1;
254
Pablo Neira93edb8c2016-05-10 21:33:38 +0200255 gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
256
Pablo Neira459aa662016-05-09 00:55:48 +0200257 rcu_read_lock();
258 pctx = gtp1_pdp_find(gtp, ntohl(gtp1->tid));
259 if (!pctx) {
260 netdev_dbg(gtp->dev, "No PDP ctx to decap skb=%p\n", skb);
261 ret = -1;
262 goto out_rcu;
263 }
264
265 if (!gtp_check_src_ms(skb, pctx, hdrlen)) {
266 netdev_dbg(gtp->dev, "No PDP ctx for this MS\n");
267 ret = -1;
268 goto out_rcu;
269 }
270 rcu_read_unlock();
271
272 /* Get rid of the GTP + UDP headers. */
273 return iptunnel_pull_header(skb, hdrlen, skb->protocol, xnet);
274out_rcu:
275 rcu_read_unlock();
276 return ret;
277}
278
279static void gtp_encap_disable(struct gtp_dev *gtp)
280{
281 if (gtp->sock0 && gtp->sock0->sk) {
282 udp_sk(gtp->sock0->sk)->encap_type = 0;
283 rcu_assign_sk_user_data(gtp->sock0->sk, NULL);
284 }
285 if (gtp->sock1u && gtp->sock1u->sk) {
286 udp_sk(gtp->sock1u->sk)->encap_type = 0;
287 rcu_assign_sk_user_data(gtp->sock1u->sk, NULL);
288 }
289
290 gtp->sock0 = NULL;
291 gtp->sock1u = NULL;
292}
293
294static void gtp_encap_destroy(struct sock *sk)
295{
296 struct gtp_dev *gtp;
297
298 gtp = rcu_dereference_sk_user_data(sk);
299 if (gtp)
300 gtp_encap_disable(gtp);
301}
302
303/* UDP encapsulation receive handler. See net/ipv4/udp.c.
304 * Return codes: 0: success, <0: error, >0: pass up to userspace UDP socket.
305 */
306static int gtp_encap_recv(struct sock *sk, struct sk_buff *skb)
307{
308 struct pcpu_sw_netstats *stats;
309 struct gtp_dev *gtp;
310 bool xnet;
311 int ret;
312
313 gtp = rcu_dereference_sk_user_data(sk);
314 if (!gtp)
315 return 1;
316
317 netdev_dbg(gtp->dev, "encap_recv sk=%p\n", sk);
318
319 xnet = !net_eq(gtp->net, dev_net(gtp->dev));
320
321 switch (udp_sk(sk)->encap_type) {
322 case UDP_ENCAP_GTP0:
323 netdev_dbg(gtp->dev, "received GTP0 packet\n");
324 ret = gtp0_udp_encap_recv(gtp, skb, xnet);
325 break;
326 case UDP_ENCAP_GTP1U:
327 netdev_dbg(gtp->dev, "received GTP1U packet\n");
328 ret = gtp1u_udp_encap_recv(gtp, skb, xnet);
329 break;
330 default:
331 ret = -1; /* Shouldn't happen. */
332 }
333
334 switch (ret) {
335 case 1:
336 netdev_dbg(gtp->dev, "pass up to the process\n");
337 return 1;
338 case 0:
339 netdev_dbg(gtp->dev, "forwarding packet from GGSN to uplink\n");
340 break;
341 case -1:
342 netdev_dbg(gtp->dev, "GTP packet has been dropped\n");
343 kfree_skb(skb);
344 return 0;
345 }
346
347 /* Now that the UDP and the GTP header have been removed, set up the
348 * new network header. This is required by the upper layer to
349 * calculate the transport header.
350 */
351 skb_reset_network_header(skb);
352
353 skb->dev = gtp->dev;
354
355 stats = this_cpu_ptr(gtp->dev->tstats);
356 u64_stats_update_begin(&stats->syncp);
357 stats->rx_packets++;
358 stats->rx_bytes += skb->len;
359 u64_stats_update_end(&stats->syncp);
360
361 netif_rx(skb);
362
363 return 0;
364}
365
366static int gtp_dev_init(struct net_device *dev)
367{
368 struct gtp_dev *gtp = netdev_priv(dev);
369
370 gtp->dev = dev;
371
372 dev->tstats = alloc_percpu(struct pcpu_sw_netstats);
373 if (!dev->tstats)
374 return -ENOMEM;
375
376 return 0;
377}
378
379static void gtp_dev_uninit(struct net_device *dev)
380{
381 struct gtp_dev *gtp = netdev_priv(dev);
382
383 gtp_encap_disable(gtp);
384 free_percpu(dev->tstats);
385}
386
387static struct rtable *ip4_route_output_gtp(struct net *net, struct flowi4 *fl4,
388 const struct sock *sk, __be32 daddr)
389{
390 memset(fl4, 0, sizeof(*fl4));
391 fl4->flowi4_oif = sk->sk_bound_dev_if;
392 fl4->daddr = daddr;
393 fl4->saddr = inet_sk(sk)->inet_saddr;
394 fl4->flowi4_tos = RT_CONN_FLAGS(sk);
395 fl4->flowi4_proto = sk->sk_protocol;
396
397 return ip_route_output_key(net, fl4);
398}
399
400static inline void gtp0_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
401{
402 int payload_len = skb->len;
403 struct gtp0_header *gtp0;
404
405 gtp0 = (struct gtp0_header *) skb_push(skb, sizeof(*gtp0));
406
407 gtp0->flags = 0x1e; /* v0, GTP-non-prime. */
408 gtp0->type = GTP_TPDU;
409 gtp0->length = htons(payload_len);
410 gtp0->seq = htons((atomic_inc_return(&pctx->tx_seq) - 1) % 0xffff);
411 gtp0->flow = htons(pctx->u.v0.flow);
412 gtp0->number = 0xff;
413 gtp0->spare[0] = gtp0->spare[1] = gtp0->spare[2] = 0xff;
414 gtp0->tid = cpu_to_be64(pctx->u.v0.tid);
415}
416
417static inline void gtp1_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
418{
419 int payload_len = skb->len;
420 struct gtp1_header *gtp1;
421
422 gtp1 = (struct gtp1_header *) skb_push(skb, sizeof(*gtp1));
423
424 /* Bits 8 7 6 5 4 3 2 1
425 * +--+--+--+--+--+--+--+--+
426 * |version |PT| 1| E| S|PN|
427 * +--+--+--+--+--+--+--+--+
428 * 0 0 1 1 1 0 0 0
429 */
430 gtp1->flags = 0x38; /* v1, GTP-non-prime. */
431 gtp1->type = GTP_TPDU;
432 gtp1->length = htons(payload_len);
433 gtp1->tid = htonl(pctx->u.v1.o_tei);
434
435 /* TODO: Suppport for extension header, sequence number and N-PDU.
436 * Update the length field if any of them is available.
437 */
438}
439
440struct gtp_pktinfo {
441 struct sock *sk;
442 struct iphdr *iph;
443 struct flowi4 fl4;
444 struct rtable *rt;
445 struct pdp_ctx *pctx;
446 struct net_device *dev;
447 __be16 gtph_port;
448};
449
450static void gtp_push_header(struct sk_buff *skb, struct gtp_pktinfo *pktinfo)
451{
452 switch (pktinfo->pctx->gtp_version) {
453 case GTP_V0:
454 pktinfo->gtph_port = htons(GTP0_PORT);
455 gtp0_push_header(skb, pktinfo->pctx);
456 break;
457 case GTP_V1:
458 pktinfo->gtph_port = htons(GTP1U_PORT);
459 gtp1_push_header(skb, pktinfo->pctx);
460 break;
461 }
462}
463
464static inline void gtp_set_pktinfo_ipv4(struct gtp_pktinfo *pktinfo,
465 struct sock *sk, struct iphdr *iph,
466 struct pdp_ctx *pctx, struct rtable *rt,
467 struct flowi4 *fl4,
468 struct net_device *dev)
469{
470 pktinfo->sk = sk;
471 pktinfo->iph = iph;
472 pktinfo->pctx = pctx;
473 pktinfo->rt = rt;
474 pktinfo->fl4 = *fl4;
475 pktinfo->dev = dev;
476}
477
478static int gtp_build_skb_ip4(struct sk_buff *skb, struct net_device *dev,
479 struct gtp_pktinfo *pktinfo)
480{
481 struct gtp_dev *gtp = netdev_priv(dev);
482 struct pdp_ctx *pctx;
483 struct rtable *rt;
484 struct flowi4 fl4;
485 struct iphdr *iph;
486 struct sock *sk;
487 __be16 df;
488 int mtu;
489
490 /* Read the IP destination address and resolve the PDP context.
491 * Prepend PDP header with TEI/TID from PDP ctx.
492 */
493 iph = ip_hdr(skb);
494 pctx = ipv4_pdp_find(gtp, iph->daddr);
495 if (!pctx) {
496 netdev_dbg(dev, "no PDP ctx found for %pI4, skip\n",
497 &iph->daddr);
498 return -ENOENT;
499 }
500 netdev_dbg(dev, "found PDP context %p\n", pctx);
501
502 switch (pctx->gtp_version) {
503 case GTP_V0:
504 if (gtp->sock0)
505 sk = gtp->sock0->sk;
506 else
507 sk = NULL;
508 break;
509 case GTP_V1:
510 if (gtp->sock1u)
511 sk = gtp->sock1u->sk;
512 else
513 sk = NULL;
514 break;
515 default:
516 return -ENOENT;
517 }
518
519 if (!sk) {
520 netdev_dbg(dev, "no userspace socket is available, skip\n");
521 return -ENOENT;
522 }
523
524 rt = ip4_route_output_gtp(sock_net(sk), &fl4, gtp->sock0->sk,
525 pctx->sgsn_addr_ip4.s_addr);
526 if (IS_ERR(rt)) {
527 netdev_dbg(dev, "no route to SSGN %pI4\n",
528 &pctx->sgsn_addr_ip4.s_addr);
529 dev->stats.tx_carrier_errors++;
530 goto err;
531 }
532
533 if (rt->dst.dev == dev) {
534 netdev_dbg(dev, "circular route to SSGN %pI4\n",
535 &pctx->sgsn_addr_ip4.s_addr);
536 dev->stats.collisions++;
537 goto err_rt;
538 }
539
540 skb_dst_drop(skb);
541
542 /* This is similar to tnl_update_pmtu(). */
543 df = iph->frag_off;
544 if (df) {
545 mtu = dst_mtu(&rt->dst) - dev->hard_header_len -
546 sizeof(struct iphdr) - sizeof(struct udphdr);
547 switch (pctx->gtp_version) {
548 case GTP_V0:
549 mtu -= sizeof(struct gtp0_header);
550 break;
551 case GTP_V1:
552 mtu -= sizeof(struct gtp1_header);
553 break;
554 }
555 } else {
556 mtu = dst_mtu(&rt->dst);
557 }
558
559 rt->dst.ops->update_pmtu(&rt->dst, NULL, skb, mtu);
560
561 if (!skb_is_gso(skb) && (iph->frag_off & htons(IP_DF)) &&
562 mtu < ntohs(iph->tot_len)) {
563 netdev_dbg(dev, "packet too big, fragmentation needed\n");
564 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
565 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
566 htonl(mtu));
567 goto err_rt;
568 }
569
570 gtp_set_pktinfo_ipv4(pktinfo, sk, iph, pctx, rt, &fl4, dev);
571 gtp_push_header(skb, pktinfo);
572
573 return 0;
574err_rt:
575 ip_rt_put(rt);
576err:
577 return -EBADMSG;
578}
579
580static netdev_tx_t gtp_dev_xmit(struct sk_buff *skb, struct net_device *dev)
581{
582 unsigned int proto = ntohs(skb->protocol);
583 struct gtp_pktinfo pktinfo;
584 int err;
585
586 /* Ensure there is sufficient headroom. */
587 if (skb_cow_head(skb, dev->needed_headroom))
588 goto tx_err;
589
590 skb_reset_inner_headers(skb);
591
592 /* PDP context lookups in gtp_build_skb_*() need rcu read-side lock. */
593 rcu_read_lock();
594 switch (proto) {
595 case ETH_P_IP:
596 err = gtp_build_skb_ip4(skb, dev, &pktinfo);
597 break;
598 default:
599 err = -EOPNOTSUPP;
600 break;
601 }
602 rcu_read_unlock();
603
604 if (err < 0)
605 goto tx_err;
606
607 switch (proto) {
608 case ETH_P_IP:
609 netdev_dbg(pktinfo.dev, "gtp -> IP src: %pI4 dst: %pI4\n",
610 &pktinfo.iph->saddr, &pktinfo.iph->daddr);
611 udp_tunnel_xmit_skb(pktinfo.rt, pktinfo.sk, skb,
612 pktinfo.fl4.saddr, pktinfo.fl4.daddr,
613 pktinfo.iph->tos,
614 ip4_dst_hoplimit(&pktinfo.rt->dst),
615 htons(IP_DF),
616 pktinfo.gtph_port, pktinfo.gtph_port,
617 true, false);
618 break;
619 }
620
621 return NETDEV_TX_OK;
622tx_err:
623 dev->stats.tx_errors++;
624 dev_kfree_skb(skb);
625 return NETDEV_TX_OK;
626}
627
628static const struct net_device_ops gtp_netdev_ops = {
629 .ndo_init = gtp_dev_init,
630 .ndo_uninit = gtp_dev_uninit,
631 .ndo_start_xmit = gtp_dev_xmit,
632 .ndo_get_stats64 = ip_tunnel_get_stats64,
633};
634
635static void gtp_link_setup(struct net_device *dev)
636{
637 dev->netdev_ops = &gtp_netdev_ops;
638 dev->destructor = free_netdev;
639
640 dev->hard_header_len = 0;
641 dev->addr_len = 0;
642
643 /* Zero header length. */
644 dev->type = ARPHRD_NONE;
645 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
646
647 dev->priv_flags |= IFF_NO_QUEUE;
648 dev->features |= NETIF_F_LLTX;
649 netif_keep_dst(dev);
650
651 /* Assume largest header, ie. GTPv0. */
652 dev->needed_headroom = LL_MAX_HEADER +
653 sizeof(struct iphdr) +
654 sizeof(struct udphdr) +
655 sizeof(struct gtp0_header);
656}
657
658static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize);
659static void gtp_hashtable_free(struct gtp_dev *gtp);
660static int gtp_encap_enable(struct net_device *dev, struct gtp_dev *gtp,
661 int fd_gtp0, int fd_gtp1, struct net *src_net);
662
663static int gtp_newlink(struct net *src_net, struct net_device *dev,
664 struct nlattr *tb[], struct nlattr *data[])
665{
666 int hashsize, err, fd0, fd1;
667 struct gtp_dev *gtp;
668 struct gtp_net *gn;
669
670 if (!data[IFLA_GTP_FD0] || !data[IFLA_GTP_FD1])
671 return -EINVAL;
672
673 gtp = netdev_priv(dev);
674
675 fd0 = nla_get_u32(data[IFLA_GTP_FD0]);
676 fd1 = nla_get_u32(data[IFLA_GTP_FD1]);
677
678 err = gtp_encap_enable(dev, gtp, fd0, fd1, src_net);
679 if (err < 0)
680 goto out_err;
681
682 if (!data[IFLA_GTP_PDP_HASHSIZE])
683 hashsize = 1024;
684 else
685 hashsize = nla_get_u32(data[IFLA_GTP_PDP_HASHSIZE]);
686
687 err = gtp_hashtable_new(gtp, hashsize);
688 if (err < 0)
689 goto out_encap;
690
691 err = register_netdevice(dev);
692 if (err < 0) {
693 netdev_dbg(dev, "failed to register new netdev %d\n", err);
694 goto out_hashtable;
695 }
696
697 gn = net_generic(dev_net(dev), gtp_net_id);
698 list_add_rcu(&gtp->list, &gn->gtp_dev_list);
699
700 netdev_dbg(dev, "registered new GTP interface\n");
701
702 return 0;
703
704out_hashtable:
705 gtp_hashtable_free(gtp);
706out_encap:
707 gtp_encap_disable(gtp);
708out_err:
709 return err;
710}
711
712static void gtp_dellink(struct net_device *dev, struct list_head *head)
713{
714 struct gtp_dev *gtp = netdev_priv(dev);
715
716 gtp_encap_disable(gtp);
717 gtp_hashtable_free(gtp);
718 list_del_rcu(&gtp->list);
719 unregister_netdevice_queue(dev, head);
720}
721
722static const struct nla_policy gtp_policy[IFLA_GTP_MAX + 1] = {
723 [IFLA_GTP_FD0] = { .type = NLA_U32 },
724 [IFLA_GTP_FD1] = { .type = NLA_U32 },
725 [IFLA_GTP_PDP_HASHSIZE] = { .type = NLA_U32 },
726};
727
728static int gtp_validate(struct nlattr *tb[], struct nlattr *data[])
729{
730 if (!data)
731 return -EINVAL;
732
733 return 0;
734}
735
736static size_t gtp_get_size(const struct net_device *dev)
737{
738 return nla_total_size(sizeof(__u32)); /* IFLA_GTP_PDP_HASHSIZE */
739}
740
741static int gtp_fill_info(struct sk_buff *skb, const struct net_device *dev)
742{
743 struct gtp_dev *gtp = netdev_priv(dev);
744
745 if (nla_put_u32(skb, IFLA_GTP_PDP_HASHSIZE, gtp->hash_size))
746 goto nla_put_failure;
747
748 return 0;
749
750nla_put_failure:
751 return -EMSGSIZE;
752}
753
754static struct rtnl_link_ops gtp_link_ops __read_mostly = {
755 .kind = "gtp",
756 .maxtype = IFLA_GTP_MAX,
757 .policy = gtp_policy,
758 .priv_size = sizeof(struct gtp_dev),
759 .setup = gtp_link_setup,
760 .validate = gtp_validate,
761 .newlink = gtp_newlink,
762 .dellink = gtp_dellink,
763 .get_size = gtp_get_size,
764 .fill_info = gtp_fill_info,
765};
766
767static struct net *gtp_genl_get_net(struct net *src_net, struct nlattr *tb[])
768{
769 struct net *net;
770
771 /* Examine the link attributes and figure out which network namespace
772 * we are talking about.
773 */
774 if (tb[GTPA_NET_NS_FD])
775 net = get_net_ns_by_fd(nla_get_u32(tb[GTPA_NET_NS_FD]));
776 else
777 net = get_net(src_net);
778
779 return net;
780}
781
782static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize)
783{
784 int i;
785
786 gtp->addr_hash = kmalloc(sizeof(struct hlist_head) * hsize, GFP_KERNEL);
787 if (gtp->addr_hash == NULL)
788 return -ENOMEM;
789
790 gtp->tid_hash = kmalloc(sizeof(struct hlist_head) * hsize, GFP_KERNEL);
791 if (gtp->tid_hash == NULL)
792 goto err1;
793
794 gtp->hash_size = hsize;
795
796 for (i = 0; i < hsize; i++) {
797 INIT_HLIST_HEAD(&gtp->addr_hash[i]);
798 INIT_HLIST_HEAD(&gtp->tid_hash[i]);
799 }
800 return 0;
801err1:
802 kfree(gtp->addr_hash);
803 return -ENOMEM;
804}
805
806static void gtp_hashtable_free(struct gtp_dev *gtp)
807{
808 struct pdp_ctx *pctx;
809 int i;
810
811 for (i = 0; i < gtp->hash_size; i++) {
812 hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid) {
813 hlist_del_rcu(&pctx->hlist_tid);
814 hlist_del_rcu(&pctx->hlist_addr);
815 kfree_rcu(pctx, rcu_head);
816 }
817 }
818 synchronize_rcu();
819 kfree(gtp->addr_hash);
820 kfree(gtp->tid_hash);
821}
822
823static int gtp_encap_enable(struct net_device *dev, struct gtp_dev *gtp,
824 int fd_gtp0, int fd_gtp1, struct net *src_net)
825{
826 struct udp_tunnel_sock_cfg tuncfg = {NULL};
827 struct socket *sock0, *sock1u;
828 int err;
829
830 netdev_dbg(dev, "enable gtp on %d, %d\n", fd_gtp0, fd_gtp1);
831
832 sock0 = sockfd_lookup(fd_gtp0, &err);
833 if (sock0 == NULL) {
834 netdev_dbg(dev, "socket fd=%d not found (gtp0)\n", fd_gtp0);
835 return -ENOENT;
836 }
837
838 if (sock0->sk->sk_protocol != IPPROTO_UDP) {
839 netdev_dbg(dev, "socket fd=%d not UDP\n", fd_gtp0);
840 err = -EINVAL;
841 goto err1;
842 }
843
844 sock1u = sockfd_lookup(fd_gtp1, &err);
845 if (sock1u == NULL) {
846 netdev_dbg(dev, "socket fd=%d not found (gtp1u)\n", fd_gtp1);
847 err = -ENOENT;
848 goto err1;
849 }
850
851 if (sock1u->sk->sk_protocol != IPPROTO_UDP) {
852 netdev_dbg(dev, "socket fd=%d not UDP\n", fd_gtp1);
853 err = -EINVAL;
854 goto err2;
855 }
856
857 netdev_dbg(dev, "enable gtp on %p, %p\n", sock0, sock1u);
858
859 gtp->sock0 = sock0;
860 gtp->sock1u = sock1u;
861 gtp->net = src_net;
862
863 tuncfg.sk_user_data = gtp;
864 tuncfg.encap_rcv = gtp_encap_recv;
865 tuncfg.encap_destroy = gtp_encap_destroy;
866
867 tuncfg.encap_type = UDP_ENCAP_GTP0;
868 setup_udp_tunnel_sock(sock_net(gtp->sock0->sk), gtp->sock0, &tuncfg);
869
870 tuncfg.encap_type = UDP_ENCAP_GTP1U;
871 setup_udp_tunnel_sock(sock_net(gtp->sock1u->sk), gtp->sock1u, &tuncfg);
872
873 err = 0;
874err2:
875 sockfd_put(sock1u);
876err1:
877 sockfd_put(sock0);
878 return err;
879}
880
881static struct net_device *gtp_find_dev(struct net *net, int ifindex)
882{
883 struct gtp_net *gn = net_generic(net, gtp_net_id);
884 struct gtp_dev *gtp;
885
886 list_for_each_entry_rcu(gtp, &gn->gtp_dev_list, list) {
887 if (ifindex == gtp->dev->ifindex)
888 return gtp->dev;
889 }
890 return NULL;
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
920static int ipv4_pdp_add(struct net_device *dev, struct genl_info *info)
921{
922 struct gtp_dev *gtp = netdev_priv(dev);
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
961 ipv4_pdp_fill(pctx, info);
962 atomic_set(&pctx->tx_seq, 0);
963
964 switch (pctx->gtp_version) {
965 case GTP_V0:
966 /* TS 09.60: "The flow label identifies unambiguously a GTP
967 * flow.". We use the tid for this instead, I cannot find a
968 * situation in which this doesn't unambiguosly identify the
969 * PDP context.
970 */
971 hash_tid = gtp0_hashfn(pctx->u.v0.tid) % gtp->hash_size;
972 break;
973 case GTP_V1:
974 hash_tid = gtp1u_hashfn(pctx->u.v1.i_tei) % gtp->hash_size;
975 break;
976 }
977
978 hlist_add_head_rcu(&pctx->hlist_addr, &gtp->addr_hash[hash_ms]);
979 hlist_add_head_rcu(&pctx->hlist_tid, &gtp->tid_hash[hash_tid]);
980
981 switch (pctx->gtp_version) {
982 case GTP_V0:
983 netdev_dbg(dev, "GTPv0-U: new PDP ctx id=%llx ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
984 pctx->u.v0.tid, &pctx->sgsn_addr_ip4,
985 &pctx->ms_addr_ip4, pctx);
986 break;
987 case GTP_V1:
988 netdev_dbg(dev, "GTPv1-U: new PDP ctx id=%x/%x ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
989 pctx->u.v1.i_tei, pctx->u.v1.o_tei,
990 &pctx->sgsn_addr_ip4, &pctx->ms_addr_ip4, pctx);
991 break;
992 }
993
994 return 0;
995}
996
997static int gtp_genl_new_pdp(struct sk_buff *skb, struct genl_info *info)
998{
999 struct net_device *dev;
1000 struct net *net;
1001
1002 if (!info->attrs[GTPA_VERSION] ||
1003 !info->attrs[GTPA_LINK] ||
1004 !info->attrs[GTPA_SGSN_ADDRESS] ||
1005 !info->attrs[GTPA_MS_ADDRESS])
1006 return -EINVAL;
1007
1008 switch (nla_get_u32(info->attrs[GTPA_VERSION])) {
1009 case GTP_V0:
1010 if (!info->attrs[GTPA_TID] ||
1011 !info->attrs[GTPA_FLOW])
1012 return -EINVAL;
1013 break;
1014 case GTP_V1:
1015 if (!info->attrs[GTPA_I_TEI] ||
1016 !info->attrs[GTPA_O_TEI])
1017 return -EINVAL;
1018 break;
1019
1020 default:
1021 return -EINVAL;
1022 }
1023
1024 net = gtp_genl_get_net(sock_net(skb->sk), info->attrs);
1025 if (IS_ERR(net))
1026 return PTR_ERR(net);
1027
1028 /* Check if there's an existing gtpX device to configure */
1029 dev = gtp_find_dev(net, nla_get_u32(info->attrs[GTPA_LINK]));
Pablo Neira27ee4412016-05-12 17:16:31 +02001030 if (dev == NULL) {
1031 put_net(net);
Pablo Neira459aa662016-05-09 00:55:48 +02001032 return -ENODEV;
Pablo Neira27ee4412016-05-12 17:16:31 +02001033 }
1034 put_net(net);
Pablo Neira459aa662016-05-09 00:55:48 +02001035
1036 return ipv4_pdp_add(dev, info);
1037}
1038
1039static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info)
1040{
1041 struct net_device *dev;
1042 struct pdp_ctx *pctx;
1043 struct gtp_dev *gtp;
1044 struct net *net;
1045
1046 if (!info->attrs[GTPA_VERSION] ||
1047 !info->attrs[GTPA_LINK])
1048 return -EINVAL;
1049
1050 net = gtp_genl_get_net(sock_net(skb->sk), info->attrs);
1051 if (IS_ERR(net))
1052 return PTR_ERR(net);
1053
1054 /* Check if there's an existing gtpX device to configure */
1055 dev = gtp_find_dev(net, nla_get_u32(info->attrs[GTPA_LINK]));
Pablo Neira27ee4412016-05-12 17:16:31 +02001056 if (dev == NULL) {
1057 put_net(net);
Pablo Neira459aa662016-05-09 00:55:48 +02001058 return -ENODEV;
Pablo Neira27ee4412016-05-12 17:16:31 +02001059 }
1060 put_net(net);
Pablo Neira459aa662016-05-09 00:55:48 +02001061
1062 gtp = netdev_priv(dev);
1063
1064 switch (nla_get_u32(info->attrs[GTPA_VERSION])) {
1065 case GTP_V0:
1066 if (!info->attrs[GTPA_TID])
1067 return -EINVAL;
1068 pctx = gtp0_pdp_find(gtp, nla_get_u64(info->attrs[GTPA_TID]));
1069 break;
1070 case GTP_V1:
1071 if (!info->attrs[GTPA_I_TEI])
1072 return -EINVAL;
1073 pctx = gtp1_pdp_find(gtp, nla_get_u64(info->attrs[GTPA_I_TEI]));
1074 break;
1075
1076 default:
1077 return -EINVAL;
1078 }
1079
1080 if (pctx == NULL)
1081 return -ENOENT;
1082
1083 if (pctx->gtp_version == GTP_V0)
1084 netdev_dbg(dev, "GTPv0-U: deleting tunnel id = %llx (pdp %p)\n",
1085 pctx->u.v0.tid, pctx);
1086 else if (pctx->gtp_version == GTP_V1)
1087 netdev_dbg(dev, "GTPv1-U: deleting tunnel id = %x/%x (pdp %p)\n",
1088 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
1089
1090 hlist_del_rcu(&pctx->hlist_tid);
1091 hlist_del_rcu(&pctx->hlist_addr);
1092 kfree_rcu(pctx, rcu_head);
1093
1094 return 0;
1095}
1096
1097static struct genl_family gtp_genl_family = {
1098 .id = GENL_ID_GENERATE,
1099 .name = "gtp",
1100 .version = 0,
1101 .hdrsize = 0,
1102 .maxattr = GTPA_MAX,
1103 .netnsok = true,
1104};
1105
1106static int gtp_genl_fill_info(struct sk_buff *skb, u32 snd_portid, u32 snd_seq,
1107 u32 type, struct pdp_ctx *pctx)
1108{
1109 void *genlh;
1110
1111 genlh = genlmsg_put(skb, snd_portid, snd_seq, &gtp_genl_family, 0,
1112 type);
1113 if (genlh == NULL)
1114 goto nlmsg_failure;
1115
1116 if (nla_put_u32(skb, GTPA_VERSION, pctx->gtp_version) ||
1117 nla_put_be32(skb, GTPA_SGSN_ADDRESS, pctx->sgsn_addr_ip4.s_addr) ||
1118 nla_put_be32(skb, GTPA_MS_ADDRESS, pctx->ms_addr_ip4.s_addr))
1119 goto nla_put_failure;
1120
1121 switch (pctx->gtp_version) {
1122 case GTP_V0:
1123 if (nla_put_u64_64bit(skb, GTPA_TID, pctx->u.v0.tid, GTPA_PAD) ||
1124 nla_put_u16(skb, GTPA_FLOW, pctx->u.v0.flow))
1125 goto nla_put_failure;
1126 break;
1127 case GTP_V1:
1128 if (nla_put_u32(skb, GTPA_I_TEI, pctx->u.v1.i_tei) ||
1129 nla_put_u32(skb, GTPA_O_TEI, pctx->u.v1.o_tei))
1130 goto nla_put_failure;
1131 break;
1132 }
1133 genlmsg_end(skb, genlh);
1134 return 0;
1135
1136nlmsg_failure:
1137nla_put_failure:
1138 genlmsg_cancel(skb, genlh);
1139 return -EMSGSIZE;
1140}
1141
1142static int gtp_genl_get_pdp(struct sk_buff *skb, struct genl_info *info)
1143{
1144 struct pdp_ctx *pctx = NULL;
1145 struct net_device *dev;
1146 struct sk_buff *skb2;
1147 struct gtp_dev *gtp;
1148 u32 gtp_version;
1149 struct net *net;
1150 int err;
1151
1152 if (!info->attrs[GTPA_VERSION] ||
1153 !info->attrs[GTPA_LINK])
1154 return -EINVAL;
1155
1156 gtp_version = nla_get_u32(info->attrs[GTPA_VERSION]);
1157 switch (gtp_version) {
1158 case GTP_V0:
1159 case GTP_V1:
1160 break;
1161 default:
1162 return -EINVAL;
1163 }
1164
1165 net = gtp_genl_get_net(sock_net(skb->sk), info->attrs);
1166 if (IS_ERR(net))
1167 return PTR_ERR(net);
1168
1169 /* Check if there's an existing gtpX device to configure */
1170 dev = gtp_find_dev(net, nla_get_u32(info->attrs[GTPA_LINK]));
Pablo Neira27ee4412016-05-12 17:16:31 +02001171 if (dev == NULL) {
1172 put_net(net);
Pablo Neira459aa662016-05-09 00:55:48 +02001173 return -ENODEV;
Pablo Neira27ee4412016-05-12 17:16:31 +02001174 }
1175 put_net(net);
Pablo Neira459aa662016-05-09 00:55:48 +02001176
1177 gtp = netdev_priv(dev);
1178
1179 rcu_read_lock();
1180 if (gtp_version == GTP_V0 &&
1181 info->attrs[GTPA_TID]) {
1182 u64 tid = nla_get_u64(info->attrs[GTPA_TID]);
1183
1184 pctx = gtp0_pdp_find(gtp, tid);
1185 } else if (gtp_version == GTP_V1 &&
1186 info->attrs[GTPA_I_TEI]) {
1187 u32 tid = nla_get_u32(info->attrs[GTPA_I_TEI]);
1188
1189 pctx = gtp1_pdp_find(gtp, tid);
1190 } else if (info->attrs[GTPA_MS_ADDRESS]) {
1191 __be32 ip = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
1192
1193 pctx = ipv4_pdp_find(gtp, ip);
1194 }
1195
1196 if (pctx == NULL) {
1197 err = -ENOENT;
1198 goto err_unlock;
1199 }
1200
1201 skb2 = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
1202 if (skb2 == NULL) {
1203 err = -ENOMEM;
1204 goto err_unlock;
1205 }
1206
1207 err = gtp_genl_fill_info(skb2, NETLINK_CB(skb).portid,
1208 info->snd_seq, info->nlhdr->nlmsg_type, pctx);
1209 if (err < 0)
1210 goto err_unlock_free;
1211
1212 rcu_read_unlock();
1213 return genlmsg_unicast(genl_info_net(info), skb2, info->snd_portid);
1214
1215err_unlock_free:
1216 kfree_skb(skb2);
1217err_unlock:
1218 rcu_read_unlock();
1219 return err;
1220}
1221
1222static int gtp_genl_dump_pdp(struct sk_buff *skb,
1223 struct netlink_callback *cb)
1224{
1225 struct gtp_dev *last_gtp = (struct gtp_dev *)cb->args[2], *gtp;
1226 struct net *net = sock_net(skb->sk);
1227 struct gtp_net *gn = net_generic(net, gtp_net_id);
1228 unsigned long tid = cb->args[1];
1229 int i, k = cb->args[0], ret;
1230 struct pdp_ctx *pctx;
1231
1232 if (cb->args[4])
1233 return 0;
1234
1235 list_for_each_entry_rcu(gtp, &gn->gtp_dev_list, list) {
1236 if (last_gtp && last_gtp != gtp)
1237 continue;
1238 else
1239 last_gtp = NULL;
1240
1241 for (i = k; i < gtp->hash_size; i++) {
1242 hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid) {
1243 if (tid && tid != pctx->u.tid)
1244 continue;
1245 else
1246 tid = 0;
1247
1248 ret = gtp_genl_fill_info(skb,
1249 NETLINK_CB(cb->skb).portid,
1250 cb->nlh->nlmsg_seq,
1251 cb->nlh->nlmsg_type, pctx);
1252 if (ret < 0) {
1253 cb->args[0] = i;
1254 cb->args[1] = pctx->u.tid;
1255 cb->args[2] = (unsigned long)gtp;
1256 goto out;
1257 }
1258 }
1259 }
1260 }
1261 cb->args[4] = 1;
1262out:
1263 return skb->len;
1264}
1265
1266static struct nla_policy gtp_genl_policy[GTPA_MAX + 1] = {
1267 [GTPA_LINK] = { .type = NLA_U32, },
1268 [GTPA_VERSION] = { .type = NLA_U32, },
1269 [GTPA_TID] = { .type = NLA_U64, },
1270 [GTPA_SGSN_ADDRESS] = { .type = NLA_U32, },
1271 [GTPA_MS_ADDRESS] = { .type = NLA_U32, },
1272 [GTPA_FLOW] = { .type = NLA_U16, },
1273 [GTPA_NET_NS_FD] = { .type = NLA_U32, },
1274 [GTPA_I_TEI] = { .type = NLA_U32, },
1275 [GTPA_O_TEI] = { .type = NLA_U32, },
1276};
1277
1278static const struct genl_ops gtp_genl_ops[] = {
1279 {
1280 .cmd = GTP_CMD_NEWPDP,
1281 .doit = gtp_genl_new_pdp,
1282 .policy = gtp_genl_policy,
1283 .flags = GENL_ADMIN_PERM,
1284 },
1285 {
1286 .cmd = GTP_CMD_DELPDP,
1287 .doit = gtp_genl_del_pdp,
1288 .policy = gtp_genl_policy,
1289 .flags = GENL_ADMIN_PERM,
1290 },
1291 {
1292 .cmd = GTP_CMD_GETPDP,
1293 .doit = gtp_genl_get_pdp,
1294 .dumpit = gtp_genl_dump_pdp,
1295 .policy = gtp_genl_policy,
1296 .flags = GENL_ADMIN_PERM,
1297 },
1298};
1299
1300static int __net_init gtp_net_init(struct net *net)
1301{
1302 struct gtp_net *gn = net_generic(net, gtp_net_id);
1303
1304 INIT_LIST_HEAD(&gn->gtp_dev_list);
1305 return 0;
1306}
1307
1308static void __net_exit gtp_net_exit(struct net *net)
1309{
1310 struct gtp_net *gn = net_generic(net, gtp_net_id);
1311 struct gtp_dev *gtp;
1312 LIST_HEAD(list);
1313
1314 rtnl_lock();
1315 list_for_each_entry(gtp, &gn->gtp_dev_list, list)
1316 gtp_dellink(gtp->dev, &list);
1317
1318 unregister_netdevice_many(&list);
1319 rtnl_unlock();
1320}
1321
1322static struct pernet_operations gtp_net_ops = {
1323 .init = gtp_net_init,
1324 .exit = gtp_net_exit,
1325 .id = &gtp_net_id,
1326 .size = sizeof(struct gtp_net),
1327};
1328
1329static int __init gtp_init(void)
1330{
1331 int err;
1332
1333 get_random_bytes(&gtp_h_initval, sizeof(gtp_h_initval));
1334
1335 err = rtnl_link_register(&gtp_link_ops);
1336 if (err < 0)
1337 goto error_out;
1338
1339 err = genl_register_family_with_ops(&gtp_genl_family, gtp_genl_ops);
1340 if (err < 0)
1341 goto unreg_rtnl_link;
1342
1343 err = register_pernet_subsys(&gtp_net_ops);
1344 if (err < 0)
1345 goto unreg_genl_family;
1346
1347 pr_info("GTP module loaded (pdp ctx size %Zd bytes)\n",
1348 sizeof(struct pdp_ctx));
1349 return 0;
1350
1351unreg_genl_family:
1352 genl_unregister_family(&gtp_genl_family);
1353unreg_rtnl_link:
1354 rtnl_link_unregister(&gtp_link_ops);
1355error_out:
1356 pr_err("error loading GTP module loaded\n");
1357 return err;
1358}
1359late_initcall(gtp_init);
1360
1361static void __exit gtp_fini(void)
1362{
1363 unregister_pernet_subsys(&gtp_net_ops);
1364 genl_unregister_family(&gtp_genl_family);
1365 rtnl_link_unregister(&gtp_link_ops);
1366
1367 pr_info("GTP module unloaded\n");
1368}
1369module_exit(gtp_fini);
1370
1371MODULE_LICENSE("GPL");
1372MODULE_AUTHOR("Harald Welte <hwelte@sysmocom.de>");
1373MODULE_DESCRIPTION("Interface driver for GTP encapsulated traffic");
1374MODULE_ALIAS_RTNL_LINK("gtp");