Holger Hans Peter Freyther | f3d1ae7 | 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 | b8781d2 | 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 | f3d1ae7 | 2010-11-03 16:40:03 +0100 | [diff] [blame] | 26 | #include <openbsc/osmo_msc_data.h> |
| 27 | #include <openbsc/debug.h> |
| 28 | |
| 29 | #include <osmocore/gsm0808.h> |
Holger Hans Peter Freyther | 9b17c76 | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 30 | #include <osmocore/talloc.h> |
Holger Hans Peter Freyther | f3d1ae7 | 2010-11-03 16:40:03 +0100 | [diff] [blame] | 31 | #include <osmocore/protocol/gsm_08_08.h> |
| 32 | |
| 33 | #include <osmocom/sccp/sccp.h> |
| 34 | |
Holger Hans Peter Freyther | c97d9b4 | 2010-11-06 20:23:58 +0100 | [diff] [blame^] | 35 | /* SCCP helper */ |
| 36 | #define SCCP_IT_TIMER 60 |
| 37 | |
Holger Hans Peter Freyther | b8781d2 | 2010-11-06 18:08:43 +0100 | [diff] [blame] | 38 | static LLIST_HEAD(active_connections); |
| 39 | |
| 40 | static void msc_outgoing_sccp_data(struct sccp_connection *conn, |
| 41 | struct msgb *msg, unsigned int len) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | static void msc_outgoing_sccp_state(struct sccp_connection *conn, int old_state) |
| 46 | { |
| 47 | } |
| 48 | |
Holger Hans Peter Freyther | c97d9b4 | 2010-11-06 20:23:58 +0100 | [diff] [blame^] | 49 | static void sccp_it_timeout(void *_data) |
| 50 | { |
| 51 | struct osmo_bsc_sccp_con *data = |
| 52 | (struct osmo_bsc_sccp_con *) _data; |
| 53 | |
| 54 | sccp_connection_send_it(data->sccp); |
| 55 | bsc_schedule_timer(&data->sccp_it_timeout, SCCP_IT_TIMER, 0); |
| 56 | } |
| 57 | |
| 58 | static void sccp_cc_timeout(void *_data) |
| 59 | { |
| 60 | struct osmo_bsc_sccp_con *data = |
| 61 | (struct osmo_bsc_sccp_con *) _data; |
| 62 | |
| 63 | if (data->sccp->connection_state >= SCCP_CONNECTION_STATE_ESTABLISHED) |
| 64 | return; |
| 65 | |
| 66 | LOGP(DMSC, LOGL_ERROR, "The connection was never established.\n"); |
| 67 | |
| 68 | |
| 69 | if (data->conn) { |
| 70 | gsm0808_clear(data->conn); |
| 71 | subscr_con_free(data->conn); |
| 72 | data->conn = NULL; |
| 73 | } |
| 74 | bsc_delete_connection(data); |
| 75 | } |
| 76 | |
Holger Hans Peter Freyther | f3d1ae7 | 2010-11-03 16:40:03 +0100 | [diff] [blame] | 77 | static void msc_sccp_write_ipa(struct sccp_connection *conn, struct msgb *msg, void *data) |
| 78 | { |
| 79 | LOGP(DMSC, LOGL_ERROR, "Writing is not implemented.\n"); |
| 80 | msgb_free(msg); |
| 81 | } |
| 82 | |
| 83 | static int msc_sccp_accept(struct sccp_connection *connection, void *data) |
| 84 | { |
| 85 | LOGP(DMSC, LOGL_DEBUG, "Rejecting incoming SCCP connection.\n"); |
| 86 | return -1; |
| 87 | } |
| 88 | |
| 89 | static int msc_sccp_read(struct msgb *msgb, unsigned int length, void *data) |
| 90 | { |
| 91 | struct bssmap_header *bs; |
| 92 | |
| 93 | LOGP(DMSC, LOGL_DEBUG, "Incoming SCCP message ftom MSC: %s\n", |
| 94 | hexdump(msgb->l3h, length)); |
| 95 | |
| 96 | if (length < sizeof(*bs)) { |
| 97 | LOGP(DMSC, LOGL_ERROR, "The header is too short.\n"); |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | bs = (struct bssmap_header *) msgb->l3h; |
| 102 | if (bs->length < length - sizeof(*bs)) |
| 103 | return -1; |
| 104 | |
| 105 | switch (bs->type) { |
| 106 | case BSSAP_MSG_BSS_MANAGEMENT: |
Holger Hans Peter Freyther | 1999cf1 | 2010-11-03 18:46:52 +0100 | [diff] [blame] | 107 | LOGP(DMSC, LOGL_ERROR, "BSS management not implemented.\n"); |
Holger Hans Peter Freyther | f3d1ae7 | 2010-11-03 16:40:03 +0100 | [diff] [blame] | 108 | break; |
| 109 | default: |
| 110 | LOGP(DMSC, LOGL_ERROR, "Unimplemented msg type: %d\n", bs->type); |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
Holger Hans Peter Freyther | 4582586 | 2010-11-04 11:48:49 +0100 | [diff] [blame] | 116 | int bsc_queue_for_msc(struct gsm_subscriber_connection *conn, struct msgb *msg) |
| 117 | { |
| 118 | LOGP(DMSC, LOGL_ERROR, "Sending SCCP messages is not yet implemented.\n"); |
| 119 | msgb_free(msg); |
| 120 | return 0; |
| 121 | } |
| 122 | |
Holger Hans Peter Freyther | 9b17c76 | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 123 | int bsc_create_new_connection(struct gsm_subscriber_connection *conn) |
Holger Hans Peter Freyther | cc48fb8 | 2010-11-05 11:10:46 +0100 | [diff] [blame] | 124 | { |
Holger Hans Peter Freyther | b8781d2 | 2010-11-06 18:08:43 +0100 | [diff] [blame] | 125 | struct gsm_network *net; |
| 126 | struct osmo_bsc_sccp_con *bsc_con; |
| 127 | struct sccp_connection *sccp; |
| 128 | |
| 129 | net = conn->bts->network; |
| 130 | if (!net->msc_data->msc_con->is_authenticated) { |
| 131 | LOGP(DMSC, LOGL_ERROR, "Not connected to a MSC. Not forwarding data.\n"); |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | if (!bsc_grace_allow_new_connection(net)) { |
| 136 | LOGP(DMSC, LOGL_NOTICE, "BSC in grace period. No new connections.\n"); |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | sccp = sccp_connection_socket(); |
| 141 | if (!sccp) { |
| 142 | LOGP(DMSC, LOGL_ERROR, "Failed to allocate memory.\n"); |
| 143 | return -ENOMEM; |
| 144 | } |
| 145 | |
| 146 | bsc_con = talloc_zero(conn->bts, struct osmo_bsc_sccp_con); |
| 147 | if (!bsc_con) { |
| 148 | LOGP(DMSC, LOGL_ERROR, "Failed to allocate.\n"); |
| 149 | sccp_connection_free(sccp); |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | /* callbacks */ |
| 154 | sccp->state_cb = msc_outgoing_sccp_state; |
| 155 | sccp->data_cb = msc_outgoing_sccp_data; |
| 156 | sccp->data_ctx = bsc_con; |
| 157 | |
Holger Hans Peter Freyther | c97d9b4 | 2010-11-06 20:23:58 +0100 | [diff] [blame^] | 158 | /* prepare the timers */ |
| 159 | bsc_con->sccp_it_timeout.cb = sccp_it_timeout; |
| 160 | bsc_con->sccp_it_timeout.data = bsc_con; |
| 161 | bsc_con->sccp_cc_timeout.cb = sccp_cc_timeout; |
| 162 | bsc_con->sccp_cc_timeout.data = bsc_con; |
| 163 | |
Holger Hans Peter Freyther | b8781d2 | 2010-11-06 18:08:43 +0100 | [diff] [blame] | 164 | bsc_con->sccp = sccp; |
| 165 | bsc_con->msc_con = net->msc_data->msc_con; |
Holger Hans Peter Freyther | 7e90108 | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 166 | bsc_con->conn = conn; |
Holger Hans Peter Freyther | b8781d2 | 2010-11-06 18:08:43 +0100 | [diff] [blame] | 167 | llist_add(&bsc_con->entry, &active_connections); |
| 168 | conn->sccp_con = bsc_con; |
| 169 | return 0; |
Holger Hans Peter Freyther | cc48fb8 | 2010-11-05 11:10:46 +0100 | [diff] [blame] | 170 | } |
| 171 | |
Holger Hans Peter Freyther | 7e90108 | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 172 | int bsc_open_connection(struct osmo_bsc_sccp_con *conn, struct msgb *msg) |
Holger Hans Peter Freyther | 9b17c76 | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 173 | { |
| 174 | LOGP(DMSC, LOGL_ERROR, "Not implemented yet.\n"); |
| 175 | return -1; |
| 176 | } |
| 177 | |
Holger Hans Peter Freyther | 7e90108 | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 178 | int bsc_delete_connection(struct osmo_bsc_sccp_con *sccp) |
Holger Hans Peter Freyther | 9b17c76 | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 179 | { |
Holger Hans Peter Freyther | b8781d2 | 2010-11-06 18:08:43 +0100 | [diff] [blame] | 180 | if (!sccp) |
Holger Hans Peter Freyther | 9b17c76 | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 181 | return 0; |
| 182 | |
Holger Hans Peter Freyther | 7e90108 | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 183 | if (sccp->conn) |
| 184 | LOGP(DMSC, LOGL_ERROR, "Should have been cleared.\n"); |
| 185 | |
Holger Hans Peter Freyther | b8781d2 | 2010-11-06 18:08:43 +0100 | [diff] [blame] | 186 | llist_del(&sccp->entry); |
| 187 | bsc_del_timer(&sccp->sccp_it_timeout); |
| 188 | bsc_del_timer(&sccp->sccp_cc_timeout); |
| 189 | talloc_free(sccp); |
Holger Hans Peter Freyther | 9b17c76 | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | |
Holger Hans Peter Freyther | f3d1ae7 | 2010-11-03 16:40:03 +0100 | [diff] [blame] | 193 | int osmo_bsc_sccp_init(struct gsm_network *gsmnet) |
| 194 | { |
| 195 | sccp_set_log_area(DSCCP); |
| 196 | sccp_system_init(msc_sccp_write_ipa, gsmnet); |
| 197 | sccp_connection_set_incoming(&sccp_ssn_bssap, msc_sccp_accept, NULL); |
| 198 | sccp_set_read(&sccp_ssn_bssap, msc_sccp_read, NULL); |
| 199 | |
| 200 | return 0; |
| 201 | } |