blob: 8d0998bb9b3a935bd4153ad445924aaf67a4fa25 [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);
Neels Hofmeyr6d670322020-05-26 03:12:29 +0200482 } else {
483 snprintf(buf, buf_len, "TMSI-invalid");
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100484 }
Neels Hofmeyr6d670322020-05-26 03:12:29 +0200485 return buf;
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100486
487 case GSM_MI_TYPE_IMSI:
488 case GSM_MI_TYPE_IMEI:
489 case GSM_MI_TYPE_IMEISV:
490 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 +0100491 snprintf(buf, buf_len, "%s-%s", gsm48_mi_type_name(mi_type), mi_string);
492 return buf;
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100493
494 default:
Neels Hofmeyr6d670322020-05-26 03:12:29 +0200495 snprintf(buf, buf_len, "unknown");
496 return buf;
Neels Hofmeyr02fd83d2019-01-05 00:38:54 +0100497 }
498}
499
Harald Welte4a62eda2019-03-18 18:27:00 +0100500/*! Return a human readable representation of a Mobile Identity in static buffer.
501 * \param[in] mi Mobile Identity buffer containing 3GPP TS 04.08 style MI type and data.
502 * \param[in] mi_len Length of mi.
503 * \return A string like "IMSI-1234567", "TMSI-0x1234ABCD" or "unknown", "TMSI-invalid"...
504 */
505const char *osmo_mi_name(const uint8_t *mi, uint8_t mi_len)
506{
Harald Welte171ef822019-03-28 10:49:05 +0100507 static __thread char mi_name[10 + GSM48_MI_SIZE + 1];
Harald Welte4a62eda2019-03-18 18:27:00 +0100508 return osmo_mi_name_buf(mi_name, sizeof(mi_name), mi, mi_len);
509}
510
Harald Welte179f3572019-03-18 18:38:47 +0100511/*! Return a human readable representation of a Mobile Identity in dynamically-allocated buffer.
512 * \param[in] ctx talloc context from which to allocate output buffer
513 * \param[in] mi Mobile Identity buffer containing 3GPP TS 04.08 style MI type and data.
514 * \param[in] mi_len Length of mi.
515 * \return A string like "IMSI-1234567", "TMSI-0x1234ABCD" or "unknown", "TMSI-invalid" in a
516 * dynamically-allocated output buffer.
517 */
518char *osmo_mi_name_c(const void *ctx, const uint8_t *mi, uint8_t mi_len)
519{
520 size_t buf_len = 10 + GSM48_MI_SIZE + 1;
521 char *mi_name = talloc_size(ctx, buf_len);
522 if (!mi_name)
523 return NULL;
524 return osmo_mi_name_buf(mi_name, buf_len, mi, mi_len);
525}
526
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200527/*! Checks is particular message is cipherable in A/Gb mode according to
Maxfdca25d2016-07-05 16:06:28 +0200528 * 3GPP TS 24.008 § 4.7.1.2
529 * \param[in] hdr Message header
530 * \return true if message can be encrypted, false otherwise
531 */
532bool gsm48_hdr_gmm_cipherable(const struct gsm48_hdr *hdr)
533{
534 switch(hdr->msg_type) {
535 case GSM48_MT_GMM_ATTACH_REQ:
536 case GSM48_MT_GMM_ATTACH_REJ:
537 case GSM48_MT_GMM_AUTH_CIPH_REQ:
538 case GSM48_MT_GMM_AUTH_CIPH_RESP:
539 case GSM48_MT_GMM_AUTH_CIPH_REJ:
540 case GSM48_MT_GMM_AUTH_CIPH_FAIL:
541 case GSM48_MT_GMM_ID_REQ:
542 case GSM48_MT_GMM_ID_RESP:
543 case GSM48_MT_GMM_RA_UPD_REQ:
544 case GSM48_MT_GMM_RA_UPD_REJ:
545 return false;
546 default:
547 return true;
548 }
549}
550
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100551/* Convert MCC + MNC to BCD representation, legacy implementation.
552 * Instead use osmo_plmn_to_bcd(), which is also capable of converting
553 * 3-digit MNC that have leading zeros. For parameters, also see there. */
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100554void gsm48_mcc_mnc_to_bcd(uint8_t *bcd_dst, uint16_t mcc, uint16_t mnc)
Harald Welte61e2bfc2010-03-04 10:53:03 +0100555{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100556 const struct osmo_plmn_id plmn = {
557 .mcc = mcc,
558 .mnc = mnc,
559 .mnc_3_digits = false,
560 };
561 osmo_plmn_to_bcd(bcd_dst, &plmn);
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100562}
Harald Welte61e2bfc2010-03-04 10:53:03 +0100563
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100564/* Convert given 3-byte BCD buffer to integers, legacy implementation.
565 * Instead use osmo_plmn_from_bcd(), which is also capable of converting
566 * 3-digit MNC that have leading zeros. For parameters, also see there. */
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100567void gsm48_mcc_mnc_from_bcd(uint8_t *bcd_src, uint16_t *mcc, uint16_t *mnc)
568{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100569 struct osmo_plmn_id plmn;
570 osmo_plmn_from_bcd(bcd_src, &plmn);
571 *mcc = plmn.mcc;
572 *mnc = plmn.mnc;
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100573}
574
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100575/*! Encode TS 04.08 Location Area Identifier, legacy implementation.
576 * Instead use gsm48_generate_lai2(), which is capable of three-digit MNC with leading zeros.
577 * \param[out] lai48 caller-provided memory for output
Harald Welte96e2a002017-06-12 21:44:18 +0200578 * \param[in] mcc Mobile Country Code
579 * \param[in] mnc Mobile Network Code
580 * \param[in] lac Location Area Code */
Neels Hofmeyrbdccc1b2016-03-15 13:28:10 +0100581void gsm48_generate_lai(struct gsm48_loc_area_id *lai48, uint16_t mcc,
582 uint16_t mnc, uint16_t lac)
583{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100584 const struct osmo_location_area_id lai = {
585 .plmn = {
586 .mcc = mcc,
587 .mnc = mnc,
588 .mnc_3_digits = false,
589 },
590 .lac = lac,
591 };
592 gsm48_generate_lai2(lai48, &lai);
Harald Welte61e2bfc2010-03-04 10:53:03 +0100593}
594
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100595/*! Encode TS 04.08 Location Area Identifier.
596 * \param[out] lai48 caller-provided memory for output.
597 * \param[in] lai input of MCC-MNC-LAC. */
598void gsm48_generate_lai2(struct gsm48_loc_area_id *lai48, const struct osmo_location_area_id *lai)
599{
600 osmo_plmn_to_bcd(&lai48->digits[0], &lai->plmn);
601 lai48->lac = osmo_htons(lai->lac);
602}
603
604/*! Decode TS 04.08 Location Area Identifier, legacy implementation.
605 * Instead use gsm48_decode_lai2(), which is capable of three-digit MNC with leading zeros.
Harald Welte96e2a002017-06-12 21:44:18 +0200606 * \param[in] Location Area Identifier (encoded)
607 * \param[out] mcc Mobile Country Code
608 * \param[out] mnc Mobile Network Code
609 * \param[out] lac Location Area Code
610 * \returns 0
611 *
612 * Attention: this function returns true integers, not hex! */
Harald Welte774a9de2012-07-13 21:35:13 +0200613int gsm48_decode_lai(struct gsm48_loc_area_id *lai, uint16_t *mcc,
614 uint16_t *mnc, uint16_t *lac)
615{
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100616 struct osmo_location_area_id decoded;
617 gsm48_decode_lai2(lai, &decoded);
618 *mcc = decoded.plmn.mcc;
619 *mnc = decoded.plmn.mnc;
620 *lac = decoded.lac;
Harald Welte774a9de2012-07-13 21:35:13 +0200621 return 0;
622}
623
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100624/*! Decode TS 04.08 Location Area Identifier.
625 * \param[in] Location Area Identifier (encoded).
626 * \param[out] decoded Target buffer to write decoded values of MCC-MNC-LAC.
627 *
628 * Attention: this function returns true integers, not hex! */
629void gsm48_decode_lai2(const struct gsm48_loc_area_id *lai, struct osmo_location_area_id *decoded)
630{
631 osmo_plmn_from_bcd(&lai->digits[0], &decoded->plmn);
632 decoded->lac = osmo_ntohs(lai->lac);
633}
634
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200635/*! Set DTX mode in Cell Options IE (3GPP TS 44.018)
Maxfe65fa72016-05-10 17:17:05 +0200636 * \param[in] op Cell Options structure in which DTX parameters will be set
637 * \param[in] full Mode for full-rate channels
638 * \param[in] half Mode for half-rate channels
639 * \param[in] is_bcch Indicates if we should use 10.5.2.3.1 instead of
640 * 10.5.2.3a.2
641 *
642 * There is no space for separate DTX settings for Full and Half rate channels
643 * in BCCH - in this case full setting is used for both and half parameter is
644 * ignored.
645 */
646void gsm48_set_dtx(struct gsm48_cell_options *op, enum gsm48_dtx_mode full,
647 enum gsm48_dtx_mode half, bool is_bcch)
648{
649 if (is_bcch) {
650 switch (full) {
651 case GSM48_DTX_MAY_BE_USED:
652 op->dtx = 0;
653 return;
654 case GSM48_DTX_SHALL_BE_USED:
655 op->dtx = 1;
656 return;
657 case GSM48_DTX_SHALL_NOT_BE_USED:
658 op->dtx = 2;
659 return;
660 }
661 } else {
662 switch (full) {
663 case GSM48_DTX_MAY_BE_USED:
664 op->dtx = (half == GSM48_DTX_SHALL_BE_USED) ? 3 : 0;
665 op->d = (half == GSM48_DTX_SHALL_NOT_BE_USED) ? 0 : 1;
666 return;
667 case GSM48_DTX_SHALL_BE_USED:
668 op->dtx = (half == GSM48_DTX_MAY_BE_USED) ? 3 : 1;
669 op->d = (half == GSM48_DTX_SHALL_BE_USED) ? 1 : 0;
670 return;
671 case GSM48_DTX_SHALL_NOT_BE_USED:
672 op->dtx = 2;
673 op->d = (half == GSM48_DTX_SHALL_BE_USED) ? 1 : 0;
674 return;
675 }
676 }
677}
678
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200679/*! Generate TS 04.08 Mobile ID from TMSI
Harald Welte96e2a002017-06-12 21:44:18 +0200680 * \param[out] buf Caller-provided output buffer (7 bytes)
681 * \param[in] tmsi TMSI to be encoded
682 * \returns number of byes encoded (always 7) */
Harald Welte61e2bfc2010-03-04 10:53:03 +0100683int gsm48_generate_mid_from_tmsi(uint8_t *buf, uint32_t tmsi)
684{
Harald Welte95871da2017-05-15 12:11:36 +0200685 uint32_t tmsi_be = osmo_htonl(tmsi);
Harald Welte61e2bfc2010-03-04 10:53:03 +0100686
687 buf[0] = GSM48_IE_MOBILE_ID;
688 buf[1] = GSM48_TMSI_LEN;
689 buf[2] = 0xf0 | GSM_MI_TYPE_TMSI;
Holger Hans Peter Freythercd252e32013-07-03 09:56:53 +0200690 memcpy(&buf[3], &tmsi_be, sizeof(tmsi_be));
Harald Welte61e2bfc2010-03-04 10:53:03 +0100691
692 return 7;
693}
694
Harald Welte1c3bae12019-01-20 10:37:49 +0100695/*! Generate TS 24.008 §10.5.1.4 Mobile ID of BCD type from ASCII string
696 * \param[out] buf Caller-provided output buffer of at least GSM48_MID_MAX_SIZE bytes
Maxebf14922018-02-15 11:42:11 +0100697 * \param[in] id Identity to be encoded
Harald Welte1c3bae12019-01-20 10:37:49 +0100698 * \param[in] mi_type Type of identity (e.g. GSM_MI_TYPE_IMSI, IMEI, IMEISV)
Maxebf14922018-02-15 11:42:11 +0100699 * \returns number of bytes used in \a buf */
700uint8_t gsm48_generate_mid(uint8_t *buf, const char *id, uint8_t mi_type)
701{
Harald Welte1c3bae12019-01-20 10:37:49 +0100702 uint8_t length = strnlen(id, 16), i, off = 0, odd = (length & 1) == 1;
703 /* maximum length == 16 (IMEISV) */
Maxebf14922018-02-15 11:42:11 +0100704
705 buf[0] = GSM48_IE_MOBILE_ID;
Neels Hofmeyr23187fa2018-12-05 23:24:50 +0100706 buf[2] = osmo_char2bcd(id[0]) << 4 | (mi_type & GSM_MI_TYPE_MASK) | (odd << 3);
Maxebf14922018-02-15 11:42:11 +0100707
708 /* if the length is even we will fill half of the last octet */
709 buf[1] = (length + (odd ? 1 : 2)) >> 1;
Harald Welte1c3bae12019-01-20 10:37:49 +0100710 /* buf[1] maximum = 18/2 = 9 */
711 OSMO_ASSERT(buf[1] <= 9);
Maxebf14922018-02-15 11:42:11 +0100712
713 for (i = 1; i < buf[1]; ++i) {
714 uint8_t upper, lower = osmo_char2bcd(id[++off]);
715 if (!odd && off + 1 == length)
716 upper = 0x0f;
717 else
718 upper = osmo_char2bcd(id[++off]) & 0x0f;
719
720 buf[2 + i] = (upper << 4) | lower;
721 }
722
Harald Welte1c3bae12019-01-20 10:37:49 +0100723 /* maximum return value: 2 + 9 = 11 */
Maxebf14922018-02-15 11:42:11 +0100724 return 2 + buf[1];
725}
726
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200727/*! Generate TS 04.08 Mobile ID from IMSI
Harald Welte96e2a002017-06-12 21:44:18 +0200728 * \param[out] buf Caller-provided output buffer
729 * \param[in] imsi IMSI to be encoded
730 * \returns number of bytes used in \a buf */
Harald Welte61e2bfc2010-03-04 10:53:03 +0100731int gsm48_generate_mid_from_imsi(uint8_t *buf, const char *imsi)
732{
Maxebf14922018-02-15 11:42:11 +0100733 return gsm48_generate_mid(buf, imsi, GSM_MI_TYPE_IMSI);
Harald Welte61e2bfc2010-03-04 10:53:03 +0100734}
Harald Welte9bb553e2010-03-28 18:14:50 +0800735
Neels Hofmeyr627e0112018-12-05 21:32:40 +0100736/*! Convert TS 04.08 Mobile Identity (10.5.1.4) to string.
737 * 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 +0200738 * \param[out] string Caller-provided buffer for output
739 * \param[in] str_len Length of \a string in bytes
740 * \param[in] mi Mobile Identity to be stringified
741 * \param[in] mi_len Length of \a mi in bytes
Neels Hofmeyr512bf022020-05-25 23:05:04 +0200742 * \returns Return <= 0 on error, > 0 on success.
743 * WARNING: the return value of this function is not well implemented.
Neels Hofmeyr6aa20ee2018-12-06 00:40:37 +0100744 * Depending on the MI type and amount of output buffer, this may return
745 * the nr of written bytes, or the written strlen(), or the snprintf()
Neels Hofmeyr512bf022020-05-25 23:05:04 +0200746 * style strlen()-if-the-buffer-were-large-enough.
747 */
Vadim Yanitskiy0d8da792019-07-25 19:12:23 +0700748int gsm48_mi_to_string(char *string, int str_len, const uint8_t *mi, int mi_len)
Harald Welte9bb553e2010-03-28 18:14:50 +0800749{
Neels Hofmeyr627e0112018-12-05 21:32:40 +0100750 int rc;
Harald Welte163d0ea2010-04-09 07:57:40 +0200751 uint8_t mi_type;
Harald Welte163d0ea2010-04-09 07:57:40 +0200752 uint32_t tmsi;
Harald Welte9bb553e2010-03-28 18:14:50 +0800753
Neels Hofmeyr6adffb92018-12-05 23:30:31 +0100754 mi_type = (mi && mi_len) ? (mi[0] & GSM_MI_TYPE_MASK) : GSM_MI_TYPE_NONE;
Harald Welte9bb553e2010-03-28 18:14:50 +0800755
756 switch (mi_type) {
Harald Welte9bb553e2010-03-28 18:14:50 +0800757 case GSM_MI_TYPE_TMSI:
Holger Freyther45abec22016-05-20 19:21:27 +0000758 /* Table 10.5.4.3, reverse generate_mid_from_tmsi */
Harald Welte9bb553e2010-03-28 18:14:50 +0800759 if (mi_len == GSM48_TMSI_LEN && mi[0] == (0xf0 | GSM_MI_TYPE_TMSI)) {
Harald Welte95871da2017-05-15 12:11:36 +0200760 tmsi = osmo_load32be(&mi[1]);
Pau Espin Pedrol45735022017-06-18 14:05:24 +0200761 return snprintf(string, str_len, "%"PRIu32, tmsi);
Harald Welte9bb553e2010-03-28 18:14:50 +0800762 }
763 break;
764 case GSM_MI_TYPE_IMSI:
765 case GSM_MI_TYPE_IMEI:
766 case GSM_MI_TYPE_IMEISV:
Neels Hofmeyr627e0112018-12-05 21:32:40 +0100767 rc = osmo_bcd2str(string, str_len, mi,
768 1, mi_len * 2 - ((mi[0] & GSM_MI_ODD) ? 0 : 1), true);
769 /* osmo_bcd2str() returns snprintf style strlen(), this returns bytes written. */
770 if (rc < 0)
771 return 0;
772 else if (rc < str_len)
773 return rc + 1;
774 else
775 return strlen(string) + 1;
Neels Hofmeyrea2a0ab2018-12-05 23:30:08 +0100776
Harald Welte9bb553e2010-03-28 18:14:50 +0800777 default:
778 break;
779 }
Harald Welte9bb553e2010-03-28 18:14:50 +0800780
Neels Hofmeyrea2a0ab2018-12-05 23:30:08 +0100781 if (str_len < 1)
782 return 0;
783 *string = '\0';
784 return 1;
Harald Welte9bb553e2010-03-28 18:14:50 +0800785}
Harald Weltea1c4f762010-05-01 11:59:42 +0200786
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200787/*! Parse TS 04.08 Routing Area Identifier
Harald Welte96e2a002017-06-12 21:44:18 +0200788 * \param[out] Caller-provided memory for decoded RA ID
789 * \param[in] buf Input buffer pointing to RAI IE value */
Harald Weltea1c4f762010-05-01 11:59:42 +0200790void gsm48_parse_ra(struct gprs_ra_id *raid, const uint8_t *buf)
791{
792 raid->mcc = (buf[0] & 0xf) * 100;
793 raid->mcc += (buf[0] >> 4) * 10;
794 raid->mcc += (buf[1] & 0xf) * 1;
795
796 /* I wonder who came up with the stupidity of encoding the MNC
797 * differently depending on how many digits its decimal number has! */
798 if ((buf[1] >> 4) == 0xf) {
799 raid->mnc = (buf[2] & 0xf) * 10;
800 raid->mnc += (buf[2] >> 4) * 1;
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100801 raid->mnc_3_digits = false;
Harald Weltea1c4f762010-05-01 11:59:42 +0200802 } else {
803 raid->mnc = (buf[2] & 0xf) * 100;
804 raid->mnc += (buf[2] >> 4) * 10;
805 raid->mnc += (buf[1] >> 4) * 1;
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100806 raid->mnc_3_digits = true;
Harald Weltea1c4f762010-05-01 11:59:42 +0200807 }
808
Harald Welte95871da2017-05-15 12:11:36 +0200809 raid->lac = osmo_load16be(buf + 3);
Harald Weltea1c4f762010-05-01 11:59:42 +0200810 raid->rac = buf[5];
811}
Harald Welte35a93942010-05-01 14:25:22 +0200812
Maxf1ad60e2018-01-05 14:19:33 +0100813/*! Encode a 3GPP TS 24.008 § 10.5.5.15 Routing area identification
814 * \param[out] out Caller-provided packed struct
815 * \param[in] raid Routing Area ID to be encoded
816 */
817void gsm48_encode_ra(struct gsm48_ra_id *out, const struct gprs_ra_id *raid)
818{
819 out->lac = osmo_htons(raid->lac);
820 out->rac = raid->rac;
821
822 out->digits[0] = ((raid->mcc / 100) % 10) | (((raid->mcc / 10) % 10) << 4);
823 out->digits[1] = raid->mcc % 10;
824
Neels Hofmeyrc4fce142018-02-20 13:47:08 +0100825 if (raid->mnc < 100 && !raid->mnc_3_digits) {
Maxf1ad60e2018-01-05 14:19:33 +0100826 out->digits[1] |= 0xf0;
827 out->digits[2] = ((raid->mnc / 10) % 10) | ((raid->mnc % 10) << 4);
828 } else {
829 out->digits[1] |= (raid->mnc % 10) << 4;
830 out->digits[2] = ((raid->mnc / 100) % 10) | (((raid->mnc / 10) % 10) << 4);
831 }
832}
833
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200834/*! Encode a TS 04.08 Routing Area Identifier
Harald Welte96e2a002017-06-12 21:44:18 +0200835 * \param[out] buf Caller-provided output buffer of 6 bytes
836 * \param[in] raid Routing Area ID to be encoded
837 * \returns number of bytes used in \a buf */
Harald Welte35a93942010-05-01 14:25:22 +0200838int gsm48_construct_ra(uint8_t *buf, const struct gprs_ra_id *raid)
839{
Maxf1ad60e2018-01-05 14:19:33 +0100840 gsm48_encode_ra((struct gsm48_ra_id *)buf, raid);
Harald Welte35a93942010-05-01 14:25:22 +0200841
842 return 6;
843}
Harald Welte2aee7b12011-06-26 14:20:04 +0200844
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200845/*! Determine number of paging sub-channels
Harald Welte96e2a002017-06-12 21:44:18 +0200846 * \param[in] chan_desc Control Channel Description
847 * \returns number of paging sub-channels
848 *
849 * Uses From Table 10.5.33 of GSM 04.08 to determine the number of
850 * paging sub-channels in the given control channel configuration
851 */
Harald Welte2aee7b12011-06-26 14:20:04 +0200852int gsm48_number_of_paging_subchannels(struct gsm48_control_channel_descr *chan_desc)
853{
Harald Welte94df39e2011-06-26 14:33:57 +0200854 unsigned int n_pag_blocks = gsm0502_get_n_pag_blocks(chan_desc);
855
856 if (chan_desc->ccch_conf == RSL_BCCH_CCCH_CONF_1_C)
857 return OSMO_MAX(1, n_pag_blocks) * (chan_desc->bs_pa_mfrms + 2);
858 else
859 return n_pag_blocks * (chan_desc->bs_pa_mfrms + 2);
Harald Welte2aee7b12011-06-26 14:20:04 +0200860}
Neels Hofmeyrdbd994c2017-03-09 23:07:02 +0100861
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200862/*! TS 04.08 Protocol Descriptor names */
Neels Hofmeyrdbd994c2017-03-09 23:07:02 +0100863const struct value_string gsm48_pdisc_names[] = {
Harald Welteea0bc962018-01-24 16:17:45 +0100864 { GSM48_PDISC_GROUP_CC, "VGCC" },
865 { GSM48_PDISC_BCAST_CC, "VBCC" },
866 { GSM48_PDISC_PDSS1, "PDSS1" },
867 { GSM48_PDISC_CC, "CC" },
868 { GSM48_PDISC_PDSS2, "PDSS2" },
869 { GSM48_PDISC_MM, "MM" },
870 { GSM48_PDISC_RR, "RR" },
871 { GSM48_PDISC_MM_GPRS, "GMM" },
872 { GSM48_PDISC_SMS, "SMS" },
873 { GSM48_PDISC_SM_GPRS, "SM" },
874 { GSM48_PDISC_NC_SS, "NCSS" },
875 { GSM48_PDISC_LOC, "LCS" },
876 { GSM48_PDISC_EXTEND, "EXTD" },
877 { GSM48_PDISC_MASK, "MASK" },
Neels Hofmeyrdbd994c2017-03-09 23:07:02 +0100878 { 0, NULL }
879};
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +0100880
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200881/*! TS 04.08 RR Message Type names */
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +0100882const struct value_string gsm48_rr_msgtype_names[] = {
883 OSMO_VALUE_STRING(GSM48_MT_RR_INIT_REQ),
884 OSMO_VALUE_STRING(GSM48_MT_RR_ADD_ASS),
885 OSMO_VALUE_STRING(GSM48_MT_RR_IMM_ASS),
886 OSMO_VALUE_STRING(GSM48_MT_RR_IMM_ASS_EXT),
887 OSMO_VALUE_STRING(GSM48_MT_RR_IMM_ASS_REJ),
888 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_ASS_FAIL),
889 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_REJECT),
890 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_REQUEST),
891 OSMO_VALUE_STRING(GSM48_MT_RR_PACKET_ASS),
892
893 OSMO_VALUE_STRING(GSM48_MT_RR_CIPH_M_CMD),
894 OSMO_VALUE_STRING(GSM48_MT_RR_CIPH_M_COMPL),
895
896 OSMO_VALUE_STRING(GSM48_MT_RR_CFG_CHG_CMD),
897 OSMO_VALUE_STRING(GSM48_MT_RR_CFG_CHG_ACK),
898 OSMO_VALUE_STRING(GSM48_MT_RR_CFG_CHG_REJ),
899
900 OSMO_VALUE_STRING(GSM48_MT_RR_ASS_CMD),
901 OSMO_VALUE_STRING(GSM48_MT_RR_ASS_COMPL),
902 OSMO_VALUE_STRING(GSM48_MT_RR_ASS_FAIL),
903 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_CMD),
904 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_COMPL),
905 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_FAIL),
906 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_INFO),
907 OSMO_VALUE_STRING(GSM48_MT_RR_HANDO_INFO),
908 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_ASS_CMD),
909
910 OSMO_VALUE_STRING(GSM48_MT_RR_CELL_CHG_ORDER),
911 OSMO_VALUE_STRING(GSM48_MT_RR_PDCH_ASS_CMD),
912
913 OSMO_VALUE_STRING(GSM48_MT_RR_CHAN_REL),
914 OSMO_VALUE_STRING(GSM48_MT_RR_PART_REL),
915 OSMO_VALUE_STRING(GSM48_MT_RR_PART_REL_COMP),
916
917 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_REQ_1),
918 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_REQ_2),
919 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_REQ_3),
920 OSMO_VALUE_STRING(GSM48_MT_RR_PAG_RESP),
921 OSMO_VALUE_STRING(GSM48_MT_RR_NOTIF_NCH),
922 OSMO_VALUE_STRING(GSM48_MT_RR_NOTIF_FACCH),
923 OSMO_VALUE_STRING(GSM48_MT_RR_NOTIF_RESP),
924 OSMO_VALUE_STRING(GSM48_MT_RR_PACKET_NOTIF),
925 OSMO_VALUE_STRING(GSM48_MT_RR_UTRAN_CLSM_CHG),
926 OSMO_VALUE_STRING(GSM48_MT_RR_CDMA2K_CLSM_CHG),
927 OSMO_VALUE_STRING(GSM48_MT_RR_IS_TO_UTRAN_HANDO),
928 OSMO_VALUE_STRING(GSM48_MT_RR_IS_TO_CDMA2K_HANDO),
929
930 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_8),
931 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_1),
932 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2),
933 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_3),
934 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_4),
935 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_5),
936 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_6),
937 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_7),
938
939 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2bis),
940 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2ter),
941 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_2quater),
942 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_5bis),
943 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_5ter),
944 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_9),
945 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_13),
946
947 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_16),
948 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_17),
949
950 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_18),
951 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_19),
952 OSMO_VALUE_STRING(GSM48_MT_RR_SYSINFO_20),
953
954 OSMO_VALUE_STRING(GSM48_MT_RR_CHAN_MODE_MODIF),
955 OSMO_VALUE_STRING(GSM48_MT_RR_STATUS),
956 OSMO_VALUE_STRING(GSM48_MT_RR_CHAN_MODE_MODIF_ACK),
957 OSMO_VALUE_STRING(GSM48_MT_RR_FREQ_REDEF),
958 OSMO_VALUE_STRING(GSM48_MT_RR_MEAS_REP),
959 OSMO_VALUE_STRING(GSM48_MT_RR_CLSM_CHG),
960 OSMO_VALUE_STRING(GSM48_MT_RR_CLSM_ENQ),
961 OSMO_VALUE_STRING(GSM48_MT_RR_EXT_MEAS_REP),
962 OSMO_VALUE_STRING(GSM48_MT_RR_EXT_MEAS_REP_ORD),
963 OSMO_VALUE_STRING(GSM48_MT_RR_GPRS_SUSP_REQ),
964 OSMO_VALUE_STRING(GSM48_MT_RR_DTM_INFO),
965
966 OSMO_VALUE_STRING(GSM48_MT_RR_VGCS_UPL_GRANT),
967 OSMO_VALUE_STRING(GSM48_MT_RR_UPLINK_RELEASE),
968 OSMO_VALUE_STRING(GSM48_MT_RR_UPLINK_FREE),
969 OSMO_VALUE_STRING(GSM48_MT_RR_UPLINK_BUSY),
970 OSMO_VALUE_STRING(GSM48_MT_RR_TALKER_IND),
971 { 0, NULL }
972};
973
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200974/*! TS 04.08 MM Message Type names */
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +0100975const struct value_string gsm48_mm_msgtype_names[] = {
976 OSMO_VALUE_STRING(GSM48_MT_MM_IMSI_DETACH_IND),
977 OSMO_VALUE_STRING(GSM48_MT_MM_LOC_UPD_ACCEPT),
978 OSMO_VALUE_STRING(GSM48_MT_MM_LOC_UPD_REJECT),
979 OSMO_VALUE_STRING(GSM48_MT_MM_LOC_UPD_REQUEST),
980
981 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_REJ),
982 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_REQ),
983 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_RESP),
984 OSMO_VALUE_STRING(GSM48_MT_MM_AUTH_FAIL),
985 OSMO_VALUE_STRING(GSM48_MT_MM_ID_REQ),
986 OSMO_VALUE_STRING(GSM48_MT_MM_ID_RESP),
987 OSMO_VALUE_STRING(GSM48_MT_MM_TMSI_REALL_CMD),
988 OSMO_VALUE_STRING(GSM48_MT_MM_TMSI_REALL_COMPL),
989
990 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_ACC),
991 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_REJ),
992 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_ABORT),
993 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_REQ),
994 OSMO_VALUE_STRING(GSM48_MT_MM_CM_SERV_PROMPT),
995 OSMO_VALUE_STRING(GSM48_MT_MM_CM_REEST_REQ),
996 OSMO_VALUE_STRING(GSM48_MT_MM_ABORT),
997
998 OSMO_VALUE_STRING(GSM48_MT_MM_NULL),
999 OSMO_VALUE_STRING(GSM48_MT_MM_STATUS),
1000 OSMO_VALUE_STRING(GSM48_MT_MM_INFO),
1001 { 0, NULL }
1002};
1003
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001004/*! TS 04.08 CC Message Type names */
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001005const struct value_string gsm48_cc_msgtype_names[] = {
1006 OSMO_VALUE_STRING(GSM48_MT_CC_ALERTING),
1007 OSMO_VALUE_STRING(GSM48_MT_CC_CALL_CONF),
1008 OSMO_VALUE_STRING(GSM48_MT_CC_CALL_PROC),
1009 OSMO_VALUE_STRING(GSM48_MT_CC_CONNECT),
1010 OSMO_VALUE_STRING(GSM48_MT_CC_CONNECT_ACK),
1011 OSMO_VALUE_STRING(GSM48_MT_CC_EMERG_SETUP),
1012 OSMO_VALUE_STRING(GSM48_MT_CC_PROGRESS),
1013 OSMO_VALUE_STRING(GSM48_MT_CC_ESTAB),
1014 OSMO_VALUE_STRING(GSM48_MT_CC_ESTAB_CONF),
1015 OSMO_VALUE_STRING(GSM48_MT_CC_RECALL),
1016 OSMO_VALUE_STRING(GSM48_MT_CC_START_CC),
1017 OSMO_VALUE_STRING(GSM48_MT_CC_SETUP),
1018
1019 OSMO_VALUE_STRING(GSM48_MT_CC_MODIFY),
1020 OSMO_VALUE_STRING(GSM48_MT_CC_MODIFY_COMPL),
1021 OSMO_VALUE_STRING(GSM48_MT_CC_MODIFY_REJECT),
1022 OSMO_VALUE_STRING(GSM48_MT_CC_USER_INFO),
1023 OSMO_VALUE_STRING(GSM48_MT_CC_HOLD),
1024 OSMO_VALUE_STRING(GSM48_MT_CC_HOLD_ACK),
1025 OSMO_VALUE_STRING(GSM48_MT_CC_HOLD_REJ),
1026 OSMO_VALUE_STRING(GSM48_MT_CC_RETR),
1027 OSMO_VALUE_STRING(GSM48_MT_CC_RETR_ACK),
1028 OSMO_VALUE_STRING(GSM48_MT_CC_RETR_REJ),
1029
1030 OSMO_VALUE_STRING(GSM48_MT_CC_DISCONNECT),
1031 OSMO_VALUE_STRING(GSM48_MT_CC_RELEASE),
1032 OSMO_VALUE_STRING(GSM48_MT_CC_RELEASE_COMPL),
1033
1034 OSMO_VALUE_STRING(GSM48_MT_CC_CONG_CTRL),
1035 OSMO_VALUE_STRING(GSM48_MT_CC_NOTIFY),
1036 OSMO_VALUE_STRING(GSM48_MT_CC_STATUS),
1037 OSMO_VALUE_STRING(GSM48_MT_CC_STATUS_ENQ),
1038 OSMO_VALUE_STRING(GSM48_MT_CC_START_DTMF),
1039 OSMO_VALUE_STRING(GSM48_MT_CC_STOP_DTMF),
1040 OSMO_VALUE_STRING(GSM48_MT_CC_STOP_DTMF_ACK),
1041 OSMO_VALUE_STRING(GSM48_MT_CC_START_DTMF_ACK),
1042 OSMO_VALUE_STRING(GSM48_MT_CC_START_DTMF_REJ),
1043 OSMO_VALUE_STRING(GSM48_MT_CC_FACILITY),
1044 { 0, NULL }
1045};
1046
Keith3cdaa8d2018-08-31 20:09:18 +02001047/*! TS 04.08 10.5..4.11 Call Control Cause Values */
1048const struct value_string gsm48_cc_cause_names[] = {
1049 { GSM48_CC_CAUSE_UNASSIGNED_NR, "UNASSIGNED_NR" },
1050 { GSM48_CC_CAUSE_NO_ROUTE, "NO_ROUTE" },
1051 { GSM48_CC_CAUSE_CHAN_UNACCEPT, "CHAN_UNACCEPT" },
1052 { GSM48_CC_CAUSE_OP_DET_BARRING, "OP_DET_BARRING" },
1053 { GSM48_CC_CAUSE_NORM_CALL_CLEAR, "NORM_CALL_CLEAR" },
1054 { GSM48_CC_CAUSE_USER_BUSY, "USER_BUSY" },
1055 { GSM48_CC_CAUSE_USER_NOTRESPOND, "USER_NOTRESPOND" },
1056 { GSM48_CC_CAUSE_USER_ALERTING_NA, "USER_ALERTING_NA" },
1057 { GSM48_CC_CAUSE_CALL_REJECTED, "CALL_REJECTED" },
1058 { GSM48_CC_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED" },
1059 { GSM48_CC_CAUSE_PRE_EMPTION, "PRE_EMPTION" },
1060 { GSM48_CC_CAUSE_NONSE_USER_CLR, "NONSE_USER_CLR" },
1061 { GSM48_CC_CAUSE_DEST_OOO, "DEST_OOO" },
1062 { GSM48_CC_CAUSE_INV_NR_FORMAT, "INV_NR_FORMAT" },
1063 { GSM48_CC_CAUSE_FACILITY_REJ, "FACILITY_REJ" },
1064 { GSM48_CC_CAUSE_RESP_STATUS_INQ, "RESP_STATUS_INQ" },
1065 { GSM48_CC_CAUSE_NORMAL_UNSPEC, "NORMAL_UNSPEC" },
1066 { GSM48_CC_CAUSE_NO_CIRCUIT_CHAN, "NO_CIRCUIT_CHAN" },
1067 { GSM48_CC_CAUSE_NETWORK_OOO, "NETWORK_OOO" },
1068 { GSM48_CC_CAUSE_TEMP_FAILURE, "TEMP_FAILURE" },
1069 { GSM48_CC_CAUSE_SWITCH_CONG, "SWITCH_CONG" },
1070 { GSM48_CC_CAUSE_ACC_INF_DISCARD, "ACC_INF_DISCARD" },
1071 { GSM48_CC_CAUSE_REQ_CHAN_UNAVAIL, "REQ_CHAN_UNAVAIL" },
1072 { GSM48_CC_CAUSE_RESOURCE_UNAVAIL, "RESOURCE_UNAVAIL" },
1073 { GSM48_CC_CAUSE_QOS_UNAVAIL, "QOS_UNAVAIL" },
1074 { GSM48_CC_CAUSE_REQ_FAC_NOT_SUBSC, "REQ_FAC_NOT_SUBSC" },
1075 { GSM48_CC_CAUSE_INC_BARRED_CUG, "INC_BARRED_CUG" },
1076 { GSM48_CC_CAUSE_BEARER_CAP_UNAUTH, "BEARER_CAP_UNAUTH" },
1077 { GSM48_CC_CAUSE_BEARER_CA_UNAVAIL, "BEARER_CA_UNAVAIL" },
1078 { GSM48_CC_CAUSE_SERV_OPT_UNAVAIL, "SERV_OPT_UNAVAIL" },
1079 { GSM48_CC_CAUSE_BEARERSERV_UNIMPL, "BEARERSERV_UNIMPL" },
1080 { GSM48_CC_CAUSE_ACM_GE_ACM_MAX, "ACM_GE_ACM_MAX" },
1081 { GSM48_CC_CAUSE_REQ_FAC_NOTIMPL, "REQ_FAC_NOTIMPL" },
1082 { GSM48_CC_CAUSE_RESTR_BCAP_AVAIL, "RESTR_BCAP_AVAIL" },
1083 { GSM48_CC_CAUSE_SERV_OPT_UNIMPL, "SERV_OPT_UNIMPL" },
1084 { GSM48_CC_CAUSE_INVAL_TRANS_ID, "INVAL_TRANS_ID" },
1085 { GSM48_CC_CAUSE_USER_NOT_IN_CUG, "USER_NOT_IN_CUG" },
1086 { GSM48_CC_CAUSE_INCOMPAT_DEST, "INCOMPAT_DEST" },
1087 { GSM48_CC_CAUSE_INVAL_TRANS_NET, "INVAL_TRANS_NET" },
1088 { GSM48_CC_CAUSE_SEMANTIC_INCORR, "SEMANTIC_INCORR" },
1089 { GSM48_CC_CAUSE_INVAL_MAND_INF, "INVAL_MAND_INF" },
1090 { GSM48_CC_CAUSE_MSGTYPE_NOTEXIST, "MSGTYPE_NOTEXIST" },
1091 { GSM48_CC_CAUSE_MSGTYPE_INCOMPAT, "MSGTYPE_INCOMPAT" },
1092 { GSM48_CC_CAUSE_IE_NOTEXIST, "IE_NOTEXIST" },
1093 { GSM48_CC_CAUSE_COND_IE_ERR, "COND_IE_ERR" },
1094 { GSM48_CC_CAUSE_MSG_INCOMP_STATE, "MSG_INCOMP_STATE" },
1095 { GSM48_CC_CAUSE_RECOVERY_TIMER, "RECOVERY_TIMER" },
1096 { GSM48_CC_CAUSE_PROTO_ERR, "PROTO_ERR" },
1097 { GSM48_CC_CAUSE_INTERWORKING, "INTERWORKING" },
1098 { 0 , NULL }
1099};
1100
Vadim Yanitskiy8d41d722018-04-17 11:17:28 +07001101/*! TS 04.80, section 3.4 Messages for supplementary services control */
1102const struct value_string gsm48_nc_ss_msgtype_names[] = {
1103 OSMO_VALUE_STRING(GSM0480_MTYPE_RELEASE_COMPLETE),
1104 OSMO_VALUE_STRING(GSM0480_MTYPE_FACILITY),
1105 OSMO_VALUE_STRING(GSM0480_MTYPE_REGISTER),
1106 { 0, NULL }
1107};
1108
Harald Welte4a62eda2019-03-18 18:27:00 +01001109/*! Compose a string naming the message type for given protocol, in a caller-provided buffer.
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001110 * If the message type string is known, return the message type name, otherwise
1111 * return "<protocol discriminator name>:<message type in hex>".
Harald Welte4a62eda2019-03-18 18:27:00 +01001112 * \param[out] buf caller-allcated output string buffer
1113 * \param[in] buf_len size of buf in bytes
Harald Welte96e2a002017-06-12 21:44:18 +02001114 * \param[in] pdisc protocol discriminator like GSM48_PDISC_MM
1115 * \param[in] msg_type message type like GSM48_MT_MM_LOC_UPD_REQUEST
Harald Welte4a62eda2019-03-18 18:27:00 +01001116 * \returns buf
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001117 */
Harald Welte4a62eda2019-03-18 18:27:00 +01001118char *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 +01001119{
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001120 const struct value_string *msgt_names;
1121
1122 switch (pdisc) {
1123 case GSM48_PDISC_RR:
1124 msgt_names = gsm48_rr_msgtype_names;
1125 break;
1126 case GSM48_PDISC_MM:
1127 msgt_names = gsm48_mm_msgtype_names;
1128 break;
1129 case GSM48_PDISC_CC:
1130 msgt_names = gsm48_cc_msgtype_names;
1131 break;
Vadim Yanitskiy8d41d722018-04-17 11:17:28 +07001132 case GSM48_PDISC_NC_SS:
1133 msgt_names = gsm48_nc_ss_msgtype_names;
1134 break;
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001135 default:
1136 msgt_names = NULL;
1137 break;
1138 }
1139
1140 if (msgt_names)
Harald Welte4a62eda2019-03-18 18:27:00 +01001141 snprintf(buf, buf_len, "%s", get_value_string(msgt_names, msg_type));
1142 else
1143 snprintf(buf, buf_len, "%s:0x%02x", gsm48_pdisc_name(pdisc), msg_type);
1144 return buf;
1145}
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001146
Harald Welte4a62eda2019-03-18 18:27:00 +01001147/*! Compose a string naming the message type for given protocol, in a static buffer.
1148 * If the message type string is known, return the message type name, otherwise
1149 * return "<protocol discriminator name>:<message type in hex>".
1150 * \param[in] pdisc protocol discriminator like GSM48_PDISC_MM
1151 * \param[in] msg_type message type like GSM48_MT_MM_LOC_UPD_REQUEST
1152 * \returns statically allocated string or string constant.
1153 */
1154const char *gsm48_pdisc_msgtype_name(uint8_t pdisc, uint8_t msg_type)
1155{
Harald Welte171ef822019-03-28 10:49:05 +01001156 static __thread char namebuf[64];
Harald Welte4a62eda2019-03-18 18:27:00 +01001157 return gsm48_pdisc_msgtype_name_buf(namebuf, sizeof(namebuf), pdisc, msg_type);
Neels Hofmeyr00ab9ed2017-03-09 23:27:56 +01001158}
Harald Welte96e2a002017-06-12 21:44:18 +02001159
Harald Welte179f3572019-03-18 18:38:47 +01001160/*! Compose a string naming the message type for given protocol, in a dynamically-allocated buffer.
1161 * If the message type string is known, return the message type name, otherwise
1162 * return "<protocol discriminator name>:<message type in hex>".
1163 * \param[in] ctx talloc context from which to allocate output buffer
1164 * \param[in] pdisc protocol discriminator like GSM48_PDISC_MM
1165 * \param[in] msg_type message type like GSM48_MT_MM_LOC_UPD_REQUEST
1166 * \returns string representation in dynamically allocated output buffer.
1167 */
1168char *gsm48_pdisc_msgtype_name_c(const void *ctx, uint8_t pdisc, uint8_t msg_type)
1169{
1170 char *namebuf = talloc_size(ctx, 64);
1171 if (!namebuf)
1172 return NULL;
1173 return gsm48_pdisc_msgtype_name_buf(namebuf, 64, pdisc, msg_type);
1174}
1175
Neels Hofmeyraead2412018-04-06 04:31:00 +02001176const struct value_string gsm48_reject_value_names[] = {
1177 { GSM48_REJECT_IMSI_UNKNOWN_IN_HLR, "IMSI_UNKNOWN_IN_HLR" },
1178 { GSM48_REJECT_ILLEGAL_MS, "ILLEGAL_MS" },
1179 { GSM48_REJECT_IMSI_UNKNOWN_IN_VLR, "IMSI_UNKNOWN_IN_VLR" },
1180 { GSM48_REJECT_IMEI_NOT_ACCEPTED, "IMEI_NOT_ACCEPTED" },
1181 { GSM48_REJECT_ILLEGAL_ME, "ILLEGAL_ME" },
1182 { GSM48_REJECT_PLMN_NOT_ALLOWED, "PLMN_NOT_ALLOWED" },
1183 { GSM48_REJECT_LOC_NOT_ALLOWED, "LOC_NOT_ALLOWED" },
1184 { GSM48_REJECT_ROAMING_NOT_ALLOWED, "ROAMING_NOT_ALLOWED" },
1185 { GSM48_REJECT_NETWORK_FAILURE, "NETWORK_FAILURE" },
1186 { GSM48_REJECT_SYNCH_FAILURE, "SYNCH_FAILURE" },
1187 { GSM48_REJECT_CONGESTION, "CONGESTION" },
1188 { GSM48_REJECT_SRV_OPT_NOT_SUPPORTED, "SRV_OPT_NOT_SUPPORTED" },
1189 { GSM48_REJECT_RQD_SRV_OPT_NOT_SUPPORTED, "RQD_SRV_OPT_NOT_SUPPORTED" },
1190 { GSM48_REJECT_SRV_OPT_TMP_OUT_OF_ORDER, "SRV_OPT_TMP_OUT_OF_ORDER" },
1191 { GSM48_REJECT_CALL_CAN_NOT_BE_IDENTIFIED, "CALL_CAN_NOT_BE_IDENTIFIED" },
1192 { GSM48_REJECT_INCORRECT_MESSAGE, "INCORRECT_MESSAGE" },
1193 { GSM48_REJECT_INVALID_MANDANTORY_INF, "INVALID_MANDANTORY_INF" },
1194 { GSM48_REJECT_MSG_TYPE_NOT_IMPLEMENTED, "MSG_TYPE_NOT_IMPLEMENTED" },
1195 { GSM48_REJECT_MSG_TYPE_NOT_COMPATIBLE, "MSG_TYPE_NOT_COMPATIBLE" },
1196 { GSM48_REJECT_INF_ELEME_NOT_IMPLEMENTED, "INF_ELEME_NOT_IMPLEMENTED" },
1197 { GSM48_REJECT_CONDTIONAL_IE_ERROR, "CONDTIONAL_IE_ERROR" },
1198 { GSM48_REJECT_MSG_NOT_COMPATIBLE, "MSG_NOT_COMPATIBLE" },
1199 { GSM48_REJECT_PROTOCOL_ERROR, "PROTOCOL_ERROR" },
1200 { GSM48_REJECT_GPRS_NOT_ALLOWED, "GPRS_NOT_ALLOWED" },
1201 { GSM48_REJECT_SERVICES_NOT_ALLOWED, "SERVICES_NOT_ALLOWED" },
1202 { GSM48_REJECT_MS_IDENTITY_NOT_DERVIVABLE, "MS_IDENTITY_NOT_DERVIVABLE" },
1203 { GSM48_REJECT_IMPLICITLY_DETACHED, "IMPLICITLY_DETACHED" },
1204 { GSM48_REJECT_GPRS_NOT_ALLOWED_IN_PLMN, "GPRS_NOT_ALLOWED_IN_PLMN" },
1205 { GSM48_REJECT_MSC_TMP_NOT_REACHABLE, "MSC_TMP_NOT_REACHABLE" },
1206 { 0, NULL }
1207};
1208
Vadim Yanitskiy30cfeeb2018-08-03 05:44:00 +07001209/*! Wrap a given \ref msg with \ref gsm48_hdr structure
1210 * \param[out] msg A message to be wrapped
1211 * \param[in] pdisc GSM TS 04.07 protocol discriminator 1/2,
1212 * sub-pdisc, trans_id or skip_ind 1/2,
1213 * see section 11.2.3.1 for details
1214 * \param[in] msg_type GSM TS 04.08 message type
1215 * @return pointer to pushed header within \ref msg
1216 */
1217struct gsm48_hdr *gsm48_push_l3hdr(struct msgb *msg,
1218 uint8_t pdisc, uint8_t msg_type)
1219{
1220 struct gsm48_hdr *gh;
1221
1222 gh = (struct gsm48_hdr *) msgb_push(msg, sizeof(*gh));
1223 gh->proto_discr = pdisc;
1224 gh->msg_type = msg_type;
1225
1226 return gh;
1227}
1228
Neels Hofmeyr92f3f5e2019-01-05 00:39:13 +01001229const struct value_string osmo_lu_type_names[] = {
1230 { GSM48_LUPD_NORMAL, "NORMAL" },
1231 { GSM48_LUPD_PERIODIC, "PERIODIC" },
1232 { GSM48_LUPD_IMSI_ATT, "IMSI-ATTACH" },
1233 { GSM48_LUPD_RESERVED, "RESERVED" },
1234 {}
1235};
1236
Neels Hofmeyrf8963f92019-01-10 23:33:32 +01001237const struct value_string osmo_cm_service_type_names[] = {
1238 { GSM48_CMSERV_MO_CALL_PACKET, "MO-Call" },
1239 { GSM48_CMSERV_EMERGENCY, "Emergency-Call" },
1240 { GSM48_CMSERV_SMS, "Short-Messaging-Service" },
1241 { GSM48_CMSERV_SUP_SERV, "Supplementary-Service" },
1242 { GSM48_CMSERV_VGCS, "Voice-Group-Call" },
1243 { GSM48_CMSERV_VBS, "Voice-Broadcast-Call" },
1244 { GSM48_CMSERV_LOC_SERV, "Location-Service" },
1245 {}
1246};
1247
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001248bool osmo_gsm48_classmark1_is_r99(const struct gsm48_classmark1 *cm1)
1249{
1250 return cm1->rev_lev >= 2;
1251}
1252
1253bool osmo_gsm48_classmark2_is_r99(const struct gsm48_classmark2 *cm2, uint8_t cm2_len)
1254{
1255 if (!cm2_len)
1256 return false;
1257 return cm2->rev_lev >= 2;
1258}
1259
1260/*! Return true if any of Classmark 1 or Classmark 2 are present and indicate R99 capability.
1261 * \param[in] cm Classmarks.
1262 * \returns True if R99 or later, false if pre-R99 or no Classmarks are present.
1263 */
1264bool osmo_gsm48_classmark_is_r99(const struct osmo_gsm48_classmark *cm)
1265{
1266 if (cm->classmark1_set)
1267 return osmo_gsm48_classmark1_is_r99(&cm->classmark1);
1268 return osmo_gsm48_classmark2_is_r99(&cm->classmark2, cm->classmark2_len);
1269}
1270
1271/*! Return a string representation of A5 cipher algorithms indicated by Classmark 1, 2 and 3.
1272 * \param[in] cm Classmarks.
1273 * \returns A statically allocated string like "cm1{a5/1=supported} cm2{0x23= A5/2 A5/3} no-cm3"
1274 */
Harald Welte4a62eda2019-03-18 18:27:00 +01001275char *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 +01001276{
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001277 char cm1[42] = "no-cm1";
1278 char cm2[42] = " no-cm2";
Neels Hofmeyr9e6f5f12019-03-11 05:07:56 +01001279 char cm3[42] = " no-cm3";
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001280
1281 if (cm->classmark1_set)
1282 snprintf(cm1, sizeof(cm1), "cm1{a5/1=%s}",
1283 cm->classmark1.a5_1 ? "not-supported":"supported" /* inverted logic */);
1284
1285 if (cm->classmark2_len >= 3)
1286 snprintf(cm2, sizeof(cm2), " cm2{0x%x=%s%s}",
1287 cm->classmark2.a5_2 + (cm->classmark2.a5_3 << 1),
1288 cm->classmark2.a5_2 ? " A5/2" : "",
1289 cm->classmark2.a5_3 ? " A5/3" : "");
1290
1291 if (cm->classmark3_len >= 1)
1292 snprintf(cm3, sizeof(cm3), " cm3{0x%x=%s%s%s%s}",
1293 cm->classmark3[0],
1294 cm->classmark3[0] & (1 << 0) ? " A5/4" : "",
1295 cm->classmark3[0] & (1 << 1) ? " A5/5" : "",
1296 cm->classmark3[0] & (1 << 2) ? " A5/6" : "",
1297 cm->classmark3[0] & (1 << 3) ? " A5/7" : "");
1298
Harald Welte4a62eda2019-03-18 18:27:00 +01001299 snprintf(buf, buf_len, "%s%s%s", cm1, cm2, cm3);
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001300 return buf;
1301}
1302
Harald Welte4a62eda2019-03-18 18:27:00 +01001303/*! Return a string representation of A5 cipher algorithms indicated by Classmark 1, 2 and 3.
1304 * \param[in] cm Classmarks.
1305 * \returns A statically allocated string like "cm1{a5/1=supported} cm2{0x23= A5/2 A5/3} no-cm3"
1306 */
1307const char *osmo_gsm48_classmark_a5_name(const struct osmo_gsm48_classmark *cm)
1308{
Harald Welte171ef822019-03-28 10:49:05 +01001309 static __thread char buf[128];
Harald Welte4a62eda2019-03-18 18:27:00 +01001310 return osmo_gsm48_classmark_a5_name_buf(buf, sizeof(buf), cm);
1311}
1312
Harald Welte179f3572019-03-18 18:38:47 +01001313/*! Return a string representation of A5 cipher algorithms indicated by Classmark 1, 2 and 3.
1314 * \param[in] ctx talloc context from which to allocate output buffer
1315 * \param[in] cm Classmarks.
1316 * \returns string like "cm1{a5/1=supported} cm2{0x23= A5/2 A5/3} no-cm3" in dynamically-allocated
1317 * output buffer.
1318 */
1319char *osmo_gsm48_classmark_a5_name_c(const void *ctx, const struct osmo_gsm48_classmark *cm)
1320{
1321 char *buf = talloc_size(ctx, 128);
1322 if (!buf)
1323 return NULL;
1324 return osmo_gsm48_classmark_a5_name_buf(buf, 128, cm);
1325}
Harald Welte4a62eda2019-03-18 18:27:00 +01001326
Neels Hofmeyr7740d262019-01-16 16:53:26 +01001327/*! Overwrite dst with the Classmark information present in src.
1328 * Add an new Classmark and overwrite in dst what src has to offer, but where src has no Classmark information, leave
1329 * dst unchanged. (For Classmark 2 and 3, dst will exactly match any non-zero Classmark length from src, hence may end
1330 * up with a shorter Classmark after this call.)
1331 * \param[out] dst The target Classmark storage to be updated.
1332 * \param[in] src The new Classmark information to read from.
1333 */
1334void osmo_gsm48_classmark_update(struct osmo_gsm48_classmark *dst, const struct osmo_gsm48_classmark *src)
1335{
1336 if (src->classmark1_set) {
1337 dst->classmark1 = src->classmark1;
1338 dst->classmark1_set = true;
1339 }
1340 if (src->classmark2_len) {
1341 dst->classmark2_len = src->classmark2_len;
1342 dst->classmark2 = src->classmark2;
1343 }
1344 if (src->classmark3_len) {
1345 dst->classmark3_len = src->classmark3_len;
1346 memcpy(dst->classmark3, src->classmark3, OSMO_MIN(sizeof(dst->classmark3), src->classmark3_len));
1347 }
1348}
1349
1350
1351/*! Determine if the given Classmark (1/2/3) value permits a given A5/n cipher.
1352 * \param[in] cm Classmarks.
1353 * \param[in] a5 The N in A5/N for which to query whether support is indicated.
1354 * \return 1 when the given A5/n is permitted, 0 when not (or a5 > 7), and negative if the respective MS Classmark is
1355 * not known, where the negative number indicates the classmark type: -2 means Classmark 2 is not available. The
1356 * idea is that when e.g. A5/3 is requested and the corresponding Classmark 3 is not available, that the caller
1357 * can react by obtaining Classmark 3 and calling again once it is available.
1358 */
1359int osmo_gsm48_classmark_supports_a5(const struct osmo_gsm48_classmark *cm, uint8_t a5)
1360{
1361 switch (a5) {
1362 case 0:
1363 /* all phones must implement A5/0, see 3GPP TS 43.020 4.9 */
1364 return 1;
1365 case 1:
1366 /* 3GPP TS 43.020 4.9 requires A5/1 to be suppored by all phones and actually states:
1367 * "The network shall not provide service to an MS which indicates that it does not
1368 * support the ciphering algorithm A5/1.". However, let's be more tolerant based
1369 * on policy here */
1370 /* See 3GPP TS 24.008 10.5.1.7 */
1371 if (!cm->classmark1_set)
1372 return -1;
1373 /* Inverted logic for this bit! */
1374 return cm->classmark1.a5_1 ? 0 : 1;
1375 case 2:
1376 /* See 3GPP TS 24.008 10.5.1.6 */
1377 if (cm->classmark2_len < 3)
1378 return -2;
1379 return cm->classmark2.a5_2 ? 1 : 0;
1380 case 3:
1381 if (cm->classmark2_len < 3)
1382 return -2;
1383 return cm->classmark2.a5_3 ? 1 : 0;
1384 case 4:
1385 case 5:
1386 case 6:
1387 case 7:
1388 /* See 3GPP TS 24.008 10.5.1.7 */
1389 if (!cm->classmark3_len)
1390 return -3;
1391 return (cm->classmark3[0] & (1 << (a5-4))) ? 1 : 0;
1392 default:
1393 return 0;
1394 }
1395}
1396
Pau Espin Pedrolb99f4ca2019-10-31 13:35:22 +01001397/*! Decode power class from Classmark1/2 RF power capability field.
1398 * \param[in] rf_power_cap The RF power capability field (3 bits).
1399 * \param[in] band the band of the arfcn from where the classmark was received
1400 * \return the MS power class on success, negative on error.
1401 */
1402int8_t osmo_gsm48_rfpowercap2powerclass(enum gsm_band band, uint8_t rf_power_cap)
1403{
1404 switch (band) {
1405 case GSM_BAND_1800:
1406 case GSM_BAND_1900:
1407 if (rf_power_cap > 2)
1408 return -1;
1409 return rf_power_cap + 1;
1410 default:
1411 if (rf_power_cap > 4)
1412 return -1;
1413 return rf_power_cap + 1;
1414 }
1415}
1416
1417
Harald Welte96e2a002017-06-12 21:44:18 +02001418/*! @} */