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