blob: e8259300628d30ef9bd170a6e82452c6140f7143 [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
Harald Welte20725b92017-05-15 12:50:04 +0200175#endif /* HAVE_SYS_SOCKET_H */
176
Philipp Maier6f725d62017-03-24 18:03:17 +0100177/* Helper function for gsm0808_enc_speech_codec()
178 * and gsm0808_enc_speech_codec_list() */
179static uint8_t enc_speech_codec(struct msgb *msg,
180 const struct gsm0808_speech_codec *sc)
181{
182 /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
183 uint8_t header = 0;
184 uint8_t *old_tail;
Harald Welte459a1802018-06-28 09:24:17 +0200185 bool type_extended = false;
Philipp Maierbb839662017-06-01 17:11:19 +0200186
187 /* Note: Extended codec types are codec types that require 8 instead
188 * of 4 bit to fully specify the selected codec. In the following,
189 * we check if we work with an extended type or not. We also check
190 * if the codec type is valid at all. */
191 switch(sc->type) {
192 case GSM0808_SCT_FR1:
193 case GSM0808_SCT_FR2:
194 case GSM0808_SCT_FR3:
195 case GSM0808_SCT_FR4:
196 case GSM0808_SCT_FR5:
197 case GSM0808_SCT_HR1:
198 case GSM0808_SCT_HR3:
199 case GSM0808_SCT_HR4:
200 case GSM0808_SCT_HR6:
201 type_extended = false;
202 break;
203 case GSM0808_SCT_CSD:
204 type_extended = true;
205 break;
206 default:
207 /* Invalid codec type specified */
208 OSMO_ASSERT(false);
209 break;
210 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100211
212 old_tail = msg->tail;
213
214 if (sc->fi)
215 header |= (1 << 7);
216 if (sc->pi)
217 header |= (1 << 6);
218 if (sc->pt)
219 header |= (1 << 5);
220 if (sc->tf)
221 header |= (1 << 4);
Philipp Maierbb839662017-06-01 17:11:19 +0200222
223 if (type_extended) {
Philipp Maier6f725d62017-03-24 18:03:17 +0100224 header |= 0x0f;
225 msgb_put_u8(msg, header);
Philipp Maierbb839662017-06-01 17:11:19 +0200226 msgb_put_u8(msg, sc->type);
Philipp Maier6f725d62017-03-24 18:03:17 +0100227 } else {
228 OSMO_ASSERT(sc->type < 0x0f);
229 header |= sc->type;
230 msgb_put_u8(msg, header);
Philipp Maier6f725d62017-03-24 18:03:17 +0100231 }
232
Philipp Maierbb839662017-06-01 17:11:19 +0200233 /* Note: Whether a configuration is present or not depends on the
234 * selected codec type. If present, it can either consist of one
235 * or two octets, depending on the codec type */
236 switch (sc->type) {
237 case GSM0808_SCT_FR3:
238 case GSM0808_SCT_HR3:
239 case GSM0808_SCT_HR6:
Philipp Maier7e27b142018-03-22 17:26:46 +0100240 msgb_put_u16(msg, osmo_ntohs(sc->cfg));
Philipp Maierbb839662017-06-01 17:11:19 +0200241 break;
242 case GSM0808_SCT_FR4:
243 case GSM0808_SCT_FR5:
244 case GSM0808_SCT_HR4:
245 case GSM0808_SCT_CSD:
246 OSMO_ASSERT((sc->cfg & 0xff00) == 0)
247 msgb_put_u8(msg, (uint8_t) sc->cfg & 0xff);
248 break;
249 default:
250 OSMO_ASSERT(sc->cfg == 0);
251 break;
252 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100253
254 return (uint8_t) (msg->tail - old_tail);
255}
256
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200257/*! Encode TS 08.08 Speech Codec IE
Harald Welte96e2a002017-06-12 21:44:18 +0200258 * \param[out] msg Message Buffer to which IE will be appended
259 * \param[in] sc Speech Codec to be encoded into IE
260 * \returns number of bytes appended to \a msg */
Philipp Maier6f725d62017-03-24 18:03:17 +0100261uint8_t gsm0808_enc_speech_codec(struct msgb *msg,
262 const struct gsm0808_speech_codec *sc)
263{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200264 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100265 uint8_t *old_tail;
266 uint8_t *tlv_len;
267
268 OSMO_ASSERT(msg);
269 OSMO_ASSERT(sc);
270
271 msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC);
272 tlv_len = msgb_put(msg, 1);
273 old_tail = msg->tail;
274
275 enc_speech_codec(msg, sc);
276
277 *tlv_len = (uint8_t) (msg->tail - old_tail);
278 return *tlv_len + 2;
279}
280
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200281/*! Decode TS 08.08 Speech Codec IE
Harald Welte96e2a002017-06-12 21:44:18 +0200282 * \param[out] sc Caller-allocated memory for Speech Codec
283 * \param[in] elem IE value to be decoded
284 * \param[in] len Length of \a elem in bytes
285 * \returns number of bytes parsed; negative on error */
Philipp Maier6f725d62017-03-24 18:03:17 +0100286int gsm0808_dec_speech_codec(struct gsm0808_speech_codec *sc,
287 const uint8_t *elem, uint8_t len)
288{
289 /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
290 uint8_t header;
291 const uint8_t *old_elem = elem;
292
293 OSMO_ASSERT(sc);
294 if (!elem)
295 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200296 if (len == 0)
Philipp Maier6f725d62017-03-24 18:03:17 +0100297 return -EINVAL;
298
299 memset(sc, 0, sizeof(*sc));
300
301 header = *elem;
302
Philipp Maier85a6af22017-04-28 10:55:05 +0200303 /* An extended codec type needs at least two fields,
304 * bail if the input data length is not sufficient. */
Philipp Maier6f725d62017-03-24 18:03:17 +0100305 if ((header & 0x0F) == 0x0F && len < 2)
306 return -EINVAL;
Philipp Maier6f725d62017-03-24 18:03:17 +0100307
308 elem++;
309 len--;
310
311 if (header & (1 << 7))
312 sc->fi = true;
313 if (header & (1 << 6))
314 sc->pi = true;
315 if (header & (1 << 5))
316 sc->pt = true;
317 if (header & (1 << 4))
318 sc->tf = true;
319
320 if ((header & 0x0F) != 0x0F) {
321 sc->type = (header & 0x0F);
Philipp Maierbb839662017-06-01 17:11:19 +0200322 } else {
323 sc->type = *elem;
324 elem++;
325 len--;
Philipp Maier6f725d62017-03-24 18:03:17 +0100326 }
327
Philipp Maierbb839662017-06-01 17:11:19 +0200328 /* Note: Whether a configuration is present or not depends on the
329 * selected codec type. If present, it can either consist of one or
330 * two octets depending on the codec type */
331 switch (sc->type) {
332 case GSM0808_SCT_FR1:
333 case GSM0808_SCT_FR2:
334 case GSM0808_SCT_HR1:
335 break;
336 case GSM0808_SCT_HR4:
337 case GSM0808_SCT_CSD:
338 case GSM0808_SCT_FR4:
339 case GSM0808_SCT_FR5:
340 if (len < 1)
341 return -EINVAL;
342 sc->cfg = *elem;
343 elem++;
344 break;
345 case GSM0808_SCT_FR3:
346 case GSM0808_SCT_HR3:
347 case GSM0808_SCT_HR6:
348 if (len < 2)
349 return -EINVAL;
Philipp Maier7e27b142018-03-22 17:26:46 +0100350 sc->cfg = osmo_load16le(elem);
Philipp Maierbb839662017-06-01 17:11:19 +0200351 elem += 2;
352 break;
353 default:
354 /* Invalid codec type => malformed speech codec element! */
355 return -EINVAL;
356 break;
357 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100358
359 return (int)(elem - old_elem);
360}
361
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200362/*! Encode TS 08.08 Speech Codec list
Harald Welte96e2a002017-06-12 21:44:18 +0200363 * \param[out] msg Message Buffer to which IE is to be appended
364 * \param[in] scl Speech Codec List to be encoded into IE
365 * \returns number of bytes added to \a msg */
Philipp Maier6f725d62017-03-24 18:03:17 +0100366uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg,
367 const struct gsm0808_speech_codec_list *scl)
368{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200369 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100370 uint8_t *old_tail;
371 uint8_t *tlv_len;
372 unsigned int i;
373 uint8_t rc;
374 unsigned int bytes_used = 0;
375
376 OSMO_ASSERT(msg);
377 OSMO_ASSERT(scl);
378
Philipp Maier6f725d62017-03-24 18:03:17 +0100379 msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC_LIST);
380 tlv_len = msgb_put(msg, 1);
381 old_tail = msg->tail;
382
383 for (i = 0; i < scl->len; i++) {
384 rc = enc_speech_codec(msg, &scl->codec[i]);
385 OSMO_ASSERT(rc >= 1);
386 bytes_used += rc;
387 OSMO_ASSERT(bytes_used <= 255);
388 }
389
390 *tlv_len = (uint8_t) (msg->tail - old_tail);
391 return *tlv_len + 2;
392}
393
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200394/*! Decode TS 08.08 Speech Codec list IE
Harald Welte96e2a002017-06-12 21:44:18 +0200395 * \param[out] scl Caller-provided memory to store codec list
396 * \param[in] elem IE value to be decoded
397 * \param[in] len Length of \a elem in bytes
398 * \returns number of bytes parsed; negative on error */
Philipp Maier6f725d62017-03-24 18:03:17 +0100399int gsm0808_dec_speech_codec_list(struct gsm0808_speech_codec_list *scl,
400 const uint8_t *elem, uint8_t len)
401{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200402 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100403 const uint8_t *old_elem = elem;
404 unsigned int i;
405 int rc;
406 uint8_t decoded = 0;
407
408 OSMO_ASSERT(scl);
409 if (!elem)
410 return -EINVAL;
Philipp Maier6f725d62017-03-24 18:03:17 +0100411
412 memset(scl, 0, sizeof(*scl));
413
414 for (i = 0; i < ARRAY_SIZE(scl->codec); i++) {
415 if (len <= 0)
416 break;
417
418 rc = gsm0808_dec_speech_codec(&scl->codec[i], elem, len);
419 if (rc < 1)
420 return -EINVAL;
421
422 elem+=rc;
423 len -= rc;
424 decoded++;
425 }
426
427 scl->len = decoded;
428
Philipp Maier6f725d62017-03-24 18:03:17 +0100429 return (int)(elem - old_elem);
430}
Philipp Maiere0c65302017-03-28 17:05:40 +0200431
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200432/*! Encode TS 08.08 Channel Type IE
Harald Welte96e2a002017-06-12 21:44:18 +0200433 * \param[out] msg Message Buffer to which IE is to be appended
434 * \param[in] ct Channel Type to be encoded
435 * \returns number of bytes added to \a msg */
Philipp Maiere0c65302017-03-28 17:05:40 +0200436uint8_t gsm0808_enc_channel_type(struct msgb *msg,
437 const struct gsm0808_channel_type *ct)
438{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200439 /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */
Philipp Maiere0c65302017-03-28 17:05:40 +0200440 unsigned int i;
441 uint8_t byte;
442 uint8_t *old_tail;
443 uint8_t *tlv_len;
444
445 OSMO_ASSERT(msg);
446 OSMO_ASSERT(ct);
447 OSMO_ASSERT(ct->perm_spch_len <= CHANNEL_TYPE_ELEMENT_MAXLEN - 2);
448
449 /* FIXME: Implement encoding support for Data
450 * and Speech + CTM Text Telephony */
451 if ((ct->ch_indctr & 0x0f) != GSM0808_CHAN_SPEECH
452 && (ct->ch_indctr & 0x0f) != GSM0808_CHAN_SIGN)
453 OSMO_ASSERT(false);
454
455 msgb_put_u8(msg, GSM0808_IE_CHANNEL_TYPE);
456 tlv_len = msgb_put(msg, 1);
457 old_tail = msg->tail;
458
459 msgb_put_u8(msg, ct->ch_indctr & 0x0f);
460 msgb_put_u8(msg, ct->ch_rate_type);
461
462 for (i = 0; i < ct->perm_spch_len; i++) {
463 byte = ct->perm_spch[i];
464
465 if (i < ct->perm_spch_len - 1)
466 byte |= 0x80;
467 msgb_put_u8(msg, byte);
468 }
469
470 *tlv_len = (uint8_t) (msg->tail - old_tail);
471 return *tlv_len + 2;
472}
473
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200474/*! Decode TS 08.08 Channel Type IE
Harald Welte96e2a002017-06-12 21:44:18 +0200475 * \param[out] ct Caller-provided memory to store channel type
476 * \param[in] elem IE Value to be decoded
477 * \param[in] len Length of \a elem in bytes
478 * \returns number of bytes parsed; negative on error */
Philipp Maiere0c65302017-03-28 17:05:40 +0200479int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct,
480 const uint8_t *elem, uint8_t len)
481{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200482 /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */
Philipp Maiere0c65302017-03-28 17:05:40 +0200483 unsigned int i;
484 uint8_t byte;
485 const uint8_t *old_elem = elem;
486
487 OSMO_ASSERT(ct);
488 if (!elem)
489 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200490 if (len < 3 || len > 11)
Philipp Maiere0c65302017-03-28 17:05:40 +0200491 return -EINVAL;
492
493 memset(ct, 0, sizeof(*ct));
494
495 ct->ch_indctr = (*elem) & 0x0f;
496 elem++;
497 ct->ch_rate_type = (*elem) & 0x0f;
498 elem++;
499
500 for (i = 0; i < ARRAY_SIZE(ct->perm_spch); i++) {
501 byte = *elem;
502 elem++;
503 ct->perm_spch[i] = byte & 0x7f;
504 if ((byte & 0x80) == 0x00)
505 break;
506 }
507 ct->perm_spch_len = i + 1;
508
509 return (int)(elem - old_elem);
510}
Philipp Maier14e76b92017-03-28 18:36:52 +0200511
Max969fb2e2018-12-10 11:01:10 +0100512/*! Create BSSMAP Global Call Reference, 3GPP TS 48.008 §3.2.2.115.
513 * \param[out] msg Message Buffer for appending IE
514 * \param[in] g Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1
515 * \returns number of bytes added to \a msg or 0 on error */
Max47022152018-12-19 18:51:00 +0100516static uint8_t gsm0808_enc_gcr(struct msgb *msg, const struct osmo_gcr_parsed *g)
Max969fb2e2018-12-10 11:01:10 +0100517{
518 uint8_t enc, *len = msgb_tl_put(msg, GSM0808_IE_GLOBAL_CALL_REF);
519
520 enc = osmo_enc_gcr(msg, g);
521 if (!enc)
522 return 0;
523
524 *len = enc;
525 return enc + 2; /* type (1 byte) + length (1 byte) */
526}
527
528/*! Decode BSSMAP Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1.
529 * \param[out] gcr Caller-provided memory to store Global Call Reference
Max036012b2018-12-19 17:48:56 +0100530 * \param[in] tp IE values to be decoded
Max969fb2e2018-12-10 11:01:10 +0100531 * \returns number of bytes parsed; negative on error */
Max47022152018-12-19 18:51:00 +0100532static int gsm0808_dec_gcr(struct osmo_gcr_parsed *gcr, const struct tlv_parsed *tp)
Max969fb2e2018-12-10 11:01:10 +0100533{
534 int ret;
535 const uint8_t *buf = TLVP_VAL_MINLEN(tp, GSM0808_IE_GLOBAL_CALL_REF, OSMO_GCR_MIN_LEN);
536 if (!buf)
537 return -EINVAL;
538
539 ret = osmo_dec_gcr(gcr, buf, TLVP_LEN(tp, GSM0808_IE_GLOBAL_CALL_REF));
540 if (ret < 0)
541 return -ENOENT;
542
543 return 2 + ret;
544}
545
Max47022152018-12-19 18:51:00 +0100546/*! Add LCLS parameters to a given msgb, 3GPP TS 48.008 §3.2.2.115 - 3.2.2.120.
547 * \param[out] msg Message Buffer for appending IE
548 * \param[in] lcls LCLS-related data
549 * \returns number of bytes added to \a msg or 0 on error */
550uint8_t gsm0808_enc_lcls(struct msgb *msg, const struct osmo_lcls *lcls)
551{
552 uint8_t enc = 0;
553
554 /* LCLS: §3.2.2.115 Global Call Reference */
Max3b901252019-01-15 14:15:11 +0100555 if (lcls->gcr_available)
556 enc = gsm0808_enc_gcr(msg, &lcls->gcr);
Max47022152018-12-19 18:51:00 +0100557
558 /* LCLS: §3.2.2.116 Configuration */
559 if (lcls->config != GSM0808_LCLS_CFG_NA) {
560 msgb_tv_put(msg, GSM0808_IE_LCLS_CONFIG, lcls->config);
561 enc += 2;
562 }
563
564 /* LCLS: §3.2.2.117 Connection Status Control */
565 if (lcls->control != GSM0808_LCLS_CSC_NA) {
566 msgb_tv_put(msg, GSM0808_IE_LCLS_CONN_STATUS_CTRL, lcls->control);
567 enc += 2;
568 }
569
570 /* LCLS: §3.2.2.118 Correlation-Not-Needed */
571 if (!lcls->corr_needed) {
572 msgb_v_put(msg, GSM0808_IE_LCLS_CORR_NOT_NEEDED);
573 enc++;
574 }
575
576 return enc;
577}
578
579/*! Decode LCLS parameters to a given msgb, 3GPP TS 48.008 §3.2.2.115 - 3.2.2.120.
580 * \param[out] lcls Caller-provided memory to store LCLS-related data
581 * \param[in] tp IE values to be decoded
582 * \returns GCR size or negative on error */
583int gsm0808_dec_lcls(struct osmo_lcls *lcls, const struct tlv_parsed *tp)
584{
Max3b901252019-01-15 14:15:11 +0100585 int ret = gsm0808_dec_gcr(&lcls->gcr, tp);
Max47022152018-12-19 18:51:00 +0100586
Max3b901252019-01-15 14:15:11 +0100587 lcls->gcr_available = (ret < 0) ? false : true;
Max47022152018-12-19 18:51:00 +0100588 lcls->config = tlvp_val8(tp, GSM0808_IE_LCLS_CONFIG, GSM0808_LCLS_CFG_NA);
589 lcls->control = tlvp_val8(tp, GSM0808_IE_LCLS_CONN_STATUS_CTRL, GSM0808_LCLS_CSC_NA);
590 lcls->corr_needed = TLVP_PRESENT(tp, GSM0808_IE_LCLS_CORR_NOT_NEEDED) ? false : true;
591
592 return ret;
593}
594
Max5ec0cf52019-01-15 16:37:09 +0100595static char dbuf[256];
596
597/*! Dump LCLS parameters (GCR excluded) into string for printing.
Harald Welte4a62eda2019-03-18 18:27:00 +0100598 * \param[out] buf caller-allocated output string buffer
599 * \param[in] buf_len size of buf in bytes
Max5ec0cf52019-01-15 16:37:09 +0100600 * \param[in] lcls pointer to the struct to print.
601 * \returns string representation of LCLS or NULL on error. */
Harald Welte4a62eda2019-03-18 18:27:00 +0100602char *osmo_lcls_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls)
Max5ec0cf52019-01-15 16:37:09 +0100603{
Harald Welte4a62eda2019-03-18 18:27:00 +0100604 struct osmo_strbuf s = { .buf = buf, .len = buf_len };
Max5ec0cf52019-01-15 16:37:09 +0100605
606 if (!lcls)
607 return NULL;
608
609 OSMO_STRBUF_PRINTF(s, "LCLS Config: %s, Control: %s, Correlation-Needed: %u",
610 gsm0808_lcls_config_name(lcls->config),
611 gsm0808_lcls_control_name(lcls->control),
612 lcls->corr_needed);
613
614 return dbuf;
615}
616
Harald Welte4a62eda2019-03-18 18:27:00 +0100617/*! Dump LCLS parameters (GCR excluded) into static string buffer for printing.
618 * \param[in] lcls pointer to the struct to print.
619 * \returns string representation of LCLS in static buffer or NULL on error. */
620char *osmo_lcls_dump(const struct osmo_lcls *lcls)
621{
622 return osmo_lcls_dump_buf(dbuf, sizeof(dbuf), lcls);
623}
624
Harald Welte179f3572019-03-18 18:38:47 +0100625char *osmo_lcls_dump_c(void *ctx, const struct osmo_lcls *lcls)
626{
627 char *buf = talloc_size(ctx, 256);
628 if (!buf)
629 return NULL;
630 return osmo_lcls_dump_buf(buf, 256, lcls);
631}
632
Max5ec0cf52019-01-15 16:37:09 +0100633/*! Dump GCR struct into string for printing.
Harald Welte4a62eda2019-03-18 18:27:00 +0100634 * \param[out] buf caller-allocated output string buffer
635 * \param[in] buf_len size of buf in bytes
Max5ec0cf52019-01-15 16:37:09 +0100636 * \param[in] lcls pointer to the struct to print.
637 * \returns string representation of GCR or NULL on error. */
Harald Welte4a62eda2019-03-18 18:27:00 +0100638char *osmo_gcr_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls)
Max5ec0cf52019-01-15 16:37:09 +0100639{
Harald Welte4a62eda2019-03-18 18:27:00 +0100640 struct osmo_strbuf s = { .buf = buf, .len = buf_len };
Max5ec0cf52019-01-15 16:37:09 +0100641
642 if (!lcls)
643 return NULL;
644
645 if (lcls->gcr_available) {
646 OSMO_STRBUF_PRINTF(s, "GCR NetID 0x%s, ", osmo_hexdump_nospc(lcls->gcr.net, lcls->gcr.net_len));
647 /* osmo_hexdump() uses static buffers so we can't call it twice withing the same parameter list */
648 OSMO_STRBUF_PRINTF(s, "Node 0x%x, CallRefID 0x%s", lcls->gcr.node, osmo_hexdump_nospc(lcls->gcr.cr, 5));
649 }
650
651 return dbuf;
652}
653
Harald Welte4a62eda2019-03-18 18:27:00 +0100654/*! Dump GCR struct into static string buffer for printing.
655 * \param[in] lcls pointer to the struct to print.
656 * \returns string representation of GCR in static buffer or NULL on error. */
657char *osmo_gcr_dump(const struct osmo_lcls *lcls)
658{
659 return osmo_gcr_dump_buf(dbuf, sizeof(dbuf), lcls);
660}
661
662
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200663/*! Encode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200664 * \param[out] msg Message Buffer to which IE is to be appended
665 * \param[in] ei Encryption Information to be encoded
666 * \returns number of bytes appended to \a msg */
Philipp Maier14e76b92017-03-28 18:36:52 +0200667uint8_t gsm0808_enc_encrypt_info(struct msgb *msg,
668 const struct gsm0808_encrypt_info *ei)
669{
670 unsigned int i;
671 uint8_t perm_algo = 0;
672 uint8_t *ptr;
673 uint8_t *old_tail;
674 uint8_t *tlv_len;
675
676 OSMO_ASSERT(msg);
677 OSMO_ASSERT(ei);
678 OSMO_ASSERT(ei->key_len <= ARRAY_SIZE(ei->key));
679 OSMO_ASSERT(ei->perm_algo_len <= ENCRY_INFO_PERM_ALGO_MAXLEN);
680
681 msgb_put_u8(msg, GSM0808_IE_ENCRYPTION_INFORMATION);
682 tlv_len = msgb_put(msg, 1);
683 old_tail = msg->tail;
684
685 for (i = 0; i < ei->perm_algo_len; i++) {
686 /* Note: gsm_08_08.h defines the permitted algorithms
687 * as an enum which ranges from 0x01 to 0x08 */
688 OSMO_ASSERT(ei->perm_algo[i] != 0);
689 OSMO_ASSERT(ei->perm_algo[i] <= ENCRY_INFO_PERM_ALGO_MAXLEN);
690 perm_algo |= (1 << (ei->perm_algo[i] - 1));
691 }
692
693 msgb_put_u8(msg, perm_algo);
694 ptr = msgb_put(msg, ei->key_len);
695 memcpy(ptr, ei->key, ei->key_len);
696
697 *tlv_len = (uint8_t) (msg->tail - old_tail);
698 return *tlv_len + 2;
699}
700
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200701/*! Decode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200702 * \param[out] ei Caller-provided memory to store encryption information
703 * \param[in] elem IE value to be decoded
704 * \param[in] len Length of \a elem in bytes
705 * \returns number of bytes parsed; negative on error */
Philipp Maier14e76b92017-03-28 18:36:52 +0200706int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei,
707 const uint8_t *elem, uint8_t len)
708{
709 uint8_t perm_algo;
710 unsigned int i;
711 unsigned int perm_algo_len = 0;
712 const uint8_t *old_elem = elem;
713
714 OSMO_ASSERT(ei);
715 if (!elem)
716 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200717 if (len == 0)
Philipp Maier14e76b92017-03-28 18:36:52 +0200718 return -EINVAL;
719
720 memset(ei, 0, sizeof(*ei));
721
722 perm_algo = *elem;
723 elem++;
724
725 for (i = 0; i < ENCRY_INFO_PERM_ALGO_MAXLEN; i++) {
726 if (perm_algo & (1 << i)) {
727 ei->perm_algo[perm_algo_len] = i + 1;
728 perm_algo_len++;
729 }
730 }
731 ei->perm_algo_len = perm_algo_len;
732
733 ei->key_len = len - 1;
734 memcpy(ei->key, elem, ei->key_len);
735 elem+=ei->key_len;
736
737 return (int)(elem - old_elem);
738}
Philipp Maier783047e2017-03-29 11:35:50 +0200739
Harald Weltee87693c2019-05-03 20:06:50 +0200740void gsm0808_msgb_put_cell_id_u(struct msgb *msg, enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
741{
742 switch (id_discr) {
743 case CELL_IDENT_WHOLE_GLOBAL: {
744 const struct osmo_cell_global_id *id = &u->global;
745 struct gsm48_loc_area_id lai;
746 gsm48_generate_lai2(&lai, &id->lai);
747 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
748 msgb_put_u16(msg, id->cell_identity);
749 break;
750 }
751 case CELL_IDENT_LAC_AND_CI: {
752 const struct osmo_lac_and_ci_id *id = &u->lac_and_ci;
753 msgb_put_u16(msg, id->lac);
754 msgb_put_u16(msg, id->ci);
755 break;
756 }
757 case CELL_IDENT_CI:
758 msgb_put_u16(msg, u->ci);
759 break;
760 case CELL_IDENT_LAI_AND_LAC: {
761 const struct osmo_location_area_id *id = &u->lai_and_lac;
762 struct gsm48_loc_area_id lai;
763 gsm48_generate_lai2(&lai, id);
764 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
765 break;
766 }
767 case CELL_IDENT_LAC:
768 msgb_put_u16(msg, u->lac);
769 break;
770 case CELL_IDENT_BSS:
771 case CELL_IDENT_NO_CELL:
772 /* Does not have any list items */
773 break;
774 default:
775 /* Support for other identifier list types is not implemented. */
776 OSMO_ASSERT(false);
777 }
778}
779
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200780/*! Encode TS 08.08 Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +0200781 * \param[out] msg Message Buffer to which IE is to be appended
782 * \param[in] cil Cell ID List to be encoded
783 * \returns number of bytes appended to \a msg */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100784uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg,
785 const struct gsm0808_cell_id_list2 *cil)
786{
787 uint8_t *old_tail;
788 uint8_t *tlv_len;
789 unsigned int i;
790
791 OSMO_ASSERT(msg);
792 OSMO_ASSERT(cil);
793
794 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
795 tlv_len = msgb_put(msg, 1);
796 old_tail = msg->tail;
797
798 msgb_put_u8(msg, cil->id_discr & 0x0f);
799
800 OSMO_ASSERT(cil->id_list_len <= GSM0808_CELL_ID_LIST2_MAXLEN)
Harald Weltee87693c2019-05-03 20:06:50 +0200801 for (i = 0; i < cil->id_list_len; i++)
802 gsm0808_msgb_put_cell_id_u(msg, cil->id_discr, &cil->id_list[i]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100803
804 *tlv_len = (uint8_t) (msg->tail - old_tail);
805 return *tlv_len + 2;
806}
807
808/*! DEPRECATED: Use gsm0808_enc_cell_id_list2 instead.
809 *
810 * Encode TS 08.08 Cell Identifier List IE
811 * \param[out] msg Message Buffer to which IE is to be appended
812 * \param[in] cil Cell ID List to be encoded
813 * \returns number of bytes appended to \a msg */
Philipp Maier783047e2017-03-29 11:35:50 +0200814uint8_t gsm0808_enc_cell_id_list(struct msgb *msg,
815 const struct gsm0808_cell_id_list *cil)
816{
817 uint8_t *old_tail;
818 uint8_t *tlv_len;
819 unsigned int i;
820
821 OSMO_ASSERT(msg);
822 OSMO_ASSERT(cil);
823
824 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
825 tlv_len = msgb_put(msg, 1);
826 old_tail = msg->tail;
827
828 msgb_put_u8(msg, cil->id_discr & 0x0f);
829
830 switch (cil->id_discr) {
831 case CELL_IDENT_LAC:
832 OSMO_ASSERT(cil->id_list_len <= CELL_ID_LIST_LAC_MAXLEN)
833 for (i=0;i<cil->id_list_len;i++) {
834 msgb_put_u16(msg, cil->id_list_lac[i]);
835 }
836 break;
837 case CELL_IDENT_BSS:
838 /* Does not have any list items */
839 break;
840 default:
841 /* FIXME: Implement support for all identifier list elements */
842 OSMO_ASSERT(false);
843 }
844
845 *tlv_len = (uint8_t) (msg->tail - old_tail);
846 return *tlv_len + 2;
847}
848
Stefan Sperling23381452018-03-15 19:38:15 +0100849/* Decode 5-byte LAI list element data (see TS 08.08 3.2.2.27) into MCC/MNC/LAC. */
850static void decode_lai(const uint8_t *data, struct osmo_location_area_id *decoded)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100851{
852 struct gsm48_loc_area_id lai;
853
Stefan Sperling23381452018-03-15 19:38:15 +0100854 /* Copy data to stack to prevent unaligned access in gsm48_decode_lai2(). */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100855 memcpy(&lai, data, sizeof(lai)); /* don't byte swap yet */
856
Stefan Sperling23381452018-03-15 19:38:15 +0100857 gsm48_decode_lai2(&lai, decoded);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100858}
859
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100860static 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 +0100861 size_t *consumed)
862{
863 struct osmo_cell_global_id *id;
864 uint16_t *ci_be;
865 size_t lai_offset;
866 int i = 0;
867 const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*ci_be);
868
869 *consumed = 0;
870 while (remain >= elemlen) {
871 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
872 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100873 id = &cil->id_list[i].global;
Stefan Sperling2873bf12018-03-14 18:38:41 +0100874 lai_offset = i * elemlen;
Stefan Sperling23381452018-03-15 19:38:15 +0100875 decode_lai(&data[lai_offset], &id->lai);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100876 ci_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]);
877 id->cell_identity = osmo_load16be(ci_be);
878 *consumed += elemlen;
879 remain -= elemlen;
880 i++;
881 }
882
883 return i;
884}
885
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100886static 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 +0100887 size_t *consumed)
888{
889 uint16_t *lacp_be, *ci_be;
890 struct osmo_lac_and_ci_id *id;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100891 int i = 0, j = 0;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100892 const size_t elemlen = sizeof(*lacp_be) + sizeof(*ci_be);
893
894 *consumed = 0;
895
896 if (remain < elemlen)
897 return -EINVAL;
898
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100899 lacp_be = (uint16_t *)(&data[j]);
900 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100901 while (remain >= elemlen) {
902 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
903 return -ENOSPC;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100904 id = &cil->id_list[i++].lac_and_ci;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100905 id->lac = osmo_load16be(lacp_be);
906 id->ci = osmo_load16be(ci_be);
907 *consumed += elemlen;
908 remain -= elemlen;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100909 j += elemlen;
910 lacp_be = (uint16_t *)(&data[j]);
911 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100912 }
913
914 return i;
915}
916
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100917static int parse_cell_id_ci_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain,
918 size_t *consumed)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100919{
920 const uint16_t *ci_be = (const uint16_t *)data;
921 int i = 0;
922 const size_t elemlen = sizeof(*ci_be);
923
924 *consumed = 0;
925 while (remain >= elemlen) {
926 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
927 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100928 cil->id_list[i++].ci = osmo_load16be(ci_be++);
Stefan Sperling9c62fc62018-03-16 10:23:34 +0100929 *consumed += elemlen;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100930 remain -= elemlen;
931 }
932 return i;
933}
934
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100935static 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 +0100936 size_t *consumed)
937{
938 struct osmo_location_area_id *id;
939 int i = 0;
940 const size_t elemlen = sizeof(struct gsm48_loc_area_id);
941
942 *consumed = 0;
943 while (remain >= elemlen) {
944 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
945 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100946 id = &cil->id_list[i].lai_and_lac;
Stefan Sperling23381452018-03-15 19:38:15 +0100947 decode_lai(&data[i * elemlen], id);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100948 *consumed += elemlen;
949 remain -= elemlen;
950 i++;
951 }
952
953 return i;
954}
955
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100956static 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 +0100957{
958 const uint16_t *lac_be = (const uint16_t *)data;
959 int i = 0;
960 const size_t elemlen = sizeof(*lac_be);
961
962 *consumed = 0;
963 while (remain >= elemlen) {
964 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
965 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100966 cil->id_list[i++].lac = osmo_load16be(lac_be++);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100967 *consumed += elemlen;
968 remain -= elemlen;
969 }
970 return i;
971}
972
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200973/*! Decode Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +0200974 * \param[out] cil Caller-provided memory to store Cell ID list
975 * \param[in] elem IE value to be decoded
976 * \param[in] len Length of \a elem in bytes
977 * \returns number of bytes parsed; negative on error */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100978int gsm0808_dec_cell_id_list2(struct gsm0808_cell_id_list2 *cil,
979 const uint8_t *elem, uint8_t len)
980{
981 uint8_t id_discr;
982 size_t bytes_elem = 0;
983 int list_len = 0;
984
985 OSMO_ASSERT(cil);
986 if (!elem)
987 return -EINVAL;
988 if (len == 0)
989 return -EINVAL;
990
991 memset(cil, 0, sizeof(*cil));
992
993 id_discr = *elem & 0x0f;
994 elem++;
995 len--;
996
997 switch (id_discr) {
998 case CELL_IDENT_WHOLE_GLOBAL:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100999 list_len = parse_cell_id_global_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001000 break;
1001 case CELL_IDENT_LAC_AND_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001002 list_len = parse_cell_id_lac_and_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001003 break;
1004 case CELL_IDENT_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001005 list_len = parse_cell_id_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001006 break;
1007 case CELL_IDENT_LAI_AND_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001008 list_len = parse_cell_id_lai_and_lac(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001009 break;
1010 case CELL_IDENT_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001011 list_len = parse_cell_id_lac_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001012 break;
1013 case CELL_IDENT_BSS:
1014 case CELL_IDENT_NO_CELL:
1015 /* Does not have any list items */
1016 break;
1017 default:
1018 /* Remaining cell identification types are not implemented. */
1019 return -EINVAL;
1020 }
1021
1022 if (list_len < 0) /* parsing error */
1023 return list_len;
1024
1025 cil->id_discr = id_discr;
1026 cil->id_list_len = list_len;
1027
1028 /* One byte for the cell ID discriminator + any remaining bytes in
1029 * the IE which were consumed by the parser functions above. */
1030 return 1 + (int)bytes_elem;
1031}
1032
1033/*! DEPRECATED: Use gsm0808_dec_cell_id_list2 instead.
1034 *
1035 * Decode Cell Identifier List IE
1036 * \param[out] cil Caller-provided memory to store Cell ID list
1037 * \param[in] elem IE value to be decoded
1038 * \param[in] len Length of \a elem in bytes
1039 * \returns number of bytes parsed; negative on error */
Philipp Maier783047e2017-03-29 11:35:50 +02001040int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil,
1041 const uint8_t *elem, uint8_t len)
1042{
1043 uint8_t id_discr;
1044 const uint8_t *old_elem = elem;
1045 unsigned int item_count = 0;
1046
1047 OSMO_ASSERT(cil);
1048 if (!elem)
1049 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +02001050 if (len == 0)
Philipp Maier783047e2017-03-29 11:35:50 +02001051 return -EINVAL;
1052
1053 memset(cil, 0, sizeof(*cil));
1054
1055 id_discr = *elem & 0x0f;
1056 elem++;
1057 len--;
1058
1059 cil->id_discr = id_discr;
1060
1061 switch (id_discr) {
1062 case CELL_IDENT_LAC:
1063 while (len >= 2) {
1064 cil->id_list_lac[item_count] = osmo_load16be(elem);
1065 elem += 2;
1066 item_count++;
1067 len -= 2;
1068 }
1069 case CELL_IDENT_BSS:
1070 /* Does not have any list items */
1071 break;
1072 default:
1073 /* FIXME: Implement support for all identifier list elements */
1074 return -EINVAL;
1075 }
1076
1077 cil->id_list_len = item_count;
1078 return (int)(elem - old_elem);
1079}
Harald Welte96e2a002017-06-12 21:44:18 +02001080
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001081static bool same_cell_id_list_entries(const struct gsm0808_cell_id_list2 *a, int ai,
1082 const struct gsm0808_cell_id_list2 *b, int bi)
1083{
1084 struct gsm0808_cell_id_list2 tmp = {
1085 .id_discr = a->id_discr,
1086 .id_list_len = 1,
1087 };
1088 uint8_t buf_a[32 + sizeof(struct msgb)];
1089 uint8_t buf_b[32 + sizeof(struct msgb)];
1090 struct msgb *msg_a = (void*)buf_a;
1091 struct msgb *msg_b = (void*)buf_b;
1092
1093 msg_a->data_len = 32;
1094 msg_b->data_len = 32;
1095 msgb_reset(msg_a);
1096 msgb_reset(msg_b);
1097
1098 if (a->id_discr != b->id_discr)
1099 return false;
1100 if (ai >= a->id_list_len
1101 || bi >= b->id_list_len)
1102 return false;
1103
1104 tmp.id_list[0] = a->id_list[ai];
1105 gsm0808_enc_cell_id_list2(msg_a, &tmp);
1106
1107 tmp.id_list[0] = b->id_list[bi];
1108 gsm0808_enc_cell_id_list2(msg_b, &tmp);
1109
1110 if (msg_a->len != msg_b->len)
1111 return false;
1112 if (memcmp(msg_a->data, msg_b->data, msg_a->len))
1113 return false;
1114
1115 return true;
1116}
1117
1118/*! Append entries from one Cell Identifier List to another.
1119 * The cell identifier types must be identical between the two lists.
1120 * \param dst[out] Append entries to this list.
1121 * \param src[in] Append these entries to \a dst.
1122 * \returns the nr of items added, or negative on error: -EINVAL if the id_discr mismatch
1123 * between the lists, -ENOSPC if the destination list does not have enough space. If an error is
1124 * returned, \a dst may have already been changed (particularly on -ENOSPC). Note that a return value
1125 * of zero may occur when the src->id_list_len is zero, or when all entries from \a src already exist
1126 * in \a dst, and does not indicate error per se. */
1127int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src)
1128{
1129 int i, j;
1130 int added = 0;
1131
1132 if (dst->id_list_len == 0
1133 && dst->id_discr != CELL_IDENT_BSS)
1134 dst->id_discr = src->id_discr;
1135 else if (dst->id_discr != src->id_discr)
1136 return -EINVAL;
1137
1138 for (i = 0; i < src->id_list_len; i++) {
1139 /* don't add duplicate entries */
1140 bool skip = false;
1141 for (j = 0; j < dst->id_list_len; j++) {
1142 if (same_cell_id_list_entries(dst, j, src, i)) {
1143 skip = true;
1144 break;
1145 }
1146 }
1147 if (skip)
1148 continue;
1149
1150 if (dst->id_list_len >= ARRAY_SIZE(dst->id_list))
1151 return -ENOSPC;
1152
1153 dst->id_list[dst->id_list_len++] = src->id_list[i];
1154 added ++;
1155 }
1156
1157 return added;
1158}
1159
Neels Hofmeyr38e58412018-05-25 16:56:35 +02001160/*! Convert a single Cell Identifier to a Cell Identifier List with one entry.
1161 * \param dst[out] Overwrite this list.
1162 * \param src[in] Set \a dst to contain exactly this item.
1163 */
1164void gsm0808_cell_id_to_list(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id *src)
1165{
1166 if (!dst)
1167 return;
1168 if (!src) {
1169 *dst = (struct gsm0808_cell_id_list2){
1170 .id_discr = CELL_IDENT_NO_CELL,
1171 };
1172 return;
1173 }
1174
1175 *dst = (struct gsm0808_cell_id_list2){
1176 .id_discr = src->id_discr,
1177 .id_list = { src->id },
1178 .id_list_len = 1,
1179 };
1180
1181 switch (src->id_discr) {
1182 case CELL_IDENT_NO_CELL:
1183 case CELL_IDENT_BSS:
1184 dst->id_list_len = 0;
1185 break;
1186 default:
1187 break;
1188 }
1189}
1190
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001191/*! Encode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1192 * \param[out] msg Message Buffer to which IE is to be appended
1193 * \param[in] ci Cell ID to be encoded
1194 * \returns number of bytes appended to \a msg */
1195uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci)
1196{
1197 uint8_t rc;
1198 uint8_t *ie_tag;
1199 struct gsm0808_cell_id_list2 cil = {
1200 .id_discr = ci->id_discr,
1201 .id_list = { ci->id },
1202 .id_list_len = 1,
1203 };
1204
1205 OSMO_ASSERT(msg);
1206 OSMO_ASSERT(ci);
1207
1208 ie_tag = msg->tail;
1209 rc = gsm0808_enc_cell_id_list2(msg, &cil);
1210
1211 if (rc <= 0)
1212 return rc;
1213
1214 *ie_tag = GSM0808_IE_CELL_IDENTIFIER;
1215 return rc;
1216}
1217
1218/*! Decode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1219 * \param[out] ci Caller-provided memory to store Cell ID.
1220 * \param[in] elem IE value to be decoded.
1221 * \param[in] len Length of \a elem in bytes.
1222 * \returns number of bytes parsed; negative on error */
1223int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len)
1224{
1225 struct gsm0808_cell_id_list2 cil;
1226 int rc;
1227 rc = gsm0808_dec_cell_id_list2(&cil, elem, len);
1228 if (rc < 0)
1229 return rc;
1230 if (cil.id_discr == CELL_IDENT_BSS || cil.id_discr == CELL_IDENT_NO_CELL) {
1231 if (cil.id_list_len != 0)
1232 return -EINVAL;
1233 } else {
1234 if (cil.id_list_len != 1)
1235 return -EINVAL;
1236 }
1237 ci->id_discr = cil.id_discr;
1238 ci->id = cil.id_list[0];
1239 return rc;
1240}
1241
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001242/*! Convert the representation of the permitted speech codec identifier
Philipp Maier3149b0d2017-06-02 13:22:34 +02001243 * that is used in struct gsm0808_channel_type to the speech codec
1244 * representation we use in struct gsm0808_speech_codec.
1245 * \param[in] perm_spch to be converted (see also gsm0808_permitted_speech)
1246 * \returns GSM speech codec type; negative on error */
1247int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch)
1248{
1249 /*! The speech codec type, which is used in the channel type field to
1250 * signal the permitted speech versions (codecs) has a different
1251 * encoding than the type field in the speech codec type element
1252 * (See also 3GPP TS 48.008, 3.2.2.11 and 3.2.2.103) */
1253
1254 switch (perm_spch) {
1255 case GSM0808_PERM_FR1:
1256 return GSM0808_SCT_FR1;
1257 case GSM0808_PERM_FR2:
1258 return GSM0808_SCT_FR2;
1259 case GSM0808_PERM_FR3:
1260 return GSM0808_SCT_FR3;
1261 case GSM0808_PERM_FR4:
1262 return GSM0808_SCT_FR4;
1263 case GSM0808_PERM_FR5:
1264 return GSM0808_SCT_FR5;
1265 case GSM0808_PERM_HR1:
1266 return GSM0808_SCT_HR1;
1267 case GSM0808_PERM_HR3:
1268 return GSM0808_SCT_HR3;
1269 case GSM0808_PERM_HR4:
1270 return GSM0808_SCT_HR4;
1271 case GSM0808_PERM_HR6:
1272 return GSM0808_SCT_HR6;
1273 }
1274
1275 /* Invalid input */
1276 return -EINVAL;
1277}
1278
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001279/*! Extrapolate a speech codec field from a given permitted speech
Philipp Maier884ba0f2017-06-02 13:49:16 +02001280 * parameter (channel type).
1281 * \param[out] sc Caller provided memory to store the resulting speech codec
1282 * \param[in] perm_spch value that is used to derive the speech codec info
1283 * (see also: enum gsm0808_speech_codec_type in gsm0808_utils.h)
1284 * \returns zero when successful; negative on error */
1285int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc,
1286 uint8_t perm_spch)
1287{
1288 int rc;
1289
1290 memset(sc, 0, sizeof(*sc));
1291
1292 /* Determine codec type */
1293 rc = gsm0808_chan_type_to_speech_codec(perm_spch);
1294 if (rc < 0)
1295 return -EINVAL;
1296 sc->type = (uint8_t) rc;
1297
1298 /* Depending on the speech codec type, pick a default codec
1299 * configuration that exactly matches the configuration on the
1300 * air interface. */
1301 switch (sc->type) {
1302 case GSM0808_SCT_FR3:
1303 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR;
1304 break;
1305 case GSM0808_SCT_FR4:
1306 sc->cfg = GSM0808_SC_CFG_DEFAULT_OFR_AMR_WB;
1307 break;
1308 case GSM0808_SCT_FR5:
1309 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR_WB;
1310 break;
1311 case GSM0808_SCT_HR3:
1312 sc->cfg = GSM0808_SC_CFG_DEFAULT_HR_AMR;
1313 break;
1314 case GSM0808_SCT_HR4:
1315 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR_WB;
1316 break;
1317 case GSM0808_SCT_HR6:
1318 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR;
1319 break;
1320 default:
1321 /* Note: Not all codec types specify a default setting,
1322 * in this case, we just set the field to zero. */
1323 sc->cfg = 0;
1324 }
1325
1326 /* Tag all codecs as "Full IP"
1327 * (see als 3GPP TS 48.008 3.2.2.103) */
1328 sc->fi = true;
1329
1330 return 0;
1331}
1332
Philipp Maier5f2eb152018-09-19 13:40:21 +02001333/*! Determine a set of AMR speech codec configuration bits (S0-S15) from a
1334 * given GSM 04.08 AMR configuration struct.
1335 * \param[in] cfg AMR configuration in GSM 04.08 format.
1336 * \param[in] hint if the resulting configuration shall be used with a FR or HR TCH.
1337 * \returns configuration bits (S0-S15) */
Philipp Maier369015c2018-09-21 09:07:20 +02001338uint16_t gsm0808_sc_cfg_from_gsm48_mr_cfg(const struct gsm48_multi_rate_conf *cfg,
Philipp Maier5f2eb152018-09-19 13:40:21 +02001339 bool fr)
1340{
1341 uint16_t s15_s0 = 0;
1342
1343 /* Check each rate bit in the AMR multirate configuration and pick the
1344 * matching default configuration as specified in 3GPP TS 28.062,
1345 * Table 7.11.3.1.3-2. */
1346 if (cfg->m4_75)
1347 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_4_75;
1348 if (cfg->m5_15)
1349 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_15;
1350 if (cfg->m5_90)
1351 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_90;
1352 if (cfg->m6_70)
1353 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_6_70;
1354 if (cfg->m7_40)
1355 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_40;
1356 if (cfg->m7_95)
1357 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_95;
1358 if (cfg->m10_2)
1359 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_10_2;
1360 if (cfg->m12_2)
1361 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_12_2;
1362
1363 /* Note: 3GPP TS 48.008, chapter 3GPP TS 48.008 states that for AMR
1364 * some of the configuration bits must be coded as zeros. The applied
1365 * bitmask matches the default codec settings. See also the definition
1366 * of enum gsm0808_speech_codec_defaults in gsm_08_08.h and
1367 * 3GPP TS 28.062, Table 7.11.3.1.3-2. */
1368 if (fr)
1369 s15_s0 &= GSM0808_SC_CFG_DEFAULT_FR_AMR;
1370 else
1371 s15_s0 &= GSM0808_SC_CFG_DEFAULT_HR_AMR;
1372
Philipp Maier94d79fd2019-03-01 10:40:48 +01001373 /* The mode that is encoded by S1 (Config-NB-Code = 1), takes a special
1374 * role as it does not stand for a single rate, but for up to four rates
1375 * at once (12.2, 7.4, 5.9, 4.75). We must check if the supplied cfg
1376 * covers this mode. If not, we need to make sure that the related
1377 * bit is removed. (See also 3GPP TS 28.062, Table 7.11.3.1.3-2) */
1378 if (!(cfg->m12_2 && cfg->m7_40 && cfg->m5_90 && cfg->m4_75) && fr)
1379 s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20;
1380 else if (!(cfg->m7_40 && cfg->m5_90 && cfg->m4_75))
1381 s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20;
1382
Philipp Maier5f2eb152018-09-19 13:40:21 +02001383 return s15_s0;
1384}
1385
Philipp Maier8515d032018-09-25 15:57:49 +02001386/*! Determine a GSM 04.08 AMR configuration struct from a set of speech codec
1387 * configuration bits (S0-S15)
1388 * \param[out] cfg AMR configuration in GSM 04.08 format.
Philipp Maier3713af82019-02-27 16:48:25 +01001389 * \param[in] s15_s0 configuration bits (S15-S0, non-ambiguous).
1390 * \returns zero when successful; negative on error */
1391int gsm48_mr_cfg_from_gsm0808_sc_cfg(struct gsm48_multi_rate_conf *cfg,
1392 uint16_t s15_s0)
Philipp Maier8515d032018-09-25 15:57:49 +02001393{
Philipp Maier3713af82019-02-27 16:48:25 +01001394 unsigned int count = 0;
1395
1396 /* Note: See also: 3GPP TS 28.062
1397 * Table 7.11.3.1.3-2: Preferred Configurations for the Adaptive
1398 * Multi-Rate Codec Types */
1399
1400 /* Note: The resulting multirate-configuration must not contain an
1401 * active set of more than four codec rates. The active set also
1402 * must contain at least one rate. */
1403
Philipp Maier8515d032018-09-25 15:57:49 +02001404 memset(cfg, 0, sizeof(*cfg));
Philipp Maier3713af82019-02-27 16:48:25 +01001405 cfg->ver = 1;
1406 cfg->icmi = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001407
1408 /* Strip option bits */
1409 s15_s0 &= 0x00ff;
1410
Philipp Maier3713af82019-02-27 16:48:25 +01001411 /* Rate 5,15k can never be selected (see table) */
1412 cfg->m5_15 = 0;
Philipp Maier8515d032018-09-25 15:57:49 +02001413
Philipp Maier3713af82019-02-27 16:48:25 +01001414 if (s15_s0 & GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20 & 0xff) {
1415 /* Table Table 7.11.3.1.3-2 lists one mode that selects 4
1416 * rates at once (Config-NB-Code = 1). The rates selected
1417 * are known to be compatible between GERAN and UTRAN, since
1418 * an active set must not contain more than four rates at
1419 * a time, we ignore all other settings as they are either
1420 * redundaned or excess settings (invalid) */
Philipp Maier8515d032018-09-25 15:57:49 +02001421 cfg->m4_75 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001422 cfg->m5_90 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001423 cfg->m7_40 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001424 cfg->m12_2 = 1;
Philipp Maier3713af82019-02-27 16:48:25 +01001425 count += 4;
1426 }
Philipp Maier8515d032018-09-25 15:57:49 +02001427
Philipp Maier3713af82019-02-27 16:48:25 +01001428 /* Check the bits in s15_s0 and set the flags for the
1429 * respective rates. */
1430 if (s15_s0 & GSM0808_SC_CFG_AMR_4_75 && !cfg->m4_75) {
1431 if (count >= 4)
1432 return -EINVAL;
1433 cfg->m4_75 = 1;
1434 count++;
1435 }
1436 if (s15_s0 & GSM0808_SC_CFG_AMR_5_90 && !cfg->m5_90) {
1437 if (count >= 4)
1438 return -EINVAL;
1439 cfg->m5_90 = 1;
1440 count++;
1441 }
1442 if (s15_s0 & GSM0808_SC_CFG_AMR_6_70) {
1443 if (count >= 4)
1444 return -EINVAL;
1445 cfg->m6_70 = 1;
1446 count++;
1447 }
1448 if (s15_s0 & GSM0808_SC_CFG_AMR_7_40 && !cfg->m7_40) {
1449 if (count >= 4)
1450 return -EINVAL;
1451 cfg->m7_40 = 1;
1452 count++;
1453 }
1454 if (s15_s0 & GSM0808_SC_CFG_AMR_7_95) {
1455 if (count >= 4)
1456 return -EINVAL;
1457 cfg->m7_95 = 1;
1458 count++;
1459 }
1460 if (s15_s0 & GSM0808_SC_CFG_AMR_10_2) {
1461 if (count >= 4)
1462 return -EINVAL;
1463 cfg->m10_2 = 1;
1464 count++;
1465 }
1466 if (s15_s0 & GSM0808_SC_CFG_AMR_12_2 && !cfg->m12_2) {
1467 if (count >= 4)
1468 return -EINVAL;
1469 cfg->m12_2 = 1;
1470 count++;
1471 }
1472
1473 if (count == 0)
1474 return -EINVAL;
1475
1476 return 0;
Philipp Maier8515d032018-09-25 15:57:49 +02001477}
1478
Maxed651d22018-11-07 15:25:05 +01001479int gsm0808_get_cipher_reject_cause(const struct tlv_parsed *tp)
1480{
1481 const uint8_t *buf = TLVP_VAL_MINLEN(tp, GSM0808_IE_CAUSE, 1);
1482
1483 if (!buf)
1484 return -EBADMSG;
1485
1486 if (TLVP_LEN(tp, GSM0808_IE_CAUSE) > 1) {
1487 if (!gsm0808_cause_ext(buf[0]))
1488 return -EINVAL;
1489 return buf[1];
1490 }
1491
1492 return buf[0];
1493}
1494
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001495/*! Print a human readable name of the cell identifier to the char buffer.
1496 * This is useful both for struct gsm0808_cell_id and struct gsm0808_cell_id_list2.
1497 * See also gsm0808_cell_id_name() and gsm0808_cell_id_list_name().
1498 * \param[out] buf Destination buffer to write string representation to.
1499 * \param[in] buflen Amount of memory available in \a buf.
1500 * \param[in] id_discr Cell Identifier type.
1501 * \param[in] u Cell Identifer value.
1502 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1503 * or that would have been written if the buffer were large enough.
1504 */
1505int gsm0808_cell_id_u_name(char *buf, size_t buflen,
1506 enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
1507{
1508 switch (id_discr) {
1509 case CELL_IDENT_LAC:
1510 return snprintf(buf, buflen, "%u", u->lac);
1511 case CELL_IDENT_CI:
1512 return snprintf(buf, buflen, "%u", u->ci);
1513 case CELL_IDENT_LAC_AND_CI:
1514 return snprintf(buf, buflen, "%u-%u", u->lac_and_ci.lac, u->lac_and_ci.ci);
1515 case CELL_IDENT_LAI_AND_LAC:
1516 return snprintf(buf, buflen, "%s", osmo_lai_name(&u->lai_and_lac));
1517 case CELL_IDENT_WHOLE_GLOBAL:
1518 return snprintf(buf, buflen, "%s", osmo_cgi_name(&u->global));
1519 default:
1520 /* For CELL_IDENT_BSS and CELL_IDENT_NO_CELL, just print the discriminator.
1521 * Same for kinds we have no string representation of yet. */
1522 return snprintf(buf, buflen, "%s", gsm0808_cell_id_discr_name(id_discr));
1523 }
1524}
1525
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02001526/* Store individual Cell Identifier information in a CGI, without clearing the remaining ones.
1527 * This is useful to supplement one CGI with information from more than one Cell Identifier,
1528 * which in turn is useful to match Cell Identifiers of differing kinds to each other.
1529 * Before first invocation, clear the *dst struct externally, this function does only write those members
1530 * that are present in parameter u.
1531 */
1532static void cell_id_to_cgi(struct osmo_cell_global_id *dst,
1533 enum CELL_IDENT discr, const union gsm0808_cell_id_u *u)
1534{
1535 switch (discr) {
1536 case CELL_IDENT_WHOLE_GLOBAL:
1537 *dst = u->global;
1538 return;
1539
1540 case CELL_IDENT_LAC_AND_CI:
1541 dst->lai.lac = u->lac_and_ci.lac;
1542 dst->cell_identity = u->lac_and_ci.ci;
1543 return;
1544
1545 case CELL_IDENT_CI:
1546 dst->cell_identity = u->ci;
1547 return;
1548
1549 case CELL_IDENT_LAI_AND_LAC:
1550 dst->lai = u->lai_and_lac;
1551 return;
1552
1553 case CELL_IDENT_LAC:
1554 dst->lai.lac = u->lac;
1555 return;
1556
1557 case CELL_IDENT_NO_CELL:
1558 case CELL_IDENT_BSS:
1559 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1560 case CELL_IDENT_UTRAN_RNC:
1561 case CELL_IDENT_UTRAN_LAC_RNC:
1562 /* No values to set. */
1563 return;
1564 }
1565}
1566
1567/*! Return true if the common information between the two Cell Identifiers match.
1568 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1569 * Note that CELL_IDENT_NO_CELL will always return false.
1570 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1571 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1572 * \param[in] discr1 Cell Identifier type.
1573 * \param[in] u1 Cell Identifier value.
1574 * \param[in] discr2 Other Cell Identifier type.
1575 * \param[in] u2 Other Cell Identifier value.
1576 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1577 * \returns True if the common fields of the above match.
1578 */
1579static bool gsm0808_cell_id_u_match(enum CELL_IDENT discr1, const union gsm0808_cell_id_u *u1,
1580 enum CELL_IDENT discr2, const union gsm0808_cell_id_u *u2,
1581 bool exact_match)
1582{
1583 struct osmo_cell_global_id a = {};
1584 struct osmo_cell_global_id b = {};
1585
1586 if (exact_match && discr1 != discr2)
1587 return false;
1588
1589 /* First handle the odd wildcard like CELL_IDENT kinds. We can't really match any of these. */
1590 switch (discr1) {
1591 case CELL_IDENT_NO_CELL:
1592 case CELL_IDENT_BSS:
1593 return discr1 == discr2;
1594 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1595 case CELL_IDENT_UTRAN_RNC:
1596 case CELL_IDENT_UTRAN_LAC_RNC:
1597 return false;
1598 default:
1599 break;
1600 }
1601 switch (discr2) {
1602 case CELL_IDENT_NO_CELL:
1603 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1604 case CELL_IDENT_UTRAN_RNC:
1605 case CELL_IDENT_UTRAN_LAC_RNC:
1606 case CELL_IDENT_BSS:
1607 return false;
1608 default:
1609 break;
1610 }
1611
1612 /* Enrich both sides to full CGI, then compare those. First set the *other* ID's values in case
1613 * they assign more items. For example:
1614 * u1 = LAC:42
1615 * u2 = LAC+CI:23+5
1616 * 1) a <- LAC+CI:23+5
1617 * 2) a <- LAC:42 so that a = LAC+CI:42+5
1618 * Now we can compare those two and find a mismatch. If the LAC were the same, we would get
1619 * identical LAC+CI and hence a match. */
1620
1621 cell_id_to_cgi(&a, discr2, u2);
1622 cell_id_to_cgi(&a, discr1, u1);
1623
1624 cell_id_to_cgi(&b, discr1, u1);
1625 cell_id_to_cgi(&b, discr2, u2);
1626
1627 return osmo_cgi_cmp(&a, &b) == 0;
1628}
1629
1630/*! Return true if the common information between the two Cell Identifiers match.
1631 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1632 * Note that CELL_IDENT_NO_CELL will always return false.
1633 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1634 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1635 * \param[in] id1 Cell Identifier.
1636 * \param[in] id2 Other Cell Identifier.
1637 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1638 * \returns True if the common fields of the above match.
1639 */
1640bool gsm0808_cell_ids_match(const struct gsm0808_cell_id *id1, const struct gsm0808_cell_id *id2, bool exact_match)
1641{
1642 return gsm0808_cell_id_u_match(id1->id_discr, &id1->id, id2->id_discr, &id2->id, exact_match);
1643}
1644
1645/*! Find an index in a Cell Identifier list that matches a given single Cell Identifer.
1646 * Compare \a id against each entry in \a list using gsm0808_cell_ids_match(), and return the list index
1647 * if a match is found. \a match_nr allows iterating all matches in the list. A match_nr <= 0 returns the
1648 * first match in the list, match_nr == 1 the second match, etc., and if match_nr exceeds the available
1649 * matches in the list, -1 is returned.
1650 * \param[in] id Cell Identifier to match.
1651 * \param[in] list Cell Identifier list to search in.
1652 * \param[in] match_nr Ignore this many matches.
1653 * \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 +01001654 * \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
1655 * entry).
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02001656 */
1657int gsm0808_cell_id_matches_list(const struct gsm0808_cell_id *id, const struct gsm0808_cell_id_list2 *list,
1658 unsigned int match_nr, bool exact_match)
1659{
1660 int i;
1661 for (i = 0; i < list->id_list_len; i++) {
1662 if (gsm0808_cell_id_u_match(id->id_discr, &id->id, list->id_discr, &list->id_list[i], exact_match)) {
1663 if (match_nr)
1664 match_nr--;
1665 else
1666 return i;
1667 }
1668 }
1669 return -1;
1670}
1671
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001672/*! Copy information from a CGI to form a Cell Identifier of the specified kind.
1673 * \param [out] cid Compose new Cell Identifier here.
1674 * \param [in] id_discr Which kind of Cell Identifier to compose.
1675 * \param [in] cgi Cell Global Identifier to form the Cell Identifier from.
1676 */
1677void gsm0808_cell_id_from_cgi(struct gsm0808_cell_id *cid, enum CELL_IDENT id_discr,
1678 const struct osmo_cell_global_id *cgi)
1679{
1680 *cid = (struct gsm0808_cell_id){
1681 .id_discr = id_discr,
1682 };
1683
1684 switch (id_discr) {
1685 case CELL_IDENT_WHOLE_GLOBAL:
1686 cid->id.global = *cgi;
1687 return;
1688
1689 case CELL_IDENT_LAC_AND_CI:
1690 cid->id.lac_and_ci = (struct osmo_lac_and_ci_id){
1691 .lac = cgi->lai.lac,
1692 .ci = cgi->cell_identity,
1693 };
1694 return;
1695
1696 case CELL_IDENT_CI:
1697 cid->id.ci = cgi->cell_identity;
1698 return;
1699
1700 case CELL_IDENT_LAI:
1701 cid->id.lai_and_lac = cgi->lai;
1702 return;
1703
1704 case CELL_IDENT_LAC:
1705 cid->id.lac = cgi->lai.lac;
1706 return;
1707
1708 case CELL_IDENT_NO_CELL:
1709 case CELL_IDENT_BSS:
1710 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1711 case CELL_IDENT_UTRAN_RNC:
1712 case CELL_IDENT_UTRAN_LAC_RNC:
1713 default:
1714 return;
1715 };
1716}
1717
1718/*! Overwrite parts of cgi with values from a Cell Identifier.
1719 * Place only those items given in cid into cgi, leaving other values unchanged.
1720 * \param[out] cgi Cell Global Identity to write to.
1721 * \param[in] cid Cell Identity to read from.
1722 * \return a bitmask of items that were set: OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI; 0 if nothing was
1723 * written to cgi.
1724 */
1725int gsm0808_cell_id_to_cgi(struct osmo_cell_global_id *cgi, const struct gsm0808_cell_id *cid)
1726{
1727 switch (cid->id_discr) {
1728 case CELL_IDENT_WHOLE_GLOBAL:
1729 *cgi = cid->id.global;
1730 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
1731
1732 case CELL_IDENT_LAC_AND_CI:
1733 cgi->lai.lac = cid->id.lac_and_ci.lac;
1734 cgi->cell_identity = cid->id.lac_and_ci.ci;
1735 return OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
1736
1737 case CELL_IDENT_CI:
1738 cgi->cell_identity = cid->id.ci;
1739 return OSMO_CGI_PART_CI;
1740
1741 case CELL_IDENT_LAI:
1742 cgi->lai = cid->id.lai_and_lac;
1743 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC;
1744
1745 case CELL_IDENT_LAC:
1746 cgi->lai.lac = cid->id.lac;
1747 return OSMO_CGI_PART_LAC;
1748
1749 case CELL_IDENT_NO_CELL:
1750 case CELL_IDENT_BSS:
1751 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1752 case CELL_IDENT_UTRAN_RNC:
1753 case CELL_IDENT_UTRAN_LAC_RNC:
1754 default:
1755 return 0;
1756 };
1757}
1758
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001759/*! value_string[] for enum CELL_IDENT. */
1760const struct value_string gsm0808_cell_id_discr_names[] = {
1761 { CELL_IDENT_WHOLE_GLOBAL, "CGI" },
1762 { CELL_IDENT_LAC_AND_CI, "LAC-CI" },
1763 { CELL_IDENT_CI, "CI" },
1764 { CELL_IDENT_NO_CELL, "NO-CELL" },
1765 { CELL_IDENT_LAI_AND_LAC, "LAI" },
1766 { CELL_IDENT_LAC, "LAC" },
1767 { CELL_IDENT_BSS, "BSS" },
1768 { CELL_IDENT_UTRAN_PLMN_LAC_RNC, "UTRAN-PLMN-LAC-RNC" },
1769 { CELL_IDENT_UTRAN_RNC, "UTRAN-RNC" },
1770 { CELL_IDENT_UTRAN_LAC_RNC, "UTRAN-LAC-RNC" },
1771 { 0, NULL }
1772};
1773
1774#define APPEND_THING(func, args...) do { \
1775 int remain = buflen - (pos - buf); \
1776 int l = func(pos, remain, ##args); \
1777 if (l < 0 || l > remain) \
1778 pos = buf + buflen; \
1779 else \
1780 pos += l; \
1781 if (l > 0) \
1782 total_len += l; \
1783 } while(0)
1784#define APPEND_STR(fmt, args...) APPEND_THING(snprintf, fmt, ##args)
1785#define APPEND_CELL_ID_U(DISCR, U) APPEND_THING(gsm0808_cell_id_u_name, DISCR, U)
1786
Harald Welte179f3572019-03-18 18:38:47 +01001787char *gsm0808_cell_id_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id *cid)
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001788{
1789 char *pos = buf;
1790 int total_len = 0;
1791 APPEND_STR("%s:", gsm0808_cell_id_discr_name(cid->id_discr));
1792 APPEND_CELL_ID_U(cid->id_discr, &cid->id);
1793 return buf;
1794}
1795
1796/*! Return a human readable representation of a Cell Identifier, like "LAC:123"
1797 * or "CGI:001-01-42-23".
1798 * \param[in] cid Cell Identifer.
1799 * \returns String in a static buffer.
1800 */
1801const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid)
1802{
1803 static char buf[64];
Harald Welte179f3572019-03-18 18:38:47 +01001804 return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001805}
1806
1807/*! Like gsm0808_cell_id_name() but uses a different static buffer.
1808 * \param[in] cid Cell Identifer.
1809 * \returns String in a static buffer.
1810 */
1811const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid)
1812{
1813 static char buf[64];
Harald Welte179f3572019-03-18 18:38:47 +01001814 return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
1815}
1816
1817char *gsm0808_cell_id_name_c(const void *ctx, const struct gsm0808_cell_id *cid)
1818{
1819 char *buf = talloc_size(ctx, 64);
1820 if (!buf)
1821 return NULL;
1822 return gsm0808_cell_id_name_buf(buf, 64, cid);
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001823}
1824
1825/*! Return a human readable representation of the Cell Identifier List, like
1826 * "LAC[2]:{123, 456}".
1827 * The return value semantics are like snprintf() and thus allow ensuring a complete
1828 * untruncated string by determining the required string length from the return value.
1829 * If buflen > 0, always nul-terminate the string in buf, also when it is truncated.
1830 * If buflen == 0, do not modify buf, just return the would-be length.
1831 * \param[out] buf Destination buffer to write string representation to.
1832 * \param[in] buflen Amount of memory available in \a buf.
1833 * \param[in] cil Cell Identifer List.
1834 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1835 * or that would have been written if the buffer were large enough.
1836 */
1837int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil)
1838{
1839 char *pos = buf;
1840 int total_len = 0;
1841 int i;
1842
1843 APPEND_STR("%s[%u]", gsm0808_cell_id_discr_name(cil->id_discr), cil->id_list_len);
1844
1845 switch (cil->id_discr) {
1846 case CELL_IDENT_BSS:
1847 case CELL_IDENT_NO_CELL:
1848 return total_len;
1849 default:
1850 break;
1851 }
1852
1853 APPEND_STR(":{");
1854
1855 for (i = 0; i < cil->id_list_len; i++) {
1856 if (i)
1857 APPEND_STR(", ");
1858 APPEND_CELL_ID_U(cil->id_discr, &cil->id_list[i]);
1859 }
1860
1861 APPEND_STR("}");
1862 return total_len;
1863}
1864
1865/*! Return a human-readable representation of \a cil in a static buffer.
1866 * If the list is too long, the output may be truncated.
1867 * See also gsm0808_cell_id_list_name_buf(). */
1868const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil)
1869{
1870 static char buf[1024];
1871 gsm0808_cell_id_list_name_buf(buf, sizeof(buf), cil);
1872 return buf;
1873}
1874
Harald Welte179f3572019-03-18 18:38:47 +01001875char *gsm0808_cell_id_list_name_c(const void *ctx, const struct gsm0808_cell_id_list2 *cil)
1876{
1877 char *buf = talloc_size(ctx, 1024);
1878 if (!buf)
1879 return NULL;
1880 gsm0808_cell_id_list_name_buf(buf, 1024, cil);
1881 return buf;
1882}
1883
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001884#undef APPEND_STR
1885#undef APPEND_CELL_ID_U
1886
Harald Welte4a62eda2019-03-18 18:27:00 +01001887char *gsm0808_channel_type_name_buf(char *buf, size_t buf_len, const struct gsm0808_channel_type *ct)
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02001888{
Harald Welte4a62eda2019-03-18 18:27:00 +01001889 snprintf(buf, buf_len, "ch_indctr=0x%x ch_rate_type=0x%x perm_spch=%s",
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02001890 ct->ch_indctr, ct->ch_rate_type,
1891 osmo_hexdump(ct->perm_spch, ct->perm_spch_len));
1892 return buf;
1893}
1894
Harald Welte4a62eda2019-03-18 18:27:00 +01001895const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct)
1896{
1897 static char buf[128];
1898 return gsm0808_channel_type_name_buf(buf, sizeof(buf), ct);
1899}
1900
Harald Welte179f3572019-03-18 18:38:47 +01001901char *gsm0808_channel_type_name_c(const void *ctx, const struct gsm0808_channel_type *ct)
1902{
1903 char *buf = talloc_size(ctx, 128);
1904 if (!buf)
1905 return NULL;
1906 return gsm0808_channel_type_name_buf(buf, 128, ct);
1907}
1908
Harald Welte96e2a002017-06-12 21:44:18 +02001909/*! @} */