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