blob: 8c7918e3621f46dd5145ab7e2932d41937d2bee6 [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 Maierc66ab2c2020-06-02 20:55:34 +020028#include <osmocom/mgcp/mgcp_trunk.h>
Philipp Maierbc0346e2018-06-07 09:52:16 +020029#include <osmocom/mgcp/mgcp_sdp.h>
30#include <osmocom/mgcp/mgcp_codec.h>
Philipp Maierffd75e42017-11-22 11:44:50 +010031#include <osmocom/gsm/gsm_utils.h>
Philipp Maier9e1d1642018-05-09 16:26:34 +020032#include <osmocom/core/rate_ctr.h>
Oliver Smithe36b7752019-01-22 16:31:36 +010033#include <osmocom/core/timer.h>
Philipp Maierffd75e42017-11-22 11:44:50 +010034#include <ctype.h>
35
Philipp Maier9e1d1642018-05-09 16:26:34 +020036const static struct rate_ctr_group_desc rate_ctr_group_desc = {
37 .group_name_prefix = "conn_rtp",
38 .group_description = "rtp connection statistics",
39 .class_id = 1,
Stefan Sperlingba25eab2018-10-30 14:32:31 +010040 .num_ctr = ARRAY_SIZE(mgcp_conn_rate_ctr_desc),
41 .ctr_desc = mgcp_conn_rate_ctr_desc
Philipp Maier9e1d1642018-05-09 16:26:34 +020042};
43
44
Philipp Maierffd75e42017-11-22 11:44:50 +010045/* Allocate a new connection identifier. According to RFC3435, they must
Philipp Maierf8bfbe82017-11-23 19:32:31 +010046 * be unique only within the scope of the endpoint. (Caller must provide
47 * memory for id) */
Philipp Maierffd75e42017-11-22 11:44:50 +010048static int mgcp_alloc_id(struct mgcp_endpoint *endp, char *id)
49{
Neels Hofmeyra729db62018-08-28 16:20:51 +020050#define MGCP_CONN_ID_GEN_LEN 8
Philipp Maierffd75e42017-11-22 11:44:50 +010051 int i;
52 int k;
53 int rc;
Neels Hofmeyra729db62018-08-28 16:20:51 +020054 uint8_t id_bin[MGCP_CONN_ID_GEN_LEN / 2];
Philipp Maierffd75e42017-11-22 11:44:50 +010055 char *id_hex;
56
57 /* Generate a connection id that is unique for the current endpoint.
58 * Technically a counter would be sufficient, but in order to
59 * be able to find a specific connection in large logfiles and to
60 * prevent unintentional connections we assign the connection
61 * identifiers randomly from a reasonable large number space */
62 for (i = 0; i < 32; i++) {
63 rc = osmo_get_rand_id(id_bin, sizeof(id_bin));
64 if (rc < 0)
65 return rc;
66
67 id_hex = osmo_hexdump_nospc(id_bin, sizeof(id_bin));
68 for (k = 0; k < strlen(id_hex); k++)
69 id_hex[k] = toupper(id_hex[k]);
70
71 /* ensure that the generated conn_id is unique
72 * for this endpoint */
73 if (!mgcp_conn_get_rtp(endp, id_hex)) {
Neels Hofmeyr55e0dcf2018-09-03 21:36:56 +020074 osmo_strlcpy(id, id_hex, MGCP_CONN_ID_MAXLEN);
Philipp Maierffd75e42017-11-22 11:44:50 +010075 return 0;
76 }
77 }
78
Pau Espin Pedrol3239f622019-04-24 18:47:46 +020079 LOGPENDP(endp, DLMGCP, LOGL_ERROR, "unable to generate a unique connectionIdentifier\n");
Philipp Maierffd75e42017-11-22 11:44:50 +010080
81 return -1;
82}
Philipp Maier87bd9be2017-08-22 16:35:41 +020083
Philipp Maierc430d192018-03-16 12:11:18 +010084/* Initialize rtp connection struct with default values */
Harald Welte3ac604e2019-05-08 14:07:41 +020085static int mgcp_rtp_conn_init(struct mgcp_conn_rtp *conn_rtp, struct mgcp_conn *conn)
Philipp Maier87bd9be2017-08-22 16:35:41 +020086{
Philipp Maier892dec02018-03-16 12:32:01 +010087 struct mgcp_rtp_end *end = &conn_rtp->end;
Philipp Maier9e1d1642018-05-09 16:26:34 +020088 /* FIXME: Each new rate counter group requires an unique index. At the
89 * moment we generate this index using this counter, but perhaps there
90 * is a more concious way to assign the indexes. */
91 static unsigned int rate_ctr_index = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +020092
Philipp Maier892dec02018-03-16 12:32:01 +010093 conn_rtp->type = MGCP_RTP_DEFAULT;
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +020094 conn_rtp->osmux.cid_allocated = false;
95 conn_rtp->osmux.cid = 0;
Philipp Maier892dec02018-03-16 12:32:01 +010096
97 /* backpointer to the generic part of the connection */
98 conn->u.rtp.conn = conn;
Philipp Maier87bd9be2017-08-22 16:35:41 +020099
100 end->rtp.fd = -1;
101 end->rtcp.fd = -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200102 end->rtp_port = end->rtcp_port = 0;
103 talloc_free(end->fmtp_extra);
104 end->fmtp_extra = NULL;
105
106 /* Set default values */
107 end->frames_per_packet = 0; /* unknown */
108 end->packet_duration_ms = DEFAULT_RTP_AUDIO_PACKET_DURATION_MS;
109 end->output_enabled = 0;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200110 end->maximum_packet_time = -1;
Philipp Maier9e1d1642018-05-09 16:26:34 +0200111
112 conn_rtp->rate_ctr_group = rate_ctr_group_alloc(conn, &rate_ctr_group_desc, rate_ctr_index);
Harald Welte3ac604e2019-05-08 14:07:41 +0200113 if (!conn_rtp->rate_ctr_group)
114 return -1;
115
Philipp Maier9e1d1642018-05-09 16:26:34 +0200116 conn_rtp->state.in_stream.err_ts_ctr = &conn_rtp->rate_ctr_group->ctr[IN_STREAM_ERR_TSTMP_CTR];
117 conn_rtp->state.out_stream.err_ts_ctr = &conn_rtp->rate_ctr_group->ctr[OUT_STREAM_ERR_TSTMP_CTR];
118 rate_ctr_index++;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200119
120 /* Make sure codec table is reset */
121 mgcp_codec_reset_all(conn_rtp);
Harald Welte3ac604e2019-05-08 14:07:41 +0200122
123 return 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200124}
125
Philipp Maierd0b470d2018-03-16 12:45:53 +0100126/* Cleanup rtp connection struct */
127static void mgcp_rtp_conn_cleanup(struct mgcp_conn_rtp *conn_rtp)
128{
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200129 if (mgcp_conn_rtp_is_osmux(conn_rtp))
130 conn_osmux_disable(conn_rtp);
Philipp Maierd0b470d2018-03-16 12:45:53 +0100131 mgcp_free_rtp_port(&conn_rtp->end);
Philipp Maier9e1d1642018-05-09 16:26:34 +0200132 rate_ctr_group_free(conn_rtp->rate_ctr_group);
Neels Hofmeyr401b7402019-08-08 22:14:08 +0200133 mgcp_codec_reset_all(conn_rtp);
Philipp Maierd0b470d2018-03-16 12:45:53 +0100134}
135
Oliver Smithe36b7752019-01-22 16:31:36 +0100136void mgcp_conn_watchdog_cb(void *data)
137{
138 struct mgcp_conn *conn = data;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200139 LOGPCONN(conn, DLMGCP, LOGL_ERROR, "connection timed out!\n");
Oliver Smithe36b7752019-01-22 16:31:36 +0100140 mgcp_conn_free(conn->endp, conn->id);
141}
142
143void mgcp_conn_watchdog_kick(struct mgcp_conn *conn)
144{
145 int timeout = conn->endp->cfg->conn_timeout;
146 if (!timeout)
147 return;
148
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200149 LOGPCONN(conn, DLMGCP, LOGL_DEBUG, "watchdog kicked\n");
Oliver Smithe36b7752019-01-22 16:31:36 +0100150 osmo_timer_schedule(&conn->watchdog, timeout, 0);
151}
152
Philipp Maier87bd9be2017-08-22 16:35:41 +0200153/*! allocate a new connection list entry.
154 * \param[in] ctx talloc context
155 * \param[in] endp associated endpoint
156 * \param[in] id identification number of the connection
157 * \param[in] type connection type (e.g. MGCP_CONN_TYPE_RTP)
158 * \returns pointer to allocated connection, NULL on error */
159struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp,
Philipp Maierffd75e42017-11-22 11:44:50 +0100160 enum mgcp_conn_type type, char *name)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200161{
162 struct mgcp_conn *conn;
Philipp Maierffd75e42017-11-22 11:44:50 +0100163 int rc;
164
Philipp Maier87bd9be2017-08-22 16:35:41 +0200165 /* Do not allow more then two connections */
166 if (llist_count(&endp->conns) >= endp->type->max_conns)
167 return NULL;
168
Philipp Maier87bd9be2017-08-22 16:35:41 +0200169 /* Create new connection and add it to the list */
170 conn = talloc_zero(ctx, struct mgcp_conn);
171 if (!conn)
172 return NULL;
173 conn->endp = endp;
174 conn->type = type;
175 conn->mode = MGCP_CONN_NONE;
176 conn->mode_orig = MGCP_CONN_NONE;
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100177 osmo_strlcpy(conn->name, name, sizeof(conn->name));
Philipp Maierffd75e42017-11-22 11:44:50 +0100178 rc = mgcp_alloc_id(endp, conn->id);
179 if (rc < 0) {
180 talloc_free(conn);
181 return NULL;
182 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200183
184 switch (type) {
185 case MGCP_CONN_TYPE_RTP:
Harald Welte3ac604e2019-05-08 14:07:41 +0200186 if (mgcp_rtp_conn_init(&conn->u.rtp, conn) < 0) {
187 talloc_free(conn);
188 return NULL;
189 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200190 break;
191 default:
192 /* NOTE: This should never be called with an
193 * invalid type, its up to the programmer
194 * to ensure propery types */
195 OSMO_ASSERT(false);
196 }
197
Oliver Smithe36b7752019-01-22 16:31:36 +0100198 /* Initialize watchdog */
199 osmo_timer_setup(&conn->watchdog, mgcp_conn_watchdog_cb, conn);
200 mgcp_conn_watchdog_kick(conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200201 llist_add(&conn->entry, &endp->conns);
202
203 return conn;
204}
205
206/*! find a connection by its ID.
207 * \param[in] endp associated endpoint
208 * \param[in] id identification number of the connection
209 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100210struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200211{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200212 struct mgcp_conn *conn;
Neels Hofmeyr65317262018-09-03 22:11:05 +0200213 const char *id_upper;
Neels Hofmeyra77eade2018-08-29 02:30:39 +0200214 const char *conn_id;
Neels Hofmeyr65317262018-09-03 22:11:05 +0200215
216 if (!id || !*id)
217 return NULL;
218
Neels Hofmeyra77eade2018-08-29 02:30:39 +0200219 /* Ignore leading zeros in needle */
220 while (*id == '0')
221 id++;
222
Neels Hofmeyr65317262018-09-03 22:11:05 +0200223 /* Use uppercase to compare identifiers, to avoid mismatches: RFC3435 2.1.3.2 "Names of
224 * Connections" defines the id as a hex string, so clients may return lower case hex even though
225 * we sent upper case hex in the CRCX response. */
226 id_upper = osmo_str_toupper(id);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200227
228 llist_for_each_entry(conn, &endp->conns, entry) {
Neels Hofmeyra77eade2018-08-29 02:30:39 +0200229 /* Ignore leading zeros in haystack */
230 for (conn_id=conn->id; *conn_id == '0'; conn_id++);
231
232 if (strcmp(conn_id, id_upper) == 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200233 return conn;
234 }
235
236 return NULL;
237}
238
239/*! find an RTP connection by its ID.
240 * \param[in] endp associated endpoint
241 * \param[in] id identification number of the connection
242 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100243struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp,
244 const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200245{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200246 struct mgcp_conn *conn;
247
248 conn = mgcp_conn_get(endp, id);
249 if (!conn)
250 return NULL;
251
252 if (conn->type == MGCP_CONN_TYPE_RTP)
253 return &conn->u.rtp;
254
255 return NULL;
256}
257
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200258static void aggregate_rtp_conn_stats(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn_rtp)
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100259{
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200260 struct rate_ctr_group *all_stats = endp->trunk->ratectr.all_rtp_conn_stats;
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100261 struct rate_ctr_group *conn_stats = conn_rtp->rate_ctr_group;
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100262
263 if (all_stats == NULL || conn_stats == NULL)
264 return;
265
266 /* Compared to per-connection RTP statistics, aggregated RTP statistics
267 * contain one additional rate couter item (RTP_NUM_CONNECTIONS).
268 * All other counters in both counter groups correspond to each other. */
269 OSMO_ASSERT(conn_stats->desc->num_ctr + 1 == all_stats->desc->num_ctr);
270
Harald Weltea48ff4a2020-03-08 14:45:08 +0100271 /* all other counters are [now] updated in real-time */
272 rate_ctr_add(&all_stats->ctr[IN_STREAM_ERR_TSTMP_CTR],
273 conn_stats->ctr[IN_STREAM_ERR_TSTMP_CTR].current);
274 rate_ctr_add(&all_stats->ctr[OUT_STREAM_ERR_TSTMP_CTR],
275 conn_stats->ctr[OUT_STREAM_ERR_TSTMP_CTR].current);
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100276
277 rate_ctr_inc(&all_stats->ctr[RTP_NUM_CONNECTIONS]);
278}
279
Philipp Maier87bd9be2017-08-22 16:35:41 +0200280/*! free a connection by its ID.
281 * \param[in] endp associated endpoint
282 * \param[in] id identification number of the connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100283void mgcp_conn_free(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200284{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200285 struct mgcp_conn *conn;
286
287 conn = mgcp_conn_get(endp, id);
288 if (!conn)
289 return;
290
Philipp Maierdf5d2192018-01-24 11:39:32 +0100291 /* Run endpoint cleanup action. By this we inform the endpoint about
292 * the removal of the connection and allow it to clean up its inner
293 * state accordingly */
294 if (endp->type->cleanup_cb)
295 endp->type->cleanup_cb(endp, conn);
296
Philipp Maier87bd9be2017-08-22 16:35:41 +0200297 switch (conn->type) {
298 case MGCP_CONN_TYPE_RTP:
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200299 aggregate_rtp_conn_stats(endp, &conn->u.rtp);
Philipp Maierd0b470d2018-03-16 12:45:53 +0100300 mgcp_rtp_conn_cleanup(&conn->u.rtp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200301 break;
302 default:
303 /* NOTE: This should never be called with an
304 * invalid type, its up to the programmer
305 * to ensure propery types */
306 OSMO_ASSERT(false);
307 }
308
Oliver Smithe36b7752019-01-22 16:31:36 +0100309 osmo_timer_del(&conn->watchdog);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200310 llist_del(&conn->entry);
311 talloc_free(conn);
312}
313
314/*! free oldest connection in the list.
315 * \param[in] endp associated endpoint */
316void mgcp_conn_free_oldest(struct mgcp_endpoint *endp)
317{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200318 struct mgcp_conn *conn;
319
320 if (llist_empty(&endp->conns))
321 return;
322
323 conn = llist_last_entry(&endp->conns, struct mgcp_conn, entry);
324 if (!conn)
325 return;
326
327 mgcp_conn_free(endp, conn->id);
328}
329
330/*! free all connections at once.
331 * \param[in] endp associated endpoint */
332void mgcp_conn_free_all(struct mgcp_endpoint *endp)
333{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200334 struct mgcp_conn *conn;
335 struct mgcp_conn *conn_tmp;
336
337 /* Drop all items in the list */
338 llist_for_each_entry_safe(conn, conn_tmp, &endp->conns, entry) {
339 mgcp_conn_free(endp, conn->id);
340 }
341
342 return;
343}
344
Harald Welte1d1b98f2017-12-25 10:03:40 +0100345/*! dump basic connection information to human readable string.
Philipp Maier87bd9be2017-08-22 16:35:41 +0200346 * \param[in] conn to dump
Harald Welte1d1b98f2017-12-25 10:03:40 +0100347 * \returns human readable string */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200348char *mgcp_conn_dump(struct mgcp_conn *conn)
349{
Philipp Maier01d24a32017-11-21 17:26:09 +0100350 static char str[sizeof(conn->name)+sizeof(conn->id)+256];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200351
352 if (!conn) {
353 snprintf(str, sizeof(str), "(null connection)");
354 return str;
355 }
356
357 switch (conn->type) {
358 case MGCP_CONN_TYPE_RTP:
359 /* Dump RTP connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100360 snprintf(str, sizeof(str), "(%s/rtp, id:0x%s, ip:%s, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200361 "rtp:%u rtcp:%u)",
362 conn->name,
363 conn->id,
364 inet_ntoa(conn->u.rtp.end.addr),
365 ntohs(conn->u.rtp.end.rtp_port),
366 ntohs(conn->u.rtp.end.rtcp_port));
367 break;
368
369 default:
370 /* Should not happen, we should be able to dump
371 * every possible connection type. */
372 snprintf(str, sizeof(str), "(unknown connection type)");
373 break;
374 }
375
376 return str;
377}
378
379/*! find destination connection on a specific endpoint.
380 * \param[in] conn to search a destination for
381 * \returns destination connection, NULL on failure */
382struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn)
383{
384 struct mgcp_endpoint *endp;
385 struct mgcp_conn *partner_conn;
386 endp = conn->endp;
387
388 /*! NOTE: This simply works by grabbing the first connection that is
389 * not the supplied connection, which is suitable for endpoints that
390 * do not serve more than two connections. */
391
392 llist_for_each_entry(partner_conn, &endp->conns, entry) {
393 if (conn != partner_conn) {
394 return partner_conn;
395 }
396 }
397
398 return NULL;
399}
Philipp Maier889fe7f2020-07-06 17:44:12 +0200400
401/*! get oldest connection in the list.
402 * \param[in] endp associated endpoint */
403struct mgcp_conn *mgcp_conn_get_oldest(struct mgcp_endpoint *endp)
404{
405 if (llist_empty(&endp->conns))
406 return NULL;
407
408 return llist_last_entry(&endp->conns, struct mgcp_conn, entry);
409}