blob: 33131642ce26cace2d6c27408b4235db15fc8992 [file] [log] [blame]
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* The protocol implementation */
3
4/*
5 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2012 by On-Waves
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <ctype.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <time.h>
28#include <limits.h>
29#include <unistd.h>
30#include <errno.h>
31
32#include <osmocom/core/msgb.h>
33#include <osmocom/core/talloc.h>
34#include <osmocom/core/select.h>
Stefan Sperling1e174872018-10-25 18:36:10 +020035#include <osmocom/core/stats.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020036
Philipp Maier87bd9be2017-08-22 16:35:41 +020037#include <osmocom/mgcp/mgcp.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020038#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020039#include <osmocom/mgcp/mgcp_internal.h>
40#include <osmocom/mgcp/mgcp_stat.h>
41#include <osmocom/mgcp/mgcp_msg.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010042#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maier8970c492017-10-11 13:33:42 +020043#include <osmocom/mgcp/mgcp_sdp.h>
Philipp Maierbc0346e2018-06-07 09:52:16 +020044#include <osmocom/mgcp/mgcp_codec.h>
Stefan Sperlingba25eab2018-10-30 14:32:31 +010045#include <osmocom/mgcp/mgcp_conn.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020046
47struct mgcp_request {
48 char *name;
Philipp Maier87bd9be2017-08-22 16:35:41 +020049 struct msgb *(*handle_request) (struct mgcp_parse_data * data);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020050 char *debug_name;
51};
52
53#define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \
54 { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME },
55
Stefan Sperling1e174872018-10-25 18:36:10 +020056static const struct rate_ctr_desc mgcp_crcx_ctr_desc[] = {
57 [MGCP_CRCX_SUCCESS] = {"crcx:success", "CRCX command processed successfully."},
58 [MGCP_CRCX_FAIL_BAD_ACTION] = {"crcx:bad_action", "bad action in CRCX command."},
59 [MGCP_CRCX_FAIL_UNHANDLED_PARAM] = {"crcx:unhandled_param", "unhandled parameter in CRCX command."},
60 [MGCP_CRCX_FAIL_MISSING_CALLID] = {"crcx:missing_callid", "missing CallId in CRCX command."},
Stefan Sperlingaa823bf2018-10-29 14:51:41 +010061 [MGCP_CRCX_FAIL_INVALID_MODE] = {"crcx:invalid_mode", "invalid connection mode in CRCX command."},
Stefan Sperling1e174872018-10-25 18:36:10 +020062 [MGCP_CRCX_FAIL_LIMIT_EXCEEDED] = {"crcx:limit_exceeded", "limit of concurrent connections was reached."},
63 [MGCP_CRCX_FAIL_UNKNOWN_CALLID] = {"crcx:unkown_callid", "unknown CallId in CRCX command."},
64 [MGCP_CRCX_FAIL_ALLOC_CONN] = {"crcx:alloc_conn_fail", "connection allocation failure."},
65 [MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC] = {"crcx:no_remote_conn_desc", "no opposite end specified for connection."},
66 [MGCP_CRCX_FAIL_START_RTP] = {"crcx:start_rtp_failure", "failure to start RTP processing."},
67 [MGCP_CRCX_FAIL_REJECTED_BY_POLICY] = {"crcx:conn_rejected", "connection rejected by policy."},
Stefan Sperlinga714abf2018-10-29 14:19:54 +010068 [MGCP_CRCX_FAIL_NO_OSMUX] = {"crcx:no_osmux", "no osmux offered by peer."},
69 [MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS] = {"crcx:conn_opt", "connection options invalid."},
70 [MGCP_CRCX_FAIL_CODEC_NEGOTIATION] = {"crcx:codec_nego", "codec negotiation failure."},
71 [MGCP_CRCX_FAIL_BIND_PORT] = {"crcx:bind_port", "port bind failure."},
Stefan Sperling1e174872018-10-25 18:36:10 +020072};
73
74const static struct rate_ctr_group_desc mgcp_crcx_ctr_group_desc = {
75 .group_name_prefix = "crcx",
76 .group_description = "crxc statistics",
77 .class_id = OSMO_STATS_CLASS_GLOBAL,
78 .num_ctr = ARRAY_SIZE(mgcp_crcx_ctr_desc),
79 .ctr_desc = mgcp_crcx_ctr_desc
80};
81
Stefan Sperlingaa823bf2018-10-29 14:51:41 +010082static const struct rate_ctr_desc mgcp_mdcx_ctr_desc[] = {
83 [MGCP_MDCX_SUCCESS] = {"mdcx:success", "MDCX command processed successfully."},
84 [MGCP_MDCX_FAIL_WILDCARD] = {"mdcx:wildcard", "wildcard endpoint names in MDCX commands are unsupported."},
85 [MGCP_MDCX_FAIL_NO_CONN] = {"mdcx:no_conn", "endpoint specified in MDCX command has no active connections."},
86 [MGCP_MDCX_FAIL_INVALID_CALLID] = {"mdcx:callid", "invalid CallId specified in MDCX command."},
87 [MGCP_MDCX_FAIL_INVALID_CONNID] = {"mdcx:connid", "invalid connection ID specified in MDCX command."},
88 [MGCP_MDCX_FAIL_UNHANDLED_PARAM] = {"crcx:unhandled_param", "unhandled parameter in MDCX command."},
89 [MGCP_MDCX_FAIL_NO_CONNID] = {"mdcx:no_connid", "no connection ID specified in MDCX command."},
90 [MGCP_MDCX_FAIL_CONN_NOT_FOUND] = {"mdcx:conn_not_found", "connection specified in MDCX command does not exist."},
91 [MGCP_MDCX_FAIL_INVALID_MODE] = {"mdcx:invalid_mode", "invalid connection mode in MDCX command."},
92 [MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS] = {"mdcx:conn_opt", "connection options invalid."},
93 [MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC] = {"mdcx:no_remote_conn_desc", "no opposite end specified for connection."},
94 [MGCP_MDCX_FAIL_START_RTP] = {"mdcx:start_rtp_failure", "failure to start RTP processing."},
95 [MGCP_MDCX_FAIL_REJECTED_BY_POLICY] = {"mdcx:conn_rejected", "connection rejected by policy."},
96 [MGCP_MDCX_FAIL_DEFERRED_BY_POLICY] = {"mdcx:conn_deferred", "connection deferred by policy."},
97};
98
99const static struct rate_ctr_group_desc mgcp_mdcx_ctr_group_desc = {
100 .group_name_prefix = "mdcx",
101 .group_description = "mdcx statistics",
102 .class_id = OSMO_STATS_CLASS_GLOBAL,
103 .num_ctr = ARRAY_SIZE(mgcp_mdcx_ctr_desc),
104 .ctr_desc = mgcp_mdcx_ctr_desc
105};
106
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100107const static struct rate_ctr_group_desc all_rtp_conn_rate_ctr_group_desc = {
108 .group_name_prefix = "all_rtp_conn",
109 .group_description = "aggregated statistics for all rtp connections",
110 .class_id = 1,
111 .num_ctr = ARRAY_SIZE(all_rtp_conn_rate_ctr_desc),
112 .ctr_desc = all_rtp_conn_rate_ctr_desc
113};
114
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200115static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *data);
116static struct msgb *handle_create_con(struct mgcp_parse_data *data);
117static struct msgb *handle_delete_con(struct mgcp_parse_data *data);
118static struct msgb *handle_modify_con(struct mgcp_parse_data *data);
119static struct msgb *handle_rsip(struct mgcp_parse_data *data);
120static struct msgb *handle_noti_req(struct mgcp_parse_data *data);
121
Philipp Maier87bd9be2017-08-22 16:35:41 +0200122/* Initalize transcoder */
123static int setup_rtp_processing(struct mgcp_endpoint *endp,
124 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200125{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200126 struct mgcp_config *cfg = endp->cfg;
127 struct mgcp_conn_rtp *conn_src = NULL;
128 struct mgcp_conn_rtp *conn_dst = conn;
129 struct mgcp_conn *_conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200130
Philipp Maier87bd9be2017-08-22 16:35:41 +0200131 if (conn->type != MGCP_RTP_DEFAULT) {
132 LOGP(DLMGCP, LOGL_NOTICE,
133 "endpoint:%x RTP-setup: Endpoint is not configured as RTP default, stopping here!\n",
134 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200135 return 0;
136 }
137
Philipp Maier87bd9be2017-08-22 16:35:41 +0200138 if (conn->conn->mode == MGCP_CONN_LOOPBACK) {
139 LOGP(DLMGCP, LOGL_NOTICE,
140 "endpoint:%x RTP-setup: Endpoint is in loopback mode, stopping here!\n",
141 ENDPOINT_NUMBER(endp));
142 return 0;
143 }
144
145 /* Find the "sister" connection */
146 llist_for_each_entry(_conn, &endp->conns, entry) {
147 if (_conn->id != conn->conn->id) {
148 conn_src = &_conn->u.rtp;
149 break;
150 }
151 }
152
Philipp Maieracc10352018-07-19 18:07:57 +0200153 return cfg->setup_rtp_processing_cb(endp, conn_dst, conn_src);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200154}
155
Philipp Maier87bd9be2017-08-22 16:35:41 +0200156/* array of function pointers for handling various
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200157 * messages. In the future this might be binary sorted
Philipp Maier87bd9be2017-08-22 16:35:41 +0200158 * for performance reasons. */
159static const struct mgcp_request mgcp_requests[] = {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200160 MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint")
Philipp Maier87bd9be2017-08-22 16:35:41 +0200161 MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
162 MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
163 MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
164 MGCP_REQUEST("RQNT", handle_noti_req, "NotificationRequest")
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200165
Philipp Maier87bd9be2017-08-22 16:35:41 +0200166 /* SPEC extension */
167 MGCP_REQUEST("RSIP", handle_rsip, "ReSetInProgress")
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200168};
169
Philipp Maier87bd9be2017-08-22 16:35:41 +0200170/* Helper function to allocate some memory for responses and retransmissions */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200171static struct msgb *mgcp_msgb_alloc(void)
172{
173 struct msgb *msg;
174 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
175 if (!msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200176 LOGP(DLMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200177
178 return msg;
179}
180
Philipp Maier87bd9be2017-08-22 16:35:41 +0200181/* Helper function for do_retransmission() and create_resp() */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200182static struct msgb *do_retransmission(const struct mgcp_endpoint *endp)
183{
184 struct msgb *msg = mgcp_msgb_alloc();
185 if (!msg)
186 return NULL;
187
188 msg->l2h = msgb_put(msg, strlen(endp->last_response));
189 memcpy(msg->l2h, endp->last_response, msgb_l2len(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200190 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Retransmitted response");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200191 return msg;
192}
193
194static struct msgb *create_resp(struct mgcp_endpoint *endp, int code,
195 const char *txt, const char *msg,
196 const char *trans, const char *param,
197 const char *sdp)
198{
199 int len;
200 struct msgb *res;
201
202 res = mgcp_msgb_alloc();
203 if (!res)
204 return NULL;
205
Philipp Maier87bd9be2017-08-22 16:35:41 +0200206 len = snprintf((char *)res->data, 2048, "%d %s%s%s\r\n%s",
207 code, trans, txt, param ? param : "", sdp ? sdp : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200208 if (len < 0) {
209 LOGP(DLMGCP, LOGL_ERROR, "Failed to sprintf MGCP response.\n");
210 msgb_free(res);
211 return NULL;
212 }
213
214 res->l2h = msgb_put(res, len);
215 LOGP(DLMGCP, LOGL_DEBUG, "Generated response: code=%d\n", code);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200216 mgcp_disp_msg(res->l2h, msgb_l2len(res), "Generated response");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200217
218 /*
219 * Remember the last transmission per endpoint.
220 */
221 if (endp) {
222 struct mgcp_trunk_config *tcfg = endp->tcfg;
223 talloc_free(endp->last_response);
224 talloc_free(endp->last_trans);
225 endp->last_trans = talloc_strdup(tcfg->endpoints, trans);
226 endp->last_response = talloc_strndup(tcfg->endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200227 (const char *)res->l2h,
228 msgb_l2len(res));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200229 }
230
231 return res;
232}
233
234static struct msgb *create_ok_resp_with_param(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200235 int code, const char *msg,
236 const char *trans,
237 const char *param)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200238{
239 return create_resp(endp, code, " OK", msg, trans, param, NULL);
240}
241
242static struct msgb *create_ok_response(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200243 int code, const char *msg,
244 const char *trans)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200245{
246 return create_ok_resp_with_param(endp, code, msg, trans, NULL);
247}
248
249static struct msgb *create_err_response(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200250 int code, const char *msg,
251 const char *trans)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200252{
253 return create_resp(endp, code, " FAIL", msg, trans, NULL, NULL);
254}
255
Philipp Maier55295f72018-01-15 14:00:28 +0100256/* Add MGCP parameters to a message buffer */
257static int add_params(struct msgb *msg, const struct mgcp_endpoint *endp,
258 const struct mgcp_conn_rtp *conn)
259{
260 int rc;
261
Philipp Maier7f0966c2018-01-17 18:18:12 +0100262 /* NOTE: Only in the virtual trunk we allow dynamic endpoint names */
Philipp Maier207ab512018-02-02 14:19:26 +0100263 if (endp->wildcarded_req
Philipp Maier7f0966c2018-01-17 18:18:12 +0100264 && endp->tcfg->trunk_type == MGCP_TRUNK_VIRTUAL) {
Philipp Maierdd4ede32018-02-01 17:52:52 +0100265 rc = msgb_printf(msg, "Z: %s%x@%s\r\n",
Philipp Maier7f0966c2018-01-17 18:18:12 +0100266 MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK,
267 ENDPOINT_NUMBER(endp), endp->cfg->domain);
Philipp Maier55295f72018-01-15 14:00:28 +0100268 if (rc < 0)
269 return -EINVAL;
270 }
271
Philipp Maierc3cfae22018-01-22 12:03:03 +0100272 rc = msgb_printf(msg, "I: %s\r\n", conn->conn->id);
Philipp Maier55295f72018-01-15 14:00:28 +0100273 if (rc < 0)
274 return -EINVAL;
275
276 return 0;
277}
278
Philipp Maier87bd9be2017-08-22 16:35:41 +0200279/* Format MGCP response string (with SDP attached) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200280static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200281 struct mgcp_conn_rtp *conn,
282 const char *msg,
Philipp Maier55295f72018-01-15 14:00:28 +0100283 const char *trans_id,
284 bool add_conn_params)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200285{
286 const char *addr = endp->cfg->local_ip;
Philipp Maier8970c492017-10-11 13:33:42 +0200287 struct msgb *sdp;
288 int rc;
289 struct msgb *result;
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100290 char osmux_extension[strlen("X-Osmux: 255") + 1];
Philipp Maier1cb1e382017-11-02 17:16:04 +0100291 char local_ip_addr[INET_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200292
Philipp Maier8970c492017-10-11 13:33:42 +0200293 sdp = msgb_alloc_headroom(4096, 128, "sdp record");
294 if (!sdp)
295 return NULL;
296
Philipp Maier1cb1e382017-11-02 17:16:04 +0100297 if (!addr) {
298 mgcp_get_local_addr(local_ip_addr, conn);
299 addr = local_ip_addr;
300 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200301
Philipp Maier87bd9be2017-08-22 16:35:41 +0200302 if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) {
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100303 sprintf(osmux_extension, "X-Osmux: %u", conn->osmux.cid);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200304 conn->osmux.state = OSMUX_STATE_ACTIVATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200305 } else {
306 osmux_extension[0] = '\0';
307 }
308
Philipp Maier55295f72018-01-15 14:00:28 +0100309 /* Attach optional connection parameters */
310 if (add_conn_params) {
311 rc = add_params(sdp, endp, conn);
312 if (rc < 0)
313 goto error;
314 }
315
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100316 /* Attach optional OSMUX parameters */
317 if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) {
Philipp Maierc3cfae22018-01-22 12:03:03 +0100318 rc = msgb_printf(sdp, "%s\r\n", osmux_extension);
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100319 if (rc < 0)
320 goto error;
321 }
322
323 /* Attach line break to separate the parameters from the SDP block */
Philipp Maierc3cfae22018-01-22 12:03:03 +0100324 rc = msgb_printf(sdp, "\r\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200325
Philipp Maier8970c492017-10-11 13:33:42 +0200326 rc = mgcp_write_response_sdp(endp, conn, sdp, addr);
327 if (rc < 0)
328 goto error;
329 result = create_resp(endp, 200, " OK", msg, trans_id, NULL, (char*) sdp->data);
330 msgb_free(sdp);
331 return result;
332error:
333 msgb_free(sdp);
334 return NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200335}
336
Philipp Maier87bd9be2017-08-22 16:35:41 +0200337/* Send out dummy packet to keep the connection open, if the connection is an
338 * osmux connection, send the dummy packet via OSMUX */
339static void send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200340{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200341 if (conn->osmux.state != OSMUX_STATE_DISABLED)
342 osmux_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200343 else
Philipp Maier87bd9be2017-08-22 16:35:41 +0200344 mgcp_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200345}
346
Philipp Maier87bd9be2017-08-22 16:35:41 +0200347/* handle incoming messages:
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200348 * - this can be a command (four letters, space, transaction id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200349 * - or a response (three numbers, space, transaction id) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200350struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
351{
352 struct mgcp_parse_data pdata;
Harald Weltee35eeae2017-12-28 13:47:37 +0100353 int rc, i, code, handled = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200354 struct msgb *resp = NULL;
355 char *data;
356
357 if (msgb_l2len(msg) < 4) {
358 LOGP(DLMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len);
359 return NULL;
360 }
361
362 if (mgcp_msg_terminate_nul(msg))
363 return NULL;
364
Philipp Maier87bd9be2017-08-22 16:35:41 +0200365 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Received message");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200366
Philipp Maier87bd9be2017-08-22 16:35:41 +0200367 /* attempt to treat it as a response */
368 if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200369 LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code);
370 return NULL;
371 }
372
373 msg->l3h = &msg->l2h[4];
374
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200375 /*
376 * Check for a duplicate message and respond.
377 */
378 memset(&pdata, 0, sizeof(pdata));
379 pdata.cfg = cfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200380 data = mgcp_strline((char *)msg->l3h, &pdata.save);
Harald Weltee35eeae2017-12-28 13:47:37 +0100381 rc = mgcp_parse_header(&pdata, data);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200382 if (pdata.endp && pdata.trans
Philipp Maier87bd9be2017-08-22 16:35:41 +0200383 && pdata.endp->last_trans
384 && strcmp(pdata.endp->last_trans, pdata.trans) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200385 return do_retransmission(pdata.endp);
386 }
387
Harald Welteabbb6b92017-12-28 13:13:50 +0100388 /* check for general parser failure */
Harald Weltee35eeae2017-12-28 13:47:37 +0100389 if (rc < 0) {
Harald Welteabbb6b92017-12-28 13:13:50 +0100390 LOGP(DLMGCP, LOGL_NOTICE, "%s: failed to find the endpoint\n", msg->l2h);
Harald Weltee35eeae2017-12-28 13:47:37 +0100391 return create_err_response(NULL, -rc, (const char *) msg->l2h, pdata.trans);
Harald Welteabbb6b92017-12-28 13:13:50 +0100392 }
393
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200394 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200395 if (strncmp
396 (mgcp_requests[i].name, (const char *)&msg->l2h[0],
397 4) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200398 handled = 1;
399 resp = mgcp_requests[i].handle_request(&pdata);
400 break;
401 }
402 }
403
404 if (!handled)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200405 LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n",
406 &msg->l2h[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200407
408 return resp;
409}
410
Philipp Maier87bd9be2017-08-22 16:35:41 +0200411/* AUEP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200412static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p)
413{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200414 LOGP(DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n");
Harald Welteabbb6b92017-12-28 13:13:50 +0100415 return create_ok_response(p->endp, 200, "AUEP", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200416}
417
Harald Welte1d1b98f2017-12-25 10:03:40 +0100418/* Try to find a free port by attempting to bind on it. Also handle the
Philipp Maier87bd9be2017-08-22 16:35:41 +0200419 * counter that points on the next free port. Since we have a pointer
Philipp Maierb38fb892018-05-22 13:52:21 +0200420 * to the next free port, binding should in work on the first attempt in
421 * general. In case of failure the next port is tryed until the whole port
422 * range is tryed once. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200423static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200424{
425 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200426 struct mgcp_rtp_end *end;
427 struct mgcp_port_range *range;
Philipp Maierb38fb892018-05-22 13:52:21 +0200428 unsigned int tries;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200429
Philipp Maier87bd9be2017-08-22 16:35:41 +0200430 OSMO_ASSERT(conn);
431 end = &conn->end;
432 OSMO_ASSERT(end);
433
434 range = &endp->cfg->net_ports;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200435
436 /* attempt to find a port */
Philipp Maierb38fb892018-05-22 13:52:21 +0200437 tries = (range->range_end - range->range_start) / 2;
438 for (i = 0; i < tries; ++i) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200439 int rc;
440
441 if (range->last_port >= range->range_end)
442 range->last_port = range->range_start;
443
Philipp Maier87bd9be2017-08-22 16:35:41 +0200444 rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200445
446 range->last_port += 2;
447 if (rc == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200448 return 0;
449 }
450
451 }
452
Philipp Maier87bd9be2017-08-22 16:35:41 +0200453 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maierb38fb892018-05-22 13:52:21 +0200454 "Allocating a RTP/RTCP port failed %u times 0x%x.\n",
455 tries, ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200456 return -1;
457}
458
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200459/*! Helper function for check_local_cx_options() to get a pointer of the next
460 * lco option identifier
461 * \param[in] lco string
462 * \returns pointer to the beginning of the LCO identifier, NULL on failure */
463char *get_lco_identifier(const char *options)
464{
465 char *ptr;
466 unsigned int count = 0;
467
468 /* Jump to the end of the lco identifier */
469 ptr = strstr(options, ":");
470 if (!ptr)
471 return NULL;
472
473 /* Walk backwards until the pointer points to the beginning of the
474 * lco identifier. We know that we stand at the beginning when we
475 * are either at the beginning of the memory or see a space or
476 * comma. (this is tolerant, it will accept a:10, b:11 as well as
477 * a:10,b:11) */
478 while (1) {
479 /* Endless loop protection */
480 if (count > 10000)
481 return NULL;
482 else if (ptr < options || *ptr == ' ' || *ptr == ',') {
483 ptr++;
484 break;
485 }
486 ptr--;
487 count++;
488 }
489
490 /* Check if we got any result */
491 if (*ptr == ':')
492 return NULL;
493
494 return ptr;
495}
496
497/*! Check the LCO option. This function checks for multiple appearence of LCO
498 * options, which is illegal
499 * \param[in] ctx talloc context
500 * \param[in] lco string
501 * \returns 0 on success, -1 on failure */
502int check_local_cx_options(void *ctx, const char *options)
503{
504 int i;
505 char *options_copy;
506 char *lco_identifier;
507 char *lco_identifier_end;
508 char *next_lco_identifier;
509
510 char **lco_seen;
511 unsigned int lco_seen_n = 0;
512
513 if (!options)
514 return -1;
515
516 lco_seen =
517 (char **)talloc_zero_size(ctx, strlen(options) * sizeof(char *));
518 options_copy = talloc_strdup(ctx, options);
519 lco_identifier = options_copy;
520
521 do {
522 /* Move the lco_identifier pointer to the beginning of the
523 * current lco option identifier */
524 lco_identifier = get_lco_identifier(lco_identifier);
525 if (!lco_identifier)
526 goto error;
527
528 /* Look ahead to the next LCO option early, since we
529 * will parse destructively */
530 next_lco_identifier = strstr(lco_identifier + 1, ",");
531
532 /* Pinch off the end of the lco field identifier name
533 * and see if we still got something, also check if
534 * there is some value after the colon. */
535 lco_identifier_end = strstr(lco_identifier, ":");
536 if (!lco_identifier_end)
537 goto error;
538 if (*(lco_identifier_end + 1) == ' '
539 || *(lco_identifier_end + 1) == ','
540 || *(lco_identifier_end + 1) == '\0')
541 goto error;
542 *lco_identifier_end = '\0';
543 if (strlen(lco_identifier) == 0)
544 goto error;
545
546 /* Check if we have already seen the current field identifier
547 * before. If yes, we must bail, an LCO must only appear once
548 * in the LCO string */
549 for (i = 0; i < lco_seen_n; i++) {
550 if (strcmp(lco_seen[i], lco_identifier) == 0)
551 goto error;
552 }
553 lco_seen[lco_seen_n] = lco_identifier;
554 lco_seen_n++;
555
556 /* The first identifier must always be found at the beginnning
557 * of the LCO string */
558 if (lco_seen[0] != options_copy)
559 goto error;
560
561 /* Go to the next lco option */
562 lco_identifier = next_lco_identifier;
563 } while (lco_identifier);
564
565 talloc_free(lco_seen);
566 talloc_free(options_copy);
567 return 0;
568error:
569 talloc_free(lco_seen);
570 talloc_free(options_copy);
571 return -1;
572}
573
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200574/* Set the LCO from a string (see RFC 3435).
Harald Welte1d1b98f2017-12-25 10:03:40 +0100575 * The string is stored in the 'string' field. A NULL string is handled exactly
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200576 * like an empty string, the 'string' field is never NULL after this function
577 * has been called. */
Philipp Maiera390d0b2018-01-31 17:30:19 +0100578static int set_local_cx_options(void *ctx, struct mgcp_lco *lco,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200579 const char *options)
580{
581 char *p_opt, *a_opt;
Philipp Maier8dbc9ed2018-10-26 14:50:25 +0200582 char codec[17];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200583
Philipp Maier604410c2018-06-06 10:02:16 +0200584 if (!options)
585 return 0;
586 if (strlen(options) == 0)
587 return 0;
588
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200589 /* Make sure the encoding of the LCO is consistant before we proceed */
590 if (check_local_cx_options(ctx, options) != 0) {
591 LOGP(DLMGCP, LOGL_ERROR,
592 "local CX options: Internal inconsistency in Local Connection Options!\n");
593 return 524;
594 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200595
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200596 talloc_free(lco->string);
597 lco->string = talloc_strdup(ctx, options);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200598
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200599 p_opt = strstr(lco->string, "p:");
600 if (p_opt && sscanf(p_opt, "p:%d-%d",
601 &lco->pkt_period_min, &lco->pkt_period_max) == 1)
602 lco->pkt_period_max = lco->pkt_period_min;
603
Philipp Maierbc0346e2018-06-07 09:52:16 +0200604 /* FIXME: LCO also supports the negotiation of more then one codec.
605 * (e.g. a:PCMU;G726-32) But this implementation only supports a single
606 * codec only. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200607 a_opt = strstr(lco->string, "a:");
Philipp Maier8dbc9ed2018-10-26 14:50:25 +0200608 if (a_opt && sscanf(a_opt, "a:%16[^,]", codec) == 1) {
Philipp Maier604410c2018-06-06 10:02:16 +0200609 talloc_free(lco->codec);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200610 lco->codec = talloc_strdup(ctx, codec);
Philipp Maier604410c2018-06-06 10:02:16 +0200611 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200612
613 LOGP(DLMGCP, LOGL_DEBUG,
614 "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n",
615 lco->pkt_period_max, lco->codec);
Philipp Maiera390d0b2018-01-31 17:30:19 +0100616
617 /* Check if the packetization fits the 20ms raster */
618 if (lco->pkt_period_min % 20 && lco->pkt_period_max % 20) {
619 LOGP(DLMGCP, LOGL_ERROR,
620 "local CX options: packetization interval is not a multiple of 20ms!\n");
621 return 535;
622 }
623
624 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200625}
626
627void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
628 struct mgcp_rtp_end *rtp)
629{
630 struct mgcp_trunk_config *tcfg = endp->tcfg;
631
632 int patch_ssrc = expect_ssrc_change && tcfg->force_constant_ssrc;
633
634 rtp->force_aligned_timing = tcfg->force_aligned_timing;
635 rtp->force_constant_ssrc = patch_ssrc ? 1 : 0;
636
637 LOGP(DLMGCP, LOGL_DEBUG,
638 "Configuring RTP endpoint: local port %d%s%s\n",
639 ntohs(rtp->rtp_port),
640 rtp->force_aligned_timing ? ", force constant timing" : "",
641 rtp->force_constant_ssrc ? ", force constant ssrc" : "");
642}
643
644uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
645 struct mgcp_rtp_end *rtp)
646{
647 int f = 0;
648
649 /* Get the number of frames per channel and packet */
650 if (rtp->frames_per_packet)
651 f = rtp->frames_per_packet;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200652 else if (rtp->packet_duration_ms && rtp->codec->frame_duration_num) {
653 int den = 1000 * rtp->codec->frame_duration_num;
654 f = (rtp->packet_duration_ms * rtp->codec->frame_duration_den +
Philipp Maier87bd9be2017-08-22 16:35:41 +0200655 den / 2)
656 / den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200657 }
658
Philipp Maierbc0346e2018-06-07 09:52:16 +0200659 return rtp->codec->rate * f * rtp->codec->frame_duration_num /
660 rtp->codec->frame_duration_den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200661}
662
663static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line)
664{
665 if (!endp->cfg->osmux_init) {
666 if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) {
667 LOGP(DLMGCP, LOGL_ERROR, "Cannot init OSMUX\n");
668 return -1;
669 }
670 LOGP(DLMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n");
671 }
672
673 return mgcp_parse_osmux_cid(line);
674}
675
Philipp Maierbc0346e2018-06-07 09:52:16 +0200676/* Process codec information contained in CRCX/MDCX */
677static int handle_codec_info(struct mgcp_conn_rtp *conn,
678 struct mgcp_parse_data *p, int have_sdp, bool crcx)
679{
680 struct mgcp_endpoint *endp = p->endp;
681 int rc;
682 char *cmd;
683
684 if (crcx)
685 cmd = "CRCX";
686 else
687 cmd = "MDCX";
688
689 /* Collect codec information */
690 if (have_sdp) {
691 /* If we have SDP, we ignore the local connection options and
692 * use only the SDP information. */
693 mgcp_codec_reset_all(conn);
694 rc = mgcp_parse_sdp_data(endp, conn, p);
695 if (rc != 0) {
696 LOGP(DLMGCP, LOGL_ERROR,
697 "%s: endpoint:%x sdp not parseable\n", cmd,
698 ENDPOINT_NUMBER(endp));
699
700 /* See also RFC 3661: Protocol error */
701 return 510;
702 }
703 } else if (endp->local_options.codec) {
704 /* When no SDP is available, we use the codec information from
705 * the local connection options (if present) */
706 mgcp_codec_reset_all(conn);
707 rc = mgcp_codec_add(conn, PTYPE_UNDEFINED, endp->local_options.codec);
708 if (rc != 0)
709 goto error;
710 }
711
712 /* Make sure we always set a sane default codec */
713 if (conn->end.codecs_assigned == 0) {
714 /* When SDP and/or LCO did not supply any codec information,
715 * than it makes sense to pick a sane default: (payload-type 0,
716 * PCMU), see also: OS#2658 */
717 mgcp_codec_reset_all(conn);
718 rc = mgcp_codec_add(conn, 0, NULL);
719 if (rc != 0)
720 goto error;
721 }
722
723 /* Make codec decision */
724 if (mgcp_codec_decide(conn) != 0)
725 goto error;
726
727 return 0;
728
729error:
730 LOGP(DLMGCP, LOGL_ERROR,
731 "%s: endpoint:0x%x codec negotiation failure\n", cmd,
732 ENDPOINT_NUMBER(endp));
733
734 /* See also RFC 3661: Codec negotiation failure */
735 return 534;
736}
737
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200738static bool parse_x_osmo_ign(struct mgcp_endpoint *endp, char *line)
739{
740 char *saveptr = NULL;
741
742 if (strncmp(line, MGCP_X_OSMO_IGN_HEADER, strlen(MGCP_X_OSMO_IGN_HEADER)))
743 return false;
744 line += strlen(MGCP_X_OSMO_IGN_HEADER);
745
746 while (1) {
747 char *token = strtok_r(line, " ", &saveptr);
748 line = NULL;
749 if (!token)
750 break;
751
752 if (!strcmp(token, "C"))
753 endp->x_osmo_ign |= MGCP_X_OSMO_IGN_CALLID;
754 else
Pau Espin Pedrol9ecceb62018-10-16 15:35:58 +0200755 LOGP(DLMGCP, LOGL_ERROR, "endpoint 0x%x: received unknown X-Osmo-IGN item '%s'\n",
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200756 ENDPOINT_NUMBER(endp), token);
757 }
758
759 return true;
760}
761
Philipp Maier87bd9be2017-08-22 16:35:41 +0200762/* CRCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200763static struct msgb *handle_create_con(struct mgcp_parse_data *p)
764{
Stefan Sperling1e174872018-10-25 18:36:10 +0200765 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200766 struct mgcp_endpoint *endp = p->endp;
Stefan Sperling9270e912018-10-29 14:10:00 +0100767 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_crcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200768 int error_code = 400;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200769 const char *local_options = NULL;
770 const char *callid = NULL;
771 const char *mode = NULL;
772 char *line;
773 int have_sdp = 0, osmux_cid = -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200774 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100775 struct mgcp_conn *_conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200776 char conn_name[512];
Philipp Maiera390d0b2018-01-31 17:30:19 +0100777 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200778
779 LOGP(DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200780
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200781 /* parse CallID C: and LocalParameters L: */
782 for_each_line(line, p->save) {
783 if (!mgcp_check_param(endp, line))
784 continue;
785
786 switch (line[0]) {
787 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200788 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200789 break;
790 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200791 callid = (const char *)line + 3;
792 break;
793 case 'I':
Philipp Maierffd75e42017-11-22 11:44:50 +0100794 /* It is illegal to send a connection identifier
795 * together with a CRCX, the MGW will assign the
796 * connection identifier by itself on CRCX */
Stefan Sperling9270e912018-10-29 14:10:00 +0100797 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_BAD_ACTION]);
Philipp Maierffd75e42017-11-22 11:44:50 +0100798 return create_err_response(NULL, 523, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200799 break;
800 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200801 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200802 break;
803 case 'X':
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200804 if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
805 /* If osmux is disabled, just skip setting it up */
806 if (!p->endp->cfg->osmux)
807 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200808 osmux_cid = mgcp_osmux_setup(endp, line);
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200809 break;
810 }
811
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200812 if (parse_x_osmo_ign(endp, line))
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200813 break;
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200814
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200815 /* Ignore unknown X-headers */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200816 break;
817 case '\0':
818 have_sdp = 1;
819 goto mgcp_header_done;
820 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200821 LOGP(DLMGCP, LOGL_NOTICE,
822 "CRCX: endpoint:%x unhandled option: '%c'/%d\n",
823 ENDPOINT_NUMBER(endp), *line, *line);
Stefan Sperling9270e912018-10-29 14:10:00 +0100824 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +0100825 return create_err_response(NULL, 539, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200826 break;
827 }
828 }
829
830mgcp_header_done:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200831 /* Check parameters */
832 if (!callid) {
833 LOGP(DLMGCP, LOGL_ERROR,
834 "CRCX: endpoint:%x insufficient parameters, missing callid\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200835 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +0100836 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_MISSING_CALLID]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100837 return create_err_response(endp, 516, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200838 }
839
Philipp Maier87bd9be2017-08-22 16:35:41 +0200840 if (!mode) {
841 LOGP(DLMGCP, LOGL_ERROR,
842 "CRCX: endpoint:%x insufficient parameters, missing mode\n",
843 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +0100844 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_MODE]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100845 return create_err_response(endp, 517, "CRCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200846 }
847
Philipp Maier87bd9be2017-08-22 16:35:41 +0200848 /* Check if we are able to accept the creation of another connection */
849 if (llist_count(&endp->conns) >= endp->type->max_conns) {
850 LOGP(DLMGCP, LOGL_ERROR,
851 "CRCX: endpoint:%x endpoint full, max. %i connections allowed!\n",
852 endp->type->max_conns, ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200853 if (tcfg->force_realloc) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200854 /* There is no more room for a connection, make some
855 * room by blindly tossing the oldest of the two two
856 * connections */
857 mgcp_conn_free_oldest(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200858 } else {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200859 /* There is no more room for a connection, leave
860 * everything as it is and return with an error */
Stefan Sperling9270e912018-10-29 14:10:00 +0100861 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_LIMIT_EXCEEDED]);
Harald Weltee35eeae2017-12-28 13:47:37 +0100862 return create_err_response(endp, 540, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200863 }
864 }
865
Philipp Maier87bd9be2017-08-22 16:35:41 +0200866 /* Check if this endpoint already serves a call, if so, check if the
867 * callids match up so that we are sure that this is our call */
868 if (endp->callid && mgcp_verify_call_id(endp, callid)) {
869 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100870 "CRCX: endpoint:0x%x allready seized by other call (%s)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200871 ENDPOINT_NUMBER(endp), endp->callid);
872 if (tcfg->force_realloc)
873 /* This is not our call, toss everything by releasing
874 * the entire endpoint. (rude!) */
Philipp Maier1355d7e2018-02-01 14:30:06 +0100875 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200876 else {
877 /* This is not our call, leave everything as it is and
878 * return with an error. */
Stefan Sperling9270e912018-10-29 14:10:00 +0100879 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_UNKNOWN_CALLID]);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200880 return create_err_response(endp, 400, "CRCX", p->trans);
881 }
882 }
883
884 /* Set the callid, creation of another connection will only be possible
Harald Welte1d1b98f2017-12-25 10:03:40 +0100885 * when the callid matches up. (Connections are distinguished by their
Philipp Maier87bd9be2017-08-22 16:35:41 +0200886 * connection ids) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200887 endp->callid = talloc_strdup(tcfg->endpoints, callid);
888
Philipp Maierffd75e42017-11-22 11:44:50 +0100889 snprintf(conn_name, sizeof(conn_name), "%s", callid);
890 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP, conn_name);
891 if (!_conn) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200892 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100893 "CRCX: endpoint:0x%x unable to allocate RTP connection\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200894 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +0100895 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_ALLOC_CONN]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200896 goto error2;
897
Philipp Maier87bd9be2017-08-22 16:35:41 +0200898 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100899 conn = mgcp_conn_get_rtp(endp, _conn->id);
900 OSMO_ASSERT(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200901
902 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
903 error_code = 517;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100904 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_MODE]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200905 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200906 }
907
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200908 /* Annotate Osmux circuit ID and set it to negotiating state until this
Philipp Maier87bd9be2017-08-22 16:35:41 +0200909 * is fully set up from the dummy load. */
910 conn->osmux.state = OSMUX_STATE_DISABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200911 if (osmux_cid >= 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200912 conn->osmux.cid = osmux_cid;
913 conn->osmux.state = OSMUX_STATE_NEGOTIATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200914 } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) {
915 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100916 "CRCX: endpoint:0x%x osmux only and no osmux offered\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200917 ENDPOINT_NUMBER(endp));
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100918 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_OSMUX]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200919 goto error2;
920 }
921
Philipp Maierbc0346e2018-06-07 09:52:16 +0200922 /* Set local connection options, if present */
923 if (local_options) {
924 rc = set_local_cx_options(endp->tcfg->endpoints,
925 &endp->local_options, local_options);
926 if (rc != 0) {
927 LOGP(DLMGCP, LOGL_ERROR,
928 "CRCX: endpoint:%x inavlid local connection options!\n",
929 ENDPOINT_NUMBER(endp));
930 error_code = rc;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100931 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS]);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200932 goto error2;
933 }
934 }
935
936 /* Handle codec information and decide for a suitable codec */
937 rc = handle_codec_info(conn, p, have_sdp, true);
938 mgcp_codec_summary(conn);
939 if (rc) {
940 error_code = rc;
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100941 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_CODEC_NEGOTIATION]);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200942 goto error2;
943 }
944
Philipp Maier87bd9be2017-08-22 16:35:41 +0200945 conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints,
946 tcfg->audio_fmtp_extra);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200947
Philipp Maier87bd9be2017-08-22 16:35:41 +0200948 if (p->cfg->force_ptime) {
949 conn->end.packet_duration_ms = p->cfg->force_ptime;
950 conn->end.force_output_ptime = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200951 }
952
Philipp Maier1cb1e382017-11-02 17:16:04 +0100953 mgcp_rtp_end_config(endp, 0, &conn->end);
954
Philipp Maierc3cc6542018-02-02 12:58:42 +0100955 /* check connection mode setting */
956 if (conn->conn->mode != MGCP_CONN_LOOPBACK
957 && conn->conn->mode != MGCP_CONN_RECV_ONLY
958 && conn->end.rtp_port == 0) {
959 LOGP(DLMGCP, LOGL_ERROR,
960 "CRCX: endpoint:%x selected connection mode type requires an opposite end!\n",
961 ENDPOINT_NUMBER(endp));
962 error_code = 527;
Stefan Sperling9270e912018-10-29 14:10:00 +0100963 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC]);
Philipp Maierc3cc6542018-02-02 12:58:42 +0100964 goto error2;
965 }
966
Philipp Maier1cb1e382017-11-02 17:16:04 +0100967 if (allocate_port(endp, conn) != 0) {
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100968 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_BIND_PORT]);
Philipp Maier1cb1e382017-11-02 17:16:04 +0100969 goto error2;
970 }
971
Philipp Maier87bd9be2017-08-22 16:35:41 +0200972 if (setup_rtp_processing(endp, conn) != 0) {
973 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100974 "CRCX: endpoint:0x%x could not start RTP processing!\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200975 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +0100976 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_START_RTP]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200977 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200978 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200979
980 /* policy CB */
981 if (p->cfg->policy_cb) {
982 int rc;
983 rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200984 MGCP_ENDP_CRCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200985 switch (rc) {
986 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200987 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100988 "CRCX: endpoint:0x%x CRCX rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200989 ENDPOINT_NUMBER(endp));
Philipp Maier1355d7e2018-02-01 14:30:06 +0100990 mgcp_endp_release(endp);
Stefan Sperling9270e912018-10-29 14:10:00 +0100991 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200992 return create_err_response(endp, 400, "CRCX", p->trans);
993 break;
994 case MGCP_POLICY_DEFER:
995 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200996 return NULL;
997 break;
998 case MGCP_POLICY_CONT:
999 /* just continue */
1000 break;
1001 }
1002 }
1003
Philipp Maier87bd9be2017-08-22 16:35:41 +02001004 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001005 "CRCX: endpoint:0x%x Creating connection: CI: %s port: %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001006 ENDPOINT_NUMBER(endp), conn->conn->id, conn->end.local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001007 if (p->cfg->change_cb)
1008 p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX);
1009
Philipp Maiere726d4f2017-11-01 10:41:34 +01001010 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
1011 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001012 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
Philipp Maiere726d4f2017-11-01 10:41:34 +01001013 && tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001014 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001015
Philipp Maier87bd9be2017-08-22 16:35:41 +02001016 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001017 "CRCX: endpoint:0x%x connection successfully created\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001018 ENDPOINT_NUMBER(endp));
Stefan Sperling9270e912018-10-29 14:10:00 +01001019 rate_ctr_inc(&rate_ctrs->ctr[MGCP_CRCX_SUCCESS]);
Philipp Maier55295f72018-01-15 14:00:28 +01001020 return create_response_with_sdp(endp, conn, "CRCX", p->trans, true);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001021error2:
Philipp Maier1355d7e2018-02-01 14:30:06 +01001022 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001023 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier3c8ccb62018-06-04 09:59:24 +02001024 "CRCX: endpoint:0x%x unable to create connection\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001025 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001026 return create_err_response(endp, error_code, "CRCX", p->trans);
1027}
1028
Philipp Maierbc0346e2018-06-07 09:52:16 +02001029
1030
1031
1032
Philipp Maier87bd9be2017-08-22 16:35:41 +02001033/* MDCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001034static struct msgb *handle_modify_con(struct mgcp_parse_data *p)
1035{
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001036 struct mgcp_trunk_config *tcfg = p->endp->tcfg;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001037 struct mgcp_endpoint *endp = p->endp;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001038 struct rate_ctr_group *rate_ctrs = tcfg->mgcp_mdcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001039 int error_code = 500;
1040 int silent = 0;
1041 int have_sdp = 0;
1042 char *line;
1043 const char *local_options = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001044 const char *mode = NULL;
1045 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier01d24a32017-11-21 17:26:09 +01001046 const char *conn_id = NULL;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001047 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001048
1049 LOGP(DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001050
Philipp Maier5656fbf2018-02-02 14:41:58 +01001051 /* Prohibit wildcarded requests */
1052 if (endp->wildcarded_req) {
1053 LOGP(DLMGCP, LOGL_ERROR,
1054 "MDCX: endpoint:0x%x wildcarded endpoint names not supported.\n",
1055 ENDPOINT_NUMBER(endp));
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001056 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_WILDCARD]);
Philipp Maier5656fbf2018-02-02 14:41:58 +01001057 return create_err_response(endp, 507, "MDCX", p->trans);
1058 }
1059
Philipp Maier87bd9be2017-08-22 16:35:41 +02001060 if (llist_count(&endp->conns) <= 0) {
1061 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001062 "MDCX: endpoint:0x%x endpoint is not holding a connection.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001063 ENDPOINT_NUMBER(endp));
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001064 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_CONN]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001065 return create_err_response(endp, 400, "MDCX", p->trans);
1066 }
1067
1068 for_each_line(line, p->save) {
1069 if (!mgcp_check_param(endp, line))
1070 continue;
1071
1072 switch (line[0]) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001073 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +01001074 if (mgcp_verify_call_id(endp, line + 3) != 0) {
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001075 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CALLID]);
Harald Weltee35eeae2017-12-28 13:47:37 +01001076 error_code = 516;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001077 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001078 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001079 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001080 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001081 conn_id = (const char *)line + 3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001082 if ((error_code = mgcp_verify_ci(endp, conn_id))) {
1083 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CONNID]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001084 goto error3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001085 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001086 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001087 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001088 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001089 break;
1090 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001091 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001092 break;
1093 case 'Z':
1094 silent = strcmp("noanswer", line + 3) == 0;
1095 break;
1096 case '\0':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001097 have_sdp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001098 goto mgcp_header_done;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001099 break;
1100 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001101 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001102 "MDCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001103 ENDPOINT_NUMBER(endp), line[0], line[0]);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001104 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_UNHANDLED_PARAM]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001105 return create_err_response(NULL, 539, "MDCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001106 break;
1107 }
1108 }
1109
Philipp Maier87bd9be2017-08-22 16:35:41 +02001110mgcp_header_done:
Philipp Maier01d24a32017-11-21 17:26:09 +01001111 if (!conn_id) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001112 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001113 "MDCX: endpoint:0x%x insufficient parameters, missing ci (connectionIdentifier)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001114 ENDPOINT_NUMBER(endp));
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001115 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_CONNID]);
Harald Weltee35eeae2017-12-28 13:47:37 +01001116 return create_err_response(endp, 515, "MDCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001117 }
1118
1119 conn = mgcp_conn_get_rtp(endp, conn_id);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001120 if (!conn) {
1121 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_CONN_NOT_FOUND]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001122 return create_err_response(endp, 400, "MDCX", p->trans);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001123 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001124
1125 if (mode) {
1126 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001127 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_MODE]);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001128 error_code = 517;
1129 goto error3;
1130 }
1131 } else
1132 conn->conn->mode = conn->conn->mode_orig;
1133
Philipp Maierbc0346e2018-06-07 09:52:16 +02001134 /* Set local connection options, if present */
1135 if (local_options) {
1136 rc = set_local_cx_options(endp->tcfg->endpoints,
1137 &endp->local_options, local_options);
1138 if (rc != 0) {
1139 LOGP(DLMGCP, LOGL_ERROR,
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001140 "MDCX: endpoint:%x invalid local connection options!\n",
Philipp Maierbc0346e2018-06-07 09:52:16 +02001141 ENDPOINT_NUMBER(endp));
1142 error_code = rc;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001143 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS]);
Philipp Maierbc0346e2018-06-07 09:52:16 +02001144 goto error3;
1145 }
1146 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001147
Philipp Maierbc0346e2018-06-07 09:52:16 +02001148 /* Handle codec information and decide for a suitable codec */
1149 rc = handle_codec_info(conn, p, have_sdp, false);
1150 mgcp_codec_summary(conn);
1151 if (rc) {
Philipp Maieraf07f662018-02-02 11:34:02 +01001152 error_code = rc;
1153 goto error3;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001154 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001155
Philipp Maierc3cc6542018-02-02 12:58:42 +01001156 /* check connection mode setting */
1157 if (conn->conn->mode != MGCP_CONN_LOOPBACK
1158 && conn->conn->mode != MGCP_CONN_RECV_ONLY
1159 && conn->end.rtp_port == 0) {
1160 LOGP(DLMGCP, LOGL_ERROR,
1161 "MDCX: endpoint:%x selected connection mode type requires an opposite end!\n",
1162 ENDPOINT_NUMBER(endp));
1163 error_code = 527;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001164 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC]);
Philipp Maierc3cc6542018-02-02 12:58:42 +01001165 goto error3;
1166 }
1167
Philipp Maierbc0346e2018-06-07 09:52:16 +02001168
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001169 if (setup_rtp_processing(endp, conn) != 0) {
1170 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_START_RTP]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001171 goto error3;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001172 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001173
Philipp Maier87bd9be2017-08-22 16:35:41 +02001174
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001175 /* policy CB */
1176 if (p->cfg->policy_cb) {
1177 int rc;
1178 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001179 MGCP_ENDP_MDCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001180 switch (rc) {
1181 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001182 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001183 "MDCX: endpoint:0x%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001184 ENDPOINT_NUMBER(endp));
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001185 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_REJECTED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001186 if (silent)
1187 goto out_silent;
1188 return create_err_response(endp, 400, "MDCX", p->trans);
1189 break;
1190 case MGCP_POLICY_DEFER:
1191 /* stop processing */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001192 LOGP(DLMGCP, LOGL_DEBUG,
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001193 "MDCX: endpoint:0x%x deferred by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001194 ENDPOINT_NUMBER(endp));
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001195 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_FAIL_DEFERRED_BY_POLICY]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001196 return NULL;
1197 break;
1198 case MGCP_POLICY_CONT:
1199 /* just continue */
1200 break;
1201 }
1202 }
1203
Philipp Maier87bd9be2017-08-22 16:35:41 +02001204 mgcp_rtp_end_config(endp, 1, &conn->end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001205
1206 /* modify */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001207 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001208 "MDCX: endpoint:0x%x modified conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001209 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001210 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001211 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1212 MGCP_ENDP_MDCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001213
Philipp Maiere726d4f2017-11-01 10:41:34 +01001214 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
1215 OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1216 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
1217 && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001218 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001219
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001220 rate_ctr_inc(&rate_ctrs->ctr[MGCP_MDCX_SUCCESS]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001221 if (silent)
1222 goto out_silent;
1223
Philipp Maier87bd9be2017-08-22 16:35:41 +02001224 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001225 "MDCX: endpoint:0x%x connection successfully modified\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001226 ENDPOINT_NUMBER(endp));
Philipp Maier55295f72018-01-15 14:00:28 +01001227 return create_response_with_sdp(endp, conn, "MDCX", p->trans, false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001228error3:
1229 return create_err_response(endp, error_code, "MDCX", p->trans);
1230
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001231out_silent:
Philipp Maier230e4fc2017-11-28 09:38:45 +01001232 LOGP(DLMGCP, LOGL_DEBUG, "MDCX: endpoint:0x%x silent exit\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001233 ENDPOINT_NUMBER(endp));
1234 return NULL;
1235}
1236
Philipp Maier87bd9be2017-08-22 16:35:41 +02001237/* DLCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001238static struct msgb *handle_delete_con(struct mgcp_parse_data *p)
1239{
1240 struct mgcp_endpoint *endp = p->endp;
1241 int error_code = 400;
1242 int silent = 0;
1243 char *line;
1244 char stats[1048];
Philipp Maier01d24a32017-11-21 17:26:09 +01001245 const char *conn_id = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001246 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001247
1248 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001249 "DLCX: endpoint:0x%x deleting connection ...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001250 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001251
Philipp Maier5656fbf2018-02-02 14:41:58 +01001252 /* Prohibit wildcarded requests */
1253 if (endp->wildcarded_req) {
1254 LOGP(DLMGCP, LOGL_ERROR,
1255 "DLCX: endpoint:0x%x wildcarded endpoint names not supported.\n",
1256 ENDPOINT_NUMBER(endp));
1257 return create_err_response(endp, 507, "DLCX", p->trans);
1258 }
1259
Philipp Maier87bd9be2017-08-22 16:35:41 +02001260 if (llist_count(&endp->conns) <= 0) {
1261 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001262 "DLCX: endpoint:0x%x endpoint is not holding a connection.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001263 ENDPOINT_NUMBER(endp));
Harald Weltee35eeae2017-12-28 13:47:37 +01001264 return create_err_response(endp, 515, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001265 }
1266
1267 for_each_line(line, p->save) {
1268 if (!mgcp_check_param(endp, line))
1269 continue;
1270
1271 switch (line[0]) {
1272 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +01001273 if (mgcp_verify_call_id(endp, line + 3) != 0) {
1274 error_code = 516;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001275 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001276 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001277 break;
1278 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001279 conn_id = (const char *)line + 3;
Neels Hofmeyreb72ff02018-09-03 23:00:07 +02001280 if ((error_code = mgcp_verify_ci(endp, conn_id)))
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001281 goto error3;
1282 break;
1283 case 'Z':
1284 silent = strcmp("noanswer", line + 3) == 0;
1285 break;
1286 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001287 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001288 "DLCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001289 ENDPOINT_NUMBER(endp), line[0], line[0]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001290 return create_err_response(NULL, 539, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001291 break;
1292 }
1293 }
1294
1295 /* policy CB */
1296 if (p->cfg->policy_cb) {
1297 int rc;
1298 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001299 MGCP_ENDP_DLCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001300 switch (rc) {
1301 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001302 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001303 "DLCX: endpoint:0x%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001304 ENDPOINT_NUMBER(endp));
1305 if (silent)
1306 goto out_silent;
1307 return create_err_response(endp, 400, "DLCX", p->trans);
1308 break;
1309 case MGCP_POLICY_DEFER:
1310 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001311 return NULL;
1312 break;
1313 case MGCP_POLICY_CONT:
1314 /* just continue */
1315 break;
1316 }
1317 }
1318
Philipp Maierf4c0e372017-10-11 16:06:45 +02001319 /* When no connection id is supplied, we will interpret this as a
1320 * wildcarded DLCX and drop all connections at once. (See also
1321 * RFC3435 Section F.7) */
Philipp Maier01d24a32017-11-21 17:26:09 +01001322 if (!conn_id) {
Philipp Maierf4c0e372017-10-11 16:06:45 +02001323 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001324 "DLCX: endpoint:0x%x missing ci (connectionIdentifier), will remove all connections at once\n",
Philipp Maierf4c0e372017-10-11 16:06:45 +02001325 ENDPOINT_NUMBER(endp));
1326
Philipp Maier1355d7e2018-02-01 14:30:06 +01001327 mgcp_endp_release(endp);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001328
1329 /* Note: In this case we do not return any statistics,
1330 * as we assume that the client is not interested in
1331 * this case. */
1332 return create_ok_response(endp, 200, "DLCX", p->trans);
1333 }
1334
Philipp Maierf4c0e372017-10-11 16:06:45 +02001335 /* Find the connection */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001336 conn = mgcp_conn_get_rtp(endp, conn_id);
1337 if (!conn)
1338 goto error3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001339
Philipp Maier87bd9be2017-08-22 16:35:41 +02001340 /* save the statistics of the current connection */
1341 mgcp_format_stats(stats, sizeof(stats), conn->conn);
1342
1343 /* delete connection */
Philipp Maier230e4fc2017-11-28 09:38:45 +01001344 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x deleting conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001345 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
1346 mgcp_conn_free(endp, conn_id);
1347 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001348 "DLCX: endpoint:0x%x connection successfully deleted\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001349 ENDPOINT_NUMBER(endp));
1350
1351 /* When all connections are closed, the endpoint will be released
1352 * in order to be ready to be used by another call. */
1353 if (llist_count(&endp->conns) <= 0) {
Philipp Maier1355d7e2018-02-01 14:30:06 +01001354 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001355 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001356 "DLCX: endpoint:0x%x endpoint released\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001357 ENDPOINT_NUMBER(endp));
1358 }
1359
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001360 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001361 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1362 MGCP_ENDP_DLCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001363
1364 if (silent)
1365 goto out_silent;
1366 return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats);
1367
1368error3:
1369 return create_err_response(endp, error_code, "DLCX", p->trans);
1370
1371out_silent:
Philipp Maier230e4fc2017-11-28 09:38:45 +01001372 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x silent exit\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001373 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001374 return NULL;
1375}
1376
Philipp Maier87bd9be2017-08-22 16:35:41 +02001377/* RSIP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001378static struct msgb *handle_rsip(struct mgcp_parse_data *p)
1379{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001380 /* TODO: Also implement the resetting of a specific endpoint
1381 * to make mgcp_send_reset_ep() work. Currently this will call
1382 * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1
1383 * to make read_call_agent() reset all endpoints when called
1384 * next time. In order to selectively reset endpoints some
1385 * mechanism to distinguish which endpoint shall be resetted
1386 * is needed */
1387
1388 LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n");
1389
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001390 if (p->cfg->reset_cb)
1391 p->cfg->reset_cb(p->endp->tcfg);
1392 return NULL;
1393}
1394
1395static char extract_tone(const char *line)
1396{
1397 const char *str = strstr(line, "D/");
1398 if (!str)
1399 return CHAR_MAX;
1400
1401 return str[2];
1402}
1403
Philipp Maier87bd9be2017-08-22 16:35:41 +02001404/* This can request like DTMF detection and forward, fax detection... it
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001405 * can also request when the notification should be send and such. We don't
Philipp Maier87bd9be2017-08-22 16:35:41 +02001406 * do this right now. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001407static struct msgb *handle_noti_req(struct mgcp_parse_data *p)
1408{
1409 int res = 0;
1410 char *line;
1411 char tone = CHAR_MAX;
1412
Philipp Maier87bd9be2017-08-22 16:35:41 +02001413 LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n");
1414
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001415 for_each_line(line, p->save) {
1416 switch (line[0]) {
1417 case 'S':
1418 tone = extract_tone(line);
1419 break;
1420 }
1421 }
1422
1423 /* we didn't see a signal request with a tone */
1424 if (tone == CHAR_MAX)
1425 return create_ok_response(p->endp, 200, "RQNT", p->trans);
1426
1427 if (p->cfg->rqnt_cb)
1428 res = p->cfg->rqnt_cb(p->endp, tone);
1429
1430 return res == 0 ?
Philipp Maier87bd9be2017-08-22 16:35:41 +02001431 create_ok_response(p->endp, 200, "RQNT", p->trans) :
1432 create_err_response(p->endp, res, "RQNT", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001433}
1434
Philipp Maier87bd9be2017-08-22 16:35:41 +02001435/* Connection keepalive timer, will take care that dummy packets are send
Harald Welte1d1b98f2017-12-25 10:03:40 +01001436 * regularly, so that NAT connections stay open */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001437static void mgcp_keepalive_timer_cb(void *_tcfg)
1438{
1439 struct mgcp_trunk_config *tcfg = _tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001440 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001441 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001442
Philipp Maiere726d4f2017-11-01 10:41:34 +01001443 LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001444 tcfg->trunk_nr);
1445
Philipp Maiere726d4f2017-11-01 10:41:34 +01001446 /* Do not accept invalid configuration values
1447 * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and
1448 * values greater 0 */
1449 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1450
1451 /* The dummy packet functionality has been disabled, we will exit
1452 * immediately, no further timer is scheduled, which means we will no
1453 * longer send dummy packets even when we did before */
1454 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER)
1455 return;
1456
1457 /* In cases where only one dummy packet is sent, we do not need
1458 * the timer since the functions that handle the CRCX and MDCX are
1459 * triggering the sending of the dummy packet. So we behave like in
1460 * the MGCP_KEEPALIVE_NEVER case */
1461 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001462 return;
1463
Philipp Maier87bd9be2017-08-22 16:35:41 +02001464 /* Send walk over all endpoints and send out dummy packets through
1465 * every connection present on each endpoint */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001466 for (i = 1; i < tcfg->number_endpoints; ++i) {
1467 struct mgcp_endpoint *endp = &tcfg->endpoints[i];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001468 llist_for_each_entry(conn, &endp->conns, entry) {
1469 if (conn->mode == MGCP_CONN_RECV_ONLY)
1470 send_dummy(endp, &conn->u.rtp);
1471 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001472 }
1473
Philipp Maiere726d4f2017-11-01 10:41:34 +01001474 /* Schedule the keepalive timer for the next round */
1475 LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001476 tcfg->trunk_nr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001477 osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval,
1478 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001479}
1480
1481void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval)
1482{
1483 tcfg->keepalive_interval = interval;
1484 osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg);
1485
1486 if (interval <= 0)
1487 osmo_timer_del(&tcfg->keepalive_timer);
1488 else
1489 osmo_timer_schedule(&tcfg->keepalive_timer,
1490 tcfg->keepalive_interval, 0);
1491}
1492
Stefan Sperling1e174872018-10-25 18:36:10 +02001493static int free_rate_counter_group(struct rate_ctr_group *rate_ctr_group)
1494{
1495 rate_ctr_group_free(rate_ctr_group);
1496 return 0;
1497}
1498
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001499static void alloc_mgcp_rate_counters(struct mgcp_trunk_config *trunk, void *ctx)
Stefan Sperling1e174872018-10-25 18:36:10 +02001500{
1501 /* FIXME: Each new rate counter group requires a unique index. At the
1502 * moment we generate an index using a counter, but perhaps there is
1503 * a better way of assigning indices? */
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001504 static unsigned int crcx_rate_ctr_index = 0;
1505 static unsigned int mdcx_rate_ctr_index = 0;
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001506 static unsigned int all_rtp_conn_rate_ctr_index = 0;
Stefan Sperling1e174872018-10-25 18:36:10 +02001507
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001508 if (trunk->mgcp_crcx_ctr_group == NULL) {
1509 trunk->mgcp_crcx_ctr_group = rate_ctr_group_alloc(ctx, &mgcp_crcx_ctr_group_desc, crcx_rate_ctr_index);
1510 OSMO_ASSERT(trunk->mgcp_crcx_ctr_group);
1511 talloc_set_destructor(trunk->mgcp_crcx_ctr_group, free_rate_counter_group);
1512 crcx_rate_ctr_index++;
1513 }
1514 if (trunk->mgcp_mdcx_ctr_group == NULL) {
1515 trunk->mgcp_mdcx_ctr_group = rate_ctr_group_alloc(ctx, &mgcp_mdcx_ctr_group_desc, mdcx_rate_ctr_index);
1516 OSMO_ASSERT(trunk->mgcp_mdcx_ctr_group);
1517 talloc_set_destructor(trunk->mgcp_mdcx_ctr_group, free_rate_counter_group);
1518 mdcx_rate_ctr_index++;
1519 }
Stefan Sperlingba25eab2018-10-30 14:32:31 +01001520 if (trunk->all_rtp_conn_stats == NULL) {
1521 trunk->all_rtp_conn_stats = rate_ctr_group_alloc(ctx, &all_rtp_conn_rate_ctr_group_desc,
1522 all_rtp_conn_rate_ctr_index);
1523 OSMO_ASSERT(trunk->all_rtp_conn_stats);
1524 talloc_set_destructor(trunk->all_rtp_conn_stats, free_rate_counter_group);
1525 all_rtp_conn_rate_ctr_index++;
1526 }
Stefan Sperling1e174872018-10-25 18:36:10 +02001527}
1528
Philipp Maier87bd9be2017-08-22 16:35:41 +02001529/*! allocate configuration with default values.
1530 * (called once at startup by main function) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001531struct mgcp_config *mgcp_config_alloc(void)
1532{
1533 struct mgcp_config *cfg;
1534
1535 cfg = talloc_zero(NULL, struct mgcp_config);
1536 if (!cfg) {
1537 LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n");
1538 return NULL;
1539 }
1540
Philipp Maier12943ea2018-01-17 15:40:25 +01001541 osmo_strlcpy(cfg->domain, "mgw", sizeof(cfg->domain));
1542
Philipp Maier87bd9be2017-08-22 16:35:41 +02001543 cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START;
1544 cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END;
1545 cfg->net_ports.last_port = cfg->net_ports.range_start;
1546
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001547 cfg->source_port = 2427;
1548 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
1549 cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
1550
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001551 cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
1552 cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
1553
1554 cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
1555
1556 /* default trunk handling */
1557 cfg->trunk.cfg = cfg;
1558 cfg->trunk.trunk_nr = 0;
1559 cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
1560 cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
1561 cfg->trunk.audio_payload = 126;
1562 cfg->trunk.audio_send_ptime = 1;
1563 cfg->trunk.audio_send_name = 1;
1564 cfg->trunk.omit_rtcp = 0;
1565 mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001566 alloc_mgcp_rate_counters(&cfg->trunk, cfg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001567
1568 INIT_LLIST_HEAD(&cfg->trunks);
1569
1570 return cfg;
1571}
1572
Philipp Maier87bd9be2017-08-22 16:35:41 +02001573/*! allocate configuration with default values.
1574 * (called once at startup by VTY)
1575 * \param[in] cfg mgcp configuration
1576 * \param[in] nr trunk number
1577 * \returns pointer to allocated trunk configuration */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001578struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
1579{
1580 struct mgcp_trunk_config *trunk;
1581
1582 trunk = talloc_zero(cfg, struct mgcp_trunk_config);
1583 if (!trunk) {
1584 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n");
1585 return NULL;
1586 }
1587
1588 trunk->cfg = cfg;
1589 trunk->trunk_type = MGCP_TRUNK_E1;
1590 trunk->trunk_nr = nr;
1591 trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
1592 trunk->audio_payload = 126;
1593 trunk->audio_send_ptime = 1;
1594 trunk->audio_send_name = 1;
Philipp Maierfcd06552017-11-10 17:32:22 +01001595 trunk->vty_number_endpoints = 33;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001596 trunk->omit_rtcp = 0;
1597 mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001598 alloc_mgcp_rate_counters(trunk, trunk);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001599 llist_add_tail(&trunk->entry, &cfg->trunks);
Stefan Sperling1e174872018-10-25 18:36:10 +02001600
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001601 return trunk;
1602}
1603
Philipp Maier87bd9be2017-08-22 16:35:41 +02001604/*! get trunk configuration by trunk number (index).
1605 * \param[in] cfg mgcp configuration
1606 * \param[in] index trunk number
1607 * \returns pointer to trunk configuration, NULL on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001608struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
1609{
1610 struct mgcp_trunk_config *trunk;
1611
1612 llist_for_each_entry(trunk, &cfg->trunks, entry)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001613 if (trunk->trunk_nr == index)
1614 return trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001615
1616 return NULL;
1617}
1618
Philipp Maier87bd9be2017-08-22 16:35:41 +02001619/*! allocate endpoints and set default values.
1620 * (called once at startup by VTY)
1621 * \param[in] tcfg trunk configuration
1622 * \returns 0 on success, -1 on failure */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001623int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
1624{
1625 int i;
1626
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001627 tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001628 sizeof(struct mgcp_endpoint),
Philipp Maierfcd06552017-11-10 17:32:22 +01001629 tcfg->vty_number_endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001630 "endpoints");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001631 if (!tcfg->endpoints)
1632 return -1;
1633
Philipp Maierfcd06552017-11-10 17:32:22 +01001634 for (i = 0; i < tcfg->vty_number_endpoints; ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001635 INIT_LLIST_HEAD(&tcfg->endpoints[i].conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001636 tcfg->endpoints[i].cfg = tcfg->cfg;
1637 tcfg->endpoints[i].tcfg = tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001638
1639 /* NOTE: Currently all endpoints are of type RTP, this will
1640 * change when new variations are implemented */
1641 tcfg->endpoints[i].type = &ep_typeset.rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001642 }
1643
Philipp Maierfcd06552017-11-10 17:32:22 +01001644 tcfg->number_endpoints = tcfg->vty_number_endpoints;
Stefan Sperlingaa823bf2018-10-29 14:51:41 +01001645 alloc_mgcp_rate_counters(tcfg, tcfg->cfg);
Stefan Sperling1e174872018-10-25 18:36:10 +02001646
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001647 return 0;
1648}
1649
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001650static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1651{
1652 return write(cfg->gw_fd.bfd.fd, buf, len);
1653}
1654
Philipp Maier87bd9be2017-08-22 16:35:41 +02001655/*! Reset all endpoints by sending RSIP message to self.
1656 * (called by VTY)
1657 * \param[in] endp trunk endpoint
1658 * \param[in] endpoint number
1659 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001660int mgcp_send_reset_all(struct mgcp_config *cfg)
1661{
Philipp Maier12943ea2018-01-17 15:40:25 +01001662 char buf[MGCP_ENDPOINT_MAXLEN + 128];
1663 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001664 int rc;
1665
Philipp Maier12943ea2018-01-17 15:40:25 +01001666 len = snprintf(buf, sizeof(buf),
1667 "RSIP 1 *@%s MGCP 1.0\r\n", cfg->domain);
1668 if (len < 0)
1669 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001670
Philipp Maier12943ea2018-01-17 15:40:25 +01001671 rc = send_agent(cfg, buf, len);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001672 if (rc <= 0)
1673 return -1;
1674
1675 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001676}
1677
Philipp Maier87bd9be2017-08-22 16:35:41 +02001678/*! Reset a single endpoint by sending RSIP message to self.
1679 * (called by VTY)
1680 * \param[in] endp trunk endpoint
1681 * \param[in] endpoint number
1682 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001683int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
1684{
Philipp Maier12943ea2018-01-17 15:40:25 +01001685 char buf[MGCP_ENDPOINT_MAXLEN + 128];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001686 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001687 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001688
1689 len = snprintf(buf, sizeof(buf),
Philipp Maier12943ea2018-01-17 15:40:25 +01001690 "RSIP 39 %x@%s MGCP 1.0\r\n", endpoint, endp->cfg->domain);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001691 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001692 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001693
Philipp Maier87bd9be2017-08-22 16:35:41 +02001694 rc = send_agent(endp->cfg, buf, len);
1695 if (rc <= 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001696 return -1;
1697
Philipp Maier87bd9be2017-08-22 16:35:41 +02001698 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001699}