blob: 59386c545d89666bbf940972b9d7e8334afa47fc [file] [log] [blame]
Holger Hans Peter Freytherfde865f2010-08-04 06:39:13 +08001/* 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
27#include <cellmgr_debug.h>
28
29#include <mgcp/mgcp.h>
30#include <mgcp/mgcp_internal.h>
31
32#include <osmocore/talloc.h>
33
34#include <osmocom/vty/command.h>
35#include <osmocom/vty/vty.h>
36
37#include <string.h>
38
39static struct mgcp_config *g_cfg = NULL;
40
41enum cellmgr_node {
42 MGCP_NODE = _LAST_OSMOVTY_NODE + 1,
43};
44
45/*
46 * vty code for mgcp below
47 */
48static struct cmd_node mgcp_node = {
49 MGCP_NODE,
50 "%s(mgcp)#",
51 1,
52};
53
54static int config_write_mgcp(struct vty *vty)
55{
56 vty_out(vty, "mgcp%s", VTY_NEWLINE);
57 if (g_cfg->local_ip)
58 vty_out(vty, " local ip %s%s", g_cfg->local_ip, VTY_NEWLINE);
59 if (g_cfg->bts_ip && strlen(g_cfg->bts_ip) != 0)
Holger Hans Peter Freythera1743182010-08-04 07:28:39 +080060 vty_out(vty, " mgw ip %s%s", g_cfg->bts_ip, VTY_NEWLINE);
Holger Hans Peter Freytherfde865f2010-08-04 06:39:13 +080061 vty_out(vty, " bind ip %s%s", g_cfg->source_addr, VTY_NEWLINE);
62 vty_out(vty, " bind port %u%s", g_cfg->source_port, VTY_NEWLINE);
Holger Hans Peter Freytherfde865f2010-08-04 06:39:13 +080063 vty_out(vty, " rtp base %u%s", g_cfg->rtp_base_port, VTY_NEWLINE);
64 vty_out(vty, " rtp ip-dscp %d%s", g_cfg->endp_dscp, VTY_NEWLINE);
65 if (g_cfg->audio_payload != -1)
66 vty_out(vty, " sdp audio payload number %d%s", g_cfg->audio_payload, VTY_NEWLINE);
67 if (g_cfg->audio_name)
68 vty_out(vty, " sdp audio payload name %s%s", g_cfg->audio_name, VTY_NEWLINE);
69 vty_out(vty, " loop %u%s", !!g_cfg->audio_loop, VTY_NEWLINE);
70 vty_out(vty, " number endpoints %u%s", g_cfg->number_endpoints - 1, VTY_NEWLINE);
Holger Hans Peter Freytherfde865f2010-08-04 06:39:13 +080071
72 return CMD_SUCCESS;
73}
74
75DEFUN(show_mcgp, show_mgcp_cmd, "show mgcp",
76 SHOW_STR "Display information about the MGCP Media Gateway")
77{
78 int i;
79
80 vty_out(vty, "MGCP is up and running with %u endpoints:%s", g_cfg->number_endpoints - 1, VTY_NEWLINE);
81 for (i = 1; i < g_cfg->number_endpoints; ++i) {
82 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
83 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",
84 i, endp->ci,
85 ntohs(endp->net_rtp), ntohs(endp->net_rtcp),
86 ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp),
87 inet_ntoa(endp->bts), endp->in_bts, endp->bts_state.lost_no,
88 endp->in_remote, endp->net_state.lost_no,
89 VTY_NEWLINE);
90 }
91
92 return CMD_SUCCESS;
93}
94
95DEFUN(cfg_mgcp,
96 cfg_mgcp_cmd,
97 "mgcp",
98 "Configure the MGCP")
99{
100 vty->node = MGCP_NODE;
101 return CMD_SUCCESS;
102}
103
104DEFUN(cfg_mgcp_local_ip,
105 cfg_mgcp_local_ip_cmd,
106 "local ip A.B.C.D",
107 "Set the IP to be used in SDP records")
108{
109 if (g_cfg->local_ip)
110 talloc_free(g_cfg->local_ip);
111 g_cfg->local_ip = talloc_strdup(g_cfg, argv[0]);
112 return CMD_SUCCESS;
113}
114
115DEFUN(cfg_mgcp_bts_ip,
116 cfg_mgcp_bts_ip_cmd,
117 "bts ip A.B.C.D",
118 "Set the IP of the BTS for RTP forwarding")
119{
120 if (g_cfg->bts_ip)
121 talloc_free(g_cfg->bts_ip);
122 g_cfg->bts_ip = talloc_strdup(g_cfg, argv[0]);
123 inet_aton(g_cfg->bts_ip, &g_cfg->bts_in);
124 return CMD_SUCCESS;
125}
126
127DEFUN(cfg_mgcp_bind_ip,
128 cfg_mgcp_bind_ip_cmd,
129 "bind ip A.B.C.D",
130 "Bind the MGCP to this local addr")
131{
132 if (g_cfg->source_addr)
133 talloc_free(g_cfg->source_addr);
134 g_cfg->source_addr = talloc_strdup(g_cfg, argv[0]);
135 return CMD_SUCCESS;
136}
137
138DEFUN(cfg_mgcp_bind_port,
139 cfg_mgcp_bind_port_cmd,
140 "bind port <0-65534>",
141 "Bind the MGCP to this port")
142{
143 unsigned int port = atoi(argv[0]);
144 g_cfg->source_port = port;
145 return CMD_SUCCESS;
146}
147
Holger Hans Peter Freytherfde865f2010-08-04 06:39:13 +0800148DEFUN(cfg_mgcp_rtp_base_port,
149 cfg_mgcp_rtp_base_port_cmd,
150 "rtp base <0-65534>",
151 "Base port to use")
152{
153 unsigned int port = atoi(argv[0]);
154 g_cfg->rtp_base_port = port;
155 return CMD_SUCCESS;
156}
157
158DEFUN(cfg_mgcp_rtp_ip_dscp,
159 cfg_mgcp_rtp_ip_dscp_cmd,
160 "rtp ip-dscp <0-255>",
161 "Set the IP_TOS socket attribute on the RTP/RTCP sockets.\n" "The DSCP value.")
162{
163 int dscp = atoi(argv[0]);
164 g_cfg->endp_dscp = dscp;
165 return CMD_SUCCESS;
166}
167
168ALIAS_DEPRECATED(cfg_mgcp_rtp_ip_dscp, cfg_mgcp_rtp_ip_tos_cmd,
169 "rtp ip-tos <0-255>",
170 "Set the IP_TOS socket attribute on the RTP/RTCP sockets.\n" "The DSCP value.")
171
172
173DEFUN(cfg_mgcp_sdp_payload_number,
174 cfg_mgcp_sdp_payload_number_cmd,
175 "sdp audio payload number <1-255>",
176 "Set the audio codec to use")
177{
178 unsigned int payload = atoi(argv[0]);
179 g_cfg->audio_payload = payload;
180 return CMD_SUCCESS;
181}
182
183DEFUN(cfg_mgcp_sdp_payload_name,
184 cfg_mgcp_sdp_payload_name_cmd,
185 "sdp audio payload name NAME",
186 "Set the audio name to use")
187{
188 if (g_cfg->audio_name)
189 talloc_free(g_cfg->audio_name);
190 g_cfg->audio_name = talloc_strdup(g_cfg, argv[0]);
191 return CMD_SUCCESS;
192}
193
194DEFUN(cfg_mgcp_loop,
195 cfg_mgcp_loop_cmd,
196 "loop (0|1)",
197 "Loop the audio")
198{
199 g_cfg->audio_loop = atoi(argv[0]);
200 return CMD_SUCCESS;
201}
202
203DEFUN(cfg_mgcp_number_endp,
204 cfg_mgcp_number_endp_cmd,
205 "number endpoints <0-65534>",
206 "The number of endpoints to allocate. This is not dynamic.")
207{
208 /* + 1 as we start counting at one */
209 g_cfg->number_endpoints = atoi(argv[0]) + 1;
210 return CMD_SUCCESS;
211}
212
Holger Hans Peter Freytherfde865f2010-08-04 06:39:13 +0800213DEFUN(loop_endp,
214 loop_endp_cmd,
215 "loop-endpoint NAME (0|1)",
216 "Loop a given endpoint\n"
217 "The name in hex of the endpoint\n" "Disable the loop\n" "Enable the loop\n")
218{
219 struct mgcp_endpoint *endp;
220
221 int endp_no = strtoul(argv[0], NULL, 16);
222 if (endp_no < 1 || endp_no >= g_cfg->number_endpoints) {
223 vty_out(vty, "Loopback number %s/%d is invalid.%s",
224 argv[0], endp_no, VTY_NEWLINE);
225 return CMD_WARNING;
226 }
227
228
229 endp = &g_cfg->endpoints[endp_no];
230 int loop = atoi(argv[1]);
231
232 if (loop)
233 endp->conn_mode = MGCP_CONN_LOOPBACK;
234 else
235 endp->conn_mode = endp->orig_mode;
236
237 return CMD_SUCCESS;
238}
239
240DEFUN(ournode_exit,
241 ournode_exit_cmd, "exit", "Exit current mode and down to previous node\n")
242{
243 switch (vty->node) {
244 case MGCP_NODE:
245 vty->node = CONFIG_NODE;
246 vty->index = NULL;
247 break;
248 }
249
250 return CMD_SUCCESS;
251}
252
253DEFUN(ournode_end,
254 ournode_end_cmd, "end", "End current mode and change to enable mode.")
255{
256 switch (vty->node) {
257 case VIEW_NODE:
258 case ENABLE_NODE:
259 break;
260 case MGCP_NODE:
261 vty_config_unlock(vty);
262 vty->node = ENABLE_NODE;
263 vty->index = NULL;
264 vty->index_sub = NULL;
265 break;
266 }
267
268 return CMD_SUCCESS;
269}
270
271int mgcp_vty_init(void)
272{
273 install_element_ve(&show_mgcp_cmd);
274 install_element(ENABLE_NODE, &loop_endp_cmd);
275
276 install_element(CONFIG_NODE, &cfg_mgcp_cmd);
277 install_node(&mgcp_node, config_write_mgcp);
278
279 install_default(MGCP_NODE);
280 install_element(MGCP_NODE, &ournode_exit_cmd);
281 install_element(MGCP_NODE, &ournode_end_cmd);
282 install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd);
283 install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd);
284 install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd);
285 install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd);
Holger Hans Peter Freytherfde865f2010-08-04 06:39:13 +0800286 install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd);
287 install_element(MGCP_NODE, &cfg_mgcp_rtp_ip_dscp_cmd);
288 install_element(MGCP_NODE, &cfg_mgcp_rtp_ip_tos_cmd);
289 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd);
290 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd);
291 install_element(MGCP_NODE, &cfg_mgcp_loop_cmd);
292 install_element(MGCP_NODE, &cfg_mgcp_number_endp_cmd);
Holger Hans Peter Freytherfde865f2010-08-04 06:39:13 +0800293 return 0;
294}
295
296int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg)
297{
298 int i, rc;
299
300 g_cfg = cfg;
301 rc = vty_read_config_file(config_file, NULL);
302 if (rc < 0) {
303 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
304 return rc;
305 }
306
307
308 if (!g_cfg->bts_ip)
309 fprintf(stderr, "No BTS ip address specified. This will allow everyone to connect.\n");
310
311 if (!g_cfg->source_addr) {
312 fprintf(stderr, "You need to specify a bind address.\n");
313 return -1;
314 }
315
316 if (mgcp_endpoints_allocate(g_cfg) != 0) {
317 fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n", g_cfg->number_endpoints);
318 return -1;
319 }
320
321 /*
322 * This application supports two modes.
323 * 1.) a true MGCP gateway with support for AUEP, CRCX, MDCX, DLCX
324 * 2.) plain forwarding of RTP packets on the endpoints.
325 * both modes are mutual exclusive
326 */
327 if (g_cfg->forward_ip) {
328 int port = g_cfg->rtp_base_port;
329 if (g_cfg->forward_port != 0)
330 port = g_cfg->forward_port;
331
332 if (!g_cfg->early_bind) {
333 LOGP(DMGCP, LOGL_NOTICE, "Forwarding requires early bind.\n");
334 return -1;
335 }
336
337 /*
338 * Store the forward IP and assign a ci. For early bind
339 * the sockets will be created after this.
340 */
341 for (i = 1; i < g_cfg->number_endpoints; ++i) {
342 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
343 inet_aton(g_cfg->forward_ip, &endp->remote);
344 endp->ci = CI_UNUSED + 23;
345 endp->net_rtp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port));
346 endp->net_rtcp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port) + 1);
347 }
348
349 LOGP(DMGCP, LOGL_NOTICE, "Configured for Audio Forwarding.\n");
350 }
351
352 /* early bind */
353 if (g_cfg->early_bind) {
354 for (i = 1; i < g_cfg->number_endpoints; ++i) {
355 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
356 int rtp_port;
357
358 rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), g_cfg->rtp_base_port);
359 if (mgcp_bind_rtp_port(endp, rtp_port) != 0) {
360 LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port);
361 return -1;
362 }
363 }
364 }
365
366 return !!g_cfg->forward_ip;
367}
368