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