blob: 60a1700c0a4dc301105cb6311f71ff35f2ebad8b [file] [log] [blame]
Philipp Maier87bd9be2017-08-22 16:35:41 +02001/* Message connection list handling */
2
3/*
4 * (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
5 * All Rights Reserved
6 *
7 * Author: Philipp Maier
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 <osmocom/mgcp/mgcp_conn.h>
25#include <osmocom/mgcp/mgcp_internal.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020026#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010027#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maierbc0346e2018-06-07 09:52:16 +020028#include <osmocom/mgcp/mgcp_sdp.h>
29#include <osmocom/mgcp/mgcp_codec.h>
Philipp Maierffd75e42017-11-22 11:44:50 +010030#include <osmocom/gsm/gsm_utils.h>
Philipp Maier9e1d1642018-05-09 16:26:34 +020031#include <osmocom/core/rate_ctr.h>
Oliver Smithe36b7752019-01-22 16:31:36 +010032#include <osmocom/core/timer.h>
Philipp Maierffd75e42017-11-22 11:44:50 +010033#include <ctype.h>
34
Philipp Maier9e1d1642018-05-09 16:26:34 +020035const static struct rate_ctr_group_desc rate_ctr_group_desc = {
36 .group_name_prefix = "conn_rtp",
37 .group_description = "rtp connection statistics",
38 .class_id = 1,
Stefan Sperlingba25eab2018-10-30 14:32:31 +010039 .num_ctr = ARRAY_SIZE(mgcp_conn_rate_ctr_desc),
40 .ctr_desc = mgcp_conn_rate_ctr_desc
Philipp Maier9e1d1642018-05-09 16:26:34 +020041};
42
43
Philipp Maierffd75e42017-11-22 11:44:50 +010044/* Allocate a new connection identifier. According to RFC3435, they must
Philipp Maierf8bfbe82017-11-23 19:32:31 +010045 * be unique only within the scope of the endpoint. (Caller must provide
46 * memory for id) */
Philipp Maierffd75e42017-11-22 11:44:50 +010047static int mgcp_alloc_id(struct mgcp_endpoint *endp, char *id)
48{
Neels Hofmeyra729db62018-08-28 16:20:51 +020049#define MGCP_CONN_ID_GEN_LEN 8
Philipp Maierffd75e42017-11-22 11:44:50 +010050 int i;
51 int k;
52 int rc;
Neels Hofmeyra729db62018-08-28 16:20:51 +020053 uint8_t id_bin[MGCP_CONN_ID_GEN_LEN / 2];
Philipp Maierffd75e42017-11-22 11:44:50 +010054 char *id_hex;
55
56 /* Generate a connection id that is unique for the current endpoint.
57 * Technically a counter would be sufficient, but in order to
58 * be able to find a specific connection in large logfiles and to
59 * prevent unintentional connections we assign the connection
60 * identifiers randomly from a reasonable large number space */
61 for (i = 0; i < 32; i++) {
62 rc = osmo_get_rand_id(id_bin, sizeof(id_bin));
63 if (rc < 0)
64 return rc;
65
66 id_hex = osmo_hexdump_nospc(id_bin, sizeof(id_bin));
67 for (k = 0; k < strlen(id_hex); k++)
68 id_hex[k] = toupper(id_hex[k]);
69
70 /* ensure that the generated conn_id is unique
71 * for this endpoint */
72 if (!mgcp_conn_get_rtp(endp, id_hex)) {
Neels Hofmeyr55e0dcf2018-09-03 21:36:56 +020073 osmo_strlcpy(id, id_hex, MGCP_CONN_ID_MAXLEN);
Philipp Maierffd75e42017-11-22 11:44:50 +010074 return 0;
75 }
76 }
77
Pau Espin Pedrol3239f622019-04-24 18:47:46 +020078 LOGPENDP(endp, DLMGCP, LOGL_ERROR, "unable to generate a unique connectionIdentifier\n");
Philipp Maierffd75e42017-11-22 11:44:50 +010079
80 return -1;
81}
Philipp Maier87bd9be2017-08-22 16:35:41 +020082
Philipp Maierc430d192018-03-16 12:11:18 +010083/* Initialize rtp connection struct with default values */
Harald Welte3ac604e2019-05-08 14:07:41 +020084static int mgcp_rtp_conn_init(struct mgcp_conn_rtp *conn_rtp, struct mgcp_conn *conn)
Philipp Maier87bd9be2017-08-22 16:35:41 +020085{
Philipp Maier892dec02018-03-16 12:32:01 +010086 struct mgcp_rtp_end *end = &conn_rtp->end;
Philipp Maier9e1d1642018-05-09 16:26:34 +020087 /* FIXME: Each new rate counter group requires an unique index. At the
88 * moment we generate this index using this counter, but perhaps there
89 * is a more concious way to assign the indexes. */
90 static unsigned int rate_ctr_index = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +020091
Philipp Maier892dec02018-03-16 12:32:01 +010092 conn_rtp->type = MGCP_RTP_DEFAULT;
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +020093 conn_rtp->osmux.cid_allocated = false;
94 conn_rtp->osmux.cid = 0;
Philipp Maier892dec02018-03-16 12:32:01 +010095
96 /* backpointer to the generic part of the connection */
97 conn->u.rtp.conn = conn;
Philipp Maier87bd9be2017-08-22 16:35:41 +020098
99 end->rtp.fd = -1;
100 end->rtcp.fd = -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200101 end->rtp_port = end->rtcp_port = 0;
102 talloc_free(end->fmtp_extra);
103 end->fmtp_extra = NULL;
104
105 /* Set default values */
106 end->frames_per_packet = 0; /* unknown */
107 end->packet_duration_ms = DEFAULT_RTP_AUDIO_PACKET_DURATION_MS;
108 end->output_enabled = 0;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200109 end->maximum_packet_time = -1;
Philipp Maier9e1d1642018-05-09 16:26:34 +0200110
111 conn_rtp->rate_ctr_group = rate_ctr_group_alloc(conn, &rate_ctr_group_desc, rate_ctr_index);
Harald Welte3ac604e2019-05-08 14:07:41 +0200112 if (!conn_rtp->rate_ctr_group)
113 return -1;
114
Philipp Maier9e1d1642018-05-09 16:26:34 +0200115 conn_rtp->state.in_stream.err_ts_ctr = &conn_rtp->rate_ctr_group->ctr[IN_STREAM_ERR_TSTMP_CTR];
116 conn_rtp->state.out_stream.err_ts_ctr = &conn_rtp->rate_ctr_group->ctr[OUT_STREAM_ERR_TSTMP_CTR];
117 rate_ctr_index++;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200118
119 /* Make sure codec table is reset */
120 mgcp_codec_reset_all(conn_rtp);
Harald Welte3ac604e2019-05-08 14:07:41 +0200121
122 return 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200123}
124
Philipp Maierd0b470d2018-03-16 12:45:53 +0100125/* Cleanup rtp connection struct */
126static void mgcp_rtp_conn_cleanup(struct mgcp_conn_rtp *conn_rtp)
127{
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200128 if (mgcp_conn_rtp_is_osmux(conn_rtp))
129 conn_osmux_disable(conn_rtp);
Philipp Maierd0b470d2018-03-16 12:45:53 +0100130 mgcp_free_rtp_port(&conn_rtp->end);
Philipp Maier9e1d1642018-05-09 16:26:34 +0200131 rate_ctr_group_free(conn_rtp->rate_ctr_group);
Neels Hofmeyrefe3f982019-08-08 22:14:08 +0200132 mgcp_codec_reset_all(conn_rtp);
Philipp Maierd0b470d2018-03-16 12:45:53 +0100133}
134
Oliver Smithe36b7752019-01-22 16:31:36 +0100135void mgcp_conn_watchdog_cb(void *data)
136{
137 struct mgcp_conn *conn = data;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200138 LOGPCONN(conn, DLMGCP, LOGL_ERROR, "connection timed out!\n");
Oliver Smithe36b7752019-01-22 16:31:36 +0100139 mgcp_conn_free(conn->endp, conn->id);
140}
141
142void mgcp_conn_watchdog_kick(struct mgcp_conn *conn)
143{
144 int timeout = conn->endp->cfg->conn_timeout;
145 if (!timeout)
146 return;
147
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200148 LOGPCONN(conn, DLMGCP, LOGL_DEBUG, "watchdog kicked\n");
Oliver Smithe36b7752019-01-22 16:31:36 +0100149 osmo_timer_schedule(&conn->watchdog, timeout, 0);
150}
151
Philipp Maier87bd9be2017-08-22 16:35:41 +0200152/*! allocate a new connection list entry.
153 * \param[in] ctx talloc context
154 * \param[in] endp associated endpoint
155 * \param[in] id identification number of the connection
156 * \param[in] type connection type (e.g. MGCP_CONN_TYPE_RTP)
157 * \returns pointer to allocated connection, NULL on error */
158struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp,
Philipp Maierffd75e42017-11-22 11:44:50 +0100159 enum mgcp_conn_type type, char *name)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200160{
161 struct mgcp_conn *conn;
Philipp Maierffd75e42017-11-22 11:44:50 +0100162 int rc;
163
Philipp Maier87bd9be2017-08-22 16:35:41 +0200164 /* Do not allow more then two connections */
165 if (llist_count(&endp->conns) >= endp->type->max_conns)
166 return NULL;
167
Philipp Maier87bd9be2017-08-22 16:35:41 +0200168 /* Create new connection and add it to the list */
169 conn = talloc_zero(ctx, struct mgcp_conn);
170 if (!conn)
171 return NULL;
172 conn->endp = endp;
173 conn->type = type;
174 conn->mode = MGCP_CONN_NONE;
175 conn->mode_orig = MGCP_CONN_NONE;
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100176 osmo_strlcpy(conn->name, name, sizeof(conn->name));
Philipp Maierffd75e42017-11-22 11:44:50 +0100177 rc = mgcp_alloc_id(endp, conn->id);
178 if (rc < 0) {
179 talloc_free(conn);
180 return NULL;
181 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200182
183 switch (type) {
184 case MGCP_CONN_TYPE_RTP:
Harald Welte3ac604e2019-05-08 14:07:41 +0200185 if (mgcp_rtp_conn_init(&conn->u.rtp, conn) < 0) {
186 talloc_free(conn);
187 return NULL;
188 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200189 break;
190 default:
191 /* NOTE: This should never be called with an
192 * invalid type, its up to the programmer
193 * to ensure propery types */
194 OSMO_ASSERT(false);
195 }
196
Oliver Smithe36b7752019-01-22 16:31:36 +0100197 /* Initialize watchdog */
198 osmo_timer_setup(&conn->watchdog, mgcp_conn_watchdog_cb, conn);
199 mgcp_conn_watchdog_kick(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200200 llist_add(&conn->entry, &endp->conns);
201
202 return conn;
203}
204
205/*! find a connection by its ID.
206 * \param[in] endp associated endpoint
207 * \param[in] id identification number of the connection
208 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100209struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200210{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200211 struct mgcp_conn *conn;
Neels Hofmeyr65317262018-09-03 22:11:05 +0200212 const char *id_upper;
Neels Hofmeyra77eade2018-08-29 02:30:39 +0200213 const char *conn_id;
Neels Hofmeyr65317262018-09-03 22:11:05 +0200214
215 if (!id || !*id)
216 return NULL;
217
Neels Hofmeyra77eade2018-08-29 02:30:39 +0200218 /* Ignore leading zeros in needle */
219 while (*id == '0')
220 id++;
221
Neels Hofmeyr65317262018-09-03 22:11:05 +0200222 /* Use uppercase to compare identifiers, to avoid mismatches: RFC3435 2.1.3.2 "Names of
223 * Connections" defines the id as a hex string, so clients may return lower case hex even though
224 * we sent upper case hex in the CRCX response. */
225 id_upper = osmo_str_toupper(id);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200226
227 llist_for_each_entry(conn, &endp->conns, entry) {
Neels Hofmeyra77eade2018-08-29 02:30:39 +0200228 /* Ignore leading zeros in haystack */
229 for (conn_id=conn->id; *conn_id == '0'; conn_id++);
230
231 if (strcmp(conn_id, id_upper) == 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200232 return conn;
233 }
234
235 return NULL;
236}
237
238/*! find an RTP connection by its ID.
239 * \param[in] endp associated endpoint
240 * \param[in] id identification number of the connection
241 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100242struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp,
243 const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200244{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200245 struct mgcp_conn *conn;
246
247 conn = mgcp_conn_get(endp, id);
248 if (!conn)
249 return NULL;
250
251 if (conn->type == MGCP_CONN_TYPE_RTP)
252 return &conn->u.rtp;
253
254 return NULL;
255}
256
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100257static void
258aggregate_rtp_conn_stats(struct mgcp_trunk_config *trunk, struct mgcp_conn_rtp *conn_rtp)
259{
260 struct rate_ctr_group *all_stats = trunk->all_rtp_conn_stats;
261 struct rate_ctr_group *conn_stats = conn_rtp->rate_ctr_group;
262 int i;
263
264 if (all_stats == NULL || conn_stats == NULL)
265 return;
266
267 /* Compared to per-connection RTP statistics, aggregated RTP statistics
268 * contain one additional rate couter item (RTP_NUM_CONNECTIONS).
269 * All other counters in both counter groups correspond to each other. */
270 OSMO_ASSERT(conn_stats->desc->num_ctr + 1 == all_stats->desc->num_ctr);
271
272 for (i = 0; i < conn_stats->desc->num_ctr; i++)
273 rate_ctr_add(&all_stats->ctr[i], conn_stats->ctr[i].current);
274
275 rate_ctr_inc(&all_stats->ctr[RTP_NUM_CONNECTIONS]);
276}
277
Philipp Maier87bd9be2017-08-22 16:35:41 +0200278/*! free a connection by its ID.
279 * \param[in] endp associated endpoint
280 * \param[in] id identification number of the connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100281void mgcp_conn_free(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200282{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200283 struct mgcp_conn *conn;
284
285 conn = mgcp_conn_get(endp, id);
286 if (!conn)
287 return;
288
Philipp Maierdf5d2192018-01-24 11:39:32 +0100289 /* Run endpoint cleanup action. By this we inform the endpoint about
290 * the removal of the connection and allow it to clean up its inner
291 * state accordingly */
292 if (endp->type->cleanup_cb)
293 endp->type->cleanup_cb(endp, conn);
294
Philipp Maier87bd9be2017-08-22 16:35:41 +0200295 switch (conn->type) {
296 case MGCP_CONN_TYPE_RTP:
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100297 aggregate_rtp_conn_stats(endp->tcfg, &conn->u.rtp);
Philipp Maierd0b470d2018-03-16 12:45:53 +0100298 mgcp_rtp_conn_cleanup(&conn->u.rtp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200299 break;
300 default:
301 /* NOTE: This should never be called with an
302 * invalid type, its up to the programmer
303 * to ensure propery types */
304 OSMO_ASSERT(false);
305 }
306
Oliver Smithe36b7752019-01-22 16:31:36 +0100307 osmo_timer_del(&conn->watchdog);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200308 llist_del(&conn->entry);
309 talloc_free(conn);
310}
311
312/*! free oldest connection in the list.
313 * \param[in] endp associated endpoint */
314void mgcp_conn_free_oldest(struct mgcp_endpoint *endp)
315{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200316 struct mgcp_conn *conn;
317
318 if (llist_empty(&endp->conns))
319 return;
320
321 conn = llist_last_entry(&endp->conns, struct mgcp_conn, entry);
322 if (!conn)
323 return;
324
325 mgcp_conn_free(endp, conn->id);
326}
327
328/*! free all connections at once.
329 * \param[in] endp associated endpoint */
330void mgcp_conn_free_all(struct mgcp_endpoint *endp)
331{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200332 struct mgcp_conn *conn;
333 struct mgcp_conn *conn_tmp;
334
335 /* Drop all items in the list */
336 llist_for_each_entry_safe(conn, conn_tmp, &endp->conns, entry) {
337 mgcp_conn_free(endp, conn->id);
338 }
339
340 return;
341}
342
Harald Welte1d1b98f2017-12-25 10:03:40 +0100343/*! dump basic connection information to human readable string.
Philipp Maier87bd9be2017-08-22 16:35:41 +0200344 * \param[in] conn to dump
Harald Welte1d1b98f2017-12-25 10:03:40 +0100345 * \returns human readable string */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200346char *mgcp_conn_dump(struct mgcp_conn *conn)
347{
Philipp Maier01d24a32017-11-21 17:26:09 +0100348 static char str[sizeof(conn->name)+sizeof(conn->id)+256];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200349
350 if (!conn) {
351 snprintf(str, sizeof(str), "(null connection)");
352 return str;
353 }
354
355 switch (conn->type) {
356 case MGCP_CONN_TYPE_RTP:
357 /* Dump RTP connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100358 snprintf(str, sizeof(str), "(%s/rtp, id:0x%s, ip:%s, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200359 "rtp:%u rtcp:%u)",
360 conn->name,
361 conn->id,
362 inet_ntoa(conn->u.rtp.end.addr),
363 ntohs(conn->u.rtp.end.rtp_port),
364 ntohs(conn->u.rtp.end.rtcp_port));
365 break;
366
367 default:
368 /* Should not happen, we should be able to dump
369 * every possible connection type. */
370 snprintf(str, sizeof(str), "(unknown connection type)");
371 break;
372 }
373
374 return str;
375}
376
377/*! find destination connection on a specific endpoint.
378 * \param[in] conn to search a destination for
379 * \returns destination connection, NULL on failure */
380struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn)
381{
382 struct mgcp_endpoint *endp;
383 struct mgcp_conn *partner_conn;
384 endp = conn->endp;
385
386 /*! NOTE: This simply works by grabbing the first connection that is
387 * not the supplied connection, which is suitable for endpoints that
388 * do not serve more than two connections. */
389
390 llist_for_each_entry(partner_conn, &endp->conns, entry) {
391 if (conn != partner_conn) {
392 return partner_conn;
393 }
394 }
395
396 return NULL;
397}