aboutsummaryrefslogtreecommitdiffstats
path: root/tools/gtp-link.c
blob: feb2efe1c44844d8bf627f302620b60669ac98e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Command line utility to create GTP link */

/* (C) 2014 by sysmocom - s.f.m.c. GmbH
 * (C) 2016 by Pablo Neira Ayuso <pablo@netfilter.org>
 *
 * 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <libmnl/libmnl.h>
#include <linux/if.h>
#include <linux/if_link.h>
#include <linux/rtnetlink.h>

#include <linux/gtp.h>
#include <linux/if_link.h>

#include <libgtpnl/gtpnl.h>

struct gtp_server_sock {
	int			family;
	int			fd1;
	int			fd2;
	socklen_t		len;
	struct {
		union {
			struct sockaddr_in	in;
			struct sockaddr_in6	in6;
		} fd1;
		union {
			struct sockaddr_in	in;
			struct sockaddr_in6	in6;
		} fd2;
	} sockaddr;
};

static void setup_sockaddr_in(struct sockaddr_in *sockaddr, uint16_t port)
{
	sockaddr->sin_family = AF_INET;
	sockaddr->sin_port = htons(port);
	sockaddr->sin_addr.s_addr = INADDR_ANY;
}

static void setup_sockaddr_in6(struct sockaddr_in6 *sockaddr, uint16_t port)
{
	sockaddr->sin6_family = AF_INET6;
	sockaddr->sin6_port = htons(port);
	sockaddr->sin6_addr = in6addr_any;
}

static int setup_socket(struct gtp_server_sock *gtp_sock, int family)
{
	int fd1 = socket(family, SOCK_DGRAM, 0);
	int fd2 = socket(family, SOCK_DGRAM, 0);

	if (fd1 < 0 || fd2 < 0)
		return -1;

	memset(gtp_sock, 0, sizeof(*gtp_sock));

	gtp_sock->family = family;
	gtp_sock->fd1 = fd1;
	gtp_sock->fd2 = fd2;

	switch (family) {
	case AF_INET:
		gtp_sock->len = sizeof(struct sockaddr_in);
		setup_sockaddr_in(&gtp_sock->sockaddr.fd1.in, 3386);
		setup_sockaddr_in(&gtp_sock->sockaddr.fd2.in, 2152);
		break;
	case AF_INET6:
		gtp_sock->len = sizeof(struct sockaddr_in6);
		setup_sockaddr_in6(&gtp_sock->sockaddr.fd1.in6, 3386);
		setup_sockaddr_in6(&gtp_sock->sockaddr.fd2.in6, 2152);
		break;
	}

	return 0;
}

int main(int argc, char *argv[])
{
	char buf[MNL_SOCKET_BUFFER_SIZE];
	struct gtp_server_sock gtp_sock;
	int ret, sgsn_mode = 0, family;

	if (argc < 3) {
		printf("Usage: %s add <device> <family> [--sgsn]\n", argv[0]);
		printf("       %s del <device>\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	if (!strcmp(argv[1], "del")) {
		printf("destroying gtp interface...\n");
		if (gtp_dev_destroy(argv[2]) < 0)
			perror("gtp_dev_destroy");

		return 0;
	} else if (!strcmp(argv[1], "add")) {
		if (argc < 4) {
			printf("Usage: %s add <device> <family> [--sgsn]\n", argv[0]);
			exit(EXIT_FAILURE);
		}

		if (argc == 5 && !strcmp(argv[4], "--sgsn"))
			sgsn_mode = 1;
	}

	if (!strcmp(argv[3], "ip"))
		family = AF_INET;
	else if (!strcmp(argv[3], "ip6"))
		family = AF_INET6;
	else {
		fprintf(stderr, "unsupport family `%s', expecting `ip' or `ip6'\n",
			argv[3]);
		exit(EXIT_FAILURE);
	}

	if (setup_socket(&gtp_sock, family) < 0) {
		perror("socket");
		exit(EXIT_FAILURE);
	}

	if (bind(gtp_sock.fd1, (struct sockaddr *) &gtp_sock.sockaddr.fd1, gtp_sock.len) < 0) {
		perror("bind");
		exit(EXIT_FAILURE);
	}
	if (bind(gtp_sock.fd2, (struct sockaddr *) &gtp_sock.sockaddr.fd2, gtp_sock.len) < 0) {
		perror("bind");
		exit(EXIT_FAILURE);
	}

	if (sgsn_mode)
		ret = gtp_dev_create_sgsn(-1, argv[2], gtp_sock.fd1, gtp_sock.fd2);
	else
		ret = gtp_dev_create(-1, argv[2], gtp_sock.fd1, gtp_sock.fd2);
	if (ret < 0) {
		perror("cannot create GTP device\n");
		exit(EXIT_FAILURE);
	}

	fprintf(stderr, "WARNING: attaching dummy socket descriptors. Keep "
			"this process running for testing purposes.\n");

	while (1) {
		union {
			struct sockaddr_in addr;
			struct sockaddr_in6 addr6;
		} sock;

		ret = recvfrom(gtp_sock.fd1, buf, sizeof(buf), 0,
			       (struct sockaddr *)&sock, &gtp_sock.len);
		printf("received %d bytes via UDP socket\n", ret);
	}

	return 0;
}