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>
32 #include <net/udp_tunnel.h>
35 #include <net/genetlink.h>
36 #include <net/netns/generic.h>
39 /* An active session for the subscriber. */
41 struct hlist_node hlist_tid;
42 struct hlist_node hlist_addr;
57 struct in_addr ms_addr_ip4;
58 struct in_addr sgsn_addr_ip4;
61 struct rcu_head rcu_head;
64 /* One instance of the GTP device. */
66 struct list_head list;
69 struct socket *sock1u;
71 struct net_device *dev;
73 unsigned int hash_size;
74 struct hlist_head *tid_hash;
75 struct hlist_head *addr_hash;
78 static int gtp_net_id __read_mostly;
81 struct list_head gtp_dev_list;
84 static u32 gtp_h_initval;
86 static inline u32 gtp0_hashfn(u64 tid)
88 u32 *tid32 = (u32 *) &tid;
89 return jhash_2words(tid32[0], tid32[1], gtp_h_initval);
92 static inline u32 gtp1u_hashfn(u32 tid)
94 return jhash_1word(tid, gtp_h_initval);
97 static inline u32 ipv4_hashfn(__be32 ip)
99 return jhash_1word((__force u32)ip, gtp_h_initval);
102 /* Resolve a PDP context structure based on the 64bit TID. */
103 static struct pdp_ctx *gtp0_pdp_find(struct gtp_dev *gtp, u64 tid)
105 struct hlist_head *head;
108 head = >p->tid_hash[gtp0_hashfn(tid) % gtp->hash_size];
110 hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
111 if (pdp->gtp_version == GTP_V0 &&
112 pdp->u.v0.tid == tid)
118 /* Resolve a PDP context structure based on the 32bit TEI. */
119 static struct pdp_ctx *gtp1_pdp_find(struct gtp_dev *gtp, u32 tid)
121 struct hlist_head *head;
124 head = >p->tid_hash[gtp1u_hashfn(tid) % gtp->hash_size];
126 hlist_for_each_entry_rcu(pdp, head, hlist_tid) {
127 if (pdp->gtp_version == GTP_V1 &&
128 pdp->u.v1.i_tei == tid)
134 /* Resolve a PDP context based on IPv4 address of MS. */
135 static struct pdp_ctx *ipv4_pdp_find(struct gtp_dev *gtp, __be32 ms_addr)
137 struct hlist_head *head;
140 head = >p->addr_hash[ipv4_hashfn(ms_addr) % gtp->hash_size];
142 hlist_for_each_entry_rcu(pdp, head, hlist_addr) {
143 if (pdp->af == AF_INET &&
144 pdp->ms_addr_ip4.s_addr == ms_addr)
151 static bool gtp_check_src_ms_ipv4(struct sk_buff *skb, struct pdp_ctx *pctx,
156 if (!pskb_may_pull(skb, hdrlen + sizeof(struct iphdr)))
159 iph = (struct iphdr *)(skb->data + hdrlen + sizeof(struct iphdr));
161 return iph->saddr != pctx->ms_addr_ip4.s_addr;
164 /* Check if the inner IP source address in this packet is assigned to any
165 * existing mobile subscriber.
167 static bool gtp_check_src_ms(struct sk_buff *skb, struct pdp_ctx *pctx,
170 switch (ntohs(skb->protocol)) {
172 return gtp_check_src_ms_ipv4(skb, pctx, hdrlen);
177 /* 1 means pass up to the stack, -1 means drop and 0 means decapsulated. */
178 static int gtp0_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb,
181 unsigned int hdrlen = sizeof(struct udphdr) +
182 sizeof(struct gtp0_header);
183 struct gtp0_header *gtp0;
184 struct pdp_ctx *pctx;
187 if (!pskb_may_pull(skb, hdrlen))
190 gtp0 = (struct gtp0_header *)(skb->data + sizeof(struct udphdr));
192 if ((gtp0->flags >> 5) != GTP_V0)
195 if (gtp0->type != GTP_TPDU)
199 pctx = gtp0_pdp_find(gtp, be64_to_cpu(gtp0->tid));
201 netdev_dbg(gtp->dev, "No PDP ctx to decap skb=%p\n", skb);
206 if (!gtp_check_src_ms(skb, pctx, hdrlen)) {
207 netdev_dbg(gtp->dev, "No PDP ctx for this MS\n");
213 /* Get rid of the GTP + UDP headers. */
214 return iptunnel_pull_header(skb, hdrlen, skb->protocol, xnet);
220 static int gtp1u_udp_encap_recv(struct gtp_dev *gtp, struct sk_buff *skb,
223 unsigned int hdrlen = sizeof(struct udphdr) +
224 sizeof(struct gtp1_header);
225 struct gtp1_header *gtp1;
226 struct pdp_ctx *pctx;
229 if (!pskb_may_pull(skb, hdrlen))
232 gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
234 if ((gtp1->flags >> 5) != GTP_V1)
237 if (gtp1->type != GTP_TPDU)
240 /* From 29.060: "This field shall be present if and only if any one or
241 * more of the S, PN and E flags are set.".
243 * If any of the bit is set, then the remaining ones also have to be
246 if (gtp1->flags & GTP1_F_MASK)
249 /* Make sure the header is larger enough, including extensions. */
250 if (!pskb_may_pull(skb, hdrlen))
253 gtp1 = (struct gtp1_header *)(skb->data + sizeof(struct udphdr));
256 pctx = gtp1_pdp_find(gtp, ntohl(gtp1->tid));
258 netdev_dbg(gtp->dev, "No PDP ctx to decap skb=%p\n", skb);
263 if (!gtp_check_src_ms(skb, pctx, hdrlen)) {
264 netdev_dbg(gtp->dev, "No PDP ctx for this MS\n");
270 /* Get rid of the GTP + UDP headers. */
271 return iptunnel_pull_header(skb, hdrlen, skb->protocol, xnet);
277 static void gtp_encap_disable(struct gtp_dev *gtp)
279 if (gtp->sock0 && gtp->sock0->sk) {
280 udp_sk(gtp->sock0->sk)->encap_type = 0;
281 rcu_assign_sk_user_data(gtp->sock0->sk, NULL);
283 if (gtp->sock1u && gtp->sock1u->sk) {
284 udp_sk(gtp->sock1u->sk)->encap_type = 0;
285 rcu_assign_sk_user_data(gtp->sock1u->sk, NULL);
292 static void gtp_encap_destroy(struct sock *sk)
296 gtp = rcu_dereference_sk_user_data(sk);
298 gtp_encap_disable(gtp);
301 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
302 * Return codes: 0: success, <0: error, >0: pass up to userspace UDP socket.
304 static int gtp_encap_recv(struct sock *sk, struct sk_buff *skb)
306 struct pcpu_sw_netstats *stats;
311 gtp = rcu_dereference_sk_user_data(sk);
315 netdev_dbg(gtp->dev, "encap_recv sk=%p\n", sk);
317 xnet = !net_eq(sock_net(sk), dev_net(gtp->dev));
319 switch (udp_sk(sk)->encap_type) {
321 netdev_dbg(gtp->dev, "received GTP0 packet\n");
322 ret = gtp0_udp_encap_recv(gtp, skb, xnet);
324 case UDP_ENCAP_GTP1U:
325 netdev_dbg(gtp->dev, "received GTP1U packet\n");
326 ret = gtp1u_udp_encap_recv(gtp, skb, xnet);
329 ret = -1; /* Shouldn't happen. */
334 netdev_dbg(gtp->dev, "pass up to the process\n");
337 netdev_dbg(gtp->dev, "forwarding packet from GGSN to uplink\n");
340 netdev_dbg(gtp->dev, "GTP packet has been dropped\n");
345 /* Now that the UDP and the GTP header have been removed, set up the
346 * new network header. This is required by the upper layer to
347 * calculate the transport header.
349 skb_reset_network_header(skb);
353 stats = this_cpu_ptr(gtp->dev->tstats);
354 u64_stats_update_begin(&stats->syncp);
356 stats->rx_bytes += skb->len;
357 u64_stats_update_end(&stats->syncp);
364 static int gtp_dev_init(struct net_device *dev)
366 struct gtp_dev *gtp = netdev_priv(dev);
370 dev->tstats = alloc_percpu(struct pcpu_sw_netstats);
377 static void gtp_dev_uninit(struct net_device *dev)
379 struct gtp_dev *gtp = netdev_priv(dev);
381 gtp_encap_disable(gtp);
382 free_percpu(dev->tstats);
385 static struct rtable *ip4_route_output_gtp(struct net *net, struct flowi4 *fl4,
386 const struct sock *sk, __be32 daddr)
388 memset(fl4, 0, sizeof(*fl4));
389 fl4->flowi4_oif = sk->sk_bound_dev_if;
391 fl4->saddr = inet_sk(sk)->inet_saddr;
392 fl4->flowi4_tos = RT_CONN_FLAGS(sk);
393 fl4->flowi4_proto = sk->sk_protocol;
395 return ip_route_output_key(net, fl4);
398 static inline void gtp0_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
400 int payload_len = skb->len;
401 struct gtp0_header *gtp0;
403 gtp0 = (struct gtp0_header *) skb_push(skb, sizeof(*gtp0));
405 gtp0->flags = 0x1e; /* v0, GTP-non-prime. */
406 gtp0->type = GTP_TPDU;
407 gtp0->length = htons(payload_len);
408 gtp0->seq = htons((atomic_inc_return(&pctx->tx_seq) - 1) % 0xffff);
409 gtp0->flow = htons(pctx->u.v0.flow);
411 gtp0->spare[0] = gtp0->spare[1] = gtp0->spare[2] = 0xff;
412 gtp0->tid = cpu_to_be64(pctx->u.v0.tid);
415 static inline void gtp1_push_header(struct sk_buff *skb, struct pdp_ctx *pctx)
417 int payload_len = skb->len;
418 struct gtp1_header *gtp1;
420 gtp1 = (struct gtp1_header *) skb_push(skb, sizeof(*gtp1));
422 /* Bits 8 7 6 5 4 3 2 1
423 * +--+--+--+--+--+--+--+--+
424 * |version |PT| 1| E| S|PN|
425 * +--+--+--+--+--+--+--+--+
428 gtp1->flags = 0x38; /* v1, GTP-non-prime. */
429 gtp1->type = GTP_TPDU;
430 gtp1->length = htons(payload_len);
431 gtp1->tid = htonl(pctx->u.v1.o_tei);
433 /* TODO: Suppport for extension header, sequence number and N-PDU.
434 * Update the length field if any of them is available.
443 struct pdp_ctx *pctx;
444 struct net_device *dev;
448 static void gtp_push_header(struct sk_buff *skb, struct gtp_pktinfo *pktinfo)
450 switch (pktinfo->pctx->gtp_version) {
452 pktinfo->gtph_port = htons(GTP0_PORT);
453 gtp0_push_header(skb, pktinfo->pctx);
456 pktinfo->gtph_port = htons(GTP1U_PORT);
457 gtp1_push_header(skb, pktinfo->pctx);
462 static inline void gtp_set_pktinfo_ipv4(struct gtp_pktinfo *pktinfo,
463 struct sock *sk, struct iphdr *iph,
464 struct pdp_ctx *pctx, struct rtable *rt,
466 struct net_device *dev)
470 pktinfo->pctx = pctx;
476 static int gtp_build_skb_ip4(struct sk_buff *skb, struct net_device *dev,
477 struct gtp_pktinfo *pktinfo)
479 struct gtp_dev *gtp = netdev_priv(dev);
480 struct pdp_ctx *pctx;
488 /* Read the IP destination address and resolve the PDP context.
489 * Prepend PDP header with TEI/TID from PDP ctx.
492 pctx = ipv4_pdp_find(gtp, iph->daddr);
494 netdev_dbg(dev, "no PDP ctx found for %pI4, skip\n",
498 netdev_dbg(dev, "found PDP context %p\n", pctx);
500 switch (pctx->gtp_version) {
509 sk = gtp->sock1u->sk;
518 netdev_dbg(dev, "no userspace socket is available, skip\n");
522 rt = ip4_route_output_gtp(sock_net(sk), &fl4, gtp->sock0->sk,
523 pctx->sgsn_addr_ip4.s_addr);
525 netdev_dbg(dev, "no route to SSGN %pI4\n",
526 &pctx->sgsn_addr_ip4.s_addr);
527 dev->stats.tx_carrier_errors++;
531 if (rt->dst.dev == dev) {
532 netdev_dbg(dev, "circular route to SSGN %pI4\n",
533 &pctx->sgsn_addr_ip4.s_addr);
534 dev->stats.collisions++;
540 /* This is similar to tnl_update_pmtu(). */
543 mtu = dst_mtu(&rt->dst) - dev->hard_header_len -
544 sizeof(struct iphdr) - sizeof(struct udphdr);
545 switch (pctx->gtp_version) {
547 mtu -= sizeof(struct gtp0_header);
550 mtu -= sizeof(struct gtp1_header);
554 mtu = dst_mtu(&rt->dst);
557 rt->dst.ops->update_pmtu(&rt->dst, NULL, skb, mtu);
559 if (!skb_is_gso(skb) && (iph->frag_off & htons(IP_DF)) &&
560 mtu < ntohs(iph->tot_len)) {
561 netdev_dbg(dev, "packet too big, fragmentation needed\n");
562 icmp_ndo_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED,
567 gtp_set_pktinfo_ipv4(pktinfo, sk, iph, pctx, rt, &fl4, dev);
568 gtp_push_header(skb, pktinfo);
577 static netdev_tx_t gtp_dev_xmit(struct sk_buff *skb, struct net_device *dev)
579 unsigned int proto = ntohs(skb->protocol);
580 struct gtp_pktinfo pktinfo;
583 /* Ensure there is sufficient headroom. */
584 if (skb_cow_head(skb, dev->needed_headroom))
587 skb_reset_inner_headers(skb);
589 /* PDP context lookups in gtp_build_skb_*() need rcu read-side lock. */
593 err = gtp_build_skb_ip4(skb, dev, &pktinfo);
606 netdev_dbg(pktinfo.dev, "gtp -> IP src: %pI4 dst: %pI4\n",
607 &pktinfo.iph->saddr, &pktinfo.iph->daddr);
608 udp_tunnel_xmit_skb(pktinfo.rt, pktinfo.sk, skb,
609 pktinfo.fl4.saddr, pktinfo.fl4.daddr,
611 ip4_dst_hoplimit(&pktinfo.rt->dst),
613 pktinfo.gtph_port, pktinfo.gtph_port,
620 dev->stats.tx_errors++;
625 static const struct net_device_ops gtp_netdev_ops = {
626 .ndo_init = gtp_dev_init,
627 .ndo_uninit = gtp_dev_uninit,
628 .ndo_start_xmit = gtp_dev_xmit,
629 .ndo_get_stats64 = ip_tunnel_get_stats64,
632 static void gtp_link_setup(struct net_device *dev)
634 dev->netdev_ops = >p_netdev_ops;
635 dev->destructor = free_netdev;
637 dev->hard_header_len = 0;
640 /* Zero header length. */
641 dev->type = ARPHRD_NONE;
642 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
644 dev->priv_flags |= IFF_NO_QUEUE;
645 dev->features |= NETIF_F_LLTX;
648 /* Assume largest header, ie. GTPv0. */
649 dev->needed_headroom = LL_MAX_HEADER +
650 sizeof(struct iphdr) +
651 sizeof(struct udphdr) +
652 sizeof(struct gtp0_header);
655 static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize);
656 static void gtp_hashtable_free(struct gtp_dev *gtp);
657 static int gtp_encap_enable(struct net_device *dev, struct gtp_dev *gtp,
658 int fd_gtp0, int fd_gtp1);
660 static int gtp_newlink(struct net *src_net, struct net_device *dev,
661 struct nlattr *tb[], struct nlattr *data[])
663 int hashsize, err, fd0, fd1;
667 if (!data[IFLA_GTP_FD0] || !data[IFLA_GTP_FD1])
670 gtp = netdev_priv(dev);
672 fd0 = nla_get_u32(data[IFLA_GTP_FD0]);
673 fd1 = nla_get_u32(data[IFLA_GTP_FD1]);
675 err = gtp_encap_enable(dev, gtp, fd0, fd1);
679 if (!data[IFLA_GTP_PDP_HASHSIZE]) {
682 hashsize = nla_get_u32(data[IFLA_GTP_PDP_HASHSIZE]);
687 err = gtp_hashtable_new(gtp, hashsize);
691 err = register_netdevice(dev);
693 netdev_dbg(dev, "failed to register new netdev %d\n", err);
697 gn = net_generic(dev_net(dev), gtp_net_id);
698 list_add_rcu(>p->list, &gn->gtp_dev_list);
700 netdev_dbg(dev, "registered new GTP interface\n");
705 gtp_hashtable_free(gtp);
707 gtp_encap_disable(gtp);
712 static void gtp_dellink(struct net_device *dev, struct list_head *head)
714 struct gtp_dev *gtp = netdev_priv(dev);
716 gtp_encap_disable(gtp);
717 gtp_hashtable_free(gtp);
718 list_del_rcu(>p->list);
719 unregister_netdevice_queue(dev, head);
722 static const struct nla_policy gtp_policy[IFLA_GTP_MAX + 1] = {
723 [IFLA_GTP_FD0] = { .type = NLA_U32 },
724 [IFLA_GTP_FD1] = { .type = NLA_U32 },
725 [IFLA_GTP_PDP_HASHSIZE] = { .type = NLA_U32 },
728 static int gtp_validate(struct nlattr *tb[], struct nlattr *data[])
736 static size_t gtp_get_size(const struct net_device *dev)
738 return nla_total_size(sizeof(__u32)); /* IFLA_GTP_PDP_HASHSIZE */
741 static int gtp_fill_info(struct sk_buff *skb, const struct net_device *dev)
743 struct gtp_dev *gtp = netdev_priv(dev);
745 if (nla_put_u32(skb, IFLA_GTP_PDP_HASHSIZE, gtp->hash_size))
746 goto nla_put_failure;
754 static struct rtnl_link_ops gtp_link_ops __read_mostly = {
756 .maxtype = IFLA_GTP_MAX,
757 .policy = gtp_policy,
758 .priv_size = sizeof(struct gtp_dev),
759 .setup = gtp_link_setup,
760 .validate = gtp_validate,
761 .newlink = gtp_newlink,
762 .dellink = gtp_dellink,
763 .get_size = gtp_get_size,
764 .fill_info = gtp_fill_info,
767 static struct net *gtp_genl_get_net(struct net *src_net, struct nlattr *tb[])
771 /* Examine the link attributes and figure out which network namespace
772 * we are talking about.
774 if (tb[GTPA_NET_NS_FD])
775 net = get_net_ns_by_fd(nla_get_u32(tb[GTPA_NET_NS_FD]));
777 net = get_net(src_net);
782 static int gtp_hashtable_new(struct gtp_dev *gtp, int hsize)
786 gtp->addr_hash = kmalloc(sizeof(struct hlist_head) * hsize,
787 GFP_KERNEL | __GFP_NOWARN);
788 if (gtp->addr_hash == NULL)
791 gtp->tid_hash = kmalloc(sizeof(struct hlist_head) * hsize,
792 GFP_KERNEL | __GFP_NOWARN);
793 if (gtp->tid_hash == NULL)
796 gtp->hash_size = hsize;
798 for (i = 0; i < hsize; i++) {
799 INIT_HLIST_HEAD(>p->addr_hash[i]);
800 INIT_HLIST_HEAD(>p->tid_hash[i]);
804 kfree(gtp->addr_hash);
808 static void gtp_hashtable_free(struct gtp_dev *gtp)
810 struct pdp_ctx *pctx;
813 for (i = 0; i < gtp->hash_size; i++) {
814 hlist_for_each_entry_rcu(pctx, >p->tid_hash[i], hlist_tid) {
815 hlist_del_rcu(&pctx->hlist_tid);
816 hlist_del_rcu(&pctx->hlist_addr);
817 kfree_rcu(pctx, rcu_head);
821 kfree(gtp->addr_hash);
822 kfree(gtp->tid_hash);
825 static int gtp_encap_enable(struct net_device *dev, struct gtp_dev *gtp,
826 int fd_gtp0, int fd_gtp1)
828 struct udp_tunnel_sock_cfg tuncfg = {NULL};
829 struct socket *sock0, *sock1u;
832 netdev_dbg(dev, "enable gtp on %d, %d\n", fd_gtp0, fd_gtp1);
834 sock0 = sockfd_lookup(fd_gtp0, &err);
836 netdev_dbg(dev, "socket fd=%d not found (gtp0)\n", fd_gtp0);
840 if (sock0->sk->sk_protocol != IPPROTO_UDP ||
841 sock0->sk->sk_type != SOCK_DGRAM ||
842 (sock0->sk->sk_family != AF_INET && sock0->sk->sk_family != AF_INET6)) {
843 netdev_dbg(dev, "socket fd=%d not UDP\n", fd_gtp0);
848 sock1u = sockfd_lookup(fd_gtp1, &err);
849 if (sock1u == NULL) {
850 netdev_dbg(dev, "socket fd=%d not found (gtp1u)\n", fd_gtp1);
855 if (sock1u->sk->sk_protocol != IPPROTO_UDP) {
856 netdev_dbg(dev, "socket fd=%d not UDP\n", fd_gtp1);
861 netdev_dbg(dev, "enable gtp on %p, %p\n", sock0, sock1u);
864 gtp->sock1u = sock1u;
866 tuncfg.sk_user_data = gtp;
867 tuncfg.encap_rcv = gtp_encap_recv;
868 tuncfg.encap_destroy = gtp_encap_destroy;
870 tuncfg.encap_type = UDP_ENCAP_GTP0;
871 setup_udp_tunnel_sock(sock_net(gtp->sock0->sk), gtp->sock0, &tuncfg);
873 tuncfg.encap_type = UDP_ENCAP_GTP1U;
874 setup_udp_tunnel_sock(sock_net(gtp->sock1u->sk), gtp->sock1u, &tuncfg);
884 static struct net_device *gtp_find_dev(struct net *net, int ifindex)
886 struct gtp_net *gn = net_generic(net, gtp_net_id);
889 list_for_each_entry_rcu(gtp, &gn->gtp_dev_list, list) {
890 if (ifindex == gtp->dev->ifindex)
896 static void ipv4_pdp_fill(struct pdp_ctx *pctx, struct genl_info *info)
898 pctx->gtp_version = nla_get_u32(info->attrs[GTPA_VERSION]);
900 pctx->sgsn_addr_ip4.s_addr =
901 nla_get_be32(info->attrs[GTPA_SGSN_ADDRESS]);
902 pctx->ms_addr_ip4.s_addr =
903 nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
905 switch (pctx->gtp_version) {
907 /* According to TS 09.60, sections 7.5.1 and 7.5.2, the flow
908 * label needs to be the same for uplink and downlink packets,
909 * so let's annotate this.
911 pctx->u.v0.tid = nla_get_u64(info->attrs[GTPA_TID]);
912 pctx->u.v0.flow = nla_get_u16(info->attrs[GTPA_FLOW]);
915 pctx->u.v1.i_tei = nla_get_u32(info->attrs[GTPA_I_TEI]);
916 pctx->u.v1.o_tei = nla_get_u32(info->attrs[GTPA_O_TEI]);
923 static int ipv4_pdp_add(struct net_device *dev, struct genl_info *info)
925 struct gtp_dev *gtp = netdev_priv(dev);
926 u32 hash_ms, hash_tid = 0;
927 struct pdp_ctx *pctx;
931 ms_addr = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
932 hash_ms = ipv4_hashfn(ms_addr) % gtp->hash_size;
934 hlist_for_each_entry_rcu(pctx, >p->addr_hash[hash_ms], hlist_addr) {
935 if (pctx->ms_addr_ip4.s_addr == ms_addr) {
942 if (info->nlhdr->nlmsg_flags & NLM_F_EXCL)
944 if (info->nlhdr->nlmsg_flags & NLM_F_REPLACE)
947 ipv4_pdp_fill(pctx, info);
949 if (pctx->gtp_version == GTP_V0)
950 netdev_dbg(dev, "GTPv0-U: update tunnel id = %llx (pdp %p)\n",
951 pctx->u.v0.tid, pctx);
952 else if (pctx->gtp_version == GTP_V1)
953 netdev_dbg(dev, "GTPv1-U: update tunnel id = %x/%x (pdp %p)\n",
954 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
960 pctx = kmalloc(sizeof(*pctx), GFP_ATOMIC);
964 ipv4_pdp_fill(pctx, info);
965 atomic_set(&pctx->tx_seq, 0);
967 switch (pctx->gtp_version) {
969 /* TS 09.60: "The flow label identifies unambiguously a GTP
970 * flow.". We use the tid for this instead, I cannot find a
971 * situation in which this doesn't unambiguosly identify the
974 hash_tid = gtp0_hashfn(pctx->u.v0.tid) % gtp->hash_size;
977 hash_tid = gtp1u_hashfn(pctx->u.v1.i_tei) % gtp->hash_size;
981 hlist_add_head_rcu(&pctx->hlist_addr, >p->addr_hash[hash_ms]);
982 hlist_add_head_rcu(&pctx->hlist_tid, >p->tid_hash[hash_tid]);
984 switch (pctx->gtp_version) {
986 netdev_dbg(dev, "GTPv0-U: new PDP ctx id=%llx ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
987 pctx->u.v0.tid, &pctx->sgsn_addr_ip4,
988 &pctx->ms_addr_ip4, pctx);
991 netdev_dbg(dev, "GTPv1-U: new PDP ctx id=%x/%x ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
992 pctx->u.v1.i_tei, pctx->u.v1.o_tei,
993 &pctx->sgsn_addr_ip4, &pctx->ms_addr_ip4, pctx);
1000 static int gtp_genl_new_pdp(struct sk_buff *skb, struct genl_info *info)
1002 struct net_device *dev;
1005 if (!info->attrs[GTPA_VERSION] ||
1006 !info->attrs[GTPA_LINK] ||
1007 !info->attrs[GTPA_SGSN_ADDRESS] ||
1008 !info->attrs[GTPA_MS_ADDRESS])
1011 switch (nla_get_u32(info->attrs[GTPA_VERSION])) {
1013 if (!info->attrs[GTPA_TID] ||
1014 !info->attrs[GTPA_FLOW])
1018 if (!info->attrs[GTPA_I_TEI] ||
1019 !info->attrs[GTPA_O_TEI])
1027 net = gtp_genl_get_net(sock_net(skb->sk), info->attrs);
1029 return PTR_ERR(net);
1031 /* Check if there's an existing gtpX device to configure */
1032 dev = gtp_find_dev(net, nla_get_u32(info->attrs[GTPA_LINK]));
1039 return ipv4_pdp_add(dev, info);
1042 static int gtp_genl_del_pdp(struct sk_buff *skb, struct genl_info *info)
1044 struct net_device *dev;
1045 struct pdp_ctx *pctx;
1046 struct gtp_dev *gtp;
1049 if (!info->attrs[GTPA_VERSION] ||
1050 !info->attrs[GTPA_LINK])
1053 net = gtp_genl_get_net(sock_net(skb->sk), info->attrs);
1055 return PTR_ERR(net);
1057 /* Check if there's an existing gtpX device to configure */
1058 dev = gtp_find_dev(net, nla_get_u32(info->attrs[GTPA_LINK]));
1065 gtp = netdev_priv(dev);
1067 switch (nla_get_u32(info->attrs[GTPA_VERSION])) {
1069 if (!info->attrs[GTPA_TID])
1071 pctx = gtp0_pdp_find(gtp, nla_get_u64(info->attrs[GTPA_TID]));
1074 if (!info->attrs[GTPA_I_TEI])
1076 pctx = gtp1_pdp_find(gtp, nla_get_u64(info->attrs[GTPA_I_TEI]));
1086 if (pctx->gtp_version == GTP_V0)
1087 netdev_dbg(dev, "GTPv0-U: deleting tunnel id = %llx (pdp %p)\n",
1088 pctx->u.v0.tid, pctx);
1089 else if (pctx->gtp_version == GTP_V1)
1090 netdev_dbg(dev, "GTPv1-U: deleting tunnel id = %x/%x (pdp %p)\n",
1091 pctx->u.v1.i_tei, pctx->u.v1.o_tei, pctx);
1093 hlist_del_rcu(&pctx->hlist_tid);
1094 hlist_del_rcu(&pctx->hlist_addr);
1095 kfree_rcu(pctx, rcu_head);
1100 static struct genl_family gtp_genl_family = {
1101 .id = GENL_ID_GENERATE,
1105 .maxattr = GTPA_MAX,
1109 static int gtp_genl_fill_info(struct sk_buff *skb, u32 snd_portid, u32 snd_seq,
1110 int flags, u32 type, struct pdp_ctx *pctx)
1114 genlh = genlmsg_put(skb, snd_portid, snd_seq, >p_genl_family, flags,
1119 if (nla_put_u32(skb, GTPA_VERSION, pctx->gtp_version) ||
1120 nla_put_be32(skb, GTPA_SGSN_ADDRESS, pctx->sgsn_addr_ip4.s_addr) ||
1121 nla_put_be32(skb, GTPA_MS_ADDRESS, pctx->ms_addr_ip4.s_addr))
1122 goto nla_put_failure;
1124 switch (pctx->gtp_version) {
1126 if (nla_put_u64_64bit(skb, GTPA_TID, pctx->u.v0.tid, GTPA_PAD) ||
1127 nla_put_u16(skb, GTPA_FLOW, pctx->u.v0.flow))
1128 goto nla_put_failure;
1131 if (nla_put_u32(skb, GTPA_I_TEI, pctx->u.v1.i_tei) ||
1132 nla_put_u32(skb, GTPA_O_TEI, pctx->u.v1.o_tei))
1133 goto nla_put_failure;
1136 genlmsg_end(skb, genlh);
1141 genlmsg_cancel(skb, genlh);
1145 static int gtp_genl_get_pdp(struct sk_buff *skb, struct genl_info *info)
1147 struct pdp_ctx *pctx = NULL;
1148 struct net_device *dev;
1149 struct sk_buff *skb2;
1150 struct gtp_dev *gtp;
1155 if (!info->attrs[GTPA_VERSION] ||
1156 !info->attrs[GTPA_LINK])
1159 gtp_version = nla_get_u32(info->attrs[GTPA_VERSION]);
1160 switch (gtp_version) {
1168 net = gtp_genl_get_net(sock_net(skb->sk), info->attrs);
1170 return PTR_ERR(net);
1172 /* Check if there's an existing gtpX device to configure */
1173 dev = gtp_find_dev(net, nla_get_u32(info->attrs[GTPA_LINK]));
1180 gtp = netdev_priv(dev);
1183 if (gtp_version == GTP_V0 &&
1184 info->attrs[GTPA_TID]) {
1185 u64 tid = nla_get_u64(info->attrs[GTPA_TID]);
1187 pctx = gtp0_pdp_find(gtp, tid);
1188 } else if (gtp_version == GTP_V1 &&
1189 info->attrs[GTPA_I_TEI]) {
1190 u32 tid = nla_get_u32(info->attrs[GTPA_I_TEI]);
1192 pctx = gtp1_pdp_find(gtp, tid);
1193 } else if (info->attrs[GTPA_MS_ADDRESS]) {
1194 __be32 ip = nla_get_be32(info->attrs[GTPA_MS_ADDRESS]);
1196 pctx = ipv4_pdp_find(gtp, ip);
1204 skb2 = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
1210 err = gtp_genl_fill_info(skb2, NETLINK_CB(skb).portid, info->snd_seq,
1211 0, info->nlhdr->nlmsg_type, pctx);
1213 goto err_unlock_free;
1216 return genlmsg_unicast(genl_info_net(info), skb2, info->snd_portid);
1225 static int gtp_genl_dump_pdp(struct sk_buff *skb,
1226 struct netlink_callback *cb)
1228 struct gtp_dev *last_gtp = (struct gtp_dev *)cb->args[2], *gtp;
1229 int i, j, bucket = cb->args[0], skip = cb->args[1];
1230 struct net *net = sock_net(skb->sk);
1231 struct pdp_ctx *pctx;
1234 gn = net_generic(net, gtp_net_id);
1240 list_for_each_entry_rcu(gtp, &gn->gtp_dev_list, list) {
1241 if (last_gtp && last_gtp != gtp)
1246 for (i = bucket; i < gtp->hash_size; i++) {
1248 hlist_for_each_entry_rcu(pctx, >p->tid_hash[i],
1251 gtp_genl_fill_info(skb,
1252 NETLINK_CB(cb->skb).portid,
1255 cb->nlh->nlmsg_type, pctx)) {
1258 cb->args[2] = (unsigned long)gtp;
1273 static struct nla_policy gtp_genl_policy[GTPA_MAX + 1] = {
1274 [GTPA_LINK] = { .type = NLA_U32, },
1275 [GTPA_VERSION] = { .type = NLA_U32, },
1276 [GTPA_TID] = { .type = NLA_U64, },
1277 [GTPA_SGSN_ADDRESS] = { .type = NLA_U32, },
1278 [GTPA_MS_ADDRESS] = { .type = NLA_U32, },
1279 [GTPA_FLOW] = { .type = NLA_U16, },
1280 [GTPA_NET_NS_FD] = { .type = NLA_U32, },
1281 [GTPA_I_TEI] = { .type = NLA_U32, },
1282 [GTPA_O_TEI] = { .type = NLA_U32, },
1285 static const struct genl_ops gtp_genl_ops[] = {
1287 .cmd = GTP_CMD_NEWPDP,
1288 .doit = gtp_genl_new_pdp,
1289 .policy = gtp_genl_policy,
1290 .flags = GENL_ADMIN_PERM,
1293 .cmd = GTP_CMD_DELPDP,
1294 .doit = gtp_genl_del_pdp,
1295 .policy = gtp_genl_policy,
1296 .flags = GENL_ADMIN_PERM,
1299 .cmd = GTP_CMD_GETPDP,
1300 .doit = gtp_genl_get_pdp,
1301 .dumpit = gtp_genl_dump_pdp,
1302 .policy = gtp_genl_policy,
1303 .flags = GENL_ADMIN_PERM,
1307 static int __net_init gtp_net_init(struct net *net)
1309 struct gtp_net *gn = net_generic(net, gtp_net_id);
1311 INIT_LIST_HEAD(&gn->gtp_dev_list);
1315 static void __net_exit gtp_net_exit(struct net *net)
1317 struct gtp_net *gn = net_generic(net, gtp_net_id);
1318 struct gtp_dev *gtp;
1322 list_for_each_entry(gtp, &gn->gtp_dev_list, list)
1323 gtp_dellink(gtp->dev, &list);
1325 unregister_netdevice_many(&list);
1329 static struct pernet_operations gtp_net_ops = {
1330 .init = gtp_net_init,
1331 .exit = gtp_net_exit,
1333 .size = sizeof(struct gtp_net),
1336 static int __init gtp_init(void)
1340 get_random_bytes(>p_h_initval, sizeof(gtp_h_initval));
1342 err = rtnl_link_register(>p_link_ops);
1346 err = genl_register_family_with_ops(>p_genl_family, gtp_genl_ops);
1348 goto unreg_rtnl_link;
1350 err = register_pernet_subsys(>p_net_ops);
1352 goto unreg_genl_family;
1354 pr_info("GTP module loaded (pdp ctx size %Zd bytes)\n",
1355 sizeof(struct pdp_ctx));
1359 genl_unregister_family(>p_genl_family);
1361 rtnl_link_unregister(>p_link_ops);
1363 pr_err("error loading GTP module loaded\n");
1366 late_initcall(gtp_init);
1368 static void __exit gtp_fini(void)
1370 genl_unregister_family(>p_genl_family);
1371 rtnl_link_unregister(>p_link_ops);
1372 unregister_pernet_subsys(>p_net_ops);
1374 pr_info("GTP module unloaded\n");
1376 module_exit(gtp_fini);
1378 MODULE_LICENSE("GPL");
1379 MODULE_AUTHOR("Harald Welte <hwelte@sysmocom.de>");
1380 MODULE_DESCRIPTION("Interface driver for GTP encapsulated traffic");
1381 MODULE_ALIAS_RTNL_LINK("gtp");
1382 MODULE_ALIAS_GENL_FAMILY("gtp");