blob: c2572599796c140844916461b021293339f709ce [file] [log] [blame]
Holger Hans Peter Freyther8ec49522011-08-15 15:53:00 +02001/* (C) 2009-2011 by Holger Hans Peter Freyther <zecke@selfish.org>
2 * (C) 2009-2011 by On-Waves
Holger Hans Peter Freyther4f448c92010-07-05 16:02:04 +08003 * All Rights Reserved
4 *
5 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01006 * 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 Freyther4f448c92010-07-05 16:02:04 +08008 * (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 Welte9af6ddf2011-01-01 15:25:50 +010013 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther4f448c92010-07-05 16:02:04 +080014 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010015 * 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 Freyther4f448c92010-07-05 16:02:04 +080017 *
18 */
19
20#include <openbsc/osmo_bsc.h>
Holger Hans Peter Freyther9b17c762010-11-05 19:48:47 +010021#include <openbsc/osmo_msc_data.h>
Holger Hans Peter Freyther7225fd12010-11-03 19:03:35 +010022#include <openbsc/debug.h>
23
Harald Welted36ff762011-03-23 18:26:56 +010024#include <osmocom/gsm/protocol/gsm_08_08.h>
25#include <osmocom/gsm/gsm0808.h>
Holger Hans Peter Freyther2d2c9102010-11-04 11:59:41 +010026
Holger Hans Peter Freyther9b9a1712011-06-09 14:44:47 +020027#include <osmocom/sccp/sccp.h>
28
Holger Hans Peter Freyther7225fd12010-11-03 19:03:35 +010029#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 Freyther4f448c92010-07-05 16:02:04 +080040
Holger Hans Peter Freyther46c5ab32010-11-05 11:02:28 +010041#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 Freyther863a55d2010-11-10 09:31:41 +010046 bsc_queue_for_msc(conn->sccp_con, resp);
Holger Hans Peter Freyther46c5ab32010-11-05 11:02:28 +010047
Holger Hans Peter Freyther83d2d382011-06-09 17:37:04 +020048static int bsc_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause);
49static int complete_layer3(struct gsm_subscriber_connection *conn,
50 struct msgb *msg, struct osmo_msc_data *msc);
51
Holger Hans Peter Freyther4cdb0502011-06-04 14:51:51 +020052static uint16_t get_network_code_for_msc(struct osmo_msc_data *msc)
Holger Hans Peter Freyther9b17c762010-11-05 19:48:47 +010053{
Holger Hans Peter Freyther4cdb0502011-06-04 14:51:51 +020054 if (msc->core_ncc != -1)
55 return msc->core_ncc;
56 return msc->network->network_code;
Holger Hans Peter Freyther9b17c762010-11-05 19:48:47 +010057}
58
Holger Hans Peter Freyther4cdb0502011-06-04 14:51:51 +020059static uint16_t get_country_code_for_msc(struct osmo_msc_data *msc)
Holger Hans Peter Freyther9b17c762010-11-05 19:48:47 +010060{
Holger Hans Peter Freyther4cdb0502011-06-04 14:51:51 +020061 if (msc->core_mcc != -1)
62 return msc->core_mcc;
63 return msc->network->country_code;
Holger Hans Peter Freyther9b17c762010-11-05 19:48:47 +010064}
65
Holger Hans Peter Freyther32aaef62010-09-16 17:26:35 +080066static void bsc_sapi_n_reject(struct gsm_subscriber_connection *conn, int dlci)
67{
Holger Hans Peter Freytherc8166f32010-11-04 12:28:32 +010068 struct msgb *resp;
Holger Hans Peter Freyther7225fd12010-11-03 19:03:35 +010069 return_when_not_connected(conn);
Holger Hans Peter Freytherc8166f32010-11-04 12:28:32 +010070
Harald Welte7d33bdf2011-07-12 00:05:11 +020071 LOGP(DMSC, LOGL_NOTICE, "Tx MSC SAPI N REJECT DLCI=0x%02x\n", dlci);
72
Holger Hans Peter Freytherc8166f32010-11-04 12:28:32 +010073 resp = gsm0808_create_sapi_reject(dlci);
Holger Hans Peter Freyther46c5ab32010-11-05 11:02:28 +010074 queue_msg_or_return(resp);
Holger Hans Peter Freyther32aaef62010-09-16 17:26:35 +080075}
76
77static void bsc_cipher_mode_compl(struct gsm_subscriber_connection *conn,
78 struct msgb *msg, uint8_t chosen_encr)
79{
Holger Hans Peter Freyther2d2c9102010-11-04 11:59:41 +010080 struct msgb *resp;
Holger Hans Peter Freyther7225fd12010-11-03 19:03:35 +010081 return_when_not_connected(conn);
Holger Hans Peter Freyther2d2c9102010-11-04 11:59:41 +010082
83 LOGP(DMSC, LOGL_DEBUG, "CIPHER MODE COMPLETE from MS, forwarding to MSC\n");
84 resp = gsm0808_create_cipher_complete(msg, chosen_encr);
Holger Hans Peter Freyther46c5ab32010-11-05 11:02:28 +010085 queue_msg_or_return(resp);
Holger Hans Peter Freyther32aaef62010-09-16 17:26:35 +080086}
87
Holger Hans Peter Freyther9b17c762010-11-05 19:48:47 +010088/*
89 * Instruct to reserve data for a new connectiom, create the complete
90 * layer three message, send it to open the connection.
91 */
Holger Hans Peter Freyther32aaef62010-09-16 17:26:35 +080092static int bsc_compl_l3(struct gsm_subscriber_connection *conn, struct msgb *msg,
93 uint16_t chosen_channel)
94{
Holger Hans Peter Freyther354c87c2011-06-07 11:40:20 +020095 struct osmo_msc_data *msc;
Holger Hans Peter Freyther9b17c762010-11-05 19:48:47 +010096
Harald Welte7d33bdf2011-07-12 00:05:11 +020097 LOGP(DMSC, LOGL_INFO, "Tx MSC COMPL L3\n");
98
Holger Hans Peter Freyther354c87c2011-06-07 11:40:20 +020099 /* find the MSC link we want to use */
Holger Hans Peter Freyther076af1c2011-06-07 19:57:02 +0200100 msc = bsc_find_msc(conn, msg);
Holger Hans Peter Freyther354c87c2011-06-07 11:40:20 +0200101 if (!msc) {
102 LOGP(DMSC, LOGL_ERROR, "Failed to find a MSC for a connection.\n");
103 return -1;
104 }
105
Holger Hans Peter Freyther83d2d382011-06-09 17:37:04 +0200106 return complete_layer3(conn, msg, msc);
107}
108
109static int complete_layer3(struct gsm_subscriber_connection *conn,
110 struct msgb *msg, struct osmo_msc_data *msc)
111{
112 struct msgb *resp;
113 uint16_t network_code;
114 uint16_t country_code;
115
Holger Hans Peter Freyther9b17c762010-11-05 19:48:47 +0100116 /* allocate resource for a new connection */
Holger Hans Peter Freyther354c87c2011-06-07 11:40:20 +0200117 if (bsc_create_new_connection(conn, msc) != 0)
Holger Hans Peter Freyther9b17c762010-11-05 19:48:47 +0100118 return BSC_API_CONN_POL_REJECT;
119
Holger Hans Peter Freyther4cdb0502011-06-04 14:51:51 +0200120 network_code = get_network_code_for_msc(conn->sccp_con->msc);
121 country_code = get_country_code_for_msc(conn->sccp_con->msc);
122
Holger Hans Peter Freyther9b17c762010-11-05 19:48:47 +0100123 bsc_scan_bts_msg(conn, msg);
124 resp = gsm0808_create_layer3(msg, network_code, country_code,
125 conn->bts->location_area_code,
126 conn->bts->cell_identity);
127 if (!resp) {
128 LOGP(DMSC, LOGL_DEBUG, "Failed to create layer3 message.\n");
Holger Hans Peter Freyther9b9a1712011-06-09 14:44:47 +0200129 sccp_connection_free(conn->sccp_con->sccp);
Holger Hans Peter Freyther7e901082010-11-06 20:15:17 +0100130 bsc_delete_connection(conn->sccp_con);
Holger Hans Peter Freyther9b17c762010-11-05 19:48:47 +0100131 return BSC_API_CONN_POL_REJECT;
Holger Hans Peter Freyther5bb874d2010-11-05 11:21:18 +0100132 }
133
Holger Hans Peter Freyther7e901082010-11-06 20:15:17 +0100134 if (bsc_open_connection(conn->sccp_con, resp) != 0) {
Holger Hans Peter Freyther9b9a1712011-06-09 14:44:47 +0200135 sccp_connection_free(conn->sccp_con->sccp);
Holger Hans Peter Freyther7e901082010-11-06 20:15:17 +0100136 bsc_delete_connection(conn->sccp_con);
Holger Hans Peter Freyther9b17c762010-11-05 19:48:47 +0100137 msgb_free(resp);
138 return BSC_API_CONN_POL_REJECT;
139 }
140
141 return BSC_API_CONN_POL_ACCEPT;
Holger Hans Peter Freyther32aaef62010-09-16 17:26:35 +0800142}
143
Holger Hans Peter Freyther83d2d382011-06-09 17:37:04 +0200144/*
145 * Plastic surgery... we want to give up the current connection
146 */
147static int move_to_msc(struct gsm_subscriber_connection *_conn,
148 struct msgb *msg, struct osmo_msc_data *msc)
149{
150 struct osmo_bsc_sccp_con *old_con = _conn->sccp_con;
151
152 /*
153 * 1. Give up the old connection.
154 * This happens by sending a clear request to the MSC,
155 * it should end with the MSC releasing the connection.
156 */
157 old_con->conn = NULL;
158 bsc_clear_request(_conn, 0);
159
160 /*
161 * 2. Attempt to create a new connection to the local
162 * MSC. If it fails the caller will need to handle this
163 * properly.
164 */
165 _conn->sccp_con = NULL;
166 if (complete_layer3(_conn, msg, msc) != BSC_API_CONN_POL_ACCEPT) {
167 gsm0808_clear(_conn);
168 subscr_con_free(_conn);
169 return 1;
170 }
171
172 return 2;
173}
174
175static int handle_cc_setup(struct gsm_subscriber_connection *conn,
176 struct msgb *msg)
177{
178 struct gsm48_hdr *gh = msgb_l3(msg);
179 uint8_t pdisc = gh->proto_discr & 0x0f;
180 uint8_t mtype = gh->msg_type & 0xbf;
181
182 struct osmo_msc_data *msc;
183 struct gsm_mncc_number called;
184 struct tlv_parsed tp;
185 unsigned payload_len;
186
187 char _dest_nr[35];
188
189 /*
190 * Do we have a setup message here? if not return fast.
191 */
192 if (pdisc != GSM48_PDISC_CC || mtype != GSM48_MT_CC_SETUP)
193 return 0;
194
195 payload_len = msgb_l3len(msg) - sizeof(*gh);
196
197 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
198 if (!TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
199 LOGP(DMSC, LOGL_ERROR, "Called BCD not present in setup.\n");
200 return -1;
201 }
202
203 memset(&called, 0, sizeof(called));
204 gsm48_decode_called(&called,
205 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD) - 1);
206
207 if (called.plan != 1)
208 return 0;
209
210 if (called.type == 1) {
211 _dest_nr[0] = _dest_nr[1] = '0';
212 memcpy(_dest_nr + 2, called.number, sizeof(called.number));
213 } else
214 memcpy(_dest_nr, called.number, sizeof(called.number));
215
216 /*
217 * Check if the connection should be moved...
218 */
219 llist_for_each_entry(msc, &conn->bts->network->bsc_data->mscs, entry) {
220 if (msc->type != MSC_CON_TYPE_LOCAL)
221 continue;
222 if (!msc->local_pref)
223 continue;
224 if (regexec(&msc->local_pref_reg, _dest_nr, 0, NULL, 0) != 0)
225 continue;
226
227 return move_to_msc(conn, msg, msc);
228 }
229
230 return 0;
231}
232
233
Holger Hans Peter Freyther46caa302010-11-04 12:18:00 +0100234static void bsc_dtap(struct gsm_subscriber_connection *conn, uint8_t link_id, struct msgb *msg)
Holger Hans Peter Freyther32aaef62010-09-16 17:26:35 +0800235{
Holger Hans Peter Freyther58f2cf62010-11-04 12:24:05 +0100236 struct msgb *resp;
Holger Hans Peter Freyther7225fd12010-11-03 19:03:35 +0100237 return_when_not_connected(conn);
Holger Hans Peter Freyther58f2cf62010-11-04 12:24:05 +0100238
Harald Welte7d33bdf2011-07-12 00:05:11 +0200239 LOGP(DMSC, LOGL_INFO, "Tx MSC DTAP LINK_ID=0x%02x\n", link_id);
240
Holger Hans Peter Freyther83d2d382011-06-09 17:37:04 +0200241 /*
242 * We might want to move this connection to a new MSC. Ask someone
243 * to handle it. If it was handled we will return.
244 */
245 if (handle_cc_setup(conn, msg) > 1)
246 return;
247
Holger Hans Peter Freyther5bb874d2010-11-05 11:21:18 +0100248 bsc_scan_bts_msg(conn, msg);
Holger Hans Peter Freyther83d2d382011-06-09 17:37:04 +0200249
250
Holger Hans Peter Freyther58f2cf62010-11-04 12:24:05 +0100251 resp = gsm0808_create_dtap(msg, link_id);
Holger Hans Peter Freyther46c5ab32010-11-05 11:02:28 +0100252 queue_msg_or_return(resp);
Holger Hans Peter Freyther32aaef62010-09-16 17:26:35 +0800253}
254
Holger Hans Peter Freytherb0941712010-11-04 12:06:57 +0100255static void bsc_assign_compl(struct gsm_subscriber_connection *conn, uint8_t rr_cause,
256 uint8_t chosen_channel, uint8_t encr_alg_id,
257 uint8_t speech_model)
Holger Hans Peter Freyther32aaef62010-09-16 17:26:35 +0800258{
Holger Hans Peter Freytherda488ae2010-11-04 12:09:45 +0100259 struct msgb *resp;
Holger Hans Peter Freyther7225fd12010-11-03 19:03:35 +0100260 return_when_not_connected(conn);
Holger Hans Peter Freytherda488ae2010-11-04 12:09:45 +0100261
Harald Welte7d33bdf2011-07-12 00:05:11 +0200262 LOGP(DMSC, LOGL_INFO, "Tx MSC ASSIGN COMPL\n");
263
Holger Hans Peter Freytherda488ae2010-11-04 12:09:45 +0100264 resp = gsm0808_create_assignment_completed(rr_cause, chosen_channel,
265 encr_alg_id, speech_model);
Holger Hans Peter Freyther46c5ab32010-11-05 11:02:28 +0100266 queue_msg_or_return(resp);
Holger Hans Peter Freyther32aaef62010-09-16 17:26:35 +0800267}
268
Holger Hans Peter Freyther45b575f2010-11-05 10:37:17 +0100269static void bsc_assign_fail(struct gsm_subscriber_connection *conn,
270 uint8_t cause, uint8_t *rr_cause)
Holger Hans Peter Freyther32aaef62010-09-16 17:26:35 +0800271{
Holger Hans Peter Freythere46f1d62010-11-05 10:59:45 +0100272 struct msgb *resp;
Holger Hans Peter Freyther7225fd12010-11-03 19:03:35 +0100273 return_when_not_connected(conn);
Holger Hans Peter Freythere46f1d62010-11-05 10:59:45 +0100274
Harald Welte7d33bdf2011-07-12 00:05:11 +0200275 LOGP(DMSC, LOGL_INFO, "Tx MSC ASSIGN FAIL\n");
276
Holger Hans Peter Freythere46f1d62010-11-05 10:59:45 +0100277 resp = gsm0808_create_assignment_failure(cause, rr_cause);
Holger Hans Peter Freyther46c5ab32010-11-05 11:02:28 +0100278 queue_msg_or_return(resp);
Holger Hans Peter Freyther32aaef62010-09-16 17:26:35 +0800279}
280
Holger Hans Peter Freyther05c68842010-11-03 19:01:58 +0100281static int bsc_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Hans Peter Freyther32aaef62010-09-16 17:26:35 +0800282{
Holger Hans Peter Freyther80c37bd2011-08-06 14:52:56 +0200283 struct osmo_bsc_sccp_con *sccp;
Holger Hans Peter Freytherc2b32702010-11-04 12:47:06 +0100284 struct msgb *resp;
Holger Hans Peter Freyther7225fd12010-11-03 19:03:35 +0100285 return_when_not_connected_val(conn, 1);
Holger Hans Peter Freytherc2b32702010-11-04 12:47:06 +0100286
Harald Welte7d33bdf2011-07-12 00:05:11 +0200287 LOGP(DMSC, LOGL_INFO, "Tx MSC CLEAR REQUEST\n");
288
Holger Hans Peter Freyther80c37bd2011-08-06 14:52:56 +0200289 /*
290 * Remove the connection from BSC<->SCCP part, the SCCP part
291 * will either be cleared by channel release or MSC disconnect
292 */
293 sccp = conn->sccp_con;
294 sccp->conn = NULL;
295 conn->sccp_con = NULL;
296
Holger Hans Peter Freytherc2b32702010-11-04 12:47:06 +0100297 resp = gsm0808_create_clear_rqst(GSM0808_CAUSE_RADIO_INTERFACE_FAILURE);
298 if (!resp) {
299 LOGP(DMSC, LOGL_ERROR, "Failed to allocate response.\n");
Holger Hans Peter Freyther80c37bd2011-08-06 14:52:56 +0200300 return 1;
Holger Hans Peter Freytherc2b32702010-11-04 12:47:06 +0100301 }
302
Holger Hans Peter Freyther80c37bd2011-08-06 14:52:56 +0200303 bsc_queue_for_msc(sccp, resp);
304 return 1;
Holger Hans Peter Freyther32aaef62010-09-16 17:26:35 +0800305}
306
Harald Welte95e862c2012-01-23 10:28:35 +0100307static void bsc_cm_update(struct gsm_subscriber_connection *conn,
308 const uint8_t *cm2, uint8_t cm2_len,
309 const uint8_t *cm3, uint8_t cm3_len)
310{
Harald Welte95e862c2012-01-23 10:28:35 +0100311 struct msgb *resp;
Holger Hans Peter Freytherdc3389f2012-03-16 11:49:46 +0100312 return_when_not_connected(conn);
Harald Welte95e862c2012-01-23 10:28:35 +0100313
314 resp = gsm0808_create_classmark_update(cm2, cm2_len, cm3, cm3_len);
315
316 queue_msg_or_return(resp);
317}
318
Holger Hans Peter Freyther32aaef62010-09-16 17:26:35 +0800319static struct bsc_api bsc_handler = {
320 .sapi_n_reject = bsc_sapi_n_reject,
321 .cipher_mode_compl = bsc_cipher_mode_compl,
Holger Hans Peter Freyther32aaef62010-09-16 17:26:35 +0800322 .compl_l3 = bsc_compl_l3,
323 .dtap = bsc_dtap,
324 .assign_compl = bsc_assign_compl,
325 .assign_fail = bsc_assign_fail,
326 .clear_request = bsc_clear_request,
Harald Welte95e862c2012-01-23 10:28:35 +0100327 .classmark_chg = bsc_cm_update,
Holger Hans Peter Freyther32aaef62010-09-16 17:26:35 +0800328};
329
Holger Hans Peter Freyther4f448c92010-07-05 16:02:04 +0800330struct bsc_api *osmo_bsc_api()
331{
Holger Hans Peter Freyther32aaef62010-09-16 17:26:35 +0800332 return &bsc_handler;
Holger Hans Peter Freyther4f448c92010-07-05 16:02:04 +0800333}