blob: af53a399071b7bfcdb97b9ae2aa6a18bfbffcbb4 [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)
60 vty_out(vty, " bts ip %s%s", g_cfg->bts_ip, VTY_NEWLINE);
61 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);
63 vty_out(vty, " bind early %u%s", !!g_cfg->early_bind, VTY_NEWLINE);
64 vty_out(vty, " rtp base %u%s", g_cfg->rtp_base_port, VTY_NEWLINE);
65 vty_out(vty, " rtp ip-dscp %d%s", g_cfg->endp_dscp, VTY_NEWLINE);
66 if (g_cfg->audio_payload != -1)
67 vty_out(vty, " sdp audio payload number %d%s", g_cfg->audio_payload, VTY_NEWLINE);
68 if (g_cfg->audio_name)
69 vty_out(vty, " sdp audio payload name %s%s", g_cfg->audio_name, VTY_NEWLINE);
70 vty_out(vty, " loop %u%s", !!g_cfg->audio_loop, VTY_NEWLINE);
71 vty_out(vty, " number endpoints %u%s", g_cfg->number_endpoints - 1, VTY_NEWLINE);
72 if (g_cfg->forward_ip)
73 vty_out(vty, " forward audio ip %s%s", g_cfg->forward_ip, VTY_NEWLINE);
74 if (g_cfg->forward_port != 0)
75 vty_out(vty, " forward audio port %d%s", g_cfg->forward_port, VTY_NEWLINE);
76 if (g_cfg->call_agent_addr)
77 vty_out(vty, " call agent ip %s%s", g_cfg->call_agent_addr, VTY_NEWLINE);
78
79 return CMD_SUCCESS;
80}
81
82DEFUN(show_mcgp, show_mgcp_cmd, "show mgcp",
83 SHOW_STR "Display information about the MGCP Media Gateway")
84{
85 int i;
86
87 vty_out(vty, "MGCP is up and running with %u endpoints:%s", g_cfg->number_endpoints - 1, VTY_NEWLINE);
88 for (i = 1; i < g_cfg->number_endpoints; ++i) {
89 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
90 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",
91 i, endp->ci,
92 ntohs(endp->net_rtp), ntohs(endp->net_rtcp),
93 ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp),
94 inet_ntoa(endp->bts), endp->in_bts, endp->bts_state.lost_no,
95 endp->in_remote, endp->net_state.lost_no,
96 VTY_NEWLINE);
97 }
98
99 return CMD_SUCCESS;
100}
101
102DEFUN(cfg_mgcp,
103 cfg_mgcp_cmd,
104 "mgcp",
105 "Configure the MGCP")
106{
107 vty->node = MGCP_NODE;
108 return CMD_SUCCESS;
109}
110
111DEFUN(cfg_mgcp_local_ip,
112 cfg_mgcp_local_ip_cmd,
113 "local ip A.B.C.D",
114 "Set the IP to be used in SDP records")
115{
116 if (g_cfg->local_ip)
117 talloc_free(g_cfg->local_ip);
118 g_cfg->local_ip = talloc_strdup(g_cfg, argv[0]);
119 return CMD_SUCCESS;
120}
121
122DEFUN(cfg_mgcp_bts_ip,
123 cfg_mgcp_bts_ip_cmd,
124 "bts ip A.B.C.D",
125 "Set the IP of the BTS for RTP forwarding")
126{
127 if (g_cfg->bts_ip)
128 talloc_free(g_cfg->bts_ip);
129 g_cfg->bts_ip = talloc_strdup(g_cfg, argv[0]);
130 inet_aton(g_cfg->bts_ip, &g_cfg->bts_in);
131 return CMD_SUCCESS;
132}
133
134DEFUN(cfg_mgcp_bind_ip,
135 cfg_mgcp_bind_ip_cmd,
136 "bind ip A.B.C.D",
137 "Bind the MGCP to this local addr")
138{
139 if (g_cfg->source_addr)
140 talloc_free(g_cfg->source_addr);
141 g_cfg->source_addr = talloc_strdup(g_cfg, argv[0]);
142 return CMD_SUCCESS;
143}
144
145DEFUN(cfg_mgcp_bind_port,
146 cfg_mgcp_bind_port_cmd,
147 "bind port <0-65534>",
148 "Bind the MGCP to this port")
149{
150 unsigned int port = atoi(argv[0]);
151 g_cfg->source_port = port;
152 return CMD_SUCCESS;
153}
154
155DEFUN(cfg_mgcp_bind_early,
156 cfg_mgcp_bind_early_cmd,
157 "bind early (0|1)",
158 "Bind all RTP ports early")
159{
160 unsigned int bind = atoi(argv[0]);
161 g_cfg->early_bind = bind == 1;
162 return CMD_SUCCESS;
163}
164
165DEFUN(cfg_mgcp_rtp_base_port,
166 cfg_mgcp_rtp_base_port_cmd,
167 "rtp base <0-65534>",
168 "Base port to use")
169{
170 unsigned int port = atoi(argv[0]);
171 g_cfg->rtp_base_port = port;
172 return CMD_SUCCESS;
173}
174
175DEFUN(cfg_mgcp_rtp_ip_dscp,
176 cfg_mgcp_rtp_ip_dscp_cmd,
177 "rtp ip-dscp <0-255>",
178 "Set the IP_TOS socket attribute on the RTP/RTCP sockets.\n" "The DSCP value.")
179{
180 int dscp = atoi(argv[0]);
181 g_cfg->endp_dscp = dscp;
182 return CMD_SUCCESS;
183}
184
185ALIAS_DEPRECATED(cfg_mgcp_rtp_ip_dscp, cfg_mgcp_rtp_ip_tos_cmd,
186 "rtp ip-tos <0-255>",
187 "Set the IP_TOS socket attribute on the RTP/RTCP sockets.\n" "The DSCP value.")
188
189
190DEFUN(cfg_mgcp_sdp_payload_number,
191 cfg_mgcp_sdp_payload_number_cmd,
192 "sdp audio payload number <1-255>",
193 "Set the audio codec to use")
194{
195 unsigned int payload = atoi(argv[0]);
196 g_cfg->audio_payload = payload;
197 return CMD_SUCCESS;
198}
199
200DEFUN(cfg_mgcp_sdp_payload_name,
201 cfg_mgcp_sdp_payload_name_cmd,
202 "sdp audio payload name NAME",
203 "Set the audio name to use")
204{
205 if (g_cfg->audio_name)
206 talloc_free(g_cfg->audio_name);
207 g_cfg->audio_name = talloc_strdup(g_cfg, argv[0]);
208 return CMD_SUCCESS;
209}
210
211DEFUN(cfg_mgcp_loop,
212 cfg_mgcp_loop_cmd,
213 "loop (0|1)",
214 "Loop the audio")
215{
216 g_cfg->audio_loop = atoi(argv[0]);
217 return CMD_SUCCESS;
218}
219
220DEFUN(cfg_mgcp_number_endp,
221 cfg_mgcp_number_endp_cmd,
222 "number endpoints <0-65534>",
223 "The number of endpoints to allocate. This is not dynamic.")
224{
225 /* + 1 as we start counting at one */
226 g_cfg->number_endpoints = atoi(argv[0]) + 1;
227 return CMD_SUCCESS;
228}
229
230DEFUN(cfg_mgcp_forward_ip,
231 cfg_mgcp_forward_ip_cmd,
232 "forward audio ip A.B.C.D",
233 "Forward packets from and to the IP. This disables most of the MGCP feature.")
234{
235 if (g_cfg->forward_ip)
236 talloc_free(g_cfg->forward_ip);
237 g_cfg->forward_ip = talloc_strdup(g_cfg, argv[0]);
238 return CMD_SUCCESS;
239}
240
241DEFUN(cfg_mgcp_forward_port,
242 cfg_mgcp_forward_port_cmd,
243 "forward audio port <1-15000>",
244 "Forward packets from and to the port. This disables most of the MGCP feature.")
245{
246 g_cfg->forward_port = atoi(argv[0]);
247 return CMD_SUCCESS;
248}
249
250DEFUN(cfg_mgcp_agent_addr,
251 cfg_mgcp_agent_addr_cmd,
252 "call agent ip IP",
253 "Set the address of the call agent.")
254{
255 if (g_cfg->call_agent_addr)
256 talloc_free(g_cfg->call_agent_addr);
257 g_cfg->call_agent_addr = talloc_strdup(g_cfg, argv[0]);
258 return CMD_SUCCESS;
259}
260
261DEFUN(loop_endp,
262 loop_endp_cmd,
263 "loop-endpoint NAME (0|1)",
264 "Loop a given endpoint\n"
265 "The name in hex of the endpoint\n" "Disable the loop\n" "Enable the loop\n")
266{
267 struct mgcp_endpoint *endp;
268
269 int endp_no = strtoul(argv[0], NULL, 16);
270 if (endp_no < 1 || endp_no >= g_cfg->number_endpoints) {
271 vty_out(vty, "Loopback number %s/%d is invalid.%s",
272 argv[0], endp_no, VTY_NEWLINE);
273 return CMD_WARNING;
274 }
275
276
277 endp = &g_cfg->endpoints[endp_no];
278 int loop = atoi(argv[1]);
279
280 if (loop)
281 endp->conn_mode = MGCP_CONN_LOOPBACK;
282 else
283 endp->conn_mode = endp->orig_mode;
284
285 return CMD_SUCCESS;
286}
287
288DEFUN(ournode_exit,
289 ournode_exit_cmd, "exit", "Exit current mode and down to previous node\n")
290{
291 switch (vty->node) {
292 case MGCP_NODE:
293 vty->node = CONFIG_NODE;
294 vty->index = NULL;
295 break;
296 }
297
298 return CMD_SUCCESS;
299}
300
301DEFUN(ournode_end,
302 ournode_end_cmd, "end", "End current mode and change to enable mode.")
303{
304 switch (vty->node) {
305 case VIEW_NODE:
306 case ENABLE_NODE:
307 break;
308 case MGCP_NODE:
309 vty_config_unlock(vty);
310 vty->node = ENABLE_NODE;
311 vty->index = NULL;
312 vty->index_sub = NULL;
313 break;
314 }
315
316 return CMD_SUCCESS;
317}
318
319int mgcp_vty_init(void)
320{
321 install_element_ve(&show_mgcp_cmd);
322 install_element(ENABLE_NODE, &loop_endp_cmd);
323
324 install_element(CONFIG_NODE, &cfg_mgcp_cmd);
325 install_node(&mgcp_node, config_write_mgcp);
326
327 install_default(MGCP_NODE);
328 install_element(MGCP_NODE, &ournode_exit_cmd);
329 install_element(MGCP_NODE, &ournode_end_cmd);
330 install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd);
331 install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd);
332 install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd);
333 install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd);
334 install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd);
335 install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd);
336 install_element(MGCP_NODE, &cfg_mgcp_rtp_ip_dscp_cmd);
337 install_element(MGCP_NODE, &cfg_mgcp_rtp_ip_tos_cmd);
338 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd);
339 install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd);
340 install_element(MGCP_NODE, &cfg_mgcp_loop_cmd);
341 install_element(MGCP_NODE, &cfg_mgcp_number_endp_cmd);
342 install_element(MGCP_NODE, &cfg_mgcp_forward_ip_cmd);
343 install_element(MGCP_NODE, &cfg_mgcp_forward_port_cmd);
344 install_element(MGCP_NODE, &cfg_mgcp_agent_addr_cmd);
345 return 0;
346}
347
348int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg)
349{
350 int i, rc;
351
352 g_cfg = cfg;
353 rc = vty_read_config_file(config_file, NULL);
354 if (rc < 0) {
355 fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
356 return rc;
357 }
358
359
360 if (!g_cfg->bts_ip)
361 fprintf(stderr, "No BTS ip address specified. This will allow everyone to connect.\n");
362
363 if (!g_cfg->source_addr) {
364 fprintf(stderr, "You need to specify a bind address.\n");
365 return -1;
366 }
367
368 if (mgcp_endpoints_allocate(g_cfg) != 0) {
369 fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n", g_cfg->number_endpoints);
370 return -1;
371 }
372
373 /*
374 * This application supports two modes.
375 * 1.) a true MGCP gateway with support for AUEP, CRCX, MDCX, DLCX
376 * 2.) plain forwarding of RTP packets on the endpoints.
377 * both modes are mutual exclusive
378 */
379 if (g_cfg->forward_ip) {
380 int port = g_cfg->rtp_base_port;
381 if (g_cfg->forward_port != 0)
382 port = g_cfg->forward_port;
383
384 if (!g_cfg->early_bind) {
385 LOGP(DMGCP, LOGL_NOTICE, "Forwarding requires early bind.\n");
386 return -1;
387 }
388
389 /*
390 * Store the forward IP and assign a ci. For early bind
391 * the sockets will be created after this.
392 */
393 for (i = 1; i < g_cfg->number_endpoints; ++i) {
394 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
395 inet_aton(g_cfg->forward_ip, &endp->remote);
396 endp->ci = CI_UNUSED + 23;
397 endp->net_rtp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port));
398 endp->net_rtcp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port) + 1);
399 }
400
401 LOGP(DMGCP, LOGL_NOTICE, "Configured for Audio Forwarding.\n");
402 }
403
404 /* early bind */
405 if (g_cfg->early_bind) {
406 for (i = 1; i < g_cfg->number_endpoints; ++i) {
407 struct mgcp_endpoint *endp = &g_cfg->endpoints[i];
408 int rtp_port;
409
410 rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), g_cfg->rtp_base_port);
411 if (mgcp_bind_rtp_port(endp, rtp_port) != 0) {
412 LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port);
413 return -1;
414 }
415 }
416 }
417
418 return !!g_cfg->forward_ip;
419}
420