blob: 54ec19c2c2f62f94d7dd70d043ca933608305b74 [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>
31#include <osmocom/gsm/gsm0808_utils.h>
Philipp Maier22401432017-03-24 17:59:26 +010032
33#define IP_V4_ADDR_LEN 4
34#define IP_V6_ADDR_LEN 16
35#define IP_PORT_LEN 2
36
Philipp Maiere0c65302017-03-28 17:05:40 +020037#define CHANNEL_TYPE_ELEMENT_MAXLEN 11
38#define CHANNEL_TYPE_ELEMENT_MINLEN 3
Philipp Maier14e76b92017-03-28 18:36:52 +020039#define ENCRYPT_INFO_ELEMENT_MINLEN 1
Philipp Maier6f725d62017-03-24 18:03:17 +010040
Harald Welte20725b92017-05-15 12:50:04 +020041#ifdef HAVE_SYS_SOCKET_H
42
43#include <sys/socket.h>
44#include <netinet/in.h>
Harald Welte96e2a002017-06-12 21:44:18 +020045
46/*! \addtogroup gsm0808
47 * @{
Harald Welte37b61652017-10-16 18:46:03 +020048 * \file gsm0808_utils.c
Harald Welte96e2a002017-06-12 21:44:18 +020049 */
50
Philipp Maier4f4905f2018-11-30 13:36:12 +010051/*! Encode TS 08.08 AoIP Cause IE
52 * \param[out] msg Message Buffer to which to append IE
53 * \param[in] cause Cause code to be used in IE
54 * \returns number of bytes added to \a msg */
55uint8_t gsm0808_enc_cause(struct msgb *msg, uint16_t cause)
56{
57 /* See also 3GPP TS 48.008 3.2.2.5 Cause */
58 uint8_t *old_tail;
59 bool extended;
60
61 old_tail = msg->tail;
62
63 extended = gsm0808_cause_ext(cause >> 8);
64
65 msgb_put_u8(msg, GSM0808_IE_CAUSE);
66 if (extended) {
67 msgb_put_u8(msg, 2);
68 msgb_put_u16(msg, cause);
69 } else {
70 msgb_put_u8(msg, 1);
71 msgb_put_u8(msg, (uint8_t) (cause & 0xFF));
72 }
73
74 return (uint8_t) (msg->tail - old_tail);
75}
76
Neels Hofmeyr87e45502017-06-20 00:17:59 +020077/*! Encode TS 08.08 AoIP transport address IE
Harald Welte96e2a002017-06-12 21:44:18 +020078 * \param[out] msg Message Buffer to which to append IE
79 * \param[in] ss Socket Address to be used in IE
80 * \returns number of bytes added to \a msg */
Philipp Maier22401432017-03-24 17:59:26 +010081uint8_t gsm0808_enc_aoip_trasp_addr(struct msgb *msg,
82 const struct sockaddr_storage *ss)
83{
84 /* See also 3GPP TS 48.008 3.2.2.102 AoIP Transport Layer Address */
85 struct sockaddr_in *sin;
86 struct sockaddr_in6 *sin6;
87 uint16_t port = 0;
88 uint8_t *ptr;
89 uint8_t *old_tail;
90 uint8_t *tlv_len;
91
92 OSMO_ASSERT(msg);
93 OSMO_ASSERT(ss);
94 OSMO_ASSERT(ss->ss_family == AF_INET || ss->ss_family == AF_INET6);
95
96 msgb_put_u8(msg, GSM0808_IE_AOIP_TRASP_ADDR);
97 tlv_len = msgb_put(msg,1);
98 old_tail = msg->tail;
99
100 switch (ss->ss_family) {
101 case AF_INET:
102 sin = (struct sockaddr_in *)ss;
Harald Welte95871da2017-05-15 12:11:36 +0200103 port = osmo_ntohs(sin->sin_port);
Philipp Maier22401432017-03-24 17:59:26 +0100104 ptr = msgb_put(msg, IP_V4_ADDR_LEN);
105 memcpy(ptr, &sin->sin_addr.s_addr, IP_V4_ADDR_LEN);
106 break;
107 case AF_INET6:
108 sin6 = (struct sockaddr_in6 *)ss;
Harald Welte95871da2017-05-15 12:11:36 +0200109 port = osmo_ntohs(sin6->sin6_port);
Philipp Maier22401432017-03-24 17:59:26 +0100110 ptr = msgb_put(msg, IP_V6_ADDR_LEN);
111 memcpy(ptr, sin6->sin6_addr.s6_addr, IP_V6_ADDR_LEN);
112 break;
113 }
114
115 msgb_put_u16(msg, port);
116
117 *tlv_len = (uint8_t) (msg->tail - old_tail);
118 return *tlv_len + 2;
119}
120
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200121/*! Decode TS 08.08 AoIP transport address IE
Harald Welte96e2a002017-06-12 21:44:18 +0200122 * \param[out] ss Caller-provided memory where decoded socket addr is stored
123 * \param[in] elem pointer to IE value
124 * \param[in] len length of \a elem in bytes
125 * \returns number of bytes parsed */
Philipp Maier22401432017-03-24 17:59:26 +0100126int gsm0808_dec_aoip_trasp_addr(struct sockaddr_storage *ss,
127 const uint8_t *elem, uint8_t len)
128{
129 /* See also 3GPP TS 48.008 3.2.2.102 AoIP Transport Layer Address */
130 struct sockaddr_in sin;
131 struct sockaddr_in6 sin6;
132 const uint8_t *old_elem = elem;
133
134 OSMO_ASSERT(ss);
135 if (!elem)
136 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200137 if (len == 0)
Philipp Maier22401432017-03-24 17:59:26 +0100138 return -EINVAL;
139
140 memset(ss, 0, sizeof(*ss));
141
142 switch (len) {
143 case IP_V4_ADDR_LEN + IP_PORT_LEN:
144 memset(&sin, 0, sizeof(sin));
145 sin.sin_family = AF_INET;
146
147 memcpy(&sin.sin_addr.s_addr, elem, IP_V4_ADDR_LEN);
148 elem += IP_V4_ADDR_LEN;
149 sin.sin_port = osmo_load16le(elem);
150 elem += IP_PORT_LEN;
151
152 memcpy(ss, &sin, sizeof(sin));
153 break;
154 case IP_V6_ADDR_LEN + IP_PORT_LEN:
155 memset(&sin6, 0, sizeof(sin6));
156 sin6.sin6_family = AF_INET6;
157
158 memcpy(sin6.sin6_addr.s6_addr, elem, IP_V6_ADDR_LEN);
159 elem += IP_V6_ADDR_LEN;
160 sin6.sin6_port = osmo_load16le(elem);
161 elem += IP_PORT_LEN;
162
163 memcpy(ss, &sin6, sizeof(sin6));
164 break;
165 default:
166 /* Malformed element! */
167 return -EINVAL;
168 break;
169 }
170
171 return (int)(elem - old_elem);
172}
Philipp Maier6f725d62017-03-24 18:03:17 +0100173
Harald Welte20725b92017-05-15 12:50:04 +0200174#endif /* HAVE_SYS_SOCKET_H */
175
Philipp Maier6f725d62017-03-24 18:03:17 +0100176/* Helper function for gsm0808_enc_speech_codec()
177 * and gsm0808_enc_speech_codec_list() */
178static uint8_t enc_speech_codec(struct msgb *msg,
179 const struct gsm0808_speech_codec *sc)
180{
181 /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
182 uint8_t header = 0;
183 uint8_t *old_tail;
Harald Welte459a1802018-06-28 09:24:17 +0200184 bool type_extended = false;
Philipp Maierbb839662017-06-01 17:11:19 +0200185
186 /* Note: Extended codec types are codec types that require 8 instead
187 * of 4 bit to fully specify the selected codec. In the following,
188 * we check if we work with an extended type or not. We also check
189 * if the codec type is valid at all. */
190 switch(sc->type) {
191 case GSM0808_SCT_FR1:
192 case GSM0808_SCT_FR2:
193 case GSM0808_SCT_FR3:
194 case GSM0808_SCT_FR4:
195 case GSM0808_SCT_FR5:
196 case GSM0808_SCT_HR1:
197 case GSM0808_SCT_HR3:
198 case GSM0808_SCT_HR4:
199 case GSM0808_SCT_HR6:
200 type_extended = false;
201 break;
202 case GSM0808_SCT_CSD:
203 type_extended = true;
204 break;
205 default:
206 /* Invalid codec type specified */
207 OSMO_ASSERT(false);
208 break;
209 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100210
211 old_tail = msg->tail;
212
213 if (sc->fi)
214 header |= (1 << 7);
215 if (sc->pi)
216 header |= (1 << 6);
217 if (sc->pt)
218 header |= (1 << 5);
219 if (sc->tf)
220 header |= (1 << 4);
Philipp Maierbb839662017-06-01 17:11:19 +0200221
222 if (type_extended) {
Philipp Maier6f725d62017-03-24 18:03:17 +0100223 header |= 0x0f;
224 msgb_put_u8(msg, header);
Philipp Maierbb839662017-06-01 17:11:19 +0200225 msgb_put_u8(msg, sc->type);
Philipp Maier6f725d62017-03-24 18:03:17 +0100226 } else {
227 OSMO_ASSERT(sc->type < 0x0f);
228 header |= sc->type;
229 msgb_put_u8(msg, header);
Philipp Maier6f725d62017-03-24 18:03:17 +0100230 }
231
Philipp Maierbb839662017-06-01 17:11:19 +0200232 /* Note: Whether a configuration is present or not depends on the
233 * selected codec type. If present, it can either consist of one
234 * or two octets, depending on the codec type */
235 switch (sc->type) {
236 case GSM0808_SCT_FR3:
237 case GSM0808_SCT_HR3:
238 case GSM0808_SCT_HR6:
Philipp Maier7e27b142018-03-22 17:26:46 +0100239 msgb_put_u16(msg, osmo_ntohs(sc->cfg));
Philipp Maierbb839662017-06-01 17:11:19 +0200240 break;
241 case GSM0808_SCT_FR4:
242 case GSM0808_SCT_FR5:
243 case GSM0808_SCT_HR4:
244 case GSM0808_SCT_CSD:
245 OSMO_ASSERT((sc->cfg & 0xff00) == 0)
246 msgb_put_u8(msg, (uint8_t) sc->cfg & 0xff);
247 break;
248 default:
249 OSMO_ASSERT(sc->cfg == 0);
250 break;
251 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100252
253 return (uint8_t) (msg->tail - old_tail);
254}
255
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200256/*! Encode TS 08.08 Speech Codec IE
Harald Welte96e2a002017-06-12 21:44:18 +0200257 * \param[out] msg Message Buffer to which IE will be appended
258 * \param[in] sc Speech Codec to be encoded into IE
259 * \returns number of bytes appended to \a msg */
Philipp Maier6f725d62017-03-24 18:03:17 +0100260uint8_t gsm0808_enc_speech_codec(struct msgb *msg,
261 const struct gsm0808_speech_codec *sc)
262{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200263 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100264 uint8_t *old_tail;
265 uint8_t *tlv_len;
266
267 OSMO_ASSERT(msg);
268 OSMO_ASSERT(sc);
269
270 msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC);
271 tlv_len = msgb_put(msg, 1);
272 old_tail = msg->tail;
273
274 enc_speech_codec(msg, sc);
275
276 *tlv_len = (uint8_t) (msg->tail - old_tail);
277 return *tlv_len + 2;
278}
279
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200280/*! Decode TS 08.08 Speech Codec IE
Harald Welte96e2a002017-06-12 21:44:18 +0200281 * \param[out] sc Caller-allocated memory for Speech Codec
282 * \param[in] elem IE value to be decoded
283 * \param[in] len Length of \a elem in bytes
284 * \returns number of bytes parsed; negative on error */
Philipp Maier6f725d62017-03-24 18:03:17 +0100285int gsm0808_dec_speech_codec(struct gsm0808_speech_codec *sc,
286 const uint8_t *elem, uint8_t len)
287{
288 /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
289 uint8_t header;
290 const uint8_t *old_elem = elem;
291
292 OSMO_ASSERT(sc);
293 if (!elem)
294 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200295 if (len == 0)
Philipp Maier6f725d62017-03-24 18:03:17 +0100296 return -EINVAL;
297
298 memset(sc, 0, sizeof(*sc));
299
300 header = *elem;
301
Philipp Maier85a6af22017-04-28 10:55:05 +0200302 /* An extended codec type needs at least two fields,
303 * bail if the input data length is not sufficient. */
Philipp Maier6f725d62017-03-24 18:03:17 +0100304 if ((header & 0x0F) == 0x0F && len < 2)
305 return -EINVAL;
Philipp Maier6f725d62017-03-24 18:03:17 +0100306
307 elem++;
308 len--;
309
310 if (header & (1 << 7))
311 sc->fi = true;
312 if (header & (1 << 6))
313 sc->pi = true;
314 if (header & (1 << 5))
315 sc->pt = true;
316 if (header & (1 << 4))
317 sc->tf = true;
318
319 if ((header & 0x0F) != 0x0F) {
320 sc->type = (header & 0x0F);
Philipp Maierbb839662017-06-01 17:11:19 +0200321 } else {
322 sc->type = *elem;
323 elem++;
324 len--;
Philipp Maier6f725d62017-03-24 18:03:17 +0100325 }
326
Philipp Maierbb839662017-06-01 17:11:19 +0200327 /* Note: Whether a configuration is present or not depends on the
328 * selected codec type. If present, it can either consist of one or
329 * two octets depending on the codec type */
330 switch (sc->type) {
331 case GSM0808_SCT_FR1:
332 case GSM0808_SCT_FR2:
333 case GSM0808_SCT_HR1:
334 break;
335 case GSM0808_SCT_HR4:
336 case GSM0808_SCT_CSD:
337 case GSM0808_SCT_FR4:
338 case GSM0808_SCT_FR5:
339 if (len < 1)
340 return -EINVAL;
341 sc->cfg = *elem;
342 elem++;
343 break;
344 case GSM0808_SCT_FR3:
345 case GSM0808_SCT_HR3:
346 case GSM0808_SCT_HR6:
347 if (len < 2)
348 return -EINVAL;
Philipp Maier7e27b142018-03-22 17:26:46 +0100349 sc->cfg = osmo_load16le(elem);
Philipp Maierbb839662017-06-01 17:11:19 +0200350 elem += 2;
351 break;
352 default:
353 /* Invalid codec type => malformed speech codec element! */
354 return -EINVAL;
355 break;
356 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100357
358 return (int)(elem - old_elem);
359}
360
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200361/*! Encode TS 08.08 Speech Codec list
Harald Welte96e2a002017-06-12 21:44:18 +0200362 * \param[out] msg Message Buffer to which IE is to be appended
363 * \param[in] scl Speech Codec List to be encoded into IE
364 * \returns number of bytes added to \a msg */
Philipp Maier6f725d62017-03-24 18:03:17 +0100365uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg,
366 const struct gsm0808_speech_codec_list *scl)
367{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200368 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100369 uint8_t *old_tail;
370 uint8_t *tlv_len;
371 unsigned int i;
372 uint8_t rc;
373 unsigned int bytes_used = 0;
374
375 OSMO_ASSERT(msg);
376 OSMO_ASSERT(scl);
377
Philipp Maier6f725d62017-03-24 18:03:17 +0100378 msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC_LIST);
379 tlv_len = msgb_put(msg, 1);
380 old_tail = msg->tail;
381
382 for (i = 0; i < scl->len; i++) {
383 rc = enc_speech_codec(msg, &scl->codec[i]);
384 OSMO_ASSERT(rc >= 1);
385 bytes_used += rc;
386 OSMO_ASSERT(bytes_used <= 255);
387 }
388
389 *tlv_len = (uint8_t) (msg->tail - old_tail);
390 return *tlv_len + 2;
391}
392
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200393/*! Decode TS 08.08 Speech Codec list IE
Harald Welte96e2a002017-06-12 21:44:18 +0200394 * \param[out] scl Caller-provided memory to store codec list
395 * \param[in] elem IE value to be decoded
396 * \param[in] len Length of \a elem in bytes
397 * \returns number of bytes parsed; negative on error */
Philipp Maier6f725d62017-03-24 18:03:17 +0100398int gsm0808_dec_speech_codec_list(struct gsm0808_speech_codec_list *scl,
399 const uint8_t *elem, uint8_t len)
400{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200401 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100402 const uint8_t *old_elem = elem;
403 unsigned int i;
404 int rc;
405 uint8_t decoded = 0;
406
407 OSMO_ASSERT(scl);
408 if (!elem)
409 return -EINVAL;
Philipp Maier6f725d62017-03-24 18:03:17 +0100410
411 memset(scl, 0, sizeof(*scl));
412
413 for (i = 0; i < ARRAY_SIZE(scl->codec); i++) {
414 if (len <= 0)
415 break;
416
417 rc = gsm0808_dec_speech_codec(&scl->codec[i], elem, len);
418 if (rc < 1)
419 return -EINVAL;
420
421 elem+=rc;
422 len -= rc;
423 decoded++;
424 }
425
426 scl->len = decoded;
427
Philipp Maier6f725d62017-03-24 18:03:17 +0100428 return (int)(elem - old_elem);
429}
Philipp Maiere0c65302017-03-28 17:05:40 +0200430
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200431/*! Encode TS 08.08 Channel Type IE
Harald Welte96e2a002017-06-12 21:44:18 +0200432 * \param[out] msg Message Buffer to which IE is to be appended
433 * \param[in] ct Channel Type to be encoded
434 * \returns number of bytes added to \a msg */
Philipp Maiere0c65302017-03-28 17:05:40 +0200435uint8_t gsm0808_enc_channel_type(struct msgb *msg,
436 const struct gsm0808_channel_type *ct)
437{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200438 /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */
Philipp Maiere0c65302017-03-28 17:05:40 +0200439 unsigned int i;
440 uint8_t byte;
441 uint8_t *old_tail;
442 uint8_t *tlv_len;
443
444 OSMO_ASSERT(msg);
445 OSMO_ASSERT(ct);
446 OSMO_ASSERT(ct->perm_spch_len <= CHANNEL_TYPE_ELEMENT_MAXLEN - 2);
447
448 /* FIXME: Implement encoding support for Data
449 * and Speech + CTM Text Telephony */
450 if ((ct->ch_indctr & 0x0f) != GSM0808_CHAN_SPEECH
451 && (ct->ch_indctr & 0x0f) != GSM0808_CHAN_SIGN)
452 OSMO_ASSERT(false);
453
454 msgb_put_u8(msg, GSM0808_IE_CHANNEL_TYPE);
455 tlv_len = msgb_put(msg, 1);
456 old_tail = msg->tail;
457
458 msgb_put_u8(msg, ct->ch_indctr & 0x0f);
459 msgb_put_u8(msg, ct->ch_rate_type);
460
461 for (i = 0; i < ct->perm_spch_len; i++) {
462 byte = ct->perm_spch[i];
463
464 if (i < ct->perm_spch_len - 1)
465 byte |= 0x80;
466 msgb_put_u8(msg, byte);
467 }
468
469 *tlv_len = (uint8_t) (msg->tail - old_tail);
470 return *tlv_len + 2;
471}
472
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200473/*! Decode TS 08.08 Channel Type IE
Harald Welte96e2a002017-06-12 21:44:18 +0200474 * \param[out] ct Caller-provided memory to store channel type
475 * \param[in] elem IE Value to be decoded
476 * \param[in] len Length of \a elem in bytes
477 * \returns number of bytes parsed; negative on error */
Philipp Maiere0c65302017-03-28 17:05:40 +0200478int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct,
479 const uint8_t *elem, uint8_t len)
480{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200481 /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */
Philipp Maiere0c65302017-03-28 17:05:40 +0200482 unsigned int i;
483 uint8_t byte;
484 const uint8_t *old_elem = elem;
485
486 OSMO_ASSERT(ct);
487 if (!elem)
488 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200489 if (len < 3 || len > 11)
Philipp Maiere0c65302017-03-28 17:05:40 +0200490 return -EINVAL;
491
492 memset(ct, 0, sizeof(*ct));
493
494 ct->ch_indctr = (*elem) & 0x0f;
495 elem++;
496 ct->ch_rate_type = (*elem) & 0x0f;
497 elem++;
498
499 for (i = 0; i < ARRAY_SIZE(ct->perm_spch); i++) {
500 byte = *elem;
501 elem++;
502 ct->perm_spch[i] = byte & 0x7f;
503 if ((byte & 0x80) == 0x00)
504 break;
505 }
506 ct->perm_spch_len = i + 1;
507
508 return (int)(elem - old_elem);
509}
Philipp Maier14e76b92017-03-28 18:36:52 +0200510
Max969fb2e2018-12-10 11:01:10 +0100511/*! Create BSSMAP Global Call Reference, 3GPP TS 48.008 §3.2.2.115.
512 * \param[out] msg Message Buffer for appending IE
513 * \param[in] g Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1
514 * \returns number of bytes added to \a msg or 0 on error */
Max47022152018-12-19 18:51:00 +0100515static uint8_t gsm0808_enc_gcr(struct msgb *msg, const struct osmo_gcr_parsed *g)
Max969fb2e2018-12-10 11:01:10 +0100516{
517 uint8_t enc, *len = msgb_tl_put(msg, GSM0808_IE_GLOBAL_CALL_REF);
518
519 enc = osmo_enc_gcr(msg, g);
520 if (!enc)
521 return 0;
522
523 *len = enc;
524 return enc + 2; /* type (1 byte) + length (1 byte) */
525}
526
527/*! Decode BSSMAP Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1.
528 * \param[out] gcr Caller-provided memory to store Global Call Reference
Max036012b2018-12-19 17:48:56 +0100529 * \param[in] tp IE values to be decoded
Max969fb2e2018-12-10 11:01:10 +0100530 * \returns number of bytes parsed; negative on error */
Max47022152018-12-19 18:51:00 +0100531static int gsm0808_dec_gcr(struct osmo_gcr_parsed *gcr, const struct tlv_parsed *tp)
Max969fb2e2018-12-10 11:01:10 +0100532{
533 int ret;
534 const uint8_t *buf = TLVP_VAL_MINLEN(tp, GSM0808_IE_GLOBAL_CALL_REF, OSMO_GCR_MIN_LEN);
535 if (!buf)
536 return -EINVAL;
537
538 ret = osmo_dec_gcr(gcr, buf, TLVP_LEN(tp, GSM0808_IE_GLOBAL_CALL_REF));
539 if (ret < 0)
540 return -ENOENT;
541
542 return 2 + ret;
543}
544
Max47022152018-12-19 18:51:00 +0100545/*! Add LCLS parameters to a given msgb, 3GPP TS 48.008 §3.2.2.115 - 3.2.2.120.
546 * \param[out] msg Message Buffer for appending IE
547 * \param[in] lcls LCLS-related data
548 * \returns number of bytes added to \a msg or 0 on error */
549uint8_t gsm0808_enc_lcls(struct msgb *msg, const struct osmo_lcls *lcls)
550{
551 uint8_t enc = 0;
552
553 /* LCLS: §3.2.2.115 Global Call Reference */
Max3b901252019-01-15 14:15:11 +0100554 if (lcls->gcr_available)
555 enc = gsm0808_enc_gcr(msg, &lcls->gcr);
Max47022152018-12-19 18:51:00 +0100556
557 /* LCLS: §3.2.2.116 Configuration */
558 if (lcls->config != GSM0808_LCLS_CFG_NA) {
559 msgb_tv_put(msg, GSM0808_IE_LCLS_CONFIG, lcls->config);
560 enc += 2;
561 }
562
563 /* LCLS: §3.2.2.117 Connection Status Control */
564 if (lcls->control != GSM0808_LCLS_CSC_NA) {
565 msgb_tv_put(msg, GSM0808_IE_LCLS_CONN_STATUS_CTRL, lcls->control);
566 enc += 2;
567 }
568
569 /* LCLS: §3.2.2.118 Correlation-Not-Needed */
570 if (!lcls->corr_needed) {
571 msgb_v_put(msg, GSM0808_IE_LCLS_CORR_NOT_NEEDED);
572 enc++;
573 }
574
575 return enc;
576}
577
578/*! Decode LCLS parameters to a given msgb, 3GPP TS 48.008 §3.2.2.115 - 3.2.2.120.
579 * \param[out] lcls Caller-provided memory to store LCLS-related data
580 * \param[in] tp IE values to be decoded
581 * \returns GCR size or negative on error */
582int gsm0808_dec_lcls(struct osmo_lcls *lcls, const struct tlv_parsed *tp)
583{
Max3b901252019-01-15 14:15:11 +0100584 int ret = gsm0808_dec_gcr(&lcls->gcr, tp);
Max47022152018-12-19 18:51:00 +0100585
Max3b901252019-01-15 14:15:11 +0100586 lcls->gcr_available = (ret < 0) ? false : true;
Max47022152018-12-19 18:51:00 +0100587 lcls->config = tlvp_val8(tp, GSM0808_IE_LCLS_CONFIG, GSM0808_LCLS_CFG_NA);
588 lcls->control = tlvp_val8(tp, GSM0808_IE_LCLS_CONN_STATUS_CTRL, GSM0808_LCLS_CSC_NA);
589 lcls->corr_needed = TLVP_PRESENT(tp, GSM0808_IE_LCLS_CORR_NOT_NEEDED) ? false : true;
590
591 return ret;
592}
593
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200594/*! Encode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200595 * \param[out] msg Message Buffer to which IE is to be appended
596 * \param[in] ei Encryption Information to be encoded
597 * \returns number of bytes appended to \a msg */
Philipp Maier14e76b92017-03-28 18:36:52 +0200598uint8_t gsm0808_enc_encrypt_info(struct msgb *msg,
599 const struct gsm0808_encrypt_info *ei)
600{
601 unsigned int i;
602 uint8_t perm_algo = 0;
603 uint8_t *ptr;
604 uint8_t *old_tail;
605 uint8_t *tlv_len;
606
607 OSMO_ASSERT(msg);
608 OSMO_ASSERT(ei);
609 OSMO_ASSERT(ei->key_len <= ARRAY_SIZE(ei->key));
610 OSMO_ASSERT(ei->perm_algo_len <= ENCRY_INFO_PERM_ALGO_MAXLEN);
611
612 msgb_put_u8(msg, GSM0808_IE_ENCRYPTION_INFORMATION);
613 tlv_len = msgb_put(msg, 1);
614 old_tail = msg->tail;
615
616 for (i = 0; i < ei->perm_algo_len; i++) {
617 /* Note: gsm_08_08.h defines the permitted algorithms
618 * as an enum which ranges from 0x01 to 0x08 */
619 OSMO_ASSERT(ei->perm_algo[i] != 0);
620 OSMO_ASSERT(ei->perm_algo[i] <= ENCRY_INFO_PERM_ALGO_MAXLEN);
621 perm_algo |= (1 << (ei->perm_algo[i] - 1));
622 }
623
624 msgb_put_u8(msg, perm_algo);
625 ptr = msgb_put(msg, ei->key_len);
626 memcpy(ptr, ei->key, ei->key_len);
627
628 *tlv_len = (uint8_t) (msg->tail - old_tail);
629 return *tlv_len + 2;
630}
631
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200632/*! Decode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200633 * \param[out] ei Caller-provided memory to store encryption information
634 * \param[in] elem IE value to be decoded
635 * \param[in] len Length of \a elem in bytes
636 * \returns number of bytes parsed; negative on error */
Philipp Maier14e76b92017-03-28 18:36:52 +0200637int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei,
638 const uint8_t *elem, uint8_t len)
639{
640 uint8_t perm_algo;
641 unsigned int i;
642 unsigned int perm_algo_len = 0;
643 const uint8_t *old_elem = elem;
644
645 OSMO_ASSERT(ei);
646 if (!elem)
647 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200648 if (len == 0)
Philipp Maier14e76b92017-03-28 18:36:52 +0200649 return -EINVAL;
650
651 memset(ei, 0, sizeof(*ei));
652
653 perm_algo = *elem;
654 elem++;
655
656 for (i = 0; i < ENCRY_INFO_PERM_ALGO_MAXLEN; i++) {
657 if (perm_algo & (1 << i)) {
658 ei->perm_algo[perm_algo_len] = i + 1;
659 perm_algo_len++;
660 }
661 }
662 ei->perm_algo_len = perm_algo_len;
663
664 ei->key_len = len - 1;
665 memcpy(ei->key, elem, ei->key_len);
666 elem+=ei->key_len;
667
668 return (int)(elem - old_elem);
669}
Philipp Maier783047e2017-03-29 11:35:50 +0200670
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200671/*! Encode TS 08.08 Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +0200672 * \param[out] msg Message Buffer to which IE is to be appended
673 * \param[in] cil Cell ID List to be encoded
674 * \returns number of bytes appended to \a msg */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100675uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg,
676 const struct gsm0808_cell_id_list2 *cil)
677{
678 uint8_t *old_tail;
679 uint8_t *tlv_len;
680 unsigned int i;
681
682 OSMO_ASSERT(msg);
683 OSMO_ASSERT(cil);
684
685 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
686 tlv_len = msgb_put(msg, 1);
687 old_tail = msg->tail;
688
689 msgb_put_u8(msg, cil->id_discr & 0x0f);
690
691 OSMO_ASSERT(cil->id_list_len <= GSM0808_CELL_ID_LIST2_MAXLEN)
692 switch (cil->id_discr) {
693 case CELL_IDENT_WHOLE_GLOBAL:
694 for (i = 0; i < cil->id_list_len; i++) {
695 const struct osmo_cell_global_id *id = &cil->id_list[i].global;
696 struct gsm48_loc_area_id lai;
Neels Hofmeyr8b8cd932018-03-23 01:47:37 +0100697 gsm48_generate_lai2(&lai, &id->lai);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100698 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
699 msgb_put_u16(msg, id->cell_identity);
700 }
701 break;
702 case CELL_IDENT_LAC_AND_CI:
703 for (i = 0; i < cil->id_list_len; i++) {
704 const struct osmo_lac_and_ci_id *id = &cil->id_list[i].lac_and_ci;
705 msgb_put_u16(msg, id->lac);
706 msgb_put_u16(msg, id->ci);
707 }
708 break;
709 case CELL_IDENT_CI:
710 for (i = 0; i < cil->id_list_len; i++)
711 msgb_put_u16(msg, cil->id_list[i].ci);
712 break;
713 case CELL_IDENT_LAI_AND_LAC:
714 for (i = 0; i < cil->id_list_len; i++) {
715 const struct osmo_location_area_id *id = &cil->id_list[i].lai_and_lac;
716 struct gsm48_loc_area_id lai;
Neels Hofmeyr8b8cd932018-03-23 01:47:37 +0100717 gsm48_generate_lai2(&lai, id);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100718 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
719 }
720 break;
721 case CELL_IDENT_LAC:
722 for (i = 0; i < cil->id_list_len; i++)
723 msgb_put_u16(msg, cil->id_list[i].lac);
724 break;
725 case CELL_IDENT_BSS:
726 case CELL_IDENT_NO_CELL:
727 /* Does not have any list items */
728 break;
729 default:
730 /* Support for other identifier list types is not implemented. */
731 OSMO_ASSERT(false);
732 }
733
734 *tlv_len = (uint8_t) (msg->tail - old_tail);
735 return *tlv_len + 2;
736}
737
738/*! DEPRECATED: Use gsm0808_enc_cell_id_list2 instead.
739 *
740 * Encode TS 08.08 Cell Identifier List IE
741 * \param[out] msg Message Buffer to which IE is to be appended
742 * \param[in] cil Cell ID List to be encoded
743 * \returns number of bytes appended to \a msg */
Philipp Maier783047e2017-03-29 11:35:50 +0200744uint8_t gsm0808_enc_cell_id_list(struct msgb *msg,
745 const struct gsm0808_cell_id_list *cil)
746{
747 uint8_t *old_tail;
748 uint8_t *tlv_len;
749 unsigned int i;
750
751 OSMO_ASSERT(msg);
752 OSMO_ASSERT(cil);
753
754 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
755 tlv_len = msgb_put(msg, 1);
756 old_tail = msg->tail;
757
758 msgb_put_u8(msg, cil->id_discr & 0x0f);
759
760 switch (cil->id_discr) {
761 case CELL_IDENT_LAC:
762 OSMO_ASSERT(cil->id_list_len <= CELL_ID_LIST_LAC_MAXLEN)
763 for (i=0;i<cil->id_list_len;i++) {
764 msgb_put_u16(msg, cil->id_list_lac[i]);
765 }
766 break;
767 case CELL_IDENT_BSS:
768 /* Does not have any list items */
769 break;
770 default:
771 /* FIXME: Implement support for all identifier list elements */
772 OSMO_ASSERT(false);
773 }
774
775 *tlv_len = (uint8_t) (msg->tail - old_tail);
776 return *tlv_len + 2;
777}
778
Stefan Sperling23381452018-03-15 19:38:15 +0100779/* Decode 5-byte LAI list element data (see TS 08.08 3.2.2.27) into MCC/MNC/LAC. */
780static void decode_lai(const uint8_t *data, struct osmo_location_area_id *decoded)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100781{
782 struct gsm48_loc_area_id lai;
783
Stefan Sperling23381452018-03-15 19:38:15 +0100784 /* Copy data to stack to prevent unaligned access in gsm48_decode_lai2(). */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100785 memcpy(&lai, data, sizeof(lai)); /* don't byte swap yet */
786
Stefan Sperling23381452018-03-15 19:38:15 +0100787 gsm48_decode_lai2(&lai, decoded);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100788}
789
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100790static 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 +0100791 size_t *consumed)
792{
793 struct osmo_cell_global_id *id;
794 uint16_t *ci_be;
795 size_t lai_offset;
796 int i = 0;
797 const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*ci_be);
798
799 *consumed = 0;
800 while (remain >= elemlen) {
801 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
802 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100803 id = &cil->id_list[i].global;
Stefan Sperling2873bf12018-03-14 18:38:41 +0100804 lai_offset = i * elemlen;
Stefan Sperling23381452018-03-15 19:38:15 +0100805 decode_lai(&data[lai_offset], &id->lai);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100806 ci_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]);
807 id->cell_identity = osmo_load16be(ci_be);
808 *consumed += elemlen;
809 remain -= elemlen;
810 i++;
811 }
812
813 return i;
814}
815
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100816static 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 +0100817 size_t *consumed)
818{
819 uint16_t *lacp_be, *ci_be;
820 struct osmo_lac_and_ci_id *id;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100821 int i = 0, j = 0;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100822 const size_t elemlen = sizeof(*lacp_be) + sizeof(*ci_be);
823
824 *consumed = 0;
825
826 if (remain < elemlen)
827 return -EINVAL;
828
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100829 lacp_be = (uint16_t *)(&data[j]);
830 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100831 while (remain >= elemlen) {
832 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
833 return -ENOSPC;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100834 id = &cil->id_list[i++].lac_and_ci;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100835 id->lac = osmo_load16be(lacp_be);
836 id->ci = osmo_load16be(ci_be);
837 *consumed += elemlen;
838 remain -= elemlen;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100839 j += elemlen;
840 lacp_be = (uint16_t *)(&data[j]);
841 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100842 }
843
844 return i;
845}
846
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100847static int parse_cell_id_ci_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain,
848 size_t *consumed)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100849{
850 const uint16_t *ci_be = (const uint16_t *)data;
851 int i = 0;
852 const size_t elemlen = sizeof(*ci_be);
853
854 *consumed = 0;
855 while (remain >= elemlen) {
856 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
857 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100858 cil->id_list[i++].ci = osmo_load16be(ci_be++);
Stefan Sperling9c62fc62018-03-16 10:23:34 +0100859 *consumed += elemlen;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100860 remain -= elemlen;
861 }
862 return i;
863}
864
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100865static 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 +0100866 size_t *consumed)
867{
868 struct osmo_location_area_id *id;
869 int i = 0;
870 const size_t elemlen = sizeof(struct gsm48_loc_area_id);
871
872 *consumed = 0;
873 while (remain >= elemlen) {
874 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
875 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100876 id = &cil->id_list[i].lai_and_lac;
Stefan Sperling23381452018-03-15 19:38:15 +0100877 decode_lai(&data[i * elemlen], id);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100878 *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_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain, size_t *consumed)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100887{
888 const uint16_t *lac_be = (const uint16_t *)data;
889 int i = 0;
890 const size_t elemlen = sizeof(*lac_be);
891
892 *consumed = 0;
893 while (remain >= elemlen) {
894 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
895 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100896 cil->id_list[i++].lac = osmo_load16be(lac_be++);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100897 *consumed += elemlen;
898 remain -= elemlen;
899 }
900 return i;
901}
902
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200903/*! Decode Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +0200904 * \param[out] cil Caller-provided memory to store Cell ID list
905 * \param[in] elem IE value to be decoded
906 * \param[in] len Length of \a elem in bytes
907 * \returns number of bytes parsed; negative on error */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100908int gsm0808_dec_cell_id_list2(struct gsm0808_cell_id_list2 *cil,
909 const uint8_t *elem, uint8_t len)
910{
911 uint8_t id_discr;
912 size_t bytes_elem = 0;
913 int list_len = 0;
914
915 OSMO_ASSERT(cil);
916 if (!elem)
917 return -EINVAL;
918 if (len == 0)
919 return -EINVAL;
920
921 memset(cil, 0, sizeof(*cil));
922
923 id_discr = *elem & 0x0f;
924 elem++;
925 len--;
926
927 switch (id_discr) {
928 case CELL_IDENT_WHOLE_GLOBAL:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100929 list_len = parse_cell_id_global_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100930 break;
931 case CELL_IDENT_LAC_AND_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100932 list_len = parse_cell_id_lac_and_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100933 break;
934 case CELL_IDENT_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100935 list_len = parse_cell_id_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100936 break;
937 case CELL_IDENT_LAI_AND_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100938 list_len = parse_cell_id_lai_and_lac(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100939 break;
940 case CELL_IDENT_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100941 list_len = parse_cell_id_lac_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100942 break;
943 case CELL_IDENT_BSS:
944 case CELL_IDENT_NO_CELL:
945 /* Does not have any list items */
946 break;
947 default:
948 /* Remaining cell identification types are not implemented. */
949 return -EINVAL;
950 }
951
952 if (list_len < 0) /* parsing error */
953 return list_len;
954
955 cil->id_discr = id_discr;
956 cil->id_list_len = list_len;
957
958 /* One byte for the cell ID discriminator + any remaining bytes in
959 * the IE which were consumed by the parser functions above. */
960 return 1 + (int)bytes_elem;
961}
962
963/*! DEPRECATED: Use gsm0808_dec_cell_id_list2 instead.
964 *
965 * Decode Cell Identifier List IE
966 * \param[out] cil Caller-provided memory to store Cell ID list
967 * \param[in] elem IE value to be decoded
968 * \param[in] len Length of \a elem in bytes
969 * \returns number of bytes parsed; negative on error */
Philipp Maier783047e2017-03-29 11:35:50 +0200970int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil,
971 const uint8_t *elem, uint8_t len)
972{
973 uint8_t id_discr;
974 const uint8_t *old_elem = elem;
975 unsigned int item_count = 0;
976
977 OSMO_ASSERT(cil);
978 if (!elem)
979 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200980 if (len == 0)
Philipp Maier783047e2017-03-29 11:35:50 +0200981 return -EINVAL;
982
983 memset(cil, 0, sizeof(*cil));
984
985 id_discr = *elem & 0x0f;
986 elem++;
987 len--;
988
989 cil->id_discr = id_discr;
990
991 switch (id_discr) {
992 case CELL_IDENT_LAC:
993 while (len >= 2) {
994 cil->id_list_lac[item_count] = osmo_load16be(elem);
995 elem += 2;
996 item_count++;
997 len -= 2;
998 }
999 case CELL_IDENT_BSS:
1000 /* Does not have any list items */
1001 break;
1002 default:
1003 /* FIXME: Implement support for all identifier list elements */
1004 return -EINVAL;
1005 }
1006
1007 cil->id_list_len = item_count;
1008 return (int)(elem - old_elem);
1009}
Harald Welte96e2a002017-06-12 21:44:18 +02001010
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001011static bool same_cell_id_list_entries(const struct gsm0808_cell_id_list2 *a, int ai,
1012 const struct gsm0808_cell_id_list2 *b, int bi)
1013{
1014 struct gsm0808_cell_id_list2 tmp = {
1015 .id_discr = a->id_discr,
1016 .id_list_len = 1,
1017 };
1018 uint8_t buf_a[32 + sizeof(struct msgb)];
1019 uint8_t buf_b[32 + sizeof(struct msgb)];
1020 struct msgb *msg_a = (void*)buf_a;
1021 struct msgb *msg_b = (void*)buf_b;
1022
1023 msg_a->data_len = 32;
1024 msg_b->data_len = 32;
1025 msgb_reset(msg_a);
1026 msgb_reset(msg_b);
1027
1028 if (a->id_discr != b->id_discr)
1029 return false;
1030 if (ai >= a->id_list_len
1031 || bi >= b->id_list_len)
1032 return false;
1033
1034 tmp.id_list[0] = a->id_list[ai];
1035 gsm0808_enc_cell_id_list2(msg_a, &tmp);
1036
1037 tmp.id_list[0] = b->id_list[bi];
1038 gsm0808_enc_cell_id_list2(msg_b, &tmp);
1039
1040 if (msg_a->len != msg_b->len)
1041 return false;
1042 if (memcmp(msg_a->data, msg_b->data, msg_a->len))
1043 return false;
1044
1045 return true;
1046}
1047
1048/*! Append entries from one Cell Identifier List to another.
1049 * The cell identifier types must be identical between the two lists.
1050 * \param dst[out] Append entries to this list.
1051 * \param src[in] Append these entries to \a dst.
1052 * \returns the nr of items added, or negative on error: -EINVAL if the id_discr mismatch
1053 * between the lists, -ENOSPC if the destination list does not have enough space. If an error is
1054 * returned, \a dst may have already been changed (particularly on -ENOSPC). Note that a return value
1055 * of zero may occur when the src->id_list_len is zero, or when all entries from \a src already exist
1056 * in \a dst, and does not indicate error per se. */
1057int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src)
1058{
1059 int i, j;
1060 int added = 0;
1061
1062 if (dst->id_list_len == 0
1063 && dst->id_discr != CELL_IDENT_BSS)
1064 dst->id_discr = src->id_discr;
1065 else if (dst->id_discr != src->id_discr)
1066 return -EINVAL;
1067
1068 for (i = 0; i < src->id_list_len; i++) {
1069 /* don't add duplicate entries */
1070 bool skip = false;
1071 for (j = 0; j < dst->id_list_len; j++) {
1072 if (same_cell_id_list_entries(dst, j, src, i)) {
1073 skip = true;
1074 break;
1075 }
1076 }
1077 if (skip)
1078 continue;
1079
1080 if (dst->id_list_len >= ARRAY_SIZE(dst->id_list))
1081 return -ENOSPC;
1082
1083 dst->id_list[dst->id_list_len++] = src->id_list[i];
1084 added ++;
1085 }
1086
1087 return added;
1088}
1089
Neels Hofmeyr38e58412018-05-25 16:56:35 +02001090/*! Convert a single Cell Identifier to a Cell Identifier List with one entry.
1091 * \param dst[out] Overwrite this list.
1092 * \param src[in] Set \a dst to contain exactly this item.
1093 */
1094void gsm0808_cell_id_to_list(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id *src)
1095{
1096 if (!dst)
1097 return;
1098 if (!src) {
1099 *dst = (struct gsm0808_cell_id_list2){
1100 .id_discr = CELL_IDENT_NO_CELL,
1101 };
1102 return;
1103 }
1104
1105 *dst = (struct gsm0808_cell_id_list2){
1106 .id_discr = src->id_discr,
1107 .id_list = { src->id },
1108 .id_list_len = 1,
1109 };
1110
1111 switch (src->id_discr) {
1112 case CELL_IDENT_NO_CELL:
1113 case CELL_IDENT_BSS:
1114 dst->id_list_len = 0;
1115 break;
1116 default:
1117 break;
1118 }
1119}
1120
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001121/*! Encode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1122 * \param[out] msg Message Buffer to which IE is to be appended
1123 * \param[in] ci Cell ID to be encoded
1124 * \returns number of bytes appended to \a msg */
1125uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci)
1126{
1127 uint8_t rc;
1128 uint8_t *ie_tag;
1129 struct gsm0808_cell_id_list2 cil = {
1130 .id_discr = ci->id_discr,
1131 .id_list = { ci->id },
1132 .id_list_len = 1,
1133 };
1134
1135 OSMO_ASSERT(msg);
1136 OSMO_ASSERT(ci);
1137
1138 ie_tag = msg->tail;
1139 rc = gsm0808_enc_cell_id_list2(msg, &cil);
1140
1141 if (rc <= 0)
1142 return rc;
1143
1144 *ie_tag = GSM0808_IE_CELL_IDENTIFIER;
1145 return rc;
1146}
1147
1148/*! Decode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1149 * \param[out] ci Caller-provided memory to store Cell ID.
1150 * \param[in] elem IE value to be decoded.
1151 * \param[in] len Length of \a elem in bytes.
1152 * \returns number of bytes parsed; negative on error */
1153int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len)
1154{
1155 struct gsm0808_cell_id_list2 cil;
1156 int rc;
1157 rc = gsm0808_dec_cell_id_list2(&cil, elem, len);
1158 if (rc < 0)
1159 return rc;
1160 if (cil.id_discr == CELL_IDENT_BSS || cil.id_discr == CELL_IDENT_NO_CELL) {
1161 if (cil.id_list_len != 0)
1162 return -EINVAL;
1163 } else {
1164 if (cil.id_list_len != 1)
1165 return -EINVAL;
1166 }
1167 ci->id_discr = cil.id_discr;
1168 ci->id = cil.id_list[0];
1169 return rc;
1170}
1171
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001172/*! Convert the representation of the permitted speech codec identifier
Philipp Maier3149b0d2017-06-02 13:22:34 +02001173 * that is used in struct gsm0808_channel_type to the speech codec
1174 * representation we use in struct gsm0808_speech_codec.
1175 * \param[in] perm_spch to be converted (see also gsm0808_permitted_speech)
1176 * \returns GSM speech codec type; negative on error */
1177int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch)
1178{
1179 /*! The speech codec type, which is used in the channel type field to
1180 * signal the permitted speech versions (codecs) has a different
1181 * encoding than the type field in the speech codec type element
1182 * (See also 3GPP TS 48.008, 3.2.2.11 and 3.2.2.103) */
1183
1184 switch (perm_spch) {
1185 case GSM0808_PERM_FR1:
1186 return GSM0808_SCT_FR1;
1187 case GSM0808_PERM_FR2:
1188 return GSM0808_SCT_FR2;
1189 case GSM0808_PERM_FR3:
1190 return GSM0808_SCT_FR3;
1191 case GSM0808_PERM_FR4:
1192 return GSM0808_SCT_FR4;
1193 case GSM0808_PERM_FR5:
1194 return GSM0808_SCT_FR5;
1195 case GSM0808_PERM_HR1:
1196 return GSM0808_SCT_HR1;
1197 case GSM0808_PERM_HR3:
1198 return GSM0808_SCT_HR3;
1199 case GSM0808_PERM_HR4:
1200 return GSM0808_SCT_HR4;
1201 case GSM0808_PERM_HR6:
1202 return GSM0808_SCT_HR6;
1203 }
1204
1205 /* Invalid input */
1206 return -EINVAL;
1207}
1208
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001209/*! Extrapolate a speech codec field from a given permitted speech
Philipp Maier884ba0f2017-06-02 13:49:16 +02001210 * parameter (channel type).
1211 * \param[out] sc Caller provided memory to store the resulting speech codec
1212 * \param[in] perm_spch value that is used to derive the speech codec info
1213 * (see also: enum gsm0808_speech_codec_type in gsm0808_utils.h)
1214 * \returns zero when successful; negative on error */
1215int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc,
1216 uint8_t perm_spch)
1217{
1218 int rc;
1219
1220 memset(sc, 0, sizeof(*sc));
1221
1222 /* Determine codec type */
1223 rc = gsm0808_chan_type_to_speech_codec(perm_spch);
1224 if (rc < 0)
1225 return -EINVAL;
1226 sc->type = (uint8_t) rc;
1227
1228 /* Depending on the speech codec type, pick a default codec
1229 * configuration that exactly matches the configuration on the
1230 * air interface. */
1231 switch (sc->type) {
1232 case GSM0808_SCT_FR3:
1233 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR;
1234 break;
1235 case GSM0808_SCT_FR4:
1236 sc->cfg = GSM0808_SC_CFG_DEFAULT_OFR_AMR_WB;
1237 break;
1238 case GSM0808_SCT_FR5:
1239 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR_WB;
1240 break;
1241 case GSM0808_SCT_HR3:
1242 sc->cfg = GSM0808_SC_CFG_DEFAULT_HR_AMR;
1243 break;
1244 case GSM0808_SCT_HR4:
1245 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR_WB;
1246 break;
1247 case GSM0808_SCT_HR6:
1248 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR;
1249 break;
1250 default:
1251 /* Note: Not all codec types specify a default setting,
1252 * in this case, we just set the field to zero. */
1253 sc->cfg = 0;
1254 }
1255
1256 /* Tag all codecs as "Full IP"
1257 * (see als 3GPP TS 48.008 3.2.2.103) */
1258 sc->fi = true;
1259
1260 return 0;
1261}
1262
Philipp Maier5f2eb152018-09-19 13:40:21 +02001263/*! Determine a set of AMR speech codec configuration bits (S0-S15) from a
1264 * given GSM 04.08 AMR configuration struct.
1265 * \param[in] cfg AMR configuration in GSM 04.08 format.
1266 * \param[in] hint if the resulting configuration shall be used with a FR or HR TCH.
1267 * \returns configuration bits (S0-S15) */
Philipp Maier369015c2018-09-21 09:07:20 +02001268uint16_t gsm0808_sc_cfg_from_gsm48_mr_cfg(const struct gsm48_multi_rate_conf *cfg,
Philipp Maier5f2eb152018-09-19 13:40:21 +02001269 bool fr)
1270{
1271 uint16_t s15_s0 = 0;
1272
1273 /* Check each rate bit in the AMR multirate configuration and pick the
1274 * matching default configuration as specified in 3GPP TS 28.062,
1275 * Table 7.11.3.1.3-2. */
1276 if (cfg->m4_75)
1277 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_4_75;
1278 if (cfg->m5_15)
1279 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_15;
1280 if (cfg->m5_90)
1281 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_90;
1282 if (cfg->m6_70)
1283 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_6_70;
1284 if (cfg->m7_40)
1285 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_40;
1286 if (cfg->m7_95)
1287 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_95;
1288 if (cfg->m10_2)
1289 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_10_2;
1290 if (cfg->m12_2)
1291 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_12_2;
1292
1293 /* Note: 3GPP TS 48.008, chapter 3GPP TS 48.008 states that for AMR
1294 * some of the configuration bits must be coded as zeros. The applied
1295 * bitmask matches the default codec settings. See also the definition
1296 * of enum gsm0808_speech_codec_defaults in gsm_08_08.h and
1297 * 3GPP TS 28.062, Table 7.11.3.1.3-2. */
1298 if (fr)
1299 s15_s0 &= GSM0808_SC_CFG_DEFAULT_FR_AMR;
1300 else
1301 s15_s0 &= GSM0808_SC_CFG_DEFAULT_HR_AMR;
1302
1303 return s15_s0;
1304}
1305
Philipp Maier8515d032018-09-25 15:57:49 +02001306/*! Determine a GSM 04.08 AMR configuration struct from a set of speech codec
1307 * configuration bits (S0-S15)
1308 * \param[out] cfg AMR configuration in GSM 04.08 format.
1309 * \param[in] s15_s0 configuration bits (S0-S15). */
1310void gsm48_mr_cfg_from_gsm0808_sc_cfg(struct gsm48_multi_rate_conf *cfg,
1311 uint16_t s15_s0)
1312{
1313 memset(cfg, 0, sizeof(*cfg));
1314
1315 /* Strip option bits */
1316 s15_s0 &= 0x00ff;
1317
1318 /* Rate 5,15k must always be present */
1319 cfg->m5_15 = 1;
1320
1321 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_4_75 & 0xff) ==
1322 (GSM0808_SC_CFG_DEFAULT_AMR_4_75 & 0xff))
1323 cfg->m4_75 = 1;
1324 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_5_90 & 0xff) ==
1325 (GSM0808_SC_CFG_DEFAULT_AMR_5_90 & 0xff))
1326 cfg->m5_90 = 1;
1327 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_6_70 & 0xff) ==
1328 (GSM0808_SC_CFG_DEFAULT_AMR_6_70 & 0xff))
1329 cfg->m6_70 = 1;
1330 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_7_40 & 0xff) ==
1331 (GSM0808_SC_CFG_DEFAULT_AMR_7_40 & 0xff))
1332 cfg->m7_40 = 1;
1333 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_7_95 & 0xff) ==
1334 (GSM0808_SC_CFG_DEFAULT_AMR_7_95 & 0xff))
1335 cfg->m7_95 = 1;
1336 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_10_2 & 0xff) ==
1337 (GSM0808_SC_CFG_DEFAULT_AMR_10_2 & 0xff))
1338 cfg->m10_2 = 1;
1339 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_12_2 & 0xff) ==
1340 (GSM0808_SC_CFG_DEFAULT_AMR_12_2 & 0xff))
1341 cfg->m12_2 = 1;
1342
1343 cfg->ver = 1;
1344 cfg->icmi = 1;
1345}
1346
Maxed651d22018-11-07 15:25:05 +01001347int gsm0808_get_cipher_reject_cause(const struct tlv_parsed *tp)
1348{
1349 const uint8_t *buf = TLVP_VAL_MINLEN(tp, GSM0808_IE_CAUSE, 1);
1350
1351 if (!buf)
1352 return -EBADMSG;
1353
1354 if (TLVP_LEN(tp, GSM0808_IE_CAUSE) > 1) {
1355 if (!gsm0808_cause_ext(buf[0]))
1356 return -EINVAL;
1357 return buf[1];
1358 }
1359
1360 return buf[0];
1361}
1362
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001363/*! Print a human readable name of the cell identifier to the char buffer.
1364 * This is useful both for struct gsm0808_cell_id and struct gsm0808_cell_id_list2.
1365 * See also gsm0808_cell_id_name() and gsm0808_cell_id_list_name().
1366 * \param[out] buf Destination buffer to write string representation to.
1367 * \param[in] buflen Amount of memory available in \a buf.
1368 * \param[in] id_discr Cell Identifier type.
1369 * \param[in] u Cell Identifer value.
1370 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1371 * or that would have been written if the buffer were large enough.
1372 */
1373int gsm0808_cell_id_u_name(char *buf, size_t buflen,
1374 enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
1375{
1376 switch (id_discr) {
1377 case CELL_IDENT_LAC:
1378 return snprintf(buf, buflen, "%u", u->lac);
1379 case CELL_IDENT_CI:
1380 return snprintf(buf, buflen, "%u", u->ci);
1381 case CELL_IDENT_LAC_AND_CI:
1382 return snprintf(buf, buflen, "%u-%u", u->lac_and_ci.lac, u->lac_and_ci.ci);
1383 case CELL_IDENT_LAI_AND_LAC:
1384 return snprintf(buf, buflen, "%s", osmo_lai_name(&u->lai_and_lac));
1385 case CELL_IDENT_WHOLE_GLOBAL:
1386 return snprintf(buf, buflen, "%s", osmo_cgi_name(&u->global));
1387 default:
1388 /* For CELL_IDENT_BSS and CELL_IDENT_NO_CELL, just print the discriminator.
1389 * Same for kinds we have no string representation of yet. */
1390 return snprintf(buf, buflen, "%s", gsm0808_cell_id_discr_name(id_discr));
1391 }
1392}
1393
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02001394/* Store individual Cell Identifier information in a CGI, without clearing the remaining ones.
1395 * This is useful to supplement one CGI with information from more than one Cell Identifier,
1396 * which in turn is useful to match Cell Identifiers of differing kinds to each other.
1397 * Before first invocation, clear the *dst struct externally, this function does only write those members
1398 * that are present in parameter u.
1399 */
1400static void cell_id_to_cgi(struct osmo_cell_global_id *dst,
1401 enum CELL_IDENT discr, const union gsm0808_cell_id_u *u)
1402{
1403 switch (discr) {
1404 case CELL_IDENT_WHOLE_GLOBAL:
1405 *dst = u->global;
1406 return;
1407
1408 case CELL_IDENT_LAC_AND_CI:
1409 dst->lai.lac = u->lac_and_ci.lac;
1410 dst->cell_identity = u->lac_and_ci.ci;
1411 return;
1412
1413 case CELL_IDENT_CI:
1414 dst->cell_identity = u->ci;
1415 return;
1416
1417 case CELL_IDENT_LAI_AND_LAC:
1418 dst->lai = u->lai_and_lac;
1419 return;
1420
1421 case CELL_IDENT_LAC:
1422 dst->lai.lac = u->lac;
1423 return;
1424
1425 case CELL_IDENT_NO_CELL:
1426 case CELL_IDENT_BSS:
1427 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1428 case CELL_IDENT_UTRAN_RNC:
1429 case CELL_IDENT_UTRAN_LAC_RNC:
1430 /* No values to set. */
1431 return;
1432 }
1433}
1434
1435/*! Return true if the common information between the two Cell Identifiers match.
1436 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1437 * Note that CELL_IDENT_NO_CELL will always return false.
1438 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1439 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1440 * \param[in] discr1 Cell Identifier type.
1441 * \param[in] u1 Cell Identifier value.
1442 * \param[in] discr2 Other Cell Identifier type.
1443 * \param[in] u2 Other Cell Identifier value.
1444 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1445 * \returns True if the common fields of the above match.
1446 */
1447static bool gsm0808_cell_id_u_match(enum CELL_IDENT discr1, const union gsm0808_cell_id_u *u1,
1448 enum CELL_IDENT discr2, const union gsm0808_cell_id_u *u2,
1449 bool exact_match)
1450{
1451 struct osmo_cell_global_id a = {};
1452 struct osmo_cell_global_id b = {};
1453
1454 if (exact_match && discr1 != discr2)
1455 return false;
1456
1457 /* First handle the odd wildcard like CELL_IDENT kinds. We can't really match any of these. */
1458 switch (discr1) {
1459 case CELL_IDENT_NO_CELL:
1460 case CELL_IDENT_BSS:
1461 return discr1 == discr2;
1462 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1463 case CELL_IDENT_UTRAN_RNC:
1464 case CELL_IDENT_UTRAN_LAC_RNC:
1465 return false;
1466 default:
1467 break;
1468 }
1469 switch (discr2) {
1470 case CELL_IDENT_NO_CELL:
1471 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1472 case CELL_IDENT_UTRAN_RNC:
1473 case CELL_IDENT_UTRAN_LAC_RNC:
1474 case CELL_IDENT_BSS:
1475 return false;
1476 default:
1477 break;
1478 }
1479
1480 /* Enrich both sides to full CGI, then compare those. First set the *other* ID's values in case
1481 * they assign more items. For example:
1482 * u1 = LAC:42
1483 * u2 = LAC+CI:23+5
1484 * 1) a <- LAC+CI:23+5
1485 * 2) a <- LAC:42 so that a = LAC+CI:42+5
1486 * Now we can compare those two and find a mismatch. If the LAC were the same, we would get
1487 * identical LAC+CI and hence a match. */
1488
1489 cell_id_to_cgi(&a, discr2, u2);
1490 cell_id_to_cgi(&a, discr1, u1);
1491
1492 cell_id_to_cgi(&b, discr1, u1);
1493 cell_id_to_cgi(&b, discr2, u2);
1494
1495 return osmo_cgi_cmp(&a, &b) == 0;
1496}
1497
1498/*! Return true if the common information between the two Cell Identifiers match.
1499 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1500 * Note that CELL_IDENT_NO_CELL will always return false.
1501 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1502 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1503 * \param[in] id1 Cell Identifier.
1504 * \param[in] id2 Other Cell Identifier.
1505 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1506 * \returns True if the common fields of the above match.
1507 */
1508bool gsm0808_cell_ids_match(const struct gsm0808_cell_id *id1, const struct gsm0808_cell_id *id2, bool exact_match)
1509{
1510 return gsm0808_cell_id_u_match(id1->id_discr, &id1->id, id2->id_discr, &id2->id, exact_match);
1511}
1512
1513/*! Find an index in a Cell Identifier list that matches a given single Cell Identifer.
1514 * Compare \a id against each entry in \a list using gsm0808_cell_ids_match(), and return the list index
1515 * if a match is found. \a match_nr allows iterating all matches in the list. A match_nr <= 0 returns the
1516 * first match in the list, match_nr == 1 the second match, etc., and if match_nr exceeds the available
1517 * matches in the list, -1 is returned.
1518 * \param[in] id Cell Identifier to match.
1519 * \param[in] list Cell Identifier list to search in.
1520 * \param[in] match_nr Ignore this many matches.
1521 * \param[in] exact_match If true, consider as match only if the CELL_IDENT types and all values are identical.
1522 * \returns -1 if no match is found, list index if a match is found.
1523 */
1524int gsm0808_cell_id_matches_list(const struct gsm0808_cell_id *id, const struct gsm0808_cell_id_list2 *list,
1525 unsigned int match_nr, bool exact_match)
1526{
1527 int i;
1528 for (i = 0; i < list->id_list_len; i++) {
1529 if (gsm0808_cell_id_u_match(id->id_discr, &id->id, list->id_discr, &list->id_list[i], exact_match)) {
1530 if (match_nr)
1531 match_nr--;
1532 else
1533 return i;
1534 }
1535 }
1536 return -1;
1537}
1538
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001539/*! value_string[] for enum CELL_IDENT. */
1540const struct value_string gsm0808_cell_id_discr_names[] = {
1541 { CELL_IDENT_WHOLE_GLOBAL, "CGI" },
1542 { CELL_IDENT_LAC_AND_CI, "LAC-CI" },
1543 { CELL_IDENT_CI, "CI" },
1544 { CELL_IDENT_NO_CELL, "NO-CELL" },
1545 { CELL_IDENT_LAI_AND_LAC, "LAI" },
1546 { CELL_IDENT_LAC, "LAC" },
1547 { CELL_IDENT_BSS, "BSS" },
1548 { CELL_IDENT_UTRAN_PLMN_LAC_RNC, "UTRAN-PLMN-LAC-RNC" },
1549 { CELL_IDENT_UTRAN_RNC, "UTRAN-RNC" },
1550 { CELL_IDENT_UTRAN_LAC_RNC, "UTRAN-LAC-RNC" },
1551 { 0, NULL }
1552};
1553
1554#define APPEND_THING(func, args...) do { \
1555 int remain = buflen - (pos - buf); \
1556 int l = func(pos, remain, ##args); \
1557 if (l < 0 || l > remain) \
1558 pos = buf + buflen; \
1559 else \
1560 pos += l; \
1561 if (l > 0) \
1562 total_len += l; \
1563 } while(0)
1564#define APPEND_STR(fmt, args...) APPEND_THING(snprintf, fmt, ##args)
1565#define APPEND_CELL_ID_U(DISCR, U) APPEND_THING(gsm0808_cell_id_u_name, DISCR, U)
1566
1567static const char *gsm0808_cell_id_name_buf(const struct gsm0808_cell_id *cid,
1568 char *buf, size_t buflen)
1569{
1570 char *pos = buf;
1571 int total_len = 0;
1572 APPEND_STR("%s:", gsm0808_cell_id_discr_name(cid->id_discr));
1573 APPEND_CELL_ID_U(cid->id_discr, &cid->id);
1574 return buf;
1575}
1576
1577/*! Return a human readable representation of a Cell Identifier, like "LAC:123"
1578 * or "CGI:001-01-42-23".
1579 * \param[in] cid Cell Identifer.
1580 * \returns String in a static buffer.
1581 */
1582const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid)
1583{
1584 static char buf[64];
1585 return gsm0808_cell_id_name_buf(cid, buf, sizeof(buf));
1586}
1587
1588/*! Like gsm0808_cell_id_name() but uses a different static buffer.
1589 * \param[in] cid Cell Identifer.
1590 * \returns String in a static buffer.
1591 */
1592const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid)
1593{
1594 static char buf[64];
1595 return gsm0808_cell_id_name_buf(cid, buf, sizeof(buf));
1596}
1597
1598/*! Return a human readable representation of the Cell Identifier List, like
1599 * "LAC[2]:{123, 456}".
1600 * The return value semantics are like snprintf() and thus allow ensuring a complete
1601 * untruncated string by determining the required string length from the return value.
1602 * If buflen > 0, always nul-terminate the string in buf, also when it is truncated.
1603 * If buflen == 0, do not modify buf, just return the would-be length.
1604 * \param[out] buf Destination buffer to write string representation to.
1605 * \param[in] buflen Amount of memory available in \a buf.
1606 * \param[in] cil Cell Identifer List.
1607 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1608 * or that would have been written if the buffer were large enough.
1609 */
1610int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil)
1611{
1612 char *pos = buf;
1613 int total_len = 0;
1614 int i;
1615
1616 APPEND_STR("%s[%u]", gsm0808_cell_id_discr_name(cil->id_discr), cil->id_list_len);
1617
1618 switch (cil->id_discr) {
1619 case CELL_IDENT_BSS:
1620 case CELL_IDENT_NO_CELL:
1621 return total_len;
1622 default:
1623 break;
1624 }
1625
1626 APPEND_STR(":{");
1627
1628 for (i = 0; i < cil->id_list_len; i++) {
1629 if (i)
1630 APPEND_STR(", ");
1631 APPEND_CELL_ID_U(cil->id_discr, &cil->id_list[i]);
1632 }
1633
1634 APPEND_STR("}");
1635 return total_len;
1636}
1637
1638/*! Return a human-readable representation of \a cil in a static buffer.
1639 * If the list is too long, the output may be truncated.
1640 * See also gsm0808_cell_id_list_name_buf(). */
1641const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil)
1642{
1643 static char buf[1024];
1644 gsm0808_cell_id_list_name_buf(buf, sizeof(buf), cil);
1645 return buf;
1646}
1647
1648#undef APPEND_STR
1649#undef APPEND_CELL_ID_U
1650
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02001651const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct)
1652{
1653 static char buf[128];
1654 snprintf(buf, sizeof(buf), "ch_indctr=0x%x ch_rate_type=0x%x perm_spch=%s",
1655 ct->ch_indctr, ct->ch_rate_type,
1656 osmo_hexdump(ct->perm_spch, ct->perm_spch_len));
1657 return buf;
1658}
1659
Harald Welte96e2a002017-06-12 21:44:18 +02001660/*! @} */