blob: ddc3836129cdddf7fcd905fdd4bcf4239ca25d12 [file] [log] [blame]
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +08001/* main MSC management code... */
2
3/*
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02004 * (C) 2010,2013 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Hans Peter Freyther85531cc2010-10-06 20:37:09 +08005 * (C) 2010 by On-Waves
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +08006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +080012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +080018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +080021 *
22 */
23
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020024#include <openbsc/osmo_msc.h>
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +080025#include <openbsc/bsc_api.h>
26#include <openbsc/debug.h>
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +080027#include <openbsc/transaction.h>
Harald Welte95e862c2012-01-23 10:28:35 +010028#include <openbsc/db.h>
Harald Welte2483f1b2016-06-19 18:06:02 +020029#include <openbsc/vlr.h>
30#include <openbsc/osmo_msc.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020031#include <openbsc/iu.h>
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +080032
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +080033#include <openbsc/gsm_04_11.h>
34
Harald Welte2483f1b2016-06-19 18:06:02 +020035/* Receive a SAPI-N-REJECT from BSC */
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +080036static void msc_sapi_n_reject(struct gsm_subscriber_connection *conn, int dlci)
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +080037{
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +080038 int sapi = dlci & 0x7;
39
40 if (sapi == UM_SAPI_SMS)
41 gsm411_sapi_n_reject(conn);
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +080042}
43
Harald Welte2483f1b2016-06-19 18:06:02 +020044static void subscr_conn_bump(struct gsm_subscriber_connection *conn)
45{
46 if (!conn)
47 return;
48 if (!conn->conn_fsm)
49 return;
50 if (!(conn->conn_fsm->state == SUBSCR_CONN_S_ACCEPTED
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020051 || conn->conn_fsm->state == SUBSCR_CONN_S_COMMUNICATING)) {
52 DEBUGP(DMM, "%s: bump: conn still being established (%s)\n",
53 vlr_subscr_name(conn->vsub),
54 osmo_fsm_inst_state_name(conn->conn_fsm));
Harald Welte2483f1b2016-06-19 18:06:02 +020055 return;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020056 }
Harald Welte2483f1b2016-06-19 18:06:02 +020057 osmo_fsm_inst_dispatch(conn->conn_fsm, SUBSCR_CONN_E_BUMP, NULL);
58}
59
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020060/* receive a Level 3 Complete message and return MSC_CONN_ACCEPT or
61 * MSC_CONN_REJECT */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020062int msc_compl_l3(struct gsm_subscriber_connection *conn,
63 struct msgb *msg, uint16_t chosen_channel)
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +080064{
Harald Welte2483f1b2016-06-19 18:06:02 +020065 msc_subscr_conn_get(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +080066 gsm0408_dispatch(conn, msg);
67
Harald Welte2483f1b2016-06-19 18:06:02 +020068 /* Bump whether the conn wants to be closed */
69 subscr_conn_bump(conn);
70
71 /* If this should be kept, the conn->conn_fsm has placed a use_count */
72 msc_subscr_conn_put(conn);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020073
74 /* Always return acceptance, because even if the conn was not accepted,
75 * we assumed ownership of it and the caller shall not interfere with
76 * that. We may even already have discarded the conn. */
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020077 return MSC_CONN_ACCEPT;
Harald Welte2483f1b2016-06-19 18:06:02 +020078
79#if 0
Holger Hans Peter Freythere9f420d2016-02-10 10:42:20 +010080 /*
81 * If this is a silent call we want the channel to remain open as long as
82 * possible and this is why we accept this connection regardless of any
83 * pending transaction or ongoing operation.
84 */
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +010085 if (conn->silent_call)
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020086 return MSC_CONN_ACCEPT;
87 if (conn->loc_operation || conn->sec_operation || conn->anch_operation)
88 return MSC_CONN_ACCEPT;
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +010089 if (trans_has_conn(conn))
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020090 return MSC_CONN_ACCEPT;
Jacob Erlbeck8e68b562014-01-30 21:01:12 +010091
92 LOGP(DRR, LOGL_INFO, "MSC Complete L3: Rejecting connection.\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020093 return MSC_CONN_REJECT;
Harald Welte2483f1b2016-06-19 18:06:02 +020094#endif
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +080095}
96
Harald Welte2483f1b2016-06-19 18:06:02 +020097/* Receive a DTAP message from BSC */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020098void msc_dtap(struct gsm_subscriber_connection *conn, uint8_t link_id, struct msgb *msg)
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +080099{
Harald Welte2483f1b2016-06-19 18:06:02 +0200100 msc_subscr_conn_get(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800101 gsm0408_dispatch(conn, msg);
Harald Welte2483f1b2016-06-19 18:06:02 +0200102
103 /* Bump whether the conn wants to be closed */
104 subscr_conn_bump(conn);
105 msc_subscr_conn_put(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800106}
107
Harald Welte2483f1b2016-06-19 18:06:02 +0200108/* Receive an ASSIGNMENT COMPLETE from BSC */
Holger Hans Peter Freyther40aac3f2011-12-27 12:31:02 +0100109static void msc_assign_compl(struct gsm_subscriber_connection *conn,
110 uint8_t rr_cause, uint8_t chosen_channel,
111 uint8_t encr_alg_id, uint8_t speec)
112{
Jacob Erlbeck8e68b562014-01-30 21:01:12 +0100113 LOGP(DRR, LOGL_DEBUG, "MSC assign complete (do nothing).\n");
Holger Hans Peter Freyther40aac3f2011-12-27 12:31:02 +0100114}
115
Harald Welte2483f1b2016-06-19 18:06:02 +0200116/* Receive an ASSIGNMENT FAILURE from BSC */
Holger Hans Peter Freyther40aac3f2011-12-27 12:31:02 +0100117static void msc_assign_fail(struct gsm_subscriber_connection *conn,
118 uint8_t cause, uint8_t *rr_cause)
119{
Jacob Erlbeck8e68b562014-01-30 21:01:12 +0100120 LOGP(DRR, LOGL_DEBUG, "MSC assign failure (do nothing).\n");
Holger Hans Peter Freyther40aac3f2011-12-27 12:31:02 +0100121}
122
Harald Welte2483f1b2016-06-19 18:06:02 +0200123/* Receive a CLASSMARK CHANGE from BSC */
Harald Welte95e862c2012-01-23 10:28:35 +0100124static void msc_classmark_chg(struct gsm_subscriber_connection *conn,
125 const uint8_t *cm2, uint8_t cm2_len,
126 const uint8_t *cm3, uint8_t cm3_len)
127{
Harald Welte2483f1b2016-06-19 18:06:02 +0200128 if (cm2 && cm2_len) {
129 if (cm2_len > sizeof(conn->classmark.classmark2)) {
130 LOGP(DRR, LOGL_NOTICE, "%s: classmark2 is %u bytes, truncating at %zu bytes\n",
131 vlr_subscr_name(conn->vsub), cm2_len, sizeof(conn->classmark.classmark2));
132 cm2_len = sizeof(conn->classmark.classmark2);
Harald Welte95e862c2012-01-23 10:28:35 +0100133 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200134 conn->classmark.classmark2_len = cm2_len;
135 memcpy(conn->classmark.classmark2, cm2, cm2_len);
136 }
137 if (cm3 && cm3_len) {
138 if (cm3_len > sizeof(conn->classmark.classmark3)) {
139 LOGP(DRR, LOGL_NOTICE, "%s: classmark3 is %u bytes, truncating at %zu bytes\n",
140 vlr_subscr_name(conn->vsub), cm3_len, sizeof(conn->classmark.classmark3));
141 cm3_len = sizeof(conn->classmark.classmark3);
142 }
143 conn->classmark.classmark3_len = cm3_len;
144 memcpy(conn->classmark.classmark3, cm3, cm3_len);
Harald Welte95e862c2012-01-23 10:28:35 +0100145 }
146}
147
Harald Welte2483f1b2016-06-19 18:06:02 +0200148/* Receive a CIPHERING MODE COMPLETE from BSC */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200149void msc_cipher_mode_compl(struct gsm_subscriber_connection *conn,
150 struct msgb *msg, uint8_t alg_id)
Harald Weltecf149ee2012-01-23 16:40:24 +0100151{
Harald Welte2483f1b2016-06-19 18:06:02 +0200152 struct gsm48_hdr *gh = msgb_l3(msg);
153 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
154 struct tlv_parsed tp;
155 uint8_t mi_type;
156 char imeisv[GSM48_MI_SIZE] = "";
157 struct vlr_ciph_result ciph_res = { .cause = VLR_CIPH_REJECT };
Harald Weltecf149ee2012-01-23 16:40:24 +0100158
Harald Welte2483f1b2016-06-19 18:06:02 +0200159 if (!gh) {
160 LOGP(DRR, LOGL_ERROR, "invalid: msgb without l3 header\n");
Harald Weltecf149ee2012-01-23 16:40:24 +0100161 return;
162 }
163
Harald Welte2483f1b2016-06-19 18:06:02 +0200164 if (!conn) {
165 LOGP(DRR, LOGL_ERROR,
166 "invalid: rx Ciphering Mode Complete on NULL conn\n");
167 return;
168 }
169 if (!conn->vsub) {
170 LOGP(DRR, LOGL_ERROR,
171 "invalid: rx Ciphering Mode Complete for NULL subscr\n");
172 return;
Harald Weltecf149ee2012-01-23 16:40:24 +0100173 }
174
Harald Welte2483f1b2016-06-19 18:06:02 +0200175 DEBUGP(DRR, "%s: CIPHERING MODE COMPLETE\n",
176 vlr_subscr_name(conn->vsub));
177
178 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
179
180 /* bearer capability */
181 if (TLVP_PRESENT(&tp, GSM48_IE_MOBILE_ID)) {
182 mi_type = TLVP_VAL(&tp, GSM48_IE_MOBILE_ID)[0] & GSM_MI_TYPE_MASK;
183 if (mi_type == GSM_MI_TYPE_IMEISV
184 && TLVP_LEN(&tp, GSM48_IE_MOBILE_ID) > 0) {
185 gsm48_mi_to_string(imeisv, sizeof(imeisv),
186 TLVP_VAL(&tp, GSM48_IE_MOBILE_ID),
187 TLVP_LEN(&tp, GSM48_IE_MOBILE_ID));
188 ciph_res.imeisv = imeisv;
189 }
190 }
191
192 ciph_res.cause = VLR_CIPH_COMPL;
193 vlr_subscr_rx_ciph_res(conn->vsub, &ciph_res);
Harald Weltecf149ee2012-01-23 16:40:24 +0100194}
195
Harald Welte2483f1b2016-06-19 18:06:02 +0200196struct gsm_subscriber_connection *msc_subscr_con_allocate(struct gsm_network *network)
197{
198 struct gsm_subscriber_connection *conn;
Harald Welte95e862c2012-01-23 10:28:35 +0100199
Harald Welte2483f1b2016-06-19 18:06:02 +0200200 conn = talloc_zero(network, struct gsm_subscriber_connection);
201 if (!conn)
202 return NULL;
Harald Welte95e862c2012-01-23 10:28:35 +0100203
Harald Welte2483f1b2016-06-19 18:06:02 +0200204 conn->network = network;
205 llist_add_tail(&conn->entry, &network->subscr_conns);
206 return conn;
207}
208
209void msc_subscr_cleanup(struct vlr_subscr *vsub)
210{
211 if (!vsub)
212 return;
213 vsub->lu_fsm = NULL;
214}
215
216void msc_subscr_con_cleanup(struct gsm_subscriber_connection *conn)
217{
218 if (!conn)
219 return;
220
221 if (conn->vsub) {
222 DEBUGP(DRLL, "subscr %s: Freeing subscriber connection\n",
223 vlr_subscr_name(conn->vsub));
224 msc_subscr_cleanup(conn->vsub);
225 vlr_subscr_put(conn->vsub);
226 conn->vsub = NULL;
227 } else
228 DEBUGP(DRLL, "Freeing subscriber connection"
229 " with NULL subscriber\n");
230
231 if (!conn->conn_fsm)
232 return;
233
234 osmo_fsm_inst_term(conn->conn_fsm,
235 (conn->conn_fsm->state == SUBSCR_CONN_S_RELEASED)
236 ? OSMO_FSM_TERM_REGULAR
237 : OSMO_FSM_TERM_ERROR,
238 NULL);
239}
240
241void msc_subscr_con_free(struct gsm_subscriber_connection *conn)
242{
243 if (!conn)
244 return;
245
246 msc_subscr_con_cleanup(conn);
247
248 llist_del(&conn->entry);
249 talloc_free(conn);
250}
251
252/* Receive a CLEAR REQUEST from BSC */
253static int msc_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
254{
255 msc_subscr_conn_close(conn, cause);
256 return 1;
257}
258
259/* MSC-level operations to be called by libbsc in NITB */
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +0800260static struct bsc_api msc_handler = {
261 .sapi_n_reject = msc_sapi_n_reject,
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800262 .compl_l3 = msc_compl_l3,
Neels Hofmeyrcc7db182016-12-18 23:52:38 +0100263 .dtap = msc_dtap,
Holger Hans Peter Freyther40aac3f2011-12-27 12:31:02 +0100264 .clear_request = msc_clear_request,
265 .assign_compl = msc_assign_compl,
266 .assign_fail = msc_assign_fail,
Harald Welte95e862c2012-01-23 10:28:35 +0100267 .classmark_chg = msc_classmark_chg,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200268 .cipher_mode_compl = msc_cipher_mode_compl,
Harald Welte2483f1b2016-06-19 18:06:02 +0200269 .conn_cleanup = msc_subscr_con_cleanup,
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +0800270};
271
272struct bsc_api *msc_bsc_api() {
273 return &msc_handler;
274}
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800275
Harald Welte2483f1b2016-06-19 18:06:02 +0200276static void msc_subscr_conn_release_all(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800277{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800278 if (conn->in_release)
279 return;
Harald Welte2483f1b2016-06-19 18:06:02 +0200280 conn->in_release = true;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800281
Harald Welte2483f1b2016-06-19 18:06:02 +0200282 /* If we're closing in a middle of a trans, we need to clean up */
283 trans_conn_closed(conn);
284
285 switch (conn->via_ran) {
286 case RAN_UTRAN_IU:
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200287 iu_tx_release(conn->iu.ue_ctx, NULL);
288 /* FIXME: keep the conn until the Iu Release Outcome is
289 * received from the UE, or a timeout expires. For now, the log
290 * says "unknown UE" for each release outcome. */
Harald Welte2483f1b2016-06-19 18:06:02 +0200291 break;
292 case RAN_GERAN_A:
293 /* future: a_iface_tx_clear_cmd(conn); */
294 break;
295 default:
296 LOGP(DMM, LOGL_ERROR, "%s: Unknown RAN type, cannot tx release/clear\n",
297 vlr_subscr_name(conn->vsub));
298 break;
299 }
300}
301
302/* If the conn->conn_fsm is still present, dispatch SUBSCR_CONN_E_CN_CLOSE
303 * event to gracefully terminate the connection. If the conn_fsm is already
304 * cleared, call msc_subscr_conn_release_all() to take release actions.
305 * \param cause a GSM_CAUSE_* constant, e.g. GSM_CAUSE_AUTH_FAILED.
306 */
307void msc_subscr_conn_close(struct gsm_subscriber_connection *conn,
308 uint32_t cause)
309{
310 if (!conn)
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800311 return;
Harald Welte2483f1b2016-06-19 18:06:02 +0200312 if (conn->in_release) {
313 DEBUGP(DMM, "msc_subscr_conn_close(vsub=%s, cause=%u):"
314 " already dispatching release, ignore.\n",
315 vlr_subscr_name(conn->vsub), cause);
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800316 return;
Harald Welte2483f1b2016-06-19 18:06:02 +0200317 }
318 if (!conn->conn_fsm) {
319 DEBUGP(DMM, "msc_subscr_conn_close(vsub=%s, cause=%u): no conn fsm,"
320 " releasing directly without release event.\n",
321 vlr_subscr_name(conn->vsub), cause);
322 /* In case of an IMSI Detach, we don't have conn_fsm. Release
323 * anyway to ensure a timely Iu Release / BSSMAP Clear. */
324 msc_subscr_conn_release_all(conn, cause);
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100325 return;
Harald Welte2483f1b2016-06-19 18:06:02 +0200326 }
327 if (conn->conn_fsm->state == SUBSCR_CONN_S_RELEASED) {
328 DEBUGP(DMM, "msc_subscr_conn_close(vsub=%s, cause=%u):"
329 " conn fsm already releasing, ignore.\n",
330 vlr_subscr_name(conn->vsub), cause);
331 return;
332 }
333 osmo_fsm_inst_dispatch(conn->conn_fsm, SUBSCR_CONN_E_CN_CLOSE, &cause);
334}
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800335
Harald Welte2483f1b2016-06-19 18:06:02 +0200336/* increment the ref-count. Needs to be called by every user */
337struct gsm_subscriber_connection *
338_msc_subscr_conn_get(struct gsm_subscriber_connection *conn,
339 const char *file, int line)
340{
341 OSMO_ASSERT(conn);
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200342
Harald Welte2483f1b2016-06-19 18:06:02 +0200343 if (conn->in_release)
344 return NULL;
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200345
Harald Welte2483f1b2016-06-19 18:06:02 +0200346 conn->use_count++;
347 LOGPSRC(DREF, LOGL_DEBUG, file, line,
348 "%s: MSC conn use + 1 == %u\n",
349 vlr_subscr_name(conn->vsub), conn->use_count);
350
351 return conn;
352}
353
354/* decrement the ref-count. Once it reaches zero, we release */
355void _msc_subscr_conn_put(struct gsm_subscriber_connection *conn,
356 const char *file, int line)
357{
358 OSMO_ASSERT(conn);
359
360 if (conn->use_count == 0) {
361 LOGPSRC(DREF, LOGL_ERROR, file, line,
362 "%s: MSC conn use - 1 failed: is already 0\n",
363 vlr_subscr_name(conn->vsub));
364 return;
365 }
366
367 conn->use_count--;
368 LOGPSRC(DREF, LOGL_DEBUG, file, line,
369 "%s: MSC conn use - 1 == %u\n",
370 vlr_subscr_name(conn->vsub), conn->use_count);
371
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200372 if (conn->use_count == 0)
373 msc_subscr_con_free(conn);
374}
375
376void msc_stop_paging(struct vlr_subscr *vsub)
377{
378 DEBUGP(DPAG, "Paging can stop for %s\n", vlr_subscr_name(vsub));
379 /* tell BSCs and RNCs to stop paging? How? */
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800380}