blob: 482679035a008afdf6874824e50bd4f461d3b495 [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 Maiere726d4f2017-11-01 10:41:34 +0100669 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
670 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200671 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
Philipp Maiere726d4f2017-11-01 10:41:34 +0100672 && tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200673 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200674
Philipp Maier87bd9be2017-08-22 16:35:41 +0200675 LOGP(DLMGCP, LOGL_NOTICE,
676 "CRCX: endpoint:%x connection successfully created\n",
677 ENDPOINT_NUMBER(endp));
678 return create_response_with_sdp(endp, conn, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200679error2:
680 mgcp_release_endp(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200681 LOGP(DLMGCP, LOGL_NOTICE,
682 "CRCX: endpoint:%x unable to create connection resource error\n",
683 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200684 return create_err_response(endp, error_code, "CRCX", p->trans);
685}
686
Philipp Maier87bd9be2017-08-22 16:35:41 +0200687/* MDCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200688static struct msgb *handle_modify_con(struct mgcp_parse_data *p)
689{
690 struct mgcp_endpoint *endp = p->endp;
691 int error_code = 500;
692 int silent = 0;
693 int have_sdp = 0;
694 char *line;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200695 const char *ci = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200696 const char *local_options = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200697 const char *mode = NULL;
698 struct mgcp_conn_rtp *conn = NULL;
699 uint32_t conn_id;
700
701 LOGP(DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200702
703 if (p->found != 0)
704 return create_err_response(NULL, 510, "MDCX", p->trans);
705
Philipp Maier87bd9be2017-08-22 16:35:41 +0200706 if (llist_count(&endp->conns) <= 0) {
707 LOGP(DLMGCP, LOGL_ERROR,
708 "MDCX: endpoint:%x endpoint is not holding a connection.\n",
709 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200710 return create_err_response(endp, 400, "MDCX", p->trans);
711 }
712
713 for_each_line(line, p->save) {
714 if (!mgcp_check_param(endp, line))
715 continue;
716
717 switch (line[0]) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200718 case 'C':
719 if (mgcp_verify_call_id(endp, line + 3) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200720 goto error3;
721 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200722 case 'I':
723 ci = (const char *)line + 3;
724 if (mgcp_verify_ci(endp, ci) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200725 goto error3;
726 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200727 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200728 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200729 break;
730 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200731 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200732 break;
733 case 'Z':
734 silent = strcmp("noanswer", line + 3) == 0;
735 break;
736 case '\0':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200737 have_sdp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200738 goto mgcp_header_done;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200739 break;
740 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200741 LOGP(DLMGCP, LOGL_NOTICE,
742 "MDCX: endpoint:%x Unhandled MGCP option: '%c'/%d\n",
743 ENDPOINT_NUMBER(endp), line[0], line[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200744 break;
745 }
746 }
747
Philipp Maier87bd9be2017-08-22 16:35:41 +0200748mgcp_header_done:
749 if (mgcp_parse_ci(&conn_id, ci)) {
750 LOGP(DLMGCP, LOGL_ERROR,
751 "MDCX: endpoint:%x insufficient parameters, missing ci (connectionIdentifier)\n",
752 ENDPOINT_NUMBER(endp));
753 return create_err_response(endp, 400, "MDCX", p->trans);
754 }
755
756 conn = mgcp_conn_get_rtp(endp, conn_id);
757 if (!conn)
758 return create_err_response(endp, 400, "MDCX", p->trans);
759
760 if (mode) {
761 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
762 error_code = 517;
763 goto error3;
764 }
765 } else
766 conn->conn->mode = conn->conn->mode_orig;
767
768 if (have_sdp)
Philipp Maier8970c492017-10-11 13:33:42 +0200769 mgcp_parse_sdp_data(endp, conn, p);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200770
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200771 set_local_cx_options(endp->tcfg->endpoints, &endp->local_options,
772 local_options);
773
774 if (!have_sdp && endp->local_options.codec)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200775 mgcp_set_audio_info(p->cfg, &conn->end.codec,
776 PTYPE_UNDEFINED, endp->local_options.codec);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200777
Philipp Maier87bd9be2017-08-22 16:35:41 +0200778 if (setup_rtp_processing(endp, conn) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200779 goto error3;
780
Philipp Maier87bd9be2017-08-22 16:35:41 +0200781
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200782 /* policy CB */
783 if (p->cfg->policy_cb) {
784 int rc;
785 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200786 MGCP_ENDP_MDCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200787 switch (rc) {
788 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200789 LOGP(DLMGCP, LOGL_NOTICE,
790 "MDCX: endpoint:%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200791 ENDPOINT_NUMBER(endp));
792 if (silent)
793 goto out_silent;
794 return create_err_response(endp, 400, "MDCX", p->trans);
795 break;
796 case MGCP_POLICY_DEFER:
797 /* stop processing */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200798 LOGP(DLMGCP, LOGL_DEBUG,
799 "MDCX: endpoint:%x defered by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200800 ENDPOINT_NUMBER(endp));
801 return NULL;
802 break;
803 case MGCP_POLICY_CONT:
804 /* just continue */
805 break;
806 }
807 }
808
Philipp Maier87bd9be2017-08-22 16:35:41 +0200809 mgcp_rtp_end_config(endp, 1, &conn->end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200810
811 /* modify */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200812 LOGP(DLMGCP, LOGL_DEBUG,
813 "MDCX: endpoint:%x modified conn:%s\n",
814 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200815 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200816 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
817 MGCP_ENDP_MDCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200818
Philipp Maiere726d4f2017-11-01 10:41:34 +0100819 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
820 OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
821 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
822 && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200823 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200824
825 if (silent)
826 goto out_silent;
827
Philipp Maier87bd9be2017-08-22 16:35:41 +0200828 LOGP(DLMGCP, LOGL_NOTICE,
829 "MDCX: endpoint:%x connection successfully modified\n",
830 ENDPOINT_NUMBER(endp));
831 return create_response_with_sdp(endp, conn, "MDCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200832error3:
833 return create_err_response(endp, error_code, "MDCX", p->trans);
834
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200835out_silent:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200836 LOGP(DLMGCP, LOGL_DEBUG, "MDCX: endpoint:%x silent exit\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200837 ENDPOINT_NUMBER(endp));
838 return NULL;
839}
840
Philipp Maier87bd9be2017-08-22 16:35:41 +0200841/* DLCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200842static struct msgb *handle_delete_con(struct mgcp_parse_data *p)
843{
844 struct mgcp_endpoint *endp = p->endp;
845 int error_code = 400;
846 int silent = 0;
847 char *line;
848 char stats[1048];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200849 const char *ci = NULL;
850 struct mgcp_conn_rtp *conn = NULL;
851 uint32_t conn_id;
852
853 LOGP(DLMGCP, LOGL_NOTICE,
854 "DLCX: endpoint:%x deleting connection ...\n",
855 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200856
857 if (p->found != 0)
858 return create_err_response(NULL, error_code, "DLCX", p->trans);
859
Philipp Maier87bd9be2017-08-22 16:35:41 +0200860 if (llist_count(&endp->conns) <= 0) {
861 LOGP(DLMGCP, LOGL_ERROR,
862 "DLCX: endpoint:%x endpoint is not holding a connection.\n",
863 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200864 return create_err_response(endp, 400, "DLCX", p->trans);
865 }
866
867 for_each_line(line, p->save) {
868 if (!mgcp_check_param(endp, line))
869 continue;
870
871 switch (line[0]) {
872 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200873 if (mgcp_verify_call_id(endp, line + 3) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200874 goto error3;
875 break;
876 case 'I':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200877 ci = (const char *)line + 3;
878 if (mgcp_verify_ci(endp, ci) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200879 goto error3;
880 break;
881 case 'Z':
882 silent = strcmp("noanswer", line + 3) == 0;
883 break;
884 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200885 LOGP(DLMGCP, LOGL_NOTICE,
886 "DLCX: endpoint:%x Unhandled MGCP option: '%c'/%d\n",
887 ENDPOINT_NUMBER(endp), line[0], line[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200888 break;
889 }
890 }
891
892 /* policy CB */
893 if (p->cfg->policy_cb) {
894 int rc;
895 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200896 MGCP_ENDP_DLCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200897 switch (rc) {
898 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200899 LOGP(DLMGCP, LOGL_NOTICE,
900 "DLCX: endpoint:%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200901 ENDPOINT_NUMBER(endp));
902 if (silent)
903 goto out_silent;
904 return create_err_response(endp, 400, "DLCX", p->trans);
905 break;
906 case MGCP_POLICY_DEFER:
907 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200908 return NULL;
909 break;
910 case MGCP_POLICY_CONT:
911 /* just continue */
912 break;
913 }
914 }
915
Philipp Maierf4c0e372017-10-11 16:06:45 +0200916 /* When no connection id is supplied, we will interpret this as a
917 * wildcarded DLCX and drop all connections at once. (See also
918 * RFC3435 Section F.7) */
919 if (!ci) {
920 LOGP(DLMGCP, LOGL_NOTICE,
921 "DLCX: endpoint:%x missing ci (connectionIdentifier), will remove all connections at once\n",
922 ENDPOINT_NUMBER(endp));
923
924 mgcp_release_endp(endp);
925
926 /* Note: In this case we do not return any statistics,
927 * as we assume that the client is not interested in
928 * this case. */
929 return create_ok_response(endp, 200, "DLCX", p->trans);
930 }
931
932 /* Parse the connection id */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200933 if (mgcp_parse_ci(&conn_id, ci)) {
934 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maierf4c0e372017-10-11 16:06:45 +0200935 "DLCX: endpoint:%x insufficient parameters, invalid ci (connectionIdentifier)\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200936 ENDPOINT_NUMBER(endp));
937 return create_err_response(endp, 400, "DLCX", p->trans);
938 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200939
Philipp Maierf4c0e372017-10-11 16:06:45 +0200940 /* Find the connection */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200941 conn = mgcp_conn_get_rtp(endp, conn_id);
942 if (!conn)
943 goto error3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200944
Philipp Maier87bd9be2017-08-22 16:35:41 +0200945 /* save the statistics of the current connection */
946 mgcp_format_stats(stats, sizeof(stats), conn->conn);
947
948 /* delete connection */
949 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:%x deleting conn:%s\n",
950 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
951 mgcp_conn_free(endp, conn_id);
952 LOGP(DLMGCP, LOGL_NOTICE,
953 "DLCX: endpoint:%x connection successfully deleted\n",
954 ENDPOINT_NUMBER(endp));
955
956 /* When all connections are closed, the endpoint will be released
957 * in order to be ready to be used by another call. */
958 if (llist_count(&endp->conns) <= 0) {
959 mgcp_release_endp(endp);
960 LOGP(DLMGCP, LOGL_DEBUG,
961 "DLCX: endpoint:%x endpoint released\n",
962 ENDPOINT_NUMBER(endp));
963 }
964
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200965 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200966 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
967 MGCP_ENDP_DLCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200968
969 if (silent)
970 goto out_silent;
971 return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats);
972
973error3:
974 return create_err_response(endp, error_code, "DLCX", p->trans);
975
976out_silent:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200977 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:%x silent exit\n",
978 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200979 return NULL;
980}
981
Philipp Maier87bd9be2017-08-22 16:35:41 +0200982/* RSIP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200983static struct msgb *handle_rsip(struct mgcp_parse_data *p)
984{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200985 /* TODO: Also implement the resetting of a specific endpoint
986 * to make mgcp_send_reset_ep() work. Currently this will call
987 * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1
988 * to make read_call_agent() reset all endpoints when called
989 * next time. In order to selectively reset endpoints some
990 * mechanism to distinguish which endpoint shall be resetted
991 * is needed */
992
993 LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n");
994
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200995 if (p->found != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200996 LOGP(DLMGCP, LOGL_ERROR,
997 "RSIP: failed to find the endpoint.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200998 return NULL;
999 }
1000
1001 if (p->cfg->reset_cb)
1002 p->cfg->reset_cb(p->endp->tcfg);
1003 return NULL;
1004}
1005
1006static char extract_tone(const char *line)
1007{
1008 const char *str = strstr(line, "D/");
1009 if (!str)
1010 return CHAR_MAX;
1011
1012 return str[2];
1013}
1014
Philipp Maier87bd9be2017-08-22 16:35:41 +02001015/* This can request like DTMF detection and forward, fax detection... it
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001016 * can also request when the notification should be send and such. We don't
Philipp Maier87bd9be2017-08-22 16:35:41 +02001017 * do this right now. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001018static struct msgb *handle_noti_req(struct mgcp_parse_data *p)
1019{
1020 int res = 0;
1021 char *line;
1022 char tone = CHAR_MAX;
1023
Philipp Maier87bd9be2017-08-22 16:35:41 +02001024 LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n");
1025
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001026 if (p->found != 0)
1027 return create_err_response(NULL, 400, "RQNT", p->trans);
1028
1029 for_each_line(line, p->save) {
1030 switch (line[0]) {
1031 case 'S':
1032 tone = extract_tone(line);
1033 break;
1034 }
1035 }
1036
1037 /* we didn't see a signal request with a tone */
1038 if (tone == CHAR_MAX)
1039 return create_ok_response(p->endp, 200, "RQNT", p->trans);
1040
1041 if (p->cfg->rqnt_cb)
1042 res = p->cfg->rqnt_cb(p->endp, tone);
1043
1044 return res == 0 ?
Philipp Maier87bd9be2017-08-22 16:35:41 +02001045 create_ok_response(p->endp, 200, "RQNT", p->trans) :
1046 create_err_response(p->endp, res, "RQNT", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001047}
1048
Philipp Maier87bd9be2017-08-22 16:35:41 +02001049/* Connection keepalive timer, will take care that dummy packets are send
1050 * regulary, so that NAT connections stay open */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001051static void mgcp_keepalive_timer_cb(void *_tcfg)
1052{
1053 struct mgcp_trunk_config *tcfg = _tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001054 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001055 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001056
Philipp Maiere726d4f2017-11-01 10:41:34 +01001057 LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001058 tcfg->trunk_nr);
1059
Philipp Maiere726d4f2017-11-01 10:41:34 +01001060 /* Do not accept invalid configuration values
1061 * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and
1062 * values greater 0 */
1063 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1064
1065 /* The dummy packet functionality has been disabled, we will exit
1066 * immediately, no further timer is scheduled, which means we will no
1067 * longer send dummy packets even when we did before */
1068 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER)
1069 return;
1070
1071 /* In cases where only one dummy packet is sent, we do not need
1072 * the timer since the functions that handle the CRCX and MDCX are
1073 * triggering the sending of the dummy packet. So we behave like in
1074 * the MGCP_KEEPALIVE_NEVER case */
1075 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001076 return;
1077
Philipp Maier87bd9be2017-08-22 16:35:41 +02001078 /* Send walk over all endpoints and send out dummy packets through
1079 * every connection present on each endpoint */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001080 for (i = 1; i < tcfg->number_endpoints; ++i) {
1081 struct mgcp_endpoint *endp = &tcfg->endpoints[i];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001082 llist_for_each_entry(conn, &endp->conns, entry) {
1083 if (conn->mode == MGCP_CONN_RECV_ONLY)
1084 send_dummy(endp, &conn->u.rtp);
1085 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001086 }
1087
Philipp Maiere726d4f2017-11-01 10:41:34 +01001088 /* Schedule the keepalive timer for the next round */
1089 LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001090 tcfg->trunk_nr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001091 osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval,
1092 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001093}
1094
1095void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval)
1096{
1097 tcfg->keepalive_interval = interval;
1098 osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg);
1099
1100 if (interval <= 0)
1101 osmo_timer_del(&tcfg->keepalive_timer);
1102 else
1103 osmo_timer_schedule(&tcfg->keepalive_timer,
1104 tcfg->keepalive_interval, 0);
1105}
1106
Philipp Maier87bd9be2017-08-22 16:35:41 +02001107/*! allocate configuration with default values.
1108 * (called once at startup by main function) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001109struct mgcp_config *mgcp_config_alloc(void)
1110{
1111 struct mgcp_config *cfg;
1112
1113 cfg = talloc_zero(NULL, struct mgcp_config);
1114 if (!cfg) {
1115 LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n");
1116 return NULL;
1117 }
1118
Philipp Maier87bd9be2017-08-22 16:35:41 +02001119 cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START;
1120 cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END;
1121 cfg->net_ports.last_port = cfg->net_ports.range_start;
1122
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001123 cfg->source_port = 2427;
1124 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
1125 cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
1126
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001127 cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
1128 cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
1129
1130 cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
1131
1132 /* default trunk handling */
1133 cfg->trunk.cfg = cfg;
1134 cfg->trunk.trunk_nr = 0;
1135 cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
1136 cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
1137 cfg->trunk.audio_payload = 126;
1138 cfg->trunk.audio_send_ptime = 1;
1139 cfg->trunk.audio_send_name = 1;
1140 cfg->trunk.omit_rtcp = 0;
1141 mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE);
1142
1143 INIT_LLIST_HEAD(&cfg->trunks);
1144
1145 return cfg;
1146}
1147
Philipp Maier87bd9be2017-08-22 16:35:41 +02001148/*! allocate configuration with default values.
1149 * (called once at startup by VTY)
1150 * \param[in] cfg mgcp configuration
1151 * \param[in] nr trunk number
1152 * \returns pointer to allocated trunk configuration */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001153struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
1154{
1155 struct mgcp_trunk_config *trunk;
1156
1157 trunk = talloc_zero(cfg, struct mgcp_trunk_config);
1158 if (!trunk) {
1159 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n");
1160 return NULL;
1161 }
1162
1163 trunk->cfg = cfg;
1164 trunk->trunk_type = MGCP_TRUNK_E1;
1165 trunk->trunk_nr = nr;
1166 trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
1167 trunk->audio_payload = 126;
1168 trunk->audio_send_ptime = 1;
1169 trunk->audio_send_name = 1;
1170 trunk->number_endpoints = 33;
1171 trunk->omit_rtcp = 0;
1172 mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
1173 llist_add_tail(&trunk->entry, &cfg->trunks);
1174 return trunk;
1175}
1176
Philipp Maier87bd9be2017-08-22 16:35:41 +02001177/*! get trunk configuration by trunk number (index).
1178 * \param[in] cfg mgcp configuration
1179 * \param[in] index trunk number
1180 * \returns pointer to trunk configuration, NULL on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001181struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
1182{
1183 struct mgcp_trunk_config *trunk;
1184
1185 llist_for_each_entry(trunk, &cfg->trunks, entry)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001186 if (trunk->trunk_nr == index)
1187 return trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001188
1189 return NULL;
1190}
1191
Philipp Maier87bd9be2017-08-22 16:35:41 +02001192/*! allocate endpoints and set default values.
1193 * (called once at startup by VTY)
1194 * \param[in] tcfg trunk configuration
1195 * \returns 0 on success, -1 on failure */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001196int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
1197{
1198 int i;
1199
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001200 tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001201 sizeof(struct mgcp_endpoint),
1202 tcfg->number_endpoints,
1203 "endpoints");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001204 if (!tcfg->endpoints)
1205 return -1;
1206
1207 for (i = 0; i < tcfg->number_endpoints; ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001208 INIT_LLIST_HEAD(&tcfg->endpoints[i].conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001209 tcfg->endpoints[i].cfg = tcfg->cfg;
1210 tcfg->endpoints[i].tcfg = tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001211
1212 /* NOTE: Currently all endpoints are of type RTP, this will
1213 * change when new variations are implemented */
1214 tcfg->endpoints[i].type = &ep_typeset.rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001215 }
1216
1217 return 0;
1218}
1219
Philipp Maier87bd9be2017-08-22 16:35:41 +02001220/*! relase endpoint, all open connections are closed.
1221 * \param[in] endp endpoint to release */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001222void mgcp_release_endp(struct mgcp_endpoint *endp)
1223{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001224 LOGP(DLMGCP, LOGL_DEBUG, "Releasing endpoint:%x\n",
1225 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001226
Philipp Maier87bd9be2017-08-22 16:35:41 +02001227 /* Normally this function should only be called wehen
1228 * all connections have been removed already. In case
1229 * that there are still connections open (e.g. when
1230 * RSIP is executed), free them all at once. */
1231 mgcp_conn_free_all(endp);
1232
1233 /* Reset endpoint parameters and states */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001234 talloc_free(endp->callid);
1235 endp->callid = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001236 talloc_free(endp->local_options.string);
1237 endp->local_options.string = NULL;
1238 talloc_free(endp->local_options.codec);
1239 endp->local_options.codec = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001240}
1241
1242static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1243{
1244 return write(cfg->gw_fd.bfd.fd, buf, len);
1245}
1246
Philipp Maier87bd9be2017-08-22 16:35:41 +02001247/*! Reset all endpoints by sending RSIP message to self.
1248 * (called by VTY)
1249 * \param[in] endp trunk endpoint
1250 * \param[in] endpoint number
1251 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001252int mgcp_send_reset_all(struct mgcp_config *cfg)
1253{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001254 int rc;
1255
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001256 static const char mgcp_reset[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001257 "RSIP 1 *@mgw MGCP 1.0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001258 };
1259
Philipp Maier87bd9be2017-08-22 16:35:41 +02001260 rc = send_agent(cfg, mgcp_reset, sizeof mgcp_reset - 1);
1261 if (rc <= 0)
1262 return -1;
1263
1264 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001265}
1266
Philipp Maier87bd9be2017-08-22 16:35:41 +02001267/*! Reset a single endpoint by sending RSIP message to self.
1268 * (called by VTY)
1269 * \param[in] endp trunk endpoint
1270 * \param[in] endpoint number
1271 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001272int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
1273{
1274 char buf[128];
1275 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001276 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001277
1278 len = snprintf(buf, sizeof(buf),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001279 "RSIP 39 %x@mgw MGCP 1.0\r\n", endpoint);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001280 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001281 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001282
1283 buf[sizeof(buf) - 1] = '\0';
1284
Philipp Maier87bd9be2017-08-22 16:35:41 +02001285 rc = send_agent(endp->cfg, buf, len);
1286 if (rc <= 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001287 return -1;
1288
Philipp Maier87bd9be2017-08-22 16:35:41 +02001289 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001290}