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 | 5e84584 | 2011-06-04 14:51:51 +0200 | [diff] [blame] | 92 | uint16_t network_code; |
| 93 | uint16_t country_code; |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 94 | |
Harald Welte | 98833af | 2011-07-12 00:05:11 +0200 | [diff] [blame] | 95 | LOGP(DMSC, LOGL_INFO, "Tx MSC COMPL L3\n"); |
| 96 | |
Holger Hans Peter Freyther | 9c61b2a | 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 | |
Holger Hans Peter Freyther | 5e84584 | 2011-06-04 14:51:51 +0200 | [diff] [blame] | 101 | network_code = get_network_code_for_msc(conn->sccp_con->msc); |
| 102 | country_code = get_country_code_for_msc(conn->sccp_con->msc); |
| 103 | |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 104 | bsc_scan_bts_msg(conn, msg); |
| 105 | resp = gsm0808_create_layer3(msg, network_code, country_code, |
| 106 | conn->bts->location_area_code, |
| 107 | conn->bts->cell_identity); |
| 108 | if (!resp) { |
| 109 | LOGP(DMSC, LOGL_DEBUG, "Failed to create layer3 message.\n"); |
Holger Hans Peter Freyther | 326a124 | 2011-06-09 14:44:47 +0200 | [diff] [blame] | 110 | sccp_connection_free(conn->sccp_con->sccp); |
Holger Hans Peter Freyther | 288b80a | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 111 | bsc_delete_connection(conn->sccp_con); |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 112 | return BSC_API_CONN_POL_REJECT; |
Holger Hans Peter Freyther | b408d79 | 2010-11-05 11:21:18 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Holger Hans Peter Freyther | 288b80a | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 115 | if (bsc_open_connection(conn->sccp_con, resp) != 0) { |
Holger Hans Peter Freyther | 326a124 | 2011-06-09 14:44:47 +0200 | [diff] [blame] | 116 | sccp_connection_free(conn->sccp_con->sccp); |
Holger Hans Peter Freyther | 288b80a | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 117 | bsc_delete_connection(conn->sccp_con); |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 118 | msgb_free(resp); |
| 119 | return BSC_API_CONN_POL_REJECT; |
| 120 | } |
| 121 | |
| 122 | return BSC_API_CONN_POL_ACCEPT; |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 123 | } |
| 124 | |
Holger Hans Peter Freyther | 767ff3c | 2010-11-04 12:18:00 +0100 | [diff] [blame] | 125 | 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] | 126 | { |
Holger Hans Peter Freyther | 3623d6b | 2010-11-04 12:24:05 +0100 | [diff] [blame] | 127 | struct msgb *resp; |
Holger Hans Peter Freyther | b6d7014 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 128 | return_when_not_connected(conn); |
Holger Hans Peter Freyther | 3623d6b | 2010-11-04 12:24:05 +0100 | [diff] [blame] | 129 | |
Harald Welte | 98833af | 2011-07-12 00:05:11 +0200 | [diff] [blame] | 130 | LOGP(DMSC, LOGL_INFO, "Tx MSC DTAP LINK_ID=0x%02x\n", link_id); |
| 131 | |
Holger Hans Peter Freyther | b408d79 | 2010-11-05 11:21:18 +0100 | [diff] [blame] | 132 | bsc_scan_bts_msg(conn, msg); |
Holger Hans Peter Freyther | 3623d6b | 2010-11-04 12:24:05 +0100 | [diff] [blame] | 133 | resp = gsm0808_create_dtap(msg, link_id); |
Holger Hans Peter Freyther | 5f2184c | 2010-11-05 11:02:28 +0100 | [diff] [blame] | 134 | queue_msg_or_return(resp); |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 135 | } |
| 136 | |
Holger Hans Peter Freyther | 8dd01ef | 2010-11-04 12:06:57 +0100 | [diff] [blame] | 137 | static void bsc_assign_compl(struct gsm_subscriber_connection *conn, uint8_t rr_cause, |
| 138 | uint8_t chosen_channel, uint8_t encr_alg_id, |
| 139 | uint8_t speech_model) |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 140 | { |
Holger Hans Peter Freyther | 03a61d1 | 2010-11-04 12:09:45 +0100 | [diff] [blame] | 141 | struct msgb *resp; |
Holger Hans Peter Freyther | b6d7014 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 142 | return_when_not_connected(conn); |
Holger Hans Peter Freyther | 03a61d1 | 2010-11-04 12:09:45 +0100 | [diff] [blame] | 143 | |
Harald Welte | 98833af | 2011-07-12 00:05:11 +0200 | [diff] [blame] | 144 | LOGP(DMSC, LOGL_INFO, "Tx MSC ASSIGN COMPL\n"); |
| 145 | |
Holger Hans Peter Freyther | 03a61d1 | 2010-11-04 12:09:45 +0100 | [diff] [blame] | 146 | resp = gsm0808_create_assignment_completed(rr_cause, chosen_channel, |
| 147 | encr_alg_id, speech_model); |
Holger Hans Peter Freyther | 5f2184c | 2010-11-05 11:02:28 +0100 | [diff] [blame] | 148 | queue_msg_or_return(resp); |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 149 | } |
| 150 | |
Holger Hans Peter Freyther | 9ff6568 | 2010-11-05 10:37:17 +0100 | [diff] [blame] | 151 | static void bsc_assign_fail(struct gsm_subscriber_connection *conn, |
| 152 | uint8_t cause, uint8_t *rr_cause) |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 153 | { |
Holger Hans Peter Freyther | cfa3747 | 2010-11-05 10:59:45 +0100 | [diff] [blame] | 154 | struct msgb *resp; |
Holger Hans Peter Freyther | b6d7014 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 155 | return_when_not_connected(conn); |
Holger Hans Peter Freyther | cfa3747 | 2010-11-05 10:59:45 +0100 | [diff] [blame] | 156 | |
Harald Welte | 98833af | 2011-07-12 00:05:11 +0200 | [diff] [blame] | 157 | LOGP(DMSC, LOGL_INFO, "Tx MSC ASSIGN FAIL\n"); |
| 158 | |
Holger Hans Peter Freyther | cfa3747 | 2010-11-05 10:59:45 +0100 | [diff] [blame] | 159 | resp = gsm0808_create_assignment_failure(cause, rr_cause); |
Holger Hans Peter Freyther | 5f2184c | 2010-11-05 11:02:28 +0100 | [diff] [blame] | 160 | queue_msg_or_return(resp); |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 161 | } |
| 162 | |
Holger Hans Peter Freyther | 0aa55a3 | 2010-11-03 19:01:58 +0100 | [diff] [blame] | 163 | 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] | 164 | { |
Holger Hans Peter Freyther | 97f5a1a | 2011-08-06 14:52:56 +0200 | [diff] [blame] | 165 | struct osmo_bsc_sccp_con *sccp; |
Holger Hans Peter Freyther | ef2d1bf | 2010-11-04 12:47:06 +0100 | [diff] [blame] | 166 | struct msgb *resp; |
Holger Hans Peter Freyther | b6d7014 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 167 | return_when_not_connected_val(conn, 1); |
Holger Hans Peter Freyther | ef2d1bf | 2010-11-04 12:47:06 +0100 | [diff] [blame] | 168 | |
Harald Welte | 98833af | 2011-07-12 00:05:11 +0200 | [diff] [blame] | 169 | LOGP(DMSC, LOGL_INFO, "Tx MSC CLEAR REQUEST\n"); |
| 170 | |
Holger Hans Peter Freyther | 97f5a1a | 2011-08-06 14:52:56 +0200 | [diff] [blame] | 171 | /* |
| 172 | * Remove the connection from BSC<->SCCP part, the SCCP part |
| 173 | * will either be cleared by channel release or MSC disconnect |
| 174 | */ |
| 175 | sccp = conn->sccp_con; |
| 176 | sccp->conn = NULL; |
| 177 | conn->sccp_con = NULL; |
| 178 | |
Holger Hans Peter Freyther | ef2d1bf | 2010-11-04 12:47:06 +0100 | [diff] [blame] | 179 | resp = gsm0808_create_clear_rqst(GSM0808_CAUSE_RADIO_INTERFACE_FAILURE); |
| 180 | if (!resp) { |
| 181 | LOGP(DMSC, LOGL_ERROR, "Failed to allocate response.\n"); |
Holger Hans Peter Freyther | 97f5a1a | 2011-08-06 14:52:56 +0200 | [diff] [blame] | 182 | return 1; |
Holger Hans Peter Freyther | ef2d1bf | 2010-11-04 12:47:06 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Holger Hans Peter Freyther | 97f5a1a | 2011-08-06 14:52:56 +0200 | [diff] [blame] | 185 | bsc_queue_for_msc(sccp, resp); |
| 186 | return 1; |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 187 | } |
| 188 | |
Harald Welte | cd4acaf | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 189 | static void bsc_cm_update(struct gsm_subscriber_connection *conn, |
| 190 | const uint8_t *cm2, uint8_t cm2_len, |
| 191 | const uint8_t *cm3, uint8_t cm3_len) |
| 192 | { |
Harald Welte | cd4acaf | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 193 | struct msgb *resp; |
Holger Hans Peter Freyther | 93b0a14 | 2012-03-16 11:49:46 +0100 | [diff] [blame] | 194 | return_when_not_connected(conn); |
Harald Welte | cd4acaf | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 195 | |
| 196 | resp = gsm0808_create_classmark_update(cm2, cm2_len, cm3, cm3_len); |
| 197 | |
| 198 | queue_msg_or_return(resp); |
| 199 | } |
| 200 | |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 201 | static struct bsc_api bsc_handler = { |
| 202 | .sapi_n_reject = bsc_sapi_n_reject, |
| 203 | .cipher_mode_compl = bsc_cipher_mode_compl, |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 204 | .compl_l3 = bsc_compl_l3, |
| 205 | .dtap = bsc_dtap, |
| 206 | .assign_compl = bsc_assign_compl, |
| 207 | .assign_fail = bsc_assign_fail, |
| 208 | .clear_request = bsc_clear_request, |
Harald Welte | cd4acaf | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 209 | .classmark_chg = bsc_cm_update, |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 210 | }; |
| 211 | |
Holger Hans Peter Freyther | 22e9ac3 | 2010-07-05 16:02:04 +0800 | [diff] [blame] | 212 | struct bsc_api *osmo_bsc_api() |
| 213 | { |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 214 | return &bsc_handler; |
Holger Hans Peter Freyther | 22e9ac3 | 2010-07-05 16:02:04 +0800 | [diff] [blame] | 215 | } |