osmo-ggsn/ggsn/gtp-kernel.c

241 lines
4.9 KiB
C
Raw Normal View History

ggsn: add support for GTP kernel data encapsulation 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 10.0.0.0/8 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. Signed-off-by: Andreas Schultz <aschultz@tpip.net>
2015-11-17 11:22:42 +00:00
#ifdef __linux__
#define _GNU_SOURCE 1 /* strdup() prototype, broken arpa/inet.h */
#endif
#include "../config.h"
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include <syslog.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <libgtpnl/gtp.h>
#include <libgtpnl/gtpnl.h>
#include <libmnl/libmnl.h>
#include <errno.h>
#include <time.h>
#include "../lib/tun.h"
#include "../lib/syserr.h"
#include "../gtp/pdp.h"
#include "../gtp/gtp.h"
#include "cmdline.h"
#include <libgtpnl/gtp.h>
#include <libgtpnl/gtpnl.h>
#include <libmnl/libmnl.h>
#include "gtp-kernel.h"
static void pdp_debug(struct pdp_t *pdp)
{
int i;
uint64_t teid;
if (!debug)
return;
printf("version %u\n", pdp->version);
if (pdp->version == 0) {
teid = pdp_gettid(pdp->imsi, pdp->nsapi);
printf("flowid %u\n", pdp->flru);
} else {
teid = pdp->teid_gn; /* GTPIE_TEI_DI */
}
printf("teid %llx\n", teid);
printf("address (%u)\n", pdp->eua.l);
/* Byte 0: 0xf1 == IETF */
/* Byte 1: 0x21 == IPv4 */
/* Byte 2-6: IPv4 address */
for (i = 0; i < 6; i++)
printf("%x ", pdp->eua.v[i] & 0xff); /* GTPIE_EUA */
printf("\n");
printf("sgsn-addr (%u)\n", pdp->gsnrc.l);
for (i = 0; i < 4; i++)
printf("%x ", pdp->gsnrc.v[i] & 0xff); /* GTPIE_GSN_ADDR */
printf("\n");
}
static int mask2prefix(struct in_addr *mask)
{
uint32_t tmp = ntohl(mask->s_addr);
int k;
for (k=0; tmp > 0; k++)
tmp = (tmp << 1);
return k;
}
static struct {
int genl_id;
struct mnl_socket *nl;
bool enabled;
} gtp_nl;
/* Always forces the kernel to allocate gtp0. If it exists it hits EEXIST */
#define GTP_DEVNAME "gtp0"
int gtp_kernel_init(struct gsn_t *gsn, struct in_addr *net,
struct in_addr *mask,
struct gengetopt_args_info *args_info)
{
if (!args_info->gtpnl_given)
return 0;
if (gtp_dev_create(GTP_DEVNAME, args_info->gtpnl_orig,
gsn->fd0, gsn->fd1u) < 0) {
sys_err(LOG_ERR, __FILE__, __LINE__, 0,
"cannot create GTP tunnel device: %s\n",
strerror(errno));
return -1;
}
gtp_nl.enabled = true;
gtp_nl.nl = genl_socket_open();
if (gtp_nl.nl == NULL) {
sys_err(LOG_ERR, __FILE__, __LINE__, 0,
"cannot create genetlink socket\n");
return -1;
}
gtp_nl.genl_id = genl_lookup_family(gtp_nl.nl, "gtp");
if (gtp_nl.genl_id < 0) {
sys_err(LOG_ERR, __FILE__, __LINE__, 0,
"cannot lookup GTP genetlink ID\n");
return -1;
}
if (debug) {
sys_err(LOG_NOTICE, __FILE__, __LINE__, 0,
"Using the GTP kernel mode (genl ID is %d)\n",
gtp_nl.genl_id);
}
if (debug) {
printf("Setting route to reach %s via %s\n",
args_info->net_arg, GTP_DEVNAME);
}
if (gtp_dev_config(GTP_DEVNAME, net, mask2prefix(mask)) < 0) {
sys_err(LOG_ERR, __FILE__, __LINE__, 0,
"Cannot add route to reach network %s\n",
args_info->net_arg);
}
/* launch script if it is set to bring up the route to reach
* the MS, eg. ip ro add 10.0.0.0/8 dev gtp0. Better add this
* using native rtnetlink interface given that we know the
* MS network mask, later.
*/
if (ipup) {
char cmd[1024];
int err;
/* eg. /home/ggsn/ipup gtp0 10.0.0.0/8 */
snprintf(cmd, sizeof(cmd), "%s %s %s",
ipup, GTP_DEVNAME, args_info->net_arg);
cmd[sizeof(cmd)-1] = '\0';
err = system(cmd);
if (err < 0) {
sys_err(LOG_ERR, __FILE__, __LINE__, 0,
"Failed to launch script `%s'", ipup);
return -1;
}
}
sys_err(LOG_NOTICE, __FILE__, __LINE__, 0, "GTP kernel configured\n");
return 0;
}
void gtp_kernel_stop(void)
{
if (!gtp_nl.enabled)
return;
gtp_dev_destroy(GTP_DEVNAME);
}
int gtp_kernel_tunnel_add(struct pdp_t *pdp)
{
struct in_addr ms, sgsn;
struct gtp_tunnel *t;
int ret;
if (!gtp_nl.enabled)
return 0;
pdp_debug(pdp);
t = gtp_tunnel_alloc();
if (t == NULL)
return -1;
memcpy(&ms, &pdp->eua.v[2], sizeof(struct in_addr));
memcpy(&sgsn, &pdp->gsnrc.v[0], sizeof(struct in_addr));
gtp_tunnel_set_ifidx(t, if_nametoindex(GTP_DEVNAME));
gtp_tunnel_set_version(t, pdp->version);
gtp_tunnel_set_ms_ip4(t, &ms);
gtp_tunnel_set_sgsn_ip4(t, &sgsn);
if (pdp->version == 0) {
gtp_tunnel_set_tid(t, pdp_gettid(pdp->imsi, pdp->nsapi));
gtp_tunnel_set_flowid(t, pdp->flru);
} else {
gtp_tunnel_set_tid(t, pdp->teid_gn); /* GTPIE_TEI_DI */
}
ret = gtp_add_tunnel(gtp_nl.genl_id, gtp_nl.nl, t);
gtp_tunnel_free(t);
return ret;
}
int gtp_kernel_tunnel_del(struct pdp_t *pdp)
{
struct gtp_tunnel *t;
int ret;
if (!gtp_nl.enabled)
return 0;
pdp_debug(pdp);
t = gtp_tunnel_alloc();
if (t == NULL)
return -1;
gtp_tunnel_set_ifidx(t, if_nametoindex(GTP_DEVNAME));
gtp_tunnel_set_version(t, pdp->version);
if (pdp->version == 0) {
gtp_tunnel_set_tid(t, pdp_gettid(pdp->imsi, pdp->nsapi));
gtp_tunnel_set_flowid(t, pdp->flru);
} else {
gtp_tunnel_set_tid(t, pdp->teid_gn); /* GTPIE_TEI_DI */
}
ret = gtp_del_tunnel(gtp_nl.genl_id, gtp_nl.nl, t);
gtp_tunnel_free(t);
return ret;
}
int gtp_kernel_enabled(void)
{
return gtp_nl.enabled;
}