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> |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 24 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | e09919b | 2010-09-16 02:29:45 +0800 | [diff] [blame] | 25 | #include <openbsc/gsm_data.h> |
| 26 | #include <openbsc/ipaccess.h> |
| 27 | #include <openbsc/osmo_msc_data.h> |
Holger Hans Peter Freyther | 0d71163 | 2010-09-16 02:30:36 +0800 | [diff] [blame] | 28 | #include <openbsc/signal.h> |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 29 | |
Harald Welte | d36ff76 | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 30 | #include <osmocom/gsm/gsm0808.h> |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 31 | |
| 32 | #include <osmocom/sccp/sccp.h> |
| 33 | |
| 34 | #include <sys/socket.h> |
| 35 | #include <netinet/tcp.h> |
| 36 | #include <unistd.h> |
| 37 | |
| 38 | |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 39 | static void initialize_if_needed(struct bsc_msc_connection *conn); |
Holger Hans Peter Freyther | 2a9eeaa | 2011-04-23 16:37:58 +0200 | [diff] [blame] | 40 | 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] | 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 | */ |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 47 | static int mgcp_do_read(struct osmo_fd *fd) |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 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 | 368a0a7 | 2011-01-07 16:54:46 +0100 | [diff] [blame] | 71 | 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] | 72 | return 0; |
| 73 | } |
| 74 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 75 | 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] | 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); |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 105 | if (osmo_wqueue_enqueue(&data->mgcp_agent, mgcp) != 0) { |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 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 | |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 147 | osmo_wqueue_init(&data->mgcp_agent, 10); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 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 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 153 | if (osmo_fd_register(&data->mgcp_agent.bfd) != 0) { |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 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); |
Pablo Neira Ayuso | e1273b1 | 2011-05-06 12:09:47 +0200 | [diff] [blame] | 169 | if (osmo_wqueue_enqueue(&conn->write_queue, msg) != 0) { |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 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 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 178 | 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] | 179 | { |
| 180 | int ret; |
| 181 | |
| 182 | LOGP(DMSC, LOGL_DEBUG, "Sending SCCP to MSC: %u\n", msgb_l2len(msg)); |
Pablo Neira Ayuso | c0d17f2 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 183 | LOGP(DMI, 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] | 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 | |
Holger Hans Peter Freyther | a78b602 | 2011-04-23 15:58:24 +0200 | [diff] [blame] | 192 | static void osmo_ext_handle(struct osmo_msc_data *msc, struct msgb *msg) |
| 193 | { |
| 194 | struct ipaccess_head *hh; |
| 195 | struct ipaccess_head_ext *hh_ext; |
| 196 | |
| 197 | hh = (struct ipaccess_head *) msg->data; |
| 198 | hh_ext = (struct ipaccess_head_ext *) hh->data; |
| 199 | if (msg->len < sizeof(*hh) + sizeof(*hh_ext)) { |
| 200 | LOGP(DMSC, LOGL_ERROR, "Packet too short for extended header.\n"); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | msg->l2h = hh_ext->data; |
| 205 | if (hh_ext->proto == IPAC_PROTO_EXT_MGCP) |
| 206 | mgcp_forward(msc, msg); |
Holger Hans Peter Freyther | 2a9eeaa | 2011-04-23 16:37:58 +0200 | [diff] [blame] | 207 | else if (hh_ext->proto == IPAC_PROTO_EXT_LAC) |
| 208 | send_lacs(msc->network, msc->msc_con); |
Holger Hans Peter Freyther | a78b602 | 2011-04-23 15:58:24 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Pablo Neira Ayuso | 4db9299 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 211 | static int ipaccess_a_fd_cb(struct osmo_fd *bfd) |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 212 | { |
| 213 | int error; |
| 214 | struct msgb *msg = ipaccess_read_msg(bfd, &error); |
| 215 | struct ipaccess_head *hh; |
Holger Hans Peter Freyther | 0425c38 | 2010-11-03 14:46:56 +0100 | [diff] [blame] | 216 | 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] | 217 | |
| 218 | if (!msg) { |
| 219 | if (error == 0) { |
| 220 | LOGP(DMSC, LOGL_ERROR, "The connection to the MSC was lost.\n"); |
| 221 | bsc_msc_lost(data->msc_con); |
| 222 | return -1; |
| 223 | } |
| 224 | |
| 225 | LOGP(DMSC, LOGL_ERROR, "Failed to parse ip access message: %d\n", error); |
| 226 | return -1; |
| 227 | } |
| 228 | |
Harald Welte | 376f782 | 2011-07-11 18:19:33 +0200 | [diff] [blame^] | 229 | LOGP(DMI, 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] | 230 | |
| 231 | /* handle base message handling */ |
| 232 | hh = (struct ipaccess_head *) msg->data; |
| 233 | ipaccess_rcvmsg_base(msg, bfd); |
| 234 | |
| 235 | /* initialize the networking. This includes sending a GSM08.08 message */ |
| 236 | if (hh->proto == IPAC_PROTO_IPACCESS) { |
| 237 | if (msg->l2h[0] == IPAC_MSGT_ID_ACK) |
| 238 | initialize_if_needed(data->msc_con); |
| 239 | else if (msg->l2h[0] == IPAC_MSGT_ID_GET) { |
| 240 | send_id_get_response(data, bfd->fd); |
| 241 | } else if (msg->l2h[0] == IPAC_MSGT_PONG) { |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 242 | osmo_timer_del(&data->pong_timer); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 243 | } |
| 244 | } else if (hh->proto == IPAC_PROTO_SCCP) { |
| 245 | sccp_system_incoming(msg); |
Holger Hans Peter Freyther | 368a0a7 | 2011-01-07 16:54:46 +0100 | [diff] [blame] | 246 | } else if (hh->proto == IPAC_PROTO_MGCP_OLD) { |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 247 | mgcp_forward(data, msg); |
Holger Hans Peter Freyther | a78b602 | 2011-04-23 15:58:24 +0200 | [diff] [blame] | 248 | } else if (hh->proto == IPAC_PROTO_OSMO) { |
| 249 | osmo_ext_handle(data, msg); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | msgb_free(msg); |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static void send_ping(struct osmo_msc_data *data) |
| 257 | { |
| 258 | struct msgb *msg; |
| 259 | |
| 260 | msg = msgb_alloc_headroom(4096, 128, "ping"); |
| 261 | if (!msg) { |
| 262 | LOGP(DMSC, LOGL_ERROR, "Failed to create PING.\n"); |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | msg->l2h = msgb_put(msg, 1); |
| 267 | msg->l2h[0] = IPAC_MSGT_PING; |
| 268 | |
| 269 | msc_queue_write(data->msc_con, msg, IPAC_PROTO_IPACCESS); |
| 270 | } |
| 271 | |
| 272 | static void msc_ping_timeout_cb(void *_data) |
| 273 | { |
| 274 | struct osmo_msc_data *data = (struct osmo_msc_data *) _data; |
| 275 | if (data->ping_timeout < 0) |
| 276 | return; |
| 277 | |
| 278 | send_ping(data); |
| 279 | |
| 280 | /* send another ping in 20 seconds */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 281 | osmo_timer_schedule(&data->ping_timer, data->ping_timeout, 0); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 282 | |
| 283 | /* also start a pong timer */ |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 284 | osmo_timer_schedule(&data->pong_timer, data->pong_timeout, 0); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static void msc_pong_timeout_cb(void *_data) |
| 288 | { |
| 289 | struct osmo_msc_data *data = (struct osmo_msc_data *) _data; |
| 290 | |
| 291 | LOGP(DMSC, LOGL_ERROR, "MSC didn't answer PING. Closing connection.\n"); |
| 292 | bsc_msc_lost(data->msc_con); |
| 293 | } |
| 294 | |
| 295 | static void msc_connection_connected(struct bsc_msc_connection *con) |
| 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 | int ret, on; |
| 300 | on = 1; |
| 301 | ret = setsockopt(con->write_queue.bfd.fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)); |
| 302 | if (ret != 0) |
| 303 | LOGP(DMSC, LOGL_ERROR, "Failed to set TCP_NODELAY: %s\n", strerror(errno)); |
| 304 | |
| 305 | data = (struct osmo_msc_data *) con->write_queue.bfd.data; |
Holger Hans Peter Freyther | 0425c38 | 2010-11-03 14:46:56 +0100 | [diff] [blame] | 306 | msc_ping_timeout_cb(data); |
Holger Hans Peter Freyther | 0d71163 | 2010-09-16 02:30:36 +0800 | [diff] [blame] | 307 | |
| 308 | sig.data = data; |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 309 | osmo_signal_dispatch(SS_MSC, S_MSC_CONNECTED, &sig); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* |
| 313 | * The connection to the MSC was lost and we will need to free all |
| 314 | * resources and then attempt to reconnect. |
| 315 | */ |
| 316 | static void msc_connection_was_lost(struct bsc_msc_connection *msc) |
| 317 | { |
Holger Hans Peter Freyther | 0d71163 | 2010-09-16 02:30:36 +0800 | [diff] [blame] | 318 | struct msc_signal_data sig; |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 319 | struct osmo_msc_data *data; |
| 320 | |
| 321 | LOGP(DMSC, LOGL_ERROR, "Lost MSC connection. Freing stuff.\n"); |
| 322 | |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 323 | data = (struct osmo_msc_data *) msc->write_queue.bfd.data; |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 324 | osmo_timer_del(&data->ping_timer); |
| 325 | osmo_timer_del(&data->pong_timer); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 326 | |
Holger Hans Peter Freyther | 0d71163 | 2010-09-16 02:30:36 +0800 | [diff] [blame] | 327 | sig.data = data; |
Pablo Neira Ayuso | bbc5b99 | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 328 | osmo_signal_dispatch(SS_MSC, S_MSC_LOST, &sig); |
Holger Hans Peter Freyther | 0d71163 | 2010-09-16 02:30:36 +0800 | [diff] [blame] | 329 | |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 330 | msc->is_authenticated = 0; |
| 331 | bsc_msc_schedule_connect(msc); |
| 332 | } |
| 333 | |
Holger Hans Peter Freyther | 2a9eeaa | 2011-04-23 16:37:58 +0200 | [diff] [blame] | 334 | static void send_lacs(struct gsm_network *net, struct bsc_msc_connection *conn) |
| 335 | { |
| 336 | struct ipac_ext_lac_cmd *lac; |
| 337 | struct gsm_bts *bts; |
| 338 | struct msgb *msg; |
| 339 | int lacs = 0; |
| 340 | |
| 341 | if (llist_empty(&net->bts_list)) { |
| 342 | LOGP(DMSC, LOGL_ERROR, "No BTSs configured. Not sending LACs.\n"); |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | msg = msgb_alloc_headroom(4096, 128, "LAC Command"); |
| 347 | if (!msg) { |
| 348 | LOGP(DMSC, LOGL_ERROR, "Failed to create the LAC command.\n"); |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | lac = (struct ipac_ext_lac_cmd *) msgb_put(msg, sizeof(*lac)); |
| 353 | lac->add_remove = 1; |
| 354 | |
| 355 | llist_for_each_entry(bts, &net->bts_list, list) { |
| 356 | if (lacs++ == 0) |
| 357 | lac->lac = htons(bts->location_area_code); |
| 358 | else |
| 359 | msgb_put_u16(msg, htons(bts->location_area_code)); |
| 360 | } |
| 361 | |
| 362 | lac->nr_extra_lacs = lacs - 1; |
| 363 | ipaccess_prepend_header_ext(msg, IPAC_PROTO_EXT_LAC); |
| 364 | msc_queue_write(conn, msg, IPAC_PROTO_OSMO); |
| 365 | } |
| 366 | |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 367 | static void initialize_if_needed(struct bsc_msc_connection *conn) |
| 368 | { |
| 369 | struct msgb *msg; |
| 370 | |
| 371 | if (!conn->is_authenticated) { |
| 372 | /* send a gsm 08.08 reset message from here */ |
| 373 | msg = gsm0808_create_reset(); |
| 374 | if (!msg) { |
| 375 | LOGP(DMSC, LOGL_ERROR, "Failed to create the reset message.\n"); |
| 376 | return; |
| 377 | } |
| 378 | |
Holger Hans Peter Freyther | 784ca9c | 2011-06-04 16:59:37 +0200 | [diff] [blame] | 379 | sccp_write(msg, &sccp_ssn_bssap, &sccp_ssn_bssap, 0, NULL); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 380 | msgb_free(msg); |
| 381 | conn->is_authenticated = 1; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | static void send_id_get_response(struct osmo_msc_data *data, int fd) |
| 386 | { |
| 387 | struct msgb *msg; |
| 388 | |
| 389 | msg = bsc_msc_id_get_resp(data->bsc_token); |
| 390 | if (!msg) |
| 391 | return; |
| 392 | msc_queue_write(data->msc_con, msg, IPAC_PROTO_IPACCESS); |
| 393 | } |
| 394 | |
| 395 | int osmo_bsc_msc_init(struct gsm_network *network) |
| 396 | { |
| 397 | struct osmo_msc_data *data = network->msc_data; |
| 398 | |
| 399 | if (mgcp_create_port(data) != 0) |
| 400 | return -1; |
| 401 | |
Holger Hans Peter Freyther | e1880105 | 2011-04-23 23:31:31 +0200 | [diff] [blame] | 402 | data->msc_con = bsc_msc_create(data, &data->dests); |
Holger Hans Peter Freyther | 5832b3e | 2010-09-16 02:14:41 +0800 | [diff] [blame] | 403 | if (!data->msc_con) { |
| 404 | LOGP(DMSC, LOGL_ERROR, "Creating the MSC network connection failed.\n"); |
| 405 | return -1; |
| 406 | } |
| 407 | |
| 408 | data->ping_timer.cb = msc_ping_timeout_cb; |
| 409 | data->ping_timer.data = data; |
| 410 | data->pong_timer.cb = msc_pong_timeout_cb; |
| 411 | data->pong_timer.data = data; |
| 412 | |
| 413 | data->msc_con->write_queue.bfd.data = data; |
| 414 | data->msc_con->connection_loss = msc_connection_was_lost; |
| 415 | data->msc_con->connected = msc_connection_connected; |
| 416 | 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] | 417 | 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] | 418 | bsc_msc_connect(data->msc_con); |
| 419 | |
| 420 | return 0; |
| 421 | } |