blob: 3e83bdffdccdc7748f57e3ee8acbf4b0b60f390f [file] [log] [blame]
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001/* MGCP Private Data */
2
3/*
4 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2009-2012 by On-Waves
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#pragma once
24
25#include <string.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020026#include <inttypes.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020027#include <osmocom/core/select.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020028#include <osmocom/mgcp/mgcp.h>
29#include <osmocom/core/linuxlist.h>
Philipp Maierc430d192018-03-16 12:11:18 +010030#include <osmocom/core/counter.h>
Philipp Maier9e1d1642018-05-09 16:26:34 +020031#include <osmocom/core/rate_ctr.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020032
33#define CI_UNUSED 0
34
Philipp Maier01d24a32017-11-21 17:26:09 +010035/* FIXME: This this is only needed to compile the currently
36 * broken OSMUX support. Remove when fixed */
37#define CONN_ID_BTS "0"
38#define CONN_ID_NET "1"
Philipp Maier87bd9be2017-08-22 16:35:41 +020039
Neels Hofmeyr306dc092018-09-30 05:01:20 +020040#define LOG_CONN(conn, level, fmt, args...) \
41 LOGP(DRTP, level, "(%d@ I:%s) " fmt, \
42 ENDPOINT_NUMBER((conn)->endp), (conn)->id, ## args)
43
44#define LOG_CONN_RTP(conn_rtp, level, fmt, args...) \
45 LOG_CONN(conn_rtp->conn, level, fmt, ## args)
46
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020047enum mgcp_trunk_type {
48 MGCP_TRUNK_VIRTUAL,
49 MGCP_TRUNK_E1,
50};
51
52struct mgcp_rtp_stream_state {
53 uint32_t ssrc;
54 uint16_t last_seq;
55 uint32_t last_timestamp;
Philipp Maier9e1d1642018-05-09 16:26:34 +020056 struct rate_ctr *err_ts_ctr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020057 int32_t last_tsdelta;
58 uint32_t last_arrival_time;
59};
60
61struct mgcp_rtp_state {
Harald Welte33381352017-12-25 09:44:26 +010062 /* has this state structure been initialized? */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020063 int initialized;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020064
Harald Welte33381352017-12-25 09:44:26 +010065 struct {
66 /* are we patching the SSRC value? */
67 int patch_ssrc;
68 /* original SSRC (to which we shall patch any different SSRC) */
69 uint32_t orig_ssrc;
70 /* offset to apply on the sequence number */
71 int seq_offset;
72 /* offset to apply on the timestamp number */
73 int32_t timestamp_offset;
74 } patch;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020075
Harald Welte33381352017-12-25 09:44:26 +010076 /* duration of a packet (FIXME: in which unit?) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020077 uint32_t packet_duration;
78
79 struct mgcp_rtp_stream_state in_stream;
80 struct mgcp_rtp_stream_state out_stream;
81
82 /* jitter and packet loss calculation */
Harald Welte49e3d5a2017-12-25 09:47:57 +010083 struct {
84 int initialized;
85 uint16_t base_seq;
86 uint16_t max_seq;
87 uint32_t ssrc;
88 uint32_t jitter;
89 int32_t transit;
90 int cycles;
91 } stats;
92
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020093 bool patched_first_rtp_payload; /* FIXME: drop this, see OS#2459 */
94};
95
96struct mgcp_rtp_codec {
97 uint32_t rate;
98 int channels;
99 uint32_t frame_duration_num;
100 uint32_t frame_duration_den;
101
102 int payload_type;
103 char *audio_name;
104 char *subtype_name;
Philipp Maier228e5912019-03-05 13:56:59 +0100105
106 bool param_present;
107 struct mgcp_codec_param param;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200108};
109
Harald Welte3f35a372017-12-26 15:48:46 +0100110/* 'mgcp_rtp_end': basically a wrapper around the RTP+RTCP ports */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200111struct mgcp_rtp_end {
Harald Welte3f35a372017-12-26 15:48:46 +0100112 /* local IP address of the RTP socket */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200113 struct in_addr addr;
114
115 /* in network byte order */
116 int rtp_port, rtcp_port;
117
Philipp Maierbc0346e2018-06-07 09:52:16 +0200118 /* currently selected audio codec */
119 struct mgcp_rtp_codec *codec;
120
121 /* array with assigned audio codecs to choose from (SDP) */
122 struct mgcp_rtp_codec codecs[MGCP_MAX_CODECS];
123
124 /* number of assigned audio codecs (SDP) */
125 unsigned int codecs_assigned;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200126
127 /* per endpoint data */
128 int frames_per_packet;
129 uint32_t packet_duration_ms;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200130 int maximum_packet_time; /* -1: not set */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200131 char *fmtp_extra;
Harald Welte3f35a372017-12-26 15:48:46 +0100132 /* are we transmitting packets (1) or dropping (0) outbound packets */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200133 int output_enabled;
Harald Welte3f35a372017-12-26 15:48:46 +0100134 /* FIXME: This parameter can be set + printed, but is nowhere used! */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200135 int force_output_ptime;
136
137 /* RTP patching */
138 int force_constant_ssrc; /* -1: always, 0: don't, 1: once */
Harald Welte3f35a372017-12-26 15:48:46 +0100139 /* should we perform align_rtp_timestamp_offset() (1) or not (0) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200140 int force_aligned_timing;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100141 bool rfc5993_hr_convert;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200142
Philipp Maier87bd9be2017-08-22 16:35:41 +0200143 /* Each end has a separate socket for RTP and RTCP */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200144 struct osmo_fd rtp;
145 struct osmo_fd rtcp;
146
Harald Welte3f35a372017-12-26 15:48:46 +0100147 /* local UDP port number of the RTP socket; RTCP is +1 */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200148 int local_port;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200149};
150
151struct mgcp_rtp_tap {
Harald Welte3f35a372017-12-26 15:48:46 +0100152 /* is this tap active (1) or not (0) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200153 int enabled;
Harald Welte3f35a372017-12-26 15:48:46 +0100154 /* IP/port to which we're forwarding the tapped data */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200155 struct sockaddr_in forward;
156};
157
158struct mgcp_lco {
159 char *string;
160 char *codec;
161 int pkt_period_min; /* time in ms */
162 int pkt_period_max; /* time in ms */
163};
164
Philipp Maier87bd9be2017-08-22 16:35:41 +0200165/* Specific rtp connection type (see struct mgcp_conn_rtp) */
166enum mgcp_conn_rtp_type {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200167 MGCP_RTP_DEFAULT = 0,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200168 MGCP_OSMUX_BSC,
169 MGCP_OSMUX_BSC_NAT,
170};
171
Philipp Maier87bd9be2017-08-22 16:35:41 +0200172#include <osmocom/mgcp/osmux.h>
173struct mgcp_conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200174
Philipp Maier87bd9be2017-08-22 16:35:41 +0200175/* MGCP connection (RTP) */
176struct mgcp_conn_rtp {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200177
Philipp Maier87bd9be2017-08-22 16:35:41 +0200178 /* Backpointer to conn struct */
179 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200180
Philipp Maier87bd9be2017-08-22 16:35:41 +0200181 /* Specific connection type */
182 enum mgcp_conn_rtp_type type;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200183
Philipp Maier87bd9be2017-08-22 16:35:41 +0200184 /* Port status */
185 struct mgcp_rtp_end end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200186
Philipp Maier87bd9be2017-08-22 16:35:41 +0200187 /* Sequence bits */
188 struct mgcp_rtp_state state;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200189
Harald Welte3f35a372017-12-26 15:48:46 +0100190 /* taps for the rtp connection; one per direction */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200191 struct mgcp_rtp_tap tap_in;
192 struct mgcp_rtp_tap tap_out;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200193
Philipp Maier87bd9be2017-08-22 16:35:41 +0200194 /* Osmux states (optional) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200195 struct {
196 /* Osmux state: disabled, activating, active */
197 enum osmux_state state;
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200198 /* Is cid holding valid data? is it allocated from pool? */
199 bool cid_allocated;
200 /* Allocated Osmux circuit ID for this conn */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200201 uint8_t cid;
202 /* handle to batch messages */
203 struct osmux_in_handle *in;
204 /* handle to unbatch messages */
205 struct osmux_out_handle out;
206 /* statistics */
207 struct {
208 uint32_t chunks;
209 uint32_t octets;
210 } stats;
211 } osmux;
Philipp Maier9e1d1642018-05-09 16:26:34 +0200212
213 struct rate_ctr_group *rate_ctr_group;
Neels Hofmeyr306dc092018-09-30 05:01:20 +0200214
215 struct osmo_iuup_cn *iuup;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200216};
217
Philipp Maier87bd9be2017-08-22 16:35:41 +0200218/*! Connection type, specifies which member of the union "u" in mgcp_conn
219 * contains a useful connection description (currently only RTP) */
220enum mgcp_conn_type {
221 MGCP_CONN_TYPE_RTP,
222};
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200223
Philipp Maier87bd9be2017-08-22 16:35:41 +0200224/*! MGCP connection (untyped) */
225struct mgcp_conn {
Neels Hofmeyr8838c622018-06-26 00:05:53 +0200226 /*! list head */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200227 struct llist_head entry;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200228
Neels Hofmeyr8838c622018-06-26 00:05:53 +0200229 /*! Backpointer to the endpoint where the conn belongs to */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200230 struct mgcp_endpoint *endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200231
Neels Hofmeyr8838c622018-06-26 00:05:53 +0200232 /*! type of the connection (union) */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200233 enum mgcp_conn_type type;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200234
Neels Hofmeyr8838c622018-06-26 00:05:53 +0200235 /*! mode of the connection */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200236 enum mgcp_connection_mode mode;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200237
Neels Hofmeyr8838c622018-06-26 00:05:53 +0200238 /*! copy of the mode to restore the original setting (VTY) */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200239 enum mgcp_connection_mode mode_orig;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200240
Neels Hofmeyr8838c622018-06-26 00:05:53 +0200241 /*! connection id to identify the connection */
Neels Hofmeyr55e0dcf2018-09-03 21:36:56 +0200242 char id[MGCP_CONN_ID_MAXLEN];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200243
Neels Hofmeyr8838c622018-06-26 00:05:53 +0200244 /*! human readable name (vty, logging) */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200245 char name[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200246
Oliver Smithe36b7752019-01-22 16:31:36 +0100247 /*! activity tracker (for cleaning up inactive connections) */
248 struct osmo_timer_list watchdog;
249
Neels Hofmeyr8838c622018-06-26 00:05:53 +0200250 /*! union with connection description */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200251 union {
252 struct mgcp_conn_rtp rtp;
253 } u;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200254
Neels Hofmeyr8838c622018-06-26 00:05:53 +0200255 /*! pointer to optional private data */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200256 void *priv;
257};
258
259#include <osmocom/mgcp/mgcp_conn.h>
260
261struct mgcp_endpoint_type;
262
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200263
264
Philipp Maierfdd603c2018-02-01 13:31:15 +0100265
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200266
267/**
268 * Internal structure while parsing a request
269 */
270struct mgcp_parse_data {
271 struct mgcp_config *cfg;
272 struct mgcp_endpoint *endp;
273 char *trans;
274 char *save;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200275};
276
Philipp Maier87bd9be2017-08-22 16:35:41 +0200277int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
Neels Hofmeyr306dc092018-09-30 05:01:20 +0200278 struct msgb *msg, struct mgcp_conn_rtp *conn_src,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200279 struct mgcp_conn_rtp *conn_dst);
280int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn);
Neels Hofmeyr306dc092018-09-30 05:01:20 +0200281int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg);
Philipp Maierdf5d2192018-01-24 11:39:32 +0100282void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200283int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
284 struct mgcp_conn_rtp *conn);
285void mgcp_free_rtp_port(struct mgcp_rtp_end *end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200286
287/* For transcoding we need to manage an in and an output that are connected */
288static inline int endp_back_channel(int endpoint)
289{
290 return endpoint + 60;
291}
292
293struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int index);
294struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index);
295
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200296char *get_lco_identifier(const char *options);
297int check_local_cx_options(void *ctx, const char *options);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200298void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
299 struct mgcp_rtp_end *rtp);
300uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
301 struct mgcp_rtp_end *rtp);
302
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200303/* payload processing default functions */
304int mgcp_rtp_processing_default(struct mgcp_endpoint *endp, struct mgcp_rtp_end *dst_end,
305 char *data, int *len, int buf_size);
306
307int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
Philipp Maieracc10352018-07-19 18:07:57 +0200308 struct mgcp_conn_rtp *conn_dst,
309 struct mgcp_conn_rtp *conn_src);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200310
311void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
Philipp Maier58128252019-03-06 11:28:18 +0100312 const struct mgcp_rtp_codec **codec,
313 const char **fmtp_extra,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200314 struct mgcp_conn_rtp *conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200315
316/* internal RTP Annex A counting */
317void mgcp_rtp_annex_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
318 const uint16_t seq, const int32_t transit,
319 const uint32_t ssrc);
320
321int mgcp_set_ip_tos(int fd, int tos);
322
Pau Espin Pedrol497611a2019-05-06 15:41:53 +0200323/* Was conn configured to handle Osmux? */
324static inline bool mgcp_conn_rtp_is_osmux(const struct mgcp_conn_rtp *conn) {
325 return conn->type == MGCP_OSMUX_BSC || conn->type == MGCP_OSMUX_BSC_NAT;
326}
327
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200328enum {
329 MGCP_DEST_NET = 0,
330 MGCP_DEST_BTS,
331};
332
333
334#define MGCP_DUMMY_LOAD 0x23
335
336
337/**
338 * SDP related information
339 */
340/* Assume audio frame length of 20ms */
341#define DEFAULT_RTP_AUDIO_FRAME_DUR_NUM 20
342#define DEFAULT_RTP_AUDIO_FRAME_DUR_DEN 1000
343#define DEFAULT_RTP_AUDIO_PACKET_DURATION_MS 20
344#define DEFAULT_RTP_AUDIO_DEFAULT_RATE 8000
345#define DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS 1
346
347#define PTYPE_UNDEFINED (-1)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200348
Philipp Maier1cb1e382017-11-02 17:16:04 +0100349void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn);
Oliver Smithe36b7752019-01-22 16:31:36 +0100350void mgcp_conn_watchdog_kick(struct mgcp_conn *conn);
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200351
352#define LOGPENDP(endp, cat, level, fmt, args...) \
353LOGP(cat, level, "endpoint:0x%x " fmt, \
Alexander Couzens9d9f44a2019-05-02 17:48:13 +0200354 endp ? ENDPOINT_NUMBER(endp) : -1, \
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200355 ## args)
356
357#define LOGPCONN(conn, cat, level, fmt, args...) \
358LOGPENDP((conn)->endp, cat, level, "CI:%s " fmt, \
359 (conn)->id, \
360 ## args)
Neels Hofmeyr306dc092018-09-30 05:01:20 +0200361
362void mgcp_patch_and_count(struct mgcp_endpoint *endp,
363 struct mgcp_rtp_state *state,
364 struct mgcp_rtp_end *rtp_end,
365 struct sockaddr_in *addr, struct msgb *msg);