Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 1 | /* MSC related stuff... */ |
| 2 | /* |
| 3 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 4 | * (C) 2010 by On-Waves |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <bsc_data.h> |
| 24 | #include <bss_patch.h> |
| 25 | #include <bssap_sccp.h> |
| 26 | #include <ipaccess.h> |
| 27 | #include <mtp_data.h> |
Holger Hans Peter Freyther | cbf7d18 | 2010-07-31 05:25:35 +0800 | [diff] [blame] | 28 | #include <cellmgr_debug.h> |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 29 | |
Holger Hans Peter Freyther | cbf7d18 | 2010-07-31 05:25:35 +0800 | [diff] [blame] | 30 | #include <osmocore/tlv.h> |
| 31 | #include <osmocore/utils.h> |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 32 | |
| 33 | #include <arpa/inet.h> |
| 34 | #include <sys/socket.h> |
| 35 | #include <netinet/tcp.h> |
| 36 | |
| 37 | #include <fcntl.h> |
| 38 | #include <unistd.h> |
| 39 | #include <errno.h> |
| 40 | #include <string.h> |
| 41 | |
| 42 | #define RECONNECT_TIME 10, 0 |
| 43 | #define NAT_MUX 0xfc |
| 44 | |
| 45 | static void msc_send_id_response(struct bsc_data *bsc); |
| 46 | static void msc_send(struct bsc_data *bsc, struct msgb *msg, int proto); |
Holger Hans Peter Freyther | 7b7c297 | 2010-08-07 05:41:06 +0800 | [diff] [blame] | 47 | static void msc_schedule_reconnect(struct bsc_data *bsc); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 48 | |
| 49 | void mtp_link_slta_recv(struct mtp_link *link) |
| 50 | { |
| 51 | struct msgb *msg; |
| 52 | unsigned int sls; |
| 53 | |
| 54 | while (!llist_empty(&link->pending_msgs)) { |
| 55 | msg = msgb_dequeue(&link->pending_msgs); |
| 56 | sls = (unsigned int) msg->l3h; |
| 57 | |
| 58 | if (mtp_link_submit_sccp_data(link, sls, msg->l2h, msgb_l2len(msg)) != 0) |
| 59 | LOGP(DMSC, LOGL_ERROR, "Could not forward SCCP message.\n"); |
| 60 | |
| 61 | msgb_free(msg); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void msc_clear_queue(struct bsc_data *data) |
| 66 | { |
| 67 | struct msgb *msg; |
| 68 | |
| 69 | LOGP(DMSC, LOGL_NOTICE, "Clearing the MSC to BSC queue.\n"); |
| 70 | while (!llist_empty(&data->link.the_link->pending_msgs)) { |
| 71 | msg = msgb_dequeue(&data->link.the_link->pending_msgs); |
| 72 | msgb_free(msg); |
| 73 | } |
| 74 | } |
| 75 | |
Holger Hans Peter Freyther | 43d9eec | 2010-08-07 01:54:19 +0800 | [diff] [blame] | 76 | void msc_close_connection(struct bsc_data *bsc) |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 77 | { |
| 78 | struct bsc_fd *bfd = &bsc->msc_connection.bfd; |
| 79 | |
| 80 | close(bfd->fd); |
| 81 | bsc_unregister_fd(bfd); |
| 82 | bfd->fd = -1; |
Holger Hans Peter Freyther | e33d93c | 2010-08-07 02:40:35 +0800 | [diff] [blame] | 83 | bsc->msc_link_down = 1; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 84 | release_bsc_resources(bsc); |
| 85 | bsc_del_timer(&bsc->ping_timeout); |
| 86 | bsc_del_timer(&bsc->pong_timeout); |
Holger Hans Peter Freyther | 7b7c297 | 2010-08-07 05:41:06 +0800 | [diff] [blame] | 87 | msc_schedule_reconnect(bsc); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static void msc_connect_timeout(void *_bsc_data) |
| 91 | { |
| 92 | struct bsc_data *bsc_data = _bsc_data; |
| 93 | |
| 94 | LOGP(DMSC, LOGL_ERROR, "Timeout on the MSC connection.\n"); |
Holger Hans Peter Freyther | 43d9eec | 2010-08-07 01:54:19 +0800 | [diff] [blame] | 95 | msc_close_connection(bsc_data); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static void msc_pong_timeout(void *_bsc_data) |
| 99 | { |
| 100 | struct bsc_data *bsc_data = _bsc_data; |
| 101 | LOGP(DMSC, LOGL_ERROR, "MSC didn't respond to ping. Closing.\n"); |
Holger Hans Peter Freyther | 43d9eec | 2010-08-07 01:54:19 +0800 | [diff] [blame] | 102 | msc_close_connection(bsc_data); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | static void send_ping(struct bsc_data *bsc) |
| 106 | { |
| 107 | struct msgb *msg; |
| 108 | |
| 109 | msg = msgb_alloc_headroom(4096, 128, "ping"); |
| 110 | if (!msg) { |
| 111 | LOGP(DMSC, LOGL_ERROR, "Failed to create PING.\n"); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | msg->l2h = msgb_put(msg, 1); |
| 116 | msg->l2h[0] = IPAC_MSGT_PING; |
| 117 | |
| 118 | msc_send(bsc, msg, IPAC_PROTO_IPACCESS); |
| 119 | } |
| 120 | |
| 121 | static void msc_ping_timeout(void *_bsc_data) |
| 122 | { |
| 123 | struct bsc_data *bsc_data = _bsc_data; |
| 124 | |
| 125 | if (bsc_data->ping_time < 0) |
| 126 | return; |
| 127 | |
| 128 | send_ping(bsc_data); |
| 129 | |
| 130 | /* send another ping in 20 seconds */ |
| 131 | bsc_schedule_timer(&bsc_data->ping_timeout, bsc_data->ping_time, 0); |
| 132 | |
| 133 | /* also start a pong timer */ |
| 134 | bsc_schedule_timer(&bsc_data->pong_timeout, bsc_data->pong_time, 0); |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * callback with IP access data |
| 139 | */ |
| 140 | static int ipaccess_a_fd_cb(struct bsc_fd *bfd) |
| 141 | { |
| 142 | int error; |
| 143 | struct ipaccess_head *hh; |
| 144 | struct mtp_link *link; |
| 145 | struct bsc_data *bsc; |
| 146 | struct msgb *msg; |
| 147 | |
| 148 | msg = ipaccess_read_msg(bfd, &error); |
| 149 | |
| 150 | bsc = (struct bsc_data *) bfd->data; |
| 151 | |
| 152 | if (!msg) { |
| 153 | if (error == 0) |
| 154 | fprintf(stderr, "The connection to the MSC was lost, exiting\n"); |
| 155 | else |
| 156 | fprintf(stderr, "Error in the IPA stream.\n"); |
| 157 | |
Holger Hans Peter Freyther | 43d9eec | 2010-08-07 01:54:19 +0800 | [diff] [blame] | 158 | msc_close_connection(bsc); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | LOGP(DMSC, LOGL_DEBUG, "From MSC: %s proto: %d\n", hexdump(msg->data, msg->len), msg->l2h[0]); |
| 163 | |
| 164 | /* handle base message handling */ |
| 165 | hh = (struct ipaccess_head *) msg->data; |
| 166 | ipaccess_rcvmsg_base(msg, bfd); |
| 167 | |
| 168 | link = bsc->link.the_link; |
| 169 | |
| 170 | /* initialize the networking. This includes sending a GSM08.08 message */ |
| 171 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 172 | if (bsc->first_contact) { |
| 173 | LOGP(DMSC, LOGL_NOTICE, "Connected to MSC. Sending reset.\n"); |
| 174 | bsc_del_timer(&bsc->msc_timeout); |
| 175 | bsc->first_contact = 0; |
Holger Hans Peter Freyther | 0c95c6a | 2010-08-07 02:37:43 +0800 | [diff] [blame] | 176 | bsc->msc_link_down = 0; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 177 | msc_send_reset(bsc); |
| 178 | } |
| 179 | if (msg->l2h[0] == IPAC_MSGT_ID_GET && bsc->token) { |
| 180 | msc_send_id_response(bsc); |
| 181 | } else if (msg->l2h[0] == IPAC_MSGT_PONG) { |
| 182 | bsc_del_timer(&bsc->pong_timeout); |
| 183 | } |
| 184 | } else if (hh->proto == IPAC_PROTO_SCCP) { |
| 185 | struct sccp_parse_result result; |
| 186 | int rc; |
| 187 | rc = bss_patch_filter_msg(msg, &result); |
| 188 | |
| 189 | if (rc == BSS_FILTER_RESET_ACK) { |
| 190 | LOGP(DMSC, LOGL_NOTICE, "Filtering reset ack from the MSC\n"); |
| 191 | } else if (rc == BSS_FILTER_RLSD) { |
| 192 | LOGP(DMSC, LOGL_DEBUG, "Filtering RLSD from the MSC\n"); |
| 193 | update_con_state(rc, &result, msg, 1, 0); |
| 194 | } else if (rc == BSS_FILTER_RLC) { |
| 195 | /* if we receive this we have forwarded a RLSD to the network */ |
| 196 | LOGP(DMSC, LOGL_ERROR, "RLC from the network. BAD!\n"); |
| 197 | } else if (rc == BSS_FILTER_CLEAR_COMPL) { |
| 198 | LOGP(DMSC, LOGL_ERROR, "Clear Complete from the network.\n"); |
| 199 | } else if (link->sccp_up) { |
| 200 | unsigned int sls; |
| 201 | |
| 202 | update_con_state(rc, &result, msg, 1, 0); |
| 203 | sls = sls_for_src_ref(result.destination_local_reference); |
| 204 | |
| 205 | /* patch a possible PC */ |
| 206 | bss_rewrite_header_to_bsc(msg, link->opc, link->dpc); |
| 207 | |
| 208 | /* we can not forward it right now */ |
| 209 | if (link->sltm_pending) { |
| 210 | LOGP(DMSC, LOGL_NOTICE, "Queueing msg for pending SLTM.\n"); |
Holger Hans Peter Freyther | 5aa1701 | 2010-07-31 04:37:26 +0800 | [diff] [blame] | 211 | msg->l3h = (uint8_t *) sls; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 212 | msgb_enqueue(&link->pending_msgs, msg); |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | if (mtp_link_submit_sccp_data(link, sls, msg->l2h, msgb_l2len(msg)) != 0) |
| 217 | LOGP(DMSC, LOGL_ERROR, "Could not forward SCCP message.\n"); |
| 218 | } |
| 219 | } else if (hh->proto == NAT_MUX) { |
| 220 | mgcp_forward(bsc, msg->l2h, msgb_l2len(msg)); |
| 221 | } else { |
| 222 | LOGP(DMSC, LOGL_ERROR, "Unknown IPA proto 0x%x\n", hh->proto); |
| 223 | } |
| 224 | |
| 225 | msgb_free(msg); |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static int ipaccess_write_cb(struct bsc_fd *fd, struct msgb *msg) |
| 230 | { |
| 231 | int rc; |
| 232 | |
| 233 | LOGP(DMSC, LOGL_DEBUG, "Sending to MSC: %s\n", hexdump(msg->data, msg->len)); |
| 234 | rc = write(fd->fd, msg->data, msg->len); |
| 235 | if (rc != msg->len) |
| 236 | LOGP(DMSC, LOGL_ERROR, "Could not write to MSC.\n"); |
| 237 | |
| 238 | return rc; |
| 239 | } |
| 240 | |
| 241 | /* called in the case of a non blocking connect */ |
| 242 | static int msc_connection_connect(struct bsc_fd *fd, unsigned int what) |
| 243 | { |
| 244 | int rc; |
| 245 | int val; |
| 246 | socklen_t len = sizeof(val); |
| 247 | struct bsc_data *bsc; |
| 248 | |
| 249 | bsc = (struct bsc_data *) fd->data; |
| 250 | |
| 251 | if (fd != &bsc->msc_connection.bfd) { |
| 252 | LOGP(DMSC, LOGL_ERROR, "This is only working with the MSC connection.\n"); |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | if ((what & BSC_FD_WRITE) == 0) |
| 257 | return -1; |
| 258 | |
| 259 | /* check the socket state */ |
| 260 | rc = getsockopt(fd->fd, SOL_SOCKET, SO_ERROR, &val, &len); |
| 261 | if (rc != 0) { |
| 262 | LOGP(DMSC, LOGL_ERROR, "getsockopt for the MSC socket failed.\n"); |
| 263 | goto error; |
| 264 | } |
| 265 | if (val != 0) { |
| 266 | LOGP(DMSC, LOGL_ERROR, "Not connected to the MSC.\n"); |
| 267 | goto error; |
| 268 | } |
| 269 | |
| 270 | |
| 271 | /* go to full operation */ |
| 272 | fd->cb = write_queue_bfd_cb; |
| 273 | fd->when = BSC_FD_READ; |
| 274 | if (!llist_empty(&bsc->msc_connection.msg_queue)) |
| 275 | fd->when |= BSC_FD_WRITE; |
| 276 | return 0; |
| 277 | |
| 278 | error: |
Holger Hans Peter Freyther | 6258539 | 2010-08-07 02:26:47 +0800 | [diff] [blame] | 279 | msc_close_connection(bsc); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 280 | return -1; |
| 281 | } |
| 282 | |
| 283 | static void setnonblocking(struct bsc_fd *fd) |
| 284 | { |
| 285 | int flags; |
| 286 | |
| 287 | flags = fcntl(fd->fd, F_GETFL); |
| 288 | if (flags < 0) { |
| 289 | perror("fcntl get failed"); |
| 290 | close(fd->fd); |
| 291 | fd->fd = -1; |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | flags |= O_NONBLOCK; |
| 296 | flags = fcntl(fd->fd, F_SETFL, flags); |
| 297 | if (flags < 0) { |
| 298 | perror("fcntl get failed"); |
| 299 | close(fd->fd); |
| 300 | fd->fd = -1; |
| 301 | return; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | static int connect_to_msc(struct bsc_fd *fd, const char *ip, int port, int tos) |
| 306 | { |
| 307 | struct sockaddr_in sin; |
| 308 | int on = 1, ret; |
| 309 | |
| 310 | LOGP(DMSC, LOGL_NOTICE, "Attempting to connect MSC at %s:%d\n", ip, port); |
| 311 | |
| 312 | fd->fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 313 | |
| 314 | if (fd->fd < 0) { |
| 315 | perror("Creating TCP socket failed"); |
| 316 | return fd->fd; |
| 317 | } |
| 318 | |
| 319 | /* make it non blocking */ |
| 320 | setnonblocking(fd); |
| 321 | |
| 322 | memset(&sin, 0, sizeof(sin)); |
| 323 | sin.sin_family = AF_INET; |
| 324 | sin.sin_port = htons(port); |
| 325 | inet_aton(ip, &sin.sin_addr); |
| 326 | |
| 327 | setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 328 | ret = setsockopt(fd->fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
| 329 | if (ret != 0) |
| 330 | LOGP(DMSC, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno)); |
| 331 | ret = setsockopt(fd->fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); |
| 332 | if (ret != 0) |
| 333 | LOGP(DMSC, LOGL_ERROR, "Failed to set IP_TOS: %s\n", strerror(errno)); |
| 334 | |
| 335 | ret = connect(fd->fd, (struct sockaddr *) &sin, sizeof(sin)); |
| 336 | |
| 337 | if (ret == -1 && errno == EINPROGRESS) { |
| 338 | LOGP(DMSC, LOGL_ERROR, "MSC Connection in progress\n"); |
| 339 | fd->when = BSC_FD_WRITE; |
| 340 | fd->cb = msc_connection_connect; |
| 341 | } else if (ret < 0) { |
| 342 | perror("Connection failed"); |
| 343 | close(fd->fd); |
| 344 | fd->fd = -1; |
| 345 | return ret; |
| 346 | } else { |
| 347 | fd->when = BSC_FD_READ; |
| 348 | fd->cb = write_queue_bfd_cb; |
| 349 | } |
| 350 | |
| 351 | ret = bsc_register_fd(fd); |
| 352 | if (ret < 0) { |
| 353 | perror("Registering the fd failed"); |
| 354 | close(fd->fd); |
| 355 | fd->fd = -1; |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | return ret; |
| 360 | } |
| 361 | |
| 362 | static void msc_reconnect(void *_data) |
| 363 | { |
| 364 | int rc; |
| 365 | struct bsc_data *bsc = (struct bsc_data *) _data; |
| 366 | |
| 367 | bsc_del_timer(&bsc->reconnect_timer); |
| 368 | bsc->first_contact = 1; |
| 369 | |
| 370 | rc = connect_to_msc(&bsc->msc_connection.bfd, bsc->msc_address, 5000, bsc->msc_ip_dscp); |
| 371 | if (rc < 0) { |
| 372 | fprintf(stderr, "Opening the MSC connection failed. Trying again\n"); |
| 373 | bsc_schedule_timer(&bsc->reconnect_timer, RECONNECT_TIME); |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | bsc->msc_timeout.cb = msc_connect_timeout; |
| 378 | bsc->msc_timeout.data = bsc; |
| 379 | bsc_schedule_timer(&bsc->msc_timeout, bsc->msc_time, 0); |
| 380 | } |
| 381 | |
Holger Hans Peter Freyther | 7b7c297 | 2010-08-07 05:41:06 +0800 | [diff] [blame] | 382 | static void msc_schedule_reconnect(struct bsc_data *bsc) |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 383 | { |
| 384 | bsc_schedule_timer(&bsc->reconnect_timer, RECONNECT_TIME); |
| 385 | } |
| 386 | |
| 387 | /* |
| 388 | * mgcp forwarding is below |
| 389 | */ |
| 390 | static int mgcp_do_write(struct bsc_fd *fd, struct msgb *msg) |
| 391 | { |
| 392 | int ret; |
| 393 | |
| 394 | LOGP(DMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len); |
| 395 | |
| 396 | ret = write(fd->fd, msg->data, msg->len); |
| 397 | if (ret != msg->len) |
| 398 | LOGP(DMGCP, LOGL_ERROR, "Failed to forward message to MGCP GW (%s).\n", strerror(errno)); |
| 399 | |
| 400 | return ret; |
| 401 | } |
| 402 | |
| 403 | static int mgcp_do_read(struct bsc_fd *fd) |
| 404 | { |
| 405 | struct msgb *mgcp; |
| 406 | int ret; |
| 407 | |
| 408 | mgcp = msgb_alloc_headroom(4096, 128, "mgcp_from_gw"); |
| 409 | if (!mgcp) { |
| 410 | LOGP(DMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n"); |
| 411 | return -1; |
| 412 | } |
| 413 | |
| 414 | ret = read(fd->fd, mgcp->data, 4096 - 128); |
| 415 | if (ret <= 0) { |
| 416 | LOGP(DMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno)); |
| 417 | msgb_free(mgcp); |
| 418 | return -1; |
| 419 | } else if (ret > 4096 - 128) { |
| 420 | LOGP(DMGCP, LOGL_ERROR, "Too much data: %d\n", ret); |
| 421 | msgb_free(mgcp); |
| 422 | return -1; |
| 423 | } |
| 424 | |
| 425 | mgcp->l2h = msgb_put(mgcp, ret); |
| 426 | msc_send(fd->data, mgcp, NAT_MUX); |
| 427 | return 0; |
| 428 | } |
| 429 | |
Holger Hans Peter Freyther | 5aa1701 | 2010-07-31 04:37:26 +0800 | [diff] [blame] | 430 | void mgcp_forward(struct bsc_data *bsc, const uint8_t *data, unsigned int length) |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 431 | { |
| 432 | struct msgb *mgcp; |
| 433 | |
| 434 | if (length > 4096) { |
| 435 | LOGP(DMGCP, LOGL_ERROR, "Can not forward too big message.\n"); |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | mgcp = msgb_alloc(4096, "mgcp_to_gw"); |
| 440 | if (!mgcp) { |
| 441 | LOGP(DMGCP, LOGL_ERROR, "Failed to send message.\n"); |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | msgb_put(mgcp, length); |
| 446 | memcpy(mgcp->data, data, mgcp->len); |
| 447 | if (write_queue_enqueue(&bsc->mgcp_agent, mgcp) != 0) { |
| 448 | LOGP(DMGCP, LOGL_FATAL, "Could not queue message to MGCP GW.\n"); |
| 449 | msgb_free(mgcp); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | static int mgcp_create_port(struct bsc_data *bsc) |
| 454 | { |
| 455 | int on; |
| 456 | struct sockaddr_in addr; |
| 457 | |
| 458 | bsc->mgcp_agent.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 459 | if (bsc->mgcp_agent.bfd.fd < 0) { |
| 460 | LOGP(DMGCP, LOGL_FATAL, "Failed to create UDP socket errno: %d\n", errno); |
| 461 | return -1; |
| 462 | } |
| 463 | |
| 464 | on = 1; |
| 465 | setsockopt(bsc->mgcp_agent.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 466 | |
| 467 | /* try to bind the socket */ |
| 468 | memset(&addr, 0, sizeof(addr)); |
| 469 | addr.sin_family = AF_INET; |
| 470 | addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 471 | addr.sin_port = 0; |
| 472 | |
| 473 | if (bind(bsc->mgcp_agent.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 474 | LOGP(DMGCP, LOGL_FATAL, "Failed to bind to any port.\n"); |
| 475 | close(bsc->mgcp_agent.bfd.fd); |
| 476 | bsc->mgcp_agent.bfd.fd = -1; |
| 477 | return -1; |
| 478 | } |
| 479 | |
| 480 | /* connect to the remote */ |
| 481 | addr.sin_port = htons(2427); |
| 482 | if (connect(bsc->mgcp_agent.bfd.fd, (struct sockaddr *) & addr, sizeof(addr)) < 0) { |
| 483 | LOGP(DMGCP, LOGL_FATAL, "Failed to connect to local MGCP GW. %s\n", strerror(errno)); |
| 484 | close(bsc->mgcp_agent.bfd.fd); |
| 485 | bsc->mgcp_agent.bfd.fd = -1; |
| 486 | return -1; |
| 487 | } |
| 488 | |
| 489 | write_queue_init(&bsc->mgcp_agent, 10); |
| 490 | bsc->mgcp_agent.bfd.data = bsc; |
| 491 | bsc->mgcp_agent.bfd.when = BSC_FD_READ; |
| 492 | bsc->mgcp_agent.read_cb = mgcp_do_read; |
| 493 | bsc->mgcp_agent.write_cb = mgcp_do_write; |
| 494 | |
| 495 | if (bsc_register_fd(&bsc->mgcp_agent.bfd) != 0) { |
| 496 | LOGP(DMGCP, LOGL_FATAL, "Failed to register BFD\n"); |
| 497 | close(bsc->mgcp_agent.bfd.fd); |
| 498 | bsc->mgcp_agent.bfd.fd = -1; |
| 499 | return -1; |
| 500 | } |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | int msc_init(struct bsc_data *bsc) |
| 506 | { |
| 507 | write_queue_init(&bsc->msc_connection, 100); |
| 508 | bsc->reconnect_timer.cb = msc_reconnect; |
| 509 | bsc->reconnect_timer.data = bsc; |
| 510 | bsc->msc_connection.read_cb = ipaccess_a_fd_cb; |
| 511 | bsc->msc_connection.write_cb = ipaccess_write_cb; |
| 512 | bsc->msc_connection.bfd.data = bsc; |
Holger Hans Peter Freyther | 0c95c6a | 2010-08-07 02:37:43 +0800 | [diff] [blame] | 513 | bsc->msc_link_down = 1; |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 514 | |
| 515 | /* handle the timeout */ |
| 516 | bsc->ping_timeout.cb = msc_ping_timeout; |
| 517 | bsc->ping_timeout.data = bsc; |
| 518 | bsc->pong_timeout.cb = msc_pong_timeout; |
| 519 | bsc->pong_timeout.data = bsc; |
| 520 | |
| 521 | /* create MGCP port */ |
| 522 | if (mgcp_create_port(bsc) != 0) |
| 523 | return -1; |
Holger Hans Peter Freyther | 7b7c297 | 2010-08-07 05:41:06 +0800 | [diff] [blame] | 524 | |
| 525 | /* now connect to the BSC */ |
| 526 | msc_schedule_reconnect(bsc); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | static void msc_send(struct bsc_data *bsc, struct msgb *msg, int proto) |
| 531 | { |
Holger Hans Peter Freyther | 7b7c297 | 2010-08-07 05:41:06 +0800 | [diff] [blame] | 532 | if (bsc->msc_link_down) { |
| 533 | LOGP(DMSC, LOGL_NOTICE, "Dropping data due lack of MSC connection.\n"); |
| 534 | msgb_free(msg); |
| 535 | return; |
| 536 | } |
| 537 | |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 538 | ipaccess_prepend_header(msg, proto); |
| 539 | |
| 540 | if (write_queue_enqueue(&bsc->msc_connection, msg) != 0) { |
| 541 | LOGP(DMSC, LOGL_FATAL, "Failed to queue MSG for the MSC.\n"); |
| 542 | msgb_free(msg); |
| 543 | return; |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | void msc_send_rlc(struct bsc_data *bsc, |
| 548 | struct sccp_source_reference *src, struct sccp_source_reference *dst) |
| 549 | { |
| 550 | struct msgb *msg; |
| 551 | |
| 552 | msg = create_sccp_rlc(src, dst); |
| 553 | if (!msg) |
| 554 | return; |
| 555 | |
| 556 | msc_send(bsc, msg, IPAC_PROTO_SCCP); |
| 557 | } |
| 558 | |
| 559 | void msc_send_reset(struct bsc_data *bsc) |
| 560 | { |
| 561 | struct msgb *msg; |
| 562 | |
| 563 | msg = create_reset(); |
| 564 | if (!msg) |
| 565 | return; |
| 566 | |
| 567 | msc_send(bsc, msg, IPAC_PROTO_SCCP); |
| 568 | msc_ping_timeout(bsc); |
| 569 | } |
| 570 | |
| 571 | static void msc_send_id_response(struct bsc_data *bsc) |
| 572 | { |
| 573 | struct msgb *msg; |
| 574 | |
| 575 | msg = msgb_alloc_headroom(4096, 128, "id resp"); |
| 576 | msg->l2h = msgb_v_put(msg, IPAC_MSGT_ID_RESP); |
| 577 | msgb_l16tv_put(msg, strlen(bsc->token) + 1, |
Holger Hans Peter Freyther | 5aa1701 | 2010-07-31 04:37:26 +0800 | [diff] [blame] | 578 | IPAC_IDTAG_UNITNAME, (uint8_t *) bsc->token); |
Holger Hans Peter Freyther | 97f66e2 | 2010-07-28 03:32:52 +0800 | [diff] [blame] | 579 | |
| 580 | msc_send(bsc, msg, IPAC_PROTO_IPACCESS); |
| 581 | } |
| 582 | |
| 583 | void msc_send_msg(struct bsc_data *bsc, int rc, struct sccp_parse_result *result, struct msgb *_msg) |
| 584 | { |
| 585 | struct msgb *msg; |
| 586 | |
| 587 | if (bsc->msc_connection.bfd.fd < 0) { |
| 588 | LOGP(DMSC, LOGL_ERROR, "No connection to the MSC. dropping\n"); |
| 589 | return; |
| 590 | } |
| 591 | |
| 592 | msg = msgb_alloc_headroom(4096, 128, "SCCP to MSC"); |
| 593 | if (!msg) { |
| 594 | LOGP(DMSC, LOGL_ERROR, "Failed to alloc MSC msg.\n"); |
| 595 | return; |
| 596 | } |
| 597 | |
| 598 | bss_rewrite_header_for_msc(rc, msg, _msg, result); |
| 599 | msc_send(bsc, msg, IPAC_PROTO_SCCP); |
| 600 | } |