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