blob: feca8da6088b3852bf28185d48293a3229508f8c [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];
Philipp Maier1cb1e382017-11-02 17:16:04 +0100206 char local_ip_addr[INET_ADDRSTRLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200207
Philipp Maier8970c492017-10-11 13:33:42 +0200208 sdp = msgb_alloc_headroom(4096, 128, "sdp record");
209 if (!sdp)
210 return NULL;
211
Philipp Maier1cb1e382017-11-02 17:16:04 +0100212 if (!addr) {
213 mgcp_get_local_addr(local_ip_addr, conn);
214 addr = local_ip_addr;
215 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200216
Philipp Maier87bd9be2017-08-22 16:35:41 +0200217 if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) {
218 sprintf(osmux_extension, "\nX-Osmux: %u", conn->osmux.cid);
219 conn->osmux.state = OSMUX_STATE_ACTIVATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200220 } else {
221 osmux_extension[0] = '\0';
222 }
223
Philipp Maier01d24a32017-11-21 17:26:09 +0100224 rc = msgb_printf(sdp, "I: %s%s\n\n", conn->conn->id, osmux_extension);
Philipp Maier8970c492017-10-11 13:33:42 +0200225 if (rc < 0)
226 goto error;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200227
Philipp Maier8970c492017-10-11 13:33:42 +0200228 rc = mgcp_write_response_sdp(endp, conn, sdp, addr);
229 if (rc < 0)
230 goto error;
231 result = create_resp(endp, 200, " OK", msg, trans_id, NULL, (char*) sdp->data);
232 msgb_free(sdp);
233 return result;
234error:
235 msgb_free(sdp);
236 return NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200237}
238
Philipp Maier87bd9be2017-08-22 16:35:41 +0200239/* Send out dummy packet to keep the connection open, if the connection is an
240 * osmux connection, send the dummy packet via OSMUX */
241static void send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200242{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200243 if (conn->osmux.state != OSMUX_STATE_DISABLED)
244 osmux_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200245 else
Philipp Maier87bd9be2017-08-22 16:35:41 +0200246 mgcp_send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200247}
248
Philipp Maier87bd9be2017-08-22 16:35:41 +0200249/* handle incoming messages:
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200250 * - this can be a command (four letters, space, transaction id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200251 * - or a response (three numbers, space, transaction id) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200252struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
253{
254 struct mgcp_parse_data pdata;
255 int i, code, handled = 0;
256 struct msgb *resp = NULL;
257 char *data;
258
259 if (msgb_l2len(msg) < 4) {
260 LOGP(DLMGCP, LOGL_ERROR, "msg too short: %d\n", msg->len);
261 return NULL;
262 }
263
264 if (mgcp_msg_terminate_nul(msg))
265 return NULL;
266
Philipp Maier87bd9be2017-08-22 16:35:41 +0200267 mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Received message");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200268
Philipp Maier87bd9be2017-08-22 16:35:41 +0200269 /* attempt to treat it as a response */
270 if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200271 LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code);
272 return NULL;
273 }
274
275 msg->l3h = &msg->l2h[4];
276
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200277 /*
278 * Check for a duplicate message and respond.
279 */
280 memset(&pdata, 0, sizeof(pdata));
281 pdata.cfg = cfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200282 data = mgcp_strline((char *)msg->l3h, &pdata.save);
283 pdata.found = mgcp_parse_header(&pdata, data);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200284 if (pdata.endp && pdata.trans
Philipp Maier87bd9be2017-08-22 16:35:41 +0200285 && pdata.endp->last_trans
286 && strcmp(pdata.endp->last_trans, pdata.trans) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200287 return do_retransmission(pdata.endp);
288 }
289
290 for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200291 if (strncmp
292 (mgcp_requests[i].name, (const char *)&msg->l2h[0],
293 4) == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200294 handled = 1;
295 resp = mgcp_requests[i].handle_request(&pdata);
296 break;
297 }
298 }
299
300 if (!handled)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200301 LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n",
302 &msg->l2h[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200303
304 return resp;
305}
306
Philipp Maier87bd9be2017-08-22 16:35:41 +0200307/* AUEP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200308static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p)
309{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200310 LOGP(DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n");
311
312 if (p->found != 0) {
313 LOGP(DLMGCP, LOGL_ERROR,
314 "AUEP: failed to find the endpoint.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200315 return create_err_response(NULL, 500, "AUEP", p->trans);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200316 } else
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200317 return create_ok_response(p->endp, 200, "AUEP", p->trans);
318}
319
Philipp Maier87bd9be2017-08-22 16:35:41 +0200320/* Try to find a free port by attemting to bind on it. Also handle the
321 * counter that points on the next free port. Since we have a pointer
322 * to the next free port, binding should work on the first attemt,
323 * neverless, try at least the next 200 ports before giving up */
324static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200325{
326 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200327 struct mgcp_rtp_end *end;
328 struct mgcp_port_range *range;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200329
Philipp Maier87bd9be2017-08-22 16:35:41 +0200330 OSMO_ASSERT(conn);
331 end = &conn->end;
332 OSMO_ASSERT(end);
333
334 range = &endp->cfg->net_ports;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200335
336 /* attempt to find a port */
337 for (i = 0; i < 200; ++i) {
338 int rc;
339
340 if (range->last_port >= range->range_end)
341 range->last_port = range->range_start;
342
Philipp Maier87bd9be2017-08-22 16:35:41 +0200343 rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200344
345 range->last_port += 2;
346 if (rc == 0) {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200347 return 0;
348 }
349
350 }
351
Philipp Maier87bd9be2017-08-22 16:35:41 +0200352 LOGP(DLMGCP, LOGL_ERROR,
353 "Allocating a RTP/RTCP port failed 200 times 0x%x.\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200354 ENDPOINT_NUMBER(endp));
355 return -1;
356}
357
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200358/* Set the LCO from a string (see RFC 3435).
Philipp Maier87bd9be2017-08-22 16:35:41 +0200359 * The string is stored in the 'string' field. A NULL string is handled excatlyy
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200360 * like an empty string, the 'string' field is never NULL after this function
361 * has been called. */
362static void set_local_cx_options(void *ctx, struct mgcp_lco *lco,
363 const char *options)
364{
365 char *p_opt, *a_opt;
366 char codec[9];
367
368 talloc_free(lco->string);
369 talloc_free(lco->codec);
370 lco->codec = NULL;
371 lco->pkt_period_min = lco->pkt_period_max = 0;
372 lco->string = talloc_strdup(ctx, options ? options : "");
373
374 p_opt = strstr(lco->string, "p:");
375 if (p_opt && sscanf(p_opt, "p:%d-%d",
376 &lco->pkt_period_min, &lco->pkt_period_max) == 1)
377 lco->pkt_period_max = lco->pkt_period_min;
378
379 a_opt = strstr(lco->string, "a:");
380 if (a_opt && sscanf(a_opt, "a:%8[^,]", codec) == 1)
381 lco->codec = talloc_strdup(ctx, codec);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200382
383 LOGP(DLMGCP, LOGL_DEBUG,
384 "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n",
385 lco->pkt_period_max, lco->codec);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200386}
387
388void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
389 struct mgcp_rtp_end *rtp)
390{
391 struct mgcp_trunk_config *tcfg = endp->tcfg;
392
393 int patch_ssrc = expect_ssrc_change && tcfg->force_constant_ssrc;
394
395 rtp->force_aligned_timing = tcfg->force_aligned_timing;
396 rtp->force_constant_ssrc = patch_ssrc ? 1 : 0;
397
398 LOGP(DLMGCP, LOGL_DEBUG,
399 "Configuring RTP endpoint: local port %d%s%s\n",
400 ntohs(rtp->rtp_port),
401 rtp->force_aligned_timing ? ", force constant timing" : "",
402 rtp->force_constant_ssrc ? ", force constant ssrc" : "");
403}
404
405uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
406 struct mgcp_rtp_end *rtp)
407{
408 int f = 0;
409
410 /* Get the number of frames per channel and packet */
411 if (rtp->frames_per_packet)
412 f = rtp->frames_per_packet;
413 else if (rtp->packet_duration_ms && rtp->codec.frame_duration_num) {
414 int den = 1000 * rtp->codec.frame_duration_num;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200415 f = (rtp->packet_duration_ms * rtp->codec.frame_duration_den +
416 den / 2)
417 / den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200418 }
419
Philipp Maier87bd9be2017-08-22 16:35:41 +0200420 return rtp->codec.rate * f * rtp->codec.frame_duration_num /
421 rtp->codec.frame_duration_den;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200422}
423
424static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line)
425{
426 if (!endp->cfg->osmux_init) {
427 if (osmux_init(OSMUX_ROLE_BSC, endp->cfg) < 0) {
428 LOGP(DLMGCP, LOGL_ERROR, "Cannot init OSMUX\n");
429 return -1;
430 }
431 LOGP(DLMGCP, LOGL_NOTICE, "OSMUX socket has been set up\n");
432 }
433
434 return mgcp_parse_osmux_cid(line);
435}
436
Philipp Maier87bd9be2017-08-22 16:35:41 +0200437/* CRCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200438static struct msgb *handle_create_con(struct mgcp_parse_data *p)
439{
440 struct mgcp_trunk_config *tcfg;
441 struct mgcp_endpoint *endp = p->endp;
442 int error_code = 400;
443
444 const char *local_options = NULL;
445 const char *callid = NULL;
446 const char *mode = NULL;
447 char *line;
448 int have_sdp = 0, osmux_cid = -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200449 struct mgcp_conn_rtp *conn = NULL;
Philipp Maierffd75e42017-11-22 11:44:50 +0100450 struct mgcp_conn *_conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200451 char conn_name[512];
452
453 LOGP(DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200454
455 if (p->found != 0)
456 return create_err_response(NULL, 510, "CRCX", p->trans);
457
458 /* parse CallID C: and LocalParameters L: */
459 for_each_line(line, p->save) {
460 if (!mgcp_check_param(endp, line))
461 continue;
462
463 switch (line[0]) {
464 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200465 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200466 break;
467 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200468 callid = (const char *)line + 3;
469 break;
470 case 'I':
Philipp Maierffd75e42017-11-22 11:44:50 +0100471 /* It is illegal to send a connection identifier
472 * together with a CRCX, the MGW will assign the
473 * connection identifier by itself on CRCX */
474 return create_err_response(NULL, 523, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200475 break;
476 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200477 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200478 break;
479 case 'X':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200480 /* If osmoux is disabled, just skip setting it up */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200481 if (!p->endp->cfg->osmux)
482 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200483 if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) ==
484 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200485 osmux_cid = mgcp_osmux_setup(endp, line);
486 break;
487 case '\0':
488 have_sdp = 1;
489 goto mgcp_header_done;
490 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200491 LOGP(DLMGCP, LOGL_NOTICE,
492 "CRCX: endpoint:%x unhandled option: '%c'/%d\n",
493 ENDPOINT_NUMBER(endp), *line, *line);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200494 break;
495 }
496 }
497
498mgcp_header_done:
499 tcfg = p->endp->tcfg;
500
Philipp Maier87bd9be2017-08-22 16:35:41 +0200501 /* Check parameters */
502 if (!callid) {
503 LOGP(DLMGCP, LOGL_ERROR,
504 "CRCX: endpoint:%x insufficient parameters, missing callid\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200505 ENDPOINT_NUMBER(endp));
506 return create_err_response(endp, 400, "CRCX", p->trans);
507 }
508
Philipp Maier87bd9be2017-08-22 16:35:41 +0200509 if (!mode) {
510 LOGP(DLMGCP, LOGL_ERROR,
511 "CRCX: endpoint:%x insufficient parameters, missing mode\n",
512 ENDPOINT_NUMBER(endp));
513 return create_err_response(endp, 400, "CRCX", p->trans);
514 }
515
Philipp Maier87bd9be2017-08-22 16:35:41 +0200516 /* Check if we are able to accept the creation of another connection */
517 if (llist_count(&endp->conns) >= endp->type->max_conns) {
518 LOGP(DLMGCP, LOGL_ERROR,
519 "CRCX: endpoint:%x endpoint full, max. %i connections allowed!\n",
520 endp->type->max_conns, ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200521 if (tcfg->force_realloc) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200522 /* There is no more room for a connection, make some
523 * room by blindly tossing the oldest of the two two
524 * connections */
525 mgcp_conn_free_oldest(endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200526 } else {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200527 /* There is no more room for a connection, leave
528 * everything as it is and return with an error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200529 return create_err_response(endp, 400, "CRCX", p->trans);
530 }
531 }
532
Philipp Maier87bd9be2017-08-22 16:35:41 +0200533 /* Check if this endpoint already serves a call, if so, check if the
534 * callids match up so that we are sure that this is our call */
535 if (endp->callid && mgcp_verify_call_id(endp, callid)) {
536 LOGP(DLMGCP, LOGL_ERROR,
537 "CRCX: endpoint:%x allready seized by other call (%s)\n",
538 ENDPOINT_NUMBER(endp), endp->callid);
539 if (tcfg->force_realloc)
540 /* This is not our call, toss everything by releasing
541 * the entire endpoint. (rude!) */
542 mgcp_release_endp(endp);
543 else {
544 /* This is not our call, leave everything as it is and
545 * return with an error. */
546 return create_err_response(endp, 400, "CRCX", p->trans);
547 }
548 }
549
550 /* Set the callid, creation of another connection will only be possible
551 * when the callid matches up. (Connections are distinuished by their
552 * connection ids) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200553 endp->callid = talloc_strdup(tcfg->endpoints, callid);
554
Philipp Maier87bd9be2017-08-22 16:35:41 +0200555 /* Extract audio codec information */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200556 set_local_cx_options(endp->tcfg->endpoints, &endp->local_options,
557 local_options);
558
Philipp Maierffd75e42017-11-22 11:44:50 +0100559 snprintf(conn_name, sizeof(conn_name), "%s", callid);
560 _conn = mgcp_conn_alloc(NULL, endp, MGCP_CONN_TYPE_RTP, conn_name);
561 if (!_conn) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200562 LOGP(DLMGCP, LOGL_ERROR,
563 "CRCX: endpoint:%x unable to allocate RTP connection\n",
564 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200565 goto error2;
566
Philipp Maier87bd9be2017-08-22 16:35:41 +0200567 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100568 conn = mgcp_conn_get_rtp(endp, _conn->id);
569 OSMO_ASSERT(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200570
571 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
572 error_code = 517;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200573 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200574 }
575
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200576 /* Annotate Osmux circuit ID and set it to negotiating state until this
Philipp Maier87bd9be2017-08-22 16:35:41 +0200577 * is fully set up from the dummy load. */
578 conn->osmux.state = OSMUX_STATE_DISABLED;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200579 if (osmux_cid >= 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200580 conn->osmux.cid = osmux_cid;
581 conn->osmux.state = OSMUX_STATE_NEGOTIATING;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200582 } else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) {
583 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200584 "CRCX: endpoint:%x osmux only and no osmux offered\n",
585 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200586 goto error2;
587 }
588
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200589 /* set up RTP media parameters */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200590 if (have_sdp)
Philipp Maier8970c492017-10-11 13:33:42 +0200591 mgcp_parse_sdp_data(endp, conn, p);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200592 else if (endp->local_options.codec)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200593 mgcp_set_audio_info(p->cfg, &conn->end.codec,
594 PTYPE_UNDEFINED, endp->local_options.codec);
595 conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints,
596 tcfg->audio_fmtp_extra);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200597
Philipp Maier87bd9be2017-08-22 16:35:41 +0200598 if (p->cfg->force_ptime) {
599 conn->end.packet_duration_ms = p->cfg->force_ptime;
600 conn->end.force_output_ptime = 1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200601 }
602
Philipp Maier1cb1e382017-11-02 17:16:04 +0100603 mgcp_rtp_end_config(endp, 0, &conn->end);
604
605 if (allocate_port(endp, conn) != 0) {
606 goto error2;
607 }
608
Philipp Maier87bd9be2017-08-22 16:35:41 +0200609 if (setup_rtp_processing(endp, conn) != 0) {
610 LOGP(DLMGCP, LOGL_ERROR,
611 "CRCX: endpoint:%x could not start RTP processing!\n",
612 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200613 goto error2;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200614 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200615
616 /* policy CB */
617 if (p->cfg->policy_cb) {
618 int rc;
619 rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200620 MGCP_ENDP_CRCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200621 switch (rc) {
622 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200623 LOGP(DLMGCP, LOGL_NOTICE,
624 "CRCX: endpoint:%x CRCX rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200625 ENDPOINT_NUMBER(endp));
626 mgcp_release_endp(endp);
627 return create_err_response(endp, 400, "CRCX", p->trans);
628 break;
629 case MGCP_POLICY_DEFER:
630 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200631 return NULL;
632 break;
633 case MGCP_POLICY_CONT:
634 /* just continue */
635 break;
636 }
637 }
638
Philipp Maier87bd9be2017-08-22 16:35:41 +0200639 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier01d24a32017-11-21 17:26:09 +0100640 "CRCX: endpoint:%x Creating connection: CI: %s port: %u\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200641 ENDPOINT_NUMBER(endp), conn->conn->id, conn->end.local_port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200642 if (p->cfg->change_cb)
643 p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX);
644
Philipp Maiere726d4f2017-11-01 10:41:34 +0100645 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
646 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200647 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
Philipp Maiere726d4f2017-11-01 10:41:34 +0100648 && tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200649 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200650
Philipp Maier87bd9be2017-08-22 16:35:41 +0200651 LOGP(DLMGCP, LOGL_NOTICE,
652 "CRCX: endpoint:%x connection successfully created\n",
653 ENDPOINT_NUMBER(endp));
654 return create_response_with_sdp(endp, conn, "CRCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200655error2:
656 mgcp_release_endp(endp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200657 LOGP(DLMGCP, LOGL_NOTICE,
658 "CRCX: endpoint:%x unable to create connection resource error\n",
659 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200660 return create_err_response(endp, error_code, "CRCX", p->trans);
661}
662
Philipp Maier87bd9be2017-08-22 16:35:41 +0200663/* MDCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200664static struct msgb *handle_modify_con(struct mgcp_parse_data *p)
665{
666 struct mgcp_endpoint *endp = p->endp;
667 int error_code = 500;
668 int silent = 0;
669 int have_sdp = 0;
670 char *line;
671 const char *local_options = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200672 const char *mode = NULL;
673 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier01d24a32017-11-21 17:26:09 +0100674 const char *conn_id = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200675
676 LOGP(DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200677
678 if (p->found != 0)
679 return create_err_response(NULL, 510, "MDCX", p->trans);
680
Philipp Maier87bd9be2017-08-22 16:35:41 +0200681 if (llist_count(&endp->conns) <= 0) {
682 LOGP(DLMGCP, LOGL_ERROR,
683 "MDCX: endpoint:%x endpoint is not holding a connection.\n",
684 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200685 return create_err_response(endp, 400, "MDCX", p->trans);
686 }
687
688 for_each_line(line, p->save) {
689 if (!mgcp_check_param(endp, line))
690 continue;
691
692 switch (line[0]) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200693 case 'C':
694 if (mgcp_verify_call_id(endp, line + 3) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200695 goto error3;
696 break;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200697 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +0100698 conn_id = (const char *)line + 3;
699 if (mgcp_verify_ci(endp, conn_id) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200700 goto error3;
701 break;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200702 case 'L':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200703 local_options = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200704 break;
705 case 'M':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200706 mode = (const char *)line + 3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200707 break;
708 case 'Z':
709 silent = strcmp("noanswer", line + 3) == 0;
710 break;
711 case '\0':
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200712 have_sdp = 1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200713 goto mgcp_header_done;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200714 break;
715 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200716 LOGP(DLMGCP, LOGL_NOTICE,
717 "MDCX: endpoint:%x Unhandled MGCP option: '%c'/%d\n",
718 ENDPOINT_NUMBER(endp), line[0], line[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200719 break;
720 }
721 }
722
Philipp Maier87bd9be2017-08-22 16:35:41 +0200723mgcp_header_done:
Philipp Maier01d24a32017-11-21 17:26:09 +0100724 if (!conn_id) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200725 LOGP(DLMGCP, LOGL_ERROR,
726 "MDCX: endpoint:%x insufficient parameters, missing ci (connectionIdentifier)\n",
727 ENDPOINT_NUMBER(endp));
728 return create_err_response(endp, 400, "MDCX", p->trans);
729 }
730
731 conn = mgcp_conn_get_rtp(endp, conn_id);
732 if (!conn)
733 return create_err_response(endp, 400, "MDCX", p->trans);
734
735 if (mode) {
736 if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
737 error_code = 517;
738 goto error3;
739 }
740 } else
741 conn->conn->mode = conn->conn->mode_orig;
742
743 if (have_sdp)
Philipp Maier8970c492017-10-11 13:33:42 +0200744 mgcp_parse_sdp_data(endp, conn, p);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200745
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200746 set_local_cx_options(endp->tcfg->endpoints, &endp->local_options,
747 local_options);
748
749 if (!have_sdp && endp->local_options.codec)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200750 mgcp_set_audio_info(p->cfg, &conn->end.codec,
751 PTYPE_UNDEFINED, endp->local_options.codec);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200752
Philipp Maier87bd9be2017-08-22 16:35:41 +0200753 if (setup_rtp_processing(endp, conn) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200754 goto error3;
755
Philipp Maier87bd9be2017-08-22 16:35:41 +0200756
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200757 /* policy CB */
758 if (p->cfg->policy_cb) {
759 int rc;
760 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200761 MGCP_ENDP_MDCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200762 switch (rc) {
763 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200764 LOGP(DLMGCP, LOGL_NOTICE,
765 "MDCX: endpoint:%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200766 ENDPOINT_NUMBER(endp));
767 if (silent)
768 goto out_silent;
769 return create_err_response(endp, 400, "MDCX", p->trans);
770 break;
771 case MGCP_POLICY_DEFER:
772 /* stop processing */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200773 LOGP(DLMGCP, LOGL_DEBUG,
774 "MDCX: endpoint:%x defered by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200775 ENDPOINT_NUMBER(endp));
776 return NULL;
777 break;
778 case MGCP_POLICY_CONT:
779 /* just continue */
780 break;
781 }
782 }
783
Philipp Maier87bd9be2017-08-22 16:35:41 +0200784 mgcp_rtp_end_config(endp, 1, &conn->end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200785
786 /* modify */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200787 LOGP(DLMGCP, LOGL_DEBUG,
788 "MDCX: endpoint:%x modified conn:%s\n",
789 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200790 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200791 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
792 MGCP_ENDP_MDCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200793
Philipp Maiere726d4f2017-11-01 10:41:34 +0100794 /* Send dummy packet, see also comments in mgcp_keepalive_timer_cb() */
795 OSMO_ASSERT(endp->tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
796 if (conn->conn->mode & MGCP_CONN_RECV_ONLY
797 && endp->tcfg->keepalive_interval != MGCP_KEEPALIVE_NEVER)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200798 send_dummy(endp, conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200799
800 if (silent)
801 goto out_silent;
802
Philipp Maier87bd9be2017-08-22 16:35:41 +0200803 LOGP(DLMGCP, LOGL_NOTICE,
804 "MDCX: endpoint:%x connection successfully modified\n",
805 ENDPOINT_NUMBER(endp));
806 return create_response_with_sdp(endp, conn, "MDCX", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200807error3:
808 return create_err_response(endp, error_code, "MDCX", p->trans);
809
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200810out_silent:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200811 LOGP(DLMGCP, LOGL_DEBUG, "MDCX: endpoint:%x silent exit\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200812 ENDPOINT_NUMBER(endp));
813 return NULL;
814}
815
Philipp Maier87bd9be2017-08-22 16:35:41 +0200816/* DLCX command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200817static struct msgb *handle_delete_con(struct mgcp_parse_data *p)
818{
819 struct mgcp_endpoint *endp = p->endp;
820 int error_code = 400;
821 int silent = 0;
822 char *line;
823 char stats[1048];
Philipp Maier01d24a32017-11-21 17:26:09 +0100824 const char *conn_id = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200825 struct mgcp_conn_rtp *conn = NULL;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200826
Neels Hofmeyr653c4ff2017-11-10 01:41:45 +0100827 if (p->found != 0)
828 return create_err_response(NULL, error_code, "DLCX", p->trans);
829
Philipp Maier87bd9be2017-08-22 16:35:41 +0200830 LOGP(DLMGCP, LOGL_NOTICE,
831 "DLCX: endpoint:%x deleting connection ...\n",
832 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200833
Philipp Maier87bd9be2017-08-22 16:35:41 +0200834 if (llist_count(&endp->conns) <= 0) {
835 LOGP(DLMGCP, LOGL_ERROR,
836 "DLCX: endpoint:%x endpoint is not holding a connection.\n",
837 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200838 return create_err_response(endp, 400, "DLCX", p->trans);
839 }
840
841 for_each_line(line, p->save) {
842 if (!mgcp_check_param(endp, line))
843 continue;
844
845 switch (line[0]) {
846 case 'C':
Philipp Maier87bd9be2017-08-22 16:35:41 +0200847 if (mgcp_verify_call_id(endp, line + 3) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200848 goto error3;
849 break;
850 case 'I':
Philipp Maier01d24a32017-11-21 17:26:09 +0100851 conn_id = (const char *)line + 3;
852 if (mgcp_verify_ci(endp, conn_id) != 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200853 goto error3;
854 break;
855 case 'Z':
856 silent = strcmp("noanswer", line + 3) == 0;
857 break;
858 default:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200859 LOGP(DLMGCP, LOGL_NOTICE,
860 "DLCX: endpoint:%x Unhandled MGCP option: '%c'/%d\n",
861 ENDPOINT_NUMBER(endp), line[0], line[0]);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200862 break;
863 }
864 }
865
866 /* policy CB */
867 if (p->cfg->policy_cb) {
868 int rc;
869 rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200870 MGCP_ENDP_DLCX, p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200871 switch (rc) {
872 case MGCP_POLICY_REJECT:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200873 LOGP(DLMGCP, LOGL_NOTICE,
874 "DLCX: endpoint:%x rejected by policy\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200875 ENDPOINT_NUMBER(endp));
876 if (silent)
877 goto out_silent;
878 return create_err_response(endp, 400, "DLCX", p->trans);
879 break;
880 case MGCP_POLICY_DEFER:
881 /* stop processing */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200882 return NULL;
883 break;
884 case MGCP_POLICY_CONT:
885 /* just continue */
886 break;
887 }
888 }
889
Philipp Maierf4c0e372017-10-11 16:06:45 +0200890 /* When no connection id is supplied, we will interpret this as a
891 * wildcarded DLCX and drop all connections at once. (See also
892 * RFC3435 Section F.7) */
Philipp Maier01d24a32017-11-21 17:26:09 +0100893 if (!conn_id) {
Philipp Maierf4c0e372017-10-11 16:06:45 +0200894 LOGP(DLMGCP, LOGL_NOTICE,
895 "DLCX: endpoint:%x missing ci (connectionIdentifier), will remove all connections at once\n",
896 ENDPOINT_NUMBER(endp));
897
898 mgcp_release_endp(endp);
899
900 /* Note: In this case we do not return any statistics,
901 * as we assume that the client is not interested in
902 * this case. */
903 return create_ok_response(endp, 200, "DLCX", p->trans);
904 }
905
Philipp Maierf4c0e372017-10-11 16:06:45 +0200906 /* Find the connection */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200907 conn = mgcp_conn_get_rtp(endp, conn_id);
908 if (!conn)
909 goto error3;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200910
Philipp Maier87bd9be2017-08-22 16:35:41 +0200911 /* save the statistics of the current connection */
912 mgcp_format_stats(stats, sizeof(stats), conn->conn);
913
914 /* delete connection */
915 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:%x deleting conn:%s\n",
916 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
917 mgcp_conn_free(endp, conn_id);
918 LOGP(DLMGCP, LOGL_NOTICE,
919 "DLCX: endpoint:%x connection successfully deleted\n",
920 ENDPOINT_NUMBER(endp));
921
922 /* When all connections are closed, the endpoint will be released
923 * in order to be ready to be used by another call. */
924 if (llist_count(&endp->conns) <= 0) {
925 mgcp_release_endp(endp);
926 LOGP(DLMGCP, LOGL_DEBUG,
927 "DLCX: endpoint:%x endpoint released\n",
928 ENDPOINT_NUMBER(endp));
929 }
930
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200931 if (p->cfg->change_cb)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200932 p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
933 MGCP_ENDP_DLCX);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200934
935 if (silent)
936 goto out_silent;
937 return create_ok_resp_with_param(endp, 250, "DLCX", p->trans, stats);
938
939error3:
940 return create_err_response(endp, error_code, "DLCX", p->trans);
941
942out_silent:
Philipp Maier87bd9be2017-08-22 16:35:41 +0200943 LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:%x silent exit\n",
944 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200945 return NULL;
946}
947
Philipp Maier87bd9be2017-08-22 16:35:41 +0200948/* RSIP command handler, processes the received command */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200949static struct msgb *handle_rsip(struct mgcp_parse_data *p)
950{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200951 /* TODO: Also implement the resetting of a specific endpoint
952 * to make mgcp_send_reset_ep() work. Currently this will call
953 * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1
954 * to make read_call_agent() reset all endpoints when called
955 * next time. In order to selectively reset endpoints some
956 * mechanism to distinguish which endpoint shall be resetted
957 * is needed */
958
959 LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n");
960
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200961 if (p->found != 0) {
Philipp Maier87bd9be2017-08-22 16:35:41 +0200962 LOGP(DLMGCP, LOGL_ERROR,
963 "RSIP: failed to find the endpoint.\n");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200964 return NULL;
965 }
966
967 if (p->cfg->reset_cb)
968 p->cfg->reset_cb(p->endp->tcfg);
969 return NULL;
970}
971
972static char extract_tone(const char *line)
973{
974 const char *str = strstr(line, "D/");
975 if (!str)
976 return CHAR_MAX;
977
978 return str[2];
979}
980
Philipp Maier87bd9be2017-08-22 16:35:41 +0200981/* This can request like DTMF detection and forward, fax detection... it
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200982 * can also request when the notification should be send and such. We don't
Philipp Maier87bd9be2017-08-22 16:35:41 +0200983 * do this right now. */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200984static struct msgb *handle_noti_req(struct mgcp_parse_data *p)
985{
986 int res = 0;
987 char *line;
988 char tone = CHAR_MAX;
989
Philipp Maier87bd9be2017-08-22 16:35:41 +0200990 LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n");
991
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200992 if (p->found != 0)
993 return create_err_response(NULL, 400, "RQNT", p->trans);
994
995 for_each_line(line, p->save) {
996 switch (line[0]) {
997 case 'S':
998 tone = extract_tone(line);
999 break;
1000 }
1001 }
1002
1003 /* we didn't see a signal request with a tone */
1004 if (tone == CHAR_MAX)
1005 return create_ok_response(p->endp, 200, "RQNT", p->trans);
1006
1007 if (p->cfg->rqnt_cb)
1008 res = p->cfg->rqnt_cb(p->endp, tone);
1009
1010 return res == 0 ?
Philipp Maier87bd9be2017-08-22 16:35:41 +02001011 create_ok_response(p->endp, 200, "RQNT", p->trans) :
1012 create_err_response(p->endp, res, "RQNT", p->trans);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001013}
1014
Philipp Maier87bd9be2017-08-22 16:35:41 +02001015/* Connection keepalive timer, will take care that dummy packets are send
1016 * regulary, so that NAT connections stay open */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001017static void mgcp_keepalive_timer_cb(void *_tcfg)
1018{
1019 struct mgcp_trunk_config *tcfg = _tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001020 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001021 int i;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001022
Philipp Maiere726d4f2017-11-01 10:41:34 +01001023 LOGP(DLMGCP, LOGL_DEBUG, "triggered trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001024 tcfg->trunk_nr);
1025
Philipp Maiere726d4f2017-11-01 10:41:34 +01001026 /* Do not accept invalid configuration values
1027 * valid is MGCP_KEEPALIVE_NEVER, MGCP_KEEPALIVE_ONCE and
1028 * values greater 0 */
1029 OSMO_ASSERT(tcfg->keepalive_interval >= MGCP_KEEPALIVE_ONCE);
1030
1031 /* The dummy packet functionality has been disabled, we will exit
1032 * immediately, no further timer is scheduled, which means we will no
1033 * longer send dummy packets even when we did before */
1034 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_NEVER)
1035 return;
1036
1037 /* In cases where only one dummy packet is sent, we do not need
1038 * the timer since the functions that handle the CRCX and MDCX are
1039 * triggering the sending of the dummy packet. So we behave like in
1040 * the MGCP_KEEPALIVE_NEVER case */
1041 if (tcfg->keepalive_interval == MGCP_KEEPALIVE_ONCE)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001042 return;
1043
Philipp Maier87bd9be2017-08-22 16:35:41 +02001044 /* Send walk over all endpoints and send out dummy packets through
1045 * every connection present on each endpoint */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001046 for (i = 1; i < tcfg->number_endpoints; ++i) {
1047 struct mgcp_endpoint *endp = &tcfg->endpoints[i];
Philipp Maier87bd9be2017-08-22 16:35:41 +02001048 llist_for_each_entry(conn, &endp->conns, entry) {
1049 if (conn->mode == MGCP_CONN_RECV_ONLY)
1050 send_dummy(endp, &conn->u.rtp);
1051 }
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001052 }
1053
Philipp Maiere726d4f2017-11-01 10:41:34 +01001054 /* Schedule the keepalive timer for the next round */
1055 LOGP(DLMGCP, LOGL_DEBUG, "rescheduling trunk %d keepalive timer\n",
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001056 tcfg->trunk_nr);
Philipp Maier87bd9be2017-08-22 16:35:41 +02001057 osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval,
1058 0);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001059}
1060
1061void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval)
1062{
1063 tcfg->keepalive_interval = interval;
1064 osmo_timer_setup(&tcfg->keepalive_timer, mgcp_keepalive_timer_cb, tcfg);
1065
1066 if (interval <= 0)
1067 osmo_timer_del(&tcfg->keepalive_timer);
1068 else
1069 osmo_timer_schedule(&tcfg->keepalive_timer,
1070 tcfg->keepalive_interval, 0);
1071}
1072
Philipp Maier87bd9be2017-08-22 16:35:41 +02001073/*! allocate configuration with default values.
1074 * (called once at startup by main function) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001075struct mgcp_config *mgcp_config_alloc(void)
1076{
1077 struct mgcp_config *cfg;
1078
1079 cfg = talloc_zero(NULL, struct mgcp_config);
1080 if (!cfg) {
1081 LOGP(DLMGCP, LOGL_FATAL, "Failed to allocate config.\n");
1082 return NULL;
1083 }
1084
Philipp Maier87bd9be2017-08-22 16:35:41 +02001085 cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START;
1086 cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END;
1087 cfg->net_ports.last_port = cfg->net_ports.range_start;
1088
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001089 cfg->source_port = 2427;
1090 cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
1091 cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
1092
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001093 cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
1094 cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
1095
1096 cfg->get_net_downlink_format_cb = &mgcp_get_net_downlink_format_default;
1097
1098 /* default trunk handling */
1099 cfg->trunk.cfg = cfg;
1100 cfg->trunk.trunk_nr = 0;
1101 cfg->trunk.trunk_type = MGCP_TRUNK_VIRTUAL;
1102 cfg->trunk.audio_name = talloc_strdup(cfg, "AMR/8000");
1103 cfg->trunk.audio_payload = 126;
1104 cfg->trunk.audio_send_ptime = 1;
1105 cfg->trunk.audio_send_name = 1;
1106 cfg->trunk.omit_rtcp = 0;
1107 mgcp_trunk_set_keepalive(&cfg->trunk, MGCP_KEEPALIVE_ONCE);
1108
1109 INIT_LLIST_HEAD(&cfg->trunks);
1110
1111 return cfg;
1112}
1113
Philipp Maier87bd9be2017-08-22 16:35:41 +02001114/*! allocate configuration with default values.
1115 * (called once at startup by VTY)
1116 * \param[in] cfg mgcp configuration
1117 * \param[in] nr trunk number
1118 * \returns pointer to allocated trunk configuration */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001119struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
1120{
1121 struct mgcp_trunk_config *trunk;
1122
1123 trunk = talloc_zero(cfg, struct mgcp_trunk_config);
1124 if (!trunk) {
1125 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n");
1126 return NULL;
1127 }
1128
1129 trunk->cfg = cfg;
1130 trunk->trunk_type = MGCP_TRUNK_E1;
1131 trunk->trunk_nr = nr;
1132 trunk->audio_name = talloc_strdup(cfg, "AMR/8000");
1133 trunk->audio_payload = 126;
1134 trunk->audio_send_ptime = 1;
1135 trunk->audio_send_name = 1;
Philipp Maierfcd06552017-11-10 17:32:22 +01001136 trunk->vty_number_endpoints = 33;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001137 trunk->omit_rtcp = 0;
1138 mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
1139 llist_add_tail(&trunk->entry, &cfg->trunks);
1140 return trunk;
1141}
1142
Philipp Maier87bd9be2017-08-22 16:35:41 +02001143/*! get trunk configuration by trunk number (index).
1144 * \param[in] cfg mgcp configuration
1145 * \param[in] index trunk number
1146 * \returns pointer to trunk configuration, NULL on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001147struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
1148{
1149 struct mgcp_trunk_config *trunk;
1150
1151 llist_for_each_entry(trunk, &cfg->trunks, entry)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001152 if (trunk->trunk_nr == index)
1153 return trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001154
1155 return NULL;
1156}
1157
Philipp Maier87bd9be2017-08-22 16:35:41 +02001158/*! allocate endpoints and set default values.
1159 * (called once at startup by VTY)
1160 * \param[in] tcfg trunk configuration
1161 * \returns 0 on success, -1 on failure */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001162int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
1163{
1164 int i;
1165
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001166 tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001167 sizeof(struct mgcp_endpoint),
Philipp Maierfcd06552017-11-10 17:32:22 +01001168 tcfg->vty_number_endpoints,
Philipp Maier87bd9be2017-08-22 16:35:41 +02001169 "endpoints");
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001170 if (!tcfg->endpoints)
1171 return -1;
1172
Philipp Maierfcd06552017-11-10 17:32:22 +01001173 for (i = 0; i < tcfg->vty_number_endpoints; ++i) {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001174 INIT_LLIST_HEAD(&tcfg->endpoints[i].conns);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001175 tcfg->endpoints[i].cfg = tcfg->cfg;
1176 tcfg->endpoints[i].tcfg = tcfg;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001177
1178 /* NOTE: Currently all endpoints are of type RTP, this will
1179 * change when new variations are implemented */
1180 tcfg->endpoints[i].type = &ep_typeset.rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001181 }
1182
Philipp Maierfcd06552017-11-10 17:32:22 +01001183 tcfg->number_endpoints = tcfg->vty_number_endpoints;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001184 return 0;
1185}
1186
Philipp Maier87bd9be2017-08-22 16:35:41 +02001187/*! relase endpoint, all open connections are closed.
1188 * \param[in] endp endpoint to release */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001189void mgcp_release_endp(struct mgcp_endpoint *endp)
1190{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001191 LOGP(DLMGCP, LOGL_DEBUG, "Releasing endpoint:%x\n",
1192 ENDPOINT_NUMBER(endp));
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001193
Philipp Maier87bd9be2017-08-22 16:35:41 +02001194 /* Normally this function should only be called wehen
1195 * all connections have been removed already. In case
1196 * that there are still connections open (e.g. when
1197 * RSIP is executed), free them all at once. */
1198 mgcp_conn_free_all(endp);
1199
1200 /* Reset endpoint parameters and states */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001201 talloc_free(endp->callid);
1202 endp->callid = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001203 talloc_free(endp->local_options.string);
1204 endp->local_options.string = NULL;
1205 talloc_free(endp->local_options.codec);
1206 endp->local_options.codec = NULL;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001207}
1208
1209static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
1210{
1211 return write(cfg->gw_fd.bfd.fd, buf, len);
1212}
1213
Philipp Maier87bd9be2017-08-22 16:35:41 +02001214/*! Reset all endpoints by sending RSIP message to self.
1215 * (called by VTY)
1216 * \param[in] endp trunk endpoint
1217 * \param[in] endpoint number
1218 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001219int mgcp_send_reset_all(struct mgcp_config *cfg)
1220{
Philipp Maier87bd9be2017-08-22 16:35:41 +02001221 int rc;
1222
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001223 static const char mgcp_reset[] = {
Philipp Maier87bd9be2017-08-22 16:35:41 +02001224 "RSIP 1 *@mgw MGCP 1.0\r\n"
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001225 };
1226
Philipp Maier87bd9be2017-08-22 16:35:41 +02001227 rc = send_agent(cfg, mgcp_reset, sizeof mgcp_reset - 1);
1228 if (rc <= 0)
1229 return -1;
1230
1231 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001232}
1233
Philipp Maier87bd9be2017-08-22 16:35:41 +02001234/*! Reset a single endpoint by sending RSIP message to self.
1235 * (called by VTY)
1236 * \param[in] endp trunk endpoint
1237 * \param[in] endpoint number
1238 * \returns 0 on success, -1 on error */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001239int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
1240{
1241 char buf[128];
1242 int len;
Philipp Maier87bd9be2017-08-22 16:35:41 +02001243 int rc;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001244
1245 len = snprintf(buf, sizeof(buf),
Philipp Maier87bd9be2017-08-22 16:35:41 +02001246 "RSIP 39 %x@mgw MGCP 1.0\r\n", endpoint);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001247 if (len < 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +02001248 return -1;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001249
1250 buf[sizeof(buf) - 1] = '\0';
1251
Philipp Maier87bd9be2017-08-22 16:35:41 +02001252 rc = send_agent(endp->cfg, buf, len);
1253 if (rc <= 0)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001254 return -1;
1255
Philipp Maier87bd9be2017-08-22 16:35:41 +02001256 return 0;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001257}