blob: 82c10aa8a3e40aefb683a9922e8c6e76da743e98 [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{
Alexander Chemeris63866002020-05-05 17:18:40 +0300382 struct mgcp_trunk_config *tcfg = &cfg->trunk;
383 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 Maierbc0346e2018-06-07 09:52:16 +02001088
1089
1090
1091
Philipp Maier87bd9be2017-08-22 16:35:41 +02001092/* MDCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001093static struct msgb *handle_modify_con(struct mgcp_parse_data *p)
1094{
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001095 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001096 struct mgcp_endpoint *endp = p->endp;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001097 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_mdcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001098 int error_code = 500;
1099 int silent = 0;
1100 int have_sdp = 0;
1101 char *line;
1102 const char *local_options = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001103 const char *mode = NULL;
1104 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier01d24a32017-11-21 17:26:09 +01001105 const char *conn_id = NULL;
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001106 int osmux_cid = -2;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001107 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001108
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001109 LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001110
Philipp Maier5656fbf2018-02-02 14:41:58 +01001111 /* Prohibit wildcarded requests */
1112 if (endp->wildcarded_req) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001113 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1114 "MDCX: wildcarded endpoint names not supported.\n");
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001115 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_WILDCARD]);
Philipp Maier5656fbf2018-02-02 14:41:58 +01001116 return create_err_response(endp, 507, "MDCX", p->trans);
1117 }
1118
Philipp Maier87bd9be2017-08-22 16:35:41 +02001119 if (llist_count(&endp->conns) <= 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001120 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1121 "MDCX: endpoint is not holding a connection.\n");
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001122 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_CONN]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001123 return create_err_response(endp, 400, "MDCX", p->trans);
1124 }
1125
1126 for_each_line(line, p->save) {
1127 if (!mgcp_check_param(endp, line))
1128 continue;
1129
Pau Espin Pedrol0c6c3c12019-06-25 17:18:12 +02001130 switch (toupper(line[0])) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001131 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +01001132 if (mgcp_verify_call_id(endp, line + 3) != 0) {
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001133 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CALLID]);
Harald Weltee35eeae2017-12-28 13:47:37 +01001134 error_code = 516;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001135 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001136 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001137 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001138 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001139 conn_id = (const char *)line + 3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001140 if ((error_code = mgcp_verify_ci(endp, conn_id))) {
1141 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CONNID]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001142 goto error3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001143 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001144 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001145 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001146 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001147 break;
1148 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001149 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001150 break;
1151 case 'Z':
Pau Espin Pedrol9b508f62019-06-26 13:11:22 +02001152 silent = strcasecmp("noanswer", line + 3) == 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001153 break;
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001154 case 'X':
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +02001155 if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001156 /* If osmux is disabled, just skip setting it up */
1157 if (!p->endp->cfg->osmux)
1158 break;
1159 osmux_cid = mgcp_osmux_setup(endp, line);
1160 break;
1161 }
1162 /* Ignore unknown X-headers */
1163 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001164 case '\0':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001165 have_sdp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001166 goto mgcp_header_done;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001167 break;
1168 default:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001169 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1170 "MDCX: Unhandled MGCP option: '%c'/%d\n",
1171 line[0], line[0]);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001172 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001173 return create_err_response(NULL, 539, "MDCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001174 break;
1175 }
1176 }
1177
Philipp Maier87bd9be2017-08-22 16:35:41 +02001178mgcp_header_done:
Philipp Maier01d24a32017-11-21 17:26:09 +01001179 if (!conn_id) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001180 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1181 "MDCX: insufficient parameters, missing ci (connectionIdentifier)\n");
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001182 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_CONNID]);
Harald Weltee35eeae2017-12-28 13:47:37 +01001183 return create_err_response(endp, 515, "MDCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001184 }
1185
1186 conn = mgcp_conn_get_rtp(endp, conn_id);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001187 if (!conn) {
1188 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_CONN_NOT_FOUND]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001189 return create_err_response(endp, 400, "MDCX", p->trans);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001190 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001191
Oliver Smithe36b7752019-01-22 16:31:36 +01001192 mgcp_conn_watchdog_kick(conn->conn);
1193
Philipp Maier87bd9be2017-08-22 16:35:41 +02001194 if (mode) {
1195 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001196 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_MODE]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001197 error_code = 517;
1198 goto error3;
1199 }
1200 } else
Pau Espin Pedrol209eb9f2019-04-24 12:03:04 +02001201 conn->conn->mode = conn->conn->mode_orig;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001202
Philipp Maierbc0346e2018-06-07 09:52:16 +02001203 /* Set local connection options, if present */
1204 if (local_options) {
1205 rc = set_local_cx_options(endp->tcfg->endpoints,
1206 &endp->local_options, local_options);
1207 if (rc != 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001208 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1209 "MDCX: invalid local connection options!\n");
Philipp Maierbc0346e2018-06-07 09:52:16 +02001210 error_code = rc;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001211 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS]);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001212 goto error3;
1213 }
1214 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001215
Philipp Maierbc0346e2018-06-07 09:52:16 +02001216 /* Handle codec information and decide for a suitable codec */
1217 rc = handle_codec_info(conn, p, have_sdp, false);
1218 mgcp_codec_summary(conn);
1219 if (rc) {
Philipp Maieraf07f662018-02-02 11:34:02 +01001220 error_code = rc;
1221 goto error3;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001222 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001223
Philipp Maierc3cc6542018-02-02 12:58:42 +01001224 /* check connection mode setting */
1225 if (conn->conn->mode != MGCP_CONN_LOOPBACK
1226 && conn->conn->mode != MGCP_CONN_RECV_ONLY
1227 && conn->end.rtp_port == 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001228 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1229 "MDCX: selected connection mode type requires an opposite end!\n");
Philipp Maierc3cc6542018-02-02 12:58:42 +01001230 error_code = 527;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001231 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC]);
Philipp Maierc3cc6542018-02-02 12:58:42 +01001232 goto error3;
1233 }
1234
Pau Espin Pedrol6be2c492019-05-08 15:22:59 +02001235 if (mgcp_conn_rtp_is_osmux(conn)) {
1236 OSMO_ASSERT(conn->osmux.cid_allocated);
1237 if (osmux_cid < -1) {
1238 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1239 "MDCX: Failed to parse Osmux CID!\n");
1240 goto error3;
1241 } else if (osmux_cid == -1) {
1242 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1243 "MDCX: wilcard in MDCX is not supported!\n");
1244 goto error3;
1245 } else if (osmux_cid != (int) conn->osmux.cid) {
1246 LOGPCONN(conn->conn, DLMGCP, LOGL_ERROR,
1247 "MDCX: changing already allocated CID is not supported!\n");
1248 goto error3;
1249 }
1250 /* TODO: In the future (when we have recvCID!=sendCID), we need to
1251 tell Osmux code that osmux_cid is to be used as sendCID for
1252 that conn. */
1253 }
Philipp Maierbc0346e2018-06-07 09:52:16 +02001254
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001255 if (setup_rtp_processing(endp, conn) != 0) {
1256 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_START_RTP]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001257 goto error3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001258 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001259
Philipp Maier87bd9be2017-08-22 16:35:41 +02001260
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001261 /* policy CB */
1262 if (p->cfg->policy_cb) {
1263 int rc;
1264 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001265 MGCP_ENDP_MDCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001266 switch (rc) {
1267 case MGCP_POLICY_REJECT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001268 LOGPCONN(conn->conn, DLMGCP, LOGL_NOTICE,
1269 "MDCX: rejected by policy\n");
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001270 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001271 if (silent)
1272 goto out_silent;
1273 return create_err_response(endp, 400, "MDCX", p->trans);
1274 break;
1275 case MGCP_POLICY_DEFER:
1276 /* stop processing */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001277 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG,
1278 "MDCX: deferred by policy\n");
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001279 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_DEFERRED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001280 return NULL;
1281 break;
1282 case MGCP_POLICY_CONT:
1283 /* just continue */
1284 break;
1285 }
1286 }
1287
Philipp Maier87bd9be2017-08-22 16:35:41 +02001288 mgcp_rtp_end_config(endp, 1, &conn->end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001289
1290 /* modify */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001291 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG,
1292 "MDCX: modified conn:%s\n", mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001293 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001294 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1295 MGCP_ENDP_MDCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001296
Philipp Maiere726d4f2017-11-01 10:41:34 +01001297 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
1298 OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1299 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
1300 && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001301 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001302
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001303 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_SUCCESS]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001304 if (silent)
1305 goto out_silent;
1306
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001307 LOGPCONN(conn->conn, DLMGCP, LOGL_NOTICE,
1308 "MDCX: connection successfully modified\n");
Philipp Maier55295f72018-01-15 14:00:28 +01001309 return create_response_with_sdp(endp, conn, "MDCX", p->trans, false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001310error3:
1311 return create_err_response(endp, error_code, "MDCX", p->trans);
1312
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001313out_silent:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001314 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "MDCX: silent exit\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001315 return NULL;
1316}
1317
Philipp Maier87bd9be2017-08-22 16:35:41 +02001318/* DLCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001319static struct msgb *handle_delete_con(struct mgcp_parse_data *p)
1320{
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001321 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001322 struct mgcp_endpoint *endp = p->endp;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001323 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_dlcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001324 int error_code = 400;
1325 int silent = 0;
1326 char *line;
1327 char stats[1048];
Philipp Maier01d24a32017-11-21 17:26:09 +01001328 const char *conn_id = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001329 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001330
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001331 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1332 "DLCX: deleting connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001333
Philipp Maier5656fbf2018-02-02 14:41:58 +01001334 /* Prohibit wildcarded requests */
1335 if (endp->wildcarded_req) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001336 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1337 "DLCX: wildcarded endpoint names not supported.\n");
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001338 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_WILDCARD]);
Philipp Maier5656fbf2018-02-02 14:41:58 +01001339 return create_err_response(endp, 507, "DLCX", p->trans);
1340 }
1341
Philipp Maier87bd9be2017-08-22 16:35:41 +02001342 if (llist_count(&endp->conns) <= 0) {
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001343 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
1344 "DLCX: endpoint is not holding a connection.\n");
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001345 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_NO_CONN]);
Harald Weltee35eeae2017-12-28 13:47:37 +01001346 return create_err_response(endp, 515, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001347 }
1348
1349 for_each_line(line, p->save) {
1350 if (!mgcp_check_param(endp, line))
1351 continue;
1352
Pau Espin Pedrol0c6c3c12019-06-25 17:18:12 +02001353 switch (toupper(line[0])) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001354 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +01001355 if (mgcp_verify_call_id(endp, line + 3) != 0) {
1356 error_code = 516;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001357 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CALLID]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001358 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001359 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001360 break;
1361 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001362 conn_id = (const char *)line + 3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001363 if ((error_code = mgcp_verify_ci(endp, conn_id))) {
1364 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CONNID]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001365 goto error3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001366 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001367 break;
1368 case 'Z':
Pau Espin Pedrol9b508f62019-06-26 13:11:22 +02001369 silent = strcasecmp("noanswer", line + 3) == 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001370 break;
1371 default:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001372 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1373 "DLCX: Unhandled MGCP option: '%c'/%d\n",
1374 line[0], line[0]);
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001375 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001376 return create_err_response(NULL, 539, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001377 break;
1378 }
1379 }
1380
1381 /* policy CB */
1382 if (p->cfg->policy_cb) {
1383 int rc;
1384 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001385 MGCP_ENDP_DLCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001386 switch (rc) {
1387 case MGCP_POLICY_REJECT:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001388 LOGPENDP(endp, DLMGCP, LOGL_NOTICE, "DLCX: rejected by policy\n");
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001389 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001390 if (silent)
1391 goto out_silent;
1392 return create_err_response(endp, 400, "DLCX", p->trans);
1393 break;
1394 case MGCP_POLICY_DEFER:
1395 /* stop processing */
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001396 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_DEFERRED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001397 return NULL;
1398 break;
1399 case MGCP_POLICY_CONT:
1400 /* just continue */
1401 break;
1402 }
1403 }
1404
Philipp Maierf4c0e372017-10-11 16:06:45 +02001405 /* When no connection id is supplied, we will interpret this as a
1406 * wildcarded DLCX and drop all connections at once. (See also
1407 * RFC3435 Section F.7) */
Philipp Maier01d24a32017-11-21 17:26:09 +01001408 if (!conn_id) {
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001409 int num_conns = llist_count(&endp->conns);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001410 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1411 "DLCX: missing ci (connectionIdentifier), will remove all connections (%d total) at once\n",
1412 num_conns);
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001413
1414 if (num_conns > 0)
1415 rate_ctr_add(&rate_ctrs->ctr[MGCP_DLCX_SUCCESS], num_conns);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001416
Philipp Maier1355d7e2018-02-01 14:30:06 +01001417 mgcp_endp_release(endp);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001418
1419 /* Note: In this case we do not return any statistics,
1420 * as we assume that the client is not interested in
1421 * this case. */
1422 return create_ok_response(endp, 200, "DLCX", p->trans);
1423 }
1424
Philipp Maierf4c0e372017-10-11 16:06:45 +02001425 /* Find the connection */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001426 conn = mgcp_conn_get_rtp(endp, conn_id);
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001427 if (!conn) {
1428 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_FAIL_INVALID_CONNID]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001429 goto error3;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001430 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001431 /* save the statistics of the current connection */
1432 mgcp_format_stats(stats, sizeof(stats), conn->conn);
1433
1434 /* delete connection */
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001435 LOGPCONN(conn->conn, DLMGCP, LOGL_DEBUG, "DLCX: deleting conn:%s\n",
1436 mgcp_conn_dump(conn->conn));
Philipp Maier87bd9be2017-08-22 16:35:41 +02001437 mgcp_conn_free(endp, conn_id);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001438 LOGPENDP(endp, DLMGCP, LOGL_NOTICE,
1439 "DLCX: connection successfully deleted\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001440
1441 /* When all connections are closed, the endpoint will be released
1442 * in order to be ready to be used by another call. */
1443 if (llist_count(&endp->conns) <= 0) {
Philipp Maier1355d7e2018-02-01 14:30:06 +01001444 mgcp_endp_release(endp);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001445 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "DLCX: endpoint released\n");
Philipp Maier87bd9be2017-08-22 16:35:41 +02001446 }
1447
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001448 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001449 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1450 MGCP_ENDP_DLCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001451
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001452 rate_ctr_inc(&rate_ctrs->ctr[MGCP_DLCX_SUCCESS]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001453 if (silent)
1454 goto out_silent;
1455 return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats);
1456
1457error3:
1458 return create_err_response(endp, error_code, "DLCX", p->trans);
1459
1460out_silent:
Pau Espin Pedrol3239f622019-04-24 18:47:46 +02001461 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "DLCX: silent exit\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001462 return NULL;
1463}
1464
Philipp Maier87bd9be2017-08-22 16:35:41 +02001465/* RSIP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001466static struct msgb *handle_rsip(struct mgcp_parse_data *p)
1467{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001468 /* TODO: Also implement the resetting of a specific endpoint
1469 * to make mgcp_send_reset_ep() work. Currently this will call
1470 * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1
1471 * to make read_call_agent() reset all endpoints when called
1472 * next time. In order to selectively reset endpoints some
1473 * mechanism to distinguish which endpoint shall be resetted
1474 * is needed */
1475
1476 LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n");
1477
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001478 if (p->cfg->reset_cb)
1479 p->cfg->reset_cb(p->endp->tcfg);
1480 return NULL;
1481}
1482
1483static char extract_tone(const char *line)
1484{
1485 const char *str = strstr(line, "D/");
1486 if (!str)
1487 return CHAR_MAX;
1488
1489 return str[2];
1490}
1491
Philipp Maier87bd9be2017-08-22 16:35:41 +02001492/* This can request like DTMF detection and forward, fax detection... it
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001493 * can also request when the notification should be send and such. We don't
Philipp Maier87bd9be2017-08-22 16:35:41 +02001494 * do this right now. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001495static struct msgb *handle_noti_req(struct mgcp_parse_data *p)
1496{
1497 int res = 0;
1498 char *line;
1499 char tone = CHAR_MAX;
1500
Philipp Maier87bd9be2017-08-22 16:35:41 +02001501 LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n");
1502
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001503 for_each_line(line, p->save) {
Pau Espin Pedrol0c6c3c12019-06-25 17:18:12 +02001504 switch (toupper(line[0])) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001505 case 'S':
1506 tone = extract_tone(line);
1507 break;
1508 }
1509 }
1510
1511 /* we didn't see a signal request with a tone */
1512 if (tone == CHAR_MAX)
1513 return create_ok_response(p->endp, 200, "RQNT", p->trans);
1514
1515 if (p->cfg->rqnt_cb)
1516 res = p->cfg->rqnt_cb(p->endp, tone);
1517
1518 return res == 0 ?
Philipp Maier87bd9be2017-08-22 16:35:41 +02001519 create_ok_response(p->endp, 200, "RQNT", p->trans) :
1520 create_err_response(p->endp, res, "RQNT", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001521}
1522
Philipp Maier87bd9be2017-08-22 16:35:41 +02001523/* Connection keepalive timer, will take care that dummy packets are send
Harald Welte1d1b98f2017-12-25 10:03:40 +01001524 * regularly, so that NAT connections stay open */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001525static void mgcp_keepalive_timer_cb(void *_tcfg)
1526{
1527 struct mgcp_trunk_config *tcfg = _tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001528 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001529 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001530
Philipp Maiere726d4f2017-11-01 10:41:34 +01001531 LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001532 tcfg->trunk_nr);
1533
Philipp Maiere726d4f2017-11-01 10:41:34 +01001534 /* Do not accept invalid configuration values
1535 * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and
1536 * values greater 0 */
1537 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1538
1539 /* The dummy packet functionality has been disabled, we will exit
1540 * immediately, no further timer is scheduled, which means we will no
1541 * longer send dummy packets even when we did before */
1542 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER)
1543 return;
1544
1545 /* In cases where only one dummy packet is sent, we do not need
1546 * the timer since the functions that handle the CRCX and MDCX are
1547 * triggering the sending of the dummy packet. So we behave like in
1548 * the MGCP_KEEPALIVE_NEVER case */
1549 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001550 return;
1551
Philipp Maier87bd9be2017-08-22 16:35:41 +02001552 /* Send walk over all endpoints and send out dummy packets through
1553 * every connection present on each endpoint */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001554 for (i = 1; i < tcfg->number_endpoints; ++i) {
1555 struct mgcp_endpoint *endp = &tcfg->endpoints[i];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001556 llist_for_each_entry(conn, &endp->conns, entry) {
1557 if (conn->mode == MGCP_CONN_RECV_ONLY)
1558 send_dummy(endp, &conn->u.rtp);
1559 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001560 }
1561
Philipp Maiere726d4f2017-11-01 10:41:34 +01001562 /* Schedule the keepalive timer for the next round */
1563 LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001564 tcfg->trunk_nr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001565 osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval,
1566 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001567}
1568
1569void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval)
1570{
1571 tcfg->keepalive_interval = interval;
1572 osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg);
1573
1574 if (interval <= 0)
1575 osmo_timer_del(&tcfg->keepalive_timer);
1576 else
1577 osmo_timer_schedule(&tcfg->keepalive_timer,
1578 tcfg->keepalive_interval, 0);
1579}
1580
Stefan Sperling1e174872018-10-25 18:36:10 +02001581static int free_rate_counter_group(struct rate_ctr_group *rate_ctr_group)
1582{
1583 rate_ctr_group_free(rate_ctr_group);
1584 return 0;
1585}
1586
Harald Welte3ac604e2019-05-08 14:07:41 +02001587static int alloc_mgcp_rate_counters(struct mgcp_trunk_config *trunk, void *ctx)
Stefan Sperling1e174872018-10-25 18:36:10 +02001588{
1589 /* FIXME: Each new rate counter group requires a unique index. At the
1590 * moment we generate an index using a counter, but perhaps there is
1591 * a better way of assigning indices? */
Alexander Chemeris63866002020-05-05 17:18:40 +03001592 static unsigned int general_rate_ctr_index = 0;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001593 static unsigned int crcx_rate_ctr_index = 0;
1594 static unsigned int mdcx_rate_ctr_index = 0;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001595 static unsigned int dlcx_rate_ctr_index = 0;
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001596 static unsigned int all_rtp_conn_rate_ctr_index = 0;
Stefan Sperling1e174872018-10-25 18:36:10 +02001597
Alexander Chemeris63866002020-05-05 17:18:40 +03001598 if (trunk->mgcp_general_ctr_group == NULL) {
1599 trunk->mgcp_general_ctr_group = rate_ctr_group_alloc(ctx, &mgcp_general_ctr_group_desc, general_rate_ctr_index);
1600 if (!trunk->mgcp_general_ctr_group)
1601 return -1;
1602 talloc_set_destructor(trunk->mgcp_general_ctr_group, free_rate_counter_group);
1603 general_rate_ctr_index++;
1604 }
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001605 if (trunk->mgcp_crcx_ctr_group == NULL) {
1606 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 +02001607 if (!trunk->mgcp_crcx_ctr_group)
1608 return -1;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001609 talloc_set_destructor(trunk->mgcp_crcx_ctr_group, free_rate_counter_group);
1610 crcx_rate_ctr_index++;
1611 }
1612 if (trunk->mgcp_mdcx_ctr_group == NULL) {
1613 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 +02001614 if (!trunk->mgcp_mdcx_ctr_group)
1615 return -1;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001616 talloc_set_destructor(trunk->mgcp_mdcx_ctr_group, free_rate_counter_group);
1617 mdcx_rate_ctr_index++;
1618 }
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001619 if (trunk->mgcp_dlcx_ctr_group == NULL) {
1620 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 +02001621 if (!trunk->mgcp_dlcx_ctr_group)
1622 return -1;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +01001623 talloc_set_destructor(trunk->mgcp_dlcx_ctr_group, free_rate_counter_group);
1624 dlcx_rate_ctr_index++;
1625 }
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001626 if (trunk->all_rtp_conn_stats == NULL) {
1627 trunk->all_rtp_conn_stats = rate_ctr_group_alloc(ctx, &all_rtp_conn_rate_ctr_group_desc,
1628 all_rtp_conn_rate_ctr_index);
Harald Welte3ac604e2019-05-08 14:07:41 +02001629 if (!trunk->all_rtp_conn_stats)
1630 return -1;
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001631 talloc_set_destructor(trunk->all_rtp_conn_stats, free_rate_counter_group);
1632 all_rtp_conn_rate_ctr_index++;
1633 }
Harald Welte3ac604e2019-05-08 14:07:41 +02001634 return 0;
Stefan Sperling1e174872018-10-25 18:36:10 +02001635}
1636
Philipp Maier87bd9be2017-08-22 16:35:41 +02001637/*! allocate configuration with default values.
1638 * (called once at startup by main function) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001639struct mgcp_config *mgcp_config_alloc(void)
1640{
1641 struct mgcp_config *cfg;
1642
1643 cfg = talloc_zero(NULL, struct mgcp_config);
1644 if (!cfg) {
1645 LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n");
1646 return NULL;
1647 }
1648
Philipp Maier12943ea2018-01-17 15:40:25 +01001649 osmo_strlcpy(cfg->domain, "mgw", sizeof(cfg->domain));
1650
Philipp Maier87bd9be2017-08-22 16:35:41 +02001651 cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START;
1652 cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END;
1653 cfg->net_ports.last_port = cfg->net_ports.range_start;
1654
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001655 cfg->source_port = 2427;
1656 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
1657 cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
1658
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001659 cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
1660 cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
1661
1662 cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
1663
Harald Welteb141ccc2020-03-08 10:49:10 +01001664 /* default trunk handling; TODO: avoid duplication with mgcp_trunk_alloc() below */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001665 cfg->trunk.cfg = cfg;
1666 cfg->trunk.trunk_nr = 0;
1667 cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
1668 cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
1669 cfg->trunk.audio_payload = 126;
1670 cfg->trunk.audio_send_ptime = 1;
1671 cfg->trunk.audio_send_name = 1;
Harald Welteb141ccc2020-03-08 10:49:10 +01001672 cfg->trunk.vty_number_endpoints = 33;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001673 cfg->trunk.omit_rtcp = 0;
1674 mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE);
Harald Welte3ac604e2019-05-08 14:07:41 +02001675 if (alloc_mgcp_rate_counters(&cfg->trunk, cfg) < 0) {
1676 talloc_free(cfg);
1677 return NULL;
1678 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001679
1680 INIT_LLIST_HEAD(&cfg->trunks);
1681
1682 return cfg;
1683}
1684
Philipp Maier87bd9be2017-08-22 16:35:41 +02001685/*! allocate configuration with default values.
1686 * (called once at startup by VTY)
1687 * \param[in] cfg mgcp configuration
1688 * \param[in] nr trunk number
1689 * \returns pointer to allocated trunk configuration */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001690struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
1691{
1692 struct mgcp_trunk_config *trunk;
1693
1694 trunk = talloc_zero(cfg, struct mgcp_trunk_config);
1695 if (!trunk) {
1696 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n");
1697 return NULL;
1698 }
1699
1700 trunk->cfg = cfg;
1701 trunk->trunk_type = MGCP_TRUNK_E1;
1702 trunk->trunk_nr = nr;
1703 trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
1704 trunk->audio_payload = 126;
1705 trunk->audio_send_ptime = 1;
1706 trunk->audio_send_name = 1;
Philipp Maierfcd06552017-11-10 17:32:22 +01001707 trunk->vty_number_endpoints = 33;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001708 trunk->omit_rtcp = 0;
1709 mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001710 alloc_mgcp_rate_counters(trunk, trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001711 llist_add_tail(&trunk->entry, &cfg->trunks);
Stefan Sperling1e174872018-10-25 18:36:10 +02001712
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001713 return trunk;
1714}
1715
Philipp Maier87bd9be2017-08-22 16:35:41 +02001716/*! get trunk configuration by trunk number (index).
1717 * \param[in] cfg mgcp configuration
1718 * \param[in] index trunk number
1719 * \returns pointer to trunk configuration, NULL on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001720struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
1721{
1722 struct mgcp_trunk_config *trunk;
1723
1724 llist_for_each_entry(trunk, &cfg->trunks, entry)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001725 if (trunk->trunk_nr == index)
1726 return trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001727
1728 return NULL;
1729}
1730
Philipp Maier87bd9be2017-08-22 16:35:41 +02001731/*! allocate endpoints and set default values.
1732 * (called once at startup by VTY)
1733 * \param[in] tcfg trunk configuration
1734 * \returns 0 on success, -1 on failure */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001735int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
1736{
1737 int i;
1738
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001739 tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001740 sizeof(struct mgcp_endpoint),
Philipp Maierfcd06552017-11-10 17:32:22 +01001741 tcfg->vty_number_endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001742 "endpoints");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001743 if (!tcfg->endpoints)
1744 return -1;
1745
Philipp Maierfcd06552017-11-10 17:32:22 +01001746 for (i = 0; i < tcfg->vty_number_endpoints; ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001747 INIT_LLIST_HEAD(&tcfg->endpoints[i].conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001748 tcfg->endpoints[i].cfg = tcfg->cfg;
1749 tcfg->endpoints[i].tcfg = tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001750
Pau Espin Pedrol9f11dc52019-04-22 20:48:50 +02001751 switch (tcfg->trunk_type) {
1752 case MGCP_TRUNK_VIRTUAL:
1753 tcfg->endpoints[i].type = &ep_typeset.rtp;
1754 break;
1755 case MGCP_TRUNK_E1:
1756 /* FIXME: Implement E1 allocation */
1757 LOGP(DLMGCP, LOGL_FATAL, "E1 trunks not implemented!\n");
1758 break;
1759 default:
1760 osmo_panic("Cannot allocate unimplemented trunk type %d! %s:%d\n",
1761 tcfg->trunk_type, __FILE__, __LINE__);
1762 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001763 }
1764
Philipp Maierfcd06552017-11-10 17:32:22 +01001765 tcfg->number_endpoints = tcfg->vty_number_endpoints;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001766 alloc_mgcp_rate_counters(tcfg, tcfg->cfg);
Stefan Sperling1e174872018-10-25 18:36:10 +02001767
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001768 return 0;
1769}
1770
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001771static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1772{
1773 return write(cfg->gw_fd.bfd.fd, buf, len);
1774}
1775
Philipp Maier87bd9be2017-08-22 16:35:41 +02001776/*! Reset all endpoints by sending RSIP message to self.
1777 * (called by VTY)
1778 * \param[in] endp trunk endpoint
1779 * \param[in] endpoint number
1780 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001781int mgcp_send_reset_all(struct mgcp_config *cfg)
1782{
Philipp Maier12943ea2018-01-17 15:40:25 +01001783 char buf[MGCP_ENDPOINT_MAXLEN + 128];
1784 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001785 int rc;
1786
Philipp Maier12943ea2018-01-17 15:40:25 +01001787 len = snprintf(buf, sizeof(buf),
1788 "RSIP 1 *@%s MGCP 1.0\r\n", cfg->domain);
1789 if (len < 0)
1790 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001791
Philipp Maier12943ea2018-01-17 15:40:25 +01001792 rc = send_agent(cfg, buf, len);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001793 if (rc <= 0)
1794 return -1;
1795
1796 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001797}
1798
Philipp Maier87bd9be2017-08-22 16:35:41 +02001799/*! Reset a single endpoint by sending RSIP message to self.
1800 * (called by VTY)
1801 * \param[in] endp trunk endpoint
1802 * \param[in] endpoint number
1803 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001804int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
1805{
Philipp Maier12943ea2018-01-17 15:40:25 +01001806 char buf[MGCP_ENDPOINT_MAXLEN + 128];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001807 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001808 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001809
1810 len = snprintf(buf, sizeof(buf),
Philipp Maier12943ea2018-01-17 15:40:25 +01001811 "RSIP 39 %x@%s MGCP 1.0\r\n", endpoint, endp->cfg->domain);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001812 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001813 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001814
Philipp Maier87bd9be2017-08-22 16:35:41 +02001815 rc = send_agent(endp->cfg, buf, len);
1816 if (rc <= 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001817 return -1;
1818
Philipp Maier87bd9be2017-08-22 16:35:41 +02001819 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001820}