blob: bb12c3f802297281f13a76d903df56e4252927d1 [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
4 * Author: Pablo Neira Ayuso <pablo@gnumonks.org>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010023#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <unistd.h>
27#include <time.h>
28#include <arpa/inet.h>
29#include <sys/socket.h>
30#include <netinet/in.h>
Andreas Schultz17c816f2016-04-11 16:10:03 +020031#include <net/if.h>
32#include <inttypes.h>
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010033
34#include <libmnl/libmnl.h>
35#include <linux/genetlink.h>
36
37#include <linux/gtp_nl.h>
Andreas Schultz17c816f2016-04-11 16:10:03 +020038#include <libgtpnl/gtp.h>
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010039#include <libgtpnl/gtpnl.h>
40
Andreas Schultz17c816f2016-04-11 16:10:03 +020041static void add_usage(const char *name)
42{
43 printf("%s add <gtp device> <v0> <tid> <ms-addr> <sgsn-addr>\n",
44 name);
45 printf("%s add <gtp device> <v1> <i_tei> <o_tei> <ms-addr> <sgsn-addr>\n",
46 name);
47}
48
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010049static int
50add_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl)
51{
Andreas Schultz17c816f2016-04-11 16:10:03 +020052 struct gtp_tunnel *t;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010053 uint32_t gtp_ifidx;
54 struct in_addr ms, sgsn;
Andreas Schultz17c816f2016-04-11 16:10:03 +020055 uint32_t gtp_version;
56 int optidx;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010057
Andreas Schultz17c816f2016-04-11 16:10:03 +020058 if (argc < 7 || argc > 8) {
59 add_usage(argv[0]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010060 return EXIT_FAILURE;
61 }
Andreas Schultz17c816f2016-04-11 16:10:03 +020062
63 t = gtp_tunnel_alloc();
64 optidx = 2;
65
66 gtp_ifidx = if_nametoindex(argv[optidx]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010067 if (gtp_ifidx == 0) {
Andreas Schultz17c816f2016-04-11 16:10:03 +020068 fprintf(stderr, "wrong GTP interface %s\n", argv[optidx]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010069 return EXIT_FAILURE;
70 }
Andreas Schultz17c816f2016-04-11 16:10:03 +020071 gtp_tunnel_set_ifidx(t, gtp_ifidx);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010072
Andreas Schultz17c816f2016-04-11 16:10:03 +020073 optidx++;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010074
Andreas Schultz17c816f2016-04-11 16:10:03 +020075 if (strcmp(argv[optidx], "v0") == 0)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010076 gtp_version = GTP_V0;
Andreas Schultz17c816f2016-04-11 16:10:03 +020077 else if (strcmp(argv[optidx], "v1") == 0)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010078 gtp_version = GTP_V1;
79 else {
80 fprintf(stderr, "wrong GTP version %s, use v0 or v1\n",
Andreas Schultz17c816f2016-04-11 16:10:03 +020081 argv[optidx]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010082 return EXIT_FAILURE;
83 }
Andreas Schultz17c816f2016-04-11 16:10:03 +020084 gtp_tunnel_set_version(t, gtp_version);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010085
Andreas Schultz17c816f2016-04-11 16:10:03 +020086 if (gtp_version == GTP_V0)
87 gtp_tunnel_set_tid(t, atoi(argv[optidx++]));
88 else if (gtp_version == GTP_V1) {
89 gtp_tunnel_set_i_tei(t, atoi(argv[optidx++]));
90 gtp_tunnel_set_o_tei(t, atoi(argv[optidx++]));
91 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010092
Andreas Schultz17c816f2016-04-11 16:10:03 +020093 if (inet_aton(argv[optidx++], &ms) < 0) {
94 perror("bad address for ms");
95 exit(EXIT_FAILURE);
96 }
97 gtp_tunnel_set_ms_ip4(t, &ms);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010098
Andreas Schultz17c816f2016-04-11 16:10:03 +020099 if (inet_aton(argv[optidx++], &sgsn) < 0) {
100 perror("bad address for sgsn");
101 exit(EXIT_FAILURE);
102 }
103 gtp_tunnel_set_sgsn_ip4(t, &sgsn);
104
105 gtp_add_tunnel(genl_id, nl, t);
106
107 gtp_tunnel_free(t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100108 return 0;
109}
110
111static int
112del_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl)
113{
Andreas Schultz17c816f2016-04-11 16:10:03 +0200114 struct gtp_tunnel *t;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100115 uint32_t gtp_ifidx;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100116
117 if (argc != 5) {
118 printf("%s add <gtp device> <version> <tid>\n",
119 argv[0]);
120 return EXIT_FAILURE;
121 }
122
Andreas Schultz17c816f2016-04-11 16:10:03 +0200123 t = gtp_tunnel_alloc();
124
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100125 gtp_ifidx = if_nametoindex(argv[2]);
Andreas Schultz17c816f2016-04-11 16:10:03 +0200126 if (gtp_ifidx == 0) {
127 fprintf(stderr, "wrong GTP interface %s\n", argv[2]);
128 return EXIT_FAILURE;
129 }
130 gtp_tunnel_set_ifidx(t, gtp_ifidx);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100131
Andreas Schultz17c816f2016-04-11 16:10:03 +0200132 if (strcmp(argv[3], "v0") == 0) {
133 gtp_tunnel_set_version(t, GTP_V0);
134 gtp_tunnel_set_tid(t, atoi(argv[4]));
135 } else if (strcmp(argv[3], "v1") == 0) {
136 gtp_tunnel_set_version(t, GTP_V1);
137 gtp_tunnel_set_i_tei(t, atoi(argv[4]));
138 } else {
139 fprintf(stderr, "wrong GTP version %s, use v0 or v1\n",
140 argv[3]);
141 return EXIT_FAILURE;
142 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100143
Andreas Schultz17c816f2016-04-11 16:10:03 +0200144 gtp_del_tunnel(genl_id, nl, t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100145
Andreas Schultz17c816f2016-04-11 16:10:03 +0200146 gtp_tunnel_free(t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100147 return 0;
148}
149
150struct gtp_pdp {
151 uint32_t version;
Andreas Schultz17c816f2016-04-11 16:10:03 +0200152 union {
153 struct {
154 uint64_t tid;
155 } v0;
156 struct {
157 uint32_t i_tei;
158 uint32_t o_tei;
159 } v1;
160 } u;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100161 struct in_addr sgsn_addr;
162 struct in_addr ms_addr;
163};
164
165static int genl_gtp_validate_cb(const struct nlattr *attr, void *data)
166{
167 const struct nlattr **tb = data;
168 int type = mnl_attr_get_type(attr);
169
170 if (mnl_attr_type_valid(attr, CTRL_ATTR_MAX) < 0)
171 return MNL_CB_OK;
172
173 switch(type) {
174 case GTPA_TID:
175 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0) {
176 perror("mnl_attr_validate");
177 return MNL_CB_ERROR;
178 }
179 break;
Andreas Schultz17c816f2016-04-11 16:10:03 +0200180 case GTPA_I_TEI:
181 case GTPA_O_TEI:
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100182 case GTPA_SGSN_ADDRESS:
183 case GTPA_MS_ADDRESS:
184 case GTPA_VERSION:
185 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
186 perror("mnl_attr_validate");
187 return MNL_CB_ERROR;
188 }
189 break;
190 default:
191 break;
192 }
193 tb[type] = attr;
194 return MNL_CB_OK;
195}
196
197static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data)
198{
199 struct nlattr *tb[GTPA_MAX + 1] = {};
Andreas Schultz17c816f2016-04-11 16:10:03 +0200200 struct gtp_pdp pdp = {};
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100201 struct genlmsghdr *genl;
202
203 mnl_attr_parse(nlh, sizeof(*genl), genl_gtp_validate_cb, tb);
204 if (tb[GTPA_TID])
Andreas Schultz17c816f2016-04-11 16:10:03 +0200205 pdp.u.v0.tid = mnl_attr_get_u64(tb[GTPA_TID]);
206 if (tb[GTPA_I_TEI])
207 pdp.u.v1.i_tei = mnl_attr_get_u32(tb[GTPA_I_TEI]);
208 if (tb[GTPA_O_TEI])
209 pdp.u.v1.o_tei = mnl_attr_get_u32(tb[GTPA_O_TEI]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100210 if (tb[GTPA_SGSN_ADDRESS]) {
211 pdp.sgsn_addr.s_addr =
212 mnl_attr_get_u32(tb[GTPA_SGSN_ADDRESS]);
213 }
214 if (tb[GTPA_MS_ADDRESS]) {
215 pdp.ms_addr.s_addr = mnl_attr_get_u32(tb[GTPA_MS_ADDRESS]);
216 }
217 if (tb[GTPA_VERSION]) {
218 pdp.version = mnl_attr_get_u32(tb[GTPA_VERSION]);
219 }
220
221 printf("version %u ", pdp.version);
Andreas Schultz17c816f2016-04-11 16:10:03 +0200222 if (pdp.version == GTP_V0)
223 printf("tid %"PRIu64" ms_addr %s ",
224 pdp.u.v0.tid, inet_ntoa(pdp.sgsn_addr));
225 else if (pdp.version == GTP_V1)
226 printf("tei %u/%u ms_addr %s ", pdp.u.v1.i_tei,
227 pdp.u.v1.o_tei, inet_ntoa(pdp.sgsn_addr));
Pablo Neira Ayusof8ca7652014-02-22 22:38:05 +0100228 printf("sgsn_addr %s\n", inet_ntoa(pdp.sgsn_addr));
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100229
230 return MNL_CB_OK;
231}
232
233static int
234list_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl)
235{
236 char buf[MNL_SOCKET_BUFFER_SIZE];
237 struct nlmsghdr *nlh;
238 uint32_t seq = time(NULL);
239
240 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_DUMP, 0,
241 GTP_CMD_TUNNEL_GET);
242
243 if (genl_socket_talk(nl, nlh, seq, genl_gtp_attr_cb, NULL) < 0) {
244 perror("genl_socket_talk");
245 return 0;
246 }
247
248 return 0;
249}
250
251int main(int argc, char *argv[])
252{
253 struct mnl_socket *nl;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100254 int32_t genl_id;
255 int ret;
256
257 if (argc < 2) {
258 printf("%s <add|delete|list> [<options,...>]\n", argv[0]);
259 exit(EXIT_FAILURE);
260 }
261
262 nl = genl_socket_open();
263 if (nl == NULL) {
264 perror("mnl_socket_open");
265 exit(EXIT_FAILURE);
266 }
267
268 genl_id = genl_lookup_family(nl, "gtp");
269 if (genl_id < 0) {
270 printf("not found gtp genl family\n");
271 exit(EXIT_FAILURE);
272 }
273
274 if (strncmp(argv[1], "add", strlen(argv[1])) == 0)
275 ret = add_tunnel(argc, argv, genl_id, nl);
276 else if (strncmp(argv[1], "delete", strlen(argv[1])) == 0)
277 ret = del_tunnel(argc, argv, genl_id, nl);
278 else if (strncmp(argv[1], "list", strlen(argv[1])) == 0)
279 ret = list_tunnel(argc, argv, genl_id, nl);
280 else {
281 printf("Unknown command `%s'\n", argv[1]);
282 exit(EXIT_FAILURE);
283 }
284
285 mnl_socket_close(nl);
286
287 return ret;
288}