blob: 62487d15e532617e75c0cc32de3ee74bc1a68b17 [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>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020043
44struct mgcp_request {
45 char *name;
Philipp Maier87bd9be2017-08-22 16:35:41 +020046 struct msgb *(*handle_request) (struct mgcp_parse_data * data);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020047 char *debug_name;
48};
49
50#define MGCP_REQUEST(NAME, REQ, DEBUG_NAME) \
51 { .name = NAME, .handle_request = REQ, .debug_name = DEBUG_NAME },
52
53static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *data);
54static struct msgb *handle_create_con(struct mgcp_parse_data *data);
55static struct msgb *handle_delete_con(struct mgcp_parse_data *data);
56static struct msgb *handle_modify_con(struct mgcp_parse_data *data);
57static struct msgb *handle_rsip(struct mgcp_parse_data *data);
58static struct msgb *handle_noti_req(struct mgcp_parse_data *data);
59
Philipp Maier87bd9be2017-08-22 16:35:41 +020060/* Initalize transcoder */
61static int setup_rtp_processing(struct mgcp_endpoint *endp,
62 struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020063{
Philipp Maier87bd9be2017-08-22 16:35:41 +020064 struct mgcp_config *cfg = endp->cfg;
65 struct mgcp_conn_rtp *conn_src = NULL;
66 struct mgcp_conn_rtp *conn_dst = conn;
67 struct mgcp_conn *_conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020068
Philipp Maier87bd9be2017-08-22 16:35:41 +020069 if (conn->type != MGCP_RTP_DEFAULT) {
70 LOGP(DLMGCP, LOGL_NOTICE,
71 "endpoint:%x RTP-setup: Endpoint is not configured as RTP default, stopping here!\n",
72 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020073 return 0;
74 }
75
Philipp Maier87bd9be2017-08-22 16:35:41 +020076 if (conn->conn->mode == MGCP_CONN_LOOPBACK) {
77 LOGP(DLMGCP, LOGL_NOTICE,
78 "endpoint:%x RTP-setup: Endpoint is in loopback mode, stopping here!\n",
79 ENDPOINT_NUMBER(endp));
80 return 0;
81 }
82
83 /* Find the "sister" connection */
84 llist_for_each_entry(_conn, &endp->conns, entry) {
85 if (_conn->id != conn->conn->id) {
86 conn_src = &_conn->u.rtp;
87 break;
88 }
89 }
90
91 return cfg->setup_rtp_processing_cb(endp, &conn_dst->end,
92 &conn_src->end);
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
Harald Welte1d1b98f2017-12-25 10:03:40 +0100359 * to the next free port, binding should work on the first attempt,
360 * nevertheless, try at least the next 200 ports before giving up */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200361static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200362{
363 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200364 struct mgcp_rtp_end *end;
365 struct mgcp_port_range *range;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200366
Philipp Maier87bd9be2017-08-22 16:35:41 +0200367 OSMO_ASSERT(conn);
368 end = &conn->end;
369 OSMO_ASSERT(end);
370
371 range = &endp->cfg->net_ports;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200372
373 /* attempt to find a port */
374 for (i = 0; i < 200; ++i) {
375 int rc;
376
377 if (range->last_port >= range->range_end)
378 range->last_port = range->range_start;
379
Philipp Maier87bd9be2017-08-22 16:35:41 +0200380 rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200381
382 range->last_port += 2;
383 if (rc == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200384 return 0;
385 }
386
387 }
388
Philipp Maier87bd9be2017-08-22 16:35:41 +0200389 LOGP(DLMGCP, LOGL_ERROR,
390 "Allocating a RTP/RTCP port failed 200 times 0x%x.\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200391 ENDPOINT_NUMBER(endp));
392 return -1;
393}
394
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200395/* Set the LCO from a string (see RFC 3435).
Harald Welte1d1b98f2017-12-25 10:03:40 +0100396 * The string is stored in the 'string' field. A NULL string is handled exactly
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200397 * like an empty string, the 'string' field is never NULL after this function
398 * has been called. */
Philipp Maiera390d0b2018-01-31 17:30:19 +0100399static int set_local_cx_options(void *ctx, struct mgcp_lco *lco,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200400 const char *options)
401{
402 char *p_opt, *a_opt;
403 char codec[9];
404
405 talloc_free(lco->string);
406 talloc_free(lco->codec);
407 lco->codec = NULL;
408 lco->pkt_period_min = lco->pkt_period_max = 0;
409 lco->string = talloc_strdup(ctx, options ? options : "");
410
411 p_opt = strstr(lco->string, "p:");
412 if (p_opt && sscanf(p_opt, "p:%d-%d",
413 &lco->pkt_period_min, &lco->pkt_period_max) == 1)
414 lco->pkt_period_max = lco->pkt_period_min;
415
416 a_opt = strstr(lco->string, "a:");
417 if (a_opt && sscanf(a_opt, "a:%8[^,]", codec) == 1)
418 lco->codec = talloc_strdup(ctx, codec);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200419
420 LOGP(DLMGCP, LOGL_DEBUG,
421 "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n",
422 lco->pkt_period_max, lco->codec);
Philipp Maiera390d0b2018-01-31 17:30:19 +0100423
424 /* Check if the packetization fits the 20ms raster */
425 if (lco->pkt_period_min % 20 && lco->pkt_period_max % 20) {
426 LOGP(DLMGCP, LOGL_ERROR,
427 "local CX options: packetization interval is not a multiple of 20ms!\n");
428 return 535;
429 }
430
431 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200432}
433
434void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
435 struct mgcp_rtp_end *rtp)
436{
437 struct mgcp_trunk_config *tcfg = endp->tcfg;
438
439 int patch_ssrc = expect_ssrc_change && tcfg->force_constant_ssrc;
440
441 rtp->force_aligned_timing = tcfg->force_aligned_timing;
442 rtp->force_constant_ssrc = patch_ssrc ? 1 : 0;
443
444 LOGP(DLMGCP, LOGL_DEBUG,
445 "Configuring RTP endpoint: local port %d%s%s\n",
446 ntohs(rtp->rtp_port),
447 rtp->force_aligned_timing ? ", force constant timing" : "",
448 rtp->force_constant_ssrc ? ", force constant ssrc" : "");
449}
450
451uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
452 struct mgcp_rtp_end *rtp)
453{
454 int f = 0;
455
456 /* Get the number of frames per channel and packet */
457 if (rtp->frames_per_packet)
458 f = rtp->frames_per_packet;
459 else if (rtp->packet_duration_ms && rtp->codec.frame_duration_num) {
460 int den = 1000 * rtp->codec.frame_duration_num;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200461 f = (rtp->packet_duration_ms * rtp->codec.frame_duration_den +
462 den / 2)
463 / den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200464 }
465
Philipp Maier87bd9be2017-08-22 16:35:41 +0200466 return rtp->codec.rate * f * rtp->codec.frame_duration_num /
467 rtp->codec.frame_duration_den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200468}
469
470static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line)
471{
472 if (!endp->cfg->osmux_init) {
473 if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) {
474 LOGP(DLMGCP, LOGL_ERROR, "Cannot init OSMUX\n");
475 return -1;
476 }
477 LOGP(DLMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n");
478 }
479
480 return mgcp_parse_osmux_cid(line);
481}
482
Philipp Maier87bd9be2017-08-22 16:35:41 +0200483/* CRCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200484static struct msgb *handle_create_con(struct mgcp_parse_data *p)
485{
486 struct mgcp_trunk_config *tcfg;
487 struct mgcp_endpoint *endp = p->endp;
488 int error_code = 400;
489
490 const char *local_options = NULL;
491 const char *callid = NULL;
492 const char *mode = NULL;
493 char *line;
494 int have_sdp = 0, osmux_cid = -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200495 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100496 struct mgcp_conn *_conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200497 char conn_name[512];
Philipp Maiera390d0b2018-01-31 17:30:19 +0100498 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200499
500 LOGP(DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200501
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200502 /* parse CallID C: and LocalParameters L: */
503 for_each_line(line, p->save) {
504 if (!mgcp_check_param(endp, line))
505 continue;
506
507 switch (line[0]) {
508 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200509 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200510 break;
511 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200512 callid = (const char *)line + 3;
513 break;
514 case 'I':
Philipp Maierffd75e42017-11-22 11:44:50 +0100515 /* It is illegal to send a connection identifier
516 * together with a CRCX, the MGW will assign the
517 * connection identifier by itself on CRCX */
518 return create_err_response(NULL, 523, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200519 break;
520 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200521 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200522 break;
523 case 'X':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200524 /* If osmoux is disabled, just skip setting it up */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200525 if (!p->endp->cfg->osmux)
526 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200527 if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) ==
528 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200529 osmux_cid = mgcp_osmux_setup(endp, line);
530 break;
531 case '\0':
532 have_sdp = 1;
533 goto mgcp_header_done;
534 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200535 LOGP(DLMGCP, LOGL_NOTICE,
536 "CRCX: endpoint:%x unhandled option: '%c'/%d\n",
537 ENDPOINT_NUMBER(endp), *line, *line);
Philipp Maierdd0c5222018-02-02 11:08:48 +0100538 return create_err_response(NULL, 539, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200539 break;
540 }
541 }
542
543mgcp_header_done:
544 tcfg = p->endp->tcfg;
545
Philipp Maier87bd9be2017-08-22 16:35:41 +0200546 /* Check parameters */
547 if (!callid) {
548 LOGP(DLMGCP, LOGL_ERROR,
549 "CRCX: endpoint:%x insufficient parameters, missing callid\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200550 ENDPOINT_NUMBER(endp));
Harald Weltee35eeae2017-12-28 13:47:37 +0100551 return create_err_response(endp, 516, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200552 }
553
Philipp Maier87bd9be2017-08-22 16:35:41 +0200554 if (!mode) {
555 LOGP(DLMGCP, LOGL_ERROR,
556 "CRCX: endpoint:%x insufficient parameters, missing mode\n",
557 ENDPOINT_NUMBER(endp));
Harald Weltee35eeae2017-12-28 13:47:37 +0100558 return create_err_response(endp, 517, "CRCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200559 }
560
Philipp Maier87bd9be2017-08-22 16:35:41 +0200561 /* Check if we are able to accept the creation of another connection */
562 if (llist_count(&endp->conns) >= endp->type->max_conns) {
563 LOGP(DLMGCP, LOGL_ERROR,
564 "CRCX: endpoint:%x endpoint full, max. %i connections allowed!\n",
565 endp->type->max_conns, ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200566 if (tcfg->force_realloc) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200567 /* There is no more room for a connection, make some
568 * room by blindly tossing the oldest of the two two
569 * connections */
570 mgcp_conn_free_oldest(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200571 } else {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200572 /* There is no more room for a connection, leave
573 * everything as it is and return with an error */
Harald Weltee35eeae2017-12-28 13:47:37 +0100574 return create_err_response(endp, 540, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200575 }
576 }
577
Philipp Maier87bd9be2017-08-22 16:35:41 +0200578 /* Check if this endpoint already serves a call, if so, check if the
579 * callids match up so that we are sure that this is our call */
580 if (endp->callid && mgcp_verify_call_id(endp, callid)) {
581 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100582 "CRCX: endpoint:0x%x allready seized by other call (%s)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200583 ENDPOINT_NUMBER(endp), endp->callid);
584 if (tcfg->force_realloc)
585 /* This is not our call, toss everything by releasing
586 * the entire endpoint. (rude!) */
Philipp Maier1355d7e2018-02-01 14:30:06 +0100587 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200588 else {
589 /* This is not our call, leave everything as it is and
590 * return with an error. */
591 return create_err_response(endp, 400, "CRCX", p->trans);
592 }
593 }
594
595 /* Set the callid, creation of another connection will only be possible
Harald Welte1d1b98f2017-12-25 10:03:40 +0100596 * when the callid matches up. (Connections are distinguished by their
Philipp Maier87bd9be2017-08-22 16:35:41 +0200597 * connection ids) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200598 endp->callid = talloc_strdup(tcfg->endpoints, callid);
599
Philipp Maier87bd9be2017-08-22 16:35:41 +0200600 /* Extract audio codec information */
Philipp Maiera390d0b2018-01-31 17:30:19 +0100601 rc = set_local_cx_options(endp->tcfg->endpoints, &endp->local_options,
602 local_options);
603 if (rc != 0) {
604 LOGP(DLMGCP, LOGL_ERROR,
605 "CRCX: endpoint:%x inavlid local connection options!\n",
606 ENDPOINT_NUMBER(endp));
Philipp Maieraf07f662018-02-02 11:34:02 +0100607 error_code = rc;
608 goto error2;
Philipp Maiera390d0b2018-01-31 17:30:19 +0100609 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200610
Philipp Maierffd75e42017-11-22 11:44:50 +0100611 snprintf(conn_name, sizeof(conn_name), "%s", callid);
612 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP, conn_name);
613 if (!_conn) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200614 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100615 "CRCX: endpoint:0x%x unable to allocate RTP connection\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200616 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200617 goto error2;
618
Philipp Maier87bd9be2017-08-22 16:35:41 +0200619 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100620 conn = mgcp_conn_get_rtp(endp, _conn->id);
621 OSMO_ASSERT(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200622
623 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
624 error_code = 517;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200625 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200626 }
627
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200628 /* Annotate Osmux circuit ID and set it to negotiating state until this
Philipp Maier87bd9be2017-08-22 16:35:41 +0200629 * is fully set up from the dummy load. */
630 conn->osmux.state = OSMUX_STATE_DISABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200631 if (osmux_cid >= 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200632 conn->osmux.cid = osmux_cid;
633 conn->osmux.state = OSMUX_STATE_NEGOTIATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200634 } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) {
635 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100636 "CRCX: endpoint:0x%x osmux only and no osmux offered\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200637 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200638 goto error2;
639 }
640
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200641 /* set up RTP media parameters */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200642 if (have_sdp)
Philipp Maier8970c492017-10-11 13:33:42 +0200643 mgcp_parse_sdp_data(endp, conn, p);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200644 else if (endp->local_options.codec)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200645 mgcp_set_audio_info(p->cfg, &conn->end.codec,
646 PTYPE_UNDEFINED, endp->local_options.codec);
647 conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints,
648 tcfg->audio_fmtp_extra);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200649
Philipp Maier87bd9be2017-08-22 16:35:41 +0200650 if (p->cfg->force_ptime) {
651 conn->end.packet_duration_ms = p->cfg->force_ptime;
652 conn->end.force_output_ptime = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200653 }
654
Philipp Maier1cb1e382017-11-02 17:16:04 +0100655 mgcp_rtp_end_config(endp, 0, &conn->end);
656
Philipp Maierc3cc6542018-02-02 12:58:42 +0100657 /* check connection mode setting */
658 if (conn->conn->mode != MGCP_CONN_LOOPBACK
659 && conn->conn->mode != MGCP_CONN_RECV_ONLY
660 && conn->end.rtp_port == 0) {
661 LOGP(DLMGCP, LOGL_ERROR,
662 "CRCX: endpoint:%x selected connection mode type requires an opposite end!\n",
663 ENDPOINT_NUMBER(endp));
664 error_code = 527;
665 goto error2;
666 }
667
Philipp Maier1cb1e382017-11-02 17:16:04 +0100668 if (allocate_port(endp, conn) != 0) {
669 goto error2;
670 }
671
Philipp Maier87bd9be2017-08-22 16:35:41 +0200672 if (setup_rtp_processing(endp, conn) != 0) {
673 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100674 "CRCX: endpoint:0x%x could not start RTP processing!\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200675 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200676 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200677 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200678
679 /* policy CB */
680 if (p->cfg->policy_cb) {
681 int rc;
682 rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200683 MGCP_ENDP_CRCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200684 switch (rc) {
685 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200686 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100687 "CRCX: endpoint:0x%x CRCX rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200688 ENDPOINT_NUMBER(endp));
Philipp Maier1355d7e2018-02-01 14:30:06 +0100689 mgcp_endp_release(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200690 return create_err_response(endp, 400, "CRCX", p->trans);
691 break;
692 case MGCP_POLICY_DEFER:
693 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200694 return NULL;
695 break;
696 case MGCP_POLICY_CONT:
697 /* just continue */
698 break;
699 }
700 }
701
Philipp Maier87bd9be2017-08-22 16:35:41 +0200702 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100703 "CRCX: endpoint:0x%x Creating connection: CI: %s port: %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200704 ENDPOINT_NUMBER(endp), conn->conn->id, conn->end.local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200705 if (p->cfg->change_cb)
706 p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX);
707
Philipp Maiere726d4f2017-11-01 10:41:34 +0100708 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
709 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200710 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
Philipp Maiere726d4f2017-11-01 10:41:34 +0100711 && tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200712 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200713
Philipp Maier87bd9be2017-08-22 16:35:41 +0200714 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100715 "CRCX: endpoint:0x%x connection successfully created\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200716 ENDPOINT_NUMBER(endp));
Philipp Maier55295f72018-01-15 14:00:28 +0100717 return create_response_with_sdp(endp, conn, "CRCX", p->trans, true);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200718error2:
Philipp Maier1355d7e2018-02-01 14:30:06 +0100719 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200720 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100721 "CRCX: endpoint:0x%x unable to create connection resource error\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200722 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200723 return create_err_response(endp, error_code, "CRCX", p->trans);
724}
725
Philipp Maier87bd9be2017-08-22 16:35:41 +0200726/* MDCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200727static struct msgb *handle_modify_con(struct mgcp_parse_data *p)
728{
729 struct mgcp_endpoint *endp = p->endp;
730 int error_code = 500;
731 int silent = 0;
732 int have_sdp = 0;
733 char *line;
734 const char *local_options = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200735 const char *mode = NULL;
736 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier01d24a32017-11-21 17:26:09 +0100737 const char *conn_id = NULL;
Philipp Maiera390d0b2018-01-31 17:30:19 +0100738 int rc;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200739
740 LOGP(DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200741
Philipp Maier87bd9be2017-08-22 16:35:41 +0200742 if (llist_count(&endp->conns) <= 0) {
743 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100744 "MDCX: endpoint:0x%x endpoint is not holding a connection.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200745 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200746 return create_err_response(endp, 400, "MDCX", p->trans);
747 }
748
749 for_each_line(line, p->save) {
750 if (!mgcp_check_param(endp, line))
751 continue;
752
753 switch (line[0]) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200754 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +0100755 if (mgcp_verify_call_id(endp, line + 3) != 0) {
756 error_code = 516;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200757 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +0100758 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200759 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200760 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +0100761 conn_id = (const char *)line + 3;
Harald Weltee35eeae2017-12-28 13:47:37 +0100762 if (mgcp_verify_ci(endp, conn_id) != 0) {
763 error_code = 515;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200764 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +0100765 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200766 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200767 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200768 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200769 break;
770 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200771 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200772 break;
773 case 'Z':
774 silent = strcmp("noanswer", line + 3) == 0;
775 break;
776 case '\0':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200777 have_sdp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200778 goto mgcp_header_done;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200779 break;
780 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200781 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100782 "MDCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200783 ENDPOINT_NUMBER(endp), line[0], line[0]);
Philipp Maierdd0c5222018-02-02 11:08:48 +0100784 return create_err_response(NULL, 539, "MDCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200785 break;
786 }
787 }
788
Philipp Maier87bd9be2017-08-22 16:35:41 +0200789mgcp_header_done:
Philipp Maier01d24a32017-11-21 17:26:09 +0100790 if (!conn_id) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200791 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100792 "MDCX: endpoint:0x%x insufficient parameters, missing ci (connectionIdentifier)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200793 ENDPOINT_NUMBER(endp));
Harald Weltee35eeae2017-12-28 13:47:37 +0100794 return create_err_response(endp, 515, "MDCX", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200795 }
796
797 conn = mgcp_conn_get_rtp(endp, conn_id);
798 if (!conn)
799 return create_err_response(endp, 400, "MDCX", p->trans);
800
801 if (mode) {
802 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
803 error_code = 517;
804 goto error3;
805 }
806 } else
807 conn->conn->mode = conn->conn->mode_orig;
808
809 if (have_sdp)
Philipp Maier8970c492017-10-11 13:33:42 +0200810 mgcp_parse_sdp_data(endp, conn, p);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200811
Philipp Maiera390d0b2018-01-31 17:30:19 +0100812 rc = set_local_cx_options(endp->tcfg->endpoints, &endp->local_options,
813 local_options);
814 if (rc != 0) {
815 LOGP(DLMGCP, LOGL_ERROR,
816 "MDCX: endpoint:%x inavlid local connection options!\n",
817 ENDPOINT_NUMBER(endp));
Philipp Maieraf07f662018-02-02 11:34:02 +0100818 error_code = rc;
819 goto error3;
Philipp Maiera390d0b2018-01-31 17:30:19 +0100820 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200821
822 if (!have_sdp && endp->local_options.codec)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200823 mgcp_set_audio_info(p->cfg, &conn->end.codec,
824 PTYPE_UNDEFINED, endp->local_options.codec);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200825
Philipp Maierc3cc6542018-02-02 12:58:42 +0100826 /* check connection mode setting */
827 if (conn->conn->mode != MGCP_CONN_LOOPBACK
828 && conn->conn->mode != MGCP_CONN_RECV_ONLY
829 && conn->end.rtp_port == 0) {
830 LOGP(DLMGCP, LOGL_ERROR,
831 "MDCX: endpoint:%x selected connection mode type requires an opposite end!\n",
832 ENDPOINT_NUMBER(endp));
833 error_code = 527;
834 goto error3;
835 }
836
Philipp Maier87bd9be2017-08-22 16:35:41 +0200837 if (setup_rtp_processing(endp, conn) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200838 goto error3;
839
Philipp Maier87bd9be2017-08-22 16:35:41 +0200840
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200841 /* policy CB */
842 if (p->cfg->policy_cb) {
843 int rc;
844 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200845 MGCP_ENDP_MDCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200846 switch (rc) {
847 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200848 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100849 "MDCX: endpoint:0x%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200850 ENDPOINT_NUMBER(endp));
851 if (silent)
852 goto out_silent;
853 return create_err_response(endp, 400, "MDCX", p->trans);
854 break;
855 case MGCP_POLICY_DEFER:
856 /* stop processing */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200857 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100858 "MDCX: endpoint:0x%x defered by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200859 ENDPOINT_NUMBER(endp));
860 return NULL;
861 break;
862 case MGCP_POLICY_CONT:
863 /* just continue */
864 break;
865 }
866 }
867
Philipp Maier87bd9be2017-08-22 16:35:41 +0200868 mgcp_rtp_end_config(endp, 1, &conn->end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200869
870 /* modify */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200871 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100872 "MDCX: endpoint:0x%x modified conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200873 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200874 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200875 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
876 MGCP_ENDP_MDCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200877
Philipp Maiere726d4f2017-11-01 10:41:34 +0100878 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
879 OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
880 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
881 && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200882 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200883
884 if (silent)
885 goto out_silent;
886
Philipp Maier87bd9be2017-08-22 16:35:41 +0200887 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100888 "MDCX: endpoint:0x%x connection successfully modified\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200889 ENDPOINT_NUMBER(endp));
Philipp Maier55295f72018-01-15 14:00:28 +0100890 return create_response_with_sdp(endp, conn, "MDCX", p->trans, false);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200891error3:
892 return create_err_response(endp, error_code, "MDCX", p->trans);
893
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200894out_silent:
Philipp Maier230e4fc2017-11-28 09:38:45 +0100895 LOGP(DLMGCP, LOGL_DEBUG, "MDCX: endpoint:0x%x silent exit\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200896 ENDPOINT_NUMBER(endp));
897 return NULL;
898}
899
Philipp Maier87bd9be2017-08-22 16:35:41 +0200900/* DLCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200901static struct msgb *handle_delete_con(struct mgcp_parse_data *p)
902{
903 struct mgcp_endpoint *endp = p->endp;
904 int error_code = 400;
905 int silent = 0;
906 char *line;
907 char stats[1048];
Philipp Maier01d24a32017-11-21 17:26:09 +0100908 const char *conn_id = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200909 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200910
911 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100912 "DLCX: endpoint:0x%x deleting connection ...\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200913 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200914
Philipp Maier87bd9be2017-08-22 16:35:41 +0200915 if (llist_count(&endp->conns) <= 0) {
916 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100917 "DLCX: endpoint:0x%x endpoint is not holding a connection.\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200918 ENDPOINT_NUMBER(endp));
Harald Weltee35eeae2017-12-28 13:47:37 +0100919 return create_err_response(endp, 515, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200920 }
921
922 for_each_line(line, p->save) {
923 if (!mgcp_check_param(endp, line))
924 continue;
925
926 switch (line[0]) {
927 case 'C':
Harald Weltee35eeae2017-12-28 13:47:37 +0100928 if (mgcp_verify_call_id(endp, line + 3) != 0) {
929 error_code = 516;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200930 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +0100931 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200932 break;
933 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +0100934 conn_id = (const char *)line + 3;
Harald Weltee35eeae2017-12-28 13:47:37 +0100935 if (mgcp_verify_ci(endp, conn_id) != 0) {
936 error_code = 515;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200937 goto error3;
Harald Weltee35eeae2017-12-28 13:47:37 +0100938 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200939 break;
940 case 'Z':
941 silent = strcmp("noanswer", line + 3) == 0;
942 break;
943 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200944 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100945 "DLCX: endpoint:0x%x Unhandled MGCP option: '%c'/%d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200946 ENDPOINT_NUMBER(endp), line[0], line[0]);
Philipp Maierdd0c5222018-02-02 11:08:48 +0100947 return create_err_response(NULL, 539, "DLCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200948 break;
949 }
950 }
951
952 /* policy CB */
953 if (p->cfg->policy_cb) {
954 int rc;
955 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200956 MGCP_ENDP_DLCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200957 switch (rc) {
958 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200959 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100960 "DLCX: endpoint:0x%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200961 ENDPOINT_NUMBER(endp));
962 if (silent)
963 goto out_silent;
964 return create_err_response(endp, 400, "DLCX", p->trans);
965 break;
966 case MGCP_POLICY_DEFER:
967 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200968 return NULL;
969 break;
970 case MGCP_POLICY_CONT:
971 /* just continue */
972 break;
973 }
974 }
975
Philipp Maierf4c0e372017-10-11 16:06:45 +0200976 /* When no connection id is supplied, we will interpret this as a
977 * wildcarded DLCX and drop all connections at once. (See also
978 * RFC3435 Section F.7) */
Philipp Maier01d24a32017-11-21 17:26:09 +0100979 if (!conn_id) {
Philipp Maierf4c0e372017-10-11 16:06:45 +0200980 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100981 "DLCX: endpoint:0x%x missing ci (connectionIdentifier), will remove all connections at once\n",
Philipp Maierf4c0e372017-10-11 16:06:45 +0200982 ENDPOINT_NUMBER(endp));
983
Philipp Maier1355d7e2018-02-01 14:30:06 +0100984 mgcp_endp_release(endp);
Philipp Maierf4c0e372017-10-11 16:06:45 +0200985
986 /* Note: In this case we do not return any statistics,
987 * as we assume that the client is not interested in
988 * this case. */
989 return create_ok_response(endp, 200, "DLCX", p->trans);
990 }
991
Philipp Maierf4c0e372017-10-11 16:06:45 +0200992 /* Find the connection */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200993 conn = mgcp_conn_get_rtp(endp, conn_id);
994 if (!conn)
995 goto error3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200996
Philipp Maier87bd9be2017-08-22 16:35:41 +0200997 /* save the statistics of the current connection */
998 mgcp_format_stats(stats, sizeof(stats), conn->conn);
999
1000 /* delete connection */
Philipp Maier230e4fc2017-11-28 09:38:45 +01001001 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x deleting conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001002 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
1003 mgcp_conn_free(endp, conn_id);
1004 LOGP(DLMGCP, LOGL_NOTICE,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001005 "DLCX: endpoint:0x%x connection successfully deleted\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001006 ENDPOINT_NUMBER(endp));
1007
1008 /* When all connections are closed, the endpoint will be released
1009 * in order to be ready to be used by another call. */
1010 if (llist_count(&endp->conns) <= 0) {
Philipp Maier1355d7e2018-02-01 14:30:06 +01001011 mgcp_endp_release(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001012 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +01001013 "DLCX: endpoint:0x%x endpoint released\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001014 ENDPOINT_NUMBER(endp));
1015 }
1016
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001017 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001018 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
1019 MGCP_ENDP_DLCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001020
1021 if (silent)
1022 goto out_silent;
1023 return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats);
1024
1025error3:
1026 return create_err_response(endp, error_code, "DLCX", p->trans);
1027
1028out_silent:
Philipp Maier230e4fc2017-11-28 09:38:45 +01001029 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:0x%x silent exit\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +02001030 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001031 return NULL;
1032}
1033
Philipp Maier87bd9be2017-08-22 16:35:41 +02001034/* RSIP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001035static struct msgb *handle_rsip(struct mgcp_parse_data *p)
1036{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001037 /* TODO: Also implement the resetting of a specific endpoint
1038 * to make mgcp_send_reset_ep() work. Currently this will call
1039 * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1
1040 * to make read_call_agent() reset all endpoints when called
1041 * next time. In order to selectively reset endpoints some
1042 * mechanism to distinguish which endpoint shall be resetted
1043 * is needed */
1044
1045 LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n");
1046
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001047 if (p->cfg->reset_cb)
1048 p->cfg->reset_cb(p->endp->tcfg);
1049 return NULL;
1050}
1051
1052static char extract_tone(const char *line)
1053{
1054 const char *str = strstr(line, "D/");
1055 if (!str)
1056 return CHAR_MAX;
1057
1058 return str[2];
1059}
1060
Philipp Maier87bd9be2017-08-22 16:35:41 +02001061/* This can request like DTMF detection and forward, fax detection... it
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001062 * can also request when the notification should be send and such. We don't
Philipp Maier87bd9be2017-08-22 16:35:41 +02001063 * do this right now. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001064static struct msgb *handle_noti_req(struct mgcp_parse_data *p)
1065{
1066 int res = 0;
1067 char *line;
1068 char tone = CHAR_MAX;
1069
Philipp Maier87bd9be2017-08-22 16:35:41 +02001070 LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n");
1071
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001072 for_each_line(line, p->save) {
1073 switch (line[0]) {
1074 case 'S':
1075 tone = extract_tone(line);
1076 break;
1077 }
1078 }
1079
1080 /* we didn't see a signal request with a tone */
1081 if (tone == CHAR_MAX)
1082 return create_ok_response(p->endp, 200, "RQNT", p->trans);
1083
1084 if (p->cfg->rqnt_cb)
1085 res = p->cfg->rqnt_cb(p->endp, tone);
1086
1087 return res == 0 ?
Philipp Maier87bd9be2017-08-22 16:35:41 +02001088 create_ok_response(p->endp, 200, "RQNT", p->trans) :
1089 create_err_response(p->endp, res, "RQNT", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001090}
1091
Philipp Maier87bd9be2017-08-22 16:35:41 +02001092/* Connection keepalive timer, will take care that dummy packets are send
Harald Welte1d1b98f2017-12-25 10:03:40 +01001093 * regularly, so that NAT connections stay open */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001094static void mgcp_keepalive_timer_cb(void *_tcfg)
1095{
1096 struct mgcp_trunk_config *tcfg = _tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001097 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001098 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001099
Philipp Maiere726d4f2017-11-01 10:41:34 +01001100 LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001101 tcfg->trunk_nr);
1102
Philipp Maiere726d4f2017-11-01 10:41:34 +01001103 /* Do not accept invalid configuration values
1104 * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and
1105 * values greater 0 */
1106 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1107
1108 /* The dummy packet functionality has been disabled, we will exit
1109 * immediately, no further timer is scheduled, which means we will no
1110 * longer send dummy packets even when we did before */
1111 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER)
1112 return;
1113
1114 /* In cases where only one dummy packet is sent, we do not need
1115 * the timer since the functions that handle the CRCX and MDCX are
1116 * triggering the sending of the dummy packet. So we behave like in
1117 * the MGCP_KEEPALIVE_NEVER case */
1118 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001119 return;
1120
Philipp Maier87bd9be2017-08-22 16:35:41 +02001121 /* Send walk over all endpoints and send out dummy packets through
1122 * every connection present on each endpoint */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001123 for (i = 1; i < tcfg->number_endpoints; ++i) {
1124 struct mgcp_endpoint *endp = &tcfg->endpoints[i];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001125 llist_for_each_entry(conn, &endp->conns, entry) {
1126 if (conn->mode == MGCP_CONN_RECV_ONLY)
1127 send_dummy(endp, &conn->u.rtp);
1128 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001129 }
1130
Philipp Maiere726d4f2017-11-01 10:41:34 +01001131 /* Schedule the keepalive timer for the next round */
1132 LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001133 tcfg->trunk_nr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001134 osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval,
1135 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001136}
1137
1138void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval)
1139{
1140 tcfg->keepalive_interval = interval;
1141 osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg);
1142
1143 if (interval <= 0)
1144 osmo_timer_del(&tcfg->keepalive_timer);
1145 else
1146 osmo_timer_schedule(&tcfg->keepalive_timer,
1147 tcfg->keepalive_interval, 0);
1148}
1149
Philipp Maier87bd9be2017-08-22 16:35:41 +02001150/*! allocate configuration with default values.
1151 * (called once at startup by main function) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001152struct mgcp_config *mgcp_config_alloc(void)
1153{
1154 struct mgcp_config *cfg;
1155
1156 cfg = talloc_zero(NULL, struct mgcp_config);
1157 if (!cfg) {
1158 LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n");
1159 return NULL;
1160 }
1161
Philipp Maier12943ea2018-01-17 15:40:25 +01001162 osmo_strlcpy(cfg->domain, "mgw", sizeof(cfg->domain));
1163
Philipp Maier87bd9be2017-08-22 16:35:41 +02001164 cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START;
1165 cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END;
1166 cfg->net_ports.last_port = cfg->net_ports.range_start;
1167
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001168 cfg->source_port = 2427;
1169 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
1170 cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
1171
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001172 cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
1173 cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
1174
1175 cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
1176
1177 /* default trunk handling */
1178 cfg->trunk.cfg = cfg;
1179 cfg->trunk.trunk_nr = 0;
1180 cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
1181 cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
1182 cfg->trunk.audio_payload = 126;
1183 cfg->trunk.audio_send_ptime = 1;
1184 cfg->trunk.audio_send_name = 1;
1185 cfg->trunk.omit_rtcp = 0;
1186 mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE);
1187
1188 INIT_LLIST_HEAD(&cfg->trunks);
1189
1190 return cfg;
1191}
1192
Philipp Maier87bd9be2017-08-22 16:35:41 +02001193/*! allocate configuration with default values.
1194 * (called once at startup by VTY)
1195 * \param[in] cfg mgcp configuration
1196 * \param[in] nr trunk number
1197 * \returns pointer to allocated trunk configuration */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001198struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
1199{
1200 struct mgcp_trunk_config *trunk;
1201
1202 trunk = talloc_zero(cfg, struct mgcp_trunk_config);
1203 if (!trunk) {
1204 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n");
1205 return NULL;
1206 }
1207
1208 trunk->cfg = cfg;
1209 trunk->trunk_type = MGCP_TRUNK_E1;
1210 trunk->trunk_nr = nr;
1211 trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
1212 trunk->audio_payload = 126;
1213 trunk->audio_send_ptime = 1;
1214 trunk->audio_send_name = 1;
Philipp Maierfcd06552017-11-10 17:32:22 +01001215 trunk->vty_number_endpoints = 33;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001216 trunk->omit_rtcp = 0;
1217 mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
1218 llist_add_tail(&trunk->entry, &cfg->trunks);
1219 return trunk;
1220}
1221
Philipp Maier87bd9be2017-08-22 16:35:41 +02001222/*! get trunk configuration by trunk number (index).
1223 * \param[in] cfg mgcp configuration
1224 * \param[in] index trunk number
1225 * \returns pointer to trunk configuration, NULL on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001226struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
1227{
1228 struct mgcp_trunk_config *trunk;
1229
1230 llist_for_each_entry(trunk, &cfg->trunks, entry)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001231 if (trunk->trunk_nr == index)
1232 return trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001233
1234 return NULL;
1235}
1236
Philipp Maier87bd9be2017-08-22 16:35:41 +02001237/*! allocate endpoints and set default values.
1238 * (called once at startup by VTY)
1239 * \param[in] tcfg trunk configuration
1240 * \returns 0 on success, -1 on failure */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001241int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
1242{
1243 int i;
1244
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001245 tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001246 sizeof(struct mgcp_endpoint),
Philipp Maierfcd06552017-11-10 17:32:22 +01001247 tcfg->vty_number_endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001248 "endpoints");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001249 if (!tcfg->endpoints)
1250 return -1;
1251
Philipp Maierfcd06552017-11-10 17:32:22 +01001252 for (i = 0; i < tcfg->vty_number_endpoints; ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001253 INIT_LLIST_HEAD(&tcfg->endpoints[i].conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001254 tcfg->endpoints[i].cfg = tcfg->cfg;
1255 tcfg->endpoints[i].tcfg = tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001256
1257 /* NOTE: Currently all endpoints are of type RTP, this will
1258 * change when new variations are implemented */
1259 tcfg->endpoints[i].type = &ep_typeset.rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001260 }
1261
Philipp Maierfcd06552017-11-10 17:32:22 +01001262 tcfg->number_endpoints = tcfg->vty_number_endpoints;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001263 return 0;
1264}
1265
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001266static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1267{
1268 return write(cfg->gw_fd.bfd.fd, buf, len);
1269}
1270
Philipp Maier87bd9be2017-08-22 16:35:41 +02001271/*! Reset all endpoints by sending RSIP message to self.
1272 * (called by VTY)
1273 * \param[in] endp trunk endpoint
1274 * \param[in] endpoint number
1275 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001276int mgcp_send_reset_all(struct mgcp_config *cfg)
1277{
Philipp Maier12943ea2018-01-17 15:40:25 +01001278 char buf[MGCP_ENDPOINT_MAXLEN + 128];
1279 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001280 int rc;
1281
Philipp Maier12943ea2018-01-17 15:40:25 +01001282 len = snprintf(buf, sizeof(buf),
1283 "RSIP 1 *@%s MGCP 1.0\r\n", cfg->domain);
1284 if (len < 0)
1285 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001286
Philipp Maier12943ea2018-01-17 15:40:25 +01001287 rc = send_agent(cfg, buf, len);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001288 if (rc <= 0)
1289 return -1;
1290
1291 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001292}
1293
Philipp Maier87bd9be2017-08-22 16:35:41 +02001294/*! Reset a single endpoint by sending RSIP message to self.
1295 * (called by VTY)
1296 * \param[in] endp trunk endpoint
1297 * \param[in] endpoint number
1298 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001299int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
1300{
Philipp Maier12943ea2018-01-17 15:40:25 +01001301 char buf[MGCP_ENDPOINT_MAXLEN + 128];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001302 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001303 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001304
1305 len = snprintf(buf, sizeof(buf),
Philipp Maier12943ea2018-01-17 15:40:25 +01001306 "RSIP 39 %x@%s MGCP 1.0\r\n", endpoint, endp->cfg->domain);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001307 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001308 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001309
Philipp Maier87bd9be2017-08-22 16:35:41 +02001310 rc = send_agent(endp->cfg, buf, len);
1311 if (rc <= 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001312 return -1;
1313
Philipp Maier87bd9be2017-08-22 16:35:41 +02001314 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001315}