blob: 9ccc5d58cda8b90eef3477dd263f52a93f565b61 [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Welte2483f1b2016-06-19 18:06:02 +02004/* (C) 2008-2016 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01005 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * 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
Harald Welte52b1f982008-12-23 20:25:15 +000012 * (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.
Harald Welte52b1f982008-12-23 20:25:15 +000018 *
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/>.
Harald Welte52b1f982008-12-23 20:25:15 +000021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Maxddee01f2016-05-24 14:23:27 +020028#include <stdbool.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Maxddee01f2016-05-24 14:23:27 +020032#include <regex.h>
33#include <sys/types.h>
Harald Welte52b1f982008-12-23 20:25:15 +000034
Harald Welte9c3dc902012-04-08 16:59:24 +020035#include "bscconfig.h"
36
Neels Hofmeyr90843962017-09-04 15:04:35 +020037#include <osmocom/msc/db.h>
38#include <osmocom/msc/debug.h>
39#include <osmocom/msc/gsm_data.h>
40#include <osmocom/msc/gsm_subscriber.h>
41#include <osmocom/msc/gsm_04_11.h>
42#include <osmocom/msc/gsm_04_08.h>
43#include <osmocom/msc/gsm_04_80.h>
44#include <osmocom/msc/gsm_04_14.h>
Vadim Yanitskiy5b860fa2018-06-12 05:24:52 +070045#include <osmocom/msc/gsm_09_11.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020046#include <osmocom/msc/signal.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020047#include <osmocom/msc/transaction.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020048#include <osmocom/msc/silent_call.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020049#include <osmocom/msc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020050#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010051#include <osmocom/core/bitvec.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020052#include <osmocom/msc/vlr.h>
53#include <osmocom/msc/msc_ifaces.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080054
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010055#include <osmocom/gsm/gsm48.h>
56#include <osmocom/gsm/gsm0480.h>
57#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020058#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010059#include <osmocom/core/msgb.h>
60#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010061#include <osmocom/core/utils.h>
Philipp Maier621ba032017-11-07 17:19:25 +010062#include <osmocom/core/byteswap.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010063#include <osmocom/gsm/tlv.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020064#include <osmocom/crypt/auth.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020065#ifdef BUILD_IU
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020066#include <osmocom/ranap/iu_client.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020067#endif
Harald Welte52b1f982008-12-23 20:25:15 +000068
Neels Hofmeyr90843962017-09-04 15:04:35 +020069#include <osmocom/msc/msc_ifaces.h>
70#include <osmocom/msc/a_iface.h>
Philipp Maier621ba032017-11-07 17:19:25 +010071#include <osmocom/msc/msc_mgcp.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020072
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020073#include <assert.h>
74
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020075
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020076void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010077void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020078
Neels Hofmeyrc036b792018-11-29 22:37:51 +010079static int gsm0408_loc_upd_acc(struct ran_conn *conn,
Harald Welte2483f1b2016-06-19 18:06:02 +020080 uint32_t send_tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +000081
Harald Welte27989d42018-06-21 20:39:20 +020082/*! Send a simple GSM 04.08 message without any payload
Neels Hofmeyrc036b792018-11-29 22:37:51 +010083 * \param conn Active RAN connection
Harald Welte27989d42018-06-21 20:39:20 +020084 * \param[in] pdisc Protocol discriminator
85 * \param[in] msg_type Message type
86 * \return result of \ref gsm48_conn_sendmsg
87 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +010088int gsm48_tx_simple(struct ran_conn *conn,
Harald Welte27989d42018-06-21 20:39:20 +020089 uint8_t pdisc, uint8_t msg_type)
90{
91 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
92 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
93
94 gh->proto_discr = pdisc;
95 gh->msg_type = msg_type;
96
97 return gsm48_conn_sendmsg(msg, conn, NULL);
98}
Harald Welte4bfdfe72009-06-10 23:11:52 +080099
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200100static bool classmark1_is_r99(const struct gsm48_classmark1 *cm1)
101{
102 return cm1->rev_lev >= 2;
103}
104
105static bool classmark2_is_r99(const uint8_t *cm2, uint8_t cm2_len)
106{
107 uint8_t rev_lev;
108 if (!cm2_len)
109 return false;
110 rev_lev = (cm2[0] >> 5) & 0x3;
111 return rev_lev >= 2;
112}
113
Neels Hofmeyr9d744252018-03-22 16:09:50 +0100114static bool classmark_is_r99(struct gsm_classmark *cm)
115{
Neels Hofmeyr9d744252018-03-22 16:09:50 +0100116 if (cm->classmark1_set)
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200117 return classmark1_is_r99(&cm->classmark1);
118 return classmark2_is_r99(cm->classmark2, cm->classmark2_len);
Neels Hofmeyr9d744252018-03-22 16:09:50 +0100119}
120
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200121/* Determine if the given CLASSMARK (1/2/3) value permits a given A5/n cipher.
122 * Return 1 when the given A5/n is permitted, 0 when not, and negative if the respective MS CLASSMARK is
123 * not known, where the negative number indicates the classmark type: -2 means Classmark 2 is not
124 * available. */
125static int classmark_supports_a5(const struct gsm_classmark *cm, uint8_t a5)
Harald Welte71330722017-12-23 19:59:02 +0100126{
127 switch (a5) {
128 case 0:
129 /* all phones must implement A5/0, see 3GPP TS 43.020 4.9 */
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200130 return 1;
Harald Welte71330722017-12-23 19:59:02 +0100131 case 1:
132 /* 3GPP TS 43.020 4.9 requires A5/1 to be suppored by all phones and actually states:
133 * "The network shall not provide service to an MS which indicates that it does not
134 * support the ciphering algorithm A5/1.". However, let's be more tolerant based
135 * on policy here */
136 /* See 3GPP TS 24.008 10.5.1.7 */
137 if (!cm->classmark1_set) {
138 DEBUGP(DMSC, "CLASSMARK 1 unknown, assuming MS supports A5/1\n");
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200139 return -1;
Harald Welte71330722017-12-23 19:59:02 +0100140 } else {
141 if (cm->classmark1.a5_1)
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200142 return 0; /* Inverted logic for this bit! */
Harald Welte71330722017-12-23 19:59:02 +0100143 else
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200144 return 1;
Harald Welte71330722017-12-23 19:59:02 +0100145 }
146 break;
147 case 2:
148 case 3:
149 /* See 3GPP TS 24.008 10.5.1.6 */
150 if (cm->classmark2_len < 3) {
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200151 return -2;
Harald Welte71330722017-12-23 19:59:02 +0100152 } else {
153 if (cm->classmark2[2] & (1 << (a5-2)))
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200154 return 1;
Harald Welte71330722017-12-23 19:59:02 +0100155 else
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200156 return 0;
Harald Welte71330722017-12-23 19:59:02 +0100157 }
158 break;
159 case 4:
160 case 5:
161 case 6:
162 case 7:
163 /* See 3GPP TS 24.008 10.5.1.7 */
164 if (cm->classmark3_len < 1) {
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200165 return -3;
Harald Welte71330722017-12-23 19:59:02 +0100166 } else {
167 if (cm->classmark3[0] & (1 << (a5-4)))
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200168 return 1;
Harald Welte71330722017-12-23 19:59:02 +0100169 else
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200170 return 0;
Harald Welte71330722017-12-23 19:59:02 +0100171 }
172 break;
173 default:
174 return false;
175 }
176}
177
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100178int gsm48_conn_sendmsg(struct msgb *msg, struct ran_conn *conn, struct gsm_trans *trans)
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800179{
180 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
181
182 /* if we get passed a transaction reference, do some common
183 * work that the caller no longer has to do */
184 if (trans) {
185 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
Harald Welte0e2fa5d2018-04-09 16:35:01 +0200186 OMSC_LINKID_CB(msg) = trans->dlci;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800187 }
188
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200189 return msc_tx_dtap(conn, msg);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800190}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100191
Harald Welte2483f1b2016-06-19 18:06:02 +0200192/* clear all transactions globally; used in case of MNCC socket disconnect */
Harald Welte371efe52010-12-22 23:17:50 +0100193void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
194{
195 struct gsm_trans *trans, *temp;
196
197 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
198
199 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100200 if (trans->protocol == protocol) {
201 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100202 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100203 }
Harald Welte371efe52010-12-22 23:17:50 +0100204 }
205}
206
Holger Freyther429e7762008-12-30 13:28:30 +0000207/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100208static int gsm0408_loc_upd_rej(struct ran_conn *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000209{
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800210 struct msgb *msg;
211
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800212 msg = gsm48_create_loc_upd_rej(cause);
213 if (!msg) {
214 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
215 return -1;
216 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200217
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200218 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT\n",
219 vlr_subscr_name(conn->vsub));
Harald Welte24ff6ee2009-12-22 00:41:05 +0100220
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800221 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000222}
223
224/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100225static int gsm0408_loc_upd_acc(struct ran_conn *conn,
Harald Welte2483f1b2016-06-19 18:06:02 +0200226 uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000227{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100228 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000229 struct gsm48_hdr *gh;
230 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200231 uint8_t *mid;
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100232 struct osmo_location_area_id laid = {
233 .plmn = conn->network->plmn,
234 .lac = conn->lac,
235 };
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200236
Harald Welte52b1f982008-12-23 20:25:15 +0000237 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
238 gh->proto_discr = GSM48_PDISC_MM;
239 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
240
241 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100242 gsm48_generate_lai2(lai, &laid);
Harald Welte52b1f982008-12-23 20:25:15 +0000243
Harald Welte2483f1b2016-06-19 18:06:02 +0200244 if (send_tmsi == GSM_RESERVED_TMSI) {
245 /* we did not allocate a TMSI to the MS, so we need to
246 * include the IMSI in order for the MS to delete any
247 * old TMSI that might still be allocated */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200248 uint8_t mi[10];
249 int len;
Harald Welte2483f1b2016-06-19 18:06:02 +0200250 len = gsm48_generate_mid_from_imsi(mi, conn->vsub->imsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200251 mid = msgb_put(msg, len);
252 memcpy(mid, mi, len);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200253 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT\n",
254 vlr_subscr_name(conn->vsub));
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200255 } else {
Harald Welte2483f1b2016-06-19 18:06:02 +0200256 /* Include the TMSI, which means that the MS will send a
257 * TMSI REALLOCATION COMPLETE, and we should wait for
258 * that until T3250 expiration */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200259 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Welte2483f1b2016-06-19 18:06:02 +0200260 gsm48_generate_mid_from_tmsi(mid, send_tmsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200261 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT (TMSI = 0x%08x)\n",
262 vlr_subscr_name(conn->vsub),
263 send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200264 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200265 /* TODO: Follow-on proceed */
266 /* TODO: CTS permission */
267 /* TODO: Equivalent PLMNs */
268 /* TODO: Emergency Number List */
269 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000270
Harald Welte52b1f982008-12-23 20:25:15 +0000271
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800272 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000273}
274
Harald Weltebf5e8df2009-02-03 12:59:45 +0000275/* Transmit Chapter 9.2.10 Identity Request */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100276static int mm_tx_identity_req(struct ran_conn *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000277{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100278 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000279 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000280
Harald Welte231ad4f2008-12-27 11:15:38 +0000281 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
282 gh->proto_discr = GSM48_PDISC_MM;
283 gh->msg_type = GSM48_MT_MM_ID_REQ;
284 gh->data[0] = id_type;
285
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800286 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000287}
288
Harald Weltebf5e8df2009-02-03 12:59:45 +0000289/* Parse Chapter 9.2.11 Identity Response */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100290static int mm_rx_id_resp(struct ran_conn *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000291{
292 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200293 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200294 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000295
Harald Welte2483f1b2016-06-19 18:06:02 +0200296 if (!conn->vsub) {
297 LOGP(DMM, LOGL_ERROR,
298 "Rx MM Identity Response: invalid: no subscriber\n");
299 return -EINVAL;
300 }
301
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200302 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200303 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
304 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000305
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200306 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100307
Harald Welte2483f1b2016-06-19 18:06:02 +0200308 return vlr_subscr_rx_id_resp(conn->vsub, gh->data+1, gh->data[0]);
Harald Welte231ad4f2008-12-27 11:15:38 +0000309}
310
Harald Welte2483f1b2016-06-19 18:06:02 +0200311/* Chapter 9.2.15: Receive Location Updating Request.
312 * Keep this function non-static for direct invocation by unit tests. */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100313int mm_rx_loc_upd_req(struct ran_conn *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000314{
Harald Welte2483f1b2016-06-19 18:06:02 +0200315 struct gsm_network *net = conn->network;
Harald Welte8470bf22008-12-25 23:28:35 +0000316 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000317 struct gsm48_loc_upd_req *lu;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200318 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200319 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200320 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte2483f1b2016-06-19 18:06:02 +0200321 uint32_t tmsi;
322 char *imsi;
323 struct osmo_location_area_id old_lai, new_lai;
324 struct osmo_fsm_inst *lu_fsm;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200325 bool is_utran;
Harald Welte52b1f982008-12-23 20:25:15 +0000326
Harald Welte8470bf22008-12-25 23:28:35 +0000327 lu = (struct gsm48_loc_upd_req *) gh->data;
328
329 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000330
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200331 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000332
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100333 if (ran_conn_is_establishing_auth_ciph(conn)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200334 LOGP(DMM, LOGL_ERROR,
335 "Cannot accept another LU, conn already busy establishing authenticity;"
336 " extraneous LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
Neels Hofmeyr55d22d92019-01-11 01:38:54 +0100337 gsm48_mi_type_name(mi_type), mi_string, osmo_lu_type_name(lu->type));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200338 return -EINVAL;
339 }
340
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100341 if (ran_conn_is_accepted(conn)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200342 LOGP(DMM, LOGL_ERROR,
343 "Cannot accept another LU, conn already established;"
344 " extraneous LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
Neels Hofmeyr55d22d92019-01-11 01:38:54 +0100345 gsm48_mi_type_name(mi_type), mi_string, osmo_lu_type_name(lu->type));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200346 return -EINVAL;
347 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200348
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100349 ran_conn_update_id(conn, COMPLETE_LAYER3_LU, mi_string);
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200350
Harald Welte2483f1b2016-06-19 18:06:02 +0200351 DEBUGP(DMM, "LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
Neels Hofmeyr55d22d92019-01-11 01:38:54 +0100352 gsm48_mi_type_name(mi_type), mi_string, osmo_lu_type_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000353
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200354 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100355
Harald Welte24ff6ee2009-12-22 00:41:05 +0100356 switch (lu->type) {
357 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200358 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200359 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100360 break;
361 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200362 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200363 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100364 break;
365 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200366 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200367 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100368 break;
369 }
370
Harald Welte2483f1b2016-06-19 18:06:02 +0200371 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
372 /* TODO: 10.5.3.14 Aditional update parameters (CS fallback calls) */
373 /* TODO: 10.5.7.8 Device properties */
374 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200375
Harald Welte52b1f982008-12-23 20:25:15 +0000376 switch (mi_type) {
377 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200378 tmsi = GSM_RESERVED_TMSI;
379 imsi = mi_string;
Harald Welte4b634542008-12-27 01:55:51 +0000380 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000381 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200382 tmsi = tmsi_from_string(mi_string);
383 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000384 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200385 default:
Harald Weltea0368542009-06-27 02:58:43 +0200386 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200387 tmsi = GSM_RESERVED_TMSI;
388 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000389 break;
390 }
391
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100392 gsm48_decode_lai2(&lu->lai, &old_lai);
393 new_lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200394 new_lai.lac = conn->lac;
Harald Welte2483f1b2016-06-19 18:06:02 +0200395 DEBUGP(DMM, "LU/new-LAC: %u/%u\n", old_lai.lac, new_lai.lac);
Harald Welte24516ea2009-07-04 10:18:00 +0200396
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100397 is_utran = (conn->via_ran == OSMO_RAT_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200398 lu_fsm = vlr_loc_update(conn->fi,
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100399 RAN_CONN_E_ACCEPTED, RAN_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +0200400 net->vlr, conn, vlr_lu_type, tmsi, imsi,
401 &old_lai, &new_lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200402 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100403 is_utran || conn->network->a5_encryption_mask > 0x01,
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200404 classmark1_is_r99(&lu->classmark1),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200405 is_utran,
Harald Welte2483f1b2016-06-19 18:06:02 +0200406 net->vlr->cfg.assign_tmsi);
407 if (!lu_fsm) {
408 DEBUGP(DRR, "%s: Can't start LU FSM\n", mi_string);
409 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800410 }
411
Harald Welte2483f1b2016-06-19 18:06:02 +0200412 /* From vlr_loc_update() we expect an implicit dispatch of
413 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
414 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000415
Harald Welte2483f1b2016-06-19 18:06:02 +0200416 if (!conn->vsub || conn->vsub->lu_fsm != lu_fsm) {
417 LOGP(DRR, LOGL_ERROR,
418 "%s: internal error during Location Updating attempt\n",
419 mi_string);
420 return -EIO;
421 }
422
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200423 conn->vsub->classmark.classmark1 = lu->classmark1;
424 conn->vsub->classmark.classmark1_set = true;
425
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100426 ran_conn_complete_layer_3(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +0200427 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000428}
429
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100430/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200431/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100432static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800433{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100434 uint8_t ret;
435
436 ret = value / 10;
437 ret |= (value % 10) << 4;
438
439 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800440}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100441
Harald Welte4bfdfe72009-06-10 23:11:52 +0800442
Harald Weltedb253af2008-12-30 17:56:55 +0000443/* Section 9.2.15a */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100444int gsm48_tx_mm_info(struct ran_conn *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000445{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100446 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000447 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200448 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200449 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200450 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100451
Harald Welte4bfdfe72009-06-10 23:11:52 +0800452 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100453 struct tm* gmt_time;
454 struct tm* local_time;
455 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100456 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000457
Harald Weltedb253af2008-12-30 17:56:55 +0000458 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
459 gh->proto_discr = GSM48_PDISC_MM;
460 gh->msg_type = GSM48_MT_MM_INFO;
461
462 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200463#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000464 name_len = strlen(net->name_long);
465 /* 10.5.3.5a */
466 ptr8 = msgb_put(msg, 3);
467 ptr8[0] = GSM48_IE_NAME_LONG;
468 ptr8[1] = name_len*2 +1;
469 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
470
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200471 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000472 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000473 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000474
475 /* FIXME: Use Cell Broadcast, not UCS-2, since
476 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200477#endif
478 name_len = (strlen(net->name_long)*7)/8;
479 name_pad = (8 - strlen(net->name_long)*7)%8;
480 if (name_pad > 0)
481 name_len++;
482 /* 10.5.3.5a */
483 ptr8 = msgb_put(msg, 3);
484 ptr8[0] = GSM48_IE_NAME_LONG;
485 ptr8[1] = name_len +1;
486 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
487
488 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100489 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200490
Harald Weltedb253af2008-12-30 17:56:55 +0000491 }
492
493 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200494#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000495 name_len = strlen(net->name_short);
496 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200497 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200498 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000499 ptr8[1] = name_len*2 + 1;
500 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
501
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200502 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000503 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000504 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200505#endif
506 name_len = (strlen(net->name_short)*7)/8;
507 name_pad = (8 - strlen(net->name_short)*7)%8;
508 if (name_pad > 0)
509 name_len++;
510 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200511 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200512 ptr8[0] = GSM48_IE_NAME_SHORT;
513 ptr8[1] = name_len +1;
514 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
515
516 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100517 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200518
Harald Weltedb253af2008-12-30 17:56:55 +0000519 }
520
Harald Weltedb253af2008-12-30 17:56:55 +0000521 /* Section 10.5.3.9 */
522 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100523 gmt_time = gmtime(&cur_t);
524
Harald Weltedb253af2008-12-30 17:56:55 +0000525 ptr8 = msgb_put(msg, 8);
526 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100527 ptr8[1] = bcdify(gmt_time->tm_year % 100);
528 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
529 ptr8[3] = bcdify(gmt_time->tm_mday);
530 ptr8[4] = bcdify(gmt_time->tm_hour);
531 ptr8[5] = bcdify(gmt_time->tm_min);
532 ptr8[6] = bcdify(gmt_time->tm_sec);
533
Neels Hofmeyr73983952016-05-10 13:29:33 +0200534 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200535 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200536 if (net->tz.hr < 0) {
537 tzunits = ((net->tz.hr/-1)*4);
538 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100539 ptr8[7] = bcdify(tzunits);
540 /* Set negative time */
541 ptr8[7] |= 0x08;
542 }
543 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200544 tzunits = net->tz.hr*4;
545 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100546 ptr8[7] = bcdify(tzunits);
547 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100548 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200549 if (net->tz.dst >= 0 && net->tz.dst <= 2)
550 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100551 }
552 else {
553 /* Need to get GSM offset and convert into 15 min units */
554 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
Harald Welte9c3dc902012-04-08 16:59:24 +0200555#ifdef HAVE_TM_GMTOFF_IN_TM
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200556 local_time = localtime(&cur_t);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100557 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200558#else
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200559 /* find timezone offset */
560 time_t utc;
561 double offsetFromUTC;
562 utc = mktime(gmt_time);
563 local_time = localtime(&cur_t);
564 offsetFromUTC = difftime(cur_t, utc);
565 if (local_time->tm_isdst)
566 offsetFromUTC += 3600.0;
567 tzunits = ((int)offsetFromUTC) / 60 / 15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200568#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100569 if (tzunits < 0) {
570 tzunits = tzunits/-1;
571 ptr8[7] = bcdify(tzunits);
572 /* Flip it to negative */
573 ptr8[7] |= 0x08;
574 }
575 else
576 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100577
578 /* Does not support DST +2 */
579 if (local_time->tm_isdst)
580 dst = 1;
581 }
582
583 if (dst) {
584 ptr8 = msgb_put(msg, 3);
585 ptr8[0] = GSM48_IE_NET_DST;
586 ptr8[1] = 1;
587 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100588 }
Harald Weltedb253af2008-12-30 17:56:55 +0000589
Daniel Willmanneea93372009-08-13 03:42:07 +0200590 DEBUGP(DMM, "-> MM INFO\n");
591
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800592 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000593}
594
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100595/*! Send an Authentication Request to MS on the given RAN connection
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100596 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
597 * \param[in] conn Subscriber connection to send on.
598 * \param[in] rand Random challenge token to send, must be 16 bytes long.
599 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
600 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
601 * \param[in] key_seq auth tuple's sequence number.
602 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100603int gsm48_tx_mm_auth_req(struct ran_conn *conn, uint8_t *rand,
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100604 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200605{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100606 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200607 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200608 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200609
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100610 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100611 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100612 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200613
Harald Welte7984d5c2009-08-12 22:56:50 +0200614 gh->proto_discr = GSM48_PDISC_MM;
615 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
616
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100617 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200618
Harald Welte7984d5c2009-08-12 22:56:50 +0200619 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100620 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200621 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200622 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200623
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100624
625 /* 16 bytes AUTN */
626 if (autn)
627 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
628
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800629 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200630}
631
632/* Section 9.2.1 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100633int gsm48_tx_mm_auth_rej(struct ran_conn *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200634{
635 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800636 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200637}
638
Harald Welte2483f1b2016-06-19 18:06:02 +0200639static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200640static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum gsm48_reject_value result);
Harald Welte2483f1b2016-06-19 18:06:02 +0200641
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100642static int cm_serv_reuse_conn(struct ran_conn *conn, const uint8_t *mi_lv)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100643{
Harald Welte2483f1b2016-06-19 18:06:02 +0200644 uint8_t mi_type;
645 char mi_string[GSM48_MI_SIZE];
646 uint32_t tmsi;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100647
Harald Welte2483f1b2016-06-19 18:06:02 +0200648 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
649 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100650
Harald Welte2483f1b2016-06-19 18:06:02 +0200651 switch (mi_type) {
652 case GSM_MI_TYPE_IMSI:
653 if (vlr_subscr_matches_imsi(conn->vsub, mi_string))
654 goto accept_reuse;
655 break;
656 case GSM_MI_TYPE_TMSI:
657 tmsi = osmo_load32be(mi_lv+2);
658 if (vlr_subscr_matches_tmsi(conn->vsub, tmsi))
659 goto accept_reuse;
660 break;
661 case GSM_MI_TYPE_IMEI:
662 if (vlr_subscr_matches_imei(conn->vsub, mi_string))
663 goto accept_reuse;
664 break;
665 default:
666 break;
667 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100668
Harald Welte2483f1b2016-06-19 18:06:02 +0200669 LOGP(DMM, LOGL_ERROR, "%s: CM Service Request with mismatching mobile identity: %s %s\n",
670 vlr_subscr_name(conn->vsub), gsm48_mi_type_name(mi_type), mi_string);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200671 msc_vlr_tx_cm_serv_rej(conn, GSM48_REJECT_ILLEGAL_MS);
Harald Welte2483f1b2016-06-19 18:06:02 +0200672 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100673
Harald Welte2483f1b2016-06-19 18:06:02 +0200674accept_reuse:
675 DEBUGP(DMM, "%s: re-using already accepted connection\n",
676 vlr_subscr_name(conn->vsub));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200677
678 if (!conn->received_cm_service_request) {
679 conn->received_cm_service_request = true;
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100680 ran_conn_get(conn, RAN_CONN_USE_CM_SERVICE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200681 }
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100682 ran_conn_update_id(conn, conn->complete_layer3_type, mi_string);
Harald Welte2483f1b2016-06-19 18:06:02 +0200683 return conn->network->vlr->ops.tx_cm_serv_acc(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100684}
685
Harald Welte4ed0e922009-01-10 03:17:30 +0000686/*
687 * Handle CM Service Requests
688 * a) Verify that the packet is long enough to contain the information
689 * we require otherwsie reject with INCORRECT_MESSAGE
690 * b) Try to parse the TMSI. If we do not have one reject
691 * c) Check that we know the subscriber with the TMSI otherwise reject
692 * with a HLR cause
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200693 * d) Set the subscriber on the conn and accept
Harald Welte2483f1b2016-06-19 18:06:02 +0200694 *
695 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000696 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100697int gsm48_rx_mm_serv_req(struct ran_conn *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000698{
Harald Welte2483f1b2016-06-19 18:06:02 +0200699 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200700 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200701 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000702
Harald Welteba4cf162009-01-10 01:49:35 +0000703 struct gsm48_hdr *gh = msgb_l3(msg);
704 struct gsm48_service_request *req =
705 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800706 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200707 uint8_t classmark2_len = gh->data[1];
708 uint8_t *classmark2 = gh->data+2;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200709 uint8_t *mi_p = classmark2 + classmark2_len;
710 uint8_t mi_len = *mi_p;
711 uint8_t *mi = mi_p + 1;
Harald Welte2483f1b2016-06-19 18:06:02 +0200712 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200713 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200714
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100715 lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200716 lai.lac = conn->lac;
Harald Welteba4cf162009-01-10 01:49:35 +0000717
718 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200719 LOGP(DMM, LOGL_ERROR, "<- CM SERVICE REQUEST wrong sized message\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200720 return msc_gsm48_tx_mm_serv_rej(conn,
721 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000722 }
723
724 if (msg->data_len < req->mi_len + 6) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200725 LOGP(DMM, LOGL_ERROR, "<- CM SERVICE REQUEST does not fit in packet\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200726 return msc_gsm48_tx_mm_serv_rej(conn,
727 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000728 }
729
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200730 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000731 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200732 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x MI(%s)=%s\n",
733 req->cm_service_type, gsm48_mi_type_name(mi_type), mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200734
Harald Welte3995d2e2018-01-24 11:23:54 +0100735 switch (mi_type) {
736 case GSM_MI_TYPE_IMSI:
737 case GSM_MI_TYPE_TMSI:
738 /* continue below */
739 break;
740 case GSM_MI_TYPE_IMEI:
741 if (req->cm_service_type == GSM48_CMSERV_EMERGENCY) {
742 /* We don't do emergency calls by IMEI */
743 LOGP(DMM, LOGL_NOTICE, "<- CM SERVICE REQUEST(IMEI=%s) rejected\n", mi_string);
744 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_IMEI_NOT_ACCEPTED);
745 }
746 /* fall-through for non-emergency setup */
747 default:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200748 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200749 return msc_gsm48_tx_mm_serv_rej(conn,
750 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000751 }
752
Harald Welte37382ec2018-01-24 12:49:34 +0100753 switch (req->cm_service_type) {
754 case GSM48_CMSERV_MO_CALL_PACKET:
755 case GSM48_CMSERV_EMERGENCY:
756 case GSM48_CMSERV_SMS:
757 case GSM48_CMSERV_SUP_SERV:
758 /* continue below */
759 break;
760 default:
761 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
762 }
763
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100764 if (ran_conn_is_accepted(conn))
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200765 return cm_serv_reuse_conn(conn, mi_p);
Harald Welte2483f1b2016-06-19 18:06:02 +0200766
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100767 if (ran_conn_is_establishing_auth_ciph(conn)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200768 LOGP(DMM, LOGL_ERROR,
769 "Cannot accept CM Service Request, conn already busy establishing authenticity\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200770 msc_vlr_tx_cm_serv_rej(conn, GSM48_REJECT_CONGESTION);
Harald Welte2483f1b2016-06-19 18:06:02 +0200771 return -EINVAL;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200772 /* or should we accept and note down the service request anyway? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200773 }
774
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100775 ran_conn_update_id(conn, COMPLETE_LAYER3_CM_SERVICE_REQ, mi_string);
Harald Welte7659de12009-12-13 12:39:18 +0100776
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200777 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, mi_p);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200778
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100779 is_utran = (conn->via_ran == OSMO_RAT_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200780 vlr_proc_acc_req(conn->fi,
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100781 RAN_CONN_E_ACCEPTED, RAN_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +0200782 net->vlr, conn,
783 VLR_PR_ARQ_T_CM_SERV_REQ, mi-1, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200784 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100785 is_utran || conn->network->a5_encryption_mask > 0x01,
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200786 classmark2_is_r99(classmark2, classmark2_len),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200787 is_utran);
Holger Freythereb443982009-06-04 13:58:42 +0000788
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200789 /* From vlr_proc_acc_req() we expect an implicit dispatch of PR_ARQ_E_START we expect
790 * msc_vlr_subscr_assoc() to already have been called and completed. Has an error occured? */
791 if (!conn->vsub) {
792 LOGP(DRR, LOGL_ERROR, "%s: subscriber not allowed to do a CM Service Request\n",
793 mi_string);
794 return -EIO;
795 }
796
797 memcpy(conn->vsub->classmark.classmark2, classmark2, classmark2_len);
798 conn->vsub->classmark.classmark2_len = classmark2_len;
799
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100800 ran_conn_complete_layer_3(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +0200801 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000802}
803
Harald Welte49f8fcb2018-01-24 21:40:19 +0100804/* Receive a CM Re-establish Request */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100805static int gsm48_rx_cm_reest_req(struct ran_conn *conn, struct msgb *msg)
Harald Welte49f8fcb2018-01-24 21:40:19 +0100806{
807 uint8_t mi_type;
808 char mi_string[GSM48_MI_SIZE];
809 struct gsm48_hdr *gh = msgb_l3(msg);
810
811 uint8_t classmark2_len = gh->data[1];
812 uint8_t *classmark2 = gh->data+2;
813 uint8_t mi_len = *(classmark2 + classmark2_len);
814 uint8_t *mi = (classmark2 + classmark2_len + 1);
815
816 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
817 mi_type = mi[0] & GSM_MI_TYPE_MASK;
818 DEBUGP(DMM, "<- CM RE-ESTABLISH REQUEST MI(%s)=%s\n", gsm48_mi_type_name(mi_type), mi_string);
819
820 /* we don't support CM call re-establishment */
821 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
822}
823
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100824static int gsm48_rx_mm_imsi_detach_ind(struct ran_conn *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000825{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200826 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +0000827 struct gsm48_hdr *gh = msgb_l3(msg);
828 struct gsm48_imsi_detach_ind *idi =
829 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200830 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200831 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200832 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000833
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200834 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200835 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
836 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000837
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200838 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100839
Harald Welte2a139372009-02-22 21:14:55 +0000840 switch (mi_type) {
841 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200842 vsub = vlr_subscr_find_by_tmsi(network->vlr,
843 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000844 break;
845 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200846 vsub = vlr_subscr_find_by_imsi(network->vlr, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000847 break;
848 case GSM_MI_TYPE_IMEI:
849 case GSM_MI_TYPE_IMEISV:
850 /* no sim card... FIXME: what to do ? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200851 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
852 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000853 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200854 default:
Harald Welte2483f1b2016-06-19 18:06:02 +0200855 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
856 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000857 break;
858 }
859
Harald Welte2483f1b2016-06-19 18:06:02 +0200860 if (!vsub) {
861 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
862 gsm48_mi_type_name(mi_type), mi_string);
863 } else {
864 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
Neels Hofmeyrfacd57a2017-12-15 03:48:48 +0100865
866 if (vsub->cs.is_paging)
867 subscr_paging_cancel(vsub, GSM_PAGING_EXPIRED);
868
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200869 /* We already got Classmark 1 during Location Updating ... but well, ok */
870 vsub->classmark.classmark1 = idi->classmark1;
871
Harald Welte2483f1b2016-06-19 18:06:02 +0200872 vlr_subscr_rx_imsi_detach(vsub);
873 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
874 vlr_subscr_put(vsub);
875 }
Harald Welte2a139372009-02-22 21:14:55 +0000876
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100877 ran_conn_close(conn, 0);
Harald Welte2a139372009-02-22 21:14:55 +0000878 return 0;
879}
880
Harald Welted2a7f5a2009-06-05 20:08:20 +0000881static int gsm48_rx_mm_status(struct msgb *msg)
882{
883 struct gsm48_hdr *gh = msgb_l3(msg);
884
885 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
886
887 return 0;
888}
889
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100890static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100891 struct ran_conn *conn,
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100892 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100893{
894 struct gsm48_hdr *gh = msgb_l3(msg);
895 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100896
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100897 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
898 LOGP(DMM, LOGL_ERROR,
899 "%s: MM AUTHENTICATION RESPONSE:"
900 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200901 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100902 return -EINVAL;
903 }
904
905 *res_len = sizeof(ar->sres);
906 memcpy(res, ar->sres, sizeof(ar->sres));
907 return 0;
908}
909
910static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100911 struct ran_conn *conn,
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100912 struct msgb *msg)
913{
914 struct gsm48_hdr *gh;
915 uint8_t *data;
916 uint8_t iei;
917 uint8_t ie_len;
918 unsigned int data_len;
919
920 /* First parse the GSM part */
921 if (parse_gsm_auth_resp(res, res_len, conn, msg))
922 return -EINVAL;
923 OSMO_ASSERT(*res_len == 4);
924
925 /* Then add the extended res part */
926 gh = msgb_l3(msg);
927 data = gh->data + sizeof(struct gsm48_auth_resp);
928 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
929
930 if (data_len < 3) {
931 LOGP(DMM, LOGL_ERROR,
932 "%s: MM AUTHENTICATION RESPONSE:"
933 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200934 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100935 return -EINVAL;
936 }
937
938 iei = data[0];
939 ie_len = data[1];
940 if (iei != GSM48_IE_AUTH_RES_EXT) {
941 LOGP(DMM, LOGL_ERROR,
942 "%s: MM R99 AUTHENTICATION RESPONSE:"
943 " expected IEI 0x%02x, got 0x%02x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200944 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100945 GSM48_IE_AUTH_RES_EXT, iei);
946 return -EINVAL;
947 }
948
949 if (ie_len > 12) {
950 LOGP(DMM, LOGL_ERROR,
951 "%s: MM R99 AUTHENTICATION RESPONSE:"
952 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200953 vlr_subscr_name(conn->vsub), GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100954 return -EINVAL;
955 }
956
957 *res_len += ie_len;
958 memcpy(res + 4, &data[2], ie_len);
959 return 0;
960}
961
962/* Chapter 9.2.3: Authentication Response */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100963static int gsm48_rx_mm_auth_resp(struct ran_conn *conn, struct msgb *msg)
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100964{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100965 uint8_t res[16];
966 uint8_t res_len;
967 int rc;
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100968 bool is_umts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100969
Harald Welte2483f1b2016-06-19 18:06:02 +0200970 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100971 LOGP(DMM, LOGL_ERROR,
972 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100973 ran_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100974 return -EINVAL;
975 }
976
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100977 is_umts = (msgb_l3len(msg) > sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp));
978
979 if (is_umts)
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100980 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100981 else
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100982 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100983
984 if (rc) {
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +0100985 LOGP(DMM, LOGL_ERROR,
986 "%s: MM AUTHENTICATION RESPONSE: invalid: parsing %s AKA Auth Response"
987 " failed with rc=%d; dispatching zero length SRES/RES to trigger failure\n",
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100988 vlr_subscr_name(conn->vsub), is_umts ? "UMTS" : "GSM", rc);
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +0100989 memset(res, 0, sizeof(res));
990 res_len = 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100991 }
992
993 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200994 vlr_subscr_name(conn->vsub),
Neels Hofmeyrd97821f2018-03-10 04:51:39 +0100995 is_umts ? "UMTS" : "GSM", is_umts ? "res" : "sres",
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100996 osmo_hexdump_nospc(res, res_len));
997
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200998 return vlr_subscr_rx_auth_resp(conn->vsub, classmark_is_r99(&conn->vsub->classmark),
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100999 conn->via_ran == OSMO_RAT_UTRAN_IU,
Harald Welte2483f1b2016-06-19 18:06:02 +02001000 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001001}
1002
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001003static int gsm48_rx_mm_auth_fail(struct ran_conn *conn, struct msgb *msg)
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001004{
1005 struct gsm48_hdr *gh = msgb_l3(msg);
1006 uint8_t cause;
1007 uint8_t auts_tag;
1008 uint8_t auts_len;
1009 uint8_t *auts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001010
Harald Welte2483f1b2016-06-19 18:06:02 +02001011 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001012 LOGP(DMM, LOGL_ERROR,
1013 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001014 ran_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001015 return -EINVAL;
1016 }
1017
1018 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
1019 LOGP(DMM, LOGL_ERROR,
1020 "%s: MM R99 AUTHENTICATION FAILURE:"
1021 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001022 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001023 ran_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001024 return -EINVAL;
1025 }
1026
1027 cause = gh->data[0];
1028
1029 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
1030 LOGP(DMM, LOGL_INFO,
1031 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001032 vlr_subscr_name(conn->vsub), cause);
1033 vlr_subscr_rx_auth_fail(conn->vsub, NULL);
1034 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001035 }
1036
1037 /* This is a Synch Failure procedure, which should pass an AUTS to
1038 * resynchronize the sequence nr with the HLR. Expecting exactly one
1039 * TLV with 14 bytes of AUTS. */
1040
1041 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
1042 LOGP(DMM, LOGL_INFO,
1043 "%s: MM R99 AUTHENTICATION FAILURE:"
1044 " invalid Synch Failure: missing AUTS IE\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001045 vlr_subscr_name(conn->vsub));
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001046 ran_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001047 return -EINVAL;
1048 }
1049
1050 auts_tag = gh->data[1];
1051 auts_len = gh->data[2];
1052 auts = &gh->data[3];
1053
1054 if (auts_tag != GSM48_IE_AUTS
1055 || auts_len != 14) {
1056 LOGP(DMM, LOGL_INFO,
1057 "%s: MM R99 AUTHENTICATION FAILURE:"
1058 " invalid Synch Failure:"
1059 " expected AUTS IE 0x%02x of 14 bytes,"
1060 " got IE 0x%02x of %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001061 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001062 GSM48_IE_AUTS, auts_tag, auts_len);
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001063 ran_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001064 return -EINVAL;
1065 }
1066
1067 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
1068 LOGP(DMM, LOGL_INFO,
1069 "%s: MM R99 AUTHENTICATION FAILURE:"
1070 " invalid Synch Failure msg: message truncated (%u)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001071 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001072 ran_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001073 return -EINVAL;
1074 }
1075
1076 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
1077 * large enough. */
1078
1079 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001080 vlr_subscr_name(conn->vsub), osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001081
Harald Welte2483f1b2016-06-19 18:06:02 +02001082 return vlr_subscr_rx_auth_fail(conn->vsub, auts);
1083}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001084
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001085static int gsm48_rx_mm_tmsi_reall_compl(struct ran_conn *conn)
Harald Welte2483f1b2016-06-19 18:06:02 +02001086{
1087 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1088 vlr_subscr_name(conn->vsub));
1089 if (!conn->vsub) {
1090 LOGP(DMM, LOGL_ERROR,
1091 "Rx MM TMSI Reallocation Complete: invalid: no subscriber\n");
1092 return -EINVAL;
1093 }
1094 return vlr_subscr_rx_tmsi_reall_compl(conn->vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001095}
1096
Harald Weltebf5e8df2009-02-03 12:59:45 +00001097/* Receive a GSM 04.08 Mobility Management (MM) message */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001098static int gsm0408_rcv_mm(struct ran_conn *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001099{
1100 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001101 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001102
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001103 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001104 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001105 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001106 break;
1107 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001108 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001109 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001110 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001111 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001112 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001113 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001114 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001115 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001116 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte2483f1b2016-06-19 18:06:02 +02001117 rc = gsm48_rx_mm_tmsi_reall_compl(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001118 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001119 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001120 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001121 break;
1122 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01001123 rc = gsm48_rx_cm_reest_req(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001124 break;
1125 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001126 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001127 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001128 case GSM48_MT_MM_AUTH_FAIL:
1129 rc = gsm48_rx_mm_auth_fail(conn, msg);
1130 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001131 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001132 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001133 gh->msg_type);
1134 break;
1135 }
1136
1137 return rc;
1138}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001139
Harald Welte2d35ae62009-02-06 12:02:13 +00001140/* Receive a PAGING RESPONSE message from the MS */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001141static int gsm48_rx_rr_pag_resp(struct ran_conn *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001142{
Harald Welte2483f1b2016-06-19 18:06:02 +02001143 struct gsm_network *net = conn->network;
Harald Welte2d35ae62009-02-06 12:02:13 +00001144 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001145 struct gsm48_pag_resp *resp;
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001146 uint8_t classmark2_len = gh->data[1];
1147 uint8_t *classmark2 = gh->data+2;
1148 uint8_t *mi_lv = classmark2 + classmark2_len;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001149 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001150 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +02001151 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001152 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +02001153
Neels Hofmeyr379d5792018-02-22 04:04:54 +01001154 lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001155 lai.lac = conn->lac;
Harald Welte2d35ae62009-02-06 12:02:13 +00001156
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001157 resp = (struct gsm48_pag_resp *) &gh->data[0];
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001158
1159 if (gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh), mi_string, &mi_type) <= 0) {
1160 LOGP(DRR, LOGL_ERROR, "PAGING RESPONSE: invalid Mobile Identity\n");
1161 return -EINVAL;
1162 }
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001163
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001164 if (ran_conn_is_establishing_auth_ciph(conn)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001165 LOGP(DMM, LOGL_ERROR,
1166 "Ignoring Paging Response, conn already busy establishing authenticity\n");
1167 return 0;
1168 }
1169
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001170 if (ran_conn_is_accepted(conn)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001171 LOGP(DMM, LOGL_ERROR, "Ignoring Paging Response, conn already established\n");
1172 return 0;
1173 }
1174
1175 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n", gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001176
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001177 ran_conn_update_id(conn, COMPLETE_LAYER3_PAGING_RESP, mi_string);
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001178
Neels Hofmeyr7814a832018-12-26 00:40:18 +01001179 is_utran = (conn->via_ran == OSMO_RAT_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +02001180 vlr_proc_acc_req(conn->fi,
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001181 RAN_CONN_E_ACCEPTED, RAN_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +02001182 net->vlr, conn,
1183 VLR_PR_ARQ_T_PAGING_RESP, mi_lv, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001184 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +01001185 is_utran || conn->network->a5_encryption_mask > 0x01,
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001186 classmark2_is_r99(classmark2, classmark2_len),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001187 is_utran);
Holger Freyther053e09d2009-02-14 22:51:06 +00001188
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001189 /* From vlr_proc_acc_req() we expect an implicit dispatch of PR_ARQ_E_START we expect
1190 * msc_vlr_subscr_assoc() to already have been called and completed. Has an error occured? */
1191 if (!conn->vsub) {
1192 LOGP(DRR, LOGL_ERROR, "%s: subscriber not allowed to do a Paging Response\n",
1193 mi_string);
1194 return -EIO;
1195 }
1196
1197 memcpy(conn->vsub->classmark.classmark2, classmark2, classmark2_len);
1198 conn->vsub->classmark.classmark2_len = classmark2_len;
1199
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001200 ran_conn_complete_layer_3(conn);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001201 return 0;
Harald Welte2d35ae62009-02-06 12:02:13 +00001202}
1203
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001204static int gsm48_rx_rr_app_info(struct ran_conn *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001205{
1206 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001207 uint8_t apdu_id_flags;
1208 uint8_t apdu_len;
1209 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001210
1211 apdu_id_flags = gh->data[0];
1212 apdu_len = gh->data[1];
1213 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001214
Max8dc8f232017-02-09 19:13:02 +01001215 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001216 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001217
Harald Welte2483f1b2016-06-19 18:06:02 +02001218 /* we're not using the app info blob anywhere, so ignore. */
1219#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001220 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte2483f1b2016-06-19 18:06:02 +02001221#else
1222 return 0;
1223#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001224}
1225
Harald Weltebf5e8df2009-02-03 12:59:45 +00001226/* Receive a GSM 04.08 Radio Resource (RR) message */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001227static int gsm0408_rcv_rr(struct ran_conn *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001228{
1229 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001230 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001231
1232 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001233 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001234 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001235 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001236 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001237 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001238 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001239 default:
Philippd37981e2016-10-10 15:11:41 +02001240 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1241 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001242 break;
1243 }
1244
Harald Welte2d35ae62009-02-06 12:02:13 +00001245 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001246}
1247
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001248int gsm48_send_rr_app_info(struct ran_conn *conn, uint8_t apdu_id,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001249 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001250{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001251 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001252 struct gsm48_hdr *gh;
1253
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001254 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1255 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001256
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001257 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1258 gh->proto_discr = GSM48_PDISC_RR;
1259 gh->msg_type = GSM48_MT_RR_APP_INFO;
1260 gh->data[0] = apdu_id;
1261 gh->data[1] = apdu_len;
1262 memcpy(gh->data+2, apdu, apdu_len);
1263
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001264 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001265}
1266
Harald Welte2483f1b2016-06-19 18:06:02 +02001267static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08001268{
Harald Welte2483f1b2016-06-19 18:06:02 +02001269 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
1270 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08001271
Harald Welte2483f1b2016-06-19 18:06:02 +02001272 switch (pdisc) {
1273 case GSM48_PDISC_MM:
1274 switch (msg_type) {
1275 case GSM48_MT_MM_LOC_UPD_REQUEST:
1276 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01001277 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte2483f1b2016-06-19 18:06:02 +02001278 case GSM48_MT_MM_AUTH_RESP:
1279 case GSM48_MT_MM_AUTH_FAIL:
1280 case GSM48_MT_MM_ID_RESP:
1281 case GSM48_MT_MM_TMSI_REALL_COMPL:
1282 case GSM48_MT_MM_IMSI_DETACH_IND:
1283 return true;
1284 default:
1285 break;
1286 }
1287 break;
1288 case GSM48_PDISC_RR:
1289 switch (msg_type) {
Neels Hofmeyr325f1062018-12-07 16:39:52 +01001290 /* GSM48_MT_RR_CIPH_M_COMPL is actually handled in bssmap_rx_ciph_compl() and gets redirected in the
1291 * BSSAP layer to ran_conn_cipher_mode_compl() (before this here is reached) */
Harald Welte2483f1b2016-06-19 18:06:02 +02001292 case GSM48_MT_RR_PAG_RESP:
1293 return true;
1294 default:
1295 break;
1296 }
1297 break;
1298 default:
1299 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02001300 }
1301
Harald Welte2483f1b2016-06-19 18:06:02 +02001302 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02001303}
1304
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001305void cm_service_request_concludes(struct ran_conn *conn,
Harald Welte2483f1b2016-06-19 18:06:02 +02001306 struct msgb *msg)
1307{
1308
1309 /* If a CM Service Request was received before, this is the request the
1310 * conn was opened for. No need to wait for further messages. */
1311
1312 if (!conn->received_cm_service_request)
1313 return;
1314
1315 if (log_check_level(DMM, LOGL_DEBUG)) {
1316 struct gsm48_hdr *gh = msgb_l3(msg);
1317 uint8_t pdisc = gsm48_hdr_pdisc(gh);
1318 uint8_t msg_type = gsm48_hdr_msg_type(gh);
1319
Neels Hofmeyr78ada642017-03-10 02:15:20 +01001320 DEBUGP(DMM, "%s: rx msg %s:"
Harald Welte2483f1b2016-06-19 18:06:02 +02001321 " received_cm_service_request changes to false\n",
1322 vlr_subscr_name(conn->vsub),
Neels Hofmeyr78ada642017-03-10 02:15:20 +01001323 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Harald Welte2483f1b2016-06-19 18:06:02 +02001324 }
1325 conn->received_cm_service_request = false;
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +01001326 ran_conn_put(conn, RAN_CONN_USE_CM_SERVICE);
Harald Welte2483f1b2016-06-19 18:06:02 +02001327}
1328
Harald Weltec2007852018-02-03 21:08:26 +01001329/* TS 24.007 11.2.3.2.3 Message Type Octet / Duplicate Detection */
1330int gsm0407_pdisc_ctr_bin(uint8_t pdisc)
1331{
1332 switch (pdisc) {
1333 case GSM48_PDISC_MM:
1334 case GSM48_PDISC_CC:
1335 case GSM48_PDISC_NC_SS:
1336 return 0;
1337 case GSM48_PDISC_GROUP_CC:
1338 return 1;
1339 case GSM48_PDISC_BCAST_CC:
1340 return 2;
1341 case GSM48_PDISC_LOC:
1342 return 3;
1343 default:
1344 return -1;
1345 }
1346}
1347
1348/* extract the N(SD) and return the modulo value for a R98 message */
1349static uint8_t gsm0407_determine_nsd_ret_modulo_r99(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
1350{
1351 switch (pdisc) {
1352 case GSM48_PDISC_MM:
1353 case GSM48_PDISC_CC:
1354 case GSM48_PDISC_NC_SS:
1355 *n_sd = (msg_type >> 6) & 0x3;
1356 return 4;
1357 case GSM48_PDISC_GROUP_CC:
1358 case GSM48_PDISC_BCAST_CC:
1359 case GSM48_PDISC_LOC:
1360 *n_sd = (msg_type >> 6) & 0x1;
1361 return 2;
1362 default:
1363 /* no sequence number, we cannot detect dups */
1364 return 0;
1365 }
1366}
1367
1368/* extract the N(SD) and return the modulo value for a R99 message */
1369static uint8_t gsm0407_determine_nsd_ret_modulo_r98(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
1370{
1371 switch (pdisc) {
1372 case GSM48_PDISC_MM:
1373 case GSM48_PDISC_CC:
1374 case GSM48_PDISC_NC_SS:
1375 case GSM48_PDISC_GROUP_CC:
1376 case GSM48_PDISC_BCAST_CC:
1377 case GSM48_PDISC_LOC:
1378 *n_sd = (msg_type >> 6) & 0x1;
1379 return 2;
1380 default:
1381 /* no sequence number, we cannot detect dups */
1382 return 0;
1383 }
1384}
1385
1386/* TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001387static bool gsm0407_is_duplicate(struct ran_conn *conn, struct msgb *msg)
Harald Weltec2007852018-02-03 21:08:26 +01001388{
1389 struct gsm48_hdr *gh;
1390 uint8_t pdisc;
Harald Weltef6400732018-10-21 12:55:11 +02001391 uint8_t n_sd, modulo;
1392 int bin;
Harald Weltec2007852018-02-03 21:08:26 +01001393
1394 gh = msgb_l3(msg);
1395 pdisc = gsm48_hdr_pdisc(gh);
1396
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001397 if (conn->vsub && classmark_is_r99(&conn->vsub->classmark)) {
Harald Weltec2007852018-02-03 21:08:26 +01001398 modulo = gsm0407_determine_nsd_ret_modulo_r99(pdisc, gh->msg_type, &n_sd);
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001399 } else { /* pre R99 */
1400 modulo = gsm0407_determine_nsd_ret_modulo_r98(pdisc, gh->msg_type, &n_sd);
Harald Weltec2007852018-02-03 21:08:26 +01001401 }
1402 if (modulo == 0)
1403 return false;
1404 bin = gsm0407_pdisc_ctr_bin(pdisc);
1405 if (bin < 0)
1406 return false;
1407
1408 OSMO_ASSERT(bin < ARRAY_SIZE(conn->n_sd_next));
1409 if (n_sd != conn->n_sd_next[bin]) {
1410 /* not what we expected: duplicate */
1411 return true;
1412 } else {
1413 /* as expected: no dup; update expected counter for next message */
1414 conn->n_sd_next[bin] = (n_sd + 1) % modulo;
1415 return false;
1416 }
1417}
Harald Welte2483f1b2016-06-19 18:06:02 +02001418
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001419extern int gsm0408_rcv_cc(struct ran_conn *conn, struct msgb *msg);
Harald Welte27989d42018-06-21 20:39:20 +02001420
Neels Hofmeyr378a4922016-05-09 21:07:43 +02001421/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001422int gsm0408_dispatch(struct ran_conn *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001423{
Philipp Maier91f10c72017-04-20 18:40:37 +02001424 struct gsm48_hdr *gh;
1425 uint8_t pdisc;
Harald Welte8470bf22008-12-25 23:28:35 +00001426 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01001427
Neels Hofmeyr0906a392017-09-19 14:36:06 +02001428 OSMO_ASSERT(msg->l3h);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02001429 OSMO_ASSERT(conn);
1430 OSMO_ASSERT(msg);
1431
Philipp Maier91f10c72017-04-20 18:40:37 +02001432 gh = msgb_l3(msg);
1433 pdisc = gsm48_hdr_pdisc(gh);
1434
Harald Weltec2007852018-02-03 21:08:26 +01001435 if (gsm0407_is_duplicate(conn, msg)) {
1436 LOGP(DRLL, LOGL_NOTICE, "%s: Discarding duplicate L3 message\n",
1437 (conn && conn->vsub) ? vlr_subscr_name(conn->vsub) : "UNKNOWN");
1438 return 0;
1439 }
1440
Neels Hofmeyr78ada642017-03-10 02:15:20 +01001441 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message %s (0x%x:0x%x)\n",
1442 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)),
1443 pdisc, gsm48_hdr_msg_type(gh));
Harald Welte2483f1b2016-06-19 18:06:02 +02001444
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001445 if (!ran_conn_is_accepted(conn)
Harald Welte2483f1b2016-06-19 18:06:02 +02001446 && !msg_is_initially_permitted(gh)) {
1447 LOGP(DRLL, LOGL_ERROR,
Neels Hofmeyr78ada642017-03-10 02:15:20 +01001448 "subscr %s: Message not permitted for initial conn: %s\n",
1449 vlr_subscr_name(conn->vsub),
1450 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
Harald Welte2483f1b2016-06-19 18:06:02 +02001451 return -EACCES;
1452 }
1453
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001454 if (conn->vsub && conn->vsub->cs.attached_via_ran != conn->via_ran) {
1455 LOGP(DMM, LOGL_ERROR,
1456 "%s: Illegal situation: RAN type mismatch:"
1457 " attached via %s, received message via %s\n",
1458 vlr_subscr_name(conn->vsub),
Neels Hofmeyr7814a832018-12-26 00:40:18 +01001459 osmo_rat_type_name(conn->vsub->cs.attached_via_ran),
1460 osmo_rat_type_name(conn->via_ran));
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001461 return -EACCES;
1462 }
1463
Philipp Maiere0d5caa2017-02-27 16:56:59 +01001464#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08001465 if (silent_call_reroute(conn, msg))
1466 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01001467#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001468
Harald Welte52b1f982008-12-23 20:25:15 +00001469 switch (pdisc) {
1470 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001471 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001472 break;
1473 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001474 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001475 break;
1476 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001477 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001478 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00001479 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08001480 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00001481 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001482 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00001483 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01001484 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
1485 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02001486 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00001487 break;
Harald Welte6eafe912009-10-16 08:32:58 +02001488 case GSM48_PDISC_NC_SS:
Vadim Yanitskiy5b860fa2018-06-12 05:24:52 +07001489 rc = gsm0911_rcv_nc_ss(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02001490 break;
Harald Welteeac38c32017-05-29 18:02:53 +02001491 case GSM48_PDISC_TEST:
1492 rc = gsm0414_rcv_test(conn, msg);
1493 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001494 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001495 LOGP(DRLL, LOGL_NOTICE, "Unknown "
1496 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02001497 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00001498 break;
1499 }
1500
1501 return rc;
1502}
Harald Welte8470bf22008-12-25 23:28:35 +00001503
Harald Welte2483f1b2016-06-19 18:06:02 +02001504/***********************************************************************
1505 * VLR integration
1506 ***********************************************************************/
1507
1508/* VLR asks us to send an authentication request */
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +01001509static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct vlr_auth_tuple *at,
Harald Welte2483f1b2016-06-19 18:06:02 +02001510 bool send_autn)
1511{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001512 struct ran_conn *conn = msc_conn_ref;
Harald Welte2483f1b2016-06-19 18:06:02 +02001513 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
1514 send_autn? at->vec.autn : NULL,
1515 at->key_seq);
1516}
1517
1518/* VLR asks us to send an authentication reject */
1519static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
1520{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001521 struct ran_conn *conn = msc_conn_ref;
Harald Welte2483f1b2016-06-19 18:06:02 +02001522 return gsm48_tx_mm_auth_rej(conn);
1523}
1524
1525/* VLR asks us to transmit an Identity Request of given type */
1526static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
1527{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001528 struct ran_conn *conn = msc_conn_ref;
Harald Welte2483f1b2016-06-19 18:06:02 +02001529 return mm_tx_identity_req(conn, mi_type);
1530}
1531
1532/* VLR asks us to transmit a Location Update Accept */
1533static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
1534{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001535 struct ran_conn *conn = msc_conn_ref;
Harald Welte2483f1b2016-06-19 18:06:02 +02001536 return gsm0408_loc_upd_acc(conn, send_tmsi);
1537}
1538
1539/* VLR asks us to transmit a Location Update Reject */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001540static int msc_vlr_tx_lu_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Harald Welte2483f1b2016-06-19 18:06:02 +02001541{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001542 struct ran_conn *conn = msc_conn_ref;
Harald Welte2483f1b2016-06-19 18:06:02 +02001543 return gsm0408_loc_upd_rej(conn, cause);
1544}
1545
1546/* VLR asks us to transmit a CM Service Accept */
1547static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
1548{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001549 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001550 return msc_gsm48_tx_mm_serv_ack(conn);
1551}
1552
1553static int msc_vlr_tx_common_id(void *msc_conn_ref)
1554{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001555 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001556 return msc_tx_common_id(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +02001557}
1558
Stefan Sperling3a741282018-03-13 21:11:49 +01001559/* VLR asks us to transmit MM info. */
1560static int msc_vlr_tx_mm_info(void *msc_conn_ref)
1561{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001562 struct ran_conn *conn = msc_conn_ref;
Stefan Sperling3a741282018-03-13 21:11:49 +01001563 if (!conn->network->send_mm_info)
1564 return 0;
1565 return gsm48_tx_mm_info(conn);
1566}
1567
Harald Welte2483f1b2016-06-19 18:06:02 +02001568/* VLR asks us to transmit a CM Service Reject */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001569static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Harald Welte2483f1b2016-06-19 18:06:02 +02001570{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001571 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001572 int rc;
Harald Welte2483f1b2016-06-19 18:06:02 +02001573
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001574 rc = msc_gsm48_tx_mm_serv_rej(conn, cause);
1575
1576 if (conn->received_cm_service_request) {
1577 conn->received_cm_service_request = false;
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +01001578 ran_conn_put(conn, RAN_CONN_USE_CM_SERVICE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001579 }
1580
1581 return rc;
Harald Welte2483f1b2016-06-19 18:06:02 +02001582}
1583
Neels Hofmeyr703638e2017-12-14 05:30:16 +01001584/* For msc_vlr_set_ciph_mode() */
1585osmo_static_assert(sizeof(((struct gsm0808_encrypt_info*)0)->key) >= sizeof(((struct osmo_auth_vector*)0)->kc),
1586 gsm0808_encrypt_info_key_fits_osmo_auth_vec_kc);
1587
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +01001588int ran_conn_geran_set_cipher_mode(struct ran_conn *conn, bool umts_aka, bool retrieve_imeisv)
Neels Hofmeyr3117b702018-09-13 03:23:07 +02001589{
1590 struct gsm_network *net = conn->network;
1591 struct gsm0808_encrypt_info ei;
1592 int i, j = 0;
1593 int request_classmark = 0;
1594 int request_classmark_for_a5_n = 0;
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +01001595 struct vlr_auth_tuple *tuple = conn->vsub->last_tuple;
Neels Hofmeyr3117b702018-09-13 03:23:07 +02001596
1597 if (!conn || !conn->vsub || !conn->vsub->last_tuple) {
1598 /* This should really never happen, because we checked this in msc_vlr_set_ciph_mode()
1599 * already. */
1600 LOGP(DMM, LOGL_ERROR, "Internal error: missing state during Ciphering Mode Command\n");
1601 return -EINVAL;
1602 }
1603
1604 for (i = 0; i < 8; i++) {
1605 int supported;
1606
1607 /* A5/n permitted by osmo-msc.cfg? */
1608 if (!(net->a5_encryption_mask & (1 << i)))
1609 continue;
1610
1611 /* A5/n supported by MS? */
1612 supported = classmark_supports_a5(&conn->vsub->classmark, i);
1613 if (supported == 1) {
1614 ei.perm_algo[j++] = vlr_ciph_to_gsm0808_alg_id(i);
1615 /* A higher A5/n is supported, so no need to request a Classmark
1616 * for support of a lesser A5/n. */
1617 request_classmark = 0;
1618 } else if (supported < 0) {
1619 request_classmark = -supported;
1620 request_classmark_for_a5_n = i;
1621 }
1622 }
1623 ei.perm_algo_len = j;
1624
1625 if (request_classmark) {
1626 /* The highest A5/n as from osmo-msc.cfg might be available, but we are
1627 * still missing the Classmark information for that from the MS. First
1628 * ask for that. */
1629 LOGP(DMM, LOGL_DEBUG, "%s: to determine whether A5/%d is supported,"
1630 " first ask for a Classmark Update to obtain Classmark %d\n",
1631 vlr_subscr_name(conn->vsub), request_classmark_for_a5_n,
1632 request_classmark);
1633
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +01001634 return ran_conn_classmark_request_then_cipher_mode_cmd(conn, umts_aka, retrieve_imeisv);
Neels Hofmeyr3117b702018-09-13 03:23:07 +02001635 }
1636
1637 if (ei.perm_algo_len == 0) {
1638 LOGP(DMM, LOGL_ERROR, "%s: cannot start ciphering, no intersection "
1639 "between MSC-configured and MS-supported A5 algorithms\n",
1640 vlr_subscr_name(conn->vsub));
1641 return -ENOTSUP;
1642 }
1643
1644 DEBUGP(DMM, "-> CIPHER MODE COMMAND %s\n", vlr_subscr_name(conn->vsub));
1645
1646 tuple = conn->vsub->last_tuple;
1647
1648 /* In case of UMTS AKA, the Kc for ciphering must be derived from the 3G auth
1649 * tokens. tuple->vec.kc was calculated from the GSM algorithm and is not
1650 * necessarily a match for the UMTS AKA tokens. */
1651 if (umts_aka)
1652 osmo_auth_c3(ei.key, tuple->vec.ck, tuple->vec.ik);
1653 else
1654 memcpy(ei.key, tuple->vec.kc, sizeof(tuple->vec.kc));
1655 ei.key_len = sizeof(tuple->vec.kc);
1656
Neels Hofmeyrf41658d2018-11-30 04:35:50 +01001657 conn->geran_encr = (struct geran_encr){};
1658 if (ei.key_len <= sizeof(conn->geran_encr.key)) {
1659 memcpy(conn->geran_encr.key, ei.key, ei.key_len);
1660 conn->geran_encr.key_len = ei.key_len;
Neels Hofmeyrb0779bb2018-11-29 23:37:19 +01001661 }
Neels Hofmeyrf41658d2018-11-30 04:35:50 +01001662 /* conn->geran_encr.alg_id remains unknown until we receive a Cipher Mode Complete from the BSC */
Neels Hofmeyrb0779bb2018-11-29 23:37:19 +01001663
Neels Hofmeyr3117b702018-09-13 03:23:07 +02001664 return a_iface_tx_cipher_mode(conn, &ei, retrieve_imeisv);
1665}
1666
Neels Hofmeyrda21a522018-03-02 01:50:09 +01001667/* VLR asks us to start using ciphering.
1668 * (Keep non-static to allow regression testing on this function.) */
1669int msc_vlr_set_ciph_mode(void *msc_conn_ref,
1670 bool umts_aka,
1671 bool retrieve_imeisv)
Harald Welte2483f1b2016-06-19 18:06:02 +02001672{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001673 struct ran_conn *conn = msc_conn_ref;
Harald Welte2483f1b2016-06-19 18:06:02 +02001674 struct vlr_subscr *vsub;
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +01001675 struct vlr_auth_tuple *tuple;
Harald Welte2483f1b2016-06-19 18:06:02 +02001676
1677 if (!conn || !conn->vsub) {
1678 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
1679 " NULL conn/subscriber");
1680 return -EINVAL;
1681 }
1682
1683 vsub = conn->vsub;
1684 tuple = vsub->last_tuple;
1685
1686 if (!tuple) {
1687 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
1688 " Command: no auth tuple available\n",
1689 vlr_subscr_name(vsub));
1690 return -EINVAL;
1691 }
1692
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001693 switch (conn->via_ran) {
Neels Hofmeyr7814a832018-12-26 00:40:18 +01001694 case OSMO_RAT_GERAN_A:
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +01001695 return ran_conn_geran_set_cipher_mode(conn, umts_aka, retrieve_imeisv);
Neels Hofmeyr703638e2017-12-14 05:30:16 +01001696
Neels Hofmeyr7814a832018-12-26 00:40:18 +01001697 case OSMO_RAT_UTRAN_IU:
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001698#ifdef BUILD_IU
1699 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n",
1700 vlr_subscr_name(conn->vsub));
Neels Hofmeyr00e82d62017-07-05 15:19:52 +02001701 return ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &tuple->vec, 0, 1);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001702#else
Neels Hofmeyr7814a832018-12-26 00:40:18 +01001703 LOGP(DMM, LOGL_ERROR, "Cannot send Security Mode Control over OSMO_RAT_UTRAN_IU,"
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001704 " built without Iu support\n");
1705 return -ENOTSUP;
1706#endif
1707
1708 default:
1709 break;
1710 }
1711 LOGP(DMM, LOGL_ERROR,
1712 "%s: cannot start ciphering, unknown RAN type %d\n",
1713 vlr_subscr_name(conn->vsub), conn->via_ran);
1714 return -ENOTSUP;
1715}
1716
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +01001717void ran_conn_rx_sec_mode_compl(struct ran_conn *conn)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001718{
1719 struct vlr_ciph_result vlr_res = {};
1720
1721 if (!conn || !conn->vsub) {
1722 LOGP(DMM, LOGL_ERROR,
1723 "Rx Security Mode Complete for invalid conn\n");
1724 return;
1725 }
1726
1727 DEBUGP(DMM, "<- SECURITY MODE COMPLETE %s\n",
1728 vlr_subscr_name(conn->vsub));
1729
1730 vlr_res.cause = VLR_CIPH_COMPL;
1731 vlr_subscr_rx_ciph_res(conn->vsub, &vlr_res);
Harald Welte2483f1b2016-06-19 18:06:02 +02001732}
1733
1734/* VLR informs us that the subscriber data has somehow been modified */
1735static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
1736{
Maxa263bb22017-12-27 13:23:44 +01001737 LOGVSUBP(LOGL_NOTICE, subscr, "VLR: update for IMSI=%s (MSISDN=%s, used=%d)\n",
1738 subscr->imsi, subscr->msisdn, subscr->use_count);
Harald Welte2483f1b2016-06-19 18:06:02 +02001739}
1740
Neels Hofmeyr68cf9572018-09-18 15:52:58 +02001741static void update_classmark(const struct gsm_classmark *src, struct gsm_classmark *dst)
1742{
1743 if (src->classmark1_set) {
1744 dst->classmark1 = src->classmark1;
1745 dst->classmark1_set = true;
1746 }
1747 if (src->classmark2_len) {
1748 dst->classmark2_len = src->classmark2_len;
1749 memcpy(dst->classmark2, src->classmark2, sizeof(dst->classmark2));
1750 }
1751 if (src->classmark3_len) {
1752 dst->classmark3_len = src->classmark3_len;
1753 memcpy(dst->classmark3, src->classmark3, sizeof(dst->classmark3));
1754 }
1755}
1756
Harald Welte2483f1b2016-06-19 18:06:02 +02001757/* VLR informs us that the subscriber has been associated with a conn */
Neels Hofmeyr1035d902018-12-28 21:22:32 +01001758static int msc_vlr_subscr_assoc(void *msc_conn_ref,
Harald Welte2483f1b2016-06-19 18:06:02 +02001759 struct vlr_subscr *vsub)
1760{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001761 struct ran_conn *conn = msc_conn_ref;
Max48d4ec02018-02-12 16:51:03 +01001762 OSMO_ASSERT(vsub);
Neels Hofmeyr1035d902018-12-28 21:22:32 +01001763 if (conn->vsub) {
1764 if (conn->vsub == vsub)
1765 LOGPCONN(conn, LOGL_NOTICE, "msc_vlr_subscr_assoc(): conn already associated with %s\n",
1766 vlr_subscr_name(vsub));
1767 else {
1768 LOGPCONN(conn, LOGL_ERROR, "msc_vlr_subscr_assoc(): conn already associated with a subscriber,"
1769 " cannot associate with %s\n", vlr_subscr_name(vsub));
1770 return -EINVAL;
1771 }
1772 }
1773
Harald Welte2483f1b2016-06-19 18:06:02 +02001774 conn->vsub = vlr_subscr_get(vsub);
Max48d4ec02018-02-12 16:51:03 +01001775 OSMO_ASSERT(conn->vsub);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001776 conn->vsub->cs.attached_via_ran = conn->via_ran;
Neels Hofmeyr68cf9572018-09-18 15:52:58 +02001777
1778 /* In case we have already received Classmark Information before the VLR Subscriber was
1779 * associated with the conn: merge the new Classmark into vsub->classmark. Don't overwrite valid
1780 * vsub->classmark with unset classmark, though. */
1781 update_classmark(&conn->temporary_classmark, &conn->vsub->classmark);
Neels Hofmeyr1035d902018-12-28 21:22:32 +01001782 return 0;
Harald Welte2483f1b2016-06-19 18:06:02 +02001783}
1784
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001785static int msc_vlr_route_gsup_msg(struct vlr_subscr *vsub,
1786 struct osmo_gsup_message *gsup_msg)
1787{
1788 switch (gsup_msg->message_type) {
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +07001789 /* GSM 09.11 code implementing SS/USSD */
1790 case OSMO_GSUP_MSGT_PROC_SS_REQUEST:
1791 case OSMO_GSUP_MSGT_PROC_SS_RESULT:
1792 case OSMO_GSUP_MSGT_PROC_SS_ERROR:
1793 DEBUGP(DMSC, "Routed to GSM 09.11 SS/USSD handler\n");
1794 return gsm0911_gsup_handler(vsub, gsup_msg);
1795
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +07001796 /* GSM 04.11 code implementing MO SMS */
1797 case OSMO_GSUP_MSGT_MO_FORWARD_SM_ERROR:
1798 case OSMO_GSUP_MSGT_MO_FORWARD_SM_RESULT:
1799 case OSMO_GSUP_MSGT_READY_FOR_SM_ERROR:
1800 case OSMO_GSUP_MSGT_READY_FOR_SM_RESULT:
1801 DEBUGP(DMSC, "Routed to GSM 04.11 MO handler\n");
1802 return gsm411_gsup_mo_handler(vsub, gsup_msg);
1803
Vadim Yanitskiy82cc8a52018-11-15 01:05:53 +07001804 /* GSM 04.11 code implementing MT SMS */
1805 case OSMO_GSUP_MSGT_MT_FORWARD_SM_REQUEST:
1806 DEBUGP(DMSC, "Routed to GSM 04.11 MT handler\n");
1807 return gsm411_gsup_mt_handler(vsub, gsup_msg);
1808
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001809 default:
1810 LOGP(DMM, LOGL_ERROR, "No handler found for %s, dropping message...\n",
1811 osmo_gsup_message_type_name(gsup_msg->message_type));
1812 return -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
1813 }
1814}
1815
Harald Welte2483f1b2016-06-19 18:06:02 +02001816/* operations that we need to implement for libvlr */
1817static const struct vlr_ops msc_vlr_ops = {
1818 .tx_auth_req = msc_vlr_tx_auth_req,
1819 .tx_auth_rej = msc_vlr_tx_auth_rej,
1820 .tx_id_req = msc_vlr_tx_id_req,
1821 .tx_lu_acc = msc_vlr_tx_lu_acc,
1822 .tx_lu_rej = msc_vlr_tx_lu_rej,
1823 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
1824 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
1825 .set_ciph_mode = msc_vlr_set_ciph_mode,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001826 .tx_common_id = msc_vlr_tx_common_id,
Stefan Sperling3a741282018-03-13 21:11:49 +01001827 .tx_mm_info = msc_vlr_tx_mm_info,
Harald Welte2483f1b2016-06-19 18:06:02 +02001828 .subscr_update = msc_vlr_subscr_update,
1829 .subscr_assoc = msc_vlr_subscr_assoc,
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001830 .forward_gsup_msg = msc_vlr_route_gsup_msg,
Harald Welte2483f1b2016-06-19 18:06:02 +02001831};
1832
1833/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
1834int msc_vlr_alloc(struct gsm_network *net)
1835{
1836 net->vlr = vlr_alloc(net, &msc_vlr_ops);
1837 if (!net->vlr)
1838 return -ENOMEM;
1839 net->vlr->user_ctx = net;
1840 return 0;
1841}
1842
1843/* Launch the VLR, i.e. its GSUP connection */
1844int msc_vlr_start(struct gsm_network *net)
1845{
Stefan Sperlingafa030d2018-12-06 12:06:59 +01001846 struct ipaccess_unit *ipa_dev;
1847
Harald Welte2483f1b2016-06-19 18:06:02 +02001848 OSMO_ASSERT(net->vlr);
Stefan Sperlingafa030d2018-12-06 12:06:59 +01001849
1850 ipa_dev = talloc_zero(net->vlr, struct ipaccess_unit);
1851 ipa_dev->unit_name = "MSC";
1852 ipa_dev->serno = net->msc_ipa_name; /* NULL unless configured via VTY */
Stefan Sperling36dedee2018-12-06 13:16:24 +01001853 ipa_dev->swversion = PACKAGE_NAME "-" PACKAGE_VERSION;
Stefan Sperlingafa030d2018-12-06 12:06:59 +01001854
1855 return vlr_start(ipa_dev, net->vlr, net->gsup_server_addr_str, net->gsup_server_port);
Harald Welte2483f1b2016-06-19 18:06:02 +02001856}
Neels Hofmeyr79e580b2018-03-22 16:48:22 +01001857
1858struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value)
1859{
1860 struct msgb *msg;
1861 struct gsm48_hdr *gh;
1862
1863 msg = gsm48_msgb_alloc_name("GSM 04.08 SERV REJ");
1864 if (!msg)
1865 return NULL;
1866
1867 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1868 gh->proto_discr = GSM48_PDISC_MM;
1869 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1870 gh->data[0] = value;
1871
1872 return msg;
1873}
1874
1875struct msgb *gsm48_create_loc_upd_rej(uint8_t cause)
1876{
1877 struct gsm48_hdr *gh;
1878 struct msgb *msg;
1879
1880 msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD REJ");
1881 if (!msg)
1882 return NULL;
1883
1884 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1885 gh->proto_discr = GSM48_PDISC_MM;
1886 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
1887 gh->data[0] = cause;
1888 return msg;
1889}
1890
1891int gsm48_extract_mi(uint8_t *classmark2_lv, int length, char *mi_string, uint8_t *mi_type)
1892{
1893 /* Check the size for the classmark */
1894 if (length < 1 + *classmark2_lv)
1895 return -1;
1896
1897 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
1898 if (length < 2 + *classmark2_lv + mi_lv[0])
1899 return -2;
1900
1901 *mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
1902 return gsm48_mi_to_string(mi_string, GSM48_MI_SIZE, mi_lv+1, *mi_lv);
1903}
1904
1905int gsm48_paging_extract_mi(struct gsm48_pag_resp *resp, int length,
1906 char *mi_string, uint8_t *mi_type)
1907{
1908 static const uint32_t classmark_offset =
1909 offsetof(struct gsm48_pag_resp, classmark2);
1910 uint8_t *classmark2_lv = (uint8_t *) &resp->classmark2;
1911 return gsm48_extract_mi(classmark2_lv, length - classmark_offset,
1912 mi_string, mi_type);
1913}