blob: 4acf18cf92a9c7af63cf9c684ac6040e6775bdf6 [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
Erica94c56e2021-09-07 00:10:31 +020024#include <stdatomic.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020025#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier993ea6b2020-08-04 18:26:50 +020026#include <osmocom/mgcp/mgcp_network.h>
27#include <osmocom/mgcp/mgcp_protocol.h>
Neels Hofmeyr67793542017-09-08 04:25:16 +020028#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010029#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maierc66ab2c2020-06-02 20:55:34 +020030#include <osmocom/mgcp/mgcp_trunk.h>
Philipp Maierbc0346e2018-06-07 09:52:16 +020031#include <osmocom/mgcp/mgcp_sdp.h>
32#include <osmocom/mgcp/mgcp_codec.h>
Philipp Maierffd75e42017-11-22 11:44:50 +010033#include <osmocom/gsm/gsm_utils.h>
Philipp Maier9e1d1642018-05-09 16:26:34 +020034#include <osmocom/core/rate_ctr.h>
Oliver Smithe36b7752019-01-22 16:31:36 +010035#include <osmocom/core/timer.h>
Philipp Maierffd75e42017-11-22 11:44:50 +010036#include <ctype.h>
37
Philipp Maier9e1d1642018-05-09 16:26:34 +020038const static struct rate_ctr_group_desc rate_ctr_group_desc = {
39 .group_name_prefix = "conn_rtp",
40 .group_description = "rtp connection statistics",
41 .class_id = 1,
Stefan Sperlingba25eab2018-10-30 14:32:31 +010042 .num_ctr = ARRAY_SIZE(mgcp_conn_rate_ctr_desc),
43 .ctr_desc = mgcp_conn_rate_ctr_desc
Philipp Maier9e1d1642018-05-09 16:26:34 +020044};
45
46
Philipp Maierffd75e42017-11-22 11:44:50 +010047/* Allocate a new connection identifier. According to RFC3435, they must
Philipp Maierf8bfbe82017-11-23 19:32:31 +010048 * be unique only within the scope of the endpoint. (Caller must provide
49 * memory for id) */
Philipp Maierffd75e42017-11-22 11:44:50 +010050static int mgcp_alloc_id(struct mgcp_endpoint *endp, char *id)
51{
Neels Hofmeyra729db62018-08-28 16:20:51 +020052#define MGCP_CONN_ID_GEN_LEN 8
Philipp Maierffd75e42017-11-22 11:44:50 +010053 int i;
54 int k;
55 int rc;
Neels Hofmeyra729db62018-08-28 16:20:51 +020056 uint8_t id_bin[MGCP_CONN_ID_GEN_LEN / 2];
Philipp Maierffd75e42017-11-22 11:44:50 +010057 char *id_hex;
58
59 /* Generate a connection id that is unique for the current endpoint.
60 * Technically a counter would be sufficient, but in order to
61 * be able to find a specific connection in large logfiles and to
62 * prevent unintentional connections we assign the connection
63 * identifiers randomly from a reasonable large number space */
64 for (i = 0; i < 32; i++) {
65 rc = osmo_get_rand_id(id_bin, sizeof(id_bin));
66 if (rc < 0)
67 return rc;
68
69 id_hex = osmo_hexdump_nospc(id_bin, sizeof(id_bin));
70 for (k = 0; k < strlen(id_hex); k++)
71 id_hex[k] = toupper(id_hex[k]);
72
73 /* ensure that the generated conn_id is unique
74 * for this endpoint */
75 if (!mgcp_conn_get_rtp(endp, id_hex)) {
Neels Hofmeyr55e0dcf2018-09-03 21:36:56 +020076 osmo_strlcpy(id, id_hex, MGCP_CONN_ID_MAXLEN);
Philipp Maierffd75e42017-11-22 11:44:50 +010077 return 0;
78 }
79 }
80
Pau Espin Pedrol3239f622019-04-24 18:47:46 +020081 LOGPENDP(endp, DLMGCP, LOGL_ERROR, "unable to generate a unique connectionIdentifier\n");
Philipp Maierffd75e42017-11-22 11:44:50 +010082
83 return -1;
84}
Philipp Maier87bd9be2017-08-22 16:35:41 +020085
Philipp Maierc430d192018-03-16 12:11:18 +010086/* Initialize rtp connection struct with default values */
Harald Welte3ac604e2019-05-08 14:07:41 +020087static int mgcp_rtp_conn_init(struct mgcp_conn_rtp *conn_rtp, struct mgcp_conn *conn)
Philipp Maier87bd9be2017-08-22 16:35:41 +020088{
Philipp Maier892dec02018-03-16 12:32:01 +010089 struct mgcp_rtp_end *end = &conn_rtp->end;
Philipp Maier9e1d1642018-05-09 16:26:34 +020090 /* FIXME: Each new rate counter group requires an unique index. At the
91 * moment we generate this index using this counter, but perhaps there
92 * is a more concious way to assign the indexes. */
Erica94c56e2021-09-07 00:10:31 +020093 static atomic_uint rate_ctr_index = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +020094
Philipp Maier892dec02018-03-16 12:32:01 +010095 conn_rtp->type = MGCP_RTP_DEFAULT;
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +020096 conn_rtp->osmux.cid_allocated = false;
97 conn_rtp->osmux.cid = 0;
Philipp Maier892dec02018-03-16 12:32:01 +010098
99 /* backpointer to the generic part of the connection */
100 conn->u.rtp.conn = conn;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200101
102 end->rtp.fd = -1;
103 end->rtcp.fd = -1;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200104 end->rtp_port = end->rtcp_port = 0;
105 talloc_free(end->fmtp_extra);
106 end->fmtp_extra = NULL;
107
108 /* Set default values */
109 end->frames_per_packet = 0; /* unknown */
110 end->packet_duration_ms = DEFAULT_RTP_AUDIO_PACKET_DURATION_MS;
111 end->output_enabled = 0;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200112 end->maximum_packet_time = -1;
Philipp Maier9e1d1642018-05-09 16:26:34 +0200113
Erica94c56e2021-09-07 00:10:31 +0200114 conn_rtp->rate_ctr_group = rate_ctr_group_alloc(conn, &rate_ctr_group_desc, rate_ctr_index++);
Harald Welte3ac604e2019-05-08 14:07:41 +0200115 if (!conn_rtp->rate_ctr_group)
116 return -1;
117
Pau Espin Pedrol907744e2021-06-04 17:57:34 +0200118 conn_rtp->state.in_stream.err_ts_ctr = rate_ctr_group_get_ctr(conn_rtp->rate_ctr_group, IN_STREAM_ERR_TSTMP_CTR);
119 conn_rtp->state.out_stream.err_ts_ctr = rate_ctr_group_get_ctr(conn_rtp->rate_ctr_group, OUT_STREAM_ERR_TSTMP_CTR);
Philipp Maierbc0346e2018-06-07 09:52:16 +0200120
121 /* Make sure codec table is reset */
122 mgcp_codec_reset_all(conn_rtp);
Harald Welte3ac604e2019-05-08 14:07:41 +0200123
124 return 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200125}
126
Philipp Maierd0b470d2018-03-16 12:45:53 +0100127/* Cleanup rtp connection struct */
128static void mgcp_rtp_conn_cleanup(struct mgcp_conn_rtp *conn_rtp)
129{
Pau Espin Pedrol85978da2019-05-17 14:38:19 +0200130 if (mgcp_conn_rtp_is_osmux(conn_rtp))
131 conn_osmux_disable(conn_rtp);
Philipp Maierd0b470d2018-03-16 12:45:53 +0100132 mgcp_free_rtp_port(&conn_rtp->end);
Philipp Maier9e1d1642018-05-09 16:26:34 +0200133 rate_ctr_group_free(conn_rtp->rate_ctr_group);
Neels Hofmeyr401b7402019-08-08 22:14:08 +0200134 mgcp_codec_reset_all(conn_rtp);
Philipp Maierd0b470d2018-03-16 12:45:53 +0100135}
136
Oliver Smithe36b7752019-01-22 16:31:36 +0100137void mgcp_conn_watchdog_cb(void *data)
138{
139 struct mgcp_conn *conn = data;
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200140 LOGPCONN(conn, DLMGCP, LOGL_ERROR, "connection timed out!\n");
Oliver Smithe36b7752019-01-22 16:31:36 +0100141 mgcp_conn_free(conn->endp, conn->id);
142}
143
144void mgcp_conn_watchdog_kick(struct mgcp_conn *conn)
145{
Ericfbf78d12021-08-23 22:31:39 +0200146 int timeout = conn->endp->trunk->cfg->conn_timeout;
Oliver Smithe36b7752019-01-22 16:31:36 +0100147 if (!timeout)
148 return;
149
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200150 LOGPCONN(conn, DLMGCP, LOGL_DEBUG, "watchdog kicked\n");
Oliver Smithe36b7752019-01-22 16:31:36 +0100151 osmo_timer_schedule(&conn->watchdog, timeout, 0);
152}
153
Philipp Maier87bd9be2017-08-22 16:35:41 +0200154/*! allocate a new connection list entry.
155 * \param[in] ctx talloc context
156 * \param[in] endp associated endpoint
157 * \param[in] id identification number of the connection
158 * \param[in] type connection type (e.g. MGCP_CONN_TYPE_RTP)
159 * \returns pointer to allocated connection, NULL on error */
160struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp,
Philipp Maierffd75e42017-11-22 11:44:50 +0100161 enum mgcp_conn_type type, char *name)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200162{
163 struct mgcp_conn *conn;
Philipp Maierffd75e42017-11-22 11:44:50 +0100164 int rc;
165
Philipp Maier87bd9be2017-08-22 16:35:41 +0200166 /* Do not allow more then two connections */
167 if (llist_count(&endp->conns) >= endp->type->max_conns)
168 return NULL;
169
Philipp Maier87bd9be2017-08-22 16:35:41 +0200170 /* Create new connection and add it to the list */
171 conn = talloc_zero(ctx, struct mgcp_conn);
172 if (!conn)
173 return NULL;
174 conn->endp = endp;
175 conn->type = type;
176 conn->mode = MGCP_CONN_NONE;
177 conn->mode_orig = MGCP_CONN_NONE;
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100178 osmo_strlcpy(conn->name, name, sizeof(conn->name));
Philipp Maierffd75e42017-11-22 11:44:50 +0100179 rc = mgcp_alloc_id(endp, conn->id);
180 if (rc < 0) {
181 talloc_free(conn);
182 return NULL;
183 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200184
185 switch (type) {
186 case MGCP_CONN_TYPE_RTP:
Harald Welte3ac604e2019-05-08 14:07:41 +0200187 if (mgcp_rtp_conn_init(&conn->u.rtp, conn) < 0) {
188 talloc_free(conn);
189 return NULL;
190 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200191 break;
192 default:
193 /* NOTE: This should never be called with an
194 * invalid type, its up to the programmer
195 * to ensure propery types */
196 OSMO_ASSERT(false);
197 }
198
Oliver Smithe36b7752019-01-22 16:31:36 +0100199 /* Initialize watchdog */
200 osmo_timer_setup(&conn->watchdog, mgcp_conn_watchdog_cb, conn);
201 mgcp_conn_watchdog_kick(conn);
Pau Espin Pedrol6d0a59a2020-09-08 16:50:22 +0200202 mgcp_endp_add_conn(endp, conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200203
204 return conn;
205}
206
207/*! find a connection by its ID.
208 * \param[in] endp associated endpoint
209 * \param[in] id identification number of the connection
210 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100211struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200212{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200213 struct mgcp_conn *conn;
Neels Hofmeyr65317262018-09-03 22:11:05 +0200214 const char *id_upper;
Neels Hofmeyra77eade2018-08-29 02:30:39 +0200215 const char *conn_id;
Neels Hofmeyr65317262018-09-03 22:11:05 +0200216
217 if (!id || !*id)
218 return NULL;
219
Neels Hofmeyra77eade2018-08-29 02:30:39 +0200220 /* Ignore leading zeros in needle */
221 while (*id == '0')
222 id++;
223
Neels Hofmeyr65317262018-09-03 22:11:05 +0200224 /* Use uppercase to compare identifiers, to avoid mismatches: RFC3435 2.1.3.2 "Names of
225 * Connections" defines the id as a hex string, so clients may return lower case hex even though
226 * we sent upper case hex in the CRCX response. */
227 id_upper = osmo_str_toupper(id);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200228
229 llist_for_each_entry(conn, &endp->conns, entry) {
Neels Hofmeyra77eade2018-08-29 02:30:39 +0200230 /* Ignore leading zeros in haystack */
231 for (conn_id=conn->id; *conn_id == '0'; conn_id++);
232
233 if (strcmp(conn_id, id_upper) == 0)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200234 return conn;
235 }
236
237 return NULL;
238}
239
240/*! find an RTP connection by its ID.
241 * \param[in] endp associated endpoint
242 * \param[in] id identification number of the connection
243 * \returns pointer to allocated connection, NULL if not found */
Philipp Maier01d24a32017-11-21 17:26:09 +0100244struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp,
245 const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200246{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200247 struct mgcp_conn *conn;
248
249 conn = mgcp_conn_get(endp, id);
250 if (!conn)
251 return NULL;
252
253 if (conn->type == MGCP_CONN_TYPE_RTP)
254 return &conn->u.rtp;
255
256 return NULL;
257}
258
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200259static void aggregate_rtp_conn_stats(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn_rtp)
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100260{
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200261 struct rate_ctr_group *all_stats = endp->trunk->ratectr.all_rtp_conn_stats;
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100262 struct rate_ctr_group *conn_stats = conn_rtp->rate_ctr_group;
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100263
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
Harald Weltea48ff4a2020-03-08 14:45:08 +0100272 /* all other counters are [now] updated in real-time */
Pau Espin Pedrol907744e2021-06-04 17:57:34 +0200273 rate_ctr_add(rate_ctr_group_get_ctr(all_stats, IN_STREAM_ERR_TSTMP_CTR),
274 rate_ctr_group_get_ctr(conn_stats, IN_STREAM_ERR_TSTMP_CTR)->current);
275 rate_ctr_add(rate_ctr_group_get_ctr(all_stats, OUT_STREAM_ERR_TSTMP_CTR),
276 rate_ctr_group_get_ctr(conn_stats, OUT_STREAM_ERR_TSTMP_CTR)->current);
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100277
Pau Espin Pedrol907744e2021-06-04 17:57:34 +0200278 rate_ctr_inc(rate_ctr_group_get_ctr(all_stats, RTP_NUM_CONNECTIONS));
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100279}
280
Philipp Maier87bd9be2017-08-22 16:35:41 +0200281/*! free a connection by its ID.
282 * \param[in] endp associated endpoint
283 * \param[in] id identification number of the connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100284void mgcp_conn_free(struct mgcp_endpoint *endp, const char *id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200285{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200286 struct mgcp_conn *conn;
287
288 conn = mgcp_conn_get(endp, id);
289 if (!conn)
290 return;
291
292 switch (conn->type) {
293 case MGCP_CONN_TYPE_RTP:
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200294 aggregate_rtp_conn_stats(endp, &conn->u.rtp);
Philipp Maierd0b470d2018-03-16 12:45:53 +0100295 mgcp_rtp_conn_cleanup(&conn->u.rtp);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200296 break;
297 default:
298 /* NOTE: This should never be called with an
299 * invalid type, its up to the programmer
300 * to ensure propery types */
301 OSMO_ASSERT(false);
302 }
303
Oliver Smithe36b7752019-01-22 16:31:36 +0100304 osmo_timer_del(&conn->watchdog);
Pau Espin Pedrol6d0a59a2020-09-08 16:50:22 +0200305 mgcp_endp_remove_conn(endp, conn);
306 /* WARN: endp may have be freed after call to mgcp_endp_remove_conn */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200307 talloc_free(conn);
308}
309
310/*! free oldest connection in the list.
311 * \param[in] endp associated endpoint */
312void mgcp_conn_free_oldest(struct mgcp_endpoint *endp)
313{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200314 struct mgcp_conn *conn;
315
316 if (llist_empty(&endp->conns))
317 return;
318
319 conn = llist_last_entry(&endp->conns, struct mgcp_conn, entry);
320 if (!conn)
321 return;
322
323 mgcp_conn_free(endp, conn->id);
324}
325
326/*! free all connections at once.
327 * \param[in] endp associated endpoint */
328void mgcp_conn_free_all(struct mgcp_endpoint *endp)
329{
Philipp Maier87bd9be2017-08-22 16:35:41 +0200330 struct mgcp_conn *conn;
331 struct mgcp_conn *conn_tmp;
332
333 /* Drop all items in the list */
334 llist_for_each_entry_safe(conn, conn_tmp, &endp->conns, entry) {
335 mgcp_conn_free(endp, conn->id);
336 }
337
338 return;
339}
340
Harald Welte1d1b98f2017-12-25 10:03:40 +0100341/*! dump basic connection information to human readable string.
Philipp Maier87bd9be2017-08-22 16:35:41 +0200342 * \param[in] conn to dump
Harald Welte1d1b98f2017-12-25 10:03:40 +0100343 * \returns human readable string */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200344char *mgcp_conn_dump(struct mgcp_conn *conn)
345{
Philipp Maier01d24a32017-11-21 17:26:09 +0100346 static char str[sizeof(conn->name)+sizeof(conn->id)+256];
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200347 char ipbuf[INET6_ADDRSTRLEN];
Philipp Maier87bd9be2017-08-22 16:35:41 +0200348
349 if (!conn) {
350 snprintf(str, sizeof(str), "(null connection)");
351 return str;
352 }
353
354 switch (conn->type) {
355 case MGCP_CONN_TYPE_RTP:
356 /* Dump RTP connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100357 snprintf(str, sizeof(str), "(%s/rtp, id:0x%s, ip:%s, "
Philipp Maier87bd9be2017-08-22 16:35:41 +0200358 "rtp:%u rtcp:%u)",
359 conn->name,
360 conn->id,
Pau Espin Pedrola790f0c2020-08-31 13:29:11 +0200361 osmo_sockaddr_ntop(&conn->u.rtp.end.addr.u.sa, ipbuf),
Philipp Maier87bd9be2017-08-22 16:35:41 +0200362 ntohs(conn->u.rtp.end.rtp_port),
363 ntohs(conn->u.rtp.end.rtcp_port));
364 break;
365
366 default:
367 /* Should not happen, we should be able to dump
368 * every possible connection type. */
369 snprintf(str, sizeof(str), "(unknown connection type)");
370 break;
371 }
372
373 return str;
374}
375
376/*! find destination connection on a specific endpoint.
377 * \param[in] conn to search a destination for
378 * \returns destination connection, NULL on failure */
379struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn)
380{
381 struct mgcp_endpoint *endp;
382 struct mgcp_conn *partner_conn;
383 endp = conn->endp;
384
385 /*! NOTE: This simply works by grabbing the first connection that is
386 * not the supplied connection, which is suitable for endpoints that
387 * do not serve more than two connections. */
388
389 llist_for_each_entry(partner_conn, &endp->conns, entry) {
390 if (conn != partner_conn) {
391 return partner_conn;
392 }
393 }
394
395 return NULL;
396}
Philipp Maier889fe7f2020-07-06 17:44:12 +0200397
398/*! get oldest connection in the list.
399 * \param[in] endp associated endpoint */
400struct mgcp_conn *mgcp_conn_get_oldest(struct mgcp_endpoint *endp)
401{
402 if (llist_empty(&endp->conns))
403 return NULL;
404
405 return llist_last_entry(&endp->conns, struct mgcp_conn, entry);
406}