blob: f1569c90cd1364a58525918e45b993f81212da38 [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;
Harald Welte459a1802018-06-28 09:24:17 +0200224 bool type_extended = false;
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. */
230 switch(sc->type) {
231 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 {
267 OSMO_ASSERT(sc->type < 0x0f);
268 header |= sc->type;
269 msgb_put_u8(msg, header);
Philipp Maier6f725d62017-03-24 18:03:17 +0100270 }
271
Philipp Maierbb839662017-06-01 17:11:19 +0200272 /* Note: Whether a configuration is present or not depends on the
273 * selected codec type. If present, it can either consist of one
274 * or two octets, depending on the codec type */
275 switch (sc->type) {
276 case GSM0808_SCT_FR3:
277 case GSM0808_SCT_HR3:
278 case GSM0808_SCT_HR6:
Philipp Maier7e27b142018-03-22 17:26:46 +0100279 msgb_put_u16(msg, osmo_ntohs(sc->cfg));
Philipp Maierbb839662017-06-01 17:11:19 +0200280 break;
281 case GSM0808_SCT_FR4:
282 case GSM0808_SCT_FR5:
283 case GSM0808_SCT_HR4:
284 case GSM0808_SCT_CSD:
Alexander Couzens4e284b62019-06-23 01:53:43 +0200285 OSMO_ASSERT((sc->cfg & 0xff00) == 0);
Philipp Maierbb839662017-06-01 17:11:19 +0200286 msgb_put_u8(msg, (uint8_t) sc->cfg & 0xff);
287 break;
288 default:
289 OSMO_ASSERT(sc->cfg == 0);
290 break;
291 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100292
293 return (uint8_t) (msg->tail - old_tail);
294}
295
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200296/*! Encode TS 08.08 Speech Codec IE
Harald Welte96e2a002017-06-12 21:44:18 +0200297 * \param[out] msg Message Buffer to which IE will be appended
298 * \param[in] sc Speech Codec to be encoded into IE
299 * \returns number of bytes appended to \a msg */
Philipp Maier6f725d62017-03-24 18:03:17 +0100300uint8_t gsm0808_enc_speech_codec(struct msgb *msg,
301 const struct gsm0808_speech_codec *sc)
302{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200303 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100304 uint8_t *old_tail;
305 uint8_t *tlv_len;
306
307 OSMO_ASSERT(msg);
308 OSMO_ASSERT(sc);
309
310 msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC);
311 tlv_len = msgb_put(msg, 1);
312 old_tail = msg->tail;
313
314 enc_speech_codec(msg, sc);
315
316 *tlv_len = (uint8_t) (msg->tail - old_tail);
317 return *tlv_len + 2;
318}
319
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200320/*! Decode TS 08.08 Speech Codec IE
Harald Welte96e2a002017-06-12 21:44:18 +0200321 * \param[out] sc Caller-allocated memory for Speech Codec
322 * \param[in] elem IE value to be decoded
323 * \param[in] len Length of \a elem in bytes
324 * \returns number of bytes parsed; negative on error */
Philipp Maier6f725d62017-03-24 18:03:17 +0100325int gsm0808_dec_speech_codec(struct gsm0808_speech_codec *sc,
326 const uint8_t *elem, uint8_t len)
327{
328 /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
329 uint8_t header;
330 const uint8_t *old_elem = elem;
331
332 OSMO_ASSERT(sc);
333 if (!elem)
334 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200335 if (len == 0)
Philipp Maier6f725d62017-03-24 18:03:17 +0100336 return -EINVAL;
337
338 memset(sc, 0, sizeof(*sc));
339
340 header = *elem;
341
Philipp Maier85a6af22017-04-28 10:55:05 +0200342 /* An extended codec type needs at least two fields,
343 * bail if the input data length is not sufficient. */
Philipp Maier6f725d62017-03-24 18:03:17 +0100344 if ((header & 0x0F) == 0x0F && len < 2)
345 return -EINVAL;
Philipp Maier6f725d62017-03-24 18:03:17 +0100346
347 elem++;
348 len--;
349
350 if (header & (1 << 7))
351 sc->fi = true;
352 if (header & (1 << 6))
353 sc->pi = true;
354 if (header & (1 << 5))
355 sc->pt = true;
356 if (header & (1 << 4))
357 sc->tf = true;
358
359 if ((header & 0x0F) != 0x0F) {
360 sc->type = (header & 0x0F);
Philipp Maierbb839662017-06-01 17:11:19 +0200361 } else {
362 sc->type = *elem;
363 elem++;
364 len--;
Philipp Maier6f725d62017-03-24 18:03:17 +0100365 }
366
Philipp Maierbb839662017-06-01 17:11:19 +0200367 /* Note: Whether a configuration is present or not depends on the
368 * selected codec type. If present, it can either consist of one or
369 * two octets depending on the codec type */
370 switch (sc->type) {
371 case GSM0808_SCT_FR1:
372 case GSM0808_SCT_FR2:
373 case GSM0808_SCT_HR1:
374 break;
375 case GSM0808_SCT_HR4:
376 case GSM0808_SCT_CSD:
377 case GSM0808_SCT_FR4:
378 case GSM0808_SCT_FR5:
379 if (len < 1)
380 return -EINVAL;
381 sc->cfg = *elem;
382 elem++;
383 break;
384 case GSM0808_SCT_FR3:
385 case GSM0808_SCT_HR3:
386 case GSM0808_SCT_HR6:
387 if (len < 2)
388 return -EINVAL;
Philipp Maier7e27b142018-03-22 17:26:46 +0100389 sc->cfg = osmo_load16le(elem);
Philipp Maierbb839662017-06-01 17:11:19 +0200390 elem += 2;
391 break;
392 default:
393 /* Invalid codec type => malformed speech codec element! */
394 return -EINVAL;
395 break;
396 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100397
398 return (int)(elem - old_elem);
399}
400
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200401/*! Encode TS 08.08 Speech Codec list
Harald Welte96e2a002017-06-12 21:44:18 +0200402 * \param[out] msg Message Buffer to which IE is to be appended
403 * \param[in] scl Speech Codec List to be encoded into IE
404 * \returns number of bytes added to \a msg */
Philipp Maier6f725d62017-03-24 18:03:17 +0100405uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg,
406 const struct gsm0808_speech_codec_list *scl)
407{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200408 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100409 uint8_t *old_tail;
410 uint8_t *tlv_len;
411 unsigned int i;
412 uint8_t rc;
413 unsigned int bytes_used = 0;
414
415 OSMO_ASSERT(msg);
416 OSMO_ASSERT(scl);
417
Philipp Maier6f725d62017-03-24 18:03:17 +0100418 msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC_LIST);
419 tlv_len = msgb_put(msg, 1);
420 old_tail = msg->tail;
421
422 for (i = 0; i < scl->len; i++) {
423 rc = enc_speech_codec(msg, &scl->codec[i]);
424 OSMO_ASSERT(rc >= 1);
425 bytes_used += rc;
426 OSMO_ASSERT(bytes_used <= 255);
427 }
428
429 *tlv_len = (uint8_t) (msg->tail - old_tail);
430 return *tlv_len + 2;
431}
432
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200433/*! Decode TS 08.08 Speech Codec list IE
Harald Welte96e2a002017-06-12 21:44:18 +0200434 * \param[out] scl Caller-provided memory to store codec list
435 * \param[in] elem IE value to be decoded
436 * \param[in] len Length of \a elem in bytes
437 * \returns number of bytes parsed; negative on error */
Philipp Maier6f725d62017-03-24 18:03:17 +0100438int gsm0808_dec_speech_codec_list(struct gsm0808_speech_codec_list *scl,
439 const uint8_t *elem, uint8_t len)
440{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200441 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100442 const uint8_t *old_elem = elem;
443 unsigned int i;
444 int rc;
445 uint8_t decoded = 0;
446
447 OSMO_ASSERT(scl);
448 if (!elem)
449 return -EINVAL;
Philipp Maier6f725d62017-03-24 18:03:17 +0100450
451 memset(scl, 0, sizeof(*scl));
452
453 for (i = 0; i < ARRAY_SIZE(scl->codec); i++) {
454 if (len <= 0)
455 break;
456
457 rc = gsm0808_dec_speech_codec(&scl->codec[i], elem, len);
458 if (rc < 1)
459 return -EINVAL;
460
461 elem+=rc;
462 len -= rc;
463 decoded++;
464 }
465
466 scl->len = decoded;
467
Philipp Maier6f725d62017-03-24 18:03:17 +0100468 return (int)(elem - old_elem);
469}
Philipp Maiere0c65302017-03-28 17:05:40 +0200470
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200471/*! Encode TS 08.08 Channel Type IE
Harald Welte96e2a002017-06-12 21:44:18 +0200472 * \param[out] msg Message Buffer to which IE is to be appended
473 * \param[in] ct Channel Type to be encoded
474 * \returns number of bytes added to \a msg */
Philipp Maiere0c65302017-03-28 17:05:40 +0200475uint8_t gsm0808_enc_channel_type(struct msgb *msg,
476 const struct gsm0808_channel_type *ct)
477{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200478 /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */
Philipp Maiere0c65302017-03-28 17:05:40 +0200479 unsigned int i;
480 uint8_t byte;
481 uint8_t *old_tail;
482 uint8_t *tlv_len;
483
484 OSMO_ASSERT(msg);
485 OSMO_ASSERT(ct);
486 OSMO_ASSERT(ct->perm_spch_len <= CHANNEL_TYPE_ELEMENT_MAXLEN - 2);
487
488 /* FIXME: Implement encoding support for Data
489 * and Speech + CTM Text Telephony */
490 if ((ct->ch_indctr & 0x0f) != GSM0808_CHAN_SPEECH
491 && (ct->ch_indctr & 0x0f) != GSM0808_CHAN_SIGN)
492 OSMO_ASSERT(false);
493
494 msgb_put_u8(msg, GSM0808_IE_CHANNEL_TYPE);
495 tlv_len = msgb_put(msg, 1);
496 old_tail = msg->tail;
497
498 msgb_put_u8(msg, ct->ch_indctr & 0x0f);
499 msgb_put_u8(msg, ct->ch_rate_type);
500
501 for (i = 0; i < ct->perm_spch_len; i++) {
502 byte = ct->perm_spch[i];
503
504 if (i < ct->perm_spch_len - 1)
505 byte |= 0x80;
506 msgb_put_u8(msg, byte);
507 }
508
509 *tlv_len = (uint8_t) (msg->tail - old_tail);
510 return *tlv_len + 2;
511}
512
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200513/*! Decode TS 08.08 Channel Type IE
Harald Welte96e2a002017-06-12 21:44:18 +0200514 * \param[out] ct Caller-provided memory to store channel type
515 * \param[in] elem IE Value to be decoded
516 * \param[in] len Length of \a elem in bytes
517 * \returns number of bytes parsed; negative on error */
Philipp Maiere0c65302017-03-28 17:05:40 +0200518int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct,
519 const uint8_t *elem, uint8_t len)
520{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200521 /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */
Philipp Maiere0c65302017-03-28 17:05:40 +0200522 unsigned int i;
523 uint8_t byte;
524 const uint8_t *old_elem = elem;
525
526 OSMO_ASSERT(ct);
527 if (!elem)
528 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200529 if (len < 3 || len > 11)
Philipp Maiere0c65302017-03-28 17:05:40 +0200530 return -EINVAL;
531
532 memset(ct, 0, sizeof(*ct));
533
534 ct->ch_indctr = (*elem) & 0x0f;
535 elem++;
536 ct->ch_rate_type = (*elem) & 0x0f;
537 elem++;
538
539 for (i = 0; i < ARRAY_SIZE(ct->perm_spch); i++) {
540 byte = *elem;
541 elem++;
542 ct->perm_spch[i] = byte & 0x7f;
543 if ((byte & 0x80) == 0x00)
544 break;
545 }
546 ct->perm_spch_len = i + 1;
547
548 return (int)(elem - old_elem);
549}
Philipp Maier14e76b92017-03-28 18:36:52 +0200550
Max969fb2e2018-12-10 11:01:10 +0100551/*! Create BSSMAP Global Call Reference, 3GPP TS 48.008 §3.2.2.115.
552 * \param[out] msg Message Buffer for appending IE
553 * \param[in] g Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1
554 * \returns number of bytes added to \a msg or 0 on error */
Max47022152018-12-19 18:51:00 +0100555static uint8_t gsm0808_enc_gcr(struct msgb *msg, const struct osmo_gcr_parsed *g)
Max969fb2e2018-12-10 11:01:10 +0100556{
557 uint8_t enc, *len = msgb_tl_put(msg, GSM0808_IE_GLOBAL_CALL_REF);
558
559 enc = osmo_enc_gcr(msg, g);
560 if (!enc)
561 return 0;
562
563 *len = enc;
564 return enc + 2; /* type (1 byte) + length (1 byte) */
565}
566
567/*! Decode BSSMAP Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1.
568 * \param[out] gcr Caller-provided memory to store Global Call Reference
Max036012b2018-12-19 17:48:56 +0100569 * \param[in] tp IE values to be decoded
Max969fb2e2018-12-10 11:01:10 +0100570 * \returns number of bytes parsed; negative on error */
Max47022152018-12-19 18:51:00 +0100571static int gsm0808_dec_gcr(struct osmo_gcr_parsed *gcr, const struct tlv_parsed *tp)
Max969fb2e2018-12-10 11:01:10 +0100572{
573 int ret;
574 const uint8_t *buf = TLVP_VAL_MINLEN(tp, GSM0808_IE_GLOBAL_CALL_REF, OSMO_GCR_MIN_LEN);
575 if (!buf)
576 return -EINVAL;
577
578 ret = osmo_dec_gcr(gcr, buf, TLVP_LEN(tp, GSM0808_IE_GLOBAL_CALL_REF));
579 if (ret < 0)
580 return -ENOENT;
581
582 return 2 + ret;
583}
584
Max47022152018-12-19 18:51:00 +0100585/*! Add LCLS parameters to a given msgb, 3GPP TS 48.008 §3.2.2.115 - 3.2.2.120.
586 * \param[out] msg Message Buffer for appending IE
587 * \param[in] lcls LCLS-related data
588 * \returns number of bytes added to \a msg or 0 on error */
589uint8_t gsm0808_enc_lcls(struct msgb *msg, const struct osmo_lcls *lcls)
590{
591 uint8_t enc = 0;
592
593 /* LCLS: §3.2.2.115 Global Call Reference */
Max3b901252019-01-15 14:15:11 +0100594 if (lcls->gcr_available)
595 enc = gsm0808_enc_gcr(msg, &lcls->gcr);
Max47022152018-12-19 18:51:00 +0100596
597 /* LCLS: §3.2.2.116 Configuration */
598 if (lcls->config != GSM0808_LCLS_CFG_NA) {
599 msgb_tv_put(msg, GSM0808_IE_LCLS_CONFIG, lcls->config);
600 enc += 2;
601 }
602
603 /* LCLS: §3.2.2.117 Connection Status Control */
604 if (lcls->control != GSM0808_LCLS_CSC_NA) {
605 msgb_tv_put(msg, GSM0808_IE_LCLS_CONN_STATUS_CTRL, lcls->control);
606 enc += 2;
607 }
608
609 /* LCLS: §3.2.2.118 Correlation-Not-Needed */
610 if (!lcls->corr_needed) {
611 msgb_v_put(msg, GSM0808_IE_LCLS_CORR_NOT_NEEDED);
612 enc++;
613 }
614
615 return enc;
616}
617
618/*! Decode LCLS parameters to a given msgb, 3GPP TS 48.008 §3.2.2.115 - 3.2.2.120.
619 * \param[out] lcls Caller-provided memory to store LCLS-related data
620 * \param[in] tp IE values to be decoded
621 * \returns GCR size or negative on error */
622int gsm0808_dec_lcls(struct osmo_lcls *lcls, const struct tlv_parsed *tp)
623{
Max3b901252019-01-15 14:15:11 +0100624 int ret = gsm0808_dec_gcr(&lcls->gcr, tp);
Max47022152018-12-19 18:51:00 +0100625
Max3b901252019-01-15 14:15:11 +0100626 lcls->gcr_available = (ret < 0) ? false : true;
Max47022152018-12-19 18:51:00 +0100627 lcls->config = tlvp_val8(tp, GSM0808_IE_LCLS_CONFIG, GSM0808_LCLS_CFG_NA);
628 lcls->control = tlvp_val8(tp, GSM0808_IE_LCLS_CONN_STATUS_CTRL, GSM0808_LCLS_CSC_NA);
629 lcls->corr_needed = TLVP_PRESENT(tp, GSM0808_IE_LCLS_CORR_NOT_NEEDED) ? false : true;
630
631 return ret;
632}
633
Harald Welte171ef822019-03-28 10:49:05 +0100634static __thread char dbuf[256];
Max5ec0cf52019-01-15 16:37:09 +0100635
636/*! Dump LCLS parameters (GCR excluded) into string for printing.
Harald Welte4a62eda2019-03-18 18:27:00 +0100637 * \param[out] buf caller-allocated output string buffer
638 * \param[in] buf_len size of buf in bytes
Max5ec0cf52019-01-15 16:37:09 +0100639 * \param[in] lcls pointer to the struct to print.
640 * \returns string representation of LCLS or NULL on error. */
Harald Welte4a62eda2019-03-18 18:27:00 +0100641char *osmo_lcls_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls)
Max5ec0cf52019-01-15 16:37:09 +0100642{
Harald Welte4a62eda2019-03-18 18:27:00 +0100643 struct osmo_strbuf s = { .buf = buf, .len = buf_len };
Max5ec0cf52019-01-15 16:37:09 +0100644
645 if (!lcls)
646 return NULL;
647
648 OSMO_STRBUF_PRINTF(s, "LCLS Config: %s, Control: %s, Correlation-Needed: %u",
649 gsm0808_lcls_config_name(lcls->config),
650 gsm0808_lcls_control_name(lcls->control),
651 lcls->corr_needed);
652
653 return dbuf;
654}
655
Harald Welte4a62eda2019-03-18 18:27:00 +0100656/*! Dump LCLS parameters (GCR excluded) into static string buffer for printing.
657 * \param[in] lcls pointer to the struct to print.
658 * \returns string representation of LCLS in static buffer or NULL on error. */
659char *osmo_lcls_dump(const struct osmo_lcls *lcls)
660{
661 return osmo_lcls_dump_buf(dbuf, sizeof(dbuf), lcls);
662}
663
Harald Welte179f3572019-03-18 18:38:47 +0100664char *osmo_lcls_dump_c(void *ctx, const struct osmo_lcls *lcls)
665{
666 char *buf = talloc_size(ctx, 256);
667 if (!buf)
668 return NULL;
669 return osmo_lcls_dump_buf(buf, 256, lcls);
670}
671
Max5ec0cf52019-01-15 16:37:09 +0100672/*! Dump GCR struct into string for printing.
Harald Welte4a62eda2019-03-18 18:27:00 +0100673 * \param[out] buf caller-allocated output string buffer
674 * \param[in] buf_len size of buf in bytes
Max5ec0cf52019-01-15 16:37:09 +0100675 * \param[in] lcls pointer to the struct to print.
676 * \returns string representation of GCR or NULL on error. */
Harald Welte4a62eda2019-03-18 18:27:00 +0100677char *osmo_gcr_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls)
Max5ec0cf52019-01-15 16:37:09 +0100678{
Harald Welte4a62eda2019-03-18 18:27:00 +0100679 struct osmo_strbuf s = { .buf = buf, .len = buf_len };
Max5ec0cf52019-01-15 16:37:09 +0100680
681 if (!lcls)
682 return NULL;
683
684 if (lcls->gcr_available) {
685 OSMO_STRBUF_PRINTF(s, "GCR NetID 0x%s, ", osmo_hexdump_nospc(lcls->gcr.net, lcls->gcr.net_len));
686 /* osmo_hexdump() uses static buffers so we can't call it twice withing the same parameter list */
687 OSMO_STRBUF_PRINTF(s, "Node 0x%x, CallRefID 0x%s", lcls->gcr.node, osmo_hexdump_nospc(lcls->gcr.cr, 5));
688 }
689
690 return dbuf;
691}
692
Harald Welte4a62eda2019-03-18 18:27:00 +0100693/*! Dump GCR struct into static string buffer for printing.
694 * \param[in] lcls pointer to the struct to print.
695 * \returns string representation of GCR in static buffer or NULL on error. */
696char *osmo_gcr_dump(const struct osmo_lcls *lcls)
697{
698 return osmo_gcr_dump_buf(dbuf, sizeof(dbuf), lcls);
699}
700
701
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200702/*! Encode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200703 * \param[out] msg Message Buffer to which IE is to be appended
704 * \param[in] ei Encryption Information to be encoded
705 * \returns number of bytes appended to \a msg */
Philipp Maier14e76b92017-03-28 18:36:52 +0200706uint8_t gsm0808_enc_encrypt_info(struct msgb *msg,
707 const struct gsm0808_encrypt_info *ei)
708{
709 unsigned int i;
710 uint8_t perm_algo = 0;
711 uint8_t *ptr;
712 uint8_t *old_tail;
713 uint8_t *tlv_len;
714
715 OSMO_ASSERT(msg);
716 OSMO_ASSERT(ei);
717 OSMO_ASSERT(ei->key_len <= ARRAY_SIZE(ei->key));
718 OSMO_ASSERT(ei->perm_algo_len <= ENCRY_INFO_PERM_ALGO_MAXLEN);
719
720 msgb_put_u8(msg, GSM0808_IE_ENCRYPTION_INFORMATION);
721 tlv_len = msgb_put(msg, 1);
722 old_tail = msg->tail;
723
724 for (i = 0; i < ei->perm_algo_len; i++) {
725 /* Note: gsm_08_08.h defines the permitted algorithms
726 * as an enum which ranges from 0x01 to 0x08 */
727 OSMO_ASSERT(ei->perm_algo[i] != 0);
728 OSMO_ASSERT(ei->perm_algo[i] <= ENCRY_INFO_PERM_ALGO_MAXLEN);
729 perm_algo |= (1 << (ei->perm_algo[i] - 1));
730 }
731
732 msgb_put_u8(msg, perm_algo);
Neels Hofmeyr26e53b12021-06-10 00:47:03 +0200733 /* FIXME: 48.008 3.2.2.10 Encryption Information says:
734 * "When present, the key shall be 8 octets long." */
Philipp Maier14e76b92017-03-28 18:36:52 +0200735 ptr = msgb_put(msg, ei->key_len);
736 memcpy(ptr, ei->key, ei->key_len);
737
738 *tlv_len = (uint8_t) (msg->tail - old_tail);
739 return *tlv_len + 2;
740}
741
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200742/*! Decode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200743 * \param[out] ei Caller-provided memory to store encryption information
744 * \param[in] elem IE value to be decoded
745 * \param[in] len Length of \a elem in bytes
746 * \returns number of bytes parsed; negative on error */
Philipp Maier14e76b92017-03-28 18:36:52 +0200747int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei,
748 const uint8_t *elem, uint8_t len)
749{
750 uint8_t perm_algo;
751 unsigned int i;
752 unsigned int perm_algo_len = 0;
753 const uint8_t *old_elem = elem;
754
755 OSMO_ASSERT(ei);
756 if (!elem)
757 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200758 if (len == 0)
Philipp Maier14e76b92017-03-28 18:36:52 +0200759 return -EINVAL;
760
761 memset(ei, 0, sizeof(*ei));
762
763 perm_algo = *elem;
764 elem++;
765
766 for (i = 0; i < ENCRY_INFO_PERM_ALGO_MAXLEN; i++) {
767 if (perm_algo & (1 << i)) {
768 ei->perm_algo[perm_algo_len] = i + 1;
769 perm_algo_len++;
770 }
771 }
772 ei->perm_algo_len = perm_algo_len;
773
Neels Hofmeyr26e53b12021-06-10 00:47:03 +0200774 /* FIXME: 48.008 3.2.2.10 Encryption Information says:
775 * "When present, the key shall be 8 octets long." */
Philipp Maier14e76b92017-03-28 18:36:52 +0200776 ei->key_len = len - 1;
777 memcpy(ei->key, elem, ei->key_len);
778 elem+=ei->key_len;
779
780 return (int)(elem - old_elem);
781}
Philipp Maier783047e2017-03-29 11:35:50 +0200782
Neels Hofmeyr4a9756c2021-06-10 00:48:15 +0200783/*! Encode TS 48.008 Kc128 IE.
784 * \param[out] msg Message Buffer to which IE is to be appended.
785 * \param[in] kc128 Pointer to 16 bytes of Kc128 key data.
786 * \returns number of bytes appended to msg */
787int gsm0808_enc_kc128(struct msgb *msg, const uint8_t *kc128)
788{
789 uint8_t *start = msg->tail;
790 msgb_tv_fixed_put(msg, GSM0808_IE_KC_128, 16, kc128);
791 return msg->tail - start;
792}
793
794/*! Decode TS 48.008 Kc128 IE.
795 * \param[out] kc128 Target buffer for received Kc128 key, 16 bytes long.
796 * \param[in] elem IE value to be decoded (without IE discriminator).
797 * \param[in] len Length of elem in bytes.
798 * \returns number of bytes parsed; negative on error */
799int gsm0808_dec_kc128(uint8_t *kc128, const uint8_t *elem, uint8_t len)
800{
801 if (len != 16)
802 return -EINVAL;
803 memcpy(kc128, elem, 16);
804 return len;
805}
806
Pau Espin Pedrol85a0f112021-02-15 16:25:33 +0100807/* Store individual Cell Identifier information in a CGI, without clearing the remaining ones.
808 * This is useful to supplement one CGI with information from more than one Cell Identifier,
809 * which in turn is useful to match Cell Identifiers of differing kinds to each other.
810 * Before first invocation, clear the *dst struct externally, this function does only write those members
811 * that are present in parameter u.
812 */
813static void cell_id_to_cgi(struct osmo_cell_global_id *dst,
814 enum CELL_IDENT discr, const union gsm0808_cell_id_u *u)
815{
816 switch (discr) {
817 case CELL_IDENT_WHOLE_GLOBAL:
818 *dst = u->global;
819 return;
820
821 case CELL_IDENT_WHOLE_GLOBAL_PS:
822 dst->lai = u->global_ps.rai.lac;
823 dst->cell_identity = u->global_ps.cell_identity;
824 return;
825
826 case CELL_IDENT_LAC_AND_CI:
827 dst->lai.lac = u->lac_and_ci.lac;
828 dst->cell_identity = u->lac_and_ci.ci;
829 return;
830
831 case CELL_IDENT_CI:
832 dst->cell_identity = u->ci;
833 return;
834
835 case CELL_IDENT_LAI_AND_LAC:
836 dst->lai = u->lai_and_lac;
837 return;
838
839 case CELL_IDENT_LAC:
840 dst->lai.lac = u->lac;
841 return;
842
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +0100843 case CELL_IDENT_SAI:
844 dst->lai = u->sai.lai;
845 return;
846
Pau Espin Pedrol85a0f112021-02-15 16:25:33 +0100847 case CELL_IDENT_NO_CELL:
848 case CELL_IDENT_BSS:
849 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
850 case CELL_IDENT_UTRAN_RNC:
851 case CELL_IDENT_UTRAN_LAC_RNC:
852 /* No values to set. */
853 return;
854 }
855}
856
Harald Weltef2210032019-08-31 21:25:05 +0200857/* Return the size of the value part of a cell identifier of given type */
858int gsm0808_cell_id_size(enum CELL_IDENT discr)
859{
860 switch (discr) {
861 case CELL_IDENT_WHOLE_GLOBAL:
862 return 7;
863 case CELL_IDENT_LAC_AND_CI:
864 return 4;
865 case CELL_IDENT_CI:
866 return 2;
867 case CELL_IDENT_LAI_AND_LAC:
868 return 5;
869 case CELL_IDENT_LAC:
870 return 2;
871 case CELL_IDENT_BSS:
872 case CELL_IDENT_NO_CELL:
873 return 0;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +0100874 case CELL_IDENT_SAI:
875 return 7;
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100876 case CELL_IDENT_WHOLE_GLOBAL_PS:
877 return 8;
Harald Weltef2210032019-08-31 21:25:05 +0200878 default:
879 return -EINVAL;
880 }
881}
Harald Welteef7be492019-05-03 21:01:13 +0200882/*! Decode a single GSM 08.08 Cell ID list element payload
883 * \param[out] out caller-provided output union
884 * \param[in] discr Cell ID discriminator describing type to be decoded
885 * \param[in] buf User-provided input buffer containing binary Cell ID list element
886 * \param[in] len Length of buf
887 * \returns 0 on success; negative on error */
888int gsm0808_decode_cell_id_u(union gsm0808_cell_id_u *out, enum CELL_IDENT discr, const uint8_t *buf, unsigned int len)
889{
890 switch (discr) {
891 case CELL_IDENT_WHOLE_GLOBAL:
892 if (len < 7)
893 return -EINVAL;
894 decode_lai(buf, &out->global.lai);
895 out->global.cell_identity = osmo_load16be(buf + sizeof(struct gsm48_loc_area_id));
896 break;
897 case CELL_IDENT_LAC_AND_CI:
898 if (len < 4)
899 return -EINVAL;
900 out->lac_and_ci.lac = osmo_load16be(buf);
901 out->lac_and_ci.ci = osmo_load16be(buf + sizeof(uint16_t));
902 break;
903 case CELL_IDENT_CI:
904 if (len < 2)
905 return -EINVAL;
906 out->ci = osmo_load16be(buf);
907 break;
908 case CELL_IDENT_LAI_AND_LAC:
909 if (len < 5)
910 return -EINVAL;
911 decode_lai(buf, &out->lai_and_lac);
912 break;
913 case CELL_IDENT_LAC:
914 if (len < 2)
915 return -EINVAL;
916 out->lac = osmo_load16be(buf);
917 break;
918 case CELL_IDENT_BSS:
919 case CELL_IDENT_NO_CELL:
920 /* Does not have any list items */
921 break;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +0100922 case CELL_IDENT_SAI:
923 if (len < 7)
924 return -EINVAL;
925 decode_lai(buf, &out->sai.lai);
926 out->sai.sac = osmo_load16be(buf + sizeof(struct gsm48_loc_area_id));
927 break;
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100928 case CELL_IDENT_WHOLE_GLOBAL_PS:
Pau Espin Pedrol52489852021-02-15 16:26:37 +0100929 /* 3GPP TS 48.018 sec 11.3.9 "Cell Identifier" */
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100930 if (len < 8)
931 return -EINVAL;
932 decode_lai(buf, (struct osmo_location_area_id *)&out->global_ps.rai); /* rai contains lai + non-decoded rac */
933 out->global_ps.rai.rac = *(buf + sizeof(struct gsm48_loc_area_id));
934 out->global_ps.cell_identity = osmo_load16be(buf + sizeof(struct gsm48_loc_area_id) + 1);
935 break;
Harald Welteef7be492019-05-03 21:01:13 +0200936 default:
937 /* Remaining cell identification types are not implemented. */
938 return -EINVAL;
939 }
940 return 0;
941}
942
Harald Weltee87693c2019-05-03 20:06:50 +0200943void gsm0808_msgb_put_cell_id_u(struct msgb *msg, enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
944{
945 switch (id_discr) {
946 case CELL_IDENT_WHOLE_GLOBAL: {
947 const struct osmo_cell_global_id *id = &u->global;
948 struct gsm48_loc_area_id lai;
949 gsm48_generate_lai2(&lai, &id->lai);
950 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
951 msgb_put_u16(msg, id->cell_identity);
952 break;
953 }
954 case CELL_IDENT_LAC_AND_CI: {
955 const struct osmo_lac_and_ci_id *id = &u->lac_and_ci;
956 msgb_put_u16(msg, id->lac);
957 msgb_put_u16(msg, id->ci);
958 break;
959 }
960 case CELL_IDENT_CI:
961 msgb_put_u16(msg, u->ci);
962 break;
963 case CELL_IDENT_LAI_AND_LAC: {
964 const struct osmo_location_area_id *id = &u->lai_and_lac;
965 struct gsm48_loc_area_id lai;
966 gsm48_generate_lai2(&lai, id);
967 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
968 break;
969 }
970 case CELL_IDENT_LAC:
971 msgb_put_u16(msg, u->lac);
972 break;
973 case CELL_IDENT_BSS:
974 case CELL_IDENT_NO_CELL:
975 /* Does not have any list items */
976 break;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +0100977
978 case CELL_IDENT_SAI: {
979 const struct osmo_service_area_id *id = &u->sai;
980 struct gsm48_loc_area_id lai;
981 gsm48_generate_lai2(&lai, &id->lai);
982 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
983 msgb_put_u16(msg, id->sac);
984 break;
985 }
986
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100987 case CELL_IDENT_WHOLE_GLOBAL_PS: {
Pau Espin Pedrol52489852021-02-15 16:26:37 +0100988 /* 3GPP TS 48.018 sec 11.3.9 "Cell Identifier" */
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100989 const struct osmo_cell_global_id_ps *id = &u->global_ps;
990 struct gsm48_loc_area_id lai;
991 gsm48_generate_lai2(&lai, &id->rai.lac);
992 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
993 memcpy(msgb_put(msg, 1), &id->rai.rac, 1);
994 msgb_put_u16(msg, id->cell_identity);
995 break;
996 }
Harald Weltee87693c2019-05-03 20:06:50 +0200997 default:
998 /* Support for other identifier list types is not implemented. */
999 OSMO_ASSERT(false);
1000 }
1001}
1002
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001003/*! Encode TS 08.08 Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +02001004 * \param[out] msg Message Buffer to which IE is to be appended
1005 * \param[in] cil Cell ID List to be encoded
1006 * \returns number of bytes appended to \a msg */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001007uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg,
1008 const struct gsm0808_cell_id_list2 *cil)
1009{
1010 uint8_t *old_tail;
1011 uint8_t *tlv_len;
1012 unsigned int i;
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001013 uint8_t id_discr;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001014
1015 OSMO_ASSERT(msg);
1016 OSMO_ASSERT(cil);
1017
1018 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
1019 tlv_len = msgb_put(msg, 1);
1020 old_tail = msg->tail;
1021
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001022 /* CGI-PS is an osmocom-specific type. In here we don't care about the
1023 * PS part, only the CS one. */
1024 if (cil->id_discr == CELL_IDENT_WHOLE_GLOBAL_PS)
1025 id_discr = CELL_IDENT_WHOLE_GLOBAL;
1026 else
1027 id_discr = cil->id_discr & 0x0f;
1028
1029 msgb_put_u8(msg, id_discr);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001030
Alexander Couzens4e284b62019-06-23 01:53:43 +02001031 OSMO_ASSERT(cil->id_list_len <= GSM0808_CELL_ID_LIST2_MAXLEN);
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001032 for (i = 0; i < cil->id_list_len; i++) {
1033 if (cil->id_discr == CELL_IDENT_WHOLE_GLOBAL_PS) {
1034 union gsm0808_cell_id_u u;
1035 cell_id_to_cgi(&u.global, cil->id_discr, &cil->id_list[i]);
1036 gsm0808_msgb_put_cell_id_u(msg, CELL_IDENT_WHOLE_GLOBAL, &u);
1037 } else {
1038 gsm0808_msgb_put_cell_id_u(msg, cil->id_discr, &cil->id_list[i]);
1039 }
1040 }
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001041
1042 *tlv_len = (uint8_t) (msg->tail - old_tail);
1043 return *tlv_len + 2;
1044}
1045
1046/*! DEPRECATED: Use gsm0808_enc_cell_id_list2 instead.
1047 *
1048 * Encode TS 08.08 Cell Identifier List IE
1049 * \param[out] msg Message Buffer to which IE is to be appended
1050 * \param[in] cil Cell ID List to be encoded
1051 * \returns number of bytes appended to \a msg */
Philipp Maier783047e2017-03-29 11:35:50 +02001052uint8_t gsm0808_enc_cell_id_list(struct msgb *msg,
1053 const struct gsm0808_cell_id_list *cil)
1054{
1055 uint8_t *old_tail;
1056 uint8_t *tlv_len;
1057 unsigned int i;
1058
1059 OSMO_ASSERT(msg);
1060 OSMO_ASSERT(cil);
1061
1062 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
1063 tlv_len = msgb_put(msg, 1);
1064 old_tail = msg->tail;
1065
1066 msgb_put_u8(msg, cil->id_discr & 0x0f);
1067
1068 switch (cil->id_discr) {
1069 case CELL_IDENT_LAC:
Alexander Couzens4e284b62019-06-23 01:53:43 +02001070 OSMO_ASSERT(cil->id_list_len <= CELL_ID_LIST_LAC_MAXLEN);
Philipp Maier783047e2017-03-29 11:35:50 +02001071 for (i=0;i<cil->id_list_len;i++) {
1072 msgb_put_u16(msg, cil->id_list_lac[i]);
1073 }
1074 break;
1075 case CELL_IDENT_BSS:
1076 /* Does not have any list items */
1077 break;
1078 default:
1079 /* FIXME: Implement support for all identifier list elements */
1080 OSMO_ASSERT(false);
1081 }
1082
1083 *tlv_len = (uint8_t) (msg->tail - old_tail);
1084 return *tlv_len + 2;
1085}
1086
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001087static 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 +01001088 size_t *consumed)
1089{
1090 struct osmo_cell_global_id *id;
1091 uint16_t *ci_be;
1092 size_t lai_offset;
1093 int i = 0;
1094 const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*ci_be);
1095
1096 *consumed = 0;
1097 while (remain >= elemlen) {
1098 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1099 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001100 id = &cil->id_list[i].global;
Stefan Sperling2873bf12018-03-14 18:38:41 +01001101 lai_offset = i * elemlen;
Stefan Sperling23381452018-03-15 19:38:15 +01001102 decode_lai(&data[lai_offset], &id->lai);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001103 ci_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]);
1104 id->cell_identity = osmo_load16be(ci_be);
1105 *consumed += elemlen;
1106 remain -= elemlen;
1107 i++;
1108 }
1109
1110 return i;
1111}
1112
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001113static 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 +01001114 size_t *consumed)
1115{
1116 uint16_t *lacp_be, *ci_be;
1117 struct osmo_lac_and_ci_id *id;
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001118 int i = 0, j = 0;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001119 const size_t elemlen = sizeof(*lacp_be) + sizeof(*ci_be);
1120
1121 *consumed = 0;
1122
1123 if (remain < elemlen)
1124 return -EINVAL;
1125
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001126 lacp_be = (uint16_t *)(&data[j]);
1127 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001128 while (remain >= elemlen) {
1129 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1130 return -ENOSPC;
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001131 id = &cil->id_list[i++].lac_and_ci;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001132 id->lac = osmo_load16be(lacp_be);
1133 id->ci = osmo_load16be(ci_be);
1134 *consumed += elemlen;
1135 remain -= elemlen;
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001136 j += elemlen;
1137 lacp_be = (uint16_t *)(&data[j]);
1138 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001139 }
1140
1141 return i;
1142}
1143
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001144static int parse_cell_id_ci_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain,
1145 size_t *consumed)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001146{
1147 const uint16_t *ci_be = (const uint16_t *)data;
1148 int i = 0;
1149 const size_t elemlen = sizeof(*ci_be);
1150
1151 *consumed = 0;
1152 while (remain >= elemlen) {
1153 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1154 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001155 cil->id_list[i++].ci = osmo_load16be(ci_be++);
Stefan Sperling9c62fc62018-03-16 10:23:34 +01001156 *consumed += elemlen;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001157 remain -= elemlen;
1158 }
1159 return i;
1160}
1161
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001162static 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 +01001163 size_t *consumed)
1164{
1165 struct osmo_location_area_id *id;
1166 int i = 0;
1167 const size_t elemlen = sizeof(struct gsm48_loc_area_id);
1168
1169 *consumed = 0;
1170 while (remain >= elemlen) {
1171 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1172 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001173 id = &cil->id_list[i].lai_and_lac;
Stefan Sperling23381452018-03-15 19:38:15 +01001174 decode_lai(&data[i * elemlen], id);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001175 *consumed += elemlen;
1176 remain -= elemlen;
1177 i++;
1178 }
1179
1180 return i;
1181}
1182
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001183static 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 +01001184{
1185 const uint16_t *lac_be = (const uint16_t *)data;
1186 int i = 0;
1187 const size_t elemlen = sizeof(*lac_be);
1188
1189 *consumed = 0;
1190 while (remain >= elemlen) {
1191 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1192 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001193 cil->id_list[i++].lac = osmo_load16be(lac_be++);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001194 *consumed += elemlen;
1195 remain -= elemlen;
1196 }
1197 return i;
1198}
1199
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001200static int parse_cell_id_sai_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain, size_t *consumed)
1201{
1202 struct osmo_service_area_id *id;
1203 uint16_t *sac_be;
1204 size_t lai_offset;
1205 int i = 0;
1206 const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*sac_be);
1207
1208 *consumed = 0;
1209 while (remain >= elemlen) {
1210 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1211 return -ENOSPC;
1212 id = &cil->id_list[i].sai;
1213 lai_offset = i * elemlen;
1214 decode_lai(&data[lai_offset], &id->lai);
1215 sac_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]);
1216 id->sac = osmo_load16be(sac_be);
1217 *consumed += elemlen;
1218 remain -= elemlen;
1219 i++;
1220 }
1221
1222 return i;
1223}
1224
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001225/*! Decode Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +02001226 * \param[out] cil Caller-provided memory to store Cell ID list
1227 * \param[in] elem IE value to be decoded
1228 * \param[in] len Length of \a elem in bytes
1229 * \returns number of bytes parsed; negative on error */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001230int gsm0808_dec_cell_id_list2(struct gsm0808_cell_id_list2 *cil,
1231 const uint8_t *elem, uint8_t len)
1232{
1233 uint8_t id_discr;
1234 size_t bytes_elem = 0;
1235 int list_len = 0;
1236
1237 OSMO_ASSERT(cil);
1238 if (!elem)
1239 return -EINVAL;
1240 if (len == 0)
1241 return -EINVAL;
1242
1243 memset(cil, 0, sizeof(*cil));
1244
1245 id_discr = *elem & 0x0f;
1246 elem++;
1247 len--;
1248
1249 switch (id_discr) {
1250 case CELL_IDENT_WHOLE_GLOBAL:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001251 list_len = parse_cell_id_global_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001252 break;
1253 case CELL_IDENT_LAC_AND_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001254 list_len = parse_cell_id_lac_and_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001255 break;
1256 case CELL_IDENT_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001257 list_len = parse_cell_id_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001258 break;
1259 case CELL_IDENT_LAI_AND_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001260 list_len = parse_cell_id_lai_and_lac(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001261 break;
1262 case CELL_IDENT_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001263 list_len = parse_cell_id_lac_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001264 break;
1265 case CELL_IDENT_BSS:
1266 case CELL_IDENT_NO_CELL:
1267 /* Does not have any list items */
1268 break;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001269 case CELL_IDENT_SAI:
1270 list_len = parse_cell_id_sai_list(cil, elem, len, &bytes_elem);
1271 break;
1272 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1273 case CELL_IDENT_UTRAN_RNC:
1274 case CELL_IDENT_UTRAN_LAC_RNC:
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001275 default:
1276 /* Remaining cell identification types are not implemented. */
1277 return -EINVAL;
1278 }
1279
1280 if (list_len < 0) /* parsing error */
1281 return list_len;
1282
1283 cil->id_discr = id_discr;
1284 cil->id_list_len = list_len;
1285
1286 /* One byte for the cell ID discriminator + any remaining bytes in
1287 * the IE which were consumed by the parser functions above. */
1288 return 1 + (int)bytes_elem;
1289}
1290
1291/*! DEPRECATED: Use gsm0808_dec_cell_id_list2 instead.
1292 *
1293 * Decode Cell Identifier List IE
1294 * \param[out] cil Caller-provided memory to store Cell ID list
1295 * \param[in] elem IE value to be decoded
1296 * \param[in] len Length of \a elem in bytes
1297 * \returns number of bytes parsed; negative on error */
Philipp Maier783047e2017-03-29 11:35:50 +02001298int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil,
1299 const uint8_t *elem, uint8_t len)
1300{
1301 uint8_t id_discr;
1302 const uint8_t *old_elem = elem;
1303 unsigned int item_count = 0;
1304
1305 OSMO_ASSERT(cil);
1306 if (!elem)
1307 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +02001308 if (len == 0)
Philipp Maier783047e2017-03-29 11:35:50 +02001309 return -EINVAL;
1310
1311 memset(cil, 0, sizeof(*cil));
1312
1313 id_discr = *elem & 0x0f;
1314 elem++;
1315 len--;
1316
1317 cil->id_discr = id_discr;
1318
1319 switch (id_discr) {
1320 case CELL_IDENT_LAC:
1321 while (len >= 2) {
1322 cil->id_list_lac[item_count] = osmo_load16be(elem);
1323 elem += 2;
1324 item_count++;
1325 len -= 2;
1326 }
1327 case CELL_IDENT_BSS:
1328 /* Does not have any list items */
1329 break;
1330 default:
1331 /* FIXME: Implement support for all identifier list elements */
1332 return -EINVAL;
1333 }
1334
1335 cil->id_list_len = item_count;
1336 return (int)(elem - old_elem);
1337}
Harald Welte96e2a002017-06-12 21:44:18 +02001338
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001339static bool same_cell_id_list_entries(const struct gsm0808_cell_id_list2 *a, int ai,
1340 const struct gsm0808_cell_id_list2 *b, int bi)
1341{
1342 struct gsm0808_cell_id_list2 tmp = {
1343 .id_discr = a->id_discr,
1344 .id_list_len = 1,
1345 };
1346 uint8_t buf_a[32 + sizeof(struct msgb)];
1347 uint8_t buf_b[32 + sizeof(struct msgb)];
1348 struct msgb *msg_a = (void*)buf_a;
1349 struct msgb *msg_b = (void*)buf_b;
1350
1351 msg_a->data_len = 32;
1352 msg_b->data_len = 32;
1353 msgb_reset(msg_a);
1354 msgb_reset(msg_b);
1355
1356 if (a->id_discr != b->id_discr)
1357 return false;
1358 if (ai >= a->id_list_len
1359 || bi >= b->id_list_len)
1360 return false;
1361
1362 tmp.id_list[0] = a->id_list[ai];
1363 gsm0808_enc_cell_id_list2(msg_a, &tmp);
1364
1365 tmp.id_list[0] = b->id_list[bi];
1366 gsm0808_enc_cell_id_list2(msg_b, &tmp);
1367
1368 if (msg_a->len != msg_b->len)
1369 return false;
1370 if (memcmp(msg_a->data, msg_b->data, msg_a->len))
1371 return false;
1372
1373 return true;
1374}
1375
1376/*! Append entries from one Cell Identifier List to another.
1377 * The cell identifier types must be identical between the two lists.
1378 * \param dst[out] Append entries to this list.
1379 * \param src[in] Append these entries to \a dst.
1380 * \returns the nr of items added, or negative on error: -EINVAL if the id_discr mismatch
1381 * between the lists, -ENOSPC if the destination list does not have enough space. If an error is
1382 * returned, \a dst may have already been changed (particularly on -ENOSPC). Note that a return value
1383 * of zero may occur when the src->id_list_len is zero, or when all entries from \a src already exist
1384 * in \a dst, and does not indicate error per se. */
1385int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src)
1386{
1387 int i, j;
1388 int added = 0;
1389
1390 if (dst->id_list_len == 0
1391 && dst->id_discr != CELL_IDENT_BSS)
1392 dst->id_discr = src->id_discr;
1393 else if (dst->id_discr != src->id_discr)
1394 return -EINVAL;
1395
1396 for (i = 0; i < src->id_list_len; i++) {
1397 /* don't add duplicate entries */
1398 bool skip = false;
1399 for (j = 0; j < dst->id_list_len; j++) {
1400 if (same_cell_id_list_entries(dst, j, src, i)) {
1401 skip = true;
1402 break;
1403 }
1404 }
1405 if (skip)
1406 continue;
1407
1408 if (dst->id_list_len >= ARRAY_SIZE(dst->id_list))
1409 return -ENOSPC;
1410
1411 dst->id_list[dst->id_list_len++] = src->id_list[i];
1412 added ++;
1413 }
1414
1415 return added;
1416}
1417
Neels Hofmeyr38e58412018-05-25 16:56:35 +02001418/*! Convert a single Cell Identifier to a Cell Identifier List with one entry.
1419 * \param dst[out] Overwrite this list.
1420 * \param src[in] Set \a dst to contain exactly this item.
1421 */
1422void gsm0808_cell_id_to_list(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id *src)
1423{
1424 if (!dst)
1425 return;
1426 if (!src) {
1427 *dst = (struct gsm0808_cell_id_list2){
1428 .id_discr = CELL_IDENT_NO_CELL,
1429 };
1430 return;
1431 }
1432
1433 *dst = (struct gsm0808_cell_id_list2){
1434 .id_discr = src->id_discr,
1435 .id_list = { src->id },
1436 .id_list_len = 1,
1437 };
1438
1439 switch (src->id_discr) {
1440 case CELL_IDENT_NO_CELL:
1441 case CELL_IDENT_BSS:
1442 dst->id_list_len = 0;
1443 break;
1444 default:
1445 break;
1446 }
1447}
1448
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001449/*! Encode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1450 * \param[out] msg Message Buffer to which IE is to be appended
1451 * \param[in] ci Cell ID to be encoded
1452 * \returns number of bytes appended to \a msg */
1453uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci)
1454{
1455 uint8_t rc;
1456 uint8_t *ie_tag;
1457 struct gsm0808_cell_id_list2 cil = {
1458 .id_discr = ci->id_discr,
1459 .id_list = { ci->id },
1460 .id_list_len = 1,
1461 };
1462
1463 OSMO_ASSERT(msg);
1464 OSMO_ASSERT(ci);
1465
1466 ie_tag = msg->tail;
1467 rc = gsm0808_enc_cell_id_list2(msg, &cil);
1468
1469 if (rc <= 0)
1470 return rc;
1471
1472 *ie_tag = GSM0808_IE_CELL_IDENTIFIER;
1473 return rc;
1474}
1475
1476/*! Decode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1477 * \param[out] ci Caller-provided memory to store Cell ID.
1478 * \param[in] elem IE value to be decoded.
1479 * \param[in] len Length of \a elem in bytes.
1480 * \returns number of bytes parsed; negative on error */
1481int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len)
1482{
1483 struct gsm0808_cell_id_list2 cil;
1484 int rc;
1485 rc = gsm0808_dec_cell_id_list2(&cil, elem, len);
1486 if (rc < 0)
1487 return rc;
1488 if (cil.id_discr == CELL_IDENT_BSS || cil.id_discr == CELL_IDENT_NO_CELL) {
1489 if (cil.id_list_len != 0)
1490 return -EINVAL;
1491 } else {
1492 if (cil.id_list_len != 1)
1493 return -EINVAL;
1494 }
1495 ci->id_discr = cil.id_discr;
1496 ci->id = cil.id_list[0];
1497 return rc;
1498}
1499
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001500/*! Convert the representation of the permitted speech codec identifier
Philipp Maier3149b0d2017-06-02 13:22:34 +02001501 * that is used in struct gsm0808_channel_type to the speech codec
1502 * representation we use in struct gsm0808_speech_codec.
1503 * \param[in] perm_spch to be converted (see also gsm0808_permitted_speech)
1504 * \returns GSM speech codec type; negative on error */
1505int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch)
1506{
1507 /*! The speech codec type, which is used in the channel type field to
1508 * signal the permitted speech versions (codecs) has a different
1509 * encoding than the type field in the speech codec type element
1510 * (See also 3GPP TS 48.008, 3.2.2.11 and 3.2.2.103) */
1511
1512 switch (perm_spch) {
1513 case GSM0808_PERM_FR1:
1514 return GSM0808_SCT_FR1;
1515 case GSM0808_PERM_FR2:
1516 return GSM0808_SCT_FR2;
1517 case GSM0808_PERM_FR3:
1518 return GSM0808_SCT_FR3;
1519 case GSM0808_PERM_FR4:
1520 return GSM0808_SCT_FR4;
1521 case GSM0808_PERM_FR5:
1522 return GSM0808_SCT_FR5;
1523 case GSM0808_PERM_HR1:
1524 return GSM0808_SCT_HR1;
1525 case GSM0808_PERM_HR3:
1526 return GSM0808_SCT_HR3;
1527 case GSM0808_PERM_HR4:
1528 return GSM0808_SCT_HR4;
1529 case GSM0808_PERM_HR6:
1530 return GSM0808_SCT_HR6;
1531 }
1532
1533 /* Invalid input */
1534 return -EINVAL;
1535}
1536
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001537/*! Extrapolate a speech codec field from a given permitted speech
Philipp Maier884ba0f2017-06-02 13:49:16 +02001538 * parameter (channel type).
1539 * \param[out] sc Caller provided memory to store the resulting speech codec
1540 * \param[in] perm_spch value that is used to derive the speech codec info
1541 * (see also: enum gsm0808_speech_codec_type in gsm0808_utils.h)
1542 * \returns zero when successful; negative on error */
1543int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc,
1544 uint8_t perm_spch)
1545{
1546 int rc;
1547
1548 memset(sc, 0, sizeof(*sc));
1549
1550 /* Determine codec type */
1551 rc = gsm0808_chan_type_to_speech_codec(perm_spch);
1552 if (rc < 0)
1553 return -EINVAL;
1554 sc->type = (uint8_t) rc;
1555
1556 /* Depending on the speech codec type, pick a default codec
1557 * configuration that exactly matches the configuration on the
1558 * air interface. */
1559 switch (sc->type) {
1560 case GSM0808_SCT_FR3:
1561 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR;
1562 break;
1563 case GSM0808_SCT_FR4:
1564 sc->cfg = GSM0808_SC_CFG_DEFAULT_OFR_AMR_WB;
1565 break;
1566 case GSM0808_SCT_FR5:
1567 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR_WB;
1568 break;
1569 case GSM0808_SCT_HR3:
1570 sc->cfg = GSM0808_SC_CFG_DEFAULT_HR_AMR;
1571 break;
1572 case GSM0808_SCT_HR4:
1573 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR_WB;
1574 break;
1575 case GSM0808_SCT_HR6:
1576 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR;
1577 break;
1578 default:
1579 /* Note: Not all codec types specify a default setting,
1580 * in this case, we just set the field to zero. */
1581 sc->cfg = 0;
1582 }
1583
1584 /* Tag all codecs as "Full IP"
1585 * (see als 3GPP TS 48.008 3.2.2.103) */
1586 sc->fi = true;
1587
1588 return 0;
1589}
1590
Philipp Maier5f2eb152018-09-19 13:40:21 +02001591/*! Determine a set of AMR speech codec configuration bits (S0-S15) from a
1592 * given GSM 04.08 AMR configuration struct.
1593 * \param[in] cfg AMR configuration in GSM 04.08 format.
1594 * \param[in] hint if the resulting configuration shall be used with a FR or HR TCH.
1595 * \returns configuration bits (S0-S15) */
Philipp Maier369015c2018-09-21 09:07:20 +02001596uint16_t gsm0808_sc_cfg_from_gsm48_mr_cfg(const struct gsm48_multi_rate_conf *cfg,
Philipp Maier5f2eb152018-09-19 13:40:21 +02001597 bool fr)
1598{
1599 uint16_t s15_s0 = 0;
1600
1601 /* Check each rate bit in the AMR multirate configuration and pick the
1602 * matching default configuration as specified in 3GPP TS 28.062,
1603 * Table 7.11.3.1.3-2. */
1604 if (cfg->m4_75)
1605 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_4_75;
1606 if (cfg->m5_15)
1607 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_15;
1608 if (cfg->m5_90)
1609 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_90;
1610 if (cfg->m6_70)
1611 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_6_70;
1612 if (cfg->m7_40)
1613 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_40;
1614 if (cfg->m7_95)
1615 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_95;
1616 if (cfg->m10_2)
1617 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_10_2;
1618 if (cfg->m12_2)
1619 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_12_2;
1620
1621 /* Note: 3GPP TS 48.008, chapter 3GPP TS 48.008 states that for AMR
1622 * some of the configuration bits must be coded as zeros. The applied
1623 * bitmask matches the default codec settings. See also the definition
1624 * of enum gsm0808_speech_codec_defaults in gsm_08_08.h and
1625 * 3GPP TS 28.062, Table 7.11.3.1.3-2. */
1626 if (fr)
1627 s15_s0 &= GSM0808_SC_CFG_DEFAULT_FR_AMR;
1628 else
1629 s15_s0 &= GSM0808_SC_CFG_DEFAULT_HR_AMR;
1630
Philipp Maier94d79fd2019-03-01 10:40:48 +01001631 /* The mode that is encoded by S1 (Config-NB-Code = 1), takes a special
1632 * role as it does not stand for a single rate, but for up to four rates
1633 * at once (12.2, 7.4, 5.9, 4.75). We must check if the supplied cfg
1634 * covers this mode. If not, we need to make sure that the related
1635 * bit is removed. (See also 3GPP TS 28.062, Table 7.11.3.1.3-2) */
1636 if (!(cfg->m12_2 && cfg->m7_40 && cfg->m5_90 && cfg->m4_75) && fr)
1637 s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20;
1638 else if (!(cfg->m7_40 && cfg->m5_90 && cfg->m4_75))
1639 s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20;
1640
Philipp Maier5f2eb152018-09-19 13:40:21 +02001641 return s15_s0;
1642}
1643
Philipp Maier8515d032018-09-25 15:57:49 +02001644/*! Determine a GSM 04.08 AMR configuration struct from a set of speech codec
1645 * configuration bits (S0-S15)
1646 * \param[out] cfg AMR configuration in GSM 04.08 format.
Philipp Maier3713af82019-02-27 16:48:25 +01001647 * \param[in] s15_s0 configuration bits (S15-S0, non-ambiguous).
1648 * \returns zero when successful; negative on error */
1649int gsm48_mr_cfg_from_gsm0808_sc_cfg(struct gsm48_multi_rate_conf *cfg,
1650 uint16_t s15_s0)
Philipp Maier8515d032018-09-25 15:57:49 +02001651{
Philipp Maier3713af82019-02-27 16:48:25 +01001652 unsigned int count = 0;
1653
1654 /* Note: See also: 3GPP TS 28.062
1655 * Table 7.11.3.1.3-2: Preferred Configurations for the Adaptive
1656 * Multi-Rate Codec Types */
1657
1658 /* Note: The resulting multirate-configuration must not contain an
1659 * active set of more than four codec rates. The active set also
1660 * must contain at least one rate. */
1661
Philipp Maier8515d032018-09-25 15:57:49 +02001662 memset(cfg, 0, sizeof(*cfg));
Philipp Maier3713af82019-02-27 16:48:25 +01001663 cfg->ver = 1;
1664 cfg->icmi = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001665
1666 /* Strip option bits */
1667 s15_s0 &= 0x00ff;
1668
Philipp Maier3713af82019-02-27 16:48:25 +01001669 /* Rate 5,15k can never be selected (see table) */
1670 cfg->m5_15 = 0;
Philipp Maier8515d032018-09-25 15:57:49 +02001671
Neels Hofmeyra0f2b212021-04-14 22:45:13 +02001672 if (s15_s0 & GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20) {
Philipp Maier3713af82019-02-27 16:48:25 +01001673 /* Table Table 7.11.3.1.3-2 lists one mode that selects 4
1674 * rates at once (Config-NB-Code = 1). The rates selected
1675 * are known to be compatible between GERAN and UTRAN, since
1676 * an active set must not contain more than four rates at
1677 * a time, we ignore all other settings as they are either
1678 * redundaned or excess settings (invalid) */
Philipp Maier8515d032018-09-25 15:57:49 +02001679 cfg->m4_75 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001680 cfg->m5_90 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001681 cfg->m7_40 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001682 cfg->m12_2 = 1;
Philipp Maier3713af82019-02-27 16:48:25 +01001683 count += 4;
1684 }
Philipp Maier8515d032018-09-25 15:57:49 +02001685
Philipp Maier3713af82019-02-27 16:48:25 +01001686 /* Check the bits in s15_s0 and set the flags for the
1687 * respective rates. */
1688 if (s15_s0 & GSM0808_SC_CFG_AMR_4_75 && !cfg->m4_75) {
1689 if (count >= 4)
1690 return -EINVAL;
1691 cfg->m4_75 = 1;
1692 count++;
1693 }
1694 if (s15_s0 & GSM0808_SC_CFG_AMR_5_90 && !cfg->m5_90) {
1695 if (count >= 4)
1696 return -EINVAL;
1697 cfg->m5_90 = 1;
1698 count++;
1699 }
1700 if (s15_s0 & GSM0808_SC_CFG_AMR_6_70) {
1701 if (count >= 4)
1702 return -EINVAL;
1703 cfg->m6_70 = 1;
1704 count++;
1705 }
1706 if (s15_s0 & GSM0808_SC_CFG_AMR_7_40 && !cfg->m7_40) {
1707 if (count >= 4)
1708 return -EINVAL;
1709 cfg->m7_40 = 1;
1710 count++;
1711 }
1712 if (s15_s0 & GSM0808_SC_CFG_AMR_7_95) {
1713 if (count >= 4)
1714 return -EINVAL;
1715 cfg->m7_95 = 1;
1716 count++;
1717 }
1718 if (s15_s0 & GSM0808_SC_CFG_AMR_10_2) {
1719 if (count >= 4)
1720 return -EINVAL;
1721 cfg->m10_2 = 1;
1722 count++;
1723 }
1724 if (s15_s0 & GSM0808_SC_CFG_AMR_12_2 && !cfg->m12_2) {
1725 if (count >= 4)
1726 return -EINVAL;
1727 cfg->m12_2 = 1;
1728 count++;
1729 }
1730
1731 if (count == 0)
1732 return -EINVAL;
1733
1734 return 0;
Philipp Maier8515d032018-09-25 15:57:49 +02001735}
1736
Alexander Chemeris2ac8f912020-05-13 22:38:08 +03001737int gsm0808_get_cipher_reject_cause(const struct tlv_parsed *tp)
1738{
1739 return gsm0808_get_cause(tp);
1740}
1741
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001742/*! Print a human readable name of the cell identifier to the char buffer.
1743 * This is useful both for struct gsm0808_cell_id and struct gsm0808_cell_id_list2.
1744 * See also gsm0808_cell_id_name() and gsm0808_cell_id_list_name().
1745 * \param[out] buf Destination buffer to write string representation to.
1746 * \param[in] buflen Amount of memory available in \a buf.
1747 * \param[in] id_discr Cell Identifier type.
1748 * \param[in] u Cell Identifer value.
1749 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1750 * or that would have been written if the buffer were large enough.
1751 */
1752int gsm0808_cell_id_u_name(char *buf, size_t buflen,
1753 enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
1754{
1755 switch (id_discr) {
1756 case CELL_IDENT_LAC:
1757 return snprintf(buf, buflen, "%u", u->lac);
1758 case CELL_IDENT_CI:
1759 return snprintf(buf, buflen, "%u", u->ci);
1760 case CELL_IDENT_LAC_AND_CI:
1761 return snprintf(buf, buflen, "%u-%u", u->lac_and_ci.lac, u->lac_and_ci.ci);
1762 case CELL_IDENT_LAI_AND_LAC:
1763 return snprintf(buf, buflen, "%s", osmo_lai_name(&u->lai_and_lac));
1764 case CELL_IDENT_WHOLE_GLOBAL:
1765 return snprintf(buf, buflen, "%s", osmo_cgi_name(&u->global));
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01001766 case CELL_IDENT_WHOLE_GLOBAL_PS:
1767 return snprintf(buf, buflen, "%s", osmo_cgi_ps_name(&u->global_ps));
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001768 case CELL_IDENT_SAI:
1769 return snprintf(buf, buflen, "%s", osmo_sai_name(&u->sai));
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001770 default:
1771 /* For CELL_IDENT_BSS and CELL_IDENT_NO_CELL, just print the discriminator.
1772 * Same for kinds we have no string representation of yet. */
1773 return snprintf(buf, buflen, "%s", gsm0808_cell_id_discr_name(id_discr));
1774 }
1775}
1776
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02001777/*! Return true if the common information between the two Cell Identifiers match.
1778 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1779 * Note that CELL_IDENT_NO_CELL will always return false.
1780 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1781 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1782 * \param[in] discr1 Cell Identifier type.
1783 * \param[in] u1 Cell Identifier value.
1784 * \param[in] discr2 Other Cell Identifier type.
1785 * \param[in] u2 Other Cell Identifier value.
1786 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1787 * \returns True if the common fields of the above match.
1788 */
1789static bool gsm0808_cell_id_u_match(enum CELL_IDENT discr1, const union gsm0808_cell_id_u *u1,
1790 enum CELL_IDENT discr2, const union gsm0808_cell_id_u *u2,
1791 bool exact_match)
1792{
1793 struct osmo_cell_global_id a = {};
1794 struct osmo_cell_global_id b = {};
1795
1796 if (exact_match && discr1 != discr2)
1797 return false;
1798
1799 /* First handle the odd wildcard like CELL_IDENT kinds. We can't really match any of these. */
1800 switch (discr1) {
1801 case CELL_IDENT_NO_CELL:
1802 case CELL_IDENT_BSS:
1803 return discr1 == discr2;
1804 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1805 case CELL_IDENT_UTRAN_RNC:
1806 case CELL_IDENT_UTRAN_LAC_RNC:
1807 return false;
1808 default:
1809 break;
1810 }
1811 switch (discr2) {
1812 case CELL_IDENT_NO_CELL:
1813 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1814 case CELL_IDENT_UTRAN_RNC:
1815 case CELL_IDENT_UTRAN_LAC_RNC:
1816 case CELL_IDENT_BSS:
1817 return false;
1818 default:
1819 break;
1820 }
1821
1822 /* Enrich both sides to full CGI, then compare those. First set the *other* ID's values in case
1823 * they assign more items. For example:
1824 * u1 = LAC:42
1825 * u2 = LAC+CI:23+5
1826 * 1) a <- LAC+CI:23+5
1827 * 2) a <- LAC:42 so that a = LAC+CI:42+5
1828 * Now we can compare those two and find a mismatch. If the LAC were the same, we would get
1829 * identical LAC+CI and hence a match. */
1830
1831 cell_id_to_cgi(&a, discr2, u2);
1832 cell_id_to_cgi(&a, discr1, u1);
1833
1834 cell_id_to_cgi(&b, discr1, u1);
1835 cell_id_to_cgi(&b, discr2, u2);
1836
1837 return osmo_cgi_cmp(&a, &b) == 0;
1838}
1839
1840/*! Return true if the common information between the two Cell Identifiers match.
1841 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1842 * Note that CELL_IDENT_NO_CELL will always return false.
1843 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1844 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1845 * \param[in] id1 Cell Identifier.
1846 * \param[in] id2 Other Cell Identifier.
1847 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1848 * \returns True if the common fields of the above match.
1849 */
1850bool gsm0808_cell_ids_match(const struct gsm0808_cell_id *id1, const struct gsm0808_cell_id *id2, bool exact_match)
1851{
1852 return gsm0808_cell_id_u_match(id1->id_discr, &id1->id, id2->id_discr, &id2->id, exact_match);
1853}
1854
1855/*! Find an index in a Cell Identifier list that matches a given single Cell Identifer.
1856 * Compare \a id against each entry in \a list using gsm0808_cell_ids_match(), and return the list index
1857 * if a match is found. \a match_nr allows iterating all matches in the list. A match_nr <= 0 returns the
1858 * first match in the list, match_nr == 1 the second match, etc., and if match_nr exceeds the available
1859 * matches in the list, -1 is returned.
1860 * \param[in] id Cell Identifier to match.
1861 * \param[in] list Cell Identifier list to search in.
1862 * \param[in] match_nr Ignore this many matches.
1863 * \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 +01001864 * \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
1865 * entry).
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02001866 */
1867int gsm0808_cell_id_matches_list(const struct gsm0808_cell_id *id, const struct gsm0808_cell_id_list2 *list,
1868 unsigned int match_nr, bool exact_match)
1869{
1870 int i;
1871 for (i = 0; i < list->id_list_len; i++) {
1872 if (gsm0808_cell_id_u_match(id->id_discr, &id->id, list->id_discr, &list->id_list[i], exact_match)) {
1873 if (match_nr)
1874 match_nr--;
1875 else
1876 return i;
1877 }
1878 }
1879 return -1;
1880}
1881
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001882/*! Copy information from a CGI to form a Cell Identifier of the specified kind.
1883 * \param [out] cid Compose new Cell Identifier here.
1884 * \param [in] id_discr Which kind of Cell Identifier to compose.
1885 * \param [in] cgi Cell Global Identifier to form the Cell Identifier from.
1886 */
1887void gsm0808_cell_id_from_cgi(struct gsm0808_cell_id *cid, enum CELL_IDENT id_discr,
1888 const struct osmo_cell_global_id *cgi)
1889{
1890 *cid = (struct gsm0808_cell_id){
1891 .id_discr = id_discr,
1892 };
1893
1894 switch (id_discr) {
1895 case CELL_IDENT_WHOLE_GLOBAL:
1896 cid->id.global = *cgi;
1897 return;
1898
Pau Espin Pedrol20b763d2021-02-15 16:06:22 +01001899 case CELL_IDENT_WHOLE_GLOBAL_PS:
1900 cid->id.global_ps = (struct osmo_cell_global_id_ps){
1901 .rai = {
1902 .lac = cgi->lai,
1903 },
1904 .cell_identity = cgi->cell_identity,
1905 };
1906 return;
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001907 case CELL_IDENT_LAC_AND_CI:
1908 cid->id.lac_and_ci = (struct osmo_lac_and_ci_id){
1909 .lac = cgi->lai.lac,
1910 .ci = cgi->cell_identity,
1911 };
1912 return;
1913
1914 case CELL_IDENT_CI:
1915 cid->id.ci = cgi->cell_identity;
1916 return;
1917
1918 case CELL_IDENT_LAI:
1919 cid->id.lai_and_lac = cgi->lai;
1920 return;
1921
1922 case CELL_IDENT_LAC:
1923 cid->id.lac = cgi->lai.lac;
1924 return;
1925
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001926 case CELL_IDENT_SAI:
1927 cid->id.sai = (struct osmo_service_area_id){
1928 .lai = cgi->lai,
1929 };
1930 return;
1931
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001932 case CELL_IDENT_NO_CELL:
1933 case CELL_IDENT_BSS:
1934 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1935 case CELL_IDENT_UTRAN_RNC:
1936 case CELL_IDENT_UTRAN_LAC_RNC:
1937 default:
1938 return;
1939 };
1940}
1941
1942/*! Overwrite parts of cgi with values from a Cell Identifier.
1943 * Place only those items given in cid into cgi, leaving other values unchanged.
1944 * \param[out] cgi Cell Global Identity to write to.
1945 * \param[in] cid Cell Identity to read from.
1946 * \return a bitmask of items that were set: OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI; 0 if nothing was
1947 * written to cgi.
1948 */
1949int gsm0808_cell_id_to_cgi(struct osmo_cell_global_id *cgi, const struct gsm0808_cell_id *cid)
1950{
1951 switch (cid->id_discr) {
1952 case CELL_IDENT_WHOLE_GLOBAL:
1953 *cgi = cid->id.global;
1954 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
1955
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01001956 case CELL_IDENT_WHOLE_GLOBAL_PS:
1957 cgi->lai = cid->id.global_ps.rai.lac;
1958 cgi->cell_identity = cid->id.global_ps.cell_identity;
1959 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
1960
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001961 case CELL_IDENT_LAC_AND_CI:
1962 cgi->lai.lac = cid->id.lac_and_ci.lac;
1963 cgi->cell_identity = cid->id.lac_and_ci.ci;
1964 return OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
1965
1966 case CELL_IDENT_CI:
1967 cgi->cell_identity = cid->id.ci;
1968 return OSMO_CGI_PART_CI;
1969
1970 case CELL_IDENT_LAI:
1971 cgi->lai = cid->id.lai_and_lac;
1972 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC;
1973
1974 case CELL_IDENT_LAC:
1975 cgi->lai.lac = cid->id.lac;
1976 return OSMO_CGI_PART_LAC;
1977
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001978 case CELL_IDENT_SAI:
1979 cgi->lai = cid->id.sai.lai;
1980 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC;
1981
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001982 case CELL_IDENT_NO_CELL:
1983 case CELL_IDENT_BSS:
1984 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1985 case CELL_IDENT_UTRAN_RNC:
1986 case CELL_IDENT_UTRAN_LAC_RNC:
1987 default:
1988 return 0;
1989 };
1990}
1991
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001992/*! value_string[] for enum CELL_IDENT. */
1993const struct value_string gsm0808_cell_id_discr_names[] = {
1994 { CELL_IDENT_WHOLE_GLOBAL, "CGI" },
1995 { CELL_IDENT_LAC_AND_CI, "LAC-CI" },
1996 { CELL_IDENT_CI, "CI" },
1997 { CELL_IDENT_NO_CELL, "NO-CELL" },
1998 { CELL_IDENT_LAI_AND_LAC, "LAI" },
1999 { CELL_IDENT_LAC, "LAC" },
2000 { CELL_IDENT_BSS, "BSS" },
2001 { CELL_IDENT_UTRAN_PLMN_LAC_RNC, "UTRAN-PLMN-LAC-RNC" },
2002 { CELL_IDENT_UTRAN_RNC, "UTRAN-RNC" },
2003 { CELL_IDENT_UTRAN_LAC_RNC, "UTRAN-LAC-RNC" },
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01002004 { CELL_IDENT_SAI, "SAI" },
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01002005 { CELL_IDENT_WHOLE_GLOBAL_PS, "CGI-PS"},
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002006 { 0, NULL }
2007};
2008
2009#define APPEND_THING(func, args...) do { \
2010 int remain = buflen - (pos - buf); \
2011 int l = func(pos, remain, ##args); \
2012 if (l < 0 || l > remain) \
2013 pos = buf + buflen; \
2014 else \
2015 pos += l; \
2016 if (l > 0) \
2017 total_len += l; \
2018 } while(0)
2019#define APPEND_STR(fmt, args...) APPEND_THING(snprintf, fmt, ##args)
2020#define APPEND_CELL_ID_U(DISCR, U) APPEND_THING(gsm0808_cell_id_u_name, DISCR, U)
2021
Harald Welte179f3572019-03-18 18:38:47 +01002022char *gsm0808_cell_id_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id *cid)
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002023{
2024 char *pos = buf;
2025 int total_len = 0;
2026 APPEND_STR("%s:", gsm0808_cell_id_discr_name(cid->id_discr));
2027 APPEND_CELL_ID_U(cid->id_discr, &cid->id);
2028 return buf;
2029}
2030
2031/*! Return a human readable representation of a Cell Identifier, like "LAC:123"
2032 * or "CGI:001-01-42-23".
2033 * \param[in] cid Cell Identifer.
2034 * \returns String in a static buffer.
2035 */
2036const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid)
2037{
Harald Welte171ef822019-03-28 10:49:05 +01002038 static __thread char buf[64];
Harald Welte179f3572019-03-18 18:38:47 +01002039 return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002040}
2041
2042/*! Like gsm0808_cell_id_name() but uses a different static buffer.
2043 * \param[in] cid Cell Identifer.
2044 * \returns String in a static buffer.
2045 */
2046const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid)
2047{
Harald Welte171ef822019-03-28 10:49:05 +01002048 static __thread char buf[64];
Harald Welte179f3572019-03-18 18:38:47 +01002049 return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
2050}
2051
2052char *gsm0808_cell_id_name_c(const void *ctx, const struct gsm0808_cell_id *cid)
2053{
2054 char *buf = talloc_size(ctx, 64);
2055 if (!buf)
2056 return NULL;
2057 return gsm0808_cell_id_name_buf(buf, 64, cid);
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002058}
2059
2060/*! Return a human readable representation of the Cell Identifier List, like
2061 * "LAC[2]:{123, 456}".
2062 * The return value semantics are like snprintf() and thus allow ensuring a complete
2063 * untruncated string by determining the required string length from the return value.
2064 * If buflen > 0, always nul-terminate the string in buf, also when it is truncated.
2065 * If buflen == 0, do not modify buf, just return the would-be length.
2066 * \param[out] buf Destination buffer to write string representation to.
2067 * \param[in] buflen Amount of memory available in \a buf.
2068 * \param[in] cil Cell Identifer List.
2069 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
2070 * or that would have been written if the buffer were large enough.
2071 */
2072int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil)
2073{
2074 char *pos = buf;
2075 int total_len = 0;
2076 int i;
2077
2078 APPEND_STR("%s[%u]", gsm0808_cell_id_discr_name(cil->id_discr), cil->id_list_len);
2079
2080 switch (cil->id_discr) {
2081 case CELL_IDENT_BSS:
2082 case CELL_IDENT_NO_CELL:
2083 return total_len;
2084 default:
2085 break;
2086 }
2087
2088 APPEND_STR(":{");
2089
2090 for (i = 0; i < cil->id_list_len; i++) {
2091 if (i)
2092 APPEND_STR(", ");
2093 APPEND_CELL_ID_U(cil->id_discr, &cil->id_list[i]);
2094 }
2095
2096 APPEND_STR("}");
2097 return total_len;
2098}
2099
2100/*! Return a human-readable representation of \a cil in a static buffer.
2101 * If the list is too long, the output may be truncated.
2102 * See also gsm0808_cell_id_list_name_buf(). */
2103const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil)
2104{
Harald Welte171ef822019-03-28 10:49:05 +01002105 static __thread char buf[1024];
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002106 gsm0808_cell_id_list_name_buf(buf, sizeof(buf), cil);
2107 return buf;
2108}
2109
Harald Welte179f3572019-03-18 18:38:47 +01002110char *gsm0808_cell_id_list_name_c(const void *ctx, const struct gsm0808_cell_id_list2 *cil)
2111{
2112 char *buf = talloc_size(ctx, 1024);
2113 if (!buf)
2114 return NULL;
2115 gsm0808_cell_id_list_name_buf(buf, 1024, cil);
2116 return buf;
2117}
2118
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002119#undef APPEND_STR
2120#undef APPEND_CELL_ID_U
2121
Harald Welte4a62eda2019-03-18 18:27:00 +01002122char *gsm0808_channel_type_name_buf(char *buf, size_t buf_len, const struct gsm0808_channel_type *ct)
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02002123{
Harald Welte4a62eda2019-03-18 18:27:00 +01002124 snprintf(buf, buf_len, "ch_indctr=0x%x ch_rate_type=0x%x perm_spch=%s",
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02002125 ct->ch_indctr, ct->ch_rate_type,
2126 osmo_hexdump(ct->perm_spch, ct->perm_spch_len));
2127 return buf;
2128}
2129
Harald Welte4a62eda2019-03-18 18:27:00 +01002130const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct)
2131{
Harald Welte171ef822019-03-28 10:49:05 +01002132 static __thread char buf[128];
Harald Welte4a62eda2019-03-18 18:27:00 +01002133 return gsm0808_channel_type_name_buf(buf, sizeof(buf), ct);
2134}
2135
Harald Welte179f3572019-03-18 18:38:47 +01002136char *gsm0808_channel_type_name_c(const void *ctx, const struct gsm0808_channel_type *ct)
2137{
2138 char *buf = talloc_size(ctx, 128);
2139 if (!buf)
2140 return NULL;
2141 return gsm0808_channel_type_name_buf(buf, 128, ct);
2142}
2143
Harald Welte96e2a002017-06-12 21:44:18 +02002144/*! @} */