blob: 704203c457f21e2c7592c4e16895ccc3e571f1d7 [file] [log] [blame]
Neels Hofmeyr87203f22017-10-31 01:15:45 +01001/* MGCP client interface to quagga VTY */
Neels Hofmeyre9920f22017-07-10 15:07:22 +02002/* (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 Welte220f4872018-02-04 09:04:16 +01009 * 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 Hofmeyre9920f22017-07-10 15:07:22 +020011 * (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 Welte220f4872018-02-04 09:04:16 +010016 * GNU General Public License for more details.
Neels Hofmeyre9920f22017-07-10 15:07:22 +020017 *
Harald Welte220f4872018-02-04 09:04:16 +010018 * You should have received a copy of the GNU General Public License
Neels Hofmeyre9920f22017-07-10 15:07:22 +020019 * 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 Maier3f4a4cb2021-07-26 13:20:05 +020027#include <osmocom/vty/vty.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020028#include <osmocom/vty/command.h>
Stefan Sperling29248252018-02-22 18:09:28 +010029#include <osmocom/vty/misc.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020030#include <osmocom/core/utils.h>
31
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020032#include <osmocom/mgcp_client/mgcp_client.h>
Philipp Maier3f2c15f2021-07-22 11:53:07 +020033#include <osmocom/mgcp_client/mgcp_client_internal.h>
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020034#include <osmocom/mgcp_client/mgcp_client_pool_internal.h>
Pau Espin Pedrol5d8b5b02022-10-13 17:52:40 +020035#include <osmocom/mgcp_client/mgcp_client_pool.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020036
Neels Hofmeyre6d8e912018-08-23 16:36:48 +020037#define MGW_STR MGCP_CLIENT_MGW_STR
Neels Hofmeyre9920f22017-07-10 15:07:22 +020038
Pau Espin Pedrolf6333022022-10-13 13:20:10 +020039/* Only common (non-pooled) VTY commands will use this talloc context. All
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020040 * pooled VTY commands will use the pool (global_mgcp_client_pool) as
41 * talloc context. */
42static void *global_mgcp_client_ctx = NULL;
43
44/* MGCP Client configuration used with mgcp_client_vty_init(). (This pointer
45 * points to user provided memory, so it cannot be used as talloc context.) */
46static struct mgcp_client_conf *global_mgcp_client_conf = NULL;
47
48/* Pointer to the MGCP pool that is managed by mgcp_client_pool_vty_init() */
49static struct mgcp_client_pool *global_mgcp_client_pool = NULL;
50
51struct mgcp_client_conf *get_mgcp_client_config(struct vty *vty)
52{
53 if (global_mgcp_client_pool && vty->node == global_mgcp_client_pool->vty_node->node)
54 return vty->index;
Pau Espin Pedrol80dd5762022-10-17 17:26:43 +020055
56 /* Global single MGCP config, deprecated: */
57 vty_out(vty, "%% MGCP commands outside of 'mgw' nodes are deprecated. "
58 "You should consider reading User Manual and migrating to 'mgw' node.%s",
59 VTY_NEWLINE);
60
61 return global_mgcp_client_conf;
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020062}
Neels Hofmeyre9920f22017-07-10 15:07:22 +020063
Neels Hofmeyr87203f22017-10-31 01:15:45 +010064DEFUN(cfg_mgw_local_ip, cfg_mgw_local_ip_cmd,
Pau Espin Pedrol8c138fe2022-10-19 12:47:31 +020065 "local-ip " VTY_IPV46_CMD,
66 "local bind to connect to MGW from\n"
Pau Espin Pedrol531470a2020-08-31 17:06:55 +020067 "local bind IPv4 address\n"
68 "local bind IPv6 address\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +020069{
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020070 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
71
Neels Hofmeyrc3132fd2018-12-19 00:30:34 +010072 osmo_talloc_replace_string(global_mgcp_client_ctx,
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020073 (char **)&conf->local_addr,
Neels Hofmeyrc3132fd2018-12-19 00:30:34 +010074 argv[0]);
Neels Hofmeyre9920f22017-07-10 15:07:22 +020075 return CMD_SUCCESS;
76}
Neels Hofmeyr87203f22017-10-31 01:15:45 +010077ALIAS_DEPRECATED(cfg_mgw_local_ip, cfg_mgcpgw_local_ip_cmd,
78 "mgcpgw local-ip A.B.C.D",
79 MGW_STR "local bind to connect to MGCP gateway with\n"
80 "local bind IP address\n")
Pau Espin Pedrol8c138fe2022-10-19 12:47:31 +020081ALIAS_DEPRECATED(cfg_mgw_local_ip,
82 cfg_mgw_mgw_local_ip_cmd,
83 "mgw local-ip " VTY_IPV46_CMD,
84 MGW_STR "local bind to connect to MGW from\n"
85 "local bind IPv4 address\n"
86 "local bind IPv6 address\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +020087
Neels Hofmeyr87203f22017-10-31 01:15:45 +010088DEFUN(cfg_mgw_local_port, cfg_mgw_local_port_cmd,
Pau Espin Pedrol8c138fe2022-10-19 12:47:31 +020089 "local-port <0-65535>",
90 "local port to connect to MGW from\n"
Neels Hofmeyre9920f22017-07-10 15:07:22 +020091 "local bind port\n")
92{
Philipp Maier3f4a4cb2021-07-26 13:20:05 +020093 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
94
95 conf->local_port = atoi(argv[0]);
Neels Hofmeyre9920f22017-07-10 15:07:22 +020096 return CMD_SUCCESS;
97}
Neels Hofmeyr87203f22017-10-31 01:15:45 +010098ALIAS_DEPRECATED(cfg_mgw_local_port, cfg_mgcpgw_local_port_cmd,
99 "mgcpgw local-port <0-65535>",
100 MGW_STR "local bind to connect to MGCP gateway with\n"
101 "local bind port\n")
Pau Espin Pedrol8c138fe2022-10-19 12:47:31 +0200102ALIAS_DEPRECATED(cfg_mgw_local_port,
103 cfg_mgw_mgw_local_port_cmd,
104 "mgw local-port <0-65535>",
105 MGW_STR "local port to connect to MGW from\n"
106 "local bind port\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200107
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100108DEFUN(cfg_mgw_remote_ip, cfg_mgw_remote_ip_cmd,
Pau Espin Pedrol8c138fe2022-10-19 12:47:31 +0200109 "remote-ip " VTY_IPV46_CMD,
110 "remote IP address to reach the MGW at\n"
Pau Espin Pedrol531470a2020-08-31 17:06:55 +0200111 "remote IPv4 address\n"
112 "remote IPv6 address\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200113{
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200114 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
115
Neels Hofmeyrc3132fd2018-12-19 00:30:34 +0100116 osmo_talloc_replace_string(global_mgcp_client_ctx,
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200117 (char **)&conf->remote_addr, argv[0]);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200118 return CMD_SUCCESS;
119}
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100120ALIAS_DEPRECATED(cfg_mgw_remote_ip, cfg_mgcpgw_remote_ip_cmd,
121 "mgcpgw remote-ip A.B.C.D",
122 MGW_STR "remote bind to connect to MGCP gateway with\n"
123 "remote bind IP address\n")
Pau Espin Pedrol8c138fe2022-10-19 12:47:31 +0200124ALIAS_DEPRECATED(cfg_mgw_remote_ip,
125 cfg_mgw_mgw_remote_ip_cmd,
126 "mgw remote-ip " VTY_IPV46_CMD,
127 MGW_STR "remote IP address to reach the MGW at\n"
128 "remote IPv4 address\n"
129 "remote IPv6 address\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200130
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100131DEFUN(cfg_mgw_remote_port, cfg_mgw_remote_port_cmd,
Pau Espin Pedrol8c138fe2022-10-19 12:47:31 +0200132 "remote-port <0-65535>",
133 "remote port to reach the MGW at\n"
Neels Hofmeyr54dd4b32017-10-31 01:18:33 +0100134 "remote port\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200135{
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200136 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
137
138 conf->remote_port = atoi(argv[0]);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200139 return CMD_SUCCESS;
140}
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100141ALIAS_DEPRECATED(cfg_mgw_remote_port, cfg_mgcpgw_remote_port_cmd,
142 "mgcpgw remote-port <0-65535>",
143 MGW_STR "remote bind to connect to MGCP gateway with\n"
144 "remote bind port\n")
Pau Espin Pedrol8c138fe2022-10-19 12:47:31 +0200145ALIAS_DEPRECATED(cfg_mgw_remote_port,
146 cfg_mgw_mgw_remote_port_cmd,
147 "mgw remote-port <0-65535>",
148 MGW_STR "remote port to reach the MGW at\n"
149 "remote port\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200150
Pau Espin Pedrol2172f402022-10-19 16:13:01 +0200151DEFUN_DEPRECATED(cfg_mgw_mgw_endpoint_range, cfg_mgw_mgw_endpoint_range_cmd,
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100152 "mgw endpoint-range <1-65534> <1-65534>",
Neels Hofmeyr72bc8da2018-12-19 00:44:37 +0100153 MGW_STR "DEPRECATED: the endpoint range cannot be defined by the client\n"
154 "-\n" "-\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200155{
Neels Hofmeyr72bc8da2018-12-19 00:44:37 +0100156 vty_out(vty, "Please do not use legacy config 'mgw endpoint-range'"
157 " (the range can no longer be defined by the MGCP client)%s",
158 VTY_NEWLINE);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200159 return CMD_SUCCESS;
160}
Pau Espin Pedrol2172f402022-10-19 16:13:01 +0200161ALIAS_DEPRECATED(cfg_mgw_mgw_endpoint_range, cfg_mgcpgw_endpoint_range_cmd,
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100162 "mgcpgw endpoint-range <1-65534> <1-65534>",
163 MGW_STR "usable range of endpoint identifiers\n"
164 "set first useable endpoint identifier\n"
165 "set the last useable endpoint identifier\n")
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200166
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000167#define BTS_START_STR "First UDP port allocated for the BTS side\n"
168#define UDP_PORT_STR "UDP Port number\n"
Neels Hofmeyr72bc8da2018-12-19 00:44:37 +0100169DEFUN_DEPRECATED(cfg_mgw_rtp_bts_base_port,
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000170 cfg_mgw_rtp_bts_base_port_cmd,
171 "mgw bts-base <0-65534>",
172 MGW_STR
Neels Hofmeyr72bc8da2018-12-19 00:44:37 +0100173 "DEPRECATED: there is no explicit BTS side in current osmo-mgw\n" "-\n")
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000174{
Neels Hofmeyr72bc8da2018-12-19 00:44:37 +0100175 vty_out(vty, "Please do not use legacy config 'mgw bts-base'"
176 " (there is no explicit BTS side in an MGW anymore)%s",
177 VTY_NEWLINE);
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000178 return CMD_SUCCESS;
179}
180ALIAS_DEPRECATED(cfg_mgw_rtp_bts_base_port,
181 cfg_mgcpgw_rtp_bts_base_port_cmd,
182 "mgcpgw bts-base <0-65534>",
183 MGW_STR
184 BTS_START_STR
185 UDP_PORT_STR)
186
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100187DEFUN(cfg_mgw_endpoint_domain_name,
188 cfg_mgw_endpoint_domain_name_cmd,
Pau Espin Pedrol8c138fe2022-10-19 12:47:31 +0200189 "endpoint-domain NAME",
190 "Set the domain name to send in MGCP messages, e.g. the part 'foo' in 'rtpbridge/*@foo'.\n"
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100191 "Domain name, should be alphanumeric.\n")
192{
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200193 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
194
195 if (osmo_strlcpy(conf->endpoint_domain_name, argv[0], sizeof(conf->endpoint_domain_name))
196 >= sizeof(conf->endpoint_domain_name)) {
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100197 vty_out(vty, "%% Error: 'mgw endpoint-domain' name too long, max length is %zu: '%s'%s",
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200198 sizeof(conf->endpoint_domain_name) - 1, argv[0], VTY_NEWLINE);
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100199 return CMD_WARNING;
200 }
201 return CMD_SUCCESS;
202}
Pau Espin Pedrol8c138fe2022-10-19 12:47:31 +0200203ALIAS_DEPRECATED(cfg_mgw_endpoint_domain_name,
204 cfg_mgw_mgw_endpoint_domain_name_cmd,
205 "mgw endpoint-domain NAME",
206 MGW_STR "Set the domain name to send in MGCP messages, e.g. the part 'foo' in 'rtpbridge/*@foo'.\n"
207 "Domain name, should be alphanumeric.\n")
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100208
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200209DEFUN(cfg_mgw_reset_ep_name,
210 cfg_mgw_reset_ep_name_cmd,
Pau Espin Pedrol8c138fe2022-10-19 12:47:31 +0200211 "reset-endpoint NAME",
212 "Add an endpoint name that should be reset (DLCX) on connect to the reset-endpoint list,"
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200213 "e.g. 'rtpbridge/*'\n"
214 "Endpoint name, e.g. 'rtpbridge/*' or 'ds/e1-0/s-3/su16-4'.\n")
215{
216 int rc;
217 struct reset_ep *reset_ep;
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200218 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200219
220 /* stop when the address is already in the list */
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200221 llist_for_each_entry(reset_ep, &conf->reset_epnames, list) {
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200222 if (strcmp(argv[0], reset_ep->name) == 0) {
223 vty_out(vty, "%% duplicate endpoint name configured ('%s')%s", argv[0], VTY_NEWLINE);
224 return CMD_WARNING;
225 }
226 }
227
228 /* the domain name is not part of the actual endpoint name */
229 if (strchr(argv[0], '@')) {
230 vty_out(vty, "%% the endpoint name must be given without domain name ('%s')%s",
231 argv[0], VTY_NEWLINE);
232 return CMD_WARNING;
233 }
234
235 reset_ep = talloc_zero(global_mgcp_client_ctx, struct reset_ep);
236 OSMO_ASSERT(reset_ep);
237
238 rc = osmo_strlcpy(reset_ep->name, argv[0], sizeof(reset_ep->name));
239 if (rc >= sizeof(reset_ep->name)) {
240 vty_out(vty, "%% Error: 'mgw reset-endpoint' name too long, max length is %zu: '%s'%s",
241 sizeof(reset_ep->name) - 1, argv[0], VTY_NEWLINE);
242 talloc_free(reset_ep);
243 return CMD_WARNING;
244 }
245
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200246 llist_add_tail(&reset_ep->list, &conf->reset_epnames);
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200247
248 return CMD_SUCCESS;
249}
Pau Espin Pedrol8c138fe2022-10-19 12:47:31 +0200250ALIAS_DEPRECATED(cfg_mgw_reset_ep_name,
251 cfg_mgw_mgw_reset_ep_name_cmd,
252 "mgw reset-endpoint NAME",
253 MGW_STR "Add an endpoint name that should be reset (DLCX) on connect to the reset-endpoint list,"
254 "e.g. 'rtpbridge/*'\n"
255 "Endpoint name, e.g. 'rtpbridge/*' or 'ds/e1-0/s-3/su16-4'.\n")
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200256
257DEFUN(cfg_mgw_no_reset_ep_name,
258 cfg_mgw_no_reset_ep_name_cmd,
Pau Espin Pedrol8c138fe2022-10-19 12:47:31 +0200259 "no reset-endpoint NAME",
260 NO_STR "remove an endpoint name from the reset-endpoint list, e.g. 'rtpbridge/*'\n"
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200261 "Endpoint name, e.g. 'rtpbridge/*' or 'ds/e1-0/s-3/su16-4'.\n")
262{
263 struct reset_ep *reset_ep;
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200264 struct mgcp_client_conf *conf = get_mgcp_client_config(vty);
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200265
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200266 llist_for_each_entry(reset_ep, &conf->reset_epnames, list) {
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200267 if (strcmp(argv[0], reset_ep->name) == 0) {
268 llist_del(&reset_ep->list);
269 talloc_free(reset_ep);
270 return CMD_SUCCESS;
271 }
272 }
273
274 vty_out(vty, "%% no such endpoint name configured ('%s')%s", argv[0], VTY_NEWLINE);
275 return CMD_WARNING;
276}
Pau Espin Pedrol8c138fe2022-10-19 12:47:31 +0200277ALIAS_DEPRECATED(cfg_mgw_no_reset_ep_name,
278 cfg_mgw_mgw_no_reset_ep_name_cmd,
279 "no mgw reset-endpoint NAME",
280 NO_STR MGW_STR "remove an endpoint name from the reset-endpoint list, e.g. 'rtpbridge/*'\n"
281 "Endpoint name, e.g. 'rtpbridge/*' or 'ds/e1-0/s-3/su16-4'.\n")
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200282
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200283static int config_write(struct vty *vty, const char *indent, struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200284{
285 const char *addr;
286 int port;
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200287 struct reset_ep *reset_ep;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200288
Pau Espin Pedrol103e50a2022-10-19 17:22:46 +0200289 /* If caller doesn't the MGW pool API (mgcp_client_pool_vty_init was never called),
290 * then the "mgw" cmd prefix must be added since the old node always contained it.
291 */
292 const char *mgw_prefix = global_mgcp_client_pool ? "" : "mgw ";
293
294 if (conf->description) /* description never had "mgw" prefix even on old node: */
Philipp Maierdf9192e2021-09-03 17:50:51 +0200295 vty_out(vty, "%sdescription %s%s", indent, conf->description, VTY_NEWLINE);
296
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200297 addr = conf->local_addr;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200298 if (addr)
Pau Espin Pedrol103e50a2022-10-19 17:22:46 +0200299 vty_out(vty, "%s%slocal-ip %s%s", indent, mgw_prefix, addr, VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200300 port = conf->local_port;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200301 if (port >= 0)
Pau Espin Pedrol103e50a2022-10-19 17:22:46 +0200302 vty_out(vty, "%s%slocal-port %u%s", indent, mgw_prefix,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200303 (uint16_t)port, VTY_NEWLINE);
304
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200305 addr = conf->remote_addr;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200306 if (addr)
Pau Espin Pedrol103e50a2022-10-19 17:22:46 +0200307 vty_out(vty, "%s%sremote-ip %s%s", indent, mgw_prefix, addr, VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200308 port = conf->remote_port;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200309 if (port >= 0)
Pau Espin Pedrol103e50a2022-10-19 17:22:46 +0200310 vty_out(vty, "%s%sremote-port %u%s", indent, mgw_prefix,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200311 (uint16_t)port, VTY_NEWLINE);
312
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200313 if (conf->endpoint_domain_name[0])
Pau Espin Pedrol103e50a2022-10-19 17:22:46 +0200314 vty_out(vty, "%s%sendpoint-domain %s%s", indent, mgw_prefix,
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200315 conf->endpoint_domain_name, VTY_NEWLINE);
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100316
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200317 llist_for_each_entry(reset_ep, &conf->reset_epnames, list)
Pau Espin Pedrol103e50a2022-10-19 17:22:46 +0200318 vty_out(vty, "%s%sreset-endpoint %s%s", indent, mgw_prefix, reset_ep->name, VTY_NEWLINE);
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200319
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200320 return CMD_SUCCESS;
321}
322
Philipp Maierd6a7e172021-08-03 16:03:27 +0200323/*! Write out MGCP client config to VTY.
324 * \param[in] vty VTY to which we should print.
325 * \param[in] string used for indentation (e.g. " ").
326 * \returns CMD_SUCCESS on success, CMD_WARNING on error */
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200327int mgcp_client_config_write(struct vty *vty, const char *indent)
328{
Pau Espin Pedrold17a9de2022-10-18 12:34:20 +0200329 /* If caller supports MGW pool API (mgcp_client_pool_vty_init was
330 * called), then skip printing any config in this node and print it when
331 * the whole 'mgw' node is printed. */
332 if (global_mgcp_client_pool)
333 return CMD_SUCCESS;
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200334 return config_write(vty, indent, global_mgcp_client_conf);
335}
336
337static void vty_init_common(void *talloc_ctx, int node)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200338{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200339 global_mgcp_client_ctx = talloc_ctx;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200340
Pau Espin Pedrol8c138fe2022-10-19 12:47:31 +0200341 /* deprecated 'mgw' commands ('mgw' prepended as first arg) */
342 install_lib_element(node, &cfg_mgw_mgw_local_ip_cmd);
343 install_lib_element(node, &cfg_mgw_mgw_local_port_cmd);
344 install_lib_element(node, &cfg_mgw_mgw_remote_ip_cmd);
345 install_lib_element(node, &cfg_mgw_mgw_remote_port_cmd);
Pau Espin Pedrol2172f402022-10-19 16:13:01 +0200346 install_lib_element(node, &cfg_mgw_mgw_endpoint_range_cmd);
Pau Espin Pedrol8c138fe2022-10-19 12:47:31 +0200347 install_lib_element(node, &cfg_mgw_mgw_endpoint_domain_name_cmd);
348 install_lib_element(node, &cfg_mgw_mgw_reset_ep_name_cmd);
349 install_lib_element(node, &cfg_mgw_mgw_no_reset_ep_name_cmd);
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100350
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200351 osmo_fsm_vty_add_cmds();
352}
353
Philipp Maierd6a7e172021-08-03 16:03:27 +0200354/*! Set up MGCP client VTY
355 * (called once at startup by the application process).
356 * \param[in] talloc_ctx talloc context to be used by the VTY for allocating memory.
357 * \param[in] node identifier of the node on which the VTY commands should be installed.
358 * \param[in] conf user provided memory to to store the MGCP client configuration data. */
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200359void mgcp_client_vty_init(void *talloc_ctx, int node, struct mgcp_client_conf *conf)
360{
361 global_mgcp_client_conf = conf;
362
Neels Hofmeyr87203f22017-10-31 01:15:45 +0100363 /* deprecated 'mgcpgw' commands */
Vadim Yanitskiy3ba40952020-10-04 16:42:04 +0700364 install_lib_element(node, &cfg_mgcpgw_local_ip_cmd);
365 install_lib_element(node, &cfg_mgcpgw_local_port_cmd);
366 install_lib_element(node, &cfg_mgcpgw_remote_ip_cmd);
367 install_lib_element(node, &cfg_mgcpgw_remote_port_cmd);
368 install_lib_element(node, &cfg_mgcpgw_endpoint_range_cmd);
369 install_lib_element(node, &cfg_mgcpgw_rtp_bts_base_port_cmd);
Stefan Sperling29248252018-02-22 18:09:28 +0100370
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200371 vty_init_common(talloc_ctx, node);
372}
373
Pau Espin Pedrol3a914e32022-10-19 17:13:20 +0200374/* Mark whether user called mgcp_client_pool_config_write() and hence support new API */
Pau Espin Pedrol7881f7d2022-10-18 14:13:04 +0200375static bool mgcp_client_pool_config_write_called = false;
Pau Espin Pedrol7881f7d2022-10-18 14:13:04 +0200376
Pau Espin Pedrol3a914e32022-10-19 17:13:20 +0200377static int _mgcp_client_pool_config_write(struct vty *vty, const char *indent)
Pau Espin Pedrol7881f7d2022-10-18 14:13:04 +0200378{
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200379 struct mgcp_client_pool *pool = global_mgcp_client_pool;
380 struct mgcp_client_pool_member *pool_member;
Pau Espin Pedrol7881f7d2022-10-18 14:13:04 +0200381 unsigned int subindent_buf_len;
382 char *subindent;
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200383
Pau Espin Pedrol7881f7d2022-10-18 14:13:04 +0200384 if (!indent)
385 indent = pool->vty_indent ? : "";
386 subindent_buf_len = strlen(indent) + 1 + 1;
387 subindent = talloc_zero_size(vty, subindent_buf_len);
388
389 snprintf(subindent, subindent_buf_len, "%s ", indent);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200390
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +0200391 llist_for_each_entry(pool_member, &pool->member_list, list) {
Pau Espin Pedrol7881f7d2022-10-18 14:13:04 +0200392 vty_out(vty, "%smgw %u%s", indent, pool_member->nr, VTY_NEWLINE);
393 config_write(vty, subindent, &pool_member->conf);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200394 }
395
Pau Espin Pedrold17a9de2022-10-18 12:34:20 +0200396 /* MGW pool API is supported by user (global_mgcp_client_pool is set
397 * because mgcp_client_pool_vty_init was called). If single MGW was
398 * configured through old VTY and no mgw in the new MGW pool VTY is
399 * replacing it, then output the single MGW converted to the new MGW
400 * pool VTY. */
401 if (llist_empty(&pool->member_list) && pool->mgcp_client_single) {
Pau Espin Pedrol7881f7d2022-10-18 14:13:04 +0200402 vty_out(vty, "%smgw 0%s", indent, VTY_NEWLINE);
403 config_write(vty, subindent, global_mgcp_client_conf);
Pau Espin Pedrold17a9de2022-10-18 12:34:20 +0200404 }
405
Pau Espin Pedrol7881f7d2022-10-18 14:13:04 +0200406 talloc_free(subindent);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200407 return CMD_SUCCESS;
408}
409
Pau Espin Pedrol3a914e32022-10-19 17:13:20 +0200410/* Deprecated, used for backward compatibility with older users which didn't call
411 * mgcp_client_pool_config_write(): */
412static int config_write_pool(struct vty *vty)
413{
414 if (mgcp_client_pool_config_write_called)
415 return CMD_SUCCESS;
416
417 return _mgcp_client_pool_config_write(vty, NULL);
418}
419
420/*! Write out MGCP client config to VTY.
421 * \param[in] vty VTY to which we should print.
422 * \param[in] indent string used for indentation (e.g. " ").
423 If NULL, indentation passed during mgcp_client_pool_vty_init() will be used.
424 * \returns CMD_SUCCESS on success, CMD_WARNING on error */
425int mgcp_client_pool_config_write(struct vty *vty, const char *indent)
426{
427 /* Tell internal node write function that the user supports calling proper API: */
428 mgcp_client_pool_config_write_called = true;
429 return _mgcp_client_pool_config_write(vty, indent);
430}
431
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200432DEFUN_ATTR(cfg_mgw,
Philipp Maierd55be052021-08-19 14:58:31 +0200433 cfg_mgw_cmd, "mgw <0-255>", "Select a MGCP client config to setup\n" "reference number\n", CMD_ATTR_IMMEDIATE)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200434{
435 int nr = atoi(argv[0]);
436 struct mgcp_client_pool_member *pool_member;
437
Pau Espin Pedrolff592152022-10-13 16:46:25 +0200438 pool_member = mgcp_client_pool_find_member_by_nr(global_mgcp_client_pool, nr);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200439 if (!pool_member) {
Pau Espin Pedrol6ddc79b2022-10-13 17:03:42 +0200440 pool_member = mgcp_client_pool_member_alloc(global_mgcp_client_pool, nr);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200441 OSMO_ASSERT(pool_member);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200442 }
443
444 vty->index = &pool_member->conf;
Philipp Maierdf9192e2021-09-03 17:50:51 +0200445 vty->index_sub = &pool_member->conf.description;
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200446 vty->node = global_mgcp_client_pool->vty_node->node;
447
448 return CMD_SUCCESS;
449}
450
451DEFUN_ATTR(cfg_no_mgw,
452 cfg_no_mgw_cmd,
Philipp Maier74e83d32021-08-20 09:57:27 +0200453 "no mgw <0-255>", NO_STR "Select a MGCP client config to remove\n" "reference number\n", CMD_ATTR_IMMEDIATE)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200454{
455 int nr = atoi(argv[0]);
456 struct mgcp_client_pool_member *pool_member;
457
Pau Espin Pedrolff592152022-10-13 16:46:25 +0200458 pool_member = mgcp_client_pool_find_member_by_nr(global_mgcp_client_pool, nr);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200459 if (!pool_member) {
460 vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE);
461 return CMD_WARNING;
462 }
463
464 /* Make sure that there are no ongoing calls */
465 if (pool_member->refcount > 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200466 vty_out(vty, "%% MGCP client (MGW %s) is still serving ongoing calls -- can't remove it now!%s",
467 mgcp_client_pool_member_name(pool_member), VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200468 return CMD_WARNING;
469 }
470
Pau Espin Pedrol6ddc79b2022-10-13 17:03:42 +0200471 mgcp_client_pool_member_free(pool_member);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200472
473 return CMD_SUCCESS;
474}
475
476DEFUN_ATTR(mgw_reconnect, mgw_reconnect_cmd,
477 "mgw <0-255> reconnect",
Philipp Maier8f91a7f2021-08-19 14:55:56 +0200478 MGW_STR "reference number\n" "reconfigure and reconnect MGCP client\n", CMD_ATTR_IMMEDIATE)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200479{
480 int nr = atoi(argv[0]);
481 struct mgcp_client_pool_member *pool_member = NULL;
482
Pau Espin Pedrolff592152022-10-13 16:46:25 +0200483 pool_member = mgcp_client_pool_find_member_by_nr(global_mgcp_client_pool, nr);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200484 if (!pool_member) {
485 vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE);
486 return CMD_WARNING;
487 }
488
489 /* Make sure that there are no ongoing calls */
490 if (pool_member->refcount > 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200491 vty_out(vty, "%% MGCP client (MGW %s) is still serving ongoing calls -- can't reconnect it now!%s",
492 mgcp_client_pool_member_name(pool_member), VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200493 return CMD_WARNING;
494 }
495
Pau Espin Pedrol35bd2522022-10-13 17:26:33 +0200496 if (mgcp_client_pool_member_reinit_client(pool_member) < 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200497 LOGP(DLMGCP, LOGL_ERROR, "(manual) MGW %s connect failed at (%s:%u)\n",
498 mgcp_client_pool_member_name(pool_member), pool_member->conf.remote_addr,
499 pool_member->conf.remote_port);
Philipp Maierdf9192e2021-09-03 17:50:51 +0200500 vty_out(vty, "%% MGCP client (MGW %s) initalization failed ('%s')%s",
501 mgcp_client_pool_member_name(pool_member), argv[0], VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200502 return CMD_WARNING;
503 }
504
505 return CMD_SUCCESS;
506}
507
508DEFUN_ATTR(mgw_block, mgw_block_cmd,
509 "mgw <0-255> block",
Philipp Maier8f91a7f2021-08-19 14:55:56 +0200510 MGW_STR "reference number\n" "block MGCP client so that it won't be used for new calls\n", CMD_ATTR_IMMEDIATE)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200511{
512 int nr = atoi(argv[0]);
513 struct mgcp_client_pool_member *pool_member = NULL;
514
Pau Espin Pedrolff592152022-10-13 16:46:25 +0200515 pool_member = mgcp_client_pool_find_member_by_nr(global_mgcp_client_pool, nr);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200516 if (!pool_member) {
517 vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE);
518 return CMD_WARNING;
519 }
520
521 pool_member->blocked = true;
522 return CMD_SUCCESS;
523}
524
525DEFUN_ATTR(mgw_unblock, mgw_unblock_cmd,
526 "mgw <0-255> unblock",
Philipp Maier8f91a7f2021-08-19 14:55:56 +0200527 MGW_STR "reference number\n" "unblock MGCP client so that it will be available for new calls\n", CMD_ATTR_IMMEDIATE)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200528{
529 int nr = atoi(argv[0]);
530 struct mgcp_client_pool_member *pool_member = NULL;
531
Pau Espin Pedrolff592152022-10-13 16:46:25 +0200532 pool_member = mgcp_client_pool_find_member_by_nr(global_mgcp_client_pool, nr);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200533 if (!pool_member) {
534 vty_out(vty, "%% no such MGCP client configured ('%s')%s", argv[0], VTY_NEWLINE);
535 return CMD_WARNING;
536 }
537
538 pool_member->blocked = false;
539 return CMD_SUCCESS;
540}
541
Pau Espin Pedrola6917fe2022-10-13 14:40:56 +0200542DEFUN(mgw_show, mgw_show_cmd, "show mgw-pool", SHOW_STR "Display information about the MGW-Pool\n")
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200543{
544 vty_out(vty, "%% MGW-Pool:%s", VTY_NEWLINE);
545 struct mgcp_client_pool_member *pool_member;
546
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +0200547 if (llist_empty(&global_mgcp_client_pool->member_list) && global_mgcp_client_pool->mgcp_client_single) {
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200548 vty_out(vty, "%% (pool is empty, single MGCP client will be used)%s", VTY_NEWLINE);
549 return CMD_SUCCESS;
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +0200550 } else if (llist_empty(&global_mgcp_client_pool->member_list)) {
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200551 vty_out(vty, "%% (pool is empty)%s", VTY_NEWLINE);
552 return CMD_SUCCESS;
553 }
554
Pau Espin Pedrold4ad77d2022-10-13 16:37:12 +0200555 llist_for_each_entry(pool_member, &global_mgcp_client_pool->member_list, list) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200556 vty_out(vty, "%% MGW %s%s", mgcp_client_pool_member_name(pool_member), VTY_NEWLINE);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200557 vty_out(vty, "%% mgcp-client: %s%s", pool_member->client ? "connected" : "disconnected",
558 VTY_NEWLINE);
559 vty_out(vty, "%% service: %s%s", pool_member->blocked ? "blocked" : "unblocked", VTY_NEWLINE);
560 vty_out(vty, "%% ongoing calls: %u%s", pool_member->refcount, VTY_NEWLINE);
561 }
562 return CMD_SUCCESS;
563}
564
565/*! Set up MGCP client VTY (pooled)
566 * (called once at startup by the application process).
567 * \param[in] parent_node identifier of the parent node on which the mgw node appears.
568 * \param[in] mgw_node identifier that should be used with the newly installed MGW node.
Pau Espin Pedrol7881f7d2022-10-18 14:13:04 +0200569 * \param[in] indent indentation string to match the indentation in the VTY config.
570 If NULL, it must be passed explicitly each time mgcp_client_pool_config_write() is called.
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200571 * \param[in] pool user provided memory to store the configured MGCP client (MGW) pool. */
572void mgcp_client_pool_vty_init(int parent_node, int mgw_node, const char *indent, struct mgcp_client_pool *pool)
573{
Philipp Maierda3a5752021-08-19 14:24:14 +0200574 /* A pool must be allocated before this function can be called */
575 OSMO_ASSERT(pool);
576
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200577 /* Never allow this function to be called twice on the same pool */
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200578 OSMO_ASSERT(!pool->vty_node);
579
Pau Espin Pedrol7881f7d2022-10-18 14:13:04 +0200580 if (indent) {
581 pool->vty_indent = talloc_strdup(pool, indent);
582 OSMO_ASSERT(pool->vty_indent);
583 }
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200584 pool->vty_node = talloc_zero(pool, struct cmd_node);
585 OSMO_ASSERT(pool->vty_node);
586 pool->vty_node->node = mgw_node;
587 pool->vty_node->vtysh = 1;
588 pool->vty_node->prompt = talloc_strdup(pool->vty_node, "%s(config-mgw)# ");
589
590 install_lib_element(parent_node, &cfg_mgw_cmd);
591 install_lib_element(parent_node, &cfg_no_mgw_cmd);
592
Pau Espin Pedrol7881f7d2022-10-18 14:13:04 +0200593 /* Note: config_write_pool is deprecated and user is expected to
594 * manually call mgcp_client_pool_config_write() when printing the VTY
595 * config */
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200596 install_node(pool->vty_node, config_write_pool);
597 vty_init_common(pool, mgw_node);
Pau Espin Pedrol8c138fe2022-10-19 12:47:31 +0200598 install_lib_element(mgw_node, &cfg_mgw_local_ip_cmd);
599 install_lib_element(mgw_node, &cfg_mgw_local_port_cmd);
600 install_lib_element(mgw_node, &cfg_mgw_remote_ip_cmd);
601 install_lib_element(mgw_node, &cfg_mgw_remote_port_cmd);
Pau Espin Pedrol8c138fe2022-10-19 12:47:31 +0200602 install_lib_element(mgw_node, &cfg_mgw_rtp_bts_base_port_cmd);
603 install_lib_element(mgw_node, &cfg_mgw_endpoint_domain_name_cmd);
604 install_lib_element(mgw_node, &cfg_mgw_reset_ep_name_cmd);
605 install_lib_element(mgw_node, &cfg_mgw_no_reset_ep_name_cmd);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200606
Philipp Maierdf9192e2021-09-03 17:50:51 +0200607 install_element(mgw_node, &cfg_description_cmd);
608
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200609 install_lib_element(ENABLE_NODE, &mgw_reconnect_cmd);
610 install_lib_element(ENABLE_NODE, &mgw_block_cmd);
611 install_lib_element(ENABLE_NODE, &mgw_unblock_cmd);
612
Pau Espin Pedrola6917fe2022-10-13 14:40:56 +0200613 install_lib_element_ve(&mgw_show_cmd);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200614
615 global_mgcp_client_pool = pool;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200616}