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