blob: c58d8284f911a58b07683899e604d25134deebbe [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*
Harald Weltee08da972017-11-13 01:00:26 +09002 * (C) 2016 by sysmocom - s.f.m.c. GmbH, Author: Philipp Maier
Philipp Maier22401432017-03-24 17:59:26 +01003 * All Rights Reserved
4 *
Harald Weltee08da972017-11-13 01:00:26 +09005 * SPDX-License-Identifier: GPL-2.0+
Philipp Maier22401432017-03-24 17:59:26 +01006 *
7 * This program is free software; you can redistribute it and/or modify
Harald Weltee08da972017-11-13 01:00:26 +09008 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
Philipp Maier22401432017-03-24 17:59:26 +010010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Weltee08da972017-11-13 01:00:26 +090015 * GNU General Public License for more details.
Philipp Maier22401432017-03-24 17:59:26 +010016 *
Harald Weltee08da972017-11-13 01:00:26 +090017 * You should have received a copy of the GNU General Public License
Philipp Maier22401432017-03-24 17:59:26 +010018 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
Harald Welte20725b92017-05-15 12:50:04 +020022#include "config.h"
23
Philipp Maier22401432017-03-24 17:59:26 +010024#include <osmocom/core/utils.h>
25#include <osmocom/core/msgb.h>
Harald Welte95871da2017-05-15 12:11:36 +020026#include <osmocom/core/byteswap.h>
Philipp Maier22401432017-03-24 17:59:26 +010027#include <string.h>
Philipp Maier22401432017-03-24 17:59:26 +010028#include <errno.h>
29#include <osmocom/gsm/protocol/gsm_08_08.h>
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010030#include <osmocom/gsm/gsm48.h>
31#include <osmocom/gsm/gsm0808_utils.h>
Philipp Maier22401432017-03-24 17:59:26 +010032
33#define IP_V4_ADDR_LEN 4
34#define IP_V6_ADDR_LEN 16
35#define IP_PORT_LEN 2
36
Philipp Maiere0c65302017-03-28 17:05:40 +020037#define CHANNEL_TYPE_ELEMENT_MAXLEN 11
38#define CHANNEL_TYPE_ELEMENT_MINLEN 3
Philipp Maier14e76b92017-03-28 18:36:52 +020039#define ENCRYPT_INFO_ELEMENT_MINLEN 1
Philipp Maier6f725d62017-03-24 18:03:17 +010040
Harald Welte20725b92017-05-15 12:50:04 +020041#ifdef HAVE_SYS_SOCKET_H
42
43#include <sys/socket.h>
44#include <netinet/in.h>
Harald Welte96e2a002017-06-12 21:44:18 +020045
46/*! \addtogroup gsm0808
47 * @{
Harald Welte37b61652017-10-16 18:46:03 +020048 * \file gsm0808_utils.c
Harald Welte96e2a002017-06-12 21:44:18 +020049 */
50
Neels Hofmeyr87e45502017-06-20 00:17:59 +020051/*! Encode TS 08.08 AoIP transport address IE
Harald Welte96e2a002017-06-12 21:44:18 +020052 * \param[out] msg Message Buffer to which to append IE
53 * \param[in] ss Socket Address to be used in IE
54 * \returns number of bytes added to \a msg */
Philipp Maier22401432017-03-24 17:59:26 +010055uint8_t gsm0808_enc_aoip_trasp_addr(struct msgb *msg,
56 const struct sockaddr_storage *ss)
57{
58 /* See also 3GPP TS 48.008 3.2.2.102 AoIP Transport Layer Address */
59 struct sockaddr_in *sin;
60 struct sockaddr_in6 *sin6;
61 uint16_t port = 0;
62 uint8_t *ptr;
63 uint8_t *old_tail;
64 uint8_t *tlv_len;
65
66 OSMO_ASSERT(msg);
67 OSMO_ASSERT(ss);
68 OSMO_ASSERT(ss->ss_family == AF_INET || ss->ss_family == AF_INET6);
69
70 msgb_put_u8(msg, GSM0808_IE_AOIP_TRASP_ADDR);
71 tlv_len = msgb_put(msg,1);
72 old_tail = msg->tail;
73
74 switch (ss->ss_family) {
75 case AF_INET:
76 sin = (struct sockaddr_in *)ss;
Harald Welte95871da2017-05-15 12:11:36 +020077 port = osmo_ntohs(sin->sin_port);
Philipp Maier22401432017-03-24 17:59:26 +010078 ptr = msgb_put(msg, IP_V4_ADDR_LEN);
79 memcpy(ptr, &sin->sin_addr.s_addr, IP_V4_ADDR_LEN);
80 break;
81 case AF_INET6:
82 sin6 = (struct sockaddr_in6 *)ss;
Harald Welte95871da2017-05-15 12:11:36 +020083 port = osmo_ntohs(sin6->sin6_port);
Philipp Maier22401432017-03-24 17:59:26 +010084 ptr = msgb_put(msg, IP_V6_ADDR_LEN);
85 memcpy(ptr, sin6->sin6_addr.s6_addr, IP_V6_ADDR_LEN);
86 break;
87 }
88
89 msgb_put_u16(msg, port);
90
91 *tlv_len = (uint8_t) (msg->tail - old_tail);
92 return *tlv_len + 2;
93}
94
Neels Hofmeyr87e45502017-06-20 00:17:59 +020095/*! Decode TS 08.08 AoIP transport address IE
Harald Welte96e2a002017-06-12 21:44:18 +020096 * \param[out] ss Caller-provided memory where decoded socket addr is stored
97 * \param[in] elem pointer to IE value
98 * \param[in] len length of \a elem in bytes
99 * \returns number of bytes parsed */
Philipp Maier22401432017-03-24 17:59:26 +0100100int gsm0808_dec_aoip_trasp_addr(struct sockaddr_storage *ss,
101 const uint8_t *elem, uint8_t len)
102{
103 /* See also 3GPP TS 48.008 3.2.2.102 AoIP Transport Layer Address */
104 struct sockaddr_in sin;
105 struct sockaddr_in6 sin6;
106 const uint8_t *old_elem = elem;
107
108 OSMO_ASSERT(ss);
109 if (!elem)
110 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200111 if (len == 0)
Philipp Maier22401432017-03-24 17:59:26 +0100112 return -EINVAL;
113
114 memset(ss, 0, sizeof(*ss));
115
116 switch (len) {
117 case IP_V4_ADDR_LEN + IP_PORT_LEN:
118 memset(&sin, 0, sizeof(sin));
119 sin.sin_family = AF_INET;
120
121 memcpy(&sin.sin_addr.s_addr, elem, IP_V4_ADDR_LEN);
122 elem += IP_V4_ADDR_LEN;
123 sin.sin_port = osmo_load16le(elem);
124 elem += IP_PORT_LEN;
125
126 memcpy(ss, &sin, sizeof(sin));
127 break;
128 case IP_V6_ADDR_LEN + IP_PORT_LEN:
129 memset(&sin6, 0, sizeof(sin6));
130 sin6.sin6_family = AF_INET6;
131
132 memcpy(sin6.sin6_addr.s6_addr, elem, IP_V6_ADDR_LEN);
133 elem += IP_V6_ADDR_LEN;
134 sin6.sin6_port = osmo_load16le(elem);
135 elem += IP_PORT_LEN;
136
137 memcpy(ss, &sin6, sizeof(sin6));
138 break;
139 default:
140 /* Malformed element! */
141 return -EINVAL;
142 break;
143 }
144
145 return (int)(elem - old_elem);
146}
Philipp Maier6f725d62017-03-24 18:03:17 +0100147
Harald Welte20725b92017-05-15 12:50:04 +0200148#endif /* HAVE_SYS_SOCKET_H */
149
Philipp Maier6f725d62017-03-24 18:03:17 +0100150/* Helper function for gsm0808_enc_speech_codec()
151 * and gsm0808_enc_speech_codec_list() */
152static uint8_t enc_speech_codec(struct msgb *msg,
153 const struct gsm0808_speech_codec *sc)
154{
155 /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
156 uint8_t header = 0;
157 uint8_t *old_tail;
Harald Welte459a1802018-06-28 09:24:17 +0200158 bool type_extended = false;
Philipp Maierbb839662017-06-01 17:11:19 +0200159
160 /* Note: Extended codec types are codec types that require 8 instead
161 * of 4 bit to fully specify the selected codec. In the following,
162 * we check if we work with an extended type or not. We also check
163 * if the codec type is valid at all. */
164 switch(sc->type) {
165 case GSM0808_SCT_FR1:
166 case GSM0808_SCT_FR2:
167 case GSM0808_SCT_FR3:
168 case GSM0808_SCT_FR4:
169 case GSM0808_SCT_FR5:
170 case GSM0808_SCT_HR1:
171 case GSM0808_SCT_HR3:
172 case GSM0808_SCT_HR4:
173 case GSM0808_SCT_HR6:
174 type_extended = false;
175 break;
176 case GSM0808_SCT_CSD:
177 type_extended = true;
178 break;
179 default:
180 /* Invalid codec type specified */
181 OSMO_ASSERT(false);
182 break;
183 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100184
185 old_tail = msg->tail;
186
187 if (sc->fi)
188 header |= (1 << 7);
189 if (sc->pi)
190 header |= (1 << 6);
191 if (sc->pt)
192 header |= (1 << 5);
193 if (sc->tf)
194 header |= (1 << 4);
Philipp Maierbb839662017-06-01 17:11:19 +0200195
196 if (type_extended) {
Philipp Maier6f725d62017-03-24 18:03:17 +0100197 header |= 0x0f;
198 msgb_put_u8(msg, header);
Philipp Maierbb839662017-06-01 17:11:19 +0200199 msgb_put_u8(msg, sc->type);
Philipp Maier6f725d62017-03-24 18:03:17 +0100200 } else {
201 OSMO_ASSERT(sc->type < 0x0f);
202 header |= sc->type;
203 msgb_put_u8(msg, header);
Philipp Maier6f725d62017-03-24 18:03:17 +0100204 }
205
Philipp Maierbb839662017-06-01 17:11:19 +0200206 /* Note: Whether a configuration is present or not depends on the
207 * selected codec type. If present, it can either consist of one
208 * or two octets, depending on the codec type */
209 switch (sc->type) {
210 case GSM0808_SCT_FR3:
211 case GSM0808_SCT_HR3:
212 case GSM0808_SCT_HR6:
Philipp Maier7e27b142018-03-22 17:26:46 +0100213 msgb_put_u16(msg, osmo_ntohs(sc->cfg));
Philipp Maierbb839662017-06-01 17:11:19 +0200214 break;
215 case GSM0808_SCT_FR4:
216 case GSM0808_SCT_FR5:
217 case GSM0808_SCT_HR4:
218 case GSM0808_SCT_CSD:
219 OSMO_ASSERT((sc->cfg & 0xff00) == 0)
220 msgb_put_u8(msg, (uint8_t) sc->cfg & 0xff);
221 break;
222 default:
223 OSMO_ASSERT(sc->cfg == 0);
224 break;
225 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100226
227 return (uint8_t) (msg->tail - old_tail);
228}
229
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200230/*! Encode TS 08.08 Speech Codec IE
Harald Welte96e2a002017-06-12 21:44:18 +0200231 * \param[out] msg Message Buffer to which IE will be appended
232 * \param[in] sc Speech Codec to be encoded into IE
233 * \returns number of bytes appended to \a msg */
Philipp Maier6f725d62017-03-24 18:03:17 +0100234uint8_t gsm0808_enc_speech_codec(struct msgb *msg,
235 const struct gsm0808_speech_codec *sc)
236{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200237 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100238 uint8_t *old_tail;
239 uint8_t *tlv_len;
240
241 OSMO_ASSERT(msg);
242 OSMO_ASSERT(sc);
243
244 msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC);
245 tlv_len = msgb_put(msg, 1);
246 old_tail = msg->tail;
247
248 enc_speech_codec(msg, sc);
249
250 *tlv_len = (uint8_t) (msg->tail - old_tail);
251 return *tlv_len + 2;
252}
253
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200254/*! Decode TS 08.08 Speech Codec IE
Harald Welte96e2a002017-06-12 21:44:18 +0200255 * \param[out] sc Caller-allocated memory for Speech Codec
256 * \param[in] elem IE value to be decoded
257 * \param[in] len Length of \a elem in bytes
258 * \returns number of bytes parsed; negative on error */
Philipp Maier6f725d62017-03-24 18:03:17 +0100259int gsm0808_dec_speech_codec(struct gsm0808_speech_codec *sc,
260 const uint8_t *elem, uint8_t len)
261{
262 /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
263 uint8_t header;
264 const uint8_t *old_elem = elem;
265
266 OSMO_ASSERT(sc);
267 if (!elem)
268 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200269 if (len == 0)
Philipp Maier6f725d62017-03-24 18:03:17 +0100270 return -EINVAL;
271
272 memset(sc, 0, sizeof(*sc));
273
274 header = *elem;
275
Philipp Maier85a6af22017-04-28 10:55:05 +0200276 /* An extended codec type needs at least two fields,
277 * bail if the input data length is not sufficient. */
Philipp Maier6f725d62017-03-24 18:03:17 +0100278 if ((header & 0x0F) == 0x0F && len < 2)
279 return -EINVAL;
Philipp Maier6f725d62017-03-24 18:03:17 +0100280
281 elem++;
282 len--;
283
284 if (header & (1 << 7))
285 sc->fi = true;
286 if (header & (1 << 6))
287 sc->pi = true;
288 if (header & (1 << 5))
289 sc->pt = true;
290 if (header & (1 << 4))
291 sc->tf = true;
292
293 if ((header & 0x0F) != 0x0F) {
294 sc->type = (header & 0x0F);
Philipp Maierbb839662017-06-01 17:11:19 +0200295 } else {
296 sc->type = *elem;
297 elem++;
298 len--;
Philipp Maier6f725d62017-03-24 18:03:17 +0100299 }
300
Philipp Maierbb839662017-06-01 17:11:19 +0200301 /* Note: Whether a configuration is present or not depends on the
302 * selected codec type. If present, it can either consist of one or
303 * two octets depending on the codec type */
304 switch (sc->type) {
305 case GSM0808_SCT_FR1:
306 case GSM0808_SCT_FR2:
307 case GSM0808_SCT_HR1:
308 break;
309 case GSM0808_SCT_HR4:
310 case GSM0808_SCT_CSD:
311 case GSM0808_SCT_FR4:
312 case GSM0808_SCT_FR5:
313 if (len < 1)
314 return -EINVAL;
315 sc->cfg = *elem;
316 elem++;
317 break;
318 case GSM0808_SCT_FR3:
319 case GSM0808_SCT_HR3:
320 case GSM0808_SCT_HR6:
321 if (len < 2)
322 return -EINVAL;
Philipp Maier7e27b142018-03-22 17:26:46 +0100323 sc->cfg = osmo_load16le(elem);
Philipp Maierbb839662017-06-01 17:11:19 +0200324 elem += 2;
325 break;
326 default:
327 /* Invalid codec type => malformed speech codec element! */
328 return -EINVAL;
329 break;
330 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100331
332 return (int)(elem - old_elem);
333}
334
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200335/*! Encode TS 08.08 Speech Codec list
Harald Welte96e2a002017-06-12 21:44:18 +0200336 * \param[out] msg Message Buffer to which IE is to be appended
337 * \param[in] scl Speech Codec List to be encoded into IE
338 * \returns number of bytes added to \a msg */
Philipp Maier6f725d62017-03-24 18:03:17 +0100339uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg,
340 const struct gsm0808_speech_codec_list *scl)
341{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200342 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100343 uint8_t *old_tail;
344 uint8_t *tlv_len;
345 unsigned int i;
346 uint8_t rc;
347 unsigned int bytes_used = 0;
348
349 OSMO_ASSERT(msg);
350 OSMO_ASSERT(scl);
351
Philipp Maier6f725d62017-03-24 18:03:17 +0100352 msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC_LIST);
353 tlv_len = msgb_put(msg, 1);
354 old_tail = msg->tail;
355
356 for (i = 0; i < scl->len; i++) {
357 rc = enc_speech_codec(msg, &scl->codec[i]);
358 OSMO_ASSERT(rc >= 1);
359 bytes_used += rc;
360 OSMO_ASSERT(bytes_used <= 255);
361 }
362
363 *tlv_len = (uint8_t) (msg->tail - old_tail);
364 return *tlv_len + 2;
365}
366
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200367/*! Decode TS 08.08 Speech Codec list IE
Harald Welte96e2a002017-06-12 21:44:18 +0200368 * \param[out] scl Caller-provided memory to store codec list
369 * \param[in] elem IE value to be decoded
370 * \param[in] len Length of \a elem in bytes
371 * \returns number of bytes parsed; negative on error */
Philipp Maier6f725d62017-03-24 18:03:17 +0100372int gsm0808_dec_speech_codec_list(struct gsm0808_speech_codec_list *scl,
373 const uint8_t *elem, uint8_t len)
374{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200375 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100376 const uint8_t *old_elem = elem;
377 unsigned int i;
378 int rc;
379 uint8_t decoded = 0;
380
381 OSMO_ASSERT(scl);
382 if (!elem)
383 return -EINVAL;
Philipp Maier6f725d62017-03-24 18:03:17 +0100384
385 memset(scl, 0, sizeof(*scl));
386
387 for (i = 0; i < ARRAY_SIZE(scl->codec); i++) {
388 if (len <= 0)
389 break;
390
391 rc = gsm0808_dec_speech_codec(&scl->codec[i], elem, len);
392 if (rc < 1)
393 return -EINVAL;
394
395 elem+=rc;
396 len -= rc;
397 decoded++;
398 }
399
400 scl->len = decoded;
401
Philipp Maier6f725d62017-03-24 18:03:17 +0100402 return (int)(elem - old_elem);
403}
Philipp Maiere0c65302017-03-28 17:05:40 +0200404
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200405/*! Encode TS 08.08 Channel Type IE
Harald Welte96e2a002017-06-12 21:44:18 +0200406 * \param[out] msg Message Buffer to which IE is to be appended
407 * \param[in] ct Channel Type to be encoded
408 * \returns number of bytes added to \a msg */
Philipp Maiere0c65302017-03-28 17:05:40 +0200409uint8_t gsm0808_enc_channel_type(struct msgb *msg,
410 const struct gsm0808_channel_type *ct)
411{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200412 /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */
Philipp Maiere0c65302017-03-28 17:05:40 +0200413 unsigned int i;
414 uint8_t byte;
415 uint8_t *old_tail;
416 uint8_t *tlv_len;
417
418 OSMO_ASSERT(msg);
419 OSMO_ASSERT(ct);
420 OSMO_ASSERT(ct->perm_spch_len <= CHANNEL_TYPE_ELEMENT_MAXLEN - 2);
421
422 /* FIXME: Implement encoding support for Data
423 * and Speech + CTM Text Telephony */
424 if ((ct->ch_indctr & 0x0f) != GSM0808_CHAN_SPEECH
425 && (ct->ch_indctr & 0x0f) != GSM0808_CHAN_SIGN)
426 OSMO_ASSERT(false);
427
428 msgb_put_u8(msg, GSM0808_IE_CHANNEL_TYPE);
429 tlv_len = msgb_put(msg, 1);
430 old_tail = msg->tail;
431
432 msgb_put_u8(msg, ct->ch_indctr & 0x0f);
433 msgb_put_u8(msg, ct->ch_rate_type);
434
435 for (i = 0; i < ct->perm_spch_len; i++) {
436 byte = ct->perm_spch[i];
437
438 if (i < ct->perm_spch_len - 1)
439 byte |= 0x80;
440 msgb_put_u8(msg, byte);
441 }
442
443 *tlv_len = (uint8_t) (msg->tail - old_tail);
444 return *tlv_len + 2;
445}
446
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200447/*! Decode TS 08.08 Channel Type IE
Harald Welte96e2a002017-06-12 21:44:18 +0200448 * \param[out] ct Caller-provided memory to store channel type
449 * \param[in] elem IE Value to be decoded
450 * \param[in] len Length of \a elem in bytes
451 * \returns number of bytes parsed; negative on error */
Philipp Maiere0c65302017-03-28 17:05:40 +0200452int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct,
453 const uint8_t *elem, uint8_t len)
454{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200455 /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */
Philipp Maiere0c65302017-03-28 17:05:40 +0200456 unsigned int i;
457 uint8_t byte;
458 const uint8_t *old_elem = elem;
459
460 OSMO_ASSERT(ct);
461 if (!elem)
462 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200463 if (len < 3 || len > 11)
Philipp Maiere0c65302017-03-28 17:05:40 +0200464 return -EINVAL;
465
466 memset(ct, 0, sizeof(*ct));
467
468 ct->ch_indctr = (*elem) & 0x0f;
469 elem++;
470 ct->ch_rate_type = (*elem) & 0x0f;
471 elem++;
472
473 for (i = 0; i < ARRAY_SIZE(ct->perm_spch); i++) {
474 byte = *elem;
475 elem++;
476 ct->perm_spch[i] = byte & 0x7f;
477 if ((byte & 0x80) == 0x00)
478 break;
479 }
480 ct->perm_spch_len = i + 1;
481
482 return (int)(elem - old_elem);
483}
Philipp Maier14e76b92017-03-28 18:36:52 +0200484
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200485/*! Encode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200486 * \param[out] msg Message Buffer to which IE is to be appended
487 * \param[in] ei Encryption Information to be encoded
488 * \returns number of bytes appended to \a msg */
Philipp Maier14e76b92017-03-28 18:36:52 +0200489uint8_t gsm0808_enc_encrypt_info(struct msgb *msg,
490 const struct gsm0808_encrypt_info *ei)
491{
492 unsigned int i;
493 uint8_t perm_algo = 0;
494 uint8_t *ptr;
495 uint8_t *old_tail;
496 uint8_t *tlv_len;
497
498 OSMO_ASSERT(msg);
499 OSMO_ASSERT(ei);
500 OSMO_ASSERT(ei->key_len <= ARRAY_SIZE(ei->key));
501 OSMO_ASSERT(ei->perm_algo_len <= ENCRY_INFO_PERM_ALGO_MAXLEN);
502
503 msgb_put_u8(msg, GSM0808_IE_ENCRYPTION_INFORMATION);
504 tlv_len = msgb_put(msg, 1);
505 old_tail = msg->tail;
506
507 for (i = 0; i < ei->perm_algo_len; i++) {
508 /* Note: gsm_08_08.h defines the permitted algorithms
509 * as an enum which ranges from 0x01 to 0x08 */
510 OSMO_ASSERT(ei->perm_algo[i] != 0);
511 OSMO_ASSERT(ei->perm_algo[i] <= ENCRY_INFO_PERM_ALGO_MAXLEN);
512 perm_algo |= (1 << (ei->perm_algo[i] - 1));
513 }
514
515 msgb_put_u8(msg, perm_algo);
516 ptr = msgb_put(msg, ei->key_len);
517 memcpy(ptr, ei->key, ei->key_len);
518
519 *tlv_len = (uint8_t) (msg->tail - old_tail);
520 return *tlv_len + 2;
521}
522
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200523/*! Decode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200524 * \param[out] ei Caller-provided memory to store encryption information
525 * \param[in] elem IE value to be decoded
526 * \param[in] len Length of \a elem in bytes
527 * \returns number of bytes parsed; negative on error */
Philipp Maier14e76b92017-03-28 18:36:52 +0200528int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei,
529 const uint8_t *elem, uint8_t len)
530{
531 uint8_t perm_algo;
532 unsigned int i;
533 unsigned int perm_algo_len = 0;
534 const uint8_t *old_elem = elem;
535
536 OSMO_ASSERT(ei);
537 if (!elem)
538 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200539 if (len == 0)
Philipp Maier14e76b92017-03-28 18:36:52 +0200540 return -EINVAL;
541
542 memset(ei, 0, sizeof(*ei));
543
544 perm_algo = *elem;
545 elem++;
546
547 for (i = 0; i < ENCRY_INFO_PERM_ALGO_MAXLEN; i++) {
548 if (perm_algo & (1 << i)) {
549 ei->perm_algo[perm_algo_len] = i + 1;
550 perm_algo_len++;
551 }
552 }
553 ei->perm_algo_len = perm_algo_len;
554
555 ei->key_len = len - 1;
556 memcpy(ei->key, elem, ei->key_len);
557 elem+=ei->key_len;
558
559 return (int)(elem - old_elem);
560}
Philipp Maier783047e2017-03-29 11:35:50 +0200561
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200562/*! Encode TS 08.08 Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +0200563 * \param[out] msg Message Buffer to which IE is to be appended
564 * \param[in] cil Cell ID List to be encoded
565 * \returns number of bytes appended to \a msg */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100566uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg,
567 const struct gsm0808_cell_id_list2 *cil)
568{
569 uint8_t *old_tail;
570 uint8_t *tlv_len;
571 unsigned int i;
572
573 OSMO_ASSERT(msg);
574 OSMO_ASSERT(cil);
575
576 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
577 tlv_len = msgb_put(msg, 1);
578 old_tail = msg->tail;
579
580 msgb_put_u8(msg, cil->id_discr & 0x0f);
581
582 OSMO_ASSERT(cil->id_list_len <= GSM0808_CELL_ID_LIST2_MAXLEN)
583 switch (cil->id_discr) {
584 case CELL_IDENT_WHOLE_GLOBAL:
585 for (i = 0; i < cil->id_list_len; i++) {
586 const struct osmo_cell_global_id *id = &cil->id_list[i].global;
587 struct gsm48_loc_area_id lai;
Neels Hofmeyr8b8cd932018-03-23 01:47:37 +0100588 gsm48_generate_lai2(&lai, &id->lai);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100589 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
590 msgb_put_u16(msg, id->cell_identity);
591 }
592 break;
593 case CELL_IDENT_LAC_AND_CI:
594 for (i = 0; i < cil->id_list_len; i++) {
595 const struct osmo_lac_and_ci_id *id = &cil->id_list[i].lac_and_ci;
596 msgb_put_u16(msg, id->lac);
597 msgb_put_u16(msg, id->ci);
598 }
599 break;
600 case CELL_IDENT_CI:
601 for (i = 0; i < cil->id_list_len; i++)
602 msgb_put_u16(msg, cil->id_list[i].ci);
603 break;
604 case CELL_IDENT_LAI_AND_LAC:
605 for (i = 0; i < cil->id_list_len; i++) {
606 const struct osmo_location_area_id *id = &cil->id_list[i].lai_and_lac;
607 struct gsm48_loc_area_id lai;
Neels Hofmeyr8b8cd932018-03-23 01:47:37 +0100608 gsm48_generate_lai2(&lai, id);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100609 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
610 }
611 break;
612 case CELL_IDENT_LAC:
613 for (i = 0; i < cil->id_list_len; i++)
614 msgb_put_u16(msg, cil->id_list[i].lac);
615 break;
616 case CELL_IDENT_BSS:
617 case CELL_IDENT_NO_CELL:
618 /* Does not have any list items */
619 break;
620 default:
621 /* Support for other identifier list types is not implemented. */
622 OSMO_ASSERT(false);
623 }
624
625 *tlv_len = (uint8_t) (msg->tail - old_tail);
626 return *tlv_len + 2;
627}
628
629/*! DEPRECATED: Use gsm0808_enc_cell_id_list2 instead.
630 *
631 * Encode TS 08.08 Cell Identifier List IE
632 * \param[out] msg Message Buffer to which IE is to be appended
633 * \param[in] cil Cell ID List to be encoded
634 * \returns number of bytes appended to \a msg */
Philipp Maier783047e2017-03-29 11:35:50 +0200635uint8_t gsm0808_enc_cell_id_list(struct msgb *msg,
636 const struct gsm0808_cell_id_list *cil)
637{
638 uint8_t *old_tail;
639 uint8_t *tlv_len;
640 unsigned int i;
641
642 OSMO_ASSERT(msg);
643 OSMO_ASSERT(cil);
644
645 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
646 tlv_len = msgb_put(msg, 1);
647 old_tail = msg->tail;
648
649 msgb_put_u8(msg, cil->id_discr & 0x0f);
650
651 switch (cil->id_discr) {
652 case CELL_IDENT_LAC:
653 OSMO_ASSERT(cil->id_list_len <= CELL_ID_LIST_LAC_MAXLEN)
654 for (i=0;i<cil->id_list_len;i++) {
655 msgb_put_u16(msg, cil->id_list_lac[i]);
656 }
657 break;
658 case CELL_IDENT_BSS:
659 /* Does not have any list items */
660 break;
661 default:
662 /* FIXME: Implement support for all identifier list elements */
663 OSMO_ASSERT(false);
664 }
665
666 *tlv_len = (uint8_t) (msg->tail - old_tail);
667 return *tlv_len + 2;
668}
669
Stefan Sperling23381452018-03-15 19:38:15 +0100670/* Decode 5-byte LAI list element data (see TS 08.08 3.2.2.27) into MCC/MNC/LAC. */
671static void decode_lai(const uint8_t *data, struct osmo_location_area_id *decoded)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100672{
673 struct gsm48_loc_area_id lai;
674
Stefan Sperling23381452018-03-15 19:38:15 +0100675 /* Copy data to stack to prevent unaligned access in gsm48_decode_lai2(). */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100676 memcpy(&lai, data, sizeof(lai)); /* don't byte swap yet */
677
Stefan Sperling23381452018-03-15 19:38:15 +0100678 gsm48_decode_lai2(&lai, decoded);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100679}
680
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100681static 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 +0100682 size_t *consumed)
683{
684 struct osmo_cell_global_id *id;
685 uint16_t *ci_be;
686 size_t lai_offset;
687 int i = 0;
688 const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*ci_be);
689
690 *consumed = 0;
691 while (remain >= elemlen) {
692 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
693 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100694 id = &cil->id_list[i].global;
Stefan Sperling2873bf12018-03-14 18:38:41 +0100695 lai_offset = i * elemlen;
Stefan Sperling23381452018-03-15 19:38:15 +0100696 decode_lai(&data[lai_offset], &id->lai);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100697 ci_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]);
698 id->cell_identity = osmo_load16be(ci_be);
699 *consumed += elemlen;
700 remain -= elemlen;
701 i++;
702 }
703
704 return i;
705}
706
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100707static 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 +0100708 size_t *consumed)
709{
710 uint16_t *lacp_be, *ci_be;
711 struct osmo_lac_and_ci_id *id;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100712 int i = 0, j = 0;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100713 const size_t elemlen = sizeof(*lacp_be) + sizeof(*ci_be);
714
715 *consumed = 0;
716
717 if (remain < elemlen)
718 return -EINVAL;
719
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100720 lacp_be = (uint16_t *)(&data[j]);
721 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100722 while (remain >= elemlen) {
723 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
724 return -ENOSPC;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100725 id = &cil->id_list[i++].lac_and_ci;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100726 id->lac = osmo_load16be(lacp_be);
727 id->ci = osmo_load16be(ci_be);
728 *consumed += elemlen;
729 remain -= elemlen;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100730 j += elemlen;
731 lacp_be = (uint16_t *)(&data[j]);
732 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100733 }
734
735 return i;
736}
737
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100738static int parse_cell_id_ci_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain,
739 size_t *consumed)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100740{
741 const uint16_t *ci_be = (const uint16_t *)data;
742 int i = 0;
743 const size_t elemlen = sizeof(*ci_be);
744
745 *consumed = 0;
746 while (remain >= elemlen) {
747 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
748 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100749 cil->id_list[i++].ci = osmo_load16be(ci_be++);
Stefan Sperling9c62fc62018-03-16 10:23:34 +0100750 *consumed += elemlen;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100751 remain -= elemlen;
752 }
753 return i;
754}
755
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100756static 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 +0100757 size_t *consumed)
758{
759 struct osmo_location_area_id *id;
760 int i = 0;
761 const size_t elemlen = sizeof(struct gsm48_loc_area_id);
762
763 *consumed = 0;
764 while (remain >= elemlen) {
765 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
766 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100767 id = &cil->id_list[i].lai_and_lac;
Stefan Sperling23381452018-03-15 19:38:15 +0100768 decode_lai(&data[i * elemlen], id);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100769 *consumed += elemlen;
770 remain -= elemlen;
771 i++;
772 }
773
774 return i;
775}
776
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100777static 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 +0100778{
779 const uint16_t *lac_be = (const uint16_t *)data;
780 int i = 0;
781 const size_t elemlen = sizeof(*lac_be);
782
783 *consumed = 0;
784 while (remain >= elemlen) {
785 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
786 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100787 cil->id_list[i++].lac = osmo_load16be(lac_be++);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100788 *consumed += elemlen;
789 remain -= elemlen;
790 }
791 return i;
792}
793
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200794/*! Decode Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +0200795 * \param[out] cil Caller-provided memory to store Cell ID list
796 * \param[in] elem IE value to be decoded
797 * \param[in] len Length of \a elem in bytes
798 * \returns number of bytes parsed; negative on error */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100799int gsm0808_dec_cell_id_list2(struct gsm0808_cell_id_list2 *cil,
800 const uint8_t *elem, uint8_t len)
801{
802 uint8_t id_discr;
803 size_t bytes_elem = 0;
804 int list_len = 0;
805
806 OSMO_ASSERT(cil);
807 if (!elem)
808 return -EINVAL;
809 if (len == 0)
810 return -EINVAL;
811
812 memset(cil, 0, sizeof(*cil));
813
814 id_discr = *elem & 0x0f;
815 elem++;
816 len--;
817
818 switch (id_discr) {
819 case CELL_IDENT_WHOLE_GLOBAL:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100820 list_len = parse_cell_id_global_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100821 break;
822 case CELL_IDENT_LAC_AND_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100823 list_len = parse_cell_id_lac_and_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100824 break;
825 case CELL_IDENT_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100826 list_len = parse_cell_id_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100827 break;
828 case CELL_IDENT_LAI_AND_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100829 list_len = parse_cell_id_lai_and_lac(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100830 break;
831 case CELL_IDENT_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100832 list_len = parse_cell_id_lac_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100833 break;
834 case CELL_IDENT_BSS:
835 case CELL_IDENT_NO_CELL:
836 /* Does not have any list items */
837 break;
838 default:
839 /* Remaining cell identification types are not implemented. */
840 return -EINVAL;
841 }
842
843 if (list_len < 0) /* parsing error */
844 return list_len;
845
846 cil->id_discr = id_discr;
847 cil->id_list_len = list_len;
848
849 /* One byte for the cell ID discriminator + any remaining bytes in
850 * the IE which were consumed by the parser functions above. */
851 return 1 + (int)bytes_elem;
852}
853
854/*! DEPRECATED: Use gsm0808_dec_cell_id_list2 instead.
855 *
856 * Decode Cell Identifier List IE
857 * \param[out] cil Caller-provided memory to store Cell ID list
858 * \param[in] elem IE value to be decoded
859 * \param[in] len Length of \a elem in bytes
860 * \returns number of bytes parsed; negative on error */
Philipp Maier783047e2017-03-29 11:35:50 +0200861int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil,
862 const uint8_t *elem, uint8_t len)
863{
864 uint8_t id_discr;
865 const uint8_t *old_elem = elem;
866 unsigned int item_count = 0;
867
868 OSMO_ASSERT(cil);
869 if (!elem)
870 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200871 if (len == 0)
Philipp Maier783047e2017-03-29 11:35:50 +0200872 return -EINVAL;
873
874 memset(cil, 0, sizeof(*cil));
875
876 id_discr = *elem & 0x0f;
877 elem++;
878 len--;
879
880 cil->id_discr = id_discr;
881
882 switch (id_discr) {
883 case CELL_IDENT_LAC:
884 while (len >= 2) {
885 cil->id_list_lac[item_count] = osmo_load16be(elem);
886 elem += 2;
887 item_count++;
888 len -= 2;
889 }
890 case CELL_IDENT_BSS:
891 /* Does not have any list items */
892 break;
893 default:
894 /* FIXME: Implement support for all identifier list elements */
895 return -EINVAL;
896 }
897
898 cil->id_list_len = item_count;
899 return (int)(elem - old_elem);
900}
Harald Welte96e2a002017-06-12 21:44:18 +0200901
Neels Hofmeyr74663d92018-03-23 01:46:42 +0100902static bool same_cell_id_list_entries(const struct gsm0808_cell_id_list2 *a, int ai,
903 const struct gsm0808_cell_id_list2 *b, int bi)
904{
905 struct gsm0808_cell_id_list2 tmp = {
906 .id_discr = a->id_discr,
907 .id_list_len = 1,
908 };
909 uint8_t buf_a[32 + sizeof(struct msgb)];
910 uint8_t buf_b[32 + sizeof(struct msgb)];
911 struct msgb *msg_a = (void*)buf_a;
912 struct msgb *msg_b = (void*)buf_b;
913
914 msg_a->data_len = 32;
915 msg_b->data_len = 32;
916 msgb_reset(msg_a);
917 msgb_reset(msg_b);
918
919 if (a->id_discr != b->id_discr)
920 return false;
921 if (ai >= a->id_list_len
922 || bi >= b->id_list_len)
923 return false;
924
925 tmp.id_list[0] = a->id_list[ai];
926 gsm0808_enc_cell_id_list2(msg_a, &tmp);
927
928 tmp.id_list[0] = b->id_list[bi];
929 gsm0808_enc_cell_id_list2(msg_b, &tmp);
930
931 if (msg_a->len != msg_b->len)
932 return false;
933 if (memcmp(msg_a->data, msg_b->data, msg_a->len))
934 return false;
935
936 return true;
937}
938
939/*! Append entries from one Cell Identifier List to another.
940 * The cell identifier types must be identical between the two lists.
941 * \param dst[out] Append entries to this list.
942 * \param src[in] Append these entries to \a dst.
943 * \returns the nr of items added, or negative on error: -EINVAL if the id_discr mismatch
944 * between the lists, -ENOSPC if the destination list does not have enough space. If an error is
945 * returned, \a dst may have already been changed (particularly on -ENOSPC). Note that a return value
946 * of zero may occur when the src->id_list_len is zero, or when all entries from \a src already exist
947 * in \a dst, and does not indicate error per se. */
948int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src)
949{
950 int i, j;
951 int added = 0;
952
953 if (dst->id_list_len == 0
954 && dst->id_discr != CELL_IDENT_BSS)
955 dst->id_discr = src->id_discr;
956 else if (dst->id_discr != src->id_discr)
957 return -EINVAL;
958
959 for (i = 0; i < src->id_list_len; i++) {
960 /* don't add duplicate entries */
961 bool skip = false;
962 for (j = 0; j < dst->id_list_len; j++) {
963 if (same_cell_id_list_entries(dst, j, src, i)) {
964 skip = true;
965 break;
966 }
967 }
968 if (skip)
969 continue;
970
971 if (dst->id_list_len >= ARRAY_SIZE(dst->id_list))
972 return -ENOSPC;
973
974 dst->id_list[dst->id_list_len++] = src->id_list[i];
975 added ++;
976 }
977
978 return added;
979}
980
Neels Hofmeyr38e58412018-05-25 16:56:35 +0200981/*! Convert a single Cell Identifier to a Cell Identifier List with one entry.
982 * \param dst[out] Overwrite this list.
983 * \param src[in] Set \a dst to contain exactly this item.
984 */
985void gsm0808_cell_id_to_list(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id *src)
986{
987 if (!dst)
988 return;
989 if (!src) {
990 *dst = (struct gsm0808_cell_id_list2){
991 .id_discr = CELL_IDENT_NO_CELL,
992 };
993 return;
994 }
995
996 *dst = (struct gsm0808_cell_id_list2){
997 .id_discr = src->id_discr,
998 .id_list = { src->id },
999 .id_list_len = 1,
1000 };
1001
1002 switch (src->id_discr) {
1003 case CELL_IDENT_NO_CELL:
1004 case CELL_IDENT_BSS:
1005 dst->id_list_len = 0;
1006 break;
1007 default:
1008 break;
1009 }
1010}
1011
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001012/*! Encode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1013 * \param[out] msg Message Buffer to which IE is to be appended
1014 * \param[in] ci Cell ID to be encoded
1015 * \returns number of bytes appended to \a msg */
1016uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci)
1017{
1018 uint8_t rc;
1019 uint8_t *ie_tag;
1020 struct gsm0808_cell_id_list2 cil = {
1021 .id_discr = ci->id_discr,
1022 .id_list = { ci->id },
1023 .id_list_len = 1,
1024 };
1025
1026 OSMO_ASSERT(msg);
1027 OSMO_ASSERT(ci);
1028
1029 ie_tag = msg->tail;
1030 rc = gsm0808_enc_cell_id_list2(msg, &cil);
1031
1032 if (rc <= 0)
1033 return rc;
1034
1035 *ie_tag = GSM0808_IE_CELL_IDENTIFIER;
1036 return rc;
1037}
1038
1039/*! Decode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1040 * \param[out] ci Caller-provided memory to store Cell ID.
1041 * \param[in] elem IE value to be decoded.
1042 * \param[in] len Length of \a elem in bytes.
1043 * \returns number of bytes parsed; negative on error */
1044int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len)
1045{
1046 struct gsm0808_cell_id_list2 cil;
1047 int rc;
1048 rc = gsm0808_dec_cell_id_list2(&cil, elem, len);
1049 if (rc < 0)
1050 return rc;
1051 if (cil.id_discr == CELL_IDENT_BSS || cil.id_discr == CELL_IDENT_NO_CELL) {
1052 if (cil.id_list_len != 0)
1053 return -EINVAL;
1054 } else {
1055 if (cil.id_list_len != 1)
1056 return -EINVAL;
1057 }
1058 ci->id_discr = cil.id_discr;
1059 ci->id = cil.id_list[0];
1060 return rc;
1061}
1062
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001063/*! Convert the representation of the permitted speech codec identifier
Philipp Maier3149b0d2017-06-02 13:22:34 +02001064 * that is used in struct gsm0808_channel_type to the speech codec
1065 * representation we use in struct gsm0808_speech_codec.
1066 * \param[in] perm_spch to be converted (see also gsm0808_permitted_speech)
1067 * \returns GSM speech codec type; negative on error */
1068int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch)
1069{
1070 /*! The speech codec type, which is used in the channel type field to
1071 * signal the permitted speech versions (codecs) has a different
1072 * encoding than the type field in the speech codec type element
1073 * (See also 3GPP TS 48.008, 3.2.2.11 and 3.2.2.103) */
1074
1075 switch (perm_spch) {
1076 case GSM0808_PERM_FR1:
1077 return GSM0808_SCT_FR1;
1078 case GSM0808_PERM_FR2:
1079 return GSM0808_SCT_FR2;
1080 case GSM0808_PERM_FR3:
1081 return GSM0808_SCT_FR3;
1082 case GSM0808_PERM_FR4:
1083 return GSM0808_SCT_FR4;
1084 case GSM0808_PERM_FR5:
1085 return GSM0808_SCT_FR5;
1086 case GSM0808_PERM_HR1:
1087 return GSM0808_SCT_HR1;
1088 case GSM0808_PERM_HR3:
1089 return GSM0808_SCT_HR3;
1090 case GSM0808_PERM_HR4:
1091 return GSM0808_SCT_HR4;
1092 case GSM0808_PERM_HR6:
1093 return GSM0808_SCT_HR6;
1094 }
1095
1096 /* Invalid input */
1097 return -EINVAL;
1098}
1099
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001100/*! Extrapolate a speech codec field from a given permitted speech
Philipp Maier884ba0f2017-06-02 13:49:16 +02001101 * parameter (channel type).
1102 * \param[out] sc Caller provided memory to store the resulting speech codec
1103 * \param[in] perm_spch value that is used to derive the speech codec info
1104 * (see also: enum gsm0808_speech_codec_type in gsm0808_utils.h)
1105 * \returns zero when successful; negative on error */
1106int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc,
1107 uint8_t perm_spch)
1108{
1109 int rc;
1110
1111 memset(sc, 0, sizeof(*sc));
1112
1113 /* Determine codec type */
1114 rc = gsm0808_chan_type_to_speech_codec(perm_spch);
1115 if (rc < 0)
1116 return -EINVAL;
1117 sc->type = (uint8_t) rc;
1118
1119 /* Depending on the speech codec type, pick a default codec
1120 * configuration that exactly matches the configuration on the
1121 * air interface. */
1122 switch (sc->type) {
1123 case GSM0808_SCT_FR3:
1124 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR;
1125 break;
1126 case GSM0808_SCT_FR4:
1127 sc->cfg = GSM0808_SC_CFG_DEFAULT_OFR_AMR_WB;
1128 break;
1129 case GSM0808_SCT_FR5:
1130 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR_WB;
1131 break;
1132 case GSM0808_SCT_HR3:
1133 sc->cfg = GSM0808_SC_CFG_DEFAULT_HR_AMR;
1134 break;
1135 case GSM0808_SCT_HR4:
1136 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR_WB;
1137 break;
1138 case GSM0808_SCT_HR6:
1139 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR;
1140 break;
1141 default:
1142 /* Note: Not all codec types specify a default setting,
1143 * in this case, we just set the field to zero. */
1144 sc->cfg = 0;
1145 }
1146
1147 /* Tag all codecs as "Full IP"
1148 * (see als 3GPP TS 48.008 3.2.2.103) */
1149 sc->fi = true;
1150
1151 return 0;
1152}
1153
Philipp Maier5f2eb152018-09-19 13:40:21 +02001154/*! Determine a set of AMR speech codec configuration bits (S0-S15) from a
1155 * given GSM 04.08 AMR configuration struct.
1156 * \param[in] cfg AMR configuration in GSM 04.08 format.
1157 * \param[in] hint if the resulting configuration shall be used with a FR or HR TCH.
1158 * \returns configuration bits (S0-S15) */
Philipp Maier369015c2018-09-21 09:07:20 +02001159uint16_t gsm0808_sc_cfg_from_gsm48_mr_cfg(const struct gsm48_multi_rate_conf *cfg,
Philipp Maier5f2eb152018-09-19 13:40:21 +02001160 bool fr)
1161{
1162 uint16_t s15_s0 = 0;
1163
1164 /* Check each rate bit in the AMR multirate configuration and pick the
1165 * matching default configuration as specified in 3GPP TS 28.062,
1166 * Table 7.11.3.1.3-2. */
1167 if (cfg->m4_75)
1168 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_4_75;
1169 if (cfg->m5_15)
1170 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_15;
1171 if (cfg->m5_90)
1172 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_90;
1173 if (cfg->m6_70)
1174 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_6_70;
1175 if (cfg->m7_40)
1176 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_40;
1177 if (cfg->m7_95)
1178 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_95;
1179 if (cfg->m10_2)
1180 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_10_2;
1181 if (cfg->m12_2)
1182 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_12_2;
1183
1184 /* Note: 3GPP TS 48.008, chapter 3GPP TS 48.008 states that for AMR
1185 * some of the configuration bits must be coded as zeros. The applied
1186 * bitmask matches the default codec settings. See also the definition
1187 * of enum gsm0808_speech_codec_defaults in gsm_08_08.h and
1188 * 3GPP TS 28.062, Table 7.11.3.1.3-2. */
1189 if (fr)
1190 s15_s0 &= GSM0808_SC_CFG_DEFAULT_FR_AMR;
1191 else
1192 s15_s0 &= GSM0808_SC_CFG_DEFAULT_HR_AMR;
1193
1194 return s15_s0;
1195}
1196
Philipp Maier8515d032018-09-25 15:57:49 +02001197/*! Determine a GSM 04.08 AMR configuration struct from a set of speech codec
1198 * configuration bits (S0-S15)
1199 * \param[out] cfg AMR configuration in GSM 04.08 format.
1200 * \param[in] s15_s0 configuration bits (S0-S15). */
1201void gsm48_mr_cfg_from_gsm0808_sc_cfg(struct gsm48_multi_rate_conf *cfg,
1202 uint16_t s15_s0)
1203{
1204 memset(cfg, 0, sizeof(*cfg));
1205
1206 /* Strip option bits */
1207 s15_s0 &= 0x00ff;
1208
1209 /* Rate 5,15k must always be present */
1210 cfg->m5_15 = 1;
1211
1212 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_4_75 & 0xff) ==
1213 (GSM0808_SC_CFG_DEFAULT_AMR_4_75 & 0xff))
1214 cfg->m4_75 = 1;
1215 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_5_90 & 0xff) ==
1216 (GSM0808_SC_CFG_DEFAULT_AMR_5_90 & 0xff))
1217 cfg->m5_90 = 1;
1218 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_6_70 & 0xff) ==
1219 (GSM0808_SC_CFG_DEFAULT_AMR_6_70 & 0xff))
1220 cfg->m6_70 = 1;
1221 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_7_40 & 0xff) ==
1222 (GSM0808_SC_CFG_DEFAULT_AMR_7_40 & 0xff))
1223 cfg->m7_40 = 1;
1224 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_7_95 & 0xff) ==
1225 (GSM0808_SC_CFG_DEFAULT_AMR_7_95 & 0xff))
1226 cfg->m7_95 = 1;
1227 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_10_2 & 0xff) ==
1228 (GSM0808_SC_CFG_DEFAULT_AMR_10_2 & 0xff))
1229 cfg->m10_2 = 1;
1230 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_12_2 & 0xff) ==
1231 (GSM0808_SC_CFG_DEFAULT_AMR_12_2 & 0xff))
1232 cfg->m12_2 = 1;
1233
1234 cfg->ver = 1;
1235 cfg->icmi = 1;
1236}
1237
Maxed651d22018-11-07 15:25:05 +01001238int gsm0808_get_cipher_reject_cause(const struct tlv_parsed *tp)
1239{
1240 const uint8_t *buf = TLVP_VAL_MINLEN(tp, GSM0808_IE_CAUSE, 1);
1241
1242 if (!buf)
1243 return -EBADMSG;
1244
1245 if (TLVP_LEN(tp, GSM0808_IE_CAUSE) > 1) {
1246 if (!gsm0808_cause_ext(buf[0]))
1247 return -EINVAL;
1248 return buf[1];
1249 }
1250
1251 return buf[0];
1252}
1253
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001254/*! Print a human readable name of the cell identifier to the char buffer.
1255 * This is useful both for struct gsm0808_cell_id and struct gsm0808_cell_id_list2.
1256 * See also gsm0808_cell_id_name() and gsm0808_cell_id_list_name().
1257 * \param[out] buf Destination buffer to write string representation to.
1258 * \param[in] buflen Amount of memory available in \a buf.
1259 * \param[in] id_discr Cell Identifier type.
1260 * \param[in] u Cell Identifer value.
1261 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1262 * or that would have been written if the buffer were large enough.
1263 */
1264int gsm0808_cell_id_u_name(char *buf, size_t buflen,
1265 enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
1266{
1267 switch (id_discr) {
1268 case CELL_IDENT_LAC:
1269 return snprintf(buf, buflen, "%u", u->lac);
1270 case CELL_IDENT_CI:
1271 return snprintf(buf, buflen, "%u", u->ci);
1272 case CELL_IDENT_LAC_AND_CI:
1273 return snprintf(buf, buflen, "%u-%u", u->lac_and_ci.lac, u->lac_and_ci.ci);
1274 case CELL_IDENT_LAI_AND_LAC:
1275 return snprintf(buf, buflen, "%s", osmo_lai_name(&u->lai_and_lac));
1276 case CELL_IDENT_WHOLE_GLOBAL:
1277 return snprintf(buf, buflen, "%s", osmo_cgi_name(&u->global));
1278 default:
1279 /* For CELL_IDENT_BSS and CELL_IDENT_NO_CELL, just print the discriminator.
1280 * Same for kinds we have no string representation of yet. */
1281 return snprintf(buf, buflen, "%s", gsm0808_cell_id_discr_name(id_discr));
1282 }
1283}
1284
1285/*! value_string[] for enum CELL_IDENT. */
1286const struct value_string gsm0808_cell_id_discr_names[] = {
1287 { CELL_IDENT_WHOLE_GLOBAL, "CGI" },
1288 { CELL_IDENT_LAC_AND_CI, "LAC-CI" },
1289 { CELL_IDENT_CI, "CI" },
1290 { CELL_IDENT_NO_CELL, "NO-CELL" },
1291 { CELL_IDENT_LAI_AND_LAC, "LAI" },
1292 { CELL_IDENT_LAC, "LAC" },
1293 { CELL_IDENT_BSS, "BSS" },
1294 { CELL_IDENT_UTRAN_PLMN_LAC_RNC, "UTRAN-PLMN-LAC-RNC" },
1295 { CELL_IDENT_UTRAN_RNC, "UTRAN-RNC" },
1296 { CELL_IDENT_UTRAN_LAC_RNC, "UTRAN-LAC-RNC" },
1297 { 0, NULL }
1298};
1299
1300#define APPEND_THING(func, args...) do { \
1301 int remain = buflen - (pos - buf); \
1302 int l = func(pos, remain, ##args); \
1303 if (l < 0 || l > remain) \
1304 pos = buf + buflen; \
1305 else \
1306 pos += l; \
1307 if (l > 0) \
1308 total_len += l; \
1309 } while(0)
1310#define APPEND_STR(fmt, args...) APPEND_THING(snprintf, fmt, ##args)
1311#define APPEND_CELL_ID_U(DISCR, U) APPEND_THING(gsm0808_cell_id_u_name, DISCR, U)
1312
1313static const char *gsm0808_cell_id_name_buf(const struct gsm0808_cell_id *cid,
1314 char *buf, size_t buflen)
1315{
1316 char *pos = buf;
1317 int total_len = 0;
1318 APPEND_STR("%s:", gsm0808_cell_id_discr_name(cid->id_discr));
1319 APPEND_CELL_ID_U(cid->id_discr, &cid->id);
1320 return buf;
1321}
1322
1323/*! Return a human readable representation of a Cell Identifier, like "LAC:123"
1324 * or "CGI:001-01-42-23".
1325 * \param[in] cid Cell Identifer.
1326 * \returns String in a static buffer.
1327 */
1328const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid)
1329{
1330 static char buf[64];
1331 return gsm0808_cell_id_name_buf(cid, buf, sizeof(buf));
1332}
1333
1334/*! Like gsm0808_cell_id_name() but uses a different static buffer.
1335 * \param[in] cid Cell Identifer.
1336 * \returns String in a static buffer.
1337 */
1338const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid)
1339{
1340 static char buf[64];
1341 return gsm0808_cell_id_name_buf(cid, buf, sizeof(buf));
1342}
1343
1344/*! Return a human readable representation of the Cell Identifier List, like
1345 * "LAC[2]:{123, 456}".
1346 * The return value semantics are like snprintf() and thus allow ensuring a complete
1347 * untruncated string by determining the required string length from the return value.
1348 * If buflen > 0, always nul-terminate the string in buf, also when it is truncated.
1349 * If buflen == 0, do not modify buf, just return the would-be length.
1350 * \param[out] buf Destination buffer to write string representation to.
1351 * \param[in] buflen Amount of memory available in \a buf.
1352 * \param[in] cil Cell Identifer List.
1353 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1354 * or that would have been written if the buffer were large enough.
1355 */
1356int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil)
1357{
1358 char *pos = buf;
1359 int total_len = 0;
1360 int i;
1361
1362 APPEND_STR("%s[%u]", gsm0808_cell_id_discr_name(cil->id_discr), cil->id_list_len);
1363
1364 switch (cil->id_discr) {
1365 case CELL_IDENT_BSS:
1366 case CELL_IDENT_NO_CELL:
1367 return total_len;
1368 default:
1369 break;
1370 }
1371
1372 APPEND_STR(":{");
1373
1374 for (i = 0; i < cil->id_list_len; i++) {
1375 if (i)
1376 APPEND_STR(", ");
1377 APPEND_CELL_ID_U(cil->id_discr, &cil->id_list[i]);
1378 }
1379
1380 APPEND_STR("}");
1381 return total_len;
1382}
1383
1384/*! Return a human-readable representation of \a cil in a static buffer.
1385 * If the list is too long, the output may be truncated.
1386 * See also gsm0808_cell_id_list_name_buf(). */
1387const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil)
1388{
1389 static char buf[1024];
1390 gsm0808_cell_id_list_name_buf(buf, sizeof(buf), cil);
1391 return buf;
1392}
1393
1394#undef APPEND_STR
1395#undef APPEND_CELL_ID_U
1396
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02001397const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct)
1398{
1399 static char buf[128];
1400 snprintf(buf, sizeof(buf), "ch_indctr=0x%x ch_rate_type=0x%x perm_spch=%s",
1401 ct->ch_indctr, ct->ch_rate_type,
1402 osmo_hexdump(ct->perm_spch, ct->perm_spch_len));
1403 return buf;
1404}
1405
Harald Welte96e2a002017-06-12 21:44:18 +02001406/*! @} */