blob: 4f79026161ce2cff680ceae5ff5fb70d5dca55f3 [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
644 if (!mi_data || mi_len < 1)
645 return -EBADMSG;
646
647 nibbles_len = (mi_len - 1) * 2 + ((mi_data[0] & GSM_MI_ODD) ? 1 : 0);
648
649 *mi = (struct osmo_mobile_identity){
650 .type = mi_data[0] & GSM_MI_TYPE_MASK,
651 };
652
653 /* First do length checks */
654 switch (mi->type) {
655 case GSM_MI_TYPE_TMSI:
656 mi->tmsi = GSM_RESERVED_TMSI;
657 if (nibbles_len != (GSM23003_TMSI_NUM_BYTES * 2)) {
658 rc = -EBADMSG;
659 goto return_error;
660 }
661 break;
662
663 case GSM_MI_TYPE_IMSI:
664 if (nibbles_len < GSM23003_IMSI_MIN_DIGITS || nibbles_len > GSM23003_IMSI_MAX_DIGITS) {
665 rc = -EBADMSG;
666 goto return_error;
667 }
668 str = mi->imsi;
669 str_size = sizeof(mi->imsi);
670 break;
671
672 case GSM_MI_TYPE_IMEI:
673 if (nibbles_len != GSM23003_IMEI_NUM_DIGITS && nibbles_len != GSM23003_IMEI_NUM_DIGITS_NO_CHK) {
674 rc = -EBADMSG;
675 goto return_error;
676 }
677 str = mi->imei;
678 str_size = sizeof(mi->imei);
679 break;
680
681 case GSM_MI_TYPE_IMEISV:
682 if (nibbles_len != GSM23003_IMEISV_NUM_DIGITS) {
683 rc = -EBADMSG;
684 goto return_error;
685 }
686 str = mi->imeisv;
687 str_size = sizeof(mi->imeisv);
688 break;
689
690 default:
691 rc = -EINVAL;
692 goto return_error;
693 }
694
695 /* Decode BCD digits */
696 switch (mi->type) {
697 case GSM_MI_TYPE_TMSI:
698 /* MI is a 32bit integer TMSI. Length has been checked above. */
699 if ((mi_data[0] & 0xf0) != 0xf0) {
700 /* A TMSI always has the first nibble == 0xf */
701 rc = -EBADMSG;
702 goto return_error;
703 }
704 mi->tmsi = osmo_load32be(&mi_data[1]);
705 return 0;
706
707 case GSM_MI_TYPE_IMSI:
708 case GSM_MI_TYPE_IMEI:
709 case GSM_MI_TYPE_IMEISV:
710 /* If the length is even, the last nibble (higher nibble of last octet) must be 0xf */
711 if (!(mi_data[0] & GSM_MI_ODD)
712 && ((mi_data[mi_len - 1] & 0xf0) != 0xf0)) {
713 rc = -EBADMSG;
714 goto return_error;
715 }
716 rc = osmo_bcd2str(str, str_size, mi_data, 1, 1 + nibbles_len, allow_hex);
Pau Espin Pedrol6fe865d2021-07-15 13:08:08 +0200717 /* check mi->str printing rc */
718 if (rc < 1 || rc >= str_size) {
719 rc = -EBADMSG;
720 goto return_error;
721 }
722 return 0;
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200723
724 default:
725 /* Already handled above, but as future bug paranoia: */
726 rc = -EINVAL;
727 goto return_error;
728 }
729
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200730return_error:
731 *mi = (struct osmo_mobile_identity){
732 .type = GSM_MI_TYPE_NONE,
733 };
734 return rc;
735}
736
737/*! Return the number of encoded Mobile Identity octets, without actually encoding.
738 * Useful to write tag-length header before encoding the MI.
739 * \param[in] mi Mobile Identity.
740 * \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).
741 * \return octets that osmo_mobile_identity_encode_msgb() will write for this mi.
742 */
743int osmo_mobile_identity_encoded_len(const struct osmo_mobile_identity *mi, int *mi_digits)
744{
745 int mi_nibbles;
746 if (!mi)
747 return -EINVAL;
748 switch (mi->type) {
749 case GSM_MI_TYPE_TMSI:
750 mi_nibbles = GSM23003_TMSI_NUM_BYTES * 2;
751 break;
752 case GSM_MI_TYPE_IMSI:
753 mi_nibbles = strlen(mi->imsi);
754 if (mi_nibbles < GSM23003_IMSI_MIN_DIGITS
755 || mi_nibbles > GSM23003_IMSI_MAX_DIGITS)
756 return -EINVAL;
757 break;
758 case GSM_MI_TYPE_IMEI:
759 mi_nibbles = strlen(mi->imei);
760 if (mi_nibbles < GSM23003_IMEI_NUM_DIGITS_NO_CHK
761 || mi_nibbles > GSM23003_IMEI_NUM_DIGITS)
762 return -EINVAL;
763 break;
764 case GSM_MI_TYPE_IMEISV:
765 mi_nibbles = strlen(mi->imeisv);
766 if (mi_nibbles != GSM23003_IMEISV_NUM_DIGITS)
767 return -EINVAL;
768 break;
769 default:
770 return -ENOTSUP;
771 }
772
773 if (mi_digits)
774 *mi_digits = mi_nibbles;
775
776 /* one type nibble, plus the MI nibbles, plus a filler nibble to complete the last octet:
777 * mi_octets = ceil((float)(mi_nibbles + 1) / 2)
778 */
779 return (mi_nibbles + 2) / 2;
780}
781
782/*! Encode Mobile Identity from uint32_t (TMSI) or digits string (all others) (3GPP TS 24.008 10.5.1.4).
783 *
784 * \param[out] buf Return buffer for encoded Mobile Identity.
785 * \param[in] buflen sizeof(buf).
786 * \param[in] mi Mobile identity to encode.
787 * \param[in] allow_hex If false, hexadecimal digits (>9) result in an error return value.
788 * \returns Amount of bytes written to buf, or negative on error.
789 */
790int osmo_mobile_identity_encode_buf(uint8_t *buf, size_t buflen, const struct osmo_mobile_identity *mi, bool allow_hex)
791{
792 int rc;
793 int nibbles_len;
794 int mi_octets;
795 const char *mi_str;
796
797 if (!buf || !buflen)
798 return -EIO;
799
800 mi_octets = osmo_mobile_identity_encoded_len(mi, &nibbles_len);
801 if (mi_octets < 0)
802 return mi_octets;
803 if (mi_octets > buflen)
804 return -ENOSPC;
805
806 buf[0] = (mi->type & GSM_MI_TYPE_MASK) | ((nibbles_len & 1) ? GSM_MI_ODD : 0);
807
808 switch (mi->type) {
809 case GSM_MI_TYPE_TMSI:
810 buf[0] |= 0xf0;
811 osmo_store32be(mi->tmsi, &buf[1]);
812 return mi_octets;
813
814 case GSM_MI_TYPE_IMSI:
815 mi_str = mi->imsi;
816 break;
817 case GSM_MI_TYPE_IMEI:
818 mi_str = mi->imei;
819 break;
820 case GSM_MI_TYPE_IMEISV:
821 mi_str = mi->imeisv;
822 break;
823 default:
824 return -ENOTSUP;
825 }
826 rc = osmo_str2bcd(buf, buflen, mi_str, 1, -1, allow_hex);
827 if (rc != mi_octets)
828 return -EINVAL;
829 return mi_octets;
830}
831
832/*! Encode Mobile Identity type and BCD digits, appended to a msgb.
833 * Example to add a GSM48_IE_MOBILE_ID IEI with tag and length to a msgb:
834 *
Neels Hofmeyr4247c942020-06-22 17:48:01 +0200835 * struct osmo_mobile_identity mi = { .type = GSM_MI_TYPE_IMSI };
836 * OSMO_STRLCPY_ARRAY(mi.imsi, "1234567890123456");
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200837 * uint8_t *l = msgb_tl_put(msg, GSM48_IE_MOBILE_ID);
838 * int rc = osmo_mobile_identity_encode_msgb(msg, &mi, false);
839 * if (rc < 0)
840 * goto error;
841 * *l = rc;
842 *
843 * Example to add a BSSGP_IE_IMSI with tag and variable-size length, where the
844 * length needs to be known at the time of writing the IE tag-length header:
845 *
846 * struct osmo_mobile_identity mi = { .type = GSM_MI_TYPE_IMSI, };
847 * OSMO_STRLCPY_ARRAY(mi.imsi, pinfo->imsi);
848 * msgb_tvl_put(msg, BSSGP_IE_IMSI, osmo_mobile_identity_encoded_len(&mi, NULL));
849 * if (osmo_mobile_identity_encode_msgb(msg, &mi, false) < 0)
850 * goto error;
851 */
852int osmo_mobile_identity_encode_msgb(struct msgb *msg, const struct osmo_mobile_identity *mi, bool allow_hex)
853{
854 int rc = osmo_mobile_identity_encode_buf(msg->tail, msgb_tailroom(msg), mi, allow_hex);
855 if (rc < 0)
856 return rc;
857 msgb_put(msg, rc);
858 return rc;
859}
860
861/*! Extract Mobile Identity from a Complete Layer 3 message.
862 *
863 * Determine the Mobile Identity data and call osmo_mobile_identity_decode() to return a decoded struct
864 * osmo_mobile_identity.
865 *
866 * \param[out] mi Return buffer for decoded Mobile Identity.
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200867 * \param[in] l3_data The Complete Layer 3 message to extract from (LU, CM Service Req or Paging Resp).
868 * \param[in] l3_len Length of l3_data in bytes.
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200869 * \returns 0 on success, negative on error: return codes as defined in osmo_mobile_identity_decode(), or
870 * -ENOTSUP = not a Complete Layer 3 message,
871 */
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200872int osmo_mobile_identity_decode_from_l3_buf(struct osmo_mobile_identity *mi, const uint8_t *l3_data, size_t l3_len,
873 bool allow_hex)
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200874{
875 const struct gsm48_hdr *gh;
876 int8_t pdisc = 0;
877 uint8_t mtype = 0;
878 const struct gsm48_loc_upd_req *lu;
879 const uint8_t *cm2_buf;
880 uint8_t cm2_len;
881 const uint8_t *mi_start;
882 const struct gsm48_pag_resp *paging_response;
883 const uint8_t *mi_data;
884 uint8_t mi_len;
885 const struct gsm48_imsi_detach_ind *idi;
886
887 *mi = (struct osmo_mobile_identity){
888 .type = GSM_MI_TYPE_NONE,
889 .tmsi = GSM_RESERVED_TMSI,
890 };
891
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200892 if (l3_len < sizeof(*gh))
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200893 return -EBADMSG;
894
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200895 gh = (void *)l3_data;
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200896 pdisc = gsm48_hdr_pdisc(gh);
897 mtype = gsm48_hdr_msg_type(gh);
898
899 switch (pdisc) {
900 case GSM48_PDISC_MM:
901
902 switch (mtype) {
903 case GSM48_MT_MM_LOC_UPD_REQUEST:
904 /* First make sure that lu-> can be dereferenced */
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200905 if (l3_len < sizeof(*gh) + sizeof(*lu))
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200906 return -EBADMSG;
907
908 /* Now we know there is enough msgb data to read a lu->mi_len, so also check that */
909 lu = (struct gsm48_loc_upd_req*)gh->data;
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200910 if (l3_len < sizeof(*gh) + sizeof(*lu) + lu->mi_len)
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200911 return -EBADMSG;
912 mi_data = lu->mi;
913 mi_len = lu->mi_len;
914 goto got_mi;
915
916 case GSM48_MT_MM_CM_SERV_REQ:
917 case GSM48_MT_MM_CM_REEST_REQ:
918 /* Unfortunately in Phase1 the Classmark2 length is variable, so we cannot
919 * just use gsm48_service_request struct, and need to parse it manually. */
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200920 if (l3_len < sizeof(*gh) + 2)
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200921 return -EBADMSG;
922
923 cm2_len = gh->data[1];
924 cm2_buf = gh->data + 2;
925 goto got_cm2;
926
927 case GSM48_MT_MM_IMSI_DETACH_IND:
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200928 if (l3_len < sizeof(*gh) + sizeof(*idi))
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200929 return -EBADMSG;
930 idi = (struct gsm48_imsi_detach_ind*) gh->data;
931 mi_data = idi->mi;
932 mi_len = idi->mi_len;
933 goto got_mi;
934
935 case GSM48_MT_MM_ID_RESP:
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200936 if (l3_len < sizeof(*gh) + 2)
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200937 return -EBADMSG;
938 mi_data = gh->data+1;
939 mi_len = gh->data[0];
940 goto got_mi;
941
942 default:
943 break;
944 }
945 break;
946
947 case GSM48_PDISC_RR:
948
949 switch (mtype) {
950 case GSM48_MT_RR_PAG_RESP:
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200951 if (l3_len < sizeof(*gh) + sizeof(*paging_response))
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200952 return -EBADMSG;
953 paging_response = (struct gsm48_pag_resp*)gh->data;
954 cm2_len = paging_response->cm2_len;
955 cm2_buf = (uint8_t*)&paging_response->cm2;
956 goto got_cm2;
957
Andreas Eversberge4c7b0a2023-06-26 10:21:48 +0200958 case GSM48_MT_RR_TALKER_IND:
959 /* Check minimum size: Header + CM2 LV + minimum MI LV */
960 if (l3_len < sizeof(*gh) + 4 + 2)
961 return -EBADMSG;
962 /* CM2 shall be always 3 bytes in length */
963 if (gh->data[0] != 3)
964 return -EBADMSG;
965 cm2_len = gh->data[0];
966 cm2_buf = gh->data + 1;
967 goto got_cm2;
968
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200969 default:
970 break;
971 }
972 break;
973 }
974
975 return -ENOTSUP;
976
977got_cm2:
978 /* MI (Mobile Identity) LV follows the Classmark2 */
979
980 /* There must be at least a mi_len byte after the CM2 */
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200981 if (cm2_buf + cm2_len + 1 > l3_data + l3_len)
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200982 return -EBADMSG;
983
984 mi_start = cm2_buf + cm2_len;
985 mi_len = mi_start[0];
986 mi_data = mi_start + 1;
987
988got_mi:
989 /* mi_data points at the start of the Mobile Identity coding of mi_len bytes */
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200990 if (mi_data + mi_len > l3_data + l3_len)
Neels Hofmeyr83025bf2020-05-26 02:45:23 +0200991 return -EBADMSG;
992
993 return osmo_mobile_identity_decode(mi, mi_data, mi_len, allow_hex);
994}
995
Neels Hofmeyre25786a2023-05-03 02:13:16 +0200996/*! Extract Mobile Identity from a Complete Layer 3 message.
997 *
998 * Determine the Mobile Identity data and call osmo_mobile_identity_decode() to return a decoded struct
999 * osmo_mobile_identity.
1000 *
1001 * \param[out] mi Return buffer for decoded Mobile Identity.
1002 * \param[in] msg The Complete Layer 3 message to extract from (LU, CM Service Req or Paging Resp).
1003 * \returns 0 on success, negative on error: return codes as defined in osmo_mobile_identity_decode(), or
1004 * -ENOTSUP = not a Complete Layer 3 message,
1005 */
1006int osmo_mobile_identity_decode_from_l3(struct osmo_mobile_identity *mi, struct msgb *msg, bool allow_hex)
1007{
1008 return osmo_mobile_identity_decode_from_l3_buf(mi, msgb_l3(msg), msgb_l3len(msg), allow_hex);
1009}
1010
Neels Hofmeyr83025bf2020-05-26 02:45:23 +02001011/*! Return a human readable representation of a struct osmo_mobile_identity.
1012 * Write a string like "IMSI-1234567", "TMSI-0x1234ABCD" or "NONE", "NULL".
1013 * \param[out] buf String buffer to write to.
1014 * \param[in] buflen sizeof(buf).
1015 * \param[in] mi Decoded Mobile Identity data.
1016 * \return the strlen() of the string written when buflen is sufficiently large, like snprintf().
1017 */
1018int osmo_mobile_identity_to_str_buf(char *buf, size_t buflen, const struct osmo_mobile_identity *mi)
1019{
1020 struct osmo_strbuf sb = { .buf = buf, .len = buflen };
1021 if (!mi)
1022 return snprintf(buf, buflen, "NULL");
1023 OSMO_STRBUF_PRINTF(sb, "%s", gsm48_mi_type_name(mi->type));
1024 switch (mi->type) {
1025 case GSM_MI_TYPE_TMSI:
1026 OSMO_STRBUF_PRINTF(sb, "-0x%08" PRIX32, mi->tmsi);
1027 break;
1028 case GSM_MI_TYPE_IMSI:
1029 OSMO_STRBUF_PRINTF(sb, "-%s", mi->imsi);
1030 break;
1031 case GSM_MI_TYPE_IMEI:
1032 OSMO_STRBUF_PRINTF(sb, "-%s", mi->imei);
1033 break;
1034 case GSM_MI_TYPE_IMEISV:
1035 OSMO_STRBUF_PRINTF(sb, "-%s", mi->imeisv);
1036 break;
1037 default:
1038 break;
1039 }
1040 return sb.chars_needed;
1041}
1042
1043/*! Like osmo_mobile_identity_to_str_buf(), but return the string in a talloc buffer.
1044 * \param[in] ctx Talloc context to allocate from.
1045 * \param[in] mi Decoded Mobile Identity data.
1046 * \return a string like "IMSI-1234567", "TMSI-0x1234ABCD" or "NONE", "NULL".
1047 */
1048char *osmo_mobile_identity_to_str_c(void *ctx, const struct osmo_mobile_identity *mi)
1049{
1050 OSMO_NAME_C_IMPL(ctx, 32, "ERROR", osmo_mobile_identity_to_str_buf, mi)
1051}
1052
1053/*! Compare two osmo_mobile_identity structs, returning typical cmp() result.
1054 * \param[in] a Left side osmo_mobile_identity.
1055 * \param[in] b Right side osmo_mobile_identity.
1056 * \returns 0 if both are equal, -1 if a < b, 1 if a > b.
1057 */
1058int osmo_mobile_identity_cmp(const struct osmo_mobile_identity *a, const struct osmo_mobile_identity *b)
1059{
1060 int cmp;
1061 if (a == b)
1062 return 0;
1063 if (!a)
1064 return -1;
1065 if (!b)
1066 return 1;
1067 cmp = OSMO_CMP(a->type, b->type);
1068 if (cmp)
1069 return cmp;
1070 switch (a->type) {
1071 case GSM_MI_TYPE_TMSI:
1072 return OSMO_CMP(a->tmsi, b->tmsi);
1073 case GSM_MI_TYPE_IMSI:
1074 return strncmp(a->imsi, b->imsi, sizeof(a->imsi));
1075 case GSM_MI_TYPE_IMEI:
1076 return strncmp(a->imei, b->imei, sizeof(a->imei));
1077 case GSM_MI_TYPE_IMEISV:
1078 return strncmp(a->imeisv, b->imeisv, sizeof(a->imeisv));
1079 default:
1080 /* No known type, but both have the same type. */
1081 return 0;
1082 }
1083}
1084
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001085/*! Checks is particular message is cipherable in A/Gb mode according to
Maxfdca25d2016-07-05 16:06:28 +02001086 * 3GPP TS 24.008 § 4.7.1.2
1087 * \param[in] hdr Message header
1088 * \return true if message can be encrypted, false otherwise
1089 */
1090bool gsm48_hdr_gmm_cipherable(const struct gsm48_hdr *hdr)
1091{
1092 switch(hdr->msg_type) {
1093 case GSM48_MT_GMM_ATTACH_REQ:
1094 case GSM48_MT_GMM_ATTACH_REJ:
1095 case GSM48_MT_GMM_AUTH_CIPH_REQ:
1096 case GSM48_MT_GMM_AUTH_CIPH_RESP:
1097 case GSM48_MT_GMM_AUTH_CIPH_REJ:
1098 case GSM48_MT_GMM_AUTH_CIPH_FAIL:
1099 case GSM48_MT_GMM_ID_REQ:
1100 case GSM48_MT_GMM_ID_RESP:
1101 case GSM48_MT_GMM_RA_UPD_REQ:
1102 case GSM48_MT_GMM_RA_UPD_REJ:
1103 return false;
1104 default:
1105 return true;
1106 }
1107}
1108
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001109/* Convert MCC + MNC to BCD representation, legacy implementation.
1110 * Instead use osmo_plmn_to_bcd(), which is also capable of converting
1111 * 3-digit MNC that have leading zeros. For parameters, also see there. */
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +01001112void gsm48_mcc_mnc_to_bcd(uint8_t *bcd_dst, uint16_t mcc, uint16_t mnc)
Harald Welte61e2bfc2010-03-04 10:53:03 +01001113{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001114 const struct osmo_plmn_id plmn = {
1115 .mcc = mcc,
1116 .mnc = mnc,
1117 .mnc_3_digits = false,
1118 };
1119 osmo_plmn_to_bcd(bcd_dst, &plmn);
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +01001120}
Harald Welte61e2bfc2010-03-04 10:53:03 +01001121
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001122/* Convert given 3-byte BCD buffer to integers, legacy implementation.
1123 * Instead use osmo_plmn_from_bcd(), which is also capable of converting
1124 * 3-digit MNC that have leading zeros. For parameters, also see there. */
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +01001125void gsm48_mcc_mnc_from_bcd(uint8_t *bcd_src, uint16_t *mcc, uint16_t *mnc)
1126{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001127 struct osmo_plmn_id plmn;
1128 osmo_plmn_from_bcd(bcd_src, &plmn);
1129 *mcc = plmn.mcc;
1130 *mnc = plmn.mnc;
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +01001131}
1132
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001133/*! Encode TS 04.08 Location Area Identifier, legacy implementation.
1134 * Instead use gsm48_generate_lai2(), which is capable of three-digit MNC with leading zeros.
1135 * \param[out] lai48 caller-provided memory for output
Harald Welte96e2a002017-06-12 21:44:18 +02001136 * \param[in] mcc Mobile Country Code
1137 * \param[in] mnc Mobile Network Code
1138 * \param[in] lac Location Area Code */
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +01001139void gsm48_generate_lai(struct gsm48_loc_area_id *lai48, uint16_t mcc,
1140 uint16_t mnc, uint16_t lac)
1141{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001142 const struct osmo_location_area_id lai = {
1143 .plmn = {
1144 .mcc = mcc,
1145 .mnc = mnc,
1146 .mnc_3_digits = false,
1147 },
1148 .lac = lac,
1149 };
1150 gsm48_generate_lai2(lai48, &lai);
Harald Welte61e2bfc2010-03-04 10:53:03 +01001151}
1152
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001153/*! Encode TS 04.08 Location Area Identifier.
1154 * \param[out] lai48 caller-provided memory for output.
1155 * \param[in] lai input of MCC-MNC-LAC. */
1156void gsm48_generate_lai2(struct gsm48_loc_area_id *lai48, const struct osmo_location_area_id *lai)
1157{
1158 osmo_plmn_to_bcd(&lai48->digits[0], &lai->plmn);
1159 lai48->lac = osmo_htons(lai->lac);
1160}
1161
1162/*! Decode TS 04.08 Location Area Identifier, legacy implementation.
1163 * Instead use gsm48_decode_lai2(), which is capable of three-digit MNC with leading zeros.
Harald Welte96e2a002017-06-12 21:44:18 +02001164 * \param[in] Location Area Identifier (encoded)
1165 * \param[out] mcc Mobile Country Code
1166 * \param[out] mnc Mobile Network Code
1167 * \param[out] lac Location Area Code
1168 * \returns 0
1169 *
1170 * Attention: this function returns true integers, not hex! */
Harald Welte774a9de2012-07-13 21:35:13 +02001171int gsm48_decode_lai(struct gsm48_loc_area_id *lai, uint16_t *mcc,
1172 uint16_t *mnc, uint16_t *lac)
1173{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001174 struct osmo_location_area_id decoded;
1175 gsm48_decode_lai2(lai, &decoded);
1176 *mcc = decoded.plmn.mcc;
1177 *mnc = decoded.plmn.mnc;
1178 *lac = decoded.lac;
Harald Welte774a9de2012-07-13 21:35:13 +02001179 return 0;
1180}
1181
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001182/*! Decode TS 04.08 Location Area Identifier.
1183 * \param[in] Location Area Identifier (encoded).
1184 * \param[out] decoded Target buffer to write decoded values of MCC-MNC-LAC.
1185 *
1186 * Attention: this function returns true integers, not hex! */
1187void gsm48_decode_lai2(const struct gsm48_loc_area_id *lai, struct osmo_location_area_id *decoded)
1188{
1189 osmo_plmn_from_bcd(&lai->digits[0], &decoded->plmn);
1190 decoded->lac = osmo_ntohs(lai->lac);
1191}
1192
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001193/*! Set DTX mode in Cell Options IE (3GPP TS 44.018)
Maxfe65fa72016-05-10 17:17:05 +02001194 * \param[in] op Cell Options structure in which DTX parameters will be set
1195 * \param[in] full Mode for full-rate channels
1196 * \param[in] half Mode for half-rate channels
1197 * \param[in] is_bcch Indicates if we should use 10.5.2.3.1 instead of
1198 * 10.5.2.3a.2
1199 *
1200 * There is no space for separate DTX settings for Full and Half rate channels
1201 * in BCCH - in this case full setting is used for both and half parameter is
1202 * ignored.
1203 */
1204void gsm48_set_dtx(struct gsm48_cell_options *op, enum gsm48_dtx_mode full,
1205 enum gsm48_dtx_mode half, bool is_bcch)
1206{
1207 if (is_bcch) {
1208 switch (full) {
1209 case GSM48_DTX_MAY_BE_USED:
1210 op->dtx = 0;
1211 return;
1212 case GSM48_DTX_SHALL_BE_USED:
1213 op->dtx = 1;
1214 return;
1215 case GSM48_DTX_SHALL_NOT_BE_USED:
1216 op->dtx = 2;
1217 return;
1218 }
1219 } else {
1220 switch (full) {
1221 case GSM48_DTX_MAY_BE_USED:
1222 op->dtx = (half == GSM48_DTX_SHALL_BE_USED) ? 3 : 0;
1223 op->d = (half == GSM48_DTX_SHALL_NOT_BE_USED) ? 0 : 1;
1224 return;
1225 case GSM48_DTX_SHALL_BE_USED:
1226 op->dtx = (half == GSM48_DTX_MAY_BE_USED) ? 3 : 1;
1227 op->d = (half == GSM48_DTX_SHALL_BE_USED) ? 1 : 0;
1228 return;
1229 case GSM48_DTX_SHALL_NOT_BE_USED:
1230 op->dtx = 2;
1231 op->d = (half == GSM48_DTX_SHALL_BE_USED) ? 1 : 0;
1232 return;
1233 }
1234 }
1235}
1236
Neels Hofmeyr83025bf2020-05-26 02:45:23 +02001237/*! Deprecated, see osmo_mobile_identity instead.
1238 * Generate TS 04.08 Mobile ID from TMSI
Harald Welte96e2a002017-06-12 21:44:18 +02001239 * \param[out] buf Caller-provided output buffer (7 bytes)
1240 * \param[in] tmsi TMSI to be encoded
1241 * \returns number of byes encoded (always 7) */
Harald Welte61e2bfc2010-03-04 10:53:03 +01001242int gsm48_generate_mid_from_tmsi(uint8_t *buf, uint32_t tmsi)
1243{
Harald Weltea13fb752020-06-16 08:44:42 +02001244 uint32_t tmsi_be = osmo_htonl(tmsi);
1245
1246 buf[0] = GSM48_IE_MOBILE_ID;
1247 buf[1] = GSM48_TMSI_LEN;
1248 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
1249 memcpy(&buf[3], &tmsi_be, sizeof(tmsi_be));
1250
1251 return 7;
Harald Welte61e2bfc2010-03-04 10:53:03 +01001252}
1253
Neels Hofmeyr83025bf2020-05-26 02:45:23 +02001254/*! Deprecated, see osmo_mobile_identity instead.
1255 * Generate TS 24.008 §10.5.1.4 Mobile ID of BCD type from ASCII string
Harald Welte1c3bae12019-01-20 10:37:49 +01001256 * \param[out] buf Caller-provided output buffer of at least GSM48_MID_MAX_SIZE bytes
Maxebf14922018-02-15 11:42:11 +01001257 * \param[in] id Identity to be encoded
Harald Welte1c3bae12019-01-20 10:37:49 +01001258 * \param[in] mi_type Type of identity (e.g. GSM_MI_TYPE_IMSI, IMEI, IMEISV)
Maxebf14922018-02-15 11:42:11 +01001259 * \returns number of bytes used in \a buf */
1260uint8_t gsm48_generate_mid(uint8_t *buf, const char *id, uint8_t mi_type)
1261{
Harald Weltea13fb752020-06-16 08:44:42 +02001262 uint8_t length = strnlen(id, 16), i, off = 0, odd = (length & 1) == 1;
1263 /* maximum length == 16 (IMEISV) */
1264
1265 buf[0] = GSM48_IE_MOBILE_ID;
1266 buf[2] = osmo_char2bcd(id[0]) << 4 | (mi_type & GSM_MI_TYPE_MASK) | (odd << 3);
1267
1268 /* if the length is even we will fill half of the last octet */
1269 buf[1] = (length + (odd ? 1 : 2)) >> 1;
1270 /* buf[1] maximum = 18/2 = 9 */
1271 OSMO_ASSERT(buf[1] <= 9);
1272
1273 for (i = 1; i < buf[1]; ++i) {
1274 uint8_t upper, lower = osmo_char2bcd(id[++off]);
1275 if (!odd && off + 1 == length)
1276 upper = 0x0f;
1277 else
1278 upper = osmo_char2bcd(id[++off]) & 0x0f;
1279
1280 buf[2 + i] = (upper << 4) | lower;
Maxebf14922018-02-15 11:42:11 +01001281 }
Harald Weltea13fb752020-06-16 08:44:42 +02001282
1283 /* maximum return value: 2 + 9 = 11 */
1284 return 2 + buf[1];
Maxebf14922018-02-15 11:42:11 +01001285}
1286
Neels Hofmeyr83025bf2020-05-26 02:45:23 +02001287/*! Deprecated, see osmo_mobile_identity instead.
1288 * Generate TS 04.08 Mobile ID from IMSI
Harald Welte96e2a002017-06-12 21:44:18 +02001289 * \param[out] buf Caller-provided output buffer
1290 * \param[in] imsi IMSI to be encoded
1291 * \returns number of bytes used in \a buf */
Harald Welte61e2bfc2010-03-04 10:53:03 +01001292int gsm48_generate_mid_from_imsi(uint8_t *buf, const char *imsi)
1293{
Harald Weltea13fb752020-06-16 08:44:42 +02001294 return gsm48_generate_mid(buf, imsi, GSM_MI_TYPE_IMSI);
Harald Welte61e2bfc2010-03-04 10:53:03 +01001295}
Harald Welte9bb553e2010-03-28 18:14:50 +08001296
Neels Hofmeyr83025bf2020-05-26 02:45:23 +02001297/*! Deprecated, see osmo_mobile_identity instead.
1298 * Convert TS 04.08 Mobile Identity (10.5.1.4) to string.
Neels Hofmeyr627e0112018-12-05 21:32:40 +01001299 * 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 +02001300 * \param[out] string Caller-provided buffer for output
1301 * \param[in] str_len Length of \a string in bytes
1302 * \param[in] mi Mobile Identity to be stringified
1303 * \param[in] mi_len Length of \a mi in bytes
Neels Hofmeyr512bf022020-05-25 23:05:04 +02001304 * \returns Return <= 0 on error, > 0 on success.
1305 * WARNING: the return value of this function is not well implemented.
Neels Hofmeyr6aa20ee2018-12-06 00:40:37 +01001306 * Depending on the MI type and amount of output buffer, this may return
1307 * the nr of written bytes, or the written strlen(), or the snprintf()
Neels Hofmeyr512bf022020-05-25 23:05:04 +02001308 * style strlen()-if-the-buffer-were-large-enough.
1309 */
Vadim Yanitskiy0d8da792019-07-25 19:12:23 +07001310int gsm48_mi_to_string(char *string, int str_len, const uint8_t *mi, int mi_len)
Harald Welte9bb553e2010-03-28 18:14:50 +08001311{
Neels Hofmeyr627e0112018-12-05 21:32:40 +01001312 int rc;
Harald Welte163d0ea2010-04-09 07:57:40 +02001313 uint8_t mi_type;
Harald Welte163d0ea2010-04-09 07:57:40 +02001314 uint32_t tmsi;
Harald Welte9bb553e2010-03-28 18:14:50 +08001315
Neels Hofmeyr6adffb92018-12-05 23:30:31 +01001316 mi_type = (mi && mi_len) ? (mi[0] & GSM_MI_TYPE_MASK) : GSM_MI_TYPE_NONE;
Harald Welte9bb553e2010-03-28 18:14:50 +08001317
1318 switch (mi_type) {
Harald Welte9bb553e2010-03-28 18:14:50 +08001319 case GSM_MI_TYPE_TMSI:
Holger Freyther45abec22016-05-20 19:21:27 +00001320 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
Harald Welte9bb553e2010-03-28 18:14:50 +08001321 if (mi_len == GSM48_TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
Harald Welte95871da2017-05-15 12:11:36 +02001322 tmsi = osmo_load32be(&mi[1]);
Pau Espin Pedrol45735022017-06-18 14:05:24 +02001323 return snprintf(string, str_len, "%"PRIu32, tmsi);
Harald Welte9bb553e2010-03-28 18:14:50 +08001324 }
1325 break;
1326 case GSM_MI_TYPE_IMSI:
1327 case GSM_MI_TYPE_IMEI:
1328 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr627e0112018-12-05 21:32:40 +01001329 rc = osmo_bcd2str(string, str_len, mi,
1330 1, mi_len * 2 - ((mi[0] & GSM_MI_ODD) ? 0 : 1), true);
1331 /* osmo_bcd2str() returns snprintf style strlen(), this returns bytes written. */
1332 if (rc < 0)
1333 return 0;
1334 else if (rc < str_len)
1335 return rc + 1;
1336 else
1337 return strlen(string) + 1;
Neels Hofmeyrea2a0ab2018-12-05 23:30:08 +01001338
Harald Welte9bb553e2010-03-28 18:14:50 +08001339 default:
1340 break;
1341 }
Harald Welte9bb553e2010-03-28 18:14:50 +08001342
Neels Hofmeyrea2a0ab2018-12-05 23:30:08 +01001343 if (str_len < 1)
1344 return 0;
1345 *string = '\0';
1346 return 1;
Harald Welte9bb553e2010-03-28 18:14:50 +08001347}
Harald Weltea1c4f762010-05-01 11:59:42 +02001348
Neels Hofmeyr8d423942023-06-08 00:35:41 +02001349/*! Decode to struct osmo_routing_area_id from a 3GPP TS 24.008 § 10.5.5.15 Routing area identification.
1350 * \param[out] dst Store the decoded result here.
1351 * \param[in] ra_data The start of a Routing Area ID in encoded form, to be decoded.
1352 * \param[in] ra_data_len Buffer size available to read from at *ra_data.
1353 * \return the number of decoded bytes on success, or negative on error (if the input buffer is too small).
1354 */
1355int osmo_routing_area_id_decode(struct osmo_routing_area_id *dst, const uint8_t *ra_data, size_t ra_data_len)
1356{
1357 const struct gsm48_ra_id *ra_id;
1358 if (ra_data_len < sizeof(*ra_id))
1359 return -ENOSPC;
1360
1361 gsm48_decode_lai2((void *)ra_data, &dst->lac);
1362
1363 ra_id = (void *)ra_data;
1364 dst->rac = ra_id->rac;
1365
1366 return sizeof(*ra_id);
1367}
1368
1369/*! Encode struct osmo_routing_area_id to a 3GPP TS 24.008 § 10.5.5.15 Routing area identification: write to a buffer.
1370 * \param[out] buf Return buffer for encoded Mobile Identity.
1371 * \param[in] buflen sizeof(buf).
1372 * \param[in] src RA to encode.
1373 * \return Amount of bytes written to buf, or negative on error.
1374 */
1375int osmo_routing_area_id_encode_buf(uint8_t *buf, size_t buflen, const struct osmo_routing_area_id *src)
1376{
1377 struct gsm48_ra_id *ra_id;
1378 if (buflen < sizeof(*ra_id))
1379 return -ENOSPC;
1380
1381 gsm48_generate_lai2((void *)buf, &src->lac);
1382
1383 ra_id = (void *)buf;
1384 ra_id->rac = src->rac;
1385
1386 return sizeof(*ra_id);
1387}
1388
1389/*! Encode struct osmo_routing_area_id to a 3GPP TS 24.008 § 10.5.5.15 Routing area identification: append to msgb.
1390 * To succeed, the msgb must have tailroom >= sizeof(struct gsm48_ra_id).
1391 * \param[out] msg Append to this msgb.
1392 * \param[in] src Encode this Routing Area ID.
1393 * \return Number of bytes appended to msgb, or negative on error.
1394 */
1395int osmo_routing_area_id_encode_msgb(struct msgb *msg, const struct osmo_routing_area_id *src)
1396{
1397 int rc = osmo_routing_area_id_encode_buf(msg->tail, msgb_tailroom(msg), src);
1398 if (rc <= 0)
1399 return rc;
1400 msgb_put(msg, rc);
1401 return rc;
1402}
1403
1404/*! Parse TS 04.08 Routing Area Identifier.
1405 * Preferably use osmo_routing_area_id_decode() instead: struct osmo_routing_area_id is better integrated with other API
1406 * like osmo_plmn_cmp().
Harald Welte96e2a002017-06-12 21:44:18 +02001407 * \param[out] Caller-provided memory for decoded RA ID
1408 * \param[in] buf Input buffer pointing to RAI IE value */
Harald Weltea1c4f762010-05-01 11:59:42 +02001409void gsm48_parse_ra(struct gprs_ra_id *raid, const uint8_t *buf)
1410{
1411 raid->mcc = (buf[0] & 0xf) * 100;
1412 raid->mcc += (buf[0] >> 4) * 10;
1413 raid->mcc += (buf[1] & 0xf) * 1;
1414
1415 /* I wonder who came up with the stupidity of encoding the MNC
1416 * differently depending on how many digits its decimal number has! */
1417 if ((buf[1] >> 4) == 0xf) {
1418 raid->mnc = (buf[2] & 0xf) * 10;
1419 raid->mnc += (buf[2] >> 4) * 1;
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001420 raid->mnc_3_digits = false;
Harald Weltea1c4f762010-05-01 11:59:42 +02001421 } else {
1422 raid->mnc = (buf[2] & 0xf) * 100;
1423 raid->mnc += (buf[2] >> 4) * 10;
1424 raid->mnc += (buf[1] >> 4) * 1;
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001425 raid->mnc_3_digits = true;
Harald Weltea1c4f762010-05-01 11:59:42 +02001426 }
1427
Harald Welte95871da2017-05-15 12:11:36 +02001428 raid->lac = osmo_load16be(buf + 3);
Harald Weltea1c4f762010-05-01 11:59:42 +02001429 raid->rac = buf[5];
1430}
Harald Welte35a93942010-05-01 14:25:22 +02001431
Maxf1ad60e2018-01-05 14:19:33 +01001432/*! Encode a 3GPP TS 24.008 § 10.5.5.15 Routing area identification
1433 * \param[out] out Caller-provided packed struct
1434 * \param[in] raid Routing Area ID to be encoded
1435 */
1436void gsm48_encode_ra(struct gsm48_ra_id *out, const struct gprs_ra_id *raid)
1437{
1438 out->lac = osmo_htons(raid->lac);
1439 out->rac = raid->rac;
1440
1441 out->digits[0] = ((raid->mcc / 100) % 10) | (((raid->mcc / 10) % 10) << 4);
1442 out->digits[1] = raid->mcc % 10;
1443
Neels Hofmeyrc4fce142018-02-20 13:47:08 +01001444 if (raid->mnc < 100 && !raid->mnc_3_digits) {
Maxf1ad60e2018-01-05 14:19:33 +01001445 out->digits[1] |= 0xf0;
1446 out->digits[2] = ((raid->mnc / 10) % 10) | ((raid->mnc % 10) << 4);
1447 } else {
1448 out->digits[1] |= (raid->mnc % 10) << 4;
1449 out->digits[2] = ((raid->mnc / 100) % 10) | (((raid->mnc / 10) % 10) << 4);
1450 }
1451}
1452
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001453/*! Encode a TS 04.08 Routing Area Identifier
Harald Welte96e2a002017-06-12 21:44:18 +02001454 * \param[out] buf Caller-provided output buffer of 6 bytes
1455 * \param[in] raid Routing Area ID to be encoded
1456 * \returns number of bytes used in \a buf */
Harald Welte35a93942010-05-01 14:25:22 +02001457int gsm48_construct_ra(uint8_t *buf, const struct gprs_ra_id *raid)
1458{
Maxf1ad60e2018-01-05 14:19:33 +01001459 gsm48_encode_ra((struct gsm48_ra_id *)buf, raid);
Harald Welte35a93942010-05-01 14:25:22 +02001460
1461 return 6;
1462}
Harald Welte2aee7b12011-06-26 14:20:04 +02001463
Philipp Maierd11a5d52021-02-03 22:11:46 +01001464/*! Compare a TS 04.08 Routing Area Identifier
1465 * \param[in] raid1 first Routing Area ID to compare.
1466 * \param[in] raid2 second Routing Area ID to compare.
1467 * \returns true if raid1 and raid2 match, false otherwise. */
1468bool gsm48_ra_equal(const struct gprs_ra_id *raid1, const struct gprs_ra_id *raid2)
1469{
1470 if (raid1->mcc != raid2->mcc)
1471 return false;
1472 if (raid1->mnc != raid2->mnc)
1473 return false;
1474 if (raid1->mnc_3_digits != raid2->mnc_3_digits)
1475 return false;
1476 if (raid1->lac != raid2->lac)
1477 return false;
1478 if (raid1->rac != raid2->rac)
1479 return false;
1480 return true;
1481}
1482
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001483/*! Determine number of paging sub-channels
Harald Welte96e2a002017-06-12 21:44:18 +02001484 * \param[in] chan_desc Control Channel Description
1485 * \returns number of paging sub-channels
1486 *
1487 * Uses From Table 10.5.33 of GSM 04.08 to determine the number of
1488 * paging sub-channels in the given control channel configuration
1489 */
Pau Espin Pedrol6ca0a432022-11-24 17:09:03 +01001490int gsm48_number_of_paging_subchannels(const struct gsm48_control_channel_descr *chan_desc)
Harald Welte2aee7b12011-06-26 14:20:04 +02001491{
Harald Welte94df39e2011-06-26 14:33:57 +02001492 unsigned int n_pag_blocks = gsm0502_get_n_pag_blocks(chan_desc);
1493
1494 if (chan_desc->ccch_conf == RSL_BCCH_CCCH_CONF_1_C)
1495 return OSMO_MAX(1, n_pag_blocks) * (chan_desc->bs_pa_mfrms + 2);
1496 else
1497 return n_pag_blocks * (chan_desc->bs_pa_mfrms + 2);
Harald Welte2aee7b12011-06-26 14:20:04 +02001498}
Neels Hofmeyrdbd994c2017-03-09 23:07:02 +01001499
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001500/*! TS 04.08 Protocol Descriptor names */
Neels Hofmeyrdbd994c2017-03-09 23:07:02 +01001501const struct value_string gsm48_pdisc_names[] = {
Harald Welteea0bc962018-01-24 16:17:45 +01001502 { GSM48_PDISC_GROUP_CC, "VGCC" },
1503 { GSM48_PDISC_BCAST_CC, "VBCC" },
1504 { GSM48_PDISC_PDSS1, "PDSS1" },
1505 { GSM48_PDISC_CC, "CC" },
1506 { GSM48_PDISC_PDSS2, "PDSS2" },
1507 { GSM48_PDISC_MM, "MM" },
1508 { GSM48_PDISC_RR, "RR" },
1509 { GSM48_PDISC_MM_GPRS, "GMM" },
1510 { GSM48_PDISC_SMS, "SMS" },
1511 { GSM48_PDISC_SM_GPRS, "SM" },
1512 { GSM48_PDISC_NC_SS, "NCSS" },
1513 { GSM48_PDISC_LOC, "LCS" },
1514 { GSM48_PDISC_EXTEND, "EXTD" },
1515 { GSM48_PDISC_MASK, "MASK" },
Neels Hofmeyrdbd994c2017-03-09 23:07:02 +01001516 { 0, NULL }
1517};
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001518
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001519/*! TS 04.08 RR Message Type names */
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001520const struct value_string gsm48_rr_msgtype_names[] = {
1521 OSMO_VALUE_STRING(GSM48_MT_RR_INIT_REQ),
1522 OSMO_VALUE_STRING(GSM48_MT_RR_ADD_ASS),
1523 OSMO_VALUE_STRING(GSM48_MT_RR_IMM_ASS),
1524 OSMO_VALUE_STRING(GSM48_MT_RR_IMM_ASS_EXT),
1525 OSMO_VALUE_STRING(GSM48_MT_RR_IMM_ASS_REJ),
1526 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_ASS_FAIL),
1527 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_REJECT),
1528 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_REQUEST),
1529 OSMO_VALUE_STRING(GSM48_MT_RR_PACKET_ASS),
1530
1531 OSMO_VALUE_STRING(GSM48_MT_RR_CIPH_M_CMD),
1532 OSMO_VALUE_STRING(GSM48_MT_RR_CIPH_M_COMPL),
1533
1534 OSMO_VALUE_STRING(GSM48_MT_RR_CFG_CHG_CMD),
1535 OSMO_VALUE_STRING(GSM48_MT_RR_CFG_CHG_ACK),
1536 OSMO_VALUE_STRING(GSM48_MT_RR_CFG_CHG_REJ),
1537
1538 OSMO_VALUE_STRING(GSM48_MT_RR_ASS_CMD),
1539 OSMO_VALUE_STRING(GSM48_MT_RR_ASS_COMPL),
1540 OSMO_VALUE_STRING(GSM48_MT_RR_ASS_FAIL),
1541 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_CMD),
1542 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_COMPL),
1543 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_FAIL),
1544 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_INFO),
1545 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_INFO),
1546 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_ASS_CMD),
1547
1548 OSMO_VALUE_STRING(GSM48_MT_RR_CELL_CHG_ORDER),
1549 OSMO_VALUE_STRING(GSM48_MT_RR_PDCH_ASS_CMD),
1550
1551 OSMO_VALUE_STRING(GSM48_MT_RR_CHAN_REL),
1552 OSMO_VALUE_STRING(GSM48_MT_RR_PART_REL),
1553 OSMO_VALUE_STRING(GSM48_MT_RR_PART_REL_COMP),
1554
1555 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_REQ_1),
1556 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_REQ_2),
1557 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_REQ_3),
1558 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_RESP),
1559 OSMO_VALUE_STRING(GSM48_MT_RR_NOTIF_NCH),
1560 OSMO_VALUE_STRING(GSM48_MT_RR_NOTIF_FACCH),
1561 OSMO_VALUE_STRING(GSM48_MT_RR_NOTIF_RESP),
1562 OSMO_VALUE_STRING(GSM48_MT_RR_PACKET_NOTIF),
1563 OSMO_VALUE_STRING(GSM48_MT_RR_UTRAN_CLSM_CHG),
1564 OSMO_VALUE_STRING(GSM48_MT_RR_CDMA2K_CLSM_CHG),
1565 OSMO_VALUE_STRING(GSM48_MT_RR_IS_TO_UTRAN_HANDO),
1566 OSMO_VALUE_STRING(GSM48_MT_RR_IS_TO_CDMA2K_HANDO),
1567
1568 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_8),
1569 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_1),
1570 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2),
1571 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_3),
1572 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_4),
1573 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_5),
1574 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_6),
1575 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_7),
1576
1577 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2bis),
1578 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2ter),
1579 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2quater),
1580 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_5bis),
1581 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_5ter),
1582 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_9),
1583 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_13),
1584
1585 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_16),
1586 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_17),
1587
1588 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_18),
1589 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_19),
1590 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_20),
1591
1592 OSMO_VALUE_STRING(GSM48_MT_RR_CHAN_MODE_MODIF),
1593 OSMO_VALUE_STRING(GSM48_MT_RR_STATUS),
1594 OSMO_VALUE_STRING(GSM48_MT_RR_CHAN_MODE_MODIF_ACK),
1595 OSMO_VALUE_STRING(GSM48_MT_RR_FREQ_REDEF),
1596 OSMO_VALUE_STRING(GSM48_MT_RR_MEAS_REP),
1597 OSMO_VALUE_STRING(GSM48_MT_RR_CLSM_CHG),
1598 OSMO_VALUE_STRING(GSM48_MT_RR_CLSM_ENQ),
1599 OSMO_VALUE_STRING(GSM48_MT_RR_EXT_MEAS_REP),
1600 OSMO_VALUE_STRING(GSM48_MT_RR_EXT_MEAS_REP_ORD),
1601 OSMO_VALUE_STRING(GSM48_MT_RR_GPRS_SUSP_REQ),
1602 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_INFO),
1603
1604 OSMO_VALUE_STRING(GSM48_MT_RR_VGCS_UPL_GRANT),
1605 OSMO_VALUE_STRING(GSM48_MT_RR_UPLINK_RELEASE),
1606 OSMO_VALUE_STRING(GSM48_MT_RR_UPLINK_FREE),
1607 OSMO_VALUE_STRING(GSM48_MT_RR_UPLINK_BUSY),
1608 OSMO_VALUE_STRING(GSM48_MT_RR_TALKER_IND),
1609 { 0, NULL }
1610};
1611
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001612/*! TS 04.08 MM Message Type names */
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001613const struct value_string gsm48_mm_msgtype_names[] = {
1614 OSMO_VALUE_STRING(GSM48_MT_MM_IMSI_DETACH_IND),
1615 OSMO_VALUE_STRING(GSM48_MT_MM_LOC_UPD_ACCEPT),
1616 OSMO_VALUE_STRING(GSM48_MT_MM_LOC_UPD_REJECT),
1617 OSMO_VALUE_STRING(GSM48_MT_MM_LOC_UPD_REQUEST),
1618
1619 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_REJ),
1620 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_REQ),
1621 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_RESP),
1622 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_FAIL),
1623 OSMO_VALUE_STRING(GSM48_MT_MM_ID_REQ),
1624 OSMO_VALUE_STRING(GSM48_MT_MM_ID_RESP),
1625 OSMO_VALUE_STRING(GSM48_MT_MM_TMSI_REALL_CMD),
1626 OSMO_VALUE_STRING(GSM48_MT_MM_TMSI_REALL_COMPL),
1627
1628 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_ACC),
1629 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_REJ),
1630 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_ABORT),
1631 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_REQ),
1632 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_PROMPT),
1633 OSMO_VALUE_STRING(GSM48_MT_MM_CM_REEST_REQ),
1634 OSMO_VALUE_STRING(GSM48_MT_MM_ABORT),
1635
1636 OSMO_VALUE_STRING(GSM48_MT_MM_NULL),
1637 OSMO_VALUE_STRING(GSM48_MT_MM_STATUS),
1638 OSMO_VALUE_STRING(GSM48_MT_MM_INFO),
1639 { 0, NULL }
1640};
1641
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001642/*! TS 04.08 CC Message Type names */
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001643const struct value_string gsm48_cc_msgtype_names[] = {
1644 OSMO_VALUE_STRING(GSM48_MT_CC_ALERTING),
1645 OSMO_VALUE_STRING(GSM48_MT_CC_CALL_CONF),
1646 OSMO_VALUE_STRING(GSM48_MT_CC_CALL_PROC),
1647 OSMO_VALUE_STRING(GSM48_MT_CC_CONNECT),
1648 OSMO_VALUE_STRING(GSM48_MT_CC_CONNECT_ACK),
1649 OSMO_VALUE_STRING(GSM48_MT_CC_EMERG_SETUP),
1650 OSMO_VALUE_STRING(GSM48_MT_CC_PROGRESS),
1651 OSMO_VALUE_STRING(GSM48_MT_CC_ESTAB),
1652 OSMO_VALUE_STRING(GSM48_MT_CC_ESTAB_CONF),
1653 OSMO_VALUE_STRING(GSM48_MT_CC_RECALL),
1654 OSMO_VALUE_STRING(GSM48_MT_CC_START_CC),
1655 OSMO_VALUE_STRING(GSM48_MT_CC_SETUP),
1656
1657 OSMO_VALUE_STRING(GSM48_MT_CC_MODIFY),
1658 OSMO_VALUE_STRING(GSM48_MT_CC_MODIFY_COMPL),
1659 OSMO_VALUE_STRING(GSM48_MT_CC_MODIFY_REJECT),
1660 OSMO_VALUE_STRING(GSM48_MT_CC_USER_INFO),
1661 OSMO_VALUE_STRING(GSM48_MT_CC_HOLD),
1662 OSMO_VALUE_STRING(GSM48_MT_CC_HOLD_ACK),
1663 OSMO_VALUE_STRING(GSM48_MT_CC_HOLD_REJ),
1664 OSMO_VALUE_STRING(GSM48_MT_CC_RETR),
1665 OSMO_VALUE_STRING(GSM48_MT_CC_RETR_ACK),
1666 OSMO_VALUE_STRING(GSM48_MT_CC_RETR_REJ),
1667
1668 OSMO_VALUE_STRING(GSM48_MT_CC_DISCONNECT),
1669 OSMO_VALUE_STRING(GSM48_MT_CC_RELEASE),
1670 OSMO_VALUE_STRING(GSM48_MT_CC_RELEASE_COMPL),
1671
1672 OSMO_VALUE_STRING(GSM48_MT_CC_CONG_CTRL),
1673 OSMO_VALUE_STRING(GSM48_MT_CC_NOTIFY),
1674 OSMO_VALUE_STRING(GSM48_MT_CC_STATUS),
1675 OSMO_VALUE_STRING(GSM48_MT_CC_STATUS_ENQ),
1676 OSMO_VALUE_STRING(GSM48_MT_CC_START_DTMF),
1677 OSMO_VALUE_STRING(GSM48_MT_CC_STOP_DTMF),
1678 OSMO_VALUE_STRING(GSM48_MT_CC_STOP_DTMF_ACK),
1679 OSMO_VALUE_STRING(GSM48_MT_CC_START_DTMF_ACK),
1680 OSMO_VALUE_STRING(GSM48_MT_CC_START_DTMF_REJ),
1681 OSMO_VALUE_STRING(GSM48_MT_CC_FACILITY),
1682 { 0, NULL }
1683};
1684
Keith3cdaa8d2018-08-31 20:09:18 +02001685/*! TS 04.08 10.5..4.11 Call Control Cause Values */
1686const struct value_string gsm48_cc_cause_names[] = {
1687 { GSM48_CC_CAUSE_UNASSIGNED_NR, "UNASSIGNED_NR" },
1688 { GSM48_CC_CAUSE_NO_ROUTE, "NO_ROUTE" },
1689 { GSM48_CC_CAUSE_CHAN_UNACCEPT, "CHAN_UNACCEPT" },
1690 { GSM48_CC_CAUSE_OP_DET_BARRING, "OP_DET_BARRING" },
1691 { GSM48_CC_CAUSE_NORM_CALL_CLEAR, "NORM_CALL_CLEAR" },
1692 { GSM48_CC_CAUSE_USER_BUSY, "USER_BUSY" },
1693 { GSM48_CC_CAUSE_USER_NOTRESPOND, "USER_NOTRESPOND" },
1694 { GSM48_CC_CAUSE_USER_ALERTING_NA, "USER_ALERTING_NA" },
1695 { GSM48_CC_CAUSE_CALL_REJECTED, "CALL_REJECTED" },
1696 { GSM48_CC_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED" },
1697 { GSM48_CC_CAUSE_PRE_EMPTION, "PRE_EMPTION" },
1698 { GSM48_CC_CAUSE_NONSE_USER_CLR, "NONSE_USER_CLR" },
1699 { GSM48_CC_CAUSE_DEST_OOO, "DEST_OOO" },
1700 { GSM48_CC_CAUSE_INV_NR_FORMAT, "INV_NR_FORMAT" },
1701 { GSM48_CC_CAUSE_FACILITY_REJ, "FACILITY_REJ" },
1702 { GSM48_CC_CAUSE_RESP_STATUS_INQ, "RESP_STATUS_INQ" },
1703 { GSM48_CC_CAUSE_NORMAL_UNSPEC, "NORMAL_UNSPEC" },
1704 { GSM48_CC_CAUSE_NO_CIRCUIT_CHAN, "NO_CIRCUIT_CHAN" },
1705 { GSM48_CC_CAUSE_NETWORK_OOO, "NETWORK_OOO" },
1706 { GSM48_CC_CAUSE_TEMP_FAILURE, "TEMP_FAILURE" },
1707 { GSM48_CC_CAUSE_SWITCH_CONG, "SWITCH_CONG" },
1708 { GSM48_CC_CAUSE_ACC_INF_DISCARD, "ACC_INF_DISCARD" },
1709 { GSM48_CC_CAUSE_REQ_CHAN_UNAVAIL, "REQ_CHAN_UNAVAIL" },
1710 { GSM48_CC_CAUSE_RESOURCE_UNAVAIL, "RESOURCE_UNAVAIL" },
1711 { GSM48_CC_CAUSE_QOS_UNAVAIL, "QOS_UNAVAIL" },
1712 { GSM48_CC_CAUSE_REQ_FAC_NOT_SUBSC, "REQ_FAC_NOT_SUBSC" },
1713 { GSM48_CC_CAUSE_INC_BARRED_CUG, "INC_BARRED_CUG" },
1714 { GSM48_CC_CAUSE_BEARER_CAP_UNAUTH, "BEARER_CAP_UNAUTH" },
1715 { GSM48_CC_CAUSE_BEARER_CA_UNAVAIL, "BEARER_CA_UNAVAIL" },
1716 { GSM48_CC_CAUSE_SERV_OPT_UNAVAIL, "SERV_OPT_UNAVAIL" },
1717 { GSM48_CC_CAUSE_BEARERSERV_UNIMPL, "BEARERSERV_UNIMPL" },
1718 { GSM48_CC_CAUSE_ACM_GE_ACM_MAX, "ACM_GE_ACM_MAX" },
1719 { GSM48_CC_CAUSE_REQ_FAC_NOTIMPL, "REQ_FAC_NOTIMPL" },
1720 { GSM48_CC_CAUSE_RESTR_BCAP_AVAIL, "RESTR_BCAP_AVAIL" },
1721 { GSM48_CC_CAUSE_SERV_OPT_UNIMPL, "SERV_OPT_UNIMPL" },
1722 { GSM48_CC_CAUSE_INVAL_TRANS_ID, "INVAL_TRANS_ID" },
1723 { GSM48_CC_CAUSE_USER_NOT_IN_CUG, "USER_NOT_IN_CUG" },
1724 { GSM48_CC_CAUSE_INCOMPAT_DEST, "INCOMPAT_DEST" },
1725 { GSM48_CC_CAUSE_INVAL_TRANS_NET, "INVAL_TRANS_NET" },
1726 { GSM48_CC_CAUSE_SEMANTIC_INCORR, "SEMANTIC_INCORR" },
1727 { GSM48_CC_CAUSE_INVAL_MAND_INF, "INVAL_MAND_INF" },
1728 { GSM48_CC_CAUSE_MSGTYPE_NOTEXIST, "MSGTYPE_NOTEXIST" },
1729 { GSM48_CC_CAUSE_MSGTYPE_INCOMPAT, "MSGTYPE_INCOMPAT" },
1730 { GSM48_CC_CAUSE_IE_NOTEXIST, "IE_NOTEXIST" },
1731 { GSM48_CC_CAUSE_COND_IE_ERR, "COND_IE_ERR" },
1732 { GSM48_CC_CAUSE_MSG_INCOMP_STATE, "MSG_INCOMP_STATE" },
1733 { GSM48_CC_CAUSE_RECOVERY_TIMER, "RECOVERY_TIMER" },
1734 { GSM48_CC_CAUSE_PROTO_ERR, "PROTO_ERR" },
1735 { GSM48_CC_CAUSE_INTERWORKING, "INTERWORKING" },
1736 { 0 , NULL }
1737};
1738
Vadim Yanitskiy8d41d722018-04-17 11:17:28 +07001739/*! TS 04.80, section 3.4 Messages for supplementary services control */
1740const struct value_string gsm48_nc_ss_msgtype_names[] = {
1741 OSMO_VALUE_STRING(GSM0480_MTYPE_RELEASE_COMPLETE),
1742 OSMO_VALUE_STRING(GSM0480_MTYPE_FACILITY),
1743 OSMO_VALUE_STRING(GSM0480_MTYPE_REGISTER),
1744 { 0, NULL }
1745};
1746
Harald Welte4a62eda2019-03-18 18:27:00 +01001747/*! Compose a string naming the message type for given protocol, in a caller-provided buffer.
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001748 * If the message type string is known, return the message type name, otherwise
1749 * return "<protocol discriminator name>:<message type in hex>".
Harald Welte4a62eda2019-03-18 18:27:00 +01001750 * \param[out] buf caller-allcated output string buffer
1751 * \param[in] buf_len size of buf in bytes
Harald Welte96e2a002017-06-12 21:44:18 +02001752 * \param[in] pdisc protocol discriminator like GSM48_PDISC_MM
1753 * \param[in] msg_type message type like GSM48_MT_MM_LOC_UPD_REQUEST
Harald Welte4a62eda2019-03-18 18:27:00 +01001754 * \returns buf
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001755 */
Harald Welte4a62eda2019-03-18 18:27:00 +01001756char *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 +01001757{
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001758 const struct value_string *msgt_names;
1759
1760 switch (pdisc) {
1761 case GSM48_PDISC_RR:
1762 msgt_names = gsm48_rr_msgtype_names;
1763 break;
1764 case GSM48_PDISC_MM:
1765 msgt_names = gsm48_mm_msgtype_names;
1766 break;
1767 case GSM48_PDISC_CC:
1768 msgt_names = gsm48_cc_msgtype_names;
1769 break;
Andreas Eversberg72ab3b52023-06-17 16:15:56 +02001770 case GSM48_PDISC_GROUP_CC:
1771 case GSM48_PDISC_BCAST_CC:
1772 msgt_names = osmo_gsm44068_msg_type_names;
1773 break;
Vadim Yanitskiy8d41d722018-04-17 11:17:28 +07001774 case GSM48_PDISC_NC_SS:
1775 msgt_names = gsm48_nc_ss_msgtype_names;
1776 break;
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001777 default:
1778 msgt_names = NULL;
1779 break;
1780 }
1781
1782 if (msgt_names)
Harald Welte4a62eda2019-03-18 18:27:00 +01001783 snprintf(buf, buf_len, "%s", get_value_string(msgt_names, msg_type));
1784 else
1785 snprintf(buf, buf_len, "%s:0x%02x", gsm48_pdisc_name(pdisc), msg_type);
1786 return buf;
1787}
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001788
Harald Welte4a62eda2019-03-18 18:27:00 +01001789/*! Compose a string naming the message type for given protocol, in a static buffer.
1790 * If the message type string is known, return the message type name, otherwise
1791 * return "<protocol discriminator name>:<message type in hex>".
1792 * \param[in] pdisc protocol discriminator like GSM48_PDISC_MM
1793 * \param[in] msg_type message type like GSM48_MT_MM_LOC_UPD_REQUEST
1794 * \returns statically allocated string or string constant.
1795 */
1796const char *gsm48_pdisc_msgtype_name(uint8_t pdisc, uint8_t msg_type)
1797{
Harald Welte171ef822019-03-28 10:49:05 +01001798 static __thread char namebuf[64];
Harald Welte4a62eda2019-03-18 18:27:00 +01001799 return gsm48_pdisc_msgtype_name_buf(namebuf, sizeof(namebuf), pdisc, msg_type);
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001800}
Harald Welte96e2a002017-06-12 21:44:18 +02001801
Harald Welte179f3572019-03-18 18:38:47 +01001802/*! Compose a string naming the message type for given protocol, in a dynamically-allocated buffer.
1803 * If the message type string is known, return the message type name, otherwise
1804 * return "<protocol discriminator name>:<message type in hex>".
1805 * \param[in] ctx talloc context from which to allocate output buffer
1806 * \param[in] pdisc protocol discriminator like GSM48_PDISC_MM
1807 * \param[in] msg_type message type like GSM48_MT_MM_LOC_UPD_REQUEST
1808 * \returns string representation in dynamically allocated output buffer.
1809 */
1810char *gsm48_pdisc_msgtype_name_c(const void *ctx, uint8_t pdisc, uint8_t msg_type)
1811{
1812 char *namebuf = talloc_size(ctx, 64);
1813 if (!namebuf)
1814 return NULL;
1815 return gsm48_pdisc_msgtype_name_buf(namebuf, 64, pdisc, msg_type);
1816}
1817
Neels Hofmeyraead2412018-04-06 04:31:00 +02001818const struct value_string gsm48_reject_value_names[] = {
1819 { GSM48_REJECT_IMSI_UNKNOWN_IN_HLR, "IMSI_UNKNOWN_IN_HLR" },
1820 { GSM48_REJECT_ILLEGAL_MS, "ILLEGAL_MS" },
1821 { GSM48_REJECT_IMSI_UNKNOWN_IN_VLR, "IMSI_UNKNOWN_IN_VLR" },
1822 { GSM48_REJECT_IMEI_NOT_ACCEPTED, "IMEI_NOT_ACCEPTED" },
1823 { GSM48_REJECT_ILLEGAL_ME, "ILLEGAL_ME" },
1824 { GSM48_REJECT_PLMN_NOT_ALLOWED, "PLMN_NOT_ALLOWED" },
1825 { GSM48_REJECT_LOC_NOT_ALLOWED, "LOC_NOT_ALLOWED" },
1826 { GSM48_REJECT_ROAMING_NOT_ALLOWED, "ROAMING_NOT_ALLOWED" },
1827 { GSM48_REJECT_NETWORK_FAILURE, "NETWORK_FAILURE" },
1828 { GSM48_REJECT_SYNCH_FAILURE, "SYNCH_FAILURE" },
1829 { GSM48_REJECT_CONGESTION, "CONGESTION" },
1830 { GSM48_REJECT_SRV_OPT_NOT_SUPPORTED, "SRV_OPT_NOT_SUPPORTED" },
1831 { GSM48_REJECT_RQD_SRV_OPT_NOT_SUPPORTED, "RQD_SRV_OPT_NOT_SUPPORTED" },
1832 { GSM48_REJECT_SRV_OPT_TMP_OUT_OF_ORDER, "SRV_OPT_TMP_OUT_OF_ORDER" },
1833 { GSM48_REJECT_CALL_CAN_NOT_BE_IDENTIFIED, "CALL_CAN_NOT_BE_IDENTIFIED" },
1834 { GSM48_REJECT_INCORRECT_MESSAGE, "INCORRECT_MESSAGE" },
1835 { GSM48_REJECT_INVALID_MANDANTORY_INF, "INVALID_MANDANTORY_INF" },
1836 { GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED, "MSG_TYPE_NOT_IMPLEMENTED" },
1837 { GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE, "MSG_TYPE_NOT_COMPATIBLE" },
1838 { GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED, "INF_ELEME_NOT_IMPLEMENTED" },
1839 { GSM48_REJECT_CONDTIONAL_IE_ERROR, "CONDTIONAL_IE_ERROR" },
1840 { GSM48_REJECT_MSG_NOT_COMPATIBLE, "MSG_NOT_COMPATIBLE" },
1841 { GSM48_REJECT_PROTOCOL_ERROR, "PROTOCOL_ERROR" },
1842 { GSM48_REJECT_GPRS_NOT_ALLOWED, "GPRS_NOT_ALLOWED" },
1843 { GSM48_REJECT_SERVICES_NOT_ALLOWED, "SERVICES_NOT_ALLOWED" },
1844 { GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE, "MS_IDENTITY_NOT_DERVIVABLE" },
1845 { GSM48_REJECT_IMPLICITLY_DETACHED, "IMPLICITLY_DETACHED" },
1846 { GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN, "GPRS_NOT_ALLOWED_IN_PLMN" },
1847 { GSM48_REJECT_MSC_TMP_NOT_REACHABLE, "MSC_TMP_NOT_REACHABLE" },
1848 { 0, NULL }
1849};
1850
Vadim Yanitskiy30cfeeb2018-08-03 05:44:00 +07001851/*! Wrap a given \ref msg with \ref gsm48_hdr structure
1852 * \param[out] msg A message to be wrapped
1853 * \param[in] pdisc GSM TS 04.07 protocol discriminator 1/2,
1854 * sub-pdisc, trans_id or skip_ind 1/2,
1855 * see section 11.2.3.1 for details
1856 * \param[in] msg_type GSM TS 04.08 message type
1857 * @return pointer to pushed header within \ref msg
1858 */
1859struct gsm48_hdr *gsm48_push_l3hdr(struct msgb *msg,
1860 uint8_t pdisc, uint8_t msg_type)
1861{
1862 struct gsm48_hdr *gh;
1863
1864 gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
1865 gh->proto_discr = pdisc;
1866 gh->msg_type = msg_type;
1867
1868 return gh;
1869}
1870
Neels Hofmeyr92f3f5e2019-01-05 00:39:13 +01001871const struct value_string osmo_lu_type_names[] = {
1872 { GSM48_LUPD_NORMAL, "NORMAL" },
1873 { GSM48_LUPD_PERIODIC, "PERIODIC" },
1874 { GSM48_LUPD_IMSI_ATT, "IMSI-ATTACH" },
1875 { GSM48_LUPD_RESERVED, "RESERVED" },
1876 {}
1877};
1878
Neels Hofmeyrf8963f92019-01-10 23:33:32 +01001879const struct value_string osmo_cm_service_type_names[] = {
1880 { GSM48_CMSERV_MO_CALL_PACKET, "MO-Call" },
1881 { GSM48_CMSERV_EMERGENCY, "Emergency-Call" },
1882 { GSM48_CMSERV_SMS, "Short-Messaging-Service" },
1883 { GSM48_CMSERV_SUP_SERV, "Supplementary-Service" },
1884 { GSM48_CMSERV_VGCS, "Voice-Group-Call" },
1885 { GSM48_CMSERV_VBS, "Voice-Broadcast-Call" },
1886 { GSM48_CMSERV_LOC_SERV, "Location-Service" },
1887 {}
1888};
1889
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001890bool osmo_gsm48_classmark1_is_r99(const struct gsm48_classmark1 *cm1)
1891{
1892 return cm1->rev_lev >= 2;
1893}
1894
1895bool osmo_gsm48_classmark2_is_r99(const struct gsm48_classmark2 *cm2, uint8_t cm2_len)
1896{
1897 if (!cm2_len)
1898 return false;
1899 return cm2->rev_lev >= 2;
1900}
1901
1902/*! Return true if any of Classmark 1 or Classmark 2 are present and indicate R99 capability.
1903 * \param[in] cm Classmarks.
1904 * \returns True if R99 or later, false if pre-R99 or no Classmarks are present.
1905 */
1906bool osmo_gsm48_classmark_is_r99(const struct osmo_gsm48_classmark *cm)
1907{
1908 if (cm->classmark1_set)
1909 return osmo_gsm48_classmark1_is_r99(&cm->classmark1);
1910 return osmo_gsm48_classmark2_is_r99(&cm->classmark2, cm->classmark2_len);
1911}
1912
1913/*! Return a string representation of A5 cipher algorithms indicated by Classmark 1, 2 and 3.
1914 * \param[in] cm Classmarks.
1915 * \returns A statically allocated string like "cm1{a5/1=supported} cm2{0x23= A5/2 A5/3} no-cm3"
1916 */
Harald Welte4a62eda2019-03-18 18:27:00 +01001917char *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 +01001918{
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001919 char cm1[42] = "no-cm1";
1920 char cm2[42] = " no-cm2";
Neels Hofmeyr9e6f5f12019-03-11 05:07:56 +01001921 char cm3[42] = " no-cm3";
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001922
1923 if (cm->classmark1_set)
1924 snprintf(cm1, sizeof(cm1), "cm1{a5/1=%s}",
1925 cm->classmark1.a5_1 ? "not-supported":"supported" /* inverted logic */);
1926
1927 if (cm->classmark2_len >= 3)
1928 snprintf(cm2, sizeof(cm2), " cm2{0x%x=%s%s}",
1929 cm->classmark2.a5_2 + (cm->classmark2.a5_3 << 1),
1930 cm->classmark2.a5_2 ? " A5/2" : "",
1931 cm->classmark2.a5_3 ? " A5/3" : "");
1932
1933 if (cm->classmark3_len >= 1)
1934 snprintf(cm3, sizeof(cm3), " cm3{0x%x=%s%s%s%s}",
1935 cm->classmark3[0],
1936 cm->classmark3[0] & (1 << 0) ? " A5/4" : "",
1937 cm->classmark3[0] & (1 << 1) ? " A5/5" : "",
1938 cm->classmark3[0] & (1 << 2) ? " A5/6" : "",
1939 cm->classmark3[0] & (1 << 3) ? " A5/7" : "");
1940
Harald Welte4a62eda2019-03-18 18:27:00 +01001941 snprintf(buf, buf_len, "%s%s%s", cm1, cm2, cm3);
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001942 return buf;
1943}
1944
Harald Welte4a62eda2019-03-18 18:27:00 +01001945/*! Return a string representation of A5 cipher algorithms indicated by Classmark 1, 2 and 3.
1946 * \param[in] cm Classmarks.
1947 * \returns A statically allocated string like "cm1{a5/1=supported} cm2{0x23= A5/2 A5/3} no-cm3"
1948 */
1949const char *osmo_gsm48_classmark_a5_name(const struct osmo_gsm48_classmark *cm)
1950{
Harald Welte171ef822019-03-28 10:49:05 +01001951 static __thread char buf[128];
Harald Welte4a62eda2019-03-18 18:27:00 +01001952 return osmo_gsm48_classmark_a5_name_buf(buf, sizeof(buf), cm);
1953}
1954
Harald Welte179f3572019-03-18 18:38:47 +01001955/*! Return a string representation of A5 cipher algorithms indicated by Classmark 1, 2 and 3.
1956 * \param[in] ctx talloc context from which to allocate output buffer
1957 * \param[in] cm Classmarks.
1958 * \returns string like "cm1{a5/1=supported} cm2{0x23= A5/2 A5/3} no-cm3" in dynamically-allocated
1959 * output buffer.
1960 */
1961char *osmo_gsm48_classmark_a5_name_c(const void *ctx, const struct osmo_gsm48_classmark *cm)
1962{
1963 char *buf = talloc_size(ctx, 128);
1964 if (!buf)
1965 return NULL;
1966 return osmo_gsm48_classmark_a5_name_buf(buf, 128, cm);
1967}
Harald Welte4a62eda2019-03-18 18:27:00 +01001968
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001969/*! Overwrite dst with the Classmark information present in src.
1970 * Add an new Classmark and overwrite in dst what src has to offer, but where src has no Classmark information, leave
1971 * dst unchanged. (For Classmark 2 and 3, dst will exactly match any non-zero Classmark length from src, hence may end
1972 * up with a shorter Classmark after this call.)
1973 * \param[out] dst The target Classmark storage to be updated.
1974 * \param[in] src The new Classmark information to read from.
1975 */
1976void osmo_gsm48_classmark_update(struct osmo_gsm48_classmark *dst, const struct osmo_gsm48_classmark *src)
1977{
1978 if (src->classmark1_set) {
1979 dst->classmark1 = src->classmark1;
1980 dst->classmark1_set = true;
1981 }
1982 if (src->classmark2_len) {
1983 dst->classmark2_len = src->classmark2_len;
1984 dst->classmark2 = src->classmark2;
1985 }
1986 if (src->classmark3_len) {
1987 dst->classmark3_len = src->classmark3_len;
1988 memcpy(dst->classmark3, src->classmark3, OSMO_MIN(sizeof(dst->classmark3), src->classmark3_len));
1989 }
1990}
1991
1992
1993/*! Determine if the given Classmark (1/2/3) value permits a given A5/n cipher.
1994 * \param[in] cm Classmarks.
1995 * \param[in] a5 The N in A5/N for which to query whether support is indicated.
1996 * \return 1 when the given A5/n is permitted, 0 when not (or a5 > 7), and negative if the respective MS Classmark is
1997 * not known, where the negative number indicates the classmark type: -2 means Classmark 2 is not available. The
1998 * idea is that when e.g. A5/3 is requested and the corresponding Classmark 3 is not available, that the caller
1999 * can react by obtaining Classmark 3 and calling again once it is available.
2000 */
2001int osmo_gsm48_classmark_supports_a5(const struct osmo_gsm48_classmark *cm, uint8_t a5)
2002{
2003 switch (a5) {
2004 case 0:
2005 /* all phones must implement A5/0, see 3GPP TS 43.020 4.9 */
2006 return 1;
2007 case 1:
2008 /* 3GPP TS 43.020 4.9 requires A5/1 to be suppored by all phones and actually states:
2009 * "The network shall not provide service to an MS which indicates that it does not
2010 * support the ciphering algorithm A5/1.". However, let's be more tolerant based
2011 * on policy here */
2012 /* See 3GPP TS 24.008 10.5.1.7 */
2013 if (!cm->classmark1_set)
2014 return -1;
2015 /* Inverted logic for this bit! */
2016 return cm->classmark1.a5_1 ? 0 : 1;
2017 case 2:
2018 /* See 3GPP TS 24.008 10.5.1.6 */
2019 if (cm->classmark2_len < 3)
2020 return -2;
2021 return cm->classmark2.a5_2 ? 1 : 0;
2022 case 3:
2023 if (cm->classmark2_len < 3)
2024 return -2;
2025 return cm->classmark2.a5_3 ? 1 : 0;
2026 case 4:
2027 case 5:
2028 case 6:
2029 case 7:
2030 /* See 3GPP TS 24.008 10.5.1.7 */
2031 if (!cm->classmark3_len)
2032 return -3;
2033 return (cm->classmark3[0] & (1 << (a5-4))) ? 1 : 0;
2034 default:
2035 return 0;
2036 }
2037}
2038
Pau Espin Pedrolb99f4ca2019-10-31 13:35:22 +01002039/*! Decode power class from Classmark1/2 RF power capability field.
2040 * \param[in] rf_power_cap The RF power capability field (3 bits).
2041 * \param[in] band the band of the arfcn from where the classmark was received
2042 * \return the MS power class on success, negative on error.
2043 */
2044int8_t osmo_gsm48_rfpowercap2powerclass(enum gsm_band band, uint8_t rf_power_cap)
2045{
2046 switch (band) {
2047 case GSM_BAND_1800:
2048 case GSM_BAND_1900:
2049 if (rf_power_cap > 2)
2050 return -1;
2051 return rf_power_cap + 1;
2052 default:
2053 if (rf_power_cap > 4)
2054 return -1;
2055 return rf_power_cap + 1;
2056 }
2057}
2058
2059
Harald Welte96e2a002017-06-12 21:44:18 +02002060/*! @} */