blob: 95e58182c5c6bcfb488fdcc84df2a6700cbe0538 [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
24#include <openbsc/bsc_api.h>
25#include <openbsc/debug.h>
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +080026#include <openbsc/transaction.h>
Harald Welte95e862c2012-01-23 10:28:35 +010027#include <openbsc/db.h>
Harald Welte7b423ed2016-06-19 18:06:02 +020028#include <openbsc/vlr.h>
29#include <openbsc/osmo_msc.h>
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +080030
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +080031#include <openbsc/gsm_04_11.h>
32
Harald Welte7b423ed2016-06-19 18:06:02 +020033/* Receive a SAPI-N-REJECT from BSC */
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +080034static void msc_sapi_n_reject(struct gsm_subscriber_connection *conn, int dlci)
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +080035{
Holger Hans Peter Freyther6a3d7652010-06-15 12:03:10 +080036 int sapi = dlci & 0x7;
37
38 if (sapi == UM_SAPI_SMS)
39 gsm411_sapi_n_reject(conn);
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +080040}
41
Harald Welte7b423ed2016-06-19 18:06:02 +020042static bool keep_conn(struct gsm_subscriber_connection *conn)
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +080043{
Harald Welte7b423ed2016-06-19 18:06:02 +020044 /* TODO: what about a silent call? */
45
46 if (!conn->conn_fsm) {
47 DEBUGP(DMM, "No conn_fsm, release conn\n");
48 return false;
49 }
50
51 switch (conn->conn_fsm->state) {
52 case SUBSCR_CONN_S_NEW:
53 case SUBSCR_CONN_S_ACCEPTED:
54 return true;
55 default:
56 return false;
57 }
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +080058}
59
Harald Welte7b423ed2016-06-19 18:06:02 +020060static void subscr_conn_bump(struct gsm_subscriber_connection *conn)
61{
62 if (!conn)
63 return;
64 if (!conn->conn_fsm)
65 return;
66 if (!(conn->conn_fsm->state == SUBSCR_CONN_S_ACCEPTED
67 || conn->conn_fsm->state == SUBSCR_CONN_S_COMMUNICATING))
68 return;
69 osmo_fsm_inst_dispatch(conn->conn_fsm, SUBSCR_CONN_E_BUMP, NULL);
70}
71
72/* Receive a COMPLETE LAYER3 INFO from BSC */
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +080073static int msc_compl_l3(struct gsm_subscriber_connection *conn, struct msgb *msg,
74 uint16_t chosen_channel)
75{
Harald Welte7b423ed2016-06-19 18:06:02 +020076 /* Ownership of the gsm_subscriber_connection is still a bit mucky
77 * between libbsc and libmsc. In libmsc, we use ref counting, but not
78 * in libbsc. This will become simpler with the MSCSPLIT. */
79
80 /* reserve for the duration of this function */
81 msc_subscr_conn_get(conn);
82
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +080083 gsm0408_dispatch(conn, msg);
84
Harald Welte7b423ed2016-06-19 18:06:02 +020085 if (!keep_conn(conn)) {
86 DEBUGP(DMM, "compl_l3: Discarding conn\n");
87 /* keep the use_count reserved, libbsc will discard. If we
88 * released the ref count and discarded here, libbsc would
89 * double-free. And we will not change bsc_api semantics. */
90 return BSC_API_CONN_POL_REJECT;
91 }
92 DEBUGP(DMM, "compl_l3: Keeping conn\n");
93
94 /* Bump whether the conn wants to be closed */
95 subscr_conn_bump(conn);
96
97 /* If this should be kept, the conn->conn_fsm has placed a use_count */
98 msc_subscr_conn_put(conn);
99 return BSC_API_CONN_POL_ACCEPT;
100
101#if 0
Holger Hans Peter Freythere9f420d2016-02-10 10:42:20 +0100102 /*
103 * If this is a silent call we want the channel to remain open as long as
104 * possible and this is why we accept this connection regardless of any
105 * pending transaction or ongoing operation.
106 */
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100107 if (conn->silent_call)
108 return BSC_API_CONN_POL_ACCEPT;
Harald Welte7b423ed2016-06-19 18:06:02 +0200109 if (conn->sec_operation || conn->anch_operation)
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100110 return BSC_API_CONN_POL_ACCEPT;
111 if (trans_has_conn(conn))
112 return BSC_API_CONN_POL_ACCEPT;
Jacob Erlbeck8e68b562014-01-30 21:01:12 +0100113
114 LOGP(DRR, LOGL_INFO, "MSC Complete L3: Rejecting connection.\n");
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100115 return BSC_API_CONN_POL_REJECT;
Harald Welte7b423ed2016-06-19 18:06:02 +0200116#endif
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800117}
118
Harald Welte7b423ed2016-06-19 18:06:02 +0200119/* Receive a DTAP message from BSC */
Holger Hans Peter Freyther46caa302010-11-04 12:18:00 +0100120static void msc_dtap(struct gsm_subscriber_connection *conn, uint8_t link_id, struct msgb *msg)
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800121{
Harald Welte7b423ed2016-06-19 18:06:02 +0200122 msc_subscr_conn_get(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800123 gsm0408_dispatch(conn, msg);
Harald Welte7b423ed2016-06-19 18:06:02 +0200124
125 /* Bump whether the conn wants to be closed */
126 subscr_conn_bump(conn);
127 msc_subscr_conn_put(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800128}
129
Harald Welte7b423ed2016-06-19 18:06:02 +0200130/* Receive an ASSIGNMENT COMPLETE from BSC */
Holger Hans Peter Freyther40aac3f2011-12-27 12:31:02 +0100131static void msc_assign_compl(struct gsm_subscriber_connection *conn,
132 uint8_t rr_cause, uint8_t chosen_channel,
133 uint8_t encr_alg_id, uint8_t speec)
134{
Jacob Erlbeck8e68b562014-01-30 21:01:12 +0100135 LOGP(DRR, LOGL_DEBUG, "MSC assign complete (do nothing).\n");
Holger Hans Peter Freyther40aac3f2011-12-27 12:31:02 +0100136}
137
Harald Welte7b423ed2016-06-19 18:06:02 +0200138/* Receive an ASSIGNMENT FAILURE from BSC */
Holger Hans Peter Freyther40aac3f2011-12-27 12:31:02 +0100139static void msc_assign_fail(struct gsm_subscriber_connection *conn,
140 uint8_t cause, uint8_t *rr_cause)
141{
Jacob Erlbeck8e68b562014-01-30 21:01:12 +0100142 LOGP(DRR, LOGL_DEBUG, "MSC assign failure (do nothing).\n");
Holger Hans Peter Freyther40aac3f2011-12-27 12:31:02 +0100143}
144
Harald Welte7b423ed2016-06-19 18:06:02 +0200145/* Receive a CLASSMARK CHANGE from BSC */
Harald Welte95e862c2012-01-23 10:28:35 +0100146static void msc_classmark_chg(struct gsm_subscriber_connection *conn,
147 const uint8_t *cm2, uint8_t cm2_len,
148 const uint8_t *cm3, uint8_t cm3_len)
149{
Harald Welte7b423ed2016-06-19 18:06:02 +0200150 if (cm2 && cm2_len) {
151 if (cm2_len > sizeof(conn->classmark.classmark2)) {
152 LOGP(DRR, LOGL_NOTICE, "%s: classmark2 is %u bytes, truncating at %zu bytes\n",
153 vlr_subscr_name(conn->vsub), cm2_len, sizeof(conn->classmark.classmark2));
154 cm2_len = sizeof(conn->classmark.classmark2);
Harald Welte95e862c2012-01-23 10:28:35 +0100155 }
Harald Welte7b423ed2016-06-19 18:06:02 +0200156 conn->classmark.classmark2_len = cm2_len;
157 memcpy(conn->classmark.classmark2, cm2, cm2_len);
158 }
159 if (cm3 && cm3_len) {
160 if (cm3_len > sizeof(conn->classmark.classmark3)) {
161 LOGP(DRR, LOGL_NOTICE, "%s: classmark3 is %u bytes, truncating at %zu bytes\n",
162 vlr_subscr_name(conn->vsub), cm3_len, sizeof(conn->classmark.classmark3));
163 cm3_len = sizeof(conn->classmark.classmark3);
164 }
165 conn->classmark.classmark3_len = cm3_len;
166 memcpy(conn->classmark.classmark3, cm3, cm3_len);
Harald Welte95e862c2012-01-23 10:28:35 +0100167 }
168}
169
Harald Welte7b423ed2016-06-19 18:06:02 +0200170/* Receive a CIPHERING MODE COMPLETE from BSC */
Harald Weltecf149ee2012-01-23 16:40:24 +0100171static void msc_ciph_m_compl(struct gsm_subscriber_connection *conn,
172 struct msgb *msg, uint8_t alg_id)
173{
Harald Welte7b423ed2016-06-19 18:06:02 +0200174 struct gsm48_hdr *gh = msgb_l3(msg);
175 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
176 struct tlv_parsed tp;
177 uint8_t mi_type;
178 char imeisv[GSM48_MI_SIZE] = "";
179 struct vlr_ciph_result ciph_res = { .cause = VLR_CIPH_REJECT };
Harald Weltecf149ee2012-01-23 16:40:24 +0100180
Harald Welte7b423ed2016-06-19 18:06:02 +0200181 if (!gh) {
182 LOGP(DRR, LOGL_ERROR, "invalid: msgb without l3 header\n");
Harald Weltecf149ee2012-01-23 16:40:24 +0100183 return;
184 }
185
Harald Welte7b423ed2016-06-19 18:06:02 +0200186 if (!conn) {
187 LOGP(DRR, LOGL_ERROR,
188 "invalid: rx Ciphering Mode Complete on NULL conn\n");
189 return;
190 }
191 if (!conn->vsub) {
192 LOGP(DRR, LOGL_ERROR,
193 "invalid: rx Ciphering Mode Complete for NULL subscr\n");
194 return;
Harald Weltecf149ee2012-01-23 16:40:24 +0100195 }
196
Harald Welte7b423ed2016-06-19 18:06:02 +0200197 DEBUGP(DRR, "%s: CIPHERING MODE COMPLETE\n",
198 vlr_subscr_name(conn->vsub));
199
200 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
201
202 /* bearer capability */
203 if (TLVP_PRESENT(&tp, GSM48_IE_MOBILE_ID)) {
204 mi_type = TLVP_VAL(&tp, GSM48_IE_MOBILE_ID)[0] & GSM_MI_TYPE_MASK;
205 if (mi_type == GSM_MI_TYPE_IMEISV
206 && TLVP_LEN(&tp, GSM48_IE_MOBILE_ID) > 0) {
207 gsm48_mi_to_string(imeisv, sizeof(imeisv),
208 TLVP_VAL(&tp, GSM48_IE_MOBILE_ID),
209 TLVP_LEN(&tp, GSM48_IE_MOBILE_ID));
210 ciph_res.imeisv = imeisv;
211 }
212 }
213
214 ciph_res.cause = VLR_CIPH_COMPL;
215 vlr_subscr_rx_ciph_res(conn->vsub, &ciph_res);
Harald Weltecf149ee2012-01-23 16:40:24 +0100216}
217
Harald Welte7b423ed2016-06-19 18:06:02 +0200218struct gsm_subscriber_connection *msc_subscr_con_allocate(struct gsm_network *network)
219{
220 struct gsm_subscriber_connection *conn;
Harald Welte95e862c2012-01-23 10:28:35 +0100221
Harald Welte7b423ed2016-06-19 18:06:02 +0200222 conn = talloc_zero(network, struct gsm_subscriber_connection);
223 if (!conn)
224 return NULL;
Harald Welte95e862c2012-01-23 10:28:35 +0100225
Harald Welte7b423ed2016-06-19 18:06:02 +0200226 conn->network = network;
227 llist_add_tail(&conn->entry, &network->subscr_conns);
228 return conn;
229}
230
231void msc_subscr_cleanup(struct vlr_subscr *vsub)
232{
233 if (!vsub)
234 return;
235 vsub->lu_fsm = NULL;
236}
237
238void msc_subscr_con_cleanup(struct gsm_subscriber_connection *conn)
239{
240 if (!conn)
241 return;
242
243 if (conn->vsub) {
244 DEBUGP(DRLL, "subscr %s: Freeing subscriber connection\n",
245 vlr_subscr_name(conn->vsub));
246 msc_subscr_cleanup(conn->vsub);
247 vlr_subscr_put(conn->vsub);
248 conn->vsub = NULL;
249 } else
250 DEBUGP(DRLL, "Freeing subscriber connection"
251 " with NULL subscriber\n");
252
253 if (!conn->conn_fsm)
254 return;
255
256 osmo_fsm_inst_term(conn->conn_fsm,
257 (conn->conn_fsm->state == SUBSCR_CONN_S_RELEASED)
258 ? OSMO_FSM_TERM_REGULAR
259 : OSMO_FSM_TERM_ERROR,
260 NULL);
261}
262
263void msc_subscr_con_free(struct gsm_subscriber_connection *conn)
264{
265 if (!conn)
266 return;
267
268 msc_subscr_con_cleanup(conn);
269
270 llist_del(&conn->entry);
271 talloc_free(conn);
272}
273
274/* Receive a CLEAR REQUEST from BSC */
275static int msc_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
276{
277 msc_subscr_conn_close(conn, cause);
278 return 1;
279}
280
281/* MSC-level operations to be called by libbsc in NITB */
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +0800282static struct bsc_api msc_handler = {
283 .sapi_n_reject = msc_sapi_n_reject,
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +0800284 .compl_l3 = msc_compl_l3,
Neels Hofmeyrcc7db182016-12-18 23:52:38 +0100285 .dtap = msc_dtap,
Holger Hans Peter Freyther40aac3f2011-12-27 12:31:02 +0100286 .clear_request = msc_clear_request,
287 .assign_compl = msc_assign_compl,
288 .assign_fail = msc_assign_fail,
Harald Welte95e862c2012-01-23 10:28:35 +0100289 .classmark_chg = msc_classmark_chg,
Harald Weltecf149ee2012-01-23 16:40:24 +0100290 .cipher_mode_compl = msc_ciph_m_compl,
Harald Welte7b423ed2016-06-19 18:06:02 +0200291 .conn_cleanup = msc_subscr_con_cleanup,
Holger Hans Peter Freyther43b09092010-06-15 11:52:51 +0800292};
293
294struct bsc_api *msc_bsc_api() {
295 return &msc_handler;
296}
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800297
Harald Welte7b423ed2016-06-19 18:06:02 +0200298static void msc_subscr_conn_release_all(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800299{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800300 if (conn->in_release)
301 return;
Harald Welte7b423ed2016-06-19 18:06:02 +0200302 conn->in_release = true;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800303
Harald Welte7b423ed2016-06-19 18:06:02 +0200304 /* If we're closing in a middle of a trans, we need to clean up */
305 trans_conn_closed(conn);
306
307 switch (conn->via_ran) {
308 case RAN_UTRAN_IU:
309 /* future: iu_tx_release(conn->iu.ue_ctx, NULL); */
310 break;
311 case RAN_GERAN_A:
312 /* future: a_iface_tx_clear_cmd(conn); */
313 break;
314 default:
315 LOGP(DMM, LOGL_ERROR, "%s: Unknown RAN type, cannot tx release/clear\n",
316 vlr_subscr_name(conn->vsub));
317 break;
318 }
319}
320
321/* If the conn->conn_fsm is still present, dispatch SUBSCR_CONN_E_CN_CLOSE
322 * event to gracefully terminate the connection. If the conn_fsm is already
323 * cleared, call msc_subscr_conn_release_all() to take release actions.
324 * \param cause a GSM_CAUSE_* constant, e.g. GSM_CAUSE_AUTH_FAILED.
325 */
326void msc_subscr_conn_close(struct gsm_subscriber_connection *conn,
327 uint32_t cause)
328{
329 if (!conn)
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800330 return;
Harald Welte7b423ed2016-06-19 18:06:02 +0200331 if (conn->in_release) {
332 DEBUGP(DMM, "msc_subscr_conn_close(vsub=%s, cause=%u):"
333 " already dispatching release, ignore.\n",
334 vlr_subscr_name(conn->vsub), cause);
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800335 return;
Harald Welte7b423ed2016-06-19 18:06:02 +0200336 }
337 if (!conn->conn_fsm) {
338 DEBUGP(DMM, "msc_subscr_conn_close(vsub=%s, cause=%u): no conn fsm,"
339 " releasing directly without release event.\n",
340 vlr_subscr_name(conn->vsub), cause);
341 /* In case of an IMSI Detach, we don't have conn_fsm. Release
342 * anyway to ensure a timely Iu Release / BSSMAP Clear. */
343 msc_subscr_conn_release_all(conn, cause);
Holger Hans Peter Freyther70ae5d32012-11-23 21:33:15 +0100344 return;
Harald Welte7b423ed2016-06-19 18:06:02 +0200345 }
346 if (conn->conn_fsm->state == SUBSCR_CONN_S_RELEASED) {
347 DEBUGP(DMM, "msc_subscr_conn_close(vsub=%s, cause=%u):"
348 " conn fsm already releasing, ignore.\n",
349 vlr_subscr_name(conn->vsub), cause);
350 return;
351 }
352 osmo_fsm_inst_dispatch(conn->conn_fsm, SUBSCR_CONN_E_CN_CLOSE, &cause);
353}
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800354
Harald Welte7b423ed2016-06-19 18:06:02 +0200355/* increment the ref-count. Needs to be called by every user */
356struct gsm_subscriber_connection *
357_msc_subscr_conn_get(struct gsm_subscriber_connection *conn,
358 const char *file, int line)
359{
360 OSMO_ASSERT(conn);
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200361
Harald Welte7b423ed2016-06-19 18:06:02 +0200362 if (conn->in_release)
363 return NULL;
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200364
Harald Welte7b423ed2016-06-19 18:06:02 +0200365 conn->use_count++;
366 LOGPSRC(DREF, LOGL_DEBUG, file, line,
367 "%s: MSC conn use + 1 == %u\n",
368 vlr_subscr_name(conn->vsub), conn->use_count);
369
370 return conn;
371}
372
373/* decrement the ref-count. Once it reaches zero, we release */
374void _msc_subscr_conn_put(struct gsm_subscriber_connection *conn,
375 const char *file, int line)
376{
377 OSMO_ASSERT(conn);
378
379 if (conn->use_count == 0) {
380 LOGPSRC(DREF, LOGL_ERROR, file, line,
381 "%s: MSC conn use - 1 failed: is already 0\n",
382 vlr_subscr_name(conn->vsub));
383 return;
384 }
385
386 conn->use_count--;
387 LOGPSRC(DREF, LOGL_DEBUG, file, line,
388 "%s: MSC conn use - 1 == %u\n",
389 vlr_subscr_name(conn->vsub), conn->use_count);
390
391 if (conn->use_count == 0) {
392 gsm0808_clear(conn);
393 bsc_subscr_con_free(conn);
394 }
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800395}