blob: fdac66533865f3a6da4893cda6c04a2728e41403 [file] [log] [blame]
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* The protocol implementation */
3
4/*
5 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2012 by On-Waves
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <ctype.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <time.h>
28#include <limits.h>
29#include <unistd.h>
30#include <errno.h>
Eric55fdfc22021-08-13 00:14:18 +020031#include <pthread.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020032
33#include <osmocom/core/msgb.h>
34#include <osmocom/core/talloc.h>
35#include <osmocom/core/select.h>
36
Philipp Maier87bd9be2017-08-22 16:35:41 +020037#include <osmocom/mgcp/mgcp.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020038#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier993ea6b2020-08-04 18:26:50 +020039#include <osmocom/mgcp/osmux.h>
40#include <osmocom/mgcp/mgcp_network.h>
41#include <osmocom/mgcp/mgcp_protocol.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020042#include <osmocom/mgcp/mgcp_stat.h>
43#include <osmocom/mgcp/mgcp_msg.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010044#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maierc66ab2c2020-06-02 20:55:34 +020045#include <osmocom/mgcp/mgcp_trunk.h>
Philipp Maier8970c492017-10-11 13:33:42 +020046#include <osmocom/mgcp/mgcp_sdp.h>
Philipp Maierbc0346e2018-06-07 09:52:16 +020047#include <osmocom/mgcp/mgcp_codec.h>
Stefan Sperlingba25eab2018-10-30 14:32:31 +010048#include <osmocom/mgcp/mgcp_conn.h>
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +010049#include <osmocom/mgcp/mgcp_iuup.h>
Pau Espin Pedrol8e0d1012022-10-06 10:29:11 +020050#include <osmocom/mgcp/debug.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020051
Philipp Maier39889e42021-08-04 17:42:57 +020052/* Contains the last successfully resolved endpoint name. This variable is used
53 * for the unit-tests to verify that the endpoint was correctly resolved. */
54static char debug_last_endpoint_name[MGCP_ENDPOINT_MAXLEN];
55
56/* Called from unit-tests only */
57char *mgcp_debug_get_last_endpoint_name(void)
58{
59 return debug_last_endpoint_name;
60}
61
Philipp Maierf486e742021-07-19 14:56:29 +020062/* A combination of LOGPENDP and LOGPTRUNK that automatically falls back to
63 * LOGPTRUNK when the endp parameter is NULL */
64#define LOGPEPTR(endp, trunk, cat, level, fmt, args...) \
65do { \
66 if (endp) \
67 LOGPENDP(endp, cat, level, fmt, ## args); \
68 else \
69 LOGPTRUNK(trunk, cat, level, fmt, ## args); \
70} while (0)
71
Philipp Maier8dc35972021-07-14 11:20:16 +020072/* Request data passed to the request handler */
73struct mgcp_request_data {
74 /* request name (e.g. "MDCX") */
75 char name[4+1];
76
77 /* parsing results from the MGCP header (trans id, endpoint name ...) */
78 struct mgcp_parse_data *pdata;
79
80 /* pointer to endpoint resource (may be NULL for wildcarded requests) */
81 struct mgcp_endpoint *endp;
82
83 /* pointer to trunk resource */
84 struct mgcp_trunk *trunk;
85
86 /* set to true when the request has been classified as wildcarded */
87 bool wildcarded;
88
89 /* contains cause code in case of problems during endp/trunk resolution */
90 int mgcp_cause;
91};
92
Philipp Maier33d97f72021-07-14 14:53:45 +020093/* Request handler specification, here we specify an array with function
94 * pointers to the various MGCP requests implemented below */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020095struct mgcp_request {
Philipp Maier33d97f72021-07-14 14:53:45 +020096 /* request name (e.g. "MDCX") */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020097 char *name;
Philipp Maier33d97f72021-07-14 14:53:45 +020098
99 /* function pointer to the request handler */
Philipp Maier8dc35972021-07-14 11:20:16 +0200100 struct msgb *(*handle_request)(struct mgcp_request_data *data);
101
Philipp Maier33d97f72021-07-14 14:53:45 +0200102 /* a human readable name that describes the request */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200103 char *debug_name;
104};
105
Philipp Maier8dc35972021-07-14 11:20:16 +0200106static struct msgb *handle_audit_endpoint(struct mgcp_request_data *data);
107static struct msgb *handle_create_con(struct mgcp_request_data *data);
108static struct msgb *handle_delete_con(struct mgcp_request_data *data);
109static struct msgb *handle_modify_con(struct mgcp_request_data *data);
110static struct msgb *handle_rsip(struct mgcp_request_data *data);
111static struct msgb *handle_noti_req(struct mgcp_request_data *data);
Philipp Maier33d97f72021-07-14 14:53:45 +0200112static const struct mgcp_request mgcp_requests[] = {
Ericee6958c2021-09-14 18:36:53 +0200113 { .name = "AUEP", .handle_request = handle_audit_endpoint, .debug_name = "AuditEndpoint" },
114 {
115 .name = "CRCX",
116 .handle_request = handle_create_con,
117 .debug_name = "CreateConnection",
118 },
119 {
120 .name = "DLCX",
121 .handle_request = handle_delete_con,
122 .debug_name = "DeleteConnection",
123 },
124 {
125 .name = "MDCX",
126 .handle_request = handle_modify_con,
127 .debug_name = "ModifiyConnection",
128 },
129 {
130 .name = "RQNT",
131 .handle_request = handle_noti_req,
132 .debug_name = "NotificationRequest",
133 },
Philipp Maier33d97f72021-07-14 14:53:45 +0200134
135 /* SPEC extension */
Ericee6958c2021-09-14 18:36:53 +0200136 {
137 .name = "RSIP",
138 .handle_request = handle_rsip,
139 .debug_name = "ReSetInProgress",
140 },
Philipp Maier33d97f72021-07-14 14:53:45 +0200141};
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200142
Philipp Maier87bd9be2017-08-22 16:35:41 +0200143/* Initalize transcoder */
144static int setup_rtp_processing(struct mgcp_endpoint *endp,
145 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200146{
Ericfbf78d12021-08-23 22:31:39 +0200147 struct mgcp_config *cfg = endp->trunk->cfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200148 struct mgcp_conn_rtp *conn_src = NULL;
149 struct mgcp_conn_rtp *conn_dst = conn;
150 struct mgcp_conn *_conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200151
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100152 switch (conn->type) {
153 case MGCP_RTP_DEFAULT:
Pau Espin Pedrol9d939b62022-10-03 16:59:20 +0200154 case MGCP_RTP_OSMUX:
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +0100155 case MGCP_RTP_IUUP:
156 break;
157 default:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200158 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
159 "RTP-setup: Endpoint is not configured as RTP default, stopping here!\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200160 return 0;
161 }
162
Philipp Maier87bd9be2017-08-22 16:35:41 +0200163 if (conn->conn->mode == MGCP_CONN_LOOPBACK) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200164 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
165 "RTP-setup: Endpoint is in loopback mode, stopping here!\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200166 return 0;
167 }
168
169 /* Find the "sister" connection */
170 llist_for_each_entry(_conn, &endp->conns, entry) {
171 if (_conn->id != conn->conn->id) {
172 conn_src = &_conn->u.rtp;
173 break;
174 }
175 }
176
Philipp Maieracc10352018-07-19 18:07:57 +0200177 return cfg->setup_rtp_processing_cb(endp, conn_dst, conn_src);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200178}
179
Philipp Maier87bd9be2017-08-22 16:35:41 +0200180/* Helper function to allocate some memory for responses and retransmissions */
Ericfbcf4a62021-09-09 18:02:31 +0200181static struct msgb *mgcp_msgb_alloc(void *ctx)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200182{
183 struct msgb *msg;
Ericfbcf4a62021-09-09 18:02:31 +0200184 msg = msgb_alloc_headroom_c(ctx, 4096, 128, "MGCP msg");
185
186 if (!msg) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200187 LOGP(DLMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n");
Ericfbcf4a62021-09-09 18:02:31 +0200188 return NULL;
189 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200190
191 return msg;
192}
193
Philipp Maier87bd9be2017-08-22 16:35:41 +0200194/* Helper function for do_retransmission() and create_resp() */
Eric958f5e72021-08-03 23:00:17 +0200195static struct msgb *create_retransmission_response(const struct mgcp_endpoint *endp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200196{
Ericfbcf4a62021-09-09 18:02:31 +0200197 struct msgb *msg = mgcp_msgb_alloc(endp->trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200198 if (!msg)
199 return NULL;
200
201 msg->l2h = msgb_put(msg, strlen(endp->last_response));
202 memcpy(msg->l2h, endp->last_response, msgb_l2len(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200203 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Retransmitted response");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200204 return msg;
205}
206
Ericfbcf4a62021-09-09 18:02:31 +0200207static struct msgb *create_resp(void *msgctx, struct mgcp_endpoint *endp, int code, const char *txt, const char *msg,
208 const char *trans, const char *param, const char *sdp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200209{
210 int len;
211 struct msgb *res;
212
Ericfbcf4a62021-09-09 18:02:31 +0200213 OSMO_ASSERT(msgctx != 0);
214 res = mgcp_msgb_alloc(msgctx);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200215 if (!res)
216 return NULL;
217
Philipp Maier87bd9be2017-08-22 16:35:41 +0200218 len = snprintf((char *)res->data, 2048, "%d %s%s%s\r\n%s",
219 code, trans, txt, param ? param : "", sdp ? sdp : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200220 if (len < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200221 LOGPENDP(endp, DLMGCP, LOGL_ERROR, "Failed to sprintf MGCP response.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200222 msgb_free(res);
223 return NULL;
224 }
225
226 res->l2h = msgb_put(res, len);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200227 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "Generated response: code=%d\n", code);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200228 mgcp_disp_msg(res->l2h, msgb_l2len(res), "Generated response");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200229
230 /*
231 * Remember the last transmission per endpoint.
232 */
233 if (endp) {
Philipp Maier14b27a82020-06-02 20:15:30 +0200234 struct mgcp_trunk *trunk = endp->trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200235 talloc_free(endp->last_response);
236 talloc_free(endp->last_trans);
Philipp Maier14b27a82020-06-02 20:15:30 +0200237 endp->last_trans = talloc_strdup(trunk->endpoints, trans);
238 endp->last_response = talloc_strndup(trunk->endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200239 (const char *)res->l2h,
240 msgb_l2len(res));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200241 }
242
243 return res;
244}
245
Ericfbcf4a62021-09-09 18:02:31 +0200246static struct msgb *create_ok_resp_with_param(void *msgctx, struct mgcp_endpoint *endp, int code, const char *msg,
247 const char *trans, const char *param)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200248{
Ericfbcf4a62021-09-09 18:02:31 +0200249 return create_resp(msgctx, endp, code, " OK", msg, trans, param, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200250}
251
Ericfbcf4a62021-09-09 18:02:31 +0200252static struct msgb *create_ok_response(void *msgctx, struct mgcp_endpoint *endp, int code, const char *msg,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200253 const char *trans)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200254{
Ericfbcf4a62021-09-09 18:02:31 +0200255 return create_ok_resp_with_param(msgctx, endp, code, msg, trans, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200256}
257
Ericfbcf4a62021-09-09 18:02:31 +0200258static struct msgb *create_err_response(void *msgctx, struct mgcp_endpoint *endp, int code, const char *msg,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200259 const char *trans)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200260{
Ericfbcf4a62021-09-09 18:02:31 +0200261 return create_resp(msgctx, endp, code, " FAIL", msg, trans, NULL, NULL);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200262}
263
Philipp Maier87bd9be2017-08-22 16:35:41 +0200264/* Format MGCP response string (with SDP attached) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200265static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200266 struct mgcp_conn_rtp *conn,
267 const char *msg,
Philipp Maier55295f72018-01-15 14:00:28 +0100268 const char *trans_id,
Philipp Maier41d59202021-07-20 15:49:00 +0200269 bool add_epname,
270 bool add_conn_id)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200271{
Pau Espin Pedrol71d42e72020-09-03 14:20:07 +0200272 /* cfg->local_ip allows overwritting the announced IP address with
273 * regards to the one we actually bind to. Useful in behind-NAT
274 * scenarios.
275 * TODO: we may want to define another local_ip_osmux var to
276 * us for OSMUX connections. Perhaps adding a new internal API to get it
277 * based on conn type.
278 */
Ericfbf78d12021-08-23 22:31:39 +0200279 const char *addr = strlen(endp->trunk->cfg->local_ip) ? endp->trunk->cfg->local_ip : conn->end.local_addr;
Philipp Maier8970c492017-10-11 13:33:42 +0200280 struct msgb *sdp;
281 int rc;
282 struct msgb *result;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200283
Ericfbcf4a62021-09-09 18:02:31 +0200284 sdp = msgb_alloc_headroom_c(endp->trunk, 4096, 128, "sdp record");
Philipp Maier8970c492017-10-11 13:33:42 +0200285 if (!sdp)
286 return NULL;
287
Philipp Maier41d59202021-07-20 15:49:00 +0200288 /* Attach optional endpoint name */
289 if (add_epname) {
290 rc = msgb_printf(sdp, "Z: %s\r\n", endp->name);
291 if (rc < 0)
292 goto error;
293 }
294
295 /* Attach optional connection id */
296 if (add_conn_id) {
297 rc = msgb_printf(sdp, "I: %s\r\n", conn->conn->id);
Philipp Maier55295f72018-01-15 14:00:28 +0100298 if (rc < 0)
299 goto error;
300 }
301
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100302 /* Attach optional OSMUX parameters */
Pau Espin Pedrolc63f15a2019-05-10 16:52:08 +0200303 if (mgcp_conn_rtp_is_osmux(conn)) {
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200304 rc = msgb_printf(sdp, MGCP_X_OSMO_OSMUX_HEADER " %u\r\n", conn->osmux.local_cid);
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100305 if (rc < 0)
306 goto error;
307 }
308
309 /* Attach line break to separate the parameters from the SDP block */
Philipp Maierc3cfae22018-01-22 12:03:03 +0100310 rc = msgb_printf(sdp, "\r\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200311
Philipp Maier8970c492017-10-11 13:33:42 +0200312 rc = mgcp_write_response_sdp(endp, conn, sdp, addr);
313 if (rc < 0)
314 goto error;
Ericfbcf4a62021-09-09 18:02:31 +0200315 result = create_resp(endp->trunk, endp, 200, " OK", msg, trans_id, NULL, (char *)sdp->data);
Philipp Maier8970c492017-10-11 13:33:42 +0200316 msgb_free(sdp);
317 return result;
318error:
319 msgb_free(sdp);
320 return NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200321}
322
Philipp Maier87bd9be2017-08-22 16:35:41 +0200323/* Send out dummy packet to keep the connection open, if the connection is an
324 * osmux connection, send the dummy packet via OSMUX */
325static void send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200326{
Pau Espin Pedrol2ca48822022-10-06 10:58:10 +0200327 /* Avoid sending dummy packet if the remote address was not yet
328 * configured through CRCX/MDCX: */
329 if (!mgcp_rtp_end_remote_addr_available(&conn->end))
330 return;
331
Pau Espin Pedrolabb9d472022-10-04 12:09:14 +0200332 if (mgcp_conn_rtp_is_osmux(conn))
Pau Espin Pedrol887c93e2022-10-06 12:36:03 +0200333 osmux_send_dummy(conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200334 else
Philipp Maier87bd9be2017-08-22 16:35:41 +0200335 mgcp_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200336}
337
Philipp Maier87bd9be2017-08-22 16:35:41 +0200338/* handle incoming messages:
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200339 * - this can be a command (four letters, space, transaction id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200340 * - or a response (three numbers, space, transaction id) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200341struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
342{
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200343 struct rate_ctr_group *rate_ctrs = cfg->ratectr.mgcp_general_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200344 struct mgcp_parse_data pdata;
Philipp Maier8dc35972021-07-14 11:20:16 +0200345 struct mgcp_request_data rq;
Harald Weltee35eeae2017-12-28 13:47:37 +0100346 int rc, i, code, handled = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200347 struct msgb *resp = NULL;
348 char *data;
349
Philipp Maier39889e42021-08-04 17:42:57 +0200350 debug_last_endpoint_name[0] = '\0';
351
Alexander Chemeris63866002020-05-05 17:18:40 +0300352 /* Count all messages, even incorect ones */
Pau Espin Pedrol907744e2021-06-04 17:57:34 +0200353 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_MSGS_TOTAL));
Alexander Chemeris63866002020-05-05 17:18:40 +0300354
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200355 if (msgb_l2len(msg) < 4) {
356 LOGP(DLMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len);
Pau Espin Pedrol907744e2021-06-04 17:57:34 +0200357 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_FAIL_MSG_PARSE));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200358 return NULL;
359 }
360
Alexander Chemeris63866002020-05-05 17:18:40 +0300361 if (mgcp_msg_terminate_nul(msg)) {
Pau Espin Pedrol907744e2021-06-04 17:57:34 +0200362 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_FAIL_MSG_PARSE));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200363 return NULL;
Alexander Chemeris63866002020-05-05 17:18:40 +0300364 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200365
Philipp Maier87bd9be2017-08-22 16:35:41 +0200366 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Received message");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200367
Philipp Maier87bd9be2017-08-22 16:35:41 +0200368 /* attempt to treat it as a response */
369 if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200370 LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code);
Pau Espin Pedrol907744e2021-06-04 17:57:34 +0200371 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_FAIL_MSG_PARSE));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200372 return NULL;
373 }
374
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200375
Philipp Maier8dc35972021-07-14 11:20:16 +0200376 /* Parse message, extract endpoint name and transaction identifier and request name etc. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200377 memset(&pdata, 0, sizeof(pdata));
Philipp Maier8dc35972021-07-14 11:20:16 +0200378 memset(&rq, 0, sizeof(rq));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200379 pdata.cfg = cfg;
Philipp Maier8dc35972021-07-14 11:20:16 +0200380 memcpy(rq.name, (const char *)&msg->l2h[0], sizeof(rq.name)-1);
381 msg->l3h = &msg->l2h[4];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200382 data = mgcp_strline((char *)msg->l3h, &pdata.save);
Harald Weltee35eeae2017-12-28 13:47:37 +0100383 rc = mgcp_parse_header(&pdata, data);
Harald Weltee35eeae2017-12-28 13:47:37 +0100384 if (rc < 0) {
Philipp Maier8dc35972021-07-14 11:20:16 +0200385 LOGP(DLMGCP, LOGL_ERROR, "%s: failed to parse MCGP message\n", rq.name);
386 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_FAIL_MSG_PARSE));
Ericfbcf4a62021-09-09 18:02:31 +0200387 return create_err_response(cfg, NULL, -rc, rq.name, "000000");
Harald Welteabbb6b92017-12-28 13:13:50 +0100388 }
389
Philipp Maier8dc35972021-07-14 11:20:16 +0200390 /* Locate endpoint and trunk, if no endpoint can be located try at least to identify the trunk. */
391 rq.pdata = &pdata;
392 rq.wildcarded = mgcp_endp_is_wildcarded(pdata.epname);
393 rq.endp = mgcp_endp_by_name(&rc, pdata.epname, pdata.cfg);
394 rq.mgcp_cause = rc;
395 if (!rq.endp) {
396 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_FAIL_NO_ENDPOINT));
397 if (rq.wildcarded) {
398 /* If we are unable to find the endpoint we still may be able to identify the trunk. Some
399 * request handlers will still be able to perform a useful action if the request refers to
400 * the whole trunk (wildcarded request). */
401 LOGP(DLMGCP, LOGL_NOTICE,
402 "%s: cannot find endpoint \"%s\", cause=%d -- trying to identify trunk...\n", rq.name,
403 pdata.epname, -rq.mgcp_cause);
404 rq.trunk = mgcp_trunk_by_name(pdata.cfg, pdata.epname);
405 if (!rq.trunk) {
406 LOGP(DLMGCP, LOGL_ERROR, "%s: failed to identify trunk for endpoint \"%s\" -- abort\n",
407 rq.name, pdata.epname);
Ericfbcf4a62021-09-09 18:02:31 +0200408 return create_err_response(cfg, NULL, -rq.mgcp_cause, rq.name, pdata.trans);
Philipp Maier8dc35972021-07-14 11:20:16 +0200409 }
410 } else {
411 /* If the endpoint name suggests that the request refers to a specific endpoint, then the
412 * request cannot be handled and we must stop early. */
413 LOGP(DLMGCP, LOGL_NOTICE,
414 "%s: cannot find endpoint \"%s\", cause=%d -- abort\n", rq.name,
415 pdata.epname, -rq.mgcp_cause);
Ericfbcf4a62021-09-09 18:02:31 +0200416 return create_err_response(cfg, NULL, -rq.mgcp_cause, rq.name, pdata.trans);
Philipp Maier8dc35972021-07-14 11:20:16 +0200417 }
418 } else {
Philipp Maier39889e42021-08-04 17:42:57 +0200419 osmo_strlcpy(debug_last_endpoint_name, rq.endp->name, sizeof(debug_last_endpoint_name));
Philipp Maier8dc35972021-07-14 11:20:16 +0200420 rq.trunk = rq.endp->trunk;
421 rq.mgcp_cause = 0;
422
423 /* Check if we have to retransmit a response from a previous transaction */
424 if (pdata.trans && rq.endp->last_trans && strcmp(rq.endp->last_trans, pdata.trans) == 0) {
425 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_MSGS_RETRANSMITTED));
Eric958f5e72021-08-03 23:00:17 +0200426 return create_retransmission_response(rq.endp);
Philipp Maier8dc35972021-07-14 11:20:16 +0200427 }
428 }
429
430 /* Find an appropriate handler for the current request and execute it */
431 for (i = 0; i < ARRAY_SIZE(mgcp_requests); i++) {
432 if (strcmp(mgcp_requests[i].name, rq.name) == 0) {
Philipp Maier8dc35972021-07-14 11:20:16 +0200433 /* Execute request handler */
434 if (rq.endp)
435 LOGP(DLMGCP, LOGL_INFO,
436 "%s: executing request handler \"%s\" for endpoint resource \"%s\"\n", rq.name,
437 mgcp_requests[i].debug_name, rq.endp->name);
438 else
439 LOGP(DLMGCP, LOGL_INFO,
440 "%s: executing request handler \"%s\" for trunk resource of endpoint \"%s\"\n",
441 rq.name, mgcp_requests[i].debug_name, pdata.epname);
442 resp = mgcp_requests[i].handle_request(&rq);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200443 handled = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200444 break;
445 }
446 }
447
Philipp Maier8dc35972021-07-14 11:20:16 +0200448 /* Check if the MGCP request was handled and increment rate counters accordingly. */
Alexander Chemeris63866002020-05-05 17:18:40 +0300449 if (handled) {
Pau Espin Pedrol907744e2021-06-04 17:57:34 +0200450 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_MSGS_HANDLED));
Alexander Chemeris63866002020-05-05 17:18:40 +0300451 } else {
Pau Espin Pedrol907744e2021-06-04 17:57:34 +0200452 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_GENERAL_RX_MSGS_UNHANDLED));
Philipp Maier8dc35972021-07-14 11:20:16 +0200453 LOGP(DLMGCP, LOGL_ERROR, "MSG with type: '%.4s' not handled\n", &msg->l2h[0]);
Alexander Chemeris63866002020-05-05 17:18:40 +0300454 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200455
456 return resp;
457}
458
Philipp Maier87bd9be2017-08-22 16:35:41 +0200459/* AUEP command handler, processes the received command */
Philipp Maier8dc35972021-07-14 11:20:16 +0200460static struct msgb *handle_audit_endpoint(struct mgcp_request_data *rq)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200461{
Philipp Maier8dc35972021-07-14 11:20:16 +0200462 LOGPENDP(rq->endp, DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n");
Ericee6958c2021-09-14 18:36:53 +0200463 if (!rq->endp || !mgcp_endp_avail(rq->endp)) {
464 LOGPENDP(rq->endp, DLMGCP, LOGL_ERROR, "AUEP: selected endpoint not available!\n");
465 return create_err_response(rq->trunk, NULL, 501, "AUEP", rq->pdata->trans);
466 }
467
Ericfbcf4a62021-09-09 18:02:31 +0200468 return create_ok_response(rq->trunk, rq->endp, 200, "AUEP", rq->pdata->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200469}
470
Harald Welte1d1b98f2017-12-25 10:03:40 +0100471/* Try to find a free port by attempting to bind on it. Also handle the
Philipp Maier87bd9be2017-08-22 16:35:41 +0200472 * counter that points on the next free port. Since we have a pointer
Philipp Maierb38fb892018-05-22 13:52:21 +0200473 * to the next free port, binding should in work on the first attempt in
Pau Espin Pedrolfc806732019-04-23 00:18:43 +0200474 * general. In case of failure the next port is tried until the whole port
475 * range is tried once. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200476static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200477{
478 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200479 struct mgcp_port_range *range;
Philipp Maierb38fb892018-05-22 13:52:21 +0200480 unsigned int tries;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200481
Philipp Maier87bd9be2017-08-22 16:35:41 +0200482 OSMO_ASSERT(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200483
Ericfbf78d12021-08-23 22:31:39 +0200484 range = &endp->trunk->cfg->net_ports;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200485
Eric55fdfc22021-08-13 00:14:18 +0200486 pthread_mutex_lock(&range->lock);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200487 /* attempt to find a port */
Philipp Maierb38fb892018-05-22 13:52:21 +0200488 tries = (range->range_end - range->range_start) / 2;
489 for (i = 0; i < tries; ++i) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200490 int rc;
491
492 if (range->last_port >= range->range_end)
493 range->last_port = range->range_start;
494
Philipp Maier87bd9be2017-08-22 16:35:41 +0200495 rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200496
497 range->last_port += 2;
498 if (rc == 0) {
Eric55fdfc22021-08-13 00:14:18 +0200499 pthread_mutex_unlock(&range->lock);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200500 return 0;
501 }
502
503 }
Eric55fdfc22021-08-13 00:14:18 +0200504 pthread_mutex_unlock(&range->lock);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200505 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
506 "Allocating a RTP/RTCP port failed %u times.\n",
507 tries);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200508 return -1;
509}
510
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200511/*! Helper function for check_local_cx_options() to get a pointer of the next
512 * lco option identifier
513 * \param[in] lco string
514 * \returns pointer to the beginning of the LCO identifier, NULL on failure */
515char *get_lco_identifier(const char *options)
516{
517 char *ptr;
518 unsigned int count = 0;
519
520 /* Jump to the end of the lco identifier */
521 ptr = strstr(options, ":");
522 if (!ptr)
523 return NULL;
524
525 /* Walk backwards until the pointer points to the beginning of the
526 * lco identifier. We know that we stand at the beginning when we
527 * are either at the beginning of the memory or see a space or
528 * comma. (this is tolerant, it will accept a:10, b:11 as well as
529 * a:10,b:11) */
530 while (1) {
531 /* Endless loop protection */
532 if (count > 10000)
533 return NULL;
534 else if (ptr < options || *ptr == ' ' || *ptr == ',') {
535 ptr++;
536 break;
537 }
538 ptr--;
539 count++;
540 }
541
542 /* Check if we got any result */
543 if (*ptr == ':')
544 return NULL;
545
546 return ptr;
547}
548
549/*! Check the LCO option. This function checks for multiple appearence of LCO
550 * options, which is illegal
551 * \param[in] ctx talloc context
552 * \param[in] lco string
553 * \returns 0 on success, -1 on failure */
554int check_local_cx_options(void *ctx, const char *options)
555{
556 int i;
557 char *options_copy;
558 char *lco_identifier;
559 char *lco_identifier_end;
560 char *next_lco_identifier;
561
562 char **lco_seen;
563 unsigned int lco_seen_n = 0;
564
565 if (!options)
566 return -1;
567
568 lco_seen =
569 (char **)talloc_zero_size(ctx, strlen(options) * sizeof(char *));
570 options_copy = talloc_strdup(ctx, options);
571 lco_identifier = options_copy;
572
573 do {
574 /* Move the lco_identifier pointer to the beginning of the
575 * current lco option identifier */
576 lco_identifier = get_lco_identifier(lco_identifier);
577 if (!lco_identifier)
578 goto error;
579
580 /* Look ahead to the next LCO option early, since we
581 * will parse destructively */
582 next_lco_identifier = strstr(lco_identifier + 1, ",");
583
584 /* Pinch off the end of the lco field identifier name
585 * and see if we still got something, also check if
586 * there is some value after the colon. */
587 lco_identifier_end = strstr(lco_identifier, ":");
588 if (!lco_identifier_end)
589 goto error;
590 if (*(lco_identifier_end + 1) == ' '
591 || *(lco_identifier_end + 1) == ','
592 || *(lco_identifier_end + 1) == '\0')
593 goto error;
594 *lco_identifier_end = '\0';
595 if (strlen(lco_identifier) == 0)
596 goto error;
597
598 /* Check if we have already seen the current field identifier
599 * before. If yes, we must bail, an LCO must only appear once
600 * in the LCO string */
601 for (i = 0; i < lco_seen_n; i++) {
Pau Espin Pedrol7eb6f2c2019-06-26 13:00:52 +0200602 if (strcasecmp(lco_seen[i], lco_identifier) == 0)
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200603 goto error;
604 }
605 lco_seen[lco_seen_n] = lco_identifier;
606 lco_seen_n++;
607
608 /* The first identifier must always be found at the beginnning
609 * of the LCO string */
610 if (lco_seen[0] != options_copy)
611 goto error;
612
613 /* Go to the next lco option */
614 lco_identifier = next_lco_identifier;
615 } while (lco_identifier);
616
617 talloc_free(lco_seen);
618 talloc_free(options_copy);
619 return 0;
620error:
621 talloc_free(lco_seen);
622 talloc_free(options_copy);
623 return -1;
624}
625
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200626/* Set the LCO from a string (see RFC 3435).
Harald Welte1d1b98f2017-12-25 10:03:40 +0100627 * The string is stored in the 'string' field. A NULL string is handled exactly
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200628 * like an empty string, the 'string' field is never NULL after this function
629 * has been called. */
Philipp Maiera390d0b2018-01-31 17:30:19 +0100630static int set_local_cx_options(void *ctx, struct mgcp_lco *lco,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200631 const char *options)
632{
Pau Espin Pedrolfe9a1fe2019-06-25 16:59:15 +0200633 char *lco_id;
Philipp Maier8dbc9ed2018-10-26 14:50:25 +0200634 char codec[17];
Pau Espin Pedrol19539862020-09-21 11:43:41 +0200635 char nt[17];
Pau Espin Pedrol83fd8a52019-06-26 12:55:26 +0200636 int len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200637
Philipp Maier604410c2018-06-06 10:02:16 +0200638 if (!options)
639 return 0;
640 if (strlen(options) == 0)
641 return 0;
642
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200643 /* Make sure the encoding of the LCO is consistant before we proceed */
644 if (check_local_cx_options(ctx, options) != 0) {
645 LOGP(DLMGCP, LOGL_ERROR,
646 "local CX options: Internal inconsistency in Local Connection Options!\n");
647 return 524;
648 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200649
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200650 talloc_free(lco->string);
651 lco->string = talloc_strdup(ctx, options);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200652
Pau Espin Pedrolfe9a1fe2019-06-25 16:59:15 +0200653 lco_id = lco->string;
654 while ((lco_id = get_lco_identifier(lco_id))) {
655 switch (tolower(lco_id[0])) {
656 case 'p':
657 if (sscanf(lco_id + 1, ":%d-%d",
658 &lco->pkt_period_min, &lco->pkt_period_max) == 1)
659 lco->pkt_period_max = lco->pkt_period_min;
660 break;
661 case 'a':
Pau Espin Pedrol1dc2dce2020-09-21 11:25:15 +0200662 /* FIXME: LCO also supports the negotiation of more than one codec.
Pau Espin Pedrolfe9a1fe2019-06-25 16:59:15 +0200663 * (e.g. a:PCMU;G726-32) But this implementation only supports a single
664 * codec only. */
665 if (sscanf(lco_id + 1, ":%16[^,]", codec) == 1) {
666 talloc_free(lco->codec);
Pau Espin Pedrol83fd8a52019-06-26 12:55:26 +0200667 /* MGCP header is case insensive, and we'll need
668 codec in uppercase when using it later: */
669 len = strlen(codec);
670 lco->codec = talloc_size(ctx, len + 1);
671 osmo_str_toupper_buf(lco->codec, len + 1, codec);
Pau Espin Pedrolfe9a1fe2019-06-25 16:59:15 +0200672 }
673 break;
Pau Espin Pedrol19539862020-09-21 11:43:41 +0200674 case 'n':
675 if (lco_id[1] == 't' && sscanf(lco_id + 2, ":%16[^,]", nt) == 1)
676 break;
677 /* else: fall throught to print notice log */
Pau Espin Pedrolfe9a1fe2019-06-25 16:59:15 +0200678 default:
679 LOGP(DLMGCP, LOGL_NOTICE,
680 "LCO: unhandled option: '%c'/%d in \"%s\"\n",
681 *lco_id, *lco_id, lco->string);
682 break;
683 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200684
Pau Espin Pedrolfe9a1fe2019-06-25 16:59:15 +0200685 lco_id = strchr(lco_id, ',');
686 if (!lco_id)
687 break;
Philipp Maier604410c2018-06-06 10:02:16 +0200688 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200689
690 LOGP(DLMGCP, LOGL_DEBUG,
691 "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n",
692 lco->pkt_period_max, lco->codec);
Philipp Maiera390d0b2018-01-31 17:30:19 +0100693
694 /* Check if the packetization fits the 20ms raster */
695 if (lco->pkt_period_min % 20 && lco->pkt_period_max % 20) {
696 LOGP(DLMGCP, LOGL_ERROR,
697 "local CX options: packetization interval is not a multiple of 20ms!\n");
698 return 535;
699 }
700
701 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200702}
703
704void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
705 struct mgcp_rtp_end *rtp)
706{
Philipp Maier14b27a82020-06-02 20:15:30 +0200707 struct mgcp_trunk *trunk = endp->trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200708
Philipp Maier14b27a82020-06-02 20:15:30 +0200709 int patch_ssrc = expect_ssrc_change && trunk->force_constant_ssrc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200710
Philipp Maier14b27a82020-06-02 20:15:30 +0200711 rtp->force_aligned_timing = trunk->force_aligned_timing;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200712 rtp->force_constant_ssrc = patch_ssrc ? 1 : 0;
Philipp Maier14b27a82020-06-02 20:15:30 +0200713 rtp->rfc5993_hr_convert = trunk->rfc5993_hr_convert;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200714
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200715 LOGPENDP(endp, DLMGCP, LOGL_DEBUG,
716 "Configuring RTP endpoint: local port %d%s%s\n",
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +0200717 osmo_sockaddr_port(&rtp->addr.u.sa),
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200718 rtp->force_aligned_timing ? ", force constant timing" : "",
719 rtp->force_constant_ssrc ? ", force constant ssrc" : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200720}
721
Pau Espin Pedrol8358c4b2021-07-07 12:41:38 +0200722uint32_t mgcp_rtp_packet_duration(const struct mgcp_endpoint *endp,
723 const struct mgcp_rtp_end *rtp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200724{
725 int f = 0;
726
727 /* Get the number of frames per channel and packet */
728 if (rtp->frames_per_packet)
729 f = rtp->frames_per_packet;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200730 else if (rtp->packet_duration_ms && rtp->codec->frame_duration_num) {
731 int den = 1000 * rtp->codec->frame_duration_num;
732 f = (rtp->packet_duration_ms * rtp->codec->frame_duration_den +
Philipp Maier87bd9be2017-08-22 16:35:41 +0200733 den / 2)
734 / den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200735 }
736
Philipp Maierbc0346e2018-06-07 09:52:16 +0200737 return rtp->codec->rate * f * rtp->codec->frame_duration_num /
738 rtp->codec->frame_duration_den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200739}
740
Pau Espin Pedrol9fb8ddf2019-05-08 15:35:36 +0200741/*! Initializes osmux socket if not yet initialized. Parses Osmux CID from MGCP line.
742 * \param[in] endp Endpoint willing to initialize osmux
743 * \param[in] line Line X-Osmux from MGCP header msg to parse
744 * \returns OSMUX CID, -1 for wildcard, -2 on parse error, -3 on osmux initalize error
745 */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200746static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line)
747{
Pau Espin Pedrolb7f52b42022-09-25 00:14:03 +0200748 if (!endp->trunk->cfg->osmux_initialized) {
Pau Espin Pedrolc1ad7fd2022-10-06 10:32:07 +0200749 if (osmux_init(endp->trunk) < 0) {
Pau Espin Pedrol8e0d1012022-10-06 10:29:11 +0200750 LOGPENDP(endp, DOSMUX, LOGL_ERROR, "Cannot init OSMUX\n");
Pau Espin Pedrol9fb8ddf2019-05-08 15:35:36 +0200751 return -3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200752 }
Pau Espin Pedrol8e0d1012022-10-06 10:29:11 +0200753 LOGPENDP(endp, DOSMUX, LOGL_NOTICE, "OSMUX socket has been set up\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200754 }
755
756 return mgcp_parse_osmux_cid(line);
757}
758
Philipp Maierbc0346e2018-06-07 09:52:16 +0200759/* Process codec information contained in CRCX/MDCX */
760static int handle_codec_info(struct mgcp_conn_rtp *conn,
Philipp Maier8dc35972021-07-14 11:20:16 +0200761 struct mgcp_request_data *rq, int have_sdp, bool crcx)
Philipp Maierbc0346e2018-06-07 09:52:16 +0200762{
Philipp Maier8dc35972021-07-14 11:20:16 +0200763 struct mgcp_endpoint *endp = rq->endp;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200764 int rc;
765 char *cmd;
766
767 if (crcx)
768 cmd = "CRCX";
769 else
770 cmd = "MDCX";
771
772 /* Collect codec information */
773 if (have_sdp) {
774 /* If we have SDP, we ignore the local connection options and
775 * use only the SDP information. */
776 mgcp_codec_reset_all(conn);
Philipp Maier8dc35972021-07-14 11:20:16 +0200777 rc = mgcp_parse_sdp_data(endp, conn, rq->pdata);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200778 if (rc != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200779 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
780 "%s: sdp not parseable\n", cmd);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200781
782 /* See also RFC 3661: Protocol error */
783 return 510;
784 }
785 } else if (endp->local_options.codec) {
786 /* When no SDP is available, we use the codec information from
787 * the local connection options (if present) */
788 mgcp_codec_reset_all(conn);
Philipp Maier228e5912019-03-05 13:56:59 +0100789 rc = mgcp_codec_add(conn, PTYPE_UNDEFINED, endp->local_options.codec, NULL);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200790 if (rc != 0)
791 goto error;
792 }
793
794 /* Make sure we always set a sane default codec */
795 if (conn->end.codecs_assigned == 0) {
796 /* When SDP and/or LCO did not supply any codec information,
797 * than it makes sense to pick a sane default: (payload-type 0,
798 * PCMU), see also: OS#2658 */
799 mgcp_codec_reset_all(conn);
Philipp Maier228e5912019-03-05 13:56:59 +0100800 rc = mgcp_codec_add(conn, 0, NULL, NULL);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200801 if (rc != 0)
802 goto error;
803 }
804
805 /* Make codec decision */
806 if (mgcp_codec_decide(conn) != 0)
807 goto error;
808
809 return 0;
810
811error:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200812 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
813 "%s: codec negotiation failure\n", cmd);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200814
815 /* See also RFC 3661: Codec negotiation failure */
816 return 534;
817}
818
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200819static bool parse_x_osmo_ign(struct mgcp_endpoint *endp, char *line)
820{
821 char *saveptr = NULL;
822
Pau Espin Pedrol6049a632020-09-21 11:03:21 +0200823 if (strncasecmp(line, MGCP_X_OSMO_IGN_HEADER, strlen(MGCP_X_OSMO_IGN_HEADER)))
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200824 return false;
825 line += strlen(MGCP_X_OSMO_IGN_HEADER);
826
827 while (1) {
828 char *token = strtok_r(line, " ", &saveptr);
829 line = NULL;
830 if (!token)
831 break;
832
Pau Espin Pedrol6e26c702019-06-26 13:09:39 +0200833 if (!strcasecmp(token, "C"))
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200834 endp->x_osmo_ign |= MGCP_X_OSMO_IGN_CALLID;
835 else
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200836 LOGPENDP(endp, DLMGCP, LOGL_ERROR, "received unknown X-Osmo-IGN item '%s'\n", token);
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200837 }
838
839 return true;
840}
841
Philipp Maier87bd9be2017-08-22 16:35:41 +0200842/* CRCX command handler, processes the received command */
Philipp Maier8dc35972021-07-14 11:20:16 +0200843static struct msgb *handle_create_con(struct mgcp_request_data *rq)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200844{
Philipp Maier8dc35972021-07-14 11:20:16 +0200845 struct mgcp_parse_data *pdata = rq->pdata;
846 struct mgcp_trunk *trunk = rq->trunk;
847 struct mgcp_endpoint *endp = rq->endp;
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200848 struct rate_ctr_group *rate_ctrs = trunk->ratectr.mgcp_crcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200849 int error_code = 400;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200850 const char *local_options = NULL;
851 const char *callid = NULL;
852 const char *mode = NULL;
853 char *line;
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200854 int have_sdp = 0, remote_osmux_cid = -2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200855 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100856 struct mgcp_conn *_conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200857 char conn_name[512];
Philipp Maiera390d0b2018-01-31 17:30:19 +0100858 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200859
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200860 LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n");
Philipp Maier246233d2020-08-18 15:15:24 +0200861
Ericee6958c2021-09-14 18:36:53 +0200862 /* we must have a free ep */
863 if (!endp) {
864 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_AVAIL));
865 LOGPENDP(endp, DLMGCP, LOGL_ERROR, "CRCX: no free endpoints available!\n");
866 return create_err_response(rq->trunk, NULL, 403, "CRCX", pdata->trans);
867 }
868
Philipp Maier8d6a1932020-06-18 12:19:31 +0200869 if (!mgcp_endp_avail(endp)) {
Pau Espin Pedrol907744e2021-06-04 17:57:34 +0200870 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_AVAIL));
Philipp Maiera910a812020-08-18 15:13:33 +0200871 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
872 "CRCX: selected endpoint not available!\n");
Ericfbcf4a62021-09-09 18:02:31 +0200873 return create_err_response(rq->trunk, NULL, 501, "CRCX", pdata->trans);
Philipp Maier8d6a1932020-06-18 12:19:31 +0200874 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200875
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200876 /* parse CallID C: and LocalParameters L: */
Philipp Maier8dc35972021-07-14 11:20:16 +0200877 for_each_line(line, pdata->save) {
Philipp Maier036612b2021-07-19 17:47:49 +0200878 if (!mgcp_check_param(endp, trunk, line))
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200879 continue;
880
Pau Espin Pedrol0c6c3c12019-06-25 17:18:12 +0200881 switch (toupper(line[0])) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200882 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200883 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200884 break;
885 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200886 callid = (const char *)line + 3;
887 break;
888 case 'I':
Philipp Maierffd75e42017-11-22 11:44:50 +0100889 /* It is illegal to send a connection identifier
890 * together with a CRCX, the MGW will assign the
891 * connection identifier by itself on CRCX */
Pau Espin Pedrol907744e2021-06-04 17:57:34 +0200892 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_BAD_ACTION));
Ericfbcf4a62021-09-09 18:02:31 +0200893 return create_err_response(rq->trunk, NULL, 523, "CRCX", pdata->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200894 break;
895 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200896 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200897 break;
898 case 'X':
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200899 if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200900 /* If osmux is disabled, just skip setting it up */
Pau Espin Pedrol928a20b2022-09-23 15:38:24 +0200901 if (rq->endp->trunk->cfg->osmux_use == OSMUX_USAGE_OFF)
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200902 break;
Pau Espin Pedrol21779192022-09-23 16:46:33 +0200903 remote_osmux_cid = mgcp_osmux_setup(endp, line);
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200904 break;
905 }
906
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200907 if (parse_x_osmo_ign(endp, line))
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200908 break;
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200909
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200910 /* Ignore unknown X-headers */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200911 break;
912 case '\0':
913 have_sdp = 1;
914 goto mgcp_header_done;
915 default:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200916 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
917 "CRCX: unhandled option: '%c'/%d\n", *line, *line);
Pau Espin Pedrol907744e2021-06-04 17:57:34 +0200918 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_UNHANDLED_PARAM));
Ericfbcf4a62021-09-09 18:02:31 +0200919 return create_err_response(rq->trunk, NULL, 539, "CRCX", pdata->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200920 break;
921 }
922 }
923
924mgcp_header_done:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200925 /* Check parameters */
926 if (!callid) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200927 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
928 "CRCX: insufficient parameters, missing callid\n");
Pau Espin Pedrol907744e2021-06-04 17:57:34 +0200929 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_MISSING_CALLID));
Ericfbcf4a62021-09-09 18:02:31 +0200930 return create_err_response(endp, endp, 516, "CRCX", pdata->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200931 }
932
Philipp Maier87bd9be2017-08-22 16:35:41 +0200933 if (!mode) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200934 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
935 "CRCX: insufficient parameters, missing mode\n");
Pau Espin Pedrol907744e2021-06-04 17:57:34 +0200936 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_INVALID_MODE));
Ericfbcf4a62021-09-09 18:02:31 +0200937 return create_err_response(endp, endp, 517, "CRCX", pdata->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200938 }
939
Philipp Maier87bd9be2017-08-22 16:35:41 +0200940 /* Check if we are able to accept the creation of another connection */
941 if (llist_count(&endp->conns) >= endp->type->max_conns) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200942 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
943 "CRCX: endpoint full, max. %i connections allowed!\n",
944 endp->type->max_conns);
Philipp Maier14b27a82020-06-02 20:15:30 +0200945 if (trunk->force_realloc) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200946 /* There is no more room for a connection, make some
947 * room by blindly tossing the oldest of the two two
948 * connections */
949 mgcp_conn_free_oldest(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200950 } else {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200951 /* There is no more room for a connection, leave
952 * everything as it is and return with an error */
Pau Espin Pedrol907744e2021-06-04 17:57:34 +0200953 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_LIMIT_EXCEEDED));
Ericfbcf4a62021-09-09 18:02:31 +0200954 return create_err_response(endp, endp, 540, "CRCX", pdata->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200955 }
956 }
957
Philipp Maier87bd9be2017-08-22 16:35:41 +0200958 /* Check if this endpoint already serves a call, if so, check if the
959 * callids match up so that we are sure that this is our call */
960 if (endp->callid && mgcp_verify_call_id(endp, callid)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200961 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
962 "CRCX: already seized by other call (%s)\n",
963 endp->callid);
Philipp Maier14b27a82020-06-02 20:15:30 +0200964 if (trunk->force_realloc)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200965 /* This is not our call, toss everything by releasing
966 * the entire endpoint. (rude!) */
Philipp Maier1355d7e2018-02-01 14:30:06 +0100967 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200968 else {
969 /* This is not our call, leave everything as it is and
970 * return with an error. */
Pau Espin Pedrol907744e2021-06-04 17:57:34 +0200971 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_UNKNOWN_CALLID));
Ericfbcf4a62021-09-09 18:02:31 +0200972 return create_err_response(endp, endp, 400, "CRCX", pdata->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200973 }
974 }
975
Philipp Maier889fe7f2020-07-06 17:44:12 +0200976 if (!endp->callid) {
977 /* Claim endpoint resources. This will also set the callid,
978 * creating additional connections will only be possible if
979 * the callid matches up (see above). */
980 rc = mgcp_endp_claim(endp, callid);
981 if (rc != 0) {
Pau Espin Pedrol907744e2021-06-04 17:57:34 +0200982 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_CLAIM));
Ericfbcf4a62021-09-09 18:02:31 +0200983 return create_err_response(endp, endp, 502, "CRCX", pdata->trans);
Philipp Maier889fe7f2020-07-06 17:44:12 +0200984 }
985 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200986
Philipp Maierffd75e42017-11-22 11:44:50 +0100987 snprintf(conn_name, sizeof(conn_name), "%s", callid);
Philipp Maier14b27a82020-06-02 20:15:30 +0200988 _conn = mgcp_conn_alloc(trunk->endpoints, endp, MGCP_CONN_TYPE_RTP, conn_name);
Philipp Maierffd75e42017-11-22 11:44:50 +0100989 if (!_conn) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200990 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
991 "CRCX: unable to allocate RTP connection\n");
Pau Espin Pedrol907744e2021-06-04 17:57:34 +0200992 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_ALLOC_CONN));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200993 goto error2;
994
Philipp Maier87bd9be2017-08-22 16:35:41 +0200995 }
Philipp Maier889fe7f2020-07-06 17:44:12 +0200996
Philipp Maierffd75e42017-11-22 11:44:50 +0100997 conn = mgcp_conn_get_rtp(endp, _conn->id);
998 OSMO_ASSERT(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200999
1000 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
1001 error_code = 517;
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001002 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_INVALID_MODE));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001003 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001004 }
1005
Pau Espin Pedrol21779192022-09-23 16:46:33 +02001006 /* If X-Osmux (remote CID) was received (-1 is wilcard), alloc next avail CID as local CID */
1007 if (remote_osmux_cid >= -1) {
Pau Espin Pedrold48a8112022-09-27 12:37:53 +02001008 if (osmux_init_conn(conn) < 0) {
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001009 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_NO_OSMUX));
Pau Espin Pedrol2b896172019-04-24 13:47:23 +02001010 goto error2;
1011 }
Pau Espin Pedrol21779192022-09-23 16:46:33 +02001012 if (remote_osmux_cid >= 0) {
1013 conn->osmux.remote_cid_present = true;
1014 conn->osmux.remote_cid = remote_osmux_cid;
1015 }
Pau Espin Pedrol928a20b2022-09-23 15:38:24 +02001016 } else if (endp->trunk->cfg->osmux_use == OSMUX_USAGE_ONLY) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001017 LOGPCONN(_conn, DLMGCP, LOGL_ERROR,
1018 "CRCX: osmux only and no osmux offered\n");
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001019 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_NO_OSMUX));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001020 goto error2;
1021 }
1022
Philipp Maierbc0346e2018-06-07 09:52:16 +02001023 /* Set local connection options, if present */
1024 if (local_options) {
Philipp Maier8dc35972021-07-14 11:20:16 +02001025 rc = set_local_cx_options(trunk->endpoints,
Philipp Maierbc0346e2018-06-07 09:52:16 +02001026 &endp->local_options, local_options);
1027 if (rc != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001028 LOGPCONN(_conn, DLMGCP, LOGL_ERROR,
1029 "CRCX: inavlid local connection options!\n");
Philipp Maierbc0346e2018-06-07 09:52:16 +02001030 error_code = rc;
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001031 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS));
Philipp Maierbc0346e2018-06-07 09:52:16 +02001032 goto error2;
1033 }
1034 }
1035
1036 /* Handle codec information and decide for a suitable codec */
Philipp Maier8dc35972021-07-14 11:20:16 +02001037 rc = handle_codec_info(conn, rq, have_sdp, true);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001038 mgcp_codec_summary(conn);
1039 if (rc) {
1040 error_code = rc;
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001041 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_CODEC_NEGOTIATION));
Philipp Maierbc0346e2018-06-07 09:52:16 +02001042 goto error2;
1043 }
Pau Espin Pedrolbb3ccde2021-12-23 19:49:26 +01001044 /* Upgrade the conn type RTP_DEFAULT->RTP_IUUP if needed based on requested codec: */
1045 /* TODO: "codec" probably needs to be moved from endp to conn */
1046 if (conn->type == MGCP_RTP_DEFAULT && strcmp(conn->end.codec->subtype_name, "VND.3GPP.IUFP") == 0) {
1047 rc = mgcp_conn_iuup_init(conn);
1048 }
Philipp Maierbc0346e2018-06-07 09:52:16 +02001049
Philipp Maier14b27a82020-06-02 20:15:30 +02001050 conn->end.fmtp_extra = talloc_strdup(trunk->endpoints,
1051 trunk->audio_fmtp_extra);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001052
Philipp Maier8dc35972021-07-14 11:20:16 +02001053 if (pdata->cfg->force_ptime) {
1054 conn->end.packet_duration_ms = pdata->cfg->force_ptime;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001055 conn->end.force_output_ptime = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001056 }
1057
Philipp Maier1cb1e382017-11-02 17:16:04 +01001058 mgcp_rtp_end_config(endp, 0, &conn->end);
1059
Philipp Maierc3cc6542018-02-02 12:58:42 +01001060 /* check connection mode setting */
1061 if (conn->conn->mode != MGCP_CONN_LOOPBACK
1062 && conn->conn->mode != MGCP_CONN_RECV_ONLY
Pau Espin Pedrol5ffd1272022-10-04 13:45:48 +02001063 && osmo_sockaddr_port(&conn->end.addr.u.sa) == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001064 LOGPCONN(_conn, DLMGCP, LOGL_ERROR,
1065 "CRCX: selected connection mode type requires an opposite end!\n");
Philipp Maierc3cc6542018-02-02 12:58:42 +01001066 error_code = 527;
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001067 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC));
Philipp Maierc3cc6542018-02-02 12:58:42 +01001068 goto error2;
1069 }
1070
Pau Espin Pedrol71d42e72020-09-03 14:20:07 +02001071 /* Find a local address for conn based on policy and initial SDP remote
1072 information, then find a free port for it */
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +02001073 if (mgcp_get_local_addr(conn->end.local_addr, conn) < 0) {
1074 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_BIND_PORT));
1075 goto error2;
1076 }
Philipp Maier1cb1e382017-11-02 17:16:04 +01001077 if (allocate_port(endp, conn) != 0) {
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001078 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_BIND_PORT));
Philipp Maier1cb1e382017-11-02 17:16:04 +01001079 goto error2;
1080 }
1081
Philipp Maier87bd9be2017-08-22 16:35:41 +02001082 if (setup_rtp_processing(endp, conn) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001083 LOGPCONN(_conn, DLMGCP, LOGL_ERROR,
1084 "CRCX: could not start RTP processing!\n");
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001085 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_START_RTP));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001086 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001087 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001088
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001089 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG,
1090 "CRCX: Creating connection: port: %u\n", conn->end.local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001091
Philipp Maiere726d4f2017-11-01 10:41:34 +01001092 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
Philipp Maier14b27a82020-06-02 20:15:30 +02001093 OSMO_ASSERT(trunk->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
Pau Espin Pedrolca280a12021-07-06 18:15:35 +02001094 if (conn->conn->mode & MGCP_CONN_RECV_ONLY &&
Pau Espin Pedrolca280a12021-07-06 18:15:35 +02001095 trunk->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001096 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001097
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001098 LOGPCONN(_conn, DLMGCP, LOGL_NOTICE,
1099 "CRCX: connection successfully created\n");
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001100 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_SUCCESS));
Philipp Maier889fe7f2020-07-06 17:44:12 +02001101 mgcp_endp_update(endp);
Philipp Maier41d59202021-07-20 15:49:00 +02001102
1103 /* NOTE: Only in the virtual trunk we allow dynamic endpoint names */
1104 bool add_epname = rq->wildcarded && trunk->trunk_type == MGCP_TRUNK_VIRTUAL;
1105 return create_response_with_sdp(endp, conn, "CRCX", pdata->trans, add_epname, true);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001106error2:
Philipp Maier1355d7e2018-02-01 14:30:06 +01001107 mgcp_endp_release(endp);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001108 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1109 "CRCX: unable to create connection\n");
Ericfbcf4a62021-09-09 18:02:31 +02001110 return create_err_response(endp, endp, error_code, "CRCX", pdata->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001111}
1112
Philipp Maier87bd9be2017-08-22 16:35:41 +02001113/* MDCX command handler, processes the received command */
Philipp Maier8dc35972021-07-14 11:20:16 +02001114static struct msgb *handle_modify_con(struct mgcp_request_data *rq)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001115{
Philipp Maier8dc35972021-07-14 11:20:16 +02001116 struct mgcp_parse_data *pdata = rq->pdata;
1117 struct mgcp_trunk *trunk = rq->trunk;
1118 struct mgcp_endpoint *endp = rq->endp;
1119 struct rate_ctr_group *rate_ctrs = trunk->ratectr.mgcp_mdcx_ctr_group;
Pau Espin Pedrolfbbe8f22020-09-03 14:49:56 +02001120 char new_local_addr[INET6_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001121 int error_code = 500;
1122 int silent = 0;
1123 int have_sdp = 0;
1124 char *line;
1125 const char *local_options = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001126 const char *mode = NULL;
1127 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier01d24a32017-11-21 17:26:09 +01001128 const char *conn_id = NULL;
Pau Espin Pedrol21779192022-09-23 16:46:33 +02001129 int remote_osmux_cid = -2;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001130 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001131
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001132 LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001133
Philipp Maier5656fbf2018-02-02 14:41:58 +01001134 /* Prohibit wildcarded requests */
Philipp Maier8dc35972021-07-14 11:20:16 +02001135 if (rq->wildcarded) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001136 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1137 "MDCX: wildcarded endpoint names not supported.\n");
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001138 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_WILDCARD));
Ericfbcf4a62021-09-09 18:02:31 +02001139 return create_err_response(rq->trunk, endp, 507, "MDCX", pdata->trans);
Philipp Maier5656fbf2018-02-02 14:41:58 +01001140 }
1141
Ericee6958c2021-09-14 18:36:53 +02001142 if (!endp || !mgcp_endp_avail(endp)) {
1143 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_AVAIL));
1144 LOGPENDP(endp, DLMGCP, LOGL_ERROR, "MDCX: selected endpoint not available!\n");
1145 return create_err_response(rq->trunk, NULL, 501, "MDCX", pdata->trans);
1146 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001147 if (llist_count(&endp->conns) <= 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001148 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1149 "MDCX: endpoint is not holding a connection.\n");
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001150 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_NO_CONN));
Ericfbcf4a62021-09-09 18:02:31 +02001151 return create_err_response(endp, endp, 400, "MDCX", pdata->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001152 }
1153
Philipp Maier8dc35972021-07-14 11:20:16 +02001154 for_each_line(line, pdata->save) {
Philipp Maier036612b2021-07-19 17:47:49 +02001155 if (!mgcp_check_param(endp, trunk, line))
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001156 continue;
1157
Pau Espin Pedrol0c6c3c12019-06-25 17:18:12 +02001158 switch (toupper(line[0])) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001159 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +01001160 if (mgcp_verify_call_id(endp, line + 3) != 0) {
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001161 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_INVALID_CALLID));
Harald Weltee35eeae2017-12-28 13:47:37 +01001162 error_code = 516;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001163 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001164 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001165 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001166 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001167 conn_id = (const char *)line + 3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001168 if ((error_code = mgcp_verify_ci(endp, conn_id))) {
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001169 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_INVALID_CONNID));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001170 goto error3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001171 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001172 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001173 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001174 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001175 break;
1176 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001177 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001178 break;
1179 case 'Z':
Pau Espin Pedrol9b508f62019-06-26 13:11:22 +02001180 silent = strcasecmp("noanswer", line + 3) == 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001181 break;
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001182 case 'X':
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +02001183 if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001184 /* If osmux is disabled, just skip setting it up */
Pau Espin Pedrol928a20b2022-09-23 15:38:24 +02001185 if (endp->trunk->cfg->osmux_use == OSMUX_USAGE_OFF)
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001186 break;
Pau Espin Pedrol21779192022-09-23 16:46:33 +02001187 remote_osmux_cid = mgcp_osmux_setup(endp, line);
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001188 break;
1189 }
1190 /* Ignore unknown X-headers */
1191 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001192 case '\0':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001193 have_sdp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001194 goto mgcp_header_done;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001195 break;
1196 default:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001197 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1198 "MDCX: Unhandled MGCP option: '%c'/%d\n",
1199 line[0], line[0]);
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001200 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_UNHANDLED_PARAM));
Ericfbcf4a62021-09-09 18:02:31 +02001201 return create_err_response(rq->trunk, NULL, 539, "MDCX", pdata->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001202 break;
1203 }
1204 }
1205
Philipp Maier87bd9be2017-08-22 16:35:41 +02001206mgcp_header_done:
Philipp Maier01d24a32017-11-21 17:26:09 +01001207 if (!conn_id) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001208 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1209 "MDCX: insufficient parameters, missing ci (connectionIdentifier)\n");
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001210 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_NO_CONNID));
Ericfbcf4a62021-09-09 18:02:31 +02001211 return create_err_response(endp, endp, 515, "MDCX", pdata->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001212 }
1213
1214 conn = mgcp_conn_get_rtp(endp, conn_id);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001215 if (!conn) {
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001216 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_CONN_NOT_FOUND));
Ericfbcf4a62021-09-09 18:02:31 +02001217 return create_err_response(endp, endp, 400, "MDCX", pdata->trans);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001218 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001219
Oliver Smithe36b7752019-01-22 16:31:36 +01001220 mgcp_conn_watchdog_kick(conn->conn);
1221
Philipp Maier87bd9be2017-08-22 16:35:41 +02001222 if (mode) {
1223 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001224 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_INVALID_MODE));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001225 error_code = 517;
1226 goto error3;
1227 }
1228 } else
Pau Espin Pedrol209eb9f2019-04-24 12:03:04 +02001229 conn->conn->mode = conn->conn->mode_orig;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001230
Philipp Maierbc0346e2018-06-07 09:52:16 +02001231 /* Set local connection options, if present */
1232 if (local_options) {
Philipp Maier8dc35972021-07-14 11:20:16 +02001233 rc = set_local_cx_options(trunk->endpoints,
Philipp Maierbc0346e2018-06-07 09:52:16 +02001234 &endp->local_options, local_options);
1235 if (rc != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001236 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1237 "MDCX: invalid local connection options!\n");
Philipp Maierbc0346e2018-06-07 09:52:16 +02001238 error_code = rc;
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001239 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS));
Philipp Maierbc0346e2018-06-07 09:52:16 +02001240 goto error3;
1241 }
1242 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001243
Philipp Maierbc0346e2018-06-07 09:52:16 +02001244 /* Handle codec information and decide for a suitable codec */
Philipp Maier8dc35972021-07-14 11:20:16 +02001245 rc = handle_codec_info(conn, rq, have_sdp, false);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001246 mgcp_codec_summary(conn);
1247 if (rc) {
Philipp Maieraf07f662018-02-02 11:34:02 +01001248 error_code = rc;
1249 goto error3;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001250 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001251
Philipp Maierc3cc6542018-02-02 12:58:42 +01001252 /* check connection mode setting */
1253 if (conn->conn->mode != MGCP_CONN_LOOPBACK
1254 && conn->conn->mode != MGCP_CONN_RECV_ONLY
Pau Espin Pedrold6769ea2021-07-06 19:44:27 +02001255 && !mgcp_rtp_end_remote_addr_available(&conn->end)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001256 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1257 "MDCX: selected connection mode type requires an opposite end!\n");
Philipp Maierc3cc6542018-02-02 12:58:42 +01001258 error_code = 527;
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001259 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC));
Philipp Maierc3cc6542018-02-02 12:58:42 +01001260 goto error3;
1261 }
1262
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001263 if (mgcp_conn_rtp_is_osmux(conn)) {
Pau Espin Pedrol21779192022-09-23 16:46:33 +02001264 OSMO_ASSERT(conn->osmux.local_cid_allocated);
1265 if (remote_osmux_cid < -1) {
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001266 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1267 "MDCX: Failed to parse Osmux CID!\n");
1268 goto error3;
Pau Espin Pedrol21779192022-09-23 16:46:33 +02001269 } else if (remote_osmux_cid == -1) {
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001270 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1271 "MDCX: wilcard in MDCX is not supported!\n");
1272 goto error3;
Pau Espin Pedrol21779192022-09-23 16:46:33 +02001273 } else if (conn->osmux.remote_cid_present &&
1274 remote_osmux_cid != (int) conn->osmux.remote_cid) {
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001275 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1276 "MDCX: changing already allocated CID is not supported!\n");
1277 goto error3;
1278 }
Pau Espin Pedrol21779192022-09-23 16:46:33 +02001279 conn->osmux.remote_cid_present = true;
1280 conn->osmux.remote_cid = remote_osmux_cid;
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001281 }
Philipp Maierbc0346e2018-06-07 09:52:16 +02001282
Pau Espin Pedrolfbbe8f22020-09-03 14:49:56 +02001283 /* MDCX may have provided a new remote address, which means we may need
1284 to update our announced IP addr and re-bind our local end. This can
1285 happen for instance if MGW initially provided an IPv4 during CRCX
1286 ACK, and now MDCX tells us the remote has an IPv6 address. */
Pau Espin Pedrol70c03f52022-10-04 16:49:41 +02001287 if (mgcp_get_local_addr(new_local_addr, conn) < 0) {
1288 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_BIND_PORT));
1289 goto error3;
1290 }
Pau Espin Pedrolfbbe8f22020-09-03 14:49:56 +02001291 if (strcmp(new_local_addr, conn->end.local_addr)) {
1292 osmo_strlcpy(conn->end.local_addr, new_local_addr, sizeof(conn->end.local_addr));
1293 mgcp_free_rtp_port(&conn->end);
1294 if (allocate_port(endp, conn) != 0) {
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001295 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_CRCX_FAIL_BIND_PORT));
Pau Espin Pedrolfbbe8f22020-09-03 14:49:56 +02001296 goto error3;
1297 }
1298 }
1299
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001300 if (setup_rtp_processing(endp, conn) != 0) {
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001301 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_FAIL_START_RTP));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001302 goto error3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001303 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001304
Philipp Maier87bd9be2017-08-22 16:35:41 +02001305 mgcp_rtp_end_config(endp, 1, &conn->end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001306
1307 /* modify */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001308 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG,
1309 "MDCX: modified conn:%s\n", mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001310
Philipp Maiere726d4f2017-11-01 10:41:34 +01001311 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
Philipp Maier8dc35972021-07-14 11:20:16 +02001312 OSMO_ASSERT(trunk->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
Pau Espin Pedrolca280a12021-07-06 18:15:35 +02001313 if (conn->conn->mode & MGCP_CONN_RECV_ONLY &&
Philipp Maier8dc35972021-07-14 11:20:16 +02001314 trunk->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001315 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001316
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001317 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_MDCX_SUCCESS));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001318 if (silent)
1319 goto out_silent;
1320
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001321 LOGPCONN(conn->conn, DLMGCP, LOGL_NOTICE,
1322 "MDCX: connection successfully modified\n");
Philipp Maier889fe7f2020-07-06 17:44:12 +02001323 mgcp_endp_update(endp);
Philipp Maier41d59202021-07-20 15:49:00 +02001324 return create_response_with_sdp(endp, conn, "MDCX", pdata->trans, false, false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001325error3:
Ericfbcf4a62021-09-09 18:02:31 +02001326 return create_err_response(endp, endp, error_code, "MDCX", pdata->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001327
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001328out_silent:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001329 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "MDCX: silent exit\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001330 return NULL;
1331}
1332
Philipp Maier87bd9be2017-08-22 16:35:41 +02001333/* DLCX command handler, processes the received command */
Philipp Maier8dc35972021-07-14 11:20:16 +02001334static struct msgb *handle_delete_con(struct mgcp_request_data *rq)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001335{
Philipp Maier8dc35972021-07-14 11:20:16 +02001336 struct mgcp_parse_data *pdata = rq->pdata;
1337 struct mgcp_trunk *trunk = rq->trunk;
1338 struct mgcp_endpoint *endp = rq->endp;
1339 struct rate_ctr_group *rate_ctrs = trunk->ratectr.mgcp_dlcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001340 int error_code = 400;
1341 int silent = 0;
1342 char *line;
1343 char stats[1048];
Philipp Maier01d24a32017-11-21 17:26:09 +01001344 const char *conn_id = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001345 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierf486e742021-07-19 14:56:29 +02001346 unsigned int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001347
Philipp Maierf486e742021-07-19 14:56:29 +02001348 /* NOTE: In this handler we can not take it for granted that the endp
1349 * pointer will be populated, however a trunk is always guaranteed. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001350
Philipp Maierf486e742021-07-19 14:56:29 +02001351 LOGPEPTR(endp, trunk, DLMGCP, LOGL_NOTICE, "DLCX: deleting connection(s) ...\n");
1352
1353 if (endp && !mgcp_endp_avail(endp)) {
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001354 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_FAIL_AVAIL));
Philipp Maiera910a812020-08-18 15:13:33 +02001355 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1356 "DLCX: selected endpoint not available!\n");
Ericfbcf4a62021-09-09 18:02:31 +02001357 return create_err_response(rq->trunk, NULL, 501, "DLCX", pdata->trans);
Philipp Maier8d6a1932020-06-18 12:19:31 +02001358 }
1359
Philipp Maierf486e742021-07-19 14:56:29 +02001360 if (endp && !rq->wildcarded && llist_empty(&endp->conns)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001361 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1362 "DLCX: endpoint is not holding a connection.\n");
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001363 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_FAIL_NO_CONN));
Ericfbcf4a62021-09-09 18:02:31 +02001364 return create_err_response(endp, endp, 515, "DLCX", pdata->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001365 }
1366
Ericee6958c2021-09-14 18:36:53 +02001367 /* Handle wildcarded DLCX that refers to the whole trunk. This means
1368 * that we walk over all endpoints on the trunk in order to drop all
1369 * connections on the trunk. (see also RFC3435 Annex F.7) */
1370 if (rq->wildcarded) {
1371 int num_conns = 0;
1372 for (i = 0; i < trunk->number_endpoints; i++) {
1373 num_conns += llist_count(&trunk->endpoints[i]->conns);
1374 mgcp_endp_release(trunk->endpoints[i]);
1375 }
1376 rate_ctr_add(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_SUCCESS), num_conns);
1377 return create_ok_response(trunk, NULL, 200, "DLCX", pdata->trans);
1378 }
1379
Philipp Maier8dc35972021-07-14 11:20:16 +02001380 for_each_line(line, pdata->save) {
Philipp Maier036612b2021-07-19 17:47:49 +02001381 if (!mgcp_check_param(endp, trunk, line))
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001382 continue;
1383
Pau Espin Pedrol0c6c3c12019-06-25 17:18:12 +02001384 switch (toupper(line[0])) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001385 case 'C':
Philipp Maierf486e742021-07-19 14:56:29 +02001386 /* If we have no endpoint, but a call id in the request,
1387 then this request cannot be handled */
1388 if (!endp) {
1389 LOGPTRUNK(trunk, DLMGCP, LOGL_NOTICE,
1390 "cannot handle requests with call-id (C) without endpoint -- abort!");
1391 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_FAIL_UNHANDLED_PARAM));
Ericfbcf4a62021-09-09 18:02:31 +02001392 return create_err_response(rq->trunk, NULL, 539, "DLCX", pdata->trans);
Philipp Maierf486e742021-07-19 14:56:29 +02001393 }
1394
Harald Weltee35eeae2017-12-28 13:47:37 +01001395 if (mgcp_verify_call_id(endp, line + 3) != 0) {
1396 error_code = 516;
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001397 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_FAIL_INVALID_CALLID));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001398 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001399 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001400 break;
1401 case 'I':
Philipp Maierf486e742021-07-19 14:56:29 +02001402 /* If we have no endpoint, but a connection id in the request,
1403 then this request cannot be handled */
1404 if (!endp) {
1405 LOGPTRUNK(trunk, DLMGCP, LOGL_NOTICE,
1406 "cannot handle requests with conn-id (I) without endpoint -- abort!");
1407 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_FAIL_UNHANDLED_PARAM));
Ericfbcf4a62021-09-09 18:02:31 +02001408 return create_err_response(rq->trunk, NULL, 539, "DLCX", pdata->trans);
Philipp Maierf486e742021-07-19 14:56:29 +02001409 }
1410
Philipp Maier01d24a32017-11-21 17:26:09 +01001411 conn_id = (const char *)line + 3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001412 if ((error_code = mgcp_verify_ci(endp, conn_id))) {
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001413 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_FAIL_INVALID_CONNID));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001414 goto error3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001415 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001416 break;
1417 case 'Z':
Pau Espin Pedrol9b508f62019-06-26 13:11:22 +02001418 silent = strcasecmp("noanswer", line + 3) == 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001419 break;
1420 default:
Philipp Maierf486e742021-07-19 14:56:29 +02001421 LOGPEPTR(endp, trunk, DLMGCP, LOGL_NOTICE, "DLCX: Unhandled MGCP option: '%c'/%d\n",
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001422 line[0], line[0]);
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001423 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_FAIL_UNHANDLED_PARAM));
Ericfbcf4a62021-09-09 18:02:31 +02001424 return create_err_response(rq->trunk, NULL, 539, "DLCX", pdata->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001425 break;
1426 }
1427 }
1428
Philipp Maierce187052021-07-23 10:58:19 +02001429 /* The logic does not permit to go past this point without having the
1430 * the endp pointer populated. */
1431 OSMO_ASSERT(endp);
1432
Philipp Maierf4c0e372017-10-11 16:06:45 +02001433 /* When no connection id is supplied, we will interpret this as a
Philipp Maierf486e742021-07-19 14:56:29 +02001434 * wildcarded DLCX that refers to the selected endpoint. This means
1435 * that we drop all connections on that specific endpoint at once.
1436 * (See also RFC3435 Section F.7) */
Philipp Maier01d24a32017-11-21 17:26:09 +01001437 if (!conn_id) {
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001438 int num_conns = llist_count(&endp->conns);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001439 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1440 "DLCX: missing ci (connectionIdentifier), will remove all connections (%d total) at once\n",
1441 num_conns);
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001442
1443 if (num_conns > 0)
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001444 rate_ctr_add(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_SUCCESS), num_conns);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001445
Philipp Maier1355d7e2018-02-01 14:30:06 +01001446 mgcp_endp_release(endp);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001447
1448 /* Note: In this case we do not return any statistics,
1449 * as we assume that the client is not interested in
1450 * this case. */
Ericfbcf4a62021-09-09 18:02:31 +02001451 return create_ok_response(endp, endp, 200, "DLCX", pdata->trans);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001452 }
1453
Philipp Maierf4c0e372017-10-11 16:06:45 +02001454 /* Find the connection */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001455 conn = mgcp_conn_get_rtp(endp, conn_id);
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001456 if (!conn) {
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001457 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_FAIL_INVALID_CONNID));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001458 goto error3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001459 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001460 /* save the statistics of the current connection */
1461 mgcp_format_stats(stats, sizeof(stats), conn->conn);
1462
1463 /* delete connection */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001464 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG, "DLCX: deleting conn:%s\n",
1465 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001466 mgcp_conn_free(endp, conn_id);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001467 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1468 "DLCX: connection successfully deleted\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001469
1470 /* When all connections are closed, the endpoint will be released
1471 * in order to be ready to be used by another call. */
1472 if (llist_count(&endp->conns) <= 0) {
Philipp Maier1355d7e2018-02-01 14:30:06 +01001473 mgcp_endp_release(endp);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001474 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "DLCX: endpoint released\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001475 }
1476
Pau Espin Pedrol907744e2021-06-04 17:57:34 +02001477 rate_ctr_inc(rate_ctr_group_get_ctr(rate_ctrs, MGCP_DLCX_SUCCESS));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001478 if (silent)
1479 goto out_silent;
Ericfbcf4a62021-09-09 18:02:31 +02001480 return create_ok_resp_with_param(endp, endp, 250, "DLCX", pdata->trans, stats);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001481
1482error3:
Ericfbcf4a62021-09-09 18:02:31 +02001483 return create_err_response(endp, endp, error_code, "DLCX", pdata->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001484
1485out_silent:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001486 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "DLCX: silent exit\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001487 return NULL;
1488}
1489
Philipp Maier87bd9be2017-08-22 16:35:41 +02001490/* RSIP command handler, processes the received command */
Philipp Maier8dc35972021-07-14 11:20:16 +02001491static struct msgb *handle_rsip(struct mgcp_request_data *rq)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001492{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001493 /* TODO: Also implement the resetting of a specific endpoint
1494 * to make mgcp_send_reset_ep() work. Currently this will call
1495 * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1
1496 * to make read_call_agent() reset all endpoints when called
1497 * next time. In order to selectively reset endpoints some
1498 * mechanism to distinguish which endpoint shall be resetted
1499 * is needed */
1500
1501 LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n");
1502
Philipp Maier8dc35972021-07-14 11:20:16 +02001503 if (rq->pdata->cfg->reset_cb)
1504 rq->pdata->cfg->reset_cb(rq->endp->trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001505 return NULL;
1506}
1507
1508static char extract_tone(const char *line)
1509{
1510 const char *str = strstr(line, "D/");
1511 if (!str)
1512 return CHAR_MAX;
1513
1514 return str[2];
1515}
1516
Philipp Maier87bd9be2017-08-22 16:35:41 +02001517/* This can request like DTMF detection and forward, fax detection... it
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001518 * can also request when the notification should be send and such. We don't
Philipp Maier87bd9be2017-08-22 16:35:41 +02001519 * do this right now. */
Philipp Maier8dc35972021-07-14 11:20:16 +02001520static struct msgb *handle_noti_req(struct mgcp_request_data *rq)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001521{
1522 int res = 0;
1523 char *line;
1524 char tone = CHAR_MAX;
1525
Philipp Maier87bd9be2017-08-22 16:35:41 +02001526 LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n");
1527
Philipp Maier8dc35972021-07-14 11:20:16 +02001528 for_each_line(line, rq->pdata->save) {
Pau Espin Pedrol0c6c3c12019-06-25 17:18:12 +02001529 switch (toupper(line[0])) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001530 case 'S':
1531 tone = extract_tone(line);
1532 break;
1533 }
1534 }
1535
1536 /* we didn't see a signal request with a tone */
1537 if (tone == CHAR_MAX)
Ericfbcf4a62021-09-09 18:02:31 +02001538 return create_ok_response(rq->endp, rq->endp, 200, "RQNT", rq->pdata->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001539
Philipp Maier8dc35972021-07-14 11:20:16 +02001540 if (rq->pdata->cfg->rqnt_cb)
1541 res = rq->pdata->cfg->rqnt_cb(rq->endp, tone);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001542
Ericfbcf4a62021-09-09 18:02:31 +02001543 return res == 0 ? create_ok_response(rq->endp, rq->endp, 200, "RQNT", rq->pdata->trans) :
1544 create_err_response(rq->endp, rq->endp, res, "RQNT", rq->pdata->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001545}
1546
Philipp Maier87bd9be2017-08-22 16:35:41 +02001547/* Connection keepalive timer, will take care that dummy packets are send
Harald Welte1d1b98f2017-12-25 10:03:40 +01001548 * regularly, so that NAT connections stay open */
Philipp Maier14b27a82020-06-02 20:15:30 +02001549static void mgcp_keepalive_timer_cb(void *_trunk)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001550{
Philipp Maier14b27a82020-06-02 20:15:30 +02001551 struct mgcp_trunk *trunk = _trunk;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001552 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001553 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001554
Philipp Maiere726d4f2017-11-01 10:41:34 +01001555 LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n",
Philipp Maier14b27a82020-06-02 20:15:30 +02001556 trunk->trunk_nr);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001557
Philipp Maiere726d4f2017-11-01 10:41:34 +01001558 /* Do not accept invalid configuration values
1559 * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and
1560 * values greater 0 */
Philipp Maier14b27a82020-06-02 20:15:30 +02001561 OSMO_ASSERT(trunk->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
Philipp Maiere726d4f2017-11-01 10:41:34 +01001562
1563 /* The dummy packet functionality has been disabled, we will exit
1564 * immediately, no further timer is scheduled, which means we will no
1565 * longer send dummy packets even when we did before */
Philipp Maier14b27a82020-06-02 20:15:30 +02001566 if (trunk->keepalive_interval == MGCP_KEEPALIVE_NEVER)
Philipp Maiere726d4f2017-11-01 10:41:34 +01001567 return;
1568
1569 /* In cases where only one dummy packet is sent, we do not need
1570 * the timer since the functions that handle the CRCX and MDCX are
1571 * triggering the sending of the dummy packet. So we behave like in
1572 * the MGCP_KEEPALIVE_NEVER case */
Philipp Maier14b27a82020-06-02 20:15:30 +02001573 if (trunk->keepalive_interval == MGCP_KEEPALIVE_ONCE)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001574 return;
1575
Philipp Maier87bd9be2017-08-22 16:35:41 +02001576 /* Send walk over all endpoints and send out dummy packets through
1577 * every connection present on each endpoint */
Philipp Maier4131a652021-07-07 14:04:34 +02001578 for (i = 0; i < trunk->number_endpoints; ++i) {
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001579 struct mgcp_endpoint *endp = trunk->endpoints[i];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001580 llist_for_each_entry(conn, &endp->conns, entry) {
Pau Espin Pedrolca280a12021-07-06 18:15:35 +02001581 if (conn->type == MGCP_CONN_TYPE_RTP &&
Pau Espin Pedrol2ca48822022-10-06 10:58:10 +02001582 conn->mode == MGCP_CONN_RECV_ONLY)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001583 send_dummy(endp, &conn->u.rtp);
1584 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001585 }
1586
Philipp Maiere726d4f2017-11-01 10:41:34 +01001587 /* Schedule the keepalive timer for the next round */
1588 LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n",
Philipp Maier14b27a82020-06-02 20:15:30 +02001589 trunk->trunk_nr);
1590 osmo_timer_schedule(&trunk->keepalive_timer, trunk->keepalive_interval,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001591 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001592}
1593
Philipp Maier14b27a82020-06-02 20:15:30 +02001594void mgcp_trunk_set_keepalive(struct mgcp_trunk *trunk, int interval)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001595{
Philipp Maier14b27a82020-06-02 20:15:30 +02001596 trunk->keepalive_interval = interval;
1597 osmo_timer_setup(&trunk->keepalive_timer, mgcp_keepalive_timer_cb, trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001598
1599 if (interval <= 0)
Philipp Maier14b27a82020-06-02 20:15:30 +02001600 osmo_timer_del(&trunk->keepalive_timer);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001601 else
Philipp Maier14b27a82020-06-02 20:15:30 +02001602 osmo_timer_schedule(&trunk->keepalive_timer,
1603 trunk->keepalive_interval, 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001604}
1605
Philipp Maier38533ba2021-07-29 17:38:34 +02001606/* Free config, this function is automatically called by talloc_free when the configuration is freed. */
1607static int config_free_talloc_destructor(struct mgcp_config *cfg)
1608{
1609 mgcp_ratectr_global_free(cfg);
1610 return 0;
1611}
1612
Philipp Maier87bd9be2017-08-22 16:35:41 +02001613/*! allocate configuration with default values.
1614 * (called once at startup by main function) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001615struct mgcp_config *mgcp_config_alloc(void)
1616{
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001617 /* FIXME: This is unrelated to the protocol, put this in some
1618 * appropiate place! */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001619 struct mgcp_config *cfg;
1620
1621 cfg = talloc_zero(NULL, struct mgcp_config);
1622 if (!cfg) {
1623 LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n");
1624 return NULL;
1625 }
1626
Philipp Maier12943ea2018-01-17 15:40:25 +01001627 osmo_strlcpy(cfg->domain, "mgw", sizeof(cfg->domain));
1628
Eric55fdfc22021-08-13 00:14:18 +02001629 cfg->net_ports.lock = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001630 cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START;
1631 cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END;
1632 cfg->net_ports.last_port = cfg->net_ports.range_start;
1633
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001634 cfg->source_port = 2427;
Eric2764bdb2021-08-23 22:11:47 +02001635 osmo_strlcpy(cfg->source_addr, "0.0.0.0", sizeof(cfg->source_addr));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001636
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001637 cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
1638 cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
1639
1640 cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
1641
Philipp Maierd19de2e2020-06-03 13:55:33 +02001642 INIT_LLIST_HEAD(&cfg->trunks);
1643
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001644 /* Allocate virtual trunk */
Philipp Maierd19de2e2020-06-03 13:55:33 +02001645 if (!mgcp_trunk_alloc(cfg, MGCP_TRUNK_VIRTUAL, MGCP_VIRT_TRUNK_ID)) {
Harald Welte3ac604e2019-05-08 14:07:41 +02001646 talloc_free(cfg);
1647 return NULL;
1648 }
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001649
Philipp Maiera065e632021-07-09 13:22:42 +02001650 mgcp_ratectr_global_alloc(cfg);
Philipp Maier38533ba2021-07-29 17:38:34 +02001651 talloc_set_destructor(cfg, config_free_talloc_destructor);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001652
1653 return cfg;
1654}
1655
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001656static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1657{
1658 return write(cfg->gw_fd.bfd.fd, buf, len);
1659}
1660
Philipp Maier87bd9be2017-08-22 16:35:41 +02001661/*! Reset all endpoints by sending RSIP message to self.
1662 * (called by VTY)
1663 * \param[in] endp trunk endpoint
1664 * \param[in] endpoint number
1665 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001666int mgcp_send_reset_all(struct mgcp_config *cfg)
1667{
Philipp Maier12943ea2018-01-17 15:40:25 +01001668 char buf[MGCP_ENDPOINT_MAXLEN + 128];
1669 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001670 int rc;
1671
Philipp Maier12943ea2018-01-17 15:40:25 +01001672 len = snprintf(buf, sizeof(buf),
1673 "RSIP 1 *@%s MGCP 1.0\r\n", cfg->domain);
1674 if (len < 0)
1675 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001676
Philipp Maier12943ea2018-01-17 15:40:25 +01001677 rc = send_agent(cfg, buf, len);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001678 if (rc <= 0)
1679 return -1;
1680
1681 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001682}
1683
Philipp Maier87bd9be2017-08-22 16:35:41 +02001684/*! Reset a single endpoint by sending RSIP message to self.
1685 * (called by VTY)
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001686 * \param[in] endp to reset
Philipp Maier87bd9be2017-08-22 16:35:41 +02001687 * \returns 0 on success, -1 on error */
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001688int mgcp_send_reset_ep(struct mgcp_endpoint *endp)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001689{
Philipp Maier12943ea2018-01-17 15:40:25 +01001690 char buf[MGCP_ENDPOINT_MAXLEN + 128];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001691 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001692 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001693
1694 len = snprintf(buf, sizeof(buf),
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001695 "RSIP 39 %s MGCP 1.0\r\n", endp->name);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001696 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001697 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001698
Ericfbf78d12021-08-23 22:31:39 +02001699 rc = send_agent(endp->trunk->cfg, buf, len);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001700 if (rc <= 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001701 return -1;
1702
Philipp Maier87bd9be2017-08-22 16:35:41 +02001703 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001704}