blob: a45d67bc6ea3f9a5762f97fa1cd1ee81302661d3 [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 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 *
26 */
27
28#include <stdint.h>
29#include <stdio.h>
30#include <string.h>
Maxfe65fa72016-05-10 17:17:05 +020031#include <stdbool.h>
Pau Espin Pedrol45735022017-06-18 14:05:24 +020032#include <inttypes.h>
Neels Hofmeyrc4fce142018-02-20 13:47:08 +010033#include <ctype.h>
Harald Welte61e2bfc2010-03-04 10:53:03 +010034
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010035#include <osmocom/core/utils.h>
Harald Welte95871da2017-05-15 12:11:36 +020036#include <osmocom/core/byteswap.h>
37#include <osmocom/core/bit16gen.h>
38#include <osmocom/core/bit32gen.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010039#include <osmocom/gsm/tlv.h>
40#include <osmocom/gsm/gsm48.h>
Harald Welte94df39e2011-06-26 14:33:57 +020041#include <osmocom/gsm/gsm0502.h>
Maxfb348ee2016-03-30 21:14:53 +020042#include <osmocom/gsm/gsm_utils.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010043#include <osmocom/gsm/protocol/gsm_04_08.h>
Vadim Yanitskiy8d41d722018-04-17 11:17:28 +070044#include <osmocom/gsm/protocol/gsm_04_80.h>
Harald Welte2aee7b12011-06-26 14:20:04 +020045#include <osmocom/gsm/protocol/gsm_08_58.h>
Maxfdca25d2016-07-05 16:06:28 +020046#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
Harald Welte4fb20752010-03-02 23:17:33 +010047
Harald Welte96e2a002017-06-12 21:44:18 +020048/*! \addtogroup gsm0408
49 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020050 * GSM Mobile Radion Interface L3 messages / TS 04.08
Harald Welte96e2a002017-06-12 21:44:18 +020051 */
52
Neels Hofmeyr87e45502017-06-20 00:17:59 +020053/*! TLV parser definitions for TS 04.08 CC */
Harald Welte4fb20752010-03-02 23:17:33 +010054const struct tlv_definition gsm48_att_tlvdef = {
55 .def = {
56 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
57 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
58 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
59 [GSM48_IE_UTC] = { TLV_TYPE_TV },
60 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
61 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
62
63 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
64 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
65 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
66 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
67 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
68 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
69 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
70 [GSM48_IE_NOTIFY] = { TLV_TYPE_TV },
71 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
72 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
73 [GSM48_IE_CONN_BCD] = { TLV_TYPE_TLV },
74 [GSM48_IE_CONN_SUB] = { TLV_TYPE_TLV },
75 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
76 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
77 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
78 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
79 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
80 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
81 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
82 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
83 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
84 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
85 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
86 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
87 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
88 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
89 [GSM48_IE_REPEAT_CIR] = { TLV_TYPE_T },
90 [GSM48_IE_REPEAT_SEQ] = { TLV_TYPE_T },
91 /* FIXME: more elements */
92 },
93};
94
Neels Hofmeyr87e45502017-06-20 00:17:59 +020095/*! TLV parser definitions for TS 04.08 RR */
Andreas Eversberg014cb872010-07-12 09:11:00 +020096const struct tlv_definition gsm48_rr_att_tlvdef = {
97 .def = {
98 /* NOTE: Don't add IE 17 = MOBILE_ID here, it already used. */
99 [GSM48_IE_VGCS_TARGET] = { TLV_TYPE_TLV },
100 [GSM48_IE_FRQSHORT_AFTER] = { TLV_TYPE_FIXED, 9 },
101 [GSM48_IE_MUL_RATE_CFG] = { TLV_TYPE_TLV },
102 [GSM48_IE_FREQ_L_AFTER] = { TLV_TYPE_TLV },
103 [GSM48_IE_MSLOT_DESC] = { TLV_TYPE_TLV },
104 [GSM48_IE_CHANMODE_2] = { TLV_TYPE_TV },
105 [GSM48_IE_FRQSHORT_BEFORE] = { TLV_TYPE_FIXED, 9 },
106 [GSM48_IE_CHANMODE_3] = { TLV_TYPE_TV },
107 [GSM48_IE_CHANMODE_4] = { TLV_TYPE_TV },
108 [GSM48_IE_CHANMODE_5] = { TLV_TYPE_TV },
109 [GSM48_IE_CHANMODE_6] = { TLV_TYPE_TV },
110 [GSM48_IE_CHANMODE_7] = { TLV_TYPE_TV },
111 [GSM48_IE_CHANMODE_8] = { TLV_TYPE_TV },
112 [GSM48_IE_FREQ_L_BEFORE] = { TLV_TYPE_TLV },
113 [GSM48_IE_CH_DESC_1_BEFORE] = { TLV_TYPE_FIXED, 3 },
114 [GSM48_IE_CH_DESC_2_BEFORE] = { TLV_TYPE_FIXED, 3 },
115 [GSM48_IE_F_CH_SEQ_BEFORE] = { TLV_TYPE_FIXED, 9 },
116 [GSM48_IE_CLASSMARK3] = { TLV_TYPE_TLV },
117 [GSM48_IE_MA_BEFORE] = { TLV_TYPE_TLV },
118 [GSM48_IE_RR_PACKET_UL] = { TLV_TYPE_TLV },
119 [GSM48_IE_RR_PACKET_DL] = { TLV_TYPE_TLV },
120 [GSM48_IE_CELL_CH_DESC] = { TLV_TYPE_FIXED, 16 },
121 [GSM48_IE_CHANMODE_1] = { TLV_TYPE_TV },
122 [GSM48_IE_CHDES_2_AFTER] = { TLV_TYPE_FIXED, 3 },
123 [GSM48_IE_MODE_SEC_CH] = { TLV_TYPE_TV },
124 [GSM48_IE_F_CH_SEQ_AFTER] = { TLV_TYPE_FIXED, 9 },
125 [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 },
133 [GSM48_IE_GROUP_CIP_SEQ] = { TLV_TYPE_SINGLE_TV },
134 [GSM48_IE_CIP_MODE_SET] = { TLV_TYPE_SINGLE_TV },
135 [GSM48_IE_GPRS_RESUMPT] = { TLV_TYPE_SINGLE_TV },
136 [GSM48_IE_SYNC_IND] = { TLV_TYPE_SINGLE_TV },
137 },
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 },
152 [GSM48_IE_PRIORITY_LEV] = { TLV_TYPE_SINGLE_TV },
153 [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" },
165 { GSM48_RR_CAUSE_HNDOVER_IMP, "Handover impossible, timing advance out of range" },
166 { GSM48_RR_CAUSE_CHAN_MODE_UNACCT, "Channel mode unacceptable" },
167 { GSM48_RR_CAUSE_FREQ_NOT_IMPL, "Frequency not implemented" },
168 { GSM48_RR_CAUSE_CALL_CLEARED, "Call already cleared" },
169 { GSM48_RR_CAUSE_SEMANT_INCORR, "Semantically incorrect message" },
170 { GSM48_RR_CAUSE_INVALID_MAND_INF, "Invalid mandatory information" },
171 { GSM48_RR_CAUSE_MSG_TYPE_N, "Message type non-existant or not implemented" },
172 { GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT, "Message type not compatible with protocol state" },
173 { GSM48_RR_CAUSE_COND_IE_ERROR, "Conditional IE error" },
174 { GSM48_RR_CAUSE_NO_CELL_ALLOC_A, "No cell allocation available" },
175 { GSM48_RR_CAUSE_PROT_ERROR_UNSPC, "Protocol error unspecified" },
176 { 0, NULL },
Harald Welte4fb20752010-03-02 23:17:33 +0100177};
178
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200179/*! return string representation of RR Cause value */
Philippada00422016-10-27 13:30:16 +0200180const char *rr_cause_name(uint8_t cause)
181{
182 return get_value_string(rr_cause_names, cause);
183}
184
Harald Welte4a62eda2019-03-18 18:27:00 +0100185/*! Return MCC-MNC-LAC-RAC as string, in a caller-provided output buffer.
186 * \param[out] buf caller-provided output buffer
187 * \param[in] buf_len size of buf in bytes
188 * \param[in] rai RAI to encode.
189 * \returns buf
190 */
191char *osmo_rai_name_buf(char *buf, size_t buf_len, const struct gprs_ra_id *rai)
192{
193 snprintf(buf, buf_len, "%s-%s-%u-%u",
194 osmo_mcc_name(rai->mcc), osmo_mnc_name(rai->mnc, rai->mnc_3_digits), rai->lac,
195 rai->rac);
196 return buf;
197}
198
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100199/*! Return MCC-MNC-LAC-RAC as string, in a static buffer.
200 * \param[in] rai RAI to encode.
201 * \returns Static string buffer.
202 */
203const char *osmo_rai_name(const struct gprs_ra_id *rai)
204{
205 static char buf[32];
Harald Welte4a62eda2019-03-18 18:27:00 +0100206 return osmo_rai_name_buf(buf, sizeof(buf), rai);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100207}
208
Harald Welte9eb6d882010-03-25 12:00:54 +0800209/* FIXME: convert to value_string */
Andreas Eversberg014cb872010-07-12 09:11:00 +0200210static const char *cc_state_names[32] = {
Harald Welte4fb20752010-03-02 23:17:33 +0100211 "NULL",
212 "INITIATED",
Andreas Eversberg1ef041f2010-04-09 07:52:12 +0200213 "MM_CONNECTION_PEND",
Harald Welte4fb20752010-03-02 23:17:33 +0100214 "MO_CALL_PROC",
215 "CALL_DELIVERED",
216 "illegal state 5",
217 "CALL_PRESENT",
218 "CALL_RECEIVED",
219 "CONNECT_REQUEST",
220 "MO_TERM_CALL_CONF",
221 "ACTIVE",
222 "DISCONNECT_REQ",
223 "DISCONNECT_IND",
224 "illegal state 13",
225 "illegal state 14",
226 "illegal state 15",
227 "illegal state 16",
228 "illegal state 17",
229 "illegal state 18",
230 "RELEASE_REQ",
231 "illegal state 20",
232 "illegal state 21",
233 "illegal state 22",
234 "illegal state 23",
235 "illegal state 24",
236 "illegal state 25",
237 "MO_ORIG_MODIFY",
238 "MO_TERM_MODIFY",
239 "CONNECT_IND",
240 "illegal state 29",
241 "illegal state 30",
242 "illegal state 31",
243};
244
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200245/*! return string representation of CC State */
Harald Welte9eb6d882010-03-25 12:00:54 +0800246const char *gsm48_cc_state_name(uint8_t state)
247{
248 if (state < ARRAY_SIZE(cc_state_names))
249 return cc_state_names[state];
250
251 return "invalid";
252}
253
254static const struct value_string cc_msg_names[] = {
255 { GSM48_MT_CC_ALERTING, "ALERTING" },
256 { GSM48_MT_CC_CALL_PROC, "CALL_PROC" },
257 { GSM48_MT_CC_PROGRESS, "PROGRESS" },
258 { GSM48_MT_CC_ESTAB, "ESTAB" },
259 { GSM48_MT_CC_SETUP, "SETUP" },
260 { GSM48_MT_CC_ESTAB_CONF, "ESTAB_CONF" },
261 { GSM48_MT_CC_CONNECT, "CONNECT" },
262 { GSM48_MT_CC_CALL_CONF, "CALL_CONF" },
263 { GSM48_MT_CC_START_CC, "START_CC" },
264 { GSM48_MT_CC_RECALL, "RECALL" },
265 { GSM48_MT_CC_EMERG_SETUP, "EMERG_SETUP" },
266 { GSM48_MT_CC_CONNECT_ACK, "CONNECT_ACK" },
267 { GSM48_MT_CC_USER_INFO, "USER_INFO" },
268 { GSM48_MT_CC_MODIFY_REJECT, "MODIFY_REJECT" },
269 { GSM48_MT_CC_MODIFY, "MODIFY" },
270 { GSM48_MT_CC_HOLD, "HOLD" },
271 { GSM48_MT_CC_HOLD_ACK, "HOLD_ACK" },
272 { GSM48_MT_CC_HOLD_REJ, "HOLD_REJ" },
273 { GSM48_MT_CC_RETR, "RETR" },
274 { GSM48_MT_CC_RETR_ACK, "RETR_ACK" },
275 { GSM48_MT_CC_RETR_REJ, "RETR_REJ" },
276 { GSM48_MT_CC_MODIFY_COMPL, "MODIFY_COMPL" },
277 { GSM48_MT_CC_DISCONNECT, "DISCONNECT" },
278 { GSM48_MT_CC_RELEASE_COMPL, "RELEASE_COMPL" },
279 { GSM48_MT_CC_RELEASE, "RELEASE" },
280 { GSM48_MT_CC_STOP_DTMF, "STOP_DTMF" },
281 { GSM48_MT_CC_STOP_DTMF_ACK, "STOP_DTMF_ACK" },
282 { GSM48_MT_CC_STATUS_ENQ, "STATUS_ENQ" },
283 { GSM48_MT_CC_START_DTMF, "START_DTMF" },
284 { GSM48_MT_CC_START_DTMF_ACK, "START_DTMF_ACK" },
285 { GSM48_MT_CC_START_DTMF_REJ, "START_DTMF_REJ" },
286 { GSM48_MT_CC_CONG_CTRL, "CONG_CTRL" },
287 { GSM48_MT_CC_FACILITY, "FACILITY" },
288 { GSM48_MT_CC_STATUS, "STATUS" },
289 { GSM48_MT_CC_NOTIFY, "NOTFIY" },
290 { 0, NULL }
Harald Welte61e2bfc2010-03-04 10:53:03 +0100291};
292
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200293/*! return string representation of CC Message Type */
Harald Welte9eb6d882010-03-25 12:00:54 +0800294const char *gsm48_cc_msg_name(uint8_t msgtype)
295{
296 return get_value_string(cc_msg_names, msgtype);
297}
Harald Welte4fb20752010-03-02 23:17:33 +0100298
Philipp72e43f02016-10-27 13:35:20 +0200299
300static const struct value_string rr_msg_names[] = {
301 /* Channel establishment messages */
302 { GSM48_MT_RR_INIT_REQ, "RR INITIALISATION REQUEST" },
303 { GSM48_MT_RR_ADD_ASS, "ADDITIONAL ASSIGNMENT" },
304 { GSM48_MT_RR_IMM_ASS, "IMMEDIATE ASSIGNMENT" },
305 { GSM48_MT_RR_IMM_ASS_EXT, "MMEDIATE ASSIGNMENT EXTENDED" },
306 { GSM48_MT_RR_IMM_ASS_REJ, "IMMEDIATE ASSIGNMENT REJECT" },
307 { GSM48_MT_RR_DTM_ASS_FAIL, "DTM ASSIGNMENT FAILURE" },
308 { GSM48_MT_RR_DTM_REJECT, "DTM REJECT" },
309 { GSM48_MT_RR_DTM_REQUEST, "DTM REQUEST" },
310 { GSM48_MT_RR_PACKET_ASS, "PACKET ASSIGNMENT" },
311
312 /* Ciphering messages */
313 { GSM48_MT_RR_CIPH_M_CMD, "CIPHERING MODE COMMAND" },
314 { GSM48_MT_RR_CIPH_M_COMPL, "CIPHERING MODE COMPLETE" },
315
316 /* Configuration change messages */
317 { GSM48_MT_RR_CFG_CHG_CMD, "CONFIGURATION CHANGE COMMAND" },
318 { GSM48_MT_RR_CFG_CHG_ACK, "CONFIGURATION CHANGE ACK" },
319 { GSM48_MT_RR_CFG_CHG_REJ, "CONFIGURATION CHANGE REJECT" },
320
321 /* Handover messages */
322 { GSM48_MT_RR_ASS_CMD, "ASSIGNMENT COMMAND" },
323 { GSM48_MT_RR_ASS_COMPL, "ASSIGNMENT COMPLETE" },
324 { GSM48_MT_RR_ASS_FAIL, "ASSIGNMENT FAILURE" },
325 { GSM48_MT_RR_HANDO_CMD, "HANDOVER COMMAND" },
326 { GSM48_MT_RR_HANDO_COMPL, "HANDOVER COMPLETE" },
327 { GSM48_MT_RR_HANDO_FAIL, "HANDOVER FAILURE" },
328 { GSM48_MT_RR_HANDO_INFO, "PHYSICAL INFORMATION" },
329 { GSM48_MT_RR_DTM_ASS_CMD, "DTM ASSIGNMENT COMMAND" },
330
331 { GSM48_MT_RR_CELL_CHG_ORDER, "RR-CELL CHANGE ORDER" },
332 { GSM48_MT_RR_PDCH_ASS_CMD, "PDCH ASSIGNMENT COMMAND" },
333
334 /* Channel release messages */
335 { GSM48_MT_RR_CHAN_REL, "CHANNEL RELEASE" },
336 { GSM48_MT_RR_PART_REL, "PARTIAL RELEASE" },
337 { GSM48_MT_RR_PART_REL_COMP, "PARTIAL RELEASE COMPLETE" },
338
339 /* Paging and Notification messages */
340 { GSM48_MT_RR_PAG_REQ_1, "PAGING REQUEST TYPE 1" },
341 { GSM48_MT_RR_PAG_REQ_2, "PAGING REQUEST TYPE 2" },
342 { GSM48_MT_RR_PAG_REQ_3, "PAGING REQUEST TYPE 3" },
343 { GSM48_MT_RR_PAG_RESP, "PAGING RESPONSE" },
344 { GSM48_MT_RR_NOTIF_NCH, "NOTIFICATION/NCH" },
345 { GSM48_MT_RR_NOTIF_FACCH, "(Reserved)" },
346 { GSM48_MT_RR_NOTIF_RESP, "NOTIFICATION/RESPONSE" },
347 { GSM48_MT_RR_PACKET_NOTIF, "PACKET NOTIFICATION" },
348 /* 3G Specific messages */
349 { GSM48_MT_RR_UTRAN_CLSM_CHG, "UTRAN Classmark Change" },
350 { GSM48_MT_RR_CDMA2K_CLSM_CHG, "cdma 2000 Classmark Change" },
351 { GSM48_MT_RR_IS_TO_UTRAN_HANDO, "Inter System to UTRAN Handover Command" },
352 { GSM48_MT_RR_IS_TO_CDMA2K_HANDO, "Inter System to cdma2000 Handover Command" },
353
354 /* System information messages */
355 { GSM48_MT_RR_SYSINFO_8, "SYSTEM INFORMATION TYPE 8" },
356 { GSM48_MT_RR_SYSINFO_1, "SYSTEM INFORMATION TYPE 1" },
357 { GSM48_MT_RR_SYSINFO_2, "SYSTEM INFORMATION TYPE 2" },
358 { GSM48_MT_RR_SYSINFO_3, "SYSTEM INFORMATION TYPE 3" },
359 { GSM48_MT_RR_SYSINFO_4, "SYSTEM INFORMATION TYPE 4" },
360 { GSM48_MT_RR_SYSINFO_5, "SYSTEM INFORMATION TYPE 5" },
361 { GSM48_MT_RR_SYSINFO_6, "SYSTEM INFORMATION TYPE 6" },
362 { GSM48_MT_RR_SYSINFO_7, "SYSTEM INFORMATION TYPE 7" },
363 { GSM48_MT_RR_SYSINFO_2bis, "SYSTEM INFORMATION TYPE 2bis" },
364 { GSM48_MT_RR_SYSINFO_2ter, "SYSTEM INFORMATION TYPE 2ter" },
365 { GSM48_MT_RR_SYSINFO_2quater, "SYSTEM INFORMATION TYPE 2quater" },
366 { GSM48_MT_RR_SYSINFO_5bis, "SYSTEM INFORMATION TYPE 5bis" },
367 { GSM48_MT_RR_SYSINFO_5ter, "SYSTEM INFORMATION TYPE 5ter" },
368 { GSM48_MT_RR_SYSINFO_9, "SYSTEM INFORMATION TYPE 9" },
369 { GSM48_MT_RR_SYSINFO_13, "SYSTEM INFORMATION TYPE 13" },
370 { GSM48_MT_RR_SYSINFO_16, "SYSTEM INFORMATION TYPE 16" },
371 { GSM48_MT_RR_SYSINFO_17, "SYSTEM INFORMATION TYPE 17" },
372 { GSM48_MT_RR_SYSINFO_18, "SYSTEM INFORMATION TYPE 18" },
373 { GSM48_MT_RR_SYSINFO_19, "SYSTEM INFORMATION TYPE 19" },
374 { GSM48_MT_RR_SYSINFO_20, "SYSTEM INFORMATION TYPE 20" },
375
376 /* Miscellaneous messages */
377 { GSM48_MT_RR_CHAN_MODE_MODIF, "CHANNEL MODE MODIFY" },
378 { GSM48_MT_RR_STATUS, "RR STATUS" },
379 { GSM48_MT_RR_CHAN_MODE_MODIF_ACK, "CHANNEL MODE MODIFY ACKNOWLEDGE" },
380 { GSM48_MT_RR_FREQ_REDEF, "FREQUENCY REDEFINITION" },
381 { GSM48_MT_RR_MEAS_REP, "MEASUREMENT REPORT" },
382 { GSM48_MT_RR_CLSM_CHG, "CLASSMARK CHANGE" },
383 { GSM48_MT_RR_CLSM_ENQ, "CLASSMARK ENQUIRY" },
384 { GSM48_MT_RR_EXT_MEAS_REP, "EXTENDED MEASUREMENT REPORT" },
385 { GSM48_MT_RR_EXT_MEAS_REP_ORD, "EXTENDED MEASUREMENT ORDER" },
386 { GSM48_MT_RR_GPRS_SUSP_REQ, "GPRS SUSPENSION REQUEST" },
387 { GSM48_MT_RR_DTM_INFO, "DTM INFORMATION" },
388
389 /* VGCS uplink control messages */
390 { GSM48_MT_RR_VGCS_UPL_GRANT, "VGCS UPLINK GRANT" },
391 { GSM48_MT_RR_UPLINK_RELEASE, "UPLINK RELEASE" },
392 { GSM48_MT_RR_UPLINK_FREE, "0c" },
393 { GSM48_MT_RR_UPLINK_BUSY, "UPLINK BUSY" },
394 { GSM48_MT_RR_TALKER_IND, "TALKER INDICATION" },
395
396 /* Application messages */
397 { GSM48_MT_RR_APP_INFO, "Application Information" },
398 { 0, NULL }
399};
400
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200401/*! return string representation of RR Message Type */
Philipp72e43f02016-10-27 13:35:20 +0200402const char *gsm48_rr_msg_name(uint8_t msgtype)
403{
404 return get_value_string(rr_msg_names, msgtype);
405}
406
407
Maxfb348ee2016-03-30 21:14:53 +0200408const struct value_string gsm48_chan_mode_names[] = {
409 { GSM48_CMODE_SIGN, "SIGNALLING" },
410 { GSM48_CMODE_SPEECH_V1, "SPEECH_V1" },
411 { GSM48_CMODE_SPEECH_EFR, "SPEECH_EFR" },
412 { GSM48_CMODE_SPEECH_AMR, "SPEECH_AMR" },
413 { GSM48_CMODE_DATA_14k5, "DATA_14k5" },
414 { GSM48_CMODE_DATA_12k0, "DATA_12k0" },
415 { GSM48_CMODE_DATA_6k0, "DATA_6k0" },
416 { GSM48_CMODE_DATA_3k6, "DATA_3k6" },
417 { 0, NULL },
418};
419
420const struct value_string gsm_chan_t_names[] = {
421 { GSM_LCHAN_NONE, "NONE" },
422 { GSM_LCHAN_SDCCH, "SDCCH" },
423 { GSM_LCHAN_TCH_F, "TCH_F" },
424 { GSM_LCHAN_TCH_H, "TCH_H" },
425 { GSM_LCHAN_UNKNOWN, "UNKNOWN" },
426 { GSM_LCHAN_CCCH, "CCCH" },
427 { GSM_LCHAN_PDTCH, "PDTCH" },
428 { GSM_LCHAN_CBCH, "CBCH" },
429 { 0, NULL },
430};
431
Harald Welte1a8c4e02015-08-16 17:56:25 +0200432static const struct value_string mi_type_names[] = {
433 { GSM_MI_TYPE_NONE, "NONE" },
434 { GSM_MI_TYPE_IMSI, "IMSI" },
435 { GSM_MI_TYPE_IMEI, "IMEI" },
436 { GSM_MI_TYPE_IMEISV, "IMEI-SV" },
437 { GSM_MI_TYPE_TMSI, "TMSI" },
438 { 0, NULL }
439};
440
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200441/*! return string representation of Mobile Identity Type */
Harald Welte1a8c4e02015-08-16 17:56:25 +0200442const char *gsm48_mi_type_name(uint8_t mi)
443{
444 return get_value_string(mi_type_names, mi);
445}
446
Harald Welte4a62eda2019-03-18 18:27:00 +0100447/*! Return a human readable representation of a Mobile Identity in caller-provided buffer.
448 * \param[out] buf caller-provided output buffer
449 * \param[in] buf_len size of buf in bytes
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100450 * \param[in] mi Mobile Identity buffer containing 3GPP TS 04.08 style MI type and data.
451 * \param[in] mi_len Length of mi.
Harald Welte4a62eda2019-03-18 18:27:00 +0100452 * \return buf
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100453 */
Harald Welte4a62eda2019-03-18 18:27:00 +0100454char *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 +0100455{
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100456 uint8_t mi_type;
457 uint32_t tmsi;
458 char mi_string[GSM48_MI_SIZE];
459
460 mi_type = (mi && mi_len) ? (mi[0] & GSM_MI_TYPE_MASK) : GSM_MI_TYPE_NONE;
461
462 switch (mi_type) {
463 case GSM_MI_TYPE_TMSI:
464 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
465 if (mi_len == GSM48_TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
466 tmsi = osmo_load32be(&mi[1]);
Harald Welte4a62eda2019-03-18 18:27:00 +0100467 snprintf(buf, buf_len, "TMSI-0x%08" PRIX32, tmsi);
468 return buf;
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100469 }
470 return "TMSI-invalid";
471
472 case GSM_MI_TYPE_IMSI:
473 case GSM_MI_TYPE_IMEI:
474 case GSM_MI_TYPE_IMEISV:
475 osmo_bcd2str(mi_string, sizeof(mi_string), mi, 1, (mi_len * 2) - (mi[0] & GSM_MI_ODD ? 0 : 1), true);
Harald Welte4a62eda2019-03-18 18:27:00 +0100476 snprintf(buf, buf_len, "%s-%s", gsm48_mi_type_name(mi_type), mi_string);
477 return buf;
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100478
479 default:
480 return "unknown";
481 }
482}
483
Harald Welte4a62eda2019-03-18 18:27:00 +0100484/*! Return a human readable representation of a Mobile Identity in static buffer.
485 * \param[in] mi Mobile Identity buffer containing 3GPP TS 04.08 style MI type and data.
486 * \param[in] mi_len Length of mi.
487 * \return A string like "IMSI-1234567", "TMSI-0x1234ABCD" or "unknown", "TMSI-invalid"...
488 */
489const char *osmo_mi_name(const uint8_t *mi, uint8_t mi_len)
490{
491 static char mi_name[10 + GSM48_MI_SIZE + 1];
492 return osmo_mi_name_buf(mi_name, sizeof(mi_name), mi, mi_len);
493}
494
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200495/*! Checks is particular message is cipherable in A/Gb mode according to
Maxfdca25d2016-07-05 16:06:28 +0200496 * 3GPP TS 24.008 § 4.7.1.2
497 * \param[in] hdr Message header
498 * \return true if message can be encrypted, false otherwise
499 */
500bool gsm48_hdr_gmm_cipherable(const struct gsm48_hdr *hdr)
501{
502 switch(hdr->msg_type) {
503 case GSM48_MT_GMM_ATTACH_REQ:
504 case GSM48_MT_GMM_ATTACH_REJ:
505 case GSM48_MT_GMM_AUTH_CIPH_REQ:
506 case GSM48_MT_GMM_AUTH_CIPH_RESP:
507 case GSM48_MT_GMM_AUTH_CIPH_REJ:
508 case GSM48_MT_GMM_AUTH_CIPH_FAIL:
509 case GSM48_MT_GMM_ID_REQ:
510 case GSM48_MT_GMM_ID_RESP:
511 case GSM48_MT_GMM_RA_UPD_REQ:
512 case GSM48_MT_GMM_RA_UPD_REJ:
513 return false;
514 default:
515 return true;
516 }
517}
518
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100519/* Convert MCC + MNC to BCD representation, legacy implementation.
520 * Instead use osmo_plmn_to_bcd(), which is also capable of converting
521 * 3-digit MNC that have leading zeros. For parameters, also see there. */
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100522void gsm48_mcc_mnc_to_bcd(uint8_t *bcd_dst, uint16_t mcc, uint16_t mnc)
Harald Welte61e2bfc2010-03-04 10:53:03 +0100523{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100524 const struct osmo_plmn_id plmn = {
525 .mcc = mcc,
526 .mnc = mnc,
527 .mnc_3_digits = false,
528 };
529 osmo_plmn_to_bcd(bcd_dst, &plmn);
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100530}
Harald Welte61e2bfc2010-03-04 10:53:03 +0100531
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100532/* Convert given 3-byte BCD buffer to integers, legacy implementation.
533 * Instead use osmo_plmn_from_bcd(), which is also capable of converting
534 * 3-digit MNC that have leading zeros. For parameters, also see there. */
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100535void gsm48_mcc_mnc_from_bcd(uint8_t *bcd_src, uint16_t *mcc, uint16_t *mnc)
536{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100537 struct osmo_plmn_id plmn;
538 osmo_plmn_from_bcd(bcd_src, &plmn);
539 *mcc = plmn.mcc;
540 *mnc = plmn.mnc;
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100541}
542
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100543/*! Encode TS 04.08 Location Area Identifier, legacy implementation.
544 * Instead use gsm48_generate_lai2(), which is capable of three-digit MNC with leading zeros.
545 * \param[out] lai48 caller-provided memory for output
Harald Welte96e2a002017-06-12 21:44:18 +0200546 * \param[in] mcc Mobile Country Code
547 * \param[in] mnc Mobile Network Code
548 * \param[in] lac Location Area Code */
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100549void gsm48_generate_lai(struct gsm48_loc_area_id *lai48, uint16_t mcc,
550 uint16_t mnc, uint16_t lac)
551{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100552 const struct osmo_location_area_id lai = {
553 .plmn = {
554 .mcc = mcc,
555 .mnc = mnc,
556 .mnc_3_digits = false,
557 },
558 .lac = lac,
559 };
560 gsm48_generate_lai2(lai48, &lai);
Harald Welte61e2bfc2010-03-04 10:53:03 +0100561}
562
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100563/*! Encode TS 04.08 Location Area Identifier.
564 * \param[out] lai48 caller-provided memory for output.
565 * \param[in] lai input of MCC-MNC-LAC. */
566void gsm48_generate_lai2(struct gsm48_loc_area_id *lai48, const struct osmo_location_area_id *lai)
567{
568 osmo_plmn_to_bcd(&lai48->digits[0], &lai->plmn);
569 lai48->lac = osmo_htons(lai->lac);
570}
571
572/*! Decode TS 04.08 Location Area Identifier, legacy implementation.
573 * Instead use gsm48_decode_lai2(), which is capable of three-digit MNC with leading zeros.
Harald Welte96e2a002017-06-12 21:44:18 +0200574 * \param[in] Location Area Identifier (encoded)
575 * \param[out] mcc Mobile Country Code
576 * \param[out] mnc Mobile Network Code
577 * \param[out] lac Location Area Code
578 * \returns 0
579 *
580 * Attention: this function returns true integers, not hex! */
Harald Welte774a9de2012-07-13 21:35:13 +0200581int gsm48_decode_lai(struct gsm48_loc_area_id *lai, uint16_t *mcc,
582 uint16_t *mnc, uint16_t *lac)
583{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100584 struct osmo_location_area_id decoded;
585 gsm48_decode_lai2(lai, &decoded);
586 *mcc = decoded.plmn.mcc;
587 *mnc = decoded.plmn.mnc;
588 *lac = decoded.lac;
Harald Welte774a9de2012-07-13 21:35:13 +0200589 return 0;
590}
591
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100592/*! Decode TS 04.08 Location Area Identifier.
593 * \param[in] Location Area Identifier (encoded).
594 * \param[out] decoded Target buffer to write decoded values of MCC-MNC-LAC.
595 *
596 * Attention: this function returns true integers, not hex! */
597void gsm48_decode_lai2(const struct gsm48_loc_area_id *lai, struct osmo_location_area_id *decoded)
598{
599 osmo_plmn_from_bcd(&lai->digits[0], &decoded->plmn);
600 decoded->lac = osmo_ntohs(lai->lac);
601}
602
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200603/*! Set DTX mode in Cell Options IE (3GPP TS 44.018)
Maxfe65fa72016-05-10 17:17:05 +0200604 * \param[in] op Cell Options structure in which DTX parameters will be set
605 * \param[in] full Mode for full-rate channels
606 * \param[in] half Mode for half-rate channels
607 * \param[in] is_bcch Indicates if we should use 10.5.2.3.1 instead of
608 * 10.5.2.3a.2
609 *
610 * There is no space for separate DTX settings for Full and Half rate channels
611 * in BCCH - in this case full setting is used for both and half parameter is
612 * ignored.
613 */
614void gsm48_set_dtx(struct gsm48_cell_options *op, enum gsm48_dtx_mode full,
615 enum gsm48_dtx_mode half, bool is_bcch)
616{
617 if (is_bcch) {
618 switch (full) {
619 case GSM48_DTX_MAY_BE_USED:
620 op->dtx = 0;
621 return;
622 case GSM48_DTX_SHALL_BE_USED:
623 op->dtx = 1;
624 return;
625 case GSM48_DTX_SHALL_NOT_BE_USED:
626 op->dtx = 2;
627 return;
628 }
629 } else {
630 switch (full) {
631 case GSM48_DTX_MAY_BE_USED:
632 op->dtx = (half == GSM48_DTX_SHALL_BE_USED) ? 3 : 0;
633 op->d = (half == GSM48_DTX_SHALL_NOT_BE_USED) ? 0 : 1;
634 return;
635 case GSM48_DTX_SHALL_BE_USED:
636 op->dtx = (half == GSM48_DTX_MAY_BE_USED) ? 3 : 1;
637 op->d = (half == GSM48_DTX_SHALL_BE_USED) ? 1 : 0;
638 return;
639 case GSM48_DTX_SHALL_NOT_BE_USED:
640 op->dtx = 2;
641 op->d = (half == GSM48_DTX_SHALL_BE_USED) ? 1 : 0;
642 return;
643 }
644 }
645}
646
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200647/*! Generate TS 04.08 Mobile ID from TMSI
Harald Welte96e2a002017-06-12 21:44:18 +0200648 * \param[out] buf Caller-provided output buffer (7 bytes)
649 * \param[in] tmsi TMSI to be encoded
650 * \returns number of byes encoded (always 7) */
Harald Welte61e2bfc2010-03-04 10:53:03 +0100651int gsm48_generate_mid_from_tmsi(uint8_t *buf, uint32_t tmsi)
652{
Harald Welte95871da2017-05-15 12:11:36 +0200653 uint32_t tmsi_be = osmo_htonl(tmsi);
Harald Welte61e2bfc2010-03-04 10:53:03 +0100654
655 buf[0] = GSM48_IE_MOBILE_ID;
656 buf[1] = GSM48_TMSI_LEN;
657 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Holger Hans Peter Freythercd252e32013-07-03 09:56:53 +0200658 memcpy(&buf[3], &tmsi_be, sizeof(tmsi_be));
Harald Welte61e2bfc2010-03-04 10:53:03 +0100659
660 return 7;
661}
662
Harald Welte1c3bae12019-01-20 10:37:49 +0100663/*! Generate TS 24.008 §10.5.1.4 Mobile ID of BCD type from ASCII string
664 * \param[out] buf Caller-provided output buffer of at least GSM48_MID_MAX_SIZE bytes
Maxebf14922018-02-15 11:42:11 +0100665 * \param[in] id Identity to be encoded
Harald Welte1c3bae12019-01-20 10:37:49 +0100666 * \param[in] mi_type Type of identity (e.g. GSM_MI_TYPE_IMSI, IMEI, IMEISV)
Maxebf14922018-02-15 11:42:11 +0100667 * \returns number of bytes used in \a buf */
668uint8_t gsm48_generate_mid(uint8_t *buf, const char *id, uint8_t mi_type)
669{
Harald Welte1c3bae12019-01-20 10:37:49 +0100670 uint8_t length = strnlen(id, 16), i, off = 0, odd = (length & 1) == 1;
671 /* maximum length == 16 (IMEISV) */
Maxebf14922018-02-15 11:42:11 +0100672
673 buf[0] = GSM48_IE_MOBILE_ID;
Neels Hofmeyr23187fa2018-12-05 23:24:50 +0100674 buf[2] = osmo_char2bcd(id[0]) << 4 | (mi_type & GSM_MI_TYPE_MASK) | (odd << 3);
Maxebf14922018-02-15 11:42:11 +0100675
676 /* if the length is even we will fill half of the last octet */
677 buf[1] = (length + (odd ? 1 : 2)) >> 1;
Harald Welte1c3bae12019-01-20 10:37:49 +0100678 /* buf[1] maximum = 18/2 = 9 */
679 OSMO_ASSERT(buf[1] <= 9);
Maxebf14922018-02-15 11:42:11 +0100680
681 for (i = 1; i < buf[1]; ++i) {
682 uint8_t upper, lower = osmo_char2bcd(id[++off]);
683 if (!odd && off + 1 == length)
684 upper = 0x0f;
685 else
686 upper = osmo_char2bcd(id[++off]) & 0x0f;
687
688 buf[2 + i] = (upper << 4) | lower;
689 }
690
Harald Welte1c3bae12019-01-20 10:37:49 +0100691 /* maximum return value: 2 + 9 = 11 */
Maxebf14922018-02-15 11:42:11 +0100692 return 2 + buf[1];
693}
694
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200695/*! Generate TS 04.08 Mobile ID from IMSI
Harald Welte96e2a002017-06-12 21:44:18 +0200696 * \param[out] buf Caller-provided output buffer
697 * \param[in] imsi IMSI to be encoded
698 * \returns number of bytes used in \a buf */
Harald Welte61e2bfc2010-03-04 10:53:03 +0100699int gsm48_generate_mid_from_imsi(uint8_t *buf, const char *imsi)
700{
Maxebf14922018-02-15 11:42:11 +0100701 return gsm48_generate_mid(buf, imsi, GSM_MI_TYPE_IMSI);
Harald Welte61e2bfc2010-03-04 10:53:03 +0100702}
Harald Welte9bb553e2010-03-28 18:14:50 +0800703
Neels Hofmeyr627e0112018-12-05 21:32:40 +0100704/*! Convert TS 04.08 Mobile Identity (10.5.1.4) to string.
705 * 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 +0200706 * \param[out] string Caller-provided buffer for output
707 * \param[in] str_len Length of \a string in bytes
708 * \param[in] mi Mobile Identity to be stringified
709 * \param[in] mi_len Length of \a mi in bytes
Neels Hofmeyr6aa20ee2018-12-06 00:40:37 +0100710 * \returns WARNING: the return value of this function is not well implemented.
711 * Depending on the MI type and amount of output buffer, this may return
712 * the nr of written bytes, or the written strlen(), or the snprintf()
713 * style strlen()-if-the-buffer-were-large-enough. */
Harald Welte163d0ea2010-04-09 07:57:40 +0200714int gsm48_mi_to_string(char *string, const int str_len, const uint8_t *mi,
715 const int mi_len)
Harald Welte9bb553e2010-03-28 18:14:50 +0800716{
Neels Hofmeyr627e0112018-12-05 21:32:40 +0100717 int rc;
Harald Welte163d0ea2010-04-09 07:57:40 +0200718 uint8_t mi_type;
Harald Welte163d0ea2010-04-09 07:57:40 +0200719 uint32_t tmsi;
Harald Welte9bb553e2010-03-28 18:14:50 +0800720
Neels Hofmeyr6adffb92018-12-05 23:30:31 +0100721 mi_type = (mi && mi_len) ? (mi[0] & GSM_MI_TYPE_MASK) : GSM_MI_TYPE_NONE;
Harald Welte9bb553e2010-03-28 18:14:50 +0800722
723 switch (mi_type) {
Harald Welte9bb553e2010-03-28 18:14:50 +0800724 case GSM_MI_TYPE_TMSI:
Holger Freyther45abec22016-05-20 19:21:27 +0000725 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
Harald Welte9bb553e2010-03-28 18:14:50 +0800726 if (mi_len == GSM48_TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
Harald Welte95871da2017-05-15 12:11:36 +0200727 tmsi = osmo_load32be(&mi[1]);
Pau Espin Pedrol45735022017-06-18 14:05:24 +0200728 return snprintf(string, str_len, "%"PRIu32, tmsi);
Harald Welte9bb553e2010-03-28 18:14:50 +0800729 }
730 break;
731 case GSM_MI_TYPE_IMSI:
732 case GSM_MI_TYPE_IMEI:
733 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr627e0112018-12-05 21:32:40 +0100734 rc = osmo_bcd2str(string, str_len, mi,
735 1, mi_len * 2 - ((mi[0] & GSM_MI_ODD) ? 0 : 1), true);
736 /* osmo_bcd2str() returns snprintf style strlen(), this returns bytes written. */
737 if (rc < 0)
738 return 0;
739 else if (rc < str_len)
740 return rc + 1;
741 else
742 return strlen(string) + 1;
Neels Hofmeyrea2a0ab2018-12-05 23:30:08 +0100743
Harald Welte9bb553e2010-03-28 18:14:50 +0800744 default:
745 break;
746 }
Harald Welte9bb553e2010-03-28 18:14:50 +0800747
Neels Hofmeyrea2a0ab2018-12-05 23:30:08 +0100748 if (str_len < 1)
749 return 0;
750 *string = '\0';
751 return 1;
Harald Welte9bb553e2010-03-28 18:14:50 +0800752}
Harald Weltea1c4f762010-05-01 11:59:42 +0200753
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200754/*! Parse TS 04.08 Routing Area Identifier
Harald Welte96e2a002017-06-12 21:44:18 +0200755 * \param[out] Caller-provided memory for decoded RA ID
756 * \param[in] buf Input buffer pointing to RAI IE value */
Harald Weltea1c4f762010-05-01 11:59:42 +0200757void gsm48_parse_ra(struct gprs_ra_id *raid, const uint8_t *buf)
758{
759 raid->mcc = (buf[0] & 0xf) * 100;
760 raid->mcc += (buf[0] >> 4) * 10;
761 raid->mcc += (buf[1] & 0xf) * 1;
762
763 /* I wonder who came up with the stupidity of encoding the MNC
764 * differently depending on how many digits its decimal number has! */
765 if ((buf[1] >> 4) == 0xf) {
766 raid->mnc = (buf[2] & 0xf) * 10;
767 raid->mnc += (buf[2] >> 4) * 1;
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100768 raid->mnc_3_digits = false;
Harald Weltea1c4f762010-05-01 11:59:42 +0200769 } else {
770 raid->mnc = (buf[2] & 0xf) * 100;
771 raid->mnc += (buf[2] >> 4) * 10;
772 raid->mnc += (buf[1] >> 4) * 1;
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100773 raid->mnc_3_digits = true;
Harald Weltea1c4f762010-05-01 11:59:42 +0200774 }
775
Harald Welte95871da2017-05-15 12:11:36 +0200776 raid->lac = osmo_load16be(buf + 3);
Harald Weltea1c4f762010-05-01 11:59:42 +0200777 raid->rac = buf[5];
778}
Harald Welte35a93942010-05-01 14:25:22 +0200779
Maxf1ad60e2018-01-05 14:19:33 +0100780/*! Encode a 3GPP TS 24.008 § 10.5.5.15 Routing area identification
781 * \param[out] out Caller-provided packed struct
782 * \param[in] raid Routing Area ID to be encoded
783 */
784void gsm48_encode_ra(struct gsm48_ra_id *out, const struct gprs_ra_id *raid)
785{
786 out->lac = osmo_htons(raid->lac);
787 out->rac = raid->rac;
788
789 out->digits[0] = ((raid->mcc / 100) % 10) | (((raid->mcc / 10) % 10) << 4);
790 out->digits[1] = raid->mcc % 10;
791
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100792 if (raid->mnc < 100 && !raid->mnc_3_digits) {
Maxf1ad60e2018-01-05 14:19:33 +0100793 out->digits[1] |= 0xf0;
794 out->digits[2] = ((raid->mnc / 10) % 10) | ((raid->mnc % 10) << 4);
795 } else {
796 out->digits[1] |= (raid->mnc % 10) << 4;
797 out->digits[2] = ((raid->mnc / 100) % 10) | (((raid->mnc / 10) % 10) << 4);
798 }
799}
800
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200801/*! Encode a TS 04.08 Routing Area Identifier
Harald Welte96e2a002017-06-12 21:44:18 +0200802 * \param[out] buf Caller-provided output buffer of 6 bytes
803 * \param[in] raid Routing Area ID to be encoded
804 * \returns number of bytes used in \a buf */
Harald Welte35a93942010-05-01 14:25:22 +0200805int gsm48_construct_ra(uint8_t *buf, const struct gprs_ra_id *raid)
806{
Maxf1ad60e2018-01-05 14:19:33 +0100807 gsm48_encode_ra((struct gsm48_ra_id *)buf, raid);
Harald Welte35a93942010-05-01 14:25:22 +0200808
809 return 6;
810}
Harald Welte2aee7b12011-06-26 14:20:04 +0200811
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200812/*! Determine number of paging sub-channels
Harald Welte96e2a002017-06-12 21:44:18 +0200813 * \param[in] chan_desc Control Channel Description
814 * \returns number of paging sub-channels
815 *
816 * Uses From Table 10.5.33 of GSM 04.08 to determine the number of
817 * paging sub-channels in the given control channel configuration
818 */
Harald Welte2aee7b12011-06-26 14:20:04 +0200819int gsm48_number_of_paging_subchannels(struct gsm48_control_channel_descr *chan_desc)
820{
Harald Welte94df39e2011-06-26 14:33:57 +0200821 unsigned int n_pag_blocks = gsm0502_get_n_pag_blocks(chan_desc);
822
823 if (chan_desc->ccch_conf == RSL_BCCH_CCCH_CONF_1_C)
824 return OSMO_MAX(1, n_pag_blocks) * (chan_desc->bs_pa_mfrms + 2);
825 else
826 return n_pag_blocks * (chan_desc->bs_pa_mfrms + 2);
Harald Welte2aee7b12011-06-26 14:20:04 +0200827}
Neels Hofmeyrdbd994c2017-03-09 23:07:02 +0100828
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200829/*! TS 04.08 Protocol Descriptor names */
Neels Hofmeyrdbd994c2017-03-09 23:07:02 +0100830const struct value_string gsm48_pdisc_names[] = {
Harald Welteea0bc962018-01-24 16:17:45 +0100831 { GSM48_PDISC_GROUP_CC, "VGCC" },
832 { GSM48_PDISC_BCAST_CC, "VBCC" },
833 { GSM48_PDISC_PDSS1, "PDSS1" },
834 { GSM48_PDISC_CC, "CC" },
835 { GSM48_PDISC_PDSS2, "PDSS2" },
836 { GSM48_PDISC_MM, "MM" },
837 { GSM48_PDISC_RR, "RR" },
838 { GSM48_PDISC_MM_GPRS, "GMM" },
839 { GSM48_PDISC_SMS, "SMS" },
840 { GSM48_PDISC_SM_GPRS, "SM" },
841 { GSM48_PDISC_NC_SS, "NCSS" },
842 { GSM48_PDISC_LOC, "LCS" },
843 { GSM48_PDISC_EXTEND, "EXTD" },
844 { GSM48_PDISC_MASK, "MASK" },
Neels Hofmeyrdbd994c2017-03-09 23:07:02 +0100845 { 0, NULL }
846};
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +0100847
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200848/*! TS 04.08 RR Message Type names */
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +0100849const struct value_string gsm48_rr_msgtype_names[] = {
850 OSMO_VALUE_STRING(GSM48_MT_RR_INIT_REQ),
851 OSMO_VALUE_STRING(GSM48_MT_RR_ADD_ASS),
852 OSMO_VALUE_STRING(GSM48_MT_RR_IMM_ASS),
853 OSMO_VALUE_STRING(GSM48_MT_RR_IMM_ASS_EXT),
854 OSMO_VALUE_STRING(GSM48_MT_RR_IMM_ASS_REJ),
855 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_ASS_FAIL),
856 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_REJECT),
857 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_REQUEST),
858 OSMO_VALUE_STRING(GSM48_MT_RR_PACKET_ASS),
859
860 OSMO_VALUE_STRING(GSM48_MT_RR_CIPH_M_CMD),
861 OSMO_VALUE_STRING(GSM48_MT_RR_CIPH_M_COMPL),
862
863 OSMO_VALUE_STRING(GSM48_MT_RR_CFG_CHG_CMD),
864 OSMO_VALUE_STRING(GSM48_MT_RR_CFG_CHG_ACK),
865 OSMO_VALUE_STRING(GSM48_MT_RR_CFG_CHG_REJ),
866
867 OSMO_VALUE_STRING(GSM48_MT_RR_ASS_CMD),
868 OSMO_VALUE_STRING(GSM48_MT_RR_ASS_COMPL),
869 OSMO_VALUE_STRING(GSM48_MT_RR_ASS_FAIL),
870 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_CMD),
871 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_COMPL),
872 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_FAIL),
873 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_INFO),
874 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_INFO),
875 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_ASS_CMD),
876
877 OSMO_VALUE_STRING(GSM48_MT_RR_CELL_CHG_ORDER),
878 OSMO_VALUE_STRING(GSM48_MT_RR_PDCH_ASS_CMD),
879
880 OSMO_VALUE_STRING(GSM48_MT_RR_CHAN_REL),
881 OSMO_VALUE_STRING(GSM48_MT_RR_PART_REL),
882 OSMO_VALUE_STRING(GSM48_MT_RR_PART_REL_COMP),
883
884 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_REQ_1),
885 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_REQ_2),
886 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_REQ_3),
887 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_RESP),
888 OSMO_VALUE_STRING(GSM48_MT_RR_NOTIF_NCH),
889 OSMO_VALUE_STRING(GSM48_MT_RR_NOTIF_FACCH),
890 OSMO_VALUE_STRING(GSM48_MT_RR_NOTIF_RESP),
891 OSMO_VALUE_STRING(GSM48_MT_RR_PACKET_NOTIF),
892 OSMO_VALUE_STRING(GSM48_MT_RR_UTRAN_CLSM_CHG),
893 OSMO_VALUE_STRING(GSM48_MT_RR_CDMA2K_CLSM_CHG),
894 OSMO_VALUE_STRING(GSM48_MT_RR_IS_TO_UTRAN_HANDO),
895 OSMO_VALUE_STRING(GSM48_MT_RR_IS_TO_CDMA2K_HANDO),
896
897 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_8),
898 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_1),
899 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2),
900 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_3),
901 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_4),
902 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_5),
903 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_6),
904 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_7),
905
906 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2bis),
907 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2ter),
908 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2quater),
909 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_5bis),
910 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_5ter),
911 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_9),
912 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_13),
913
914 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_16),
915 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_17),
916
917 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_18),
918 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_19),
919 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_20),
920
921 OSMO_VALUE_STRING(GSM48_MT_RR_CHAN_MODE_MODIF),
922 OSMO_VALUE_STRING(GSM48_MT_RR_STATUS),
923 OSMO_VALUE_STRING(GSM48_MT_RR_CHAN_MODE_MODIF_ACK),
924 OSMO_VALUE_STRING(GSM48_MT_RR_FREQ_REDEF),
925 OSMO_VALUE_STRING(GSM48_MT_RR_MEAS_REP),
926 OSMO_VALUE_STRING(GSM48_MT_RR_CLSM_CHG),
927 OSMO_VALUE_STRING(GSM48_MT_RR_CLSM_ENQ),
928 OSMO_VALUE_STRING(GSM48_MT_RR_EXT_MEAS_REP),
929 OSMO_VALUE_STRING(GSM48_MT_RR_EXT_MEAS_REP_ORD),
930 OSMO_VALUE_STRING(GSM48_MT_RR_GPRS_SUSP_REQ),
931 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_INFO),
932
933 OSMO_VALUE_STRING(GSM48_MT_RR_VGCS_UPL_GRANT),
934 OSMO_VALUE_STRING(GSM48_MT_RR_UPLINK_RELEASE),
935 OSMO_VALUE_STRING(GSM48_MT_RR_UPLINK_FREE),
936 OSMO_VALUE_STRING(GSM48_MT_RR_UPLINK_BUSY),
937 OSMO_VALUE_STRING(GSM48_MT_RR_TALKER_IND),
938 { 0, NULL }
939};
940
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200941/*! TS 04.08 MM Message Type names */
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +0100942const struct value_string gsm48_mm_msgtype_names[] = {
943 OSMO_VALUE_STRING(GSM48_MT_MM_IMSI_DETACH_IND),
944 OSMO_VALUE_STRING(GSM48_MT_MM_LOC_UPD_ACCEPT),
945 OSMO_VALUE_STRING(GSM48_MT_MM_LOC_UPD_REJECT),
946 OSMO_VALUE_STRING(GSM48_MT_MM_LOC_UPD_REQUEST),
947
948 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_REJ),
949 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_REQ),
950 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_RESP),
951 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_FAIL),
952 OSMO_VALUE_STRING(GSM48_MT_MM_ID_REQ),
953 OSMO_VALUE_STRING(GSM48_MT_MM_ID_RESP),
954 OSMO_VALUE_STRING(GSM48_MT_MM_TMSI_REALL_CMD),
955 OSMO_VALUE_STRING(GSM48_MT_MM_TMSI_REALL_COMPL),
956
957 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_ACC),
958 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_REJ),
959 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_ABORT),
960 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_REQ),
961 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_PROMPT),
962 OSMO_VALUE_STRING(GSM48_MT_MM_CM_REEST_REQ),
963 OSMO_VALUE_STRING(GSM48_MT_MM_ABORT),
964
965 OSMO_VALUE_STRING(GSM48_MT_MM_NULL),
966 OSMO_VALUE_STRING(GSM48_MT_MM_STATUS),
967 OSMO_VALUE_STRING(GSM48_MT_MM_INFO),
968 { 0, NULL }
969};
970
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200971/*! TS 04.08 CC Message Type names */
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +0100972const struct value_string gsm48_cc_msgtype_names[] = {
973 OSMO_VALUE_STRING(GSM48_MT_CC_ALERTING),
974 OSMO_VALUE_STRING(GSM48_MT_CC_CALL_CONF),
975 OSMO_VALUE_STRING(GSM48_MT_CC_CALL_PROC),
976 OSMO_VALUE_STRING(GSM48_MT_CC_CONNECT),
977 OSMO_VALUE_STRING(GSM48_MT_CC_CONNECT_ACK),
978 OSMO_VALUE_STRING(GSM48_MT_CC_EMERG_SETUP),
979 OSMO_VALUE_STRING(GSM48_MT_CC_PROGRESS),
980 OSMO_VALUE_STRING(GSM48_MT_CC_ESTAB),
981 OSMO_VALUE_STRING(GSM48_MT_CC_ESTAB_CONF),
982 OSMO_VALUE_STRING(GSM48_MT_CC_RECALL),
983 OSMO_VALUE_STRING(GSM48_MT_CC_START_CC),
984 OSMO_VALUE_STRING(GSM48_MT_CC_SETUP),
985
986 OSMO_VALUE_STRING(GSM48_MT_CC_MODIFY),
987 OSMO_VALUE_STRING(GSM48_MT_CC_MODIFY_COMPL),
988 OSMO_VALUE_STRING(GSM48_MT_CC_MODIFY_REJECT),
989 OSMO_VALUE_STRING(GSM48_MT_CC_USER_INFO),
990 OSMO_VALUE_STRING(GSM48_MT_CC_HOLD),
991 OSMO_VALUE_STRING(GSM48_MT_CC_HOLD_ACK),
992 OSMO_VALUE_STRING(GSM48_MT_CC_HOLD_REJ),
993 OSMO_VALUE_STRING(GSM48_MT_CC_RETR),
994 OSMO_VALUE_STRING(GSM48_MT_CC_RETR_ACK),
995 OSMO_VALUE_STRING(GSM48_MT_CC_RETR_REJ),
996
997 OSMO_VALUE_STRING(GSM48_MT_CC_DISCONNECT),
998 OSMO_VALUE_STRING(GSM48_MT_CC_RELEASE),
999 OSMO_VALUE_STRING(GSM48_MT_CC_RELEASE_COMPL),
1000
1001 OSMO_VALUE_STRING(GSM48_MT_CC_CONG_CTRL),
1002 OSMO_VALUE_STRING(GSM48_MT_CC_NOTIFY),
1003 OSMO_VALUE_STRING(GSM48_MT_CC_STATUS),
1004 OSMO_VALUE_STRING(GSM48_MT_CC_STATUS_ENQ),
1005 OSMO_VALUE_STRING(GSM48_MT_CC_START_DTMF),
1006 OSMO_VALUE_STRING(GSM48_MT_CC_STOP_DTMF),
1007 OSMO_VALUE_STRING(GSM48_MT_CC_STOP_DTMF_ACK),
1008 OSMO_VALUE_STRING(GSM48_MT_CC_START_DTMF_ACK),
1009 OSMO_VALUE_STRING(GSM48_MT_CC_START_DTMF_REJ),
1010 OSMO_VALUE_STRING(GSM48_MT_CC_FACILITY),
1011 { 0, NULL }
1012};
1013
Keith3cdaa8d2018-08-31 20:09:18 +02001014/*! TS 04.08 10.5..4.11 Call Control Cause Values */
1015const struct value_string gsm48_cc_cause_names[] = {
1016 { GSM48_CC_CAUSE_UNASSIGNED_NR, "UNASSIGNED_NR" },
1017 { GSM48_CC_CAUSE_NO_ROUTE, "NO_ROUTE" },
1018 { GSM48_CC_CAUSE_CHAN_UNACCEPT, "CHAN_UNACCEPT" },
1019 { GSM48_CC_CAUSE_OP_DET_BARRING, "OP_DET_BARRING" },
1020 { GSM48_CC_CAUSE_NORM_CALL_CLEAR, "NORM_CALL_CLEAR" },
1021 { GSM48_CC_CAUSE_USER_BUSY, "USER_BUSY" },
1022 { GSM48_CC_CAUSE_USER_NOTRESPOND, "USER_NOTRESPOND" },
1023 { GSM48_CC_CAUSE_USER_ALERTING_NA, "USER_ALERTING_NA" },
1024 { GSM48_CC_CAUSE_CALL_REJECTED, "CALL_REJECTED" },
1025 { GSM48_CC_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED" },
1026 { GSM48_CC_CAUSE_PRE_EMPTION, "PRE_EMPTION" },
1027 { GSM48_CC_CAUSE_NONSE_USER_CLR, "NONSE_USER_CLR" },
1028 { GSM48_CC_CAUSE_DEST_OOO, "DEST_OOO" },
1029 { GSM48_CC_CAUSE_INV_NR_FORMAT, "INV_NR_FORMAT" },
1030 { GSM48_CC_CAUSE_FACILITY_REJ, "FACILITY_REJ" },
1031 { GSM48_CC_CAUSE_RESP_STATUS_INQ, "RESP_STATUS_INQ" },
1032 { GSM48_CC_CAUSE_NORMAL_UNSPEC, "NORMAL_UNSPEC" },
1033 { GSM48_CC_CAUSE_NO_CIRCUIT_CHAN, "NO_CIRCUIT_CHAN" },
1034 { GSM48_CC_CAUSE_NETWORK_OOO, "NETWORK_OOO" },
1035 { GSM48_CC_CAUSE_TEMP_FAILURE, "TEMP_FAILURE" },
1036 { GSM48_CC_CAUSE_SWITCH_CONG, "SWITCH_CONG" },
1037 { GSM48_CC_CAUSE_ACC_INF_DISCARD, "ACC_INF_DISCARD" },
1038 { GSM48_CC_CAUSE_REQ_CHAN_UNAVAIL, "REQ_CHAN_UNAVAIL" },
1039 { GSM48_CC_CAUSE_RESOURCE_UNAVAIL, "RESOURCE_UNAVAIL" },
1040 { GSM48_CC_CAUSE_QOS_UNAVAIL, "QOS_UNAVAIL" },
1041 { GSM48_CC_CAUSE_REQ_FAC_NOT_SUBSC, "REQ_FAC_NOT_SUBSC" },
1042 { GSM48_CC_CAUSE_INC_BARRED_CUG, "INC_BARRED_CUG" },
1043 { GSM48_CC_CAUSE_BEARER_CAP_UNAUTH, "BEARER_CAP_UNAUTH" },
1044 { GSM48_CC_CAUSE_BEARER_CA_UNAVAIL, "BEARER_CA_UNAVAIL" },
1045 { GSM48_CC_CAUSE_SERV_OPT_UNAVAIL, "SERV_OPT_UNAVAIL" },
1046 { GSM48_CC_CAUSE_BEARERSERV_UNIMPL, "BEARERSERV_UNIMPL" },
1047 { GSM48_CC_CAUSE_ACM_GE_ACM_MAX, "ACM_GE_ACM_MAX" },
1048 { GSM48_CC_CAUSE_REQ_FAC_NOTIMPL, "REQ_FAC_NOTIMPL" },
1049 { GSM48_CC_CAUSE_RESTR_BCAP_AVAIL, "RESTR_BCAP_AVAIL" },
1050 { GSM48_CC_CAUSE_SERV_OPT_UNIMPL, "SERV_OPT_UNIMPL" },
1051 { GSM48_CC_CAUSE_INVAL_TRANS_ID, "INVAL_TRANS_ID" },
1052 { GSM48_CC_CAUSE_USER_NOT_IN_CUG, "USER_NOT_IN_CUG" },
1053 { GSM48_CC_CAUSE_INCOMPAT_DEST, "INCOMPAT_DEST" },
1054 { GSM48_CC_CAUSE_INVAL_TRANS_NET, "INVAL_TRANS_NET" },
1055 { GSM48_CC_CAUSE_SEMANTIC_INCORR, "SEMANTIC_INCORR" },
1056 { GSM48_CC_CAUSE_INVAL_MAND_INF, "INVAL_MAND_INF" },
1057 { GSM48_CC_CAUSE_MSGTYPE_NOTEXIST, "MSGTYPE_NOTEXIST" },
1058 { GSM48_CC_CAUSE_MSGTYPE_INCOMPAT, "MSGTYPE_INCOMPAT" },
1059 { GSM48_CC_CAUSE_IE_NOTEXIST, "IE_NOTEXIST" },
1060 { GSM48_CC_CAUSE_COND_IE_ERR, "COND_IE_ERR" },
1061 { GSM48_CC_CAUSE_MSG_INCOMP_STATE, "MSG_INCOMP_STATE" },
1062 { GSM48_CC_CAUSE_RECOVERY_TIMER, "RECOVERY_TIMER" },
1063 { GSM48_CC_CAUSE_PROTO_ERR, "PROTO_ERR" },
1064 { GSM48_CC_CAUSE_INTERWORKING, "INTERWORKING" },
1065 { 0 , NULL }
1066};
1067
Vadim Yanitskiy8d41d722018-04-17 11:17:28 +07001068/*! TS 04.80, section 3.4 Messages for supplementary services control */
1069const struct value_string gsm48_nc_ss_msgtype_names[] = {
1070 OSMO_VALUE_STRING(GSM0480_MTYPE_RELEASE_COMPLETE),
1071 OSMO_VALUE_STRING(GSM0480_MTYPE_FACILITY),
1072 OSMO_VALUE_STRING(GSM0480_MTYPE_REGISTER),
1073 { 0, NULL }
1074};
1075
Harald Welte4a62eda2019-03-18 18:27:00 +01001076/*! Compose a string naming the message type for given protocol, in a caller-provided buffer.
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001077 * If the message type string is known, return the message type name, otherwise
1078 * return "<protocol discriminator name>:<message type in hex>".
Harald Welte4a62eda2019-03-18 18:27:00 +01001079 * \param[out] buf caller-allcated output string buffer
1080 * \param[in] buf_len size of buf in bytes
Harald Welte96e2a002017-06-12 21:44:18 +02001081 * \param[in] pdisc protocol discriminator like GSM48_PDISC_MM
1082 * \param[in] msg_type message type like GSM48_MT_MM_LOC_UPD_REQUEST
Harald Welte4a62eda2019-03-18 18:27:00 +01001083 * \returns buf
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001084 */
Harald Welte4a62eda2019-03-18 18:27:00 +01001085char *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 +01001086{
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001087 const struct value_string *msgt_names;
1088
1089 switch (pdisc) {
1090 case GSM48_PDISC_RR:
1091 msgt_names = gsm48_rr_msgtype_names;
1092 break;
1093 case GSM48_PDISC_MM:
1094 msgt_names = gsm48_mm_msgtype_names;
1095 break;
1096 case GSM48_PDISC_CC:
1097 msgt_names = gsm48_cc_msgtype_names;
1098 break;
Vadim Yanitskiy8d41d722018-04-17 11:17:28 +07001099 case GSM48_PDISC_NC_SS:
1100 msgt_names = gsm48_nc_ss_msgtype_names;
1101 break;
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001102 default:
1103 msgt_names = NULL;
1104 break;
1105 }
1106
1107 if (msgt_names)
Harald Welte4a62eda2019-03-18 18:27:00 +01001108 snprintf(buf, buf_len, "%s", get_value_string(msgt_names, msg_type));
1109 else
1110 snprintf(buf, buf_len, "%s:0x%02x", gsm48_pdisc_name(pdisc), msg_type);
1111 return buf;
1112}
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001113
Harald Welte4a62eda2019-03-18 18:27:00 +01001114/*! Compose a string naming the message type for given protocol, in a static buffer.
1115 * If the message type string is known, return the message type name, otherwise
1116 * return "<protocol discriminator name>:<message type in hex>".
1117 * \param[in] pdisc protocol discriminator like GSM48_PDISC_MM
1118 * \param[in] msg_type message type like GSM48_MT_MM_LOC_UPD_REQUEST
1119 * \returns statically allocated string or string constant.
1120 */
1121const char *gsm48_pdisc_msgtype_name(uint8_t pdisc, uint8_t msg_type)
1122{
1123 static char namebuf[64];
1124 return gsm48_pdisc_msgtype_name_buf(namebuf, sizeof(namebuf), pdisc, msg_type);
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001125}
Harald Welte96e2a002017-06-12 21:44:18 +02001126
Neels Hofmeyraead2412018-04-06 04:31:00 +02001127const struct value_string gsm48_reject_value_names[] = {
1128 { GSM48_REJECT_IMSI_UNKNOWN_IN_HLR, "IMSI_UNKNOWN_IN_HLR" },
1129 { GSM48_REJECT_ILLEGAL_MS, "ILLEGAL_MS" },
1130 { GSM48_REJECT_IMSI_UNKNOWN_IN_VLR, "IMSI_UNKNOWN_IN_VLR" },
1131 { GSM48_REJECT_IMEI_NOT_ACCEPTED, "IMEI_NOT_ACCEPTED" },
1132 { GSM48_REJECT_ILLEGAL_ME, "ILLEGAL_ME" },
1133 { GSM48_REJECT_PLMN_NOT_ALLOWED, "PLMN_NOT_ALLOWED" },
1134 { GSM48_REJECT_LOC_NOT_ALLOWED, "LOC_NOT_ALLOWED" },
1135 { GSM48_REJECT_ROAMING_NOT_ALLOWED, "ROAMING_NOT_ALLOWED" },
1136 { GSM48_REJECT_NETWORK_FAILURE, "NETWORK_FAILURE" },
1137 { GSM48_REJECT_SYNCH_FAILURE, "SYNCH_FAILURE" },
1138 { GSM48_REJECT_CONGESTION, "CONGESTION" },
1139 { GSM48_REJECT_SRV_OPT_NOT_SUPPORTED, "SRV_OPT_NOT_SUPPORTED" },
1140 { GSM48_REJECT_RQD_SRV_OPT_NOT_SUPPORTED, "RQD_SRV_OPT_NOT_SUPPORTED" },
1141 { GSM48_REJECT_SRV_OPT_TMP_OUT_OF_ORDER, "SRV_OPT_TMP_OUT_OF_ORDER" },
1142 { GSM48_REJECT_CALL_CAN_NOT_BE_IDENTIFIED, "CALL_CAN_NOT_BE_IDENTIFIED" },
1143 { GSM48_REJECT_INCORRECT_MESSAGE, "INCORRECT_MESSAGE" },
1144 { GSM48_REJECT_INVALID_MANDANTORY_INF, "INVALID_MANDANTORY_INF" },
1145 { GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED, "MSG_TYPE_NOT_IMPLEMENTED" },
1146 { GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE, "MSG_TYPE_NOT_COMPATIBLE" },
1147 { GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED, "INF_ELEME_NOT_IMPLEMENTED" },
1148 { GSM48_REJECT_CONDTIONAL_IE_ERROR, "CONDTIONAL_IE_ERROR" },
1149 { GSM48_REJECT_MSG_NOT_COMPATIBLE, "MSG_NOT_COMPATIBLE" },
1150 { GSM48_REJECT_PROTOCOL_ERROR, "PROTOCOL_ERROR" },
1151 { GSM48_REJECT_GPRS_NOT_ALLOWED, "GPRS_NOT_ALLOWED" },
1152 { GSM48_REJECT_SERVICES_NOT_ALLOWED, "SERVICES_NOT_ALLOWED" },
1153 { GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE, "MS_IDENTITY_NOT_DERVIVABLE" },
1154 { GSM48_REJECT_IMPLICITLY_DETACHED, "IMPLICITLY_DETACHED" },
1155 { GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN, "GPRS_NOT_ALLOWED_IN_PLMN" },
1156 { GSM48_REJECT_MSC_TMP_NOT_REACHABLE, "MSC_TMP_NOT_REACHABLE" },
1157 { 0, NULL }
1158};
1159
Vadim Yanitskiy30cfeeb2018-08-03 05:44:00 +07001160/*! Wrap a given \ref msg with \ref gsm48_hdr structure
1161 * \param[out] msg A message to be wrapped
1162 * \param[in] pdisc GSM TS 04.07 protocol discriminator 1/2,
1163 * sub-pdisc, trans_id or skip_ind 1/2,
1164 * see section 11.2.3.1 for details
1165 * \param[in] msg_type GSM TS 04.08 message type
1166 * @return pointer to pushed header within \ref msg
1167 */
1168struct gsm48_hdr *gsm48_push_l3hdr(struct msgb *msg,
1169 uint8_t pdisc, uint8_t msg_type)
1170{
1171 struct gsm48_hdr *gh;
1172
1173 gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
1174 gh->proto_discr = pdisc;
1175 gh->msg_type = msg_type;
1176
1177 return gh;
1178}
1179
Neels Hofmeyr92f3f5e2019-01-05 00:39:13 +01001180const struct value_string osmo_lu_type_names[] = {
1181 { GSM48_LUPD_NORMAL, "NORMAL" },
1182 { GSM48_LUPD_PERIODIC, "PERIODIC" },
1183 { GSM48_LUPD_IMSI_ATT, "IMSI-ATTACH" },
1184 { GSM48_LUPD_RESERVED, "RESERVED" },
1185 {}
1186};
1187
Neels Hofmeyrf8963f92019-01-10 23:33:32 +01001188const struct value_string osmo_cm_service_type_names[] = {
1189 { GSM48_CMSERV_MO_CALL_PACKET, "MO-Call" },
1190 { GSM48_CMSERV_EMERGENCY, "Emergency-Call" },
1191 { GSM48_CMSERV_SMS, "Short-Messaging-Service" },
1192 { GSM48_CMSERV_SUP_SERV, "Supplementary-Service" },
1193 { GSM48_CMSERV_VGCS, "Voice-Group-Call" },
1194 { GSM48_CMSERV_VBS, "Voice-Broadcast-Call" },
1195 { GSM48_CMSERV_LOC_SERV, "Location-Service" },
1196 {}
1197};
1198
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001199bool osmo_gsm48_classmark1_is_r99(const struct gsm48_classmark1 *cm1)
1200{
1201 return cm1->rev_lev >= 2;
1202}
1203
1204bool osmo_gsm48_classmark2_is_r99(const struct gsm48_classmark2 *cm2, uint8_t cm2_len)
1205{
1206 if (!cm2_len)
1207 return false;
1208 return cm2->rev_lev >= 2;
1209}
1210
1211/*! Return true if any of Classmark 1 or Classmark 2 are present and indicate R99 capability.
1212 * \param[in] cm Classmarks.
1213 * \returns True if R99 or later, false if pre-R99 or no Classmarks are present.
1214 */
1215bool osmo_gsm48_classmark_is_r99(const struct osmo_gsm48_classmark *cm)
1216{
1217 if (cm->classmark1_set)
1218 return osmo_gsm48_classmark1_is_r99(&cm->classmark1);
1219 return osmo_gsm48_classmark2_is_r99(&cm->classmark2, cm->classmark2_len);
1220}
1221
1222/*! Return a string representation of A5 cipher algorithms indicated by Classmark 1, 2 and 3.
1223 * \param[in] cm Classmarks.
1224 * \returns A statically allocated string like "cm1{a5/1=supported} cm2{0x23= A5/2 A5/3} no-cm3"
1225 */
Harald Welte4a62eda2019-03-18 18:27:00 +01001226char *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 +01001227{
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001228 char cm1[42] = "no-cm1";
1229 char cm2[42] = " no-cm2";
Neels Hofmeyr9e6f5f12019-03-11 05:07:56 +01001230 char cm3[42] = " no-cm3";
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001231
1232 if (cm->classmark1_set)
1233 snprintf(cm1, sizeof(cm1), "cm1{a5/1=%s}",
1234 cm->classmark1.a5_1 ? "not-supported":"supported" /* inverted logic */);
1235
1236 if (cm->classmark2_len >= 3)
1237 snprintf(cm2, sizeof(cm2), " cm2{0x%x=%s%s}",
1238 cm->classmark2.a5_2 + (cm->classmark2.a5_3 << 1),
1239 cm->classmark2.a5_2 ? " A5/2" : "",
1240 cm->classmark2.a5_3 ? " A5/3" : "");
1241
1242 if (cm->classmark3_len >= 1)
1243 snprintf(cm3, sizeof(cm3), " cm3{0x%x=%s%s%s%s}",
1244 cm->classmark3[0],
1245 cm->classmark3[0] & (1 << 0) ? " A5/4" : "",
1246 cm->classmark3[0] & (1 << 1) ? " A5/5" : "",
1247 cm->classmark3[0] & (1 << 2) ? " A5/6" : "",
1248 cm->classmark3[0] & (1 << 3) ? " A5/7" : "");
1249
Harald Welte4a62eda2019-03-18 18:27:00 +01001250 snprintf(buf, buf_len, "%s%s%s", cm1, cm2, cm3);
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001251 return buf;
1252}
1253
Harald Welte4a62eda2019-03-18 18:27:00 +01001254/*! Return a string representation of A5 cipher algorithms indicated by Classmark 1, 2 and 3.
1255 * \param[in] cm Classmarks.
1256 * \returns A statically allocated string like "cm1{a5/1=supported} cm2{0x23= A5/2 A5/3} no-cm3"
1257 */
1258const char *osmo_gsm48_classmark_a5_name(const struct osmo_gsm48_classmark *cm)
1259{
1260 static char buf[128];
1261 return osmo_gsm48_classmark_a5_name_buf(buf, sizeof(buf), cm);
1262}
1263
1264
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001265/*! Overwrite dst with the Classmark information present in src.
1266 * Add an new Classmark and overwrite in dst what src has to offer, but where src has no Classmark information, leave
1267 * dst unchanged. (For Classmark 2 and 3, dst will exactly match any non-zero Classmark length from src, hence may end
1268 * up with a shorter Classmark after this call.)
1269 * \param[out] dst The target Classmark storage to be updated.
1270 * \param[in] src The new Classmark information to read from.
1271 */
1272void osmo_gsm48_classmark_update(struct osmo_gsm48_classmark *dst, const struct osmo_gsm48_classmark *src)
1273{
1274 if (src->classmark1_set) {
1275 dst->classmark1 = src->classmark1;
1276 dst->classmark1_set = true;
1277 }
1278 if (src->classmark2_len) {
1279 dst->classmark2_len = src->classmark2_len;
1280 dst->classmark2 = src->classmark2;
1281 }
1282 if (src->classmark3_len) {
1283 dst->classmark3_len = src->classmark3_len;
1284 memcpy(dst->classmark3, src->classmark3, OSMO_MIN(sizeof(dst->classmark3), src->classmark3_len));
1285 }
1286}
1287
1288
1289/*! Determine if the given Classmark (1/2/3) value permits a given A5/n cipher.
1290 * \param[in] cm Classmarks.
1291 * \param[in] a5 The N in A5/N for which to query whether support is indicated.
1292 * \return 1 when the given A5/n is permitted, 0 when not (or a5 > 7), and negative if the respective MS Classmark is
1293 * not known, where the negative number indicates the classmark type: -2 means Classmark 2 is not available. The
1294 * idea is that when e.g. A5/3 is requested and the corresponding Classmark 3 is not available, that the caller
1295 * can react by obtaining Classmark 3 and calling again once it is available.
1296 */
1297int osmo_gsm48_classmark_supports_a5(const struct osmo_gsm48_classmark *cm, uint8_t a5)
1298{
1299 switch (a5) {
1300 case 0:
1301 /* all phones must implement A5/0, see 3GPP TS 43.020 4.9 */
1302 return 1;
1303 case 1:
1304 /* 3GPP TS 43.020 4.9 requires A5/1 to be suppored by all phones and actually states:
1305 * "The network shall not provide service to an MS which indicates that it does not
1306 * support the ciphering algorithm A5/1.". However, let's be more tolerant based
1307 * on policy here */
1308 /* See 3GPP TS 24.008 10.5.1.7 */
1309 if (!cm->classmark1_set)
1310 return -1;
1311 /* Inverted logic for this bit! */
1312 return cm->classmark1.a5_1 ? 0 : 1;
1313 case 2:
1314 /* See 3GPP TS 24.008 10.5.1.6 */
1315 if (cm->classmark2_len < 3)
1316 return -2;
1317 return cm->classmark2.a5_2 ? 1 : 0;
1318 case 3:
1319 if (cm->classmark2_len < 3)
1320 return -2;
1321 return cm->classmark2.a5_3 ? 1 : 0;
1322 case 4:
1323 case 5:
1324 case 6:
1325 case 7:
1326 /* See 3GPP TS 24.008 10.5.1.7 */
1327 if (!cm->classmark3_len)
1328 return -3;
1329 return (cm->classmark3[0] & (1 << (a5-4))) ? 1 : 0;
1330 default:
1331 return 0;
1332 }
1333}
1334
Harald Welte96e2a002017-06-12 21:44:18 +02001335/*! @} */