blob: 00a2e351f34150133451173ae466de8ec3f253b9 [file] [log] [blame]
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +01001/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
2/* The protocol implementation */
3
4/*
5 * (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
6 * (C) 2009-2010 by On-Waves
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <sys/types.h>
26
Holger Hans Peter Freyther1ebad742010-02-26 20:16:37 +010027#include <osmocore/talloc.h>
28
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010029#include <openbsc/debug.h>
30#include <openbsc/mgcp.h>
31#include <openbsc/mgcp_internal.h>
Harald Welte62ab20c2010-05-14 18:59:17 +020032#include <openbsc/vty.h>
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010033
Harald Welte4b037e42010-05-19 19:45:32 +020034#include <osmocom/vty/command.h>
35#include <osmocom/vty/vty.h>
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010036
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020037#include <string.h>
38
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010039static struct mgcp_config *g_cfg = NULL;
40
41/*
42 * vty code for mgcp below
43 */
44struct cmd_node mgcp_node = {
45 MGCP_NODE,
46 "%s(mgcp)#",
47 1,
48};
49
50static int config_write_mgcp(struct vty *vty)
51{
52 vty_out(vty, "mgcp%s", VTY_NEWLINE);
53 if (g_cfg->local_ip)
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020054 vty_out(vty, " local ip %s%s", g_cfg->local_ip, VTY_NEWLINE);
55 if (g_cfg->bts_ip && strlen(g_cfg->bts_ip) != 0)
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010056 vty_out(vty, " bts ip %s%s", g_cfg->bts_ip, VTY_NEWLINE);
57 vty_out(vty, " bind ip %s%s", g_cfg->source_addr, VTY_NEWLINE);
58 vty_out(vty, " bind port %u%s", g_cfg->source_port, VTY_NEWLINE);
59 vty_out(vty, " bind early %u%s", !!g_cfg->early_bind, VTY_NEWLINE);
60 vty_out(vty, " rtp base %u%s", g_cfg->rtp_base_port, VTY_NEWLINE);
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +080061 vty_out(vty, " rtp ip-dscp %d%s", g_cfg->endp_dscp, VTY_NEWLINE);
Holger Hans Peter Freyther2d425052010-04-13 09:28:40 +020062 if (g_cfg->audio_payload != -1)
63 vty_out(vty, " sdp audio payload number %d%s", g_cfg->audio_payload, VTY_NEWLINE);
64 if (g_cfg->audio_name)
65 vty_out(vty, " sdp audio payload name %s%s", g_cfg->audio_name, VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010066 vty_out(vty, " loop %u%s", !!g_cfg->audio_loop, VTY_NEWLINE);
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020067 vty_out(vty, " number endpoints %u%s", g_cfg->number_endpoints - 1, VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010068 if (g_cfg->forward_ip)
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020069 vty_out(vty, " forward audio ip %s%s", g_cfg->forward_ip, VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010070 if (g_cfg->forward_port != 0)
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020071 vty_out(vty, " forward audio port %d%s", g_cfg->forward_port, VTY_NEWLINE);
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +020072 if (g_cfg->call_agent_addr)
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020073 vty_out(vty, " call agent ip %s%s", g_cfg->call_agent_addr, VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010074
75 return CMD_SUCCESS;
76}
77
78DEFUN(show_mcgp, show_mgcp_cmd, "show mgcp",
79 SHOW_STR "Display information about the MGCP Media Gateway")
80{
81 int i;
82
83 vty_out(vty, "MGCP is up and running with %u endpoints:%s", g_cfg->number_endpoints - 1, VTY_NEWLINE);
84 for (i = 1; i < g_cfg->number_endpoints; ++i) {
85 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
Holger Hans Peter Freyther5615b982010-04-09 18:53:24 +020086 vty_out(vty, " Endpoint 0x%.2x: CI: %d net: %u/%u bts: %u/%u on %s traffic received bts: %u remote: %u%s",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010087 i, endp->ci,
88 ntohs(endp->net_rtp), ntohs(endp->net_rtcp),
Holger Hans Peter Freyther6c0729f2010-04-05 09:00:53 +020089 ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp),
Holger Hans Peter Freytherb4b135e2010-04-07 09:37:17 +020090 inet_ntoa(endp->bts), endp->in_bts, endp->in_remote,
91 VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010092 }
93
94 return CMD_SUCCESS;
95}
96
97DEFUN(cfg_mgcp,
98 cfg_mgcp_cmd,
99 "mgcp",
100 "Configure the MGCP")
101{
102 vty->node = MGCP_NODE;
103 return CMD_SUCCESS;
104}
105
106DEFUN(cfg_mgcp_local_ip,
107 cfg_mgcp_local_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800108 "local ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100109 "Set the IP to be used in SDP records")
110{
111 if (g_cfg->local_ip)
112 talloc_free(g_cfg->local_ip);
113 g_cfg->local_ip = talloc_strdup(g_cfg, argv[0]);
114 return CMD_SUCCESS;
115}
116
117DEFUN(cfg_mgcp_bts_ip,
118 cfg_mgcp_bts_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800119 "bts ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100120 "Set the IP of the BTS for RTP forwarding")
121{
122 if (g_cfg->bts_ip)
123 talloc_free(g_cfg->bts_ip);
124 g_cfg->bts_ip = talloc_strdup(g_cfg, argv[0]);
125 inet_aton(g_cfg->bts_ip, &g_cfg->bts_in);
126 return CMD_SUCCESS;
127}
128
129DEFUN(cfg_mgcp_bind_ip,
130 cfg_mgcp_bind_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800131 "bind ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100132 "Bind the MGCP to this local addr")
133{
134 if (g_cfg->source_addr)
135 talloc_free(g_cfg->source_addr);
136 g_cfg->source_addr = talloc_strdup(g_cfg, argv[0]);
137 return CMD_SUCCESS;
138}
139
140DEFUN(cfg_mgcp_bind_port,
141 cfg_mgcp_bind_port_cmd,
142 "bind port <0-65534>",
143 "Bind the MGCP to this port")
144{
145 unsigned int port = atoi(argv[0]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100146 g_cfg->source_port = port;
147 return CMD_SUCCESS;
148}
149
150DEFUN(cfg_mgcp_bind_early,
151 cfg_mgcp_bind_early_cmd,
152 "bind early (0|1)",
153 "Bind all RTP ports early")
154{
155 unsigned int bind = atoi(argv[0]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100156 g_cfg->early_bind = bind == 1;
157 return CMD_SUCCESS;
158}
159
160DEFUN(cfg_mgcp_rtp_base_port,
161 cfg_mgcp_rtp_base_port_cmd,
162 "rtp base <0-65534>",
163 "Base port to use")
164{
165 unsigned int port = atoi(argv[0]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100166 g_cfg->rtp_base_port = port;
167 return CMD_SUCCESS;
168}
169
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +0800170DEFUN(cfg_mgcp_rtp_ip_dscp,
171 cfg_mgcp_rtp_ip_dscp_cmd,
172 "rtp ip-dscp <0-255>",
173 "Set the IP_TOS socket attribute on the RTP/RTCP sockets.\n" "The DSCP value.")
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800174{
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +0800175 int dscp = atoi(argv[0]);
176 g_cfg->endp_dscp = dscp;
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800177 return CMD_SUCCESS;
178}
179
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +0800180ALIAS_DEPRECATED(cfg_mgcp_rtp_ip_dscp, cfg_mgcp_rtp_ip_tos_cmd,
181 "rtp ip-tos <0-255>",
182 "Set the IP_TOS socket attribute on the RTP/RTCP sockets.\n" "The DSCP value.")
183
184
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100185DEFUN(cfg_mgcp_sdp_payload_number,
186 cfg_mgcp_sdp_payload_number_cmd,
187 "sdp audio payload number <1-255>",
188 "Set the audio codec to use")
189{
190 unsigned int payload = atoi(argv[0]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100191 g_cfg->audio_payload = payload;
192 return CMD_SUCCESS;
193}
194
195DEFUN(cfg_mgcp_sdp_payload_name,
196 cfg_mgcp_sdp_payload_name_cmd,
197 "sdp audio payload name NAME",
198 "Set the audio name to use")
199{
200 if (g_cfg->audio_name)
201 talloc_free(g_cfg->audio_name);
202 g_cfg->audio_name = talloc_strdup(g_cfg, argv[0]);
203 return CMD_SUCCESS;
204}
205
206DEFUN(cfg_mgcp_loop,
207 cfg_mgcp_loop_cmd,
208 "loop (0|1)",
209 "Loop the audio")
210{
211 g_cfg->audio_loop = atoi(argv[0]);
212 return CMD_SUCCESS;
213}
214
215DEFUN(cfg_mgcp_number_endp,
216 cfg_mgcp_number_endp_cmd,
217 "number endpoints <0-65534>",
218 "The number of endpoints to allocate. This is not dynamic.")
219{
220 /* + 1 as we start counting at one */
221 g_cfg->number_endpoints = atoi(argv[0]) + 1;
222 return CMD_SUCCESS;
223}
224
225DEFUN(cfg_mgcp_forward_ip,
226 cfg_mgcp_forward_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800227 "forward audio ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100228 "Forward packets from and to the IP. This disables most of the MGCP feature.")
229{
230 if (g_cfg->forward_ip)
231 talloc_free(g_cfg->forward_ip);
232 g_cfg->forward_ip = talloc_strdup(g_cfg, argv[0]);
233 return CMD_SUCCESS;
234}
235
236DEFUN(cfg_mgcp_forward_port,
237 cfg_mgcp_forward_port_cmd,
238 "forward audio port <1-15000>",
239 "Forward packets from and to the port. This disables most of the MGCP feature.")
240{
241 g_cfg->forward_port = atoi(argv[0]);
242 return CMD_SUCCESS;
243}
244
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +0200245DEFUN(cfg_mgcp_agent_addr,
246 cfg_mgcp_agent_addr_cmd,
247 "call agent ip IP",
248 "Set the address of the call agent.")
249{
250 if (g_cfg->call_agent_addr)
251 talloc_free(g_cfg->call_agent_addr);
252 g_cfg->call_agent_addr = talloc_strdup(g_cfg, argv[0]);
253 return CMD_SUCCESS;
254}
255
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100256int mgcp_vty_init(void)
257{
Holger Hans Peter Freytherb5be7ac2010-05-14 02:45:52 +0800258 install_element_ve(&show_mgcp_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100259
260 install_element(CONFIG_NODE, &cfg_mgcp_cmd);
261 install_node(&mgcp_node, config_write_mgcp);
262 install_default(MGCP_NODE);
Harald Welte62ab20c2010-05-14 18:59:17 +0200263 install_element(MGCP_NODE, &ournode_exit_cmd);
Harald Welte54f74242010-05-14 19:11:04 +0200264 install_element(MGCP_NODE, &ournode_end_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100265 install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd);
266 install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd);
267 install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd);
268 install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd);
269 install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd);
270 install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd);
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +0800271 install_element(MGCP_NODE, &cfg_mgcp_rtp_ip_dscp_cmd);
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800272 install_element(MGCP_NODE, &cfg_mgcp_rtp_ip_tos_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100273 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd);
274 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd);
275 install_element(MGCP_NODE, &cfg_mgcp_loop_cmd);
276 install_element(MGCP_NODE, &cfg_mgcp_number_endp_cmd);
277 install_element(MGCP_NODE, &cfg_mgcp_forward_ip_cmd);
278 install_element(MGCP_NODE, &cfg_mgcp_forward_port_cmd);
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +0200279 install_element(MGCP_NODE, &cfg_mgcp_agent_addr_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100280 return 0;
281}
282
283int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg)
284{
285 int i, rc;
286
287 g_cfg = cfg;
Harald Weltedcccb182010-05-16 20:52:23 +0200288 rc = vty_read_config_file(config_file, NULL);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100289 if (rc < 0) {
290 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
291 return rc;
292 }
293
294
295 if (!g_cfg->bts_ip)
296 fprintf(stderr, "No BTS ip address specified. This will allow everyone to connect.\n");
297
Holger Hans Peter Freyther95e4d342010-03-30 13:00:10 +0200298 if (!g_cfg->source_addr) {
299 fprintf(stderr, "You need to specify a bind address.\n");
300 return -1;
301 }
302
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100303 if (mgcp_endpoints_allocate(g_cfg) != 0) {
304 fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n", g_cfg->number_endpoints);
305 return -1;
306 }
307
308 /*
309 * This application supports two modes.
Holger Hans Peter Freythera5811362010-04-30 13:32:05 +0800310 * 1.) a true MGCP gateway with support for AUEP, CRCX, MDCX, DLCX
311 * 2.) plain forwarding of RTP packets on the endpoints.
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100312 * both modes are mutual exclusive
313 */
314 if (g_cfg->forward_ip) {
315 int port = g_cfg->rtp_base_port;
316 if (g_cfg->forward_port != 0)
317 port = g_cfg->forward_port;
318
319 if (!g_cfg->early_bind) {
320 LOGP(DMGCP, LOGL_NOTICE, "Forwarding requires early bind.\n");
321 return -1;
322 }
323
324 /*
325 * Store the forward IP and assign a ci. For early bind
326 * the sockets will be created after this.
327 */
328 for (i = 1; i < g_cfg->number_endpoints; ++i) {
329 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
330 inet_aton(g_cfg->forward_ip, &endp->remote);
331 endp->ci = CI_UNUSED + 23;
332 endp->net_rtp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port));
333 endp->net_rtcp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port) + 1);
334 }
335
336 LOGP(DMGCP, LOGL_NOTICE, "Configured for Audio Forwarding.\n");
337 }
338
339 /* early bind */
340 if (g_cfg->early_bind) {
341 for (i = 1; i < g_cfg->number_endpoints; ++i) {
342 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
343 int rtp_port;
344
345 rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), g_cfg->rtp_base_port);
346 if (mgcp_bind_rtp_port(endp, rtp_port) != 0) {
Holger Hans Peter Freyther590cd982010-02-26 13:10:51 +0100347 LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100348 return -1;
349 }
350 }
351 }
352
353 return !!g_cfg->forward_ip;
354}
355