blob: c017fafa3c41c2c7a02d3867090089f429f6a75e [file] [log] [blame]
Neels Hofmeyre9920f22017-07-10 15:07:22 +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
23#ifndef OPENBSC_MGCP_H
24#define OPENBSC_MGCP_H
25
26#include <osmocom/core/msgb.h>
27#include <osmocom/core/write_queue.h>
28#include <osmocom/core/timer.h>
29#include <osmocom/core/logging.h>
30
31#include <arpa/inet.h>
32#include <sys/types.h>
33#include <sys/socket.h>
34#include <netinet/in.h>
35
36#define RTP_PORT_DEFAULT 4000
37#define RTP_PORT_NET_DEFAULT 16000
38
39/**
40 * Calculate the RTP audio port for the given multiplex
41 * and the direction. This allows a semi static endpoint
42 * to port calculation removing the need for the BSC
43 * and the MediaGateway to communicate.
44 *
45 * Port usage explained:
46 * base + (multiplex * 2) + 0 == local port to wait for network packets
47 * base + (multiplex * 2) + 1 == local port for rtcp
48 *
49 * The above port will receive packets from the BTS that need
50 * to be patched and forwarded to the network.
51 * The above port will receive packets from the network that
52 * need to be patched and forwarded to the BTS.
53 *
54 * We assume to have a static BTS IP address so we can differentiate
55 * network and BTS.
56 *
57 */
58static inline int rtp_calculate_port(int multiplex, int base)
59{
60 return base + (multiplex * 2);
61}
62
63
64/*
65 * Handling of MGCP Endpoints and the MGCP Config
66 */
67struct mgcp_endpoint;
68struct mgcp_config;
69struct mgcp_trunk_config;
70struct mgcp_rtp_end;
71
72#define MGCP_ENDP_CRCX 1
73#define MGCP_ENDP_DLCX 2
74#define MGCP_ENDP_MDCX 3
75
76/*
77 * what to do with the msg?
78 * - continue as usual?
79 * - reject and send a failure code?
80 * - defer? do not send anything
81 */
82#define MGCP_POLICY_CONT 4
83#define MGCP_POLICY_REJECT 5
84#define MGCP_POLICY_DEFER 6
85
86typedef int (*mgcp_realloc)(struct mgcp_trunk_config *cfg, int endpoint);
87typedef int (*mgcp_change)(struct mgcp_trunk_config *cfg, int endpoint, int state);
88typedef int (*mgcp_policy)(struct mgcp_trunk_config *cfg, int endpoint, int state, const char *transactio_id);
89typedef int (*mgcp_reset)(struct mgcp_trunk_config *cfg);
90typedef int (*mgcp_rqnt)(struct mgcp_endpoint *endp, char tone);
91
92/**
93 * Return:
94 * < 0 in case no audio was processed
95 * >= 0 in case audio was processed. The remaining payload
96 * length will be returned.
97 */
98typedef int (*mgcp_processing)(struct mgcp_endpoint *endp,
99 struct mgcp_rtp_end *dst_end,
100 char *data, int *len, int buf_size);
101typedef int (*mgcp_processing_setup)(struct mgcp_endpoint *endp,
102 struct mgcp_rtp_end *dst_end,
103 struct mgcp_rtp_end *src_end);
104
105typedef void (*mgcp_get_format)(struct mgcp_endpoint *endp,
106 int *payload_type,
107 const char**subtype_name,
108 const char**fmtp_extra);
109
110#define PORT_ALLOC_STATIC 0
111#define PORT_ALLOC_DYNAMIC 1
112
113/**
114 * This holds information on how to allocate ports
115 */
116struct mgcp_port_range {
117 int mode;
118
119 /* addr or NULL to fall-back to default */
120 char *bind_addr;
121
122 /* pre-allocated from a base? */
123 int base_port;
124
125 /* dynamically allocated */
126 int range_start;
127 int range_end;
128 int last_port;
129};
130
131#define MGCP_KEEPALIVE_ONCE (-1)
132
133struct mgcp_trunk_config {
134 struct llist_head entry;
135
136 struct mgcp_config *cfg;
137
138 int trunk_nr;
139 int trunk_type;
140
141 char *audio_fmtp_extra;
142 char *audio_name;
143 int audio_payload;
144 int audio_send_ptime;
145 int audio_send_name;
146 int audio_loop;
147
148 int no_audio_transcoding;
149
150 int omit_rtcp;
151 int keepalive_interval;
152
153 /* RTP patching */
154 int force_constant_ssrc; /* 0: don't, 1: once */
155 int force_aligned_timing;
156
157 /* spec handling */
158 int force_realloc;
159
160 /* timer */
161 struct osmo_timer_list keepalive_timer;
162
163 unsigned int number_endpoints;
164 struct mgcp_endpoint *endpoints;
165};
166
167enum mgcp_role {
168 MGCP_BSC = 0,
169 MGCP_BSC_NAT,
170};
171
172enum mgcp_connection_mode {
173 MGCP_CONN_NONE = 0,
174 MGCP_CONN_RECV_ONLY = 1,
175 MGCP_CONN_SEND_ONLY = 2,
176 MGCP_CONN_RECV_SEND = MGCP_CONN_RECV_ONLY | MGCP_CONN_SEND_ONLY,
177 MGCP_CONN_LOOPBACK = 4 | MGCP_CONN_RECV_SEND,
178};
179
180extern const struct value_string mgcp_connection_mode_strs[];
181
182static inline const char *mgcp_cmode_name(enum mgcp_connection_mode mode)
183{
184 return get_value_string(mgcp_connection_mode_strs, mode);
185}
186
187struct mgcp_config {
188 int source_port;
189 char *local_ip;
190 char *source_addr;
191 char *bts_ip;
192 char *call_agent_addr;
193
194 struct in_addr bts_in;
195
196 /* transcoder handling */
197 char *transcoder_ip;
198 struct in_addr transcoder_in;
199 int transcoder_remote_base;
200
201 /* RTP processing */
202 mgcp_processing rtp_processing_cb;
203 mgcp_processing_setup setup_rtp_processing_cb;
204
205 mgcp_get_format get_net_downlink_format_cb;
206
207 struct osmo_wqueue gw_fd;
208
209 struct mgcp_port_range bts_ports;
210 struct mgcp_port_range net_ports;
211 struct mgcp_port_range transcoder_ports;
212 int endp_dscp;
213
214 int bts_force_ptime;
215
216 mgcp_change change_cb;
217 mgcp_policy policy_cb;
218 mgcp_reset reset_cb;
219 mgcp_realloc realloc_cb;
220 mgcp_rqnt rqnt_cb;
221 void *data;
222
223 uint32_t last_call_id;
224
225 /* trunk handling */
226 struct mgcp_trunk_config trunk;
227 struct llist_head trunks;
228
229 /* only used for start with a static configuration */
230 int last_net_port;
231 int last_bts_port;
232
233 enum mgcp_role role;
234
235 /* osmux translator: 0 means disabled, 1 means enabled */
236 int osmux;
237 /* addr to bind the server to */
238 char *osmux_addr;
239 /* The BSC-NAT may ask for enabling osmux on demand. This tells us if
240 * the osmux socket is already initialized.
241 */
242 int osmux_init;
243 /* osmux batch factor: from 1 to 4 maximum */
244 int osmux_batch;
245 /* osmux batch size (in bytes) */
246 int osmux_batch_size;
247 /* osmux port */
248 uint16_t osmux_port;
249 /* Pad circuit with dummy messages until we see the first voice
250 * message.
251 */
252 uint16_t osmux_dummy;
253};
254
255/* config management */
256struct mgcp_config *mgcp_config_alloc(void);
257int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg,
258 enum mgcp_role role);
259int mgcp_vty_init(void);
260int mgcp_endpoints_allocate(struct mgcp_trunk_config *cfg);
261void mgcp_release_endp(struct mgcp_endpoint *endp);
262void mgcp_initialize_endp(struct mgcp_endpoint *endp);
263int mgcp_reset_transcoder(struct mgcp_config *cfg);
264void mgcp_format_stats(struct mgcp_endpoint *endp, char *stats, size_t size);
265int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os, uint32_t *pr, uint32_t *_or, int *loss, uint32_t *jitter);
266
267void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval);
268
269/*
270 * format helper functions
271 */
272struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg);
273
274/* adc helper */
275static inline int mgcp_timeslot_to_endpoint(int multiplex, int timeslot)
276{
277 if (timeslot == 0) {
278 LOGP(DLMGCP, LOGL_ERROR, "Timeslot should not be 0\n");
279 timeslot = 255;
280 }
281
282 return timeslot + (32 * multiplex);
283}
284
285static inline void mgcp_endpoint_to_timeslot(int endpoint, int *multiplex, int *timeslot)
286{
287 *multiplex = endpoint / 32;
288 *timeslot = endpoint % 32;
289}
290
291int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint);
292int mgcp_send_reset_all(struct mgcp_config *cfg);
293
294
295int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port);
296int mgcp_send(struct mgcp_endpoint *endp, int dest, int is_rtp, struct sockaddr_in *addr, char *buf, int rc);
297int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len);
298
299#endif