Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / gtp.c
blobd73850ebb671f64c59fc67e0c47ff089100415ec
1 /* GTP according to GSM TS 09.60 / 3GPP TS 29.060
3 * (C) 2012-2014 by sysmocom - s.f.m.c. GmbH
4 * (C) 2016 by Pablo Neira Ayuso <pablo@netfilter.org>
6 * Author: Harald Welte <hwelte@sysmocom.de>
7 * Pablo Neira Ayuso <pablo@netfilter.org>
8 * Andreas Schultz <aschultz@travelping.com>
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.
16 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 #include <linux/module.h>
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>
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>
39 /* An active session for the subscriber. */
40 struct pdp_ctx {
41 struct hlist_node hlist_tid;
42 struct hlist_node hlist_addr;
44 union {
45 struct {
46 u64 tid;
47 u16 flow;
48 } v0;
49 struct {
50 u32 i_tei;
51 u32 o_tei;
52 } v1;
53 } u;
54 u8 gtp_version;
55 u16 af;
57 struct in_addr ms_addr_ip4;
58 struct in_addr peer_addr_ip4;
60 struct sock *sk;
61 struct net_device *dev;
63 atomic_t tx_seq;
64 struct rcu_head rcu_head;
67 /* One instance of the GTP device. */
68 struct gtp_dev {
69 struct list_head list;
71 struct sock *sk0;
72 struct sock *sk1u;
74 struct net_device *dev;
76 unsigned int role;
77 unsigned int hash_size;
78 struct hlist_head *tid_hash;
79 struct hlist_head *addr_hash;
82 static unsigned int gtp_net_id __read_mostly;
84 struct gtp_net {
85 struct list_head gtp_dev_list;
88 static u32 gtp_h_initval;
90 static void pdp_context_delete(struct pdp_ctx *pctx);
92 static inline u32 gtp0_hashfn(u64 tid)
94 u32 *tid32 = (u32 *) &tid;
95 return jhash_2words(tid32[0], tid32[1], gtp_h_initval);
98 static inline u32 gtp1u_hashfn(u32 tid)
100 return jhash_1word(tid, gtp_h_initval);
103 static inline u32 ipv4_hashfn(__be32 ip)
105 return jhash_1word((__force u32)ip, gtp_h_initval);
108 /* Resolve a PDP context structure based on the 64bit TID. */
109 static struct pdp_ctx *gtp0_pdp_find(struct gtp_dev *gtp, u64 tid)
111 struct hlist_head *head;
112 struct pdp_ctx *pdp;
114 head = &gtp->tid_hash[gtp0_hashfn(tid) % gtp->hash_size];
116 hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
117 if (pdp->gtp_version == GTP_V0 &&
118 pdp->u.v0.tid == tid)
119 return pdp;
121 return NULL;
124 /* Resolve a PDP context structure based on the 32bit TEI. */
125 static struct pdp_ctx *gtp1_pdp_find(struct gtp_dev *gtp, u32 tid)
127 struct hlist_head *head;
128 struct pdp_ctx *pdp;
130 head = &gtp->tid_hash[gtp1u_hashfn(tid) % gtp->hash_size];
132 hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
133 if (pdp->gtp_version == GTP_V1 &&
134 pdp->u.v1.i_tei == tid)
135 return pdp;
137 return NULL;
140 /* Resolve a PDP context based on IPv4 address of MS. */
141 static struct pdp_ctx *ipv4_pdp_find(struct gtp_dev *gtp, __be32 ms_addr)
143 struct hlist_head *head;
144 struct pdp_ctx *pdp;
146 head = &gtp->addr_hash[ipv4_hashfn(ms_addr) % gtp->hash_size];
148 hlist_for_each_entry_rcu(pdp, head, hlist_addr) {
149 if (pdp->af == AF_INET &&
150 pdp->ms_addr_ip4.s_addr == ms_addr)
151 return pdp;
154 return NULL;
157 static bool gtp_check_ms_ipv4(struct sk_buff *skb, struct pdp_ctx *pctx,
158 unsigned int hdrlen, unsigned int role)
160 struct iphdr *iph;
162 if (!pskb_may_pull(skb, hdrlen + sizeof(struct iphdr)))
163 return false;
165 iph = (struct iphdr *)(skb->data + hdrlen);
167 if (role == GTP_ROLE_SGSN)
168 return iph->daddr == pctx->ms_addr_ip4.s_addr;
169 else
170 return iph->saddr == pctx->ms_addr_ip4.s_addr;
173 /* Check if the inner IP address in this packet is assigned to any
174 * existing mobile subscriber.
176 static bool gtp_check_ms(struct sk_buff *skb, struct pdp_ctx *pctx,
177 unsigned int hdrlen, unsigned int role)
179 switch (ntohs(skb->protocol)) {
180 case ETH_P_IP:
181 return gtp_check_ms_ipv4(skb, pctx, hdrlen, role);
183 return false;
186 static int gtp_rx(struct pdp_ctx *pctx, struct sk_buff *skb,
187 unsigned int hdrlen, unsigned int role)
189 struct pcpu_sw_netstats *stats;
191 if (!gtp_check_ms(skb, pctx, hdrlen, role)) {
192 netdev_dbg(pctx->dev, "No PDP ctx for this MS\n");
193 return 1;
196 /* Get rid of the GTP + UDP headers. */
197 if (iptunnel_pull_header(skb, hdrlen, skb->protocol,
198 !net_eq(sock_net(pctx->sk), dev_net(pctx->dev))))
199 return -1;
201 netdev_dbg(pctx->dev, "forwarding packet from GGSN to uplink\n");
203 /* Now that the UDP and the GTP header have been removed, set up the
204 * new network header. This is required by the upper layer to
205 * calculate the transport header.
207 skb_reset_network_header(skb);
209 skb->dev = pctx->dev;
211 stats = this_cpu_ptr(pctx->dev->tstats);
212 u64_stats_update_begin(&stats->syncp);
213 stats->rx_packets++;
214 stats->rx_bytes += skb->len;
215 u64_stats_update_end(&stats->syncp);
217 netif_rx(skb);
218 return 0;
221 /* 1 means pass up to the stack, -1 means drop and 0 means decapsulated. */
222 static int gtp0_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb)
224 unsigned int hdrlen = sizeof(struct udphdr) +
225 sizeof(struct gtp0_header);
226 struct gtp0_header *gtp0;
227 struct pdp_ctx *pctx;
229 if (!pskb_may_pull(skb, hdrlen))
230 return -1;
232 gtp0 = (struct gtp0_header *)(skb->data + sizeof(struct udphdr));
234 if ((gtp0->flags >> 5) != GTP_V0)
235 return 1;
237 if (gtp0->type != GTP_TPDU)
238 return 1;
240 pctx = gtp0_pdp_find(gtp, be64_to_cpu(gtp0->tid));
241 if (!pctx) {
242 netdev_dbg(gtp->dev, "No PDP ctx to decap skb=%p\n", skb);
243 return 1;
246 return gtp_rx(pctx, skb, hdrlen, gtp->role);
249 static int gtp1u_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb)
251 unsigned int hdrlen = sizeof(struct udphdr) +
252 sizeof(struct gtp1_header);
253 struct gtp1_header *gtp1;
254 struct pdp_ctx *pctx;
256 if (!pskb_may_pull(skb, hdrlen))
257 return -1;
259 gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
261 if ((gtp1->flags >> 5) != GTP_V1)
262 return 1;
264 if (gtp1->type != GTP_TPDU)
265 return 1;
267 /* From 29.060: "This field shall be present if and only if any one or
268 * more of the S, PN and E flags are set.".
270 * If any of the bit is set, then the remaining ones also have to be
271 * set.
273 if (gtp1->flags & GTP1_F_MASK)
274 hdrlen += 4;
276 /* Make sure the header is larger enough, including extensions. */
277 if (!pskb_may_pull(skb, hdrlen))
278 return -1;
280 gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
282 pctx = gtp1_pdp_find(gtp, ntohl(gtp1->tid));
283 if (!pctx) {
284 netdev_dbg(gtp->dev, "No PDP ctx to decap skb=%p\n", skb);
285 return 1;
288 return gtp_rx(pctx, skb, hdrlen, gtp->role);
291 static void __gtp_encap_destroy(struct sock *sk)
293 struct gtp_dev *gtp;
295 lock_sock(sk);
296 gtp = sk->sk_user_data;
297 if (gtp) {
298 if (gtp->sk0 == sk)
299 gtp->sk0 = NULL;
300 else
301 gtp->sk1u = NULL;
302 udp_sk(sk)->encap_type = 0;
303 rcu_assign_sk_user_data(sk, NULL);
304 sock_put(sk);
306 release_sock(sk);
309 static void gtp_encap_destroy(struct sock *sk)
311 rtnl_lock();
312 __gtp_encap_destroy(sk);
313 rtnl_unlock();
316 static void gtp_encap_disable_sock(struct sock *sk)
318 if (!sk)
319 return;
321 __gtp_encap_destroy(sk);
324 static void gtp_encap_disable(struct gtp_dev *gtp)
326 gtp_encap_disable_sock(gtp->sk0);
327 gtp_encap_disable_sock(gtp->sk1u);
330 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
331 * Return codes: 0: success, <0: error, >0: pass up to userspace UDP socket.
333 static int gtp_encap_recv(struct sock *sk, struct sk_buff *skb)
335 struct gtp_dev *gtp;
336 int ret = 0;
338 gtp = rcu_dereference_sk_user_data(sk);
339 if (!gtp)
340 return 1;
342 netdev_dbg(gtp->dev, "encap_recv sk=%p\n", sk);
344 switch (udp_sk(sk)->encap_type) {
345 case UDP_ENCAP_GTP0:
346 netdev_dbg(gtp->dev, "received GTP0 packet\n");
347 ret = gtp0_udp_encap_recv(gtp, skb);
348 break;
349 case UDP_ENCAP_GTP1U:
350 netdev_dbg(gtp->dev, "received GTP1U packet\n");
351 ret = gtp1u_udp_encap_recv(gtp, skb);
352 break;
353 default:
354 ret = -1; /* Shouldn't happen. */
357 switch (ret) {
358 case 1:
359 netdev_dbg(gtp->dev, "pass up to the process\n");
360 break;
361 case 0:
362 break;
363 case -1:
364 netdev_dbg(gtp->dev, "GTP packet has been dropped\n");
365 kfree_skb(skb);
366 ret = 0;
367 break;
370 return ret;
373 static int gtp_dev_init(struct net_device *dev)
375 struct gtp_dev *gtp = netdev_priv(dev);
377 gtp->dev = dev;
379 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
380 if (!dev->tstats)
381 return -ENOMEM;
383 return 0;
386 static void gtp_dev_uninit(struct net_device *dev)
388 struct gtp_dev *gtp = netdev_priv(dev);
390 gtp_encap_disable(gtp);
391 free_percpu(dev->tstats);
394 static struct rtable *ip4_route_output_gtp(struct flowi4 *fl4,
395 const struct sock *sk,
396 __be32 daddr)
398 memset(fl4, 0, sizeof(*fl4));
399 fl4->flowi4_oif = sk->sk_bound_dev_if;
400 fl4->daddr = daddr;
401 fl4->saddr = inet_sk(sk)->inet_saddr;
402 fl4->flowi4_tos = RT_CONN_FLAGS(sk);
403 fl4->flowi4_proto = sk->sk_protocol;
405 return ip_route_output_key(sock_net(sk), fl4);
408 static inline void gtp0_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
410 int payload_len = skb->len;
411 struct gtp0_header *gtp0;
413 gtp0 = skb_push(skb, sizeof(*gtp0));
415 gtp0->flags = 0x1e; /* v0, GTP-non-prime. */
416 gtp0->type = GTP_TPDU;
417 gtp0->length = htons(payload_len);
418 gtp0->seq = htons((atomic_inc_return(&pctx->tx_seq) - 1) % 0xffff);
419 gtp0->flow = htons(pctx->u.v0.flow);
420 gtp0->number = 0xff;
421 gtp0->spare[0] = gtp0->spare[1] = gtp0->spare[2] = 0xff;
422 gtp0->tid = cpu_to_be64(pctx->u.v0.tid);
425 static inline void gtp1_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
427 int payload_len = skb->len;
428 struct gtp1_header *gtp1;
430 gtp1 = skb_push(skb, sizeof(*gtp1));
432 /* Bits 8 7 6 5 4 3 2 1
433 * +--+--+--+--+--+--+--+--+
434 * |version |PT| 0| E| S|PN|
435 * +--+--+--+--+--+--+--+--+
436 * 0 0 1 1 1 0 0 0
438 gtp1->flags = 0x30; /* v1, GTP-non-prime. */
439 gtp1->type = GTP_TPDU;
440 gtp1->length = htons(payload_len);
441 gtp1->tid = htonl(pctx->u.v1.o_tei);
443 /* TODO: Suppport for extension header, sequence number and N-PDU.
444 * Update the length field if any of them is available.
448 struct gtp_pktinfo {
449 struct sock *sk;
450 struct iphdr *iph;
451 struct flowi4 fl4;
452 struct rtable *rt;
453 struct pdp_ctx *pctx;
454 struct net_device *dev;
455 __be16 gtph_port;
458 static void gtp_push_header(struct sk_buff *skb, struct gtp_pktinfo *pktinfo)
460 switch (pktinfo->pctx->gtp_version) {
461 case GTP_V0:
462 pktinfo->gtph_port = htons(GTP0_PORT);
463 gtp0_push_header(skb, pktinfo->pctx);
464 break;
465 case GTP_V1:
466 pktinfo->gtph_port = htons(GTP1U_PORT);
467 gtp1_push_header(skb, pktinfo->pctx);
468 break;
472 static inline void gtp_set_pktinfo_ipv4(struct gtp_pktinfo *pktinfo,
473 struct sock *sk, struct iphdr *iph,
474 struct pdp_ctx *pctx, struct rtable *rt,
475 struct flowi4 *fl4,
476 struct net_device *dev)
478 pktinfo->sk = sk;
479 pktinfo->iph = iph;
480 pktinfo->pctx = pctx;
481 pktinfo->rt = rt;
482 pktinfo->fl4 = *fl4;
483 pktinfo->dev = dev;
486 static int gtp_build_skb_ip4(struct sk_buff *skb, struct net_device *dev,
487 struct gtp_pktinfo *pktinfo)
489 struct gtp_dev *gtp = netdev_priv(dev);
490 struct pdp_ctx *pctx;
491 struct rtable *rt;
492 struct flowi4 fl4;
493 struct iphdr *iph;
494 __be16 df;
495 int mtu;
497 /* Read the IP destination address and resolve the PDP context.
498 * Prepend PDP header with TEI/TID from PDP ctx.
500 iph = ip_hdr(skb);
501 if (gtp->role == GTP_ROLE_SGSN)
502 pctx = ipv4_pdp_find(gtp, iph->saddr);
503 else
504 pctx = ipv4_pdp_find(gtp, iph->daddr);
506 if (!pctx) {
507 netdev_dbg(dev, "no PDP ctx found for %pI4, skip\n",
508 &iph->daddr);
509 return -ENOENT;
511 netdev_dbg(dev, "found PDP context %p\n", pctx);
513 rt = ip4_route_output_gtp(&fl4, pctx->sk, pctx->peer_addr_ip4.s_addr);
514 if (IS_ERR(rt)) {
515 netdev_dbg(dev, "no route to SSGN %pI4\n",
516 &pctx->peer_addr_ip4.s_addr);
517 dev->stats.tx_carrier_errors++;
518 goto err;
521 if (rt->dst.dev == dev) {
522 netdev_dbg(dev, "circular route to SSGN %pI4\n",
523 &pctx->peer_addr_ip4.s_addr);
524 dev->stats.collisions++;
525 goto err_rt;
528 skb_dst_drop(skb);
530 /* This is similar to tnl_update_pmtu(). */
531 df = iph->frag_off;
532 if (df) {
533 mtu = dst_mtu(&rt->dst) - dev->hard_header_len -
534 sizeof(struct iphdr) - sizeof(struct udphdr);
535 switch (pctx->gtp_version) {
536 case GTP_V0:
537 mtu -= sizeof(struct gtp0_header);
538 break;
539 case GTP_V1:
540 mtu -= sizeof(struct gtp1_header);
541 break;
543 } else {
544 mtu = dst_mtu(&rt->dst);
547 rt->dst.ops->update_pmtu(&rt->dst, NULL, skb, mtu, false);
549 if (!skb_is_gso(skb) && (iph->frag_off & htons(IP_DF)) &&
550 mtu < ntohs(iph->tot_len)) {
551 netdev_dbg(dev, "packet too big, fragmentation needed\n");
552 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
553 icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
554 htonl(mtu));
555 goto err_rt;
558 gtp_set_pktinfo_ipv4(pktinfo, pctx->sk, iph, pctx, rt, &fl4, dev);
559 gtp_push_header(skb, pktinfo);
561 return 0;
562 err_rt:
563 ip_rt_put(rt);
564 err:
565 return -EBADMSG;
568 static netdev_tx_t gtp_dev_xmit(struct sk_buff *skb, struct net_device *dev)
570 unsigned int proto = ntohs(skb->protocol);
571 struct gtp_pktinfo pktinfo;
572 int err;
574 /* Ensure there is sufficient headroom. */
575 if (skb_cow_head(skb, dev->needed_headroom))
576 goto tx_err;
578 skb_reset_inner_headers(skb);
580 /* PDP context lookups in gtp_build_skb_*() need rcu read-side lock. */
581 rcu_read_lock();
582 switch (proto) {
583 case ETH_P_IP:
584 err = gtp_build_skb_ip4(skb, dev, &pktinfo);
585 break;
586 default:
587 err = -EOPNOTSUPP;
588 break;
590 rcu_read_unlock();
592 if (err < 0)
593 goto tx_err;
595 switch (proto) {
596 case ETH_P_IP:
597 netdev_dbg(pktinfo.dev, "gtp -> IP src: %pI4 dst: %pI4\n",
598 &pktinfo.iph->saddr, &pktinfo.iph->daddr);
599 udp_tunnel_xmit_skb(pktinfo.rt, pktinfo.sk, skb,
600 pktinfo.fl4.saddr, pktinfo.fl4.daddr,
601 pktinfo.iph->tos,
602 ip4_dst_hoplimit(&pktinfo.rt->dst),
604 pktinfo.gtph_port, pktinfo.gtph_port,
605 true, false);
606 break;
609 return NETDEV_TX_OK;
610 tx_err:
611 dev->stats.tx_errors++;
612 dev_kfree_skb(skb);
613 return NETDEV_TX_OK;
616 static const struct net_device_ops gtp_netdev_ops = {
617 .ndo_init = gtp_dev_init,
618 .ndo_uninit = gtp_dev_uninit,
619 .ndo_start_xmit = gtp_dev_xmit,
620 .ndo_get_stats64 = ip_tunnel_get_stats64,
623 static void gtp_link_setup(struct net_device *dev)
625 dev->netdev_ops = &gtp_netdev_ops;
626 dev->needs_free_netdev = true;
628 dev->hard_header_len = 0;
629 dev->addr_len = 0;
631 /* Zero header length. */
632 dev->type = ARPHRD_NONE;
633 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
635 dev->priv_flags |= IFF_NO_QUEUE;
636 dev->features |= NETIF_F_LLTX;
637 netif_keep_dst(dev);
639 /* Assume largest header, ie. GTPv0. */
640 dev->needed_headroom = LL_MAX_HEADER +
641 sizeof(struct iphdr) +
642 sizeof(struct udphdr) +
643 sizeof(struct gtp0_header);
646 static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize);
647 static int gtp_encap_enable(struct gtp_dev *gtp, struct nlattr *data[]);
649 static void gtp_destructor(struct net_device *dev)
651 struct gtp_dev *gtp = netdev_priv(dev);
653 kfree(gtp->addr_hash);
654 kfree(gtp->tid_hash);
657 static int gtp_newlink(struct net *src_net, struct net_device *dev,
658 struct nlattr *tb[], struct nlattr *data[],
659 struct netlink_ext_ack *extack)
661 struct gtp_dev *gtp;
662 struct gtp_net *gn;
663 int hashsize, err;
665 if (!data[IFLA_GTP_FD0] && !data[IFLA_GTP_FD1])
666 return -EINVAL;
668 gtp = netdev_priv(dev);
670 err = gtp_encap_enable(gtp, data);
671 if (err < 0)
672 return err;
674 if (!data[IFLA_GTP_PDP_HASHSIZE]) {
675 hashsize = 1024;
676 } else {
677 hashsize = nla_get_u32(data[IFLA_GTP_PDP_HASHSIZE]);
678 if (!hashsize)
679 hashsize = 1024;
682 err = gtp_hashtable_new(gtp, hashsize);
683 if (err < 0)
684 goto out_encap;
686 err = register_netdevice(dev);
687 if (err < 0) {
688 netdev_dbg(dev, "failed to register new netdev %d\n", err);
689 goto out_hashtable;
692 gn = net_generic(dev_net(dev), gtp_net_id);
693 list_add_rcu(&gtp->list, &gn->gtp_dev_list);
694 dev->priv_destructor = gtp_destructor;
696 netdev_dbg(dev, "registered new GTP interface\n");
698 return 0;
700 out_hashtable:
701 kfree(gtp->addr_hash);
702 kfree(gtp->tid_hash);
703 out_encap:
704 gtp_encap_disable(gtp);
705 return err;
708 static void gtp_dellink(struct net_device *dev, struct list_head *head)
710 struct gtp_dev *gtp = netdev_priv(dev);
711 struct pdp_ctx *pctx;
712 int i;
714 for (i = 0; i < gtp->hash_size; i++)
715 hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i], hlist_tid)
716 pdp_context_delete(pctx);
718 gtp_encap_disable(gtp);
719 list_del_rcu(&gtp->list);
720 unregister_netdevice_queue(dev, head);
723 static const struct nla_policy gtp_policy[IFLA_GTP_MAX + 1] = {
724 [IFLA_GTP_FD0] = { .type = NLA_U32 },
725 [IFLA_GTP_FD1] = { .type = NLA_U32 },
726 [IFLA_GTP_PDP_HASHSIZE] = { .type = NLA_U32 },
727 [IFLA_GTP_ROLE] = { .type = NLA_U32 },
730 static int gtp_validate(struct nlattr *tb[], struct nlattr *data[],
731 struct netlink_ext_ack *extack)
733 if (!data)
734 return -EINVAL;
736 return 0;
739 static size_t gtp_get_size(const struct net_device *dev)
741 return nla_total_size(sizeof(__u32)); /* IFLA_GTP_PDP_HASHSIZE */
744 static int gtp_fill_info(struct sk_buff *skb, const struct net_device *dev)
746 struct gtp_dev *gtp = netdev_priv(dev);
748 if (nla_put_u32(skb, IFLA_GTP_PDP_HASHSIZE, gtp->hash_size))
749 goto nla_put_failure;
751 return 0;
753 nla_put_failure:
754 return -EMSGSIZE;
757 static struct rtnl_link_ops gtp_link_ops __read_mostly = {
758 .kind = "gtp",
759 .maxtype = IFLA_GTP_MAX,
760 .policy = gtp_policy,
761 .priv_size = sizeof(struct gtp_dev),
762 .setup = gtp_link_setup,
763 .validate = gtp_validate,
764 .newlink = gtp_newlink,
765 .dellink = gtp_dellink,
766 .get_size = gtp_get_size,
767 .fill_info = gtp_fill_info,
770 static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize)
772 int i;
774 gtp->addr_hash = kmalloc_array(hsize, sizeof(struct hlist_head),
775 GFP_KERNEL | __GFP_NOWARN);
776 if (gtp->addr_hash == NULL)
777 return -ENOMEM;
779 gtp->tid_hash = kmalloc_array(hsize, sizeof(struct hlist_head),
780 GFP_KERNEL | __GFP_NOWARN);
781 if (gtp->tid_hash == NULL)
782 goto err1;
784 gtp->hash_size = hsize;
786 for (i = 0; i < hsize; i++) {
787 INIT_HLIST_HEAD(&gtp->addr_hash[i]);
788 INIT_HLIST_HEAD(&gtp->tid_hash[i]);
790 return 0;
791 err1:
792 kfree(gtp->addr_hash);
793 return -ENOMEM;
796 static struct sock *gtp_encap_enable_socket(int fd, int type,
797 struct gtp_dev *gtp)
799 struct udp_tunnel_sock_cfg tuncfg = {NULL};
800 struct socket *sock;
801 struct sock *sk;
802 int err;
804 pr_debug("enable gtp on %d, %d\n", fd, type);
806 sock = sockfd_lookup(fd, &err);
807 if (!sock) {
808 pr_debug("gtp socket fd=%d not found\n", fd);
809 return NULL;
812 sk = sock->sk;
813 if (sk->sk_protocol != IPPROTO_UDP ||
814 sk->sk_type != SOCK_DGRAM ||
815 (sk->sk_family != AF_INET && sk->sk_family != AF_INET6)) {
816 pr_debug("socket fd=%d not UDP\n", fd);
817 sk = ERR_PTR(-EINVAL);
818 goto out_sock;
821 lock_sock(sk);
822 if (sk->sk_user_data) {
823 sk = ERR_PTR(-EBUSY);
824 goto out_rel_sock;
827 sock_hold(sk);
829 tuncfg.sk_user_data = gtp;
830 tuncfg.encap_type = type;
831 tuncfg.encap_rcv = gtp_encap_recv;
832 tuncfg.encap_destroy = gtp_encap_destroy;
834 setup_udp_tunnel_sock(sock_net(sock->sk), sock, &tuncfg);
836 out_rel_sock:
837 release_sock(sock->sk);
838 out_sock:
839 sockfd_put(sock);
840 return sk;
843 static int gtp_encap_enable(struct gtp_dev *gtp, struct nlattr *data[])
845 struct sock *sk1u = NULL;
846 struct sock *sk0 = NULL;
847 unsigned int role = GTP_ROLE_GGSN;
849 if (data[IFLA_GTP_FD0]) {
850 u32 fd0 = nla_get_u32(data[IFLA_GTP_FD0]);
852 sk0 = gtp_encap_enable_socket(fd0, UDP_ENCAP_GTP0, gtp);
853 if (IS_ERR(sk0))
854 return PTR_ERR(sk0);
857 if (data[IFLA_GTP_FD1]) {
858 u32 fd1 = nla_get_u32(data[IFLA_GTP_FD1]);
860 sk1u = gtp_encap_enable_socket(fd1, UDP_ENCAP_GTP1U, gtp);
861 if (IS_ERR(sk1u)) {
862 if (sk0)
863 gtp_encap_disable_sock(sk0);
864 return PTR_ERR(sk1u);
868 if (data[IFLA_GTP_ROLE]) {
869 role = nla_get_u32(data[IFLA_GTP_ROLE]);
870 if (role > GTP_ROLE_SGSN) {
871 if (sk0)
872 gtp_encap_disable_sock(sk0);
873 if (sk1u)
874 gtp_encap_disable_sock(sk1u);
875 return -EINVAL;
879 gtp->sk0 = sk0;
880 gtp->sk1u = sk1u;
881 gtp->role = role;
883 return 0;
886 static struct gtp_dev *gtp_find_dev(struct net *src_net, struct nlattr *nla[])
888 struct gtp_dev *gtp = NULL;
889 struct net_device *dev;
890 struct net *net;
892 /* Examine the link attributes and figure out which network namespace
893 * we are talking about.
895 if (nla[GTPA_NET_NS_FD])
896 net = get_net_ns_by_fd(nla_get_u32(nla[GTPA_NET_NS_FD]));
897 else
898 net = get_net(src_net);
900 if (IS_ERR(net))
901 return NULL;
903 /* Check if there's an existing gtpX device to configure */
904 dev = dev_get_by_index_rcu(net, nla_get_u32(nla[GTPA_LINK]));
905 if (dev && dev->netdev_ops == &gtp_netdev_ops)
906 gtp = netdev_priv(dev);
908 put_net(net);
909 return gtp;
912 static void ipv4_pdp_fill(struct pdp_ctx *pctx, struct genl_info *info)
914 pctx->gtp_version = nla_get_u32(info->attrs[GTPA_VERSION]);
915 pctx->af = AF_INET;
916 pctx->peer_addr_ip4.s_addr =
917 nla_get_be32(info->attrs[GTPA_PEER_ADDRESS]);
918 pctx->ms_addr_ip4.s_addr =
919 nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
921 switch (pctx->gtp_version) {
922 case GTP_V0:
923 /* According to TS 09.60, sections 7.5.1 and 7.5.2, the flow
924 * label needs to be the same for uplink and downlink packets,
925 * so let's annotate this.
927 pctx->u.v0.tid = nla_get_u64(info->attrs[GTPA_TID]);
928 pctx->u.v0.flow = nla_get_u16(info->attrs[GTPA_FLOW]);
929 break;
930 case GTP_V1:
931 pctx->u.v1.i_tei = nla_get_u32(info->attrs[GTPA_I_TEI]);
932 pctx->u.v1.o_tei = nla_get_u32(info->attrs[GTPA_O_TEI]);
933 break;
934 default:
935 break;
939 static int gtp_pdp_add(struct gtp_dev *gtp, struct sock *sk,
940 struct genl_info *info)
942 struct pdp_ctx *pctx, *pctx_tid = NULL;
943 struct net_device *dev = gtp->dev;
944 u32 hash_ms, hash_tid = 0;
945 unsigned int version;
946 bool found = false;
947 __be32 ms_addr;
949 ms_addr = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
950 hash_ms = ipv4_hashfn(ms_addr) % gtp->hash_size;
951 version = nla_get_u32(info->attrs[GTPA_VERSION]);
953 pctx = ipv4_pdp_find(gtp, ms_addr);
954 if (pctx)
955 found = true;
956 if (version == GTP_V0)
957 pctx_tid = gtp0_pdp_find(gtp,
958 nla_get_u64(info->attrs[GTPA_TID]));
959 else if (version == GTP_V1)
960 pctx_tid = gtp1_pdp_find(gtp,
961 nla_get_u32(info->attrs[GTPA_I_TEI]));
962 if (pctx_tid)
963 found = true;
965 if (found) {
966 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
967 return -EEXIST;
968 if (info->nlhdr->nlmsg_flags & NLM_F_REPLACE)
969 return -EOPNOTSUPP;
971 if (pctx && pctx_tid)
972 return -EEXIST;
973 if (!pctx)
974 pctx = pctx_tid;
976 ipv4_pdp_fill(pctx, info);
978 if (pctx->gtp_version == GTP_V0)
979 netdev_dbg(dev, "GTPv0-U: update tunnel id = %llx (pdp %p)\n",
980 pctx->u.v0.tid, pctx);
981 else if (pctx->gtp_version == GTP_V1)
982 netdev_dbg(dev, "GTPv1-U: update tunnel id = %x/%x (pdp %p)\n",
983 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
985 return 0;
989 pctx = kmalloc(sizeof(*pctx), GFP_ATOMIC);
990 if (pctx == NULL)
991 return -ENOMEM;
993 sock_hold(sk);
994 pctx->sk = sk;
995 pctx->dev = gtp->dev;
996 ipv4_pdp_fill(pctx, info);
997 atomic_set(&pctx->tx_seq, 0);
999 switch (pctx->gtp_version) {
1000 case GTP_V0:
1001 /* TS 09.60: "The flow label identifies unambiguously a GTP
1002 * flow.". We use the tid for this instead, I cannot find a
1003 * situation in which this doesn't unambiguosly identify the
1004 * PDP context.
1006 hash_tid = gtp0_hashfn(pctx->u.v0.tid) % gtp->hash_size;
1007 break;
1008 case GTP_V1:
1009 hash_tid = gtp1u_hashfn(pctx->u.v1.i_tei) % gtp->hash_size;
1010 break;
1013 hlist_add_head_rcu(&pctx->hlist_addr, &gtp->addr_hash[hash_ms]);
1014 hlist_add_head_rcu(&pctx->hlist_tid, &gtp->tid_hash[hash_tid]);
1016 switch (pctx->gtp_version) {
1017 case GTP_V0:
1018 netdev_dbg(dev, "GTPv0-U: new PDP ctx id=%llx ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
1019 pctx->u.v0.tid, &pctx->peer_addr_ip4,
1020 &pctx->ms_addr_ip4, pctx);
1021 break;
1022 case GTP_V1:
1023 netdev_dbg(dev, "GTPv1-U: new PDP ctx id=%x/%x ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
1024 pctx->u.v1.i_tei, pctx->u.v1.o_tei,
1025 &pctx->peer_addr_ip4, &pctx->ms_addr_ip4, pctx);
1026 break;
1029 return 0;
1032 static void pdp_context_free(struct rcu_head *head)
1034 struct pdp_ctx *pctx = container_of(head, struct pdp_ctx, rcu_head);
1036 sock_put(pctx->sk);
1037 kfree(pctx);
1040 static void pdp_context_delete(struct pdp_ctx *pctx)
1042 hlist_del_rcu(&pctx->hlist_tid);
1043 hlist_del_rcu(&pctx->hlist_addr);
1044 call_rcu(&pctx->rcu_head, pdp_context_free);
1047 static int gtp_genl_new_pdp(struct sk_buff *skb, struct genl_info *info)
1049 unsigned int version;
1050 struct gtp_dev *gtp;
1051 struct sock *sk;
1052 int err;
1054 if (!info->attrs[GTPA_VERSION] ||
1055 !info->attrs[GTPA_LINK] ||
1056 !info->attrs[GTPA_PEER_ADDRESS] ||
1057 !info->attrs[GTPA_MS_ADDRESS])
1058 return -EINVAL;
1060 version = nla_get_u32(info->attrs[GTPA_VERSION]);
1062 switch (version) {
1063 case GTP_V0:
1064 if (!info->attrs[GTPA_TID] ||
1065 !info->attrs[GTPA_FLOW])
1066 return -EINVAL;
1067 break;
1068 case GTP_V1:
1069 if (!info->attrs[GTPA_I_TEI] ||
1070 !info->attrs[GTPA_O_TEI])
1071 return -EINVAL;
1072 break;
1074 default:
1075 return -EINVAL;
1078 rtnl_lock();
1079 rcu_read_lock();
1081 gtp = gtp_find_dev(sock_net(skb->sk), info->attrs);
1082 if (!gtp) {
1083 err = -ENODEV;
1084 goto out_unlock;
1087 if (version == GTP_V0)
1088 sk = gtp->sk0;
1089 else if (version == GTP_V1)
1090 sk = gtp->sk1u;
1091 else
1092 sk = NULL;
1094 if (!sk) {
1095 err = -ENODEV;
1096 goto out_unlock;
1099 err = gtp_pdp_add(gtp, sk, info);
1101 out_unlock:
1102 rcu_read_unlock();
1103 rtnl_unlock();
1104 return err;
1107 static struct pdp_ctx *gtp_find_pdp_by_link(struct net *net,
1108 struct nlattr *nla[])
1110 struct gtp_dev *gtp;
1112 gtp = gtp_find_dev(net, nla);
1113 if (!gtp)
1114 return ERR_PTR(-ENODEV);
1116 if (nla[GTPA_MS_ADDRESS]) {
1117 __be32 ip = nla_get_be32(nla[GTPA_MS_ADDRESS]);
1119 return ipv4_pdp_find(gtp, ip);
1120 } else if (nla[GTPA_VERSION]) {
1121 u32 gtp_version = nla_get_u32(nla[GTPA_VERSION]);
1123 if (gtp_version == GTP_V0 && nla[GTPA_TID])
1124 return gtp0_pdp_find(gtp, nla_get_u64(nla[GTPA_TID]));
1125 else if (gtp_version == GTP_V1 && nla[GTPA_I_TEI])
1126 return gtp1_pdp_find(gtp, nla_get_u32(nla[GTPA_I_TEI]));
1129 return ERR_PTR(-EINVAL);
1132 static struct pdp_ctx *gtp_find_pdp(struct net *net, struct nlattr *nla[])
1134 struct pdp_ctx *pctx;
1136 if (nla[GTPA_LINK])
1137 pctx = gtp_find_pdp_by_link(net, nla);
1138 else
1139 pctx = ERR_PTR(-EINVAL);
1141 if (!pctx)
1142 pctx = ERR_PTR(-ENOENT);
1144 return pctx;
1147 static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info)
1149 struct pdp_ctx *pctx;
1150 int err = 0;
1152 if (!info->attrs[GTPA_VERSION])
1153 return -EINVAL;
1155 rcu_read_lock();
1157 pctx = gtp_find_pdp(sock_net(skb->sk), info->attrs);
1158 if (IS_ERR(pctx)) {
1159 err = PTR_ERR(pctx);
1160 goto out_unlock;
1163 if (pctx->gtp_version == GTP_V0)
1164 netdev_dbg(pctx->dev, "GTPv0-U: deleting tunnel id = %llx (pdp %p)\n",
1165 pctx->u.v0.tid, pctx);
1166 else if (pctx->gtp_version == GTP_V1)
1167 netdev_dbg(pctx->dev, "GTPv1-U: deleting tunnel id = %x/%x (pdp %p)\n",
1168 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
1170 pdp_context_delete(pctx);
1172 out_unlock:
1173 rcu_read_unlock();
1174 return err;
1177 static struct genl_family gtp_genl_family;
1179 static int gtp_genl_fill_info(struct sk_buff *skb, u32 snd_portid, u32 snd_seq,
1180 int flags, u32 type, struct pdp_ctx *pctx)
1182 void *genlh;
1184 genlh = genlmsg_put(skb, snd_portid, snd_seq, &gtp_genl_family, flags,
1185 type);
1186 if (genlh == NULL)
1187 goto nlmsg_failure;
1189 if (nla_put_u32(skb, GTPA_VERSION, pctx->gtp_version) ||
1190 nla_put_be32(skb, GTPA_PEER_ADDRESS, pctx->peer_addr_ip4.s_addr) ||
1191 nla_put_be32(skb, GTPA_MS_ADDRESS, pctx->ms_addr_ip4.s_addr))
1192 goto nla_put_failure;
1194 switch (pctx->gtp_version) {
1195 case GTP_V0:
1196 if (nla_put_u64_64bit(skb, GTPA_TID, pctx->u.v0.tid, GTPA_PAD) ||
1197 nla_put_u16(skb, GTPA_FLOW, pctx->u.v0.flow))
1198 goto nla_put_failure;
1199 break;
1200 case GTP_V1:
1201 if (nla_put_u32(skb, GTPA_I_TEI, pctx->u.v1.i_tei) ||
1202 nla_put_u32(skb, GTPA_O_TEI, pctx->u.v1.o_tei))
1203 goto nla_put_failure;
1204 break;
1206 genlmsg_end(skb, genlh);
1207 return 0;
1209 nlmsg_failure:
1210 nla_put_failure:
1211 genlmsg_cancel(skb, genlh);
1212 return -EMSGSIZE;
1215 static int gtp_genl_get_pdp(struct sk_buff *skb, struct genl_info *info)
1217 struct pdp_ctx *pctx = NULL;
1218 struct sk_buff *skb2;
1219 int err;
1221 if (!info->attrs[GTPA_VERSION])
1222 return -EINVAL;
1224 rcu_read_lock();
1226 pctx = gtp_find_pdp(sock_net(skb->sk), info->attrs);
1227 if (IS_ERR(pctx)) {
1228 err = PTR_ERR(pctx);
1229 goto err_unlock;
1232 skb2 = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
1233 if (skb2 == NULL) {
1234 err = -ENOMEM;
1235 goto err_unlock;
1238 err = gtp_genl_fill_info(skb2, NETLINK_CB(skb).portid, info->snd_seq,
1239 0, info->nlhdr->nlmsg_type, pctx);
1240 if (err < 0)
1241 goto err_unlock_free;
1243 rcu_read_unlock();
1244 return genlmsg_unicast(genl_info_net(info), skb2, info->snd_portid);
1246 err_unlock_free:
1247 kfree_skb(skb2);
1248 err_unlock:
1249 rcu_read_unlock();
1250 return err;
1253 static int gtp_genl_dump_pdp(struct sk_buff *skb,
1254 struct netlink_callback *cb)
1256 struct gtp_dev *last_gtp = (struct gtp_dev *)cb->args[2], *gtp;
1257 int i, j, bucket = cb->args[0], skip = cb->args[1];
1258 struct net *net = sock_net(skb->sk);
1259 struct pdp_ctx *pctx;
1260 struct gtp_net *gn;
1262 gn = net_generic(net, gtp_net_id);
1264 if (cb->args[4])
1265 return 0;
1267 rcu_read_lock();
1268 list_for_each_entry_rcu(gtp, &gn->gtp_dev_list, list) {
1269 if (last_gtp && last_gtp != gtp)
1270 continue;
1271 else
1272 last_gtp = NULL;
1274 for (i = bucket; i < gtp->hash_size; i++) {
1275 j = 0;
1276 hlist_for_each_entry_rcu(pctx, &gtp->tid_hash[i],
1277 hlist_tid) {
1278 if (j >= skip &&
1279 gtp_genl_fill_info(skb,
1280 NETLINK_CB(cb->skb).portid,
1281 cb->nlh->nlmsg_seq,
1282 NLM_F_MULTI,
1283 cb->nlh->nlmsg_type, pctx)) {
1284 cb->args[0] = i;
1285 cb->args[1] = j;
1286 cb->args[2] = (unsigned long)gtp;
1287 goto out;
1289 j++;
1291 skip = 0;
1293 bucket = 0;
1295 cb->args[4] = 1;
1296 out:
1297 rcu_read_unlock();
1298 return skb->len;
1301 static const struct nla_policy gtp_genl_policy[GTPA_MAX + 1] = {
1302 [GTPA_LINK] = { .type = NLA_U32, },
1303 [GTPA_VERSION] = { .type = NLA_U32, },
1304 [GTPA_TID] = { .type = NLA_U64, },
1305 [GTPA_PEER_ADDRESS] = { .type = NLA_U32, },
1306 [GTPA_MS_ADDRESS] = { .type = NLA_U32, },
1307 [GTPA_FLOW] = { .type = NLA_U16, },
1308 [GTPA_NET_NS_FD] = { .type = NLA_U32, },
1309 [GTPA_I_TEI] = { .type = NLA_U32, },
1310 [GTPA_O_TEI] = { .type = NLA_U32, },
1313 static const struct genl_ops gtp_genl_ops[] = {
1315 .cmd = GTP_CMD_NEWPDP,
1316 .doit = gtp_genl_new_pdp,
1317 .policy = gtp_genl_policy,
1318 .flags = GENL_ADMIN_PERM,
1321 .cmd = GTP_CMD_DELPDP,
1322 .doit = gtp_genl_del_pdp,
1323 .policy = gtp_genl_policy,
1324 .flags = GENL_ADMIN_PERM,
1327 .cmd = GTP_CMD_GETPDP,
1328 .doit = gtp_genl_get_pdp,
1329 .dumpit = gtp_genl_dump_pdp,
1330 .policy = gtp_genl_policy,
1331 .flags = GENL_ADMIN_PERM,
1335 static struct genl_family gtp_genl_family __ro_after_init = {
1336 .name = "gtp",
1337 .version = 0,
1338 .hdrsize = 0,
1339 .maxattr = GTPA_MAX,
1340 .netnsok = true,
1341 .module = THIS_MODULE,
1342 .ops = gtp_genl_ops,
1343 .n_ops = ARRAY_SIZE(gtp_genl_ops),
1346 static int __net_init gtp_net_init(struct net *net)
1348 struct gtp_net *gn = net_generic(net, gtp_net_id);
1350 INIT_LIST_HEAD(&gn->gtp_dev_list);
1351 return 0;
1354 static void __net_exit gtp_net_exit(struct net *net)
1356 struct gtp_net *gn = net_generic(net, gtp_net_id);
1357 struct gtp_dev *gtp;
1358 LIST_HEAD(list);
1360 rtnl_lock();
1361 list_for_each_entry(gtp, &gn->gtp_dev_list, list)
1362 gtp_dellink(gtp->dev, &list);
1364 unregister_netdevice_many(&list);
1365 rtnl_unlock();
1368 static struct pernet_operations gtp_net_ops = {
1369 .init = gtp_net_init,
1370 .exit = gtp_net_exit,
1371 .id = &gtp_net_id,
1372 .size = sizeof(struct gtp_net),
1375 static int __init gtp_init(void)
1377 int err;
1379 get_random_bytes(&gtp_h_initval, sizeof(gtp_h_initval));
1381 err = rtnl_link_register(&gtp_link_ops);
1382 if (err < 0)
1383 goto error_out;
1385 err = genl_register_family(&gtp_genl_family);
1386 if (err < 0)
1387 goto unreg_rtnl_link;
1389 err = register_pernet_subsys(&gtp_net_ops);
1390 if (err < 0)
1391 goto unreg_genl_family;
1393 pr_info("GTP module loaded (pdp ctx size %zd bytes)\n",
1394 sizeof(struct pdp_ctx));
1395 return 0;
1397 unreg_genl_family:
1398 genl_unregister_family(&gtp_genl_family);
1399 unreg_rtnl_link:
1400 rtnl_link_unregister(&gtp_link_ops);
1401 error_out:
1402 pr_err("error loading GTP module loaded\n");
1403 return err;
1405 late_initcall(gtp_init);
1407 static void __exit gtp_fini(void)
1409 genl_unregister_family(&gtp_genl_family);
1410 rtnl_link_unregister(&gtp_link_ops);
1411 unregister_pernet_subsys(&gtp_net_ops);
1413 pr_info("GTP module unloaded\n");
1415 module_exit(gtp_fini);
1417 MODULE_LICENSE("GPL");
1418 MODULE_AUTHOR("Harald Welte <hwelte@sysmocom.de>");
1419 MODULE_DESCRIPTION("Interface driver for GTP encapsulated traffic");
1420 MODULE_ALIAS_RTNL_LINK("gtp");
1421 MODULE_ALIAS_GENL_FAMILY("gtp");