blob: 7f4a7b82d911ab8d8fa319a1d15acd458681c0bf [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>
35
Philipp Maier87bd9be2017-08-22 16:35:41 +020036#include <osmocom/mgcp/mgcp.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020037#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020038#include <osmocom/mgcp/mgcp_internal.h>
39#include <osmocom/mgcp/mgcp_stat.h>
40#include <osmocom/mgcp/mgcp_msg.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010041#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maier8970c492017-10-11 13:33:42 +020042#include <osmocom/mgcp/mgcp_sdp.h>
Philipp Maierbc0346e2018-06-07 09:52:16 +020043#include <osmocom/mgcp/mgcp_codec.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020044
45struct mgcp_request {
46 char *name;
Philipp Maier87bd9be2017-08-22 16:35:41 +020047 struct msgb *(*handle_request) (struct mgcp_parse_data * data);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020048 char *debug_name;
49};
50
51#define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \
52 { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME },
53
54static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *data);
55static struct msgb *handle_create_con(struct mgcp_parse_data *data);
56static struct msgb *handle_delete_con(struct mgcp_parse_data *data);
57static struct msgb *handle_modify_con(struct mgcp_parse_data *data);
58static struct msgb *handle_rsip(struct mgcp_parse_data *data);
59static struct msgb *handle_noti_req(struct mgcp_parse_data *data);
60
Philipp Maier87bd9be2017-08-22 16:35:41 +020061/* Initalize transcoder */
62static int setup_rtp_processing(struct mgcp_endpoint *endp,
63 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020064{
Philipp Maier87bd9be2017-08-22 16:35:41 +020065 struct mgcp_config *cfg = endp->cfg;
66 struct mgcp_conn_rtp *conn_src = NULL;
67 struct mgcp_conn_rtp *conn_dst = conn;
68 struct mgcp_conn *_conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020069
Philipp Maier87bd9be2017-08-22 16:35:41 +020070 if (conn->type != MGCP_RTP_DEFAULT) {
71 LOGP(DLMGCP, LOGL_NOTICE,
72 "endpoint:%x RTP-setup: Endpoint is not configured as RTP default, stopping here!\n",
73 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020074 return 0;
75 }
76
Philipp Maier87bd9be2017-08-22 16:35:41 +020077 if (conn->conn->mode == MGCP_CONN_LOOPBACK) {
78 LOGP(DLMGCP, LOGL_NOTICE,
79 "endpoint:%x RTP-setup: Endpoint is in loopback mode, stopping here!\n",
80 ENDPOINT_NUMBER(endp));
81 return 0;
82 }
83
84 /* Find the "sister" connection */
85 llist_for_each_entry(_conn, &endp->conns, entry) {
86 if (_conn->id != conn->conn->id) {
87 conn_src = &_conn->u.rtp;
88 break;
89 }
90 }
91
Philipp Maieracc10352018-07-19 18:07:57 +020092 return cfg->setup_rtp_processing_cb(endp, conn_dst, conn_src);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020093}
94
Philipp Maier87bd9be2017-08-22 16:35:41 +020095/* array of function pointers for handling various
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020096 * messages. In the future this might be binary sorted
Philipp Maier87bd9be2017-08-22 16:35:41 +020097 * for performance reasons. */
98static const struct mgcp_request mgcp_requests[] = {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020099 MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint")
Philipp Maier87bd9be2017-08-22 16:35:41 +0200100 MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
101 MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
102 MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
103 MGCP_REQUEST("RQNT", handle_noti_req, "NotificationRequest")
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200104
Philipp Maier87bd9be2017-08-22 16:35:41 +0200105 /* SPEC extension */
106 MGCP_REQUEST("RSIP", handle_rsip, "ReSetInProgress")
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200107};
108
Philipp Maier87bd9be2017-08-22 16:35:41 +0200109/* Helper function to allocate some memory for responses and retransmissions */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200110static struct msgb *mgcp_msgb_alloc(void)
111{
112 struct msgb *msg;
113 msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
114 if (!msg)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200115 LOGP(DLMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200116
117 return msg;
118}
119
Philipp Maier87bd9be2017-08-22 16:35:41 +0200120/* Helper function for do_retransmission() and create_resp() */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200121static struct msgb *do_retransmission(const struct mgcp_endpoint *endp)
122{
123 struct msgb *msg = mgcp_msgb_alloc();
124 if (!msg)
125 return NULL;
126
127 msg->l2h = msgb_put(msg, strlen(endp->last_response));
128 memcpy(msg->l2h, endp->last_response, msgb_l2len(msg));
Philipp Maier87bd9be2017-08-22 16:35:41 +0200129 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Retransmitted response");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200130 return msg;
131}
132
133static struct msgb *create_resp(struct mgcp_endpoint *endp, int code,
134 const char *txt, const char *msg,
135 const char *trans, const char *param,
136 const char *sdp)
137{
138 int len;
139 struct msgb *res;
140
141 res = mgcp_msgb_alloc();
142 if (!res)
143 return NULL;
144
Philipp Maier87bd9be2017-08-22 16:35:41 +0200145 len = snprintf((char *)res->data, 2048, "%d %s%s%s\r\n%s",
146 code, trans, txt, param ? param : "", sdp ? sdp : "");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200147 if (len < 0) {
148 LOGP(DLMGCP, LOGL_ERROR, "Failed to sprintf MGCP response.\n");
149 msgb_free(res);
150 return NULL;
151 }
152
153 res->l2h = msgb_put(res, len);
154 LOGP(DLMGCP, LOGL_DEBUG, "Generated response: code=%d\n", code);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200155 mgcp_disp_msg(res->l2h, msgb_l2len(res), "Generated response");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200156
157 /*
158 * Remember the last transmission per endpoint.
159 */
160 if (endp) {
161 struct mgcp_trunk_config *tcfg = endp->tcfg;
162 talloc_free(endp->last_response);
163 talloc_free(endp->last_trans);
164 endp->last_trans = talloc_strdup(tcfg->endpoints, trans);
165 endp->last_response = talloc_strndup(tcfg->endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200166 (const char *)res->l2h,
167 msgb_l2len(res));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200168 }
169
170 return res;
171}
172
173static struct msgb *create_ok_resp_with_param(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200174 int code, const char *msg,
175 const char *trans,
176 const char *param)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200177{
178 return create_resp(endp, code, " OK", msg, trans, param, NULL);
179}
180
181static struct msgb *create_ok_response(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200182 int code, const char *msg,
183 const char *trans)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200184{
185 return create_ok_resp_with_param(endp, code, msg, trans, NULL);
186}
187
188static struct msgb *create_err_response(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200189 int code, const char *msg,
190 const char *trans)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200191{
192 return create_resp(endp, code, " FAIL", msg, trans, NULL, NULL);
193}
194
Philipp Maier55295f72018-01-15 14:00:28 +0100195/* Add MGCP parameters to a message buffer */
196static int add_params(struct msgb *msg, const struct mgcp_endpoint *endp,
197 const struct mgcp_conn_rtp *conn)
198{
199 int rc;
200
Philipp Maier7f0966c2018-01-17 18:18:12 +0100201 /* NOTE: Only in the virtual trunk we allow dynamic endpoint names */
Philipp Maier207ab512018-02-02 14:19:26 +0100202 if (endp->wildcarded_req
Philipp Maier7f0966c2018-01-17 18:18:12 +0100203 && endp->tcfg->trunk_type == MGCP_TRUNK_VIRTUAL) {
Philipp Maierdd4ede32018-02-01 17:52:52 +0100204 rc = msgb_printf(msg, "Z: %s%x@%s\r\n",
Philipp Maier7f0966c2018-01-17 18:18:12 +0100205 MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK,
206 ENDPOINT_NUMBER(endp), endp->cfg->domain);
Philipp Maier55295f72018-01-15 14:00:28 +0100207 if (rc < 0)
208 return -EINVAL;
209 }
210
Philipp Maierc3cfae22018-01-22 12:03:03 +0100211 rc = msgb_printf(msg, "I: %s\r\n", conn->conn->id);
Philipp Maier55295f72018-01-15 14:00:28 +0100212 if (rc < 0)
213 return -EINVAL;
214
215 return 0;
216}
217
Philipp Maier87bd9be2017-08-22 16:35:41 +0200218/* Format MGCP response string (with SDP attached) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200219static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200220 struct mgcp_conn_rtp *conn,
221 const char *msg,
Philipp Maier55295f72018-01-15 14:00:28 +0100222 const char *trans_id,
223 bool add_conn_params)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200224{
225 const char *addr = endp->cfg->local_ip;
Philipp Maier8970c492017-10-11 13:33:42 +0200226 struct msgb *sdp;
227 int rc;
228 struct msgb *result;
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100229 char osmux_extension[strlen("X-Osmux: 255") + 1];
Philipp Maier1cb1e382017-11-02 17:16:04 +0100230 char local_ip_addr[INET_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200231
Philipp Maier8970c492017-10-11 13:33:42 +0200232 sdp = msgb_alloc_headroom(4096, 128, "sdp record");
233 if (!sdp)
234 return NULL;
235
Philipp Maier1cb1e382017-11-02 17:16:04 +0100236 if (!addr) {
237 mgcp_get_local_addr(local_ip_addr, conn);
238 addr = local_ip_addr;
239 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200240
Philipp Maier87bd9be2017-08-22 16:35:41 +0200241 if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) {
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100242 sprintf(osmux_extension, "X-Osmux: %u", conn->osmux.cid);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200243 conn->osmux.state = OSMUX_STATE_ACTIVATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200244 } else {
245 osmux_extension[0] = '\0';
246 }
247
Philipp Maier55295f72018-01-15 14:00:28 +0100248 /* Attach optional connection parameters */
249 if (add_conn_params) {
250 rc = add_params(sdp, endp, conn);
251 if (rc < 0)
252 goto error;
253 }
254
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100255 /* Attach optional OSMUX parameters */
256 if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) {
Philipp Maierc3cfae22018-01-22 12:03:03 +0100257 rc = msgb_printf(sdp, "%s\r\n", osmux_extension);
Philipp Maier3cbfb8a2018-01-22 11:39:59 +0100258 if (rc < 0)
259 goto error;
260 }
261
262 /* Attach line break to separate the parameters from the SDP block */
Philipp Maierc3cfae22018-01-22 12:03:03 +0100263 rc = msgb_printf(sdp, "\r\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200264
Philipp Maier8970c492017-10-11 13:33:42 +0200265 rc = mgcp_write_response_sdp(endp, conn, sdp, addr);
266 if (rc < 0)
267 goto error;
268 result = create_resp(endp, 200, " OK", msg, trans_id, NULL, (char*) sdp->data);
269 msgb_free(sdp);
270 return result;
271error:
272 msgb_free(sdp);
273 return NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200274}
275
Philipp Maier87bd9be2017-08-22 16:35:41 +0200276/* Send out dummy packet to keep the connection open, if the connection is an
277 * osmux connection, send the dummy packet via OSMUX */
278static void send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200279{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200280 if (conn->osmux.state != OSMUX_STATE_DISABLED)
281 osmux_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200282 else
Philipp Maier87bd9be2017-08-22 16:35:41 +0200283 mgcp_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200284}
285
Philipp Maier87bd9be2017-08-22 16:35:41 +0200286/* handle incoming messages:
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200287 * - this can be a command (four letters, space, transaction id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200288 * - or a response (three numbers, space, transaction id) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200289struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
290{
291 struct mgcp_parse_data pdata;
Harald Weltee35eeae2017-12-28 13:47:37 +0100292 int rc, i, code, handled = 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200293 struct msgb *resp = NULL;
294 char *data;
295
296 if (msgb_l2len(msg) < 4) {
297 LOGP(DLMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len);
298 return NULL;
299 }
300
301 if (mgcp_msg_terminate_nul(msg))
302 return NULL;
303
Philipp Maier87bd9be2017-08-22 16:35:41 +0200304 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Received message");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200305
Philipp Maier87bd9be2017-08-22 16:35:41 +0200306 /* attempt to treat it as a response */
307 if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200308 LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code);
309 return NULL;
310 }
311
312 msg->l3h = &msg->l2h[4];
313
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200314 /*
315 * Check for a duplicate message and respond.
316 */
317 memset(&pdata, 0, sizeof(pdata));
318 pdata.cfg = cfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200319 data = mgcp_strline((char *)msg->l3h, &pdata.save);
Harald Weltee35eeae2017-12-28 13:47:37 +0100320 rc = mgcp_parse_header(&pdata, data);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200321 if (pdata.endp && pdata.trans
Philipp Maier87bd9be2017-08-22 16:35:41 +0200322 && pdata.endp->last_trans
323 && strcmp(pdata.endp->last_trans, pdata.trans) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200324 return do_retransmission(pdata.endp);
325 }
326
Harald Welteabbb6b92017-12-28 13:13:50 +0100327 /* check for general parser failure */
Harald Weltee35eeae2017-12-28 13:47:37 +0100328 if (rc < 0) {
Harald Welteabbb6b92017-12-28 13:13:50 +0100329 LOGP(DLMGCP, LOGL_NOTICE, "%s: failed to find the endpoint\n", msg->l2h);
Harald Weltee35eeae2017-12-28 13:47:37 +0100330 return create_err_response(NULL, -rc, (const char *) msg->l2h, pdata.trans);
Harald Welteabbb6b92017-12-28 13:13:50 +0100331 }
332
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200333 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200334 if (strncmp
335 (mgcp_requests[i].name, (const char *)&msg->l2h[0],
336 4) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200337 handled = 1;
338 resp = mgcp_requests[i].handle_request(&pdata);
339 break;
340 }
341 }
342
343 if (!handled)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200344 LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n",
345 &msg->l2h[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200346
347 return resp;
348}
349
Philipp Maier87bd9be2017-08-22 16:35:41 +0200350/* AUEP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200351static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p)
352{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200353 LOGP(DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n");
Harald Welteabbb6b92017-12-28 13:13:50 +0100354 return create_ok_response(p->endp, 200, "AUEP", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200355}
356
Harald Welte1d1b98f2017-12-25 10:03:40 +0100357/* Try to find a free port by attempting to bind on it. Also handle the
Philipp Maier87bd9be2017-08-22 16:35:41 +0200358 * counter that points on the next free port. Since we have a pointer
Philipp Maierb38fb892018-05-22 13:52:21 +0200359 * to the next free port, binding should in work on the first attempt in
360 * general. In case of failure the next port is tryed until the whole port
361 * range is tryed once. */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200362static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200363{
364 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200365 struct mgcp_rtp_end *end;
366 struct mgcp_port_range *range;
Philipp Maierb38fb892018-05-22 13:52:21 +0200367 unsigned int tries;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200368
Philipp Maier87bd9be2017-08-22 16:35:41 +0200369 OSMO_ASSERT(conn);
370 end = &conn->end;
371 OSMO_ASSERT(end);
372
373 range = &endp->cfg->net_ports;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200374
375 /* attempt to find a port */
Philipp Maierb38fb892018-05-22 13:52:21 +0200376 tries = (range->range_end - range->range_start) / 2;
377 for (i = 0; i < tries; ++i) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200378 int rc;
379
380 if (range->last_port >= range->range_end)
381 range->last_port = range->range_start;
382
Philipp Maier87bd9be2017-08-22 16:35:41 +0200383 rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200384
385 range->last_port += 2;
386 if (rc == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200387 return 0;
388 }
389
390 }
391
Philipp Maier87bd9be2017-08-22 16:35:41 +0200392 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maierb38fb892018-05-22 13:52:21 +0200393 "Allocating a RTP/RTCP port failed %u times 0x%x.\n",
394 tries, ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200395 return -1;
396}
397
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200398/*! Helper function for check_local_cx_options() to get a pointer of the next
399 * lco option identifier
400 * \param[in] lco string
401 * \returns pointer to the beginning of the LCO identifier, NULL on failure */
402char *get_lco_identifier(const char *options)
403{
404 char *ptr;
405 unsigned int count = 0;
406
407 /* Jump to the end of the lco identifier */
408 ptr = strstr(options, ":");
409 if (!ptr)
410 return NULL;
411
412 /* Walk backwards until the pointer points to the beginning of the
413 * lco identifier. We know that we stand at the beginning when we
414 * are either at the beginning of the memory or see a space or
415 * comma. (this is tolerant, it will accept a:10, b:11 as well as
416 * a:10,b:11) */
417 while (1) {
418 /* Endless loop protection */
419 if (count > 10000)
420 return NULL;
421 else if (ptr < options || *ptr == ' ' || *ptr == ',') {
422 ptr++;
423 break;
424 }
425 ptr--;
426 count++;
427 }
428
429 /* Check if we got any result */
430 if (*ptr == ':')
431 return NULL;
432
433 return ptr;
434}
435
436/*! Check the LCO option. This function checks for multiple appearence of LCO
437 * options, which is illegal
438 * \param[in] ctx talloc context
439 * \param[in] lco string
440 * \returns 0 on success, -1 on failure */
441int check_local_cx_options(void *ctx, const char *options)
442{
443 int i;
444 char *options_copy;
445 char *lco_identifier;
446 char *lco_identifier_end;
447 char *next_lco_identifier;
448
449 char **lco_seen;
450 unsigned int lco_seen_n = 0;
451
452 if (!options)
453 return -1;
454
455 lco_seen =
456 (char **)talloc_zero_size(ctx, strlen(options) * sizeof(char *));
457 options_copy = talloc_strdup(ctx, options);
458 lco_identifier = options_copy;
459
460 do {
461 /* Move the lco_identifier pointer to the beginning of the
462 * current lco option identifier */
463 lco_identifier = get_lco_identifier(lco_identifier);
464 if (!lco_identifier)
465 goto error;
466
467 /* Look ahead to the next LCO option early, since we
468 * will parse destructively */
469 next_lco_identifier = strstr(lco_identifier + 1, ",");
470
471 /* Pinch off the end of the lco field identifier name
472 * and see if we still got something, also check if
473 * there is some value after the colon. */
474 lco_identifier_end = strstr(lco_identifier, ":");
475 if (!lco_identifier_end)
476 goto error;
477 if (*(lco_identifier_end + 1) == ' '
478 || *(lco_identifier_end + 1) == ','
479 || *(lco_identifier_end + 1) == '\0')
480 goto error;
481 *lco_identifier_end = '\0';
482 if (strlen(lco_identifier) == 0)
483 goto error;
484
485 /* Check if we have already seen the current field identifier
486 * before. If yes, we must bail, an LCO must only appear once
487 * in the LCO string */
488 for (i = 0; i < lco_seen_n; i++) {
489 if (strcmp(lco_seen[i], lco_identifier) == 0)
490 goto error;
491 }
492 lco_seen[lco_seen_n] = lco_identifier;
493 lco_seen_n++;
494
495 /* The first identifier must always be found at the beginnning
496 * of the LCO string */
497 if (lco_seen[0] != options_copy)
498 goto error;
499
500 /* Go to the next lco option */
501 lco_identifier = next_lco_identifier;
502 } while (lco_identifier);
503
504 talloc_free(lco_seen);
505 talloc_free(options_copy);
506 return 0;
507error:
508 talloc_free(lco_seen);
509 talloc_free(options_copy);
510 return -1;
511}
512
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200513/* Set the LCO from a string (see RFC 3435).
Harald Welte1d1b98f2017-12-25 10:03:40 +0100514 * The string is stored in the 'string' field. A NULL string is handled exactly
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200515 * like an empty string, the 'string' field is never NULL after this function
516 * has been called. */
Philipp Maiera390d0b2018-01-31 17:30:19 +0100517static int set_local_cx_options(void *ctx, struct mgcp_lco *lco,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200518 const char *options)
519{
520 char *p_opt, *a_opt;
521 char codec[9];
522
Philipp Maier604410c2018-06-06 10:02:16 +0200523 if (!options)
524 return 0;
525 if (strlen(options) == 0)
526 return 0;
527
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200528 /* Make sure the encoding of the LCO is consistant before we proceed */
529 if (check_local_cx_options(ctx, options) != 0) {
530 LOGP(DLMGCP, LOGL_ERROR,
531 "local CX options: Internal inconsistency in Local Connection Options!\n");
532 return 524;
533 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200534
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200535 talloc_free(lco->string);
536 lco->string = talloc_strdup(ctx, options);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200537
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200538 p_opt = strstr(lco->string, "p:");
539 if (p_opt && sscanf(p_opt, "p:%d-%d",
540 &lco->pkt_period_min, &lco->pkt_period_max) == 1)
541 lco->pkt_period_max = lco->pkt_period_min;
542
Philipp Maierbc0346e2018-06-07 09:52:16 +0200543 /* FIXME: LCO also supports the negotiation of more then one codec.
544 * (e.g. a:PCMU;G726-32) But this implementation only supports a single
545 * codec only. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200546 a_opt = strstr(lco->string, "a:");
Philipp Maier604410c2018-06-06 10:02:16 +0200547 if (a_opt && sscanf(a_opt, "a:%8[^,]", codec) == 1) {
548 talloc_free(lco->codec);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200549 lco->codec = talloc_strdup(ctx, codec);
Philipp Maier604410c2018-06-06 10:02:16 +0200550 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200551
552 LOGP(DLMGCP, LOGL_DEBUG,
553 "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n",
554 lco->pkt_period_max, lco->codec);
Philipp Maiera390d0b2018-01-31 17:30:19 +0100555
556 /* Check if the packetization fits the 20ms raster */
557 if (lco->pkt_period_min % 20 && lco->pkt_period_max % 20) {
558 LOGP(DLMGCP, LOGL_ERROR,
559 "local CX options: packetization interval is not a multiple of 20ms!\n");
560 return 535;
561 }
562
563 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200564}
565
566void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
567 struct mgcp_rtp_end *rtp)
568{
569 struct mgcp_trunk_config *tcfg = endp->tcfg;
570
571 int patch_ssrc = expect_ssrc_change && tcfg->force_constant_ssrc;
572
573 rtp->force_aligned_timing = tcfg->force_aligned_timing;
574 rtp->force_constant_ssrc = patch_ssrc ? 1 : 0;
575
576 LOGP(DLMGCP, LOGL_DEBUG,
577 "Configuring RTP endpoint: local port %d%s%s\n",
578 ntohs(rtp->rtp_port),
579 rtp->force_aligned_timing ? ", force constant timing" : "",
580 rtp->force_constant_ssrc ? ", force constant ssrc" : "");
581}
582
583uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
584 struct mgcp_rtp_end *rtp)
585{
586 int f = 0;
587
588 /* Get the number of frames per channel and packet */
589 if (rtp->frames_per_packet)
590 f = rtp->frames_per_packet;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200591 else if (rtp->packet_duration_ms && rtp->codec->frame_duration_num) {
592 int den = 1000 * rtp->codec->frame_duration_num;
593 f = (rtp->packet_duration_ms * rtp->codec->frame_duration_den +
Philipp Maier87bd9be2017-08-22 16:35:41 +0200594 den / 2)
595 / den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200596 }
597
Philipp Maierbc0346e2018-06-07 09:52:16 +0200598 return rtp->codec->rate * f * rtp->codec->frame_duration_num /
599 rtp->codec->frame_duration_den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200600}
601
602static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line)
603{
604 if (!endp->cfg->osmux_init) {
605 if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) {
606 LOGP(DLMGCP, LOGL_ERROR, "Cannot init OSMUX\n");
607 return -1;
608 }
609 LOGP(DLMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n");
610 }
611
612 return mgcp_parse_osmux_cid(line);
613}
614
Philipp Maierbc0346e2018-06-07 09:52:16 +0200615/* Process codec information contained in CRCX/MDCX */
616static int handle_codec_info(struct mgcp_conn_rtp *conn,
617 struct mgcp_parse_data *p, int have_sdp, bool crcx)
618{
619 struct mgcp_endpoint *endp = p->endp;
620 int rc;
621 char *cmd;
622
623 if (crcx)
624 cmd = "CRCX";
625 else
626 cmd = "MDCX";
627
628 /* Collect codec information */
629 if (have_sdp) {
630 /* If we have SDP, we ignore the local connection options and
631 * use only the SDP information. */
632 mgcp_codec_reset_all(conn);
633 rc = mgcp_parse_sdp_data(endp, conn, p);
634 if (rc != 0) {
635 LOGP(DLMGCP, LOGL_ERROR,
636 "%s: endpoint:%x sdp not parseable\n", cmd,
637 ENDPOINT_NUMBER(endp));
638
639 /* See also RFC 3661: Protocol error */
640 return 510;
641 }
642 } else if (endp->local_options.codec) {
643 /* When no SDP is available, we use the codec information from
644 * the local connection options (if present) */
645 mgcp_codec_reset_all(conn);
646 rc = mgcp_codec_add(conn, PTYPE_UNDEFINED, endp->local_options.codec);
647 if (rc != 0)
648 goto error;
649 }
650
651 /* Make sure we always set a sane default codec */
652 if (conn->end.codecs_assigned == 0) {
653 /* When SDP and/or LCO did not supply any codec information,
654 * than it makes sense to pick a sane default: (payload-type 0,
655 * PCMU), see also: OS#2658 */
656 mgcp_codec_reset_all(conn);
657 rc = mgcp_codec_add(conn, 0, NULL);
658 if (rc != 0)
659 goto error;
660 }
661
662 /* Make codec decision */
663 if (mgcp_codec_decide(conn) != 0)
664 goto error;
665
666 return 0;
667
668error:
669 LOGP(DLMGCP, LOGL_ERROR,
670 "%s: endpoint:0x%x codec negotiation failure\n", cmd,
671 ENDPOINT_NUMBER(endp));
672
673 /* See also RFC 3661: Codec negotiation failure */
674 return 534;
675}
676
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200677static bool parse_x_osmo_ign(struct mgcp_endpoint *endp, char *line)
678{
679 char *saveptr = NULL;
680
681 if (strncmp(line, MGCP_X_OSMO_IGN_HEADER, strlen(MGCP_X_OSMO_IGN_HEADER)))
682 return false;
683 line += strlen(MGCP_X_OSMO_IGN_HEADER);
684
685 while (1) {
686 char *token = strtok_r(line, " ", &saveptr);
687 line = NULL;
688 if (!token)
689 break;
690
691 if (!strcmp(token, "C"))
692 endp->x_osmo_ign |= MGCP_X_OSMO_IGN_CALLID;
693 else
694 LOGP(DLMGCP, LOGL_ERROR, "endpoint %x: received unknown X-Osmo-IGN item '%s'\n",
695 ENDPOINT_NUMBER(endp), token);
696 }
697
698 return true;
699}
700
Philipp Maier87bd9be2017-08-22 16:35:41 +0200701/* CRCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200702static struct msgb *handle_create_con(struct mgcp_parse_data *p)
703{
704 struct mgcp_trunk_config *tcfg;
705 struct mgcp_endpoint *endp = p->endp;
706 int error_code = 400;
707
708 const char *local_options = NULL;
709 const char *callid = NULL;
710 const char *mode = NULL;
711 char *line;
712 int have_sdp = 0, osmux_cid = -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200713 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100714 struct mgcp_conn *_conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200715 char conn_name[512];
Philipp Maiera390d0b2018-01-31 17:30:19 +0100716 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200717
718 LOGP(DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200719
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200720 /* parse CallID C: and LocalParameters L: */
721 for_each_line(line, p->save) {
722 if (!mgcp_check_param(endp, line))
723 continue;
724
725 switch (line[0]) {
726 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200727 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200728 break;
729 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200730 callid = (const char *)line + 3;
731 break;
732 case 'I':
Philipp Maierffd75e42017-11-22 11:44:50 +0100733 /* It is illegal to send a connection identifier
734 * together with a CRCX, the MGW will assign the
735 * connection identifier by itself on CRCX */
736 return create_err_response(NULL, 523, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200737 break;
738 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200739 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200740 break;
741 case 'X':
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200742 if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
743 /* If osmux is disabled, just skip setting it up */
744 if (!p->endp->cfg->osmux)
745 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200746 osmux_cid = mgcp_osmux_setup(endp, line);
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200747 break;
748 }
749
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200750 if (parse_x_osmo_ign(endp, line))
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200751 break;
Neels Hofmeyrf2388ea2018-08-26 23:36:53 +0200752
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200753 /* Ignore unknown X-headers */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200754 break;
755 case '\0':
756 have_sdp = 1;
757 goto mgcp_header_done;
758 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200759 LOGP(DLMGCP, LOGL_NOTICE,
760 "CRCX: endpoint:%x unhandled option: '%c'/%d\n",
761 ENDPOINT_NUMBER(endp), *line, *line);
Philipp Maierdd0c5222018-02-02 11:08:48 +0100762 return create_err_response(NULL, 539, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200763 break;
764 }
765 }
766
767mgcp_header_done:
768 tcfg = p->endp->tcfg;
769
Philipp Maier87bd9be2017-08-22 16:35:41 +0200770 /* Check parameters */
771 if (!callid) {
772 LOGP(DLMGCP, LOGL_ERROR,
773 "CRCX: endpoint:%x insufficient parameters, missing callid\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200774 ENDPOINT_NUMBER(endp));
Harald Weltee35eeae2017-12-28 13:47:37 +0100775 return create_err_response(endp, 516, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200776 }
777
Philipp Maier87bd9be2017-08-22 16:35:41 +0200778 if (!mode) {
779 LOGP(DLMGCP, LOGL_ERROR,
780 "CRCX: endpoint:%x insufficient parameters, missing mode\n",
781 ENDPOINT_NUMBER(endp));
Harald Weltee35eeae2017-12-28 13:47:37 +0100782 return create_err_response(endp, 517, "CRCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200783 }
784
Philipp Maier87bd9be2017-08-22 16:35:41 +0200785 /* Check if we are able to accept the creation of another connection */
786 if (llist_count(&endp->conns) >= endp->type->max_conns) {
787 LOGP(DLMGCP, LOGL_ERROR,
788 "CRCX: endpoint:%x endpoint full, max. %i connections allowed!\n",
789 endp->type->max_conns, ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200790 if (tcfg->force_realloc) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200791 /* There is no more room for a connection, make some
792 * room by blindly tossing the oldest of the two two
793 * connections */
794 mgcp_conn_free_oldest(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200795 } else {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200796 /* There is no more room for a connection, leave
797 * everything as it is and return with an error */
Harald Weltee35eeae2017-12-28 13:47:37 +0100798 return create_err_response(endp, 540, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200799 }
800 }
801
Philipp Maier87bd9be2017-08-22 16:35:41 +0200802 /* Check if this endpoint already serves a call, if so, check if the
803 * callids match up so that we are sure that this is our call */
804 if (endp->callid && mgcp_verify_call_id(endp, callid)) {
805 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100806 "CRCX: endpoint:0x%x allready seized by other call (%s)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200807 ENDPOINT_NUMBER(endp), endp->callid);
808 if (tcfg->force_realloc)
809 /* This is not our call, toss everything by releasing
810 * the entire endpoint. (rude!) */
Philipp Maier1355d7e2018-02-01 14:30:06 +0100811 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200812 else {
813 /* This is not our call, leave everything as it is and
814 * return with an error. */
815 return create_err_response(endp, 400, "CRCX", p->trans);
816 }
817 }
818
819 /* Set the callid, creation of another connection will only be possible
Harald Welte1d1b98f2017-12-25 10:03:40 +0100820 * when the callid matches up. (Connections are distinguished by their
Philipp Maier87bd9be2017-08-22 16:35:41 +0200821 * connection ids) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200822 endp->callid = talloc_strdup(tcfg->endpoints, callid);
823
Philipp Maierffd75e42017-11-22 11:44:50 +0100824 snprintf(conn_name, sizeof(conn_name), "%s", callid);
825 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP, conn_name);
826 if (!_conn) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200827 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100828 "CRCX: endpoint:0x%x unable to allocate RTP connection\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200829 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200830 goto error2;
831
Philipp Maier87bd9be2017-08-22 16:35:41 +0200832 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100833 conn = mgcp_conn_get_rtp(endp, _conn->id);
834 OSMO_ASSERT(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200835
836 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
837 error_code = 517;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200838 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200839 }
840
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200841 /* Annotate Osmux circuit ID and set it to negotiating state until this
Philipp Maier87bd9be2017-08-22 16:35:41 +0200842 * is fully set up from the dummy load. */
843 conn->osmux.state = OSMUX_STATE_DISABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200844 if (osmux_cid >= 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200845 conn->osmux.cid = osmux_cid;
846 conn->osmux.state = OSMUX_STATE_NEGOTIATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200847 } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) {
848 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100849 "CRCX: endpoint:0x%x osmux only and no osmux offered\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200850 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200851 goto error2;
852 }
853
Philipp Maierbc0346e2018-06-07 09:52:16 +0200854 /* Set local connection options, if present */
855 if (local_options) {
856 rc = set_local_cx_options(endp->tcfg->endpoints,
857 &endp->local_options, local_options);
858 if (rc != 0) {
859 LOGP(DLMGCP, LOGL_ERROR,
860 "CRCX: endpoint:%x inavlid local connection options!\n",
861 ENDPOINT_NUMBER(endp));
862 error_code = rc;
863 goto error2;
864 }
865 }
866
867 /* Handle codec information and decide for a suitable codec */
868 rc = handle_codec_info(conn, p, have_sdp, true);
869 mgcp_codec_summary(conn);
870 if (rc) {
871 error_code = rc;
872 goto error2;
873 }
874
Philipp Maier87bd9be2017-08-22 16:35:41 +0200875 conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints,
876 tcfg->audio_fmtp_extra);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200877
Philipp Maier87bd9be2017-08-22 16:35:41 +0200878 if (p->cfg->force_ptime) {
879 conn->end.packet_duration_ms = p->cfg->force_ptime;
880 conn->end.force_output_ptime = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200881 }
882
Philipp Maier1cb1e382017-11-02 17:16:04 +0100883 mgcp_rtp_end_config(endp, 0, &conn->end);
884
Philipp Maierc3cc6542018-02-02 12:58:42 +0100885 /* check connection mode setting */
886 if (conn->conn->mode != MGCP_CONN_LOOPBACK
887 && conn->conn->mode != MGCP_CONN_RECV_ONLY
888 && conn->end.rtp_port == 0) {
889 LOGP(DLMGCP, LOGL_ERROR,
890 "CRCX: endpoint:%x selected connection mode type requires an opposite end!\n",
891 ENDPOINT_NUMBER(endp));
892 error_code = 527;
893 goto error2;
894 }
895
Philipp Maier1cb1e382017-11-02 17:16:04 +0100896 if (allocate_port(endp, conn) != 0) {
897 goto error2;
898 }
899
Philipp Maier87bd9be2017-08-22 16:35:41 +0200900 if (setup_rtp_processing(endp, conn) != 0) {
901 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100902 "CRCX: endpoint:0x%x could not start RTP processing!\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200903 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200904 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200905 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200906
907 /* policy CB */
908 if (p->cfg->policy_cb) {
909 int rc;
910 rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200911 MGCP_ENDP_CRCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200912 switch (rc) {
913 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200914 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100915 "CRCX: endpoint:0x%x CRCX rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200916 ENDPOINT_NUMBER(endp));
Philipp Maier1355d7e2018-02-01 14:30:06 +0100917 mgcp_endp_release(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200918 return create_err_response(endp, 400, "CRCX", p->trans);
919 break;
920 case MGCP_POLICY_DEFER:
921 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200922 return NULL;
923 break;
924 case MGCP_POLICY_CONT:
925 /* just continue */
926 break;
927 }
928 }
929
Philipp Maier87bd9be2017-08-22 16:35:41 +0200930 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100931 "CRCX: endpoint:0x%x Creating connection: CI: %s port: %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200932 ENDPOINT_NUMBER(endp), conn->conn->id, conn->end.local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200933 if (p->cfg->change_cb)
934 p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX);
935
Philipp Maiere726d4f2017-11-01 10:41:34 +0100936 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
937 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200938 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
Philipp Maiere726d4f2017-11-01 10:41:34 +0100939 && tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200940 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200941
Philipp Maier87bd9be2017-08-22 16:35:41 +0200942 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100943 "CRCX: endpoint:0x%x connection successfully created\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200944 ENDPOINT_NUMBER(endp));
Philipp Maier55295f72018-01-15 14:00:28 +0100945 return create_response_with_sdp(endp, conn, "CRCX", p->trans, true);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200946error2:
Philipp Maier1355d7e2018-02-01 14:30:06 +0100947 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200948 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier3c8ccb62018-06-04 09:59:24 +0200949 "CRCX: endpoint:0x%x unable to create connection\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200950 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200951 return create_err_response(endp, error_code, "CRCX", p->trans);
952}
953
Philipp Maierbc0346e2018-06-07 09:52:16 +0200954
955
956
957
Philipp Maier87bd9be2017-08-22 16:35:41 +0200958/* MDCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200959static struct msgb *handle_modify_con(struct mgcp_parse_data *p)
960{
961 struct mgcp_endpoint *endp = p->endp;
962 int error_code = 500;
963 int silent = 0;
964 int have_sdp = 0;
965 char *line;
966 const char *local_options = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200967 const char *mode = NULL;
968 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier01d24a32017-11-21 17:26:09 +0100969 const char *conn_id = NULL;
Philipp Maiera390d0b2018-01-31 17:30:19 +0100970 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200971
972 LOGP(DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200973
Philipp Maier5656fbf2018-02-02 14:41:58 +0100974 /* Prohibit wildcarded requests */
975 if (endp->wildcarded_req) {
976 LOGP(DLMGCP, LOGL_ERROR,
977 "MDCX: endpoint:0x%x wildcarded endpoint names not supported.\n",
978 ENDPOINT_NUMBER(endp));
979 return create_err_response(endp, 507, "MDCX", p->trans);
980 }
981
Philipp Maier87bd9be2017-08-22 16:35:41 +0200982 if (llist_count(&endp->conns) <= 0) {
983 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100984 "MDCX: endpoint:0x%x endpoint is not holding a connection.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200985 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200986 return create_err_response(endp, 400, "MDCX", p->trans);
987 }
988
989 for_each_line(line, p->save) {
990 if (!mgcp_check_param(endp, line))
991 continue;
992
993 switch (line[0]) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200994 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +0100995 if (mgcp_verify_call_id(endp, line + 3) != 0) {
996 error_code = 516;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200997 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +0100998 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200999 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001000 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001001 conn_id = (const char *)line + 3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001002 if (mgcp_verify_ci(endp, conn_id) != 0) {
1003 error_code = 515;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001004 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001005 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001006 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001007 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001008 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001009 break;
1010 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +02001011 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001012 break;
1013 case 'Z':
1014 silent = strcmp("noanswer", line + 3) == 0;
1015 break;
1016 case '\0':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001017 have_sdp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001018 goto mgcp_header_done;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001019 break;
1020 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001021 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001022 "MDCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001023 ENDPOINT_NUMBER(endp), line[0], line[0]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001024 return create_err_response(NULL, 539, "MDCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001025 break;
1026 }
1027 }
1028
Philipp Maier87bd9be2017-08-22 16:35:41 +02001029mgcp_header_done:
Philipp Maier01d24a32017-11-21 17:26:09 +01001030 if (!conn_id) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001031 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001032 "MDCX: endpoint:0x%x insufficient parameters, missing ci (connectionIdentifier)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001033 ENDPOINT_NUMBER(endp));
Harald Weltee35eeae2017-12-28 13:47:37 +01001034 return create_err_response(endp, 515, "MDCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001035 }
1036
1037 conn = mgcp_conn_get_rtp(endp, conn_id);
1038 if (!conn)
1039 return create_err_response(endp, 400, "MDCX", p->trans);
1040
1041 if (mode) {
1042 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
1043 error_code = 517;
1044 goto error3;
1045 }
1046 } else
1047 conn->conn->mode = conn->conn->mode_orig;
1048
Philipp Maierbc0346e2018-06-07 09:52:16 +02001049 /* Set local connection options, if present */
1050 if (local_options) {
1051 rc = set_local_cx_options(endp->tcfg->endpoints,
1052 &endp->local_options, local_options);
1053 if (rc != 0) {
1054 LOGP(DLMGCP, LOGL_ERROR,
1055 "MDCX: endpoint:%x inavlid local connection options!\n",
1056 ENDPOINT_NUMBER(endp));
1057 error_code = rc;
1058 goto error3;
1059 }
1060 }
Philipp Maier87bd9be2017-08-22 16:35:41 +02001061
Philipp Maierbc0346e2018-06-07 09:52:16 +02001062 /* Handle codec information and decide for a suitable codec */
1063 rc = handle_codec_info(conn, p, have_sdp, false);
1064 mgcp_codec_summary(conn);
1065 if (rc) {
Philipp Maieraf07f662018-02-02 11:34:02 +01001066 error_code = rc;
1067 goto error3;
Philipp Maiera390d0b2018-01-31 17:30:19 +01001068 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001069
Philipp Maierc3cc6542018-02-02 12:58:42 +01001070 /* check connection mode setting */
1071 if (conn->conn->mode != MGCP_CONN_LOOPBACK
1072 && conn->conn->mode != MGCP_CONN_RECV_ONLY
1073 && conn->end.rtp_port == 0) {
1074 LOGP(DLMGCP, LOGL_ERROR,
1075 "MDCX: endpoint:%x selected connection mode type requires an opposite end!\n",
1076 ENDPOINT_NUMBER(endp));
1077 error_code = 527;
1078 goto error3;
1079 }
1080
Philipp Maierbc0346e2018-06-07 09:52:16 +02001081
Philipp Maier87bd9be2017-08-22 16:35:41 +02001082 if (setup_rtp_processing(endp, conn) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001083 goto error3;
1084
Philipp Maier87bd9be2017-08-22 16:35:41 +02001085
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001086 /* policy CB */
1087 if (p->cfg->policy_cb) {
1088 int rc;
1089 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001090 MGCP_ENDP_MDCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001091 switch (rc) {
1092 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001093 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001094 "MDCX: endpoint:0x%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001095 ENDPOINT_NUMBER(endp));
1096 if (silent)
1097 goto out_silent;
1098 return create_err_response(endp, 400, "MDCX", p->trans);
1099 break;
1100 case MGCP_POLICY_DEFER:
1101 /* stop processing */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001102 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001103 "MDCX: endpoint:0x%x defered by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001104 ENDPOINT_NUMBER(endp));
1105 return NULL;
1106 break;
1107 case MGCP_POLICY_CONT:
1108 /* just continue */
1109 break;
1110 }
1111 }
1112
Philipp Maier87bd9be2017-08-22 16:35:41 +02001113 mgcp_rtp_end_config(endp, 1, &conn->end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001114
1115 /* modify */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001116 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001117 "MDCX: endpoint:0x%x modified conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001118 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001119 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001120 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1121 MGCP_ENDP_MDCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001122
Philipp Maiere726d4f2017-11-01 10:41:34 +01001123 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
1124 OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1125 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
1126 && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001127 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001128
1129 if (silent)
1130 goto out_silent;
1131
Philipp Maier87bd9be2017-08-22 16:35:41 +02001132 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001133 "MDCX: endpoint:0x%x connection successfully modified\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001134 ENDPOINT_NUMBER(endp));
Philipp Maier55295f72018-01-15 14:00:28 +01001135 return create_response_with_sdp(endp, conn, "MDCX", p->trans, false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001136error3:
1137 return create_err_response(endp, error_code, "MDCX", p->trans);
1138
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001139out_silent:
Philipp Maier230e4fc2017-11-28 09:38:45 +01001140 LOGP(DLMGCP, LOGL_DEBUG, "MDCX: endpoint:0x%x silent exit\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001141 ENDPOINT_NUMBER(endp));
1142 return NULL;
1143}
1144
Philipp Maier87bd9be2017-08-22 16:35:41 +02001145/* DLCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001146static struct msgb *handle_delete_con(struct mgcp_parse_data *p)
1147{
1148 struct mgcp_endpoint *endp = p->endp;
1149 int error_code = 400;
1150 int silent = 0;
1151 char *line;
1152 char stats[1048];
Philipp Maier01d24a32017-11-21 17:26:09 +01001153 const char *conn_id = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001154 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001155
1156 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001157 "DLCX: endpoint:0x%x deleting connection ...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001158 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001159
Philipp Maier5656fbf2018-02-02 14:41:58 +01001160 /* Prohibit wildcarded requests */
1161 if (endp->wildcarded_req) {
1162 LOGP(DLMGCP, LOGL_ERROR,
1163 "DLCX: endpoint:0x%x wildcarded endpoint names not supported.\n",
1164 ENDPOINT_NUMBER(endp));
1165 return create_err_response(endp, 507, "DLCX", p->trans);
1166 }
1167
Philipp Maier87bd9be2017-08-22 16:35:41 +02001168 if (llist_count(&endp->conns) <= 0) {
1169 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001170 "DLCX: endpoint:0x%x endpoint is not holding a connection.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001171 ENDPOINT_NUMBER(endp));
Harald Weltee35eeae2017-12-28 13:47:37 +01001172 return create_err_response(endp, 515, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001173 }
1174
1175 for_each_line(line, p->save) {
1176 if (!mgcp_check_param(endp, line))
1177 continue;
1178
1179 switch (line[0]) {
1180 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +01001181 if (mgcp_verify_call_id(endp, line + 3) != 0) {
1182 error_code = 516;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001183 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001184 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001185 break;
1186 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +01001187 conn_id = (const char *)line + 3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001188 if (mgcp_verify_ci(endp, conn_id) != 0) {
1189 error_code = 515;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001190 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +01001191 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001192 break;
1193 case 'Z':
1194 silent = strcmp("noanswer", line + 3) == 0;
1195 break;
1196 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001197 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001198 "DLCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001199 ENDPOINT_NUMBER(endp), line[0], line[0]);
Philipp Maierdd0c5222018-02-02 11:08:48 +01001200 return create_err_response(NULL, 539, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001201 break;
1202 }
1203 }
1204
1205 /* policy CB */
1206 if (p->cfg->policy_cb) {
1207 int rc;
1208 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001209 MGCP_ENDP_DLCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001210 switch (rc) {
1211 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +02001212 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001213 "DLCX: endpoint:0x%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001214 ENDPOINT_NUMBER(endp));
1215 if (silent)
1216 goto out_silent;
1217 return create_err_response(endp, 400, "DLCX", p->trans);
1218 break;
1219 case MGCP_POLICY_DEFER:
1220 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001221 return NULL;
1222 break;
1223 case MGCP_POLICY_CONT:
1224 /* just continue */
1225 break;
1226 }
1227 }
1228
Philipp Maierf4c0e372017-10-11 16:06:45 +02001229 /* When no connection id is supplied, we will interpret this as a
1230 * wildcarded DLCX and drop all connections at once. (See also
1231 * RFC3435 Section F.7) */
Philipp Maier01d24a32017-11-21 17:26:09 +01001232 if (!conn_id) {
Philipp Maierf4c0e372017-10-11 16:06:45 +02001233 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001234 "DLCX: endpoint:0x%x missing ci (connectionIdentifier), will remove all connections at once\n",
Philipp Maierf4c0e372017-10-11 16:06:45 +02001235 ENDPOINT_NUMBER(endp));
1236
Philipp Maier1355d7e2018-02-01 14:30:06 +01001237 mgcp_endp_release(endp);
Philipp Maierf4c0e372017-10-11 16:06:45 +02001238
1239 /* Note: In this case we do not return any statistics,
1240 * as we assume that the client is not interested in
1241 * this case. */
1242 return create_ok_response(endp, 200, "DLCX", p->trans);
1243 }
1244
Philipp Maierf4c0e372017-10-11 16:06:45 +02001245 /* Find the connection */
Philipp Maier87bd9be2017-08-22 16:35:41 +02001246 conn = mgcp_conn_get_rtp(endp, conn_id);
1247 if (!conn)
1248 goto error3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001249
Philipp Maier87bd9be2017-08-22 16:35:41 +02001250 /* save the statistics of the current connection */
1251 mgcp_format_stats(stats, sizeof(stats), conn->conn);
1252
1253 /* delete connection */
Philipp Maier230e4fc2017-11-28 09:38:45 +01001254 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x deleting conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001255 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
1256 mgcp_conn_free(endp, conn_id);
1257 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001258 "DLCX: endpoint:0x%x connection successfully deleted\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001259 ENDPOINT_NUMBER(endp));
1260
1261 /* When all connections are closed, the endpoint will be released
1262 * in order to be ready to be used by another call. */
1263 if (llist_count(&endp->conns) <= 0) {
Philipp Maier1355d7e2018-02-01 14:30:06 +01001264 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001265 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001266 "DLCX: endpoint:0x%x endpoint released\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001267 ENDPOINT_NUMBER(endp));
1268 }
1269
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001270 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001271 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1272 MGCP_ENDP_DLCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001273
1274 if (silent)
1275 goto out_silent;
1276 return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats);
1277
1278error3:
1279 return create_err_response(endp, error_code, "DLCX", p->trans);
1280
1281out_silent:
Philipp Maier230e4fc2017-11-28 09:38:45 +01001282 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x silent exit\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001283 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001284 return NULL;
1285}
1286
Philipp Maier87bd9be2017-08-22 16:35:41 +02001287/* RSIP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001288static struct msgb *handle_rsip(struct mgcp_parse_data *p)
1289{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001290 /* TODO: Also implement the resetting of a specific endpoint
1291 * to make mgcp_send_reset_ep() work. Currently this will call
1292 * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1
1293 * to make read_call_agent() reset all endpoints when called
1294 * next time. In order to selectively reset endpoints some
1295 * mechanism to distinguish which endpoint shall be resetted
1296 * is needed */
1297
1298 LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n");
1299
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001300 if (p->cfg->reset_cb)
1301 p->cfg->reset_cb(p->endp->tcfg);
1302 return NULL;
1303}
1304
1305static char extract_tone(const char *line)
1306{
1307 const char *str = strstr(line, "D/");
1308 if (!str)
1309 return CHAR_MAX;
1310
1311 return str[2];
1312}
1313
Philipp Maier87bd9be2017-08-22 16:35:41 +02001314/* This can request like DTMF detection and forward, fax detection... it
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001315 * can also request when the notification should be send and such. We don't
Philipp Maier87bd9be2017-08-22 16:35:41 +02001316 * do this right now. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001317static struct msgb *handle_noti_req(struct mgcp_parse_data *p)
1318{
1319 int res = 0;
1320 char *line;
1321 char tone = CHAR_MAX;
1322
Philipp Maier87bd9be2017-08-22 16:35:41 +02001323 LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n");
1324
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001325 for_each_line(line, p->save) {
1326 switch (line[0]) {
1327 case 'S':
1328 tone = extract_tone(line);
1329 break;
1330 }
1331 }
1332
1333 /* we didn't see a signal request with a tone */
1334 if (tone == CHAR_MAX)
1335 return create_ok_response(p->endp, 200, "RQNT", p->trans);
1336
1337 if (p->cfg->rqnt_cb)
1338 res = p->cfg->rqnt_cb(p->endp, tone);
1339
1340 return res == 0 ?
Philipp Maier87bd9be2017-08-22 16:35:41 +02001341 create_ok_response(p->endp, 200, "RQNT", p->trans) :
1342 create_err_response(p->endp, res, "RQNT", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001343}
1344
Philipp Maier87bd9be2017-08-22 16:35:41 +02001345/* Connection keepalive timer, will take care that dummy packets are send
Harald Welte1d1b98f2017-12-25 10:03:40 +01001346 * regularly, so that NAT connections stay open */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001347static void mgcp_keepalive_timer_cb(void *_tcfg)
1348{
1349 struct mgcp_trunk_config *tcfg = _tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001350 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001351 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001352
Philipp Maiere726d4f2017-11-01 10:41:34 +01001353 LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001354 tcfg->trunk_nr);
1355
Philipp Maiere726d4f2017-11-01 10:41:34 +01001356 /* Do not accept invalid configuration values
1357 * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and
1358 * values greater 0 */
1359 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1360
1361 /* The dummy packet functionality has been disabled, we will exit
1362 * immediately, no further timer is scheduled, which means we will no
1363 * longer send dummy packets even when we did before */
1364 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER)
1365 return;
1366
1367 /* In cases where only one dummy packet is sent, we do not need
1368 * the timer since the functions that handle the CRCX and MDCX are
1369 * triggering the sending of the dummy packet. So we behave like in
1370 * the MGCP_KEEPALIVE_NEVER case */
1371 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001372 return;
1373
Philipp Maier87bd9be2017-08-22 16:35:41 +02001374 /* Send walk over all endpoints and send out dummy packets through
1375 * every connection present on each endpoint */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001376 for (i = 1; i < tcfg->number_endpoints; ++i) {
1377 struct mgcp_endpoint *endp = &tcfg->endpoints[i];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001378 llist_for_each_entry(conn, &endp->conns, entry) {
1379 if (conn->mode == MGCP_CONN_RECV_ONLY)
1380 send_dummy(endp, &conn->u.rtp);
1381 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001382 }
1383
Philipp Maiere726d4f2017-11-01 10:41:34 +01001384 /* Schedule the keepalive timer for the next round */
1385 LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001386 tcfg->trunk_nr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001387 osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval,
1388 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001389}
1390
1391void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval)
1392{
1393 tcfg->keepalive_interval = interval;
1394 osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg);
1395
1396 if (interval <= 0)
1397 osmo_timer_del(&tcfg->keepalive_timer);
1398 else
1399 osmo_timer_schedule(&tcfg->keepalive_timer,
1400 tcfg->keepalive_interval, 0);
1401}
1402
Philipp Maier87bd9be2017-08-22 16:35:41 +02001403/*! allocate configuration with default values.
1404 * (called once at startup by main function) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001405struct mgcp_config *mgcp_config_alloc(void)
1406{
1407 struct mgcp_config *cfg;
1408
1409 cfg = talloc_zero(NULL, struct mgcp_config);
1410 if (!cfg) {
1411 LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n");
1412 return NULL;
1413 }
1414
Philipp Maier12943ea2018-01-17 15:40:25 +01001415 osmo_strlcpy(cfg->domain, "mgw", sizeof(cfg->domain));
1416
Philipp Maier87bd9be2017-08-22 16:35:41 +02001417 cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START;
1418 cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END;
1419 cfg->net_ports.last_port = cfg->net_ports.range_start;
1420
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001421 cfg->source_port = 2427;
1422 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
1423 cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
1424
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001425 cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
1426 cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
1427
1428 cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
1429
1430 /* default trunk handling */
1431 cfg->trunk.cfg = cfg;
1432 cfg->trunk.trunk_nr = 0;
1433 cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
1434 cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
1435 cfg->trunk.audio_payload = 126;
1436 cfg->trunk.audio_send_ptime = 1;
1437 cfg->trunk.audio_send_name = 1;
1438 cfg->trunk.omit_rtcp = 0;
1439 mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE);
1440
1441 INIT_LLIST_HEAD(&cfg->trunks);
1442
1443 return cfg;
1444}
1445
Philipp Maier87bd9be2017-08-22 16:35:41 +02001446/*! allocate configuration with default values.
1447 * (called once at startup by VTY)
1448 * \param[in] cfg mgcp configuration
1449 * \param[in] nr trunk number
1450 * \returns pointer to allocated trunk configuration */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001451struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
1452{
1453 struct mgcp_trunk_config *trunk;
1454
1455 trunk = talloc_zero(cfg, struct mgcp_trunk_config);
1456 if (!trunk) {
1457 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n");
1458 return NULL;
1459 }
1460
1461 trunk->cfg = cfg;
1462 trunk->trunk_type = MGCP_TRUNK_E1;
1463 trunk->trunk_nr = nr;
1464 trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
1465 trunk->audio_payload = 126;
1466 trunk->audio_send_ptime = 1;
1467 trunk->audio_send_name = 1;
Philipp Maierfcd06552017-11-10 17:32:22 +01001468 trunk->vty_number_endpoints = 33;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001469 trunk->omit_rtcp = 0;
1470 mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
1471 llist_add_tail(&trunk->entry, &cfg->trunks);
1472 return trunk;
1473}
1474
Philipp Maier87bd9be2017-08-22 16:35:41 +02001475/*! get trunk configuration by trunk number (index).
1476 * \param[in] cfg mgcp configuration
1477 * \param[in] index trunk number
1478 * \returns pointer to trunk configuration, NULL on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001479struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
1480{
1481 struct mgcp_trunk_config *trunk;
1482
1483 llist_for_each_entry(trunk, &cfg->trunks, entry)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001484 if (trunk->trunk_nr == index)
1485 return trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001486
1487 return NULL;
1488}
1489
Philipp Maier87bd9be2017-08-22 16:35:41 +02001490/*! allocate endpoints and set default values.
1491 * (called once at startup by VTY)
1492 * \param[in] tcfg trunk configuration
1493 * \returns 0 on success, -1 on failure */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001494int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
1495{
1496 int i;
1497
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001498 tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001499 sizeof(struct mgcp_endpoint),
Philipp Maierfcd06552017-11-10 17:32:22 +01001500 tcfg->vty_number_endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001501 "endpoints");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001502 if (!tcfg->endpoints)
1503 return -1;
1504
Philipp Maierfcd06552017-11-10 17:32:22 +01001505 for (i = 0; i < tcfg->vty_number_endpoints; ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001506 INIT_LLIST_HEAD(&tcfg->endpoints[i].conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001507 tcfg->endpoints[i].cfg = tcfg->cfg;
1508 tcfg->endpoints[i].tcfg = tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001509
1510 /* NOTE: Currently all endpoints are of type RTP, this will
1511 * change when new variations are implemented */
1512 tcfg->endpoints[i].type = &ep_typeset.rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001513 }
1514
Philipp Maierfcd06552017-11-10 17:32:22 +01001515 tcfg->number_endpoints = tcfg->vty_number_endpoints;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001516 return 0;
1517}
1518
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001519static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1520{
1521 return write(cfg->gw_fd.bfd.fd, buf, len);
1522}
1523
Philipp Maier87bd9be2017-08-22 16:35:41 +02001524/*! Reset all endpoints by sending RSIP message to self.
1525 * (called by VTY)
1526 * \param[in] endp trunk endpoint
1527 * \param[in] endpoint number
1528 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001529int mgcp_send_reset_all(struct mgcp_config *cfg)
1530{
Philipp Maier12943ea2018-01-17 15:40:25 +01001531 char buf[MGCP_ENDPOINT_MAXLEN + 128];
1532 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001533 int rc;
1534
Philipp Maier12943ea2018-01-17 15:40:25 +01001535 len = snprintf(buf, sizeof(buf),
1536 "RSIP 1 *@%s MGCP 1.0\r\n", cfg->domain);
1537 if (len < 0)
1538 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001539
Philipp Maier12943ea2018-01-17 15:40:25 +01001540 rc = send_agent(cfg, buf, len);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001541 if (rc <= 0)
1542 return -1;
1543
1544 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001545}
1546
Philipp Maier87bd9be2017-08-22 16:35:41 +02001547/*! Reset a single endpoint by sending RSIP message to self.
1548 * (called by VTY)
1549 * \param[in] endp trunk endpoint
1550 * \param[in] endpoint number
1551 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001552int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
1553{
Philipp Maier12943ea2018-01-17 15:40:25 +01001554 char buf[MGCP_ENDPOINT_MAXLEN + 128];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001555 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001556 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001557
1558 len = snprintf(buf, sizeof(buf),
Philipp Maier12943ea2018-01-17 15:40:25 +01001559 "RSIP 39 %x@%s MGCP 1.0\r\n", endpoint, endp->cfg->domain);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001560 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001561 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001562
Philipp Maier87bd9be2017-08-22 16:35:41 +02001563 rc = send_agent(endp->cfg, buf, len);
1564 if (rc <= 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001565 return -1;
1566
Philipp Maier87bd9be2017-08-22 16:35:41 +02001567 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001568}