blob: f6ac53b5ede6afed776d3e428eb106c461818aa8 [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>
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010032
33#include <vty/command.h>
34#include <vty/vty.h>
35
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020036#include <string.h>
37
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010038static struct mgcp_config *g_cfg = NULL;
39
40/*
41 * vty code for mgcp below
42 */
43struct cmd_node mgcp_node = {
44 MGCP_NODE,
45 "%s(mgcp)#",
46 1,
47};
48
49static int config_write_mgcp(struct vty *vty)
50{
51 vty_out(vty, "mgcp%s", VTY_NEWLINE);
52 if (g_cfg->local_ip)
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020053 vty_out(vty, " local ip %s%s", g_cfg->local_ip, VTY_NEWLINE);
54 if (g_cfg->bts_ip && strlen(g_cfg->bts_ip) != 0)
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010055 vty_out(vty, " bts ip %s%s", g_cfg->bts_ip, VTY_NEWLINE);
56 vty_out(vty, " bind ip %s%s", g_cfg->source_addr, VTY_NEWLINE);
57 vty_out(vty, " bind port %u%s", g_cfg->source_port, VTY_NEWLINE);
58 vty_out(vty, " bind early %u%s", !!g_cfg->early_bind, VTY_NEWLINE);
59 vty_out(vty, " rtp base %u%s", g_cfg->rtp_base_port, VTY_NEWLINE);
Holger Hans Peter Freyther2d425052010-04-13 09:28:40 +020060 if (g_cfg->audio_payload != -1)
61 vty_out(vty, " sdp audio payload number %d%s", g_cfg->audio_payload, VTY_NEWLINE);
62 if (g_cfg->audio_name)
63 vty_out(vty, " sdp audio payload name %s%s", g_cfg->audio_name, VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010064 vty_out(vty, " loop %u%s", !!g_cfg->audio_loop, VTY_NEWLINE);
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020065 vty_out(vty, " number endpoints %u%s", g_cfg->number_endpoints - 1, VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010066 if (g_cfg->forward_ip)
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020067 vty_out(vty, " forward audio ip %s%s", g_cfg->forward_ip, VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010068 if (g_cfg->forward_port != 0)
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020069 vty_out(vty, " forward audio port %d%s", g_cfg->forward_port, VTY_NEWLINE);
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +020070 if (g_cfg->call_agent_addr)
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020071 vty_out(vty, " call agent ip %s%s", g_cfg->call_agent_addr, VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010072
73 return CMD_SUCCESS;
74}
75
76DEFUN(show_mcgp, show_mgcp_cmd, "show mgcp",
77 SHOW_STR "Display information about the MGCP Media Gateway")
78{
79 int i;
80
81 vty_out(vty, "MGCP is up and running with %u endpoints:%s", g_cfg->number_endpoints - 1, VTY_NEWLINE);
82 for (i = 1; i < g_cfg->number_endpoints; ++i) {
83 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
Holger Hans Peter Freyther5615b982010-04-09 18:53:24 +020084 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 +010085 i, endp->ci,
86 ntohs(endp->net_rtp), ntohs(endp->net_rtcp),
Holger Hans Peter Freyther6c0729f2010-04-05 09:00:53 +020087 ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp),
Holger Hans Peter Freytherb4b135e2010-04-07 09:37:17 +020088 inet_ntoa(endp->bts), endp->in_bts, endp->in_remote,
89 VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010090 }
91
92 return CMD_SUCCESS;
93}
94
95DEFUN(cfg_mgcp,
96 cfg_mgcp_cmd,
97 "mgcp",
98 "Configure the MGCP")
99{
100 vty->node = MGCP_NODE;
101 return CMD_SUCCESS;
102}
103
104DEFUN(cfg_mgcp_local_ip,
105 cfg_mgcp_local_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800106 "local ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100107 "Set the IP to be used in SDP records")
108{
109 if (g_cfg->local_ip)
110 talloc_free(g_cfg->local_ip);
111 g_cfg->local_ip = talloc_strdup(g_cfg, argv[0]);
112 return CMD_SUCCESS;
113}
114
115DEFUN(cfg_mgcp_bts_ip,
116 cfg_mgcp_bts_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800117 "bts ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100118 "Set the IP of the BTS for RTP forwarding")
119{
120 if (g_cfg->bts_ip)
121 talloc_free(g_cfg->bts_ip);
122 g_cfg->bts_ip = talloc_strdup(g_cfg, argv[0]);
123 inet_aton(g_cfg->bts_ip, &g_cfg->bts_in);
124 return CMD_SUCCESS;
125}
126
127DEFUN(cfg_mgcp_bind_ip,
128 cfg_mgcp_bind_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800129 "bind ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100130 "Bind the MGCP to this local addr")
131{
132 if (g_cfg->source_addr)
133 talloc_free(g_cfg->source_addr);
134 g_cfg->source_addr = talloc_strdup(g_cfg, argv[0]);
135 return CMD_SUCCESS;
136}
137
138DEFUN(cfg_mgcp_bind_port,
139 cfg_mgcp_bind_port_cmd,
140 "bind port <0-65534>",
141 "Bind the MGCP to this port")
142{
143 unsigned int port = atoi(argv[0]);
144 if (port > 65534) {
145 vty_out(vty, "%% wrong bind port '%s'%s", argv[0], VTY_NEWLINE);
146 return CMD_WARNING;
147 }
148
149 g_cfg->source_port = port;
150 return CMD_SUCCESS;
151}
152
153DEFUN(cfg_mgcp_bind_early,
154 cfg_mgcp_bind_early_cmd,
155 "bind early (0|1)",
156 "Bind all RTP ports early")
157{
158 unsigned int bind = atoi(argv[0]);
159 if (bind != 0 && bind != 1) {
160 vty_out(vty, "%% param must be 0 or 1.%s", VTY_NEWLINE);
161 return CMD_WARNING;
162 }
163
164 g_cfg->early_bind = bind == 1;
165 return CMD_SUCCESS;
166}
167
168DEFUN(cfg_mgcp_rtp_base_port,
169 cfg_mgcp_rtp_base_port_cmd,
170 "rtp base <0-65534>",
171 "Base port to use")
172{
173 unsigned int port = atoi(argv[0]);
174 if (port > 65534) {
175 vty_out(vty, "%% wrong base port '%s'%s", argv[0], VTY_NEWLINE);
176 return CMD_WARNING;
177 }
178
179 g_cfg->rtp_base_port = port;
180 return CMD_SUCCESS;
181}
182
183DEFUN(cfg_mgcp_sdp_payload_number,
184 cfg_mgcp_sdp_payload_number_cmd,
185 "sdp audio payload number <1-255>",
186 "Set the audio codec to use")
187{
188 unsigned int payload = atoi(argv[0]);
189 if (payload > 255) {
190 vty_out(vty, "%% wrong payload number '%s'%s", argv[0], VTY_NEWLINE);
191 return CMD_WARNING;
192 }
193
194 g_cfg->audio_payload = payload;
195 return CMD_SUCCESS;
196}
197
198DEFUN(cfg_mgcp_sdp_payload_name,
199 cfg_mgcp_sdp_payload_name_cmd,
200 "sdp audio payload name NAME",
201 "Set the audio name to use")
202{
203 if (g_cfg->audio_name)
204 talloc_free(g_cfg->audio_name);
205 g_cfg->audio_name = talloc_strdup(g_cfg, argv[0]);
206 return CMD_SUCCESS;
207}
208
209DEFUN(cfg_mgcp_loop,
210 cfg_mgcp_loop_cmd,
211 "loop (0|1)",
212 "Loop the audio")
213{
214 g_cfg->audio_loop = atoi(argv[0]);
215 return CMD_SUCCESS;
216}
217
218DEFUN(cfg_mgcp_number_endp,
219 cfg_mgcp_number_endp_cmd,
220 "number endpoints <0-65534>",
221 "The number of endpoints to allocate. This is not dynamic.")
222{
223 /* + 1 as we start counting at one */
224 g_cfg->number_endpoints = atoi(argv[0]) + 1;
225 return CMD_SUCCESS;
226}
227
228DEFUN(cfg_mgcp_forward_ip,
229 cfg_mgcp_forward_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800230 "forward audio ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100231 "Forward packets from and to the IP. This disables most of the MGCP feature.")
232{
233 if (g_cfg->forward_ip)
234 talloc_free(g_cfg->forward_ip);
235 g_cfg->forward_ip = talloc_strdup(g_cfg, argv[0]);
236 return CMD_SUCCESS;
237}
238
239DEFUN(cfg_mgcp_forward_port,
240 cfg_mgcp_forward_port_cmd,
241 "forward audio port <1-15000>",
242 "Forward packets from and to the port. This disables most of the MGCP feature.")
243{
244 g_cfg->forward_port = atoi(argv[0]);
245 return CMD_SUCCESS;
246}
247
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +0200248DEFUN(cfg_mgcp_agent_addr,
249 cfg_mgcp_agent_addr_cmd,
250 "call agent ip IP",
251 "Set the address of the call agent.")
252{
253 if (g_cfg->call_agent_addr)
254 talloc_free(g_cfg->call_agent_addr);
255 g_cfg->call_agent_addr = talloc_strdup(g_cfg, argv[0]);
256 return CMD_SUCCESS;
257}
258
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100259int mgcp_vty_init(void)
260{
261 install_element(VIEW_NODE, &show_mgcp_cmd);
262
263 install_element(CONFIG_NODE, &cfg_mgcp_cmd);
264 install_node(&mgcp_node, config_write_mgcp);
265 install_default(MGCP_NODE);
266 install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd);
267 install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd);
268 install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd);
269 install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd);
270 install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd);
271 install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd);
272 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd);
273 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd);
274 install_element(MGCP_NODE, &cfg_mgcp_loop_cmd);
275 install_element(MGCP_NODE, &cfg_mgcp_number_endp_cmd);
276 install_element(MGCP_NODE, &cfg_mgcp_forward_ip_cmd);
277 install_element(MGCP_NODE, &cfg_mgcp_forward_port_cmd);
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +0200278 install_element(MGCP_NODE, &cfg_mgcp_agent_addr_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100279 return 0;
280}
281
282int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg)
283{
284 int i, rc;
285
286 g_cfg = cfg;
287 rc = vty_read_config_file(config_file);
288 if (rc < 0) {
289 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
290 return rc;
291 }
292
293
294 if (!g_cfg->bts_ip)
295 fprintf(stderr, "No BTS ip address specified. This will allow everyone to connect.\n");
296
Holger Hans Peter Freyther95e4d342010-03-30 13:00:10 +0200297 if (!g_cfg->source_addr) {
298 fprintf(stderr, "You need to specify a bind address.\n");
299 return -1;
300 }
301
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100302 if (mgcp_endpoints_allocate(g_cfg) != 0) {
303 fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n", g_cfg->number_endpoints);
304 return -1;
305 }
306
307 /*
308 * This application supports two modes.
309 * 1.) a true MGCP gateway with support for AUEP, CRCX, MDCX, DLCX
310 * 2.) plain forwarding of RTP packets on the endpoints.
311 * both modes are mutual exclusive
312 */
313 if (g_cfg->forward_ip) {
314 int port = g_cfg->rtp_base_port;
315 if (g_cfg->forward_port != 0)
316 port = g_cfg->forward_port;
317
318 if (!g_cfg->early_bind) {
319 LOGP(DMGCP, LOGL_NOTICE, "Forwarding requires early bind.\n");
320 return -1;
321 }
322
323 /*
324 * Store the forward IP and assign a ci. For early bind
325 * the sockets will be created after this.
326 */
327 for (i = 1; i < g_cfg->number_endpoints; ++i) {
328 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
329 inet_aton(g_cfg->forward_ip, &endp->remote);
330 endp->ci = CI_UNUSED + 23;
331 endp->net_rtp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port));
332 endp->net_rtcp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port) + 1);
333 }
334
335 LOGP(DMGCP, LOGL_NOTICE, "Configured for Audio Forwarding.\n");
336 }
337
338 /* early bind */
339 if (g_cfg->early_bind) {
340 for (i = 1; i < g_cfg->number_endpoints; ++i) {
341 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
342 int rtp_port;
343
344 rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), g_cfg->rtp_base_port);
345 if (mgcp_bind_rtp_port(endp, rtp_port) != 0) {
Holger Hans Peter Freyther590cd982010-02-26 13:10:51 +0100346 LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100347 return -1;
348 }
349 }
350 }
351
352 return !!g_cfg->forward_ip;
353}
354