Holger Hans Peter Freyther | 98da544 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2011-2012 by Holger Hans Peter Freyther |
| 3 | * (C) 2011-2012 by On-Waves |
| 4 | * (C) 2011 by Daniel Willmann |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU Affero General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #include <osmocom/core/talloc.h> |
| 23 | |
Harald Welte | ba874b8 | 2014-08-20 23:47:15 +0200 | [diff] [blame] | 24 | #include <osmocom/ctrl/control_cmd.h> |
| 25 | #include <osmocom/ctrl/control_if.h> |
Harald Welte | 55dc31e | 2014-08-24 17:54:49 +0200 | [diff] [blame] | 26 | #include <osmocom/ctrl/ports.h> |
Holger Hans Peter Freyther | 98da544 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 27 | |
Holger Hans Peter Freyther | 8ccf06c | 2015-04-05 15:26:33 +0200 | [diff] [blame] | 28 | #include <osmocom/vty/misc.h> |
| 29 | |
Harald Welte | ba874b8 | 2014-08-20 23:47:15 +0200 | [diff] [blame] | 30 | #include <openbsc/ctrl.h> |
Holger Hans Peter Freyther | 98da544 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 31 | #include <openbsc/bsc_nat.h> |
Holger Hans Peter Freyther | 973dbae | 2015-04-04 20:47:03 +0200 | [diff] [blame] | 32 | #include <openbsc/bsc_msg_filter.h> |
Holger Hans Peter Freyther | cd48525 | 2014-08-29 10:58:12 +0200 | [diff] [blame] | 33 | #include <openbsc/vty.h> |
Holger Hans Peter Freyther | ab94ca1 | 2015-04-05 15:15:36 +0200 | [diff] [blame] | 34 | #include <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | 98da544 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 35 | |
| 36 | #include <unistd.h> |
Harald Welte | ba874b8 | 2014-08-20 23:47:15 +0200 | [diff] [blame] | 37 | #include <string.h> |
| 38 | #include <errno.h> |
Holger Hans Peter Freyther | 98da544 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 39 | |
| 40 | |
| 41 | #define NAT_MAX_CTRL_ID 65535 |
| 42 | |
| 43 | static struct bsc_nat *g_nat; |
| 44 | |
| 45 | static int bsc_id_unused(int id, struct bsc_connection *bsc) |
| 46 | { |
| 47 | struct bsc_cmd_list *pending; |
| 48 | |
| 49 | llist_for_each_entry(pending, &bsc->cmd_pending, list_entry) { |
| 50 | if (pending->nat_id == id) |
| 51 | return 0; |
| 52 | } |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | static int get_next_free_bsc_id(struct bsc_connection *bsc) |
| 57 | { |
| 58 | int new_id, overflow = 0; |
| 59 | |
| 60 | new_id = bsc->last_id; |
| 61 | |
| 62 | do { |
| 63 | new_id++; |
| 64 | if (new_id == NAT_MAX_CTRL_ID) { |
| 65 | new_id = 1; |
| 66 | overflow++; |
| 67 | } |
| 68 | |
| 69 | if (bsc_id_unused(new_id, bsc)) { |
| 70 | bsc->last_id = new_id; |
| 71 | return new_id; |
| 72 | } |
| 73 | } while (overflow != 2); |
| 74 | |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | void bsc_nat_ctrl_del_pending(struct bsc_cmd_list *pending) |
| 79 | { |
| 80 | llist_del(&pending->list_entry); |
| 81 | osmo_timer_del(&pending->timeout); |
| 82 | talloc_free(pending->cmd); |
| 83 | talloc_free(pending); |
| 84 | } |
| 85 | |
| 86 | static struct bsc_cmd_list *bsc_get_pending(struct bsc_connection *bsc, char *id_str) |
| 87 | { |
| 88 | struct bsc_cmd_list *cmd_entry; |
| 89 | int id = atoi(id_str); |
| 90 | if (id == 0) |
| 91 | return NULL; |
| 92 | |
| 93 | llist_for_each_entry(cmd_entry, &bsc->cmd_pending, list_entry) { |
| 94 | if (cmd_entry->nat_id == id) { |
| 95 | return cmd_entry; |
| 96 | } |
| 97 | } |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | int bsc_nat_handle_ctrlif_msg(struct bsc_connection *bsc, struct msgb *msg) |
| 102 | { |
| 103 | struct ctrl_cmd *cmd; |
| 104 | struct bsc_cmd_list *pending; |
| 105 | char *var, *id; |
| 106 | |
| 107 | cmd = ctrl_cmd_parse(bsc, msg); |
| 108 | msgb_free(msg); |
| 109 | |
| 110 | if (!cmd) { |
| 111 | cmd = talloc_zero(bsc, struct ctrl_cmd); |
| 112 | if (!cmd) { |
| 113 | LOGP(DNAT, LOGL_ERROR, "OOM!\n"); |
| 114 | return -ENOMEM; |
| 115 | } |
| 116 | cmd->type = CTRL_TYPE_ERROR; |
| 117 | cmd->id = "err"; |
| 118 | cmd->reply = "Failed to parse command."; |
| 119 | goto err; |
| 120 | } |
| 121 | |
| 122 | if (bsc->cfg && !llist_empty(&bsc->cfg->lac_list)) { |
| 123 | if (cmd->variable) { |
| 124 | var = talloc_asprintf(cmd, "net.0.bsc.%i.%s", bsc->cfg->nr, |
| 125 | cmd->variable); |
| 126 | if (!var) { |
| 127 | cmd->type = CTRL_TYPE_ERROR; |
| 128 | cmd->reply = "OOM"; |
| 129 | goto err; |
| 130 | } |
| 131 | talloc_free(cmd->variable); |
| 132 | cmd->variable = var; |
| 133 | } |
| 134 | |
| 135 | /* We have to handle TRAPs before matching pending */ |
| 136 | if (cmd->type == CTRL_TYPE_TRAP) { |
| 137 | ctrl_cmd_send_to_all(bsc->nat->ctrl, cmd); |
| 138 | talloc_free(cmd); |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | /* Find the pending command */ |
| 143 | pending = bsc_get_pending(bsc, cmd->id); |
| 144 | if (pending) { |
| 145 | id = talloc_strdup(cmd, pending->cmd->id); |
| 146 | if (!id) { |
| 147 | cmd->type = CTRL_TYPE_ERROR; |
| 148 | cmd->reply = "OOM"; |
| 149 | goto err; |
| 150 | } |
| 151 | cmd->id = id; |
| 152 | ctrl_cmd_send(&pending->ccon->write_queue, cmd); |
| 153 | bsc_nat_ctrl_del_pending(pending); |
| 154 | } else { |
| 155 | /* We need to handle TRAPS here */ |
| 156 | if ((cmd->type != CTRL_TYPE_ERROR) && |
| 157 | (cmd->type != CTRL_TYPE_TRAP)) { |
| 158 | LOGP(DNAT, LOGL_NOTICE, "Got control message " |
| 159 | "from BSC without pending entry\n"); |
| 160 | cmd->type = CTRL_TYPE_ERROR; |
| 161 | cmd->reply = "No request outstanding"; |
| 162 | goto err; |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | talloc_free(cmd); |
| 167 | return 0; |
| 168 | err: |
| 169 | ctrl_cmd_send(&bsc->write_queue, cmd); |
| 170 | talloc_free(cmd); |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static void pending_timeout_cb(void *data) |
| 175 | { |
| 176 | struct bsc_cmd_list *pending = data; |
| 177 | LOGP(DNAT, LOGL_ERROR, "Command timed out\n"); |
| 178 | pending->cmd->type = CTRL_TYPE_ERROR; |
| 179 | pending->cmd->reply = "Command timed out"; |
| 180 | ctrl_cmd_send(&pending->ccon->write_queue, pending->cmd); |
| 181 | |
| 182 | bsc_nat_ctrl_del_pending(pending); |
| 183 | } |
| 184 | |
| 185 | static void ctrl_conn_closed_cb(struct ctrl_connection *connection) |
| 186 | { |
| 187 | struct bsc_connection *bsc; |
| 188 | struct bsc_cmd_list *pending, *tmp; |
| 189 | |
| 190 | llist_for_each_entry(bsc, &g_nat->bsc_connections, list_entry) { |
| 191 | llist_for_each_entry_safe(pending, tmp, &bsc->cmd_pending, list_entry) { |
| 192 | if (pending->ccon == connection) |
| 193 | bsc_nat_ctrl_del_pending(pending); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
Holger Hans Peter Freyther | bc3780a | 2014-02-04 19:12:04 +0100 | [diff] [blame] | 198 | static int extract_bsc_nr_variable(char *variable, unsigned int *nr, char **bsc_variable) |
| 199 | { |
| 200 | char *nr_str, *tmp, *saveptr = NULL; |
| 201 | |
| 202 | tmp = strtok_r(variable, ".", &saveptr); |
| 203 | tmp = strtok_r(NULL, ".", &saveptr); |
| 204 | tmp = strtok_r(NULL, ".", &saveptr); |
| 205 | nr_str = strtok_r(NULL, ".", &saveptr); |
| 206 | if (!nr_str) |
| 207 | return 0; |
| 208 | *nr = atoi(nr_str); |
| 209 | |
| 210 | tmp = strtok_r(NULL, "\0", &saveptr); |
| 211 | if (!tmp) |
| 212 | return 0; |
| 213 | |
| 214 | *bsc_variable = tmp; |
| 215 | return 1; |
| 216 | } |
| 217 | |
Holger Hans Peter Freyther | 98da544 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 218 | static int forward_to_bsc(struct ctrl_cmd *cmd) |
| 219 | { |
| 220 | int ret = CTRL_CMD_HANDLED; |
| 221 | struct ctrl_cmd *bsc_cmd = NULL; |
| 222 | struct bsc_connection *bsc; |
| 223 | struct bsc_cmd_list *pending; |
| 224 | unsigned int nr; |
Holger Hans Peter Freyther | bc3780a | 2014-02-04 19:12:04 +0100 | [diff] [blame] | 225 | char *bsc_variable; |
Holger Hans Peter Freyther | 98da544 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 226 | |
| 227 | /* Skip over the beginning (bsc.) */ |
Holger Hans Peter Freyther | bc3780a | 2014-02-04 19:12:04 +0100 | [diff] [blame] | 228 | if (!extract_bsc_nr_variable(cmd->variable, &nr, &bsc_variable)) { |
Holger Hans Peter Freyther | 98da544 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 229 | cmd->reply = "command incomplete"; |
| 230 | goto err; |
| 231 | } |
Holger Hans Peter Freyther | 98da544 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 232 | |
Holger Hans Peter Freyther | 98da544 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 233 | |
| 234 | llist_for_each_entry(bsc, &g_nat->bsc_connections, list_entry) { |
| 235 | if (!bsc->cfg) |
| 236 | continue; |
| 237 | if (!bsc->authenticated) |
| 238 | continue; |
| 239 | if (bsc->cfg->nr == nr) { |
| 240 | /* Add pending command to list */ |
| 241 | pending = talloc_zero(bsc, struct bsc_cmd_list); |
| 242 | if (!pending) { |
| 243 | cmd->reply = "OOM"; |
| 244 | goto err; |
| 245 | } |
| 246 | |
| 247 | pending->nat_id = get_next_free_bsc_id(bsc); |
| 248 | if (pending->nat_id < 0) { |
| 249 | cmd->reply = "No free ID found"; |
| 250 | goto err; |
| 251 | } |
| 252 | |
| 253 | bsc_cmd = ctrl_cmd_cpy(bsc, cmd); |
| 254 | if (!bsc_cmd) { |
| 255 | cmd->reply = "Could not forward command"; |
| 256 | goto err; |
| 257 | } |
| 258 | |
| 259 | talloc_free(bsc_cmd->id); |
| 260 | bsc_cmd->id = talloc_asprintf(bsc_cmd, "%i", pending->nat_id); |
| 261 | if (!bsc_cmd->id) { |
| 262 | cmd->reply = "OOM"; |
| 263 | goto err; |
| 264 | } |
| 265 | |
| 266 | talloc_free(bsc_cmd->variable); |
Holger Hans Peter Freyther | bc3780a | 2014-02-04 19:12:04 +0100 | [diff] [blame] | 267 | bsc_cmd->variable = talloc_strdup(bsc_cmd, bsc_variable); |
Holger Hans Peter Freyther | 98da544 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 268 | if (!bsc_cmd->variable) { |
| 269 | cmd->reply = "OOM"; |
| 270 | goto err; |
| 271 | } |
| 272 | |
| 273 | if (ctrl_cmd_send(&bsc->write_queue, bsc_cmd)) { |
| 274 | cmd->reply = "Sending failed"; |
| 275 | goto err; |
| 276 | } |
| 277 | pending->ccon = cmd->ccon; |
| 278 | pending->ccon->closed_cb = ctrl_conn_closed_cb; |
| 279 | pending->cmd = cmd; |
| 280 | |
| 281 | /* Setup the timeout */ |
| 282 | pending->timeout.data = pending; |
| 283 | pending->timeout.cb = pending_timeout_cb; |
| 284 | /* TODO: Make timeout configurable */ |
| 285 | osmo_timer_schedule(&pending->timeout, 10, 0); |
| 286 | llist_add_tail(&pending->list_entry, &bsc->cmd_pending); |
| 287 | |
| 288 | goto done; |
| 289 | } |
| 290 | } |
| 291 | /* We end up here if there's no bsc to handle our LAC */ |
| 292 | cmd->reply = "no BSC with this nr"; |
| 293 | err: |
| 294 | ret = CTRL_CMD_ERROR; |
| 295 | done: |
Holger Hans Peter Freyther | bc3780a | 2014-02-04 19:12:04 +0100 | [diff] [blame] | 296 | talloc_free(bsc_cmd); |
Holger Hans Peter Freyther | 98da544 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 297 | return ret; |
| 298 | |
| 299 | } |
| 300 | |
| 301 | |
| 302 | CTRL_CMD_DEFINE(fwd_cmd, "net 0 bsc *"); |
| 303 | static int get_fwd_cmd(struct ctrl_cmd *cmd, void *data) |
| 304 | { |
| 305 | return forward_to_bsc(cmd); |
| 306 | } |
| 307 | |
| 308 | static int set_fwd_cmd(struct ctrl_cmd *cmd, void *data) |
| 309 | { |
| 310 | return forward_to_bsc(cmd); |
| 311 | } |
| 312 | |
| 313 | static int verify_fwd_cmd(struct ctrl_cmd *cmd, const char *value, void *data) |
| 314 | { |
| 315 | return 0; |
| 316 | } |
| 317 | |
Holger Hans Peter Freyther | bc3780a | 2014-02-04 19:12:04 +0100 | [diff] [blame] | 318 | static int extract_bsc_cfg_variable(struct ctrl_cmd *cmd, struct bsc_config **cfg, |
| 319 | char **bsc_variable) |
| 320 | { |
| 321 | unsigned int nr; |
| 322 | |
| 323 | if (!extract_bsc_nr_variable(cmd->variable, &nr, bsc_variable)) { |
| 324 | cmd->reply = "command incomplete"; |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | *cfg = bsc_config_num(g_nat, nr); |
| 329 | if (!*cfg) { |
| 330 | cmd->reply = "Unknown BSC"; |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | return 1; |
| 335 | } |
| 336 | |
| 337 | CTRL_CMD_DEFINE(net_cfg_cmd, "net 0 bsc_cfg *"); |
| 338 | static int get_net_cfg_cmd(struct ctrl_cmd *cmd, void *data) |
| 339 | { |
| 340 | char *bsc_variable; |
| 341 | struct bsc_config *bsc_cfg; |
| 342 | |
| 343 | if (!extract_bsc_cfg_variable(cmd, &bsc_cfg, &bsc_variable)) |
| 344 | return CTRL_CMD_ERROR; |
| 345 | |
| 346 | if (strcmp(bsc_variable, "access-list-name") == 0) { |
| 347 | cmd->reply = talloc_asprintf(cmd, "%s", |
| 348 | bsc_cfg->acc_lst_name ? bsc_cfg->acc_lst_name : ""); |
| 349 | return CTRL_CMD_REPLY; |
| 350 | } |
| 351 | |
| 352 | cmd->reply = "unknown command"; |
| 353 | return CTRL_CMD_ERROR; |
| 354 | } |
| 355 | |
| 356 | static int set_net_cfg_cmd(struct ctrl_cmd *cmd, void *data) |
| 357 | { |
| 358 | char *bsc_variable; |
| 359 | struct bsc_config *bsc_cfg; |
| 360 | |
| 361 | if (!extract_bsc_cfg_variable(cmd, &bsc_cfg, &bsc_variable)) |
| 362 | return CTRL_CMD_ERROR; |
| 363 | |
| 364 | if (strcmp(bsc_variable, "access-list-name") == 0) { |
| 365 | bsc_replace_string(bsc_cfg, &bsc_cfg->acc_lst_name, cmd->value); |
| 366 | cmd->reply = talloc_asprintf(cmd, "%s", |
| 367 | bsc_cfg->acc_lst_name ? bsc_cfg->acc_lst_name : ""); |
| 368 | return CTRL_CMD_REPLY; |
Holger Hans Peter Freyther | 472f3bd | 2014-03-04 15:20:27 +0100 | [diff] [blame] | 369 | } else if (strcmp(bsc_variable, "no-access-list-name") == 0) { |
| 370 | talloc_free(bsc_cfg->acc_lst_name); |
| 371 | bsc_cfg->acc_lst_name = NULL; |
| 372 | cmd->reply = ""; |
| 373 | return CTRL_CMD_REPLY; |
Holger Hans Peter Freyther | bc3780a | 2014-02-04 19:12:04 +0100 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | cmd->reply = "unknown command"; |
| 377 | return CTRL_CMD_ERROR; |
| 378 | } |
| 379 | |
| 380 | static int verify_net_cfg_cmd(struct ctrl_cmd *cmd, const char *value, void *data) |
| 381 | { |
| 382 | return 0; |
| 383 | } |
| 384 | |
Holger Hans Peter Freyther | ab94ca1 | 2015-04-05 15:15:36 +0200 | [diff] [blame] | 385 | CTRL_CMD_DEFINE(net_cfg_acc_cmd, "net 0 add allow access-list *"); |
| 386 | static const char *extract_acc_name(const char *var) |
| 387 | { |
| 388 | char *str; |
| 389 | |
| 390 | str = strstr(var, "net.0.add.allow.access-list."); |
| 391 | if (!str) |
| 392 | return NULL; |
| 393 | str += strlen("net.0.add.allow.access-list."); |
| 394 | if (strlen(str) == 0) |
| 395 | return NULL; |
| 396 | return str; |
| 397 | } |
| 398 | |
| 399 | static int get_net_cfg_acc_cmd(struct ctrl_cmd *cmd, void *data) |
| 400 | { |
| 401 | cmd->reply = "Append only"; |
| 402 | return CTRL_CMD_ERROR; |
| 403 | } |
| 404 | |
| 405 | static int set_net_cfg_acc_cmd(struct ctrl_cmd *cmd, void *data) |
| 406 | { |
| 407 | const char *access_name = extract_acc_name(cmd->variable); |
Holger Hans Peter Freyther | a1e6bd6 | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 408 | struct bsc_msg_acc_lst *acc; |
| 409 | struct bsc_msg_acc_lst_entry *entry; |
Holger Hans Peter Freyther | ab94ca1 | 2015-04-05 15:15:36 +0200 | [diff] [blame] | 410 | const char *value = cmd->value; |
| 411 | int rc; |
| 412 | |
Holger Hans Peter Freyther | 08ea4d8 | 2015-04-07 09:08:32 +0200 | [diff] [blame] | 413 | /* Should have been caught by verify_net_cfg_acc_cmd */ |
Holger Hans Peter Freyther | a1e6bd6 | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 414 | acc = bsc_msg_acc_lst_find(&g_nat->access_lists, access_name); |
Holger Hans Peter Freyther | 08ea4d8 | 2015-04-07 09:08:32 +0200 | [diff] [blame] | 415 | if (!acc) { |
| 416 | cmd->reply = "Access list not found"; |
| 417 | return CTRL_CMD_ERROR; |
| 418 | } |
| 419 | |
Holger Hans Peter Freyther | a1e6bd6 | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 420 | entry = bsc_msg_acc_lst_entry_create(acc); |
Holger Hans Peter Freyther | ab94ca1 | 2015-04-05 15:15:36 +0200 | [diff] [blame] | 421 | if (!entry) { |
| 422 | cmd->reply = "OOM"; |
| 423 | return CTRL_CMD_ERROR; |
| 424 | } |
| 425 | |
| 426 | rc = gsm_parse_reg(acc, &entry->imsi_allow_re, &entry->imsi_allow, 1, &value); |
| 427 | if (rc != 0) { |
| 428 | cmd->reply = "Failed to compile expression"; |
| 429 | return CTRL_CMD_ERROR; |
| 430 | } |
| 431 | |
| 432 | cmd->reply = "IMSI allow added to access list"; |
| 433 | return CTRL_CMD_REPLY; |
| 434 | } |
| 435 | |
| 436 | static int verify_net_cfg_acc_cmd(struct ctrl_cmd *cmd, const char *value, void *data) |
| 437 | { |
| 438 | const char *access_name = extract_acc_name(cmd->variable); |
Holger Hans Peter Freyther | a1e6bd6 | 2015-04-04 22:40:12 +0200 | [diff] [blame] | 439 | struct bsc_msg_acc_lst *acc = bsc_msg_acc_lst_find(&g_nat->access_lists, access_name); |
Holger Hans Peter Freyther | ab94ca1 | 2015-04-05 15:15:36 +0200 | [diff] [blame] | 440 | |
| 441 | if (!acc) { |
| 442 | cmd->reply = "Access list not known"; |
| 443 | return -1; |
| 444 | } |
| 445 | |
| 446 | return 0; |
| 447 | } |
Holger Hans Peter Freyther | bc3780a | 2014-02-04 19:12:04 +0100 | [diff] [blame] | 448 | |
Holger Hans Peter Freyther | 8ccf06c | 2015-04-05 15:26:33 +0200 | [diff] [blame] | 449 | CTRL_CMD_DEFINE(net_save_cmd, "net 0 save-configuration"); |
| 450 | static int verify_net_save_cmd(struct ctrl_cmd *cmd, const char *v, void *d) |
| 451 | { |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static int set_net_save_cmd(struct ctrl_cmd *cmd, void *data) |
| 456 | { |
| 457 | int rc = osmo_vty_save_config_file(); |
| 458 | cmd->reply = talloc_asprintf(cmd, "%d", rc); |
| 459 | if (!cmd->reply) { |
| 460 | cmd->reply = "OOM"; |
| 461 | return CTRL_CMD_ERROR; |
| 462 | } |
| 463 | |
| 464 | return CTRL_CMD_REPLY; |
| 465 | } |
| 466 | |
| 467 | static int get_net_save_cmd(struct ctrl_cmd *cmd, void *data) |
| 468 | { |
| 469 | cmd->reply = "Write only attribute"; |
| 470 | return CTRL_CMD_ERROR; |
| 471 | } |
| 472 | |
Holger Hans Peter Freyther | 98da544 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 473 | struct ctrl_handle *bsc_nat_controlif_setup(struct bsc_nat *nat, int port) |
| 474 | { |
| 475 | struct ctrl_handle *ctrl; |
| 476 | int rc; |
| 477 | |
| 478 | |
Harald Welte | 55dc31e | 2014-08-24 17:54:49 +0200 | [diff] [blame] | 479 | ctrl = bsc_controlif_setup(NULL, OSMO_CTRL_PORT_BSC_NAT); |
Holger Hans Peter Freyther | 98da544 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 480 | if (!ctrl) { |
| 481 | fprintf(stderr, "Failed to initialize the control interface. Exiting.\n"); |
| 482 | return NULL; |
| 483 | } |
| 484 | |
| 485 | rc = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_fwd_cmd); |
| 486 | if (rc) { |
| 487 | fprintf(stderr, "Failed to install the control command. Exiting.\n"); |
Holger Hans Peter Freyther | bc3780a | 2014-02-04 19:12:04 +0100 | [diff] [blame] | 488 | goto error; |
| 489 | } |
| 490 | rc = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_net_cfg_cmd); |
| 491 | if (rc) { |
| 492 | fprintf(stderr, "Failed to install the net cfg command. Exiting.\n"); |
| 493 | goto error; |
Holger Hans Peter Freyther | 98da544 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 494 | } |
Holger Hans Peter Freyther | ab94ca1 | 2015-04-05 15:15:36 +0200 | [diff] [blame] | 495 | rc = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_net_cfg_acc_cmd); |
| 496 | if (rc) { |
| 497 | fprintf(stderr, "Failed to install the net acc command. Exiting.\n"); |
| 498 | goto error; |
| 499 | } |
Holger Hans Peter Freyther | 8ccf06c | 2015-04-05 15:26:33 +0200 | [diff] [blame] | 500 | rc = ctrl_cmd_install(CTRL_NODE_ROOT, &cmd_net_save_cmd); |
| 501 | if (rc) { |
| 502 | fprintf(stderr, "Failed to install the net save command. Exiting.\n"); |
| 503 | goto error; |
| 504 | } |
Holger Hans Peter Freyther | 98da544 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 505 | |
| 506 | g_nat = nat; |
| 507 | return ctrl; |
Holger Hans Peter Freyther | bc3780a | 2014-02-04 19:12:04 +0100 | [diff] [blame] | 508 | |
| 509 | error: |
| 510 | osmo_fd_unregister(&ctrl->listen_fd); |
| 511 | close(ctrl->listen_fd.fd); |
| 512 | talloc_free(ctrl); |
| 513 | return NULL; |
Holger Hans Peter Freyther | 98da544 | 2012-11-05 16:04:10 +0100 | [diff] [blame] | 514 | } |
| 515 | |
Holger Hans Peter Freyther | 7c00983 | 2015-04-05 13:45:53 +0200 | [diff] [blame] | 516 | void bsc_nat_inform_reject(struct bsc_connection *conn, const char *imsi) |
| 517 | { |
| 518 | struct ctrl_cmd *cmd; |
| 519 | |
| 520 | cmd = ctrl_cmd_create(conn, CTRL_TYPE_TRAP); |
| 521 | if (!cmd) { |
| 522 | LOGP(DCTRL, LOGL_ERROR, "Failed to create TRAP command.\n"); |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | cmd->id = "0"; |
| 527 | cmd->variable = talloc_asprintf(cmd, "net.0.bsc.%d.notification-rejection-v1", |
| 528 | conn->cfg->nr); |
| 529 | cmd->reply = talloc_asprintf(cmd, "imsi=%s", imsi); |
| 530 | |
| 531 | ctrl_cmd_send_to_all(conn->cfg->nat->ctrl, cmd); |
| 532 | talloc_free(cmd); |
| 533 | } |