Holger Hans Peter Freyther | 22e9ac3 | 2010-07-05 16:02:04 +0800 | [diff] [blame] | 1 | /* (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 2 | * (C) 2009-2010 by On-Waves |
| 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 | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 48 | static uint16_t get_network_code_for_msc(struct gsm_network *net) |
| 49 | { |
| 50 | if (net->msc_data->core_ncc != -1) |
| 51 | return net->msc_data->core_ncc; |
| 52 | return net->network_code; |
| 53 | } |
| 54 | |
| 55 | static uint16_t get_country_code_for_msc(struct gsm_network *net) |
| 56 | { |
| 57 | if (net->msc_data->core_mcc != -1) |
| 58 | return net->msc_data->core_mcc; |
| 59 | return net->country_code; |
| 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; |
| 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 | 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 | |
| 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 | 326a124 | 2011-06-09 14:44:47 +0200 | [diff] [blame] | 107 | sccp_connection_free(conn->sccp_con->sccp); |
Holger Hans Peter Freyther | 288b80a | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 108 | bsc_delete_connection(conn->sccp_con); |
Holger Hans Peter Freyther | 9c61b2a | 2010-11-05 19:48:47 +0100 | [diff] [blame] | 109 | return BSC_API_CONN_POL_REJECT; |
Holger Hans Peter Freyther | b408d79 | 2010-11-05 11:21:18 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Holger Hans Peter Freyther | 288b80a | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 112 | if (bsc_open_connection(conn->sccp_con, resp) != 0) { |
Holger Hans Peter Freyther | 326a124 | 2011-06-09 14:44:47 +0200 | [diff] [blame] | 113 | sccp_connection_free(conn->sccp_con->sccp); |
Holger Hans Peter Freyther | 288b80a | 2010-11-06 20:15:17 +0100 | [diff] [blame] | 114 | bsc_delete_connection(conn->sccp_con); |
Holger Hans Peter Freyther | 9c61b2a | 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 | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 120 | } |
| 121 | |
Holger Hans Peter Freyther | 767ff3c | 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 | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 123 | { |
Holger Hans Peter Freyther | 3623d6b | 2010-11-04 12:24:05 +0100 | [diff] [blame] | 124 | struct msgb *resp; |
Holger Hans Peter Freyther | b6d7014 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 125 | return_when_not_connected(conn); |
Holger Hans Peter Freyther | 3623d6b | 2010-11-04 12:24:05 +0100 | [diff] [blame] | 126 | |
Harald Welte | 98833af | 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 | b408d79 | 2010-11-05 11:21:18 +0100 | [diff] [blame] | 129 | bsc_scan_bts_msg(conn, msg); |
Holger Hans Peter Freyther | 3623d6b | 2010-11-04 12:24:05 +0100 | [diff] [blame] | 130 | resp = gsm0808_create_dtap(msg, link_id); |
Holger Hans Peter Freyther | 5f2184c | 2010-11-05 11:02:28 +0100 | [diff] [blame] | 131 | queue_msg_or_return(resp); |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 132 | } |
| 133 | |
Holger Hans Peter Freyther | 8dd01ef | 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 | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 137 | { |
Holger Hans Peter Freyther | 03a61d1 | 2010-11-04 12:09:45 +0100 | [diff] [blame] | 138 | struct msgb *resp; |
Holger Hans Peter Freyther | b6d7014 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 139 | return_when_not_connected(conn); |
Holger Hans Peter Freyther | 03a61d1 | 2010-11-04 12:09:45 +0100 | [diff] [blame] | 140 | |
Harald Welte | 98833af | 2011-07-12 00:05:11 +0200 | [diff] [blame] | 141 | LOGP(DMSC, LOGL_INFO, "Tx MSC ASSIGN COMPL\n"); |
| 142 | |
Holger Hans Peter Freyther | 03a61d1 | 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 | 5f2184c | 2010-11-05 11:02:28 +0100 | [diff] [blame] | 145 | queue_msg_or_return(resp); |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 146 | } |
| 147 | |
Holger Hans Peter Freyther | 9ff6568 | 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 | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 150 | { |
Holger Hans Peter Freyther | cfa3747 | 2010-11-05 10:59:45 +0100 | [diff] [blame] | 151 | struct msgb *resp; |
Holger Hans Peter Freyther | b6d7014 | 2010-11-03 19:03:35 +0100 | [diff] [blame] | 152 | return_when_not_connected(conn); |
Holger Hans Peter Freyther | cfa3747 | 2010-11-05 10:59:45 +0100 | [diff] [blame] | 153 | |
Harald Welte | 98833af | 2011-07-12 00:05:11 +0200 | [diff] [blame] | 154 | LOGP(DMSC, LOGL_INFO, "Tx MSC ASSIGN FAIL\n"); |
| 155 | |
Holger Hans Peter Freyther | cfa3747 | 2010-11-05 10:59:45 +0100 | [diff] [blame] | 156 | resp = gsm0808_create_assignment_failure(cause, rr_cause); |
Holger Hans Peter Freyther | 5f2184c | 2010-11-05 11:02:28 +0100 | [diff] [blame] | 157 | queue_msg_or_return(resp); |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 158 | } |
| 159 | |
Holger Hans Peter Freyther | 0aa55a3 | 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 | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 161 | { |
Holger Hans Peter Freyther | ef2d1bf | 2010-11-04 12:47:06 +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_val(conn, 1); |
Holger Hans Peter Freyther | ef2d1bf | 2010-11-04 12:47:06 +0100 | [diff] [blame] | 164 | |
Harald Welte | 98833af | 2011-07-12 00:05:11 +0200 | [diff] [blame] | 165 | LOGP(DMSC, LOGL_INFO, "Tx MSC CLEAR REQUEST\n"); |
| 166 | |
Holger Hans Peter Freyther | ef2d1bf | 2010-11-04 12:47:06 +0100 | [diff] [blame] | 167 | resp = gsm0808_create_clear_rqst(GSM0808_CAUSE_RADIO_INTERFACE_FAILURE); |
| 168 | if (!resp) { |
| 169 | LOGP(DMSC, LOGL_ERROR, "Failed to allocate response.\n"); |
| 170 | return 0; |
| 171 | } |
| 172 | |
Holger Hans Peter Freyther | 1f334cc | 2010-11-10 09:31:41 +0100 | [diff] [blame] | 173 | bsc_queue_for_msc(conn->sccp_con, resp); |
Holger Hans Peter Freyther | 0aa55a3 | 2010-11-03 19:01:58 +0100 | [diff] [blame] | 174 | return 0; |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 175 | } |
| 176 | |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 177 | static struct bsc_api bsc_handler = { |
| 178 | .sapi_n_reject = bsc_sapi_n_reject, |
| 179 | .cipher_mode_compl = bsc_cipher_mode_compl, |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 180 | .compl_l3 = bsc_compl_l3, |
| 181 | .dtap = bsc_dtap, |
| 182 | .assign_compl = bsc_assign_compl, |
| 183 | .assign_fail = bsc_assign_fail, |
| 184 | .clear_request = bsc_clear_request, |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 185 | }; |
| 186 | |
Holger Hans Peter Freyther | 22e9ac3 | 2010-07-05 16:02:04 +0800 | [diff] [blame] | 187 | struct bsc_api *osmo_bsc_api() |
| 188 | { |
Holger Hans Peter Freyther | 7d23130 | 2010-09-16 17:26:35 +0800 | [diff] [blame] | 189 | return &bsc_handler; |
Holger Hans Peter Freyther | 22e9ac3 | 2010-07-05 16:02:04 +0800 | [diff] [blame] | 190 | } |