blob: 84299d0c87a42cbede82f89144dc86c2c0d66be6 [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>
Harald Welte62ab20c2010-05-14 18:59:17 +020032#include <openbsc/vty.h>
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010033
Harald Welte4b037e42010-05-19 19:45:32 +020034#include <osmocom/vty/command.h>
35#include <osmocom/vty/vty.h>
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010036
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020037#include <string.h>
38
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010039static struct mgcp_config *g_cfg = NULL;
40
41/*
42 * vty code for mgcp below
43 */
44struct cmd_node mgcp_node = {
45 MGCP_NODE,
46 "%s(mgcp)#",
47 1,
48};
49
50static int config_write_mgcp(struct vty *vty)
51{
52 vty_out(vty, "mgcp%s", VTY_NEWLINE);
53 if (g_cfg->local_ip)
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020054 vty_out(vty, " local ip %s%s", g_cfg->local_ip, VTY_NEWLINE);
55 if (g_cfg->bts_ip && strlen(g_cfg->bts_ip) != 0)
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010056 vty_out(vty, " bts ip %s%s", g_cfg->bts_ip, VTY_NEWLINE);
57 vty_out(vty, " bind ip %s%s", g_cfg->source_addr, VTY_NEWLINE);
58 vty_out(vty, " bind port %u%s", g_cfg->source_port, VTY_NEWLINE);
59 vty_out(vty, " bind early %u%s", !!g_cfg->early_bind, VTY_NEWLINE);
60 vty_out(vty, " rtp base %u%s", g_cfg->rtp_base_port, VTY_NEWLINE);
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +080061 vty_out(vty, " rtp ip-dscp %d%s", g_cfg->endp_dscp, VTY_NEWLINE);
Holger Hans Peter Freyther2d425052010-04-13 09:28:40 +020062 if (g_cfg->audio_payload != -1)
63 vty_out(vty, " sdp audio payload number %d%s", g_cfg->audio_payload, VTY_NEWLINE);
64 if (g_cfg->audio_name)
65 vty_out(vty, " sdp audio payload name %s%s", g_cfg->audio_name, VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010066 vty_out(vty, " loop %u%s", !!g_cfg->audio_loop, VTY_NEWLINE);
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020067 vty_out(vty, " number endpoints %u%s", g_cfg->number_endpoints - 1, VTY_NEWLINE);
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +020068 if (g_cfg->call_agent_addr)
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020069 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 Freyther1aa42462010-07-29 02:43:14 +080082 vty_out(vty, " Endpoint 0x%.2x: CI: %d net: %u/%u bts: %u/%u on %s traffic received bts: %u/%u remote: %u/%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 Freyther31868922010-08-03 11:59:04 +000086 inet_ntoa(endp->bts), endp->in_bts, endp->bts_state.lost_no,
87 endp->in_remote, endp->net_state.lost_no,
Holger Hans Peter Freytherb4b135e2010-04-07 09:37:17 +020088 VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010089 }
90
91 return CMD_SUCCESS;
92}
93
94DEFUN(cfg_mgcp,
95 cfg_mgcp_cmd,
96 "mgcp",
97 "Configure the MGCP")
98{
99 vty->node = MGCP_NODE;
100 return CMD_SUCCESS;
101}
102
103DEFUN(cfg_mgcp_local_ip,
104 cfg_mgcp_local_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800105 "local ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100106 "Set the IP to be used in SDP records")
107{
108 if (g_cfg->local_ip)
109 talloc_free(g_cfg->local_ip);
110 g_cfg->local_ip = talloc_strdup(g_cfg, argv[0]);
111 return CMD_SUCCESS;
112}
113
114DEFUN(cfg_mgcp_bts_ip,
115 cfg_mgcp_bts_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800116 "bts ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100117 "Set the IP of the BTS for RTP forwarding")
118{
119 if (g_cfg->bts_ip)
120 talloc_free(g_cfg->bts_ip);
121 g_cfg->bts_ip = talloc_strdup(g_cfg, argv[0]);
122 inet_aton(g_cfg->bts_ip, &g_cfg->bts_in);
123 return CMD_SUCCESS;
124}
125
126DEFUN(cfg_mgcp_bind_ip,
127 cfg_mgcp_bind_ip_cmd,
Holger Hans Peter Freyther1384af62010-05-14 02:27:50 +0800128 "bind ip A.B.C.D",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100129 "Bind the MGCP to this local addr")
130{
131 if (g_cfg->source_addr)
132 talloc_free(g_cfg->source_addr);
133 g_cfg->source_addr = talloc_strdup(g_cfg, argv[0]);
134 return CMD_SUCCESS;
135}
136
137DEFUN(cfg_mgcp_bind_port,
138 cfg_mgcp_bind_port_cmd,
139 "bind port <0-65534>",
140 "Bind the MGCP to this port")
141{
142 unsigned int port = atoi(argv[0]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100143 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]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100153 g_cfg->early_bind = bind == 1;
154 return CMD_SUCCESS;
155}
156
157DEFUN(cfg_mgcp_rtp_base_port,
158 cfg_mgcp_rtp_base_port_cmd,
159 "rtp base <0-65534>",
160 "Base port to use")
161{
162 unsigned int port = atoi(argv[0]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100163 g_cfg->rtp_base_port = port;
164 return CMD_SUCCESS;
165}
166
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +0800167DEFUN(cfg_mgcp_rtp_ip_dscp,
168 cfg_mgcp_rtp_ip_dscp_cmd,
169 "rtp ip-dscp <0-255>",
170 "Set the IP_TOS socket attribute on the RTP/RTCP sockets.\n" "The DSCP value.")
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800171{
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +0800172 int dscp = atoi(argv[0]);
173 g_cfg->endp_dscp = dscp;
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800174 return CMD_SUCCESS;
175}
176
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +0800177ALIAS_DEPRECATED(cfg_mgcp_rtp_ip_dscp, cfg_mgcp_rtp_ip_tos_cmd,
178 "rtp ip-tos <0-255>",
179 "Set the IP_TOS socket attribute on the RTP/RTCP sockets.\n" "The DSCP value.")
180
181
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100182DEFUN(cfg_mgcp_sdp_payload_number,
183 cfg_mgcp_sdp_payload_number_cmd,
184 "sdp audio payload number <1-255>",
185 "Set the audio codec to use")
186{
187 unsigned int payload = atoi(argv[0]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100188 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
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +0200222DEFUN(cfg_mgcp_agent_addr,
223 cfg_mgcp_agent_addr_cmd,
224 "call agent ip IP",
225 "Set the address of the call agent.")
226{
227 if (g_cfg->call_agent_addr)
228 talloc_free(g_cfg->call_agent_addr);
229 g_cfg->call_agent_addr = talloc_strdup(g_cfg, argv[0]);
230 return CMD_SUCCESS;
231}
232
Holger Hans Peter Freytherc597a4e2010-08-03 02:57:02 +0800233DEFUN(loop_endp,
234 loop_endp_cmd,
235 "loop-endpoint NAME (0|1)",
236 "Loop a given endpoint\n"
Holger Hans Peter Freytherebc824c2010-08-03 23:00:03 +0800237 "The name in hex of the endpoint\n" "Disable the loop\n" "Enable the loop\n")
Holger Hans Peter Freytherc597a4e2010-08-03 02:57:02 +0800238{
239 struct mgcp_endpoint *endp;
240
241 int endp_no = strtoul(argv[0], NULL, 16);
242 if (endp_no < 1 || endp_no >= g_cfg->number_endpoints) {
243 vty_out(vty, "Loopback number %s/%d is invalid.%s",
244 argv[0], endp_no, VTY_NEWLINE);
245 return CMD_WARNING;
246 }
247
248
249 endp = &g_cfg->endpoints[endp_no];
250 int loop = atoi(argv[1]);
251
252 if (loop)
253 endp->conn_mode = MGCP_CONN_LOOPBACK;
254 else
255 endp->conn_mode = endp->orig_mode;
256
257 return CMD_SUCCESS;
258}
259
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100260int mgcp_vty_init(void)
261{
Holger Hans Peter Freytherb5be7ac2010-05-14 02:45:52 +0800262 install_element_ve(&show_mgcp_cmd);
Holger Hans Peter Freytherc597a4e2010-08-03 02:57:02 +0800263 install_element(ENABLE_NODE, &loop_endp_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100264
265 install_element(CONFIG_NODE, &cfg_mgcp_cmd);
266 install_node(&mgcp_node, config_write_mgcp);
Holger Hans Peter Freytherc597a4e2010-08-03 02:57:02 +0800267
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100268 install_default(MGCP_NODE);
Harald Welte62ab20c2010-05-14 18:59:17 +0200269 install_element(MGCP_NODE, &ournode_exit_cmd);
Harald Welte54f74242010-05-14 19:11:04 +0200270 install_element(MGCP_NODE, &ournode_end_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100271 install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd);
272 install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd);
273 install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd);
274 install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd);
275 install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd);
276 install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd);
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +0800277 install_element(MGCP_NODE, &cfg_mgcp_rtp_ip_dscp_cmd);
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800278 install_element(MGCP_NODE, &cfg_mgcp_rtp_ip_tos_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100279 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd);
280 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd);
281 install_element(MGCP_NODE, &cfg_mgcp_loop_cmd);
282 install_element(MGCP_NODE, &cfg_mgcp_number_endp_cmd);
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +0200283 install_element(MGCP_NODE, &cfg_mgcp_agent_addr_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100284 return 0;
285}
286
287int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg)
288{
289 int i, rc;
290
291 g_cfg = cfg;
Harald Weltedcccb182010-05-16 20:52:23 +0200292 rc = vty_read_config_file(config_file, NULL);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100293 if (rc < 0) {
294 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
295 return rc;
296 }
297
298
299 if (!g_cfg->bts_ip)
300 fprintf(stderr, "No BTS ip address specified. This will allow everyone to connect.\n");
301
Holger Hans Peter Freyther95e4d342010-03-30 13:00:10 +0200302 if (!g_cfg->source_addr) {
303 fprintf(stderr, "You need to specify a bind address.\n");
304 return -1;
305 }
306
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100307 if (mgcp_endpoints_allocate(g_cfg) != 0) {
308 fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n", g_cfg->number_endpoints);
309 return -1;
310 }
311
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100312 /* early bind */
313 if (g_cfg->early_bind) {
314 for (i = 1; i < g_cfg->number_endpoints; ++i) {
315 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
316 int rtp_port;
317
318 rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), g_cfg->rtp_base_port);
319 if (mgcp_bind_rtp_port(endp, rtp_port) != 0) {
Holger Hans Peter Freyther590cd982010-02-26 13:10:51 +0100320 LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100321 return -1;
322 }
323 }
324 }
325
Holger Hans Peter Freyther8b5772e2010-08-05 01:28:22 +0800326 return 0;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100327}
328