blob: b5649050664e4d136e256a33aacd2602774da61a [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 Hofmeyrf83ec562017-09-07 19:18:40 +020040enum mgcp_trunk_type {
41 MGCP_TRUNK_VIRTUAL,
42 MGCP_TRUNK_E1,
43};
44
45struct mgcp_rtp_stream_state {
46 uint32_t ssrc;
47 uint16_t last_seq;
48 uint32_t last_timestamp;
Philipp Maier9e1d1642018-05-09 16:26:34 +020049 struct rate_ctr *err_ts_ctr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020050 int32_t last_tsdelta;
51 uint32_t last_arrival_time;
52};
53
54struct mgcp_rtp_state {
Harald Welte33381352017-12-25 09:44:26 +010055 /* has this state structure been initialized? */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020056 int initialized;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020057
Harald Welte33381352017-12-25 09:44:26 +010058 struct {
59 /* are we patching the SSRC value? */
60 int patch_ssrc;
61 /* original SSRC (to which we shall patch any different SSRC) */
62 uint32_t orig_ssrc;
63 /* offset to apply on the sequence number */
64 int seq_offset;
65 /* offset to apply on the timestamp number */
66 int32_t timestamp_offset;
67 } patch;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020068
Harald Welte33381352017-12-25 09:44:26 +010069 /* duration of a packet (FIXME: in which unit?) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020070 uint32_t packet_duration;
71
72 struct mgcp_rtp_stream_state in_stream;
73 struct mgcp_rtp_stream_state out_stream;
74
75 /* jitter and packet loss calculation */
Harald Welte49e3d5a2017-12-25 09:47:57 +010076 struct {
77 int initialized;
78 uint16_t base_seq;
79 uint16_t max_seq;
80 uint32_t ssrc;
81 uint32_t jitter;
82 int32_t transit;
83 int cycles;
84 } stats;
85
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020086 bool patched_first_rtp_payload; /* FIXME: drop this, see OS#2459 */
87};
88
89struct mgcp_rtp_codec {
90 uint32_t rate;
91 int channels;
92 uint32_t frame_duration_num;
93 uint32_t frame_duration_den;
94
95 int payload_type;
96 char *audio_name;
97 char *subtype_name;
98};
99
Harald Welte3f35a372017-12-26 15:48:46 +0100100/* 'mgcp_rtp_end': basically a wrapper around the RTP+RTCP ports */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200101struct mgcp_rtp_end {
102 /* statistics */
Harald Weltea0ac30f2017-12-25 09:52:30 +0100103 struct {
104 unsigned int packets_rx;
105 unsigned int octets_rx;
106 unsigned int packets_tx;
107 unsigned int octets_tx;
108 unsigned int dropped_packets;
109 } stats;
110
Harald Welte3f35a372017-12-26 15:48:46 +0100111 /* local IP address of the RTP socket */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200112 struct in_addr addr;
113
114 /* in network byte order */
115 int rtp_port, rtcp_port;
116
Philipp Maierbc0346e2018-06-07 09:52:16 +0200117 /* currently selected audio codec */
118 struct mgcp_rtp_codec *codec;
119
120 /* array with assigned audio codecs to choose from (SDP) */
121 struct mgcp_rtp_codec codecs[MGCP_MAX_CODECS];
122
123 /* number of assigned audio codecs (SDP) */
124 unsigned int codecs_assigned;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200125
126 /* per endpoint data */
127 int frames_per_packet;
128 uint32_t packet_duration_ms;
Philipp Maierbc0346e2018-06-07 09:52:16 +0200129 int maximum_packet_time; /* -1: not set */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200130 char *fmtp_extra;
Harald Welte3f35a372017-12-26 15:48:46 +0100131 /* are we transmitting packets (1) or dropping (0) outbound packets */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200132 int output_enabled;
Harald Welte3f35a372017-12-26 15:48:46 +0100133 /* FIXME: This parameter can be set + printed, but is nowhere used! */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200134 int force_output_ptime;
135
136 /* RTP patching */
137 int force_constant_ssrc; /* -1: always, 0: don't, 1: once */
Harald Welte3f35a372017-12-26 15:48:46 +0100138 /* should we perform align_rtp_timestamp_offset() (1) or not (0) */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200139 int force_aligned_timing;
Harald Welte3f35a372017-12-26 15:48:46 +0100140 /* FIXME: not used anymore, used to be [external] transcoding related */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200141 void *rtp_process_data;
142
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;
198 /* Allocated Osmux circuit ID for this endpoint */
199 int allocated_cid;
200 /* Used Osmux circuit ID for this endpoint */
201 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 Hofmeyrf83ec562017-09-07 19:18:40 +0200214};
215
Philipp Maier87bd9be2017-08-22 16:35:41 +0200216/*! Connection type, specifies which member of the union "u" in mgcp_conn
217 * contains a useful connection description (currently only RTP) */
218enum mgcp_conn_type {
219 MGCP_CONN_TYPE_RTP,
220};
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200221
Philipp Maier87bd9be2017-08-22 16:35:41 +0200222/*! MGCP connection (untyped) */
223struct mgcp_conn {
224 /*!< list head */
225 struct llist_head entry;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200226
Philipp Maier87bd9be2017-08-22 16:35:41 +0200227 /*!< Backpointer to the endpoint where the conn belongs to */
228 struct mgcp_endpoint *endp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200229
Philipp Maier87bd9be2017-08-22 16:35:41 +0200230 /*!< type of the connection (union) */
231 enum mgcp_conn_type type;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200232
Philipp Maier87bd9be2017-08-22 16:35:41 +0200233 /*!< mode of the connection */
234 enum mgcp_connection_mode mode;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200235
Philipp Maier87bd9be2017-08-22 16:35:41 +0200236 /*!< copy of the mode to restore the original setting (VTY) */
237 enum mgcp_connection_mode mode_orig;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200238
Harald Welte1d1b98f2017-12-25 10:03:40 +0100239 /*!< connection id to identify the connection */
Philipp Maier01d24a32017-11-21 17:26:09 +0100240 char id[MGCP_CONN_ID_LENGTH];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200241
Philipp Maier87bd9be2017-08-22 16:35:41 +0200242 /*!< human readable name (vty, logging) */
243 char name[256];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200244
Philipp Maier87bd9be2017-08-22 16:35:41 +0200245 /*!< union with connection description */
246 union {
247 struct mgcp_conn_rtp rtp;
248 } u;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200249
Philipp Maier87bd9be2017-08-22 16:35:41 +0200250 /*!< pointer to optional private data */
251 void *priv;
252};
253
254#include <osmocom/mgcp/mgcp_conn.h>
255
256struct mgcp_endpoint_type;
257
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200258
259
Philipp Maierfdd603c2018-02-01 13:31:15 +0100260
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200261
262/**
263 * Internal structure while parsing a request
264 */
265struct mgcp_parse_data {
266 struct mgcp_config *cfg;
267 struct mgcp_endpoint *endp;
268 char *trans;
269 char *save;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200270};
271
Philipp Maier87bd9be2017-08-22 16:35:41 +0200272int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
273 char *buf, int rc, struct mgcp_conn_rtp *conn_src,
274 struct mgcp_conn_rtp *conn_dst);
275int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn);
276int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, char *buf,
277 unsigned int buf_size, struct mgcp_conn *conn);
Philipp Maierdf5d2192018-01-24 11:39:32 +0100278void mgcp_cleanup_rtp_bridge_cb(struct mgcp_endpoint *endp, struct mgcp_conn *conn);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200279int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
280 struct mgcp_conn_rtp *conn);
281void mgcp_free_rtp_port(struct mgcp_rtp_end *end);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200282
283/* For transcoding we need to manage an in and an output that are connected */
284static inline int endp_back_channel(int endpoint)
285{
286 return endpoint + 60;
287}
288
289struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int index);
290struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index);
291
Philipp Maier3d7b58d2018-06-06 09:35:31 +0200292char *get_lco_identifier(const char *options);
293int check_local_cx_options(void *ctx, const char *options);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200294void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
295 struct mgcp_rtp_end *rtp);
296uint32_t mgcp_rtp_packet_duration(struct mgcp_endpoint *endp,
297 struct mgcp_rtp_end *rtp);
298
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200299/* payload processing default functions */
300int mgcp_rtp_processing_default(struct mgcp_endpoint *endp, struct mgcp_rtp_end *dst_end,
301 char *data, int *len, int buf_size);
302
303int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
304 struct mgcp_rtp_end *dst_end,
305 struct mgcp_rtp_end *src_end);
306
307void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
308 int *payload_type,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200309 const char**audio_name,
310 const char**fmtp_extra,
311 struct mgcp_conn_rtp *conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200312
313/* internal RTP Annex A counting */
314void mgcp_rtp_annex_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
315 const uint16_t seq, const int32_t transit,
316 const uint32_t ssrc);
317
318int mgcp_set_ip_tos(int fd, int tos);
319
320enum {
321 MGCP_DEST_NET = 0,
322 MGCP_DEST_BTS,
323};
324
325
326#define MGCP_DUMMY_LOAD 0x23
327
328
329/**
330 * SDP related information
331 */
332/* Assume audio frame length of 20ms */
333#define DEFAULT_RTP_AUDIO_FRAME_DUR_NUM 20
334#define DEFAULT_RTP_AUDIO_FRAME_DUR_DEN 1000
335#define DEFAULT_RTP_AUDIO_PACKET_DURATION_MS 20
336#define DEFAULT_RTP_AUDIO_DEFAULT_RATE 8000
337#define DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS 1
338
339#define PTYPE_UNDEFINED (-1)
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200340
Philipp Maier1cb1e382017-11-02 17:16:04 +0100341void mgcp_get_local_addr(char *addr, struct mgcp_conn_rtp *conn);