blob: ffb94d934687c11920d89522cb2b5143f05eec43 [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);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010059 vty_out(vty, " rtp base %u%s", g_cfg->rtp_base_port, VTY_NEWLINE);
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +080060 vty_out(vty, " rtp ip-dscp %d%s", g_cfg->endp_dscp, 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 Freytherb79994c2010-03-31 11:46:41 +020067 if (g_cfg->call_agent_addr)
Holger Hans Peter Freyther8d9833e2010-04-16 16:59:48 +020068 vty_out(vty, " call agent ip %s%s", g_cfg->call_agent_addr, VTY_NEWLINE);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +010069
70 return CMD_SUCCESS;
71}
72
73DEFUN(show_mcgp, show_mgcp_cmd, "show mgcp",
74 SHOW_STR "Display information about the MGCP Media Gateway")
75{
76 int i;
77
78 vty_out(vty, "MGCP is up and running with %u endpoints:%s", g_cfg->number_endpoints - 1, VTY_NEWLINE);
79 for (i = 1; i < g_cfg->number_endpoints; ++i) {
80 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
Holger Hans Peter Freyther1aa42462010-07-29 02:43:14 +080081 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 +010082 i, endp->ci,
Holger Hans Peter Freythera17d7012010-08-05 01:34:51 +080083 ntohs(endp->net_end.rtp_port), ntohs(endp->net_end.rtcp_port),
84 ntohs(endp->bts_end.rtp_port), ntohs(endp->bts_end.rtcp_port),
85 inet_ntoa(endp->bts_end.addr),
86 endp->bts_end.packets, endp->bts_state.lost_no,
87 endp->net_end.packets, 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{
Holger Hans Peter Freytherf1a168d2010-08-05 03:22:24 +0800152 vty_out(vty, "bind early is deprecated, remove it from the config.\n");
153 return CMD_WARNING;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100154}
155
156DEFUN(cfg_mgcp_rtp_base_port,
157 cfg_mgcp_rtp_base_port_cmd,
158 "rtp base <0-65534>",
159 "Base port to use")
160{
161 unsigned int port = atoi(argv[0]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100162 g_cfg->rtp_base_port = port;
163 return CMD_SUCCESS;
164}
165
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +0800166DEFUN(cfg_mgcp_rtp_ip_dscp,
167 cfg_mgcp_rtp_ip_dscp_cmd,
168 "rtp ip-dscp <0-255>",
169 "Set the IP_TOS socket attribute on the RTP/RTCP sockets.\n" "The DSCP value.")
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800170{
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +0800171 int dscp = atoi(argv[0]);
172 g_cfg->endp_dscp = dscp;
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800173 return CMD_SUCCESS;
174}
175
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +0800176ALIAS_DEPRECATED(cfg_mgcp_rtp_ip_dscp, cfg_mgcp_rtp_ip_tos_cmd,
177 "rtp ip-tos <0-255>",
178 "Set the IP_TOS socket attribute on the RTP/RTCP sockets.\n" "The DSCP value.")
179
180
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100181DEFUN(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]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100187 g_cfg->audio_payload = payload;
188 return CMD_SUCCESS;
189}
190
191DEFUN(cfg_mgcp_sdp_payload_name,
192 cfg_mgcp_sdp_payload_name_cmd,
193 "sdp audio payload name NAME",
194 "Set the audio name to use")
195{
196 if (g_cfg->audio_name)
197 talloc_free(g_cfg->audio_name);
198 g_cfg->audio_name = talloc_strdup(g_cfg, argv[0]);
199 return CMD_SUCCESS;
200}
201
202DEFUN(cfg_mgcp_loop,
203 cfg_mgcp_loop_cmd,
204 "loop (0|1)",
205 "Loop the audio")
206{
207 g_cfg->audio_loop = atoi(argv[0]);
208 return CMD_SUCCESS;
209}
210
211DEFUN(cfg_mgcp_number_endp,
212 cfg_mgcp_number_endp_cmd,
213 "number endpoints <0-65534>",
214 "The number of endpoints to allocate. This is not dynamic.")
215{
216 /* + 1 as we start counting at one */
217 g_cfg->number_endpoints = atoi(argv[0]) + 1;
218 return CMD_SUCCESS;
219}
220
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +0200221DEFUN(cfg_mgcp_agent_addr,
222 cfg_mgcp_agent_addr_cmd,
223 "call agent ip IP",
224 "Set the address of the call agent.")
225{
226 if (g_cfg->call_agent_addr)
227 talloc_free(g_cfg->call_agent_addr);
228 g_cfg->call_agent_addr = talloc_strdup(g_cfg, argv[0]);
229 return CMD_SUCCESS;
230}
231
Holger Hans Peter Freytherc597a4e2010-08-03 02:57:02 +0800232DEFUN(loop_endp,
233 loop_endp_cmd,
234 "loop-endpoint NAME (0|1)",
235 "Loop a given endpoint\n"
Holger Hans Peter Freytherebc824c2010-08-03 23:00:03 +0800236 "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 +0800237{
238 struct mgcp_endpoint *endp;
239
240 int endp_no = strtoul(argv[0], NULL, 16);
241 if (endp_no < 1 || endp_no >= g_cfg->number_endpoints) {
242 vty_out(vty, "Loopback number %s/%d is invalid.%s",
243 argv[0], endp_no, VTY_NEWLINE);
244 return CMD_WARNING;
245 }
246
247
248 endp = &g_cfg->endpoints[endp_no];
249 int loop = atoi(argv[1]);
250
251 if (loop)
252 endp->conn_mode = MGCP_CONN_LOOPBACK;
253 else
254 endp->conn_mode = endp->orig_mode;
255
256 return CMD_SUCCESS;
257}
258
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100259int mgcp_vty_init(void)
260{
Holger Hans Peter Freytherb5be7ac2010-05-14 02:45:52 +0800261 install_element_ve(&show_mgcp_cmd);
Holger Hans Peter Freytherc597a4e2010-08-03 02:57:02 +0800262 install_element(ENABLE_NODE, &loop_endp_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100263
264 install_element(CONFIG_NODE, &cfg_mgcp_cmd);
265 install_node(&mgcp_node, config_write_mgcp);
Holger Hans Peter Freytherc597a4e2010-08-03 02:57:02 +0800266
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100267 install_default(MGCP_NODE);
Harald Welte62ab20c2010-05-14 18:59:17 +0200268 install_element(MGCP_NODE, &ournode_exit_cmd);
Harald Welte54f74242010-05-14 19:11:04 +0200269 install_element(MGCP_NODE, &ournode_end_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100270 install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd);
271 install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd);
272 install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd);
273 install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd);
274 install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd);
275 install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd);
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +0800276 install_element(MGCP_NODE, &cfg_mgcp_rtp_ip_dscp_cmd);
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800277 install_element(MGCP_NODE, &cfg_mgcp_rtp_ip_tos_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100278 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd);
279 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd);
280 install_element(MGCP_NODE, &cfg_mgcp_loop_cmd);
281 install_element(MGCP_NODE, &cfg_mgcp_number_endp_cmd);
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +0200282 install_element(MGCP_NODE, &cfg_mgcp_agent_addr_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100283 return 0;
284}
285
286int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg)
287{
288 int i, rc;
289
290 g_cfg = cfg;
Harald Weltedcccb182010-05-16 20:52:23 +0200291 rc = vty_read_config_file(config_file, NULL);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100292 if (rc < 0) {
293 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
294 return rc;
295 }
296
297
298 if (!g_cfg->bts_ip)
299 fprintf(stderr, "No BTS ip address specified. This will allow everyone to connect.\n");
300
Holger Hans Peter Freyther95e4d342010-03-30 13:00:10 +0200301 if (!g_cfg->source_addr) {
302 fprintf(stderr, "You need to specify a bind address.\n");
303 return -1;
304 }
305
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100306 if (mgcp_endpoints_allocate(g_cfg) != 0) {
307 fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n", g_cfg->number_endpoints);
308 return -1;
309 }
310
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100311 /* early bind */
Holger Hans Peter Freytherf1a168d2010-08-05 03:22:24 +0800312 for (i = 1; i < g_cfg->number_endpoints; ++i) {
313 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
314 int rtp_port;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100315
Holger Hans Peter Freytherf1a168d2010-08-05 03:22:24 +0800316 rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), g_cfg->rtp_base_port);
317 if (mgcp_bind_rtp_port(endp, rtp_port) != 0) {
318 LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port);
319 return -1;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100320 }
321 }
322
Holger Hans Peter Freyther8b5772e2010-08-05 01:28:22 +0800323 return 0;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100324}
325