Harald Welte | 51b00a6 | 2014-04-03 09:37:38 -0400 | [diff] [blame] | 1 | /* 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 23 | #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 Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 31 | #include <net/if.h> |
| 32 | #include <inttypes.h> |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 33 | |
| 34 | #include <libmnl/libmnl.h> |
| 35 | #include <linux/genetlink.h> |
| 36 | |
| 37 | #include <linux/gtp_nl.h> |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 38 | #include <libgtpnl/gtp.h> |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 39 | #include <libgtpnl/gtpnl.h> |
| 40 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 41 | static 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 49 | static int |
| 50 | add_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl) |
| 51 | { |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 52 | struct gtp_tunnel *t; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 53 | uint32_t gtp_ifidx; |
| 54 | struct in_addr ms, sgsn; |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 55 | uint32_t gtp_version; |
| 56 | int optidx; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 57 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 58 | if (argc < 7 || argc > 8) { |
| 59 | add_usage(argv[0]); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 60 | return EXIT_FAILURE; |
| 61 | } |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 62 | |
| 63 | t = gtp_tunnel_alloc(); |
| 64 | optidx = 2; |
| 65 | |
| 66 | gtp_ifidx = if_nametoindex(argv[optidx]); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 67 | if (gtp_ifidx == 0) { |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 68 | fprintf(stderr, "wrong GTP interface %s\n", argv[optidx]); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 69 | return EXIT_FAILURE; |
| 70 | } |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 71 | gtp_tunnel_set_ifidx(t, gtp_ifidx); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 72 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 73 | optidx++; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 74 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 75 | if (strcmp(argv[optidx], "v0") == 0) |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 76 | gtp_version = GTP_V0; |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 77 | else if (strcmp(argv[optidx], "v1") == 0) |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 78 | gtp_version = GTP_V1; |
| 79 | else { |
| 80 | fprintf(stderr, "wrong GTP version %s, use v0 or v1\n", |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 81 | argv[optidx]); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 82 | return EXIT_FAILURE; |
| 83 | } |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 84 | gtp_tunnel_set_version(t, gtp_version); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 85 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 86 | 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 92 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 93 | 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 98 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 99 | 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static int |
| 112 | del_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl) |
| 113 | { |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 114 | struct gtp_tunnel *t; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 115 | uint32_t gtp_ifidx; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 116 | |
| 117 | if (argc != 5) { |
| 118 | printf("%s add <gtp device> <version> <tid>\n", |
| 119 | argv[0]); |
| 120 | return EXIT_FAILURE; |
| 121 | } |
| 122 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 123 | t = gtp_tunnel_alloc(); |
| 124 | |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 125 | gtp_ifidx = if_nametoindex(argv[2]); |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 126 | 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 131 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 132 | 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 143 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 144 | gtp_del_tunnel(genl_id, nl, t); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 145 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 146 | gtp_tunnel_free(t); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | struct gtp_pdp { |
| 151 | uint32_t version; |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 152 | 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 161 | struct in_addr sgsn_addr; |
| 162 | struct in_addr ms_addr; |
| 163 | }; |
| 164 | |
| 165 | static 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 Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 180 | case GTPA_I_TEI: |
| 181 | case GTPA_O_TEI: |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 182 | 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 | |
| 197 | static int genl_gtp_attr_cb(const struct nlmsghdr *nlh, void *data) |
| 198 | { |
| 199 | struct nlattr *tb[GTPA_MAX + 1] = {}; |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 200 | struct gtp_pdp pdp = {}; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 201 | struct genlmsghdr *genl; |
| 202 | |
| 203 | mnl_attr_parse(nlh, sizeof(*genl), genl_gtp_validate_cb, tb); |
| 204 | if (tb[GTPA_TID]) |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 205 | 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 210 | 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 Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 222 | 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 Ayuso | f8ca765 | 2014-02-22 22:38:05 +0100 | [diff] [blame] | 228 | printf("sgsn_addr %s\n", inet_ntoa(pdp.sgsn_addr)); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 229 | |
| 230 | return MNL_CB_OK; |
| 231 | } |
| 232 | |
| 233 | static int |
| 234 | list_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 | |
| 251 | int main(int argc, char *argv[]) |
| 252 | { |
| 253 | struct mnl_socket *nl; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 254 | 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 | } |