blob: 38a8664cbb9c6c3dfed20ca1f1a141ce1c727d22 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*
Harald Weltee08da972017-11-13 01:00:26 +09002 * (C) 2016 by sysmocom - s.f.m.c. GmbH, Author: Philipp Maier
Philipp Maier22401432017-03-24 17:59:26 +01003 * All Rights Reserved
4 *
Harald Weltee08da972017-11-13 01:00:26 +09005 * SPDX-License-Identifier: GPL-2.0+
Philipp Maier22401432017-03-24 17:59:26 +01006 *
7 * This program is free software; you can redistribute it and/or modify
Harald Weltee08da972017-11-13 01:00:26 +09008 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
Philipp Maier22401432017-03-24 17:59:26 +010010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Weltee08da972017-11-13 01:00:26 +090015 * GNU General Public License for more details.
Philipp Maier22401432017-03-24 17:59:26 +010016 *
Harald Weltee08da972017-11-13 01:00:26 +090017 * You should have received a copy of the GNU General Public License
Philipp Maier22401432017-03-24 17:59:26 +010018 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
Harald Welte20725b92017-05-15 12:50:04 +020022#include "config.h"
23
Philipp Maier22401432017-03-24 17:59:26 +010024#include <osmocom/core/utils.h>
25#include <osmocom/core/msgb.h>
Harald Welte95871da2017-05-15 12:11:36 +020026#include <osmocom/core/byteswap.h>
Philipp Maier22401432017-03-24 17:59:26 +010027#include <string.h>
Philipp Maier22401432017-03-24 17:59:26 +010028#include <errno.h>
29#include <osmocom/gsm/protocol/gsm_08_08.h>
Stefan Sperling11a4d9d2018-02-15 18:28:04 +010030#include <osmocom/gsm/gsm48.h>
31#include <osmocom/gsm/gsm0808_utils.h>
Philipp Maier22401432017-03-24 17:59:26 +010032
33#define IP_V4_ADDR_LEN 4
34#define IP_V6_ADDR_LEN 16
35#define IP_PORT_LEN 2
36
Philipp Maiere0c65302017-03-28 17:05:40 +020037#define CHANNEL_TYPE_ELEMENT_MAXLEN 11
38#define CHANNEL_TYPE_ELEMENT_MINLEN 3
Philipp Maier14e76b92017-03-28 18:36:52 +020039#define ENCRYPT_INFO_ELEMENT_MINLEN 1
Philipp Maier6f725d62017-03-24 18:03:17 +010040
Harald Welte20725b92017-05-15 12:50:04 +020041#ifdef HAVE_SYS_SOCKET_H
42
43#include <sys/socket.h>
44#include <netinet/in.h>
Harald Welte96e2a002017-06-12 21:44:18 +020045
46/*! \addtogroup gsm0808
47 * @{
Harald Welte37b61652017-10-16 18:46:03 +020048 * \file gsm0808_utils.c
Harald Welte96e2a002017-06-12 21:44:18 +020049 */
50
Philipp Maier4f4905f2018-11-30 13:36:12 +010051/*! Encode TS 08.08 AoIP Cause IE
52 * \param[out] msg Message Buffer to which to append IE
53 * \param[in] cause Cause code to be used in IE
54 * \returns number of bytes added to \a msg */
55uint8_t gsm0808_enc_cause(struct msgb *msg, uint16_t cause)
56{
57 /* See also 3GPP TS 48.008 3.2.2.5 Cause */
58 uint8_t *old_tail;
59 bool extended;
60
61 old_tail = msg->tail;
62
63 extended = gsm0808_cause_ext(cause >> 8);
64
65 msgb_put_u8(msg, GSM0808_IE_CAUSE);
66 if (extended) {
67 msgb_put_u8(msg, 2);
68 msgb_put_u16(msg, cause);
69 } else {
70 msgb_put_u8(msg, 1);
71 msgb_put_u8(msg, (uint8_t) (cause & 0xFF));
72 }
73
74 return (uint8_t) (msg->tail - old_tail);
75}
76
Neels Hofmeyr87e45502017-06-20 00:17:59 +020077/*! Encode TS 08.08 AoIP transport address IE
Harald Welte96e2a002017-06-12 21:44:18 +020078 * \param[out] msg Message Buffer to which to append IE
79 * \param[in] ss Socket Address to be used in IE
80 * \returns number of bytes added to \a msg */
Philipp Maier22401432017-03-24 17:59:26 +010081uint8_t gsm0808_enc_aoip_trasp_addr(struct msgb *msg,
82 const struct sockaddr_storage *ss)
83{
84 /* See also 3GPP TS 48.008 3.2.2.102 AoIP Transport Layer Address */
85 struct sockaddr_in *sin;
86 struct sockaddr_in6 *sin6;
87 uint16_t port = 0;
88 uint8_t *ptr;
89 uint8_t *old_tail;
90 uint8_t *tlv_len;
91
92 OSMO_ASSERT(msg);
93 OSMO_ASSERT(ss);
94 OSMO_ASSERT(ss->ss_family == AF_INET || ss->ss_family == AF_INET6);
95
96 msgb_put_u8(msg, GSM0808_IE_AOIP_TRASP_ADDR);
97 tlv_len = msgb_put(msg,1);
98 old_tail = msg->tail;
99
100 switch (ss->ss_family) {
101 case AF_INET:
102 sin = (struct sockaddr_in *)ss;
Harald Welte95871da2017-05-15 12:11:36 +0200103 port = osmo_ntohs(sin->sin_port);
Philipp Maier22401432017-03-24 17:59:26 +0100104 ptr = msgb_put(msg, IP_V4_ADDR_LEN);
105 memcpy(ptr, &sin->sin_addr.s_addr, IP_V4_ADDR_LEN);
106 break;
107 case AF_INET6:
108 sin6 = (struct sockaddr_in6 *)ss;
Harald Welte95871da2017-05-15 12:11:36 +0200109 port = osmo_ntohs(sin6->sin6_port);
Philipp Maier22401432017-03-24 17:59:26 +0100110 ptr = msgb_put(msg, IP_V6_ADDR_LEN);
111 memcpy(ptr, sin6->sin6_addr.s6_addr, IP_V6_ADDR_LEN);
112 break;
113 }
114
115 msgb_put_u16(msg, port);
116
117 *tlv_len = (uint8_t) (msg->tail - old_tail);
118 return *tlv_len + 2;
119}
120
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200121/*! Decode TS 08.08 AoIP transport address IE
Harald Welte96e2a002017-06-12 21:44:18 +0200122 * \param[out] ss Caller-provided memory where decoded socket addr is stored
123 * \param[in] elem pointer to IE value
124 * \param[in] len length of \a elem in bytes
125 * \returns number of bytes parsed */
Philipp Maier22401432017-03-24 17:59:26 +0100126int gsm0808_dec_aoip_trasp_addr(struct sockaddr_storage *ss,
127 const uint8_t *elem, uint8_t len)
128{
129 /* See also 3GPP TS 48.008 3.2.2.102 AoIP Transport Layer Address */
130 struct sockaddr_in sin;
131 struct sockaddr_in6 sin6;
132 const uint8_t *old_elem = elem;
133
134 OSMO_ASSERT(ss);
135 if (!elem)
136 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200137 if (len == 0)
Philipp Maier22401432017-03-24 17:59:26 +0100138 return -EINVAL;
139
140 memset(ss, 0, sizeof(*ss));
141
142 switch (len) {
143 case IP_V4_ADDR_LEN + IP_PORT_LEN:
144 memset(&sin, 0, sizeof(sin));
145 sin.sin_family = AF_INET;
146
147 memcpy(&sin.sin_addr.s_addr, elem, IP_V4_ADDR_LEN);
148 elem += IP_V4_ADDR_LEN;
149 sin.sin_port = osmo_load16le(elem);
150 elem += IP_PORT_LEN;
151
152 memcpy(ss, &sin, sizeof(sin));
153 break;
154 case IP_V6_ADDR_LEN + IP_PORT_LEN:
155 memset(&sin6, 0, sizeof(sin6));
156 sin6.sin6_family = AF_INET6;
157
158 memcpy(sin6.sin6_addr.s6_addr, elem, IP_V6_ADDR_LEN);
159 elem += IP_V6_ADDR_LEN;
160 sin6.sin6_port = osmo_load16le(elem);
161 elem += IP_PORT_LEN;
162
163 memcpy(ss, &sin6, sizeof(sin6));
164 break;
165 default:
166 /* Malformed element! */
167 return -EINVAL;
168 break;
169 }
170
171 return (int)(elem - old_elem);
172}
Philipp Maier6f725d62017-03-24 18:03:17 +0100173
Harald Welte20725b92017-05-15 12:50:04 +0200174#endif /* HAVE_SYS_SOCKET_H */
175
Philipp Maier6f725d62017-03-24 18:03:17 +0100176/* Helper function for gsm0808_enc_speech_codec()
177 * and gsm0808_enc_speech_codec_list() */
178static uint8_t enc_speech_codec(struct msgb *msg,
179 const struct gsm0808_speech_codec *sc)
180{
181 /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
182 uint8_t header = 0;
183 uint8_t *old_tail;
Harald Welte459a1802018-06-28 09:24:17 +0200184 bool type_extended = false;
Philipp Maierbb839662017-06-01 17:11:19 +0200185
186 /* Note: Extended codec types are codec types that require 8 instead
187 * of 4 bit to fully specify the selected codec. In the following,
188 * we check if we work with an extended type or not. We also check
189 * if the codec type is valid at all. */
190 switch(sc->type) {
191 case GSM0808_SCT_FR1:
192 case GSM0808_SCT_FR2:
193 case GSM0808_SCT_FR3:
194 case GSM0808_SCT_FR4:
195 case GSM0808_SCT_FR5:
196 case GSM0808_SCT_HR1:
197 case GSM0808_SCT_HR3:
198 case GSM0808_SCT_HR4:
199 case GSM0808_SCT_HR6:
200 type_extended = false;
201 break;
202 case GSM0808_SCT_CSD:
203 type_extended = true;
204 break;
205 default:
206 /* Invalid codec type specified */
207 OSMO_ASSERT(false);
208 break;
209 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100210
211 old_tail = msg->tail;
212
213 if (sc->fi)
214 header |= (1 << 7);
215 if (sc->pi)
216 header |= (1 << 6);
217 if (sc->pt)
218 header |= (1 << 5);
219 if (sc->tf)
220 header |= (1 << 4);
Philipp Maierbb839662017-06-01 17:11:19 +0200221
222 if (type_extended) {
Philipp Maier6f725d62017-03-24 18:03:17 +0100223 header |= 0x0f;
224 msgb_put_u8(msg, header);
Philipp Maierbb839662017-06-01 17:11:19 +0200225 msgb_put_u8(msg, sc->type);
Philipp Maier6f725d62017-03-24 18:03:17 +0100226 } else {
227 OSMO_ASSERT(sc->type < 0x0f);
228 header |= sc->type;
229 msgb_put_u8(msg, header);
Philipp Maier6f725d62017-03-24 18:03:17 +0100230 }
231
Philipp Maierbb839662017-06-01 17:11:19 +0200232 /* Note: Whether a configuration is present or not depends on the
233 * selected codec type. If present, it can either consist of one
234 * or two octets, depending on the codec type */
235 switch (sc->type) {
236 case GSM0808_SCT_FR3:
237 case GSM0808_SCT_HR3:
238 case GSM0808_SCT_HR6:
Philipp Maier7e27b142018-03-22 17:26:46 +0100239 msgb_put_u16(msg, osmo_ntohs(sc->cfg));
Philipp Maierbb839662017-06-01 17:11:19 +0200240 break;
241 case GSM0808_SCT_FR4:
242 case GSM0808_SCT_FR5:
243 case GSM0808_SCT_HR4:
244 case GSM0808_SCT_CSD:
245 OSMO_ASSERT((sc->cfg & 0xff00) == 0)
246 msgb_put_u8(msg, (uint8_t) sc->cfg & 0xff);
247 break;
248 default:
249 OSMO_ASSERT(sc->cfg == 0);
250 break;
251 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100252
253 return (uint8_t) (msg->tail - old_tail);
254}
255
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200256/*! Encode TS 08.08 Speech Codec IE
Harald Welte96e2a002017-06-12 21:44:18 +0200257 * \param[out] msg Message Buffer to which IE will be appended
258 * \param[in] sc Speech Codec to be encoded into IE
259 * \returns number of bytes appended to \a msg */
Philipp Maier6f725d62017-03-24 18:03:17 +0100260uint8_t gsm0808_enc_speech_codec(struct msgb *msg,
261 const struct gsm0808_speech_codec *sc)
262{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200263 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100264 uint8_t *old_tail;
265 uint8_t *tlv_len;
266
267 OSMO_ASSERT(msg);
268 OSMO_ASSERT(sc);
269
270 msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC);
271 tlv_len = msgb_put(msg, 1);
272 old_tail = msg->tail;
273
274 enc_speech_codec(msg, sc);
275
276 *tlv_len = (uint8_t) (msg->tail - old_tail);
277 return *tlv_len + 2;
278}
279
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200280/*! Decode TS 08.08 Speech Codec IE
Harald Welte96e2a002017-06-12 21:44:18 +0200281 * \param[out] sc Caller-allocated memory for Speech Codec
282 * \param[in] elem IE value to be decoded
283 * \param[in] len Length of \a elem in bytes
284 * \returns number of bytes parsed; negative on error */
Philipp Maier6f725d62017-03-24 18:03:17 +0100285int gsm0808_dec_speech_codec(struct gsm0808_speech_codec *sc,
286 const uint8_t *elem, uint8_t len)
287{
288 /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
289 uint8_t header;
290 const uint8_t *old_elem = elem;
291
292 OSMO_ASSERT(sc);
293 if (!elem)
294 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200295 if (len == 0)
Philipp Maier6f725d62017-03-24 18:03:17 +0100296 return -EINVAL;
297
298 memset(sc, 0, sizeof(*sc));
299
300 header = *elem;
301
Philipp Maier85a6af22017-04-28 10:55:05 +0200302 /* An extended codec type needs at least two fields,
303 * bail if the input data length is not sufficient. */
Philipp Maier6f725d62017-03-24 18:03:17 +0100304 if ((header & 0x0F) == 0x0F && len < 2)
305 return -EINVAL;
Philipp Maier6f725d62017-03-24 18:03:17 +0100306
307 elem++;
308 len--;
309
310 if (header & (1 << 7))
311 sc->fi = true;
312 if (header & (1 << 6))
313 sc->pi = true;
314 if (header & (1 << 5))
315 sc->pt = true;
316 if (header & (1 << 4))
317 sc->tf = true;
318
319 if ((header & 0x0F) != 0x0F) {
320 sc->type = (header & 0x0F);
Philipp Maierbb839662017-06-01 17:11:19 +0200321 } else {
322 sc->type = *elem;
323 elem++;
324 len--;
Philipp Maier6f725d62017-03-24 18:03:17 +0100325 }
326
Philipp Maierbb839662017-06-01 17:11:19 +0200327 /* Note: Whether a configuration is present or not depends on the
328 * selected codec type. If present, it can either consist of one or
329 * two octets depending on the codec type */
330 switch (sc->type) {
331 case GSM0808_SCT_FR1:
332 case GSM0808_SCT_FR2:
333 case GSM0808_SCT_HR1:
334 break;
335 case GSM0808_SCT_HR4:
336 case GSM0808_SCT_CSD:
337 case GSM0808_SCT_FR4:
338 case GSM0808_SCT_FR5:
339 if (len < 1)
340 return -EINVAL;
341 sc->cfg = *elem;
342 elem++;
343 break;
344 case GSM0808_SCT_FR3:
345 case GSM0808_SCT_HR3:
346 case GSM0808_SCT_HR6:
347 if (len < 2)
348 return -EINVAL;
Philipp Maier7e27b142018-03-22 17:26:46 +0100349 sc->cfg = osmo_load16le(elem);
Philipp Maierbb839662017-06-01 17:11:19 +0200350 elem += 2;
351 break;
352 default:
353 /* Invalid codec type => malformed speech codec element! */
354 return -EINVAL;
355 break;
356 }
Philipp Maier6f725d62017-03-24 18:03:17 +0100357
358 return (int)(elem - old_elem);
359}
360
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200361/*! Encode TS 08.08 Speech Codec list
Harald Welte96e2a002017-06-12 21:44:18 +0200362 * \param[out] msg Message Buffer to which IE is to be appended
363 * \param[in] scl Speech Codec List to be encoded into IE
364 * \returns number of bytes added to \a msg */
Philipp Maier6f725d62017-03-24 18:03:17 +0100365uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg,
366 const struct gsm0808_speech_codec_list *scl)
367{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200368 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100369 uint8_t *old_tail;
370 uint8_t *tlv_len;
371 unsigned int i;
372 uint8_t rc;
373 unsigned int bytes_used = 0;
374
375 OSMO_ASSERT(msg);
376 OSMO_ASSERT(scl);
377
Philipp Maier6f725d62017-03-24 18:03:17 +0100378 msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC_LIST);
379 tlv_len = msgb_put(msg, 1);
380 old_tail = msg->tail;
381
382 for (i = 0; i < scl->len; i++) {
383 rc = enc_speech_codec(msg, &scl->codec[i]);
384 OSMO_ASSERT(rc >= 1);
385 bytes_used += rc;
386 OSMO_ASSERT(bytes_used <= 255);
387 }
388
389 *tlv_len = (uint8_t) (msg->tail - old_tail);
390 return *tlv_len + 2;
391}
392
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200393/*! Decode TS 08.08 Speech Codec list IE
Harald Welte96e2a002017-06-12 21:44:18 +0200394 * \param[out] scl Caller-provided memory to store codec list
395 * \param[in] elem IE value to be decoded
396 * \param[in] len Length of \a elem in bytes
397 * \returns number of bytes parsed; negative on error */
Philipp Maier6f725d62017-03-24 18:03:17 +0100398int gsm0808_dec_speech_codec_list(struct gsm0808_speech_codec_list *scl,
399 const uint8_t *elem, uint8_t len)
400{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200401 /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
Philipp Maier6f725d62017-03-24 18:03:17 +0100402 const uint8_t *old_elem = elem;
403 unsigned int i;
404 int rc;
405 uint8_t decoded = 0;
406
407 OSMO_ASSERT(scl);
408 if (!elem)
409 return -EINVAL;
Philipp Maier6f725d62017-03-24 18:03:17 +0100410
411 memset(scl, 0, sizeof(*scl));
412
413 for (i = 0; i < ARRAY_SIZE(scl->codec); i++) {
414 if (len <= 0)
415 break;
416
417 rc = gsm0808_dec_speech_codec(&scl->codec[i], elem, len);
418 if (rc < 1)
419 return -EINVAL;
420
421 elem+=rc;
422 len -= rc;
423 decoded++;
424 }
425
426 scl->len = decoded;
427
Philipp Maier6f725d62017-03-24 18:03:17 +0100428 return (int)(elem - old_elem);
429}
Philipp Maiere0c65302017-03-28 17:05:40 +0200430
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200431/*! Encode TS 08.08 Channel Type IE
Harald Welte96e2a002017-06-12 21:44:18 +0200432 * \param[out] msg Message Buffer to which IE is to be appended
433 * \param[in] ct Channel Type to be encoded
434 * \returns number of bytes added to \a msg */
Philipp Maiere0c65302017-03-28 17:05:40 +0200435uint8_t gsm0808_enc_channel_type(struct msgb *msg,
436 const struct gsm0808_channel_type *ct)
437{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200438 /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */
Philipp Maiere0c65302017-03-28 17:05:40 +0200439 unsigned int i;
440 uint8_t byte;
441 uint8_t *old_tail;
442 uint8_t *tlv_len;
443
444 OSMO_ASSERT(msg);
445 OSMO_ASSERT(ct);
446 OSMO_ASSERT(ct->perm_spch_len <= CHANNEL_TYPE_ELEMENT_MAXLEN - 2);
447
448 /* FIXME: Implement encoding support for Data
449 * and Speech + CTM Text Telephony */
450 if ((ct->ch_indctr & 0x0f) != GSM0808_CHAN_SPEECH
451 && (ct->ch_indctr & 0x0f) != GSM0808_CHAN_SIGN)
452 OSMO_ASSERT(false);
453
454 msgb_put_u8(msg, GSM0808_IE_CHANNEL_TYPE);
455 tlv_len = msgb_put(msg, 1);
456 old_tail = msg->tail;
457
458 msgb_put_u8(msg, ct->ch_indctr & 0x0f);
459 msgb_put_u8(msg, ct->ch_rate_type);
460
461 for (i = 0; i < ct->perm_spch_len; i++) {
462 byte = ct->perm_spch[i];
463
464 if (i < ct->perm_spch_len - 1)
465 byte |= 0x80;
466 msgb_put_u8(msg, byte);
467 }
468
469 *tlv_len = (uint8_t) (msg->tail - old_tail);
470 return *tlv_len + 2;
471}
472
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200473/*! Decode TS 08.08 Channel Type IE
Harald Welte96e2a002017-06-12 21:44:18 +0200474 * \param[out] ct Caller-provided memory to store channel type
475 * \param[in] elem IE Value to be decoded
476 * \param[in] len Length of \a elem in bytes
477 * \returns number of bytes parsed; negative on error */
Philipp Maiere0c65302017-03-28 17:05:40 +0200478int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct,
479 const uint8_t *elem, uint8_t len)
480{
Philipp Maier452a6bb2017-06-23 00:29:34 +0200481 /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */
Philipp Maiere0c65302017-03-28 17:05:40 +0200482 unsigned int i;
483 uint8_t byte;
484 const uint8_t *old_elem = elem;
485
486 OSMO_ASSERT(ct);
487 if (!elem)
488 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200489 if (len < 3 || len > 11)
Philipp Maiere0c65302017-03-28 17:05:40 +0200490 return -EINVAL;
491
492 memset(ct, 0, sizeof(*ct));
493
494 ct->ch_indctr = (*elem) & 0x0f;
495 elem++;
496 ct->ch_rate_type = (*elem) & 0x0f;
497 elem++;
498
499 for (i = 0; i < ARRAY_SIZE(ct->perm_spch); i++) {
500 byte = *elem;
501 elem++;
502 ct->perm_spch[i] = byte & 0x7f;
503 if ((byte & 0x80) == 0x00)
504 break;
505 }
506 ct->perm_spch_len = i + 1;
507
508 return (int)(elem - old_elem);
509}
Philipp Maier14e76b92017-03-28 18:36:52 +0200510
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200511/*! Encode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200512 * \param[out] msg Message Buffer to which IE is to be appended
513 * \param[in] ei Encryption Information to be encoded
514 * \returns number of bytes appended to \a msg */
Philipp Maier14e76b92017-03-28 18:36:52 +0200515uint8_t gsm0808_enc_encrypt_info(struct msgb *msg,
516 const struct gsm0808_encrypt_info *ei)
517{
518 unsigned int i;
519 uint8_t perm_algo = 0;
520 uint8_t *ptr;
521 uint8_t *old_tail;
522 uint8_t *tlv_len;
523
524 OSMO_ASSERT(msg);
525 OSMO_ASSERT(ei);
526 OSMO_ASSERT(ei->key_len <= ARRAY_SIZE(ei->key));
527 OSMO_ASSERT(ei->perm_algo_len <= ENCRY_INFO_PERM_ALGO_MAXLEN);
528
529 msgb_put_u8(msg, GSM0808_IE_ENCRYPTION_INFORMATION);
530 tlv_len = msgb_put(msg, 1);
531 old_tail = msg->tail;
532
533 for (i = 0; i < ei->perm_algo_len; i++) {
534 /* Note: gsm_08_08.h defines the permitted algorithms
535 * as an enum which ranges from 0x01 to 0x08 */
536 OSMO_ASSERT(ei->perm_algo[i] != 0);
537 OSMO_ASSERT(ei->perm_algo[i] <= ENCRY_INFO_PERM_ALGO_MAXLEN);
538 perm_algo |= (1 << (ei->perm_algo[i] - 1));
539 }
540
541 msgb_put_u8(msg, perm_algo);
542 ptr = msgb_put(msg, ei->key_len);
543 memcpy(ptr, ei->key, ei->key_len);
544
545 *tlv_len = (uint8_t) (msg->tail - old_tail);
546 return *tlv_len + 2;
547}
548
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200549/*! Decode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200550 * \param[out] ei Caller-provided memory to store encryption information
551 * \param[in] elem IE value to be decoded
552 * \param[in] len Length of \a elem in bytes
553 * \returns number of bytes parsed; negative on error */
Philipp Maier14e76b92017-03-28 18:36:52 +0200554int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei,
555 const uint8_t *elem, uint8_t len)
556{
557 uint8_t perm_algo;
558 unsigned int i;
559 unsigned int perm_algo_len = 0;
560 const uint8_t *old_elem = elem;
561
562 OSMO_ASSERT(ei);
563 if (!elem)
564 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200565 if (len == 0)
Philipp Maier14e76b92017-03-28 18:36:52 +0200566 return -EINVAL;
567
568 memset(ei, 0, sizeof(*ei));
569
570 perm_algo = *elem;
571 elem++;
572
573 for (i = 0; i < ENCRY_INFO_PERM_ALGO_MAXLEN; i++) {
574 if (perm_algo & (1 << i)) {
575 ei->perm_algo[perm_algo_len] = i + 1;
576 perm_algo_len++;
577 }
578 }
579 ei->perm_algo_len = perm_algo_len;
580
581 ei->key_len = len - 1;
582 memcpy(ei->key, elem, ei->key_len);
583 elem+=ei->key_len;
584
585 return (int)(elem - old_elem);
586}
Philipp Maier783047e2017-03-29 11:35:50 +0200587
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200588/*! Encode TS 08.08 Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +0200589 * \param[out] msg Message Buffer to which IE is to be appended
590 * \param[in] cil Cell ID List to be encoded
591 * \returns number of bytes appended to \a msg */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100592uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg,
593 const struct gsm0808_cell_id_list2 *cil)
594{
595 uint8_t *old_tail;
596 uint8_t *tlv_len;
597 unsigned int i;
598
599 OSMO_ASSERT(msg);
600 OSMO_ASSERT(cil);
601
602 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
603 tlv_len = msgb_put(msg, 1);
604 old_tail = msg->tail;
605
606 msgb_put_u8(msg, cil->id_discr & 0x0f);
607
608 OSMO_ASSERT(cil->id_list_len <= GSM0808_CELL_ID_LIST2_MAXLEN)
609 switch (cil->id_discr) {
610 case CELL_IDENT_WHOLE_GLOBAL:
611 for (i = 0; i < cil->id_list_len; i++) {
612 const struct osmo_cell_global_id *id = &cil->id_list[i].global;
613 struct gsm48_loc_area_id lai;
Neels Hofmeyr8b8cd932018-03-23 01:47:37 +0100614 gsm48_generate_lai2(&lai, &id->lai);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100615 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
616 msgb_put_u16(msg, id->cell_identity);
617 }
618 break;
619 case CELL_IDENT_LAC_AND_CI:
620 for (i = 0; i < cil->id_list_len; i++) {
621 const struct osmo_lac_and_ci_id *id = &cil->id_list[i].lac_and_ci;
622 msgb_put_u16(msg, id->lac);
623 msgb_put_u16(msg, id->ci);
624 }
625 break;
626 case CELL_IDENT_CI:
627 for (i = 0; i < cil->id_list_len; i++)
628 msgb_put_u16(msg, cil->id_list[i].ci);
629 break;
630 case CELL_IDENT_LAI_AND_LAC:
631 for (i = 0; i < cil->id_list_len; i++) {
632 const struct osmo_location_area_id *id = &cil->id_list[i].lai_and_lac;
633 struct gsm48_loc_area_id lai;
Neels Hofmeyr8b8cd932018-03-23 01:47:37 +0100634 gsm48_generate_lai2(&lai, id);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100635 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
636 }
637 break;
638 case CELL_IDENT_LAC:
639 for (i = 0; i < cil->id_list_len; i++)
640 msgb_put_u16(msg, cil->id_list[i].lac);
641 break;
642 case CELL_IDENT_BSS:
643 case CELL_IDENT_NO_CELL:
644 /* Does not have any list items */
645 break;
646 default:
647 /* Support for other identifier list types is not implemented. */
648 OSMO_ASSERT(false);
649 }
650
651 *tlv_len = (uint8_t) (msg->tail - old_tail);
652 return *tlv_len + 2;
653}
654
655/*! DEPRECATED: Use gsm0808_enc_cell_id_list2 instead.
656 *
657 * Encode TS 08.08 Cell Identifier List IE
658 * \param[out] msg Message Buffer to which IE is to be appended
659 * \param[in] cil Cell ID List to be encoded
660 * \returns number of bytes appended to \a msg */
Philipp Maier783047e2017-03-29 11:35:50 +0200661uint8_t gsm0808_enc_cell_id_list(struct msgb *msg,
662 const struct gsm0808_cell_id_list *cil)
663{
664 uint8_t *old_tail;
665 uint8_t *tlv_len;
666 unsigned int i;
667
668 OSMO_ASSERT(msg);
669 OSMO_ASSERT(cil);
670
671 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
672 tlv_len = msgb_put(msg, 1);
673 old_tail = msg->tail;
674
675 msgb_put_u8(msg, cil->id_discr & 0x0f);
676
677 switch (cil->id_discr) {
678 case CELL_IDENT_LAC:
679 OSMO_ASSERT(cil->id_list_len <= CELL_ID_LIST_LAC_MAXLEN)
680 for (i=0;i<cil->id_list_len;i++) {
681 msgb_put_u16(msg, cil->id_list_lac[i]);
682 }
683 break;
684 case CELL_IDENT_BSS:
685 /* Does not have any list items */
686 break;
687 default:
688 /* FIXME: Implement support for all identifier list elements */
689 OSMO_ASSERT(false);
690 }
691
692 *tlv_len = (uint8_t) (msg->tail - old_tail);
693 return *tlv_len + 2;
694}
695
Stefan Sperling23381452018-03-15 19:38:15 +0100696/* Decode 5-byte LAI list element data (see TS 08.08 3.2.2.27) into MCC/MNC/LAC. */
697static void decode_lai(const uint8_t *data, struct osmo_location_area_id *decoded)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100698{
699 struct gsm48_loc_area_id lai;
700
Stefan Sperling23381452018-03-15 19:38:15 +0100701 /* Copy data to stack to prevent unaligned access in gsm48_decode_lai2(). */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100702 memcpy(&lai, data, sizeof(lai)); /* don't byte swap yet */
703
Stefan Sperling23381452018-03-15 19:38:15 +0100704 gsm48_decode_lai2(&lai, decoded);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100705}
706
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100707static 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 +0100708 size_t *consumed)
709{
710 struct osmo_cell_global_id *id;
711 uint16_t *ci_be;
712 size_t lai_offset;
713 int i = 0;
714 const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*ci_be);
715
716 *consumed = 0;
717 while (remain >= elemlen) {
718 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
719 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100720 id = &cil->id_list[i].global;
Stefan Sperling2873bf12018-03-14 18:38:41 +0100721 lai_offset = i * elemlen;
Stefan Sperling23381452018-03-15 19:38:15 +0100722 decode_lai(&data[lai_offset], &id->lai);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100723 ci_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]);
724 id->cell_identity = osmo_load16be(ci_be);
725 *consumed += elemlen;
726 remain -= elemlen;
727 i++;
728 }
729
730 return i;
731}
732
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100733static 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 +0100734 size_t *consumed)
735{
736 uint16_t *lacp_be, *ci_be;
737 struct osmo_lac_and_ci_id *id;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100738 int i = 0, j = 0;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100739 const size_t elemlen = sizeof(*lacp_be) + sizeof(*ci_be);
740
741 *consumed = 0;
742
743 if (remain < elemlen)
744 return -EINVAL;
745
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100746 lacp_be = (uint16_t *)(&data[j]);
747 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100748 while (remain >= elemlen) {
749 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
750 return -ENOSPC;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100751 id = &cil->id_list[i++].lac_and_ci;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100752 id->lac = osmo_load16be(lacp_be);
753 id->ci = osmo_load16be(ci_be);
754 *consumed += elemlen;
755 remain -= elemlen;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100756 j += elemlen;
757 lacp_be = (uint16_t *)(&data[j]);
758 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100759 }
760
761 return i;
762}
763
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100764static int parse_cell_id_ci_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain,
765 size_t *consumed)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100766{
767 const uint16_t *ci_be = (const uint16_t *)data;
768 int i = 0;
769 const size_t elemlen = sizeof(*ci_be);
770
771 *consumed = 0;
772 while (remain >= elemlen) {
773 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
774 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100775 cil->id_list[i++].ci = osmo_load16be(ci_be++);
Stefan Sperling9c62fc62018-03-16 10:23:34 +0100776 *consumed += elemlen;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100777 remain -= elemlen;
778 }
779 return i;
780}
781
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100782static 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 +0100783 size_t *consumed)
784{
785 struct osmo_location_area_id *id;
786 int i = 0;
787 const size_t elemlen = sizeof(struct gsm48_loc_area_id);
788
789 *consumed = 0;
790 while (remain >= elemlen) {
791 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
792 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100793 id = &cil->id_list[i].lai_and_lac;
Stefan Sperling23381452018-03-15 19:38:15 +0100794 decode_lai(&data[i * elemlen], id);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100795 *consumed += elemlen;
796 remain -= elemlen;
797 i++;
798 }
799
800 return i;
801}
802
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100803static 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 +0100804{
805 const uint16_t *lac_be = (const uint16_t *)data;
806 int i = 0;
807 const size_t elemlen = sizeof(*lac_be);
808
809 *consumed = 0;
810 while (remain >= elemlen) {
811 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
812 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100813 cil->id_list[i++].lac = osmo_load16be(lac_be++);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100814 *consumed += elemlen;
815 remain -= elemlen;
816 }
817 return i;
818}
819
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200820/*! Decode Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +0200821 * \param[out] cil Caller-provided memory to store Cell ID list
822 * \param[in] elem IE value to be decoded
823 * \param[in] len Length of \a elem in bytes
824 * \returns number of bytes parsed; negative on error */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100825int gsm0808_dec_cell_id_list2(struct gsm0808_cell_id_list2 *cil,
826 const uint8_t *elem, uint8_t len)
827{
828 uint8_t id_discr;
829 size_t bytes_elem = 0;
830 int list_len = 0;
831
832 OSMO_ASSERT(cil);
833 if (!elem)
834 return -EINVAL;
835 if (len == 0)
836 return -EINVAL;
837
838 memset(cil, 0, sizeof(*cil));
839
840 id_discr = *elem & 0x0f;
841 elem++;
842 len--;
843
844 switch (id_discr) {
845 case CELL_IDENT_WHOLE_GLOBAL:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100846 list_len = parse_cell_id_global_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100847 break;
848 case CELL_IDENT_LAC_AND_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100849 list_len = parse_cell_id_lac_and_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100850 break;
851 case CELL_IDENT_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100852 list_len = parse_cell_id_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100853 break;
854 case CELL_IDENT_LAI_AND_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100855 list_len = parse_cell_id_lai_and_lac(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100856 break;
857 case CELL_IDENT_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100858 list_len = parse_cell_id_lac_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100859 break;
860 case CELL_IDENT_BSS:
861 case CELL_IDENT_NO_CELL:
862 /* Does not have any list items */
863 break;
864 default:
865 /* Remaining cell identification types are not implemented. */
866 return -EINVAL;
867 }
868
869 if (list_len < 0) /* parsing error */
870 return list_len;
871
872 cil->id_discr = id_discr;
873 cil->id_list_len = list_len;
874
875 /* One byte for the cell ID discriminator + any remaining bytes in
876 * the IE which were consumed by the parser functions above. */
877 return 1 + (int)bytes_elem;
878}
879
880/*! DEPRECATED: Use gsm0808_dec_cell_id_list2 instead.
881 *
882 * Decode Cell Identifier List IE
883 * \param[out] cil Caller-provided memory to store Cell ID list
884 * \param[in] elem IE value to be decoded
885 * \param[in] len Length of \a elem in bytes
886 * \returns number of bytes parsed; negative on error */
Philipp Maier783047e2017-03-29 11:35:50 +0200887int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil,
888 const uint8_t *elem, uint8_t len)
889{
890 uint8_t id_discr;
891 const uint8_t *old_elem = elem;
892 unsigned int item_count = 0;
893
894 OSMO_ASSERT(cil);
895 if (!elem)
896 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200897 if (len == 0)
Philipp Maier783047e2017-03-29 11:35:50 +0200898 return -EINVAL;
899
900 memset(cil, 0, sizeof(*cil));
901
902 id_discr = *elem & 0x0f;
903 elem++;
904 len--;
905
906 cil->id_discr = id_discr;
907
908 switch (id_discr) {
909 case CELL_IDENT_LAC:
910 while (len >= 2) {
911 cil->id_list_lac[item_count] = osmo_load16be(elem);
912 elem += 2;
913 item_count++;
914 len -= 2;
915 }
916 case CELL_IDENT_BSS:
917 /* Does not have any list items */
918 break;
919 default:
920 /* FIXME: Implement support for all identifier list elements */
921 return -EINVAL;
922 }
923
924 cil->id_list_len = item_count;
925 return (int)(elem - old_elem);
926}
Harald Welte96e2a002017-06-12 21:44:18 +0200927
Neels Hofmeyr74663d92018-03-23 01:46:42 +0100928static bool same_cell_id_list_entries(const struct gsm0808_cell_id_list2 *a, int ai,
929 const struct gsm0808_cell_id_list2 *b, int bi)
930{
931 struct gsm0808_cell_id_list2 tmp = {
932 .id_discr = a->id_discr,
933 .id_list_len = 1,
934 };
935 uint8_t buf_a[32 + sizeof(struct msgb)];
936 uint8_t buf_b[32 + sizeof(struct msgb)];
937 struct msgb *msg_a = (void*)buf_a;
938 struct msgb *msg_b = (void*)buf_b;
939
940 msg_a->data_len = 32;
941 msg_b->data_len = 32;
942 msgb_reset(msg_a);
943 msgb_reset(msg_b);
944
945 if (a->id_discr != b->id_discr)
946 return false;
947 if (ai >= a->id_list_len
948 || bi >= b->id_list_len)
949 return false;
950
951 tmp.id_list[0] = a->id_list[ai];
952 gsm0808_enc_cell_id_list2(msg_a, &tmp);
953
954 tmp.id_list[0] = b->id_list[bi];
955 gsm0808_enc_cell_id_list2(msg_b, &tmp);
956
957 if (msg_a->len != msg_b->len)
958 return false;
959 if (memcmp(msg_a->data, msg_b->data, msg_a->len))
960 return false;
961
962 return true;
963}
964
965/*! Append entries from one Cell Identifier List to another.
966 * The cell identifier types must be identical between the two lists.
967 * \param dst[out] Append entries to this list.
968 * \param src[in] Append these entries to \a dst.
969 * \returns the nr of items added, or negative on error: -EINVAL if the id_discr mismatch
970 * between the lists, -ENOSPC if the destination list does not have enough space. If an error is
971 * returned, \a dst may have already been changed (particularly on -ENOSPC). Note that a return value
972 * of zero may occur when the src->id_list_len is zero, or when all entries from \a src already exist
973 * in \a dst, and does not indicate error per se. */
974int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src)
975{
976 int i, j;
977 int added = 0;
978
979 if (dst->id_list_len == 0
980 && dst->id_discr != CELL_IDENT_BSS)
981 dst->id_discr = src->id_discr;
982 else if (dst->id_discr != src->id_discr)
983 return -EINVAL;
984
985 for (i = 0; i < src->id_list_len; i++) {
986 /* don't add duplicate entries */
987 bool skip = false;
988 for (j = 0; j < dst->id_list_len; j++) {
989 if (same_cell_id_list_entries(dst, j, src, i)) {
990 skip = true;
991 break;
992 }
993 }
994 if (skip)
995 continue;
996
997 if (dst->id_list_len >= ARRAY_SIZE(dst->id_list))
998 return -ENOSPC;
999
1000 dst->id_list[dst->id_list_len++] = src->id_list[i];
1001 added ++;
1002 }
1003
1004 return added;
1005}
1006
Neels Hofmeyr38e58412018-05-25 16:56:35 +02001007/*! Convert a single Cell Identifier to a Cell Identifier List with one entry.
1008 * \param dst[out] Overwrite this list.
1009 * \param src[in] Set \a dst to contain exactly this item.
1010 */
1011void gsm0808_cell_id_to_list(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id *src)
1012{
1013 if (!dst)
1014 return;
1015 if (!src) {
1016 *dst = (struct gsm0808_cell_id_list2){
1017 .id_discr = CELL_IDENT_NO_CELL,
1018 };
1019 return;
1020 }
1021
1022 *dst = (struct gsm0808_cell_id_list2){
1023 .id_discr = src->id_discr,
1024 .id_list = { src->id },
1025 .id_list_len = 1,
1026 };
1027
1028 switch (src->id_discr) {
1029 case CELL_IDENT_NO_CELL:
1030 case CELL_IDENT_BSS:
1031 dst->id_list_len = 0;
1032 break;
1033 default:
1034 break;
1035 }
1036}
1037
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001038/*! Encode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1039 * \param[out] msg Message Buffer to which IE is to be appended
1040 * \param[in] ci Cell ID to be encoded
1041 * \returns number of bytes appended to \a msg */
1042uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci)
1043{
1044 uint8_t rc;
1045 uint8_t *ie_tag;
1046 struct gsm0808_cell_id_list2 cil = {
1047 .id_discr = ci->id_discr,
1048 .id_list = { ci->id },
1049 .id_list_len = 1,
1050 };
1051
1052 OSMO_ASSERT(msg);
1053 OSMO_ASSERT(ci);
1054
1055 ie_tag = msg->tail;
1056 rc = gsm0808_enc_cell_id_list2(msg, &cil);
1057
1058 if (rc <= 0)
1059 return rc;
1060
1061 *ie_tag = GSM0808_IE_CELL_IDENTIFIER;
1062 return rc;
1063}
1064
1065/*! Decode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1066 * \param[out] ci Caller-provided memory to store Cell ID.
1067 * \param[in] elem IE value to be decoded.
1068 * \param[in] len Length of \a elem in bytes.
1069 * \returns number of bytes parsed; negative on error */
1070int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len)
1071{
1072 struct gsm0808_cell_id_list2 cil;
1073 int rc;
1074 rc = gsm0808_dec_cell_id_list2(&cil, elem, len);
1075 if (rc < 0)
1076 return rc;
1077 if (cil.id_discr == CELL_IDENT_BSS || cil.id_discr == CELL_IDENT_NO_CELL) {
1078 if (cil.id_list_len != 0)
1079 return -EINVAL;
1080 } else {
1081 if (cil.id_list_len != 1)
1082 return -EINVAL;
1083 }
1084 ci->id_discr = cil.id_discr;
1085 ci->id = cil.id_list[0];
1086 return rc;
1087}
1088
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001089/*! Convert the representation of the permitted speech codec identifier
Philipp Maier3149b0d2017-06-02 13:22:34 +02001090 * that is used in struct gsm0808_channel_type to the speech codec
1091 * representation we use in struct gsm0808_speech_codec.
1092 * \param[in] perm_spch to be converted (see also gsm0808_permitted_speech)
1093 * \returns GSM speech codec type; negative on error */
1094int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch)
1095{
1096 /*! The speech codec type, which is used in the channel type field to
1097 * signal the permitted speech versions (codecs) has a different
1098 * encoding than the type field in the speech codec type element
1099 * (See also 3GPP TS 48.008, 3.2.2.11 and 3.2.2.103) */
1100
1101 switch (perm_spch) {
1102 case GSM0808_PERM_FR1:
1103 return GSM0808_SCT_FR1;
1104 case GSM0808_PERM_FR2:
1105 return GSM0808_SCT_FR2;
1106 case GSM0808_PERM_FR3:
1107 return GSM0808_SCT_FR3;
1108 case GSM0808_PERM_FR4:
1109 return GSM0808_SCT_FR4;
1110 case GSM0808_PERM_FR5:
1111 return GSM0808_SCT_FR5;
1112 case GSM0808_PERM_HR1:
1113 return GSM0808_SCT_HR1;
1114 case GSM0808_PERM_HR3:
1115 return GSM0808_SCT_HR3;
1116 case GSM0808_PERM_HR4:
1117 return GSM0808_SCT_HR4;
1118 case GSM0808_PERM_HR6:
1119 return GSM0808_SCT_HR6;
1120 }
1121
1122 /* Invalid input */
1123 return -EINVAL;
1124}
1125
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001126/*! Extrapolate a speech codec field from a given permitted speech
Philipp Maier884ba0f2017-06-02 13:49:16 +02001127 * parameter (channel type).
1128 * \param[out] sc Caller provided memory to store the resulting speech codec
1129 * \param[in] perm_spch value that is used to derive the speech codec info
1130 * (see also: enum gsm0808_speech_codec_type in gsm0808_utils.h)
1131 * \returns zero when successful; negative on error */
1132int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc,
1133 uint8_t perm_spch)
1134{
1135 int rc;
1136
1137 memset(sc, 0, sizeof(*sc));
1138
1139 /* Determine codec type */
1140 rc = gsm0808_chan_type_to_speech_codec(perm_spch);
1141 if (rc < 0)
1142 return -EINVAL;
1143 sc->type = (uint8_t) rc;
1144
1145 /* Depending on the speech codec type, pick a default codec
1146 * configuration that exactly matches the configuration on the
1147 * air interface. */
1148 switch (sc->type) {
1149 case GSM0808_SCT_FR3:
1150 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR;
1151 break;
1152 case GSM0808_SCT_FR4:
1153 sc->cfg = GSM0808_SC_CFG_DEFAULT_OFR_AMR_WB;
1154 break;
1155 case GSM0808_SCT_FR5:
1156 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR_WB;
1157 break;
1158 case GSM0808_SCT_HR3:
1159 sc->cfg = GSM0808_SC_CFG_DEFAULT_HR_AMR;
1160 break;
1161 case GSM0808_SCT_HR4:
1162 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR_WB;
1163 break;
1164 case GSM0808_SCT_HR6:
1165 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR;
1166 break;
1167 default:
1168 /* Note: Not all codec types specify a default setting,
1169 * in this case, we just set the field to zero. */
1170 sc->cfg = 0;
1171 }
1172
1173 /* Tag all codecs as "Full IP"
1174 * (see als 3GPP TS 48.008 3.2.2.103) */
1175 sc->fi = true;
1176
1177 return 0;
1178}
1179
Philipp Maier5f2eb152018-09-19 13:40:21 +02001180/*! Determine a set of AMR speech codec configuration bits (S0-S15) from a
1181 * given GSM 04.08 AMR configuration struct.
1182 * \param[in] cfg AMR configuration in GSM 04.08 format.
1183 * \param[in] hint if the resulting configuration shall be used with a FR or HR TCH.
1184 * \returns configuration bits (S0-S15) */
Philipp Maier369015c2018-09-21 09:07:20 +02001185uint16_t gsm0808_sc_cfg_from_gsm48_mr_cfg(const struct gsm48_multi_rate_conf *cfg,
Philipp Maier5f2eb152018-09-19 13:40:21 +02001186 bool fr)
1187{
1188 uint16_t s15_s0 = 0;
1189
1190 /* Check each rate bit in the AMR multirate configuration and pick the
1191 * matching default configuration as specified in 3GPP TS 28.062,
1192 * Table 7.11.3.1.3-2. */
1193 if (cfg->m4_75)
1194 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_4_75;
1195 if (cfg->m5_15)
1196 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_15;
1197 if (cfg->m5_90)
1198 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_90;
1199 if (cfg->m6_70)
1200 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_6_70;
1201 if (cfg->m7_40)
1202 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_40;
1203 if (cfg->m7_95)
1204 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_95;
1205 if (cfg->m10_2)
1206 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_10_2;
1207 if (cfg->m12_2)
1208 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_12_2;
1209
1210 /* Note: 3GPP TS 48.008, chapter 3GPP TS 48.008 states that for AMR
1211 * some of the configuration bits must be coded as zeros. The applied
1212 * bitmask matches the default codec settings. See also the definition
1213 * of enum gsm0808_speech_codec_defaults in gsm_08_08.h and
1214 * 3GPP TS 28.062, Table 7.11.3.1.3-2. */
1215 if (fr)
1216 s15_s0 &= GSM0808_SC_CFG_DEFAULT_FR_AMR;
1217 else
1218 s15_s0 &= GSM0808_SC_CFG_DEFAULT_HR_AMR;
1219
1220 return s15_s0;
1221}
1222
Philipp Maier8515d032018-09-25 15:57:49 +02001223/*! Determine a GSM 04.08 AMR configuration struct from a set of speech codec
1224 * configuration bits (S0-S15)
1225 * \param[out] cfg AMR configuration in GSM 04.08 format.
1226 * \param[in] s15_s0 configuration bits (S0-S15). */
1227void gsm48_mr_cfg_from_gsm0808_sc_cfg(struct gsm48_multi_rate_conf *cfg,
1228 uint16_t s15_s0)
1229{
1230 memset(cfg, 0, sizeof(*cfg));
1231
1232 /* Strip option bits */
1233 s15_s0 &= 0x00ff;
1234
1235 /* Rate 5,15k must always be present */
1236 cfg->m5_15 = 1;
1237
1238 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_4_75 & 0xff) ==
1239 (GSM0808_SC_CFG_DEFAULT_AMR_4_75 & 0xff))
1240 cfg->m4_75 = 1;
1241 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_5_90 & 0xff) ==
1242 (GSM0808_SC_CFG_DEFAULT_AMR_5_90 & 0xff))
1243 cfg->m5_90 = 1;
1244 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_6_70 & 0xff) ==
1245 (GSM0808_SC_CFG_DEFAULT_AMR_6_70 & 0xff))
1246 cfg->m6_70 = 1;
1247 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_7_40 & 0xff) ==
1248 (GSM0808_SC_CFG_DEFAULT_AMR_7_40 & 0xff))
1249 cfg->m7_40 = 1;
1250 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_7_95 & 0xff) ==
1251 (GSM0808_SC_CFG_DEFAULT_AMR_7_95 & 0xff))
1252 cfg->m7_95 = 1;
1253 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_10_2 & 0xff) ==
1254 (GSM0808_SC_CFG_DEFAULT_AMR_10_2 & 0xff))
1255 cfg->m10_2 = 1;
1256 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_12_2 & 0xff) ==
1257 (GSM0808_SC_CFG_DEFAULT_AMR_12_2 & 0xff))
1258 cfg->m12_2 = 1;
1259
1260 cfg->ver = 1;
1261 cfg->icmi = 1;
1262}
1263
Maxed651d22018-11-07 15:25:05 +01001264int gsm0808_get_cipher_reject_cause(const struct tlv_parsed *tp)
1265{
1266 const uint8_t *buf = TLVP_VAL_MINLEN(tp, GSM0808_IE_CAUSE, 1);
1267
1268 if (!buf)
1269 return -EBADMSG;
1270
1271 if (TLVP_LEN(tp, GSM0808_IE_CAUSE) > 1) {
1272 if (!gsm0808_cause_ext(buf[0]))
1273 return -EINVAL;
1274 return buf[1];
1275 }
1276
1277 return buf[0];
1278}
1279
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001280/*! Print a human readable name of the cell identifier to the char buffer.
1281 * This is useful both for struct gsm0808_cell_id and struct gsm0808_cell_id_list2.
1282 * See also gsm0808_cell_id_name() and gsm0808_cell_id_list_name().
1283 * \param[out] buf Destination buffer to write string representation to.
1284 * \param[in] buflen Amount of memory available in \a buf.
1285 * \param[in] id_discr Cell Identifier type.
1286 * \param[in] u Cell Identifer value.
1287 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1288 * or that would have been written if the buffer were large enough.
1289 */
1290int gsm0808_cell_id_u_name(char *buf, size_t buflen,
1291 enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
1292{
1293 switch (id_discr) {
1294 case CELL_IDENT_LAC:
1295 return snprintf(buf, buflen, "%u", u->lac);
1296 case CELL_IDENT_CI:
1297 return snprintf(buf, buflen, "%u", u->ci);
1298 case CELL_IDENT_LAC_AND_CI:
1299 return snprintf(buf, buflen, "%u-%u", u->lac_and_ci.lac, u->lac_and_ci.ci);
1300 case CELL_IDENT_LAI_AND_LAC:
1301 return snprintf(buf, buflen, "%s", osmo_lai_name(&u->lai_and_lac));
1302 case CELL_IDENT_WHOLE_GLOBAL:
1303 return snprintf(buf, buflen, "%s", osmo_cgi_name(&u->global));
1304 default:
1305 /* For CELL_IDENT_BSS and CELL_IDENT_NO_CELL, just print the discriminator.
1306 * Same for kinds we have no string representation of yet. */
1307 return snprintf(buf, buflen, "%s", gsm0808_cell_id_discr_name(id_discr));
1308 }
1309}
1310
1311/*! value_string[] for enum CELL_IDENT. */
1312const struct value_string gsm0808_cell_id_discr_names[] = {
1313 { CELL_IDENT_WHOLE_GLOBAL, "CGI" },
1314 { CELL_IDENT_LAC_AND_CI, "LAC-CI" },
1315 { CELL_IDENT_CI, "CI" },
1316 { CELL_IDENT_NO_CELL, "NO-CELL" },
1317 { CELL_IDENT_LAI_AND_LAC, "LAI" },
1318 { CELL_IDENT_LAC, "LAC" },
1319 { CELL_IDENT_BSS, "BSS" },
1320 { CELL_IDENT_UTRAN_PLMN_LAC_RNC, "UTRAN-PLMN-LAC-RNC" },
1321 { CELL_IDENT_UTRAN_RNC, "UTRAN-RNC" },
1322 { CELL_IDENT_UTRAN_LAC_RNC, "UTRAN-LAC-RNC" },
1323 { 0, NULL }
1324};
1325
1326#define APPEND_THING(func, args...) do { \
1327 int remain = buflen - (pos - buf); \
1328 int l = func(pos, remain, ##args); \
1329 if (l < 0 || l > remain) \
1330 pos = buf + buflen; \
1331 else \
1332 pos += l; \
1333 if (l > 0) \
1334 total_len += l; \
1335 } while(0)
1336#define APPEND_STR(fmt, args...) APPEND_THING(snprintf, fmt, ##args)
1337#define APPEND_CELL_ID_U(DISCR, U) APPEND_THING(gsm0808_cell_id_u_name, DISCR, U)
1338
1339static const char *gsm0808_cell_id_name_buf(const struct gsm0808_cell_id *cid,
1340 char *buf, size_t buflen)
1341{
1342 char *pos = buf;
1343 int total_len = 0;
1344 APPEND_STR("%s:", gsm0808_cell_id_discr_name(cid->id_discr));
1345 APPEND_CELL_ID_U(cid->id_discr, &cid->id);
1346 return buf;
1347}
1348
1349/*! Return a human readable representation of a Cell Identifier, like "LAC:123"
1350 * or "CGI:001-01-42-23".
1351 * \param[in] cid Cell Identifer.
1352 * \returns String in a static buffer.
1353 */
1354const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid)
1355{
1356 static char buf[64];
1357 return gsm0808_cell_id_name_buf(cid, buf, sizeof(buf));
1358}
1359
1360/*! Like gsm0808_cell_id_name() but uses a different static buffer.
1361 * \param[in] cid Cell Identifer.
1362 * \returns String in a static buffer.
1363 */
1364const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid)
1365{
1366 static char buf[64];
1367 return gsm0808_cell_id_name_buf(cid, buf, sizeof(buf));
1368}
1369
1370/*! Return a human readable representation of the Cell Identifier List, like
1371 * "LAC[2]:{123, 456}".
1372 * The return value semantics are like snprintf() and thus allow ensuring a complete
1373 * untruncated string by determining the required string length from the return value.
1374 * If buflen > 0, always nul-terminate the string in buf, also when it is truncated.
1375 * If buflen == 0, do not modify buf, just return the would-be length.
1376 * \param[out] buf Destination buffer to write string representation to.
1377 * \param[in] buflen Amount of memory available in \a buf.
1378 * \param[in] cil Cell Identifer List.
1379 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1380 * or that would have been written if the buffer were large enough.
1381 */
1382int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil)
1383{
1384 char *pos = buf;
1385 int total_len = 0;
1386 int i;
1387
1388 APPEND_STR("%s[%u]", gsm0808_cell_id_discr_name(cil->id_discr), cil->id_list_len);
1389
1390 switch (cil->id_discr) {
1391 case CELL_IDENT_BSS:
1392 case CELL_IDENT_NO_CELL:
1393 return total_len;
1394 default:
1395 break;
1396 }
1397
1398 APPEND_STR(":{");
1399
1400 for (i = 0; i < cil->id_list_len; i++) {
1401 if (i)
1402 APPEND_STR(", ");
1403 APPEND_CELL_ID_U(cil->id_discr, &cil->id_list[i]);
1404 }
1405
1406 APPEND_STR("}");
1407 return total_len;
1408}
1409
1410/*! Return a human-readable representation of \a cil in a static buffer.
1411 * If the list is too long, the output may be truncated.
1412 * See also gsm0808_cell_id_list_name_buf(). */
1413const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil)
1414{
1415 static char buf[1024];
1416 gsm0808_cell_id_list_name_buf(buf, sizeof(buf), cil);
1417 return buf;
1418}
1419
1420#undef APPEND_STR
1421#undef APPEND_CELL_ID_U
1422
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02001423const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct)
1424{
1425 static char buf[128];
1426 snprintf(buf, sizeof(buf), "ch_indctr=0x%x ch_rate_type=0x%x perm_spch=%s",
1427 ct->ch_indctr, ct->ch_rate_type,
1428 osmo_hexdump(ct->perm_spch, ct->perm_spch_len));
1429 return buf;
1430}
1431
Harald Welte96e2a002017-06-12 21:44:18 +02001432/*! @} */