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