Holger Hans Peter Freyther | 43b0909 | 2010-06-15 11:52:51 +0800 | [diff] [blame] | 1 | /* main MSC management code... */ |
| 2 | |
| 3 | /* |
Holger Hans Peter Freyther | e7bd863 | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 4 | * (C) 2010,2013 by Holger Hans Peter Freyther <zecke@selfish.org> |
Holger Hans Peter Freyther | 85531cc | 2010-10-06 20:37:09 +0800 | [diff] [blame] | 5 | * (C) 2010 by On-Waves |
Holger Hans Peter Freyther | 43b0909 | 2010-06-15 11:52:51 +0800 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 10 | * 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 Freyther | 43b0909 | 2010-06-15 11:52:51 +0800 | [diff] [blame] | 12 | * (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 Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 17 | * GNU Affero General Public License for more details. |
Holger Hans Peter Freyther | 43b0909 | 2010-06-15 11:52:51 +0800 | [diff] [blame] | 18 | * |
Harald Welte | 9af6ddf | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 19 | * 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 Freyther | 43b0909 | 2010-06-15 11:52:51 +0800 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 24 | #include <osmocom/msc/osmo_msc.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 25 | #include <osmocom/msc/debug.h> |
| 26 | #include <osmocom/msc/transaction.h> |
| 27 | #include <osmocom/msc/db.h> |
| 28 | #include <osmocom/msc/vlr.h> |
| 29 | #include <osmocom/msc/osmo_msc.h> |
| 30 | #include <osmocom/msc/a_iface.h> |
Max | 43b01b0 | 2017-09-15 11:22:30 +0200 | [diff] [blame] | 31 | #include <osmocom/msc/gsm_04_08.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 32 | #include <osmocom/msc/gsm_04_11.h> |
Holger Hans Peter Freyther | 6a3d765 | 2010-06-15 12:03:10 +0800 | [diff] [blame] | 33 | |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 34 | #include "../../bscconfig.h" |
| 35 | #ifdef BUILD_IU |
| 36 | #include <osmocom/ranap/iu_client.h> |
| 37 | #else |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 38 | #include <osmocom/msc/iu_dummy.h> |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 39 | #endif |
| 40 | |
Neels Hofmeyr | 55014d0 | 2018-03-22 16:43:40 +0100 | [diff] [blame] | 41 | struct gsm_network *gsm_network_init(void *ctx, mncc_recv_cb_t mncc_recv) |
| 42 | { |
| 43 | struct gsm_network *net; |
| 44 | |
| 45 | net = talloc_zero(ctx, struct gsm_network); |
| 46 | if (!net) |
| 47 | return NULL; |
| 48 | |
| 49 | net->plmn = (struct osmo_plmn_id){ .mcc=1, .mnc=1 }; |
| 50 | |
| 51 | /* Permit a compile-time default of A5/3 and A5/1 */ |
| 52 | net->a5_encryption_mask = (1 << 3) | (1 << 1); |
| 53 | |
| 54 | /* Use 30 min periodic update interval as sane default */ |
| 55 | net->t3212 = 5; |
| 56 | |
| 57 | net->paging_response_timer = MSC_PAGING_RESPONSE_TIMER_DEFAULT; |
| 58 | |
| 59 | INIT_LLIST_HEAD(&net->trans_list); |
| 60 | INIT_LLIST_HEAD(&net->upqueue); |
| 61 | INIT_LLIST_HEAD(&net->subscr_conns); |
| 62 | |
| 63 | /* init statistics */ |
| 64 | net->msc_ctrs = rate_ctr_group_alloc(net, &msc_ctrg_desc, 0); |
| 65 | if (!net->msc_ctrs) { |
| 66 | talloc_free(net); |
| 67 | return NULL; |
| 68 | } |
| 69 | net->active_calls = osmo_counter_alloc("msc.active_calls"); |
| 70 | |
| 71 | net->mncc_recv = mncc_recv; |
| 72 | |
| 73 | INIT_LLIST_HEAD(&net->a.bscs); |
| 74 | |
| 75 | return net; |
| 76 | } |
| 77 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 78 | /* Receive a SAPI-N-REJECT from BSC */ |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 79 | void msc_sapi_n_reject(struct gsm_subscriber_connection *conn, int dlci) |
Holger Hans Peter Freyther | 43b0909 | 2010-06-15 11:52:51 +0800 | [diff] [blame] | 80 | { |
Holger Hans Peter Freyther | 6a3d765 | 2010-06-15 12:03:10 +0800 | [diff] [blame] | 81 | int sapi = dlci & 0x7; |
| 82 | |
| 83 | if (sapi == UM_SAPI_SMS) |
| 84 | gsm411_sapi_n_reject(conn); |
Holger Hans Peter Freyther | 43b0909 | 2010-06-15 11:52:51 +0800 | [diff] [blame] | 85 | } |
| 86 | |
Neels Hofmeyr | d6a769b | 2018-03-12 23:59:07 +0100 | [diff] [blame] | 87 | void subscr_conn_release_when_unused(struct gsm_subscriber_connection *conn) |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 88 | { |
| 89 | if (!conn) |
| 90 | return; |
Neels Hofmeyr | 4d3a66b | 2018-03-31 18:45:59 +0200 | [diff] [blame] | 91 | if (!conn->fi) |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 92 | return; |
Neels Hofmeyr | 4d3a66b | 2018-03-31 18:45:59 +0200 | [diff] [blame] | 93 | if (!(conn->fi->state == SUBSCR_CONN_S_ACCEPTED |
| 94 | || conn->fi->state == SUBSCR_CONN_S_COMMUNICATING)) { |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 95 | DEBUGP(DMM, "%s: %s: conn still being established (%s)\n", |
| 96 | vlr_subscr_name(conn->vsub), __func__, |
Neels Hofmeyr | 4d3a66b | 2018-03-31 18:45:59 +0200 | [diff] [blame] | 97 | osmo_fsm_inst_state_name(conn->fi)); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 98 | return; |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 99 | } |
Neels Hofmeyr | 4d3a66b | 2018-03-31 18:45:59 +0200 | [diff] [blame] | 100 | osmo_fsm_inst_dispatch(conn->fi, SUBSCR_CONN_E_RELEASE_WHEN_UNUSED, NULL); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 103 | /* receive a Level 3 Complete message and return MSC_CONN_ACCEPT or |
| 104 | * MSC_CONN_REJECT */ |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 105 | int msc_compl_l3(struct gsm_subscriber_connection *conn, |
| 106 | struct msgb *msg, uint16_t chosen_channel) |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 107 | { |
Neels Hofmeyr | 6166f29 | 2017-11-22 14:33:12 +0100 | [diff] [blame] | 108 | msc_subscr_conn_get(conn, MSC_CONN_USE_COMPL_L3); |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 109 | gsm0408_dispatch(conn, msg); |
| 110 | |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 111 | subscr_conn_release_when_unused(conn); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 112 | |
Neels Hofmeyr | 4d3a66b | 2018-03-31 18:45:59 +0200 | [diff] [blame] | 113 | /* If this should be kept, the conn->fi has placed a use_count */ |
Neels Hofmeyr | 6166f29 | 2017-11-22 14:33:12 +0100 | [diff] [blame] | 114 | msc_subscr_conn_put(conn, MSC_CONN_USE_COMPL_L3); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 115 | |
| 116 | /* Always return acceptance, because even if the conn was not accepted, |
| 117 | * we assumed ownership of it and the caller shall not interfere with |
| 118 | * that. We may even already have discarded the conn. */ |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 119 | return MSC_CONN_ACCEPT; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 120 | |
| 121 | #if 0 |
Holger Hans Peter Freyther | e9f420d | 2016-02-10 10:42:20 +0100 | [diff] [blame] | 122 | /* |
| 123 | * If this is a silent call we want the channel to remain open as long as |
| 124 | * possible and this is why we accept this connection regardless of any |
| 125 | * pending transaction or ongoing operation. |
| 126 | */ |
Holger Hans Peter Freyther | 70ae5d3 | 2012-11-23 21:33:15 +0100 | [diff] [blame] | 127 | if (conn->silent_call) |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 128 | return MSC_CONN_ACCEPT; |
| 129 | if (conn->loc_operation || conn->sec_operation || conn->anch_operation) |
| 130 | return MSC_CONN_ACCEPT; |
Holger Hans Peter Freyther | 70ae5d3 | 2012-11-23 21:33:15 +0100 | [diff] [blame] | 131 | if (trans_has_conn(conn)) |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 132 | return MSC_CONN_ACCEPT; |
Jacob Erlbeck | 8e68b56 | 2014-01-30 21:01:12 +0100 | [diff] [blame] | 133 | |
| 134 | LOGP(DRR, LOGL_INFO, "MSC Complete L3: Rejecting connection.\n"); |
Neels Hofmeyr | e2f24d5 | 2017-05-08 15:12:20 +0200 | [diff] [blame] | 135 | return MSC_CONN_REJECT; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 136 | #endif |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 137 | } |
| 138 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 139 | /* Receive a DTAP message from BSC */ |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 140 | void msc_dtap(struct gsm_subscriber_connection *conn, uint8_t link_id, struct msgb *msg) |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 141 | { |
Neels Hofmeyr | 6166f29 | 2017-11-22 14:33:12 +0100 | [diff] [blame] | 142 | msc_subscr_conn_get(conn, MSC_CONN_USE_DTAP); |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 143 | gsm0408_dispatch(conn, msg); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 144 | |
Neels Hofmeyr | e9e2f5c | 2018-03-15 13:26:43 +0100 | [diff] [blame] | 145 | subscr_conn_release_when_unused(conn); |
Neels Hofmeyr | 6166f29 | 2017-11-22 14:33:12 +0100 | [diff] [blame] | 146 | msc_subscr_conn_put(conn, MSC_CONN_USE_DTAP); |
Holger Hans Peter Freyther | 9764331 | 2010-06-17 16:41:25 +0800 | [diff] [blame] | 147 | } |
| 148 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 149 | /* Receive an ASSIGNMENT COMPLETE from BSC */ |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 150 | void msc_assign_compl(struct gsm_subscriber_connection *conn, |
| 151 | uint8_t rr_cause, uint8_t chosen_channel, |
| 152 | uint8_t encr_alg_id, uint8_t speec) |
Holger Hans Peter Freyther | 40aac3f | 2011-12-27 12:31:02 +0100 | [diff] [blame] | 153 | { |
Jacob Erlbeck | 8e68b56 | 2014-01-30 21:01:12 +0100 | [diff] [blame] | 154 | LOGP(DRR, LOGL_DEBUG, "MSC assign complete (do nothing).\n"); |
Holger Hans Peter Freyther | 40aac3f | 2011-12-27 12:31:02 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 157 | /* Receive an ASSIGNMENT FAILURE from BSC */ |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 158 | void msc_assign_fail(struct gsm_subscriber_connection *conn, |
| 159 | uint8_t cause, uint8_t *rr_cause) |
Holger Hans Peter Freyther | 40aac3f | 2011-12-27 12:31:02 +0100 | [diff] [blame] | 160 | { |
Jacob Erlbeck | 8e68b56 | 2014-01-30 21:01:12 +0100 | [diff] [blame] | 161 | LOGP(DRR, LOGL_DEBUG, "MSC assign failure (do nothing).\n"); |
Holger Hans Peter Freyther | 40aac3f | 2011-12-27 12:31:02 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 164 | /* Receive a CLASSMARK CHANGE from BSC */ |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 165 | void msc_classmark_chg(struct gsm_subscriber_connection *conn, |
| 166 | const uint8_t *cm2, uint8_t cm2_len, |
| 167 | const uint8_t *cm3, uint8_t cm3_len) |
Harald Welte | 95e862c | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 168 | { |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 169 | if (cm2 && cm2_len) { |
| 170 | if (cm2_len > sizeof(conn->classmark.classmark2)) { |
| 171 | LOGP(DRR, LOGL_NOTICE, "%s: classmark2 is %u bytes, truncating at %zu bytes\n", |
| 172 | vlr_subscr_name(conn->vsub), cm2_len, sizeof(conn->classmark.classmark2)); |
| 173 | cm2_len = sizeof(conn->classmark.classmark2); |
Harald Welte | 95e862c | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 174 | } |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 175 | conn->classmark.classmark2_len = cm2_len; |
| 176 | memcpy(conn->classmark.classmark2, cm2, cm2_len); |
| 177 | } |
| 178 | if (cm3 && cm3_len) { |
| 179 | if (cm3_len > sizeof(conn->classmark.classmark3)) { |
| 180 | LOGP(DRR, LOGL_NOTICE, "%s: classmark3 is %u bytes, truncating at %zu bytes\n", |
| 181 | vlr_subscr_name(conn->vsub), cm3_len, sizeof(conn->classmark.classmark3)); |
| 182 | cm3_len = sizeof(conn->classmark.classmark3); |
| 183 | } |
| 184 | conn->classmark.classmark3_len = cm3_len; |
| 185 | memcpy(conn->classmark.classmark3, cm3, cm3_len); |
Harald Welte | 95e862c | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 189 | /* Receive a CIPHERING MODE COMPLETE from BSC */ |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 190 | void msc_cipher_mode_compl(struct gsm_subscriber_connection *conn, |
| 191 | struct msgb *msg, uint8_t alg_id) |
Harald Welte | cf149ee | 2012-01-23 16:40:24 +0100 | [diff] [blame] | 192 | { |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 193 | struct vlr_ciph_result ciph_res = { .cause = VLR_CIPH_REJECT }; |
Harald Welte | cf149ee | 2012-01-23 16:40:24 +0100 | [diff] [blame] | 194 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 195 | if (!conn) { |
Harald Welte | 284c39a | 2018-01-24 22:38:06 +0100 | [diff] [blame] | 196 | LOGP(DRR, LOGL_ERROR, "invalid: rx Ciphering Mode Complete on NULL conn\n"); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 197 | return; |
| 198 | } |
| 199 | if (!conn->vsub) { |
Harald Welte | 284c39a | 2018-01-24 22:38:06 +0100 | [diff] [blame] | 200 | LOGP(DRR, LOGL_ERROR, "invalid: rx Ciphering Mode Complete for NULL subscr\n"); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 201 | return; |
Harald Welte | cf149ee | 2012-01-23 16:40:24 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Harald Welte | 284c39a | 2018-01-24 22:38:06 +0100 | [diff] [blame] | 204 | DEBUGP(DRR, "%s: CIPHERING MODE COMPLETE\n", vlr_subscr_name(conn->vsub)); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 205 | |
Harald Welte | 284c39a | 2018-01-24 22:38:06 +0100 | [diff] [blame] | 206 | if (msg) { |
| 207 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 208 | unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh); |
| 209 | struct tlv_parsed tp; |
| 210 | uint8_t mi_type; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 211 | |
Harald Welte | 284c39a | 2018-01-24 22:38:06 +0100 | [diff] [blame] | 212 | if (!gh) { |
| 213 | LOGP(DRR, LOGL_ERROR, "invalid: msgb without l3 header\n"); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0); |
| 218 | |
| 219 | /* bearer capability */ |
| 220 | if (TLVP_PRESENT(&tp, GSM48_IE_MOBILE_ID)) { |
| 221 | mi_type = TLVP_VAL(&tp, GSM48_IE_MOBILE_ID)[0] & GSM_MI_TYPE_MASK; |
| 222 | if (mi_type == GSM_MI_TYPE_IMEISV |
| 223 | && TLVP_LEN(&tp, GSM48_IE_MOBILE_ID) > 0) { |
Neels Hofmeyr | fa10eda | 2018-03-13 01:22:01 +0100 | [diff] [blame] | 224 | gsm48_mi_to_string(ciph_res.imeisv, sizeof(ciph_res.imeisv), |
Harald Welte | 284c39a | 2018-01-24 22:38:06 +0100 | [diff] [blame] | 225 | TLVP_VAL(&tp, GSM48_IE_MOBILE_ID), |
| 226 | TLVP_LEN(&tp, GSM48_IE_MOBILE_ID)); |
Harald Welte | 284c39a | 2018-01-24 22:38:06 +0100 | [diff] [blame] | 227 | } |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | ciph_res.cause = VLR_CIPH_COMPL; |
| 232 | vlr_subscr_rx_ciph_res(conn->vsub, &ciph_res); |
Harald Welte | cf149ee | 2012-01-23 16:40:24 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 235 | struct gsm_subscriber_connection *msc_subscr_con_allocate(struct gsm_network *network) |
| 236 | { |
| 237 | struct gsm_subscriber_connection *conn; |
Harald Welte | 95e862c | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 238 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 239 | conn = talloc_zero(network, struct gsm_subscriber_connection); |
| 240 | if (!conn) |
| 241 | return NULL; |
Harald Welte | 95e862c | 2012-01-23 10:28:35 +0100 | [diff] [blame] | 242 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 243 | conn->network = network; |
| 244 | llist_add_tail(&conn->entry, &network->subscr_conns); |
| 245 | return conn; |
| 246 | } |
| 247 | |
| 248 | void msc_subscr_cleanup(struct vlr_subscr *vsub) |
| 249 | { |
| 250 | if (!vsub) |
| 251 | return; |
| 252 | vsub->lu_fsm = NULL; |
| 253 | } |
| 254 | |
| 255 | void msc_subscr_con_cleanup(struct gsm_subscriber_connection *conn) |
| 256 | { |
| 257 | if (!conn) |
| 258 | return; |
| 259 | |
| 260 | if (conn->vsub) { |
| 261 | DEBUGP(DRLL, "subscr %s: Freeing subscriber connection\n", |
| 262 | vlr_subscr_name(conn->vsub)); |
| 263 | msc_subscr_cleanup(conn->vsub); |
Neels Hofmeyr | 1db394f | 2018-03-09 17:04:53 +0100 | [diff] [blame] | 264 | conn->vsub->msc_conn_ref = NULL; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 265 | vlr_subscr_put(conn->vsub); |
| 266 | conn->vsub = NULL; |
| 267 | } else |
| 268 | DEBUGP(DRLL, "Freeing subscriber connection" |
| 269 | " with NULL subscriber\n"); |
| 270 | |
Neels Hofmeyr | 4d3a66b | 2018-03-31 18:45:59 +0200 | [diff] [blame] | 271 | if (!conn->fi) |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 272 | return; |
| 273 | |
Neels Hofmeyr | 4d3a66b | 2018-03-31 18:45:59 +0200 | [diff] [blame] | 274 | osmo_fsm_inst_term(conn->fi, |
| 275 | (conn->fi->state == SUBSCR_CONN_S_RELEASED) |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 276 | ? OSMO_FSM_TERM_REGULAR |
| 277 | : OSMO_FSM_TERM_ERROR, |
| 278 | NULL); |
| 279 | } |
| 280 | |
| 281 | void msc_subscr_con_free(struct gsm_subscriber_connection *conn) |
| 282 | { |
| 283 | if (!conn) |
| 284 | return; |
| 285 | |
| 286 | msc_subscr_con_cleanup(conn); |
| 287 | |
| 288 | llist_del(&conn->entry); |
| 289 | talloc_free(conn); |
| 290 | } |
| 291 | |
| 292 | /* Receive a CLEAR REQUEST from BSC */ |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 293 | int msc_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause) |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 294 | { |
| 295 | msc_subscr_conn_close(conn, cause); |
| 296 | return 1; |
| 297 | } |
| 298 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 299 | static void msc_subscr_conn_release_all(struct gsm_subscriber_connection *conn, uint32_t cause) |
Holger Hans Peter Freyther | 4049455 | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 300 | { |
Holger Hans Peter Freyther | 4049455 | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 301 | if (conn->in_release) |
| 302 | return; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 303 | conn->in_release = true; |
Holger Hans Peter Freyther | 4049455 | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 304 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 305 | /* If we're closing in a middle of a trans, we need to clean up */ |
| 306 | trans_conn_closed(conn); |
| 307 | |
| 308 | switch (conn->via_ran) { |
| 309 | case RAN_UTRAN_IU: |
Neels Hofmeyr | 00e82d6 | 2017-07-05 15:19:52 +0200 | [diff] [blame] | 310 | ranap_iu_tx_release(conn->iu.ue_ctx, NULL); |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 311 | /* FIXME: keep the conn until the Iu Release Outcome is |
| 312 | * received from the UE, or a timeout expires. For now, the log |
| 313 | * says "unknown UE" for each release outcome. */ |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 314 | break; |
| 315 | case RAN_GERAN_A: |
Philipp Maier | fbf6610 | 2017-04-09 12:32:51 +0200 | [diff] [blame] | 316 | a_iface_tx_clear_cmd(conn); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 317 | break; |
| 318 | default: |
| 319 | LOGP(DMM, LOGL_ERROR, "%s: Unknown RAN type, cannot tx release/clear\n", |
| 320 | vlr_subscr_name(conn->vsub)); |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | |
Neels Hofmeyr | 4d3a66b | 2018-03-31 18:45:59 +0200 | [diff] [blame] | 325 | /* If the conn->fi is still present, dispatch SUBSCR_CONN_E_CN_CLOSE |
| 326 | * event to gracefully terminate the connection. If the fi is already |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 327 | * cleared, call msc_subscr_conn_release_all() to take release actions. |
| 328 | * \param cause a GSM_CAUSE_* constant, e.g. GSM_CAUSE_AUTH_FAILED. |
| 329 | */ |
| 330 | void msc_subscr_conn_close(struct gsm_subscriber_connection *conn, |
| 331 | uint32_t cause) |
| 332 | { |
| 333 | if (!conn) |
Holger Hans Peter Freyther | 4049455 | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 334 | return; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 335 | if (conn->in_release) { |
| 336 | DEBUGP(DMM, "msc_subscr_conn_close(vsub=%s, cause=%u):" |
| 337 | " already dispatching release, ignore.\n", |
| 338 | vlr_subscr_name(conn->vsub), cause); |
Holger Hans Peter Freyther | 4049455 | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 339 | return; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 340 | } |
Neels Hofmeyr | 4d3a66b | 2018-03-31 18:45:59 +0200 | [diff] [blame] | 341 | if (!conn->fi) { |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 342 | DEBUGP(DMM, "msc_subscr_conn_close(vsub=%s, cause=%u): no conn fsm," |
| 343 | " releasing directly without release event.\n", |
| 344 | vlr_subscr_name(conn->vsub), cause); |
Neels Hofmeyr | 4d3a66b | 2018-03-31 18:45:59 +0200 | [diff] [blame] | 345 | /* In case of an IMSI Detach, we don't have fi. Release |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 346 | * anyway to ensure a timely Iu Release / BSSMAP Clear. */ |
| 347 | msc_subscr_conn_release_all(conn, cause); |
Holger Hans Peter Freyther | 70ae5d3 | 2012-11-23 21:33:15 +0100 | [diff] [blame] | 348 | return; |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 349 | } |
Neels Hofmeyr | 4d3a66b | 2018-03-31 18:45:59 +0200 | [diff] [blame] | 350 | if (conn->fi->state == SUBSCR_CONN_S_RELEASED) { |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 351 | DEBUGP(DMM, "msc_subscr_conn_close(vsub=%s, cause=%u):" |
| 352 | " conn fsm already releasing, ignore.\n", |
| 353 | vlr_subscr_name(conn->vsub), cause); |
| 354 | return; |
| 355 | } |
Neels Hofmeyr | 4d3a66b | 2018-03-31 18:45:59 +0200 | [diff] [blame] | 356 | osmo_fsm_inst_dispatch(conn->fi, SUBSCR_CONN_E_CN_CLOSE, &cause); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 357 | } |
Holger Hans Peter Freyther | 4049455 | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 358 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 359 | /* increment the ref-count. Needs to be called by every user */ |
| 360 | struct gsm_subscriber_connection * |
| 361 | _msc_subscr_conn_get(struct gsm_subscriber_connection *conn, |
Neels Hofmeyr | 6166f29 | 2017-11-22 14:33:12 +0100 | [diff] [blame] | 362 | enum msc_subscr_conn_use balance_token, |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 363 | const char *file, int line) |
| 364 | { |
| 365 | OSMO_ASSERT(conn); |
Holger Hans Peter Freyther | e7bd863 | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 366 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 367 | if (conn->in_release) |
Neels Hofmeyr | 785fadc | 2017-11-22 14:44:35 +0100 | [diff] [blame] | 368 | LOGPSRC(DREF, LOGL_ERROR, file, line, |
| 369 | "%s: MSC conn use error: using conn that is already in release (%s)\n", |
| 370 | vlr_subscr_name(conn->vsub), |
| 371 | msc_subscr_conn_use_name(balance_token)); |
Holger Hans Peter Freyther | e7bd863 | 2013-06-30 15:30:47 +0200 | [diff] [blame] | 372 | |
Neels Hofmeyr | 6166f29 | 2017-11-22 14:33:12 +0100 | [diff] [blame] | 373 | if (balance_token != MSC_CONN_USE_UNTRACKED) { |
| 374 | uint32_t flag = 1 << balance_token; |
| 375 | OSMO_ASSERT(balance_token < 32); |
| 376 | if (conn->use_tokens & flag) |
| 377 | LOGPSRC(DREF, LOGL_ERROR, file, line, |
| 378 | "%s: MSC conn use error: using an already used token: %s\n", |
| 379 | vlr_subscr_name(conn->vsub), |
| 380 | msc_subscr_conn_use_name(balance_token)); |
| 381 | conn->use_tokens |= flag; |
| 382 | } |
| 383 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 384 | conn->use_count++; |
| 385 | LOGPSRC(DREF, LOGL_DEBUG, file, line, |
Neels Hofmeyr | 6166f29 | 2017-11-22 14:33:12 +0100 | [diff] [blame] | 386 | "%s: MSC conn use + %s == %u (0x%x)\n", |
| 387 | vlr_subscr_name(conn->vsub), msc_subscr_conn_use_name(balance_token), |
| 388 | conn->use_count, conn->use_tokens); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 389 | |
| 390 | return conn; |
| 391 | } |
| 392 | |
| 393 | /* decrement the ref-count. Once it reaches zero, we release */ |
| 394 | void _msc_subscr_conn_put(struct gsm_subscriber_connection *conn, |
Neels Hofmeyr | 6166f29 | 2017-11-22 14:33:12 +0100 | [diff] [blame] | 395 | enum msc_subscr_conn_use balance_token, |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 396 | const char *file, int line) |
| 397 | { |
| 398 | OSMO_ASSERT(conn); |
| 399 | |
Neels Hofmeyr | 6166f29 | 2017-11-22 14:33:12 +0100 | [diff] [blame] | 400 | if (balance_token != MSC_CONN_USE_UNTRACKED) { |
| 401 | uint32_t flag = 1 << balance_token; |
| 402 | OSMO_ASSERT(balance_token < 32); |
| 403 | if (!(conn->use_tokens & flag)) |
| 404 | LOGPSRC(DREF, LOGL_ERROR, file, line, |
| 405 | "%s: MSC conn use error: freeing an unused token: %s\n", |
| 406 | vlr_subscr_name(conn->vsub), |
| 407 | msc_subscr_conn_use_name(balance_token)); |
| 408 | conn->use_tokens &= ~flag; |
| 409 | } |
| 410 | |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 411 | if (conn->use_count == 0) { |
| 412 | LOGPSRC(DREF, LOGL_ERROR, file, line, |
Neels Hofmeyr | 6166f29 | 2017-11-22 14:33:12 +0100 | [diff] [blame] | 413 | "%s: MSC conn use - %s failed: is already 0\n", |
| 414 | vlr_subscr_name(conn->vsub), |
| 415 | msc_subscr_conn_use_name(balance_token)); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 416 | return; |
| 417 | } |
| 418 | |
| 419 | conn->use_count--; |
| 420 | LOGPSRC(DREF, LOGL_DEBUG, file, line, |
Neels Hofmeyr | 6166f29 | 2017-11-22 14:33:12 +0100 | [diff] [blame] | 421 | "%s: MSC conn use - %s == %u (0x%x)\n", |
| 422 | vlr_subscr_name(conn->vsub), msc_subscr_conn_use_name(balance_token), |
| 423 | conn->use_count, conn->use_tokens); |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 424 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 425 | if (conn->use_count == 0) |
| 426 | msc_subscr_con_free(conn); |
| 427 | } |
| 428 | |
Neels Hofmeyr | 6166f29 | 2017-11-22 14:33:12 +0100 | [diff] [blame] | 429 | const struct value_string msc_subscr_conn_use_names[] = { |
| 430 | {MSC_CONN_USE_UNTRACKED, "UNTRACKED"}, |
| 431 | {MSC_CONN_USE_COMPL_L3, "compl_l3"}, |
| 432 | {MSC_CONN_USE_DTAP, "dtap"}, |
| 433 | {MSC_CONN_USE_FSM, "fsm"}, |
| 434 | {MSC_CONN_USE_TRANS_CC, "trans_cc"}, |
| 435 | {MSC_CONN_USE_TRANS_SMS, "trans_sms"}, |
| 436 | {MSC_CONN_USE_TRANS_USSD, "trans_ussd"}, |
| 437 | {MSC_CONN_USE_SILENT_CALL, "silent_call"}, |
| 438 | {0, NULL}, |
| 439 | }; |
| 440 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 441 | void msc_stop_paging(struct vlr_subscr *vsub) |
| 442 | { |
| 443 | DEBUGP(DPAG, "Paging can stop for %s\n", vlr_subscr_name(vsub)); |
| 444 | /* tell BSCs and RNCs to stop paging? How? */ |
Holger Hans Peter Freyther | 4049455 | 2010-06-28 17:09:29 +0800 | [diff] [blame] | 445 | } |