blob: ea59abf15d24a64f65734990e470c1ba60e86d21 [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 Freyther2d425052010-04-13 09:28:40 +020061 if (g_cfg->audio_payload != -1)
62 vty_out(vty, " sdp audio payload number %d%s", g_cfg->audio_payload, VTY_NEWLINE);
63 if (g_cfg->audio_name)
64 vty_out(vty, " sdp audio payload name %s%s", g_cfg->audio_name, VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010065 vty_out(vty, " loop %u%s", !!g_cfg->audio_loop, VTY_NEWLINE);
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020066 vty_out(vty, " number endpoints %u%s", g_cfg->number_endpoints - 1, VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010067 if (g_cfg->forward_ip)
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020068 vty_out(vty, " forward audio ip %s%s", g_cfg->forward_ip, VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010069 if (g_cfg->forward_port != 0)
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020070 vty_out(vty, " forward audio port %d%s", g_cfg->forward_port, VTY_NEWLINE);
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +020071 if (g_cfg->call_agent_addr)
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020072 vty_out(vty, " call agent ip %s%s", g_cfg->call_agent_addr, VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010073
74 return CMD_SUCCESS;
75}
76
77DEFUN(show_mcgp, show_mgcp_cmd, "show mgcp",
78 SHOW_STR "Display information about the MGCP Media Gateway")
79{
80 int i;
81
82 vty_out(vty, "MGCP is up and running with %u endpoints:%s", g_cfg->number_endpoints - 1, VTY_NEWLINE);
83 for (i = 1; i < g_cfg->number_endpoints; ++i) {
84 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
Holger Hans Peter Freyther5615b982010-04-09 18:53:24 +020085 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 +010086 i, endp->ci,
87 ntohs(endp->net_rtp), ntohs(endp->net_rtcp),
Holger Hans Peter Freyther6c0729f2010-04-05 09:00:53 +020088 ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp),
Holger Hans Peter Freytherb4b135e2010-04-07 09:37:17 +020089 inet_ntoa(endp->bts), endp->in_bts, endp->in_remote,
90 VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010091 }
92
93 return CMD_SUCCESS;
94}
95
96DEFUN(cfg_mgcp,
97 cfg_mgcp_cmd,
98 "mgcp",
99 "Configure the MGCP")
100{
101 vty->node = MGCP_NODE;
102 return CMD_SUCCESS;
103}
104
105DEFUN(cfg_mgcp_local_ip,
106 cfg_mgcp_local_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800107 "local ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100108 "Set the IP to be used in SDP records")
109{
110 if (g_cfg->local_ip)
111 talloc_free(g_cfg->local_ip);
112 g_cfg->local_ip = talloc_strdup(g_cfg, argv[0]);
113 return CMD_SUCCESS;
114}
115
116DEFUN(cfg_mgcp_bts_ip,
117 cfg_mgcp_bts_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800118 "bts ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100119 "Set the IP of the BTS for RTP forwarding")
120{
121 if (g_cfg->bts_ip)
122 talloc_free(g_cfg->bts_ip);
123 g_cfg->bts_ip = talloc_strdup(g_cfg, argv[0]);
124 inet_aton(g_cfg->bts_ip, &g_cfg->bts_in);
125 return CMD_SUCCESS;
126}
127
128DEFUN(cfg_mgcp_bind_ip,
129 cfg_mgcp_bind_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800130 "bind ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100131 "Bind the MGCP to this local addr")
132{
133 if (g_cfg->source_addr)
134 talloc_free(g_cfg->source_addr);
135 g_cfg->source_addr = talloc_strdup(g_cfg, argv[0]);
136 return CMD_SUCCESS;
137}
138
139DEFUN(cfg_mgcp_bind_port,
140 cfg_mgcp_bind_port_cmd,
141 "bind port <0-65534>",
142 "Bind the MGCP to this port")
143{
144 unsigned int port = atoi(argv[0]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100145 g_cfg->source_port = port;
146 return CMD_SUCCESS;
147}
148
149DEFUN(cfg_mgcp_bind_early,
150 cfg_mgcp_bind_early_cmd,
151 "bind early (0|1)",
152 "Bind all RTP ports early")
153{
154 unsigned int bind = atoi(argv[0]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100155 g_cfg->early_bind = bind == 1;
156 return CMD_SUCCESS;
157}
158
159DEFUN(cfg_mgcp_rtp_base_port,
160 cfg_mgcp_rtp_base_port_cmd,
161 "rtp base <0-65534>",
162 "Base port to use")
163{
164 unsigned int port = atoi(argv[0]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100165 g_cfg->rtp_base_port = port;
166 return CMD_SUCCESS;
167}
168
169DEFUN(cfg_mgcp_sdp_payload_number,
170 cfg_mgcp_sdp_payload_number_cmd,
171 "sdp audio payload number <1-255>",
172 "Set the audio codec to use")
173{
174 unsigned int payload = atoi(argv[0]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100175 g_cfg->audio_payload = payload;
176 return CMD_SUCCESS;
177}
178
179DEFUN(cfg_mgcp_sdp_payload_name,
180 cfg_mgcp_sdp_payload_name_cmd,
181 "sdp audio payload name NAME",
182 "Set the audio name to use")
183{
184 if (g_cfg->audio_name)
185 talloc_free(g_cfg->audio_name);
186 g_cfg->audio_name = talloc_strdup(g_cfg, argv[0]);
187 return CMD_SUCCESS;
188}
189
190DEFUN(cfg_mgcp_loop,
191 cfg_mgcp_loop_cmd,
192 "loop (0|1)",
193 "Loop the audio")
194{
195 g_cfg->audio_loop = atoi(argv[0]);
196 return CMD_SUCCESS;
197}
198
199DEFUN(cfg_mgcp_number_endp,
200 cfg_mgcp_number_endp_cmd,
201 "number endpoints <0-65534>",
202 "The number of endpoints to allocate. This is not dynamic.")
203{
204 /* + 1 as we start counting at one */
205 g_cfg->number_endpoints = atoi(argv[0]) + 1;
206 return CMD_SUCCESS;
207}
208
209DEFUN(cfg_mgcp_forward_ip,
210 cfg_mgcp_forward_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800211 "forward audio ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100212 "Forward packets from and to the IP. This disables most of the MGCP feature.")
213{
214 if (g_cfg->forward_ip)
215 talloc_free(g_cfg->forward_ip);
216 g_cfg->forward_ip = talloc_strdup(g_cfg, argv[0]);
217 return CMD_SUCCESS;
218}
219
220DEFUN(cfg_mgcp_forward_port,
221 cfg_mgcp_forward_port_cmd,
222 "forward audio port <1-15000>",
223 "Forward packets from and to the port. This disables most of the MGCP feature.")
224{
225 g_cfg->forward_port = atoi(argv[0]);
226 return CMD_SUCCESS;
227}
228
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +0200229DEFUN(cfg_mgcp_agent_addr,
230 cfg_mgcp_agent_addr_cmd,
231 "call agent ip IP",
232 "Set the address of the call agent.")
233{
234 if (g_cfg->call_agent_addr)
235 talloc_free(g_cfg->call_agent_addr);
236 g_cfg->call_agent_addr = talloc_strdup(g_cfg, argv[0]);
237 return CMD_SUCCESS;
238}
239
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100240int mgcp_vty_init(void)
241{
Holger Hans Peter Freytherb5be7ac2010-05-14 02:45:52 +0800242 install_element_ve(&show_mgcp_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100243
244 install_element(CONFIG_NODE, &cfg_mgcp_cmd);
245 install_node(&mgcp_node, config_write_mgcp);
246 install_default(MGCP_NODE);
Harald Welte62ab20c2010-05-14 18:59:17 +0200247 install_element(MGCP_NODE, &ournode_exit_cmd);
Harald Welte54f74242010-05-14 19:11:04 +0200248 install_element(MGCP_NODE, &ournode_end_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100249 install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd);
250 install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd);
251 install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd);
252 install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd);
253 install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd);
254 install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd);
255 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd);
256 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd);
257 install_element(MGCP_NODE, &cfg_mgcp_loop_cmd);
258 install_element(MGCP_NODE, &cfg_mgcp_number_endp_cmd);
259 install_element(MGCP_NODE, &cfg_mgcp_forward_ip_cmd);
260 install_element(MGCP_NODE, &cfg_mgcp_forward_port_cmd);
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +0200261 install_element(MGCP_NODE, &cfg_mgcp_agent_addr_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100262 return 0;
263}
264
265int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg)
266{
267 int i, rc;
268
269 g_cfg = cfg;
Harald Weltedcccb182010-05-16 20:52:23 +0200270 rc = vty_read_config_file(config_file, NULL);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100271 if (rc < 0) {
272 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
273 return rc;
274 }
275
276
277 if (!g_cfg->bts_ip)
278 fprintf(stderr, "No BTS ip address specified. This will allow everyone to connect.\n");
279
Holger Hans Peter Freyther95e4d342010-03-30 13:00:10 +0200280 if (!g_cfg->source_addr) {
281 fprintf(stderr, "You need to specify a bind address.\n");
282 return -1;
283 }
284
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100285 if (mgcp_endpoints_allocate(g_cfg) != 0) {
286 fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n", g_cfg->number_endpoints);
287 return -1;
288 }
289
290 /*
291 * This application supports two modes.
Holger Hans Peter Freythera5811362010-04-30 13:32:05 +0800292 * 1.) a true MGCP gateway with support for AUEP, CRCX, MDCX, DLCX
293 * 2.) plain forwarding of RTP packets on the endpoints.
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100294 * both modes are mutual exclusive
295 */
296 if (g_cfg->forward_ip) {
297 int port = g_cfg->rtp_base_port;
298 if (g_cfg->forward_port != 0)
299 port = g_cfg->forward_port;
300
301 if (!g_cfg->early_bind) {
302 LOGP(DMGCP, LOGL_NOTICE, "Forwarding requires early bind.\n");
303 return -1;
304 }
305
306 /*
307 * Store the forward IP and assign a ci. For early bind
308 * the sockets will be created after this.
309 */
310 for (i = 1; i < g_cfg->number_endpoints; ++i) {
311 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
312 inet_aton(g_cfg->forward_ip, &endp->remote);
313 endp->ci = CI_UNUSED + 23;
314 endp->net_rtp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port));
315 endp->net_rtcp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port) + 1);
316 }
317
318 LOGP(DMGCP, LOGL_NOTICE, "Configured for Audio Forwarding.\n");
319 }
320
321 /* early bind */
322 if (g_cfg->early_bind) {
323 for (i = 1; i < g_cfg->number_endpoints; ++i) {
324 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
325 int rtp_port;
326
327 rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), g_cfg->rtp_base_port);
328 if (mgcp_bind_rtp_port(endp, rtp_port) != 0) {
Holger Hans Peter Freyther590cd982010-02-26 13:10:51 +0100329 LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100330 return -1;
331 }
332 }
333 }
334
335 return !!g_cfg->forward_ip;
336}
337