blob: 70eed15ed1ad0f48f83bdae96cc47e5a380a2e69 [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
81 * \returns number of bytes added to \a msg */
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;
90 uint8_t *old_tail;
91 uint8_t *tlv_len;
92
93 OSMO_ASSERT(msg);
94 OSMO_ASSERT(ss);
95 OSMO_ASSERT(ss->ss_family == AF_INET || ss->ss_family == AF_INET6);
96
97 msgb_put_u8(msg, GSM0808_IE_AOIP_TRASP_ADDR);
98 tlv_len = msgb_put(msg,1);
99 old_tail = msg->tail;
100
101 switch (ss->ss_family) {
102 case AF_INET:
103 sin = (struct sockaddr_in *)ss;
Harald Welte95871da2017-05-15 12:11:36 +0200104 port = osmo_ntohs(sin->sin_port);
Philipp Maier22401432017-03-24 17:59:26 +0100105 ptr = msgb_put(msg, IP_V4_ADDR_LEN);
106 memcpy(ptr, &sin->sin_addr.s_addr, IP_V4_ADDR_LEN);
107 break;
108 case AF_INET6:
109 sin6 = (struct sockaddr_in6 *)ss;
Harald Welte95871da2017-05-15 12:11:36 +0200110 port = osmo_ntohs(sin6->sin6_port);
Philipp Maier22401432017-03-24 17:59:26 +0100111 ptr = msgb_put(msg, IP_V6_ADDR_LEN);
112 memcpy(ptr, sin6->sin6_addr.s6_addr, IP_V6_ADDR_LEN);
113 break;
114 }
115
116 msgb_put_u16(msg, port);
117
118 *tlv_len = (uint8_t) (msg->tail - old_tail);
119 return *tlv_len + 2;
120}
121
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200122/*! Decode TS 08.08 AoIP transport address IE
Harald Welte96e2a002017-06-12 21:44:18 +0200123 * \param[out] ss Caller-provided memory where decoded socket addr is stored
124 * \param[in] elem pointer to IE value
125 * \param[in] len length of \a elem in bytes
126 * \returns number of bytes parsed */
Philipp Maier22401432017-03-24 17:59:26 +0100127int gsm0808_dec_aoip_trasp_addr(struct sockaddr_storage *ss,
128 const uint8_t *elem, uint8_t len)
129{
130 /* See also 3GPP TS 48.008 3.2.2.102 AoIP Transport Layer Address */
131 struct sockaddr_in sin;
132 struct sockaddr_in6 sin6;
133 const uint8_t *old_elem = elem;
134
135 OSMO_ASSERT(ss);
136 if (!elem)
137 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200138 if (len == 0)
Philipp Maier22401432017-03-24 17:59:26 +0100139 return -EINVAL;
140
141 memset(ss, 0, sizeof(*ss));
142
143 switch (len) {
144 case IP_V4_ADDR_LEN + IP_PORT_LEN:
145 memset(&sin, 0, sizeof(sin));
146 sin.sin_family = AF_INET;
147
148 memcpy(&sin.sin_addr.s_addr, elem, IP_V4_ADDR_LEN);
149 elem += IP_V4_ADDR_LEN;
150 sin.sin_port = osmo_load16le(elem);
151 elem += IP_PORT_LEN;
152
153 memcpy(ss, &sin, sizeof(sin));
154 break;
155 case IP_V6_ADDR_LEN + IP_PORT_LEN:
156 memset(&sin6, 0, sizeof(sin6));
157 sin6.sin6_family = AF_INET6;
158
159 memcpy(sin6.sin6_addr.s6_addr, elem, IP_V6_ADDR_LEN);
160 elem += IP_V6_ADDR_LEN;
161 sin6.sin6_port = osmo_load16le(elem);
162 elem += IP_PORT_LEN;
163
164 memcpy(ss, &sin6, sizeof(sin6));
165 break;
166 default:
167 /* Malformed element! */
168 return -EINVAL;
169 break;
170 }
171
172 return (int)(elem - old_elem);
173}
Philipp Maier6f725d62017-03-24 18:03:17 +0100174
Pau Espin Pedrol18506c82019-04-16 15:47:59 +0200175/*! Decode TS 08.08 (Osmocom Extension) Osmux CID
176 * TV with len(V) == 1, and V is the CID to be used.
177 * \param[out] cid Caller-provided variable where CID is stored
178 * \param[in] elem pointer to IE value
179 * \param[in] len length of \a elem in bytes
180 * \returns number of bytes parsed */
181int gsm0808_dec_osmux_cid(uint8_t *cid, const uint8_t *elem, uint8_t len)
182{
183 OSMO_ASSERT(cid);
184 if (!elem)
185 return -EINVAL;
186 if (len != 1)
187 return -EINVAL;
188
189 *cid = *elem;
190
191 return 1;
192}
193
Harald Welte20725b92017-05-15 12:50:04 +0200194#endif /* HAVE_SYS_SOCKET_H */
195
Philipp Maier6f725d62017-03-24 18:03:17 +0100196/* Helper function for gsm0808_enc_speech_codec()
197 * and gsm0808_enc_speech_codec_list() */
198static uint8_t enc_speech_codec(struct msgb *msg,
199 const struct gsm0808_speech_codec *sc)
200{
201 /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
202 uint8_t header = 0;
203 uint8_t *old_tail;
Harald Welte459a1802018-06-28 09:24:17 +0200204 bool type_extended = false;
Philipp Maierbb839662017-06-01 17:11:19 +0200205
206 /* Note: Extended codec types are codec types that require 8 instead
207 * of 4 bit to fully specify the selected codec. In the following,
208 * we check if we work with an extended type or not. We also check
209 * if the codec type is valid at all. */
210 switch(sc->type) {
211 case GSM0808_SCT_FR1:
212 case GSM0808_SCT_FR2:
213 case GSM0808_SCT_FR3:
214 case GSM0808_SCT_FR4:
215 case GSM0808_SCT_FR5:
216 case GSM0808_SCT_HR1:
217 case GSM0808_SCT_HR3:
218 case GSM0808_SCT_HR4:
219 case GSM0808_SCT_HR6:
220 type_extended = false;
221 break;
222 case GSM0808_SCT_CSD:
223 type_extended = true;
224 break;
225 default:
226 /* Invalid codec type specified */
227 OSMO_ASSERT(false);
228 break;
229 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100230
231 old_tail = msg->tail;
232
233 if (sc->fi)
234 header |= (1 << 7);
235 if (sc->pi)
236 header |= (1 << 6);
237 if (sc->pt)
238 header |= (1 << 5);
239 if (sc->tf)
240 header |= (1 << 4);
Philipp Maierbb839662017-06-01 17:11:19 +0200241
242 if (type_extended) {
Philipp Maier6f725d62017-03-24 18:03:17 +0100243 header |= 0x0f;
244 msgb_put_u8(msg, header);
Philipp Maierbb839662017-06-01 17:11:19 +0200245 msgb_put_u8(msg, sc->type);
Philipp Maier6f725d62017-03-24 18:03:17 +0100246 } else {
247 OSMO_ASSERT(sc->type < 0x0f);
248 header |= sc->type;
249 msgb_put_u8(msg, header);
Philipp Maier6f725d62017-03-24 18:03:17 +0100250 }
251
Philipp Maierbb839662017-06-01 17:11:19 +0200252 /* Note: Whether a configuration is present or not depends on the
253 * selected codec type. If present, it can either consist of one
254 * or two octets, depending on the codec type */
255 switch (sc->type) {
256 case GSM0808_SCT_FR3:
257 case GSM0808_SCT_HR3:
258 case GSM0808_SCT_HR6:
Philipp Maier7e27b142018-03-22 17:26:46 +0100259 msgb_put_u16(msg, osmo_ntohs(sc->cfg));
Philipp Maierbb839662017-06-01 17:11:19 +0200260 break;
261 case GSM0808_SCT_FR4:
262 case GSM0808_SCT_FR5:
263 case GSM0808_SCT_HR4:
264 case GSM0808_SCT_CSD:
265 OSMO_ASSERT((sc->cfg & 0xff00) == 0)
266 msgb_put_u8(msg, (uint8_t) sc->cfg & 0xff);
267 break;
268 default:
269 OSMO_ASSERT(sc->cfg == 0);
270 break;
271 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100272
273 return (uint8_t) (msg->tail - old_tail);
274}
275
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200276/*! Encode TS 08.08 Speech Codec IE
Harald Welte96e2a002017-06-12 21:44:18 +0200277 * \param[out] msg Message Buffer to which IE will be appended
278 * \param[in] sc Speech Codec to be encoded into IE
279 * \returns number of bytes appended to \a msg */
Philipp Maier6f725d62017-03-24 18:03:17 +0100280uint8_t gsm0808_enc_speech_codec(struct msgb *msg,
281 const struct gsm0808_speech_codec *sc)
282{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200283 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100284 uint8_t *old_tail;
285 uint8_t *tlv_len;
286
287 OSMO_ASSERT(msg);
288 OSMO_ASSERT(sc);
289
290 msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC);
291 tlv_len = msgb_put(msg, 1);
292 old_tail = msg->tail;
293
294 enc_speech_codec(msg, sc);
295
296 *tlv_len = (uint8_t) (msg->tail - old_tail);
297 return *tlv_len + 2;
298}
299
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200300/*! Decode TS 08.08 Speech Codec IE
Harald Welte96e2a002017-06-12 21:44:18 +0200301 * \param[out] sc Caller-allocated memory for Speech Codec
302 * \param[in] elem IE value to be decoded
303 * \param[in] len Length of \a elem in bytes
304 * \returns number of bytes parsed; negative on error */
Philipp Maier6f725d62017-03-24 18:03:17 +0100305int gsm0808_dec_speech_codec(struct gsm0808_speech_codec *sc,
306 const uint8_t *elem, uint8_t len)
307{
308 /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
309 uint8_t header;
310 const uint8_t *old_elem = elem;
311
312 OSMO_ASSERT(sc);
313 if (!elem)
314 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200315 if (len == 0)
Philipp Maier6f725d62017-03-24 18:03:17 +0100316 return -EINVAL;
317
318 memset(sc, 0, sizeof(*sc));
319
320 header = *elem;
321
Philipp Maier85a6af22017-04-28 10:55:05 +0200322 /* An extended codec type needs at least two fields,
323 * bail if the input data length is not sufficient. */
Philipp Maier6f725d62017-03-24 18:03:17 +0100324 if ((header & 0x0F) == 0x0F && len < 2)
325 return -EINVAL;
Philipp Maier6f725d62017-03-24 18:03:17 +0100326
327 elem++;
328 len--;
329
330 if (header & (1 << 7))
331 sc->fi = true;
332 if (header & (1 << 6))
333 sc->pi = true;
334 if (header & (1 << 5))
335 sc->pt = true;
336 if (header & (1 << 4))
337 sc->tf = true;
338
339 if ((header & 0x0F) != 0x0F) {
340 sc->type = (header & 0x0F);
Philipp Maierbb839662017-06-01 17:11:19 +0200341 } else {
342 sc->type = *elem;
343 elem++;
344 len--;
Philipp Maier6f725d62017-03-24 18:03:17 +0100345 }
346
Philipp Maierbb839662017-06-01 17:11:19 +0200347 /* Note: Whether a configuration is present or not depends on the
348 * selected codec type. If present, it can either consist of one or
349 * two octets depending on the codec type */
350 switch (sc->type) {
351 case GSM0808_SCT_FR1:
352 case GSM0808_SCT_FR2:
353 case GSM0808_SCT_HR1:
354 break;
355 case GSM0808_SCT_HR4:
356 case GSM0808_SCT_CSD:
357 case GSM0808_SCT_FR4:
358 case GSM0808_SCT_FR5:
359 if (len < 1)
360 return -EINVAL;
361 sc->cfg = *elem;
362 elem++;
363 break;
364 case GSM0808_SCT_FR3:
365 case GSM0808_SCT_HR3:
366 case GSM0808_SCT_HR6:
367 if (len < 2)
368 return -EINVAL;
Philipp Maier7e27b142018-03-22 17:26:46 +0100369 sc->cfg = osmo_load16le(elem);
Philipp Maierbb839662017-06-01 17:11:19 +0200370 elem += 2;
371 break;
372 default:
373 /* Invalid codec type => malformed speech codec element! */
374 return -EINVAL;
375 break;
376 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100377
378 return (int)(elem - old_elem);
379}
380
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200381/*! Encode TS 08.08 Speech Codec list
Harald Welte96e2a002017-06-12 21:44:18 +0200382 * \param[out] msg Message Buffer to which IE is to be appended
383 * \param[in] scl Speech Codec List to be encoded into IE
384 * \returns number of bytes added to \a msg */
Philipp Maier6f725d62017-03-24 18:03:17 +0100385uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg,
386 const struct gsm0808_speech_codec_list *scl)
387{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200388 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100389 uint8_t *old_tail;
390 uint8_t *tlv_len;
391 unsigned int i;
392 uint8_t rc;
393 unsigned int bytes_used = 0;
394
395 OSMO_ASSERT(msg);
396 OSMO_ASSERT(scl);
397
Philipp Maier6f725d62017-03-24 18:03:17 +0100398 msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC_LIST);
399 tlv_len = msgb_put(msg, 1);
400 old_tail = msg->tail;
401
402 for (i = 0; i < scl->len; i++) {
403 rc = enc_speech_codec(msg, &scl->codec[i]);
404 OSMO_ASSERT(rc >= 1);
405 bytes_used += rc;
406 OSMO_ASSERT(bytes_used <= 255);
407 }
408
409 *tlv_len = (uint8_t) (msg->tail - old_tail);
410 return *tlv_len + 2;
411}
412
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200413/*! Decode TS 08.08 Speech Codec list IE
Harald Welte96e2a002017-06-12 21:44:18 +0200414 * \param[out] scl Caller-provided memory to store codec list
415 * \param[in] elem IE value to be decoded
416 * \param[in] len Length of \a elem in bytes
417 * \returns number of bytes parsed; negative on error */
Philipp Maier6f725d62017-03-24 18:03:17 +0100418int gsm0808_dec_speech_codec_list(struct gsm0808_speech_codec_list *scl,
419 const uint8_t *elem, uint8_t len)
420{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200421 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100422 const uint8_t *old_elem = elem;
423 unsigned int i;
424 int rc;
425 uint8_t decoded = 0;
426
427 OSMO_ASSERT(scl);
428 if (!elem)
429 return -EINVAL;
Philipp Maier6f725d62017-03-24 18:03:17 +0100430
431 memset(scl, 0, sizeof(*scl));
432
433 for (i = 0; i < ARRAY_SIZE(scl->codec); i++) {
434 if (len <= 0)
435 break;
436
437 rc = gsm0808_dec_speech_codec(&scl->codec[i], elem, len);
438 if (rc < 1)
439 return -EINVAL;
440
441 elem+=rc;
442 len -= rc;
443 decoded++;
444 }
445
446 scl->len = decoded;
447
Philipp Maier6f725d62017-03-24 18:03:17 +0100448 return (int)(elem - old_elem);
449}
Philipp Maiere0c65302017-03-28 17:05:40 +0200450
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200451/*! Encode TS 08.08 Channel Type IE
Harald Welte96e2a002017-06-12 21:44:18 +0200452 * \param[out] msg Message Buffer to which IE is to be appended
453 * \param[in] ct Channel Type to be encoded
454 * \returns number of bytes added to \a msg */
Philipp Maiere0c65302017-03-28 17:05:40 +0200455uint8_t gsm0808_enc_channel_type(struct msgb *msg,
456 const struct gsm0808_channel_type *ct)
457{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200458 /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */
Philipp Maiere0c65302017-03-28 17:05:40 +0200459 unsigned int i;
460 uint8_t byte;
461 uint8_t *old_tail;
462 uint8_t *tlv_len;
463
464 OSMO_ASSERT(msg);
465 OSMO_ASSERT(ct);
466 OSMO_ASSERT(ct->perm_spch_len <= CHANNEL_TYPE_ELEMENT_MAXLEN - 2);
467
468 /* FIXME: Implement encoding support for Data
469 * and Speech + CTM Text Telephony */
470 if ((ct->ch_indctr & 0x0f) != GSM0808_CHAN_SPEECH
471 && (ct->ch_indctr & 0x0f) != GSM0808_CHAN_SIGN)
472 OSMO_ASSERT(false);
473
474 msgb_put_u8(msg, GSM0808_IE_CHANNEL_TYPE);
475 tlv_len = msgb_put(msg, 1);
476 old_tail = msg->tail;
477
478 msgb_put_u8(msg, ct->ch_indctr & 0x0f);
479 msgb_put_u8(msg, ct->ch_rate_type);
480
481 for (i = 0; i < ct->perm_spch_len; i++) {
482 byte = ct->perm_spch[i];
483
484 if (i < ct->perm_spch_len - 1)
485 byte |= 0x80;
486 msgb_put_u8(msg, byte);
487 }
488
489 *tlv_len = (uint8_t) (msg->tail - old_tail);
490 return *tlv_len + 2;
491}
492
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200493/*! Decode TS 08.08 Channel Type IE
Harald Welte96e2a002017-06-12 21:44:18 +0200494 * \param[out] ct Caller-provided memory to store channel type
495 * \param[in] elem IE Value to be decoded
496 * \param[in] len Length of \a elem in bytes
497 * \returns number of bytes parsed; negative on error */
Philipp Maiere0c65302017-03-28 17:05:40 +0200498int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct,
499 const uint8_t *elem, uint8_t len)
500{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200501 /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */
Philipp Maiere0c65302017-03-28 17:05:40 +0200502 unsigned int i;
503 uint8_t byte;
504 const uint8_t *old_elem = elem;
505
506 OSMO_ASSERT(ct);
507 if (!elem)
508 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200509 if (len < 3 || len > 11)
Philipp Maiere0c65302017-03-28 17:05:40 +0200510 return -EINVAL;
511
512 memset(ct, 0, sizeof(*ct));
513
514 ct->ch_indctr = (*elem) & 0x0f;
515 elem++;
516 ct->ch_rate_type = (*elem) & 0x0f;
517 elem++;
518
519 for (i = 0; i < ARRAY_SIZE(ct->perm_spch); i++) {
520 byte = *elem;
521 elem++;
522 ct->perm_spch[i] = byte & 0x7f;
523 if ((byte & 0x80) == 0x00)
524 break;
525 }
526 ct->perm_spch_len = i + 1;
527
528 return (int)(elem - old_elem);
529}
Philipp Maier14e76b92017-03-28 18:36:52 +0200530
Max969fb2e2018-12-10 11:01:10 +0100531/*! Create BSSMAP Global Call Reference, 3GPP TS 48.008 §3.2.2.115.
532 * \param[out] msg Message Buffer for appending IE
533 * \param[in] g Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1
534 * \returns number of bytes added to \a msg or 0 on error */
Max47022152018-12-19 18:51:00 +0100535static uint8_t gsm0808_enc_gcr(struct msgb *msg, const struct osmo_gcr_parsed *g)
Max969fb2e2018-12-10 11:01:10 +0100536{
537 uint8_t enc, *len = msgb_tl_put(msg, GSM0808_IE_GLOBAL_CALL_REF);
538
539 enc = osmo_enc_gcr(msg, g);
540 if (!enc)
541 return 0;
542
543 *len = enc;
544 return enc + 2; /* type (1 byte) + length (1 byte) */
545}
546
547/*! Decode BSSMAP Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1.
548 * \param[out] gcr Caller-provided memory to store Global Call Reference
Max036012b2018-12-19 17:48:56 +0100549 * \param[in] tp IE values to be decoded
Max969fb2e2018-12-10 11:01:10 +0100550 * \returns number of bytes parsed; negative on error */
Max47022152018-12-19 18:51:00 +0100551static int gsm0808_dec_gcr(struct osmo_gcr_parsed *gcr, const struct tlv_parsed *tp)
Max969fb2e2018-12-10 11:01:10 +0100552{
553 int ret;
554 const uint8_t *buf = TLVP_VAL_MINLEN(tp, GSM0808_IE_GLOBAL_CALL_REF, OSMO_GCR_MIN_LEN);
555 if (!buf)
556 return -EINVAL;
557
558 ret = osmo_dec_gcr(gcr, buf, TLVP_LEN(tp, GSM0808_IE_GLOBAL_CALL_REF));
559 if (ret < 0)
560 return -ENOENT;
561
562 return 2 + ret;
563}
564
Max47022152018-12-19 18:51:00 +0100565/*! Add LCLS parameters to a given msgb, 3GPP TS 48.008 §3.2.2.115 - 3.2.2.120.
566 * \param[out] msg Message Buffer for appending IE
567 * \param[in] lcls LCLS-related data
568 * \returns number of bytes added to \a msg or 0 on error */
569uint8_t gsm0808_enc_lcls(struct msgb *msg, const struct osmo_lcls *lcls)
570{
571 uint8_t enc = 0;
572
573 /* LCLS: §3.2.2.115 Global Call Reference */
Max3b901252019-01-15 14:15:11 +0100574 if (lcls->gcr_available)
575 enc = gsm0808_enc_gcr(msg, &lcls->gcr);
Max47022152018-12-19 18:51:00 +0100576
577 /* LCLS: §3.2.2.116 Configuration */
578 if (lcls->config != GSM0808_LCLS_CFG_NA) {
579 msgb_tv_put(msg, GSM0808_IE_LCLS_CONFIG, lcls->config);
580 enc += 2;
581 }
582
583 /* LCLS: §3.2.2.117 Connection Status Control */
584 if (lcls->control != GSM0808_LCLS_CSC_NA) {
585 msgb_tv_put(msg, GSM0808_IE_LCLS_CONN_STATUS_CTRL, lcls->control);
586 enc += 2;
587 }
588
589 /* LCLS: §3.2.2.118 Correlation-Not-Needed */
590 if (!lcls->corr_needed) {
591 msgb_v_put(msg, GSM0808_IE_LCLS_CORR_NOT_NEEDED);
592 enc++;
593 }
594
595 return enc;
596}
597
598/*! Decode LCLS parameters to a given msgb, 3GPP TS 48.008 §3.2.2.115 - 3.2.2.120.
599 * \param[out] lcls Caller-provided memory to store LCLS-related data
600 * \param[in] tp IE values to be decoded
601 * \returns GCR size or negative on error */
602int gsm0808_dec_lcls(struct osmo_lcls *lcls, const struct tlv_parsed *tp)
603{
Max3b901252019-01-15 14:15:11 +0100604 int ret = gsm0808_dec_gcr(&lcls->gcr, tp);
Max47022152018-12-19 18:51:00 +0100605
Max3b901252019-01-15 14:15:11 +0100606 lcls->gcr_available = (ret < 0) ? false : true;
Max47022152018-12-19 18:51:00 +0100607 lcls->config = tlvp_val8(tp, GSM0808_IE_LCLS_CONFIG, GSM0808_LCLS_CFG_NA);
608 lcls->control = tlvp_val8(tp, GSM0808_IE_LCLS_CONN_STATUS_CTRL, GSM0808_LCLS_CSC_NA);
609 lcls->corr_needed = TLVP_PRESENT(tp, GSM0808_IE_LCLS_CORR_NOT_NEEDED) ? false : true;
610
611 return ret;
612}
613
Harald Welte171ef822019-03-28 10:49:05 +0100614static __thread char dbuf[256];
Max5ec0cf52019-01-15 16:37:09 +0100615
616/*! Dump LCLS parameters (GCR excluded) into string for printing.
Harald Welte4a62eda2019-03-18 18:27:00 +0100617 * \param[out] buf caller-allocated output string buffer
618 * \param[in] buf_len size of buf in bytes
Max5ec0cf52019-01-15 16:37:09 +0100619 * \param[in] lcls pointer to the struct to print.
620 * \returns string representation of LCLS or NULL on error. */
Harald Welte4a62eda2019-03-18 18:27:00 +0100621char *osmo_lcls_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls)
Max5ec0cf52019-01-15 16:37:09 +0100622{
Harald Welte4a62eda2019-03-18 18:27:00 +0100623 struct osmo_strbuf s = { .buf = buf, .len = buf_len };
Max5ec0cf52019-01-15 16:37:09 +0100624
625 if (!lcls)
626 return NULL;
627
628 OSMO_STRBUF_PRINTF(s, "LCLS Config: %s, Control: %s, Correlation-Needed: %u",
629 gsm0808_lcls_config_name(lcls->config),
630 gsm0808_lcls_control_name(lcls->control),
631 lcls->corr_needed);
632
633 return dbuf;
634}
635
Harald Welte4a62eda2019-03-18 18:27:00 +0100636/*! Dump LCLS parameters (GCR excluded) into static string buffer for printing.
637 * \param[in] lcls pointer to the struct to print.
638 * \returns string representation of LCLS in static buffer or NULL on error. */
639char *osmo_lcls_dump(const struct osmo_lcls *lcls)
640{
641 return osmo_lcls_dump_buf(dbuf, sizeof(dbuf), lcls);
642}
643
Harald Welte179f3572019-03-18 18:38:47 +0100644char *osmo_lcls_dump_c(void *ctx, const struct osmo_lcls *lcls)
645{
646 char *buf = talloc_size(ctx, 256);
647 if (!buf)
648 return NULL;
649 return osmo_lcls_dump_buf(buf, 256, lcls);
650}
651
Max5ec0cf52019-01-15 16:37:09 +0100652/*! Dump GCR struct into string for printing.
Harald Welte4a62eda2019-03-18 18:27:00 +0100653 * \param[out] buf caller-allocated output string buffer
654 * \param[in] buf_len size of buf in bytes
Max5ec0cf52019-01-15 16:37:09 +0100655 * \param[in] lcls pointer to the struct to print.
656 * \returns string representation of GCR or NULL on error. */
Harald Welte4a62eda2019-03-18 18:27:00 +0100657char *osmo_gcr_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls)
Max5ec0cf52019-01-15 16:37:09 +0100658{
Harald Welte4a62eda2019-03-18 18:27:00 +0100659 struct osmo_strbuf s = { .buf = buf, .len = buf_len };
Max5ec0cf52019-01-15 16:37:09 +0100660
661 if (!lcls)
662 return NULL;
663
664 if (lcls->gcr_available) {
665 OSMO_STRBUF_PRINTF(s, "GCR NetID 0x%s, ", osmo_hexdump_nospc(lcls->gcr.net, lcls->gcr.net_len));
666 /* osmo_hexdump() uses static buffers so we can't call it twice withing the same parameter list */
667 OSMO_STRBUF_PRINTF(s, "Node 0x%x, CallRefID 0x%s", lcls->gcr.node, osmo_hexdump_nospc(lcls->gcr.cr, 5));
668 }
669
670 return dbuf;
671}
672
Harald Welte4a62eda2019-03-18 18:27:00 +0100673/*! Dump GCR struct into static string buffer for printing.
674 * \param[in] lcls pointer to the struct to print.
675 * \returns string representation of GCR in static buffer or NULL on error. */
676char *osmo_gcr_dump(const struct osmo_lcls *lcls)
677{
678 return osmo_gcr_dump_buf(dbuf, sizeof(dbuf), lcls);
679}
680
681
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200682/*! Encode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200683 * \param[out] msg Message Buffer to which IE is to be appended
684 * \param[in] ei Encryption Information to be encoded
685 * \returns number of bytes appended to \a msg */
Philipp Maier14e76b92017-03-28 18:36:52 +0200686uint8_t gsm0808_enc_encrypt_info(struct msgb *msg,
687 const struct gsm0808_encrypt_info *ei)
688{
689 unsigned int i;
690 uint8_t perm_algo = 0;
691 uint8_t *ptr;
692 uint8_t *old_tail;
693 uint8_t *tlv_len;
694
695 OSMO_ASSERT(msg);
696 OSMO_ASSERT(ei);
697 OSMO_ASSERT(ei->key_len <= ARRAY_SIZE(ei->key));
698 OSMO_ASSERT(ei->perm_algo_len <= ENCRY_INFO_PERM_ALGO_MAXLEN);
699
700 msgb_put_u8(msg, GSM0808_IE_ENCRYPTION_INFORMATION);
701 tlv_len = msgb_put(msg, 1);
702 old_tail = msg->tail;
703
704 for (i = 0; i < ei->perm_algo_len; i++) {
705 /* Note: gsm_08_08.h defines the permitted algorithms
706 * as an enum which ranges from 0x01 to 0x08 */
707 OSMO_ASSERT(ei->perm_algo[i] != 0);
708 OSMO_ASSERT(ei->perm_algo[i] <= ENCRY_INFO_PERM_ALGO_MAXLEN);
709 perm_algo |= (1 << (ei->perm_algo[i] - 1));
710 }
711
712 msgb_put_u8(msg, perm_algo);
713 ptr = msgb_put(msg, ei->key_len);
714 memcpy(ptr, ei->key, ei->key_len);
715
716 *tlv_len = (uint8_t) (msg->tail - old_tail);
717 return *tlv_len + 2;
718}
719
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200720/*! Decode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200721 * \param[out] ei Caller-provided memory to store encryption information
722 * \param[in] elem IE value to be decoded
723 * \param[in] len Length of \a elem in bytes
724 * \returns number of bytes parsed; negative on error */
Philipp Maier14e76b92017-03-28 18:36:52 +0200725int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei,
726 const uint8_t *elem, uint8_t len)
727{
728 uint8_t perm_algo;
729 unsigned int i;
730 unsigned int perm_algo_len = 0;
731 const uint8_t *old_elem = elem;
732
733 OSMO_ASSERT(ei);
734 if (!elem)
735 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200736 if (len == 0)
Philipp Maier14e76b92017-03-28 18:36:52 +0200737 return -EINVAL;
738
739 memset(ei, 0, sizeof(*ei));
740
741 perm_algo = *elem;
742 elem++;
743
744 for (i = 0; i < ENCRY_INFO_PERM_ALGO_MAXLEN; i++) {
745 if (perm_algo & (1 << i)) {
746 ei->perm_algo[perm_algo_len] = i + 1;
747 perm_algo_len++;
748 }
749 }
750 ei->perm_algo_len = perm_algo_len;
751
752 ei->key_len = len - 1;
753 memcpy(ei->key, elem, ei->key_len);
754 elem+=ei->key_len;
755
756 return (int)(elem - old_elem);
757}
Philipp Maier783047e2017-03-29 11:35:50 +0200758
Harald Weltee87693c2019-05-03 20:06:50 +0200759void gsm0808_msgb_put_cell_id_u(struct msgb *msg, enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
760{
761 switch (id_discr) {
762 case CELL_IDENT_WHOLE_GLOBAL: {
763 const struct osmo_cell_global_id *id = &u->global;
764 struct gsm48_loc_area_id lai;
765 gsm48_generate_lai2(&lai, &id->lai);
766 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
767 msgb_put_u16(msg, id->cell_identity);
768 break;
769 }
770 case CELL_IDENT_LAC_AND_CI: {
771 const struct osmo_lac_and_ci_id *id = &u->lac_and_ci;
772 msgb_put_u16(msg, id->lac);
773 msgb_put_u16(msg, id->ci);
774 break;
775 }
776 case CELL_IDENT_CI:
777 msgb_put_u16(msg, u->ci);
778 break;
779 case CELL_IDENT_LAI_AND_LAC: {
780 const struct osmo_location_area_id *id = &u->lai_and_lac;
781 struct gsm48_loc_area_id lai;
782 gsm48_generate_lai2(&lai, id);
783 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
784 break;
785 }
786 case CELL_IDENT_LAC:
787 msgb_put_u16(msg, u->lac);
788 break;
789 case CELL_IDENT_BSS:
790 case CELL_IDENT_NO_CELL:
791 /* Does not have any list items */
792 break;
793 default:
794 /* Support for other identifier list types is not implemented. */
795 OSMO_ASSERT(false);
796 }
797}
798
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200799/*! Encode TS 08.08 Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +0200800 * \param[out] msg Message Buffer to which IE is to be appended
801 * \param[in] cil Cell ID List to be encoded
802 * \returns number of bytes appended to \a msg */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100803uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg,
804 const struct gsm0808_cell_id_list2 *cil)
805{
806 uint8_t *old_tail;
807 uint8_t *tlv_len;
808 unsigned int i;
809
810 OSMO_ASSERT(msg);
811 OSMO_ASSERT(cil);
812
813 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
814 tlv_len = msgb_put(msg, 1);
815 old_tail = msg->tail;
816
817 msgb_put_u8(msg, cil->id_discr & 0x0f);
818
819 OSMO_ASSERT(cil->id_list_len <= GSM0808_CELL_ID_LIST2_MAXLEN)
Harald Weltee87693c2019-05-03 20:06:50 +0200820 for (i = 0; i < cil->id_list_len; i++)
821 gsm0808_msgb_put_cell_id_u(msg, cil->id_discr, &cil->id_list[i]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100822
823 *tlv_len = (uint8_t) (msg->tail - old_tail);
824 return *tlv_len + 2;
825}
826
827/*! DEPRECATED: Use gsm0808_enc_cell_id_list2 instead.
828 *
829 * Encode TS 08.08 Cell Identifier List IE
830 * \param[out] msg Message Buffer to which IE is to be appended
831 * \param[in] cil Cell ID List to be encoded
832 * \returns number of bytes appended to \a msg */
Philipp Maier783047e2017-03-29 11:35:50 +0200833uint8_t gsm0808_enc_cell_id_list(struct msgb *msg,
834 const struct gsm0808_cell_id_list *cil)
835{
836 uint8_t *old_tail;
837 uint8_t *tlv_len;
838 unsigned int i;
839
840 OSMO_ASSERT(msg);
841 OSMO_ASSERT(cil);
842
843 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
844 tlv_len = msgb_put(msg, 1);
845 old_tail = msg->tail;
846
847 msgb_put_u8(msg, cil->id_discr & 0x0f);
848
849 switch (cil->id_discr) {
850 case CELL_IDENT_LAC:
851 OSMO_ASSERT(cil->id_list_len <= CELL_ID_LIST_LAC_MAXLEN)
852 for (i=0;i<cil->id_list_len;i++) {
853 msgb_put_u16(msg, cil->id_list_lac[i]);
854 }
855 break;
856 case CELL_IDENT_BSS:
857 /* Does not have any list items */
858 break;
859 default:
860 /* FIXME: Implement support for all identifier list elements */
861 OSMO_ASSERT(false);
862 }
863
864 *tlv_len = (uint8_t) (msg->tail - old_tail);
865 return *tlv_len + 2;
866}
867
Stefan Sperling23381452018-03-15 19:38:15 +0100868/* Decode 5-byte LAI list element data (see TS 08.08 3.2.2.27) into MCC/MNC/LAC. */
869static void decode_lai(const uint8_t *data, struct osmo_location_area_id *decoded)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100870{
871 struct gsm48_loc_area_id lai;
872
Stefan Sperling23381452018-03-15 19:38:15 +0100873 /* Copy data to stack to prevent unaligned access in gsm48_decode_lai2(). */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100874 memcpy(&lai, data, sizeof(lai)); /* don't byte swap yet */
875
Stefan Sperling23381452018-03-15 19:38:15 +0100876 gsm48_decode_lai2(&lai, decoded);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100877}
878
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100879static 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 +0100880 size_t *consumed)
881{
882 struct osmo_cell_global_id *id;
883 uint16_t *ci_be;
884 size_t lai_offset;
885 int i = 0;
886 const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*ci_be);
887
888 *consumed = 0;
889 while (remain >= elemlen) {
890 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
891 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100892 id = &cil->id_list[i].global;
Stefan Sperling2873bf12018-03-14 18:38:41 +0100893 lai_offset = i * elemlen;
Stefan Sperling23381452018-03-15 19:38:15 +0100894 decode_lai(&data[lai_offset], &id->lai);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100895 ci_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]);
896 id->cell_identity = osmo_load16be(ci_be);
897 *consumed += elemlen;
898 remain -= elemlen;
899 i++;
900 }
901
902 return i;
903}
904
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100905static 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 +0100906 size_t *consumed)
907{
908 uint16_t *lacp_be, *ci_be;
909 struct osmo_lac_and_ci_id *id;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100910 int i = 0, j = 0;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100911 const size_t elemlen = sizeof(*lacp_be) + sizeof(*ci_be);
912
913 *consumed = 0;
914
915 if (remain < elemlen)
916 return -EINVAL;
917
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100918 lacp_be = (uint16_t *)(&data[j]);
919 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100920 while (remain >= elemlen) {
921 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
922 return -ENOSPC;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100923 id = &cil->id_list[i++].lac_and_ci;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100924 id->lac = osmo_load16be(lacp_be);
925 id->ci = osmo_load16be(ci_be);
926 *consumed += elemlen;
927 remain -= elemlen;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100928 j += elemlen;
929 lacp_be = (uint16_t *)(&data[j]);
930 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100931 }
932
933 return i;
934}
935
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100936static int parse_cell_id_ci_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain,
937 size_t *consumed)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100938{
939 const uint16_t *ci_be = (const uint16_t *)data;
940 int i = 0;
941 const size_t elemlen = sizeof(*ci_be);
942
943 *consumed = 0;
944 while (remain >= elemlen) {
945 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
946 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100947 cil->id_list[i++].ci = osmo_load16be(ci_be++);
Stefan Sperling9c62fc62018-03-16 10:23:34 +0100948 *consumed += elemlen;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100949 remain -= elemlen;
950 }
951 return i;
952}
953
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100954static 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 +0100955 size_t *consumed)
956{
957 struct osmo_location_area_id *id;
958 int i = 0;
959 const size_t elemlen = sizeof(struct gsm48_loc_area_id);
960
961 *consumed = 0;
962 while (remain >= elemlen) {
963 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
964 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100965 id = &cil->id_list[i].lai_and_lac;
Stefan Sperling23381452018-03-15 19:38:15 +0100966 decode_lai(&data[i * elemlen], id);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100967 *consumed += elemlen;
968 remain -= elemlen;
969 i++;
970 }
971
972 return i;
973}
974
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100975static 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 +0100976{
977 const uint16_t *lac_be = (const uint16_t *)data;
978 int i = 0;
979 const size_t elemlen = sizeof(*lac_be);
980
981 *consumed = 0;
982 while (remain >= elemlen) {
983 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
984 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100985 cil->id_list[i++].lac = osmo_load16be(lac_be++);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100986 *consumed += elemlen;
987 remain -= elemlen;
988 }
989 return i;
990}
991
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200992/*! Decode Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +0200993 * \param[out] cil Caller-provided memory to store Cell ID list
994 * \param[in] elem IE value to be decoded
995 * \param[in] len Length of \a elem in bytes
996 * \returns number of bytes parsed; negative on error */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100997int gsm0808_dec_cell_id_list2(struct gsm0808_cell_id_list2 *cil,
998 const uint8_t *elem, uint8_t len)
999{
1000 uint8_t id_discr;
1001 size_t bytes_elem = 0;
1002 int list_len = 0;
1003
1004 OSMO_ASSERT(cil);
1005 if (!elem)
1006 return -EINVAL;
1007 if (len == 0)
1008 return -EINVAL;
1009
1010 memset(cil, 0, sizeof(*cil));
1011
1012 id_discr = *elem & 0x0f;
1013 elem++;
1014 len--;
1015
1016 switch (id_discr) {
1017 case CELL_IDENT_WHOLE_GLOBAL:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001018 list_len = parse_cell_id_global_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001019 break;
1020 case CELL_IDENT_LAC_AND_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001021 list_len = parse_cell_id_lac_and_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001022 break;
1023 case CELL_IDENT_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001024 list_len = parse_cell_id_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001025 break;
1026 case CELL_IDENT_LAI_AND_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001027 list_len = parse_cell_id_lai_and_lac(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001028 break;
1029 case CELL_IDENT_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001030 list_len = parse_cell_id_lac_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001031 break;
1032 case CELL_IDENT_BSS:
1033 case CELL_IDENT_NO_CELL:
1034 /* Does not have any list items */
1035 break;
1036 default:
1037 /* Remaining cell identification types are not implemented. */
1038 return -EINVAL;
1039 }
1040
1041 if (list_len < 0) /* parsing error */
1042 return list_len;
1043
1044 cil->id_discr = id_discr;
1045 cil->id_list_len = list_len;
1046
1047 /* One byte for the cell ID discriminator + any remaining bytes in
1048 * the IE which were consumed by the parser functions above. */
1049 return 1 + (int)bytes_elem;
1050}
1051
1052/*! DEPRECATED: Use gsm0808_dec_cell_id_list2 instead.
1053 *
1054 * Decode Cell Identifier List IE
1055 * \param[out] cil Caller-provided memory to store Cell ID list
1056 * \param[in] elem IE value to be decoded
1057 * \param[in] len Length of \a elem in bytes
1058 * \returns number of bytes parsed; negative on error */
Philipp Maier783047e2017-03-29 11:35:50 +02001059int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil,
1060 const uint8_t *elem, uint8_t len)
1061{
1062 uint8_t id_discr;
1063 const uint8_t *old_elem = elem;
1064 unsigned int item_count = 0;
1065
1066 OSMO_ASSERT(cil);
1067 if (!elem)
1068 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +02001069 if (len == 0)
Philipp Maier783047e2017-03-29 11:35:50 +02001070 return -EINVAL;
1071
1072 memset(cil, 0, sizeof(*cil));
1073
1074 id_discr = *elem & 0x0f;
1075 elem++;
1076 len--;
1077
1078 cil->id_discr = id_discr;
1079
1080 switch (id_discr) {
1081 case CELL_IDENT_LAC:
1082 while (len >= 2) {
1083 cil->id_list_lac[item_count] = osmo_load16be(elem);
1084 elem += 2;
1085 item_count++;
1086 len -= 2;
1087 }
1088 case CELL_IDENT_BSS:
1089 /* Does not have any list items */
1090 break;
1091 default:
1092 /* FIXME: Implement support for all identifier list elements */
1093 return -EINVAL;
1094 }
1095
1096 cil->id_list_len = item_count;
1097 return (int)(elem - old_elem);
1098}
Harald Welte96e2a002017-06-12 21:44:18 +02001099
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001100static bool same_cell_id_list_entries(const struct gsm0808_cell_id_list2 *a, int ai,
1101 const struct gsm0808_cell_id_list2 *b, int bi)
1102{
1103 struct gsm0808_cell_id_list2 tmp = {
1104 .id_discr = a->id_discr,
1105 .id_list_len = 1,
1106 };
1107 uint8_t buf_a[32 + sizeof(struct msgb)];
1108 uint8_t buf_b[32 + sizeof(struct msgb)];
1109 struct msgb *msg_a = (void*)buf_a;
1110 struct msgb *msg_b = (void*)buf_b;
1111
1112 msg_a->data_len = 32;
1113 msg_b->data_len = 32;
1114 msgb_reset(msg_a);
1115 msgb_reset(msg_b);
1116
1117 if (a->id_discr != b->id_discr)
1118 return false;
1119 if (ai >= a->id_list_len
1120 || bi >= b->id_list_len)
1121 return false;
1122
1123 tmp.id_list[0] = a->id_list[ai];
1124 gsm0808_enc_cell_id_list2(msg_a, &tmp);
1125
1126 tmp.id_list[0] = b->id_list[bi];
1127 gsm0808_enc_cell_id_list2(msg_b, &tmp);
1128
1129 if (msg_a->len != msg_b->len)
1130 return false;
1131 if (memcmp(msg_a->data, msg_b->data, msg_a->len))
1132 return false;
1133
1134 return true;
1135}
1136
1137/*! Append entries from one Cell Identifier List to another.
1138 * The cell identifier types must be identical between the two lists.
1139 * \param dst[out] Append entries to this list.
1140 * \param src[in] Append these entries to \a dst.
1141 * \returns the nr of items added, or negative on error: -EINVAL if the id_discr mismatch
1142 * between the lists, -ENOSPC if the destination list does not have enough space. If an error is
1143 * returned, \a dst may have already been changed (particularly on -ENOSPC). Note that a return value
1144 * of zero may occur when the src->id_list_len is zero, or when all entries from \a src already exist
1145 * in \a dst, and does not indicate error per se. */
1146int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src)
1147{
1148 int i, j;
1149 int added = 0;
1150
1151 if (dst->id_list_len == 0
1152 && dst->id_discr != CELL_IDENT_BSS)
1153 dst->id_discr = src->id_discr;
1154 else if (dst->id_discr != src->id_discr)
1155 return -EINVAL;
1156
1157 for (i = 0; i < src->id_list_len; i++) {
1158 /* don't add duplicate entries */
1159 bool skip = false;
1160 for (j = 0; j < dst->id_list_len; j++) {
1161 if (same_cell_id_list_entries(dst, j, src, i)) {
1162 skip = true;
1163 break;
1164 }
1165 }
1166 if (skip)
1167 continue;
1168
1169 if (dst->id_list_len >= ARRAY_SIZE(dst->id_list))
1170 return -ENOSPC;
1171
1172 dst->id_list[dst->id_list_len++] = src->id_list[i];
1173 added ++;
1174 }
1175
1176 return added;
1177}
1178
Neels Hofmeyr38e58412018-05-25 16:56:35 +02001179/*! Convert a single Cell Identifier to a Cell Identifier List with one entry.
1180 * \param dst[out] Overwrite this list.
1181 * \param src[in] Set \a dst to contain exactly this item.
1182 */
1183void gsm0808_cell_id_to_list(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id *src)
1184{
1185 if (!dst)
1186 return;
1187 if (!src) {
1188 *dst = (struct gsm0808_cell_id_list2){
1189 .id_discr = CELL_IDENT_NO_CELL,
1190 };
1191 return;
1192 }
1193
1194 *dst = (struct gsm0808_cell_id_list2){
1195 .id_discr = src->id_discr,
1196 .id_list = { src->id },
1197 .id_list_len = 1,
1198 };
1199
1200 switch (src->id_discr) {
1201 case CELL_IDENT_NO_CELL:
1202 case CELL_IDENT_BSS:
1203 dst->id_list_len = 0;
1204 break;
1205 default:
1206 break;
1207 }
1208}
1209
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001210/*! Encode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1211 * \param[out] msg Message Buffer to which IE is to be appended
1212 * \param[in] ci Cell ID to be encoded
1213 * \returns number of bytes appended to \a msg */
1214uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci)
1215{
1216 uint8_t rc;
1217 uint8_t *ie_tag;
1218 struct gsm0808_cell_id_list2 cil = {
1219 .id_discr = ci->id_discr,
1220 .id_list = { ci->id },
1221 .id_list_len = 1,
1222 };
1223
1224 OSMO_ASSERT(msg);
1225 OSMO_ASSERT(ci);
1226
1227 ie_tag = msg->tail;
1228 rc = gsm0808_enc_cell_id_list2(msg, &cil);
1229
1230 if (rc <= 0)
1231 return rc;
1232
1233 *ie_tag = GSM0808_IE_CELL_IDENTIFIER;
1234 return rc;
1235}
1236
1237/*! Decode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1238 * \param[out] ci Caller-provided memory to store Cell ID.
1239 * \param[in] elem IE value to be decoded.
1240 * \param[in] len Length of \a elem in bytes.
1241 * \returns number of bytes parsed; negative on error */
1242int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len)
1243{
1244 struct gsm0808_cell_id_list2 cil;
1245 int rc;
1246 rc = gsm0808_dec_cell_id_list2(&cil, elem, len);
1247 if (rc < 0)
1248 return rc;
1249 if (cil.id_discr == CELL_IDENT_BSS || cil.id_discr == CELL_IDENT_NO_CELL) {
1250 if (cil.id_list_len != 0)
1251 return -EINVAL;
1252 } else {
1253 if (cil.id_list_len != 1)
1254 return -EINVAL;
1255 }
1256 ci->id_discr = cil.id_discr;
1257 ci->id = cil.id_list[0];
1258 return rc;
1259}
1260
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001261/*! Convert the representation of the permitted speech codec identifier
Philipp Maier3149b0d2017-06-02 13:22:34 +02001262 * that is used in struct gsm0808_channel_type to the speech codec
1263 * representation we use in struct gsm0808_speech_codec.
1264 * \param[in] perm_spch to be converted (see also gsm0808_permitted_speech)
1265 * \returns GSM speech codec type; negative on error */
1266int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch)
1267{
1268 /*! The speech codec type, which is used in the channel type field to
1269 * signal the permitted speech versions (codecs) has a different
1270 * encoding than the type field in the speech codec type element
1271 * (See also 3GPP TS 48.008, 3.2.2.11 and 3.2.2.103) */
1272
1273 switch (perm_spch) {
1274 case GSM0808_PERM_FR1:
1275 return GSM0808_SCT_FR1;
1276 case GSM0808_PERM_FR2:
1277 return GSM0808_SCT_FR2;
1278 case GSM0808_PERM_FR3:
1279 return GSM0808_SCT_FR3;
1280 case GSM0808_PERM_FR4:
1281 return GSM0808_SCT_FR4;
1282 case GSM0808_PERM_FR5:
1283 return GSM0808_SCT_FR5;
1284 case GSM0808_PERM_HR1:
1285 return GSM0808_SCT_HR1;
1286 case GSM0808_PERM_HR3:
1287 return GSM0808_SCT_HR3;
1288 case GSM0808_PERM_HR4:
1289 return GSM0808_SCT_HR4;
1290 case GSM0808_PERM_HR6:
1291 return GSM0808_SCT_HR6;
1292 }
1293
1294 /* Invalid input */
1295 return -EINVAL;
1296}
1297
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001298/*! Extrapolate a speech codec field from a given permitted speech
Philipp Maier884ba0f2017-06-02 13:49:16 +02001299 * parameter (channel type).
1300 * \param[out] sc Caller provided memory to store the resulting speech codec
1301 * \param[in] perm_spch value that is used to derive the speech codec info
1302 * (see also: enum gsm0808_speech_codec_type in gsm0808_utils.h)
1303 * \returns zero when successful; negative on error */
1304int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc,
1305 uint8_t perm_spch)
1306{
1307 int rc;
1308
1309 memset(sc, 0, sizeof(*sc));
1310
1311 /* Determine codec type */
1312 rc = gsm0808_chan_type_to_speech_codec(perm_spch);
1313 if (rc < 0)
1314 return -EINVAL;
1315 sc->type = (uint8_t) rc;
1316
1317 /* Depending on the speech codec type, pick a default codec
1318 * configuration that exactly matches the configuration on the
1319 * air interface. */
1320 switch (sc->type) {
1321 case GSM0808_SCT_FR3:
1322 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR;
1323 break;
1324 case GSM0808_SCT_FR4:
1325 sc->cfg = GSM0808_SC_CFG_DEFAULT_OFR_AMR_WB;
1326 break;
1327 case GSM0808_SCT_FR5:
1328 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR_WB;
1329 break;
1330 case GSM0808_SCT_HR3:
1331 sc->cfg = GSM0808_SC_CFG_DEFAULT_HR_AMR;
1332 break;
1333 case GSM0808_SCT_HR4:
1334 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR_WB;
1335 break;
1336 case GSM0808_SCT_HR6:
1337 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR;
1338 break;
1339 default:
1340 /* Note: Not all codec types specify a default setting,
1341 * in this case, we just set the field to zero. */
1342 sc->cfg = 0;
1343 }
1344
1345 /* Tag all codecs as "Full IP"
1346 * (see als 3GPP TS 48.008 3.2.2.103) */
1347 sc->fi = true;
1348
1349 return 0;
1350}
1351
Philipp Maier5f2eb152018-09-19 13:40:21 +02001352/*! Determine a set of AMR speech codec configuration bits (S0-S15) from a
1353 * given GSM 04.08 AMR configuration struct.
1354 * \param[in] cfg AMR configuration in GSM 04.08 format.
1355 * \param[in] hint if the resulting configuration shall be used with a FR or HR TCH.
1356 * \returns configuration bits (S0-S15) */
Philipp Maier369015c2018-09-21 09:07:20 +02001357uint16_t gsm0808_sc_cfg_from_gsm48_mr_cfg(const struct gsm48_multi_rate_conf *cfg,
Philipp Maier5f2eb152018-09-19 13:40:21 +02001358 bool fr)
1359{
1360 uint16_t s15_s0 = 0;
1361
1362 /* Check each rate bit in the AMR multirate configuration and pick the
1363 * matching default configuration as specified in 3GPP TS 28.062,
1364 * Table 7.11.3.1.3-2. */
1365 if (cfg->m4_75)
1366 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_4_75;
1367 if (cfg->m5_15)
1368 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_15;
1369 if (cfg->m5_90)
1370 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_90;
1371 if (cfg->m6_70)
1372 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_6_70;
1373 if (cfg->m7_40)
1374 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_40;
1375 if (cfg->m7_95)
1376 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_95;
1377 if (cfg->m10_2)
1378 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_10_2;
1379 if (cfg->m12_2)
1380 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_12_2;
1381
1382 /* Note: 3GPP TS 48.008, chapter 3GPP TS 48.008 states that for AMR
1383 * some of the configuration bits must be coded as zeros. The applied
1384 * bitmask matches the default codec settings. See also the definition
1385 * of enum gsm0808_speech_codec_defaults in gsm_08_08.h and
1386 * 3GPP TS 28.062, Table 7.11.3.1.3-2. */
1387 if (fr)
1388 s15_s0 &= GSM0808_SC_CFG_DEFAULT_FR_AMR;
1389 else
1390 s15_s0 &= GSM0808_SC_CFG_DEFAULT_HR_AMR;
1391
Philipp Maier94d79fd2019-03-01 10:40:48 +01001392 /* The mode that is encoded by S1 (Config-NB-Code = 1), takes a special
1393 * role as it does not stand for a single rate, but for up to four rates
1394 * at once (12.2, 7.4, 5.9, 4.75). We must check if the supplied cfg
1395 * covers this mode. If not, we need to make sure that the related
1396 * bit is removed. (See also 3GPP TS 28.062, Table 7.11.3.1.3-2) */
1397 if (!(cfg->m12_2 && cfg->m7_40 && cfg->m5_90 && cfg->m4_75) && fr)
1398 s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20;
1399 else if (!(cfg->m7_40 && cfg->m5_90 && cfg->m4_75))
1400 s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20;
1401
Philipp Maier5f2eb152018-09-19 13:40:21 +02001402 return s15_s0;
1403}
1404
Philipp Maier8515d032018-09-25 15:57:49 +02001405/*! Determine a GSM 04.08 AMR configuration struct from a set of speech codec
1406 * configuration bits (S0-S15)
1407 * \param[out] cfg AMR configuration in GSM 04.08 format.
Philipp Maier3713af82019-02-27 16:48:25 +01001408 * \param[in] s15_s0 configuration bits (S15-S0, non-ambiguous).
1409 * \returns zero when successful; negative on error */
1410int gsm48_mr_cfg_from_gsm0808_sc_cfg(struct gsm48_multi_rate_conf *cfg,
1411 uint16_t s15_s0)
Philipp Maier8515d032018-09-25 15:57:49 +02001412{
Philipp Maier3713af82019-02-27 16:48:25 +01001413 unsigned int count = 0;
1414
1415 /* Note: See also: 3GPP TS 28.062
1416 * Table 7.11.3.1.3-2: Preferred Configurations for the Adaptive
1417 * Multi-Rate Codec Types */
1418
1419 /* Note: The resulting multirate-configuration must not contain an
1420 * active set of more than four codec rates. The active set also
1421 * must contain at least one rate. */
1422
Philipp Maier8515d032018-09-25 15:57:49 +02001423 memset(cfg, 0, sizeof(*cfg));
Philipp Maier3713af82019-02-27 16:48:25 +01001424 cfg->ver = 1;
1425 cfg->icmi = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001426
1427 /* Strip option bits */
1428 s15_s0 &= 0x00ff;
1429
Philipp Maier3713af82019-02-27 16:48:25 +01001430 /* Rate 5,15k can never be selected (see table) */
1431 cfg->m5_15 = 0;
Philipp Maier8515d032018-09-25 15:57:49 +02001432
Philipp Maier3713af82019-02-27 16:48:25 +01001433 if (s15_s0 & GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20 & 0xff) {
1434 /* Table Table 7.11.3.1.3-2 lists one mode that selects 4
1435 * rates at once (Config-NB-Code = 1). The rates selected
1436 * are known to be compatible between GERAN and UTRAN, since
1437 * an active set must not contain more than four rates at
1438 * a time, we ignore all other settings as they are either
1439 * redundaned or excess settings (invalid) */
Philipp Maier8515d032018-09-25 15:57:49 +02001440 cfg->m4_75 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001441 cfg->m5_90 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001442 cfg->m7_40 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001443 cfg->m12_2 = 1;
Philipp Maier3713af82019-02-27 16:48:25 +01001444 count += 4;
1445 }
Philipp Maier8515d032018-09-25 15:57:49 +02001446
Philipp Maier3713af82019-02-27 16:48:25 +01001447 /* Check the bits in s15_s0 and set the flags for the
1448 * respective rates. */
1449 if (s15_s0 & GSM0808_SC_CFG_AMR_4_75 && !cfg->m4_75) {
1450 if (count >= 4)
1451 return -EINVAL;
1452 cfg->m4_75 = 1;
1453 count++;
1454 }
1455 if (s15_s0 & GSM0808_SC_CFG_AMR_5_90 && !cfg->m5_90) {
1456 if (count >= 4)
1457 return -EINVAL;
1458 cfg->m5_90 = 1;
1459 count++;
1460 }
1461 if (s15_s0 & GSM0808_SC_CFG_AMR_6_70) {
1462 if (count >= 4)
1463 return -EINVAL;
1464 cfg->m6_70 = 1;
1465 count++;
1466 }
1467 if (s15_s0 & GSM0808_SC_CFG_AMR_7_40 && !cfg->m7_40) {
1468 if (count >= 4)
1469 return -EINVAL;
1470 cfg->m7_40 = 1;
1471 count++;
1472 }
1473 if (s15_s0 & GSM0808_SC_CFG_AMR_7_95) {
1474 if (count >= 4)
1475 return -EINVAL;
1476 cfg->m7_95 = 1;
1477 count++;
1478 }
1479 if (s15_s0 & GSM0808_SC_CFG_AMR_10_2) {
1480 if (count >= 4)
1481 return -EINVAL;
1482 cfg->m10_2 = 1;
1483 count++;
1484 }
1485 if (s15_s0 & GSM0808_SC_CFG_AMR_12_2 && !cfg->m12_2) {
1486 if (count >= 4)
1487 return -EINVAL;
1488 cfg->m12_2 = 1;
1489 count++;
1490 }
1491
1492 if (count == 0)
1493 return -EINVAL;
1494
1495 return 0;
Philipp Maier8515d032018-09-25 15:57:49 +02001496}
1497
Maxed651d22018-11-07 15:25:05 +01001498int gsm0808_get_cipher_reject_cause(const struct tlv_parsed *tp)
1499{
1500 const uint8_t *buf = TLVP_VAL_MINLEN(tp, GSM0808_IE_CAUSE, 1);
1501
1502 if (!buf)
1503 return -EBADMSG;
1504
1505 if (TLVP_LEN(tp, GSM0808_IE_CAUSE) > 1) {
1506 if (!gsm0808_cause_ext(buf[0]))
1507 return -EINVAL;
1508 return buf[1];
1509 }
1510
1511 return buf[0];
1512}
1513
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001514/*! Print a human readable name of the cell identifier to the char buffer.
1515 * This is useful both for struct gsm0808_cell_id and struct gsm0808_cell_id_list2.
1516 * See also gsm0808_cell_id_name() and gsm0808_cell_id_list_name().
1517 * \param[out] buf Destination buffer to write string representation to.
1518 * \param[in] buflen Amount of memory available in \a buf.
1519 * \param[in] id_discr Cell Identifier type.
1520 * \param[in] u Cell Identifer value.
1521 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1522 * or that would have been written if the buffer were large enough.
1523 */
1524int gsm0808_cell_id_u_name(char *buf, size_t buflen,
1525 enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
1526{
1527 switch (id_discr) {
1528 case CELL_IDENT_LAC:
1529 return snprintf(buf, buflen, "%u", u->lac);
1530 case CELL_IDENT_CI:
1531 return snprintf(buf, buflen, "%u", u->ci);
1532 case CELL_IDENT_LAC_AND_CI:
1533 return snprintf(buf, buflen, "%u-%u", u->lac_and_ci.lac, u->lac_and_ci.ci);
1534 case CELL_IDENT_LAI_AND_LAC:
1535 return snprintf(buf, buflen, "%s", osmo_lai_name(&u->lai_and_lac));
1536 case CELL_IDENT_WHOLE_GLOBAL:
1537 return snprintf(buf, buflen, "%s", osmo_cgi_name(&u->global));
1538 default:
1539 /* For CELL_IDENT_BSS and CELL_IDENT_NO_CELL, just print the discriminator.
1540 * Same for kinds we have no string representation of yet. */
1541 return snprintf(buf, buflen, "%s", gsm0808_cell_id_discr_name(id_discr));
1542 }
1543}
1544
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02001545/* Store individual Cell Identifier information in a CGI, without clearing the remaining ones.
1546 * This is useful to supplement one CGI with information from more than one Cell Identifier,
1547 * which in turn is useful to match Cell Identifiers of differing kinds to each other.
1548 * Before first invocation, clear the *dst struct externally, this function does only write those members
1549 * that are present in parameter u.
1550 */
1551static void cell_id_to_cgi(struct osmo_cell_global_id *dst,
1552 enum CELL_IDENT discr, const union gsm0808_cell_id_u *u)
1553{
1554 switch (discr) {
1555 case CELL_IDENT_WHOLE_GLOBAL:
1556 *dst = u->global;
1557 return;
1558
1559 case CELL_IDENT_LAC_AND_CI:
1560 dst->lai.lac = u->lac_and_ci.lac;
1561 dst->cell_identity = u->lac_and_ci.ci;
1562 return;
1563
1564 case CELL_IDENT_CI:
1565 dst->cell_identity = u->ci;
1566 return;
1567
1568 case CELL_IDENT_LAI_AND_LAC:
1569 dst->lai = u->lai_and_lac;
1570 return;
1571
1572 case CELL_IDENT_LAC:
1573 dst->lai.lac = u->lac;
1574 return;
1575
1576 case CELL_IDENT_NO_CELL:
1577 case CELL_IDENT_BSS:
1578 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1579 case CELL_IDENT_UTRAN_RNC:
1580 case CELL_IDENT_UTRAN_LAC_RNC:
1581 /* No values to set. */
1582 return;
1583 }
1584}
1585
1586/*! Return true if the common information between the two Cell Identifiers match.
1587 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1588 * Note that CELL_IDENT_NO_CELL will always return false.
1589 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1590 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1591 * \param[in] discr1 Cell Identifier type.
1592 * \param[in] u1 Cell Identifier value.
1593 * \param[in] discr2 Other Cell Identifier type.
1594 * \param[in] u2 Other Cell Identifier value.
1595 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1596 * \returns True if the common fields of the above match.
1597 */
1598static bool gsm0808_cell_id_u_match(enum CELL_IDENT discr1, const union gsm0808_cell_id_u *u1,
1599 enum CELL_IDENT discr2, const union gsm0808_cell_id_u *u2,
1600 bool exact_match)
1601{
1602 struct osmo_cell_global_id a = {};
1603 struct osmo_cell_global_id b = {};
1604
1605 if (exact_match && discr1 != discr2)
1606 return false;
1607
1608 /* First handle the odd wildcard like CELL_IDENT kinds. We can't really match any of these. */
1609 switch (discr1) {
1610 case CELL_IDENT_NO_CELL:
1611 case CELL_IDENT_BSS:
1612 return discr1 == discr2;
1613 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1614 case CELL_IDENT_UTRAN_RNC:
1615 case CELL_IDENT_UTRAN_LAC_RNC:
1616 return false;
1617 default:
1618 break;
1619 }
1620 switch (discr2) {
1621 case CELL_IDENT_NO_CELL:
1622 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1623 case CELL_IDENT_UTRAN_RNC:
1624 case CELL_IDENT_UTRAN_LAC_RNC:
1625 case CELL_IDENT_BSS:
1626 return false;
1627 default:
1628 break;
1629 }
1630
1631 /* Enrich both sides to full CGI, then compare those. First set the *other* ID's values in case
1632 * they assign more items. For example:
1633 * u1 = LAC:42
1634 * u2 = LAC+CI:23+5
1635 * 1) a <- LAC+CI:23+5
1636 * 2) a <- LAC:42 so that a = LAC+CI:42+5
1637 * Now we can compare those two and find a mismatch. If the LAC were the same, we would get
1638 * identical LAC+CI and hence a match. */
1639
1640 cell_id_to_cgi(&a, discr2, u2);
1641 cell_id_to_cgi(&a, discr1, u1);
1642
1643 cell_id_to_cgi(&b, discr1, u1);
1644 cell_id_to_cgi(&b, discr2, u2);
1645
1646 return osmo_cgi_cmp(&a, &b) == 0;
1647}
1648
1649/*! Return true if the common information between the two Cell Identifiers match.
1650 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1651 * Note that CELL_IDENT_NO_CELL will always return false.
1652 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1653 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1654 * \param[in] id1 Cell Identifier.
1655 * \param[in] id2 Other Cell Identifier.
1656 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1657 * \returns True if the common fields of the above match.
1658 */
1659bool gsm0808_cell_ids_match(const struct gsm0808_cell_id *id1, const struct gsm0808_cell_id *id2, bool exact_match)
1660{
1661 return gsm0808_cell_id_u_match(id1->id_discr, &id1->id, id2->id_discr, &id2->id, exact_match);
1662}
1663
1664/*! Find an index in a Cell Identifier list that matches a given single Cell Identifer.
1665 * Compare \a id against each entry in \a list using gsm0808_cell_ids_match(), and return the list index
1666 * if a match is found. \a match_nr allows iterating all matches in the list. A match_nr <= 0 returns the
1667 * first match in the list, match_nr == 1 the second match, etc., and if match_nr exceeds the available
1668 * matches in the list, -1 is returned.
1669 * \param[in] id Cell Identifier to match.
1670 * \param[in] list Cell Identifier list to search in.
1671 * \param[in] match_nr Ignore this many matches.
1672 * \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 +01001673 * \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
1674 * entry).
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02001675 */
1676int gsm0808_cell_id_matches_list(const struct gsm0808_cell_id *id, const struct gsm0808_cell_id_list2 *list,
1677 unsigned int match_nr, bool exact_match)
1678{
1679 int i;
1680 for (i = 0; i < list->id_list_len; i++) {
1681 if (gsm0808_cell_id_u_match(id->id_discr, &id->id, list->id_discr, &list->id_list[i], exact_match)) {
1682 if (match_nr)
1683 match_nr--;
1684 else
1685 return i;
1686 }
1687 }
1688 return -1;
1689}
1690
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001691/*! Copy information from a CGI to form a Cell Identifier of the specified kind.
1692 * \param [out] cid Compose new Cell Identifier here.
1693 * \param [in] id_discr Which kind of Cell Identifier to compose.
1694 * \param [in] cgi Cell Global Identifier to form the Cell Identifier from.
1695 */
1696void gsm0808_cell_id_from_cgi(struct gsm0808_cell_id *cid, enum CELL_IDENT id_discr,
1697 const struct osmo_cell_global_id *cgi)
1698{
1699 *cid = (struct gsm0808_cell_id){
1700 .id_discr = id_discr,
1701 };
1702
1703 switch (id_discr) {
1704 case CELL_IDENT_WHOLE_GLOBAL:
1705 cid->id.global = *cgi;
1706 return;
1707
1708 case CELL_IDENT_LAC_AND_CI:
1709 cid->id.lac_and_ci = (struct osmo_lac_and_ci_id){
1710 .lac = cgi->lai.lac,
1711 .ci = cgi->cell_identity,
1712 };
1713 return;
1714
1715 case CELL_IDENT_CI:
1716 cid->id.ci = cgi->cell_identity;
1717 return;
1718
1719 case CELL_IDENT_LAI:
1720 cid->id.lai_and_lac = cgi->lai;
1721 return;
1722
1723 case CELL_IDENT_LAC:
1724 cid->id.lac = cgi->lai.lac;
1725 return;
1726
1727 case CELL_IDENT_NO_CELL:
1728 case CELL_IDENT_BSS:
1729 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1730 case CELL_IDENT_UTRAN_RNC:
1731 case CELL_IDENT_UTRAN_LAC_RNC:
1732 default:
1733 return;
1734 };
1735}
1736
1737/*! Overwrite parts of cgi with values from a Cell Identifier.
1738 * Place only those items given in cid into cgi, leaving other values unchanged.
1739 * \param[out] cgi Cell Global Identity to write to.
1740 * \param[in] cid Cell Identity to read from.
1741 * \return a bitmask of items that were set: OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI; 0 if nothing was
1742 * written to cgi.
1743 */
1744int gsm0808_cell_id_to_cgi(struct osmo_cell_global_id *cgi, const struct gsm0808_cell_id *cid)
1745{
1746 switch (cid->id_discr) {
1747 case CELL_IDENT_WHOLE_GLOBAL:
1748 *cgi = cid->id.global;
1749 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
1750
1751 case CELL_IDENT_LAC_AND_CI:
1752 cgi->lai.lac = cid->id.lac_and_ci.lac;
1753 cgi->cell_identity = cid->id.lac_and_ci.ci;
1754 return OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
1755
1756 case CELL_IDENT_CI:
1757 cgi->cell_identity = cid->id.ci;
1758 return OSMO_CGI_PART_CI;
1759
1760 case CELL_IDENT_LAI:
1761 cgi->lai = cid->id.lai_and_lac;
1762 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC;
1763
1764 case CELL_IDENT_LAC:
1765 cgi->lai.lac = cid->id.lac;
1766 return OSMO_CGI_PART_LAC;
1767
1768 case CELL_IDENT_NO_CELL:
1769 case CELL_IDENT_BSS:
1770 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1771 case CELL_IDENT_UTRAN_RNC:
1772 case CELL_IDENT_UTRAN_LAC_RNC:
1773 default:
1774 return 0;
1775 };
1776}
1777
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001778/*! value_string[] for enum CELL_IDENT. */
1779const struct value_string gsm0808_cell_id_discr_names[] = {
1780 { CELL_IDENT_WHOLE_GLOBAL, "CGI" },
1781 { CELL_IDENT_LAC_AND_CI, "LAC-CI" },
1782 { CELL_IDENT_CI, "CI" },
1783 { CELL_IDENT_NO_CELL, "NO-CELL" },
1784 { CELL_IDENT_LAI_AND_LAC, "LAI" },
1785 { CELL_IDENT_LAC, "LAC" },
1786 { CELL_IDENT_BSS, "BSS" },
1787 { CELL_IDENT_UTRAN_PLMN_LAC_RNC, "UTRAN-PLMN-LAC-RNC" },
1788 { CELL_IDENT_UTRAN_RNC, "UTRAN-RNC" },
1789 { CELL_IDENT_UTRAN_LAC_RNC, "UTRAN-LAC-RNC" },
1790 { 0, NULL }
1791};
1792
1793#define APPEND_THING(func, args...) do { \
1794 int remain = buflen - (pos - buf); \
1795 int l = func(pos, remain, ##args); \
1796 if (l < 0 || l > remain) \
1797 pos = buf + buflen; \
1798 else \
1799 pos += l; \
1800 if (l > 0) \
1801 total_len += l; \
1802 } while(0)
1803#define APPEND_STR(fmt, args...) APPEND_THING(snprintf, fmt, ##args)
1804#define APPEND_CELL_ID_U(DISCR, U) APPEND_THING(gsm0808_cell_id_u_name, DISCR, U)
1805
Harald Welte179f3572019-03-18 18:38:47 +01001806char *gsm0808_cell_id_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id *cid)
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001807{
1808 char *pos = buf;
1809 int total_len = 0;
1810 APPEND_STR("%s:", gsm0808_cell_id_discr_name(cid->id_discr));
1811 APPEND_CELL_ID_U(cid->id_discr, &cid->id);
1812 return buf;
1813}
1814
1815/*! Return a human readable representation of a Cell Identifier, like "LAC:123"
1816 * or "CGI:001-01-42-23".
1817 * \param[in] cid Cell Identifer.
1818 * \returns String in a static buffer.
1819 */
1820const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid)
1821{
Harald Welte171ef822019-03-28 10:49:05 +01001822 static __thread char buf[64];
Harald Welte179f3572019-03-18 18:38:47 +01001823 return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001824}
1825
1826/*! Like gsm0808_cell_id_name() but uses a different static buffer.
1827 * \param[in] cid Cell Identifer.
1828 * \returns String in a static buffer.
1829 */
1830const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid)
1831{
Harald Welte171ef822019-03-28 10:49:05 +01001832 static __thread char buf[64];
Harald Welte179f3572019-03-18 18:38:47 +01001833 return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
1834}
1835
1836char *gsm0808_cell_id_name_c(const void *ctx, const struct gsm0808_cell_id *cid)
1837{
1838 char *buf = talloc_size(ctx, 64);
1839 if (!buf)
1840 return NULL;
1841 return gsm0808_cell_id_name_buf(buf, 64, cid);
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001842}
1843
1844/*! Return a human readable representation of the Cell Identifier List, like
1845 * "LAC[2]:{123, 456}".
1846 * The return value semantics are like snprintf() and thus allow ensuring a complete
1847 * untruncated string by determining the required string length from the return value.
1848 * If buflen > 0, always nul-terminate the string in buf, also when it is truncated.
1849 * If buflen == 0, do not modify buf, just return the would-be length.
1850 * \param[out] buf Destination buffer to write string representation to.
1851 * \param[in] buflen Amount of memory available in \a buf.
1852 * \param[in] cil Cell Identifer List.
1853 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1854 * or that would have been written if the buffer were large enough.
1855 */
1856int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil)
1857{
1858 char *pos = buf;
1859 int total_len = 0;
1860 int i;
1861
1862 APPEND_STR("%s[%u]", gsm0808_cell_id_discr_name(cil->id_discr), cil->id_list_len);
1863
1864 switch (cil->id_discr) {
1865 case CELL_IDENT_BSS:
1866 case CELL_IDENT_NO_CELL:
1867 return total_len;
1868 default:
1869 break;
1870 }
1871
1872 APPEND_STR(":{");
1873
1874 for (i = 0; i < cil->id_list_len; i++) {
1875 if (i)
1876 APPEND_STR(", ");
1877 APPEND_CELL_ID_U(cil->id_discr, &cil->id_list[i]);
1878 }
1879
1880 APPEND_STR("}");
1881 return total_len;
1882}
1883
1884/*! Return a human-readable representation of \a cil in a static buffer.
1885 * If the list is too long, the output may be truncated.
1886 * See also gsm0808_cell_id_list_name_buf(). */
1887const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil)
1888{
Harald Welte171ef822019-03-28 10:49:05 +01001889 static __thread char buf[1024];
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001890 gsm0808_cell_id_list_name_buf(buf, sizeof(buf), cil);
1891 return buf;
1892}
1893
Harald Welte179f3572019-03-18 18:38:47 +01001894char *gsm0808_cell_id_list_name_c(const void *ctx, const struct gsm0808_cell_id_list2 *cil)
1895{
1896 char *buf = talloc_size(ctx, 1024);
1897 if (!buf)
1898 return NULL;
1899 gsm0808_cell_id_list_name_buf(buf, 1024, cil);
1900 return buf;
1901}
1902
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001903#undef APPEND_STR
1904#undef APPEND_CELL_ID_U
1905
Harald Welte4a62eda2019-03-18 18:27:00 +01001906char *gsm0808_channel_type_name_buf(char *buf, size_t buf_len, const struct gsm0808_channel_type *ct)
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02001907{
Harald Welte4a62eda2019-03-18 18:27:00 +01001908 snprintf(buf, buf_len, "ch_indctr=0x%x ch_rate_type=0x%x perm_spch=%s",
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02001909 ct->ch_indctr, ct->ch_rate_type,
1910 osmo_hexdump(ct->perm_spch, ct->perm_spch_len));
1911 return buf;
1912}
1913
Harald Welte4a62eda2019-03-18 18:27:00 +01001914const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct)
1915{
Harald Welte171ef822019-03-28 10:49:05 +01001916 static __thread char buf[128];
Harald Welte4a62eda2019-03-18 18:27:00 +01001917 return gsm0808_channel_type_name_buf(buf, sizeof(buf), ct);
1918}
1919
Harald Welte179f3572019-03-18 18:38:47 +01001920char *gsm0808_channel_type_name_c(const void *ctx, const struct gsm0808_channel_type *ct)
1921{
1922 char *buf = talloc_size(ctx, 128);
1923 if (!buf)
1924 return NULL;
1925 return gsm0808_channel_type_name_buf(buf, 128, ct);
1926}
1927
Harald Welte96e2a002017-06-12 21:44:18 +02001928/*! @} */