blob: 67c499bb0d623b10ed0a8e342e420a354d003582 [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>
Philipp Maier87bd9be2017-08-22 16:35:41 +020029#include <osmocom/mgcp/mgcp_ep.h>
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020030
31#include <arpa/inet.h>
32#include <sys/types.h>
33#include <sys/socket.h>
34#include <netinet/in.h>
35
Philipp Maier87bd9be2017-08-22 16:35:41 +020036#define RTP_PORT_DEFAULT_RANGE_START 16002
37#define RTP_PORT_DEFAULT_RANGE_END RTP_PORT_DEFAULT_RANGE_START + 64
Neels Hofmeyrf83ec562017-09-07 19:18:40 +020038
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
Philipp Maier87bd9be2017-08-22 16:35:41 +0200105struct mgcp_conn_rtp;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200106typedef void (*mgcp_get_format)(struct mgcp_endpoint *endp,
107 int *payload_type,
108 const char**subtype_name,
Philipp Maier87bd9be2017-08-22 16:35:41 +0200109 const char**fmtp_extra,
110 struct mgcp_conn_rtp *conn);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200111
112/**
113 * This holds information on how to allocate ports
114 */
115struct mgcp_port_range {
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200116 /* addr or NULL to fall-back to default */
117 char *bind_addr;
118
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200119 /* dynamically allocated */
120 int range_start;
121 int range_end;
122 int last_port;
123};
124
125#define MGCP_KEEPALIVE_ONCE (-1)
126
127struct mgcp_trunk_config {
128 struct llist_head entry;
129
130 struct mgcp_config *cfg;
131
132 int trunk_nr;
133 int trunk_type;
134
135 char *audio_fmtp_extra;
136 char *audio_name;
137 int audio_payload;
138 int audio_send_ptime;
139 int audio_send_name;
140 int audio_loop;
141
142 int no_audio_transcoding;
143
144 int omit_rtcp;
145 int keepalive_interval;
146
147 /* RTP patching */
148 int force_constant_ssrc; /* 0: don't, 1: once */
149 int force_aligned_timing;
150
151 /* spec handling */
152 int force_realloc;
153
154 /* timer */
155 struct osmo_timer_list keepalive_timer;
156
Philipp Maier87bd9be2017-08-22 16:35:41 +0200157 /* When set, incoming RTP packets are not filtered
158 * when ports and ip-address do not match (debug) */
159 int rtp_accept_all;
160
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200161 unsigned int number_endpoints;
162 struct mgcp_endpoint *endpoints;
163};
164
165enum mgcp_role {
166 MGCP_BSC = 0,
167 MGCP_BSC_NAT,
168};
169
170enum mgcp_connection_mode {
171 MGCP_CONN_NONE = 0,
172 MGCP_CONN_RECV_ONLY = 1,
173 MGCP_CONN_SEND_ONLY = 2,
174 MGCP_CONN_RECV_SEND = MGCP_CONN_RECV_ONLY | MGCP_CONN_SEND_ONLY,
175 MGCP_CONN_LOOPBACK = 4 | MGCP_CONN_RECV_SEND,
176};
177
178extern const struct value_string mgcp_connection_mode_strs[];
179
180static inline const char *mgcp_cmode_name(enum mgcp_connection_mode mode)
181{
182 return get_value_string(mgcp_connection_mode_strs, mode);
183}
184
185struct mgcp_config {
186 int source_port;
187 char *local_ip;
188 char *source_addr;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200189 char *call_agent_addr;
190
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200191 /* RTP processing */
192 mgcp_processing rtp_processing_cb;
193 mgcp_processing_setup setup_rtp_processing_cb;
194
195 mgcp_get_format get_net_downlink_format_cb;
196
197 struct osmo_wqueue gw_fd;
198
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200199 struct mgcp_port_range net_ports;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200200 int endp_dscp;
201
Philipp Maier87bd9be2017-08-22 16:35:41 +0200202 int force_ptime;
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200203
204 mgcp_change change_cb;
205 mgcp_policy policy_cb;
206 mgcp_reset reset_cb;
207 mgcp_realloc realloc_cb;
208 mgcp_rqnt rqnt_cb;
209 void *data;
210
211 uint32_t last_call_id;
212
213 /* trunk handling */
214 struct mgcp_trunk_config trunk;
215 struct llist_head trunks;
216
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200217 enum mgcp_role role;
218
219 /* osmux translator: 0 means disabled, 1 means enabled */
220 int osmux;
221 /* addr to bind the server to */
222 char *osmux_addr;
223 /* The BSC-NAT may ask for enabling osmux on demand. This tells us if
224 * the osmux socket is already initialized.
225 */
226 int osmux_init;
227 /* osmux batch factor: from 1 to 4 maximum */
228 int osmux_batch;
229 /* osmux batch size (in bytes) */
230 int osmux_batch_size;
231 /* osmux port */
232 uint16_t osmux_port;
233 /* Pad circuit with dummy messages until we see the first voice
234 * message.
235 */
236 uint16_t osmux_dummy;
237};
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);
245void mgcp_release_endp(struct mgcp_endpoint *endp);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200246void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval);
247
248/*
249 * format helper functions
250 */
251struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg);
252
253/* adc helper */
254static inline int mgcp_timeslot_to_endpoint(int multiplex, int timeslot)
255{
256 if (timeslot == 0) {
257 LOGP(DLMGCP, LOGL_ERROR, "Timeslot should not be 0\n");
258 timeslot = 255;
259 }
260
261 return timeslot + (32 * multiplex);
262}
263
264static inline void mgcp_endpoint_to_timeslot(int endpoint, int *multiplex, int *timeslot)
265{
266 *multiplex = endpoint / 32;
267 *timeslot = endpoint % 32;
268}
269
270int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint);
271int mgcp_send_reset_all(struct mgcp_config *cfg);
272
273
274int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port);
Neels Hofmeyrf83ec562017-09-07 19:18:40 +0200275int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len);