blob: ea10a2e6f85494da378bcb1c7cad1b9083fee703 [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 Freyther196349d2010-08-05 03:46:07 +080059 vty_out(vty, " rtp bts-base %u%s", g_cfg->rtp_bts_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
Holger Hans Peter Freyther196349d2010-08-05 03:46:07 +0800156DEFUN(cfg_mgcp_rtp_bts_base_port,
157 cfg_mgcp_rtp_bts_base_port_cmd,
158 "rtp bts-base <0-65534>",
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100159 "Base port to use")
160{
161 unsigned int port = atoi(argv[0]);
Holger Hans Peter Freyther196349d2010-08-05 03:46:07 +0800162 g_cfg->rtp_bts_base_port = port;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100163 return CMD_SUCCESS;
164}
165
Holger Hans Peter Freyther196349d2010-08-05 03:46:07 +0800166ALIAS_DEPRECATED(cfg_mgcp_rtp_bts_base_port, cfg_mgcp_rtp_base_port_cmd,
167 "rtp base <0-65534>", "Base port to use")
168
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +0800169DEFUN(cfg_mgcp_rtp_ip_dscp,
170 cfg_mgcp_rtp_ip_dscp_cmd,
171 "rtp ip-dscp <0-255>",
172 "Set the IP_TOS socket attribute on the RTP/RTCP sockets.\n" "The DSCP value.")
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800173{
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +0800174 int dscp = atoi(argv[0]);
175 g_cfg->endp_dscp = dscp;
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800176 return CMD_SUCCESS;
177}
178
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +0800179ALIAS_DEPRECATED(cfg_mgcp_rtp_ip_dscp, cfg_mgcp_rtp_ip_tos_cmd,
180 "rtp ip-tos <0-255>",
181 "Set the IP_TOS socket attribute on the RTP/RTCP sockets.\n" "The DSCP value.")
182
183
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100184DEFUN(cfg_mgcp_sdp_payload_number,
185 cfg_mgcp_sdp_payload_number_cmd,
186 "sdp audio payload number <1-255>",
187 "Set the audio codec to use")
188{
189 unsigned int payload = atoi(argv[0]);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100190 g_cfg->audio_payload = payload;
191 return CMD_SUCCESS;
192}
193
194DEFUN(cfg_mgcp_sdp_payload_name,
195 cfg_mgcp_sdp_payload_name_cmd,
196 "sdp audio payload name NAME",
197 "Set the audio name to use")
198{
199 if (g_cfg->audio_name)
200 talloc_free(g_cfg->audio_name);
201 g_cfg->audio_name = talloc_strdup(g_cfg, argv[0]);
202 return CMD_SUCCESS;
203}
204
205DEFUN(cfg_mgcp_loop,
206 cfg_mgcp_loop_cmd,
207 "loop (0|1)",
208 "Loop the audio")
209{
210 g_cfg->audio_loop = atoi(argv[0]);
211 return CMD_SUCCESS;
212}
213
214DEFUN(cfg_mgcp_number_endp,
215 cfg_mgcp_number_endp_cmd,
216 "number endpoints <0-65534>",
217 "The number of endpoints to allocate. This is not dynamic.")
218{
219 /* + 1 as we start counting at one */
220 g_cfg->number_endpoints = atoi(argv[0]) + 1;
221 return CMD_SUCCESS;
222}
223
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +0200224DEFUN(cfg_mgcp_agent_addr,
225 cfg_mgcp_agent_addr_cmd,
226 "call agent ip IP",
227 "Set the address of the call agent.")
228{
229 if (g_cfg->call_agent_addr)
230 talloc_free(g_cfg->call_agent_addr);
231 g_cfg->call_agent_addr = talloc_strdup(g_cfg, argv[0]);
232 return CMD_SUCCESS;
233}
234
Holger Hans Peter Freytherc597a4e2010-08-03 02:57:02 +0800235DEFUN(loop_endp,
236 loop_endp_cmd,
237 "loop-endpoint NAME (0|1)",
238 "Loop a given endpoint\n"
Holger Hans Peter Freytherebc824c2010-08-03 23:00:03 +0800239 "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 +0800240{
241 struct mgcp_endpoint *endp;
242
243 int endp_no = strtoul(argv[0], NULL, 16);
244 if (endp_no < 1 || endp_no >= g_cfg->number_endpoints) {
245 vty_out(vty, "Loopback number %s/%d is invalid.%s",
246 argv[0], endp_no, VTY_NEWLINE);
247 return CMD_WARNING;
248 }
249
250
251 endp = &g_cfg->endpoints[endp_no];
252 int loop = atoi(argv[1]);
253
254 if (loop)
255 endp->conn_mode = MGCP_CONN_LOOPBACK;
256 else
257 endp->conn_mode = endp->orig_mode;
258
259 return CMD_SUCCESS;
260}
261
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100262int mgcp_vty_init(void)
263{
Holger Hans Peter Freytherb5be7ac2010-05-14 02:45:52 +0800264 install_element_ve(&show_mgcp_cmd);
Holger Hans Peter Freytherc597a4e2010-08-03 02:57:02 +0800265 install_element(ENABLE_NODE, &loop_endp_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100266
267 install_element(CONFIG_NODE, &cfg_mgcp_cmd);
268 install_node(&mgcp_node, config_write_mgcp);
Holger Hans Peter Freytherc597a4e2010-08-03 02:57:02 +0800269
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100270 install_default(MGCP_NODE);
Harald Welte62ab20c2010-05-14 18:59:17 +0200271 install_element(MGCP_NODE, &ournode_exit_cmd);
Harald Welte54f74242010-05-14 19:11:04 +0200272 install_element(MGCP_NODE, &ournode_end_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100273 install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd);
274 install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd);
275 install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd);
276 install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd);
277 install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd);
278 install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd);
Holger Hans Peter Freyther196349d2010-08-05 03:46:07 +0800279 install_element(MGCP_NODE, &cfg_mgcp_rtp_bts_base_port_cmd);
Holger Hans Peter Freytherd0c32292010-07-27 20:34:45 +0800280 install_element(MGCP_NODE, &cfg_mgcp_rtp_ip_dscp_cmd);
Holger Hans Peter Freyther75492e62010-05-31 10:22:00 +0800281 install_element(MGCP_NODE, &cfg_mgcp_rtp_ip_tos_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100282 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd);
283 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd);
284 install_element(MGCP_NODE, &cfg_mgcp_loop_cmd);
285 install_element(MGCP_NODE, &cfg_mgcp_number_endp_cmd);
Holger Hans Peter Freytherb79994c2010-03-31 11:46:41 +0200286 install_element(MGCP_NODE, &cfg_mgcp_agent_addr_cmd);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100287 return 0;
288}
289
290int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg)
291{
292 int i, rc;
293
294 g_cfg = cfg;
Harald Weltedcccb182010-05-16 20:52:23 +0200295 rc = vty_read_config_file(config_file, NULL);
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100296 if (rc < 0) {
297 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
298 return rc;
299 }
300
301
302 if (!g_cfg->bts_ip)
303 fprintf(stderr, "No BTS ip address specified. This will allow everyone to connect.\n");
304
Holger Hans Peter Freyther95e4d342010-03-30 13:00:10 +0200305 if (!g_cfg->source_addr) {
306 fprintf(stderr, "You need to specify a bind address.\n");
307 return -1;
308 }
309
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100310 if (mgcp_endpoints_allocate(g_cfg) != 0) {
311 fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n", g_cfg->number_endpoints);
312 return -1;
313 }
314
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100315 /* early bind */
Holger Hans Peter Freytherf1a168d2010-08-05 03:22:24 +0800316 for (i = 1; i < g_cfg->number_endpoints; ++i) {
317 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
318 int rtp_port;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100319
Holger Hans Peter Freyther196349d2010-08-05 03:46:07 +0800320 rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), g_cfg->rtp_bts_base_port);
Holger Hans Peter Freyther217d7122010-08-05 03:27:41 +0800321 if (mgcp_bind_bts_rtp_port(endp, rtp_port) != 0) {
Holger Hans Peter Freytherf1a168d2010-08-05 03:22:24 +0800322 LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port);
323 return -1;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100324 }
325 }
326
Holger Hans Peter Freyther8b5772e2010-08-05 01:28:22 +0800327 return 0;
Holger Hans Peter Freyther7bdc6372010-02-20 21:21:02 +0100328}
329