blob: 9c92c65b8f0901d294b793aedb9b72018f1d5475 [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>
41#include <osmocom/mgcp/mgcp_ep.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 Maier87bd9be2017-08-22 16:35:41 +0200195/* Format MGCP response string (with SDP attached) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200196static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200197 struct mgcp_conn_rtp *conn,
198 const char *msg,
199 const char *trans_id)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200200{
201 const char *addr = endp->cfg->local_ip;
Philipp Maier8970c492017-10-11 13:33:42 +0200202 struct msgb *sdp;
203 int rc;
204 struct msgb *result;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200205 char osmux_extension[strlen("\nX-Osmux: 255") + 1];
206
Philipp Maier8970c492017-10-11 13:33:42 +0200207 sdp = msgb_alloc_headroom(4096, 128, "sdp record");
208 if (!sdp)
209 return NULL;
210
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200211 if (!addr)
212 addr = mgcp_net_src_addr(endp);
213
Philipp Maier87bd9be2017-08-22 16:35:41 +0200214 if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) {
215 sprintf(osmux_extension, "\nX-Osmux: %u", conn->osmux.cid);
216 conn->osmux.state = OSMUX_STATE_ACTIVATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200217 } else {
218 osmux_extension[0] = '\0';
219 }
220
Philipp Maier8970c492017-10-11 13:33:42 +0200221 rc = msgb_printf(sdp, "I: %u%s\n\n", conn->conn->id, osmux_extension);
222 if (rc < 0)
223 goto error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200224
Philipp Maier8970c492017-10-11 13:33:42 +0200225 rc = mgcp_write_response_sdp(endp, conn, sdp, addr);
226 if (rc < 0)
227 goto error;
228 result = create_resp(endp, 200, " OK", msg, trans_id, NULL, (char*) sdp->data);
229 msgb_free(sdp);
230 return result;
231error:
232 msgb_free(sdp);
233 return NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200234}
235
Philipp Maier87bd9be2017-08-22 16:35:41 +0200236/* Send out dummy packet to keep the connection open, if the connection is an
237 * osmux connection, send the dummy packet via OSMUX */
238static void send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200239{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200240 if (conn->osmux.state != OSMUX_STATE_DISABLED)
241 osmux_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200242 else
Philipp Maier87bd9be2017-08-22 16:35:41 +0200243 mgcp_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200244}
245
Philipp Maier87bd9be2017-08-22 16:35:41 +0200246/* handle incoming messages:
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200247 * - this can be a command (four letters, space, transaction id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200248 * - or a response (three numbers, space, transaction id) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200249struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
250{
251 struct mgcp_parse_data pdata;
252 int i, code, handled = 0;
253 struct msgb *resp = NULL;
254 char *data;
255
256 if (msgb_l2len(msg) < 4) {
257 LOGP(DLMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len);
258 return NULL;
259 }
260
261 if (mgcp_msg_terminate_nul(msg))
262 return NULL;
263
Philipp Maier87bd9be2017-08-22 16:35:41 +0200264 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Received message");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200265
Philipp Maier87bd9be2017-08-22 16:35:41 +0200266 /* attempt to treat it as a response */
267 if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200268 LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code);
269 return NULL;
270 }
271
272 msg->l3h = &msg->l2h[4];
273
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200274 /*
275 * Check for a duplicate message and respond.
276 */
277 memset(&pdata, 0, sizeof(pdata));
278 pdata.cfg = cfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200279 data = mgcp_strline((char *)msg->l3h, &pdata.save);
280 pdata.found = mgcp_parse_header(&pdata, data);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200281 if (pdata.endp && pdata.trans
Philipp Maier87bd9be2017-08-22 16:35:41 +0200282 && pdata.endp->last_trans
283 && strcmp(pdata.endp->last_trans, pdata.trans) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200284 return do_retransmission(pdata.endp);
285 }
286
287 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200288 if (strncmp
289 (mgcp_requests[i].name, (const char *)&msg->l2h[0],
290 4) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200291 handled = 1;
292 resp = mgcp_requests[i].handle_request(&pdata);
293 break;
294 }
295 }
296
297 if (!handled)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200298 LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n",
299 &msg->l2h[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200300
301 return resp;
302}
303
Philipp Maier87bd9be2017-08-22 16:35:41 +0200304/* AUEP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200305static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p)
306{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200307 LOGP(DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n");
308
309 if (p->found != 0) {
310 LOGP(DLMGCP, LOGL_ERROR,
311 "AUEP: failed to find the endpoint.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200312 return create_err_response(NULL, 500, "AUEP", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200313 } else
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200314 return create_ok_response(p->endp, 200, "AUEP", p->trans);
315}
316
Philipp Maier87bd9be2017-08-22 16:35:41 +0200317/* Try to find a free port by attemting to bind on it. Also handle the
318 * counter that points on the next free port. Since we have a pointer
319 * to the next free port, binding should work on the first attemt,
320 * neverless, try at least the next 200 ports before giving up */
321static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200322{
323 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200324 struct mgcp_rtp_end *end;
325 struct mgcp_port_range *range;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200326
Philipp Maier87bd9be2017-08-22 16:35:41 +0200327 OSMO_ASSERT(conn);
328 end = &conn->end;
329 OSMO_ASSERT(end);
330
331 range = &endp->cfg->net_ports;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200332
333 /* attempt to find a port */
334 for (i = 0; i < 200; ++i) {
335 int rc;
336
337 if (range->last_port >= range->range_end)
338 range->last_port = range->range_start;
339
Philipp Maier87bd9be2017-08-22 16:35:41 +0200340 rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200341
342 range->last_port += 2;
343 if (rc == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200344 return 0;
345 }
346
347 }
348
Philipp Maier87bd9be2017-08-22 16:35:41 +0200349 LOGP(DLMGCP, LOGL_ERROR,
350 "Allocating a RTP/RTCP port failed 200 times 0x%x.\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200351 ENDPOINT_NUMBER(endp));
352 return -1;
353}
354
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200355/* Set the LCO from a string (see RFC 3435).
Philipp Maier87bd9be2017-08-22 16:35:41 +0200356 * The string is stored in the 'string' field. A NULL string is handled excatlyy
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200357 * like an empty string, the 'string' field is never NULL after this function
358 * has been called. */
359static void set_local_cx_options(void *ctx, struct mgcp_lco *lco,
360 const char *options)
361{
362 char *p_opt, *a_opt;
363 char codec[9];
364
365 talloc_free(lco->string);
366 talloc_free(lco->codec);
367 lco->codec = NULL;
368 lco->pkt_period_min = lco->pkt_period_max = 0;
369 lco->string = talloc_strdup(ctx, options ? options : "");
370
371 p_opt = strstr(lco->string, "p:");
372 if (p_opt && sscanf(p_opt, "p:%d-%d",
373 &lco->pkt_period_min, &lco->pkt_period_max) == 1)
374 lco->pkt_period_max = lco->pkt_period_min;
375
376 a_opt = strstr(lco->string, "a:");
377 if (a_opt && sscanf(a_opt, "a:%8[^,]", codec) == 1)
378 lco->codec = talloc_strdup(ctx, codec);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200379
380 LOGP(DLMGCP, LOGL_DEBUG,
381 "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n",
382 lco->pkt_period_max, lco->codec);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200383}
384
385void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
386 struct mgcp_rtp_end *rtp)
387{
388 struct mgcp_trunk_config *tcfg = endp->tcfg;
389
390 int patch_ssrc = expect_ssrc_change && tcfg->force_constant_ssrc;
391
392 rtp->force_aligned_timing = tcfg->force_aligned_timing;
393 rtp->force_constant_ssrc = patch_ssrc ? 1 : 0;
394
395 LOGP(DLMGCP, LOGL_DEBUG,
396 "Configuring RTP endpoint: local port %d%s%s\n",
397 ntohs(rtp->rtp_port),
398 rtp->force_aligned_timing ? ", force constant timing" : "",
399 rtp->force_constant_ssrc ? ", force constant ssrc" : "");
400}
401
402uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
403 struct mgcp_rtp_end *rtp)
404{
405 int f = 0;
406
407 /* Get the number of frames per channel and packet */
408 if (rtp->frames_per_packet)
409 f = rtp->frames_per_packet;
410 else if (rtp->packet_duration_ms && rtp->codec.frame_duration_num) {
411 int den = 1000 * rtp->codec.frame_duration_num;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200412 f = (rtp->packet_duration_ms * rtp->codec.frame_duration_den +
413 den / 2)
414 / den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200415 }
416
Philipp Maier87bd9be2017-08-22 16:35:41 +0200417 return rtp->codec.rate * f * rtp->codec.frame_duration_num /
418 rtp->codec.frame_duration_den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200419}
420
421static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line)
422{
423 if (!endp->cfg->osmux_init) {
424 if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) {
425 LOGP(DLMGCP, LOGL_ERROR, "Cannot init OSMUX\n");
426 return -1;
427 }
428 LOGP(DLMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n");
429 }
430
431 return mgcp_parse_osmux_cid(line);
432}
433
Philipp Maier87bd9be2017-08-22 16:35:41 +0200434/* CRCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200435static struct msgb *handle_create_con(struct mgcp_parse_data *p)
436{
437 struct mgcp_trunk_config *tcfg;
438 struct mgcp_endpoint *endp = p->endp;
439 int error_code = 400;
440
441 const char *local_options = NULL;
442 const char *callid = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200443 const char *ci = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200444 const char *mode = NULL;
445 char *line;
446 int have_sdp = 0, osmux_cid = -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200447 struct mgcp_conn_rtp *conn = NULL;
448 uint32_t conn_id;
449 char conn_name[512];
450
451 LOGP(DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200452
453 if (p->found != 0)
454 return create_err_response(NULL, 510, "CRCX", p->trans);
455
456 /* parse CallID C: and LocalParameters L: */
457 for_each_line(line, p->save) {
458 if (!mgcp_check_param(endp, line))
459 continue;
460
461 switch (line[0]) {
462 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200463 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200464 break;
465 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200466 callid = (const char *)line + 3;
467 break;
468 case 'I':
469 ci = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200470 break;
471 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200472 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200473 break;
474 case 'X':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200475 /* If osmoux is disabled, just skip setting it up */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200476 if (!p->endp->cfg->osmux)
477 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200478 if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) ==
479 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200480 osmux_cid = mgcp_osmux_setup(endp, line);
481 break;
482 case '\0':
483 have_sdp = 1;
484 goto mgcp_header_done;
485 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200486 LOGP(DLMGCP, LOGL_NOTICE,
487 "CRCX: endpoint:%x unhandled option: '%c'/%d\n",
488 ENDPOINT_NUMBER(endp), *line, *line);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200489 break;
490 }
491 }
492
493mgcp_header_done:
494 tcfg = p->endp->tcfg;
495
Philipp Maier87bd9be2017-08-22 16:35:41 +0200496 /* Check parameters */
497 if (!callid) {
498 LOGP(DLMGCP, LOGL_ERROR,
499 "CRCX: endpoint:%x insufficient parameters, missing callid\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200500 ENDPOINT_NUMBER(endp));
501 return create_err_response(endp, 400, "CRCX", p->trans);
502 }
503
Philipp Maier87bd9be2017-08-22 16:35:41 +0200504 if (!mode) {
505 LOGP(DLMGCP, LOGL_ERROR,
506 "CRCX: endpoint:%x insufficient parameters, missing mode\n",
507 ENDPOINT_NUMBER(endp));
508 return create_err_response(endp, 400, "CRCX", p->trans);
509 }
510
511 if (!ci) {
512 LOGP(DLMGCP, LOGL_ERROR,
513 "CRCX: endpoint:%x insufficient parameters, missing connection id\n",
514 ENDPOINT_NUMBER(endp));
515 return create_err_response(endp, 400, "CRCX", p->trans);
516 }
517
518 /* Check if we are able to accept the creation of another connection */
519 if (llist_count(&endp->conns) >= endp->type->max_conns) {
520 LOGP(DLMGCP, LOGL_ERROR,
521 "CRCX: endpoint:%x endpoint full, max. %i connections allowed!\n",
522 endp->type->max_conns, ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200523 if (tcfg->force_realloc) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200524 /* There is no more room for a connection, make some
525 * room by blindly tossing the oldest of the two two
526 * connections */
527 mgcp_conn_free_oldest(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200528 } else {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200529 /* There is no more room for a connection, leave
530 * everything as it is and return with an error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200531 return create_err_response(endp, 400, "CRCX", p->trans);
532 }
533 }
534
Philipp Maier87bd9be2017-08-22 16:35:41 +0200535 /* Check if this endpoint already serves a call, if so, check if the
536 * callids match up so that we are sure that this is our call */
537 if (endp->callid && mgcp_verify_call_id(endp, callid)) {
538 LOGP(DLMGCP, LOGL_ERROR,
539 "CRCX: endpoint:%x allready seized by other call (%s)\n",
540 ENDPOINT_NUMBER(endp), endp->callid);
541 if (tcfg->force_realloc)
542 /* This is not our call, toss everything by releasing
543 * the entire endpoint. (rude!) */
544 mgcp_release_endp(endp);
545 else {
546 /* This is not our call, leave everything as it is and
547 * return with an error. */
548 return create_err_response(endp, 400, "CRCX", p->trans);
549 }
550 }
551
552 /* Set the callid, creation of another connection will only be possible
553 * when the callid matches up. (Connections are distinuished by their
554 * connection ids) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200555 endp->callid = talloc_strdup(tcfg->endpoints, callid);
556
Philipp Maier87bd9be2017-08-22 16:35:41 +0200557 /* Extract audio codec information */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200558 set_local_cx_options(endp->tcfg->endpoints, &endp->local_options,
559 local_options);
560
Philipp Maier87bd9be2017-08-22 16:35:41 +0200561 if (mgcp_parse_ci(&conn_id, ci)) {
562 LOGP(DLMGCP, LOGL_ERROR,
563 "CRCX: endpoint:%x insufficient parameters, missing ci (connectionIdentifier)\n",
564 ENDPOINT_NUMBER(endp));
565 return create_err_response(endp, 400, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200566 }
567
Philipp Maier87bd9be2017-08-22 16:35:41 +0200568 /* Only accept another connection when the connection ID is different. */
569 if (mgcp_conn_get_rtp(endp, conn_id)) {
570 LOGP(DLMGCP, LOGL_ERROR,
571 "CRCX: endpoint:%x there is already a connection with id %u present!\n",
572 conn_id, ENDPOINT_NUMBER(endp));
573 if (tcfg->force_realloc) {
574 /* Ignore the existing connection by just freeing it */
575 mgcp_conn_free(endp, conn_id);
576 } else {
577 /* There is already a connection with that ID present,
578 * leave everything as it is and return with an error. */
579 return create_err_response(endp, 400, "CRCX", p->trans);
580 }
581 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200582
Philipp Maier87bd9be2017-08-22 16:35:41 +0200583 snprintf(conn_name, sizeof(conn_name), "%s-%u", callid, conn_id);
584 mgcp_conn_alloc(NULL, endp, conn_id, MGCP_CONN_TYPE_RTP,
585 conn_name);
586 conn = mgcp_conn_get_rtp(endp, conn_id);
587 if (!conn) {
588 LOGP(DLMGCP, LOGL_ERROR,
589 "CRCX: endpoint:%x unable to allocate RTP connection\n",
590 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200591 goto error2;
592
Philipp Maier87bd9be2017-08-22 16:35:41 +0200593 }
594
595 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
596 error_code = 517;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200597 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200598 }
599
600 mgcp_rtp_end_config(endp, 0, &conn->end);
601
602 if (allocate_port(endp, conn) != 0) {
603 goto error2;
604 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200605
606 /* Annotate Osmux circuit ID and set it to negotiating state until this
Philipp Maier87bd9be2017-08-22 16:35:41 +0200607 * is fully set up from the dummy load. */
608 conn->osmux.state = OSMUX_STATE_DISABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200609 if (osmux_cid >= 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200610 conn->osmux.cid = osmux_cid;
611 conn->osmux.state = OSMUX_STATE_NEGOTIATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200612 } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) {
613 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200614 "CRCX: endpoint:%x osmux only and no osmux offered\n",
615 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200616 goto error2;
617 }
618
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200619 /* set up RTP media parameters */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200620 if (have_sdp)
Philipp Maier8970c492017-10-11 13:33:42 +0200621 mgcp_parse_sdp_data(endp, conn, p);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200622 else if (endp->local_options.codec)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200623 mgcp_set_audio_info(p->cfg, &conn->end.codec,
624 PTYPE_UNDEFINED, endp->local_options.codec);
625 conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints,
626 tcfg->audio_fmtp_extra);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200627
Philipp Maier87bd9be2017-08-22 16:35:41 +0200628 if (p->cfg->force_ptime) {
629 conn->end.packet_duration_ms = p->cfg->force_ptime;
630 conn->end.force_output_ptime = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200631 }
632
Philipp Maier87bd9be2017-08-22 16:35:41 +0200633 if (setup_rtp_processing(endp, conn) != 0) {
634 LOGP(DLMGCP, LOGL_ERROR,
635 "CRCX: endpoint:%x could not start RTP processing!\n",
636 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200637 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200638 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200639
640 /* policy CB */
641 if (p->cfg->policy_cb) {
642 int rc;
643 rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200644 MGCP_ENDP_CRCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200645 switch (rc) {
646 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200647 LOGP(DLMGCP, LOGL_NOTICE,
648 "CRCX: endpoint:%x CRCX rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200649 ENDPOINT_NUMBER(endp));
650 mgcp_release_endp(endp);
651 return create_err_response(endp, 400, "CRCX", p->trans);
652 break;
653 case MGCP_POLICY_DEFER:
654 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200655 return NULL;
656 break;
657 case MGCP_POLICY_CONT:
658 /* just continue */
659 break;
660 }
661 }
662
Philipp Maier87bd9be2017-08-22 16:35:41 +0200663 LOGP(DLMGCP, LOGL_DEBUG,
664 "CRCX: endpoint:%x Creating connection: CI: %u port: %u\n",
665 ENDPOINT_NUMBER(endp), conn->conn->id, conn->end.local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200666 if (p->cfg->change_cb)
667 p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX);
668
Philipp Maier87bd9be2017-08-22 16:35:41 +0200669 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
670 && tcfg->keepalive_interval != 0) {
671 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200672 }
673
Philipp Maier87bd9be2017-08-22 16:35:41 +0200674 LOGP(DLMGCP, LOGL_NOTICE,
675 "CRCX: endpoint:%x connection successfully created\n",
676 ENDPOINT_NUMBER(endp));
677 return create_response_with_sdp(endp, conn, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200678error2:
679 mgcp_release_endp(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200680 LOGP(DLMGCP, LOGL_NOTICE,
681 "CRCX: endpoint:%x unable to create connection resource error\n",
682 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200683 return create_err_response(endp, error_code, "CRCX", p->trans);
684}
685
Philipp Maier87bd9be2017-08-22 16:35:41 +0200686/* MDCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200687static struct msgb *handle_modify_con(struct mgcp_parse_data *p)
688{
689 struct mgcp_endpoint *endp = p->endp;
690 int error_code = 500;
691 int silent = 0;
692 int have_sdp = 0;
693 char *line;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200694 const char *ci = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200695 const char *local_options = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200696 const char *mode = NULL;
697 struct mgcp_conn_rtp *conn = NULL;
698 uint32_t conn_id;
699
700 LOGP(DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200701
702 if (p->found != 0)
703 return create_err_response(NULL, 510, "MDCX", p->trans);
704
Philipp Maier87bd9be2017-08-22 16:35:41 +0200705 if (llist_count(&endp->conns) <= 0) {
706 LOGP(DLMGCP, LOGL_ERROR,
707 "MDCX: endpoint:%x endpoint is not holding a connection.\n",
708 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200709 return create_err_response(endp, 400, "MDCX", p->trans);
710 }
711
712 for_each_line(line, p->save) {
713 if (!mgcp_check_param(endp, line))
714 continue;
715
716 switch (line[0]) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200717 case 'C':
718 if (mgcp_verify_call_id(endp, line + 3) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200719 goto error3;
720 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200721 case 'I':
722 ci = (const char *)line + 3;
723 if (mgcp_verify_ci(endp, ci) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200724 goto error3;
725 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200726 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 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200730 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200731 break;
732 case 'Z':
733 silent = strcmp("noanswer", line + 3) == 0;
734 break;
735 case '\0':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200736 have_sdp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200737 goto mgcp_header_done;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200738 break;
739 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200740 LOGP(DLMGCP, LOGL_NOTICE,
741 "MDCX: endpoint:%x Unhandled MGCP option: '%c'/%d\n",
742 ENDPOINT_NUMBER(endp), line[0], line[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200743 break;
744 }
745 }
746
Philipp Maier87bd9be2017-08-22 16:35:41 +0200747mgcp_header_done:
748 if (mgcp_parse_ci(&conn_id, ci)) {
749 LOGP(DLMGCP, LOGL_ERROR,
750 "MDCX: endpoint:%x insufficient parameters, missing ci (connectionIdentifier)\n",
751 ENDPOINT_NUMBER(endp));
752 return create_err_response(endp, 400, "MDCX", p->trans);
753 }
754
755 conn = mgcp_conn_get_rtp(endp, conn_id);
756 if (!conn)
757 return create_err_response(endp, 400, "MDCX", p->trans);
758
759 if (mode) {
760 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
761 error_code = 517;
762 goto error3;
763 }
764 } else
765 conn->conn->mode = conn->conn->mode_orig;
766
767 if (have_sdp)
Philipp Maier8970c492017-10-11 13:33:42 +0200768 mgcp_parse_sdp_data(endp, conn, p);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200769
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200770 set_local_cx_options(endp->tcfg->endpoints, &endp->local_options,
771 local_options);
772
773 if (!have_sdp && endp->local_options.codec)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200774 mgcp_set_audio_info(p->cfg, &conn->end.codec,
775 PTYPE_UNDEFINED, endp->local_options.codec);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200776
Philipp Maier87bd9be2017-08-22 16:35:41 +0200777 if (setup_rtp_processing(endp, conn) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200778 goto error3;
779
Philipp Maier87bd9be2017-08-22 16:35:41 +0200780
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200781 /* policy CB */
782 if (p->cfg->policy_cb) {
783 int rc;
784 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200785 MGCP_ENDP_MDCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200786 switch (rc) {
787 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200788 LOGP(DLMGCP, LOGL_NOTICE,
789 "MDCX: endpoint:%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200790 ENDPOINT_NUMBER(endp));
791 if (silent)
792 goto out_silent;
793 return create_err_response(endp, 400, "MDCX", p->trans);
794 break;
795 case MGCP_POLICY_DEFER:
796 /* stop processing */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200797 LOGP(DLMGCP, LOGL_DEBUG,
798 "MDCX: endpoint:%x defered by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200799 ENDPOINT_NUMBER(endp));
800 return NULL;
801 break;
802 case MGCP_POLICY_CONT:
803 /* just continue */
804 break;
805 }
806 }
807
Philipp Maier87bd9be2017-08-22 16:35:41 +0200808 mgcp_rtp_end_config(endp, 1, &conn->end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200809
810 /* modify */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200811 LOGP(DLMGCP, LOGL_DEBUG,
812 "MDCX: endpoint:%x modified conn:%s\n",
813 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200814 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200815 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
816 MGCP_ENDP_MDCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200817
Philipp Maier87bd9be2017-08-22 16:35:41 +0200818 if (conn->conn->mode & MGCP_CONN_RECV_ONLY &&
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200819 endp->tcfg->keepalive_interval != 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200820 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200821
822 if (silent)
823 goto out_silent;
824
Philipp Maier87bd9be2017-08-22 16:35:41 +0200825 LOGP(DLMGCP, LOGL_NOTICE,
826 "MDCX: endpoint:%x connection successfully modified\n",
827 ENDPOINT_NUMBER(endp));
828 return create_response_with_sdp(endp, conn, "MDCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200829error3:
830 return create_err_response(endp, error_code, "MDCX", p->trans);
831
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200832out_silent:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200833 LOGP(DLMGCP, LOGL_DEBUG, "MDCX: endpoint:%x silent exit\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200834 ENDPOINT_NUMBER(endp));
835 return NULL;
836}
837
Philipp Maier87bd9be2017-08-22 16:35:41 +0200838/* DLCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200839static struct msgb *handle_delete_con(struct mgcp_parse_data *p)
840{
841 struct mgcp_endpoint *endp = p->endp;
842 int error_code = 400;
843 int silent = 0;
844 char *line;
845 char stats[1048];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200846 const char *ci = NULL;
847 struct mgcp_conn_rtp *conn = NULL;
848 uint32_t conn_id;
849
850 LOGP(DLMGCP, LOGL_NOTICE,
851 "DLCX: endpoint:%x deleting connection ...\n",
852 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200853
854 if (p->found != 0)
855 return create_err_response(NULL, error_code, "DLCX", p->trans);
856
Philipp Maier87bd9be2017-08-22 16:35:41 +0200857 if (llist_count(&endp->conns) <= 0) {
858 LOGP(DLMGCP, LOGL_ERROR,
859 "DLCX: endpoint:%x endpoint is not holding a connection.\n",
860 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200861 return create_err_response(endp, 400, "DLCX", p->trans);
862 }
863
864 for_each_line(line, p->save) {
865 if (!mgcp_check_param(endp, line))
866 continue;
867
868 switch (line[0]) {
869 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200870 if (mgcp_verify_call_id(endp, line + 3) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200871 goto error3;
872 break;
873 case 'I':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200874 ci = (const char *)line + 3;
875 if (mgcp_verify_ci(endp, ci) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200876 goto error3;
877 break;
878 case 'Z':
879 silent = strcmp("noanswer", line + 3) == 0;
880 break;
881 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200882 LOGP(DLMGCP, LOGL_NOTICE,
883 "DLCX: endpoint:%x Unhandled MGCP option: '%c'/%d\n",
884 ENDPOINT_NUMBER(endp), line[0], line[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200885 break;
886 }
887 }
888
889 /* policy CB */
890 if (p->cfg->policy_cb) {
891 int rc;
892 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200893 MGCP_ENDP_DLCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200894 switch (rc) {
895 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200896 LOGP(DLMGCP, LOGL_NOTICE,
897 "DLCX: endpoint:%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200898 ENDPOINT_NUMBER(endp));
899 if (silent)
900 goto out_silent;
901 return create_err_response(endp, 400, "DLCX", p->trans);
902 break;
903 case MGCP_POLICY_DEFER:
904 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200905 return NULL;
906 break;
907 case MGCP_POLICY_CONT:
908 /* just continue */
909 break;
910 }
911 }
912
Philipp Maierf4c0e372017-10-11 16:06:45 +0200913 /* When no connection id is supplied, we will interpret this as a
914 * wildcarded DLCX and drop all connections at once. (See also
915 * RFC3435 Section F.7) */
916 if (!ci) {
917 LOGP(DLMGCP, LOGL_NOTICE,
918 "DLCX: endpoint:%x missing ci (connectionIdentifier), will remove all connections at once\n",
919 ENDPOINT_NUMBER(endp));
920
921 mgcp_release_endp(endp);
922
923 /* Note: In this case we do not return any statistics,
924 * as we assume that the client is not interested in
925 * this case. */
926 return create_ok_response(endp, 200, "DLCX", p->trans);
927 }
928
929 /* Parse the connection id */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200930 if (mgcp_parse_ci(&conn_id, ci)) {
931 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maierf4c0e372017-10-11 16:06:45 +0200932 "DLCX: endpoint:%x insufficient parameters, invalid ci (connectionIdentifier)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200933 ENDPOINT_NUMBER(endp));
934 return create_err_response(endp, 400, "DLCX", p->trans);
935 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200936
Philipp Maierf4c0e372017-10-11 16:06:45 +0200937 /* Find the connection */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200938 conn = mgcp_conn_get_rtp(endp, conn_id);
939 if (!conn)
940 goto error3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200941
Philipp Maier87bd9be2017-08-22 16:35:41 +0200942 /* save the statistics of the current connection */
943 mgcp_format_stats(stats, sizeof(stats), conn->conn);
944
945 /* delete connection */
946 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:%x deleting conn:%s\n",
947 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
948 mgcp_conn_free(endp, conn_id);
949 LOGP(DLMGCP, LOGL_NOTICE,
950 "DLCX: endpoint:%x connection successfully deleted\n",
951 ENDPOINT_NUMBER(endp));
952
953 /* When all connections are closed, the endpoint will be released
954 * in order to be ready to be used by another call. */
955 if (llist_count(&endp->conns) <= 0) {
956 mgcp_release_endp(endp);
957 LOGP(DLMGCP, LOGL_DEBUG,
958 "DLCX: endpoint:%x endpoint released\n",
959 ENDPOINT_NUMBER(endp));
960 }
961
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200962 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200963 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
964 MGCP_ENDP_DLCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200965
966 if (silent)
967 goto out_silent;
968 return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats);
969
970error3:
971 return create_err_response(endp, error_code, "DLCX", p->trans);
972
973out_silent:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200974 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:%x silent exit\n",
975 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200976 return NULL;
977}
978
Philipp Maier87bd9be2017-08-22 16:35:41 +0200979/* RSIP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200980static struct msgb *handle_rsip(struct mgcp_parse_data *p)
981{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200982 /* TODO: Also implement the resetting of a specific endpoint
983 * to make mgcp_send_reset_ep() work. Currently this will call
984 * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1
985 * to make read_call_agent() reset all endpoints when called
986 * next time. In order to selectively reset endpoints some
987 * mechanism to distinguish which endpoint shall be resetted
988 * is needed */
989
990 LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n");
991
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200992 if (p->found != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200993 LOGP(DLMGCP, LOGL_ERROR,
994 "RSIP: failed to find the endpoint.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200995 return NULL;
996 }
997
998 if (p->cfg->reset_cb)
999 p->cfg->reset_cb(p->endp->tcfg);
1000 return NULL;
1001}
1002
1003static char extract_tone(const char *line)
1004{
1005 const char *str = strstr(line, "D/");
1006 if (!str)
1007 return CHAR_MAX;
1008
1009 return str[2];
1010}
1011
Philipp Maier87bd9be2017-08-22 16:35:41 +02001012/* This can request like DTMF detection and forward, fax detection... it
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001013 * can also request when the notification should be send and such. We don't
Philipp Maier87bd9be2017-08-22 16:35:41 +02001014 * do this right now. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001015static struct msgb *handle_noti_req(struct mgcp_parse_data *p)
1016{
1017 int res = 0;
1018 char *line;
1019 char tone = CHAR_MAX;
1020
Philipp Maier87bd9be2017-08-22 16:35:41 +02001021 LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n");
1022
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001023 if (p->found != 0)
1024 return create_err_response(NULL, 400, "RQNT", p->trans);
1025
1026 for_each_line(line, p->save) {
1027 switch (line[0]) {
1028 case 'S':
1029 tone = extract_tone(line);
1030 break;
1031 }
1032 }
1033
1034 /* we didn't see a signal request with a tone */
1035 if (tone == CHAR_MAX)
1036 return create_ok_response(p->endp, 200, "RQNT", p->trans);
1037
1038 if (p->cfg->rqnt_cb)
1039 res = p->cfg->rqnt_cb(p->endp, tone);
1040
1041 return res == 0 ?
Philipp Maier87bd9be2017-08-22 16:35:41 +02001042 create_ok_response(p->endp, 200, "RQNT", p->trans) :
1043 create_err_response(p->endp, res, "RQNT", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001044}
1045
Philipp Maier87bd9be2017-08-22 16:35:41 +02001046/* Connection keepalive timer, will take care that dummy packets are send
1047 * regulary, so that NAT connections stay open */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001048static void mgcp_keepalive_timer_cb(void *_tcfg)
1049{
1050 struct mgcp_trunk_config *tcfg = _tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001051 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001052 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001053
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001054 LOGP(DLMGCP, LOGL_DEBUG, "Triggered trunk %d keepalive timer.\n",
1055 tcfg->trunk_nr);
1056
1057 if (tcfg->keepalive_interval <= 0)
1058 return;
1059
Philipp Maier87bd9be2017-08-22 16:35:41 +02001060 /* Send walk over all endpoints and send out dummy packets through
1061 * every connection present on each endpoint */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001062 for (i = 1; i < tcfg->number_endpoints; ++i) {
1063 struct mgcp_endpoint *endp = &tcfg->endpoints[i];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001064 llist_for_each_entry(conn, &endp->conns, entry) {
1065 if (conn->mode == MGCP_CONN_RECV_ONLY)
1066 send_dummy(endp, &conn->u.rtp);
1067 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001068 }
1069
1070 LOGP(DLMGCP, LOGL_DEBUG, "Rescheduling trunk %d keepalive timer.\n",
1071 tcfg->trunk_nr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001072 osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval,
1073 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001074}
1075
1076void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval)
1077{
1078 tcfg->keepalive_interval = interval;
1079 osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg);
1080
1081 if (interval <= 0)
1082 osmo_timer_del(&tcfg->keepalive_timer);
1083 else
1084 osmo_timer_schedule(&tcfg->keepalive_timer,
1085 tcfg->keepalive_interval, 0);
1086}
1087
Philipp Maier87bd9be2017-08-22 16:35:41 +02001088/*! allocate configuration with default values.
1089 * (called once at startup by main function) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001090struct mgcp_config *mgcp_config_alloc(void)
1091{
1092 struct mgcp_config *cfg;
1093
1094 cfg = talloc_zero(NULL, struct mgcp_config);
1095 if (!cfg) {
1096 LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n");
1097 return NULL;
1098 }
1099
Philipp Maier87bd9be2017-08-22 16:35:41 +02001100 cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START;
1101 cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END;
1102 cfg->net_ports.last_port = cfg->net_ports.range_start;
1103
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001104 cfg->source_port = 2427;
1105 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
1106 cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
1107
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001108 cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
1109 cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
1110
1111 cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
1112
1113 /* default trunk handling */
1114 cfg->trunk.cfg = cfg;
1115 cfg->trunk.trunk_nr = 0;
1116 cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
1117 cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
1118 cfg->trunk.audio_payload = 126;
1119 cfg->trunk.audio_send_ptime = 1;
1120 cfg->trunk.audio_send_name = 1;
1121 cfg->trunk.omit_rtcp = 0;
1122 mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE);
1123
1124 INIT_LLIST_HEAD(&cfg->trunks);
1125
1126 return cfg;
1127}
1128
Philipp Maier87bd9be2017-08-22 16:35:41 +02001129/*! allocate configuration with default values.
1130 * (called once at startup by VTY)
1131 * \param[in] cfg mgcp configuration
1132 * \param[in] nr trunk number
1133 * \returns pointer to allocated trunk configuration */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001134struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
1135{
1136 struct mgcp_trunk_config *trunk;
1137
1138 trunk = talloc_zero(cfg, struct mgcp_trunk_config);
1139 if (!trunk) {
1140 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n");
1141 return NULL;
1142 }
1143
1144 trunk->cfg = cfg;
1145 trunk->trunk_type = MGCP_TRUNK_E1;
1146 trunk->trunk_nr = nr;
1147 trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
1148 trunk->audio_payload = 126;
1149 trunk->audio_send_ptime = 1;
1150 trunk->audio_send_name = 1;
1151 trunk->number_endpoints = 33;
1152 trunk->omit_rtcp = 0;
1153 mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
1154 llist_add_tail(&trunk->entry, &cfg->trunks);
1155 return trunk;
1156}
1157
Philipp Maier87bd9be2017-08-22 16:35:41 +02001158/*! get trunk configuration by trunk number (index).
1159 * \param[in] cfg mgcp configuration
1160 * \param[in] index trunk number
1161 * \returns pointer to trunk configuration, NULL on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001162struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
1163{
1164 struct mgcp_trunk_config *trunk;
1165
1166 llist_for_each_entry(trunk, &cfg->trunks, entry)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001167 if (trunk->trunk_nr == index)
1168 return trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001169
1170 return NULL;
1171}
1172
Philipp Maier87bd9be2017-08-22 16:35:41 +02001173/*! allocate endpoints and set default values.
1174 * (called once at startup by VTY)
1175 * \param[in] tcfg trunk configuration
1176 * \returns 0 on success, -1 on failure */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001177int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
1178{
1179 int i;
1180
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001181 tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001182 sizeof(struct mgcp_endpoint),
1183 tcfg->number_endpoints,
1184 "endpoints");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001185 if (!tcfg->endpoints)
1186 return -1;
1187
1188 for (i = 0; i < tcfg->number_endpoints; ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001189 INIT_LLIST_HEAD(&tcfg->endpoints[i].conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001190 tcfg->endpoints[i].cfg = tcfg->cfg;
1191 tcfg->endpoints[i].tcfg = tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001192
1193 /* NOTE: Currently all endpoints are of type RTP, this will
1194 * change when new variations are implemented */
1195 tcfg->endpoints[i].type = &ep_typeset.rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001196 }
1197
1198 return 0;
1199}
1200
Philipp Maier87bd9be2017-08-22 16:35:41 +02001201/*! relase endpoint, all open connections are closed.
1202 * \param[in] endp endpoint to release */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001203void mgcp_release_endp(struct mgcp_endpoint *endp)
1204{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001205 LOGP(DLMGCP, LOGL_DEBUG, "Releasing endpoint:%x\n",
1206 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001207
Philipp Maier87bd9be2017-08-22 16:35:41 +02001208 /* Normally this function should only be called wehen
1209 * all connections have been removed already. In case
1210 * that there are still connections open (e.g. when
1211 * RSIP is executed), free them all at once. */
1212 mgcp_conn_free_all(endp);
1213
1214 /* Reset endpoint parameters and states */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001215 talloc_free(endp->callid);
1216 endp->callid = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001217 talloc_free(endp->local_options.string);
1218 endp->local_options.string = NULL;
1219 talloc_free(endp->local_options.codec);
1220 endp->local_options.codec = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001221}
1222
1223static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1224{
1225 return write(cfg->gw_fd.bfd.fd, buf, len);
1226}
1227
Philipp Maier87bd9be2017-08-22 16:35:41 +02001228/*! Reset all endpoints by sending RSIP message to self.
1229 * (called by VTY)
1230 * \param[in] endp trunk endpoint
1231 * \param[in] endpoint number
1232 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001233int mgcp_send_reset_all(struct mgcp_config *cfg)
1234{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001235 int rc;
1236
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001237 static const char mgcp_reset[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001238 "RSIP 1 *@mgw MGCP 1.0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001239 };
1240
Philipp Maier87bd9be2017-08-22 16:35:41 +02001241 rc = send_agent(cfg, mgcp_reset, sizeof mgcp_reset - 1);
1242 if (rc <= 0)
1243 return -1;
1244
1245 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001246}
1247
Philipp Maier87bd9be2017-08-22 16:35:41 +02001248/*! Reset a single endpoint by sending RSIP message to self.
1249 * (called by VTY)
1250 * \param[in] endp trunk endpoint
1251 * \param[in] endpoint number
1252 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001253int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
1254{
1255 char buf[128];
1256 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001257 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001258
1259 len = snprintf(buf, sizeof(buf),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001260 "RSIP 39 %x@mgw MGCP 1.0\r\n", endpoint);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001261 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001262 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001263
1264 buf[sizeof(buf) - 1] = '\0';
1265
Philipp Maier87bd9be2017-08-22 16:35:41 +02001266 rc = send_agent(endp->cfg, buf, len);
1267 if (rc <= 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001268 return -1;
1269
Philipp Maier87bd9be2017-08-22 16:35:41 +02001270 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001271}