blob: e0cdaaf6e5a2e4297dae38e6892ec1d784e8665f [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.
598 * \param[in] lcls pointer to the struct to print.
599 * \returns string representation of LCLS or NULL on error. */
600char *osmo_lcls_dump(const struct osmo_lcls *lcls)
601{
602 struct osmo_strbuf s = { .buf = dbuf, .len = 256 };
603
604 if (!lcls)
605 return NULL;
606
607 OSMO_STRBUF_PRINTF(s, "LCLS Config: %s, Control: %s, Correlation-Needed: %u",
608 gsm0808_lcls_config_name(lcls->config),
609 gsm0808_lcls_control_name(lcls->control),
610 lcls->corr_needed);
611
612 return dbuf;
613}
614
615/*! Dump GCR struct into string for printing.
616 * \param[in] lcls pointer to the struct to print.
617 * \returns string representation of GCR or NULL on error. */
618char *osmo_gcr_dump(const struct osmo_lcls *lcls)
619{
620 struct osmo_strbuf s = { .buf = dbuf, .len = 256 };
621
622 if (!lcls)
623 return NULL;
624
625 if (lcls->gcr_available) {
626 OSMO_STRBUF_PRINTF(s, "GCR NetID 0x%s, ", osmo_hexdump_nospc(lcls->gcr.net, lcls->gcr.net_len));
627 /* osmo_hexdump() uses static buffers so we can't call it twice withing the same parameter list */
628 OSMO_STRBUF_PRINTF(s, "Node 0x%x, CallRefID 0x%s", lcls->gcr.node, osmo_hexdump_nospc(lcls->gcr.cr, 5));
629 }
630
631 return dbuf;
632}
633
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200634/*! Encode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200635 * \param[out] msg Message Buffer to which IE is to be appended
636 * \param[in] ei Encryption Information to be encoded
637 * \returns number of bytes appended to \a msg */
Philipp Maier14e76b92017-03-28 18:36:52 +0200638uint8_t gsm0808_enc_encrypt_info(struct msgb *msg,
639 const struct gsm0808_encrypt_info *ei)
640{
641 unsigned int i;
642 uint8_t perm_algo = 0;
643 uint8_t *ptr;
644 uint8_t *old_tail;
645 uint8_t *tlv_len;
646
647 OSMO_ASSERT(msg);
648 OSMO_ASSERT(ei);
649 OSMO_ASSERT(ei->key_len <= ARRAY_SIZE(ei->key));
650 OSMO_ASSERT(ei->perm_algo_len <= ENCRY_INFO_PERM_ALGO_MAXLEN);
651
652 msgb_put_u8(msg, GSM0808_IE_ENCRYPTION_INFORMATION);
653 tlv_len = msgb_put(msg, 1);
654 old_tail = msg->tail;
655
656 for (i = 0; i < ei->perm_algo_len; i++) {
657 /* Note: gsm_08_08.h defines the permitted algorithms
658 * as an enum which ranges from 0x01 to 0x08 */
659 OSMO_ASSERT(ei->perm_algo[i] != 0);
660 OSMO_ASSERT(ei->perm_algo[i] <= ENCRY_INFO_PERM_ALGO_MAXLEN);
661 perm_algo |= (1 << (ei->perm_algo[i] - 1));
662 }
663
664 msgb_put_u8(msg, perm_algo);
665 ptr = msgb_put(msg, ei->key_len);
666 memcpy(ptr, ei->key, ei->key_len);
667
668 *tlv_len = (uint8_t) (msg->tail - old_tail);
669 return *tlv_len + 2;
670}
671
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200672/*! Decode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200673 * \param[out] ei Caller-provided memory to store encryption information
674 * \param[in] elem IE value to be decoded
675 * \param[in] len Length of \a elem in bytes
676 * \returns number of bytes parsed; negative on error */
Philipp Maier14e76b92017-03-28 18:36:52 +0200677int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei,
678 const uint8_t *elem, uint8_t len)
679{
680 uint8_t perm_algo;
681 unsigned int i;
682 unsigned int perm_algo_len = 0;
683 const uint8_t *old_elem = elem;
684
685 OSMO_ASSERT(ei);
686 if (!elem)
687 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200688 if (len == 0)
Philipp Maier14e76b92017-03-28 18:36:52 +0200689 return -EINVAL;
690
691 memset(ei, 0, sizeof(*ei));
692
693 perm_algo = *elem;
694 elem++;
695
696 for (i = 0; i < ENCRY_INFO_PERM_ALGO_MAXLEN; i++) {
697 if (perm_algo & (1 << i)) {
698 ei->perm_algo[perm_algo_len] = i + 1;
699 perm_algo_len++;
700 }
701 }
702 ei->perm_algo_len = perm_algo_len;
703
704 ei->key_len = len - 1;
705 memcpy(ei->key, elem, ei->key_len);
706 elem+=ei->key_len;
707
708 return (int)(elem - old_elem);
709}
Philipp Maier783047e2017-03-29 11:35:50 +0200710
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200711/*! Encode TS 08.08 Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +0200712 * \param[out] msg Message Buffer to which IE is to be appended
713 * \param[in] cil Cell ID List to be encoded
714 * \returns number of bytes appended to \a msg */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100715uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg,
716 const struct gsm0808_cell_id_list2 *cil)
717{
718 uint8_t *old_tail;
719 uint8_t *tlv_len;
720 unsigned int i;
721
722 OSMO_ASSERT(msg);
723 OSMO_ASSERT(cil);
724
725 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
726 tlv_len = msgb_put(msg, 1);
727 old_tail = msg->tail;
728
729 msgb_put_u8(msg, cil->id_discr & 0x0f);
730
731 OSMO_ASSERT(cil->id_list_len <= GSM0808_CELL_ID_LIST2_MAXLEN)
732 switch (cil->id_discr) {
733 case CELL_IDENT_WHOLE_GLOBAL:
734 for (i = 0; i < cil->id_list_len; i++) {
735 const struct osmo_cell_global_id *id = &cil->id_list[i].global;
736 struct gsm48_loc_area_id lai;
Neels Hofmeyr8b8cd932018-03-23 01:47:37 +0100737 gsm48_generate_lai2(&lai, &id->lai);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100738 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
739 msgb_put_u16(msg, id->cell_identity);
740 }
741 break;
742 case CELL_IDENT_LAC_AND_CI:
743 for (i = 0; i < cil->id_list_len; i++) {
744 const struct osmo_lac_and_ci_id *id = &cil->id_list[i].lac_and_ci;
745 msgb_put_u16(msg, id->lac);
746 msgb_put_u16(msg, id->ci);
747 }
748 break;
749 case CELL_IDENT_CI:
750 for (i = 0; i < cil->id_list_len; i++)
751 msgb_put_u16(msg, cil->id_list[i].ci);
752 break;
753 case CELL_IDENT_LAI_AND_LAC:
754 for (i = 0; i < cil->id_list_len; i++) {
755 const struct osmo_location_area_id *id = &cil->id_list[i].lai_and_lac;
756 struct gsm48_loc_area_id lai;
Neels Hofmeyr8b8cd932018-03-23 01:47:37 +0100757 gsm48_generate_lai2(&lai, id);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100758 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
759 }
760 break;
761 case CELL_IDENT_LAC:
762 for (i = 0; i < cil->id_list_len; i++)
763 msgb_put_u16(msg, cil->id_list[i].lac);
764 break;
765 case CELL_IDENT_BSS:
766 case CELL_IDENT_NO_CELL:
767 /* Does not have any list items */
768 break;
769 default:
770 /* Support for other identifier list types is not implemented. */
771 OSMO_ASSERT(false);
772 }
773
774 *tlv_len = (uint8_t) (msg->tail - old_tail);
775 return *tlv_len + 2;
776}
777
778/*! DEPRECATED: Use gsm0808_enc_cell_id_list2 instead.
779 *
780 * Encode TS 08.08 Cell Identifier List IE
781 * \param[out] msg Message Buffer to which IE is to be appended
782 * \param[in] cil Cell ID List to be encoded
783 * \returns number of bytes appended to \a msg */
Philipp Maier783047e2017-03-29 11:35:50 +0200784uint8_t gsm0808_enc_cell_id_list(struct msgb *msg,
785 const struct gsm0808_cell_id_list *cil)
786{
787 uint8_t *old_tail;
788 uint8_t *tlv_len;
789 unsigned int i;
790
791 OSMO_ASSERT(msg);
792 OSMO_ASSERT(cil);
793
794 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
795 tlv_len = msgb_put(msg, 1);
796 old_tail = msg->tail;
797
798 msgb_put_u8(msg, cil->id_discr & 0x0f);
799
800 switch (cil->id_discr) {
801 case CELL_IDENT_LAC:
802 OSMO_ASSERT(cil->id_list_len <= CELL_ID_LIST_LAC_MAXLEN)
803 for (i=0;i<cil->id_list_len;i++) {
804 msgb_put_u16(msg, cil->id_list_lac[i]);
805 }
806 break;
807 case CELL_IDENT_BSS:
808 /* Does not have any list items */
809 break;
810 default:
811 /* FIXME: Implement support for all identifier list elements */
812 OSMO_ASSERT(false);
813 }
814
815 *tlv_len = (uint8_t) (msg->tail - old_tail);
816 return *tlv_len + 2;
817}
818
Stefan Sperling23381452018-03-15 19:38:15 +0100819/* Decode 5-byte LAI list element data (see TS 08.08 3.2.2.27) into MCC/MNC/LAC. */
820static void decode_lai(const uint8_t *data, struct osmo_location_area_id *decoded)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100821{
822 struct gsm48_loc_area_id lai;
823
Stefan Sperling23381452018-03-15 19:38:15 +0100824 /* Copy data to stack to prevent unaligned access in gsm48_decode_lai2(). */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100825 memcpy(&lai, data, sizeof(lai)); /* don't byte swap yet */
826
Stefan Sperling23381452018-03-15 19:38:15 +0100827 gsm48_decode_lai2(&lai, decoded);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100828}
829
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100830static 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 +0100831 size_t *consumed)
832{
833 struct osmo_cell_global_id *id;
834 uint16_t *ci_be;
835 size_t lai_offset;
836 int i = 0;
837 const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*ci_be);
838
839 *consumed = 0;
840 while (remain >= elemlen) {
841 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
842 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100843 id = &cil->id_list[i].global;
Stefan Sperling2873bf12018-03-14 18:38:41 +0100844 lai_offset = i * elemlen;
Stefan Sperling23381452018-03-15 19:38:15 +0100845 decode_lai(&data[lai_offset], &id->lai);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100846 ci_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]);
847 id->cell_identity = osmo_load16be(ci_be);
848 *consumed += elemlen;
849 remain -= elemlen;
850 i++;
851 }
852
853 return i;
854}
855
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100856static 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 +0100857 size_t *consumed)
858{
859 uint16_t *lacp_be, *ci_be;
860 struct osmo_lac_and_ci_id *id;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100861 int i = 0, j = 0;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100862 const size_t elemlen = sizeof(*lacp_be) + sizeof(*ci_be);
863
864 *consumed = 0;
865
866 if (remain < elemlen)
867 return -EINVAL;
868
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100869 lacp_be = (uint16_t *)(&data[j]);
870 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100871 while (remain >= elemlen) {
872 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
873 return -ENOSPC;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100874 id = &cil->id_list[i++].lac_and_ci;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100875 id->lac = osmo_load16be(lacp_be);
876 id->ci = osmo_load16be(ci_be);
877 *consumed += elemlen;
878 remain -= elemlen;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100879 j += elemlen;
880 lacp_be = (uint16_t *)(&data[j]);
881 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100882 }
883
884 return i;
885}
886
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100887static int parse_cell_id_ci_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain,
888 size_t *consumed)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100889{
890 const uint16_t *ci_be = (const uint16_t *)data;
891 int i = 0;
892 const size_t elemlen = sizeof(*ci_be);
893
894 *consumed = 0;
895 while (remain >= elemlen) {
896 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
897 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100898 cil->id_list[i++].ci = osmo_load16be(ci_be++);
Stefan Sperling9c62fc62018-03-16 10:23:34 +0100899 *consumed += elemlen;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100900 remain -= elemlen;
901 }
902 return i;
903}
904
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100905static 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 +0100906 size_t *consumed)
907{
908 struct osmo_location_area_id *id;
909 int i = 0;
910 const size_t elemlen = sizeof(struct gsm48_loc_area_id);
911
912 *consumed = 0;
913 while (remain >= elemlen) {
914 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
915 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100916 id = &cil->id_list[i].lai_and_lac;
Stefan Sperling23381452018-03-15 19:38:15 +0100917 decode_lai(&data[i * elemlen], id);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100918 *consumed += elemlen;
919 remain -= elemlen;
920 i++;
921 }
922
923 return i;
924}
925
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100926static 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 +0100927{
928 const uint16_t *lac_be = (const uint16_t *)data;
929 int i = 0;
930 const size_t elemlen = sizeof(*lac_be);
931
932 *consumed = 0;
933 while (remain >= elemlen) {
934 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
935 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100936 cil->id_list[i++].lac = osmo_load16be(lac_be++);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100937 *consumed += elemlen;
938 remain -= elemlen;
939 }
940 return i;
941}
942
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200943/*! Decode Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +0200944 * \param[out] cil Caller-provided memory to store Cell ID list
945 * \param[in] elem IE value to be decoded
946 * \param[in] len Length of \a elem in bytes
947 * \returns number of bytes parsed; negative on error */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100948int gsm0808_dec_cell_id_list2(struct gsm0808_cell_id_list2 *cil,
949 const uint8_t *elem, uint8_t len)
950{
951 uint8_t id_discr;
952 size_t bytes_elem = 0;
953 int list_len = 0;
954
955 OSMO_ASSERT(cil);
956 if (!elem)
957 return -EINVAL;
958 if (len == 0)
959 return -EINVAL;
960
961 memset(cil, 0, sizeof(*cil));
962
963 id_discr = *elem & 0x0f;
964 elem++;
965 len--;
966
967 switch (id_discr) {
968 case CELL_IDENT_WHOLE_GLOBAL:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100969 list_len = parse_cell_id_global_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100970 break;
971 case CELL_IDENT_LAC_AND_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100972 list_len = parse_cell_id_lac_and_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100973 break;
974 case CELL_IDENT_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100975 list_len = parse_cell_id_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100976 break;
977 case CELL_IDENT_LAI_AND_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100978 list_len = parse_cell_id_lai_and_lac(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100979 break;
980 case CELL_IDENT_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100981 list_len = parse_cell_id_lac_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100982 break;
983 case CELL_IDENT_BSS:
984 case CELL_IDENT_NO_CELL:
985 /* Does not have any list items */
986 break;
987 default:
988 /* Remaining cell identification types are not implemented. */
989 return -EINVAL;
990 }
991
992 if (list_len < 0) /* parsing error */
993 return list_len;
994
995 cil->id_discr = id_discr;
996 cil->id_list_len = list_len;
997
998 /* One byte for the cell ID discriminator + any remaining bytes in
999 * the IE which were consumed by the parser functions above. */
1000 return 1 + (int)bytes_elem;
1001}
1002
1003/*! DEPRECATED: Use gsm0808_dec_cell_id_list2 instead.
1004 *
1005 * Decode Cell Identifier List IE
1006 * \param[out] cil Caller-provided memory to store Cell ID list
1007 * \param[in] elem IE value to be decoded
1008 * \param[in] len Length of \a elem in bytes
1009 * \returns number of bytes parsed; negative on error */
Philipp Maier783047e2017-03-29 11:35:50 +02001010int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil,
1011 const uint8_t *elem, uint8_t len)
1012{
1013 uint8_t id_discr;
1014 const uint8_t *old_elem = elem;
1015 unsigned int item_count = 0;
1016
1017 OSMO_ASSERT(cil);
1018 if (!elem)
1019 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +02001020 if (len == 0)
Philipp Maier783047e2017-03-29 11:35:50 +02001021 return -EINVAL;
1022
1023 memset(cil, 0, sizeof(*cil));
1024
1025 id_discr = *elem & 0x0f;
1026 elem++;
1027 len--;
1028
1029 cil->id_discr = id_discr;
1030
1031 switch (id_discr) {
1032 case CELL_IDENT_LAC:
1033 while (len >= 2) {
1034 cil->id_list_lac[item_count] = osmo_load16be(elem);
1035 elem += 2;
1036 item_count++;
1037 len -= 2;
1038 }
1039 case CELL_IDENT_BSS:
1040 /* Does not have any list items */
1041 break;
1042 default:
1043 /* FIXME: Implement support for all identifier list elements */
1044 return -EINVAL;
1045 }
1046
1047 cil->id_list_len = item_count;
1048 return (int)(elem - old_elem);
1049}
Harald Welte96e2a002017-06-12 21:44:18 +02001050
Neels Hofmeyr74663d92018-03-23 01:46:42 +01001051static bool same_cell_id_list_entries(const struct gsm0808_cell_id_list2 *a, int ai,
1052 const struct gsm0808_cell_id_list2 *b, int bi)
1053{
1054 struct gsm0808_cell_id_list2 tmp = {
1055 .id_discr = a->id_discr,
1056 .id_list_len = 1,
1057 };
1058 uint8_t buf_a[32 + sizeof(struct msgb)];
1059 uint8_t buf_b[32 + sizeof(struct msgb)];
1060 struct msgb *msg_a = (void*)buf_a;
1061 struct msgb *msg_b = (void*)buf_b;
1062
1063 msg_a->data_len = 32;
1064 msg_b->data_len = 32;
1065 msgb_reset(msg_a);
1066 msgb_reset(msg_b);
1067
1068 if (a->id_discr != b->id_discr)
1069 return false;
1070 if (ai >= a->id_list_len
1071 || bi >= b->id_list_len)
1072 return false;
1073
1074 tmp.id_list[0] = a->id_list[ai];
1075 gsm0808_enc_cell_id_list2(msg_a, &tmp);
1076
1077 tmp.id_list[0] = b->id_list[bi];
1078 gsm0808_enc_cell_id_list2(msg_b, &tmp);
1079
1080 if (msg_a->len != msg_b->len)
1081 return false;
1082 if (memcmp(msg_a->data, msg_b->data, msg_a->len))
1083 return false;
1084
1085 return true;
1086}
1087
1088/*! Append entries from one Cell Identifier List to another.
1089 * The cell identifier types must be identical between the two lists.
1090 * \param dst[out] Append entries to this list.
1091 * \param src[in] Append these entries to \a dst.
1092 * \returns the nr of items added, or negative on error: -EINVAL if the id_discr mismatch
1093 * between the lists, -ENOSPC if the destination list does not have enough space. If an error is
1094 * returned, \a dst may have already been changed (particularly on -ENOSPC). Note that a return value
1095 * of zero may occur when the src->id_list_len is zero, or when all entries from \a src already exist
1096 * in \a dst, and does not indicate error per se. */
1097int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src)
1098{
1099 int i, j;
1100 int added = 0;
1101
1102 if (dst->id_list_len == 0
1103 && dst->id_discr != CELL_IDENT_BSS)
1104 dst->id_discr = src->id_discr;
1105 else if (dst->id_discr != src->id_discr)
1106 return -EINVAL;
1107
1108 for (i = 0; i < src->id_list_len; i++) {
1109 /* don't add duplicate entries */
1110 bool skip = false;
1111 for (j = 0; j < dst->id_list_len; j++) {
1112 if (same_cell_id_list_entries(dst, j, src, i)) {
1113 skip = true;
1114 break;
1115 }
1116 }
1117 if (skip)
1118 continue;
1119
1120 if (dst->id_list_len >= ARRAY_SIZE(dst->id_list))
1121 return -ENOSPC;
1122
1123 dst->id_list[dst->id_list_len++] = src->id_list[i];
1124 added ++;
1125 }
1126
1127 return added;
1128}
1129
Neels Hofmeyr38e58412018-05-25 16:56:35 +02001130/*! Convert a single Cell Identifier to a Cell Identifier List with one entry.
1131 * \param dst[out] Overwrite this list.
1132 * \param src[in] Set \a dst to contain exactly this item.
1133 */
1134void gsm0808_cell_id_to_list(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id *src)
1135{
1136 if (!dst)
1137 return;
1138 if (!src) {
1139 *dst = (struct gsm0808_cell_id_list2){
1140 .id_discr = CELL_IDENT_NO_CELL,
1141 };
1142 return;
1143 }
1144
1145 *dst = (struct gsm0808_cell_id_list2){
1146 .id_discr = src->id_discr,
1147 .id_list = { src->id },
1148 .id_list_len = 1,
1149 };
1150
1151 switch (src->id_discr) {
1152 case CELL_IDENT_NO_CELL:
1153 case CELL_IDENT_BSS:
1154 dst->id_list_len = 0;
1155 break;
1156 default:
1157 break;
1158 }
1159}
1160
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001161/*! Encode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1162 * \param[out] msg Message Buffer to which IE is to be appended
1163 * \param[in] ci Cell ID to be encoded
1164 * \returns number of bytes appended to \a msg */
1165uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci)
1166{
1167 uint8_t rc;
1168 uint8_t *ie_tag;
1169 struct gsm0808_cell_id_list2 cil = {
1170 .id_discr = ci->id_discr,
1171 .id_list = { ci->id },
1172 .id_list_len = 1,
1173 };
1174
1175 OSMO_ASSERT(msg);
1176 OSMO_ASSERT(ci);
1177
1178 ie_tag = msg->tail;
1179 rc = gsm0808_enc_cell_id_list2(msg, &cil);
1180
1181 if (rc <= 0)
1182 return rc;
1183
1184 *ie_tag = GSM0808_IE_CELL_IDENTIFIER;
1185 return rc;
1186}
1187
1188/*! Decode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1189 * \param[out] ci Caller-provided memory to store Cell ID.
1190 * \param[in] elem IE value to be decoded.
1191 * \param[in] len Length of \a elem in bytes.
1192 * \returns number of bytes parsed; negative on error */
1193int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len)
1194{
1195 struct gsm0808_cell_id_list2 cil;
1196 int rc;
1197 rc = gsm0808_dec_cell_id_list2(&cil, elem, len);
1198 if (rc < 0)
1199 return rc;
1200 if (cil.id_discr == CELL_IDENT_BSS || cil.id_discr == CELL_IDENT_NO_CELL) {
1201 if (cil.id_list_len != 0)
1202 return -EINVAL;
1203 } else {
1204 if (cil.id_list_len != 1)
1205 return -EINVAL;
1206 }
1207 ci->id_discr = cil.id_discr;
1208 ci->id = cil.id_list[0];
1209 return rc;
1210}
1211
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001212/*! Convert the representation of the permitted speech codec identifier
Philipp Maier3149b0d2017-06-02 13:22:34 +02001213 * that is used in struct gsm0808_channel_type to the speech codec
1214 * representation we use in struct gsm0808_speech_codec.
1215 * \param[in] perm_spch to be converted (see also gsm0808_permitted_speech)
1216 * \returns GSM speech codec type; negative on error */
1217int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch)
1218{
1219 /*! The speech codec type, which is used in the channel type field to
1220 * signal the permitted speech versions (codecs) has a different
1221 * encoding than the type field in the speech codec type element
1222 * (See also 3GPP TS 48.008, 3.2.2.11 and 3.2.2.103) */
1223
1224 switch (perm_spch) {
1225 case GSM0808_PERM_FR1:
1226 return GSM0808_SCT_FR1;
1227 case GSM0808_PERM_FR2:
1228 return GSM0808_SCT_FR2;
1229 case GSM0808_PERM_FR3:
1230 return GSM0808_SCT_FR3;
1231 case GSM0808_PERM_FR4:
1232 return GSM0808_SCT_FR4;
1233 case GSM0808_PERM_FR5:
1234 return GSM0808_SCT_FR5;
1235 case GSM0808_PERM_HR1:
1236 return GSM0808_SCT_HR1;
1237 case GSM0808_PERM_HR3:
1238 return GSM0808_SCT_HR3;
1239 case GSM0808_PERM_HR4:
1240 return GSM0808_SCT_HR4;
1241 case GSM0808_PERM_HR6:
1242 return GSM0808_SCT_HR6;
1243 }
1244
1245 /* Invalid input */
1246 return -EINVAL;
1247}
1248
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001249/*! Extrapolate a speech codec field from a given permitted speech
Philipp Maier884ba0f2017-06-02 13:49:16 +02001250 * parameter (channel type).
1251 * \param[out] sc Caller provided memory to store the resulting speech codec
1252 * \param[in] perm_spch value that is used to derive the speech codec info
1253 * (see also: enum gsm0808_speech_codec_type in gsm0808_utils.h)
1254 * \returns zero when successful; negative on error */
1255int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc,
1256 uint8_t perm_spch)
1257{
1258 int rc;
1259
1260 memset(sc, 0, sizeof(*sc));
1261
1262 /* Determine codec type */
1263 rc = gsm0808_chan_type_to_speech_codec(perm_spch);
1264 if (rc < 0)
1265 return -EINVAL;
1266 sc->type = (uint8_t) rc;
1267
1268 /* Depending on the speech codec type, pick a default codec
1269 * configuration that exactly matches the configuration on the
1270 * air interface. */
1271 switch (sc->type) {
1272 case GSM0808_SCT_FR3:
1273 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR;
1274 break;
1275 case GSM0808_SCT_FR4:
1276 sc->cfg = GSM0808_SC_CFG_DEFAULT_OFR_AMR_WB;
1277 break;
1278 case GSM0808_SCT_FR5:
1279 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR_WB;
1280 break;
1281 case GSM0808_SCT_HR3:
1282 sc->cfg = GSM0808_SC_CFG_DEFAULT_HR_AMR;
1283 break;
1284 case GSM0808_SCT_HR4:
1285 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR_WB;
1286 break;
1287 case GSM0808_SCT_HR6:
1288 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR;
1289 break;
1290 default:
1291 /* Note: Not all codec types specify a default setting,
1292 * in this case, we just set the field to zero. */
1293 sc->cfg = 0;
1294 }
1295
1296 /* Tag all codecs as "Full IP"
1297 * (see als 3GPP TS 48.008 3.2.2.103) */
1298 sc->fi = true;
1299
1300 return 0;
1301}
1302
Philipp Maier5f2eb152018-09-19 13:40:21 +02001303/*! Determine a set of AMR speech codec configuration bits (S0-S15) from a
1304 * given GSM 04.08 AMR configuration struct.
1305 * \param[in] cfg AMR configuration in GSM 04.08 format.
1306 * \param[in] hint if the resulting configuration shall be used with a FR or HR TCH.
1307 * \returns configuration bits (S0-S15) */
Philipp Maier369015c2018-09-21 09:07:20 +02001308uint16_t gsm0808_sc_cfg_from_gsm48_mr_cfg(const struct gsm48_multi_rate_conf *cfg,
Philipp Maier5f2eb152018-09-19 13:40:21 +02001309 bool fr)
1310{
1311 uint16_t s15_s0 = 0;
1312
1313 /* Check each rate bit in the AMR multirate configuration and pick the
1314 * matching default configuration as specified in 3GPP TS 28.062,
1315 * Table 7.11.3.1.3-2. */
1316 if (cfg->m4_75)
1317 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_4_75;
1318 if (cfg->m5_15)
1319 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_15;
1320 if (cfg->m5_90)
1321 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_90;
1322 if (cfg->m6_70)
1323 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_6_70;
1324 if (cfg->m7_40)
1325 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_40;
1326 if (cfg->m7_95)
1327 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_95;
1328 if (cfg->m10_2)
1329 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_10_2;
1330 if (cfg->m12_2)
1331 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_12_2;
1332
1333 /* Note: 3GPP TS 48.008, chapter 3GPP TS 48.008 states that for AMR
1334 * some of the configuration bits must be coded as zeros. The applied
1335 * bitmask matches the default codec settings. See also the definition
1336 * of enum gsm0808_speech_codec_defaults in gsm_08_08.h and
1337 * 3GPP TS 28.062, Table 7.11.3.1.3-2. */
1338 if (fr)
1339 s15_s0 &= GSM0808_SC_CFG_DEFAULT_FR_AMR;
1340 else
1341 s15_s0 &= GSM0808_SC_CFG_DEFAULT_HR_AMR;
1342
Philipp Maier94d79fd2019-03-01 10:40:48 +01001343 /* The mode that is encoded by S1 (Config-NB-Code = 1), takes a special
1344 * role as it does not stand for a single rate, but for up to four rates
1345 * at once (12.2, 7.4, 5.9, 4.75). We must check if the supplied cfg
1346 * covers this mode. If not, we need to make sure that the related
1347 * bit is removed. (See also 3GPP TS 28.062, Table 7.11.3.1.3-2) */
1348 if (!(cfg->m12_2 && cfg->m7_40 && cfg->m5_90 && cfg->m4_75) && fr)
1349 s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20;
1350 else if (!(cfg->m7_40 && cfg->m5_90 && cfg->m4_75))
1351 s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20;
1352
Philipp Maier5f2eb152018-09-19 13:40:21 +02001353 return s15_s0;
1354}
1355
Philipp Maier8515d032018-09-25 15:57:49 +02001356/*! Determine a GSM 04.08 AMR configuration struct from a set of speech codec
1357 * configuration bits (S0-S15)
1358 * \param[out] cfg AMR configuration in GSM 04.08 format.
Philipp Maier3713af82019-02-27 16:48:25 +01001359 * \param[in] s15_s0 configuration bits (S15-S0, non-ambiguous).
1360 * \returns zero when successful; negative on error */
1361int gsm48_mr_cfg_from_gsm0808_sc_cfg(struct gsm48_multi_rate_conf *cfg,
1362 uint16_t s15_s0)
Philipp Maier8515d032018-09-25 15:57:49 +02001363{
Philipp Maier3713af82019-02-27 16:48:25 +01001364 unsigned int count = 0;
1365
1366 /* Note: See also: 3GPP TS 28.062
1367 * Table 7.11.3.1.3-2: Preferred Configurations for the Adaptive
1368 * Multi-Rate Codec Types */
1369
1370 /* Note: The resulting multirate-configuration must not contain an
1371 * active set of more than four codec rates. The active set also
1372 * must contain at least one rate. */
1373
Philipp Maier8515d032018-09-25 15:57:49 +02001374 memset(cfg, 0, sizeof(*cfg));
Philipp Maier3713af82019-02-27 16:48:25 +01001375 cfg->ver = 1;
1376 cfg->icmi = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001377
1378 /* Strip option bits */
1379 s15_s0 &= 0x00ff;
1380
Philipp Maier3713af82019-02-27 16:48:25 +01001381 /* Rate 5,15k can never be selected (see table) */
1382 cfg->m5_15 = 0;
Philipp Maier8515d032018-09-25 15:57:49 +02001383
Philipp Maier3713af82019-02-27 16:48:25 +01001384 if (s15_s0 & GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20 & 0xff) {
1385 /* Table Table 7.11.3.1.3-2 lists one mode that selects 4
1386 * rates at once (Config-NB-Code = 1). The rates selected
1387 * are known to be compatible between GERAN and UTRAN, since
1388 * an active set must not contain more than four rates at
1389 * a time, we ignore all other settings as they are either
1390 * redundaned or excess settings (invalid) */
Philipp Maier8515d032018-09-25 15:57:49 +02001391 cfg->m4_75 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001392 cfg->m5_90 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001393 cfg->m7_40 = 1;
Philipp Maier8515d032018-09-25 15:57:49 +02001394 cfg->m12_2 = 1;
Philipp Maier3713af82019-02-27 16:48:25 +01001395 count += 4;
1396 }
Philipp Maier8515d032018-09-25 15:57:49 +02001397
Philipp Maier3713af82019-02-27 16:48:25 +01001398 /* Check the bits in s15_s0 and set the flags for the
1399 * respective rates. */
1400 if (s15_s0 & GSM0808_SC_CFG_AMR_4_75 && !cfg->m4_75) {
1401 if (count >= 4)
1402 return -EINVAL;
1403 cfg->m4_75 = 1;
1404 count++;
1405 }
1406 if (s15_s0 & GSM0808_SC_CFG_AMR_5_90 && !cfg->m5_90) {
1407 if (count >= 4)
1408 return -EINVAL;
1409 cfg->m5_90 = 1;
1410 count++;
1411 }
1412 if (s15_s0 & GSM0808_SC_CFG_AMR_6_70) {
1413 if (count >= 4)
1414 return -EINVAL;
1415 cfg->m6_70 = 1;
1416 count++;
1417 }
1418 if (s15_s0 & GSM0808_SC_CFG_AMR_7_40 && !cfg->m7_40) {
1419 if (count >= 4)
1420 return -EINVAL;
1421 cfg->m7_40 = 1;
1422 count++;
1423 }
1424 if (s15_s0 & GSM0808_SC_CFG_AMR_7_95) {
1425 if (count >= 4)
1426 return -EINVAL;
1427 cfg->m7_95 = 1;
1428 count++;
1429 }
1430 if (s15_s0 & GSM0808_SC_CFG_AMR_10_2) {
1431 if (count >= 4)
1432 return -EINVAL;
1433 cfg->m10_2 = 1;
1434 count++;
1435 }
1436 if (s15_s0 & GSM0808_SC_CFG_AMR_12_2 && !cfg->m12_2) {
1437 if (count >= 4)
1438 return -EINVAL;
1439 cfg->m12_2 = 1;
1440 count++;
1441 }
1442
1443 if (count == 0)
1444 return -EINVAL;
1445
1446 return 0;
Philipp Maier8515d032018-09-25 15:57:49 +02001447}
1448
Maxed651d22018-11-07 15:25:05 +01001449int gsm0808_get_cipher_reject_cause(const struct tlv_parsed *tp)
1450{
1451 const uint8_t *buf = TLVP_VAL_MINLEN(tp, GSM0808_IE_CAUSE, 1);
1452
1453 if (!buf)
1454 return -EBADMSG;
1455
1456 if (TLVP_LEN(tp, GSM0808_IE_CAUSE) > 1) {
1457 if (!gsm0808_cause_ext(buf[0]))
1458 return -EINVAL;
1459 return buf[1];
1460 }
1461
1462 return buf[0];
1463}
1464
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001465/*! Print a human readable name of the cell identifier to the char buffer.
1466 * This is useful both for struct gsm0808_cell_id and struct gsm0808_cell_id_list2.
1467 * See also gsm0808_cell_id_name() and gsm0808_cell_id_list_name().
1468 * \param[out] buf Destination buffer to write string representation to.
1469 * \param[in] buflen Amount of memory available in \a buf.
1470 * \param[in] id_discr Cell Identifier type.
1471 * \param[in] u Cell Identifer value.
1472 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1473 * or that would have been written if the buffer were large enough.
1474 */
1475int gsm0808_cell_id_u_name(char *buf, size_t buflen,
1476 enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
1477{
1478 switch (id_discr) {
1479 case CELL_IDENT_LAC:
1480 return snprintf(buf, buflen, "%u", u->lac);
1481 case CELL_IDENT_CI:
1482 return snprintf(buf, buflen, "%u", u->ci);
1483 case CELL_IDENT_LAC_AND_CI:
1484 return snprintf(buf, buflen, "%u-%u", u->lac_and_ci.lac, u->lac_and_ci.ci);
1485 case CELL_IDENT_LAI_AND_LAC:
1486 return snprintf(buf, buflen, "%s", osmo_lai_name(&u->lai_and_lac));
1487 case CELL_IDENT_WHOLE_GLOBAL:
1488 return snprintf(buf, buflen, "%s", osmo_cgi_name(&u->global));
1489 default:
1490 /* For CELL_IDENT_BSS and CELL_IDENT_NO_CELL, just print the discriminator.
1491 * Same for kinds we have no string representation of yet. */
1492 return snprintf(buf, buflen, "%s", gsm0808_cell_id_discr_name(id_discr));
1493 }
1494}
1495
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02001496/* Store individual Cell Identifier information in a CGI, without clearing the remaining ones.
1497 * This is useful to supplement one CGI with information from more than one Cell Identifier,
1498 * which in turn is useful to match Cell Identifiers of differing kinds to each other.
1499 * Before first invocation, clear the *dst struct externally, this function does only write those members
1500 * that are present in parameter u.
1501 */
1502static void cell_id_to_cgi(struct osmo_cell_global_id *dst,
1503 enum CELL_IDENT discr, const union gsm0808_cell_id_u *u)
1504{
1505 switch (discr) {
1506 case CELL_IDENT_WHOLE_GLOBAL:
1507 *dst = u->global;
1508 return;
1509
1510 case CELL_IDENT_LAC_AND_CI:
1511 dst->lai.lac = u->lac_and_ci.lac;
1512 dst->cell_identity = u->lac_and_ci.ci;
1513 return;
1514
1515 case CELL_IDENT_CI:
1516 dst->cell_identity = u->ci;
1517 return;
1518
1519 case CELL_IDENT_LAI_AND_LAC:
1520 dst->lai = u->lai_and_lac;
1521 return;
1522
1523 case CELL_IDENT_LAC:
1524 dst->lai.lac = u->lac;
1525 return;
1526
1527 case CELL_IDENT_NO_CELL:
1528 case CELL_IDENT_BSS:
1529 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1530 case CELL_IDENT_UTRAN_RNC:
1531 case CELL_IDENT_UTRAN_LAC_RNC:
1532 /* No values to set. */
1533 return;
1534 }
1535}
1536
1537/*! Return true if the common information between the two Cell Identifiers match.
1538 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1539 * Note that CELL_IDENT_NO_CELL will always return false.
1540 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1541 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1542 * \param[in] discr1 Cell Identifier type.
1543 * \param[in] u1 Cell Identifier value.
1544 * \param[in] discr2 Other Cell Identifier type.
1545 * \param[in] u2 Other Cell Identifier value.
1546 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1547 * \returns True if the common fields of the above match.
1548 */
1549static bool gsm0808_cell_id_u_match(enum CELL_IDENT discr1, const union gsm0808_cell_id_u *u1,
1550 enum CELL_IDENT discr2, const union gsm0808_cell_id_u *u2,
1551 bool exact_match)
1552{
1553 struct osmo_cell_global_id a = {};
1554 struct osmo_cell_global_id b = {};
1555
1556 if (exact_match && discr1 != discr2)
1557 return false;
1558
1559 /* First handle the odd wildcard like CELL_IDENT kinds. We can't really match any of these. */
1560 switch (discr1) {
1561 case CELL_IDENT_NO_CELL:
1562 case CELL_IDENT_BSS:
1563 return discr1 == discr2;
1564 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1565 case CELL_IDENT_UTRAN_RNC:
1566 case CELL_IDENT_UTRAN_LAC_RNC:
1567 return false;
1568 default:
1569 break;
1570 }
1571 switch (discr2) {
1572 case CELL_IDENT_NO_CELL:
1573 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1574 case CELL_IDENT_UTRAN_RNC:
1575 case CELL_IDENT_UTRAN_LAC_RNC:
1576 case CELL_IDENT_BSS:
1577 return false;
1578 default:
1579 break;
1580 }
1581
1582 /* Enrich both sides to full CGI, then compare those. First set the *other* ID's values in case
1583 * they assign more items. For example:
1584 * u1 = LAC:42
1585 * u2 = LAC+CI:23+5
1586 * 1) a <- LAC+CI:23+5
1587 * 2) a <- LAC:42 so that a = LAC+CI:42+5
1588 * Now we can compare those two and find a mismatch. If the LAC were the same, we would get
1589 * identical LAC+CI and hence a match. */
1590
1591 cell_id_to_cgi(&a, discr2, u2);
1592 cell_id_to_cgi(&a, discr1, u1);
1593
1594 cell_id_to_cgi(&b, discr1, u1);
1595 cell_id_to_cgi(&b, discr2, u2);
1596
1597 return osmo_cgi_cmp(&a, &b) == 0;
1598}
1599
1600/*! Return true if the common information between the two Cell Identifiers match.
1601 * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same.
1602 * Note that CELL_IDENT_NO_CELL will always return false.
1603 * Also CELL_IDENT_BSS will always return false, since this function cannot possibly
1604 * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially.
1605 * \param[in] id1 Cell Identifier.
1606 * \param[in] id2 Other Cell Identifier.
1607 * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical.
1608 * \returns True if the common fields of the above match.
1609 */
1610bool gsm0808_cell_ids_match(const struct gsm0808_cell_id *id1, const struct gsm0808_cell_id *id2, bool exact_match)
1611{
1612 return gsm0808_cell_id_u_match(id1->id_discr, &id1->id, id2->id_discr, &id2->id, exact_match);
1613}
1614
1615/*! Find an index in a Cell Identifier list that matches a given single Cell Identifer.
1616 * Compare \a id against each entry in \a list using gsm0808_cell_ids_match(), and return the list index
1617 * if a match is found. \a match_nr allows iterating all matches in the list. A match_nr <= 0 returns the
1618 * first match in the list, match_nr == 1 the second match, etc., and if match_nr exceeds the available
1619 * matches in the list, -1 is returned.
1620 * \param[in] id Cell Identifier to match.
1621 * \param[in] list Cell Identifier list to search in.
1622 * \param[in] match_nr Ignore this many matches.
1623 * \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 +01001624 * \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
1625 * entry).
Neels Hofmeyrd01ef752018-09-21 15:57:26 +02001626 */
1627int gsm0808_cell_id_matches_list(const struct gsm0808_cell_id *id, const struct gsm0808_cell_id_list2 *list,
1628 unsigned int match_nr, bool exact_match)
1629{
1630 int i;
1631 for (i = 0; i < list->id_list_len; i++) {
1632 if (gsm0808_cell_id_u_match(id->id_discr, &id->id, list->id_discr, &list->id_list[i], exact_match)) {
1633 if (match_nr)
1634 match_nr--;
1635 else
1636 return i;
1637 }
1638 }
1639 return -1;
1640}
1641
Neels Hofmeyr3a504532019-02-10 22:28:27 +01001642/*! Copy information from a CGI to form a Cell Identifier of the specified kind.
1643 * \param [out] cid Compose new Cell Identifier here.
1644 * \param [in] id_discr Which kind of Cell Identifier to compose.
1645 * \param [in] cgi Cell Global Identifier to form the Cell Identifier from.
1646 */
1647void gsm0808_cell_id_from_cgi(struct gsm0808_cell_id *cid, enum CELL_IDENT id_discr,
1648 const struct osmo_cell_global_id *cgi)
1649{
1650 *cid = (struct gsm0808_cell_id){
1651 .id_discr = id_discr,
1652 };
1653
1654 switch (id_discr) {
1655 case CELL_IDENT_WHOLE_GLOBAL:
1656 cid->id.global = *cgi;
1657 return;
1658
1659 case CELL_IDENT_LAC_AND_CI:
1660 cid->id.lac_and_ci = (struct osmo_lac_and_ci_id){
1661 .lac = cgi->lai.lac,
1662 .ci = cgi->cell_identity,
1663 };
1664 return;
1665
1666 case CELL_IDENT_CI:
1667 cid->id.ci = cgi->cell_identity;
1668 return;
1669
1670 case CELL_IDENT_LAI:
1671 cid->id.lai_and_lac = cgi->lai;
1672 return;
1673
1674 case CELL_IDENT_LAC:
1675 cid->id.lac = cgi->lai.lac;
1676 return;
1677
1678 case CELL_IDENT_NO_CELL:
1679 case CELL_IDENT_BSS:
1680 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1681 case CELL_IDENT_UTRAN_RNC:
1682 case CELL_IDENT_UTRAN_LAC_RNC:
1683 default:
1684 return;
1685 };
1686}
1687
1688/*! Overwrite parts of cgi with values from a Cell Identifier.
1689 * Place only those items given in cid into cgi, leaving other values unchanged.
1690 * \param[out] cgi Cell Global Identity to write to.
1691 * \param[in] cid Cell Identity to read from.
1692 * \return a bitmask of items that were set: OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI; 0 if nothing was
1693 * written to cgi.
1694 */
1695int gsm0808_cell_id_to_cgi(struct osmo_cell_global_id *cgi, const struct gsm0808_cell_id *cid)
1696{
1697 switch (cid->id_discr) {
1698 case CELL_IDENT_WHOLE_GLOBAL:
1699 *cgi = cid->id.global;
1700 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
1701
1702 case CELL_IDENT_LAC_AND_CI:
1703 cgi->lai.lac = cid->id.lac_and_ci.lac;
1704 cgi->cell_identity = cid->id.lac_and_ci.ci;
1705 return OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI;
1706
1707 case CELL_IDENT_CI:
1708 cgi->cell_identity = cid->id.ci;
1709 return OSMO_CGI_PART_CI;
1710
1711 case CELL_IDENT_LAI:
1712 cgi->lai = cid->id.lai_and_lac;
1713 return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC;
1714
1715 case CELL_IDENT_LAC:
1716 cgi->lai.lac = cid->id.lac;
1717 return OSMO_CGI_PART_LAC;
1718
1719 case CELL_IDENT_NO_CELL:
1720 case CELL_IDENT_BSS:
1721 case CELL_IDENT_UTRAN_PLMN_LAC_RNC:
1722 case CELL_IDENT_UTRAN_RNC:
1723 case CELL_IDENT_UTRAN_LAC_RNC:
1724 default:
1725 return 0;
1726 };
1727}
1728
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001729/*! value_string[] for enum CELL_IDENT. */
1730const struct value_string gsm0808_cell_id_discr_names[] = {
1731 { CELL_IDENT_WHOLE_GLOBAL, "CGI" },
1732 { CELL_IDENT_LAC_AND_CI, "LAC-CI" },
1733 { CELL_IDENT_CI, "CI" },
1734 { CELL_IDENT_NO_CELL, "NO-CELL" },
1735 { CELL_IDENT_LAI_AND_LAC, "LAI" },
1736 { CELL_IDENT_LAC, "LAC" },
1737 { CELL_IDENT_BSS, "BSS" },
1738 { CELL_IDENT_UTRAN_PLMN_LAC_RNC, "UTRAN-PLMN-LAC-RNC" },
1739 { CELL_IDENT_UTRAN_RNC, "UTRAN-RNC" },
1740 { CELL_IDENT_UTRAN_LAC_RNC, "UTRAN-LAC-RNC" },
1741 { 0, NULL }
1742};
1743
1744#define APPEND_THING(func, args...) do { \
1745 int remain = buflen - (pos - buf); \
1746 int l = func(pos, remain, ##args); \
1747 if (l < 0 || l > remain) \
1748 pos = buf + buflen; \
1749 else \
1750 pos += l; \
1751 if (l > 0) \
1752 total_len += l; \
1753 } while(0)
1754#define APPEND_STR(fmt, args...) APPEND_THING(snprintf, fmt, ##args)
1755#define APPEND_CELL_ID_U(DISCR, U) APPEND_THING(gsm0808_cell_id_u_name, DISCR, U)
1756
1757static const char *gsm0808_cell_id_name_buf(const struct gsm0808_cell_id *cid,
1758 char *buf, size_t buflen)
1759{
1760 char *pos = buf;
1761 int total_len = 0;
1762 APPEND_STR("%s:", gsm0808_cell_id_discr_name(cid->id_discr));
1763 APPEND_CELL_ID_U(cid->id_discr, &cid->id);
1764 return buf;
1765}
1766
1767/*! Return a human readable representation of a Cell Identifier, like "LAC:123"
1768 * or "CGI:001-01-42-23".
1769 * \param[in] cid Cell Identifer.
1770 * \returns String in a static buffer.
1771 */
1772const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid)
1773{
1774 static char buf[64];
1775 return gsm0808_cell_id_name_buf(cid, buf, sizeof(buf));
1776}
1777
1778/*! Like gsm0808_cell_id_name() but uses a different static buffer.
1779 * \param[in] cid Cell Identifer.
1780 * \returns String in a static buffer.
1781 */
1782const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid)
1783{
1784 static char buf[64];
1785 return gsm0808_cell_id_name_buf(cid, buf, sizeof(buf));
1786}
1787
1788/*! Return a human readable representation of the Cell Identifier List, like
1789 * "LAC[2]:{123, 456}".
1790 * The return value semantics are like snprintf() and thus allow ensuring a complete
1791 * untruncated string by determining the required string length from the return value.
1792 * If buflen > 0, always nul-terminate the string in buf, also when it is truncated.
1793 * If buflen == 0, do not modify buf, just return the would-be length.
1794 * \param[out] buf Destination buffer to write string representation to.
1795 * \param[in] buflen Amount of memory available in \a buf.
1796 * \param[in] cil Cell Identifer List.
1797 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1798 * or that would have been written if the buffer were large enough.
1799 */
1800int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil)
1801{
1802 char *pos = buf;
1803 int total_len = 0;
1804 int i;
1805
1806 APPEND_STR("%s[%u]", gsm0808_cell_id_discr_name(cil->id_discr), cil->id_list_len);
1807
1808 switch (cil->id_discr) {
1809 case CELL_IDENT_BSS:
1810 case CELL_IDENT_NO_CELL:
1811 return total_len;
1812 default:
1813 break;
1814 }
1815
1816 APPEND_STR(":{");
1817
1818 for (i = 0; i < cil->id_list_len; i++) {
1819 if (i)
1820 APPEND_STR(", ");
1821 APPEND_CELL_ID_U(cil->id_discr, &cil->id_list[i]);
1822 }
1823
1824 APPEND_STR("}");
1825 return total_len;
1826}
1827
1828/*! Return a human-readable representation of \a cil in a static buffer.
1829 * If the list is too long, the output may be truncated.
1830 * See also gsm0808_cell_id_list_name_buf(). */
1831const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil)
1832{
1833 static char buf[1024];
1834 gsm0808_cell_id_list_name_buf(buf, sizeof(buf), cil);
1835 return buf;
1836}
1837
1838#undef APPEND_STR
1839#undef APPEND_CELL_ID_U
1840
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02001841const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct)
1842{
1843 static char buf[128];
1844 snprintf(buf, sizeof(buf), "ch_indctr=0x%x ch_rate_type=0x%x perm_spch=%s",
1845 ct->ch_indctr, ct->ch_rate_type,
1846 osmo_hexdump(ct->perm_spch, ct->perm_spch_len));
1847 return buf;
1848}
1849
Harald Welte96e2a002017-06-12 21:44:18 +02001850/*! @} */