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