blob: 778630d6d64ac1977c9bc27876168cfc43737de9 [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) {
Vadim Yanitskiyf93ef0c2023-07-25 03:23:08 +0700258 header |= GSM0808_SCT_EXT;
Philipp Maier6f725d62017-03-24 18:03:17 +0100259 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. */
Vadim Yanitskiyf93ef0c2023-07-25 03:23:08 +0700350 if ((header & 0x0F) == GSM0808_SCT_EXT && len < 2)
Philipp Maier6f725d62017-03-24 18:03:17 +0100351 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
Vadim Yanitskiyf93ef0c2023-07-25 03:23:08 +0700365 if ((header & 0x0F) != GSM0808_SCT_EXT) {
Philipp Maier6f725d62017-03-24 18:03:17 +0100366 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 Smith57d4fae2023-02-27 10:28:40 +0100513 if (!ct->data_transparent)
Oliver Smithf047a4a2023-01-31 13:10:03 +0100514 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 */
Matan Perelman310b1072023-02-19 13:27:57 +0200544 msgb_put_u8(msg, 0);
Oliver Smithf047a4a2023-01-31 13:10:03 +0100545 break;
546 default:
547 OSMO_ASSERT(false);
Philipp Maiere0c65302017-03-28 17:05:40 +0200548 }
549
550 *tlv_len = (uint8_t) (msg->tail - old_tail);
551 return *tlv_len + 2;
552}
553
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200554/*! Decode TS 08.08 Channel Type IE
Harald Welte96e2a002017-06-12 21:44:18 +0200555 * \param[out] ct Caller-provided memory to store channel type
556 * \param[in] elem IE Value to be decoded
557 * \param[in] len Length of \a elem in bytes
558 * \returns number of bytes parsed; negative on error */
Philipp Maiere0c65302017-03-28 17:05:40 +0200559int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct,
560 const uint8_t *elem, uint8_t len)
561{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200562 /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */
Philipp Maiere0c65302017-03-28 17:05:40 +0200563 unsigned int i;
564 uint8_t byte;
565 const uint8_t *old_elem = elem;
566
Philipp Maiere0c65302017-03-28 17:05:40 +0200567 if (!elem)
568 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200569 if (len < 3 || len > 11)
Philipp Maiere0c65302017-03-28 17:05:40 +0200570 return -EINVAL;
571
572 memset(ct, 0, sizeof(*ct));
573
574 ct->ch_indctr = (*elem) & 0x0f;
575 elem++;
Oliver Smithdcaab852023-02-20 12:55:04 +0100576 ct->ch_rate_type = *elem;
Philipp Maiere0c65302017-03-28 17:05:40 +0200577 elem++;
578
Oliver Smithf047a4a2023-01-31 13:10:03 +0100579 switch (ct->ch_indctr) {
580 case GSM0808_CHAN_DATA:
Philipp Maiere0c65302017-03-28 17:05:40 +0200581 byte = *elem;
582 elem++;
Oliver Smith57d4fae2023-02-27 10:28:40 +0100583 ct->data_transparent = !(byte & 0x40); /* T/NT */
Oliver Smithf047a4a2023-01-31 13:10:03 +0100584 ct->data_rate = byte & 0x3f;
585
586 /* Optional extension for non-transparent service */
587 if (byte & 0x80) {
588 if (ct->data_transparent)
589 return -EINVAL;
590 if (elem - old_elem >= len)
591 return -EOVERFLOW;
592 byte = *elem;
593 elem++;
594
595 ct->data_rate_allowed_is_set = true;
596 ct->data_rate_allowed = byte & 0x7f;
597
598 /* Optional extension */
599 if (byte & 0x80) {
600 if (elem - old_elem >= len)
601 return -EOVERFLOW;
602 byte = *elem;
603 elem++;
604
605 ct->data_asym_pref_is_set = true;
606 ct->data_asym_pref = byte & 0x60 >> 5;
607 }
608 }
609 break;
610 case GSM0808_CHAN_SPEECH:
611 case GSM0808_CHAN_SPEECH_CTM_TEXT_TELEPHONY:
612 for (i = 0; i < ARRAY_SIZE(ct->perm_spch); i++) {
613 if (elem - old_elem >= len)
614 return -EOVERFLOW;
615
616 byte = *elem;
617 elem++;
618 ct->perm_spch[i] = byte & 0x7f;
619 if ((byte & 0x80) == 0x00)
620 break;
621 }
622 ct->perm_spch_len = i + 1;
623 break;
624 case GSM0808_CHAN_SIGN:
625 /* Octet 5 is spare */
626 break;
627 default:
628 return -ENOTSUP;
Philipp Maiere0c65302017-03-28 17:05:40 +0200629 }
Philipp Maiere0c65302017-03-28 17:05:40 +0200630
631 return (int)(elem - old_elem);
632}
Philipp Maier14e76b92017-03-28 18:36:52 +0200633
Max969fb2e2018-12-10 11:01:10 +0100634/*! Create BSSMAP Global Call Reference, 3GPP TS 48.008 §3.2.2.115.
635 * \param[out] msg Message Buffer for appending IE
636 * \param[in] g Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1
637 * \returns number of bytes added to \a msg or 0 on error */
Max47022152018-12-19 18:51:00 +0100638static uint8_t gsm0808_enc_gcr(struct msgb *msg, const struct osmo_gcr_parsed *g)
Max969fb2e2018-12-10 11:01:10 +0100639{
640 uint8_t enc, *len = msgb_tl_put(msg, GSM0808_IE_GLOBAL_CALL_REF);
641
642 enc = osmo_enc_gcr(msg, g);
643 if (!enc)
644 return 0;
645
646 *len = enc;
647 return enc + 2; /* type (1 byte) + length (1 byte) */
648}
649
650/*! Decode BSSMAP Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1.
651 * \param[out] gcr Caller-provided memory to store Global Call Reference
Max036012b2018-12-19 17:48:56 +0100652 * \param[in] tp IE values to be decoded
Max969fb2e2018-12-10 11:01:10 +0100653 * \returns number of bytes parsed; negative on error */
Max47022152018-12-19 18:51:00 +0100654static int gsm0808_dec_gcr(struct osmo_gcr_parsed *gcr, const struct tlv_parsed *tp)
Max969fb2e2018-12-10 11:01:10 +0100655{
656 int ret;
657 const uint8_t *buf = TLVP_VAL_MINLEN(tp, GSM0808_IE_GLOBAL_CALL_REF, OSMO_GCR_MIN_LEN);
658 if (!buf)
659 return -EINVAL;
660
661 ret = osmo_dec_gcr(gcr, buf, TLVP_LEN(tp, GSM0808_IE_GLOBAL_CALL_REF));
662 if (ret < 0)
663 return -ENOENT;
664
665 return 2 + ret;
666}
667
Max47022152018-12-19 18:51:00 +0100668/*! Add LCLS parameters to a given msgb, 3GPP TS 48.008 §3.2.2.115 - 3.2.2.120.
669 * \param[out] msg Message Buffer for appending IE
670 * \param[in] lcls LCLS-related data
671 * \returns number of bytes added to \a msg or 0 on error */
672uint8_t gsm0808_enc_lcls(struct msgb *msg, const struct osmo_lcls *lcls)
673{
674 uint8_t enc = 0;
675
676 /* LCLS: §3.2.2.115 Global Call Reference */
Max3b901252019-01-15 14:15:11 +0100677 if (lcls->gcr_available)
678 enc = gsm0808_enc_gcr(msg, &lcls->gcr);
Max47022152018-12-19 18:51:00 +0100679
680 /* LCLS: §3.2.2.116 Configuration */
681 if (lcls->config != GSM0808_LCLS_CFG_NA) {
682 msgb_tv_put(msg, GSM0808_IE_LCLS_CONFIG, lcls->config);
683 enc += 2;
684 }
685
686 /* LCLS: §3.2.2.117 Connection Status Control */
687 if (lcls->control != GSM0808_LCLS_CSC_NA) {
688 msgb_tv_put(msg, GSM0808_IE_LCLS_CONN_STATUS_CTRL, lcls->control);
689 enc += 2;
690 }
691
692 /* LCLS: §3.2.2.118 Correlation-Not-Needed */
693 if (!lcls->corr_needed) {
694 msgb_v_put(msg, GSM0808_IE_LCLS_CORR_NOT_NEEDED);
695 enc++;
696 }
697
698 return enc;
699}
700
701/*! Decode LCLS parameters to a given msgb, 3GPP TS 48.008 §3.2.2.115 - 3.2.2.120.
702 * \param[out] lcls Caller-provided memory to store LCLS-related data
703 * \param[in] tp IE values to be decoded
704 * \returns GCR size or negative on error */
705int gsm0808_dec_lcls(struct osmo_lcls *lcls, const struct tlv_parsed *tp)
706{
Max3b901252019-01-15 14:15:11 +0100707 int ret = gsm0808_dec_gcr(&lcls->gcr, tp);
Max47022152018-12-19 18:51:00 +0100708
Max3b901252019-01-15 14:15:11 +0100709 lcls->gcr_available = (ret < 0) ? false : true;
Max47022152018-12-19 18:51:00 +0100710 lcls->config = tlvp_val8(tp, GSM0808_IE_LCLS_CONFIG, GSM0808_LCLS_CFG_NA);
711 lcls->control = tlvp_val8(tp, GSM0808_IE_LCLS_CONN_STATUS_CTRL, GSM0808_LCLS_CSC_NA);
712 lcls->corr_needed = TLVP_PRESENT(tp, GSM0808_IE_LCLS_CORR_NOT_NEEDED) ? false : true;
713
714 return ret;
715}
716
Harald Welte171ef822019-03-28 10:49:05 +0100717static __thread char dbuf[256];
Max5ec0cf52019-01-15 16:37:09 +0100718
719/*! Dump LCLS parameters (GCR excluded) into string for printing.
Harald Welte4a62eda2019-03-18 18:27:00 +0100720 * \param[out] buf caller-allocated output string buffer
721 * \param[in] buf_len size of buf in bytes
Max5ec0cf52019-01-15 16:37:09 +0100722 * \param[in] lcls pointer to the struct to print.
723 * \returns string representation of LCLS or NULL on error. */
Harald Welte4a62eda2019-03-18 18:27:00 +0100724char *osmo_lcls_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls)
Max5ec0cf52019-01-15 16:37:09 +0100725{
Harald Welte4a62eda2019-03-18 18:27:00 +0100726 struct osmo_strbuf s = { .buf = buf, .len = buf_len };
Max5ec0cf52019-01-15 16:37:09 +0100727
728 if (!lcls)
729 return NULL;
730
731 OSMO_STRBUF_PRINTF(s, "LCLS Config: %s, Control: %s, Correlation-Needed: %u",
732 gsm0808_lcls_config_name(lcls->config),
733 gsm0808_lcls_control_name(lcls->control),
734 lcls->corr_needed);
735
736 return dbuf;
737}
738
Harald Welte4a62eda2019-03-18 18:27:00 +0100739/*! Dump LCLS parameters (GCR excluded) into static string buffer for printing.
740 * \param[in] lcls pointer to the struct to print.
741 * \returns string representation of LCLS in static buffer or NULL on error. */
742char *osmo_lcls_dump(const struct osmo_lcls *lcls)
743{
744 return osmo_lcls_dump_buf(dbuf, sizeof(dbuf), lcls);
745}
746
Harald Welte179f3572019-03-18 18:38:47 +0100747char *osmo_lcls_dump_c(void *ctx, const struct osmo_lcls *lcls)
748{
749 char *buf = talloc_size(ctx, 256);
750 if (!buf)
751 return NULL;
752 return osmo_lcls_dump_buf(buf, 256, lcls);
753}
754
Max5ec0cf52019-01-15 16:37:09 +0100755/*! Dump GCR struct into string for printing.
Harald Welte4a62eda2019-03-18 18:27:00 +0100756 * \param[out] buf caller-allocated output string buffer
757 * \param[in] buf_len size of buf in bytes
Max5ec0cf52019-01-15 16:37:09 +0100758 * \param[in] lcls pointer to the struct to print.
759 * \returns string representation of GCR or NULL on error. */
Harald Welte4a62eda2019-03-18 18:27:00 +0100760char *osmo_gcr_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls)
Max5ec0cf52019-01-15 16:37:09 +0100761{
Harald Welte4a62eda2019-03-18 18:27:00 +0100762 struct osmo_strbuf s = { .buf = buf, .len = buf_len };
Max5ec0cf52019-01-15 16:37:09 +0100763
764 if (!lcls)
765 return NULL;
766
767 if (lcls->gcr_available) {
768 OSMO_STRBUF_PRINTF(s, "GCR NetID 0x%s, ", osmo_hexdump_nospc(lcls->gcr.net, lcls->gcr.net_len));
769 /* osmo_hexdump() uses static buffers so we can't call it twice withing the same parameter list */
770 OSMO_STRBUF_PRINTF(s, "Node 0x%x, CallRefID 0x%s", lcls->gcr.node, osmo_hexdump_nospc(lcls->gcr.cr, 5));
771 }
772
773 return dbuf;
774}
775
Harald Welte4a62eda2019-03-18 18:27:00 +0100776/*! Dump GCR struct into static string buffer for printing.
777 * \param[in] lcls pointer to the struct to print.
778 * \returns string representation of GCR in static buffer or NULL on error. */
779char *osmo_gcr_dump(const struct osmo_lcls *lcls)
780{
781 return osmo_gcr_dump_buf(dbuf, sizeof(dbuf), lcls);
782}
783
784
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200785/*! Encode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200786 * \param[out] msg Message Buffer to which IE is to be appended
787 * \param[in] ei Encryption Information to be encoded
788 * \returns number of bytes appended to \a msg */
Philipp Maier14e76b92017-03-28 18:36:52 +0200789uint8_t gsm0808_enc_encrypt_info(struct msgb *msg,
790 const struct gsm0808_encrypt_info *ei)
791{
792 unsigned int i;
793 uint8_t perm_algo = 0;
794 uint8_t *ptr;
795 uint8_t *old_tail;
796 uint8_t *tlv_len;
797
Philipp Maier14e76b92017-03-28 18:36:52 +0200798 OSMO_ASSERT(ei->key_len <= ARRAY_SIZE(ei->key));
799 OSMO_ASSERT(ei->perm_algo_len <= ENCRY_INFO_PERM_ALGO_MAXLEN);
800
801 msgb_put_u8(msg, GSM0808_IE_ENCRYPTION_INFORMATION);
802 tlv_len = msgb_put(msg, 1);
803 old_tail = msg->tail;
804
805 for (i = 0; i < ei->perm_algo_len; i++) {
806 /* Note: gsm_08_08.h defines the permitted algorithms
807 * as an enum which ranges from 0x01 to 0x08 */
808 OSMO_ASSERT(ei->perm_algo[i] != 0);
809 OSMO_ASSERT(ei->perm_algo[i] <= ENCRY_INFO_PERM_ALGO_MAXLEN);
810 perm_algo |= (1 << (ei->perm_algo[i] - 1));
811 }
812
813 msgb_put_u8(msg, perm_algo);
Neels Hofmeyr26e53b12021-06-10 00:47:03 +0200814 /* FIXME: 48.008 3.2.2.10 Encryption Information says:
815 * "When present, the key shall be 8 octets long." */
Philipp Maier14e76b92017-03-28 18:36:52 +0200816 ptr = msgb_put(msg, ei->key_len);
817 memcpy(ptr, ei->key, ei->key_len);
818
819 *tlv_len = (uint8_t) (msg->tail - old_tail);
820 return *tlv_len + 2;
821}
822
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200823/*! Decode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200824 * \param[out] ei Caller-provided memory to store encryption information
825 * \param[in] elem IE value to be decoded
826 * \param[in] len Length of \a elem in bytes
827 * \returns number of bytes parsed; negative on error */
Philipp Maier14e76b92017-03-28 18:36:52 +0200828int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei,
829 const uint8_t *elem, uint8_t len)
830{
831 uint8_t perm_algo;
832 unsigned int i;
833 unsigned int perm_algo_len = 0;
834 const uint8_t *old_elem = elem;
835
Philipp Maier14e76b92017-03-28 18:36:52 +0200836 if (!elem)
837 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200838 if (len == 0)
Philipp Maier14e76b92017-03-28 18:36:52 +0200839 return -EINVAL;
840
841 memset(ei, 0, sizeof(*ei));
842
843 perm_algo = *elem;
844 elem++;
845
846 for (i = 0; i < ENCRY_INFO_PERM_ALGO_MAXLEN; i++) {
847 if (perm_algo & (1 << i)) {
848 ei->perm_algo[perm_algo_len] = i + 1;
849 perm_algo_len++;
850 }
851 }
852 ei->perm_algo_len = perm_algo_len;
853
Neels Hofmeyr26e53b12021-06-10 00:47:03 +0200854 /* FIXME: 48.008 3.2.2.10 Encryption Information says:
855 * "When present, the key shall be 8 octets long." */
Philipp Maier14e76b92017-03-28 18:36:52 +0200856 ei->key_len = len - 1;
857 memcpy(ei->key, elem, ei->key_len);
858 elem+=ei->key_len;
859
860 return (int)(elem - old_elem);
861}
Philipp Maier783047e2017-03-29 11:35:50 +0200862
Neels Hofmeyr4a9756c2021-06-10 00:48:15 +0200863/*! Encode TS 48.008 Kc128 IE.
864 * \param[out] msg Message Buffer to which IE is to be appended.
865 * \param[in] kc128 Pointer to 16 bytes of Kc128 key data.
866 * \returns number of bytes appended to msg */
867int gsm0808_enc_kc128(struct msgb *msg, const uint8_t *kc128)
868{
869 uint8_t *start = msg->tail;
870 msgb_tv_fixed_put(msg, GSM0808_IE_KC_128, 16, kc128);
871 return msg->tail - start;
872}
873
874/*! Decode TS 48.008 Kc128 IE.
875 * \param[out] kc128 Target buffer for received Kc128 key, 16 bytes long.
876 * \param[in] elem IE value to be decoded (without IE discriminator).
877 * \param[in] len Length of elem in bytes.
878 * \returns number of bytes parsed; negative on error */
879int gsm0808_dec_kc128(uint8_t *kc128, const uint8_t *elem, uint8_t len)
880{
881 if (len != 16)
882 return -EINVAL;
883 memcpy(kc128, elem, 16);
884 return len;
885}
886
Pau Espin Pedrol85a0f112021-02-15 16:25:33 +0100887/* Store individual Cell Identifier information in a CGI, without clearing the remaining ones.
888 * This is useful to supplement one CGI with information from more than one Cell Identifier,
889 * which in turn is useful to match Cell Identifiers of differing kinds to each other.
890 * Before first invocation, clear the *dst struct externally, this function does only write those members
891 * that are present in parameter u.
892 */
893static void cell_id_to_cgi(struct osmo_cell_global_id *dst,
894 enum CELL_IDENT discr, const union gsm0808_cell_id_u *u)
895{
896 switch (discr) {
897 case CELL_IDENT_WHOLE_GLOBAL:
898 *dst = u->global;
899 return;
900
901 case CELL_IDENT_WHOLE_GLOBAL_PS:
902 dst->lai = u->global_ps.rai.lac;
903 dst->cell_identity = u->global_ps.cell_identity;
904 return;
905
906 case CELL_IDENT_LAC_AND_CI:
907 dst->lai.lac = u->lac_and_ci.lac;
908 dst->cell_identity = u->lac_and_ci.ci;
909 return;
910
911 case CELL_IDENT_CI:
912 dst->cell_identity = u->ci;
913 return;
914
915 case CELL_IDENT_LAI_AND_LAC:
916 dst->lai = u->lai_and_lac;
917 return;
918
919 case CELL_IDENT_LAC:
920 dst->lai.lac = u->lac;
921 return;
922
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +0100923 case CELL_IDENT_SAI:
924 dst->lai = u->sai.lai;
925 return;
926
Pau Espin Pedrol85a0f112021-02-15 16:25:33 +0100927 case CELL_IDENT_NO_CELL:
928 case CELL_IDENT_BSS:
929 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
930 case CELL_IDENT_UTRAN_RNC:
931 case CELL_IDENT_UTRAN_LAC_RNC:
932 /* No values to set. */
933 return;
934 }
935}
936
Harald Weltef2210032019-08-31 21:25:05 +0200937/* Return the size of the value part of a cell identifier of given type */
938int gsm0808_cell_id_size(enum CELL_IDENT discr)
939{
940 switch (discr) {
941 case CELL_IDENT_WHOLE_GLOBAL:
942 return 7;
943 case CELL_IDENT_LAC_AND_CI:
944 return 4;
945 case CELL_IDENT_CI:
946 return 2;
947 case CELL_IDENT_LAI_AND_LAC:
948 return 5;
949 case CELL_IDENT_LAC:
950 return 2;
951 case CELL_IDENT_BSS:
952 case CELL_IDENT_NO_CELL:
953 return 0;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +0100954 case CELL_IDENT_SAI:
955 return 7;
Pau Espin Pedrolca33a712021-01-05 19:36:48 +0100956 case CELL_IDENT_WHOLE_GLOBAL_PS:
957 return 8;
Harald Weltef2210032019-08-31 21:25:05 +0200958 default:
959 return -EINVAL;
960 }
961}
Harald Welteef7be492019-05-03 21:01:13 +0200962/*! Decode a single GSM 08.08 Cell ID list element payload
963 * \param[out] out caller-provided output union
964 * \param[in] discr Cell ID discriminator describing type to be decoded
965 * \param[in] buf User-provided input buffer containing binary Cell ID list element
966 * \param[in] len Length of buf
967 * \returns 0 on success; negative on error */
968int gsm0808_decode_cell_id_u(union gsm0808_cell_id_u *out, enum CELL_IDENT discr, const uint8_t *buf, unsigned int len)
969{
970 switch (discr) {
971 case CELL_IDENT_WHOLE_GLOBAL:
972 if (len < 7)
973 return -EINVAL;
974 decode_lai(buf, &out->global.lai);
975 out->global.cell_identity = osmo_load16be(buf + sizeof(struct gsm48_loc_area_id));
976 break;
977 case CELL_IDENT_LAC_AND_CI:
978 if (len < 4)
979 return -EINVAL;
980 out->lac_and_ci.lac = osmo_load16be(buf);
981 out->lac_and_ci.ci = osmo_load16be(buf + sizeof(uint16_t));
982 break;
983 case CELL_IDENT_CI:
984 if (len < 2)
985 return -EINVAL;
986 out->ci = osmo_load16be(buf);
987 break;
988 case CELL_IDENT_LAI_AND_LAC:
989 if (len < 5)
990 return -EINVAL;
991 decode_lai(buf, &out->lai_and_lac);
992 break;
993 case CELL_IDENT_LAC:
994 if (len < 2)
995 return -EINVAL;
996 out->lac = osmo_load16be(buf);
997 break;
998 case CELL_IDENT_BSS:
999 case CELL_IDENT_NO_CELL:
1000 /* Does not have any list items */
1001 break;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001002 case CELL_IDENT_SAI:
1003 if (len < 7)
1004 return -EINVAL;
1005 decode_lai(buf, &out->sai.lai);
1006 out->sai.sac = osmo_load16be(buf + sizeof(struct gsm48_loc_area_id));
1007 break;
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01001008 case CELL_IDENT_WHOLE_GLOBAL_PS:
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001009 /* 3GPP TS 48.018 sec 11.3.9 "Cell Identifier" */
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01001010 if (len < 8)
1011 return -EINVAL;
1012 decode_lai(buf, (struct osmo_location_area_id *)&out->global_ps.rai); /* rai contains lai + non-decoded rac */
1013 out->global_ps.rai.rac = *(buf + sizeof(struct gsm48_loc_area_id));
1014 out->global_ps.cell_identity = osmo_load16be(buf + sizeof(struct gsm48_loc_area_id) + 1);
1015 break;
Harald Welteef7be492019-05-03 21:01:13 +02001016 default:
1017 /* Remaining cell identification types are not implemented. */
1018 return -EINVAL;
1019 }
1020 return 0;
1021}
1022
Harald Weltee87693c2019-05-03 20:06:50 +02001023void gsm0808_msgb_put_cell_id_u(struct msgb *msg, enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
1024{
1025 switch (id_discr) {
1026 case CELL_IDENT_WHOLE_GLOBAL: {
1027 const struct osmo_cell_global_id *id = &u->global;
1028 struct gsm48_loc_area_id lai;
1029 gsm48_generate_lai2(&lai, &id->lai);
1030 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
1031 msgb_put_u16(msg, id->cell_identity);
1032 break;
1033 }
1034 case CELL_IDENT_LAC_AND_CI: {
1035 const struct osmo_lac_and_ci_id *id = &u->lac_and_ci;
1036 msgb_put_u16(msg, id->lac);
1037 msgb_put_u16(msg, id->ci);
1038 break;
1039 }
1040 case CELL_IDENT_CI:
1041 msgb_put_u16(msg, u->ci);
1042 break;
1043 case CELL_IDENT_LAI_AND_LAC: {
1044 const struct osmo_location_area_id *id = &u->lai_and_lac;
1045 struct gsm48_loc_area_id lai;
1046 gsm48_generate_lai2(&lai, id);
1047 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
1048 break;
1049 }
1050 case CELL_IDENT_LAC:
1051 msgb_put_u16(msg, u->lac);
1052 break;
1053 case CELL_IDENT_BSS:
1054 case CELL_IDENT_NO_CELL:
1055 /* Does not have any list items */
1056 break;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001057
1058 case CELL_IDENT_SAI: {
1059 const struct osmo_service_area_id *id = &u->sai;
1060 struct gsm48_loc_area_id lai;
1061 gsm48_generate_lai2(&lai, &id->lai);
1062 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
1063 msgb_put_u16(msg, id->sac);
1064 break;
1065 }
1066
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01001067 case CELL_IDENT_WHOLE_GLOBAL_PS: {
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001068 /* 3GPP TS 48.018 sec 11.3.9 "Cell Identifier" */
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01001069 const struct osmo_cell_global_id_ps *id = &u->global_ps;
1070 struct gsm48_loc_area_id lai;
1071 gsm48_generate_lai2(&lai, &id->rai.lac);
1072 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
1073 memcpy(msgb_put(msg, 1), &id->rai.rac, 1);
1074 msgb_put_u16(msg, id->cell_identity);
1075 break;
1076 }
Harald Weltee87693c2019-05-03 20:06:50 +02001077 default:
1078 /* Support for other identifier list types is not implemented. */
1079 OSMO_ASSERT(false);
1080 }
1081}
1082
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001083/*! Encode TS 08.08 Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +02001084 * \param[out] msg Message Buffer to which IE is to be appended
1085 * \param[in] cil Cell ID List to be encoded
1086 * \returns number of bytes appended to \a msg */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001087uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg,
1088 const struct gsm0808_cell_id_list2 *cil)
1089{
1090 uint8_t *old_tail;
1091 uint8_t *tlv_len;
1092 unsigned int i;
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001093 uint8_t id_discr;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001094
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001095 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
1096 tlv_len = msgb_put(msg, 1);
1097 old_tail = msg->tail;
1098
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001099 /* CGI-PS is an osmocom-specific type. In here we don't care about the
1100 * PS part, only the CS one. */
1101 if (cil->id_discr == CELL_IDENT_WHOLE_GLOBAL_PS)
1102 id_discr = CELL_IDENT_WHOLE_GLOBAL;
1103 else
1104 id_discr = cil->id_discr & 0x0f;
1105
1106 msgb_put_u8(msg, id_discr);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001107
Alexander Couzens4e284b62019-06-23 01:53:43 +02001108 OSMO_ASSERT(cil->id_list_len <= GSM0808_CELL_ID_LIST2_MAXLEN);
Pau Espin Pedrol52489852021-02-15 16:26:37 +01001109 for (i = 0; i < cil->id_list_len; i++) {
1110 if (cil->id_discr == CELL_IDENT_WHOLE_GLOBAL_PS) {
1111 union gsm0808_cell_id_u u;
1112 cell_id_to_cgi(&u.global, cil->id_discr, &cil->id_list[i]);
1113 gsm0808_msgb_put_cell_id_u(msg, CELL_IDENT_WHOLE_GLOBAL, &u);
1114 } else {
1115 gsm0808_msgb_put_cell_id_u(msg, cil->id_discr, &cil->id_list[i]);
1116 }
1117 }
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001118
1119 *tlv_len = (uint8_t) (msg->tail - old_tail);
1120 return *tlv_len + 2;
1121}
1122
1123/*! DEPRECATED: Use gsm0808_enc_cell_id_list2 instead.
1124 *
1125 * Encode TS 08.08 Cell Identifier List IE
1126 * \param[out] msg Message Buffer to which IE is to be appended
1127 * \param[in] cil Cell ID List to be encoded
1128 * \returns number of bytes appended to \a msg */
Philipp Maier783047e2017-03-29 11:35:50 +02001129uint8_t gsm0808_enc_cell_id_list(struct msgb *msg,
1130 const struct gsm0808_cell_id_list *cil)
1131{
1132 uint8_t *old_tail;
1133 uint8_t *tlv_len;
1134 unsigned int i;
1135
Philipp Maier783047e2017-03-29 11:35:50 +02001136 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
1137 tlv_len = msgb_put(msg, 1);
1138 old_tail = msg->tail;
1139
1140 msgb_put_u8(msg, cil->id_discr & 0x0f);
1141
1142 switch (cil->id_discr) {
1143 case CELL_IDENT_LAC:
Alexander Couzens4e284b62019-06-23 01:53:43 +02001144 OSMO_ASSERT(cil->id_list_len <= CELL_ID_LIST_LAC_MAXLEN);
Philipp Maier783047e2017-03-29 11:35:50 +02001145 for (i=0;i<cil->id_list_len;i++) {
1146 msgb_put_u16(msg, cil->id_list_lac[i]);
1147 }
1148 break;
1149 case CELL_IDENT_BSS:
1150 /* Does not have any list items */
1151 break;
1152 default:
1153 /* FIXME: Implement support for all identifier list elements */
1154 OSMO_ASSERT(false);
1155 }
1156
1157 *tlv_len = (uint8_t) (msg->tail - old_tail);
1158 return *tlv_len + 2;
1159}
1160
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001161static 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 +01001162 size_t *consumed)
1163{
1164 struct osmo_cell_global_id *id;
1165 uint16_t *ci_be;
1166 size_t lai_offset;
1167 int i = 0;
1168 const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*ci_be);
1169
1170 *consumed = 0;
1171 while (remain >= elemlen) {
1172 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1173 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001174 id = &cil->id_list[i].global;
Stefan Sperling2873bf12018-03-14 18:38:41 +01001175 lai_offset = i * elemlen;
Stefan Sperling23381452018-03-15 19:38:15 +01001176 decode_lai(&data[lai_offset], &id->lai);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001177 ci_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]);
1178 id->cell_identity = osmo_load16be(ci_be);
1179 *consumed += elemlen;
1180 remain -= elemlen;
1181 i++;
1182 }
1183
1184 return i;
1185}
1186
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001187static 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 +01001188 size_t *consumed)
1189{
1190 uint16_t *lacp_be, *ci_be;
1191 struct osmo_lac_and_ci_id *id;
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001192 int i = 0, j = 0;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001193 const size_t elemlen = sizeof(*lacp_be) + sizeof(*ci_be);
1194
1195 *consumed = 0;
1196
1197 if (remain < elemlen)
1198 return -EINVAL;
1199
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001200 lacp_be = (uint16_t *)(&data[j]);
1201 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001202 while (remain >= elemlen) {
1203 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1204 return -ENOSPC;
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001205 id = &cil->id_list[i++].lac_and_ci;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001206 id->lac = osmo_load16be(lacp_be);
1207 id->ci = osmo_load16be(ci_be);
1208 *consumed += elemlen;
1209 remain -= elemlen;
Stefan Sperlinged4327c2018-03-16 11:02:59 +01001210 j += elemlen;
1211 lacp_be = (uint16_t *)(&data[j]);
1212 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001213 }
1214
1215 return i;
1216}
1217
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001218static int parse_cell_id_ci_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain,
1219 size_t *consumed)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001220{
1221 const uint16_t *ci_be = (const uint16_t *)data;
1222 int i = 0;
1223 const size_t elemlen = sizeof(*ci_be);
1224
1225 *consumed = 0;
1226 while (remain >= elemlen) {
1227 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1228 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001229 cil->id_list[i++].ci = osmo_load16be(ci_be++);
Stefan Sperling9c62fc62018-03-16 10:23:34 +01001230 *consumed += elemlen;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001231 remain -= elemlen;
1232 }
1233 return i;
1234}
1235
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001236static 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 +01001237 size_t *consumed)
1238{
1239 struct osmo_location_area_id *id;
1240 int i = 0;
1241 const size_t elemlen = sizeof(struct gsm48_loc_area_id);
1242
1243 *consumed = 0;
1244 while (remain >= elemlen) {
1245 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1246 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001247 id = &cil->id_list[i].lai_and_lac;
Stefan Sperling23381452018-03-15 19:38:15 +01001248 decode_lai(&data[i * elemlen], id);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001249 *consumed += elemlen;
1250 remain -= elemlen;
1251 i++;
1252 }
1253
1254 return i;
1255}
1256
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001257static 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 +01001258{
1259 const uint16_t *lac_be = (const uint16_t *)data;
1260 int i = 0;
1261 const size_t elemlen = sizeof(*lac_be);
1262
1263 *consumed = 0;
1264 while (remain >= elemlen) {
1265 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1266 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001267 cil->id_list[i++].lac = osmo_load16be(lac_be++);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001268 *consumed += elemlen;
1269 remain -= elemlen;
1270 }
1271 return i;
1272}
1273
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001274static int parse_cell_id_sai_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain, size_t *consumed)
1275{
1276 struct osmo_service_area_id *id;
1277 uint16_t *sac_be;
1278 size_t lai_offset;
1279 int i = 0;
1280 const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*sac_be);
1281
1282 *consumed = 0;
1283 while (remain >= elemlen) {
1284 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
1285 return -ENOSPC;
1286 id = &cil->id_list[i].sai;
1287 lai_offset = i * elemlen;
1288 decode_lai(&data[lai_offset], &id->lai);
1289 sac_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]);
1290 id->sac = osmo_load16be(sac_be);
1291 *consumed += elemlen;
1292 remain -= elemlen;
1293 i++;
1294 }
1295
1296 return i;
1297}
1298
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001299/*! Decode Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +02001300 * \param[out] cil Caller-provided memory to store Cell ID list
1301 * \param[in] elem IE value to be decoded
1302 * \param[in] len Length of \a elem in bytes
1303 * \returns number of bytes parsed; negative on error */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001304int gsm0808_dec_cell_id_list2(struct gsm0808_cell_id_list2 *cil,
1305 const uint8_t *elem, uint8_t len)
1306{
1307 uint8_t id_discr;
1308 size_t bytes_elem = 0;
1309 int list_len = 0;
1310
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001311 if (!elem)
1312 return -EINVAL;
1313 if (len == 0)
1314 return -EINVAL;
1315
1316 memset(cil, 0, sizeof(*cil));
1317
1318 id_discr = *elem & 0x0f;
1319 elem++;
1320 len--;
1321
1322 switch (id_discr) {
1323 case CELL_IDENT_WHOLE_GLOBAL:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001324 list_len = parse_cell_id_global_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001325 break;
1326 case CELL_IDENT_LAC_AND_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001327 list_len = parse_cell_id_lac_and_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001328 break;
1329 case CELL_IDENT_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001330 list_len = parse_cell_id_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001331 break;
1332 case CELL_IDENT_LAI_AND_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001333 list_len = parse_cell_id_lai_and_lac(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001334 break;
1335 case CELL_IDENT_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001336 list_len = parse_cell_id_lac_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001337 break;
1338 case CELL_IDENT_BSS:
1339 case CELL_IDENT_NO_CELL:
1340 /* Does not have any list items */
1341 break;
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001342 case CELL_IDENT_SAI:
1343 list_len = parse_cell_id_sai_list(cil, elem, len, &bytes_elem);
1344 break;
1345 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1346 case CELL_IDENT_UTRAN_RNC:
1347 case CELL_IDENT_UTRAN_LAC_RNC:
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001348 default:
1349 /* Remaining cell identification types are not implemented. */
1350 return -EINVAL;
1351 }
1352
1353 if (list_len < 0) /* parsing error */
1354 return list_len;
1355
1356 cil->id_discr = id_discr;
1357 cil->id_list_len = list_len;
1358
1359 /* One byte for the cell ID discriminator + any remaining bytes in
1360 * the IE which were consumed by the parser functions above. */
1361 return 1 + (int)bytes_elem;
1362}
1363
1364/*! DEPRECATED: Use gsm0808_dec_cell_id_list2 instead.
1365 *
1366 * Decode Cell Identifier List IE
1367 * \param[out] cil Caller-provided memory to store Cell ID list
1368 * \param[in] elem IE value to be decoded
1369 * \param[in] len Length of \a elem in bytes
1370 * \returns number of bytes parsed; negative on error */
Philipp Maier783047e2017-03-29 11:35:50 +02001371int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil,
1372 const uint8_t *elem, uint8_t len)
1373{
1374 uint8_t id_discr;
1375 const uint8_t *old_elem = elem;
1376 unsigned int item_count = 0;
1377
Philipp Maier783047e2017-03-29 11:35:50 +02001378 if (!elem)
1379 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +02001380 if (len == 0)
Philipp Maier783047e2017-03-29 11:35:50 +02001381 return -EINVAL;
1382
1383 memset(cil, 0, sizeof(*cil));
1384
1385 id_discr = *elem & 0x0f;
1386 elem++;
1387 len--;
1388
1389 cil->id_discr = id_discr;
1390
1391 switch (id_discr) {
1392 case CELL_IDENT_LAC:
1393 while (len >= 2) {
1394 cil->id_list_lac[item_count] = osmo_load16be(elem);
1395 elem += 2;
1396 item_count++;
1397 len -= 2;
1398 }
1399 case CELL_IDENT_BSS:
1400 /* Does not have any list items */
1401 break;
1402 default:
1403 /* FIXME: Implement support for all identifier list elements */
1404 return -EINVAL;
1405 }
1406
1407 cil->id_list_len = item_count;
1408 return (int)(elem - old_elem);
1409}
Harald Welte96e2a002017-06-12 21:44:18 +02001410
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001411static bool same_cell_id_list_entries(const struct gsm0808_cell_id_list2 *a, int ai,
1412 const struct gsm0808_cell_id_list2 *b, int bi)
1413{
1414 struct gsm0808_cell_id_list2 tmp = {
1415 .id_discr = a->id_discr,
1416 .id_list_len = 1,
1417 };
1418 uint8_t buf_a[32 + sizeof(struct msgb)];
1419 uint8_t buf_b[32 + sizeof(struct msgb)];
1420 struct msgb *msg_a = (void*)buf_a;
1421 struct msgb *msg_b = (void*)buf_b;
1422
1423 msg_a->data_len = 32;
1424 msg_b->data_len = 32;
1425 msgb_reset(msg_a);
1426 msgb_reset(msg_b);
1427
1428 if (a->id_discr != b->id_discr)
1429 return false;
1430 if (ai >= a->id_list_len
1431 || bi >= b->id_list_len)
1432 return false;
1433
1434 tmp.id_list[0] = a->id_list[ai];
1435 gsm0808_enc_cell_id_list2(msg_a, &tmp);
1436
1437 tmp.id_list[0] = b->id_list[bi];
1438 gsm0808_enc_cell_id_list2(msg_b, &tmp);
1439
1440 if (msg_a->len != msg_b->len)
1441 return false;
1442 if (memcmp(msg_a->data, msg_b->data, msg_a->len))
1443 return false;
1444
1445 return true;
1446}
1447
1448/*! Append entries from one Cell Identifier List to another.
1449 * The cell identifier types must be identical between the two lists.
1450 * \param dst[out] Append entries to this list.
1451 * \param src[in] Append these entries to \a dst.
1452 * \returns the nr of items added, or negative on error: -EINVAL if the id_discr mismatch
1453 * between the lists, -ENOSPC if the destination list does not have enough space. If an error is
1454 * returned, \a dst may have already been changed (particularly on -ENOSPC). Note that a return value
1455 * of zero may occur when the src->id_list_len is zero, or when all entries from \a src already exist
1456 * in \a dst, and does not indicate error per se. */
1457int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src)
1458{
1459 int i, j;
1460 int added = 0;
1461
1462 if (dst->id_list_len == 0
1463 && dst->id_discr != CELL_IDENT_BSS)
1464 dst->id_discr = src->id_discr;
1465 else if (dst->id_discr != src->id_discr)
1466 return -EINVAL;
1467
1468 for (i = 0; i < src->id_list_len; i++) {
1469 /* don't add duplicate entries */
1470 bool skip = false;
1471 for (j = 0; j < dst->id_list_len; j++) {
1472 if (same_cell_id_list_entries(dst, j, src, i)) {
1473 skip = true;
1474 break;
1475 }
1476 }
1477 if (skip)
1478 continue;
1479
1480 if (dst->id_list_len >= ARRAY_SIZE(dst->id_list))
1481 return -ENOSPC;
1482
1483 dst->id_list[dst->id_list_len++] = src->id_list[i];
1484 added ++;
1485 }
1486
1487 return added;
1488}
1489
Neels Hofmeyr38e58412018-05-25 16:56:35 +02001490/*! Convert a single Cell Identifier to a Cell Identifier List with one entry.
1491 * \param dst[out] Overwrite this list.
1492 * \param src[in] Set \a dst to contain exactly this item.
1493 */
1494void gsm0808_cell_id_to_list(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id *src)
1495{
1496 if (!dst)
1497 return;
1498 if (!src) {
1499 *dst = (struct gsm0808_cell_id_list2){
1500 .id_discr = CELL_IDENT_NO_CELL,
1501 };
1502 return;
1503 }
1504
1505 *dst = (struct gsm0808_cell_id_list2){
1506 .id_discr = src->id_discr,
1507 .id_list = { src->id },
1508 .id_list_len = 1,
1509 };
1510
1511 switch (src->id_discr) {
1512 case CELL_IDENT_NO_CELL:
1513 case CELL_IDENT_BSS:
1514 dst->id_list_len = 0;
1515 break;
1516 default:
1517 break;
1518 }
1519}
1520
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001521/*! Encode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1522 * \param[out] msg Message Buffer to which IE is to be appended
1523 * \param[in] ci Cell ID to be encoded
1524 * \returns number of bytes appended to \a msg */
1525uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci)
1526{
1527 uint8_t rc;
1528 uint8_t *ie_tag;
1529 struct gsm0808_cell_id_list2 cil = {
1530 .id_discr = ci->id_discr,
1531 .id_list = { ci->id },
1532 .id_list_len = 1,
1533 };
1534
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001535 ie_tag = msg->tail;
1536 rc = gsm0808_enc_cell_id_list2(msg, &cil);
1537
1538 if (rc <= 0)
1539 return rc;
1540
1541 *ie_tag = GSM0808_IE_CELL_IDENTIFIER;
1542 return rc;
1543}
1544
1545/*! Decode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1546 * \param[out] ci Caller-provided memory to store Cell ID.
1547 * \param[in] elem IE value to be decoded.
1548 * \param[in] len Length of \a elem in bytes.
1549 * \returns number of bytes parsed; negative on error */
1550int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len)
1551{
1552 struct gsm0808_cell_id_list2 cil;
1553 int rc;
1554 rc = gsm0808_dec_cell_id_list2(&cil, elem, len);
1555 if (rc < 0)
1556 return rc;
1557 if (cil.id_discr == CELL_IDENT_BSS || cil.id_discr == CELL_IDENT_NO_CELL) {
1558 if (cil.id_list_len != 0)
1559 return -EINVAL;
1560 } else {
1561 if (cil.id_list_len != 1)
1562 return -EINVAL;
1563 }
1564 ci->id_discr = cil.id_discr;
1565 ci->id = cil.id_list[0];
1566 return rc;
1567}
1568
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001569/*! Convert the representation of the permitted speech codec identifier
Philipp Maier3149b0d2017-06-02 13:22:34 +02001570 * that is used in struct gsm0808_channel_type to the speech codec
1571 * representation we use in struct gsm0808_speech_codec.
1572 * \param[in] perm_spch to be converted (see also gsm0808_permitted_speech)
1573 * \returns GSM speech codec type; negative on error */
1574int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch)
1575{
1576 /*! The speech codec type, which is used in the channel type field to
1577 * signal the permitted speech versions (codecs) has a different
1578 * encoding than the type field in the speech codec type element
1579 * (See also 3GPP TS 48.008, 3.2.2.11 and 3.2.2.103) */
1580
1581 switch (perm_spch) {
1582 case GSM0808_PERM_FR1:
1583 return GSM0808_SCT_FR1;
1584 case GSM0808_PERM_FR2:
1585 return GSM0808_SCT_FR2;
1586 case GSM0808_PERM_FR3:
1587 return GSM0808_SCT_FR3;
1588 case GSM0808_PERM_FR4:
1589 return GSM0808_SCT_FR4;
1590 case GSM0808_PERM_FR5:
1591 return GSM0808_SCT_FR5;
1592 case GSM0808_PERM_HR1:
1593 return GSM0808_SCT_HR1;
1594 case GSM0808_PERM_HR3:
1595 return GSM0808_SCT_HR3;
1596 case GSM0808_PERM_HR4:
1597 return GSM0808_SCT_HR4;
1598 case GSM0808_PERM_HR6:
1599 return GSM0808_SCT_HR6;
1600 }
1601
1602 /* Invalid input */
1603 return -EINVAL;
1604}
1605
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001606/*! Extrapolate a speech codec field from a given permitted speech
Philipp Maier884ba0f2017-06-02 13:49:16 +02001607 * parameter (channel type).
1608 * \param[out] sc Caller provided memory to store the resulting speech codec
1609 * \param[in] perm_spch value that is used to derive the speech codec info
1610 * (see also: enum gsm0808_speech_codec_type in gsm0808_utils.h)
1611 * \returns zero when successful; negative on error */
1612int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc,
1613 uint8_t perm_spch)
1614{
1615 int rc;
1616
1617 memset(sc, 0, sizeof(*sc));
1618
1619 /* Determine codec type */
1620 rc = gsm0808_chan_type_to_speech_codec(perm_spch);
1621 if (rc < 0)
1622 return -EINVAL;
1623 sc->type = (uint8_t) rc;
1624
1625 /* Depending on the speech codec type, pick a default codec
1626 * configuration that exactly matches the configuration on the
1627 * air interface. */
1628 switch (sc->type) {
1629 case GSM0808_SCT_FR3:
1630 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR;
1631 break;
1632 case GSM0808_SCT_FR4:
1633 sc->cfg = GSM0808_SC_CFG_DEFAULT_OFR_AMR_WB;
1634 break;
1635 case GSM0808_SCT_FR5:
1636 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR_WB;
1637 break;
1638 case GSM0808_SCT_HR3:
1639 sc->cfg = GSM0808_SC_CFG_DEFAULT_HR_AMR;
1640 break;
1641 case GSM0808_SCT_HR4:
1642 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR_WB;
1643 break;
1644 case GSM0808_SCT_HR6:
1645 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR;
1646 break;
1647 default:
1648 /* Note: Not all codec types specify a default setting,
1649 * in this case, we just set the field to zero. */
1650 sc->cfg = 0;
1651 }
1652
1653 /* Tag all codecs as "Full IP"
1654 * (see als 3GPP TS 48.008 3.2.2.103) */
1655 sc->fi = true;
1656
1657 return 0;
1658}
1659
Philipp Maier5f2eb152018-09-19 13:40:21 +02001660/*! Determine a set of AMR speech codec configuration bits (S0-S15) from a
1661 * given GSM 04.08 AMR configuration struct.
1662 * \param[in] cfg AMR configuration in GSM 04.08 format.
1663 * \param[in] hint if the resulting configuration shall be used with a FR or HR TCH.
1664 * \returns configuration bits (S0-S15) */
Philipp Maier369015c2018-09-21 09:07:20 +02001665uint16_t gsm0808_sc_cfg_from_gsm48_mr_cfg(const struct gsm48_multi_rate_conf *cfg,
Philipp Maier5f2eb152018-09-19 13:40:21 +02001666 bool fr)
1667{
1668 uint16_t s15_s0 = 0;
1669
1670 /* Check each rate bit in the AMR multirate configuration and pick the
1671 * matching default configuration as specified in 3GPP TS 28.062,
1672 * Table 7.11.3.1.3-2. */
1673 if (cfg->m4_75)
1674 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_4_75;
1675 if (cfg->m5_15)
1676 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_15;
1677 if (cfg->m5_90)
1678 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_90;
1679 if (cfg->m6_70)
1680 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_6_70;
1681 if (cfg->m7_40)
1682 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_40;
1683 if (cfg->m7_95)
1684 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_95;
1685 if (cfg->m10_2)
1686 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_10_2;
1687 if (cfg->m12_2)
1688 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_12_2;
1689
1690 /* Note: 3GPP TS 48.008, chapter 3GPP TS 48.008 states that for AMR
1691 * some of the configuration bits must be coded as zeros. The applied
1692 * bitmask matches the default codec settings. See also the definition
1693 * of enum gsm0808_speech_codec_defaults in gsm_08_08.h and
1694 * 3GPP TS 28.062, Table 7.11.3.1.3-2. */
1695 if (fr)
1696 s15_s0 &= GSM0808_SC_CFG_DEFAULT_FR_AMR;
1697 else
1698 s15_s0 &= GSM0808_SC_CFG_DEFAULT_HR_AMR;
1699
Philipp Maier94d79fd2019-03-01 10:40:48 +01001700 /* The mode that is encoded by S1 (Config-NB-Code = 1), takes a special
1701 * role as it does not stand for a single rate, but for up to four rates
1702 * at once (12.2, 7.4, 5.9, 4.75). We must check if the supplied cfg
1703 * covers this mode. If not, we need to make sure that the related
1704 * bit is removed. (See also 3GPP TS 28.062, Table 7.11.3.1.3-2) */
1705 if (!(cfg->m12_2 && cfg->m7_40 && cfg->m5_90 && cfg->m4_75) && fr)
1706 s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20;
1707 else if (!(cfg->m7_40 && cfg->m5_90 && cfg->m4_75))
1708 s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20;
1709
Philipp Maier5f2eb152018-09-19 13:40:21 +02001710 return s15_s0;
1711}
1712
Philipp Maier8515d032018-09-25 15:57:49 +02001713/*! Determine a GSM 04.08 AMR configuration struct from a set of speech codec
1714 * configuration bits (S0-S15)
1715 * \param[out] cfg AMR configuration in GSM 04.08 format.
Philipp Maier3713af82019-02-27 16:48:25 +01001716 * \param[in] s15_s0 configuration bits (S15-S0, non-ambiguous).
1717 * \returns zero when successful; negative on error */
1718int gsm48_mr_cfg_from_gsm0808_sc_cfg(struct gsm48_multi_rate_conf *cfg,
1719 uint16_t s15_s0)
Philipp Maier8515d032018-09-25 15:57:49 +02001720{
Philipp Maier3713af82019-02-27 16:48:25 +01001721 unsigned int count = 0;
1722
1723 /* Note: See also: 3GPP TS 28.062
1724 * Table 7.11.3.1.3-2: Preferred Configurations for the Adaptive
1725 * Multi-Rate Codec Types */
1726
1727 /* Note: The resulting multirate-configuration must not contain an
1728 * active set of more than four codec rates. The active set also
1729 * must contain at least one rate. */
1730
Philipp Maier8515d032018-09-25 15:57:49 +02001731 memset(cfg, 0, sizeof(*cfg));
Philipp Maier3713af82019-02-27 16:48:25 +01001732 cfg->ver = 1;
1733 cfg->icmi = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001734
1735 /* Strip option bits */
1736 s15_s0 &= 0x00ff;
1737
Philipp Maier3713af82019-02-27 16:48:25 +01001738 /* Rate 5,15k can never be selected (see table) */
1739 cfg->m5_15 = 0;
Philipp Maier8515d032018-09-25 15:57:49 +02001740
Neels Hofmeyra0f2b212021-04-14 22:45:13 +02001741 if (s15_s0 & GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20) {
Philipp Maier3713af82019-02-27 16:48:25 +01001742 /* Table Table 7.11.3.1.3-2 lists one mode that selects 4
1743 * rates at once (Config-NB-Code = 1). The rates selected
1744 * are known to be compatible between GERAN and UTRAN, since
1745 * an active set must not contain more than four rates at
1746 * a time, we ignore all other settings as they are either
1747 * redundaned or excess settings (invalid) */
Philipp Maier8515d032018-09-25 15:57:49 +02001748 cfg->m4_75 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001749 cfg->m5_90 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001750 cfg->m7_40 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001751 cfg->m12_2 = 1;
Philipp Maier3713af82019-02-27 16:48:25 +01001752 count += 4;
1753 }
Philipp Maier8515d032018-09-25 15:57:49 +02001754
Philipp Maier3713af82019-02-27 16:48:25 +01001755 /* Check the bits in s15_s0 and set the flags for the
1756 * respective rates. */
1757 if (s15_s0 & GSM0808_SC_CFG_AMR_4_75 && !cfg->m4_75) {
1758 if (count >= 4)
1759 return -EINVAL;
1760 cfg->m4_75 = 1;
1761 count++;
1762 }
1763 if (s15_s0 & GSM0808_SC_CFG_AMR_5_90 && !cfg->m5_90) {
1764 if (count >= 4)
1765 return -EINVAL;
1766 cfg->m5_90 = 1;
1767 count++;
1768 }
1769 if (s15_s0 & GSM0808_SC_CFG_AMR_6_70) {
1770 if (count >= 4)
1771 return -EINVAL;
1772 cfg->m6_70 = 1;
1773 count++;
1774 }
1775 if (s15_s0 & GSM0808_SC_CFG_AMR_7_40 && !cfg->m7_40) {
1776 if (count >= 4)
1777 return -EINVAL;
1778 cfg->m7_40 = 1;
1779 count++;
1780 }
1781 if (s15_s0 & GSM0808_SC_CFG_AMR_7_95) {
1782 if (count >= 4)
1783 return -EINVAL;
1784 cfg->m7_95 = 1;
1785 count++;
1786 }
1787 if (s15_s0 & GSM0808_SC_CFG_AMR_10_2) {
1788 if (count >= 4)
1789 return -EINVAL;
1790 cfg->m10_2 = 1;
1791 count++;
1792 }
1793 if (s15_s0 & GSM0808_SC_CFG_AMR_12_2 && !cfg->m12_2) {
1794 if (count >= 4)
1795 return -EINVAL;
1796 cfg->m12_2 = 1;
1797 count++;
1798 }
1799
1800 if (count == 0)
1801 return -EINVAL;
1802
1803 return 0;
Philipp Maier8515d032018-09-25 15:57:49 +02001804}
1805
Alexander Chemeris2ac8f912020-05-13 22:38:08 +03001806int gsm0808_get_cipher_reject_cause(const struct tlv_parsed *tp)
1807{
1808 return gsm0808_get_cause(tp);
1809}
1810
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001811/*! Print a human readable name of the cell identifier to the char buffer.
1812 * This is useful both for struct gsm0808_cell_id and struct gsm0808_cell_id_list2.
1813 * See also gsm0808_cell_id_name() and gsm0808_cell_id_list_name().
1814 * \param[out] buf Destination buffer to write string representation to.
1815 * \param[in] buflen Amount of memory available in \a buf.
1816 * \param[in] id_discr Cell Identifier type.
1817 * \param[in] u Cell Identifer value.
1818 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1819 * or that would have been written if the buffer were large enough.
1820 */
1821int gsm0808_cell_id_u_name(char *buf, size_t buflen,
1822 enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
1823{
1824 switch (id_discr) {
1825 case CELL_IDENT_LAC:
1826 return snprintf(buf, buflen, "%u", u->lac);
1827 case CELL_IDENT_CI:
1828 return snprintf(buf, buflen, "%u", u->ci);
1829 case CELL_IDENT_LAC_AND_CI:
1830 return snprintf(buf, buflen, "%u-%u", u->lac_and_ci.lac, u->lac_and_ci.ci);
1831 case CELL_IDENT_LAI_AND_LAC:
1832 return snprintf(buf, buflen, "%s", osmo_lai_name(&u->lai_and_lac));
1833 case CELL_IDENT_WHOLE_GLOBAL:
1834 return snprintf(buf, buflen, "%s", osmo_cgi_name(&u->global));
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01001835 case CELL_IDENT_WHOLE_GLOBAL_PS:
1836 return snprintf(buf, buflen, "%s", osmo_cgi_ps_name(&u->global_ps));
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001837 case CELL_IDENT_SAI:
1838 return snprintf(buf, buflen, "%s", osmo_sai_name(&u->sai));
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001839 default:
1840 /* For CELL_IDENT_BSS and CELL_IDENT_NO_CELL, just print the discriminator.
1841 * Same for kinds we have no string representation of yet. */
1842 return snprintf(buf, buflen, "%s", gsm0808_cell_id_discr_name(id_discr));
1843 }
1844}
1845
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02001846/*! Return true if the common information between the two Cell Identifiers match.
1847 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1848 * Note that CELL_IDENT_NO_CELL will always return false.
1849 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1850 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1851 * \param[in] discr1 Cell Identifier type.
1852 * \param[in] u1 Cell Identifier value.
1853 * \param[in] discr2 Other Cell Identifier type.
1854 * \param[in] u2 Other Cell Identifier value.
1855 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1856 * \returns True if the common fields of the above match.
1857 */
1858static bool gsm0808_cell_id_u_match(enum CELL_IDENT discr1, const union gsm0808_cell_id_u *u1,
1859 enum CELL_IDENT discr2, const union gsm0808_cell_id_u *u2,
1860 bool exact_match)
1861{
1862 struct osmo_cell_global_id a = {};
1863 struct osmo_cell_global_id b = {};
1864
1865 if (exact_match && discr1 != discr2)
1866 return false;
1867
1868 /* First handle the odd wildcard like CELL_IDENT kinds. We can't really match any of these. */
1869 switch (discr1) {
1870 case CELL_IDENT_NO_CELL:
1871 case CELL_IDENT_BSS:
1872 return discr1 == discr2;
1873 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1874 case CELL_IDENT_UTRAN_RNC:
1875 case CELL_IDENT_UTRAN_LAC_RNC:
1876 return false;
1877 default:
1878 break;
1879 }
1880 switch (discr2) {
1881 case CELL_IDENT_NO_CELL:
1882 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1883 case CELL_IDENT_UTRAN_RNC:
1884 case CELL_IDENT_UTRAN_LAC_RNC:
1885 case CELL_IDENT_BSS:
1886 return false;
1887 default:
1888 break;
1889 }
1890
1891 /* Enrich both sides to full CGI, then compare those. First set the *other* ID's values in case
1892 * they assign more items. For example:
1893 * u1 = LAC:42
1894 * u2 = LAC+CI:23+5
1895 * 1) a <- LAC+CI:23+5
1896 * 2) a <- LAC:42 so that a = LAC+CI:42+5
1897 * Now we can compare those two and find a mismatch. If the LAC were the same, we would get
1898 * identical LAC+CI and hence a match. */
1899
1900 cell_id_to_cgi(&a, discr2, u2);
1901 cell_id_to_cgi(&a, discr1, u1);
1902
1903 cell_id_to_cgi(&b, discr1, u1);
1904 cell_id_to_cgi(&b, discr2, u2);
1905
1906 return osmo_cgi_cmp(&a, &b) == 0;
1907}
1908
1909/*! Return true if the common information between the two Cell Identifiers match.
1910 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1911 * Note that CELL_IDENT_NO_CELL will always return false.
1912 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1913 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1914 * \param[in] id1 Cell Identifier.
1915 * \param[in] id2 Other Cell Identifier.
1916 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1917 * \returns True if the common fields of the above match.
1918 */
1919bool gsm0808_cell_ids_match(const struct gsm0808_cell_id *id1, const struct gsm0808_cell_id *id2, bool exact_match)
1920{
1921 return gsm0808_cell_id_u_match(id1->id_discr, &id1->id, id2->id_discr, &id2->id, exact_match);
1922}
1923
1924/*! Find an index in a Cell Identifier list that matches a given single Cell Identifer.
1925 * Compare \a id against each entry in \a list using gsm0808_cell_ids_match(), and return the list index
1926 * if a match is found. \a match_nr allows iterating all matches in the list. A match_nr <= 0 returns the
1927 * first match in the list, match_nr == 1 the second match, etc., and if match_nr exceeds the available
1928 * matches in the list, -1 is returned.
1929 * \param[in] id Cell Identifier to match.
1930 * \param[in] list Cell Identifier list to search in.
1931 * \param[in] match_nr Ignore this many matches.
1932 * \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 +01001933 * \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
1934 * entry).
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02001935 */
1936int gsm0808_cell_id_matches_list(const struct gsm0808_cell_id *id, const struct gsm0808_cell_id_list2 *list,
1937 unsigned int match_nr, bool exact_match)
1938{
1939 int i;
1940 for (i = 0; i < list->id_list_len; i++) {
1941 if (gsm0808_cell_id_u_match(id->id_discr, &id->id, list->id_discr, &list->id_list[i], exact_match)) {
1942 if (match_nr)
1943 match_nr--;
1944 else
1945 return i;
1946 }
1947 }
1948 return -1;
1949}
1950
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001951/*! Copy information from a CGI to form a Cell Identifier of the specified kind.
1952 * \param [out] cid Compose new Cell Identifier here.
1953 * \param [in] id_discr Which kind of Cell Identifier to compose.
1954 * \param [in] cgi Cell Global Identifier to form the Cell Identifier from.
1955 */
1956void gsm0808_cell_id_from_cgi(struct gsm0808_cell_id *cid, enum CELL_IDENT id_discr,
1957 const struct osmo_cell_global_id *cgi)
1958{
1959 *cid = (struct gsm0808_cell_id){
1960 .id_discr = id_discr,
1961 };
1962
1963 switch (id_discr) {
1964 case CELL_IDENT_WHOLE_GLOBAL:
1965 cid->id.global = *cgi;
1966 return;
1967
Pau Espin Pedrol20b763d2021-02-15 16:06:22 +01001968 case CELL_IDENT_WHOLE_GLOBAL_PS:
1969 cid->id.global_ps = (struct osmo_cell_global_id_ps){
1970 .rai = {
1971 .lac = cgi->lai,
1972 },
1973 .cell_identity = cgi->cell_identity,
1974 };
1975 return;
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001976 case CELL_IDENT_LAC_AND_CI:
1977 cid->id.lac_and_ci = (struct osmo_lac_and_ci_id){
1978 .lac = cgi->lai.lac,
1979 .ci = cgi->cell_identity,
1980 };
1981 return;
1982
1983 case CELL_IDENT_CI:
1984 cid->id.ci = cgi->cell_identity;
1985 return;
1986
1987 case CELL_IDENT_LAI:
1988 cid->id.lai_and_lac = cgi->lai;
1989 return;
1990
1991 case CELL_IDENT_LAC:
1992 cid->id.lac = cgi->lai.lac;
1993 return;
1994
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01001995 case CELL_IDENT_SAI:
1996 cid->id.sai = (struct osmo_service_area_id){
1997 .lai = cgi->lai,
1998 };
1999 return;
2000
Neels Hofmeyr3a504532019-02-10 22:28:27 +01002001 case CELL_IDENT_NO_CELL:
2002 case CELL_IDENT_BSS:
2003 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
2004 case CELL_IDENT_UTRAN_RNC:
2005 case CELL_IDENT_UTRAN_LAC_RNC:
2006 default:
2007 return;
2008 };
2009}
2010
2011/*! Overwrite parts of cgi with values from a Cell Identifier.
2012 * Place only those items given in cid into cgi, leaving other values unchanged.
2013 * \param[out] cgi Cell Global Identity to write to.
2014 * \param[in] cid Cell Identity to read from.
2015 * \return a bitmask of items that were set: OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI; 0 if nothing was
2016 * written to cgi.
2017 */
2018int gsm0808_cell_id_to_cgi(struct osmo_cell_global_id *cgi, const struct gsm0808_cell_id *cid)
2019{
2020 switch (cid->id_discr) {
2021 case CELL_IDENT_WHOLE_GLOBAL:
2022 *cgi = cid->id.global;
2023 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
2024
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01002025 case CELL_IDENT_WHOLE_GLOBAL_PS:
2026 cgi->lai = cid->id.global_ps.rai.lac;
2027 cgi->cell_identity = cid->id.global_ps.cell_identity;
2028 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
2029
Neels Hofmeyr3a504532019-02-10 22:28:27 +01002030 case CELL_IDENT_LAC_AND_CI:
2031 cgi->lai.lac = cid->id.lac_and_ci.lac;
2032 cgi->cell_identity = cid->id.lac_and_ci.ci;
2033 return OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
2034
2035 case CELL_IDENT_CI:
2036 cgi->cell_identity = cid->id.ci;
2037 return OSMO_CGI_PART_CI;
2038
2039 case CELL_IDENT_LAI:
2040 cgi->lai = cid->id.lai_and_lac;
2041 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC;
2042
2043 case CELL_IDENT_LAC:
2044 cgi->lai.lac = cid->id.lac;
2045 return OSMO_CGI_PART_LAC;
2046
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01002047 case CELL_IDENT_SAI:
2048 cgi->lai = cid->id.sai.lai;
2049 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC;
2050
Neels Hofmeyr3a504532019-02-10 22:28:27 +01002051 case CELL_IDENT_NO_CELL:
2052 case CELL_IDENT_BSS:
2053 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
2054 case CELL_IDENT_UTRAN_RNC:
2055 case CELL_IDENT_UTRAN_LAC_RNC:
2056 default:
2057 return 0;
2058 };
2059}
2060
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002061/*! value_string[] for enum CELL_IDENT. */
2062const struct value_string gsm0808_cell_id_discr_names[] = {
2063 { CELL_IDENT_WHOLE_GLOBAL, "CGI" },
2064 { CELL_IDENT_LAC_AND_CI, "LAC-CI" },
2065 { CELL_IDENT_CI, "CI" },
2066 { CELL_IDENT_NO_CELL, "NO-CELL" },
2067 { CELL_IDENT_LAI_AND_LAC, "LAI" },
2068 { CELL_IDENT_LAC, "LAC" },
2069 { CELL_IDENT_BSS, "BSS" },
2070 { CELL_IDENT_UTRAN_PLMN_LAC_RNC, "UTRAN-PLMN-LAC-RNC" },
2071 { CELL_IDENT_UTRAN_RNC, "UTRAN-RNC" },
2072 { CELL_IDENT_UTRAN_LAC_RNC, "UTRAN-LAC-RNC" },
Pau Espin Pedrolb5551ee2022-02-16 13:09:32 +01002073 { CELL_IDENT_SAI, "SAI" },
Pau Espin Pedrolca33a712021-01-05 19:36:48 +01002074 { CELL_IDENT_WHOLE_GLOBAL_PS, "CGI-PS"},
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002075 { 0, NULL }
2076};
2077
2078#define APPEND_THING(func, args...) do { \
2079 int remain = buflen - (pos - buf); \
2080 int l = func(pos, remain, ##args); \
2081 if (l < 0 || l > remain) \
2082 pos = buf + buflen; \
2083 else \
2084 pos += l; \
2085 if (l > 0) \
2086 total_len += l; \
2087 } while(0)
2088#define APPEND_STR(fmt, args...) APPEND_THING(snprintf, fmt, ##args)
2089#define APPEND_CELL_ID_U(DISCR, U) APPEND_THING(gsm0808_cell_id_u_name, DISCR, U)
2090
Harald Welte179f3572019-03-18 18:38:47 +01002091char *gsm0808_cell_id_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id *cid)
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002092{
2093 char *pos = buf;
2094 int total_len = 0;
2095 APPEND_STR("%s:", gsm0808_cell_id_discr_name(cid->id_discr));
2096 APPEND_CELL_ID_U(cid->id_discr, &cid->id);
2097 return buf;
2098}
2099
2100/*! Return a human readable representation of a Cell Identifier, like "LAC:123"
2101 * or "CGI:001-01-42-23".
2102 * \param[in] cid Cell Identifer.
2103 * \returns String in a static buffer.
2104 */
2105const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid)
2106{
Harald Welte171ef822019-03-28 10:49:05 +01002107 static __thread char buf[64];
Harald Welte179f3572019-03-18 18:38:47 +01002108 return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002109}
2110
2111/*! Like gsm0808_cell_id_name() but uses a different static buffer.
2112 * \param[in] cid Cell Identifer.
2113 * \returns String in a static buffer.
2114 */
2115const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid)
2116{
Harald Welte171ef822019-03-28 10:49:05 +01002117 static __thread char buf[64];
Harald Welte179f3572019-03-18 18:38:47 +01002118 return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
2119}
2120
2121char *gsm0808_cell_id_name_c(const void *ctx, const struct gsm0808_cell_id *cid)
2122{
2123 char *buf = talloc_size(ctx, 64);
2124 if (!buf)
2125 return NULL;
2126 return gsm0808_cell_id_name_buf(buf, 64, cid);
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002127}
2128
2129/*! Return a human readable representation of the Cell Identifier List, like
2130 * "LAC[2]:{123, 456}".
2131 * The return value semantics are like snprintf() and thus allow ensuring a complete
2132 * untruncated string by determining the required string length from the return value.
2133 * If buflen > 0, always nul-terminate the string in buf, also when it is truncated.
2134 * If buflen == 0, do not modify buf, just return the would-be length.
2135 * \param[out] buf Destination buffer to write string representation to.
2136 * \param[in] buflen Amount of memory available in \a buf.
2137 * \param[in] cil Cell Identifer List.
2138 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
2139 * or that would have been written if the buffer were large enough.
2140 */
2141int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil)
2142{
2143 char *pos = buf;
2144 int total_len = 0;
2145 int i;
2146
2147 APPEND_STR("%s[%u]", gsm0808_cell_id_discr_name(cil->id_discr), cil->id_list_len);
2148
2149 switch (cil->id_discr) {
2150 case CELL_IDENT_BSS:
2151 case CELL_IDENT_NO_CELL:
2152 return total_len;
2153 default:
2154 break;
2155 }
2156
2157 APPEND_STR(":{");
2158
2159 for (i = 0; i < cil->id_list_len; i++) {
2160 if (i)
2161 APPEND_STR(", ");
2162 APPEND_CELL_ID_U(cil->id_discr, &cil->id_list[i]);
2163 }
2164
2165 APPEND_STR("}");
2166 return total_len;
2167}
2168
2169/*! Return a human-readable representation of \a cil in a static buffer.
2170 * If the list is too long, the output may be truncated.
2171 * See also gsm0808_cell_id_list_name_buf(). */
2172const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil)
2173{
Harald Welte171ef822019-03-28 10:49:05 +01002174 static __thread char buf[1024];
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002175 gsm0808_cell_id_list_name_buf(buf, sizeof(buf), cil);
2176 return buf;
2177}
2178
Harald Welte179f3572019-03-18 18:38:47 +01002179char *gsm0808_cell_id_list_name_c(const void *ctx, const struct gsm0808_cell_id_list2 *cil)
2180{
2181 char *buf = talloc_size(ctx, 1024);
2182 if (!buf)
2183 return NULL;
2184 gsm0808_cell_id_list_name_buf(buf, 1024, cil);
2185 return buf;
2186}
2187
Neels Hofmeyra4399c82018-04-17 02:26:10 +02002188#undef APPEND_STR
2189#undef APPEND_CELL_ID_U
2190
Harald Welte4a62eda2019-03-18 18:27:00 +01002191char *gsm0808_channel_type_name_buf(char *buf, size_t buf_len, const struct gsm0808_channel_type *ct)
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02002192{
Harald Welte4a62eda2019-03-18 18:27:00 +01002193 snprintf(buf, buf_len, "ch_indctr=0x%x ch_rate_type=0x%x perm_spch=%s",
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02002194 ct->ch_indctr, ct->ch_rate_type,
2195 osmo_hexdump(ct->perm_spch, ct->perm_spch_len));
2196 return buf;
2197}
2198
Harald Welte4a62eda2019-03-18 18:27:00 +01002199const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct)
2200{
Harald Welte171ef822019-03-28 10:49:05 +01002201 static __thread char buf[128];
Harald Welte4a62eda2019-03-18 18:27:00 +01002202 return gsm0808_channel_type_name_buf(buf, sizeof(buf), ct);
2203}
2204
Harald Welte179f3572019-03-18 18:38:47 +01002205char *gsm0808_channel_type_name_c(const void *ctx, const struct gsm0808_channel_type *ct)
2206{
2207 char *buf = talloc_size(ctx, 128);
2208 if (!buf)
2209 return NULL;
2210 return gsm0808_channel_type_name_buf(buf, 128, ct);
2211}
2212
Andreas Eversberg5c7336c2023-04-20 11:04:14 +02002213/*! Encode Group Call Reference IE (3GPP TS 48.008 3.2.2.55).
2214 * \param[out] msg Message Buffer to which IE is to be appended
2215 * \param[in] gc Group Call Reference to be encoded
2216 * \returns number of bytes appended to \a msg */
2217uint8_t gsm0808_enc_group_callref(struct msgb *msg, const struct gsm0808_group_callref *gc)
2218{
2219 uint8_t *old_tail;
2220 uint8_t *tlv_len;
2221
2222 OSMO_ASSERT(msg);
2223 OSMO_ASSERT(gc);
2224
2225 msgb_put_u8(msg, GSM0808_IE_GROUP_CALL_REFERENCE);
2226 tlv_len = msgb_put(msg, 1);
2227 old_tail = msg->tail;
2228
2229 memcpy(msgb_put(msg, sizeof(*gc)), gc, sizeof(*gc));
2230
2231 *tlv_len = (uint8_t) (msg->tail - old_tail);
2232 return *tlv_len + 2;
2233}
2234
2235/*! Decode Group Call Reference IE (3GPP TS 48.008 3.2.2.55).
2236 * \param[out] gc Group Call Reference structure to store data
2237 * \param[in] elem IE value to be decoded.
2238 * \param[in] len Length of \a elem in bytes.
2239 * \returns number of bytes parsed; negative on error */
2240int gsm0808_dec_group_callref(struct gsm0808_group_callref *gc, const uint8_t *elem, uint8_t len)
2241{
2242 OSMO_ASSERT(gc);
2243 OSMO_ASSERT(elem);
2244
2245 if (len != sizeof(*gc))
2246 return -EINVAL;
2247
2248 memcpy(gc, elem, sizeof(*gc));
2249
2250 return len;
2251}
2252
2253/*! Encode Priority IE (3GPP TS 48.008 3.2.2.18).
2254 * \param[out] msg Message Buffer to which IE is to be appended
2255 * \param[in] pri Priority to be encoded
2256 * \returns number of bytes appended to \a msg */
2257uint8_t gsm0808_enc_priority(struct msgb *msg, const struct gsm0808_priority *pri)
2258{
2259 uint8_t *old_tail;
2260 uint8_t *tlv_len;
2261
2262 OSMO_ASSERT(msg);
2263 OSMO_ASSERT(pri);
2264
2265 msgb_put_u8(msg, GSM0808_IE_PRIORITY);
2266 tlv_len = msgb_put(msg, 1);
2267 old_tail = msg->tail;
2268
2269 memcpy(msgb_put(msg, sizeof(*pri)), pri, sizeof(*pri));
2270
2271 *tlv_len = (uint8_t) (msg->tail - old_tail);
2272 return *tlv_len + 2;
2273}
2274
2275/*! Decode Priority IE (3GPP TS 48.008 3.2.2.18).
2276 * \param[out] pri Priority structure to store data
2277 * \param[in] elem IE value to be decoded.
2278 * \param[in] len Length of \a elem in bytes.
2279 * \returns number of bytes parsed; negative on error */
2280int gsm0808_dec_priority(struct gsm0808_priority *pri, const uint8_t *elem, uint8_t len)
2281{
2282 OSMO_ASSERT(pri);
2283 OSMO_ASSERT(elem);
2284
2285 if (len != sizeof(*pri))
2286 return -EINVAL;
2287
2288 memcpy(pri, elem, sizeof(*pri));
2289
2290 return len;
2291}
2292
2293/*! Encode VGCS Feature Flags IE (3GPP TS 48.008 3.2.2.88).
2294 * \param[out] msg Message Buffer to which IE is to be appended
2295 * \param[in] ff VGCS Feature Flags to be encoded
2296 * \returns number of bytes appended to \a msg */
2297uint8_t gsm0808_enc_vgcs_feature_flags(struct msgb *msg, const struct gsm0808_vgcs_feature_flags *ff)
2298{
2299 uint8_t *old_tail;
2300 uint8_t *tlv_len;
2301
2302 OSMO_ASSERT(msg);
2303 OSMO_ASSERT(ff);
2304
2305 msgb_put_u8(msg, GSM0808_IE_VGCS_FEATURE_FLAGS);
2306 tlv_len = msgb_put(msg, 1);
2307 old_tail = msg->tail;
2308
2309 memcpy(msgb_put(msg, sizeof(*ff)), ff, sizeof(*ff));
2310
2311 *tlv_len = (uint8_t) (msg->tail - old_tail);
2312 return *tlv_len + 2;
2313}
2314
2315/*! Decode VGCS Feature Flags IE (3GPP TS 48.008 3.2.2.88).
2316 * \param[out] ff VGCS Feature Flags structure to store data
2317 * \param[in] elem IE value to be decoded.
2318 * \param[in] len Length of \a elem in bytes.
2319 * \returns number of bytes parsed; negative on error */
2320int gsm0808_dec_vgcs_feature_flags(struct gsm0808_vgcs_feature_flags *ff, const uint8_t *elem, uint8_t len)
2321{
2322 OSMO_ASSERT(ff);
2323 OSMO_ASSERT(elem);
2324
2325 if (len != sizeof(*ff))
2326 return -EINVAL;
2327
2328 memcpy(ff, elem, sizeof(*ff));
2329
2330 return len;
2331}
2332
2333/*! Encode Data Identity IE (3GPP TS 48.008 3.2.2.99).
2334 * \param[out] msg Message Buffer to which IE is to be appended
2335 * \param[in] di Data Identity to be encoded
2336 * \returns number of bytes appended to \a msg */
2337uint8_t gsm0808_enc_data_identity(struct msgb *msg, const struct gsm0808_data_identity *ti)
2338{
2339 uint8_t *old_tail;
2340 uint8_t *tlv_len;
2341
2342 OSMO_ASSERT(msg);
2343 OSMO_ASSERT(ti);
2344
2345 msgb_put_u8(msg, GSM0808_IE_DATA_IDENTITY);
2346 tlv_len = msgb_put(msg, 1);
2347 old_tail = msg->tail;
2348
2349 memcpy(msgb_put(msg, sizeof(*ti)), ti, sizeof(*ti));
2350
2351 *tlv_len = (uint8_t) (msg->tail - old_tail);
2352 return *tlv_len + 2;
2353}
2354
2355/*! Decode Data Identity IE (3GPP TS 48.008 3.2.2.99).
2356 * \param[out] di Data Identity structure to store data
2357 * \param[in] elem IE value to be decoded.
2358 * \param[in] len Length of \a elem in bytes.
2359 * \returns number of bytes parsed; negative on error */
2360int gsm0808_dec_data_identity(struct gsm0808_data_identity *ti, const uint8_t *elem, uint8_t len)
2361{
2362 OSMO_ASSERT(ti);
2363 OSMO_ASSERT(elem);
2364
2365 if (len < sizeof(*ti))
2366 return -EINVAL;
2367
2368 memcpy(ti, elem, sizeof(*ti));
2369
2370 return len;
2371}
2372
2373/*! Encode MSISDN IE (3GPP TS 48.008 3.2.2.101).
2374 * \param[out] msg Message Buffer to which IE is to be appended
2375 * \param[in] msisdn MSISDN to be encoded
2376 * \returns number of bytes appended to \a msg */
2377uint8_t gsm0808_enc_msisdn(struct msgb *msg, const char *msisdn)
2378{
2379 uint8_t *tlv_len;
2380 uint8_t bcd_lv[11];
2381 int rc;
2382
2383 OSMO_ASSERT(msg);
2384 OSMO_ASSERT(msisdn);
2385
2386 rc = gsm48_encode_bcd_number(bcd_lv, sizeof(bcd_lv), 0, msisdn);
2387 if (rc < 1)
2388 return 0;
2389
2390 msgb_put_u8(msg, GSM0808_IE_MSISDN);
2391 tlv_len = msgb_put(msg, rc);
2392
2393 memcpy(tlv_len, bcd_lv, rc);
2394
2395 *tlv_len = rc - 1;
2396 return *tlv_len + 2;
2397}
2398
2399/*! Decode MSISDN IE (3GPP TS 48.008 3.2.2.101).
2400 * \param[out] msisdn MSISDN structure to store data
2401 * \param[in] elem IE value to be decoded.
2402 * \param[in] len Length of \a elem in bytes.
2403 * \returns number of bytes parsed; negative on error */
2404int gsm0808_dec_msisdn(char *msisdn, const char *elem, uint8_t len)
2405{
2406 OSMO_ASSERT(msisdn);
2407 OSMO_ASSERT(elem);
2408
2409 if (len < 1)
2410 return -EINVAL;
2411
2412 gsm48_decode_bcd_number(msisdn, MSISDN_MAXLEN + 1, (uint8_t *)(elem - 1), 0);
2413
2414 return len;
2415}
2416
2417/*! Encode Talker Identity IE (3GPP TS 48.008 3.2.2.91).
2418 * \param[out] msg Message Buffer to which IE is to be appended
2419 * \param[in] ti Talker Identity to be encoded
2420 * \returns number of bytes appended to \a msg */
2421uint8_t gsm0808_enc_talker_identity(struct msgb *msg, const struct gsm0808_talker_identity *ti)
2422{
2423 uint8_t *old_tail;
2424 uint8_t *tlv_len;
2425 uint8_t *ptr;
2426 unsigned int bytes;
2427
2428 OSMO_ASSERT(msg);
2429 OSMO_ASSERT(ti);
2430
2431 msgb_put_u8(msg, GSM0808_IE_TALKER_IDENTITY);
2432 tlv_len = msgb_put(msg, 1);
2433 old_tail = msg->tail;
2434
2435 bytes = (ti->id_bits + 7) >> 3;
2436 ptr = msgb_put(msg, 1 + bytes);
2437 ptr[0] = -ti->id_bits & 0x7;
2438 memcpy(ptr + 1, ti->talker_id, bytes);
2439
2440 *tlv_len = (uint8_t) (msg->tail - old_tail);
2441 return *tlv_len + 2;
2442}
2443
2444/*! Decode Talker Identity IE (3GPP TS 48.008 3.2.2.91).
2445 * \param[out] ti Talker Identity structure to store data
2446 * \param[in] elem IE value to be decoded.
2447 * \param[in] len Length of \a elem in bytes.
2448 * \returns number of bytes parsed; negative on error */
2449int gsm0808_dec_talker_identity(struct gsm0808_talker_identity *ti, const uint8_t *elem, uint8_t len)
2450{
2451 OSMO_ASSERT(ti);
2452 OSMO_ASSERT(elem);
2453
2454 if (len < 2)
2455 return -EINVAL;
2456 unsigned int bytes;
2457
2458 bytes = len - 1;
2459 if (bytes > TALKER_IDENTITY_MAXLEN)
2460 return -EINVAL;
2461 ti->id_bits = (bytes << 3) - (elem[0] & 0x7);
2462 memcpy(ti->talker_id, elem + 1, bytes);
2463
2464 return len;
2465}
2466
2467/*! Encode Assignment Requirements IE (3GPP TS 48.008 3.2.2.52).
2468 * \param[out] msg Message Buffer to which IE is to be appended
2469 * \param[in] ar Assignment Requirement to be encoded
2470 * \returns number of bytes appended to \a msg */
2471uint8_t gsm0808_enc_assign_req(struct msgb *msg, const enum gsm0808_assignment_requirement ar)
2472{
2473 uint8_t *ptr;
2474
2475 OSMO_ASSERT(msg);
2476
2477 msgb_put_u8(msg, GSM0808_IE_ASSIGNMENT_REQUIREMENT);
2478
2479 ptr = msgb_put(msg, 1);
2480 ptr[0] = ar;
2481
2482 return 2;
2483}
2484
2485/*! Decode Assignment Requirements IE (3GPP TS 48.008 3.2.2.52).
2486 * \param[out] ar Assignment Requirements enum to store data
2487 * \param[in] elem IE value to be decoded.
2488 * \param[in] len Length of \a elem in bytes.
2489 * \returns number of bytes parsed; negative on error */
2490int gsm0808_dec_assign_req(enum gsm0808_assignment_requirement *ar, const uint8_t *elem, uint8_t len)
2491{
2492 OSMO_ASSERT(ar);
2493 OSMO_ASSERT(elem);
2494
2495 if (len != 1)
2496 return -EINVAL;
2497
2498 *ar = elem[0];
2499
2500 return 1;
2501}
2502
2503/*! Encode Cell Identifier List Segment IE (3GPP TS 48.008 3.2.2.27a).
2504 * \param[out] msg Message Buffer to which IE is to be appended
2505 * \param[in] ie_type Type of IE to use (5 different lists are specified.)
2506 * \param[in] ci Cell Identifier List Segment to be encoded
2507 * \returns number of bytes appended to \a msg */
2508uint8_t gsm0808_enc_cell_id_list_segment(struct msgb *msg, uint8_t ie_type,
2509 const struct gsm0808_cell_id_list_segment *ci)
2510{
2511 uint8_t *old_tail;
2512 uint8_t *tlv_len;
2513 int rc;
2514
2515 OSMO_ASSERT(msg);
2516 OSMO_ASSERT(ci);
2517
2518 msgb_put_u8(msg, ie_type);
2519 tlv_len = msgb_put(msg, 1);
2520 old_tail = msg->tail;
2521
2522 msgb_put_u8(msg, (ci->seq_last << 4) | ci->seq_number);
2523
2524 rc = gsm0808_enc_cell_id_list2(msg, &ci->cil);
2525 if (rc <= 0)
2526 return rc;
2527
2528 *tlv_len = (uint8_t) (msg->tail - old_tail);
2529 return *tlv_len + 2;
2530}
2531
2532/*! Decode Cell Identifier List Segment IE (3GPP TS 48.008 3.2.2.27a).
2533 * \param[out] ci Cell Identifier List Segment structure to store data
2534 * \param[in] elem IE value to be decoded.
2535 * \param[in] len Length of \a elem in bytes.
2536 * \returns number of bytes parsed; negative on error */
2537int gsm0808_dec_cell_id_list_segment(struct gsm0808_cell_id_list_segment *ci, const uint8_t *elem, uint8_t len)
2538{
2539 int rc;
2540
2541 OSMO_ASSERT(ci);
2542 OSMO_ASSERT(elem);
2543
2544 if (len < 1)
2545 return -EINVAL;
2546
2547 ci->seq_last = elem[0] >> 4;
2548 ci->seq_number = elem[0] & 0x0f;
2549
2550 rc = gsm0808_dec_cell_id_list2(&ci->cil, elem + 1, len - 1);
2551 if (rc < 0)
2552 return rc;
2553
2554 return len;
2555}
2556
2557/*! Decode Call Identifier IE (3GPP TS 48.008 3.2.2.105).
2558 * \param[out] ci Call Identifier structure to store data
2559 * \param[in] elem IE value to be decoded.
2560 * \param[in] len Length of \a elem in bytes.
2561 * \returns number of bytes parsed; negative on error */
2562int gsm0808_dec_call_id(uint32_t *ci, const uint8_t *elem, uint8_t len)
2563{
2564 OSMO_ASSERT(ci);
2565 OSMO_ASSERT(elem);
2566
2567 if (len != 4)
2568 return -EINVAL;
2569
2570 /* The Call Identifier is stored as little endian. */
2571 *ci = osmo_load32le(elem);
2572
2573 return 4;
2574}
2575
Harald Welte96e2a002017-06-12 21:44:18 +02002576/*! @} */