blob: caed0b70cacab9ab6ef89d7c8a3c14398959d454 [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
Alexander Chemeris63866002020-05-05 17:18:40 +030056static const struct rate_ctr_desc mgcp_general_ctr_desc[] = {
57 /* rx_msgs = rx_msgs_retransmitted + rx_msgs_handled + rx_msgs_unhandled + err_rx_msg_parse + err_rx_no_endpoint */
58 [MGCP_GENERAL_RX_MSGS_TOTAL] = {"mgcp:rx_msgs", "total number of MGCP messages received."},
59 [MGCP_GENERAL_RX_MSGS_RETRANSMITTED] = {"mgcp:rx_msgs_retransmitted", "number of received retransmissions."},
60 [MGCP_GENERAL_RX_MSGS_HANDLED] = {"mgcp:rx_msgs_handled", "number of handled MGCP messages."},
61 [MGCP_GENERAL_RX_MSGS_UNHANDLED] = {"mgcp:rx_msgs_unhandled", "number of unhandled MGCP messages."},
62 [MGCP_GENERAL_RX_FAIL_MSG_PARSE] = {"mgcp:err_rx_msg_parse", "error parsing MGCP message."},
63 [MGCP_GENERAL_RX_FAIL_NO_ENDPOINT] = {"mgcp:err_rx_no_endpoint", "can't find MGCP endpoint, probably we've used all allocated endpoints."},
64};
65
66const static struct rate_ctr_group_desc mgcp_general_ctr_group_desc = {
67 .group_name_prefix = "mgcp",
68 .group_description = "mgcp general statistics",
69 .class_id = OSMO_STATS_CLASS_GLOBAL,
70 .num_ctr = ARRAY_SIZE(mgcp_general_ctr_desc),
71 .ctr_desc = mgcp_general_ctr_desc
72};
73
Stefan Sperling1e174872018-10-25 18:36:10 +020074static const struct rate_ctr_desc mgcp_crcx_ctr_desc[] = {
75 [MGCP_CRCX_SUCCESS] = {"crcx:success", "CRCX command processed successfully."},
76 [MGCP_CRCX_FAIL_BAD_ACTION] = {"crcx:bad_action", "bad action in CRCX command."},
77 [MGCP_CRCX_FAIL_UNHANDLED_PARAM] = {"crcx:unhandled_param", "unhandled parameter in CRCX command."},
78 [MGCP_CRCX_FAIL_MISSING_CALLID] = {"crcx:missing_callid", "missing CallId in CRCX command."},
Stefan Sperlingaa823bf2018-10-29 14:51:41 +010079 [MGCP_CRCX_FAIL_INVALID_MODE] = {"crcx:invalid_mode", "invalid connection mode in CRCX command."},
Stefan Sperling1e174872018-10-25 18:36:10 +020080 [MGCP_CRCX_FAIL_LIMIT_EXCEEDED] = {"crcx:limit_exceeded", "limit of concurrent connections was reached."},
81 [MGCP_CRCX_FAIL_UNKNOWN_CALLID] = {"crcx:unkown_callid", "unknown CallId in CRCX command."},
82 [MGCP_CRCX_FAIL_ALLOC_CONN] = {"crcx:alloc_conn_fail", "connection allocation failure."},
83 [MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC] = {"crcx:no_remote_conn_desc", "no opposite end specified for connection."},
84 [MGCP_CRCX_FAIL_START_RTP] = {"crcx:start_rtp_failure", "failure to start RTP processing."},
85 [MGCP_CRCX_FAIL_REJECTED_BY_POLICY] = {"crcx:conn_rejected", "connection rejected by policy."},
Stefan Sperlinga714abf2018-10-29 14:19:54 +010086 [MGCP_CRCX_FAIL_NO_OSMUX] = {"crcx:no_osmux", "no osmux offered by peer."},
87 [MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS] = {"crcx:conn_opt", "connection options invalid."},
88 [MGCP_CRCX_FAIL_CODEC_NEGOTIATION] = {"crcx:codec_nego", "codec negotiation failure."},
89 [MGCP_CRCX_FAIL_BIND_PORT] = {"crcx:bind_port", "port bind failure."},
Stefan Sperling1e174872018-10-25 18:36:10 +020090};
91
92const static struct rate_ctr_group_desc mgcp_crcx_ctr_group_desc = {
93 .group_name_prefix = "crcx",
94 .group_description = "crxc statistics",
95 .class_id = OSMO_STATS_CLASS_GLOBAL,
96 .num_ctr = ARRAY_SIZE(mgcp_crcx_ctr_desc),
97 .ctr_desc = mgcp_crcx_ctr_desc
98};
99
Stefan Sperlingaa823bf2018-10-29 14:51:41 +0100100static const struct rate_ctr_desc mgcp_mdcx_ctr_desc[] = {
101 [MGCP_MDCX_SUCCESS] = {"mdcx:success", "MDCX command processed successfully."},
102 [MGCP_MDCX_FAIL_WILDCARD] = {"mdcx:wildcard", "wildcard endpoint names in MDCX commands are unsupported."},
103 [MGCP_MDCX_FAIL_NO_CONN] = {"mdcx:no_conn", "endpoint specified in MDCX command has no active connections."},
104 [MGCP_MDCX_FAIL_INVALID_CALLID] = {"mdcx:callid", "invalid CallId specified in MDCX command."},
105 [MGCP_MDCX_FAIL_INVALID_CONNID] = {"mdcx:connid", "invalid connection ID specified in MDCX command."},
106 [MGCP_MDCX_FAIL_UNHANDLED_PARAM] = {"crcx:unhandled_param", "unhandled parameter in MDCX command."},
107 [MGCP_MDCX_FAIL_NO_CONNID] = {"mdcx:no_connid", "no connection ID specified in MDCX command."},
108 [MGCP_MDCX_FAIL_CONN_NOT_FOUND] = {"mdcx:conn_not_found", "connection specified in MDCX command does not exist."},
109 [MGCP_MDCX_FAIL_INVALID_MODE] = {"mdcx:invalid_mode", "invalid connection mode in MDCX command."},
110 [MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS] = {"mdcx:conn_opt", "connection options invalid."},
111 [MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC] = {"mdcx:no_remote_conn_desc", "no opposite end specified for connection."},
112 [MGCP_MDCX_FAIL_START_RTP] = {"mdcx:start_rtp_failure", "failure to start RTP processing."},
113 [MGCP_MDCX_FAIL_REJECTED_BY_POLICY] = {"mdcx:conn_rejected", "connection rejected by policy."},
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +0100114 [MGCP_MDCX_DEFERRED_BY_POLICY] = {"mdcx:conn_deferred", "connection deferred by policy."},
Stefan Sperlingaa823bf2018-10-29 14:51:41 +0100115};
116
117const static struct rate_ctr_group_desc mgcp_mdcx_ctr_group_desc = {
118 .group_name_prefix = "mdcx",
119 .group_description = "mdcx statistics",
120 .class_id = OSMO_STATS_CLASS_GLOBAL,
121 .num_ctr = ARRAY_SIZE(mgcp_mdcx_ctr_desc),
122 .ctr_desc = mgcp_mdcx_ctr_desc
123};
124
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +0100125static const struct rate_ctr_desc mgcp_dlcx_ctr_desc[] = {
126 [MGCP_DLCX_SUCCESS] = {"dlcx:success", "DLCX command processed successfully."},
127 [MGCP_DLCX_FAIL_WILDCARD] = {"dlcx:wildcard", "wildcard names in DLCX commands are unsupported."},
128 [MGCP_DLCX_FAIL_NO_CONN] = {"dlcx:no_conn", "endpoint specified in DLCX command has no active connections."},
129 [MGCP_DLCX_FAIL_INVALID_CALLID] = {"dlcx:callid", "CallId specified in DLCX command mismatches endpoint's CallId ."},
130 [MGCP_DLCX_FAIL_INVALID_CONNID] = {"dlcx:connid", "connection ID specified in DLCX command does not exist on endpoint."},
131 [MGCP_DLCX_FAIL_UNHANDLED_PARAM] = {"dlcx:unhandled_param", "unhandled parameter in DLCX command."},
132 [MGCP_DLCX_FAIL_REJECTED_BY_POLICY] = {"dlcx:rejected", "connection deletion rejected by policy."},
133 [MGCP_DLCX_DEFERRED_BY_POLICY] = {"dlcx:deferred", "connection deletion deferred by policy."},
134};
135
136const static struct rate_ctr_group_desc mgcp_dlcx_ctr_group_desc = {
137 .group_name_prefix = "dlcx",
138 .group_description = "dlcx statistics",
139 .class_id = OSMO_STATS_CLASS_GLOBAL,
140 .num_ctr = ARRAY_SIZE(mgcp_dlcx_ctr_desc),
141 .ctr_desc = mgcp_dlcx_ctr_desc
142};
143
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100144const static struct rate_ctr_group_desc all_rtp_conn_rate_ctr_group_desc = {
145 .group_name_prefix = "all_rtp_conn",
146 .group_description = "aggregated statistics for all rtp connections",
147 .class_id = 1,
148 .num_ctr = ARRAY_SIZE(all_rtp_conn_rate_ctr_desc),
149 .ctr_desc = all_rtp_conn_rate_ctr_desc
150};
151
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200152static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *data);
153static struct msgb *handle_create_con(struct mgcp_parse_data *data);
154static struct msgb *handle_delete_con(struct mgcp_parse_data *data);
155static struct msgb *handle_modify_con(struct mgcp_parse_data *data);
156static struct msgb *handle_rsip(struct mgcp_parse_data *data);
157static struct msgb *handle_noti_req(struct mgcp_parse_data *data);
158
Philipp Maier87bd9be2017-08-22 16:35:41 +0200159/* Initalize transcoder */
160static int setup_rtp_processing(struct mgcp_endpoint *endp,
161 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200162{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200163 struct mgcp_config *cfg = endp->cfg;
164 struct mgcp_conn_rtp *conn_src = NULL;
165 struct mgcp_conn_rtp *conn_dst = conn;
166 struct mgcp_conn *_conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200167
Pau Espin Pedrolfa810e82019-05-06 18:54:10 +0200168 if (conn->type != MGCP_RTP_DEFAULT && !mgcp_conn_rtp_is_osmux(conn)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200169 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
170 "RTP-setup: Endpoint is not configured as RTP default, stopping here!\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200171 return 0;
172 }
173
Philipp Maier87bd9be2017-08-22 16:35:41 +0200174 if (conn->conn->mode == MGCP_CONN_LOOPBACK) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200175 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
176 "RTP-setup: Endpoint is in loopback mode, stopping here!\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +0200177 return 0;
178 }
179
180 /* Find the "sister" connection */
181 llist_for_each_entry(_conn, &endp->conns, entry) {
182 if (_conn->id != conn->conn->id) {
183 conn_src = &_conn->u.rtp;
184 break;
185 }
186 }
187
Philipp Maieracc10352018-07-19 18:07:57 +0200188 return cfg->setup_rtp_processing_cb(endp, conn_dst, conn_src);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200189}
190
Philipp Maier87bd9be2017-08-22 16:35:41 +0200191/* array of function pointers for handling various
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200192 * messages. In the future this might be binary sorted
Philipp Maier87bd9be2017-08-22 16:35:41 +0200193 * for performance reasons. */
194static const struct mgcp_request mgcp_requests[] = {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200195 MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint")
Oliver Smith622dd612019-01-30 14:14:45 +0100196 MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
197 MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
198 MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
199 MGCP_REQUEST("RQNT", handle_noti_req, "NotificationRequest")
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200200
Oliver Smith622dd612019-01-30 14:14:45 +0100201 /* SPEC extension */
202 MGCP_REQUEST("RSIP", handle_rsip, "ReSetInProgress")
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200203};
204
Philipp Maier87bd9be2017-08-22 16:35:41 +0200205/* Helper function to allocate some memory for responses and retransmissions */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200206static struct msgb *mgcp_msgb_alloc(void)
207{
208 struct msgb *msg;
209 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
210 if (!msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200211 LOGP(DLMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200212
213 return msg;
214}
215
Philipp Maier87bd9be2017-08-22 16:35:41 +0200216/* Helper function for do_retransmission() and create_resp() */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200217static struct msgb *do_retransmission(const struct mgcp_endpoint *endp)
218{
219 struct msgb *msg = mgcp_msgb_alloc();
220 if (!msg)
221 return NULL;
222
223 msg->l2h = msgb_put(msg, strlen(endp->last_response));
224 memcpy(msg->l2h, endp->last_response, msgb_l2len(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200225 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Retransmitted response");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200226 return msg;
227}
228
229static struct msgb *create_resp(struct mgcp_endpoint *endp, int code,
230 const char *txt, const char *msg,
231 const char *trans, const char *param,
232 const char *sdp)
233{
234 int len;
235 struct msgb *res;
236
237 res = mgcp_msgb_alloc();
238 if (!res)
239 return NULL;
240
Philipp Maier87bd9be2017-08-22 16:35:41 +0200241 len = snprintf((char *)res->data, 2048, "%d %s%s%s\r\n%s",
242 code, trans, txt, param ? param : "", sdp ? sdp : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200243 if (len < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200244 LOGPENDP(endp, DLMGCP, LOGL_ERROR, "Failed to sprintf MGCP response.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200245 msgb_free(res);
246 return NULL;
247 }
248
249 res->l2h = msgb_put(res, len);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200250 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "Generated response: code=%d\n", code);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200251 mgcp_disp_msg(res->l2h, msgb_l2len(res), "Generated response");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200252
253 /*
254 * Remember the last transmission per endpoint.
255 */
256 if (endp) {
257 struct mgcp_trunk_config *tcfg = endp->tcfg;
258 talloc_free(endp->last_response);
259 talloc_free(endp->last_trans);
260 endp->last_trans = talloc_strdup(tcfg->endpoints, trans);
261 endp->last_response = talloc_strndup(tcfg->endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200262 (const char *)res->l2h,
263 msgb_l2len(res));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200264 }
265
266 return res;
267}
268
269static struct msgb *create_ok_resp_with_param(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200270 int code, const char *msg,
271 const char *trans,
272 const char *param)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200273{
274 return create_resp(endp, code, " OK", msg, trans, param, NULL);
275}
276
277static struct msgb *create_ok_response(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200278 int code, const char *msg,
279 const char *trans)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200280{
281 return create_ok_resp_with_param(endp, code, msg, trans, NULL);
282}
283
284static struct msgb *create_err_response(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200285 int code, const char *msg,
286 const char *trans)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200287{
288 return create_resp(endp, code, " FAIL", msg, trans, NULL, NULL);
289}
290
Philipp Maier55295f72018-01-15 14:00:28 +0100291/* Add MGCP parameters to a message buffer */
292static int add_params(struct msgb *msg, const struct mgcp_endpoint *endp,
293 const struct mgcp_conn_rtp *conn)
294{
295 int rc;
296
Philipp Maier7f0966c2018-01-17 18:18:12 +0100297 /* NOTE: Only in the virtual trunk we allow dynamic endpoint names */
Philipp Maier207ab512018-02-02 14:19:26 +0100298 if (endp->wildcarded_req
Philipp Maier7f0966c2018-01-17 18:18:12 +0100299 && endp->tcfg->trunk_type == MGCP_TRUNK_VIRTUAL) {
Philipp Maierdd4ede32018-02-01 17:52:52 +0100300 rc = msgb_printf(msg, "Z: %s%x@%s\r\n",
Philipp Maier7f0966c2018-01-17 18:18:12 +0100301 MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK,
302 ENDPOINT_NUMBER(endp), endp->cfg->domain);
Philipp Maier55295f72018-01-15 14:00:28 +0100303 if (rc < 0)
304 return -EINVAL;
305 }
306
Philipp Maierc3cfae22018-01-22 12:03:03 +0100307 rc = msgb_printf(msg, "I: %s\r\n", conn->conn->id);
Philipp Maier55295f72018-01-15 14:00:28 +0100308 if (rc < 0)
309 return -EINVAL;
310
311 return 0;
312}
313
Philipp Maier87bd9be2017-08-22 16:35:41 +0200314/* Format MGCP response string (with SDP attached) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200315static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200316 struct mgcp_conn_rtp *conn,
317 const char *msg,
Philipp Maier55295f72018-01-15 14:00:28 +0100318 const char *trans_id,
319 bool add_conn_params)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200320{
Pau Espin Pedrola93c6e92019-05-06 15:23:57 +0200321 /* TODO: we may want to define another local_ip_osmux var to us for
322 OSMUX connections. Perhaps adding a new internal API to get it based
323 on conn type */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200324 const char *addr = endp->cfg->local_ip;
Philipp Maier8970c492017-10-11 13:33:42 +0200325 struct msgb *sdp;
326 int rc;
327 struct msgb *result;
Philipp Maier1cb1e382017-11-02 17:16:04 +0100328 char local_ip_addr[INET_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200329
Philipp Maier8970c492017-10-11 13:33:42 +0200330 sdp = msgb_alloc_headroom(4096, 128, "sdp record");
331 if (!sdp)
332 return NULL;
333
Philipp Maier1cb1e382017-11-02 17:16:04 +0100334 if (!addr) {
335 mgcp_get_local_addr(local_ip_addr, conn);
336 addr = local_ip_addr;
337 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200338
Philipp Maier55295f72018-01-15 14:00:28 +0100339 /* Attach optional connection parameters */
340 if (add_conn_params) {
341 rc = add_params(sdp, endp, conn);
342 if (rc < 0)
343 goto error;
344 }
345
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100346 /* Attach optional OSMUX parameters */
Pau Espin Pedrolc63f15a2019-05-10 16:52:08 +0200347 if (mgcp_conn_rtp_is_osmux(conn)) {
Pau Espin Pedrol5e8d7992019-04-24 19:56:43 +0200348 rc = msgb_printf(sdp, "X-Osmux: %u\r\n", conn->osmux.cid);
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100349 if (rc < 0)
350 goto error;
351 }
352
353 /* Attach line break to separate the parameters from the SDP block */
Philipp Maierc3cfae22018-01-22 12:03:03 +0100354 rc = msgb_printf(sdp, "\r\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200355
Philipp Maier8970c492017-10-11 13:33:42 +0200356 rc = mgcp_write_response_sdp(endp, conn, sdp, addr);
357 if (rc < 0)
358 goto error;
359 result = create_resp(endp, 200, " OK", msg, trans_id, NULL, (char*) sdp->data);
360 msgb_free(sdp);
361 return result;
362error:
363 msgb_free(sdp);
364 return NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200365}
366
Philipp Maier87bd9be2017-08-22 16:35:41 +0200367/* Send out dummy packet to keep the connection open, if the connection is an
368 * osmux connection, send the dummy packet via OSMUX */
369static void send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200370{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200371 if (conn->osmux.state != OSMUX_STATE_DISABLED)
372 osmux_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200373 else
Philipp Maier87bd9be2017-08-22 16:35:41 +0200374 mgcp_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200375}
376
Philipp Maier87bd9be2017-08-22 16:35:41 +0200377/* handle incoming messages:
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200378 * - this can be a command (four letters, space, transaction id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200379 * - or a response (three numbers, space, transaction id) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200380struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
381{
Harald Weltec39b1bf2020-03-08 11:29:39 +0100382 struct mgcp_trunk_config *tcfg = cfg->virt_trunk;
Alexander Chemeris63866002020-05-05 17:18:40 +0300383 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_general_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200384 struct mgcp_parse_data pdata;
Harald Weltee35eeae2017-12-28 13:47:37 +0100385 int rc, i, code, handled = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200386 struct msgb *resp = NULL;
387 char *data;
388
Alexander Chemeris63866002020-05-05 17:18:40 +0300389 /* Count all messages, even incorect ones */
390 rate_ctr_inc(&rate_ctrs->ctr[MGCP_GENERAL_RX_MSGS_TOTAL]);
391
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200392 if (msgb_l2len(msg) < 4) {
393 LOGP(DLMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len);
Alexander Chemeris63866002020-05-05 17:18:40 +0300394 rate_ctr_inc(&rate_ctrs->ctr[MGCP_GENERAL_RX_FAIL_MSG_PARSE]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200395 return NULL;
396 }
397
Alexander Chemeris63866002020-05-05 17:18:40 +0300398 if (mgcp_msg_terminate_nul(msg)) {
399 rate_ctr_inc(&rate_ctrs->ctr[MGCP_GENERAL_RX_FAIL_MSG_PARSE]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200400 return NULL;
Alexander Chemeris63866002020-05-05 17:18:40 +0300401 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200402
Philipp Maier87bd9be2017-08-22 16:35:41 +0200403 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Received message");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200404
Philipp Maier87bd9be2017-08-22 16:35:41 +0200405 /* attempt to treat it as a response */
406 if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200407 LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code);
Alexander Chemeris63866002020-05-05 17:18:40 +0300408 rate_ctr_inc(&rate_ctrs->ctr[MGCP_GENERAL_RX_FAIL_MSG_PARSE]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200409 return NULL;
410 }
411
412 msg->l3h = &msg->l2h[4];
413
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200414 /*
415 * Check for a duplicate message and respond.
416 */
417 memset(&pdata, 0, sizeof(pdata));
418 pdata.cfg = cfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200419 data = mgcp_strline((char *)msg->l3h, &pdata.save);
Harald Weltee35eeae2017-12-28 13:47:37 +0100420 rc = mgcp_parse_header(&pdata, data);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200421 if (pdata.endp && pdata.trans
Philipp Maier87bd9be2017-08-22 16:35:41 +0200422 && pdata.endp->last_trans
423 && strcmp(pdata.endp->last_trans, pdata.trans) == 0) {
Alexander Chemeris63866002020-05-05 17:18:40 +0300424 rate_ctr_inc(&rate_ctrs->ctr[MGCP_GENERAL_RX_MSGS_RETRANSMITTED]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200425 return do_retransmission(pdata.endp);
426 }
427
Harald Welteabbb6b92017-12-28 13:13:50 +0100428 /* check for general parser failure */
Harald Weltee35eeae2017-12-28 13:47:37 +0100429 if (rc < 0) {
Harald Welteabbb6b92017-12-28 13:13:50 +0100430 LOGP(DLMGCP, LOGL_NOTICE, "%s: failed to find the endpoint\n", msg->l2h);
Alexander Chemeris63866002020-05-05 17:18:40 +0300431 rate_ctr_inc(&rate_ctrs->ctr[MGCP_GENERAL_RX_FAIL_NO_ENDPOINT]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100432 return create_err_response(NULL, -rc, (const char *) msg->l2h, pdata.trans);
Harald Welteabbb6b92017-12-28 13:13:50 +0100433 }
434
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200435 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200436 if (strncmp
437 (mgcp_requests[i].name, (const char *)&msg->l2h[0],
438 4) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200439 handled = 1;
440 resp = mgcp_requests[i].handle_request(&pdata);
441 break;
442 }
443 }
444
Alexander Chemeris63866002020-05-05 17:18:40 +0300445 if (handled) {
446 rate_ctr_inc(&rate_ctrs->ctr[MGCP_GENERAL_RX_MSGS_HANDLED]);
447 } else {
448 rate_ctr_inc(&rate_ctrs->ctr[MGCP_GENERAL_RX_MSGS_UNHANDLED]);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200449 LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n",
450 &msg->l2h[0]);
Alexander Chemeris63866002020-05-05 17:18:40 +0300451 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200452
453 return resp;
454}
455
Philipp Maier87bd9be2017-08-22 16:35:41 +0200456/* AUEP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200457static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p)
458{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200459 LOGPENDP(p->endp, DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n");
Harald Welteabbb6b92017-12-28 13:13:50 +0100460 return create_ok_response(p->endp, 200, "AUEP", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200461}
462
Harald Welte1d1b98f2017-12-25 10:03:40 +0100463/* Try to find a free port by attempting to bind on it. Also handle the
Philipp Maier87bd9be2017-08-22 16:35:41 +0200464 * counter that points on the next free port. Since we have a pointer
Philipp Maierb38fb892018-05-22 13:52:21 +0200465 * to the next free port, binding should in work on the first attempt in
Pau Espin Pedrolfc806732019-04-23 00:18:43 +0200466 * general. In case of failure the next port is tried until the whole port
467 * range is tried once. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200468static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200469{
470 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200471 struct mgcp_rtp_end *end;
472 struct mgcp_port_range *range;
Philipp Maierb38fb892018-05-22 13:52:21 +0200473 unsigned int tries;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200474
Philipp Maier87bd9be2017-08-22 16:35:41 +0200475 OSMO_ASSERT(conn);
476 end = &conn->end;
477 OSMO_ASSERT(end);
478
479 range = &endp->cfg->net_ports;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200480
481 /* attempt to find a port */
Philipp Maierb38fb892018-05-22 13:52:21 +0200482 tries = (range->range_end - range->range_start) / 2;
483 for (i = 0; i < tries; ++i) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200484 int rc;
485
486 if (range->last_port >= range->range_end)
487 range->last_port = range->range_start;
488
Philipp Maier87bd9be2017-08-22 16:35:41 +0200489 rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200490
491 range->last_port += 2;
492 if (rc == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200493 return 0;
494 }
495
496 }
497
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200498 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
499 "Allocating a RTP/RTCP port failed %u times.\n",
500 tries);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200501 return -1;
502}
503
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200504/*! Helper function for check_local_cx_options() to get a pointer of the next
505 * lco option identifier
506 * \param[in] lco string
507 * \returns pointer to the beginning of the LCO identifier, NULL on failure */
508char *get_lco_identifier(const char *options)
509{
510 char *ptr;
511 unsigned int count = 0;
512
513 /* Jump to the end of the lco identifier */
514 ptr = strstr(options, ":");
515 if (!ptr)
516 return NULL;
517
518 /* Walk backwards until the pointer points to the beginning of the
519 * lco identifier. We know that we stand at the beginning when we
520 * are either at the beginning of the memory or see a space or
521 * comma. (this is tolerant, it will accept a:10, b:11 as well as
522 * a:10,b:11) */
523 while (1) {
524 /* Endless loop protection */
525 if (count > 10000)
526 return NULL;
527 else if (ptr < options || *ptr == ' ' || *ptr == ',') {
528 ptr++;
529 break;
530 }
531 ptr--;
532 count++;
533 }
534
535 /* Check if we got any result */
536 if (*ptr == ':')
537 return NULL;
538
539 return ptr;
540}
541
542/*! Check the LCO option. This function checks for multiple appearence of LCO
543 * options, which is illegal
544 * \param[in] ctx talloc context
545 * \param[in] lco string
546 * \returns 0 on success, -1 on failure */
547int check_local_cx_options(void *ctx, const char *options)
548{
549 int i;
550 char *options_copy;
551 char *lco_identifier;
552 char *lco_identifier_end;
553 char *next_lco_identifier;
554
555 char **lco_seen;
556 unsigned int lco_seen_n = 0;
557
558 if (!options)
559 return -1;
560
561 lco_seen =
562 (char **)talloc_zero_size(ctx, strlen(options) * sizeof(char *));
563 options_copy = talloc_strdup(ctx, options);
564 lco_identifier = options_copy;
565
566 do {
567 /* Move the lco_identifier pointer to the beginning of the
568 * current lco option identifier */
569 lco_identifier = get_lco_identifier(lco_identifier);
570 if (!lco_identifier)
571 goto error;
572
573 /* Look ahead to the next LCO option early, since we
574 * will parse destructively */
575 next_lco_identifier = strstr(lco_identifier + 1, ",");
576
577 /* Pinch off the end of the lco field identifier name
578 * and see if we still got something, also check if
579 * there is some value after the colon. */
580 lco_identifier_end = strstr(lco_identifier, ":");
581 if (!lco_identifier_end)
582 goto error;
583 if (*(lco_identifier_end + 1) == ' '
584 || *(lco_identifier_end + 1) == ','
585 || *(lco_identifier_end + 1) == '\0')
586 goto error;
587 *lco_identifier_end = '\0';
588 if (strlen(lco_identifier) == 0)
589 goto error;
590
591 /* Check if we have already seen the current field identifier
592 * before. If yes, we must bail, an LCO must only appear once
593 * in the LCO string */
594 for (i = 0; i < lco_seen_n; i++) {
Pau Espin Pedrol7eb6f2c2019-06-26 13:00:52 +0200595 if (strcasecmp(lco_seen[i], lco_identifier) == 0)
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200596 goto error;
597 }
598 lco_seen[lco_seen_n] = lco_identifier;
599 lco_seen_n++;
600
601 /* The first identifier must always be found at the beginnning
602 * of the LCO string */
603 if (lco_seen[0] != options_copy)
604 goto error;
605
606 /* Go to the next lco option */
607 lco_identifier = next_lco_identifier;
608 } while (lco_identifier);
609
610 talloc_free(lco_seen);
611 talloc_free(options_copy);
612 return 0;
613error:
614 talloc_free(lco_seen);
615 talloc_free(options_copy);
616 return -1;
617}
618
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200619/* Set the LCO from a string (see RFC 3435).
Harald Welte1d1b98f2017-12-25 10:03:40 +0100620 * The string is stored in the 'string' field. A NULL string is handled exactly
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200621 * like an empty string, the 'string' field is never NULL after this function
622 * has been called. */
Philipp Maiera390d0b2018-01-31 17:30:19 +0100623static int set_local_cx_options(void *ctx, struct mgcp_lco *lco,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200624 const char *options)
625{
Pau Espin Pedrolfe9a1fe2019-06-25 16:59:15 +0200626 char *lco_id;
Philipp Maier8dbc9ed2018-10-26 14:50:25 +0200627 char codec[17];
Pau Espin Pedrol83fd8a52019-06-26 12:55:26 +0200628 int len;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200629
Philipp Maier604410c2018-06-06 10:02:16 +0200630 if (!options)
631 return 0;
632 if (strlen(options) == 0)
633 return 0;
634
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200635 /* Make sure the encoding of the LCO is consistant before we proceed */
636 if (check_local_cx_options(ctx, options) != 0) {
637 LOGP(DLMGCP, LOGL_ERROR,
638 "local CX options: Internal inconsistency in Local Connection Options!\n");
639 return 524;
640 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200641
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200642 talloc_free(lco->string);
643 lco->string = talloc_strdup(ctx, options);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200644
Pau Espin Pedrolfe9a1fe2019-06-25 16:59:15 +0200645 lco_id = lco->string;
646 while ((lco_id = get_lco_identifier(lco_id))) {
647 switch (tolower(lco_id[0])) {
648 case 'p':
649 if (sscanf(lco_id + 1, ":%d-%d",
650 &lco->pkt_period_min, &lco->pkt_period_max) == 1)
651 lco->pkt_period_max = lco->pkt_period_min;
652 break;
653 case 'a':
654 /* FIXME: LCO also supports the negotiation of more then one codec.
655 * (e.g. a:PCMU;G726-32) But this implementation only supports a single
656 * codec only. */
657 if (sscanf(lco_id + 1, ":%16[^,]", codec) == 1) {
658 talloc_free(lco->codec);
Pau Espin Pedrol83fd8a52019-06-26 12:55:26 +0200659 /* MGCP header is case insensive, and we'll need
660 codec in uppercase when using it later: */
661 len = strlen(codec);
662 lco->codec = talloc_size(ctx, len + 1);
663 osmo_str_toupper_buf(lco->codec, len + 1, codec);
Pau Espin Pedrolfe9a1fe2019-06-25 16:59:15 +0200664 }
665 break;
666 default:
667 LOGP(DLMGCP, LOGL_NOTICE,
668 "LCO: unhandled option: '%c'/%d in \"%s\"\n",
669 *lco_id, *lco_id, lco->string);
670 break;
671 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200672
Pau Espin Pedrolfe9a1fe2019-06-25 16:59:15 +0200673 lco_id = strchr(lco_id, ',');
674 if (!lco_id)
675 break;
Philipp Maier604410c2018-06-06 10:02:16 +0200676 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200677
678 LOGP(DLMGCP, LOGL_DEBUG,
679 "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n",
680 lco->pkt_period_max, lco->codec);
Philipp Maiera390d0b2018-01-31 17:30:19 +0100681
682 /* Check if the packetization fits the 20ms raster */
683 if (lco->pkt_period_min % 20 && lco->pkt_period_max % 20) {
684 LOGP(DLMGCP, LOGL_ERROR,
685 "local CX options: packetization interval is not a multiple of 20ms!\n");
686 return 535;
687 }
688
689 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200690}
691
692void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
693 struct mgcp_rtp_end *rtp)
694{
695 struct mgcp_trunk_config *tcfg = endp->tcfg;
696
697 int patch_ssrc = expect_ssrc_change && tcfg->force_constant_ssrc;
698
699 rtp->force_aligned_timing = tcfg->force_aligned_timing;
700 rtp->force_constant_ssrc = patch_ssrc ? 1 : 0;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100701 rtp->rfc5993_hr_convert = tcfg->rfc5993_hr_convert;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200702
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200703 LOGPENDP(endp, DLMGCP, LOGL_DEBUG,
704 "Configuring RTP endpoint: local port %d%s%s\n",
705 ntohs(rtp->rtp_port),
706 rtp->force_aligned_timing ? ", force constant timing" : "",
707 rtp->force_constant_ssrc ? ", force constant ssrc" : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200708}
709
710uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
711 struct mgcp_rtp_end *rtp)
712{
713 int f = 0;
714
715 /* Get the number of frames per channel and packet */
716 if (rtp->frames_per_packet)
717 f = rtp->frames_per_packet;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200718 else if (rtp->packet_duration_ms && rtp->codec->frame_duration_num) {
719 int den = 1000 * rtp->codec->frame_duration_num;
720 f = (rtp->packet_duration_ms * rtp->codec->frame_duration_den +
Philipp Maier87bd9be2017-08-22 16:35:41 +0200721 den / 2)
722 / den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200723 }
724
Philipp Maierbc0346e2018-06-07 09:52:16 +0200725 return rtp->codec->rate * f * rtp->codec->frame_duration_num /
726 rtp->codec->frame_duration_den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200727}
728
Pau Espin Pedrol9fb8ddf2019-05-08 15:35:36 +0200729/*! Initializes osmux socket if not yet initialized. Parses Osmux CID from MGCP line.
730 * \param[in] endp Endpoint willing to initialize osmux
731 * \param[in] line Line X-Osmux from MGCP header msg to parse
732 * \returns OSMUX CID, -1 for wildcard, -2 on parse error, -3 on osmux initalize error
733 */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200734static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line)
735{
736 if (!endp->cfg->osmux_init) {
737 if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200738 LOGPENDP(endp, DLMGCP, LOGL_ERROR, "Cannot init OSMUX\n");
Pau Espin Pedrol9fb8ddf2019-05-08 15:35:36 +0200739 return -3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200740 }
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200741 LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200742 }
743
744 return mgcp_parse_osmux_cid(line);
745}
746
Philipp Maierbc0346e2018-06-07 09:52:16 +0200747/* Process codec information contained in CRCX/MDCX */
748static int handle_codec_info(struct mgcp_conn_rtp *conn,
749 struct mgcp_parse_data *p, int have_sdp, bool crcx)
750{
751 struct mgcp_endpoint *endp = p->endp;
752 int rc;
753 char *cmd;
754
755 if (crcx)
756 cmd = "CRCX";
757 else
758 cmd = "MDCX";
759
760 /* Collect codec information */
761 if (have_sdp) {
762 /* If we have SDP, we ignore the local connection options and
763 * use only the SDP information. */
764 mgcp_codec_reset_all(conn);
765 rc = mgcp_parse_sdp_data(endp, conn, p);
766 if (rc != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200767 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
768 "%s: sdp not parseable\n", cmd);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200769
770 /* See also RFC 3661: Protocol error */
771 return 510;
772 }
773 } else if (endp->local_options.codec) {
774 /* When no SDP is available, we use the codec information from
775 * the local connection options (if present) */
776 mgcp_codec_reset_all(conn);
Philipp Maier228e5912019-03-05 13:56:59 +0100777 rc = mgcp_codec_add(conn, PTYPE_UNDEFINED, endp->local_options.codec, NULL);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200778 if (rc != 0)
779 goto error;
780 }
781
782 /* Make sure we always set a sane default codec */
783 if (conn->end.codecs_assigned == 0) {
784 /* When SDP and/or LCO did not supply any codec information,
785 * than it makes sense to pick a sane default: (payload-type 0,
786 * PCMU), see also: OS#2658 */
787 mgcp_codec_reset_all(conn);
Philipp Maier228e5912019-03-05 13:56:59 +0100788 rc = mgcp_codec_add(conn, 0, NULL, NULL);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200789 if (rc != 0)
790 goto error;
791 }
792
793 /* Make codec decision */
794 if (mgcp_codec_decide(conn) != 0)
795 goto error;
796
797 return 0;
798
799error:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200800 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
801 "%s: codec negotiation failure\n", cmd);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200802
803 /* See also RFC 3661: Codec negotiation failure */
804 return 534;
805}
806
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200807static bool parse_x_osmo_ign(struct mgcp_endpoint *endp, char *line)
808{
809 char *saveptr = NULL;
810
811 if (strncmp(line, MGCP_X_OSMO_IGN_HEADER, strlen(MGCP_X_OSMO_IGN_HEADER)))
812 return false;
813 line += strlen(MGCP_X_OSMO_IGN_HEADER);
814
815 while (1) {
816 char *token = strtok_r(line, " ", &saveptr);
817 line = NULL;
818 if (!token)
819 break;
820
Pau Espin Pedrol6e26c702019-06-26 13:09:39 +0200821 if (!strcasecmp(token, "C"))
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200822 endp->x_osmo_ign |= MGCP_X_OSMO_IGN_CALLID;
823 else
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200824 LOGPENDP(endp, DLMGCP, LOGL_ERROR, "received unknown X-Osmo-IGN item '%s'\n", token);
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200825 }
826
827 return true;
828}
829
Philipp Maier87bd9be2017-08-22 16:35:41 +0200830/* CRCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200831static struct msgb *handle_create_con(struct mgcp_parse_data *p)
832{
Stefan Sperling1e174872018-10-25 18:36:10 +0200833 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200834 struct mgcp_endpoint *endp = p->endp;
Stefan Sperling9270e912018-10-29 14:10:00 +0100835 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_crcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200836 int error_code = 400;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200837 const char *local_options = NULL;
838 const char *callid = NULL;
839 const char *mode = NULL;
840 char *line;
Pau Espin Pedrol2b896172019-04-24 13:47:23 +0200841 int have_sdp = 0, osmux_cid = -2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200842 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100843 struct mgcp_conn *_conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200844 char conn_name[512];
Philipp Maiera390d0b2018-01-31 17:30:19 +0100845 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200846
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200847 LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200848
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200849 /* parse CallID C: and LocalParameters L: */
850 for_each_line(line, p->save) {
851 if (!mgcp_check_param(endp, line))
852 continue;
853
Pau Espin Pedrol0c6c3c12019-06-25 17:18:12 +0200854 switch (toupper(line[0])) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200855 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200856 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200857 break;
858 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200859 callid = (const char *)line + 3;
860 break;
861 case 'I':
Philipp Maierffd75e42017-11-22 11:44:50 +0100862 /* It is illegal to send a connection identifier
863 * together with a CRCX, the MGW will assign the
864 * connection identifier by itself on CRCX */
Stefan Sperling9270e912018-10-29 14:10:00 +0100865 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_BAD_ACTION]);
Philipp Maierffd75e42017-11-22 11:44:50 +0100866 return create_err_response(NULL, 523, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200867 break;
868 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200869 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200870 break;
871 case 'X':
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200872 if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200873 /* If osmux is disabled, just skip setting it up */
874 if (!p->endp->cfg->osmux)
875 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200876 osmux_cid = mgcp_osmux_setup(endp, line);
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200877 break;
878 }
879
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200880 if (parse_x_osmo_ign(endp, line))
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200881 break;
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200882
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200883 /* Ignore unknown X-headers */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200884 break;
885 case '\0':
886 have_sdp = 1;
887 goto mgcp_header_done;
888 default:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200889 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
890 "CRCX: unhandled option: '%c'/%d\n", *line, *line);
Stefan Sperling9270e912018-10-29 14:10:00 +0100891 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +0100892 return create_err_response(NULL, 539, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200893 break;
894 }
895 }
896
897mgcp_header_done:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200898 /* Check parameters */
899 if (!callid) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200900 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
901 "CRCX: insufficient parameters, missing callid\n");
Stefan Sperling9270e912018-10-29 14:10:00 +0100902 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_MISSING_CALLID]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100903 return create_err_response(endp, 516, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200904 }
905
Philipp Maier87bd9be2017-08-22 16:35:41 +0200906 if (!mode) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200907 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
908 "CRCX: insufficient parameters, missing mode\n");
Stefan Sperling9270e912018-10-29 14:10:00 +0100909 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_MODE]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100910 return create_err_response(endp, 517, "CRCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200911 }
912
Philipp Maier87bd9be2017-08-22 16:35:41 +0200913 /* Check if we are able to accept the creation of another connection */
914 if (llist_count(&endp->conns) >= endp->type->max_conns) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200915 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
916 "CRCX: endpoint full, max. %i connections allowed!\n",
917 endp->type->max_conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200918 if (tcfg->force_realloc) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200919 /* There is no more room for a connection, make some
920 * room by blindly tossing the oldest of the two two
921 * connections */
922 mgcp_conn_free_oldest(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200923 } else {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200924 /* There is no more room for a connection, leave
925 * everything as it is and return with an error */
Stefan Sperling9270e912018-10-29 14:10:00 +0100926 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_LIMIT_EXCEEDED]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100927 return create_err_response(endp, 540, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200928 }
929 }
930
Philipp Maier87bd9be2017-08-22 16:35:41 +0200931 /* Check if this endpoint already serves a call, if so, check if the
932 * callids match up so that we are sure that this is our call */
933 if (endp->callid && mgcp_verify_call_id(endp, callid)) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200934 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
935 "CRCX: already seized by other call (%s)\n",
936 endp->callid);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200937 if (tcfg->force_realloc)
938 /* This is not our call, toss everything by releasing
939 * the entire endpoint. (rude!) */
Philipp Maier1355d7e2018-02-01 14:30:06 +0100940 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200941 else {
942 /* This is not our call, leave everything as it is and
943 * return with an error. */
Stefan Sperling9270e912018-10-29 14:10:00 +0100944 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_UNKNOWN_CALLID]);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200945 return create_err_response(endp, 400, "CRCX", p->trans);
946 }
947 }
948
949 /* Set the callid, creation of another connection will only be possible
Harald Welte1d1b98f2017-12-25 10:03:40 +0100950 * when the callid matches up. (Connections are distinguished by their
Philipp Maier87bd9be2017-08-22 16:35:41 +0200951 * connection ids) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200952 endp->callid = talloc_strdup(tcfg->endpoints, callid);
953
Philipp Maierffd75e42017-11-22 11:44:50 +0100954 snprintf(conn_name, sizeof(conn_name), "%s", callid);
Pau Espin Pedrol843d9032019-09-19 17:44:14 +0200955 _conn = mgcp_conn_alloc(tcfg->endpoints, endp, MGCP_CONN_TYPE_RTP, conn_name);
Philipp Maierffd75e42017-11-22 11:44:50 +0100956 if (!_conn) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200957 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
958 "CRCX: unable to allocate RTP connection\n");
Stefan Sperling9270e912018-10-29 14:10:00 +0100959 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_ALLOC_CONN]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200960 goto error2;
961
Philipp Maier87bd9be2017-08-22 16:35:41 +0200962 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100963 conn = mgcp_conn_get_rtp(endp, _conn->id);
964 OSMO_ASSERT(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200965
966 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
967 error_code = 517;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100968 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_MODE]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200969 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200970 }
971
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200972 /* Annotate Osmux circuit ID and set it to negotiating state until this
Philipp Maier87bd9be2017-08-22 16:35:41 +0200973 * is fully set up from the dummy load. */
974 conn->osmux.state = OSMUX_STATE_DISABLED;
Pau Espin Pedrol2b896172019-04-24 13:47:23 +0200975 if (osmux_cid >= -1) { /* -1 is wilcard, alloc next avail CID */
Pau Espin Pedrol14f8a082019-05-13 13:10:06 +0200976 conn->osmux.state = OSMUX_STATE_ACTIVATING;
Pau Espin Pedrol2b896172019-04-24 13:47:23 +0200977 if (conn_osmux_allocate_cid(conn, osmux_cid) == -1) {
978 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_OSMUX]);
979 goto error2;
980 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200981 } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200982 LOGPCONN(_conn, DLMGCP, LOGL_ERROR,
983 "CRCX: osmux only and no osmux offered\n");
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100984 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_OSMUX]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200985 goto error2;
986 }
987
Philipp Maierbc0346e2018-06-07 09:52:16 +0200988 /* Set local connection options, if present */
989 if (local_options) {
990 rc = set_local_cx_options(endp->tcfg->endpoints,
991 &endp->local_options, local_options);
992 if (rc != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200993 LOGPCONN(_conn, DLMGCP, LOGL_ERROR,
994 "CRCX: inavlid local connection options!\n");
Philipp Maierbc0346e2018-06-07 09:52:16 +0200995 error_code = rc;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100996 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS]);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200997 goto error2;
998 }
999 }
1000
1001 /* Handle codec information and decide for a suitable codec */
1002 rc = handle_codec_info(conn, p, have_sdp, true);
1003 mgcp_codec_summary(conn);
1004 if (rc) {
1005 error_code = rc;
Stefan Sperlinga714abf2018-10-29 14:19:54 +01001006 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_CODEC_NEGOTIATION]);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001007 goto error2;
1008 }
1009
Philipp Maier87bd9be2017-08-22 16:35:41 +02001010 conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints,
1011 tcfg->audio_fmtp_extra);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001012
Philipp Maier87bd9be2017-08-22 16:35:41 +02001013 if (p->cfg->force_ptime) {
1014 conn->end.packet_duration_ms = p->cfg->force_ptime;
1015 conn->end.force_output_ptime = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001016 }
1017
Philipp Maier1cb1e382017-11-02 17:16:04 +01001018 mgcp_rtp_end_config(endp, 0, &conn->end);
1019
Philipp Maierc3cc6542018-02-02 12:58:42 +01001020 /* check connection mode setting */
1021 if (conn->conn->mode != MGCP_CONN_LOOPBACK
1022 && conn->conn->mode != MGCP_CONN_RECV_ONLY
1023 && conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001024 LOGPCONN(_conn, DLMGCP, LOGL_ERROR,
1025 "CRCX: selected connection mode type requires an opposite end!\n");
Philipp Maierc3cc6542018-02-02 12:58:42 +01001026 error_code = 527;
Stefan Sperling9270e912018-10-29 14:10:00 +01001027 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC]);
Philipp Maierc3cc6542018-02-02 12:58:42 +01001028 goto error2;
1029 }
1030
Philipp Maier1cb1e382017-11-02 17:16:04 +01001031 if (allocate_port(endp, conn) != 0) {
Stefan Sperlinga714abf2018-10-29 14:19:54 +01001032 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_BIND_PORT]);
Philipp Maier1cb1e382017-11-02 17:16:04 +01001033 goto error2;
1034 }
1035
Philipp Maier87bd9be2017-08-22 16:35:41 +02001036 if (setup_rtp_processing(endp, conn) != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001037 LOGPCONN(_conn, DLMGCP, LOGL_ERROR,
1038 "CRCX: could not start RTP processing!\n");
Stefan Sperling9270e912018-10-29 14:10:00 +01001039 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_START_RTP]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001040 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001041 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001042
1043 /* policy CB */
1044 if (p->cfg->policy_cb) {
1045 int rc;
1046 rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001047 MGCP_ENDP_CRCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001048 switch (rc) {
1049 case MGCP_POLICY_REJECT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001050 LOGPCONN(_conn, DLMGCP, LOGL_NOTICE,
1051 "CRCX: CRCX rejected by policy\n");
Philipp Maier1355d7e2018-02-01 14:30:06 +01001052 mgcp_endp_release(endp);
Stefan Sperling9270e912018-10-29 14:10:00 +01001053 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001054 return create_err_response(endp, 400, "CRCX", p->trans);
1055 break;
1056 case MGCP_POLICY_DEFER:
1057 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001058 return NULL;
1059 break;
1060 case MGCP_POLICY_CONT:
1061 /* just continue */
1062 break;
1063 }
1064 }
1065
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001066 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG,
1067 "CRCX: Creating connection: port: %u\n", conn->end.local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001068 if (p->cfg->change_cb)
1069 p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX);
1070
Philipp Maiere726d4f2017-11-01 10:41:34 +01001071 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
1072 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001073 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
Philipp Maiere726d4f2017-11-01 10:41:34 +01001074 && tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001075 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001076
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001077 LOGPCONN(_conn, DLMGCP, LOGL_NOTICE,
1078 "CRCX: connection successfully created\n");
Stefan Sperling9270e912018-10-29 14:10:00 +01001079 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_SUCCESS]);
Philipp Maier55295f72018-01-15 14:00:28 +01001080 return create_response_with_sdp(endp, conn, "CRCX", p->trans, true);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001081error2:
Philipp Maier1355d7e2018-02-01 14:30:06 +01001082 mgcp_endp_release(endp);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001083 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1084 "CRCX: unable to create connection\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001085 return create_err_response(endp, error_code, "CRCX", p->trans);
1086}
1087
Philipp Maier87bd9be2017-08-22 16:35:41 +02001088/* MDCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001089static struct msgb *handle_modify_con(struct mgcp_parse_data *p)
1090{
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001091 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001092 struct mgcp_endpoint *endp = p->endp;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001093 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_mdcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001094 int error_code = 500;
1095 int silent = 0;
1096 int have_sdp = 0;
1097 char *line;
1098 const char *local_options = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001099 const char *mode = NULL;
1100 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier01d24a32017-11-21 17:26:09 +01001101 const char *conn_id = NULL;
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001102 int osmux_cid = -2;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001103 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001104
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001105 LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001106
Philipp Maier5656fbf2018-02-02 14:41:58 +01001107 /* Prohibit wildcarded requests */
1108 if (endp->wildcarded_req) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001109 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1110 "MDCX: wildcarded endpoint names not supported.\n");
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001111 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_WILDCARD]);
Philipp Maier5656fbf2018-02-02 14:41:58 +01001112 return create_err_response(endp, 507, "MDCX", p->trans);
1113 }
1114
Philipp Maier87bd9be2017-08-22 16:35:41 +02001115 if (llist_count(&endp->conns) <= 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001116 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1117 "MDCX: endpoint is not holding a connection.\n");
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001118 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_CONN]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001119 return create_err_response(endp, 400, "MDCX", p->trans);
1120 }
1121
1122 for_each_line(line, p->save) {
1123 if (!mgcp_check_param(endp, line))
1124 continue;
1125
Pau Espin Pedrol0c6c3c12019-06-25 17:18:12 +02001126 switch (toupper(line[0])) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001127 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +01001128 if (mgcp_verify_call_id(endp, line + 3) != 0) {
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001129 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CALLID]);
Harald Weltee35eeae2017-12-28 13:47:37 +01001130 error_code = 516;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001131 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001132 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001133 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001134 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001135 conn_id = (const char *)line + 3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001136 if ((error_code = mgcp_verify_ci(endp, conn_id))) {
1137 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CONNID]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001138 goto error3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001139 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001140 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001141 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001142 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001143 break;
1144 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001145 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001146 break;
1147 case 'Z':
Pau Espin Pedrol9b508f62019-06-26 13:11:22 +02001148 silent = strcasecmp("noanswer", line + 3) == 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001149 break;
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001150 case 'X':
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +02001151 if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001152 /* If osmux is disabled, just skip setting it up */
1153 if (!p->endp->cfg->osmux)
1154 break;
1155 osmux_cid = mgcp_osmux_setup(endp, line);
1156 break;
1157 }
1158 /* Ignore unknown X-headers */
1159 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001160 case '\0':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001161 have_sdp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001162 goto mgcp_header_done;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001163 break;
1164 default:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001165 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1166 "MDCX: Unhandled MGCP option: '%c'/%d\n",
1167 line[0], line[0]);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001168 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001169 return create_err_response(NULL, 539, "MDCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001170 break;
1171 }
1172 }
1173
Philipp Maier87bd9be2017-08-22 16:35:41 +02001174mgcp_header_done:
Philipp Maier01d24a32017-11-21 17:26:09 +01001175 if (!conn_id) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001176 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1177 "MDCX: insufficient parameters, missing ci (connectionIdentifier)\n");
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001178 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_CONNID]);
Harald Weltee35eeae2017-12-28 13:47:37 +01001179 return create_err_response(endp, 515, "MDCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001180 }
1181
1182 conn = mgcp_conn_get_rtp(endp, conn_id);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001183 if (!conn) {
1184 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_CONN_NOT_FOUND]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001185 return create_err_response(endp, 400, "MDCX", p->trans);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001186 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001187
Oliver Smithe36b7752019-01-22 16:31:36 +01001188 mgcp_conn_watchdog_kick(conn->conn);
1189
Philipp Maier87bd9be2017-08-22 16:35:41 +02001190 if (mode) {
1191 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001192 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_MODE]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001193 error_code = 517;
1194 goto error3;
1195 }
1196 } else
Pau Espin Pedrol209eb9f2019-04-24 12:03:04 +02001197 conn->conn->mode = conn->conn->mode_orig;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001198
Philipp Maierbc0346e2018-06-07 09:52:16 +02001199 /* Set local connection options, if present */
1200 if (local_options) {
1201 rc = set_local_cx_options(endp->tcfg->endpoints,
1202 &endp->local_options, local_options);
1203 if (rc != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001204 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1205 "MDCX: invalid local connection options!\n");
Philipp Maierbc0346e2018-06-07 09:52:16 +02001206 error_code = rc;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001207 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS]);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001208 goto error3;
1209 }
1210 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001211
Philipp Maierbc0346e2018-06-07 09:52:16 +02001212 /* Handle codec information and decide for a suitable codec */
1213 rc = handle_codec_info(conn, p, have_sdp, false);
1214 mgcp_codec_summary(conn);
1215 if (rc) {
Philipp Maieraf07f662018-02-02 11:34:02 +01001216 error_code = rc;
1217 goto error3;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001218 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001219
Philipp Maierc3cc6542018-02-02 12:58:42 +01001220 /* check connection mode setting */
1221 if (conn->conn->mode != MGCP_CONN_LOOPBACK
1222 && conn->conn->mode != MGCP_CONN_RECV_ONLY
1223 && conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001224 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1225 "MDCX: selected connection mode type requires an opposite end!\n");
Philipp Maierc3cc6542018-02-02 12:58:42 +01001226 error_code = 527;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001227 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC]);
Philipp Maierc3cc6542018-02-02 12:58:42 +01001228 goto error3;
1229 }
1230
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001231 if (mgcp_conn_rtp_is_osmux(conn)) {
1232 OSMO_ASSERT(conn->osmux.cid_allocated);
1233 if (osmux_cid < -1) {
1234 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1235 "MDCX: Failed to parse Osmux CID!\n");
1236 goto error3;
1237 } else if (osmux_cid == -1) {
1238 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1239 "MDCX: wilcard in MDCX is not supported!\n");
1240 goto error3;
1241 } else if (osmux_cid != (int) conn->osmux.cid) {
1242 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1243 "MDCX: changing already allocated CID is not supported!\n");
1244 goto error3;
1245 }
1246 /* TODO: In the future (when we have recvCID!=sendCID), we need to
1247 tell Osmux code that osmux_cid is to be used as sendCID for
1248 that conn. */
1249 }
Philipp Maierbc0346e2018-06-07 09:52:16 +02001250
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001251 if (setup_rtp_processing(endp, conn) != 0) {
1252 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_START_RTP]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001253 goto error3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001254 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001255
Philipp Maier87bd9be2017-08-22 16:35:41 +02001256
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001257 /* policy CB */
1258 if (p->cfg->policy_cb) {
1259 int rc;
1260 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001261 MGCP_ENDP_MDCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001262 switch (rc) {
1263 case MGCP_POLICY_REJECT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001264 LOGPCONN(conn->conn, DLMGCP, LOGL_NOTICE,
1265 "MDCX: rejected by policy\n");
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001266 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001267 if (silent)
1268 goto out_silent;
1269 return create_err_response(endp, 400, "MDCX", p->trans);
1270 break;
1271 case MGCP_POLICY_DEFER:
1272 /* stop processing */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001273 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG,
1274 "MDCX: deferred by policy\n");
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001275 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_DEFERRED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001276 return NULL;
1277 break;
1278 case MGCP_POLICY_CONT:
1279 /* just continue */
1280 break;
1281 }
1282 }
1283
Philipp Maier87bd9be2017-08-22 16:35:41 +02001284 mgcp_rtp_end_config(endp, 1, &conn->end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001285
1286 /* modify */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001287 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG,
1288 "MDCX: modified conn:%s\n", mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001289 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001290 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1291 MGCP_ENDP_MDCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001292
Philipp Maiere726d4f2017-11-01 10:41:34 +01001293 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
1294 OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1295 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
1296 && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001297 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001298
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001299 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_SUCCESS]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001300 if (silent)
1301 goto out_silent;
1302
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001303 LOGPCONN(conn->conn, DLMGCP, LOGL_NOTICE,
1304 "MDCX: connection successfully modified\n");
Philipp Maier55295f72018-01-15 14:00:28 +01001305 return create_response_with_sdp(endp, conn, "MDCX", p->trans, false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001306error3:
1307 return create_err_response(endp, error_code, "MDCX", p->trans);
1308
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001309out_silent:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001310 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "MDCX: silent exit\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001311 return NULL;
1312}
1313
Philipp Maier87bd9be2017-08-22 16:35:41 +02001314/* DLCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001315static struct msgb *handle_delete_con(struct mgcp_parse_data *p)
1316{
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001317 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001318 struct mgcp_endpoint *endp = p->endp;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001319 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_dlcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001320 int error_code = 400;
1321 int silent = 0;
1322 char *line;
1323 char stats[1048];
Philipp Maier01d24a32017-11-21 17:26:09 +01001324 const char *conn_id = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001325 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001326
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001327 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1328 "DLCX: deleting connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001329
Philipp Maier5656fbf2018-02-02 14:41:58 +01001330 /* Prohibit wildcarded requests */
1331 if (endp->wildcarded_req) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001332 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1333 "DLCX: wildcarded endpoint names not supported.\n");
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001334 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_WILDCARD]);
Philipp Maier5656fbf2018-02-02 14:41:58 +01001335 return create_err_response(endp, 507, "DLCX", p->trans);
1336 }
1337
Philipp Maier87bd9be2017-08-22 16:35:41 +02001338 if (llist_count(&endp->conns) <= 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001339 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1340 "DLCX: endpoint is not holding a connection.\n");
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001341 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_NO_CONN]);
Harald Weltee35eeae2017-12-28 13:47:37 +01001342 return create_err_response(endp, 515, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001343 }
1344
1345 for_each_line(line, p->save) {
1346 if (!mgcp_check_param(endp, line))
1347 continue;
1348
Pau Espin Pedrol0c6c3c12019-06-25 17:18:12 +02001349 switch (toupper(line[0])) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001350 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +01001351 if (mgcp_verify_call_id(endp, line + 3) != 0) {
1352 error_code = 516;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001353 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CALLID]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001354 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001355 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001356 break;
1357 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001358 conn_id = (const char *)line + 3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001359 if ((error_code = mgcp_verify_ci(endp, conn_id))) {
1360 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CONNID]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001361 goto error3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001362 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001363 break;
1364 case 'Z':
Pau Espin Pedrol9b508f62019-06-26 13:11:22 +02001365 silent = strcasecmp("noanswer", line + 3) == 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001366 break;
1367 default:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001368 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1369 "DLCX: Unhandled MGCP option: '%c'/%d\n",
1370 line[0], line[0]);
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001371 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001372 return create_err_response(NULL, 539, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001373 break;
1374 }
1375 }
1376
1377 /* policy CB */
1378 if (p->cfg->policy_cb) {
1379 int rc;
1380 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001381 MGCP_ENDP_DLCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001382 switch (rc) {
1383 case MGCP_POLICY_REJECT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001384 LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "DLCX: rejected by policy\n");
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001385 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001386 if (silent)
1387 goto out_silent;
1388 return create_err_response(endp, 400, "DLCX", p->trans);
1389 break;
1390 case MGCP_POLICY_DEFER:
1391 /* stop processing */
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001392 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_DEFERRED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001393 return NULL;
1394 break;
1395 case MGCP_POLICY_CONT:
1396 /* just continue */
1397 break;
1398 }
1399 }
1400
Philipp Maierf4c0e372017-10-11 16:06:45 +02001401 /* When no connection id is supplied, we will interpret this as a
1402 * wildcarded DLCX and drop all connections at once. (See also
1403 * RFC3435 Section F.7) */
Philipp Maier01d24a32017-11-21 17:26:09 +01001404 if (!conn_id) {
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001405 int num_conns = llist_count(&endp->conns);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001406 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1407 "DLCX: missing ci (connectionIdentifier), will remove all connections (%d total) at once\n",
1408 num_conns);
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001409
1410 if (num_conns > 0)
1411 rate_ctr_add(&rate_ctrs->ctr[MGCP_DLCX_SUCCESS], num_conns);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001412
Philipp Maier1355d7e2018-02-01 14:30:06 +01001413 mgcp_endp_release(endp);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001414
1415 /* Note: In this case we do not return any statistics,
1416 * as we assume that the client is not interested in
1417 * this case. */
1418 return create_ok_response(endp, 200, "DLCX", p->trans);
1419 }
1420
Philipp Maierf4c0e372017-10-11 16:06:45 +02001421 /* Find the connection */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001422 conn = mgcp_conn_get_rtp(endp, conn_id);
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001423 if (!conn) {
1424 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CONNID]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001425 goto error3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001426 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001427 /* save the statistics of the current connection */
1428 mgcp_format_stats(stats, sizeof(stats), conn->conn);
1429
1430 /* delete connection */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001431 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG, "DLCX: deleting conn:%s\n",
1432 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001433 mgcp_conn_free(endp, conn_id);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001434 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1435 "DLCX: connection successfully deleted\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001436
1437 /* When all connections are closed, the endpoint will be released
1438 * in order to be ready to be used by another call. */
1439 if (llist_count(&endp->conns) <= 0) {
Philipp Maier1355d7e2018-02-01 14:30:06 +01001440 mgcp_endp_release(endp);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001441 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "DLCX: endpoint released\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001442 }
1443
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001444 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001445 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1446 MGCP_ENDP_DLCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001447
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001448 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_SUCCESS]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001449 if (silent)
1450 goto out_silent;
1451 return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats);
1452
1453error3:
1454 return create_err_response(endp, error_code, "DLCX", p->trans);
1455
1456out_silent:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001457 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "DLCX: silent exit\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001458 return NULL;
1459}
1460
Philipp Maier87bd9be2017-08-22 16:35:41 +02001461/* RSIP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001462static struct msgb *handle_rsip(struct mgcp_parse_data *p)
1463{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001464 /* TODO: Also implement the resetting of a specific endpoint
1465 * to make mgcp_send_reset_ep() work. Currently this will call
1466 * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1
1467 * to make read_call_agent() reset all endpoints when called
1468 * next time. In order to selectively reset endpoints some
1469 * mechanism to distinguish which endpoint shall be resetted
1470 * is needed */
1471
1472 LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n");
1473
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001474 if (p->cfg->reset_cb)
1475 p->cfg->reset_cb(p->endp->tcfg);
1476 return NULL;
1477}
1478
1479static char extract_tone(const char *line)
1480{
1481 const char *str = strstr(line, "D/");
1482 if (!str)
1483 return CHAR_MAX;
1484
1485 return str[2];
1486}
1487
Philipp Maier87bd9be2017-08-22 16:35:41 +02001488/* This can request like DTMF detection and forward, fax detection... it
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001489 * can also request when the notification should be send and such. We don't
Philipp Maier87bd9be2017-08-22 16:35:41 +02001490 * do this right now. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001491static struct msgb *handle_noti_req(struct mgcp_parse_data *p)
1492{
1493 int res = 0;
1494 char *line;
1495 char tone = CHAR_MAX;
1496
Philipp Maier87bd9be2017-08-22 16:35:41 +02001497 LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n");
1498
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001499 for_each_line(line, p->save) {
Pau Espin Pedrol0c6c3c12019-06-25 17:18:12 +02001500 switch (toupper(line[0])) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001501 case 'S':
1502 tone = extract_tone(line);
1503 break;
1504 }
1505 }
1506
1507 /* we didn't see a signal request with a tone */
1508 if (tone == CHAR_MAX)
1509 return create_ok_response(p->endp, 200, "RQNT", p->trans);
1510
1511 if (p->cfg->rqnt_cb)
1512 res = p->cfg->rqnt_cb(p->endp, tone);
1513
1514 return res == 0 ?
Philipp Maier87bd9be2017-08-22 16:35:41 +02001515 create_ok_response(p->endp, 200, "RQNT", p->trans) :
1516 create_err_response(p->endp, res, "RQNT", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001517}
1518
Philipp Maier87bd9be2017-08-22 16:35:41 +02001519/* Connection keepalive timer, will take care that dummy packets are send
Harald Welte1d1b98f2017-12-25 10:03:40 +01001520 * regularly, so that NAT connections stay open */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001521static void mgcp_keepalive_timer_cb(void *_tcfg)
1522{
1523 struct mgcp_trunk_config *tcfg = _tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001524 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001525 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001526
Philipp Maiere726d4f2017-11-01 10:41:34 +01001527 LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001528 tcfg->trunk_nr);
1529
Philipp Maiere726d4f2017-11-01 10:41:34 +01001530 /* Do not accept invalid configuration values
1531 * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and
1532 * values greater 0 */
1533 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1534
1535 /* The dummy packet functionality has been disabled, we will exit
1536 * immediately, no further timer is scheduled, which means we will no
1537 * longer send dummy packets even when we did before */
1538 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER)
1539 return;
1540
1541 /* In cases where only one dummy packet is sent, we do not need
1542 * the timer since the functions that handle the CRCX and MDCX are
1543 * triggering the sending of the dummy packet. So we behave like in
1544 * the MGCP_KEEPALIVE_NEVER case */
1545 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001546 return;
1547
Philipp Maier87bd9be2017-08-22 16:35:41 +02001548 /* Send walk over all endpoints and send out dummy packets through
1549 * every connection present on each endpoint */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001550 for (i = 1; i < tcfg->number_endpoints; ++i) {
1551 struct mgcp_endpoint *endp = &tcfg->endpoints[i];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001552 llist_for_each_entry(conn, &endp->conns, entry) {
1553 if (conn->mode == MGCP_CONN_RECV_ONLY)
1554 send_dummy(endp, &conn->u.rtp);
1555 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001556 }
1557
Philipp Maiere726d4f2017-11-01 10:41:34 +01001558 /* Schedule the keepalive timer for the next round */
1559 LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001560 tcfg->trunk_nr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001561 osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval,
1562 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001563}
1564
1565void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval)
1566{
1567 tcfg->keepalive_interval = interval;
1568 osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg);
1569
1570 if (interval <= 0)
1571 osmo_timer_del(&tcfg->keepalive_timer);
1572 else
1573 osmo_timer_schedule(&tcfg->keepalive_timer,
1574 tcfg->keepalive_interval, 0);
1575}
1576
Stefan Sperling1e174872018-10-25 18:36:10 +02001577static int free_rate_counter_group(struct rate_ctr_group *rate_ctr_group)
1578{
1579 rate_ctr_group_free(rate_ctr_group);
1580 return 0;
1581}
1582
Harald Welte3ac604e2019-05-08 14:07:41 +02001583static int alloc_mgcp_rate_counters(struct mgcp_trunk_config *trunk, void *ctx)
Stefan Sperling1e174872018-10-25 18:36:10 +02001584{
1585 /* FIXME: Each new rate counter group requires a unique index. At the
1586 * moment we generate an index using a counter, but perhaps there is
1587 * a better way of assigning indices? */
Alexander Chemeris63866002020-05-05 17:18:40 +03001588 static unsigned int general_rate_ctr_index = 0;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001589 static unsigned int crcx_rate_ctr_index = 0;
1590 static unsigned int mdcx_rate_ctr_index = 0;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001591 static unsigned int dlcx_rate_ctr_index = 0;
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001592 static unsigned int all_rtp_conn_rate_ctr_index = 0;
Stefan Sperling1e174872018-10-25 18:36:10 +02001593
Alexander Chemeris63866002020-05-05 17:18:40 +03001594 if (trunk->mgcp_general_ctr_group == NULL) {
1595 trunk->mgcp_general_ctr_group = rate_ctr_group_alloc(ctx, &mgcp_general_ctr_group_desc, general_rate_ctr_index);
1596 if (!trunk->mgcp_general_ctr_group)
1597 return -1;
1598 talloc_set_destructor(trunk->mgcp_general_ctr_group, free_rate_counter_group);
1599 general_rate_ctr_index++;
1600 }
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001601 if (trunk->mgcp_crcx_ctr_group == NULL) {
1602 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 +02001603 if (!trunk->mgcp_crcx_ctr_group)
1604 return -1;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001605 talloc_set_destructor(trunk->mgcp_crcx_ctr_group, free_rate_counter_group);
1606 crcx_rate_ctr_index++;
1607 }
1608 if (trunk->mgcp_mdcx_ctr_group == NULL) {
1609 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 +02001610 if (!trunk->mgcp_mdcx_ctr_group)
1611 return -1;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001612 talloc_set_destructor(trunk->mgcp_mdcx_ctr_group, free_rate_counter_group);
1613 mdcx_rate_ctr_index++;
1614 }
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001615 if (trunk->mgcp_dlcx_ctr_group == NULL) {
1616 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 +02001617 if (!trunk->mgcp_dlcx_ctr_group)
1618 return -1;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001619 talloc_set_destructor(trunk->mgcp_dlcx_ctr_group, free_rate_counter_group);
1620 dlcx_rate_ctr_index++;
1621 }
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001622 if (trunk->all_rtp_conn_stats == NULL) {
1623 trunk->all_rtp_conn_stats = rate_ctr_group_alloc(ctx, &all_rtp_conn_rate_ctr_group_desc,
1624 all_rtp_conn_rate_ctr_index);
Harald Welte3ac604e2019-05-08 14:07:41 +02001625 if (!trunk->all_rtp_conn_stats)
1626 return -1;
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001627 talloc_set_destructor(trunk->all_rtp_conn_stats, free_rate_counter_group);
1628 all_rtp_conn_rate_ctr_index++;
1629 }
Harald Welte3ac604e2019-05-08 14:07:41 +02001630 return 0;
Stefan Sperling1e174872018-10-25 18:36:10 +02001631}
1632
Philipp Maier87bd9be2017-08-22 16:35:41 +02001633/*! allocate configuration with default values.
1634 * (called once at startup by main function) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001635struct mgcp_config *mgcp_config_alloc(void)
1636{
1637 struct mgcp_config *cfg;
1638
1639 cfg = talloc_zero(NULL, struct mgcp_config);
1640 if (!cfg) {
1641 LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n");
1642 return NULL;
1643 }
1644
Philipp Maier12943ea2018-01-17 15:40:25 +01001645 osmo_strlcpy(cfg->domain, "mgw", sizeof(cfg->domain));
1646
Philipp Maier87bd9be2017-08-22 16:35:41 +02001647 cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START;
1648 cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END;
1649 cfg->net_ports.last_port = cfg->net_ports.range_start;
1650
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001651 cfg->source_port = 2427;
1652 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
1653 cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
1654
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001655 cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
1656 cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
1657
1658 cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
1659
Harald Weltec39b1bf2020-03-08 11:29:39 +01001660 INIT_LLIST_HEAD(&cfg->trunks);
1661
1662 /* default trunk handling */
1663 cfg->virt_trunk = mgcp_trunk_alloc(cfg, MGCP_TRUNK_VIRTUAL, 0);
1664 if (!cfg->virt_trunk) {
Harald Welte3ac604e2019-05-08 14:07:41 +02001665 talloc_free(cfg);
1666 return NULL;
1667 }
Harald Weltec39b1bf2020-03-08 11:29:39 +01001668 /* virtual trunk is not part of the list! */
1669 llist_del(&cfg->virt_trunk->entry);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001670
1671 return cfg;
1672}
1673
Harald Weltec39b1bf2020-03-08 11:29:39 +01001674/*! allocate configuration with default values. Do not link it into global list yet!
Philipp Maier87bd9be2017-08-22 16:35:41 +02001675 * (called once at startup by VTY)
1676 * \param[in] cfg mgcp configuration
1677 * \param[in] nr trunk number
1678 * \returns pointer to allocated trunk configuration */
Harald Weltec39b1bf2020-03-08 11:29:39 +01001679struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, enum mgcp_trunk_type ttype, int nr)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001680{
1681 struct mgcp_trunk_config *trunk;
1682
1683 trunk = talloc_zero(cfg, struct mgcp_trunk_config);
1684 if (!trunk) {
1685 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n");
1686 return NULL;
1687 }
1688
1689 trunk->cfg = cfg;
Harald Weltec39b1bf2020-03-08 11:29:39 +01001690 trunk->trunk_type = ttype;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001691 trunk->trunk_nr = nr;
Harald Weltec39b1bf2020-03-08 11:29:39 +01001692 trunk->audio_name = talloc_strdup(trunk, "AMR/8000");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001693 trunk->audio_payload = 126;
1694 trunk->audio_send_ptime = 1;
1695 trunk->audio_send_name = 1;
Philipp Maierfcd06552017-11-10 17:32:22 +01001696 trunk->vty_number_endpoints = 33;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001697 trunk->omit_rtcp = 0;
1698 mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
Harald Weltec39b1bf2020-03-08 11:29:39 +01001699 if (alloc_mgcp_rate_counters(trunk, trunk) < 0) {
1700 talloc_free(trunk);
1701 return NULL;
1702 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001703 llist_add_tail(&trunk->entry, &cfg->trunks);
Stefan Sperling1e174872018-10-25 18:36:10 +02001704
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001705 return trunk;
1706}
1707
Philipp Maier87bd9be2017-08-22 16:35:41 +02001708/*! get trunk configuration by trunk number (index).
1709 * \param[in] cfg mgcp configuration
1710 * \param[in] index trunk number
1711 * \returns pointer to trunk configuration, NULL on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001712struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
1713{
1714 struct mgcp_trunk_config *trunk;
1715
1716 llist_for_each_entry(trunk, &cfg->trunks, entry)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001717 if (trunk->trunk_nr == index)
1718 return trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001719
1720 return NULL;
1721}
1722
Philipp Maier87bd9be2017-08-22 16:35:41 +02001723/*! allocate endpoints and set default values.
1724 * (called once at startup by VTY)
1725 * \param[in] tcfg trunk configuration
1726 * \returns 0 on success, -1 on failure */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001727int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
1728{
1729 int i;
1730
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001731 tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001732 sizeof(struct mgcp_endpoint),
Philipp Maierfcd06552017-11-10 17:32:22 +01001733 tcfg->vty_number_endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001734 "endpoints");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001735 if (!tcfg->endpoints)
1736 return -1;
1737
Philipp Maierfcd06552017-11-10 17:32:22 +01001738 for (i = 0; i < tcfg->vty_number_endpoints; ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001739 INIT_LLIST_HEAD(&tcfg->endpoints[i].conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001740 tcfg->endpoints[i].cfg = tcfg->cfg;
1741 tcfg->endpoints[i].tcfg = tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001742
Pau Espin Pedrol9f11dc52019-04-22 20:48:50 +02001743 switch (tcfg->trunk_type) {
1744 case MGCP_TRUNK_VIRTUAL:
1745 tcfg->endpoints[i].type = &ep_typeset.rtp;
1746 break;
1747 case MGCP_TRUNK_E1:
1748 /* FIXME: Implement E1 allocation */
1749 LOGP(DLMGCP, LOGL_FATAL, "E1 trunks not implemented!\n");
1750 break;
1751 default:
1752 osmo_panic("Cannot allocate unimplemented trunk type %d! %s:%d\n",
1753 tcfg->trunk_type, __FILE__, __LINE__);
1754 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001755 }
1756
Philipp Maierfcd06552017-11-10 17:32:22 +01001757 tcfg->number_endpoints = tcfg->vty_number_endpoints;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001758 alloc_mgcp_rate_counters(tcfg, tcfg->cfg);
Stefan Sperling1e174872018-10-25 18:36:10 +02001759
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001760 return 0;
1761}
1762
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001763static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1764{
1765 return write(cfg->gw_fd.bfd.fd, buf, len);
1766}
1767
Philipp Maier87bd9be2017-08-22 16:35:41 +02001768/*! Reset all endpoints by sending RSIP message to self.
1769 * (called by VTY)
1770 * \param[in] endp trunk endpoint
1771 * \param[in] endpoint number
1772 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001773int mgcp_send_reset_all(struct mgcp_config *cfg)
1774{
Philipp Maier12943ea2018-01-17 15:40:25 +01001775 char buf[MGCP_ENDPOINT_MAXLEN + 128];
1776 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001777 int rc;
1778
Philipp Maier12943ea2018-01-17 15:40:25 +01001779 len = snprintf(buf, sizeof(buf),
1780 "RSIP 1 *@%s MGCP 1.0\r\n", cfg->domain);
1781 if (len < 0)
1782 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001783
Philipp Maier12943ea2018-01-17 15:40:25 +01001784 rc = send_agent(cfg, buf, len);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001785 if (rc <= 0)
1786 return -1;
1787
1788 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001789}
1790
Philipp Maier87bd9be2017-08-22 16:35:41 +02001791/*! Reset a single endpoint by sending RSIP message to self.
1792 * (called by VTY)
1793 * \param[in] endp trunk endpoint
1794 * \param[in] endpoint number
1795 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001796int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
1797{
Philipp Maier12943ea2018-01-17 15:40:25 +01001798 char buf[MGCP_ENDPOINT_MAXLEN + 128];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001799 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001800 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001801
1802 len = snprintf(buf, sizeof(buf),
Philipp Maier12943ea2018-01-17 15:40:25 +01001803 "RSIP 39 %x@%s MGCP 1.0\r\n", endpoint, endp->cfg->domain);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001804 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001805 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001806
Philipp Maier87bd9be2017-08-22 16:35:41 +02001807 rc = send_agent(endp->cfg, buf, len);
1808 if (rc <= 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001809 return -1;
1810
Philipp Maier87bd9be2017-08-22 16:35:41 +02001811 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001812}