blob: 7cf83b27cfdd04cb222dedca827bd7b2bc2230f3 [file] [log] [blame]
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2
3/*
Holger Hans Peter Freyther8d0be252012-11-29 12:54:22 +01004 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2009-2012 by On-Waves
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +02006 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01009 * 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
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +020011 * (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
Harald Welte9af6ddf2011-01-01 15:25:50 +010016 * GNU Affero General Public License for more details.
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +020017 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010018 * 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/>.
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +020020 *
21 */
22
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +010023#ifndef OPENBSC_MGCP_H
24#define OPENBSC_MGCP_H
25
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010026#include <osmocom/core/msgb.h>
27#include <osmocom/core/write_queue.h>
Jacob Erlbeck075a9eb2013-12-19 18:53:07 +010028#include <osmocom/core/timer.h>
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +010029
Holger Hans Peter Freythere3946f42010-09-20 01:07:23 +080030#include "debug.h"
31
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010032#include <arpa/inet.h>
Holger Hans Peter Freyther46720592015-08-01 23:35:19 +000033#include <sys/types.h>
34#include <sys/socket.h>
35#include <netinet/in.h>
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010036
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010037#define RTP_PORT_DEFAULT 4000
Holger Hans Peter Freyther314584a2010-08-05 04:10:21 +080038#define RTP_PORT_NET_DEFAULT 16000
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +020039
40/**
41 * Calculate the RTP audio port for the given multiplex
42 * and the direction. This allows a semi static endpoint
43 * to port calculation removing the need for the BSC
44 * and the MediaGateway to communicate.
45 *
46 * Port usage explained:
47 * base + (multiplex * 2) + 0 == local port to wait for network packets
48 * base + (multiplex * 2) + 1 == local port for rtcp
49 *
50 * The above port will receive packets from the BTS that need
51 * to be patched and forwarded to the network.
52 * The above port will receive packets from the network that
53 * need to be patched and forwarded to the BTS.
54 *
55 * We assume to have a static BTS IP address so we can differentiate
56 * network and BTS.
57 *
58 */
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010059static inline int rtp_calculate_port(int multiplex, int base)
Holger Hans Peter Freytherf67945f2009-10-09 07:08:11 +020060{
61 return base + (multiplex * 2);
62}
Holger Hans Peter Freythere0955022010-02-03 08:50:33 +010063
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +010064
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010065/*
66 * Handling of MGCP Endpoints and the MGCP Config
67 */
68struct mgcp_endpoint;
69struct mgcp_config;
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +010070struct mgcp_trunk_config;
Jacob Erlbecka0d64ce2014-03-13 14:24:52 +010071struct mgcp_rtp_end;
Holger Hans Peter Freyther77f7afe2010-02-03 09:54:43 +010072
Holger Hans Peter Freyther77f7afe2010-02-03 09:54:43 +010073#define MGCP_ENDP_CRCX 1
74#define MGCP_ENDP_DLCX 2
75#define MGCP_ENDP_MDCX 3
76
Holger Hans Peter Freytherfe86d3c2010-02-26 13:37:05 +010077/*
78 * what to do with the msg?
79 * - continue as usual?
80 * - reject and send a failure code?
81 * - defer? do not send anything
82 */
83#define MGCP_POLICY_CONT 4
84#define MGCP_POLICY_REJECT 5
85#define MGCP_POLICY_DEFER 6
86
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +010087typedef int (*mgcp_realloc)(struct mgcp_trunk_config *cfg, int endpoint);
88typedef int (*mgcp_change)(struct mgcp_trunk_config *cfg, int endpoint, int state);
89typedef int (*mgcp_policy)(struct mgcp_trunk_config *cfg, int endpoint, int state, const char *transactio_id);
Holger Hans Peter Freyther74db7742011-06-10 00:04:55 +020090typedef int (*mgcp_reset)(struct mgcp_trunk_config *cfg);
Holger Hans Peter Freytherce553612012-12-07 15:04:07 +010091typedef int (*mgcp_rqnt)(struct mgcp_endpoint *endp, char tone);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010092
Holger Hans Peter Freytherbd4109b2014-06-27 19:27:38 +020093/**
94 * Return:
95 * < 0 in case no audio was processed
96 * >= 0 in case audio was processed. The remaining payload
97 * length will be returned.
98 */
Jacob Erlbeck42a833e2014-04-14 10:31:47 +020099typedef int (*mgcp_processing)(struct mgcp_endpoint *endp,
100 struct mgcp_rtp_end *dst_end,
Jacob Erlbecka0d64ce2014-03-13 14:24:52 +0100101 char *data, int *len, int buf_size);
102typedef int (*mgcp_processing_setup)(struct mgcp_endpoint *endp,
103 struct mgcp_rtp_end *dst_end,
104 struct mgcp_rtp_end *src_end);
Jacob Erlbeck168ca002014-03-17 12:40:07 +0100105
106typedef void (*mgcp_get_format)(struct mgcp_endpoint *endp,
107 int *payload_type,
108 const char**subtype_name,
109 const char**fmtp_extra);
110
Holger Hans Peter Freyther15e73892010-08-05 07:10:56 +0800111#define PORT_ALLOC_STATIC 0
112#define PORT_ALLOC_DYNAMIC 1
113
114/**
115 * This holds information on how to allocate ports
116 */
117struct mgcp_port_range {
118 int mode;
119
Holger Hans Peter Freytherc390ae82015-08-20 15:15:50 +0200120 /* addr or NULL to fall-back to default */
121 char *bind_addr;
122
Holger Hans Peter Freyther15e73892010-08-05 07:10:56 +0800123 /* pre-allocated from a base? */
124 int base_port;
Holger Hans Peter Freyther1be9f2f2010-08-05 07:20:09 +0800125
126 /* dynamically allocated */
127 int range_start;
128 int range_end;
129 int last_port;
Holger Hans Peter Freyther15e73892010-08-05 07:10:56 +0800130};
131
Jacob Erlbeck075a9eb2013-12-19 18:53:07 +0100132#define MGCP_KEEPALIVE_ONCE (-1)
133
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100134struct mgcp_trunk_config {
Holger Hans Peter Freyther0e939fe2011-02-28 12:11:02 +0100135 struct llist_head entry;
136
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100137 struct mgcp_config *cfg;
138
139 int trunk_nr;
140 int trunk_type;
141
Holger Hans Peter Freyther5ea1bc72012-09-03 00:07:39 +0200142 char *audio_fmtp_extra;
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100143 char *audio_name;
144 int audio_payload;
Jacob Erlbeck0a1bc562013-12-10 13:09:37 +0100145 int audio_send_ptime;
Holger Hans Peter Freyther619b0142014-11-19 16:04:45 +0100146 int audio_send_name;
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100147 int audio_loop;
148
Holger Hans Peter Freythercb43a9a2015-04-24 16:03:55 -0400149 int no_audio_transcoding;
150
Holger Hans Peter Freythera8090d52012-05-11 13:00:45 +0200151 int omit_rtcp;
Jacob Erlbeck075a9eb2013-12-19 18:53:07 +0100152 int keepalive_interval;
Holger Hans Peter Freythera8090d52012-05-11 13:00:45 +0200153
Jacob Erlbeckdb2d4312013-12-03 14:43:34 +0100154 /* RTP patching */
155 int force_constant_ssrc; /* 0: don't, 1: once */
Jacob Erlbeck4bbddc62013-12-18 12:54:51 +0100156 int force_aligned_timing;
Jacob Erlbeckdb2d4312013-12-03 14:43:34 +0100157
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100158 /* spec handling */
159 int force_realloc;
160
Jacob Erlbeck075a9eb2013-12-19 18:53:07 +0100161 /* timer */
162 struct osmo_timer_list keepalive_timer;
163
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100164 unsigned int number_endpoints;
165 struct mgcp_endpoint *endpoints;
166};
167
Pablo Neira Ayuso326b5d82013-08-02 21:14:14 +0200168enum mgcp_role {
169 MGCP_BSC = 0,
170 MGCP_BSC_NAT,
171};
172
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200173enum mgcp_connection_mode {
174 MGCP_CONN_NONE = 0,
175 MGCP_CONN_RECV_ONLY = 1,
176 MGCP_CONN_SEND_ONLY = 2,
177 MGCP_CONN_RECV_SEND = MGCP_CONN_RECV_ONLY | MGCP_CONN_SEND_ONLY,
178 MGCP_CONN_LOOPBACK = 4 | MGCP_CONN_RECV_SEND,
179};
180
181extern const struct value_string mgcp_connection_mode_strs[];
182
183static inline const char *mgcp_cmode_name(enum mgcp_connection_mode mode)
184{
185 return get_value_string(mgcp_connection_mode_strs, mode);
186}
187
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100188struct mgcp_config {
189 int source_port;
190 char *local_ip;
191 char *source_addr;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100192 char *bts_ip;
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +0200193 char *call_agent_addr;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100194
195 struct in_addr bts_in;
Holger Hans Peter Freyther15e73892010-08-05 07:10:56 +0800196
Holger Hans Peter Freytherb98ba722010-09-19 04:21:39 +0800197 /* transcoder handling */
198 char *transcoder_ip;
199 struct in_addr transcoder_in;
200 int transcoder_remote_base;
201
Jacob Erlbecka0d64ce2014-03-13 14:24:52 +0100202 /* RTP processing */
203 mgcp_processing rtp_processing_cb;
204 mgcp_processing_setup setup_rtp_processing_cb;
205
Jacob Erlbeck168ca002014-03-17 12:40:07 +0100206 mgcp_get_format get_net_downlink_format_cb;
207
Pablo Neira Ayusoe1273b12011-05-06 12:09:47 +0200208 struct osmo_wqueue gw_fd;
Holger Hans Peter Freyther6f680102010-09-18 20:40:22 +0800209
Holger Hans Peter Freyther15e73892010-08-05 07:10:56 +0800210 struct mgcp_port_range bts_ports;
211 struct mgcp_port_range net_ports;
Holger Hans Peter Freyther54aaa0f2010-09-17 23:35:53 +0800212 struct mgcp_port_range transcoder_ports;
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +0800213 int endp_dscp;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100214
Jacob Erlbeck42a833e2014-04-14 10:31:47 +0200215 int bts_force_ptime;
216
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100217 mgcp_change change_cb;
Holger Hans Peter Freytherfe86d3c2010-02-26 13:37:05 +0100218 mgcp_policy policy_cb;
Holger Hans Peter Freyther9bdcc9c2010-03-31 06:39:35 +0200219 mgcp_reset reset_cb;
Holger Hans Peter Freyther869e38e2010-08-06 17:54:27 +0800220 mgcp_realloc realloc_cb;
Holger Hans Peter Freyther8d0be252012-11-29 12:54:22 +0100221 mgcp_rqnt rqnt_cb;
Holger Hans Peter Freytherfe86d3c2010-02-26 13:37:05 +0100222 void *data;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100223
Holger Hans Peter Freyther46340132010-08-06 08:26:54 +0800224 uint32_t last_call_id;
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100225
226 /* trunk handling */
227 struct mgcp_trunk_config trunk;
Holger Hans Peter Freyther0e939fe2011-02-28 12:11:02 +0100228 struct llist_head trunks;
Holger Hans Peter Freyther44016fe2011-02-28 14:46:01 +0100229
230 /* only used for start with a static configuration */
231 int last_net_port;
232 int last_bts_port;
Pablo Neira Ayuso326b5d82013-08-02 21:14:14 +0200233
234 enum mgcp_role role;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100235
236 /* osmux translator: 0 means disabled, 1 means enabled */
237 int osmux;
Holger Hans Peter Freythera777c9e2015-10-12 20:06:16 +0200238 /* addr to bind the server to */
239 char *osmux_addr;
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100240 /* The BSC-NAT may ask for enabling osmux on demand. This tells us if
241 * the osmux socket is already initialized.
242 */
243 int osmux_init;
244 /* osmux batch factor: from 1 to 4 maximum */
245 int osmux_batch;
Pablo Neira Ayuso03ab79a2014-08-29 12:30:38 +0200246 /* osmux batch size (in bytes) */
247 int osmux_batch_size;
Pablo Neira Ayuso0fe78d32014-08-28 16:43:38 +0200248 /* osmux port */
249 uint16_t osmux_port;
Pablo Neira Ayuso57e95a22015-07-17 21:56:23 +0200250 /* Pad circuit with dummy messages until we see the first voice
251 * message.
252 */
253 uint16_t osmux_dummy;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100254};
255
256/* config management */
257struct mgcp_config *mgcp_config_alloc(void);
Pablo Neira Ayuso326b5d82013-08-02 21:14:14 +0200258int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg,
259 enum mgcp_role role);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100260int mgcp_vty_init(void);
Holger Hans Peter Freyther1f0c5b42011-02-28 14:37:03 +0100261int mgcp_endpoints_allocate(struct mgcp_trunk_config *cfg);
Holger Hans Peter Freythercb6ad702014-07-22 15:00:52 +0200262void mgcp_release_endp(struct mgcp_endpoint *endp);
263void mgcp_initialize_endp(struct mgcp_endpoint *endp);
Holger Hans Peter Freytherf2eedff2010-09-19 04:36:07 +0800264int mgcp_reset_transcoder(struct mgcp_config *cfg);
Holger Hans Peter Freyther0bf15a82012-09-14 17:18:12 +0200265void mgcp_format_stats(struct mgcp_endpoint *endp, char *stats, size_t size);
Holger Hans Peter Freyther462b7d72012-10-24 21:53:40 +0200266int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os, uint32_t *pr, uint32_t *_or, int *loss, uint32_t *jitter);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100267
Jacob Erlbeck075a9eb2013-12-19 18:53:07 +0100268void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval);
269
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100270/*
271 * format helper functions
272 */
273struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100274
Holger Hans Peter Freyther616d2222010-03-31 09:27:04 +0200275/* adc helper */
276static inline int mgcp_timeslot_to_endpoint(int multiplex, int timeslot)
277{
Holger Hans Peter Freythere3946f42010-09-20 01:07:23 +0800278 if (timeslot == 0) {
279 LOGP(DMGCP, LOGL_ERROR, "Timeslot should not be 0\n");
280 timeslot = 255;
281 }
282
Holger Hans Peter Freythercb8c35c2010-08-10 20:24:24 +0800283 return timeslot + (32 * multiplex);
Holger Hans Peter Freyther616d2222010-03-31 09:27:04 +0200284}
285
Holger Hans Peter Freyther82049d82010-08-28 17:59:15 +0800286static inline void mgcp_endpoint_to_timeslot(int endpoint, int *multiplex, int *timeslot)
287{
288 *multiplex = endpoint / 32;
289 *timeslot = endpoint % 32;
Holger Hans Peter Freyther82049d82010-08-28 17:59:15 +0800290}
291
Harald Welte94b499c2012-01-27 00:46:49 +0100292int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint);
293int mgcp_send_reset_all(struct mgcp_config *cfg);
294
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100295
Pablo Neira Ayusocab6e752014-02-05 18:56:17 +0100296int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port);
297int mgcp_send(struct mgcp_endpoint *endp, int dest, int is_rtp, struct sockaddr_in *addr, char *buf, int rc);
298int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len);
299
Holger Hans Peter Freyther62e836c2010-02-03 11:03:45 +0100300#endif