blob: 4121b9f55c96b2088c39ace6b118c98c58dece70 [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;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100655 rtp->rfc5993_hr_convert = tcfg->rfc5993_hr_convert;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200656
657 LOGP(DLMGCP, LOGL_DEBUG,
658 "Configuring RTP endpoint: local port %d%s%s\n",
659 ntohs(rtp->rtp_port),
660 rtp->force_aligned_timing ? ", force constant timing" : "",
661 rtp->force_constant_ssrc ? ", force constant ssrc" : "");
662}
663
664uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
665 struct mgcp_rtp_end *rtp)
666{
667 int f = 0;
668
669 /* Get the number of frames per channel and packet */
670 if (rtp->frames_per_packet)
671 f = rtp->frames_per_packet;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200672 else if (rtp->packet_duration_ms && rtp->codec->frame_duration_num) {
673 int den = 1000 * rtp->codec->frame_duration_num;
674 f = (rtp->packet_duration_ms * rtp->codec->frame_duration_den +
Philipp Maier87bd9be2017-08-22 16:35:41 +0200675 den / 2)
676 / den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200677 }
678
Philipp Maierbc0346e2018-06-07 09:52:16 +0200679 return rtp->codec->rate * f * rtp->codec->frame_duration_num /
680 rtp->codec->frame_duration_den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200681}
682
683static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line)
684{
685 if (!endp->cfg->osmux_init) {
686 if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) {
687 LOGP(DLMGCP, LOGL_ERROR, "Cannot init OSMUX\n");
688 return -1;
689 }
690 LOGP(DLMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n");
691 }
692
693 return mgcp_parse_osmux_cid(line);
694}
695
Philipp Maierbc0346e2018-06-07 09:52:16 +0200696/* Process codec information contained in CRCX/MDCX */
697static int handle_codec_info(struct mgcp_conn_rtp *conn,
698 struct mgcp_parse_data *p, int have_sdp, bool crcx)
699{
700 struct mgcp_endpoint *endp = p->endp;
701 int rc;
702 char *cmd;
703
704 if (crcx)
705 cmd = "CRCX";
706 else
707 cmd = "MDCX";
708
709 /* Collect codec information */
710 if (have_sdp) {
711 /* If we have SDP, we ignore the local connection options and
712 * use only the SDP information. */
713 mgcp_codec_reset_all(conn);
714 rc = mgcp_parse_sdp_data(endp, conn, p);
715 if (rc != 0) {
716 LOGP(DLMGCP, LOGL_ERROR,
717 "%s: endpoint:%x sdp not parseable\n", cmd,
718 ENDPOINT_NUMBER(endp));
719
720 /* See also RFC 3661: Protocol error */
721 return 510;
722 }
723 } else if (endp->local_options.codec) {
724 /* When no SDP is available, we use the codec information from
725 * the local connection options (if present) */
726 mgcp_codec_reset_all(conn);
Philipp Maier228e5912019-03-05 13:56:59 +0100727 rc = mgcp_codec_add(conn, PTYPE_UNDEFINED, endp->local_options.codec, NULL);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200728 if (rc != 0)
729 goto error;
730 }
731
732 /* Make sure we always set a sane default codec */
733 if (conn->end.codecs_assigned == 0) {
734 /* When SDP and/or LCO did not supply any codec information,
735 * than it makes sense to pick a sane default: (payload-type 0,
736 * PCMU), see also: OS#2658 */
737 mgcp_codec_reset_all(conn);
Philipp Maier228e5912019-03-05 13:56:59 +0100738 rc = mgcp_codec_add(conn, 0, NULL, NULL);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200739 if (rc != 0)
740 goto error;
741 }
742
743 /* Make codec decision */
744 if (mgcp_codec_decide(conn) != 0)
745 goto error;
746
747 return 0;
748
749error:
750 LOGP(DLMGCP, LOGL_ERROR,
751 "%s: endpoint:0x%x codec negotiation failure\n", cmd,
752 ENDPOINT_NUMBER(endp));
753
754 /* See also RFC 3661: Codec negotiation failure */
755 return 534;
756}
757
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200758static bool parse_x_osmo_ign(struct mgcp_endpoint *endp, char *line)
759{
760 char *saveptr = NULL;
761
762 if (strncmp(line, MGCP_X_OSMO_IGN_HEADER, strlen(MGCP_X_OSMO_IGN_HEADER)))
763 return false;
764 line += strlen(MGCP_X_OSMO_IGN_HEADER);
765
766 while (1) {
767 char *token = strtok_r(line, " ", &saveptr);
768 line = NULL;
769 if (!token)
770 break;
771
772 if (!strcmp(token, "C"))
773 endp->x_osmo_ign |= MGCP_X_OSMO_IGN_CALLID;
774 else
Pau Espin Pedrol9ecceb62018-10-16 15:35:58 +0200775 LOGP(DLMGCP, LOGL_ERROR, "endpoint 0x%x: received unknown X-Osmo-IGN item '%s'\n",
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200776 ENDPOINT_NUMBER(endp), token);
777 }
778
779 return true;
780}
781
Philipp Maier87bd9be2017-08-22 16:35:41 +0200782/* CRCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200783static struct msgb *handle_create_con(struct mgcp_parse_data *p)
784{
Stefan Sperling1e174872018-10-25 18:36:10 +0200785 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200786 struct mgcp_endpoint *endp = p->endp;
Stefan Sperling9270e912018-10-29 14:10:00 +0100787 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_crcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200788 int error_code = 400;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200789 const char *local_options = NULL;
790 const char *callid = NULL;
791 const char *mode = NULL;
792 char *line;
793 int have_sdp = 0, osmux_cid = -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200794 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100795 struct mgcp_conn *_conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200796 char conn_name[512];
Philipp Maiera390d0b2018-01-31 17:30:19 +0100797 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200798
799 LOGP(DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200800
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200801 /* parse CallID C: and LocalParameters L: */
802 for_each_line(line, p->save) {
803 if (!mgcp_check_param(endp, line))
804 continue;
805
806 switch (line[0]) {
807 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200808 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200809 break;
810 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200811 callid = (const char *)line + 3;
812 break;
813 case 'I':
Philipp Maierffd75e42017-11-22 11:44:50 +0100814 /* It is illegal to send a connection identifier
815 * together with a CRCX, the MGW will assign the
816 * connection identifier by itself on CRCX */
Stefan Sperling9270e912018-10-29 14:10:00 +0100817 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_BAD_ACTION]);
Philipp Maierffd75e42017-11-22 11:44:50 +0100818 return create_err_response(NULL, 523, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200819 break;
820 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200821 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200822 break;
823 case 'X':
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200824 if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
825 /* If osmux is disabled, just skip setting it up */
826 if (!p->endp->cfg->osmux)
827 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200828 osmux_cid = mgcp_osmux_setup(endp, line);
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200829 break;
830 }
831
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200832 if (parse_x_osmo_ign(endp, line))
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200833 break;
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200834
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200835 /* Ignore unknown X-headers */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200836 break;
837 case '\0':
838 have_sdp = 1;
839 goto mgcp_header_done;
840 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200841 LOGP(DLMGCP, LOGL_NOTICE,
842 "CRCX: endpoint:%x unhandled option: '%c'/%d\n",
843 ENDPOINT_NUMBER(endp), *line, *line);
Stefan Sperling9270e912018-10-29 14:10:00 +0100844 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +0100845 return create_err_response(NULL, 539, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200846 break;
847 }
848 }
849
850mgcp_header_done:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200851 /* Check parameters */
852 if (!callid) {
853 LOGP(DLMGCP, LOGL_ERROR,
854 "CRCX: endpoint:%x insufficient parameters, missing callid\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200855 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +0100856 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_MISSING_CALLID]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100857 return create_err_response(endp, 516, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200858 }
859
Philipp Maier87bd9be2017-08-22 16:35:41 +0200860 if (!mode) {
861 LOGP(DLMGCP, LOGL_ERROR,
862 "CRCX: endpoint:%x insufficient parameters, missing mode\n",
863 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +0100864 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_MODE]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100865 return create_err_response(endp, 517, "CRCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200866 }
867
Philipp Maier87bd9be2017-08-22 16:35:41 +0200868 /* Check if we are able to accept the creation of another connection */
869 if (llist_count(&endp->conns) >= endp->type->max_conns) {
870 LOGP(DLMGCP, LOGL_ERROR,
871 "CRCX: endpoint:%x endpoint full, max. %i connections allowed!\n",
872 endp->type->max_conns, ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200873 if (tcfg->force_realloc) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200874 /* There is no more room for a connection, make some
875 * room by blindly tossing the oldest of the two two
876 * connections */
877 mgcp_conn_free_oldest(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200878 } else {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200879 /* There is no more room for a connection, leave
880 * everything as it is and return with an error */
Stefan Sperling9270e912018-10-29 14:10:00 +0100881 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_LIMIT_EXCEEDED]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100882 return create_err_response(endp, 540, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200883 }
884 }
885
Philipp Maier87bd9be2017-08-22 16:35:41 +0200886 /* Check if this endpoint already serves a call, if so, check if the
887 * callids match up so that we are sure that this is our call */
888 if (endp->callid && mgcp_verify_call_id(endp, callid)) {
889 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100890 "CRCX: endpoint:0x%x allready seized by other call (%s)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200891 ENDPOINT_NUMBER(endp), endp->callid);
892 if (tcfg->force_realloc)
893 /* This is not our call, toss everything by releasing
894 * the entire endpoint. (rude!) */
Philipp Maier1355d7e2018-02-01 14:30:06 +0100895 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200896 else {
897 /* This is not our call, leave everything as it is and
898 * return with an error. */
Stefan Sperling9270e912018-10-29 14:10:00 +0100899 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_UNKNOWN_CALLID]);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200900 return create_err_response(endp, 400, "CRCX", p->trans);
901 }
902 }
903
904 /* Set the callid, creation of another connection will only be possible
Harald Welte1d1b98f2017-12-25 10:03:40 +0100905 * when the callid matches up. (Connections are distinguished by their
Philipp Maier87bd9be2017-08-22 16:35:41 +0200906 * connection ids) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200907 endp->callid = talloc_strdup(tcfg->endpoints, callid);
908
Philipp Maierffd75e42017-11-22 11:44:50 +0100909 snprintf(conn_name, sizeof(conn_name), "%s", callid);
910 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP, conn_name);
911 if (!_conn) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200912 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100913 "CRCX: endpoint:0x%x unable to allocate RTP connection\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200914 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +0100915 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_ALLOC_CONN]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200916 goto error2;
917
Philipp Maier87bd9be2017-08-22 16:35:41 +0200918 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100919 conn = mgcp_conn_get_rtp(endp, _conn->id);
920 OSMO_ASSERT(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200921
922 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
923 error_code = 517;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100924 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_MODE]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200925 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200926 }
927
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200928 /* Annotate Osmux circuit ID and set it to negotiating state until this
Philipp Maier87bd9be2017-08-22 16:35:41 +0200929 * is fully set up from the dummy load. */
930 conn->osmux.state = OSMUX_STATE_DISABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200931 if (osmux_cid >= 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200932 conn->osmux.cid = osmux_cid;
933 conn->osmux.state = OSMUX_STATE_NEGOTIATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200934 } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) {
935 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100936 "CRCX: endpoint:0x%x osmux only and no osmux offered\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200937 ENDPOINT_NUMBER(endp));
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100938 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_OSMUX]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200939 goto error2;
940 }
941
Philipp Maierbc0346e2018-06-07 09:52:16 +0200942 /* Set local connection options, if present */
943 if (local_options) {
944 rc = set_local_cx_options(endp->tcfg->endpoints,
945 &endp->local_options, local_options);
946 if (rc != 0) {
947 LOGP(DLMGCP, LOGL_ERROR,
948 "CRCX: endpoint:%x inavlid local connection options!\n",
949 ENDPOINT_NUMBER(endp));
950 error_code = rc;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100951 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS]);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200952 goto error2;
953 }
954 }
955
956 /* Handle codec information and decide for a suitable codec */
957 rc = handle_codec_info(conn, p, have_sdp, true);
958 mgcp_codec_summary(conn);
959 if (rc) {
960 error_code = rc;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100961 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_CODEC_NEGOTIATION]);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200962 goto error2;
963 }
964
Philipp Maier87bd9be2017-08-22 16:35:41 +0200965 conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints,
966 tcfg->audio_fmtp_extra);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200967
Philipp Maier87bd9be2017-08-22 16:35:41 +0200968 if (p->cfg->force_ptime) {
969 conn->end.packet_duration_ms = p->cfg->force_ptime;
970 conn->end.force_output_ptime = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200971 }
972
Philipp Maier1cb1e382017-11-02 17:16:04 +0100973 mgcp_rtp_end_config(endp, 0, &conn->end);
974
Philipp Maierc3cc6542018-02-02 12:58:42 +0100975 /* check connection mode setting */
976 if (conn->conn->mode != MGCP_CONN_LOOPBACK
977 && conn->conn->mode != MGCP_CONN_RECV_ONLY
978 && conn->end.rtp_port == 0) {
979 LOGP(DLMGCP, LOGL_ERROR,
980 "CRCX: endpoint:%x selected connection mode type requires an opposite end!\n",
981 ENDPOINT_NUMBER(endp));
982 error_code = 527;
Stefan Sperling9270e912018-10-29 14:10:00 +0100983 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC]);
Philipp Maierc3cc6542018-02-02 12:58:42 +0100984 goto error2;
985 }
986
Philipp Maier1cb1e382017-11-02 17:16:04 +0100987 if (allocate_port(endp, conn) != 0) {
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100988 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_BIND_PORT]);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100989 goto error2;
990 }
991
Philipp Maier87bd9be2017-08-22 16:35:41 +0200992 if (setup_rtp_processing(endp, conn) != 0) {
993 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100994 "CRCX: endpoint:0x%x could not start RTP processing!\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200995 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +0100996 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_START_RTP]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200997 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200998 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200999
1000 /* policy CB */
1001 if (p->cfg->policy_cb) {
1002 int rc;
1003 rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001004 MGCP_ENDP_CRCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001005 switch (rc) {
1006 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001007 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001008 "CRCX: endpoint:0x%x CRCX rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001009 ENDPOINT_NUMBER(endp));
Philipp Maier1355d7e2018-02-01 14:30:06 +01001010 mgcp_endp_release(endp);
Stefan Sperling9270e912018-10-29 14:10:00 +01001011 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001012 return create_err_response(endp, 400, "CRCX", p->trans);
1013 break;
1014 case MGCP_POLICY_DEFER:
1015 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001016 return NULL;
1017 break;
1018 case MGCP_POLICY_CONT:
1019 /* just continue */
1020 break;
1021 }
1022 }
1023
Philipp Maier87bd9be2017-08-22 16:35:41 +02001024 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001025 "CRCX: endpoint:0x%x Creating connection: CI: %s port: %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001026 ENDPOINT_NUMBER(endp), conn->conn->id, conn->end.local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001027 if (p->cfg->change_cb)
1028 p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX);
1029
Philipp Maiere726d4f2017-11-01 10:41:34 +01001030 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
1031 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001032 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
Philipp Maiere726d4f2017-11-01 10:41:34 +01001033 && tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001034 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001035
Philipp Maier87bd9be2017-08-22 16:35:41 +02001036 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001037 "CRCX: endpoint:0x%x connection successfully created\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001038 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +01001039 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_SUCCESS]);
Philipp Maier55295f72018-01-15 14:00:28 +01001040 return create_response_with_sdp(endp, conn, "CRCX", p->trans, true);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001041error2:
Philipp Maier1355d7e2018-02-01 14:30:06 +01001042 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001043 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier3c8ccb62018-06-04 09:59:24 +02001044 "CRCX: endpoint:0x%x unable to create connection\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001045 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001046 return create_err_response(endp, error_code, "CRCX", p->trans);
1047}
1048
Philipp Maierbc0346e2018-06-07 09:52:16 +02001049
1050
1051
1052
Philipp Maier87bd9be2017-08-22 16:35:41 +02001053/* MDCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001054static struct msgb *handle_modify_con(struct mgcp_parse_data *p)
1055{
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001056 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001057 struct mgcp_endpoint *endp = p->endp;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001058 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_mdcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001059 int error_code = 500;
1060 int silent = 0;
1061 int have_sdp = 0;
1062 char *line;
1063 const char *local_options = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001064 const char *mode = NULL;
1065 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier01d24a32017-11-21 17:26:09 +01001066 const char *conn_id = NULL;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001067 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001068
1069 LOGP(DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001070
Philipp Maier5656fbf2018-02-02 14:41:58 +01001071 /* Prohibit wildcarded requests */
1072 if (endp->wildcarded_req) {
1073 LOGP(DLMGCP, LOGL_ERROR,
1074 "MDCX: endpoint:0x%x wildcarded endpoint names not supported.\n",
1075 ENDPOINT_NUMBER(endp));
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001076 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_WILDCARD]);
Philipp Maier5656fbf2018-02-02 14:41:58 +01001077 return create_err_response(endp, 507, "MDCX", p->trans);
1078 }
1079
Philipp Maier87bd9be2017-08-22 16:35:41 +02001080 if (llist_count(&endp->conns) <= 0) {
1081 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001082 "MDCX: endpoint:0x%x endpoint is not holding a connection.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001083 ENDPOINT_NUMBER(endp));
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001084 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_CONN]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001085 return create_err_response(endp, 400, "MDCX", p->trans);
1086 }
1087
1088 for_each_line(line, p->save) {
1089 if (!mgcp_check_param(endp, line))
1090 continue;
1091
1092 switch (line[0]) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001093 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +01001094 if (mgcp_verify_call_id(endp, line + 3) != 0) {
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001095 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CALLID]);
Harald Weltee35eeae2017-12-28 13:47:37 +01001096 error_code = 516;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001097 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001098 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001099 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001100 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001101 conn_id = (const char *)line + 3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001102 if ((error_code = mgcp_verify_ci(endp, conn_id))) {
1103 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CONNID]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001104 goto error3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001105 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001106 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001107 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001108 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001109 break;
1110 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001111 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001112 break;
1113 case 'Z':
1114 silent = strcmp("noanswer", line + 3) == 0;
1115 break;
1116 case '\0':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001117 have_sdp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001118 goto mgcp_header_done;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001119 break;
1120 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001121 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001122 "MDCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001123 ENDPOINT_NUMBER(endp), line[0], line[0]);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001124 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001125 return create_err_response(NULL, 539, "MDCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001126 break;
1127 }
1128 }
1129
Philipp Maier87bd9be2017-08-22 16:35:41 +02001130mgcp_header_done:
Philipp Maier01d24a32017-11-21 17:26:09 +01001131 if (!conn_id) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001132 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001133 "MDCX: endpoint:0x%x insufficient parameters, missing ci (connectionIdentifier)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001134 ENDPOINT_NUMBER(endp));
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001135 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_CONNID]);
Harald Weltee35eeae2017-12-28 13:47:37 +01001136 return create_err_response(endp, 515, "MDCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001137 }
1138
1139 conn = mgcp_conn_get_rtp(endp, conn_id);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001140 if (!conn) {
1141 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_CONN_NOT_FOUND]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001142 return create_err_response(endp, 400, "MDCX", p->trans);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001143 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001144
Oliver Smithe36b7752019-01-22 16:31:36 +01001145 mgcp_conn_watchdog_kick(conn->conn);
1146
Philipp Maier87bd9be2017-08-22 16:35:41 +02001147 if (mode) {
1148 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001149 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_MODE]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001150 error_code = 517;
1151 goto error3;
1152 }
1153 } else
1154 conn->conn->mode = conn->conn->mode_orig;
1155
Philipp Maierbc0346e2018-06-07 09:52:16 +02001156 /* Set local connection options, if present */
1157 if (local_options) {
1158 rc = set_local_cx_options(endp->tcfg->endpoints,
1159 &endp->local_options, local_options);
1160 if (rc != 0) {
1161 LOGP(DLMGCP, LOGL_ERROR,
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001162 "MDCX: endpoint:%x invalid local connection options!\n",
Philipp Maierbc0346e2018-06-07 09:52:16 +02001163 ENDPOINT_NUMBER(endp));
1164 error_code = rc;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001165 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS]);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001166 goto error3;
1167 }
1168 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001169
Philipp Maierbc0346e2018-06-07 09:52:16 +02001170 /* Handle codec information and decide for a suitable codec */
1171 rc = handle_codec_info(conn, p, have_sdp, false);
1172 mgcp_codec_summary(conn);
1173 if (rc) {
Philipp Maieraf07f662018-02-02 11:34:02 +01001174 error_code = rc;
1175 goto error3;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001176 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001177
Philipp Maierc3cc6542018-02-02 12:58:42 +01001178 /* check connection mode setting */
1179 if (conn->conn->mode != MGCP_CONN_LOOPBACK
1180 && conn->conn->mode != MGCP_CONN_RECV_ONLY
1181 && conn->end.rtp_port == 0) {
1182 LOGP(DLMGCP, LOGL_ERROR,
1183 "MDCX: endpoint:%x selected connection mode type requires an opposite end!\n",
1184 ENDPOINT_NUMBER(endp));
1185 error_code = 527;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001186 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC]);
Philipp Maierc3cc6542018-02-02 12:58:42 +01001187 goto error3;
1188 }
1189
Philipp Maierbc0346e2018-06-07 09:52:16 +02001190
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001191 if (setup_rtp_processing(endp, conn) != 0) {
1192 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_START_RTP]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001193 goto error3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001194 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001195
Philipp Maier87bd9be2017-08-22 16:35:41 +02001196
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001197 /* policy CB */
1198 if (p->cfg->policy_cb) {
1199 int rc;
1200 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001201 MGCP_ENDP_MDCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001202 switch (rc) {
1203 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001204 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001205 "MDCX: endpoint:0x%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001206 ENDPOINT_NUMBER(endp));
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001207 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001208 if (silent)
1209 goto out_silent;
1210 return create_err_response(endp, 400, "MDCX", p->trans);
1211 break;
1212 case MGCP_POLICY_DEFER:
1213 /* stop processing */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001214 LOGP(DLMGCP, LOGL_DEBUG,
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001215 "MDCX: endpoint:0x%x deferred by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001216 ENDPOINT_NUMBER(endp));
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001217 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_DEFERRED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001218 return NULL;
1219 break;
1220 case MGCP_POLICY_CONT:
1221 /* just continue */
1222 break;
1223 }
1224 }
1225
Philipp Maier87bd9be2017-08-22 16:35:41 +02001226 mgcp_rtp_end_config(endp, 1, &conn->end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001227
1228 /* modify */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001229 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001230 "MDCX: endpoint:0x%x modified conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001231 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001232 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001233 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1234 MGCP_ENDP_MDCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001235
Philipp Maiere726d4f2017-11-01 10:41:34 +01001236 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
1237 OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1238 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
1239 && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001240 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001241
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001242 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_SUCCESS]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001243 if (silent)
1244 goto out_silent;
1245
Philipp Maier87bd9be2017-08-22 16:35:41 +02001246 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001247 "MDCX: endpoint:0x%x connection successfully modified\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001248 ENDPOINT_NUMBER(endp));
Philipp Maier55295f72018-01-15 14:00:28 +01001249 return create_response_with_sdp(endp, conn, "MDCX", p->trans, false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001250error3:
1251 return create_err_response(endp, error_code, "MDCX", p->trans);
1252
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001253out_silent:
Philipp Maier230e4fc2017-11-28 09:38:45 +01001254 LOGP(DLMGCP, LOGL_DEBUG, "MDCX: endpoint:0x%x silent exit\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001255 ENDPOINT_NUMBER(endp));
1256 return NULL;
1257}
1258
Philipp Maier87bd9be2017-08-22 16:35:41 +02001259/* DLCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001260static struct msgb *handle_delete_con(struct mgcp_parse_data *p)
1261{
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001262 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001263 struct mgcp_endpoint *endp = p->endp;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001264 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_dlcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001265 int error_code = 400;
1266 int silent = 0;
1267 char *line;
1268 char stats[1048];
Philipp Maier01d24a32017-11-21 17:26:09 +01001269 const char *conn_id = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001270 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001271
1272 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001273 "DLCX: endpoint:0x%x deleting connection ...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001274 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001275
Philipp Maier5656fbf2018-02-02 14:41:58 +01001276 /* Prohibit wildcarded requests */
1277 if (endp->wildcarded_req) {
1278 LOGP(DLMGCP, LOGL_ERROR,
1279 "DLCX: endpoint:0x%x wildcarded endpoint names not supported.\n",
1280 ENDPOINT_NUMBER(endp));
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001281 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_WILDCARD]);
Philipp Maier5656fbf2018-02-02 14:41:58 +01001282 return create_err_response(endp, 507, "DLCX", p->trans);
1283 }
1284
Philipp Maier87bd9be2017-08-22 16:35:41 +02001285 if (llist_count(&endp->conns) <= 0) {
1286 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001287 "DLCX: endpoint:0x%x endpoint is not holding a connection.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001288 ENDPOINT_NUMBER(endp));
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001289 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_NO_CONN]);
Harald Weltee35eeae2017-12-28 13:47:37 +01001290 return create_err_response(endp, 515, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001291 }
1292
1293 for_each_line(line, p->save) {
1294 if (!mgcp_check_param(endp, line))
1295 continue;
1296
1297 switch (line[0]) {
1298 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +01001299 if (mgcp_verify_call_id(endp, line + 3) != 0) {
1300 error_code = 516;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001301 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CALLID]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001302 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001303 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001304 break;
1305 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001306 conn_id = (const char *)line + 3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001307 if ((error_code = mgcp_verify_ci(endp, conn_id))) {
1308 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CONNID]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001309 goto error3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001310 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001311 break;
1312 case 'Z':
1313 silent = strcmp("noanswer", line + 3) == 0;
1314 break;
1315 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001316 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001317 "DLCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001318 ENDPOINT_NUMBER(endp), line[0], line[0]);
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001319 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001320 return create_err_response(NULL, 539, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001321 break;
1322 }
1323 }
1324
1325 /* policy CB */
1326 if (p->cfg->policy_cb) {
1327 int rc;
1328 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001329 MGCP_ENDP_DLCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001330 switch (rc) {
1331 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001332 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001333 "DLCX: endpoint:0x%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001334 ENDPOINT_NUMBER(endp));
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001335 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001336 if (silent)
1337 goto out_silent;
1338 return create_err_response(endp, 400, "DLCX", p->trans);
1339 break;
1340 case MGCP_POLICY_DEFER:
1341 /* stop processing */
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001342 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_DEFERRED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001343 return NULL;
1344 break;
1345 case MGCP_POLICY_CONT:
1346 /* just continue */
1347 break;
1348 }
1349 }
1350
Philipp Maierf4c0e372017-10-11 16:06:45 +02001351 /* When no connection id is supplied, we will interpret this as a
1352 * wildcarded DLCX and drop all connections at once. (See also
1353 * RFC3435 Section F.7) */
Philipp Maier01d24a32017-11-21 17:26:09 +01001354 if (!conn_id) {
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001355 int num_conns = llist_count(&endp->conns);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001356 LOGP(DLMGCP, LOGL_NOTICE,
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001357 "DLCX: endpoint:0x%x missing ci (connectionIdentifier), will remove all connections (%d total) at once\n",
1358 num_conns, ENDPOINT_NUMBER(endp));
1359
1360 if (num_conns > 0)
1361 rate_ctr_add(&rate_ctrs->ctr[MGCP_DLCX_SUCCESS], num_conns);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001362
Philipp Maier1355d7e2018-02-01 14:30:06 +01001363 mgcp_endp_release(endp);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001364
1365 /* Note: In this case we do not return any statistics,
1366 * as we assume that the client is not interested in
1367 * this case. */
1368 return create_ok_response(endp, 200, "DLCX", p->trans);
1369 }
1370
Philipp Maierf4c0e372017-10-11 16:06:45 +02001371 /* Find the connection */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001372 conn = mgcp_conn_get_rtp(endp, conn_id);
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001373 if (!conn) {
1374 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CONNID]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001375 goto error3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001376 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001377 /* save the statistics of the current connection */
1378 mgcp_format_stats(stats, sizeof(stats), conn->conn);
1379
1380 /* delete connection */
Philipp Maier230e4fc2017-11-28 09:38:45 +01001381 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x deleting conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001382 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
1383 mgcp_conn_free(endp, conn_id);
1384 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001385 "DLCX: endpoint:0x%x connection successfully deleted\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001386 ENDPOINT_NUMBER(endp));
1387
1388 /* When all connections are closed, the endpoint will be released
1389 * in order to be ready to be used by another call. */
1390 if (llist_count(&endp->conns) <= 0) {
Philipp Maier1355d7e2018-02-01 14:30:06 +01001391 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001392 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001393 "DLCX: endpoint:0x%x endpoint released\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001394 ENDPOINT_NUMBER(endp));
1395 }
1396
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001397 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001398 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1399 MGCP_ENDP_DLCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001400
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001401 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_SUCCESS]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001402 if (silent)
1403 goto out_silent;
1404 return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats);
1405
1406error3:
1407 return create_err_response(endp, error_code, "DLCX", p->trans);
1408
1409out_silent:
Philipp Maier230e4fc2017-11-28 09:38:45 +01001410 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x silent exit\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001411 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001412 return NULL;
1413}
1414
Philipp Maier87bd9be2017-08-22 16:35:41 +02001415/* RSIP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001416static struct msgb *handle_rsip(struct mgcp_parse_data *p)
1417{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001418 /* TODO: Also implement the resetting of a specific endpoint
1419 * to make mgcp_send_reset_ep() work. Currently this will call
1420 * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1
1421 * to make read_call_agent() reset all endpoints when called
1422 * next time. In order to selectively reset endpoints some
1423 * mechanism to distinguish which endpoint shall be resetted
1424 * is needed */
1425
1426 LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n");
1427
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001428 if (p->cfg->reset_cb)
1429 p->cfg->reset_cb(p->endp->tcfg);
1430 return NULL;
1431}
1432
1433static char extract_tone(const char *line)
1434{
1435 const char *str = strstr(line, "D/");
1436 if (!str)
1437 return CHAR_MAX;
1438
1439 return str[2];
1440}
1441
Philipp Maier87bd9be2017-08-22 16:35:41 +02001442/* This can request like DTMF detection and forward, fax detection... it
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001443 * can also request when the notification should be send and such. We don't
Philipp Maier87bd9be2017-08-22 16:35:41 +02001444 * do this right now. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001445static struct msgb *handle_noti_req(struct mgcp_parse_data *p)
1446{
1447 int res = 0;
1448 char *line;
1449 char tone = CHAR_MAX;
1450
Philipp Maier87bd9be2017-08-22 16:35:41 +02001451 LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n");
1452
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001453 for_each_line(line, p->save) {
1454 switch (line[0]) {
1455 case 'S':
1456 tone = extract_tone(line);
1457 break;
1458 }
1459 }
1460
1461 /* we didn't see a signal request with a tone */
1462 if (tone == CHAR_MAX)
1463 return create_ok_response(p->endp, 200, "RQNT", p->trans);
1464
1465 if (p->cfg->rqnt_cb)
1466 res = p->cfg->rqnt_cb(p->endp, tone);
1467
1468 return res == 0 ?
Philipp Maier87bd9be2017-08-22 16:35:41 +02001469 create_ok_response(p->endp, 200, "RQNT", p->trans) :
1470 create_err_response(p->endp, res, "RQNT", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001471}
1472
Philipp Maier87bd9be2017-08-22 16:35:41 +02001473/* Connection keepalive timer, will take care that dummy packets are send
Harald Welte1d1b98f2017-12-25 10:03:40 +01001474 * regularly, so that NAT connections stay open */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001475static void mgcp_keepalive_timer_cb(void *_tcfg)
1476{
1477 struct mgcp_trunk_config *tcfg = _tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001478 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001479 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001480
Philipp Maiere726d4f2017-11-01 10:41:34 +01001481 LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001482 tcfg->trunk_nr);
1483
Philipp Maiere726d4f2017-11-01 10:41:34 +01001484 /* Do not accept invalid configuration values
1485 * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and
1486 * values greater 0 */
1487 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1488
1489 /* The dummy packet functionality has been disabled, we will exit
1490 * immediately, no further timer is scheduled, which means we will no
1491 * longer send dummy packets even when we did before */
1492 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER)
1493 return;
1494
1495 /* In cases where only one dummy packet is sent, we do not need
1496 * the timer since the functions that handle the CRCX and MDCX are
1497 * triggering the sending of the dummy packet. So we behave like in
1498 * the MGCP_KEEPALIVE_NEVER case */
1499 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001500 return;
1501
Philipp Maier87bd9be2017-08-22 16:35:41 +02001502 /* Send walk over all endpoints and send out dummy packets through
1503 * every connection present on each endpoint */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001504 for (i = 1; i < tcfg->number_endpoints; ++i) {
1505 struct mgcp_endpoint *endp = &tcfg->endpoints[i];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001506 llist_for_each_entry(conn, &endp->conns, entry) {
1507 if (conn->mode == MGCP_CONN_RECV_ONLY)
1508 send_dummy(endp, &conn->u.rtp);
1509 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001510 }
1511
Philipp Maiere726d4f2017-11-01 10:41:34 +01001512 /* Schedule the keepalive timer for the next round */
1513 LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001514 tcfg->trunk_nr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001515 osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval,
1516 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001517}
1518
1519void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval)
1520{
1521 tcfg->keepalive_interval = interval;
1522 osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg);
1523
1524 if (interval <= 0)
1525 osmo_timer_del(&tcfg->keepalive_timer);
1526 else
1527 osmo_timer_schedule(&tcfg->keepalive_timer,
1528 tcfg->keepalive_interval, 0);
1529}
1530
Stefan Sperling1e174872018-10-25 18:36:10 +02001531static int free_rate_counter_group(struct rate_ctr_group *rate_ctr_group)
1532{
1533 rate_ctr_group_free(rate_ctr_group);
1534 return 0;
1535}
1536
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001537static void alloc_mgcp_rate_counters(struct mgcp_trunk_config *trunk, void *ctx)
Stefan Sperling1e174872018-10-25 18:36:10 +02001538{
1539 /* FIXME: Each new rate counter group requires a unique index. At the
1540 * moment we generate an index using a counter, but perhaps there is
1541 * a better way of assigning indices? */
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001542 static unsigned int crcx_rate_ctr_index = 0;
1543 static unsigned int mdcx_rate_ctr_index = 0;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001544 static unsigned int dlcx_rate_ctr_index = 0;
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001545 static unsigned int all_rtp_conn_rate_ctr_index = 0;
Stefan Sperling1e174872018-10-25 18:36:10 +02001546
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001547 if (trunk->mgcp_crcx_ctr_group == NULL) {
1548 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 +01001549 talloc_set_destructor(trunk->mgcp_crcx_ctr_group, free_rate_counter_group);
1550 crcx_rate_ctr_index++;
1551 }
1552 if (trunk->mgcp_mdcx_ctr_group == NULL) {
1553 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 +01001554 talloc_set_destructor(trunk->mgcp_mdcx_ctr_group, free_rate_counter_group);
1555 mdcx_rate_ctr_index++;
1556 }
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001557 if (trunk->mgcp_dlcx_ctr_group == NULL) {
1558 trunk->mgcp_dlcx_ctr_group = rate_ctr_group_alloc(ctx, &mgcp_dlcx_ctr_group_desc, dlcx_rate_ctr_index);
1559 talloc_set_destructor(trunk->mgcp_dlcx_ctr_group, free_rate_counter_group);
1560 dlcx_rate_ctr_index++;
1561 }
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001562 if (trunk->all_rtp_conn_stats == NULL) {
1563 trunk->all_rtp_conn_stats = rate_ctr_group_alloc(ctx, &all_rtp_conn_rate_ctr_group_desc,
1564 all_rtp_conn_rate_ctr_index);
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001565 talloc_set_destructor(trunk->all_rtp_conn_stats, free_rate_counter_group);
1566 all_rtp_conn_rate_ctr_index++;
1567 }
Stefan Sperling1e174872018-10-25 18:36:10 +02001568}
1569
Philipp Maier87bd9be2017-08-22 16:35:41 +02001570/*! allocate configuration with default values.
1571 * (called once at startup by main function) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001572struct mgcp_config *mgcp_config_alloc(void)
1573{
1574 struct mgcp_config *cfg;
1575
1576 cfg = talloc_zero(NULL, struct mgcp_config);
1577 if (!cfg) {
1578 LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n");
1579 return NULL;
1580 }
1581
Philipp Maier12943ea2018-01-17 15:40:25 +01001582 osmo_strlcpy(cfg->domain, "mgw", sizeof(cfg->domain));
1583
Philipp Maier87bd9be2017-08-22 16:35:41 +02001584 cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START;
1585 cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END;
1586 cfg->net_ports.last_port = cfg->net_ports.range_start;
1587
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001588 cfg->source_port = 2427;
1589 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
1590 cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
1591
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001592 cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
1593 cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
1594
1595 cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
1596
1597 /* default trunk handling */
1598 cfg->trunk.cfg = cfg;
1599 cfg->trunk.trunk_nr = 0;
1600 cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
1601 cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
1602 cfg->trunk.audio_payload = 126;
1603 cfg->trunk.audio_send_ptime = 1;
1604 cfg->trunk.audio_send_name = 1;
1605 cfg->trunk.omit_rtcp = 0;
1606 mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001607 alloc_mgcp_rate_counters(&cfg->trunk, cfg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001608
1609 INIT_LLIST_HEAD(&cfg->trunks);
1610
1611 return cfg;
1612}
1613
Philipp Maier87bd9be2017-08-22 16:35:41 +02001614/*! allocate configuration with default values.
1615 * (called once at startup by VTY)
1616 * \param[in] cfg mgcp configuration
1617 * \param[in] nr trunk number
1618 * \returns pointer to allocated trunk configuration */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001619struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
1620{
1621 struct mgcp_trunk_config *trunk;
1622
1623 trunk = talloc_zero(cfg, struct mgcp_trunk_config);
1624 if (!trunk) {
1625 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n");
1626 return NULL;
1627 }
1628
1629 trunk->cfg = cfg;
1630 trunk->trunk_type = MGCP_TRUNK_E1;
1631 trunk->trunk_nr = nr;
1632 trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
1633 trunk->audio_payload = 126;
1634 trunk->audio_send_ptime = 1;
1635 trunk->audio_send_name = 1;
Philipp Maierfcd06552017-11-10 17:32:22 +01001636 trunk->vty_number_endpoints = 33;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001637 trunk->omit_rtcp = 0;
1638 mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001639 alloc_mgcp_rate_counters(trunk, trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001640 llist_add_tail(&trunk->entry, &cfg->trunks);
Stefan Sperling1e174872018-10-25 18:36:10 +02001641
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001642 return trunk;
1643}
1644
Philipp Maier87bd9be2017-08-22 16:35:41 +02001645/*! get trunk configuration by trunk number (index).
1646 * \param[in] cfg mgcp configuration
1647 * \param[in] index trunk number
1648 * \returns pointer to trunk configuration, NULL on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001649struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
1650{
1651 struct mgcp_trunk_config *trunk;
1652
1653 llist_for_each_entry(trunk, &cfg->trunks, entry)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001654 if (trunk->trunk_nr == index)
1655 return trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001656
1657 return NULL;
1658}
1659
Philipp Maier87bd9be2017-08-22 16:35:41 +02001660/*! allocate endpoints and set default values.
1661 * (called once at startup by VTY)
1662 * \param[in] tcfg trunk configuration
1663 * \returns 0 on success, -1 on failure */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001664int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
1665{
1666 int i;
1667
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001668 tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001669 sizeof(struct mgcp_endpoint),
Philipp Maierfcd06552017-11-10 17:32:22 +01001670 tcfg->vty_number_endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001671 "endpoints");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001672 if (!tcfg->endpoints)
1673 return -1;
1674
Philipp Maierfcd06552017-11-10 17:32:22 +01001675 for (i = 0; i < tcfg->vty_number_endpoints; ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001676 INIT_LLIST_HEAD(&tcfg->endpoints[i].conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001677 tcfg->endpoints[i].cfg = tcfg->cfg;
1678 tcfg->endpoints[i].tcfg = tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001679
1680 /* NOTE: Currently all endpoints are of type RTP, this will
1681 * change when new variations are implemented */
1682 tcfg->endpoints[i].type = &ep_typeset.rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001683 }
1684
Philipp Maierfcd06552017-11-10 17:32:22 +01001685 tcfg->number_endpoints = tcfg->vty_number_endpoints;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001686 alloc_mgcp_rate_counters(tcfg, tcfg->cfg);
Stefan Sperling1e174872018-10-25 18:36:10 +02001687
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001688 return 0;
1689}
1690
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001691static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1692{
1693 return write(cfg->gw_fd.bfd.fd, buf, len);
1694}
1695
Philipp Maier87bd9be2017-08-22 16:35:41 +02001696/*! Reset all endpoints by sending RSIP message to self.
1697 * (called by VTY)
1698 * \param[in] endp trunk endpoint
1699 * \param[in] endpoint number
1700 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001701int mgcp_send_reset_all(struct mgcp_config *cfg)
1702{
Philipp Maier12943ea2018-01-17 15:40:25 +01001703 char buf[MGCP_ENDPOINT_MAXLEN + 128];
1704 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001705 int rc;
1706
Philipp Maier12943ea2018-01-17 15:40:25 +01001707 len = snprintf(buf, sizeof(buf),
1708 "RSIP 1 *@%s MGCP 1.0\r\n", cfg->domain);
1709 if (len < 0)
1710 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001711
Philipp Maier12943ea2018-01-17 15:40:25 +01001712 rc = send_agent(cfg, buf, len);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001713 if (rc <= 0)
1714 return -1;
1715
1716 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001717}
1718
Philipp Maier87bd9be2017-08-22 16:35:41 +02001719/*! Reset a single endpoint by sending RSIP message to self.
1720 * (called by VTY)
1721 * \param[in] endp trunk endpoint
1722 * \param[in] endpoint number
1723 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001724int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
1725{
Philipp Maier12943ea2018-01-17 15:40:25 +01001726 char buf[MGCP_ENDPOINT_MAXLEN + 128];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001727 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001728 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001729
1730 len = snprintf(buf, sizeof(buf),
Philipp Maier12943ea2018-01-17 15:40:25 +01001731 "RSIP 39 %x@%s MGCP 1.0\r\n", endpoint, endp->cfg->domain);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001732 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001733 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001734
Philipp Maier87bd9be2017-08-22 16:35:41 +02001735 rc = send_agent(endp->cfg, buf, len);
1736 if (rc <= 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001737 return -1;
1738
Philipp Maier87bd9be2017-08-22 16:35:41 +02001739 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001740}