gtp-rtnl: Split TID handling for GTPv0 and GTPv1

GTPv1 tunnel use separate 32bit Tunnel Endpoint Identifier's for each
direction while GTPv0 uses only one 64bit Tunnel IDentifier.

Signed-off-by: Andreas Schultz <aschultz@tpip.net>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
This commit is contained in:
Andreas Schultz 2016-04-11 16:10:03 +02:00 committed by Pablo Neira Ayuso
parent af422a432c
commit 17c816ff84
9 changed files with 171 additions and 88 deletions

View File

@ -14,6 +14,8 @@ void gtp_tunnel_set_ms_ip4(struct gtp_tunnel *t, struct in_addr *ms_addr);
void gtp_tunnel_set_sgsn_ip4(struct gtp_tunnel *t, struct in_addr *sgsn_addr);
void gtp_tunnel_set_version(struct gtp_tunnel *t, uint32_t version);
void gtp_tunnel_set_tid(struct gtp_tunnel *t, uint64_t tid);
void gtp_tunnel_set_i_tei(struct gtp_tunnel *t, uint32_t i_tei);
void gtp_tunnel_set_o_tei(struct gtp_tunnel *t, uint32_t o_tei);
void gtp_tunnel_set_flowid(struct gtp_tunnel *t, uint16_t flowid);
const int gtp_tunnel_get_ifns(struct gtp_tunnel *t);
@ -22,6 +24,8 @@ const struct in_addr *gtp_tunnel_get_ms_ip4(struct gtp_tunnel *t);
const struct in_addr *gtp_tunnel_get_sgsn_ip4(struct gtp_tunnel *t);
int gtp_tunnel_get_version(struct gtp_tunnel *t);
uint64_t gtp_tunnel_get_tid(struct gtp_tunnel *t);
uint32_t gtp_tunnel_get_i_tei(struct gtp_tunnel *t);
uint32_t gtp_tunnel_get_o_tei(struct gtp_tunnel *t);
uint16_t gtp_tunnel_get_flowid(struct gtp_tunnel *t);
#endif

View File

@ -41,6 +41,8 @@ enum gtp_attrs {
GTPA_MS_ADDRESS,
GTPA_FLOW, /* only for GTPv0 */
GTPA_NET_NS_FD,
GTPA_I_TEI,
GTPA_O_TEI,
__GTPA_MAX,
};
#define GTPA_MAX (__GTPA_MAX + 1)

View File

@ -49,8 +49,13 @@ static void gtp_build_payload(struct nlmsghdr *nlh, struct gtp_tunnel *t)
mnl_attr_put_u32(nlh, GTPA_LINK, t->ifidx);
mnl_attr_put_u32(nlh, GTPA_SGSN_ADDRESS, t->sgsn_addr.s_addr);
mnl_attr_put_u32(nlh, GTPA_MS_ADDRESS, t->ms_addr.s_addr);
mnl_attr_put_u64(nlh, GTPA_TID, t->tid);
mnl_attr_put_u16(nlh, GTPA_FLOW, t->flowid);
if (t->gtp_version == GTP_V0) {
mnl_attr_put_u64(nlh, GTPA_TID, t->u.v0.tid);
mnl_attr_put_u16(nlh, GTPA_FLOW, t->u.v0.flowid);
} else if (t->gtp_version == GTP_V1) {
mnl_attr_put_u32(nlh, GTPA_I_TEI, t->u.v1.i_tei);
mnl_attr_put_u32(nlh, GTPA_O_TEI, t->u.v1.o_tei);
}
}
int gtp_add_tunnel(int genl_id, struct mnl_socket *nl, struct gtp_tunnel *t)
@ -95,7 +100,15 @@ EXPORT_SYMBOL(gtp_del_tunnel);
struct gtp_pdp {
uint32_t version;
uint64_t tid;
union {
struct {
uint64_t tid;
} v0;
struct {
uint32_t i_tei;
uint32_t o_tei;
} v1;
} u;
struct in_addr sgsn_addr;
struct in_addr ms_addr;
};
@ -115,6 +128,8 @@ static int genl_gtp_validate_cb(const struct nlattr *attr, void *data)
return MNL_CB_ERROR;
}
break;
case GTPA_O_TEI:
case GTPA_I_TEI:
case GTPA_SGSN_ADDRESS:
case GTPA_MS_ADDRESS:
case GTPA_VERSION:
@ -138,7 +153,11 @@ static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data)
mnl_attr_parse(nlh, sizeof(*genl), genl_gtp_validate_cb, tb);
if (tb[GTPA_TID])
pdp.tid = mnl_attr_get_u64(tb[GTPA_TID]);
pdp.u.v0.tid = mnl_attr_get_u64(tb[GTPA_TID]);
if (tb[GTPA_I_TEI])
pdp.u.v1.i_tei = mnl_attr_get_u32(tb[GTPA_I_TEI]);
if (tb[GTPA_O_TEI])
pdp.u.v1.o_tei = mnl_attr_get_u32(tb[GTPA_O_TEI]);
if (tb[GTPA_SGSN_ADDRESS]) {
pdp.sgsn_addr.s_addr =
mnl_attr_get_u32(tb[GTPA_SGSN_ADDRESS]);
@ -151,7 +170,12 @@ static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data)
}
printf("version %u ", pdp.version);
printf("tid %"PRIu64" ms_addr %s ", pdp.tid, inet_ntoa(pdp.sgsn_addr));
if (pdp.version == GTP_V0)
printf("tid %"PRIu64" ms_addr %s ",
pdp.u.v0.tid, inet_ntoa(pdp.sgsn_addr));
else if (pdp.version == GTP_V1)
printf("tei %u/%u ms_addr %s ", pdp.u.v1.i_tei,
pdp.u.v1.o_tei, inet_ntoa(pdp.sgsn_addr));
printf("sgsn_addr %s\n", inet_ntoa(pdp.ms_addr));
return MNL_CB_OK;

View File

@ -71,16 +71,28 @@ EXPORT_SYMBOL(gtp_tunnel_set_version);
void gtp_tunnel_set_tid(struct gtp_tunnel *t, uint64_t tid)
{
t->tid = tid;
t->u.v0.tid = tid;
}
EXPORT_SYMBOL(gtp_tunnel_set_tid);
void gtp_tunnel_set_flowid(struct gtp_tunnel *t, uint16_t flowid)
{
t->flowid = flowid;
t->u.v0.flowid = flowid;
}
EXPORT_SYMBOL(gtp_tunnel_set_flowid);
void gtp_tunnel_set_i_tei(struct gtp_tunnel *t, uint32_t i_tei)
{
t->u.v1.i_tei = i_tei;
}
EXPORT_SYMBOL(gtp_tunnel_set_i_tei);
void gtp_tunnel_set_o_tei(struct gtp_tunnel *t, uint32_t o_tei)
{
t->u.v1.o_tei = o_tei;
}
EXPORT_SYMBOL(gtp_tunnel_set_o_tei);
const int gtp_tunnel_get_ifns(struct gtp_tunnel *t)
{
return t->ifns;
@ -113,12 +125,24 @@ EXPORT_SYMBOL(gtp_tunnel_get_version);
uint64_t gtp_tunnel_get_tid(struct gtp_tunnel *t)
{
return t->tid;
return t->u.v0.tid;
}
EXPORT_SYMBOL(gtp_tunnel_get_tid);
uint16_t gtp_tunnel_get_flowid(struct gtp_tunnel *t)
{
return t->flowid;
return t->u.v0.flowid;
}
EXPORT_SYMBOL(gtp_tunnel_get_flowid);
uint32_t gtp_tunnel_get_i_tei(struct gtp_tunnel *t)
{
return t->u.v1.i_tei;
}
EXPORT_SYMBOL(gtp_tunnel_get_i_tei);
uint32_t gtp_tunnel_get_o_tei(struct gtp_tunnel *t)
{
return t->u.v1.o_tei;
}
EXPORT_SYMBOL(gtp_tunnel_get_o_tei);

View File

@ -17,9 +17,17 @@ struct gtp_tunnel {
uint32_t ifidx;
struct in_addr ms_addr;
struct in_addr sgsn_addr;
uint64_t tid;
uint16_t flowid;
int gtp_version;
union {
struct {
uint64_t tid;
uint16_t flowid;
} v0;
struct {
uint32_t i_tei;
uint32_t o_tei;
} v1;
} u;
};
#endif

View File

@ -22,6 +22,8 @@ global:
gtp_tunnel_set_version;
gtp_tunnel_set_tid;
gtp_tunnel_set_flowid;
gtp_tunnel_set_i_tei;
gtp_tunnel_set_o_tei;
gtp_tunnel_get_ifns;
gtp_tunnel_get_ifidx;
gtp_tunnel_get_ms_ip4;
@ -29,6 +31,8 @@ global:
gtp_tunnel_get_version;
gtp_tunnel_get_tid;
gtp_tunnel_get_flowid;
gtp_tunnel_get_i_tei;
gtp_tunnel_get_o_tei;
local: *;
};

View File

@ -3,8 +3,8 @@ include $(top_srcdir)/Make_global.am
check_PROGRAMS = gtp-link-add \
gtp-tunnel
gtp_link_add_SOURCES = gtp-link-add.c gtpnl.c
gtp_link_add_SOURCES = gtp-link-add.c
gtp_link_add_LDADD = ../src/libgtpnl.la ${LIBMNL_LIBS}
gtp_tunnel_SOURCES = gtp-tunnel.c gtpnl.c
gtp_tunnel_SOURCES = gtp-tunnel.c
gtp_tunnel_LDADD = ../src/libgtpnl.la ${LIBMNL_LIBS}

View File

@ -28,71 +28,91 @@
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#include <inttypes.h>
#include <libmnl/libmnl.h>
#include <linux/genetlink.h>
#include <linux/gtp_nl.h>
#include <libgtpnl/gtp.h>
#include <libgtpnl/gtpnl.h>
static void add_usage(const char *name)
{
printf("%s add <gtp device> <v0> <tid> <ms-addr> <sgsn-addr>\n",
name);
printf("%s add <gtp device> <v1> <i_tei> <o_tei> <ms-addr> <sgsn-addr>\n",
name);
}
static int
add_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl)
{
struct gtp_tunnel *t;
uint32_t gtp_ifidx;
struct in_addr ms, sgsn;
struct nlmsghdr *nlh;
char buf[MNL_SOCKET_BUFFER_SIZE];
uint32_t seq = time(NULL), gtp_version;
uint32_t gtp_version;
int optidx;
if (argc != 7) {
printf("%s add <gtp device> <v0|v1> <tid> <ms-addr> <sgsn-addr>\n",
argv[0]);
if (argc < 7 || argc > 8) {
add_usage(argv[0]);
return EXIT_FAILURE;
}
gtp_ifidx = if_nametoindex(argv[2]);
t = gtp_tunnel_alloc();
optidx = 2;
gtp_ifidx = if_nametoindex(argv[optidx]);
if (gtp_ifidx == 0) {
fprintf(stderr, "wrong GTP interface %s\n", argv[2]);
fprintf(stderr, "wrong GTP interface %s\n", argv[optidx]);
return EXIT_FAILURE;
}
gtp_tunnel_set_ifidx(t, gtp_ifidx);
if (inet_aton(argv[5], &ms) < 0) {
perror("bad address for ms");
exit(EXIT_FAILURE);
}
optidx++;
if (inet_aton(argv[6], &sgsn) < 0) {
perror("bad address for sgsn");
exit(EXIT_FAILURE);
}
if (strcmp(argv[3], "v0") == 0)
if (strcmp(argv[optidx], "v0") == 0)
gtp_version = GTP_V0;
else if (strcmp(argv[3], "v1") == 0)
else if (strcmp(argv[optidx], "v1") == 0)
gtp_version = GTP_V1;
else {
fprintf(stderr, "wrong GTP version %s, use v0 or v1\n",
argv[3]);
argv[optidx]);
return EXIT_FAILURE;
}
gtp_tunnel_set_version(t, gtp_version);
nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_EXCL | NLM_F_ACK, ++seq,
GTP_CMD_TUNNEL_NEW);
gtp_build_payload(nlh, atoi(argv[4]), gtp_ifidx, sgsn.s_addr,
ms.s_addr, gtp_version);
if (gtp_version == GTP_V0)
gtp_tunnel_set_tid(t, atoi(argv[optidx++]));
else if (gtp_version == GTP_V1) {
gtp_tunnel_set_i_tei(t, atoi(argv[optidx++]));
gtp_tunnel_set_o_tei(t, atoi(argv[optidx++]));
}
if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0)
perror("genl_socket_talk");
if (inet_aton(argv[optidx++], &ms) < 0) {
perror("bad address for ms");
exit(EXIT_FAILURE);
}
gtp_tunnel_set_ms_ip4(t, &ms);
if (inet_aton(argv[optidx++], &sgsn) < 0) {
perror("bad address for sgsn");
exit(EXIT_FAILURE);
}
gtp_tunnel_set_sgsn_ip4(t, &sgsn);
gtp_add_tunnel(genl_id, nl, t);
gtp_tunnel_free(t);
return 0;
}
static int
del_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl)
{
struct gtp_tunnel *t;
uint32_t gtp_ifidx;
char buf[MNL_SOCKET_BUFFER_SIZE];
struct nlmsghdr *nlh;
uint32_t seq = time(NULL);
if (argc != 5) {
printf("%s add <gtp device> <version> <tid>\n",
@ -100,21 +120,44 @@ del_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl)
return EXIT_FAILURE;
}
t = gtp_tunnel_alloc();
gtp_ifidx = if_nametoindex(argv[2]);
if (gtp_ifidx == 0) {
fprintf(stderr, "wrong GTP interface %s\n", argv[2]);
return EXIT_FAILURE;
}
gtp_tunnel_set_ifidx(t, gtp_ifidx);
nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_ACK, ++seq,
GTP_CMD_TUNNEL_DELETE);
gtp_build_payload(nlh, atoi(argv[4]), gtp_ifidx, 0, 0, atoi(argv[3]));
if (strcmp(argv[3], "v0") == 0) {
gtp_tunnel_set_version(t, GTP_V0);
gtp_tunnel_set_tid(t, atoi(argv[4]));
} else if (strcmp(argv[3], "v1") == 0) {
gtp_tunnel_set_version(t, GTP_V1);
gtp_tunnel_set_i_tei(t, atoi(argv[4]));
} else {
fprintf(stderr, "wrong GTP version %s, use v0 or v1\n",
argv[3]);
return EXIT_FAILURE;
}
if (genl_socket_talk(nl, nlh, seq, NULL, NULL) < 0)
perror("genl_socket_talk");
gtp_del_tunnel(genl_id, nl, t);
gtp_tunnel_free(t);
return 0;
}
struct gtp_pdp {
uint32_t version;
uint64_t tid;
union {
struct {
uint64_t tid;
} v0;
struct {
uint32_t i_tei;
uint32_t o_tei;
} v1;
} u;
struct in_addr sgsn_addr;
struct in_addr ms_addr;
};
@ -134,6 +177,8 @@ static int genl_gtp_validate_cb(const struct nlattr *attr, void *data)
return MNL_CB_ERROR;
}
break;
case GTPA_I_TEI:
case GTPA_O_TEI:
case GTPA_SGSN_ADDRESS:
case GTPA_MS_ADDRESS:
case GTPA_VERSION:
@ -152,12 +197,16 @@ static int genl_gtp_validate_cb(const struct nlattr *attr, void *data)
static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data)
{
struct nlattr *tb[GTPA_MAX + 1] = {};
struct gtp_pdp pdp;
struct gtp_pdp pdp = {};
struct genlmsghdr *genl;
mnl_attr_parse(nlh, sizeof(*genl), genl_gtp_validate_cb, tb);
if (tb[GTPA_TID])
pdp.tid = mnl_attr_get_u64(tb[GTPA_TID]);
pdp.u.v0.tid = mnl_attr_get_u64(tb[GTPA_TID]);
if (tb[GTPA_I_TEI])
pdp.u.v1.i_tei = mnl_attr_get_u32(tb[GTPA_I_TEI]);
if (tb[GTPA_O_TEI])
pdp.u.v1.o_tei = mnl_attr_get_u32(tb[GTPA_O_TEI]);
if (tb[GTPA_SGSN_ADDRESS]) {
pdp.sgsn_addr.s_addr =
mnl_attr_get_u32(tb[GTPA_SGSN_ADDRESS]);
@ -170,7 +219,12 @@ static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data)
}
printf("version %u ", pdp.version);
printf("tid %llx ms_addr %s ", pdp.tid, inet_ntoa(pdp.ms_addr));
if (pdp.version == GTP_V0)
printf("tid %"PRIu64" ms_addr %s ",
pdp.u.v0.tid, inet_ntoa(pdp.sgsn_addr));
else if (pdp.version == GTP_V1)
printf("tei %u/%u ms_addr %s ", pdp.u.v1.i_tei,
pdp.u.v1.o_tei, inet_ntoa(pdp.sgsn_addr));
printf("sgsn_addr %s\n", inet_ntoa(pdp.sgsn_addr));
return MNL_CB_OK;
@ -197,8 +251,6 @@ list_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl)
int main(int argc, char *argv[])
{
struct mnl_socket *nl;
char buf[MNL_SOCKET_BUFFER_SIZE];
unsigned int portid;
int32_t genl_id;
int ret;

View File

@ -1,35 +0,0 @@
/* Helper functions */
/* (C) 2014 by sysmocom - s.f.m.c. GmbH
* Author: Pablo Neira Ayuso <pablo@gnumonks.org>
*
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdint.h>
#include <libmnl/libmnl.h>
#include <linux/gtp_nl.h>
void gtp_build_payload(struct nlmsghdr *nlh, uint64_t tid, uint32_t ifidx,
uint32_t sgsn_addr, uint32_t ms_addr, uint32_t version)
{
mnl_attr_put_u32(nlh, GTPA_VERSION, version);
mnl_attr_put_u32(nlh, GTPA_LINK, ifidx);
mnl_attr_put_u32(nlh, GTPA_SGSN_ADDRESS, sgsn_addr);
mnl_attr_put_u32(nlh, GTPA_MS_ADDRESS, ms_addr);
mnl_attr_put_u64(nlh, GTPA_TID, tid);
}