blob: 3d7224e8080afdfff01ebd5baab1707f45f1e835 [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
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +020035/* 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"
39
40#define LOG_CONN(conn, level, fmt, args...) \
41 LOGP(DRTP, level, "(%s I:%s) " fmt, \
42 (conn)->endp ? (conn)->endp->name : "none", (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 +020047struct mgcp_rtp_stream_state {
48 uint32_t ssrc;
49 uint16_t last_seq;
50 uint32_t last_timestamp;
Philipp Maier9e1d1642018-05-09 16:26:34 +020051 struct rate_ctr *err_ts_ctr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020052 int32_t last_tsdelta;
53 uint32_t last_arrival_time;
54};
55
56struct mgcp_rtp_state {
Harald Welte33381352017-12-25 09:44:26 +010057 /* has this state structure been initialized? */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020058 int initialized;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020059
Harald Welte33381352017-12-25 09:44:26 +010060 struct {
61 /* are we patching the SSRC value? */
62 int patch_ssrc;
63 /* original SSRC (to which we shall patch any different SSRC) */
64 uint32_t orig_ssrc;
65 /* offset to apply on the sequence number */
66 int seq_offset;
67 /* offset to apply on the timestamp number */
68 int32_t timestamp_offset;
69 } patch;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020070
Harald Welte33381352017-12-25 09:44:26 +010071 /* duration of a packet (FIXME: in which unit?) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020072 uint32_t packet_duration;
73
74 struct mgcp_rtp_stream_state in_stream;
75 struct mgcp_rtp_stream_state out_stream;
76
77 /* jitter and packet loss calculation */
Harald Welte49e3d5a2017-12-25 09:47:57 +010078 struct {
79 int initialized;
80 uint16_t base_seq;
81 uint16_t max_seq;
82 uint32_t ssrc;
83 uint32_t jitter;
84 int32_t transit;
85 int cycles;
86 } stats;
87
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020088 bool patched_first_rtp_payload; /* FIXME: drop this, see OS#2459 */
89};
90
91struct mgcp_rtp_codec {
92 uint32_t rate;
93 int channels;
94 uint32_t frame_duration_num;
95 uint32_t frame_duration_den;
96
97 int payload_type;
98 char *audio_name;
99 char *subtype_name;
Philipp Maier228e5912019-03-05 13:56:59 +0100100
101 bool param_present;
102 struct mgcp_codec_param param;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200103};
104
Harald Welte3f35a372017-12-26 15:48:46 +0100105/* 'mgcp_rtp_end': basically a wrapper around the RTP+RTCP ports */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200106struct mgcp_rtp_end {
Harald Welte3f35a372017-12-26 15:48:46 +0100107 /* local IP address of the RTP socket */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200108 struct in_addr addr;
109
110 /* in network byte order */
111 int rtp_port, rtcp_port;
112
Philipp Maierbc0346e2018-06-07 09:52:16 +0200113 /* currently selected audio codec */
114 struct mgcp_rtp_codec *codec;
115
116 /* array with assigned audio codecs to choose from (SDP) */
117 struct mgcp_rtp_codec codecs[MGCP_MAX_CODECS];
118
119 /* number of assigned audio codecs (SDP) */
120 unsigned int codecs_assigned;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200121
122 /* per endpoint data */
123 int frames_per_packet;
124 uint32_t packet_duration_ms;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200125 int maximum_packet_time; /* -1: not set */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200126 char *fmtp_extra;
Harald Welte3f35a372017-12-26 15:48:46 +0100127 /* are we transmitting packets (1) or dropping (0) outbound packets */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200128 int output_enabled;
Harald Welte3f35a372017-12-26 15:48:46 +0100129 /* FIXME: This parameter can be set + printed, but is nowhere used! */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200130 int force_output_ptime;
131
132 /* RTP patching */
133 int force_constant_ssrc; /* -1: always, 0: don't, 1: once */
Harald Welte3f35a372017-12-26 15:48:46 +0100134 /* should we perform align_rtp_timestamp_offset() (1) or not (0) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200135 int force_aligned_timing;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100136 bool rfc5993_hr_convert;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200137
Philipp Maier87bd9be2017-08-22 16:35:41 +0200138 /* Each end has a separate socket for RTP and RTCP */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200139 struct osmo_fd rtp;
140 struct osmo_fd rtcp;
141
Harald Welte3f35a372017-12-26 15:48:46 +0100142 /* local UDP port number of the RTP socket; RTCP is +1 */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200143 int local_port;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200144};
145
146struct mgcp_rtp_tap {
Harald Welte3f35a372017-12-26 15:48:46 +0100147 /* is this tap active (1) or not (0) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200148 int enabled;
Harald Welte3f35a372017-12-26 15:48:46 +0100149 /* IP/port to which we're forwarding the tapped data */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200150 struct sockaddr_in forward;
151};
152
153struct mgcp_lco {
154 char *string;
155 char *codec;
156 int pkt_period_min; /* time in ms */
157 int pkt_period_max; /* time in ms */
158};
159
Philipp Maier87bd9be2017-08-22 16:35:41 +0200160/* Specific rtp connection type (see struct mgcp_conn_rtp) */
161enum mgcp_conn_rtp_type {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200162 MGCP_RTP_DEFAULT = 0,
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200163 MGCP_OSMUX_BSC,
164 MGCP_OSMUX_BSC_NAT,
165};
166
Philipp Maier87bd9be2017-08-22 16:35:41 +0200167#include <osmocom/mgcp/osmux.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200168
Philipp Maier87bd9be2017-08-22 16:35:41 +0200169/* MGCP connection (RTP) */
170struct mgcp_conn_rtp {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200171
Philipp Maier87bd9be2017-08-22 16:35:41 +0200172 /* Backpointer to conn struct */
173 struct mgcp_conn *conn;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200174
Philipp Maier87bd9be2017-08-22 16:35:41 +0200175 /* Specific connection type */
176 enum mgcp_conn_rtp_type type;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200177
Philipp Maier87bd9be2017-08-22 16:35:41 +0200178 /* Port status */
179 struct mgcp_rtp_end end;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200180
Philipp Maier87bd9be2017-08-22 16:35:41 +0200181 /* Sequence bits */
182 struct mgcp_rtp_state state;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200183
Harald Welte3f35a372017-12-26 15:48:46 +0100184 /* taps for the rtp connection; one per direction */
Philipp Maier87bd9be2017-08-22 16:35:41 +0200185 struct mgcp_rtp_tap tap_in;
186 struct mgcp_rtp_tap tap_out;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200187
Philipp Maier87bd9be2017-08-22 16:35:41 +0200188 /* Osmux states (optional) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200189 struct {
190 /* Osmux state: disabled, activating, active */
191 enum osmux_state state;
Pau Espin Pedrol8de58e72019-04-24 13:33:46 +0200192 /* Is cid holding valid data? is it allocated from pool? */
193 bool cid_allocated;
194 /* Allocated Osmux circuit ID for this conn */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200195 uint8_t cid;
196 /* handle to batch messages */
197 struct osmux_in_handle *in;
198 /* handle to unbatch messages */
199 struct osmux_out_handle out;
200 /* statistics */
201 struct {
202 uint32_t chunks;
203 uint32_t octets;
204 } stats;
205 } osmux;
Philipp Maier9e1d1642018-05-09 16:26:34 +0200206
207 struct rate_ctr_group *rate_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200208};
209
Philipp Maier87bd9be2017-08-22 16:35:41 +0200210#include <osmocom/mgcp/mgcp_conn.h>
211
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200212/**
213 * Internal structure while parsing a request
214 */
215struct mgcp_parse_data {
216 struct mgcp_config *cfg;
217 struct mgcp_endpoint *endp;
218 char *trans;
219 char *save;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200220};
221
Philipp Maier87bd9be2017-08-22 16:35:41 +0200222int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200223 struct msgb *msg, struct mgcp_conn_rtp *conn_src,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200224 struct mgcp_conn_rtp *conn_dst);
225int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200226int mgcp_dispatch_rtp_bridge_cb(struct msgb *msg);
Philipp Maierdf5d2192018-01-24 11:39:32 +0100227void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200228int mgcp_dispatch_e1_bridge_cb(struct msgb *msg);
Philipp Maier0996a1e2020-06-10 15:27:14 +0200229void mgcp_cleanup_e1_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200230int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
231 struct mgcp_conn_rtp *conn);
232void mgcp_free_rtp_port(struct mgcp_rtp_end *end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200233
234/* For transcoding we need to manage an in and an output that are connected */
235static inline int endp_back_channel(int endpoint)
236{
237 return endpoint + 60;
238}
239
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200240char *get_lco_identifier(const char *options);
241int check_local_cx_options(void *ctx, const char *options);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200242void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
243 struct mgcp_rtp_end *rtp);
244uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
245 struct mgcp_rtp_end *rtp);
246
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200247/* payload processing default functions */
248int mgcp_rtp_processing_default(struct mgcp_endpoint *endp, struct mgcp_rtp_end *dst_end,
249 char *data, int *len, int buf_size);
250
251int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
Philipp Maieracc10352018-07-19 18:07:57 +0200252 struct mgcp_conn_rtp *conn_dst,
253 struct mgcp_conn_rtp *conn_src);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200254
255void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
Philipp Maier58128252019-03-06 11:28:18 +0100256 const struct mgcp_rtp_codec **codec,
257 const char **fmtp_extra,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200258 struct mgcp_conn_rtp *conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200259
260/* internal RTP Annex A counting */
261void mgcp_rtp_annex_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
262 const uint16_t seq, const int32_t transit,
263 const uint32_t ssrc);
264
265int mgcp_set_ip_tos(int fd, int tos);
266
Pau Espin Pedrol497611a2019-05-06 15:41:53 +0200267/* Was conn configured to handle Osmux? */
268static inline bool mgcp_conn_rtp_is_osmux(const struct mgcp_conn_rtp *conn) {
269 return conn->type == MGCP_OSMUX_BSC || conn->type == MGCP_OSMUX_BSC_NAT;
270}
271
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200272enum {
273 MGCP_DEST_NET = 0,
274 MGCP_DEST_BTS,
275};
276
277
278#define MGCP_DUMMY_LOAD 0x23
279
280
281/**
282 * SDP related information
283 */
284/* Assume audio frame length of 20ms */
285#define DEFAULT_RTP_AUDIO_FRAME_DUR_NUM 20
286#define DEFAULT_RTP_AUDIO_FRAME_DUR_DEN 1000
287#define DEFAULT_RTP_AUDIO_PACKET_DURATION_MS 20
288#define DEFAULT_RTP_AUDIO_DEFAULT_RATE 8000
289#define DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS 1
290
291#define PTYPE_UNDEFINED (-1)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200292
Philipp Maier1cb1e382017-11-02 17:16:04 +0100293void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn);
Oliver Smithe36b7752019-01-22 16:31:36 +0100294void mgcp_conn_watchdog_kick(struct mgcp_conn *conn);
Neels Hofmeyr51b42ff2020-06-19 01:34:42 +0200295void mgcp_patch_and_count(struct mgcp_endpoint *endp,
296 struct mgcp_rtp_state *state,
297 struct mgcp_rtp_end *rtp_end,
298 struct sockaddr_in *addr, struct msgb *msg);