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