path: root/ggsn/ggsn.c
diff options
authorPablo Neira Ayuso <pablo@gnumonks.org>2014-03-24 18:06:53 +0100
committerPablo Neira Ayuso <pablo@gnumonks.org>2014-03-24 19:20:26 +0100
commit3f58223436f1b91591e9faff0dbc8b20ad2f2eb6 (patch)
tree136f658b089b7399974cc516ad3cccfaf376b2d1 /ggsn/ggsn.c
parent746b944ad6835c38e94a591bf5363b1797429337 (diff)
ggsn: add support for GTP kernel data encapsulationosmo-ggsn
This patch adds the -g, --gtpnl=device option that allows you to enable the GTP kernel tunneling mode in openggsn. You have to specify the real downlink device that will be used to tunnel traffic, eg. -g=eth0 This means that the gtp0 device will be created and it will use eth0 as the real device to encapsulate packet coming from the Internet that are addressed to the MS (so the tunnel devuce encapsulates these IP packets in GTP packets when traveling to the SGSN). Alternatively, you can also add this to the ggsn.conf configuration file: gtpnl eth0 The device has to be the real device that can route packets to the SGSN, if you select the wrong device, the kernel routing code may not find a way to reach the SSGN, you've been warned. Therefore, if this option is set, the operational becomes the following: 1) A gtp0 device is created via rtnetlink and configure the socket encapsulation infrastructure in the kernel. 2) Whenever a PDP context is created, this adds the necessary tunnel configuration via genetlink GTP interface. 3) Whenever a PDP context is destroyed, this deletes the tunnel via genetlink GTP interface. 4) Destroy the gtp0 device if ggsn is stopped, including all of the existing tunnels. You require the osmo-ggsn.git tree, which contains the kernel module gtp.ko and the libgtpnl library that you have to compile and install. Make sure you have loaded the gtp.ko kernel module before launching the ggsn daemon using the kernel driver mode, otherwise you will get a nice "operation not supported" error message ;-). This patch also adds supports for "ipup" configuration option to invoke an external script after the gtp0 device has been brought up. Typical command to add the route to reach the MS behind the GGSN is required, eg. ip route add dev gtp0. The (horrible) ggsn parser has been manually extended to support the new configuration option. That code doesn't look nice, but it just mimics what we already have there for consistency, please don't blame me for that. If you want to run in debugging mode, I suggest you to use: sudo ggsn -c ggsn.conf -f -d Note that you do have to run openggsn as root to bring up the gtp0 device. You have to see this message that announce that the GTP kernel mode is enabled. openggsn[1106]: ggsn.c: 656: Using the GTP kernel mode (genl ID is 25) This patch also automagically sets up route to reach MS from Internet just like tun mode does. This is fundamental to get this working, better don't leave to the admin, he may forget to add this route. In this patch, I tried to encapsulate this new feature as much as possible as Harald initially suggested. To compile this feature, you have to pass --enable-gtp-kernel, ie. ./configire --enable-gtp-kernel Otherwise, the code to interact with the gtp kernel part is not compiled.
Diffstat (limited to 'ggsn/ggsn.c')
1 files changed, 32 insertions, 3 deletions
diff --git a/ggsn/ggsn.c b/ggsn/ggsn.c
index 0022b1b..ddc77ee 100644
--- a/ggsn/ggsn.c
+++ b/ggsn/ggsn.c
@@ -53,6 +53,7 @@
#include "../gtp/pdp.h"
#include "../gtp/gtp.h"
#include "cmdline.h"
+#include "gtp-kernel.h"
int end = 0;
int maxfd = 0; /* For select() */
@@ -150,6 +151,13 @@ int delete_context(struct pdp_t *pdp)
ippool_freeip(ippool, (struct ippoolm_t *)pdp->peer);
sys_err(LOG_ERR, __FILE__, __LINE__, 0, "Peer not defined!");
+ if (gtp_kernel_tunnel_del(pdp)) {
+ sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+ "Cannot delete tunnel from kernel: %s\n",
+ strerror(errno));
+ }
return 0;
@@ -184,6 +192,11 @@ int create_context_ind(struct pdp_t *pdp)
pdp->ipif = tun; /* TODO */
member->peer = pdp;
+ if (gtp_kernel_tunnel_add(pdp) < 0) {
+ sys_err(LOG_ERR, __FILE__, __LINE__, 0,
+ "Cannot add tunnel to kernel: %s\n", strerror(errno));
+ }
gtp_create_context_resp(gsn, pdp, GTPCAUSE_ACC_REQ);
return 0; /* Success */
@@ -275,6 +288,8 @@ int main(int argc, char **argv)
printf("pidfile: %s\n", args_info.pidfile_arg);
if (args_info.statedir_arg)
printf("statedir: %s\n", args_info.statedir_arg);
+ if (args_info.gtpnl_arg)
+ printf("gtpnl: %s\n", args_info.gtpnl_arg);
printf("timelimit: %d\n", args_info.timelimit_arg);
@@ -318,6 +333,8 @@ int main(int argc, char **argv)
printf("pidfile: %s\n", args_info.pidfile_arg);
if (args_info.statedir_arg)
printf("statedir: %s\n", args_info.statedir_arg);
+ if (args_info.gtpnl_arg)
+ printf("gtpnl: %s\n", args_info.gtpnl_arg);
printf("timelimit: %d\n", args_info.timelimit_arg);
@@ -517,10 +534,18 @@ int main(int argc, char **argv)
if (gsn->fd1u > maxfd)
maxfd = gsn->fd1u;
+ /* use GTP kernel module for data packet encapsulation */
+ if (gtp_kernel_init(gsn, &net, &mask, &args_info) < 0)
+ goto err;
gtp_set_cb_data_ind(gsn, encaps_tun);
gtp_set_cb_delete_context(gsn, delete_context);
gtp_set_cb_create_context_ind(gsn, create_context_ind);
+ /* skip the configuration of the tun0 if we're using the gtp0 device */
+ if (gtp_kernel_enabled())
+ goto skip_tun;
/* Create a tunnel interface */
if (debug)
printf("Creating tun interface\n");
@@ -548,6 +573,8 @@ int main(int argc, char **argv)
if (ipup)
tun_runscript(tun, ipup);
/* Main select loop */
@@ -578,7 +605,7 @@ int main(int argc, char **argv)
- if (tun->fd != -1 && FD_ISSET(tun->fd, &fds) &&
+ if (tun && tun->fd != -1 && FD_ISSET(tun->fd, &fds) &&
tun_decaps(tun) < 0) {
sys_err(LOG_ERR, __FILE__, __LINE__, 0,
"TUN read failed (fd)=(%d)", tun->fd);
@@ -594,11 +621,13 @@ int main(int argc, char **argv)
+ gtp_kernel_stop();
- tun_free(tun);
+ if (tun)
+ tun_free(tun);
return 1;