blob: 64b176583397b35f3fc5a8fc7596f4da5c8e5646 [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>
Andreas Eversberg72ab3b52023-06-17 16:15:56 +020045#include <osmocom/gsm/protocol/gsm_44_068.h>
Harald Welte4fb20752010-03-02 23:17:33 +010046
Harald Welte96e2a002017-06-12 21:44:18 +020047/*! \addtogroup gsm0408
48 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020049 * GSM Mobile Radion Interface L3 messages / TS 04.08
Harald Welte96e2a002017-06-12 21:44:18 +020050 */
51
Neels Hofmeyr87e45502017-06-20 00:17:59 +020052/*! TLV parser definitions for TS 04.08 CC */
Harald Welte4fb20752010-03-02 23:17:33 +010053const struct tlv_definition gsm48_att_tlvdef = {
54 .def = {
55 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
56 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
57 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
58 [GSM48_IE_UTC] = { TLV_TYPE_TV },
59 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
60 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
61
62 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
63 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
64 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
65 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
66 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
67 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
68 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
69 [GSM48_IE_NOTIFY] = { TLV_TYPE_TV },
70 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
71 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
72 [GSM48_IE_CONN_BCD] = { TLV_TYPE_TLV },
73 [GSM48_IE_CONN_SUB] = { TLV_TYPE_TLV },
74 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
75 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
76 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
77 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
78 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
79 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
80 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
81 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
82 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
83 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
84 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
85 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
86 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
87 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
88 [GSM48_IE_REPEAT_CIR] = { TLV_TYPE_T },
89 [GSM48_IE_REPEAT_SEQ] = { TLV_TYPE_T },
90 /* FIXME: more elements */
91 },
92};
93
Neels Hofmeyr87e45502017-06-20 00:17:59 +020094/*! TLV parser definitions for TS 04.08 RR */
Andreas Eversberg014cb872010-07-12 09:11:00 +020095const struct tlv_definition gsm48_rr_att_tlvdef = {
96 .def = {
97 /* NOTE: Don't add IE 17 = MOBILE_ID here, it already used. */
98 [GSM48_IE_VGCS_TARGET] = { TLV_TYPE_TLV },
99 [GSM48_IE_FRQSHORT_AFTER] = { TLV_TYPE_FIXED, 9 },
100 [GSM48_IE_MUL_RATE_CFG] = { TLV_TYPE_TLV },
101 [GSM48_IE_FREQ_L_AFTER] = { TLV_TYPE_TLV },
102 [GSM48_IE_MSLOT_DESC] = { TLV_TYPE_TLV },
103 [GSM48_IE_CHANMODE_2] = { TLV_TYPE_TV },
104 [GSM48_IE_FRQSHORT_BEFORE] = { TLV_TYPE_FIXED, 9 },
105 [GSM48_IE_CHANMODE_3] = { TLV_TYPE_TV },
106 [GSM48_IE_CHANMODE_4] = { TLV_TYPE_TV },
107 [GSM48_IE_CHANMODE_5] = { TLV_TYPE_TV },
108 [GSM48_IE_CHANMODE_6] = { TLV_TYPE_TV },
109 [GSM48_IE_CHANMODE_7] = { TLV_TYPE_TV },
110 [GSM48_IE_CHANMODE_8] = { TLV_TYPE_TV },
111 [GSM48_IE_FREQ_L_BEFORE] = { TLV_TYPE_TLV },
112 [GSM48_IE_CH_DESC_1_BEFORE] = { TLV_TYPE_FIXED, 3 },
113 [GSM48_IE_CH_DESC_2_BEFORE] = { TLV_TYPE_FIXED, 3 },
114 [GSM48_IE_F_CH_SEQ_BEFORE] = { TLV_TYPE_FIXED, 9 },
115 [GSM48_IE_CLASSMARK3] = { TLV_TYPE_TLV },
116 [GSM48_IE_MA_BEFORE] = { TLV_TYPE_TLV },
117 [GSM48_IE_RR_PACKET_UL] = { TLV_TYPE_TLV },
118 [GSM48_IE_RR_PACKET_DL] = { TLV_TYPE_TLV },
119 [GSM48_IE_CELL_CH_DESC] = { TLV_TYPE_FIXED, 16 },
120 [GSM48_IE_CHANMODE_1] = { TLV_TYPE_TV },
121 [GSM48_IE_CHDES_2_AFTER] = { TLV_TYPE_FIXED, 3 },
122 [GSM48_IE_MODE_SEC_CH] = { TLV_TYPE_TV },
123 [GSM48_IE_F_CH_SEQ_AFTER] = { TLV_TYPE_FIXED, 9 },
Neels Hofmeyr4b44ac42021-04-19 23:33:34 +0200124 [GSM48_IE_EXTENDED_TSC_SET] = { TLV_TYPE_TV },
Andreas Eversberg014cb872010-07-12 09:11:00 +0200125 [GSM48_IE_MA_AFTER] = { TLV_TYPE_TLV },
126 [GSM48_IE_BA_RANGE] = { TLV_TYPE_TLV },
127 [GSM48_IE_GROUP_CHDES] = { TLV_TYPE_TLV },
128 [GSM48_IE_BA_LIST_PREF] = { TLV_TYPE_TLV },
129 [GSM48_IE_MOB_OVSERV_DIF] = { TLV_TYPE_TLV },
130 [GSM48_IE_REALTIME_DIFF] = { TLV_TYPE_TLV },
131 [GSM48_IE_START_TIME] = { TLV_TYPE_FIXED, 2 },
132 [GSM48_IE_TIMING_ADVANCE] = { TLV_TYPE_TV },
Pau Espin Pedrol559a6ee2023-03-22 13:17:09 +0100133 [GSM48_IE_GROUP_CIP_SEQ_HO] = { TLV_TYPE_SINGLE_TV },
134 [GSM48_IE_CIP_MODE_SET_HO] = { TLV_TYPE_SINGLE_TV },
135 [GSM48_IE_GPRS_RESUMPT_HO] = { TLV_TYPE_SINGLE_TV },
136 [GSM48_IE_SYNC_IND_HO] = { TLV_TYPE_SINGLE_TV },
Andreas Eversberg014cb872010-07-12 09:11:00 +0200137 },
138};
139
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200140/*! TLV parser definitions for TS 04.08 MM */
Andreas Eversberg014cb872010-07-12 09:11:00 +0200141const struct tlv_definition gsm48_mm_att_tlvdef = {
142 .def = {
143 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
144 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
145 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
146 [GSM48_IE_UTC] = { TLV_TYPE_TV },
147 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
148 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
Jacob Erlbeck1c3f0882013-09-16 10:29:57 +0200149 [GSM48_IE_NET_DST] = { TLV_TYPE_TLV },
Andreas Eversberg014cb872010-07-12 09:11:00 +0200150
151 [GSM48_IE_LOCATION_AREA] = { TLV_TYPE_FIXED, 5 },
Pau Espin Pedrol559a6ee2023-03-22 13:17:09 +0100152 [GSM48_IE_PRIORITY_LEV_HO] = { TLV_TYPE_SINGLE_TV },
Andreas Eversberg014cb872010-07-12 09:11:00 +0200153 [GSM48_IE_FOLLOW_ON_PROC] = { TLV_TYPE_T },
154 [GSM48_IE_CTS_PERMISSION] = { TLV_TYPE_T },
155 },
156};
157
Harald Weltee9e190a2010-03-25 11:44:57 +0800158static const struct value_string rr_cause_names[] = {
159 { GSM48_RR_CAUSE_NORMAL, "Normal event" },
160 { GSM48_RR_CAUSE_ABNORMAL_UNSPEC, "Abnormal release, unspecified" },
161 { GSM48_RR_CAUSE_ABNORMAL_UNACCT, "Abnormal release, channel unacceptable" },
162 { GSM48_RR_CAUSE_ABNORMAL_TIMER, "Abnormal release, timer expired" },
163 { GSM48_RR_CAUSE_ABNORMAL_NOACT, "Abnormal release, no activity on radio path" },
164 { GSM48_RR_CAUSE_PREMPTIVE_REL, "Preemptive release" },
Vadim Yanitskiyf5f0d432020-10-29 18:21:53 +0700165 { GSM48_RR_CAUSE_UTRAN_CFG_UNK, "UTRAN configuration unknown" },
Harald Weltee9e190a2010-03-25 11:44:57 +0800166 { GSM48_RR_CAUSE_HNDOVER_IMP, "Handover impossible, timing advance out of range" },
167 { GSM48_RR_CAUSE_CHAN_MODE_UNACCT, "Channel mode unacceptable" },
168 { GSM48_RR_CAUSE_FREQ_NOT_IMPL, "Frequency not implemented" },
Vadim Yanitskiyf5f0d432020-10-29 18:21:53 +0700169 { GSM48_RR_CAUSE_LEAVE_GROUP_CA, "Originator or talker leaving group call area" },
170 { GSM48_RR_CAUSE_LOW_LEVEL_FAIL, "Lower layer failure" },
Harald Weltee9e190a2010-03-25 11:44:57 +0800171 { GSM48_RR_CAUSE_CALL_CLEARED, "Call already cleared" },
172 { GSM48_RR_CAUSE_SEMANT_INCORR, "Semantically incorrect message" },
173 { GSM48_RR_CAUSE_INVALID_MAND_INF, "Invalid mandatory information" },
Vadim Yanitskiy65954ba2020-10-29 18:08:52 +0700174 { GSM48_RR_CAUSE_MSG_TYPE_N, "Message type non-existent or not implemented" },
Harald Weltee9e190a2010-03-25 11:44:57 +0800175 { GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT, "Message type not compatible with protocol state" },
176 { GSM48_RR_CAUSE_COND_IE_ERROR, "Conditional IE error" },
177 { GSM48_RR_CAUSE_NO_CELL_ALLOC_A, "No cell allocation available" },
178 { GSM48_RR_CAUSE_PROT_ERROR_UNSPC, "Protocol error unspecified" },
179 { 0, NULL },
Harald Welte4fb20752010-03-02 23:17:33 +0100180};
181
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200182/*! return string representation of RR Cause value */
Philippada00422016-10-27 13:30:16 +0200183const char *rr_cause_name(uint8_t cause)
184{
185 return get_value_string(rr_cause_names, cause);
186}
187
Harald Welte4a62eda2019-03-18 18:27:00 +0100188/*! Return MCC-MNC-LAC-RAC as string, in a caller-provided output buffer.
189 * \param[out] buf caller-provided output buffer
190 * \param[in] buf_len size of buf in bytes
191 * \param[in] rai RAI to encode.
192 * \returns buf
193 */
194char *osmo_rai_name_buf(char *buf, size_t buf_len, const struct gprs_ra_id *rai)
195{
196 snprintf(buf, buf_len, "%s-%s-%u-%u",
197 osmo_mcc_name(rai->mcc), osmo_mnc_name(rai->mnc, rai->mnc_3_digits), rai->lac,
198 rai->rac);
199 return buf;
200}
201
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100202/*! Return MCC-MNC-LAC-RAC as string, in a static buffer.
203 * \param[in] rai RAI to encode.
204 * \returns Static string buffer.
205 */
206const char *osmo_rai_name(const struct gprs_ra_id *rai)
207{
Harald Welte171ef822019-03-28 10:49:05 +0100208 static __thread char buf[32];
Harald Welte4a62eda2019-03-18 18:27:00 +0100209 return osmo_rai_name_buf(buf, sizeof(buf), rai);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100210}
211
Harald Welte179f3572019-03-18 18:38:47 +0100212/*! Return MCC-MNC-LAC-RAC as string, in dynamically-allocated output buffer.
213 * \param[in] ctx talloc context from which to allocate output buffer
214 * \param[in] rai RAI to encode.
215 * \returns string representation in dynamically-allocated output buffer.
216 */
217char *osmo_rai_name_c(const void *ctx, const struct gprs_ra_id *rai)
218{
219 char *buf = talloc_size(ctx, 32);
220 if (!buf)
221 return NULL;
222 return osmo_rai_name_buf(buf, 32, rai);
223}
224
Harald Welte9eb6d882010-03-25 12:00:54 +0800225/* FIXME: convert to value_string */
Andreas Eversberg014cb872010-07-12 09:11:00 +0200226static const char *cc_state_names[32] = {
Harald Welte4fb20752010-03-02 23:17:33 +0100227 "NULL",
228 "INITIATED",
Andreas Eversberg1ef041f2010-04-09 07:52:12 +0200229 "MM_CONNECTION_PEND",
Harald Welte4fb20752010-03-02 23:17:33 +0100230 "MO_CALL_PROC",
231 "CALL_DELIVERED",
232 "illegal state 5",
233 "CALL_PRESENT",
234 "CALL_RECEIVED",
235 "CONNECT_REQUEST",
236 "MO_TERM_CALL_CONF",
237 "ACTIVE",
238 "DISCONNECT_REQ",
239 "DISCONNECT_IND",
240 "illegal state 13",
241 "illegal state 14",
242 "illegal state 15",
243 "illegal state 16",
244 "illegal state 17",
245 "illegal state 18",
246 "RELEASE_REQ",
247 "illegal state 20",
248 "illegal state 21",
249 "illegal state 22",
250 "illegal state 23",
251 "illegal state 24",
252 "illegal state 25",
253 "MO_ORIG_MODIFY",
254 "MO_TERM_MODIFY",
255 "CONNECT_IND",
256 "illegal state 29",
257 "illegal state 30",
258 "illegal state 31",
259};
260
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200261/*! return string representation of CC State */
Harald Welte9eb6d882010-03-25 12:00:54 +0800262const char *gsm48_cc_state_name(uint8_t state)
263{
264 if (state < ARRAY_SIZE(cc_state_names))
265 return cc_state_names[state];
266
267 return "invalid";
268}
269
270static const struct value_string cc_msg_names[] = {
271 { GSM48_MT_CC_ALERTING, "ALERTING" },
272 { GSM48_MT_CC_CALL_PROC, "CALL_PROC" },
273 { GSM48_MT_CC_PROGRESS, "PROGRESS" },
274 { GSM48_MT_CC_ESTAB, "ESTAB" },
275 { GSM48_MT_CC_SETUP, "SETUP" },
276 { GSM48_MT_CC_ESTAB_CONF, "ESTAB_CONF" },
277 { GSM48_MT_CC_CONNECT, "CONNECT" },
278 { GSM48_MT_CC_CALL_CONF, "CALL_CONF" },
279 { GSM48_MT_CC_START_CC, "START_CC" },
280 { GSM48_MT_CC_RECALL, "RECALL" },
281 { GSM48_MT_CC_EMERG_SETUP, "EMERG_SETUP" },
282 { GSM48_MT_CC_CONNECT_ACK, "CONNECT_ACK" },
283 { GSM48_MT_CC_USER_INFO, "USER_INFO" },
284 { GSM48_MT_CC_MODIFY_REJECT, "MODIFY_REJECT" },
285 { GSM48_MT_CC_MODIFY, "MODIFY" },
286 { GSM48_MT_CC_HOLD, "HOLD" },
287 { GSM48_MT_CC_HOLD_ACK, "HOLD_ACK" },
288 { GSM48_MT_CC_HOLD_REJ, "HOLD_REJ" },
289 { GSM48_MT_CC_RETR, "RETR" },
290 { GSM48_MT_CC_RETR_ACK, "RETR_ACK" },
291 { GSM48_MT_CC_RETR_REJ, "RETR_REJ" },
292 { GSM48_MT_CC_MODIFY_COMPL, "MODIFY_COMPL" },
293 { GSM48_MT_CC_DISCONNECT, "DISCONNECT" },
294 { GSM48_MT_CC_RELEASE_COMPL, "RELEASE_COMPL" },
295 { GSM48_MT_CC_RELEASE, "RELEASE" },
296 { GSM48_MT_CC_STOP_DTMF, "STOP_DTMF" },
297 { GSM48_MT_CC_STOP_DTMF_ACK, "STOP_DTMF_ACK" },
298 { GSM48_MT_CC_STATUS_ENQ, "STATUS_ENQ" },
299 { GSM48_MT_CC_START_DTMF, "START_DTMF" },
300 { GSM48_MT_CC_START_DTMF_ACK, "START_DTMF_ACK" },
301 { GSM48_MT_CC_START_DTMF_REJ, "START_DTMF_REJ" },
302 { GSM48_MT_CC_CONG_CTRL, "CONG_CTRL" },
303 { GSM48_MT_CC_FACILITY, "FACILITY" },
304 { GSM48_MT_CC_STATUS, "STATUS" },
305 { GSM48_MT_CC_NOTIFY, "NOTFIY" },
306 { 0, NULL }
Harald Welte61e2bfc2010-03-04 10:53:03 +0100307};
308
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200309/*! return string representation of CC Message Type */
Harald Welte9eb6d882010-03-25 12:00:54 +0800310const char *gsm48_cc_msg_name(uint8_t msgtype)
311{
312 return get_value_string(cc_msg_names, msgtype);
313}
Harald Welte4fb20752010-03-02 23:17:33 +0100314
Philipp72e43f02016-10-27 13:35:20 +0200315
316static const struct value_string rr_msg_names[] = {
317 /* Channel establishment messages */
318 { GSM48_MT_RR_INIT_REQ, "RR INITIALISATION REQUEST" },
319 { GSM48_MT_RR_ADD_ASS, "ADDITIONAL ASSIGNMENT" },
320 { GSM48_MT_RR_IMM_ASS, "IMMEDIATE ASSIGNMENT" },
321 { GSM48_MT_RR_IMM_ASS_EXT, "MMEDIATE ASSIGNMENT EXTENDED" },
322 { GSM48_MT_RR_IMM_ASS_REJ, "IMMEDIATE ASSIGNMENT REJECT" },
323 { GSM48_MT_RR_DTM_ASS_FAIL, "DTM ASSIGNMENT FAILURE" },
324 { GSM48_MT_RR_DTM_REJECT, "DTM REJECT" },
325 { GSM48_MT_RR_DTM_REQUEST, "DTM REQUEST" },
326 { GSM48_MT_RR_PACKET_ASS, "PACKET ASSIGNMENT" },
327
328 /* Ciphering messages */
329 { GSM48_MT_RR_CIPH_M_CMD, "CIPHERING MODE COMMAND" },
330 { GSM48_MT_RR_CIPH_M_COMPL, "CIPHERING MODE COMPLETE" },
331
332 /* Configuration change messages */
333 { GSM48_MT_RR_CFG_CHG_CMD, "CONFIGURATION CHANGE COMMAND" },
334 { GSM48_MT_RR_CFG_CHG_ACK, "CONFIGURATION CHANGE ACK" },
335 { GSM48_MT_RR_CFG_CHG_REJ, "CONFIGURATION CHANGE REJECT" },
336
337 /* Handover messages */
338 { GSM48_MT_RR_ASS_CMD, "ASSIGNMENT COMMAND" },
339 { GSM48_MT_RR_ASS_COMPL, "ASSIGNMENT COMPLETE" },
340 { GSM48_MT_RR_ASS_FAIL, "ASSIGNMENT FAILURE" },
341 { GSM48_MT_RR_HANDO_CMD, "HANDOVER COMMAND" },
342 { GSM48_MT_RR_HANDO_COMPL, "HANDOVER COMPLETE" },
343 { GSM48_MT_RR_HANDO_FAIL, "HANDOVER FAILURE" },
344 { GSM48_MT_RR_HANDO_INFO, "PHYSICAL INFORMATION" },
345 { GSM48_MT_RR_DTM_ASS_CMD, "DTM ASSIGNMENT COMMAND" },
346
347 { GSM48_MT_RR_CELL_CHG_ORDER, "RR-CELL CHANGE ORDER" },
348 { GSM48_MT_RR_PDCH_ASS_CMD, "PDCH ASSIGNMENT COMMAND" },
349
350 /* Channel release messages */
351 { GSM48_MT_RR_CHAN_REL, "CHANNEL RELEASE" },
352 { GSM48_MT_RR_PART_REL, "PARTIAL RELEASE" },
353 { GSM48_MT_RR_PART_REL_COMP, "PARTIAL RELEASE COMPLETE" },
354
355 /* Paging and Notification messages */
356 { GSM48_MT_RR_PAG_REQ_1, "PAGING REQUEST TYPE 1" },
357 { GSM48_MT_RR_PAG_REQ_2, "PAGING REQUEST TYPE 2" },
358 { GSM48_MT_RR_PAG_REQ_3, "PAGING REQUEST TYPE 3" },
359 { GSM48_MT_RR_PAG_RESP, "PAGING RESPONSE" },
360 { GSM48_MT_RR_NOTIF_NCH, "NOTIFICATION/NCH" },
361 { GSM48_MT_RR_NOTIF_FACCH, "(Reserved)" },
362 { GSM48_MT_RR_NOTIF_RESP, "NOTIFICATION/RESPONSE" },
363 { GSM48_MT_RR_PACKET_NOTIF, "PACKET NOTIFICATION" },
364 /* 3G Specific messages */
365 { GSM48_MT_RR_UTRAN_CLSM_CHG, "UTRAN Classmark Change" },
366 { GSM48_MT_RR_CDMA2K_CLSM_CHG, "cdma 2000 Classmark Change" },
367 { GSM48_MT_RR_IS_TO_UTRAN_HANDO, "Inter System to UTRAN Handover Command" },
368 { GSM48_MT_RR_IS_TO_CDMA2K_HANDO, "Inter System to cdma2000 Handover Command" },
369
370 /* System information messages */
371 { GSM48_MT_RR_SYSINFO_8, "SYSTEM INFORMATION TYPE 8" },
372 { GSM48_MT_RR_SYSINFO_1, "SYSTEM INFORMATION TYPE 1" },
373 { GSM48_MT_RR_SYSINFO_2, "SYSTEM INFORMATION TYPE 2" },
374 { GSM48_MT_RR_SYSINFO_3, "SYSTEM INFORMATION TYPE 3" },
375 { GSM48_MT_RR_SYSINFO_4, "SYSTEM INFORMATION TYPE 4" },
376 { GSM48_MT_RR_SYSINFO_5, "SYSTEM INFORMATION TYPE 5" },
377 { GSM48_MT_RR_SYSINFO_6, "SYSTEM INFORMATION TYPE 6" },
378 { GSM48_MT_RR_SYSINFO_7, "SYSTEM INFORMATION TYPE 7" },
379 { GSM48_MT_RR_SYSINFO_2bis, "SYSTEM INFORMATION TYPE 2bis" },
380 { GSM48_MT_RR_SYSINFO_2ter, "SYSTEM INFORMATION TYPE 2ter" },
381 { GSM48_MT_RR_SYSINFO_2quater, "SYSTEM INFORMATION TYPE 2quater" },
382 { GSM48_MT_RR_SYSINFO_5bis, "SYSTEM INFORMATION TYPE 5bis" },
383 { GSM48_MT_RR_SYSINFO_5ter, "SYSTEM INFORMATION TYPE 5ter" },
384 { GSM48_MT_RR_SYSINFO_9, "SYSTEM INFORMATION TYPE 9" },
385 { GSM48_MT_RR_SYSINFO_13, "SYSTEM INFORMATION TYPE 13" },
386 { GSM48_MT_RR_SYSINFO_16, "SYSTEM INFORMATION TYPE 16" },
387 { GSM48_MT_RR_SYSINFO_17, "SYSTEM INFORMATION TYPE 17" },
388 { GSM48_MT_RR_SYSINFO_18, "SYSTEM INFORMATION TYPE 18" },
389 { GSM48_MT_RR_SYSINFO_19, "SYSTEM INFORMATION TYPE 19" },
390 { GSM48_MT_RR_SYSINFO_20, "SYSTEM INFORMATION TYPE 20" },
391
392 /* Miscellaneous messages */
393 { GSM48_MT_RR_CHAN_MODE_MODIF, "CHANNEL MODE MODIFY" },
394 { GSM48_MT_RR_STATUS, "RR STATUS" },
395 { GSM48_MT_RR_CHAN_MODE_MODIF_ACK, "CHANNEL MODE MODIFY ACKNOWLEDGE" },
396 { GSM48_MT_RR_FREQ_REDEF, "FREQUENCY REDEFINITION" },
397 { GSM48_MT_RR_MEAS_REP, "MEASUREMENT REPORT" },
398 { GSM48_MT_RR_CLSM_CHG, "CLASSMARK CHANGE" },
399 { GSM48_MT_RR_CLSM_ENQ, "CLASSMARK ENQUIRY" },
400 { GSM48_MT_RR_EXT_MEAS_REP, "EXTENDED MEASUREMENT REPORT" },
401 { GSM48_MT_RR_EXT_MEAS_REP_ORD, "EXTENDED MEASUREMENT ORDER" },
402 { GSM48_MT_RR_GPRS_SUSP_REQ, "GPRS SUSPENSION REQUEST" },
403 { GSM48_MT_RR_DTM_INFO, "DTM INFORMATION" },
404
405 /* VGCS uplink control messages */
406 { GSM48_MT_RR_VGCS_UPL_GRANT, "VGCS UPLINK GRANT" },
407 { GSM48_MT_RR_UPLINK_RELEASE, "UPLINK RELEASE" },
408 { GSM48_MT_RR_UPLINK_FREE, "0c" },
409 { GSM48_MT_RR_UPLINK_BUSY, "UPLINK BUSY" },
410 { GSM48_MT_RR_TALKER_IND, "TALKER INDICATION" },
411
412 /* Application messages */
413 { GSM48_MT_RR_APP_INFO, "Application Information" },
414 { 0, NULL }
415};
416
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200417/*! return string representation of RR Message Type */
Philipp72e43f02016-10-27 13:35:20 +0200418const char *gsm48_rr_msg_name(uint8_t msgtype)
419{
420 return get_value_string(rr_msg_names, msgtype);
421}
422
Max600d4ee2023-01-23 21:49:52 +0300423/* 3GPP TS 44.018 Table 10.4.2 */
424static const struct value_string rr_msg_type_short_names[] = {
425 { GSM48_MT_RR_SH_SI10, "System Information Type 10" },
426 { GSM48_MT_RR_SH_FACCH, "Notification/FACCH" },
427 { GSM48_MT_RR_SH_UL_FREE, "Uplink Free" },
428 { GSM48_MT_RR_SH_MEAS_REP, "Enhanced Measurement Report (uplink)" },
429 { GSM48_MT_RR_SH_MEAS_INFO, "Measurement Information (downlink)" },
430 { GSM48_MT_RR_SH_VGCS_RECON, "VBS/VGCS Reconfigure" },
431 { GSM48_MT_RR_SH_VGCS_RECON2, "VBS/VGCS Reconfigure2" },
432 { GSM48_MT_RR_SH_VGCS_INFO, "VGCS Additional Information" },
433 { GSM48_MT_RR_SH_VGCS_SMS, "VGCS SMS Information" },
434 { GSM48_MT_RR_SH_SI10bis, "System Information Type 10bis" },
435 { GSM48_MT_RR_SH_SI10ter, "System Information Type 10ter" },
436 { GSM48_MT_RR_SH_VGCS_NEIGH, "VGCS Neighbour Cell Information" },
437 { GSM48_MT_RR_SH_APP_DATA, "Notify Application Data" },
438 { 0, NULL }
439};
440
441/*! return string representation of RR Message Type using the RR short protocol discriminator */
Maxbc127282023-01-26 14:40:23 +0300442const char *gsm48_rr_short_pd_msg_name(uint8_t msgtype)
Max600d4ee2023-01-23 21:49:52 +0300443{
444 return get_value_string(rr_msg_type_short_names, msgtype);
445}
Philipp72e43f02016-10-27 13:35:20 +0200446
Maxfb348ee2016-03-30 21:14:53 +0200447const struct value_string gsm48_chan_mode_names[] = {
448 { GSM48_CMODE_SIGN, "SIGNALLING" },
449 { GSM48_CMODE_SPEECH_V1, "SPEECH_V1" },
450 { GSM48_CMODE_SPEECH_EFR, "SPEECH_EFR" },
451 { GSM48_CMODE_SPEECH_AMR, "SPEECH_AMR" },
Vadim Yanitskiy1c88ff92023-03-23 06:33:51 +0700452 { GSM48_CMODE_SPEECH_V4, "SPEECH_V4" },
453 { GSM48_CMODE_SPEECH_V5, "SPEECH_V5" },
454 { GSM48_CMODE_SPEECH_V6, "SPEECH_V6" },
455
Vadim Yanitskiy173c84c2023-03-23 06:34:13 +0700456 { GSM48_CMODE_DATA_43k5_14k5, "DATA_43k5_14k5" },
457 { GSM48_CMODE_DATA_29k0_14k5, "DATA_29k0_14k5" },
458 { GSM48_CMODE_DATA_43k5_29k0, "DATA_43k5_29k0" },
459 { GSM48_CMODE_DATA_14k5_43k5, "DATA_14k5_43k5" },
460 { GSM48_CMODE_DATA_14k5_29k0, "DATA_14k5_29k0" },
461 { GSM48_CMODE_DATA_29k0_43k5, "DATA_29k0_43k5" },
462
463 { GSM48_CMODE_DATA_43k5, "DATA_43k5" },
464 { GSM48_CMODE_DATA_32k0, "DATA_32k0" },
465 { GSM48_CMODE_DATA_29k0, "DATA_29k0" },
Maxfb348ee2016-03-30 21:14:53 +0200466 { GSM48_CMODE_DATA_14k5, "DATA_14k5" },
467 { GSM48_CMODE_DATA_12k0, "DATA_12k0" },
468 { GSM48_CMODE_DATA_6k0, "DATA_6k0" },
469 { GSM48_CMODE_DATA_3k6, "DATA_3k6" },
Vadim Yanitskiy1c88ff92023-03-23 06:33:51 +0700470
Neels Hofmeyra9da9f72021-04-16 15:36:58 +0200471 { GSM48_CMODE_SPEECH_V1_VAMOS, "SPEECH_V1_VAMOS" },
472 { GSM48_CMODE_SPEECH_V2_VAMOS, "SPEECH_V2_VAMOS" },
473 { GSM48_CMODE_SPEECH_V3_VAMOS, "SPEECH_V3_VAMOS" },
474 { GSM48_CMODE_SPEECH_V5_VAMOS, "SPEECH_V5_VAMOS" },
Maxfb348ee2016-03-30 21:14:53 +0200475 { 0, NULL },
476};
477
Neels Hofmeyra9da9f72021-04-16 15:36:58 +0200478/*! Translate GSM48_CMODE_SPEECH_* to its corresponding GSM48_CMODE_SPEECH_*_VAMOS mode.
479 * If the mode has no equivalent VAMOS mode, return a negative value.
480 */
481enum gsm48_chan_mode gsm48_chan_mode_to_vamos(enum gsm48_chan_mode mode)
482{
483 switch (mode) {
484 case GSM48_CMODE_SPEECH_V1:
485 case GSM48_CMODE_SPEECH_V1_VAMOS:
486 return GSM48_CMODE_SPEECH_V1_VAMOS;
487 case GSM48_CMODE_SPEECH_EFR:
488 case GSM48_CMODE_SPEECH_V2_VAMOS:
489 return GSM48_CMODE_SPEECH_V2_VAMOS;
490 case GSM48_CMODE_SPEECH_AMR:
491 case GSM48_CMODE_SPEECH_V3_VAMOS:
492 return GSM48_CMODE_SPEECH_V3_VAMOS;
493 case GSM48_CMODE_SPEECH_V5_VAMOS:
494 return GSM48_CMODE_SPEECH_V5_VAMOS;
495 default:
496 return -1;
497 }
498}
499
500/*! Translate GSM48_CMODE_SPEECH_*_VAMOS to its corresponding GSM48_CMODE_SPEECH_* non-vamos mode.
Neels Hofmeyr208c5b62021-05-29 21:31:07 +0000501 * If the mode is not a VAMOS mode, return the unchanged mode.
Neels Hofmeyra9da9f72021-04-16 15:36:58 +0200502 */
503enum gsm48_chan_mode gsm48_chan_mode_to_non_vamos(enum gsm48_chan_mode mode)
504{
505 switch (mode) {
506 case GSM48_CMODE_SPEECH_V1_VAMOS:
Neels Hofmeyra9da9f72021-04-16 15:36:58 +0200507 return GSM48_CMODE_SPEECH_V1;
508 case GSM48_CMODE_SPEECH_V2_VAMOS:
Neels Hofmeyra9da9f72021-04-16 15:36:58 +0200509 return GSM48_CMODE_SPEECH_EFR;
510 case GSM48_CMODE_SPEECH_V3_VAMOS:
Neels Hofmeyra9da9f72021-04-16 15:36:58 +0200511 return GSM48_CMODE_SPEECH_AMR;
Vadim Yanitskiy1c88ff92023-03-23 06:33:51 +0700512 case GSM48_CMODE_SPEECH_V5_VAMOS:
513 return GSM48_CMODE_SPEECH_V5;
Neels Hofmeyra9da9f72021-04-16 15:36:58 +0200514 default:
Neels Hofmeyr208c5b62021-05-29 21:31:07 +0000515 return mode;
Neels Hofmeyra9da9f72021-04-16 15:36:58 +0200516 }
517}
518
Maxfb348ee2016-03-30 21:14:53 +0200519const struct value_string gsm_chan_t_names[] = {
520 { GSM_LCHAN_NONE, "NONE" },
521 { GSM_LCHAN_SDCCH, "SDCCH" },
522 { GSM_LCHAN_TCH_F, "TCH_F" },
523 { GSM_LCHAN_TCH_H, "TCH_H" },
524 { GSM_LCHAN_UNKNOWN, "UNKNOWN" },
525 { GSM_LCHAN_CCCH, "CCCH" },
526 { GSM_LCHAN_PDTCH, "PDTCH" },
527 { GSM_LCHAN_CBCH, "CBCH" },
528 { 0, NULL },
529};
530
Harald Welte1a8c4e02015-08-16 17:56:25 +0200531static const struct value_string mi_type_names[] = {
532 { GSM_MI_TYPE_NONE, "NONE" },
533 { GSM_MI_TYPE_IMSI, "IMSI" },
534 { GSM_MI_TYPE_IMEI, "IMEI" },
535 { GSM_MI_TYPE_IMEISV, "IMEI-SV" },
536 { GSM_MI_TYPE_TMSI, "TMSI" },
537 { 0, NULL }
538};
539
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200540/*! return string representation of Mobile Identity Type */
Harald Welte1a8c4e02015-08-16 17:56:25 +0200541const char *gsm48_mi_type_name(uint8_t mi)
542{
543 return get_value_string(mi_type_names, mi);
544}
545
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200546/*! Deprecated, see osmo_mobile_identity instead.
547 * Return a human readable representation of a Mobile Identity in caller-provided buffer.
Harald Welte4a62eda2019-03-18 18:27:00 +0100548 * \param[out] buf caller-provided output buffer
549 * \param[in] buf_len size of buf in bytes
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100550 * \param[in] mi Mobile Identity buffer containing 3GPP TS 04.08 style MI type and data.
551 * \param[in] mi_len Length of mi.
Harald Welte4a62eda2019-03-18 18:27:00 +0100552 * \return buf
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100553 */
Harald Weltea13fb752020-06-16 08:44:42 +0200554char *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 +0100555{
Harald Weltea13fb752020-06-16 08:44:42 +0200556 uint8_t mi_type;
557 uint32_t tmsi;
558 char mi_string[GSM48_MI_SIZE];
559
560 mi_type = (mi && mi_len) ? (mi[0] & GSM_MI_TYPE_MASK) : GSM_MI_TYPE_NONE;
561
562 switch (mi_type) {
563 case GSM_MI_TYPE_TMSI:
564 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
565 if (mi_len == GSM48_TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
566 tmsi = osmo_load32be(&mi[1]);
567 snprintf(buf, buf_len, "TMSI-0x%08" PRIX32, tmsi);
568 } else {
569 snprintf(buf, buf_len, "TMSI-invalid");
570 }
571 return buf;
572
573 case GSM_MI_TYPE_IMSI:
574 case GSM_MI_TYPE_IMEI:
575 case GSM_MI_TYPE_IMEISV:
576 osmo_bcd2str(mi_string, sizeof(mi_string), mi, 1, (mi_len * 2) - (mi[0] & GSM_MI_ODD ? 0 : 1), true);
577 snprintf(buf, buf_len, "%s-%s", gsm48_mi_type_name(mi_type), mi_string);
578 return buf;
579
580 default:
Neels Hofmeyr6d670322020-05-26 03:12:29 +0200581 snprintf(buf, buf_len, "unknown");
582 return buf;
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100583 }
584}
585
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200586/*! Deprecated, see osmo_mobile_identity instead.
587 * Return a human readable representation of a Mobile Identity in static buffer.
Harald Welte4a62eda2019-03-18 18:27:00 +0100588 * \param[in] mi Mobile Identity buffer containing 3GPP TS 04.08 style MI type and data.
589 * \param[in] mi_len Length of mi.
590 * \return A string like "IMSI-1234567", "TMSI-0x1234ABCD" or "unknown", "TMSI-invalid"...
591 */
Harald Weltea13fb752020-06-16 08:44:42 +0200592const char *osmo_mi_name(const uint8_t *mi, uint8_t mi_len)
Harald Welte4a62eda2019-03-18 18:27:00 +0100593{
Harald Welte171ef822019-03-28 10:49:05 +0100594 static __thread char mi_name[10 + GSM48_MI_SIZE + 1];
Harald Weltea13fb752020-06-16 08:44:42 +0200595 return osmo_mi_name_buf(mi_name, sizeof(mi_name), mi, mi_len);
Harald Welte4a62eda2019-03-18 18:27:00 +0100596}
597
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200598/*! Deprecated, see osmo_mobile_identity instead.
599 * Return a human readable representation of a Mobile Identity in dynamically-allocated buffer.
Harald Welte179f3572019-03-18 18:38:47 +0100600 * \param[in] ctx talloc context from which to allocate output buffer
601 * \param[in] mi Mobile Identity buffer containing 3GPP TS 04.08 style MI type and data.
602 * \param[in] mi_len Length of mi.
603 * \return A string like "IMSI-1234567", "TMSI-0x1234ABCD" or "unknown", "TMSI-invalid" in a
604 * dynamically-allocated output buffer.
605 */
Harald Weltea13fb752020-06-16 08:44:42 +0200606char *osmo_mi_name_c(const void *ctx, const uint8_t *mi, uint8_t mi_len)
Harald Welte179f3572019-03-18 18:38:47 +0100607{
Harald Weltea13fb752020-06-16 08:44:42 +0200608 size_t buf_len = 10 + GSM48_MI_SIZE + 1;
609 char *mi_name = talloc_size(ctx, buf_len);
610 if (!mi_name)
611 return NULL;
612 return osmo_mi_name_buf(mi_name, buf_len, mi, mi_len);
Harald Welte179f3572019-03-18 18:38:47 +0100613}
614
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200615/*! Extract Mobile Identity from encoded bytes (3GPP TS 24.008 10.5.1.4).
616 *
617 * On failure (negative return value), mi->type == GSM_MI_TYPE_NONE, mi->string[] is all-zero and mi->tmsi ==
618 * GSM_RESERVED_TMSI.
619 *
620 * On success, mi->type reflects the decoded Mobile Identity type (GSM_MI_TYPE_IMSI, GSM_MI_TYPE_TMSI, GSM_MI_TYPE_IMEI
621 * or GSM_MI_TYPE_IMEISV).
622 *
623 * On success, mi->string always contains a human readable representation of the Mobile Identity digits: IMSI, IMEI and
624 * IMEISV as digits like "12345678", and TMSI as "0x" and 8 hexadecimal digits like "0x1234abcd".
625 *
626 * mi->tmsi contains the uint32_t TMSI value iff the extracted Mobile Identity was a TMSI, or GSM_RESERVED_TMSI
627 * otherwise.
628 *
629 * \param[out] mi Return buffer for decoded Mobile Identity.
630 * \param[in] mi_data The encoded Mobile Identity octets.
631 * \param[in] mi_len Number of octets in mi_data.
632 * \param[in] allow_hex If false, hexadecimal digits (>9) result in an error return value.
633 * \returns 0 on success, negative on error: -EBADMSG = invalid length indication or invalid data,
634 * -EINVAL = unknown Mobile Identity type.
635 */
636int osmo_mobile_identity_decode(struct osmo_mobile_identity *mi, const uint8_t *mi_data, uint8_t mi_len,
637 bool allow_hex)
638{
639 int rc;
640 int nibbles_len;
Pau Espin Pedrol6fe865d2021-07-15 13:08:08 +0200641 char *str = NULL; /* initialize to avoid uninitialized false warnings on some gcc versions (11.1.0) */
642 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 +0200643
Vadim Yanitskiy90df3f42024-01-08 21:38:47 +0700644 if (!mi_data || mi_len < 1) {
645 rc = -EBADMSG;
646 goto return_error;
647 }
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200648
649 nibbles_len = (mi_len - 1) * 2 + ((mi_data[0] & GSM_MI_ODD) ? 1 : 0);
650
651 *mi = (struct osmo_mobile_identity){
652 .type = mi_data[0] & GSM_MI_TYPE_MASK,
653 };
654
655 /* First do length checks */
656 switch (mi->type) {
657 case GSM_MI_TYPE_TMSI:
658 mi->tmsi = GSM_RESERVED_TMSI;
659 if (nibbles_len != (GSM23003_TMSI_NUM_BYTES * 2)) {
660 rc = -EBADMSG;
661 goto return_error;
662 }
663 break;
664
665 case GSM_MI_TYPE_IMSI:
666 if (nibbles_len < GSM23003_IMSI_MIN_DIGITS || nibbles_len > GSM23003_IMSI_MAX_DIGITS) {
667 rc = -EBADMSG;
668 goto return_error;
669 }
670 str = mi->imsi;
671 str_size = sizeof(mi->imsi);
672 break;
673
674 case GSM_MI_TYPE_IMEI:
675 if (nibbles_len != GSM23003_IMEI_NUM_DIGITS && nibbles_len != GSM23003_IMEI_NUM_DIGITS_NO_CHK) {
676 rc = -EBADMSG;
677 goto return_error;
678 }
679 str = mi->imei;
680 str_size = sizeof(mi->imei);
681 break;
682
683 case GSM_MI_TYPE_IMEISV:
684 if (nibbles_len != GSM23003_IMEISV_NUM_DIGITS) {
685 rc = -EBADMSG;
686 goto return_error;
687 }
688 str = mi->imeisv;
689 str_size = sizeof(mi->imeisv);
690 break;
691
692 default:
693 rc = -EINVAL;
694 goto return_error;
695 }
696
697 /* Decode BCD digits */
698 switch (mi->type) {
699 case GSM_MI_TYPE_TMSI:
700 /* MI is a 32bit integer TMSI. Length has been checked above. */
701 if ((mi_data[0] & 0xf0) != 0xf0) {
702 /* A TMSI always has the first nibble == 0xf */
703 rc = -EBADMSG;
704 goto return_error;
705 }
706 mi->tmsi = osmo_load32be(&mi_data[1]);
707 return 0;
708
709 case GSM_MI_TYPE_IMSI:
710 case GSM_MI_TYPE_IMEI:
711 case GSM_MI_TYPE_IMEISV:
712 /* If the length is even, the last nibble (higher nibble of last octet) must be 0xf */
713 if (!(mi_data[0] & GSM_MI_ODD)
714 && ((mi_data[mi_len - 1] & 0xf0) != 0xf0)) {
715 rc = -EBADMSG;
716 goto return_error;
717 }
718 rc = osmo_bcd2str(str, str_size, mi_data, 1, 1 + nibbles_len, allow_hex);
Pau Espin Pedrol6fe865d2021-07-15 13:08:08 +0200719 /* check mi->str printing rc */
720 if (rc < 1 || rc >= str_size) {
721 rc = -EBADMSG;
722 goto return_error;
723 }
724 return 0;
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200725
726 default:
727 /* Already handled above, but as future bug paranoia: */
728 rc = -EINVAL;
729 goto return_error;
730 }
731
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200732return_error:
733 *mi = (struct osmo_mobile_identity){
734 .type = GSM_MI_TYPE_NONE,
735 };
736 return rc;
737}
738
739/*! Return the number of encoded Mobile Identity octets, without actually encoding.
740 * Useful to write tag-length header before encoding the MI.
741 * \param[in] mi Mobile Identity.
742 * \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).
743 * \return octets that osmo_mobile_identity_encode_msgb() will write for this mi.
744 */
745int osmo_mobile_identity_encoded_len(const struct osmo_mobile_identity *mi, int *mi_digits)
746{
747 int mi_nibbles;
748 if (!mi)
749 return -EINVAL;
750 switch (mi->type) {
751 case GSM_MI_TYPE_TMSI:
752 mi_nibbles = GSM23003_TMSI_NUM_BYTES * 2;
753 break;
754 case GSM_MI_TYPE_IMSI:
755 mi_nibbles = strlen(mi->imsi);
756 if (mi_nibbles < GSM23003_IMSI_MIN_DIGITS
757 || mi_nibbles > GSM23003_IMSI_MAX_DIGITS)
758 return -EINVAL;
759 break;
760 case GSM_MI_TYPE_IMEI:
761 mi_nibbles = strlen(mi->imei);
762 if (mi_nibbles < GSM23003_IMEI_NUM_DIGITS_NO_CHK
763 || mi_nibbles > GSM23003_IMEI_NUM_DIGITS)
764 return -EINVAL;
765 break;
766 case GSM_MI_TYPE_IMEISV:
767 mi_nibbles = strlen(mi->imeisv);
768 if (mi_nibbles != GSM23003_IMEISV_NUM_DIGITS)
769 return -EINVAL;
770 break;
771 default:
772 return -ENOTSUP;
773 }
774
775 if (mi_digits)
776 *mi_digits = mi_nibbles;
777
778 /* one type nibble, plus the MI nibbles, plus a filler nibble to complete the last octet:
779 * mi_octets = ceil((float)(mi_nibbles + 1) / 2)
780 */
781 return (mi_nibbles + 2) / 2;
782}
783
784/*! Encode Mobile Identity from uint32_t (TMSI) or digits string (all others) (3GPP TS 24.008 10.5.1.4).
785 *
786 * \param[out] buf Return buffer for encoded Mobile Identity.
787 * \param[in] buflen sizeof(buf).
788 * \param[in] mi Mobile identity to encode.
789 * \param[in] allow_hex If false, hexadecimal digits (>9) result in an error return value.
790 * \returns Amount of bytes written to buf, or negative on error.
791 */
792int osmo_mobile_identity_encode_buf(uint8_t *buf, size_t buflen, const struct osmo_mobile_identity *mi, bool allow_hex)
793{
794 int rc;
795 int nibbles_len;
796 int mi_octets;
797 const char *mi_str;
798
799 if (!buf || !buflen)
800 return -EIO;
801
802 mi_octets = osmo_mobile_identity_encoded_len(mi, &nibbles_len);
803 if (mi_octets < 0)
804 return mi_octets;
805 if (mi_octets > buflen)
806 return -ENOSPC;
807
808 buf[0] = (mi->type & GSM_MI_TYPE_MASK) | ((nibbles_len & 1) ? GSM_MI_ODD : 0);
809
810 switch (mi->type) {
811 case GSM_MI_TYPE_TMSI:
812 buf[0] |= 0xf0;
813 osmo_store32be(mi->tmsi, &buf[1]);
814 return mi_octets;
815
816 case GSM_MI_TYPE_IMSI:
817 mi_str = mi->imsi;
818 break;
819 case GSM_MI_TYPE_IMEI:
820 mi_str = mi->imei;
821 break;
822 case GSM_MI_TYPE_IMEISV:
823 mi_str = mi->imeisv;
824 break;
825 default:
826 return -ENOTSUP;
827 }
828 rc = osmo_str2bcd(buf, buflen, mi_str, 1, -1, allow_hex);
829 if (rc != mi_octets)
830 return -EINVAL;
831 return mi_octets;
832}
833
834/*! Encode Mobile Identity type and BCD digits, appended to a msgb.
835 * Example to add a GSM48_IE_MOBILE_ID IEI with tag and length to a msgb:
836 *
Neels Hofmeyr4247c942020-06-22 17:48:01 +0200837 * struct osmo_mobile_identity mi = { .type = GSM_MI_TYPE_IMSI };
838 * OSMO_STRLCPY_ARRAY(mi.imsi, "1234567890123456");
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200839 * uint8_t *l = msgb_tl_put(msg, GSM48_IE_MOBILE_ID);
840 * int rc = osmo_mobile_identity_encode_msgb(msg, &mi, false);
841 * if (rc < 0)
842 * goto error;
843 * *l = rc;
844 *
845 * Example to add a BSSGP_IE_IMSI with tag and variable-size length, where the
846 * length needs to be known at the time of writing the IE tag-length header:
847 *
848 * struct osmo_mobile_identity mi = { .type = GSM_MI_TYPE_IMSI, };
849 * OSMO_STRLCPY_ARRAY(mi.imsi, pinfo->imsi);
850 * msgb_tvl_put(msg, BSSGP_IE_IMSI, osmo_mobile_identity_encoded_len(&mi, NULL));
851 * if (osmo_mobile_identity_encode_msgb(msg, &mi, false) < 0)
852 * goto error;
853 */
854int osmo_mobile_identity_encode_msgb(struct msgb *msg, const struct osmo_mobile_identity *mi, bool allow_hex)
855{
856 int rc = osmo_mobile_identity_encode_buf(msg->tail, msgb_tailroom(msg), mi, allow_hex);
857 if (rc < 0)
858 return rc;
859 msgb_put(msg, rc);
860 return rc;
861}
862
863/*! Extract Mobile Identity from a Complete Layer 3 message.
864 *
865 * Determine the Mobile Identity data and call osmo_mobile_identity_decode() to return a decoded struct
866 * osmo_mobile_identity.
867 *
868 * \param[out] mi Return buffer for decoded Mobile Identity.
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200869 * \param[in] l3_data The Complete Layer 3 message to extract from (LU, CM Service Req or Paging Resp).
870 * \param[in] l3_len Length of l3_data in bytes.
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200871 * \returns 0 on success, negative on error: return codes as defined in osmo_mobile_identity_decode(), or
872 * -ENOTSUP = not a Complete Layer 3 message,
873 */
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200874int osmo_mobile_identity_decode_from_l3_buf(struct osmo_mobile_identity *mi, const uint8_t *l3_data, size_t l3_len,
875 bool allow_hex)
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200876{
877 const struct gsm48_hdr *gh;
878 int8_t pdisc = 0;
879 uint8_t mtype = 0;
880 const struct gsm48_loc_upd_req *lu;
881 const uint8_t *cm2_buf;
882 uint8_t cm2_len;
883 const uint8_t *mi_start;
884 const struct gsm48_pag_resp *paging_response;
885 const uint8_t *mi_data;
886 uint8_t mi_len;
887 const struct gsm48_imsi_detach_ind *idi;
888
889 *mi = (struct osmo_mobile_identity){
890 .type = GSM_MI_TYPE_NONE,
891 .tmsi = GSM_RESERVED_TMSI,
892 };
893
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200894 if (l3_len < sizeof(*gh))
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200895 return -EBADMSG;
896
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200897 gh = (void *)l3_data;
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200898 pdisc = gsm48_hdr_pdisc(gh);
899 mtype = gsm48_hdr_msg_type(gh);
900
901 switch (pdisc) {
902 case GSM48_PDISC_MM:
903
904 switch (mtype) {
905 case GSM48_MT_MM_LOC_UPD_REQUEST:
906 /* First make sure that lu-> can be dereferenced */
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200907 if (l3_len < sizeof(*gh) + sizeof(*lu))
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200908 return -EBADMSG;
909
910 /* Now we know there is enough msgb data to read a lu->mi_len, so also check that */
911 lu = (struct gsm48_loc_upd_req*)gh->data;
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200912 if (l3_len < sizeof(*gh) + sizeof(*lu) + lu->mi_len)
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200913 return -EBADMSG;
914 mi_data = lu->mi;
915 mi_len = lu->mi_len;
916 goto got_mi;
917
918 case GSM48_MT_MM_CM_SERV_REQ:
919 case GSM48_MT_MM_CM_REEST_REQ:
920 /* Unfortunately in Phase1 the Classmark2 length is variable, so we cannot
921 * just use gsm48_service_request struct, and need to parse it manually. */
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200922 if (l3_len < sizeof(*gh) + 2)
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200923 return -EBADMSG;
924
925 cm2_len = gh->data[1];
926 cm2_buf = gh->data + 2;
927 goto got_cm2;
928
929 case GSM48_MT_MM_IMSI_DETACH_IND:
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200930 if (l3_len < sizeof(*gh) + sizeof(*idi))
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200931 return -EBADMSG;
932 idi = (struct gsm48_imsi_detach_ind*) gh->data;
933 mi_data = idi->mi;
934 mi_len = idi->mi_len;
935 goto got_mi;
936
937 case GSM48_MT_MM_ID_RESP:
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200938 if (l3_len < sizeof(*gh) + 2)
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200939 return -EBADMSG;
940 mi_data = gh->data+1;
941 mi_len = gh->data[0];
942 goto got_mi;
943
944 default:
945 break;
946 }
947 break;
948
949 case GSM48_PDISC_RR:
950
951 switch (mtype) {
952 case GSM48_MT_RR_PAG_RESP:
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200953 if (l3_len < sizeof(*gh) + sizeof(*paging_response))
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200954 return -EBADMSG;
955 paging_response = (struct gsm48_pag_resp*)gh->data;
956 cm2_len = paging_response->cm2_len;
957 cm2_buf = (uint8_t*)&paging_response->cm2;
958 goto got_cm2;
959
Andreas Eversberge4c7b0a2023-06-26 10:21:48 +0200960 case GSM48_MT_RR_TALKER_IND:
961 /* Check minimum size: Header + CM2 LV + minimum MI LV */
962 if (l3_len < sizeof(*gh) + 4 + 2)
963 return -EBADMSG;
964 /* CM2 shall be always 3 bytes in length */
965 if (gh->data[0] != 3)
966 return -EBADMSG;
967 cm2_len = gh->data[0];
968 cm2_buf = gh->data + 1;
969 goto got_cm2;
970
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200971 default:
972 break;
973 }
974 break;
975 }
976
977 return -ENOTSUP;
978
979got_cm2:
980 /* MI (Mobile Identity) LV follows the Classmark2 */
981
982 /* There must be at least a mi_len byte after the CM2 */
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200983 if (cm2_buf + cm2_len + 1 > l3_data + l3_len)
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200984 return -EBADMSG;
985
986 mi_start = cm2_buf + cm2_len;
987 mi_len = mi_start[0];
988 mi_data = mi_start + 1;
989
990got_mi:
991 /* mi_data points at the start of the Mobile Identity coding of mi_len bytes */
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200992 if (mi_data + mi_len > l3_data + l3_len)
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200993 return -EBADMSG;
994
995 return osmo_mobile_identity_decode(mi, mi_data, mi_len, allow_hex);
996}
997
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200998/*! Extract Mobile Identity from a Complete Layer 3 message.
999 *
1000 * Determine the Mobile Identity data and call osmo_mobile_identity_decode() to return a decoded struct
1001 * osmo_mobile_identity.
1002 *
1003 * \param[out] mi Return buffer for decoded Mobile Identity.
1004 * \param[in] msg The Complete Layer 3 message to extract from (LU, CM Service Req or Paging Resp).
1005 * \returns 0 on success, negative on error: return codes as defined in osmo_mobile_identity_decode(), or
1006 * -ENOTSUP = not a Complete Layer 3 message,
1007 */
1008int osmo_mobile_identity_decode_from_l3(struct osmo_mobile_identity *mi, struct msgb *msg, bool allow_hex)
1009{
1010 return osmo_mobile_identity_decode_from_l3_buf(mi, msgb_l3(msg), msgb_l3len(msg), allow_hex);
1011}
1012
Neels Hofmeyr83025bf2020-05-26 02:45:23 +02001013/*! Return a human readable representation of a struct osmo_mobile_identity.
1014 * Write a string like "IMSI-1234567", "TMSI-0x1234ABCD" or "NONE", "NULL".
1015 * \param[out] buf String buffer to write to.
1016 * \param[in] buflen sizeof(buf).
1017 * \param[in] mi Decoded Mobile Identity data.
1018 * \return the strlen() of the string written when buflen is sufficiently large, like snprintf().
1019 */
1020int osmo_mobile_identity_to_str_buf(char *buf, size_t buflen, const struct osmo_mobile_identity *mi)
1021{
1022 struct osmo_strbuf sb = { .buf = buf, .len = buflen };
1023 if (!mi)
1024 return snprintf(buf, buflen, "NULL");
1025 OSMO_STRBUF_PRINTF(sb, "%s", gsm48_mi_type_name(mi->type));
1026 switch (mi->type) {
1027 case GSM_MI_TYPE_TMSI:
1028 OSMO_STRBUF_PRINTF(sb, "-0x%08" PRIX32, mi->tmsi);
1029 break;
1030 case GSM_MI_TYPE_IMSI:
1031 OSMO_STRBUF_PRINTF(sb, "-%s", mi->imsi);
1032 break;
1033 case GSM_MI_TYPE_IMEI:
1034 OSMO_STRBUF_PRINTF(sb, "-%s", mi->imei);
1035 break;
1036 case GSM_MI_TYPE_IMEISV:
1037 OSMO_STRBUF_PRINTF(sb, "-%s", mi->imeisv);
1038 break;
1039 default:
1040 break;
1041 }
1042 return sb.chars_needed;
1043}
1044
1045/*! Like osmo_mobile_identity_to_str_buf(), but return the string in a talloc buffer.
1046 * \param[in] ctx Talloc context to allocate from.
1047 * \param[in] mi Decoded Mobile Identity data.
1048 * \return a string like "IMSI-1234567", "TMSI-0x1234ABCD" or "NONE", "NULL".
1049 */
1050char *osmo_mobile_identity_to_str_c(void *ctx, const struct osmo_mobile_identity *mi)
1051{
1052 OSMO_NAME_C_IMPL(ctx, 32, "ERROR", osmo_mobile_identity_to_str_buf, mi)
1053}
1054
1055/*! Compare two osmo_mobile_identity structs, returning typical cmp() result.
1056 * \param[in] a Left side osmo_mobile_identity.
1057 * \param[in] b Right side osmo_mobile_identity.
1058 * \returns 0 if both are equal, -1 if a < b, 1 if a > b.
1059 */
1060int osmo_mobile_identity_cmp(const struct osmo_mobile_identity *a, const struct osmo_mobile_identity *b)
1061{
1062 int cmp;
1063 if (a == b)
1064 return 0;
1065 if (!a)
1066 return -1;
1067 if (!b)
1068 return 1;
1069 cmp = OSMO_CMP(a->type, b->type);
1070 if (cmp)
1071 return cmp;
1072 switch (a->type) {
1073 case GSM_MI_TYPE_TMSI:
1074 return OSMO_CMP(a->tmsi, b->tmsi);
1075 case GSM_MI_TYPE_IMSI:
1076 return strncmp(a->imsi, b->imsi, sizeof(a->imsi));
1077 case GSM_MI_TYPE_IMEI:
1078 return strncmp(a->imei, b->imei, sizeof(a->imei));
1079 case GSM_MI_TYPE_IMEISV:
1080 return strncmp(a->imeisv, b->imeisv, sizeof(a->imeisv));
1081 default:
1082 /* No known type, but both have the same type. */
1083 return 0;
1084 }
1085}
1086
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001087/*! Checks is particular message is cipherable in A/Gb mode according to
Maxfdca25d2016-07-05 16:06:28 +02001088 * 3GPP TS 24.008 § 4.7.1.2
1089 * \param[in] hdr Message header
1090 * \return true if message can be encrypted, false otherwise
1091 */
1092bool gsm48_hdr_gmm_cipherable(const struct gsm48_hdr *hdr)
1093{
1094 switch(hdr->msg_type) {
1095 case GSM48_MT_GMM_ATTACH_REQ:
1096 case GSM48_MT_GMM_ATTACH_REJ:
1097 case GSM48_MT_GMM_AUTH_CIPH_REQ:
1098 case GSM48_MT_GMM_AUTH_CIPH_RESP:
1099 case GSM48_MT_GMM_AUTH_CIPH_REJ:
1100 case GSM48_MT_GMM_AUTH_CIPH_FAIL:
1101 case GSM48_MT_GMM_ID_REQ:
1102 case GSM48_MT_GMM_ID_RESP:
1103 case GSM48_MT_GMM_RA_UPD_REQ:
1104 case GSM48_MT_GMM_RA_UPD_REJ:
1105 return false;
1106 default:
1107 return true;
1108 }
1109}
1110
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001111/* Convert MCC + MNC to BCD representation, legacy implementation.
1112 * Instead use osmo_plmn_to_bcd(), which is also capable of converting
1113 * 3-digit MNC that have leading zeros. For parameters, also see there. */
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +01001114void gsm48_mcc_mnc_to_bcd(uint8_t *bcd_dst, uint16_t mcc, uint16_t mnc)
Harald Welte61e2bfc2010-03-04 10:53:03 +01001115{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001116 const struct osmo_plmn_id plmn = {
1117 .mcc = mcc,
1118 .mnc = mnc,
1119 .mnc_3_digits = false,
1120 };
1121 osmo_plmn_to_bcd(bcd_dst, &plmn);
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +01001122}
Harald Welte61e2bfc2010-03-04 10:53:03 +01001123
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001124/* Convert given 3-byte BCD buffer to integers, legacy implementation.
1125 * Instead use osmo_plmn_from_bcd(), which is also capable of converting
1126 * 3-digit MNC that have leading zeros. For parameters, also see there. */
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +01001127void gsm48_mcc_mnc_from_bcd(uint8_t *bcd_src, uint16_t *mcc, uint16_t *mnc)
1128{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001129 struct osmo_plmn_id plmn;
1130 osmo_plmn_from_bcd(bcd_src, &plmn);
1131 *mcc = plmn.mcc;
1132 *mnc = plmn.mnc;
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +01001133}
1134
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001135/*! Encode TS 04.08 Location Area Identifier, legacy implementation.
1136 * Instead use gsm48_generate_lai2(), which is capable of three-digit MNC with leading zeros.
1137 * \param[out] lai48 caller-provided memory for output
Harald Welte96e2a002017-06-12 21:44:18 +02001138 * \param[in] mcc Mobile Country Code
1139 * \param[in] mnc Mobile Network Code
1140 * \param[in] lac Location Area Code */
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +01001141void gsm48_generate_lai(struct gsm48_loc_area_id *lai48, uint16_t mcc,
1142 uint16_t mnc, uint16_t lac)
1143{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001144 const struct osmo_location_area_id lai = {
1145 .plmn = {
1146 .mcc = mcc,
1147 .mnc = mnc,
1148 .mnc_3_digits = false,
1149 },
1150 .lac = lac,
1151 };
1152 gsm48_generate_lai2(lai48, &lai);
Harald Welte61e2bfc2010-03-04 10:53:03 +01001153}
1154
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001155/*! Encode TS 04.08 Location Area Identifier.
1156 * \param[out] lai48 caller-provided memory for output.
1157 * \param[in] lai input of MCC-MNC-LAC. */
1158void gsm48_generate_lai2(struct gsm48_loc_area_id *lai48, const struct osmo_location_area_id *lai)
1159{
1160 osmo_plmn_to_bcd(&lai48->digits[0], &lai->plmn);
1161 lai48->lac = osmo_htons(lai->lac);
1162}
1163
1164/*! Decode TS 04.08 Location Area Identifier, legacy implementation.
1165 * Instead use gsm48_decode_lai2(), which is capable of three-digit MNC with leading zeros.
Harald Welte96e2a002017-06-12 21:44:18 +02001166 * \param[in] Location Area Identifier (encoded)
1167 * \param[out] mcc Mobile Country Code
1168 * \param[out] mnc Mobile Network Code
1169 * \param[out] lac Location Area Code
1170 * \returns 0
1171 *
1172 * Attention: this function returns true integers, not hex! */
Harald Welte774a9de2012-07-13 21:35:13 +02001173int gsm48_decode_lai(struct gsm48_loc_area_id *lai, uint16_t *mcc,
1174 uint16_t *mnc, uint16_t *lac)
1175{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001176 struct osmo_location_area_id decoded;
1177 gsm48_decode_lai2(lai, &decoded);
1178 *mcc = decoded.plmn.mcc;
1179 *mnc = decoded.plmn.mnc;
1180 *lac = decoded.lac;
Harald Welte774a9de2012-07-13 21:35:13 +02001181 return 0;
1182}
1183
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001184/*! Decode TS 04.08 Location Area Identifier.
1185 * \param[in] Location Area Identifier (encoded).
1186 * \param[out] decoded Target buffer to write decoded values of MCC-MNC-LAC.
1187 *
1188 * Attention: this function returns true integers, not hex! */
1189void gsm48_decode_lai2(const struct gsm48_loc_area_id *lai, struct osmo_location_area_id *decoded)
1190{
1191 osmo_plmn_from_bcd(&lai->digits[0], &decoded->plmn);
1192 decoded->lac = osmo_ntohs(lai->lac);
1193}
1194
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001195/*! Set DTX mode in Cell Options IE (3GPP TS 44.018)
Maxfe65fa72016-05-10 17:17:05 +02001196 * \param[in] op Cell Options structure in which DTX parameters will be set
1197 * \param[in] full Mode for full-rate channels
1198 * \param[in] half Mode for half-rate channels
1199 * \param[in] is_bcch Indicates if we should use 10.5.2.3.1 instead of
1200 * 10.5.2.3a.2
1201 *
1202 * There is no space for separate DTX settings for Full and Half rate channels
1203 * in BCCH - in this case full setting is used for both and half parameter is
1204 * ignored.
1205 */
1206void gsm48_set_dtx(struct gsm48_cell_options *op, enum gsm48_dtx_mode full,
1207 enum gsm48_dtx_mode half, bool is_bcch)
1208{
1209 if (is_bcch) {
1210 switch (full) {
1211 case GSM48_DTX_MAY_BE_USED:
1212 op->dtx = 0;
1213 return;
1214 case GSM48_DTX_SHALL_BE_USED:
1215 op->dtx = 1;
1216 return;
1217 case GSM48_DTX_SHALL_NOT_BE_USED:
1218 op->dtx = 2;
1219 return;
1220 }
1221 } else {
1222 switch (full) {
1223 case GSM48_DTX_MAY_BE_USED:
1224 op->dtx = (half == GSM48_DTX_SHALL_BE_USED) ? 3 : 0;
1225 op->d = (half == GSM48_DTX_SHALL_NOT_BE_USED) ? 0 : 1;
1226 return;
1227 case GSM48_DTX_SHALL_BE_USED:
1228 op->dtx = (half == GSM48_DTX_MAY_BE_USED) ? 3 : 1;
1229 op->d = (half == GSM48_DTX_SHALL_BE_USED) ? 1 : 0;
1230 return;
1231 case GSM48_DTX_SHALL_NOT_BE_USED:
1232 op->dtx = 2;
1233 op->d = (half == GSM48_DTX_SHALL_BE_USED) ? 1 : 0;
1234 return;
1235 }
1236 }
1237}
1238
Neels Hofmeyr83025bf2020-05-26 02:45:23 +02001239/*! Deprecated, see osmo_mobile_identity instead.
1240 * Generate TS 04.08 Mobile ID from TMSI
Harald Welte96e2a002017-06-12 21:44:18 +02001241 * \param[out] buf Caller-provided output buffer (7 bytes)
1242 * \param[in] tmsi TMSI to be encoded
1243 * \returns number of byes encoded (always 7) */
Harald Welte61e2bfc2010-03-04 10:53:03 +01001244int gsm48_generate_mid_from_tmsi(uint8_t *buf, uint32_t tmsi)
1245{
Harald Weltea13fb752020-06-16 08:44:42 +02001246 uint32_t tmsi_be = osmo_htonl(tmsi);
1247
1248 buf[0] = GSM48_IE_MOBILE_ID;
1249 buf[1] = GSM48_TMSI_LEN;
1250 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
1251 memcpy(&buf[3], &tmsi_be, sizeof(tmsi_be));
1252
1253 return 7;
Harald Welte61e2bfc2010-03-04 10:53:03 +01001254}
1255
Neels Hofmeyr83025bf2020-05-26 02:45:23 +02001256/*! Deprecated, see osmo_mobile_identity instead.
1257 * Generate TS 24.008 §10.5.1.4 Mobile ID of BCD type from ASCII string
Harald Welte1c3bae12019-01-20 10:37:49 +01001258 * \param[out] buf Caller-provided output buffer of at least GSM48_MID_MAX_SIZE bytes
Maxebf14922018-02-15 11:42:11 +01001259 * \param[in] id Identity to be encoded
Harald Welte1c3bae12019-01-20 10:37:49 +01001260 * \param[in] mi_type Type of identity (e.g. GSM_MI_TYPE_IMSI, IMEI, IMEISV)
Maxebf14922018-02-15 11:42:11 +01001261 * \returns number of bytes used in \a buf */
1262uint8_t gsm48_generate_mid(uint8_t *buf, const char *id, uint8_t mi_type)
1263{
Harald Weltea13fb752020-06-16 08:44:42 +02001264 uint8_t length = strnlen(id, 16), i, off = 0, odd = (length & 1) == 1;
1265 /* maximum length == 16 (IMEISV) */
1266
1267 buf[0] = GSM48_IE_MOBILE_ID;
1268 buf[2] = osmo_char2bcd(id[0]) << 4 | (mi_type & GSM_MI_TYPE_MASK) | (odd << 3);
1269
1270 /* if the length is even we will fill half of the last octet */
1271 buf[1] = (length + (odd ? 1 : 2)) >> 1;
1272 /* buf[1] maximum = 18/2 = 9 */
1273 OSMO_ASSERT(buf[1] <= 9);
1274
1275 for (i = 1; i < buf[1]; ++i) {
1276 uint8_t upper, lower = osmo_char2bcd(id[++off]);
1277 if (!odd && off + 1 == length)
1278 upper = 0x0f;
1279 else
1280 upper = osmo_char2bcd(id[++off]) & 0x0f;
1281
1282 buf[2 + i] = (upper << 4) | lower;
Maxebf14922018-02-15 11:42:11 +01001283 }
Harald Weltea13fb752020-06-16 08:44:42 +02001284
1285 /* maximum return value: 2 + 9 = 11 */
1286 return 2 + buf[1];
Maxebf14922018-02-15 11:42:11 +01001287}
1288
Neels Hofmeyr83025bf2020-05-26 02:45:23 +02001289/*! Deprecated, see osmo_mobile_identity instead.
1290 * Generate TS 04.08 Mobile ID from IMSI
Harald Welte96e2a002017-06-12 21:44:18 +02001291 * \param[out] buf Caller-provided output buffer
1292 * \param[in] imsi IMSI to be encoded
1293 * \returns number of bytes used in \a buf */
Harald Welte61e2bfc2010-03-04 10:53:03 +01001294int gsm48_generate_mid_from_imsi(uint8_t *buf, const char *imsi)
1295{
Harald Weltea13fb752020-06-16 08:44:42 +02001296 return gsm48_generate_mid(buf, imsi, GSM_MI_TYPE_IMSI);
Harald Welte61e2bfc2010-03-04 10:53:03 +01001297}
Harald Welte9bb553e2010-03-28 18:14:50 +08001298
Neels Hofmeyr83025bf2020-05-26 02:45:23 +02001299/*! Deprecated, see osmo_mobile_identity instead.
1300 * Convert TS 04.08 Mobile Identity (10.5.1.4) to string.
Neels Hofmeyr627e0112018-12-05 21:32:40 +01001301 * 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 +02001302 * \param[out] string Caller-provided buffer for output
1303 * \param[in] str_len Length of \a string in bytes
1304 * \param[in] mi Mobile Identity to be stringified
1305 * \param[in] mi_len Length of \a mi in bytes
Neels Hofmeyr512bf022020-05-25 23:05:04 +02001306 * \returns Return <= 0 on error, > 0 on success.
1307 * WARNING: the return value of this function is not well implemented.
Neels Hofmeyr6aa20ee2018-12-06 00:40:37 +01001308 * Depending on the MI type and amount of output buffer, this may return
1309 * the nr of written bytes, or the written strlen(), or the snprintf()
Neels Hofmeyr512bf022020-05-25 23:05:04 +02001310 * style strlen()-if-the-buffer-were-large-enough.
1311 */
Vadim Yanitskiy0d8da792019-07-25 19:12:23 +07001312int gsm48_mi_to_string(char *string, int str_len, const uint8_t *mi, int mi_len)
Harald Welte9bb553e2010-03-28 18:14:50 +08001313{
Neels Hofmeyr627e0112018-12-05 21:32:40 +01001314 int rc;
Harald Welte163d0ea2010-04-09 07:57:40 +02001315 uint8_t mi_type;
Harald Welte163d0ea2010-04-09 07:57:40 +02001316 uint32_t tmsi;
Harald Welte9bb553e2010-03-28 18:14:50 +08001317
Neels Hofmeyr6adffb92018-12-05 23:30:31 +01001318 mi_type = (mi && mi_len) ? (mi[0] & GSM_MI_TYPE_MASK) : GSM_MI_TYPE_NONE;
Harald Welte9bb553e2010-03-28 18:14:50 +08001319
1320 switch (mi_type) {
Harald Welte9bb553e2010-03-28 18:14:50 +08001321 case GSM_MI_TYPE_TMSI:
Holger Freyther45abec22016-05-20 19:21:27 +00001322 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
Harald Welte9bb553e2010-03-28 18:14:50 +08001323 if (mi_len == GSM48_TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
Harald Welte95871da2017-05-15 12:11:36 +02001324 tmsi = osmo_load32be(&mi[1]);
Pau Espin Pedrol45735022017-06-18 14:05:24 +02001325 return snprintf(string, str_len, "%"PRIu32, tmsi);
Harald Welte9bb553e2010-03-28 18:14:50 +08001326 }
1327 break;
1328 case GSM_MI_TYPE_IMSI:
1329 case GSM_MI_TYPE_IMEI:
1330 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr627e0112018-12-05 21:32:40 +01001331 rc = osmo_bcd2str(string, str_len, mi,
1332 1, mi_len * 2 - ((mi[0] & GSM_MI_ODD) ? 0 : 1), true);
1333 /* osmo_bcd2str() returns snprintf style strlen(), this returns bytes written. */
1334 if (rc < 0)
1335 return 0;
1336 else if (rc < str_len)
1337 return rc + 1;
1338 else
1339 return strlen(string) + 1;
Neels Hofmeyrea2a0ab2018-12-05 23:30:08 +01001340
Harald Welte9bb553e2010-03-28 18:14:50 +08001341 default:
1342 break;
1343 }
Harald Welte9bb553e2010-03-28 18:14:50 +08001344
Neels Hofmeyrea2a0ab2018-12-05 23:30:08 +01001345 if (str_len < 1)
1346 return 0;
1347 *string = '\0';
1348 return 1;
Harald Welte9bb553e2010-03-28 18:14:50 +08001349}
Harald Weltea1c4f762010-05-01 11:59:42 +02001350
Neels Hofmeyr8d423942023-06-08 00:35:41 +02001351/*! Decode to struct osmo_routing_area_id from a 3GPP TS 24.008 § 10.5.5.15 Routing area identification.
1352 * \param[out] dst Store the decoded result here.
1353 * \param[in] ra_data The start of a Routing Area ID in encoded form, to be decoded.
1354 * \param[in] ra_data_len Buffer size available to read from at *ra_data.
1355 * \return the number of decoded bytes on success, or negative on error (if the input buffer is too small).
1356 */
1357int osmo_routing_area_id_decode(struct osmo_routing_area_id *dst, const uint8_t *ra_data, size_t ra_data_len)
1358{
1359 const struct gsm48_ra_id *ra_id;
1360 if (ra_data_len < sizeof(*ra_id))
1361 return -ENOSPC;
1362
1363 gsm48_decode_lai2((void *)ra_data, &dst->lac);
1364
1365 ra_id = (void *)ra_data;
1366 dst->rac = ra_id->rac;
1367
1368 return sizeof(*ra_id);
1369}
1370
1371/*! Encode struct osmo_routing_area_id to a 3GPP TS 24.008 § 10.5.5.15 Routing area identification: write to a buffer.
1372 * \param[out] buf Return buffer for encoded Mobile Identity.
1373 * \param[in] buflen sizeof(buf).
1374 * \param[in] src RA to encode.
1375 * \return Amount of bytes written to buf, or negative on error.
1376 */
1377int osmo_routing_area_id_encode_buf(uint8_t *buf, size_t buflen, const struct osmo_routing_area_id *src)
1378{
1379 struct gsm48_ra_id *ra_id;
1380 if (buflen < sizeof(*ra_id))
1381 return -ENOSPC;
1382
1383 gsm48_generate_lai2((void *)buf, &src->lac);
1384
1385 ra_id = (void *)buf;
1386 ra_id->rac = src->rac;
1387
1388 return sizeof(*ra_id);
1389}
1390
1391/*! Encode struct osmo_routing_area_id to a 3GPP TS 24.008 § 10.5.5.15 Routing area identification: append to msgb.
1392 * To succeed, the msgb must have tailroom >= sizeof(struct gsm48_ra_id).
1393 * \param[out] msg Append to this msgb.
1394 * \param[in] src Encode this Routing Area ID.
1395 * \return Number of bytes appended to msgb, or negative on error.
1396 */
1397int osmo_routing_area_id_encode_msgb(struct msgb *msg, const struct osmo_routing_area_id *src)
1398{
1399 int rc = osmo_routing_area_id_encode_buf(msg->tail, msgb_tailroom(msg), src);
1400 if (rc <= 0)
1401 return rc;
1402 msgb_put(msg, rc);
1403 return rc;
1404}
1405
1406/*! Parse TS 04.08 Routing Area Identifier.
1407 * Preferably use osmo_routing_area_id_decode() instead: struct osmo_routing_area_id is better integrated with other API
1408 * like osmo_plmn_cmp().
Harald Welte96e2a002017-06-12 21:44:18 +02001409 * \param[out] Caller-provided memory for decoded RA ID
1410 * \param[in] buf Input buffer pointing to RAI IE value */
Harald Weltea1c4f762010-05-01 11:59:42 +02001411void gsm48_parse_ra(struct gprs_ra_id *raid, const uint8_t *buf)
1412{
1413 raid->mcc = (buf[0] & 0xf) * 100;
1414 raid->mcc += (buf[0] >> 4) * 10;
1415 raid->mcc += (buf[1] & 0xf) * 1;
1416
1417 /* I wonder who came up with the stupidity of encoding the MNC
1418 * differently depending on how many digits its decimal number has! */
1419 if ((buf[1] >> 4) == 0xf) {
1420 raid->mnc = (buf[2] & 0xf) * 10;
1421 raid->mnc += (buf[2] >> 4) * 1;
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001422 raid->mnc_3_digits = false;
Harald Weltea1c4f762010-05-01 11:59:42 +02001423 } else {
1424 raid->mnc = (buf[2] & 0xf) * 100;
1425 raid->mnc += (buf[2] >> 4) * 10;
1426 raid->mnc += (buf[1] >> 4) * 1;
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001427 raid->mnc_3_digits = true;
Harald Weltea1c4f762010-05-01 11:59:42 +02001428 }
1429
Harald Welte95871da2017-05-15 12:11:36 +02001430 raid->lac = osmo_load16be(buf + 3);
Harald Weltea1c4f762010-05-01 11:59:42 +02001431 raid->rac = buf[5];
1432}
Harald Welte35a93942010-05-01 14:25:22 +02001433
Maxf1ad60e2018-01-05 14:19:33 +01001434/*! Encode a 3GPP TS 24.008 § 10.5.5.15 Routing area identification
1435 * \param[out] out Caller-provided packed struct
1436 * \param[in] raid Routing Area ID to be encoded
1437 */
1438void gsm48_encode_ra(struct gsm48_ra_id *out, const struct gprs_ra_id *raid)
1439{
1440 out->lac = osmo_htons(raid->lac);
1441 out->rac = raid->rac;
1442
1443 out->digits[0] = ((raid->mcc / 100) % 10) | (((raid->mcc / 10) % 10) << 4);
1444 out->digits[1] = raid->mcc % 10;
1445
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001446 if (raid->mnc < 100 && !raid->mnc_3_digits) {
Maxf1ad60e2018-01-05 14:19:33 +01001447 out->digits[1] |= 0xf0;
1448 out->digits[2] = ((raid->mnc / 10) % 10) | ((raid->mnc % 10) << 4);
1449 } else {
1450 out->digits[1] |= (raid->mnc % 10) << 4;
1451 out->digits[2] = ((raid->mnc / 100) % 10) | (((raid->mnc / 10) % 10) << 4);
1452 }
1453}
1454
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001455/*! Encode a TS 04.08 Routing Area Identifier
Harald Welte96e2a002017-06-12 21:44:18 +02001456 * \param[out] buf Caller-provided output buffer of 6 bytes
1457 * \param[in] raid Routing Area ID to be encoded
1458 * \returns number of bytes used in \a buf */
Harald Welte35a93942010-05-01 14:25:22 +02001459int gsm48_construct_ra(uint8_t *buf, const struct gprs_ra_id *raid)
1460{
Maxf1ad60e2018-01-05 14:19:33 +01001461 gsm48_encode_ra((struct gsm48_ra_id *)buf, raid);
Harald Welte35a93942010-05-01 14:25:22 +02001462
1463 return 6;
1464}
Harald Welte2aee7b12011-06-26 14:20:04 +02001465
Philipp Maierd11a5d52021-02-03 22:11:46 +01001466/*! Compare a TS 04.08 Routing Area Identifier
1467 * \param[in] raid1 first Routing Area ID to compare.
1468 * \param[in] raid2 second Routing Area ID to compare.
1469 * \returns true if raid1 and raid2 match, false otherwise. */
1470bool gsm48_ra_equal(const struct gprs_ra_id *raid1, const struct gprs_ra_id *raid2)
1471{
1472 if (raid1->mcc != raid2->mcc)
1473 return false;
1474 if (raid1->mnc != raid2->mnc)
1475 return false;
1476 if (raid1->mnc_3_digits != raid2->mnc_3_digits)
1477 return false;
1478 if (raid1->lac != raid2->lac)
1479 return false;
1480 if (raid1->rac != raid2->rac)
1481 return false;
1482 return true;
1483}
1484
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001485/*! Determine number of paging sub-channels
Harald Welte96e2a002017-06-12 21:44:18 +02001486 * \param[in] chan_desc Control Channel Description
1487 * \returns number of paging sub-channels
1488 *
1489 * Uses From Table 10.5.33 of GSM 04.08 to determine the number of
1490 * paging sub-channels in the given control channel configuration
1491 */
Pau Espin Pedrol6ca0a432022-11-24 17:09:03 +01001492int gsm48_number_of_paging_subchannels(const struct gsm48_control_channel_descr *chan_desc)
Harald Welte2aee7b12011-06-26 14:20:04 +02001493{
Harald Welte94df39e2011-06-26 14:33:57 +02001494 unsigned int n_pag_blocks = gsm0502_get_n_pag_blocks(chan_desc);
1495
1496 if (chan_desc->ccch_conf == RSL_BCCH_CCCH_CONF_1_C)
1497 return OSMO_MAX(1, n_pag_blocks) * (chan_desc->bs_pa_mfrms + 2);
1498 else
1499 return n_pag_blocks * (chan_desc->bs_pa_mfrms + 2);
Harald Welte2aee7b12011-06-26 14:20:04 +02001500}
Neels Hofmeyrdbd994c2017-03-09 23:07:02 +01001501
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001502/*! TS 04.08 Protocol Descriptor names */
Neels Hofmeyrdbd994c2017-03-09 23:07:02 +01001503const struct value_string gsm48_pdisc_names[] = {
Harald Welteea0bc962018-01-24 16:17:45 +01001504 { GSM48_PDISC_GROUP_CC, "VGCC" },
1505 { GSM48_PDISC_BCAST_CC, "VBCC" },
1506 { GSM48_PDISC_PDSS1, "PDSS1" },
1507 { GSM48_PDISC_CC, "CC" },
1508 { GSM48_PDISC_PDSS2, "PDSS2" },
1509 { GSM48_PDISC_MM, "MM" },
1510 { GSM48_PDISC_RR, "RR" },
1511 { GSM48_PDISC_MM_GPRS, "GMM" },
1512 { GSM48_PDISC_SMS, "SMS" },
1513 { GSM48_PDISC_SM_GPRS, "SM" },
1514 { GSM48_PDISC_NC_SS, "NCSS" },
1515 { GSM48_PDISC_LOC, "LCS" },
1516 { GSM48_PDISC_EXTEND, "EXTD" },
1517 { GSM48_PDISC_MASK, "MASK" },
Neels Hofmeyrdbd994c2017-03-09 23:07:02 +01001518 { 0, NULL }
1519};
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001520
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001521/*! TS 04.08 RR Message Type names */
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001522const struct value_string gsm48_rr_msgtype_names[] = {
1523 OSMO_VALUE_STRING(GSM48_MT_RR_INIT_REQ),
1524 OSMO_VALUE_STRING(GSM48_MT_RR_ADD_ASS),
1525 OSMO_VALUE_STRING(GSM48_MT_RR_IMM_ASS),
1526 OSMO_VALUE_STRING(GSM48_MT_RR_IMM_ASS_EXT),
1527 OSMO_VALUE_STRING(GSM48_MT_RR_IMM_ASS_REJ),
1528 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_ASS_FAIL),
1529 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_REJECT),
1530 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_REQUEST),
1531 OSMO_VALUE_STRING(GSM48_MT_RR_PACKET_ASS),
1532
1533 OSMO_VALUE_STRING(GSM48_MT_RR_CIPH_M_CMD),
1534 OSMO_VALUE_STRING(GSM48_MT_RR_CIPH_M_COMPL),
1535
1536 OSMO_VALUE_STRING(GSM48_MT_RR_CFG_CHG_CMD),
1537 OSMO_VALUE_STRING(GSM48_MT_RR_CFG_CHG_ACK),
1538 OSMO_VALUE_STRING(GSM48_MT_RR_CFG_CHG_REJ),
1539
1540 OSMO_VALUE_STRING(GSM48_MT_RR_ASS_CMD),
1541 OSMO_VALUE_STRING(GSM48_MT_RR_ASS_COMPL),
1542 OSMO_VALUE_STRING(GSM48_MT_RR_ASS_FAIL),
1543 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_CMD),
1544 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_COMPL),
1545 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_FAIL),
1546 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_INFO),
1547 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_INFO),
1548 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_ASS_CMD),
1549
1550 OSMO_VALUE_STRING(GSM48_MT_RR_CELL_CHG_ORDER),
1551 OSMO_VALUE_STRING(GSM48_MT_RR_PDCH_ASS_CMD),
1552
1553 OSMO_VALUE_STRING(GSM48_MT_RR_CHAN_REL),
1554 OSMO_VALUE_STRING(GSM48_MT_RR_PART_REL),
1555 OSMO_VALUE_STRING(GSM48_MT_RR_PART_REL_COMP),
1556
1557 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_REQ_1),
1558 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_REQ_2),
1559 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_REQ_3),
1560 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_RESP),
1561 OSMO_VALUE_STRING(GSM48_MT_RR_NOTIF_NCH),
1562 OSMO_VALUE_STRING(GSM48_MT_RR_NOTIF_FACCH),
1563 OSMO_VALUE_STRING(GSM48_MT_RR_NOTIF_RESP),
1564 OSMO_VALUE_STRING(GSM48_MT_RR_PACKET_NOTIF),
1565 OSMO_VALUE_STRING(GSM48_MT_RR_UTRAN_CLSM_CHG),
1566 OSMO_VALUE_STRING(GSM48_MT_RR_CDMA2K_CLSM_CHG),
1567 OSMO_VALUE_STRING(GSM48_MT_RR_IS_TO_UTRAN_HANDO),
1568 OSMO_VALUE_STRING(GSM48_MT_RR_IS_TO_CDMA2K_HANDO),
1569
1570 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_8),
1571 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_1),
1572 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2),
1573 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_3),
1574 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_4),
1575 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_5),
1576 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_6),
1577 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_7),
1578
1579 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2bis),
1580 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2ter),
1581 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2quater),
1582 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_5bis),
1583 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_5ter),
1584 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_9),
1585 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_13),
1586
1587 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_16),
1588 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_17),
1589
1590 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_18),
1591 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_19),
1592 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_20),
1593
1594 OSMO_VALUE_STRING(GSM48_MT_RR_CHAN_MODE_MODIF),
1595 OSMO_VALUE_STRING(GSM48_MT_RR_STATUS),
1596 OSMO_VALUE_STRING(GSM48_MT_RR_CHAN_MODE_MODIF_ACK),
1597 OSMO_VALUE_STRING(GSM48_MT_RR_FREQ_REDEF),
1598 OSMO_VALUE_STRING(GSM48_MT_RR_MEAS_REP),
1599 OSMO_VALUE_STRING(GSM48_MT_RR_CLSM_CHG),
1600 OSMO_VALUE_STRING(GSM48_MT_RR_CLSM_ENQ),
1601 OSMO_VALUE_STRING(GSM48_MT_RR_EXT_MEAS_REP),
1602 OSMO_VALUE_STRING(GSM48_MT_RR_EXT_MEAS_REP_ORD),
1603 OSMO_VALUE_STRING(GSM48_MT_RR_GPRS_SUSP_REQ),
1604 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_INFO),
1605
1606 OSMO_VALUE_STRING(GSM48_MT_RR_VGCS_UPL_GRANT),
1607 OSMO_VALUE_STRING(GSM48_MT_RR_UPLINK_RELEASE),
1608 OSMO_VALUE_STRING(GSM48_MT_RR_UPLINK_FREE),
1609 OSMO_VALUE_STRING(GSM48_MT_RR_UPLINK_BUSY),
1610 OSMO_VALUE_STRING(GSM48_MT_RR_TALKER_IND),
1611 { 0, NULL }
1612};
1613
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001614/*! TS 04.08 MM Message Type names */
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001615const struct value_string gsm48_mm_msgtype_names[] = {
1616 OSMO_VALUE_STRING(GSM48_MT_MM_IMSI_DETACH_IND),
1617 OSMO_VALUE_STRING(GSM48_MT_MM_LOC_UPD_ACCEPT),
1618 OSMO_VALUE_STRING(GSM48_MT_MM_LOC_UPD_REJECT),
1619 OSMO_VALUE_STRING(GSM48_MT_MM_LOC_UPD_REQUEST),
1620
1621 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_REJ),
1622 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_REQ),
1623 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_RESP),
1624 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_FAIL),
1625 OSMO_VALUE_STRING(GSM48_MT_MM_ID_REQ),
1626 OSMO_VALUE_STRING(GSM48_MT_MM_ID_RESP),
1627 OSMO_VALUE_STRING(GSM48_MT_MM_TMSI_REALL_CMD),
1628 OSMO_VALUE_STRING(GSM48_MT_MM_TMSI_REALL_COMPL),
1629
1630 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_ACC),
1631 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_REJ),
1632 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_ABORT),
1633 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_REQ),
1634 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_PROMPT),
1635 OSMO_VALUE_STRING(GSM48_MT_MM_CM_REEST_REQ),
1636 OSMO_VALUE_STRING(GSM48_MT_MM_ABORT),
1637
1638 OSMO_VALUE_STRING(GSM48_MT_MM_NULL),
1639 OSMO_VALUE_STRING(GSM48_MT_MM_STATUS),
1640 OSMO_VALUE_STRING(GSM48_MT_MM_INFO),
1641 { 0, NULL }
1642};
1643
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001644/*! TS 04.08 CC Message Type names */
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001645const struct value_string gsm48_cc_msgtype_names[] = {
1646 OSMO_VALUE_STRING(GSM48_MT_CC_ALERTING),
1647 OSMO_VALUE_STRING(GSM48_MT_CC_CALL_CONF),
1648 OSMO_VALUE_STRING(GSM48_MT_CC_CALL_PROC),
1649 OSMO_VALUE_STRING(GSM48_MT_CC_CONNECT),
1650 OSMO_VALUE_STRING(GSM48_MT_CC_CONNECT_ACK),
1651 OSMO_VALUE_STRING(GSM48_MT_CC_EMERG_SETUP),
1652 OSMO_VALUE_STRING(GSM48_MT_CC_PROGRESS),
1653 OSMO_VALUE_STRING(GSM48_MT_CC_ESTAB),
1654 OSMO_VALUE_STRING(GSM48_MT_CC_ESTAB_CONF),
1655 OSMO_VALUE_STRING(GSM48_MT_CC_RECALL),
1656 OSMO_VALUE_STRING(GSM48_MT_CC_START_CC),
1657 OSMO_VALUE_STRING(GSM48_MT_CC_SETUP),
1658
1659 OSMO_VALUE_STRING(GSM48_MT_CC_MODIFY),
1660 OSMO_VALUE_STRING(GSM48_MT_CC_MODIFY_COMPL),
1661 OSMO_VALUE_STRING(GSM48_MT_CC_MODIFY_REJECT),
1662 OSMO_VALUE_STRING(GSM48_MT_CC_USER_INFO),
1663 OSMO_VALUE_STRING(GSM48_MT_CC_HOLD),
1664 OSMO_VALUE_STRING(GSM48_MT_CC_HOLD_ACK),
1665 OSMO_VALUE_STRING(GSM48_MT_CC_HOLD_REJ),
1666 OSMO_VALUE_STRING(GSM48_MT_CC_RETR),
1667 OSMO_VALUE_STRING(GSM48_MT_CC_RETR_ACK),
1668 OSMO_VALUE_STRING(GSM48_MT_CC_RETR_REJ),
1669
1670 OSMO_VALUE_STRING(GSM48_MT_CC_DISCONNECT),
1671 OSMO_VALUE_STRING(GSM48_MT_CC_RELEASE),
1672 OSMO_VALUE_STRING(GSM48_MT_CC_RELEASE_COMPL),
1673
1674 OSMO_VALUE_STRING(GSM48_MT_CC_CONG_CTRL),
1675 OSMO_VALUE_STRING(GSM48_MT_CC_NOTIFY),
1676 OSMO_VALUE_STRING(GSM48_MT_CC_STATUS),
1677 OSMO_VALUE_STRING(GSM48_MT_CC_STATUS_ENQ),
1678 OSMO_VALUE_STRING(GSM48_MT_CC_START_DTMF),
1679 OSMO_VALUE_STRING(GSM48_MT_CC_STOP_DTMF),
1680 OSMO_VALUE_STRING(GSM48_MT_CC_STOP_DTMF_ACK),
1681 OSMO_VALUE_STRING(GSM48_MT_CC_START_DTMF_ACK),
1682 OSMO_VALUE_STRING(GSM48_MT_CC_START_DTMF_REJ),
1683 OSMO_VALUE_STRING(GSM48_MT_CC_FACILITY),
1684 { 0, NULL }
1685};
1686
Keith3cdaa8d2018-08-31 20:09:18 +02001687/*! TS 04.08 10.5..4.11 Call Control Cause Values */
1688const struct value_string gsm48_cc_cause_names[] = {
1689 { GSM48_CC_CAUSE_UNASSIGNED_NR, "UNASSIGNED_NR" },
1690 { GSM48_CC_CAUSE_NO_ROUTE, "NO_ROUTE" },
1691 { GSM48_CC_CAUSE_CHAN_UNACCEPT, "CHAN_UNACCEPT" },
1692 { GSM48_CC_CAUSE_OP_DET_BARRING, "OP_DET_BARRING" },
1693 { GSM48_CC_CAUSE_NORM_CALL_CLEAR, "NORM_CALL_CLEAR" },
1694 { GSM48_CC_CAUSE_USER_BUSY, "USER_BUSY" },
1695 { GSM48_CC_CAUSE_USER_NOTRESPOND, "USER_NOTRESPOND" },
1696 { GSM48_CC_CAUSE_USER_ALERTING_NA, "USER_ALERTING_NA" },
1697 { GSM48_CC_CAUSE_CALL_REJECTED, "CALL_REJECTED" },
1698 { GSM48_CC_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED" },
1699 { GSM48_CC_CAUSE_PRE_EMPTION, "PRE_EMPTION" },
1700 { GSM48_CC_CAUSE_NONSE_USER_CLR, "NONSE_USER_CLR" },
1701 { GSM48_CC_CAUSE_DEST_OOO, "DEST_OOO" },
1702 { GSM48_CC_CAUSE_INV_NR_FORMAT, "INV_NR_FORMAT" },
1703 { GSM48_CC_CAUSE_FACILITY_REJ, "FACILITY_REJ" },
1704 { GSM48_CC_CAUSE_RESP_STATUS_INQ, "RESP_STATUS_INQ" },
1705 { GSM48_CC_CAUSE_NORMAL_UNSPEC, "NORMAL_UNSPEC" },
1706 { GSM48_CC_CAUSE_NO_CIRCUIT_CHAN, "NO_CIRCUIT_CHAN" },
1707 { GSM48_CC_CAUSE_NETWORK_OOO, "NETWORK_OOO" },
1708 { GSM48_CC_CAUSE_TEMP_FAILURE, "TEMP_FAILURE" },
1709 { GSM48_CC_CAUSE_SWITCH_CONG, "SWITCH_CONG" },
1710 { GSM48_CC_CAUSE_ACC_INF_DISCARD, "ACC_INF_DISCARD" },
1711 { GSM48_CC_CAUSE_REQ_CHAN_UNAVAIL, "REQ_CHAN_UNAVAIL" },
1712 { GSM48_CC_CAUSE_RESOURCE_UNAVAIL, "RESOURCE_UNAVAIL" },
1713 { GSM48_CC_CAUSE_QOS_UNAVAIL, "QOS_UNAVAIL" },
1714 { GSM48_CC_CAUSE_REQ_FAC_NOT_SUBSC, "REQ_FAC_NOT_SUBSC" },
1715 { GSM48_CC_CAUSE_INC_BARRED_CUG, "INC_BARRED_CUG" },
1716 { GSM48_CC_CAUSE_BEARER_CAP_UNAUTH, "BEARER_CAP_UNAUTH" },
1717 { GSM48_CC_CAUSE_BEARER_CA_UNAVAIL, "BEARER_CA_UNAVAIL" },
1718 { GSM48_CC_CAUSE_SERV_OPT_UNAVAIL, "SERV_OPT_UNAVAIL" },
1719 { GSM48_CC_CAUSE_BEARERSERV_UNIMPL, "BEARERSERV_UNIMPL" },
1720 { GSM48_CC_CAUSE_ACM_GE_ACM_MAX, "ACM_GE_ACM_MAX" },
1721 { GSM48_CC_CAUSE_REQ_FAC_NOTIMPL, "REQ_FAC_NOTIMPL" },
1722 { GSM48_CC_CAUSE_RESTR_BCAP_AVAIL, "RESTR_BCAP_AVAIL" },
1723 { GSM48_CC_CAUSE_SERV_OPT_UNIMPL, "SERV_OPT_UNIMPL" },
1724 { GSM48_CC_CAUSE_INVAL_TRANS_ID, "INVAL_TRANS_ID" },
1725 { GSM48_CC_CAUSE_USER_NOT_IN_CUG, "USER_NOT_IN_CUG" },
1726 { GSM48_CC_CAUSE_INCOMPAT_DEST, "INCOMPAT_DEST" },
1727 { GSM48_CC_CAUSE_INVAL_TRANS_NET, "INVAL_TRANS_NET" },
1728 { GSM48_CC_CAUSE_SEMANTIC_INCORR, "SEMANTIC_INCORR" },
1729 { GSM48_CC_CAUSE_INVAL_MAND_INF, "INVAL_MAND_INF" },
1730 { GSM48_CC_CAUSE_MSGTYPE_NOTEXIST, "MSGTYPE_NOTEXIST" },
1731 { GSM48_CC_CAUSE_MSGTYPE_INCOMPAT, "MSGTYPE_INCOMPAT" },
1732 { GSM48_CC_CAUSE_IE_NOTEXIST, "IE_NOTEXIST" },
1733 { GSM48_CC_CAUSE_COND_IE_ERR, "COND_IE_ERR" },
1734 { GSM48_CC_CAUSE_MSG_INCOMP_STATE, "MSG_INCOMP_STATE" },
1735 { GSM48_CC_CAUSE_RECOVERY_TIMER, "RECOVERY_TIMER" },
1736 { GSM48_CC_CAUSE_PROTO_ERR, "PROTO_ERR" },
1737 { GSM48_CC_CAUSE_INTERWORKING, "INTERWORKING" },
1738 { 0 , NULL }
1739};
1740
Vadim Yanitskiy8d41d722018-04-17 11:17:28 +07001741/*! TS 04.80, section 3.4 Messages for supplementary services control */
1742const struct value_string gsm48_nc_ss_msgtype_names[] = {
1743 OSMO_VALUE_STRING(GSM0480_MTYPE_RELEASE_COMPLETE),
1744 OSMO_VALUE_STRING(GSM0480_MTYPE_FACILITY),
1745 OSMO_VALUE_STRING(GSM0480_MTYPE_REGISTER),
1746 { 0, NULL }
1747};
1748
Harald Welte4a62eda2019-03-18 18:27:00 +01001749/*! Compose a string naming the message type for given protocol, in a caller-provided buffer.
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001750 * If the message type string is known, return the message type name, otherwise
1751 * return "<protocol discriminator name>:<message type in hex>".
Harald Welte4a62eda2019-03-18 18:27:00 +01001752 * \param[out] buf caller-allcated output string buffer
1753 * \param[in] buf_len size of buf in bytes
Harald Welte96e2a002017-06-12 21:44:18 +02001754 * \param[in] pdisc protocol discriminator like GSM48_PDISC_MM
1755 * \param[in] msg_type message type like GSM48_MT_MM_LOC_UPD_REQUEST
Harald Welte4a62eda2019-03-18 18:27:00 +01001756 * \returns buf
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001757 */
Harald Welte4a62eda2019-03-18 18:27:00 +01001758char *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 +01001759{
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001760 const struct value_string *msgt_names;
1761
1762 switch (pdisc) {
1763 case GSM48_PDISC_RR:
1764 msgt_names = gsm48_rr_msgtype_names;
1765 break;
1766 case GSM48_PDISC_MM:
1767 msgt_names = gsm48_mm_msgtype_names;
1768 break;
1769 case GSM48_PDISC_CC:
1770 msgt_names = gsm48_cc_msgtype_names;
1771 break;
Andreas Eversberg72ab3b52023-06-17 16:15:56 +02001772 case GSM48_PDISC_GROUP_CC:
1773 case GSM48_PDISC_BCAST_CC:
1774 msgt_names = osmo_gsm44068_msg_type_names;
1775 break;
Vadim Yanitskiy8d41d722018-04-17 11:17:28 +07001776 case GSM48_PDISC_NC_SS:
1777 msgt_names = gsm48_nc_ss_msgtype_names;
1778 break;
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001779 default:
1780 msgt_names = NULL;
1781 break;
1782 }
1783
1784 if (msgt_names)
Harald Welte4a62eda2019-03-18 18:27:00 +01001785 snprintf(buf, buf_len, "%s", get_value_string(msgt_names, msg_type));
1786 else
1787 snprintf(buf, buf_len, "%s:0x%02x", gsm48_pdisc_name(pdisc), msg_type);
1788 return buf;
1789}
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001790
Harald Welte4a62eda2019-03-18 18:27:00 +01001791/*! Compose a string naming the message type for given protocol, in a static buffer.
1792 * If the message type string is known, return the message type name, otherwise
1793 * return "<protocol discriminator name>:<message type in hex>".
1794 * \param[in] pdisc protocol discriminator like GSM48_PDISC_MM
1795 * \param[in] msg_type message type like GSM48_MT_MM_LOC_UPD_REQUEST
1796 * \returns statically allocated string or string constant.
1797 */
1798const char *gsm48_pdisc_msgtype_name(uint8_t pdisc, uint8_t msg_type)
1799{
Harald Welte171ef822019-03-28 10:49:05 +01001800 static __thread char namebuf[64];
Harald Welte4a62eda2019-03-18 18:27:00 +01001801 return gsm48_pdisc_msgtype_name_buf(namebuf, sizeof(namebuf), pdisc, msg_type);
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001802}
Harald Welte96e2a002017-06-12 21:44:18 +02001803
Harald Welte179f3572019-03-18 18:38:47 +01001804/*! Compose a string naming the message type for given protocol, in a dynamically-allocated buffer.
1805 * If the message type string is known, return the message type name, otherwise
1806 * return "<protocol discriminator name>:<message type in hex>".
1807 * \param[in] ctx talloc context from which to allocate output buffer
1808 * \param[in] pdisc protocol discriminator like GSM48_PDISC_MM
1809 * \param[in] msg_type message type like GSM48_MT_MM_LOC_UPD_REQUEST
1810 * \returns string representation in dynamically allocated output buffer.
1811 */
1812char *gsm48_pdisc_msgtype_name_c(const void *ctx, uint8_t pdisc, uint8_t msg_type)
1813{
1814 char *namebuf = talloc_size(ctx, 64);
1815 if (!namebuf)
1816 return NULL;
1817 return gsm48_pdisc_msgtype_name_buf(namebuf, 64, pdisc, msg_type);
1818}
1819
Neels Hofmeyraead2412018-04-06 04:31:00 +02001820const struct value_string gsm48_reject_value_names[] = {
1821 { GSM48_REJECT_IMSI_UNKNOWN_IN_HLR, "IMSI_UNKNOWN_IN_HLR" },
1822 { GSM48_REJECT_ILLEGAL_MS, "ILLEGAL_MS" },
1823 { GSM48_REJECT_IMSI_UNKNOWN_IN_VLR, "IMSI_UNKNOWN_IN_VLR" },
1824 { GSM48_REJECT_IMEI_NOT_ACCEPTED, "IMEI_NOT_ACCEPTED" },
1825 { GSM48_REJECT_ILLEGAL_ME, "ILLEGAL_ME" },
1826 { GSM48_REJECT_PLMN_NOT_ALLOWED, "PLMN_NOT_ALLOWED" },
1827 { GSM48_REJECT_LOC_NOT_ALLOWED, "LOC_NOT_ALLOWED" },
1828 { GSM48_REJECT_ROAMING_NOT_ALLOWED, "ROAMING_NOT_ALLOWED" },
1829 { GSM48_REJECT_NETWORK_FAILURE, "NETWORK_FAILURE" },
1830 { GSM48_REJECT_SYNCH_FAILURE, "SYNCH_FAILURE" },
1831 { GSM48_REJECT_CONGESTION, "CONGESTION" },
1832 { GSM48_REJECT_SRV_OPT_NOT_SUPPORTED, "SRV_OPT_NOT_SUPPORTED" },
1833 { GSM48_REJECT_RQD_SRV_OPT_NOT_SUPPORTED, "RQD_SRV_OPT_NOT_SUPPORTED" },
1834 { GSM48_REJECT_SRV_OPT_TMP_OUT_OF_ORDER, "SRV_OPT_TMP_OUT_OF_ORDER" },
1835 { GSM48_REJECT_CALL_CAN_NOT_BE_IDENTIFIED, "CALL_CAN_NOT_BE_IDENTIFIED" },
1836 { GSM48_REJECT_INCORRECT_MESSAGE, "INCORRECT_MESSAGE" },
1837 { GSM48_REJECT_INVALID_MANDANTORY_INF, "INVALID_MANDANTORY_INF" },
1838 { GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED, "MSG_TYPE_NOT_IMPLEMENTED" },
1839 { GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE, "MSG_TYPE_NOT_COMPATIBLE" },
1840 { GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED, "INF_ELEME_NOT_IMPLEMENTED" },
1841 { GSM48_REJECT_CONDTIONAL_IE_ERROR, "CONDTIONAL_IE_ERROR" },
1842 { GSM48_REJECT_MSG_NOT_COMPATIBLE, "MSG_NOT_COMPATIBLE" },
1843 { GSM48_REJECT_PROTOCOL_ERROR, "PROTOCOL_ERROR" },
1844 { GSM48_REJECT_GPRS_NOT_ALLOWED, "GPRS_NOT_ALLOWED" },
1845 { GSM48_REJECT_SERVICES_NOT_ALLOWED, "SERVICES_NOT_ALLOWED" },
1846 { GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE, "MS_IDENTITY_NOT_DERVIVABLE" },
1847 { GSM48_REJECT_IMPLICITLY_DETACHED, "IMPLICITLY_DETACHED" },
1848 { GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN, "GPRS_NOT_ALLOWED_IN_PLMN" },
1849 { GSM48_REJECT_MSC_TMP_NOT_REACHABLE, "MSC_TMP_NOT_REACHABLE" },
1850 { 0, NULL }
1851};
1852
Vadim Yanitskiy30cfeeb2018-08-03 05:44:00 +07001853/*! Wrap a given \ref msg with \ref gsm48_hdr structure
1854 * \param[out] msg A message to be wrapped
1855 * \param[in] pdisc GSM TS 04.07 protocol discriminator 1/2,
1856 * sub-pdisc, trans_id or skip_ind 1/2,
1857 * see section 11.2.3.1 for details
1858 * \param[in] msg_type GSM TS 04.08 message type
1859 * @return pointer to pushed header within \ref msg
1860 */
1861struct gsm48_hdr *gsm48_push_l3hdr(struct msgb *msg,
1862 uint8_t pdisc, uint8_t msg_type)
1863{
1864 struct gsm48_hdr *gh;
1865
1866 gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
1867 gh->proto_discr = pdisc;
1868 gh->msg_type = msg_type;
1869
1870 return gh;
1871}
1872
Neels Hofmeyr92f3f5e2019-01-05 00:39:13 +01001873const struct value_string osmo_lu_type_names[] = {
1874 { GSM48_LUPD_NORMAL, "NORMAL" },
1875 { GSM48_LUPD_PERIODIC, "PERIODIC" },
1876 { GSM48_LUPD_IMSI_ATT, "IMSI-ATTACH" },
1877 { GSM48_LUPD_RESERVED, "RESERVED" },
1878 {}
1879};
1880
Neels Hofmeyrf8963f92019-01-10 23:33:32 +01001881const struct value_string osmo_cm_service_type_names[] = {
1882 { GSM48_CMSERV_MO_CALL_PACKET, "MO-Call" },
1883 { GSM48_CMSERV_EMERGENCY, "Emergency-Call" },
1884 { GSM48_CMSERV_SMS, "Short-Messaging-Service" },
1885 { GSM48_CMSERV_SUP_SERV, "Supplementary-Service" },
1886 { GSM48_CMSERV_VGCS, "Voice-Group-Call" },
1887 { GSM48_CMSERV_VBS, "Voice-Broadcast-Call" },
1888 { GSM48_CMSERV_LOC_SERV, "Location-Service" },
1889 {}
1890};
1891
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001892bool osmo_gsm48_classmark1_is_r99(const struct gsm48_classmark1 *cm1)
1893{
1894 return cm1->rev_lev >= 2;
1895}
1896
1897bool osmo_gsm48_classmark2_is_r99(const struct gsm48_classmark2 *cm2, uint8_t cm2_len)
1898{
1899 if (!cm2_len)
1900 return false;
1901 return cm2->rev_lev >= 2;
1902}
1903
1904/*! Return true if any of Classmark 1 or Classmark 2 are present and indicate R99 capability.
1905 * \param[in] cm Classmarks.
1906 * \returns True if R99 or later, false if pre-R99 or no Classmarks are present.
1907 */
1908bool osmo_gsm48_classmark_is_r99(const struct osmo_gsm48_classmark *cm)
1909{
1910 if (cm->classmark1_set)
1911 return osmo_gsm48_classmark1_is_r99(&cm->classmark1);
1912 return osmo_gsm48_classmark2_is_r99(&cm->classmark2, cm->classmark2_len);
1913}
1914
1915/*! Return a string representation of A5 cipher algorithms indicated by Classmark 1, 2 and 3.
1916 * \param[in] cm Classmarks.
1917 * \returns A statically allocated string like "cm1{a5/1=supported} cm2{0x23= A5/2 A5/3} no-cm3"
1918 */
Harald Welte4a62eda2019-03-18 18:27:00 +01001919char *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 +01001920{
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001921 char cm1[42] = "no-cm1";
1922 char cm2[42] = " no-cm2";
Neels Hofmeyr9e6f5f12019-03-11 05:07:56 +01001923 char cm3[42] = " no-cm3";
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001924
1925 if (cm->classmark1_set)
1926 snprintf(cm1, sizeof(cm1), "cm1{a5/1=%s}",
1927 cm->classmark1.a5_1 ? "not-supported":"supported" /* inverted logic */);
1928
1929 if (cm->classmark2_len >= 3)
1930 snprintf(cm2, sizeof(cm2), " cm2{0x%x=%s%s}",
1931 cm->classmark2.a5_2 + (cm->classmark2.a5_3 << 1),
1932 cm->classmark2.a5_2 ? " A5/2" : "",
1933 cm->classmark2.a5_3 ? " A5/3" : "");
1934
1935 if (cm->classmark3_len >= 1)
1936 snprintf(cm3, sizeof(cm3), " cm3{0x%x=%s%s%s%s}",
1937 cm->classmark3[0],
1938 cm->classmark3[0] & (1 << 0) ? " A5/4" : "",
1939 cm->classmark3[0] & (1 << 1) ? " A5/5" : "",
1940 cm->classmark3[0] & (1 << 2) ? " A5/6" : "",
1941 cm->classmark3[0] & (1 << 3) ? " A5/7" : "");
1942
Harald Welte4a62eda2019-03-18 18:27:00 +01001943 snprintf(buf, buf_len, "%s%s%s", cm1, cm2, cm3);
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001944 return buf;
1945}
1946
Harald Welte4a62eda2019-03-18 18:27:00 +01001947/*! Return a string representation of A5 cipher algorithms indicated by Classmark 1, 2 and 3.
1948 * \param[in] cm Classmarks.
1949 * \returns A statically allocated string like "cm1{a5/1=supported} cm2{0x23= A5/2 A5/3} no-cm3"
1950 */
1951const char *osmo_gsm48_classmark_a5_name(const struct osmo_gsm48_classmark *cm)
1952{
Harald Welte171ef822019-03-28 10:49:05 +01001953 static __thread char buf[128];
Harald Welte4a62eda2019-03-18 18:27:00 +01001954 return osmo_gsm48_classmark_a5_name_buf(buf, sizeof(buf), cm);
1955}
1956
Harald Welte179f3572019-03-18 18:38:47 +01001957/*! Return a string representation of A5 cipher algorithms indicated by Classmark 1, 2 and 3.
1958 * \param[in] ctx talloc context from which to allocate output buffer
1959 * \param[in] cm Classmarks.
1960 * \returns string like "cm1{a5/1=supported} cm2{0x23= A5/2 A5/3} no-cm3" in dynamically-allocated
1961 * output buffer.
1962 */
1963char *osmo_gsm48_classmark_a5_name_c(const void *ctx, const struct osmo_gsm48_classmark *cm)
1964{
1965 char *buf = talloc_size(ctx, 128);
1966 if (!buf)
1967 return NULL;
1968 return osmo_gsm48_classmark_a5_name_buf(buf, 128, cm);
1969}
Harald Welte4a62eda2019-03-18 18:27:00 +01001970
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001971/*! Overwrite dst with the Classmark information present in src.
1972 * Add an new Classmark and overwrite in dst what src has to offer, but where src has no Classmark information, leave
1973 * dst unchanged. (For Classmark 2 and 3, dst will exactly match any non-zero Classmark length from src, hence may end
1974 * up with a shorter Classmark after this call.)
1975 * \param[out] dst The target Classmark storage to be updated.
1976 * \param[in] src The new Classmark information to read from.
1977 */
1978void osmo_gsm48_classmark_update(struct osmo_gsm48_classmark *dst, const struct osmo_gsm48_classmark *src)
1979{
1980 if (src->classmark1_set) {
1981 dst->classmark1 = src->classmark1;
1982 dst->classmark1_set = true;
1983 }
1984 if (src->classmark2_len) {
1985 dst->classmark2_len = src->classmark2_len;
1986 dst->classmark2 = src->classmark2;
1987 }
1988 if (src->classmark3_len) {
1989 dst->classmark3_len = src->classmark3_len;
1990 memcpy(dst->classmark3, src->classmark3, OSMO_MIN(sizeof(dst->classmark3), src->classmark3_len));
1991 }
1992}
1993
1994
1995/*! Determine if the given Classmark (1/2/3) value permits a given A5/n cipher.
1996 * \param[in] cm Classmarks.
1997 * \param[in] a5 The N in A5/N for which to query whether support is indicated.
1998 * \return 1 when the given A5/n is permitted, 0 when not (or a5 > 7), and negative if the respective MS Classmark is
1999 * not known, where the negative number indicates the classmark type: -2 means Classmark 2 is not available. The
2000 * idea is that when e.g. A5/3 is requested and the corresponding Classmark 3 is not available, that the caller
2001 * can react by obtaining Classmark 3 and calling again once it is available.
2002 */
2003int osmo_gsm48_classmark_supports_a5(const struct osmo_gsm48_classmark *cm, uint8_t a5)
2004{
2005 switch (a5) {
2006 case 0:
2007 /* all phones must implement A5/0, see 3GPP TS 43.020 4.9 */
2008 return 1;
2009 case 1:
2010 /* 3GPP TS 43.020 4.9 requires A5/1 to be suppored by all phones and actually states:
2011 * "The network shall not provide service to an MS which indicates that it does not
2012 * support the ciphering algorithm A5/1.". However, let's be more tolerant based
2013 * on policy here */
2014 /* See 3GPP TS 24.008 10.5.1.7 */
2015 if (!cm->classmark1_set)
2016 return -1;
2017 /* Inverted logic for this bit! */
2018 return cm->classmark1.a5_1 ? 0 : 1;
2019 case 2:
2020 /* See 3GPP TS 24.008 10.5.1.6 */
2021 if (cm->classmark2_len < 3)
2022 return -2;
2023 return cm->classmark2.a5_2 ? 1 : 0;
2024 case 3:
2025 if (cm->classmark2_len < 3)
2026 return -2;
2027 return cm->classmark2.a5_3 ? 1 : 0;
2028 case 4:
2029 case 5:
2030 case 6:
2031 case 7:
2032 /* See 3GPP TS 24.008 10.5.1.7 */
2033 if (!cm->classmark3_len)
2034 return -3;
2035 return (cm->classmark3[0] & (1 << (a5-4))) ? 1 : 0;
2036 default:
2037 return 0;
2038 }
2039}
2040
Pau Espin Pedrolb99f4ca2019-10-31 13:35:22 +01002041/*! Decode power class from Classmark1/2 RF power capability field.
2042 * \param[in] rf_power_cap The RF power capability field (3 bits).
2043 * \param[in] band the band of the arfcn from where the classmark was received
2044 * \return the MS power class on success, negative on error.
2045 */
2046int8_t osmo_gsm48_rfpowercap2powerclass(enum gsm_band band, uint8_t rf_power_cap)
2047{
2048 switch (band) {
2049 case GSM_BAND_1800:
2050 case GSM_BAND_1900:
2051 if (rf_power_cap > 2)
2052 return -1;
2053 return rf_power_cap + 1;
2054 default:
2055 if (rf_power_cap > 4)
2056 return -1;
2057 return rf_power_cap + 1;
2058 }
2059}
2060
2061
Harald Welte96e2a002017-06-12 21:44:18 +02002062/*! @} */