blob: 69794590fc66dd88f9ba7bb451e93d5c7f81b44d [file] [log] [blame]
Harald Welte51b00a62014-04-03 09:37:38 -04001/* Command line utility to create GTP link */
2
3/* (C) 2014 by sysmocom - s.f.m.c. GmbH
Pablo Neira Ayuso448bce42016-05-08 21:43:40 +02004 * (C) 2016 by Pablo Neira Ayuso <pablo@netfilter.org>
5 *
Harald Welte51b00a62014-04-03 09:37:38 -04006 * Author: Pablo Neira Ayuso <pablo@gnumonks.org>
7 *
8 * All Rights Reserved
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010025#include <stdio.h>
26#include <stdlib.h>
27#include <unistd.h>
28#include <string.h>
29#include <time.h>
Pablo Neira Ayuso50826a52016-05-10 18:00:51 +020030#include <netinet/in.h>
31#include <arpa/inet.h>
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010032
33#include <libmnl/libmnl.h>
34#include <linux/if.h>
35#include <linux/if_link.h>
36#include <linux/rtnetlink.h>
37
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +020038#include <linux/gtp.h>
39#include <linux/if_link.h>
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010040
Pablo Neira Ayuso448bce42016-05-08 21:43:40 +020041#include <libgtpnl/gtpnl.h>
42
Pablo Neira Ayuso38b5e232023-11-03 17:58:43 +010043struct gtp_server_sock {
44 int family;
45 int fd1;
46 int fd2;
47 socklen_t len;
48 struct {
49 union {
50 struct sockaddr_in in;
51 struct sockaddr_in6 in6;
52 } fd1;
53 union {
54 struct sockaddr_in in;
55 struct sockaddr_in6 in6;
56 } fd2;
57 } sockaddr;
58};
59
60static void setup_sockaddr_in(struct sockaddr_in *sockaddr, uint16_t port)
61{
62 sockaddr->sin_family = AF_INET;
63 sockaddr->sin_port = htons(port);
64 sockaddr->sin_addr.s_addr = INADDR_ANY;
65}
66
67static void setup_sockaddr_in6(struct sockaddr_in6 *sockaddr, uint16_t port)
68{
69 sockaddr->sin6_family = AF_INET6;
70 sockaddr->sin6_port = htons(port);
71 sockaddr->sin6_addr = in6addr_any;
72}
73
74static int setup_socket(struct gtp_server_sock *gtp_sock, int family)
75{
76 int fd1 = socket(family, SOCK_DGRAM, 0);
77 int fd2 = socket(family, SOCK_DGRAM, 0);
78
79 if (fd1 < 0 || fd2 < 0)
80 return -1;
81
82 memset(gtp_sock, 0, sizeof(*gtp_sock));
83
84 gtp_sock->family = family;
85 gtp_sock->fd1 = fd1;
86 gtp_sock->fd2 = fd2;
87
88 switch (family) {
89 case AF_INET:
90 gtp_sock->len = sizeof(struct sockaddr_in);
91 setup_sockaddr_in(&gtp_sock->sockaddr.fd1.in, 3386);
92 setup_sockaddr_in(&gtp_sock->sockaddr.fd2.in, 2152);
93 break;
94 case AF_INET6:
95 gtp_sock->len = sizeof(struct sockaddr_in6);
96 setup_sockaddr_in6(&gtp_sock->sockaddr.fd1.in6, 3386);
97 setup_sockaddr_in6(&gtp_sock->sockaddr.fd2.in6, 2152);
Pablo Neira Ayuso0e7a6352024-01-31 18:17:51 +010098 if (setsockopt(fd1, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one)) < 0)
99 perror("setsockopt IPV6_V6ONLY: ");
100 if (setsockopt(fd2, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one)) < 0)
101 perror("setsockopt IPV6_V6ONLY: ");
Pablo Neira Ayuso38b5e232023-11-03 17:58:43 +0100102 break;
103 }
104
105 return 0;
106}
107
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100108int main(int argc, char *argv[])
109{
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100110 char buf[MNL_SOCKET_BUFFER_SIZE];
Pablo Neira Ayuso38b5e232023-11-03 17:58:43 +0100111 struct gtp_server_sock gtp_sock;
112 int ret, sgsn_mode = 0, family;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100113
Harald Welte3bf55c32017-03-15 18:03:42 +0100114 if (argc < 3) {
Pablo Neira Ayuso38b5e232023-11-03 17:58:43 +0100115 printf("Usage: %s add <device> <family> [--sgsn]\n", argv[0]);
Oliver Smith6954da72022-09-07 15:48:16 +0200116 printf(" %s del <device>\n", argv[0]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100117 exit(EXIT_FAILURE);
118 }
119
Pablo Neira Ayuso448bce42016-05-08 21:43:40 +0200120 if (!strcmp(argv[1], "del")) {
121 printf("destroying gtp interface...\n");
122 if (gtp_dev_destroy(argv[2]) < 0)
123 perror("gtp_dev_destroy");
124
125 return 0;
Pablo Neira Ayuso38b5e232023-11-03 17:58:43 +0100126 } else if (!strcmp(argv[1], "add")) {
127 if (argc < 4) {
128 printf("Usage: %s add <device> <family> [--sgsn]\n", argv[0]);
129 exit(EXIT_FAILURE);
130 }
131
132 if (argc == 5 && !strcmp(argv[4], "--sgsn"))
133 sgsn_mode = 1;
Pablo Neira Ayuso448bce42016-05-08 21:43:40 +0200134 }
135
Pablo Neira Ayuso38b5e232023-11-03 17:58:43 +0100136 if (!strcmp(argv[3], "ip"))
137 family = AF_INET;
138 else if (!strcmp(argv[3], "ip6"))
139 family = AF_INET6;
140 else {
141 fprintf(stderr, "unsupport family `%s', expecting `ip' or `ip6'\n",
142 argv[3]);
143 exit(EXIT_FAILURE);
144 }
Harald Welte3bf55c32017-03-15 18:03:42 +0100145
Pablo Neira Ayuso38b5e232023-11-03 17:58:43 +0100146 if (setup_socket(&gtp_sock, family) < 0) {
147 perror("socket");
148 exit(EXIT_FAILURE);
149 }
Pablo Neira Ayuso50826a52016-05-10 18:00:51 +0200150
Pablo Neira Ayuso38b5e232023-11-03 17:58:43 +0100151 if (bind(gtp_sock.fd1, (struct sockaddr *) &gtp_sock.sockaddr.fd1, gtp_sock.len) < 0) {
Pablo Neira Ayuso50826a52016-05-10 18:00:51 +0200152 perror("bind");
153 exit(EXIT_FAILURE);
154 }
Pablo Neira Ayuso38b5e232023-11-03 17:58:43 +0100155 if (bind(gtp_sock.fd2, (struct sockaddr *) &gtp_sock.sockaddr.fd2, gtp_sock.len) < 0) {
Pablo Neira Ayuso50826a52016-05-10 18:00:51 +0200156 perror("bind");
157 exit(EXIT_FAILURE);
158 }
Pablo Neira Ayuso7aa20872014-02-24 11:38:52 +0100159
Harald Welte3bf55c32017-03-15 18:03:42 +0100160 if (sgsn_mode)
Pablo Neira Ayuso38b5e232023-11-03 17:58:43 +0100161 ret = gtp_dev_create_sgsn(-1, argv[2], gtp_sock.fd1, gtp_sock.fd2);
Harald Welte3bf55c32017-03-15 18:03:42 +0100162 else
Pablo Neira Ayuso38b5e232023-11-03 17:58:43 +0100163 ret = gtp_dev_create(-1, argv[2], gtp_sock.fd1, gtp_sock.fd2);
Harald Weltea7a4df32017-03-15 20:53:08 +0100164 if (ret < 0) {
165 perror("cannot create GTP device\n");
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100166 exit(EXIT_FAILURE);
167 }
168
Pablo Neira Ayuso448bce42016-05-08 21:43:40 +0200169 fprintf(stderr, "WARNING: attaching dummy socket descriptors. Keep "
170 "this process running for testing purposes.\n");
Pablo Neira Ayuso50826a52016-05-10 18:00:51 +0200171
172 while (1) {
Pablo Neira Ayuso38b5e232023-11-03 17:58:43 +0100173 union {
174 struct sockaddr_in addr;
175 struct sockaddr_in6 addr6;
176 } sock;
Pablo Neira Ayuso50826a52016-05-10 18:00:51 +0200177
Pablo Neira Ayuso38b5e232023-11-03 17:58:43 +0100178 ret = recvfrom(gtp_sock.fd1, buf, sizeof(buf), 0,
179 (struct sockaddr *)&sock, &gtp_sock.len);
Pablo Neira Ayuso50826a52016-05-10 18:00:51 +0200180 printf("received %d bytes via UDP socket\n", ret);
181 }
Pablo Neira Ayuso448bce42016-05-08 21:43:40 +0200182
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100183 return 0;
184}