blob: d68f19d2eaa00ba07f7a8a9c554689ed8b46e40e [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
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +020037#include <linux/gtp.h>
38#include <linux/if_link.h>
Andreas Schultz17c816f2016-04-11 16:10:03 +020039#include <libgtpnl/gtp.h>
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010040#include <libgtpnl/gtpnl.h>
41
Andreas Schultz17c816f2016-04-11 16:10:03 +020042static void add_usage(const char *name)
43{
44 printf("%s add <gtp device> <v0> <tid> <ms-addr> <sgsn-addr>\n",
45 name);
46 printf("%s add <gtp device> <v1> <i_tei> <o_tei> <ms-addr> <sgsn-addr>\n",
47 name);
48}
49
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010050static int
51add_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl)
52{
Andreas Schultz17c816f2016-04-11 16:10:03 +020053 struct gtp_tunnel *t;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010054 uint32_t gtp_ifidx;
55 struct in_addr ms, sgsn;
Andreas Schultz17c816f2016-04-11 16:10:03 +020056 uint32_t gtp_version;
57 int optidx;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010058
Andreas Schultz17c816f2016-04-11 16:10:03 +020059 if (argc < 7 || argc > 8) {
60 add_usage(argv[0]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010061 return EXIT_FAILURE;
62 }
Andreas Schultz17c816f2016-04-11 16:10:03 +020063
64 t = gtp_tunnel_alloc();
65 optidx = 2;
66
67 gtp_ifidx = if_nametoindex(argv[optidx]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010068 if (gtp_ifidx == 0) {
Andreas Schultz17c816f2016-04-11 16:10:03 +020069 fprintf(stderr, "wrong GTP interface %s\n", argv[optidx]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010070 return EXIT_FAILURE;
71 }
Andreas Schultz17c816f2016-04-11 16:10:03 +020072 gtp_tunnel_set_ifidx(t, gtp_ifidx);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010073
Andreas Schultz17c816f2016-04-11 16:10:03 +020074 optidx++;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010075
Andreas Schultz17c816f2016-04-11 16:10:03 +020076 if (strcmp(argv[optidx], "v0") == 0)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010077 gtp_version = GTP_V0;
Andreas Schultz17c816f2016-04-11 16:10:03 +020078 else if (strcmp(argv[optidx], "v1") == 0)
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010079 gtp_version = GTP_V1;
80 else {
81 fprintf(stderr, "wrong GTP version %s, use v0 or v1\n",
Andreas Schultz17c816f2016-04-11 16:10:03 +020082 argv[optidx]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010083 return EXIT_FAILURE;
84 }
Andreas Schultz17c816f2016-04-11 16:10:03 +020085 gtp_tunnel_set_version(t, gtp_version);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010086
Andreas Schultz17c816f2016-04-11 16:10:03 +020087 if (gtp_version == GTP_V0)
88 gtp_tunnel_set_tid(t, atoi(argv[optidx++]));
89 else if (gtp_version == GTP_V1) {
90 gtp_tunnel_set_i_tei(t, atoi(argv[optidx++]));
91 gtp_tunnel_set_o_tei(t, atoi(argv[optidx++]));
92 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010093
Andreas Schultz17c816f2016-04-11 16:10:03 +020094 if (inet_aton(argv[optidx++], &ms) < 0) {
95 perror("bad address for ms");
96 exit(EXIT_FAILURE);
97 }
98 gtp_tunnel_set_ms_ip4(t, &ms);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +010099
Andreas Schultz17c816f2016-04-11 16:10:03 +0200100 if (inet_aton(argv[optidx++], &sgsn) < 0) {
101 perror("bad address for sgsn");
102 exit(EXIT_FAILURE);
103 }
104 gtp_tunnel_set_sgsn_ip4(t, &sgsn);
105
106 gtp_add_tunnel(genl_id, nl, t);
107
108 gtp_tunnel_free(t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100109 return 0;
110}
111
112static int
113del_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl)
114{
Andreas Schultz17c816f2016-04-11 16:10:03 +0200115 struct gtp_tunnel *t;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100116 uint32_t gtp_ifidx;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100117
118 if (argc != 5) {
119 printf("%s add <gtp device> <version> <tid>\n",
120 argv[0]);
121 return EXIT_FAILURE;
122 }
123
Andreas Schultz17c816f2016-04-11 16:10:03 +0200124 t = gtp_tunnel_alloc();
125
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100126 gtp_ifidx = if_nametoindex(argv[2]);
Andreas Schultz17c816f2016-04-11 16:10:03 +0200127 if (gtp_ifidx == 0) {
128 fprintf(stderr, "wrong GTP interface %s\n", argv[2]);
129 return EXIT_FAILURE;
130 }
131 gtp_tunnel_set_ifidx(t, gtp_ifidx);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100132
Andreas Schultz17c816f2016-04-11 16:10:03 +0200133 if (strcmp(argv[3], "v0") == 0) {
134 gtp_tunnel_set_version(t, GTP_V0);
135 gtp_tunnel_set_tid(t, atoi(argv[4]));
136 } else if (strcmp(argv[3], "v1") == 0) {
137 gtp_tunnel_set_version(t, GTP_V1);
138 gtp_tunnel_set_i_tei(t, atoi(argv[4]));
139 } else {
140 fprintf(stderr, "wrong GTP version %s, use v0 or v1\n",
141 argv[3]);
142 return EXIT_FAILURE;
143 }
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100144
Andreas Schultz17c816f2016-04-11 16:10:03 +0200145 gtp_del_tunnel(genl_id, nl, t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100146
Andreas Schultz17c816f2016-04-11 16:10:03 +0200147 gtp_tunnel_free(t);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100148 return 0;
149}
150
151struct gtp_pdp {
152 uint32_t version;
Andreas Schultz17c816f2016-04-11 16:10:03 +0200153 union {
154 struct {
155 uint64_t tid;
156 } v0;
157 struct {
158 uint32_t i_tei;
159 uint32_t o_tei;
160 } v1;
161 } u;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100162 struct in_addr sgsn_addr;
163 struct in_addr ms_addr;
164};
165
166static int genl_gtp_validate_cb(const struct nlattr *attr, void *data)
167{
168 const struct nlattr **tb = data;
169 int type = mnl_attr_get_type(attr);
170
171 if (mnl_attr_type_valid(attr, CTRL_ATTR_MAX) < 0)
172 return MNL_CB_OK;
173
174 switch(type) {
175 case GTPA_TID:
176 if (mnl_attr_validate(attr, MNL_TYPE_U64) < 0) {
177 perror("mnl_attr_validate");
178 return MNL_CB_ERROR;
179 }
180 break;
Andreas Schultz17c816f2016-04-11 16:10:03 +0200181 case GTPA_I_TEI:
182 case GTPA_O_TEI:
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100183 case GTPA_SGSN_ADDRESS:
184 case GTPA_MS_ADDRESS:
185 case GTPA_VERSION:
186 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
187 perror("mnl_attr_validate");
188 return MNL_CB_ERROR;
189 }
190 break;
191 default:
192 break;
193 }
194 tb[type] = attr;
195 return MNL_CB_OK;
196}
197
198static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data)
199{
200 struct nlattr *tb[GTPA_MAX + 1] = {};
Andreas Schultz17c816f2016-04-11 16:10:03 +0200201 struct gtp_pdp pdp = {};
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100202 struct genlmsghdr *genl;
203
204 mnl_attr_parse(nlh, sizeof(*genl), genl_gtp_validate_cb, tb);
205 if (tb[GTPA_TID])
Andreas Schultz17c816f2016-04-11 16:10:03 +0200206 pdp.u.v0.tid = mnl_attr_get_u64(tb[GTPA_TID]);
207 if (tb[GTPA_I_TEI])
208 pdp.u.v1.i_tei = mnl_attr_get_u32(tb[GTPA_I_TEI]);
209 if (tb[GTPA_O_TEI])
210 pdp.u.v1.o_tei = mnl_attr_get_u32(tb[GTPA_O_TEI]);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100211 if (tb[GTPA_SGSN_ADDRESS]) {
212 pdp.sgsn_addr.s_addr =
213 mnl_attr_get_u32(tb[GTPA_SGSN_ADDRESS]);
214 }
215 if (tb[GTPA_MS_ADDRESS]) {
216 pdp.ms_addr.s_addr = mnl_attr_get_u32(tb[GTPA_MS_ADDRESS]);
217 }
218 if (tb[GTPA_VERSION]) {
219 pdp.version = mnl_attr_get_u32(tb[GTPA_VERSION]);
220 }
221
222 printf("version %u ", pdp.version);
Andreas Schultz17c816f2016-04-11 16:10:03 +0200223 if (pdp.version == GTP_V0)
224 printf("tid %"PRIu64" ms_addr %s ",
225 pdp.u.v0.tid, inet_ntoa(pdp.sgsn_addr));
226 else if (pdp.version == GTP_V1)
227 printf("tei %u/%u ms_addr %s ", pdp.u.v1.i_tei,
228 pdp.u.v1.o_tei, inet_ntoa(pdp.sgsn_addr));
Pablo Neira Ayusof8ca7652014-02-22 22:38:05 +0100229 printf("sgsn_addr %s\n", inet_ntoa(pdp.sgsn_addr));
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100230
231 return MNL_CB_OK;
232}
233
234static int
235list_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl)
236{
237 char buf[MNL_SOCKET_BUFFER_SIZE];
238 struct nlmsghdr *nlh;
239 uint32_t seq = time(NULL);
240
241 nlh = genl_nlmsg_build_hdr(buf, genl_id, NLM_F_DUMP, 0,
Pablo Neira Ayusob9f6ffe2016-05-08 18:27:55 +0200242 GTP_CMD_GETPDP);
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100243
244 if (genl_socket_talk(nl, nlh, seq, genl_gtp_attr_cb, NULL) < 0) {
245 perror("genl_socket_talk");
246 return 0;
247 }
248
249 return 0;
250}
251
252int main(int argc, char *argv[])
253{
254 struct mnl_socket *nl;
Pablo Neira Ayuso14506662014-02-20 18:43:15 +0100255 int32_t genl_id;
256 int ret;
257
258 if (argc < 2) {
259 printf("%s <add|delete|list> [<options,...>]\n", argv[0]);
260 exit(EXIT_FAILURE);
261 }
262
263 nl = genl_socket_open();
264 if (nl == NULL) {
265 perror("mnl_socket_open");
266 exit(EXIT_FAILURE);
267 }
268
269 genl_id = genl_lookup_family(nl, "gtp");
270 if (genl_id < 0) {
271 printf("not found gtp genl family\n");
272 exit(EXIT_FAILURE);
273 }
274
275 if (strncmp(argv[1], "add", strlen(argv[1])) == 0)
276 ret = add_tunnel(argc, argv, genl_id, nl);
277 else if (strncmp(argv[1], "delete", strlen(argv[1])) == 0)
278 ret = del_tunnel(argc, argv, genl_id, nl);
279 else if (strncmp(argv[1], "list", strlen(argv[1])) == 0)
280 ret = list_tunnel(argc, argv, genl_id, nl);
281 else {
282 printf("Unknown command `%s'\n", argv[1]);
283 exit(EXIT_FAILURE);
284 }
285
286 mnl_socket_close(nl);
287
288 return ret;
289}