blob: 29494c6a819393536d96838b0be173aefdef63e1 [file] [log] [blame]
Harald Welte799e0c92010-04-30 21:49:24 +02001/*
2 * (C) 2010 by Harald Welte <laforge@gnumonks.org>
3 * (C) 2010 by On-Waves
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01007 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
Harald Welte799e0c92010-04-30 21:49:24 +02009 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010014 * GNU Affero General Public License for more details.
Harald Welte799e0c92010-04-30 21:49:24 +020015 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte799e0c92010-04-30 21:49:24 +020018 *
19 */
20
Harald Welte799e0c92010-04-30 21:49:24 +020021#include <sys/socket.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
Jacob Erlbeck73685282014-05-23 20:48:07 +020024#include <string.h>
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +020025#include <time.h>
Harald Welte799e0c92010-04-30 21:49:24 +020026
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010027#include <osmocom/core/talloc.h>
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +020028#include <osmocom/core/rate_ctr.h>
Harald Welte799e0c92010-04-30 21:49:24 +020029
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +020030#include <openbsc/gsm_04_08.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080031#include <osmocom/gprs/gprs_ns.h>
32
Harald Welte799e0c92010-04-30 21:49:24 +020033#include <openbsc/debug.h>
Harald Welteb77c6972010-05-01 11:28:43 +020034#include <openbsc/gb_proxy.h>
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +020035#include <openbsc/gprs_utils.h>
Harald Welte62ab20c2010-05-14 18:59:17 +020036#include <openbsc/vty.h>
Harald Welte799e0c92010-04-30 21:49:24 +020037
Harald Welte4b037e42010-05-19 19:45:32 +020038#include <osmocom/vty/command.h>
39#include <osmocom/vty/vty.h>
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +020040#include <osmocom/vty/misc.h>
Harald Welte799e0c92010-04-30 21:49:24 +020041
Harald Welte799e0c92010-04-30 21:49:24 +020042static struct gbproxy_config *g_cfg = NULL;
43
44/*
45 * vty code for mgcp below
46 */
Harald Welteb77c6972010-05-01 11:28:43 +020047static struct cmd_node gbproxy_node = {
Harald Welte799e0c92010-04-30 21:49:24 +020048 GBPROXY_NODE,
Harald Welte570ce242012-08-17 13:16:10 +020049 "%s(config-gbproxy)# ",
Harald Welte799e0c92010-04-30 21:49:24 +020050 1,
51};
52
Jacob Erlbeck91fb6802014-05-28 10:59:10 +020053static const struct value_string patch_modes[] = {
54 {GBPROX_PATCH_DEFAULT, "default"},
55 {GBPROX_PATCH_BSSGP, "bssgp"},
56 {GBPROX_PATCH_LLC_ATTACH_REQ, "llc-attach-req"},
57 {GBPROX_PATCH_LLC_ATTACH, "llc-attach"},
58 {GBPROX_PATCH_LLC_GMM, "llc-gmm"},
Jacob Erlbeck73685282014-05-23 20:48:07 +020059 {GBPROX_PATCH_LLC_GSM, "llc-gsm"},
Jacob Erlbeck91fb6802014-05-28 10:59:10 +020060 {GBPROX_PATCH_LLC, "llc"},
61 {0, NULL}
62};
63
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +020064static void gbprox_vty_print_peer(struct vty *vty, struct gbproxy_peer *peer)
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +020065{
66 struct gprs_ra_id raid;
67 gsm48_parse_ra(&raid, peer->ra);
68
69 vty_out(vty, "NSEI %5u, PTP-BVCI %5u, "
70 "RAI %u-%u-%u-%u",
71 peer->nsei, peer->bvci,
72 raid.mcc, raid.mnc, raid.lac, raid.rac);
73 if (peer->blocked)
74 vty_out(vty, " [BVC-BLOCKED]");
75
76 vty_out(vty, "%s", VTY_NEWLINE);
77}
78
Harald Welte799e0c92010-04-30 21:49:24 +020079static int config_write_gbproxy(struct vty *vty)
80{
Harald Welte799e0c92010-04-30 21:49:24 +020081 vty_out(vty, "gbproxy%s", VTY_NEWLINE);
82
Harald Welteff3bde82010-05-19 15:09:09 +020083 vty_out(vty, " sgsn nsei %u%s", g_cfg->nsip_sgsn_nsei,
Harald Welte799e0c92010-04-30 21:49:24 +020084 VTY_NEWLINE);
Harald Welte799e0c92010-04-30 21:49:24 +020085
Jacob Erlbeck67a44452014-05-19 10:14:58 +020086 if (g_cfg->core_mcc > 0)
87 vty_out(vty, " core-mobile-country-code %d%s",
88 g_cfg->core_mcc, VTY_NEWLINE);
89 if (g_cfg->core_mnc > 0)
90 vty_out(vty, " core-mobile-network-code %d%s",
91 g_cfg->core_mnc, VTY_NEWLINE);
Jacob Erlbeck73685282014-05-23 20:48:07 +020092 if (g_cfg->core_apn != NULL) {
93 if (g_cfg->core_apn_size > 0) {
94 char str[500] = {0};
Jacob Erlbeck7c101d92014-06-06 18:49:23 +020095 vty_out(vty, " core-access-point-name %s",
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +020096 gprs_apn_to_str(str, g_cfg->core_apn,
Jacob Erlbeck7c101d92014-06-06 18:49:23 +020097 g_cfg->core_apn_size));
Jacob Erlbeck73685282014-05-23 20:48:07 +020098 } else {
Jacob Erlbeck7c101d92014-06-06 18:49:23 +020099 vty_out(vty, " core-access-point-name none");
Jacob Erlbeck73685282014-05-23 20:48:07 +0200100 }
Jacob Erlbeck7c101d92014-06-06 18:49:23 +0200101 if (g_cfg->match_re)
102 vty_out(vty, " match-imsi %s%s",
103 g_cfg->match_re, VTY_NEWLINE);
104 else
105 vty_out(vty, "%s", VTY_NEWLINE);
Jacob Erlbeck73685282014-05-23 20:48:07 +0200106 }
Jacob Erlbeck7c101d92014-06-06 18:49:23 +0200107 if (g_cfg->tlli_max_age > 0)
108 vty_out(vty, " tlli-list max-age %d%s",
109 g_cfg->tlli_max_age, VTY_NEWLINE);
110 if (g_cfg->tlli_max_len > 0)
111 vty_out(vty, " tlli-list max-length %d%s",
112 g_cfg->tlli_max_len, VTY_NEWLINE);
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200113
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200114 if (g_cfg->patch_mode != GBPROX_PATCH_DEFAULT)
115 vty_out(vty, " patch-mode %s%s",
116 get_value_string(patch_modes, g_cfg->patch_mode),
117 VTY_NEWLINE);
118
Harald Welte799e0c92010-04-30 21:49:24 +0200119 return CMD_SUCCESS;
120}
121
Harald Welte799e0c92010-04-30 21:49:24 +0200122DEFUN(cfg_gbproxy,
123 cfg_gbproxy_cmd,
124 "gbproxy",
125 "Configure the Gb proxy")
126{
127 vty->node = GBPROXY_NODE;
128 return CMD_SUCCESS;
129}
130
Harald Welte799e0c92010-04-30 21:49:24 +0200131DEFUN(cfg_nsip_sgsn_nsei,
132 cfg_nsip_sgsn_nsei_cmd,
Harald Welteff3bde82010-05-19 15:09:09 +0200133 "sgsn nsei <0-65534>",
Holger Hans Peter Freyther2eb6e2c2011-11-05 15:14:59 +0100134 "SGSN information\n"
135 "NSEI to be used in the connection with the SGSN\n"
136 "The NSEI\n")
Harald Welte799e0c92010-04-30 21:49:24 +0200137{
138 unsigned int port = atoi(argv[0]);
139
140 g_cfg->nsip_sgsn_nsei = port;
141 return CMD_SUCCESS;
142}
143
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200144#define GBPROXY_CORE_MNC_STR "Use this network code for the core network\n"
145
146DEFUN(cfg_gbproxy_core_mnc,
147 cfg_gbproxy_core_mnc_cmd,
148 "core-mobile-network-code <1-999>",
149 GBPROXY_CORE_MNC_STR "NCC value\n")
150{
151 g_cfg->core_mnc = atoi(argv[0]);
152 return CMD_SUCCESS;
153}
154
155DEFUN(cfg_gbproxy_no_core_mnc,
156 cfg_gbproxy_no_core_mnc_cmd,
157 "no core-mobile-network-code",
158 NO_STR GBPROXY_CORE_MNC_STR)
159{
160 g_cfg->core_mnc = 0;
161 return CMD_SUCCESS;
162}
163
164#define GBPROXY_CORE_MCC_STR "Use this country code for the core network\n"
165
166DEFUN(cfg_gbproxy_core_mcc,
167 cfg_gbproxy_core_mcc_cmd,
168 "core-mobile-country-code <1-999>",
169 GBPROXY_CORE_MCC_STR "MCC value\n")
170{
171 g_cfg->core_mcc = atoi(argv[0]);
172 return CMD_SUCCESS;
173}
174
175DEFUN(cfg_gbproxy_no_core_mcc,
176 cfg_gbproxy_no_core_mcc_cmd,
177 "no core-mobile-country-code",
178 NO_STR GBPROXY_CORE_MCC_STR)
179{
180 g_cfg->core_mcc = 0;
181 return CMD_SUCCESS;
182}
183
Jacob Erlbeck73685282014-05-23 20:48:07 +0200184#define GBPROXY_CORE_APN_STR "Use this access point name (APN) for the backbone\n"
Jacob Erlbeck7c101d92014-06-06 18:49:23 +0200185#define GBPROXY_CORE_APN_ARG_STR "Replace APN by this string\n" "Remove APN\n"
Jacob Erlbeck73685282014-05-23 20:48:07 +0200186
Jacob Erlbeck7c101d92014-06-06 18:49:23 +0200187static int set_core_apn(struct vty *vty, const char *apn, const char *filter)
Jacob Erlbeck73685282014-05-23 20:48:07 +0200188{
Jacob Erlbeck7c101d92014-06-06 18:49:23 +0200189 const char *err_msg = NULL;
190 int apn_len;
Jacob Erlbeck73685282014-05-23 20:48:07 +0200191
Jacob Erlbeck7c101d92014-06-06 18:49:23 +0200192 if (!apn) {
193 talloc_free(g_cfg->core_apn);
194 g_cfg->core_apn = NULL;
195 g_cfg->core_apn_size = 0;
196 gbprox_set_patch_filter(NULL, NULL);
197 return CMD_SUCCESS;
198 }
Jacob Erlbeck73685282014-05-23 20:48:07 +0200199
Jacob Erlbeck7c101d92014-06-06 18:49:23 +0200200 apn_len = strlen(apn);
201
202 if (apn_len >= 100) {
Jacob Erlbeck73685282014-05-23 20:48:07 +0200203 vty_out(vty, "APN string too long (max 99 chars)%s",
204 VTY_NEWLINE);
205 return CMD_WARNING;
206 }
207
Jacob Erlbeck7c101d92014-06-06 18:49:23 +0200208 if (!filter) {
209 gbprox_set_patch_filter(NULL, NULL);
210 } else if (gbprox_set_patch_filter(filter, &err_msg) != 0) {
211 vty_out(vty, "Match expression invalid: %s%s",
212 err_msg, VTY_NEWLINE);
213 return CMD_WARNING;
214 }
215
216 talloc_free(g_cfg->match_re);
217 if (filter)
218 /* TODO: replace NULL */
219 g_cfg->match_re = talloc_strdup(NULL, filter);
220 else
221 g_cfg->match_re = NULL;
222
223 if (apn_len == 0) {
224 talloc_free(g_cfg->core_apn);
225 /* TODO: replace NULL */
226 g_cfg->core_apn = talloc_zero_size(NULL, 2);
227 g_cfg->core_apn_size = 0;
228 } else {
229 /* TODO: replace NULL */
230 g_cfg->core_apn =
231 talloc_realloc_size(NULL, g_cfg->core_apn, apn_len + 1);
232 g_cfg->core_apn_size =
Holger Hans Peter Freyther7127b022014-08-04 11:52:52 +0200233 gprs_str_to_apn(g_cfg->core_apn, apn, apn_len + 1);
Jacob Erlbeck7c101d92014-06-06 18:49:23 +0200234 }
Jacob Erlbeck73685282014-05-23 20:48:07 +0200235
236 return CMD_SUCCESS;
237}
238
Jacob Erlbeck7c101d92014-06-06 18:49:23 +0200239DEFUN(cfg_gbproxy_core_apn,
240 cfg_gbproxy_core_apn_cmd,
241 "core-access-point-name (APN|none)",
242 GBPROXY_CORE_APN_STR GBPROXY_CORE_APN_ARG_STR)
243{
244 if (strcmp(argv[0], "none") == 0)
245 return set_core_apn(vty, "", NULL);
246 else
247 return set_core_apn(vty, argv[0], NULL);
248}
249
250DEFUN(cfg_gbproxy_core_apn_match,
251 cfg_gbproxy_core_apn_match_cmd,
252 "core-access-point-name (APN|none) match-imsi .REGEXP",
253 GBPROXY_CORE_APN_STR GBPROXY_CORE_APN_ARG_STR
254 "Only modify if the IMSI matches\n"
255 "Regular expression for the match\n")
256{
257 if (strcmp(argv[0], "none") == 0)
258 return set_core_apn(vty, "", argv[1]);
259 else
260 return set_core_apn(vty, argv[0], argv[1]);
261}
262
Jacob Erlbeck73685282014-05-23 20:48:07 +0200263DEFUN(cfg_gbproxy_no_core_apn,
264 cfg_gbproxy_no_core_apn_cmd,
265 "no core-access-point-name",
266 NO_STR GBPROXY_CORE_APN_STR)
267{
Jacob Erlbeck7c101d92014-06-06 18:49:23 +0200268 return set_core_apn(vty, NULL, NULL);
269}
270
271#define GBPROXY_TLLI_LIST_STR "Set TLLI list parameters\n"
272#define GBPROXY_MAX_AGE_STR "Limit maximum age\n"
273
274DEFUN(cfg_gbproxy_tlli_list_max_age,
275 cfg_gbproxy_tlli_list_max_age_cmd,
276 "tlli-list max-age <1-999999>",
277 GBPROXY_TLLI_LIST_STR GBPROXY_MAX_AGE_STR
278 "Maximum age in seconds\n")
279{
280 g_cfg->tlli_max_age = atoi(argv[0]);
281
Jacob Erlbeck73685282014-05-23 20:48:07 +0200282 return CMD_SUCCESS;
283}
284
Jacob Erlbeck7c101d92014-06-06 18:49:23 +0200285DEFUN(cfg_gbproxy_tlli_list_no_max_age,
286 cfg_gbproxy_tlli_list_no_max_age_cmd,
287 "no tlli-list max-age",
288 NO_STR GBPROXY_TLLI_LIST_STR GBPROXY_MAX_AGE_STR)
289{
290 g_cfg->tlli_max_age = 0;
291
292 return CMD_SUCCESS;
293}
294
295#define GBPROXY_MAX_LEN_STR "Limit list length\n"
296
297DEFUN(cfg_gbproxy_tlli_list_max_len,
298 cfg_gbproxy_tlli_list_max_len_cmd,
299 "tlli-list max-length <1-99999>",
300 GBPROXY_TLLI_LIST_STR GBPROXY_MAX_LEN_STR
301 "Maximum number of TLLIs in the list\n")
302{
303 g_cfg->tlli_max_len = atoi(argv[0]);
304
305 return CMD_SUCCESS;
306}
307
308DEFUN(cfg_gbproxy_tlli_list_no_max_len,
309 cfg_gbproxy_tlli_list_no_max_len_cmd,
310 "no tlli-list max-length",
311 NO_STR GBPROXY_TLLI_LIST_STR GBPROXY_MAX_LEN_STR)
312{
313 g_cfg->tlli_max_len = 0;
314
315 return CMD_SUCCESS;
316}
317
318
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200319DEFUN(cfg_gbproxy_patch_mode,
320 cfg_gbproxy_patch_mode_cmd,
Jacob Erlbeck73685282014-05-23 20:48:07 +0200321 "patch-mode (default|bssgp|llc-attach-req|llc-attach|llc-gmm|llc-gsm|llc)",
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200322 "Set patch mode\n"
Jacob Erlbeck73685282014-05-23 20:48:07 +0200323 "Use build-in default (best effort, try to patch everything)\n"
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200324 "Only patch BSSGP headers\n"
325 "Patch BSSGP headers and LLC Attach Request messages\n"
326 "Patch BSSGP headers and LLC Attach Request/Accept messages\n"
Jacob Erlbeck73685282014-05-23 20:48:07 +0200327 "Patch BSSGP headers and LLC GMM messages\n"
328 "Patch BSSGP headers, LLC GMM, and LLC GSM messages\n"
329 "Patch BSSGP headers and all supported LLC messages\n"
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200330 )
331{
332 int val = get_string_value(patch_modes, argv[0]);
333 OSMO_ASSERT(val >= 0);
334 g_cfg->patch_mode = val;
335 return CMD_SUCCESS;
336}
337
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +0200338DEFUN(show_gbproxy, show_gbproxy_cmd, "show gbproxy [stats]",
339 SHOW_STR "Display information about the Gb proxy\n" "Show statistics\n")
340{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200341 struct gbproxy_peer *peer;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +0200342 int show_stats = argc >= 1;
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200343
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +0200344 if (show_stats)
Holger Hans Peter Freythera7027a02014-08-04 11:19:56 +0200345 vty_out_rate_ctr_group(vty, "", gbcfg.ctrg);
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +0200346
347 llist_for_each_entry(peer, &gbcfg.bts_peers, list) {
348 gbprox_vty_print_peer(vty, peer);
349
350 if (show_stats)
351 vty_out_rate_ctr_group(vty, " ", peer->ctrg);
352 }
353 return CMD_SUCCESS;
354}
355
356DEFUN(show_gbproxy_tllis, show_gbproxy_tllis_cmd, "show gbproxy tllis",
357 SHOW_STR "Display information about the Gb proxy\n" "Show TLLIs\n")
358{
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200359 struct gbproxy_peer *peer;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +0200360 char mi_buf[200];
361 time_t now = time(NULL);
362
363 llist_for_each_entry(peer, &gbcfg.bts_peers, list) {
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200364 struct gbproxy_tlli_info *tlli_info;
365 struct gbproxy_patch_state *state = &peer->patch_state;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +0200366
367 gbprox_vty_print_peer(vty, peer);
368
369 llist_for_each_entry(tlli_info, &state->enabled_tllis, list) {
370 time_t age = now - tlli_info->timestamp;
371 snprintf(mi_buf, sizeof(mi_buf), "(invalid)");
372 gsm48_mi_to_string(mi_buf, sizeof(mi_buf),
373 tlli_info->mi_data,
374 tlli_info->mi_data_len);
375 vty_out(vty, " TLLI %08x, IMSI %s, AGE %d%s",
376 tlli_info->tlli, mi_buf, (int)age,
377 VTY_NEWLINE);
378 }
379 }
380 return CMD_SUCCESS;
381}
382
383DEFUN(delete_gb_bvci, delete_gb_bvci_cmd,
384 "delete-gbproxy-peer <0-65534> bvci <2-65534>",
385 "Delete a GBProxy peer by NSEI and optionally BVCI\n"
386 "NSEI number\n"
387 "Only delete peer with a matching BVCI\n"
388 "BVCI number\n")
389{
390 const uint16_t nsei = atoi(argv[0]);
391 const uint16_t bvci = atoi(argv[1]);
392 int counter;
393
394 counter = gbprox_cleanup_peers(nsei, bvci);
395
396 if (counter == 0) {
397 vty_out(vty, "BVC not found%s", VTY_NEWLINE);
398 return CMD_WARNING;
399 }
400
401 return CMD_SUCCESS;
402}
403
404DEFUN(delete_gb_nsei, delete_gb_nsei_cmd,
405 "delete-gbproxy-peer <0-65534> (only-bvc|only-nsvc|all) [dry-run]",
406 "Delete a GBProxy peer by NSEI and optionally BVCI\n"
407 "NSEI number\n"
408 "Only delete BSSGP connections (BVC)\n"
409 "Only delete dynamic NS connections (NS-VC)\n"
410 "Delete BVC and dynamic NS connections\n"
411 "Show what would be deleted instead of actually deleting\n"
412 )
413{
414 const uint16_t nsei = atoi(argv[0]);
415 const char *mode = argv[1];
416 int dry_run = argc > 2;
417 int delete_bvc = 0;
418 int delete_nsvc = 0;
419 int counter;
420
421 if (strcmp(mode, "only-bvc") == 0)
422 delete_bvc = 1;
423 else if (strcmp(mode, "only-nsvc") == 0)
424 delete_nsvc = 1;
425 else
426 delete_bvc = delete_nsvc = 1;
427
428 if (delete_bvc) {
429 if (!dry_run)
430 counter = gbprox_cleanup_peers(nsei, 0);
431 else {
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200432 struct gbproxy_peer *peer;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +0200433 counter = 0;
434 llist_for_each_entry(peer, &gbcfg.bts_peers, list) {
435 if (peer->nsei != nsei)
436 continue;
437
438 vty_out(vty, "BVC: ");
439 gbprox_vty_print_peer(vty, peer);
440 counter += 1;
441 }
442 }
443 vty_out(vty, "%sDeleted %d BVC%s",
444 dry_run ? "Not " : "", counter, VTY_NEWLINE);
445 }
446
447 if (delete_nsvc) {
448 struct gprs_ns_inst *nsi = gbcfg.nsi;
449 struct gprs_nsvc *nsvc, *nsvc2;
450
451 counter = 0;
452 llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list) {
453 if (nsvc->nsei != nsei)
454 continue;
455 if (nsvc->persistent)
456 continue;
457
458 if (!dry_run)
459 gprs_nsvc_delete(nsvc);
460 else
461 vty_out(vty, "NS-VC: NSEI %5u, NS-VCI %5u, "
462 "remote %s%s",
463 nsvc->nsei, nsvc->nsvci,
464 gprs_ns_ll_str(nsvc), VTY_NEWLINE);
465 counter += 1;
466 }
467 vty_out(vty, "%sDeleted %d NS-VC%s",
468 dry_run ? "Not " : "", counter, VTY_NEWLINE);
469 }
470
471 return CMD_SUCCESS;
472}
473
474DEFUN(delete_gb_tlli, delete_gb_tlli_cmd,
475 "delete-gbproxy-tlli <0-65534> (tlli|imsi|stale) [IDENT]",
476 "Delete a GBProxy TLLI entry by NSEI and identification\n"
477 "NSEI number\n"
478 "Delete entries with a matching TLLI (hex)\n"
479 "Delete entries with a matching IMSI\n"
480 "Identification to match\n")
481{
482 const uint16_t nsei = atoi(argv[0]);
483 enum {MATCH_TLLI = 't', MATCH_IMSI = 'i', MATCH_STALE = 's'} match;
484 uint32_t tlli = 0;
485 const char *imsi = NULL;
Holger Hans Peter Freyther1ddd9e52014-08-04 11:35:32 +0200486 struct gbproxy_peer *peer = 0;
487 struct gbproxy_tlli_info *tlli_info, *nxt;
488 struct gbproxy_patch_state *state;
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +0200489 char mi_buf[200];
490 int found = 0;
491
492 match = argv[1][0];
493
494 switch (match) {
495 case MATCH_TLLI:
496 if (argc < 2 || !argv[2][0]) {
497 vty_out(vty, "%% Missing TLLI%s", VTY_NEWLINE);
498 return CMD_WARNING;
499 }
500 tlli = strtoll(argv[2], NULL, 16);
501 break;
502 case MATCH_IMSI:
503 if (argc < 2 || !argv[2][0]) {
504 vty_out(vty, "%% Missing IMSI%s", VTY_NEWLINE);
505 return CMD_WARNING;
506 }
507 imsi = argv[2];
508 break;
509 default:
510 break;
511 }
512
Holger Hans Peter Freyther16f30b52014-08-04 11:24:25 +0200513 peer = gbprox_peer_by_nsei(nsei);
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +0200514 if (!peer) {
515 vty_out(vty, "Didn't find peer with NSEI %d%s",
516 nsei, VTY_NEWLINE);
517 return CMD_WARNING;
518 }
519
520 state = &peer->patch_state;
521
522 if (match == MATCH_STALE) {
Holger Hans Peter Freytherd4d36f22014-08-04 11:14:54 +0200523 found = gbprox_remove_stale_tllis(peer, time(NULL));
Holger Hans Peter Freyther18739ea2014-08-04 11:10:09 +0200524 if (found)
525 vty_out(vty, "Deleted %d stale TLLI%s%s",
526 found, found == 1 ? "" : "s", VTY_NEWLINE);
527 return CMD_SUCCESS;
528 }
529
530 llist_for_each_entry_safe(tlli_info, nxt, &state->enabled_tllis, list) {
531 if (match == MATCH_TLLI && tlli_info->tlli != tlli)
532 continue;
533
534 if (match == MATCH_IMSI) {
535 mi_buf[0] = '\0';
536 gsm48_mi_to_string(mi_buf, sizeof(mi_buf),
537 tlli_info->mi_data,
538 tlli_info->mi_data_len);
539
540 if (strcmp(mi_buf, imsi) != 0)
541 continue;
542 }
543 vty_out(vty, "Deleting TLLI %08x%s", tlli_info->tlli, VTY_NEWLINE);
544 gbprox_delete_tlli(peer, tlli_info);
545 found += 1;
546 }
547
548 if (!found && argc >= 2) {
549 vty_out(vty, "Didn't find TLLI entry with %s %s%s",
550 argv[1], argv[2], VTY_NEWLINE);
551 }
552
553 return CMD_SUCCESS;
554}
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200555
Harald Welte799e0c92010-04-30 21:49:24 +0200556int gbproxy_vty_init(void)
557{
Harald Welte995a2d32010-05-12 16:50:52 +0000558 install_element_ve(&show_gbproxy_cmd);
Jacob Erlbeck7dd498d2014-06-19 10:15:27 +0200559 install_element_ve(&show_gbproxy_tllis_cmd);
Harald Welte799e0c92010-04-30 21:49:24 +0200560
Jacob Erlbeck4211d792013-10-24 12:48:23 +0200561 install_element(ENABLE_NODE, &delete_gb_bvci_cmd);
562 install_element(ENABLE_NODE, &delete_gb_nsei_cmd);
Jacob Erlbeck7dd498d2014-06-19 10:15:27 +0200563 install_element(ENABLE_NODE, &delete_gb_tlli_cmd);
Holger Hans Peter Freyther90267a92013-10-23 11:24:17 +0200564
Harald Welte799e0c92010-04-30 21:49:24 +0200565 install_element(CONFIG_NODE, &cfg_gbproxy_cmd);
566 install_node(&gbproxy_node, config_write_gbproxy);
Jacob Erlbeck36722e12013-10-29 09:30:30 +0100567 vty_install_default(GBPROXY_NODE);
Harald Welte799e0c92010-04-30 21:49:24 +0200568 install_element(GBPROXY_NODE, &cfg_nsip_sgsn_nsei_cmd);
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200569 install_element(GBPROXY_NODE, &cfg_gbproxy_core_mcc_cmd);
570 install_element(GBPROXY_NODE, &cfg_gbproxy_core_mnc_cmd);
Jacob Erlbeck73685282014-05-23 20:48:07 +0200571 install_element(GBPROXY_NODE, &cfg_gbproxy_core_apn_cmd);
Jacob Erlbeck7c101d92014-06-06 18:49:23 +0200572 install_element(GBPROXY_NODE, &cfg_gbproxy_core_apn_match_cmd);
573 install_element(GBPROXY_NODE, &cfg_gbproxy_tlli_list_max_age_cmd);
574 install_element(GBPROXY_NODE, &cfg_gbproxy_tlli_list_max_len_cmd);
Jacob Erlbeck67a44452014-05-19 10:14:58 +0200575 install_element(GBPROXY_NODE, &cfg_gbproxy_no_core_mcc_cmd);
576 install_element(GBPROXY_NODE, &cfg_gbproxy_no_core_mnc_cmd);
Jacob Erlbeck73685282014-05-23 20:48:07 +0200577 install_element(GBPROXY_NODE, &cfg_gbproxy_no_core_apn_cmd);
Jacob Erlbeck7c101d92014-06-06 18:49:23 +0200578 install_element(GBPROXY_NODE, &cfg_gbproxy_tlli_list_no_max_age_cmd);
579 install_element(GBPROXY_NODE, &cfg_gbproxy_tlli_list_no_max_len_cmd);
Jacob Erlbeck91fb6802014-05-28 10:59:10 +0200580 install_element(GBPROXY_NODE, &cfg_gbproxy_patch_mode_cmd);
Harald Welte799e0c92010-04-30 21:49:24 +0200581
582 return 0;
583}
584
585int gbproxy_parse_config(const char *config_file, struct gbproxy_config *cfg)
586{
587 int rc;
588
589 g_cfg = cfg;
Harald Weltedcccb182010-05-16 20:52:23 +0200590 rc = vty_read_config_file(config_file, NULL);
Harald Welte799e0c92010-04-30 21:49:24 +0200591 if (rc < 0) {
592 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
593 return rc;
594 }
595
596 return 0;
597}
598