blob: 99cf18813f500c8eb924db418105dd7d5a35a83f [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
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200740/*! Encode TS 08.08 Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +0200741 * \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 */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100744uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg,
745 const struct gsm0808_cell_id_list2 *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 OSMO_ASSERT(cil->id_list_len <= GSM0808_CELL_ID_LIST2_MAXLEN)
761 switch (cil->id_discr) {
762 case CELL_IDENT_WHOLE_GLOBAL:
763 for (i = 0; i < cil->id_list_len; i++) {
764 const struct osmo_cell_global_id *id = &cil->id_list[i].global;
765 struct gsm48_loc_area_id lai;
Neels Hofmeyr8b8cd932018-03-23 01:47:37 +0100766 gsm48_generate_lai2(&lai, &id->lai);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100767 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
768 msgb_put_u16(msg, id->cell_identity);
769 }
770 break;
771 case CELL_IDENT_LAC_AND_CI:
772 for (i = 0; i < cil->id_list_len; i++) {
773 const struct osmo_lac_and_ci_id *id = &cil->id_list[i].lac_and_ci;
774 msgb_put_u16(msg, id->lac);
775 msgb_put_u16(msg, id->ci);
776 }
777 break;
778 case CELL_IDENT_CI:
779 for (i = 0; i < cil->id_list_len; i++)
780 msgb_put_u16(msg, cil->id_list[i].ci);
781 break;
782 case CELL_IDENT_LAI_AND_LAC:
783 for (i = 0; i < cil->id_list_len; i++) {
784 const struct osmo_location_area_id *id = &cil->id_list[i].lai_and_lac;
785 struct gsm48_loc_area_id lai;
Neels Hofmeyr8b8cd932018-03-23 01:47:37 +0100786 gsm48_generate_lai2(&lai, id);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100787 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
788 }
789 break;
790 case CELL_IDENT_LAC:
791 for (i = 0; i < cil->id_list_len; i++)
792 msgb_put_u16(msg, cil->id_list[i].lac);
793 break;
794 case CELL_IDENT_BSS:
795 case CELL_IDENT_NO_CELL:
796 /* Does not have any list items */
797 break;
798 default:
799 /* Support for other identifier list types is not implemented. */
800 OSMO_ASSERT(false);
801 }
802
803 *tlv_len = (uint8_t) (msg->tail - old_tail);
804 return *tlv_len + 2;
805}
806
807/*! DEPRECATED: Use gsm0808_enc_cell_id_list2 instead.
808 *
809 * Encode TS 08.08 Cell Identifier List IE
810 * \param[out] msg Message Buffer to which IE is to be appended
811 * \param[in] cil Cell ID List to be encoded
812 * \returns number of bytes appended to \a msg */
Philipp Maier783047e2017-03-29 11:35:50 +0200813uint8_t gsm0808_enc_cell_id_list(struct msgb *msg,
814 const struct gsm0808_cell_id_list *cil)
815{
816 uint8_t *old_tail;
817 uint8_t *tlv_len;
818 unsigned int i;
819
820 OSMO_ASSERT(msg);
821 OSMO_ASSERT(cil);
822
823 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
824 tlv_len = msgb_put(msg, 1);
825 old_tail = msg->tail;
826
827 msgb_put_u8(msg, cil->id_discr & 0x0f);
828
829 switch (cil->id_discr) {
830 case CELL_IDENT_LAC:
831 OSMO_ASSERT(cil->id_list_len <= CELL_ID_LIST_LAC_MAXLEN)
832 for (i=0;i<cil->id_list_len;i++) {
833 msgb_put_u16(msg, cil->id_list_lac[i]);
834 }
835 break;
836 case CELL_IDENT_BSS:
837 /* Does not have any list items */
838 break;
839 default:
840 /* FIXME: Implement support for all identifier list elements */
841 OSMO_ASSERT(false);
842 }
843
844 *tlv_len = (uint8_t) (msg->tail - old_tail);
845 return *tlv_len + 2;
846}
847
Stefan Sperling23381452018-03-15 19:38:15 +0100848/* Decode 5-byte LAI list element data (see TS 08.08 3.2.2.27) into MCC/MNC/LAC. */
849static void decode_lai(const uint8_t *data, struct osmo_location_area_id *decoded)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100850{
851 struct gsm48_loc_area_id lai;
852
Stefan Sperling23381452018-03-15 19:38:15 +0100853 /* Copy data to stack to prevent unaligned access in gsm48_decode_lai2(). */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100854 memcpy(&lai, data, sizeof(lai)); /* don't byte swap yet */
855
Stefan Sperling23381452018-03-15 19:38:15 +0100856 gsm48_decode_lai2(&lai, decoded);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100857}
858
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100859static 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 +0100860 size_t *consumed)
861{
862 struct osmo_cell_global_id *id;
863 uint16_t *ci_be;
864 size_t lai_offset;
865 int i = 0;
866 const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*ci_be);
867
868 *consumed = 0;
869 while (remain >= elemlen) {
870 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
871 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100872 id = &cil->id_list[i].global;
Stefan Sperling2873bf12018-03-14 18:38:41 +0100873 lai_offset = i * elemlen;
Stefan Sperling23381452018-03-15 19:38:15 +0100874 decode_lai(&data[lai_offset], &id->lai);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100875 ci_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]);
876 id->cell_identity = osmo_load16be(ci_be);
877 *consumed += elemlen;
878 remain -= elemlen;
879 i++;
880 }
881
882 return i;
883}
884
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100885static 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 +0100886 size_t *consumed)
887{
888 uint16_t *lacp_be, *ci_be;
889 struct osmo_lac_and_ci_id *id;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100890 int i = 0, j = 0;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100891 const size_t elemlen = sizeof(*lacp_be) + sizeof(*ci_be);
892
893 *consumed = 0;
894
895 if (remain < elemlen)
896 return -EINVAL;
897
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100898 lacp_be = (uint16_t *)(&data[j]);
899 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100900 while (remain >= elemlen) {
901 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
902 return -ENOSPC;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100903 id = &cil->id_list[i++].lac_and_ci;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100904 id->lac = osmo_load16be(lacp_be);
905 id->ci = osmo_load16be(ci_be);
906 *consumed += elemlen;
907 remain -= elemlen;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100908 j += elemlen;
909 lacp_be = (uint16_t *)(&data[j]);
910 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100911 }
912
913 return i;
914}
915
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100916static int parse_cell_id_ci_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain,
917 size_t *consumed)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100918{
919 const uint16_t *ci_be = (const uint16_t *)data;
920 int i = 0;
921 const size_t elemlen = sizeof(*ci_be);
922
923 *consumed = 0;
924 while (remain >= elemlen) {
925 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
926 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100927 cil->id_list[i++].ci = osmo_load16be(ci_be++);
Stefan Sperling9c62fc62018-03-16 10:23:34 +0100928 *consumed += elemlen;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100929 remain -= elemlen;
930 }
931 return i;
932}
933
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100934static 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 +0100935 size_t *consumed)
936{
937 struct osmo_location_area_id *id;
938 int i = 0;
939 const size_t elemlen = sizeof(struct gsm48_loc_area_id);
940
941 *consumed = 0;
942 while (remain >= elemlen) {
943 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
944 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100945 id = &cil->id_list[i].lai_and_lac;
Stefan Sperling23381452018-03-15 19:38:15 +0100946 decode_lai(&data[i * elemlen], id);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100947 *consumed += elemlen;
948 remain -= elemlen;
949 i++;
950 }
951
952 return i;
953}
954
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100955static 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 +0100956{
957 const uint16_t *lac_be = (const uint16_t *)data;
958 int i = 0;
959 const size_t elemlen = sizeof(*lac_be);
960
961 *consumed = 0;
962 while (remain >= elemlen) {
963 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
964 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100965 cil->id_list[i++].lac = osmo_load16be(lac_be++);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100966 *consumed += elemlen;
967 remain -= elemlen;
968 }
969 return i;
970}
971
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200972/*! Decode Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +0200973 * \param[out] cil Caller-provided memory to store Cell ID list
974 * \param[in] elem IE value to be decoded
975 * \param[in] len Length of \a elem in bytes
976 * \returns number of bytes parsed; negative on error */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100977int gsm0808_dec_cell_id_list2(struct gsm0808_cell_id_list2 *cil,
978 const uint8_t *elem, uint8_t len)
979{
980 uint8_t id_discr;
981 size_t bytes_elem = 0;
982 int list_len = 0;
983
984 OSMO_ASSERT(cil);
985 if (!elem)
986 return -EINVAL;
987 if (len == 0)
988 return -EINVAL;
989
990 memset(cil, 0, sizeof(*cil));
991
992 id_discr = *elem & 0x0f;
993 elem++;
994 len--;
995
996 switch (id_discr) {
997 case CELL_IDENT_WHOLE_GLOBAL:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100998 list_len = parse_cell_id_global_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100999 break;
1000 case CELL_IDENT_LAC_AND_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001001 list_len = parse_cell_id_lac_and_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001002 break;
1003 case CELL_IDENT_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001004 list_len = parse_cell_id_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001005 break;
1006 case CELL_IDENT_LAI_AND_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001007 list_len = parse_cell_id_lai_and_lac(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001008 break;
1009 case CELL_IDENT_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +01001010 list_len = parse_cell_id_lac_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +01001011 break;
1012 case CELL_IDENT_BSS:
1013 case CELL_IDENT_NO_CELL:
1014 /* Does not have any list items */
1015 break;
1016 default:
1017 /* Remaining cell identification types are not implemented. */
1018 return -EINVAL;
1019 }
1020
1021 if (list_len < 0) /* parsing error */
1022 return list_len;
1023
1024 cil->id_discr = id_discr;
1025 cil->id_list_len = list_len;
1026
1027 /* One byte for the cell ID discriminator + any remaining bytes in
1028 * the IE which were consumed by the parser functions above. */
1029 return 1 + (int)bytes_elem;
1030}
1031
1032/*! DEPRECATED: Use gsm0808_dec_cell_id_list2 instead.
1033 *
1034 * Decode Cell Identifier List IE
1035 * \param[out] cil Caller-provided memory to store Cell ID list
1036 * \param[in] elem IE value to be decoded
1037 * \param[in] len Length of \a elem in bytes
1038 * \returns number of bytes parsed; negative on error */
Philipp Maier783047e2017-03-29 11:35:50 +02001039int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil,
1040 const uint8_t *elem, uint8_t len)
1041{
1042 uint8_t id_discr;
1043 const uint8_t *old_elem = elem;
1044 unsigned int item_count = 0;
1045
1046 OSMO_ASSERT(cil);
1047 if (!elem)
1048 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +02001049 if (len == 0)
Philipp Maier783047e2017-03-29 11:35:50 +02001050 return -EINVAL;
1051
1052 memset(cil, 0, sizeof(*cil));
1053
1054 id_discr = *elem & 0x0f;
1055 elem++;
1056 len--;
1057
1058 cil->id_discr = id_discr;
1059
1060 switch (id_discr) {
1061 case CELL_IDENT_LAC:
1062 while (len >= 2) {
1063 cil->id_list_lac[item_count] = osmo_load16be(elem);
1064 elem += 2;
1065 item_count++;
1066 len -= 2;
1067 }
1068 case CELL_IDENT_BSS:
1069 /* Does not have any list items */
1070 break;
1071 default:
1072 /* FIXME: Implement support for all identifier list elements */
1073 return -EINVAL;
1074 }
1075
1076 cil->id_list_len = item_count;
1077 return (int)(elem - old_elem);
1078}
Harald Welte96e2a002017-06-12 21:44:18 +02001079
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001080static bool same_cell_id_list_entries(const struct gsm0808_cell_id_list2 *a, int ai,
1081 const struct gsm0808_cell_id_list2 *b, int bi)
1082{
1083 struct gsm0808_cell_id_list2 tmp = {
1084 .id_discr = a->id_discr,
1085 .id_list_len = 1,
1086 };
1087 uint8_t buf_a[32 + sizeof(struct msgb)];
1088 uint8_t buf_b[32 + sizeof(struct msgb)];
1089 struct msgb *msg_a = (void*)buf_a;
1090 struct msgb *msg_b = (void*)buf_b;
1091
1092 msg_a->data_len = 32;
1093 msg_b->data_len = 32;
1094 msgb_reset(msg_a);
1095 msgb_reset(msg_b);
1096
1097 if (a->id_discr != b->id_discr)
1098 return false;
1099 if (ai >= a->id_list_len
1100 || bi >= b->id_list_len)
1101 return false;
1102
1103 tmp.id_list[0] = a->id_list[ai];
1104 gsm0808_enc_cell_id_list2(msg_a, &tmp);
1105
1106 tmp.id_list[0] = b->id_list[bi];
1107 gsm0808_enc_cell_id_list2(msg_b, &tmp);
1108
1109 if (msg_a->len != msg_b->len)
1110 return false;
1111 if (memcmp(msg_a->data, msg_b->data, msg_a->len))
1112 return false;
1113
1114 return true;
1115}
1116
1117/*! Append entries from one Cell Identifier List to another.
1118 * The cell identifier types must be identical between the two lists.
1119 * \param dst[out] Append entries to this list.
1120 * \param src[in] Append these entries to \a dst.
1121 * \returns the nr of items added, or negative on error: -EINVAL if the id_discr mismatch
1122 * between the lists, -ENOSPC if the destination list does not have enough space. If an error is
1123 * returned, \a dst may have already been changed (particularly on -ENOSPC). Note that a return value
1124 * of zero may occur when the src->id_list_len is zero, or when all entries from \a src already exist
1125 * in \a dst, and does not indicate error per se. */
1126int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src)
1127{
1128 int i, j;
1129 int added = 0;
1130
1131 if (dst->id_list_len == 0
1132 && dst->id_discr != CELL_IDENT_BSS)
1133 dst->id_discr = src->id_discr;
1134 else if (dst->id_discr != src->id_discr)
1135 return -EINVAL;
1136
1137 for (i = 0; i < src->id_list_len; i++) {
1138 /* don't add duplicate entries */
1139 bool skip = false;
1140 for (j = 0; j < dst->id_list_len; j++) {
1141 if (same_cell_id_list_entries(dst, j, src, i)) {
1142 skip = true;
1143 break;
1144 }
1145 }
1146 if (skip)
1147 continue;
1148
1149 if (dst->id_list_len >= ARRAY_SIZE(dst->id_list))
1150 return -ENOSPC;
1151
1152 dst->id_list[dst->id_list_len++] = src->id_list[i];
1153 added ++;
1154 }
1155
1156 return added;
1157}
1158
Neels Hofmeyr38e58412018-05-25 16:56:35 +02001159/*! Convert a single Cell Identifier to a Cell Identifier List with one entry.
1160 * \param dst[out] Overwrite this list.
1161 * \param src[in] Set \a dst to contain exactly this item.
1162 */
1163void gsm0808_cell_id_to_list(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id *src)
1164{
1165 if (!dst)
1166 return;
1167 if (!src) {
1168 *dst = (struct gsm0808_cell_id_list2){
1169 .id_discr = CELL_IDENT_NO_CELL,
1170 };
1171 return;
1172 }
1173
1174 *dst = (struct gsm0808_cell_id_list2){
1175 .id_discr = src->id_discr,
1176 .id_list = { src->id },
1177 .id_list_len = 1,
1178 };
1179
1180 switch (src->id_discr) {
1181 case CELL_IDENT_NO_CELL:
1182 case CELL_IDENT_BSS:
1183 dst->id_list_len = 0;
1184 break;
1185 default:
1186 break;
1187 }
1188}
1189
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001190/*! Encode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1191 * \param[out] msg Message Buffer to which IE is to be appended
1192 * \param[in] ci Cell ID to be encoded
1193 * \returns number of bytes appended to \a msg */
1194uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci)
1195{
1196 uint8_t rc;
1197 uint8_t *ie_tag;
1198 struct gsm0808_cell_id_list2 cil = {
1199 .id_discr = ci->id_discr,
1200 .id_list = { ci->id },
1201 .id_list_len = 1,
1202 };
1203
1204 OSMO_ASSERT(msg);
1205 OSMO_ASSERT(ci);
1206
1207 ie_tag = msg->tail;
1208 rc = gsm0808_enc_cell_id_list2(msg, &cil);
1209
1210 if (rc <= 0)
1211 return rc;
1212
1213 *ie_tag = GSM0808_IE_CELL_IDENTIFIER;
1214 return rc;
1215}
1216
1217/*! Decode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1218 * \param[out] ci Caller-provided memory to store Cell ID.
1219 * \param[in] elem IE value to be decoded.
1220 * \param[in] len Length of \a elem in bytes.
1221 * \returns number of bytes parsed; negative on error */
1222int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len)
1223{
1224 struct gsm0808_cell_id_list2 cil;
1225 int rc;
1226 rc = gsm0808_dec_cell_id_list2(&cil, elem, len);
1227 if (rc < 0)
1228 return rc;
1229 if (cil.id_discr == CELL_IDENT_BSS || cil.id_discr == CELL_IDENT_NO_CELL) {
1230 if (cil.id_list_len != 0)
1231 return -EINVAL;
1232 } else {
1233 if (cil.id_list_len != 1)
1234 return -EINVAL;
1235 }
1236 ci->id_discr = cil.id_discr;
1237 ci->id = cil.id_list[0];
1238 return rc;
1239}
1240
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001241/*! Convert the representation of the permitted speech codec identifier
Philipp Maier3149b0d2017-06-02 13:22:34 +02001242 * that is used in struct gsm0808_channel_type to the speech codec
1243 * representation we use in struct gsm0808_speech_codec.
1244 * \param[in] perm_spch to be converted (see also gsm0808_permitted_speech)
1245 * \returns GSM speech codec type; negative on error */
1246int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch)
1247{
1248 /*! The speech codec type, which is used in the channel type field to
1249 * signal the permitted speech versions (codecs) has a different
1250 * encoding than the type field in the speech codec type element
1251 * (See also 3GPP TS 48.008, 3.2.2.11 and 3.2.2.103) */
1252
1253 switch (perm_spch) {
1254 case GSM0808_PERM_FR1:
1255 return GSM0808_SCT_FR1;
1256 case GSM0808_PERM_FR2:
1257 return GSM0808_SCT_FR2;
1258 case GSM0808_PERM_FR3:
1259 return GSM0808_SCT_FR3;
1260 case GSM0808_PERM_FR4:
1261 return GSM0808_SCT_FR4;
1262 case GSM0808_PERM_FR5:
1263 return GSM0808_SCT_FR5;
1264 case GSM0808_PERM_HR1:
1265 return GSM0808_SCT_HR1;
1266 case GSM0808_PERM_HR3:
1267 return GSM0808_SCT_HR3;
1268 case GSM0808_PERM_HR4:
1269 return GSM0808_SCT_HR4;
1270 case GSM0808_PERM_HR6:
1271 return GSM0808_SCT_HR6;
1272 }
1273
1274 /* Invalid input */
1275 return -EINVAL;
1276}
1277
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001278/*! Extrapolate a speech codec field from a given permitted speech
Philipp Maier884ba0f2017-06-02 13:49:16 +02001279 * parameter (channel type).
1280 * \param[out] sc Caller provided memory to store the resulting speech codec
1281 * \param[in] perm_spch value that is used to derive the speech codec info
1282 * (see also: enum gsm0808_speech_codec_type in gsm0808_utils.h)
1283 * \returns zero when successful; negative on error */
1284int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc,
1285 uint8_t perm_spch)
1286{
1287 int rc;
1288
1289 memset(sc, 0, sizeof(*sc));
1290
1291 /* Determine codec type */
1292 rc = gsm0808_chan_type_to_speech_codec(perm_spch);
1293 if (rc < 0)
1294 return -EINVAL;
1295 sc->type = (uint8_t) rc;
1296
1297 /* Depending on the speech codec type, pick a default codec
1298 * configuration that exactly matches the configuration on the
1299 * air interface. */
1300 switch (sc->type) {
1301 case GSM0808_SCT_FR3:
1302 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR;
1303 break;
1304 case GSM0808_SCT_FR4:
1305 sc->cfg = GSM0808_SC_CFG_DEFAULT_OFR_AMR_WB;
1306 break;
1307 case GSM0808_SCT_FR5:
1308 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR_WB;
1309 break;
1310 case GSM0808_SCT_HR3:
1311 sc->cfg = GSM0808_SC_CFG_DEFAULT_HR_AMR;
1312 break;
1313 case GSM0808_SCT_HR4:
1314 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR_WB;
1315 break;
1316 case GSM0808_SCT_HR6:
1317 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR;
1318 break;
1319 default:
1320 /* Note: Not all codec types specify a default setting,
1321 * in this case, we just set the field to zero. */
1322 sc->cfg = 0;
1323 }
1324
1325 /* Tag all codecs as "Full IP"
1326 * (see als 3GPP TS 48.008 3.2.2.103) */
1327 sc->fi = true;
1328
1329 return 0;
1330}
1331
Philipp Maier5f2eb152018-09-19 13:40:21 +02001332/*! Determine a set of AMR speech codec configuration bits (S0-S15) from a
1333 * given GSM 04.08 AMR configuration struct.
1334 * \param[in] cfg AMR configuration in GSM 04.08 format.
1335 * \param[in] hint if the resulting configuration shall be used with a FR or HR TCH.
1336 * \returns configuration bits (S0-S15) */
Philipp Maier369015c2018-09-21 09:07:20 +02001337uint16_t gsm0808_sc_cfg_from_gsm48_mr_cfg(const struct gsm48_multi_rate_conf *cfg,
Philipp Maier5f2eb152018-09-19 13:40:21 +02001338 bool fr)
1339{
1340 uint16_t s15_s0 = 0;
1341
1342 /* Check each rate bit in the AMR multirate configuration and pick the
1343 * matching default configuration as specified in 3GPP TS 28.062,
1344 * Table 7.11.3.1.3-2. */
1345 if (cfg->m4_75)
1346 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_4_75;
1347 if (cfg->m5_15)
1348 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_15;
1349 if (cfg->m5_90)
1350 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_90;
1351 if (cfg->m6_70)
1352 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_6_70;
1353 if (cfg->m7_40)
1354 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_40;
1355 if (cfg->m7_95)
1356 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_95;
1357 if (cfg->m10_2)
1358 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_10_2;
1359 if (cfg->m12_2)
1360 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_12_2;
1361
1362 /* Note: 3GPP TS 48.008, chapter 3GPP TS 48.008 states that for AMR
1363 * some of the configuration bits must be coded as zeros. The applied
1364 * bitmask matches the default codec settings. See also the definition
1365 * of enum gsm0808_speech_codec_defaults in gsm_08_08.h and
1366 * 3GPP TS 28.062, Table 7.11.3.1.3-2. */
1367 if (fr)
1368 s15_s0 &= GSM0808_SC_CFG_DEFAULT_FR_AMR;
1369 else
1370 s15_s0 &= GSM0808_SC_CFG_DEFAULT_HR_AMR;
1371
Philipp Maier94d79fd2019-03-01 10:40:48 +01001372 /* The mode that is encoded by S1 (Config-NB-Code = 1), takes a special
1373 * role as it does not stand for a single rate, but for up to four rates
1374 * at once (12.2, 7.4, 5.9, 4.75). We must check if the supplied cfg
1375 * covers this mode. If not, we need to make sure that the related
1376 * bit is removed. (See also 3GPP TS 28.062, Table 7.11.3.1.3-2) */
1377 if (!(cfg->m12_2 && cfg->m7_40 && cfg->m5_90 && cfg->m4_75) && fr)
1378 s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20;
1379 else if (!(cfg->m7_40 && cfg->m5_90 && cfg->m4_75))
1380 s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20;
1381
Philipp Maier5f2eb152018-09-19 13:40:21 +02001382 return s15_s0;
1383}
1384
Philipp Maier8515d032018-09-25 15:57:49 +02001385/*! Determine a GSM 04.08 AMR configuration struct from a set of speech codec
1386 * configuration bits (S0-S15)
1387 * \param[out] cfg AMR configuration in GSM 04.08 format.
Philipp Maier3713af82019-02-27 16:48:25 +01001388 * \param[in] s15_s0 configuration bits (S15-S0, non-ambiguous).
1389 * \returns zero when successful; negative on error */
1390int gsm48_mr_cfg_from_gsm0808_sc_cfg(struct gsm48_multi_rate_conf *cfg,
1391 uint16_t s15_s0)
Philipp Maier8515d032018-09-25 15:57:49 +02001392{
Philipp Maier3713af82019-02-27 16:48:25 +01001393 unsigned int count = 0;
1394
1395 /* Note: See also: 3GPP TS 28.062
1396 * Table 7.11.3.1.3-2: Preferred Configurations for the Adaptive
1397 * Multi-Rate Codec Types */
1398
1399 /* Note: The resulting multirate-configuration must not contain an
1400 * active set of more than four codec rates. The active set also
1401 * must contain at least one rate. */
1402
Philipp Maier8515d032018-09-25 15:57:49 +02001403 memset(cfg, 0, sizeof(*cfg));
Philipp Maier3713af82019-02-27 16:48:25 +01001404 cfg->ver = 1;
1405 cfg->icmi = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001406
1407 /* Strip option bits */
1408 s15_s0 &= 0x00ff;
1409
Philipp Maier3713af82019-02-27 16:48:25 +01001410 /* Rate 5,15k can never be selected (see table) */
1411 cfg->m5_15 = 0;
Philipp Maier8515d032018-09-25 15:57:49 +02001412
Philipp Maier3713af82019-02-27 16:48:25 +01001413 if (s15_s0 & GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20 & 0xff) {
1414 /* Table Table 7.11.3.1.3-2 lists one mode that selects 4
1415 * rates at once (Config-NB-Code = 1). The rates selected
1416 * are known to be compatible between GERAN and UTRAN, since
1417 * an active set must not contain more than four rates at
1418 * a time, we ignore all other settings as they are either
1419 * redundaned or excess settings (invalid) */
Philipp Maier8515d032018-09-25 15:57:49 +02001420 cfg->m4_75 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001421 cfg->m5_90 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001422 cfg->m7_40 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001423 cfg->m12_2 = 1;
Philipp Maier3713af82019-02-27 16:48:25 +01001424 count += 4;
1425 }
Philipp Maier8515d032018-09-25 15:57:49 +02001426
Philipp Maier3713af82019-02-27 16:48:25 +01001427 /* Check the bits in s15_s0 and set the flags for the
1428 * respective rates. */
1429 if (s15_s0 & GSM0808_SC_CFG_AMR_4_75 && !cfg->m4_75) {
1430 if (count >= 4)
1431 return -EINVAL;
1432 cfg->m4_75 = 1;
1433 count++;
1434 }
1435 if (s15_s0 & GSM0808_SC_CFG_AMR_5_90 && !cfg->m5_90) {
1436 if (count >= 4)
1437 return -EINVAL;
1438 cfg->m5_90 = 1;
1439 count++;
1440 }
1441 if (s15_s0 & GSM0808_SC_CFG_AMR_6_70) {
1442 if (count >= 4)
1443 return -EINVAL;
1444 cfg->m6_70 = 1;
1445 count++;
1446 }
1447 if (s15_s0 & GSM0808_SC_CFG_AMR_7_40 && !cfg->m7_40) {
1448 if (count >= 4)
1449 return -EINVAL;
1450 cfg->m7_40 = 1;
1451 count++;
1452 }
1453 if (s15_s0 & GSM0808_SC_CFG_AMR_7_95) {
1454 if (count >= 4)
1455 return -EINVAL;
1456 cfg->m7_95 = 1;
1457 count++;
1458 }
1459 if (s15_s0 & GSM0808_SC_CFG_AMR_10_2) {
1460 if (count >= 4)
1461 return -EINVAL;
1462 cfg->m10_2 = 1;
1463 count++;
1464 }
1465 if (s15_s0 & GSM0808_SC_CFG_AMR_12_2 && !cfg->m12_2) {
1466 if (count >= 4)
1467 return -EINVAL;
1468 cfg->m12_2 = 1;
1469 count++;
1470 }
1471
1472 if (count == 0)
1473 return -EINVAL;
1474
1475 return 0;
Philipp Maier8515d032018-09-25 15:57:49 +02001476}
1477
Maxed651d22018-11-07 15:25:05 +01001478int gsm0808_get_cipher_reject_cause(const struct tlv_parsed *tp)
1479{
1480 const uint8_t *buf = TLVP_VAL_MINLEN(tp, GSM0808_IE_CAUSE, 1);
1481
1482 if (!buf)
1483 return -EBADMSG;
1484
1485 if (TLVP_LEN(tp, GSM0808_IE_CAUSE) > 1) {
1486 if (!gsm0808_cause_ext(buf[0]))
1487 return -EINVAL;
1488 return buf[1];
1489 }
1490
1491 return buf[0];
1492}
1493
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001494/*! Print a human readable name of the cell identifier to the char buffer.
1495 * This is useful both for struct gsm0808_cell_id and struct gsm0808_cell_id_list2.
1496 * See also gsm0808_cell_id_name() and gsm0808_cell_id_list_name().
1497 * \param[out] buf Destination buffer to write string representation to.
1498 * \param[in] buflen Amount of memory available in \a buf.
1499 * \param[in] id_discr Cell Identifier type.
1500 * \param[in] u Cell Identifer value.
1501 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1502 * or that would have been written if the buffer were large enough.
1503 */
1504int gsm0808_cell_id_u_name(char *buf, size_t buflen,
1505 enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
1506{
1507 switch (id_discr) {
1508 case CELL_IDENT_LAC:
1509 return snprintf(buf, buflen, "%u", u->lac);
1510 case CELL_IDENT_CI:
1511 return snprintf(buf, buflen, "%u", u->ci);
1512 case CELL_IDENT_LAC_AND_CI:
1513 return snprintf(buf, buflen, "%u-%u", u->lac_and_ci.lac, u->lac_and_ci.ci);
1514 case CELL_IDENT_LAI_AND_LAC:
1515 return snprintf(buf, buflen, "%s", osmo_lai_name(&u->lai_and_lac));
1516 case CELL_IDENT_WHOLE_GLOBAL:
1517 return snprintf(buf, buflen, "%s", osmo_cgi_name(&u->global));
1518 default:
1519 /* For CELL_IDENT_BSS and CELL_IDENT_NO_CELL, just print the discriminator.
1520 * Same for kinds we have no string representation of yet. */
1521 return snprintf(buf, buflen, "%s", gsm0808_cell_id_discr_name(id_discr));
1522 }
1523}
1524
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02001525/* Store individual Cell Identifier information in a CGI, without clearing the remaining ones.
1526 * This is useful to supplement one CGI with information from more than one Cell Identifier,
1527 * which in turn is useful to match Cell Identifiers of differing kinds to each other.
1528 * Before first invocation, clear the *dst struct externally, this function does only write those members
1529 * that are present in parameter u.
1530 */
1531static void cell_id_to_cgi(struct osmo_cell_global_id *dst,
1532 enum CELL_IDENT discr, const union gsm0808_cell_id_u *u)
1533{
1534 switch (discr) {
1535 case CELL_IDENT_WHOLE_GLOBAL:
1536 *dst = u->global;
1537 return;
1538
1539 case CELL_IDENT_LAC_AND_CI:
1540 dst->lai.lac = u->lac_and_ci.lac;
1541 dst->cell_identity = u->lac_and_ci.ci;
1542 return;
1543
1544 case CELL_IDENT_CI:
1545 dst->cell_identity = u->ci;
1546 return;
1547
1548 case CELL_IDENT_LAI_AND_LAC:
1549 dst->lai = u->lai_and_lac;
1550 return;
1551
1552 case CELL_IDENT_LAC:
1553 dst->lai.lac = u->lac;
1554 return;
1555
1556 case CELL_IDENT_NO_CELL:
1557 case CELL_IDENT_BSS:
1558 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1559 case CELL_IDENT_UTRAN_RNC:
1560 case CELL_IDENT_UTRAN_LAC_RNC:
1561 /* No values to set. */
1562 return;
1563 }
1564}
1565
1566/*! Return true if the common information between the two Cell Identifiers match.
1567 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1568 * Note that CELL_IDENT_NO_CELL will always return false.
1569 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1570 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1571 * \param[in] discr1 Cell Identifier type.
1572 * \param[in] u1 Cell Identifier value.
1573 * \param[in] discr2 Other Cell Identifier type.
1574 * \param[in] u2 Other Cell Identifier value.
1575 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1576 * \returns True if the common fields of the above match.
1577 */
1578static bool gsm0808_cell_id_u_match(enum CELL_IDENT discr1, const union gsm0808_cell_id_u *u1,
1579 enum CELL_IDENT discr2, const union gsm0808_cell_id_u *u2,
1580 bool exact_match)
1581{
1582 struct osmo_cell_global_id a = {};
1583 struct osmo_cell_global_id b = {};
1584
1585 if (exact_match && discr1 != discr2)
1586 return false;
1587
1588 /* First handle the odd wildcard like CELL_IDENT kinds. We can't really match any of these. */
1589 switch (discr1) {
1590 case CELL_IDENT_NO_CELL:
1591 case CELL_IDENT_BSS:
1592 return discr1 == discr2;
1593 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1594 case CELL_IDENT_UTRAN_RNC:
1595 case CELL_IDENT_UTRAN_LAC_RNC:
1596 return false;
1597 default:
1598 break;
1599 }
1600 switch (discr2) {
1601 case CELL_IDENT_NO_CELL:
1602 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1603 case CELL_IDENT_UTRAN_RNC:
1604 case CELL_IDENT_UTRAN_LAC_RNC:
1605 case CELL_IDENT_BSS:
1606 return false;
1607 default:
1608 break;
1609 }
1610
1611 /* Enrich both sides to full CGI, then compare those. First set the *other* ID's values in case
1612 * they assign more items. For example:
1613 * u1 = LAC:42
1614 * u2 = LAC+CI:23+5
1615 * 1) a <- LAC+CI:23+5
1616 * 2) a <- LAC:42 so that a = LAC+CI:42+5
1617 * Now we can compare those two and find a mismatch. If the LAC were the same, we would get
1618 * identical LAC+CI and hence a match. */
1619
1620 cell_id_to_cgi(&a, discr2, u2);
1621 cell_id_to_cgi(&a, discr1, u1);
1622
1623 cell_id_to_cgi(&b, discr1, u1);
1624 cell_id_to_cgi(&b, discr2, u2);
1625
1626 return osmo_cgi_cmp(&a, &b) == 0;
1627}
1628
1629/*! Return true if the common information between the two Cell Identifiers match.
1630 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1631 * Note that CELL_IDENT_NO_CELL will always return false.
1632 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1633 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1634 * \param[in] id1 Cell Identifier.
1635 * \param[in] id2 Other Cell Identifier.
1636 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1637 * \returns True if the common fields of the above match.
1638 */
1639bool gsm0808_cell_ids_match(const struct gsm0808_cell_id *id1, const struct gsm0808_cell_id *id2, bool exact_match)
1640{
1641 return gsm0808_cell_id_u_match(id1->id_discr, &id1->id, id2->id_discr, &id2->id, exact_match);
1642}
1643
1644/*! Find an index in a Cell Identifier list that matches a given single Cell Identifer.
1645 * Compare \a id against each entry in \a list using gsm0808_cell_ids_match(), and return the list index
1646 * if a match is found. \a match_nr allows iterating all matches in the list. A match_nr <= 0 returns the
1647 * first match in the list, match_nr == 1 the second match, etc., and if match_nr exceeds the available
1648 * matches in the list, -1 is returned.
1649 * \param[in] id Cell Identifier to match.
1650 * \param[in] list Cell Identifier list to search in.
1651 * \param[in] match_nr Ignore this many matches.
1652 * \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 +01001653 * \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
1654 * entry).
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02001655 */
1656int gsm0808_cell_id_matches_list(const struct gsm0808_cell_id *id, const struct gsm0808_cell_id_list2 *list,
1657 unsigned int match_nr, bool exact_match)
1658{
1659 int i;
1660 for (i = 0; i < list->id_list_len; i++) {
1661 if (gsm0808_cell_id_u_match(id->id_discr, &id->id, list->id_discr, &list->id_list[i], exact_match)) {
1662 if (match_nr)
1663 match_nr--;
1664 else
1665 return i;
1666 }
1667 }
1668 return -1;
1669}
1670
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001671/*! Copy information from a CGI to form a Cell Identifier of the specified kind.
1672 * \param [out] cid Compose new Cell Identifier here.
1673 * \param [in] id_discr Which kind of Cell Identifier to compose.
1674 * \param [in] cgi Cell Global Identifier to form the Cell Identifier from.
1675 */
1676void gsm0808_cell_id_from_cgi(struct gsm0808_cell_id *cid, enum CELL_IDENT id_discr,
1677 const struct osmo_cell_global_id *cgi)
1678{
1679 *cid = (struct gsm0808_cell_id){
1680 .id_discr = id_discr,
1681 };
1682
1683 switch (id_discr) {
1684 case CELL_IDENT_WHOLE_GLOBAL:
1685 cid->id.global = *cgi;
1686 return;
1687
1688 case CELL_IDENT_LAC_AND_CI:
1689 cid->id.lac_and_ci = (struct osmo_lac_and_ci_id){
1690 .lac = cgi->lai.lac,
1691 .ci = cgi->cell_identity,
1692 };
1693 return;
1694
1695 case CELL_IDENT_CI:
1696 cid->id.ci = cgi->cell_identity;
1697 return;
1698
1699 case CELL_IDENT_LAI:
1700 cid->id.lai_and_lac = cgi->lai;
1701 return;
1702
1703 case CELL_IDENT_LAC:
1704 cid->id.lac = cgi->lai.lac;
1705 return;
1706
1707 case CELL_IDENT_NO_CELL:
1708 case CELL_IDENT_BSS:
1709 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1710 case CELL_IDENT_UTRAN_RNC:
1711 case CELL_IDENT_UTRAN_LAC_RNC:
1712 default:
1713 return;
1714 };
1715}
1716
1717/*! Overwrite parts of cgi with values from a Cell Identifier.
1718 * Place only those items given in cid into cgi, leaving other values unchanged.
1719 * \param[out] cgi Cell Global Identity to write to.
1720 * \param[in] cid Cell Identity to read from.
1721 * \return a bitmask of items that were set: OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI; 0 if nothing was
1722 * written to cgi.
1723 */
1724int gsm0808_cell_id_to_cgi(struct osmo_cell_global_id *cgi, const struct gsm0808_cell_id *cid)
1725{
1726 switch (cid->id_discr) {
1727 case CELL_IDENT_WHOLE_GLOBAL:
1728 *cgi = cid->id.global;
1729 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
1730
1731 case CELL_IDENT_LAC_AND_CI:
1732 cgi->lai.lac = cid->id.lac_and_ci.lac;
1733 cgi->cell_identity = cid->id.lac_and_ci.ci;
1734 return OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
1735
1736 case CELL_IDENT_CI:
1737 cgi->cell_identity = cid->id.ci;
1738 return OSMO_CGI_PART_CI;
1739
1740 case CELL_IDENT_LAI:
1741 cgi->lai = cid->id.lai_and_lac;
1742 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC;
1743
1744 case CELL_IDENT_LAC:
1745 cgi->lai.lac = cid->id.lac;
1746 return OSMO_CGI_PART_LAC;
1747
1748 case CELL_IDENT_NO_CELL:
1749 case CELL_IDENT_BSS:
1750 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1751 case CELL_IDENT_UTRAN_RNC:
1752 case CELL_IDENT_UTRAN_LAC_RNC:
1753 default:
1754 return 0;
1755 };
1756}
1757
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001758/*! value_string[] for enum CELL_IDENT. */
1759const struct value_string gsm0808_cell_id_discr_names[] = {
1760 { CELL_IDENT_WHOLE_GLOBAL, "CGI" },
1761 { CELL_IDENT_LAC_AND_CI, "LAC-CI" },
1762 { CELL_IDENT_CI, "CI" },
1763 { CELL_IDENT_NO_CELL, "NO-CELL" },
1764 { CELL_IDENT_LAI_AND_LAC, "LAI" },
1765 { CELL_IDENT_LAC, "LAC" },
1766 { CELL_IDENT_BSS, "BSS" },
1767 { CELL_IDENT_UTRAN_PLMN_LAC_RNC, "UTRAN-PLMN-LAC-RNC" },
1768 { CELL_IDENT_UTRAN_RNC, "UTRAN-RNC" },
1769 { CELL_IDENT_UTRAN_LAC_RNC, "UTRAN-LAC-RNC" },
1770 { 0, NULL }
1771};
1772
1773#define APPEND_THING(func, args...) do { \
1774 int remain = buflen - (pos - buf); \
1775 int l = func(pos, remain, ##args); \
1776 if (l < 0 || l > remain) \
1777 pos = buf + buflen; \
1778 else \
1779 pos += l; \
1780 if (l > 0) \
1781 total_len += l; \
1782 } while(0)
1783#define APPEND_STR(fmt, args...) APPEND_THING(snprintf, fmt, ##args)
1784#define APPEND_CELL_ID_U(DISCR, U) APPEND_THING(gsm0808_cell_id_u_name, DISCR, U)
1785
Harald Welte179f3572019-03-18 18:38:47 +01001786char *gsm0808_cell_id_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id *cid)
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001787{
1788 char *pos = buf;
1789 int total_len = 0;
1790 APPEND_STR("%s:", gsm0808_cell_id_discr_name(cid->id_discr));
1791 APPEND_CELL_ID_U(cid->id_discr, &cid->id);
1792 return buf;
1793}
1794
1795/*! Return a human readable representation of a Cell Identifier, like "LAC:123"
1796 * or "CGI:001-01-42-23".
1797 * \param[in] cid Cell Identifer.
1798 * \returns String in a static buffer.
1799 */
1800const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid)
1801{
1802 static char buf[64];
Harald Welte179f3572019-03-18 18:38:47 +01001803 return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001804}
1805
1806/*! Like gsm0808_cell_id_name() but uses a different static buffer.
1807 * \param[in] cid Cell Identifer.
1808 * \returns String in a static buffer.
1809 */
1810const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid)
1811{
1812 static char buf[64];
Harald Welte179f3572019-03-18 18:38:47 +01001813 return gsm0808_cell_id_name_buf(buf, sizeof(buf), cid);
1814}
1815
1816char *gsm0808_cell_id_name_c(const void *ctx, const struct gsm0808_cell_id *cid)
1817{
1818 char *buf = talloc_size(ctx, 64);
1819 if (!buf)
1820 return NULL;
1821 return gsm0808_cell_id_name_buf(buf, 64, cid);
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001822}
1823
1824/*! Return a human readable representation of the Cell Identifier List, like
1825 * "LAC[2]:{123, 456}".
1826 * The return value semantics are like snprintf() and thus allow ensuring a complete
1827 * untruncated string by determining the required string length from the return value.
1828 * If buflen > 0, always nul-terminate the string in buf, also when it is truncated.
1829 * If buflen == 0, do not modify buf, just return the would-be length.
1830 * \param[out] buf Destination buffer to write string representation to.
1831 * \param[in] buflen Amount of memory available in \a buf.
1832 * \param[in] cil Cell Identifer List.
1833 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1834 * or that would have been written if the buffer were large enough.
1835 */
1836int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil)
1837{
1838 char *pos = buf;
1839 int total_len = 0;
1840 int i;
1841
1842 APPEND_STR("%s[%u]", gsm0808_cell_id_discr_name(cil->id_discr), cil->id_list_len);
1843
1844 switch (cil->id_discr) {
1845 case CELL_IDENT_BSS:
1846 case CELL_IDENT_NO_CELL:
1847 return total_len;
1848 default:
1849 break;
1850 }
1851
1852 APPEND_STR(":{");
1853
1854 for (i = 0; i < cil->id_list_len; i++) {
1855 if (i)
1856 APPEND_STR(", ");
1857 APPEND_CELL_ID_U(cil->id_discr, &cil->id_list[i]);
1858 }
1859
1860 APPEND_STR("}");
1861 return total_len;
1862}
1863
1864/*! Return a human-readable representation of \a cil in a static buffer.
1865 * If the list is too long, the output may be truncated.
1866 * See also gsm0808_cell_id_list_name_buf(). */
1867const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil)
1868{
1869 static char buf[1024];
1870 gsm0808_cell_id_list_name_buf(buf, sizeof(buf), cil);
1871 return buf;
1872}
1873
Harald Welte179f3572019-03-18 18:38:47 +01001874char *gsm0808_cell_id_list_name_c(const void *ctx, const struct gsm0808_cell_id_list2 *cil)
1875{
1876 char *buf = talloc_size(ctx, 1024);
1877 if (!buf)
1878 return NULL;
1879 gsm0808_cell_id_list_name_buf(buf, 1024, cil);
1880 return buf;
1881}
1882
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001883#undef APPEND_STR
1884#undef APPEND_CELL_ID_U
1885
Harald Welte4a62eda2019-03-18 18:27:00 +01001886char *gsm0808_channel_type_name_buf(char *buf, size_t buf_len, const struct gsm0808_channel_type *ct)
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02001887{
Harald Welte4a62eda2019-03-18 18:27:00 +01001888 snprintf(buf, buf_len, "ch_indctr=0x%x ch_rate_type=0x%x perm_spch=%s",
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02001889 ct->ch_indctr, ct->ch_rate_type,
1890 osmo_hexdump(ct->perm_spch, ct->perm_spch_len));
1891 return buf;
1892}
1893
Harald Welte4a62eda2019-03-18 18:27:00 +01001894const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct)
1895{
1896 static char buf[128];
1897 return gsm0808_channel_type_name_buf(buf, sizeof(buf), ct);
1898}
1899
Harald Welte179f3572019-03-18 18:38:47 +01001900char *gsm0808_channel_type_name_c(const void *ctx, const struct gsm0808_channel_type *ct)
1901{
1902 char *buf = talloc_size(ctx, 128);
1903 if (!buf)
1904 return NULL;
1905 return gsm0808_channel_type_name_buf(buf, 128, ct);
1906}
1907
Harald Welte96e2a002017-06-12 21:44:18 +02001908/*! @} */