blob: 513ff4a9630697ad6edef0ebd5acd1f98bef130b [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
Pablo Neira Ayuso517be892016-05-08 19:50:49 +020089 optidx++;
90
Andreas Schultz17c816f2016-04-11 16:10:03 +020091 if (gtp_version == GTP_V0)
92 gtp_tunnel_set_tid(t, atoi(argv[optidx++]));
93 else if (gtp_version == GTP_V1) {
94 gtp_tunnel_set_i_tei(t, atoi(argv[optidx++]));
95 gtp_tunnel_set_o_tei(t, atoi(argv[optidx++]));
96 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010097
Andreas Schultz17c816f2016-04-11 16:10:03 +020098 if (inet_aton(argv[optidx++], &ms) < 0) {
99 perror("bad address for ms");
100 exit(EXIT_FAILURE);
101 }
102 gtp_tunnel_set_ms_ip4(t, &ms);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100103
Andreas Schultz17c816f2016-04-11 16:10:03 +0200104 if (inet_aton(argv[optidx++], &sgsn) < 0) {
105 perror("bad address for sgsn");
106 exit(EXIT_FAILURE);
107 }
108 gtp_tunnel_set_sgsn_ip4(t, &sgsn);
109
110 gtp_add_tunnel(genl_id, nl, t);
111
112 gtp_tunnel_free(t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100113 return 0;
114}
115
116static int
117del_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl)
118{
Andreas Schultz17c816f2016-04-11 16:10:03 +0200119 struct gtp_tunnel *t;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100120 uint32_t gtp_ifidx;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100121
122 if (argc != 5) {
123 printf("%s add <gtp device> <version> <tid>\n",
124 argv[0]);
125 return EXIT_FAILURE;
126 }
127
Andreas Schultz17c816f2016-04-11 16:10:03 +0200128 t = gtp_tunnel_alloc();
129
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100130 gtp_ifidx = if_nametoindex(argv[2]);
Andreas Schultz17c816f2016-04-11 16:10:03 +0200131 if (gtp_ifidx == 0) {
132 fprintf(stderr, "wrong GTP interface %s\n", argv[2]);
133 return EXIT_FAILURE;
134 }
135 gtp_tunnel_set_ifidx(t, gtp_ifidx);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100136
Andreas Schultz17c816f2016-04-11 16:10:03 +0200137 if (strcmp(argv[3], "v0") == 0) {
138 gtp_tunnel_set_version(t, GTP_V0);
139 gtp_tunnel_set_tid(t, atoi(argv[4]));
140 } else if (strcmp(argv[3], "v1") == 0) {
141 gtp_tunnel_set_version(t, GTP_V1);
142 gtp_tunnel_set_i_tei(t, atoi(argv[4]));
143 } else {
144 fprintf(stderr, "wrong GTP version %s, use v0 or v1\n",
145 argv[3]);
146 return EXIT_FAILURE;
147 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100148
Andreas Schultz17c816f2016-04-11 16:10:03 +0200149 gtp_del_tunnel(genl_id, nl, t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100150
Andreas Schultz17c816f2016-04-11 16:10:03 +0200151 gtp_tunnel_free(t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100152 return 0;
153}
154
155struct gtp_pdp {
156 uint32_t version;
Andreas Schultz17c816f2016-04-11 16:10:03 +0200157 union {
158 struct {
159 uint64_t tid;
160 } v0;
161 struct {
162 uint32_t i_tei;
163 uint32_t o_tei;
164 } v1;
165 } u;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100166 struct in_addr sgsn_addr;
167 struct in_addr ms_addr;
168};
169
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100170static int
171list_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl)
172{
Pablo Neira Ayuso9438f722016-05-08 18:48:25 +0200173 return gtp_list_tunnel(genl_id, nl);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100174}
175
176int main(int argc, char *argv[])
177{
178 struct mnl_socket *nl;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100179 int32_t genl_id;
180 int ret;
181
182 if (argc < 2) {
183 printf("%s <add|delete|list> [<options,...>]\n", argv[0]);
184 exit(EXIT_FAILURE);
185 }
186
187 nl = genl_socket_open();
188 if (nl == NULL) {
189 perror("mnl_socket_open");
190 exit(EXIT_FAILURE);
191 }
192
193 genl_id = genl_lookup_family(nl, "gtp");
194 if (genl_id < 0) {
195 printf("not found gtp genl family\n");
196 exit(EXIT_FAILURE);
197 }
198
199 if (strncmp(argv[1], "add", strlen(argv[1])) == 0)
200 ret = add_tunnel(argc, argv, genl_id, nl);
201 else if (strncmp(argv[1], "delete", strlen(argv[1])) == 0)
202 ret = del_tunnel(argc, argv, genl_id, nl);
203 else if (strncmp(argv[1], "list", strlen(argv[1])) == 0)
204 ret = list_tunnel(argc, argv, genl_id, nl);
205 else {
206 printf("Unknown command `%s'\n", argv[1]);
207 exit(EXIT_FAILURE);
208 }
209
210 mnl_socket_close(nl);
211
212 return ret;
213}