blob: 3286198970f173d841b3fa08f4adfc00c01a5735 [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 Freyther75492e62010-05-31 10:22:00 +080060 vty_out(vty, " rtp ip-tos %d%s", g_cfg->endp_tos, VTY_NEWLINE);
Holger Hans Peter Freyther2d425052010-04-13 09:28:40 +020061 if (g_cfg->audio_payload != -1)
62 vty_out(vty, " sdp audio payload number %d%s", g_cfg->audio_payload, VTY_NEWLINE);
63 if (g_cfg->audio_name)
64 vty_out(vty, " sdp audio payload name %s%s", g_cfg->audio_name, VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010065 vty_out(vty, " loop %u%s", !!g_cfg->audio_loop, VTY_NEWLINE);
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020066 vty_out(vty, " number endpoints %u%s", g_cfg->number_endpoints - 1, VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010067 if (g_cfg->forward_ip)
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020068 vty_out(vty, " forward audio ip %s%s", g_cfg->forward_ip, VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010069 if (g_cfg->forward_port != 0)
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020070 vty_out(vty, " forward audio port %d%s", g_cfg->forward_port, VTY_NEWLINE);
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +020071 if (g_cfg->call_agent_addr)
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020072 vty_out(vty, " call agent ip %s%s", g_cfg->call_agent_addr, VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010073
74 return CMD_SUCCESS;
75}
76
77DEFUN(show_mcgp, show_mgcp_cmd, "show mgcp",
78 SHOW_STR "Display information about the MGCP Media Gateway")
79{
80 int i;
81
82 vty_out(vty, "MGCP is up and running with %u endpoints:%s", g_cfg->number_endpoints - 1, VTY_NEWLINE);
83 for (i = 1; i < g_cfg->number_endpoints; ++i) {
84 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
Holger Hans Peter Freyther5615b982010-04-09 18:53:24 +020085 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 +010086 i, endp->ci,
87 ntohs(endp->net_rtp), ntohs(endp->net_rtcp),
Holger Hans Peter Freyther6c0729f2010-04-05 09:00:53 +020088 ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp),
Holger Hans Peter Freytherb4b135e2010-04-07 09:37:17 +020089 inet_ntoa(endp->bts), endp->in_bts, endp->in_remote,
90 VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010091 }
92
93 return CMD_SUCCESS;
94}
95
96DEFUN(cfg_mgcp,
97 cfg_mgcp_cmd,
98 "mgcp",
99 "Configure the MGCP")
100{
101 vty->node = MGCP_NODE;
102 return CMD_SUCCESS;
103}
104
105DEFUN(cfg_mgcp_local_ip,
106 cfg_mgcp_local_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800107 "local ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100108 "Set the IP to be used in SDP records")
109{
110 if (g_cfg->local_ip)
111 talloc_free(g_cfg->local_ip);
112 g_cfg->local_ip = talloc_strdup(g_cfg, argv[0]);
113 return CMD_SUCCESS;
114}
115
116DEFUN(cfg_mgcp_bts_ip,
117 cfg_mgcp_bts_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800118 "bts ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100119 "Set the IP of the BTS for RTP forwarding")
120{
121 if (g_cfg->bts_ip)
122 talloc_free(g_cfg->bts_ip);
123 g_cfg->bts_ip = talloc_strdup(g_cfg, argv[0]);
124 inet_aton(g_cfg->bts_ip, &g_cfg->bts_in);
125 return CMD_SUCCESS;
126}
127
128DEFUN(cfg_mgcp_bind_ip,
129 cfg_mgcp_bind_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800130 "bind ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100131 "Bind the MGCP to this local addr")
132{
133 if (g_cfg->source_addr)
134 talloc_free(g_cfg->source_addr);
135 g_cfg->source_addr = talloc_strdup(g_cfg, argv[0]);
136 return CMD_SUCCESS;
137}
138
139DEFUN(cfg_mgcp_bind_port,
140 cfg_mgcp_bind_port_cmd,
141 "bind port <0-65534>",
142 "Bind the MGCP to this port")
143{
144 unsigned int port = atoi(argv[0]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100145 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]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100155 g_cfg->early_bind = bind == 1;
156 return CMD_SUCCESS;
157}
158
159DEFUN(cfg_mgcp_rtp_base_port,
160 cfg_mgcp_rtp_base_port_cmd,
161 "rtp base <0-65534>",
162 "Base port to use")
163{
164 unsigned int port = atoi(argv[0]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100165 g_cfg->rtp_base_port = port;
166 return CMD_SUCCESS;
167}
168
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800169DEFUN(cfg_mgcp_rtp_ip_tos,
170 cfg_mgcp_rtp_ip_tos_cmd,
171 "rtp ip-tos <0-255>",
172 "Set the IP_TOS socket attribute on the RTP/RTCP sockets.\n" "The TOS value.")
173{
174 int tos = atoi(argv[0]);
175 g_cfg->endp_tos = tos;
176 return CMD_SUCCESS;
177}
178
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100179DEFUN(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]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100185 g_cfg->audio_payload = payload;
186 return CMD_SUCCESS;
187}
188
189DEFUN(cfg_mgcp_sdp_payload_name,
190 cfg_mgcp_sdp_payload_name_cmd,
191 "sdp audio payload name NAME",
192 "Set the audio name to use")
193{
194 if (g_cfg->audio_name)
195 talloc_free(g_cfg->audio_name);
196 g_cfg->audio_name = talloc_strdup(g_cfg, argv[0]);
197 return CMD_SUCCESS;
198}
199
200DEFUN(cfg_mgcp_loop,
201 cfg_mgcp_loop_cmd,
202 "loop (0|1)",
203 "Loop the audio")
204{
205 g_cfg->audio_loop = atoi(argv[0]);
206 return CMD_SUCCESS;
207}
208
209DEFUN(cfg_mgcp_number_endp,
210 cfg_mgcp_number_endp_cmd,
211 "number endpoints <0-65534>",
212 "The number of endpoints to allocate. This is not dynamic.")
213{
214 /* + 1 as we start counting at one */
215 g_cfg->number_endpoints = atoi(argv[0]) + 1;
216 return CMD_SUCCESS;
217}
218
219DEFUN(cfg_mgcp_forward_ip,
220 cfg_mgcp_forward_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800221 "forward audio ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100222 "Forward packets from and to the IP. This disables most of the MGCP feature.")
223{
224 if (g_cfg->forward_ip)
225 talloc_free(g_cfg->forward_ip);
226 g_cfg->forward_ip = talloc_strdup(g_cfg, argv[0]);
227 return CMD_SUCCESS;
228}
229
230DEFUN(cfg_mgcp_forward_port,
231 cfg_mgcp_forward_port_cmd,
232 "forward audio port <1-15000>",
233 "Forward packets from and to the port. This disables most of the MGCP feature.")
234{
235 g_cfg->forward_port = atoi(argv[0]);
236 return CMD_SUCCESS;
237}
238
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +0200239DEFUN(cfg_mgcp_agent_addr,
240 cfg_mgcp_agent_addr_cmd,
241 "call agent ip IP",
242 "Set the address of the call agent.")
243{
244 if (g_cfg->call_agent_addr)
245 talloc_free(g_cfg->call_agent_addr);
246 g_cfg->call_agent_addr = talloc_strdup(g_cfg, argv[0]);
247 return CMD_SUCCESS;
248}
249
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100250int mgcp_vty_init(void)
251{
Holger Hans Peter Freytherb5be7ac2010-05-14 02:45:52 +0800252 install_element_ve(&show_mgcp_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100253
254 install_element(CONFIG_NODE, &cfg_mgcp_cmd);
255 install_node(&mgcp_node, config_write_mgcp);
256 install_default(MGCP_NODE);
257 install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd);
258 install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd);
259 install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd);
260 install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd);
261 install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd);
262 install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd);
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800263 install_element(MGCP_NODE, &cfg_mgcp_rtp_ip_tos_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100264 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd);
265 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd);
266 install_element(MGCP_NODE, &cfg_mgcp_loop_cmd);
267 install_element(MGCP_NODE, &cfg_mgcp_number_endp_cmd);
268 install_element(MGCP_NODE, &cfg_mgcp_forward_ip_cmd);
269 install_element(MGCP_NODE, &cfg_mgcp_forward_port_cmd);
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +0200270 install_element(MGCP_NODE, &cfg_mgcp_agent_addr_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100271 return 0;
272}
273
274int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg)
275{
276 int i, rc;
277
278 g_cfg = cfg;
279 rc = vty_read_config_file(config_file);
280 if (rc < 0) {
281 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
282 return rc;
283 }
284
285
286 if (!g_cfg->bts_ip)
287 fprintf(stderr, "No BTS ip address specified. This will allow everyone to connect.\n");
288
Holger Hans Peter Freyther95e4d342010-03-30 13:00:10 +0200289 if (!g_cfg->source_addr) {
290 fprintf(stderr, "You need to specify a bind address.\n");
291 return -1;
292 }
293
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100294 if (mgcp_endpoints_allocate(g_cfg) != 0) {
295 fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n", g_cfg->number_endpoints);
296 return -1;
297 }
298
299 /*
300 * This application supports two modes.
Holger Hans Peter Freythera5811362010-04-30 13:32:05 +0800301 * 1.) a true MGCP gateway with support for AUEP, CRCX, MDCX, DLCX
302 * 2.) plain forwarding of RTP packets on the endpoints.
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100303 * both modes are mutual exclusive
304 */
305 if (g_cfg->forward_ip) {
306 int port = g_cfg->rtp_base_port;
307 if (g_cfg->forward_port != 0)
308 port = g_cfg->forward_port;
309
310 if (!g_cfg->early_bind) {
311 LOGP(DMGCP, LOGL_NOTICE, "Forwarding requires early bind.\n");
312 return -1;
313 }
314
315 /*
316 * Store the forward IP and assign a ci. For early bind
317 * the sockets will be created after this.
318 */
319 for (i = 1; i < g_cfg->number_endpoints; ++i) {
320 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
321 inet_aton(g_cfg->forward_ip, &endp->remote);
322 endp->ci = CI_UNUSED + 23;
323 endp->net_rtp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port));
324 endp->net_rtcp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port) + 1);
325 }
326
327 LOGP(DMGCP, LOGL_NOTICE, "Configured for Audio Forwarding.\n");
328 }
329
330 /* early bind */
331 if (g_cfg->early_bind) {
332 for (i = 1; i < g_cfg->number_endpoints; ++i) {
333 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
334 int rtp_port;
335
336 rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), g_cfg->rtp_base_port);
337 if (mgcp_bind_rtp_port(endp, rtp_port) != 0) {
Holger Hans Peter Freyther590cd982010-02-26 13:10:51 +0100338 LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100339 return -1;
340 }
341 }
342 }
343
344 return !!g_cfg->forward_ip;
345}
346