Holger Hans Peter Freyther | 689e3cc | 2010-11-03 16:40:03 +0100 | [diff] [blame] | 1 | /* Interaction with the SCCP subsystem */ |
| 2 | /* |
| 3 | * (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 4 | * (C) 2009-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 <openbsc/gsm_data.h> |
Holger Hans Peter Freyther | b93e35d | 2010-11-06 18:08:43 +0100 | [diff] [blame] | 24 | #include <openbsc/osmo_bsc.h> |
| 25 | #include <openbsc/osmo_bsc_grace.h> |
Holger Hans Peter Freyther | 689e3cc | 2010-11-03 16:40:03 +0100 | [diff] [blame] | 26 | #include <openbsc/osmo_msc_data.h> |
| 27 | #include <openbsc/debug.h> |
Holger Hans Peter Freyther | fa25bd6 | 2010-11-06 23:56:43 +0100 | [diff] [blame^] | 28 | #include <openbsc/ipaccess.h> |
Holger Hans Peter Freyther | b4713b8 | 2010-11-06 23:50:06 +0100 | [diff] [blame] | 29 | #include <openbsc/signal.h> |
Holger Hans Peter Freyther | 689e3cc | 2010-11-03 16:40:03 +0100 | [diff] [blame] | 30 | |
| 31 | #include <osmocore/gsm0808.h> |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 32 | #include <osmocore/talloc.h> |
Holger Hans Peter Freyther | 689e3cc | 2010-11-03 16:40:03 +0100 | [diff] [blame] | 33 | #include <osmocore/protocol/gsm_08_08.h> |
| 34 | |
| 35 | #include <osmocom/sccp/sccp.h> |
| 36 | |
Holger Hans Peter Freyther | 94cc41f | 2010-11-06 20:23:58 +0100 | [diff] [blame] | 37 | /* SCCP helper */ |
| 38 | #define SCCP_IT_TIMER 60 |
| 39 | |
Holger Hans Peter Freyther | b93e35d | 2010-11-06 18:08:43 +0100 | [diff] [blame] | 40 | static LLIST_HEAD(active_connections); |
| 41 | |
| 42 | static void msc_outgoing_sccp_data(struct sccp_connection *conn, |
| 43 | struct msgb *msg, unsigned int len) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | static void msc_outgoing_sccp_state(struct sccp_connection *conn, int old_state) |
| 48 | { |
| 49 | } |
| 50 | |
Holger Hans Peter Freyther | b4713b8 | 2010-11-06 23:50:06 +0100 | [diff] [blame] | 51 | static void bsc_sccp_force_free(struct osmo_bsc_sccp_con *data) |
| 52 | { |
| 53 | if (data->conn) { |
| 54 | gsm0808_clear(data->conn); |
| 55 | subscr_con_free(data->conn); |
| 56 | data->conn = NULL; |
| 57 | } |
| 58 | |
| 59 | sccp_connection_force_free(data->sccp); |
| 60 | data->sccp = NULL; |
| 61 | bsc_delete_connection(data); |
| 62 | } |
| 63 | |
Holger Hans Peter Freyther | 94cc41f | 2010-11-06 20:23:58 +0100 | [diff] [blame] | 64 | static void sccp_it_timeout(void *_data) |
| 65 | { |
| 66 | struct osmo_bsc_sccp_con *data = |
| 67 | (struct osmo_bsc_sccp_con *) _data; |
| 68 | |
| 69 | sccp_connection_send_it(data->sccp); |
| 70 | bsc_schedule_timer(&data->sccp_it_timeout, SCCP_IT_TIMER, 0); |
| 71 | } |
| 72 | |
| 73 | static void sccp_cc_timeout(void *_data) |
| 74 | { |
| 75 | struct osmo_bsc_sccp_con *data = |
| 76 | (struct osmo_bsc_sccp_con *) _data; |
| 77 | |
| 78 | if (data->sccp->connection_state >= SCCP_CONNECTION_STATE_ESTABLISHED) |
| 79 | return; |
| 80 | |
| 81 | LOGP(DMSC, LOGL_ERROR, "The connection was never established.\n"); |
Holger Hans Peter Freyther | b4713b8 | 2010-11-06 23:50:06 +0100 | [diff] [blame] | 82 | bsc_sccp_force_free(data); |
Holger Hans Peter Freyther | 94cc41f | 2010-11-06 20:23:58 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Holger Hans Peter Freyther | 689e3cc | 2010-11-03 16:40:03 +0100 | [diff] [blame] | 85 | static void msc_sccp_write_ipa(struct sccp_connection *conn, struct msgb *msg, void *data) |
| 86 | { |
Holger Hans Peter Freyther | fa25bd6 | 2010-11-06 23:56:43 +0100 | [diff] [blame^] | 87 | struct gsm_network *net = (struct gsm_network *) data; |
| 88 | msc_queue_write(net->msc_data->msc_con, msg, IPAC_PROTO_SCCP); |
Holger Hans Peter Freyther | 689e3cc | 2010-11-03 16:40:03 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static int msc_sccp_accept(struct sccp_connection *connection, void *data) |
| 92 | { |
| 93 | LOGP(DMSC, LOGL_DEBUG, "Rejecting incoming SCCP connection.\n"); |
| 94 | return -1; |
| 95 | } |
| 96 | |
| 97 | static int msc_sccp_read(struct msgb *msgb, unsigned int length, void *data) |
| 98 | { |
| 99 | struct bssmap_header *bs; |
| 100 | |
| 101 | LOGP(DMSC, LOGL_DEBUG, "Incoming SCCP message ftom MSC: %s\n", |
| 102 | hexdump(msgb->l3h, length)); |
| 103 | |
| 104 | if (length < sizeof(*bs)) { |
| 105 | LOGP(DMSC, LOGL_ERROR, "The header is too short.\n"); |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | bs = (struct bssmap_header *) msgb->l3h; |
| 110 | if (bs->length < length - sizeof(*bs)) |
| 111 | return -1; |
| 112 | |
| 113 | switch (bs->type) { |
| 114 | case BSSAP_MSG_BSS_MANAGEMENT: |
Holger Hans Peter Freyther | e0f3bb5 | 2010-11-03 18:46:52 +0100 | [diff] [blame] | 115 | LOGP(DMSC, LOGL_ERROR, "BSS management not implemented.\n"); |
Holger Hans Peter Freyther | 689e3cc | 2010-11-03 16:40:03 +0100 | [diff] [blame] | 116 | break; |
| 117 | default: |
| 118 | LOGP(DMSC, LOGL_ERROR, "Unimplemented msg type: %d\n", bs->type); |
| 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
Holger Hans Peter Freyther | 13cde94 | 2010-11-04 11:48:49 +0100 | [diff] [blame] | 124 | int bsc_queue_for_msc(struct gsm_subscriber_connection *conn, struct msgb *msg) |
| 125 | { |
Holger Hans Peter Freyther | 912a6ad | 2010-11-06 23:35:02 +0100 | [diff] [blame] | 126 | struct sccp_connection *sccp = conn->sccp_con->sccp; |
| 127 | |
| 128 | if (sccp->connection_state != SCCP_CONNECTION_STATE_ESTABLISHED) { |
| 129 | LOGP(DMSC, LOGL_ERROR, "The connection is not established.\n"); |
| 130 | msgb_free(msg); |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | sccp_connection_write(sccp, msg); |
Holger Hans Peter Freyther | 13cde94 | 2010-11-04 11:48:49 +0100 | [diff] [blame] | 135 | msgb_free(msg); |
| 136 | return 0; |
| 137 | } |
| 138 | |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 139 | int bsc_create_new_connection(struct gsm_subscriber_connection *conn) |
Holger Hans Peter Freyther | 1b8e90a | 2010-11-05 11:10:46 +0100 | [diff] [blame] | 140 | { |
Holger Hans Peter Freyther | b93e35d | 2010-11-06 18:08:43 +0100 | [diff] [blame] | 141 | struct gsm_network *net; |
| 142 | struct osmo_bsc_sccp_con *bsc_con; |
| 143 | struct sccp_connection *sccp; |
| 144 | |
| 145 | net = conn->bts->network; |
| 146 | if (!net->msc_data->msc_con->is_authenticated) { |
| 147 | LOGP(DMSC, LOGL_ERROR, "Not connected to a MSC. Not forwarding data.\n"); |
| 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | if (!bsc_grace_allow_new_connection(net)) { |
| 152 | LOGP(DMSC, LOGL_NOTICE, "BSC in grace period. No new connections.\n"); |
| 153 | return -1; |
| 154 | } |
| 155 | |
| 156 | sccp = sccp_connection_socket(); |
| 157 | if (!sccp) { |
| 158 | LOGP(DMSC, LOGL_ERROR, "Failed to allocate memory.\n"); |
| 159 | return -ENOMEM; |
| 160 | } |
| 161 | |
| 162 | bsc_con = talloc_zero(conn->bts, struct osmo_bsc_sccp_con); |
| 163 | if (!bsc_con) { |
| 164 | LOGP(DMSC, LOGL_ERROR, "Failed to allocate.\n"); |
| 165 | sccp_connection_free(sccp); |
| 166 | return -1; |
| 167 | } |
| 168 | |
| 169 | /* callbacks */ |
| 170 | sccp->state_cb = msc_outgoing_sccp_state; |
| 171 | sccp->data_cb = msc_outgoing_sccp_data; |
| 172 | sccp->data_ctx = bsc_con; |
| 173 | |
Holger Hans Peter Freyther | 94cc41f | 2010-11-06 20:23:58 +0100 | [diff] [blame] | 174 | /* prepare the timers */ |
| 175 | bsc_con->sccp_it_timeout.cb = sccp_it_timeout; |
| 176 | bsc_con->sccp_it_timeout.data = bsc_con; |
| 177 | bsc_con->sccp_cc_timeout.cb = sccp_cc_timeout; |
| 178 | bsc_con->sccp_cc_timeout.data = bsc_con; |
| 179 | |
Holger Hans Peter Freyther | b93e35d | 2010-11-06 18:08:43 +0100 | [diff] [blame] | 180 | bsc_con->sccp = sccp; |
| 181 | bsc_con->msc_con = net->msc_data->msc_con; |
Holger Hans Peter Freyther | 288b80a | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 182 | bsc_con->conn = conn; |
Holger Hans Peter Freyther | b93e35d | 2010-11-06 18:08:43 +0100 | [diff] [blame] | 183 | llist_add(&bsc_con->entry, &active_connections); |
| 184 | conn->sccp_con = bsc_con; |
| 185 | return 0; |
Holger Hans Peter Freyther | 1b8e90a | 2010-11-05 11:10:46 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Holger Hans Peter Freyther | 288b80a | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 188 | int bsc_open_connection(struct osmo_bsc_sccp_con *conn, struct msgb *msg) |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 189 | { |
| 190 | LOGP(DMSC, LOGL_ERROR, "Not implemented yet.\n"); |
| 191 | return -1; |
| 192 | } |
| 193 | |
Holger Hans Peter Freyther | 288b80a | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 194 | int bsc_delete_connection(struct osmo_bsc_sccp_con *sccp) |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 195 | { |
Holger Hans Peter Freyther | b93e35d | 2010-11-06 18:08:43 +0100 | [diff] [blame] | 196 | if (!sccp) |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 197 | return 0; |
| 198 | |
Holger Hans Peter Freyther | 288b80a | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 199 | if (sccp->conn) |
| 200 | LOGP(DMSC, LOGL_ERROR, "Should have been cleared.\n"); |
| 201 | |
Holger Hans Peter Freyther | b93e35d | 2010-11-06 18:08:43 +0100 | [diff] [blame] | 202 | llist_del(&sccp->entry); |
| 203 | bsc_del_timer(&sccp->sccp_it_timeout); |
| 204 | bsc_del_timer(&sccp->sccp_cc_timeout); |
| 205 | talloc_free(sccp); |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | |
Holger Hans Peter Freyther | b4713b8 | 2010-11-06 23:50:06 +0100 | [diff] [blame] | 209 | static void bsc_close_connections(struct bsc_msc_connection *msc_con) |
| 210 | { |
| 211 | struct osmo_bsc_sccp_con *con, *tmp; |
| 212 | |
| 213 | llist_for_each_entry_safe(con, tmp, &active_connections, entry) |
| 214 | bsc_sccp_force_free(con); |
| 215 | } |
| 216 | |
| 217 | static int handle_msc_signal(unsigned int subsys, unsigned int signal, |
| 218 | void *handler_data, void *signal_data) |
| 219 | { |
| 220 | struct osmo_msc_data *data; |
| 221 | |
| 222 | if (subsys != SS_MSC) |
| 223 | return 0; |
| 224 | |
| 225 | data = (struct osmo_msc_data *) signal_data; |
| 226 | if (signal == S_MSC_LOST) |
| 227 | bsc_close_connections(data->msc_con); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
Holger Hans Peter Freyther | 689e3cc | 2010-11-03 16:40:03 +0100 | [diff] [blame] | 232 | int osmo_bsc_sccp_init(struct gsm_network *gsmnet) |
| 233 | { |
| 234 | sccp_set_log_area(DSCCP); |
| 235 | sccp_system_init(msc_sccp_write_ipa, gsmnet); |
| 236 | sccp_connection_set_incoming(&sccp_ssn_bssap, msc_sccp_accept, NULL); |
| 237 | sccp_set_read(&sccp_ssn_bssap, msc_sccp_read, NULL); |
| 238 | |
Holger Hans Peter Freyther | b4713b8 | 2010-11-06 23:50:06 +0100 | [diff] [blame] | 239 | register_signal_handler(SS_MSC, handle_msc_signal, gsmnet); |
| 240 | |
Holger Hans Peter Freyther | 689e3cc | 2010-11-03 16:40:03 +0100 | [diff] [blame] | 241 | return 0; |
| 242 | } |