blob: f9f0ac7a785c5424a700389a4298dd89d033b180 [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
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020084typedef void (*mgcp_get_format)(struct mgcp_endpoint *endp,
85 int *payload_type,
86 const char**subtype_name,
Philipp Maier87bd9be2017-08-22 16:35:41 +020087 const char**fmtp_extra,
88 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
Stefan Sperling1e174872018-10-25 18:36:10 +0200121/* Global MCGP CRCX related rate counters */
122enum {
123 MGCP_CRCX_SUCCESS,
124 MGCP_CRCX_FAIL_BAD_ACTION,
125 MGCP_CRCX_FAIL_UNHANDLED_PARAM,
126 MGCP_CRCX_FAIL_MISSING_CALLID,
127 MGCP_CRCX_FAIL_INVALID_MODE,
128 MGCP_CRCX_FAIL_LIMIT_EXCEEDED,
129 MGCP_CRCX_FAIL_UNKNOWN_CALLID,
130 MGCP_CRCX_FAIL_ALLOC_CONN,
131 MGCP_CRCX_FAIL_NO_REMOTE_CONN_DESC,
132 MGCP_CRCX_FAIL_START_RTP,
133 MGCP_CRCX_FAIL_REJECTED_BY_POLICY,
134};
135
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200136struct mgcp_trunk_config {
137 struct llist_head entry;
138
139 struct mgcp_config *cfg;
140
141 int trunk_nr;
142 int trunk_type;
143
144 char *audio_fmtp_extra;
145 char *audio_name;
146 int audio_payload;
147 int audio_send_ptime;
148 int audio_send_name;
149 int audio_loop;
150
151 int no_audio_transcoding;
152
153 int omit_rtcp;
154 int keepalive_interval;
155
156 /* RTP patching */
157 int force_constant_ssrc; /* 0: don't, 1: once */
158 int force_aligned_timing;
159
160 /* spec handling */
161 int force_realloc;
162
163 /* timer */
164 struct osmo_timer_list keepalive_timer;
165
Philipp Maier87bd9be2017-08-22 16:35:41 +0200166 /* When set, incoming RTP packets are not filtered
167 * when ports and ip-address do not match (debug) */
168 int rtp_accept_all;
169
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200170 unsigned int number_endpoints;
Philipp Maierfcd06552017-11-10 17:32:22 +0100171 int vty_number_endpoints;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200172 struct mgcp_endpoint *endpoints;
Stefan Sperling1e174872018-10-25 18:36:10 +0200173
174 /* rate counters */
175 struct rate_ctr_group *mgcp_crcx_ctr_group;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200176};
177
178enum mgcp_role {
179 MGCP_BSC = 0,
180 MGCP_BSC_NAT,
181};
182
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200183struct mgcp_config {
184 int source_port;
185 char *local_ip;
186 char *source_addr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200187 char *call_agent_addr;
188
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200189 /* RTP processing */
190 mgcp_processing rtp_processing_cb;
191 mgcp_processing_setup setup_rtp_processing_cb;
192
193 mgcp_get_format get_net_downlink_format_cb;
194
195 struct osmo_wqueue gw_fd;
196
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200197 struct mgcp_port_range net_ports;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200198 int endp_dscp;
199
Philipp Maier87bd9be2017-08-22 16:35:41 +0200200 int force_ptime;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200201
202 mgcp_change change_cb;
203 mgcp_policy policy_cb;
204 mgcp_reset reset_cb;
205 mgcp_realloc realloc_cb;
206 mgcp_rqnt rqnt_cb;
207 void *data;
208
209 uint32_t last_call_id;
210
211 /* trunk handling */
212 struct mgcp_trunk_config trunk;
213 struct llist_head trunks;
214
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200215 enum mgcp_role role;
216
217 /* osmux translator: 0 means disabled, 1 means enabled */
218 int osmux;
219 /* addr to bind the server to */
220 char *osmux_addr;
221 /* The BSC-NAT may ask for enabling osmux on demand. This tells us if
222 * the osmux socket is already initialized.
223 */
224 int osmux_init;
225 /* osmux batch factor: from 1 to 4 maximum */
226 int osmux_batch;
227 /* osmux batch size (in bytes) */
228 int osmux_batch_size;
229 /* osmux port */
230 uint16_t osmux_port;
231 /* Pad circuit with dummy messages until we see the first voice
232 * message.
233 */
234 uint16_t osmux_dummy;
Philipp Maier12943ea2018-01-17 15:40:25 +0100235 /* domain name of the media gateway */
236 char domain[255+1];
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200237};
238
239/* config management */
240struct mgcp_config *mgcp_config_alloc(void);
241int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg,
242 enum mgcp_role role);
243int mgcp_vty_init(void);
244int mgcp_endpoints_allocate(struct mgcp_trunk_config *cfg);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200245void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval);
246
247/*
248 * format helper functions
249 */
250struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg);
251
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200252
253int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint);
254int mgcp_send_reset_all(struct mgcp_config *cfg);
255
256
257int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200258int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len);