Holger Hans Peter Freyther | 8ec4952 | 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 | 4f448c9 | 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 | 9af6ddf | 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 | 4f448c9 | 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 | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 13 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 4f448c9 | 2010-07-05 16:02:04 +0800 | [diff] [blame] | 14 | * |
Harald Welte | 9af6ddf | 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 | 4f448c9 | 2010-07-05 16:02:04 +0800 | [diff] [blame] | 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <openbsc/osmo_bsc.h> |
Holger Hans Peter Freyther | 9b17c76 | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 21 | #include <openbsc/osmo_msc_data.h> |
Holger Hans Peter Freyther | 7225fd1 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 22 | #include <openbsc/debug.h> |
| 23 | |
Harald Welte | d36ff76 | 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 | 2d2c910 | 2010-11-04 11:59:41 +0100 | [diff] [blame] | 26 | |
Holger Hans Peter Freyther | 9b9a171 | 2011-06-09 14:44:47 +0200 | [diff] [blame] | 27 | #include <osmocom/sccp/sccp.h> |
| 28 | |
Holger Hans Peter Freyther | 7225fd1 | 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 | 4f448c9 | 2010-07-05 16:02:04 +0800 | [diff] [blame] | 40 | |
Holger Hans Peter Freyther | 46c5ab3 | 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 | 863a55d | 2010-11-10 09:31:41 +0100 | [diff] [blame] | 46 | bsc_queue_for_msc(conn->sccp_con, resp); |
Holger Hans Peter Freyther | 46c5ab3 | 2010-11-05 11:02:28 +0100 | [diff] [blame] | 47 | |
Holger Hans Peter Freyther | 9b17c76 | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 48 | static uint16_t get_network_code_for_msc(struct gsm_network *net) |
| 49 | { |
Holger Hans Peter Freyther | 8ec4952 | 2011-08-15 15:53:00 +0200 | [diff] [blame] | 50 | if (net->bsc_data->msc.core_ncc != -1) |
| 51 | return net->bsc_data->msc.core_ncc; |
Holger Hans Peter Freyther | 9b17c76 | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 52 | return net->network_code; |
| 53 | } |
| 54 | |
| 55 | static uint16_t get_country_code_for_msc(struct gsm_network *net) |
| 56 | { |
Holger Hans Peter Freyther | 8ec4952 | 2011-08-15 15:53:00 +0200 | [diff] [blame] | 57 | if (net->bsc_data->msc.core_mcc != -1) |
| 58 | return net->bsc_data->msc.core_mcc; |
Holger Hans Peter Freyther | 9b17c76 | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 59 | return net->country_code; |
| 60 | } |
| 61 | |
Holger Hans Peter Freyther | 32aaef6 | 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 | c8166f3 | 2010-11-04 12:28:32 +0100 | [diff] [blame] | 64 | struct msgb *resp; |
Holger Hans Peter Freyther | 7225fd1 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 65 | return_when_not_connected(conn); |
Holger Hans Peter Freyther | c8166f3 | 2010-11-04 12:28:32 +0100 | [diff] [blame] | 66 | |
Harald Welte | 7d33bdf | 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 | c8166f3 | 2010-11-04 12:28:32 +0100 | [diff] [blame] | 69 | resp = gsm0808_create_sapi_reject(dlci); |
Holger Hans Peter Freyther | 46c5ab3 | 2010-11-05 11:02:28 +0100 | [diff] [blame] | 70 | queue_msg_or_return(resp); |
Holger Hans Peter Freyther | 32aaef6 | 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 | 2d2c910 | 2010-11-04 11:59:41 +0100 | [diff] [blame] | 76 | struct msgb *resp; |
Holger Hans Peter Freyther | 7225fd1 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 77 | return_when_not_connected(conn); |
Holger Hans Peter Freyther | 2d2c910 | 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 | 46c5ab3 | 2010-11-05 11:02:28 +0100 | [diff] [blame] | 81 | queue_msg_or_return(resp); |
Holger Hans Peter Freyther | 32aaef6 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 82 | } |
| 83 | |
Holger Hans Peter Freyther | 9b17c76 | 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 | 32aaef6 | 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 | 9b17c76 | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 91 | struct msgb *resp; |
| 92 | uint16_t network_code = get_network_code_for_msc(conn->bts->network); |
| 93 | uint16_t country_code = get_country_code_for_msc(conn->bts->network); |
| 94 | |
Harald Welte | 7d33bdf | 2011-07-12 00:05:11 +0200 | [diff] [blame] | 95 | LOGP(DMSC, LOGL_INFO, "Tx MSC COMPL L3\n"); |
| 96 | |
Holger Hans Peter Freyther | 9b17c76 | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 97 | /* allocate resource for a new connection */ |
| 98 | if (bsc_create_new_connection(conn) != 0) |
| 99 | return BSC_API_CONN_POL_REJECT; |
| 100 | |
| 101 | bsc_scan_bts_msg(conn, msg); |
| 102 | resp = gsm0808_create_layer3(msg, network_code, country_code, |
| 103 | conn->bts->location_area_code, |
| 104 | conn->bts->cell_identity); |
| 105 | if (!resp) { |
| 106 | LOGP(DMSC, LOGL_DEBUG, "Failed to create layer3 message.\n"); |
Holger Hans Peter Freyther | 9b9a171 | 2011-06-09 14:44:47 +0200 | [diff] [blame] | 107 | sccp_connection_free(conn->sccp_con->sccp); |
Holger Hans Peter Freyther | 7e90108 | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 108 | bsc_delete_connection(conn->sccp_con); |
Holger Hans Peter Freyther | 9b17c76 | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 109 | return BSC_API_CONN_POL_REJECT; |
Holger Hans Peter Freyther | 5bb874d | 2010-11-05 11:21:18 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Holger Hans Peter Freyther | 7e90108 | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 112 | if (bsc_open_connection(conn->sccp_con, resp) != 0) { |
Holger Hans Peter Freyther | 9b9a171 | 2011-06-09 14:44:47 +0200 | [diff] [blame] | 113 | sccp_connection_free(conn->sccp_con->sccp); |
Holger Hans Peter Freyther | 7e90108 | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 114 | bsc_delete_connection(conn->sccp_con); |
Holger Hans Peter Freyther | 9b17c76 | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 115 | msgb_free(resp); |
| 116 | return BSC_API_CONN_POL_REJECT; |
| 117 | } |
| 118 | |
| 119 | return BSC_API_CONN_POL_ACCEPT; |
Holger Hans Peter Freyther | 32aaef6 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 120 | } |
| 121 | |
Holger Hans Peter Freyther | 46caa30 | 2010-11-04 12:18:00 +0100 | [diff] [blame] | 122 | static void bsc_dtap(struct gsm_subscriber_connection *conn, uint8_t link_id, struct msgb *msg) |
Holger Hans Peter Freyther | 32aaef6 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 123 | { |
Holger Hans Peter Freyther | 58f2cf6 | 2010-11-04 12:24:05 +0100 | [diff] [blame] | 124 | struct msgb *resp; |
Holger Hans Peter Freyther | 7225fd1 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 125 | return_when_not_connected(conn); |
Holger Hans Peter Freyther | 58f2cf6 | 2010-11-04 12:24:05 +0100 | [diff] [blame] | 126 | |
Harald Welte | 7d33bdf | 2011-07-12 00:05:11 +0200 | [diff] [blame] | 127 | LOGP(DMSC, LOGL_INFO, "Tx MSC DTAP LINK_ID=0x%02x\n", link_id); |
| 128 | |
Holger Hans Peter Freyther | 5bb874d | 2010-11-05 11:21:18 +0100 | [diff] [blame] | 129 | bsc_scan_bts_msg(conn, msg); |
Holger Hans Peter Freyther | 58f2cf6 | 2010-11-04 12:24:05 +0100 | [diff] [blame] | 130 | resp = gsm0808_create_dtap(msg, link_id); |
Holger Hans Peter Freyther | 46c5ab3 | 2010-11-05 11:02:28 +0100 | [diff] [blame] | 131 | queue_msg_or_return(resp); |
Holger Hans Peter Freyther | 32aaef6 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 132 | } |
| 133 | |
Holger Hans Peter Freyther | b094171 | 2010-11-04 12:06:57 +0100 | [diff] [blame] | 134 | static void bsc_assign_compl(struct gsm_subscriber_connection *conn, uint8_t rr_cause, |
| 135 | uint8_t chosen_channel, uint8_t encr_alg_id, |
| 136 | uint8_t speech_model) |
Holger Hans Peter Freyther | 32aaef6 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 137 | { |
Holger Hans Peter Freyther | da488ae | 2010-11-04 12:09:45 +0100 | [diff] [blame] | 138 | struct msgb *resp; |
Holger Hans Peter Freyther | 7225fd1 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 139 | return_when_not_connected(conn); |
Holger Hans Peter Freyther | da488ae | 2010-11-04 12:09:45 +0100 | [diff] [blame] | 140 | |
Harald Welte | 7d33bdf | 2011-07-12 00:05:11 +0200 | [diff] [blame] | 141 | LOGP(DMSC, LOGL_INFO, "Tx MSC ASSIGN COMPL\n"); |
| 142 | |
Holger Hans Peter Freyther | da488ae | 2010-11-04 12:09:45 +0100 | [diff] [blame] | 143 | resp = gsm0808_create_assignment_completed(rr_cause, chosen_channel, |
| 144 | encr_alg_id, speech_model); |
Holger Hans Peter Freyther | 46c5ab3 | 2010-11-05 11:02:28 +0100 | [diff] [blame] | 145 | queue_msg_or_return(resp); |
Holger Hans Peter Freyther | 32aaef6 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 146 | } |
| 147 | |
Holger Hans Peter Freyther | 45b575f | 2010-11-05 10:37:17 +0100 | [diff] [blame] | 148 | static void bsc_assign_fail(struct gsm_subscriber_connection *conn, |
| 149 | uint8_t cause, uint8_t *rr_cause) |
Holger Hans Peter Freyther | 32aaef6 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 150 | { |
Holger Hans Peter Freyther | e46f1d6 | 2010-11-05 10:59:45 +0100 | [diff] [blame] | 151 | struct msgb *resp; |
Holger Hans Peter Freyther | 7225fd1 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 152 | return_when_not_connected(conn); |
Holger Hans Peter Freyther | e46f1d6 | 2010-11-05 10:59:45 +0100 | [diff] [blame] | 153 | |
Harald Welte | 7d33bdf | 2011-07-12 00:05:11 +0200 | [diff] [blame] | 154 | LOGP(DMSC, LOGL_INFO, "Tx MSC ASSIGN FAIL\n"); |
| 155 | |
Holger Hans Peter Freyther | e46f1d6 | 2010-11-05 10:59:45 +0100 | [diff] [blame] | 156 | resp = gsm0808_create_assignment_failure(cause, rr_cause); |
Holger Hans Peter Freyther | 46c5ab3 | 2010-11-05 11:02:28 +0100 | [diff] [blame] | 157 | queue_msg_or_return(resp); |
Holger Hans Peter Freyther | 32aaef6 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 158 | } |
| 159 | |
Holger Hans Peter Freyther | 05c6884 | 2010-11-03 19:01:58 +0100 | [diff] [blame] | 160 | static int bsc_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause) |
Holger Hans Peter Freyther | 32aaef6 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 161 | { |
Holger Hans Peter Freyther | 80c37bd | 2011-08-06 14:52:56 +0200 | [diff] [blame] | 162 | struct osmo_bsc_sccp_con *sccp; |
Holger Hans Peter Freyther | c2b3270 | 2010-11-04 12:47:06 +0100 | [diff] [blame] | 163 | struct msgb *resp; |
Holger Hans Peter Freyther | 7225fd1 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 164 | return_when_not_connected_val(conn, 1); |
Holger Hans Peter Freyther | c2b3270 | 2010-11-04 12:47:06 +0100 | [diff] [blame] | 165 | |
Harald Welte | 7d33bdf | 2011-07-12 00:05:11 +0200 | [diff] [blame] | 166 | LOGP(DMSC, LOGL_INFO, "Tx MSC CLEAR REQUEST\n"); |
| 167 | |
Holger Hans Peter Freyther | 80c37bd | 2011-08-06 14:52:56 +0200 | [diff] [blame] | 168 | /* |
| 169 | * Remove the connection from BSC<->SCCP part, the SCCP part |
| 170 | * will either be cleared by channel release or MSC disconnect |
| 171 | */ |
| 172 | sccp = conn->sccp_con; |
| 173 | sccp->conn = NULL; |
| 174 | conn->sccp_con = NULL; |
| 175 | |
Holger Hans Peter Freyther | c2b3270 | 2010-11-04 12:47:06 +0100 | [diff] [blame] | 176 | resp = gsm0808_create_clear_rqst(GSM0808_CAUSE_RADIO_INTERFACE_FAILURE); |
| 177 | if (!resp) { |
| 178 | LOGP(DMSC, LOGL_ERROR, "Failed to allocate response.\n"); |
Holger Hans Peter Freyther | 80c37bd | 2011-08-06 14:52:56 +0200 | [diff] [blame] | 179 | return 1; |
Holger Hans Peter Freyther | c2b3270 | 2010-11-04 12:47:06 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Holger Hans Peter Freyther | 80c37bd | 2011-08-06 14:52:56 +0200 | [diff] [blame] | 182 | bsc_queue_for_msc(sccp, resp); |
| 183 | return 1; |
Holger Hans Peter Freyther | 32aaef6 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 184 | } |
| 185 | |
Harald Welte | 95e862c | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 186 | static void bsc_cm_update(struct gsm_subscriber_connection *conn, |
| 187 | const uint8_t *cm2, uint8_t cm2_len, |
| 188 | const uint8_t *cm3, uint8_t cm3_len) |
| 189 | { |
Harald Welte | 95e862c | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 190 | struct msgb *resp; |
Holger Hans Peter Freyther | dc3389f | 2012-03-16 11:49:46 +0100 | [diff] [blame] | 191 | return_when_not_connected(conn); |
Harald Welte | 95e862c | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 192 | |
| 193 | resp = gsm0808_create_classmark_update(cm2, cm2_len, cm3, cm3_len); |
| 194 | |
| 195 | queue_msg_or_return(resp); |
| 196 | } |
| 197 | |
Holger Hans Peter Freyther | 32aaef6 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 198 | static struct bsc_api bsc_handler = { |
| 199 | .sapi_n_reject = bsc_sapi_n_reject, |
| 200 | .cipher_mode_compl = bsc_cipher_mode_compl, |
Holger Hans Peter Freyther | 32aaef6 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 201 | .compl_l3 = bsc_compl_l3, |
| 202 | .dtap = bsc_dtap, |
| 203 | .assign_compl = bsc_assign_compl, |
| 204 | .assign_fail = bsc_assign_fail, |
| 205 | .clear_request = bsc_clear_request, |
Harald Welte | 95e862c | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 206 | .classmark_chg = bsc_cm_update, |
Holger Hans Peter Freyther | 32aaef6 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 207 | }; |
| 208 | |
Holger Hans Peter Freyther | 4f448c9 | 2010-07-05 16:02:04 +0800 | [diff] [blame] | 209 | struct bsc_api *osmo_bsc_api() |
| 210 | { |
Holger Hans Peter Freyther | 32aaef6 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 211 | return &bsc_handler; |
Holger Hans Peter Freyther | 4f448c9 | 2010-07-05 16:02:04 +0800 | [diff] [blame] | 212 | } |