blob: 5b7fff2c8a34138f28ff9229bde87e827bdae7ef [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
93 OSMO_ASSERT(msg);
94 OSMO_ASSERT(ss);
95 OSMO_ASSERT(ss->ss_family == AF_INET || ss->ss_family == AF_INET6);
96
Oliver Smithc66b35b2022-08-05 11:27:55 +020097 switch (ss->ss_family) {
98 case AF_INET:
99 len_v += IP_V4_ADDR_LEN;
100 break;
101 case AF_INET6:
102 len_v += IP_V6_ADDR_LEN;
103 break;
104 }
105
106 if (msgb_tailroom(msg) < len_tl + len_v)
107 return 0;
108
Philipp Maier22401432017-03-24 17:59:26 +0100109 msgb_put_u8(msg, GSM0808_IE_AOIP_TRASP_ADDR);
Oliver Smithc66b35b2022-08-05 11:27:55 +0200110 msgb_put_u8(msg, len_v);
Philipp Maier22401432017-03-24 17:59:26 +0100111
112 switch (ss->ss_family) {
113 case AF_INET:
114 sin = (struct sockaddr_in *)ss;
Harald Welte95871da2017-05-15 12:11:36 +0200115 port = osmo_ntohs(sin->sin_port);
Philipp Maier22401432017-03-24 17:59:26 +0100116 ptr = msgb_put(msg, IP_V4_ADDR_LEN);
117 memcpy(ptr, &sin->sin_addr.s_addr, IP_V4_ADDR_LEN);
118 break;
119 case AF_INET6:
120 sin6 = (struct sockaddr_in6 *)ss;
Harald Welte95871da2017-05-15 12:11:36 +0200121 port = osmo_ntohs(sin6->sin6_port);
Philipp Maier22401432017-03-24 17:59:26 +0100122 ptr = msgb_put(msg, IP_V6_ADDR_LEN);
123 memcpy(ptr, sin6->sin6_addr.s6_addr, IP_V6_ADDR_LEN);
124 break;
125 }
126
127 msgb_put_u16(msg, port);
Oliver Smithc66b35b2022-08-05 11:27:55 +0200128 return len_tl + len_v;
Philipp Maier22401432017-03-24 17:59:26 +0100129}
130
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200131/*! Decode TS 08.08 AoIP transport address IE
Harald Welte96e2a002017-06-12 21:44:18 +0200132 * \param[out] ss Caller-provided memory where decoded socket addr is stored
133 * \param[in] elem pointer to IE value
134 * \param[in] len length of \a elem in bytes
135 * \returns number of bytes parsed */
Philipp Maier22401432017-03-24 17:59:26 +0100136int gsm0808_dec_aoip_trasp_addr(struct sockaddr_storage *ss,
137 const uint8_t *elem, uint8_t len)
138{
139 /* See also 3GPP TS 48.008 3.2.2.102 AoIP Transport Layer Address */
140 struct sockaddr_in sin;
141 struct sockaddr_in6 sin6;
142 const uint8_t *old_elem = elem;
143
144 OSMO_ASSERT(ss);
145 if (!elem)
146 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200147 if (len == 0)
Philipp Maier22401432017-03-24 17:59:26 +0100148 return -EINVAL;
149
150 memset(ss, 0, sizeof(*ss));
151
152 switch (len) {
153 case IP_V4_ADDR_LEN + IP_PORT_LEN:
154 memset(&sin, 0, sizeof(sin));
155 sin.sin_family = AF_INET;
156
157 memcpy(&sin.sin_addr.s_addr, elem, IP_V4_ADDR_LEN);
158 elem += IP_V4_ADDR_LEN;
159 sin.sin_port = osmo_load16le(elem);
160 elem += IP_PORT_LEN;
161
162 memcpy(ss, &sin, sizeof(sin));
163 break;
164 case IP_V6_ADDR_LEN + IP_PORT_LEN:
165 memset(&sin6, 0, sizeof(sin6));
166 sin6.sin6_family = AF_INET6;
167
168 memcpy(sin6.sin6_addr.s6_addr, elem, IP_V6_ADDR_LEN);
169 elem += IP_V6_ADDR_LEN;
170 sin6.sin6_port = osmo_load16le(elem);
171 elem += IP_PORT_LEN;
172
173 memcpy(ss, &sin6, sizeof(sin6));
174 break;
175 default:
176 /* Malformed element! */
177 return -EINVAL;
178 break;
179 }
180
181 return (int)(elem - old_elem);
182}
Philipp Maier6f725d62017-03-24 18:03:17 +0100183
Pau Espin Pedrol18506c82019-04-16 15:47:59 +0200184/*! Decode TS 08.08 (Osmocom Extension) Osmux CID
185 * TV with len(V) == 1, and V is the CID to be used.
186 * \param[out] cid Caller-provided variable where CID is stored
187 * \param[in] elem pointer to IE value
188 * \param[in] len length of \a elem in bytes
189 * \returns number of bytes parsed */
190int gsm0808_dec_osmux_cid(uint8_t *cid, const uint8_t *elem, uint8_t len)
191{
192 OSMO_ASSERT(cid);
193 if (!elem)
194 return -EINVAL;
195 if (len != 1)
196 return -EINVAL;
197
198 *cid = *elem;
199
200 return 1;
201}
202
Harald Welte20725b92017-05-15 12:50:04 +0200203#endif /* HAVE_SYS_SOCKET_H */
204
Harald Welteef7be492019-05-03 21:01:13 +0200205/* Decode 5-byte LAI list element data (see TS 08.08 3.2.2.27) into MCC/MNC/LAC. */
206static void decode_lai(const uint8_t *data, struct osmo_location_area_id *decoded)
207{
208 struct gsm48_loc_area_id lai;
209
210 /* Copy data to stack to prevent unaligned access in gsm48_decode_lai2(). */
211 memcpy(&lai, data, sizeof(lai)); /* don't byte swap yet */
212
213 gsm48_decode_lai2(&lai, decoded);
214}
215
Philipp Maier6f725d62017-03-24 18:03:17 +0100216/* Helper function for gsm0808_enc_speech_codec()
217 * and gsm0808_enc_speech_codec_list() */
218static uint8_t enc_speech_codec(struct msgb *msg,
219 const struct gsm0808_speech_codec *sc)
220{
221 /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
222 uint8_t header = 0;
223 uint8_t *old_tail;
Vadim Yanitskiy061b7ec2022-12-14 02:06:06 +0700224 bool type_extended;
Philipp Maierbb839662017-06-01 17:11:19 +0200225
226 /* Note: Extended codec types are codec types that require 8 instead
227 * of 4 bit to fully specify the selected codec. In the following,
228 * we check if we work with an extended type or not. We also check
229 * if the codec type is valid at all. */
Vadim Yanitskiy8e962452022-12-14 01:13:19 +0700230 switch (sc->type) {
Philipp Maierbb839662017-06-01 17:11:19 +0200231 case GSM0808_SCT_FR1:
232 case GSM0808_SCT_FR2:
233 case GSM0808_SCT_FR3:
234 case GSM0808_SCT_FR4:
235 case GSM0808_SCT_FR5:
236 case GSM0808_SCT_HR1:
237 case GSM0808_SCT_HR3:
238 case GSM0808_SCT_HR4:
239 case GSM0808_SCT_HR6:
240 type_extended = false;
241 break;
242 case GSM0808_SCT_CSD:
243 type_extended = true;
244 break;
245 default:
246 /* Invalid codec type specified */
247 OSMO_ASSERT(false);
248 break;
249 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100250
251 old_tail = msg->tail;
252
253 if (sc->fi)
254 header |= (1 << 7);
255 if (sc->pi)
256 header |= (1 << 6);
257 if (sc->pt)
258 header |= (1 << 5);
259 if (sc->tf)
260 header |= (1 << 4);
Philipp Maierbb839662017-06-01 17:11:19 +0200261
262 if (type_extended) {
Philipp Maier6f725d62017-03-24 18:03:17 +0100263 header |= 0x0f;
264 msgb_put_u8(msg, header);
Philipp Maierbb839662017-06-01 17:11:19 +0200265 msgb_put_u8(msg, sc->type);
Philipp Maier6f725d62017-03-24 18:03:17 +0100266 } else {
Philipp Maier6f725d62017-03-24 18:03:17 +0100267 header |= sc->type;
268 msgb_put_u8(msg, header);
Philipp Maier6f725d62017-03-24 18:03:17 +0100269 }
270
Philipp Maierbb839662017-06-01 17:11:19 +0200271 /* Note: Whether a configuration is present or not depends on the
272 * selected codec type. If present, it can either consist of one
273 * or two octets, depending on the codec type */
274 switch (sc->type) {
275 case GSM0808_SCT_FR3:
276 case GSM0808_SCT_HR3:
277 case GSM0808_SCT_HR6:
Philipp Maier7e27b142018-03-22 17:26:46 +0100278 msgb_put_u16(msg, osmo_ntohs(sc->cfg));
Philipp Maierbb839662017-06-01 17:11:19 +0200279 break;
280 case GSM0808_SCT_FR4:
281 case GSM0808_SCT_FR5:
282 case GSM0808_SCT_HR4:
283 case GSM0808_SCT_CSD:
Alexander Couzens4e284b62019-06-23 01:53:43 +0200284 OSMO_ASSERT((sc->cfg & 0xff00) == 0);
Philipp Maierbb839662017-06-01 17:11:19 +0200285 msgb_put_u8(msg, (uint8_t) sc->cfg & 0xff);
286 break;
287 default:
288 OSMO_ASSERT(sc->cfg == 0);
289 break;
290 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100291
292 return (uint8_t) (msg->tail - old_tail);
293}
294
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200295/*! Encode TS 08.08 Speech Codec IE
Harald Welte96e2a002017-06-12 21:44:18 +0200296 * \param[out] msg Message Buffer to which IE will be appended
297 * \param[in] sc Speech Codec to be encoded into IE
298 * \returns number of bytes appended to \a msg */
Philipp Maier6f725d62017-03-24 18:03:17 +0100299uint8_t gsm0808_enc_speech_codec(struct msgb *msg,
300 const struct gsm0808_speech_codec *sc)
301{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200302 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100303 uint8_t *old_tail;
304 uint8_t *tlv_len;
305
306 OSMO_ASSERT(msg);
307 OSMO_ASSERT(sc);
308
309 msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC);
310 tlv_len = msgb_put(msg, 1);
311 old_tail = msg->tail;
312
313 enc_speech_codec(msg, sc);
314
315 *tlv_len = (uint8_t) (msg->tail - old_tail);
316 return *tlv_len + 2;
317}
318
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200319/*! Decode TS 08.08 Speech Codec IE
Harald Welte96e2a002017-06-12 21:44:18 +0200320 * \param[out] sc Caller-allocated memory for Speech Codec
321 * \param[in] elem IE value to be decoded
322 * \param[in] len Length of \a elem in bytes
323 * \returns number of bytes parsed; negative on error */
Philipp Maier6f725d62017-03-24 18:03:17 +0100324int gsm0808_dec_speech_codec(struct gsm0808_speech_codec *sc,
325 const uint8_t *elem, uint8_t len)
326{
327 /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
328 uint8_t header;
329 const uint8_t *old_elem = elem;
330
331 OSMO_ASSERT(sc);
332 if (!elem)
333 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200334 if (len == 0)
Philipp Maier6f725d62017-03-24 18:03:17 +0100335 return -EINVAL;
336
337 memset(sc, 0, sizeof(*sc));
338
339 header = *elem;
340
Philipp Maier85a6af22017-04-28 10:55:05 +0200341 /* An extended codec type needs at least two fields,
342 * bail if the input data length is not sufficient. */
Philipp Maier6f725d62017-03-24 18:03:17 +0100343 if ((header & 0x0F) == 0x0F && len < 2)
344 return -EINVAL;
Philipp Maier6f725d62017-03-24 18:03:17 +0100345
346 elem++;
347 len--;
348
349 if (header & (1 << 7))
350 sc->fi = true;
351 if (header & (1 << 6))
352 sc->pi = true;
353 if (header & (1 << 5))
354 sc->pt = true;
355 if (header & (1 << 4))
356 sc->tf = true;
357
358 if ((header & 0x0F) != 0x0F) {
359 sc->type = (header & 0x0F);
Philipp Maierbb839662017-06-01 17:11:19 +0200360 } else {
361 sc->type = *elem;
362 elem++;
363 len--;
Philipp Maier6f725d62017-03-24 18:03:17 +0100364 }
365
Philipp Maierbb839662017-06-01 17:11:19 +0200366 /* Note: Whether a configuration is present or not depends on the
367 * selected codec type. If present, it can either consist of one or
368 * two octets depending on the codec type */
369 switch (sc->type) {
370 case GSM0808_SCT_FR1:
371 case GSM0808_SCT_FR2:
372 case GSM0808_SCT_HR1:
373 break;
374 case GSM0808_SCT_HR4:
375 case GSM0808_SCT_CSD:
376 case GSM0808_SCT_FR4:
377 case GSM0808_SCT_FR5:
378 if (len < 1)
379 return -EINVAL;
380 sc->cfg = *elem;
381 elem++;
382 break;
383 case GSM0808_SCT_FR3:
384 case GSM0808_SCT_HR3:
385 case GSM0808_SCT_HR6:
386 if (len < 2)
387 return -EINVAL;
Philipp Maier7e27b142018-03-22 17:26:46 +0100388 sc->cfg = osmo_load16le(elem);
Philipp Maierbb839662017-06-01 17:11:19 +0200389 elem += 2;
390 break;
391 default:
392 /* Invalid codec type => malformed speech codec element! */
393 return -EINVAL;
394 break;
395 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100396
397 return (int)(elem - old_elem);
398}
399
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200400/*! Encode TS 08.08 Speech Codec list
Harald Welte96e2a002017-06-12 21:44:18 +0200401 * \param[out] msg Message Buffer to which IE is to be appended
402 * \param[in] scl Speech Codec List to be encoded into IE
403 * \returns number of bytes added to \a msg */
Philipp Maier6f725d62017-03-24 18:03:17 +0100404uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg,
405 const struct gsm0808_speech_codec_list *scl)
406{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200407 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100408 uint8_t *old_tail;
409 uint8_t *tlv_len;
410 unsigned int i;
411 uint8_t rc;
412 unsigned int bytes_used = 0;
413
414 OSMO_ASSERT(msg);
415 OSMO_ASSERT(scl);
416
Philipp Maier6f725d62017-03-24 18:03:17 +0100417 msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC_LIST);
418 tlv_len = msgb_put(msg, 1);
419 old_tail = msg->tail;
420
421 for (i = 0; i < scl->len; i++) {
422 rc = enc_speech_codec(msg, &scl->codec[i]);
423 OSMO_ASSERT(rc >= 1);
424 bytes_used += rc;
425 OSMO_ASSERT(bytes_used <= 255);
426 }
427
428 *tlv_len = (uint8_t) (msg->tail - old_tail);
429 return *tlv_len + 2;
430}
431
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200432/*! Decode TS 08.08 Speech Codec list IE
Harald Welte96e2a002017-06-12 21:44:18 +0200433 * \param[out] scl Caller-provided memory to store codec list
434 * \param[in] elem IE value to be decoded
435 * \param[in] len Length of \a elem in bytes
436 * \returns number of bytes parsed; negative on error */
Philipp Maier6f725d62017-03-24 18:03:17 +0100437int gsm0808_dec_speech_codec_list(struct gsm0808_speech_codec_list *scl,
438 const uint8_t *elem, uint8_t len)
439{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200440 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100441 const uint8_t *old_elem = elem;
442 unsigned int i;
443 int rc;
444 uint8_t decoded = 0;
445
446 OSMO_ASSERT(scl);
447 if (!elem)
448 return -EINVAL;
Philipp Maier6f725d62017-03-24 18:03:17 +0100449
450 memset(scl, 0, sizeof(*scl));
451
452 for (i = 0; i < ARRAY_SIZE(scl->codec); i++) {
453 if (len <= 0)
454 break;
455
456 rc = gsm0808_dec_speech_codec(&scl->codec[i], elem, len);
457 if (rc < 1)
458 return -EINVAL;
459
460 elem+=rc;
461 len -= rc;
462 decoded++;
463 }
464
465 scl->len = decoded;
466
Philipp Maier6f725d62017-03-24 18:03:17 +0100467 return (int)(elem - old_elem);
468}
Philipp Maiere0c65302017-03-28 17:05:40 +0200469
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200470/*! Encode TS 08.08 Channel Type IE
Harald Welte96e2a002017-06-12 21:44:18 +0200471 * \param[out] msg Message Buffer to which IE is to be appended
472 * \param[in] ct Channel Type to be encoded
473 * \returns number of bytes added to \a msg */
Philipp Maiere0c65302017-03-28 17:05:40 +0200474uint8_t gsm0808_enc_channel_type(struct msgb *msg,
475 const struct gsm0808_channel_type *ct)
476{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200477 /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */
Philipp Maiere0c65302017-03-28 17:05:40 +0200478 unsigned int i;
479 uint8_t byte;
480 uint8_t *old_tail;
481 uint8_t *tlv_len;
482
483 OSMO_ASSERT(msg);
484 OSMO_ASSERT(ct);
485 OSMO_ASSERT(ct->perm_spch_len <= CHANNEL_TYPE_ELEMENT_MAXLEN - 2);
486
487 /* FIXME: Implement encoding support for Data
488 * and Speech + CTM Text Telephony */
489 if ((ct->ch_indctr & 0x0f) != GSM0808_CHAN_SPEECH
490 && (ct->ch_indctr & 0x0f) != GSM0808_CHAN_SIGN)
491 OSMO_ASSERT(false);
492
493 msgb_put_u8(msg, GSM0808_IE_CHANNEL_TYPE);
494 tlv_len = msgb_put(msg, 1);
495 old_tail = msg->tail;
496
497 msgb_put_u8(msg, ct->ch_indctr & 0x0f);
498 msgb_put_u8(msg, ct->ch_rate_type);
499
500 for (i = 0; i < ct->perm_spch_len; i++) {
501 byte = ct->perm_spch[i];
502
503 if (i < ct->perm_spch_len - 1)
504 byte |= 0x80;
505 msgb_put_u8(msg, byte);
506 }
507
508 *tlv_len = (uint8_t) (msg->tail - old_tail);
509 return *tlv_len + 2;
510}
511
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200512/*! Decode TS 08.08 Channel Type IE
Harald Welte96e2a002017-06-12 21:44:18 +0200513 * \param[out] ct Caller-provided memory to store channel type
514 * \param[in] elem IE Value to be decoded
515 * \param[in] len Length of \a elem in bytes
516 * \returns number of bytes parsed; negative on error */
Philipp Maiere0c65302017-03-28 17:05:40 +0200517int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct,
518 const uint8_t *elem, uint8_t len)
519{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200520 /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */
Philipp Maiere0c65302017-03-28 17:05:40 +0200521 unsigned int i;
522 uint8_t byte;
523 const uint8_t *old_elem = elem;
524
525 OSMO_ASSERT(ct);
526 if (!elem)
527 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200528 if (len < 3 || len > 11)
Philipp Maiere0c65302017-03-28 17:05:40 +0200529 return -EINVAL;
530
531 memset(ct, 0, sizeof(*ct));
532
533 ct->ch_indctr = (*elem) & 0x0f;
534 elem++;
535 ct->ch_rate_type = (*elem) & 0x0f;
536 elem++;
537
538 for (i = 0; i < ARRAY_SIZE(ct->perm_spch); i++) {
539 byte = *elem;
540 elem++;
541 ct->perm_spch[i] = byte & 0x7f;
542 if ((byte & 0x80) == 0x00)
543 break;
544 }
545 ct->perm_spch_len = i + 1;
546
547 return (int)(elem - old_elem);
548}
Philipp Maier14e76b92017-03-28 18:36:52 +0200549
Max969fb2e2018-12-10 11:01:10 +0100550/*! Create BSSMAP Global Call Reference, 3GPP TS 48.008 §3.2.2.115.
551 * \param[out] msg Message Buffer for appending IE
552 * \param[in] g Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1
553 * \returns number of bytes added to \a msg or 0 on error */
Max47022152018-12-19 18:51:00 +0100554static uint8_t gsm0808_enc_gcr(struct msgb *msg, const struct osmo_gcr_parsed *g)
Max969fb2e2018-12-10 11:01:10 +0100555{
556 uint8_t enc, *len = msgb_tl_put(msg, GSM0808_IE_GLOBAL_CALL_REF);
557
558 enc = osmo_enc_gcr(msg, g);
559 if (!enc)
560 return 0;
561
562 *len = enc;
563 return enc + 2; /* type (1 byte) + length (1 byte) */
564}
565
566/*! Decode BSSMAP Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1.
567 * \param[out] gcr Caller-provided memory to store Global Call Reference
Max036012b2018-12-19 17:48:56 +0100568 * \param[in] tp IE values to be decoded
Max969fb2e2018-12-10 11:01:10 +0100569 * \returns number of bytes parsed; negative on error */
Max47022152018-12-19 18:51:00 +0100570static int gsm0808_dec_gcr(struct osmo_gcr_parsed *gcr, const struct tlv_parsed *tp)
Max969fb2e2018-12-10 11:01:10 +0100571{
572 int ret;
573 const uint8_t *buf = TLVP_VAL_MINLEN(tp, GSM0808_IE_GLOBAL_CALL_REF, OSMO_GCR_MIN_LEN);
574 if (!buf)
575 return -EINVAL;
576
577 ret = osmo_dec_gcr(gcr, buf, TLVP_LEN(tp, GSM0808_IE_GLOBAL_CALL_REF));
578 if (ret < 0)
579 return -ENOENT;
580
581 return 2 + ret;
582}
583
Max47022152018-12-19 18:51:00 +0100584/*! Add LCLS parameters to a given msgb, 3GPP TS 48.008 §3.2.2.115 - 3.2.2.120.
585 * \param[out] msg Message Buffer for appending IE
586 * \param[in] lcls LCLS-related data
587 * \returns number of bytes added to \a msg or 0 on error */
588uint8_t gsm0808_enc_lcls(struct msgb *msg, const struct osmo_lcls *lcls)
589{
590 uint8_t enc = 0;
591
592 /* LCLS: §3.2.2.115 Global Call Reference */
Max3b901252019-01-15 14:15:11 +0100593 if (lcls->gcr_available)
594 enc = gsm0808_enc_gcr(msg, &lcls->gcr);
Max47022152018-12-19 18:51:00 +0100595
596 /* LCLS: §3.2.2.116 Configuration */
597 if (lcls->config != GSM0808_LCLS_CFG_NA) {
598 msgb_tv_put(msg, GSM0808_IE_LCLS_CONFIG, lcls->config);
599 enc += 2;
600 }
601
602 /* LCLS: §3.2.2.117 Connection Status Control */
603 if (lcls->control != GSM0808_LCLS_CSC_NA) {
604 msgb_tv_put(msg, GSM0808_IE_LCLS_CONN_STATUS_CTRL, lcls->control);
605 enc += 2;
606 }
607
608 /* LCLS: §3.2.2.118 Correlation-Not-Needed */
609 if (!lcls->corr_needed) {
610 msgb_v_put(msg, GSM0808_IE_LCLS_CORR_NOT_NEEDED);
611 enc++;
612 }
613
614 return enc;
615}
616
617/*! Decode LCLS parameters to a given msgb, 3GPP TS 48.008 §3.2.2.115 - 3.2.2.120.
618 * \param[out] lcls Caller-provided memory to store LCLS-related data
619 * \param[in] tp IE values to be decoded
620 * \returns GCR size or negative on error */
621int gsm0808_dec_lcls(struct osmo_lcls *lcls, const struct tlv_parsed *tp)
622{
Max3b901252019-01-15 14:15:11 +0100623 int ret = gsm0808_dec_gcr(&lcls->gcr, tp);
Max47022152018-12-19 18:51:00 +0100624
Max3b901252019-01-15 14:15:11 +0100625 lcls->gcr_available = (ret < 0) ? false : true;
Max47022152018-12-19 18:51:00 +0100626 lcls->config = tlvp_val8(tp, GSM0808_IE_LCLS_CONFIG, GSM0808_LCLS_CFG_NA);
627 lcls->control = tlvp_val8(tp, GSM0808_IE_LCLS_CONN_STATUS_CTRL, GSM0808_LCLS_CSC_NA);
628 lcls->corr_needed = TLVP_PRESENT(tp, GSM0808_IE_LCLS_CORR_NOT_NEEDED) ? false : true;
629
630 return ret;
631}
632
Harald Welte171ef822019-03-28 10:49:05 +0100633static __thread char dbuf[256];
Max5ec0cf52019-01-15 16:37:09 +0100634
635/*! Dump LCLS parameters (GCR excluded) into string for printing.
Harald Welte4a62eda2019-03-18 18:27:00 +0100636 * \param[out] buf caller-allocated output string buffer
637 * \param[in] buf_len size of buf in bytes
Max5ec0cf52019-01-15 16:37:09 +0100638 * \param[in] lcls pointer to the struct to print.
639 * \returns string representation of LCLS or NULL on error. */
Harald Welte4a62eda2019-03-18 18:27:00 +0100640char *osmo_lcls_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls)
Max5ec0cf52019-01-15 16:37:09 +0100641{
Harald Welte4a62eda2019-03-18 18:27:00 +0100642 struct osmo_strbuf s = { .buf = buf, .len = buf_len };
Max5ec0cf52019-01-15 16:37:09 +0100643
644 if (!lcls)
645 return NULL;
646
647 OSMO_STRBUF_PRINTF(s, "LCLS Config: %s, Control: %s, Correlation-Needed: %u",
648 gsm0808_lcls_config_name(lcls->config),
649 gsm0808_lcls_control_name(lcls->control),
650 lcls->corr_needed);
651
652 return dbuf;
653}
654
Harald Welte4a62eda2019-03-18 18:27:00 +0100655/*! Dump LCLS parameters (GCR excluded) into static string buffer for printing.
656 * \param[in] lcls pointer to the struct to print.
657 * \returns string representation of LCLS in static buffer or NULL on error. */
658char *osmo_lcls_dump(const struct osmo_lcls *lcls)
659{
660 return osmo_lcls_dump_buf(dbuf, sizeof(dbuf), lcls);
661}
662
Harald Welte179f3572019-03-18 18:38:47 +0100663char *osmo_lcls_dump_c(void *ctx, const struct osmo_lcls *lcls)
664{
665 char *buf = talloc_size(ctx, 256);
666 if (!buf)
667 return NULL;
668 return osmo_lcls_dump_buf(buf, 256, lcls);
669}
670
Max5ec0cf52019-01-15 16:37:09 +0100671/*! Dump GCR struct into string for printing.
Harald Welte4a62eda2019-03-18 18:27:00 +0100672 * \param[out] buf caller-allocated output string buffer
673 * \param[in] buf_len size of buf in bytes
Max5ec0cf52019-01-15 16:37:09 +0100674 * \param[in] lcls pointer to the struct to print.
675 * \returns string representation of GCR or NULL on error. */
Harald Welte4a62eda2019-03-18 18:27:00 +0100676char *osmo_gcr_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls)
Max5ec0cf52019-01-15 16:37:09 +0100677{
Harald Welte4a62eda2019-03-18 18:27:00 +0100678 struct osmo_strbuf s = { .buf = buf, .len = buf_len };
Max5ec0cf52019-01-15 16:37:09 +0100679
680 if (!lcls)
681 return NULL;
682
683 if (lcls->gcr_available) {
684 OSMO_STRBUF_PRINTF(s, "GCR NetID 0x%s, ", osmo_hexdump_nospc(lcls->gcr.net, lcls->gcr.net_len));
685 /* osmo_hexdump() uses static buffers so we can't call it twice withing the same parameter list */
686 OSMO_STRBUF_PRINTF(s, "Node 0x%x, CallRefID 0x%s", lcls->gcr.node, osmo_hexdump_nospc(lcls->gcr.cr, 5));
687 }
688
689 return dbuf;
690}
691
Harald Welte4a62eda2019-03-18 18:27:00 +0100692/*! Dump GCR struct into static string buffer for printing.
693 * \param[in] lcls pointer to the struct to print.
694 * \returns string representation of GCR in static buffer or NULL on error. */
695char *osmo_gcr_dump(const struct osmo_lcls *lcls)
696{
697 return osmo_gcr_dump_buf(dbuf, sizeof(dbuf), lcls);
698}
699
700
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200701/*! Encode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200702 * \param[out] msg Message Buffer to which IE is to be appended
703 * \param[in] ei Encryption Information to be encoded
704 * \returns number of bytes appended to \a msg */
Philipp Maier14e76b92017-03-28 18:36:52 +0200705uint8_t gsm0808_enc_encrypt_info(struct msgb *msg,
706 const struct gsm0808_encrypt_info *ei)
707{
708 unsigned int i;
709 uint8_t perm_algo = 0;
710 uint8_t *ptr;
711 uint8_t *old_tail;
712 uint8_t *tlv_len;
713
714 OSMO_ASSERT(msg);
715 OSMO_ASSERT(ei);
716 OSMO_ASSERT(ei->key_len <= ARRAY_SIZE(ei->key));
717 OSMO_ASSERT(ei->perm_algo_len <= ENCRY_INFO_PERM_ALGO_MAXLEN);
718
719 msgb_put_u8(msg, GSM0808_IE_ENCRYPTION_INFORMATION);
720 tlv_len = msgb_put(msg, 1);
721 old_tail = msg->tail;
722
723 for (i = 0; i < ei->perm_algo_len; i++) {
724 /* Note: gsm_08_08.h defines the permitted algorithms
725 * as an enum which ranges from 0x01 to 0x08 */
726 OSMO_ASSERT(ei->perm_algo[i] != 0);
727 OSMO_ASSERT(ei->perm_algo[i] <= ENCRY_INFO_PERM_ALGO_MAXLEN);
728 perm_algo |= (1 << (ei->perm_algo[i] - 1));
729 }
730
731 msgb_put_u8(msg, perm_algo);
Neels Hofmeyr26e53b12021-06-10 00:47:03 +0200732 /* FIXME: 48.008 3.2.2.10 Encryption Information says:
733 * "When present, the key shall be 8 octets long." */
Philipp Maier14e76b92017-03-28 18:36:52 +0200734 ptr = msgb_put(msg, ei->key_len);
735 memcpy(ptr, ei->key, ei->key_len);
736
737 *tlv_len = (uint8_t) (msg->tail - old_tail);
738 return *tlv_len + 2;
739}
740
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200741/*! Decode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200742 * \param[out] ei Caller-provided memory to store encryption information
743 * \param[in] elem IE value to be decoded
744 * \param[in] len Length of \a elem in bytes
745 * \returns number of bytes parsed; negative on error */
Philipp Maier14e76b92017-03-28 18:36:52 +0200746int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei,
747 const uint8_t *elem, uint8_t len)
748{
749 uint8_t perm_algo;
750 unsigned int i;
751 unsigned int perm_algo_len = 0;
752 const uint8_t *old_elem = elem;
753
754 OSMO_ASSERT(ei);
755 if (!elem)
756 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200757 if (len == 0)
Philipp Maier14e76b92017-03-28 18:36:52 +0200758 return -EINVAL;
759
760 memset(ei, 0, sizeof(*ei));
761
762 perm_algo = *elem;
763 elem++;
764
765 for (i = 0; i < ENCRY_INFO_PERM_ALGO_MAXLEN; i++) {
766 if (perm_algo & (1 << i)) {
767 ei->perm_algo[perm_algo_len] = i + 1;
768 perm_algo_len++;
769 }
770 }
771 ei->perm_algo_len = perm_algo_len;
772
Neels Hofmeyr26e53b12021-06-10 00:47:03 +0200773 /* FIXME: 48.008 3.2.2.10 Encryption Information says:
774 * "When present, the key shall be 8 octets long." */
Philipp Maier14e76b92017-03-28 18:36:52 +0200775 ei->key_len = len - 1;
776 memcpy(ei->key, elem, ei->key_len);
777 elem+=ei->key_len;
778
779 return (int)(elem - old_elem);
780}
Philipp Maier783047e2017-03-29 11:35:50 +0200781
Neels Hofmeyr4a9756c2021-06-10 00:48:15 +0200782/*! Encode TS 48.008 Kc128 IE.
783 * \param[out] msg Message Buffer to which IE is to be appended.
784 * \param[in] kc128 Pointer to 16 bytes of Kc128 key data.
785 * \returns number of bytes appended to msg */
786int gsm0808_enc_kc128(struct msgb *msg, const uint8_t *kc128)
787{
788 uint8_t *start = msg->tail;
789 msgb_tv_fixed_put(msg, GSM0808_IE_KC_128, 16, kc128);
790 return msg->tail - start;
791}
792
793/*! Decode TS 48.008 Kc128 IE.
794 * \param[out] kc128 Target buffer for received Kc128 key, 16 bytes long.
795 * \param[in] elem IE value to be decoded (without IE discriminator).
796 * \param[in] len Length of elem in bytes.
797 * \returns number of bytes parsed; negative on error */
798int gsm0808_dec_kc128(uint8_t *kc128, const uint8_t *elem, uint8_t len)
799{
800 if (len != 16)
801 return -EINVAL;
802 memcpy(kc128, elem, 16);
803 return len;
804}
805
Pau Espin Pedrol85a0f112021-02-15 16:25:33 +0100806/* Store individual Cell Identifier information in a CGI, without clearing the remaining ones.
807 * This is useful to supplement one CGI with information from more than one Cell Identifier,
808 * which in turn is useful to match Cell Identifiers of differing kinds to each other.
809 * Before first invocation, clear the *dst struct externally, this function does only write those members
810 * that are present in parameter u.
811 */
812static void cell_id_to_cgi(struct osmo_cell_global_id *dst,
813 enum CELL_IDENT discr, const union gsm0808_cell_id_u *u)
814{
815 switch (discr) {
816 case CELL_IDENT_WHOLE_GLOBAL:
817 *dst = u->global;
818 return;
819
820 case CELL_IDENT_WHOLE_GLOBAL_PS:
821 dst->lai = u->global_ps.rai.lac;
822 dst->cell_identity = u->global_ps.cell_identity;
823 return;
824
825 case CELL_IDENT_LAC_AND_CI:
826 dst->lai.lac = u->lac_and_ci.lac;
827 dst->cell_identity = u->lac_and_ci.ci;
828 return;
829
830 case CELL_IDENT_CI:
831 dst->cell_identity = u->ci;
832 return;
833
834 case CELL_IDENT_LAI_AND_LAC:
835 dst->lai = u->lai_and_lac;
836 return;
837
838 case CELL_IDENT_LAC:
839 dst->lai.lac = u->lac;
840 return;
841
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +0100842 case CELL_IDENT_SAI:
843 dst->lai = u->sai.lai;
844 return;
845
Pau Espin Pedrol85a0f112021-02-15 16:25:33 +0100846 case CELL_IDENT_NO_CELL:
847 case CELL_IDENT_BSS:
848 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
849 case CELL_IDENT_UTRAN_RNC:
850 case CELL_IDENT_UTRAN_LAC_RNC:
851 /* No values to set. */
852 return;
853 }
854}
855
Harald Weltef2210032019-08-31 21:25:05 +0200856/* Return the size of the value part of a cell identifier of given type */
857int gsm0808_cell_id_size(enum CELL_IDENT discr)
858{
859 switch (discr) {
860 case CELL_IDENT_WHOLE_GLOBAL:
861 return 7;
862 case CELL_IDENT_LAC_AND_CI:
863 return 4;
864 case CELL_IDENT_CI:
865 return 2;
866 case CELL_IDENT_LAI_AND_LAC:
867 return 5;
868 case CELL_IDENT_LAC:
869 return 2;
870 case CELL_IDENT_BSS:
871 case CELL_IDENT_NO_CELL:
872 return 0;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +0100873 case CELL_IDENT_SAI:
874 return 7;
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100875 case CELL_IDENT_WHOLE_GLOBAL_PS:
876 return 8;
Harald Weltef2210032019-08-31 21:25:05 +0200877 default:
878 return -EINVAL;
879 }
880}
Harald Welteef7be492019-05-03 21:01:13 +0200881/*! Decode a single GSM 08.08 Cell ID list element payload
882 * \param[out] out caller-provided output union
883 * \param[in] discr Cell ID discriminator describing type to be decoded
884 * \param[in] buf User-provided input buffer containing binary Cell ID list element
885 * \param[in] len Length of buf
886 * \returns 0 on success; negative on error */
887int gsm0808_decode_cell_id_u(union gsm0808_cell_id_u *out, enum CELL_IDENT discr, const uint8_t *buf, unsigned int len)
888{
889 switch (discr) {
890 case CELL_IDENT_WHOLE_GLOBAL:
891 if (len < 7)
892 return -EINVAL;
893 decode_lai(buf, &out->global.lai);
894 out->global.cell_identity = osmo_load16be(buf + sizeof(struct gsm48_loc_area_id));
895 break;
896 case CELL_IDENT_LAC_AND_CI:
897 if (len < 4)
898 return -EINVAL;
899 out->lac_and_ci.lac = osmo_load16be(buf);
900 out->lac_and_ci.ci = osmo_load16be(buf + sizeof(uint16_t));
901 break;
902 case CELL_IDENT_CI:
903 if (len < 2)
904 return -EINVAL;
905 out->ci = osmo_load16be(buf);
906 break;
907 case CELL_IDENT_LAI_AND_LAC:
908 if (len < 5)
909 return -EINVAL;
910 decode_lai(buf, &out->lai_and_lac);
911 break;
912 case CELL_IDENT_LAC:
913 if (len < 2)
914 return -EINVAL;
915 out->lac = osmo_load16be(buf);
916 break;
917 case CELL_IDENT_BSS:
918 case CELL_IDENT_NO_CELL:
919 /* Does not have any list items */
920 break;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +0100921 case CELL_IDENT_SAI:
922 if (len < 7)
923 return -EINVAL;
924 decode_lai(buf, &out->sai.lai);
925 out->sai.sac = osmo_load16be(buf + sizeof(struct gsm48_loc_area_id));
926 break;
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100927 case CELL_IDENT_WHOLE_GLOBAL_PS:
Pau Espin Pedrol52489852021-02-15 16:26:37 +0100928 /* 3GPP TS 48.018 sec 11.3.9 "Cell Identifier" */
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100929 if (len < 8)
930 return -EINVAL;
931 decode_lai(buf, (struct osmo_location_area_id *)&out->global_ps.rai); /* rai contains lai + non-decoded rac */
932 out->global_ps.rai.rac = *(buf + sizeof(struct gsm48_loc_area_id));
933 out->global_ps.cell_identity = osmo_load16be(buf + sizeof(struct gsm48_loc_area_id) + 1);
934 break;
Harald Welteef7be492019-05-03 21:01:13 +0200935 default:
936 /* Remaining cell identification types are not implemented. */
937 return -EINVAL;
938 }
939 return 0;
940}
941
Harald Weltee87693c2019-05-03 20:06:50 +0200942void gsm0808_msgb_put_cell_id_u(struct msgb *msg, enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
943{
944 switch (id_discr) {
945 case CELL_IDENT_WHOLE_GLOBAL: {
946 const struct osmo_cell_global_id *id = &u->global;
947 struct gsm48_loc_area_id lai;
948 gsm48_generate_lai2(&lai, &id->lai);
949 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
950 msgb_put_u16(msg, id->cell_identity);
951 break;
952 }
953 case CELL_IDENT_LAC_AND_CI: {
954 const struct osmo_lac_and_ci_id *id = &u->lac_and_ci;
955 msgb_put_u16(msg, id->lac);
956 msgb_put_u16(msg, id->ci);
957 break;
958 }
959 case CELL_IDENT_CI:
960 msgb_put_u16(msg, u->ci);
961 break;
962 case CELL_IDENT_LAI_AND_LAC: {
963 const struct osmo_location_area_id *id = &u->lai_and_lac;
964 struct gsm48_loc_area_id lai;
965 gsm48_generate_lai2(&lai, id);
966 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
967 break;
968 }
969 case CELL_IDENT_LAC:
970 msgb_put_u16(msg, u->lac);
971 break;
972 case CELL_IDENT_BSS:
973 case CELL_IDENT_NO_CELL:
974 /* Does not have any list items */
975 break;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +0100976
977 case CELL_IDENT_SAI: {
978 const struct osmo_service_area_id *id = &u->sai;
979 struct gsm48_loc_area_id lai;
980 gsm48_generate_lai2(&lai, &id->lai);
981 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
982 msgb_put_u16(msg, id->sac);
983 break;
984 }
985
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100986 case CELL_IDENT_WHOLE_GLOBAL_PS: {
Pau Espin Pedrol52489852021-02-15 16:26:37 +0100987 /* 3GPP TS 48.018 sec 11.3.9 "Cell Identifier" */
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100988 const struct osmo_cell_global_id_ps *id = &u->global_ps;
989 struct gsm48_loc_area_id lai;
990 gsm48_generate_lai2(&lai, &id->rai.lac);
991 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
992 memcpy(msgb_put(msg, 1), &id->rai.rac, 1);
993 msgb_put_u16(msg, id->cell_identity);
994 break;
995 }
Harald Weltee87693c2019-05-03 20:06:50 +0200996 default:
997 /* Support for other identifier list types is not implemented. */
998 OSMO_ASSERT(false);
999 }
1000}
1001
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001002/*! Encode TS 08.08 Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +02001003 * \param[out] msg Message Buffer to which IE is to be appended
1004 * \param[in] cil Cell ID List to be encoded
1005 * \returns number of bytes appended to \a msg */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001006uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg,
1007 const struct gsm0808_cell_id_list2 *cil)
1008{
1009 uint8_t *old_tail;
1010 uint8_t *tlv_len;
1011 unsigned int i;
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001012 uint8_t id_discr;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001013
1014 OSMO_ASSERT(msg);
1015 OSMO_ASSERT(cil);
1016
1017 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
1018 tlv_len = msgb_put(msg, 1);
1019 old_tail = msg->tail;
1020
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001021 /* CGI-PS is an osmocom-specific type. In here we don't care about the
1022 * PS part, only the CS one. */
1023 if (cil->id_discr == CELL_IDENT_WHOLE_GLOBAL_PS)
1024 id_discr = CELL_IDENT_WHOLE_GLOBAL;
1025 else
1026 id_discr = cil->id_discr & 0x0f;
1027
1028 msgb_put_u8(msg, id_discr);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001029
Alexander Couzens4e284b62019-06-23 01:53:43 +02001030 OSMO_ASSERT(cil->id_list_len <= GSM0808_CELL_ID_LIST2_MAXLEN);
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001031 for (i = 0; i < cil->id_list_len; i++) {
1032 if (cil->id_discr == CELL_IDENT_WHOLE_GLOBAL_PS) {
1033 union gsm0808_cell_id_u u;
1034 cell_id_to_cgi(&u.global, cil->id_discr, &cil->id_list[i]);
1035 gsm0808_msgb_put_cell_id_u(msg, CELL_IDENT_WHOLE_GLOBAL, &u);
1036 } else {
1037 gsm0808_msgb_put_cell_id_u(msg, cil->id_discr, &cil->id_list[i]);
1038 }
1039 }
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001040
1041 *tlv_len = (uint8_t) (msg->tail - old_tail);
1042 return *tlv_len + 2;
1043}
1044
1045/*! DEPRECATED: Use gsm0808_enc_cell_id_list2 instead.
1046 *
1047 * Encode TS 08.08 Cell Identifier List IE
1048 * \param[out] msg Message Buffer to which IE is to be appended
1049 * \param[in] cil Cell ID List to be encoded
1050 * \returns number of bytes appended to \a msg */
Philipp Maier783047e2017-03-29 11:35:50 +02001051uint8_t gsm0808_enc_cell_id_list(struct msgb *msg,
1052 const struct gsm0808_cell_id_list *cil)
1053{
1054 uint8_t *old_tail;
1055 uint8_t *tlv_len;
1056 unsigned int i;
1057
1058 OSMO_ASSERT(msg);
1059 OSMO_ASSERT(cil);
1060
1061 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
1062 tlv_len = msgb_put(msg, 1);
1063 old_tail = msg->tail;
1064
1065 msgb_put_u8(msg, cil->id_discr & 0x0f);
1066
1067 switch (cil->id_discr) {
1068 case CELL_IDENT_LAC:
Alexander Couzens4e284b62019-06-23 01:53:43 +02001069 OSMO_ASSERT(cil->id_list_len <= CELL_ID_LIST_LAC_MAXLEN);
Philipp Maier783047e2017-03-29 11:35:50 +02001070 for (i=0;i<cil->id_list_len;i++) {
1071 msgb_put_u16(msg, cil->id_list_lac[i]);
1072 }
1073 break;
1074 case CELL_IDENT_BSS:
1075 /* Does not have any list items */
1076 break;
1077 default:
1078 /* FIXME: Implement support for all identifier list elements */
1079 OSMO_ASSERT(false);
1080 }
1081
1082 *tlv_len = (uint8_t) (msg->tail - old_tail);
1083 return *tlv_len + 2;
1084}
1085
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001086static 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 +01001087 size_t *consumed)
1088{
1089 struct osmo_cell_global_id *id;
1090 uint16_t *ci_be;
1091 size_t lai_offset;
1092 int i = 0;
1093 const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*ci_be);
1094
1095 *consumed = 0;
1096 while (remain >= elemlen) {
1097 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1098 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001099 id = &cil->id_list[i].global;
Stefan Sperling2873bf12018-03-14 18:38:41 +01001100 lai_offset = i * elemlen;
Stefan Sperling23381452018-03-15 19:38:15 +01001101 decode_lai(&data[lai_offset], &id->lai);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001102 ci_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]);
1103 id->cell_identity = osmo_load16be(ci_be);
1104 *consumed += elemlen;
1105 remain -= elemlen;
1106 i++;
1107 }
1108
1109 return i;
1110}
1111
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001112static 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 +01001113 size_t *consumed)
1114{
1115 uint16_t *lacp_be, *ci_be;
1116 struct osmo_lac_and_ci_id *id;
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001117 int i = 0, j = 0;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001118 const size_t elemlen = sizeof(*lacp_be) + sizeof(*ci_be);
1119
1120 *consumed = 0;
1121
1122 if (remain < elemlen)
1123 return -EINVAL;
1124
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001125 lacp_be = (uint16_t *)(&data[j]);
1126 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001127 while (remain >= elemlen) {
1128 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1129 return -ENOSPC;
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001130 id = &cil->id_list[i++].lac_and_ci;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001131 id->lac = osmo_load16be(lacp_be);
1132 id->ci = osmo_load16be(ci_be);
1133 *consumed += elemlen;
1134 remain -= elemlen;
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001135 j += elemlen;
1136 lacp_be = (uint16_t *)(&data[j]);
1137 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001138 }
1139
1140 return i;
1141}
1142
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001143static int parse_cell_id_ci_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain,
1144 size_t *consumed)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001145{
1146 const uint16_t *ci_be = (const uint16_t *)data;
1147 int i = 0;
1148 const size_t elemlen = sizeof(*ci_be);
1149
1150 *consumed = 0;
1151 while (remain >= elemlen) {
1152 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1153 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001154 cil->id_list[i++].ci = osmo_load16be(ci_be++);
Stefan Sperling9c62fc62018-03-16 10:23:34 +01001155 *consumed += elemlen;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001156 remain -= elemlen;
1157 }
1158 return i;
1159}
1160
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001161static 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 +01001162 size_t *consumed)
1163{
1164 struct osmo_location_area_id *id;
1165 int i = 0;
1166 const size_t elemlen = sizeof(struct gsm48_loc_area_id);
1167
1168 *consumed = 0;
1169 while (remain >= elemlen) {
1170 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1171 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001172 id = &cil->id_list[i].lai_and_lac;
Stefan Sperling23381452018-03-15 19:38:15 +01001173 decode_lai(&data[i * elemlen], id);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001174 *consumed += elemlen;
1175 remain -= elemlen;
1176 i++;
1177 }
1178
1179 return i;
1180}
1181
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001182static 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 +01001183{
1184 const uint16_t *lac_be = (const uint16_t *)data;
1185 int i = 0;
1186 const size_t elemlen = sizeof(*lac_be);
1187
1188 *consumed = 0;
1189 while (remain >= elemlen) {
1190 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1191 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001192 cil->id_list[i++].lac = osmo_load16be(lac_be++);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001193 *consumed += elemlen;
1194 remain -= elemlen;
1195 }
1196 return i;
1197}
1198
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001199static int parse_cell_id_sai_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain, size_t *consumed)
1200{
1201 struct osmo_service_area_id *id;
1202 uint16_t *sac_be;
1203 size_t lai_offset;
1204 int i = 0;
1205 const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*sac_be);
1206
1207 *consumed = 0;
1208 while (remain >= elemlen) {
1209 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1210 return -ENOSPC;
1211 id = &cil->id_list[i].sai;
1212 lai_offset = i * elemlen;
1213 decode_lai(&data[lai_offset], &id->lai);
1214 sac_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]);
1215 id->sac = osmo_load16be(sac_be);
1216 *consumed += elemlen;
1217 remain -= elemlen;
1218 i++;
1219 }
1220
1221 return i;
1222}
1223
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001224/*! Decode Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +02001225 * \param[out] cil Caller-provided memory to store Cell ID list
1226 * \param[in] elem IE value to be decoded
1227 * \param[in] len Length of \a elem in bytes
1228 * \returns number of bytes parsed; negative on error */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001229int gsm0808_dec_cell_id_list2(struct gsm0808_cell_id_list2 *cil,
1230 const uint8_t *elem, uint8_t len)
1231{
1232 uint8_t id_discr;
1233 size_t bytes_elem = 0;
1234 int list_len = 0;
1235
1236 OSMO_ASSERT(cil);
1237 if (!elem)
1238 return -EINVAL;
1239 if (len == 0)
1240 return -EINVAL;
1241
1242 memset(cil, 0, sizeof(*cil));
1243
1244 id_discr = *elem & 0x0f;
1245 elem++;
1246 len--;
1247
1248 switch (id_discr) {
1249 case CELL_IDENT_WHOLE_GLOBAL:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001250 list_len = parse_cell_id_global_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001251 break;
1252 case CELL_IDENT_LAC_AND_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001253 list_len = parse_cell_id_lac_and_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001254 break;
1255 case CELL_IDENT_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001256 list_len = parse_cell_id_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001257 break;
1258 case CELL_IDENT_LAI_AND_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001259 list_len = parse_cell_id_lai_and_lac(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001260 break;
1261 case CELL_IDENT_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001262 list_len = parse_cell_id_lac_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001263 break;
1264 case CELL_IDENT_BSS:
1265 case CELL_IDENT_NO_CELL:
1266 /* Does not have any list items */
1267 break;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001268 case CELL_IDENT_SAI:
1269 list_len = parse_cell_id_sai_list(cil, elem, len, &bytes_elem);
1270 break;
1271 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1272 case CELL_IDENT_UTRAN_RNC:
1273 case CELL_IDENT_UTRAN_LAC_RNC:
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001274 default:
1275 /* Remaining cell identification types are not implemented. */
1276 return -EINVAL;
1277 }
1278
1279 if (list_len < 0) /* parsing error */
1280 return list_len;
1281
1282 cil->id_discr = id_discr;
1283 cil->id_list_len = list_len;
1284
1285 /* One byte for the cell ID discriminator + any remaining bytes in
1286 * the IE which were consumed by the parser functions above. */
1287 return 1 + (int)bytes_elem;
1288}
1289
1290/*! DEPRECATED: Use gsm0808_dec_cell_id_list2 instead.
1291 *
1292 * Decode Cell Identifier List IE
1293 * \param[out] cil Caller-provided memory to store Cell ID list
1294 * \param[in] elem IE value to be decoded
1295 * \param[in] len Length of \a elem in bytes
1296 * \returns number of bytes parsed; negative on error */
Philipp Maier783047e2017-03-29 11:35:50 +02001297int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil,
1298 const uint8_t *elem, uint8_t len)
1299{
1300 uint8_t id_discr;
1301 const uint8_t *old_elem = elem;
1302 unsigned int item_count = 0;
1303
1304 OSMO_ASSERT(cil);
1305 if (!elem)
1306 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +02001307 if (len == 0)
Philipp Maier783047e2017-03-29 11:35:50 +02001308 return -EINVAL;
1309
1310 memset(cil, 0, sizeof(*cil));
1311
1312 id_discr = *elem & 0x0f;
1313 elem++;
1314 len--;
1315
1316 cil->id_discr = id_discr;
1317
1318 switch (id_discr) {
1319 case CELL_IDENT_LAC:
1320 while (len >= 2) {
1321 cil->id_list_lac[item_count] = osmo_load16be(elem);
1322 elem += 2;
1323 item_count++;
1324 len -= 2;
1325 }
1326 case CELL_IDENT_BSS:
1327 /* Does not have any list items */
1328 break;
1329 default:
1330 /* FIXME: Implement support for all identifier list elements */
1331 return -EINVAL;
1332 }
1333
1334 cil->id_list_len = item_count;
1335 return (int)(elem - old_elem);
1336}
Harald Welte96e2a002017-06-12 21:44:18 +02001337
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001338static bool same_cell_id_list_entries(const struct gsm0808_cell_id_list2 *a, int ai,
1339 const struct gsm0808_cell_id_list2 *b, int bi)
1340{
1341 struct gsm0808_cell_id_list2 tmp = {
1342 .id_discr = a->id_discr,
1343 .id_list_len = 1,
1344 };
1345 uint8_t buf_a[32 + sizeof(struct msgb)];
1346 uint8_t buf_b[32 + sizeof(struct msgb)];
1347 struct msgb *msg_a = (void*)buf_a;
1348 struct msgb *msg_b = (void*)buf_b;
1349
1350 msg_a->data_len = 32;
1351 msg_b->data_len = 32;
1352 msgb_reset(msg_a);
1353 msgb_reset(msg_b);
1354
1355 if (a->id_discr != b->id_discr)
1356 return false;
1357 if (ai >= a->id_list_len
1358 || bi >= b->id_list_len)
1359 return false;
1360
1361 tmp.id_list[0] = a->id_list[ai];
1362 gsm0808_enc_cell_id_list2(msg_a, &tmp);
1363
1364 tmp.id_list[0] = b->id_list[bi];
1365 gsm0808_enc_cell_id_list2(msg_b, &tmp);
1366
1367 if (msg_a->len != msg_b->len)
1368 return false;
1369 if (memcmp(msg_a->data, msg_b->data, msg_a->len))
1370 return false;
1371
1372 return true;
1373}
1374
1375/*! Append entries from one Cell Identifier List to another.
1376 * The cell identifier types must be identical between the two lists.
1377 * \param dst[out] Append entries to this list.
1378 * \param src[in] Append these entries to \a dst.
1379 * \returns the nr of items added, or negative on error: -EINVAL if the id_discr mismatch
1380 * between the lists, -ENOSPC if the destination list does not have enough space. If an error is
1381 * returned, \a dst may have already been changed (particularly on -ENOSPC). Note that a return value
1382 * of zero may occur when the src->id_list_len is zero, or when all entries from \a src already exist
1383 * in \a dst, and does not indicate error per se. */
1384int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src)
1385{
1386 int i, j;
1387 int added = 0;
1388
1389 if (dst->id_list_len == 0
1390 && dst->id_discr != CELL_IDENT_BSS)
1391 dst->id_discr = src->id_discr;
1392 else if (dst->id_discr != src->id_discr)
1393 return -EINVAL;
1394
1395 for (i = 0; i < src->id_list_len; i++) {
1396 /* don't add duplicate entries */
1397 bool skip = false;
1398 for (j = 0; j < dst->id_list_len; j++) {
1399 if (same_cell_id_list_entries(dst, j, src, i)) {
1400 skip = true;
1401 break;
1402 }
1403 }
1404 if (skip)
1405 continue;
1406
1407 if (dst->id_list_len >= ARRAY_SIZE(dst->id_list))
1408 return -ENOSPC;
1409
1410 dst->id_list[dst->id_list_len++] = src->id_list[i];
1411 added ++;
1412 }
1413
1414 return added;
1415}
1416
Neels Hofmeyr38e58412018-05-25 16:56:35 +02001417/*! Convert a single Cell Identifier to a Cell Identifier List with one entry.
1418 * \param dst[out] Overwrite this list.
1419 * \param src[in] Set \a dst to contain exactly this item.
1420 */
1421void gsm0808_cell_id_to_list(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id *src)
1422{
1423 if (!dst)
1424 return;
1425 if (!src) {
1426 *dst = (struct gsm0808_cell_id_list2){
1427 .id_discr = CELL_IDENT_NO_CELL,
1428 };
1429 return;
1430 }
1431
1432 *dst = (struct gsm0808_cell_id_list2){
1433 .id_discr = src->id_discr,
1434 .id_list = { src->id },
1435 .id_list_len = 1,
1436 };
1437
1438 switch (src->id_discr) {
1439 case CELL_IDENT_NO_CELL:
1440 case CELL_IDENT_BSS:
1441 dst->id_list_len = 0;
1442 break;
1443 default:
1444 break;
1445 }
1446}
1447
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001448/*! Encode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1449 * \param[out] msg Message Buffer to which IE is to be appended
1450 * \param[in] ci Cell ID to be encoded
1451 * \returns number of bytes appended to \a msg */
1452uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci)
1453{
1454 uint8_t rc;
1455 uint8_t *ie_tag;
1456 struct gsm0808_cell_id_list2 cil = {
1457 .id_discr = ci->id_discr,
1458 .id_list = { ci->id },
1459 .id_list_len = 1,
1460 };
1461
1462 OSMO_ASSERT(msg);
1463 OSMO_ASSERT(ci);
1464
1465 ie_tag = msg->tail;
1466 rc = gsm0808_enc_cell_id_list2(msg, &cil);
1467
1468 if (rc <= 0)
1469 return rc;
1470
1471 *ie_tag = GSM0808_IE_CELL_IDENTIFIER;
1472 return rc;
1473}
1474
1475/*! Decode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1476 * \param[out] ci Caller-provided memory to store Cell ID.
1477 * \param[in] elem IE value to be decoded.
1478 * \param[in] len Length of \a elem in bytes.
1479 * \returns number of bytes parsed; negative on error */
1480int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len)
1481{
1482 struct gsm0808_cell_id_list2 cil;
1483 int rc;
1484 rc = gsm0808_dec_cell_id_list2(&cil, elem, len);
1485 if (rc < 0)
1486 return rc;
1487 if (cil.id_discr == CELL_IDENT_BSS || cil.id_discr == CELL_IDENT_NO_CELL) {
1488 if (cil.id_list_len != 0)
1489 return -EINVAL;
1490 } else {
1491 if (cil.id_list_len != 1)
1492 return -EINVAL;
1493 }
1494 ci->id_discr = cil.id_discr;
1495 ci->id = cil.id_list[0];
1496 return rc;
1497}
1498
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001499/*! Convert the representation of the permitted speech codec identifier
Philipp Maier3149b0d2017-06-02 13:22:34 +02001500 * that is used in struct gsm0808_channel_type to the speech codec
1501 * representation we use in struct gsm0808_speech_codec.
1502 * \param[in] perm_spch to be converted (see also gsm0808_permitted_speech)
1503 * \returns GSM speech codec type; negative on error */
1504int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch)
1505{
1506 /*! The speech codec type, which is used in the channel type field to
1507 * signal the permitted speech versions (codecs) has a different
1508 * encoding than the type field in the speech codec type element
1509 * (See also 3GPP TS 48.008, 3.2.2.11 and 3.2.2.103) */
1510
1511 switch (perm_spch) {
1512 case GSM0808_PERM_FR1:
1513 return GSM0808_SCT_FR1;
1514 case GSM0808_PERM_FR2:
1515 return GSM0808_SCT_FR2;
1516 case GSM0808_PERM_FR3:
1517 return GSM0808_SCT_FR3;
1518 case GSM0808_PERM_FR4:
1519 return GSM0808_SCT_FR4;
1520 case GSM0808_PERM_FR5:
1521 return GSM0808_SCT_FR5;
1522 case GSM0808_PERM_HR1:
1523 return GSM0808_SCT_HR1;
1524 case GSM0808_PERM_HR3:
1525 return GSM0808_SCT_HR3;
1526 case GSM0808_PERM_HR4:
1527 return GSM0808_SCT_HR4;
1528 case GSM0808_PERM_HR6:
1529 return GSM0808_SCT_HR6;
1530 }
1531
1532 /* Invalid input */
1533 return -EINVAL;
1534}
1535
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001536/*! Extrapolate a speech codec field from a given permitted speech
Philipp Maier884ba0f2017-06-02 13:49:16 +02001537 * parameter (channel type).
1538 * \param[out] sc Caller provided memory to store the resulting speech codec
1539 * \param[in] perm_spch value that is used to derive the speech codec info
1540 * (see also: enum gsm0808_speech_codec_type in gsm0808_utils.h)
1541 * \returns zero when successful; negative on error */
1542int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc,
1543 uint8_t perm_spch)
1544{
1545 int rc;
1546
1547 memset(sc, 0, sizeof(*sc));
1548
1549 /* Determine codec type */
1550 rc = gsm0808_chan_type_to_speech_codec(perm_spch);
1551 if (rc < 0)
1552 return -EINVAL;
1553 sc->type = (uint8_t) rc;
1554
1555 /* Depending on the speech codec type, pick a default codec
1556 * configuration that exactly matches the configuration on the
1557 * air interface. */
1558 switch (sc->type) {
1559 case GSM0808_SCT_FR3:
1560 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR;
1561 break;
1562 case GSM0808_SCT_FR4:
1563 sc->cfg = GSM0808_SC_CFG_DEFAULT_OFR_AMR_WB;
1564 break;
1565 case GSM0808_SCT_FR5:
1566 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR_WB;
1567 break;
1568 case GSM0808_SCT_HR3:
1569 sc->cfg = GSM0808_SC_CFG_DEFAULT_HR_AMR;
1570 break;
1571 case GSM0808_SCT_HR4:
1572 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR_WB;
1573 break;
1574 case GSM0808_SCT_HR6:
1575 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR;
1576 break;
1577 default:
1578 /* Note: Not all codec types specify a default setting,
1579 * in this case, we just set the field to zero. */
1580 sc->cfg = 0;
1581 }
1582
1583 /* Tag all codecs as "Full IP"
1584 * (see als 3GPP TS 48.008 3.2.2.103) */
1585 sc->fi = true;
1586
1587 return 0;
1588}
1589
Philipp Maier5f2eb152018-09-19 13:40:21 +02001590/*! Determine a set of AMR speech codec configuration bits (S0-S15) from a
1591 * given GSM 04.08 AMR configuration struct.
1592 * \param[in] cfg AMR configuration in GSM 04.08 format.
1593 * \param[in] hint if the resulting configuration shall be used with a FR or HR TCH.
1594 * \returns configuration bits (S0-S15) */
Philipp Maier369015c2018-09-21 09:07:20 +02001595uint16_t gsm0808_sc_cfg_from_gsm48_mr_cfg(const struct gsm48_multi_rate_conf *cfg,
Philipp Maier5f2eb152018-09-19 13:40:21 +02001596 bool fr)
1597{
1598 uint16_t s15_s0 = 0;
1599
1600 /* Check each rate bit in the AMR multirate configuration and pick the
1601 * matching default configuration as specified in 3GPP TS 28.062,
1602 * Table 7.11.3.1.3-2. */
1603 if (cfg->m4_75)
1604 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_4_75;
1605 if (cfg->m5_15)
1606 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_15;
1607 if (cfg->m5_90)
1608 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_90;
1609 if (cfg->m6_70)
1610 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_6_70;
1611 if (cfg->m7_40)
1612 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_40;
1613 if (cfg->m7_95)
1614 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_95;
1615 if (cfg->m10_2)
1616 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_10_2;
1617 if (cfg->m12_2)
1618 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_12_2;
1619
1620 /* Note: 3GPP TS 48.008, chapter 3GPP TS 48.008 states that for AMR
1621 * some of the configuration bits must be coded as zeros. The applied
1622 * bitmask matches the default codec settings. See also the definition
1623 * of enum gsm0808_speech_codec_defaults in gsm_08_08.h and
1624 * 3GPP TS 28.062, Table 7.11.3.1.3-2. */
1625 if (fr)
1626 s15_s0 &= GSM0808_SC_CFG_DEFAULT_FR_AMR;
1627 else
1628 s15_s0 &= GSM0808_SC_CFG_DEFAULT_HR_AMR;
1629
Philipp Maier94d79fd2019-03-01 10:40:48 +01001630 /* The mode that is encoded by S1 (Config-NB-Code = 1), takes a special
1631 * role as it does not stand for a single rate, but for up to four rates
1632 * at once (12.2, 7.4, 5.9, 4.75). We must check if the supplied cfg
1633 * covers this mode. If not, we need to make sure that the related
1634 * bit is removed. (See also 3GPP TS 28.062, Table 7.11.3.1.3-2) */
1635 if (!(cfg->m12_2 && cfg->m7_40 && cfg->m5_90 && cfg->m4_75) && fr)
1636 s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20;
1637 else if (!(cfg->m7_40 && cfg->m5_90 && cfg->m4_75))
1638 s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20;
1639
Philipp Maier5f2eb152018-09-19 13:40:21 +02001640 return s15_s0;
1641}
1642
Philipp Maier8515d032018-09-25 15:57:49 +02001643/*! Determine a GSM 04.08 AMR configuration struct from a set of speech codec
1644 * configuration bits (S0-S15)
1645 * \param[out] cfg AMR configuration in GSM 04.08 format.
Philipp Maier3713af82019-02-27 16:48:25 +01001646 * \param[in] s15_s0 configuration bits (S15-S0, non-ambiguous).
1647 * \returns zero when successful; negative on error */
1648int gsm48_mr_cfg_from_gsm0808_sc_cfg(struct gsm48_multi_rate_conf *cfg,
1649 uint16_t s15_s0)
Philipp Maier8515d032018-09-25 15:57:49 +02001650{
Philipp Maier3713af82019-02-27 16:48:25 +01001651 unsigned int count = 0;
1652
1653 /* Note: See also: 3GPP TS 28.062
1654 * Table 7.11.3.1.3-2: Preferred Configurations for the Adaptive
1655 * Multi-Rate Codec Types */
1656
1657 /* Note: The resulting multirate-configuration must not contain an
1658 * active set of more than four codec rates. The active set also
1659 * must contain at least one rate. */
1660
Philipp Maier8515d032018-09-25 15:57:49 +02001661 memset(cfg, 0, sizeof(*cfg));
Philipp Maier3713af82019-02-27 16:48:25 +01001662 cfg->ver = 1;
1663 cfg->icmi = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001664
1665 /* Strip option bits */
1666 s15_s0 &= 0x00ff;
1667
Philipp Maier3713af82019-02-27 16:48:25 +01001668 /* Rate 5,15k can never be selected (see table) */
1669 cfg->m5_15 = 0;
Philipp Maier8515d032018-09-25 15:57:49 +02001670
Neels Hofmeyra0f2b212021-04-14 22:45:13 +02001671 if (s15_s0 & GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20) {
Philipp Maier3713af82019-02-27 16:48:25 +01001672 /* Table Table 7.11.3.1.3-2 lists one mode that selects 4
1673 * rates at once (Config-NB-Code = 1). The rates selected
1674 * are known to be compatible between GERAN and UTRAN, since
1675 * an active set must not contain more than four rates at
1676 * a time, we ignore all other settings as they are either
1677 * redundaned or excess settings (invalid) */
Philipp Maier8515d032018-09-25 15:57:49 +02001678 cfg->m4_75 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001679 cfg->m5_90 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001680 cfg->m7_40 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001681 cfg->m12_2 = 1;
Philipp Maier3713af82019-02-27 16:48:25 +01001682 count += 4;
1683 }
Philipp Maier8515d032018-09-25 15:57:49 +02001684
Philipp Maier3713af82019-02-27 16:48:25 +01001685 /* Check the bits in s15_s0 and set the flags for the
1686 * respective rates. */
1687 if (s15_s0 & GSM0808_SC_CFG_AMR_4_75 && !cfg->m4_75) {
1688 if (count >= 4)
1689 return -EINVAL;
1690 cfg->m4_75 = 1;
1691 count++;
1692 }
1693 if (s15_s0 & GSM0808_SC_CFG_AMR_5_90 && !cfg->m5_90) {
1694 if (count >= 4)
1695 return -EINVAL;
1696 cfg->m5_90 = 1;
1697 count++;
1698 }
1699 if (s15_s0 & GSM0808_SC_CFG_AMR_6_70) {
1700 if (count >= 4)
1701 return -EINVAL;
1702 cfg->m6_70 = 1;
1703 count++;
1704 }
1705 if (s15_s0 & GSM0808_SC_CFG_AMR_7_40 && !cfg->m7_40) {
1706 if (count >= 4)
1707 return -EINVAL;
1708 cfg->m7_40 = 1;
1709 count++;
1710 }
1711 if (s15_s0 & GSM0808_SC_CFG_AMR_7_95) {
1712 if (count >= 4)
1713 return -EINVAL;
1714 cfg->m7_95 = 1;
1715 count++;
1716 }
1717 if (s15_s0 & GSM0808_SC_CFG_AMR_10_2) {
1718 if (count >= 4)
1719 return -EINVAL;
1720 cfg->m10_2 = 1;
1721 count++;
1722 }
1723 if (s15_s0 & GSM0808_SC_CFG_AMR_12_2 && !cfg->m12_2) {
1724 if (count >= 4)
1725 return -EINVAL;
1726 cfg->m12_2 = 1;
1727 count++;
1728 }
1729
1730 if (count == 0)
1731 return -EINVAL;
1732
1733 return 0;
Philipp Maier8515d032018-09-25 15:57:49 +02001734}
1735
Alexander Chemeris2ac8f912020-05-13 22:38:08 +03001736int gsm0808_get_cipher_reject_cause(const struct tlv_parsed *tp)
1737{
1738 return gsm0808_get_cause(tp);
1739}
1740
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001741/*! Print a human readable name of the cell identifier to the char buffer.
1742 * This is useful both for struct gsm0808_cell_id and struct gsm0808_cell_id_list2.
1743 * See also gsm0808_cell_id_name() and gsm0808_cell_id_list_name().
1744 * \param[out] buf Destination buffer to write string representation to.
1745 * \param[in] buflen Amount of memory available in \a buf.
1746 * \param[in] id_discr Cell Identifier type.
1747 * \param[in] u Cell Identifer value.
1748 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1749 * or that would have been written if the buffer were large enough.
1750 */
1751int gsm0808_cell_id_u_name(char *buf, size_t buflen,
1752 enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
1753{
1754 switch (id_discr) {
1755 case CELL_IDENT_LAC:
1756 return snprintf(buf, buflen, "%u", u->lac);
1757 case CELL_IDENT_CI:
1758 return snprintf(buf, buflen, "%u", u->ci);
1759 case CELL_IDENT_LAC_AND_CI:
1760 return snprintf(buf, buflen, "%u-%u", u->lac_and_ci.lac, u->lac_and_ci.ci);
1761 case CELL_IDENT_LAI_AND_LAC:
1762 return snprintf(buf, buflen, "%s", osmo_lai_name(&u->lai_and_lac));
1763 case CELL_IDENT_WHOLE_GLOBAL:
1764 return snprintf(buf, buflen, "%s", osmo_cgi_name(&u->global));
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01001765 case CELL_IDENT_WHOLE_GLOBAL_PS:
1766 return snprintf(buf, buflen, "%s", osmo_cgi_ps_name(&u->global_ps));
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001767 case CELL_IDENT_SAI:
1768 return snprintf(buf, buflen, "%s", osmo_sai_name(&u->sai));
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001769 default:
1770 /* For CELL_IDENT_BSS and CELL_IDENT_NO_CELL, just print the discriminator.
1771 * Same for kinds we have no string representation of yet. */
1772 return snprintf(buf, buflen, "%s", gsm0808_cell_id_discr_name(id_discr));
1773 }
1774}
1775
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02001776/*! Return true if the common information between the two Cell Identifiers match.
1777 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1778 * Note that CELL_IDENT_NO_CELL will always return false.
1779 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1780 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1781 * \param[in] discr1 Cell Identifier type.
1782 * \param[in] u1 Cell Identifier value.
1783 * \param[in] discr2 Other Cell Identifier type.
1784 * \param[in] u2 Other Cell Identifier value.
1785 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1786 * \returns True if the common fields of the above match.
1787 */
1788static bool gsm0808_cell_id_u_match(enum CELL_IDENT discr1, const union gsm0808_cell_id_u *u1,
1789 enum CELL_IDENT discr2, const union gsm0808_cell_id_u *u2,
1790 bool exact_match)
1791{
1792 struct osmo_cell_global_id a = {};
1793 struct osmo_cell_global_id b = {};
1794
1795 if (exact_match && discr1 != discr2)
1796 return false;
1797
1798 /* First handle the odd wildcard like CELL_IDENT kinds. We can't really match any of these. */
1799 switch (discr1) {
1800 case CELL_IDENT_NO_CELL:
1801 case CELL_IDENT_BSS:
1802 return discr1 == discr2;
1803 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1804 case CELL_IDENT_UTRAN_RNC:
1805 case CELL_IDENT_UTRAN_LAC_RNC:
1806 return false;
1807 default:
1808 break;
1809 }
1810 switch (discr2) {
1811 case CELL_IDENT_NO_CELL:
1812 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1813 case CELL_IDENT_UTRAN_RNC:
1814 case CELL_IDENT_UTRAN_LAC_RNC:
1815 case CELL_IDENT_BSS:
1816 return false;
1817 default:
1818 break;
1819 }
1820
1821 /* Enrich both sides to full CGI, then compare those. First set the *other* ID's values in case
1822 * they assign more items. For example:
1823 * u1 = LAC:42
1824 * u2 = LAC+CI:23+5
1825 * 1) a <- LAC+CI:23+5
1826 * 2) a <- LAC:42 so that a = LAC+CI:42+5
1827 * Now we can compare those two and find a mismatch. If the LAC were the same, we would get
1828 * identical LAC+CI and hence a match. */
1829
1830 cell_id_to_cgi(&a, discr2, u2);
1831 cell_id_to_cgi(&a, discr1, u1);
1832
1833 cell_id_to_cgi(&b, discr1, u1);
1834 cell_id_to_cgi(&b, discr2, u2);
1835
1836 return osmo_cgi_cmp(&a, &b) == 0;
1837}
1838
1839/*! Return true if the common information between the two Cell Identifiers match.
1840 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1841 * Note that CELL_IDENT_NO_CELL will always return false.
1842 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1843 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1844 * \param[in] id1 Cell Identifier.
1845 * \param[in] id2 Other Cell Identifier.
1846 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1847 * \returns True if the common fields of the above match.
1848 */
1849bool gsm0808_cell_ids_match(const struct gsm0808_cell_id *id1, const struct gsm0808_cell_id *id2, bool exact_match)
1850{
1851 return gsm0808_cell_id_u_match(id1->id_discr, &id1->id, id2->id_discr, &id2->id, exact_match);
1852}
1853
1854/*! Find an index in a Cell Identifier list that matches a given single Cell Identifer.
1855 * Compare \a id against each entry in \a list using gsm0808_cell_ids_match(), and return the list index
1856 * if a match is found. \a match_nr allows iterating all matches in the list. A match_nr <= 0 returns the
1857 * first match in the list, match_nr == 1 the second match, etc., and if match_nr exceeds the available
1858 * matches in the list, -1 is returned.
1859 * \param[in] id Cell Identifier to match.
1860 * \param[in] list Cell Identifier list to search in.
1861 * \param[in] match_nr Ignore this many matches.
1862 * \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 +01001863 * \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
1864 * entry).
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02001865 */
1866int gsm0808_cell_id_matches_list(const struct gsm0808_cell_id *id, const struct gsm0808_cell_id_list2 *list,
1867 unsigned int match_nr, bool exact_match)
1868{
1869 int i;
1870 for (i = 0; i < list->id_list_len; i++) {
1871 if (gsm0808_cell_id_u_match(id->id_discr, &id->id, list->id_discr, &list->id_list[i], exact_match)) {
1872 if (match_nr)
1873 match_nr--;
1874 else
1875 return i;
1876 }
1877 }
1878 return -1;
1879}
1880
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001881/*! Copy information from a CGI to form a Cell Identifier of the specified kind.
1882 * \param [out] cid Compose new Cell Identifier here.
1883 * \param [in] id_discr Which kind of Cell Identifier to compose.
1884 * \param [in] cgi Cell Global Identifier to form the Cell Identifier from.
1885 */
1886void gsm0808_cell_id_from_cgi(struct gsm0808_cell_id *cid, enum CELL_IDENT id_discr,
1887 const struct osmo_cell_global_id *cgi)
1888{
1889 *cid = (struct gsm0808_cell_id){
1890 .id_discr = id_discr,
1891 };
1892
1893 switch (id_discr) {
1894 case CELL_IDENT_WHOLE_GLOBAL:
1895 cid->id.global = *cgi;
1896 return;
1897
Pau Espin Pedrol20b763d2021-02-15 16:06:22 +01001898 case CELL_IDENT_WHOLE_GLOBAL_PS:
1899 cid->id.global_ps = (struct osmo_cell_global_id_ps){
1900 .rai = {
1901 .lac = cgi->lai,
1902 },
1903 .cell_identity = cgi->cell_identity,
1904 };
1905 return;
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001906 case CELL_IDENT_LAC_AND_CI:
1907 cid->id.lac_and_ci = (struct osmo_lac_and_ci_id){
1908 .lac = cgi->lai.lac,
1909 .ci = cgi->cell_identity,
1910 };
1911 return;
1912
1913 case CELL_IDENT_CI:
1914 cid->id.ci = cgi->cell_identity;
1915 return;
1916
1917 case CELL_IDENT_LAI:
1918 cid->id.lai_and_lac = cgi->lai;
1919 return;
1920
1921 case CELL_IDENT_LAC:
1922 cid->id.lac = cgi->lai.lac;
1923 return;
1924
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001925 case CELL_IDENT_SAI:
1926 cid->id.sai = (struct osmo_service_area_id){
1927 .lai = cgi->lai,
1928 };
1929 return;
1930
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001931 case CELL_IDENT_NO_CELL:
1932 case CELL_IDENT_BSS:
1933 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1934 case CELL_IDENT_UTRAN_RNC:
1935 case CELL_IDENT_UTRAN_LAC_RNC:
1936 default:
1937 return;
1938 };
1939}
1940
1941/*! Overwrite parts of cgi with values from a Cell Identifier.
1942 * Place only those items given in cid into cgi, leaving other values unchanged.
1943 * \param[out] cgi Cell Global Identity to write to.
1944 * \param[in] cid Cell Identity to read from.
1945 * \return a bitmask of items that were set: OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI; 0 if nothing was
1946 * written to cgi.
1947 */
1948int gsm0808_cell_id_to_cgi(struct osmo_cell_global_id *cgi, const struct gsm0808_cell_id *cid)
1949{
1950 switch (cid->id_discr) {
1951 case CELL_IDENT_WHOLE_GLOBAL:
1952 *cgi = cid->id.global;
1953 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
1954
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01001955 case CELL_IDENT_WHOLE_GLOBAL_PS:
1956 cgi->lai = cid->id.global_ps.rai.lac;
1957 cgi->cell_identity = cid->id.global_ps.cell_identity;
1958 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
1959
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001960 case CELL_IDENT_LAC_AND_CI:
1961 cgi->lai.lac = cid->id.lac_and_ci.lac;
1962 cgi->cell_identity = cid->id.lac_and_ci.ci;
1963 return OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
1964
1965 case CELL_IDENT_CI:
1966 cgi->cell_identity = cid->id.ci;
1967 return OSMO_CGI_PART_CI;
1968
1969 case CELL_IDENT_LAI:
1970 cgi->lai = cid->id.lai_and_lac;
1971 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC;
1972
1973 case CELL_IDENT_LAC:
1974 cgi->lai.lac = cid->id.lac;
1975 return OSMO_CGI_PART_LAC;
1976
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001977 case CELL_IDENT_SAI:
1978 cgi->lai = cid->id.sai.lai;
1979 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC;
1980
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001981 case CELL_IDENT_NO_CELL:
1982 case CELL_IDENT_BSS:
1983 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1984 case CELL_IDENT_UTRAN_RNC:
1985 case CELL_IDENT_UTRAN_LAC_RNC:
1986 default:
1987 return 0;
1988 };
1989}
1990
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001991/*! value_string[] for enum CELL_IDENT. */
1992const struct value_string gsm0808_cell_id_discr_names[] = {
1993 { CELL_IDENT_WHOLE_GLOBAL, "CGI" },
1994 { CELL_IDENT_LAC_AND_CI, "LAC-CI" },
1995 { CELL_IDENT_CI, "CI" },
1996 { CELL_IDENT_NO_CELL, "NO-CELL" },
1997 { CELL_IDENT_LAI_AND_LAC, "LAI" },
1998 { CELL_IDENT_LAC, "LAC" },
1999 { CELL_IDENT_BSS, "BSS" },
2000 { CELL_IDENT_UTRAN_PLMN_LAC_RNC, "UTRAN-PLMN-LAC-RNC" },
2001 { CELL_IDENT_UTRAN_RNC, "UTRAN-RNC" },
2002 { CELL_IDENT_UTRAN_LAC_RNC, "UTRAN-LAC-RNC" },
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01002003 { CELL_IDENT_SAI, "SAI" },
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01002004 { CELL_IDENT_WHOLE_GLOBAL_PS, "CGI-PS"},
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002005 { 0, NULL }
2006};
2007
2008#define APPEND_THING(func, args...) do { \
2009 int remain = buflen - (pos - buf); \
2010 int l = func(pos, remain, ##args); \
2011 if (l < 0 || l > remain) \
2012 pos = buf + buflen; \
2013 else \
2014 pos += l; \
2015 if (l > 0) \
2016 total_len += l; \
2017 } while(0)
2018#define APPEND_STR(fmt, args...) APPEND_THING(snprintf, fmt, ##args)
2019#define APPEND_CELL_ID_U(DISCR, U) APPEND_THING(gsm0808_cell_id_u_name, DISCR, U)
2020
Harald Welte179f3572019-03-18 18:38:47 +01002021char *gsm0808_cell_id_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id *cid)
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002022{
2023 char *pos = buf;
2024 int total_len = 0;
2025 APPEND_STR("%s:", gsm0808_cell_id_discr_name(cid->id_discr));
2026 APPEND_CELL_ID_U(cid->id_discr, &cid->id);
2027 return buf;
2028}
2029
2030/*! Return a human readable representation of a Cell Identifier, like "LAC:123"
2031 * or "CGI:001-01-42-23".
2032 * \param[in] cid Cell Identifer.
2033 * \returns String in a static buffer.
2034 */
2035const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid)
2036{
Harald Welte171ef822019-03-28 10:49:05 +01002037 static __thread char buf[64];
Harald Welte179f3572019-03-18 18:38:47 +01002038 return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002039}
2040
2041/*! Like gsm0808_cell_id_name() but uses a different static buffer.
2042 * \param[in] cid Cell Identifer.
2043 * \returns String in a static buffer.
2044 */
2045const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid)
2046{
Harald Welte171ef822019-03-28 10:49:05 +01002047 static __thread char buf[64];
Harald Welte179f3572019-03-18 18:38:47 +01002048 return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
2049}
2050
2051char *gsm0808_cell_id_name_c(const void *ctx, const struct gsm0808_cell_id *cid)
2052{
2053 char *buf = talloc_size(ctx, 64);
2054 if (!buf)
2055 return NULL;
2056 return gsm0808_cell_id_name_buf(buf, 64, cid);
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002057}
2058
2059/*! Return a human readable representation of the Cell Identifier List, like
2060 * "LAC[2]:{123, 456}".
2061 * The return value semantics are like snprintf() and thus allow ensuring a complete
2062 * untruncated string by determining the required string length from the return value.
2063 * If buflen > 0, always nul-terminate the string in buf, also when it is truncated.
2064 * If buflen == 0, do not modify buf, just return the would-be length.
2065 * \param[out] buf Destination buffer to write string representation to.
2066 * \param[in] buflen Amount of memory available in \a buf.
2067 * \param[in] cil Cell Identifer List.
2068 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
2069 * or that would have been written if the buffer were large enough.
2070 */
2071int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil)
2072{
2073 char *pos = buf;
2074 int total_len = 0;
2075 int i;
2076
2077 APPEND_STR("%s[%u]", gsm0808_cell_id_discr_name(cil->id_discr), cil->id_list_len);
2078
2079 switch (cil->id_discr) {
2080 case CELL_IDENT_BSS:
2081 case CELL_IDENT_NO_CELL:
2082 return total_len;
2083 default:
2084 break;
2085 }
2086
2087 APPEND_STR(":{");
2088
2089 for (i = 0; i < cil->id_list_len; i++) {
2090 if (i)
2091 APPEND_STR(", ");
2092 APPEND_CELL_ID_U(cil->id_discr, &cil->id_list[i]);
2093 }
2094
2095 APPEND_STR("}");
2096 return total_len;
2097}
2098
2099/*! Return a human-readable representation of \a cil in a static buffer.
2100 * If the list is too long, the output may be truncated.
2101 * See also gsm0808_cell_id_list_name_buf(). */
2102const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil)
2103{
Harald Welte171ef822019-03-28 10:49:05 +01002104 static __thread char buf[1024];
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002105 gsm0808_cell_id_list_name_buf(buf, sizeof(buf), cil);
2106 return buf;
2107}
2108
Harald Welte179f3572019-03-18 18:38:47 +01002109char *gsm0808_cell_id_list_name_c(const void *ctx, const struct gsm0808_cell_id_list2 *cil)
2110{
2111 char *buf = talloc_size(ctx, 1024);
2112 if (!buf)
2113 return NULL;
2114 gsm0808_cell_id_list_name_buf(buf, 1024, cil);
2115 return buf;
2116}
2117
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002118#undef APPEND_STR
2119#undef APPEND_CELL_ID_U
2120
Harald Welte4a62eda2019-03-18 18:27:00 +01002121char *gsm0808_channel_type_name_buf(char *buf, size_t buf_len, const struct gsm0808_channel_type *ct)
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02002122{
Harald Welte4a62eda2019-03-18 18:27:00 +01002123 snprintf(buf, buf_len, "ch_indctr=0x%x ch_rate_type=0x%x perm_spch=%s",
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02002124 ct->ch_indctr, ct->ch_rate_type,
2125 osmo_hexdump(ct->perm_spch, ct->perm_spch_len));
2126 return buf;
2127}
2128
Harald Welte4a62eda2019-03-18 18:27:00 +01002129const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct)
2130{
Harald Welte171ef822019-03-28 10:49:05 +01002131 static __thread char buf[128];
Harald Welte4a62eda2019-03-18 18:27:00 +01002132 return gsm0808_channel_type_name_buf(buf, sizeof(buf), ct);
2133}
2134
Harald Welte179f3572019-03-18 18:38:47 +01002135char *gsm0808_channel_type_name_c(const void *ctx, const struct gsm0808_channel_type *ct)
2136{
2137 char *buf = talloc_size(ctx, 128);
2138 if (!buf)
2139 return NULL;
2140 return gsm0808_channel_type_name_buf(buf, 128, ct);
2141}
2142
Harald Welte96e2a002017-06-12 21:44:18 +02002143/*! @} */