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/version.h>
20 #include <linux/skbuff.h>
21 #include <linux/udp.h>
22 #include <linux/rculist.h>
23 #include <linux/jhash.h>
24 #include <linux/if_tunnel.h>
25 #include <linux/net.h>
26 #include <linux/file.h>
27 #include <linux/gtp.h>
29 #include <net/net_namespace.h>
30 #include <net/protocol.h>
33 #include <net/udp_tunnel.h>
36 #include <net/genetlink.h>
37 #include <net/netns/generic.h>
40 /* An active session for the subscriber. */
42 struct hlist_node hlist_tid
;
43 struct hlist_node hlist_addr
;
59 struct in_addr ms_addr_ip4
;
60 struct in_addr sgsn_addr_ip4
;
63 struct rcu_head rcu_head
;
66 /* One instance of the GTP device. */
68 struct list_head list
;
71 struct socket
*sock1u
;
74 struct net_device
*dev
;
76 unsigned int hash_size
;
77 struct hlist_head
*tid_hash
;
78 struct hlist_head
*addr_hash
;
81 static int gtp_net_id __read_mostly
;
84 struct list_head gtp_dev_list
;
87 static u32 gtp_h_initval
;
89 static inline u32
gtp0_hashfn(u64 tid
)
91 u32
*tid32
= (u32
*) &tid
;
92 return jhash_2words(tid32
[0], tid32
[1], gtp_h_initval
);
95 static inline u32
gtp1u_hashfn(u32 tid
)
97 return jhash_1word(tid
, gtp_h_initval
);
100 static inline u32
ipv4_hashfn(__be32 ip
)
102 return jhash_1word((__force u32
)ip
, gtp_h_initval
);
105 /* Resolve a PDP context structure based on the 64bit TID. */
106 static struct pdp_ctx
*gtp0_pdp_find(struct gtp_dev
*gtp
, u64 tid
)
108 struct hlist_head
*head
;
111 head
= >p
->tid_hash
[gtp0_hashfn(tid
) % gtp
->hash_size
];
113 hlist_for_each_entry_rcu(pdp
, head
, hlist_tid
) {
114 if (pdp
->gtp_version
== GTP_V0
&&
115 pdp
->u
.v0
.tid
== tid
)
121 /* Resolve a PDP context structure based on the 32bit TEI. */
122 static struct pdp_ctx
*gtp1_pdp_find(struct gtp_dev
*gtp
, u32 tid
)
124 struct hlist_head
*head
;
127 head
= >p
->tid_hash
[gtp1u_hashfn(tid
) % gtp
->hash_size
];
129 hlist_for_each_entry_rcu(pdp
, head
, hlist_tid
) {
130 if (pdp
->gtp_version
== GTP_V1
&&
131 pdp
->u
.v1
.i_tei
== tid
)
137 /* Resolve a PDP context based on IPv4 address of MS. */
138 static struct pdp_ctx
*ipv4_pdp_find(struct gtp_dev
*gtp
, __be32 ms_addr
)
140 struct hlist_head
*head
;
143 head
= >p
->addr_hash
[ipv4_hashfn(ms_addr
) % gtp
->hash_size
];
145 hlist_for_each_entry_rcu(pdp
, head
, hlist_addr
) {
146 if (pdp
->af
== AF_INET
&&
147 pdp
->ms_addr_ip4
.s_addr
== ms_addr
)
154 static bool gtp_check_src_ms_ipv4(struct sk_buff
*skb
, struct pdp_ctx
*pctx
,
159 if (!pskb_may_pull(skb
, hdrlen
+ sizeof(struct iphdr
)))
162 iph
= (struct iphdr
*)(skb
->data
+ hdrlen
+ sizeof(struct iphdr
));
164 return iph
->saddr
!= pctx
->ms_addr_ip4
.s_addr
;
167 /* Check if the inner IP source address in this packet is assigned to any
168 * existing mobile subscriber.
170 static bool gtp_check_src_ms(struct sk_buff
*skb
, struct pdp_ctx
*pctx
,
173 switch (ntohs(skb
->protocol
)) {
175 return gtp_check_src_ms_ipv4(skb
, pctx
, hdrlen
);
180 /* 1 means pass up to the stack, -1 means drop and 0 means decapsulated. */
181 static int gtp0_udp_encap_recv(struct gtp_dev
*gtp
, struct sk_buff
*skb
,
184 unsigned int hdrlen
= sizeof(struct udphdr
) +
185 sizeof(struct gtp0_header
);
186 struct gtp0_header
*gtp0
;
187 struct pdp_ctx
*pctx
;
190 if (!pskb_may_pull(skb
, hdrlen
))
193 gtp0
= (struct gtp0_header
*)(skb
->data
+ sizeof(struct udphdr
));
195 if ((gtp0
->flags
>> 5) != GTP_V0
)
198 if (gtp0
->type
!= GTP_TPDU
)
202 pctx
= gtp0_pdp_find(gtp
, be64_to_cpu(gtp0
->tid
));
204 netdev_dbg(gtp
->dev
, "No PDP ctx to decap skb=%p\n", skb
);
209 if (!gtp_check_src_ms(skb
, pctx
, hdrlen
)) {
210 netdev_dbg(gtp
->dev
, "No PDP ctx for this MS\n");
216 /* Get rid of the GTP + UDP headers. */
217 return iptunnel_pull_header(skb
, hdrlen
, skb
->protocol
, xnet
);
223 static int gtp1u_udp_encap_recv(struct gtp_dev
*gtp
, struct sk_buff
*skb
,
226 unsigned int hdrlen
= sizeof(struct udphdr
) +
227 sizeof(struct gtp1_header
);
228 struct gtp1_header
*gtp1
;
229 struct pdp_ctx
*pctx
;
232 if (!pskb_may_pull(skb
, hdrlen
))
235 gtp1
= (struct gtp1_header
*)(skb
->data
+ sizeof(struct udphdr
));
237 if ((gtp1
->flags
>> 5) != GTP_V1
)
240 if (gtp1
->type
!= GTP_TPDU
)
243 /* From 29.060: "This field shall be present if and only if any one or
244 * more of the S, PN and E flags are set.".
246 * If any of the bit is set, then the remaining ones also have to be
249 if (gtp1
->flags
& GTP1_F_MASK
)
252 /* Make sure the header is larger enough, including extensions. */
253 if (!pskb_may_pull(skb
, hdrlen
))
256 gtp1
= (struct gtp1_header
*)(skb
->data
+ sizeof(struct udphdr
));
259 pctx
= gtp1_pdp_find(gtp
, ntohl(gtp1
->tid
));
261 netdev_dbg(gtp
->dev
, "No PDP ctx to decap skb=%p\n", skb
);
266 if (!gtp_check_src_ms(skb
, pctx
, hdrlen
)) {
267 netdev_dbg(gtp
->dev
, "No PDP ctx for this MS\n");
273 /* Get rid of the GTP + UDP headers. */
274 return iptunnel_pull_header(skb
, hdrlen
, skb
->protocol
, xnet
);
280 static void gtp_encap_disable(struct gtp_dev
*gtp
)
282 if (gtp
->sock0
&& gtp
->sock0
->sk
) {
283 udp_sk(gtp
->sock0
->sk
)->encap_type
= 0;
284 rcu_assign_sk_user_data(gtp
->sock0
->sk
, NULL
);
286 if (gtp
->sock1u
&& gtp
->sock1u
->sk
) {
287 udp_sk(gtp
->sock1u
->sk
)->encap_type
= 0;
288 rcu_assign_sk_user_data(gtp
->sock1u
->sk
, NULL
);
295 static void gtp_encap_destroy(struct sock
*sk
)
299 gtp
= rcu_dereference_sk_user_data(sk
);
301 gtp_encap_disable(gtp
);
304 /* UDP encapsulation receive handler. See net/ipv4/udp.c.
305 * Return codes: 0: success, <0: error, >0: pass up to userspace UDP socket.
307 static int gtp_encap_recv(struct sock
*sk
, struct sk_buff
*skb
)
309 struct pcpu_sw_netstats
*stats
;
314 gtp
= rcu_dereference_sk_user_data(sk
);
318 netdev_dbg(gtp
->dev
, "encap_recv sk=%p\n", sk
);
320 xnet
= !net_eq(gtp
->net
, dev_net(gtp
->dev
));
322 switch (udp_sk(sk
)->encap_type
) {
324 netdev_dbg(gtp
->dev
, "received GTP0 packet\n");
325 ret
= gtp0_udp_encap_recv(gtp
, skb
, xnet
);
327 case UDP_ENCAP_GTP1U
:
328 netdev_dbg(gtp
->dev
, "received GTP1U packet\n");
329 ret
= gtp1u_udp_encap_recv(gtp
, skb
, xnet
);
332 ret
= -1; /* Shouldn't happen. */
337 netdev_dbg(gtp
->dev
, "pass up to the process\n");
340 netdev_dbg(gtp
->dev
, "forwarding packet from GGSN to uplink\n");
343 netdev_dbg(gtp
->dev
, "GTP packet has been dropped\n");
348 /* Now that the UDP and the GTP header have been removed, set up the
349 * new network header. This is required by the upper layer to
350 * calculate the transport header.
352 skb_reset_network_header(skb
);
356 stats
= this_cpu_ptr(gtp
->dev
->tstats
);
357 u64_stats_update_begin(&stats
->syncp
);
359 stats
->rx_bytes
+= skb
->len
;
360 u64_stats_update_end(&stats
->syncp
);
367 static int gtp_dev_init(struct net_device
*dev
)
369 struct gtp_dev
*gtp
= netdev_priv(dev
);
373 dev
->tstats
= alloc_percpu(struct pcpu_sw_netstats
);
380 static void gtp_dev_uninit(struct net_device
*dev
)
382 struct gtp_dev
*gtp
= netdev_priv(dev
);
384 gtp_encap_disable(gtp
);
385 free_percpu(dev
->tstats
);
388 static struct rtable
*ip4_route_output_gtp(struct net
*net
, struct flowi4
*fl4
,
389 const struct sock
*sk
, __be32 daddr
)
391 memset(fl4
, 0, sizeof(*fl4
));
392 fl4
->flowi4_oif
= sk
->sk_bound_dev_if
;
394 fl4
->saddr
= inet_sk(sk
)->inet_saddr
;
395 fl4
->flowi4_tos
= RT_CONN_FLAGS(sk
);
396 fl4
->flowi4_proto
= sk
->sk_protocol
;
398 return ip_route_output_key(net
, fl4
);
401 static inline void gtp0_push_header(struct sk_buff
*skb
, struct pdp_ctx
*pctx
)
403 int payload_len
= skb
->len
;
404 struct gtp0_header
*gtp0
;
406 gtp0
= (struct gtp0_header
*) skb_push(skb
, sizeof(*gtp0
));
408 gtp0
->flags
= 0x1e; /* v0, GTP-non-prime. */
409 gtp0
->type
= GTP_TPDU
;
410 gtp0
->length
= htons(payload_len
);
411 gtp0
->seq
= htons((atomic_inc_return(&pctx
->tx_seq
) - 1) % 0xffff);
412 gtp0
->flow
= htons(pctx
->u
.v0
.flow
);
414 gtp0
->spare
[0] = gtp0
->spare
[1] = gtp0
->spare
[2] = 0xff;
415 gtp0
->tid
= cpu_to_be64(pctx
->u
.v0
.tid
);
418 static inline void gtp1_push_header(struct sk_buff
*skb
, struct pdp_ctx
*pctx
)
420 int payload_len
= skb
->len
;
421 struct gtp1_header
*gtp1
;
423 gtp1
= (struct gtp1_header
*) skb_push(skb
, sizeof(*gtp1
));
425 /* Bits 8 7 6 5 4 3 2 1
426 * +--+--+--+--+--+--+--+--+
427 * |version |PT| 1| E| S|PN|
428 * +--+--+--+--+--+--+--+--+
431 gtp1
->flags
= 0x38; /* v1, GTP-non-prime. */
432 gtp1
->type
= GTP_TPDU
;
433 gtp1
->length
= htons(payload_len
);
434 gtp1
->tid
= htonl(pctx
->u
.v1
.o_tei
);
436 /* TODO: Suppport for extension header, sequence number and N-PDU.
437 * Update the length field if any of them is available.
446 struct pdp_ctx
*pctx
;
447 struct net_device
*dev
;
451 static void gtp_push_header(struct sk_buff
*skb
, struct gtp_pktinfo
*pktinfo
)
453 switch (pktinfo
->pctx
->gtp_version
) {
455 pktinfo
->gtph_port
= htons(GTP0_PORT
);
456 gtp0_push_header(skb
, pktinfo
->pctx
);
459 pktinfo
->gtph_port
= htons(GTP1U_PORT
);
460 gtp1_push_header(skb
, pktinfo
->pctx
);
465 static inline void gtp_set_pktinfo_ipv4(struct gtp_pktinfo
*pktinfo
,
466 struct sock
*sk
, struct iphdr
*iph
,
467 struct pdp_ctx
*pctx
, struct rtable
*rt
,
469 struct net_device
*dev
)
473 pktinfo
->pctx
= pctx
;
479 static int gtp_build_skb_ip4(struct sk_buff
*skb
, struct net_device
*dev
,
480 struct gtp_pktinfo
*pktinfo
)
482 struct gtp_dev
*gtp
= netdev_priv(dev
);
483 struct pdp_ctx
*pctx
;
491 /* Read the IP destination address and resolve the PDP context.
492 * Prepend PDP header with TEI/TID from PDP ctx.
495 pctx
= ipv4_pdp_find(gtp
, iph
->daddr
);
497 netdev_dbg(dev
, "no PDP ctx found for %pI4, skip\n",
501 netdev_dbg(dev
, "found PDP context %p\n", pctx
);
503 switch (pctx
->gtp_version
) {
512 sk
= gtp
->sock1u
->sk
;
521 netdev_dbg(dev
, "no userspace socket is available, skip\n");
525 rt
= ip4_route_output_gtp(sock_net(sk
), &fl4
, gtp
->sock0
->sk
,
526 pctx
->sgsn_addr_ip4
.s_addr
);
528 netdev_dbg(dev
, "no route to SSGN %pI4\n",
529 &pctx
->sgsn_addr_ip4
.s_addr
);
530 dev
->stats
.tx_carrier_errors
++;
534 if (rt
->dst
.dev
== dev
) {
535 netdev_dbg(dev
, "circular route to SSGN %pI4\n",
536 &pctx
->sgsn_addr_ip4
.s_addr
);
537 dev
->stats
.collisions
++;
543 /* This is similar to tnl_update_pmtu(). */
546 mtu
= dst_mtu(&rt
->dst
) - dev
->hard_header_len
-
547 sizeof(struct iphdr
) - sizeof(struct udphdr
);
548 switch (pctx
->gtp_version
) {
550 mtu
-= sizeof(struct gtp0_header
);
553 mtu
-= sizeof(struct gtp1_header
);
557 mtu
= dst_mtu(&rt
->dst
);
560 rt
->dst
.ops
->update_pmtu(&rt
->dst
, NULL
, skb
, mtu
);
562 if (!skb_is_gso(skb
) && (iph
->frag_off
& htons(IP_DF
)) &&
563 mtu
< ntohs(iph
->tot_len
)) {
564 netdev_dbg(dev
, "packet too big, fragmentation needed\n");
565 memset(IPCB(skb
), 0, sizeof(*IPCB(skb
)));
566 icmp_send(skb
, ICMP_DEST_UNREACH
, ICMP_FRAG_NEEDED
,
571 gtp_set_pktinfo_ipv4(pktinfo
, sk
, iph
, pctx
, rt
, &fl4
, dev
);
572 gtp_push_header(skb
, pktinfo
);
581 static netdev_tx_t
gtp_dev_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
583 unsigned int proto
= ntohs(skb
->protocol
);
584 struct gtp_pktinfo pktinfo
;
587 /* Ensure there is sufficient headroom. */
588 if (skb_cow_head(skb
, dev
->needed_headroom
))
591 skb_reset_inner_headers(skb
);
593 /* PDP context lookups in gtp_build_skb_*() need rcu read-side lock. */
597 err
= gtp_build_skb_ip4(skb
, dev
, &pktinfo
);
610 netdev_dbg(pktinfo
.dev
, "gtp -> IP src: %pI4 dst: %pI4\n",
611 &pktinfo
.iph
->saddr
, &pktinfo
.iph
->daddr
);
612 udp_tunnel_xmit_skb(pktinfo
.rt
, pktinfo
.sk
, skb
,
613 pktinfo
.fl4
.saddr
, pktinfo
.fl4
.daddr
,
615 ip4_dst_hoplimit(&pktinfo
.rt
->dst
),
617 pktinfo
.gtph_port
, pktinfo
.gtph_port
,
624 dev
->stats
.tx_errors
++;
629 static const struct net_device_ops gtp_netdev_ops
= {
630 .ndo_init
= gtp_dev_init
,
631 .ndo_uninit
= gtp_dev_uninit
,
632 .ndo_start_xmit
= gtp_dev_xmit
,
633 .ndo_get_stats64
= ip_tunnel_get_stats64
,
636 static void gtp_link_setup(struct net_device
*dev
)
638 dev
->netdev_ops
= >p_netdev_ops
;
639 dev
->destructor
= free_netdev
;
641 dev
->hard_header_len
= 0;
644 /* Zero header length. */
645 dev
->type
= ARPHRD_NONE
;
646 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
| IFF_MULTICAST
;
648 dev
->priv_flags
|= IFF_NO_QUEUE
;
649 dev
->features
|= NETIF_F_LLTX
;
652 /* Assume largest header, ie. GTPv0. */
653 dev
->needed_headroom
= LL_MAX_HEADER
+
654 sizeof(struct iphdr
) +
655 sizeof(struct udphdr
) +
656 sizeof(struct gtp0_header
);
659 static int gtp_hashtable_new(struct gtp_dev
*gtp
, int hsize
);
660 static void gtp_hashtable_free(struct gtp_dev
*gtp
);
661 static int gtp_encap_enable(struct net_device
*dev
, struct gtp_dev
*gtp
,
662 int fd_gtp0
, int fd_gtp1
, struct net
*src_net
);
664 static int gtp_newlink(struct net
*src_net
, struct net_device
*dev
,
665 struct nlattr
*tb
[], struct nlattr
*data
[])
667 int hashsize
, err
, fd0
, fd1
;
671 if (!data
[IFLA_GTP_FD0
] || !data
[IFLA_GTP_FD1
])
674 gtp
= netdev_priv(dev
);
676 fd0
= nla_get_u32(data
[IFLA_GTP_FD0
]);
677 fd1
= nla_get_u32(data
[IFLA_GTP_FD1
]);
679 err
= gtp_encap_enable(dev
, gtp
, fd0
, fd1
, src_net
);
683 if (!data
[IFLA_GTP_PDP_HASHSIZE
])
686 hashsize
= nla_get_u32(data
[IFLA_GTP_PDP_HASHSIZE
]);
688 err
= gtp_hashtable_new(gtp
, hashsize
);
692 err
= register_netdevice(dev
);
694 netdev_dbg(dev
, "failed to register new netdev %d\n", err
);
698 gn
= net_generic(dev_net(dev
), gtp_net_id
);
699 list_add_rcu(>p
->list
, &gn
->gtp_dev_list
);
701 netdev_dbg(dev
, "registered new GTP interface\n");
706 gtp_hashtable_free(gtp
);
708 gtp_encap_disable(gtp
);
713 static void gtp_dellink(struct net_device
*dev
, struct list_head
*head
)
715 struct gtp_dev
*gtp
= netdev_priv(dev
);
717 gtp_encap_disable(gtp
);
718 gtp_hashtable_free(gtp
);
719 list_del_rcu(>p
->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
},
729 static int gtp_validate(struct nlattr
*tb
[], struct nlattr
*data
[])
737 static size_t gtp_get_size(const struct net_device
*dev
)
739 return nla_total_size(sizeof(__u32
)); /* IFLA_GTP_PDP_HASHSIZE */
742 static int gtp_fill_info(struct sk_buff
*skb
, const struct net_device
*dev
)
744 struct gtp_dev
*gtp
= netdev_priv(dev
);
746 if (nla_put_u32(skb
, IFLA_GTP_PDP_HASHSIZE
, gtp
->hash_size
))
747 goto nla_put_failure
;
755 static struct rtnl_link_ops gtp_link_ops __read_mostly
= {
757 .maxtype
= IFLA_GTP_MAX
,
758 .policy
= gtp_policy
,
759 .priv_size
= sizeof(struct gtp_dev
),
760 .setup
= gtp_link_setup
,
761 .validate
= gtp_validate
,
762 .newlink
= gtp_newlink
,
763 .dellink
= gtp_dellink
,
764 .get_size
= gtp_get_size
,
765 .fill_info
= gtp_fill_info
,
768 static struct net
*gtp_genl_get_net(struct net
*src_net
, struct nlattr
*tb
[])
772 /* Examine the link attributes and figure out which network namespace
773 * we are talking about.
775 if (tb
[GTPA_NET_NS_FD
])
776 net
= get_net_ns_by_fd(nla_get_u32(tb
[GTPA_NET_NS_FD
]));
778 net
= get_net(src_net
);
783 static int gtp_hashtable_new(struct gtp_dev
*gtp
, int hsize
)
787 gtp
->addr_hash
= kmalloc(sizeof(struct hlist_head
) * hsize
, GFP_KERNEL
);
788 if (gtp
->addr_hash
== NULL
)
791 gtp
->tid_hash
= kmalloc(sizeof(struct hlist_head
) * hsize
, GFP_KERNEL
);
792 if (gtp
->tid_hash
== NULL
)
795 gtp
->hash_size
= hsize
;
797 for (i
= 0; i
< hsize
; i
++) {
798 INIT_HLIST_HEAD(>p
->addr_hash
[i
]);
799 INIT_HLIST_HEAD(>p
->tid_hash
[i
]);
803 kfree(gtp
->addr_hash
);
807 static void gtp_hashtable_free(struct gtp_dev
*gtp
)
809 struct pdp_ctx
*pctx
;
812 for (i
= 0; i
< gtp
->hash_size
; i
++) {
813 hlist_for_each_entry_rcu(pctx
, >p
->tid_hash
[i
], hlist_tid
) {
814 hlist_del_rcu(&pctx
->hlist_tid
);
815 hlist_del_rcu(&pctx
->hlist_addr
);
816 kfree_rcu(pctx
, rcu_head
);
820 kfree(gtp
->addr_hash
);
821 kfree(gtp
->tid_hash
);
824 static int gtp_encap_enable(struct net_device
*dev
, struct gtp_dev
*gtp
,
825 int fd_gtp0
, int fd_gtp1
, struct net
*src_net
)
827 struct udp_tunnel_sock_cfg tuncfg
= {NULL
};
828 struct socket
*sock0
, *sock1u
;
831 netdev_dbg(dev
, "enable gtp on %d, %d\n", fd_gtp0
, fd_gtp1
);
833 sock0
= sockfd_lookup(fd_gtp0
, &err
);
835 netdev_dbg(dev
, "socket fd=%d not found (gtp0)\n", fd_gtp0
);
839 if (sock0
->sk
->sk_protocol
!= IPPROTO_UDP
) {
840 netdev_dbg(dev
, "socket fd=%d not UDP\n", fd_gtp0
);
845 sock1u
= sockfd_lookup(fd_gtp1
, &err
);
846 if (sock1u
== NULL
) {
847 netdev_dbg(dev
, "socket fd=%d not found (gtp1u)\n", fd_gtp1
);
852 if (sock1u
->sk
->sk_protocol
!= IPPROTO_UDP
) {
853 netdev_dbg(dev
, "socket fd=%d not UDP\n", fd_gtp1
);
858 netdev_dbg(dev
, "enable gtp on %p, %p\n", sock0
, sock1u
);
861 gtp
->sock1u
= sock1u
;
864 tuncfg
.sk_user_data
= gtp
;
865 tuncfg
.encap_rcv
= gtp_encap_recv
;
866 tuncfg
.encap_destroy
= gtp_encap_destroy
;
868 tuncfg
.encap_type
= UDP_ENCAP_GTP0
;
869 setup_udp_tunnel_sock(sock_net(gtp
->sock0
->sk
), gtp
->sock0
, &tuncfg
);
871 tuncfg
.encap_type
= UDP_ENCAP_GTP1U
;
872 setup_udp_tunnel_sock(sock_net(gtp
->sock1u
->sk
), gtp
->sock1u
, &tuncfg
);
882 static struct net_device
*gtp_find_dev(struct net
*net
, int ifindex
)
884 struct gtp_net
*gn
= net_generic(net
, gtp_net_id
);
887 list_for_each_entry_rcu(gtp
, &gn
->gtp_dev_list
, list
) {
888 if (ifindex
== gtp
->dev
->ifindex
)
894 static void ipv4_pdp_fill(struct pdp_ctx
*pctx
, struct genl_info
*info
)
896 pctx
->gtp_version
= nla_get_u32(info
->attrs
[GTPA_VERSION
]);
898 pctx
->sgsn_addr_ip4
.s_addr
=
899 nla_get_be32(info
->attrs
[GTPA_SGSN_ADDRESS
]);
900 pctx
->ms_addr_ip4
.s_addr
=
901 nla_get_be32(info
->attrs
[GTPA_MS_ADDRESS
]);
903 switch (pctx
->gtp_version
) {
905 /* According to TS 09.60, sections 7.5.1 and 7.5.2, the flow
906 * label needs to be the same for uplink and downlink packets,
907 * so let's annotate this.
909 pctx
->u
.v0
.tid
= nla_get_u64(info
->attrs
[GTPA_TID
]);
910 pctx
->u
.v0
.flow
= nla_get_u16(info
->attrs
[GTPA_FLOW
]);
913 pctx
->u
.v1
.i_tei
= nla_get_u32(info
->attrs
[GTPA_I_TEI
]);
914 pctx
->u
.v1
.o_tei
= nla_get_u32(info
->attrs
[GTPA_O_TEI
]);
921 static int ipv4_pdp_add(struct net_device
*dev
, struct genl_info
*info
)
923 struct gtp_dev
*gtp
= netdev_priv(dev
);
924 u32 hash_ms
, hash_tid
= 0;
925 struct pdp_ctx
*pctx
;
929 ms_addr
= nla_get_be32(info
->attrs
[GTPA_MS_ADDRESS
]);
930 hash_ms
= ipv4_hashfn(ms_addr
) % gtp
->hash_size
;
932 hlist_for_each_entry_rcu(pctx
, >p
->addr_hash
[hash_ms
], hlist_addr
) {
933 if (pctx
->ms_addr_ip4
.s_addr
== ms_addr
) {
940 if (info
->nlhdr
->nlmsg_flags
& NLM_F_EXCL
)
942 if (info
->nlhdr
->nlmsg_flags
& NLM_F_REPLACE
)
945 ipv4_pdp_fill(pctx
, info
);
947 if (pctx
->gtp_version
== GTP_V0
)
948 netdev_dbg(dev
, "GTPv0-U: update tunnel id = %llx (pdp %p)\n",
949 pctx
->u
.v0
.tid
, pctx
);
950 else if (pctx
->gtp_version
== GTP_V1
)
951 netdev_dbg(dev
, "GTPv1-U: update tunnel id = %x/%x (pdp %p)\n",
952 pctx
->u
.v1
.i_tei
, pctx
->u
.v1
.o_tei
, pctx
);
958 pctx
= kmalloc(sizeof(struct pdp_ctx
), GFP_KERNEL
);
962 ipv4_pdp_fill(pctx
, info
);
963 atomic_set(&pctx
->tx_seq
, 0);
965 switch (pctx
->gtp_version
) {
967 /* TS 09.60: "The flow label identifies unambiguously a GTP
968 * flow.". We use the tid for this instead, I cannot find a
969 * situation in which this doesn't unambiguosly identify the
972 hash_tid
= gtp0_hashfn(pctx
->u
.v0
.tid
) % gtp
->hash_size
;
975 hash_tid
= gtp1u_hashfn(pctx
->u
.v1
.i_tei
) % gtp
->hash_size
;
979 hlist_add_head_rcu(&pctx
->hlist_addr
, >p
->addr_hash
[hash_ms
]);
980 hlist_add_head_rcu(&pctx
->hlist_tid
, >p
->tid_hash
[hash_tid
]);
982 switch (pctx
->gtp_version
) {
984 netdev_dbg(dev
, "GTPv0-U: new PDP ctx id=%llx ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
985 pctx
->u
.v0
.tid
, &pctx
->sgsn_addr_ip4
,
986 &pctx
->ms_addr_ip4
, pctx
);
989 netdev_dbg(dev
, "GTPv1-U: new PDP ctx id=%x/%x ssgn=%pI4 ms=%pI4 (pdp=%p)\n",
990 pctx
->u
.v1
.i_tei
, pctx
->u
.v1
.o_tei
,
991 &pctx
->sgsn_addr_ip4
, &pctx
->ms_addr_ip4
, pctx
);
998 static int gtp_genl_new_pdp(struct sk_buff
*skb
, struct genl_info
*info
)
1000 struct net_device
*dev
;
1003 if (!info
->attrs
[GTPA_VERSION
] ||
1004 !info
->attrs
[GTPA_LINK
] ||
1005 !info
->attrs
[GTPA_SGSN_ADDRESS
] ||
1006 !info
->attrs
[GTPA_MS_ADDRESS
])
1009 switch (nla_get_u32(info
->attrs
[GTPA_VERSION
])) {
1011 if (!info
->attrs
[GTPA_TID
] ||
1012 !info
->attrs
[GTPA_FLOW
])
1016 if (!info
->attrs
[GTPA_I_TEI
] ||
1017 !info
->attrs
[GTPA_O_TEI
])
1025 net
= gtp_genl_get_net(sock_net(skb
->sk
), info
->attrs
);
1027 return PTR_ERR(net
);
1029 /* Check if there's an existing gtpX device to configure */
1030 dev
= gtp_find_dev(net
, nla_get_u32(info
->attrs
[GTPA_LINK
]));
1037 return ipv4_pdp_add(dev
, info
);
1040 static int gtp_genl_del_pdp(struct sk_buff
*skb
, struct genl_info
*info
)
1042 struct net_device
*dev
;
1043 struct pdp_ctx
*pctx
;
1044 struct gtp_dev
*gtp
;
1047 if (!info
->attrs
[GTPA_VERSION
] ||
1048 !info
->attrs
[GTPA_LINK
])
1051 net
= gtp_genl_get_net(sock_net(skb
->sk
), info
->attrs
);
1053 return PTR_ERR(net
);
1055 /* Check if there's an existing gtpX device to configure */
1056 dev
= gtp_find_dev(net
, nla_get_u32(info
->attrs
[GTPA_LINK
]));
1063 gtp
= netdev_priv(dev
);
1065 switch (nla_get_u32(info
->attrs
[GTPA_VERSION
])) {
1067 if (!info
->attrs
[GTPA_TID
])
1069 pctx
= gtp0_pdp_find(gtp
, nla_get_u64(info
->attrs
[GTPA_TID
]));
1072 if (!info
->attrs
[GTPA_I_TEI
])
1074 pctx
= gtp1_pdp_find(gtp
, nla_get_u64(info
->attrs
[GTPA_I_TEI
]));
1084 if (pctx
->gtp_version
== GTP_V0
)
1085 netdev_dbg(dev
, "GTPv0-U: deleting tunnel id = %llx (pdp %p)\n",
1086 pctx
->u
.v0
.tid
, pctx
);
1087 else if (pctx
->gtp_version
== GTP_V1
)
1088 netdev_dbg(dev
, "GTPv1-U: deleting tunnel id = %x/%x (pdp %p)\n",
1089 pctx
->u
.v1
.i_tei
, pctx
->u
.v1
.o_tei
, pctx
);
1091 hlist_del_rcu(&pctx
->hlist_tid
);
1092 hlist_del_rcu(&pctx
->hlist_addr
);
1093 kfree_rcu(pctx
, rcu_head
);
1098 static struct genl_family gtp_genl_family
= {
1099 .id
= GENL_ID_GENERATE
,
1103 .maxattr
= GTPA_MAX
,
1107 static int gtp_genl_fill_info(struct sk_buff
*skb
, u32 snd_portid
, u32 snd_seq
,
1108 u32 type
, struct pdp_ctx
*pctx
)
1112 genlh
= genlmsg_put(skb
, snd_portid
, snd_seq
, >p_genl_family
, 0,
1117 if (nla_put_u32(skb
, GTPA_VERSION
, pctx
->gtp_version
) ||
1118 nla_put_be32(skb
, GTPA_SGSN_ADDRESS
, pctx
->sgsn_addr_ip4
.s_addr
) ||
1119 nla_put_be32(skb
, GTPA_MS_ADDRESS
, pctx
->ms_addr_ip4
.s_addr
))
1120 goto nla_put_failure
;
1122 switch (pctx
->gtp_version
) {
1124 if (nla_put_u64_64bit(skb
, GTPA_TID
, pctx
->u
.v0
.tid
, GTPA_PAD
) ||
1125 nla_put_u16(skb
, GTPA_FLOW
, pctx
->u
.v0
.flow
))
1126 goto nla_put_failure
;
1129 if (nla_put_u32(skb
, GTPA_I_TEI
, pctx
->u
.v1
.i_tei
) ||
1130 nla_put_u32(skb
, GTPA_O_TEI
, pctx
->u
.v1
.o_tei
))
1131 goto nla_put_failure
;
1134 genlmsg_end(skb
, genlh
);
1139 genlmsg_cancel(skb
, genlh
);
1143 static int gtp_genl_get_pdp(struct sk_buff
*skb
, struct genl_info
*info
)
1145 struct pdp_ctx
*pctx
= NULL
;
1146 struct net_device
*dev
;
1147 struct sk_buff
*skb2
;
1148 struct gtp_dev
*gtp
;
1153 if (!info
->attrs
[GTPA_VERSION
] ||
1154 !info
->attrs
[GTPA_LINK
])
1157 gtp_version
= nla_get_u32(info
->attrs
[GTPA_VERSION
]);
1158 switch (gtp_version
) {
1166 net
= gtp_genl_get_net(sock_net(skb
->sk
), info
->attrs
);
1168 return PTR_ERR(net
);
1170 /* Check if there's an existing gtpX device to configure */
1171 dev
= gtp_find_dev(net
, nla_get_u32(info
->attrs
[GTPA_LINK
]));
1178 gtp
= netdev_priv(dev
);
1181 if (gtp_version
== GTP_V0
&&
1182 info
->attrs
[GTPA_TID
]) {
1183 u64 tid
= nla_get_u64(info
->attrs
[GTPA_TID
]);
1185 pctx
= gtp0_pdp_find(gtp
, tid
);
1186 } else if (gtp_version
== GTP_V1
&&
1187 info
->attrs
[GTPA_I_TEI
]) {
1188 u32 tid
= nla_get_u32(info
->attrs
[GTPA_I_TEI
]);
1190 pctx
= gtp1_pdp_find(gtp
, tid
);
1191 } else if (info
->attrs
[GTPA_MS_ADDRESS
]) {
1192 __be32 ip
= nla_get_be32(info
->attrs
[GTPA_MS_ADDRESS
]);
1194 pctx
= ipv4_pdp_find(gtp
, ip
);
1202 skb2
= genlmsg_new(NLMSG_GOODSIZE
, GFP_ATOMIC
);
1208 err
= gtp_genl_fill_info(skb2
, NETLINK_CB(skb
).portid
,
1209 info
->snd_seq
, info
->nlhdr
->nlmsg_type
, pctx
);
1211 goto err_unlock_free
;
1214 return genlmsg_unicast(genl_info_net(info
), skb2
, info
->snd_portid
);
1223 static int gtp_genl_dump_pdp(struct sk_buff
*skb
,
1224 struct netlink_callback
*cb
)
1226 struct gtp_dev
*last_gtp
= (struct gtp_dev
*)cb
->args
[2], *gtp
;
1227 struct net
*net
= sock_net(skb
->sk
);
1228 struct gtp_net
*gn
= net_generic(net
, gtp_net_id
);
1229 unsigned long tid
= cb
->args
[1];
1230 int i
, k
= cb
->args
[0], ret
;
1231 struct pdp_ctx
*pctx
;
1236 list_for_each_entry_rcu(gtp
, &gn
->gtp_dev_list
, list
) {
1237 if (last_gtp
&& last_gtp
!= gtp
)
1242 for (i
= k
; i
< gtp
->hash_size
; i
++) {
1243 hlist_for_each_entry_rcu(pctx
, >p
->tid_hash
[i
], hlist_tid
) {
1244 if (tid
&& tid
!= pctx
->u
.tid
)
1249 ret
= gtp_genl_fill_info(skb
,
1250 NETLINK_CB(cb
->skb
).portid
,
1252 cb
->nlh
->nlmsg_type
, pctx
);
1255 cb
->args
[1] = pctx
->u
.tid
;
1256 cb
->args
[2] = (unsigned long)gtp
;
1267 static struct nla_policy gtp_genl_policy
[GTPA_MAX
+ 1] = {
1268 [GTPA_LINK
] = { .type
= NLA_U32
, },
1269 [GTPA_VERSION
] = { .type
= NLA_U32
, },
1270 [GTPA_TID
] = { .type
= NLA_U64
, },
1271 [GTPA_SGSN_ADDRESS
] = { .type
= NLA_U32
, },
1272 [GTPA_MS_ADDRESS
] = { .type
= NLA_U32
, },
1273 [GTPA_FLOW
] = { .type
= NLA_U16
, },
1274 [GTPA_NET_NS_FD
] = { .type
= NLA_U32
, },
1275 [GTPA_I_TEI
] = { .type
= NLA_U32
, },
1276 [GTPA_O_TEI
] = { .type
= NLA_U32
, },
1279 static const struct genl_ops gtp_genl_ops
[] = {
1281 .cmd
= GTP_CMD_NEWPDP
,
1282 .doit
= gtp_genl_new_pdp
,
1283 .policy
= gtp_genl_policy
,
1284 .flags
= GENL_ADMIN_PERM
,
1287 .cmd
= GTP_CMD_DELPDP
,
1288 .doit
= gtp_genl_del_pdp
,
1289 .policy
= gtp_genl_policy
,
1290 .flags
= GENL_ADMIN_PERM
,
1293 .cmd
= GTP_CMD_GETPDP
,
1294 .doit
= gtp_genl_get_pdp
,
1295 .dumpit
= gtp_genl_dump_pdp
,
1296 .policy
= gtp_genl_policy
,
1297 .flags
= GENL_ADMIN_PERM
,
1301 static int __net_init
gtp_net_init(struct net
*net
)
1303 struct gtp_net
*gn
= net_generic(net
, gtp_net_id
);
1305 INIT_LIST_HEAD(&gn
->gtp_dev_list
);
1309 static void __net_exit
gtp_net_exit(struct net
*net
)
1311 struct gtp_net
*gn
= net_generic(net
, gtp_net_id
);
1312 struct gtp_dev
*gtp
;
1316 list_for_each_entry(gtp
, &gn
->gtp_dev_list
, list
)
1317 gtp_dellink(gtp
->dev
, &list
);
1319 unregister_netdevice_many(&list
);
1323 static struct pernet_operations gtp_net_ops
= {
1324 .init
= gtp_net_init
,
1325 .exit
= gtp_net_exit
,
1327 .size
= sizeof(struct gtp_net
),
1330 static int __init
gtp_init(void)
1334 get_random_bytes(>p_h_initval
, sizeof(gtp_h_initval
));
1336 err
= rtnl_link_register(>p_link_ops
);
1340 err
= genl_register_family_with_ops(>p_genl_family
, gtp_genl_ops
);
1342 goto unreg_rtnl_link
;
1344 err
= register_pernet_subsys(>p_net_ops
);
1346 goto unreg_genl_family
;
1348 pr_info("GTP module loaded (pdp ctx size %Zd bytes)\n",
1349 sizeof(struct pdp_ctx
));
1353 genl_unregister_family(>p_genl_family
);
1355 rtnl_link_unregister(>p_link_ops
);
1357 pr_err("error loading GTP module loaded\n");
1360 late_initcall(gtp_init
);
1362 static void __exit
gtp_fini(void)
1364 unregister_pernet_subsys(>p_net_ops
);
1365 genl_unregister_family(>p_genl_family
);
1366 rtnl_link_unregister(>p_link_ops
);
1368 pr_info("GTP module unloaded\n");
1370 module_exit(gtp_fini
);
1372 MODULE_LICENSE("GPL");
1373 MODULE_AUTHOR("Harald Welte <hwelte@sysmocom.de>");
1374 MODULE_DESCRIPTION("Interface driver for GTP encapsulated traffic");
1375 MODULE_ALIAS_RTNL_LINK("gtp");