blob: 709b4f963cc93d355cb82446f781302d06dab751 [file] [log] [blame]
Harald Welte51b00a62014-04-03 09:37:38 -04001/* Command line utility to create GTP tunnels (PDP contexts) */
2
3/* (C) 2014 by sysmocom - s.f.m.c. GmbH
Pablo Neira Ayuso9438f722016-05-08 18:48:25 +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 <string.h>
28#include <unistd.h>
29#include <time.h>
30#include <arpa/inet.h>
31#include <sys/socket.h>
32#include <netinet/in.h>
Andreas Schultz17c816f2016-04-11 16:10:03 +020033#include <net/if.h>
34#include <inttypes.h>
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010035
36#include <libmnl/libmnl.h>
37#include <linux/genetlink.h>
38
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +020039#include <linux/gtp.h>
40#include <linux/if_link.h>
Andreas Schultz17c816f2016-04-11 16:10:03 +020041#include <libgtpnl/gtp.h>
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010042#include <libgtpnl/gtpnl.h>
43
Andreas Schultz17c816f2016-04-11 16:10:03 +020044static void add_usage(const char *name)
45{
46 printf("%s add <gtp device> <v0> <tid> <ms-addr> <sgsn-addr>\n",
47 name);
48 printf("%s add <gtp device> <v1> <i_tei> <o_tei> <ms-addr> <sgsn-addr>\n",
49 name);
50}
51
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010052static int
53add_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl)
54{
Andreas Schultz17c816f2016-04-11 16:10:03 +020055 struct gtp_tunnel *t;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010056 uint32_t gtp_ifidx;
57 struct in_addr ms, sgsn;
Andreas Schultz17c816f2016-04-11 16:10:03 +020058 uint32_t gtp_version;
59 int optidx;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010060
Andreas Schultz17c816f2016-04-11 16:10:03 +020061 if (argc < 7 || argc > 8) {
62 add_usage(argv[0]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010063 return EXIT_FAILURE;
64 }
Andreas Schultz17c816f2016-04-11 16:10:03 +020065
66 t = gtp_tunnel_alloc();
67 optidx = 2;
68
69 gtp_ifidx = if_nametoindex(argv[optidx]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010070 if (gtp_ifidx == 0) {
Andreas Schultz17c816f2016-04-11 16:10:03 +020071 fprintf(stderr, "wrong GTP interface %s\n", argv[optidx]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010072 return EXIT_FAILURE;
73 }
Andreas Schultz17c816f2016-04-11 16:10:03 +020074 gtp_tunnel_set_ifidx(t, gtp_ifidx);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010075
Andreas Schultz17c816f2016-04-11 16:10:03 +020076 optidx++;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010077
Andreas Schultz17c816f2016-04-11 16:10:03 +020078 if (strcmp(argv[optidx], "v0") == 0)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010079 gtp_version = GTP_V0;
Andreas Schultz17c816f2016-04-11 16:10:03 +020080 else if (strcmp(argv[optidx], "v1") == 0)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010081 gtp_version = GTP_V1;
82 else {
83 fprintf(stderr, "wrong GTP version %s, use v0 or v1\n",
Andreas Schultz17c816f2016-04-11 16:10:03 +020084 argv[optidx]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010085 return EXIT_FAILURE;
86 }
Andreas Schultz17c816f2016-04-11 16:10:03 +020087 gtp_tunnel_set_version(t, gtp_version);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010088
Andreas Schultz17c816f2016-04-11 16:10:03 +020089 if (gtp_version == GTP_V0)
90 gtp_tunnel_set_tid(t, atoi(argv[optidx++]));
91 else if (gtp_version == GTP_V1) {
92 gtp_tunnel_set_i_tei(t, atoi(argv[optidx++]));
93 gtp_tunnel_set_o_tei(t, atoi(argv[optidx++]));
94 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010095
Andreas Schultz17c816f2016-04-11 16:10:03 +020096 if (inet_aton(argv[optidx++], &ms) < 0) {
97 perror("bad address for ms");
98 exit(EXIT_FAILURE);
99 }
100 gtp_tunnel_set_ms_ip4(t, &ms);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100101
Andreas Schultz17c816f2016-04-11 16:10:03 +0200102 if (inet_aton(argv[optidx++], &sgsn) < 0) {
103 perror("bad address for sgsn");
104 exit(EXIT_FAILURE);
105 }
106 gtp_tunnel_set_sgsn_ip4(t, &sgsn);
107
108 gtp_add_tunnel(genl_id, nl, t);
109
110 gtp_tunnel_free(t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100111 return 0;
112}
113
114static int
115del_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl)
116{
Andreas Schultz17c816f2016-04-11 16:10:03 +0200117 struct gtp_tunnel *t;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100118 uint32_t gtp_ifidx;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100119
120 if (argc != 5) {
121 printf("%s add <gtp device> <version> <tid>\n",
122 argv[0]);
123 return EXIT_FAILURE;
124 }
125
Andreas Schultz17c816f2016-04-11 16:10:03 +0200126 t = gtp_tunnel_alloc();
127
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100128 gtp_ifidx = if_nametoindex(argv[2]);
Andreas Schultz17c816f2016-04-11 16:10:03 +0200129 if (gtp_ifidx == 0) {
130 fprintf(stderr, "wrong GTP interface %s\n", argv[2]);
131 return EXIT_FAILURE;
132 }
133 gtp_tunnel_set_ifidx(t, gtp_ifidx);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100134
Andreas Schultz17c816f2016-04-11 16:10:03 +0200135 if (strcmp(argv[3], "v0") == 0) {
136 gtp_tunnel_set_version(t, GTP_V0);
137 gtp_tunnel_set_tid(t, atoi(argv[4]));
138 } else if (strcmp(argv[3], "v1") == 0) {
139 gtp_tunnel_set_version(t, GTP_V1);
140 gtp_tunnel_set_i_tei(t, atoi(argv[4]));
141 } else {
142 fprintf(stderr, "wrong GTP version %s, use v0 or v1\n",
143 argv[3]);
144 return EXIT_FAILURE;
145 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100146
Andreas Schultz17c816f2016-04-11 16:10:03 +0200147 gtp_del_tunnel(genl_id, nl, t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100148
Andreas Schultz17c816f2016-04-11 16:10:03 +0200149 gtp_tunnel_free(t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100150 return 0;
151}
152
153struct gtp_pdp {
154 uint32_t version;
Andreas Schultz17c816f2016-04-11 16:10:03 +0200155 union {
156 struct {
157 uint64_t tid;
158 } v0;
159 struct {
160 uint32_t i_tei;
161 uint32_t o_tei;
162 } v1;
163 } u;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100164 struct in_addr sgsn_addr;
165 struct in_addr ms_addr;
166};
167
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100168static int
169list_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl)
170{
Pablo Neira Ayuso9438f722016-05-08 18:48:25 +0200171 return gtp_list_tunnel(genl_id, nl);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100172}
173
174int main(int argc, char *argv[])
175{
176 struct mnl_socket *nl;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100177 int32_t genl_id;
178 int ret;
179
180 if (argc < 2) {
181 printf("%s <add|delete|list> [<options,...>]\n", argv[0]);
182 exit(EXIT_FAILURE);
183 }
184
185 nl = genl_socket_open();
186 if (nl == NULL) {
187 perror("mnl_socket_open");
188 exit(EXIT_FAILURE);
189 }
190
191 genl_id = genl_lookup_family(nl, "gtp");
192 if (genl_id < 0) {
193 printf("not found gtp genl family\n");
194 exit(EXIT_FAILURE);
195 }
196
197 if (strncmp(argv[1], "add", strlen(argv[1])) == 0)
198 ret = add_tunnel(argc, argv, genl_id, nl);
199 else if (strncmp(argv[1], "delete", strlen(argv[1])) == 0)
200 ret = del_tunnel(argc, argv, genl_id, nl);
201 else if (strncmp(argv[1], "list", strlen(argv[1])) == 0)
202 ret = list_tunnel(argc, argv, genl_id, nl);
203 else {
204 printf("Unknown command `%s'\n", argv[1]);
205 exit(EXIT_FAILURE);
206 }
207
208 mnl_socket_close(nl);
209
210 return ret;
211}