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