blob: 35141f848e967a8ee1935b905f381009dd84f3e9 [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
Pau Espin Pedrolfa810e82019-05-06 18:54:10 +0200150 if (conn->type != MGCP_RTP_DEFAULT && !mgcp_conn_rtp_is_osmux(conn)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200151 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
152 "RTP-setup: Endpoint is not configured as RTP default, stopping here!\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200153 return 0;
154 }
155
Philipp Maier87bd9be2017-08-22 16:35:41 +0200156 if (conn->conn->mode == MGCP_CONN_LOOPBACK) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200157 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
158 "RTP-setup: Endpoint is in loopback mode, stopping here!\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200159 return 0;
160 }
161
162 /* Find the "sister" connection */
163 llist_for_each_entry(_conn, &endp->conns, entry) {
164 if (_conn->id != conn->conn->id) {
165 conn_src = &_conn->u.rtp;
166 break;
167 }
168 }
169
Philipp Maieracc10352018-07-19 18:07:57 +0200170 return cfg->setup_rtp_processing_cb(endp, conn_dst, conn_src);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200171}
172
Philipp Maier87bd9be2017-08-22 16:35:41 +0200173/* array of function pointers for handling various
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200174 * messages. In the future this might be binary sorted
Philipp Maier87bd9be2017-08-22 16:35:41 +0200175 * for performance reasons. */
176static const struct mgcp_request mgcp_requests[] = {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200177 MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint")
Oliver Smith622dd612019-01-30 14:14:45 +0100178 MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
179 MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
180 MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
181 MGCP_REQUEST("RQNT", handle_noti_req, "NotificationRequest")
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200182
Oliver Smith622dd612019-01-30 14:14:45 +0100183 /* SPEC extension */
184 MGCP_REQUEST("RSIP", handle_rsip, "ReSetInProgress")
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200185};
186
Philipp Maier87bd9be2017-08-22 16:35:41 +0200187/* Helper function to allocate some memory for responses and retransmissions */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200188static struct msgb *mgcp_msgb_alloc(void)
189{
190 struct msgb *msg;
191 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
192 if (!msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200193 LOGP(DLMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200194
195 return msg;
196}
197
Philipp Maier87bd9be2017-08-22 16:35:41 +0200198/* Helper function for do_retransmission() and create_resp() */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200199static struct msgb *do_retransmission(const struct mgcp_endpoint *endp)
200{
201 struct msgb *msg = mgcp_msgb_alloc();
202 if (!msg)
203 return NULL;
204
205 msg->l2h = msgb_put(msg, strlen(endp->last_response));
206 memcpy(msg->l2h, endp->last_response, msgb_l2len(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200207 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Retransmitted response");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200208 return msg;
209}
210
211static struct msgb *create_resp(struct mgcp_endpoint *endp, int code,
212 const char *txt, const char *msg,
213 const char *trans, const char *param,
214 const char *sdp)
215{
216 int len;
217 struct msgb *res;
218
219 res = mgcp_msgb_alloc();
220 if (!res)
221 return NULL;
222
Philipp Maier87bd9be2017-08-22 16:35:41 +0200223 len = snprintf((char *)res->data, 2048, "%d %s%s%s\r\n%s",
224 code, trans, txt, param ? param : "", sdp ? sdp : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200225 if (len < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200226 LOGPENDP(endp, DLMGCP, LOGL_ERROR, "Failed to sprintf MGCP response.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200227 msgb_free(res);
228 return NULL;
229 }
230
231 res->l2h = msgb_put(res, len);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200232 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "Generated response: code=%d\n", code);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200233 mgcp_disp_msg(res->l2h, msgb_l2len(res), "Generated response");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200234
235 /*
236 * Remember the last transmission per endpoint.
237 */
238 if (endp) {
239 struct mgcp_trunk_config *tcfg = endp->tcfg;
240 talloc_free(endp->last_response);
241 talloc_free(endp->last_trans);
242 endp->last_trans = talloc_strdup(tcfg->endpoints, trans);
243 endp->last_response = talloc_strndup(tcfg->endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200244 (const char *)res->l2h,
245 msgb_l2len(res));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200246 }
247
248 return res;
249}
250
251static struct msgb *create_ok_resp_with_param(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200252 int code, const char *msg,
253 const char *trans,
254 const char *param)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200255{
256 return create_resp(endp, code, " OK", msg, trans, param, NULL);
257}
258
259static struct msgb *create_ok_response(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200260 int code, const char *msg,
261 const char *trans)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200262{
263 return create_ok_resp_with_param(endp, code, msg, trans, NULL);
264}
265
266static struct msgb *create_err_response(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200267 int code, const char *msg,
268 const char *trans)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200269{
270 return create_resp(endp, code, " FAIL", msg, trans, NULL, NULL);
271}
272
Philipp Maier55295f72018-01-15 14:00:28 +0100273/* Add MGCP parameters to a message buffer */
274static int add_params(struct msgb *msg, const struct mgcp_endpoint *endp,
275 const struct mgcp_conn_rtp *conn)
276{
277 int rc;
278
Philipp Maier7f0966c2018-01-17 18:18:12 +0100279 /* NOTE: Only in the virtual trunk we allow dynamic endpoint names */
Philipp Maier207ab512018-02-02 14:19:26 +0100280 if (endp->wildcarded_req
Philipp Maier7f0966c2018-01-17 18:18:12 +0100281 && endp->tcfg->trunk_type == MGCP_TRUNK_VIRTUAL) {
Philipp Maierdd4ede32018-02-01 17:52:52 +0100282 rc = msgb_printf(msg, "Z: %s%x@%s\r\n",
Philipp Maier7f0966c2018-01-17 18:18:12 +0100283 MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK,
284 ENDPOINT_NUMBER(endp), endp->cfg->domain);
Philipp Maier55295f72018-01-15 14:00:28 +0100285 if (rc < 0)
286 return -EINVAL;
287 }
288
Philipp Maierc3cfae22018-01-22 12:03:03 +0100289 rc = msgb_printf(msg, "I: %s\r\n", conn->conn->id);
Philipp Maier55295f72018-01-15 14:00:28 +0100290 if (rc < 0)
291 return -EINVAL;
292
293 return 0;
294}
295
Philipp Maier87bd9be2017-08-22 16:35:41 +0200296/* Format MGCP response string (with SDP attached) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200297static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200298 struct mgcp_conn_rtp *conn,
299 const char *msg,
Philipp Maier55295f72018-01-15 14:00:28 +0100300 const char *trans_id,
301 bool add_conn_params)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200302{
303 const char *addr = endp->cfg->local_ip;
Philipp Maier8970c492017-10-11 13:33:42 +0200304 struct msgb *sdp;
305 int rc;
306 struct msgb *result;
Philipp Maier1cb1e382017-11-02 17:16:04 +0100307 char local_ip_addr[INET_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200308
Philipp Maier8970c492017-10-11 13:33:42 +0200309 sdp = msgb_alloc_headroom(4096, 128, "sdp record");
310 if (!sdp)
311 return NULL;
312
Philipp Maier1cb1e382017-11-02 17:16:04 +0100313 if (!addr) {
314 mgcp_get_local_addr(local_ip_addr, conn);
315 addr = local_ip_addr;
316 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200317
Philipp Maier55295f72018-01-15 14:00:28 +0100318 /* Attach optional connection parameters */
319 if (add_conn_params) {
320 rc = add_params(sdp, endp, conn);
321 if (rc < 0)
322 goto error;
323 }
324
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100325 /* Attach optional OSMUX parameters */
326 if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) {
Pau Espin Pedrol5e8d7992019-04-24 19:56:43 +0200327 rc = msgb_printf(sdp, "X-Osmux: %u\r\n", conn->osmux.cid);
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100328 if (rc < 0)
329 goto error;
Pau Espin Pedrol5e8d7992019-04-24 19:56:43 +0200330 conn->osmux.state = OSMUX_STATE_ACTIVATING;
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100331 }
332
333 /* Attach line break to separate the parameters from the SDP block */
Philipp Maierc3cfae22018-01-22 12:03:03 +0100334 rc = msgb_printf(sdp, "\r\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200335
Philipp Maier8970c492017-10-11 13:33:42 +0200336 rc = mgcp_write_response_sdp(endp, conn, sdp, addr);
337 if (rc < 0)
338 goto error;
339 result = create_resp(endp, 200, " OK", msg, trans_id, NULL, (char*) sdp->data);
340 msgb_free(sdp);
341 return result;
342error:
343 msgb_free(sdp);
344 return NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200345}
346
Philipp Maier87bd9be2017-08-22 16:35:41 +0200347/* Send out dummy packet to keep the connection open, if the connection is an
348 * osmux connection, send the dummy packet via OSMUX */
349static void send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200350{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200351 if (conn->osmux.state != OSMUX_STATE_DISABLED)
352 osmux_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200353 else
Philipp Maier87bd9be2017-08-22 16:35:41 +0200354 mgcp_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200355}
356
Philipp Maier87bd9be2017-08-22 16:35:41 +0200357/* handle incoming messages:
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200358 * - this can be a command (four letters, space, transaction id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200359 * - or a response (three numbers, space, transaction id) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200360struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
361{
362 struct mgcp_parse_data pdata;
Harald Weltee35eeae2017-12-28 13:47:37 +0100363 int rc, i, code, handled = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200364 struct msgb *resp = NULL;
365 char *data;
366
367 if (msgb_l2len(msg) < 4) {
368 LOGP(DLMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len);
369 return NULL;
370 }
371
372 if (mgcp_msg_terminate_nul(msg))
373 return NULL;
374
Philipp Maier87bd9be2017-08-22 16:35:41 +0200375 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Received message");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200376
Philipp Maier87bd9be2017-08-22 16:35:41 +0200377 /* attempt to treat it as a response */
378 if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200379 LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code);
380 return NULL;
381 }
382
383 msg->l3h = &msg->l2h[4];
384
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200385 /*
386 * Check for a duplicate message and respond.
387 */
388 memset(&pdata, 0, sizeof(pdata));
389 pdata.cfg = cfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200390 data = mgcp_strline((char *)msg->l3h, &pdata.save);
Harald Weltee35eeae2017-12-28 13:47:37 +0100391 rc = mgcp_parse_header(&pdata, data);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200392 if (pdata.endp && pdata.trans
Philipp Maier87bd9be2017-08-22 16:35:41 +0200393 && pdata.endp->last_trans
394 && strcmp(pdata.endp->last_trans, pdata.trans) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200395 return do_retransmission(pdata.endp);
396 }
397
Harald Welteabbb6b92017-12-28 13:13:50 +0100398 /* check for general parser failure */
Harald Weltee35eeae2017-12-28 13:47:37 +0100399 if (rc < 0) {
Harald Welteabbb6b92017-12-28 13:13:50 +0100400 LOGP(DLMGCP, LOGL_NOTICE, "%s: failed to find the endpoint\n", msg->l2h);
Harald Weltee35eeae2017-12-28 13:47:37 +0100401 return create_err_response(NULL, -rc, (const char *) msg->l2h, pdata.trans);
Harald Welteabbb6b92017-12-28 13:13:50 +0100402 }
403
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200404 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200405 if (strncmp
406 (mgcp_requests[i].name, (const char *)&msg->l2h[0],
407 4) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200408 handled = 1;
409 resp = mgcp_requests[i].handle_request(&pdata);
410 break;
411 }
412 }
413
414 if (!handled)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200415 LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n",
416 &msg->l2h[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200417
418 return resp;
419}
420
Philipp Maier87bd9be2017-08-22 16:35:41 +0200421/* AUEP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200422static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p)
423{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200424 LOGPENDP(p->endp, DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n");
Harald Welteabbb6b92017-12-28 13:13:50 +0100425 return create_ok_response(p->endp, 200, "AUEP", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200426}
427
Harald Welte1d1b98f2017-12-25 10:03:40 +0100428/* Try to find a free port by attempting to bind on it. Also handle the
Philipp Maier87bd9be2017-08-22 16:35:41 +0200429 * counter that points on the next free port. Since we have a pointer
Philipp Maierb38fb892018-05-22 13:52:21 +0200430 * to the next free port, binding should in work on the first attempt in
Pau Espin Pedrolfc806732019-04-23 00:18:43 +0200431 * general. In case of failure the next port is tried until the whole port
432 * range is tried once. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200433static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200434{
435 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200436 struct mgcp_rtp_end *end;
437 struct mgcp_port_range *range;
Philipp Maierb38fb892018-05-22 13:52:21 +0200438 unsigned int tries;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200439
Philipp Maier87bd9be2017-08-22 16:35:41 +0200440 OSMO_ASSERT(conn);
441 end = &conn->end;
442 OSMO_ASSERT(end);
443
444 range = &endp->cfg->net_ports;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200445
446 /* attempt to find a port */
Philipp Maierb38fb892018-05-22 13:52:21 +0200447 tries = (range->range_end - range->range_start) / 2;
448 for (i = 0; i < tries; ++i) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200449 int rc;
450
451 if (range->last_port >= range->range_end)
452 range->last_port = range->range_start;
453
Philipp Maier87bd9be2017-08-22 16:35:41 +0200454 rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200455
456 range->last_port += 2;
457 if (rc == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200458 return 0;
459 }
460
461 }
462
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200463 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
464 "Allocating a RTP/RTCP port failed %u times.\n",
465 tries);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200466 return -1;
467}
468
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200469/*! Helper function for check_local_cx_options() to get a pointer of the next
470 * lco option identifier
471 * \param[in] lco string
472 * \returns pointer to the beginning of the LCO identifier, NULL on failure */
473char *get_lco_identifier(const char *options)
474{
475 char *ptr;
476 unsigned int count = 0;
477
478 /* Jump to the end of the lco identifier */
479 ptr = strstr(options, ":");
480 if (!ptr)
481 return NULL;
482
483 /* Walk backwards until the pointer points to the beginning of the
484 * lco identifier. We know that we stand at the beginning when we
485 * are either at the beginning of the memory or see a space or
486 * comma. (this is tolerant, it will accept a:10, b:11 as well as
487 * a:10,b:11) */
488 while (1) {
489 /* Endless loop protection */
490 if (count > 10000)
491 return NULL;
492 else if (ptr < options || *ptr == ' ' || *ptr == ',') {
493 ptr++;
494 break;
495 }
496 ptr--;
497 count++;
498 }
499
500 /* Check if we got any result */
501 if (*ptr == ':')
502 return NULL;
503
504 return ptr;
505}
506
507/*! Check the LCO option. This function checks for multiple appearence of LCO
508 * options, which is illegal
509 * \param[in] ctx talloc context
510 * \param[in] lco string
511 * \returns 0 on success, -1 on failure */
512int check_local_cx_options(void *ctx, const char *options)
513{
514 int i;
515 char *options_copy;
516 char *lco_identifier;
517 char *lco_identifier_end;
518 char *next_lco_identifier;
519
520 char **lco_seen;
521 unsigned int lco_seen_n = 0;
522
523 if (!options)
524 return -1;
525
526 lco_seen =
527 (char **)talloc_zero_size(ctx, strlen(options) * sizeof(char *));
528 options_copy = talloc_strdup(ctx, options);
529 lco_identifier = options_copy;
530
531 do {
532 /* Move the lco_identifier pointer to the beginning of the
533 * current lco option identifier */
534 lco_identifier = get_lco_identifier(lco_identifier);
535 if (!lco_identifier)
536 goto error;
537
538 /* Look ahead to the next LCO option early, since we
539 * will parse destructively */
540 next_lco_identifier = strstr(lco_identifier + 1, ",");
541
542 /* Pinch off the end of the lco field identifier name
543 * and see if we still got something, also check if
544 * there is some value after the colon. */
545 lco_identifier_end = strstr(lco_identifier, ":");
546 if (!lco_identifier_end)
547 goto error;
548 if (*(lco_identifier_end + 1) == ' '
549 || *(lco_identifier_end + 1) == ','
550 || *(lco_identifier_end + 1) == '\0')
551 goto error;
552 *lco_identifier_end = '\0';
553 if (strlen(lco_identifier) == 0)
554 goto error;
555
556 /* Check if we have already seen the current field identifier
557 * before. If yes, we must bail, an LCO must only appear once
558 * in the LCO string */
559 for (i = 0; i < lco_seen_n; i++) {
560 if (strcmp(lco_seen[i], lco_identifier) == 0)
561 goto error;
562 }
563 lco_seen[lco_seen_n] = lco_identifier;
564 lco_seen_n++;
565
566 /* The first identifier must always be found at the beginnning
567 * of the LCO string */
568 if (lco_seen[0] != options_copy)
569 goto error;
570
571 /* Go to the next lco option */
572 lco_identifier = next_lco_identifier;
573 } while (lco_identifier);
574
575 talloc_free(lco_seen);
576 talloc_free(options_copy);
577 return 0;
578error:
579 talloc_free(lco_seen);
580 talloc_free(options_copy);
581 return -1;
582}
583
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200584/* Set the LCO from a string (see RFC 3435).
Harald Welte1d1b98f2017-12-25 10:03:40 +0100585 * The string is stored in the 'string' field. A NULL string is handled exactly
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200586 * like an empty string, the 'string' field is never NULL after this function
587 * has been called. */
Philipp Maiera390d0b2018-01-31 17:30:19 +0100588static int set_local_cx_options(void *ctx, struct mgcp_lco *lco,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200589 const char *options)
590{
591 char *p_opt, *a_opt;
Philipp Maier8dbc9ed2018-10-26 14:50:25 +0200592 char codec[17];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200593
Philipp Maier604410c2018-06-06 10:02:16 +0200594 if (!options)
595 return 0;
596 if (strlen(options) == 0)
597 return 0;
598
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200599 /* Make sure the encoding of the LCO is consistant before we proceed */
600 if (check_local_cx_options(ctx, options) != 0) {
601 LOGP(DLMGCP, LOGL_ERROR,
602 "local CX options: Internal inconsistency in Local Connection Options!\n");
603 return 524;
604 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200605
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200606 talloc_free(lco->string);
607 lco->string = talloc_strdup(ctx, options);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200608
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200609 p_opt = strstr(lco->string, "p:");
610 if (p_opt && sscanf(p_opt, "p:%d-%d",
611 &lco->pkt_period_min, &lco->pkt_period_max) == 1)
612 lco->pkt_period_max = lco->pkt_period_min;
613
Philipp Maierbc0346e2018-06-07 09:52:16 +0200614 /* FIXME: LCO also supports the negotiation of more then one codec.
615 * (e.g. a:PCMU;G726-32) But this implementation only supports a single
616 * codec only. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200617 a_opt = strstr(lco->string, "a:");
Philipp Maier8dbc9ed2018-10-26 14:50:25 +0200618 if (a_opt && sscanf(a_opt, "a:%16[^,]", codec) == 1) {
Philipp Maier604410c2018-06-06 10:02:16 +0200619 talloc_free(lco->codec);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200620 lco->codec = talloc_strdup(ctx, codec);
Philipp Maier604410c2018-06-06 10:02:16 +0200621 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200622
623 LOGP(DLMGCP, LOGL_DEBUG,
624 "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n",
625 lco->pkt_period_max, lco->codec);
Philipp Maiera390d0b2018-01-31 17:30:19 +0100626
627 /* Check if the packetization fits the 20ms raster */
628 if (lco->pkt_period_min % 20 && lco->pkt_period_max % 20) {
629 LOGP(DLMGCP, LOGL_ERROR,
630 "local CX options: packetization interval is not a multiple of 20ms!\n");
631 return 535;
632 }
633
634 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200635}
636
637void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
638 struct mgcp_rtp_end *rtp)
639{
640 struct mgcp_trunk_config *tcfg = endp->tcfg;
641
642 int patch_ssrc = expect_ssrc_change && tcfg->force_constant_ssrc;
643
644 rtp->force_aligned_timing = tcfg->force_aligned_timing;
645 rtp->force_constant_ssrc = patch_ssrc ? 1 : 0;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100646 rtp->rfc5993_hr_convert = tcfg->rfc5993_hr_convert;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200647
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200648 LOGPENDP(endp, DLMGCP, LOGL_DEBUG,
649 "Configuring RTP endpoint: local port %d%s%s\n",
650 ntohs(rtp->rtp_port),
651 rtp->force_aligned_timing ? ", force constant timing" : "",
652 rtp->force_constant_ssrc ? ", force constant ssrc" : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200653}
654
655uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
656 struct mgcp_rtp_end *rtp)
657{
658 int f = 0;
659
660 /* Get the number of frames per channel and packet */
661 if (rtp->frames_per_packet)
662 f = rtp->frames_per_packet;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200663 else if (rtp->packet_duration_ms && rtp->codec->frame_duration_num) {
664 int den = 1000 * rtp->codec->frame_duration_num;
665 f = (rtp->packet_duration_ms * rtp->codec->frame_duration_den +
Philipp Maier87bd9be2017-08-22 16:35:41 +0200666 den / 2)
667 / den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200668 }
669
Philipp Maierbc0346e2018-06-07 09:52:16 +0200670 return rtp->codec->rate * f * rtp->codec->frame_duration_num /
671 rtp->codec->frame_duration_den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200672}
673
Pau Espin Pedrol9fb8ddf2019-05-08 15:35:36 +0200674/*! Initializes osmux socket if not yet initialized. Parses Osmux CID from MGCP line.
675 * \param[in] endp Endpoint willing to initialize osmux
676 * \param[in] line Line X-Osmux from MGCP header msg to parse
677 * \returns OSMUX CID, -1 for wildcard, -2 on parse error, -3 on osmux initalize error
678 */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200679static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line)
680{
681 if (!endp->cfg->osmux_init) {
682 if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200683 LOGPENDP(endp, DLMGCP, LOGL_ERROR, "Cannot init OSMUX\n");
Pau Espin Pedrol9fb8ddf2019-05-08 15:35:36 +0200684 return -3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200685 }
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200686 LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200687 }
688
689 return mgcp_parse_osmux_cid(line);
690}
691
Philipp Maierbc0346e2018-06-07 09:52:16 +0200692/* Process codec information contained in CRCX/MDCX */
693static int handle_codec_info(struct mgcp_conn_rtp *conn,
694 struct mgcp_parse_data *p, int have_sdp, bool crcx)
695{
696 struct mgcp_endpoint *endp = p->endp;
697 int rc;
698 char *cmd;
699
700 if (crcx)
701 cmd = "CRCX";
702 else
703 cmd = "MDCX";
704
705 /* Collect codec information */
706 if (have_sdp) {
707 /* If we have SDP, we ignore the local connection options and
708 * use only the SDP information. */
709 mgcp_codec_reset_all(conn);
710 rc = mgcp_parse_sdp_data(endp, conn, p);
711 if (rc != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200712 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
713 "%s: sdp not parseable\n", cmd);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200714
715 /* See also RFC 3661: Protocol error */
716 return 510;
717 }
718 } else if (endp->local_options.codec) {
719 /* When no SDP is available, we use the codec information from
720 * the local connection options (if present) */
721 mgcp_codec_reset_all(conn);
Philipp Maier228e5912019-03-05 13:56:59 +0100722 rc = mgcp_codec_add(conn, PTYPE_UNDEFINED, endp->local_options.codec, NULL);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200723 if (rc != 0)
724 goto error;
725 }
726
727 /* Make sure we always set a sane default codec */
728 if (conn->end.codecs_assigned == 0) {
729 /* When SDP and/or LCO did not supply any codec information,
730 * than it makes sense to pick a sane default: (payload-type 0,
731 * PCMU), see also: OS#2658 */
732 mgcp_codec_reset_all(conn);
Philipp Maier228e5912019-03-05 13:56:59 +0100733 rc = mgcp_codec_add(conn, 0, NULL, NULL);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200734 if (rc != 0)
735 goto error;
736 }
737
738 /* Make codec decision */
739 if (mgcp_codec_decide(conn) != 0)
740 goto error;
741
742 return 0;
743
744error:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200745 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
746 "%s: codec negotiation failure\n", cmd);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200747
748 /* See also RFC 3661: Codec negotiation failure */
749 return 534;
750}
751
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200752static bool parse_x_osmo_ign(struct mgcp_endpoint *endp, char *line)
753{
754 char *saveptr = NULL;
755
756 if (strncmp(line, MGCP_X_OSMO_IGN_HEADER, strlen(MGCP_X_OSMO_IGN_HEADER)))
757 return false;
758 line += strlen(MGCP_X_OSMO_IGN_HEADER);
759
760 while (1) {
761 char *token = strtok_r(line, " ", &saveptr);
762 line = NULL;
763 if (!token)
764 break;
765
766 if (!strcmp(token, "C"))
767 endp->x_osmo_ign |= MGCP_X_OSMO_IGN_CALLID;
768 else
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200769 LOGPENDP(endp, DLMGCP, LOGL_ERROR, "received unknown X-Osmo-IGN item '%s'\n", token);
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200770 }
771
772 return true;
773}
774
Philipp Maier87bd9be2017-08-22 16:35:41 +0200775/* CRCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200776static struct msgb *handle_create_con(struct mgcp_parse_data *p)
777{
Stefan Sperling1e174872018-10-25 18:36:10 +0200778 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200779 struct mgcp_endpoint *endp = p->endp;
Stefan Sperling9270e912018-10-29 14:10:00 +0100780 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_crcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200781 int error_code = 400;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200782 const char *local_options = NULL;
783 const char *callid = NULL;
784 const char *mode = NULL;
785 char *line;
Pau Espin Pedrol2b896172019-04-24 13:47:23 +0200786 int have_sdp = 0, osmux_cid = -2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200787 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100788 struct mgcp_conn *_conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200789 char conn_name[512];
Philipp Maiera390d0b2018-01-31 17:30:19 +0100790 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200791
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200792 LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200793
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200794 /* parse CallID C: and LocalParameters L: */
795 for_each_line(line, p->save) {
796 if (!mgcp_check_param(endp, line))
797 continue;
798
799 switch (line[0]) {
800 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200801 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200802 break;
803 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200804 callid = (const char *)line + 3;
805 break;
806 case 'I':
Philipp Maierffd75e42017-11-22 11:44:50 +0100807 /* It is illegal to send a connection identifier
808 * together with a CRCX, the MGW will assign the
809 * connection identifier by itself on CRCX */
Stefan Sperling9270e912018-10-29 14:10:00 +0100810 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_BAD_ACTION]);
Philipp Maierffd75e42017-11-22 11:44:50 +0100811 return create_err_response(NULL, 523, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200812 break;
813 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200814 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200815 break;
816 case 'X':
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200817 if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
818 /* If osmux is disabled, just skip setting it up */
819 if (!p->endp->cfg->osmux)
820 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200821 osmux_cid = mgcp_osmux_setup(endp, line);
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200822 break;
823 }
824
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200825 if (parse_x_osmo_ign(endp, line))
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200826 break;
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200827
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200828 /* Ignore unknown X-headers */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200829 break;
830 case '\0':
831 have_sdp = 1;
832 goto mgcp_header_done;
833 default:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200834 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
835 "CRCX: unhandled option: '%c'/%d\n", *line, *line);
Stefan Sperling9270e912018-10-29 14:10:00 +0100836 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +0100837 return create_err_response(NULL, 539, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200838 break;
839 }
840 }
841
842mgcp_header_done:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200843 /* Check parameters */
844 if (!callid) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200845 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
846 "CRCX: insufficient parameters, missing callid\n");
Stefan Sperling9270e912018-10-29 14:10:00 +0100847 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_MISSING_CALLID]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100848 return create_err_response(endp, 516, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200849 }
850
Philipp Maier87bd9be2017-08-22 16:35:41 +0200851 if (!mode) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200852 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
853 "CRCX: insufficient parameters, missing mode\n");
Stefan Sperling9270e912018-10-29 14:10:00 +0100854 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_MODE]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100855 return create_err_response(endp, 517, "CRCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200856 }
857
Philipp Maier87bd9be2017-08-22 16:35:41 +0200858 /* Check if we are able to accept the creation of another connection */
859 if (llist_count(&endp->conns) >= endp->type->max_conns) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200860 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
861 "CRCX: endpoint full, max. %i connections allowed!\n",
862 endp->type->max_conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200863 if (tcfg->force_realloc) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200864 /* There is no more room for a connection, make some
865 * room by blindly tossing the oldest of the two two
866 * connections */
867 mgcp_conn_free_oldest(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200868 } else {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200869 /* There is no more room for a connection, leave
870 * everything as it is and return with an error */
Stefan Sperling9270e912018-10-29 14:10:00 +0100871 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_LIMIT_EXCEEDED]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100872 return create_err_response(endp, 540, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200873 }
874 }
875
Philipp Maier87bd9be2017-08-22 16:35:41 +0200876 /* Check if this endpoint already serves a call, if so, check if the
877 * callids match up so that we are sure that this is our call */
878 if (endp->callid && mgcp_verify_call_id(endp, callid)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200879 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
880 "CRCX: already seized by other call (%s)\n",
881 endp->callid);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200882 if (tcfg->force_realloc)
883 /* This is not our call, toss everything by releasing
884 * the entire endpoint. (rude!) */
Philipp Maier1355d7e2018-02-01 14:30:06 +0100885 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200886 else {
887 /* This is not our call, leave everything as it is and
888 * return with an error. */
Stefan Sperling9270e912018-10-29 14:10:00 +0100889 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_UNKNOWN_CALLID]);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200890 return create_err_response(endp, 400, "CRCX", p->trans);
891 }
892 }
893
894 /* Set the callid, creation of another connection will only be possible
Harald Welte1d1b98f2017-12-25 10:03:40 +0100895 * when the callid matches up. (Connections are distinguished by their
Philipp Maier87bd9be2017-08-22 16:35:41 +0200896 * connection ids) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200897 endp->callid = talloc_strdup(tcfg->endpoints, callid);
898
Philipp Maierffd75e42017-11-22 11:44:50 +0100899 snprintf(conn_name, sizeof(conn_name), "%s", callid);
900 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP, conn_name);
901 if (!_conn) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200902 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
903 "CRCX: unable to allocate RTP connection\n");
Stefan Sperling9270e912018-10-29 14:10:00 +0100904 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_ALLOC_CONN]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200905 goto error2;
906
Philipp Maier87bd9be2017-08-22 16:35:41 +0200907 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100908 conn = mgcp_conn_get_rtp(endp, _conn->id);
909 OSMO_ASSERT(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200910
911 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
912 error_code = 517;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100913 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_MODE]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200914 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200915 }
916
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200917 /* Annotate Osmux circuit ID and set it to negotiating state until this
Philipp Maier87bd9be2017-08-22 16:35:41 +0200918 * is fully set up from the dummy load. */
919 conn->osmux.state = OSMUX_STATE_DISABLED;
Pau Espin Pedrol2b896172019-04-24 13:47:23 +0200920 if (osmux_cid >= -1) { /* -1 is wilcard, alloc next avail CID */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200921 conn->osmux.state = OSMUX_STATE_NEGOTIATING;
Pau Espin Pedrol2b896172019-04-24 13:47:23 +0200922 if (conn_osmux_allocate_cid(conn, osmux_cid) == -1) {
923 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_OSMUX]);
924 goto error2;
925 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200926 } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200927 LOGPCONN(_conn, DLMGCP, LOGL_ERROR,
928 "CRCX: osmux only and no osmux offered\n");
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100929 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_OSMUX]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200930 goto error2;
931 }
932
Philipp Maierbc0346e2018-06-07 09:52:16 +0200933 /* Set local connection options, if present */
934 if (local_options) {
935 rc = set_local_cx_options(endp->tcfg->endpoints,
936 &endp->local_options, local_options);
937 if (rc != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200938 LOGPCONN(_conn, DLMGCP, LOGL_ERROR,
939 "CRCX: inavlid local connection options!\n");
Philipp Maierbc0346e2018-06-07 09:52:16 +0200940 error_code = rc;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100941 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS]);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200942 goto error2;
943 }
944 }
945
946 /* Handle codec information and decide for a suitable codec */
947 rc = handle_codec_info(conn, p, have_sdp, true);
948 mgcp_codec_summary(conn);
949 if (rc) {
950 error_code = rc;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100951 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_CODEC_NEGOTIATION]);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200952 goto error2;
953 }
954
Philipp Maier87bd9be2017-08-22 16:35:41 +0200955 conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints,
956 tcfg->audio_fmtp_extra);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200957
Philipp Maier87bd9be2017-08-22 16:35:41 +0200958 if (p->cfg->force_ptime) {
959 conn->end.packet_duration_ms = p->cfg->force_ptime;
960 conn->end.force_output_ptime = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200961 }
962
Philipp Maier1cb1e382017-11-02 17:16:04 +0100963 mgcp_rtp_end_config(endp, 0, &conn->end);
964
Philipp Maierc3cc6542018-02-02 12:58:42 +0100965 /* check connection mode setting */
966 if (conn->conn->mode != MGCP_CONN_LOOPBACK
967 && conn->conn->mode != MGCP_CONN_RECV_ONLY
968 && conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200969 LOGPCONN(_conn, DLMGCP, LOGL_ERROR,
970 "CRCX: selected connection mode type requires an opposite end!\n");
Philipp Maierc3cc6542018-02-02 12:58:42 +0100971 error_code = 527;
Stefan Sperling9270e912018-10-29 14:10:00 +0100972 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC]);
Philipp Maierc3cc6542018-02-02 12:58:42 +0100973 goto error2;
974 }
975
Philipp Maier1cb1e382017-11-02 17:16:04 +0100976 if (allocate_port(endp, conn) != 0) {
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100977 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_BIND_PORT]);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100978 goto error2;
979 }
980
Philipp Maier87bd9be2017-08-22 16:35:41 +0200981 if (setup_rtp_processing(endp, conn) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200982 LOGPCONN(_conn, DLMGCP, LOGL_ERROR,
983 "CRCX: could not start RTP processing!\n");
Stefan Sperling9270e912018-10-29 14:10:00 +0100984 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_START_RTP]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200985 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200986 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200987
988 /* policy CB */
989 if (p->cfg->policy_cb) {
990 int rc;
991 rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200992 MGCP_ENDP_CRCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200993 switch (rc) {
994 case MGCP_POLICY_REJECT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200995 LOGPCONN(_conn, DLMGCP, LOGL_NOTICE,
996 "CRCX: CRCX rejected by policy\n");
Philipp Maier1355d7e2018-02-01 14:30:06 +0100997 mgcp_endp_release(endp);
Stefan Sperling9270e912018-10-29 14:10:00 +0100998 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200999 return create_err_response(endp, 400, "CRCX", p->trans);
1000 break;
1001 case MGCP_POLICY_DEFER:
1002 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001003 return NULL;
1004 break;
1005 case MGCP_POLICY_CONT:
1006 /* just continue */
1007 break;
1008 }
1009 }
1010
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001011 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG,
1012 "CRCX: Creating connection: port: %u\n", conn->end.local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001013 if (p->cfg->change_cb)
1014 p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX);
1015
Philipp Maiere726d4f2017-11-01 10:41:34 +01001016 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
1017 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001018 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
Philipp Maiere726d4f2017-11-01 10:41:34 +01001019 && tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001020 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001021
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001022 LOGPCONN(_conn, DLMGCP, LOGL_NOTICE,
1023 "CRCX: connection successfully created\n");
Stefan Sperling9270e912018-10-29 14:10:00 +01001024 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_SUCCESS]);
Philipp Maier55295f72018-01-15 14:00:28 +01001025 return create_response_with_sdp(endp, conn, "CRCX", p->trans, true);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001026error2:
Philipp Maier1355d7e2018-02-01 14:30:06 +01001027 mgcp_endp_release(endp);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001028 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1029 "CRCX: unable to create connection\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001030 return create_err_response(endp, error_code, "CRCX", p->trans);
1031}
1032
Philipp Maierbc0346e2018-06-07 09:52:16 +02001033
1034
1035
1036
Philipp Maier87bd9be2017-08-22 16:35:41 +02001037/* MDCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001038static struct msgb *handle_modify_con(struct mgcp_parse_data *p)
1039{
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001040 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001041 struct mgcp_endpoint *endp = p->endp;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001042 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_mdcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001043 int error_code = 500;
1044 int silent = 0;
1045 int have_sdp = 0;
1046 char *line;
1047 const char *local_options = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001048 const char *mode = NULL;
1049 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier01d24a32017-11-21 17:26:09 +01001050 const char *conn_id = NULL;
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001051 int osmux_cid = -2;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001052 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001053
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001054 LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001055
Philipp Maier5656fbf2018-02-02 14:41:58 +01001056 /* Prohibit wildcarded requests */
1057 if (endp->wildcarded_req) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001058 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1059 "MDCX: wildcarded endpoint names not supported.\n");
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001060 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_WILDCARD]);
Philipp Maier5656fbf2018-02-02 14:41:58 +01001061 return create_err_response(endp, 507, "MDCX", p->trans);
1062 }
1063
Philipp Maier87bd9be2017-08-22 16:35:41 +02001064 if (llist_count(&endp->conns) <= 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001065 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1066 "MDCX: endpoint is not holding a connection.\n");
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001067 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_CONN]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001068 return create_err_response(endp, 400, "MDCX", p->trans);
1069 }
1070
1071 for_each_line(line, p->save) {
1072 if (!mgcp_check_param(endp, line))
1073 continue;
1074
1075 switch (line[0]) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001076 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +01001077 if (mgcp_verify_call_id(endp, line + 3) != 0) {
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001078 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CALLID]);
Harald Weltee35eeae2017-12-28 13:47:37 +01001079 error_code = 516;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001080 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001081 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001082 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001083 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001084 conn_id = (const char *)line + 3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001085 if ((error_code = mgcp_verify_ci(endp, conn_id))) {
1086 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CONNID]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001087 goto error3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001088 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001089 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001090 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001091 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001092 break;
1093 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001094 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001095 break;
1096 case 'Z':
1097 silent = strcmp("noanswer", line + 3) == 0;
1098 break;
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001099 case 'X':
1100 if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
1101 /* If osmux is disabled, just skip setting it up */
1102 if (!p->endp->cfg->osmux)
1103 break;
1104 osmux_cid = mgcp_osmux_setup(endp, line);
1105 break;
1106 }
1107 /* Ignore unknown X-headers */
1108 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001109 case '\0':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001110 have_sdp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001111 goto mgcp_header_done;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001112 break;
1113 default:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001114 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1115 "MDCX: Unhandled MGCP option: '%c'/%d\n",
1116 line[0], line[0]);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001117 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001118 return create_err_response(NULL, 539, "MDCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001119 break;
1120 }
1121 }
1122
Philipp Maier87bd9be2017-08-22 16:35:41 +02001123mgcp_header_done:
Philipp Maier01d24a32017-11-21 17:26:09 +01001124 if (!conn_id) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001125 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1126 "MDCX: insufficient parameters, missing ci (connectionIdentifier)\n");
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001127 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_CONNID]);
Harald Weltee35eeae2017-12-28 13:47:37 +01001128 return create_err_response(endp, 515, "MDCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001129 }
1130
1131 conn = mgcp_conn_get_rtp(endp, conn_id);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001132 if (!conn) {
1133 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_CONN_NOT_FOUND]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001134 return create_err_response(endp, 400, "MDCX", p->trans);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001135 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001136
Oliver Smithe36b7752019-01-22 16:31:36 +01001137 mgcp_conn_watchdog_kick(conn->conn);
1138
Philipp Maier87bd9be2017-08-22 16:35:41 +02001139 if (mode) {
1140 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001141 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_MODE]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001142 error_code = 517;
1143 goto error3;
1144 }
1145 } else
Pau Espin Pedrol209eb9f2019-04-24 12:03:04 +02001146 conn->conn->mode = conn->conn->mode_orig;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001147
Philipp Maierbc0346e2018-06-07 09:52:16 +02001148 /* Set local connection options, if present */
1149 if (local_options) {
1150 rc = set_local_cx_options(endp->tcfg->endpoints,
1151 &endp->local_options, local_options);
1152 if (rc != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001153 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1154 "MDCX: invalid local connection options!\n");
Philipp Maierbc0346e2018-06-07 09:52:16 +02001155 error_code = rc;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001156 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS]);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001157 goto error3;
1158 }
1159 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001160
Philipp Maierbc0346e2018-06-07 09:52:16 +02001161 /* Handle codec information and decide for a suitable codec */
1162 rc = handle_codec_info(conn, p, have_sdp, false);
1163 mgcp_codec_summary(conn);
1164 if (rc) {
Philipp Maieraf07f662018-02-02 11:34:02 +01001165 error_code = rc;
1166 goto error3;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001167 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001168
Philipp Maierc3cc6542018-02-02 12:58:42 +01001169 /* check connection mode setting */
1170 if (conn->conn->mode != MGCP_CONN_LOOPBACK
1171 && conn->conn->mode != MGCP_CONN_RECV_ONLY
1172 && conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001173 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1174 "MDCX: selected connection mode type requires an opposite end!\n");
Philipp Maierc3cc6542018-02-02 12:58:42 +01001175 error_code = 527;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001176 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC]);
Philipp Maierc3cc6542018-02-02 12:58:42 +01001177 goto error3;
1178 }
1179
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001180 if (mgcp_conn_rtp_is_osmux(conn)) {
1181 OSMO_ASSERT(conn->osmux.cid_allocated);
1182 if (osmux_cid < -1) {
1183 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1184 "MDCX: Failed to parse Osmux CID!\n");
1185 goto error3;
1186 } else if (osmux_cid == -1) {
1187 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1188 "MDCX: wilcard in MDCX is not supported!\n");
1189 goto error3;
1190 } else if (osmux_cid != (int) conn->osmux.cid) {
1191 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1192 "MDCX: changing already allocated CID is not supported!\n");
1193 goto error3;
1194 }
1195 /* TODO: In the future (when we have recvCID!=sendCID), we need to
1196 tell Osmux code that osmux_cid is to be used as sendCID for
1197 that conn. */
1198 }
Philipp Maierbc0346e2018-06-07 09:52:16 +02001199
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001200 if (setup_rtp_processing(endp, conn) != 0) {
1201 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_START_RTP]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001202 goto error3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001203 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001204
Philipp Maier87bd9be2017-08-22 16:35:41 +02001205
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001206 /* policy CB */
1207 if (p->cfg->policy_cb) {
1208 int rc;
1209 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001210 MGCP_ENDP_MDCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001211 switch (rc) {
1212 case MGCP_POLICY_REJECT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001213 LOGPCONN(conn->conn, DLMGCP, LOGL_NOTICE,
1214 "MDCX: rejected by policy\n");
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001215 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001216 if (silent)
1217 goto out_silent;
1218 return create_err_response(endp, 400, "MDCX", p->trans);
1219 break;
1220 case MGCP_POLICY_DEFER:
1221 /* stop processing */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001222 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG,
1223 "MDCX: deferred by policy\n");
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001224 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_DEFERRED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001225 return NULL;
1226 break;
1227 case MGCP_POLICY_CONT:
1228 /* just continue */
1229 break;
1230 }
1231 }
1232
Philipp Maier87bd9be2017-08-22 16:35:41 +02001233 mgcp_rtp_end_config(endp, 1, &conn->end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001234
1235 /* modify */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001236 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG,
1237 "MDCX: modified conn:%s\n", mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001238 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001239 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1240 MGCP_ENDP_MDCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001241
Philipp Maiere726d4f2017-11-01 10:41:34 +01001242 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
1243 OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1244 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
1245 && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001246 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001247
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001248 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_SUCCESS]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001249 if (silent)
1250 goto out_silent;
1251
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001252 LOGPCONN(conn->conn, DLMGCP, LOGL_NOTICE,
1253 "MDCX: connection successfully modified\n");
Philipp Maier55295f72018-01-15 14:00:28 +01001254 return create_response_with_sdp(endp, conn, "MDCX", p->trans, false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001255error3:
1256 return create_err_response(endp, error_code, "MDCX", p->trans);
1257
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001258out_silent:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001259 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "MDCX: silent exit\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001260 return NULL;
1261}
1262
Philipp Maier87bd9be2017-08-22 16:35:41 +02001263/* DLCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001264static struct msgb *handle_delete_con(struct mgcp_parse_data *p)
1265{
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001266 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001267 struct mgcp_endpoint *endp = p->endp;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001268 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_dlcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001269 int error_code = 400;
1270 int silent = 0;
1271 char *line;
1272 char stats[1048];
Philipp Maier01d24a32017-11-21 17:26:09 +01001273 const char *conn_id = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001274 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001275
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001276 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1277 "DLCX: deleting connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001278
Philipp Maier5656fbf2018-02-02 14:41:58 +01001279 /* Prohibit wildcarded requests */
1280 if (endp->wildcarded_req) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001281 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1282 "DLCX: wildcarded endpoint names not supported.\n");
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001283 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_WILDCARD]);
Philipp Maier5656fbf2018-02-02 14:41:58 +01001284 return create_err_response(endp, 507, "DLCX", p->trans);
1285 }
1286
Philipp Maier87bd9be2017-08-22 16:35:41 +02001287 if (llist_count(&endp->conns) <= 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001288 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1289 "DLCX: endpoint is not holding a connection.\n");
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001290 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_NO_CONN]);
Harald Weltee35eeae2017-12-28 13:47:37 +01001291 return create_err_response(endp, 515, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001292 }
1293
1294 for_each_line(line, p->save) {
1295 if (!mgcp_check_param(endp, line))
1296 continue;
1297
1298 switch (line[0]) {
1299 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +01001300 if (mgcp_verify_call_id(endp, line + 3) != 0) {
1301 error_code = 516;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001302 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CALLID]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001303 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001304 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001305 break;
1306 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001307 conn_id = (const char *)line + 3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001308 if ((error_code = mgcp_verify_ci(endp, conn_id))) {
1309 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CONNID]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001310 goto error3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001311 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001312 break;
1313 case 'Z':
1314 silent = strcmp("noanswer", line + 3) == 0;
1315 break;
1316 default:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001317 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1318 "DLCX: Unhandled MGCP option: '%c'/%d\n",
1319 line[0], line[0]);
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001320 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001321 return create_err_response(NULL, 539, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001322 break;
1323 }
1324 }
1325
1326 /* policy CB */
1327 if (p->cfg->policy_cb) {
1328 int rc;
1329 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001330 MGCP_ENDP_DLCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001331 switch (rc) {
1332 case MGCP_POLICY_REJECT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001333 LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "DLCX: rejected by policy\n");
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001334 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001335 if (silent)
1336 goto out_silent;
1337 return create_err_response(endp, 400, "DLCX", p->trans);
1338 break;
1339 case MGCP_POLICY_DEFER:
1340 /* stop processing */
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001341 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_DEFERRED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001342 return NULL;
1343 break;
1344 case MGCP_POLICY_CONT:
1345 /* just continue */
1346 break;
1347 }
1348 }
1349
Philipp Maierf4c0e372017-10-11 16:06:45 +02001350 /* When no connection id is supplied, we will interpret this as a
1351 * wildcarded DLCX and drop all connections at once. (See also
1352 * RFC3435 Section F.7) */
Philipp Maier01d24a32017-11-21 17:26:09 +01001353 if (!conn_id) {
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001354 int num_conns = llist_count(&endp->conns);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001355 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1356 "DLCX: missing ci (connectionIdentifier), will remove all connections (%d total) at once\n",
1357 num_conns);
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001358
1359 if (num_conns > 0)
1360 rate_ctr_add(&rate_ctrs->ctr[MGCP_DLCX_SUCCESS], num_conns);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001361
Philipp Maier1355d7e2018-02-01 14:30:06 +01001362 mgcp_endp_release(endp);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001363
1364 /* Note: In this case we do not return any statistics,
1365 * as we assume that the client is not interested in
1366 * this case. */
1367 return create_ok_response(endp, 200, "DLCX", p->trans);
1368 }
1369
Philipp Maierf4c0e372017-10-11 16:06:45 +02001370 /* Find the connection */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001371 conn = mgcp_conn_get_rtp(endp, conn_id);
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001372 if (!conn) {
1373 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CONNID]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001374 goto error3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001375 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001376 /* save the statistics of the current connection */
1377 mgcp_format_stats(stats, sizeof(stats), conn->conn);
1378
1379 /* delete connection */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001380 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG, "DLCX: deleting conn:%s\n",
1381 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001382 mgcp_conn_free(endp, conn_id);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001383 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1384 "DLCX: connection successfully deleted\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001385
1386 /* When all connections are closed, the endpoint will be released
1387 * in order to be ready to be used by another call. */
1388 if (llist_count(&endp->conns) <= 0) {
Philipp Maier1355d7e2018-02-01 14:30:06 +01001389 mgcp_endp_release(endp);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001390 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "DLCX: endpoint released\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001391 }
1392
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001393 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001394 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1395 MGCP_ENDP_DLCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001396
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001397 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_SUCCESS]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001398 if (silent)
1399 goto out_silent;
1400 return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats);
1401
1402error3:
1403 return create_err_response(endp, error_code, "DLCX", p->trans);
1404
1405out_silent:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001406 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "DLCX: silent exit\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001407 return NULL;
1408}
1409
Philipp Maier87bd9be2017-08-22 16:35:41 +02001410/* RSIP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001411static struct msgb *handle_rsip(struct mgcp_parse_data *p)
1412{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001413 /* TODO: Also implement the resetting of a specific endpoint
1414 * to make mgcp_send_reset_ep() work. Currently this will call
1415 * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1
1416 * to make read_call_agent() reset all endpoints when called
1417 * next time. In order to selectively reset endpoints some
1418 * mechanism to distinguish which endpoint shall be resetted
1419 * is needed */
1420
1421 LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n");
1422
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001423 if (p->cfg->reset_cb)
1424 p->cfg->reset_cb(p->endp->tcfg);
1425 return NULL;
1426}
1427
1428static char extract_tone(const char *line)
1429{
1430 const char *str = strstr(line, "D/");
1431 if (!str)
1432 return CHAR_MAX;
1433
1434 return str[2];
1435}
1436
Philipp Maier87bd9be2017-08-22 16:35:41 +02001437/* This can request like DTMF detection and forward, fax detection... it
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001438 * can also request when the notification should be send and such. We don't
Philipp Maier87bd9be2017-08-22 16:35:41 +02001439 * do this right now. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001440static struct msgb *handle_noti_req(struct mgcp_parse_data *p)
1441{
1442 int res = 0;
1443 char *line;
1444 char tone = CHAR_MAX;
1445
Philipp Maier87bd9be2017-08-22 16:35:41 +02001446 LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n");
1447
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001448 for_each_line(line, p->save) {
1449 switch (line[0]) {
1450 case 'S':
1451 tone = extract_tone(line);
1452 break;
1453 }
1454 }
1455
1456 /* we didn't see a signal request with a tone */
1457 if (tone == CHAR_MAX)
1458 return create_ok_response(p->endp, 200, "RQNT", p->trans);
1459
1460 if (p->cfg->rqnt_cb)
1461 res = p->cfg->rqnt_cb(p->endp, tone);
1462
1463 return res == 0 ?
Philipp Maier87bd9be2017-08-22 16:35:41 +02001464 create_ok_response(p->endp, 200, "RQNT", p->trans) :
1465 create_err_response(p->endp, res, "RQNT", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001466}
1467
Philipp Maier87bd9be2017-08-22 16:35:41 +02001468/* Connection keepalive timer, will take care that dummy packets are send
Harald Welte1d1b98f2017-12-25 10:03:40 +01001469 * regularly, so that NAT connections stay open */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001470static void mgcp_keepalive_timer_cb(void *_tcfg)
1471{
1472 struct mgcp_trunk_config *tcfg = _tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001473 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001474 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001475
Philipp Maiere726d4f2017-11-01 10:41:34 +01001476 LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001477 tcfg->trunk_nr);
1478
Philipp Maiere726d4f2017-11-01 10:41:34 +01001479 /* Do not accept invalid configuration values
1480 * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and
1481 * values greater 0 */
1482 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1483
1484 /* The dummy packet functionality has been disabled, we will exit
1485 * immediately, no further timer is scheduled, which means we will no
1486 * longer send dummy packets even when we did before */
1487 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER)
1488 return;
1489
1490 /* In cases where only one dummy packet is sent, we do not need
1491 * the timer since the functions that handle the CRCX and MDCX are
1492 * triggering the sending of the dummy packet. So we behave like in
1493 * the MGCP_KEEPALIVE_NEVER case */
1494 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001495 return;
1496
Philipp Maier87bd9be2017-08-22 16:35:41 +02001497 /* Send walk over all endpoints and send out dummy packets through
1498 * every connection present on each endpoint */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001499 for (i = 1; i < tcfg->number_endpoints; ++i) {
1500 struct mgcp_endpoint *endp = &tcfg->endpoints[i];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001501 llist_for_each_entry(conn, &endp->conns, entry) {
1502 if (conn->mode == MGCP_CONN_RECV_ONLY)
1503 send_dummy(endp, &conn->u.rtp);
1504 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001505 }
1506
Philipp Maiere726d4f2017-11-01 10:41:34 +01001507 /* Schedule the keepalive timer for the next round */
1508 LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001509 tcfg->trunk_nr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001510 osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval,
1511 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001512}
1513
1514void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval)
1515{
1516 tcfg->keepalive_interval = interval;
1517 osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg);
1518
1519 if (interval <= 0)
1520 osmo_timer_del(&tcfg->keepalive_timer);
1521 else
1522 osmo_timer_schedule(&tcfg->keepalive_timer,
1523 tcfg->keepalive_interval, 0);
1524}
1525
Stefan Sperling1e174872018-10-25 18:36:10 +02001526static int free_rate_counter_group(struct rate_ctr_group *rate_ctr_group)
1527{
1528 rate_ctr_group_free(rate_ctr_group);
1529 return 0;
1530}
1531
Harald Welte3ac604e2019-05-08 14:07:41 +02001532static int alloc_mgcp_rate_counters(struct mgcp_trunk_config *trunk, void *ctx)
Stefan Sperling1e174872018-10-25 18:36:10 +02001533{
1534 /* FIXME: Each new rate counter group requires a unique index. At the
1535 * moment we generate an index using a counter, but perhaps there is
1536 * a better way of assigning indices? */
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001537 static unsigned int crcx_rate_ctr_index = 0;
1538 static unsigned int mdcx_rate_ctr_index = 0;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001539 static unsigned int dlcx_rate_ctr_index = 0;
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001540 static unsigned int all_rtp_conn_rate_ctr_index = 0;
Stefan Sperling1e174872018-10-25 18:36:10 +02001541
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001542 if (trunk->mgcp_crcx_ctr_group == NULL) {
1543 trunk->mgcp_crcx_ctr_group = rate_ctr_group_alloc(ctx, &mgcp_crcx_ctr_group_desc, crcx_rate_ctr_index);
Harald Welte3ac604e2019-05-08 14:07:41 +02001544 if (!trunk->mgcp_crcx_ctr_group)
1545 return -1;
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);
Harald Welte3ac604e2019-05-08 14:07:41 +02001551 if (!trunk->mgcp_mdcx_ctr_group)
1552 return -1;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001553 talloc_set_destructor(trunk->mgcp_mdcx_ctr_group, free_rate_counter_group);
1554 mdcx_rate_ctr_index++;
1555 }
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001556 if (trunk->mgcp_dlcx_ctr_group == NULL) {
1557 trunk->mgcp_dlcx_ctr_group = rate_ctr_group_alloc(ctx, &mgcp_dlcx_ctr_group_desc, dlcx_rate_ctr_index);
Harald Welte3ac604e2019-05-08 14:07:41 +02001558 if (!trunk->mgcp_dlcx_ctr_group)
1559 return -1;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001560 talloc_set_destructor(trunk->mgcp_dlcx_ctr_group, free_rate_counter_group);
1561 dlcx_rate_ctr_index++;
1562 }
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001563 if (trunk->all_rtp_conn_stats == NULL) {
1564 trunk->all_rtp_conn_stats = rate_ctr_group_alloc(ctx, &all_rtp_conn_rate_ctr_group_desc,
1565 all_rtp_conn_rate_ctr_index);
Harald Welte3ac604e2019-05-08 14:07:41 +02001566 if (!trunk->all_rtp_conn_stats)
1567 return -1;
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001568 talloc_set_destructor(trunk->all_rtp_conn_stats, free_rate_counter_group);
1569 all_rtp_conn_rate_ctr_index++;
1570 }
Harald Welte3ac604e2019-05-08 14:07:41 +02001571 return 0;
Stefan Sperling1e174872018-10-25 18:36:10 +02001572}
1573
Philipp Maier87bd9be2017-08-22 16:35:41 +02001574/*! allocate configuration with default values.
1575 * (called once at startup by main function) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001576struct mgcp_config *mgcp_config_alloc(void)
1577{
1578 struct mgcp_config *cfg;
1579
1580 cfg = talloc_zero(NULL, struct mgcp_config);
1581 if (!cfg) {
1582 LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n");
1583 return NULL;
1584 }
1585
Philipp Maier12943ea2018-01-17 15:40:25 +01001586 osmo_strlcpy(cfg->domain, "mgw", sizeof(cfg->domain));
1587
Philipp Maier87bd9be2017-08-22 16:35:41 +02001588 cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START;
1589 cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END;
1590 cfg->net_ports.last_port = cfg->net_ports.range_start;
1591
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001592 cfg->source_port = 2427;
1593 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
1594 cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
1595
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001596 cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
1597 cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
1598
1599 cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
1600
1601 /* default trunk handling */
1602 cfg->trunk.cfg = cfg;
1603 cfg->trunk.trunk_nr = 0;
1604 cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
1605 cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
1606 cfg->trunk.audio_payload = 126;
1607 cfg->trunk.audio_send_ptime = 1;
1608 cfg->trunk.audio_send_name = 1;
1609 cfg->trunk.omit_rtcp = 0;
1610 mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE);
Harald Welte3ac604e2019-05-08 14:07:41 +02001611 if (alloc_mgcp_rate_counters(&cfg->trunk, cfg) < 0) {
1612 talloc_free(cfg);
1613 return NULL;
1614 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001615
1616 INIT_LLIST_HEAD(&cfg->trunks);
1617
1618 return cfg;
1619}
1620
Philipp Maier87bd9be2017-08-22 16:35:41 +02001621/*! allocate configuration with default values.
1622 * (called once at startup by VTY)
1623 * \param[in] cfg mgcp configuration
1624 * \param[in] nr trunk number
1625 * \returns pointer to allocated trunk configuration */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001626struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
1627{
1628 struct mgcp_trunk_config *trunk;
1629
1630 trunk = talloc_zero(cfg, struct mgcp_trunk_config);
1631 if (!trunk) {
1632 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n");
1633 return NULL;
1634 }
1635
1636 trunk->cfg = cfg;
1637 trunk->trunk_type = MGCP_TRUNK_E1;
1638 trunk->trunk_nr = nr;
1639 trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
1640 trunk->audio_payload = 126;
1641 trunk->audio_send_ptime = 1;
1642 trunk->audio_send_name = 1;
Philipp Maierfcd06552017-11-10 17:32:22 +01001643 trunk->vty_number_endpoints = 33;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001644 trunk->omit_rtcp = 0;
1645 mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001646 alloc_mgcp_rate_counters(trunk, trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001647 llist_add_tail(&trunk->entry, &cfg->trunks);
Stefan Sperling1e174872018-10-25 18:36:10 +02001648
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001649 return trunk;
1650}
1651
Philipp Maier87bd9be2017-08-22 16:35:41 +02001652/*! get trunk configuration by trunk number (index).
1653 * \param[in] cfg mgcp configuration
1654 * \param[in] index trunk number
1655 * \returns pointer to trunk configuration, NULL on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001656struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
1657{
1658 struct mgcp_trunk_config *trunk;
1659
1660 llist_for_each_entry(trunk, &cfg->trunks, entry)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001661 if (trunk->trunk_nr == index)
1662 return trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001663
1664 return NULL;
1665}
1666
Philipp Maier87bd9be2017-08-22 16:35:41 +02001667/*! allocate endpoints and set default values.
1668 * (called once at startup by VTY)
1669 * \param[in] tcfg trunk configuration
1670 * \returns 0 on success, -1 on failure */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001671int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
1672{
1673 int i;
1674
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001675 tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001676 sizeof(struct mgcp_endpoint),
Philipp Maierfcd06552017-11-10 17:32:22 +01001677 tcfg->vty_number_endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001678 "endpoints");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001679 if (!tcfg->endpoints)
1680 return -1;
1681
Philipp Maierfcd06552017-11-10 17:32:22 +01001682 for (i = 0; i < tcfg->vty_number_endpoints; ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001683 INIT_LLIST_HEAD(&tcfg->endpoints[i].conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001684 tcfg->endpoints[i].cfg = tcfg->cfg;
1685 tcfg->endpoints[i].tcfg = tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001686
Pau Espin Pedrol9f11dc52019-04-22 20:48:50 +02001687 switch (tcfg->trunk_type) {
1688 case MGCP_TRUNK_VIRTUAL:
1689 tcfg->endpoints[i].type = &ep_typeset.rtp;
1690 break;
1691 case MGCP_TRUNK_E1:
1692 /* FIXME: Implement E1 allocation */
1693 LOGP(DLMGCP, LOGL_FATAL, "E1 trunks not implemented!\n");
1694 break;
1695 default:
1696 osmo_panic("Cannot allocate unimplemented trunk type %d! %s:%d\n",
1697 tcfg->trunk_type, __FILE__, __LINE__);
1698 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001699 }
1700
Philipp Maierfcd06552017-11-10 17:32:22 +01001701 tcfg->number_endpoints = tcfg->vty_number_endpoints;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001702 alloc_mgcp_rate_counters(tcfg, tcfg->cfg);
Stefan Sperling1e174872018-10-25 18:36:10 +02001703
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001704 return 0;
1705}
1706
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001707static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1708{
1709 return write(cfg->gw_fd.bfd.fd, buf, len);
1710}
1711
Philipp Maier87bd9be2017-08-22 16:35:41 +02001712/*! Reset all endpoints by sending RSIP message to self.
1713 * (called by VTY)
1714 * \param[in] endp trunk endpoint
1715 * \param[in] endpoint number
1716 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001717int mgcp_send_reset_all(struct mgcp_config *cfg)
1718{
Philipp Maier12943ea2018-01-17 15:40:25 +01001719 char buf[MGCP_ENDPOINT_MAXLEN + 128];
1720 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001721 int rc;
1722
Philipp Maier12943ea2018-01-17 15:40:25 +01001723 len = snprintf(buf, sizeof(buf),
1724 "RSIP 1 *@%s MGCP 1.0\r\n", cfg->domain);
1725 if (len < 0)
1726 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001727
Philipp Maier12943ea2018-01-17 15:40:25 +01001728 rc = send_agent(cfg, buf, len);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001729 if (rc <= 0)
1730 return -1;
1731
1732 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001733}
1734
Philipp Maier87bd9be2017-08-22 16:35:41 +02001735/*! Reset a single endpoint by sending RSIP message to self.
1736 * (called by VTY)
1737 * \param[in] endp trunk endpoint
1738 * \param[in] endpoint number
1739 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001740int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
1741{
Philipp Maier12943ea2018-01-17 15:40:25 +01001742 char buf[MGCP_ENDPOINT_MAXLEN + 128];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001743 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001744 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001745
1746 len = snprintf(buf, sizeof(buf),
Philipp Maier12943ea2018-01-17 15:40:25 +01001747 "RSIP 39 %x@%s MGCP 1.0\r\n", endpoint, endp->cfg->domain);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001748 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001749 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001750
Philipp Maier87bd9be2017-08-22 16:35:41 +02001751 rc = send_agent(endp->cfg, buf, len);
1752 if (rc <= 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001753 return -1;
1754
Philipp Maier87bd9be2017-08-22 16:35:41 +02001755 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001756}