blob: f8bb58eb092f4206257d8355f764d75848fb76f0 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gsm_utils.c */
Harald Welteec8b4502010-02-20 20:34:29 +01002/*
3 * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de>
Holger Hans Peter Freyther6bfa7442013-08-08 12:38:52 +02004 * (C) 2009,2013 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte622b7182010-03-07 17:50:21 +01005 * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Nico Goldec56a56d2012-09-18 14:29:40 +02006 * (C) 2010-2012 by Nico Golde <nico@ngolde.de>
Harald Welteec8b4502010-02-20 20:34:29 +01007 *
8 * All Rights Reserved
9 *
Harald Weltee08da972017-11-13 01:00:26 +090010 * SPDX-License-Identifier: GPL-2.0+
11 *
Harald Welteec8b4502010-02-20 20:34:29 +010012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
Harald Welteec8b4502010-02-20 20:34:29 +010022 */
23
Harald Welted38c8b82011-08-30 11:32:56 +020024/*! \mainpage libosmogsm Documentation
25 *
26 * \section sec_intro Introduction
27 * This library is a collection of common code used in various
28 * GSM related sub-projects inside the Osmocom family of projects. It
29 * includes A5/1 and A5/2 ciphers, COMP128v1, a LAPDm implementation,
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +020030 * a GSM TLV parser, SMS utility routines as well as
Harald Welted38c8b82011-08-30 11:32:56 +020031 * protocol definitions for a series of protocols:
32 * * Um L2 (04.06)
33 * * Um L3 (04.08)
34 * * A-bis RSL (08.58)
35 * * A-bis OML (08.59, 12.21)
36 * * A (08.08)
37 * \n\n
38 * Please note that C language projects inside Osmocom are typically
39 * single-threaded event-loop state machine designs. As such,
40 * routines in libosmogsm are not thread-safe. If you must use them in
41 * a multi-threaded context, you have to add your own locking.
42 *
Harald Welte71658802017-06-12 15:40:52 +020043 * libosmogsm is developed as part of the Osmocom (Open Source Mobile
44 * Communications) project, a community-based, collaborative development
45 * project to create Free and Open Source implementations of mobile
46 * communications systems. For more information about Osmocom, please
47 * see https://osmocom.org/
48 *
Harald Welted38c8b82011-08-30 11:32:56 +020049 * \section sec_copyright Copyright and License
50 * Copyright © 2008-2011 - Harald Welte, Holger Freyther and contributors\n
51 * All rights reserved. \n\n
52 * The source code of libosmogsm is licensed under the terms of the GNU
53 * General Public License as published by the Free Software Foundation;
54 * either version 2 of the License, or (at your option) any later
55 * version.\n
56 * See <http://www.gnu.org/licenses/> or COPYING included in the source
57 * code package istelf.\n
58 * The information detailed here is provided AS IS with NO WARRANTY OF
59 * ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND
60 * FITNESS FOR A PARTICULAR PURPOSE.
61 * \n\n
62 *
Harald Welte71658802017-06-12 15:40:52 +020063 * \section sec_tracker Homepage + Issue Tracker
64 * libosmogsm is distributed as part of libosmocore and shares its
65 * project page at http://osmocom.org/projects/libosmocore
66 *
67 * An Issue Tracker can be found at
68 * https://osmocom.org/projects/libosmocore/issues
69 *
Harald Welted38c8b82011-08-30 11:32:56 +020070 * \section sec_contact Contact and Support
71 * Community-based support is available at the OpenBSC mailing list
72 * <http://lists.osmocom.org/mailman/listinfo/openbsc>\n
73 * Commercial support options available upon request from
74 * <http://sysmocom.de/>
75 */
76
Harald Welteec8b4502010-02-20 20:34:29 +010077//#include <openbsc/gsm_data.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010078#include <osmocom/core/utils.h>
Max8a5346b2016-04-22 19:28:09 +020079#include <osmocom/core/bitvec.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010080#include <osmocom/gsm/gsm_utils.h>
Max764b0222016-05-11 17:33:17 +020081#include <osmocom/gsm/meas_rep.h>
Max8a5346b2016-04-22 19:28:09 +020082#include <osmocom/gsm/protocol/gsm_04_08.h>
Harald Welteec8b4502010-02-20 20:34:29 +010083
84#include <stdlib.h>
85#include <stdint.h>
86#include <string.h>
Max764b0222016-05-11 17:33:17 +020087#include <stdbool.h>
Harald Welteec8b4502010-02-20 20:34:29 +010088#include <stdio.h>
89#include <errno.h>
Harald Welteaebe08c2010-03-04 10:39:17 +010090#include <ctype.h>
Pau Espin Pedrol363130f2017-07-03 10:42:42 +020091#include <inttypes.h>
Max4b2b0cc2017-07-10 14:32:48 +020092#include <time.h>
93#include <unistd.h>
Harald Welteec8b4502010-02-20 20:34:29 +010094
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010095#include "../../config.h"
Harald Welteec8b4502010-02-20 20:34:29 +010096
Max0187c3a2018-11-09 13:08:42 +010097#if (!EMBEDDED)
Max4b2b0cc2017-07-10 14:32:48 +020098/* FIXME: this can be removed once we bump glibc requirements to 2.25: */
Karl Koscher76f025e2019-03-11 13:19:46 -070099#ifdef __GLIBC_PREREQ
100#if __GLIBC_PREREQ(2,25)
101#define HAVE_GLIBC_GETRANDOM
102#endif /* if __GLIBC_PREREQ(2,25) */
103#endif /* ifdef __GLIBC_PREREQ */
104#ifdef HAVE_GLIBC_GETRANDOM
Max0187c3a2018-11-09 13:08:42 +0100105#pragma message ("glibc " OSMO_STRINGIFY_VAL(__GLIBC__) "." OSMO_STRINGIFY_VAL(__GLIBC_MINOR__) " random detected")
Pau Espin Pedrole2640ef2017-11-16 16:29:36 +0100106#include <sys/random.h>
Max42e567c2018-11-09 13:13:45 +0100107#undef USE_GNUTLS
Max4b2b0cc2017-07-10 14:32:48 +0200108#elif HAVE_DECL_SYS_GETRANDOM
109#include <sys/syscall.h>
Maxf6245462017-10-09 14:35:03 +0200110#ifndef GRND_NONBLOCK
111#define GRND_NONBLOCK 0x0001
Max0187c3a2018-11-09 13:08:42 +0100112#endif /* ifndef GRND_NONBLOCK */
Karl Koscher76f025e2019-03-11 13:19:46 -0700113#endif /* ifdef HAVE_GLIBC_GETRANDOM */
Max0187c3a2018-11-09 13:08:42 +0100114#endif /* !EMBEDDED */
Max4b2b0cc2017-07-10 14:32:48 +0200115
Maxed029df2017-10-26 10:56:04 +0200116#if (USE_GNUTLS)
117#pragma message ("including GnuTLS for getrandom fallback.")
118#include <gnutls/gnutls.h>
119#include <gnutls/crypto.h>
Alexander Couzens500d6332018-04-24 14:00:24 +0200120
121/* gnutls < 3.3.0 requires global init.
122 * gnutls >= 3.3.0 does it automatic.
123 * It doesn't hurt calling it twice,
124 * as long it's not done at the same time (threads).
125 */
126__attribute__((constructor))
127static void on_dso_load_gnutls(void)
128{
129 if (!gnutls_check_version("3.3.0"))
130 gnutls_global_init();
131}
132
133__attribute__((destructor))
134static void on_dso_unload_gnutls(void)
135{
136 if (!gnutls_check_version("3.3.0"))
137 gnutls_global_deinit();
138}
Max0187c3a2018-11-09 13:08:42 +0100139
140#endif /* if (USE_GNUTLS) */
Maxed029df2017-10-26 10:56:04 +0200141
Holger Hans Peter Freytherdd02a472010-07-23 16:35:00 +0800142/* ETSI GSM 03.38 6.2.1 and 6.2.1.1 default alphabet
143 * Greek symbols at hex positions 0x10 and 0x12-0x1a
144 * left out as they can't be handled with a char and
145 * since most phones don't display or write these
146 * characters this would only needlessly make the code
Jacob Erlbeck18109252013-08-12 17:07:54 +0200147 * more complex.
148 *
149 * Note that this table contains the latin1->7bit mapping _and_ has
150 * been merged with the reverse mapping (7bit->latin1) for the
151 * extended characters at offset 0x7f.
152 */
Holger Hans Peter Freytherdd02a472010-07-23 16:35:00 +0800153static unsigned char gsm_7bit_alphabet[] = {
154 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0a, 0xff, 0xff, 0x0d, 0xff,
155 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
156 0xff, 0xff, 0x20, 0x21, 0x22, 0x23, 0x02, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c,
157 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
158 0x3c, 0x3d, 0x3e, 0x3f, 0x00, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
159 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
160 0x5a, 0x3c, 0x2f, 0x3e, 0x14, 0x11, 0xff, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
161 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
162 0x78, 0x79, 0x7a, 0x28, 0x40, 0x29, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
163 0xff, 0xff, 0x0c, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5e, 0xff, 0xff,
164 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x40, 0xff, 0x01, 0xff,
165 0x03, 0xff, 0x7b, 0x7d, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5c, 0xff, 0xff, 0xff, 0xff, 0xff,
166 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5b, 0x7e, 0x5d, 0xff, 0x7c, 0xff, 0xff, 0xff,
167 0xff, 0x5b, 0x0e, 0x1c, 0x09, 0xff, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x5d,
168 0xff, 0xff, 0xff, 0xff, 0x5c, 0xff, 0x0b, 0xff, 0xff, 0xff, 0x5e, 0xff, 0xff, 0x1e, 0x7f,
169 0xff, 0xff, 0xff, 0x7b, 0x0f, 0x1d, 0xff, 0x04, 0x05, 0xff, 0xff, 0x07, 0xff, 0xff, 0xff,
170 0xff, 0x7d, 0x08, 0xff, 0xff, 0xff, 0x7c, 0xff, 0x0c, 0x06, 0xff, 0xff, 0x7e, 0xff, 0xff
171};
172
Nico Golde28de0532010-07-09 17:19:12 +0200173/* GSM 03.38 6.2.1 Character lookup for decoding */
174static int gsm_septet_lookup(uint8_t ch)
175{
176 int i = 0;
Holger Hans Peter Freyther7e1a6222011-07-24 20:17:00 +0200177 for (; i < sizeof(gsm_7bit_alphabet); i++) {
178 if (gsm_7bit_alphabet[i] == ch)
Nico Golde28de0532010-07-09 17:19:12 +0200179 return i;
180 }
181 return -1;
182}
183
Vadim Yanitskiyed8e2632019-04-07 03:20:55 +0700184/*! Compute number of octets from number of septets.
185 * For instance: 47 septets need 41,125 = 42 octets.
186 * \param[in] sept_len Number of septets
187 * \returns Number of octets required */
Dennis Wehrle291e6132011-07-24 20:14:13 +0200188uint8_t gsm_get_octet_len(const uint8_t sept_len){
189 int octet_len = (sept_len * 7) / 8;
190 if ((sept_len * 7) % 8 != 0)
191 octet_len++;
192
193 return octet_len;
194}
195
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200196/*! TS 03.38 7-bit Character unpacking (6.2.1)
Harald Welte96e2a002017-06-12 21:44:18 +0200197 * \param[out] text Caller-provided output text buffer
198 * \param[in] n Length of \a text
199 * \param[in] user_data Input Data (septets)
200 * \param[in] septet_l Number of septets in \a user_data
201 * \param[in] ud_hdr_ind User Data Header present in data
202 * \returns number of bytes written to \a text */
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200203int gsm_7bit_decode_n_hdr(char *text, size_t n, const uint8_t *user_data, uint8_t septet_l, uint8_t ud_hdr_ind)
Harald Welteec8b4502010-02-20 20:34:29 +0100204{
Maxcf37c4c2016-01-21 16:52:40 +0100205 unsigned shift = 0;
206 uint8_t c7, c8, next_is_ext = 0, lu, ru;
207 const uint8_t maxlen = gsm_get_octet_len(septet_l);
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200208 const char *text_buf_begin = text;
209 const char *text_buf_end = text + n;
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200210
211 OSMO_ASSERT (n > 0);
Harald Welteec8b4502010-02-20 20:34:29 +0100212
Dennis Wehrle291e6132011-07-24 20:14:13 +0200213 /* skip the user data header */
214 if (ud_hdr_ind) {
215 /* get user data header length + 1 (for the 'user data header length'-field) */
216 shift = ((user_data[0] + 1) * 8) / 7;
217 if ((((user_data[0] + 1) * 8) % 7) != 0)
218 shift++;
219 septet_l = septet_l - shift;
220 }
221
Maxcf37c4c2016-01-21 16:52:40 +0100222 unsigned i, l, r;
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200223 for (i = 0; i < septet_l && text != text_buf_end - 1; i++) {
Maxcf37c4c2016-01-21 16:52:40 +0100224
225 l = ((i + shift) * 7 + 7) >> 3;
226 r = ((i + shift) * 7) >> 3;
227
228 /* the left side index is always >= right side index
229 sometimes it even gets beyond array boundary
230 check for that explicitly and force 0 instead
231 */
232 if (l >= maxlen)
233 lu = 0;
234 else
235 lu = user_data[l] << (7 - (((i + shift) * 7 + 7) & 7));
236
237 ru = user_data[r] >> (((i + shift) * 7) & 7);
238
239 c7 = (lu | ru) & 0x7f;
Nico Goldec0ce9aa2010-07-20 15:43:58 +0200240
Harald Weltebe55a8b2012-09-20 10:00:25 +0200241 if (next_is_ext) {
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200242 /* this is an extension character */
Nico Goldec56a56d2012-09-18 14:29:40 +0200243 next_is_ext = 0;
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200244 c8 = gsm_7bit_alphabet[0x7f + c7];
245 } else if (c7 == 0x1b && i + 1 < septet_l) {
246 next_is_ext = 1;
Nico Golde5b67a042012-09-20 16:56:23 +0200247 continue;
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200248 } else {
249 c8 = gsm_septet_lookup(c7);
Nico Golde5b67a042012-09-20 16:56:23 +0200250 }
251
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200252 *(text++) = c8;
Harald Welteec8b4502010-02-20 20:34:29 +0100253 }
254
Nico Golde28de0532010-07-09 17:19:12 +0200255 *text = '\0';
Nico Golde28de0532010-07-09 17:19:12 +0200256
Maxcf37c4c2016-01-21 16:52:40 +0100257 return text - text_buf_begin;
Nico Golde28de0532010-07-09 17:19:12 +0200258}
259
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200260/*! Decode 7bit GSM Alphabet */
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200261int gsm_7bit_decode_n(char *text, size_t n, const uint8_t *user_data, uint8_t septet_l)
Dennis Wehrle291e6132011-07-24 20:14:13 +0200262{
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200263 return gsm_7bit_decode_n_hdr(text, n, user_data, septet_l, 0);
Dennis Wehrle291e6132011-07-24 20:14:13 +0200264}
265
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200266/*! Decode 7bit GSM Alphabet (USSD) */
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200267int gsm_7bit_decode_n_ussd(char *text, size_t n, const uint8_t *user_data, uint8_t length)
Andreas Eversberg95975552013-08-08 12:38:53 +0200268{
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200269 int nchars;
Andreas Eversberg95975552013-08-08 12:38:53 +0200270
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200271 nchars = gsm_7bit_decode_n_hdr(text, n, user_data, length, 0);
Andreas Eversberg95975552013-08-08 12:38:53 +0200272 /* remove last <CR>, if it fits up to the end of last octet */
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200273 if (nchars && (user_data[gsm_get_octet_len(length) - 1] >> 1) == '\r')
274 text[--nchars] = '\0';
Andreas Eversberg95975552013-08-08 12:38:53 +0200275
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200276 return nchars;
Andreas Eversberg95975552013-08-08 12:38:53 +0200277}
278
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200279/*! Encode a ASCII characterrs as 7-bit GSM alphabet (TS 03.38)
Harald Welte96e2a002017-06-12 21:44:18 +0200280 *
281 * This function converts a zero-terminated input string \a data from
282 * ASCII into octet-aligned 7-bit GSM characters. No packing is
283 * performed.
284 *
285 * \param[out] result caller-allocated output buffer
286 * \param[in] data input data, ASCII
287 * \returns number of octets used in \a result */
Dennis Wehrle291e6132011-07-24 20:14:13 +0200288int gsm_septet_encode(uint8_t *result, const char *data)
Nico Golde28de0532010-07-09 17:19:12 +0200289{
290 int i, y = 0;
291 uint8_t ch;
Holger Hans Peter Freyther7e1a6222011-07-24 20:17:00 +0200292 for (i = 0; i < strlen(data); i++) {
Nico Golde28de0532010-07-09 17:19:12 +0200293 ch = data[i];
294 switch(ch){
295 /* fall-through for extension characters */
296 case 0x0c:
297 case 0x5e:
298 case 0x7b:
299 case 0x7d:
300 case 0x5c:
301 case 0x5b:
302 case 0x7e:
303 case 0x5d:
304 case 0x7c:
305 result[y++] = 0x1b;
Vadim Yanitskiy46914742019-04-12 22:42:42 +0700306 /* fall-through */
Nico Golde28de0532010-07-09 17:19:12 +0200307 default:
308 result[y] = gsm_7bit_alphabet[ch];
309 break;
310 }
311 y++;
312 }
313
314 return y;
315}
316
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200317/*! GSM Default Alphabet 7bit to octet packing
Harald Welte96e2a002017-06-12 21:44:18 +0200318 * \param[out] result Caller-provided output buffer
319 * \param[in] rdata Input data septets
320 * \param[in] septet_len Length of \a rdata
321 * \param[in] padding padding bits at start
322 * \returns number of bytes used in \a result */
Vadim Yanitskiy943133c2021-01-30 01:31:32 +0100323int gsm_septet_pack(uint8_t *result, const uint8_t *rdata, size_t septet_len, uint8_t padding)
Harald Welteca693882013-03-13 15:10:55 +0100324{
Dennis Wehrle291e6132011-07-24 20:14:13 +0200325 int i = 0, z = 0;
Nico Golde28de0532010-07-09 17:19:12 +0200326 uint8_t cb, nb;
327 int shift = 0;
Dennis Wehrle291e6132011-07-24 20:14:13 +0200328 uint8_t *data = calloc(septet_len + 1, sizeof(uint8_t));
Nico Golde28de0532010-07-09 17:19:12 +0200329
Dennis Wehrle291e6132011-07-24 20:14:13 +0200330 if (padding) {
331 shift = 7 - padding;
332 /* the first zero is needed for padding */
333 memcpy(data + 1, rdata, septet_len);
334 septet_len++;
335 } else
336 memcpy(data, rdata, septet_len);
Nico Golde28de0532010-07-09 17:19:12 +0200337
Holger Hans Peter Freyther7e1a6222011-07-24 20:17:00 +0200338 for (i = 0; i < septet_len; i++) {
Dennis Wehrle291e6132011-07-24 20:14:13 +0200339 if (shift == 7) {
340 /*
341 * special end case with the. This is necessary if the
342 * last septet fits into the previous octet. E.g. 48
343 * non-extension characters:
344 * ....ag ( a = 1100001, g = 1100111)
345 * result[40] = 100001 XX, result[41] = 1100111 1 */
346 if (i + 1 < septet_len) {
347 shift = 0;
348 continue;
349 } else if (i + 1 == septet_len)
350 break;
Nico Golde28de0532010-07-09 17:19:12 +0200351 }
352
Dennis Wehrle291e6132011-07-24 20:14:13 +0200353 cb = (data[i] & 0x7f) >> shift;
354 if (i + 1 < septet_len) {
355 nb = (data[i + 1] & 0x7f) << (7 - shift);
Nico Golde28de0532010-07-09 17:19:12 +0200356 cb = cb | nb;
357 }
358
359 result[z++] = cb;
Nico Golde28de0532010-07-09 17:19:12 +0200360 shift++;
361 }
362
Dennis Wehrle291e6132011-07-24 20:14:13 +0200363 free(data);
364
Nico Goldec0ce9aa2010-07-20 15:43:58 +0200365 return z;
Harald Welteec8b4502010-02-20 20:34:29 +0100366}
367
Vadim Yanitskiy943133c2021-01-30 01:31:32 +0100368/*! Backwards compatibility wrapper for gsm_septets_pack(), deprecated. */
369int gsm_septets2octets(uint8_t *result, const uint8_t *rdata, uint8_t septet_len, uint8_t padding)
370{
371 return gsm_septet_pack(result, rdata, septet_len, padding);
372}
373
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200374/*! GSM 7-bit alphabet TS 03.38 6.2.1 Character packing
Harald Welte96e2a002017-06-12 21:44:18 +0200375 * \param[out] result Caller-provided output buffer
376 * \param[in] n Maximum length of \a result in bytes
377 * \param[in] data octet-aligned string
378 * \param[out] octets Number of octets encoded
379 * \returns number of septets encoded */
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200380int gsm_7bit_encode_n(uint8_t *result, size_t n, const char *data, int *octets)
Holger Hans Peter Freyther6bfa7442013-08-08 12:38:52 +0200381{
Holger Hans Peter Freythercc7d9ec2012-09-11 10:38:43 +0200382 int y = 0;
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200383 int o;
Jacob Erlbeck26cbd452014-01-07 13:39:24 +0100384 size_t max_septets = n * 8 / 7;
Holger Hans Peter Freythercc7d9ec2012-09-11 10:38:43 +0200385
Dennis Wehrle291e6132011-07-24 20:14:13 +0200386 /* prepare for the worst case, every character expanding to two bytes */
387 uint8_t *rdata = calloc(strlen(data) * 2, sizeof(uint8_t));
388 y = gsm_septet_encode(rdata, data);
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200389
390 if (y > max_septets) {
391 /*
392 * Limit the number of septets to avoid the generation
393 * of more than n octets.
394 */
395 y = max_septets;
396 }
397
Vadim Yanitskiy943133c2021-01-30 01:31:32 +0100398 o = gsm_septet_pack(result, rdata, y, 0);
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200399
400 if (octets)
401 *octets = o;
Dennis Wehrle291e6132011-07-24 20:14:13 +0200402
403 free(rdata);
404
405 /*
Holger Hans Peter Freythercc7d9ec2012-09-11 10:38:43 +0200406 * We don't care about the number of octets, because they are not
Dennis Wehrle291e6132011-07-24 20:14:13 +0200407 * unique. E.g.:
408 * 1.) 46 non-extension characters + 1 extension character
409 * => (46 * 7 bit + (1 * (2 * 7 bit))) / 8 bit = 42 octets
410 * 2.) 47 non-extension characters
411 * => (47 * 7 bit) / 8 bit = 41,125 = 42 octets
412 * 3.) 48 non-extension characters
413 * => (48 * 7 bit) / 8 bit = 42 octects
414 */
415 return y;
416}
417
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200418/*! Encode according to GSM 7-bit alphabet (TS 03.38 6.2.1) for USSD
Harald Welte96e2a002017-06-12 21:44:18 +0200419 * \param[out] result Caller-provided output buffer
420 * \param[in] n Maximum length of \a result in bytes
421 * \param[in] data octet-aligned string
422 * \param[out] octets Number of octets encoded
423 * \returns number of septets encoded */
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200424int gsm_7bit_encode_n_ussd(uint8_t *result, size_t n, const char *data, int *octets)
425{
426 int y;
427
428 y = gsm_7bit_encode_n(result, n, data, octets);
429 /* if last octet contains only one bit, add <CR> */
430 if (((y * 7) & 7) == 1)
431 result[(*octets) - 1] |= ('\r' << 1);
432 /* if last character is <CR> and completely fills last octet, add
433 * another <CR>. */
434 if (y && ((y * 7) & 7) == 0 && (result[(*octets) - 1] >> 1) == '\r' && *octets < n - 1) {
435 result[(*octets)++] = '\r';
436 y++;
437 }
438
439 return y;
440}
441
Max4b2b0cc2017-07-10 14:32:48 +0200442/*! Generate random identifier
443 * We use /dev/urandom (default when GRND_RANDOM flag is not set).
444 * Both /dev/(u)random numbers are coming from the same CSPRNG anyway (at least on GNU/Linux >= 4.8).
445 * See also RFC4086.
446 * \param[out] out Buffer to be filled with random data
447 * \param[in] len Number of random bytes required
448 * \returns 0 on success, or a negative error code on error.
449 */
450int osmo_get_rand_id(uint8_t *out, size_t len)
451{
Maxed029df2017-10-26 10:56:04 +0200452 int rc = -ENOTSUP;
Max4b2b0cc2017-07-10 14:32:48 +0200453
454 /* this function is intended for generating short identifiers only, not arbitrary-length random data */
455 if (len > OSMO_MAX_RAND_ID_LEN)
456 return -E2BIG;
Max0187c3a2018-11-09 13:08:42 +0100457#if (!EMBEDDED)
Karl Koscher76f025e2019-03-11 13:19:46 -0700458#ifdef HAVE_GLIBC_GETRANDOM
Max4b2b0cc2017-07-10 14:32:48 +0200459 rc = getrandom(out, len, GRND_NONBLOCK);
460#elif HAVE_DECL_SYS_GETRANDOM
461#pragma message ("Using direct syscall access for getrandom(): consider upgrading to glibc >= 2.25")
462 /* FIXME: this can be removed once we bump glibc requirements to 2.25: */
463 rc = syscall(SYS_getrandom, out, len, GRND_NONBLOCK);
Max4b2b0cc2017-07-10 14:32:48 +0200464#endif
Max0187c3a2018-11-09 13:08:42 +0100465#endif /* !EMBEDDED */
Maxed029df2017-10-26 10:56:04 +0200466
Max4b2b0cc2017-07-10 14:32:48 +0200467 /* getrandom() failed entirely: */
Maxed029df2017-10-26 10:56:04 +0200468 if (rc < 0) {
469#if (USE_GNUTLS)
Maxed029df2017-10-26 10:56:04 +0200470 return gnutls_rnd(GNUTLS_RND_RANDOM, out, len);
Harald Weltedd0d76b2019-07-21 12:08:22 +0200471#else
Max4b2b0cc2017-07-10 14:32:48 +0200472 return -errno;
Harald Weltedd0d76b2019-07-21 12:08:22 +0200473#endif
Maxed029df2017-10-26 10:56:04 +0200474 }
Max4b2b0cc2017-07-10 14:32:48 +0200475
476 /* getrandom() failed partially due to signal interruption:
477 this should never happen (according to getrandom(2)) as long as OSMO_MAX_RAND_ID_LEN < 256
478 because we do not set GRND_RANDOM but it's better to be paranoid and check anyway */
479 if (rc != len)
480 return -EAGAIN;
481
482 return 0;
483}
484
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200485/*! Build the RSL uplink measurement IE (3GPP TS 08.58 § 9.3.25)
Max764b0222016-05-11 17:33:17 +0200486 * \param[in] mru Unidirectional measurement report structure
487 * \param[in] dtxd_used Indicates if DTXd was used during measurement report
488 * period
489 * \param[out] buf Pre-allocated bufer for storing IE
490 * \returns Number of bytes filled in buf
491 */
Pau Espin Pedrol50e6f062021-09-28 11:32:00 +0200492size_t gsm0858_rsl_ul_meas_enc(const struct gsm_meas_rep_unidir *mru, bool dtxd_used,
Max764b0222016-05-11 17:33:17 +0200493 uint8_t *buf)
494{
495 buf[0] = dtxd_used ? (1 << 6) : 0;
496 buf[0] |= (mru->full.rx_lev & 0x3f);
497 buf[1] = (mru->sub.rx_lev & 0x3f);
498 buf[2] = ((mru->full.rx_qual & 7) << 3) | (mru->sub.rx_qual & 7);
499
500 return 3;
501}
502
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200503/*! Convert power class to dBm according to GSM TS 05.05
Harald Welte96e2a002017-06-12 21:44:18 +0200504 * \param[in] band GSM frequency band
505 * \param[in] class GSM power class
Pau Espin Pedrole40b9632019-10-31 15:38:30 +0100506 * \returns maximum transmit power of power class in dBm, negative on error */
507int ms_class_gmsk_dbm(enum gsm_band band, int class)
Andreas Eversberg2a68c7c2011-06-26 11:41:48 +0200508{
509 switch (band) {
510 case GSM_BAND_450:
511 case GSM_BAND_480:
512 case GSM_BAND_750:
513 case GSM_BAND_900:
514 case GSM_BAND_810:
515 case GSM_BAND_850:
516 if (class == 1)
517 return 43; /* 20W */
518 if (class == 2)
519 return 39; /* 8W */
520 if (class == 3)
521 return 37; /* 5W */
522 if (class == 4)
523 return 33; /* 2W */
524 if (class == 5)
525 return 29; /* 0.8W */
526 break;
527 case GSM_BAND_1800:
528 if (class == 1)
529 return 30; /* 1W */
530 if (class == 2)
531 return 24; /* 0.25W */
532 if (class == 3)
533 return 36; /* 4W */
534 break;
535 case GSM_BAND_1900:
536 if (class == 1)
537 return 30; /* 1W */
538 if (class == 2)
539 return 24; /* 0.25W */
540 if (class == 3)
541 return 33; /* 2W */
542 break;
543 }
544 return -EINVAL;
545}
546
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200547/*! determine power control level for given dBm value, as indicated
Harald Welte96e2a002017-06-12 21:44:18 +0200548 * by the tables in chapter 4.1.1 of GSM TS 05.05
549 * \param[in] GSM frequency band
550 * \param[in] dbm RF power value in dBm
551 * \returns TS 05.05 power control level */
Harald Welteec8b4502010-02-20 20:34:29 +0100552int ms_pwr_ctl_lvl(enum gsm_band band, unsigned int dbm)
553{
554 switch (band) {
555 case GSM_BAND_450:
556 case GSM_BAND_480:
557 case GSM_BAND_750:
558 case GSM_BAND_900:
559 case GSM_BAND_810:
560 case GSM_BAND_850:
561 if (dbm >= 39)
562 return 0;
563 else if (dbm < 5)
564 return 19;
565 else {
566 /* we are guaranteed to have (5 <= dbm < 39) */
567 return 2 + ((39 - dbm) / 2);
568 }
569 break;
570 case GSM_BAND_1800:
571 if (dbm >= 36)
572 return 29;
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200573 else if (dbm >= 34)
Harald Welteec8b4502010-02-20 20:34:29 +0100574 return 30;
575 else if (dbm >= 32)
576 return 31;
577 else if (dbm == 31)
578 return 0;
579 else {
580 /* we are guaranteed to have (0 <= dbm < 31) */
581 return (30 - dbm) / 2;
582 }
583 break;
584 case GSM_BAND_1900:
585 if (dbm >= 33)
586 return 30;
587 else if (dbm >= 32)
588 return 31;
589 else if (dbm == 31)
590 return 0;
591 else {
592 /* we are guaranteed to have (0 <= dbm < 31) */
593 return (30 - dbm) / 2;
594 }
595 break;
596 }
597 return -EINVAL;
598}
599
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200600/*! Convert TS 05.05 power level to absolute dBm value
Harald Welte96e2a002017-06-12 21:44:18 +0200601 * \param[in] band GSM frequency band
602 * \param[in] lvl TS 05.05 power control level
603 * \returns RF power level in dBm */
Harald Welteec8b4502010-02-20 20:34:29 +0100604int ms_pwr_dbm(enum gsm_band band, uint8_t lvl)
605{
606 lvl &= 0x1f;
607
608 switch (band) {
609 case GSM_BAND_450:
610 case GSM_BAND_480:
611 case GSM_BAND_750:
612 case GSM_BAND_900:
613 case GSM_BAND_810:
614 case GSM_BAND_850:
615 if (lvl < 2)
616 return 39;
617 else if (lvl < 20)
618 return 39 - ((lvl - 2) * 2) ;
619 else
620 return 5;
621 break;
622 case GSM_BAND_1800:
623 if (lvl < 16)
624 return 30 - (lvl * 2);
625 else if (lvl < 29)
626 return 0;
627 else
628 return 36 - ((lvl - 29) * 2);
629 break;
630 case GSM_BAND_1900:
631 if (lvl < 16)
632 return 30 - (lvl * 2);
633 else if (lvl < 30)
634 return -EINVAL;
635 else
636 return 33 - (lvl - 30);
637 break;
638 }
639 return -EINVAL;
640}
641
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200642/*! Convert TS 05.08 RxLev to dBm (TS 05.08 Chapter 8.1.4)
Harald Welte96e2a002017-06-12 21:44:18 +0200643 * \param[in] rxlev TS 05.08 RxLev value
644 * \returns Received RF power in dBm */
Harald Welteec8b4502010-02-20 20:34:29 +0100645int rxlev2dbm(uint8_t rxlev)
646{
647 if (rxlev > 63)
648 rxlev = 63;
649
650 return -110 + rxlev;
651}
652
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200653/*! Convert RF signal level in dBm to TS 05.08 RxLev (TS 05.08 Chapter 8.1.4)
Harald Welte96e2a002017-06-12 21:44:18 +0200654 * \param[in] dbm RF signal level in dBm
655 * \returns TS 05.08 RxLev value */
Harald Welteec8b4502010-02-20 20:34:29 +0100656uint8_t dbm2rxlev(int dbm)
657{
658 int rxlev = dbm + 110;
659
660 if (rxlev > 63)
661 rxlev = 63;
662 else if (rxlev < 0)
663 rxlev = 0;
664
665 return rxlev;
666}
667
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200668/*! Return string name of a given GSM Band */
Harald Weltecbc80622010-03-22 08:28:44 +0800669const char *gsm_band_name(enum gsm_band band)
Harald Welteaebe08c2010-03-04 10:39:17 +0100670{
671 switch (band) {
672 case GSM_BAND_450:
673 return "GSM450";
674 case GSM_BAND_480:
Sylvain Munaute10ae5b2010-07-04 11:41:36 +0200675 return "GSM480";
Harald Welteaebe08c2010-03-04 10:39:17 +0100676 case GSM_BAND_750:
677 return "GSM750";
678 case GSM_BAND_810:
679 return "GSM810";
680 case GSM_BAND_850:
681 return "GSM850";
682 case GSM_BAND_900:
683 return "GSM900";
684 case GSM_BAND_1800:
685 return "DCS1800";
686 case GSM_BAND_1900:
687 return "PCS1900";
688 }
689 return "invalid";
690}
691
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200692/*! Parse string name of a GSM band */
Harald Welteaebe08c2010-03-04 10:39:17 +0100693enum gsm_band gsm_band_parse(const char* mhz)
694{
Pau Espin Pedrol399a6f02017-06-18 14:07:37 +0200695 while (*mhz && !isdigit((unsigned char)*mhz))
Harald Welteaebe08c2010-03-04 10:39:17 +0100696 mhz++;
697
698 if (*mhz == '\0')
699 return -EINVAL;
700
Harald Welted3ff15f2010-03-07 18:23:47 +0100701 switch (strtol(mhz, NULL, 10)) {
Harald Welteaebe08c2010-03-04 10:39:17 +0100702 case 450:
703 return GSM_BAND_450;
704 case 480:
705 return GSM_BAND_480;
706 case 750:
707 return GSM_BAND_750;
708 case 810:
709 return GSM_BAND_810;
710 case 850:
711 return GSM_BAND_850;
712 case 900:
713 return GSM_BAND_900;
714 case 1800:
715 return GSM_BAND_1800;
716 case 1900:
717 return GSM_BAND_1900;
718 default:
719 return -EINVAL;
720 }
721}
722
Pau Espin Pedrolc8772512018-11-16 12:59:46 +0100723/*! Resolve GSM band from ARFCN.
Harald Welte96e2a002017-06-12 21:44:18 +0200724 * In Osmocom, we use the highest bit of the \a arfcn to indicate PCS
725 * \param[in] arfcn Osmocom ARFCN, highest bit determines PCS mode
Pau Espin Pedrolc8772512018-11-16 12:59:46 +0100726 * \param[out] band GSM Band containing \arfcn if arfcn is valid, undetermined otherwise
727 * \returns 0 if arfcn is valid and \a band was set, negative on error */
728int gsm_arfcn2band_rc(uint16_t arfcn, enum gsm_band *band)
Harald Welte622b7182010-03-07 17:50:21 +0100729{
Sylvain Munaut2a471ee2010-11-13 17:51:37 +0100730 int is_pcs = arfcn & ARFCN_PCS;
731
732 arfcn &= ~ARFCN_FLAG_MASK;
733
Pau Espin Pedrolc8772512018-11-16 12:59:46 +0100734 if (is_pcs) {
735 *band = GSM_BAND_1900;
736 return 0;
737 } else if (arfcn <= 124) {
738 *band = GSM_BAND_900;
739 return 0;
740 } else if (arfcn >= 955 && arfcn <= 1023) {
741 *band = GSM_BAND_900;
742 return 0;
743 } else if (arfcn >= 128 && arfcn <= 251) {
744 *band = GSM_BAND_850;
745 return 0;
746 } else if (arfcn >= 512 && arfcn <= 885) {
747 *band = GSM_BAND_1800;
748 return 0;
749 } else if (arfcn >= 259 && arfcn <= 293) {
750 *band = GSM_BAND_450;
751 return 0;
752 } else if (arfcn >= 306 && arfcn <= 340) {
753 *band = GSM_BAND_480;
754 return 0;
755 } else if (arfcn >= 350 && arfcn <= 425) {
756 *band = GSM_BAND_810;
757 return 0;
758 } else if (arfcn >= 438 && arfcn <= 511) {
759 *band = GSM_BAND_750;
760 return 0;
761 }
762 return -1;
763}
764
765/*! Resolve GSM band from ARFCN, aborts process on invalid ARFCN.
766 * In Osmocom, we use the highest bit of the \a arfcn to indicate PCS.
767 * DEPRECATED: Use gsm_arfcn2band_rc instead.
768 * \param[in] arfcn Osmocom ARFCN, highest bit determines PCS mode
769 * \returns GSM Band if ARFCN is valid (part of any valid band), aborts otherwise */
770enum gsm_band gsm_arfcn2band(uint16_t arfcn)
771{
772 enum gsm_band band;
773 if (gsm_arfcn2band_rc(arfcn, &band) == 0)
774 return band;
775
776 osmo_panic("%s:%d Invalid arfcn %" PRIu16 " passed to gsm_arfcn2band\n",
777 __FILE__, __LINE__, arfcn);
Harald Welte622b7182010-03-07 17:50:21 +0100778}
779
Sylvain Munaut55720312012-12-11 23:44:41 +0100780struct gsm_freq_range {
781 uint16_t arfcn_first;
782 uint16_t arfcn_last;
783 uint16_t freq_ul_first;
784 uint16_t freq_dl_offset;
785 uint16_t flags;
786};
787
788static struct gsm_freq_range gsm_ranges[] = {
789 { 512, 810, 18502, 800, ARFCN_PCS }, /* PCS 1900 */
790 { 0, 124, 8900, 450, 0 }, /* P-GSM + E-GSM ARFCN 0 */
791 { 955, 1023, 8762, 450, 0 }, /* E-GSM + R-GSM */
792 { 128, 251, 8242, 450, 0 }, /* GSM 850 */
793 { 512, 885, 17102, 950, 0 }, /* DCS 1800 */
794 { 259, 293, 4506, 100, 0 }, /* GSM 450 */
795 { 306, 340, 4790, 100, 0 }, /* GSM 480 */
796 { 350, 425, 8060, 450, 0 }, /* GSM 810 */
797 { 438, 511, 7472, 300, 0 }, /* GSM 750 */
798 { /* Guard */ }
799};
800
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200801/*! Convert an ARFCN to the frequency in MHz * 10
Harald Welte96e2a002017-06-12 21:44:18 +0200802 * \param[in] arfcn GSM ARFCN to convert
803 * \param[in] uplink Uplink (1) or Downlink (0) frequency
804 * \returns Frequency in units of 1/10ths of MHz (100kHz) */
Harald Welte622b7182010-03-07 17:50:21 +0100805uint16_t gsm_arfcn2freq10(uint16_t arfcn, int uplink)
806{
Sylvain Munaut55720312012-12-11 23:44:41 +0100807 struct gsm_freq_range *r;
808 uint16_t flags = arfcn & ARFCN_FLAG_MASK;
809 uint16_t freq10_ul = 0xffff;
810 uint16_t freq10_dl = 0xffff;
Harald Welte622b7182010-03-07 17:50:21 +0100811
Sylvain Munaut2a471ee2010-11-13 17:51:37 +0100812 arfcn &= ~ARFCN_FLAG_MASK;
813
Sylvain Munaut55720312012-12-11 23:44:41 +0100814 for (r=gsm_ranges; r->freq_ul_first>0; r++) {
815 if ((flags == r->flags) &&
816 (arfcn >= r->arfcn_first) &&
817 (arfcn <= r->arfcn_last))
818 {
819 freq10_ul = r->freq_ul_first + 2 * (arfcn - r->arfcn_first);
820 freq10_dl = freq10_ul + r->freq_dl_offset;
821 break;
822 }
823 }
824
825 return uplink ? freq10_ul : freq10_dl;
826}
827
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200828/*! Convert a Frequency in MHz * 10 to ARFCN
Harald Welte96e2a002017-06-12 21:44:18 +0200829 * \param[in] freq10 Frequency in units of 1/10ths of MHz (100kHz)
830 * \param[in] uplink Frequency is Uplink (1) or Downlink (0)
831 * \returns ARFCN in case of success; 0xffff on error */
Sylvain Munaut55720312012-12-11 23:44:41 +0100832uint16_t gsm_freq102arfcn(uint16_t freq10, int uplink)
833{
834 struct gsm_freq_range *r;
835 uint16_t freq10_lo, freq10_hi;
836 uint16_t arfcn = 0xffff;
837
838 for (r=gsm_ranges; r->freq_ul_first>0; r++) {
839 /* Generate frequency limits */
840 freq10_lo = r->freq_ul_first;
841 freq10_hi = freq10_lo + 2 * (r->arfcn_last - r->arfcn_first);
842 if (!uplink) {
843 freq10_lo += r->freq_dl_offset;
844 freq10_hi += r->freq_dl_offset;
845 }
846
847 /* Check if this fits */
848 if (freq10 >= freq10_lo && freq10 <= freq10_hi) {
849 arfcn = r->arfcn_first + ((freq10 - freq10_lo) >> 1);
850 arfcn |= r->flags;
851 break;
852 }
853 }
Harald Welte622b7182010-03-07 17:50:21 +0100854
855 if (uplink)
Sylvain Munaut55720312012-12-11 23:44:41 +0100856 arfcn |= ARFCN_UPLINK;
857
858 return arfcn;
Harald Welte622b7182010-03-07 17:50:21 +0100859}
860
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200861/*! Parse GSM Frame Number into struct \ref gsm_time
Harald Welte96e2a002017-06-12 21:44:18 +0200862 * \param[out] time Caller-provided memory for \ref gsm_time
863 * \param[in] fn GSM Frame Number */
Harald Welte622b7182010-03-07 17:50:21 +0100864void gsm_fn2gsmtime(struct gsm_time *time, uint32_t fn)
865{
866 time->fn = fn;
867 time->t1 = time->fn / (26*51);
868 time->t2 = time->fn % 26;
869 time->t3 = time->fn % 51;
870 time->tc = (time->fn / 51) % 8;
871}
872
Philipp Maierb808da42017-06-26 10:50:28 +0200873/*! Parse GSM Frame Number into printable string
874 * \param[in] fn GSM Frame Number
875 * \returns pointer to printable string */
876char *gsm_fn_as_gsmtime_str(uint32_t fn)
877{
878 struct gsm_time time;
879
880 gsm_fn2gsmtime(&time, fn);
881 return osmo_dump_gsmtime(&time);
882}
883
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200884/*! Encode decoded \ref gsm_time to Frame Number
Harald Welte96e2a002017-06-12 21:44:18 +0200885 * \param[in] time GSM Time in decoded structure
886 * \returns GSM Frame Number */
Harald Welte622b7182010-03-07 17:50:21 +0100887uint32_t gsm_gsmtime2fn(struct gsm_time *time)
888{
889 /* TS 05.02 Chapter 4.3.3 TDMA frame number */
890 return (51 * ((time->t3 - time->t2 + 26) % 26) + time->t3 + (26 * 51 * time->t1));
891}
Harald Weltea1c4f762010-05-01 11:59:42 +0200892
Harald Welte4a62eda2019-03-18 18:27:00 +0100893char *osmo_dump_gsmtime_buf(char *buf, size_t buf_len, const struct gsm_time *tm)
Pau Espin Pedrol363130f2017-07-03 10:42:42 +0200894{
Harald Welte4a62eda2019-03-18 18:27:00 +0100895 snprintf(buf, buf_len, "%06"PRIu32"/%02"PRIu16"/%02"PRIu8"/%02"PRIu8"/%02"PRIu8,
Pau Espin Pedrol363130f2017-07-03 10:42:42 +0200896 tm->fn, tm->t1, tm->t2, tm->t3, (uint8_t)tm->fn%52);
Philipp Maiere8461e42019-04-08 12:07:36 +0200897 buf[buf_len-1] = '\0';
Pau Espin Pedrol363130f2017-07-03 10:42:42 +0200898 return buf;
899}
900
Harald Welte4a62eda2019-03-18 18:27:00 +0100901char *osmo_dump_gsmtime(const struct gsm_time *tm)
902{
Harald Welte171ef822019-03-28 10:49:05 +0100903 static __thread char buf[64];
Harald Welte4a62eda2019-03-18 18:27:00 +0100904 return osmo_dump_gsmtime_buf(buf, sizeof(buf), tm);
905}
906
Harald Welte179f3572019-03-18 18:38:47 +0100907char *osmo_dump_gsmtime_c(const void *ctx, const struct gsm_time *tm)
908{
909 char *buf = talloc_size(ctx, 64);
910 if (!buf)
911 return NULL;
912 return osmo_dump_gsmtime_buf(buf, 64, tm);
913}
Harald Welte4a62eda2019-03-18 18:27:00 +0100914
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200915/*! append range1024 encoded data to bit vector
Harald Welte96e2a002017-06-12 21:44:18 +0200916 * \param[out] bv Caller-provided output bit-vector
917 * \param[in] r Input Range1024 sructure */
Max8a5346b2016-04-22 19:28:09 +0200918void bitvec_add_range1024(struct bitvec *bv, const struct gsm48_range_1024 *r)
919{
920 bitvec_set_uint(bv, r->w1_hi, 2);
921 bitvec_set_uint(bv, r->w1_lo, 8);
922 bitvec_set_uint(bv, r->w2_hi, 8);
923 bitvec_set_uint(bv, r->w2_lo, 1);
924 bitvec_set_uint(bv, r->w3_hi, 7);
925 bitvec_set_uint(bv, r->w3_lo, 2);
926 bitvec_set_uint(bv, r->w4_hi, 6);
927 bitvec_set_uint(bv, r->w4_lo, 2);
928 bitvec_set_uint(bv, r->w5_hi, 6);
929 bitvec_set_uint(bv, r->w5_lo, 2);
930 bitvec_set_uint(bv, r->w6_hi, 6);
931 bitvec_set_uint(bv, r->w6_lo, 2);
932 bitvec_set_uint(bv, r->w7_hi, 6);
933 bitvec_set_uint(bv, r->w7_lo, 2);
934 bitvec_set_uint(bv, r->w8_hi, 6);
935 bitvec_set_uint(bv, r->w8_lo, 1);
936 bitvec_set_uint(bv, r->w9, 7);
937 bitvec_set_uint(bv, r->w10, 7);
938 bitvec_set_uint(bv, r->w11_hi, 1);
939 bitvec_set_uint(bv, r->w11_lo, 6);
940 bitvec_set_uint(bv, r->w12_hi, 2);
941 bitvec_set_uint(bv, r->w12_lo, 5);
942 bitvec_set_uint(bv, r->w13_hi, 3);
943 bitvec_set_uint(bv, r->w13_lo, 4);
944 bitvec_set_uint(bv, r->w14_hi, 4);
945 bitvec_set_uint(bv, r->w14_lo, 3);
946 bitvec_set_uint(bv, r->w15_hi, 5);
947 bitvec_set_uint(bv, r->w15_lo, 2);
948 bitvec_set_uint(bv, r->w16, 6);
949}
950
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200951/*! Determine GPRS TLLI Type (TS 23.003 Chapter 2.6) */
Harald Weltea1c4f762010-05-01 11:59:42 +0200952int gprs_tlli_type(uint32_t tlli)
953{
954 if ((tlli & 0xc0000000) == 0xc0000000)
955 return TLLI_LOCAL;
956 else if ((tlli & 0xc0000000) == 0x80000000)
957 return TLLI_FOREIGN;
958 else if ((tlli & 0xf8000000) == 0x78000000)
959 return TLLI_RANDOM;
960 else if ((tlli & 0xf8000000) == 0x70000000)
961 return TLLI_AUXILIARY;
Harald Welte1f6aad12015-08-15 19:51:45 +0200962 else if ((tlli & 0xf0000000) == 0x00000000)
963 return TLLI_G_RNTI;
964 else if ((tlli & 0xf0000000) == 0x10000000)
965 return TLLI_RAND_G_RNTI;
Harald Weltea1c4f762010-05-01 11:59:42 +0200966
967 return TLLI_RESERVED;
968}
Harald Weltec2263172010-06-01 10:47:07 +0200969
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200970/*! Determine TLLI from P-TMSI
Harald Welte96e2a002017-06-12 21:44:18 +0200971 * \param[in] p_tmsi P-TMSI
972 * \param[in] type TLLI Type we want to derive from \a p_tmsi
973 * \returns TLLI of given type */
Harald Weltec2263172010-06-01 10:47:07 +0200974uint32_t gprs_tmsi2tlli(uint32_t p_tmsi, enum gprs_tlli_type type)
975{
976 uint32_t tlli;
977 switch (type) {
978 case TLLI_LOCAL:
979 tlli = p_tmsi | 0xc0000000;
980 break;
981 case TLLI_FOREIGN:
982 tlli = (p_tmsi & 0x3fffffff) | 0x80000000;
983 break;
984 default:
985 tlli = 0;
986 break;
987 }
988 return tlli;
989}
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200990
991/* Wrappers for deprecated functions: */
992
993int gsm_7bit_decode(char *text, const uint8_t *user_data, uint8_t septet_l)
994{
Jacob Erlbeck26cbd452014-01-07 13:39:24 +0100995 gsm_7bit_decode_n(text, GSM_7BIT_LEGACY_MAX_BUFFER_SIZE,
996 user_data, septet_l);
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +0200997
998 /* Mimic the original behaviour. */
999 return septet_l;
1000}
1001
1002int gsm_7bit_decode_ussd(char *text, const uint8_t *user_data, uint8_t length)
1003{
Jacob Erlbeck26cbd452014-01-07 13:39:24 +01001004 return gsm_7bit_decode_n_ussd(text, GSM_7BIT_LEGACY_MAX_BUFFER_SIZE,
1005 user_data, length);
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +02001006}
1007
1008int gsm_7bit_encode(uint8_t *result, const char *data)
1009{
1010 int out;
Jacob Erlbeck26cbd452014-01-07 13:39:24 +01001011 return gsm_7bit_encode_n(result, GSM_7BIT_LEGACY_MAX_BUFFER_SIZE,
1012 data, &out);
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +02001013}
1014
1015int gsm_7bit_encode_ussd(uint8_t *result, const char *data, int *octets)
1016{
Jacob Erlbeck26cbd452014-01-07 13:39:24 +01001017 return gsm_7bit_encode_n_ussd(result, GSM_7BIT_LEGACY_MAX_BUFFER_SIZE,
1018 data, octets);
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +02001019}
1020
1021int gsm_7bit_encode_oct(uint8_t *result, const char *data, int *octets)
1022{
Jacob Erlbeck26cbd452014-01-07 13:39:24 +01001023 return gsm_7bit_encode_n(result, GSM_7BIT_LEGACY_MAX_BUFFER_SIZE,
1024 data, octets);
Jacob Erlbeck1d7f3b52013-08-12 17:07:53 +02001025}
Neels Hofmeyr5b5c3492018-12-26 18:03:17 +01001026
1027/* This is also used by osmo-hlr's db schema */
1028const struct value_string osmo_rat_type_names[] = {
1029 { OSMO_RAT_UNKNOWN, "unknown" },
1030 { OSMO_RAT_GERAN_A, "GERAN-A" },
1031 { OSMO_RAT_UTRAN_IU, "UTRAN-Iu" },
Neels Hofmeyr0d39a8d2019-01-04 04:37:00 +01001032 { OSMO_RAT_EUTRAN_SGS, "EUTRAN-SGs" },
Neels Hofmeyr5b5c3492018-12-26 18:03:17 +01001033 {}
1034};