blob: c6e5528afd03cf6fb128a14170dcb0df490b653d [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 Hofmeyr5769d362018-12-18 03:13:51 +0100121static const char *classmark_a5_name(const struct gsm_classmark *cm)
122{
123 static char buf[128];
124 char cm1[42];
125 char cm2[42];
126 char cm3[42];
127
128 if (cm->classmark1_set)
129 snprintf(cm1, sizeof(cm1), "cm1{a5/1=%s}",
130 cm->classmark1.a5_1 ? "not-supported":"supported" /* inverted logic */);
131 else
132 snprintf(cm1, sizeof(cm1), "no-cm1");
133
134 if (cm->classmark2_len >= 3)
135 snprintf(cm2, sizeof(cm2), " cm2{0x%x=%s%s}",
136 cm->classmark2[2],
137 cm->classmark2[2] & 0x1 ? " A5/2" : "",
138 cm->classmark2[2] & 0x2 ? " A5/3" : "");
139 else
140 snprintf(cm2, sizeof(cm2), " no-cm2");
141
142 if (cm->classmark3_len >= 1)
143 snprintf(cm3, sizeof(cm3), " cm3{0x%x=%s%s%s%s}",
144 cm->classmark3[0],
145 cm->classmark3[0] & (1 << 0) ? " A5/4" : "",
146 cm->classmark3[0] & (1 << 1) ? " A5/5" : "",
147 cm->classmark3[0] & (1 << 2) ? " A5/6" : "",
148 cm->classmark3[0] & (1 << 3) ? " A5/7" : "");
149 else
150 snprintf(cm3, sizeof(cm3), " no-cm3");
151
152 snprintf(buf, sizeof(buf), "%s%s%s", cm1, cm2, cm3);
153 return buf;
154}
155
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200156/* Determine if the given CLASSMARK (1/2/3) value permits a given A5/n cipher.
157 * Return 1 when the given A5/n is permitted, 0 when not, and negative if the respective MS CLASSMARK is
158 * not known, where the negative number indicates the classmark type: -2 means Classmark 2 is not
159 * available. */
160static int classmark_supports_a5(const struct gsm_classmark *cm, uint8_t a5)
Harald Welte71330722017-12-23 19:59:02 +0100161{
162 switch (a5) {
163 case 0:
164 /* all phones must implement A5/0, see 3GPP TS 43.020 4.9 */
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200165 return 1;
Harald Welte71330722017-12-23 19:59:02 +0100166 case 1:
167 /* 3GPP TS 43.020 4.9 requires A5/1 to be suppored by all phones and actually states:
168 * "The network shall not provide service to an MS which indicates that it does not
169 * support the ciphering algorithm A5/1.". However, let's be more tolerant based
170 * on policy here */
171 /* See 3GPP TS 24.008 10.5.1.7 */
172 if (!cm->classmark1_set) {
173 DEBUGP(DMSC, "CLASSMARK 1 unknown, assuming MS supports A5/1\n");
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200174 return -1;
Harald Welte71330722017-12-23 19:59:02 +0100175 } else {
176 if (cm->classmark1.a5_1)
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200177 return 0; /* Inverted logic for this bit! */
Harald Welte71330722017-12-23 19:59:02 +0100178 else
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200179 return 1;
Harald Welte71330722017-12-23 19:59:02 +0100180 }
181 break;
182 case 2:
183 case 3:
184 /* See 3GPP TS 24.008 10.5.1.6 */
185 if (cm->classmark2_len < 3) {
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200186 return -2;
Harald Welte71330722017-12-23 19:59:02 +0100187 } else {
188 if (cm->classmark2[2] & (1 << (a5-2)))
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200189 return 1;
Harald Welte71330722017-12-23 19:59:02 +0100190 else
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200191 return 0;
Harald Welte71330722017-12-23 19:59:02 +0100192 }
193 break;
194 case 4:
195 case 5:
196 case 6:
197 case 7:
198 /* See 3GPP TS 24.008 10.5.1.7 */
199 if (cm->classmark3_len < 1) {
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200200 return -3;
Harald Welte71330722017-12-23 19:59:02 +0100201 } else {
202 if (cm->classmark3[0] & (1 << (a5-4)))
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200203 return 1;
Harald Welte71330722017-12-23 19:59:02 +0100204 else
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200205 return 0;
Harald Welte71330722017-12-23 19:59:02 +0100206 }
207 break;
208 default:
209 return false;
210 }
211}
212
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100213int gsm48_conn_sendmsg(struct msgb *msg, struct ran_conn *conn, struct gsm_trans *trans)
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800214{
215 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
216
217 /* if we get passed a transaction reference, do some common
218 * work that the caller no longer has to do */
219 if (trans) {
220 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
Harald Welte0e2fa5d2018-04-09 16:35:01 +0200221 OMSC_LINKID_CB(msg) = trans->dlci;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800222 }
223
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200224 return msc_tx_dtap(conn, msg);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800225}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100226
Harald Welte2483f1b2016-06-19 18:06:02 +0200227/* clear all transactions globally; used in case of MNCC socket disconnect */
Harald Welte371efe52010-12-22 23:17:50 +0100228void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
229{
230 struct gsm_trans *trans, *temp;
231
232 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
233
234 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100235 if (trans->protocol == protocol) {
236 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100237 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100238 }
Harald Welte371efe52010-12-22 23:17:50 +0100239 }
240}
241
Holger Freyther429e7762008-12-30 13:28:30 +0000242/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100243static int gsm0408_loc_upd_rej(struct ran_conn *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000244{
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800245 struct msgb *msg;
246
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800247 msg = gsm48_create_loc_upd_rej(cause);
248 if (!msg) {
249 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
250 return -1;
251 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200252
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200253 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT\n",
254 vlr_subscr_name(conn->vsub));
Harald Welte24ff6ee2009-12-22 00:41:05 +0100255
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800256 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000257}
258
259/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100260static int gsm0408_loc_upd_acc(struct ran_conn *conn,
Harald Welte2483f1b2016-06-19 18:06:02 +0200261 uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000262{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100263 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000264 struct gsm48_hdr *gh;
265 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200266 uint8_t *mid;
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100267 struct osmo_location_area_id laid = {
268 .plmn = conn->network->plmn,
269 .lac = conn->lac,
270 };
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200271
Harald Welte52b1f982008-12-23 20:25:15 +0000272 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
273 gh->proto_discr = GSM48_PDISC_MM;
274 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
275
276 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100277 gsm48_generate_lai2(lai, &laid);
Harald Welte52b1f982008-12-23 20:25:15 +0000278
Harald Welte2483f1b2016-06-19 18:06:02 +0200279 if (send_tmsi == GSM_RESERVED_TMSI) {
280 /* we did not allocate a TMSI to the MS, so we need to
281 * include the IMSI in order for the MS to delete any
282 * old TMSI that might still be allocated */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200283 uint8_t mi[10];
284 int len;
Harald Welte2483f1b2016-06-19 18:06:02 +0200285 len = gsm48_generate_mid_from_imsi(mi, conn->vsub->imsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200286 mid = msgb_put(msg, len);
287 memcpy(mid, mi, len);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200288 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT\n",
289 vlr_subscr_name(conn->vsub));
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200290 } else {
Harald Welte2483f1b2016-06-19 18:06:02 +0200291 /* Include the TMSI, which means that the MS will send a
292 * TMSI REALLOCATION COMPLETE, and we should wait for
293 * that until T3250 expiration */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200294 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Welte2483f1b2016-06-19 18:06:02 +0200295 gsm48_generate_mid_from_tmsi(mid, send_tmsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200296 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT (TMSI = 0x%08x)\n",
297 vlr_subscr_name(conn->vsub),
298 send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200299 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200300 /* TODO: Follow-on proceed */
301 /* TODO: CTS permission */
302 /* TODO: Equivalent PLMNs */
303 /* TODO: Emergency Number List */
304 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000305
Harald Welte52b1f982008-12-23 20:25:15 +0000306
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800307 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000308}
309
Harald Weltebf5e8df2009-02-03 12:59:45 +0000310/* Transmit Chapter 9.2.10 Identity Request */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100311static int mm_tx_identity_req(struct ran_conn *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000312{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100313 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000314 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000315
Harald Welte231ad4f2008-12-27 11:15:38 +0000316 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
317 gh->proto_discr = GSM48_PDISC_MM;
318 gh->msg_type = GSM48_MT_MM_ID_REQ;
319 gh->data[0] = id_type;
320
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800321 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000322}
323
Harald Weltebf5e8df2009-02-03 12:59:45 +0000324/* Parse Chapter 9.2.11 Identity Response */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100325static int mm_rx_id_resp(struct ran_conn *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000326{
327 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyrb62289a2019-01-11 00:04:32 +0100328 uint8_t *mi = gh->data+1;
329 uint8_t mi_len = gh->data[0];
Harald Welte231ad4f2008-12-27 11:15:38 +0000330
Harald Welte2483f1b2016-06-19 18:06:02 +0200331 if (!conn->vsub) {
332 LOGP(DMM, LOGL_ERROR,
333 "Rx MM Identity Response: invalid: no subscriber\n");
334 return -EINVAL;
335 }
336
Neels Hofmeyrb62289a2019-01-11 00:04:32 +0100337 DEBUGP(DMM, "IDENTITY RESPONSE: MI=%s\n", osmo_mi_name(mi, mi_len));
Harald Welte231ad4f2008-12-27 11:15:38 +0000338
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200339 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100340
Neels Hofmeyrb62289a2019-01-11 00:04:32 +0100341 return vlr_subscr_rx_id_resp(conn->vsub, mi, mi_len);
Harald Welte231ad4f2008-12-27 11:15:38 +0000342}
343
Harald Welte2483f1b2016-06-19 18:06:02 +0200344/* Chapter 9.2.15: Receive Location Updating Request.
345 * Keep this function non-static for direct invocation by unit tests. */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100346int mm_rx_loc_upd_req(struct ran_conn *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000347{
Harald Welte2483f1b2016-06-19 18:06:02 +0200348 struct gsm_network *net = conn->network;
Harald Welte8470bf22008-12-25 23:28:35 +0000349 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000350 struct gsm48_loc_upd_req *lu;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200351 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200352 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200353 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte2483f1b2016-06-19 18:06:02 +0200354 uint32_t tmsi;
355 char *imsi;
356 struct osmo_location_area_id old_lai, new_lai;
357 struct osmo_fsm_inst *lu_fsm;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200358 bool is_utran;
Harald Welte52b1f982008-12-23 20:25:15 +0000359
Harald Welte8470bf22008-12-25 23:28:35 +0000360 lu = (struct gsm48_loc_upd_req *) gh->data;
361
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100362 if (ran_conn_is_establishing_auth_ciph(conn)) {
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100363 LOG_RAN_CONN_CAT(conn, DMM, LOGL_ERROR,
364 "Cannot accept another LU, conn already busy establishing authenticity;"
365 " extraneous LOCATION UPDATING REQUEST: MI=%s LU-type=%s\n",
366 osmo_mi_name(lu->mi, lu->mi_len), osmo_lu_type_name(lu->type));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200367 return -EINVAL;
368 }
369
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100370 if (ran_conn_is_accepted(conn)) {
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100371 LOG_RAN_CONN_CAT(conn, DMM, LOGL_ERROR,
372 "Cannot accept another LU, conn already established;"
373 " extraneous LOCATION UPDATING REQUEST: MI=%s LU-type=%s\n",
374 osmo_mi_name(lu->mi, lu->mi_len), osmo_lu_type_name(lu->type));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200375 return -EINVAL;
376 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200377
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100378 conn->complete_layer3_type = COMPLETE_LAYER3_LU;
379 ran_conn_update_id_from_mi(conn, lu->mi, lu->mi_len);
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200380
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100381 LOG_RAN_CONN_CAT(conn, DMM, LOGL_DEBUG, "LOCATION UPDATING REQUEST: MI=%s LU-type=%s\n",
382 osmo_mi_name(lu->mi, lu->mi_len), osmo_lu_type_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000383
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200384 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100385
Harald Welte24ff6ee2009-12-22 00:41:05 +0100386 switch (lu->type) {
387 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200388 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200389 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100390 break;
391 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200392 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200393 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100394 break;
395 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200396 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200397 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100398 break;
399 }
400
Harald Welte2483f1b2016-06-19 18:06:02 +0200401 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
402 /* TODO: 10.5.3.14 Aditional update parameters (CS fallback calls) */
403 /* TODO: 10.5.7.8 Device properties */
404 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200405
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100406 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
407 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Welte52b1f982008-12-23 20:25:15 +0000408 switch (mi_type) {
409 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200410 tmsi = GSM_RESERVED_TMSI;
411 imsi = mi_string;
Harald Welte4b634542008-12-27 01:55:51 +0000412 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000413 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200414 tmsi = tmsi_from_string(mi_string);
415 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000416 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200417 default:
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100418 LOG_RAN_CONN_CAT(conn, DMM, LOGL_ERROR, "unknown mobile identity type\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200419 tmsi = GSM_RESERVED_TMSI;
420 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000421 break;
422 }
423
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100424 gsm48_decode_lai2(&lu->lai, &old_lai);
425 new_lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200426 new_lai.lac = conn->lac;
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100427 LOG_RAN_CONN_CAT(conn, DMM, LOGL_DEBUG, "LU/new-LAC: %u/%u\n", old_lai.lac, new_lai.lac);
Harald Welte24516ea2009-07-04 10:18:00 +0200428
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100429 is_utran = (conn->via_ran == OSMO_RAT_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200430 lu_fsm = vlr_loc_update(conn->fi,
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100431 RAN_CONN_E_ACCEPTED, RAN_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +0200432 net->vlr, conn, vlr_lu_type, tmsi, imsi,
433 &old_lai, &new_lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200434 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100435 is_utran || conn->network->a5_encryption_mask > 0x01,
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200436 classmark1_is_r99(&lu->classmark1),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200437 is_utran,
Harald Welte2483f1b2016-06-19 18:06:02 +0200438 net->vlr->cfg.assign_tmsi);
439 if (!lu_fsm) {
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100440 LOG_RAN_CONN(conn, LOGL_ERROR, "Can't start LU FSM\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200441 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800442 }
443
Harald Welte2483f1b2016-06-19 18:06:02 +0200444 /* From vlr_loc_update() we expect an implicit dispatch of
445 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
446 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000447
Harald Welte2483f1b2016-06-19 18:06:02 +0200448 if (!conn->vsub || conn->vsub->lu_fsm != lu_fsm) {
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100449 LOG_RAN_CONN(conn, LOGL_ERROR, "internal error during Location Updating attempt\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200450 return -EIO;
451 }
452
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200453 conn->vsub->classmark.classmark1 = lu->classmark1;
454 conn->vsub->classmark.classmark1_set = true;
455
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100456 ran_conn_complete_layer_3(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +0200457 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000458}
459
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100460/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200461/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100462static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800463{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100464 uint8_t ret;
465
466 ret = value / 10;
467 ret |= (value % 10) << 4;
468
469 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800470}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100471
Harald Welte0df904d2018-12-03 11:00:04 +0100472/* Generate a message buffer that contains a valid MM info message,
473 * See also 3GPP TS 24.008, chapter 9.2.15a */
474struct msgb *gsm48_create_mm_info(struct gsm_network *net)
Harald Weltedb253af2008-12-30 17:56:55 +0000475{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100476 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000477 struct gsm48_hdr *gh;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200478 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200479 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100480
Harald Welte4bfdfe72009-06-10 23:11:52 +0800481 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100482 struct tm* gmt_time;
483 struct tm* local_time;
484 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100485 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000486
Harald Weltedb253af2008-12-30 17:56:55 +0000487 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
488 gh->proto_discr = GSM48_PDISC_MM;
489 gh->msg_type = GSM48_MT_MM_INFO;
490
491 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200492#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000493 name_len = strlen(net->name_long);
494 /* 10.5.3.5a */
495 ptr8 = msgb_put(msg, 3);
496 ptr8[0] = GSM48_IE_NAME_LONG;
497 ptr8[1] = name_len*2 +1;
498 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
499
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200500 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000501 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000502 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000503
504 /* FIXME: Use Cell Broadcast, not UCS-2, since
505 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200506#endif
507 name_len = (strlen(net->name_long)*7)/8;
508 name_pad = (8 - strlen(net->name_long)*7)%8;
509 if (name_pad > 0)
510 name_len++;
511 /* 10.5.3.5a */
512 ptr8 = msgb_put(msg, 3);
513 ptr8[0] = GSM48_IE_NAME_LONG;
514 ptr8[1] = name_len +1;
515 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
516
517 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100518 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200519
Harald Weltedb253af2008-12-30 17:56:55 +0000520 }
521
522 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200523#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000524 name_len = strlen(net->name_short);
525 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200526 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200527 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000528 ptr8[1] = name_len*2 + 1;
529 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
530
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200531 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000532 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000533 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200534#endif
535 name_len = (strlen(net->name_short)*7)/8;
536 name_pad = (8 - strlen(net->name_short)*7)%8;
537 if (name_pad > 0)
538 name_len++;
539 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200540 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200541 ptr8[0] = GSM48_IE_NAME_SHORT;
542 ptr8[1] = name_len +1;
543 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
544
545 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100546 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200547
Harald Weltedb253af2008-12-30 17:56:55 +0000548 }
549
Harald Weltedb253af2008-12-30 17:56:55 +0000550 /* Section 10.5.3.9 */
551 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100552 gmt_time = gmtime(&cur_t);
553
Harald Weltedb253af2008-12-30 17:56:55 +0000554 ptr8 = msgb_put(msg, 8);
555 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100556 ptr8[1] = bcdify(gmt_time->tm_year % 100);
557 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
558 ptr8[3] = bcdify(gmt_time->tm_mday);
559 ptr8[4] = bcdify(gmt_time->tm_hour);
560 ptr8[5] = bcdify(gmt_time->tm_min);
561 ptr8[6] = bcdify(gmt_time->tm_sec);
562
Neels Hofmeyr73983952016-05-10 13:29:33 +0200563 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200564 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200565 if (net->tz.hr < 0) {
566 tzunits = ((net->tz.hr/-1)*4);
567 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100568 ptr8[7] = bcdify(tzunits);
569 /* Set negative time */
570 ptr8[7] |= 0x08;
571 }
572 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200573 tzunits = net->tz.hr*4;
574 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100575 ptr8[7] = bcdify(tzunits);
576 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100577 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200578 if (net->tz.dst >= 0 && net->tz.dst <= 2)
579 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100580 }
581 else {
582 /* Need to get GSM offset and convert into 15 min units */
583 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
Harald Welte9c3dc902012-04-08 16:59:24 +0200584#ifdef HAVE_TM_GMTOFF_IN_TM
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200585 local_time = localtime(&cur_t);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100586 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200587#else
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200588 /* find timezone offset */
589 time_t utc;
590 double offsetFromUTC;
591 utc = mktime(gmt_time);
592 local_time = localtime(&cur_t);
593 offsetFromUTC = difftime(cur_t, utc);
594 if (local_time->tm_isdst)
595 offsetFromUTC += 3600.0;
596 tzunits = ((int)offsetFromUTC) / 60 / 15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200597#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100598 if (tzunits < 0) {
599 tzunits = tzunits/-1;
600 ptr8[7] = bcdify(tzunits);
601 /* Flip it to negative */
602 ptr8[7] |= 0x08;
603 }
604 else
605 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100606
607 /* Does not support DST +2 */
608 if (local_time->tm_isdst)
609 dst = 1;
610 }
611
612 if (dst) {
613 ptr8 = msgb_put(msg, 3);
614 ptr8[0] = GSM48_IE_NET_DST;
615 ptr8[1] = 1;
616 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100617 }
Harald Weltedb253af2008-12-30 17:56:55 +0000618
Harald Welte0df904d2018-12-03 11:00:04 +0100619 return msg;
620}
Daniel Willmanneea93372009-08-13 03:42:07 +0200621
Harald Welte0df904d2018-12-03 11:00:04 +0100622/* Section 9.2.15a */
623int gsm48_tx_mm_info(struct ran_conn *conn)
624{
625 struct gsm_network *net = conn->network;
626 struct msgb *msg;
627
628 msg = gsm48_create_mm_info(net);
629
630 LOG_RAN_CONN(conn, LOGL_DEBUG, "Tx MM INFO\n");
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800631 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000632}
633
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100634/*! Send an Authentication Request to MS on the given RAN connection
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100635 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
636 * \param[in] conn Subscriber connection to send on.
637 * \param[in] rand Random challenge token to send, must be 16 bytes long.
638 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
639 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
640 * \param[in] key_seq auth tuple's sequence number.
641 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100642int gsm48_tx_mm_auth_req(struct ran_conn *conn, uint8_t *rand,
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100643 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200644{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100645 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200646 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200647 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200648
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100649 DEBUGP(DMM, "Tx AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100650 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100651 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200652
Harald Welte7984d5c2009-08-12 22:56:50 +0200653 gh->proto_discr = GSM48_PDISC_MM;
654 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
655
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100656 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200657
Harald Welte7984d5c2009-08-12 22:56:50 +0200658 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100659 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200660 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200661 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200662
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100663
664 /* 16 bytes AUTN */
665 if (autn)
666 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
667
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800668 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200669}
670
671/* Section 9.2.1 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100672int gsm48_tx_mm_auth_rej(struct ran_conn *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200673{
674 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800675 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200676}
677
Harald Welte2483f1b2016-06-19 18:06:02 +0200678static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200679static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum gsm48_reject_value result);
Harald Welte2483f1b2016-06-19 18:06:02 +0200680
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100681static int cm_serv_reuse_conn(struct ran_conn *conn, const uint8_t *mi_lv)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100682{
Harald Welte2483f1b2016-06-19 18:06:02 +0200683 uint8_t mi_type;
684 char mi_string[GSM48_MI_SIZE];
685 uint32_t tmsi;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100686
Harald Welte2483f1b2016-06-19 18:06:02 +0200687 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
688 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100689
Harald Welte2483f1b2016-06-19 18:06:02 +0200690 switch (mi_type) {
691 case GSM_MI_TYPE_IMSI:
692 if (vlr_subscr_matches_imsi(conn->vsub, mi_string))
693 goto accept_reuse;
694 break;
695 case GSM_MI_TYPE_TMSI:
696 tmsi = osmo_load32be(mi_lv+2);
697 if (vlr_subscr_matches_tmsi(conn->vsub, tmsi))
698 goto accept_reuse;
699 break;
700 case GSM_MI_TYPE_IMEI:
701 if (vlr_subscr_matches_imei(conn->vsub, mi_string))
702 goto accept_reuse;
703 break;
704 default:
705 break;
706 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100707
Harald Welte2483f1b2016-06-19 18:06:02 +0200708 LOGP(DMM, LOGL_ERROR, "%s: CM Service Request with mismatching mobile identity: %s %s\n",
709 vlr_subscr_name(conn->vsub), gsm48_mi_type_name(mi_type), mi_string);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200710 msc_vlr_tx_cm_serv_rej(conn, GSM48_REJECT_ILLEGAL_MS);
Harald Welte2483f1b2016-06-19 18:06:02 +0200711 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100712
Harald Welte2483f1b2016-06-19 18:06:02 +0200713accept_reuse:
714 DEBUGP(DMM, "%s: re-using already accepted connection\n",
715 vlr_subscr_name(conn->vsub));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200716
717 if (!conn->received_cm_service_request) {
718 conn->received_cm_service_request = true;
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +0100719 ran_conn_get(conn, RAN_CONN_USE_CM_SERVICE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200720 }
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100721 ran_conn_update_id(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +0200722 return conn->network->vlr->ops.tx_cm_serv_acc(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100723}
724
Harald Welte4ed0e922009-01-10 03:17:30 +0000725/*
726 * Handle CM Service Requests
727 * a) Verify that the packet is long enough to contain the information
728 * we require otherwsie reject with INCORRECT_MESSAGE
729 * b) Try to parse the TMSI. If we do not have one reject
730 * c) Check that we know the subscriber with the TMSI otherwise reject
731 * with a HLR cause
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200732 * d) Set the subscriber on the conn and accept
Harald Welte2483f1b2016-06-19 18:06:02 +0200733 *
734 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000735 */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100736int gsm48_rx_mm_serv_req(struct ran_conn *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000737{
Harald Welte2483f1b2016-06-19 18:06:02 +0200738 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200739 uint8_t mi_type;
Harald Welteba4cf162009-01-10 01:49:35 +0000740 struct gsm48_hdr *gh = msgb_l3(msg);
741 struct gsm48_service_request *req =
742 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800743 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200744 uint8_t classmark2_len = gh->data[1];
745 uint8_t *classmark2 = gh->data+2;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200746 uint8_t *mi_p = classmark2 + classmark2_len;
747 uint8_t mi_len = *mi_p;
748 uint8_t *mi = mi_p + 1;
Harald Welte2483f1b2016-06-19 18:06:02 +0200749 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200750 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200751
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100752 lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200753 lai.lac = conn->lac;
Harald Welteba4cf162009-01-10 01:49:35 +0000754
755 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100756 LOG_RAN_CONN(conn, LOGL_ERROR, "Rx CM SERVICE REQUEST: wrong message size (%u < %zu)\n",
757 msg->data_len, sizeof(struct gsm48_service_request*));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200758 return msc_gsm48_tx_mm_serv_rej(conn,
759 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000760 }
761
762 if (msg->data_len < req->mi_len + 6) {
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100763 LOG_RAN_CONN(conn, LOGL_ERROR, "Rx CM SERVICE REQUEST: message does not fit in packet\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200764 return msc_gsm48_tx_mm_serv_rej(conn,
765 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000766 }
767
Neels Hofmeyr7ce21dc2019-01-04 17:42:05 +0100768 if (ran_conn_is_establishing_auth_ciph(conn)) {
769 LOG_RAN_CONN(conn, LOGL_ERROR,
770 "Cannot accept CM Service Request, conn already busy establishing authenticity\n");
771 msc_vlr_tx_cm_serv_rej(conn, GSM48_REJECT_CONGESTION);
772 return -EINVAL;
773 /* or should we accept and note down the service request anyway? */
774 }
775
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100776 conn->complete_layer3_type = COMPLETE_LAYER3_CM_SERVICE_REQ;
777 ran_conn_update_id_from_mi(conn, mi, mi_len);
778 LOG_RAN_CONN_CAT(conn, DMM, LOGL_DEBUG, "Rx CM SERVICE REQUEST cm_service_type=0x%02x\n",
779 req->cm_service_type);
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100780
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100781 mi_type = (mi && mi_len) ? (mi[0] & GSM_MI_TYPE_MASK) : GSM_MI_TYPE_NONE;
Harald Welte3995d2e2018-01-24 11:23:54 +0100782 switch (mi_type) {
783 case GSM_MI_TYPE_IMSI:
784 case GSM_MI_TYPE_TMSI:
785 /* continue below */
786 break;
787 case GSM_MI_TYPE_IMEI:
788 if (req->cm_service_type == GSM48_CMSERV_EMERGENCY) {
789 /* We don't do emergency calls by IMEI */
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100790 LOG_RAN_CONN(conn, LOGL_NOTICE, "Tx CM SERVICE REQUEST REJECT\n");
Harald Welte3995d2e2018-01-24 11:23:54 +0100791 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_IMEI_NOT_ACCEPTED);
792 }
793 /* fall-through for non-emergency setup */
794 default:
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100795 LOG_RAN_CONN(conn, LOGL_ERROR, "MI type is not expected: %s\n", gsm48_mi_type_name(mi_type));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200796 return msc_gsm48_tx_mm_serv_rej(conn,
797 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000798 }
799
Harald Welte37382ec2018-01-24 12:49:34 +0100800 switch (req->cm_service_type) {
801 case GSM48_CMSERV_MO_CALL_PACKET:
802 case GSM48_CMSERV_EMERGENCY:
803 case GSM48_CMSERV_SMS:
804 case GSM48_CMSERV_SUP_SERV:
805 /* continue below */
806 break;
807 default:
808 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
809 }
810
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100811 if (ran_conn_is_accepted(conn))
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200812 return cm_serv_reuse_conn(conn, mi_p);
Harald Welte2483f1b2016-06-19 18:06:02 +0200813
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200814 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, mi_p);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200815
Neels Hofmeyr7814a832018-12-26 00:40:18 +0100816 is_utran = (conn->via_ran == OSMO_RAT_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200817 vlr_proc_acc_req(conn->fi,
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100818 RAN_CONN_E_ACCEPTED, RAN_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +0200819 net->vlr, conn,
820 VLR_PR_ARQ_T_CM_SERV_REQ, mi-1, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200821 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100822 is_utran || conn->network->a5_encryption_mask > 0x01,
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200823 classmark2_is_r99(classmark2, classmark2_len),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200824 is_utran);
Holger Freythereb443982009-06-04 13:58:42 +0000825
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200826 /* From vlr_proc_acc_req() we expect an implicit dispatch of PR_ARQ_E_START we expect
827 * msc_vlr_subscr_assoc() to already have been called and completed. Has an error occured? */
828 if (!conn->vsub) {
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100829 LOG_RAN_CONN(conn, LOGL_ERROR, "subscriber not allowed to do a CM Service Request\n");
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200830 return -EIO;
831 }
832
833 memcpy(conn->vsub->classmark.classmark2, classmark2, classmark2_len);
834 conn->vsub->classmark.classmark2_len = classmark2_len;
835
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100836 ran_conn_complete_layer_3(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +0200837 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000838}
839
Harald Welte49f8fcb2018-01-24 21:40:19 +0100840/* Receive a CM Re-establish Request */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100841static int gsm48_rx_cm_reest_req(struct ran_conn *conn, struct msgb *msg)
Harald Welte49f8fcb2018-01-24 21:40:19 +0100842{
843 uint8_t mi_type;
844 char mi_string[GSM48_MI_SIZE];
845 struct gsm48_hdr *gh = msgb_l3(msg);
846
847 uint8_t classmark2_len = gh->data[1];
848 uint8_t *classmark2 = gh->data+2;
849 uint8_t mi_len = *(classmark2 + classmark2_len);
850 uint8_t *mi = (classmark2 + classmark2_len + 1);
851
852 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
853 mi_type = mi[0] & GSM_MI_TYPE_MASK;
854 DEBUGP(DMM, "<- CM RE-ESTABLISH REQUEST MI(%s)=%s\n", gsm48_mi_type_name(mi_type), mi_string);
855
856 /* we don't support CM call re-establishment */
857 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
858}
859
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100860static int gsm48_rx_mm_imsi_detach_ind(struct ran_conn *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000861{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200862 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +0000863 struct gsm48_hdr *gh = msgb_l3(msg);
864 struct gsm48_imsi_detach_ind *idi =
865 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200866 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200867 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200868 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000869
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200870 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200871 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
872 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000873
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200874 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100875
Harald Welte2a139372009-02-22 21:14:55 +0000876 switch (mi_type) {
877 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200878 vsub = vlr_subscr_find_by_tmsi(network->vlr,
879 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000880 break;
881 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200882 vsub = vlr_subscr_find_by_imsi(network->vlr, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000883 break;
884 case GSM_MI_TYPE_IMEI:
885 case GSM_MI_TYPE_IMEISV:
886 /* no sim card... FIXME: what to do ? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200887 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
888 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000889 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200890 default:
Harald Welte2483f1b2016-06-19 18:06:02 +0200891 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
892 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000893 break;
894 }
895
Harald Welte2483f1b2016-06-19 18:06:02 +0200896 if (!vsub) {
897 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
898 gsm48_mi_type_name(mi_type), mi_string);
899 } else {
900 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
Neels Hofmeyrfacd57a2017-12-15 03:48:48 +0100901
902 if (vsub->cs.is_paging)
903 subscr_paging_cancel(vsub, GSM_PAGING_EXPIRED);
904
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200905 /* We already got Classmark 1 during Location Updating ... but well, ok */
906 vsub->classmark.classmark1 = idi->classmark1;
907
Harald Welte2483f1b2016-06-19 18:06:02 +0200908 vlr_subscr_rx_imsi_detach(vsub);
909 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
910 vlr_subscr_put(vsub);
911 }
Harald Welte2a139372009-02-22 21:14:55 +0000912
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100913 ran_conn_close(conn, 0);
Harald Welte2a139372009-02-22 21:14:55 +0000914 return 0;
915}
916
Harald Welted2a7f5a2009-06-05 20:08:20 +0000917static int gsm48_rx_mm_status(struct msgb *msg)
918{
919 struct gsm48_hdr *gh = msgb_l3(msg);
920
921 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
922
923 return 0;
924}
925
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100926static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100927 struct ran_conn *conn,
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100928 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100929{
930 struct gsm48_hdr *gh = msgb_l3(msg);
931 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100932
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100933 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
934 LOGP(DMM, LOGL_ERROR,
935 "%s: MM AUTHENTICATION RESPONSE:"
936 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200937 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100938 return -EINVAL;
939 }
940
941 *res_len = sizeof(ar->sres);
942 memcpy(res, ar->sres, sizeof(ar->sres));
943 return 0;
944}
945
946static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100947 struct ran_conn *conn,
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100948 struct msgb *msg)
949{
950 struct gsm48_hdr *gh;
951 uint8_t *data;
952 uint8_t iei;
953 uint8_t ie_len;
954 unsigned int data_len;
955
956 /* First parse the GSM part */
957 if (parse_gsm_auth_resp(res, res_len, conn, msg))
958 return -EINVAL;
959 OSMO_ASSERT(*res_len == 4);
960
961 /* Then add the extended res part */
962 gh = msgb_l3(msg);
963 data = gh->data + sizeof(struct gsm48_auth_resp);
964 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
965
966 if (data_len < 3) {
967 LOGP(DMM, LOGL_ERROR,
968 "%s: MM AUTHENTICATION RESPONSE:"
969 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200970 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100971 return -EINVAL;
972 }
973
974 iei = data[0];
975 ie_len = data[1];
976 if (iei != GSM48_IE_AUTH_RES_EXT) {
977 LOGP(DMM, LOGL_ERROR,
978 "%s: MM R99 AUTHENTICATION RESPONSE:"
979 " expected IEI 0x%02x, got 0x%02x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200980 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100981 GSM48_IE_AUTH_RES_EXT, iei);
982 return -EINVAL;
983 }
984
985 if (ie_len > 12) {
986 LOGP(DMM, LOGL_ERROR,
987 "%s: MM R99 AUTHENTICATION RESPONSE:"
988 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200989 vlr_subscr_name(conn->vsub), GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100990 return -EINVAL;
991 }
992
993 *res_len += ie_len;
994 memcpy(res + 4, &data[2], ie_len);
995 return 0;
996}
997
998/* Chapter 9.2.3: Authentication Response */
Neels Hofmeyrc036b792018-11-29 22:37:51 +0100999static int gsm48_rx_mm_auth_resp(struct ran_conn *conn, struct msgb *msg)
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001000{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001001 uint8_t res[16];
1002 uint8_t res_len;
1003 int rc;
Neels Hofmeyr25f69d52018-03-10 03:44:06 +01001004 bool is_umts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001005
Harald Welte2483f1b2016-06-19 18:06:02 +02001006 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001007 LOGP(DMM, LOGL_ERROR,
1008 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001009 ran_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001010 return -EINVAL;
1011 }
1012
Neels Hofmeyr25f69d52018-03-10 03:44:06 +01001013 is_umts = (msgb_l3len(msg) > sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp));
1014
1015 if (is_umts)
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001016 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
Neels Hofmeyr25f69d52018-03-10 03:44:06 +01001017 else
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001018 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001019
1020 if (rc) {
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +01001021 LOGP(DMM, LOGL_ERROR,
1022 "%s: MM AUTHENTICATION RESPONSE: invalid: parsing %s AKA Auth Response"
1023 " failed with rc=%d; dispatching zero length SRES/RES to trigger failure\n",
Neels Hofmeyr25f69d52018-03-10 03:44:06 +01001024 vlr_subscr_name(conn->vsub), is_umts ? "UMTS" : "GSM", rc);
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +01001025 memset(res, 0, sizeof(res));
1026 res_len = 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001027 }
1028
1029 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001030 vlr_subscr_name(conn->vsub),
Neels Hofmeyrd97821f2018-03-10 04:51:39 +01001031 is_umts ? "UMTS" : "GSM", is_umts ? "res" : "sres",
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001032 osmo_hexdump_nospc(res, res_len));
1033
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001034 return vlr_subscr_rx_auth_resp(conn->vsub, classmark_is_r99(&conn->vsub->classmark),
Neels Hofmeyr7814a832018-12-26 00:40:18 +01001035 conn->via_ran == OSMO_RAT_UTRAN_IU,
Harald Welte2483f1b2016-06-19 18:06:02 +02001036 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001037}
1038
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001039static int gsm48_rx_mm_auth_fail(struct ran_conn *conn, struct msgb *msg)
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001040{
1041 struct gsm48_hdr *gh = msgb_l3(msg);
1042 uint8_t cause;
1043 uint8_t auts_tag;
1044 uint8_t auts_len;
1045 uint8_t *auts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001046
Harald Welte2483f1b2016-06-19 18:06:02 +02001047 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001048 LOGP(DMM, LOGL_ERROR,
1049 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001050 ran_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001051 return -EINVAL;
1052 }
1053
1054 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
1055 LOGP(DMM, LOGL_ERROR,
1056 "%s: MM R99 AUTHENTICATION FAILURE:"
1057 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001058 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001059 ran_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001060 return -EINVAL;
1061 }
1062
1063 cause = gh->data[0];
1064
1065 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
1066 LOGP(DMM, LOGL_INFO,
1067 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001068 vlr_subscr_name(conn->vsub), cause);
1069 vlr_subscr_rx_auth_fail(conn->vsub, NULL);
1070 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001071 }
1072
1073 /* This is a Synch Failure procedure, which should pass an AUTS to
1074 * resynchronize the sequence nr with the HLR. Expecting exactly one
1075 * TLV with 14 bytes of AUTS. */
1076
1077 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
1078 LOGP(DMM, LOGL_INFO,
1079 "%s: MM R99 AUTHENTICATION FAILURE:"
1080 " invalid Synch Failure: missing AUTS IE\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001081 vlr_subscr_name(conn->vsub));
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001082 ran_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001083 return -EINVAL;
1084 }
1085
1086 auts_tag = gh->data[1];
1087 auts_len = gh->data[2];
1088 auts = &gh->data[3];
1089
1090 if (auts_tag != GSM48_IE_AUTS
1091 || auts_len != 14) {
1092 LOGP(DMM, LOGL_INFO,
1093 "%s: MM R99 AUTHENTICATION FAILURE:"
1094 " invalid Synch Failure:"
1095 " expected AUTS IE 0x%02x of 14 bytes,"
1096 " got IE 0x%02x of %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001097 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001098 GSM48_IE_AUTS, auts_tag, auts_len);
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001099 ran_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001100 return -EINVAL;
1101 }
1102
1103 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
1104 LOGP(DMM, LOGL_INFO,
1105 "%s: MM R99 AUTHENTICATION FAILURE:"
1106 " invalid Synch Failure msg: message truncated (%u)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001107 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001108 ran_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001109 return -EINVAL;
1110 }
1111
1112 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
1113 * large enough. */
1114
1115 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001116 vlr_subscr_name(conn->vsub), osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001117
Harald Welte2483f1b2016-06-19 18:06:02 +02001118 return vlr_subscr_rx_auth_fail(conn->vsub, auts);
1119}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001120
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001121static int gsm48_rx_mm_tmsi_reall_compl(struct ran_conn *conn)
Harald Welte2483f1b2016-06-19 18:06:02 +02001122{
1123 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1124 vlr_subscr_name(conn->vsub));
1125 if (!conn->vsub) {
1126 LOGP(DMM, LOGL_ERROR,
1127 "Rx MM TMSI Reallocation Complete: invalid: no subscriber\n");
1128 return -EINVAL;
1129 }
1130 return vlr_subscr_rx_tmsi_reall_compl(conn->vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001131}
1132
Harald Weltebf5e8df2009-02-03 12:59:45 +00001133/* Receive a GSM 04.08 Mobility Management (MM) message */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001134static int gsm0408_rcv_mm(struct ran_conn *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001135{
1136 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001137 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001138
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001139 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001140 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001141 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001142 break;
1143 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001144 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001145 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001146 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001147 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001148 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001149 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001150 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001151 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001152 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte2483f1b2016-06-19 18:06:02 +02001153 rc = gsm48_rx_mm_tmsi_reall_compl(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001154 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001155 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001156 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001157 break;
1158 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01001159 rc = gsm48_rx_cm_reest_req(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001160 break;
1161 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001162 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001163 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001164 case GSM48_MT_MM_AUTH_FAIL:
1165 rc = gsm48_rx_mm_auth_fail(conn, msg);
1166 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001167 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001168 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001169 gh->msg_type);
1170 break;
1171 }
1172
1173 return rc;
1174}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001175
Harald Welte2d35ae62009-02-06 12:02:13 +00001176/* Receive a PAGING RESPONSE message from the MS */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001177static int gsm48_rx_rr_pag_resp(struct ran_conn *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001178{
Harald Welte2483f1b2016-06-19 18:06:02 +02001179 struct gsm_network *net = conn->network;
Harald Welte2d35ae62009-02-06 12:02:13 +00001180 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001181 uint8_t classmark2_len = gh->data[1];
1182 uint8_t *classmark2 = gh->data+2;
1183 uint8_t *mi_lv = classmark2 + classmark2_len;
Harald Welte2483f1b2016-06-19 18:06:02 +02001184 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001185 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +02001186
Neels Hofmeyr379d5792018-02-22 04:04:54 +01001187 lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001188 lai.lac = conn->lac;
Harald Welte2d35ae62009-02-06 12:02:13 +00001189
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001190 if (ran_conn_is_establishing_auth_ciph(conn)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001191 LOGP(DMM, LOGL_ERROR,
1192 "Ignoring Paging Response, conn already busy establishing authenticity\n");
1193 return 0;
1194 }
1195
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001196 if (ran_conn_is_accepted(conn)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001197 LOGP(DMM, LOGL_ERROR, "Ignoring Paging Response, conn already established\n");
1198 return 0;
1199 }
1200
Neels Hofmeyr361e5712019-01-03 02:32:14 +01001201 conn->complete_layer3_type = COMPLETE_LAYER3_PAGING_RESP;
1202 ran_conn_update_id_from_mi(conn, mi_lv + 1, *mi_lv);
1203 LOG_RAN_CONN_CAT(conn, DRR, LOGL_DEBUG, "PAGING RESPONSE\n");
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001204
Neels Hofmeyr7814a832018-12-26 00:40:18 +01001205 is_utran = (conn->via_ran == OSMO_RAT_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +02001206 vlr_proc_acc_req(conn->fi,
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001207 RAN_CONN_E_ACCEPTED, RAN_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +02001208 net->vlr, conn,
1209 VLR_PR_ARQ_T_PAGING_RESP, mi_lv, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001210 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +01001211 is_utran || conn->network->a5_encryption_mask > 0x01,
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001212 classmark2_is_r99(classmark2, classmark2_len),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001213 is_utran);
Holger Freyther053e09d2009-02-14 22:51:06 +00001214
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001215 /* From vlr_proc_acc_req() we expect an implicit dispatch of PR_ARQ_E_START we expect
1216 * msc_vlr_subscr_assoc() to already have been called and completed. Has an error occured? */
1217 if (!conn->vsub) {
Neels Hofmeyr46c06e22019-01-04 17:42:05 +01001218 LOG_RAN_CONN(conn, LOGL_ERROR, "subscriber not allowed to do a Paging Response\n");
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001219 return -EIO;
1220 }
1221
1222 memcpy(conn->vsub->classmark.classmark2, classmark2, classmark2_len);
1223 conn->vsub->classmark.classmark2_len = classmark2_len;
1224
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001225 ran_conn_complete_layer_3(conn);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001226 return 0;
Harald Welte2d35ae62009-02-06 12:02:13 +00001227}
1228
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001229static int gsm48_rx_rr_app_info(struct ran_conn *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001230{
1231 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001232 uint8_t apdu_id_flags;
1233 uint8_t apdu_len;
1234 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001235
1236 apdu_id_flags = gh->data[0];
1237 apdu_len = gh->data[1];
1238 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001239
Max8dc8f232017-02-09 19:13:02 +01001240 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001241 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001242
Harald Welte2483f1b2016-06-19 18:06:02 +02001243 /* we're not using the app info blob anywhere, so ignore. */
1244#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001245 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte2483f1b2016-06-19 18:06:02 +02001246#else
1247 return 0;
1248#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001249}
1250
Harald Weltebf5e8df2009-02-03 12:59:45 +00001251/* Receive a GSM 04.08 Radio Resource (RR) message */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001252static int gsm0408_rcv_rr(struct ran_conn *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001253{
1254 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001255 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001256
1257 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001258 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001259 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001260 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001261 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001262 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001263 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001264 default:
Philippd37981e2016-10-10 15:11:41 +02001265 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1266 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001267 break;
1268 }
1269
Harald Welte2d35ae62009-02-06 12:02:13 +00001270 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001271}
1272
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001273int gsm48_send_rr_app_info(struct ran_conn *conn, uint8_t apdu_id,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001274 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001275{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001276 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001277 struct gsm48_hdr *gh;
1278
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001279 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1280 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001281
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001282 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1283 gh->proto_discr = GSM48_PDISC_RR;
1284 gh->msg_type = GSM48_MT_RR_APP_INFO;
1285 gh->data[0] = apdu_id;
1286 gh->data[1] = apdu_len;
1287 memcpy(gh->data+2, apdu, apdu_len);
1288
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001289 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001290}
1291
Harald Welte2483f1b2016-06-19 18:06:02 +02001292static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08001293{
Harald Welte2483f1b2016-06-19 18:06:02 +02001294 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
1295 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08001296
Harald Welte2483f1b2016-06-19 18:06:02 +02001297 switch (pdisc) {
1298 case GSM48_PDISC_MM:
1299 switch (msg_type) {
1300 case GSM48_MT_MM_LOC_UPD_REQUEST:
1301 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01001302 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte2483f1b2016-06-19 18:06:02 +02001303 case GSM48_MT_MM_AUTH_RESP:
1304 case GSM48_MT_MM_AUTH_FAIL:
1305 case GSM48_MT_MM_ID_RESP:
1306 case GSM48_MT_MM_TMSI_REALL_COMPL:
1307 case GSM48_MT_MM_IMSI_DETACH_IND:
1308 return true;
1309 default:
1310 break;
1311 }
1312 break;
1313 case GSM48_PDISC_RR:
1314 switch (msg_type) {
Neels Hofmeyr325f1062018-12-07 16:39:52 +01001315 /* GSM48_MT_RR_CIPH_M_COMPL is actually handled in bssmap_rx_ciph_compl() and gets redirected in the
1316 * BSSAP layer to ran_conn_cipher_mode_compl() (before this here is reached) */
Harald Welte2483f1b2016-06-19 18:06:02 +02001317 case GSM48_MT_RR_PAG_RESP:
1318 return true;
1319 default:
1320 break;
1321 }
1322 break;
1323 default:
1324 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02001325 }
1326
Harald Welte2483f1b2016-06-19 18:06:02 +02001327 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02001328}
1329
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001330void cm_service_request_concludes(struct ran_conn *conn,
Harald Welte2483f1b2016-06-19 18:06:02 +02001331 struct msgb *msg)
1332{
1333
1334 /* If a CM Service Request was received before, this is the request the
1335 * conn was opened for. No need to wait for further messages. */
1336
1337 if (!conn->received_cm_service_request)
1338 return;
1339
1340 if (log_check_level(DMM, LOGL_DEBUG)) {
1341 struct gsm48_hdr *gh = msgb_l3(msg);
1342 uint8_t pdisc = gsm48_hdr_pdisc(gh);
1343 uint8_t msg_type = gsm48_hdr_msg_type(gh);
1344
Neels Hofmeyr78ada642017-03-10 02:15:20 +01001345 DEBUGP(DMM, "%s: rx msg %s:"
Harald Welte2483f1b2016-06-19 18:06:02 +02001346 " received_cm_service_request changes to false\n",
1347 vlr_subscr_name(conn->vsub),
Neels Hofmeyr78ada642017-03-10 02:15:20 +01001348 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Harald Welte2483f1b2016-06-19 18:06:02 +02001349 }
1350 conn->received_cm_service_request = false;
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +01001351 ran_conn_put(conn, RAN_CONN_USE_CM_SERVICE);
Harald Welte2483f1b2016-06-19 18:06:02 +02001352}
1353
Harald Weltec2007852018-02-03 21:08:26 +01001354/* TS 24.007 11.2.3.2.3 Message Type Octet / Duplicate Detection */
1355int gsm0407_pdisc_ctr_bin(uint8_t pdisc)
1356{
1357 switch (pdisc) {
1358 case GSM48_PDISC_MM:
1359 case GSM48_PDISC_CC:
1360 case GSM48_PDISC_NC_SS:
1361 return 0;
1362 case GSM48_PDISC_GROUP_CC:
1363 return 1;
1364 case GSM48_PDISC_BCAST_CC:
1365 return 2;
1366 case GSM48_PDISC_LOC:
1367 return 3;
1368 default:
1369 return -1;
1370 }
1371}
1372
1373/* extract the N(SD) and return the modulo value for a R98 message */
1374static uint8_t gsm0407_determine_nsd_ret_modulo_r99(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
1375{
1376 switch (pdisc) {
1377 case GSM48_PDISC_MM:
1378 case GSM48_PDISC_CC:
1379 case GSM48_PDISC_NC_SS:
1380 *n_sd = (msg_type >> 6) & 0x3;
1381 return 4;
1382 case GSM48_PDISC_GROUP_CC:
1383 case GSM48_PDISC_BCAST_CC:
1384 case GSM48_PDISC_LOC:
1385 *n_sd = (msg_type >> 6) & 0x1;
1386 return 2;
1387 default:
1388 /* no sequence number, we cannot detect dups */
1389 return 0;
1390 }
1391}
1392
1393/* extract the N(SD) and return the modulo value for a R99 message */
1394static uint8_t gsm0407_determine_nsd_ret_modulo_r98(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
1395{
1396 switch (pdisc) {
1397 case GSM48_PDISC_MM:
1398 case GSM48_PDISC_CC:
1399 case GSM48_PDISC_NC_SS:
1400 case GSM48_PDISC_GROUP_CC:
1401 case GSM48_PDISC_BCAST_CC:
1402 case GSM48_PDISC_LOC:
1403 *n_sd = (msg_type >> 6) & 0x1;
1404 return 2;
1405 default:
1406 /* no sequence number, we cannot detect dups */
1407 return 0;
1408 }
1409}
1410
1411/* TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001412static bool gsm0407_is_duplicate(struct ran_conn *conn, struct msgb *msg)
Harald Weltec2007852018-02-03 21:08:26 +01001413{
1414 struct gsm48_hdr *gh;
1415 uint8_t pdisc;
Harald Weltef6400732018-10-21 12:55:11 +02001416 uint8_t n_sd, modulo;
1417 int bin;
Harald Weltec2007852018-02-03 21:08:26 +01001418
1419 gh = msgb_l3(msg);
1420 pdisc = gsm48_hdr_pdisc(gh);
1421
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001422 if (conn->vsub && classmark_is_r99(&conn->vsub->classmark)) {
Harald Weltec2007852018-02-03 21:08:26 +01001423 modulo = gsm0407_determine_nsd_ret_modulo_r99(pdisc, gh->msg_type, &n_sd);
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001424 } else { /* pre R99 */
1425 modulo = gsm0407_determine_nsd_ret_modulo_r98(pdisc, gh->msg_type, &n_sd);
Harald Weltec2007852018-02-03 21:08:26 +01001426 }
1427 if (modulo == 0)
1428 return false;
1429 bin = gsm0407_pdisc_ctr_bin(pdisc);
1430 if (bin < 0)
1431 return false;
1432
1433 OSMO_ASSERT(bin < ARRAY_SIZE(conn->n_sd_next));
1434 if (n_sd != conn->n_sd_next[bin]) {
1435 /* not what we expected: duplicate */
1436 return true;
1437 } else {
1438 /* as expected: no dup; update expected counter for next message */
1439 conn->n_sd_next[bin] = (n_sd + 1) % modulo;
1440 return false;
1441 }
1442}
Harald Welte2483f1b2016-06-19 18:06:02 +02001443
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001444extern int gsm0408_rcv_cc(struct ran_conn *conn, struct msgb *msg);
Harald Welte27989d42018-06-21 20:39:20 +02001445
Neels Hofmeyr378a4922016-05-09 21:07:43 +02001446/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001447int gsm0408_dispatch(struct ran_conn *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001448{
Philipp Maier91f10c72017-04-20 18:40:37 +02001449 struct gsm48_hdr *gh;
1450 uint8_t pdisc;
Harald Welte8470bf22008-12-25 23:28:35 +00001451 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01001452
Neels Hofmeyr0906a392017-09-19 14:36:06 +02001453 OSMO_ASSERT(msg->l3h);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02001454 OSMO_ASSERT(conn);
1455 OSMO_ASSERT(msg);
1456
Philipp Maier91f10c72017-04-20 18:40:37 +02001457 gh = msgb_l3(msg);
1458 pdisc = gsm48_hdr_pdisc(gh);
1459
Harald Weltec2007852018-02-03 21:08:26 +01001460 if (gsm0407_is_duplicate(conn, msg)) {
1461 LOGP(DRLL, LOGL_NOTICE, "%s: Discarding duplicate L3 message\n",
1462 (conn && conn->vsub) ? vlr_subscr_name(conn->vsub) : "UNKNOWN");
1463 return 0;
1464 }
1465
Neels Hofmeyr78ada642017-03-10 02:15:20 +01001466 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message %s (0x%x:0x%x)\n",
1467 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)),
1468 pdisc, gsm48_hdr_msg_type(gh));
Harald Welte2483f1b2016-06-19 18:06:02 +02001469
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001470 if (!ran_conn_is_accepted(conn)
Harald Welte2483f1b2016-06-19 18:06:02 +02001471 && !msg_is_initially_permitted(gh)) {
1472 LOGP(DRLL, LOGL_ERROR,
Neels Hofmeyr78ada642017-03-10 02:15:20 +01001473 "subscr %s: Message not permitted for initial conn: %s\n",
1474 vlr_subscr_name(conn->vsub),
1475 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
Harald Welte2483f1b2016-06-19 18:06:02 +02001476 return -EACCES;
1477 }
1478
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001479 if (conn->vsub && conn->vsub->cs.attached_via_ran != conn->via_ran) {
1480 LOGP(DMM, LOGL_ERROR,
1481 "%s: Illegal situation: RAN type mismatch:"
1482 " attached via %s, received message via %s\n",
1483 vlr_subscr_name(conn->vsub),
Neels Hofmeyr7814a832018-12-26 00:40:18 +01001484 osmo_rat_type_name(conn->vsub->cs.attached_via_ran),
1485 osmo_rat_type_name(conn->via_ran));
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001486 return -EACCES;
1487 }
1488
Philipp Maiere0d5caa2017-02-27 16:56:59 +01001489#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08001490 if (silent_call_reroute(conn, msg))
1491 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01001492#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001493
Harald Welte52b1f982008-12-23 20:25:15 +00001494 switch (pdisc) {
1495 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001496 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001497 break;
1498 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001499 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001500 break;
1501 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001502 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001503 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00001504 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08001505 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00001506 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001507 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00001508 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01001509 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
1510 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02001511 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00001512 break;
Harald Welte6eafe912009-10-16 08:32:58 +02001513 case GSM48_PDISC_NC_SS:
Vadim Yanitskiy5b860fa2018-06-12 05:24:52 +07001514 rc = gsm0911_rcv_nc_ss(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02001515 break;
Harald Welteeac38c32017-05-29 18:02:53 +02001516 case GSM48_PDISC_TEST:
1517 rc = gsm0414_rcv_test(conn, msg);
1518 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001519 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001520 LOGP(DRLL, LOGL_NOTICE, "Unknown "
1521 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02001522 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00001523 break;
1524 }
1525
1526 return rc;
1527}
Harald Welte8470bf22008-12-25 23:28:35 +00001528
Harald Welte2483f1b2016-06-19 18:06:02 +02001529/***********************************************************************
1530 * VLR integration
1531 ***********************************************************************/
1532
1533/* VLR asks us to send an authentication request */
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +01001534static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct vlr_auth_tuple *at,
Harald Welte2483f1b2016-06-19 18:06:02 +02001535 bool send_autn)
1536{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001537 struct ran_conn *conn = msc_conn_ref;
Harald Welte2483f1b2016-06-19 18:06:02 +02001538 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
1539 send_autn? at->vec.autn : NULL,
1540 at->key_seq);
1541}
1542
1543/* VLR asks us to send an authentication reject */
1544static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
1545{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001546 struct ran_conn *conn = msc_conn_ref;
Harald Welte2483f1b2016-06-19 18:06:02 +02001547 return gsm48_tx_mm_auth_rej(conn);
1548}
1549
1550/* VLR asks us to transmit an Identity Request of given type */
1551static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
1552{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001553 struct ran_conn *conn = msc_conn_ref;
Harald Welte2483f1b2016-06-19 18:06:02 +02001554 return mm_tx_identity_req(conn, mi_type);
1555}
1556
1557/* VLR asks us to transmit a Location Update Accept */
1558static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
1559{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001560 struct ran_conn *conn = msc_conn_ref;
Harald Welte2483f1b2016-06-19 18:06:02 +02001561 return gsm0408_loc_upd_acc(conn, send_tmsi);
1562}
1563
1564/* VLR asks us to transmit a Location Update Reject */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001565static int msc_vlr_tx_lu_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Harald Welte2483f1b2016-06-19 18:06:02 +02001566{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001567 struct ran_conn *conn = msc_conn_ref;
Harald Welte2483f1b2016-06-19 18:06:02 +02001568 return gsm0408_loc_upd_rej(conn, cause);
1569}
1570
1571/* VLR asks us to transmit a CM Service Accept */
1572static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
1573{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001574 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001575 return msc_gsm48_tx_mm_serv_ack(conn);
1576}
1577
1578static int msc_vlr_tx_common_id(void *msc_conn_ref)
1579{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001580 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001581 return msc_tx_common_id(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +02001582}
1583
Stefan Sperling3a741282018-03-13 21:11:49 +01001584/* VLR asks us to transmit MM info. */
1585static int msc_vlr_tx_mm_info(void *msc_conn_ref)
1586{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001587 struct ran_conn *conn = msc_conn_ref;
Stefan Sperling3a741282018-03-13 21:11:49 +01001588 if (!conn->network->send_mm_info)
1589 return 0;
1590 return gsm48_tx_mm_info(conn);
1591}
1592
Harald Welte2483f1b2016-06-19 18:06:02 +02001593/* VLR asks us to transmit a CM Service Reject */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001594static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Harald Welte2483f1b2016-06-19 18:06:02 +02001595{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001596 struct ran_conn *conn = msc_conn_ref;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001597 int rc;
Harald Welte2483f1b2016-06-19 18:06:02 +02001598
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001599 rc = msc_gsm48_tx_mm_serv_rej(conn, cause);
1600
1601 if (conn->received_cm_service_request) {
1602 conn->received_cm_service_request = false;
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +01001603 ran_conn_put(conn, RAN_CONN_USE_CM_SERVICE);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001604 }
1605
1606 return rc;
Harald Welte2483f1b2016-06-19 18:06:02 +02001607}
1608
Neels Hofmeyr703638e2017-12-14 05:30:16 +01001609/* For msc_vlr_set_ciph_mode() */
1610osmo_static_assert(sizeof(((struct gsm0808_encrypt_info*)0)->key) >= sizeof(((struct osmo_auth_vector*)0)->kc),
1611 gsm0808_encrypt_info_key_fits_osmo_auth_vec_kc);
1612
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +01001613int ran_conn_geran_set_cipher_mode(struct ran_conn *conn, bool umts_aka, bool retrieve_imeisv)
Neels Hofmeyr3117b702018-09-13 03:23:07 +02001614{
Philipp Maierec5901c2019-01-08 12:29:49 +01001615 struct gsm_network *net;
Neels Hofmeyr3117b702018-09-13 03:23:07 +02001616 struct gsm0808_encrypt_info ei;
1617 int i, j = 0;
1618 int request_classmark = 0;
1619 int request_classmark_for_a5_n = 0;
Philipp Maierec5901c2019-01-08 12:29:49 +01001620 struct vlr_auth_tuple *tuple;
Neels Hofmeyr3117b702018-09-13 03:23:07 +02001621
1622 if (!conn || !conn->vsub || !conn->vsub->last_tuple) {
1623 /* This should really never happen, because we checked this in msc_vlr_set_ciph_mode()
1624 * already. */
1625 LOGP(DMM, LOGL_ERROR, "Internal error: missing state during Ciphering Mode Command\n");
1626 return -EINVAL;
1627 }
1628
Philipp Maierec5901c2019-01-08 12:29:49 +01001629 net = conn->network;
1630 tuple = conn->vsub->last_tuple;
1631
Neels Hofmeyr3117b702018-09-13 03:23:07 +02001632 for (i = 0; i < 8; i++) {
1633 int supported;
1634
1635 /* A5/n permitted by osmo-msc.cfg? */
1636 if (!(net->a5_encryption_mask & (1 << i)))
1637 continue;
1638
1639 /* A5/n supported by MS? */
1640 supported = classmark_supports_a5(&conn->vsub->classmark, i);
1641 if (supported == 1) {
1642 ei.perm_algo[j++] = vlr_ciph_to_gsm0808_alg_id(i);
1643 /* A higher A5/n is supported, so no need to request a Classmark
1644 * for support of a lesser A5/n. */
1645 request_classmark = 0;
1646 } else if (supported < 0) {
1647 request_classmark = -supported;
1648 request_classmark_for_a5_n = i;
1649 }
1650 }
1651 ei.perm_algo_len = j;
1652
1653 if (request_classmark) {
1654 /* The highest A5/n as from osmo-msc.cfg might be available, but we are
1655 * still missing the Classmark information for that from the MS. First
1656 * ask for that. */
1657 LOGP(DMM, LOGL_DEBUG, "%s: to determine whether A5/%d is supported,"
1658 " first ask for a Classmark Update to obtain Classmark %d\n",
1659 vlr_subscr_name(conn->vsub), request_classmark_for_a5_n,
1660 request_classmark);
1661
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +01001662 return ran_conn_classmark_request_then_cipher_mode_cmd(conn, umts_aka, retrieve_imeisv);
Neels Hofmeyr3117b702018-09-13 03:23:07 +02001663 }
1664
1665 if (ei.perm_algo_len == 0) {
1666 LOGP(DMM, LOGL_ERROR, "%s: cannot start ciphering, no intersection "
Neels Hofmeyr5769d362018-12-18 03:13:51 +01001667 "between MSC-configured and MS-supported A5 algorithms. MSC: %x MS: %s\n",
1668 vlr_subscr_name(conn->vsub), net->a5_encryption_mask,
1669 classmark_a5_name(&conn->vsub->classmark));
Neels Hofmeyr3117b702018-09-13 03:23:07 +02001670 return -ENOTSUP;
1671 }
1672
1673 DEBUGP(DMM, "-> CIPHER MODE COMMAND %s\n", vlr_subscr_name(conn->vsub));
1674
1675 tuple = conn->vsub->last_tuple;
1676
1677 /* In case of UMTS AKA, the Kc for ciphering must be derived from the 3G auth
1678 * tokens. tuple->vec.kc was calculated from the GSM algorithm and is not
1679 * necessarily a match for the UMTS AKA tokens. */
1680 if (umts_aka)
1681 osmo_auth_c3(ei.key, tuple->vec.ck, tuple->vec.ik);
1682 else
1683 memcpy(ei.key, tuple->vec.kc, sizeof(tuple->vec.kc));
1684 ei.key_len = sizeof(tuple->vec.kc);
1685
Neels Hofmeyrf41658d2018-11-30 04:35:50 +01001686 conn->geran_encr = (struct geran_encr){};
1687 if (ei.key_len <= sizeof(conn->geran_encr.key)) {
1688 memcpy(conn->geran_encr.key, ei.key, ei.key_len);
1689 conn->geran_encr.key_len = ei.key_len;
Neels Hofmeyrb0779bb2018-11-29 23:37:19 +01001690 }
Neels Hofmeyrf41658d2018-11-30 04:35:50 +01001691 /* conn->geran_encr.alg_id remains unknown until we receive a Cipher Mode Complete from the BSC */
Neels Hofmeyrb0779bb2018-11-29 23:37:19 +01001692
Neels Hofmeyr3117b702018-09-13 03:23:07 +02001693 return a_iface_tx_cipher_mode(conn, &ei, retrieve_imeisv);
1694}
1695
Neels Hofmeyrda21a522018-03-02 01:50:09 +01001696/* VLR asks us to start using ciphering.
1697 * (Keep non-static to allow regression testing on this function.) */
1698int msc_vlr_set_ciph_mode(void *msc_conn_ref,
1699 bool umts_aka,
1700 bool retrieve_imeisv)
Harald Welte2483f1b2016-06-19 18:06:02 +02001701{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001702 struct ran_conn *conn = msc_conn_ref;
Harald Welte2483f1b2016-06-19 18:06:02 +02001703 struct vlr_subscr *vsub;
Neels Hofmeyr8b6e5362018-11-30 02:57:33 +01001704 struct vlr_auth_tuple *tuple;
Harald Welte2483f1b2016-06-19 18:06:02 +02001705
1706 if (!conn || !conn->vsub) {
1707 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
1708 " NULL conn/subscriber");
1709 return -EINVAL;
1710 }
1711
1712 vsub = conn->vsub;
1713 tuple = vsub->last_tuple;
1714
1715 if (!tuple) {
1716 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
1717 " Command: no auth tuple available\n",
1718 vlr_subscr_name(vsub));
1719 return -EINVAL;
1720 }
1721
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001722 switch (conn->via_ran) {
Neels Hofmeyr7814a832018-12-26 00:40:18 +01001723 case OSMO_RAT_GERAN_A:
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +01001724 return ran_conn_geran_set_cipher_mode(conn, umts_aka, retrieve_imeisv);
Neels Hofmeyr703638e2017-12-14 05:30:16 +01001725
Neels Hofmeyr7814a832018-12-26 00:40:18 +01001726 case OSMO_RAT_UTRAN_IU:
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001727#ifdef BUILD_IU
1728 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n",
1729 vlr_subscr_name(conn->vsub));
Neels Hofmeyr00e82d62017-07-05 15:19:52 +02001730 return ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &tuple->vec, 0, 1);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001731#else
Neels Hofmeyr7814a832018-12-26 00:40:18 +01001732 LOGP(DMM, LOGL_ERROR, "Cannot send Security Mode Control over OSMO_RAT_UTRAN_IU,"
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001733 " built without Iu support\n");
1734 return -ENOTSUP;
1735#endif
1736
1737 default:
1738 break;
1739 }
1740 LOGP(DMM, LOGL_ERROR,
1741 "%s: cannot start ciphering, unknown RAN type %d\n",
1742 vlr_subscr_name(conn->vsub), conn->via_ran);
1743 return -ENOTSUP;
1744}
1745
Neels Hofmeyr3c20a5e2018-11-30 01:08:36 +01001746void ran_conn_rx_sec_mode_compl(struct ran_conn *conn)
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001747{
1748 struct vlr_ciph_result vlr_res = {};
1749
1750 if (!conn || !conn->vsub) {
1751 LOGP(DMM, LOGL_ERROR,
1752 "Rx Security Mode Complete for invalid conn\n");
1753 return;
1754 }
1755
1756 DEBUGP(DMM, "<- SECURITY MODE COMPLETE %s\n",
1757 vlr_subscr_name(conn->vsub));
1758
1759 vlr_res.cause = VLR_CIPH_COMPL;
1760 vlr_subscr_rx_ciph_res(conn->vsub, &vlr_res);
Harald Welte2483f1b2016-06-19 18:06:02 +02001761}
1762
1763/* VLR informs us that the subscriber data has somehow been modified */
1764static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
1765{
Maxa263bb22017-12-27 13:23:44 +01001766 LOGVSUBP(LOGL_NOTICE, subscr, "VLR: update for IMSI=%s (MSISDN=%s, used=%d)\n",
1767 subscr->imsi, subscr->msisdn, subscr->use_count);
Neels Hofmeyr361e5712019-01-03 02:32:14 +01001768 ran_conn_update_id_for_vsub(subscr);
Harald Welte2483f1b2016-06-19 18:06:02 +02001769}
1770
Neels Hofmeyr68cf9572018-09-18 15:52:58 +02001771static void update_classmark(const struct gsm_classmark *src, struct gsm_classmark *dst)
1772{
1773 if (src->classmark1_set) {
1774 dst->classmark1 = src->classmark1;
1775 dst->classmark1_set = true;
1776 }
1777 if (src->classmark2_len) {
1778 dst->classmark2_len = src->classmark2_len;
1779 memcpy(dst->classmark2, src->classmark2, sizeof(dst->classmark2));
1780 }
1781 if (src->classmark3_len) {
1782 dst->classmark3_len = src->classmark3_len;
1783 memcpy(dst->classmark3, src->classmark3, sizeof(dst->classmark3));
1784 }
1785}
1786
Harald Welte2483f1b2016-06-19 18:06:02 +02001787/* VLR informs us that the subscriber has been associated with a conn */
Neels Hofmeyr1035d902018-12-28 21:22:32 +01001788static int msc_vlr_subscr_assoc(void *msc_conn_ref,
Harald Welte2483f1b2016-06-19 18:06:02 +02001789 struct vlr_subscr *vsub)
1790{
Neels Hofmeyrc036b792018-11-29 22:37:51 +01001791 struct ran_conn *conn = msc_conn_ref;
Max48d4ec02018-02-12 16:51:03 +01001792 OSMO_ASSERT(vsub);
Neels Hofmeyr1035d902018-12-28 21:22:32 +01001793 if (conn->vsub) {
1794 if (conn->vsub == vsub)
Neels Hofmeyr46c06e22019-01-04 17:42:05 +01001795 LOG_RAN_CONN(conn, LOGL_NOTICE, "msc_vlr_subscr_assoc(): conn already associated with %s\n",
1796 vlr_subscr_name(vsub));
Neels Hofmeyr1035d902018-12-28 21:22:32 +01001797 else {
Neels Hofmeyr46c06e22019-01-04 17:42:05 +01001798 LOG_RAN_CONN(conn, LOGL_ERROR, "msc_vlr_subscr_assoc(): conn already associated with a subscriber,"
1799 " cannot associate with %s\n", vlr_subscr_name(vsub));
Neels Hofmeyr1035d902018-12-28 21:22:32 +01001800 return -EINVAL;
1801 }
1802 }
1803
Harald Welte2483f1b2016-06-19 18:06:02 +02001804 conn->vsub = vlr_subscr_get(vsub);
Max48d4ec02018-02-12 16:51:03 +01001805 OSMO_ASSERT(conn->vsub);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001806 conn->vsub->cs.attached_via_ran = conn->via_ran;
Neels Hofmeyr68cf9572018-09-18 15:52:58 +02001807
1808 /* In case we have already received Classmark Information before the VLR Subscriber was
1809 * associated with the conn: merge the new Classmark into vsub->classmark. Don't overwrite valid
1810 * vsub->classmark with unset classmark, though. */
1811 update_classmark(&conn->temporary_classmark, &conn->vsub->classmark);
Neels Hofmeyr361e5712019-01-03 02:32:14 +01001812 ran_conn_update_id(conn);
Neels Hofmeyr1035d902018-12-28 21:22:32 +01001813 return 0;
Harald Welte2483f1b2016-06-19 18:06:02 +02001814}
1815
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001816static int msc_vlr_route_gsup_msg(struct vlr_subscr *vsub,
1817 struct osmo_gsup_message *gsup_msg)
1818{
1819 switch (gsup_msg->message_type) {
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +07001820 /* GSM 09.11 code implementing SS/USSD */
1821 case OSMO_GSUP_MSGT_PROC_SS_REQUEST:
1822 case OSMO_GSUP_MSGT_PROC_SS_RESULT:
1823 case OSMO_GSUP_MSGT_PROC_SS_ERROR:
1824 DEBUGP(DMSC, "Routed to GSM 09.11 SS/USSD handler\n");
1825 return gsm0911_gsup_handler(vsub, gsup_msg);
1826
Vadim Yanitskiy76ef72d2018-11-07 05:08:18 +07001827 /* GSM 04.11 code implementing MO SMS */
1828 case OSMO_GSUP_MSGT_MO_FORWARD_SM_ERROR:
1829 case OSMO_GSUP_MSGT_MO_FORWARD_SM_RESULT:
1830 case OSMO_GSUP_MSGT_READY_FOR_SM_ERROR:
1831 case OSMO_GSUP_MSGT_READY_FOR_SM_RESULT:
1832 DEBUGP(DMSC, "Routed to GSM 04.11 MO handler\n");
1833 return gsm411_gsup_mo_handler(vsub, gsup_msg);
1834
Vadim Yanitskiy82cc8a52018-11-15 01:05:53 +07001835 /* GSM 04.11 code implementing MT SMS */
1836 case OSMO_GSUP_MSGT_MT_FORWARD_SM_REQUEST:
1837 DEBUGP(DMSC, "Routed to GSM 04.11 MT handler\n");
1838 return gsm411_gsup_mt_handler(vsub, gsup_msg);
1839
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001840 default:
1841 LOGP(DMM, LOGL_ERROR, "No handler found for %s, dropping message...\n",
1842 osmo_gsup_message_type_name(gsup_msg->message_type));
1843 return -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
1844 }
1845}
1846
Harald Welte2483f1b2016-06-19 18:06:02 +02001847/* operations that we need to implement for libvlr */
1848static const struct vlr_ops msc_vlr_ops = {
1849 .tx_auth_req = msc_vlr_tx_auth_req,
1850 .tx_auth_rej = msc_vlr_tx_auth_rej,
1851 .tx_id_req = msc_vlr_tx_id_req,
1852 .tx_lu_acc = msc_vlr_tx_lu_acc,
1853 .tx_lu_rej = msc_vlr_tx_lu_rej,
1854 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
1855 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
1856 .set_ciph_mode = msc_vlr_set_ciph_mode,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001857 .tx_common_id = msc_vlr_tx_common_id,
Stefan Sperling3a741282018-03-13 21:11:49 +01001858 .tx_mm_info = msc_vlr_tx_mm_info,
Harald Welte2483f1b2016-06-19 18:06:02 +02001859 .subscr_update = msc_vlr_subscr_update,
1860 .subscr_assoc = msc_vlr_subscr_assoc,
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001861 .forward_gsup_msg = msc_vlr_route_gsup_msg,
Harald Welte2483f1b2016-06-19 18:06:02 +02001862};
1863
1864/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
1865int msc_vlr_alloc(struct gsm_network *net)
1866{
1867 net->vlr = vlr_alloc(net, &msc_vlr_ops);
1868 if (!net->vlr)
1869 return -ENOMEM;
1870 net->vlr->user_ctx = net;
1871 return 0;
1872}
1873
1874/* Launch the VLR, i.e. its GSUP connection */
1875int msc_vlr_start(struct gsm_network *net)
1876{
Stefan Sperlingafa030d2018-12-06 12:06:59 +01001877 struct ipaccess_unit *ipa_dev;
1878
Harald Welte2483f1b2016-06-19 18:06:02 +02001879 OSMO_ASSERT(net->vlr);
Stefan Sperlingafa030d2018-12-06 12:06:59 +01001880
1881 ipa_dev = talloc_zero(net->vlr, struct ipaccess_unit);
1882 ipa_dev->unit_name = "MSC";
1883 ipa_dev->serno = net->msc_ipa_name; /* NULL unless configured via VTY */
Stefan Sperling36dedee2018-12-06 13:16:24 +01001884 ipa_dev->swversion = PACKAGE_NAME "-" PACKAGE_VERSION;
Stefan Sperlingafa030d2018-12-06 12:06:59 +01001885
1886 return vlr_start(ipa_dev, net->vlr, net->gsup_server_addr_str, net->gsup_server_port);
Harald Welte2483f1b2016-06-19 18:06:02 +02001887}
Neels Hofmeyr79e580b2018-03-22 16:48:22 +01001888
1889struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value)
1890{
1891 struct msgb *msg;
1892 struct gsm48_hdr *gh;
1893
1894 msg = gsm48_msgb_alloc_name("GSM 04.08 SERV REJ");
1895 if (!msg)
1896 return NULL;
1897
1898 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1899 gh->proto_discr = GSM48_PDISC_MM;
1900 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1901 gh->data[0] = value;
1902
1903 return msg;
1904}
1905
1906struct msgb *gsm48_create_loc_upd_rej(uint8_t cause)
1907{
1908 struct gsm48_hdr *gh;
1909 struct msgb *msg;
1910
1911 msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD REJ");
1912 if (!msg)
1913 return NULL;
1914
1915 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1916 gh->proto_discr = GSM48_PDISC_MM;
1917 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
1918 gh->data[0] = cause;
1919 return msg;
1920}
1921
1922int gsm48_extract_mi(uint8_t *classmark2_lv, int length, char *mi_string, uint8_t *mi_type)
1923{
1924 /* Check the size for the classmark */
1925 if (length < 1 + *classmark2_lv)
1926 return -1;
1927
1928 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
1929 if (length < 2 + *classmark2_lv + mi_lv[0])
1930 return -2;
1931
1932 *mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
1933 return gsm48_mi_to_string(mi_string, GSM48_MI_SIZE, mi_lv+1, *mi_lv);
1934}
1935
1936int gsm48_paging_extract_mi(struct gsm48_pag_resp *resp, int length,
1937 char *mi_string, uint8_t *mi_type)
1938{
1939 static const uint32_t classmark_offset =
1940 offsetof(struct gsm48_pag_resp, classmark2);
1941 uint8_t *classmark2_lv = (uint8_t *) &resp->classmark2;
1942 return gsm48_extract_mi(classmark2_lv, length - classmark_offset,
1943 mi_string, mi_type);
1944}