blob: b942a0391a07df904422bfb2805706c749fe4353 [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
Harald Welte71330722017-12-23 19:59:02 +0100122/* Determine if the given CLASSMARK (1/2/3) value permits a given A5/n cipher */
123static bool classmark_supports_a5(const struct gsm_classmark *cm, uint8_t a5)
124{
125 switch (a5) {
126 case 0:
127 /* all phones must implement A5/0, see 3GPP TS 43.020 4.9 */
128 return true;
129 case 1:
130 /* 3GPP TS 43.020 4.9 requires A5/1 to be suppored by all phones and actually states:
131 * "The network shall not provide service to an MS which indicates that it does not
132 * support the ciphering algorithm A5/1.". However, let's be more tolerant based
133 * on policy here */
134 /* See 3GPP TS 24.008 10.5.1.7 */
135 if (!cm->classmark1_set) {
136 DEBUGP(DMSC, "CLASSMARK 1 unknown, assuming MS supports A5/1\n");
137 return true;
138 } else {
139 if (cm->classmark1.a5_1)
140 return false; /* Inverted logic for this bit! */
141 else
142 return true;
143 }
144 break;
145 case 2:
146 case 3:
147 /* See 3GPP TS 24.008 10.5.1.6 */
148 if (cm->classmark2_len < 3) {
149 DEBUGP(DMSC, "CLASSMARK 2 unknown, assuming MS doesn't support A5/%u\n", a5);
150 return false;
151 } else {
152 if (cm->classmark2[2] & (1 << (a5-2)))
153 return true;
154 else
155 return false;
156 }
157 break;
158 case 4:
159 case 5:
160 case 6:
161 case 7:
162 /* See 3GPP TS 24.008 10.5.1.7 */
163 if (cm->classmark3_len < 1) {
164 DEBUGP(DMSC, "CLASSMARK 3 unknown, assuming MS doesn't support A5/%u\n", a5);
165 return false;
166 } else {
167 if (cm->classmark3[0] & (1 << (a5-4)))
168 return true;
169 else
170 return false;
171 }
172 break;
173 default:
174 return false;
175 }
176}
177
Harald Welte27989d42018-06-21 20:39:20 +0200178int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn, struct gsm_trans *trans)
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800179{
180 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
181
182 /* if we get passed a transaction reference, do some common
183 * work that the caller no longer has to do */
184 if (trans) {
185 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
Harald Welte0e2fa5d2018-04-09 16:35:01 +0200186 OMSC_LINKID_CB(msg) = trans->dlci;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800187 }
188
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200189 return msc_tx_dtap(conn, msg);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800190}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100191
Harald Welte2483f1b2016-06-19 18:06:02 +0200192/* clear all transactions globally; used in case of MNCC socket disconnect */
Harald Welte371efe52010-12-22 23:17:50 +0100193void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
194{
195 struct gsm_trans *trans, *temp;
196
197 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
198
199 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100200 if (trans->protocol == protocol) {
201 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100202 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100203 }
Harald Welte371efe52010-12-22 23:17:50 +0100204 }
205}
206
Holger Freyther429e7762008-12-30 13:28:30 +0000207/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200208static int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000209{
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800210 struct msgb *msg;
211
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800212 msg = gsm48_create_loc_upd_rej(cause);
213 if (!msg) {
214 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
215 return -1;
216 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200217
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200218 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT\n",
219 vlr_subscr_name(conn->vsub));
Harald Welte24ff6ee2009-12-22 00:41:05 +0100220
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800221 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000222}
223
224/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte2483f1b2016-06-19 18:06:02 +0200225static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
226 uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000227{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100228 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000229 struct gsm48_hdr *gh;
230 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200231 uint8_t *mid;
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100232 struct osmo_location_area_id laid = {
233 .plmn = conn->network->plmn,
234 .lac = conn->lac,
235 };
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200236
Harald Welte52b1f982008-12-23 20:25:15 +0000237 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
238 gh->proto_discr = GSM48_PDISC_MM;
239 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
240
241 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100242 gsm48_generate_lai2(lai, &laid);
Harald Welte52b1f982008-12-23 20:25:15 +0000243
Harald Welte2483f1b2016-06-19 18:06:02 +0200244 if (send_tmsi == GSM_RESERVED_TMSI) {
245 /* we did not allocate a TMSI to the MS, so we need to
246 * include the IMSI in order for the MS to delete any
247 * old TMSI that might still be allocated */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200248 uint8_t mi[10];
249 int len;
Harald Welte2483f1b2016-06-19 18:06:02 +0200250 len = gsm48_generate_mid_from_imsi(mi, conn->vsub->imsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200251 mid = msgb_put(msg, len);
252 memcpy(mid, mi, len);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200253 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT\n",
254 vlr_subscr_name(conn->vsub));
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200255 } else {
Harald Welte2483f1b2016-06-19 18:06:02 +0200256 /* Include the TMSI, which means that the MS will send a
257 * TMSI REALLOCATION COMPLETE, and we should wait for
258 * that until T3250 expiration */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200259 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Welte2483f1b2016-06-19 18:06:02 +0200260 gsm48_generate_mid_from_tmsi(mid, send_tmsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200261 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT (TMSI = 0x%08x)\n",
262 vlr_subscr_name(conn->vsub),
263 send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200264 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200265 /* TODO: Follow-on proceed */
266 /* TODO: CTS permission */
267 /* TODO: Equivalent PLMNs */
268 /* TODO: Emergency Number List */
269 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000270
Harald Welte52b1f982008-12-23 20:25:15 +0000271
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800272 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000273}
274
Harald Weltebf5e8df2009-02-03 12:59:45 +0000275/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200276static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000277{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100278 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000279 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000280
Harald Welte231ad4f2008-12-27 11:15:38 +0000281 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
282 gh->proto_discr = GSM48_PDISC_MM;
283 gh->msg_type = GSM48_MT_MM_ID_REQ;
284 gh->data[0] = id_type;
285
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800286 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000287}
288
Harald Weltebf5e8df2009-02-03 12:59:45 +0000289/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800290static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000291{
292 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200293 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200294 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000295
Harald Welte2483f1b2016-06-19 18:06:02 +0200296 if (!conn->vsub) {
297 LOGP(DMM, LOGL_ERROR,
298 "Rx MM Identity Response: invalid: no subscriber\n");
299 return -EINVAL;
300 }
301
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200302 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200303 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
304 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000305
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200306 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100307
Harald Welte2483f1b2016-06-19 18:06:02 +0200308 return vlr_subscr_rx_id_resp(conn->vsub, gh->data+1, gh->data[0]);
Harald Welte231ad4f2008-12-27 11:15:38 +0000309}
310
Harald Welte2483f1b2016-06-19 18:06:02 +0200311/* FIXME: to libosmogsm */
Harald Welteb9845f92015-08-16 18:07:48 +0200312static const struct value_string lupd_names[] = {
313 { GSM48_LUPD_NORMAL, "NORMAL" },
314 { GSM48_LUPD_PERIODIC, "PERIODIC" },
315 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
316 { 0, NULL }
317};
Harald Welte2a139372009-02-22 21:14:55 +0000318
Harald Welte2483f1b2016-06-19 18:06:02 +0200319/* Chapter 9.2.15: Receive Location Updating Request.
320 * Keep this function non-static for direct invocation by unit tests. */
321int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000322{
Harald Welte2483f1b2016-06-19 18:06:02 +0200323 struct gsm_network *net = conn->network;
Harald Welte8470bf22008-12-25 23:28:35 +0000324 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000325 struct gsm48_loc_upd_req *lu;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200326 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200327 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200328 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte2483f1b2016-06-19 18:06:02 +0200329 uint32_t tmsi;
330 char *imsi;
331 struct osmo_location_area_id old_lai, new_lai;
332 struct osmo_fsm_inst *lu_fsm;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200333 bool is_utran;
Harald Welte52b1f982008-12-23 20:25:15 +0000334
Harald Welte8470bf22008-12-25 23:28:35 +0000335 lu = (struct gsm48_loc_upd_req *) gh->data;
336
337 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000338
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200339 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000340
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200341 if (msc_subscr_conn_is_establishing_auth_ciph(conn)) {
342 LOGP(DMM, LOGL_ERROR,
343 "Cannot accept another LU, conn already busy establishing authenticity;"
344 " extraneous LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
345 gsm48_mi_type_name(mi_type), mi_string, get_value_string(lupd_names, lu->type));
346 return -EINVAL;
347 }
348
349 if (msc_subscr_conn_is_accepted(conn)) {
350 LOGP(DMM, LOGL_ERROR,
351 "Cannot accept another LU, conn already established;"
352 " extraneous LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
353 gsm48_mi_type_name(mi_type), mi_string, get_value_string(lupd_names, lu->type));
354 return -EINVAL;
355 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200356
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200357 msc_subscr_conn_update_id(conn, COMPLETE_LAYER3_LU, mi_string);
358
Harald Welte2483f1b2016-06-19 18:06:02 +0200359 DEBUGP(DMM, "LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
360 gsm48_mi_type_name(mi_type), mi_string,
361 get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000362
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200363 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100364
Harald Welte24ff6ee2009-12-22 00:41:05 +0100365 switch (lu->type) {
366 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200367 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200368 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100369 break;
370 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200371 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200372 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100373 break;
374 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200375 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200376 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100377 break;
378 }
379
Harald Welte2483f1b2016-06-19 18:06:02 +0200380 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
381 /* TODO: 10.5.3.14 Aditional update parameters (CS fallback calls) */
382 /* TODO: 10.5.7.8 Device properties */
383 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200384
Harald Welte52b1f982008-12-23 20:25:15 +0000385 switch (mi_type) {
386 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200387 tmsi = GSM_RESERVED_TMSI;
388 imsi = mi_string;
Harald Welte4b634542008-12-27 01:55:51 +0000389 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000390 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200391 tmsi = tmsi_from_string(mi_string);
392 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000393 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200394 default:
Harald Weltea0368542009-06-27 02:58:43 +0200395 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200396 tmsi = GSM_RESERVED_TMSI;
397 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000398 break;
399 }
400
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100401 gsm48_decode_lai2(&lu->lai, &old_lai);
402 new_lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200403 new_lai.lac = conn->lac;
Harald Welte2483f1b2016-06-19 18:06:02 +0200404 DEBUGP(DMM, "LU/new-LAC: %u/%u\n", old_lai.lac, new_lai.lac);
Harald Welte24516ea2009-07-04 10:18:00 +0200405
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200406 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200407 lu_fsm = vlr_loc_update(conn->fi,
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200408 SUBSCR_CONN_E_ACCEPTED, SUBSCR_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +0200409 net->vlr, conn, vlr_lu_type, tmsi, imsi,
410 &old_lai, &new_lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200411 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100412 is_utran || conn->network->a5_encryption_mask > 0x01,
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200413 classmark1_is_r99(&lu->classmark1),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200414 is_utran,
Harald Welte2483f1b2016-06-19 18:06:02 +0200415 net->vlr->cfg.assign_tmsi);
416 if (!lu_fsm) {
417 DEBUGP(DRR, "%s: Can't start LU FSM\n", mi_string);
418 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800419 }
420
Harald Welte2483f1b2016-06-19 18:06:02 +0200421 /* From vlr_loc_update() we expect an implicit dispatch of
422 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
423 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000424
Harald Welte2483f1b2016-06-19 18:06:02 +0200425 if (!conn->vsub || conn->vsub->lu_fsm != lu_fsm) {
426 LOGP(DRR, LOGL_ERROR,
427 "%s: internal error during Location Updating attempt\n",
428 mi_string);
429 return -EIO;
430 }
431
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200432 conn->vsub->classmark.classmark1 = lu->classmark1;
433 conn->vsub->classmark.classmark1_set = true;
434
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200435 msc_subscr_conn_complete_layer_3(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +0200436 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000437}
438
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100439/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200440/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100441static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800442{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100443 uint8_t ret;
444
445 ret = value / 10;
446 ret |= (value % 10) << 4;
447
448 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800449}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100450
Harald Welte4bfdfe72009-06-10 23:11:52 +0800451
Harald Weltedb253af2008-12-30 17:56:55 +0000452/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800453int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000454{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100455 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000456 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200457 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200458 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200459 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100460
Harald Welte4bfdfe72009-06-10 23:11:52 +0800461 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100462 struct tm* gmt_time;
463 struct tm* local_time;
464 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100465 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000466
Harald Weltedb253af2008-12-30 17:56:55 +0000467 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
468 gh->proto_discr = GSM48_PDISC_MM;
469 gh->msg_type = GSM48_MT_MM_INFO;
470
471 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200472#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000473 name_len = strlen(net->name_long);
474 /* 10.5.3.5a */
475 ptr8 = msgb_put(msg, 3);
476 ptr8[0] = GSM48_IE_NAME_LONG;
477 ptr8[1] = name_len*2 +1;
478 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
479
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200480 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000481 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000482 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000483
484 /* FIXME: Use Cell Broadcast, not UCS-2, since
485 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200486#endif
487 name_len = (strlen(net->name_long)*7)/8;
488 name_pad = (8 - strlen(net->name_long)*7)%8;
489 if (name_pad > 0)
490 name_len++;
491 /* 10.5.3.5a */
492 ptr8 = msgb_put(msg, 3);
493 ptr8[0] = GSM48_IE_NAME_LONG;
494 ptr8[1] = name_len +1;
495 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
496
497 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100498 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200499
Harald Weltedb253af2008-12-30 17:56:55 +0000500 }
501
502 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200503#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000504 name_len = strlen(net->name_short);
505 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200506 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200507 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000508 ptr8[1] = name_len*2 + 1;
509 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
510
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200511 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000512 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000513 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200514#endif
515 name_len = (strlen(net->name_short)*7)/8;
516 name_pad = (8 - strlen(net->name_short)*7)%8;
517 if (name_pad > 0)
518 name_len++;
519 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200520 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200521 ptr8[0] = GSM48_IE_NAME_SHORT;
522 ptr8[1] = name_len +1;
523 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
524
525 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100526 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200527
Harald Weltedb253af2008-12-30 17:56:55 +0000528 }
529
Harald Weltedb253af2008-12-30 17:56:55 +0000530 /* Section 10.5.3.9 */
531 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100532 gmt_time = gmtime(&cur_t);
533
Harald Weltedb253af2008-12-30 17:56:55 +0000534 ptr8 = msgb_put(msg, 8);
535 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100536 ptr8[1] = bcdify(gmt_time->tm_year % 100);
537 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
538 ptr8[3] = bcdify(gmt_time->tm_mday);
539 ptr8[4] = bcdify(gmt_time->tm_hour);
540 ptr8[5] = bcdify(gmt_time->tm_min);
541 ptr8[6] = bcdify(gmt_time->tm_sec);
542
Neels Hofmeyr73983952016-05-10 13:29:33 +0200543 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200544 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200545 if (net->tz.hr < 0) {
546 tzunits = ((net->tz.hr/-1)*4);
547 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100548 ptr8[7] = bcdify(tzunits);
549 /* Set negative time */
550 ptr8[7] |= 0x08;
551 }
552 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200553 tzunits = net->tz.hr*4;
554 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100555 ptr8[7] = bcdify(tzunits);
556 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100557 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200558 if (net->tz.dst >= 0 && net->tz.dst <= 2)
559 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100560 }
561 else {
562 /* Need to get GSM offset and convert into 15 min units */
563 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
Harald Welte9c3dc902012-04-08 16:59:24 +0200564#ifdef HAVE_TM_GMTOFF_IN_TM
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200565 local_time = localtime(&cur_t);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100566 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200567#else
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200568 /* find timezone offset */
569 time_t utc;
570 double offsetFromUTC;
571 utc = mktime(gmt_time);
572 local_time = localtime(&cur_t);
573 offsetFromUTC = difftime(cur_t, utc);
574 if (local_time->tm_isdst)
575 offsetFromUTC += 3600.0;
576 tzunits = ((int)offsetFromUTC) / 60 / 15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200577#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100578 if (tzunits < 0) {
579 tzunits = tzunits/-1;
580 ptr8[7] = bcdify(tzunits);
581 /* Flip it to negative */
582 ptr8[7] |= 0x08;
583 }
584 else
585 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100586
587 /* Does not support DST +2 */
588 if (local_time->tm_isdst)
589 dst = 1;
590 }
591
592 if (dst) {
593 ptr8 = msgb_put(msg, 3);
594 ptr8[0] = GSM48_IE_NET_DST;
595 ptr8[1] = 1;
596 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100597 }
Harald Weltedb253af2008-12-30 17:56:55 +0000598
Daniel Willmanneea93372009-08-13 03:42:07 +0200599 DEBUGP(DMM, "-> MM INFO\n");
600
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800601 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000602}
603
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100604/*! Send an Authentication Request to MS on the given subscriber connection
605 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
606 * \param[in] conn Subscriber connection to send on.
607 * \param[in] rand Random challenge token to send, must be 16 bytes long.
608 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
609 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
610 * \param[in] key_seq auth tuple's sequence number.
611 */
612int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
613 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200614{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100615 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200616 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200617 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200618
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100619 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100620 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100621 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200622
Harald Welte7984d5c2009-08-12 22:56:50 +0200623 gh->proto_discr = GSM48_PDISC_MM;
624 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
625
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100626 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200627
Harald Welte7984d5c2009-08-12 22:56:50 +0200628 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100629 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200630 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200631 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200632
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100633
634 /* 16 bytes AUTN */
635 if (autn)
636 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
637
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800638 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200639}
640
641/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800642int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200643{
644 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800645 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200646}
647
Harald Welte2483f1b2016-06-19 18:06:02 +0200648static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200649static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum gsm48_reject_value result);
Harald Welte2483f1b2016-06-19 18:06:02 +0200650
651static int cm_serv_reuse_conn(struct gsm_subscriber_connection *conn, const uint8_t *mi_lv)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100652{
Harald Welte2483f1b2016-06-19 18:06:02 +0200653 uint8_t mi_type;
654 char mi_string[GSM48_MI_SIZE];
655 uint32_t tmsi;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100656
Harald Welte2483f1b2016-06-19 18:06:02 +0200657 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
658 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100659
Harald Welte2483f1b2016-06-19 18:06:02 +0200660 switch (mi_type) {
661 case GSM_MI_TYPE_IMSI:
662 if (vlr_subscr_matches_imsi(conn->vsub, mi_string))
663 goto accept_reuse;
664 break;
665 case GSM_MI_TYPE_TMSI:
666 tmsi = osmo_load32be(mi_lv+2);
667 if (vlr_subscr_matches_tmsi(conn->vsub, tmsi))
668 goto accept_reuse;
669 break;
670 case GSM_MI_TYPE_IMEI:
671 if (vlr_subscr_matches_imei(conn->vsub, mi_string))
672 goto accept_reuse;
673 break;
674 default:
675 break;
676 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100677
Harald Welte2483f1b2016-06-19 18:06:02 +0200678 LOGP(DMM, LOGL_ERROR, "%s: CM Service Request with mismatching mobile identity: %s %s\n",
679 vlr_subscr_name(conn->vsub), gsm48_mi_type_name(mi_type), mi_string);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200680 msc_vlr_tx_cm_serv_rej(conn, GSM48_REJECT_ILLEGAL_MS);
Harald Welte2483f1b2016-06-19 18:06:02 +0200681 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100682
Harald Welte2483f1b2016-06-19 18:06:02 +0200683accept_reuse:
684 DEBUGP(DMM, "%s: re-using already accepted connection\n",
685 vlr_subscr_name(conn->vsub));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200686
687 if (!conn->received_cm_service_request) {
688 conn->received_cm_service_request = true;
689 msc_subscr_conn_get(conn, MSC_CONN_USE_CM_SERVICE);
690 }
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200691 msc_subscr_conn_update_id(conn, conn->complete_layer3_type, mi_string);
Harald Welte2483f1b2016-06-19 18:06:02 +0200692 return conn->network->vlr->ops.tx_cm_serv_acc(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100693}
694
Harald Welte4ed0e922009-01-10 03:17:30 +0000695/*
696 * Handle CM Service Requests
697 * a) Verify that the packet is long enough to contain the information
698 * we require otherwsie reject with INCORRECT_MESSAGE
699 * b) Try to parse the TMSI. If we do not have one reject
700 * c) Check that we know the subscriber with the TMSI otherwise reject
701 * with a HLR cause
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200702 * d) Set the subscriber on the conn and accept
Harald Welte2483f1b2016-06-19 18:06:02 +0200703 *
704 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000705 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200706int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000707{
Harald Welte2483f1b2016-06-19 18:06:02 +0200708 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200709 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200710 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000711
Harald Welteba4cf162009-01-10 01:49:35 +0000712 struct gsm48_hdr *gh = msgb_l3(msg);
713 struct gsm48_service_request *req =
714 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800715 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200716 uint8_t classmark2_len = gh->data[1];
717 uint8_t *classmark2 = gh->data+2;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200718 uint8_t *mi_p = classmark2 + classmark2_len;
719 uint8_t mi_len = *mi_p;
720 uint8_t *mi = mi_p + 1;
Harald Welte2483f1b2016-06-19 18:06:02 +0200721 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200722 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200723
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100724 lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200725 lai.lac = conn->lac;
Harald Welteba4cf162009-01-10 01:49:35 +0000726
727 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200728 LOGP(DMM, LOGL_ERROR, "<- CM SERVICE REQUEST wrong sized message\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200729 return msc_gsm48_tx_mm_serv_rej(conn,
730 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000731 }
732
733 if (msg->data_len < req->mi_len + 6) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200734 LOGP(DMM, LOGL_ERROR, "<- CM SERVICE REQUEST does not fit in packet\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200735 return msc_gsm48_tx_mm_serv_rej(conn,
736 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000737 }
738
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200739 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000740 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200741 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x MI(%s)=%s\n",
742 req->cm_service_type, gsm48_mi_type_name(mi_type), mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200743
Harald Welte3995d2e2018-01-24 11:23:54 +0100744 switch (mi_type) {
745 case GSM_MI_TYPE_IMSI:
746 case GSM_MI_TYPE_TMSI:
747 /* continue below */
748 break;
749 case GSM_MI_TYPE_IMEI:
750 if (req->cm_service_type == GSM48_CMSERV_EMERGENCY) {
751 /* We don't do emergency calls by IMEI */
752 LOGP(DMM, LOGL_NOTICE, "<- CM SERVICE REQUEST(IMEI=%s) rejected\n", mi_string);
753 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_IMEI_NOT_ACCEPTED);
754 }
755 /* fall-through for non-emergency setup */
756 default:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200757 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
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
Harald Welte37382ec2018-01-24 12:49:34 +0100762 switch (req->cm_service_type) {
763 case GSM48_CMSERV_MO_CALL_PACKET:
764 case GSM48_CMSERV_EMERGENCY:
765 case GSM48_CMSERV_SMS:
766 case GSM48_CMSERV_SUP_SERV:
767 /* continue below */
768 break;
769 default:
770 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
771 }
772
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200773 if (msc_subscr_conn_is_accepted(conn))
774 return cm_serv_reuse_conn(conn, mi_p);
Harald Welte2483f1b2016-06-19 18:06:02 +0200775
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200776 if (msc_subscr_conn_is_establishing_auth_ciph(conn)) {
777 LOGP(DMM, LOGL_ERROR,
778 "Cannot accept CM Service Request, conn already busy establishing authenticity\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200779 msc_vlr_tx_cm_serv_rej(conn, GSM48_REJECT_CONGESTION);
Harald Welte2483f1b2016-06-19 18:06:02 +0200780 return -EINVAL;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200781 /* or should we accept and note down the service request anyway? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200782 }
783
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200784 msc_subscr_conn_update_id(conn, COMPLETE_LAYER3_CM_SERVICE_REQ, mi_string);
Harald Welte7659de12009-12-13 12:39:18 +0100785
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200786 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, mi_p);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200787
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200788 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200789 vlr_proc_acc_req(conn->fi,
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200790 SUBSCR_CONN_E_ACCEPTED, SUBSCR_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +0200791 net->vlr, conn,
792 VLR_PR_ARQ_T_CM_SERV_REQ, mi-1, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200793 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100794 is_utran || conn->network->a5_encryption_mask > 0x01,
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200795 classmark2_is_r99(classmark2, classmark2_len),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200796 is_utran);
Holger Freythereb443982009-06-04 13:58:42 +0000797
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200798 /* From vlr_proc_acc_req() we expect an implicit dispatch of PR_ARQ_E_START we expect
799 * msc_vlr_subscr_assoc() to already have been called and completed. Has an error occured? */
800 if (!conn->vsub) {
801 LOGP(DRR, LOGL_ERROR, "%s: subscriber not allowed to do a CM Service Request\n",
802 mi_string);
803 return -EIO;
804 }
805
806 memcpy(conn->vsub->classmark.classmark2, classmark2, classmark2_len);
807 conn->vsub->classmark.classmark2_len = classmark2_len;
808
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200809 msc_subscr_conn_complete_layer_3(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +0200810 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000811}
812
Harald Welte49f8fcb2018-01-24 21:40:19 +0100813/* Receive a CM Re-establish Request */
814static int gsm48_rx_cm_reest_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
815{
816 uint8_t mi_type;
817 char mi_string[GSM48_MI_SIZE];
818 struct gsm48_hdr *gh = msgb_l3(msg);
819
820 uint8_t classmark2_len = gh->data[1];
821 uint8_t *classmark2 = gh->data+2;
822 uint8_t mi_len = *(classmark2 + classmark2_len);
823 uint8_t *mi = (classmark2 + classmark2_len + 1);
824
825 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
826 mi_type = mi[0] & GSM_MI_TYPE_MASK;
827 DEBUGP(DMM, "<- CM RE-ESTABLISH REQUEST MI(%s)=%s\n", gsm48_mi_type_name(mi_type), mi_string);
828
829 /* we don't support CM call re-establishment */
830 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
831}
832
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200833static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000834{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200835 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +0000836 struct gsm48_hdr *gh = msgb_l3(msg);
837 struct gsm48_imsi_detach_ind *idi =
838 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200839 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200840 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200841 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000842
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200843 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200844 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
845 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000846
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200847 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100848
Harald Welte2a139372009-02-22 21:14:55 +0000849 switch (mi_type) {
850 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200851 vsub = vlr_subscr_find_by_tmsi(network->vlr,
852 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000853 break;
854 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200855 vsub = vlr_subscr_find_by_imsi(network->vlr, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000856 break;
857 case GSM_MI_TYPE_IMEI:
858 case GSM_MI_TYPE_IMEISV:
859 /* no sim card... FIXME: what to do ? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200860 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
861 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000862 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200863 default:
Harald Welte2483f1b2016-06-19 18:06:02 +0200864 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
865 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000866 break;
867 }
868
Harald Welte2483f1b2016-06-19 18:06:02 +0200869 if (!vsub) {
870 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
871 gsm48_mi_type_name(mi_type), mi_string);
872 } else {
873 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
Neels Hofmeyrfacd57a2017-12-15 03:48:48 +0100874
875 if (vsub->cs.is_paging)
876 subscr_paging_cancel(vsub, GSM_PAGING_EXPIRED);
877
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +0200878 /* We already got Classmark 1 during Location Updating ... but well, ok */
879 vsub->classmark.classmark1 = idi->classmark1;
880
Harald Welte2483f1b2016-06-19 18:06:02 +0200881 vlr_subscr_rx_imsi_detach(vsub);
882 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
883 vlr_subscr_put(vsub);
884 }
Harald Welte2a139372009-02-22 21:14:55 +0000885
Harald Welte2483f1b2016-06-19 18:06:02 +0200886 msc_subscr_conn_close(conn, 0);
Harald Welte2a139372009-02-22 21:14:55 +0000887 return 0;
888}
889
Harald Welted2a7f5a2009-06-05 20:08:20 +0000890static int gsm48_rx_mm_status(struct msgb *msg)
891{
892 struct gsm48_hdr *gh = msgb_l3(msg);
893
894 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
895
896 return 0;
897}
898
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100899static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
900 struct gsm_subscriber_connection *conn,
901 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100902{
903 struct gsm48_hdr *gh = msgb_l3(msg);
904 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100905
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100906 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
907 LOGP(DMM, LOGL_ERROR,
908 "%s: MM AUTHENTICATION RESPONSE:"
909 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200910 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100911 return -EINVAL;
912 }
913
914 *res_len = sizeof(ar->sres);
915 memcpy(res, ar->sres, sizeof(ar->sres));
916 return 0;
917}
918
919static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
920 struct gsm_subscriber_connection *conn,
921 struct msgb *msg)
922{
923 struct gsm48_hdr *gh;
924 uint8_t *data;
925 uint8_t iei;
926 uint8_t ie_len;
927 unsigned int data_len;
928
929 /* First parse the GSM part */
930 if (parse_gsm_auth_resp(res, res_len, conn, msg))
931 return -EINVAL;
932 OSMO_ASSERT(*res_len == 4);
933
934 /* Then add the extended res part */
935 gh = msgb_l3(msg);
936 data = gh->data + sizeof(struct gsm48_auth_resp);
937 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
938
939 if (data_len < 3) {
940 LOGP(DMM, LOGL_ERROR,
941 "%s: MM AUTHENTICATION RESPONSE:"
942 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200943 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100944 return -EINVAL;
945 }
946
947 iei = data[0];
948 ie_len = data[1];
949 if (iei != GSM48_IE_AUTH_RES_EXT) {
950 LOGP(DMM, LOGL_ERROR,
951 "%s: MM R99 AUTHENTICATION RESPONSE:"
952 " expected IEI 0x%02x, got 0x%02x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200953 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100954 GSM48_IE_AUTH_RES_EXT, iei);
955 return -EINVAL;
956 }
957
958 if (ie_len > 12) {
959 LOGP(DMM, LOGL_ERROR,
960 "%s: MM R99 AUTHENTICATION RESPONSE:"
961 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200962 vlr_subscr_name(conn->vsub), GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100963 return -EINVAL;
964 }
965
966 *res_len += ie_len;
967 memcpy(res + 4, &data[2], ie_len);
968 return 0;
969}
970
971/* Chapter 9.2.3: Authentication Response */
972static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
973{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100974 uint8_t res[16];
975 uint8_t res_len;
976 int rc;
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100977 bool is_umts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100978
Harald Welte2483f1b2016-06-19 18:06:02 +0200979 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100980 LOGP(DMM, LOGL_ERROR,
981 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200982 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100983 return -EINVAL;
984 }
985
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100986 is_umts = (msgb_l3len(msg) > sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp));
987
988 if (is_umts)
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100989 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100990 else
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100991 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100992
993 if (rc) {
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +0100994 LOGP(DMM, LOGL_ERROR,
995 "%s: MM AUTHENTICATION RESPONSE: invalid: parsing %s AKA Auth Response"
996 " failed with rc=%d; dispatching zero length SRES/RES to trigger failure\n",
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100997 vlr_subscr_name(conn->vsub), is_umts ? "UMTS" : "GSM", rc);
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +0100998 memset(res, 0, sizeof(res));
999 res_len = 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001000 }
1001
1002 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001003 vlr_subscr_name(conn->vsub),
Neels Hofmeyrd97821f2018-03-10 04:51:39 +01001004 is_umts ? "UMTS" : "GSM", is_umts ? "res" : "sres",
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001005 osmo_hexdump_nospc(res, res_len));
1006
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001007 return vlr_subscr_rx_auth_resp(conn->vsub, classmark_is_r99(&conn->vsub->classmark),
Harald Welte2483f1b2016-06-19 18:06:02 +02001008 conn->via_ran == RAN_UTRAN_IU,
1009 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001010}
1011
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001012static int gsm48_rx_mm_auth_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
1013{
1014 struct gsm48_hdr *gh = msgb_l3(msg);
1015 uint8_t cause;
1016 uint8_t auts_tag;
1017 uint8_t auts_len;
1018 uint8_t *auts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001019
Harald Welte2483f1b2016-06-19 18:06:02 +02001020 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001021 LOGP(DMM, LOGL_ERROR,
1022 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02001023 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001024 return -EINVAL;
1025 }
1026
1027 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
1028 LOGP(DMM, LOGL_ERROR,
1029 "%s: MM R99 AUTHENTICATION FAILURE:"
1030 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001031 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
1032 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001033 return -EINVAL;
1034 }
1035
1036 cause = gh->data[0];
1037
1038 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
1039 LOGP(DMM, LOGL_INFO,
1040 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001041 vlr_subscr_name(conn->vsub), cause);
1042 vlr_subscr_rx_auth_fail(conn->vsub, NULL);
1043 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001044 }
1045
1046 /* This is a Synch Failure procedure, which should pass an AUTS to
1047 * resynchronize the sequence nr with the HLR. Expecting exactly one
1048 * TLV with 14 bytes of AUTS. */
1049
1050 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
1051 LOGP(DMM, LOGL_INFO,
1052 "%s: MM R99 AUTHENTICATION FAILURE:"
1053 " invalid Synch Failure: missing AUTS IE\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001054 vlr_subscr_name(conn->vsub));
1055 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001056 return -EINVAL;
1057 }
1058
1059 auts_tag = gh->data[1];
1060 auts_len = gh->data[2];
1061 auts = &gh->data[3];
1062
1063 if (auts_tag != GSM48_IE_AUTS
1064 || auts_len != 14) {
1065 LOGP(DMM, LOGL_INFO,
1066 "%s: MM R99 AUTHENTICATION FAILURE:"
1067 " invalid Synch Failure:"
1068 " expected AUTS IE 0x%02x of 14 bytes,"
1069 " got IE 0x%02x of %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001070 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001071 GSM48_IE_AUTS, auts_tag, auts_len);
Harald Welte2483f1b2016-06-19 18:06:02 +02001072 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001073 return -EINVAL;
1074 }
1075
1076 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
1077 LOGP(DMM, LOGL_INFO,
1078 "%s: MM R99 AUTHENTICATION FAILURE:"
1079 " invalid Synch Failure msg: message truncated (%u)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001080 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
1081 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001082 return -EINVAL;
1083 }
1084
1085 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
1086 * large enough. */
1087
1088 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001089 vlr_subscr_name(conn->vsub), osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001090
Harald Welte2483f1b2016-06-19 18:06:02 +02001091 return vlr_subscr_rx_auth_fail(conn->vsub, auts);
1092}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001093
Harald Welte2483f1b2016-06-19 18:06:02 +02001094static int gsm48_rx_mm_tmsi_reall_compl(struct gsm_subscriber_connection *conn)
1095{
1096 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1097 vlr_subscr_name(conn->vsub));
1098 if (!conn->vsub) {
1099 LOGP(DMM, LOGL_ERROR,
1100 "Rx MM TMSI Reallocation Complete: invalid: no subscriber\n");
1101 return -EINVAL;
1102 }
1103 return vlr_subscr_rx_tmsi_reall_compl(conn->vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001104}
1105
Harald Weltebf5e8df2009-02-03 12:59:45 +00001106/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001107static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001108{
1109 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001110 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001111
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001112 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001113 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001114 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001115 break;
1116 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001117 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001118 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001119 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001120 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001121 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001122 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001123 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001124 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001125 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte2483f1b2016-06-19 18:06:02 +02001126 rc = gsm48_rx_mm_tmsi_reall_compl(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001127 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001128 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001129 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001130 break;
1131 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01001132 rc = gsm48_rx_cm_reest_req(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001133 break;
1134 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001135 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001136 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001137 case GSM48_MT_MM_AUTH_FAIL:
1138 rc = gsm48_rx_mm_auth_fail(conn, msg);
1139 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001140 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001141 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001142 gh->msg_type);
1143 break;
1144 }
1145
1146 return rc;
1147}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001148
Harald Welte2d35ae62009-02-06 12:02:13 +00001149/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001150static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001151{
Harald Welte2483f1b2016-06-19 18:06:02 +02001152 struct gsm_network *net = conn->network;
Harald Welte2d35ae62009-02-06 12:02:13 +00001153 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001154 struct gsm48_pag_resp *resp;
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001155 uint8_t classmark2_len = gh->data[1];
1156 uint8_t *classmark2 = gh->data+2;
1157 uint8_t *mi_lv = classmark2 + classmark2_len;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001158 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001159 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +02001160 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001161 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +02001162
Neels Hofmeyr379d5792018-02-22 04:04:54 +01001163 lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001164 lai.lac = conn->lac;
Harald Welte2d35ae62009-02-06 12:02:13 +00001165
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001166 resp = (struct gsm48_pag_resp *) &gh->data[0];
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001167
1168 if (gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh), mi_string, &mi_type) <= 0) {
1169 LOGP(DRR, LOGL_ERROR, "PAGING RESPONSE: invalid Mobile Identity\n");
1170 return -EINVAL;
1171 }
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001172
1173 if (msc_subscr_conn_is_establishing_auth_ciph(conn)) {
1174 LOGP(DMM, LOGL_ERROR,
1175 "Ignoring Paging Response, conn already busy establishing authenticity\n");
1176 return 0;
1177 }
1178
1179 if (msc_subscr_conn_is_accepted(conn)) {
1180 LOGP(DMM, LOGL_ERROR, "Ignoring Paging Response, conn already established\n");
1181 return 0;
1182 }
1183
1184 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n", gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001185
Neels Hofmeyr16c42b52018-04-02 12:26:16 +02001186 msc_subscr_conn_update_id(conn, COMPLETE_LAYER3_PAGING_RESP, mi_string);
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001187
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001188 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +02001189 vlr_proc_acc_req(conn->fi,
Neels Hofmeyr16c42b52018-04-02 12:26:16 +02001190 SUBSCR_CONN_E_ACCEPTED, SUBSCR_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +02001191 net->vlr, conn,
1192 VLR_PR_ARQ_T_PAGING_RESP, mi_lv, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001193 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +01001194 is_utran || conn->network->a5_encryption_mask > 0x01,
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001195 classmark2_is_r99(classmark2, classmark2_len),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001196 is_utran);
Holger Freyther053e09d2009-02-14 22:51:06 +00001197
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001198 /* From vlr_proc_acc_req() we expect an implicit dispatch of PR_ARQ_E_START we expect
1199 * msc_vlr_subscr_assoc() to already have been called and completed. Has an error occured? */
1200 if (!conn->vsub) {
1201 LOGP(DRR, LOGL_ERROR, "%s: subscriber not allowed to do a Paging Response\n",
1202 mi_string);
1203 return -EIO;
1204 }
1205
1206 memcpy(conn->vsub->classmark.classmark2, classmark2, classmark2_len);
1207 conn->vsub->classmark.classmark2_len = classmark2_len;
1208
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001209 msc_subscr_conn_complete_layer_3(conn);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001210 return 0;
Harald Welte2d35ae62009-02-06 12:02:13 +00001211}
1212
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001213static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001214{
1215 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001216 uint8_t apdu_id_flags;
1217 uint8_t apdu_len;
1218 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001219
1220 apdu_id_flags = gh->data[0];
1221 apdu_len = gh->data[1];
1222 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001223
Max8dc8f232017-02-09 19:13:02 +01001224 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001225 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001226
Harald Welte2483f1b2016-06-19 18:06:02 +02001227 /* we're not using the app info blob anywhere, so ignore. */
1228#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001229 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte2483f1b2016-06-19 18:06:02 +02001230#else
1231 return 0;
1232#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001233}
1234
Harald Weltebf5e8df2009-02-03 12:59:45 +00001235/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001236static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001237{
1238 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001239 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001240
1241 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001242 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001243 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001244 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001245 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001246 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001247 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001248 default:
Philippd37981e2016-10-10 15:11:41 +02001249 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1250 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001251 break;
1252 }
1253
Harald Welte2d35ae62009-02-06 12:02:13 +00001254 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001255}
1256
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001257int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1258 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001259{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001260 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001261 struct gsm48_hdr *gh;
1262
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001263 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1264 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001265
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001266 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1267 gh->proto_discr = GSM48_PDISC_RR;
1268 gh->msg_type = GSM48_MT_RR_APP_INFO;
1269 gh->data[0] = apdu_id;
1270 gh->data[1] = apdu_len;
1271 memcpy(gh->data+2, apdu, apdu_len);
1272
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001273 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001274}
1275
Harald Welte2483f1b2016-06-19 18:06:02 +02001276static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08001277{
Harald Welte2483f1b2016-06-19 18:06:02 +02001278 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
1279 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08001280
Harald Welte2483f1b2016-06-19 18:06:02 +02001281 switch (pdisc) {
1282 case GSM48_PDISC_MM:
1283 switch (msg_type) {
1284 case GSM48_MT_MM_LOC_UPD_REQUEST:
1285 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01001286 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte2483f1b2016-06-19 18:06:02 +02001287 case GSM48_MT_MM_AUTH_RESP:
1288 case GSM48_MT_MM_AUTH_FAIL:
1289 case GSM48_MT_MM_ID_RESP:
1290 case GSM48_MT_MM_TMSI_REALL_COMPL:
1291 case GSM48_MT_MM_IMSI_DETACH_IND:
1292 return true;
1293 default:
1294 break;
1295 }
1296 break;
1297 case GSM48_PDISC_RR:
1298 switch (msg_type) {
1299 case GSM48_MT_RR_CIPH_M_COMPL:
1300 case GSM48_MT_RR_PAG_RESP:
1301 return true;
1302 default:
1303 break;
1304 }
1305 break;
1306 default:
1307 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02001308 }
1309
Harald Welte2483f1b2016-06-19 18:06:02 +02001310 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02001311}
1312
Harald Welte2483f1b2016-06-19 18:06:02 +02001313void cm_service_request_concludes(struct gsm_subscriber_connection *conn,
1314 struct msgb *msg)
1315{
1316
1317 /* If a CM Service Request was received before, this is the request the
1318 * conn was opened for. No need to wait for further messages. */
1319
1320 if (!conn->received_cm_service_request)
1321 return;
1322
1323 if (log_check_level(DMM, LOGL_DEBUG)) {
1324 struct gsm48_hdr *gh = msgb_l3(msg);
1325 uint8_t pdisc = gsm48_hdr_pdisc(gh);
1326 uint8_t msg_type = gsm48_hdr_msg_type(gh);
1327
Neels Hofmeyr78ada642017-03-10 02:15:20 +01001328 DEBUGP(DMM, "%s: rx msg %s:"
Harald Welte2483f1b2016-06-19 18:06:02 +02001329 " received_cm_service_request changes to false\n",
1330 vlr_subscr_name(conn->vsub),
Neels Hofmeyr78ada642017-03-10 02:15:20 +01001331 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Harald Welte2483f1b2016-06-19 18:06:02 +02001332 }
1333 conn->received_cm_service_request = false;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001334 msc_subscr_conn_put(conn, MSC_CONN_USE_CM_SERVICE);
Harald Welte2483f1b2016-06-19 18:06:02 +02001335}
1336
Harald Weltec2007852018-02-03 21:08:26 +01001337/* TS 24.007 11.2.3.2.3 Message Type Octet / Duplicate Detection */
1338int gsm0407_pdisc_ctr_bin(uint8_t pdisc)
1339{
1340 switch (pdisc) {
1341 case GSM48_PDISC_MM:
1342 case GSM48_PDISC_CC:
1343 case GSM48_PDISC_NC_SS:
1344 return 0;
1345 case GSM48_PDISC_GROUP_CC:
1346 return 1;
1347 case GSM48_PDISC_BCAST_CC:
1348 return 2;
1349 case GSM48_PDISC_LOC:
1350 return 3;
1351 default:
1352 return -1;
1353 }
1354}
1355
1356/* extract the N(SD) and return the modulo value for a R98 message */
1357static uint8_t gsm0407_determine_nsd_ret_modulo_r99(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
1358{
1359 switch (pdisc) {
1360 case GSM48_PDISC_MM:
1361 case GSM48_PDISC_CC:
1362 case GSM48_PDISC_NC_SS:
1363 *n_sd = (msg_type >> 6) & 0x3;
1364 return 4;
1365 case GSM48_PDISC_GROUP_CC:
1366 case GSM48_PDISC_BCAST_CC:
1367 case GSM48_PDISC_LOC:
1368 *n_sd = (msg_type >> 6) & 0x1;
1369 return 2;
1370 default:
1371 /* no sequence number, we cannot detect dups */
1372 return 0;
1373 }
1374}
1375
1376/* extract the N(SD) and return the modulo value for a R99 message */
1377static uint8_t gsm0407_determine_nsd_ret_modulo_r98(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
1378{
1379 switch (pdisc) {
1380 case GSM48_PDISC_MM:
1381 case GSM48_PDISC_CC:
1382 case GSM48_PDISC_NC_SS:
1383 case GSM48_PDISC_GROUP_CC:
1384 case GSM48_PDISC_BCAST_CC:
1385 case GSM48_PDISC_LOC:
1386 *n_sd = (msg_type >> 6) & 0x1;
1387 return 2;
1388 default:
1389 /* no sequence number, we cannot detect dups */
1390 return 0;
1391 }
1392}
1393
1394/* TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection */
1395static bool gsm0407_is_duplicate(struct gsm_subscriber_connection *conn, struct msgb *msg)
1396{
1397 struct gsm48_hdr *gh;
1398 uint8_t pdisc;
1399 uint8_t n_sd, modulo, bin;
1400
1401 gh = msgb_l3(msg);
1402 pdisc = gsm48_hdr_pdisc(gh);
1403
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001404 if (conn->vsub && classmark_is_r99(&conn->vsub->classmark)) {
Harald Weltec2007852018-02-03 21:08:26 +01001405 modulo = gsm0407_determine_nsd_ret_modulo_r99(pdisc, gh->msg_type, &n_sd);
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001406 } else { /* pre R99 */
1407 modulo = gsm0407_determine_nsd_ret_modulo_r98(pdisc, gh->msg_type, &n_sd);
Harald Weltec2007852018-02-03 21:08:26 +01001408 }
1409 if (modulo == 0)
1410 return false;
1411 bin = gsm0407_pdisc_ctr_bin(pdisc);
1412 if (bin < 0)
1413 return false;
1414
1415 OSMO_ASSERT(bin < ARRAY_SIZE(conn->n_sd_next));
1416 if (n_sd != conn->n_sd_next[bin]) {
1417 /* not what we expected: duplicate */
1418 return true;
1419 } else {
1420 /* as expected: no dup; update expected counter for next message */
1421 conn->n_sd_next[bin] = (n_sd + 1) % modulo;
1422 return false;
1423 }
1424}
Harald Welte2483f1b2016-06-19 18:06:02 +02001425
Harald Welte27989d42018-06-21 20:39:20 +02001426extern int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg);
1427
Neels Hofmeyr378a4922016-05-09 21:07:43 +02001428/* 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 +08001429int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001430{
Philipp Maier91f10c72017-04-20 18:40:37 +02001431 struct gsm48_hdr *gh;
1432 uint8_t pdisc;
Harald Welte8470bf22008-12-25 23:28:35 +00001433 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01001434
Neels Hofmeyr0906a392017-09-19 14:36:06 +02001435 OSMO_ASSERT(msg->l3h);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02001436 OSMO_ASSERT(conn);
1437 OSMO_ASSERT(msg);
1438
Philipp Maier91f10c72017-04-20 18:40:37 +02001439 gh = msgb_l3(msg);
1440 pdisc = gsm48_hdr_pdisc(gh);
1441
Harald Weltec2007852018-02-03 21:08:26 +01001442 if (gsm0407_is_duplicate(conn, msg)) {
1443 LOGP(DRLL, LOGL_NOTICE, "%s: Discarding duplicate L3 message\n",
1444 (conn && conn->vsub) ? vlr_subscr_name(conn->vsub) : "UNKNOWN");
1445 return 0;
1446 }
1447
Neels Hofmeyr78ada642017-03-10 02:15:20 +01001448 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message %s (0x%x:0x%x)\n",
1449 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)),
1450 pdisc, gsm48_hdr_msg_type(gh));
Harald Welte2483f1b2016-06-19 18:06:02 +02001451
1452 if (!msc_subscr_conn_is_accepted(conn)
1453 && !msg_is_initially_permitted(gh)) {
1454 LOGP(DRLL, LOGL_ERROR,
Neels Hofmeyr78ada642017-03-10 02:15:20 +01001455 "subscr %s: Message not permitted for initial conn: %s\n",
1456 vlr_subscr_name(conn->vsub),
1457 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
Harald Welte2483f1b2016-06-19 18:06:02 +02001458 return -EACCES;
1459 }
1460
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001461 if (conn->vsub && conn->vsub->cs.attached_via_ran != conn->via_ran) {
1462 LOGP(DMM, LOGL_ERROR,
1463 "%s: Illegal situation: RAN type mismatch:"
1464 " attached via %s, received message via %s\n",
1465 vlr_subscr_name(conn->vsub),
1466 ran_type_name(conn->vsub->cs.attached_via_ran),
1467 ran_type_name(conn->via_ran));
1468 return -EACCES;
1469 }
1470
Philipp Maiere0d5caa2017-02-27 16:56:59 +01001471#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08001472 if (silent_call_reroute(conn, msg))
1473 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01001474#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001475
Harald Welte52b1f982008-12-23 20:25:15 +00001476 switch (pdisc) {
1477 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001478 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001479 break;
1480 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001481 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001482 break;
1483 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001484 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001485 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00001486 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08001487 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00001488 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001489 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00001490 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01001491 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
1492 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02001493 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00001494 break;
Harald Welte6eafe912009-10-16 08:32:58 +02001495 case GSM48_PDISC_NC_SS:
Vadim Yanitskiy5b860fa2018-06-12 05:24:52 +07001496 rc = gsm0911_rcv_nc_ss(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02001497 break;
Harald Welteeac38c32017-05-29 18:02:53 +02001498 case GSM48_PDISC_TEST:
1499 rc = gsm0414_rcv_test(conn, msg);
1500 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001501 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001502 LOGP(DRLL, LOGL_NOTICE, "Unknown "
1503 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02001504 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00001505 break;
1506 }
1507
1508 return rc;
1509}
Harald Welte8470bf22008-12-25 23:28:35 +00001510
Harald Welte2483f1b2016-06-19 18:06:02 +02001511/***********************************************************************
1512 * VLR integration
1513 ***********************************************************************/
1514
1515/* VLR asks us to send an authentication request */
1516static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
1517 bool send_autn)
1518{
1519 struct gsm_subscriber_connection *conn = msc_conn_ref;
1520 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
1521 send_autn? at->vec.autn : NULL,
1522 at->key_seq);
1523}
1524
1525/* VLR asks us to send an authentication reject */
1526static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
1527{
1528 struct gsm_subscriber_connection *conn = msc_conn_ref;
1529 return gsm48_tx_mm_auth_rej(conn);
1530}
1531
1532/* VLR asks us to transmit an Identity Request of given type */
1533static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
1534{
1535 struct gsm_subscriber_connection *conn = msc_conn_ref;
1536 return mm_tx_identity_req(conn, mi_type);
1537}
1538
1539/* VLR asks us to transmit a Location Update Accept */
1540static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
1541{
1542 struct gsm_subscriber_connection *conn = msc_conn_ref;
1543 return gsm0408_loc_upd_acc(conn, send_tmsi);
1544}
1545
1546/* VLR asks us to transmit a Location Update Reject */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001547static int msc_vlr_tx_lu_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Harald Welte2483f1b2016-06-19 18:06:02 +02001548{
1549 struct gsm_subscriber_connection *conn = msc_conn_ref;
1550 return gsm0408_loc_upd_rej(conn, cause);
1551}
1552
1553/* VLR asks us to transmit a CM Service Accept */
1554static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
1555{
1556 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001557 return msc_gsm48_tx_mm_serv_ack(conn);
1558}
1559
1560static int msc_vlr_tx_common_id(void *msc_conn_ref)
1561{
1562 struct gsm_subscriber_connection *conn = msc_conn_ref;
1563 return msc_tx_common_id(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +02001564}
1565
Stefan Sperling3a741282018-03-13 21:11:49 +01001566/* VLR asks us to transmit MM info. */
1567static int msc_vlr_tx_mm_info(void *msc_conn_ref)
1568{
1569 struct gsm_subscriber_connection *conn = msc_conn_ref;
1570 if (!conn->network->send_mm_info)
1571 return 0;
1572 return gsm48_tx_mm_info(conn);
1573}
1574
Harald Welte2483f1b2016-06-19 18:06:02 +02001575/* VLR asks us to transmit a CM Service Reject */
Neels Hofmeyr15809592018-04-06 02:57:51 +02001576static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Harald Welte2483f1b2016-06-19 18:06:02 +02001577{
Harald Welte2483f1b2016-06-19 18:06:02 +02001578 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001579 int rc;
Harald Welte2483f1b2016-06-19 18:06:02 +02001580
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001581 rc = msc_gsm48_tx_mm_serv_rej(conn, cause);
1582
1583 if (conn->received_cm_service_request) {
1584 conn->received_cm_service_request = false;
1585 msc_subscr_conn_put(conn, MSC_CONN_USE_CM_SERVICE);
1586 }
1587
1588 return rc;
Harald Welte2483f1b2016-06-19 18:06:02 +02001589}
1590
Neels Hofmeyr703638e2017-12-14 05:30:16 +01001591/* For msc_vlr_set_ciph_mode() */
1592osmo_static_assert(sizeof(((struct gsm0808_encrypt_info*)0)->key) >= sizeof(((struct osmo_auth_vector*)0)->kc),
1593 gsm0808_encrypt_info_key_fits_osmo_auth_vec_kc);
1594
Neels Hofmeyrda21a522018-03-02 01:50:09 +01001595/* VLR asks us to start using ciphering.
1596 * (Keep non-static to allow regression testing on this function.) */
1597int msc_vlr_set_ciph_mode(void *msc_conn_ref,
1598 bool umts_aka,
1599 bool retrieve_imeisv)
Harald Welte2483f1b2016-06-19 18:06:02 +02001600{
1601 struct gsm_subscriber_connection *conn = msc_conn_ref;
1602 struct vlr_subscr *vsub;
1603 struct gsm_auth_tuple *tuple;
1604
1605 if (!conn || !conn->vsub) {
1606 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
1607 " NULL conn/subscriber");
1608 return -EINVAL;
1609 }
1610
1611 vsub = conn->vsub;
1612 tuple = vsub->last_tuple;
1613
1614 if (!tuple) {
1615 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
1616 " Command: no auth tuple available\n",
1617 vlr_subscr_name(vsub));
1618 return -EINVAL;
1619 }
1620
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001621 switch (conn->via_ran) {
1622 case RAN_GERAN_A:
1623 DEBUGP(DMM, "-> CIPHER MODE COMMAND %s\n",
1624 vlr_subscr_name(conn->vsub));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01001625 {
Harald Welte7b222aa2017-12-23 19:30:32 +01001626 struct gsm_network *net = conn->network;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01001627 struct gsm0808_encrypt_info ei;
Harald Welte7b222aa2017-12-23 19:30:32 +01001628 int i, j = 0;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01001629
Harald Welte7b222aa2017-12-23 19:30:32 +01001630 for (i = 0; i < 8; i++) {
Harald Welte71330722017-12-23 19:59:02 +01001631 if (net->a5_encryption_mask & (1 << i) &&
Neels Hofmeyr986fe7e2018-09-13 03:05:52 +02001632 classmark_supports_a5(&conn->vsub->classmark, i))
Harald Welte7b222aa2017-12-23 19:30:32 +01001633 ei.perm_algo[j++] = vlr_ciph_to_gsm0808_alg_id(i);
1634 }
1635 ei.perm_algo_len = j;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01001636
Harald Welte71330722017-12-23 19:59:02 +01001637 if (ei.perm_algo_len == 0) {
1638 LOGP(DMM, LOGL_ERROR, "%s: cannot start ciphering, no intersection "
1639 "between MSC-configured and MS-supported A5 algorithms\n",
1640 vlr_subscr_name(conn->vsub));
1641 return -ENOTSUP;
1642 }
1643
Neels Hofmeyr52821712017-12-18 01:23:42 +01001644 /* In case of UMTS AKA, the Kc for ciphering must be derived from the 3G auth
1645 * tokens. tuple->vec.kc was calculated from the GSM algorithm and is not
1646 * necessarily a match for the UMTS AKA tokens. */
1647 if (umts_aka)
1648 osmo_auth_c3(ei.key, tuple->vec.ck, tuple->vec.ik);
1649 else
1650 memcpy(ei.key, tuple->vec.kc, sizeof(tuple->vec.kc));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01001651 ei.key_len = sizeof(tuple->vec.kc);
1652
1653 return a_iface_tx_cipher_mode(conn, &ei, retrieve_imeisv);
1654 }
1655
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001656 case RAN_UTRAN_IU:
1657#ifdef BUILD_IU
1658 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n",
1659 vlr_subscr_name(conn->vsub));
Neels Hofmeyr00e82d62017-07-05 15:19:52 +02001660 return ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &tuple->vec, 0, 1);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001661#else
1662 LOGP(DMM, LOGL_ERROR, "Cannot send Security Mode Control over RAN_UTRAN_IU,"
1663 " built without Iu support\n");
1664 return -ENOTSUP;
1665#endif
1666
1667 default:
1668 break;
1669 }
1670 LOGP(DMM, LOGL_ERROR,
1671 "%s: cannot start ciphering, unknown RAN type %d\n",
1672 vlr_subscr_name(conn->vsub), conn->via_ran);
1673 return -ENOTSUP;
1674}
1675
1676void msc_rx_sec_mode_compl(struct gsm_subscriber_connection *conn)
1677{
1678 struct vlr_ciph_result vlr_res = {};
1679
1680 if (!conn || !conn->vsub) {
1681 LOGP(DMM, LOGL_ERROR,
1682 "Rx Security Mode Complete for invalid conn\n");
1683 return;
1684 }
1685
1686 DEBUGP(DMM, "<- SECURITY MODE COMPLETE %s\n",
1687 vlr_subscr_name(conn->vsub));
1688
1689 vlr_res.cause = VLR_CIPH_COMPL;
1690 vlr_subscr_rx_ciph_res(conn->vsub, &vlr_res);
Harald Welte2483f1b2016-06-19 18:06:02 +02001691}
1692
1693/* VLR informs us that the subscriber data has somehow been modified */
1694static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
1695{
Maxa263bb22017-12-27 13:23:44 +01001696 LOGVSUBP(LOGL_NOTICE, subscr, "VLR: update for IMSI=%s (MSISDN=%s, used=%d)\n",
1697 subscr->imsi, subscr->msisdn, subscr->use_count);
Harald Welte2483f1b2016-06-19 18:06:02 +02001698}
1699
1700/* VLR informs us that the subscriber has been associated with a conn */
1701static void msc_vlr_subscr_assoc(void *msc_conn_ref,
1702 struct vlr_subscr *vsub)
1703{
1704 struct gsm_subscriber_connection *conn = msc_conn_ref;
Max48d4ec02018-02-12 16:51:03 +01001705 OSMO_ASSERT(vsub);
Harald Welte2483f1b2016-06-19 18:06:02 +02001706 OSMO_ASSERT(!conn->vsub);
1707 conn->vsub = vlr_subscr_get(vsub);
Max48d4ec02018-02-12 16:51:03 +01001708 OSMO_ASSERT(conn->vsub);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001709 conn->vsub->cs.attached_via_ran = conn->via_ran;
Harald Welte2483f1b2016-06-19 18:06:02 +02001710}
1711
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001712static int msc_vlr_route_gsup_msg(struct vlr_subscr *vsub,
1713 struct osmo_gsup_message *gsup_msg)
1714{
1715 switch (gsup_msg->message_type) {
Vadim Yanitskiy8a6ef552018-06-12 08:21:20 +07001716 /* GSM 09.11 code implementing SS/USSD */
1717 case OSMO_GSUP_MSGT_PROC_SS_REQUEST:
1718 case OSMO_GSUP_MSGT_PROC_SS_RESULT:
1719 case OSMO_GSUP_MSGT_PROC_SS_ERROR:
1720 DEBUGP(DMSC, "Routed to GSM 09.11 SS/USSD handler\n");
1721 return gsm0911_gsup_handler(vsub, gsup_msg);
1722
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001723 default:
1724 LOGP(DMM, LOGL_ERROR, "No handler found for %s, dropping message...\n",
1725 osmo_gsup_message_type_name(gsup_msg->message_type));
1726 return -GMM_CAUSE_MSGT_NOTEXIST_NOTIMPL;
1727 }
1728}
1729
Harald Welte2483f1b2016-06-19 18:06:02 +02001730/* operations that we need to implement for libvlr */
1731static const struct vlr_ops msc_vlr_ops = {
1732 .tx_auth_req = msc_vlr_tx_auth_req,
1733 .tx_auth_rej = msc_vlr_tx_auth_rej,
1734 .tx_id_req = msc_vlr_tx_id_req,
1735 .tx_lu_acc = msc_vlr_tx_lu_acc,
1736 .tx_lu_rej = msc_vlr_tx_lu_rej,
1737 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
1738 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
1739 .set_ciph_mode = msc_vlr_set_ciph_mode,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001740 .tx_common_id = msc_vlr_tx_common_id,
Stefan Sperling3a741282018-03-13 21:11:49 +01001741 .tx_mm_info = msc_vlr_tx_mm_info,
Harald Welte2483f1b2016-06-19 18:06:02 +02001742 .subscr_update = msc_vlr_subscr_update,
1743 .subscr_assoc = msc_vlr_subscr_assoc,
Vadim Yanitskiy8a0e2582018-06-14 03:54:33 +07001744 .forward_gsup_msg = msc_vlr_route_gsup_msg,
Harald Welte2483f1b2016-06-19 18:06:02 +02001745};
1746
1747/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
1748int msc_vlr_alloc(struct gsm_network *net)
1749{
1750 net->vlr = vlr_alloc(net, &msc_vlr_ops);
1751 if (!net->vlr)
1752 return -ENOMEM;
1753 net->vlr->user_ctx = net;
1754 return 0;
1755}
1756
1757/* Launch the VLR, i.e. its GSUP connection */
1758int msc_vlr_start(struct gsm_network *net)
1759{
1760 OSMO_ASSERT(net->vlr);
1761 return vlr_start("MSC", net->vlr, net->gsup_server_addr_str,
1762 net->gsup_server_port);
1763}
Neels Hofmeyr79e580b2018-03-22 16:48:22 +01001764
1765struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value)
1766{
1767 struct msgb *msg;
1768 struct gsm48_hdr *gh;
1769
1770 msg = gsm48_msgb_alloc_name("GSM 04.08 SERV REJ");
1771 if (!msg)
1772 return NULL;
1773
1774 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1775 gh->proto_discr = GSM48_PDISC_MM;
1776 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
1777 gh->data[0] = value;
1778
1779 return msg;
1780}
1781
1782struct msgb *gsm48_create_loc_upd_rej(uint8_t cause)
1783{
1784 struct gsm48_hdr *gh;
1785 struct msgb *msg;
1786
1787 msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD REJ");
1788 if (!msg)
1789 return NULL;
1790
1791 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
1792 gh->proto_discr = GSM48_PDISC_MM;
1793 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
1794 gh->data[0] = cause;
1795 return msg;
1796}
1797
1798int gsm48_extract_mi(uint8_t *classmark2_lv, int length, char *mi_string, uint8_t *mi_type)
1799{
1800 /* Check the size for the classmark */
1801 if (length < 1 + *classmark2_lv)
1802 return -1;
1803
1804 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
1805 if (length < 2 + *classmark2_lv + mi_lv[0])
1806 return -2;
1807
1808 *mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
1809 return gsm48_mi_to_string(mi_string, GSM48_MI_SIZE, mi_lv+1, *mi_lv);
1810}
1811
1812int gsm48_paging_extract_mi(struct gsm48_pag_resp *resp, int length,
1813 char *mi_string, uint8_t *mi_type)
1814{
1815 static const uint32_t classmark_offset =
1816 offsetof(struct gsm48_pag_resp, classmark2);
1817 uint8_t *classmark2_lv = (uint8_t *) &resp->classmark2;
1818 return gsm48_extract_mi(classmark2_lv, length - classmark_offset,
1819 mi_string, mi_type);
1820}