blob: 538c90750b606a139240eb209c8bf07a097c69fe [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;
Philipp Maier14b27a82020-06-02 20:15:30 +020045struct mgcp_trunk;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020046struct 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
Philipp Maier14b27a82020-06-02 20:15:30 +020062typedef int (*mgcp_change)(struct mgcp_trunk *cfg, int endpoint, int state);
63typedef int (*mgcp_policy)(struct mgcp_trunk *cfg, int endpoint, int state, const char *transactio_id);
64typedef int (*mgcp_reset)(struct mgcp_trunk *cfg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020065typedef int (*mgcp_rqnt)(struct mgcp_endpoint *endp, char tone);
66
67/**
68 * Return:
69 * < 0 in case no audio was processed
70 * >= 0 in case audio was processed. The remaining payload
71 * length will be returned.
72 */
73typedef int (*mgcp_processing)(struct mgcp_endpoint *endp,
74 struct mgcp_rtp_end *dst_end,
75 char *data, int *len, int buf_size);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020076
Philipp Maier87bd9be2017-08-22 16:35:41 +020077struct mgcp_conn_rtp;
Philipp Maieracc10352018-07-19 18:07:57 +020078
79typedef int (*mgcp_processing_setup)(struct mgcp_endpoint *endp,
80 struct mgcp_conn_rtp *conn_dst,
81 struct mgcp_conn_rtp *conn_src);
82
Philipp Maier58128252019-03-06 11:28:18 +010083struct mgcp_rtp_codec;
84
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020085typedef void (*mgcp_get_format)(struct mgcp_endpoint *endp,
Philipp Maier58128252019-03-06 11:28:18 +010086 const struct mgcp_rtp_codec **codec,
87 const char **fmtp_extra,
Philipp Maier87bd9be2017-08-22 16:35:41 +020088 struct mgcp_conn_rtp *conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020089
90/**
91 * This holds information on how to allocate ports
92 */
93struct mgcp_port_range {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020094 /* addr or NULL to fall-back to default */
95 char *bind_addr;
96
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020097 /* dynamically allocated */
98 int range_start;
99 int range_end;
100 int last_port;
Philipp Maier1cb1e382017-11-02 17:16:04 +0100101
102 /* set to true to enable automatic probing
103 * of the local bind IP-Address, bind_addr
104 * (or its fall back) is used when automatic
105 * probing fails */
106 bool bind_addr_probe;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200107};
108
Philipp Maiere726d4f2017-11-01 10:41:34 +0100109/* There are up to three modes in which the keep-alive dummy packet can be
Harald Welte1d1b98f2017-12-25 10:03:40 +0100110 * sent. The behaviour is controlled via the keepalive_interval member of the
Philipp Maiere726d4f2017-11-01 10:41:34 +0100111 * trunk config. If that member is set to 0 (MGCP_KEEPALIVE_NEVER) no dummy-
112 * packet is sent at all and the timer that sends regular dummy packets
113 * is no longer scheduled. If the keepalive_interval is set to -1, only
114 * one dummy packet is sent when an CRCX or an MDCX is performed. No timer
Harald Welte1d1b98f2017-12-25 10:03:40 +0100115 * is scheduled. For all vales greater 0, the timer is scheduled and the
Philipp Maiere726d4f2017-11-01 10:41:34 +0100116 * value is used as interval. See also mgcp_keepalive_timer_cb(),
117 * handle_modify_con(), and handle_create_con() */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200118#define MGCP_KEEPALIVE_ONCE (-1)
Philipp Maiere726d4f2017-11-01 10:41:34 +0100119#define MGCP_KEEPALIVE_NEVER 0
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200120
Alexander Chemeris63866002020-05-05 17:18:40 +0300121/* Global MCGP general rate counters */
122enum {
123 MGCP_GENERAL_RX_MSGS_TOTAL,
124 MGCP_GENERAL_RX_MSGS_RETRANSMITTED,
125 MGCP_GENERAL_RX_MSGS_HANDLED,
126 MGCP_GENERAL_RX_MSGS_UNHANDLED,
127 MGCP_GENERAL_RX_FAIL_MSG_PARSE,
128 MGCP_GENERAL_RX_FAIL_NO_ENDPOINT,
129};
130
Stefan Sperling1e174872018-10-25 18:36:10 +0200131/* Global MCGP CRCX related rate counters */
132enum {
133 MGCP_CRCX_SUCCESS,
134 MGCP_CRCX_FAIL_BAD_ACTION,
135 MGCP_CRCX_FAIL_UNHANDLED_PARAM,
136 MGCP_CRCX_FAIL_MISSING_CALLID,
137 MGCP_CRCX_FAIL_INVALID_MODE,
138 MGCP_CRCX_FAIL_LIMIT_EXCEEDED,
139 MGCP_CRCX_FAIL_UNKNOWN_CALLID,
140 MGCP_CRCX_FAIL_ALLOC_CONN,
141 MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC,
142 MGCP_CRCX_FAIL_START_RTP,
143 MGCP_CRCX_FAIL_REJECTED_BY_POLICY,
Stefan Sperlinga714abf2018-10-29 14:19:54 +0100144 MGCP_CRCX_FAIL_NO_OSMUX,
145 MGCP_CRCX_FAIL_INVALID_CONN_OPTIONS,
146 MGCP_CRCX_FAIL_CODEC_NEGOTIATION,
147 MGCP_CRCX_FAIL_BIND_PORT,
Stefan Sperling1e174872018-10-25 18:36:10 +0200148};
149
Stefan Sperlingaa823bf2018-10-29 14:51:41 +0100150/* Global MCGP MDCX related rate counters */
151enum {
152 MGCP_MDCX_SUCCESS,
153 MGCP_MDCX_FAIL_WILDCARD,
154 MGCP_MDCX_FAIL_NO_CONN,
155 MGCP_MDCX_FAIL_INVALID_CALLID,
156 MGCP_MDCX_FAIL_INVALID_CONNID,
157 MGCP_MDCX_FAIL_UNHANDLED_PARAM,
158 MGCP_MDCX_FAIL_NO_CONNID,
159 MGCP_MDCX_FAIL_CONN_NOT_FOUND,
160 MGCP_MDCX_FAIL_INVALID_MODE,
161 MGCP_MDCX_FAIL_INVALID_CONN_OPTIONS,
162 MGCP_MDCX_FAIL_NO_REMOTE_CONN_DESC,
163 MGCP_MDCX_FAIL_START_RTP,
164 MGCP_MDCX_FAIL_REJECTED_BY_POLICY,
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +0100165 MGCP_MDCX_DEFERRED_BY_POLICY
Stefan Sperlingaa823bf2018-10-29 14:51:41 +0100166};
167
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +0100168/* Global MCGP DLCX related rate counters */
169enum {
170 MGCP_DLCX_SUCCESS,
171 MGCP_DLCX_FAIL_WILDCARD,
172 MGCP_DLCX_FAIL_NO_CONN,
173 MGCP_DLCX_FAIL_INVALID_CALLID,
174 MGCP_DLCX_FAIL_INVALID_CONNID,
175 MGCP_DLCX_FAIL_UNHANDLED_PARAM,
176 MGCP_DLCX_FAIL_REJECTED_BY_POLICY,
177 MGCP_DLCX_DEFERRED_BY_POLICY,
178};
Stefan Sperlingaa823bf2018-10-29 14:51:41 +0100179
Philipp Maier14b27a82020-06-02 20:15:30 +0200180struct mgcp_trunk {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200181 struct llist_head entry;
182
183 struct mgcp_config *cfg;
184
185 int trunk_nr;
186 int trunk_type;
187
188 char *audio_fmtp_extra;
189 char *audio_name;
190 int audio_payload;
191 int audio_send_ptime;
192 int audio_send_name;
193 int audio_loop;
194
195 int no_audio_transcoding;
196
197 int omit_rtcp;
198 int keepalive_interval;
199
200 /* RTP patching */
201 int force_constant_ssrc; /* 0: don't, 1: once */
202 int force_aligned_timing;
Philipp Maier9fc8a022019-02-20 12:26:52 +0100203 bool rfc5993_hr_convert;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200204
205 /* spec handling */
206 int force_realloc;
207
208 /* timer */
209 struct osmo_timer_list keepalive_timer;
210
Philipp Maier87bd9be2017-08-22 16:35:41 +0200211 /* When set, incoming RTP packets are not filtered
212 * when ports and ip-address do not match (debug) */
213 int rtp_accept_all;
214
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200215 unsigned int number_endpoints;
Philipp Maierfcd06552017-11-10 17:32:22 +0100216 int vty_number_endpoints;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200217 struct mgcp_endpoint *endpoints;
Stefan Sperling1e174872018-10-25 18:36:10 +0200218
Alexander Chemeris63866002020-05-05 17:18:40 +0300219 /* Rate counter group which contains stats for generic MGCP events. */
220 struct rate_ctr_group *mgcp_general_ctr_group;
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100221 /* Rate counter group which contains stats for processed CRCX commands. */
Stefan Sperling1e174872018-10-25 18:36:10 +0200222 struct rate_ctr_group *mgcp_crcx_ctr_group;
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100223 /* Rate counter group which contains stats for processed MDCX commands. */
Stefan Sperlingaa823bf2018-10-29 14:51:41 +0100224 struct rate_ctr_group *mgcp_mdcx_ctr_group;
Stefan Sperling8ab3fbb2018-10-30 14:57:25 +0100225 /* Rate counter group which contains stats for processed DLCX commands. */
226 struct rate_ctr_group *mgcp_dlcx_ctr_group;
Stefan Sperlingba25eab2018-10-30 14:32:31 +0100227 /* Rate counter group which aggregates stats of individual RTP connections. */
228 struct rate_ctr_group *all_rtp_conn_stats;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200229};
230
231enum mgcp_role {
232 MGCP_BSC = 0,
233 MGCP_BSC_NAT,
234};
235
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200236struct mgcp_config {
237 int source_port;
238 char *local_ip;
239 char *source_addr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200240 char *call_agent_addr;
241
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200242 /* RTP processing */
243 mgcp_processing rtp_processing_cb;
244 mgcp_processing_setup setup_rtp_processing_cb;
245
246 mgcp_get_format get_net_downlink_format_cb;
247
248 struct osmo_wqueue gw_fd;
249
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200250 struct mgcp_port_range net_ports;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200251 int endp_dscp;
252
Philipp Maier87bd9be2017-08-22 16:35:41 +0200253 int force_ptime;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200254
255 mgcp_change change_cb;
256 mgcp_policy policy_cb;
257 mgcp_reset reset_cb;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200258 mgcp_rqnt rqnt_cb;
259 void *data;
260
261 uint32_t last_call_id;
262
263 /* trunk handling */
Harald Weltec39b1bf2020-03-08 11:29:39 +0100264
265 /* virtual trunk for RTP - RTP endpoints */
Philipp Maier14b27a82020-06-02 20:15:30 +0200266 struct mgcp_trunk *virt_trunk;
Harald Weltec39b1bf2020-03-08 11:29:39 +0100267 /* physical trunks with underlying E1 endpoints */
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200268 struct llist_head trunks;
269
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200270 enum mgcp_role role;
271
272 /* osmux translator: 0 means disabled, 1 means enabled */
273 int osmux;
274 /* addr to bind the server to */
275 char *osmux_addr;
276 /* The BSC-NAT may ask for enabling osmux on demand. This tells us if
277 * the osmux socket is already initialized.
278 */
279 int osmux_init;
280 /* osmux batch factor: from 1 to 4 maximum */
281 int osmux_batch;
282 /* osmux batch size (in bytes) */
283 int osmux_batch_size;
284 /* osmux port */
285 uint16_t osmux_port;
286 /* Pad circuit with dummy messages until we see the first voice
287 * message.
288 */
289 uint16_t osmux_dummy;
Philipp Maier12943ea2018-01-17 15:40:25 +0100290 /* domain name of the media gateway */
291 char domain[255+1];
Oliver Smithe36b7752019-01-22 16:31:36 +0100292
293 /* time after which inactive connections (CIs) get closed */
294 int conn_timeout;
Harald Welte9852e222020-03-08 10:22:14 +0100295
296 /* osmocom CTRL interface */
297 struct ctrl_handle *ctrl;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200298};
299
300/* config management */
301struct mgcp_config *mgcp_config_alloc(void);
302int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg,
303 enum mgcp_role role);
304int mgcp_vty_init(void);
Philipp Maier14b27a82020-06-02 20:15:30 +0200305int mgcp_endpoints_allocate(struct mgcp_trunk *cfg);
306void mgcp_trunk_set_keepalive(struct mgcp_trunk *trunk, int interval);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200307
308/*
309 * format helper functions
310 */
311struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg);
312
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200313
314int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint);
315int mgcp_send_reset_all(struct mgcp_config *cfg);
316
317
318int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200319int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len);