blob: 3a4591fa1419f57b6ed279f55b23fdda576668f2 [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{
Pau Espin Pedrola93c6e92019-05-06 15:23:57 +0200303 /* TODO: we may want to define another local_ip_osmux var to us for
304 OSMUX connections. Perhaps adding a new internal API to get it based
305 on conn type */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200306 const char *addr = endp->cfg->local_ip;
Philipp Maier8970c492017-10-11 13:33:42 +0200307 struct msgb *sdp;
308 int rc;
309 struct msgb *result;
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 Maier55295f72018-01-15 14:00:28 +0100321 /* Attach optional connection parameters */
322 if (add_conn_params) {
323 rc = add_params(sdp, endp, conn);
324 if (rc < 0)
325 goto error;
326 }
327
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100328 /* Attach optional OSMUX parameters */
Pau Espin Pedrolc63f15a2019-05-10 16:52:08 +0200329 if (mgcp_conn_rtp_is_osmux(conn)) {
Pau Espin Pedrol5e8d7992019-04-24 19:56:43 +0200330 rc = msgb_printf(sdp, "X-Osmux: %u\r\n", conn->osmux.cid);
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100331 if (rc < 0)
332 goto error;
333 }
334
335 /* Attach line break to separate the parameters from the SDP block */
Philipp Maierc3cfae22018-01-22 12:03:03 +0100336 rc = msgb_printf(sdp, "\r\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200337
Philipp Maier8970c492017-10-11 13:33:42 +0200338 rc = mgcp_write_response_sdp(endp, conn, sdp, addr);
339 if (rc < 0)
340 goto error;
341 result = create_resp(endp, 200, " OK", msg, trans_id, NULL, (char*) sdp->data);
342 msgb_free(sdp);
343 return result;
344error:
345 msgb_free(sdp);
346 return NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200347}
348
Philipp Maier87bd9be2017-08-22 16:35:41 +0200349/* Send out dummy packet to keep the connection open, if the connection is an
350 * osmux connection, send the dummy packet via OSMUX */
351static void send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200352{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200353 if (conn->osmux.state != OSMUX_STATE_DISABLED)
354 osmux_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200355 else
Philipp Maier87bd9be2017-08-22 16:35:41 +0200356 mgcp_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200357}
358
Philipp Maier87bd9be2017-08-22 16:35:41 +0200359/* handle incoming messages:
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200360 * - this can be a command (four letters, space, transaction id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200361 * - or a response (three numbers, space, transaction id) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200362struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
363{
364 struct mgcp_parse_data pdata;
Harald Weltee35eeae2017-12-28 13:47:37 +0100365 int rc, i, code, handled = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200366 struct msgb *resp = NULL;
367 char *data;
368
369 if (msgb_l2len(msg) < 4) {
370 LOGP(DLMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len);
371 return NULL;
372 }
373
374 if (mgcp_msg_terminate_nul(msg))
375 return NULL;
376
Philipp Maier87bd9be2017-08-22 16:35:41 +0200377 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Received message");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200378
Philipp Maier87bd9be2017-08-22 16:35:41 +0200379 /* attempt to treat it as a response */
380 if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200381 LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code);
382 return NULL;
383 }
384
385 msg->l3h = &msg->l2h[4];
386
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200387 /*
388 * Check for a duplicate message and respond.
389 */
390 memset(&pdata, 0, sizeof(pdata));
391 pdata.cfg = cfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200392 data = mgcp_strline((char *)msg->l3h, &pdata.save);
Harald Weltee35eeae2017-12-28 13:47:37 +0100393 rc = mgcp_parse_header(&pdata, data);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200394 if (pdata.endp && pdata.trans
Philipp Maier87bd9be2017-08-22 16:35:41 +0200395 && pdata.endp->last_trans
396 && strcmp(pdata.endp->last_trans, pdata.trans) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200397 return do_retransmission(pdata.endp);
398 }
399
Harald Welteabbb6b92017-12-28 13:13:50 +0100400 /* check for general parser failure */
Harald Weltee35eeae2017-12-28 13:47:37 +0100401 if (rc < 0) {
Harald Welteabbb6b92017-12-28 13:13:50 +0100402 LOGP(DLMGCP, LOGL_NOTICE, "%s: failed to find the endpoint\n", msg->l2h);
Harald Weltee35eeae2017-12-28 13:47:37 +0100403 return create_err_response(NULL, -rc, (const char *) msg->l2h, pdata.trans);
Harald Welteabbb6b92017-12-28 13:13:50 +0100404 }
405
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200406 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200407 if (strncmp
408 (mgcp_requests[i].name, (const char *)&msg->l2h[0],
409 4) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200410 handled = 1;
411 resp = mgcp_requests[i].handle_request(&pdata);
412 break;
413 }
414 }
415
416 if (!handled)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200417 LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n",
418 &msg->l2h[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200419
420 return resp;
421}
422
Philipp Maier87bd9be2017-08-22 16:35:41 +0200423/* AUEP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200424static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p)
425{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200426 LOGPENDP(p->endp, DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n");
Harald Welteabbb6b92017-12-28 13:13:50 +0100427 return create_ok_response(p->endp, 200, "AUEP", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200428}
429
Harald Welte1d1b98f2017-12-25 10:03:40 +0100430/* Try to find a free port by attempting to bind on it. Also handle the
Philipp Maier87bd9be2017-08-22 16:35:41 +0200431 * counter that points on the next free port. Since we have a pointer
Philipp Maierb38fb892018-05-22 13:52:21 +0200432 * to the next free port, binding should in work on the first attempt in
Pau Espin Pedrolfc806732019-04-23 00:18:43 +0200433 * general. In case of failure the next port is tried until the whole port
434 * range is tried once. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200435static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200436{
437 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200438 struct mgcp_rtp_end *end;
439 struct mgcp_port_range *range;
Philipp Maierb38fb892018-05-22 13:52:21 +0200440 unsigned int tries;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200441
Philipp Maier87bd9be2017-08-22 16:35:41 +0200442 OSMO_ASSERT(conn);
443 end = &conn->end;
444 OSMO_ASSERT(end);
445
446 range = &endp->cfg->net_ports;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200447
448 /* attempt to find a port */
Philipp Maierb38fb892018-05-22 13:52:21 +0200449 tries = (range->range_end - range->range_start) / 2;
450 for (i = 0; i < tries; ++i) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200451 int rc;
452
453 if (range->last_port >= range->range_end)
454 range->last_port = range->range_start;
455
Philipp Maier87bd9be2017-08-22 16:35:41 +0200456 rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200457
458 range->last_port += 2;
459 if (rc == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200460 return 0;
461 }
462
463 }
464
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200465 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
466 "Allocating a RTP/RTCP port failed %u times.\n",
467 tries);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200468 return -1;
469}
470
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200471/*! Helper function for check_local_cx_options() to get a pointer of the next
472 * lco option identifier
473 * \param[in] lco string
474 * \returns pointer to the beginning of the LCO identifier, NULL on failure */
475char *get_lco_identifier(const char *options)
476{
477 char *ptr;
478 unsigned int count = 0;
479
480 /* Jump to the end of the lco identifier */
481 ptr = strstr(options, ":");
482 if (!ptr)
483 return NULL;
484
485 /* Walk backwards until the pointer points to the beginning of the
486 * lco identifier. We know that we stand at the beginning when we
487 * are either at the beginning of the memory or see a space or
488 * comma. (this is tolerant, it will accept a:10, b:11 as well as
489 * a:10,b:11) */
490 while (1) {
491 /* Endless loop protection */
492 if (count > 10000)
493 return NULL;
494 else if (ptr < options || *ptr == ' ' || *ptr == ',') {
495 ptr++;
496 break;
497 }
498 ptr--;
499 count++;
500 }
501
502 /* Check if we got any result */
503 if (*ptr == ':')
504 return NULL;
505
506 return ptr;
507}
508
509/*! Check the LCO option. This function checks for multiple appearence of LCO
510 * options, which is illegal
511 * \param[in] ctx talloc context
512 * \param[in] lco string
513 * \returns 0 on success, -1 on failure */
514int check_local_cx_options(void *ctx, const char *options)
515{
516 int i;
517 char *options_copy;
518 char *lco_identifier;
519 char *lco_identifier_end;
520 char *next_lco_identifier;
521
522 char **lco_seen;
523 unsigned int lco_seen_n = 0;
524
525 if (!options)
526 return -1;
527
528 lco_seen =
529 (char **)talloc_zero_size(ctx, strlen(options) * sizeof(char *));
530 options_copy = talloc_strdup(ctx, options);
531 lco_identifier = options_copy;
532
533 do {
534 /* Move the lco_identifier pointer to the beginning of the
535 * current lco option identifier */
536 lco_identifier = get_lco_identifier(lco_identifier);
537 if (!lco_identifier)
538 goto error;
539
540 /* Look ahead to the next LCO option early, since we
541 * will parse destructively */
542 next_lco_identifier = strstr(lco_identifier + 1, ",");
543
544 /* Pinch off the end of the lco field identifier name
545 * and see if we still got something, also check if
546 * there is some value after the colon. */
547 lco_identifier_end = strstr(lco_identifier, ":");
548 if (!lco_identifier_end)
549 goto error;
550 if (*(lco_identifier_end + 1) == ' '
551 || *(lco_identifier_end + 1) == ','
552 || *(lco_identifier_end + 1) == '\0')
553 goto error;
554 *lco_identifier_end = '\0';
555 if (strlen(lco_identifier) == 0)
556 goto error;
557
558 /* Check if we have already seen the current field identifier
559 * before. If yes, we must bail, an LCO must only appear once
560 * in the LCO string */
561 for (i = 0; i < lco_seen_n; i++) {
562 if (strcmp(lco_seen[i], lco_identifier) == 0)
563 goto error;
564 }
565 lco_seen[lco_seen_n] = lco_identifier;
566 lco_seen_n++;
567
568 /* The first identifier must always be found at the beginnning
569 * of the LCO string */
570 if (lco_seen[0] != options_copy)
571 goto error;
572
573 /* Go to the next lco option */
574 lco_identifier = next_lco_identifier;
575 } while (lco_identifier);
576
577 talloc_free(lco_seen);
578 talloc_free(options_copy);
579 return 0;
580error:
581 talloc_free(lco_seen);
582 talloc_free(options_copy);
583 return -1;
584}
585
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200586/* Set the LCO from a string (see RFC 3435).
Harald Welte1d1b98f2017-12-25 10:03:40 +0100587 * The string is stored in the 'string' field. A NULL string is handled exactly
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200588 * like an empty string, the 'string' field is never NULL after this function
589 * has been called. */
Philipp Maiera390d0b2018-01-31 17:30:19 +0100590static int set_local_cx_options(void *ctx, struct mgcp_lco *lco,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200591 const char *options)
592{
Pau Espin Pedrolfe9a1fe2019-06-25 16:59:15 +0200593 char *lco_id;
Philipp Maier8dbc9ed2018-10-26 14:50:25 +0200594 char codec[17];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200595
Philipp Maier604410c2018-06-06 10:02:16 +0200596 if (!options)
597 return 0;
598 if (strlen(options) == 0)
599 return 0;
600
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200601 /* Make sure the encoding of the LCO is consistant before we proceed */
602 if (check_local_cx_options(ctx, options) != 0) {
603 LOGP(DLMGCP, LOGL_ERROR,
604 "local CX options: Internal inconsistency in Local Connection Options!\n");
605 return 524;
606 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200607
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200608 talloc_free(lco->string);
609 lco->string = talloc_strdup(ctx, options);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200610
Pau Espin Pedrolfe9a1fe2019-06-25 16:59:15 +0200611 lco_id = lco->string;
612 while ((lco_id = get_lco_identifier(lco_id))) {
613 switch (tolower(lco_id[0])) {
614 case 'p':
615 if (sscanf(lco_id + 1, ":%d-%d",
616 &lco->pkt_period_min, &lco->pkt_period_max) == 1)
617 lco->pkt_period_max = lco->pkt_period_min;
618 break;
619 case 'a':
620 /* FIXME: LCO also supports the negotiation of more then one codec.
621 * (e.g. a:PCMU;G726-32) But this implementation only supports a single
622 * codec only. */
623 if (sscanf(lco_id + 1, ":%16[^,]", codec) == 1) {
624 talloc_free(lco->codec);
625 lco->codec = talloc_strdup(ctx, codec);
626 }
627 break;
628 default:
629 LOGP(DLMGCP, LOGL_NOTICE,
630 "LCO: unhandled option: '%c'/%d in \"%s\"\n",
631 *lco_id, *lco_id, lco->string);
632 break;
633 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200634
Pau Espin Pedrolfe9a1fe2019-06-25 16:59:15 +0200635 lco_id = strchr(lco_id, ',');
636 if (!lco_id)
637 break;
Philipp Maier604410c2018-06-06 10:02:16 +0200638 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200639
640 LOGP(DLMGCP, LOGL_DEBUG,
641 "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n",
642 lco->pkt_period_max, lco->codec);
Philipp Maiera390d0b2018-01-31 17:30:19 +0100643
644 /* Check if the packetization fits the 20ms raster */
645 if (lco->pkt_period_min % 20 && lco->pkt_period_max % 20) {
646 LOGP(DLMGCP, LOGL_ERROR,
647 "local CX options: packetization interval is not a multiple of 20ms!\n");
648 return 535;
649 }
650
651 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200652}
653
654void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
655 struct mgcp_rtp_end *rtp)
656{
657 struct mgcp_trunk_config *tcfg = endp->tcfg;
658
659 int patch_ssrc = expect_ssrc_change && tcfg->force_constant_ssrc;
660
661 rtp->force_aligned_timing = tcfg->force_aligned_timing;
662 rtp->force_constant_ssrc = patch_ssrc ? 1 : 0;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100663 rtp->rfc5993_hr_convert = tcfg->rfc5993_hr_convert;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200664
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200665 LOGPENDP(endp, DLMGCP, LOGL_DEBUG,
666 "Configuring RTP endpoint: local port %d%s%s\n",
667 ntohs(rtp->rtp_port),
668 rtp->force_aligned_timing ? ", force constant timing" : "",
669 rtp->force_constant_ssrc ? ", force constant ssrc" : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200670}
671
672uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
673 struct mgcp_rtp_end *rtp)
674{
675 int f = 0;
676
677 /* Get the number of frames per channel and packet */
678 if (rtp->frames_per_packet)
679 f = rtp->frames_per_packet;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200680 else if (rtp->packet_duration_ms && rtp->codec->frame_duration_num) {
681 int den = 1000 * rtp->codec->frame_duration_num;
682 f = (rtp->packet_duration_ms * rtp->codec->frame_duration_den +
Philipp Maier87bd9be2017-08-22 16:35:41 +0200683 den / 2)
684 / den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200685 }
686
Philipp Maierbc0346e2018-06-07 09:52:16 +0200687 return rtp->codec->rate * f * rtp->codec->frame_duration_num /
688 rtp->codec->frame_duration_den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200689}
690
Pau Espin Pedrol9fb8ddf2019-05-08 15:35:36 +0200691/*! Initializes osmux socket if not yet initialized. Parses Osmux CID from MGCP line.
692 * \param[in] endp Endpoint willing to initialize osmux
693 * \param[in] line Line X-Osmux from MGCP header msg to parse
694 * \returns OSMUX CID, -1 for wildcard, -2 on parse error, -3 on osmux initalize error
695 */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200696static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line)
697{
698 if (!endp->cfg->osmux_init) {
699 if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200700 LOGPENDP(endp, DLMGCP, LOGL_ERROR, "Cannot init OSMUX\n");
Pau Espin Pedrol9fb8ddf2019-05-08 15:35:36 +0200701 return -3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200702 }
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200703 LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200704 }
705
706 return mgcp_parse_osmux_cid(line);
707}
708
Philipp Maierbc0346e2018-06-07 09:52:16 +0200709/* Process codec information contained in CRCX/MDCX */
710static int handle_codec_info(struct mgcp_conn_rtp *conn,
711 struct mgcp_parse_data *p, int have_sdp, bool crcx)
712{
713 struct mgcp_endpoint *endp = p->endp;
714 int rc;
715 char *cmd;
716
717 if (crcx)
718 cmd = "CRCX";
719 else
720 cmd = "MDCX";
721
722 /* Collect codec information */
723 if (have_sdp) {
724 /* If we have SDP, we ignore the local connection options and
725 * use only the SDP information. */
726 mgcp_codec_reset_all(conn);
727 rc = mgcp_parse_sdp_data(endp, conn, p);
728 if (rc != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200729 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
730 "%s: sdp not parseable\n", cmd);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200731
732 /* See also RFC 3661: Protocol error */
733 return 510;
734 }
735 } else if (endp->local_options.codec) {
736 /* When no SDP is available, we use the codec information from
737 * the local connection options (if present) */
738 mgcp_codec_reset_all(conn);
Philipp Maier228e5912019-03-05 13:56:59 +0100739 rc = mgcp_codec_add(conn, PTYPE_UNDEFINED, endp->local_options.codec, NULL);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200740 if (rc != 0)
741 goto error;
742 }
743
744 /* Make sure we always set a sane default codec */
745 if (conn->end.codecs_assigned == 0) {
746 /* When SDP and/or LCO did not supply any codec information,
747 * than it makes sense to pick a sane default: (payload-type 0,
748 * PCMU), see also: OS#2658 */
749 mgcp_codec_reset_all(conn);
Philipp Maier228e5912019-03-05 13:56:59 +0100750 rc = mgcp_codec_add(conn, 0, NULL, NULL);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200751 if (rc != 0)
752 goto error;
753 }
754
755 /* Make codec decision */
756 if (mgcp_codec_decide(conn) != 0)
757 goto error;
758
759 return 0;
760
761error:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200762 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
763 "%s: codec negotiation failure\n", cmd);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200764
765 /* See also RFC 3661: Codec negotiation failure */
766 return 534;
767}
768
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200769static bool parse_x_osmo_ign(struct mgcp_endpoint *endp, char *line)
770{
771 char *saveptr = NULL;
772
773 if (strncmp(line, MGCP_X_OSMO_IGN_HEADER, strlen(MGCP_X_OSMO_IGN_HEADER)))
774 return false;
775 line += strlen(MGCP_X_OSMO_IGN_HEADER);
776
777 while (1) {
778 char *token = strtok_r(line, " ", &saveptr);
779 line = NULL;
780 if (!token)
781 break;
782
783 if (!strcmp(token, "C"))
784 endp->x_osmo_ign |= MGCP_X_OSMO_IGN_CALLID;
785 else
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200786 LOGPENDP(endp, DLMGCP, LOGL_ERROR, "received unknown X-Osmo-IGN item '%s'\n", token);
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200787 }
788
789 return true;
790}
791
Philipp Maier87bd9be2017-08-22 16:35:41 +0200792/* CRCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200793static struct msgb *handle_create_con(struct mgcp_parse_data *p)
794{
Stefan Sperling1e174872018-10-25 18:36:10 +0200795 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200796 struct mgcp_endpoint *endp = p->endp;
Stefan Sperling9270e912018-10-29 14:10:00 +0100797 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_crcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200798 int error_code = 400;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200799 const char *local_options = NULL;
800 const char *callid = NULL;
801 const char *mode = NULL;
802 char *line;
Pau Espin Pedrol2b896172019-04-24 13:47:23 +0200803 int have_sdp = 0, osmux_cid = -2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200804 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100805 struct mgcp_conn *_conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200806 char conn_name[512];
Philipp Maiera390d0b2018-01-31 17:30:19 +0100807 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200808
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200809 LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200810
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200811 /* parse CallID C: and LocalParameters L: */
812 for_each_line(line, p->save) {
813 if (!mgcp_check_param(endp, line))
814 continue;
815
Pau Espin Pedrol0c6c3c12019-06-25 17:18:12 +0200816 switch (toupper(line[0])) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200817 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200818 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200819 break;
820 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200821 callid = (const char *)line + 3;
822 break;
823 case 'I':
Philipp Maierffd75e42017-11-22 11:44:50 +0100824 /* It is illegal to send a connection identifier
825 * together with a CRCX, the MGW will assign the
826 * connection identifier by itself on CRCX */
Stefan Sperling9270e912018-10-29 14:10:00 +0100827 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_BAD_ACTION]);
Philipp Maierffd75e42017-11-22 11:44:50 +0100828 return create_err_response(NULL, 523, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200829 break;
830 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200831 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200832 break;
833 case 'X':
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200834 if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200835 /* If osmux is disabled, just skip setting it up */
836 if (!p->endp->cfg->osmux)
837 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200838 osmux_cid = mgcp_osmux_setup(endp, line);
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200839 break;
840 }
841
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200842 if (parse_x_osmo_ign(endp, line))
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200843 break;
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200844
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200845 /* Ignore unknown X-headers */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200846 break;
847 case '\0':
848 have_sdp = 1;
849 goto mgcp_header_done;
850 default:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200851 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
852 "CRCX: unhandled option: '%c'/%d\n", *line, *line);
Stefan Sperling9270e912018-10-29 14:10:00 +0100853 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +0100854 return create_err_response(NULL, 539, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200855 break;
856 }
857 }
858
859mgcp_header_done:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200860 /* Check parameters */
861 if (!callid) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200862 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
863 "CRCX: insufficient parameters, missing callid\n");
Stefan Sperling9270e912018-10-29 14:10:00 +0100864 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_MISSING_CALLID]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100865 return create_err_response(endp, 516, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200866 }
867
Philipp Maier87bd9be2017-08-22 16:35:41 +0200868 if (!mode) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200869 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
870 "CRCX: insufficient parameters, missing mode\n");
Stefan Sperling9270e912018-10-29 14:10:00 +0100871 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_MODE]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100872 return create_err_response(endp, 517, "CRCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200873 }
874
Philipp Maier87bd9be2017-08-22 16:35:41 +0200875 /* Check if we are able to accept the creation of another connection */
876 if (llist_count(&endp->conns) >= endp->type->max_conns) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200877 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
878 "CRCX: endpoint full, max. %i connections allowed!\n",
879 endp->type->max_conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200880 if (tcfg->force_realloc) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200881 /* There is no more room for a connection, make some
882 * room by blindly tossing the oldest of the two two
883 * connections */
884 mgcp_conn_free_oldest(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200885 } else {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200886 /* There is no more room for a connection, leave
887 * everything as it is and return with an error */
Stefan Sperling9270e912018-10-29 14:10:00 +0100888 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_LIMIT_EXCEEDED]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100889 return create_err_response(endp, 540, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200890 }
891 }
892
Philipp Maier87bd9be2017-08-22 16:35:41 +0200893 /* Check if this endpoint already serves a call, if so, check if the
894 * callids match up so that we are sure that this is our call */
895 if (endp->callid && mgcp_verify_call_id(endp, callid)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200896 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
897 "CRCX: already seized by other call (%s)\n",
898 endp->callid);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200899 if (tcfg->force_realloc)
900 /* This is not our call, toss everything by releasing
901 * the entire endpoint. (rude!) */
Philipp Maier1355d7e2018-02-01 14:30:06 +0100902 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200903 else {
904 /* This is not our call, leave everything as it is and
905 * return with an error. */
Stefan Sperling9270e912018-10-29 14:10:00 +0100906 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_UNKNOWN_CALLID]);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200907 return create_err_response(endp, 400, "CRCX", p->trans);
908 }
909 }
910
911 /* Set the callid, creation of another connection will only be possible
Harald Welte1d1b98f2017-12-25 10:03:40 +0100912 * when the callid matches up. (Connections are distinguished by their
Philipp Maier87bd9be2017-08-22 16:35:41 +0200913 * connection ids) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200914 endp->callid = talloc_strdup(tcfg->endpoints, callid);
915
Philipp Maierffd75e42017-11-22 11:44:50 +0100916 snprintf(conn_name, sizeof(conn_name), "%s", callid);
917 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP, conn_name);
918 if (!_conn) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200919 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
920 "CRCX: unable to allocate RTP connection\n");
Stefan Sperling9270e912018-10-29 14:10:00 +0100921 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_ALLOC_CONN]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200922 goto error2;
923
Philipp Maier87bd9be2017-08-22 16:35:41 +0200924 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100925 conn = mgcp_conn_get_rtp(endp, _conn->id);
926 OSMO_ASSERT(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200927
928 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
929 error_code = 517;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100930 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_MODE]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200931 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200932 }
933
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200934 /* Annotate Osmux circuit ID and set it to negotiating state until this
Philipp Maier87bd9be2017-08-22 16:35:41 +0200935 * is fully set up from the dummy load. */
936 conn->osmux.state = OSMUX_STATE_DISABLED;
Pau Espin Pedrol2b896172019-04-24 13:47:23 +0200937 if (osmux_cid >= -1) { /* -1 is wilcard, alloc next avail CID */
Pau Espin Pedrol14f8a082019-05-13 13:10:06 +0200938 conn->osmux.state = OSMUX_STATE_ACTIVATING;
Pau Espin Pedrol2b896172019-04-24 13:47:23 +0200939 if (conn_osmux_allocate_cid(conn, osmux_cid) == -1) {
940 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_OSMUX]);
941 goto error2;
942 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200943 } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200944 LOGPCONN(_conn, DLMGCP, LOGL_ERROR,
945 "CRCX: osmux only and no osmux offered\n");
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100946 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_OSMUX]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200947 goto error2;
948 }
949
Philipp Maierbc0346e2018-06-07 09:52:16 +0200950 /* Set local connection options, if present */
951 if (local_options) {
952 rc = set_local_cx_options(endp->tcfg->endpoints,
953 &endp->local_options, local_options);
954 if (rc != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200955 LOGPCONN(_conn, DLMGCP, LOGL_ERROR,
956 "CRCX: inavlid local connection options!\n");
Philipp Maierbc0346e2018-06-07 09:52:16 +0200957 error_code = rc;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100958 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS]);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200959 goto error2;
960 }
961 }
962
963 /* Handle codec information and decide for a suitable codec */
964 rc = handle_codec_info(conn, p, have_sdp, true);
965 mgcp_codec_summary(conn);
966 if (rc) {
967 error_code = rc;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100968 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_CODEC_NEGOTIATION]);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200969 goto error2;
970 }
971
Philipp Maier87bd9be2017-08-22 16:35:41 +0200972 conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints,
973 tcfg->audio_fmtp_extra);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200974
Philipp Maier87bd9be2017-08-22 16:35:41 +0200975 if (p->cfg->force_ptime) {
976 conn->end.packet_duration_ms = p->cfg->force_ptime;
977 conn->end.force_output_ptime = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200978 }
979
Philipp Maier1cb1e382017-11-02 17:16:04 +0100980 mgcp_rtp_end_config(endp, 0, &conn->end);
981
Philipp Maierc3cc6542018-02-02 12:58:42 +0100982 /* check connection mode setting */
983 if (conn->conn->mode != MGCP_CONN_LOOPBACK
984 && conn->conn->mode != MGCP_CONN_RECV_ONLY
985 && conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200986 LOGPCONN(_conn, DLMGCP, LOGL_ERROR,
987 "CRCX: selected connection mode type requires an opposite end!\n");
Philipp Maierc3cc6542018-02-02 12:58:42 +0100988 error_code = 527;
Stefan Sperling9270e912018-10-29 14:10:00 +0100989 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC]);
Philipp Maierc3cc6542018-02-02 12:58:42 +0100990 goto error2;
991 }
992
Philipp Maier1cb1e382017-11-02 17:16:04 +0100993 if (allocate_port(endp, conn) != 0) {
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100994 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_BIND_PORT]);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100995 goto error2;
996 }
997
Philipp Maier87bd9be2017-08-22 16:35:41 +0200998 if (setup_rtp_processing(endp, conn) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200999 LOGPCONN(_conn, DLMGCP, LOGL_ERROR,
1000 "CRCX: could not start RTP processing!\n");
Stefan Sperling9270e912018-10-29 14:10:00 +01001001 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_START_RTP]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001002 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001003 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001004
1005 /* policy CB */
1006 if (p->cfg->policy_cb) {
1007 int rc;
1008 rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001009 MGCP_ENDP_CRCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001010 switch (rc) {
1011 case MGCP_POLICY_REJECT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001012 LOGPCONN(_conn, DLMGCP, LOGL_NOTICE,
1013 "CRCX: CRCX rejected by policy\n");
Philipp Maier1355d7e2018-02-01 14:30:06 +01001014 mgcp_endp_release(endp);
Stefan Sperling9270e912018-10-29 14:10:00 +01001015 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001016 return create_err_response(endp, 400, "CRCX", p->trans);
1017 break;
1018 case MGCP_POLICY_DEFER:
1019 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001020 return NULL;
1021 break;
1022 case MGCP_POLICY_CONT:
1023 /* just continue */
1024 break;
1025 }
1026 }
1027
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001028 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG,
1029 "CRCX: Creating connection: port: %u\n", conn->end.local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001030 if (p->cfg->change_cb)
1031 p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX);
1032
Philipp Maiere726d4f2017-11-01 10:41:34 +01001033 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
1034 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001035 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
Philipp Maiere726d4f2017-11-01 10:41:34 +01001036 && tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001037 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001038
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001039 LOGPCONN(_conn, DLMGCP, LOGL_NOTICE,
1040 "CRCX: connection successfully created\n");
Stefan Sperling9270e912018-10-29 14:10:00 +01001041 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_SUCCESS]);
Philipp Maier55295f72018-01-15 14:00:28 +01001042 return create_response_with_sdp(endp, conn, "CRCX", p->trans, true);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001043error2:
Philipp Maier1355d7e2018-02-01 14:30:06 +01001044 mgcp_endp_release(endp);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001045 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1046 "CRCX: unable to create connection\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001047 return create_err_response(endp, error_code, "CRCX", p->trans);
1048}
1049
Philipp Maierbc0346e2018-06-07 09:52:16 +02001050
1051
1052
1053
Philipp Maier87bd9be2017-08-22 16:35:41 +02001054/* MDCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001055static struct msgb *handle_modify_con(struct mgcp_parse_data *p)
1056{
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001057 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001058 struct mgcp_endpoint *endp = p->endp;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001059 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_mdcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001060 int error_code = 500;
1061 int silent = 0;
1062 int have_sdp = 0;
1063 char *line;
1064 const char *local_options = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001065 const char *mode = NULL;
1066 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier01d24a32017-11-21 17:26:09 +01001067 const char *conn_id = NULL;
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001068 int osmux_cid = -2;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001069 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001070
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001071 LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001072
Philipp Maier5656fbf2018-02-02 14:41:58 +01001073 /* Prohibit wildcarded requests */
1074 if (endp->wildcarded_req) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001075 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1076 "MDCX: wildcarded endpoint names not supported.\n");
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001077 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_WILDCARD]);
Philipp Maier5656fbf2018-02-02 14:41:58 +01001078 return create_err_response(endp, 507, "MDCX", p->trans);
1079 }
1080
Philipp Maier87bd9be2017-08-22 16:35:41 +02001081 if (llist_count(&endp->conns) <= 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001082 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1083 "MDCX: endpoint is not holding a connection.\n");
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
Pau Espin Pedrol0c6c3c12019-06-25 17:18:12 +02001092 switch (toupper(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;
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001116 case 'X':
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +02001117 if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001118 /* If osmux is disabled, just skip setting it up */
1119 if (!p->endp->cfg->osmux)
1120 break;
1121 osmux_cid = mgcp_osmux_setup(endp, line);
1122 break;
1123 }
1124 /* Ignore unknown X-headers */
1125 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001126 case '\0':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001127 have_sdp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001128 goto mgcp_header_done;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001129 break;
1130 default:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001131 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1132 "MDCX: Unhandled MGCP option: '%c'/%d\n",
1133 line[0], line[0]);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001134 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001135 return create_err_response(NULL, 539, "MDCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001136 break;
1137 }
1138 }
1139
Philipp Maier87bd9be2017-08-22 16:35:41 +02001140mgcp_header_done:
Philipp Maier01d24a32017-11-21 17:26:09 +01001141 if (!conn_id) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001142 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1143 "MDCX: insufficient parameters, missing ci (connectionIdentifier)\n");
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001144 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_CONNID]);
Harald Weltee35eeae2017-12-28 13:47:37 +01001145 return create_err_response(endp, 515, "MDCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001146 }
1147
1148 conn = mgcp_conn_get_rtp(endp, conn_id);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001149 if (!conn) {
1150 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_CONN_NOT_FOUND]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001151 return create_err_response(endp, 400, "MDCX", p->trans);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001152 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001153
Oliver Smithe36b7752019-01-22 16:31:36 +01001154 mgcp_conn_watchdog_kick(conn->conn);
1155
Philipp Maier87bd9be2017-08-22 16:35:41 +02001156 if (mode) {
1157 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001158 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_MODE]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001159 error_code = 517;
1160 goto error3;
1161 }
1162 } else
Pau Espin Pedrol209eb9f2019-04-24 12:03:04 +02001163 conn->conn->mode = conn->conn->mode_orig;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001164
Philipp Maierbc0346e2018-06-07 09:52:16 +02001165 /* Set local connection options, if present */
1166 if (local_options) {
1167 rc = set_local_cx_options(endp->tcfg->endpoints,
1168 &endp->local_options, local_options);
1169 if (rc != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001170 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1171 "MDCX: invalid local connection options!\n");
Philipp Maierbc0346e2018-06-07 09:52:16 +02001172 error_code = rc;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001173 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS]);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001174 goto error3;
1175 }
1176 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001177
Philipp Maierbc0346e2018-06-07 09:52:16 +02001178 /* Handle codec information and decide for a suitable codec */
1179 rc = handle_codec_info(conn, p, have_sdp, false);
1180 mgcp_codec_summary(conn);
1181 if (rc) {
Philipp Maieraf07f662018-02-02 11:34:02 +01001182 error_code = rc;
1183 goto error3;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001184 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001185
Philipp Maierc3cc6542018-02-02 12:58:42 +01001186 /* check connection mode setting */
1187 if (conn->conn->mode != MGCP_CONN_LOOPBACK
1188 && conn->conn->mode != MGCP_CONN_RECV_ONLY
1189 && conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001190 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1191 "MDCX: selected connection mode type requires an opposite end!\n");
Philipp Maierc3cc6542018-02-02 12:58:42 +01001192 error_code = 527;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001193 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC]);
Philipp Maierc3cc6542018-02-02 12:58:42 +01001194 goto error3;
1195 }
1196
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001197 if (mgcp_conn_rtp_is_osmux(conn)) {
1198 OSMO_ASSERT(conn->osmux.cid_allocated);
1199 if (osmux_cid < -1) {
1200 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1201 "MDCX: Failed to parse Osmux CID!\n");
1202 goto error3;
1203 } else if (osmux_cid == -1) {
1204 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1205 "MDCX: wilcard in MDCX is not supported!\n");
1206 goto error3;
1207 } else if (osmux_cid != (int) conn->osmux.cid) {
1208 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1209 "MDCX: changing already allocated CID is not supported!\n");
1210 goto error3;
1211 }
1212 /* TODO: In the future (when we have recvCID!=sendCID), we need to
1213 tell Osmux code that osmux_cid is to be used as sendCID for
1214 that conn. */
1215 }
Philipp Maierbc0346e2018-06-07 09:52:16 +02001216
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001217 if (setup_rtp_processing(endp, conn) != 0) {
1218 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_START_RTP]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001219 goto error3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001220 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001221
Philipp Maier87bd9be2017-08-22 16:35:41 +02001222
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001223 /* policy CB */
1224 if (p->cfg->policy_cb) {
1225 int rc;
1226 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001227 MGCP_ENDP_MDCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001228 switch (rc) {
1229 case MGCP_POLICY_REJECT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001230 LOGPCONN(conn->conn, DLMGCP, LOGL_NOTICE,
1231 "MDCX: rejected by policy\n");
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001232 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001233 if (silent)
1234 goto out_silent;
1235 return create_err_response(endp, 400, "MDCX", p->trans);
1236 break;
1237 case MGCP_POLICY_DEFER:
1238 /* stop processing */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001239 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG,
1240 "MDCX: deferred by policy\n");
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001241 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_DEFERRED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001242 return NULL;
1243 break;
1244 case MGCP_POLICY_CONT:
1245 /* just continue */
1246 break;
1247 }
1248 }
1249
Philipp Maier87bd9be2017-08-22 16:35:41 +02001250 mgcp_rtp_end_config(endp, 1, &conn->end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001251
1252 /* modify */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001253 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG,
1254 "MDCX: modified conn:%s\n", mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001255 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001256 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1257 MGCP_ENDP_MDCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001258
Philipp Maiere726d4f2017-11-01 10:41:34 +01001259 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
1260 OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1261 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
1262 && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001263 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001264
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001265 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_SUCCESS]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001266 if (silent)
1267 goto out_silent;
1268
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001269 LOGPCONN(conn->conn, DLMGCP, LOGL_NOTICE,
1270 "MDCX: connection successfully modified\n");
Philipp Maier55295f72018-01-15 14:00:28 +01001271 return create_response_with_sdp(endp, conn, "MDCX", p->trans, false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001272error3:
1273 return create_err_response(endp, error_code, "MDCX", p->trans);
1274
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001275out_silent:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001276 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "MDCX: silent exit\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001277 return NULL;
1278}
1279
Philipp Maier87bd9be2017-08-22 16:35:41 +02001280/* DLCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001281static struct msgb *handle_delete_con(struct mgcp_parse_data *p)
1282{
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001283 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001284 struct mgcp_endpoint *endp = p->endp;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001285 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_dlcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001286 int error_code = 400;
1287 int silent = 0;
1288 char *line;
1289 char stats[1048];
Philipp Maier01d24a32017-11-21 17:26:09 +01001290 const char *conn_id = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001291 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001292
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001293 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1294 "DLCX: deleting connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001295
Philipp Maier5656fbf2018-02-02 14:41:58 +01001296 /* Prohibit wildcarded requests */
1297 if (endp->wildcarded_req) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001298 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1299 "DLCX: wildcarded endpoint names not supported.\n");
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001300 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_WILDCARD]);
Philipp Maier5656fbf2018-02-02 14:41:58 +01001301 return create_err_response(endp, 507, "DLCX", p->trans);
1302 }
1303
Philipp Maier87bd9be2017-08-22 16:35:41 +02001304 if (llist_count(&endp->conns) <= 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001305 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1306 "DLCX: endpoint is not holding a connection.\n");
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001307 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_NO_CONN]);
Harald Weltee35eeae2017-12-28 13:47:37 +01001308 return create_err_response(endp, 515, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001309 }
1310
1311 for_each_line(line, p->save) {
1312 if (!mgcp_check_param(endp, line))
1313 continue;
1314
Pau Espin Pedrol0c6c3c12019-06-25 17:18:12 +02001315 switch (toupper(line[0])) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001316 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +01001317 if (mgcp_verify_call_id(endp, line + 3) != 0) {
1318 error_code = 516;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001319 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CALLID]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001320 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001321 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001322 break;
1323 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001324 conn_id = (const char *)line + 3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001325 if ((error_code = mgcp_verify_ci(endp, conn_id))) {
1326 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CONNID]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001327 goto error3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001328 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001329 break;
1330 case 'Z':
1331 silent = strcmp("noanswer", line + 3) == 0;
1332 break;
1333 default:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001334 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1335 "DLCX: Unhandled MGCP option: '%c'/%d\n",
1336 line[0], line[0]);
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001337 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001338 return create_err_response(NULL, 539, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001339 break;
1340 }
1341 }
1342
1343 /* policy CB */
1344 if (p->cfg->policy_cb) {
1345 int rc;
1346 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001347 MGCP_ENDP_DLCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001348 switch (rc) {
1349 case MGCP_POLICY_REJECT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001350 LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "DLCX: rejected by policy\n");
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001351 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001352 if (silent)
1353 goto out_silent;
1354 return create_err_response(endp, 400, "DLCX", p->trans);
1355 break;
1356 case MGCP_POLICY_DEFER:
1357 /* stop processing */
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001358 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_DEFERRED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001359 return NULL;
1360 break;
1361 case MGCP_POLICY_CONT:
1362 /* just continue */
1363 break;
1364 }
1365 }
1366
Philipp Maierf4c0e372017-10-11 16:06:45 +02001367 /* When no connection id is supplied, we will interpret this as a
1368 * wildcarded DLCX and drop all connections at once. (See also
1369 * RFC3435 Section F.7) */
Philipp Maier01d24a32017-11-21 17:26:09 +01001370 if (!conn_id) {
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001371 int num_conns = llist_count(&endp->conns);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001372 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1373 "DLCX: missing ci (connectionIdentifier), will remove all connections (%d total) at once\n",
1374 num_conns);
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001375
1376 if (num_conns > 0)
1377 rate_ctr_add(&rate_ctrs->ctr[MGCP_DLCX_SUCCESS], num_conns);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001378
Philipp Maier1355d7e2018-02-01 14:30:06 +01001379 mgcp_endp_release(endp);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001380
1381 /* Note: In this case we do not return any statistics,
1382 * as we assume that the client is not interested in
1383 * this case. */
1384 return create_ok_response(endp, 200, "DLCX", p->trans);
1385 }
1386
Philipp Maierf4c0e372017-10-11 16:06:45 +02001387 /* Find the connection */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001388 conn = mgcp_conn_get_rtp(endp, conn_id);
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001389 if (!conn) {
1390 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CONNID]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001391 goto error3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001392 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001393 /* save the statistics of the current connection */
1394 mgcp_format_stats(stats, sizeof(stats), conn->conn);
1395
1396 /* delete connection */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001397 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG, "DLCX: deleting conn:%s\n",
1398 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001399 mgcp_conn_free(endp, conn_id);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001400 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1401 "DLCX: connection successfully deleted\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001402
1403 /* When all connections are closed, the endpoint will be released
1404 * in order to be ready to be used by another call. */
1405 if (llist_count(&endp->conns) <= 0) {
Philipp Maier1355d7e2018-02-01 14:30:06 +01001406 mgcp_endp_release(endp);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001407 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "DLCX: endpoint released\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001408 }
1409
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001410 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001411 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1412 MGCP_ENDP_DLCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001413
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001414 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_SUCCESS]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001415 if (silent)
1416 goto out_silent;
1417 return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats);
1418
1419error3:
1420 return create_err_response(endp, error_code, "DLCX", p->trans);
1421
1422out_silent:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001423 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "DLCX: silent exit\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001424 return NULL;
1425}
1426
Philipp Maier87bd9be2017-08-22 16:35:41 +02001427/* RSIP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001428static struct msgb *handle_rsip(struct mgcp_parse_data *p)
1429{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001430 /* TODO: Also implement the resetting of a specific endpoint
1431 * to make mgcp_send_reset_ep() work. Currently this will call
1432 * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1
1433 * to make read_call_agent() reset all endpoints when called
1434 * next time. In order to selectively reset endpoints some
1435 * mechanism to distinguish which endpoint shall be resetted
1436 * is needed */
1437
1438 LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n");
1439
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001440 if (p->cfg->reset_cb)
1441 p->cfg->reset_cb(p->endp->tcfg);
1442 return NULL;
1443}
1444
1445static char extract_tone(const char *line)
1446{
1447 const char *str = strstr(line, "D/");
1448 if (!str)
1449 return CHAR_MAX;
1450
1451 return str[2];
1452}
1453
Philipp Maier87bd9be2017-08-22 16:35:41 +02001454/* This can request like DTMF detection and forward, fax detection... it
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001455 * can also request when the notification should be send and such. We don't
Philipp Maier87bd9be2017-08-22 16:35:41 +02001456 * do this right now. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001457static struct msgb *handle_noti_req(struct mgcp_parse_data *p)
1458{
1459 int res = 0;
1460 char *line;
1461 char tone = CHAR_MAX;
1462
Philipp Maier87bd9be2017-08-22 16:35:41 +02001463 LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n");
1464
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001465 for_each_line(line, p->save) {
Pau Espin Pedrol0c6c3c12019-06-25 17:18:12 +02001466 switch (toupper(line[0])) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001467 case 'S':
1468 tone = extract_tone(line);
1469 break;
1470 }
1471 }
1472
1473 /* we didn't see a signal request with a tone */
1474 if (tone == CHAR_MAX)
1475 return create_ok_response(p->endp, 200, "RQNT", p->trans);
1476
1477 if (p->cfg->rqnt_cb)
1478 res = p->cfg->rqnt_cb(p->endp, tone);
1479
1480 return res == 0 ?
Philipp Maier87bd9be2017-08-22 16:35:41 +02001481 create_ok_response(p->endp, 200, "RQNT", p->trans) :
1482 create_err_response(p->endp, res, "RQNT", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001483}
1484
Philipp Maier87bd9be2017-08-22 16:35:41 +02001485/* Connection keepalive timer, will take care that dummy packets are send
Harald Welte1d1b98f2017-12-25 10:03:40 +01001486 * regularly, so that NAT connections stay open */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001487static void mgcp_keepalive_timer_cb(void *_tcfg)
1488{
1489 struct mgcp_trunk_config *tcfg = _tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001490 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001491 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001492
Philipp Maiere726d4f2017-11-01 10:41:34 +01001493 LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001494 tcfg->trunk_nr);
1495
Philipp Maiere726d4f2017-11-01 10:41:34 +01001496 /* Do not accept invalid configuration values
1497 * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and
1498 * values greater 0 */
1499 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1500
1501 /* The dummy packet functionality has been disabled, we will exit
1502 * immediately, no further timer is scheduled, which means we will no
1503 * longer send dummy packets even when we did before */
1504 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER)
1505 return;
1506
1507 /* In cases where only one dummy packet is sent, we do not need
1508 * the timer since the functions that handle the CRCX and MDCX are
1509 * triggering the sending of the dummy packet. So we behave like in
1510 * the MGCP_KEEPALIVE_NEVER case */
1511 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001512 return;
1513
Philipp Maier87bd9be2017-08-22 16:35:41 +02001514 /* Send walk over all endpoints and send out dummy packets through
1515 * every connection present on each endpoint */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001516 for (i = 1; i < tcfg->number_endpoints; ++i) {
1517 struct mgcp_endpoint *endp = &tcfg->endpoints[i];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001518 llist_for_each_entry(conn, &endp->conns, entry) {
1519 if (conn->mode == MGCP_CONN_RECV_ONLY)
1520 send_dummy(endp, &conn->u.rtp);
1521 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001522 }
1523
Philipp Maiere726d4f2017-11-01 10:41:34 +01001524 /* Schedule the keepalive timer for the next round */
1525 LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001526 tcfg->trunk_nr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001527 osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval,
1528 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001529}
1530
1531void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval)
1532{
1533 tcfg->keepalive_interval = interval;
1534 osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg);
1535
1536 if (interval <= 0)
1537 osmo_timer_del(&tcfg->keepalive_timer);
1538 else
1539 osmo_timer_schedule(&tcfg->keepalive_timer,
1540 tcfg->keepalive_interval, 0);
1541}
1542
Stefan Sperling1e174872018-10-25 18:36:10 +02001543static int free_rate_counter_group(struct rate_ctr_group *rate_ctr_group)
1544{
1545 rate_ctr_group_free(rate_ctr_group);
1546 return 0;
1547}
1548
Harald Welte3ac604e2019-05-08 14:07:41 +02001549static int alloc_mgcp_rate_counters(struct mgcp_trunk_config *trunk, void *ctx)
Stefan Sperling1e174872018-10-25 18:36:10 +02001550{
1551 /* FIXME: Each new rate counter group requires a unique index. At the
1552 * moment we generate an index using a counter, but perhaps there is
1553 * a better way of assigning indices? */
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001554 static unsigned int crcx_rate_ctr_index = 0;
1555 static unsigned int mdcx_rate_ctr_index = 0;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001556 static unsigned int dlcx_rate_ctr_index = 0;
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001557 static unsigned int all_rtp_conn_rate_ctr_index = 0;
Stefan Sperling1e174872018-10-25 18:36:10 +02001558
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001559 if (trunk->mgcp_crcx_ctr_group == NULL) {
1560 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 +02001561 if (!trunk->mgcp_crcx_ctr_group)
1562 return -1;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001563 talloc_set_destructor(trunk->mgcp_crcx_ctr_group, free_rate_counter_group);
1564 crcx_rate_ctr_index++;
1565 }
1566 if (trunk->mgcp_mdcx_ctr_group == NULL) {
1567 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 +02001568 if (!trunk->mgcp_mdcx_ctr_group)
1569 return -1;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001570 talloc_set_destructor(trunk->mgcp_mdcx_ctr_group, free_rate_counter_group);
1571 mdcx_rate_ctr_index++;
1572 }
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001573 if (trunk->mgcp_dlcx_ctr_group == NULL) {
1574 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 +02001575 if (!trunk->mgcp_dlcx_ctr_group)
1576 return -1;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001577 talloc_set_destructor(trunk->mgcp_dlcx_ctr_group, free_rate_counter_group);
1578 dlcx_rate_ctr_index++;
1579 }
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001580 if (trunk->all_rtp_conn_stats == NULL) {
1581 trunk->all_rtp_conn_stats = rate_ctr_group_alloc(ctx, &all_rtp_conn_rate_ctr_group_desc,
1582 all_rtp_conn_rate_ctr_index);
Harald Welte3ac604e2019-05-08 14:07:41 +02001583 if (!trunk->all_rtp_conn_stats)
1584 return -1;
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001585 talloc_set_destructor(trunk->all_rtp_conn_stats, free_rate_counter_group);
1586 all_rtp_conn_rate_ctr_index++;
1587 }
Harald Welte3ac604e2019-05-08 14:07:41 +02001588 return 0;
Stefan Sperling1e174872018-10-25 18:36:10 +02001589}
1590
Philipp Maier87bd9be2017-08-22 16:35:41 +02001591/*! allocate configuration with default values.
1592 * (called once at startup by main function) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001593struct mgcp_config *mgcp_config_alloc(void)
1594{
1595 struct mgcp_config *cfg;
1596
1597 cfg = talloc_zero(NULL, struct mgcp_config);
1598 if (!cfg) {
1599 LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n");
1600 return NULL;
1601 }
1602
Philipp Maier12943ea2018-01-17 15:40:25 +01001603 osmo_strlcpy(cfg->domain, "mgw", sizeof(cfg->domain));
1604
Philipp Maier87bd9be2017-08-22 16:35:41 +02001605 cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START;
1606 cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END;
1607 cfg->net_ports.last_port = cfg->net_ports.range_start;
1608
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001609 cfg->source_port = 2427;
1610 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
1611 cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
1612
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001613 cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
1614 cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
1615
1616 cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
1617
1618 /* default trunk handling */
1619 cfg->trunk.cfg = cfg;
1620 cfg->trunk.trunk_nr = 0;
1621 cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
1622 cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
1623 cfg->trunk.audio_payload = 126;
1624 cfg->trunk.audio_send_ptime = 1;
1625 cfg->trunk.audio_send_name = 1;
1626 cfg->trunk.omit_rtcp = 0;
1627 mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE);
Harald Welte3ac604e2019-05-08 14:07:41 +02001628 if (alloc_mgcp_rate_counters(&cfg->trunk, cfg) < 0) {
1629 talloc_free(cfg);
1630 return NULL;
1631 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001632
1633 INIT_LLIST_HEAD(&cfg->trunks);
1634
1635 return cfg;
1636}
1637
Philipp Maier87bd9be2017-08-22 16:35:41 +02001638/*! allocate configuration with default values.
1639 * (called once at startup by VTY)
1640 * \param[in] cfg mgcp configuration
1641 * \param[in] nr trunk number
1642 * \returns pointer to allocated trunk configuration */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001643struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
1644{
1645 struct mgcp_trunk_config *trunk;
1646
1647 trunk = talloc_zero(cfg, struct mgcp_trunk_config);
1648 if (!trunk) {
1649 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n");
1650 return NULL;
1651 }
1652
1653 trunk->cfg = cfg;
1654 trunk->trunk_type = MGCP_TRUNK_E1;
1655 trunk->trunk_nr = nr;
1656 trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
1657 trunk->audio_payload = 126;
1658 trunk->audio_send_ptime = 1;
1659 trunk->audio_send_name = 1;
Philipp Maierfcd06552017-11-10 17:32:22 +01001660 trunk->vty_number_endpoints = 33;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001661 trunk->omit_rtcp = 0;
1662 mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001663 alloc_mgcp_rate_counters(trunk, trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001664 llist_add_tail(&trunk->entry, &cfg->trunks);
Stefan Sperling1e174872018-10-25 18:36:10 +02001665
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001666 return trunk;
1667}
1668
Philipp Maier87bd9be2017-08-22 16:35:41 +02001669/*! get trunk configuration by trunk number (index).
1670 * \param[in] cfg mgcp configuration
1671 * \param[in] index trunk number
1672 * \returns pointer to trunk configuration, NULL on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001673struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
1674{
1675 struct mgcp_trunk_config *trunk;
1676
1677 llist_for_each_entry(trunk, &cfg->trunks, entry)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001678 if (trunk->trunk_nr == index)
1679 return trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001680
1681 return NULL;
1682}
1683
Philipp Maier87bd9be2017-08-22 16:35:41 +02001684/*! allocate endpoints and set default values.
1685 * (called once at startup by VTY)
1686 * \param[in] tcfg trunk configuration
1687 * \returns 0 on success, -1 on failure */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001688int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
1689{
1690 int i;
1691
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001692 tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001693 sizeof(struct mgcp_endpoint),
Philipp Maierfcd06552017-11-10 17:32:22 +01001694 tcfg->vty_number_endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001695 "endpoints");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001696 if (!tcfg->endpoints)
1697 return -1;
1698
Philipp Maierfcd06552017-11-10 17:32:22 +01001699 for (i = 0; i < tcfg->vty_number_endpoints; ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001700 INIT_LLIST_HEAD(&tcfg->endpoints[i].conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001701 tcfg->endpoints[i].cfg = tcfg->cfg;
1702 tcfg->endpoints[i].tcfg = tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001703
Pau Espin Pedrol9f11dc52019-04-22 20:48:50 +02001704 switch (tcfg->trunk_type) {
1705 case MGCP_TRUNK_VIRTUAL:
1706 tcfg->endpoints[i].type = &ep_typeset.rtp;
1707 break;
1708 case MGCP_TRUNK_E1:
1709 /* FIXME: Implement E1 allocation */
1710 LOGP(DLMGCP, LOGL_FATAL, "E1 trunks not implemented!\n");
1711 break;
1712 default:
1713 osmo_panic("Cannot allocate unimplemented trunk type %d! %s:%d\n",
1714 tcfg->trunk_type, __FILE__, __LINE__);
1715 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001716 }
1717
Philipp Maierfcd06552017-11-10 17:32:22 +01001718 tcfg->number_endpoints = tcfg->vty_number_endpoints;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001719 alloc_mgcp_rate_counters(tcfg, tcfg->cfg);
Stefan Sperling1e174872018-10-25 18:36:10 +02001720
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001721 return 0;
1722}
1723
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001724static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1725{
1726 return write(cfg->gw_fd.bfd.fd, buf, len);
1727}
1728
Philipp Maier87bd9be2017-08-22 16:35:41 +02001729/*! Reset all endpoints by sending RSIP message to self.
1730 * (called by VTY)
1731 * \param[in] endp trunk endpoint
1732 * \param[in] endpoint number
1733 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001734int mgcp_send_reset_all(struct mgcp_config *cfg)
1735{
Philipp Maier12943ea2018-01-17 15:40:25 +01001736 char buf[MGCP_ENDPOINT_MAXLEN + 128];
1737 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001738 int rc;
1739
Philipp Maier12943ea2018-01-17 15:40:25 +01001740 len = snprintf(buf, sizeof(buf),
1741 "RSIP 1 *@%s MGCP 1.0\r\n", cfg->domain);
1742 if (len < 0)
1743 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001744
Philipp Maier12943ea2018-01-17 15:40:25 +01001745 rc = send_agent(cfg, buf, len);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001746 if (rc <= 0)
1747 return -1;
1748
1749 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001750}
1751
Philipp Maier87bd9be2017-08-22 16:35:41 +02001752/*! Reset a single endpoint by sending RSIP message to self.
1753 * (called by VTY)
1754 * \param[in] endp trunk endpoint
1755 * \param[in] endpoint number
1756 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001757int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
1758{
Philipp Maier12943ea2018-01-17 15:40:25 +01001759 char buf[MGCP_ENDPOINT_MAXLEN + 128];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001760 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001761 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001762
1763 len = snprintf(buf, sizeof(buf),
Philipp Maier12943ea2018-01-17 15:40:25 +01001764 "RSIP 39 %x@%s MGCP 1.0\r\n", endpoint, endp->cfg->domain);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001765 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001766 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001767
Philipp Maier87bd9be2017-08-22 16:35:41 +02001768 rc = send_agent(endp->cfg, buf, len);
1769 if (rc <= 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001770 return -1;
1771
Philipp Maier87bd9be2017-08-22 16:35:41 +02001772 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001773}