blob: f1414858c7c35d937fd5ab3199090ea1e3ced8b8 [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>
31
32#include <osmocom/core/msgb.h>
33#include <osmocom/core/talloc.h>
34#include <osmocom/core/select.h>
Stefan Sperling1e174872018-10-25 18:36:10 +020035#include <osmocom/core/stats.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020036
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 Maier87bd9be2017-08-22 16:35:41 +020039#include <osmocom/mgcp/mgcp_internal.h>
40#include <osmocom/mgcp/mgcp_stat.h>
41#include <osmocom/mgcp/mgcp_msg.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010042#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maier8970c492017-10-11 13:33:42 +020043#include <osmocom/mgcp/mgcp_sdp.h>
Philipp Maierbc0346e2018-06-07 09:52:16 +020044#include <osmocom/mgcp/mgcp_codec.h>
Stefan Sperlingba25eab2018-10-30 14:32:31 +010045#include <osmocom/mgcp/mgcp_conn.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020046
47struct mgcp_request {
48 char *name;
Philipp Maier87bd9be2017-08-22 16:35:41 +020049 struct msgb *(*handle_request) (struct mgcp_parse_data * data);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020050 char *debug_name;
51};
52
53#define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \
54 { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME },
55
Stefan Sperling1e174872018-10-25 18:36:10 +020056static const struct rate_ctr_desc mgcp_crcx_ctr_desc[] = {
57 [MGCP_CRCX_SUCCESS] = {"crcx:success", "CRCX command processed successfully."},
58 [MGCP_CRCX_FAIL_BAD_ACTION] = {"crcx:bad_action", "bad action in CRCX command."},
59 [MGCP_CRCX_FAIL_UNHANDLED_PARAM] = {"crcx:unhandled_param", "unhandled parameter in CRCX command."},
60 [MGCP_CRCX_FAIL_MISSING_CALLID] = {"crcx:missing_callid", "missing CallId in CRCX command."},
Stefan Sperlingaa823bf2018-10-29 14:51:41 +010061 [MGCP_CRCX_FAIL_INVALID_MODE] = {"crcx:invalid_mode", "invalid connection mode in CRCX command."},
Stefan Sperling1e174872018-10-25 18:36:10 +020062 [MGCP_CRCX_FAIL_LIMIT_EXCEEDED] = {"crcx:limit_exceeded", "limit of concurrent connections was reached."},
63 [MGCP_CRCX_FAIL_UNKNOWN_CALLID] = {"crcx:unkown_callid", "unknown CallId in CRCX command."},
64 [MGCP_CRCX_FAIL_ALLOC_CONN] = {"crcx:alloc_conn_fail", "connection allocation failure."},
65 [MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC] = {"crcx:no_remote_conn_desc", "no opposite end specified for connection."},
66 [MGCP_CRCX_FAIL_START_RTP] = {"crcx:start_rtp_failure", "failure to start RTP processing."},
67 [MGCP_CRCX_FAIL_REJECTED_BY_POLICY] = {"crcx:conn_rejected", "connection rejected by policy."},
Stefan Sperlinga714abf2018-10-29 14:19:54 +010068 [MGCP_CRCX_FAIL_NO_OSMUX] = {"crcx:no_osmux", "no osmux offered by peer."},
69 [MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS] = {"crcx:conn_opt", "connection options invalid."},
70 [MGCP_CRCX_FAIL_CODEC_NEGOTIATION] = {"crcx:codec_nego", "codec negotiation failure."},
71 [MGCP_CRCX_FAIL_BIND_PORT] = {"crcx:bind_port", "port bind failure."},
Stefan Sperling1e174872018-10-25 18:36:10 +020072};
73
74const static struct rate_ctr_group_desc mgcp_crcx_ctr_group_desc = {
75 .group_name_prefix = "crcx",
76 .group_description = "crxc statistics",
77 .class_id = OSMO_STATS_CLASS_GLOBAL,
78 .num_ctr = ARRAY_SIZE(mgcp_crcx_ctr_desc),
79 .ctr_desc = mgcp_crcx_ctr_desc
80};
81
Stefan Sperlingaa823bf2018-10-29 14:51:41 +010082static const struct rate_ctr_desc mgcp_mdcx_ctr_desc[] = {
83 [MGCP_MDCX_SUCCESS] = {"mdcx:success", "MDCX command processed successfully."},
84 [MGCP_MDCX_FAIL_WILDCARD] = {"mdcx:wildcard", "wildcard endpoint names in MDCX commands are unsupported."},
85 [MGCP_MDCX_FAIL_NO_CONN] = {"mdcx:no_conn", "endpoint specified in MDCX command has no active connections."},
86 [MGCP_MDCX_FAIL_INVALID_CALLID] = {"mdcx:callid", "invalid CallId specified in MDCX command."},
87 [MGCP_MDCX_FAIL_INVALID_CONNID] = {"mdcx:connid", "invalid connection ID specified in MDCX command."},
88 [MGCP_MDCX_FAIL_UNHANDLED_PARAM] = {"crcx:unhandled_param", "unhandled parameter in MDCX command."},
89 [MGCP_MDCX_FAIL_NO_CONNID] = {"mdcx:no_connid", "no connection ID specified in MDCX command."},
90 [MGCP_MDCX_FAIL_CONN_NOT_FOUND] = {"mdcx:conn_not_found", "connection specified in MDCX command does not exist."},
91 [MGCP_MDCX_FAIL_INVALID_MODE] = {"mdcx:invalid_mode", "invalid connection mode in MDCX command."},
92 [MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS] = {"mdcx:conn_opt", "connection options invalid."},
93 [MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC] = {"mdcx:no_remote_conn_desc", "no opposite end specified for connection."},
94 [MGCP_MDCX_FAIL_START_RTP] = {"mdcx:start_rtp_failure", "failure to start RTP processing."},
95 [MGCP_MDCX_FAIL_REJECTED_BY_POLICY] = {"mdcx:conn_rejected", "connection rejected by policy."},
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +010096 [MGCP_MDCX_DEFERRED_BY_POLICY] = {"mdcx:conn_deferred", "connection deferred by policy."},
Stefan Sperlingaa823bf2018-10-29 14:51:41 +010097};
98
99const static struct rate_ctr_group_desc mgcp_mdcx_ctr_group_desc = {
100 .group_name_prefix = "mdcx",
101 .group_description = "mdcx statistics",
102 .class_id = OSMO_STATS_CLASS_GLOBAL,
103 .num_ctr = ARRAY_SIZE(mgcp_mdcx_ctr_desc),
104 .ctr_desc = mgcp_mdcx_ctr_desc
105};
106
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +0100107static const struct rate_ctr_desc mgcp_dlcx_ctr_desc[] = {
108 [MGCP_DLCX_SUCCESS] = {"dlcx:success", "DLCX command processed successfully."},
109 [MGCP_DLCX_FAIL_WILDCARD] = {"dlcx:wildcard", "wildcard names in DLCX commands are unsupported."},
110 [MGCP_DLCX_FAIL_NO_CONN] = {"dlcx:no_conn", "endpoint specified in DLCX command has no active connections."},
111 [MGCP_DLCX_FAIL_INVALID_CALLID] = {"dlcx:callid", "CallId specified in DLCX command mismatches endpoint's CallId ."},
112 [MGCP_DLCX_FAIL_INVALID_CONNID] = {"dlcx:connid", "connection ID specified in DLCX command does not exist on endpoint."},
113 [MGCP_DLCX_FAIL_UNHANDLED_PARAM] = {"dlcx:unhandled_param", "unhandled parameter in DLCX command."},
114 [MGCP_DLCX_FAIL_REJECTED_BY_POLICY] = {"dlcx:rejected", "connection deletion rejected by policy."},
115 [MGCP_DLCX_DEFERRED_BY_POLICY] = {"dlcx:deferred", "connection deletion deferred by policy."},
116};
117
118const static struct rate_ctr_group_desc mgcp_dlcx_ctr_group_desc = {
119 .group_name_prefix = "dlcx",
120 .group_description = "dlcx statistics",
121 .class_id = OSMO_STATS_CLASS_GLOBAL,
122 .num_ctr = ARRAY_SIZE(mgcp_dlcx_ctr_desc),
123 .ctr_desc = mgcp_dlcx_ctr_desc
124};
125
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100126const static struct rate_ctr_group_desc all_rtp_conn_rate_ctr_group_desc = {
127 .group_name_prefix = "all_rtp_conn",
128 .group_description = "aggregated statistics for all rtp connections",
129 .class_id = 1,
130 .num_ctr = ARRAY_SIZE(all_rtp_conn_rate_ctr_desc),
131 .ctr_desc = all_rtp_conn_rate_ctr_desc
132};
133
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200134static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *data);
135static struct msgb *handle_create_con(struct mgcp_parse_data *data);
136static struct msgb *handle_delete_con(struct mgcp_parse_data *data);
137static struct msgb *handle_modify_con(struct mgcp_parse_data *data);
138static struct msgb *handle_rsip(struct mgcp_parse_data *data);
139static struct msgb *handle_noti_req(struct mgcp_parse_data *data);
140
Philipp Maier87bd9be2017-08-22 16:35:41 +0200141/* Initalize transcoder */
142static int setup_rtp_processing(struct mgcp_endpoint *endp,
143 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200144{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200145 struct mgcp_config *cfg = endp->cfg;
146 struct mgcp_conn_rtp *conn_src = NULL;
147 struct mgcp_conn_rtp *conn_dst = conn;
148 struct mgcp_conn *_conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200149
Philipp Maier87bd9be2017-08-22 16:35:41 +0200150 if (conn->type != MGCP_RTP_DEFAULT) {
151 LOGP(DLMGCP, LOGL_NOTICE,
152 "endpoint:%x RTP-setup: Endpoint is not configured as RTP default, stopping here!\n",
153 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200154 return 0;
155 }
156
Philipp Maier87bd9be2017-08-22 16:35:41 +0200157 if (conn->conn->mode == MGCP_CONN_LOOPBACK) {
158 LOGP(DLMGCP, LOGL_NOTICE,
159 "endpoint:%x RTP-setup: Endpoint is in loopback mode, stopping here!\n",
160 ENDPOINT_NUMBER(endp));
161 return 0;
162 }
163
164 /* Find the "sister" connection */
165 llist_for_each_entry(_conn, &endp->conns, entry) {
166 if (_conn->id != conn->conn->id) {
167 conn_src = &_conn->u.rtp;
168 break;
169 }
170 }
171
Philipp Maieracc10352018-07-19 18:07:57 +0200172 return cfg->setup_rtp_processing_cb(endp, conn_dst, conn_src);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200173}
174
Philipp Maier87bd9be2017-08-22 16:35:41 +0200175/* array of function pointers for handling various
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200176 * messages. In the future this might be binary sorted
Philipp Maier87bd9be2017-08-22 16:35:41 +0200177 * for performance reasons. */
178static const struct mgcp_request mgcp_requests[] = {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200179 MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint")
Oliver Smith622dd612019-01-30 14:14:45 +0100180 MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
181 MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
182 MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
183 MGCP_REQUEST("RQNT", handle_noti_req, "NotificationRequest")
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200184
Oliver Smith622dd612019-01-30 14:14:45 +0100185 /* SPEC extension */
186 MGCP_REQUEST("RSIP", handle_rsip, "ReSetInProgress")
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200187};
188
Philipp Maier87bd9be2017-08-22 16:35:41 +0200189/* Helper function to allocate some memory for responses and retransmissions */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200190static struct msgb *mgcp_msgb_alloc(void)
191{
192 struct msgb *msg;
193 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
194 if (!msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200195 LOGP(DLMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200196
197 return msg;
198}
199
Philipp Maier87bd9be2017-08-22 16:35:41 +0200200/* Helper function for do_retransmission() and create_resp() */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200201static struct msgb *do_retransmission(const struct mgcp_endpoint *endp)
202{
203 struct msgb *msg = mgcp_msgb_alloc();
204 if (!msg)
205 return NULL;
206
207 msg->l2h = msgb_put(msg, strlen(endp->last_response));
208 memcpy(msg->l2h, endp->last_response, msgb_l2len(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200209 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Retransmitted response");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200210 return msg;
211}
212
213static struct msgb *create_resp(struct mgcp_endpoint *endp, int code,
214 const char *txt, const char *msg,
215 const char *trans, const char *param,
216 const char *sdp)
217{
218 int len;
219 struct msgb *res;
220
221 res = mgcp_msgb_alloc();
222 if (!res)
223 return NULL;
224
Philipp Maier87bd9be2017-08-22 16:35:41 +0200225 len = snprintf((char *)res->data, 2048, "%d %s%s%s\r\n%s",
226 code, trans, txt, param ? param : "", sdp ? sdp : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200227 if (len < 0) {
228 LOGP(DLMGCP, LOGL_ERROR, "Failed to sprintf MGCP response.\n");
229 msgb_free(res);
230 return NULL;
231 }
232
233 res->l2h = msgb_put(res, len);
234 LOGP(DLMGCP, LOGL_DEBUG, "Generated response: code=%d\n", code);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200235 mgcp_disp_msg(res->l2h, msgb_l2len(res), "Generated response");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200236
237 /*
238 * Remember the last transmission per endpoint.
239 */
240 if (endp) {
241 struct mgcp_trunk_config *tcfg = endp->tcfg;
242 talloc_free(endp->last_response);
243 talloc_free(endp->last_trans);
244 endp->last_trans = talloc_strdup(tcfg->endpoints, trans);
245 endp->last_response = talloc_strndup(tcfg->endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200246 (const char *)res->l2h,
247 msgb_l2len(res));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200248 }
249
250 return res;
251}
252
253static struct msgb *create_ok_resp_with_param(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200254 int code, const char *msg,
255 const char *trans,
256 const char *param)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200257{
258 return create_resp(endp, code, " OK", msg, trans, param, NULL);
259}
260
261static struct msgb *create_ok_response(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200262 int code, const char *msg,
263 const char *trans)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200264{
265 return create_ok_resp_with_param(endp, code, msg, trans, NULL);
266}
267
268static struct msgb *create_err_response(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200269 int code, const char *msg,
270 const char *trans)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200271{
272 return create_resp(endp, code, " FAIL", msg, trans, NULL, NULL);
273}
274
Philipp Maier55295f72018-01-15 14:00:28 +0100275/* Add MGCP parameters to a message buffer */
276static int add_params(struct msgb *msg, const struct mgcp_endpoint *endp,
277 const struct mgcp_conn_rtp *conn)
278{
279 int rc;
280
Philipp Maier7f0966c2018-01-17 18:18:12 +0100281 /* NOTE: Only in the virtual trunk we allow dynamic endpoint names */
Philipp Maier207ab512018-02-02 14:19:26 +0100282 if (endp->wildcarded_req
Philipp Maier7f0966c2018-01-17 18:18:12 +0100283 && endp->tcfg->trunk_type == MGCP_TRUNK_VIRTUAL) {
Philipp Maierdd4ede32018-02-01 17:52:52 +0100284 rc = msgb_printf(msg, "Z: %s%x@%s\r\n",
Philipp Maier7f0966c2018-01-17 18:18:12 +0100285 MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK,
286 ENDPOINT_NUMBER(endp), endp->cfg->domain);
Philipp Maier55295f72018-01-15 14:00:28 +0100287 if (rc < 0)
288 return -EINVAL;
289 }
290
Philipp Maierc3cfae22018-01-22 12:03:03 +0100291 rc = msgb_printf(msg, "I: %s\r\n", conn->conn->id);
Philipp Maier55295f72018-01-15 14:00:28 +0100292 if (rc < 0)
293 return -EINVAL;
294
295 return 0;
296}
297
Philipp Maier87bd9be2017-08-22 16:35:41 +0200298/* Format MGCP response string (with SDP attached) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200299static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200300 struct mgcp_conn_rtp *conn,
301 const char *msg,
Philipp Maier55295f72018-01-15 14:00:28 +0100302 const char *trans_id,
303 bool add_conn_params)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200304{
305 const char *addr = endp->cfg->local_ip;
Philipp Maier8970c492017-10-11 13:33:42 +0200306 struct msgb *sdp;
307 int rc;
308 struct msgb *result;
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100309 char osmux_extension[strlen("X-Osmux: 255") + 1];
Philipp Maier1cb1e382017-11-02 17:16:04 +0100310 char local_ip_addr[INET_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200311
Philipp Maier8970c492017-10-11 13:33:42 +0200312 sdp = msgb_alloc_headroom(4096, 128, "sdp record");
313 if (!sdp)
314 return NULL;
315
Philipp Maier1cb1e382017-11-02 17:16:04 +0100316 if (!addr) {
317 mgcp_get_local_addr(local_ip_addr, conn);
318 addr = local_ip_addr;
319 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200320
Philipp Maier87bd9be2017-08-22 16:35:41 +0200321 if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) {
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100322 sprintf(osmux_extension, "X-Osmux: %u", conn->osmux.cid);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200323 conn->osmux.state = OSMUX_STATE_ACTIVATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200324 } else {
325 osmux_extension[0] = '\0';
326 }
327
Philipp Maier55295f72018-01-15 14:00:28 +0100328 /* Attach optional connection parameters */
329 if (add_conn_params) {
330 rc = add_params(sdp, endp, conn);
331 if (rc < 0)
332 goto error;
333 }
334
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100335 /* Attach optional OSMUX parameters */
336 if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) {
Philipp Maierc3cfae22018-01-22 12:03:03 +0100337 rc = msgb_printf(sdp, "%s\r\n", osmux_extension);
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100338 if (rc < 0)
339 goto error;
340 }
341
342 /* Attach line break to separate the parameters from the SDP block */
Philipp Maierc3cfae22018-01-22 12:03:03 +0100343 rc = msgb_printf(sdp, "\r\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200344
Philipp Maier8970c492017-10-11 13:33:42 +0200345 rc = mgcp_write_response_sdp(endp, conn, sdp, addr);
346 if (rc < 0)
347 goto error;
348 result = create_resp(endp, 200, " OK", msg, trans_id, NULL, (char*) sdp->data);
349 msgb_free(sdp);
350 return result;
351error:
352 msgb_free(sdp);
353 return NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200354}
355
Philipp Maier87bd9be2017-08-22 16:35:41 +0200356/* Send out dummy packet to keep the connection open, if the connection is an
357 * osmux connection, send the dummy packet via OSMUX */
358static void send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200359{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200360 if (conn->osmux.state != OSMUX_STATE_DISABLED)
361 osmux_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200362 else
Philipp Maier87bd9be2017-08-22 16:35:41 +0200363 mgcp_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200364}
365
Philipp Maier87bd9be2017-08-22 16:35:41 +0200366/* handle incoming messages:
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200367 * - this can be a command (four letters, space, transaction id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200368 * - or a response (three numbers, space, transaction id) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200369struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
370{
371 struct mgcp_parse_data pdata;
Harald Weltee35eeae2017-12-28 13:47:37 +0100372 int rc, i, code, handled = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200373 struct msgb *resp = NULL;
374 char *data;
375
376 if (msgb_l2len(msg) < 4) {
377 LOGP(DLMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len);
378 return NULL;
379 }
380
381 if (mgcp_msg_terminate_nul(msg))
382 return NULL;
383
Philipp Maier87bd9be2017-08-22 16:35:41 +0200384 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Received message");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200385
Philipp Maier87bd9be2017-08-22 16:35:41 +0200386 /* attempt to treat it as a response */
387 if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200388 LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code);
389 return NULL;
390 }
391
392 msg->l3h = &msg->l2h[4];
393
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200394 /*
395 * Check for a duplicate message and respond.
396 */
397 memset(&pdata, 0, sizeof(pdata));
398 pdata.cfg = cfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200399 data = mgcp_strline((char *)msg->l3h, &pdata.save);
Harald Weltee35eeae2017-12-28 13:47:37 +0100400 rc = mgcp_parse_header(&pdata, data);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200401 if (pdata.endp && pdata.trans
Philipp Maier87bd9be2017-08-22 16:35:41 +0200402 && pdata.endp->last_trans
403 && strcmp(pdata.endp->last_trans, pdata.trans) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200404 return do_retransmission(pdata.endp);
405 }
406
Harald Welteabbb6b92017-12-28 13:13:50 +0100407 /* check for general parser failure */
Harald Weltee35eeae2017-12-28 13:47:37 +0100408 if (rc < 0) {
Harald Welteabbb6b92017-12-28 13:13:50 +0100409 LOGP(DLMGCP, LOGL_NOTICE, "%s: failed to find the endpoint\n", msg->l2h);
Harald Weltee35eeae2017-12-28 13:47:37 +0100410 return create_err_response(NULL, -rc, (const char *) msg->l2h, pdata.trans);
Harald Welteabbb6b92017-12-28 13:13:50 +0100411 }
412
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200413 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200414 if (strncmp
415 (mgcp_requests[i].name, (const char *)&msg->l2h[0],
416 4) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200417 handled = 1;
418 resp = mgcp_requests[i].handle_request(&pdata);
419 break;
420 }
421 }
422
423 if (!handled)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200424 LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n",
425 &msg->l2h[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200426
427 return resp;
428}
429
Philipp Maier87bd9be2017-08-22 16:35:41 +0200430/* AUEP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200431static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p)
432{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200433 LOGP(DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n");
Harald Welteabbb6b92017-12-28 13:13:50 +0100434 return create_ok_response(p->endp, 200, "AUEP", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200435}
436
Harald Welte1d1b98f2017-12-25 10:03:40 +0100437/* Try to find a free port by attempting to bind on it. Also handle the
Philipp Maier87bd9be2017-08-22 16:35:41 +0200438 * counter that points on the next free port. Since we have a pointer
Philipp Maierb38fb892018-05-22 13:52:21 +0200439 * to the next free port, binding should in work on the first attempt in
440 * general. In case of failure the next port is tryed until the whole port
441 * range is tryed once. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200442static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200443{
444 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200445 struct mgcp_rtp_end *end;
446 struct mgcp_port_range *range;
Philipp Maierb38fb892018-05-22 13:52:21 +0200447 unsigned int tries;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200448
Philipp Maier87bd9be2017-08-22 16:35:41 +0200449 OSMO_ASSERT(conn);
450 end = &conn->end;
451 OSMO_ASSERT(end);
452
453 range = &endp->cfg->net_ports;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200454
455 /* attempt to find a port */
Philipp Maierb38fb892018-05-22 13:52:21 +0200456 tries = (range->range_end - range->range_start) / 2;
457 for (i = 0; i < tries; ++i) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200458 int rc;
459
460 if (range->last_port >= range->range_end)
461 range->last_port = range->range_start;
462
Philipp Maier87bd9be2017-08-22 16:35:41 +0200463 rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200464
465 range->last_port += 2;
466 if (rc == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200467 return 0;
468 }
469
470 }
471
Philipp Maier87bd9be2017-08-22 16:35:41 +0200472 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maierb38fb892018-05-22 13:52:21 +0200473 "Allocating a RTP/RTCP port failed %u times 0x%x.\n",
474 tries, ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200475 return -1;
476}
477
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200478/*! Helper function for check_local_cx_options() to get a pointer of the next
479 * lco option identifier
480 * \param[in] lco string
481 * \returns pointer to the beginning of the LCO identifier, NULL on failure */
482char *get_lco_identifier(const char *options)
483{
484 char *ptr;
485 unsigned int count = 0;
486
487 /* Jump to the end of the lco identifier */
488 ptr = strstr(options, ":");
489 if (!ptr)
490 return NULL;
491
492 /* Walk backwards until the pointer points to the beginning of the
493 * lco identifier. We know that we stand at the beginning when we
494 * are either at the beginning of the memory or see a space or
495 * comma. (this is tolerant, it will accept a:10, b:11 as well as
496 * a:10,b:11) */
497 while (1) {
498 /* Endless loop protection */
499 if (count > 10000)
500 return NULL;
501 else if (ptr < options || *ptr == ' ' || *ptr == ',') {
502 ptr++;
503 break;
504 }
505 ptr--;
506 count++;
507 }
508
509 /* Check if we got any result */
510 if (*ptr == ':')
511 return NULL;
512
513 return ptr;
514}
515
516/*! Check the LCO option. This function checks for multiple appearence of LCO
517 * options, which is illegal
518 * \param[in] ctx talloc context
519 * \param[in] lco string
520 * \returns 0 on success, -1 on failure */
521int check_local_cx_options(void *ctx, const char *options)
522{
523 int i;
524 char *options_copy;
525 char *lco_identifier;
526 char *lco_identifier_end;
527 char *next_lco_identifier;
528
529 char **lco_seen;
530 unsigned int lco_seen_n = 0;
531
532 if (!options)
533 return -1;
534
535 lco_seen =
536 (char **)talloc_zero_size(ctx, strlen(options) * sizeof(char *));
537 options_copy = talloc_strdup(ctx, options);
538 lco_identifier = options_copy;
539
540 do {
541 /* Move the lco_identifier pointer to the beginning of the
542 * current lco option identifier */
543 lco_identifier = get_lco_identifier(lco_identifier);
544 if (!lco_identifier)
545 goto error;
546
547 /* Look ahead to the next LCO option early, since we
548 * will parse destructively */
549 next_lco_identifier = strstr(lco_identifier + 1, ",");
550
551 /* Pinch off the end of the lco field identifier name
552 * and see if we still got something, also check if
553 * there is some value after the colon. */
554 lco_identifier_end = strstr(lco_identifier, ":");
555 if (!lco_identifier_end)
556 goto error;
557 if (*(lco_identifier_end + 1) == ' '
558 || *(lco_identifier_end + 1) == ','
559 || *(lco_identifier_end + 1) == '\0')
560 goto error;
561 *lco_identifier_end = '\0';
562 if (strlen(lco_identifier) == 0)
563 goto error;
564
565 /* Check if we have already seen the current field identifier
566 * before. If yes, we must bail, an LCO must only appear once
567 * in the LCO string */
568 for (i = 0; i < lco_seen_n; i++) {
569 if (strcmp(lco_seen[i], lco_identifier) == 0)
570 goto error;
571 }
572 lco_seen[lco_seen_n] = lco_identifier;
573 lco_seen_n++;
574
575 /* The first identifier must always be found at the beginnning
576 * of the LCO string */
577 if (lco_seen[0] != options_copy)
578 goto error;
579
580 /* Go to the next lco option */
581 lco_identifier = next_lco_identifier;
582 } while (lco_identifier);
583
584 talloc_free(lco_seen);
585 talloc_free(options_copy);
586 return 0;
587error:
588 talloc_free(lco_seen);
589 talloc_free(options_copy);
590 return -1;
591}
592
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200593/* Set the LCO from a string (see RFC 3435).
Harald Welte1d1b98f2017-12-25 10:03:40 +0100594 * The string is stored in the 'string' field. A NULL string is handled exactly
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200595 * like an empty string, the 'string' field is never NULL after this function
596 * has been called. */
Philipp Maiera390d0b2018-01-31 17:30:19 +0100597static int set_local_cx_options(void *ctx, struct mgcp_lco *lco,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200598 const char *options)
599{
600 char *p_opt, *a_opt;
Philipp Maier8dbc9ed2018-10-26 14:50:25 +0200601 char codec[17];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200602
Philipp Maier604410c2018-06-06 10:02:16 +0200603 if (!options)
604 return 0;
605 if (strlen(options) == 0)
606 return 0;
607
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200608 /* Make sure the encoding of the LCO is consistant before we proceed */
609 if (check_local_cx_options(ctx, options) != 0) {
610 LOGP(DLMGCP, LOGL_ERROR,
611 "local CX options: Internal inconsistency in Local Connection Options!\n");
612 return 524;
613 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200614
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200615 talloc_free(lco->string);
616 lco->string = talloc_strdup(ctx, options);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200617
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200618 p_opt = strstr(lco->string, "p:");
619 if (p_opt && sscanf(p_opt, "p:%d-%d",
620 &lco->pkt_period_min, &lco->pkt_period_max) == 1)
621 lco->pkt_period_max = lco->pkt_period_min;
622
Philipp Maierbc0346e2018-06-07 09:52:16 +0200623 /* FIXME: LCO also supports the negotiation of more then one codec.
624 * (e.g. a:PCMU;G726-32) But this implementation only supports a single
625 * codec only. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200626 a_opt = strstr(lco->string, "a:");
Philipp Maier8dbc9ed2018-10-26 14:50:25 +0200627 if (a_opt && sscanf(a_opt, "a:%16[^,]", codec) == 1) {
Philipp Maier604410c2018-06-06 10:02:16 +0200628 talloc_free(lco->codec);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200629 lco->codec = talloc_strdup(ctx, codec);
Philipp Maier604410c2018-06-06 10:02:16 +0200630 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200631
632 LOGP(DLMGCP, LOGL_DEBUG,
633 "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n",
634 lco->pkt_period_max, lco->codec);
Philipp Maiera390d0b2018-01-31 17:30:19 +0100635
636 /* Check if the packetization fits the 20ms raster */
637 if (lco->pkt_period_min % 20 && lco->pkt_period_max % 20) {
638 LOGP(DLMGCP, LOGL_ERROR,
639 "local CX options: packetization interval is not a multiple of 20ms!\n");
640 return 535;
641 }
642
643 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200644}
645
646void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
647 struct mgcp_rtp_end *rtp)
648{
649 struct mgcp_trunk_config *tcfg = endp->tcfg;
650
651 int patch_ssrc = expect_ssrc_change && tcfg->force_constant_ssrc;
652
653 rtp->force_aligned_timing = tcfg->force_aligned_timing;
654 rtp->force_constant_ssrc = patch_ssrc ? 1 : 0;
655
656 LOGP(DLMGCP, LOGL_DEBUG,
657 "Configuring RTP endpoint: local port %d%s%s\n",
658 ntohs(rtp->rtp_port),
659 rtp->force_aligned_timing ? ", force constant timing" : "",
660 rtp->force_constant_ssrc ? ", force constant ssrc" : "");
661}
662
663uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
664 struct mgcp_rtp_end *rtp)
665{
666 int f = 0;
667
668 /* Get the number of frames per channel and packet */
669 if (rtp->frames_per_packet)
670 f = rtp->frames_per_packet;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200671 else if (rtp->packet_duration_ms && rtp->codec->frame_duration_num) {
672 int den = 1000 * rtp->codec->frame_duration_num;
673 f = (rtp->packet_duration_ms * rtp->codec->frame_duration_den +
Philipp Maier87bd9be2017-08-22 16:35:41 +0200674 den / 2)
675 / den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200676 }
677
Philipp Maierbc0346e2018-06-07 09:52:16 +0200678 return rtp->codec->rate * f * rtp->codec->frame_duration_num /
679 rtp->codec->frame_duration_den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200680}
681
682static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line)
683{
684 if (!endp->cfg->osmux_init) {
685 if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) {
686 LOGP(DLMGCP, LOGL_ERROR, "Cannot init OSMUX\n");
687 return -1;
688 }
689 LOGP(DLMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n");
690 }
691
692 return mgcp_parse_osmux_cid(line);
693}
694
Philipp Maierbc0346e2018-06-07 09:52:16 +0200695/* Process codec information contained in CRCX/MDCX */
696static int handle_codec_info(struct mgcp_conn_rtp *conn,
697 struct mgcp_parse_data *p, int have_sdp, bool crcx)
698{
699 struct mgcp_endpoint *endp = p->endp;
700 int rc;
701 char *cmd;
702
703 if (crcx)
704 cmd = "CRCX";
705 else
706 cmd = "MDCX";
707
708 /* Collect codec information */
709 if (have_sdp) {
710 /* If we have SDP, we ignore the local connection options and
711 * use only the SDP information. */
712 mgcp_codec_reset_all(conn);
713 rc = mgcp_parse_sdp_data(endp, conn, p);
714 if (rc != 0) {
715 LOGP(DLMGCP, LOGL_ERROR,
716 "%s: endpoint:%x sdp not parseable\n", cmd,
717 ENDPOINT_NUMBER(endp));
718
719 /* See also RFC 3661: Protocol error */
720 return 510;
721 }
722 } else if (endp->local_options.codec) {
723 /* When no SDP is available, we use the codec information from
724 * the local connection options (if present) */
725 mgcp_codec_reset_all(conn);
726 rc = mgcp_codec_add(conn, PTYPE_UNDEFINED, endp->local_options.codec);
727 if (rc != 0)
728 goto error;
729 }
730
731 /* Make sure we always set a sane default codec */
732 if (conn->end.codecs_assigned == 0) {
733 /* When SDP and/or LCO did not supply any codec information,
734 * than it makes sense to pick a sane default: (payload-type 0,
735 * PCMU), see also: OS#2658 */
736 mgcp_codec_reset_all(conn);
737 rc = mgcp_codec_add(conn, 0, NULL);
738 if (rc != 0)
739 goto error;
740 }
741
742 /* Make codec decision */
743 if (mgcp_codec_decide(conn) != 0)
744 goto error;
745
746 return 0;
747
748error:
749 LOGP(DLMGCP, LOGL_ERROR,
750 "%s: endpoint:0x%x codec negotiation failure\n", cmd,
751 ENDPOINT_NUMBER(endp));
752
753 /* See also RFC 3661: Codec negotiation failure */
754 return 534;
755}
756
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200757static bool parse_x_osmo_ign(struct mgcp_endpoint *endp, char *line)
758{
759 char *saveptr = NULL;
760
761 if (strncmp(line, MGCP_X_OSMO_IGN_HEADER, strlen(MGCP_X_OSMO_IGN_HEADER)))
762 return false;
763 line += strlen(MGCP_X_OSMO_IGN_HEADER);
764
765 while (1) {
766 char *token = strtok_r(line, " ", &saveptr);
767 line = NULL;
768 if (!token)
769 break;
770
771 if (!strcmp(token, "C"))
772 endp->x_osmo_ign |= MGCP_X_OSMO_IGN_CALLID;
773 else
Pau Espin Pedrol9ecceb62018-10-16 15:35:58 +0200774 LOGP(DLMGCP, LOGL_ERROR, "endpoint 0x%x: received unknown X-Osmo-IGN item '%s'\n",
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200775 ENDPOINT_NUMBER(endp), token);
776 }
777
778 return true;
779}
780
Philipp Maier87bd9be2017-08-22 16:35:41 +0200781/* CRCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200782static struct msgb *handle_create_con(struct mgcp_parse_data *p)
783{
Stefan Sperling1e174872018-10-25 18:36:10 +0200784 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200785 struct mgcp_endpoint *endp = p->endp;
Stefan Sperling9270e912018-10-29 14:10:00 +0100786 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_crcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200787 int error_code = 400;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200788 const char *local_options = NULL;
789 const char *callid = NULL;
790 const char *mode = NULL;
791 char *line;
792 int have_sdp = 0, osmux_cid = -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200793 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100794 struct mgcp_conn *_conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200795 char conn_name[512];
Philipp Maiera390d0b2018-01-31 17:30:19 +0100796 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200797
798 LOGP(DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200799
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200800 /* parse CallID C: and LocalParameters L: */
801 for_each_line(line, p->save) {
802 if (!mgcp_check_param(endp, line))
803 continue;
804
805 switch (line[0]) {
806 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200807 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200808 break;
809 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200810 callid = (const char *)line + 3;
811 break;
812 case 'I':
Philipp Maierffd75e42017-11-22 11:44:50 +0100813 /* It is illegal to send a connection identifier
814 * together with a CRCX, the MGW will assign the
815 * connection identifier by itself on CRCX */
Stefan Sperling9270e912018-10-29 14:10:00 +0100816 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_BAD_ACTION]);
Philipp Maierffd75e42017-11-22 11:44:50 +0100817 return create_err_response(NULL, 523, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200818 break;
819 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200820 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200821 break;
822 case 'X':
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200823 if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
824 /* If osmux is disabled, just skip setting it up */
825 if (!p->endp->cfg->osmux)
826 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200827 osmux_cid = mgcp_osmux_setup(endp, line);
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200828 break;
829 }
830
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200831 if (parse_x_osmo_ign(endp, line))
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200832 break;
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200833
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200834 /* Ignore unknown X-headers */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200835 break;
836 case '\0':
837 have_sdp = 1;
838 goto mgcp_header_done;
839 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200840 LOGP(DLMGCP, LOGL_NOTICE,
841 "CRCX: endpoint:%x unhandled option: '%c'/%d\n",
842 ENDPOINT_NUMBER(endp), *line, *line);
Stefan Sperling9270e912018-10-29 14:10:00 +0100843 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +0100844 return create_err_response(NULL, 539, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200845 break;
846 }
847 }
848
849mgcp_header_done:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200850 /* Check parameters */
851 if (!callid) {
852 LOGP(DLMGCP, LOGL_ERROR,
853 "CRCX: endpoint:%x insufficient parameters, missing callid\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200854 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +0100855 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_MISSING_CALLID]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100856 return create_err_response(endp, 516, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200857 }
858
Philipp Maier87bd9be2017-08-22 16:35:41 +0200859 if (!mode) {
860 LOGP(DLMGCP, LOGL_ERROR,
861 "CRCX: endpoint:%x insufficient parameters, missing mode\n",
862 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +0100863 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_MODE]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100864 return create_err_response(endp, 517, "CRCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200865 }
866
Philipp Maier87bd9be2017-08-22 16:35:41 +0200867 /* Check if we are able to accept the creation of another connection */
868 if (llist_count(&endp->conns) >= endp->type->max_conns) {
869 LOGP(DLMGCP, LOGL_ERROR,
870 "CRCX: endpoint:%x endpoint full, max. %i connections allowed!\n",
871 endp->type->max_conns, ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200872 if (tcfg->force_realloc) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200873 /* There is no more room for a connection, make some
874 * room by blindly tossing the oldest of the two two
875 * connections */
876 mgcp_conn_free_oldest(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200877 } else {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200878 /* There is no more room for a connection, leave
879 * everything as it is and return with an error */
Stefan Sperling9270e912018-10-29 14:10:00 +0100880 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_LIMIT_EXCEEDED]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100881 return create_err_response(endp, 540, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200882 }
883 }
884
Philipp Maier87bd9be2017-08-22 16:35:41 +0200885 /* Check if this endpoint already serves a call, if so, check if the
886 * callids match up so that we are sure that this is our call */
887 if (endp->callid && mgcp_verify_call_id(endp, callid)) {
888 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100889 "CRCX: endpoint:0x%x allready seized by other call (%s)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200890 ENDPOINT_NUMBER(endp), endp->callid);
891 if (tcfg->force_realloc)
892 /* This is not our call, toss everything by releasing
893 * the entire endpoint. (rude!) */
Philipp Maier1355d7e2018-02-01 14:30:06 +0100894 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200895 else {
896 /* This is not our call, leave everything as it is and
897 * return with an error. */
Stefan Sperling9270e912018-10-29 14:10:00 +0100898 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_UNKNOWN_CALLID]);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200899 return create_err_response(endp, 400, "CRCX", p->trans);
900 }
901 }
902
903 /* Set the callid, creation of another connection will only be possible
Harald Welte1d1b98f2017-12-25 10:03:40 +0100904 * when the callid matches up. (Connections are distinguished by their
Philipp Maier87bd9be2017-08-22 16:35:41 +0200905 * connection ids) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200906 endp->callid = talloc_strdup(tcfg->endpoints, callid);
907
Philipp Maierffd75e42017-11-22 11:44:50 +0100908 snprintf(conn_name, sizeof(conn_name), "%s", callid);
909 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP, conn_name);
910 if (!_conn) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200911 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100912 "CRCX: endpoint:0x%x unable to allocate RTP connection\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200913 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +0100914 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_ALLOC_CONN]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200915 goto error2;
916
Philipp Maier87bd9be2017-08-22 16:35:41 +0200917 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100918 conn = mgcp_conn_get_rtp(endp, _conn->id);
919 OSMO_ASSERT(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200920
921 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
922 error_code = 517;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100923 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_MODE]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200924 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200925 }
926
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200927 /* Annotate Osmux circuit ID and set it to negotiating state until this
Philipp Maier87bd9be2017-08-22 16:35:41 +0200928 * is fully set up from the dummy load. */
929 conn->osmux.state = OSMUX_STATE_DISABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200930 if (osmux_cid >= 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200931 conn->osmux.cid = osmux_cid;
932 conn->osmux.state = OSMUX_STATE_NEGOTIATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200933 } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) {
934 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100935 "CRCX: endpoint:0x%x osmux only and no osmux offered\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200936 ENDPOINT_NUMBER(endp));
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100937 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_OSMUX]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200938 goto error2;
939 }
940
Philipp Maierbc0346e2018-06-07 09:52:16 +0200941 /* Set local connection options, if present */
942 if (local_options) {
943 rc = set_local_cx_options(endp->tcfg->endpoints,
944 &endp->local_options, local_options);
945 if (rc != 0) {
946 LOGP(DLMGCP, LOGL_ERROR,
947 "CRCX: endpoint:%x inavlid local connection options!\n",
948 ENDPOINT_NUMBER(endp));
949 error_code = rc;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100950 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS]);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200951 goto error2;
952 }
953 }
954
955 /* Handle codec information and decide for a suitable codec */
956 rc = handle_codec_info(conn, p, have_sdp, true);
957 mgcp_codec_summary(conn);
958 if (rc) {
959 error_code = rc;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100960 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_CODEC_NEGOTIATION]);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200961 goto error2;
962 }
963
Philipp Maier87bd9be2017-08-22 16:35:41 +0200964 conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints,
965 tcfg->audio_fmtp_extra);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200966
Philipp Maier87bd9be2017-08-22 16:35:41 +0200967 if (p->cfg->force_ptime) {
968 conn->end.packet_duration_ms = p->cfg->force_ptime;
969 conn->end.force_output_ptime = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200970 }
971
Philipp Maier1cb1e382017-11-02 17:16:04 +0100972 mgcp_rtp_end_config(endp, 0, &conn->end);
973
Philipp Maierc3cc6542018-02-02 12:58:42 +0100974 /* check connection mode setting */
975 if (conn->conn->mode != MGCP_CONN_LOOPBACK
976 && conn->conn->mode != MGCP_CONN_RECV_ONLY
977 && conn->end.rtp_port == 0) {
978 LOGP(DLMGCP, LOGL_ERROR,
979 "CRCX: endpoint:%x selected connection mode type requires an opposite end!\n",
980 ENDPOINT_NUMBER(endp));
981 error_code = 527;
Stefan Sperling9270e912018-10-29 14:10:00 +0100982 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC]);
Philipp Maierc3cc6542018-02-02 12:58:42 +0100983 goto error2;
984 }
985
Philipp Maier1cb1e382017-11-02 17:16:04 +0100986 if (allocate_port(endp, conn) != 0) {
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100987 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_BIND_PORT]);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100988 goto error2;
989 }
990
Philipp Maier87bd9be2017-08-22 16:35:41 +0200991 if (setup_rtp_processing(endp, conn) != 0) {
992 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100993 "CRCX: endpoint:0x%x could not start RTP processing!\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200994 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +0100995 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_START_RTP]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200996 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200997 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200998
999 /* policy CB */
1000 if (p->cfg->policy_cb) {
1001 int rc;
1002 rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001003 MGCP_ENDP_CRCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001004 switch (rc) {
1005 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001006 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001007 "CRCX: endpoint:0x%x CRCX rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001008 ENDPOINT_NUMBER(endp));
Philipp Maier1355d7e2018-02-01 14:30:06 +01001009 mgcp_endp_release(endp);
Stefan Sperling9270e912018-10-29 14:10:00 +01001010 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001011 return create_err_response(endp, 400, "CRCX", p->trans);
1012 break;
1013 case MGCP_POLICY_DEFER:
1014 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001015 return NULL;
1016 break;
1017 case MGCP_POLICY_CONT:
1018 /* just continue */
1019 break;
1020 }
1021 }
1022
Philipp Maier87bd9be2017-08-22 16:35:41 +02001023 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001024 "CRCX: endpoint:0x%x Creating connection: CI: %s port: %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001025 ENDPOINT_NUMBER(endp), conn->conn->id, conn->end.local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001026 if (p->cfg->change_cb)
1027 p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX);
1028
Philipp Maiere726d4f2017-11-01 10:41:34 +01001029 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
1030 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001031 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
Philipp Maiere726d4f2017-11-01 10:41:34 +01001032 && tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001033 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001034
Philipp Maier87bd9be2017-08-22 16:35:41 +02001035 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001036 "CRCX: endpoint:0x%x connection successfully created\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001037 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +01001038 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_SUCCESS]);
Philipp Maier55295f72018-01-15 14:00:28 +01001039 return create_response_with_sdp(endp, conn, "CRCX", p->trans, true);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001040error2:
Philipp Maier1355d7e2018-02-01 14:30:06 +01001041 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001042 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier3c8ccb62018-06-04 09:59:24 +02001043 "CRCX: endpoint:0x%x unable to create connection\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001044 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001045 return create_err_response(endp, error_code, "CRCX", p->trans);
1046}
1047
Philipp Maierbc0346e2018-06-07 09:52:16 +02001048
1049
1050
1051
Philipp Maier87bd9be2017-08-22 16:35:41 +02001052/* MDCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001053static struct msgb *handle_modify_con(struct mgcp_parse_data *p)
1054{
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001055 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001056 struct mgcp_endpoint *endp = p->endp;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001057 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_mdcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001058 int error_code = 500;
1059 int silent = 0;
1060 int have_sdp = 0;
1061 char *line;
1062 const char *local_options = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001063 const char *mode = NULL;
1064 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier01d24a32017-11-21 17:26:09 +01001065 const char *conn_id = NULL;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001066 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001067
1068 LOGP(DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001069
Philipp Maier5656fbf2018-02-02 14:41:58 +01001070 /* Prohibit wildcarded requests */
1071 if (endp->wildcarded_req) {
1072 LOGP(DLMGCP, LOGL_ERROR,
1073 "MDCX: endpoint:0x%x wildcarded endpoint names not supported.\n",
1074 ENDPOINT_NUMBER(endp));
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001075 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_WILDCARD]);
Philipp Maier5656fbf2018-02-02 14:41:58 +01001076 return create_err_response(endp, 507, "MDCX", p->trans);
1077 }
1078
Philipp Maier87bd9be2017-08-22 16:35:41 +02001079 if (llist_count(&endp->conns) <= 0) {
1080 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001081 "MDCX: endpoint:0x%x endpoint is not holding a connection.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001082 ENDPOINT_NUMBER(endp));
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001083 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_CONN]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001084 return create_err_response(endp, 400, "MDCX", p->trans);
1085 }
1086
1087 for_each_line(line, p->save) {
1088 if (!mgcp_check_param(endp, line))
1089 continue;
1090
1091 switch (line[0]) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001092 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +01001093 if (mgcp_verify_call_id(endp, line + 3) != 0) {
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001094 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CALLID]);
Harald Weltee35eeae2017-12-28 13:47:37 +01001095 error_code = 516;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001096 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001097 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001098 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001099 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001100 conn_id = (const char *)line + 3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001101 if ((error_code = mgcp_verify_ci(endp, conn_id))) {
1102 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CONNID]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001103 goto error3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001104 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001105 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001106 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001107 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001108 break;
1109 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001110 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001111 break;
1112 case 'Z':
1113 silent = strcmp("noanswer", line + 3) == 0;
1114 break;
1115 case '\0':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001116 have_sdp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001117 goto mgcp_header_done;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001118 break;
1119 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001120 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001121 "MDCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001122 ENDPOINT_NUMBER(endp), line[0], line[0]);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001123 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001124 return create_err_response(NULL, 539, "MDCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001125 break;
1126 }
1127 }
1128
Philipp Maier87bd9be2017-08-22 16:35:41 +02001129mgcp_header_done:
Philipp Maier01d24a32017-11-21 17:26:09 +01001130 if (!conn_id) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001131 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001132 "MDCX: endpoint:0x%x insufficient parameters, missing ci (connectionIdentifier)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001133 ENDPOINT_NUMBER(endp));
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001134 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_CONNID]);
Harald Weltee35eeae2017-12-28 13:47:37 +01001135 return create_err_response(endp, 515, "MDCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001136 }
1137
1138 conn = mgcp_conn_get_rtp(endp, conn_id);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001139 if (!conn) {
1140 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_CONN_NOT_FOUND]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001141 return create_err_response(endp, 400, "MDCX", p->trans);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001142 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001143
1144 if (mode) {
1145 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001146 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_MODE]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001147 error_code = 517;
1148 goto error3;
1149 }
1150 } else
1151 conn->conn->mode = conn->conn->mode_orig;
1152
Philipp Maierbc0346e2018-06-07 09:52:16 +02001153 /* Set local connection options, if present */
1154 if (local_options) {
1155 rc = set_local_cx_options(endp->tcfg->endpoints,
1156 &endp->local_options, local_options);
1157 if (rc != 0) {
1158 LOGP(DLMGCP, LOGL_ERROR,
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001159 "MDCX: endpoint:%x invalid local connection options!\n",
Philipp Maierbc0346e2018-06-07 09:52:16 +02001160 ENDPOINT_NUMBER(endp));
1161 error_code = rc;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001162 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS]);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001163 goto error3;
1164 }
1165 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001166
Philipp Maierbc0346e2018-06-07 09:52:16 +02001167 /* Handle codec information and decide for a suitable codec */
1168 rc = handle_codec_info(conn, p, have_sdp, false);
1169 mgcp_codec_summary(conn);
1170 if (rc) {
Philipp Maieraf07f662018-02-02 11:34:02 +01001171 error_code = rc;
1172 goto error3;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001173 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001174
Philipp Maierc3cc6542018-02-02 12:58:42 +01001175 /* check connection mode setting */
1176 if (conn->conn->mode != MGCP_CONN_LOOPBACK
1177 && conn->conn->mode != MGCP_CONN_RECV_ONLY
1178 && conn->end.rtp_port == 0) {
1179 LOGP(DLMGCP, LOGL_ERROR,
1180 "MDCX: endpoint:%x selected connection mode type requires an opposite end!\n",
1181 ENDPOINT_NUMBER(endp));
1182 error_code = 527;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001183 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC]);
Philipp Maierc3cc6542018-02-02 12:58:42 +01001184 goto error3;
1185 }
1186
Philipp Maierbc0346e2018-06-07 09:52:16 +02001187
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001188 if (setup_rtp_processing(endp, conn) != 0) {
1189 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_START_RTP]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001190 goto error3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001191 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001192
Philipp Maier87bd9be2017-08-22 16:35:41 +02001193
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001194 /* policy CB */
1195 if (p->cfg->policy_cb) {
1196 int rc;
1197 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001198 MGCP_ENDP_MDCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001199 switch (rc) {
1200 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001201 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001202 "MDCX: endpoint:0x%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001203 ENDPOINT_NUMBER(endp));
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001204 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001205 if (silent)
1206 goto out_silent;
1207 return create_err_response(endp, 400, "MDCX", p->trans);
1208 break;
1209 case MGCP_POLICY_DEFER:
1210 /* stop processing */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001211 LOGP(DLMGCP, LOGL_DEBUG,
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001212 "MDCX: endpoint:0x%x deferred by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001213 ENDPOINT_NUMBER(endp));
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001214 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_DEFERRED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001215 return NULL;
1216 break;
1217 case MGCP_POLICY_CONT:
1218 /* just continue */
1219 break;
1220 }
1221 }
1222
Philipp Maier87bd9be2017-08-22 16:35:41 +02001223 mgcp_rtp_end_config(endp, 1, &conn->end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001224
1225 /* modify */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001226 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001227 "MDCX: endpoint:0x%x modified conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001228 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001229 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001230 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1231 MGCP_ENDP_MDCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001232
Philipp Maiere726d4f2017-11-01 10:41:34 +01001233 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
1234 OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1235 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
1236 && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001237 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001238
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001239 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_SUCCESS]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001240 if (silent)
1241 goto out_silent;
1242
Philipp Maier87bd9be2017-08-22 16:35:41 +02001243 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001244 "MDCX: endpoint:0x%x connection successfully modified\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001245 ENDPOINT_NUMBER(endp));
Philipp Maier55295f72018-01-15 14:00:28 +01001246 return create_response_with_sdp(endp, conn, "MDCX", p->trans, false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001247error3:
1248 return create_err_response(endp, error_code, "MDCX", p->trans);
1249
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001250out_silent:
Philipp Maier230e4fc2017-11-28 09:38:45 +01001251 LOGP(DLMGCP, LOGL_DEBUG, "MDCX: endpoint:0x%x silent exit\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001252 ENDPOINT_NUMBER(endp));
1253 return NULL;
1254}
1255
Philipp Maier87bd9be2017-08-22 16:35:41 +02001256/* DLCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001257static struct msgb *handle_delete_con(struct mgcp_parse_data *p)
1258{
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001259 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001260 struct mgcp_endpoint *endp = p->endp;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001261 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_dlcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001262 int error_code = 400;
1263 int silent = 0;
1264 char *line;
1265 char stats[1048];
Philipp Maier01d24a32017-11-21 17:26:09 +01001266 const char *conn_id = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001267 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001268
1269 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001270 "DLCX: endpoint:0x%x deleting connection ...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001271 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001272
Philipp Maier5656fbf2018-02-02 14:41:58 +01001273 /* Prohibit wildcarded requests */
1274 if (endp->wildcarded_req) {
1275 LOGP(DLMGCP, LOGL_ERROR,
1276 "DLCX: endpoint:0x%x wildcarded endpoint names not supported.\n",
1277 ENDPOINT_NUMBER(endp));
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001278 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_WILDCARD]);
Philipp Maier5656fbf2018-02-02 14:41:58 +01001279 return create_err_response(endp, 507, "DLCX", p->trans);
1280 }
1281
Philipp Maier87bd9be2017-08-22 16:35:41 +02001282 if (llist_count(&endp->conns) <= 0) {
1283 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001284 "DLCX: endpoint:0x%x endpoint is not holding a connection.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001285 ENDPOINT_NUMBER(endp));
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001286 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_NO_CONN]);
Harald Weltee35eeae2017-12-28 13:47:37 +01001287 return create_err_response(endp, 515, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001288 }
1289
1290 for_each_line(line, p->save) {
1291 if (!mgcp_check_param(endp, line))
1292 continue;
1293
1294 switch (line[0]) {
1295 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +01001296 if (mgcp_verify_call_id(endp, line + 3) != 0) {
1297 error_code = 516;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001298 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CALLID]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001299 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001300 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001301 break;
1302 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001303 conn_id = (const char *)line + 3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001304 if ((error_code = mgcp_verify_ci(endp, conn_id))) {
1305 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CONNID]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001306 goto error3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001307 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001308 break;
1309 case 'Z':
1310 silent = strcmp("noanswer", line + 3) == 0;
1311 break;
1312 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001313 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001314 "DLCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001315 ENDPOINT_NUMBER(endp), line[0], line[0]);
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001316 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001317 return create_err_response(NULL, 539, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001318 break;
1319 }
1320 }
1321
1322 /* policy CB */
1323 if (p->cfg->policy_cb) {
1324 int rc;
1325 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001326 MGCP_ENDP_DLCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001327 switch (rc) {
1328 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001329 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001330 "DLCX: endpoint:0x%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001331 ENDPOINT_NUMBER(endp));
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001332 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001333 if (silent)
1334 goto out_silent;
1335 return create_err_response(endp, 400, "DLCX", p->trans);
1336 break;
1337 case MGCP_POLICY_DEFER:
1338 /* stop processing */
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001339 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_DEFERRED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001340 return NULL;
1341 break;
1342 case MGCP_POLICY_CONT:
1343 /* just continue */
1344 break;
1345 }
1346 }
1347
Philipp Maierf4c0e372017-10-11 16:06:45 +02001348 /* When no connection id is supplied, we will interpret this as a
1349 * wildcarded DLCX and drop all connections at once. (See also
1350 * RFC3435 Section F.7) */
Philipp Maier01d24a32017-11-21 17:26:09 +01001351 if (!conn_id) {
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001352 int num_conns = llist_count(&endp->conns);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001353 LOGP(DLMGCP, LOGL_NOTICE,
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001354 "DLCX: endpoint:0x%x missing ci (connectionIdentifier), will remove all connections (%d total) at once\n",
1355 num_conns, ENDPOINT_NUMBER(endp));
1356
1357 if (num_conns > 0)
1358 rate_ctr_add(&rate_ctrs->ctr[MGCP_DLCX_SUCCESS], num_conns);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001359
Philipp Maier1355d7e2018-02-01 14:30:06 +01001360 mgcp_endp_release(endp);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001361
1362 /* Note: In this case we do not return any statistics,
1363 * as we assume that the client is not interested in
1364 * this case. */
1365 return create_ok_response(endp, 200, "DLCX", p->trans);
1366 }
1367
Philipp Maierf4c0e372017-10-11 16:06:45 +02001368 /* Find the connection */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001369 conn = mgcp_conn_get_rtp(endp, conn_id);
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001370 if (!conn) {
1371 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CONNID]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001372 goto error3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001373 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001374 /* save the statistics of the current connection */
1375 mgcp_format_stats(stats, sizeof(stats), conn->conn);
1376
1377 /* delete connection */
Philipp Maier230e4fc2017-11-28 09:38:45 +01001378 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x deleting conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001379 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
1380 mgcp_conn_free(endp, conn_id);
1381 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001382 "DLCX: endpoint:0x%x connection successfully deleted\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001383 ENDPOINT_NUMBER(endp));
1384
1385 /* When all connections are closed, the endpoint will be released
1386 * in order to be ready to be used by another call. */
1387 if (llist_count(&endp->conns) <= 0) {
Philipp Maier1355d7e2018-02-01 14:30:06 +01001388 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001389 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001390 "DLCX: endpoint:0x%x endpoint released\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001391 ENDPOINT_NUMBER(endp));
1392 }
1393
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001394 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001395 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1396 MGCP_ENDP_DLCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001397
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001398 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_SUCCESS]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001399 if (silent)
1400 goto out_silent;
1401 return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats);
1402
1403error3:
1404 return create_err_response(endp, error_code, "DLCX", p->trans);
1405
1406out_silent:
Philipp Maier230e4fc2017-11-28 09:38:45 +01001407 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x silent exit\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001408 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001409 return NULL;
1410}
1411
Philipp Maier87bd9be2017-08-22 16:35:41 +02001412/* RSIP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001413static struct msgb *handle_rsip(struct mgcp_parse_data *p)
1414{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001415 /* TODO: Also implement the resetting of a specific endpoint
1416 * to make mgcp_send_reset_ep() work. Currently this will call
1417 * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1
1418 * to make read_call_agent() reset all endpoints when called
1419 * next time. In order to selectively reset endpoints some
1420 * mechanism to distinguish which endpoint shall be resetted
1421 * is needed */
1422
1423 LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n");
1424
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001425 if (p->cfg->reset_cb)
1426 p->cfg->reset_cb(p->endp->tcfg);
1427 return NULL;
1428}
1429
1430static char extract_tone(const char *line)
1431{
1432 const char *str = strstr(line, "D/");
1433 if (!str)
1434 return CHAR_MAX;
1435
1436 return str[2];
1437}
1438
Philipp Maier87bd9be2017-08-22 16:35:41 +02001439/* This can request like DTMF detection and forward, fax detection... it
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001440 * can also request when the notification should be send and such. We don't
Philipp Maier87bd9be2017-08-22 16:35:41 +02001441 * do this right now. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001442static struct msgb *handle_noti_req(struct mgcp_parse_data *p)
1443{
1444 int res = 0;
1445 char *line;
1446 char tone = CHAR_MAX;
1447
Philipp Maier87bd9be2017-08-22 16:35:41 +02001448 LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n");
1449
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001450 for_each_line(line, p->save) {
1451 switch (line[0]) {
1452 case 'S':
1453 tone = extract_tone(line);
1454 break;
1455 }
1456 }
1457
1458 /* we didn't see a signal request with a tone */
1459 if (tone == CHAR_MAX)
1460 return create_ok_response(p->endp, 200, "RQNT", p->trans);
1461
1462 if (p->cfg->rqnt_cb)
1463 res = p->cfg->rqnt_cb(p->endp, tone);
1464
1465 return res == 0 ?
Philipp Maier87bd9be2017-08-22 16:35:41 +02001466 create_ok_response(p->endp, 200, "RQNT", p->trans) :
1467 create_err_response(p->endp, res, "RQNT", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001468}
1469
Philipp Maier87bd9be2017-08-22 16:35:41 +02001470/* Connection keepalive timer, will take care that dummy packets are send
Harald Welte1d1b98f2017-12-25 10:03:40 +01001471 * regularly, so that NAT connections stay open */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001472static void mgcp_keepalive_timer_cb(void *_tcfg)
1473{
1474 struct mgcp_trunk_config *tcfg = _tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001475 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001476 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001477
Philipp Maiere726d4f2017-11-01 10:41:34 +01001478 LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001479 tcfg->trunk_nr);
1480
Philipp Maiere726d4f2017-11-01 10:41:34 +01001481 /* Do not accept invalid configuration values
1482 * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and
1483 * values greater 0 */
1484 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1485
1486 /* The dummy packet functionality has been disabled, we will exit
1487 * immediately, no further timer is scheduled, which means we will no
1488 * longer send dummy packets even when we did before */
1489 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER)
1490 return;
1491
1492 /* In cases where only one dummy packet is sent, we do not need
1493 * the timer since the functions that handle the CRCX and MDCX are
1494 * triggering the sending of the dummy packet. So we behave like in
1495 * the MGCP_KEEPALIVE_NEVER case */
1496 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001497 return;
1498
Philipp Maier87bd9be2017-08-22 16:35:41 +02001499 /* Send walk over all endpoints and send out dummy packets through
1500 * every connection present on each endpoint */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001501 for (i = 1; i < tcfg->number_endpoints; ++i) {
1502 struct mgcp_endpoint *endp = &tcfg->endpoints[i];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001503 llist_for_each_entry(conn, &endp->conns, entry) {
1504 if (conn->mode == MGCP_CONN_RECV_ONLY)
1505 send_dummy(endp, &conn->u.rtp);
1506 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001507 }
1508
Philipp Maiere726d4f2017-11-01 10:41:34 +01001509 /* Schedule the keepalive timer for the next round */
1510 LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001511 tcfg->trunk_nr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001512 osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval,
1513 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001514}
1515
1516void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval)
1517{
1518 tcfg->keepalive_interval = interval;
1519 osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg);
1520
1521 if (interval <= 0)
1522 osmo_timer_del(&tcfg->keepalive_timer);
1523 else
1524 osmo_timer_schedule(&tcfg->keepalive_timer,
1525 tcfg->keepalive_interval, 0);
1526}
1527
Stefan Sperling1e174872018-10-25 18:36:10 +02001528static int free_rate_counter_group(struct rate_ctr_group *rate_ctr_group)
1529{
1530 rate_ctr_group_free(rate_ctr_group);
1531 return 0;
1532}
1533
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001534static void alloc_mgcp_rate_counters(struct mgcp_trunk_config *trunk, void *ctx)
Stefan Sperling1e174872018-10-25 18:36:10 +02001535{
1536 /* FIXME: Each new rate counter group requires a unique index. At the
1537 * moment we generate an index using a counter, but perhaps there is
1538 * a better way of assigning indices? */
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001539 static unsigned int crcx_rate_ctr_index = 0;
1540 static unsigned int mdcx_rate_ctr_index = 0;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001541 static unsigned int dlcx_rate_ctr_index = 0;
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001542 static unsigned int all_rtp_conn_rate_ctr_index = 0;
Stefan Sperling1e174872018-10-25 18:36:10 +02001543
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001544 if (trunk->mgcp_crcx_ctr_group == NULL) {
1545 trunk->mgcp_crcx_ctr_group = rate_ctr_group_alloc(ctx, &mgcp_crcx_ctr_group_desc, crcx_rate_ctr_index);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001546 talloc_set_destructor(trunk->mgcp_crcx_ctr_group, free_rate_counter_group);
1547 crcx_rate_ctr_index++;
1548 }
1549 if (trunk->mgcp_mdcx_ctr_group == NULL) {
1550 trunk->mgcp_mdcx_ctr_group = rate_ctr_group_alloc(ctx, &mgcp_mdcx_ctr_group_desc, mdcx_rate_ctr_index);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001551 talloc_set_destructor(trunk->mgcp_mdcx_ctr_group, free_rate_counter_group);
1552 mdcx_rate_ctr_index++;
1553 }
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001554 if (trunk->mgcp_dlcx_ctr_group == NULL) {
1555 trunk->mgcp_dlcx_ctr_group = rate_ctr_group_alloc(ctx, &mgcp_dlcx_ctr_group_desc, dlcx_rate_ctr_index);
1556 talloc_set_destructor(trunk->mgcp_dlcx_ctr_group, free_rate_counter_group);
1557 dlcx_rate_ctr_index++;
1558 }
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001559 if (trunk->all_rtp_conn_stats == NULL) {
1560 trunk->all_rtp_conn_stats = rate_ctr_group_alloc(ctx, &all_rtp_conn_rate_ctr_group_desc,
1561 all_rtp_conn_rate_ctr_index);
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001562 talloc_set_destructor(trunk->all_rtp_conn_stats, free_rate_counter_group);
1563 all_rtp_conn_rate_ctr_index++;
1564 }
Stefan Sperling1e174872018-10-25 18:36:10 +02001565}
1566
Philipp Maier87bd9be2017-08-22 16:35:41 +02001567/*! allocate configuration with default values.
1568 * (called once at startup by main function) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001569struct mgcp_config *mgcp_config_alloc(void)
1570{
1571 struct mgcp_config *cfg;
1572
1573 cfg = talloc_zero(NULL, struct mgcp_config);
1574 if (!cfg) {
1575 LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n");
1576 return NULL;
1577 }
1578
Philipp Maier12943ea2018-01-17 15:40:25 +01001579 osmo_strlcpy(cfg->domain, "mgw", sizeof(cfg->domain));
1580
Philipp Maier87bd9be2017-08-22 16:35:41 +02001581 cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START;
1582 cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END;
1583 cfg->net_ports.last_port = cfg->net_ports.range_start;
1584
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001585 cfg->source_port = 2427;
1586 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
1587 cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
1588
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001589 cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
1590 cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
1591
1592 cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
1593
1594 /* default trunk handling */
1595 cfg->trunk.cfg = cfg;
1596 cfg->trunk.trunk_nr = 0;
1597 cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
1598 cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
1599 cfg->trunk.audio_payload = 126;
1600 cfg->trunk.audio_send_ptime = 1;
1601 cfg->trunk.audio_send_name = 1;
1602 cfg->trunk.omit_rtcp = 0;
1603 mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001604 alloc_mgcp_rate_counters(&cfg->trunk, cfg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001605
1606 INIT_LLIST_HEAD(&cfg->trunks);
1607
1608 return cfg;
1609}
1610
Philipp Maier87bd9be2017-08-22 16:35:41 +02001611/*! allocate configuration with default values.
1612 * (called once at startup by VTY)
1613 * \param[in] cfg mgcp configuration
1614 * \param[in] nr trunk number
1615 * \returns pointer to allocated trunk configuration */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001616struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
1617{
1618 struct mgcp_trunk_config *trunk;
1619
1620 trunk = talloc_zero(cfg, struct mgcp_trunk_config);
1621 if (!trunk) {
1622 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n");
1623 return NULL;
1624 }
1625
1626 trunk->cfg = cfg;
1627 trunk->trunk_type = MGCP_TRUNK_E1;
1628 trunk->trunk_nr = nr;
1629 trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
1630 trunk->audio_payload = 126;
1631 trunk->audio_send_ptime = 1;
1632 trunk->audio_send_name = 1;
Philipp Maierfcd06552017-11-10 17:32:22 +01001633 trunk->vty_number_endpoints = 33;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001634 trunk->omit_rtcp = 0;
1635 mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001636 alloc_mgcp_rate_counters(trunk, trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001637 llist_add_tail(&trunk->entry, &cfg->trunks);
Stefan Sperling1e174872018-10-25 18:36:10 +02001638
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001639 return trunk;
1640}
1641
Philipp Maier87bd9be2017-08-22 16:35:41 +02001642/*! get trunk configuration by trunk number (index).
1643 * \param[in] cfg mgcp configuration
1644 * \param[in] index trunk number
1645 * \returns pointer to trunk configuration, NULL on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001646struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
1647{
1648 struct mgcp_trunk_config *trunk;
1649
1650 llist_for_each_entry(trunk, &cfg->trunks, entry)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001651 if (trunk->trunk_nr == index)
1652 return trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001653
1654 return NULL;
1655}
1656
Philipp Maier87bd9be2017-08-22 16:35:41 +02001657/*! allocate endpoints and set default values.
1658 * (called once at startup by VTY)
1659 * \param[in] tcfg trunk configuration
1660 * \returns 0 on success, -1 on failure */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001661int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
1662{
1663 int i;
1664
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001665 tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001666 sizeof(struct mgcp_endpoint),
Philipp Maierfcd06552017-11-10 17:32:22 +01001667 tcfg->vty_number_endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001668 "endpoints");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001669 if (!tcfg->endpoints)
1670 return -1;
1671
Philipp Maierfcd06552017-11-10 17:32:22 +01001672 for (i = 0; i < tcfg->vty_number_endpoints; ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001673 INIT_LLIST_HEAD(&tcfg->endpoints[i].conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001674 tcfg->endpoints[i].cfg = tcfg->cfg;
1675 tcfg->endpoints[i].tcfg = tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001676
1677 /* NOTE: Currently all endpoints are of type RTP, this will
1678 * change when new variations are implemented */
1679 tcfg->endpoints[i].type = &ep_typeset.rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001680 }
1681
Philipp Maierfcd06552017-11-10 17:32:22 +01001682 tcfg->number_endpoints = tcfg->vty_number_endpoints;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001683 alloc_mgcp_rate_counters(tcfg, tcfg->cfg);
Stefan Sperling1e174872018-10-25 18:36:10 +02001684
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001685 return 0;
1686}
1687
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001688static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1689{
1690 return write(cfg->gw_fd.bfd.fd, buf, len);
1691}
1692
Philipp Maier87bd9be2017-08-22 16:35:41 +02001693/*! Reset all endpoints by sending RSIP message to self.
1694 * (called by VTY)
1695 * \param[in] endp trunk endpoint
1696 * \param[in] endpoint number
1697 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001698int mgcp_send_reset_all(struct mgcp_config *cfg)
1699{
Philipp Maier12943ea2018-01-17 15:40:25 +01001700 char buf[MGCP_ENDPOINT_MAXLEN + 128];
1701 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001702 int rc;
1703
Philipp Maier12943ea2018-01-17 15:40:25 +01001704 len = snprintf(buf, sizeof(buf),
1705 "RSIP 1 *@%s MGCP 1.0\r\n", cfg->domain);
1706 if (len < 0)
1707 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001708
Philipp Maier12943ea2018-01-17 15:40:25 +01001709 rc = send_agent(cfg, buf, len);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001710 if (rc <= 0)
1711 return -1;
1712
1713 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001714}
1715
Philipp Maier87bd9be2017-08-22 16:35:41 +02001716/*! Reset a single endpoint by sending RSIP message to self.
1717 * (called by VTY)
1718 * \param[in] endp trunk endpoint
1719 * \param[in] endpoint number
1720 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001721int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
1722{
Philipp Maier12943ea2018-01-17 15:40:25 +01001723 char buf[MGCP_ENDPOINT_MAXLEN + 128];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001724 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001725 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001726
1727 len = snprintf(buf, sizeof(buf),
Philipp Maier12943ea2018-01-17 15:40:25 +01001728 "RSIP 39 %x@%s MGCP 1.0\r\n", endpoint, endp->cfg->domain);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001729 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001730 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001731
Philipp Maier87bd9be2017-08-22 16:35:41 +02001732 rc = send_agent(endp->cfg, buf, len);
1733 if (rc <= 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001734 return -1;
1735
Philipp Maier87bd9be2017-08-22 16:35:41 +02001736 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001737}