Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Handle the connection to the MSC. This include ping/timeout/reconnect |
| 3 | * (C) 2008-2009 by Harald Welte <laforge@gnumonks.org> |
| 4 | * (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * (C) 2009-2010 by On-Waves |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <openbsc/bsc_nat.h> |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 25 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | e09919b | 2010-09-16 02:29:45 +0800 | [diff] [blame] | 26 | #include <openbsc/gsm_data.h> |
| 27 | #include <openbsc/ipaccess.h> |
| 28 | #include <openbsc/osmo_msc_data.h> |
Holger Hans Peter Freyther | 0d71163 | 2010-09-16 02:30:36 +0800 | [diff] [blame] | 29 | #include <openbsc/signal.h> |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 30 | |
| 31 | #include <osmocore/gsm0808.h> |
| 32 | |
| 33 | #include <osmocom/sccp/sccp.h> |
| 34 | |
| 35 | #include <sys/socket.h> |
| 36 | #include <netinet/tcp.h> |
| 37 | #include <unistd.h> |
| 38 | |
| 39 | |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 40 | static void initialize_if_needed(struct bsc_msc_connection *conn); |
| 41 | static void send_id_get_response(struct osmo_msc_data *data, int fd); |
| 42 | static void send_ping(struct osmo_msc_data *data); |
| 43 | |
| 44 | /* |
| 45 | * MGCP forwarding code |
| 46 | */ |
| 47 | static int mgcp_do_read(struct bsc_fd *fd) |
| 48 | { |
| 49 | struct osmo_msc_data *data = (struct osmo_msc_data *) fd->data; |
| 50 | struct msgb *mgcp; |
| 51 | int ret; |
| 52 | |
| 53 | mgcp = msgb_alloc_headroom(4096, 128, "mgcp_from_gw"); |
| 54 | if (!mgcp) { |
| 55 | LOGP(DMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n"); |
| 56 | return -1; |
| 57 | } |
| 58 | |
| 59 | ret = read(fd->fd, mgcp->data, 4096 - 128); |
| 60 | if (ret <= 0) { |
| 61 | LOGP(DMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno)); |
| 62 | msgb_free(mgcp); |
| 63 | return -1; |
| 64 | } else if (ret > 4096 - 128) { |
| 65 | LOGP(DMGCP, LOGL_ERROR, "Too much data: %d\n", ret); |
| 66 | msgb_free(mgcp); |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | mgcp->l2h = msgb_put(mgcp, ret); |
Holger Hans Peter Freyther | 19c530c | 2010-10-13 23:52:01 +0200 | [diff] [blame] | 71 | msc_queue_write(data->msc_con, mgcp, IPAC_PROTO_MGCP); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int mgcp_do_write(struct bsc_fd *fd, struct msgb *msg) |
| 76 | { |
| 77 | int ret; |
| 78 | |
| 79 | LOGP(DMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len); |
| 80 | |
| 81 | ret = write(fd->fd, msg->data, msg->len); |
| 82 | if (ret != msg->len) |
| 83 | LOGP(DMGCP, LOGL_ERROR, "Failed to forward message to MGCP GW (%s).\n", strerror(errno)); |
| 84 | |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | static void mgcp_forward(struct osmo_msc_data *data, struct msgb *msg) |
| 89 | { |
| 90 | struct msgb *mgcp; |
| 91 | |
| 92 | if (msgb_l2len(msg) > 4096) { |
| 93 | LOGP(DMGCP, LOGL_ERROR, "Can not forward too big message.\n"); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | mgcp = msgb_alloc(4096, "mgcp_to_gw"); |
| 98 | if (!mgcp) { |
| 99 | LOGP(DMGCP, LOGL_ERROR, "Failed to send message.\n"); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | msgb_put(mgcp, msgb_l2len(msg)); |
| 104 | memcpy(mgcp->data, msg->l2h, mgcp->len); |
| 105 | if (write_queue_enqueue(&data->mgcp_agent, mgcp) != 0) { |
| 106 | LOGP(DMGCP, LOGL_FATAL, "Could not queue message to MGCP GW.\n"); |
| 107 | msgb_free(mgcp); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | static int mgcp_create_port(struct osmo_msc_data *data) |
| 112 | { |
| 113 | int on; |
| 114 | struct sockaddr_in addr; |
| 115 | |
| 116 | data->mgcp_agent.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 117 | if (data->mgcp_agent.bfd.fd < 0) { |
| 118 | LOGP(DMGCP, LOGL_FATAL, "Failed to create UDP socket errno: %d\n", errno); |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | on = 1; |
| 123 | setsockopt(data->mgcp_agent.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 124 | |
| 125 | /* try to bind the socket */ |
| 126 | memset(&addr, 0, sizeof(addr)); |
| 127 | addr.sin_family = AF_INET; |
| 128 | addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 129 | addr.sin_port = 0; |
| 130 | |
| 131 | if (bind(data->mgcp_agent.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 132 | LOGP(DMGCP, LOGL_FATAL, "Failed to bind to any port.\n"); |
| 133 | close(data->mgcp_agent.bfd.fd); |
| 134 | data->mgcp_agent.bfd.fd = -1; |
| 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | /* connect to the remote */ |
| 139 | addr.sin_port = htons(2427); |
| 140 | if (connect(data->mgcp_agent.bfd.fd, (struct sockaddr *) & addr, sizeof(addr)) < 0) { |
| 141 | LOGP(DMGCP, LOGL_FATAL, "Failed to connect to local MGCP GW. %s\n", strerror(errno)); |
| 142 | close(data->mgcp_agent.bfd.fd); |
| 143 | data->mgcp_agent.bfd.fd = -1; |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | write_queue_init(&data->mgcp_agent, 10); |
| 148 | data->mgcp_agent.bfd.when = BSC_FD_READ; |
| 149 | data->mgcp_agent.bfd.data = data; |
| 150 | data->mgcp_agent.read_cb = mgcp_do_read; |
| 151 | data->mgcp_agent.write_cb = mgcp_do_write; |
| 152 | |
| 153 | if (bsc_register_fd(&data->mgcp_agent.bfd) != 0) { |
| 154 | LOGP(DMGCP, LOGL_FATAL, "Failed to register BFD\n"); |
| 155 | close(data->mgcp_agent.bfd.fd); |
| 156 | data->mgcp_agent.bfd.fd = -1; |
| 157 | return -1; |
| 158 | } |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Send data to the network |
| 165 | */ |
Holger Hans Peter Freyther | 6098038 | 2010-09-16 02:22:20 +0800 | [diff] [blame] | 166 | int msc_queue_write(struct bsc_msc_connection *conn, struct msgb *msg, int proto) |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 167 | { |
| 168 | ipaccess_prepend_header(msg, proto); |
| 169 | if (write_queue_enqueue(&conn->write_queue, msg) != 0) { |
| 170 | LOGP(DMSC, LOGL_FATAL, "Failed to queue IPA/%d\n", proto); |
| 171 | msgb_free(msg); |
| 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static int msc_sccp_do_write(struct bsc_fd *fd, struct msgb *msg) |
| 179 | { |
| 180 | int ret; |
| 181 | |
| 182 | LOGP(DMSC, LOGL_DEBUG, "Sending SCCP to MSC: %u\n", msgb_l2len(msg)); |
| 183 | LOGP(DMI, LOGL_DEBUG, "MSC TX %s\n", hexdump(msg->l2h, msgb_l2len(msg))); |
| 184 | |
| 185 | ret = write(fd->fd, msg->data, msg->len); |
| 186 | if (ret < msg->len) |
| 187 | perror("MSC: Failed to send SCCP"); |
| 188 | |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | static int ipaccess_a_fd_cb(struct bsc_fd *bfd) |
| 193 | { |
| 194 | int error; |
| 195 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
| 196 | struct ipaccess_head *hh; |
Holger Hans Peter Freyther | 0425c38 | 2010-11-03 14:46:56 +0100 | [diff] [blame] | 197 | struct osmo_msc_data *data = (struct osmo_msc_data *) bfd->data; |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 198 | |
| 199 | if (!msg) { |
| 200 | if (error == 0) { |
| 201 | LOGP(DMSC, LOGL_ERROR, "The connection to the MSC was lost.\n"); |
| 202 | bsc_msc_lost(data->msc_con); |
| 203 | return -1; |
| 204 | } |
| 205 | |
| 206 | LOGP(DMSC, LOGL_ERROR, "Failed to parse ip access message: %d\n", error); |
| 207 | return -1; |
| 208 | } |
| 209 | |
| 210 | LOGP(DMSC, LOGL_DEBUG, "From MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]); |
| 211 | |
| 212 | /* handle base message handling */ |
| 213 | hh = (struct ipaccess_head *) msg->data; |
| 214 | ipaccess_rcvmsg_base(msg, bfd); |
| 215 | |
| 216 | /* initialize the networking. This includes sending a GSM08.08 message */ |
| 217 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 218 | if (msg->l2h[0] == IPAC_MSGT_ID_ACK) |
| 219 | initialize_if_needed(data->msc_con); |
| 220 | else if (msg->l2h[0] == IPAC_MSGT_ID_GET) { |
| 221 | send_id_get_response(data, bfd->fd); |
| 222 | } else if (msg->l2h[0] == IPAC_MSGT_PONG) { |
| 223 | bsc_del_timer(&data->pong_timer); |
| 224 | } |
| 225 | } else if (hh->proto == IPAC_PROTO_SCCP) { |
| 226 | sccp_system_incoming(msg); |
Holger Hans Peter Freyther | 19c530c | 2010-10-13 23:52:01 +0200 | [diff] [blame] | 227 | } else if (hh->proto == IPAC_PROTO_MGCP) { |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 228 | mgcp_forward(data, msg); |
| 229 | } |
| 230 | |
| 231 | msgb_free(msg); |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static void send_ping(struct osmo_msc_data *data) |
| 236 | { |
| 237 | struct msgb *msg; |
| 238 | |
| 239 | msg = msgb_alloc_headroom(4096, 128, "ping"); |
| 240 | if (!msg) { |
| 241 | LOGP(DMSC, LOGL_ERROR, "Failed to create PING.\n"); |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | msg->l2h = msgb_put(msg, 1); |
| 246 | msg->l2h[0] = IPAC_MSGT_PING; |
| 247 | |
| 248 | msc_queue_write(data->msc_con, msg, IPAC_PROTO_IPACCESS); |
| 249 | } |
| 250 | |
| 251 | static void msc_ping_timeout_cb(void *_data) |
| 252 | { |
| 253 | struct osmo_msc_data *data = (struct osmo_msc_data *) _data; |
| 254 | if (data->ping_timeout < 0) |
| 255 | return; |
| 256 | |
| 257 | send_ping(data); |
| 258 | |
| 259 | /* send another ping in 20 seconds */ |
| 260 | bsc_schedule_timer(&data->ping_timer, data->ping_timeout, 0); |
| 261 | |
| 262 | /* also start a pong timer */ |
| 263 | bsc_schedule_timer(&data->pong_timer, data->pong_timeout, 0); |
| 264 | } |
| 265 | |
| 266 | static void msc_pong_timeout_cb(void *_data) |
| 267 | { |
| 268 | struct osmo_msc_data *data = (struct osmo_msc_data *) _data; |
| 269 | |
| 270 | LOGP(DMSC, LOGL_ERROR, "MSC didn't answer PING. Closing connection.\n"); |
| 271 | bsc_msc_lost(data->msc_con); |
| 272 | } |
| 273 | |
| 274 | static void msc_connection_connected(struct bsc_msc_connection *con) |
| 275 | { |
Holger Hans Peter Freyther | 0d71163 | 2010-09-16 02:30:36 +0800 | [diff] [blame] | 276 | struct msc_signal_data sig; |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 277 | struct osmo_msc_data *data; |
| 278 | int ret, on; |
| 279 | on = 1; |
| 280 | ret = setsockopt(con->write_queue.bfd.fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
| 281 | if (ret != 0) |
| 282 | LOGP(DMSC, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno)); |
| 283 | |
| 284 | data = (struct osmo_msc_data *) con->write_queue.bfd.data; |
Holger Hans Peter Freyther | 0425c38 | 2010-11-03 14:46:56 +0100 | [diff] [blame] | 285 | msc_ping_timeout_cb(data); |
Holger Hans Peter Freyther | 0d71163 | 2010-09-16 02:30:36 +0800 | [diff] [blame] | 286 | |
| 287 | sig.data = data; |
| 288 | dispatch_signal(SS_MSC, S_MSC_CONNECTED, &sig); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* |
| 292 | * The connection to the MSC was lost and we will need to free all |
| 293 | * resources and then attempt to reconnect. |
| 294 | */ |
| 295 | static void msc_connection_was_lost(struct bsc_msc_connection *msc) |
| 296 | { |
Holger Hans Peter Freyther | 0d71163 | 2010-09-16 02:30:36 +0800 | [diff] [blame] | 297 | struct msc_signal_data sig; |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 298 | struct osmo_msc_data *data; |
| 299 | |
| 300 | LOGP(DMSC, LOGL_ERROR, "Lost MSC connection. Freing stuff.\n"); |
| 301 | |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 302 | data = (struct osmo_msc_data *) msc->write_queue.bfd.data; |
| 303 | bsc_del_timer(&data->ping_timer); |
| 304 | bsc_del_timer(&data->pong_timer); |
| 305 | |
Holger Hans Peter Freyther | 0d71163 | 2010-09-16 02:30:36 +0800 | [diff] [blame] | 306 | sig.data = data; |
| 307 | dispatch_signal(SS_MSC, S_MSC_LOST, &sig); |
| 308 | |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 309 | msc->is_authenticated = 0; |
| 310 | bsc_msc_schedule_connect(msc); |
| 311 | } |
| 312 | |
| 313 | static void initialize_if_needed(struct bsc_msc_connection *conn) |
| 314 | { |
| 315 | struct msgb *msg; |
| 316 | |
| 317 | if (!conn->is_authenticated) { |
| 318 | /* send a gsm 08.08 reset message from here */ |
| 319 | msg = gsm0808_create_reset(); |
| 320 | if (!msg) { |
| 321 | LOGP(DMSC, LOGL_ERROR, "Failed to create the reset message.\n"); |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | sccp_write(msg, &sccp_ssn_bssap, &sccp_ssn_bssap, 0); |
| 326 | msgb_free(msg); |
| 327 | conn->is_authenticated = 1; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | static void send_id_get_response(struct osmo_msc_data *data, int fd) |
| 332 | { |
| 333 | struct msgb *msg; |
| 334 | |
| 335 | msg = bsc_msc_id_get_resp(data->bsc_token); |
| 336 | if (!msg) |
| 337 | return; |
| 338 | msc_queue_write(data->msc_con, msg, IPAC_PROTO_IPACCESS); |
| 339 | } |
| 340 | |
| 341 | int osmo_bsc_msc_init(struct gsm_network *network) |
| 342 | { |
| 343 | struct osmo_msc_data *data = network->msc_data; |
| 344 | |
| 345 | if (mgcp_create_port(data) != 0) |
| 346 | return -1; |
| 347 | |
| 348 | data->msc_con = bsc_msc_create(data->msc_ip, |
| 349 | data->msc_port, |
| 350 | data->msc_ip_dscp); |
| 351 | if (!data->msc_con) { |
| 352 | LOGP(DMSC, LOGL_ERROR, "Creating the MSC network connection failed.\n"); |
| 353 | return -1; |
| 354 | } |
| 355 | |
| 356 | data->ping_timer.cb = msc_ping_timeout_cb; |
| 357 | data->ping_timer.data = data; |
| 358 | data->pong_timer.cb = msc_pong_timeout_cb; |
| 359 | data->pong_timer.data = data; |
| 360 | |
| 361 | data->msc_con->write_queue.bfd.data = data; |
| 362 | data->msc_con->connection_loss = msc_connection_was_lost; |
| 363 | data->msc_con->connected = msc_connection_connected; |
| 364 | data->msc_con->write_queue.read_cb = ipaccess_a_fd_cb; |
| 365 | data->msc_con->write_queue.write_cb = msc_sccp_do_write; |
| 366 | bsc_msc_connect(data->msc_con); |
| 367 | |
| 368 | return 0; |
| 369 | } |