Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 1 | /* 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 Freyther | f41fb1f | 2010-02-26 20:16:37 +0100 | [diff] [blame] | 27 | #include <osmocore/talloc.h> |
| 28 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 29 | #include <openbsc/debug.h> |
| 30 | #include <openbsc/mgcp.h> |
| 31 | #include <openbsc/mgcp_internal.h> |
Harald Welte | 58ed1cb | 2010-05-14 18:59:17 +0200 | [diff] [blame] | 32 | #include <openbsc/vty.h> |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 33 | |
Harald Welte | bd9591f | 2010-05-19 19:45:32 +0200 | [diff] [blame] | 34 | #include <osmocom/vty/command.h> |
| 35 | #include <osmocom/vty/vty.h> |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 36 | |
Holger Hans Peter Freyther | e8d1371 | 2010-04-16 16:59:48 +0200 | [diff] [blame] | 37 | #include <string.h> |
| 38 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 39 | static struct mgcp_config *g_cfg = NULL; |
| 40 | |
| 41 | /* |
| 42 | * vty code for mgcp below |
| 43 | */ |
| 44 | struct cmd_node mgcp_node = { |
| 45 | MGCP_NODE, |
| 46 | "%s(mgcp)#", |
| 47 | 1, |
| 48 | }; |
| 49 | |
| 50 | static 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 Freyther | e8d1371 | 2010-04-16 16:59:48 +0200 | [diff] [blame] | 54 | 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 Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 56 | 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 Freyther | f89a988 | 2010-04-13 09:28:40 +0200 | [diff] [blame] | 61 | 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 Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 65 | vty_out(vty, " loop %u%s", !!g_cfg->audio_loop, VTY_NEWLINE); |
Holger Hans Peter Freyther | e8d1371 | 2010-04-16 16:59:48 +0200 | [diff] [blame] | 66 | vty_out(vty, " number endpoints %u%s", g_cfg->number_endpoints - 1, VTY_NEWLINE); |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 67 | if (g_cfg->forward_ip) |
Holger Hans Peter Freyther | e8d1371 | 2010-04-16 16:59:48 +0200 | [diff] [blame] | 68 | vty_out(vty, " forward audio ip %s%s", g_cfg->forward_ip, VTY_NEWLINE); |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 69 | if (g_cfg->forward_port != 0) |
Holger Hans Peter Freyther | e8d1371 | 2010-04-16 16:59:48 +0200 | [diff] [blame] | 70 | vty_out(vty, " forward audio port %d%s", g_cfg->forward_port, VTY_NEWLINE); |
Holger Hans Peter Freyther | 19ff679 | 2010-03-31 11:46:41 +0200 | [diff] [blame] | 71 | if (g_cfg->call_agent_addr) |
Holger Hans Peter Freyther | e8d1371 | 2010-04-16 16:59:48 +0200 | [diff] [blame] | 72 | vty_out(vty, " call agent ip %s%s", g_cfg->call_agent_addr, VTY_NEWLINE); |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 73 | |
| 74 | return CMD_SUCCESS; |
| 75 | } |
| 76 | |
| 77 | DEFUN(show_mcgp, show_mgcp_cmd, "show mgcp", |
| 78 | SHOW_STR "Display information about the MGCP Media Gateway") |
| 79 | { |
| 80 | int i; |
| 81 | |
| 82 | vty_out(vty, "MGCP is up and running with %u endpoints:%s", g_cfg->number_endpoints - 1, VTY_NEWLINE); |
| 83 | for (i = 1; i < g_cfg->number_endpoints; ++i) { |
| 84 | struct mgcp_endpoint *endp = &g_cfg->endpoints[i]; |
Holger Hans Peter Freyther | dad118c | 2010-04-09 18:53:24 +0200 | [diff] [blame] | 85 | vty_out(vty, " Endpoint 0x%.2x: CI: %d net: %u/%u bts: %u/%u on %s traffic received bts: %u remote: %u%s", |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 86 | i, endp->ci, |
| 87 | ntohs(endp->net_rtp), ntohs(endp->net_rtcp), |
Holger Hans Peter Freyther | 9c7035e | 2010-04-05 09:00:53 +0200 | [diff] [blame] | 88 | ntohs(endp->bts_rtp), ntohs(endp->bts_rtcp), |
Holger Hans Peter Freyther | 6de5b11 | 2010-04-07 09:37:17 +0200 | [diff] [blame] | 89 | inet_ntoa(endp->bts), endp->in_bts, endp->in_remote, |
| 90 | VTY_NEWLINE); |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | return CMD_SUCCESS; |
| 94 | } |
| 95 | |
| 96 | DEFUN(cfg_mgcp, |
| 97 | cfg_mgcp_cmd, |
| 98 | "mgcp", |
| 99 | "Configure the MGCP") |
| 100 | { |
| 101 | vty->node = MGCP_NODE; |
| 102 | return CMD_SUCCESS; |
| 103 | } |
| 104 | |
| 105 | DEFUN(cfg_mgcp_local_ip, |
| 106 | cfg_mgcp_local_ip_cmd, |
Holger Hans Peter Freyther | 5c9a645 | 2010-05-14 02:27:50 +0800 | [diff] [blame] | 107 | "local ip A.B.C.D", |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 108 | "Set the IP to be used in SDP records") |
| 109 | { |
| 110 | if (g_cfg->local_ip) |
| 111 | talloc_free(g_cfg->local_ip); |
| 112 | g_cfg->local_ip = talloc_strdup(g_cfg, argv[0]); |
| 113 | return CMD_SUCCESS; |
| 114 | } |
| 115 | |
| 116 | DEFUN(cfg_mgcp_bts_ip, |
| 117 | cfg_mgcp_bts_ip_cmd, |
Holger Hans Peter Freyther | 5c9a645 | 2010-05-14 02:27:50 +0800 | [diff] [blame] | 118 | "bts ip A.B.C.D", |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 119 | "Set the IP of the BTS for RTP forwarding") |
| 120 | { |
| 121 | if (g_cfg->bts_ip) |
| 122 | talloc_free(g_cfg->bts_ip); |
| 123 | g_cfg->bts_ip = talloc_strdup(g_cfg, argv[0]); |
| 124 | inet_aton(g_cfg->bts_ip, &g_cfg->bts_in); |
| 125 | return CMD_SUCCESS; |
| 126 | } |
| 127 | |
| 128 | DEFUN(cfg_mgcp_bind_ip, |
| 129 | cfg_mgcp_bind_ip_cmd, |
Holger Hans Peter Freyther | 5c9a645 | 2010-05-14 02:27:50 +0800 | [diff] [blame] | 130 | "bind ip A.B.C.D", |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 131 | "Bind the MGCP to this local addr") |
| 132 | { |
| 133 | if (g_cfg->source_addr) |
| 134 | talloc_free(g_cfg->source_addr); |
| 135 | g_cfg->source_addr = talloc_strdup(g_cfg, argv[0]); |
| 136 | return CMD_SUCCESS; |
| 137 | } |
| 138 | |
| 139 | DEFUN(cfg_mgcp_bind_port, |
| 140 | cfg_mgcp_bind_port_cmd, |
| 141 | "bind port <0-65534>", |
| 142 | "Bind the MGCP to this port") |
| 143 | { |
| 144 | unsigned int port = atoi(argv[0]); |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 145 | g_cfg->source_port = port; |
| 146 | return CMD_SUCCESS; |
| 147 | } |
| 148 | |
| 149 | DEFUN(cfg_mgcp_bind_early, |
| 150 | cfg_mgcp_bind_early_cmd, |
| 151 | "bind early (0|1)", |
| 152 | "Bind all RTP ports early") |
| 153 | { |
| 154 | unsigned int bind = atoi(argv[0]); |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 155 | g_cfg->early_bind = bind == 1; |
| 156 | return CMD_SUCCESS; |
| 157 | } |
| 158 | |
| 159 | DEFUN(cfg_mgcp_rtp_base_port, |
| 160 | cfg_mgcp_rtp_base_port_cmd, |
| 161 | "rtp base <0-65534>", |
| 162 | "Base port to use") |
| 163 | { |
| 164 | unsigned int port = atoi(argv[0]); |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 165 | g_cfg->rtp_base_port = port; |
| 166 | return CMD_SUCCESS; |
| 167 | } |
| 168 | |
| 169 | DEFUN(cfg_mgcp_sdp_payload_number, |
| 170 | cfg_mgcp_sdp_payload_number_cmd, |
| 171 | "sdp audio payload number <1-255>", |
| 172 | "Set the audio codec to use") |
| 173 | { |
| 174 | unsigned int payload = atoi(argv[0]); |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 175 | g_cfg->audio_payload = payload; |
| 176 | return CMD_SUCCESS; |
| 177 | } |
| 178 | |
| 179 | DEFUN(cfg_mgcp_sdp_payload_name, |
| 180 | cfg_mgcp_sdp_payload_name_cmd, |
| 181 | "sdp audio payload name NAME", |
| 182 | "Set the audio name to use") |
| 183 | { |
| 184 | if (g_cfg->audio_name) |
| 185 | talloc_free(g_cfg->audio_name); |
| 186 | g_cfg->audio_name = talloc_strdup(g_cfg, argv[0]); |
| 187 | return CMD_SUCCESS; |
| 188 | } |
| 189 | |
| 190 | DEFUN(cfg_mgcp_loop, |
| 191 | cfg_mgcp_loop_cmd, |
| 192 | "loop (0|1)", |
| 193 | "Loop the audio") |
| 194 | { |
| 195 | g_cfg->audio_loop = atoi(argv[0]); |
| 196 | return CMD_SUCCESS; |
| 197 | } |
| 198 | |
| 199 | DEFUN(cfg_mgcp_number_endp, |
| 200 | cfg_mgcp_number_endp_cmd, |
| 201 | "number endpoints <0-65534>", |
| 202 | "The number of endpoints to allocate. This is not dynamic.") |
| 203 | { |
| 204 | /* + 1 as we start counting at one */ |
| 205 | g_cfg->number_endpoints = atoi(argv[0]) + 1; |
| 206 | return CMD_SUCCESS; |
| 207 | } |
| 208 | |
| 209 | DEFUN(cfg_mgcp_forward_ip, |
| 210 | cfg_mgcp_forward_ip_cmd, |
Holger Hans Peter Freyther | 5c9a645 | 2010-05-14 02:27:50 +0800 | [diff] [blame] | 211 | "forward audio ip A.B.C.D", |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 212 | "Forward packets from and to the IP. This disables most of the MGCP feature.") |
| 213 | { |
| 214 | if (g_cfg->forward_ip) |
| 215 | talloc_free(g_cfg->forward_ip); |
| 216 | g_cfg->forward_ip = talloc_strdup(g_cfg, argv[0]); |
| 217 | return CMD_SUCCESS; |
| 218 | } |
| 219 | |
| 220 | DEFUN(cfg_mgcp_forward_port, |
| 221 | cfg_mgcp_forward_port_cmd, |
| 222 | "forward audio port <1-15000>", |
| 223 | "Forward packets from and to the port. This disables most of the MGCP feature.") |
| 224 | { |
| 225 | g_cfg->forward_port = atoi(argv[0]); |
| 226 | return CMD_SUCCESS; |
| 227 | } |
| 228 | |
Holger Hans Peter Freyther | 19ff679 | 2010-03-31 11:46:41 +0200 | [diff] [blame] | 229 | DEFUN(cfg_mgcp_agent_addr, |
| 230 | cfg_mgcp_agent_addr_cmd, |
| 231 | "call agent ip IP", |
| 232 | "Set the address of the call agent.") |
| 233 | { |
| 234 | if (g_cfg->call_agent_addr) |
| 235 | talloc_free(g_cfg->call_agent_addr); |
| 236 | g_cfg->call_agent_addr = talloc_strdup(g_cfg, argv[0]); |
| 237 | return CMD_SUCCESS; |
| 238 | } |
| 239 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 240 | int mgcp_vty_init(void) |
| 241 | { |
Holger Hans Peter Freyther | 8a22385 | 2010-05-14 02:45:52 +0800 | [diff] [blame] | 242 | install_element_ve(&show_mgcp_cmd); |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 243 | |
| 244 | install_element(CONFIG_NODE, &cfg_mgcp_cmd); |
| 245 | install_node(&mgcp_node, config_write_mgcp); |
| 246 | install_default(MGCP_NODE); |
Harald Welte | 58ed1cb | 2010-05-14 18:59:17 +0200 | [diff] [blame] | 247 | install_element(MGCP_NODE, &ournode_exit_cmd); |
Harald Welte | dc82b9b | 2010-05-14 19:11:04 +0200 | [diff] [blame] | 248 | install_element(MGCP_NODE, &ournode_end_cmd); |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 249 | install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd); |
| 250 | install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd); |
| 251 | install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd); |
| 252 | install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd); |
| 253 | install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd); |
| 254 | install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd); |
| 255 | install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd); |
| 256 | install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd); |
| 257 | install_element(MGCP_NODE, &cfg_mgcp_loop_cmd); |
| 258 | install_element(MGCP_NODE, &cfg_mgcp_number_endp_cmd); |
| 259 | install_element(MGCP_NODE, &cfg_mgcp_forward_ip_cmd); |
| 260 | install_element(MGCP_NODE, &cfg_mgcp_forward_port_cmd); |
Holger Hans Peter Freyther | 19ff679 | 2010-03-31 11:46:41 +0200 | [diff] [blame] | 261 | install_element(MGCP_NODE, &cfg_mgcp_agent_addr_cmd); |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | int mgcp_parse_config(const char *config_file, struct mgcp_config *cfg) |
| 266 | { |
| 267 | int i, rc; |
| 268 | |
| 269 | g_cfg = cfg; |
Harald Welte | 4015287 | 2010-05-16 20:52:23 +0200 | [diff] [blame] | 270 | rc = vty_read_config_file(config_file, NULL); |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 271 | if (rc < 0) { |
| 272 | fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file); |
| 273 | return rc; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | if (!g_cfg->bts_ip) |
| 278 | fprintf(stderr, "No BTS ip address specified. This will allow everyone to connect.\n"); |
| 279 | |
Holger Hans Peter Freyther | bdd3cc3 | 2010-03-30 13:00:10 +0200 | [diff] [blame] | 280 | if (!g_cfg->source_addr) { |
| 281 | fprintf(stderr, "You need to specify a bind address.\n"); |
| 282 | return -1; |
| 283 | } |
| 284 | |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 285 | if (mgcp_endpoints_allocate(g_cfg) != 0) { |
| 286 | fprintf(stderr, "Failed to allocate endpoints: %d. Quitting.\n", g_cfg->number_endpoints); |
| 287 | return -1; |
| 288 | } |
| 289 | |
| 290 | /* |
| 291 | * This application supports two modes. |
Holger Hans Peter Freyther | 8c99434 | 2010-04-30 13:32:05 +0800 | [diff] [blame] | 292 | * 1.) a true MGCP gateway with support for AUEP, CRCX, MDCX, DLCX |
| 293 | * 2.) plain forwarding of RTP packets on the endpoints. |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 294 | * both modes are mutual exclusive |
| 295 | */ |
| 296 | if (g_cfg->forward_ip) { |
| 297 | int port = g_cfg->rtp_base_port; |
| 298 | if (g_cfg->forward_port != 0) |
| 299 | port = g_cfg->forward_port; |
| 300 | |
| 301 | if (!g_cfg->early_bind) { |
| 302 | LOGP(DMGCP, LOGL_NOTICE, "Forwarding requires early bind.\n"); |
| 303 | return -1; |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Store the forward IP and assign a ci. For early bind |
| 308 | * the sockets will be created after this. |
| 309 | */ |
| 310 | for (i = 1; i < g_cfg->number_endpoints; ++i) { |
| 311 | struct mgcp_endpoint *endp = &g_cfg->endpoints[i]; |
| 312 | inet_aton(g_cfg->forward_ip, &endp->remote); |
| 313 | endp->ci = CI_UNUSED + 23; |
| 314 | endp->net_rtp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port)); |
| 315 | endp->net_rtcp = htons(rtp_calculate_port(ENDPOINT_NUMBER(endp), port) + 1); |
| 316 | } |
| 317 | |
| 318 | LOGP(DMGCP, LOGL_NOTICE, "Configured for Audio Forwarding.\n"); |
| 319 | } |
| 320 | |
| 321 | /* early bind */ |
| 322 | if (g_cfg->early_bind) { |
| 323 | for (i = 1; i < g_cfg->number_endpoints; ++i) { |
| 324 | struct mgcp_endpoint *endp = &g_cfg->endpoints[i]; |
| 325 | int rtp_port; |
| 326 | |
| 327 | rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp), g_cfg->rtp_base_port); |
| 328 | if (mgcp_bind_rtp_port(endp, rtp_port) != 0) { |
Holger Hans Peter Freyther | 7b3ff54 | 2010-02-26 13:10:51 +0100 | [diff] [blame] | 329 | LOGP(DMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port); |
Holger Hans Peter Freyther | 6f38c06 | 2010-02-20 21:21:02 +0100 | [diff] [blame] | 330 | return -1; |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | return !!g_cfg->forward_ip; |
| 336 | } |
| 337 | |