Neels Hofmeyr | 87203f2 | 2017-10-31 01:15:45 +0100 | [diff] [blame] | 1 | /* MGCP client interface to quagga VTY */ |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 2 | /* (C) 2016 by sysmocom s.m.f.c. GmbH <info@sysmocom.de> |
| 3 | * Based on OpenBSC interface to quagga VTY (libmsc/vty_interface_layer3.c) |
| 4 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
| 5 | * (C) 2009-2011 by Holger Hans Peter Freyther |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 220f487 | 2018-02-04 09:04:16 +0100 | [diff] [blame] | 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 |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 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 |
Harald Welte | 220f487 | 2018-02-04 09:04:16 +0100 | [diff] [blame] | 16 | * GNU General Public License for more details. |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 17 | * |
Harald Welte | 220f487 | 2018-02-04 09:04:16 +0100 | [diff] [blame] | 18 | * You should have received a copy of the GNU General Public License |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <inttypes.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <talloc.h> |
| 26 | |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 27 | #include <osmocom/vty/vty.h> |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 28 | #include <osmocom/vty/command.h> |
Stefan Sperling | 2924825 | 2018-02-22 18:09:28 +0100 | [diff] [blame] | 29 | #include <osmocom/vty/misc.h> |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 30 | #include <osmocom/core/utils.h> |
| 31 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 32 | #include <osmocom/mgcp_client/mgcp_client.h> |
Philipp Maier | 3f2c15f | 2021-07-22 11:53:07 +0200 | [diff] [blame] | 33 | #include <osmocom/mgcp_client/mgcp_client_internal.h> |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 34 | #include <osmocom/mgcp_client/mgcp_client_pool_internal.h> |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 35 | |
Neels Hofmeyr | e6d8e91 | 2018-08-23 16:36:48 +0200 | [diff] [blame] | 36 | #define MGW_STR MGCP_CLIENT_MGW_STR |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 37 | |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 38 | /* Only common (non-pooled) VTY connands will use this talloc context. All |
| 39 | * pooled VTY commands will use the pool (global_mgcp_client_pool) as |
| 40 | * talloc context. */ |
| 41 | static void *global_mgcp_client_ctx = NULL; |
| 42 | |
| 43 | /* MGCP Client configuration used with mgcp_client_vty_init(). (This pointer |
| 44 | * points to user provided memory, so it cannot be used as talloc context.) */ |
| 45 | static struct mgcp_client_conf *global_mgcp_client_conf = NULL; |
| 46 | |
| 47 | /* Pointer to the MGCP pool that is managed by mgcp_client_pool_vty_init() */ |
| 48 | static struct mgcp_client_pool *global_mgcp_client_pool = NULL; |
| 49 | |
| 50 | struct mgcp_client_conf *get_mgcp_client_config(struct vty *vty) |
| 51 | { |
| 52 | if (global_mgcp_client_pool && vty->node == global_mgcp_client_pool->vty_node->node) |
| 53 | return vty->index; |
| 54 | else |
| 55 | return global_mgcp_client_conf; |
| 56 | } |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 57 | |
Neels Hofmeyr | 87203f2 | 2017-10-31 01:15:45 +0100 | [diff] [blame] | 58 | DEFUN(cfg_mgw_local_ip, cfg_mgw_local_ip_cmd, |
Pau Espin Pedrol | 531470a | 2020-08-31 17:06:55 +0200 | [diff] [blame] | 59 | "mgw local-ip " VTY_IPV46_CMD, |
Neels Hofmeyr | 54dd4b3 | 2017-10-31 01:18:33 +0100 | [diff] [blame] | 60 | MGW_STR "local bind to connect to MGW from\n" |
Pau Espin Pedrol | 531470a | 2020-08-31 17:06:55 +0200 | [diff] [blame] | 61 | "local bind IPv4 address\n" |
| 62 | "local bind IPv6 address\n") |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 63 | { |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 64 | struct mgcp_client_conf *conf = get_mgcp_client_config(vty); |
| 65 | |
Neels Hofmeyr | c3132fd | 2018-12-19 00:30:34 +0100 | [diff] [blame] | 66 | osmo_talloc_replace_string(global_mgcp_client_ctx, |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 67 | (char **)&conf->local_addr, |
Neels Hofmeyr | c3132fd | 2018-12-19 00:30:34 +0100 | [diff] [blame] | 68 | argv[0]); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 69 | return CMD_SUCCESS; |
| 70 | } |
Neels Hofmeyr | 87203f2 | 2017-10-31 01:15:45 +0100 | [diff] [blame] | 71 | ALIAS_DEPRECATED(cfg_mgw_local_ip, cfg_mgcpgw_local_ip_cmd, |
| 72 | "mgcpgw local-ip A.B.C.D", |
| 73 | MGW_STR "local bind to connect to MGCP gateway with\n" |
| 74 | "local bind IP address\n") |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 75 | |
Neels Hofmeyr | 87203f2 | 2017-10-31 01:15:45 +0100 | [diff] [blame] | 76 | DEFUN(cfg_mgw_local_port, cfg_mgw_local_port_cmd, |
| 77 | "mgw local-port <0-65535>", |
Neels Hofmeyr | 54dd4b3 | 2017-10-31 01:18:33 +0100 | [diff] [blame] | 78 | MGW_STR "local port to connect to MGW from\n" |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 79 | "local bind port\n") |
| 80 | { |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 81 | struct mgcp_client_conf *conf = get_mgcp_client_config(vty); |
| 82 | |
| 83 | conf->local_port = atoi(argv[0]); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 84 | return CMD_SUCCESS; |
| 85 | } |
Neels Hofmeyr | 87203f2 | 2017-10-31 01:15:45 +0100 | [diff] [blame] | 86 | ALIAS_DEPRECATED(cfg_mgw_local_port, cfg_mgcpgw_local_port_cmd, |
| 87 | "mgcpgw local-port <0-65535>", |
| 88 | MGW_STR "local bind to connect to MGCP gateway with\n" |
| 89 | "local bind port\n") |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 90 | |
Neels Hofmeyr | 87203f2 | 2017-10-31 01:15:45 +0100 | [diff] [blame] | 91 | DEFUN(cfg_mgw_remote_ip, cfg_mgw_remote_ip_cmd, |
Pau Espin Pedrol | 531470a | 2020-08-31 17:06:55 +0200 | [diff] [blame] | 92 | "mgw remote-ip " VTY_IPV46_CMD, |
Neels Hofmeyr | 54dd4b3 | 2017-10-31 01:18:33 +0100 | [diff] [blame] | 93 | MGW_STR "remote IP address to reach the MGW at\n" |
Pau Espin Pedrol | 531470a | 2020-08-31 17:06:55 +0200 | [diff] [blame] | 94 | "remote IPv4 address\n" |
| 95 | "remote IPv6 address\n") |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 96 | { |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 97 | struct mgcp_client_conf *conf = get_mgcp_client_config(vty); |
| 98 | |
Neels Hofmeyr | c3132fd | 2018-12-19 00:30:34 +0100 | [diff] [blame] | 99 | osmo_talloc_replace_string(global_mgcp_client_ctx, |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 100 | (char **)&conf->remote_addr, argv[0]); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 101 | return CMD_SUCCESS; |
| 102 | } |
Neels Hofmeyr | 87203f2 | 2017-10-31 01:15:45 +0100 | [diff] [blame] | 103 | ALIAS_DEPRECATED(cfg_mgw_remote_ip, cfg_mgcpgw_remote_ip_cmd, |
| 104 | "mgcpgw remote-ip A.B.C.D", |
| 105 | MGW_STR "remote bind to connect to MGCP gateway with\n" |
| 106 | "remote bind IP address\n") |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 107 | |
Neels Hofmeyr | 87203f2 | 2017-10-31 01:15:45 +0100 | [diff] [blame] | 108 | DEFUN(cfg_mgw_remote_port, cfg_mgw_remote_port_cmd, |
| 109 | "mgw remote-port <0-65535>", |
Neels Hofmeyr | 54dd4b3 | 2017-10-31 01:18:33 +0100 | [diff] [blame] | 110 | MGW_STR "remote port to reach the MGW at\n" |
| 111 | "remote port\n") |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 112 | { |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 113 | struct mgcp_client_conf *conf = get_mgcp_client_config(vty); |
| 114 | |
| 115 | conf->remote_port = atoi(argv[0]); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 116 | return CMD_SUCCESS; |
| 117 | } |
Neels Hofmeyr | 87203f2 | 2017-10-31 01:15:45 +0100 | [diff] [blame] | 118 | ALIAS_DEPRECATED(cfg_mgw_remote_port, cfg_mgcpgw_remote_port_cmd, |
| 119 | "mgcpgw remote-port <0-65535>", |
| 120 | MGW_STR "remote bind to connect to MGCP gateway with\n" |
| 121 | "remote bind port\n") |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 122 | |
Neels Hofmeyr | 72bc8da | 2018-12-19 00:44:37 +0100 | [diff] [blame] | 123 | DEFUN_DEPRECATED(cfg_mgw_endpoint_range, cfg_mgw_endpoint_range_cmd, |
Neels Hofmeyr | 87203f2 | 2017-10-31 01:15:45 +0100 | [diff] [blame] | 124 | "mgw endpoint-range <1-65534> <1-65534>", |
Neels Hofmeyr | 72bc8da | 2018-12-19 00:44:37 +0100 | [diff] [blame] | 125 | MGW_STR "DEPRECATED: the endpoint range cannot be defined by the client\n" |
| 126 | "-\n" "-\n") |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 127 | { |
Neels Hofmeyr | 72bc8da | 2018-12-19 00:44:37 +0100 | [diff] [blame] | 128 | vty_out(vty, "Please do not use legacy config 'mgw endpoint-range'" |
| 129 | " (the range can no longer be defined by the MGCP client)%s", |
| 130 | VTY_NEWLINE); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 131 | return CMD_SUCCESS; |
| 132 | } |
Neels Hofmeyr | 87203f2 | 2017-10-31 01:15:45 +0100 | [diff] [blame] | 133 | ALIAS_DEPRECATED(cfg_mgw_endpoint_range, cfg_mgcpgw_endpoint_range_cmd, |
| 134 | "mgcpgw endpoint-range <1-65534> <1-65534>", |
| 135 | MGW_STR "usable range of endpoint identifiers\n" |
| 136 | "set first useable endpoint identifier\n" |
| 137 | "set the last useable endpoint identifier\n") |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 138 | |
Neels Hofmeyr | c0dcc3c | 2017-12-02 18:31:34 +0000 | [diff] [blame] | 139 | #define BTS_START_STR "First UDP port allocated for the BTS side\n" |
| 140 | #define UDP_PORT_STR "UDP Port number\n" |
Neels Hofmeyr | 72bc8da | 2018-12-19 00:44:37 +0100 | [diff] [blame] | 141 | DEFUN_DEPRECATED(cfg_mgw_rtp_bts_base_port, |
Neels Hofmeyr | c0dcc3c | 2017-12-02 18:31:34 +0000 | [diff] [blame] | 142 | cfg_mgw_rtp_bts_base_port_cmd, |
| 143 | "mgw bts-base <0-65534>", |
| 144 | MGW_STR |
Neels Hofmeyr | 72bc8da | 2018-12-19 00:44:37 +0100 | [diff] [blame] | 145 | "DEPRECATED: there is no explicit BTS side in current osmo-mgw\n" "-\n") |
Neels Hofmeyr | c0dcc3c | 2017-12-02 18:31:34 +0000 | [diff] [blame] | 146 | { |
Neels Hofmeyr | 72bc8da | 2018-12-19 00:44:37 +0100 | [diff] [blame] | 147 | vty_out(vty, "Please do not use legacy config 'mgw bts-base'" |
| 148 | " (there is no explicit BTS side in an MGW anymore)%s", |
| 149 | VTY_NEWLINE); |
Neels Hofmeyr | c0dcc3c | 2017-12-02 18:31:34 +0000 | [diff] [blame] | 150 | return CMD_SUCCESS; |
| 151 | } |
| 152 | ALIAS_DEPRECATED(cfg_mgw_rtp_bts_base_port, |
| 153 | cfg_mgcpgw_rtp_bts_base_port_cmd, |
| 154 | "mgcpgw bts-base <0-65534>", |
| 155 | MGW_STR |
| 156 | BTS_START_STR |
| 157 | UDP_PORT_STR) |
| 158 | |
Neels Hofmeyr | ac69ea9 | 2018-12-19 00:27:50 +0100 | [diff] [blame] | 159 | DEFUN(cfg_mgw_endpoint_domain_name, |
| 160 | cfg_mgw_endpoint_domain_name_cmd, |
| 161 | "mgw endpoint-domain NAME", |
| 162 | MGW_STR "Set the domain name to send in MGCP messages, e.g. the part 'foo' in 'rtpbridge/*@foo'.\n" |
| 163 | "Domain name, should be alphanumeric.\n") |
| 164 | { |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 165 | struct mgcp_client_conf *conf = get_mgcp_client_config(vty); |
| 166 | |
| 167 | if (osmo_strlcpy(conf->endpoint_domain_name, argv[0], sizeof(conf->endpoint_domain_name)) |
| 168 | >= sizeof(conf->endpoint_domain_name)) { |
Neels Hofmeyr | ac69ea9 | 2018-12-19 00:27:50 +0100 | [diff] [blame] | 169 | vty_out(vty, "%% Error: 'mgw endpoint-domain' name too long, max length is %zu: '%s'%s", |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 170 | sizeof(conf->endpoint_domain_name) - 1, argv[0], VTY_NEWLINE); |
Neels Hofmeyr | ac69ea9 | 2018-12-19 00:27:50 +0100 | [diff] [blame] | 171 | return CMD_WARNING; |
| 172 | } |
| 173 | return CMD_SUCCESS; |
| 174 | } |
| 175 | |
Philipp Maier | 3f2c15f | 2021-07-22 11:53:07 +0200 | [diff] [blame] | 176 | DEFUN(cfg_mgw_reset_ep_name, |
| 177 | cfg_mgw_reset_ep_name_cmd, |
| 178 | "mgw reset-endpoint NAME", |
| 179 | MGW_STR "Add an endpoint name that should be reset (DLCX) on connect to the reset-endpoint list," |
| 180 | "e.g. 'rtpbridge/*'\n" |
| 181 | "Endpoint name, e.g. 'rtpbridge/*' or 'ds/e1-0/s-3/su16-4'.\n") |
| 182 | { |
| 183 | int rc; |
| 184 | struct reset_ep *reset_ep; |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 185 | struct mgcp_client_conf *conf = get_mgcp_client_config(vty); |
Philipp Maier | 3f2c15f | 2021-07-22 11:53:07 +0200 | [diff] [blame] | 186 | |
| 187 | /* stop when the address is already in the list */ |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 188 | llist_for_each_entry(reset_ep, &conf->reset_epnames, list) { |
Philipp Maier | 3f2c15f | 2021-07-22 11:53:07 +0200 | [diff] [blame] | 189 | if (strcmp(argv[0], reset_ep->name) == 0) { |
| 190 | vty_out(vty, "%% duplicate endpoint name configured ('%s')%s", argv[0], VTY_NEWLINE); |
| 191 | return CMD_WARNING; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /* the domain name is not part of the actual endpoint name */ |
| 196 | if (strchr(argv[0], '@')) { |
| 197 | vty_out(vty, "%% the endpoint name must be given without domain name ('%s')%s", |
| 198 | argv[0], VTY_NEWLINE); |
| 199 | return CMD_WARNING; |
| 200 | } |
| 201 | |
| 202 | reset_ep = talloc_zero(global_mgcp_client_ctx, struct reset_ep); |
| 203 | OSMO_ASSERT(reset_ep); |
| 204 | |
| 205 | rc = osmo_strlcpy(reset_ep->name, argv[0], sizeof(reset_ep->name)); |
| 206 | if (rc >= sizeof(reset_ep->name)) { |
| 207 | vty_out(vty, "%% Error: 'mgw reset-endpoint' name too long, max length is %zu: '%s'%s", |
| 208 | sizeof(reset_ep->name) - 1, argv[0], VTY_NEWLINE); |
| 209 | talloc_free(reset_ep); |
| 210 | return CMD_WARNING; |
| 211 | } |
| 212 | |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 213 | llist_add_tail(&reset_ep->list, &conf->reset_epnames); |
Philipp Maier | 3f2c15f | 2021-07-22 11:53:07 +0200 | [diff] [blame] | 214 | |
| 215 | return CMD_SUCCESS; |
| 216 | } |
| 217 | |
| 218 | DEFUN(cfg_mgw_no_reset_ep_name, |
| 219 | cfg_mgw_no_reset_ep_name_cmd, |
| 220 | "no mgw reset-endpoint NAME", |
Oliver Smith | 29e671f | 2021-08-18 08:44:47 +0200 | [diff] [blame] | 221 | NO_STR MGW_STR "remove an endpoint name from the reset-endpoint list, e.g. 'rtpbridge/*'\n" |
Philipp Maier | 3f2c15f | 2021-07-22 11:53:07 +0200 | [diff] [blame] | 222 | "Endpoint name, e.g. 'rtpbridge/*' or 'ds/e1-0/s-3/su16-4'.\n") |
| 223 | { |
| 224 | struct reset_ep *reset_ep; |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 225 | struct mgcp_client_conf *conf = get_mgcp_client_config(vty); |
Philipp Maier | 3f2c15f | 2021-07-22 11:53:07 +0200 | [diff] [blame] | 226 | |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 227 | llist_for_each_entry(reset_ep, &conf->reset_epnames, list) { |
Philipp Maier | 3f2c15f | 2021-07-22 11:53:07 +0200 | [diff] [blame] | 228 | if (strcmp(argv[0], reset_ep->name) == 0) { |
| 229 | llist_del(&reset_ep->list); |
| 230 | talloc_free(reset_ep); |
| 231 | return CMD_SUCCESS; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | vty_out(vty, "%% no such endpoint name configured ('%s')%s", argv[0], VTY_NEWLINE); |
| 236 | return CMD_WARNING; |
| 237 | } |
| 238 | |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 239 | static int config_write(struct vty *vty, const char *indent, struct mgcp_client_conf *conf) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 240 | { |
| 241 | const char *addr; |
| 242 | int port; |
Philipp Maier | 3f2c15f | 2021-07-22 11:53:07 +0200 | [diff] [blame] | 243 | struct reset_ep *reset_ep; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 244 | |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 245 | if (conf->description) |
| 246 | vty_out(vty, "%sdescription %s%s", indent, conf->description, VTY_NEWLINE); |
| 247 | |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 248 | addr = conf->local_addr; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 249 | if (addr) |
Neels Hofmeyr | 87203f2 | 2017-10-31 01:15:45 +0100 | [diff] [blame] | 250 | vty_out(vty, "%smgw local-ip %s%s", indent, addr, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 251 | VTY_NEWLINE); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 252 | port = conf->local_port; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 253 | if (port >= 0) |
Neels Hofmeyr | 87203f2 | 2017-10-31 01:15:45 +0100 | [diff] [blame] | 254 | vty_out(vty, "%smgw local-port %u%s", indent, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 255 | (uint16_t)port, VTY_NEWLINE); |
| 256 | |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 257 | addr = conf->remote_addr; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 258 | if (addr) |
Neels Hofmeyr | 87203f2 | 2017-10-31 01:15:45 +0100 | [diff] [blame] | 259 | vty_out(vty, "%smgw remote-ip %s%s", indent, addr, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 260 | VTY_NEWLINE); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 261 | port = conf->remote_port; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 262 | if (port >= 0) |
Neels Hofmeyr | 87203f2 | 2017-10-31 01:15:45 +0100 | [diff] [blame] | 263 | vty_out(vty, "%smgw remote-port %u%s", indent, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 264 | (uint16_t)port, VTY_NEWLINE); |
| 265 | |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 266 | if (conf->endpoint_domain_name[0]) |
Neels Hofmeyr | ac69ea9 | 2018-12-19 00:27:50 +0100 | [diff] [blame] | 267 | vty_out(vty, "%smgw endpoint-domain %s%s", indent, |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 268 | conf->endpoint_domain_name, VTY_NEWLINE); |
Neels Hofmeyr | ac69ea9 | 2018-12-19 00:27:50 +0100 | [diff] [blame] | 269 | |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 270 | llist_for_each_entry(reset_ep, &conf->reset_epnames, list) |
Philipp Maier | 3f2c15f | 2021-07-22 11:53:07 +0200 | [diff] [blame] | 271 | vty_out(vty, "%smgw reset-endpoint %s%s", indent, reset_ep->name, VTY_NEWLINE); |
| 272 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 273 | return CMD_SUCCESS; |
| 274 | } |
| 275 | |
Philipp Maier | d6a7e17 | 2021-08-03 16:03:27 +0200 | [diff] [blame] | 276 | /*! Write out MGCP client config to VTY. |
| 277 | * \param[in] vty VTY to which we should print. |
| 278 | * \param[in] string used for indentation (e.g. " "). |
| 279 | * \returns CMD_SUCCESS on success, CMD_WARNING on error */ |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 280 | int mgcp_client_config_write(struct vty *vty, const char *indent) |
| 281 | { |
| 282 | return config_write(vty, indent, global_mgcp_client_conf); |
| 283 | } |
| 284 | |
| 285 | static void vty_init_common(void *talloc_ctx, int node) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 286 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 287 | global_mgcp_client_ctx = talloc_ctx; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 288 | |
Vadim Yanitskiy | 3ba4095 | 2020-10-04 16:42:04 +0700 | [diff] [blame] | 289 | install_lib_element(node, &cfg_mgw_local_ip_cmd); |
| 290 | install_lib_element(node, &cfg_mgw_local_port_cmd); |
| 291 | install_lib_element(node, &cfg_mgw_remote_ip_cmd); |
| 292 | install_lib_element(node, &cfg_mgw_remote_port_cmd); |
| 293 | install_lib_element(node, &cfg_mgw_endpoint_range_cmd); |
| 294 | install_lib_element(node, &cfg_mgw_rtp_bts_base_port_cmd); |
| 295 | install_lib_element(node, &cfg_mgw_endpoint_domain_name_cmd); |
Philipp Maier | 3f2c15f | 2021-07-22 11:53:07 +0200 | [diff] [blame] | 296 | install_lib_element(node, &cfg_mgw_reset_ep_name_cmd); |
| 297 | install_lib_element(node, &cfg_mgw_no_reset_ep_name_cmd); |
Neels Hofmeyr | 87203f2 | 2017-10-31 01:15:45 +0100 | [diff] [blame] | 298 | |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 299 | osmo_fsm_vty_add_cmds(); |
| 300 | } |
| 301 | |
Philipp Maier | d6a7e17 | 2021-08-03 16:03:27 +0200 | [diff] [blame] | 302 | /*! Set up MGCP client VTY |
| 303 | * (called once at startup by the application process). |
| 304 | * \param[in] talloc_ctx talloc context to be used by the VTY for allocating memory. |
| 305 | * \param[in] node identifier of the node on which the VTY commands should be installed. |
| 306 | * \param[in] conf user provided memory to to store the MGCP client configuration data. */ |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 307 | void mgcp_client_vty_init(void *talloc_ctx, int node, struct mgcp_client_conf *conf) |
| 308 | { |
| 309 | global_mgcp_client_conf = conf; |
| 310 | |
Neels Hofmeyr | 87203f2 | 2017-10-31 01:15:45 +0100 | [diff] [blame] | 311 | /* deprecated 'mgcpgw' commands */ |
Vadim Yanitskiy | 3ba4095 | 2020-10-04 16:42:04 +0700 | [diff] [blame] | 312 | install_lib_element(node, &cfg_mgcpgw_local_ip_cmd); |
| 313 | install_lib_element(node, &cfg_mgcpgw_local_port_cmd); |
| 314 | install_lib_element(node, &cfg_mgcpgw_remote_ip_cmd); |
| 315 | install_lib_element(node, &cfg_mgcpgw_remote_port_cmd); |
| 316 | install_lib_element(node, &cfg_mgcpgw_endpoint_range_cmd); |
| 317 | install_lib_element(node, &cfg_mgcpgw_rtp_bts_base_port_cmd); |
Stefan Sperling | 2924825 | 2018-02-22 18:09:28 +0100 | [diff] [blame] | 318 | |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 319 | vty_init_common(talloc_ctx, node); |
| 320 | } |
| 321 | |
| 322 | static int config_write_pool(struct vty *vty) |
| 323 | { |
| 324 | struct mgcp_client_pool *pool = global_mgcp_client_pool; |
| 325 | struct mgcp_client_pool_member *pool_member; |
| 326 | unsigned int indent_buf_len = strlen(pool->vty_indent) + 1 + 1; |
| 327 | char *indent = talloc_zero_size(vty, indent_buf_len); |
| 328 | |
| 329 | snprintf(indent, indent_buf_len, "%s ", pool->vty_indent); |
| 330 | |
| 331 | llist_for_each_entry(pool_member, &pool->pool, list) { |
| 332 | vty_out(vty, "%smgw %u%s", pool->vty_indent, pool_member->nr, VTY_NEWLINE); |
| 333 | config_write(vty, indent, &pool_member->conf); |
| 334 | } |
| 335 | |
| 336 | talloc_free(indent); |
| 337 | return CMD_SUCCESS; |
| 338 | } |
| 339 | |
| 340 | /* Lookup the selected MGCP client config by its reference number */ |
| 341 | static struct mgcp_client_pool_member *pool_member_by_nr(unsigned int nr) |
| 342 | { |
| 343 | struct mgcp_client_pool_member *pool_member = NULL; |
| 344 | struct mgcp_client_pool_member *pool_member_tmp; |
| 345 | |
| 346 | llist_for_each_entry(pool_member_tmp, &global_mgcp_client_pool->pool, list) { |
| 347 | if (pool_member_tmp->nr == nr) { |
| 348 | pool_member = pool_member_tmp; |
| 349 | break; |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | return pool_member; |
| 354 | } |
| 355 | |
| 356 | DEFUN_ATTR(cfg_mgw, |
Philipp Maier | d55be05 | 2021-08-19 14:58:31 +0200 | [diff] [blame] | 357 | cfg_mgw_cmd, "mgw <0-255>", "Select a MGCP client config to setup\n" "reference number\n", CMD_ATTR_IMMEDIATE) |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 358 | { |
| 359 | int nr = atoi(argv[0]); |
| 360 | struct mgcp_client_pool_member *pool_member; |
| 361 | |
| 362 | pool_member = pool_member_by_nr(nr); |
| 363 | if (!pool_member) { |
| 364 | pool_member = talloc_zero(global_mgcp_client_pool, struct mgcp_client_pool_member); |
| 365 | OSMO_ASSERT(pool_member); |
| 366 | mgcp_client_conf_init(&pool_member->conf); |
| 367 | pool_member->nr = nr; |
| 368 | llist_add_tail(&pool_member->list, &global_mgcp_client_pool->pool); |
| 369 | } |
| 370 | |
| 371 | vty->index = &pool_member->conf; |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 372 | vty->index_sub = &pool_member->conf.description; |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 373 | vty->node = global_mgcp_client_pool->vty_node->node; |
| 374 | |
| 375 | return CMD_SUCCESS; |
| 376 | } |
| 377 | |
| 378 | DEFUN_ATTR(cfg_no_mgw, |
| 379 | cfg_no_mgw_cmd, |
Philipp Maier | 74e83d3 | 2021-08-20 09:57:27 +0200 | [diff] [blame] | 380 | "no mgw <0-255>", NO_STR "Select a MGCP client config to remove\n" "reference number\n", CMD_ATTR_IMMEDIATE) |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 381 | { |
| 382 | int nr = atoi(argv[0]); |
| 383 | struct mgcp_client_pool_member *pool_member; |
| 384 | |
| 385 | pool_member = pool_member_by_nr(nr); |
| 386 | if (!pool_member) { |
| 387 | vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE); |
| 388 | return CMD_WARNING; |
| 389 | } |
| 390 | |
| 391 | /* Make sure that there are no ongoing calls */ |
| 392 | if (pool_member->refcount > 0) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 393 | vty_out(vty, "%% MGCP client (MGW %s) is still serving ongoing calls -- can't remove it now!%s", |
| 394 | mgcp_client_pool_member_name(pool_member), VTY_NEWLINE); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 395 | return CMD_WARNING; |
| 396 | } |
| 397 | |
| 398 | llist_del(&pool_member->list); |
| 399 | if (pool_member->client) { |
| 400 | mgcp_client_disconnect(pool_member->client); |
| 401 | talloc_free(pool_member->client); |
| 402 | } |
| 403 | talloc_free(pool_member); |
| 404 | |
| 405 | return CMD_SUCCESS; |
| 406 | } |
| 407 | |
| 408 | DEFUN_ATTR(mgw_reconnect, mgw_reconnect_cmd, |
| 409 | "mgw <0-255> reconnect", |
Philipp Maier | 8f91a7f | 2021-08-19 14:55:56 +0200 | [diff] [blame] | 410 | MGW_STR "reference number\n" "reconfigure and reconnect MGCP client\n", CMD_ATTR_IMMEDIATE) |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 411 | { |
| 412 | int nr = atoi(argv[0]); |
| 413 | struct mgcp_client_pool_member *pool_member = NULL; |
| 414 | |
| 415 | pool_member = pool_member_by_nr(nr); |
| 416 | if (!pool_member) { |
| 417 | vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE); |
| 418 | return CMD_WARNING; |
| 419 | } |
| 420 | |
| 421 | /* Make sure that there are no ongoing calls */ |
| 422 | if (pool_member->refcount > 0) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 423 | vty_out(vty, "%% MGCP client (MGW %s) is still serving ongoing calls -- can't reconnect it now!%s", |
| 424 | mgcp_client_pool_member_name(pool_member), VTY_NEWLINE); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 425 | return CMD_WARNING; |
| 426 | } |
| 427 | |
| 428 | /* Get rid of a possibly existing old MGCP client instance first */ |
| 429 | if (pool_member->client) { |
| 430 | mgcp_client_disconnect(pool_member->client); |
| 431 | talloc_free(pool_member->client); |
| 432 | } |
| 433 | |
| 434 | /* Create a new MGCP client instance with the current config */ |
| 435 | pool_member->client = mgcp_client_init(pool_member, &pool_member->conf); |
| 436 | if (!pool_member->client) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 437 | LOGP(DLMGCP, LOGL_ERROR, "(manual) MGW %s initalization failed\n", |
| 438 | mgcp_client_pool_member_name(pool_member)); |
| 439 | vty_out(vty, "%% MGCP client (MGW %s) initalization failed ('%s')%s", |
| 440 | mgcp_client_pool_member_name(pool_member), argv[0], VTY_NEWLINE); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 441 | return CMD_WARNING; |
| 442 | } |
| 443 | |
| 444 | /* Set backpointer so that we can detect later that this MGCP client is managed by this pool. */ |
| 445 | pool_member->client->pool = global_mgcp_client_pool; |
| 446 | |
| 447 | /* Connect client */ |
| 448 | if (mgcp_client_connect(pool_member->client)) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 449 | LOGP(DLMGCP, LOGL_ERROR, "(manual) MGW %s connect failed at (%s:%u)\n", |
| 450 | mgcp_client_pool_member_name(pool_member), pool_member->conf.remote_addr, |
| 451 | pool_member->conf.remote_port); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 452 | talloc_free(pool_member->client); |
| 453 | pool_member->client = NULL; |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 454 | vty_out(vty, "%% MGCP client (MGW %s) initalization failed ('%s')%s", |
| 455 | mgcp_client_pool_member_name(pool_member), argv[0], VTY_NEWLINE); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 456 | return CMD_WARNING; |
| 457 | } |
| 458 | |
| 459 | return CMD_SUCCESS; |
| 460 | } |
| 461 | |
| 462 | DEFUN_ATTR(mgw_block, mgw_block_cmd, |
| 463 | "mgw <0-255> block", |
Philipp Maier | 8f91a7f | 2021-08-19 14:55:56 +0200 | [diff] [blame] | 464 | MGW_STR "reference number\n" "block MGCP client so that it won't be used for new calls\n", CMD_ATTR_IMMEDIATE) |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 465 | { |
| 466 | int nr = atoi(argv[0]); |
| 467 | struct mgcp_client_pool_member *pool_member = NULL; |
| 468 | |
| 469 | pool_member = pool_member_by_nr(nr); |
| 470 | if (!pool_member) { |
| 471 | vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE); |
| 472 | return CMD_WARNING; |
| 473 | } |
| 474 | |
| 475 | pool_member->blocked = true; |
| 476 | return CMD_SUCCESS; |
| 477 | } |
| 478 | |
| 479 | DEFUN_ATTR(mgw_unblock, mgw_unblock_cmd, |
| 480 | "mgw <0-255> unblock", |
Philipp Maier | 8f91a7f | 2021-08-19 14:55:56 +0200 | [diff] [blame] | 481 | MGW_STR "reference number\n" "unblock MGCP client so that it will be available for new calls\n", CMD_ATTR_IMMEDIATE) |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 482 | { |
| 483 | int nr = atoi(argv[0]); |
| 484 | struct mgcp_client_pool_member *pool_member = NULL; |
| 485 | |
| 486 | pool_member = pool_member_by_nr(nr); |
| 487 | if (!pool_member) { |
| 488 | vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE); |
| 489 | return CMD_WARNING; |
| 490 | } |
| 491 | |
| 492 | pool_member->blocked = false; |
| 493 | return CMD_SUCCESS; |
| 494 | } |
| 495 | |
| 496 | DEFUN(mgw_show, mgw_snow_cmd, "show mgw-pool", SHOW_STR "Display information about the MGW-Pool\n") |
| 497 | { |
| 498 | vty_out(vty, "%% MGW-Pool:%s", VTY_NEWLINE); |
| 499 | struct mgcp_client_pool_member *pool_member; |
| 500 | |
| 501 | if (llist_empty(&global_mgcp_client_pool->pool) && global_mgcp_client_pool->mgcp_client_single) { |
| 502 | vty_out(vty, "%% (pool is empty, single MGCP client will be used)%s", VTY_NEWLINE); |
| 503 | return CMD_SUCCESS; |
| 504 | } else if (llist_empty(&global_mgcp_client_pool->pool)) { |
| 505 | vty_out(vty, "%% (pool is empty)%s", VTY_NEWLINE); |
| 506 | return CMD_SUCCESS; |
| 507 | } |
| 508 | |
| 509 | llist_for_each_entry(pool_member, &global_mgcp_client_pool->pool, list) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 510 | vty_out(vty, "%% MGW %s%s", mgcp_client_pool_member_name(pool_member), VTY_NEWLINE); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 511 | vty_out(vty, "%% mgcp-client: %s%s", pool_member->client ? "connected" : "disconnected", |
| 512 | VTY_NEWLINE); |
| 513 | vty_out(vty, "%% service: %s%s", pool_member->blocked ? "blocked" : "unblocked", VTY_NEWLINE); |
| 514 | vty_out(vty, "%% ongoing calls: %u%s", pool_member->refcount, VTY_NEWLINE); |
| 515 | } |
| 516 | return CMD_SUCCESS; |
| 517 | } |
| 518 | |
| 519 | /*! Set up MGCP client VTY (pooled) |
| 520 | * (called once at startup by the application process). |
| 521 | * \param[in] parent_node identifier of the parent node on which the mgw node appears. |
| 522 | * \param[in] mgw_node identifier that should be used with the newly installed MGW node. |
| 523 | * \param[in] indent indentation string to match the indentation in the VTY config |
| 524 | * \param[in] pool user provided memory to store the configured MGCP client (MGW) pool. */ |
| 525 | void mgcp_client_pool_vty_init(int parent_node, int mgw_node, const char *indent, struct mgcp_client_pool *pool) |
| 526 | { |
Philipp Maier | da3a575 | 2021-08-19 14:24:14 +0200 | [diff] [blame] | 527 | /* A pool must be allocated before this function can be called */ |
| 528 | OSMO_ASSERT(pool); |
| 529 | |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 530 | /* Never allow this function to be called twice on the same pool */ |
| 531 | OSMO_ASSERT(!pool->vty_indent); |
| 532 | OSMO_ASSERT(!pool->vty_node); |
| 533 | |
| 534 | pool->vty_indent = talloc_strdup(pool, indent); |
| 535 | OSMO_ASSERT(pool->vty_indent); |
| 536 | pool->vty_node = talloc_zero(pool, struct cmd_node); |
| 537 | OSMO_ASSERT(pool->vty_node); |
| 538 | pool->vty_node->node = mgw_node; |
| 539 | pool->vty_node->vtysh = 1; |
| 540 | pool->vty_node->prompt = talloc_strdup(pool->vty_node, "%s(config-mgw)# "); |
| 541 | |
| 542 | install_lib_element(parent_node, &cfg_mgw_cmd); |
| 543 | install_lib_element(parent_node, &cfg_no_mgw_cmd); |
| 544 | |
| 545 | install_node(pool->vty_node, config_write_pool); |
| 546 | vty_init_common(pool, mgw_node); |
| 547 | |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 548 | install_element(mgw_node, &cfg_description_cmd); |
| 549 | |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 550 | install_lib_element(ENABLE_NODE, &mgw_reconnect_cmd); |
| 551 | install_lib_element(ENABLE_NODE, &mgw_block_cmd); |
| 552 | install_lib_element(ENABLE_NODE, &mgw_unblock_cmd); |
| 553 | |
| 554 | install_lib_element_ve(&mgw_snow_cmd); |
| 555 | |
| 556 | global_mgcp_client_pool = pool; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 557 | } |