blob: 83d2ce75f3922dffae2196c6d345b2e91d469571 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*
Harald Weltee08da972017-11-13 01:00:26 +09002 * (C) 2016 by sysmocom - s.f.m.c. GmbH, Author: Philipp Maier
Philipp Maier22401432017-03-24 17:59:26 +01003 * All Rights Reserved
4 *
Harald Weltee08da972017-11-13 01:00:26 +09005 * SPDX-License-Identifier: GPL-2.0+
Philipp Maier22401432017-03-24 17:59:26 +01006 *
7 * This program is free software; you can redistribute it and/or modify
Harald Weltee08da972017-11-13 01:00:26 +09008 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
Philipp Maier22401432017-03-24 17:59:26 +010010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Weltee08da972017-11-13 01:00:26 +090015 * GNU General Public License for more details.
Philipp Maier22401432017-03-24 17:59:26 +010016 *
Harald Weltee08da972017-11-13 01:00:26 +090017 * You should have received a copy of the GNU General Public License
Philipp Maier22401432017-03-24 17:59:26 +010018 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
Harald Welte20725b92017-05-15 12:50:04 +020022#include "config.h"
23
Philipp Maier22401432017-03-24 17:59:26 +010024#include <osmocom/core/utils.h>
25#include <osmocom/core/msgb.h>
Harald Welte95871da2017-05-15 12:11:36 +020026#include <osmocom/core/byteswap.h>
Philipp Maier22401432017-03-24 17:59:26 +010027#include <string.h>
Philipp Maier22401432017-03-24 17:59:26 +010028#include <errno.h>
29#include <osmocom/gsm/protocol/gsm_08_08.h>
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010030#include <osmocom/gsm/gsm48.h>
Max5ec0cf52019-01-15 16:37:09 +010031#include <osmocom/gsm/gsm0808.h>
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010032#include <osmocom/gsm/gsm0808_utils.h>
Philipp Maier22401432017-03-24 17:59:26 +010033
34#define IP_V4_ADDR_LEN 4
35#define IP_V6_ADDR_LEN 16
36#define IP_PORT_LEN 2
37
Philipp Maiere0c65302017-03-28 17:05:40 +020038#define CHANNEL_TYPE_ELEMENT_MAXLEN 11
39#define CHANNEL_TYPE_ELEMENT_MINLEN 3
Philipp Maier14e76b92017-03-28 18:36:52 +020040#define ENCRYPT_INFO_ELEMENT_MINLEN 1
Philipp Maier6f725d62017-03-24 18:03:17 +010041
Harald Welte20725b92017-05-15 12:50:04 +020042#ifdef HAVE_SYS_SOCKET_H
43
44#include <sys/socket.h>
45#include <netinet/in.h>
Harald Welte96e2a002017-06-12 21:44:18 +020046
47/*! \addtogroup gsm0808
48 * @{
Harald Welte37b61652017-10-16 18:46:03 +020049 * \file gsm0808_utils.c
Harald Welte96e2a002017-06-12 21:44:18 +020050 */
51
Philipp Maier4f4905f2018-11-30 13:36:12 +010052/*! Encode TS 08.08 AoIP Cause IE
53 * \param[out] msg Message Buffer to which to append IE
54 * \param[in] cause Cause code to be used in IE
55 * \returns number of bytes added to \a msg */
56uint8_t gsm0808_enc_cause(struct msgb *msg, uint16_t cause)
57{
58 /* See also 3GPP TS 48.008 3.2.2.5 Cause */
59 uint8_t *old_tail;
60 bool extended;
61
62 old_tail = msg->tail;
63
64 extended = gsm0808_cause_ext(cause >> 8);
65
66 msgb_put_u8(msg, GSM0808_IE_CAUSE);
67 if (extended) {
68 msgb_put_u8(msg, 2);
69 msgb_put_u16(msg, cause);
70 } else {
71 msgb_put_u8(msg, 1);
72 msgb_put_u8(msg, (uint8_t) (cause & 0xFF));
73 }
74
75 return (uint8_t) (msg->tail - old_tail);
76}
77
Neels Hofmeyr87e45502017-06-20 00:17:59 +020078/*! Encode TS 08.08 AoIP transport address IE
Harald Welte96e2a002017-06-12 21:44:18 +020079 * \param[out] msg Message Buffer to which to append IE
80 * \param[in] ss Socket Address to be used in IE
Oliver Smithc66b35b2022-08-05 11:27:55 +020081 * \returns number of bytes added to \a msg; 0 if msg is too small */
Philipp Maier22401432017-03-24 17:59:26 +010082uint8_t gsm0808_enc_aoip_trasp_addr(struct msgb *msg,
83 const struct sockaddr_storage *ss)
84{
85 /* See also 3GPP TS 48.008 3.2.2.102 AoIP Transport Layer Address */
86 struct sockaddr_in *sin;
87 struct sockaddr_in6 *sin6;
88 uint16_t port = 0;
89 uint8_t *ptr;
Oliver Smithc66b35b2022-08-05 11:27:55 +020090 const uint8_t len_tl = 2;
91 uint8_t len_v = sizeof(port);
Philipp Maier22401432017-03-24 17:59:26 +010092
Philipp Maier22401432017-03-24 17:59:26 +010093 OSMO_ASSERT(ss->ss_family == AF_INET || ss->ss_family == AF_INET6);
94
Oliver Smithc66b35b2022-08-05 11:27:55 +020095 switch (ss->ss_family) {
96 case AF_INET:
97 len_v += IP_V4_ADDR_LEN;
98 break;
99 case AF_INET6:
100 len_v += IP_V6_ADDR_LEN;
101 break;
102 }
103
104 if (msgb_tailroom(msg) < len_tl + len_v)
105 return 0;
106
Philipp Maier22401432017-03-24 17:59:26 +0100107 msgb_put_u8(msg, GSM0808_IE_AOIP_TRASP_ADDR);
Oliver Smithc66b35b2022-08-05 11:27:55 +0200108 msgb_put_u8(msg, len_v);
Philipp Maier22401432017-03-24 17:59:26 +0100109
110 switch (ss->ss_family) {
111 case AF_INET:
112 sin = (struct sockaddr_in *)ss;
Harald Welte95871da2017-05-15 12:11:36 +0200113 port = osmo_ntohs(sin->sin_port);
Philipp Maier22401432017-03-24 17:59:26 +0100114 ptr = msgb_put(msg, IP_V4_ADDR_LEN);
115 memcpy(ptr, &sin->sin_addr.s_addr, IP_V4_ADDR_LEN);
116 break;
117 case AF_INET6:
118 sin6 = (struct sockaddr_in6 *)ss;
Harald Welte95871da2017-05-15 12:11:36 +0200119 port = osmo_ntohs(sin6->sin6_port);
Philipp Maier22401432017-03-24 17:59:26 +0100120 ptr = msgb_put(msg, IP_V6_ADDR_LEN);
121 memcpy(ptr, sin6->sin6_addr.s6_addr, IP_V6_ADDR_LEN);
122 break;
123 }
124
125 msgb_put_u16(msg, port);
Oliver Smithc66b35b2022-08-05 11:27:55 +0200126 return len_tl + len_v;
Philipp Maier22401432017-03-24 17:59:26 +0100127}
128
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200129/*! Decode TS 08.08 AoIP transport address IE
Harald Welte96e2a002017-06-12 21:44:18 +0200130 * \param[out] ss Caller-provided memory where decoded socket addr is stored
131 * \param[in] elem pointer to IE value
132 * \param[in] len length of \a elem in bytes
133 * \returns number of bytes parsed */
Philipp Maier22401432017-03-24 17:59:26 +0100134int gsm0808_dec_aoip_trasp_addr(struct sockaddr_storage *ss,
135 const uint8_t *elem, uint8_t len)
136{
137 /* See also 3GPP TS 48.008 3.2.2.102 AoIP Transport Layer Address */
138 struct sockaddr_in sin;
139 struct sockaddr_in6 sin6;
140 const uint8_t *old_elem = elem;
141
Philipp Maier22401432017-03-24 17:59:26 +0100142 if (!elem)
143 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200144 if (len == 0)
Philipp Maier22401432017-03-24 17:59:26 +0100145 return -EINVAL;
146
147 memset(ss, 0, sizeof(*ss));
148
149 switch (len) {
150 case IP_V4_ADDR_LEN + IP_PORT_LEN:
151 memset(&sin, 0, sizeof(sin));
152 sin.sin_family = AF_INET;
153
154 memcpy(&sin.sin_addr.s_addr, elem, IP_V4_ADDR_LEN);
155 elem += IP_V4_ADDR_LEN;
156 sin.sin_port = osmo_load16le(elem);
157 elem += IP_PORT_LEN;
158
159 memcpy(ss, &sin, sizeof(sin));
160 break;
161 case IP_V6_ADDR_LEN + IP_PORT_LEN:
162 memset(&sin6, 0, sizeof(sin6));
163 sin6.sin6_family = AF_INET6;
164
165 memcpy(sin6.sin6_addr.s6_addr, elem, IP_V6_ADDR_LEN);
166 elem += IP_V6_ADDR_LEN;
167 sin6.sin6_port = osmo_load16le(elem);
168 elem += IP_PORT_LEN;
169
170 memcpy(ss, &sin6, sizeof(sin6));
171 break;
172 default:
173 /* Malformed element! */
174 return -EINVAL;
175 break;
176 }
177
178 return (int)(elem - old_elem);
179}
Philipp Maier6f725d62017-03-24 18:03:17 +0100180
Pau Espin Pedrol18506c82019-04-16 15:47:59 +0200181/*! Decode TS 08.08 (Osmocom Extension) Osmux CID
182 * TV with len(V) == 1, and V is the CID to be used.
183 * \param[out] cid Caller-provided variable where CID is stored
184 * \param[in] elem pointer to IE value
185 * \param[in] len length of \a elem in bytes
186 * \returns number of bytes parsed */
187int gsm0808_dec_osmux_cid(uint8_t *cid, const uint8_t *elem, uint8_t len)
188{
Pau Espin Pedrol18506c82019-04-16 15:47:59 +0200189 if (!elem)
190 return -EINVAL;
191 if (len != 1)
192 return -EINVAL;
193
194 *cid = *elem;
195
196 return 1;
197}
198
Harald Welte20725b92017-05-15 12:50:04 +0200199#endif /* HAVE_SYS_SOCKET_H */
200
Harald Welteef7be492019-05-03 21:01:13 +0200201/* Decode 5-byte LAI list element data (see TS 08.08 3.2.2.27) into MCC/MNC/LAC. */
202static void decode_lai(const uint8_t *data, struct osmo_location_area_id *decoded)
203{
204 struct gsm48_loc_area_id lai;
205
206 /* Copy data to stack to prevent unaligned access in gsm48_decode_lai2(). */
207 memcpy(&lai, data, sizeof(lai)); /* don't byte swap yet */
208
209 gsm48_decode_lai2(&lai, decoded);
210}
211
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700212/* Helper function for gsm0808_enc_speech_codec[_list]().
213 * Returns number of bytes appended; negative on error. */
214static int enc_speech_codec(struct msgb *msg,
215 const struct gsm0808_speech_codec *sc)
Philipp Maier6f725d62017-03-24 18:03:17 +0100216{
217 /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
218 uint8_t header = 0;
219 uint8_t *old_tail;
Vadim Yanitskiy061b7ec2022-12-14 02:06:06 +0700220 bool type_extended;
Philipp Maierbb839662017-06-01 17:11:19 +0200221
222 /* Note: Extended codec types are codec types that require 8 instead
223 * of 4 bit to fully specify the selected codec. In the following,
224 * we check if we work with an extended type or not. We also check
225 * if the codec type is valid at all. */
Vadim Yanitskiy8e962452022-12-14 01:13:19 +0700226 switch (sc->type) {
Philipp Maierbb839662017-06-01 17:11:19 +0200227 case GSM0808_SCT_FR1:
228 case GSM0808_SCT_FR2:
229 case GSM0808_SCT_FR3:
230 case GSM0808_SCT_FR4:
231 case GSM0808_SCT_FR5:
232 case GSM0808_SCT_HR1:
233 case GSM0808_SCT_HR3:
234 case GSM0808_SCT_HR4:
235 case GSM0808_SCT_HR6:
236 type_extended = false;
237 break;
238 case GSM0808_SCT_CSD:
239 type_extended = true;
240 break;
241 default:
242 /* Invalid codec type specified */
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700243 return -EINVAL;
Philipp Maierbb839662017-06-01 17:11:19 +0200244 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100245
246 old_tail = msg->tail;
247
248 if (sc->fi)
249 header |= (1 << 7);
250 if (sc->pi)
251 header |= (1 << 6);
252 if (sc->pt)
253 header |= (1 << 5);
254 if (sc->tf)
255 header |= (1 << 4);
Philipp Maierbb839662017-06-01 17:11:19 +0200256
257 if (type_extended) {
Philipp Maier6f725d62017-03-24 18:03:17 +0100258 header |= 0x0f;
259 msgb_put_u8(msg, header);
Philipp Maierbb839662017-06-01 17:11:19 +0200260 msgb_put_u8(msg, sc->type);
Philipp Maier6f725d62017-03-24 18:03:17 +0100261 } else {
Philipp Maier6f725d62017-03-24 18:03:17 +0100262 header |= sc->type;
263 msgb_put_u8(msg, header);
Philipp Maier6f725d62017-03-24 18:03:17 +0100264 }
265
Philipp Maierbb839662017-06-01 17:11:19 +0200266 /* Note: Whether a configuration is present or not depends on the
267 * selected codec type. If present, it can either consist of one
268 * or two octets, depending on the codec type */
269 switch (sc->type) {
270 case GSM0808_SCT_FR3:
271 case GSM0808_SCT_HR3:
272 case GSM0808_SCT_HR6:
Philipp Maier7e27b142018-03-22 17:26:46 +0100273 msgb_put_u16(msg, osmo_ntohs(sc->cfg));
Philipp Maierbb839662017-06-01 17:11:19 +0200274 break;
275 case GSM0808_SCT_FR4:
276 case GSM0808_SCT_FR5:
277 case GSM0808_SCT_HR4:
278 case GSM0808_SCT_CSD:
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700279 if (sc->cfg >> 8)
280 return -EINVAL;
Philipp Maierbb839662017-06-01 17:11:19 +0200281 msgb_put_u8(msg, (uint8_t) sc->cfg & 0xff);
282 break;
283 default:
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700284 if (sc->cfg != 0)
285 return -EINVAL;
Philipp Maierbb839662017-06-01 17:11:19 +0200286 break;
287 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100288
289 return (uint8_t) (msg->tail - old_tail);
290}
291
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200292/*! Encode TS 08.08 Speech Codec IE
Harald Welte96e2a002017-06-12 21:44:18 +0200293 * \param[out] msg Message Buffer to which IE will be appended
294 * \param[in] sc Speech Codec to be encoded into IE
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700295 * \returns number of bytes appended to \a msg; negative on error */
296int gsm0808_enc_speech_codec2(struct msgb *msg,
297 const struct gsm0808_speech_codec *sc)
Philipp Maier6f725d62017-03-24 18:03:17 +0100298{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200299 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100300 uint8_t *tlv_len;
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700301 int rc;
Philipp Maier6f725d62017-03-24 18:03:17 +0100302
Philipp Maier6f725d62017-03-24 18:03:17 +0100303 msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC);
304 tlv_len = msgb_put(msg, 1);
Philipp Maier6f725d62017-03-24 18:03:17 +0100305
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700306 rc = enc_speech_codec(msg, sc);
307 if (rc < 0)
308 return rc;
Philipp Maier6f725d62017-03-24 18:03:17 +0100309
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700310 *tlv_len = rc;
Philipp Maier6f725d62017-03-24 18:03:17 +0100311 return *tlv_len + 2;
312}
313
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700314/*! Deprecated: gsm0808_enc_speech_codec2() wrapper for backwards compatibility.
315 * Mimics the old behavior: crash on missing and/or invalid input. */
316uint8_t gsm0808_enc_speech_codec(struct msgb *msg,
317 const struct gsm0808_speech_codec *sc)
318{
319 int rc;
320
321 rc = gsm0808_enc_speech_codec2(msg, sc);
322 OSMO_ASSERT(rc > 0);
323
324 return rc;
325}
326
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200327/*! Decode TS 08.08 Speech Codec IE
Harald Welte96e2a002017-06-12 21:44:18 +0200328 * \param[out] sc Caller-allocated memory for Speech Codec
329 * \param[in] elem IE value to be decoded
330 * \param[in] len Length of \a elem in bytes
331 * \returns number of bytes parsed; negative on error */
Philipp Maier6f725d62017-03-24 18:03:17 +0100332int gsm0808_dec_speech_codec(struct gsm0808_speech_codec *sc,
333 const uint8_t *elem, uint8_t len)
334{
335 /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
336 uint8_t header;
337 const uint8_t *old_elem = elem;
338
Philipp Maier6f725d62017-03-24 18:03:17 +0100339 if (!elem)
340 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200341 if (len == 0)
Philipp Maier6f725d62017-03-24 18:03:17 +0100342 return -EINVAL;
343
344 memset(sc, 0, sizeof(*sc));
345
346 header = *elem;
347
Philipp Maier85a6af22017-04-28 10:55:05 +0200348 /* An extended codec type needs at least two fields,
349 * bail if the input data length is not sufficient. */
Philipp Maier6f725d62017-03-24 18:03:17 +0100350 if ((header & 0x0F) == 0x0F && len < 2)
351 return -EINVAL;
Philipp Maier6f725d62017-03-24 18:03:17 +0100352
353 elem++;
354 len--;
355
356 if (header & (1 << 7))
357 sc->fi = true;
358 if (header & (1 << 6))
359 sc->pi = true;
360 if (header & (1 << 5))
361 sc->pt = true;
362 if (header & (1 << 4))
363 sc->tf = true;
364
365 if ((header & 0x0F) != 0x0F) {
366 sc->type = (header & 0x0F);
Philipp Maierbb839662017-06-01 17:11:19 +0200367 } else {
368 sc->type = *elem;
369 elem++;
370 len--;
Philipp Maier6f725d62017-03-24 18:03:17 +0100371 }
372
Philipp Maierbb839662017-06-01 17:11:19 +0200373 /* Note: Whether a configuration is present or not depends on the
374 * selected codec type. If present, it can either consist of one or
375 * two octets depending on the codec type */
376 switch (sc->type) {
377 case GSM0808_SCT_FR1:
378 case GSM0808_SCT_FR2:
379 case GSM0808_SCT_HR1:
380 break;
381 case GSM0808_SCT_HR4:
382 case GSM0808_SCT_CSD:
383 case GSM0808_SCT_FR4:
384 case GSM0808_SCT_FR5:
385 if (len < 1)
386 return -EINVAL;
387 sc->cfg = *elem;
388 elem++;
389 break;
390 case GSM0808_SCT_FR3:
391 case GSM0808_SCT_HR3:
392 case GSM0808_SCT_HR6:
393 if (len < 2)
394 return -EINVAL;
Philipp Maier7e27b142018-03-22 17:26:46 +0100395 sc->cfg = osmo_load16le(elem);
Philipp Maierbb839662017-06-01 17:11:19 +0200396 elem += 2;
397 break;
398 default:
399 /* Invalid codec type => malformed speech codec element! */
400 return -EINVAL;
401 break;
402 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100403
404 return (int)(elem - old_elem);
405}
406
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200407/*! Encode TS 08.08 Speech Codec list
Harald Welte96e2a002017-06-12 21:44:18 +0200408 * \param[out] msg Message Buffer to which IE is to be appended
409 * \param[in] scl Speech Codec List to be encoded into IE
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700410 * \returns number of bytes added to \a msg; negative on error */
411int gsm0808_enc_speech_codec_list2(struct msgb *msg,
412 const struct gsm0808_speech_codec_list *scl)
Philipp Maier6f725d62017-03-24 18:03:17 +0100413{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200414 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100415 uint8_t *old_tail;
416 uint8_t *tlv_len;
417 unsigned int i;
Philipp Maier6f725d62017-03-24 18:03:17 +0100418 unsigned int bytes_used = 0;
419
Philipp Maier6f725d62017-03-24 18:03:17 +0100420 msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC_LIST);
421 tlv_len = msgb_put(msg, 1);
422 old_tail = msg->tail;
423
424 for (i = 0; i < scl->len; i++) {
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700425 int rc = enc_speech_codec(msg, &scl->codec[i]);
426 if (rc < 1)
427 return rc;
Philipp Maier6f725d62017-03-24 18:03:17 +0100428 bytes_used += rc;
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700429 if (bytes_used > 0xff)
430 return -EOVERFLOW;
Philipp Maier6f725d62017-03-24 18:03:17 +0100431 }
432
433 *tlv_len = (uint8_t) (msg->tail - old_tail);
434 return *tlv_len + 2;
435}
436
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700437/*! Deprecated: gsm0808_enc_speech_codec_list2() wrapper for backwards compatibility.
438 * Mimics the old behavior: crash on missing and/or invalid input. */
439uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg,
440 const struct gsm0808_speech_codec_list *scl)
441{
442 int rc;
443
444 rc = gsm0808_enc_speech_codec_list2(msg, scl);
445 OSMO_ASSERT(rc > 0);
446
447 return rc;
448}
449
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200450/*! Decode TS 08.08 Speech Codec list IE
Harald Welte96e2a002017-06-12 21:44:18 +0200451 * \param[out] scl Caller-provided memory to store codec list
452 * \param[in] elem IE value to be decoded
453 * \param[in] len Length of \a elem in bytes
454 * \returns number of bytes parsed; negative on error */
Philipp Maier6f725d62017-03-24 18:03:17 +0100455int gsm0808_dec_speech_codec_list(struct gsm0808_speech_codec_list *scl,
456 const uint8_t *elem, uint8_t len)
457{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200458 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100459 const uint8_t *old_elem = elem;
460 unsigned int i;
461 int rc;
462 uint8_t decoded = 0;
463
Philipp Maier6f725d62017-03-24 18:03:17 +0100464 if (!elem)
465 return -EINVAL;
Philipp Maier6f725d62017-03-24 18:03:17 +0100466
467 memset(scl, 0, sizeof(*scl));
468
469 for (i = 0; i < ARRAY_SIZE(scl->codec); i++) {
470 if (len <= 0)
471 break;
472
473 rc = gsm0808_dec_speech_codec(&scl->codec[i], elem, len);
474 if (rc < 1)
475 return -EINVAL;
476
477 elem+=rc;
478 len -= rc;
479 decoded++;
480 }
481
482 scl->len = decoded;
483
Philipp Maier6f725d62017-03-24 18:03:17 +0100484 return (int)(elem - old_elem);
485}
Philipp Maiere0c65302017-03-28 17:05:40 +0200486
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200487/*! Encode TS 08.08 Channel Type IE
Harald Welte96e2a002017-06-12 21:44:18 +0200488 * \param[out] msg Message Buffer to which IE is to be appended
489 * \param[in] ct Channel Type to be encoded
490 * \returns number of bytes added to \a msg */
Philipp Maiere0c65302017-03-28 17:05:40 +0200491uint8_t gsm0808_enc_channel_type(struct msgb *msg,
492 const struct gsm0808_channel_type *ct)
493{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200494 /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */
Philipp Maiere0c65302017-03-28 17:05:40 +0200495 unsigned int i;
496 uint8_t byte;
497 uint8_t *old_tail;
498 uint8_t *tlv_len;
499
Philipp Maiere0c65302017-03-28 17:05:40 +0200500 OSMO_ASSERT(ct->perm_spch_len <= CHANNEL_TYPE_ELEMENT_MAXLEN - 2);
501
502 /* FIXME: Implement encoding support for Data
503 * and Speech + CTM Text Telephony */
504 if ((ct->ch_indctr & 0x0f) != GSM0808_CHAN_SPEECH
505 && (ct->ch_indctr & 0x0f) != GSM0808_CHAN_SIGN)
506 OSMO_ASSERT(false);
507
508 msgb_put_u8(msg, GSM0808_IE_CHANNEL_TYPE);
509 tlv_len = msgb_put(msg, 1);
510 old_tail = msg->tail;
511
512 msgb_put_u8(msg, ct->ch_indctr & 0x0f);
513 msgb_put_u8(msg, ct->ch_rate_type);
514
515 for (i = 0; i < ct->perm_spch_len; i++) {
516 byte = ct->perm_spch[i];
517
518 if (i < ct->perm_spch_len - 1)
519 byte |= 0x80;
520 msgb_put_u8(msg, byte);
521 }
522
523 *tlv_len = (uint8_t) (msg->tail - old_tail);
524 return *tlv_len + 2;
525}
526
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200527/*! Decode TS 08.08 Channel Type IE
Harald Welte96e2a002017-06-12 21:44:18 +0200528 * \param[out] ct Caller-provided memory to store channel type
529 * \param[in] elem IE Value to be decoded
530 * \param[in] len Length of \a elem in bytes
531 * \returns number of bytes parsed; negative on error */
Philipp Maiere0c65302017-03-28 17:05:40 +0200532int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct,
533 const uint8_t *elem, uint8_t len)
534{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200535 /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */
Philipp Maiere0c65302017-03-28 17:05:40 +0200536 unsigned int i;
537 uint8_t byte;
538 const uint8_t *old_elem = elem;
539
Philipp Maiere0c65302017-03-28 17:05:40 +0200540 if (!elem)
541 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200542 if (len < 3 || len > 11)
Philipp Maiere0c65302017-03-28 17:05:40 +0200543 return -EINVAL;
544
545 memset(ct, 0, sizeof(*ct));
546
547 ct->ch_indctr = (*elem) & 0x0f;
548 elem++;
549 ct->ch_rate_type = (*elem) & 0x0f;
550 elem++;
551
552 for (i = 0; i < ARRAY_SIZE(ct->perm_spch); i++) {
Oliver Smith81e5a6f2023-01-31 12:34:26 +0100553 if (elem - old_elem >= len)
554 return -EOVERFLOW;
555
Philipp Maiere0c65302017-03-28 17:05:40 +0200556 byte = *elem;
557 elem++;
558 ct->perm_spch[i] = byte & 0x7f;
559 if ((byte & 0x80) == 0x00)
560 break;
561 }
562 ct->perm_spch_len = i + 1;
563
564 return (int)(elem - old_elem);
565}
Philipp Maier14e76b92017-03-28 18:36:52 +0200566
Max969fb2e2018-12-10 11:01:10 +0100567/*! Create BSSMAP Global Call Reference, 3GPP TS 48.008 §3.2.2.115.
568 * \param[out] msg Message Buffer for appending IE
569 * \param[in] g Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1
570 * \returns number of bytes added to \a msg or 0 on error */
Max47022152018-12-19 18:51:00 +0100571static uint8_t gsm0808_enc_gcr(struct msgb *msg, const struct osmo_gcr_parsed *g)
Max969fb2e2018-12-10 11:01:10 +0100572{
573 uint8_t enc, *len = msgb_tl_put(msg, GSM0808_IE_GLOBAL_CALL_REF);
574
575 enc = osmo_enc_gcr(msg, g);
576 if (!enc)
577 return 0;
578
579 *len = enc;
580 return enc + 2; /* type (1 byte) + length (1 byte) */
581}
582
583/*! Decode BSSMAP Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1.
584 * \param[out] gcr Caller-provided memory to store Global Call Reference
Max036012b2018-12-19 17:48:56 +0100585 * \param[in] tp IE values to be decoded
Max969fb2e2018-12-10 11:01:10 +0100586 * \returns number of bytes parsed; negative on error */
Max47022152018-12-19 18:51:00 +0100587static int gsm0808_dec_gcr(struct osmo_gcr_parsed *gcr, const struct tlv_parsed *tp)
Max969fb2e2018-12-10 11:01:10 +0100588{
589 int ret;
590 const uint8_t *buf = TLVP_VAL_MINLEN(tp, GSM0808_IE_GLOBAL_CALL_REF, OSMO_GCR_MIN_LEN);
591 if (!buf)
592 return -EINVAL;
593
594 ret = osmo_dec_gcr(gcr, buf, TLVP_LEN(tp, GSM0808_IE_GLOBAL_CALL_REF));
595 if (ret < 0)
596 return -ENOENT;
597
598 return 2 + ret;
599}
600
Max47022152018-12-19 18:51:00 +0100601/*! Add LCLS parameters to a given msgb, 3GPP TS 48.008 §3.2.2.115 - 3.2.2.120.
602 * \param[out] msg Message Buffer for appending IE
603 * \param[in] lcls LCLS-related data
604 * \returns number of bytes added to \a msg or 0 on error */
605uint8_t gsm0808_enc_lcls(struct msgb *msg, const struct osmo_lcls *lcls)
606{
607 uint8_t enc = 0;
608
609 /* LCLS: §3.2.2.115 Global Call Reference */
Max3b901252019-01-15 14:15:11 +0100610 if (lcls->gcr_available)
611 enc = gsm0808_enc_gcr(msg, &lcls->gcr);
Max47022152018-12-19 18:51:00 +0100612
613 /* LCLS: §3.2.2.116 Configuration */
614 if (lcls->config != GSM0808_LCLS_CFG_NA) {
615 msgb_tv_put(msg, GSM0808_IE_LCLS_CONFIG, lcls->config);
616 enc += 2;
617 }
618
619 /* LCLS: §3.2.2.117 Connection Status Control */
620 if (lcls->control != GSM0808_LCLS_CSC_NA) {
621 msgb_tv_put(msg, GSM0808_IE_LCLS_CONN_STATUS_CTRL, lcls->control);
622 enc += 2;
623 }
624
625 /* LCLS: §3.2.2.118 Correlation-Not-Needed */
626 if (!lcls->corr_needed) {
627 msgb_v_put(msg, GSM0808_IE_LCLS_CORR_NOT_NEEDED);
628 enc++;
629 }
630
631 return enc;
632}
633
634/*! Decode LCLS parameters to a given msgb, 3GPP TS 48.008 §3.2.2.115 - 3.2.2.120.
635 * \param[out] lcls Caller-provided memory to store LCLS-related data
636 * \param[in] tp IE values to be decoded
637 * \returns GCR size or negative on error */
638int gsm0808_dec_lcls(struct osmo_lcls *lcls, const struct tlv_parsed *tp)
639{
Max3b901252019-01-15 14:15:11 +0100640 int ret = gsm0808_dec_gcr(&lcls->gcr, tp);
Max47022152018-12-19 18:51:00 +0100641
Max3b901252019-01-15 14:15:11 +0100642 lcls->gcr_available = (ret < 0) ? false : true;
Max47022152018-12-19 18:51:00 +0100643 lcls->config = tlvp_val8(tp, GSM0808_IE_LCLS_CONFIG, GSM0808_LCLS_CFG_NA);
644 lcls->control = tlvp_val8(tp, GSM0808_IE_LCLS_CONN_STATUS_CTRL, GSM0808_LCLS_CSC_NA);
645 lcls->corr_needed = TLVP_PRESENT(tp, GSM0808_IE_LCLS_CORR_NOT_NEEDED) ? false : true;
646
647 return ret;
648}
649
Harald Welte171ef822019-03-28 10:49:05 +0100650static __thread char dbuf[256];
Max5ec0cf52019-01-15 16:37:09 +0100651
652/*! Dump LCLS parameters (GCR excluded) into string for printing.
Harald Welte4a62eda2019-03-18 18:27:00 +0100653 * \param[out] buf caller-allocated output string buffer
654 * \param[in] buf_len size of buf in bytes
Max5ec0cf52019-01-15 16:37:09 +0100655 * \param[in] lcls pointer to the struct to print.
656 * \returns string representation of LCLS or NULL on error. */
Harald Welte4a62eda2019-03-18 18:27:00 +0100657char *osmo_lcls_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls)
Max5ec0cf52019-01-15 16:37:09 +0100658{
Harald Welte4a62eda2019-03-18 18:27:00 +0100659 struct osmo_strbuf s = { .buf = buf, .len = buf_len };
Max5ec0cf52019-01-15 16:37:09 +0100660
661 if (!lcls)
662 return NULL;
663
664 OSMO_STRBUF_PRINTF(s, "LCLS Config: %s, Control: %s, Correlation-Needed: %u",
665 gsm0808_lcls_config_name(lcls->config),
666 gsm0808_lcls_control_name(lcls->control),
667 lcls->corr_needed);
668
669 return dbuf;
670}
671
Harald Welte4a62eda2019-03-18 18:27:00 +0100672/*! Dump LCLS parameters (GCR excluded) into static string buffer for printing.
673 * \param[in] lcls pointer to the struct to print.
674 * \returns string representation of LCLS in static buffer or NULL on error. */
675char *osmo_lcls_dump(const struct osmo_lcls *lcls)
676{
677 return osmo_lcls_dump_buf(dbuf, sizeof(dbuf), lcls);
678}
679
Harald Welte179f3572019-03-18 18:38:47 +0100680char *osmo_lcls_dump_c(void *ctx, const struct osmo_lcls *lcls)
681{
682 char *buf = talloc_size(ctx, 256);
683 if (!buf)
684 return NULL;
685 return osmo_lcls_dump_buf(buf, 256, lcls);
686}
687
Max5ec0cf52019-01-15 16:37:09 +0100688/*! Dump GCR struct into string for printing.
Harald Welte4a62eda2019-03-18 18:27:00 +0100689 * \param[out] buf caller-allocated output string buffer
690 * \param[in] buf_len size of buf in bytes
Max5ec0cf52019-01-15 16:37:09 +0100691 * \param[in] lcls pointer to the struct to print.
692 * \returns string representation of GCR or NULL on error. */
Harald Welte4a62eda2019-03-18 18:27:00 +0100693char *osmo_gcr_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls)
Max5ec0cf52019-01-15 16:37:09 +0100694{
Harald Welte4a62eda2019-03-18 18:27:00 +0100695 struct osmo_strbuf s = { .buf = buf, .len = buf_len };
Max5ec0cf52019-01-15 16:37:09 +0100696
697 if (!lcls)
698 return NULL;
699
700 if (lcls->gcr_available) {
701 OSMO_STRBUF_PRINTF(s, "GCR NetID 0x%s, ", osmo_hexdump_nospc(lcls->gcr.net, lcls->gcr.net_len));
702 /* osmo_hexdump() uses static buffers so we can't call it twice withing the same parameter list */
703 OSMO_STRBUF_PRINTF(s, "Node 0x%x, CallRefID 0x%s", lcls->gcr.node, osmo_hexdump_nospc(lcls->gcr.cr, 5));
704 }
705
706 return dbuf;
707}
708
Harald Welte4a62eda2019-03-18 18:27:00 +0100709/*! Dump GCR struct into static string buffer for printing.
710 * \param[in] lcls pointer to the struct to print.
711 * \returns string representation of GCR in static buffer or NULL on error. */
712char *osmo_gcr_dump(const struct osmo_lcls *lcls)
713{
714 return osmo_gcr_dump_buf(dbuf, sizeof(dbuf), lcls);
715}
716
717
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200718/*! Encode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200719 * \param[out] msg Message Buffer to which IE is to be appended
720 * \param[in] ei Encryption Information to be encoded
721 * \returns number of bytes appended to \a msg */
Philipp Maier14e76b92017-03-28 18:36:52 +0200722uint8_t gsm0808_enc_encrypt_info(struct msgb *msg,
723 const struct gsm0808_encrypt_info *ei)
724{
725 unsigned int i;
726 uint8_t perm_algo = 0;
727 uint8_t *ptr;
728 uint8_t *old_tail;
729 uint8_t *tlv_len;
730
Philipp Maier14e76b92017-03-28 18:36:52 +0200731 OSMO_ASSERT(ei->key_len <= ARRAY_SIZE(ei->key));
732 OSMO_ASSERT(ei->perm_algo_len <= ENCRY_INFO_PERM_ALGO_MAXLEN);
733
734 msgb_put_u8(msg, GSM0808_IE_ENCRYPTION_INFORMATION);
735 tlv_len = msgb_put(msg, 1);
736 old_tail = msg->tail;
737
738 for (i = 0; i < ei->perm_algo_len; i++) {
739 /* Note: gsm_08_08.h defines the permitted algorithms
740 * as an enum which ranges from 0x01 to 0x08 */
741 OSMO_ASSERT(ei->perm_algo[i] != 0);
742 OSMO_ASSERT(ei->perm_algo[i] <= ENCRY_INFO_PERM_ALGO_MAXLEN);
743 perm_algo |= (1 << (ei->perm_algo[i] - 1));
744 }
745
746 msgb_put_u8(msg, perm_algo);
Neels Hofmeyr26e53b12021-06-10 00:47:03 +0200747 /* FIXME: 48.008 3.2.2.10 Encryption Information says:
748 * "When present, the key shall be 8 octets long." */
Philipp Maier14e76b92017-03-28 18:36:52 +0200749 ptr = msgb_put(msg, ei->key_len);
750 memcpy(ptr, ei->key, ei->key_len);
751
752 *tlv_len = (uint8_t) (msg->tail - old_tail);
753 return *tlv_len + 2;
754}
755
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200756/*! Decode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200757 * \param[out] ei Caller-provided memory to store encryption information
758 * \param[in] elem IE value to be decoded
759 * \param[in] len Length of \a elem in bytes
760 * \returns number of bytes parsed; negative on error */
Philipp Maier14e76b92017-03-28 18:36:52 +0200761int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei,
762 const uint8_t *elem, uint8_t len)
763{
764 uint8_t perm_algo;
765 unsigned int i;
766 unsigned int perm_algo_len = 0;
767 const uint8_t *old_elem = elem;
768
Philipp Maier14e76b92017-03-28 18:36:52 +0200769 if (!elem)
770 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200771 if (len == 0)
Philipp Maier14e76b92017-03-28 18:36:52 +0200772 return -EINVAL;
773
774 memset(ei, 0, sizeof(*ei));
775
776 perm_algo = *elem;
777 elem++;
778
779 for (i = 0; i < ENCRY_INFO_PERM_ALGO_MAXLEN; i++) {
780 if (perm_algo & (1 << i)) {
781 ei->perm_algo[perm_algo_len] = i + 1;
782 perm_algo_len++;
783 }
784 }
785 ei->perm_algo_len = perm_algo_len;
786
Neels Hofmeyr26e53b12021-06-10 00:47:03 +0200787 /* FIXME: 48.008 3.2.2.10 Encryption Information says:
788 * "When present, the key shall be 8 octets long." */
Philipp Maier14e76b92017-03-28 18:36:52 +0200789 ei->key_len = len - 1;
790 memcpy(ei->key, elem, ei->key_len);
791 elem+=ei->key_len;
792
793 return (int)(elem - old_elem);
794}
Philipp Maier783047e2017-03-29 11:35:50 +0200795
Neels Hofmeyr4a9756c2021-06-10 00:48:15 +0200796/*! Encode TS 48.008 Kc128 IE.
797 * \param[out] msg Message Buffer to which IE is to be appended.
798 * \param[in] kc128 Pointer to 16 bytes of Kc128 key data.
799 * \returns number of bytes appended to msg */
800int gsm0808_enc_kc128(struct msgb *msg, const uint8_t *kc128)
801{
802 uint8_t *start = msg->tail;
803 msgb_tv_fixed_put(msg, GSM0808_IE_KC_128, 16, kc128);
804 return msg->tail - start;
805}
806
807/*! Decode TS 48.008 Kc128 IE.
808 * \param[out] kc128 Target buffer for received Kc128 key, 16 bytes long.
809 * \param[in] elem IE value to be decoded (without IE discriminator).
810 * \param[in] len Length of elem in bytes.
811 * \returns number of bytes parsed; negative on error */
812int gsm0808_dec_kc128(uint8_t *kc128, const uint8_t *elem, uint8_t len)
813{
814 if (len != 16)
815 return -EINVAL;
816 memcpy(kc128, elem, 16);
817 return len;
818}
819
Pau Espin Pedrol85a0f112021-02-15 16:25:33 +0100820/* Store individual Cell Identifier information in a CGI, without clearing the remaining ones.
821 * This is useful to supplement one CGI with information from more than one Cell Identifier,
822 * which in turn is useful to match Cell Identifiers of differing kinds to each other.
823 * Before first invocation, clear the *dst struct externally, this function does only write those members
824 * that are present in parameter u.
825 */
826static void cell_id_to_cgi(struct osmo_cell_global_id *dst,
827 enum CELL_IDENT discr, const union gsm0808_cell_id_u *u)
828{
829 switch (discr) {
830 case CELL_IDENT_WHOLE_GLOBAL:
831 *dst = u->global;
832 return;
833
834 case CELL_IDENT_WHOLE_GLOBAL_PS:
835 dst->lai = u->global_ps.rai.lac;
836 dst->cell_identity = u->global_ps.cell_identity;
837 return;
838
839 case CELL_IDENT_LAC_AND_CI:
840 dst->lai.lac = u->lac_and_ci.lac;
841 dst->cell_identity = u->lac_and_ci.ci;
842 return;
843
844 case CELL_IDENT_CI:
845 dst->cell_identity = u->ci;
846 return;
847
848 case CELL_IDENT_LAI_AND_LAC:
849 dst->lai = u->lai_and_lac;
850 return;
851
852 case CELL_IDENT_LAC:
853 dst->lai.lac = u->lac;
854 return;
855
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +0100856 case CELL_IDENT_SAI:
857 dst->lai = u->sai.lai;
858 return;
859
Pau Espin Pedrol85a0f112021-02-15 16:25:33 +0100860 case CELL_IDENT_NO_CELL:
861 case CELL_IDENT_BSS:
862 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
863 case CELL_IDENT_UTRAN_RNC:
864 case CELL_IDENT_UTRAN_LAC_RNC:
865 /* No values to set. */
866 return;
867 }
868}
869
Harald Weltef2210032019-08-31 21:25:05 +0200870/* Return the size of the value part of a cell identifier of given type */
871int gsm0808_cell_id_size(enum CELL_IDENT discr)
872{
873 switch (discr) {
874 case CELL_IDENT_WHOLE_GLOBAL:
875 return 7;
876 case CELL_IDENT_LAC_AND_CI:
877 return 4;
878 case CELL_IDENT_CI:
879 return 2;
880 case CELL_IDENT_LAI_AND_LAC:
881 return 5;
882 case CELL_IDENT_LAC:
883 return 2;
884 case CELL_IDENT_BSS:
885 case CELL_IDENT_NO_CELL:
886 return 0;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +0100887 case CELL_IDENT_SAI:
888 return 7;
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100889 case CELL_IDENT_WHOLE_GLOBAL_PS:
890 return 8;
Harald Weltef2210032019-08-31 21:25:05 +0200891 default:
892 return -EINVAL;
893 }
894}
Harald Welteef7be492019-05-03 21:01:13 +0200895/*! Decode a single GSM 08.08 Cell ID list element payload
896 * \param[out] out caller-provided output union
897 * \param[in] discr Cell ID discriminator describing type to be decoded
898 * \param[in] buf User-provided input buffer containing binary Cell ID list element
899 * \param[in] len Length of buf
900 * \returns 0 on success; negative on error */
901int gsm0808_decode_cell_id_u(union gsm0808_cell_id_u *out, enum CELL_IDENT discr, const uint8_t *buf, unsigned int len)
902{
903 switch (discr) {
904 case CELL_IDENT_WHOLE_GLOBAL:
905 if (len < 7)
906 return -EINVAL;
907 decode_lai(buf, &out->global.lai);
908 out->global.cell_identity = osmo_load16be(buf + sizeof(struct gsm48_loc_area_id));
909 break;
910 case CELL_IDENT_LAC_AND_CI:
911 if (len < 4)
912 return -EINVAL;
913 out->lac_and_ci.lac = osmo_load16be(buf);
914 out->lac_and_ci.ci = osmo_load16be(buf + sizeof(uint16_t));
915 break;
916 case CELL_IDENT_CI:
917 if (len < 2)
918 return -EINVAL;
919 out->ci = osmo_load16be(buf);
920 break;
921 case CELL_IDENT_LAI_AND_LAC:
922 if (len < 5)
923 return -EINVAL;
924 decode_lai(buf, &out->lai_and_lac);
925 break;
926 case CELL_IDENT_LAC:
927 if (len < 2)
928 return -EINVAL;
929 out->lac = osmo_load16be(buf);
930 break;
931 case CELL_IDENT_BSS:
932 case CELL_IDENT_NO_CELL:
933 /* Does not have any list items */
934 break;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +0100935 case CELL_IDENT_SAI:
936 if (len < 7)
937 return -EINVAL;
938 decode_lai(buf, &out->sai.lai);
939 out->sai.sac = osmo_load16be(buf + sizeof(struct gsm48_loc_area_id));
940 break;
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100941 case CELL_IDENT_WHOLE_GLOBAL_PS:
Pau Espin Pedrol52489852021-02-15 16:26:37 +0100942 /* 3GPP TS 48.018 sec 11.3.9 "Cell Identifier" */
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100943 if (len < 8)
944 return -EINVAL;
945 decode_lai(buf, (struct osmo_location_area_id *)&out->global_ps.rai); /* rai contains lai + non-decoded rac */
946 out->global_ps.rai.rac = *(buf + sizeof(struct gsm48_loc_area_id));
947 out->global_ps.cell_identity = osmo_load16be(buf + sizeof(struct gsm48_loc_area_id) + 1);
948 break;
Harald Welteef7be492019-05-03 21:01:13 +0200949 default:
950 /* Remaining cell identification types are not implemented. */
951 return -EINVAL;
952 }
953 return 0;
954}
955
Harald Weltee87693c2019-05-03 20:06:50 +0200956void gsm0808_msgb_put_cell_id_u(struct msgb *msg, enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
957{
958 switch (id_discr) {
959 case CELL_IDENT_WHOLE_GLOBAL: {
960 const struct osmo_cell_global_id *id = &u->global;
961 struct gsm48_loc_area_id lai;
962 gsm48_generate_lai2(&lai, &id->lai);
963 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
964 msgb_put_u16(msg, id->cell_identity);
965 break;
966 }
967 case CELL_IDENT_LAC_AND_CI: {
968 const struct osmo_lac_and_ci_id *id = &u->lac_and_ci;
969 msgb_put_u16(msg, id->lac);
970 msgb_put_u16(msg, id->ci);
971 break;
972 }
973 case CELL_IDENT_CI:
974 msgb_put_u16(msg, u->ci);
975 break;
976 case CELL_IDENT_LAI_AND_LAC: {
977 const struct osmo_location_area_id *id = &u->lai_and_lac;
978 struct gsm48_loc_area_id lai;
979 gsm48_generate_lai2(&lai, id);
980 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
981 break;
982 }
983 case CELL_IDENT_LAC:
984 msgb_put_u16(msg, u->lac);
985 break;
986 case CELL_IDENT_BSS:
987 case CELL_IDENT_NO_CELL:
988 /* Does not have any list items */
989 break;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +0100990
991 case CELL_IDENT_SAI: {
992 const struct osmo_service_area_id *id = &u->sai;
993 struct gsm48_loc_area_id lai;
994 gsm48_generate_lai2(&lai, &id->lai);
995 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
996 msgb_put_u16(msg, id->sac);
997 break;
998 }
999
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01001000 case CELL_IDENT_WHOLE_GLOBAL_PS: {
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001001 /* 3GPP TS 48.018 sec 11.3.9 "Cell Identifier" */
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01001002 const struct osmo_cell_global_id_ps *id = &u->global_ps;
1003 struct gsm48_loc_area_id lai;
1004 gsm48_generate_lai2(&lai, &id->rai.lac);
1005 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
1006 memcpy(msgb_put(msg, 1), &id->rai.rac, 1);
1007 msgb_put_u16(msg, id->cell_identity);
1008 break;
1009 }
Harald Weltee87693c2019-05-03 20:06:50 +02001010 default:
1011 /* Support for other identifier list types is not implemented. */
1012 OSMO_ASSERT(false);
1013 }
1014}
1015
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001016/*! Encode TS 08.08 Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +02001017 * \param[out] msg Message Buffer to which IE is to be appended
1018 * \param[in] cil Cell ID List to be encoded
1019 * \returns number of bytes appended to \a msg */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001020uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg,
1021 const struct gsm0808_cell_id_list2 *cil)
1022{
1023 uint8_t *old_tail;
1024 uint8_t *tlv_len;
1025 unsigned int i;
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001026 uint8_t id_discr;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001027
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001028 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
1029 tlv_len = msgb_put(msg, 1);
1030 old_tail = msg->tail;
1031
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001032 /* CGI-PS is an osmocom-specific type. In here we don't care about the
1033 * PS part, only the CS one. */
1034 if (cil->id_discr == CELL_IDENT_WHOLE_GLOBAL_PS)
1035 id_discr = CELL_IDENT_WHOLE_GLOBAL;
1036 else
1037 id_discr = cil->id_discr & 0x0f;
1038
1039 msgb_put_u8(msg, id_discr);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001040
Alexander Couzens4e284b62019-06-23 01:53:43 +02001041 OSMO_ASSERT(cil->id_list_len <= GSM0808_CELL_ID_LIST2_MAXLEN);
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001042 for (i = 0; i < cil->id_list_len; i++) {
1043 if (cil->id_discr == CELL_IDENT_WHOLE_GLOBAL_PS) {
1044 union gsm0808_cell_id_u u;
1045 cell_id_to_cgi(&u.global, cil->id_discr, &cil->id_list[i]);
1046 gsm0808_msgb_put_cell_id_u(msg, CELL_IDENT_WHOLE_GLOBAL, &u);
1047 } else {
1048 gsm0808_msgb_put_cell_id_u(msg, cil->id_discr, &cil->id_list[i]);
1049 }
1050 }
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001051
1052 *tlv_len = (uint8_t) (msg->tail - old_tail);
1053 return *tlv_len + 2;
1054}
1055
1056/*! DEPRECATED: Use gsm0808_enc_cell_id_list2 instead.
1057 *
1058 * Encode TS 08.08 Cell Identifier List IE
1059 * \param[out] msg Message Buffer to which IE is to be appended
1060 * \param[in] cil Cell ID List to be encoded
1061 * \returns number of bytes appended to \a msg */
Philipp Maier783047e2017-03-29 11:35:50 +02001062uint8_t gsm0808_enc_cell_id_list(struct msgb *msg,
1063 const struct gsm0808_cell_id_list *cil)
1064{
1065 uint8_t *old_tail;
1066 uint8_t *tlv_len;
1067 unsigned int i;
1068
Philipp Maier783047e2017-03-29 11:35:50 +02001069 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
1070 tlv_len = msgb_put(msg, 1);
1071 old_tail = msg->tail;
1072
1073 msgb_put_u8(msg, cil->id_discr & 0x0f);
1074
1075 switch (cil->id_discr) {
1076 case CELL_IDENT_LAC:
Alexander Couzens4e284b62019-06-23 01:53:43 +02001077 OSMO_ASSERT(cil->id_list_len <= CELL_ID_LIST_LAC_MAXLEN);
Philipp Maier783047e2017-03-29 11:35:50 +02001078 for (i=0;i<cil->id_list_len;i++) {
1079 msgb_put_u16(msg, cil->id_list_lac[i]);
1080 }
1081 break;
1082 case CELL_IDENT_BSS:
1083 /* Does not have any list items */
1084 break;
1085 default:
1086 /* FIXME: Implement support for all identifier list elements */
1087 OSMO_ASSERT(false);
1088 }
1089
1090 *tlv_len = (uint8_t) (msg->tail - old_tail);
1091 return *tlv_len + 2;
1092}
1093
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001094static int parse_cell_id_global_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain,
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001095 size_t *consumed)
1096{
1097 struct osmo_cell_global_id *id;
1098 uint16_t *ci_be;
1099 size_t lai_offset;
1100 int i = 0;
1101 const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*ci_be);
1102
1103 *consumed = 0;
1104 while (remain >= elemlen) {
1105 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1106 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001107 id = &cil->id_list[i].global;
Stefan Sperling2873bf12018-03-14 18:38:41 +01001108 lai_offset = i * elemlen;
Stefan Sperling23381452018-03-15 19:38:15 +01001109 decode_lai(&data[lai_offset], &id->lai);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001110 ci_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]);
1111 id->cell_identity = osmo_load16be(ci_be);
1112 *consumed += elemlen;
1113 remain -= elemlen;
1114 i++;
1115 }
1116
1117 return i;
1118}
1119
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001120static int parse_cell_id_lac_and_ci_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain,
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001121 size_t *consumed)
1122{
1123 uint16_t *lacp_be, *ci_be;
1124 struct osmo_lac_and_ci_id *id;
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001125 int i = 0, j = 0;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001126 const size_t elemlen = sizeof(*lacp_be) + sizeof(*ci_be);
1127
1128 *consumed = 0;
1129
1130 if (remain < elemlen)
1131 return -EINVAL;
1132
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001133 lacp_be = (uint16_t *)(&data[j]);
1134 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001135 while (remain >= elemlen) {
1136 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1137 return -ENOSPC;
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001138 id = &cil->id_list[i++].lac_and_ci;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001139 id->lac = osmo_load16be(lacp_be);
1140 id->ci = osmo_load16be(ci_be);
1141 *consumed += elemlen;
1142 remain -= elemlen;
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001143 j += elemlen;
1144 lacp_be = (uint16_t *)(&data[j]);
1145 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001146 }
1147
1148 return i;
1149}
1150
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001151static int parse_cell_id_ci_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain,
1152 size_t *consumed)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001153{
1154 const uint16_t *ci_be = (const uint16_t *)data;
1155 int i = 0;
1156 const size_t elemlen = sizeof(*ci_be);
1157
1158 *consumed = 0;
1159 while (remain >= elemlen) {
1160 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1161 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001162 cil->id_list[i++].ci = osmo_load16be(ci_be++);
Stefan Sperling9c62fc62018-03-16 10:23:34 +01001163 *consumed += elemlen;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001164 remain -= elemlen;
1165 }
1166 return i;
1167}
1168
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001169static int parse_cell_id_lai_and_lac(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain,
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001170 size_t *consumed)
1171{
1172 struct osmo_location_area_id *id;
1173 int i = 0;
1174 const size_t elemlen = sizeof(struct gsm48_loc_area_id);
1175
1176 *consumed = 0;
1177 while (remain >= elemlen) {
1178 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1179 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001180 id = &cil->id_list[i].lai_and_lac;
Stefan Sperling23381452018-03-15 19:38:15 +01001181 decode_lai(&data[i * elemlen], id);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001182 *consumed += elemlen;
1183 remain -= elemlen;
1184 i++;
1185 }
1186
1187 return i;
1188}
1189
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001190static int parse_cell_id_lac_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain, size_t *consumed)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001191{
1192 const uint16_t *lac_be = (const uint16_t *)data;
1193 int i = 0;
1194 const size_t elemlen = sizeof(*lac_be);
1195
1196 *consumed = 0;
1197 while (remain >= elemlen) {
1198 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1199 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001200 cil->id_list[i++].lac = osmo_load16be(lac_be++);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001201 *consumed += elemlen;
1202 remain -= elemlen;
1203 }
1204 return i;
1205}
1206
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001207static int parse_cell_id_sai_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain, size_t *consumed)
1208{
1209 struct osmo_service_area_id *id;
1210 uint16_t *sac_be;
1211 size_t lai_offset;
1212 int i = 0;
1213 const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*sac_be);
1214
1215 *consumed = 0;
1216 while (remain >= elemlen) {
1217 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1218 return -ENOSPC;
1219 id = &cil->id_list[i].sai;
1220 lai_offset = i * elemlen;
1221 decode_lai(&data[lai_offset], &id->lai);
1222 sac_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]);
1223 id->sac = osmo_load16be(sac_be);
1224 *consumed += elemlen;
1225 remain -= elemlen;
1226 i++;
1227 }
1228
1229 return i;
1230}
1231
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001232/*! Decode Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +02001233 * \param[out] cil Caller-provided memory to store Cell ID list
1234 * \param[in] elem IE value to be decoded
1235 * \param[in] len Length of \a elem in bytes
1236 * \returns number of bytes parsed; negative on error */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001237int gsm0808_dec_cell_id_list2(struct gsm0808_cell_id_list2 *cil,
1238 const uint8_t *elem, uint8_t len)
1239{
1240 uint8_t id_discr;
1241 size_t bytes_elem = 0;
1242 int list_len = 0;
1243
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001244 if (!elem)
1245 return -EINVAL;
1246 if (len == 0)
1247 return -EINVAL;
1248
1249 memset(cil, 0, sizeof(*cil));
1250
1251 id_discr = *elem & 0x0f;
1252 elem++;
1253 len--;
1254
1255 switch (id_discr) {
1256 case CELL_IDENT_WHOLE_GLOBAL:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001257 list_len = parse_cell_id_global_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001258 break;
1259 case CELL_IDENT_LAC_AND_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001260 list_len = parse_cell_id_lac_and_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001261 break;
1262 case CELL_IDENT_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001263 list_len = parse_cell_id_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001264 break;
1265 case CELL_IDENT_LAI_AND_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001266 list_len = parse_cell_id_lai_and_lac(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001267 break;
1268 case CELL_IDENT_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001269 list_len = parse_cell_id_lac_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001270 break;
1271 case CELL_IDENT_BSS:
1272 case CELL_IDENT_NO_CELL:
1273 /* Does not have any list items */
1274 break;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001275 case CELL_IDENT_SAI:
1276 list_len = parse_cell_id_sai_list(cil, elem, len, &bytes_elem);
1277 break;
1278 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1279 case CELL_IDENT_UTRAN_RNC:
1280 case CELL_IDENT_UTRAN_LAC_RNC:
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001281 default:
1282 /* Remaining cell identification types are not implemented. */
1283 return -EINVAL;
1284 }
1285
1286 if (list_len < 0) /* parsing error */
1287 return list_len;
1288
1289 cil->id_discr = id_discr;
1290 cil->id_list_len = list_len;
1291
1292 /* One byte for the cell ID discriminator + any remaining bytes in
1293 * the IE which were consumed by the parser functions above. */
1294 return 1 + (int)bytes_elem;
1295}
1296
1297/*! DEPRECATED: Use gsm0808_dec_cell_id_list2 instead.
1298 *
1299 * Decode Cell Identifier List IE
1300 * \param[out] cil Caller-provided memory to store Cell ID list
1301 * \param[in] elem IE value to be decoded
1302 * \param[in] len Length of \a elem in bytes
1303 * \returns number of bytes parsed; negative on error */
Philipp Maier783047e2017-03-29 11:35:50 +02001304int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil,
1305 const uint8_t *elem, uint8_t len)
1306{
1307 uint8_t id_discr;
1308 const uint8_t *old_elem = elem;
1309 unsigned int item_count = 0;
1310
Philipp Maier783047e2017-03-29 11:35:50 +02001311 if (!elem)
1312 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +02001313 if (len == 0)
Philipp Maier783047e2017-03-29 11:35:50 +02001314 return -EINVAL;
1315
1316 memset(cil, 0, sizeof(*cil));
1317
1318 id_discr = *elem & 0x0f;
1319 elem++;
1320 len--;
1321
1322 cil->id_discr = id_discr;
1323
1324 switch (id_discr) {
1325 case CELL_IDENT_LAC:
1326 while (len >= 2) {
1327 cil->id_list_lac[item_count] = osmo_load16be(elem);
1328 elem += 2;
1329 item_count++;
1330 len -= 2;
1331 }
1332 case CELL_IDENT_BSS:
1333 /* Does not have any list items */
1334 break;
1335 default:
1336 /* FIXME: Implement support for all identifier list elements */
1337 return -EINVAL;
1338 }
1339
1340 cil->id_list_len = item_count;
1341 return (int)(elem - old_elem);
1342}
Harald Welte96e2a002017-06-12 21:44:18 +02001343
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001344static bool same_cell_id_list_entries(const struct gsm0808_cell_id_list2 *a, int ai,
1345 const struct gsm0808_cell_id_list2 *b, int bi)
1346{
1347 struct gsm0808_cell_id_list2 tmp = {
1348 .id_discr = a->id_discr,
1349 .id_list_len = 1,
1350 };
1351 uint8_t buf_a[32 + sizeof(struct msgb)];
1352 uint8_t buf_b[32 + sizeof(struct msgb)];
1353 struct msgb *msg_a = (void*)buf_a;
1354 struct msgb *msg_b = (void*)buf_b;
1355
1356 msg_a->data_len = 32;
1357 msg_b->data_len = 32;
1358 msgb_reset(msg_a);
1359 msgb_reset(msg_b);
1360
1361 if (a->id_discr != b->id_discr)
1362 return false;
1363 if (ai >= a->id_list_len
1364 || bi >= b->id_list_len)
1365 return false;
1366
1367 tmp.id_list[0] = a->id_list[ai];
1368 gsm0808_enc_cell_id_list2(msg_a, &tmp);
1369
1370 tmp.id_list[0] = b->id_list[bi];
1371 gsm0808_enc_cell_id_list2(msg_b, &tmp);
1372
1373 if (msg_a->len != msg_b->len)
1374 return false;
1375 if (memcmp(msg_a->data, msg_b->data, msg_a->len))
1376 return false;
1377
1378 return true;
1379}
1380
1381/*! Append entries from one Cell Identifier List to another.
1382 * The cell identifier types must be identical between the two lists.
1383 * \param dst[out] Append entries to this list.
1384 * \param src[in] Append these entries to \a dst.
1385 * \returns the nr of items added, or negative on error: -EINVAL if the id_discr mismatch
1386 * between the lists, -ENOSPC if the destination list does not have enough space. If an error is
1387 * returned, \a dst may have already been changed (particularly on -ENOSPC). Note that a return value
1388 * of zero may occur when the src->id_list_len is zero, or when all entries from \a src already exist
1389 * in \a dst, and does not indicate error per se. */
1390int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src)
1391{
1392 int i, j;
1393 int added = 0;
1394
1395 if (dst->id_list_len == 0
1396 && dst->id_discr != CELL_IDENT_BSS)
1397 dst->id_discr = src->id_discr;
1398 else if (dst->id_discr != src->id_discr)
1399 return -EINVAL;
1400
1401 for (i = 0; i < src->id_list_len; i++) {
1402 /* don't add duplicate entries */
1403 bool skip = false;
1404 for (j = 0; j < dst->id_list_len; j++) {
1405 if (same_cell_id_list_entries(dst, j, src, i)) {
1406 skip = true;
1407 break;
1408 }
1409 }
1410 if (skip)
1411 continue;
1412
1413 if (dst->id_list_len >= ARRAY_SIZE(dst->id_list))
1414 return -ENOSPC;
1415
1416 dst->id_list[dst->id_list_len++] = src->id_list[i];
1417 added ++;
1418 }
1419
1420 return added;
1421}
1422
Neels Hofmeyr38e58412018-05-25 16:56:35 +02001423/*! Convert a single Cell Identifier to a Cell Identifier List with one entry.
1424 * \param dst[out] Overwrite this list.
1425 * \param src[in] Set \a dst to contain exactly this item.
1426 */
1427void gsm0808_cell_id_to_list(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id *src)
1428{
1429 if (!dst)
1430 return;
1431 if (!src) {
1432 *dst = (struct gsm0808_cell_id_list2){
1433 .id_discr = CELL_IDENT_NO_CELL,
1434 };
1435 return;
1436 }
1437
1438 *dst = (struct gsm0808_cell_id_list2){
1439 .id_discr = src->id_discr,
1440 .id_list = { src->id },
1441 .id_list_len = 1,
1442 };
1443
1444 switch (src->id_discr) {
1445 case CELL_IDENT_NO_CELL:
1446 case CELL_IDENT_BSS:
1447 dst->id_list_len = 0;
1448 break;
1449 default:
1450 break;
1451 }
1452}
1453
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001454/*! Encode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1455 * \param[out] msg Message Buffer to which IE is to be appended
1456 * \param[in] ci Cell ID to be encoded
1457 * \returns number of bytes appended to \a msg */
1458uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci)
1459{
1460 uint8_t rc;
1461 uint8_t *ie_tag;
1462 struct gsm0808_cell_id_list2 cil = {
1463 .id_discr = ci->id_discr,
1464 .id_list = { ci->id },
1465 .id_list_len = 1,
1466 };
1467
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001468 ie_tag = msg->tail;
1469 rc = gsm0808_enc_cell_id_list2(msg, &cil);
1470
1471 if (rc <= 0)
1472 return rc;
1473
1474 *ie_tag = GSM0808_IE_CELL_IDENTIFIER;
1475 return rc;
1476}
1477
1478/*! Decode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1479 * \param[out] ci Caller-provided memory to store Cell ID.
1480 * \param[in] elem IE value to be decoded.
1481 * \param[in] len Length of \a elem in bytes.
1482 * \returns number of bytes parsed; negative on error */
1483int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len)
1484{
1485 struct gsm0808_cell_id_list2 cil;
1486 int rc;
1487 rc = gsm0808_dec_cell_id_list2(&cil, elem, len);
1488 if (rc < 0)
1489 return rc;
1490 if (cil.id_discr == CELL_IDENT_BSS || cil.id_discr == CELL_IDENT_NO_CELL) {
1491 if (cil.id_list_len != 0)
1492 return -EINVAL;
1493 } else {
1494 if (cil.id_list_len != 1)
1495 return -EINVAL;
1496 }
1497 ci->id_discr = cil.id_discr;
1498 ci->id = cil.id_list[0];
1499 return rc;
1500}
1501
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001502/*! Convert the representation of the permitted speech codec identifier
Philipp Maier3149b0d2017-06-02 13:22:34 +02001503 * that is used in struct gsm0808_channel_type to the speech codec
1504 * representation we use in struct gsm0808_speech_codec.
1505 * \param[in] perm_spch to be converted (see also gsm0808_permitted_speech)
1506 * \returns GSM speech codec type; negative on error */
1507int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch)
1508{
1509 /*! The speech codec type, which is used in the channel type field to
1510 * signal the permitted speech versions (codecs) has a different
1511 * encoding than the type field in the speech codec type element
1512 * (See also 3GPP TS 48.008, 3.2.2.11 and 3.2.2.103) */
1513
1514 switch (perm_spch) {
1515 case GSM0808_PERM_FR1:
1516 return GSM0808_SCT_FR1;
1517 case GSM0808_PERM_FR2:
1518 return GSM0808_SCT_FR2;
1519 case GSM0808_PERM_FR3:
1520 return GSM0808_SCT_FR3;
1521 case GSM0808_PERM_FR4:
1522 return GSM0808_SCT_FR4;
1523 case GSM0808_PERM_FR5:
1524 return GSM0808_SCT_FR5;
1525 case GSM0808_PERM_HR1:
1526 return GSM0808_SCT_HR1;
1527 case GSM0808_PERM_HR3:
1528 return GSM0808_SCT_HR3;
1529 case GSM0808_PERM_HR4:
1530 return GSM0808_SCT_HR4;
1531 case GSM0808_PERM_HR6:
1532 return GSM0808_SCT_HR6;
1533 }
1534
1535 /* Invalid input */
1536 return -EINVAL;
1537}
1538
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001539/*! Extrapolate a speech codec field from a given permitted speech
Philipp Maier884ba0f2017-06-02 13:49:16 +02001540 * parameter (channel type).
1541 * \param[out] sc Caller provided memory to store the resulting speech codec
1542 * \param[in] perm_spch value that is used to derive the speech codec info
1543 * (see also: enum gsm0808_speech_codec_type in gsm0808_utils.h)
1544 * \returns zero when successful; negative on error */
1545int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc,
1546 uint8_t perm_spch)
1547{
1548 int rc;
1549
1550 memset(sc, 0, sizeof(*sc));
1551
1552 /* Determine codec type */
1553 rc = gsm0808_chan_type_to_speech_codec(perm_spch);
1554 if (rc < 0)
1555 return -EINVAL;
1556 sc->type = (uint8_t) rc;
1557
1558 /* Depending on the speech codec type, pick a default codec
1559 * configuration that exactly matches the configuration on the
1560 * air interface. */
1561 switch (sc->type) {
1562 case GSM0808_SCT_FR3:
1563 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR;
1564 break;
1565 case GSM0808_SCT_FR4:
1566 sc->cfg = GSM0808_SC_CFG_DEFAULT_OFR_AMR_WB;
1567 break;
1568 case GSM0808_SCT_FR5:
1569 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR_WB;
1570 break;
1571 case GSM0808_SCT_HR3:
1572 sc->cfg = GSM0808_SC_CFG_DEFAULT_HR_AMR;
1573 break;
1574 case GSM0808_SCT_HR4:
1575 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR_WB;
1576 break;
1577 case GSM0808_SCT_HR6:
1578 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR;
1579 break;
1580 default:
1581 /* Note: Not all codec types specify a default setting,
1582 * in this case, we just set the field to zero. */
1583 sc->cfg = 0;
1584 }
1585
1586 /* Tag all codecs as "Full IP"
1587 * (see als 3GPP TS 48.008 3.2.2.103) */
1588 sc->fi = true;
1589
1590 return 0;
1591}
1592
Philipp Maier5f2eb152018-09-19 13:40:21 +02001593/*! Determine a set of AMR speech codec configuration bits (S0-S15) from a
1594 * given GSM 04.08 AMR configuration struct.
1595 * \param[in] cfg AMR configuration in GSM 04.08 format.
1596 * \param[in] hint if the resulting configuration shall be used with a FR or HR TCH.
1597 * \returns configuration bits (S0-S15) */
Philipp Maier369015c2018-09-21 09:07:20 +02001598uint16_t gsm0808_sc_cfg_from_gsm48_mr_cfg(const struct gsm48_multi_rate_conf *cfg,
Philipp Maier5f2eb152018-09-19 13:40:21 +02001599 bool fr)
1600{
1601 uint16_t s15_s0 = 0;
1602
1603 /* Check each rate bit in the AMR multirate configuration and pick the
1604 * matching default configuration as specified in 3GPP TS 28.062,
1605 * Table 7.11.3.1.3-2. */
1606 if (cfg->m4_75)
1607 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_4_75;
1608 if (cfg->m5_15)
1609 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_15;
1610 if (cfg->m5_90)
1611 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_90;
1612 if (cfg->m6_70)
1613 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_6_70;
1614 if (cfg->m7_40)
1615 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_40;
1616 if (cfg->m7_95)
1617 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_95;
1618 if (cfg->m10_2)
1619 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_10_2;
1620 if (cfg->m12_2)
1621 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_12_2;
1622
1623 /* Note: 3GPP TS 48.008, chapter 3GPP TS 48.008 states that for AMR
1624 * some of the configuration bits must be coded as zeros. The applied
1625 * bitmask matches the default codec settings. See also the definition
1626 * of enum gsm0808_speech_codec_defaults in gsm_08_08.h and
1627 * 3GPP TS 28.062, Table 7.11.3.1.3-2. */
1628 if (fr)
1629 s15_s0 &= GSM0808_SC_CFG_DEFAULT_FR_AMR;
1630 else
1631 s15_s0 &= GSM0808_SC_CFG_DEFAULT_HR_AMR;
1632
Philipp Maier94d79fd2019-03-01 10:40:48 +01001633 /* The mode that is encoded by S1 (Config-NB-Code = 1), takes a special
1634 * role as it does not stand for a single rate, but for up to four rates
1635 * at once (12.2, 7.4, 5.9, 4.75). We must check if the supplied cfg
1636 * covers this mode. If not, we need to make sure that the related
1637 * bit is removed. (See also 3GPP TS 28.062, Table 7.11.3.1.3-2) */
1638 if (!(cfg->m12_2 && cfg->m7_40 && cfg->m5_90 && cfg->m4_75) && fr)
1639 s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20;
1640 else if (!(cfg->m7_40 && cfg->m5_90 && cfg->m4_75))
1641 s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20;
1642
Philipp Maier5f2eb152018-09-19 13:40:21 +02001643 return s15_s0;
1644}
1645
Philipp Maier8515d032018-09-25 15:57:49 +02001646/*! Determine a GSM 04.08 AMR configuration struct from a set of speech codec
1647 * configuration bits (S0-S15)
1648 * \param[out] cfg AMR configuration in GSM 04.08 format.
Philipp Maier3713af82019-02-27 16:48:25 +01001649 * \param[in] s15_s0 configuration bits (S15-S0, non-ambiguous).
1650 * \returns zero when successful; negative on error */
1651int gsm48_mr_cfg_from_gsm0808_sc_cfg(struct gsm48_multi_rate_conf *cfg,
1652 uint16_t s15_s0)
Philipp Maier8515d032018-09-25 15:57:49 +02001653{
Philipp Maier3713af82019-02-27 16:48:25 +01001654 unsigned int count = 0;
1655
1656 /* Note: See also: 3GPP TS 28.062
1657 * Table 7.11.3.1.3-2: Preferred Configurations for the Adaptive
1658 * Multi-Rate Codec Types */
1659
1660 /* Note: The resulting multirate-configuration must not contain an
1661 * active set of more than four codec rates. The active set also
1662 * must contain at least one rate. */
1663
Philipp Maier8515d032018-09-25 15:57:49 +02001664 memset(cfg, 0, sizeof(*cfg));
Philipp Maier3713af82019-02-27 16:48:25 +01001665 cfg->ver = 1;
1666 cfg->icmi = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001667
1668 /* Strip option bits */
1669 s15_s0 &= 0x00ff;
1670
Philipp Maier3713af82019-02-27 16:48:25 +01001671 /* Rate 5,15k can never be selected (see table) */
1672 cfg->m5_15 = 0;
Philipp Maier8515d032018-09-25 15:57:49 +02001673
Neels Hofmeyra0f2b212021-04-14 22:45:13 +02001674 if (s15_s0 & GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20) {
Philipp Maier3713af82019-02-27 16:48:25 +01001675 /* Table Table 7.11.3.1.3-2 lists one mode that selects 4
1676 * rates at once (Config-NB-Code = 1). The rates selected
1677 * are known to be compatible between GERAN and UTRAN, since
1678 * an active set must not contain more than four rates at
1679 * a time, we ignore all other settings as they are either
1680 * redundaned or excess settings (invalid) */
Philipp Maier8515d032018-09-25 15:57:49 +02001681 cfg->m4_75 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001682 cfg->m5_90 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001683 cfg->m7_40 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001684 cfg->m12_2 = 1;
Philipp Maier3713af82019-02-27 16:48:25 +01001685 count += 4;
1686 }
Philipp Maier8515d032018-09-25 15:57:49 +02001687
Philipp Maier3713af82019-02-27 16:48:25 +01001688 /* Check the bits in s15_s0 and set the flags for the
1689 * respective rates. */
1690 if (s15_s0 & GSM0808_SC_CFG_AMR_4_75 && !cfg->m4_75) {
1691 if (count >= 4)
1692 return -EINVAL;
1693 cfg->m4_75 = 1;
1694 count++;
1695 }
1696 if (s15_s0 & GSM0808_SC_CFG_AMR_5_90 && !cfg->m5_90) {
1697 if (count >= 4)
1698 return -EINVAL;
1699 cfg->m5_90 = 1;
1700 count++;
1701 }
1702 if (s15_s0 & GSM0808_SC_CFG_AMR_6_70) {
1703 if (count >= 4)
1704 return -EINVAL;
1705 cfg->m6_70 = 1;
1706 count++;
1707 }
1708 if (s15_s0 & GSM0808_SC_CFG_AMR_7_40 && !cfg->m7_40) {
1709 if (count >= 4)
1710 return -EINVAL;
1711 cfg->m7_40 = 1;
1712 count++;
1713 }
1714 if (s15_s0 & GSM0808_SC_CFG_AMR_7_95) {
1715 if (count >= 4)
1716 return -EINVAL;
1717 cfg->m7_95 = 1;
1718 count++;
1719 }
1720 if (s15_s0 & GSM0808_SC_CFG_AMR_10_2) {
1721 if (count >= 4)
1722 return -EINVAL;
1723 cfg->m10_2 = 1;
1724 count++;
1725 }
1726 if (s15_s0 & GSM0808_SC_CFG_AMR_12_2 && !cfg->m12_2) {
1727 if (count >= 4)
1728 return -EINVAL;
1729 cfg->m12_2 = 1;
1730 count++;
1731 }
1732
1733 if (count == 0)
1734 return -EINVAL;
1735
1736 return 0;
Philipp Maier8515d032018-09-25 15:57:49 +02001737}
1738
Alexander Chemeris2ac8f912020-05-13 22:38:08 +03001739int gsm0808_get_cipher_reject_cause(const struct tlv_parsed *tp)
1740{
1741 return gsm0808_get_cause(tp);
1742}
1743
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001744/*! Print a human readable name of the cell identifier to the char buffer.
1745 * This is useful both for struct gsm0808_cell_id and struct gsm0808_cell_id_list2.
1746 * See also gsm0808_cell_id_name() and gsm0808_cell_id_list_name().
1747 * \param[out] buf Destination buffer to write string representation to.
1748 * \param[in] buflen Amount of memory available in \a buf.
1749 * \param[in] id_discr Cell Identifier type.
1750 * \param[in] u Cell Identifer value.
1751 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1752 * or that would have been written if the buffer were large enough.
1753 */
1754int gsm0808_cell_id_u_name(char *buf, size_t buflen,
1755 enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
1756{
1757 switch (id_discr) {
1758 case CELL_IDENT_LAC:
1759 return snprintf(buf, buflen, "%u", u->lac);
1760 case CELL_IDENT_CI:
1761 return snprintf(buf, buflen, "%u", u->ci);
1762 case CELL_IDENT_LAC_AND_CI:
1763 return snprintf(buf, buflen, "%u-%u", u->lac_and_ci.lac, u->lac_and_ci.ci);
1764 case CELL_IDENT_LAI_AND_LAC:
1765 return snprintf(buf, buflen, "%s", osmo_lai_name(&u->lai_and_lac));
1766 case CELL_IDENT_WHOLE_GLOBAL:
1767 return snprintf(buf, buflen, "%s", osmo_cgi_name(&u->global));
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01001768 case CELL_IDENT_WHOLE_GLOBAL_PS:
1769 return snprintf(buf, buflen, "%s", osmo_cgi_ps_name(&u->global_ps));
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001770 case CELL_IDENT_SAI:
1771 return snprintf(buf, buflen, "%s", osmo_sai_name(&u->sai));
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001772 default:
1773 /* For CELL_IDENT_BSS and CELL_IDENT_NO_CELL, just print the discriminator.
1774 * Same for kinds we have no string representation of yet. */
1775 return snprintf(buf, buflen, "%s", gsm0808_cell_id_discr_name(id_discr));
1776 }
1777}
1778
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02001779/*! Return true if the common information between the two Cell Identifiers match.
1780 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1781 * Note that CELL_IDENT_NO_CELL will always return false.
1782 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1783 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1784 * \param[in] discr1 Cell Identifier type.
1785 * \param[in] u1 Cell Identifier value.
1786 * \param[in] discr2 Other Cell Identifier type.
1787 * \param[in] u2 Other Cell Identifier value.
1788 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1789 * \returns True if the common fields of the above match.
1790 */
1791static bool gsm0808_cell_id_u_match(enum CELL_IDENT discr1, const union gsm0808_cell_id_u *u1,
1792 enum CELL_IDENT discr2, const union gsm0808_cell_id_u *u2,
1793 bool exact_match)
1794{
1795 struct osmo_cell_global_id a = {};
1796 struct osmo_cell_global_id b = {};
1797
1798 if (exact_match && discr1 != discr2)
1799 return false;
1800
1801 /* First handle the odd wildcard like CELL_IDENT kinds. We can't really match any of these. */
1802 switch (discr1) {
1803 case CELL_IDENT_NO_CELL:
1804 case CELL_IDENT_BSS:
1805 return discr1 == discr2;
1806 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1807 case CELL_IDENT_UTRAN_RNC:
1808 case CELL_IDENT_UTRAN_LAC_RNC:
1809 return false;
1810 default:
1811 break;
1812 }
1813 switch (discr2) {
1814 case CELL_IDENT_NO_CELL:
1815 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1816 case CELL_IDENT_UTRAN_RNC:
1817 case CELL_IDENT_UTRAN_LAC_RNC:
1818 case CELL_IDENT_BSS:
1819 return false;
1820 default:
1821 break;
1822 }
1823
1824 /* Enrich both sides to full CGI, then compare those. First set the *other* ID's values in case
1825 * they assign more items. For example:
1826 * u1 = LAC:42
1827 * u2 = LAC+CI:23+5
1828 * 1) a <- LAC+CI:23+5
1829 * 2) a <- LAC:42 so that a = LAC+CI:42+5
1830 * Now we can compare those two and find a mismatch. If the LAC were the same, we would get
1831 * identical LAC+CI and hence a match. */
1832
1833 cell_id_to_cgi(&a, discr2, u2);
1834 cell_id_to_cgi(&a, discr1, u1);
1835
1836 cell_id_to_cgi(&b, discr1, u1);
1837 cell_id_to_cgi(&b, discr2, u2);
1838
1839 return osmo_cgi_cmp(&a, &b) == 0;
1840}
1841
1842/*! Return true if the common information between the two Cell Identifiers match.
1843 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1844 * Note that CELL_IDENT_NO_CELL will always return false.
1845 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1846 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1847 * \param[in] id1 Cell Identifier.
1848 * \param[in] id2 Other Cell Identifier.
1849 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1850 * \returns True if the common fields of the above match.
1851 */
1852bool gsm0808_cell_ids_match(const struct gsm0808_cell_id *id1, const struct gsm0808_cell_id *id2, bool exact_match)
1853{
1854 return gsm0808_cell_id_u_match(id1->id_discr, &id1->id, id2->id_discr, &id2->id, exact_match);
1855}
1856
1857/*! Find an index in a Cell Identifier list that matches a given single Cell Identifer.
1858 * Compare \a id against each entry in \a list using gsm0808_cell_ids_match(), and return the list index
1859 * if a match is found. \a match_nr allows iterating all matches in the list. A match_nr <= 0 returns the
1860 * first match in the list, match_nr == 1 the second match, etc., and if match_nr exceeds the available
1861 * matches in the list, -1 is returned.
1862 * \param[in] id Cell Identifier to match.
1863 * \param[in] list Cell Identifier list to search in.
1864 * \param[in] match_nr Ignore this many matches.
1865 * \param[in] exact_match If true, consider as match only if the CELL_IDENT types and all values are identical.
Neels Hofmeyr8aa691f2019-02-26 02:59:37 +01001866 * \returns -1 if no match is found, list index if a match is found (i.e. rc == 0 means a match was found on the first
1867 * entry).
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02001868 */
1869int gsm0808_cell_id_matches_list(const struct gsm0808_cell_id *id, const struct gsm0808_cell_id_list2 *list,
1870 unsigned int match_nr, bool exact_match)
1871{
1872 int i;
1873 for (i = 0; i < list->id_list_len; i++) {
1874 if (gsm0808_cell_id_u_match(id->id_discr, &id->id, list->id_discr, &list->id_list[i], exact_match)) {
1875 if (match_nr)
1876 match_nr--;
1877 else
1878 return i;
1879 }
1880 }
1881 return -1;
1882}
1883
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001884/*! Copy information from a CGI to form a Cell Identifier of the specified kind.
1885 * \param [out] cid Compose new Cell Identifier here.
1886 * \param [in] id_discr Which kind of Cell Identifier to compose.
1887 * \param [in] cgi Cell Global Identifier to form the Cell Identifier from.
1888 */
1889void gsm0808_cell_id_from_cgi(struct gsm0808_cell_id *cid, enum CELL_IDENT id_discr,
1890 const struct osmo_cell_global_id *cgi)
1891{
1892 *cid = (struct gsm0808_cell_id){
1893 .id_discr = id_discr,
1894 };
1895
1896 switch (id_discr) {
1897 case CELL_IDENT_WHOLE_GLOBAL:
1898 cid->id.global = *cgi;
1899 return;
1900
Pau Espin Pedrol20b763d2021-02-15 16:06:22 +01001901 case CELL_IDENT_WHOLE_GLOBAL_PS:
1902 cid->id.global_ps = (struct osmo_cell_global_id_ps){
1903 .rai = {
1904 .lac = cgi->lai,
1905 },
1906 .cell_identity = cgi->cell_identity,
1907 };
1908 return;
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001909 case CELL_IDENT_LAC_AND_CI:
1910 cid->id.lac_and_ci = (struct osmo_lac_and_ci_id){
1911 .lac = cgi->lai.lac,
1912 .ci = cgi->cell_identity,
1913 };
1914 return;
1915
1916 case CELL_IDENT_CI:
1917 cid->id.ci = cgi->cell_identity;
1918 return;
1919
1920 case CELL_IDENT_LAI:
1921 cid->id.lai_and_lac = cgi->lai;
1922 return;
1923
1924 case CELL_IDENT_LAC:
1925 cid->id.lac = cgi->lai.lac;
1926 return;
1927
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001928 case CELL_IDENT_SAI:
1929 cid->id.sai = (struct osmo_service_area_id){
1930 .lai = cgi->lai,
1931 };
1932 return;
1933
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001934 case CELL_IDENT_NO_CELL:
1935 case CELL_IDENT_BSS:
1936 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1937 case CELL_IDENT_UTRAN_RNC:
1938 case CELL_IDENT_UTRAN_LAC_RNC:
1939 default:
1940 return;
1941 };
1942}
1943
1944/*! Overwrite parts of cgi with values from a Cell Identifier.
1945 * Place only those items given in cid into cgi, leaving other values unchanged.
1946 * \param[out] cgi Cell Global Identity to write to.
1947 * \param[in] cid Cell Identity to read from.
1948 * \return a bitmask of items that were set: OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI; 0 if nothing was
1949 * written to cgi.
1950 */
1951int gsm0808_cell_id_to_cgi(struct osmo_cell_global_id *cgi, const struct gsm0808_cell_id *cid)
1952{
1953 switch (cid->id_discr) {
1954 case CELL_IDENT_WHOLE_GLOBAL:
1955 *cgi = cid->id.global;
1956 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
1957
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01001958 case CELL_IDENT_WHOLE_GLOBAL_PS:
1959 cgi->lai = cid->id.global_ps.rai.lac;
1960 cgi->cell_identity = cid->id.global_ps.cell_identity;
1961 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
1962
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001963 case CELL_IDENT_LAC_AND_CI:
1964 cgi->lai.lac = cid->id.lac_and_ci.lac;
1965 cgi->cell_identity = cid->id.lac_and_ci.ci;
1966 return OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
1967
1968 case CELL_IDENT_CI:
1969 cgi->cell_identity = cid->id.ci;
1970 return OSMO_CGI_PART_CI;
1971
1972 case CELL_IDENT_LAI:
1973 cgi->lai = cid->id.lai_and_lac;
1974 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC;
1975
1976 case CELL_IDENT_LAC:
1977 cgi->lai.lac = cid->id.lac;
1978 return OSMO_CGI_PART_LAC;
1979
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001980 case CELL_IDENT_SAI:
1981 cgi->lai = cid->id.sai.lai;
1982 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC;
1983
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001984 case CELL_IDENT_NO_CELL:
1985 case CELL_IDENT_BSS:
1986 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1987 case CELL_IDENT_UTRAN_RNC:
1988 case CELL_IDENT_UTRAN_LAC_RNC:
1989 default:
1990 return 0;
1991 };
1992}
1993
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001994/*! value_string[] for enum CELL_IDENT. */
1995const struct value_string gsm0808_cell_id_discr_names[] = {
1996 { CELL_IDENT_WHOLE_GLOBAL, "CGI" },
1997 { CELL_IDENT_LAC_AND_CI, "LAC-CI" },
1998 { CELL_IDENT_CI, "CI" },
1999 { CELL_IDENT_NO_CELL, "NO-CELL" },
2000 { CELL_IDENT_LAI_AND_LAC, "LAI" },
2001 { CELL_IDENT_LAC, "LAC" },
2002 { CELL_IDENT_BSS, "BSS" },
2003 { CELL_IDENT_UTRAN_PLMN_LAC_RNC, "UTRAN-PLMN-LAC-RNC" },
2004 { CELL_IDENT_UTRAN_RNC, "UTRAN-RNC" },
2005 { CELL_IDENT_UTRAN_LAC_RNC, "UTRAN-LAC-RNC" },
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01002006 { CELL_IDENT_SAI, "SAI" },
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01002007 { CELL_IDENT_WHOLE_GLOBAL_PS, "CGI-PS"},
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002008 { 0, NULL }
2009};
2010
2011#define APPEND_THING(func, args...) do { \
2012 int remain = buflen - (pos - buf); \
2013 int l = func(pos, remain, ##args); \
2014 if (l < 0 || l > remain) \
2015 pos = buf + buflen; \
2016 else \
2017 pos += l; \
2018 if (l > 0) \
2019 total_len += l; \
2020 } while(0)
2021#define APPEND_STR(fmt, args...) APPEND_THING(snprintf, fmt, ##args)
2022#define APPEND_CELL_ID_U(DISCR, U) APPEND_THING(gsm0808_cell_id_u_name, DISCR, U)
2023
Harald Welte179f3572019-03-18 18:38:47 +01002024char *gsm0808_cell_id_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id *cid)
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002025{
2026 char *pos = buf;
2027 int total_len = 0;
2028 APPEND_STR("%s:", gsm0808_cell_id_discr_name(cid->id_discr));
2029 APPEND_CELL_ID_U(cid->id_discr, &cid->id);
2030 return buf;
2031}
2032
2033/*! Return a human readable representation of a Cell Identifier, like "LAC:123"
2034 * or "CGI:001-01-42-23".
2035 * \param[in] cid Cell Identifer.
2036 * \returns String in a static buffer.
2037 */
2038const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid)
2039{
Harald Welte171ef822019-03-28 10:49:05 +01002040 static __thread char buf[64];
Harald Welte179f3572019-03-18 18:38:47 +01002041 return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002042}
2043
2044/*! Like gsm0808_cell_id_name() but uses a different static buffer.
2045 * \param[in] cid Cell Identifer.
2046 * \returns String in a static buffer.
2047 */
2048const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid)
2049{
Harald Welte171ef822019-03-28 10:49:05 +01002050 static __thread char buf[64];
Harald Welte179f3572019-03-18 18:38:47 +01002051 return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
2052}
2053
2054char *gsm0808_cell_id_name_c(const void *ctx, const struct gsm0808_cell_id *cid)
2055{
2056 char *buf = talloc_size(ctx, 64);
2057 if (!buf)
2058 return NULL;
2059 return gsm0808_cell_id_name_buf(buf, 64, cid);
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002060}
2061
2062/*! Return a human readable representation of the Cell Identifier List, like
2063 * "LAC[2]:{123, 456}".
2064 * The return value semantics are like snprintf() and thus allow ensuring a complete
2065 * untruncated string by determining the required string length from the return value.
2066 * If buflen > 0, always nul-terminate the string in buf, also when it is truncated.
2067 * If buflen == 0, do not modify buf, just return the would-be length.
2068 * \param[out] buf Destination buffer to write string representation to.
2069 * \param[in] buflen Amount of memory available in \a buf.
2070 * \param[in] cil Cell Identifer List.
2071 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
2072 * or that would have been written if the buffer were large enough.
2073 */
2074int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil)
2075{
2076 char *pos = buf;
2077 int total_len = 0;
2078 int i;
2079
2080 APPEND_STR("%s[%u]", gsm0808_cell_id_discr_name(cil->id_discr), cil->id_list_len);
2081
2082 switch (cil->id_discr) {
2083 case CELL_IDENT_BSS:
2084 case CELL_IDENT_NO_CELL:
2085 return total_len;
2086 default:
2087 break;
2088 }
2089
2090 APPEND_STR(":{");
2091
2092 for (i = 0; i < cil->id_list_len; i++) {
2093 if (i)
2094 APPEND_STR(", ");
2095 APPEND_CELL_ID_U(cil->id_discr, &cil->id_list[i]);
2096 }
2097
2098 APPEND_STR("}");
2099 return total_len;
2100}
2101
2102/*! Return a human-readable representation of \a cil in a static buffer.
2103 * If the list is too long, the output may be truncated.
2104 * See also gsm0808_cell_id_list_name_buf(). */
2105const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil)
2106{
Harald Welte171ef822019-03-28 10:49:05 +01002107 static __thread char buf[1024];
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002108 gsm0808_cell_id_list_name_buf(buf, sizeof(buf), cil);
2109 return buf;
2110}
2111
Harald Welte179f3572019-03-18 18:38:47 +01002112char *gsm0808_cell_id_list_name_c(const void *ctx, const struct gsm0808_cell_id_list2 *cil)
2113{
2114 char *buf = talloc_size(ctx, 1024);
2115 if (!buf)
2116 return NULL;
2117 gsm0808_cell_id_list_name_buf(buf, 1024, cil);
2118 return buf;
2119}
2120
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002121#undef APPEND_STR
2122#undef APPEND_CELL_ID_U
2123
Harald Welte4a62eda2019-03-18 18:27:00 +01002124char *gsm0808_channel_type_name_buf(char *buf, size_t buf_len, const struct gsm0808_channel_type *ct)
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02002125{
Harald Welte4a62eda2019-03-18 18:27:00 +01002126 snprintf(buf, buf_len, "ch_indctr=0x%x ch_rate_type=0x%x perm_spch=%s",
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02002127 ct->ch_indctr, ct->ch_rate_type,
2128 osmo_hexdump(ct->perm_spch, ct->perm_spch_len));
2129 return buf;
2130}
2131
Harald Welte4a62eda2019-03-18 18:27:00 +01002132const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct)
2133{
Harald Welte171ef822019-03-28 10:49:05 +01002134 static __thread char buf[128];
Harald Welte4a62eda2019-03-18 18:27:00 +01002135 return gsm0808_channel_type_name_buf(buf, sizeof(buf), ct);
2136}
2137
Harald Welte179f3572019-03-18 18:38:47 +01002138char *gsm0808_channel_type_name_c(const void *ctx, const struct gsm0808_channel_type *ct)
2139{
2140 char *buf = talloc_size(ctx, 128);
2141 if (!buf)
2142 return NULL;
2143 return gsm0808_channel_type_name_buf(buf, 128, ct);
2144}
2145
Harald Welte96e2a002017-06-12 21:44:18 +02002146/*! @} */