Commit Graph

128 Commits

Author SHA1 Message Date
Andreas Schultz b7b46cd757 gtp: add some debug instrumentation
Signed-off-by: Andreas Schultz <>
Signed-off-by: Pablo Neira Ayuso <>
2015-11-17 13:35:04 +01:00
Andreas Schultz 976dcfc9f1 gtp: fix the order of error cases in gtp_encap_enable
The ordering of the error case exit was wrong and would
attempt to release the wrong socket.

Signed-off-by: Andreas Schultz <>
Signed-off-by: Pablo Neira Ayuso <>
2015-11-16 18:58:37 +01:00
Andreas Schultz b40f105841 gtp: convert the global gtp_instance_list to a per netns list
This add basic network namespace support by changing to global
gtp_instance_list into a pre namespace list.
Before this change all pdp context would be visible from all
network namespaces, now only the namespace that they belong too,
can see them.

Also selectively destroy all gtp devices when a namespace is

Signed-off-by: Andreas Schultz <>
Signed-off-by: Pablo Neira Ayuso <>
2015-11-16 18:58:32 +01:00
Andreas Schultz 10855555e9 gtp: remove genl_register_family_with_ops for Linux < 3.13
Remove the support for genl_register_family_with_ops for
Linux < 3.13. Also reorder the initialization to be more
in line with similar modules.

Signed-off-by: Andreas Schultz <>
Signed-off-by: Pablo Neira Ayuso <>
2015-11-16 18:33:59 +01:00
Andreas Schultz 7018f19683 gtp: remove unused local variable
Signed-off-by: Andreas Schultz <>
Signed-off-by: Pablo Neira Ayuso <>
2015-11-16 18:33:20 +01:00
Andreas Schultz 6deeee8cf3 gtp: update for Linux > 4.2+, set NO_QUEUE flag on gtp device
Signed-off-by: Andreas Schultz <>
Signed-off-by: Pablo Neira Ayuso <>
2015-11-07 17:55:47 +01:00
Andreas Schultz 6bc7645c24 gtp: update for Linux > 4.1+, genlmsg_end return should be ignored
Signed-off-by: Andreas Schultz <>
Signed-off-by: Pablo Neira Ayuso <>
2015-11-07 17:51:50 +01:00
Andreas Schultz 090d6d1b89 gtp: update for Linux > 4.1+, __ip_select_ident arguments have changed
Signed-off-by: Andreas Schultz <>
Signed-off-by: Pablo Neira Ayuso <>
2015-11-07 17:51:33 +01:00
Andreas Schultz 9afbef8d18 gtp: remove per cpu stats compat ifdef for Linux < 3.13
Signed-off-by: Andreas Schultz <>
Signed-off-by: Pablo Neira Ayuso <>
2015-11-07 17:51:17 +01:00
Andreas Schultz 3a45fc68e0 gtp: remove genl_ops compat ifdef for Linux < 3.13
Signed-off-by: Andreas Schultz <>
Signed-off-by: Pablo Neira Ayuso <>
2015-11-07 17:50:55 +01:00
Andreas Schultz 785aa50a6b build: update Makefile for simpler out of tree builds
Signed-off-by: Andreas Schultz <>
Signed-off-by: Pablo Neira Ayuso <>
2015-11-07 17:50:27 +01:00
Harald Welte fba534b95a Add copright headers and license files, list Pablo as author
The kernel module still only listed me as author, even though Pablo
was doing most of the work on it after my initial incomplete version.
2014-04-03 09:37:38 -04:00
Pablo Neira Ayuso 1a18b8689d build: link this library to libmnl
With this change, application don't need to pass to the linker -lmnl
which is the right way to make it since they don't directly refer to
mnl_* functions.
2014-03-21 13:54:53 +01:00
Pablo Neira Ayuso 892ec8664c gtp-rtnl: add gtp_dev_config function
This function allows us to set the gtp0 device configuration the route
to encapsulate all traffic that is addressed to the MS from the GGSN
2014-03-20 16:47:00 +01:00
Pablo Neira Ayuso 4779603790 gtp-rtnl: refactor common rtnetlink socket initialization code
This allow reusing this code in follow up changes.
2014-03-20 16:21:29 +01:00
Pablo Neira Ayuso e83f70671e libgtpnl: add flow id support
Add new attribute to the gtp tunnel object, this extension is required
by GTPv0 since the same flowid needs to be used for uplink and downlink
2014-03-20 13:57:22 +01:00
Pablo Neira Ayuso a03b3754d6 gtp: set flow ID for PDP context created throught GTPv0
This field is very important, downlink and uplink packet shall use
the same flow ID.
2014-03-20 13:43:04 +01:00
Pablo Neira Ayuso 9b649117b6 libgtpnl: tools: gtp-tunnel: display teid in hexadecimal
So it is displayed like in wireshark.
2014-03-20 12:56:31 +01:00
Pablo Neira Ayuso 57c6ecb185 gtp: add attribute validation for GTPA_TID
Make sure userspace passes a u64.
2014-03-20 12:55:43 +01:00
Pablo Neira Ayuso 0eca321846 gtp: fix hashing of GTPv0 entries in the forwarding base
Use the corresponding hash function depending on the version
type. Using ipv4_hashfn was working with GTPv1 because the
TEI is also 32-bits, bit GTPv0 was broken.
2014-03-20 12:55:41 +01:00
Pablo Neira Ayuso d454a53f62 gtp-rtnl: remove debugging message that print netlink message 2014-03-20 10:33:18 +01:00
Pablo Neira Ayuso 65af4b0a0e gtp: use proper netdev_dbg dynamic debugging infrastructure
Add some instruction in the proper way that can be enabled on the
demand to debug problems. Let's make it quite verbose at this stage
to relax this later.

Make sure debugfs is mounted:

 # mount -t debugfs nodev /sys/kernel/debug

To enable:

  echo -n 'file gtp.c +p' > /sys/kernel/debug/dynamic_debug/control

To disable:

  echo -n 'file gtp.c -p' > /sys/kernel/debug/dynamic_debug/control

More information here on how to use this:
2014-03-19 15:43:20 +01:00
Pablo Neira Ayuso 3e71452952 gtp: implement gtp_genl_tunnel_get
This function allows you to check if there is a MS address or TEID
that already exists in the gtp forwarding base in a very fast way,
without having to dump the entire forwarding base and doing a search
from userspace.
2014-03-19 15:12:06 +01:00
Pablo Neira Ayuso 23ddc1f778 gtp: fix incorrect UDP header length field
With this fix, osmo-sgsn doesn't ignore our tunneled packet
anymore. It was complaining about incorrect packet length
for received packets.

And osmo-ggsn is working in my testbed :-).
2014-03-18 18:30:05 +01:00
Pablo Neira Ayuso 2cbf0a1483 gtp: fix wrong handling of option header fields in GTPv1
TS 29.060 expresses thing is a quite complicated way just to say
that if any of the optional fields is there, then all optional
fields become mandatory :-).

"This field shall be present if and only if any one or more of the S,
PN and E flags are set.".

So, if any flag is set, we *always* have 4 extra bytes.
2014-03-18 18:30:05 +01:00
Pablo Neira Ayuso 151652cc80 libgtpnl: tools: fix compilation of gtp-link-add
A typo slipped through into that file and it stopped compiling.
2014-03-18 18:30:05 +01:00
Pablo Neira Ayuso f427e3fc9d gtp: fix wrong stripping of UDP and GTP headers
We cannot strip the UDP header until we are sure that this is
a data packet, otherwise this passes a malformed packet to
userspace. The header stripping is now the very last operation
in the processing of GTP packets.
2014-03-18 18:30:01 +01:00
Pablo Neira Ayuso 4c86bcaab4 gtp: remove leftover workqueue
Not needed anymore after 5747406dbb.
2014-02-24 12:31:26 +01:00
Pablo Neira Ayuso 1783c7cda4 allow to specify the interface name for the GTP device
We can use this to specify 'gtp0' from openggsn, so we make sure that
it always uses the same tunnel device on creation. If it already exists,
it will return EEXIST. This is used to skip the problem of lacking
NLM_F_ECHO in the rtnetlink link interface that allows us to know the
name of the gtp device that has been dynamically allocated from the

And, finally, I don't find any use case for having more than one tunnel
device when integrating this with openggsn.

This patch also adjusts tools/gtp-link-add.c which needs some care, since
it is not yet using any of the library functions. This tools are likely
to be useful for troubleshooting and debugging.
2014-02-24 11:41:51 +01:00
Pablo Neira Ayuso 5747406dbb gtp: create socket from userspace and pass them as configuration
openggsn already sets up the UDP sockets that we need for the control
and user planes of GTP. Since we cannot bind two UDP sockets (one from
userspace and another from the kernel) to the same port, change the
current code to pass the socket descriptors that has been allocated
by openggsn (or whatever daemon which uses the GTP kernel

Two new attributes are added to set up the tunnel device: IFLA_GTP_FD0
(for GTP0) and IFLA_GTP_FD1 (for GTP1u), which specify the UDP socket
created from userspace. Thus, the GTP kernel code only takes care of
enabling the kernel UDP encapsulation routine.
2014-02-23 23:09:59 +01:00
Pablo Neira Ayuso 1afa16a16b gtp-rtnl: add gtp_dev_destroy
This function allows you to destroy the gtpX device.
2014-02-22 23:19:58 +01:00
Pablo Neira Ayuso b94023299f gtp-rtnl: refactor code to prepare the addition of gtp_dev_destroy
Add gtp_dev_talk() and gtp_put_nlmsg().
2014-02-22 23:09:38 +01:00
Pablo Neira Ayuso efdbb3bc73 gtp-rtnl: fix leak in struct mnl_socket on error in gtp_dev_create()
Release it in the error path.
2014-02-22 22:50:00 +01:00
Pablo Neira Ayuso e91a1a4ab7 tools: gtp-tunnel: fix accidentally swapped ms_addr and sgsn_addr
The output was swapping the ms and the sgsn addresses.
2014-02-22 22:38:05 +01:00
Pablo Neira Ayuso 4c2e76077a gtp-genl: fix compilation warning
Fix these compilation warning:

gtp-genl.c:131:8: warning: 'pdp.version' may be used uninitialized in this function [-Wmaybe-uninitialized]
gtp-genl.c:132:8: warning: 'pdp.tid' may be used uninitialized in this function [-Wmaybe-uninitialized]

By initializing the pdp object.
2014-02-22 22:30:23 +01:00
Pablo Neira Ayuso 2cf5c87b96 add struct gtp_tunnel and adapt functions to use it
This patch adds a gtp_tunnel structure to avoid having to deal with
functions with lots of parameters. This should also help to extend
the interfaces and the gtp_tunnel object without breaking the binary
interface (which will be good by when ipv6 support will be added).
2014-02-22 22:30:14 +01:00
Pablo Neira Ayuso 1b00243775 gtp: delay initialization if compiled built-in
Make sure the IPv4/IPv6 sockets are ready to be used before gtp needs
2014-02-21 16:18:22 +01:00
Pablo Neira Ayuso dd9a15af4d gtp: fix missing initialization of iph->id
This is required by a sane stack to reassemble fragments.
2014-02-21 13:03:18 +01:00
Pablo Neira Ayuso c7e6ffadb4 add libgtpnl
This patch adds the libgtpnl library. Harald mentioned that he wanted
that the specific code that is added is well encapsulated, so let's
start a small library to interact with the GTP kernel module via netlink

This was done a bit while in the rush, so the interfaces are not nice
at all and the tools need to be ported on top of this library.

This library will be used to integrate openggsn with the GTP kernel
2014-02-20 19:41:29 +01:00
Pablo Neira Ayuso a3ab375c9a gtp: get rid of tid variable
It's only used once.
2014-02-20 12:49:35 +01:00
Pablo Neira Ayuso 20d702b90e gtp: ensure sufficient room for the header in gtp0_udp_encap_recv 2014-02-20 12:49:35 +01:00
Pablo Neira Ayuso d123a0c0f5 gtp: fix handling of messages in the encap input path
* pass up to the stack if we don't know what to do.
* drop packet if not well-formed or not found in our PDP ctx database.
2014-02-20 12:48:40 +01:00
Pablo Neira Ayuso d7cdbddebd gtp: compile with Linux kernel 3.10.28
Add tweak to get this compiling with 3.10.28. Test shows that works
for me.
2014-02-20 02:34:40 +01:00
Pablo Neira Ayuso 4e93f63f6f gtp: don't care if GTP socket is not yet ready in gtp_dev_xmit()
If we receive any packet, it will be just ignored. I don't find
any reason to prematurely drop this in the xmit patch.
2014-02-20 01:14:07 +01:00
Pablo Neira Ayuso e29789755f gtp: kill unused fields in struct pdp_ctx
This is using the sgsn_addr to know where to send the GTP packets,
I guess that was the intention of those fields.
2014-02-20 01:12:03 +01:00
Pablo Neira Ayuso c63d5c5b86 gtp: remove dead code in gtp_link_setup()
I don't find a good reason why this can't be used with pktgen
(IFF_TX_SKB_SHARING) and the tx_queue is already zero if not
2014-02-20 01:04:23 +01:00
Pablo Neira Ayuso 972c2a20b0 gtp: use GTP_V0 and GTP_V1 whenever possible
Just a cleanup.
2014-02-20 00:24:24 +01:00
Pablo Neira Ayuso 1fa0cd93f8 gtp: rework handling of GTPv1 extensions
* Conditional check if we can pull the extensions (if any).
* Pull the GTPv1 header (8 bytes) and the extensions (if any).

Tested with emulated replayed pcap traffic, works for me.
2014-02-20 00:06:05 +01:00
Pablo Neira Ayuso 5df6cd2115 gtp: fix flags field for GTPv1
Now wireshark shows a well-formed GTPv1 message.
2014-02-19 23:16:04 +01:00
Pablo Neira Ayuso f33f89787b gtp: fix wrong version in header
v1 is 001 (as interpreted in binary), not 0001 (which is interpreted
as v0.
2014-02-19 16:23:20 +01:00