blob: c97337b681f5b9f1ca0f9666d9fd60def03201c1 [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>
Harald Welte179f3572019-03-18 18:38:47 +010039#include <osmocom/core/talloc.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010040#include <osmocom/gsm/tlv.h>
41#include <osmocom/gsm/gsm48.h>
Harald Welte94df39e2011-06-26 14:33:57 +020042#include <osmocom/gsm/gsm0502.h>
Maxfb348ee2016-03-30 21:14:53 +020043#include <osmocom/gsm/gsm_utils.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010044#include <osmocom/gsm/protocol/gsm_04_08.h>
Vadim Yanitskiy8d41d722018-04-17 11:17:28 +070045#include <osmocom/gsm/protocol/gsm_04_80.h>
Harald Welte2aee7b12011-06-26 14:20:04 +020046#include <osmocom/gsm/protocol/gsm_08_58.h>
Maxfdca25d2016-07-05 16:06:28 +020047#include <osmocom/gsm/protocol/gsm_04_08_gprs.h>
Harald Welte4fb20752010-03-02 23:17:33 +010048
Harald Welte96e2a002017-06-12 21:44:18 +020049/*! \addtogroup gsm0408
50 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020051 * GSM Mobile Radion Interface L3 messages / TS 04.08
Harald Welte96e2a002017-06-12 21:44:18 +020052 */
53
Neels Hofmeyr87e45502017-06-20 00:17:59 +020054/*! TLV parser definitions for TS 04.08 CC */
Harald Welte4fb20752010-03-02 23:17:33 +010055const struct tlv_definition gsm48_att_tlvdef = {
56 .def = {
57 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
58 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
59 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
60 [GSM48_IE_UTC] = { TLV_TYPE_TV },
61 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
62 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
63
64 [GSM48_IE_BEARER_CAP] = { TLV_TYPE_TLV },
65 [GSM48_IE_CAUSE] = { TLV_TYPE_TLV },
66 [GSM48_IE_CC_CAP] = { TLV_TYPE_TLV },
67 [GSM48_IE_ALERT] = { TLV_TYPE_TLV },
68 [GSM48_IE_FACILITY] = { TLV_TYPE_TLV },
69 [GSM48_IE_PROGR_IND] = { TLV_TYPE_TLV },
70 [GSM48_IE_AUX_STATUS] = { TLV_TYPE_TLV },
71 [GSM48_IE_NOTIFY] = { TLV_TYPE_TV },
72 [GSM48_IE_KPD_FACILITY] = { TLV_TYPE_TV },
73 [GSM48_IE_SIGNAL] = { TLV_TYPE_TV },
74 [GSM48_IE_CONN_BCD] = { TLV_TYPE_TLV },
75 [GSM48_IE_CONN_SUB] = { TLV_TYPE_TLV },
76 [GSM48_IE_CALLING_BCD] = { TLV_TYPE_TLV },
77 [GSM48_IE_CALLING_SUB] = { TLV_TYPE_TLV },
78 [GSM48_IE_CALLED_BCD] = { TLV_TYPE_TLV },
79 [GSM48_IE_CALLED_SUB] = { TLV_TYPE_TLV },
80 [GSM48_IE_REDIR_BCD] = { TLV_TYPE_TLV },
81 [GSM48_IE_REDIR_SUB] = { TLV_TYPE_TLV },
82 [GSM48_IE_LOWL_COMPAT] = { TLV_TYPE_TLV },
83 [GSM48_IE_HIGHL_COMPAT] = { TLV_TYPE_TLV },
84 [GSM48_IE_USER_USER] = { TLV_TYPE_TLV },
85 [GSM48_IE_SS_VERS] = { TLV_TYPE_TLV },
86 [GSM48_IE_MORE_DATA] = { TLV_TYPE_T },
87 [GSM48_IE_CLIR_SUPP] = { TLV_TYPE_T },
88 [GSM48_IE_CLIR_INVOC] = { TLV_TYPE_T },
89 [GSM48_IE_REV_C_SETUP] = { TLV_TYPE_T },
90 [GSM48_IE_REPEAT_CIR] = { TLV_TYPE_T },
91 [GSM48_IE_REPEAT_SEQ] = { TLV_TYPE_T },
92 /* FIXME: more elements */
93 },
94};
95
Neels Hofmeyr87e45502017-06-20 00:17:59 +020096/*! TLV parser definitions for TS 04.08 RR */
Andreas Eversberg014cb872010-07-12 09:11:00 +020097const struct tlv_definition gsm48_rr_att_tlvdef = {
98 .def = {
99 /* NOTE: Don't add IE 17 = MOBILE_ID here, it already used. */
100 [GSM48_IE_VGCS_TARGET] = { TLV_TYPE_TLV },
101 [GSM48_IE_FRQSHORT_AFTER] = { TLV_TYPE_FIXED, 9 },
102 [GSM48_IE_MUL_RATE_CFG] = { TLV_TYPE_TLV },
103 [GSM48_IE_FREQ_L_AFTER] = { TLV_TYPE_TLV },
104 [GSM48_IE_MSLOT_DESC] = { TLV_TYPE_TLV },
105 [GSM48_IE_CHANMODE_2] = { TLV_TYPE_TV },
106 [GSM48_IE_FRQSHORT_BEFORE] = { TLV_TYPE_FIXED, 9 },
107 [GSM48_IE_CHANMODE_3] = { TLV_TYPE_TV },
108 [GSM48_IE_CHANMODE_4] = { TLV_TYPE_TV },
109 [GSM48_IE_CHANMODE_5] = { TLV_TYPE_TV },
110 [GSM48_IE_CHANMODE_6] = { TLV_TYPE_TV },
111 [GSM48_IE_CHANMODE_7] = { TLV_TYPE_TV },
112 [GSM48_IE_CHANMODE_8] = { TLV_TYPE_TV },
113 [GSM48_IE_FREQ_L_BEFORE] = { TLV_TYPE_TLV },
114 [GSM48_IE_CH_DESC_1_BEFORE] = { TLV_TYPE_FIXED, 3 },
115 [GSM48_IE_CH_DESC_2_BEFORE] = { TLV_TYPE_FIXED, 3 },
116 [GSM48_IE_F_CH_SEQ_BEFORE] = { TLV_TYPE_FIXED, 9 },
117 [GSM48_IE_CLASSMARK3] = { TLV_TYPE_TLV },
118 [GSM48_IE_MA_BEFORE] = { TLV_TYPE_TLV },
119 [GSM48_IE_RR_PACKET_UL] = { TLV_TYPE_TLV },
120 [GSM48_IE_RR_PACKET_DL] = { TLV_TYPE_TLV },
121 [GSM48_IE_CELL_CH_DESC] = { TLV_TYPE_FIXED, 16 },
122 [GSM48_IE_CHANMODE_1] = { TLV_TYPE_TV },
123 [GSM48_IE_CHDES_2_AFTER] = { TLV_TYPE_FIXED, 3 },
124 [GSM48_IE_MODE_SEC_CH] = { TLV_TYPE_TV },
125 [GSM48_IE_F_CH_SEQ_AFTER] = { TLV_TYPE_FIXED, 9 },
126 [GSM48_IE_MA_AFTER] = { TLV_TYPE_TLV },
127 [GSM48_IE_BA_RANGE] = { TLV_TYPE_TLV },
128 [GSM48_IE_GROUP_CHDES] = { TLV_TYPE_TLV },
129 [GSM48_IE_BA_LIST_PREF] = { TLV_TYPE_TLV },
130 [GSM48_IE_MOB_OVSERV_DIF] = { TLV_TYPE_TLV },
131 [GSM48_IE_REALTIME_DIFF] = { TLV_TYPE_TLV },
132 [GSM48_IE_START_TIME] = { TLV_TYPE_FIXED, 2 },
133 [GSM48_IE_TIMING_ADVANCE] = { TLV_TYPE_TV },
134 [GSM48_IE_GROUP_CIP_SEQ] = { TLV_TYPE_SINGLE_TV },
135 [GSM48_IE_CIP_MODE_SET] = { TLV_TYPE_SINGLE_TV },
136 [GSM48_IE_GPRS_RESUMPT] = { TLV_TYPE_SINGLE_TV },
137 [GSM48_IE_SYNC_IND] = { TLV_TYPE_SINGLE_TV },
138 },
139};
140
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200141/*! TLV parser definitions for TS 04.08 MM */
Andreas Eversberg014cb872010-07-12 09:11:00 +0200142const struct tlv_definition gsm48_mm_att_tlvdef = {
143 .def = {
144 [GSM48_IE_MOBILE_ID] = { TLV_TYPE_TLV },
145 [GSM48_IE_NAME_LONG] = { TLV_TYPE_TLV },
146 [GSM48_IE_NAME_SHORT] = { TLV_TYPE_TLV },
147 [GSM48_IE_UTC] = { TLV_TYPE_TV },
148 [GSM48_IE_NET_TIME_TZ] = { TLV_TYPE_FIXED, 7 },
149 [GSM48_IE_LSA_IDENT] = { TLV_TYPE_TLV },
Jacob Erlbeck1c3f0882013-09-16 10:29:57 +0200150 [GSM48_IE_NET_DST] = { TLV_TYPE_TLV },
Andreas Eversberg014cb872010-07-12 09:11:00 +0200151
152 [GSM48_IE_LOCATION_AREA] = { TLV_TYPE_FIXED, 5 },
153 [GSM48_IE_PRIORITY_LEV] = { TLV_TYPE_SINGLE_TV },
154 [GSM48_IE_FOLLOW_ON_PROC] = { TLV_TYPE_T },
155 [GSM48_IE_CTS_PERMISSION] = { TLV_TYPE_T },
156 },
157};
158
Harald Weltee9e190a2010-03-25 11:44:57 +0800159static const struct value_string rr_cause_names[] = {
160 { GSM48_RR_CAUSE_NORMAL, "Normal event" },
161 { GSM48_RR_CAUSE_ABNORMAL_UNSPEC, "Abnormal release, unspecified" },
162 { GSM48_RR_CAUSE_ABNORMAL_UNACCT, "Abnormal release, channel unacceptable" },
163 { GSM48_RR_CAUSE_ABNORMAL_TIMER, "Abnormal release, timer expired" },
164 { GSM48_RR_CAUSE_ABNORMAL_NOACT, "Abnormal release, no activity on radio path" },
165 { GSM48_RR_CAUSE_PREMPTIVE_REL, "Preemptive release" },
166 { 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" },
169 { GSM48_RR_CAUSE_CALL_CLEARED, "Call already cleared" },
170 { GSM48_RR_CAUSE_SEMANT_INCORR, "Semantically incorrect message" },
171 { GSM48_RR_CAUSE_INVALID_MAND_INF, "Invalid mandatory information" },
172 { GSM48_RR_CAUSE_MSG_TYPE_N, "Message type non-existant or not implemented" },
173 { GSM48_RR_CAUSE_MSG_TYPE_N_COMPAT, "Message type not compatible with protocol state" },
174 { GSM48_RR_CAUSE_COND_IE_ERROR, "Conditional IE error" },
175 { GSM48_RR_CAUSE_NO_CELL_ALLOC_A, "No cell allocation available" },
176 { GSM48_RR_CAUSE_PROT_ERROR_UNSPC, "Protocol error unspecified" },
177 { 0, NULL },
Harald Welte4fb20752010-03-02 23:17:33 +0100178};
179
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200180/*! return string representation of RR Cause value */
Philippada00422016-10-27 13:30:16 +0200181const char *rr_cause_name(uint8_t cause)
182{
183 return get_value_string(rr_cause_names, cause);
184}
185
Harald Welte4a62eda2019-03-18 18:27:00 +0100186/*! Return MCC-MNC-LAC-RAC as string, in a caller-provided output buffer.
187 * \param[out] buf caller-provided output buffer
188 * \param[in] buf_len size of buf in bytes
189 * \param[in] rai RAI to encode.
190 * \returns buf
191 */
192char *osmo_rai_name_buf(char *buf, size_t buf_len, const struct gprs_ra_id *rai)
193{
194 snprintf(buf, buf_len, "%s-%s-%u-%u",
195 osmo_mcc_name(rai->mcc), osmo_mnc_name(rai->mnc, rai->mnc_3_digits), rai->lac,
196 rai->rac);
197 return buf;
198}
199
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100200/*! Return MCC-MNC-LAC-RAC as string, in a static buffer.
201 * \param[in] rai RAI to encode.
202 * \returns Static string buffer.
203 */
204const char *osmo_rai_name(const struct gprs_ra_id *rai)
205{
Harald Welte171ef822019-03-28 10:49:05 +0100206 static __thread char buf[32];
Harald Welte4a62eda2019-03-18 18:27:00 +0100207 return osmo_rai_name_buf(buf, sizeof(buf), rai);
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100208}
209
Harald Welte179f3572019-03-18 18:38:47 +0100210/*! Return MCC-MNC-LAC-RAC as string, in dynamically-allocated output buffer.
211 * \param[in] ctx talloc context from which to allocate output buffer
212 * \param[in] rai RAI to encode.
213 * \returns string representation in dynamically-allocated output buffer.
214 */
215char *osmo_rai_name_c(const void *ctx, const struct gprs_ra_id *rai)
216{
217 char *buf = talloc_size(ctx, 32);
218 if (!buf)
219 return NULL;
220 return osmo_rai_name_buf(buf, 32, rai);
221}
222
Harald Welte9eb6d882010-03-25 12:00:54 +0800223/* FIXME: convert to value_string */
Andreas Eversberg014cb872010-07-12 09:11:00 +0200224static const char *cc_state_names[32] = {
Harald Welte4fb20752010-03-02 23:17:33 +0100225 "NULL",
226 "INITIATED",
Andreas Eversberg1ef041f2010-04-09 07:52:12 +0200227 "MM_CONNECTION_PEND",
Harald Welte4fb20752010-03-02 23:17:33 +0100228 "MO_CALL_PROC",
229 "CALL_DELIVERED",
230 "illegal state 5",
231 "CALL_PRESENT",
232 "CALL_RECEIVED",
233 "CONNECT_REQUEST",
234 "MO_TERM_CALL_CONF",
235 "ACTIVE",
236 "DISCONNECT_REQ",
237 "DISCONNECT_IND",
238 "illegal state 13",
239 "illegal state 14",
240 "illegal state 15",
241 "illegal state 16",
242 "illegal state 17",
243 "illegal state 18",
244 "RELEASE_REQ",
245 "illegal state 20",
246 "illegal state 21",
247 "illegal state 22",
248 "illegal state 23",
249 "illegal state 24",
250 "illegal state 25",
251 "MO_ORIG_MODIFY",
252 "MO_TERM_MODIFY",
253 "CONNECT_IND",
254 "illegal state 29",
255 "illegal state 30",
256 "illegal state 31",
257};
258
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200259/*! return string representation of CC State */
Harald Welte9eb6d882010-03-25 12:00:54 +0800260const char *gsm48_cc_state_name(uint8_t state)
261{
262 if (state < ARRAY_SIZE(cc_state_names))
263 return cc_state_names[state];
264
265 return "invalid";
266}
267
268static const struct value_string cc_msg_names[] = {
269 { GSM48_MT_CC_ALERTING, "ALERTING" },
270 { GSM48_MT_CC_CALL_PROC, "CALL_PROC" },
271 { GSM48_MT_CC_PROGRESS, "PROGRESS" },
272 { GSM48_MT_CC_ESTAB, "ESTAB" },
273 { GSM48_MT_CC_SETUP, "SETUP" },
274 { GSM48_MT_CC_ESTAB_CONF, "ESTAB_CONF" },
275 { GSM48_MT_CC_CONNECT, "CONNECT" },
276 { GSM48_MT_CC_CALL_CONF, "CALL_CONF" },
277 { GSM48_MT_CC_START_CC, "START_CC" },
278 { GSM48_MT_CC_RECALL, "RECALL" },
279 { GSM48_MT_CC_EMERG_SETUP, "EMERG_SETUP" },
280 { GSM48_MT_CC_CONNECT_ACK, "CONNECT_ACK" },
281 { GSM48_MT_CC_USER_INFO, "USER_INFO" },
282 { GSM48_MT_CC_MODIFY_REJECT, "MODIFY_REJECT" },
283 { GSM48_MT_CC_MODIFY, "MODIFY" },
284 { GSM48_MT_CC_HOLD, "HOLD" },
285 { GSM48_MT_CC_HOLD_ACK, "HOLD_ACK" },
286 { GSM48_MT_CC_HOLD_REJ, "HOLD_REJ" },
287 { GSM48_MT_CC_RETR, "RETR" },
288 { GSM48_MT_CC_RETR_ACK, "RETR_ACK" },
289 { GSM48_MT_CC_RETR_REJ, "RETR_REJ" },
290 { GSM48_MT_CC_MODIFY_COMPL, "MODIFY_COMPL" },
291 { GSM48_MT_CC_DISCONNECT, "DISCONNECT" },
292 { GSM48_MT_CC_RELEASE_COMPL, "RELEASE_COMPL" },
293 { GSM48_MT_CC_RELEASE, "RELEASE" },
294 { GSM48_MT_CC_STOP_DTMF, "STOP_DTMF" },
295 { GSM48_MT_CC_STOP_DTMF_ACK, "STOP_DTMF_ACK" },
296 { GSM48_MT_CC_STATUS_ENQ, "STATUS_ENQ" },
297 { GSM48_MT_CC_START_DTMF, "START_DTMF" },
298 { GSM48_MT_CC_START_DTMF_ACK, "START_DTMF_ACK" },
299 { GSM48_MT_CC_START_DTMF_REJ, "START_DTMF_REJ" },
300 { GSM48_MT_CC_CONG_CTRL, "CONG_CTRL" },
301 { GSM48_MT_CC_FACILITY, "FACILITY" },
302 { GSM48_MT_CC_STATUS, "STATUS" },
303 { GSM48_MT_CC_NOTIFY, "NOTFIY" },
304 { 0, NULL }
Harald Welte61e2bfc2010-03-04 10:53:03 +0100305};
306
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200307/*! return string representation of CC Message Type */
Harald Welte9eb6d882010-03-25 12:00:54 +0800308const char *gsm48_cc_msg_name(uint8_t msgtype)
309{
310 return get_value_string(cc_msg_names, msgtype);
311}
Harald Welte4fb20752010-03-02 23:17:33 +0100312
Philipp72e43f02016-10-27 13:35:20 +0200313
314static const struct value_string rr_msg_names[] = {
315 /* Channel establishment messages */
316 { GSM48_MT_RR_INIT_REQ, "RR INITIALISATION REQUEST" },
317 { GSM48_MT_RR_ADD_ASS, "ADDITIONAL ASSIGNMENT" },
318 { GSM48_MT_RR_IMM_ASS, "IMMEDIATE ASSIGNMENT" },
319 { GSM48_MT_RR_IMM_ASS_EXT, "MMEDIATE ASSIGNMENT EXTENDED" },
320 { GSM48_MT_RR_IMM_ASS_REJ, "IMMEDIATE ASSIGNMENT REJECT" },
321 { GSM48_MT_RR_DTM_ASS_FAIL, "DTM ASSIGNMENT FAILURE" },
322 { GSM48_MT_RR_DTM_REJECT, "DTM REJECT" },
323 { GSM48_MT_RR_DTM_REQUEST, "DTM REQUEST" },
324 { GSM48_MT_RR_PACKET_ASS, "PACKET ASSIGNMENT" },
325
326 /* Ciphering messages */
327 { GSM48_MT_RR_CIPH_M_CMD, "CIPHERING MODE COMMAND" },
328 { GSM48_MT_RR_CIPH_M_COMPL, "CIPHERING MODE COMPLETE" },
329
330 /* Configuration change messages */
331 { GSM48_MT_RR_CFG_CHG_CMD, "CONFIGURATION CHANGE COMMAND" },
332 { GSM48_MT_RR_CFG_CHG_ACK, "CONFIGURATION CHANGE ACK" },
333 { GSM48_MT_RR_CFG_CHG_REJ, "CONFIGURATION CHANGE REJECT" },
334
335 /* Handover messages */
336 { GSM48_MT_RR_ASS_CMD, "ASSIGNMENT COMMAND" },
337 { GSM48_MT_RR_ASS_COMPL, "ASSIGNMENT COMPLETE" },
338 { GSM48_MT_RR_ASS_FAIL, "ASSIGNMENT FAILURE" },
339 { GSM48_MT_RR_HANDO_CMD, "HANDOVER COMMAND" },
340 { GSM48_MT_RR_HANDO_COMPL, "HANDOVER COMPLETE" },
341 { GSM48_MT_RR_HANDO_FAIL, "HANDOVER FAILURE" },
342 { GSM48_MT_RR_HANDO_INFO, "PHYSICAL INFORMATION" },
343 { GSM48_MT_RR_DTM_ASS_CMD, "DTM ASSIGNMENT COMMAND" },
344
345 { GSM48_MT_RR_CELL_CHG_ORDER, "RR-CELL CHANGE ORDER" },
346 { GSM48_MT_RR_PDCH_ASS_CMD, "PDCH ASSIGNMENT COMMAND" },
347
348 /* Channel release messages */
349 { GSM48_MT_RR_CHAN_REL, "CHANNEL RELEASE" },
350 { GSM48_MT_RR_PART_REL, "PARTIAL RELEASE" },
351 { GSM48_MT_RR_PART_REL_COMP, "PARTIAL RELEASE COMPLETE" },
352
353 /* Paging and Notification messages */
354 { GSM48_MT_RR_PAG_REQ_1, "PAGING REQUEST TYPE 1" },
355 { GSM48_MT_RR_PAG_REQ_2, "PAGING REQUEST TYPE 2" },
356 { GSM48_MT_RR_PAG_REQ_3, "PAGING REQUEST TYPE 3" },
357 { GSM48_MT_RR_PAG_RESP, "PAGING RESPONSE" },
358 { GSM48_MT_RR_NOTIF_NCH, "NOTIFICATION/NCH" },
359 { GSM48_MT_RR_NOTIF_FACCH, "(Reserved)" },
360 { GSM48_MT_RR_NOTIF_RESP, "NOTIFICATION/RESPONSE" },
361 { GSM48_MT_RR_PACKET_NOTIF, "PACKET NOTIFICATION" },
362 /* 3G Specific messages */
363 { GSM48_MT_RR_UTRAN_CLSM_CHG, "UTRAN Classmark Change" },
364 { GSM48_MT_RR_CDMA2K_CLSM_CHG, "cdma 2000 Classmark Change" },
365 { GSM48_MT_RR_IS_TO_UTRAN_HANDO, "Inter System to UTRAN Handover Command" },
366 { GSM48_MT_RR_IS_TO_CDMA2K_HANDO, "Inter System to cdma2000 Handover Command" },
367
368 /* System information messages */
369 { GSM48_MT_RR_SYSINFO_8, "SYSTEM INFORMATION TYPE 8" },
370 { GSM48_MT_RR_SYSINFO_1, "SYSTEM INFORMATION TYPE 1" },
371 { GSM48_MT_RR_SYSINFO_2, "SYSTEM INFORMATION TYPE 2" },
372 { GSM48_MT_RR_SYSINFO_3, "SYSTEM INFORMATION TYPE 3" },
373 { GSM48_MT_RR_SYSINFO_4, "SYSTEM INFORMATION TYPE 4" },
374 { GSM48_MT_RR_SYSINFO_5, "SYSTEM INFORMATION TYPE 5" },
375 { GSM48_MT_RR_SYSINFO_6, "SYSTEM INFORMATION TYPE 6" },
376 { GSM48_MT_RR_SYSINFO_7, "SYSTEM INFORMATION TYPE 7" },
377 { GSM48_MT_RR_SYSINFO_2bis, "SYSTEM INFORMATION TYPE 2bis" },
378 { GSM48_MT_RR_SYSINFO_2ter, "SYSTEM INFORMATION TYPE 2ter" },
379 { GSM48_MT_RR_SYSINFO_2quater, "SYSTEM INFORMATION TYPE 2quater" },
380 { GSM48_MT_RR_SYSINFO_5bis, "SYSTEM INFORMATION TYPE 5bis" },
381 { GSM48_MT_RR_SYSINFO_5ter, "SYSTEM INFORMATION TYPE 5ter" },
382 { GSM48_MT_RR_SYSINFO_9, "SYSTEM INFORMATION TYPE 9" },
383 { GSM48_MT_RR_SYSINFO_13, "SYSTEM INFORMATION TYPE 13" },
384 { GSM48_MT_RR_SYSINFO_16, "SYSTEM INFORMATION TYPE 16" },
385 { GSM48_MT_RR_SYSINFO_17, "SYSTEM INFORMATION TYPE 17" },
386 { GSM48_MT_RR_SYSINFO_18, "SYSTEM INFORMATION TYPE 18" },
387 { GSM48_MT_RR_SYSINFO_19, "SYSTEM INFORMATION TYPE 19" },
388 { GSM48_MT_RR_SYSINFO_20, "SYSTEM INFORMATION TYPE 20" },
389
390 /* Miscellaneous messages */
391 { GSM48_MT_RR_CHAN_MODE_MODIF, "CHANNEL MODE MODIFY" },
392 { GSM48_MT_RR_STATUS, "RR STATUS" },
393 { GSM48_MT_RR_CHAN_MODE_MODIF_ACK, "CHANNEL MODE MODIFY ACKNOWLEDGE" },
394 { GSM48_MT_RR_FREQ_REDEF, "FREQUENCY REDEFINITION" },
395 { GSM48_MT_RR_MEAS_REP, "MEASUREMENT REPORT" },
396 { GSM48_MT_RR_CLSM_CHG, "CLASSMARK CHANGE" },
397 { GSM48_MT_RR_CLSM_ENQ, "CLASSMARK ENQUIRY" },
398 { GSM48_MT_RR_EXT_MEAS_REP, "EXTENDED MEASUREMENT REPORT" },
399 { GSM48_MT_RR_EXT_MEAS_REP_ORD, "EXTENDED MEASUREMENT ORDER" },
400 { GSM48_MT_RR_GPRS_SUSP_REQ, "GPRS SUSPENSION REQUEST" },
401 { GSM48_MT_RR_DTM_INFO, "DTM INFORMATION" },
402
403 /* VGCS uplink control messages */
404 { GSM48_MT_RR_VGCS_UPL_GRANT, "VGCS UPLINK GRANT" },
405 { GSM48_MT_RR_UPLINK_RELEASE, "UPLINK RELEASE" },
406 { GSM48_MT_RR_UPLINK_FREE, "0c" },
407 { GSM48_MT_RR_UPLINK_BUSY, "UPLINK BUSY" },
408 { GSM48_MT_RR_TALKER_IND, "TALKER INDICATION" },
409
410 /* Application messages */
411 { GSM48_MT_RR_APP_INFO, "Application Information" },
412 { 0, NULL }
413};
414
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200415/*! return string representation of RR Message Type */
Philipp72e43f02016-10-27 13:35:20 +0200416const char *gsm48_rr_msg_name(uint8_t msgtype)
417{
418 return get_value_string(rr_msg_names, msgtype);
419}
420
421
Maxfb348ee2016-03-30 21:14:53 +0200422const struct value_string gsm48_chan_mode_names[] = {
423 { GSM48_CMODE_SIGN, "SIGNALLING" },
424 { GSM48_CMODE_SPEECH_V1, "SPEECH_V1" },
425 { GSM48_CMODE_SPEECH_EFR, "SPEECH_EFR" },
426 { GSM48_CMODE_SPEECH_AMR, "SPEECH_AMR" },
427 { GSM48_CMODE_DATA_14k5, "DATA_14k5" },
428 { GSM48_CMODE_DATA_12k0, "DATA_12k0" },
429 { GSM48_CMODE_DATA_6k0, "DATA_6k0" },
430 { GSM48_CMODE_DATA_3k6, "DATA_3k6" },
431 { 0, NULL },
432};
433
434const struct value_string gsm_chan_t_names[] = {
435 { GSM_LCHAN_NONE, "NONE" },
436 { GSM_LCHAN_SDCCH, "SDCCH" },
437 { GSM_LCHAN_TCH_F, "TCH_F" },
438 { GSM_LCHAN_TCH_H, "TCH_H" },
439 { GSM_LCHAN_UNKNOWN, "UNKNOWN" },
440 { GSM_LCHAN_CCCH, "CCCH" },
441 { GSM_LCHAN_PDTCH, "PDTCH" },
442 { GSM_LCHAN_CBCH, "CBCH" },
443 { 0, NULL },
444};
445
Harald Welte1a8c4e02015-08-16 17:56:25 +0200446static const struct value_string mi_type_names[] = {
447 { GSM_MI_TYPE_NONE, "NONE" },
448 { GSM_MI_TYPE_IMSI, "IMSI" },
449 { GSM_MI_TYPE_IMEI, "IMEI" },
450 { GSM_MI_TYPE_IMEISV, "IMEI-SV" },
451 { GSM_MI_TYPE_TMSI, "TMSI" },
452 { 0, NULL }
453};
454
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200455/*! return string representation of Mobile Identity Type */
Harald Welte1a8c4e02015-08-16 17:56:25 +0200456const char *gsm48_mi_type_name(uint8_t mi)
457{
458 return get_value_string(mi_type_names, mi);
459}
460
Harald Welte4a62eda2019-03-18 18:27:00 +0100461/*! Return a human readable representation of a Mobile Identity in caller-provided buffer.
462 * \param[out] buf caller-provided output buffer
463 * \param[in] buf_len size of buf in bytes
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100464 * \param[in] mi Mobile Identity buffer containing 3GPP TS 04.08 style MI type and data.
465 * \param[in] mi_len Length of mi.
Harald Welte4a62eda2019-03-18 18:27:00 +0100466 * \return buf
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100467 */
Harald Welte4a62eda2019-03-18 18:27:00 +0100468char *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 +0100469{
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100470 uint8_t mi_type;
471 uint32_t tmsi;
472 char mi_string[GSM48_MI_SIZE];
473
474 mi_type = (mi && mi_len) ? (mi[0] & GSM_MI_TYPE_MASK) : GSM_MI_TYPE_NONE;
475
476 switch (mi_type) {
477 case GSM_MI_TYPE_TMSI:
478 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
479 if (mi_len == GSM48_TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
480 tmsi = osmo_load32be(&mi[1]);
Harald Welte4a62eda2019-03-18 18:27:00 +0100481 snprintf(buf, buf_len, "TMSI-0x%08" PRIX32, tmsi);
482 return buf;
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100483 }
484 return "TMSI-invalid";
485
486 case GSM_MI_TYPE_IMSI:
487 case GSM_MI_TYPE_IMEI:
488 case GSM_MI_TYPE_IMEISV:
489 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 +0100490 snprintf(buf, buf_len, "%s-%s", gsm48_mi_type_name(mi_type), mi_string);
491 return buf;
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100492
493 default:
494 return "unknown";
495 }
496}
497
Harald Welte4a62eda2019-03-18 18:27:00 +0100498/*! Return a human readable representation of a Mobile Identity in static buffer.
499 * \param[in] mi Mobile Identity buffer containing 3GPP TS 04.08 style MI type and data.
500 * \param[in] mi_len Length of mi.
501 * \return A string like "IMSI-1234567", "TMSI-0x1234ABCD" or "unknown", "TMSI-invalid"...
502 */
503const char *osmo_mi_name(const uint8_t *mi, uint8_t mi_len)
504{
Harald Welte171ef822019-03-28 10:49:05 +0100505 static __thread char mi_name[10 + GSM48_MI_SIZE + 1];
Harald Welte4a62eda2019-03-18 18:27:00 +0100506 return osmo_mi_name_buf(mi_name, sizeof(mi_name), mi, mi_len);
507}
508
Harald Welte179f3572019-03-18 18:38:47 +0100509/*! Return a human readable representation of a Mobile Identity in dynamically-allocated buffer.
510 * \param[in] ctx talloc context from which to allocate output buffer
511 * \param[in] mi Mobile Identity buffer containing 3GPP TS 04.08 style MI type and data.
512 * \param[in] mi_len Length of mi.
513 * \return A string like "IMSI-1234567", "TMSI-0x1234ABCD" or "unknown", "TMSI-invalid" in a
514 * dynamically-allocated output buffer.
515 */
516char *osmo_mi_name_c(const void *ctx, const uint8_t *mi, uint8_t mi_len)
517{
518 size_t buf_len = 10 + GSM48_MI_SIZE + 1;
519 char *mi_name = talloc_size(ctx, buf_len);
520 if (!mi_name)
521 return NULL;
522 return osmo_mi_name_buf(mi_name, buf_len, mi, mi_len);
523}
524
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200525/*! Checks is particular message is cipherable in A/Gb mode according to
Maxfdca25d2016-07-05 16:06:28 +0200526 * 3GPP TS 24.008 § 4.7.1.2
527 * \param[in] hdr Message header
528 * \return true if message can be encrypted, false otherwise
529 */
530bool gsm48_hdr_gmm_cipherable(const struct gsm48_hdr *hdr)
531{
532 switch(hdr->msg_type) {
533 case GSM48_MT_GMM_ATTACH_REQ:
534 case GSM48_MT_GMM_ATTACH_REJ:
535 case GSM48_MT_GMM_AUTH_CIPH_REQ:
536 case GSM48_MT_GMM_AUTH_CIPH_RESP:
537 case GSM48_MT_GMM_AUTH_CIPH_REJ:
538 case GSM48_MT_GMM_AUTH_CIPH_FAIL:
539 case GSM48_MT_GMM_ID_REQ:
540 case GSM48_MT_GMM_ID_RESP:
541 case GSM48_MT_GMM_RA_UPD_REQ:
542 case GSM48_MT_GMM_RA_UPD_REJ:
543 return false;
544 default:
545 return true;
546 }
547}
548
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100549/* Convert MCC + MNC to BCD representation, legacy implementation.
550 * Instead use osmo_plmn_to_bcd(), which is also capable of converting
551 * 3-digit MNC that have leading zeros. For parameters, also see there. */
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100552void gsm48_mcc_mnc_to_bcd(uint8_t *bcd_dst, uint16_t mcc, uint16_t mnc)
Harald Welte61e2bfc2010-03-04 10:53:03 +0100553{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100554 const struct osmo_plmn_id plmn = {
555 .mcc = mcc,
556 .mnc = mnc,
557 .mnc_3_digits = false,
558 };
559 osmo_plmn_to_bcd(bcd_dst, &plmn);
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100560}
Harald Welte61e2bfc2010-03-04 10:53:03 +0100561
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100562/* Convert given 3-byte BCD buffer to integers, legacy implementation.
563 * Instead use osmo_plmn_from_bcd(), which is also capable of converting
564 * 3-digit MNC that have leading zeros. For parameters, also see there. */
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100565void gsm48_mcc_mnc_from_bcd(uint8_t *bcd_src, uint16_t *mcc, uint16_t *mnc)
566{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100567 struct osmo_plmn_id plmn;
568 osmo_plmn_from_bcd(bcd_src, &plmn);
569 *mcc = plmn.mcc;
570 *mnc = plmn.mnc;
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100571}
572
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100573/*! Encode TS 04.08 Location Area Identifier, legacy implementation.
574 * Instead use gsm48_generate_lai2(), which is capable of three-digit MNC with leading zeros.
575 * \param[out] lai48 caller-provided memory for output
Harald Welte96e2a002017-06-12 21:44:18 +0200576 * \param[in] mcc Mobile Country Code
577 * \param[in] mnc Mobile Network Code
578 * \param[in] lac Location Area Code */
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100579void gsm48_generate_lai(struct gsm48_loc_area_id *lai48, uint16_t mcc,
580 uint16_t mnc, uint16_t lac)
581{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100582 const struct osmo_location_area_id lai = {
583 .plmn = {
584 .mcc = mcc,
585 .mnc = mnc,
586 .mnc_3_digits = false,
587 },
588 .lac = lac,
589 };
590 gsm48_generate_lai2(lai48, &lai);
Harald Welte61e2bfc2010-03-04 10:53:03 +0100591}
592
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100593/*! Encode TS 04.08 Location Area Identifier.
594 * \param[out] lai48 caller-provided memory for output.
595 * \param[in] lai input of MCC-MNC-LAC. */
596void gsm48_generate_lai2(struct gsm48_loc_area_id *lai48, const struct osmo_location_area_id *lai)
597{
598 osmo_plmn_to_bcd(&lai48->digits[0], &lai->plmn);
599 lai48->lac = osmo_htons(lai->lac);
600}
601
602/*! Decode TS 04.08 Location Area Identifier, legacy implementation.
603 * Instead use gsm48_decode_lai2(), which is capable of three-digit MNC with leading zeros.
Harald Welte96e2a002017-06-12 21:44:18 +0200604 * \param[in] Location Area Identifier (encoded)
605 * \param[out] mcc Mobile Country Code
606 * \param[out] mnc Mobile Network Code
607 * \param[out] lac Location Area Code
608 * \returns 0
609 *
610 * Attention: this function returns true integers, not hex! */
Harald Welte774a9de2012-07-13 21:35:13 +0200611int gsm48_decode_lai(struct gsm48_loc_area_id *lai, uint16_t *mcc,
612 uint16_t *mnc, uint16_t *lac)
613{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100614 struct osmo_location_area_id decoded;
615 gsm48_decode_lai2(lai, &decoded);
616 *mcc = decoded.plmn.mcc;
617 *mnc = decoded.plmn.mnc;
618 *lac = decoded.lac;
Harald Welte774a9de2012-07-13 21:35:13 +0200619 return 0;
620}
621
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100622/*! Decode TS 04.08 Location Area Identifier.
623 * \param[in] Location Area Identifier (encoded).
624 * \param[out] decoded Target buffer to write decoded values of MCC-MNC-LAC.
625 *
626 * Attention: this function returns true integers, not hex! */
627void gsm48_decode_lai2(const struct gsm48_loc_area_id *lai, struct osmo_location_area_id *decoded)
628{
629 osmo_plmn_from_bcd(&lai->digits[0], &decoded->plmn);
630 decoded->lac = osmo_ntohs(lai->lac);
631}
632
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200633/*! Set DTX mode in Cell Options IE (3GPP TS 44.018)
Maxfe65fa72016-05-10 17:17:05 +0200634 * \param[in] op Cell Options structure in which DTX parameters will be set
635 * \param[in] full Mode for full-rate channels
636 * \param[in] half Mode for half-rate channels
637 * \param[in] is_bcch Indicates if we should use 10.5.2.3.1 instead of
638 * 10.5.2.3a.2
639 *
640 * There is no space for separate DTX settings for Full and Half rate channels
641 * in BCCH - in this case full setting is used for both and half parameter is
642 * ignored.
643 */
644void gsm48_set_dtx(struct gsm48_cell_options *op, enum gsm48_dtx_mode full,
645 enum gsm48_dtx_mode half, bool is_bcch)
646{
647 if (is_bcch) {
648 switch (full) {
649 case GSM48_DTX_MAY_BE_USED:
650 op->dtx = 0;
651 return;
652 case GSM48_DTX_SHALL_BE_USED:
653 op->dtx = 1;
654 return;
655 case GSM48_DTX_SHALL_NOT_BE_USED:
656 op->dtx = 2;
657 return;
658 }
659 } else {
660 switch (full) {
661 case GSM48_DTX_MAY_BE_USED:
662 op->dtx = (half == GSM48_DTX_SHALL_BE_USED) ? 3 : 0;
663 op->d = (half == GSM48_DTX_SHALL_NOT_BE_USED) ? 0 : 1;
664 return;
665 case GSM48_DTX_SHALL_BE_USED:
666 op->dtx = (half == GSM48_DTX_MAY_BE_USED) ? 3 : 1;
667 op->d = (half == GSM48_DTX_SHALL_BE_USED) ? 1 : 0;
668 return;
669 case GSM48_DTX_SHALL_NOT_BE_USED:
670 op->dtx = 2;
671 op->d = (half == GSM48_DTX_SHALL_BE_USED) ? 1 : 0;
672 return;
673 }
674 }
675}
676
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200677/*! Generate TS 04.08 Mobile ID from TMSI
Harald Welte96e2a002017-06-12 21:44:18 +0200678 * \param[out] buf Caller-provided output buffer (7 bytes)
679 * \param[in] tmsi TMSI to be encoded
680 * \returns number of byes encoded (always 7) */
Harald Welte61e2bfc2010-03-04 10:53:03 +0100681int gsm48_generate_mid_from_tmsi(uint8_t *buf, uint32_t tmsi)
682{
Harald Welte95871da2017-05-15 12:11:36 +0200683 uint32_t tmsi_be = osmo_htonl(tmsi);
Harald Welte61e2bfc2010-03-04 10:53:03 +0100684
685 buf[0] = GSM48_IE_MOBILE_ID;
686 buf[1] = GSM48_TMSI_LEN;
687 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Holger Hans Peter Freythercd252e32013-07-03 09:56:53 +0200688 memcpy(&buf[3], &tmsi_be, sizeof(tmsi_be));
Harald Welte61e2bfc2010-03-04 10:53:03 +0100689
690 return 7;
691}
692
Harald Welte1c3bae12019-01-20 10:37:49 +0100693/*! Generate TS 24.008 §10.5.1.4 Mobile ID of BCD type from ASCII string
694 * \param[out] buf Caller-provided output buffer of at least GSM48_MID_MAX_SIZE bytes
Maxebf14922018-02-15 11:42:11 +0100695 * \param[in] id Identity to be encoded
Harald Welte1c3bae12019-01-20 10:37:49 +0100696 * \param[in] mi_type Type of identity (e.g. GSM_MI_TYPE_IMSI, IMEI, IMEISV)
Maxebf14922018-02-15 11:42:11 +0100697 * \returns number of bytes used in \a buf */
698uint8_t gsm48_generate_mid(uint8_t *buf, const char *id, uint8_t mi_type)
699{
Harald Welte1c3bae12019-01-20 10:37:49 +0100700 uint8_t length = strnlen(id, 16), i, off = 0, odd = (length & 1) == 1;
701 /* maximum length == 16 (IMEISV) */
Maxebf14922018-02-15 11:42:11 +0100702
703 buf[0] = GSM48_IE_MOBILE_ID;
Neels Hofmeyr23187fa2018-12-05 23:24:50 +0100704 buf[2] = osmo_char2bcd(id[0]) << 4 | (mi_type & GSM_MI_TYPE_MASK) | (odd << 3);
Maxebf14922018-02-15 11:42:11 +0100705
706 /* if the length is even we will fill half of the last octet */
707 buf[1] = (length + (odd ? 1 : 2)) >> 1;
Harald Welte1c3bae12019-01-20 10:37:49 +0100708 /* buf[1] maximum = 18/2 = 9 */
709 OSMO_ASSERT(buf[1] <= 9);
Maxebf14922018-02-15 11:42:11 +0100710
711 for (i = 1; i < buf[1]; ++i) {
712 uint8_t upper, lower = osmo_char2bcd(id[++off]);
713 if (!odd && off + 1 == length)
714 upper = 0x0f;
715 else
716 upper = osmo_char2bcd(id[++off]) & 0x0f;
717
718 buf[2 + i] = (upper << 4) | lower;
719 }
720
Harald Welte1c3bae12019-01-20 10:37:49 +0100721 /* maximum return value: 2 + 9 = 11 */
Maxebf14922018-02-15 11:42:11 +0100722 return 2 + buf[1];
723}
724
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200725/*! Generate TS 04.08 Mobile ID from IMSI
Harald Welte96e2a002017-06-12 21:44:18 +0200726 * \param[out] buf Caller-provided output buffer
727 * \param[in] imsi IMSI to be encoded
728 * \returns number of bytes used in \a buf */
Harald Welte61e2bfc2010-03-04 10:53:03 +0100729int gsm48_generate_mid_from_imsi(uint8_t *buf, const char *imsi)
730{
Maxebf14922018-02-15 11:42:11 +0100731 return gsm48_generate_mid(buf, imsi, GSM_MI_TYPE_IMSI);
Harald Welte61e2bfc2010-03-04 10:53:03 +0100732}
Harald Welte9bb553e2010-03-28 18:14:50 +0800733
Neels Hofmeyr627e0112018-12-05 21:32:40 +0100734/*! Convert TS 04.08 Mobile Identity (10.5.1.4) to string.
735 * 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 +0200736 * \param[out] string Caller-provided buffer for output
737 * \param[in] str_len Length of \a string in bytes
738 * \param[in] mi Mobile Identity to be stringified
739 * \param[in] mi_len Length of \a mi in bytes
Neels Hofmeyr6aa20ee2018-12-06 00:40:37 +0100740 * \returns WARNING: the return value of this function is not well implemented.
741 * Depending on the MI type and amount of output buffer, this may return
742 * the nr of written bytes, or the written strlen(), or the snprintf()
743 * style strlen()-if-the-buffer-were-large-enough. */
Harald Welte163d0ea2010-04-09 07:57:40 +0200744int gsm48_mi_to_string(char *string, const int str_len, const uint8_t *mi,
745 const int mi_len)
Harald Welte9bb553e2010-03-28 18:14:50 +0800746{
Neels Hofmeyr627e0112018-12-05 21:32:40 +0100747 int rc;
Harald Welte163d0ea2010-04-09 07:57:40 +0200748 uint8_t mi_type;
Harald Welte163d0ea2010-04-09 07:57:40 +0200749 uint32_t tmsi;
Harald Welte9bb553e2010-03-28 18:14:50 +0800750
Neels Hofmeyr6adffb92018-12-05 23:30:31 +0100751 mi_type = (mi && mi_len) ? (mi[0] & GSM_MI_TYPE_MASK) : GSM_MI_TYPE_NONE;
Harald Welte9bb553e2010-03-28 18:14:50 +0800752
753 switch (mi_type) {
Harald Welte9bb553e2010-03-28 18:14:50 +0800754 case GSM_MI_TYPE_TMSI:
Holger Freyther45abec22016-05-20 19:21:27 +0000755 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
Harald Welte9bb553e2010-03-28 18:14:50 +0800756 if (mi_len == GSM48_TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
Harald Welte95871da2017-05-15 12:11:36 +0200757 tmsi = osmo_load32be(&mi[1]);
Pau Espin Pedrol45735022017-06-18 14:05:24 +0200758 return snprintf(string, str_len, "%"PRIu32, tmsi);
Harald Welte9bb553e2010-03-28 18:14:50 +0800759 }
760 break;
761 case GSM_MI_TYPE_IMSI:
762 case GSM_MI_TYPE_IMEI:
763 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr627e0112018-12-05 21:32:40 +0100764 rc = osmo_bcd2str(string, str_len, mi,
765 1, mi_len * 2 - ((mi[0] & GSM_MI_ODD) ? 0 : 1), true);
766 /* osmo_bcd2str() returns snprintf style strlen(), this returns bytes written. */
767 if (rc < 0)
768 return 0;
769 else if (rc < str_len)
770 return rc + 1;
771 else
772 return strlen(string) + 1;
Neels Hofmeyrea2a0ab2018-12-05 23:30:08 +0100773
Harald Welte9bb553e2010-03-28 18:14:50 +0800774 default:
775 break;
776 }
Harald Welte9bb553e2010-03-28 18:14:50 +0800777
Neels Hofmeyrea2a0ab2018-12-05 23:30:08 +0100778 if (str_len < 1)
779 return 0;
780 *string = '\0';
781 return 1;
Harald Welte9bb553e2010-03-28 18:14:50 +0800782}
Harald Weltea1c4f762010-05-01 11:59:42 +0200783
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200784/*! Parse TS 04.08 Routing Area Identifier
Harald Welte96e2a002017-06-12 21:44:18 +0200785 * \param[out] Caller-provided memory for decoded RA ID
786 * \param[in] buf Input buffer pointing to RAI IE value */
Harald Weltea1c4f762010-05-01 11:59:42 +0200787void gsm48_parse_ra(struct gprs_ra_id *raid, const uint8_t *buf)
788{
789 raid->mcc = (buf[0] & 0xf) * 100;
790 raid->mcc += (buf[0] >> 4) * 10;
791 raid->mcc += (buf[1] & 0xf) * 1;
792
793 /* I wonder who came up with the stupidity of encoding the MNC
794 * differently depending on how many digits its decimal number has! */
795 if ((buf[1] >> 4) == 0xf) {
796 raid->mnc = (buf[2] & 0xf) * 10;
797 raid->mnc += (buf[2] >> 4) * 1;
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100798 raid->mnc_3_digits = false;
Harald Weltea1c4f762010-05-01 11:59:42 +0200799 } else {
800 raid->mnc = (buf[2] & 0xf) * 100;
801 raid->mnc += (buf[2] >> 4) * 10;
802 raid->mnc += (buf[1] >> 4) * 1;
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100803 raid->mnc_3_digits = true;
Harald Weltea1c4f762010-05-01 11:59:42 +0200804 }
805
Harald Welte95871da2017-05-15 12:11:36 +0200806 raid->lac = osmo_load16be(buf + 3);
Harald Weltea1c4f762010-05-01 11:59:42 +0200807 raid->rac = buf[5];
808}
Harald Welte35a93942010-05-01 14:25:22 +0200809
Maxf1ad60e2018-01-05 14:19:33 +0100810/*! Encode a 3GPP TS 24.008 § 10.5.5.15 Routing area identification
811 * \param[out] out Caller-provided packed struct
812 * \param[in] raid Routing Area ID to be encoded
813 */
814void gsm48_encode_ra(struct gsm48_ra_id *out, const struct gprs_ra_id *raid)
815{
816 out->lac = osmo_htons(raid->lac);
817 out->rac = raid->rac;
818
819 out->digits[0] = ((raid->mcc / 100) % 10) | (((raid->mcc / 10) % 10) << 4);
820 out->digits[1] = raid->mcc % 10;
821
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100822 if (raid->mnc < 100 && !raid->mnc_3_digits) {
Maxf1ad60e2018-01-05 14:19:33 +0100823 out->digits[1] |= 0xf0;
824 out->digits[2] = ((raid->mnc / 10) % 10) | ((raid->mnc % 10) << 4);
825 } else {
826 out->digits[1] |= (raid->mnc % 10) << 4;
827 out->digits[2] = ((raid->mnc / 100) % 10) | (((raid->mnc / 10) % 10) << 4);
828 }
829}
830
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200831/*! Encode a TS 04.08 Routing Area Identifier
Harald Welte96e2a002017-06-12 21:44:18 +0200832 * \param[out] buf Caller-provided output buffer of 6 bytes
833 * \param[in] raid Routing Area ID to be encoded
834 * \returns number of bytes used in \a buf */
Harald Welte35a93942010-05-01 14:25:22 +0200835int gsm48_construct_ra(uint8_t *buf, const struct gprs_ra_id *raid)
836{
Maxf1ad60e2018-01-05 14:19:33 +0100837 gsm48_encode_ra((struct gsm48_ra_id *)buf, raid);
Harald Welte35a93942010-05-01 14:25:22 +0200838
839 return 6;
840}
Harald Welte2aee7b12011-06-26 14:20:04 +0200841
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200842/*! Determine number of paging sub-channels
Harald Welte96e2a002017-06-12 21:44:18 +0200843 * \param[in] chan_desc Control Channel Description
844 * \returns number of paging sub-channels
845 *
846 * Uses From Table 10.5.33 of GSM 04.08 to determine the number of
847 * paging sub-channels in the given control channel configuration
848 */
Harald Welte2aee7b12011-06-26 14:20:04 +0200849int gsm48_number_of_paging_subchannels(struct gsm48_control_channel_descr *chan_desc)
850{
Harald Welte94df39e2011-06-26 14:33:57 +0200851 unsigned int n_pag_blocks = gsm0502_get_n_pag_blocks(chan_desc);
852
853 if (chan_desc->ccch_conf == RSL_BCCH_CCCH_CONF_1_C)
854 return OSMO_MAX(1, n_pag_blocks) * (chan_desc->bs_pa_mfrms + 2);
855 else
856 return n_pag_blocks * (chan_desc->bs_pa_mfrms + 2);
Harald Welte2aee7b12011-06-26 14:20:04 +0200857}
Neels Hofmeyrdbd994c2017-03-09 23:07:02 +0100858
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200859/*! TS 04.08 Protocol Descriptor names */
Neels Hofmeyrdbd994c2017-03-09 23:07:02 +0100860const struct value_string gsm48_pdisc_names[] = {
Harald Welteea0bc962018-01-24 16:17:45 +0100861 { GSM48_PDISC_GROUP_CC, "VGCC" },
862 { GSM48_PDISC_BCAST_CC, "VBCC" },
863 { GSM48_PDISC_PDSS1, "PDSS1" },
864 { GSM48_PDISC_CC, "CC" },
865 { GSM48_PDISC_PDSS2, "PDSS2" },
866 { GSM48_PDISC_MM, "MM" },
867 { GSM48_PDISC_RR, "RR" },
868 { GSM48_PDISC_MM_GPRS, "GMM" },
869 { GSM48_PDISC_SMS, "SMS" },
870 { GSM48_PDISC_SM_GPRS, "SM" },
871 { GSM48_PDISC_NC_SS, "NCSS" },
872 { GSM48_PDISC_LOC, "LCS" },
873 { GSM48_PDISC_EXTEND, "EXTD" },
874 { GSM48_PDISC_MASK, "MASK" },
Neels Hofmeyrdbd994c2017-03-09 23:07:02 +0100875 { 0, NULL }
876};
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +0100877
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200878/*! TS 04.08 RR Message Type names */
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +0100879const struct value_string gsm48_rr_msgtype_names[] = {
880 OSMO_VALUE_STRING(GSM48_MT_RR_INIT_REQ),
881 OSMO_VALUE_STRING(GSM48_MT_RR_ADD_ASS),
882 OSMO_VALUE_STRING(GSM48_MT_RR_IMM_ASS),
883 OSMO_VALUE_STRING(GSM48_MT_RR_IMM_ASS_EXT),
884 OSMO_VALUE_STRING(GSM48_MT_RR_IMM_ASS_REJ),
885 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_ASS_FAIL),
886 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_REJECT),
887 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_REQUEST),
888 OSMO_VALUE_STRING(GSM48_MT_RR_PACKET_ASS),
889
890 OSMO_VALUE_STRING(GSM48_MT_RR_CIPH_M_CMD),
891 OSMO_VALUE_STRING(GSM48_MT_RR_CIPH_M_COMPL),
892
893 OSMO_VALUE_STRING(GSM48_MT_RR_CFG_CHG_CMD),
894 OSMO_VALUE_STRING(GSM48_MT_RR_CFG_CHG_ACK),
895 OSMO_VALUE_STRING(GSM48_MT_RR_CFG_CHG_REJ),
896
897 OSMO_VALUE_STRING(GSM48_MT_RR_ASS_CMD),
898 OSMO_VALUE_STRING(GSM48_MT_RR_ASS_COMPL),
899 OSMO_VALUE_STRING(GSM48_MT_RR_ASS_FAIL),
900 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_CMD),
901 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_COMPL),
902 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_FAIL),
903 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_INFO),
904 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_INFO),
905 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_ASS_CMD),
906
907 OSMO_VALUE_STRING(GSM48_MT_RR_CELL_CHG_ORDER),
908 OSMO_VALUE_STRING(GSM48_MT_RR_PDCH_ASS_CMD),
909
910 OSMO_VALUE_STRING(GSM48_MT_RR_CHAN_REL),
911 OSMO_VALUE_STRING(GSM48_MT_RR_PART_REL),
912 OSMO_VALUE_STRING(GSM48_MT_RR_PART_REL_COMP),
913
914 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_REQ_1),
915 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_REQ_2),
916 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_REQ_3),
917 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_RESP),
918 OSMO_VALUE_STRING(GSM48_MT_RR_NOTIF_NCH),
919 OSMO_VALUE_STRING(GSM48_MT_RR_NOTIF_FACCH),
920 OSMO_VALUE_STRING(GSM48_MT_RR_NOTIF_RESP),
921 OSMO_VALUE_STRING(GSM48_MT_RR_PACKET_NOTIF),
922 OSMO_VALUE_STRING(GSM48_MT_RR_UTRAN_CLSM_CHG),
923 OSMO_VALUE_STRING(GSM48_MT_RR_CDMA2K_CLSM_CHG),
924 OSMO_VALUE_STRING(GSM48_MT_RR_IS_TO_UTRAN_HANDO),
925 OSMO_VALUE_STRING(GSM48_MT_RR_IS_TO_CDMA2K_HANDO),
926
927 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_8),
928 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_1),
929 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2),
930 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_3),
931 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_4),
932 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_5),
933 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_6),
934 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_7),
935
936 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2bis),
937 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2ter),
938 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2quater),
939 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_5bis),
940 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_5ter),
941 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_9),
942 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_13),
943
944 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_16),
945 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_17),
946
947 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_18),
948 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_19),
949 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_20),
950
951 OSMO_VALUE_STRING(GSM48_MT_RR_CHAN_MODE_MODIF),
952 OSMO_VALUE_STRING(GSM48_MT_RR_STATUS),
953 OSMO_VALUE_STRING(GSM48_MT_RR_CHAN_MODE_MODIF_ACK),
954 OSMO_VALUE_STRING(GSM48_MT_RR_FREQ_REDEF),
955 OSMO_VALUE_STRING(GSM48_MT_RR_MEAS_REP),
956 OSMO_VALUE_STRING(GSM48_MT_RR_CLSM_CHG),
957 OSMO_VALUE_STRING(GSM48_MT_RR_CLSM_ENQ),
958 OSMO_VALUE_STRING(GSM48_MT_RR_EXT_MEAS_REP),
959 OSMO_VALUE_STRING(GSM48_MT_RR_EXT_MEAS_REP_ORD),
960 OSMO_VALUE_STRING(GSM48_MT_RR_GPRS_SUSP_REQ),
961 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_INFO),
962
963 OSMO_VALUE_STRING(GSM48_MT_RR_VGCS_UPL_GRANT),
964 OSMO_VALUE_STRING(GSM48_MT_RR_UPLINK_RELEASE),
965 OSMO_VALUE_STRING(GSM48_MT_RR_UPLINK_FREE),
966 OSMO_VALUE_STRING(GSM48_MT_RR_UPLINK_BUSY),
967 OSMO_VALUE_STRING(GSM48_MT_RR_TALKER_IND),
968 { 0, NULL }
969};
970
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200971/*! TS 04.08 MM Message Type names */
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +0100972const struct value_string gsm48_mm_msgtype_names[] = {
973 OSMO_VALUE_STRING(GSM48_MT_MM_IMSI_DETACH_IND),
974 OSMO_VALUE_STRING(GSM48_MT_MM_LOC_UPD_ACCEPT),
975 OSMO_VALUE_STRING(GSM48_MT_MM_LOC_UPD_REJECT),
976 OSMO_VALUE_STRING(GSM48_MT_MM_LOC_UPD_REQUEST),
977
978 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_REJ),
979 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_REQ),
980 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_RESP),
981 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_FAIL),
982 OSMO_VALUE_STRING(GSM48_MT_MM_ID_REQ),
983 OSMO_VALUE_STRING(GSM48_MT_MM_ID_RESP),
984 OSMO_VALUE_STRING(GSM48_MT_MM_TMSI_REALL_CMD),
985 OSMO_VALUE_STRING(GSM48_MT_MM_TMSI_REALL_COMPL),
986
987 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_ACC),
988 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_REJ),
989 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_ABORT),
990 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_REQ),
991 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_PROMPT),
992 OSMO_VALUE_STRING(GSM48_MT_MM_CM_REEST_REQ),
993 OSMO_VALUE_STRING(GSM48_MT_MM_ABORT),
994
995 OSMO_VALUE_STRING(GSM48_MT_MM_NULL),
996 OSMO_VALUE_STRING(GSM48_MT_MM_STATUS),
997 OSMO_VALUE_STRING(GSM48_MT_MM_INFO),
998 { 0, NULL }
999};
1000
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001001/*! TS 04.08 CC Message Type names */
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001002const struct value_string gsm48_cc_msgtype_names[] = {
1003 OSMO_VALUE_STRING(GSM48_MT_CC_ALERTING),
1004 OSMO_VALUE_STRING(GSM48_MT_CC_CALL_CONF),
1005 OSMO_VALUE_STRING(GSM48_MT_CC_CALL_PROC),
1006 OSMO_VALUE_STRING(GSM48_MT_CC_CONNECT),
1007 OSMO_VALUE_STRING(GSM48_MT_CC_CONNECT_ACK),
1008 OSMO_VALUE_STRING(GSM48_MT_CC_EMERG_SETUP),
1009 OSMO_VALUE_STRING(GSM48_MT_CC_PROGRESS),
1010 OSMO_VALUE_STRING(GSM48_MT_CC_ESTAB),
1011 OSMO_VALUE_STRING(GSM48_MT_CC_ESTAB_CONF),
1012 OSMO_VALUE_STRING(GSM48_MT_CC_RECALL),
1013 OSMO_VALUE_STRING(GSM48_MT_CC_START_CC),
1014 OSMO_VALUE_STRING(GSM48_MT_CC_SETUP),
1015
1016 OSMO_VALUE_STRING(GSM48_MT_CC_MODIFY),
1017 OSMO_VALUE_STRING(GSM48_MT_CC_MODIFY_COMPL),
1018 OSMO_VALUE_STRING(GSM48_MT_CC_MODIFY_REJECT),
1019 OSMO_VALUE_STRING(GSM48_MT_CC_USER_INFO),
1020 OSMO_VALUE_STRING(GSM48_MT_CC_HOLD),
1021 OSMO_VALUE_STRING(GSM48_MT_CC_HOLD_ACK),
1022 OSMO_VALUE_STRING(GSM48_MT_CC_HOLD_REJ),
1023 OSMO_VALUE_STRING(GSM48_MT_CC_RETR),
1024 OSMO_VALUE_STRING(GSM48_MT_CC_RETR_ACK),
1025 OSMO_VALUE_STRING(GSM48_MT_CC_RETR_REJ),
1026
1027 OSMO_VALUE_STRING(GSM48_MT_CC_DISCONNECT),
1028 OSMO_VALUE_STRING(GSM48_MT_CC_RELEASE),
1029 OSMO_VALUE_STRING(GSM48_MT_CC_RELEASE_COMPL),
1030
1031 OSMO_VALUE_STRING(GSM48_MT_CC_CONG_CTRL),
1032 OSMO_VALUE_STRING(GSM48_MT_CC_NOTIFY),
1033 OSMO_VALUE_STRING(GSM48_MT_CC_STATUS),
1034 OSMO_VALUE_STRING(GSM48_MT_CC_STATUS_ENQ),
1035 OSMO_VALUE_STRING(GSM48_MT_CC_START_DTMF),
1036 OSMO_VALUE_STRING(GSM48_MT_CC_STOP_DTMF),
1037 OSMO_VALUE_STRING(GSM48_MT_CC_STOP_DTMF_ACK),
1038 OSMO_VALUE_STRING(GSM48_MT_CC_START_DTMF_ACK),
1039 OSMO_VALUE_STRING(GSM48_MT_CC_START_DTMF_REJ),
1040 OSMO_VALUE_STRING(GSM48_MT_CC_FACILITY),
1041 { 0, NULL }
1042};
1043
Keith3cdaa8d2018-08-31 20:09:18 +02001044/*! TS 04.08 10.5..4.11 Call Control Cause Values */
1045const struct value_string gsm48_cc_cause_names[] = {
1046 { GSM48_CC_CAUSE_UNASSIGNED_NR, "UNASSIGNED_NR" },
1047 { GSM48_CC_CAUSE_NO_ROUTE, "NO_ROUTE" },
1048 { GSM48_CC_CAUSE_CHAN_UNACCEPT, "CHAN_UNACCEPT" },
1049 { GSM48_CC_CAUSE_OP_DET_BARRING, "OP_DET_BARRING" },
1050 { GSM48_CC_CAUSE_NORM_CALL_CLEAR, "NORM_CALL_CLEAR" },
1051 { GSM48_CC_CAUSE_USER_BUSY, "USER_BUSY" },
1052 { GSM48_CC_CAUSE_USER_NOTRESPOND, "USER_NOTRESPOND" },
1053 { GSM48_CC_CAUSE_USER_ALERTING_NA, "USER_ALERTING_NA" },
1054 { GSM48_CC_CAUSE_CALL_REJECTED, "CALL_REJECTED" },
1055 { GSM48_CC_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED" },
1056 { GSM48_CC_CAUSE_PRE_EMPTION, "PRE_EMPTION" },
1057 { GSM48_CC_CAUSE_NONSE_USER_CLR, "NONSE_USER_CLR" },
1058 { GSM48_CC_CAUSE_DEST_OOO, "DEST_OOO" },
1059 { GSM48_CC_CAUSE_INV_NR_FORMAT, "INV_NR_FORMAT" },
1060 { GSM48_CC_CAUSE_FACILITY_REJ, "FACILITY_REJ" },
1061 { GSM48_CC_CAUSE_RESP_STATUS_INQ, "RESP_STATUS_INQ" },
1062 { GSM48_CC_CAUSE_NORMAL_UNSPEC, "NORMAL_UNSPEC" },
1063 { GSM48_CC_CAUSE_NO_CIRCUIT_CHAN, "NO_CIRCUIT_CHAN" },
1064 { GSM48_CC_CAUSE_NETWORK_OOO, "NETWORK_OOO" },
1065 { GSM48_CC_CAUSE_TEMP_FAILURE, "TEMP_FAILURE" },
1066 { GSM48_CC_CAUSE_SWITCH_CONG, "SWITCH_CONG" },
1067 { GSM48_CC_CAUSE_ACC_INF_DISCARD, "ACC_INF_DISCARD" },
1068 { GSM48_CC_CAUSE_REQ_CHAN_UNAVAIL, "REQ_CHAN_UNAVAIL" },
1069 { GSM48_CC_CAUSE_RESOURCE_UNAVAIL, "RESOURCE_UNAVAIL" },
1070 { GSM48_CC_CAUSE_QOS_UNAVAIL, "QOS_UNAVAIL" },
1071 { GSM48_CC_CAUSE_REQ_FAC_NOT_SUBSC, "REQ_FAC_NOT_SUBSC" },
1072 { GSM48_CC_CAUSE_INC_BARRED_CUG, "INC_BARRED_CUG" },
1073 { GSM48_CC_CAUSE_BEARER_CAP_UNAUTH, "BEARER_CAP_UNAUTH" },
1074 { GSM48_CC_CAUSE_BEARER_CA_UNAVAIL, "BEARER_CA_UNAVAIL" },
1075 { GSM48_CC_CAUSE_SERV_OPT_UNAVAIL, "SERV_OPT_UNAVAIL" },
1076 { GSM48_CC_CAUSE_BEARERSERV_UNIMPL, "BEARERSERV_UNIMPL" },
1077 { GSM48_CC_CAUSE_ACM_GE_ACM_MAX, "ACM_GE_ACM_MAX" },
1078 { GSM48_CC_CAUSE_REQ_FAC_NOTIMPL, "REQ_FAC_NOTIMPL" },
1079 { GSM48_CC_CAUSE_RESTR_BCAP_AVAIL, "RESTR_BCAP_AVAIL" },
1080 { GSM48_CC_CAUSE_SERV_OPT_UNIMPL, "SERV_OPT_UNIMPL" },
1081 { GSM48_CC_CAUSE_INVAL_TRANS_ID, "INVAL_TRANS_ID" },
1082 { GSM48_CC_CAUSE_USER_NOT_IN_CUG, "USER_NOT_IN_CUG" },
1083 { GSM48_CC_CAUSE_INCOMPAT_DEST, "INCOMPAT_DEST" },
1084 { GSM48_CC_CAUSE_INVAL_TRANS_NET, "INVAL_TRANS_NET" },
1085 { GSM48_CC_CAUSE_SEMANTIC_INCORR, "SEMANTIC_INCORR" },
1086 { GSM48_CC_CAUSE_INVAL_MAND_INF, "INVAL_MAND_INF" },
1087 { GSM48_CC_CAUSE_MSGTYPE_NOTEXIST, "MSGTYPE_NOTEXIST" },
1088 { GSM48_CC_CAUSE_MSGTYPE_INCOMPAT, "MSGTYPE_INCOMPAT" },
1089 { GSM48_CC_CAUSE_IE_NOTEXIST, "IE_NOTEXIST" },
1090 { GSM48_CC_CAUSE_COND_IE_ERR, "COND_IE_ERR" },
1091 { GSM48_CC_CAUSE_MSG_INCOMP_STATE, "MSG_INCOMP_STATE" },
1092 { GSM48_CC_CAUSE_RECOVERY_TIMER, "RECOVERY_TIMER" },
1093 { GSM48_CC_CAUSE_PROTO_ERR, "PROTO_ERR" },
1094 { GSM48_CC_CAUSE_INTERWORKING, "INTERWORKING" },
1095 { 0 , NULL }
1096};
1097
Vadim Yanitskiy8d41d722018-04-17 11:17:28 +07001098/*! TS 04.80, section 3.4 Messages for supplementary services control */
1099const struct value_string gsm48_nc_ss_msgtype_names[] = {
1100 OSMO_VALUE_STRING(GSM0480_MTYPE_RELEASE_COMPLETE),
1101 OSMO_VALUE_STRING(GSM0480_MTYPE_FACILITY),
1102 OSMO_VALUE_STRING(GSM0480_MTYPE_REGISTER),
1103 { 0, NULL }
1104};
1105
Harald Welte4a62eda2019-03-18 18:27:00 +01001106/*! Compose a string naming the message type for given protocol, in a caller-provided buffer.
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001107 * If the message type string is known, return the message type name, otherwise
1108 * return "<protocol discriminator name>:<message type in hex>".
Harald Welte4a62eda2019-03-18 18:27:00 +01001109 * \param[out] buf caller-allcated output string buffer
1110 * \param[in] buf_len size of buf in bytes
Harald Welte96e2a002017-06-12 21:44:18 +02001111 * \param[in] pdisc protocol discriminator like GSM48_PDISC_MM
1112 * \param[in] msg_type message type like GSM48_MT_MM_LOC_UPD_REQUEST
Harald Welte4a62eda2019-03-18 18:27:00 +01001113 * \returns buf
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001114 */
Harald Welte4a62eda2019-03-18 18:27:00 +01001115char *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 +01001116{
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001117 const struct value_string *msgt_names;
1118
1119 switch (pdisc) {
1120 case GSM48_PDISC_RR:
1121 msgt_names = gsm48_rr_msgtype_names;
1122 break;
1123 case GSM48_PDISC_MM:
1124 msgt_names = gsm48_mm_msgtype_names;
1125 break;
1126 case GSM48_PDISC_CC:
1127 msgt_names = gsm48_cc_msgtype_names;
1128 break;
Vadim Yanitskiy8d41d722018-04-17 11:17:28 +07001129 case GSM48_PDISC_NC_SS:
1130 msgt_names = gsm48_nc_ss_msgtype_names;
1131 break;
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001132 default:
1133 msgt_names = NULL;
1134 break;
1135 }
1136
1137 if (msgt_names)
Harald Welte4a62eda2019-03-18 18:27:00 +01001138 snprintf(buf, buf_len, "%s", get_value_string(msgt_names, msg_type));
1139 else
1140 snprintf(buf, buf_len, "%s:0x%02x", gsm48_pdisc_name(pdisc), msg_type);
1141 return buf;
1142}
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001143
Harald Welte4a62eda2019-03-18 18:27:00 +01001144/*! Compose a string naming the message type for given protocol, in a static buffer.
1145 * If the message type string is known, return the message type name, otherwise
1146 * return "<protocol discriminator name>:<message type in hex>".
1147 * \param[in] pdisc protocol discriminator like GSM48_PDISC_MM
1148 * \param[in] msg_type message type like GSM48_MT_MM_LOC_UPD_REQUEST
1149 * \returns statically allocated string or string constant.
1150 */
1151const char *gsm48_pdisc_msgtype_name(uint8_t pdisc, uint8_t msg_type)
1152{
Harald Welte171ef822019-03-28 10:49:05 +01001153 static __thread char namebuf[64];
Harald Welte4a62eda2019-03-18 18:27:00 +01001154 return gsm48_pdisc_msgtype_name_buf(namebuf, sizeof(namebuf), pdisc, msg_type);
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001155}
Harald Welte96e2a002017-06-12 21:44:18 +02001156
Harald Welte179f3572019-03-18 18:38:47 +01001157/*! Compose a string naming the message type for given protocol, in a dynamically-allocated buffer.
1158 * If the message type string is known, return the message type name, otherwise
1159 * return "<protocol discriminator name>:<message type in hex>".
1160 * \param[in] ctx talloc context from which to allocate output buffer
1161 * \param[in] pdisc protocol discriminator like GSM48_PDISC_MM
1162 * \param[in] msg_type message type like GSM48_MT_MM_LOC_UPD_REQUEST
1163 * \returns string representation in dynamically allocated output buffer.
1164 */
1165char *gsm48_pdisc_msgtype_name_c(const void *ctx, uint8_t pdisc, uint8_t msg_type)
1166{
1167 char *namebuf = talloc_size(ctx, 64);
1168 if (!namebuf)
1169 return NULL;
1170 return gsm48_pdisc_msgtype_name_buf(namebuf, 64, pdisc, msg_type);
1171}
1172
Neels Hofmeyraead2412018-04-06 04:31:00 +02001173const struct value_string gsm48_reject_value_names[] = {
1174 { GSM48_REJECT_IMSI_UNKNOWN_IN_HLR, "IMSI_UNKNOWN_IN_HLR" },
1175 { GSM48_REJECT_ILLEGAL_MS, "ILLEGAL_MS" },
1176 { GSM48_REJECT_IMSI_UNKNOWN_IN_VLR, "IMSI_UNKNOWN_IN_VLR" },
1177 { GSM48_REJECT_IMEI_NOT_ACCEPTED, "IMEI_NOT_ACCEPTED" },
1178 { GSM48_REJECT_ILLEGAL_ME, "ILLEGAL_ME" },
1179 { GSM48_REJECT_PLMN_NOT_ALLOWED, "PLMN_NOT_ALLOWED" },
1180 { GSM48_REJECT_LOC_NOT_ALLOWED, "LOC_NOT_ALLOWED" },
1181 { GSM48_REJECT_ROAMING_NOT_ALLOWED, "ROAMING_NOT_ALLOWED" },
1182 { GSM48_REJECT_NETWORK_FAILURE, "NETWORK_FAILURE" },
1183 { GSM48_REJECT_SYNCH_FAILURE, "SYNCH_FAILURE" },
1184 { GSM48_REJECT_CONGESTION, "CONGESTION" },
1185 { GSM48_REJECT_SRV_OPT_NOT_SUPPORTED, "SRV_OPT_NOT_SUPPORTED" },
1186 { GSM48_REJECT_RQD_SRV_OPT_NOT_SUPPORTED, "RQD_SRV_OPT_NOT_SUPPORTED" },
1187 { GSM48_REJECT_SRV_OPT_TMP_OUT_OF_ORDER, "SRV_OPT_TMP_OUT_OF_ORDER" },
1188 { GSM48_REJECT_CALL_CAN_NOT_BE_IDENTIFIED, "CALL_CAN_NOT_BE_IDENTIFIED" },
1189 { GSM48_REJECT_INCORRECT_MESSAGE, "INCORRECT_MESSAGE" },
1190 { GSM48_REJECT_INVALID_MANDANTORY_INF, "INVALID_MANDANTORY_INF" },
1191 { GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED, "MSG_TYPE_NOT_IMPLEMENTED" },
1192 { GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE, "MSG_TYPE_NOT_COMPATIBLE" },
1193 { GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED, "INF_ELEME_NOT_IMPLEMENTED" },
1194 { GSM48_REJECT_CONDTIONAL_IE_ERROR, "CONDTIONAL_IE_ERROR" },
1195 { GSM48_REJECT_MSG_NOT_COMPATIBLE, "MSG_NOT_COMPATIBLE" },
1196 { GSM48_REJECT_PROTOCOL_ERROR, "PROTOCOL_ERROR" },
1197 { GSM48_REJECT_GPRS_NOT_ALLOWED, "GPRS_NOT_ALLOWED" },
1198 { GSM48_REJECT_SERVICES_NOT_ALLOWED, "SERVICES_NOT_ALLOWED" },
1199 { GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE, "MS_IDENTITY_NOT_DERVIVABLE" },
1200 { GSM48_REJECT_IMPLICITLY_DETACHED, "IMPLICITLY_DETACHED" },
1201 { GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN, "GPRS_NOT_ALLOWED_IN_PLMN" },
1202 { GSM48_REJECT_MSC_TMP_NOT_REACHABLE, "MSC_TMP_NOT_REACHABLE" },
1203 { 0, NULL }
1204};
1205
Vadim Yanitskiy30cfeeb2018-08-03 05:44:00 +07001206/*! Wrap a given \ref msg with \ref gsm48_hdr structure
1207 * \param[out] msg A message to be wrapped
1208 * \param[in] pdisc GSM TS 04.07 protocol discriminator 1/2,
1209 * sub-pdisc, trans_id or skip_ind 1/2,
1210 * see section 11.2.3.1 for details
1211 * \param[in] msg_type GSM TS 04.08 message type
1212 * @return pointer to pushed header within \ref msg
1213 */
1214struct gsm48_hdr *gsm48_push_l3hdr(struct msgb *msg,
1215 uint8_t pdisc, uint8_t msg_type)
1216{
1217 struct gsm48_hdr *gh;
1218
1219 gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
1220 gh->proto_discr = pdisc;
1221 gh->msg_type = msg_type;
1222
1223 return gh;
1224}
1225
Neels Hofmeyr92f3f5e2019-01-05 00:39:13 +01001226const struct value_string osmo_lu_type_names[] = {
1227 { GSM48_LUPD_NORMAL, "NORMAL" },
1228 { GSM48_LUPD_PERIODIC, "PERIODIC" },
1229 { GSM48_LUPD_IMSI_ATT, "IMSI-ATTACH" },
1230 { GSM48_LUPD_RESERVED, "RESERVED" },
1231 {}
1232};
1233
Neels Hofmeyrf8963f92019-01-10 23:33:32 +01001234const struct value_string osmo_cm_service_type_names[] = {
1235 { GSM48_CMSERV_MO_CALL_PACKET, "MO-Call" },
1236 { GSM48_CMSERV_EMERGENCY, "Emergency-Call" },
1237 { GSM48_CMSERV_SMS, "Short-Messaging-Service" },
1238 { GSM48_CMSERV_SUP_SERV, "Supplementary-Service" },
1239 { GSM48_CMSERV_VGCS, "Voice-Group-Call" },
1240 { GSM48_CMSERV_VBS, "Voice-Broadcast-Call" },
1241 { GSM48_CMSERV_LOC_SERV, "Location-Service" },
1242 {}
1243};
1244
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001245bool osmo_gsm48_classmark1_is_r99(const struct gsm48_classmark1 *cm1)
1246{
1247 return cm1->rev_lev >= 2;
1248}
1249
1250bool osmo_gsm48_classmark2_is_r99(const struct gsm48_classmark2 *cm2, uint8_t cm2_len)
1251{
1252 if (!cm2_len)
1253 return false;
1254 return cm2->rev_lev >= 2;
1255}
1256
1257/*! Return true if any of Classmark 1 or Classmark 2 are present and indicate R99 capability.
1258 * \param[in] cm Classmarks.
1259 * \returns True if R99 or later, false if pre-R99 or no Classmarks are present.
1260 */
1261bool osmo_gsm48_classmark_is_r99(const struct osmo_gsm48_classmark *cm)
1262{
1263 if (cm->classmark1_set)
1264 return osmo_gsm48_classmark1_is_r99(&cm->classmark1);
1265 return osmo_gsm48_classmark2_is_r99(&cm->classmark2, cm->classmark2_len);
1266}
1267
1268/*! Return a string representation of A5 cipher algorithms indicated by Classmark 1, 2 and 3.
1269 * \param[in] cm Classmarks.
1270 * \returns A statically allocated string like "cm1{a5/1=supported} cm2{0x23= A5/2 A5/3} no-cm3"
1271 */
Harald Welte4a62eda2019-03-18 18:27:00 +01001272char *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 +01001273{
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001274 char cm1[42] = "no-cm1";
1275 char cm2[42] = " no-cm2";
Neels Hofmeyr9e6f5f12019-03-11 05:07:56 +01001276 char cm3[42] = " no-cm3";
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001277
1278 if (cm->classmark1_set)
1279 snprintf(cm1, sizeof(cm1), "cm1{a5/1=%s}",
1280 cm->classmark1.a5_1 ? "not-supported":"supported" /* inverted logic */);
1281
1282 if (cm->classmark2_len >= 3)
1283 snprintf(cm2, sizeof(cm2), " cm2{0x%x=%s%s}",
1284 cm->classmark2.a5_2 + (cm->classmark2.a5_3 << 1),
1285 cm->classmark2.a5_2 ? " A5/2" : "",
1286 cm->classmark2.a5_3 ? " A5/3" : "");
1287
1288 if (cm->classmark3_len >= 1)
1289 snprintf(cm3, sizeof(cm3), " cm3{0x%x=%s%s%s%s}",
1290 cm->classmark3[0],
1291 cm->classmark3[0] & (1 << 0) ? " A5/4" : "",
1292 cm->classmark3[0] & (1 << 1) ? " A5/5" : "",
1293 cm->classmark3[0] & (1 << 2) ? " A5/6" : "",
1294 cm->classmark3[0] & (1 << 3) ? " A5/7" : "");
1295
Harald Welte4a62eda2019-03-18 18:27:00 +01001296 snprintf(buf, buf_len, "%s%s%s", cm1, cm2, cm3);
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001297 return buf;
1298}
1299
Harald Welte4a62eda2019-03-18 18:27:00 +01001300/*! Return a string representation of A5 cipher algorithms indicated by Classmark 1, 2 and 3.
1301 * \param[in] cm Classmarks.
1302 * \returns A statically allocated string like "cm1{a5/1=supported} cm2{0x23= A5/2 A5/3} no-cm3"
1303 */
1304const char *osmo_gsm48_classmark_a5_name(const struct osmo_gsm48_classmark *cm)
1305{
Harald Welte171ef822019-03-28 10:49:05 +01001306 static __thread char buf[128];
Harald Welte4a62eda2019-03-18 18:27:00 +01001307 return osmo_gsm48_classmark_a5_name_buf(buf, sizeof(buf), cm);
1308}
1309
Harald Welte179f3572019-03-18 18:38:47 +01001310/*! Return a string representation of A5 cipher algorithms indicated by Classmark 1, 2 and 3.
1311 * \param[in] ctx talloc context from which to allocate output buffer
1312 * \param[in] cm Classmarks.
1313 * \returns string like "cm1{a5/1=supported} cm2{0x23= A5/2 A5/3} no-cm3" in dynamically-allocated
1314 * output buffer.
1315 */
1316char *osmo_gsm48_classmark_a5_name_c(const void *ctx, const struct osmo_gsm48_classmark *cm)
1317{
1318 char *buf = talloc_size(ctx, 128);
1319 if (!buf)
1320 return NULL;
1321 return osmo_gsm48_classmark_a5_name_buf(buf, 128, cm);
1322}
Harald Welte4a62eda2019-03-18 18:27:00 +01001323
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001324/*! Overwrite dst with the Classmark information present in src.
1325 * Add an new Classmark and overwrite in dst what src has to offer, but where src has no Classmark information, leave
1326 * dst unchanged. (For Classmark 2 and 3, dst will exactly match any non-zero Classmark length from src, hence may end
1327 * up with a shorter Classmark after this call.)
1328 * \param[out] dst The target Classmark storage to be updated.
1329 * \param[in] src The new Classmark information to read from.
1330 */
1331void osmo_gsm48_classmark_update(struct osmo_gsm48_classmark *dst, const struct osmo_gsm48_classmark *src)
1332{
1333 if (src->classmark1_set) {
1334 dst->classmark1 = src->classmark1;
1335 dst->classmark1_set = true;
1336 }
1337 if (src->classmark2_len) {
1338 dst->classmark2_len = src->classmark2_len;
1339 dst->classmark2 = src->classmark2;
1340 }
1341 if (src->classmark3_len) {
1342 dst->classmark3_len = src->classmark3_len;
1343 memcpy(dst->classmark3, src->classmark3, OSMO_MIN(sizeof(dst->classmark3), src->classmark3_len));
1344 }
1345}
1346
1347
1348/*! Determine if the given Classmark (1/2/3) value permits a given A5/n cipher.
1349 * \param[in] cm Classmarks.
1350 * \param[in] a5 The N in A5/N for which to query whether support is indicated.
1351 * \return 1 when the given A5/n is permitted, 0 when not (or a5 > 7), and negative if the respective MS Classmark is
1352 * not known, where the negative number indicates the classmark type: -2 means Classmark 2 is not available. The
1353 * idea is that when e.g. A5/3 is requested and the corresponding Classmark 3 is not available, that the caller
1354 * can react by obtaining Classmark 3 and calling again once it is available.
1355 */
1356int osmo_gsm48_classmark_supports_a5(const struct osmo_gsm48_classmark *cm, uint8_t a5)
1357{
1358 switch (a5) {
1359 case 0:
1360 /* all phones must implement A5/0, see 3GPP TS 43.020 4.9 */
1361 return 1;
1362 case 1:
1363 /* 3GPP TS 43.020 4.9 requires A5/1 to be suppored by all phones and actually states:
1364 * "The network shall not provide service to an MS which indicates that it does not
1365 * support the ciphering algorithm A5/1.". However, let's be more tolerant based
1366 * on policy here */
1367 /* See 3GPP TS 24.008 10.5.1.7 */
1368 if (!cm->classmark1_set)
1369 return -1;
1370 /* Inverted logic for this bit! */
1371 return cm->classmark1.a5_1 ? 0 : 1;
1372 case 2:
1373 /* See 3GPP TS 24.008 10.5.1.6 */
1374 if (cm->classmark2_len < 3)
1375 return -2;
1376 return cm->classmark2.a5_2 ? 1 : 0;
1377 case 3:
1378 if (cm->classmark2_len < 3)
1379 return -2;
1380 return cm->classmark2.a5_3 ? 1 : 0;
1381 case 4:
1382 case 5:
1383 case 6:
1384 case 7:
1385 /* See 3GPP TS 24.008 10.5.1.7 */
1386 if (!cm->classmark3_len)
1387 return -3;
1388 return (cm->classmark3[0] & (1 << (a5-4))) ? 1 : 0;
1389 default:
1390 return 0;
1391 }
1392}
1393
Harald Welte96e2a002017-06-12 21:44:18 +02001394/*! @} */