blob: 4993539f3c1ea7641a8df67ee81d86c0bee95052 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*
Harald Weltee08da972017-11-13 01:00:26 +09002 * (C) 2016 by sysmocom - s.f.m.c. GmbH, Author: Philipp Maier
Philipp Maier22401432017-03-24 17:59:26 +01003 * All Rights Reserved
4 *
Harald Weltee08da972017-11-13 01:00:26 +09005 * SPDX-License-Identifier: GPL-2.0+
Philipp Maier22401432017-03-24 17:59:26 +01006 *
7 * This program is free software; you can redistribute it and/or modify
Harald Weltee08da972017-11-13 01:00:26 +09008 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
Philipp Maier22401432017-03-24 17:59:26 +010010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Weltee08da972017-11-13 01:00:26 +090015 * GNU General Public License for more details.
Philipp Maier22401432017-03-24 17:59:26 +010016 *
Harald Weltee08da972017-11-13 01:00:26 +090017 * You should have received a copy of the GNU General Public License
Philipp Maier22401432017-03-24 17:59:26 +010018 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
Harald Welte20725b92017-05-15 12:50:04 +020022#include "config.h"
23
Philipp Maier22401432017-03-24 17:59:26 +010024#include <osmocom/core/utils.h>
25#include <osmocom/core/msgb.h>
Harald Welte95871da2017-05-15 12:11:36 +020026#include <osmocom/core/byteswap.h>
Philipp Maier22401432017-03-24 17:59:26 +010027#include <string.h>
Philipp Maier22401432017-03-24 17:59:26 +010028#include <errno.h>
29#include <osmocom/gsm/protocol/gsm_08_08.h>
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010030#include <osmocom/gsm/gsm48.h>
Max5ec0cf52019-01-15 16:37:09 +010031#include <osmocom/gsm/gsm0808.h>
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010032#include <osmocom/gsm/gsm0808_utils.h>
Philipp Maier22401432017-03-24 17:59:26 +010033
34#define IP_V4_ADDR_LEN 4
35#define IP_V6_ADDR_LEN 16
36#define IP_PORT_LEN 2
37
Philipp Maiere0c65302017-03-28 17:05:40 +020038#define CHANNEL_TYPE_ELEMENT_MAXLEN 11
39#define CHANNEL_TYPE_ELEMENT_MINLEN 3
Philipp Maier14e76b92017-03-28 18:36:52 +020040#define ENCRYPT_INFO_ELEMENT_MINLEN 1
Philipp Maier6f725d62017-03-24 18:03:17 +010041
Harald Welte20725b92017-05-15 12:50:04 +020042#ifdef HAVE_SYS_SOCKET_H
43
44#include <sys/socket.h>
45#include <netinet/in.h>
Harald Welte96e2a002017-06-12 21:44:18 +020046
47/*! \addtogroup gsm0808
48 * @{
Harald Welte37b61652017-10-16 18:46:03 +020049 * \file gsm0808_utils.c
Harald Welte96e2a002017-06-12 21:44:18 +020050 */
51
Philipp Maier4f4905f2018-11-30 13:36:12 +010052/*! Encode TS 08.08 AoIP Cause IE
53 * \param[out] msg Message Buffer to which to append IE
54 * \param[in] cause Cause code to be used in IE
55 * \returns number of bytes added to \a msg */
56uint8_t gsm0808_enc_cause(struct msgb *msg, uint16_t cause)
57{
58 /* See also 3GPP TS 48.008 3.2.2.5 Cause */
59 uint8_t *old_tail;
60 bool extended;
61
62 old_tail = msg->tail;
63
64 extended = gsm0808_cause_ext(cause >> 8);
65
66 msgb_put_u8(msg, GSM0808_IE_CAUSE);
67 if (extended) {
68 msgb_put_u8(msg, 2);
69 msgb_put_u16(msg, cause);
70 } else {
71 msgb_put_u8(msg, 1);
72 msgb_put_u8(msg, (uint8_t) (cause & 0xFF));
73 }
74
75 return (uint8_t) (msg->tail - old_tail);
76}
77
Neels Hofmeyr87e45502017-06-20 00:17:59 +020078/*! Encode TS 08.08 AoIP transport address IE
Harald Welte96e2a002017-06-12 21:44:18 +020079 * \param[out] msg Message Buffer to which to append IE
80 * \param[in] ss Socket Address to be used in IE
Oliver Smithc66b35b2022-08-05 11:27:55 +020081 * \returns number of bytes added to \a msg; 0 if msg is too small */
Philipp Maier22401432017-03-24 17:59:26 +010082uint8_t gsm0808_enc_aoip_trasp_addr(struct msgb *msg,
83 const struct sockaddr_storage *ss)
84{
85 /* See also 3GPP TS 48.008 3.2.2.102 AoIP Transport Layer Address */
86 struct sockaddr_in *sin;
87 struct sockaddr_in6 *sin6;
88 uint16_t port = 0;
89 uint8_t *ptr;
Oliver Smithc66b35b2022-08-05 11:27:55 +020090 const uint8_t len_tl = 2;
91 uint8_t len_v = sizeof(port);
Philipp Maier22401432017-03-24 17:59:26 +010092
Philipp Maier22401432017-03-24 17:59:26 +010093 OSMO_ASSERT(ss->ss_family == AF_INET || ss->ss_family == AF_INET6);
94
Oliver Smithc66b35b2022-08-05 11:27:55 +020095 switch (ss->ss_family) {
96 case AF_INET:
97 len_v += IP_V4_ADDR_LEN;
98 break;
99 case AF_INET6:
100 len_v += IP_V6_ADDR_LEN;
101 break;
102 }
103
104 if (msgb_tailroom(msg) < len_tl + len_v)
105 return 0;
106
Philipp Maier22401432017-03-24 17:59:26 +0100107 msgb_put_u8(msg, GSM0808_IE_AOIP_TRASP_ADDR);
Oliver Smithc66b35b2022-08-05 11:27:55 +0200108 msgb_put_u8(msg, len_v);
Philipp Maier22401432017-03-24 17:59:26 +0100109
110 switch (ss->ss_family) {
111 case AF_INET:
112 sin = (struct sockaddr_in *)ss;
Harald Welte95871da2017-05-15 12:11:36 +0200113 port = osmo_ntohs(sin->sin_port);
Philipp Maier22401432017-03-24 17:59:26 +0100114 ptr = msgb_put(msg, IP_V4_ADDR_LEN);
115 memcpy(ptr, &sin->sin_addr.s_addr, IP_V4_ADDR_LEN);
116 break;
117 case AF_INET6:
118 sin6 = (struct sockaddr_in6 *)ss;
Harald Welte95871da2017-05-15 12:11:36 +0200119 port = osmo_ntohs(sin6->sin6_port);
Philipp Maier22401432017-03-24 17:59:26 +0100120 ptr = msgb_put(msg, IP_V6_ADDR_LEN);
121 memcpy(ptr, sin6->sin6_addr.s6_addr, IP_V6_ADDR_LEN);
122 break;
123 }
124
125 msgb_put_u16(msg, port);
Oliver Smithc66b35b2022-08-05 11:27:55 +0200126 return len_tl + len_v;
Philipp Maier22401432017-03-24 17:59:26 +0100127}
128
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200129/*! Decode TS 08.08 AoIP transport address IE
Harald Welte96e2a002017-06-12 21:44:18 +0200130 * \param[out] ss Caller-provided memory where decoded socket addr is stored
131 * \param[in] elem pointer to IE value
132 * \param[in] len length of \a elem in bytes
133 * \returns number of bytes parsed */
Philipp Maier22401432017-03-24 17:59:26 +0100134int gsm0808_dec_aoip_trasp_addr(struct sockaddr_storage *ss,
135 const uint8_t *elem, uint8_t len)
136{
137 /* See also 3GPP TS 48.008 3.2.2.102 AoIP Transport Layer Address */
138 struct sockaddr_in sin;
139 struct sockaddr_in6 sin6;
140 const uint8_t *old_elem = elem;
141
Philipp Maier22401432017-03-24 17:59:26 +0100142 if (!elem)
143 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200144 if (len == 0)
Philipp Maier22401432017-03-24 17:59:26 +0100145 return -EINVAL;
146
147 memset(ss, 0, sizeof(*ss));
148
149 switch (len) {
150 case IP_V4_ADDR_LEN + IP_PORT_LEN:
151 memset(&sin, 0, sizeof(sin));
152 sin.sin_family = AF_INET;
153
154 memcpy(&sin.sin_addr.s_addr, elem, IP_V4_ADDR_LEN);
155 elem += IP_V4_ADDR_LEN;
156 sin.sin_port = osmo_load16le(elem);
157 elem += IP_PORT_LEN;
158
159 memcpy(ss, &sin, sizeof(sin));
160 break;
161 case IP_V6_ADDR_LEN + IP_PORT_LEN:
162 memset(&sin6, 0, sizeof(sin6));
163 sin6.sin6_family = AF_INET6;
164
165 memcpy(sin6.sin6_addr.s6_addr, elem, IP_V6_ADDR_LEN);
166 elem += IP_V6_ADDR_LEN;
167 sin6.sin6_port = osmo_load16le(elem);
168 elem += IP_PORT_LEN;
169
170 memcpy(ss, &sin6, sizeof(sin6));
171 break;
172 default:
173 /* Malformed element! */
174 return -EINVAL;
175 break;
176 }
177
178 return (int)(elem - old_elem);
179}
Philipp Maier6f725d62017-03-24 18:03:17 +0100180
Pau Espin Pedrol18506c82019-04-16 15:47:59 +0200181/*! Decode TS 08.08 (Osmocom Extension) Osmux CID
182 * TV with len(V) == 1, and V is the CID to be used.
183 * \param[out] cid Caller-provided variable where CID is stored
184 * \param[in] elem pointer to IE value
185 * \param[in] len length of \a elem in bytes
186 * \returns number of bytes parsed */
187int gsm0808_dec_osmux_cid(uint8_t *cid, const uint8_t *elem, uint8_t len)
188{
Pau Espin Pedrol18506c82019-04-16 15:47:59 +0200189 if (!elem)
190 return -EINVAL;
191 if (len != 1)
192 return -EINVAL;
193
194 *cid = *elem;
195
196 return 1;
197}
198
Harald Welte20725b92017-05-15 12:50:04 +0200199#endif /* HAVE_SYS_SOCKET_H */
200
Harald Welteef7be492019-05-03 21:01:13 +0200201/* Decode 5-byte LAI list element data (see TS 08.08 3.2.2.27) into MCC/MNC/LAC. */
202static void decode_lai(const uint8_t *data, struct osmo_location_area_id *decoded)
203{
204 struct gsm48_loc_area_id lai;
205
206 /* Copy data to stack to prevent unaligned access in gsm48_decode_lai2(). */
207 memcpy(&lai, data, sizeof(lai)); /* don't byte swap yet */
208
209 gsm48_decode_lai2(&lai, decoded);
210}
211
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700212/* Helper function for gsm0808_enc_speech_codec[_list]().
213 * Returns number of bytes appended; negative on error. */
214static int enc_speech_codec(struct msgb *msg,
215 const struct gsm0808_speech_codec *sc)
Philipp Maier6f725d62017-03-24 18:03:17 +0100216{
217 /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
218 uint8_t header = 0;
219 uint8_t *old_tail;
Vadim Yanitskiy061b7ec2022-12-14 02:06:06 +0700220 bool type_extended;
Philipp Maierbb839662017-06-01 17:11:19 +0200221
222 /* Note: Extended codec types are codec types that require 8 instead
223 * of 4 bit to fully specify the selected codec. In the following,
224 * we check if we work with an extended type or not. We also check
225 * if the codec type is valid at all. */
Vadim Yanitskiy8e962452022-12-14 01:13:19 +0700226 switch (sc->type) {
Philipp Maierbb839662017-06-01 17:11:19 +0200227 case GSM0808_SCT_FR1:
228 case GSM0808_SCT_FR2:
229 case GSM0808_SCT_FR3:
230 case GSM0808_SCT_FR4:
231 case GSM0808_SCT_FR5:
232 case GSM0808_SCT_HR1:
233 case GSM0808_SCT_HR3:
234 case GSM0808_SCT_HR4:
235 case GSM0808_SCT_HR6:
236 type_extended = false;
237 break;
238 case GSM0808_SCT_CSD:
239 type_extended = true;
240 break;
241 default:
242 /* Invalid codec type specified */
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700243 return -EINVAL;
Philipp Maierbb839662017-06-01 17:11:19 +0200244 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100245
246 old_tail = msg->tail;
247
248 if (sc->fi)
249 header |= (1 << 7);
250 if (sc->pi)
251 header |= (1 << 6);
252 if (sc->pt)
253 header |= (1 << 5);
254 if (sc->tf)
255 header |= (1 << 4);
Philipp Maierbb839662017-06-01 17:11:19 +0200256
257 if (type_extended) {
Philipp Maier6f725d62017-03-24 18:03:17 +0100258 header |= 0x0f;
259 msgb_put_u8(msg, header);
Philipp Maierbb839662017-06-01 17:11:19 +0200260 msgb_put_u8(msg, sc->type);
Philipp Maier6f725d62017-03-24 18:03:17 +0100261 } else {
Philipp Maier6f725d62017-03-24 18:03:17 +0100262 header |= sc->type;
263 msgb_put_u8(msg, header);
Philipp Maier6f725d62017-03-24 18:03:17 +0100264 }
265
Philipp Maierbb839662017-06-01 17:11:19 +0200266 /* Note: Whether a configuration is present or not depends on the
267 * selected codec type. If present, it can either consist of one
268 * or two octets, depending on the codec type */
269 switch (sc->type) {
270 case GSM0808_SCT_FR3:
271 case GSM0808_SCT_HR3:
272 case GSM0808_SCT_HR6:
Philipp Maier7e27b142018-03-22 17:26:46 +0100273 msgb_put_u16(msg, osmo_ntohs(sc->cfg));
Philipp Maierbb839662017-06-01 17:11:19 +0200274 break;
275 case GSM0808_SCT_FR4:
276 case GSM0808_SCT_FR5:
277 case GSM0808_SCT_HR4:
278 case GSM0808_SCT_CSD:
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700279 if (sc->cfg >> 8)
280 return -EINVAL;
Philipp Maierbb839662017-06-01 17:11:19 +0200281 msgb_put_u8(msg, (uint8_t) sc->cfg & 0xff);
282 break;
283 default:
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700284 if (sc->cfg != 0)
285 return -EINVAL;
Philipp Maierbb839662017-06-01 17:11:19 +0200286 break;
287 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100288
289 return (uint8_t) (msg->tail - old_tail);
290}
291
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200292/*! Encode TS 08.08 Speech Codec IE
Harald Welte96e2a002017-06-12 21:44:18 +0200293 * \param[out] msg Message Buffer to which IE will be appended
294 * \param[in] sc Speech Codec to be encoded into IE
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700295 * \returns number of bytes appended to \a msg; negative on error */
296int gsm0808_enc_speech_codec2(struct msgb *msg,
297 const struct gsm0808_speech_codec *sc)
Philipp Maier6f725d62017-03-24 18:03:17 +0100298{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200299 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100300 uint8_t *tlv_len;
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700301 int rc;
Philipp Maier6f725d62017-03-24 18:03:17 +0100302
Philipp Maier6f725d62017-03-24 18:03:17 +0100303 msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC);
304 tlv_len = msgb_put(msg, 1);
Philipp Maier6f725d62017-03-24 18:03:17 +0100305
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700306 rc = enc_speech_codec(msg, sc);
307 if (rc < 0)
308 return rc;
Philipp Maier6f725d62017-03-24 18:03:17 +0100309
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700310 *tlv_len = rc;
Philipp Maier6f725d62017-03-24 18:03:17 +0100311 return *tlv_len + 2;
312}
313
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700314/*! Deprecated: gsm0808_enc_speech_codec2() wrapper for backwards compatibility.
315 * Mimics the old behavior: crash on missing and/or invalid input. */
316uint8_t gsm0808_enc_speech_codec(struct msgb *msg,
317 const struct gsm0808_speech_codec *sc)
318{
319 int rc;
320
321 rc = gsm0808_enc_speech_codec2(msg, sc);
322 OSMO_ASSERT(rc > 0);
323
324 return rc;
325}
326
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200327/*! Decode TS 08.08 Speech Codec IE
Harald Welte96e2a002017-06-12 21:44:18 +0200328 * \param[out] sc Caller-allocated memory for Speech Codec
329 * \param[in] elem IE value to be decoded
330 * \param[in] len Length of \a elem in bytes
331 * \returns number of bytes parsed; negative on error */
Philipp Maier6f725d62017-03-24 18:03:17 +0100332int gsm0808_dec_speech_codec(struct gsm0808_speech_codec *sc,
333 const uint8_t *elem, uint8_t len)
334{
335 /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
336 uint8_t header;
337 const uint8_t *old_elem = elem;
338
Philipp Maier6f725d62017-03-24 18:03:17 +0100339 if (!elem)
340 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200341 if (len == 0)
Philipp Maier6f725d62017-03-24 18:03:17 +0100342 return -EINVAL;
343
344 memset(sc, 0, sizeof(*sc));
345
346 header = *elem;
347
Philipp Maier85a6af22017-04-28 10:55:05 +0200348 /* An extended codec type needs at least two fields,
349 * bail if the input data length is not sufficient. */
Philipp Maier6f725d62017-03-24 18:03:17 +0100350 if ((header & 0x0F) == 0x0F && len < 2)
351 return -EINVAL;
Philipp Maier6f725d62017-03-24 18:03:17 +0100352
353 elem++;
354 len--;
355
356 if (header & (1 << 7))
357 sc->fi = true;
358 if (header & (1 << 6))
359 sc->pi = true;
360 if (header & (1 << 5))
361 sc->pt = true;
362 if (header & (1 << 4))
363 sc->tf = true;
364
365 if ((header & 0x0F) != 0x0F) {
366 sc->type = (header & 0x0F);
Philipp Maierbb839662017-06-01 17:11:19 +0200367 } else {
368 sc->type = *elem;
369 elem++;
370 len--;
Philipp Maier6f725d62017-03-24 18:03:17 +0100371 }
372
Philipp Maierbb839662017-06-01 17:11:19 +0200373 /* Note: Whether a configuration is present or not depends on the
374 * selected codec type. If present, it can either consist of one or
375 * two octets depending on the codec type */
376 switch (sc->type) {
377 case GSM0808_SCT_FR1:
378 case GSM0808_SCT_FR2:
379 case GSM0808_SCT_HR1:
380 break;
381 case GSM0808_SCT_HR4:
382 case GSM0808_SCT_CSD:
383 case GSM0808_SCT_FR4:
384 case GSM0808_SCT_FR5:
385 if (len < 1)
386 return -EINVAL;
387 sc->cfg = *elem;
388 elem++;
389 break;
390 case GSM0808_SCT_FR3:
391 case GSM0808_SCT_HR3:
392 case GSM0808_SCT_HR6:
393 if (len < 2)
394 return -EINVAL;
Philipp Maier7e27b142018-03-22 17:26:46 +0100395 sc->cfg = osmo_load16le(elem);
Philipp Maierbb839662017-06-01 17:11:19 +0200396 elem += 2;
397 break;
398 default:
399 /* Invalid codec type => malformed speech codec element! */
400 return -EINVAL;
401 break;
402 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100403
404 return (int)(elem - old_elem);
405}
406
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200407/*! Encode TS 08.08 Speech Codec list
Harald Welte96e2a002017-06-12 21:44:18 +0200408 * \param[out] msg Message Buffer to which IE is to be appended
409 * \param[in] scl Speech Codec List to be encoded into IE
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700410 * \returns number of bytes added to \a msg; negative on error */
411int gsm0808_enc_speech_codec_list2(struct msgb *msg,
412 const struct gsm0808_speech_codec_list *scl)
Philipp Maier6f725d62017-03-24 18:03:17 +0100413{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200414 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100415 uint8_t *old_tail;
416 uint8_t *tlv_len;
417 unsigned int i;
Philipp Maier6f725d62017-03-24 18:03:17 +0100418 unsigned int bytes_used = 0;
419
Philipp Maier6f725d62017-03-24 18:03:17 +0100420 msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC_LIST);
421 tlv_len = msgb_put(msg, 1);
422 old_tail = msg->tail;
423
424 for (i = 0; i < scl->len; i++) {
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700425 int rc = enc_speech_codec(msg, &scl->codec[i]);
426 if (rc < 1)
427 return rc;
Philipp Maier6f725d62017-03-24 18:03:17 +0100428 bytes_used += rc;
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700429 if (bytes_used > 0xff)
430 return -EOVERFLOW;
Philipp Maier6f725d62017-03-24 18:03:17 +0100431 }
432
433 *tlv_len = (uint8_t) (msg->tail - old_tail);
434 return *tlv_len + 2;
435}
436
Vadim Yanitskiy5a513312022-12-14 00:23:59 +0700437/*! Deprecated: gsm0808_enc_speech_codec_list2() wrapper for backwards compatibility.
438 * Mimics the old behavior: crash on missing and/or invalid input. */
439uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg,
440 const struct gsm0808_speech_codec_list *scl)
441{
442 int rc;
443
444 rc = gsm0808_enc_speech_codec_list2(msg, scl);
445 OSMO_ASSERT(rc > 0);
446
447 return rc;
448}
449
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200450/*! Decode TS 08.08 Speech Codec list IE
Harald Welte96e2a002017-06-12 21:44:18 +0200451 * \param[out] scl Caller-provided memory to store codec list
452 * \param[in] elem IE value to be decoded
453 * \param[in] len Length of \a elem in bytes
454 * \returns number of bytes parsed; negative on error */
Philipp Maier6f725d62017-03-24 18:03:17 +0100455int gsm0808_dec_speech_codec_list(struct gsm0808_speech_codec_list *scl,
456 const uint8_t *elem, uint8_t len)
457{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200458 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100459 const uint8_t *old_elem = elem;
460 unsigned int i;
461 int rc;
462 uint8_t decoded = 0;
463
Philipp Maier6f725d62017-03-24 18:03:17 +0100464 if (!elem)
465 return -EINVAL;
Philipp Maier6f725d62017-03-24 18:03:17 +0100466
467 memset(scl, 0, sizeof(*scl));
468
469 for (i = 0; i < ARRAY_SIZE(scl->codec); i++) {
470 if (len <= 0)
471 break;
472
473 rc = gsm0808_dec_speech_codec(&scl->codec[i], elem, len);
474 if (rc < 1)
475 return -EINVAL;
476
477 elem+=rc;
478 len -= rc;
479 decoded++;
480 }
481
482 scl->len = decoded;
483
Philipp Maier6f725d62017-03-24 18:03:17 +0100484 return (int)(elem - old_elem);
485}
Philipp Maiere0c65302017-03-28 17:05:40 +0200486
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200487/*! Encode TS 08.08 Channel Type IE
Harald Welte96e2a002017-06-12 21:44:18 +0200488 * \param[out] msg Message Buffer to which IE is to be appended
489 * \param[in] ct Channel Type to be encoded
490 * \returns number of bytes added to \a msg */
Philipp Maiere0c65302017-03-28 17:05:40 +0200491uint8_t gsm0808_enc_channel_type(struct msgb *msg,
492 const struct gsm0808_channel_type *ct)
493{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200494 /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */
Philipp Maiere0c65302017-03-28 17:05:40 +0200495 unsigned int i;
496 uint8_t byte;
497 uint8_t *old_tail;
498 uint8_t *tlv_len;
499
Philipp Maiere0c65302017-03-28 17:05:40 +0200500 OSMO_ASSERT(ct->perm_spch_len <= CHANNEL_TYPE_ELEMENT_MAXLEN - 2);
501
Philipp Maiere0c65302017-03-28 17:05:40 +0200502 msgb_put_u8(msg, GSM0808_IE_CHANNEL_TYPE);
503 tlv_len = msgb_put(msg, 1);
504 old_tail = msg->tail;
505
506 msgb_put_u8(msg, ct->ch_indctr & 0x0f);
507 msgb_put_u8(msg, ct->ch_rate_type);
508
Oliver Smithf047a4a2023-01-31 13:10:03 +0100509 switch (ct->ch_indctr) {
510 case GSM0808_CHAN_DATA:
511 byte = ct->data_rate;
Philipp Maiere0c65302017-03-28 17:05:40 +0200512
Oliver Smithf047a4a2023-01-31 13:10:03 +0100513 if (ct->data_transparent)
514 byte |= 0x40; /* Set T/NT */
515
516 if (ct->data_rate_allowed_is_set) {
517 OSMO_ASSERT(!ct->data_transparent);
518 byte |= 0x80; /* Set ext */
519 msgb_put_u8(msg, byte);
520
521 byte = ct->data_rate_allowed;
522 if (ct->data_asym_pref_is_set) {
523 byte |= 0x80; /* Set ext */
524 msgb_put_u8(msg, byte);
525
526 /* Set asymmetry indication, rest is spare */
527 byte = ct->data_asym_pref << 5;
528 }
529 }
Philipp Maiere0c65302017-03-28 17:05:40 +0200530 msgb_put_u8(msg, byte);
Oliver Smithf047a4a2023-01-31 13:10:03 +0100531 break;
532 case GSM0808_CHAN_SPEECH:
533 case GSM0808_CHAN_SPEECH_CTM_TEXT_TELEPHONY:
534 for (i = 0; i < ct->perm_spch_len; i++) {
535 byte = ct->perm_spch[i];
536
537 if (i < ct->perm_spch_len - 1)
538 byte |= 0x80;
539 msgb_put_u8(msg, byte);
540 }
541 break;
542 case GSM0808_CHAN_SIGN:
543 /* Octet 5 is spare */
544 break;
545 default:
546 OSMO_ASSERT(false);
Philipp Maiere0c65302017-03-28 17:05:40 +0200547 }
548
549 *tlv_len = (uint8_t) (msg->tail - old_tail);
550 return *tlv_len + 2;
551}
552
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200553/*! Decode TS 08.08 Channel Type IE
Harald Welte96e2a002017-06-12 21:44:18 +0200554 * \param[out] ct Caller-provided memory to store channel type
555 * \param[in] elem IE Value to be decoded
556 * \param[in] len Length of \a elem in bytes
557 * \returns number of bytes parsed; negative on error */
Philipp Maiere0c65302017-03-28 17:05:40 +0200558int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct,
559 const uint8_t *elem, uint8_t len)
560{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200561 /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */
Philipp Maiere0c65302017-03-28 17:05:40 +0200562 unsigned int i;
563 uint8_t byte;
564 const uint8_t *old_elem = elem;
565
Philipp Maiere0c65302017-03-28 17:05:40 +0200566 if (!elem)
567 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200568 if (len < 3 || len > 11)
Philipp Maiere0c65302017-03-28 17:05:40 +0200569 return -EINVAL;
570
571 memset(ct, 0, sizeof(*ct));
572
573 ct->ch_indctr = (*elem) & 0x0f;
574 elem++;
575 ct->ch_rate_type = (*elem) & 0x0f;
576 elem++;
577
Oliver Smithf047a4a2023-01-31 13:10:03 +0100578 switch (ct->ch_indctr) {
579 case GSM0808_CHAN_DATA:
Philipp Maiere0c65302017-03-28 17:05:40 +0200580 byte = *elem;
581 elem++;
Oliver Smithf047a4a2023-01-31 13:10:03 +0100582 ct->data_transparent = byte & 0x40; /* T/NT */
583 ct->data_rate = byte & 0x3f;
584
585 /* Optional extension for non-transparent service */
586 if (byte & 0x80) {
587 if (ct->data_transparent)
588 return -EINVAL;
589 if (elem - old_elem >= len)
590 return -EOVERFLOW;
591 byte = *elem;
592 elem++;
593
594 ct->data_rate_allowed_is_set = true;
595 ct->data_rate_allowed = byte & 0x7f;
596
597 /* Optional extension */
598 if (byte & 0x80) {
599 if (elem - old_elem >= len)
600 return -EOVERFLOW;
601 byte = *elem;
602 elem++;
603
604 ct->data_asym_pref_is_set = true;
605 ct->data_asym_pref = byte & 0x60 >> 5;
606 }
607 }
608 break;
609 case GSM0808_CHAN_SPEECH:
610 case GSM0808_CHAN_SPEECH_CTM_TEXT_TELEPHONY:
611 for (i = 0; i < ARRAY_SIZE(ct->perm_spch); i++) {
612 if (elem - old_elem >= len)
613 return -EOVERFLOW;
614
615 byte = *elem;
616 elem++;
617 ct->perm_spch[i] = byte & 0x7f;
618 if ((byte & 0x80) == 0x00)
619 break;
620 }
621 ct->perm_spch_len = i + 1;
622 break;
623 case GSM0808_CHAN_SIGN:
624 /* Octet 5 is spare */
625 break;
626 default:
627 return -ENOTSUP;
Philipp Maiere0c65302017-03-28 17:05:40 +0200628 }
Philipp Maiere0c65302017-03-28 17:05:40 +0200629
630 return (int)(elem - old_elem);
631}
Philipp Maier14e76b92017-03-28 18:36:52 +0200632
Max969fb2e2018-12-10 11:01:10 +0100633/*! Create BSSMAP Global Call Reference, 3GPP TS 48.008 §3.2.2.115.
634 * \param[out] msg Message Buffer for appending IE
635 * \param[in] g Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1
636 * \returns number of bytes added to \a msg or 0 on error */
Max47022152018-12-19 18:51:00 +0100637static uint8_t gsm0808_enc_gcr(struct msgb *msg, const struct osmo_gcr_parsed *g)
Max969fb2e2018-12-10 11:01:10 +0100638{
639 uint8_t enc, *len = msgb_tl_put(msg, GSM0808_IE_GLOBAL_CALL_REF);
640
641 enc = osmo_enc_gcr(msg, g);
642 if (!enc)
643 return 0;
644
645 *len = enc;
646 return enc + 2; /* type (1 byte) + length (1 byte) */
647}
648
649/*! Decode BSSMAP Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1.
650 * \param[out] gcr Caller-provided memory to store Global Call Reference
Max036012b2018-12-19 17:48:56 +0100651 * \param[in] tp IE values to be decoded
Max969fb2e2018-12-10 11:01:10 +0100652 * \returns number of bytes parsed; negative on error */
Max47022152018-12-19 18:51:00 +0100653static int gsm0808_dec_gcr(struct osmo_gcr_parsed *gcr, const struct tlv_parsed *tp)
Max969fb2e2018-12-10 11:01:10 +0100654{
655 int ret;
656 const uint8_t *buf = TLVP_VAL_MINLEN(tp, GSM0808_IE_GLOBAL_CALL_REF, OSMO_GCR_MIN_LEN);
657 if (!buf)
658 return -EINVAL;
659
660 ret = osmo_dec_gcr(gcr, buf, TLVP_LEN(tp, GSM0808_IE_GLOBAL_CALL_REF));
661 if (ret < 0)
662 return -ENOENT;
663
664 return 2 + ret;
665}
666
Max47022152018-12-19 18:51:00 +0100667/*! Add LCLS parameters to a given msgb, 3GPP TS 48.008 §3.2.2.115 - 3.2.2.120.
668 * \param[out] msg Message Buffer for appending IE
669 * \param[in] lcls LCLS-related data
670 * \returns number of bytes added to \a msg or 0 on error */
671uint8_t gsm0808_enc_lcls(struct msgb *msg, const struct osmo_lcls *lcls)
672{
673 uint8_t enc = 0;
674
675 /* LCLS: §3.2.2.115 Global Call Reference */
Max3b901252019-01-15 14:15:11 +0100676 if (lcls->gcr_available)
677 enc = gsm0808_enc_gcr(msg, &lcls->gcr);
Max47022152018-12-19 18:51:00 +0100678
679 /* LCLS: §3.2.2.116 Configuration */
680 if (lcls->config != GSM0808_LCLS_CFG_NA) {
681 msgb_tv_put(msg, GSM0808_IE_LCLS_CONFIG, lcls->config);
682 enc += 2;
683 }
684
685 /* LCLS: §3.2.2.117 Connection Status Control */
686 if (lcls->control != GSM0808_LCLS_CSC_NA) {
687 msgb_tv_put(msg, GSM0808_IE_LCLS_CONN_STATUS_CTRL, lcls->control);
688 enc += 2;
689 }
690
691 /* LCLS: §3.2.2.118 Correlation-Not-Needed */
692 if (!lcls->corr_needed) {
693 msgb_v_put(msg, GSM0808_IE_LCLS_CORR_NOT_NEEDED);
694 enc++;
695 }
696
697 return enc;
698}
699
700/*! Decode LCLS parameters to a given msgb, 3GPP TS 48.008 §3.2.2.115 - 3.2.2.120.
701 * \param[out] lcls Caller-provided memory to store LCLS-related data
702 * \param[in] tp IE values to be decoded
703 * \returns GCR size or negative on error */
704int gsm0808_dec_lcls(struct osmo_lcls *lcls, const struct tlv_parsed *tp)
705{
Max3b901252019-01-15 14:15:11 +0100706 int ret = gsm0808_dec_gcr(&lcls->gcr, tp);
Max47022152018-12-19 18:51:00 +0100707
Max3b901252019-01-15 14:15:11 +0100708 lcls->gcr_available = (ret < 0) ? false : true;
Max47022152018-12-19 18:51:00 +0100709 lcls->config = tlvp_val8(tp, GSM0808_IE_LCLS_CONFIG, GSM0808_LCLS_CFG_NA);
710 lcls->control = tlvp_val8(tp, GSM0808_IE_LCLS_CONN_STATUS_CTRL, GSM0808_LCLS_CSC_NA);
711 lcls->corr_needed = TLVP_PRESENT(tp, GSM0808_IE_LCLS_CORR_NOT_NEEDED) ? false : true;
712
713 return ret;
714}
715
Harald Welte171ef822019-03-28 10:49:05 +0100716static __thread char dbuf[256];
Max5ec0cf52019-01-15 16:37:09 +0100717
718/*! Dump LCLS parameters (GCR excluded) into string for printing.
Harald Welte4a62eda2019-03-18 18:27:00 +0100719 * \param[out] buf caller-allocated output string buffer
720 * \param[in] buf_len size of buf in bytes
Max5ec0cf52019-01-15 16:37:09 +0100721 * \param[in] lcls pointer to the struct to print.
722 * \returns string representation of LCLS or NULL on error. */
Harald Welte4a62eda2019-03-18 18:27:00 +0100723char *osmo_lcls_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls)
Max5ec0cf52019-01-15 16:37:09 +0100724{
Harald Welte4a62eda2019-03-18 18:27:00 +0100725 struct osmo_strbuf s = { .buf = buf, .len = buf_len };
Max5ec0cf52019-01-15 16:37:09 +0100726
727 if (!lcls)
728 return NULL;
729
730 OSMO_STRBUF_PRINTF(s, "LCLS Config: %s, Control: %s, Correlation-Needed: %u",
731 gsm0808_lcls_config_name(lcls->config),
732 gsm0808_lcls_control_name(lcls->control),
733 lcls->corr_needed);
734
735 return dbuf;
736}
737
Harald Welte4a62eda2019-03-18 18:27:00 +0100738/*! Dump LCLS parameters (GCR excluded) into static string buffer for printing.
739 * \param[in] lcls pointer to the struct to print.
740 * \returns string representation of LCLS in static buffer or NULL on error. */
741char *osmo_lcls_dump(const struct osmo_lcls *lcls)
742{
743 return osmo_lcls_dump_buf(dbuf, sizeof(dbuf), lcls);
744}
745
Harald Welte179f3572019-03-18 18:38:47 +0100746char *osmo_lcls_dump_c(void *ctx, const struct osmo_lcls *lcls)
747{
748 char *buf = talloc_size(ctx, 256);
749 if (!buf)
750 return NULL;
751 return osmo_lcls_dump_buf(buf, 256, lcls);
752}
753
Max5ec0cf52019-01-15 16:37:09 +0100754/*! Dump GCR struct into string for printing.
Harald Welte4a62eda2019-03-18 18:27:00 +0100755 * \param[out] buf caller-allocated output string buffer
756 * \param[in] buf_len size of buf in bytes
Max5ec0cf52019-01-15 16:37:09 +0100757 * \param[in] lcls pointer to the struct to print.
758 * \returns string representation of GCR or NULL on error. */
Harald Welte4a62eda2019-03-18 18:27:00 +0100759char *osmo_gcr_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls)
Max5ec0cf52019-01-15 16:37:09 +0100760{
Harald Welte4a62eda2019-03-18 18:27:00 +0100761 struct osmo_strbuf s = { .buf = buf, .len = buf_len };
Max5ec0cf52019-01-15 16:37:09 +0100762
763 if (!lcls)
764 return NULL;
765
766 if (lcls->gcr_available) {
767 OSMO_STRBUF_PRINTF(s, "GCR NetID 0x%s, ", osmo_hexdump_nospc(lcls->gcr.net, lcls->gcr.net_len));
768 /* osmo_hexdump() uses static buffers so we can't call it twice withing the same parameter list */
769 OSMO_STRBUF_PRINTF(s, "Node 0x%x, CallRefID 0x%s", lcls->gcr.node, osmo_hexdump_nospc(lcls->gcr.cr, 5));
770 }
771
772 return dbuf;
773}
774
Harald Welte4a62eda2019-03-18 18:27:00 +0100775/*! Dump GCR struct into static string buffer for printing.
776 * \param[in] lcls pointer to the struct to print.
777 * \returns string representation of GCR in static buffer or NULL on error. */
778char *osmo_gcr_dump(const struct osmo_lcls *lcls)
779{
780 return osmo_gcr_dump_buf(dbuf, sizeof(dbuf), lcls);
781}
782
783
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200784/*! Encode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200785 * \param[out] msg Message Buffer to which IE is to be appended
786 * \param[in] ei Encryption Information to be encoded
787 * \returns number of bytes appended to \a msg */
Philipp Maier14e76b92017-03-28 18:36:52 +0200788uint8_t gsm0808_enc_encrypt_info(struct msgb *msg,
789 const struct gsm0808_encrypt_info *ei)
790{
791 unsigned int i;
792 uint8_t perm_algo = 0;
793 uint8_t *ptr;
794 uint8_t *old_tail;
795 uint8_t *tlv_len;
796
Philipp Maier14e76b92017-03-28 18:36:52 +0200797 OSMO_ASSERT(ei->key_len <= ARRAY_SIZE(ei->key));
798 OSMO_ASSERT(ei->perm_algo_len <= ENCRY_INFO_PERM_ALGO_MAXLEN);
799
800 msgb_put_u8(msg, GSM0808_IE_ENCRYPTION_INFORMATION);
801 tlv_len = msgb_put(msg, 1);
802 old_tail = msg->tail;
803
804 for (i = 0; i < ei->perm_algo_len; i++) {
805 /* Note: gsm_08_08.h defines the permitted algorithms
806 * as an enum which ranges from 0x01 to 0x08 */
807 OSMO_ASSERT(ei->perm_algo[i] != 0);
808 OSMO_ASSERT(ei->perm_algo[i] <= ENCRY_INFO_PERM_ALGO_MAXLEN);
809 perm_algo |= (1 << (ei->perm_algo[i] - 1));
810 }
811
812 msgb_put_u8(msg, perm_algo);
Neels Hofmeyr26e53b12021-06-10 00:47:03 +0200813 /* FIXME: 48.008 3.2.2.10 Encryption Information says:
814 * "When present, the key shall be 8 octets long." */
Philipp Maier14e76b92017-03-28 18:36:52 +0200815 ptr = msgb_put(msg, ei->key_len);
816 memcpy(ptr, ei->key, ei->key_len);
817
818 *tlv_len = (uint8_t) (msg->tail - old_tail);
819 return *tlv_len + 2;
820}
821
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200822/*! Decode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200823 * \param[out] ei Caller-provided memory to store encryption information
824 * \param[in] elem IE value to be decoded
825 * \param[in] len Length of \a elem in bytes
826 * \returns number of bytes parsed; negative on error */
Philipp Maier14e76b92017-03-28 18:36:52 +0200827int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei,
828 const uint8_t *elem, uint8_t len)
829{
830 uint8_t perm_algo;
831 unsigned int i;
832 unsigned int perm_algo_len = 0;
833 const uint8_t *old_elem = elem;
834
Philipp Maier14e76b92017-03-28 18:36:52 +0200835 if (!elem)
836 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200837 if (len == 0)
Philipp Maier14e76b92017-03-28 18:36:52 +0200838 return -EINVAL;
839
840 memset(ei, 0, sizeof(*ei));
841
842 perm_algo = *elem;
843 elem++;
844
845 for (i = 0; i < ENCRY_INFO_PERM_ALGO_MAXLEN; i++) {
846 if (perm_algo & (1 << i)) {
847 ei->perm_algo[perm_algo_len] = i + 1;
848 perm_algo_len++;
849 }
850 }
851 ei->perm_algo_len = perm_algo_len;
852
Neels Hofmeyr26e53b12021-06-10 00:47:03 +0200853 /* FIXME: 48.008 3.2.2.10 Encryption Information says:
854 * "When present, the key shall be 8 octets long." */
Philipp Maier14e76b92017-03-28 18:36:52 +0200855 ei->key_len = len - 1;
856 memcpy(ei->key, elem, ei->key_len);
857 elem+=ei->key_len;
858
859 return (int)(elem - old_elem);
860}
Philipp Maier783047e2017-03-29 11:35:50 +0200861
Neels Hofmeyr4a9756c2021-06-10 00:48:15 +0200862/*! Encode TS 48.008 Kc128 IE.
863 * \param[out] msg Message Buffer to which IE is to be appended.
864 * \param[in] kc128 Pointer to 16 bytes of Kc128 key data.
865 * \returns number of bytes appended to msg */
866int gsm0808_enc_kc128(struct msgb *msg, const uint8_t *kc128)
867{
868 uint8_t *start = msg->tail;
869 msgb_tv_fixed_put(msg, GSM0808_IE_KC_128, 16, kc128);
870 return msg->tail - start;
871}
872
873/*! Decode TS 48.008 Kc128 IE.
874 * \param[out] kc128 Target buffer for received Kc128 key, 16 bytes long.
875 * \param[in] elem IE value to be decoded (without IE discriminator).
876 * \param[in] len Length of elem in bytes.
877 * \returns number of bytes parsed; negative on error */
878int gsm0808_dec_kc128(uint8_t *kc128, const uint8_t *elem, uint8_t len)
879{
880 if (len != 16)
881 return -EINVAL;
882 memcpy(kc128, elem, 16);
883 return len;
884}
885
Pau Espin Pedrol85a0f112021-02-15 16:25:33 +0100886/* Store individual Cell Identifier information in a CGI, without clearing the remaining ones.
887 * This is useful to supplement one CGI with information from more than one Cell Identifier,
888 * which in turn is useful to match Cell Identifiers of differing kinds to each other.
889 * Before first invocation, clear the *dst struct externally, this function does only write those members
890 * that are present in parameter u.
891 */
892static void cell_id_to_cgi(struct osmo_cell_global_id *dst,
893 enum CELL_IDENT discr, const union gsm0808_cell_id_u *u)
894{
895 switch (discr) {
896 case CELL_IDENT_WHOLE_GLOBAL:
897 *dst = u->global;
898 return;
899
900 case CELL_IDENT_WHOLE_GLOBAL_PS:
901 dst->lai = u->global_ps.rai.lac;
902 dst->cell_identity = u->global_ps.cell_identity;
903 return;
904
905 case CELL_IDENT_LAC_AND_CI:
906 dst->lai.lac = u->lac_and_ci.lac;
907 dst->cell_identity = u->lac_and_ci.ci;
908 return;
909
910 case CELL_IDENT_CI:
911 dst->cell_identity = u->ci;
912 return;
913
914 case CELL_IDENT_LAI_AND_LAC:
915 dst->lai = u->lai_and_lac;
916 return;
917
918 case CELL_IDENT_LAC:
919 dst->lai.lac = u->lac;
920 return;
921
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +0100922 case CELL_IDENT_SAI:
923 dst->lai = u->sai.lai;
924 return;
925
Pau Espin Pedrol85a0f112021-02-15 16:25:33 +0100926 case CELL_IDENT_NO_CELL:
927 case CELL_IDENT_BSS:
928 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
929 case CELL_IDENT_UTRAN_RNC:
930 case CELL_IDENT_UTRAN_LAC_RNC:
931 /* No values to set. */
932 return;
933 }
934}
935
Harald Weltef2210032019-08-31 21:25:05 +0200936/* Return the size of the value part of a cell identifier of given type */
937int gsm0808_cell_id_size(enum CELL_IDENT discr)
938{
939 switch (discr) {
940 case CELL_IDENT_WHOLE_GLOBAL:
941 return 7;
942 case CELL_IDENT_LAC_AND_CI:
943 return 4;
944 case CELL_IDENT_CI:
945 return 2;
946 case CELL_IDENT_LAI_AND_LAC:
947 return 5;
948 case CELL_IDENT_LAC:
949 return 2;
950 case CELL_IDENT_BSS:
951 case CELL_IDENT_NO_CELL:
952 return 0;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +0100953 case CELL_IDENT_SAI:
954 return 7;
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100955 case CELL_IDENT_WHOLE_GLOBAL_PS:
956 return 8;
Harald Weltef2210032019-08-31 21:25:05 +0200957 default:
958 return -EINVAL;
959 }
960}
Harald Welteef7be492019-05-03 21:01:13 +0200961/*! Decode a single GSM 08.08 Cell ID list element payload
962 * \param[out] out caller-provided output union
963 * \param[in] discr Cell ID discriminator describing type to be decoded
964 * \param[in] buf User-provided input buffer containing binary Cell ID list element
965 * \param[in] len Length of buf
966 * \returns 0 on success; negative on error */
967int gsm0808_decode_cell_id_u(union gsm0808_cell_id_u *out, enum CELL_IDENT discr, const uint8_t *buf, unsigned int len)
968{
969 switch (discr) {
970 case CELL_IDENT_WHOLE_GLOBAL:
971 if (len < 7)
972 return -EINVAL;
973 decode_lai(buf, &out->global.lai);
974 out->global.cell_identity = osmo_load16be(buf + sizeof(struct gsm48_loc_area_id));
975 break;
976 case CELL_IDENT_LAC_AND_CI:
977 if (len < 4)
978 return -EINVAL;
979 out->lac_and_ci.lac = osmo_load16be(buf);
980 out->lac_and_ci.ci = osmo_load16be(buf + sizeof(uint16_t));
981 break;
982 case CELL_IDENT_CI:
983 if (len < 2)
984 return -EINVAL;
985 out->ci = osmo_load16be(buf);
986 break;
987 case CELL_IDENT_LAI_AND_LAC:
988 if (len < 5)
989 return -EINVAL;
990 decode_lai(buf, &out->lai_and_lac);
991 break;
992 case CELL_IDENT_LAC:
993 if (len < 2)
994 return -EINVAL;
995 out->lac = osmo_load16be(buf);
996 break;
997 case CELL_IDENT_BSS:
998 case CELL_IDENT_NO_CELL:
999 /* Does not have any list items */
1000 break;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001001 case CELL_IDENT_SAI:
1002 if (len < 7)
1003 return -EINVAL;
1004 decode_lai(buf, &out->sai.lai);
1005 out->sai.sac = osmo_load16be(buf + sizeof(struct gsm48_loc_area_id));
1006 break;
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01001007 case CELL_IDENT_WHOLE_GLOBAL_PS:
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001008 /* 3GPP TS 48.018 sec 11.3.9 "Cell Identifier" */
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01001009 if (len < 8)
1010 return -EINVAL;
1011 decode_lai(buf, (struct osmo_location_area_id *)&out->global_ps.rai); /* rai contains lai + non-decoded rac */
1012 out->global_ps.rai.rac = *(buf + sizeof(struct gsm48_loc_area_id));
1013 out->global_ps.cell_identity = osmo_load16be(buf + sizeof(struct gsm48_loc_area_id) + 1);
1014 break;
Harald Welteef7be492019-05-03 21:01:13 +02001015 default:
1016 /* Remaining cell identification types are not implemented. */
1017 return -EINVAL;
1018 }
1019 return 0;
1020}
1021
Harald Weltee87693c2019-05-03 20:06:50 +02001022void gsm0808_msgb_put_cell_id_u(struct msgb *msg, enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
1023{
1024 switch (id_discr) {
1025 case CELL_IDENT_WHOLE_GLOBAL: {
1026 const struct osmo_cell_global_id *id = &u->global;
1027 struct gsm48_loc_area_id lai;
1028 gsm48_generate_lai2(&lai, &id->lai);
1029 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
1030 msgb_put_u16(msg, id->cell_identity);
1031 break;
1032 }
1033 case CELL_IDENT_LAC_AND_CI: {
1034 const struct osmo_lac_and_ci_id *id = &u->lac_and_ci;
1035 msgb_put_u16(msg, id->lac);
1036 msgb_put_u16(msg, id->ci);
1037 break;
1038 }
1039 case CELL_IDENT_CI:
1040 msgb_put_u16(msg, u->ci);
1041 break;
1042 case CELL_IDENT_LAI_AND_LAC: {
1043 const struct osmo_location_area_id *id = &u->lai_and_lac;
1044 struct gsm48_loc_area_id lai;
1045 gsm48_generate_lai2(&lai, id);
1046 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
1047 break;
1048 }
1049 case CELL_IDENT_LAC:
1050 msgb_put_u16(msg, u->lac);
1051 break;
1052 case CELL_IDENT_BSS:
1053 case CELL_IDENT_NO_CELL:
1054 /* Does not have any list items */
1055 break;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001056
1057 case CELL_IDENT_SAI: {
1058 const struct osmo_service_area_id *id = &u->sai;
1059 struct gsm48_loc_area_id lai;
1060 gsm48_generate_lai2(&lai, &id->lai);
1061 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
1062 msgb_put_u16(msg, id->sac);
1063 break;
1064 }
1065
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01001066 case CELL_IDENT_WHOLE_GLOBAL_PS: {
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001067 /* 3GPP TS 48.018 sec 11.3.9 "Cell Identifier" */
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01001068 const struct osmo_cell_global_id_ps *id = &u->global_ps;
1069 struct gsm48_loc_area_id lai;
1070 gsm48_generate_lai2(&lai, &id->rai.lac);
1071 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
1072 memcpy(msgb_put(msg, 1), &id->rai.rac, 1);
1073 msgb_put_u16(msg, id->cell_identity);
1074 break;
1075 }
Harald Weltee87693c2019-05-03 20:06:50 +02001076 default:
1077 /* Support for other identifier list types is not implemented. */
1078 OSMO_ASSERT(false);
1079 }
1080}
1081
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001082/*! Encode TS 08.08 Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +02001083 * \param[out] msg Message Buffer to which IE is to be appended
1084 * \param[in] cil Cell ID List to be encoded
1085 * \returns number of bytes appended to \a msg */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001086uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg,
1087 const struct gsm0808_cell_id_list2 *cil)
1088{
1089 uint8_t *old_tail;
1090 uint8_t *tlv_len;
1091 unsigned int i;
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001092 uint8_t id_discr;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001093
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001094 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
1095 tlv_len = msgb_put(msg, 1);
1096 old_tail = msg->tail;
1097
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001098 /* CGI-PS is an osmocom-specific type. In here we don't care about the
1099 * PS part, only the CS one. */
1100 if (cil->id_discr == CELL_IDENT_WHOLE_GLOBAL_PS)
1101 id_discr = CELL_IDENT_WHOLE_GLOBAL;
1102 else
1103 id_discr = cil->id_discr & 0x0f;
1104
1105 msgb_put_u8(msg, id_discr);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001106
Alexander Couzens4e284b62019-06-23 01:53:43 +02001107 OSMO_ASSERT(cil->id_list_len <= GSM0808_CELL_ID_LIST2_MAXLEN);
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001108 for (i = 0; i < cil->id_list_len; i++) {
1109 if (cil->id_discr == CELL_IDENT_WHOLE_GLOBAL_PS) {
1110 union gsm0808_cell_id_u u;
1111 cell_id_to_cgi(&u.global, cil->id_discr, &cil->id_list[i]);
1112 gsm0808_msgb_put_cell_id_u(msg, CELL_IDENT_WHOLE_GLOBAL, &u);
1113 } else {
1114 gsm0808_msgb_put_cell_id_u(msg, cil->id_discr, &cil->id_list[i]);
1115 }
1116 }
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001117
1118 *tlv_len = (uint8_t) (msg->tail - old_tail);
1119 return *tlv_len + 2;
1120}
1121
1122/*! DEPRECATED: Use gsm0808_enc_cell_id_list2 instead.
1123 *
1124 * Encode TS 08.08 Cell Identifier List IE
1125 * \param[out] msg Message Buffer to which IE is to be appended
1126 * \param[in] cil Cell ID List to be encoded
1127 * \returns number of bytes appended to \a msg */
Philipp Maier783047e2017-03-29 11:35:50 +02001128uint8_t gsm0808_enc_cell_id_list(struct msgb *msg,
1129 const struct gsm0808_cell_id_list *cil)
1130{
1131 uint8_t *old_tail;
1132 uint8_t *tlv_len;
1133 unsigned int i;
1134
Philipp Maier783047e2017-03-29 11:35:50 +02001135 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
1136 tlv_len = msgb_put(msg, 1);
1137 old_tail = msg->tail;
1138
1139 msgb_put_u8(msg, cil->id_discr & 0x0f);
1140
1141 switch (cil->id_discr) {
1142 case CELL_IDENT_LAC:
Alexander Couzens4e284b62019-06-23 01:53:43 +02001143 OSMO_ASSERT(cil->id_list_len <= CELL_ID_LIST_LAC_MAXLEN);
Philipp Maier783047e2017-03-29 11:35:50 +02001144 for (i=0;i<cil->id_list_len;i++) {
1145 msgb_put_u16(msg, cil->id_list_lac[i]);
1146 }
1147 break;
1148 case CELL_IDENT_BSS:
1149 /* Does not have any list items */
1150 break;
1151 default:
1152 /* FIXME: Implement support for all identifier list elements */
1153 OSMO_ASSERT(false);
1154 }
1155
1156 *tlv_len = (uint8_t) (msg->tail - old_tail);
1157 return *tlv_len + 2;
1158}
1159
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001160static 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 +01001161 size_t *consumed)
1162{
1163 struct osmo_cell_global_id *id;
1164 uint16_t *ci_be;
1165 size_t lai_offset;
1166 int i = 0;
1167 const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*ci_be);
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].global;
Stefan Sperling2873bf12018-03-14 18:38:41 +01001174 lai_offset = i * elemlen;
Stefan Sperling23381452018-03-15 19:38:15 +01001175 decode_lai(&data[lai_offset], &id->lai);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001176 ci_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]);
1177 id->cell_identity = osmo_load16be(ci_be);
1178 *consumed += elemlen;
1179 remain -= elemlen;
1180 i++;
1181 }
1182
1183 return i;
1184}
1185
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001186static 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 +01001187 size_t *consumed)
1188{
1189 uint16_t *lacp_be, *ci_be;
1190 struct osmo_lac_and_ci_id *id;
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001191 int i = 0, j = 0;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001192 const size_t elemlen = sizeof(*lacp_be) + sizeof(*ci_be);
1193
1194 *consumed = 0;
1195
1196 if (remain < elemlen)
1197 return -EINVAL;
1198
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001199 lacp_be = (uint16_t *)(&data[j]);
1200 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001201 while (remain >= elemlen) {
1202 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1203 return -ENOSPC;
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001204 id = &cil->id_list[i++].lac_and_ci;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001205 id->lac = osmo_load16be(lacp_be);
1206 id->ci = osmo_load16be(ci_be);
1207 *consumed += elemlen;
1208 remain -= elemlen;
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001209 j += elemlen;
1210 lacp_be = (uint16_t *)(&data[j]);
1211 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001212 }
1213
1214 return i;
1215}
1216
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001217static int parse_cell_id_ci_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain,
1218 size_t *consumed)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001219{
1220 const uint16_t *ci_be = (const uint16_t *)data;
1221 int i = 0;
1222 const size_t elemlen = sizeof(*ci_be);
1223
1224 *consumed = 0;
1225 while (remain >= elemlen) {
1226 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1227 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001228 cil->id_list[i++].ci = osmo_load16be(ci_be++);
Stefan Sperling9c62fc62018-03-16 10:23:34 +01001229 *consumed += elemlen;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001230 remain -= elemlen;
1231 }
1232 return i;
1233}
1234
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001235static 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 +01001236 size_t *consumed)
1237{
1238 struct osmo_location_area_id *id;
1239 int i = 0;
1240 const size_t elemlen = sizeof(struct gsm48_loc_area_id);
1241
1242 *consumed = 0;
1243 while (remain >= elemlen) {
1244 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1245 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001246 id = &cil->id_list[i].lai_and_lac;
Stefan Sperling23381452018-03-15 19:38:15 +01001247 decode_lai(&data[i * elemlen], id);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001248 *consumed += elemlen;
1249 remain -= elemlen;
1250 i++;
1251 }
1252
1253 return i;
1254}
1255
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001256static 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 +01001257{
1258 const uint16_t *lac_be = (const uint16_t *)data;
1259 int i = 0;
1260 const size_t elemlen = sizeof(*lac_be);
1261
1262 *consumed = 0;
1263 while (remain >= elemlen) {
1264 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1265 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001266 cil->id_list[i++].lac = osmo_load16be(lac_be++);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001267 *consumed += elemlen;
1268 remain -= elemlen;
1269 }
1270 return i;
1271}
1272
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001273static int parse_cell_id_sai_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain, size_t *consumed)
1274{
1275 struct osmo_service_area_id *id;
1276 uint16_t *sac_be;
1277 size_t lai_offset;
1278 int i = 0;
1279 const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*sac_be);
1280
1281 *consumed = 0;
1282 while (remain >= elemlen) {
1283 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1284 return -ENOSPC;
1285 id = &cil->id_list[i].sai;
1286 lai_offset = i * elemlen;
1287 decode_lai(&data[lai_offset], &id->lai);
1288 sac_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]);
1289 id->sac = osmo_load16be(sac_be);
1290 *consumed += elemlen;
1291 remain -= elemlen;
1292 i++;
1293 }
1294
1295 return i;
1296}
1297
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001298/*! Decode Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +02001299 * \param[out] cil Caller-provided memory to store Cell ID list
1300 * \param[in] elem IE value to be decoded
1301 * \param[in] len Length of \a elem in bytes
1302 * \returns number of bytes parsed; negative on error */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001303int gsm0808_dec_cell_id_list2(struct gsm0808_cell_id_list2 *cil,
1304 const uint8_t *elem, uint8_t len)
1305{
1306 uint8_t id_discr;
1307 size_t bytes_elem = 0;
1308 int list_len = 0;
1309
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001310 if (!elem)
1311 return -EINVAL;
1312 if (len == 0)
1313 return -EINVAL;
1314
1315 memset(cil, 0, sizeof(*cil));
1316
1317 id_discr = *elem & 0x0f;
1318 elem++;
1319 len--;
1320
1321 switch (id_discr) {
1322 case CELL_IDENT_WHOLE_GLOBAL:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001323 list_len = parse_cell_id_global_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001324 break;
1325 case CELL_IDENT_LAC_AND_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001326 list_len = parse_cell_id_lac_and_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001327 break;
1328 case CELL_IDENT_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001329 list_len = parse_cell_id_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001330 break;
1331 case CELL_IDENT_LAI_AND_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001332 list_len = parse_cell_id_lai_and_lac(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001333 break;
1334 case CELL_IDENT_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001335 list_len = parse_cell_id_lac_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001336 break;
1337 case CELL_IDENT_BSS:
1338 case CELL_IDENT_NO_CELL:
1339 /* Does not have any list items */
1340 break;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001341 case CELL_IDENT_SAI:
1342 list_len = parse_cell_id_sai_list(cil, elem, len, &bytes_elem);
1343 break;
1344 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1345 case CELL_IDENT_UTRAN_RNC:
1346 case CELL_IDENT_UTRAN_LAC_RNC:
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001347 default:
1348 /* Remaining cell identification types are not implemented. */
1349 return -EINVAL;
1350 }
1351
1352 if (list_len < 0) /* parsing error */
1353 return list_len;
1354
1355 cil->id_discr = id_discr;
1356 cil->id_list_len = list_len;
1357
1358 /* One byte for the cell ID discriminator + any remaining bytes in
1359 * the IE which were consumed by the parser functions above. */
1360 return 1 + (int)bytes_elem;
1361}
1362
1363/*! DEPRECATED: Use gsm0808_dec_cell_id_list2 instead.
1364 *
1365 * Decode Cell Identifier List IE
1366 * \param[out] cil Caller-provided memory to store Cell ID list
1367 * \param[in] elem IE value to be decoded
1368 * \param[in] len Length of \a elem in bytes
1369 * \returns number of bytes parsed; negative on error */
Philipp Maier783047e2017-03-29 11:35:50 +02001370int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil,
1371 const uint8_t *elem, uint8_t len)
1372{
1373 uint8_t id_discr;
1374 const uint8_t *old_elem = elem;
1375 unsigned int item_count = 0;
1376
Philipp Maier783047e2017-03-29 11:35:50 +02001377 if (!elem)
1378 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +02001379 if (len == 0)
Philipp Maier783047e2017-03-29 11:35:50 +02001380 return -EINVAL;
1381
1382 memset(cil, 0, sizeof(*cil));
1383
1384 id_discr = *elem & 0x0f;
1385 elem++;
1386 len--;
1387
1388 cil->id_discr = id_discr;
1389
1390 switch (id_discr) {
1391 case CELL_IDENT_LAC:
1392 while (len >= 2) {
1393 cil->id_list_lac[item_count] = osmo_load16be(elem);
1394 elem += 2;
1395 item_count++;
1396 len -= 2;
1397 }
1398 case CELL_IDENT_BSS:
1399 /* Does not have any list items */
1400 break;
1401 default:
1402 /* FIXME: Implement support for all identifier list elements */
1403 return -EINVAL;
1404 }
1405
1406 cil->id_list_len = item_count;
1407 return (int)(elem - old_elem);
1408}
Harald Welte96e2a002017-06-12 21:44:18 +02001409
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001410static bool same_cell_id_list_entries(const struct gsm0808_cell_id_list2 *a, int ai,
1411 const struct gsm0808_cell_id_list2 *b, int bi)
1412{
1413 struct gsm0808_cell_id_list2 tmp = {
1414 .id_discr = a->id_discr,
1415 .id_list_len = 1,
1416 };
1417 uint8_t buf_a[32 + sizeof(struct msgb)];
1418 uint8_t buf_b[32 + sizeof(struct msgb)];
1419 struct msgb *msg_a = (void*)buf_a;
1420 struct msgb *msg_b = (void*)buf_b;
1421
1422 msg_a->data_len = 32;
1423 msg_b->data_len = 32;
1424 msgb_reset(msg_a);
1425 msgb_reset(msg_b);
1426
1427 if (a->id_discr != b->id_discr)
1428 return false;
1429 if (ai >= a->id_list_len
1430 || bi >= b->id_list_len)
1431 return false;
1432
1433 tmp.id_list[0] = a->id_list[ai];
1434 gsm0808_enc_cell_id_list2(msg_a, &tmp);
1435
1436 tmp.id_list[0] = b->id_list[bi];
1437 gsm0808_enc_cell_id_list2(msg_b, &tmp);
1438
1439 if (msg_a->len != msg_b->len)
1440 return false;
1441 if (memcmp(msg_a->data, msg_b->data, msg_a->len))
1442 return false;
1443
1444 return true;
1445}
1446
1447/*! Append entries from one Cell Identifier List to another.
1448 * The cell identifier types must be identical between the two lists.
1449 * \param dst[out] Append entries to this list.
1450 * \param src[in] Append these entries to \a dst.
1451 * \returns the nr of items added, or negative on error: -EINVAL if the id_discr mismatch
1452 * between the lists, -ENOSPC if the destination list does not have enough space. If an error is
1453 * returned, \a dst may have already been changed (particularly on -ENOSPC). Note that a return value
1454 * of zero may occur when the src->id_list_len is zero, or when all entries from \a src already exist
1455 * in \a dst, and does not indicate error per se. */
1456int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src)
1457{
1458 int i, j;
1459 int added = 0;
1460
1461 if (dst->id_list_len == 0
1462 && dst->id_discr != CELL_IDENT_BSS)
1463 dst->id_discr = src->id_discr;
1464 else if (dst->id_discr != src->id_discr)
1465 return -EINVAL;
1466
1467 for (i = 0; i < src->id_list_len; i++) {
1468 /* don't add duplicate entries */
1469 bool skip = false;
1470 for (j = 0; j < dst->id_list_len; j++) {
1471 if (same_cell_id_list_entries(dst, j, src, i)) {
1472 skip = true;
1473 break;
1474 }
1475 }
1476 if (skip)
1477 continue;
1478
1479 if (dst->id_list_len >= ARRAY_SIZE(dst->id_list))
1480 return -ENOSPC;
1481
1482 dst->id_list[dst->id_list_len++] = src->id_list[i];
1483 added ++;
1484 }
1485
1486 return added;
1487}
1488
Neels Hofmeyr38e58412018-05-25 16:56:35 +02001489/*! Convert a single Cell Identifier to a Cell Identifier List with one entry.
1490 * \param dst[out] Overwrite this list.
1491 * \param src[in] Set \a dst to contain exactly this item.
1492 */
1493void gsm0808_cell_id_to_list(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id *src)
1494{
1495 if (!dst)
1496 return;
1497 if (!src) {
1498 *dst = (struct gsm0808_cell_id_list2){
1499 .id_discr = CELL_IDENT_NO_CELL,
1500 };
1501 return;
1502 }
1503
1504 *dst = (struct gsm0808_cell_id_list2){
1505 .id_discr = src->id_discr,
1506 .id_list = { src->id },
1507 .id_list_len = 1,
1508 };
1509
1510 switch (src->id_discr) {
1511 case CELL_IDENT_NO_CELL:
1512 case CELL_IDENT_BSS:
1513 dst->id_list_len = 0;
1514 break;
1515 default:
1516 break;
1517 }
1518}
1519
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001520/*! Encode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1521 * \param[out] msg Message Buffer to which IE is to be appended
1522 * \param[in] ci Cell ID to be encoded
1523 * \returns number of bytes appended to \a msg */
1524uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci)
1525{
1526 uint8_t rc;
1527 uint8_t *ie_tag;
1528 struct gsm0808_cell_id_list2 cil = {
1529 .id_discr = ci->id_discr,
1530 .id_list = { ci->id },
1531 .id_list_len = 1,
1532 };
1533
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001534 ie_tag = msg->tail;
1535 rc = gsm0808_enc_cell_id_list2(msg, &cil);
1536
1537 if (rc <= 0)
1538 return rc;
1539
1540 *ie_tag = GSM0808_IE_CELL_IDENTIFIER;
1541 return rc;
1542}
1543
1544/*! Decode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1545 * \param[out] ci Caller-provided memory to store Cell ID.
1546 * \param[in] elem IE value to be decoded.
1547 * \param[in] len Length of \a elem in bytes.
1548 * \returns number of bytes parsed; negative on error */
1549int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len)
1550{
1551 struct gsm0808_cell_id_list2 cil;
1552 int rc;
1553 rc = gsm0808_dec_cell_id_list2(&cil, elem, len);
1554 if (rc < 0)
1555 return rc;
1556 if (cil.id_discr == CELL_IDENT_BSS || cil.id_discr == CELL_IDENT_NO_CELL) {
1557 if (cil.id_list_len != 0)
1558 return -EINVAL;
1559 } else {
1560 if (cil.id_list_len != 1)
1561 return -EINVAL;
1562 }
1563 ci->id_discr = cil.id_discr;
1564 ci->id = cil.id_list[0];
1565 return rc;
1566}
1567
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001568/*! Convert the representation of the permitted speech codec identifier
Philipp Maier3149b0d2017-06-02 13:22:34 +02001569 * that is used in struct gsm0808_channel_type to the speech codec
1570 * representation we use in struct gsm0808_speech_codec.
1571 * \param[in] perm_spch to be converted (see also gsm0808_permitted_speech)
1572 * \returns GSM speech codec type; negative on error */
1573int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch)
1574{
1575 /*! The speech codec type, which is used in the channel type field to
1576 * signal the permitted speech versions (codecs) has a different
1577 * encoding than the type field in the speech codec type element
1578 * (See also 3GPP TS 48.008, 3.2.2.11 and 3.2.2.103) */
1579
1580 switch (perm_spch) {
1581 case GSM0808_PERM_FR1:
1582 return GSM0808_SCT_FR1;
1583 case GSM0808_PERM_FR2:
1584 return GSM0808_SCT_FR2;
1585 case GSM0808_PERM_FR3:
1586 return GSM0808_SCT_FR3;
1587 case GSM0808_PERM_FR4:
1588 return GSM0808_SCT_FR4;
1589 case GSM0808_PERM_FR5:
1590 return GSM0808_SCT_FR5;
1591 case GSM0808_PERM_HR1:
1592 return GSM0808_SCT_HR1;
1593 case GSM0808_PERM_HR3:
1594 return GSM0808_SCT_HR3;
1595 case GSM0808_PERM_HR4:
1596 return GSM0808_SCT_HR4;
1597 case GSM0808_PERM_HR6:
1598 return GSM0808_SCT_HR6;
1599 }
1600
1601 /* Invalid input */
1602 return -EINVAL;
1603}
1604
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001605/*! Extrapolate a speech codec field from a given permitted speech
Philipp Maier884ba0f2017-06-02 13:49:16 +02001606 * parameter (channel type).
1607 * \param[out] sc Caller provided memory to store the resulting speech codec
1608 * \param[in] perm_spch value that is used to derive the speech codec info
1609 * (see also: enum gsm0808_speech_codec_type in gsm0808_utils.h)
1610 * \returns zero when successful; negative on error */
1611int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc,
1612 uint8_t perm_spch)
1613{
1614 int rc;
1615
1616 memset(sc, 0, sizeof(*sc));
1617
1618 /* Determine codec type */
1619 rc = gsm0808_chan_type_to_speech_codec(perm_spch);
1620 if (rc < 0)
1621 return -EINVAL;
1622 sc->type = (uint8_t) rc;
1623
1624 /* Depending on the speech codec type, pick a default codec
1625 * configuration that exactly matches the configuration on the
1626 * air interface. */
1627 switch (sc->type) {
1628 case GSM0808_SCT_FR3:
1629 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR;
1630 break;
1631 case GSM0808_SCT_FR4:
1632 sc->cfg = GSM0808_SC_CFG_DEFAULT_OFR_AMR_WB;
1633 break;
1634 case GSM0808_SCT_FR5:
1635 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR_WB;
1636 break;
1637 case GSM0808_SCT_HR3:
1638 sc->cfg = GSM0808_SC_CFG_DEFAULT_HR_AMR;
1639 break;
1640 case GSM0808_SCT_HR4:
1641 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR_WB;
1642 break;
1643 case GSM0808_SCT_HR6:
1644 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR;
1645 break;
1646 default:
1647 /* Note: Not all codec types specify a default setting,
1648 * in this case, we just set the field to zero. */
1649 sc->cfg = 0;
1650 }
1651
1652 /* Tag all codecs as "Full IP"
1653 * (see als 3GPP TS 48.008 3.2.2.103) */
1654 sc->fi = true;
1655
1656 return 0;
1657}
1658
Philipp Maier5f2eb152018-09-19 13:40:21 +02001659/*! Determine a set of AMR speech codec configuration bits (S0-S15) from a
1660 * given GSM 04.08 AMR configuration struct.
1661 * \param[in] cfg AMR configuration in GSM 04.08 format.
1662 * \param[in] hint if the resulting configuration shall be used with a FR or HR TCH.
1663 * \returns configuration bits (S0-S15) */
Philipp Maier369015c2018-09-21 09:07:20 +02001664uint16_t gsm0808_sc_cfg_from_gsm48_mr_cfg(const struct gsm48_multi_rate_conf *cfg,
Philipp Maier5f2eb152018-09-19 13:40:21 +02001665 bool fr)
1666{
1667 uint16_t s15_s0 = 0;
1668
1669 /* Check each rate bit in the AMR multirate configuration and pick the
1670 * matching default configuration as specified in 3GPP TS 28.062,
1671 * Table 7.11.3.1.3-2. */
1672 if (cfg->m4_75)
1673 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_4_75;
1674 if (cfg->m5_15)
1675 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_15;
1676 if (cfg->m5_90)
1677 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_90;
1678 if (cfg->m6_70)
1679 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_6_70;
1680 if (cfg->m7_40)
1681 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_40;
1682 if (cfg->m7_95)
1683 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_95;
1684 if (cfg->m10_2)
1685 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_10_2;
1686 if (cfg->m12_2)
1687 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_12_2;
1688
1689 /* Note: 3GPP TS 48.008, chapter 3GPP TS 48.008 states that for AMR
1690 * some of the configuration bits must be coded as zeros. The applied
1691 * bitmask matches the default codec settings. See also the definition
1692 * of enum gsm0808_speech_codec_defaults in gsm_08_08.h and
1693 * 3GPP TS 28.062, Table 7.11.3.1.3-2. */
1694 if (fr)
1695 s15_s0 &= GSM0808_SC_CFG_DEFAULT_FR_AMR;
1696 else
1697 s15_s0 &= GSM0808_SC_CFG_DEFAULT_HR_AMR;
1698
Philipp Maier94d79fd2019-03-01 10:40:48 +01001699 /* The mode that is encoded by S1 (Config-NB-Code = 1), takes a special
1700 * role as it does not stand for a single rate, but for up to four rates
1701 * at once (12.2, 7.4, 5.9, 4.75). We must check if the supplied cfg
1702 * covers this mode. If not, we need to make sure that the related
1703 * bit is removed. (See also 3GPP TS 28.062, Table 7.11.3.1.3-2) */
1704 if (!(cfg->m12_2 && cfg->m7_40 && cfg->m5_90 && cfg->m4_75) && fr)
1705 s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20;
1706 else if (!(cfg->m7_40 && cfg->m5_90 && cfg->m4_75))
1707 s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20;
1708
Philipp Maier5f2eb152018-09-19 13:40:21 +02001709 return s15_s0;
1710}
1711
Philipp Maier8515d032018-09-25 15:57:49 +02001712/*! Determine a GSM 04.08 AMR configuration struct from a set of speech codec
1713 * configuration bits (S0-S15)
1714 * \param[out] cfg AMR configuration in GSM 04.08 format.
Philipp Maier3713af82019-02-27 16:48:25 +01001715 * \param[in] s15_s0 configuration bits (S15-S0, non-ambiguous).
1716 * \returns zero when successful; negative on error */
1717int gsm48_mr_cfg_from_gsm0808_sc_cfg(struct gsm48_multi_rate_conf *cfg,
1718 uint16_t s15_s0)
Philipp Maier8515d032018-09-25 15:57:49 +02001719{
Philipp Maier3713af82019-02-27 16:48:25 +01001720 unsigned int count = 0;
1721
1722 /* Note: See also: 3GPP TS 28.062
1723 * Table 7.11.3.1.3-2: Preferred Configurations for the Adaptive
1724 * Multi-Rate Codec Types */
1725
1726 /* Note: The resulting multirate-configuration must not contain an
1727 * active set of more than four codec rates. The active set also
1728 * must contain at least one rate. */
1729
Philipp Maier8515d032018-09-25 15:57:49 +02001730 memset(cfg, 0, sizeof(*cfg));
Philipp Maier3713af82019-02-27 16:48:25 +01001731 cfg->ver = 1;
1732 cfg->icmi = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001733
1734 /* Strip option bits */
1735 s15_s0 &= 0x00ff;
1736
Philipp Maier3713af82019-02-27 16:48:25 +01001737 /* Rate 5,15k can never be selected (see table) */
1738 cfg->m5_15 = 0;
Philipp Maier8515d032018-09-25 15:57:49 +02001739
Neels Hofmeyra0f2b212021-04-14 22:45:13 +02001740 if (s15_s0 & GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20) {
Philipp Maier3713af82019-02-27 16:48:25 +01001741 /* Table Table 7.11.3.1.3-2 lists one mode that selects 4
1742 * rates at once (Config-NB-Code = 1). The rates selected
1743 * are known to be compatible between GERAN and UTRAN, since
1744 * an active set must not contain more than four rates at
1745 * a time, we ignore all other settings as they are either
1746 * redundaned or excess settings (invalid) */
Philipp Maier8515d032018-09-25 15:57:49 +02001747 cfg->m4_75 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001748 cfg->m5_90 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001749 cfg->m7_40 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001750 cfg->m12_2 = 1;
Philipp Maier3713af82019-02-27 16:48:25 +01001751 count += 4;
1752 }
Philipp Maier8515d032018-09-25 15:57:49 +02001753
Philipp Maier3713af82019-02-27 16:48:25 +01001754 /* Check the bits in s15_s0 and set the flags for the
1755 * respective rates. */
1756 if (s15_s0 & GSM0808_SC_CFG_AMR_4_75 && !cfg->m4_75) {
1757 if (count >= 4)
1758 return -EINVAL;
1759 cfg->m4_75 = 1;
1760 count++;
1761 }
1762 if (s15_s0 & GSM0808_SC_CFG_AMR_5_90 && !cfg->m5_90) {
1763 if (count >= 4)
1764 return -EINVAL;
1765 cfg->m5_90 = 1;
1766 count++;
1767 }
1768 if (s15_s0 & GSM0808_SC_CFG_AMR_6_70) {
1769 if (count >= 4)
1770 return -EINVAL;
1771 cfg->m6_70 = 1;
1772 count++;
1773 }
1774 if (s15_s0 & GSM0808_SC_CFG_AMR_7_40 && !cfg->m7_40) {
1775 if (count >= 4)
1776 return -EINVAL;
1777 cfg->m7_40 = 1;
1778 count++;
1779 }
1780 if (s15_s0 & GSM0808_SC_CFG_AMR_7_95) {
1781 if (count >= 4)
1782 return -EINVAL;
1783 cfg->m7_95 = 1;
1784 count++;
1785 }
1786 if (s15_s0 & GSM0808_SC_CFG_AMR_10_2) {
1787 if (count >= 4)
1788 return -EINVAL;
1789 cfg->m10_2 = 1;
1790 count++;
1791 }
1792 if (s15_s0 & GSM0808_SC_CFG_AMR_12_2 && !cfg->m12_2) {
1793 if (count >= 4)
1794 return -EINVAL;
1795 cfg->m12_2 = 1;
1796 count++;
1797 }
1798
1799 if (count == 0)
1800 return -EINVAL;
1801
1802 return 0;
Philipp Maier8515d032018-09-25 15:57:49 +02001803}
1804
Alexander Chemeris2ac8f912020-05-13 22:38:08 +03001805int gsm0808_get_cipher_reject_cause(const struct tlv_parsed *tp)
1806{
1807 return gsm0808_get_cause(tp);
1808}
1809
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001810/*! Print a human readable name of the cell identifier to the char buffer.
1811 * This is useful both for struct gsm0808_cell_id and struct gsm0808_cell_id_list2.
1812 * See also gsm0808_cell_id_name() and gsm0808_cell_id_list_name().
1813 * \param[out] buf Destination buffer to write string representation to.
1814 * \param[in] buflen Amount of memory available in \a buf.
1815 * \param[in] id_discr Cell Identifier type.
1816 * \param[in] u Cell Identifer value.
1817 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1818 * or that would have been written if the buffer were large enough.
1819 */
1820int gsm0808_cell_id_u_name(char *buf, size_t buflen,
1821 enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
1822{
1823 switch (id_discr) {
1824 case CELL_IDENT_LAC:
1825 return snprintf(buf, buflen, "%u", u->lac);
1826 case CELL_IDENT_CI:
1827 return snprintf(buf, buflen, "%u", u->ci);
1828 case CELL_IDENT_LAC_AND_CI:
1829 return snprintf(buf, buflen, "%u-%u", u->lac_and_ci.lac, u->lac_and_ci.ci);
1830 case CELL_IDENT_LAI_AND_LAC:
1831 return snprintf(buf, buflen, "%s", osmo_lai_name(&u->lai_and_lac));
1832 case CELL_IDENT_WHOLE_GLOBAL:
1833 return snprintf(buf, buflen, "%s", osmo_cgi_name(&u->global));
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01001834 case CELL_IDENT_WHOLE_GLOBAL_PS:
1835 return snprintf(buf, buflen, "%s", osmo_cgi_ps_name(&u->global_ps));
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001836 case CELL_IDENT_SAI:
1837 return snprintf(buf, buflen, "%s", osmo_sai_name(&u->sai));
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001838 default:
1839 /* For CELL_IDENT_BSS and CELL_IDENT_NO_CELL, just print the discriminator.
1840 * Same for kinds we have no string representation of yet. */
1841 return snprintf(buf, buflen, "%s", gsm0808_cell_id_discr_name(id_discr));
1842 }
1843}
1844
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02001845/*! Return true if the common information between the two Cell Identifiers match.
1846 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1847 * Note that CELL_IDENT_NO_CELL will always return false.
1848 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1849 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1850 * \param[in] discr1 Cell Identifier type.
1851 * \param[in] u1 Cell Identifier value.
1852 * \param[in] discr2 Other Cell Identifier type.
1853 * \param[in] u2 Other Cell Identifier value.
1854 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1855 * \returns True if the common fields of the above match.
1856 */
1857static bool gsm0808_cell_id_u_match(enum CELL_IDENT discr1, const union gsm0808_cell_id_u *u1,
1858 enum CELL_IDENT discr2, const union gsm0808_cell_id_u *u2,
1859 bool exact_match)
1860{
1861 struct osmo_cell_global_id a = {};
1862 struct osmo_cell_global_id b = {};
1863
1864 if (exact_match && discr1 != discr2)
1865 return false;
1866
1867 /* First handle the odd wildcard like CELL_IDENT kinds. We can't really match any of these. */
1868 switch (discr1) {
1869 case CELL_IDENT_NO_CELL:
1870 case CELL_IDENT_BSS:
1871 return discr1 == discr2;
1872 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1873 case CELL_IDENT_UTRAN_RNC:
1874 case CELL_IDENT_UTRAN_LAC_RNC:
1875 return false;
1876 default:
1877 break;
1878 }
1879 switch (discr2) {
1880 case CELL_IDENT_NO_CELL:
1881 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1882 case CELL_IDENT_UTRAN_RNC:
1883 case CELL_IDENT_UTRAN_LAC_RNC:
1884 case CELL_IDENT_BSS:
1885 return false;
1886 default:
1887 break;
1888 }
1889
1890 /* Enrich both sides to full CGI, then compare those. First set the *other* ID's values in case
1891 * they assign more items. For example:
1892 * u1 = LAC:42
1893 * u2 = LAC+CI:23+5
1894 * 1) a <- LAC+CI:23+5
1895 * 2) a <- LAC:42 so that a = LAC+CI:42+5
1896 * Now we can compare those two and find a mismatch. If the LAC were the same, we would get
1897 * identical LAC+CI and hence a match. */
1898
1899 cell_id_to_cgi(&a, discr2, u2);
1900 cell_id_to_cgi(&a, discr1, u1);
1901
1902 cell_id_to_cgi(&b, discr1, u1);
1903 cell_id_to_cgi(&b, discr2, u2);
1904
1905 return osmo_cgi_cmp(&a, &b) == 0;
1906}
1907
1908/*! Return true if the common information between the two Cell Identifiers match.
1909 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1910 * Note that CELL_IDENT_NO_CELL will always return false.
1911 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1912 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1913 * \param[in] id1 Cell Identifier.
1914 * \param[in] id2 Other Cell Identifier.
1915 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1916 * \returns True if the common fields of the above match.
1917 */
1918bool gsm0808_cell_ids_match(const struct gsm0808_cell_id *id1, const struct gsm0808_cell_id *id2, bool exact_match)
1919{
1920 return gsm0808_cell_id_u_match(id1->id_discr, &id1->id, id2->id_discr, &id2->id, exact_match);
1921}
1922
1923/*! Find an index in a Cell Identifier list that matches a given single Cell Identifer.
1924 * Compare \a id against each entry in \a list using gsm0808_cell_ids_match(), and return the list index
1925 * if a match is found. \a match_nr allows iterating all matches in the list. A match_nr <= 0 returns the
1926 * first match in the list, match_nr == 1 the second match, etc., and if match_nr exceeds the available
1927 * matches in the list, -1 is returned.
1928 * \param[in] id Cell Identifier to match.
1929 * \param[in] list Cell Identifier list to search in.
1930 * \param[in] match_nr Ignore this many matches.
1931 * \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 +01001932 * \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
1933 * entry).
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02001934 */
1935int gsm0808_cell_id_matches_list(const struct gsm0808_cell_id *id, const struct gsm0808_cell_id_list2 *list,
1936 unsigned int match_nr, bool exact_match)
1937{
1938 int i;
1939 for (i = 0; i < list->id_list_len; i++) {
1940 if (gsm0808_cell_id_u_match(id->id_discr, &id->id, list->id_discr, &list->id_list[i], exact_match)) {
1941 if (match_nr)
1942 match_nr--;
1943 else
1944 return i;
1945 }
1946 }
1947 return -1;
1948}
1949
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001950/*! Copy information from a CGI to form a Cell Identifier of the specified kind.
1951 * \param [out] cid Compose new Cell Identifier here.
1952 * \param [in] id_discr Which kind of Cell Identifier to compose.
1953 * \param [in] cgi Cell Global Identifier to form the Cell Identifier from.
1954 */
1955void gsm0808_cell_id_from_cgi(struct gsm0808_cell_id *cid, enum CELL_IDENT id_discr,
1956 const struct osmo_cell_global_id *cgi)
1957{
1958 *cid = (struct gsm0808_cell_id){
1959 .id_discr = id_discr,
1960 };
1961
1962 switch (id_discr) {
1963 case CELL_IDENT_WHOLE_GLOBAL:
1964 cid->id.global = *cgi;
1965 return;
1966
Pau Espin Pedrol20b763d2021-02-15 16:06:22 +01001967 case CELL_IDENT_WHOLE_GLOBAL_PS:
1968 cid->id.global_ps = (struct osmo_cell_global_id_ps){
1969 .rai = {
1970 .lac = cgi->lai,
1971 },
1972 .cell_identity = cgi->cell_identity,
1973 };
1974 return;
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001975 case CELL_IDENT_LAC_AND_CI:
1976 cid->id.lac_and_ci = (struct osmo_lac_and_ci_id){
1977 .lac = cgi->lai.lac,
1978 .ci = cgi->cell_identity,
1979 };
1980 return;
1981
1982 case CELL_IDENT_CI:
1983 cid->id.ci = cgi->cell_identity;
1984 return;
1985
1986 case CELL_IDENT_LAI:
1987 cid->id.lai_and_lac = cgi->lai;
1988 return;
1989
1990 case CELL_IDENT_LAC:
1991 cid->id.lac = cgi->lai.lac;
1992 return;
1993
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001994 case CELL_IDENT_SAI:
1995 cid->id.sai = (struct osmo_service_area_id){
1996 .lai = cgi->lai,
1997 };
1998 return;
1999
Neels Hofmeyr3a504532019-02-10 22:28:27 +01002000 case CELL_IDENT_NO_CELL:
2001 case CELL_IDENT_BSS:
2002 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
2003 case CELL_IDENT_UTRAN_RNC:
2004 case CELL_IDENT_UTRAN_LAC_RNC:
2005 default:
2006 return;
2007 };
2008}
2009
2010/*! Overwrite parts of cgi with values from a Cell Identifier.
2011 * Place only those items given in cid into cgi, leaving other values unchanged.
2012 * \param[out] cgi Cell Global Identity to write to.
2013 * \param[in] cid Cell Identity to read from.
2014 * \return a bitmask of items that were set: OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI; 0 if nothing was
2015 * written to cgi.
2016 */
2017int gsm0808_cell_id_to_cgi(struct osmo_cell_global_id *cgi, const struct gsm0808_cell_id *cid)
2018{
2019 switch (cid->id_discr) {
2020 case CELL_IDENT_WHOLE_GLOBAL:
2021 *cgi = cid->id.global;
2022 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
2023
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01002024 case CELL_IDENT_WHOLE_GLOBAL_PS:
2025 cgi->lai = cid->id.global_ps.rai.lac;
2026 cgi->cell_identity = cid->id.global_ps.cell_identity;
2027 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
2028
Neels Hofmeyr3a504532019-02-10 22:28:27 +01002029 case CELL_IDENT_LAC_AND_CI:
2030 cgi->lai.lac = cid->id.lac_and_ci.lac;
2031 cgi->cell_identity = cid->id.lac_and_ci.ci;
2032 return OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
2033
2034 case CELL_IDENT_CI:
2035 cgi->cell_identity = cid->id.ci;
2036 return OSMO_CGI_PART_CI;
2037
2038 case CELL_IDENT_LAI:
2039 cgi->lai = cid->id.lai_and_lac;
2040 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC;
2041
2042 case CELL_IDENT_LAC:
2043 cgi->lai.lac = cid->id.lac;
2044 return OSMO_CGI_PART_LAC;
2045
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01002046 case CELL_IDENT_SAI:
2047 cgi->lai = cid->id.sai.lai;
2048 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC;
2049
Neels Hofmeyr3a504532019-02-10 22:28:27 +01002050 case CELL_IDENT_NO_CELL:
2051 case CELL_IDENT_BSS:
2052 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
2053 case CELL_IDENT_UTRAN_RNC:
2054 case CELL_IDENT_UTRAN_LAC_RNC:
2055 default:
2056 return 0;
2057 };
2058}
2059
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002060/*! value_string[] for enum CELL_IDENT. */
2061const struct value_string gsm0808_cell_id_discr_names[] = {
2062 { CELL_IDENT_WHOLE_GLOBAL, "CGI" },
2063 { CELL_IDENT_LAC_AND_CI, "LAC-CI" },
2064 { CELL_IDENT_CI, "CI" },
2065 { CELL_IDENT_NO_CELL, "NO-CELL" },
2066 { CELL_IDENT_LAI_AND_LAC, "LAI" },
2067 { CELL_IDENT_LAC, "LAC" },
2068 { CELL_IDENT_BSS, "BSS" },
2069 { CELL_IDENT_UTRAN_PLMN_LAC_RNC, "UTRAN-PLMN-LAC-RNC" },
2070 { CELL_IDENT_UTRAN_RNC, "UTRAN-RNC" },
2071 { CELL_IDENT_UTRAN_LAC_RNC, "UTRAN-LAC-RNC" },
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01002072 { CELL_IDENT_SAI, "SAI" },
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01002073 { CELL_IDENT_WHOLE_GLOBAL_PS, "CGI-PS"},
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002074 { 0, NULL }
2075};
2076
2077#define APPEND_THING(func, args...) do { \
2078 int remain = buflen - (pos - buf); \
2079 int l = func(pos, remain, ##args); \
2080 if (l < 0 || l > remain) \
2081 pos = buf + buflen; \
2082 else \
2083 pos += l; \
2084 if (l > 0) \
2085 total_len += l; \
2086 } while(0)
2087#define APPEND_STR(fmt, args...) APPEND_THING(snprintf, fmt, ##args)
2088#define APPEND_CELL_ID_U(DISCR, U) APPEND_THING(gsm0808_cell_id_u_name, DISCR, U)
2089
Harald Welte179f3572019-03-18 18:38:47 +01002090char *gsm0808_cell_id_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id *cid)
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002091{
2092 char *pos = buf;
2093 int total_len = 0;
2094 APPEND_STR("%s:", gsm0808_cell_id_discr_name(cid->id_discr));
2095 APPEND_CELL_ID_U(cid->id_discr, &cid->id);
2096 return buf;
2097}
2098
2099/*! Return a human readable representation of a Cell Identifier, like "LAC:123"
2100 * or "CGI:001-01-42-23".
2101 * \param[in] cid Cell Identifer.
2102 * \returns String in a static buffer.
2103 */
2104const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid)
2105{
Harald Welte171ef822019-03-28 10:49:05 +01002106 static __thread char buf[64];
Harald Welte179f3572019-03-18 18:38:47 +01002107 return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002108}
2109
2110/*! Like gsm0808_cell_id_name() but uses a different static buffer.
2111 * \param[in] cid Cell Identifer.
2112 * \returns String in a static buffer.
2113 */
2114const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid)
2115{
Harald Welte171ef822019-03-28 10:49:05 +01002116 static __thread char buf[64];
Harald Welte179f3572019-03-18 18:38:47 +01002117 return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
2118}
2119
2120char *gsm0808_cell_id_name_c(const void *ctx, const struct gsm0808_cell_id *cid)
2121{
2122 char *buf = talloc_size(ctx, 64);
2123 if (!buf)
2124 return NULL;
2125 return gsm0808_cell_id_name_buf(buf, 64, cid);
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002126}
2127
2128/*! Return a human readable representation of the Cell Identifier List, like
2129 * "LAC[2]:{123, 456}".
2130 * The return value semantics are like snprintf() and thus allow ensuring a complete
2131 * untruncated string by determining the required string length from the return value.
2132 * If buflen > 0, always nul-terminate the string in buf, also when it is truncated.
2133 * If buflen == 0, do not modify buf, just return the would-be length.
2134 * \param[out] buf Destination buffer to write string representation to.
2135 * \param[in] buflen Amount of memory available in \a buf.
2136 * \param[in] cil Cell Identifer List.
2137 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
2138 * or that would have been written if the buffer were large enough.
2139 */
2140int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil)
2141{
2142 char *pos = buf;
2143 int total_len = 0;
2144 int i;
2145
2146 APPEND_STR("%s[%u]", gsm0808_cell_id_discr_name(cil->id_discr), cil->id_list_len);
2147
2148 switch (cil->id_discr) {
2149 case CELL_IDENT_BSS:
2150 case CELL_IDENT_NO_CELL:
2151 return total_len;
2152 default:
2153 break;
2154 }
2155
2156 APPEND_STR(":{");
2157
2158 for (i = 0; i < cil->id_list_len; i++) {
2159 if (i)
2160 APPEND_STR(", ");
2161 APPEND_CELL_ID_U(cil->id_discr, &cil->id_list[i]);
2162 }
2163
2164 APPEND_STR("}");
2165 return total_len;
2166}
2167
2168/*! Return a human-readable representation of \a cil in a static buffer.
2169 * If the list is too long, the output may be truncated.
2170 * See also gsm0808_cell_id_list_name_buf(). */
2171const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil)
2172{
Harald Welte171ef822019-03-28 10:49:05 +01002173 static __thread char buf[1024];
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002174 gsm0808_cell_id_list_name_buf(buf, sizeof(buf), cil);
2175 return buf;
2176}
2177
Harald Welte179f3572019-03-18 18:38:47 +01002178char *gsm0808_cell_id_list_name_c(const void *ctx, const struct gsm0808_cell_id_list2 *cil)
2179{
2180 char *buf = talloc_size(ctx, 1024);
2181 if (!buf)
2182 return NULL;
2183 gsm0808_cell_id_list_name_buf(buf, 1024, cil);
2184 return buf;
2185}
2186
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002187#undef APPEND_STR
2188#undef APPEND_CELL_ID_U
2189
Harald Welte4a62eda2019-03-18 18:27:00 +01002190char *gsm0808_channel_type_name_buf(char *buf, size_t buf_len, const struct gsm0808_channel_type *ct)
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02002191{
Harald Welte4a62eda2019-03-18 18:27:00 +01002192 snprintf(buf, buf_len, "ch_indctr=0x%x ch_rate_type=0x%x perm_spch=%s",
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02002193 ct->ch_indctr, ct->ch_rate_type,
2194 osmo_hexdump(ct->perm_spch, ct->perm_spch_len));
2195 return buf;
2196}
2197
Harald Welte4a62eda2019-03-18 18:27:00 +01002198const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct)
2199{
Harald Welte171ef822019-03-28 10:49:05 +01002200 static __thread char buf[128];
Harald Welte4a62eda2019-03-18 18:27:00 +01002201 return gsm0808_channel_type_name_buf(buf, sizeof(buf), ct);
2202}
2203
Harald Welte179f3572019-03-18 18:38:47 +01002204char *gsm0808_channel_type_name_c(const void *ctx, const struct gsm0808_channel_type *ct)
2205{
2206 char *buf = talloc_size(ctx, 128);
2207 if (!buf)
2208 return NULL;
2209 return gsm0808_channel_type_name_buf(buf, 128, ct);
2210}
2211
Harald Welte96e2a002017-06-12 21:44:18 +02002212/*! @} */