blob: 564e90bffb740c080973d15a75f12ff417a05710 [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/osmo_msc.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020050#include <osmocom/msc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020051#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010052#include <osmocom/core/bitvec.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020053#include <osmocom/msc/vlr.h>
54#include <osmocom/msc/msc_ifaces.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080055
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010056#include <osmocom/gsm/gsm48.h>
57#include <osmocom/gsm/gsm0480.h>
58#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020059#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010060#include <osmocom/core/msgb.h>
61#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010062#include <osmocom/core/utils.h>
Philipp Maier621ba032017-11-07 17:19:25 +010063#include <osmocom/core/byteswap.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010064#include <osmocom/gsm/tlv.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020065#include <osmocom/crypt/auth.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020066#ifdef BUILD_IU
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020067#include <osmocom/ranap/iu_client.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020068#endif
Harald Welte52b1f982008-12-23 20:25:15 +000069
Neels Hofmeyr90843962017-09-04 15:04:35 +020070#include <osmocom/msc/msc_ifaces.h>
71#include <osmocom/msc/a_iface.h>
Philipp Maier621ba032017-11-07 17:19:25 +010072#include <osmocom/msc/msc_mgcp.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020073
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020074#include <assert.h>
75
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020076
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020077void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010078void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020079
Harald Welte2483f1b2016-06-19 18:06:02 +020080static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
81 uint32_t send_tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +000082
Harald Welte27989d42018-06-21 20:39:20 +020083/*! Send a simple GSM 04.08 message without any payload
84 * \param conn Active subscriber connection
85 * \param[in] pdisc Protocol discriminator
86 * \param[in] msg_type Message type
87 * \return result of \ref gsm48_conn_sendmsg
88 */
89int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
90 uint8_t pdisc, uint8_t msg_type)
91{
92 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
93 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
94
95 gh->proto_discr = pdisc;
96 gh->msg_type = msg_type;
97
98 return gsm48_conn_sendmsg(msg, conn, NULL);
99}
Harald Welte4bfdfe72009-06-10 23:11:52 +0800100
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200101static bool classmark1_is_r99(const struct gsm48_classmark1 *cm1)
102{
103 return cm1->rev_lev >= 2;
104}
105
106static bool classmark2_is_r99(const uint8_t *cm2, uint8_t cm2_len)
107{
108 uint8_t rev_lev;
109 if (!cm2_len)
110 return false;
111 rev_lev = (cm2[0] >> 5) & 0x3;
112 return rev_lev >= 2;
113}
114
Neels Hofmeyr9d744252018-03-22 16:09:50 +0100115static bool classmark_is_r99(struct gsm_classmark *cm)
116{
Neels Hofmeyr9d744252018-03-22 16:09:50 +0100117 if (cm->classmark1_set)
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200118 return classmark1_is_r99(&cm->classmark1);
119 return classmark2_is_r99(cm->classmark2, cm->classmark2_len);
Neels Hofmeyr9d744252018-03-22 16:09:50 +0100120}
121
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200122/* Determine if the given CLASSMARK (1/2/3) value permits a given A5/n cipher.
123 * Return 1 when the given A5/n is permitted, 0 when not, and negative if the respective MS CLASSMARK is
124 * not known, where the negative number indicates the classmark type: -2 means Classmark 2 is not
125 * available. */
126static int classmark_supports_a5(const struct gsm_classmark *cm, uint8_t a5)
Harald Welte71330722017-12-23 19:59:02 +0100127{
128 switch (a5) {
129 case 0:
130 /* all phones must implement A5/0, see 3GPP TS 43.020 4.9 */
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200131 return 1;
Harald Welte71330722017-12-23 19:59:02 +0100132 case 1:
133 /* 3GPP TS 43.020 4.9 requires A5/1 to be suppored by all phones and actually states:
134 * "The network shall not provide service to an MS which indicates that it does not
135 * support the ciphering algorithm A5/1.". However, let's be more tolerant based
136 * on policy here */
137 /* See 3GPP TS 24.008 10.5.1.7 */
138 if (!cm->classmark1_set) {
139 DEBUGP(DMSC, "CLASSMARK 1 unknown, assuming MS supports A5/1\n");
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200140 return -1;
Harald Welte71330722017-12-23 19:59:02 +0100141 } else {
142 if (cm->classmark1.a5_1)
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200143 return 0; /* Inverted logic for this bit! */
Harald Welte71330722017-12-23 19:59:02 +0100144 else
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200145 return 1;
Harald Welte71330722017-12-23 19:59:02 +0100146 }
147 break;
148 case 2:
149 case 3:
150 /* See 3GPP TS 24.008 10.5.1.6 */
151 if (cm->classmark2_len < 3) {
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200152 return -2;
Harald Welte71330722017-12-23 19:59:02 +0100153 } else {
154 if (cm->classmark2[2] & (1 << (a5-2)))
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200155 return 1;
Harald Welte71330722017-12-23 19:59:02 +0100156 else
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200157 return 0;
Harald Welte71330722017-12-23 19:59:02 +0100158 }
159 break;
160 case 4:
161 case 5:
162 case 6:
163 case 7:
164 /* See 3GPP TS 24.008 10.5.1.7 */
165 if (cm->classmark3_len < 1) {
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200166 return -3;
Harald Welte71330722017-12-23 19:59:02 +0100167 } else {
168 if (cm->classmark3[0] & (1 << (a5-4)))
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200169 return 1;
Harald Welte71330722017-12-23 19:59:02 +0100170 else
Neels Hofmeyr3117b702018-09-13 03:23:07 +0200171 return 0;
Harald Welte71330722017-12-23 19:59:02 +0100172 }
173 break;
174 default:
175 return false;
176 }
177}
178
Harald Welte27989d42018-06-21 20:39:20 +0200179int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn, struct gsm_trans *trans)
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800180{
181 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
182
183 /* if we get passed a transaction reference, do some common
184 * work that the caller no longer has to do */
185 if (trans) {
186 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
Harald Welte0e2fa5d2018-04-09 16:35:01 +0200187 OMSC_LINKID_CB(msg) = trans->dlci;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800188 }
189
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200190 return msc_tx_dtap(conn, msg);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800191}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100192
Harald Welte2483f1b2016-06-19 18:06:02 +0200193/* clear all transactions globally; used in case of MNCC socket disconnect */
Harald Welte371efe52010-12-22 23:17:50 +0100194void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
195{
196 struct gsm_trans *trans, *temp;
197
198 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
199
200 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100201 if (trans->protocol == protocol) {
202 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100203 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100204 }
Harald Welte371efe52010-12-22 23:17:50 +0100205 }
206}
207
Holger Freyther429e7762008-12-30 13:28:30 +0000208/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200209static int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000210{
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800211 struct msgb *msg;
212
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800213 msg = gsm48_create_loc_upd_rej(cause);
214 if (!msg) {
215 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
216 return -1;
217 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200218
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200219 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT\n",
220 vlr_subscr_name(conn->vsub));
Harald Welte24ff6ee2009-12-22 00:41:05 +0100221
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800222 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000223}
224
225/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte2483f1b2016-06-19 18:06:02 +0200226static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
227 uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000228{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100229 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000230 struct gsm48_hdr *gh;
231 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200232 uint8_t *mid;
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100233 struct osmo_location_area_id laid = {
234 .plmn = conn->network->plmn,
235 .lac = conn->lac,
236 };
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200237
Harald Welte52b1f982008-12-23 20:25:15 +0000238 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
239 gh->proto_discr = GSM48_PDISC_MM;
240 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
241
242 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100243 gsm48_generate_lai2(lai, &laid);
Harald Welte52b1f982008-12-23 20:25:15 +0000244
Harald Welte2483f1b2016-06-19 18:06:02 +0200245 if (send_tmsi == GSM_RESERVED_TMSI) {
246 /* we did not allocate a TMSI to the MS, so we need to
247 * include the IMSI in order for the MS to delete any
248 * old TMSI that might still be allocated */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200249 uint8_t mi[10];
250 int len;
Harald Welte2483f1b2016-06-19 18:06:02 +0200251 len = gsm48_generate_mid_from_imsi(mi, conn->vsub->imsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200252 mid = msgb_put(msg, len);
253 memcpy(mid, mi, len);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200254 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT\n",
255 vlr_subscr_name(conn->vsub));
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200256 } else {
Harald Welte2483f1b2016-06-19 18:06:02 +0200257 /* Include the TMSI, which means that the MS will send a
258 * TMSI REALLOCATION COMPLETE, and we should wait for
259 * that until T3250 expiration */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200260 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Welte2483f1b2016-06-19 18:06:02 +0200261 gsm48_generate_mid_from_tmsi(mid, send_tmsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200262 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT (TMSI = 0x%08x)\n",
263 vlr_subscr_name(conn->vsub),
264 send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200265 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200266 /* TODO: Follow-on proceed */
267 /* TODO: CTS permission */
268 /* TODO: Equivalent PLMNs */
269 /* TODO: Emergency Number List */
270 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000271
Harald Welte52b1f982008-12-23 20:25:15 +0000272
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800273 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000274}
275
Harald Weltebf5e8df2009-02-03 12:59:45 +0000276/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200277static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000278{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100279 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000280 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000281
Harald Welte231ad4f2008-12-27 11:15:38 +0000282 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
283 gh->proto_discr = GSM48_PDISC_MM;
284 gh->msg_type = GSM48_MT_MM_ID_REQ;
285 gh->data[0] = id_type;
286
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800287 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000288}
289
Harald Weltebf5e8df2009-02-03 12:59:45 +0000290/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800291static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000292{
293 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200294 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200295 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000296
Harald Welte2483f1b2016-06-19 18:06:02 +0200297 if (!conn->vsub) {
298 LOGP(DMM, LOGL_ERROR,
299 "Rx MM Identity Response: invalid: no subscriber\n");
300 return -EINVAL;
301 }
302
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200303 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200304 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
305 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000306
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200307 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100308
Harald Welte2483f1b2016-06-19 18:06:02 +0200309 return vlr_subscr_rx_id_resp(conn->vsub, gh->data+1, gh->data[0]);
Harald Welte231ad4f2008-12-27 11:15:38 +0000310}
311
Harald Welte2483f1b2016-06-19 18:06:02 +0200312/* FIXME: to libosmogsm */
Harald Welteb9845f92015-08-16 18:07:48 +0200313static const struct value_string lupd_names[] = {
314 { GSM48_LUPD_NORMAL, "NORMAL" },
315 { GSM48_LUPD_PERIODIC, "PERIODIC" },
316 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
317 { 0, NULL }
318};
Harald Welte2a139372009-02-22 21:14:55 +0000319
Harald Welte2483f1b2016-06-19 18:06:02 +0200320/* Chapter 9.2.15: Receive Location Updating Request.
321 * Keep this function non-static for direct invocation by unit tests. */
322int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000323{
Harald Welte2483f1b2016-06-19 18:06:02 +0200324 struct gsm_network *net = conn->network;
Harald Welte8470bf22008-12-25 23:28:35 +0000325 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000326 struct gsm48_loc_upd_req *lu;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200327 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200328 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200329 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte2483f1b2016-06-19 18:06:02 +0200330 uint32_t tmsi;
331 char *imsi;
332 struct osmo_location_area_id old_lai, new_lai;
333 struct osmo_fsm_inst *lu_fsm;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200334 bool is_utran;
Harald Welte52b1f982008-12-23 20:25:15 +0000335
Harald Welte8470bf22008-12-25 23:28:35 +0000336 lu = (struct gsm48_loc_upd_req *) gh->data;
337
338 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000339
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200340 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000341
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200342 if (msc_subscr_conn_is_establishing_auth_ciph(conn)) {
343 LOGP(DMM, LOGL_ERROR,
344 "Cannot accept another LU, conn already busy establishing authenticity;"
345 " extraneous LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
346 gsm48_mi_type_name(mi_type), mi_string, get_value_string(lupd_names, lu->type));
347 return -EINVAL;
348 }
349
350 if (msc_subscr_conn_is_accepted(conn)) {
351 LOGP(DMM, LOGL_ERROR,
352 "Cannot accept another LU, conn already established;"
353 " extraneous LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
354 gsm48_mi_type_name(mi_type), mi_string, get_value_string(lupd_names, lu->type));
355 return -EINVAL;
356 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200357
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200358 msc_subscr_conn_update_id(conn, COMPLETE_LAYER3_LU, mi_string);
359
Harald Welte2483f1b2016-06-19 18:06:02 +0200360 DEBUGP(DMM, "LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
361 gsm48_mi_type_name(mi_type), mi_string,
362 get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000363
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200364 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100365
Harald Welte24ff6ee2009-12-22 00:41:05 +0100366 switch (lu->type) {
367 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200368 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200369 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100370 break;
371 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200372 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200373 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100374 break;
375 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200376 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200377 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100378 break;
379 }
380
Harald Welte2483f1b2016-06-19 18:06:02 +0200381 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
382 /* TODO: 10.5.3.14 Aditional update parameters (CS fallback calls) */
383 /* TODO: 10.5.7.8 Device properties */
384 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200385
Harald Welte52b1f982008-12-23 20:25:15 +0000386 switch (mi_type) {
387 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200388 tmsi = GSM_RESERVED_TMSI;
389 imsi = mi_string;
Harald Welte4b634542008-12-27 01:55:51 +0000390 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000391 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200392 tmsi = tmsi_from_string(mi_string);
393 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000394 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200395 default:
Harald Weltea0368542009-06-27 02:58:43 +0200396 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200397 tmsi = GSM_RESERVED_TMSI;
398 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000399 break;
400 }
401
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100402 gsm48_decode_lai2(&lu->lai, &old_lai);
403 new_lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200404 new_lai.lac = conn->lac;
Harald Welte2483f1b2016-06-19 18:06:02 +0200405 DEBUGP(DMM, "LU/new-LAC: %u/%u\n", old_lai.lac, new_lai.lac);
Harald Welte24516ea2009-07-04 10:18:00 +0200406
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200407 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200408 lu_fsm = vlr_loc_update(conn->fi,
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200409 SUBSCR_CONN_E_ACCEPTED, SUBSCR_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +0200410 net->vlr, conn, vlr_lu_type, tmsi, imsi,
411 &old_lai, &new_lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200412 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100413 is_utran || conn->network->a5_encryption_mask > 0x01,
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200414 classmark1_is_r99(&lu->classmark1),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200415 is_utran,
Harald Welte2483f1b2016-06-19 18:06:02 +0200416 net->vlr->cfg.assign_tmsi);
417 if (!lu_fsm) {
418 DEBUGP(DRR, "%s: Can't start LU FSM\n", mi_string);
419 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800420 }
421
Harald Welte2483f1b2016-06-19 18:06:02 +0200422 /* From vlr_loc_update() we expect an implicit dispatch of
423 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
424 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000425
Harald Welte2483f1b2016-06-19 18:06:02 +0200426 if (!conn->vsub || conn->vsub->lu_fsm != lu_fsm) {
427 LOGP(DRR, LOGL_ERROR,
428 "%s: internal error during Location Updating attempt\n",
429 mi_string);
430 return -EIO;
431 }
432
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200433 conn->vsub->classmark.classmark1 = lu->classmark1;
434 conn->vsub->classmark.classmark1_set = true;
435
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200436 msc_subscr_conn_complete_layer_3(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +0200437 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000438}
439
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100440/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200441/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100442static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800443{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100444 uint8_t ret;
445
446 ret = value / 10;
447 ret |= (value % 10) << 4;
448
449 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800450}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100451
Harald Welte4bfdfe72009-06-10 23:11:52 +0800452
Harald Weltedb253af2008-12-30 17:56:55 +0000453/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800454int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000455{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100456 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000457 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200458 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200459 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200460 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100461
Harald Welte4bfdfe72009-06-10 23:11:52 +0800462 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100463 struct tm* gmt_time;
464 struct tm* local_time;
465 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100466 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000467
Harald Weltedb253af2008-12-30 17:56:55 +0000468 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
469 gh->proto_discr = GSM48_PDISC_MM;
470 gh->msg_type = GSM48_MT_MM_INFO;
471
472 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200473#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000474 name_len = strlen(net->name_long);
475 /* 10.5.3.5a */
476 ptr8 = msgb_put(msg, 3);
477 ptr8[0] = GSM48_IE_NAME_LONG;
478 ptr8[1] = name_len*2 +1;
479 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
480
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200481 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000482 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000483 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000484
485 /* FIXME: Use Cell Broadcast, not UCS-2, since
486 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200487#endif
488 name_len = (strlen(net->name_long)*7)/8;
489 name_pad = (8 - strlen(net->name_long)*7)%8;
490 if (name_pad > 0)
491 name_len++;
492 /* 10.5.3.5a */
493 ptr8 = msgb_put(msg, 3);
494 ptr8[0] = GSM48_IE_NAME_LONG;
495 ptr8[1] = name_len +1;
496 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
497
498 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100499 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200500
Harald Weltedb253af2008-12-30 17:56:55 +0000501 }
502
503 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200504#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000505 name_len = strlen(net->name_short);
506 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200507 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200508 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000509 ptr8[1] = name_len*2 + 1;
510 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
511
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200512 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000513 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000514 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200515#endif
516 name_len = (strlen(net->name_short)*7)/8;
517 name_pad = (8 - strlen(net->name_short)*7)%8;
518 if (name_pad > 0)
519 name_len++;
520 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200521 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200522 ptr8[0] = GSM48_IE_NAME_SHORT;
523 ptr8[1] = name_len +1;
524 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
525
526 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100527 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200528
Harald Weltedb253af2008-12-30 17:56:55 +0000529 }
530
Harald Weltedb253af2008-12-30 17:56:55 +0000531 /* Section 10.5.3.9 */
532 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100533 gmt_time = gmtime(&cur_t);
534
Harald Weltedb253af2008-12-30 17:56:55 +0000535 ptr8 = msgb_put(msg, 8);
536 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100537 ptr8[1] = bcdify(gmt_time->tm_year % 100);
538 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
539 ptr8[3] = bcdify(gmt_time->tm_mday);
540 ptr8[4] = bcdify(gmt_time->tm_hour);
541 ptr8[5] = bcdify(gmt_time->tm_min);
542 ptr8[6] = bcdify(gmt_time->tm_sec);
543
Neels Hofmeyr73983952016-05-10 13:29:33 +0200544 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200545 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200546 if (net->tz.hr < 0) {
547 tzunits = ((net->tz.hr/-1)*4);
548 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100549 ptr8[7] = bcdify(tzunits);
550 /* Set negative time */
551 ptr8[7] |= 0x08;
552 }
553 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200554 tzunits = net->tz.hr*4;
555 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100556 ptr8[7] = bcdify(tzunits);
557 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100558 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200559 if (net->tz.dst >= 0 && net->tz.dst <= 2)
560 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100561 }
562 else {
563 /* Need to get GSM offset and convert into 15 min units */
564 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
Harald Welte9c3dc902012-04-08 16:59:24 +0200565#ifdef HAVE_TM_GMTOFF_IN_TM
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200566 local_time = localtime(&cur_t);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100567 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200568#else
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200569 /* find timezone offset */
570 time_t utc;
571 double offsetFromUTC;
572 utc = mktime(gmt_time);
573 local_time = localtime(&cur_t);
574 offsetFromUTC = difftime(cur_t, utc);
575 if (local_time->tm_isdst)
576 offsetFromUTC += 3600.0;
577 tzunits = ((int)offsetFromUTC) / 60 / 15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200578#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100579 if (tzunits < 0) {
580 tzunits = tzunits/-1;
581 ptr8[7] = bcdify(tzunits);
582 /* Flip it to negative */
583 ptr8[7] |= 0x08;
584 }
585 else
586 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100587
588 /* Does not support DST +2 */
589 if (local_time->tm_isdst)
590 dst = 1;
591 }
592
593 if (dst) {
594 ptr8 = msgb_put(msg, 3);
595 ptr8[0] = GSM48_IE_NET_DST;
596 ptr8[1] = 1;
597 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100598 }
Harald Weltedb253af2008-12-30 17:56:55 +0000599
Daniel Willmanneea93372009-08-13 03:42:07 +0200600 DEBUGP(DMM, "-> MM INFO\n");
601
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800602 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000603}
604
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100605/*! Send an Authentication Request to MS on the given subscriber connection
606 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
607 * \param[in] conn Subscriber connection to send on.
608 * \param[in] rand Random challenge token to send, must be 16 bytes long.
609 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
610 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
611 * \param[in] key_seq auth tuple's sequence number.
612 */
613int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
614 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200615{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100616 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200617 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200618 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200619
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100620 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100621 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100622 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200623
Harald Welte7984d5c2009-08-12 22:56:50 +0200624 gh->proto_discr = GSM48_PDISC_MM;
625 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
626
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100627 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200628
Harald Welte7984d5c2009-08-12 22:56:50 +0200629 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100630 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200631 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200632 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200633
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100634
635 /* 16 bytes AUTN */
636 if (autn)
637 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
638
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800639 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200640}
641
642/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800643int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200644{
645 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800646 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200647}
648
Harald Welte2483f1b2016-06-19 18:06:02 +0200649static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200650static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum gsm48_reject_value result);
Harald Welte2483f1b2016-06-19 18:06:02 +0200651
652static int cm_serv_reuse_conn(struct gsm_subscriber_connection *conn, const uint8_t *mi_lv)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100653{
Harald Welte2483f1b2016-06-19 18:06:02 +0200654 uint8_t mi_type;
655 char mi_string[GSM48_MI_SIZE];
656 uint32_t tmsi;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100657
Harald Welte2483f1b2016-06-19 18:06:02 +0200658 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
659 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100660
Harald Welte2483f1b2016-06-19 18:06:02 +0200661 switch (mi_type) {
662 case GSM_MI_TYPE_IMSI:
663 if (vlr_subscr_matches_imsi(conn->vsub, mi_string))
664 goto accept_reuse;
665 break;
666 case GSM_MI_TYPE_TMSI:
667 tmsi = osmo_load32be(mi_lv+2);
668 if (vlr_subscr_matches_tmsi(conn->vsub, tmsi))
669 goto accept_reuse;
670 break;
671 case GSM_MI_TYPE_IMEI:
672 if (vlr_subscr_matches_imei(conn->vsub, mi_string))
673 goto accept_reuse;
674 break;
675 default:
676 break;
677 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100678
Harald Welte2483f1b2016-06-19 18:06:02 +0200679 LOGP(DMM, LOGL_ERROR, "%s: CM Service Request with mismatching mobile identity: %s %s\n",
680 vlr_subscr_name(conn->vsub), gsm48_mi_type_name(mi_type), mi_string);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200681 msc_vlr_tx_cm_serv_rej(conn, GSM48_REJECT_ILLEGAL_MS);
Harald Welte2483f1b2016-06-19 18:06:02 +0200682 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100683
Harald Welte2483f1b2016-06-19 18:06:02 +0200684accept_reuse:
685 DEBUGP(DMM, "%s: re-using already accepted connection\n",
686 vlr_subscr_name(conn->vsub));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200687
688 if (!conn->received_cm_service_request) {
689 conn->received_cm_service_request = true;
690 msc_subscr_conn_get(conn, MSC_CONN_USE_CM_SERVICE);
691 }
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200692 msc_subscr_conn_update_id(conn, conn->complete_layer3_type, mi_string);
Harald Welte2483f1b2016-06-19 18:06:02 +0200693 return conn->network->vlr->ops.tx_cm_serv_acc(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100694}
695
Harald Welte4ed0e922009-01-10 03:17:30 +0000696/*
697 * Handle CM Service Requests
698 * a) Verify that the packet is long enough to contain the information
699 * we require otherwsie reject with INCORRECT_MESSAGE
700 * b) Try to parse the TMSI. If we do not have one reject
701 * c) Check that we know the subscriber with the TMSI otherwise reject
702 * with a HLR cause
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200703 * d) Set the subscriber on the conn and accept
Harald Welte2483f1b2016-06-19 18:06:02 +0200704 *
705 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000706 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200707int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000708{
Harald Welte2483f1b2016-06-19 18:06:02 +0200709 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200710 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200711 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000712
Harald Welteba4cf162009-01-10 01:49:35 +0000713 struct gsm48_hdr *gh = msgb_l3(msg);
714 struct gsm48_service_request *req =
715 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800716 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200717 uint8_t classmark2_len = gh->data[1];
718 uint8_t *classmark2 = gh->data+2;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200719 uint8_t *mi_p = classmark2 + classmark2_len;
720 uint8_t mi_len = *mi_p;
721 uint8_t *mi = mi_p + 1;
Harald Welte2483f1b2016-06-19 18:06:02 +0200722 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200723 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200724
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100725 lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200726 lai.lac = conn->lac;
Harald Welteba4cf162009-01-10 01:49:35 +0000727
728 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200729 LOGP(DMM, LOGL_ERROR, "<- CM SERVICE REQUEST wrong sized message\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200730 return msc_gsm48_tx_mm_serv_rej(conn,
731 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000732 }
733
734 if (msg->data_len < req->mi_len + 6) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200735 LOGP(DMM, LOGL_ERROR, "<- CM SERVICE REQUEST does not fit in packet\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200736 return msc_gsm48_tx_mm_serv_rej(conn,
737 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000738 }
739
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200740 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000741 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200742 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x MI(%s)=%s\n",
743 req->cm_service_type, gsm48_mi_type_name(mi_type), mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200744
Harald Welte3995d2e2018-01-24 11:23:54 +0100745 switch (mi_type) {
746 case GSM_MI_TYPE_IMSI:
747 case GSM_MI_TYPE_TMSI:
748 /* continue below */
749 break;
750 case GSM_MI_TYPE_IMEI:
751 if (req->cm_service_type == GSM48_CMSERV_EMERGENCY) {
752 /* We don't do emergency calls by IMEI */
753 LOGP(DMM, LOGL_NOTICE, "<- CM SERVICE REQUEST(IMEI=%s) rejected\n", mi_string);
754 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_IMEI_NOT_ACCEPTED);
755 }
756 /* fall-through for non-emergency setup */
757 default:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200758 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200759 return msc_gsm48_tx_mm_serv_rej(conn,
760 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000761 }
762
Harald Welte37382ec2018-01-24 12:49:34 +0100763 switch (req->cm_service_type) {
764 case GSM48_CMSERV_MO_CALL_PACKET:
765 case GSM48_CMSERV_EMERGENCY:
766 case GSM48_CMSERV_SMS:
767 case GSM48_CMSERV_SUP_SERV:
768 /* continue below */
769 break;
770 default:
771 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
772 }
773
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200774 if (msc_subscr_conn_is_accepted(conn))
775 return cm_serv_reuse_conn(conn, mi_p);
Harald Welte2483f1b2016-06-19 18:06:02 +0200776
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200777 if (msc_subscr_conn_is_establishing_auth_ciph(conn)) {
778 LOGP(DMM, LOGL_ERROR,
779 "Cannot accept CM Service Request, conn already busy establishing authenticity\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200780 msc_vlr_tx_cm_serv_rej(conn, GSM48_REJECT_CONGESTION);
Harald Welte2483f1b2016-06-19 18:06:02 +0200781 return -EINVAL;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200782 /* or should we accept and note down the service request anyway? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200783 }
784
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200785 msc_subscr_conn_update_id(conn, COMPLETE_LAYER3_CM_SERVICE_REQ, mi_string);
Harald Welte7659de12009-12-13 12:39:18 +0100786
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200787 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, mi_p);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200788
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200789 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200790 vlr_proc_acc_req(conn->fi,
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200791 SUBSCR_CONN_E_ACCEPTED, SUBSCR_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +0200792 net->vlr, conn,
793 VLR_PR_ARQ_T_CM_SERV_REQ, mi-1, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200794 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100795 is_utran || conn->network->a5_encryption_mask > 0x01,
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200796 classmark2_is_r99(classmark2, classmark2_len),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200797 is_utran);
Holger Freythereb443982009-06-04 13:58:42 +0000798
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200799 /* From vlr_proc_acc_req() we expect an implicit dispatch of PR_ARQ_E_START we expect
800 * msc_vlr_subscr_assoc() to already have been called and completed. Has an error occured? */
801 if (!conn->vsub) {
802 LOGP(DRR, LOGL_ERROR, "%s: subscriber not allowed to do a CM Service Request\n",
803 mi_string);
804 return -EIO;
805 }
806
807 memcpy(conn->vsub->classmark.classmark2, classmark2, classmark2_len);
808 conn->vsub->classmark.classmark2_len = classmark2_len;
809
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200810 msc_subscr_conn_complete_layer_3(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +0200811 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000812}
813
Harald Welte49f8fcb2018-01-24 21:40:19 +0100814/* Receive a CM Re-establish Request */
815static int gsm48_rx_cm_reest_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
816{
817 uint8_t mi_type;
818 char mi_string[GSM48_MI_SIZE];
819 struct gsm48_hdr *gh = msgb_l3(msg);
820
821 uint8_t classmark2_len = gh->data[1];
822 uint8_t *classmark2 = gh->data+2;
823 uint8_t mi_len = *(classmark2 + classmark2_len);
824 uint8_t *mi = (classmark2 + classmark2_len + 1);
825
826 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
827 mi_type = mi[0] & GSM_MI_TYPE_MASK;
828 DEBUGP(DMM, "<- CM RE-ESTABLISH REQUEST MI(%s)=%s\n", gsm48_mi_type_name(mi_type), mi_string);
829
830 /* we don't support CM call re-establishment */
831 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
832}
833
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200834static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000835{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200836 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +0000837 struct gsm48_hdr *gh = msgb_l3(msg);
838 struct gsm48_imsi_detach_ind *idi =
839 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200840 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200841 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200842 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000843
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200844 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200845 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
846 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000847
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200848 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100849
Harald Welte2a139372009-02-22 21:14:55 +0000850 switch (mi_type) {
851 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200852 vsub = vlr_subscr_find_by_tmsi(network->vlr,
853 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000854 break;
855 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200856 vsub = vlr_subscr_find_by_imsi(network->vlr, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000857 break;
858 case GSM_MI_TYPE_IMEI:
859 case GSM_MI_TYPE_IMEISV:
860 /* no sim card... FIXME: what to do ? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200861 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
862 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000863 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200864 default:
Harald Welte2483f1b2016-06-19 18:06:02 +0200865 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
866 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000867 break;
868 }
869
Harald Welte2483f1b2016-06-19 18:06:02 +0200870 if (!vsub) {
871 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
872 gsm48_mi_type_name(mi_type), mi_string);
873 } else {
874 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
Neels Hofmeyrfacd57a2017-12-15 03:48:48 +0100875
876 if (vsub->cs.is_paging)
877 subscr_paging_cancel(vsub, GSM_PAGING_EXPIRED);
878
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200879 /* We already got Classmark 1 during Location Updating ... but well, ok */
880 vsub->classmark.classmark1 = idi->classmark1;
881
Harald Welte2483f1b2016-06-19 18:06:02 +0200882 vlr_subscr_rx_imsi_detach(vsub);
883 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
884 vlr_subscr_put(vsub);
885 }
Harald Welte2a139372009-02-22 21:14:55 +0000886
Harald Welte2483f1b2016-06-19 18:06:02 +0200887 msc_subscr_conn_close(conn, 0);
Harald Welte2a139372009-02-22 21:14:55 +0000888 return 0;
889}
890
Harald Welted2a7f5a2009-06-05 20:08:20 +0000891static int gsm48_rx_mm_status(struct msgb *msg)
892{
893 struct gsm48_hdr *gh = msgb_l3(msg);
894
895 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
896
897 return 0;
898}
899
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100900static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
901 struct gsm_subscriber_connection *conn,
902 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100903{
904 struct gsm48_hdr *gh = msgb_l3(msg);
905 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100906
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100907 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
908 LOGP(DMM, LOGL_ERROR,
909 "%s: MM AUTHENTICATION RESPONSE:"
910 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200911 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100912 return -EINVAL;
913 }
914
915 *res_len = sizeof(ar->sres);
916 memcpy(res, ar->sres, sizeof(ar->sres));
917 return 0;
918}
919
920static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
921 struct gsm_subscriber_connection *conn,
922 struct msgb *msg)
923{
924 struct gsm48_hdr *gh;
925 uint8_t *data;
926 uint8_t iei;
927 uint8_t ie_len;
928 unsigned int data_len;
929
930 /* First parse the GSM part */
931 if (parse_gsm_auth_resp(res, res_len, conn, msg))
932 return -EINVAL;
933 OSMO_ASSERT(*res_len == 4);
934
935 /* Then add the extended res part */
936 gh = msgb_l3(msg);
937 data = gh->data + sizeof(struct gsm48_auth_resp);
938 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
939
940 if (data_len < 3) {
941 LOGP(DMM, LOGL_ERROR,
942 "%s: MM AUTHENTICATION RESPONSE:"
943 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200944 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100945 return -EINVAL;
946 }
947
948 iei = data[0];
949 ie_len = data[1];
950 if (iei != GSM48_IE_AUTH_RES_EXT) {
951 LOGP(DMM, LOGL_ERROR,
952 "%s: MM R99 AUTHENTICATION RESPONSE:"
953 " expected IEI 0x%02x, got 0x%02x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200954 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100955 GSM48_IE_AUTH_RES_EXT, iei);
956 return -EINVAL;
957 }
958
959 if (ie_len > 12) {
960 LOGP(DMM, LOGL_ERROR,
961 "%s: MM R99 AUTHENTICATION RESPONSE:"
962 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200963 vlr_subscr_name(conn->vsub), GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100964 return -EINVAL;
965 }
966
967 *res_len += ie_len;
968 memcpy(res + 4, &data[2], ie_len);
969 return 0;
970}
971
972/* Chapter 9.2.3: Authentication Response */
973static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
974{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100975 uint8_t res[16];
976 uint8_t res_len;
977 int rc;
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100978 bool is_umts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100979
Harald Welte2483f1b2016-06-19 18:06:02 +0200980 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100981 LOGP(DMM, LOGL_ERROR,
982 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200983 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100984 return -EINVAL;
985 }
986
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100987 is_umts = (msgb_l3len(msg) > sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp));
988
989 if (is_umts)
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100990 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100991 else
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100992 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100993
994 if (rc) {
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +0100995 LOGP(DMM, LOGL_ERROR,
996 "%s: MM AUTHENTICATION RESPONSE: invalid: parsing %s AKA Auth Response"
997 " failed with rc=%d; dispatching zero length SRES/RES to trigger failure\n",
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100998 vlr_subscr_name(conn->vsub), is_umts ? "UMTS" : "GSM", rc);
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +0100999 memset(res, 0, sizeof(res));
1000 res_len = 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001001 }
1002
1003 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001004 vlr_subscr_name(conn->vsub),
Neels Hofmeyrd97821f2018-03-10 04:51:39 +01001005 is_umts ? "UMTS" : "GSM", is_umts ? "res" : "sres",
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001006 osmo_hexdump_nospc(res, res_len));
1007
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001008 return vlr_subscr_rx_auth_resp(conn->vsub, classmark_is_r99(&conn->vsub->classmark),
Harald Welte2483f1b2016-06-19 18:06:02 +02001009 conn->via_ran == RAN_UTRAN_IU,
1010 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001011}
1012
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001013static int gsm48_rx_mm_auth_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
1014{
1015 struct gsm48_hdr *gh = msgb_l3(msg);
1016 uint8_t cause;
1017 uint8_t auts_tag;
1018 uint8_t auts_len;
1019 uint8_t *auts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001020
Harald Welte2483f1b2016-06-19 18:06:02 +02001021 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001022 LOGP(DMM, LOGL_ERROR,
1023 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02001024 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001025 return -EINVAL;
1026 }
1027
1028 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
1029 LOGP(DMM, LOGL_ERROR,
1030 "%s: MM R99 AUTHENTICATION FAILURE:"
1031 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001032 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
1033 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001034 return -EINVAL;
1035 }
1036
1037 cause = gh->data[0];
1038
1039 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
1040 LOGP(DMM, LOGL_INFO,
1041 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001042 vlr_subscr_name(conn->vsub), cause);
1043 vlr_subscr_rx_auth_fail(conn->vsub, NULL);
1044 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001045 }
1046
1047 /* This is a Synch Failure procedure, which should pass an AUTS to
1048 * resynchronize the sequence nr with the HLR. Expecting exactly one
1049 * TLV with 14 bytes of AUTS. */
1050
1051 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
1052 LOGP(DMM, LOGL_INFO,
1053 "%s: MM R99 AUTHENTICATION FAILURE:"
1054 " invalid Synch Failure: missing AUTS IE\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001055 vlr_subscr_name(conn->vsub));
1056 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001057 return -EINVAL;
1058 }
1059
1060 auts_tag = gh->data[1];
1061 auts_len = gh->data[2];
1062 auts = &gh->data[3];
1063
1064 if (auts_tag != GSM48_IE_AUTS
1065 || auts_len != 14) {
1066 LOGP(DMM, LOGL_INFO,
1067 "%s: MM R99 AUTHENTICATION FAILURE:"
1068 " invalid Synch Failure:"
1069 " expected AUTS IE 0x%02x of 14 bytes,"
1070 " got IE 0x%02x of %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001071 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001072 GSM48_IE_AUTS, auts_tag, auts_len);
Harald Welte2483f1b2016-06-19 18:06:02 +02001073 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001074 return -EINVAL;
1075 }
1076
1077 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
1078 LOGP(DMM, LOGL_INFO,
1079 "%s: MM R99 AUTHENTICATION FAILURE:"
1080 " invalid Synch Failure msg: message truncated (%u)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001081 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
1082 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001083 return -EINVAL;
1084 }
1085
1086 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
1087 * large enough. */
1088
1089 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001090 vlr_subscr_name(conn->vsub), osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001091
Harald Welte2483f1b2016-06-19 18:06:02 +02001092 return vlr_subscr_rx_auth_fail(conn->vsub, auts);
1093}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001094
Harald Welte2483f1b2016-06-19 18:06:02 +02001095static int gsm48_rx_mm_tmsi_reall_compl(struct gsm_subscriber_connection *conn)
1096{
1097 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1098 vlr_subscr_name(conn->vsub));
1099 if (!conn->vsub) {
1100 LOGP(DMM, LOGL_ERROR,
1101 "Rx MM TMSI Reallocation Complete: invalid: no subscriber\n");
1102 return -EINVAL;
1103 }
1104 return vlr_subscr_rx_tmsi_reall_compl(conn->vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001105}
1106
Harald Weltebf5e8df2009-02-03 12:59:45 +00001107/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001108static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001109{
1110 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001111 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001112
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001113 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001114 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001115 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001116 break;
1117 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001118 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001119 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001120 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001121 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001122 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001123 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001124 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001125 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001126 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte2483f1b2016-06-19 18:06:02 +02001127 rc = gsm48_rx_mm_tmsi_reall_compl(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001128 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001129 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001130 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001131 break;
1132 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01001133 rc = gsm48_rx_cm_reest_req(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001134 break;
1135 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001136 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001137 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001138 case GSM48_MT_MM_AUTH_FAIL:
1139 rc = gsm48_rx_mm_auth_fail(conn, msg);
1140 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001141 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001142 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001143 gh->msg_type);
1144 break;
1145 }
1146
1147 return rc;
1148}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001149
Harald Welte2d35ae62009-02-06 12:02:13 +00001150/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001151static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001152{
Harald Welte2483f1b2016-06-19 18:06:02 +02001153 struct gsm_network *net = conn->network;
Harald Welte2d35ae62009-02-06 12:02:13 +00001154 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001155 struct gsm48_pag_resp *resp;
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001156 uint8_t classmark2_len = gh->data[1];
1157 uint8_t *classmark2 = gh->data+2;
1158 uint8_t *mi_lv = classmark2 + classmark2_len;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001159 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001160 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +02001161 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001162 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +02001163
Neels Hofmeyr379d5792018-02-22 04:04:54 +01001164 lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001165 lai.lac = conn->lac;
Harald Welte2d35ae62009-02-06 12:02:13 +00001166
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001167 resp = (struct gsm48_pag_resp *) &gh->data[0];
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001168
1169 if (gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh), mi_string, &mi_type) <= 0) {
1170 LOGP(DRR, LOGL_ERROR, "PAGING RESPONSE: invalid Mobile Identity\n");
1171 return -EINVAL;
1172 }
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001173
1174 if (msc_subscr_conn_is_establishing_auth_ciph(conn)) {
1175 LOGP(DMM, LOGL_ERROR,
1176 "Ignoring Paging Response, conn already busy establishing authenticity\n");
1177 return 0;
1178 }
1179
1180 if (msc_subscr_conn_is_accepted(conn)) {
1181 LOGP(DMM, LOGL_ERROR, "Ignoring Paging Response, conn already established\n");
1182 return 0;
1183 }
1184
1185 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n", gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001186
Neels Hofmeyr16c42b52018-04-02 12:26:16 +02001187 msc_subscr_conn_update_id(conn, COMPLETE_LAYER3_PAGING_RESP, mi_string);
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001188
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001189 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +02001190 vlr_proc_acc_req(conn->fi,
Neels Hofmeyr16c42b52018-04-02 12:26:16 +02001191 SUBSCR_CONN_E_ACCEPTED, SUBSCR_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +02001192 net->vlr, conn,
1193 VLR_PR_ARQ_T_PAGING_RESP, mi_lv, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001194 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +01001195 is_utran || conn->network->a5_encryption_mask > 0x01,
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001196 classmark2_is_r99(classmark2, classmark2_len),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001197 is_utran);
Holger Freyther053e09d2009-02-14 22:51:06 +00001198
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001199 /* From vlr_proc_acc_req() we expect an implicit dispatch of PR_ARQ_E_START we expect
1200 * msc_vlr_subscr_assoc() to already have been called and completed. Has an error occured? */
1201 if (!conn->vsub) {
1202 LOGP(DRR, LOGL_ERROR, "%s: subscriber not allowed to do a Paging Response\n",
1203 mi_string);
1204 return -EIO;
1205 }
1206
1207 memcpy(conn->vsub->classmark.classmark2, classmark2, classmark2_len);
1208 conn->vsub->classmark.classmark2_len = classmark2_len;
1209
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001210 msc_subscr_conn_complete_layer_3(conn);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001211 return 0;
Harald Welte2d35ae62009-02-06 12:02:13 +00001212}
1213
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001214static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001215{
1216 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001217 uint8_t apdu_id_flags;
1218 uint8_t apdu_len;
1219 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001220
1221 apdu_id_flags = gh->data[0];
1222 apdu_len = gh->data[1];
1223 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001224
Max8dc8f232017-02-09 19:13:02 +01001225 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001226 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001227
Harald Welte2483f1b2016-06-19 18:06:02 +02001228 /* we're not using the app info blob anywhere, so ignore. */
1229#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001230 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte2483f1b2016-06-19 18:06:02 +02001231#else
1232 return 0;
1233#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001234}
1235
Harald Weltebf5e8df2009-02-03 12:59:45 +00001236/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001237static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001238{
1239 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001240 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001241
1242 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001243 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001244 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001245 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001246 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001247 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001248 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001249 default:
Philippd37981e2016-10-10 15:11:41 +02001250 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1251 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001252 break;
1253 }
1254
Harald Welte2d35ae62009-02-06 12:02:13 +00001255 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001256}
1257
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001258int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1259 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001260{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001261 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001262 struct gsm48_hdr *gh;
1263
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001264 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1265 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001266
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001267 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1268 gh->proto_discr = GSM48_PDISC_RR;
1269 gh->msg_type = GSM48_MT_RR_APP_INFO;
1270 gh->data[0] = apdu_id;
1271 gh->data[1] = apdu_len;
1272 memcpy(gh->data+2, apdu, apdu_len);
1273
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001274 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001275}
1276
Harald Welte2483f1b2016-06-19 18:06:02 +02001277static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08001278{
Harald Welte2483f1b2016-06-19 18:06:02 +02001279 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
1280 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08001281
Harald Welte2483f1b2016-06-19 18:06:02 +02001282 switch (pdisc) {
1283 case GSM48_PDISC_MM:
1284 switch (msg_type) {
1285 case GSM48_MT_MM_LOC_UPD_REQUEST:
1286 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01001287 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte2483f1b2016-06-19 18:06:02 +02001288 case GSM48_MT_MM_AUTH_RESP:
1289 case GSM48_MT_MM_AUTH_FAIL:
1290 case GSM48_MT_MM_ID_RESP:
1291 case GSM48_MT_MM_TMSI_REALL_COMPL:
1292 case GSM48_MT_MM_IMSI_DETACH_IND:
1293 return true;
1294 default:
1295 break;
1296 }
1297 break;
1298 case GSM48_PDISC_RR:
1299 switch (msg_type) {
1300 case GSM48_MT_RR_CIPH_M_COMPL:
1301 case GSM48_MT_RR_PAG_RESP:
1302 return true;
1303 default:
1304 break;
1305 }
1306 break;
1307 default:
1308 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02001309 }
1310
Harald Welte2483f1b2016-06-19 18:06:02 +02001311 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02001312}
1313
Harald Welte2483f1b2016-06-19 18:06:02 +02001314void cm_service_request_concludes(struct gsm_subscriber_connection *conn,
1315 struct msgb *msg)
1316{
1317
1318 /* If a CM Service Request was received before, this is the request the
1319 * conn was opened for. No need to wait for further messages. */
1320
1321 if (!conn->received_cm_service_request)
1322 return;
1323
1324 if (log_check_level(DMM, LOGL_DEBUG)) {
1325 struct gsm48_hdr *gh = msgb_l3(msg);
1326 uint8_t pdisc = gsm48_hdr_pdisc(gh);
1327 uint8_t msg_type = gsm48_hdr_msg_type(gh);
1328
Neels Hofmeyr78ada642017-03-10 02:15:20 +01001329 DEBUGP(DMM, "%s: rx msg %s:"
Harald Welte2483f1b2016-06-19 18:06:02 +02001330 " received_cm_service_request changes to false\n",
1331 vlr_subscr_name(conn->vsub),
Neels Hofmeyr78ada642017-03-10 02:15:20 +01001332 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Harald Welte2483f1b2016-06-19 18:06:02 +02001333 }
1334 conn->received_cm_service_request = false;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001335 msc_subscr_conn_put(conn, MSC_CONN_USE_CM_SERVICE);
Harald Welte2483f1b2016-06-19 18:06:02 +02001336}
1337
Harald Weltec2007852018-02-03 21:08:26 +01001338/* TS 24.007 11.2.3.2.3 Message Type Octet / Duplicate Detection */
1339int gsm0407_pdisc_ctr_bin(uint8_t pdisc)
1340{
1341 switch (pdisc) {
1342 case GSM48_PDISC_MM:
1343 case GSM48_PDISC_CC:
1344 case GSM48_PDISC_NC_SS:
1345 return 0;
1346 case GSM48_PDISC_GROUP_CC:
1347 return 1;
1348 case GSM48_PDISC_BCAST_CC:
1349 return 2;
1350 case GSM48_PDISC_LOC:
1351 return 3;
1352 default:
1353 return -1;
1354 }
1355}
1356
1357/* extract the N(SD) and return the modulo value for a R98 message */
1358static uint8_t gsm0407_determine_nsd_ret_modulo_r99(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
1359{
1360 switch (pdisc) {
1361 case GSM48_PDISC_MM:
1362 case GSM48_PDISC_CC:
1363 case GSM48_PDISC_NC_SS:
1364 *n_sd = (msg_type >> 6) & 0x3;
1365 return 4;
1366 case GSM48_PDISC_GROUP_CC:
1367 case GSM48_PDISC_BCAST_CC:
1368 case GSM48_PDISC_LOC:
1369 *n_sd = (msg_type >> 6) & 0x1;
1370 return 2;
1371 default:
1372 /* no sequence number, we cannot detect dups */
1373 return 0;
1374 }
1375}
1376
1377/* extract the N(SD) and return the modulo value for a R99 message */
1378static uint8_t gsm0407_determine_nsd_ret_modulo_r98(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
1379{
1380 switch (pdisc) {
1381 case GSM48_PDISC_MM:
1382 case GSM48_PDISC_CC:
1383 case GSM48_PDISC_NC_SS:
1384 case GSM48_PDISC_GROUP_CC:
1385 case GSM48_PDISC_BCAST_CC:
1386 case GSM48_PDISC_LOC:
1387 *n_sd = (msg_type >> 6) & 0x1;
1388 return 2;
1389 default:
1390 /* no sequence number, we cannot detect dups */
1391 return 0;
1392 }
1393}
1394
1395/* TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection */
1396static bool gsm0407_is_duplicate(struct gsm_subscriber_connection *conn, struct msgb *msg)
1397{
1398 struct gsm48_hdr *gh;
1399 uint8_t pdisc;
1400 uint8_t n_sd, modulo, bin;
1401
1402 gh = msgb_l3(msg);
1403 pdisc = gsm48_hdr_pdisc(gh);
1404
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001405 if (conn->vsub && classmark_is_r99(&conn->vsub->classmark)) {
Harald Weltec2007852018-02-03 21:08:26 +01001406 modulo = gsm0407_determine_nsd_ret_modulo_r99(pdisc, gh->msg_type, &n_sd);
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001407 } else { /* pre R99 */
1408 modulo = gsm0407_determine_nsd_ret_modulo_r98(pdisc, gh->msg_type, &n_sd);
Harald Weltec2007852018-02-03 21:08:26 +01001409 }
1410 if (modulo == 0)
1411 return false;
1412 bin = gsm0407_pdisc_ctr_bin(pdisc);
1413 if (bin < 0)
1414 return false;
1415
1416 OSMO_ASSERT(bin < ARRAY_SIZE(conn->n_sd_next));
1417 if (n_sd != conn->n_sd_next[bin]) {
1418 /* not what we expected: duplicate */
1419 return true;
1420 } else {
1421 /* as expected: no dup; update expected counter for next message */
1422 conn->n_sd_next[bin] = (n_sd + 1) % modulo;
1423 return false;
1424 }
1425}
Harald Welte2483f1b2016-06-19 18:06:02 +02001426
Harald Welte27989d42018-06-21 20:39:20 +02001427extern int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg);
1428
Neels Hofmeyr378a4922016-05-09 21:07:43 +02001429/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08001430int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001431{
Philipp Maier91f10c72017-04-20 18:40:37 +02001432 struct gsm48_hdr *gh;
1433 uint8_t pdisc;
Harald Welte8470bf22008-12-25 23:28:35 +00001434 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01001435
Neels Hofmeyr0906a392017-09-19 14:36:06 +02001436 OSMO_ASSERT(msg->l3h);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02001437 OSMO_ASSERT(conn);
1438 OSMO_ASSERT(msg);
1439
Philipp Maier91f10c72017-04-20 18:40:37 +02001440 gh = msgb_l3(msg);
1441 pdisc = gsm48_hdr_pdisc(gh);
1442
Harald Weltec2007852018-02-03 21:08:26 +01001443 if (gsm0407_is_duplicate(conn, msg)) {
1444 LOGP(DRLL, LOGL_NOTICE, "%s: Discarding duplicate L3 message\n",
1445 (conn && conn->vsub) ? vlr_subscr_name(conn->vsub) : "UNKNOWN");
1446 return 0;
1447 }
1448
Neels Hofmeyr78ada642017-03-10 02:15:20 +01001449 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message %s (0x%x:0x%x)\n",
1450 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)),
1451 pdisc, gsm48_hdr_msg_type(gh));
Harald Welte2483f1b2016-06-19 18:06:02 +02001452
1453 if (!msc_subscr_conn_is_accepted(conn)
1454 && !msg_is_initially_permitted(gh)) {
1455 LOGP(DRLL, LOGL_ERROR,
Neels Hofmeyr78ada642017-03-10 02:15:20 +01001456 "subscr %s: Message not permitted for initial conn: %s\n",
1457 vlr_subscr_name(conn->vsub),
1458 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
Harald Welte2483f1b2016-06-19 18:06:02 +02001459 return -EACCES;
1460 }
1461
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001462 if (conn->vsub && conn->vsub->cs.attached_via_ran != conn->via_ran) {
1463 LOGP(DMM, LOGL_ERROR,
1464 "%s: Illegal situation: RAN type mismatch:"
1465 " attached via %s, received message via %s\n",
1466 vlr_subscr_name(conn->vsub),
1467 ran_type_name(conn->vsub->cs.attached_via_ran),
1468 ran_type_name(conn->via_ran));
1469 return -EACCES;
1470 }
1471
Philipp Maiere0d5caa2017-02-27 16:56:59 +01001472#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08001473 if (silent_call_reroute(conn, msg))
1474 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01001475#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001476
Harald Welte52b1f982008-12-23 20:25:15 +00001477 switch (pdisc) {
1478 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001479 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001480 break;
1481 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001482 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001483 break;
1484 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001485 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001486 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00001487 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08001488 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00001489 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001490 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00001491 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01001492 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
1493 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02001494 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00001495 break;
Harald Welte6eafe912009-10-16 08:32:58 +02001496 case GSM48_PDISC_NC_SS:
Vadim Yanitskiy5b860fa2018-06-12 05:24:52 +07001497 rc = gsm0911_rcv_nc_ss(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02001498 break;
Harald Welteeac38c32017-05-29 18:02:53 +02001499 case GSM48_PDISC_TEST:
1500 rc = gsm0414_rcv_test(conn, msg);
1501 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001502 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001503 LOGP(DRLL, LOGL_NOTICE, "Unknown "
1504 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02001505 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00001506 break;
1507 }
1508
1509 return rc;
1510}
Harald Welte8470bf22008-12-25 23:28:35 +00001511
Harald Welte2483f1b2016-06-19 18:06:02 +02001512/***********************************************************************
1513 * VLR integration
1514 ***********************************************************************/
1515
1516/* VLR asks us to send an authentication request */
1517static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
1518 bool send_autn)
1519{
1520 struct gsm_subscriber_connection *conn = msc_conn_ref;
1521 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
1522 send_autn? at->vec.autn : NULL,
1523 at->key_seq);
1524}
1525
1526/* VLR asks us to send an authentication reject */
1527static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
1528{
1529 struct gsm_subscriber_connection *conn = msc_conn_ref;
1530 return gsm48_tx_mm_auth_rej(conn);
1531}
1532
1533/* VLR asks us to transmit an Identity Request of given type */
1534static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
1535{
1536 struct gsm_subscriber_connection *conn = msc_conn_ref;
1537 return mm_tx_identity_req(conn, mi_type);
1538}
1539
1540/* VLR asks us to transmit a Location Update Accept */
1541static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
1542{
1543 struct gsm_subscriber_connection *conn = msc_conn_ref;
1544 return gsm0408_loc_upd_acc(conn, send_tmsi);
1545}
1546
1547/* VLR asks us to transmit a Location Update Reject */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001548static int msc_vlr_tx_lu_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Harald Welte2483f1b2016-06-19 18:06:02 +02001549{
1550 struct gsm_subscriber_connection *conn = msc_conn_ref;
1551 return gsm0408_loc_upd_rej(conn, cause);
1552}
1553
1554/* VLR asks us to transmit a CM Service Accept */
1555static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
1556{
1557 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001558 return msc_gsm48_tx_mm_serv_ack(conn);
1559}
1560
1561static int msc_vlr_tx_common_id(void *msc_conn_ref)
1562{
1563 struct gsm_subscriber_connection *conn = msc_conn_ref;
1564 return msc_tx_common_id(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +02001565}
1566
Stefan Sperling3a741282018-03-13 21:11:49 +01001567/* VLR asks us to transmit MM info. */
1568static int msc_vlr_tx_mm_info(void *msc_conn_ref)
1569{
1570 struct gsm_subscriber_connection *conn = msc_conn_ref;
1571 if (!conn->network->send_mm_info)
1572 return 0;
1573 return gsm48_tx_mm_info(conn);
1574}
1575
Harald Welte2483f1b2016-06-19 18:06:02 +02001576/* VLR asks us to transmit a CM Service Reject */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001577static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Harald Welte2483f1b2016-06-19 18:06:02 +02001578{
Harald Welte2483f1b2016-06-19 18:06:02 +02001579 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001580 int rc;
Harald Welte2483f1b2016-06-19 18:06:02 +02001581
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001582 rc = msc_gsm48_tx_mm_serv_rej(conn, cause);
1583
1584 if (conn->received_cm_service_request) {
1585 conn->received_cm_service_request = false;
1586 msc_subscr_conn_put(conn, MSC_CONN_USE_CM_SERVICE);
1587 }
1588
1589 return rc;
Harald Welte2483f1b2016-06-19 18:06:02 +02001590}
1591
Neels Hofmeyr703638e2017-12-14 05:30:16 +01001592/* For msc_vlr_set_ciph_mode() */
1593osmo_static_assert(sizeof(((struct gsm0808_encrypt_info*)0)->key) >= sizeof(((struct osmo_auth_vector*)0)->kc),
1594 gsm0808_encrypt_info_key_fits_osmo_auth_vec_kc);
1595
Neels Hofmeyr3117b702018-09-13 03:23:07 +02001596int msc_geran_set_cipher_mode(struct gsm_subscriber_connection *conn, bool umts_aka, bool retrieve_imeisv)
1597{
1598 struct gsm_network *net = conn->network;
1599 struct gsm0808_encrypt_info ei;
1600 int i, j = 0;
1601 int request_classmark = 0;
1602 int request_classmark_for_a5_n = 0;
1603 struct gsm_auth_tuple *tuple = conn->vsub->last_tuple;
1604
1605 if (!conn || !conn->vsub || !conn->vsub->last_tuple) {
1606 /* This should really never happen, because we checked this in msc_vlr_set_ciph_mode()
1607 * already. */
1608 LOGP(DMM, LOGL_ERROR, "Internal error: missing state during Ciphering Mode Command\n");
1609 return -EINVAL;
1610 }
1611
1612 for (i = 0; i < 8; i++) {
1613 int supported;
1614
1615 /* A5/n permitted by osmo-msc.cfg? */
1616 if (!(net->a5_encryption_mask & (1 << i)))
1617 continue;
1618
1619 /* A5/n supported by MS? */
1620 supported = classmark_supports_a5(&conn->vsub->classmark, i);
1621 if (supported == 1) {
1622 ei.perm_algo[j++] = vlr_ciph_to_gsm0808_alg_id(i);
1623 /* A higher A5/n is supported, so no need to request a Classmark
1624 * for support of a lesser A5/n. */
1625 request_classmark = 0;
1626 } else if (supported < 0) {
1627 request_classmark = -supported;
1628 request_classmark_for_a5_n = i;
1629 }
1630 }
1631 ei.perm_algo_len = j;
1632
1633 if (request_classmark) {
1634 /* The highest A5/n as from osmo-msc.cfg might be available, but we are
1635 * still missing the Classmark information for that from the MS. First
1636 * ask for that. */
1637 LOGP(DMM, LOGL_DEBUG, "%s: to determine whether A5/%d is supported,"
1638 " first ask for a Classmark Update to obtain Classmark %d\n",
1639 vlr_subscr_name(conn->vsub), request_classmark_for_a5_n,
1640 request_classmark);
1641
1642 return msc_classmark_request_then_cipher_mode_cmd(conn, umts_aka, retrieve_imeisv);
1643 }
1644
1645 if (ei.perm_algo_len == 0) {
1646 LOGP(DMM, LOGL_ERROR, "%s: cannot start ciphering, no intersection "
1647 "between MSC-configured and MS-supported A5 algorithms\n",
1648 vlr_subscr_name(conn->vsub));
1649 return -ENOTSUP;
1650 }
1651
1652 DEBUGP(DMM, "-> CIPHER MODE COMMAND %s\n", vlr_subscr_name(conn->vsub));
1653
1654 tuple = conn->vsub->last_tuple;
1655
1656 /* In case of UMTS AKA, the Kc for ciphering must be derived from the 3G auth
1657 * tokens. tuple->vec.kc was calculated from the GSM algorithm and is not
1658 * necessarily a match for the UMTS AKA tokens. */
1659 if (umts_aka)
1660 osmo_auth_c3(ei.key, tuple->vec.ck, tuple->vec.ik);
1661 else
1662 memcpy(ei.key, tuple->vec.kc, sizeof(tuple->vec.kc));
1663 ei.key_len = sizeof(tuple->vec.kc);
1664
1665 return a_iface_tx_cipher_mode(conn, &ei, retrieve_imeisv);
1666}
1667
Neels Hofmeyrda21a522018-03-02 01:50:09 +01001668/* VLR asks us to start using ciphering.
1669 * (Keep non-static to allow regression testing on this function.) */
1670int msc_vlr_set_ciph_mode(void *msc_conn_ref,
1671 bool umts_aka,
1672 bool retrieve_imeisv)
Harald Welte2483f1b2016-06-19 18:06:02 +02001673{
1674 struct gsm_subscriber_connection *conn = msc_conn_ref;
1675 struct vlr_subscr *vsub;
1676 struct gsm_auth_tuple *tuple;
1677
1678 if (!conn || !conn->vsub) {
1679 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
1680 " NULL conn/subscriber");
1681 return -EINVAL;
1682 }
1683
1684 vsub = conn->vsub;
1685 tuple = vsub->last_tuple;
1686
1687 if (!tuple) {
1688 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
1689 " Command: no auth tuple available\n",
1690 vlr_subscr_name(vsub));
1691 return -EINVAL;
1692 }
1693
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001694 switch (conn->via_ran) {
1695 case RAN_GERAN_A:
Neels Hofmeyr3117b702018-09-13 03:23:07 +02001696 return msc_geran_set_cipher_mode(conn, umts_aka, retrieve_imeisv);
Neels Hofmeyr703638e2017-12-14 05:30:16 +01001697
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001698 case RAN_UTRAN_IU:
1699#ifdef BUILD_IU
1700 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n",
1701 vlr_subscr_name(conn->vsub));
Neels Hofmeyr00e82d62017-07-05 15:19:52 +02001702 return ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &tuple->vec, 0, 1);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001703#else
1704 LOGP(DMM, LOGL_ERROR, "Cannot send Security Mode Control over RAN_UTRAN_IU,"
1705 " built without Iu support\n");
1706 return -ENOTSUP;
1707#endif
1708
1709 default:
1710 break;
1711 }
1712 LOGP(DMM, LOGL_ERROR,
1713 "%s: cannot start ciphering, unknown RAN type %d\n",
1714 vlr_subscr_name(conn->vsub), conn->via_ran);
1715 return -ENOTSUP;
1716}
1717
1718void msc_rx_sec_mode_compl(struct gsm_subscriber_connection *conn)
1719{
1720 struct vlr_ciph_result vlr_res = {};
1721
1722 if (!conn || !conn->vsub) {
1723 LOGP(DMM, LOGL_ERROR,
1724 "Rx Security Mode Complete for invalid conn\n");
1725 return;
1726 }
1727
1728 DEBUGP(DMM, "<- SECURITY MODE COMPLETE %s\n",
1729 vlr_subscr_name(conn->vsub));
1730
1731 vlr_res.cause = VLR_CIPH_COMPL;
1732 vlr_subscr_rx_ciph_res(conn->vsub, &vlr_res);
Harald Welte2483f1b2016-06-19 18:06:02 +02001733}
1734
1735/* VLR informs us that the subscriber data has somehow been modified */
1736static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
1737{
Maxa263bb22017-12-27 13:23:44 +01001738 LOGVSUBP(LOGL_NOTICE, subscr, "VLR: update for IMSI=%s (MSISDN=%s, used=%d)\n",
1739 subscr->imsi, subscr->msisdn, subscr->use_count);
Harald Welte2483f1b2016-06-19 18:06:02 +02001740}
1741
1742/* VLR informs us that the subscriber has been associated with a conn */
1743static void msc_vlr_subscr_assoc(void *msc_conn_ref,
1744 struct vlr_subscr *vsub)
1745{
1746 struct gsm_subscriber_connection *conn = msc_conn_ref;
Max48d4ec02018-02-12 16:51:03 +01001747 OSMO_ASSERT(vsub);
Harald Welte2483f1b2016-06-19 18:06:02 +02001748 OSMO_ASSERT(!conn->vsub);
1749 conn->vsub = vlr_subscr_get(vsub);
Max48d4ec02018-02-12 16:51:03 +01001750 OSMO_ASSERT(conn->vsub);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001751 conn->vsub->cs.attached_via_ran = conn->via_ran;
Harald Welte2483f1b2016-06-19 18:06:02 +02001752}
1753
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001754static int msc_vlr_route_gsup_msg(struct vlr_subscr *vsub,
1755 struct osmo_gsup_message *gsup_msg)
1756{
1757 switch (gsup_msg->message_type) {
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +07001758 /* GSM 09.11 code implementing SS/USSD */
1759 case OSMO_GSUP_MSGT_PROC_SS_REQUEST:
1760 case OSMO_GSUP_MSGT_PROC_SS_RESULT:
1761 case OSMO_GSUP_MSGT_PROC_SS_ERROR:
1762 DEBUGP(DMSC, "Routed to GSM 09.11 SS/USSD handler\n");
1763 return gsm0911_gsup_handler(vsub, gsup_msg);
1764
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001765 default:
1766 LOGP(DMM, LOGL_ERROR, "No handler found for %s, dropping message...\n",
1767 osmo_gsup_message_type_name(gsup_msg->message_type));
1768 return -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
1769 }
1770}
1771
Harald Welte2483f1b2016-06-19 18:06:02 +02001772/* operations that we need to implement for libvlr */
1773static const struct vlr_ops msc_vlr_ops = {
1774 .tx_auth_req = msc_vlr_tx_auth_req,
1775 .tx_auth_rej = msc_vlr_tx_auth_rej,
1776 .tx_id_req = msc_vlr_tx_id_req,
1777 .tx_lu_acc = msc_vlr_tx_lu_acc,
1778 .tx_lu_rej = msc_vlr_tx_lu_rej,
1779 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
1780 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
1781 .set_ciph_mode = msc_vlr_set_ciph_mode,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001782 .tx_common_id = msc_vlr_tx_common_id,
Stefan Sperling3a741282018-03-13 21:11:49 +01001783 .tx_mm_info = msc_vlr_tx_mm_info,
Harald Welte2483f1b2016-06-19 18:06:02 +02001784 .subscr_update = msc_vlr_subscr_update,
1785 .subscr_assoc = msc_vlr_subscr_assoc,
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001786 .forward_gsup_msg = msc_vlr_route_gsup_msg,
Harald Welte2483f1b2016-06-19 18:06:02 +02001787};
1788
1789/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
1790int msc_vlr_alloc(struct gsm_network *net)
1791{
1792 net->vlr = vlr_alloc(net, &msc_vlr_ops);
1793 if (!net->vlr)
1794 return -ENOMEM;
1795 net->vlr->user_ctx = net;
1796 return 0;
1797}
1798
1799/* Launch the VLR, i.e. its GSUP connection */
1800int msc_vlr_start(struct gsm_network *net)
1801{
1802 OSMO_ASSERT(net->vlr);
1803 return vlr_start("MSC", net->vlr, net->gsup_server_addr_str,
1804 net->gsup_server_port);
1805}
Neels Hofmeyr79e580b2018-03-22 16:48:22 +01001806
1807struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value)
1808{
1809 struct msgb *msg;
1810 struct gsm48_hdr *gh;
1811
1812 msg = gsm48_msgb_alloc_name("GSM 04.08 SERV REJ");
1813 if (!msg)
1814 return NULL;
1815
1816 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1817 gh->proto_discr = GSM48_PDISC_MM;
1818 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1819 gh->data[0] = value;
1820
1821 return msg;
1822}
1823
1824struct msgb *gsm48_create_loc_upd_rej(uint8_t cause)
1825{
1826 struct gsm48_hdr *gh;
1827 struct msgb *msg;
1828
1829 msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD REJ");
1830 if (!msg)
1831 return NULL;
1832
1833 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1834 gh->proto_discr = GSM48_PDISC_MM;
1835 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
1836 gh->data[0] = cause;
1837 return msg;
1838}
1839
1840int gsm48_extract_mi(uint8_t *classmark2_lv, int length, char *mi_string, uint8_t *mi_type)
1841{
1842 /* Check the size for the classmark */
1843 if (length < 1 + *classmark2_lv)
1844 return -1;
1845
1846 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
1847 if (length < 2 + *classmark2_lv + mi_lv[0])
1848 return -2;
1849
1850 *mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
1851 return gsm48_mi_to_string(mi_string, GSM48_MI_SIZE, mi_lv+1, *mi_lv);
1852}
1853
1854int gsm48_paging_extract_mi(struct gsm48_pag_resp *resp, int length,
1855 char *mi_string, uint8_t *mi_type)
1856{
1857 static const uint32_t classmark_offset =
1858 offsetof(struct gsm48_pag_resp, classmark2);
1859 uint8_t *classmark2_lv = (uint8_t *) &resp->classmark2;
1860 return gsm48_extract_mi(classmark2_lv, length - classmark_offset,
1861 mi_string, mi_type);
1862}