blob: a479fbba36cd2ab0c29207ae57501f33edfac1f2 [file] [log] [blame]
Neels Hofmeyrf83ec562017-09-07 19:18:40 +02001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
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
Philipp Maier87bd9be2017-08-22 16:35:41 +020023#pragma once
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020024
25#include <osmocom/core/msgb.h>
26#include <osmocom/core/write_queue.h>
27#include <osmocom/core/timer.h>
28#include <osmocom/core/logging.h>
29
Neels Hofmeyr67793542017-09-08 04:25:16 +020030#include <osmocom/mgcp/mgcp_common.h>
31
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020032#include <arpa/inet.h>
33#include <sys/types.h>
34#include <sys/socket.h>
35#include <netinet/in.h>
36
Philipp Maier87bd9be2017-08-22 16:35:41 +020037#define RTP_PORT_DEFAULT_RANGE_START 16002
38#define RTP_PORT_DEFAULT_RANGE_END RTP_PORT_DEFAULT_RANGE_START + 64
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020039
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020040/*
41 * Handling of MGCP Endpoints and the MGCP Config
42 */
43struct mgcp_endpoint;
44struct mgcp_config;
45struct mgcp_trunk_config;
46struct mgcp_rtp_end;
47
48#define MGCP_ENDP_CRCX 1
49#define MGCP_ENDP_DLCX 2
50#define MGCP_ENDP_MDCX 3
51
52/*
53 * what to do with the msg?
54 * - continue as usual?
55 * - reject and send a failure code?
56 * - defer? do not send anything
57 */
58#define MGCP_POLICY_CONT 4
59#define MGCP_POLICY_REJECT 5
60#define MGCP_POLICY_DEFER 6
61
62typedef int (*mgcp_realloc)(struct mgcp_trunk_config *cfg, int endpoint);
63typedef int (*mgcp_change)(struct mgcp_trunk_config *cfg, int endpoint, int state);
64typedef int (*mgcp_policy)(struct mgcp_trunk_config *cfg, int endpoint, int state, const char *transactio_id);
65typedef int (*mgcp_reset)(struct mgcp_trunk_config *cfg);
66typedef int (*mgcp_rqnt)(struct mgcp_endpoint *endp, char tone);
67
68/**
69 * Return:
70 * < 0 in case no audio was processed
71 * >= 0 in case audio was processed. The remaining payload
72 * length will be returned.
73 */
74typedef int (*mgcp_processing)(struct mgcp_endpoint *endp,
75 struct mgcp_rtp_end *dst_end,
76 char *data, int *len, int buf_size);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020077
Philipp Maier87bd9be2017-08-22 16:35:41 +020078struct mgcp_conn_rtp;
Philipp Maieracc10352018-07-19 18:07:57 +020079
80typedef int (*mgcp_processing_setup)(struct mgcp_endpoint *endp,
81 struct mgcp_conn_rtp *conn_dst,
82 struct mgcp_conn_rtp *conn_src);
83
Philipp Maier58128252019-03-06 11:28:18 +010084struct mgcp_rtp_codec;
85
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020086typedef void (*mgcp_get_format)(struct mgcp_endpoint *endp,
Philipp Maier58128252019-03-06 11:28:18 +010087 const struct mgcp_rtp_codec **codec,
88 const char **fmtp_extra,
Philipp Maier87bd9be2017-08-22 16:35:41 +020089 struct mgcp_conn_rtp *conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020090
91/**
92 * This holds information on how to allocate ports
93 */
94struct mgcp_port_range {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020095 /* addr or NULL to fall-back to default */
96 char *bind_addr;
97
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020098 /* dynamically allocated */
99 int range_start;
100 int range_end;
101 int last_port;
Philipp Maier1cb1e382017-11-02 17:16:04 +0100102
103 /* set to true to enable automatic probing
104 * of the local bind IP-Address, bind_addr
105 * (or its fall back) is used when automatic
106 * probing fails */
107 bool bind_addr_probe;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200108};
109
Philipp Maiere726d4f2017-11-01 10:41:34 +0100110/* There are up to three modes in which the keep-alive dummy packet can be
Harald Welte1d1b98f2017-12-25 10:03:40 +0100111 * sent. The behaviour is controlled via the keepalive_interval member of the
Philipp Maiere726d4f2017-11-01 10:41:34 +0100112 * trunk config. If that member is set to 0 (MGCP_KEEPALIVE_NEVER) no dummy-
113 * packet is sent at all and the timer that sends regular dummy packets
114 * is no longer scheduled. If the keepalive_interval is set to -1, only
115 * one dummy packet is sent when an CRCX or an MDCX is performed. No timer
Harald Welte1d1b98f2017-12-25 10:03:40 +0100116 * is scheduled. For all vales greater 0, the timer is scheduled and the
Philipp Maiere726d4f2017-11-01 10:41:34 +0100117 * value is used as interval. See also mgcp_keepalive_timer_cb(),
118 * handle_modify_con(), and handle_create_con() */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200119#define MGCP_KEEPALIVE_ONCE (-1)
Philipp Maiere726d4f2017-11-01 10:41:34 +0100120#define MGCP_KEEPALIVE_NEVER 0
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200121
Stefan Sperling1e174872018-10-25 18:36:10 +0200122/* Global MCGP CRCX related rate counters */
123enum {
124 MGCP_CRCX_SUCCESS,
125 MGCP_CRCX_FAIL_BAD_ACTION,
126 MGCP_CRCX_FAIL_UNHANDLED_PARAM,
127 MGCP_CRCX_FAIL_MISSING_CALLID,
128 MGCP_CRCX_FAIL_INVALID_MODE,
129 MGCP_CRCX_FAIL_LIMIT_EXCEEDED,
130 MGCP_CRCX_FAIL_UNKNOWN_CALLID,
131 MGCP_CRCX_FAIL_ALLOC_CONN,
132 MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC,
133 MGCP_CRCX_FAIL_START_RTP,
134 MGCP_CRCX_FAIL_REJECTED_BY_POLICY,
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100135 MGCP_CRCX_FAIL_NO_OSMUX,
136 MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS,
137 MGCP_CRCX_FAIL_CODEC_NEGOTIATION,
138 MGCP_CRCX_FAIL_BIND_PORT,
Stefan Sperling1e174872018-10-25 18:36:10 +0200139};
140
Stefan Sperlingaa823bf2018-10-29 14:51:41 +0100141/* Global MCGP MDCX related rate counters */
142enum {
143 MGCP_MDCX_SUCCESS,
144 MGCP_MDCX_FAIL_WILDCARD,
145 MGCP_MDCX_FAIL_NO_CONN,
146 MGCP_MDCX_FAIL_INVALID_CALLID,
147 MGCP_MDCX_FAIL_INVALID_CONNID,
148 MGCP_MDCX_FAIL_UNHANDLED_PARAM,
149 MGCP_MDCX_FAIL_NO_CONNID,
150 MGCP_MDCX_FAIL_CONN_NOT_FOUND,
151 MGCP_MDCX_FAIL_INVALID_MODE,
152 MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS,
153 MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC,
154 MGCP_MDCX_FAIL_START_RTP,
155 MGCP_MDCX_FAIL_REJECTED_BY_POLICY,
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +0100156 MGCP_MDCX_DEFERRED_BY_POLICY
Stefan Sperlingaa823bf2018-10-29 14:51:41 +0100157};
158
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +0100159/* Global MCGP DLCX related rate counters */
160enum {
161 MGCP_DLCX_SUCCESS,
162 MGCP_DLCX_FAIL_WILDCARD,
163 MGCP_DLCX_FAIL_NO_CONN,
164 MGCP_DLCX_FAIL_INVALID_CALLID,
165 MGCP_DLCX_FAIL_INVALID_CONNID,
166 MGCP_DLCX_FAIL_UNHANDLED_PARAM,
167 MGCP_DLCX_FAIL_REJECTED_BY_POLICY,
168 MGCP_DLCX_DEFERRED_BY_POLICY,
169};
Stefan Sperlingaa823bf2018-10-29 14:51:41 +0100170
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200171struct mgcp_trunk_config {
172 struct llist_head entry;
173
174 struct mgcp_config *cfg;
175
176 int trunk_nr;
177 int trunk_type;
178
179 char *audio_fmtp_extra;
180 char *audio_name;
181 int audio_payload;
182 int audio_send_ptime;
183 int audio_send_name;
184 int audio_loop;
185
186 int no_audio_transcoding;
187
188 int omit_rtcp;
189 int keepalive_interval;
190
191 /* RTP patching */
192 int force_constant_ssrc; /* 0: don't, 1: once */
193 int force_aligned_timing;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100194 bool rfc5993_hr_convert;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200195
196 /* spec handling */
197 int force_realloc;
198
199 /* timer */
200 struct osmo_timer_list keepalive_timer;
201
Philipp Maier87bd9be2017-08-22 16:35:41 +0200202 /* When set, incoming RTP packets are not filtered
203 * when ports and ip-address do not match (debug) */
204 int rtp_accept_all;
205
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200206 unsigned int number_endpoints;
Philipp Maierfcd06552017-11-10 17:32:22 +0100207 int vty_number_endpoints;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200208 struct mgcp_endpoint *endpoints;
Stefan Sperling1e174872018-10-25 18:36:10 +0200209
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100210 /* Rate counter group which contains stats for processed CRCX commands. */
Stefan Sperling1e174872018-10-25 18:36:10 +0200211 struct rate_ctr_group *mgcp_crcx_ctr_group;
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100212 /* Rate counter group which contains stats for processed MDCX commands. */
Stefan Sperlingaa823bf2018-10-29 14:51:41 +0100213 struct rate_ctr_group *mgcp_mdcx_ctr_group;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +0100214 /* Rate counter group which contains stats for processed DLCX commands. */
215 struct rate_ctr_group *mgcp_dlcx_ctr_group;
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100216 /* Rate counter group which aggregates stats of individual RTP connections. */
217 struct rate_ctr_group *all_rtp_conn_stats;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200218};
219
220enum mgcp_role {
221 MGCP_BSC = 0,
222 MGCP_BSC_NAT,
223};
224
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200225struct mgcp_config {
226 int source_port;
227 char *local_ip;
228 char *source_addr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200229 char *call_agent_addr;
230
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200231 /* RTP processing */
232 mgcp_processing rtp_processing_cb;
233 mgcp_processing_setup setup_rtp_processing_cb;
234
235 mgcp_get_format get_net_downlink_format_cb;
236
237 struct osmo_wqueue gw_fd;
238
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200239 struct mgcp_port_range net_ports;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200240 int endp_dscp;
241
Philipp Maier87bd9be2017-08-22 16:35:41 +0200242 int force_ptime;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200243
244 mgcp_change change_cb;
245 mgcp_policy policy_cb;
246 mgcp_reset reset_cb;
247 mgcp_realloc realloc_cb;
248 mgcp_rqnt rqnt_cb;
249 void *data;
250
251 uint32_t last_call_id;
252
253 /* trunk handling */
254 struct mgcp_trunk_config trunk;
255 struct llist_head trunks;
256
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200257 enum mgcp_role role;
258
259 /* osmux translator: 0 means disabled, 1 means enabled */
260 int osmux;
261 /* addr to bind the server to */
262 char *osmux_addr;
263 /* The BSC-NAT may ask for enabling osmux on demand. This tells us if
264 * the osmux socket is already initialized.
265 */
266 int osmux_init;
267 /* osmux batch factor: from 1 to 4 maximum */
268 int osmux_batch;
269 /* osmux batch size (in bytes) */
270 int osmux_batch_size;
271 /* osmux port */
272 uint16_t osmux_port;
273 /* Pad circuit with dummy messages until we see the first voice
274 * message.
275 */
276 uint16_t osmux_dummy;
Philipp Maier12943ea2018-01-17 15:40:25 +0100277 /* domain name of the media gateway */
278 char domain[255+1];
Oliver Smithe36b7752019-01-22 16:31:36 +0100279
280 /* time after which inactive connections (CIs) get closed */
281 int conn_timeout;
Harald Welte9852e222020-03-08 10:22:14 +0100282
283 /* osmocom CTRL interface */
284 struct ctrl_handle *ctrl;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200285};
286
287/* config management */
288struct mgcp_config *mgcp_config_alloc(void);
289int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg,
290 enum mgcp_role role);
291int mgcp_vty_init(void);
292int mgcp_endpoints_allocate(struct mgcp_trunk_config *cfg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200293void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval);
294
295/*
296 * format helper functions
297 */
298struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg);
299
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200300
301int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint);
302int mgcp_send_reset_all(struct mgcp_config *cfg);
303
304
305int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200306int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len);