blob: 53e0989c551f7fca966037d69d221dc0366bde70 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gsm48.c
2 * GSM Mobile Radio Interface Layer 3 messages
Harald Welte4fb20752010-03-02 23:17:33 +01003 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02004/*
5 * (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte4fb20752010-03-02 23:17:33 +01006 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
7 *
8 * All Rights Reserved
9 *
Harald Weltee08da972017-11-13 01:00:26 +090010 * SPDX-License-Identifier: GPL-2.0+
11 *
Harald Welte4fb20752010-03-02 23:17:33 +010012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
Harald Welte4fb20752010-03-02 23:17:33 +010022 */
23
24#include <stdint.h>
25#include <stdio.h>
26#include <string.h>
Maxfe65fa72016-05-10 17:17:05 +020027#include <stdbool.h>
Pau Espin Pedrol45735022017-06-18 14:05:24 +020028#include <inttypes.h>
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010029#include <ctype.h>
Harald Welte61e2bfc2010-03-04 10:53:03 +010030
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010031#include <osmocom/core/utils.h>
Harald Welte95871da2017-05-15 12:11:36 +020032#include <osmocom/core/byteswap.h>
33#include <osmocom/core/bit16gen.h>
34#include <osmocom/core/bit32gen.h>
Harald Welte179f3572019-03-18 18:38:47 +010035#include <osmocom/core/talloc.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010036#include <osmocom/gsm/tlv.h>
37#include <osmocom/gsm/gsm48.h>
Harald Welte94df39e2011-06-26 14:33:57 +020038#include <osmocom/gsm/gsm0502.h>
Maxfb348ee2016-03-30 21:14:53 +020039#include <osmocom/gsm/gsm_utils.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010040#include <osmocom/gsm/protocol/gsm_04_08.h>
Vadim Yanitskiy8d41d722018-04-17 11:17:28 +070041#include <osmocom/gsm/protocol/gsm_04_80.h>
Harald Welte2aee7b12011-06-26 14:20:04 +020042#include <osmocom/gsm/protocol/gsm_08_58.h>
Maxfdca25d2016-07-05 16:06:28 +020043#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
Neels Hofmeyr83025bf2020-05-26 02:45:23 +020044#include <osmocom/gsm/protocol/gsm_23_003.h>
Harald Welte4fb20752010-03-02 23:17:33 +010045
Harald Welte96e2a002017-06-12 21:44:18 +020046/*! \addtogroup gsm0408
47 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020048 * GSM Mobile Radion Interface L3 messages / TS 04.08
Harald Welte96e2a002017-06-12 21:44:18 +020049 */
50
Neels Hofmeyr87e45502017-06-20 00:17:59 +020051/*! TLV parser definitions for TS 04.08 CC */
Harald Welte4fb20752010-03-02 23:17:33 +010052const struct tlv_definition gsm48_att_tlvdef = {
53 .def = {
54 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
55 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
56 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
57 [GSM48_IE_UTC] = { TLV_TYPE_TV },
58 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
59 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
60
61 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
62 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
63 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
64 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
65 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
66 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
67 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
68 [GSM48_IE_NOTIFY] = { TLV_TYPE_TV },
69 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
70 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
71 [GSM48_IE_CONN_BCD] = { TLV_TYPE_TLV },
72 [GSM48_IE_CONN_SUB] = { TLV_TYPE_TLV },
73 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
74 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
75 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
76 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
77 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
78 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
79 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
80 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
81 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
82 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
83 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
84 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
85 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
86 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
87 [GSM48_IE_REPEAT_CIR] = { TLV_TYPE_T },
88 [GSM48_IE_REPEAT_SEQ] = { TLV_TYPE_T },
89 /* FIXME: more elements */
90 },
91};
92
Neels Hofmeyr87e45502017-06-20 00:17:59 +020093/*! TLV parser definitions for TS 04.08 RR */
Andreas Eversberg014cb872010-07-12 09:11:00 +020094const struct tlv_definition gsm48_rr_att_tlvdef = {
95 .def = {
96 /* NOTE: Don't add IE 17 = MOBILE_ID here, it already used. */
97 [GSM48_IE_VGCS_TARGET] = { TLV_TYPE_TLV },
98 [GSM48_IE_FRQSHORT_AFTER] = { TLV_TYPE_FIXED, 9 },
99 [GSM48_IE_MUL_RATE_CFG] = { TLV_TYPE_TLV },
100 [GSM48_IE_FREQ_L_AFTER] = { TLV_TYPE_TLV },
101 [GSM48_IE_MSLOT_DESC] = { TLV_TYPE_TLV },
102 [GSM48_IE_CHANMODE_2] = { TLV_TYPE_TV },
103 [GSM48_IE_FRQSHORT_BEFORE] = { TLV_TYPE_FIXED, 9 },
104 [GSM48_IE_CHANMODE_3] = { TLV_TYPE_TV },
105 [GSM48_IE_CHANMODE_4] = { TLV_TYPE_TV },
106 [GSM48_IE_CHANMODE_5] = { TLV_TYPE_TV },
107 [GSM48_IE_CHANMODE_6] = { TLV_TYPE_TV },
108 [GSM48_IE_CHANMODE_7] = { TLV_TYPE_TV },
109 [GSM48_IE_CHANMODE_8] = { TLV_TYPE_TV },
110 [GSM48_IE_FREQ_L_BEFORE] = { TLV_TYPE_TLV },
111 [GSM48_IE_CH_DESC_1_BEFORE] = { TLV_TYPE_FIXED, 3 },
112 [GSM48_IE_CH_DESC_2_BEFORE] = { TLV_TYPE_FIXED, 3 },
113 [GSM48_IE_F_CH_SEQ_BEFORE] = { TLV_TYPE_FIXED, 9 },
114 [GSM48_IE_CLASSMARK3] = { TLV_TYPE_TLV },
115 [GSM48_IE_MA_BEFORE] = { TLV_TYPE_TLV },
116 [GSM48_IE_RR_PACKET_UL] = { TLV_TYPE_TLV },
117 [GSM48_IE_RR_PACKET_DL] = { TLV_TYPE_TLV },
118 [GSM48_IE_CELL_CH_DESC] = { TLV_TYPE_FIXED, 16 },
119 [GSM48_IE_CHANMODE_1] = { TLV_TYPE_TV },
120 [GSM48_IE_CHDES_2_AFTER] = { TLV_TYPE_FIXED, 3 },
121 [GSM48_IE_MODE_SEC_CH] = { TLV_TYPE_TV },
122 [GSM48_IE_F_CH_SEQ_AFTER] = { TLV_TYPE_FIXED, 9 },
Neels Hofmeyr4b44ac42021-04-19 23:33:34 +0200123 [GSM48_IE_EXTENDED_TSC_SET] = { TLV_TYPE_TV },
Andreas Eversberg014cb872010-07-12 09:11:00 +0200124 [GSM48_IE_MA_AFTER] = { TLV_TYPE_TLV },
125 [GSM48_IE_BA_RANGE] = { TLV_TYPE_TLV },
126 [GSM48_IE_GROUP_CHDES] = { TLV_TYPE_TLV },
127 [GSM48_IE_BA_LIST_PREF] = { TLV_TYPE_TLV },
128 [GSM48_IE_MOB_OVSERV_DIF] = { TLV_TYPE_TLV },
129 [GSM48_IE_REALTIME_DIFF] = { TLV_TYPE_TLV },
130 [GSM48_IE_START_TIME] = { TLV_TYPE_FIXED, 2 },
131 [GSM48_IE_TIMING_ADVANCE] = { TLV_TYPE_TV },
Pau Espin Pedrol559a6ee2023-03-22 13:17:09 +0100132 [GSM48_IE_GROUP_CIP_SEQ_HO] = { TLV_TYPE_SINGLE_TV },
133 [GSM48_IE_CIP_MODE_SET_HO] = { TLV_TYPE_SINGLE_TV },
134 [GSM48_IE_GPRS_RESUMPT_HO] = { TLV_TYPE_SINGLE_TV },
135 [GSM48_IE_SYNC_IND_HO] = { TLV_TYPE_SINGLE_TV },
Andreas Eversberg014cb872010-07-12 09:11:00 +0200136 },
137};
138
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200139/*! TLV parser definitions for TS 04.08 MM */
Andreas Eversberg014cb872010-07-12 09:11:00 +0200140const struct tlv_definition gsm48_mm_att_tlvdef = {
141 .def = {
142 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
143 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
144 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
145 [GSM48_IE_UTC] = { TLV_TYPE_TV },
146 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
147 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
Jacob Erlbeck1c3f0882013-09-16 10:29:57 +0200148 [GSM48_IE_NET_DST] = { TLV_TYPE_TLV },
Andreas Eversberg014cb872010-07-12 09:11:00 +0200149
150 [GSM48_IE_LOCATION_AREA] = { TLV_TYPE_FIXED, 5 },
Pau Espin Pedrol559a6ee2023-03-22 13:17:09 +0100151 [GSM48_IE_PRIORITY_LEV_HO] = { TLV_TYPE_SINGLE_TV },
Andreas Eversberg014cb872010-07-12 09:11:00 +0200152 [GSM48_IE_FOLLOW_ON_PROC] = { TLV_TYPE_T },
153 [GSM48_IE_CTS_PERMISSION] = { TLV_TYPE_T },
154 },
155};
156
Harald Weltee9e190a2010-03-25 11:44:57 +0800157static const struct value_string rr_cause_names[] = {
158 { GSM48_RR_CAUSE_NORMAL, "Normal event" },
159 { GSM48_RR_CAUSE_ABNORMAL_UNSPEC, "Abnormal release, unspecified" },
160 { GSM48_RR_CAUSE_ABNORMAL_UNACCT, "Abnormal release, channel unacceptable" },
161 { GSM48_RR_CAUSE_ABNORMAL_TIMER, "Abnormal release, timer expired" },
162 { GSM48_RR_CAUSE_ABNORMAL_NOACT, "Abnormal release, no activity on radio path" },
163 { GSM48_RR_CAUSE_PREMPTIVE_REL, "Preemptive release" },
Vadim Yanitskiyf5f0d432020-10-29 18:21:53 +0700164 { GSM48_RR_CAUSE_UTRAN_CFG_UNK, "UTRAN configuration unknown" },
Harald Weltee9e190a2010-03-25 11:44:57 +0800165 { GSM48_RR_CAUSE_HNDOVER_IMP, "Handover impossible, timing advance out of range" },
166 { GSM48_RR_CAUSE_CHAN_MODE_UNACCT, "Channel mode unacceptable" },
167 { GSM48_RR_CAUSE_FREQ_NOT_IMPL, "Frequency not implemented" },
Vadim Yanitskiyf5f0d432020-10-29 18:21:53 +0700168 { GSM48_RR_CAUSE_LEAVE_GROUP_CA, "Originator or talker leaving group call area" },
169 { GSM48_RR_CAUSE_LOW_LEVEL_FAIL, "Lower layer failure" },
Harald Weltee9e190a2010-03-25 11:44:57 +0800170 { GSM48_RR_CAUSE_CALL_CLEARED, "Call already cleared" },
171 { GSM48_RR_CAUSE_SEMANT_INCORR, "Semantically incorrect message" },
172 { GSM48_RR_CAUSE_INVALID_MAND_INF, "Invalid mandatory information" },
Vadim Yanitskiy65954ba2020-10-29 18:08:52 +0700173 { GSM48_RR_CAUSE_MSG_TYPE_N, "Message type non-existent or not implemented" },
Harald Weltee9e190a2010-03-25 11:44:57 +0800174 { GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT, "Message type not compatible with protocol state" },
175 { GSM48_RR_CAUSE_COND_IE_ERROR, "Conditional IE error" },
176 { GSM48_RR_CAUSE_NO_CELL_ALLOC_A, "No cell allocation available" },
177 { GSM48_RR_CAUSE_PROT_ERROR_UNSPC, "Protocol error unspecified" },
178 { 0, NULL },
Harald Welte4fb20752010-03-02 23:17:33 +0100179};
180
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200181/*! return string representation of RR Cause value */
Philippada00422016-10-27 13:30:16 +0200182const char *rr_cause_name(uint8_t cause)
183{
184 return get_value_string(rr_cause_names, cause);
185}
186
Harald Welte4a62eda2019-03-18 18:27:00 +0100187/*! Return MCC-MNC-LAC-RAC as string, in a caller-provided output buffer.
188 * \param[out] buf caller-provided output buffer
189 * \param[in] buf_len size of buf in bytes
190 * \param[in] rai RAI to encode.
191 * \returns buf
192 */
193char *osmo_rai_name_buf(char *buf, size_t buf_len, const struct gprs_ra_id *rai)
194{
195 snprintf(buf, buf_len, "%s-%s-%u-%u",
196 osmo_mcc_name(rai->mcc), osmo_mnc_name(rai->mnc, rai->mnc_3_digits), rai->lac,
197 rai->rac);
198 return buf;
199}
200
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100201/*! Return MCC-MNC-LAC-RAC as string, in a static buffer.
202 * \param[in] rai RAI to encode.
203 * \returns Static string buffer.
204 */
205const char *osmo_rai_name(const struct gprs_ra_id *rai)
206{
Harald Welte171ef822019-03-28 10:49:05 +0100207 static __thread char buf[32];
Harald Welte4a62eda2019-03-18 18:27:00 +0100208 return osmo_rai_name_buf(buf, sizeof(buf), rai);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100209}
210
Harald Welte179f3572019-03-18 18:38:47 +0100211/*! Return MCC-MNC-LAC-RAC as string, in dynamically-allocated output buffer.
212 * \param[in] ctx talloc context from which to allocate output buffer
213 * \param[in] rai RAI to encode.
214 * \returns string representation in dynamically-allocated output buffer.
215 */
216char *osmo_rai_name_c(const void *ctx, const struct gprs_ra_id *rai)
217{
218 char *buf = talloc_size(ctx, 32);
219 if (!buf)
220 return NULL;
221 return osmo_rai_name_buf(buf, 32, rai);
222}
223
Harald Welte9eb6d882010-03-25 12:00:54 +0800224/* FIXME: convert to value_string */
Andreas Eversberg014cb872010-07-12 09:11:00 +0200225static const char *cc_state_names[32] = {
Harald Welte4fb20752010-03-02 23:17:33 +0100226 "NULL",
227 "INITIATED",
Andreas Eversberg1ef041f2010-04-09 07:52:12 +0200228 "MM_CONNECTION_PEND",
Harald Welte4fb20752010-03-02 23:17:33 +0100229 "MO_CALL_PROC",
230 "CALL_DELIVERED",
231 "illegal state 5",
232 "CALL_PRESENT",
233 "CALL_RECEIVED",
234 "CONNECT_REQUEST",
235 "MO_TERM_CALL_CONF",
236 "ACTIVE",
237 "DISCONNECT_REQ",
238 "DISCONNECT_IND",
239 "illegal state 13",
240 "illegal state 14",
241 "illegal state 15",
242 "illegal state 16",
243 "illegal state 17",
244 "illegal state 18",
245 "RELEASE_REQ",
246 "illegal state 20",
247 "illegal state 21",
248 "illegal state 22",
249 "illegal state 23",
250 "illegal state 24",
251 "illegal state 25",
252 "MO_ORIG_MODIFY",
253 "MO_TERM_MODIFY",
254 "CONNECT_IND",
255 "illegal state 29",
256 "illegal state 30",
257 "illegal state 31",
258};
259
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200260/*! return string representation of CC State */
Harald Welte9eb6d882010-03-25 12:00:54 +0800261const char *gsm48_cc_state_name(uint8_t state)
262{
263 if (state < ARRAY_SIZE(cc_state_names))
264 return cc_state_names[state];
265
266 return "invalid";
267}
268
269static const struct value_string cc_msg_names[] = {
270 { GSM48_MT_CC_ALERTING, "ALERTING" },
271 { GSM48_MT_CC_CALL_PROC, "CALL_PROC" },
272 { GSM48_MT_CC_PROGRESS, "PROGRESS" },
273 { GSM48_MT_CC_ESTAB, "ESTAB" },
274 { GSM48_MT_CC_SETUP, "SETUP" },
275 { GSM48_MT_CC_ESTAB_CONF, "ESTAB_CONF" },
276 { GSM48_MT_CC_CONNECT, "CONNECT" },
277 { GSM48_MT_CC_CALL_CONF, "CALL_CONF" },
278 { GSM48_MT_CC_START_CC, "START_CC" },
279 { GSM48_MT_CC_RECALL, "RECALL" },
280 { GSM48_MT_CC_EMERG_SETUP, "EMERG_SETUP" },
281 { GSM48_MT_CC_CONNECT_ACK, "CONNECT_ACK" },
282 { GSM48_MT_CC_USER_INFO, "USER_INFO" },
283 { GSM48_MT_CC_MODIFY_REJECT, "MODIFY_REJECT" },
284 { GSM48_MT_CC_MODIFY, "MODIFY" },
285 { GSM48_MT_CC_HOLD, "HOLD" },
286 { GSM48_MT_CC_HOLD_ACK, "HOLD_ACK" },
287 { GSM48_MT_CC_HOLD_REJ, "HOLD_REJ" },
288 { GSM48_MT_CC_RETR, "RETR" },
289 { GSM48_MT_CC_RETR_ACK, "RETR_ACK" },
290 { GSM48_MT_CC_RETR_REJ, "RETR_REJ" },
291 { GSM48_MT_CC_MODIFY_COMPL, "MODIFY_COMPL" },
292 { GSM48_MT_CC_DISCONNECT, "DISCONNECT" },
293 { GSM48_MT_CC_RELEASE_COMPL, "RELEASE_COMPL" },
294 { GSM48_MT_CC_RELEASE, "RELEASE" },
295 { GSM48_MT_CC_STOP_DTMF, "STOP_DTMF" },
296 { GSM48_MT_CC_STOP_DTMF_ACK, "STOP_DTMF_ACK" },
297 { GSM48_MT_CC_STATUS_ENQ, "STATUS_ENQ" },
298 { GSM48_MT_CC_START_DTMF, "START_DTMF" },
299 { GSM48_MT_CC_START_DTMF_ACK, "START_DTMF_ACK" },
300 { GSM48_MT_CC_START_DTMF_REJ, "START_DTMF_REJ" },
301 { GSM48_MT_CC_CONG_CTRL, "CONG_CTRL" },
302 { GSM48_MT_CC_FACILITY, "FACILITY" },
303 { GSM48_MT_CC_STATUS, "STATUS" },
304 { GSM48_MT_CC_NOTIFY, "NOTFIY" },
305 { 0, NULL }
Harald Welte61e2bfc2010-03-04 10:53:03 +0100306};
307
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200308/*! return string representation of CC Message Type */
Harald Welte9eb6d882010-03-25 12:00:54 +0800309const char *gsm48_cc_msg_name(uint8_t msgtype)
310{
311 return get_value_string(cc_msg_names, msgtype);
312}
Harald Welte4fb20752010-03-02 23:17:33 +0100313
Philipp72e43f02016-10-27 13:35:20 +0200314
315static const struct value_string rr_msg_names[] = {
316 /* Channel establishment messages */
317 { GSM48_MT_RR_INIT_REQ, "RR INITIALISATION REQUEST" },
318 { GSM48_MT_RR_ADD_ASS, "ADDITIONAL ASSIGNMENT" },
319 { GSM48_MT_RR_IMM_ASS, "IMMEDIATE ASSIGNMENT" },
320 { GSM48_MT_RR_IMM_ASS_EXT, "MMEDIATE ASSIGNMENT EXTENDED" },
321 { GSM48_MT_RR_IMM_ASS_REJ, "IMMEDIATE ASSIGNMENT REJECT" },
322 { GSM48_MT_RR_DTM_ASS_FAIL, "DTM ASSIGNMENT FAILURE" },
323 { GSM48_MT_RR_DTM_REJECT, "DTM REJECT" },
324 { GSM48_MT_RR_DTM_REQUEST, "DTM REQUEST" },
325 { GSM48_MT_RR_PACKET_ASS, "PACKET ASSIGNMENT" },
326
327 /* Ciphering messages */
328 { GSM48_MT_RR_CIPH_M_CMD, "CIPHERING MODE COMMAND" },
329 { GSM48_MT_RR_CIPH_M_COMPL, "CIPHERING MODE COMPLETE" },
330
331 /* Configuration change messages */
332 { GSM48_MT_RR_CFG_CHG_CMD, "CONFIGURATION CHANGE COMMAND" },
333 { GSM48_MT_RR_CFG_CHG_ACK, "CONFIGURATION CHANGE ACK" },
334 { GSM48_MT_RR_CFG_CHG_REJ, "CONFIGURATION CHANGE REJECT" },
335
336 /* Handover messages */
337 { GSM48_MT_RR_ASS_CMD, "ASSIGNMENT COMMAND" },
338 { GSM48_MT_RR_ASS_COMPL, "ASSIGNMENT COMPLETE" },
339 { GSM48_MT_RR_ASS_FAIL, "ASSIGNMENT FAILURE" },
340 { GSM48_MT_RR_HANDO_CMD, "HANDOVER COMMAND" },
341 { GSM48_MT_RR_HANDO_COMPL, "HANDOVER COMPLETE" },
342 { GSM48_MT_RR_HANDO_FAIL, "HANDOVER FAILURE" },
343 { GSM48_MT_RR_HANDO_INFO, "PHYSICAL INFORMATION" },
344 { GSM48_MT_RR_DTM_ASS_CMD, "DTM ASSIGNMENT COMMAND" },
345
346 { GSM48_MT_RR_CELL_CHG_ORDER, "RR-CELL CHANGE ORDER" },
347 { GSM48_MT_RR_PDCH_ASS_CMD, "PDCH ASSIGNMENT COMMAND" },
348
349 /* Channel release messages */
350 { GSM48_MT_RR_CHAN_REL, "CHANNEL RELEASE" },
351 { GSM48_MT_RR_PART_REL, "PARTIAL RELEASE" },
352 { GSM48_MT_RR_PART_REL_COMP, "PARTIAL RELEASE COMPLETE" },
353
354 /* Paging and Notification messages */
355 { GSM48_MT_RR_PAG_REQ_1, "PAGING REQUEST TYPE 1" },
356 { GSM48_MT_RR_PAG_REQ_2, "PAGING REQUEST TYPE 2" },
357 { GSM48_MT_RR_PAG_REQ_3, "PAGING REQUEST TYPE 3" },
358 { GSM48_MT_RR_PAG_RESP, "PAGING RESPONSE" },
359 { GSM48_MT_RR_NOTIF_NCH, "NOTIFICATION/NCH" },
360 { GSM48_MT_RR_NOTIF_FACCH, "(Reserved)" },
361 { GSM48_MT_RR_NOTIF_RESP, "NOTIFICATION/RESPONSE" },
362 { GSM48_MT_RR_PACKET_NOTIF, "PACKET NOTIFICATION" },
363 /* 3G Specific messages */
364 { GSM48_MT_RR_UTRAN_CLSM_CHG, "UTRAN Classmark Change" },
365 { GSM48_MT_RR_CDMA2K_CLSM_CHG, "cdma 2000 Classmark Change" },
366 { GSM48_MT_RR_IS_TO_UTRAN_HANDO, "Inter System to UTRAN Handover Command" },
367 { GSM48_MT_RR_IS_TO_CDMA2K_HANDO, "Inter System to cdma2000 Handover Command" },
368
369 /* System information messages */
370 { GSM48_MT_RR_SYSINFO_8, "SYSTEM INFORMATION TYPE 8" },
371 { GSM48_MT_RR_SYSINFO_1, "SYSTEM INFORMATION TYPE 1" },
372 { GSM48_MT_RR_SYSINFO_2, "SYSTEM INFORMATION TYPE 2" },
373 { GSM48_MT_RR_SYSINFO_3, "SYSTEM INFORMATION TYPE 3" },
374 { GSM48_MT_RR_SYSINFO_4, "SYSTEM INFORMATION TYPE 4" },
375 { GSM48_MT_RR_SYSINFO_5, "SYSTEM INFORMATION TYPE 5" },
376 { GSM48_MT_RR_SYSINFO_6, "SYSTEM INFORMATION TYPE 6" },
377 { GSM48_MT_RR_SYSINFO_7, "SYSTEM INFORMATION TYPE 7" },
378 { GSM48_MT_RR_SYSINFO_2bis, "SYSTEM INFORMATION TYPE 2bis" },
379 { GSM48_MT_RR_SYSINFO_2ter, "SYSTEM INFORMATION TYPE 2ter" },
380 { GSM48_MT_RR_SYSINFO_2quater, "SYSTEM INFORMATION TYPE 2quater" },
381 { GSM48_MT_RR_SYSINFO_5bis, "SYSTEM INFORMATION TYPE 5bis" },
382 { GSM48_MT_RR_SYSINFO_5ter, "SYSTEM INFORMATION TYPE 5ter" },
383 { GSM48_MT_RR_SYSINFO_9, "SYSTEM INFORMATION TYPE 9" },
384 { GSM48_MT_RR_SYSINFO_13, "SYSTEM INFORMATION TYPE 13" },
385 { GSM48_MT_RR_SYSINFO_16, "SYSTEM INFORMATION TYPE 16" },
386 { GSM48_MT_RR_SYSINFO_17, "SYSTEM INFORMATION TYPE 17" },
387 { GSM48_MT_RR_SYSINFO_18, "SYSTEM INFORMATION TYPE 18" },
388 { GSM48_MT_RR_SYSINFO_19, "SYSTEM INFORMATION TYPE 19" },
389 { GSM48_MT_RR_SYSINFO_20, "SYSTEM INFORMATION TYPE 20" },
390
391 /* Miscellaneous messages */
392 { GSM48_MT_RR_CHAN_MODE_MODIF, "CHANNEL MODE MODIFY" },
393 { GSM48_MT_RR_STATUS, "RR STATUS" },
394 { GSM48_MT_RR_CHAN_MODE_MODIF_ACK, "CHANNEL MODE MODIFY ACKNOWLEDGE" },
395 { GSM48_MT_RR_FREQ_REDEF, "FREQUENCY REDEFINITION" },
396 { GSM48_MT_RR_MEAS_REP, "MEASUREMENT REPORT" },
397 { GSM48_MT_RR_CLSM_CHG, "CLASSMARK CHANGE" },
398 { GSM48_MT_RR_CLSM_ENQ, "CLASSMARK ENQUIRY" },
399 { GSM48_MT_RR_EXT_MEAS_REP, "EXTENDED MEASUREMENT REPORT" },
400 { GSM48_MT_RR_EXT_MEAS_REP_ORD, "EXTENDED MEASUREMENT ORDER" },
401 { GSM48_MT_RR_GPRS_SUSP_REQ, "GPRS SUSPENSION REQUEST" },
402 { GSM48_MT_RR_DTM_INFO, "DTM INFORMATION" },
403
404 /* VGCS uplink control messages */
405 { GSM48_MT_RR_VGCS_UPL_GRANT, "VGCS UPLINK GRANT" },
406 { GSM48_MT_RR_UPLINK_RELEASE, "UPLINK RELEASE" },
407 { GSM48_MT_RR_UPLINK_FREE, "0c" },
408 { GSM48_MT_RR_UPLINK_BUSY, "UPLINK BUSY" },
409 { GSM48_MT_RR_TALKER_IND, "TALKER INDICATION" },
410
411 /* Application messages */
412 { GSM48_MT_RR_APP_INFO, "Application Information" },
413 { 0, NULL }
414};
415
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200416/*! return string representation of RR Message Type */
Philipp72e43f02016-10-27 13:35:20 +0200417const char *gsm48_rr_msg_name(uint8_t msgtype)
418{
419 return get_value_string(rr_msg_names, msgtype);
420}
421
Max600d4ee2023-01-23 21:49:52 +0300422/* 3GPP TS 44.018 Table 10.4.2 */
423static const struct value_string rr_msg_type_short_names[] = {
424 { GSM48_MT_RR_SH_SI10, "System Information Type 10" },
425 { GSM48_MT_RR_SH_FACCH, "Notification/FACCH" },
426 { GSM48_MT_RR_SH_UL_FREE, "Uplink Free" },
427 { GSM48_MT_RR_SH_MEAS_REP, "Enhanced Measurement Report (uplink)" },
428 { GSM48_MT_RR_SH_MEAS_INFO, "Measurement Information (downlink)" },
429 { GSM48_MT_RR_SH_VGCS_RECON, "VBS/VGCS Reconfigure" },
430 { GSM48_MT_RR_SH_VGCS_RECON2, "VBS/VGCS Reconfigure2" },
431 { GSM48_MT_RR_SH_VGCS_INFO, "VGCS Additional Information" },
432 { GSM48_MT_RR_SH_VGCS_SMS, "VGCS SMS Information" },
433 { GSM48_MT_RR_SH_SI10bis, "System Information Type 10bis" },
434 { GSM48_MT_RR_SH_SI10ter, "System Information Type 10ter" },
435 { GSM48_MT_RR_SH_VGCS_NEIGH, "VGCS Neighbour Cell Information" },
436 { GSM48_MT_RR_SH_APP_DATA, "Notify Application Data" },
437 { 0, NULL }
438};
439
440/*! return string representation of RR Message Type using the RR short protocol discriminator */
Maxbc127282023-01-26 14:40:23 +0300441const char *gsm48_rr_short_pd_msg_name(uint8_t msgtype)
Max600d4ee2023-01-23 21:49:52 +0300442{
443 return get_value_string(rr_msg_type_short_names, msgtype);
444}
Philipp72e43f02016-10-27 13:35:20 +0200445
Maxfb348ee2016-03-30 21:14:53 +0200446const struct value_string gsm48_chan_mode_names[] = {
447 { GSM48_CMODE_SIGN, "SIGNALLING" },
448 { GSM48_CMODE_SPEECH_V1, "SPEECH_V1" },
449 { GSM48_CMODE_SPEECH_EFR, "SPEECH_EFR" },
450 { GSM48_CMODE_SPEECH_AMR, "SPEECH_AMR" },
Vadim Yanitskiy1c88ff92023-03-23 06:33:51 +0700451 { GSM48_CMODE_SPEECH_V4, "SPEECH_V4" },
452 { GSM48_CMODE_SPEECH_V5, "SPEECH_V5" },
453 { GSM48_CMODE_SPEECH_V6, "SPEECH_V6" },
454
Vadim Yanitskiy173c84c2023-03-23 06:34:13 +0700455 { GSM48_CMODE_DATA_43k5_14k5, "DATA_43k5_14k5" },
456 { GSM48_CMODE_DATA_29k0_14k5, "DATA_29k0_14k5" },
457 { GSM48_CMODE_DATA_43k5_29k0, "DATA_43k5_29k0" },
458 { GSM48_CMODE_DATA_14k5_43k5, "DATA_14k5_43k5" },
459 { GSM48_CMODE_DATA_14k5_29k0, "DATA_14k5_29k0" },
460 { GSM48_CMODE_DATA_29k0_43k5, "DATA_29k0_43k5" },
461
462 { GSM48_CMODE_DATA_43k5, "DATA_43k5" },
463 { GSM48_CMODE_DATA_32k0, "DATA_32k0" },
464 { GSM48_CMODE_DATA_29k0, "DATA_29k0" },
Maxfb348ee2016-03-30 21:14:53 +0200465 { GSM48_CMODE_DATA_14k5, "DATA_14k5" },
466 { GSM48_CMODE_DATA_12k0, "DATA_12k0" },
467 { GSM48_CMODE_DATA_6k0, "DATA_6k0" },
468 { GSM48_CMODE_DATA_3k6, "DATA_3k6" },
Vadim Yanitskiy1c88ff92023-03-23 06:33:51 +0700469
Neels Hofmeyra9da9f72021-04-16 15:36:58 +0200470 { GSM48_CMODE_SPEECH_V1_VAMOS, "SPEECH_V1_VAMOS" },
471 { GSM48_CMODE_SPEECH_V2_VAMOS, "SPEECH_V2_VAMOS" },
472 { GSM48_CMODE_SPEECH_V3_VAMOS, "SPEECH_V3_VAMOS" },
473 { GSM48_CMODE_SPEECH_V5_VAMOS, "SPEECH_V5_VAMOS" },
Maxfb348ee2016-03-30 21:14:53 +0200474 { 0, NULL },
475};
476
Neels Hofmeyra9da9f72021-04-16 15:36:58 +0200477/*! Translate GSM48_CMODE_SPEECH_* to its corresponding GSM48_CMODE_SPEECH_*_VAMOS mode.
478 * If the mode has no equivalent VAMOS mode, return a negative value.
479 */
480enum gsm48_chan_mode gsm48_chan_mode_to_vamos(enum gsm48_chan_mode mode)
481{
482 switch (mode) {
483 case GSM48_CMODE_SPEECH_V1:
484 case GSM48_CMODE_SPEECH_V1_VAMOS:
485 return GSM48_CMODE_SPEECH_V1_VAMOS;
486 case GSM48_CMODE_SPEECH_EFR:
487 case GSM48_CMODE_SPEECH_V2_VAMOS:
488 return GSM48_CMODE_SPEECH_V2_VAMOS;
489 case GSM48_CMODE_SPEECH_AMR:
490 case GSM48_CMODE_SPEECH_V3_VAMOS:
491 return GSM48_CMODE_SPEECH_V3_VAMOS;
492 case GSM48_CMODE_SPEECH_V5_VAMOS:
493 return GSM48_CMODE_SPEECH_V5_VAMOS;
494 default:
495 return -1;
496 }
497}
498
499/*! Translate GSM48_CMODE_SPEECH_*_VAMOS to its corresponding GSM48_CMODE_SPEECH_* non-vamos mode.
Neels Hofmeyr208c5b62021-05-29 21:31:07 +0000500 * If the mode is not a VAMOS mode, return the unchanged mode.
Neels Hofmeyra9da9f72021-04-16 15:36:58 +0200501 */
502enum gsm48_chan_mode gsm48_chan_mode_to_non_vamos(enum gsm48_chan_mode mode)
503{
504 switch (mode) {
505 case GSM48_CMODE_SPEECH_V1_VAMOS:
Neels Hofmeyra9da9f72021-04-16 15:36:58 +0200506 return GSM48_CMODE_SPEECH_V1;
507 case GSM48_CMODE_SPEECH_V2_VAMOS:
Neels Hofmeyra9da9f72021-04-16 15:36:58 +0200508 return GSM48_CMODE_SPEECH_EFR;
509 case GSM48_CMODE_SPEECH_V3_VAMOS:
Neels Hofmeyra9da9f72021-04-16 15:36:58 +0200510 return GSM48_CMODE_SPEECH_AMR;
Vadim Yanitskiy1c88ff92023-03-23 06:33:51 +0700511 case GSM48_CMODE_SPEECH_V5_VAMOS:
512 return GSM48_CMODE_SPEECH_V5;
Neels Hofmeyra9da9f72021-04-16 15:36:58 +0200513 default:
Neels Hofmeyr208c5b62021-05-29 21:31:07 +0000514 return mode;
Neels Hofmeyra9da9f72021-04-16 15:36:58 +0200515 }
516}
517
Maxfb348ee2016-03-30 21:14:53 +0200518const struct value_string gsm_chan_t_names[] = {
519 { GSM_LCHAN_NONE, "NONE" },
520 { GSM_LCHAN_SDCCH, "SDCCH" },
521 { GSM_LCHAN_TCH_F, "TCH_F" },
522 { GSM_LCHAN_TCH_H, "TCH_H" },
523 { GSM_LCHAN_UNKNOWN, "UNKNOWN" },
524 { GSM_LCHAN_CCCH, "CCCH" },
525 { GSM_LCHAN_PDTCH, "PDTCH" },
526 { GSM_LCHAN_CBCH, "CBCH" },
527 { 0, NULL },
528};
529
Harald Welte1a8c4e02015-08-16 17:56:25 +0200530static const struct value_string mi_type_names[] = {
531 { GSM_MI_TYPE_NONE, "NONE" },
532 { GSM_MI_TYPE_IMSI, "IMSI" },
533 { GSM_MI_TYPE_IMEI, "IMEI" },
534 { GSM_MI_TYPE_IMEISV, "IMEI-SV" },
535 { GSM_MI_TYPE_TMSI, "TMSI" },
536 { 0, NULL }
537};
538
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200539/*! return string representation of Mobile Identity Type */
Harald Welte1a8c4e02015-08-16 17:56:25 +0200540const char *gsm48_mi_type_name(uint8_t mi)
541{
542 return get_value_string(mi_type_names, mi);
543}
544
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200545/*! Deprecated, see osmo_mobile_identity instead.
546 * Return a human readable representation of a Mobile Identity in caller-provided buffer.
Harald Welte4a62eda2019-03-18 18:27:00 +0100547 * \param[out] buf caller-provided output buffer
548 * \param[in] buf_len size of buf in bytes
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100549 * \param[in] mi Mobile Identity buffer containing 3GPP TS 04.08 style MI type and data.
550 * \param[in] mi_len Length of mi.
Harald Welte4a62eda2019-03-18 18:27:00 +0100551 * \return buf
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100552 */
Harald Weltea13fb752020-06-16 08:44:42 +0200553char *osmo_mi_name_buf(char *buf, size_t buf_len, const uint8_t *mi, uint8_t mi_len)
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100554{
Harald Weltea13fb752020-06-16 08:44:42 +0200555 uint8_t mi_type;
556 uint32_t tmsi;
557 char mi_string[GSM48_MI_SIZE];
558
559 mi_type = (mi && mi_len) ? (mi[0] & GSM_MI_TYPE_MASK) : GSM_MI_TYPE_NONE;
560
561 switch (mi_type) {
562 case GSM_MI_TYPE_TMSI:
563 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
564 if (mi_len == GSM48_TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
565 tmsi = osmo_load32be(&mi[1]);
566 snprintf(buf, buf_len, "TMSI-0x%08" PRIX32, tmsi);
567 } else {
568 snprintf(buf, buf_len, "TMSI-invalid");
569 }
570 return buf;
571
572 case GSM_MI_TYPE_IMSI:
573 case GSM_MI_TYPE_IMEI:
574 case GSM_MI_TYPE_IMEISV:
575 osmo_bcd2str(mi_string, sizeof(mi_string), mi, 1, (mi_len * 2) - (mi[0] & GSM_MI_ODD ? 0 : 1), true);
576 snprintf(buf, buf_len, "%s-%s", gsm48_mi_type_name(mi_type), mi_string);
577 return buf;
578
579 default:
Neels Hofmeyr6d670322020-05-26 03:12:29 +0200580 snprintf(buf, buf_len, "unknown");
581 return buf;
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100582 }
583}
584
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200585/*! Deprecated, see osmo_mobile_identity instead.
586 * Return a human readable representation of a Mobile Identity in static buffer.
Harald Welte4a62eda2019-03-18 18:27:00 +0100587 * \param[in] mi Mobile Identity buffer containing 3GPP TS 04.08 style MI type and data.
588 * \param[in] mi_len Length of mi.
589 * \return A string like "IMSI-1234567", "TMSI-0x1234ABCD" or "unknown", "TMSI-invalid"...
590 */
Harald Weltea13fb752020-06-16 08:44:42 +0200591const char *osmo_mi_name(const uint8_t *mi, uint8_t mi_len)
Harald Welte4a62eda2019-03-18 18:27:00 +0100592{
Harald Welte171ef822019-03-28 10:49:05 +0100593 static __thread char mi_name[10 + GSM48_MI_SIZE + 1];
Harald Weltea13fb752020-06-16 08:44:42 +0200594 return osmo_mi_name_buf(mi_name, sizeof(mi_name), mi, mi_len);
Harald Welte4a62eda2019-03-18 18:27:00 +0100595}
596
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200597/*! Deprecated, see osmo_mobile_identity instead.
598 * Return a human readable representation of a Mobile Identity in dynamically-allocated buffer.
Harald Welte179f3572019-03-18 18:38:47 +0100599 * \param[in] ctx talloc context from which to allocate output buffer
600 * \param[in] mi Mobile Identity buffer containing 3GPP TS 04.08 style MI type and data.
601 * \param[in] mi_len Length of mi.
602 * \return A string like "IMSI-1234567", "TMSI-0x1234ABCD" or "unknown", "TMSI-invalid" in a
603 * dynamically-allocated output buffer.
604 */
Harald Weltea13fb752020-06-16 08:44:42 +0200605char *osmo_mi_name_c(const void *ctx, const uint8_t *mi, uint8_t mi_len)
Harald Welte179f3572019-03-18 18:38:47 +0100606{
Harald Weltea13fb752020-06-16 08:44:42 +0200607 size_t buf_len = 10 + GSM48_MI_SIZE + 1;
608 char *mi_name = talloc_size(ctx, buf_len);
609 if (!mi_name)
610 return NULL;
611 return osmo_mi_name_buf(mi_name, buf_len, mi, mi_len);
Harald Welte179f3572019-03-18 18:38:47 +0100612}
613
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200614/*! Extract Mobile Identity from encoded bytes (3GPP TS 24.008 10.5.1.4).
615 *
616 * On failure (negative return value), mi->type == GSM_MI_TYPE_NONE, mi->string[] is all-zero and mi->tmsi ==
617 * GSM_RESERVED_TMSI.
618 *
619 * On success, mi->type reflects the decoded Mobile Identity type (GSM_MI_TYPE_IMSI, GSM_MI_TYPE_TMSI, GSM_MI_TYPE_IMEI
620 * or GSM_MI_TYPE_IMEISV).
621 *
622 * On success, mi->string always contains a human readable representation of the Mobile Identity digits: IMSI, IMEI and
623 * IMEISV as digits like "12345678", and TMSI as "0x" and 8 hexadecimal digits like "0x1234abcd".
624 *
625 * mi->tmsi contains the uint32_t TMSI value iff the extracted Mobile Identity was a TMSI, or GSM_RESERVED_TMSI
626 * otherwise.
627 *
628 * \param[out] mi Return buffer for decoded Mobile Identity.
629 * \param[in] mi_data The encoded Mobile Identity octets.
630 * \param[in] mi_len Number of octets in mi_data.
631 * \param[in] allow_hex If false, hexadecimal digits (>9) result in an error return value.
632 * \returns 0 on success, negative on error: -EBADMSG = invalid length indication or invalid data,
633 * -EINVAL = unknown Mobile Identity type.
634 */
635int osmo_mobile_identity_decode(struct osmo_mobile_identity *mi, const uint8_t *mi_data, uint8_t mi_len,
636 bool allow_hex)
637{
638 int rc;
639 int nibbles_len;
Pau Espin Pedrol6fe865d2021-07-15 13:08:08 +0200640 char *str = NULL; /* initialize to avoid uninitialized false warnings on some gcc versions (11.1.0) */
641 size_t str_size = 0; /* initialize to avoid uninitialized false warnings on some gcc versions (11.1.0) */
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200642
643 if (!mi_data || mi_len < 1)
644 return -EBADMSG;
645
646 nibbles_len = (mi_len - 1) * 2 + ((mi_data[0] & GSM_MI_ODD) ? 1 : 0);
647
648 *mi = (struct osmo_mobile_identity){
649 .type = mi_data[0] & GSM_MI_TYPE_MASK,
650 };
651
652 /* First do length checks */
653 switch (mi->type) {
654 case GSM_MI_TYPE_TMSI:
655 mi->tmsi = GSM_RESERVED_TMSI;
656 if (nibbles_len != (GSM23003_TMSI_NUM_BYTES * 2)) {
657 rc = -EBADMSG;
658 goto return_error;
659 }
660 break;
661
662 case GSM_MI_TYPE_IMSI:
663 if (nibbles_len < GSM23003_IMSI_MIN_DIGITS || nibbles_len > GSM23003_IMSI_MAX_DIGITS) {
664 rc = -EBADMSG;
665 goto return_error;
666 }
667 str = mi->imsi;
668 str_size = sizeof(mi->imsi);
669 break;
670
671 case GSM_MI_TYPE_IMEI:
672 if (nibbles_len != GSM23003_IMEI_NUM_DIGITS && nibbles_len != GSM23003_IMEI_NUM_DIGITS_NO_CHK) {
673 rc = -EBADMSG;
674 goto return_error;
675 }
676 str = mi->imei;
677 str_size = sizeof(mi->imei);
678 break;
679
680 case GSM_MI_TYPE_IMEISV:
681 if (nibbles_len != GSM23003_IMEISV_NUM_DIGITS) {
682 rc = -EBADMSG;
683 goto return_error;
684 }
685 str = mi->imeisv;
686 str_size = sizeof(mi->imeisv);
687 break;
688
689 default:
690 rc = -EINVAL;
691 goto return_error;
692 }
693
694 /* Decode BCD digits */
695 switch (mi->type) {
696 case GSM_MI_TYPE_TMSI:
697 /* MI is a 32bit integer TMSI. Length has been checked above. */
698 if ((mi_data[0] & 0xf0) != 0xf0) {
699 /* A TMSI always has the first nibble == 0xf */
700 rc = -EBADMSG;
701 goto return_error;
702 }
703 mi->tmsi = osmo_load32be(&mi_data[1]);
704 return 0;
705
706 case GSM_MI_TYPE_IMSI:
707 case GSM_MI_TYPE_IMEI:
708 case GSM_MI_TYPE_IMEISV:
709 /* If the length is even, the last nibble (higher nibble of last octet) must be 0xf */
710 if (!(mi_data[0] & GSM_MI_ODD)
711 && ((mi_data[mi_len - 1] & 0xf0) != 0xf0)) {
712 rc = -EBADMSG;
713 goto return_error;
714 }
715 rc = osmo_bcd2str(str, str_size, mi_data, 1, 1 + nibbles_len, allow_hex);
Pau Espin Pedrol6fe865d2021-07-15 13:08:08 +0200716 /* check mi->str printing rc */
717 if (rc < 1 || rc >= str_size) {
718 rc = -EBADMSG;
719 goto return_error;
720 }
721 return 0;
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200722
723 default:
724 /* Already handled above, but as future bug paranoia: */
725 rc = -EINVAL;
726 goto return_error;
727 }
728
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200729return_error:
730 *mi = (struct osmo_mobile_identity){
731 .type = GSM_MI_TYPE_NONE,
732 };
733 return rc;
734}
735
736/*! Return the number of encoded Mobile Identity octets, without actually encoding.
737 * Useful to write tag-length header before encoding the MI.
738 * \param[in] mi Mobile Identity.
739 * \param[out] mi_digits If not NULL, store the number of nibbles of used MI data (i.e. strlen(mi->string) or 8 for a TMSI).
740 * \return octets that osmo_mobile_identity_encode_msgb() will write for this mi.
741 */
742int osmo_mobile_identity_encoded_len(const struct osmo_mobile_identity *mi, int *mi_digits)
743{
744 int mi_nibbles;
745 if (!mi)
746 return -EINVAL;
747 switch (mi->type) {
748 case GSM_MI_TYPE_TMSI:
749 mi_nibbles = GSM23003_TMSI_NUM_BYTES * 2;
750 break;
751 case GSM_MI_TYPE_IMSI:
752 mi_nibbles = strlen(mi->imsi);
753 if (mi_nibbles < GSM23003_IMSI_MIN_DIGITS
754 || mi_nibbles > GSM23003_IMSI_MAX_DIGITS)
755 return -EINVAL;
756 break;
757 case GSM_MI_TYPE_IMEI:
758 mi_nibbles = strlen(mi->imei);
759 if (mi_nibbles < GSM23003_IMEI_NUM_DIGITS_NO_CHK
760 || mi_nibbles > GSM23003_IMEI_NUM_DIGITS)
761 return -EINVAL;
762 break;
763 case GSM_MI_TYPE_IMEISV:
764 mi_nibbles = strlen(mi->imeisv);
765 if (mi_nibbles != GSM23003_IMEISV_NUM_DIGITS)
766 return -EINVAL;
767 break;
768 default:
769 return -ENOTSUP;
770 }
771
772 if (mi_digits)
773 *mi_digits = mi_nibbles;
774
775 /* one type nibble, plus the MI nibbles, plus a filler nibble to complete the last octet:
776 * mi_octets = ceil((float)(mi_nibbles + 1) / 2)
777 */
778 return (mi_nibbles + 2) / 2;
779}
780
781/*! Encode Mobile Identity from uint32_t (TMSI) or digits string (all others) (3GPP TS 24.008 10.5.1.4).
782 *
783 * \param[out] buf Return buffer for encoded Mobile Identity.
784 * \param[in] buflen sizeof(buf).
785 * \param[in] mi Mobile identity to encode.
786 * \param[in] allow_hex If false, hexadecimal digits (>9) result in an error return value.
787 * \returns Amount of bytes written to buf, or negative on error.
788 */
789int osmo_mobile_identity_encode_buf(uint8_t *buf, size_t buflen, const struct osmo_mobile_identity *mi, bool allow_hex)
790{
791 int rc;
792 int nibbles_len;
793 int mi_octets;
794 const char *mi_str;
795
796 if (!buf || !buflen)
797 return -EIO;
798
799 mi_octets = osmo_mobile_identity_encoded_len(mi, &nibbles_len);
800 if (mi_octets < 0)
801 return mi_octets;
802 if (mi_octets > buflen)
803 return -ENOSPC;
804
805 buf[0] = (mi->type & GSM_MI_TYPE_MASK) | ((nibbles_len & 1) ? GSM_MI_ODD : 0);
806
807 switch (mi->type) {
808 case GSM_MI_TYPE_TMSI:
809 buf[0] |= 0xf0;
810 osmo_store32be(mi->tmsi, &buf[1]);
811 return mi_octets;
812
813 case GSM_MI_TYPE_IMSI:
814 mi_str = mi->imsi;
815 break;
816 case GSM_MI_TYPE_IMEI:
817 mi_str = mi->imei;
818 break;
819 case GSM_MI_TYPE_IMEISV:
820 mi_str = mi->imeisv;
821 break;
822 default:
823 return -ENOTSUP;
824 }
825 rc = osmo_str2bcd(buf, buflen, mi_str, 1, -1, allow_hex);
826 if (rc != mi_octets)
827 return -EINVAL;
828 return mi_octets;
829}
830
831/*! Encode Mobile Identity type and BCD digits, appended to a msgb.
832 * Example to add a GSM48_IE_MOBILE_ID IEI with tag and length to a msgb:
833 *
Neels Hofmeyr4247c942020-06-22 17:48:01 +0200834 * struct osmo_mobile_identity mi = { .type = GSM_MI_TYPE_IMSI };
835 * OSMO_STRLCPY_ARRAY(mi.imsi, "1234567890123456");
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200836 * uint8_t *l = msgb_tl_put(msg, GSM48_IE_MOBILE_ID);
837 * int rc = osmo_mobile_identity_encode_msgb(msg, &mi, false);
838 * if (rc < 0)
839 * goto error;
840 * *l = rc;
841 *
842 * Example to add a BSSGP_IE_IMSI with tag and variable-size length, where the
843 * length needs to be known at the time of writing the IE tag-length header:
844 *
845 * struct osmo_mobile_identity mi = { .type = GSM_MI_TYPE_IMSI, };
846 * OSMO_STRLCPY_ARRAY(mi.imsi, pinfo->imsi);
847 * msgb_tvl_put(msg, BSSGP_IE_IMSI, osmo_mobile_identity_encoded_len(&mi, NULL));
848 * if (osmo_mobile_identity_encode_msgb(msg, &mi, false) < 0)
849 * goto error;
850 */
851int osmo_mobile_identity_encode_msgb(struct msgb *msg, const struct osmo_mobile_identity *mi, bool allow_hex)
852{
853 int rc = osmo_mobile_identity_encode_buf(msg->tail, msgb_tailroom(msg), mi, allow_hex);
854 if (rc < 0)
855 return rc;
856 msgb_put(msg, rc);
857 return rc;
858}
859
860/*! Extract Mobile Identity from a Complete Layer 3 message.
861 *
862 * Determine the Mobile Identity data and call osmo_mobile_identity_decode() to return a decoded struct
863 * osmo_mobile_identity.
864 *
865 * \param[out] mi Return buffer for decoded Mobile Identity.
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200866 * \param[in] l3_data The Complete Layer 3 message to extract from (LU, CM Service Req or Paging Resp).
867 * \param[in] l3_len Length of l3_data in bytes.
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200868 * \returns 0 on success, negative on error: return codes as defined in osmo_mobile_identity_decode(), or
869 * -ENOTSUP = not a Complete Layer 3 message,
870 */
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200871int osmo_mobile_identity_decode_from_l3_buf(struct osmo_mobile_identity *mi, const uint8_t *l3_data, size_t l3_len,
872 bool allow_hex)
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200873{
874 const struct gsm48_hdr *gh;
875 int8_t pdisc = 0;
876 uint8_t mtype = 0;
877 const struct gsm48_loc_upd_req *lu;
878 const uint8_t *cm2_buf;
879 uint8_t cm2_len;
880 const uint8_t *mi_start;
881 const struct gsm48_pag_resp *paging_response;
882 const uint8_t *mi_data;
883 uint8_t mi_len;
884 const struct gsm48_imsi_detach_ind *idi;
885
886 *mi = (struct osmo_mobile_identity){
887 .type = GSM_MI_TYPE_NONE,
888 .tmsi = GSM_RESERVED_TMSI,
889 };
890
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200891 if (l3_len < sizeof(*gh))
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200892 return -EBADMSG;
893
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200894 gh = (void *)l3_data;
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200895 pdisc = gsm48_hdr_pdisc(gh);
896 mtype = gsm48_hdr_msg_type(gh);
897
898 switch (pdisc) {
899 case GSM48_PDISC_MM:
900
901 switch (mtype) {
902 case GSM48_MT_MM_LOC_UPD_REQUEST:
903 /* First make sure that lu-> can be dereferenced */
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200904 if (l3_len < sizeof(*gh) + sizeof(*lu))
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200905 return -EBADMSG;
906
907 /* Now we know there is enough msgb data to read a lu->mi_len, so also check that */
908 lu = (struct gsm48_loc_upd_req*)gh->data;
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200909 if (l3_len < sizeof(*gh) + sizeof(*lu) + lu->mi_len)
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200910 return -EBADMSG;
911 mi_data = lu->mi;
912 mi_len = lu->mi_len;
913 goto got_mi;
914
915 case GSM48_MT_MM_CM_SERV_REQ:
916 case GSM48_MT_MM_CM_REEST_REQ:
917 /* Unfortunately in Phase1 the Classmark2 length is variable, so we cannot
918 * just use gsm48_service_request struct, and need to parse it manually. */
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200919 if (l3_len < sizeof(*gh) + 2)
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200920 return -EBADMSG;
921
922 cm2_len = gh->data[1];
923 cm2_buf = gh->data + 2;
924 goto got_cm2;
925
926 case GSM48_MT_MM_IMSI_DETACH_IND:
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200927 if (l3_len < sizeof(*gh) + sizeof(*idi))
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200928 return -EBADMSG;
929 idi = (struct gsm48_imsi_detach_ind*) gh->data;
930 mi_data = idi->mi;
931 mi_len = idi->mi_len;
932 goto got_mi;
933
934 case GSM48_MT_MM_ID_RESP:
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200935 if (l3_len < sizeof(*gh) + 2)
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200936 return -EBADMSG;
937 mi_data = gh->data+1;
938 mi_len = gh->data[0];
939 goto got_mi;
940
941 default:
942 break;
943 }
944 break;
945
946 case GSM48_PDISC_RR:
947
948 switch (mtype) {
949 case GSM48_MT_RR_PAG_RESP:
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200950 if (l3_len < sizeof(*gh) + sizeof(*paging_response))
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200951 return -EBADMSG;
952 paging_response = (struct gsm48_pag_resp*)gh->data;
953 cm2_len = paging_response->cm2_len;
954 cm2_buf = (uint8_t*)&paging_response->cm2;
955 goto got_cm2;
956
957 default:
958 break;
959 }
960 break;
961 }
962
963 return -ENOTSUP;
964
965got_cm2:
966 /* MI (Mobile Identity) LV follows the Classmark2 */
967
968 /* There must be at least a mi_len byte after the CM2 */
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200969 if (cm2_buf + cm2_len + 1 > l3_data + l3_len)
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200970 return -EBADMSG;
971
972 mi_start = cm2_buf + cm2_len;
973 mi_len = mi_start[0];
974 mi_data = mi_start + 1;
975
976got_mi:
977 /* mi_data points at the start of the Mobile Identity coding of mi_len bytes */
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200978 if (mi_data + mi_len > l3_data + l3_len)
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200979 return -EBADMSG;
980
981 return osmo_mobile_identity_decode(mi, mi_data, mi_len, allow_hex);
982}
983
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200984/*! Extract Mobile Identity from a Complete Layer 3 message.
985 *
986 * Determine the Mobile Identity data and call osmo_mobile_identity_decode() to return a decoded struct
987 * osmo_mobile_identity.
988 *
989 * \param[out] mi Return buffer for decoded Mobile Identity.
990 * \param[in] msg The Complete Layer 3 message to extract from (LU, CM Service Req or Paging Resp).
991 * \returns 0 on success, negative on error: return codes as defined in osmo_mobile_identity_decode(), or
992 * -ENOTSUP = not a Complete Layer 3 message,
993 */
994int osmo_mobile_identity_decode_from_l3(struct osmo_mobile_identity *mi, struct msgb *msg, bool allow_hex)
995{
996 return osmo_mobile_identity_decode_from_l3_buf(mi, msgb_l3(msg), msgb_l3len(msg), allow_hex);
997}
998
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200999/*! Return a human readable representation of a struct osmo_mobile_identity.
1000 * Write a string like "IMSI-1234567", "TMSI-0x1234ABCD" or "NONE", "NULL".
1001 * \param[out] buf String buffer to write to.
1002 * \param[in] buflen sizeof(buf).
1003 * \param[in] mi Decoded Mobile Identity data.
1004 * \return the strlen() of the string written when buflen is sufficiently large, like snprintf().
1005 */
1006int osmo_mobile_identity_to_str_buf(char *buf, size_t buflen, const struct osmo_mobile_identity *mi)
1007{
1008 struct osmo_strbuf sb = { .buf = buf, .len = buflen };
1009 if (!mi)
1010 return snprintf(buf, buflen, "NULL");
1011 OSMO_STRBUF_PRINTF(sb, "%s", gsm48_mi_type_name(mi->type));
1012 switch (mi->type) {
1013 case GSM_MI_TYPE_TMSI:
1014 OSMO_STRBUF_PRINTF(sb, "-0x%08" PRIX32, mi->tmsi);
1015 break;
1016 case GSM_MI_TYPE_IMSI:
1017 OSMO_STRBUF_PRINTF(sb, "-%s", mi->imsi);
1018 break;
1019 case GSM_MI_TYPE_IMEI:
1020 OSMO_STRBUF_PRINTF(sb, "-%s", mi->imei);
1021 break;
1022 case GSM_MI_TYPE_IMEISV:
1023 OSMO_STRBUF_PRINTF(sb, "-%s", mi->imeisv);
1024 break;
1025 default:
1026 break;
1027 }
1028 return sb.chars_needed;
1029}
1030
1031/*! Like osmo_mobile_identity_to_str_buf(), but return the string in a talloc buffer.
1032 * \param[in] ctx Talloc context to allocate from.
1033 * \param[in] mi Decoded Mobile Identity data.
1034 * \return a string like "IMSI-1234567", "TMSI-0x1234ABCD" or "NONE", "NULL".
1035 */
1036char *osmo_mobile_identity_to_str_c(void *ctx, const struct osmo_mobile_identity *mi)
1037{
1038 OSMO_NAME_C_IMPL(ctx, 32, "ERROR", osmo_mobile_identity_to_str_buf, mi)
1039}
1040
1041/*! Compare two osmo_mobile_identity structs, returning typical cmp() result.
1042 * \param[in] a Left side osmo_mobile_identity.
1043 * \param[in] b Right side osmo_mobile_identity.
1044 * \returns 0 if both are equal, -1 if a < b, 1 if a > b.
1045 */
1046int osmo_mobile_identity_cmp(const struct osmo_mobile_identity *a, const struct osmo_mobile_identity *b)
1047{
1048 int cmp;
1049 if (a == b)
1050 return 0;
1051 if (!a)
1052 return -1;
1053 if (!b)
1054 return 1;
1055 cmp = OSMO_CMP(a->type, b->type);
1056 if (cmp)
1057 return cmp;
1058 switch (a->type) {
1059 case GSM_MI_TYPE_TMSI:
1060 return OSMO_CMP(a->tmsi, b->tmsi);
1061 case GSM_MI_TYPE_IMSI:
1062 return strncmp(a->imsi, b->imsi, sizeof(a->imsi));
1063 case GSM_MI_TYPE_IMEI:
1064 return strncmp(a->imei, b->imei, sizeof(a->imei));
1065 case GSM_MI_TYPE_IMEISV:
1066 return strncmp(a->imeisv, b->imeisv, sizeof(a->imeisv));
1067 default:
1068 /* No known type, but both have the same type. */
1069 return 0;
1070 }
1071}
1072
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001073/*! Checks is particular message is cipherable in A/Gb mode according to
Maxfdca25d2016-07-05 16:06:28 +02001074 * 3GPP TS 24.008 § 4.7.1.2
1075 * \param[in] hdr Message header
1076 * \return true if message can be encrypted, false otherwise
1077 */
1078bool gsm48_hdr_gmm_cipherable(const struct gsm48_hdr *hdr)
1079{
1080 switch(hdr->msg_type) {
1081 case GSM48_MT_GMM_ATTACH_REQ:
1082 case GSM48_MT_GMM_ATTACH_REJ:
1083 case GSM48_MT_GMM_AUTH_CIPH_REQ:
1084 case GSM48_MT_GMM_AUTH_CIPH_RESP:
1085 case GSM48_MT_GMM_AUTH_CIPH_REJ:
1086 case GSM48_MT_GMM_AUTH_CIPH_FAIL:
1087 case GSM48_MT_GMM_ID_REQ:
1088 case GSM48_MT_GMM_ID_RESP:
1089 case GSM48_MT_GMM_RA_UPD_REQ:
1090 case GSM48_MT_GMM_RA_UPD_REJ:
1091 return false;
1092 default:
1093 return true;
1094 }
1095}
1096
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001097/* Convert MCC + MNC to BCD representation, legacy implementation.
1098 * Instead use osmo_plmn_to_bcd(), which is also capable of converting
1099 * 3-digit MNC that have leading zeros. For parameters, also see there. */
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +01001100void gsm48_mcc_mnc_to_bcd(uint8_t *bcd_dst, uint16_t mcc, uint16_t mnc)
Harald Welte61e2bfc2010-03-04 10:53:03 +01001101{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001102 const struct osmo_plmn_id plmn = {
1103 .mcc = mcc,
1104 .mnc = mnc,
1105 .mnc_3_digits = false,
1106 };
1107 osmo_plmn_to_bcd(bcd_dst, &plmn);
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +01001108}
Harald Welte61e2bfc2010-03-04 10:53:03 +01001109
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001110/* Convert given 3-byte BCD buffer to integers, legacy implementation.
1111 * Instead use osmo_plmn_from_bcd(), which is also capable of converting
1112 * 3-digit MNC that have leading zeros. For parameters, also see there. */
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +01001113void gsm48_mcc_mnc_from_bcd(uint8_t *bcd_src, uint16_t *mcc, uint16_t *mnc)
1114{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001115 struct osmo_plmn_id plmn;
1116 osmo_plmn_from_bcd(bcd_src, &plmn);
1117 *mcc = plmn.mcc;
1118 *mnc = plmn.mnc;
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +01001119}
1120
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001121/*! Encode TS 04.08 Location Area Identifier, legacy implementation.
1122 * Instead use gsm48_generate_lai2(), which is capable of three-digit MNC with leading zeros.
1123 * \param[out] lai48 caller-provided memory for output
Harald Welte96e2a002017-06-12 21:44:18 +02001124 * \param[in] mcc Mobile Country Code
1125 * \param[in] mnc Mobile Network Code
1126 * \param[in] lac Location Area Code */
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +01001127void gsm48_generate_lai(struct gsm48_loc_area_id *lai48, uint16_t mcc,
1128 uint16_t mnc, uint16_t lac)
1129{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001130 const struct osmo_location_area_id lai = {
1131 .plmn = {
1132 .mcc = mcc,
1133 .mnc = mnc,
1134 .mnc_3_digits = false,
1135 },
1136 .lac = lac,
1137 };
1138 gsm48_generate_lai2(lai48, &lai);
Harald Welte61e2bfc2010-03-04 10:53:03 +01001139}
1140
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001141/*! Encode TS 04.08 Location Area Identifier.
1142 * \param[out] lai48 caller-provided memory for output.
1143 * \param[in] lai input of MCC-MNC-LAC. */
1144void gsm48_generate_lai2(struct gsm48_loc_area_id *lai48, const struct osmo_location_area_id *lai)
1145{
1146 osmo_plmn_to_bcd(&lai48->digits[0], &lai->plmn);
1147 lai48->lac = osmo_htons(lai->lac);
1148}
1149
1150/*! Decode TS 04.08 Location Area Identifier, legacy implementation.
1151 * Instead use gsm48_decode_lai2(), which is capable of three-digit MNC with leading zeros.
Harald Welte96e2a002017-06-12 21:44:18 +02001152 * \param[in] Location Area Identifier (encoded)
1153 * \param[out] mcc Mobile Country Code
1154 * \param[out] mnc Mobile Network Code
1155 * \param[out] lac Location Area Code
1156 * \returns 0
1157 *
1158 * Attention: this function returns true integers, not hex! */
Harald Welte774a9de2012-07-13 21:35:13 +02001159int gsm48_decode_lai(struct gsm48_loc_area_id *lai, uint16_t *mcc,
1160 uint16_t *mnc, uint16_t *lac)
1161{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001162 struct osmo_location_area_id decoded;
1163 gsm48_decode_lai2(lai, &decoded);
1164 *mcc = decoded.plmn.mcc;
1165 *mnc = decoded.plmn.mnc;
1166 *lac = decoded.lac;
Harald Welte774a9de2012-07-13 21:35:13 +02001167 return 0;
1168}
1169
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001170/*! Decode TS 04.08 Location Area Identifier.
1171 * \param[in] Location Area Identifier (encoded).
1172 * \param[out] decoded Target buffer to write decoded values of MCC-MNC-LAC.
1173 *
1174 * Attention: this function returns true integers, not hex! */
1175void gsm48_decode_lai2(const struct gsm48_loc_area_id *lai, struct osmo_location_area_id *decoded)
1176{
1177 osmo_plmn_from_bcd(&lai->digits[0], &decoded->plmn);
1178 decoded->lac = osmo_ntohs(lai->lac);
1179}
1180
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001181/*! Set DTX mode in Cell Options IE (3GPP TS 44.018)
Maxfe65fa72016-05-10 17:17:05 +02001182 * \param[in] op Cell Options structure in which DTX parameters will be set
1183 * \param[in] full Mode for full-rate channels
1184 * \param[in] half Mode for half-rate channels
1185 * \param[in] is_bcch Indicates if we should use 10.5.2.3.1 instead of
1186 * 10.5.2.3a.2
1187 *
1188 * There is no space for separate DTX settings for Full and Half rate channels
1189 * in BCCH - in this case full setting is used for both and half parameter is
1190 * ignored.
1191 */
1192void gsm48_set_dtx(struct gsm48_cell_options *op, enum gsm48_dtx_mode full,
1193 enum gsm48_dtx_mode half, bool is_bcch)
1194{
1195 if (is_bcch) {
1196 switch (full) {
1197 case GSM48_DTX_MAY_BE_USED:
1198 op->dtx = 0;
1199 return;
1200 case GSM48_DTX_SHALL_BE_USED:
1201 op->dtx = 1;
1202 return;
1203 case GSM48_DTX_SHALL_NOT_BE_USED:
1204 op->dtx = 2;
1205 return;
1206 }
1207 } else {
1208 switch (full) {
1209 case GSM48_DTX_MAY_BE_USED:
1210 op->dtx = (half == GSM48_DTX_SHALL_BE_USED) ? 3 : 0;
1211 op->d = (half == GSM48_DTX_SHALL_NOT_BE_USED) ? 0 : 1;
1212 return;
1213 case GSM48_DTX_SHALL_BE_USED:
1214 op->dtx = (half == GSM48_DTX_MAY_BE_USED) ? 3 : 1;
1215 op->d = (half == GSM48_DTX_SHALL_BE_USED) ? 1 : 0;
1216 return;
1217 case GSM48_DTX_SHALL_NOT_BE_USED:
1218 op->dtx = 2;
1219 op->d = (half == GSM48_DTX_SHALL_BE_USED) ? 1 : 0;
1220 return;
1221 }
1222 }
1223}
1224
Neels Hofmeyr83025bf2020-05-26 02:45:23 +02001225/*! Deprecated, see osmo_mobile_identity instead.
1226 * Generate TS 04.08 Mobile ID from TMSI
Harald Welte96e2a002017-06-12 21:44:18 +02001227 * \param[out] buf Caller-provided output buffer (7 bytes)
1228 * \param[in] tmsi TMSI to be encoded
1229 * \returns number of byes encoded (always 7) */
Harald Welte61e2bfc2010-03-04 10:53:03 +01001230int gsm48_generate_mid_from_tmsi(uint8_t *buf, uint32_t tmsi)
1231{
Harald Weltea13fb752020-06-16 08:44:42 +02001232 uint32_t tmsi_be = osmo_htonl(tmsi);
1233
1234 buf[0] = GSM48_IE_MOBILE_ID;
1235 buf[1] = GSM48_TMSI_LEN;
1236 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
1237 memcpy(&buf[3], &tmsi_be, sizeof(tmsi_be));
1238
1239 return 7;
Harald Welte61e2bfc2010-03-04 10:53:03 +01001240}
1241
Neels Hofmeyr83025bf2020-05-26 02:45:23 +02001242/*! Deprecated, see osmo_mobile_identity instead.
1243 * Generate TS 24.008 §10.5.1.4 Mobile ID of BCD type from ASCII string
Harald Welte1c3bae12019-01-20 10:37:49 +01001244 * \param[out] buf Caller-provided output buffer of at least GSM48_MID_MAX_SIZE bytes
Maxebf14922018-02-15 11:42:11 +01001245 * \param[in] id Identity to be encoded
Harald Welte1c3bae12019-01-20 10:37:49 +01001246 * \param[in] mi_type Type of identity (e.g. GSM_MI_TYPE_IMSI, IMEI, IMEISV)
Maxebf14922018-02-15 11:42:11 +01001247 * \returns number of bytes used in \a buf */
1248uint8_t gsm48_generate_mid(uint8_t *buf, const char *id, uint8_t mi_type)
1249{
Harald Weltea13fb752020-06-16 08:44:42 +02001250 uint8_t length = strnlen(id, 16), i, off = 0, odd = (length & 1) == 1;
1251 /* maximum length == 16 (IMEISV) */
1252
1253 buf[0] = GSM48_IE_MOBILE_ID;
1254 buf[2] = osmo_char2bcd(id[0]) << 4 | (mi_type & GSM_MI_TYPE_MASK) | (odd << 3);
1255
1256 /* if the length is even we will fill half of the last octet */
1257 buf[1] = (length + (odd ? 1 : 2)) >> 1;
1258 /* buf[1] maximum = 18/2 = 9 */
1259 OSMO_ASSERT(buf[1] <= 9);
1260
1261 for (i = 1; i < buf[1]; ++i) {
1262 uint8_t upper, lower = osmo_char2bcd(id[++off]);
1263 if (!odd && off + 1 == length)
1264 upper = 0x0f;
1265 else
1266 upper = osmo_char2bcd(id[++off]) & 0x0f;
1267
1268 buf[2 + i] = (upper << 4) | lower;
Maxebf14922018-02-15 11:42:11 +01001269 }
Harald Weltea13fb752020-06-16 08:44:42 +02001270
1271 /* maximum return value: 2 + 9 = 11 */
1272 return 2 + buf[1];
Maxebf14922018-02-15 11:42:11 +01001273}
1274
Neels Hofmeyr83025bf2020-05-26 02:45:23 +02001275/*! Deprecated, see osmo_mobile_identity instead.
1276 * Generate TS 04.08 Mobile ID from IMSI
Harald Welte96e2a002017-06-12 21:44:18 +02001277 * \param[out] buf Caller-provided output buffer
1278 * \param[in] imsi IMSI to be encoded
1279 * \returns number of bytes used in \a buf */
Harald Welte61e2bfc2010-03-04 10:53:03 +01001280int gsm48_generate_mid_from_imsi(uint8_t *buf, const char *imsi)
1281{
Harald Weltea13fb752020-06-16 08:44:42 +02001282 return gsm48_generate_mid(buf, imsi, GSM_MI_TYPE_IMSI);
Harald Welte61e2bfc2010-03-04 10:53:03 +01001283}
Harald Welte9bb553e2010-03-28 18:14:50 +08001284
Neels Hofmeyr83025bf2020-05-26 02:45:23 +02001285/*! Deprecated, see osmo_mobile_identity instead.
1286 * Convert TS 04.08 Mobile Identity (10.5.1.4) to string.
Neels Hofmeyr627e0112018-12-05 21:32:40 +01001287 * This function does not validate the Mobile Identity digits, i.e. digits > 9 are returned as 'A'-'F'.
Harald Welte96e2a002017-06-12 21:44:18 +02001288 * \param[out] string Caller-provided buffer for output
1289 * \param[in] str_len Length of \a string in bytes
1290 * \param[in] mi Mobile Identity to be stringified
1291 * \param[in] mi_len Length of \a mi in bytes
Neels Hofmeyr512bf022020-05-25 23:05:04 +02001292 * \returns Return <= 0 on error, > 0 on success.
1293 * WARNING: the return value of this function is not well implemented.
Neels Hofmeyr6aa20ee2018-12-06 00:40:37 +01001294 * Depending on the MI type and amount of output buffer, this may return
1295 * the nr of written bytes, or the written strlen(), or the snprintf()
Neels Hofmeyr512bf022020-05-25 23:05:04 +02001296 * style strlen()-if-the-buffer-were-large-enough.
1297 */
Vadim Yanitskiy0d8da792019-07-25 19:12:23 +07001298int gsm48_mi_to_string(char *string, int str_len, const uint8_t *mi, int mi_len)
Harald Welte9bb553e2010-03-28 18:14:50 +08001299{
Neels Hofmeyr627e0112018-12-05 21:32:40 +01001300 int rc;
Harald Welte163d0ea2010-04-09 07:57:40 +02001301 uint8_t mi_type;
Harald Welte163d0ea2010-04-09 07:57:40 +02001302 uint32_t tmsi;
Harald Welte9bb553e2010-03-28 18:14:50 +08001303
Neels Hofmeyr6adffb92018-12-05 23:30:31 +01001304 mi_type = (mi && mi_len) ? (mi[0] & GSM_MI_TYPE_MASK) : GSM_MI_TYPE_NONE;
Harald Welte9bb553e2010-03-28 18:14:50 +08001305
1306 switch (mi_type) {
Harald Welte9bb553e2010-03-28 18:14:50 +08001307 case GSM_MI_TYPE_TMSI:
Holger Freyther45abec22016-05-20 19:21:27 +00001308 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
Harald Welte9bb553e2010-03-28 18:14:50 +08001309 if (mi_len == GSM48_TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
Harald Welte95871da2017-05-15 12:11:36 +02001310 tmsi = osmo_load32be(&mi[1]);
Pau Espin Pedrol45735022017-06-18 14:05:24 +02001311 return snprintf(string, str_len, "%"PRIu32, tmsi);
Harald Welte9bb553e2010-03-28 18:14:50 +08001312 }
1313 break;
1314 case GSM_MI_TYPE_IMSI:
1315 case GSM_MI_TYPE_IMEI:
1316 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr627e0112018-12-05 21:32:40 +01001317 rc = osmo_bcd2str(string, str_len, mi,
1318 1, mi_len * 2 - ((mi[0] & GSM_MI_ODD) ? 0 : 1), true);
1319 /* osmo_bcd2str() returns snprintf style strlen(), this returns bytes written. */
1320 if (rc < 0)
1321 return 0;
1322 else if (rc < str_len)
1323 return rc + 1;
1324 else
1325 return strlen(string) + 1;
Neels Hofmeyrea2a0ab2018-12-05 23:30:08 +01001326
Harald Welte9bb553e2010-03-28 18:14:50 +08001327 default:
1328 break;
1329 }
Harald Welte9bb553e2010-03-28 18:14:50 +08001330
Neels Hofmeyrea2a0ab2018-12-05 23:30:08 +01001331 if (str_len < 1)
1332 return 0;
1333 *string = '\0';
1334 return 1;
Harald Welte9bb553e2010-03-28 18:14:50 +08001335}
Harald Weltea1c4f762010-05-01 11:59:42 +02001336
Neels Hofmeyr8d423942023-06-08 00:35:41 +02001337/*! Decode to struct osmo_routing_area_id from a 3GPP TS 24.008 § 10.5.5.15 Routing area identification.
1338 * \param[out] dst Store the decoded result here.
1339 * \param[in] ra_data The start of a Routing Area ID in encoded form, to be decoded.
1340 * \param[in] ra_data_len Buffer size available to read from at *ra_data.
1341 * \return the number of decoded bytes on success, or negative on error (if the input buffer is too small).
1342 */
1343int osmo_routing_area_id_decode(struct osmo_routing_area_id *dst, const uint8_t *ra_data, size_t ra_data_len)
1344{
1345 const struct gsm48_ra_id *ra_id;
1346 if (ra_data_len < sizeof(*ra_id))
1347 return -ENOSPC;
1348
1349 gsm48_decode_lai2((void *)ra_data, &dst->lac);
1350
1351 ra_id = (void *)ra_data;
1352 dst->rac = ra_id->rac;
1353
1354 return sizeof(*ra_id);
1355}
1356
1357/*! Encode struct osmo_routing_area_id to a 3GPP TS 24.008 § 10.5.5.15 Routing area identification: write to a buffer.
1358 * \param[out] buf Return buffer for encoded Mobile Identity.
1359 * \param[in] buflen sizeof(buf).
1360 * \param[in] src RA to encode.
1361 * \return Amount of bytes written to buf, or negative on error.
1362 */
1363int osmo_routing_area_id_encode_buf(uint8_t *buf, size_t buflen, const struct osmo_routing_area_id *src)
1364{
1365 struct gsm48_ra_id *ra_id;
1366 if (buflen < sizeof(*ra_id))
1367 return -ENOSPC;
1368
1369 gsm48_generate_lai2((void *)buf, &src->lac);
1370
1371 ra_id = (void *)buf;
1372 ra_id->rac = src->rac;
1373
1374 return sizeof(*ra_id);
1375}
1376
1377/*! Encode struct osmo_routing_area_id to a 3GPP TS 24.008 § 10.5.5.15 Routing area identification: append to msgb.
1378 * To succeed, the msgb must have tailroom >= sizeof(struct gsm48_ra_id).
1379 * \param[out] msg Append to this msgb.
1380 * \param[in] src Encode this Routing Area ID.
1381 * \return Number of bytes appended to msgb, or negative on error.
1382 */
1383int osmo_routing_area_id_encode_msgb(struct msgb *msg, const struct osmo_routing_area_id *src)
1384{
1385 int rc = osmo_routing_area_id_encode_buf(msg->tail, msgb_tailroom(msg), src);
1386 if (rc <= 0)
1387 return rc;
1388 msgb_put(msg, rc);
1389 return rc;
1390}
1391
1392/*! Parse TS 04.08 Routing Area Identifier.
1393 * Preferably use osmo_routing_area_id_decode() instead: struct osmo_routing_area_id is better integrated with other API
1394 * like osmo_plmn_cmp().
Harald Welte96e2a002017-06-12 21:44:18 +02001395 * \param[out] Caller-provided memory for decoded RA ID
1396 * \param[in] buf Input buffer pointing to RAI IE value */
Harald Weltea1c4f762010-05-01 11:59:42 +02001397void gsm48_parse_ra(struct gprs_ra_id *raid, const uint8_t *buf)
1398{
1399 raid->mcc = (buf[0] & 0xf) * 100;
1400 raid->mcc += (buf[0] >> 4) * 10;
1401 raid->mcc += (buf[1] & 0xf) * 1;
1402
1403 /* I wonder who came up with the stupidity of encoding the MNC
1404 * differently depending on how many digits its decimal number has! */
1405 if ((buf[1] >> 4) == 0xf) {
1406 raid->mnc = (buf[2] & 0xf) * 10;
1407 raid->mnc += (buf[2] >> 4) * 1;
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001408 raid->mnc_3_digits = false;
Harald Weltea1c4f762010-05-01 11:59:42 +02001409 } else {
1410 raid->mnc = (buf[2] & 0xf) * 100;
1411 raid->mnc += (buf[2] >> 4) * 10;
1412 raid->mnc += (buf[1] >> 4) * 1;
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001413 raid->mnc_3_digits = true;
Harald Weltea1c4f762010-05-01 11:59:42 +02001414 }
1415
Harald Welte95871da2017-05-15 12:11:36 +02001416 raid->lac = osmo_load16be(buf + 3);
Harald Weltea1c4f762010-05-01 11:59:42 +02001417 raid->rac = buf[5];
1418}
Harald Welte35a93942010-05-01 14:25:22 +02001419
Maxf1ad60e2018-01-05 14:19:33 +01001420/*! Encode a 3GPP TS 24.008 § 10.5.5.15 Routing area identification
1421 * \param[out] out Caller-provided packed struct
1422 * \param[in] raid Routing Area ID to be encoded
1423 */
1424void gsm48_encode_ra(struct gsm48_ra_id *out, const struct gprs_ra_id *raid)
1425{
1426 out->lac = osmo_htons(raid->lac);
1427 out->rac = raid->rac;
1428
1429 out->digits[0] = ((raid->mcc / 100) % 10) | (((raid->mcc / 10) % 10) << 4);
1430 out->digits[1] = raid->mcc % 10;
1431
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001432 if (raid->mnc < 100 && !raid->mnc_3_digits) {
Maxf1ad60e2018-01-05 14:19:33 +01001433 out->digits[1] |= 0xf0;
1434 out->digits[2] = ((raid->mnc / 10) % 10) | ((raid->mnc % 10) << 4);
1435 } else {
1436 out->digits[1] |= (raid->mnc % 10) << 4;
1437 out->digits[2] = ((raid->mnc / 100) % 10) | (((raid->mnc / 10) % 10) << 4);
1438 }
1439}
1440
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001441/*! Encode a TS 04.08 Routing Area Identifier
Harald Welte96e2a002017-06-12 21:44:18 +02001442 * \param[out] buf Caller-provided output buffer of 6 bytes
1443 * \param[in] raid Routing Area ID to be encoded
1444 * \returns number of bytes used in \a buf */
Harald Welte35a93942010-05-01 14:25:22 +02001445int gsm48_construct_ra(uint8_t *buf, const struct gprs_ra_id *raid)
1446{
Maxf1ad60e2018-01-05 14:19:33 +01001447 gsm48_encode_ra((struct gsm48_ra_id *)buf, raid);
Harald Welte35a93942010-05-01 14:25:22 +02001448
1449 return 6;
1450}
Harald Welte2aee7b12011-06-26 14:20:04 +02001451
Philipp Maierd11a5d52021-02-03 22:11:46 +01001452/*! Compare a TS 04.08 Routing Area Identifier
1453 * \param[in] raid1 first Routing Area ID to compare.
1454 * \param[in] raid2 second Routing Area ID to compare.
1455 * \returns true if raid1 and raid2 match, false otherwise. */
1456bool gsm48_ra_equal(const struct gprs_ra_id *raid1, const struct gprs_ra_id *raid2)
1457{
1458 if (raid1->mcc != raid2->mcc)
1459 return false;
1460 if (raid1->mnc != raid2->mnc)
1461 return false;
1462 if (raid1->mnc_3_digits != raid2->mnc_3_digits)
1463 return false;
1464 if (raid1->lac != raid2->lac)
1465 return false;
1466 if (raid1->rac != raid2->rac)
1467 return false;
1468 return true;
1469}
1470
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001471/*! Determine number of paging sub-channels
Harald Welte96e2a002017-06-12 21:44:18 +02001472 * \param[in] chan_desc Control Channel Description
1473 * \returns number of paging sub-channels
1474 *
1475 * Uses From Table 10.5.33 of GSM 04.08 to determine the number of
1476 * paging sub-channels in the given control channel configuration
1477 */
Pau Espin Pedrol6ca0a432022-11-24 17:09:03 +01001478int gsm48_number_of_paging_subchannels(const struct gsm48_control_channel_descr *chan_desc)
Harald Welte2aee7b12011-06-26 14:20:04 +02001479{
Harald Welte94df39e2011-06-26 14:33:57 +02001480 unsigned int n_pag_blocks = gsm0502_get_n_pag_blocks(chan_desc);
1481
1482 if (chan_desc->ccch_conf == RSL_BCCH_CCCH_CONF_1_C)
1483 return OSMO_MAX(1, n_pag_blocks) * (chan_desc->bs_pa_mfrms + 2);
1484 else
1485 return n_pag_blocks * (chan_desc->bs_pa_mfrms + 2);
Harald Welte2aee7b12011-06-26 14:20:04 +02001486}
Neels Hofmeyrdbd994c2017-03-09 23:07:02 +01001487
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001488/*! TS 04.08 Protocol Descriptor names */
Neels Hofmeyrdbd994c2017-03-09 23:07:02 +01001489const struct value_string gsm48_pdisc_names[] = {
Harald Welteea0bc962018-01-24 16:17:45 +01001490 { GSM48_PDISC_GROUP_CC, "VGCC" },
1491 { GSM48_PDISC_BCAST_CC, "VBCC" },
1492 { GSM48_PDISC_PDSS1, "PDSS1" },
1493 { GSM48_PDISC_CC, "CC" },
1494 { GSM48_PDISC_PDSS2, "PDSS2" },
1495 { GSM48_PDISC_MM, "MM" },
1496 { GSM48_PDISC_RR, "RR" },
1497 { GSM48_PDISC_MM_GPRS, "GMM" },
1498 { GSM48_PDISC_SMS, "SMS" },
1499 { GSM48_PDISC_SM_GPRS, "SM" },
1500 { GSM48_PDISC_NC_SS, "NCSS" },
1501 { GSM48_PDISC_LOC, "LCS" },
1502 { GSM48_PDISC_EXTEND, "EXTD" },
1503 { GSM48_PDISC_MASK, "MASK" },
Neels Hofmeyrdbd994c2017-03-09 23:07:02 +01001504 { 0, NULL }
1505};
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001506
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001507/*! TS 04.08 RR Message Type names */
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001508const struct value_string gsm48_rr_msgtype_names[] = {
1509 OSMO_VALUE_STRING(GSM48_MT_RR_INIT_REQ),
1510 OSMO_VALUE_STRING(GSM48_MT_RR_ADD_ASS),
1511 OSMO_VALUE_STRING(GSM48_MT_RR_IMM_ASS),
1512 OSMO_VALUE_STRING(GSM48_MT_RR_IMM_ASS_EXT),
1513 OSMO_VALUE_STRING(GSM48_MT_RR_IMM_ASS_REJ),
1514 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_ASS_FAIL),
1515 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_REJECT),
1516 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_REQUEST),
1517 OSMO_VALUE_STRING(GSM48_MT_RR_PACKET_ASS),
1518
1519 OSMO_VALUE_STRING(GSM48_MT_RR_CIPH_M_CMD),
1520 OSMO_VALUE_STRING(GSM48_MT_RR_CIPH_M_COMPL),
1521
1522 OSMO_VALUE_STRING(GSM48_MT_RR_CFG_CHG_CMD),
1523 OSMO_VALUE_STRING(GSM48_MT_RR_CFG_CHG_ACK),
1524 OSMO_VALUE_STRING(GSM48_MT_RR_CFG_CHG_REJ),
1525
1526 OSMO_VALUE_STRING(GSM48_MT_RR_ASS_CMD),
1527 OSMO_VALUE_STRING(GSM48_MT_RR_ASS_COMPL),
1528 OSMO_VALUE_STRING(GSM48_MT_RR_ASS_FAIL),
1529 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_CMD),
1530 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_COMPL),
1531 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_FAIL),
1532 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_INFO),
1533 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_INFO),
1534 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_ASS_CMD),
1535
1536 OSMO_VALUE_STRING(GSM48_MT_RR_CELL_CHG_ORDER),
1537 OSMO_VALUE_STRING(GSM48_MT_RR_PDCH_ASS_CMD),
1538
1539 OSMO_VALUE_STRING(GSM48_MT_RR_CHAN_REL),
1540 OSMO_VALUE_STRING(GSM48_MT_RR_PART_REL),
1541 OSMO_VALUE_STRING(GSM48_MT_RR_PART_REL_COMP),
1542
1543 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_REQ_1),
1544 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_REQ_2),
1545 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_REQ_3),
1546 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_RESP),
1547 OSMO_VALUE_STRING(GSM48_MT_RR_NOTIF_NCH),
1548 OSMO_VALUE_STRING(GSM48_MT_RR_NOTIF_FACCH),
1549 OSMO_VALUE_STRING(GSM48_MT_RR_NOTIF_RESP),
1550 OSMO_VALUE_STRING(GSM48_MT_RR_PACKET_NOTIF),
1551 OSMO_VALUE_STRING(GSM48_MT_RR_UTRAN_CLSM_CHG),
1552 OSMO_VALUE_STRING(GSM48_MT_RR_CDMA2K_CLSM_CHG),
1553 OSMO_VALUE_STRING(GSM48_MT_RR_IS_TO_UTRAN_HANDO),
1554 OSMO_VALUE_STRING(GSM48_MT_RR_IS_TO_CDMA2K_HANDO),
1555
1556 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_8),
1557 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_1),
1558 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2),
1559 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_3),
1560 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_4),
1561 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_5),
1562 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_6),
1563 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_7),
1564
1565 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2bis),
1566 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2ter),
1567 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2quater),
1568 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_5bis),
1569 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_5ter),
1570 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_9),
1571 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_13),
1572
1573 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_16),
1574 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_17),
1575
1576 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_18),
1577 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_19),
1578 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_20),
1579
1580 OSMO_VALUE_STRING(GSM48_MT_RR_CHAN_MODE_MODIF),
1581 OSMO_VALUE_STRING(GSM48_MT_RR_STATUS),
1582 OSMO_VALUE_STRING(GSM48_MT_RR_CHAN_MODE_MODIF_ACK),
1583 OSMO_VALUE_STRING(GSM48_MT_RR_FREQ_REDEF),
1584 OSMO_VALUE_STRING(GSM48_MT_RR_MEAS_REP),
1585 OSMO_VALUE_STRING(GSM48_MT_RR_CLSM_CHG),
1586 OSMO_VALUE_STRING(GSM48_MT_RR_CLSM_ENQ),
1587 OSMO_VALUE_STRING(GSM48_MT_RR_EXT_MEAS_REP),
1588 OSMO_VALUE_STRING(GSM48_MT_RR_EXT_MEAS_REP_ORD),
1589 OSMO_VALUE_STRING(GSM48_MT_RR_GPRS_SUSP_REQ),
1590 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_INFO),
1591
1592 OSMO_VALUE_STRING(GSM48_MT_RR_VGCS_UPL_GRANT),
1593 OSMO_VALUE_STRING(GSM48_MT_RR_UPLINK_RELEASE),
1594 OSMO_VALUE_STRING(GSM48_MT_RR_UPLINK_FREE),
1595 OSMO_VALUE_STRING(GSM48_MT_RR_UPLINK_BUSY),
1596 OSMO_VALUE_STRING(GSM48_MT_RR_TALKER_IND),
1597 { 0, NULL }
1598};
1599
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001600/*! TS 04.08 MM Message Type names */
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001601const struct value_string gsm48_mm_msgtype_names[] = {
1602 OSMO_VALUE_STRING(GSM48_MT_MM_IMSI_DETACH_IND),
1603 OSMO_VALUE_STRING(GSM48_MT_MM_LOC_UPD_ACCEPT),
1604 OSMO_VALUE_STRING(GSM48_MT_MM_LOC_UPD_REJECT),
1605 OSMO_VALUE_STRING(GSM48_MT_MM_LOC_UPD_REQUEST),
1606
1607 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_REJ),
1608 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_REQ),
1609 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_RESP),
1610 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_FAIL),
1611 OSMO_VALUE_STRING(GSM48_MT_MM_ID_REQ),
1612 OSMO_VALUE_STRING(GSM48_MT_MM_ID_RESP),
1613 OSMO_VALUE_STRING(GSM48_MT_MM_TMSI_REALL_CMD),
1614 OSMO_VALUE_STRING(GSM48_MT_MM_TMSI_REALL_COMPL),
1615
1616 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_ACC),
1617 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_REJ),
1618 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_ABORT),
1619 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_REQ),
1620 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_PROMPT),
1621 OSMO_VALUE_STRING(GSM48_MT_MM_CM_REEST_REQ),
1622 OSMO_VALUE_STRING(GSM48_MT_MM_ABORT),
1623
1624 OSMO_VALUE_STRING(GSM48_MT_MM_NULL),
1625 OSMO_VALUE_STRING(GSM48_MT_MM_STATUS),
1626 OSMO_VALUE_STRING(GSM48_MT_MM_INFO),
1627 { 0, NULL }
1628};
1629
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001630/*! TS 04.08 CC Message Type names */
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001631const struct value_string gsm48_cc_msgtype_names[] = {
1632 OSMO_VALUE_STRING(GSM48_MT_CC_ALERTING),
1633 OSMO_VALUE_STRING(GSM48_MT_CC_CALL_CONF),
1634 OSMO_VALUE_STRING(GSM48_MT_CC_CALL_PROC),
1635 OSMO_VALUE_STRING(GSM48_MT_CC_CONNECT),
1636 OSMO_VALUE_STRING(GSM48_MT_CC_CONNECT_ACK),
1637 OSMO_VALUE_STRING(GSM48_MT_CC_EMERG_SETUP),
1638 OSMO_VALUE_STRING(GSM48_MT_CC_PROGRESS),
1639 OSMO_VALUE_STRING(GSM48_MT_CC_ESTAB),
1640 OSMO_VALUE_STRING(GSM48_MT_CC_ESTAB_CONF),
1641 OSMO_VALUE_STRING(GSM48_MT_CC_RECALL),
1642 OSMO_VALUE_STRING(GSM48_MT_CC_START_CC),
1643 OSMO_VALUE_STRING(GSM48_MT_CC_SETUP),
1644
1645 OSMO_VALUE_STRING(GSM48_MT_CC_MODIFY),
1646 OSMO_VALUE_STRING(GSM48_MT_CC_MODIFY_COMPL),
1647 OSMO_VALUE_STRING(GSM48_MT_CC_MODIFY_REJECT),
1648 OSMO_VALUE_STRING(GSM48_MT_CC_USER_INFO),
1649 OSMO_VALUE_STRING(GSM48_MT_CC_HOLD),
1650 OSMO_VALUE_STRING(GSM48_MT_CC_HOLD_ACK),
1651 OSMO_VALUE_STRING(GSM48_MT_CC_HOLD_REJ),
1652 OSMO_VALUE_STRING(GSM48_MT_CC_RETR),
1653 OSMO_VALUE_STRING(GSM48_MT_CC_RETR_ACK),
1654 OSMO_VALUE_STRING(GSM48_MT_CC_RETR_REJ),
1655
1656 OSMO_VALUE_STRING(GSM48_MT_CC_DISCONNECT),
1657 OSMO_VALUE_STRING(GSM48_MT_CC_RELEASE),
1658 OSMO_VALUE_STRING(GSM48_MT_CC_RELEASE_COMPL),
1659
1660 OSMO_VALUE_STRING(GSM48_MT_CC_CONG_CTRL),
1661 OSMO_VALUE_STRING(GSM48_MT_CC_NOTIFY),
1662 OSMO_VALUE_STRING(GSM48_MT_CC_STATUS),
1663 OSMO_VALUE_STRING(GSM48_MT_CC_STATUS_ENQ),
1664 OSMO_VALUE_STRING(GSM48_MT_CC_START_DTMF),
1665 OSMO_VALUE_STRING(GSM48_MT_CC_STOP_DTMF),
1666 OSMO_VALUE_STRING(GSM48_MT_CC_STOP_DTMF_ACK),
1667 OSMO_VALUE_STRING(GSM48_MT_CC_START_DTMF_ACK),
1668 OSMO_VALUE_STRING(GSM48_MT_CC_START_DTMF_REJ),
1669 OSMO_VALUE_STRING(GSM48_MT_CC_FACILITY),
1670 { 0, NULL }
1671};
1672
Keith3cdaa8d2018-08-31 20:09:18 +02001673/*! TS 04.08 10.5..4.11 Call Control Cause Values */
1674const struct value_string gsm48_cc_cause_names[] = {
1675 { GSM48_CC_CAUSE_UNASSIGNED_NR, "UNASSIGNED_NR" },
1676 { GSM48_CC_CAUSE_NO_ROUTE, "NO_ROUTE" },
1677 { GSM48_CC_CAUSE_CHAN_UNACCEPT, "CHAN_UNACCEPT" },
1678 { GSM48_CC_CAUSE_OP_DET_BARRING, "OP_DET_BARRING" },
1679 { GSM48_CC_CAUSE_NORM_CALL_CLEAR, "NORM_CALL_CLEAR" },
1680 { GSM48_CC_CAUSE_USER_BUSY, "USER_BUSY" },
1681 { GSM48_CC_CAUSE_USER_NOTRESPOND, "USER_NOTRESPOND" },
1682 { GSM48_CC_CAUSE_USER_ALERTING_NA, "USER_ALERTING_NA" },
1683 { GSM48_CC_CAUSE_CALL_REJECTED, "CALL_REJECTED" },
1684 { GSM48_CC_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED" },
1685 { GSM48_CC_CAUSE_PRE_EMPTION, "PRE_EMPTION" },
1686 { GSM48_CC_CAUSE_NONSE_USER_CLR, "NONSE_USER_CLR" },
1687 { GSM48_CC_CAUSE_DEST_OOO, "DEST_OOO" },
1688 { GSM48_CC_CAUSE_INV_NR_FORMAT, "INV_NR_FORMAT" },
1689 { GSM48_CC_CAUSE_FACILITY_REJ, "FACILITY_REJ" },
1690 { GSM48_CC_CAUSE_RESP_STATUS_INQ, "RESP_STATUS_INQ" },
1691 { GSM48_CC_CAUSE_NORMAL_UNSPEC, "NORMAL_UNSPEC" },
1692 { GSM48_CC_CAUSE_NO_CIRCUIT_CHAN, "NO_CIRCUIT_CHAN" },
1693 { GSM48_CC_CAUSE_NETWORK_OOO, "NETWORK_OOO" },
1694 { GSM48_CC_CAUSE_TEMP_FAILURE, "TEMP_FAILURE" },
1695 { GSM48_CC_CAUSE_SWITCH_CONG, "SWITCH_CONG" },
1696 { GSM48_CC_CAUSE_ACC_INF_DISCARD, "ACC_INF_DISCARD" },
1697 { GSM48_CC_CAUSE_REQ_CHAN_UNAVAIL, "REQ_CHAN_UNAVAIL" },
1698 { GSM48_CC_CAUSE_RESOURCE_UNAVAIL, "RESOURCE_UNAVAIL" },
1699 { GSM48_CC_CAUSE_QOS_UNAVAIL, "QOS_UNAVAIL" },
1700 { GSM48_CC_CAUSE_REQ_FAC_NOT_SUBSC, "REQ_FAC_NOT_SUBSC" },
1701 { GSM48_CC_CAUSE_INC_BARRED_CUG, "INC_BARRED_CUG" },
1702 { GSM48_CC_CAUSE_BEARER_CAP_UNAUTH, "BEARER_CAP_UNAUTH" },
1703 { GSM48_CC_CAUSE_BEARER_CA_UNAVAIL, "BEARER_CA_UNAVAIL" },
1704 { GSM48_CC_CAUSE_SERV_OPT_UNAVAIL, "SERV_OPT_UNAVAIL" },
1705 { GSM48_CC_CAUSE_BEARERSERV_UNIMPL, "BEARERSERV_UNIMPL" },
1706 { GSM48_CC_CAUSE_ACM_GE_ACM_MAX, "ACM_GE_ACM_MAX" },
1707 { GSM48_CC_CAUSE_REQ_FAC_NOTIMPL, "REQ_FAC_NOTIMPL" },
1708 { GSM48_CC_CAUSE_RESTR_BCAP_AVAIL, "RESTR_BCAP_AVAIL" },
1709 { GSM48_CC_CAUSE_SERV_OPT_UNIMPL, "SERV_OPT_UNIMPL" },
1710 { GSM48_CC_CAUSE_INVAL_TRANS_ID, "INVAL_TRANS_ID" },
1711 { GSM48_CC_CAUSE_USER_NOT_IN_CUG, "USER_NOT_IN_CUG" },
1712 { GSM48_CC_CAUSE_INCOMPAT_DEST, "INCOMPAT_DEST" },
1713 { GSM48_CC_CAUSE_INVAL_TRANS_NET, "INVAL_TRANS_NET" },
1714 { GSM48_CC_CAUSE_SEMANTIC_INCORR, "SEMANTIC_INCORR" },
1715 { GSM48_CC_CAUSE_INVAL_MAND_INF, "INVAL_MAND_INF" },
1716 { GSM48_CC_CAUSE_MSGTYPE_NOTEXIST, "MSGTYPE_NOTEXIST" },
1717 { GSM48_CC_CAUSE_MSGTYPE_INCOMPAT, "MSGTYPE_INCOMPAT" },
1718 { GSM48_CC_CAUSE_IE_NOTEXIST, "IE_NOTEXIST" },
1719 { GSM48_CC_CAUSE_COND_IE_ERR, "COND_IE_ERR" },
1720 { GSM48_CC_CAUSE_MSG_INCOMP_STATE, "MSG_INCOMP_STATE" },
1721 { GSM48_CC_CAUSE_RECOVERY_TIMER, "RECOVERY_TIMER" },
1722 { GSM48_CC_CAUSE_PROTO_ERR, "PROTO_ERR" },
1723 { GSM48_CC_CAUSE_INTERWORKING, "INTERWORKING" },
1724 { 0 , NULL }
1725};
1726
Vadim Yanitskiy8d41d722018-04-17 11:17:28 +07001727/*! TS 04.80, section 3.4 Messages for supplementary services control */
1728const struct value_string gsm48_nc_ss_msgtype_names[] = {
1729 OSMO_VALUE_STRING(GSM0480_MTYPE_RELEASE_COMPLETE),
1730 OSMO_VALUE_STRING(GSM0480_MTYPE_FACILITY),
1731 OSMO_VALUE_STRING(GSM0480_MTYPE_REGISTER),
1732 { 0, NULL }
1733};
1734
Harald Welte4a62eda2019-03-18 18:27:00 +01001735/*! Compose a string naming the message type for given protocol, in a caller-provided buffer.
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001736 * If the message type string is known, return the message type name, otherwise
1737 * return "<protocol discriminator name>:<message type in hex>".
Harald Welte4a62eda2019-03-18 18:27:00 +01001738 * \param[out] buf caller-allcated output string buffer
1739 * \param[in] buf_len size of buf in bytes
Harald Welte96e2a002017-06-12 21:44:18 +02001740 * \param[in] pdisc protocol discriminator like GSM48_PDISC_MM
1741 * \param[in] msg_type message type like GSM48_MT_MM_LOC_UPD_REQUEST
Harald Welte4a62eda2019-03-18 18:27:00 +01001742 * \returns buf
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001743 */
Harald Welte4a62eda2019-03-18 18:27:00 +01001744char *gsm48_pdisc_msgtype_name_buf(char *buf, size_t buf_len, uint8_t pdisc, uint8_t msg_type)
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001745{
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001746 const struct value_string *msgt_names;
1747
1748 switch (pdisc) {
1749 case GSM48_PDISC_RR:
1750 msgt_names = gsm48_rr_msgtype_names;
1751 break;
1752 case GSM48_PDISC_MM:
1753 msgt_names = gsm48_mm_msgtype_names;
1754 break;
1755 case GSM48_PDISC_CC:
1756 msgt_names = gsm48_cc_msgtype_names;
1757 break;
Vadim Yanitskiy8d41d722018-04-17 11:17:28 +07001758 case GSM48_PDISC_NC_SS:
1759 msgt_names = gsm48_nc_ss_msgtype_names;
1760 break;
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001761 default:
1762 msgt_names = NULL;
1763 break;
1764 }
1765
1766 if (msgt_names)
Harald Welte4a62eda2019-03-18 18:27:00 +01001767 snprintf(buf, buf_len, "%s", get_value_string(msgt_names, msg_type));
1768 else
1769 snprintf(buf, buf_len, "%s:0x%02x", gsm48_pdisc_name(pdisc), msg_type);
1770 return buf;
1771}
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001772
Harald Welte4a62eda2019-03-18 18:27:00 +01001773/*! Compose a string naming the message type for given protocol, in a static buffer.
1774 * If the message type string is known, return the message type name, otherwise
1775 * return "<protocol discriminator name>:<message type in hex>".
1776 * \param[in] pdisc protocol discriminator like GSM48_PDISC_MM
1777 * \param[in] msg_type message type like GSM48_MT_MM_LOC_UPD_REQUEST
1778 * \returns statically allocated string or string constant.
1779 */
1780const char *gsm48_pdisc_msgtype_name(uint8_t pdisc, uint8_t msg_type)
1781{
Harald Welte171ef822019-03-28 10:49:05 +01001782 static __thread char namebuf[64];
Harald Welte4a62eda2019-03-18 18:27:00 +01001783 return gsm48_pdisc_msgtype_name_buf(namebuf, sizeof(namebuf), pdisc, msg_type);
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001784}
Harald Welte96e2a002017-06-12 21:44:18 +02001785
Harald Welte179f3572019-03-18 18:38:47 +01001786/*! Compose a string naming the message type for given protocol, in a dynamically-allocated buffer.
1787 * If the message type string is known, return the message type name, otherwise
1788 * return "<protocol discriminator name>:<message type in hex>".
1789 * \param[in] ctx talloc context from which to allocate output buffer
1790 * \param[in] pdisc protocol discriminator like GSM48_PDISC_MM
1791 * \param[in] msg_type message type like GSM48_MT_MM_LOC_UPD_REQUEST
1792 * \returns string representation in dynamically allocated output buffer.
1793 */
1794char *gsm48_pdisc_msgtype_name_c(const void *ctx, uint8_t pdisc, uint8_t msg_type)
1795{
1796 char *namebuf = talloc_size(ctx, 64);
1797 if (!namebuf)
1798 return NULL;
1799 return gsm48_pdisc_msgtype_name_buf(namebuf, 64, pdisc, msg_type);
1800}
1801
Neels Hofmeyraead2412018-04-06 04:31:00 +02001802const struct value_string gsm48_reject_value_names[] = {
1803 { GSM48_REJECT_IMSI_UNKNOWN_IN_HLR, "IMSI_UNKNOWN_IN_HLR" },
1804 { GSM48_REJECT_ILLEGAL_MS, "ILLEGAL_MS" },
1805 { GSM48_REJECT_IMSI_UNKNOWN_IN_VLR, "IMSI_UNKNOWN_IN_VLR" },
1806 { GSM48_REJECT_IMEI_NOT_ACCEPTED, "IMEI_NOT_ACCEPTED" },
1807 { GSM48_REJECT_ILLEGAL_ME, "ILLEGAL_ME" },
1808 { GSM48_REJECT_PLMN_NOT_ALLOWED, "PLMN_NOT_ALLOWED" },
1809 { GSM48_REJECT_LOC_NOT_ALLOWED, "LOC_NOT_ALLOWED" },
1810 { GSM48_REJECT_ROAMING_NOT_ALLOWED, "ROAMING_NOT_ALLOWED" },
1811 { GSM48_REJECT_NETWORK_FAILURE, "NETWORK_FAILURE" },
1812 { GSM48_REJECT_SYNCH_FAILURE, "SYNCH_FAILURE" },
1813 { GSM48_REJECT_CONGESTION, "CONGESTION" },
1814 { GSM48_REJECT_SRV_OPT_NOT_SUPPORTED, "SRV_OPT_NOT_SUPPORTED" },
1815 { GSM48_REJECT_RQD_SRV_OPT_NOT_SUPPORTED, "RQD_SRV_OPT_NOT_SUPPORTED" },
1816 { GSM48_REJECT_SRV_OPT_TMP_OUT_OF_ORDER, "SRV_OPT_TMP_OUT_OF_ORDER" },
1817 { GSM48_REJECT_CALL_CAN_NOT_BE_IDENTIFIED, "CALL_CAN_NOT_BE_IDENTIFIED" },
1818 { GSM48_REJECT_INCORRECT_MESSAGE, "INCORRECT_MESSAGE" },
1819 { GSM48_REJECT_INVALID_MANDANTORY_INF, "INVALID_MANDANTORY_INF" },
1820 { GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED, "MSG_TYPE_NOT_IMPLEMENTED" },
1821 { GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE, "MSG_TYPE_NOT_COMPATIBLE" },
1822 { GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED, "INF_ELEME_NOT_IMPLEMENTED" },
1823 { GSM48_REJECT_CONDTIONAL_IE_ERROR, "CONDTIONAL_IE_ERROR" },
1824 { GSM48_REJECT_MSG_NOT_COMPATIBLE, "MSG_NOT_COMPATIBLE" },
1825 { GSM48_REJECT_PROTOCOL_ERROR, "PROTOCOL_ERROR" },
1826 { GSM48_REJECT_GPRS_NOT_ALLOWED, "GPRS_NOT_ALLOWED" },
1827 { GSM48_REJECT_SERVICES_NOT_ALLOWED, "SERVICES_NOT_ALLOWED" },
1828 { GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE, "MS_IDENTITY_NOT_DERVIVABLE" },
1829 { GSM48_REJECT_IMPLICITLY_DETACHED, "IMPLICITLY_DETACHED" },
1830 { GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN, "GPRS_NOT_ALLOWED_IN_PLMN" },
1831 { GSM48_REJECT_MSC_TMP_NOT_REACHABLE, "MSC_TMP_NOT_REACHABLE" },
1832 { 0, NULL }
1833};
1834
Vadim Yanitskiy30cfeeb2018-08-03 05:44:00 +07001835/*! Wrap a given \ref msg with \ref gsm48_hdr structure
1836 * \param[out] msg A message to be wrapped
1837 * \param[in] pdisc GSM TS 04.07 protocol discriminator 1/2,
1838 * sub-pdisc, trans_id or skip_ind 1/2,
1839 * see section 11.2.3.1 for details
1840 * \param[in] msg_type GSM TS 04.08 message type
1841 * @return pointer to pushed header within \ref msg
1842 */
1843struct gsm48_hdr *gsm48_push_l3hdr(struct msgb *msg,
1844 uint8_t pdisc, uint8_t msg_type)
1845{
1846 struct gsm48_hdr *gh;
1847
1848 gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
1849 gh->proto_discr = pdisc;
1850 gh->msg_type = msg_type;
1851
1852 return gh;
1853}
1854
Neels Hofmeyr92f3f5e2019-01-05 00:39:13 +01001855const struct value_string osmo_lu_type_names[] = {
1856 { GSM48_LUPD_NORMAL, "NORMAL" },
1857 { GSM48_LUPD_PERIODIC, "PERIODIC" },
1858 { GSM48_LUPD_IMSI_ATT, "IMSI-ATTACH" },
1859 { GSM48_LUPD_RESERVED, "RESERVED" },
1860 {}
1861};
1862
Neels Hofmeyrf8963f92019-01-10 23:33:32 +01001863const struct value_string osmo_cm_service_type_names[] = {
1864 { GSM48_CMSERV_MO_CALL_PACKET, "MO-Call" },
1865 { GSM48_CMSERV_EMERGENCY, "Emergency-Call" },
1866 { GSM48_CMSERV_SMS, "Short-Messaging-Service" },
1867 { GSM48_CMSERV_SUP_SERV, "Supplementary-Service" },
1868 { GSM48_CMSERV_VGCS, "Voice-Group-Call" },
1869 { GSM48_CMSERV_VBS, "Voice-Broadcast-Call" },
1870 { GSM48_CMSERV_LOC_SERV, "Location-Service" },
1871 {}
1872};
1873
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001874bool osmo_gsm48_classmark1_is_r99(const struct gsm48_classmark1 *cm1)
1875{
1876 return cm1->rev_lev >= 2;
1877}
1878
1879bool osmo_gsm48_classmark2_is_r99(const struct gsm48_classmark2 *cm2, uint8_t cm2_len)
1880{
1881 if (!cm2_len)
1882 return false;
1883 return cm2->rev_lev >= 2;
1884}
1885
1886/*! Return true if any of Classmark 1 or Classmark 2 are present and indicate R99 capability.
1887 * \param[in] cm Classmarks.
1888 * \returns True if R99 or later, false if pre-R99 or no Classmarks are present.
1889 */
1890bool osmo_gsm48_classmark_is_r99(const struct osmo_gsm48_classmark *cm)
1891{
1892 if (cm->classmark1_set)
1893 return osmo_gsm48_classmark1_is_r99(&cm->classmark1);
1894 return osmo_gsm48_classmark2_is_r99(&cm->classmark2, cm->classmark2_len);
1895}
1896
1897/*! Return a string representation of A5 cipher algorithms indicated by Classmark 1, 2 and 3.
1898 * \param[in] cm Classmarks.
1899 * \returns A statically allocated string like "cm1{a5/1=supported} cm2{0x23= A5/2 A5/3} no-cm3"
1900 */
Harald Welte4a62eda2019-03-18 18:27:00 +01001901char *osmo_gsm48_classmark_a5_name_buf(char *buf, size_t buf_len, const struct osmo_gsm48_classmark *cm)
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001902{
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001903 char cm1[42] = "no-cm1";
1904 char cm2[42] = " no-cm2";
Neels Hofmeyr9e6f5f12019-03-11 05:07:56 +01001905 char cm3[42] = " no-cm3";
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001906
1907 if (cm->classmark1_set)
1908 snprintf(cm1, sizeof(cm1), "cm1{a5/1=%s}",
1909 cm->classmark1.a5_1 ? "not-supported":"supported" /* inverted logic */);
1910
1911 if (cm->classmark2_len >= 3)
1912 snprintf(cm2, sizeof(cm2), " cm2{0x%x=%s%s}",
1913 cm->classmark2.a5_2 + (cm->classmark2.a5_3 << 1),
1914 cm->classmark2.a5_2 ? " A5/2" : "",
1915 cm->classmark2.a5_3 ? " A5/3" : "");
1916
1917 if (cm->classmark3_len >= 1)
1918 snprintf(cm3, sizeof(cm3), " cm3{0x%x=%s%s%s%s}",
1919 cm->classmark3[0],
1920 cm->classmark3[0] & (1 << 0) ? " A5/4" : "",
1921 cm->classmark3[0] & (1 << 1) ? " A5/5" : "",
1922 cm->classmark3[0] & (1 << 2) ? " A5/6" : "",
1923 cm->classmark3[0] & (1 << 3) ? " A5/7" : "");
1924
Harald Welte4a62eda2019-03-18 18:27:00 +01001925 snprintf(buf, buf_len, "%s%s%s", cm1, cm2, cm3);
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001926 return buf;
1927}
1928
Harald Welte4a62eda2019-03-18 18:27:00 +01001929/*! Return a string representation of A5 cipher algorithms indicated by Classmark 1, 2 and 3.
1930 * \param[in] cm Classmarks.
1931 * \returns A statically allocated string like "cm1{a5/1=supported} cm2{0x23= A5/2 A5/3} no-cm3"
1932 */
1933const char *osmo_gsm48_classmark_a5_name(const struct osmo_gsm48_classmark *cm)
1934{
Harald Welte171ef822019-03-28 10:49:05 +01001935 static __thread char buf[128];
Harald Welte4a62eda2019-03-18 18:27:00 +01001936 return osmo_gsm48_classmark_a5_name_buf(buf, sizeof(buf), cm);
1937}
1938
Harald Welte179f3572019-03-18 18:38:47 +01001939/*! Return a string representation of A5 cipher algorithms indicated by Classmark 1, 2 and 3.
1940 * \param[in] ctx talloc context from which to allocate output buffer
1941 * \param[in] cm Classmarks.
1942 * \returns string like "cm1{a5/1=supported} cm2{0x23= A5/2 A5/3} no-cm3" in dynamically-allocated
1943 * output buffer.
1944 */
1945char *osmo_gsm48_classmark_a5_name_c(const void *ctx, const struct osmo_gsm48_classmark *cm)
1946{
1947 char *buf = talloc_size(ctx, 128);
1948 if (!buf)
1949 return NULL;
1950 return osmo_gsm48_classmark_a5_name_buf(buf, 128, cm);
1951}
Harald Welte4a62eda2019-03-18 18:27:00 +01001952
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001953/*! Overwrite dst with the Classmark information present in src.
1954 * Add an new Classmark and overwrite in dst what src has to offer, but where src has no Classmark information, leave
1955 * dst unchanged. (For Classmark 2 and 3, dst will exactly match any non-zero Classmark length from src, hence may end
1956 * up with a shorter Classmark after this call.)
1957 * \param[out] dst The target Classmark storage to be updated.
1958 * \param[in] src The new Classmark information to read from.
1959 */
1960void osmo_gsm48_classmark_update(struct osmo_gsm48_classmark *dst, const struct osmo_gsm48_classmark *src)
1961{
1962 if (src->classmark1_set) {
1963 dst->classmark1 = src->classmark1;
1964 dst->classmark1_set = true;
1965 }
1966 if (src->classmark2_len) {
1967 dst->classmark2_len = src->classmark2_len;
1968 dst->classmark2 = src->classmark2;
1969 }
1970 if (src->classmark3_len) {
1971 dst->classmark3_len = src->classmark3_len;
1972 memcpy(dst->classmark3, src->classmark3, OSMO_MIN(sizeof(dst->classmark3), src->classmark3_len));
1973 }
1974}
1975
1976
1977/*! Determine if the given Classmark (1/2/3) value permits a given A5/n cipher.
1978 * \param[in] cm Classmarks.
1979 * \param[in] a5 The N in A5/N for which to query whether support is indicated.
1980 * \return 1 when the given A5/n is permitted, 0 when not (or a5 > 7), and negative if the respective MS Classmark is
1981 * not known, where the negative number indicates the classmark type: -2 means Classmark 2 is not available. The
1982 * idea is that when e.g. A5/3 is requested and the corresponding Classmark 3 is not available, that the caller
1983 * can react by obtaining Classmark 3 and calling again once it is available.
1984 */
1985int osmo_gsm48_classmark_supports_a5(const struct osmo_gsm48_classmark *cm, uint8_t a5)
1986{
1987 switch (a5) {
1988 case 0:
1989 /* all phones must implement A5/0, see 3GPP TS 43.020 4.9 */
1990 return 1;
1991 case 1:
1992 /* 3GPP TS 43.020 4.9 requires A5/1 to be suppored by all phones and actually states:
1993 * "The network shall not provide service to an MS which indicates that it does not
1994 * support the ciphering algorithm A5/1.". However, let's be more tolerant based
1995 * on policy here */
1996 /* See 3GPP TS 24.008 10.5.1.7 */
1997 if (!cm->classmark1_set)
1998 return -1;
1999 /* Inverted logic for this bit! */
2000 return cm->classmark1.a5_1 ? 0 : 1;
2001 case 2:
2002 /* See 3GPP TS 24.008 10.5.1.6 */
2003 if (cm->classmark2_len < 3)
2004 return -2;
2005 return cm->classmark2.a5_2 ? 1 : 0;
2006 case 3:
2007 if (cm->classmark2_len < 3)
2008 return -2;
2009 return cm->classmark2.a5_3 ? 1 : 0;
2010 case 4:
2011 case 5:
2012 case 6:
2013 case 7:
2014 /* See 3GPP TS 24.008 10.5.1.7 */
2015 if (!cm->classmark3_len)
2016 return -3;
2017 return (cm->classmark3[0] & (1 << (a5-4))) ? 1 : 0;
2018 default:
2019 return 0;
2020 }
2021}
2022
Pau Espin Pedrolb99f4ca2019-10-31 13:35:22 +01002023/*! Decode power class from Classmark1/2 RF power capability field.
2024 * \param[in] rf_power_cap The RF power capability field (3 bits).
2025 * \param[in] band the band of the arfcn from where the classmark was received
2026 * \return the MS power class on success, negative on error.
2027 */
2028int8_t osmo_gsm48_rfpowercap2powerclass(enum gsm_band band, uint8_t rf_power_cap)
2029{
2030 switch (band) {
2031 case GSM_BAND_1800:
2032 case GSM_BAND_1900:
2033 if (rf_power_cap > 2)
2034 return -1;
2035 return rf_power_cap + 1;
2036 default:
2037 if (rf_power_cap > 4)
2038 return -1;
2039 return rf_power_cap + 1;
2040 }
2041}
2042
2043
Harald Welte96e2a002017-06-12 21:44:18 +02002044/*! @} */