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>
diff --git a/tools/gtp-tunnel.c b/tools/gtp-tunnel.c
index 9c52a27..bb12c3f 100644
--- a/tools/gtp-tunnel.c
+++ b/tools/gtp-tunnel.c
@@ -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 @@
 		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 @@
 			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_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 @@
 	}
 
 	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 @@
 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;