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 |
Pablo Neira Ayuso | 9438f72 | 2016-05-08 18:48:25 +0200 | [diff] [blame] | 4 | * (C) 2016 by Pablo Neira Ayuso <pablo@netfilter.org> |
| 5 | * |
Harald Welte | 51b00a6 | 2014-04-03 09:37:38 -0400 | [diff] [blame] | 6 | * 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 25 | #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 Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 33 | #include <net/if.h> |
| 34 | #include <inttypes.h> |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 35 | |
| 36 | #include <libmnl/libmnl.h> |
| 37 | #include <linux/genetlink.h> |
| 38 | |
Pablo Neira Ayuso | b9f6ffe | 2016-05-08 18:27:55 +0200 | [diff] [blame] | 39 | #include <linux/gtp.h> |
| 40 | #include <linux/if_link.h> |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 41 | #include <libgtpnl/gtp.h> |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 42 | #include <libgtpnl/gtpnl.h> |
| 43 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 44 | static 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 52 | static int |
| 53 | add_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl) |
| 54 | { |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 55 | struct gtp_tunnel *t; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 56 | uint32_t gtp_ifidx; |
| 57 | struct in_addr ms, sgsn; |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 58 | uint32_t gtp_version; |
| 59 | int optidx; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 60 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 61 | if (argc < 7 || argc > 8) { |
| 62 | add_usage(argv[0]); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 63 | return EXIT_FAILURE; |
| 64 | } |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 65 | |
| 66 | t = gtp_tunnel_alloc(); |
| 67 | optidx = 2; |
| 68 | |
| 69 | gtp_ifidx = if_nametoindex(argv[optidx]); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 70 | if (gtp_ifidx == 0) { |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 71 | fprintf(stderr, "wrong GTP interface %s\n", argv[optidx]); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 72 | return EXIT_FAILURE; |
| 73 | } |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 74 | gtp_tunnel_set_ifidx(t, gtp_ifidx); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 75 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 76 | optidx++; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 77 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 78 | if (strcmp(argv[optidx], "v0") == 0) |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 79 | gtp_version = GTP_V0; |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 80 | else if (strcmp(argv[optidx], "v1") == 0) |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 81 | gtp_version = GTP_V1; |
| 82 | else { |
| 83 | fprintf(stderr, "wrong GTP version %s, use v0 or v1\n", |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 84 | argv[optidx]); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 85 | return EXIT_FAILURE; |
| 86 | } |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 87 | gtp_tunnel_set_version(t, gtp_version); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 88 | |
Pablo Neira Ayuso | 517be89 | 2016-05-08 19:50:49 +0200 | [diff] [blame] | 89 | optidx++; |
| 90 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 91 | 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 97 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 98 | 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 103 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 104 | 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static int |
| 117 | del_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl) |
| 118 | { |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 119 | struct gtp_tunnel *t; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 120 | uint32_t gtp_ifidx; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 121 | |
| 122 | if (argc != 5) { |
| 123 | printf("%s add <gtp device> <version> <tid>\n", |
| 124 | argv[0]); |
| 125 | return EXIT_FAILURE; |
| 126 | } |
| 127 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 128 | t = gtp_tunnel_alloc(); |
| 129 | |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 130 | gtp_ifidx = if_nametoindex(argv[2]); |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 131 | 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 136 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 137 | 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 148 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 149 | gtp_del_tunnel(genl_id, nl, t); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 150 | |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 151 | gtp_tunnel_free(t); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | struct gtp_pdp { |
| 156 | uint32_t version; |
Andreas Schultz | 17c816f | 2016-04-11 16:10:03 +0200 | [diff] [blame] | 157 | 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 Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 166 | struct in_addr sgsn_addr; |
| 167 | struct in_addr ms_addr; |
| 168 | }; |
| 169 | |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 170 | static int |
| 171 | list_tunnel(int argc, char *argv[], int genl_id, struct mnl_socket *nl) |
| 172 | { |
Pablo Neira Ayuso | 9438f72 | 2016-05-08 18:48:25 +0200 | [diff] [blame] | 173 | return gtp_list_tunnel(genl_id, nl); |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | int main(int argc, char *argv[]) |
| 177 | { |
| 178 | struct mnl_socket *nl; |
Pablo Neira Ayuso | 1450666 | 2014-02-20 18:43:15 +0100 | [diff] [blame] | 179 | 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 | } |