Holger Hans Peter Freyther | 4d61417 | 2011-08-15 15:53:00 +0200 | [diff] [blame] | 1 | /* (C) 2009-2011 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 2 | * (C) 2009-2011 by On-Waves |
Holger Hans Peter Freyther | 22e9ac3 | 2010-07-05 16:02:04 +0800 | [diff] [blame] | 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 6 | * it under the terms of the GNU Affero General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
Holger Hans Peter Freyther | 22e9ac3 | 2010-07-05 16:02:04 +0800 | [diff] [blame] | 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 13 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 22e9ac3 | 2010-07-05 16:02:04 +0800 | [diff] [blame] | 14 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 15 | * You should have received a copy of the GNU Affero General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Holger Hans Peter Freyther | 22e9ac3 | 2010-07-05 16:02:04 +0800 | [diff] [blame] | 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <openbsc/osmo_bsc.h> |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 21 | #include <openbsc/osmo_msc_data.h> |
Holger Hans Peter Freyther | b6d7014 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 22 | #include <openbsc/debug.h> |
| 23 | |
Harald Welte | 8b1713a | 2011-03-23 18:26:56 +0100 | [diff] [blame] | 24 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
| 25 | #include <osmocom/gsm/gsm0808.h> |
Holger Hans Peter Freyther | a62919e | 2010-11-04 11:59:41 +0100 | [diff] [blame] | 26 | |
Holger Hans Peter Freyther | 326a124 | 2011-06-09 14:44:47 +0200 | [diff] [blame] | 27 | #include <osmocom/sccp/sccp.h> |
| 28 | |
Holger Hans Peter Freyther | b6d7014 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 29 | #define return_when_not_connected(conn) \ |
| 30 | if (!conn->sccp_con) {\ |
| 31 | LOGP(DMSC, LOGL_ERROR, "MSC Connection not present.\n"); \ |
| 32 | return; \ |
| 33 | } |
| 34 | |
| 35 | #define return_when_not_connected_val(conn, ret) \ |
| 36 | if (!conn->sccp_con) {\ |
| 37 | LOGP(DMSC, LOGL_ERROR, "MSC Connection not present.\n"); \ |
| 38 | return ret; \ |
| 39 | } |
Holger Hans Peter Freyther | 22e9ac3 | 2010-07-05 16:02:04 +0800 | [diff] [blame] | 40 | |
Holger Hans Peter Freyther | 5f2184c | 2010-11-05 11:02:28 +0100 | [diff] [blame] | 41 | #define queue_msg_or_return(resp) \ |
| 42 | if (!resp) { \ |
| 43 | LOGP(DMSC, LOGL_ERROR, "Failed to allocate response.\n"); \ |
| 44 | return; \ |
| 45 | } \ |
Holger Hans Peter Freyther | 1f334cc | 2010-11-10 09:31:41 +0100 | [diff] [blame] | 46 | bsc_queue_for_msc(conn->sccp_con, resp); |
Holger Hans Peter Freyther | 5f2184c | 2010-11-05 11:02:28 +0100 | [diff] [blame] | 47 | |
Holger Hans Peter Freyther | 5e84584 | 2011-06-04 14:51:51 +0200 | [diff] [blame] | 48 | static uint16_t get_network_code_for_msc(struct osmo_msc_data *msc) |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 49 | { |
Holger Hans Peter Freyther | 5e84584 | 2011-06-04 14:51:51 +0200 | [diff] [blame] | 50 | if (msc->core_ncc != -1) |
| 51 | return msc->core_ncc; |
| 52 | return msc->network->network_code; |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 53 | } |
| 54 | |
Holger Hans Peter Freyther | 5e84584 | 2011-06-04 14:51:51 +0200 | [diff] [blame] | 55 | static uint16_t get_country_code_for_msc(struct osmo_msc_data *msc) |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 56 | { |
Holger Hans Peter Freyther | 5e84584 | 2011-06-04 14:51:51 +0200 | [diff] [blame] | 57 | if (msc->core_mcc != -1) |
| 58 | return msc->core_mcc; |
| 59 | return msc->network->country_code; |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 62 | static void bsc_sapi_n_reject(struct gsm_subscriber_connection *conn, int dlci) |
| 63 | { |
Holger Hans Peter Freyther | 313324e | 2010-11-04 12:28:32 +0100 | [diff] [blame] | 64 | struct msgb *resp; |
Holger Hans Peter Freyther | b6d7014 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 65 | return_when_not_connected(conn); |
Holger Hans Peter Freyther | 313324e | 2010-11-04 12:28:32 +0100 | [diff] [blame] | 66 | |
Harald Welte | 98833af | 2011-07-12 00:05:11 +0200 | [diff] [blame] | 67 | LOGP(DMSC, LOGL_NOTICE, "Tx MSC SAPI N REJECT DLCI=0x%02x\n", dlci); |
| 68 | |
Holger Hans Peter Freyther | 313324e | 2010-11-04 12:28:32 +0100 | [diff] [blame] | 69 | resp = gsm0808_create_sapi_reject(dlci); |
Holger Hans Peter Freyther | 5f2184c | 2010-11-05 11:02:28 +0100 | [diff] [blame] | 70 | queue_msg_or_return(resp); |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static void bsc_cipher_mode_compl(struct gsm_subscriber_connection *conn, |
| 74 | struct msgb *msg, uint8_t chosen_encr) |
| 75 | { |
Holger Hans Peter Freyther | a62919e | 2010-11-04 11:59:41 +0100 | [diff] [blame] | 76 | struct msgb *resp; |
Holger Hans Peter Freyther | b6d7014 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 77 | return_when_not_connected(conn); |
Holger Hans Peter Freyther | a62919e | 2010-11-04 11:59:41 +0100 | [diff] [blame] | 78 | |
| 79 | LOGP(DMSC, LOGL_DEBUG, "CIPHER MODE COMPLETE from MS, forwarding to MSC\n"); |
| 80 | resp = gsm0808_create_cipher_complete(msg, chosen_encr); |
Holger Hans Peter Freyther | 5f2184c | 2010-11-05 11:02:28 +0100 | [diff] [blame] | 81 | queue_msg_or_return(resp); |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 82 | } |
| 83 | |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 84 | /* |
| 85 | * Instruct to reserve data for a new connectiom, create the complete |
| 86 | * layer three message, send it to open the connection. |
| 87 | */ |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 88 | static int bsc_compl_l3(struct gsm_subscriber_connection *conn, struct msgb *msg, |
| 89 | uint16_t chosen_channel) |
| 90 | { |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 91 | struct msgb *resp; |
Holger Hans Peter Freyther | 508e662 | 2011-06-07 11:40:20 +0200 | [diff] [blame] | 92 | struct osmo_msc_data *msc; |
Holger Hans Peter Freyther | 5e84584 | 2011-06-04 14:51:51 +0200 | [diff] [blame] | 93 | uint16_t network_code; |
| 94 | uint16_t country_code; |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 95 | |
Harald Welte | 98833af | 2011-07-12 00:05:11 +0200 | [diff] [blame] | 96 | LOGP(DMSC, LOGL_INFO, "Tx MSC COMPL L3\n"); |
| 97 | |
Holger Hans Peter Freyther | 508e662 | 2011-06-07 11:40:20 +0200 | [diff] [blame] | 98 | /* find the MSC link we want to use */ |
Holger Hans Peter Freyther | e592cb1 | 2011-06-07 19:57:02 +0200 | [diff] [blame] | 99 | msc = bsc_find_msc(conn, msg); |
Holger Hans Peter Freyther | 508e662 | 2011-06-07 11:40:20 +0200 | [diff] [blame] | 100 | if (!msc) { |
| 101 | LOGP(DMSC, LOGL_ERROR, "Failed to find a MSC for a connection.\n"); |
| 102 | return -1; |
| 103 | } |
| 104 | |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 105 | /* allocate resource for a new connection */ |
Holger Hans Peter Freyther | 508e662 | 2011-06-07 11:40:20 +0200 | [diff] [blame] | 106 | if (bsc_create_new_connection(conn, msc) != 0) |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 107 | return BSC_API_CONN_POL_REJECT; |
| 108 | |
Holger Hans Peter Freyther | 5e84584 | 2011-06-04 14:51:51 +0200 | [diff] [blame] | 109 | network_code = get_network_code_for_msc(conn->sccp_con->msc); |
| 110 | country_code = get_country_code_for_msc(conn->sccp_con->msc); |
| 111 | |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 112 | bsc_scan_bts_msg(conn, msg); |
| 113 | resp = gsm0808_create_layer3(msg, network_code, country_code, |
| 114 | conn->bts->location_area_code, |
| 115 | conn->bts->cell_identity); |
| 116 | if (!resp) { |
| 117 | LOGP(DMSC, LOGL_DEBUG, "Failed to create layer3 message.\n"); |
Holger Hans Peter Freyther | 326a124 | 2011-06-09 14:44:47 +0200 | [diff] [blame] | 118 | sccp_connection_free(conn->sccp_con->sccp); |
Holger Hans Peter Freyther | 288b80a | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 119 | bsc_delete_connection(conn->sccp_con); |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 120 | return BSC_API_CONN_POL_REJECT; |
Holger Hans Peter Freyther | b408d79 | 2010-11-05 11:21:18 +0100 | [diff] [blame] | 121 | } |
| 122 | |
Holger Hans Peter Freyther | 288b80a | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 123 | if (bsc_open_connection(conn->sccp_con, resp) != 0) { |
Holger Hans Peter Freyther | 326a124 | 2011-06-09 14:44:47 +0200 | [diff] [blame] | 124 | sccp_connection_free(conn->sccp_con->sccp); |
Holger Hans Peter Freyther | 288b80a | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 125 | bsc_delete_connection(conn->sccp_con); |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 126 | msgb_free(resp); |
| 127 | return BSC_API_CONN_POL_REJECT; |
| 128 | } |
| 129 | |
| 130 | return BSC_API_CONN_POL_ACCEPT; |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 131 | } |
| 132 | |
Holger Hans Peter Freyther | 767ff3c | 2010-11-04 12:18:00 +0100 | [diff] [blame] | 133 | static void bsc_dtap(struct gsm_subscriber_connection *conn, uint8_t link_id, struct msgb *msg) |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 134 | { |
Holger Hans Peter Freyther | 3623d6b | 2010-11-04 12:24:05 +0100 | [diff] [blame] | 135 | struct msgb *resp; |
Holger Hans Peter Freyther | b6d7014 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 136 | return_when_not_connected(conn); |
Holger Hans Peter Freyther | 3623d6b | 2010-11-04 12:24:05 +0100 | [diff] [blame] | 137 | |
Harald Welte | 98833af | 2011-07-12 00:05:11 +0200 | [diff] [blame] | 138 | LOGP(DMSC, LOGL_INFO, "Tx MSC DTAP LINK_ID=0x%02x\n", link_id); |
| 139 | |
Holger Hans Peter Freyther | b408d79 | 2010-11-05 11:21:18 +0100 | [diff] [blame] | 140 | bsc_scan_bts_msg(conn, msg); |
Holger Hans Peter Freyther | 3623d6b | 2010-11-04 12:24:05 +0100 | [diff] [blame] | 141 | resp = gsm0808_create_dtap(msg, link_id); |
Holger Hans Peter Freyther | 5f2184c | 2010-11-05 11:02:28 +0100 | [diff] [blame] | 142 | queue_msg_or_return(resp); |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 143 | } |
| 144 | |
Holger Hans Peter Freyther | 8dd01ef | 2010-11-04 12:06:57 +0100 | [diff] [blame] | 145 | static void bsc_assign_compl(struct gsm_subscriber_connection *conn, uint8_t rr_cause, |
| 146 | uint8_t chosen_channel, uint8_t encr_alg_id, |
| 147 | uint8_t speech_model) |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 148 | { |
Holger Hans Peter Freyther | 03a61d1 | 2010-11-04 12:09:45 +0100 | [diff] [blame] | 149 | struct msgb *resp; |
Holger Hans Peter Freyther | b6d7014 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 150 | return_when_not_connected(conn); |
Holger Hans Peter Freyther | 03a61d1 | 2010-11-04 12:09:45 +0100 | [diff] [blame] | 151 | |
Harald Welte | 98833af | 2011-07-12 00:05:11 +0200 | [diff] [blame] | 152 | LOGP(DMSC, LOGL_INFO, "Tx MSC ASSIGN COMPL\n"); |
| 153 | |
Holger Hans Peter Freyther | 03a61d1 | 2010-11-04 12:09:45 +0100 | [diff] [blame] | 154 | resp = gsm0808_create_assignment_completed(rr_cause, chosen_channel, |
| 155 | encr_alg_id, speech_model); |
Holger Hans Peter Freyther | 5f2184c | 2010-11-05 11:02:28 +0100 | [diff] [blame] | 156 | queue_msg_or_return(resp); |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 157 | } |
| 158 | |
Holger Hans Peter Freyther | 9ff6568 | 2010-11-05 10:37:17 +0100 | [diff] [blame] | 159 | static void bsc_assign_fail(struct gsm_subscriber_connection *conn, |
| 160 | uint8_t cause, uint8_t *rr_cause) |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 161 | { |
Holger Hans Peter Freyther | cfa3747 | 2010-11-05 10:59:45 +0100 | [diff] [blame] | 162 | struct msgb *resp; |
Holger Hans Peter Freyther | b6d7014 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 163 | return_when_not_connected(conn); |
Holger Hans Peter Freyther | cfa3747 | 2010-11-05 10:59:45 +0100 | [diff] [blame] | 164 | |
Harald Welte | 98833af | 2011-07-12 00:05:11 +0200 | [diff] [blame] | 165 | LOGP(DMSC, LOGL_INFO, "Tx MSC ASSIGN FAIL\n"); |
| 166 | |
Holger Hans Peter Freyther | cfa3747 | 2010-11-05 10:59:45 +0100 | [diff] [blame] | 167 | resp = gsm0808_create_assignment_failure(cause, rr_cause); |
Holger Hans Peter Freyther | 5f2184c | 2010-11-05 11:02:28 +0100 | [diff] [blame] | 168 | queue_msg_or_return(resp); |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 169 | } |
| 170 | |
Holger Hans Peter Freyther | 0aa55a3 | 2010-11-03 19:01:58 +0100 | [diff] [blame] | 171 | static int bsc_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause) |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 172 | { |
Holger Hans Peter Freyther | 97f5a1a | 2011-08-06 14:52:56 +0200 | [diff] [blame] | 173 | struct osmo_bsc_sccp_con *sccp; |
Holger Hans Peter Freyther | ef2d1bf | 2010-11-04 12:47:06 +0100 | [diff] [blame] | 174 | struct msgb *resp; |
Holger Hans Peter Freyther | b6d7014 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 175 | return_when_not_connected_val(conn, 1); |
Holger Hans Peter Freyther | ef2d1bf | 2010-11-04 12:47:06 +0100 | [diff] [blame] | 176 | |
Harald Welte | 98833af | 2011-07-12 00:05:11 +0200 | [diff] [blame] | 177 | LOGP(DMSC, LOGL_INFO, "Tx MSC CLEAR REQUEST\n"); |
| 178 | |
Holger Hans Peter Freyther | 97f5a1a | 2011-08-06 14:52:56 +0200 | [diff] [blame] | 179 | /* |
| 180 | * Remove the connection from BSC<->SCCP part, the SCCP part |
| 181 | * will either be cleared by channel release or MSC disconnect |
| 182 | */ |
| 183 | sccp = conn->sccp_con; |
| 184 | sccp->conn = NULL; |
| 185 | conn->sccp_con = NULL; |
| 186 | |
Holger Hans Peter Freyther | ef2d1bf | 2010-11-04 12:47:06 +0100 | [diff] [blame] | 187 | resp = gsm0808_create_clear_rqst(GSM0808_CAUSE_RADIO_INTERFACE_FAILURE); |
| 188 | if (!resp) { |
| 189 | LOGP(DMSC, LOGL_ERROR, "Failed to allocate response.\n"); |
Holger Hans Peter Freyther | 97f5a1a | 2011-08-06 14:52:56 +0200 | [diff] [blame] | 190 | return 1; |
Holger Hans Peter Freyther | ef2d1bf | 2010-11-04 12:47:06 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Holger Hans Peter Freyther | 97f5a1a | 2011-08-06 14:52:56 +0200 | [diff] [blame] | 193 | bsc_queue_for_msc(sccp, resp); |
| 194 | return 1; |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 195 | } |
| 196 | |
Harald Welte | cd4acaf | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 197 | static void bsc_cm_update(struct gsm_subscriber_connection *conn, |
| 198 | const uint8_t *cm2, uint8_t cm2_len, |
| 199 | const uint8_t *cm3, uint8_t cm3_len) |
| 200 | { |
Harald Welte | cd4acaf | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 201 | struct msgb *resp; |
Holger Hans Peter Freyther | 93b0a14 | 2012-03-16 11:49:46 +0100 | [diff] [blame] | 202 | return_when_not_connected(conn); |
Harald Welte | cd4acaf | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 203 | |
| 204 | resp = gsm0808_create_classmark_update(cm2, cm2_len, cm3, cm3_len); |
| 205 | |
| 206 | queue_msg_or_return(resp); |
| 207 | } |
| 208 | |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 209 | static struct bsc_api bsc_handler = { |
| 210 | .sapi_n_reject = bsc_sapi_n_reject, |
| 211 | .cipher_mode_compl = bsc_cipher_mode_compl, |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 212 | .compl_l3 = bsc_compl_l3, |
| 213 | .dtap = bsc_dtap, |
| 214 | .assign_compl = bsc_assign_compl, |
| 215 | .assign_fail = bsc_assign_fail, |
| 216 | .clear_request = bsc_clear_request, |
Harald Welte | cd4acaf | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 217 | .classmark_chg = bsc_cm_update, |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 218 | }; |
| 219 | |
Holger Hans Peter Freyther | 22e9ac3 | 2010-07-05 16:02:04 +0800 | [diff] [blame] | 220 | struct bsc_api *osmo_bsc_api() |
| 221 | { |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 222 | return &bsc_handler; |
Holger Hans Peter Freyther | 22e9ac3 | 2010-07-05 16:02:04 +0800 | [diff] [blame] | 223 | } |