blob: 68698457add0af7ca39d12ffe4124de80053c54e [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-or-later
2/* GTP according to GSM TS 09.60 / 3GPP TS 29.060
3 *
4 * (C) 2012-2014 by sysmocom - s.f.m.c. GmbH
5 * (C) 2016 by Pablo Neira Ayuso <pablo@netfilter.org>
6 *
7 * Author: Harald Welte <hwelte@sysmocom.de>
8 * Pablo Neira Ayuso <pablo@netfilter.org>
9 * Andreas Schultz <aschultz@travelping.com>
10 */
11
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14#include <linux/module.h>
15#include <linux/skbuff.h>
16#include <linux/udp.h>
17#include <linux/rculist.h>
18#include <linux/jhash.h>
19#include <linux/if_tunnel.h>
20#include <linux/net.h>
21#include <linux/file.h>
22#include <linux/gtp.h>
23
24#include <net/net_namespace.h>
25#include <net/protocol.h>
26#include <net/ip.h>
27#include <net/udp.h>
28#include <net/udp_tunnel.h>
29#include <net/icmp.h>
30#include <net/xfrm.h>
31#include <net/genetlink.h>
32#include <net/netns/generic.h>
33#include <net/gtp.h>
34
35/* An active session for the subscriber. */
36struct pdp_ctx {
37 struct hlist_node hlist_tid;
38 struct hlist_node hlist_addr;
39
40 union {
41 struct {
42 u64 tid;
43 u16 flow;
44 } v0;
45 struct {
46 u32 i_tei;
47 u32 o_tei;
48 } v1;
49 } u;
50 u8 gtp_version;
51 u16 af;
52
53 struct in_addr ms_addr_ip4;
54 struct in_addr peer_addr_ip4;
55
56 struct sock *sk;
57 struct net_device *dev;
58
59 atomic_t tx_seq;
60 struct rcu_head rcu_head;
61};
62
63/* One instance of the GTP device. */
64struct gtp_dev {
65 struct list_head list;
66
67 struct sock *sk0;
68 struct sock *sk1u;
69
70 struct net_device *dev;
71
72 unsigned int role;
73 unsigned int hash_size;
74 struct hlist_head *tid_hash;
75 struct hlist_head *addr_hash;
76};
77
78static unsigned int gtp_net_id __read_mostly;
79
80struct gtp_net {
81 struct list_head gtp_dev_list;
82};
83
84static u32 gtp_h_initval;
85
86static void pdp_context_delete(struct pdp_ctx *pctx);
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_ms_ipv4(struct sk_buff *skb, struct pdp_ctx *pctx,
154 unsigned int hdrlen, unsigned int role)
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);
162
163 if (role == GTP_ROLE_SGSN)
164 return iph->daddr == pctx->ms_addr_ip4.s_addr;
165 else
166 return iph->saddr == pctx->ms_addr_ip4.s_addr;
167}
168
169/* Check if the inner IP address in this packet is assigned to any
170 * existing mobile subscriber.
171 */
172static bool gtp_check_ms(struct sk_buff *skb, struct pdp_ctx *pctx,
173 unsigned int hdrlen, unsigned int role)
174{
175 switch (ntohs(skb->protocol)) {
176 case ETH_P_IP:
177 return gtp_check_ms_ipv4(skb, pctx, hdrlen, role);
178 }
179 return false;
180}
181
182static int gtp_rx(struct pdp_ctx *pctx, struct sk_buff *skb,
183 unsigned int hdrlen, unsigned int role)
184{
185 struct pcpu_sw_netstats *stats;
186
187 if (!gtp_check_ms(skb, pctx, hdrlen, role)) {
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,
194 !net_eq(sock_net(pctx->sk), dev_net(pctx->dev))))
195 return -1;
196
197 netdev_dbg(pctx->dev, "forwarding packet from GGSN to uplink\n");
198
199 /* Now that the UDP and the GTP header have been removed, set up the
200 * new network header. This is required by the upper layer to
201 * calculate the transport header.
202 */
203 skb_reset_network_header(skb);
204
205 skb->dev = pctx->dev;
206
207 stats = this_cpu_ptr(pctx->dev->tstats);
208 u64_stats_update_begin(&stats->syncp);
209 stats->rx_packets++;
210 stats->rx_bytes += skb->len;
211 u64_stats_update_end(&stats->syncp);
212
213 netif_rx(skb);
214 return 0;
215}
216
217/* 1 means pass up to the stack, -1 means drop and 0 means decapsulated. */
218static int gtp0_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb)
219{
220 unsigned int hdrlen = sizeof(struct udphdr) +
221 sizeof(struct gtp0_header);
222 struct gtp0_header *gtp0;
223 struct pdp_ctx *pctx;
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
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);
239 return 1;
240 }
241
242 return gtp_rx(pctx, skb, hdrlen, gtp->role);
243}
244
245static int gtp1u_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb)
246{
247 unsigned int hdrlen = sizeof(struct udphdr) +
248 sizeof(struct gtp1_header);
249 struct gtp1_header *gtp1;
250 struct pdp_ctx *pctx;
251
252 if (!pskb_may_pull(skb, hdrlen))
253 return -1;
254
255 gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
256
257 if ((gtp1->flags >> 5) != GTP_V1)
258 return 1;
259
260 if (gtp1->type != GTP_TPDU)
261 return 1;
262
263 /* From 29.060: "This field shall be present if and only if any one or
264 * more of the S, PN and E flags are set.".
265 *
266 * If any of the bit is set, then the remaining ones also have to be
267 * set.
268 */
269 if (gtp1->flags & GTP1_F_MASK)
270 hdrlen += 4;
271
272 /* Make sure the header is larger enough, including extensions. */
273 if (!pskb_may_pull(skb, hdrlen))
274 return -1;
275
276 gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
277
278 pctx = gtp1_pdp_find(gtp, ntohl(gtp1->tid));
279 if (!pctx) {
280 netdev_dbg(gtp->dev, "No PDP ctx to decap skb=%p\n", skb);
281 return 1;
282 }
283
284 return gtp_rx(pctx, skb, hdrlen, gtp->role);
285}
286
287static void __gtp_encap_destroy(struct sock *sk)
288{
289 struct gtp_dev *gtp;
290
291 lock_sock(sk);
292 gtp = sk->sk_user_data;
293 if (gtp) {
294 if (gtp->sk0 == sk)
295 gtp->sk0 = NULL;
296 else
297 gtp->sk1u = NULL;
298 udp_sk(sk)->encap_type = 0;
299 rcu_assign_sk_user_data(sk, NULL);
300 release_sock(sk);
301 sock_put(sk);
302 return;
303 }
304 release_sock(sk);
305}
306
307static void gtp_encap_destroy(struct sock *sk)
308{
309 rtnl_lock();
310 __gtp_encap_destroy(sk);
311 rtnl_unlock();
312}
313
314static void gtp_encap_disable_sock(struct sock *sk)
315{
316 if (!sk)
317 return;
318
319 __gtp_encap_destroy(sk);
320}
321
322static void gtp_encap_disable(struct gtp_dev *gtp)
323{
324 gtp_encap_disable_sock(gtp->sk0);
325 gtp_encap_disable_sock(gtp->sk1u);
326}
327
328/* UDP encapsulation receive handler. See net/ipv4/udp.c.
329 * Return codes: 0: success, <0: error, >0: pass up to userspace UDP socket.
330 */
331static int gtp_encap_recv(struct sock *sk, struct sk_buff *skb)
332{
333 struct gtp_dev *gtp;
334 int ret = 0;
335
336 gtp = rcu_dereference_sk_user_data(sk);
337 if (!gtp)
338 return 1;
339
340 netdev_dbg(gtp->dev, "encap_recv sk=%p\n", sk);
341
342 switch (udp_sk(sk)->encap_type) {
343 case UDP_ENCAP_GTP0:
344 netdev_dbg(gtp->dev, "received GTP0 packet\n");
345 ret = gtp0_udp_encap_recv(gtp, skb);
346 break;
347 case UDP_ENCAP_GTP1U:
348 netdev_dbg(gtp->dev, "received GTP1U packet\n");
349 ret = gtp1u_udp_encap_recv(gtp, skb);
350 break;
351 default:
352 ret = -1; /* Shouldn't happen. */
353 }
354
355 switch (ret) {
356 case 1:
357 netdev_dbg(gtp->dev, "pass up to the process\n");
358 break;
359 case 0:
360 break;
361 case -1:
362 netdev_dbg(gtp->dev, "GTP packet has been dropped\n");
363 kfree_skb(skb);
364 ret = 0;
365 break;
366 }
367
368 return ret;
369}
370
371static int gtp_dev_init(struct net_device *dev)
372{
373 struct gtp_dev *gtp = netdev_priv(dev);
374
375 gtp->dev = dev;
376
377 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
378 if (!dev->tstats)
379 return -ENOMEM;
380
381 return 0;
382}
383
384static void gtp_dev_uninit(struct net_device *dev)
385{
386 struct gtp_dev *gtp = netdev_priv(dev);
387
388 gtp_encap_disable(gtp);
389 free_percpu(dev->tstats);
390}
391
392static struct rtable *ip4_route_output_gtp(struct flowi4 *fl4,
393 const struct sock *sk,
394 __be32 daddr)
395{
396 memset(fl4, 0, sizeof(*fl4));
397 fl4->flowi4_oif = sk->sk_bound_dev_if;
398 fl4->daddr = daddr;
399 fl4->saddr = inet_sk(sk)->inet_saddr;
400 fl4->flowi4_tos = RT_CONN_FLAGS(sk);
401 fl4->flowi4_proto = sk->sk_protocol;
402
403 return ip_route_output_key(sock_net(sk), fl4);
404}
405
406static inline void gtp0_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
407{
408 int payload_len = skb->len;
409 struct gtp0_header *gtp0;
410
411 gtp0 = skb_push(skb, sizeof(*gtp0));
412
413 gtp0->flags = 0x1e; /* v0, GTP-non-prime. */
414 gtp0->type = GTP_TPDU;
415 gtp0->length = htons(payload_len);
416 gtp0->seq = htons((atomic_inc_return(&pctx->tx_seq) - 1) % 0xffff);
417 gtp0->flow = htons(pctx->u.v0.flow);
418 gtp0->number = 0xff;
419 gtp0->spare[0] = gtp0->spare[1] = gtp0->spare[2] = 0xff;
420 gtp0->tid = cpu_to_be64(pctx->u.v0.tid);
421}
422
423static inline void gtp1_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
424{
425 int payload_len = skb->len;
426 struct gtp1_header *gtp1;
427
428 gtp1 = skb_push(skb, sizeof(*gtp1));
429
430 /* Bits 8 7 6 5 4 3 2 1
431 * +--+--+--+--+--+--+--+--+
432 * |version |PT| 0| E| S|PN|
433 * +--+--+--+--+--+--+--+--+
434 * 0 0 1 1 1 0 0 0
435 */
436 gtp1->flags = 0x30; /* v1, GTP-non-prime. */
437 gtp1->type = GTP_TPDU;
438 gtp1->length = htons(payload_len);
439 gtp1->tid = htonl(pctx->u.v1.o_tei);
440
441 /* TODO: Suppport for extension header, sequence number and N-PDU.
442 * Update the length field if any of them is available.
443 */
444}
445
446struct gtp_pktinfo {
447 struct sock *sk;
448 struct iphdr *iph;
449 struct flowi4 fl4;
450 struct rtable *rt;
451 struct pdp_ctx *pctx;
452 struct net_device *dev;
453 __be16 gtph_port;
454};
455
456static void gtp_push_header(struct sk_buff *skb, struct gtp_pktinfo *pktinfo)
457{
458 switch (pktinfo->pctx->gtp_version) {
459 case GTP_V0:
460 pktinfo->gtph_port = htons(GTP0_PORT);
461 gtp0_push_header(skb, pktinfo->pctx);
462 break;
463 case GTP_V1:
464 pktinfo->gtph_port = htons(GTP1U_PORT);
465 gtp1_push_header(skb, pktinfo->pctx);
466 break;
467 }
468}
469
470static inline void gtp_set_pktinfo_ipv4(struct gtp_pktinfo *pktinfo,
471 struct sock *sk, struct iphdr *iph,
472 struct pdp_ctx *pctx, struct rtable *rt,
473 struct flowi4 *fl4,
474 struct net_device *dev)
475{
476 pktinfo->sk = sk;
477 pktinfo->iph = iph;
478 pktinfo->pctx = pctx;
479 pktinfo->rt = rt;
480 pktinfo->fl4 = *fl4;
481 pktinfo->dev = dev;
482}
483
484static int gtp_build_skb_ip4(struct sk_buff *skb, struct net_device *dev,
485 struct gtp_pktinfo *pktinfo)
486{
487 struct gtp_dev *gtp = netdev_priv(dev);
488 struct pdp_ctx *pctx;
489 struct rtable *rt;
490 struct flowi4 fl4;
491 struct iphdr *iph;
492 __be16 df;
493 int mtu;
494
495 /* Read the IP destination address and resolve the PDP context.
496 * Prepend PDP header with TEI/TID from PDP ctx.
497 */
498 iph = ip_hdr(skb);
499 if (gtp->role == GTP_ROLE_SGSN)
500 pctx = ipv4_pdp_find(gtp, iph->saddr);
501 else
502 pctx = ipv4_pdp_find(gtp, iph->daddr);
503
504 if (!pctx) {
505 netdev_dbg(dev, "no PDP ctx found for %pI4, skip\n",
506 &iph->daddr);
507 return -ENOENT;
508 }
509 netdev_dbg(dev, "found PDP context %p\n", pctx);
510
511 rt = ip4_route_output_gtp(&fl4, pctx->sk, pctx->peer_addr_ip4.s_addr);
512 if (IS_ERR(rt)) {
513 netdev_dbg(dev, "no route to SSGN %pI4\n",
514 &pctx->peer_addr_ip4.s_addr);
515 dev->stats.tx_carrier_errors++;
516 goto err;
517 }
518
519 if (rt->dst.dev == dev) {
520 netdev_dbg(dev, "circular route to SSGN %pI4\n",
521 &pctx->peer_addr_ip4.s_addr);
522 dev->stats.collisions++;
523 goto err_rt;
524 }
525
526 skb_dst_drop(skb);
527
528 /* This is similar to tnl_update_pmtu(). */
529 df = iph->frag_off;
530 if (df) {
531 mtu = dst_mtu(&rt->dst) - dev->hard_header_len -
532 sizeof(struct iphdr) - sizeof(struct udphdr);
533 switch (pctx->gtp_version) {
534 case GTP_V0:
535 mtu -= sizeof(struct gtp0_header);
536 break;
537 case GTP_V1:
538 mtu -= sizeof(struct gtp1_header);
539 break;
540 }
541 } else {
542 mtu = dst_mtu(&rt->dst);
543 }
544
545 rt->dst.ops->update_pmtu(&rt->dst, NULL, skb, mtu, false);
546
547 if (iph->frag_off & htons(IP_DF) &&
548 ((!skb_is_gso(skb) && skb->len > mtu) ||
549 (skb_is_gso(skb) && !skb_gso_validate_network_len(skb, mtu)))) {
550 netdev_dbg(dev, "packet too big, fragmentation needed\n");
551 icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
552 htonl(mtu));
553 goto err_rt;
554 }
555
556 gtp_set_pktinfo_ipv4(pktinfo, pctx->sk, iph, pctx, rt, &fl4, dev);
557 gtp_push_header(skb, pktinfo);
558
559 return 0;
560err_rt:
561 ip_rt_put(rt);
562err:
563 return -EBADMSG;
564}
565
566static netdev_tx_t gtp_dev_xmit(struct sk_buff *skb, struct net_device *dev)
567{
568 unsigned int proto = ntohs(skb->protocol);
569 struct gtp_pktinfo pktinfo;
570 int err;
571
572 /* Ensure there is sufficient headroom. */
573 if (skb_cow_head(skb, dev->needed_headroom))
574 goto tx_err;
575
576 if (!pskb_inet_may_pull(skb))
577 goto tx_err;
578
579 skb_reset_inner_headers(skb);
580
581 /* PDP context lookups in gtp_build_skb_*() need rcu read-side lock. */
582 rcu_read_lock();
583 switch (proto) {
584 case ETH_P_IP:
585 err = gtp_build_skb_ip4(skb, dev, &pktinfo);
586 break;
587 default:
588 err = -EOPNOTSUPP;
589 break;
590 }
591 rcu_read_unlock();
592
593 if (err < 0)
594 goto tx_err;
595
596 switch (proto) {
597 case ETH_P_IP:
598 netdev_dbg(pktinfo.dev, "gtp -> IP src: %pI4 dst: %pI4\n",
599 &pktinfo.iph->saddr, &pktinfo.iph->daddr);
600 udp_tunnel_xmit_skb(pktinfo.rt, pktinfo.sk, skb,
601 pktinfo.fl4.saddr, pktinfo.fl4.daddr,
602 pktinfo.iph->tos,
603 ip4_dst_hoplimit(&pktinfo.rt->dst),
604 0,
605 pktinfo.gtph_port, pktinfo.gtph_port,
606 true, false);
607 break;
608 }
609
610 return NETDEV_TX_OK;
611tx_err:
612 dev->stats.tx_errors++;
613 dev_kfree_skb(skb);
614 return NETDEV_TX_OK;
615}
616
617static const struct net_device_ops gtp_netdev_ops = {
618 .ndo_init = gtp_dev_init,
619 .ndo_uninit = gtp_dev_uninit,
620 .ndo_start_xmit = gtp_dev_xmit,
621 .ndo_get_stats64 = ip_tunnel_get_stats64,
622};
623
624static void gtp_link_setup(struct net_device *dev)
625{
626 dev->netdev_ops = &gtp_netdev_ops;
627 dev->needs_free_netdev = true;
628
629 dev->hard_header_len = 0;
630 dev->addr_len = 0;
631
632 /* Zero header length. */
633 dev->type = ARPHRD_NONE;
634 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
635
636 dev->priv_flags |= IFF_NO_QUEUE;
637 dev->features |= NETIF_F_LLTX;
638 netif_keep_dst(dev);
639
640 /* Assume largest header, ie. GTPv0. */
641 dev->needed_headroom = LL_MAX_HEADER +
642 sizeof(struct iphdr) +
643 sizeof(struct udphdr) +
644 sizeof(struct gtp0_header);
645}
646
647static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize);
648static int gtp_encap_enable(struct gtp_dev *gtp, struct nlattr *data[]);
649
650static void gtp_destructor(struct net_device *dev)
651{
652 struct gtp_dev *gtp = netdev_priv(dev);
653
654 kfree(gtp->addr_hash);
655 kfree(gtp->tid_hash);
656}
657
658static int gtp_newlink(struct net *src_net, struct net_device *dev,
659 struct nlattr *tb[], struct nlattr *data[],
660 struct netlink_ext_ack *extack)
661{
662 struct gtp_dev *gtp;
663 struct gtp_net *gn;
664 int hashsize, err;
665
666 if (!data[IFLA_GTP_FD0] && !data[IFLA_GTP_FD1])
667 return -EINVAL;
668
669 gtp = netdev_priv(dev);
670
671 if (!data[IFLA_GTP_PDP_HASHSIZE]) {
672 hashsize = 1024;
673 } else {
674 hashsize = nla_get_u32(data[IFLA_GTP_PDP_HASHSIZE]);
675 if (!hashsize)
676 hashsize = 1024;
677 }
678
679 err = gtp_hashtable_new(gtp, hashsize);
680 if (err < 0)
681 return err;
682
683 err = gtp_encap_enable(gtp, data);
684 if (err < 0)
685 goto out_hashtable;
686
687 err = register_netdevice(dev);
688 if (err < 0) {
689 netdev_dbg(dev, "failed to register new netdev %d\n", err);
690 goto out_encap;
691 }
692
693 gn = net_generic(src_net, gtp_net_id);
694 list_add(&gtp->list, &gn->gtp_dev_list);
695 dev->priv_destructor = gtp_destructor;
696
697 netdev_dbg(dev, "registered new GTP interface\n");
698
699 return 0;
700
701out_encap:
702 gtp_encap_disable(gtp);
703out_hashtable:
704 kfree(gtp->addr_hash);
705 kfree(gtp->tid_hash);
706 return err;
707}
708
709static void gtp_dellink(struct net_device *dev, struct list_head *head)
710{
711 struct gtp_dev *gtp = netdev_priv(dev);
712 struct hlist_node *next;
713 struct pdp_ctx *pctx;
714 int i;
715
716 for (i = 0; i < gtp->hash_size; i++)
717 hlist_for_each_entry_safe(pctx, next, &gtp->tid_hash[i], hlist_tid)
718 pdp_context_delete(pctx);
719
720 list_del(&gtp->list);
721 unregister_netdevice_queue(dev, head);
722}
723
724static const struct nla_policy gtp_policy[IFLA_GTP_MAX + 1] = {
725 [IFLA_GTP_FD0] = { .type = NLA_U32 },
726 [IFLA_GTP_FD1] = { .type = NLA_U32 },
727 [IFLA_GTP_PDP_HASHSIZE] = { .type = NLA_U32 },
728 [IFLA_GTP_ROLE] = { .type = NLA_U32 },
729};
730
731static int gtp_validate(struct nlattr *tb[], struct nlattr *data[],
732 struct netlink_ext_ack *extack)
733{
734 if (!data)
735 return -EINVAL;
736
737 return 0;
738}
739
740static size_t gtp_get_size(const struct net_device *dev)
741{
742 return nla_total_size(sizeof(__u32)); /* IFLA_GTP_PDP_HASHSIZE */
743}
744
745static int gtp_fill_info(struct sk_buff *skb, const struct net_device *dev)
746{
747 struct gtp_dev *gtp = netdev_priv(dev);
748
749 if (nla_put_u32(skb, IFLA_GTP_PDP_HASHSIZE, gtp->hash_size))
750 goto nla_put_failure;
751
752 return 0;
753
754nla_put_failure:
755 return -EMSGSIZE;
756}
757
758static struct rtnl_link_ops gtp_link_ops __read_mostly = {
759 .kind = "gtp",
760 .maxtype = IFLA_GTP_MAX,
761 .policy = gtp_policy,
762 .priv_size = sizeof(struct gtp_dev),
763 .setup = gtp_link_setup,
764 .validate = gtp_validate,
765 .newlink = gtp_newlink,
766 .dellink = gtp_dellink,
767 .get_size = gtp_get_size,
768 .fill_info = gtp_fill_info,
769};
770
771static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize)
772{
773 int i;
774
775 gtp->addr_hash = kmalloc_array(hsize, sizeof(struct hlist_head),
776 GFP_KERNEL | __GFP_NOWARN);
777 if (gtp->addr_hash == NULL)
778 return -ENOMEM;
779
780 gtp->tid_hash = kmalloc_array(hsize, sizeof(struct hlist_head),
781 GFP_KERNEL | __GFP_NOWARN);
782 if (gtp->tid_hash == NULL)
783 goto err1;
784
785 gtp->hash_size = hsize;
786
787 for (i = 0; i < hsize; i++) {
788 INIT_HLIST_HEAD(&gtp->addr_hash[i]);
789 INIT_HLIST_HEAD(&gtp->tid_hash[i]);
790 }
791 return 0;
792err1:
793 kfree(gtp->addr_hash);
794 return -ENOMEM;
795}
796
797static struct sock *gtp_encap_enable_socket(int fd, int type,
798 struct gtp_dev *gtp)
799{
800 struct udp_tunnel_sock_cfg tuncfg = {NULL};
801 struct socket *sock;
802 struct sock *sk;
803 int err;
804
805 pr_debug("enable gtp on %d, %d\n", fd, type);
806
807 sock = sockfd_lookup(fd, &err);
808 if (!sock) {
809 pr_debug("gtp socket fd=%d not found\n", fd);
810 return ERR_PTR(err);
811 }
812
813 sk = sock->sk;
814 if (sk->sk_protocol != IPPROTO_UDP ||
815 sk->sk_type != SOCK_DGRAM ||
816 (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)) {
817 pr_debug("socket fd=%d not UDP\n", fd);
818 sk = ERR_PTR(-EINVAL);
819 goto out_sock;
820 }
821
822 lock_sock(sk);
823 if (sk->sk_user_data) {
824 sk = ERR_PTR(-EBUSY);
825 goto out_rel_sock;
826 }
827
828 sock_hold(sk);
829
830 tuncfg.sk_user_data = gtp;
831 tuncfg.encap_type = type;
832 tuncfg.encap_rcv = gtp_encap_recv;
833 tuncfg.encap_destroy = gtp_encap_destroy;
834
835 setup_udp_tunnel_sock(sock_net(sock->sk), sock, &tuncfg);
836
837out_rel_sock:
838 release_sock(sock->sk);
839out_sock:
840 sockfd_put(sock);
841 return sk;
842}
843
844static int gtp_encap_enable(struct gtp_dev *gtp, struct nlattr *data[])
845{
846 struct sock *sk1u = NULL;
847 struct sock *sk0 = NULL;
848 unsigned int role = GTP_ROLE_GGSN;
849
850 if (data[IFLA_GTP_FD0]) {
851 int fd0 = nla_get_u32(data[IFLA_GTP_FD0]);
852
853 if (fd0 >= 0) {
854 sk0 = gtp_encap_enable_socket(fd0, UDP_ENCAP_GTP0, gtp);
855 if (IS_ERR(sk0))
856 return PTR_ERR(sk0);
857 }
858 }
859
860 if (data[IFLA_GTP_FD1]) {
861 int fd1 = nla_get_u32(data[IFLA_GTP_FD1]);
862
863 if (fd1 >= 0) {
864 sk1u = gtp_encap_enable_socket(fd1, UDP_ENCAP_GTP1U, gtp);
865 if (IS_ERR(sk1u)) {
866 gtp_encap_disable_sock(sk0);
867 return PTR_ERR(sk1u);
868 }
869 }
870 }
871
872 if (data[IFLA_GTP_ROLE]) {
873 role = nla_get_u32(data[IFLA_GTP_ROLE]);
874 if (role > GTP_ROLE_SGSN) {
875 gtp_encap_disable_sock(sk0);
876 gtp_encap_disable_sock(sk1u);
877 return -EINVAL;
878 }
879 }
880
881 gtp->sk0 = sk0;
882 gtp->sk1u = sk1u;
883 gtp->role = role;
884
885 return 0;
886}
887
888static struct gtp_dev *gtp_find_dev(struct net *src_net, struct nlattr *nla[])
889{
890 struct gtp_dev *gtp = NULL;
891 struct net_device *dev;
892 struct net *net;
893
894 /* Examine the link attributes and figure out which network namespace
895 * we are talking about.
896 */
897 if (nla[GTPA_NET_NS_FD])
898 net = get_net_ns_by_fd(nla_get_u32(nla[GTPA_NET_NS_FD]));
899 else
900 net = get_net(src_net);
901
902 if (IS_ERR(net))
903 return NULL;
904
905 /* Check if there's an existing gtpX device to configure */
906 dev = dev_get_by_index_rcu(net, nla_get_u32(nla[GTPA_LINK]));
907 if (dev && dev->netdev_ops == &gtp_netdev_ops)
908 gtp = netdev_priv(dev);
909
910 put_net(net);
911 return gtp;
912}
913
914static void ipv4_pdp_fill(struct pdp_ctx *pctx, struct genl_info *info)
915{
916 pctx->gtp_version = nla_get_u32(info->attrs[GTPA_VERSION]);
917 pctx->af = AF_INET;
918 pctx->peer_addr_ip4.s_addr =
919 nla_get_be32(info->attrs[GTPA_PEER_ADDRESS]);
920 pctx->ms_addr_ip4.s_addr =
921 nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
922
923 switch (pctx->gtp_version) {
924 case GTP_V0:
925 /* According to TS 09.60, sections 7.5.1 and 7.5.2, the flow
926 * label needs to be the same for uplink and downlink packets,
927 * so let's annotate this.
928 */
929 pctx->u.v0.tid = nla_get_u64(info->attrs[GTPA_TID]);
930 pctx->u.v0.flow = nla_get_u16(info->attrs[GTPA_FLOW]);
931 break;
932 case GTP_V1:
933 pctx->u.v1.i_tei = nla_get_u32(info->attrs[GTPA_I_TEI]);
934 pctx->u.v1.o_tei = nla_get_u32(info->attrs[GTPA_O_TEI]);
935 break;
936 default:
937 break;
938 }
939}
940
941static int gtp_pdp_add(struct gtp_dev *gtp, struct sock *sk,
942 struct genl_info *info)
943{
944 struct pdp_ctx *pctx, *pctx_tid = NULL;
945 struct net_device *dev = gtp->dev;
946 u32 hash_ms, hash_tid = 0;
947 unsigned int version;
948 bool found = false;
949 __be32 ms_addr;
950
951 ms_addr = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
952 hash_ms = ipv4_hashfn(ms_addr) % gtp->hash_size;
953 version = nla_get_u32(info->attrs[GTPA_VERSION]);
954
955 pctx = ipv4_pdp_find(gtp, ms_addr);
956 if (pctx)
957 found = true;
958 if (version == GTP_V0)
959 pctx_tid = gtp0_pdp_find(gtp,
960 nla_get_u64(info->attrs[GTPA_TID]));
961 else if (version == GTP_V1)
962 pctx_tid = gtp1_pdp_find(gtp,
963 nla_get_u32(info->attrs[GTPA_I_TEI]));
964 if (pctx_tid)
965 found = true;
966
967 if (found) {
968 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
969 return -EEXIST;
970 if (info->nlhdr->nlmsg_flags & NLM_F_REPLACE)
971 return -EOPNOTSUPP;
972
973 if (pctx && pctx_tid)
974 return -EEXIST;
975 if (!pctx)
976 pctx = pctx_tid;
977
978 ipv4_pdp_fill(pctx, info);
979
980 if (pctx->gtp_version == GTP_V0)
981 netdev_dbg(dev, "GTPv0-U: update tunnel id = %llx (pdp %p)\n",
982 pctx->u.v0.tid, pctx);
983 else if (pctx->gtp_version == GTP_V1)
984 netdev_dbg(dev, "GTPv1-U: update tunnel id = %x/%x (pdp %p)\n",
985 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
986
987 return 0;
988
989 }
990
991 pctx = kmalloc(sizeof(*pctx), GFP_ATOMIC);
992 if (pctx == NULL)
993 return -ENOMEM;
994
995 sock_hold(sk);
996 pctx->sk = sk;
997 pctx->dev = gtp->dev;
998 ipv4_pdp_fill(pctx, info);
999 atomic_set(&pctx->tx_seq, 0);
1000
1001 switch (pctx->gtp_version) {
1002 case GTP_V0:
1003 /* TS 09.60: "The flow label identifies unambiguously a GTP
1004 * flow.". We use the tid for this instead, I cannot find a
1005 * situation in which this doesn't unambiguosly identify the
1006 * PDP context.
1007 */
1008 hash_tid = gtp0_hashfn(pctx->u.v0.tid) % gtp->hash_size;
1009 break;
1010 case GTP_V1:
1011 hash_tid = gtp1u_hashfn(pctx->u.v1.i_tei) % gtp->hash_size;
1012 break;
1013 }
1014
1015 hlist_add_head_rcu(&pctx->hlist_addr, &gtp->addr_hash[hash_ms]);
1016 hlist_add_head_rcu(&pctx->hlist_tid, &gtp->tid_hash[hash_tid]);
1017
1018 switch (pctx->gtp_version) {
1019 case GTP_V0:
1020 netdev_dbg(dev, "GTPv0-U: new PDP ctx id=%llx ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
1021 pctx->u.v0.tid, &pctx->peer_addr_ip4,
1022 &pctx->ms_addr_ip4, pctx);
1023 break;
1024 case GTP_V1:
1025 netdev_dbg(dev, "GTPv1-U: new PDP ctx id=%x/%x ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
1026 pctx->u.v1.i_tei, pctx->u.v1.o_tei,
1027 &pctx->peer_addr_ip4, &pctx->ms_addr_ip4, pctx);
1028 break;
1029 }
1030
1031 return 0;
1032}
1033
1034static void pdp_context_free(struct rcu_head *head)
1035{
1036 struct pdp_ctx *pctx = container_of(head, struct pdp_ctx, rcu_head);
1037
1038 sock_put(pctx->sk);
1039 kfree(pctx);
1040}
1041
1042static void pdp_context_delete(struct pdp_ctx *pctx)
1043{
1044 hlist_del_rcu(&pctx->hlist_tid);
1045 hlist_del_rcu(&pctx->hlist_addr);
1046 call_rcu(&pctx->rcu_head, pdp_context_free);
1047}
1048
1049static int gtp_genl_new_pdp(struct sk_buff *skb, struct genl_info *info)
1050{
1051 unsigned int version;
1052 struct gtp_dev *gtp;
1053 struct sock *sk;
1054 int err;
1055
1056 if (!info->attrs[GTPA_VERSION] ||
1057 !info->attrs[GTPA_LINK] ||
1058 !info->attrs[GTPA_PEER_ADDRESS] ||
1059 !info->attrs[GTPA_MS_ADDRESS])
1060 return -EINVAL;
1061
1062 version = nla_get_u32(info->attrs[GTPA_VERSION]);
1063
1064 switch (version) {
1065 case GTP_V0:
1066 if (!info->attrs[GTPA_TID] ||
1067 !info->attrs[GTPA_FLOW])
1068 return -EINVAL;
1069 break;
1070 case GTP_V1:
1071 if (!info->attrs[GTPA_I_TEI] ||
1072 !info->attrs[GTPA_O_TEI])
1073 return -EINVAL;
1074 break;
1075
1076 default:
1077 return -EINVAL;
1078 }
1079
1080 rtnl_lock();
1081 rcu_read_lock();
1082
1083 gtp = gtp_find_dev(sock_net(skb->sk), info->attrs);
1084 if (!gtp) {
1085 err = -ENODEV;
1086 goto out_unlock;
1087 }
1088
1089 if (version == GTP_V0)
1090 sk = gtp->sk0;
1091 else if (version == GTP_V1)
1092 sk = gtp->sk1u;
1093 else
1094 sk = NULL;
1095
1096 if (!sk) {
1097 err = -ENODEV;
1098 goto out_unlock;
1099 }
1100
1101 err = gtp_pdp_add(gtp, sk, info);
1102
1103out_unlock:
1104 rcu_read_unlock();
1105 rtnl_unlock();
1106 return err;
1107}
1108
1109static struct pdp_ctx *gtp_find_pdp_by_link(struct net *net,
1110 struct nlattr *nla[])
1111{
1112 struct gtp_dev *gtp;
1113
1114 gtp = gtp_find_dev(net, nla);
1115 if (!gtp)
1116 return ERR_PTR(-ENODEV);
1117
1118 if (nla[GTPA_MS_ADDRESS]) {
1119 __be32 ip = nla_get_be32(nla[GTPA_MS_ADDRESS]);
1120
1121 return ipv4_pdp_find(gtp, ip);
1122 } else if (nla[GTPA_VERSION]) {
1123 u32 gtp_version = nla_get_u32(nla[GTPA_VERSION]);
1124
1125 if (gtp_version == GTP_V0 && nla[GTPA_TID])
1126 return gtp0_pdp_find(gtp, nla_get_u64(nla[GTPA_TID]));
1127 else if (gtp_version == GTP_V1 && nla[GTPA_I_TEI])
1128 return gtp1_pdp_find(gtp, nla_get_u32(nla[GTPA_I_TEI]));
1129 }
1130
1131 return ERR_PTR(-EINVAL);
1132}
1133
1134static struct pdp_ctx *gtp_find_pdp(struct net *net, struct nlattr *nla[])
1135{
1136 struct pdp_ctx *pctx;
1137
1138 if (nla[GTPA_LINK])
1139 pctx = gtp_find_pdp_by_link(net, nla);
1140 else
1141 pctx = ERR_PTR(-EINVAL);
1142
1143 if (!pctx)
1144 pctx = ERR_PTR(-ENOENT);
1145
1146 return pctx;
1147}
1148
1149static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info)
1150{
1151 struct pdp_ctx *pctx;
1152 int err = 0;
1153
1154 if (!info->attrs[GTPA_VERSION])
1155 return -EINVAL;
1156
1157 rcu_read_lock();
1158
1159 pctx = gtp_find_pdp(sock_net(skb->sk), info->attrs);
1160 if (IS_ERR(pctx)) {
1161 err = PTR_ERR(pctx);
1162 goto out_unlock;
1163 }
1164
1165 if (pctx->gtp_version == GTP_V0)
1166 netdev_dbg(pctx->dev, "GTPv0-U: deleting tunnel id = %llx (pdp %p)\n",
1167 pctx->u.v0.tid, pctx);
1168 else if (pctx->gtp_version == GTP_V1)
1169 netdev_dbg(pctx->dev, "GTPv1-U: deleting tunnel id = %x/%x (pdp %p)\n",
1170 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
1171
1172 pdp_context_delete(pctx);
1173
1174out_unlock:
1175 rcu_read_unlock();
1176 return err;
1177}
1178
1179static struct genl_family gtp_genl_family;
1180
1181static int gtp_genl_fill_info(struct sk_buff *skb, u32 snd_portid, u32 snd_seq,
1182 int flags, u32 type, struct pdp_ctx *pctx)
1183{
1184 void *genlh;
1185
1186 genlh = genlmsg_put(skb, snd_portid, snd_seq, &gtp_genl_family, flags,
1187 type);
1188 if (genlh == NULL)
1189 goto nlmsg_failure;
1190
1191 if (nla_put_u32(skb, GTPA_VERSION, pctx->gtp_version) ||
1192 nla_put_u32(skb, GTPA_LINK, pctx->dev->ifindex) ||
1193 nla_put_be32(skb, GTPA_PEER_ADDRESS, pctx->peer_addr_ip4.s_addr) ||
1194 nla_put_be32(skb, GTPA_MS_ADDRESS, pctx->ms_addr_ip4.s_addr))
1195 goto nla_put_failure;
1196
1197 switch (pctx->gtp_version) {
1198 case GTP_V0:
1199 if (nla_put_u64_64bit(skb, GTPA_TID, pctx->u.v0.tid, GTPA_PAD) ||
1200 nla_put_u16(skb, GTPA_FLOW, pctx->u.v0.flow))
1201 goto nla_put_failure;
1202 break;
1203 case GTP_V1:
1204 if (nla_put_u32(skb, GTPA_I_TEI, pctx->u.v1.i_tei) ||
1205 nla_put_u32(skb, GTPA_O_TEI, pctx->u.v1.o_tei))
1206 goto nla_put_failure;
1207 break;
1208 }
1209 genlmsg_end(skb, genlh);
1210 return 0;
1211
1212nlmsg_failure:
1213nla_put_failure:
1214 genlmsg_cancel(skb, genlh);
1215 return -EMSGSIZE;
1216}
1217
1218static int gtp_genl_get_pdp(struct sk_buff *skb, struct genl_info *info)
1219{
1220 struct pdp_ctx *pctx = NULL;
1221 struct sk_buff *skb2;
1222 int err;
1223
1224 if (!info->attrs[GTPA_VERSION])
1225 return -EINVAL;
1226
1227 rcu_read_lock();
1228
1229 pctx = gtp_find_pdp(sock_net(skb->sk), info->attrs);
1230 if (IS_ERR(pctx)) {
1231 err = PTR_ERR(pctx);
1232 goto err_unlock;
1233 }
1234
1235 skb2 = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
1236 if (skb2 == NULL) {
1237 err = -ENOMEM;
1238 goto err_unlock;
1239 }
1240
1241 err = gtp_genl_fill_info(skb2, NETLINK_CB(skb).portid, info->snd_seq,
1242 0, info->nlhdr->nlmsg_type, pctx);
1243 if (err < 0)
1244 goto err_unlock_free;
1245
1246 rcu_read_unlock();
1247 return genlmsg_unicast(genl_info_net(info), skb2, info->snd_portid);
1248
1249err_unlock_free:
1250 kfree_skb(skb2);
1251err_unlock:
1252 rcu_read_unlock();
1253 return err;
1254}
1255
1256static int gtp_genl_dump_pdp(struct sk_buff *skb,
1257 struct netlink_callback *cb)
1258{
1259 struct gtp_dev *last_gtp = (struct gtp_dev *)cb->args[2], *gtp;
1260 int i, j, bucket = cb->args[0], skip = cb->args[1];
1261 struct net *net = sock_net(skb->sk);
1262 struct net_device *dev;
1263 struct pdp_ctx *pctx;
1264
1265 if (cb->args[4])
1266 return 0;
1267
1268 rcu_read_lock();
1269 for_each_netdev_rcu(net, dev) {
1270 if (dev->rtnl_link_ops != &gtp_link_ops)
1271 continue;
1272
1273 gtp = netdev_priv(dev);
1274
1275 if (last_gtp && last_gtp != gtp)
1276 continue;
1277 else
1278 last_gtp = NULL;
1279
1280 for (i = bucket; i < gtp->hash_size; i++) {
1281 j = 0;
1282 hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i],
1283 hlist_tid) {
1284 if (j >= skip &&
1285 gtp_genl_fill_info(skb,
1286 NETLINK_CB(cb->skb).portid,
1287 cb->nlh->nlmsg_seq,
1288 NLM_F_MULTI,
1289 cb->nlh->nlmsg_type, pctx)) {
1290 cb->args[0] = i;
1291 cb->args[1] = j;
1292 cb->args[2] = (unsigned long)gtp;
1293 goto out;
1294 }
1295 j++;
1296 }
1297 skip = 0;
1298 }
1299 bucket = 0;
1300 }
1301 cb->args[4] = 1;
1302out:
1303 rcu_read_unlock();
1304 return skb->len;
1305}
1306
1307static const struct nla_policy gtp_genl_policy[GTPA_MAX + 1] = {
1308 [GTPA_LINK] = { .type = NLA_U32, },
1309 [GTPA_VERSION] = { .type = NLA_U32, },
1310 [GTPA_TID] = { .type = NLA_U64, },
1311 [GTPA_PEER_ADDRESS] = { .type = NLA_U32, },
1312 [GTPA_MS_ADDRESS] = { .type = NLA_U32, },
1313 [GTPA_FLOW] = { .type = NLA_U16, },
1314 [GTPA_NET_NS_FD] = { .type = NLA_U32, },
1315 [GTPA_I_TEI] = { .type = NLA_U32, },
1316 [GTPA_O_TEI] = { .type = NLA_U32, },
1317};
1318
1319static const struct genl_ops gtp_genl_ops[] = {
1320 {
1321 .cmd = GTP_CMD_NEWPDP,
1322 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1323 .doit = gtp_genl_new_pdp,
1324 .flags = GENL_ADMIN_PERM,
1325 },
1326 {
1327 .cmd = GTP_CMD_DELPDP,
1328 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1329 .doit = gtp_genl_del_pdp,
1330 .flags = GENL_ADMIN_PERM,
1331 },
1332 {
1333 .cmd = GTP_CMD_GETPDP,
1334 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1335 .doit = gtp_genl_get_pdp,
1336 .dumpit = gtp_genl_dump_pdp,
1337 .flags = GENL_ADMIN_PERM,
1338 },
1339};
1340
1341static struct genl_family gtp_genl_family __ro_after_init = {
1342 .name = "gtp",
1343 .version = 0,
1344 .hdrsize = 0,
1345 .maxattr = GTPA_MAX,
1346 .policy = gtp_genl_policy,
1347 .netnsok = true,
1348 .module = THIS_MODULE,
1349 .ops = gtp_genl_ops,
1350 .n_ops = ARRAY_SIZE(gtp_genl_ops),
1351};
1352
1353static int __net_init gtp_net_init(struct net *net)
1354{
1355 struct gtp_net *gn = net_generic(net, gtp_net_id);
1356
1357 INIT_LIST_HEAD(&gn->gtp_dev_list);
1358 return 0;
1359}
1360
1361static void __net_exit gtp_net_exit_batch_rtnl(struct list_head *net_list,
1362 struct list_head *dev_to_kill)
1363{
1364 struct net *net;
1365
1366 list_for_each_entry(net, net_list, exit_list) {
1367 struct gtp_net *gn = net_generic(net, gtp_net_id);
1368 struct gtp_dev *gtp, *gtp_next;
1369 struct net_device *dev;
1370
1371 for_each_netdev(net, dev)
1372 if (dev->rtnl_link_ops == &gtp_link_ops)
1373 gtp_dellink(dev, dev_to_kill);
1374
1375 list_for_each_entry_safe(gtp, gtp_next, &gn->gtp_dev_list, list)
1376 gtp_dellink(gtp->dev, dev_to_kill);
1377 }
1378}
1379
1380static struct pernet_operations gtp_net_ops = {
1381 .init = gtp_net_init,
1382 .exit_batch_rtnl = gtp_net_exit_batch_rtnl,
1383 .id = &gtp_net_id,
1384 .size = sizeof(struct gtp_net),
1385};
1386
1387static int __init gtp_init(void)
1388{
1389 int err;
1390
1391 get_random_bytes(&gtp_h_initval, sizeof(gtp_h_initval));
1392
1393 err = register_pernet_subsys(&gtp_net_ops);
1394 if (err < 0)
1395 goto error_out;
1396
1397 err = rtnl_link_register(&gtp_link_ops);
1398 if (err < 0)
1399 goto unreg_pernet_subsys;
1400
1401 err = genl_register_family(&gtp_genl_family);
1402 if (err < 0)
1403 goto unreg_rtnl_link;
1404
1405 pr_info("GTP module loaded (pdp ctx size %zd bytes)\n",
1406 sizeof(struct pdp_ctx));
1407 return 0;
1408
1409unreg_rtnl_link:
1410 rtnl_link_unregister(&gtp_link_ops);
1411unreg_pernet_subsys:
1412 unregister_pernet_subsys(&gtp_net_ops);
1413error_out:
1414 pr_err("error loading GTP module loaded\n");
1415 return err;
1416}
1417late_initcall(gtp_init);
1418
1419static void __exit gtp_fini(void)
1420{
1421 genl_unregister_family(&gtp_genl_family);
1422 rtnl_link_unregister(&gtp_link_ops);
1423 unregister_pernet_subsys(&gtp_net_ops);
1424
1425 pr_info("GTP module unloaded\n");
1426}
1427module_exit(gtp_fini);
1428
1429MODULE_LICENSE("GPL");
1430MODULE_AUTHOR("Harald Welte <hwelte@sysmocom.de>");
1431MODULE_DESCRIPTION("Interface driver for GTP encapsulated traffic");
1432MODULE_ALIAS_RTNL_LINK("gtp");
1433MODULE_ALIAS_GENL_FAMILY("gtp");