blob: dd0293a750cab1a2d0b2f4d9644b23647bad41b0 [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]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100144 g_cfg->source_port = port;
145 return CMD_SUCCESS;
146}
147
148DEFUN(cfg_mgcp_bind_early,
149 cfg_mgcp_bind_early_cmd,
150 "bind early (0|1)",
151 "Bind all RTP ports early")
152{
153 unsigned int bind = atoi(argv[0]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100154 g_cfg->early_bind = bind == 1;
155 return CMD_SUCCESS;
156}
157
158DEFUN(cfg_mgcp_rtp_base_port,
159 cfg_mgcp_rtp_base_port_cmd,
160 "rtp base <0-65534>",
161 "Base port to use")
162{
163 unsigned int port = atoi(argv[0]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100164 g_cfg->rtp_base_port = port;
165 return CMD_SUCCESS;
166}
167
168DEFUN(cfg_mgcp_sdp_payload_number,
169 cfg_mgcp_sdp_payload_number_cmd,
170 "sdp audio payload number <1-255>",
171 "Set the audio codec to use")
172{
173 unsigned int payload = atoi(argv[0]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100174 g_cfg->audio_payload = payload;
175 return CMD_SUCCESS;
176}
177
178DEFUN(cfg_mgcp_sdp_payload_name,
179 cfg_mgcp_sdp_payload_name_cmd,
180 "sdp audio payload name NAME",
181 "Set the audio name to use")
182{
183 if (g_cfg->audio_name)
184 talloc_free(g_cfg->audio_name);
185 g_cfg->audio_name = talloc_strdup(g_cfg, argv[0]);
186 return CMD_SUCCESS;
187}
188
189DEFUN(cfg_mgcp_loop,
190 cfg_mgcp_loop_cmd,
191 "loop (0|1)",
192 "Loop the audio")
193{
194 g_cfg->audio_loop = atoi(argv[0]);
195 return CMD_SUCCESS;
196}
197
198DEFUN(cfg_mgcp_number_endp,
199 cfg_mgcp_number_endp_cmd,
200 "number endpoints <0-65534>",
201 "The number of endpoints to allocate. This is not dynamic.")
202{
203 /* + 1 as we start counting at one */
204 g_cfg->number_endpoints = atoi(argv[0]) + 1;
205 return CMD_SUCCESS;
206}
207
208DEFUN(cfg_mgcp_forward_ip,
209 cfg_mgcp_forward_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800210 "forward audio ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100211 "Forward packets from and to the IP. This disables most of the MGCP feature.")
212{
213 if (g_cfg->forward_ip)
214 talloc_free(g_cfg->forward_ip);
215 g_cfg->forward_ip = talloc_strdup(g_cfg, argv[0]);
216 return CMD_SUCCESS;
217}
218
219DEFUN(cfg_mgcp_forward_port,
220 cfg_mgcp_forward_port_cmd,
221 "forward audio port <1-15000>",
222 "Forward packets from and to the port. This disables most of the MGCP feature.")
223{
224 g_cfg->forward_port = atoi(argv[0]);
225 return CMD_SUCCESS;
226}
227
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +0200228DEFUN(cfg_mgcp_agent_addr,
229 cfg_mgcp_agent_addr_cmd,
230 "call agent ip IP",
231 "Set the address of the call agent.")
232{
233 if (g_cfg->call_agent_addr)
234 talloc_free(g_cfg->call_agent_addr);
235 g_cfg->call_agent_addr = talloc_strdup(g_cfg, argv[0]);
236 return CMD_SUCCESS;
237}
238
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100239int mgcp_vty_init(void)
240{
Holger Hans Peter Freytherb5be7ac2010-05-14 02:45:52 +0800241 install_element_ve(&show_mgcp_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100242
243 install_element(CONFIG_NODE, &cfg_mgcp_cmd);
244 install_node(&mgcp_node, config_write_mgcp);
245 install_default(MGCP_NODE);
246 install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd);
247 install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd);
248 install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd);
249 install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd);
250 install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd);
251 install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd);
252 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd);
253 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd);
254 install_element(MGCP_NODE, &cfg_mgcp_loop_cmd);
255 install_element(MGCP_NODE, &cfg_mgcp_number_endp_cmd);
256 install_element(MGCP_NODE, &cfg_mgcp_forward_ip_cmd);
257 install_element(MGCP_NODE, &cfg_mgcp_forward_port_cmd);
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +0200258 install_element(MGCP_NODE, &cfg_mgcp_agent_addr_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100259 return 0;
260}
261
262int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg)
263{
264 int i, rc;
265
266 g_cfg = cfg;
267 rc = vty_read_config_file(config_file);
268 if (rc < 0) {
269 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
270 return rc;
271 }
272
273
274 if (!g_cfg->bts_ip)
275 fprintf(stderr, "No BTS ip address specified. This will allow everyone to connect.\n");
276
Holger Hans Peter Freyther95e4d342010-03-30 13:00:10 +0200277 if (!g_cfg->source_addr) {
278 fprintf(stderr, "You need to specify a bind address.\n");
279 return -1;
280 }
281
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100282 if (mgcp_endpoints_allocate(g_cfg) != 0) {
283 fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n", g_cfg->number_endpoints);
284 return -1;
285 }
286
287 /*
288 * This application supports two modes.
Holger Hans Peter Freythera5811362010-04-30 13:32:05 +0800289 * 1.) a true MGCP gateway with support for AUEP, CRCX, MDCX, DLCX
290 * 2.) plain forwarding of RTP packets on the endpoints.
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100291 * both modes are mutual exclusive
292 */
293 if (g_cfg->forward_ip) {
294 int port = g_cfg->rtp_base_port;
295 if (g_cfg->forward_port != 0)
296 port = g_cfg->forward_port;
297
298 if (!g_cfg->early_bind) {
299 LOGP(DMGCP, LOGL_NOTICE, "Forwarding requires early bind.\n");
300 return -1;
301 }
302
303 /*
304 * Store the forward IP and assign a ci. For early bind
305 * the sockets will be created after this.
306 */
307 for (i = 1; i < g_cfg->number_endpoints; ++i) {
308 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
309 inet_aton(g_cfg->forward_ip, &endp->remote);
310 endp->ci = CI_UNUSED + 23;
311 endp->net_rtp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port));
312 endp->net_rtcp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port) + 1);
313 }
314
315 LOGP(DMGCP, LOGL_NOTICE, "Configured for Audio Forwarding.\n");
316 }
317
318 /* early bind */
319 if (g_cfg->early_bind) {
320 for (i = 1; i < g_cfg->number_endpoints; ++i) {
321 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
322 int rtp_port;
323
324 rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), g_cfg->rtp_base_port);
325 if (mgcp_bind_rtp_port(endp, rtp_port) != 0) {
Holger Hans Peter Freyther590cd982010-02-26 13:10:51 +0100326 LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100327 return -1;
328 }
329 }
330 }
331
332 return !!g_cfg->forward_ip;
333}
334