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> |
Holger Hans Peter Freyther | 2a9eeaa | 2011-04-23 16:37:58 +0200 | [diff] [blame] | 4 | * (C) 2009-2011 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * (C) 2009-2011 by On-Waves |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 9 | * it under the terms of the GNU Affero General Public License as published by |
| 10 | * the Free Software Foundation; either version 3 of the License, or |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 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 |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 16 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 17 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 18 | * You should have received a copy of the GNU Affero General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <openbsc/bsc_nat.h> |
Daniel Willmann | f7d557c | 2011-02-18 14:30:25 +0100 | [diff] [blame] | 24 | #include <openbsc/control_cmd.h> |
Holger Hans Peter Freyther | 49f9e5b | 2014-03-23 16:25:16 +0100 | [diff] [blame] | 25 | #include <openbsc/control_if.h> |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 26 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | e09919b | 2010-09-16 02:29:45 +0800 | [diff] [blame] | 27 | #include <openbsc/gsm_data.h> |
| 28 | #include <openbsc/ipaccess.h> |
| 29 | #include <openbsc/osmo_msc_data.h> |
Holger Hans Peter Freyther | 0d71163 | 2010-09-16 02:30:36 +0800 | [diff] [blame] | 30 | #include <openbsc/signal.h> |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 31 | |
Daniel Willmann | f7d557c | 2011-02-18 14:30:25 +0100 | [diff] [blame] | 32 | #include <osmocom/core/talloc.h> |
| 33 | |
Harald Welte | d36ff76 | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 34 | #include <osmocom/gsm/gsm0808.h> |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 35 | |
| 36 | #include <osmocom/sccp/sccp.h> |
| 37 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 38 | #include <osmocom/abis/ipa.h> |
| 39 | |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 40 | #include <sys/socket.h> |
| 41 | #include <netinet/tcp.h> |
| 42 | #include <unistd.h> |
| 43 | |
| 44 | |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 45 | static void initialize_if_needed(struct bsc_msc_connection *conn); |
Holger Hans Peter Freyther | 2a9eeaa | 2011-04-23 16:37:58 +0200 | [diff] [blame] | 46 | static void send_lacs(struct gsm_network *net, struct bsc_msc_connection *conn); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 47 | static void send_id_get_response(struct osmo_msc_data *data, int fd); |
| 48 | static void send_ping(struct osmo_msc_data *data); |
| 49 | |
| 50 | /* |
| 51 | * MGCP forwarding code |
| 52 | */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 53 | static int mgcp_do_read(struct osmo_fd *fd) |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 54 | { |
| 55 | struct osmo_msc_data *data = (struct osmo_msc_data *) fd->data; |
| 56 | struct msgb *mgcp; |
| 57 | int ret; |
| 58 | |
| 59 | mgcp = msgb_alloc_headroom(4096, 128, "mgcp_from_gw"); |
| 60 | if (!mgcp) { |
| 61 | LOGP(DMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n"); |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | ret = read(fd->fd, mgcp->data, 4096 - 128); |
| 66 | if (ret <= 0) { |
| 67 | LOGP(DMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno)); |
| 68 | msgb_free(mgcp); |
| 69 | return -1; |
| 70 | } else if (ret > 4096 - 128) { |
| 71 | LOGP(DMGCP, LOGL_ERROR, "Too much data: %d\n", ret); |
| 72 | msgb_free(mgcp); |
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | mgcp->l2h = msgb_put(mgcp, ret); |
Holger Hans Peter Freyther | 368a0a7 | 2011-01-07 16:54:46 +0100 | [diff] [blame] | 77 | msc_queue_write(data->msc_con, mgcp, IPAC_PROTO_MGCP_OLD); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 78 | return 0; |
| 79 | } |
| 80 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 81 | static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg) |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 82 | { |
| 83 | int ret; |
| 84 | |
| 85 | LOGP(DMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len); |
| 86 | |
| 87 | ret = write(fd->fd, msg->data, msg->len); |
| 88 | if (ret != msg->len) |
| 89 | LOGP(DMGCP, LOGL_ERROR, "Failed to forward message to MGCP GW (%s).\n", strerror(errno)); |
| 90 | |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | static void mgcp_forward(struct osmo_msc_data *data, struct msgb *msg) |
| 95 | { |
| 96 | struct msgb *mgcp; |
| 97 | |
| 98 | if (msgb_l2len(msg) > 4096) { |
| 99 | LOGP(DMGCP, LOGL_ERROR, "Can not forward too big message.\n"); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | mgcp = msgb_alloc(4096, "mgcp_to_gw"); |
| 104 | if (!mgcp) { |
| 105 | LOGP(DMGCP, LOGL_ERROR, "Failed to send message.\n"); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | msgb_put(mgcp, msgb_l2len(msg)); |
| 110 | memcpy(mgcp->data, msg->l2h, mgcp->len); |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 111 | if (osmo_wqueue_enqueue(&data->mgcp_agent, mgcp) != 0) { |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 112 | LOGP(DMGCP, LOGL_FATAL, "Could not queue message to MGCP GW.\n"); |
| 113 | msgb_free(mgcp); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static int mgcp_create_port(struct osmo_msc_data *data) |
| 118 | { |
| 119 | int on; |
| 120 | struct sockaddr_in addr; |
| 121 | |
| 122 | data->mgcp_agent.bfd.fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 123 | if (data->mgcp_agent.bfd.fd < 0) { |
| 124 | LOGP(DMGCP, LOGL_FATAL, "Failed to create UDP socket errno: %d\n", errno); |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | on = 1; |
| 129 | setsockopt(data->mgcp_agent.bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); |
| 130 | |
| 131 | /* try to bind the socket */ |
| 132 | memset(&addr, 0, sizeof(addr)); |
| 133 | addr.sin_family = AF_INET; |
| 134 | addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
| 135 | addr.sin_port = 0; |
| 136 | |
| 137 | if (bind(data->mgcp_agent.bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) { |
| 138 | LOGP(DMGCP, LOGL_FATAL, "Failed to bind to any port.\n"); |
| 139 | close(data->mgcp_agent.bfd.fd); |
| 140 | data->mgcp_agent.bfd.fd = -1; |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | /* connect to the remote */ |
| 145 | addr.sin_port = htons(2427); |
| 146 | if (connect(data->mgcp_agent.bfd.fd, (struct sockaddr *) & addr, sizeof(addr)) < 0) { |
| 147 | LOGP(DMGCP, LOGL_FATAL, "Failed to connect to local MGCP GW. %s\n", strerror(errno)); |
| 148 | close(data->mgcp_agent.bfd.fd); |
| 149 | data->mgcp_agent.bfd.fd = -1; |
| 150 | return -1; |
| 151 | } |
| 152 | |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 153 | osmo_wqueue_init(&data->mgcp_agent, 10); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 154 | data->mgcp_agent.bfd.when = BSC_FD_READ; |
| 155 | data->mgcp_agent.bfd.data = data; |
| 156 | data->mgcp_agent.read_cb = mgcp_do_read; |
| 157 | data->mgcp_agent.write_cb = mgcp_do_write; |
| 158 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 159 | if (osmo_fd_register(&data->mgcp_agent.bfd) != 0) { |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 160 | LOGP(DMGCP, LOGL_FATAL, "Failed to register BFD\n"); |
| 161 | close(data->mgcp_agent.bfd.fd); |
| 162 | data->mgcp_agent.bfd.fd = -1; |
| 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Send data to the network |
| 171 | */ |
Holger Hans Peter Freyther | 6098038 | 2010-09-16 02:22:20 +0800 | [diff] [blame] | 172 | 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] | 173 | { |
| 174 | ipaccess_prepend_header(msg, proto); |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 175 | if (osmo_wqueue_enqueue(&conn->write_queue, msg) != 0) { |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 176 | LOGP(DMSC, LOGL_FATAL, "Failed to queue IPA/%d\n", proto); |
| 177 | msgb_free(msg); |
| 178 | return -1; |
| 179 | } |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 184 | static int msc_alink_do_write(struct osmo_fd *fd, struct msgb *msg) |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 185 | { |
| 186 | int ret; |
| 187 | |
| 188 | LOGP(DMSC, LOGL_DEBUG, "Sending SCCP to MSC: %u\n", msgb_l2len(msg)); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 189 | LOGP(DLMI, LOGL_DEBUG, "MSC TX %s\n", osmo_hexdump(msg->data, msg->len)); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 190 | |
| 191 | ret = write(fd->fd, msg->data, msg->len); |
| 192 | if (ret < msg->len) |
| 193 | perror("MSC: Failed to send SCCP"); |
| 194 | |
| 195 | return ret; |
| 196 | } |
| 197 | |
Daniel Willmann | f7d557c | 2011-02-18 14:30:25 +0100 | [diff] [blame] | 198 | static void handle_ctrl(struct osmo_msc_data *msc, struct msgb *msg) |
| 199 | { |
| 200 | int ret; |
| 201 | struct ctrl_cmd *cmd; |
| 202 | |
| 203 | cmd = ctrl_cmd_parse(msc->msc_con, msg); |
| 204 | if (!cmd) { |
| 205 | LOGP(DMSC, LOGL_ERROR, "Failed to parse control message.\n"); |
| 206 | cmd = talloc_zero(msc->msc_con, struct ctrl_cmd); |
| 207 | if (!cmd) { |
| 208 | LOGP(DMSC, LOGL_ERROR, "OOM!\n"); |
| 209 | return; |
| 210 | } |
| 211 | cmd->type = CTRL_TYPE_ERROR; |
| 212 | cmd->id = "err"; |
| 213 | cmd->reply = "Failed to parse control message."; |
| 214 | |
| 215 | ctrl_cmd_send(&msc->msc_con->write_queue, cmd); |
| 216 | talloc_free(cmd); |
| 217 | |
| 218 | return; |
| 219 | } |
| 220 | |
Holger Hans Peter Freyther | 49f9e5b | 2014-03-23 16:25:16 +0100 | [diff] [blame] | 221 | ret = bsc_ctrl_cmd_handle(cmd, msc->network); |
Daniel Willmann | f7d557c | 2011-02-18 14:30:25 +0100 | [diff] [blame] | 222 | if (ret != CTRL_CMD_HANDLED) |
| 223 | ctrl_cmd_send(&msc->msc_con->write_queue, cmd); |
| 224 | talloc_free(cmd); |
| 225 | } |
| 226 | |
Holger Hans Peter Freyther | a78b602 | 2011-04-23 15:58:24 +0200 | [diff] [blame] | 227 | static void osmo_ext_handle(struct osmo_msc_data *msc, struct msgb *msg) |
| 228 | { |
| 229 | struct ipaccess_head *hh; |
| 230 | struct ipaccess_head_ext *hh_ext; |
| 231 | |
| 232 | hh = (struct ipaccess_head *) msg->data; |
| 233 | hh_ext = (struct ipaccess_head_ext *) hh->data; |
| 234 | if (msg->len < sizeof(*hh) + sizeof(*hh_ext)) { |
| 235 | LOGP(DMSC, LOGL_ERROR, "Packet too short for extended header.\n"); |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | msg->l2h = hh_ext->data; |
| 240 | if (hh_ext->proto == IPAC_PROTO_EXT_MGCP) |
| 241 | mgcp_forward(msc, msg); |
Holger Hans Peter Freyther | 2a9eeaa | 2011-04-23 16:37:58 +0200 | [diff] [blame] | 242 | else if (hh_ext->proto == IPAC_PROTO_EXT_LAC) |
| 243 | send_lacs(msc->network, msc->msc_con); |
Daniel Willmann | f7d557c | 2011-02-18 14:30:25 +0100 | [diff] [blame] | 244 | else if (hh_ext->proto == IPAC_PROTO_EXT_CTRL) |
| 245 | handle_ctrl(msc, msg); |
Holger Hans Peter Freyther | a78b602 | 2011-04-23 15:58:24 +0200 | [diff] [blame] | 246 | } |
| 247 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 248 | static int ipaccess_a_fd_cb(struct osmo_fd *bfd) |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 249 | { |
Jacob Erlbeck | e827812 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 250 | struct msgb *msg = NULL; |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 251 | struct ipaccess_head *hh; |
Holger Hans Peter Freyther | 0425c38 | 2010-11-03 14:46:56 +0100 | [diff] [blame] | 252 | struct osmo_msc_data *data = (struct osmo_msc_data *) bfd->data; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 253 | int ret; |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 254 | |
Jacob Erlbeck | e827812 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 255 | ret = ipa_msg_recv_buffered(bfd->fd, &msg, &data->msc_con->pending_msg); |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 256 | if (ret <= 0) { |
Jacob Erlbeck | e827812 | 2014-03-31 13:42:11 +0200 | [diff] [blame] | 257 | if (ret == -EAGAIN) |
| 258 | return 0; |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 259 | if (ret == 0) { |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 260 | LOGP(DMSC, LOGL_ERROR, "The connection to the MSC was lost.\n"); |
| 261 | bsc_msc_lost(data->msc_con); |
| 262 | return -1; |
| 263 | } |
| 264 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 265 | LOGP(DMSC, LOGL_ERROR, "Failed to parse ip access message: %d\n", ret); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 266 | return -1; |
| 267 | } |
| 268 | |
Pablo Neira Ayuso | ed5cacb | 2011-08-17 22:44:07 +0200 | [diff] [blame] | 269 | LOGP(DLMI, LOGL_DEBUG, "From MSC: %s proto: %d\n", osmo_hexdump(msg->data, msg->len), msg->l2h[0]); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 270 | |
| 271 | /* handle base message handling */ |
| 272 | hh = (struct ipaccess_head *) msg->data; |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 273 | |
| 274 | /* initialize the networking. This includes sending a GSM08.08 message */ |
Holger Hans Peter Freyther | f31a4a4 | 2011-06-04 13:20:04 +0200 | [diff] [blame] | 275 | msg->cb[0] = (unsigned long) data; |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 276 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
Holger Hans Peter Freyther | abddd6a | 2014-05-06 06:45:30 +0200 | [diff] [blame^] | 277 | ipaccess_rcvmsg_base(msg, bfd); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 278 | if (msg->l2h[0] == IPAC_MSGT_ID_ACK) |
| 279 | initialize_if_needed(data->msc_con); |
| 280 | else if (msg->l2h[0] == IPAC_MSGT_ID_GET) { |
| 281 | send_id_get_response(data, bfd->fd); |
| 282 | } else if (msg->l2h[0] == IPAC_MSGT_PONG) { |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 283 | osmo_timer_del(&data->pong_timer); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 284 | } |
| 285 | } else if (hh->proto == IPAC_PROTO_SCCP) { |
| 286 | sccp_system_incoming(msg); |
Holger Hans Peter Freyther | 368a0a7 | 2011-01-07 16:54:46 +0100 | [diff] [blame] | 287 | } else if (hh->proto == IPAC_PROTO_MGCP_OLD) { |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 288 | mgcp_forward(data, msg); |
Holger Hans Peter Freyther | a78b602 | 2011-04-23 15:58:24 +0200 | [diff] [blame] | 289 | } else if (hh->proto == IPAC_PROTO_OSMO) { |
| 290 | osmo_ext_handle(data, msg); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | msgb_free(msg); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static void send_ping(struct osmo_msc_data *data) |
| 298 | { |
| 299 | struct msgb *msg; |
| 300 | |
| 301 | msg = msgb_alloc_headroom(4096, 128, "ping"); |
| 302 | if (!msg) { |
| 303 | LOGP(DMSC, LOGL_ERROR, "Failed to create PING.\n"); |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | msg->l2h = msgb_put(msg, 1); |
| 308 | msg->l2h[0] = IPAC_MSGT_PING; |
| 309 | |
| 310 | msc_queue_write(data->msc_con, msg, IPAC_PROTO_IPACCESS); |
| 311 | } |
| 312 | |
| 313 | static void msc_ping_timeout_cb(void *_data) |
| 314 | { |
| 315 | struct osmo_msc_data *data = (struct osmo_msc_data *) _data; |
| 316 | if (data->ping_timeout < 0) |
| 317 | return; |
| 318 | |
| 319 | send_ping(data); |
| 320 | |
| 321 | /* send another ping in 20 seconds */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 322 | osmo_timer_schedule(&data->ping_timer, data->ping_timeout, 0); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 323 | |
| 324 | /* also start a pong timer */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 325 | osmo_timer_schedule(&data->pong_timer, data->pong_timeout, 0); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | static void msc_pong_timeout_cb(void *_data) |
| 329 | { |
| 330 | struct osmo_msc_data *data = (struct osmo_msc_data *) _data; |
| 331 | |
| 332 | LOGP(DMSC, LOGL_ERROR, "MSC didn't answer PING. Closing connection.\n"); |
| 333 | bsc_msc_lost(data->msc_con); |
| 334 | } |
| 335 | |
| 336 | static void msc_connection_connected(struct bsc_msc_connection *con) |
| 337 | { |
Holger Hans Peter Freyther | 0d71163 | 2010-09-16 02:30:36 +0800 | [diff] [blame] | 338 | struct msc_signal_data sig; |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 339 | struct osmo_msc_data *data; |
| 340 | int ret, on; |
| 341 | on = 1; |
| 342 | ret = setsockopt(con->write_queue.bfd.fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
| 343 | if (ret != 0) |
| 344 | LOGP(DMSC, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno)); |
| 345 | |
| 346 | data = (struct osmo_msc_data *) con->write_queue.bfd.data; |
Holger Hans Peter Freyther | 0425c38 | 2010-11-03 14:46:56 +0100 | [diff] [blame] | 347 | msc_ping_timeout_cb(data); |
Holger Hans Peter Freyther | 0d71163 | 2010-09-16 02:30:36 +0800 | [diff] [blame] | 348 | |
| 349 | sig.data = data; |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 350 | osmo_signal_dispatch(SS_MSC, S_MSC_CONNECTED, &sig); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /* |
| 354 | * The connection to the MSC was lost and we will need to free all |
| 355 | * resources and then attempt to reconnect. |
| 356 | */ |
| 357 | static void msc_connection_was_lost(struct bsc_msc_connection *msc) |
| 358 | { |
Holger Hans Peter Freyther | 0d71163 | 2010-09-16 02:30:36 +0800 | [diff] [blame] | 359 | struct msc_signal_data sig; |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 360 | struct osmo_msc_data *data; |
| 361 | |
| 362 | LOGP(DMSC, LOGL_ERROR, "Lost MSC connection. Freing stuff.\n"); |
| 363 | |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 364 | data = (struct osmo_msc_data *) msc->write_queue.bfd.data; |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 365 | osmo_timer_del(&data->ping_timer); |
| 366 | osmo_timer_del(&data->pong_timer); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 367 | |
Holger Hans Peter Freyther | 0d71163 | 2010-09-16 02:30:36 +0800 | [diff] [blame] | 368 | sig.data = data; |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 369 | osmo_signal_dispatch(SS_MSC, S_MSC_LOST, &sig); |
Holger Hans Peter Freyther | 0d71163 | 2010-09-16 02:30:36 +0800 | [diff] [blame] | 370 | |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 371 | msc->is_authenticated = 0; |
| 372 | bsc_msc_schedule_connect(msc); |
| 373 | } |
| 374 | |
Holger Hans Peter Freyther | 2a9eeaa | 2011-04-23 16:37:58 +0200 | [diff] [blame] | 375 | static void send_lacs(struct gsm_network *net, struct bsc_msc_connection *conn) |
| 376 | { |
| 377 | struct ipac_ext_lac_cmd *lac; |
| 378 | struct gsm_bts *bts; |
| 379 | struct msgb *msg; |
| 380 | int lacs = 0; |
| 381 | |
| 382 | if (llist_empty(&net->bts_list)) { |
| 383 | LOGP(DMSC, LOGL_ERROR, "No BTSs configured. Not sending LACs.\n"); |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | msg = msgb_alloc_headroom(4096, 128, "LAC Command"); |
| 388 | if (!msg) { |
| 389 | LOGP(DMSC, LOGL_ERROR, "Failed to create the LAC command.\n"); |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | lac = (struct ipac_ext_lac_cmd *) msgb_put(msg, sizeof(*lac)); |
| 394 | lac->add_remove = 1; |
| 395 | |
| 396 | llist_for_each_entry(bts, &net->bts_list, list) { |
| 397 | if (lacs++ == 0) |
| 398 | lac->lac = htons(bts->location_area_code); |
| 399 | else |
| 400 | msgb_put_u16(msg, htons(bts->location_area_code)); |
| 401 | } |
| 402 | |
| 403 | lac->nr_extra_lacs = lacs - 1; |
| 404 | ipaccess_prepend_header_ext(msg, IPAC_PROTO_EXT_LAC); |
| 405 | msc_queue_write(conn, msg, IPAC_PROTO_OSMO); |
| 406 | } |
| 407 | |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 408 | static void initialize_if_needed(struct bsc_msc_connection *conn) |
| 409 | { |
| 410 | struct msgb *msg; |
| 411 | |
| 412 | if (!conn->is_authenticated) { |
| 413 | /* send a gsm 08.08 reset message from here */ |
| 414 | msg = gsm0808_create_reset(); |
| 415 | if (!msg) { |
| 416 | LOGP(DMSC, LOGL_ERROR, "Failed to create the reset message.\n"); |
| 417 | return; |
| 418 | } |
| 419 | |
Holger Hans Peter Freyther | 44e5dad | 2011-06-04 17:34:02 +0200 | [diff] [blame] | 420 | sccp_write(msg, &sccp_ssn_bssap, &sccp_ssn_bssap, 0, conn); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 421 | msgb_free(msg); |
| 422 | conn->is_authenticated = 1; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | static void send_id_get_response(struct osmo_msc_data *data, int fd) |
| 427 | { |
Holger Hans Peter Freyther | ed0374f | 2011-08-17 17:37:16 +0200 | [diff] [blame] | 428 | struct msc_signal_data sig; |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 429 | struct msgb *msg; |
| 430 | |
| 431 | msg = bsc_msc_id_get_resp(data->bsc_token); |
| 432 | if (!msg) |
| 433 | return; |
| 434 | msc_queue_write(data->msc_con, msg, IPAC_PROTO_IPACCESS); |
Holger Hans Peter Freyther | ed0374f | 2011-08-17 17:37:16 +0200 | [diff] [blame] | 435 | |
| 436 | sig.data = data; |
| 437 | osmo_signal_dispatch(SS_MSC, S_MSC_AUTHENTICATED, &sig); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 438 | } |
| 439 | |
Holger Hans Peter Freyther | abd668a | 2011-06-04 15:34:37 +0200 | [diff] [blame] | 440 | int osmo_bsc_msc_init(struct osmo_msc_data *data) |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 441 | { |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 442 | if (mgcp_create_port(data) != 0) |
| 443 | return -1; |
| 444 | |
Holger Hans Peter Freyther | e188010 | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 445 | data->msc_con = bsc_msc_create(data, &data->dests); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 446 | if (!data->msc_con) { |
| 447 | LOGP(DMSC, LOGL_ERROR, "Creating the MSC network connection failed.\n"); |
| 448 | return -1; |
| 449 | } |
| 450 | |
| 451 | data->ping_timer.cb = msc_ping_timeout_cb; |
| 452 | data->ping_timer.data = data; |
| 453 | data->pong_timer.cb = msc_pong_timeout_cb; |
| 454 | data->pong_timer.data = data; |
| 455 | |
| 456 | data->msc_con->write_queue.bfd.data = data; |
| 457 | data->msc_con->connection_loss = msc_connection_was_lost; |
| 458 | data->msc_con->connected = msc_connection_connected; |
| 459 | data->msc_con->write_queue.read_cb = ipaccess_a_fd_cb; |
Holger Hans Peter Freyther | f358964 | 2011-02-18 23:39:34 +0100 | [diff] [blame] | 460 | data->msc_con->write_queue.write_cb = msc_alink_do_write; |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 461 | bsc_msc_connect(data->msc_con); |
| 462 | |
| 463 | return 0; |
| 464 | } |
Holger Hans Peter Freyther | 20fea24 | 2011-06-04 19:58:26 +0200 | [diff] [blame] | 465 | |
| 466 | struct osmo_msc_data *osmo_msc_data_find(struct gsm_network *net, int nr) |
| 467 | { |
| 468 | struct osmo_msc_data *msc_data; |
| 469 | |
| 470 | llist_for_each_entry(msc_data, &net->bsc_data->mscs, entry) |
| 471 | if (msc_data->nr == nr) |
| 472 | return msc_data; |
| 473 | return NULL; |
| 474 | } |
| 475 | |
| 476 | struct osmo_msc_data *osmo_msc_data_alloc(struct gsm_network *net, int nr) |
| 477 | { |
| 478 | struct osmo_msc_data *msc_data; |
| 479 | |
| 480 | /* check if there is already one */ |
| 481 | msc_data = osmo_msc_data_find(net, nr); |
| 482 | if (msc_data) |
| 483 | return msc_data; |
| 484 | |
| 485 | msc_data = talloc_zero(net, struct osmo_msc_data); |
| 486 | if (!msc_data) |
| 487 | return NULL; |
| 488 | |
| 489 | llist_add_tail(&msc_data->entry, &net->bsc_data->mscs); |
| 490 | |
| 491 | /* Init back pointer */ |
| 492 | msc_data->network = net; |
| 493 | |
| 494 | INIT_LLIST_HEAD(&msc_data->dests); |
| 495 | msc_data->ping_timeout = 20; |
| 496 | msc_data->pong_timeout = 5; |
| 497 | msc_data->core_ncc = -1; |
| 498 | msc_data->core_mcc = -1; |
| 499 | msc_data->rtp_base = 4000; |
| 500 | |
| 501 | msc_data->nr = nr; |
Holger Hans Peter Freyther | 18fa70a | 2011-06-08 19:25:38 +0200 | [diff] [blame] | 502 | msc_data->allow_emerg = 1; |
Holger Hans Peter Freyther | 20fea24 | 2011-06-04 19:58:26 +0200 | [diff] [blame] | 503 | |
Holger Hans Peter Freyther | 25aa749 | 2011-08-06 07:00:52 +0200 | [diff] [blame] | 504 | /* Defaults for the audio setup */ |
| 505 | msc_data->amr_conf.m5_90 = 1; |
| 506 | |
Holger Hans Peter Freyther | 20fea24 | 2011-06-04 19:58:26 +0200 | [diff] [blame] | 507 | return msc_data; |
| 508 | } |