blob: a04addeeff223b9d13ea73372f3f8e0527817890 [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 */
515uint8_t gsm0808_enc_gcr(struct msgb *msg, const struct osmo_gcr_parsed *g)
516{
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
529 * \param[in] elem IE value to be decoded
530 * \param[in] len Length of \a elem in bytes
531 * \returns number of bytes parsed; negative on error */
532int gsm0808_dec_gcr(struct osmo_gcr_parsed *gcr, const struct tlv_parsed *tp)
533{
534 int ret;
535 const uint8_t *buf = TLVP_VAL_MINLEN(tp, GSM0808_IE_GLOBAL_CALL_REF, OSMO_GCR_MIN_LEN);
536 if (!buf)
537 return -EINVAL;
538
539 ret = osmo_dec_gcr(gcr, buf, TLVP_LEN(tp, GSM0808_IE_GLOBAL_CALL_REF));
540 if (ret < 0)
541 return -ENOENT;
542
543 return 2 + ret;
544}
545
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200546/*! Encode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200547 * \param[out] msg Message Buffer to which IE is to be appended
548 * \param[in] ei Encryption Information to be encoded
549 * \returns number of bytes appended to \a msg */
Philipp Maier14e76b92017-03-28 18:36:52 +0200550uint8_t gsm0808_enc_encrypt_info(struct msgb *msg,
551 const struct gsm0808_encrypt_info *ei)
552{
553 unsigned int i;
554 uint8_t perm_algo = 0;
555 uint8_t *ptr;
556 uint8_t *old_tail;
557 uint8_t *tlv_len;
558
559 OSMO_ASSERT(msg);
560 OSMO_ASSERT(ei);
561 OSMO_ASSERT(ei->key_len <= ARRAY_SIZE(ei->key));
562 OSMO_ASSERT(ei->perm_algo_len <= ENCRY_INFO_PERM_ALGO_MAXLEN);
563
564 msgb_put_u8(msg, GSM0808_IE_ENCRYPTION_INFORMATION);
565 tlv_len = msgb_put(msg, 1);
566 old_tail = msg->tail;
567
568 for (i = 0; i < ei->perm_algo_len; i++) {
569 /* Note: gsm_08_08.h defines the permitted algorithms
570 * as an enum which ranges from 0x01 to 0x08 */
571 OSMO_ASSERT(ei->perm_algo[i] != 0);
572 OSMO_ASSERT(ei->perm_algo[i] <= ENCRY_INFO_PERM_ALGO_MAXLEN);
573 perm_algo |= (1 << (ei->perm_algo[i] - 1));
574 }
575
576 msgb_put_u8(msg, perm_algo);
577 ptr = msgb_put(msg, ei->key_len);
578 memcpy(ptr, ei->key, ei->key_len);
579
580 *tlv_len = (uint8_t) (msg->tail - old_tail);
581 return *tlv_len + 2;
582}
583
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200584/*! Decode TS 08.08 Encryption Information IE
Harald Welte96e2a002017-06-12 21:44:18 +0200585 * \param[out] ei Caller-provided memory to store encryption information
586 * \param[in] elem IE value to be decoded
587 * \param[in] len Length of \a elem in bytes
588 * \returns number of bytes parsed; negative on error */
Philipp Maier14e76b92017-03-28 18:36:52 +0200589int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei,
590 const uint8_t *elem, uint8_t len)
591{
592 uint8_t perm_algo;
593 unsigned int i;
594 unsigned int perm_algo_len = 0;
595 const uint8_t *old_elem = elem;
596
597 OSMO_ASSERT(ei);
598 if (!elem)
599 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200600 if (len == 0)
Philipp Maier14e76b92017-03-28 18:36:52 +0200601 return -EINVAL;
602
603 memset(ei, 0, sizeof(*ei));
604
605 perm_algo = *elem;
606 elem++;
607
608 for (i = 0; i < ENCRY_INFO_PERM_ALGO_MAXLEN; i++) {
609 if (perm_algo & (1 << i)) {
610 ei->perm_algo[perm_algo_len] = i + 1;
611 perm_algo_len++;
612 }
613 }
614 ei->perm_algo_len = perm_algo_len;
615
616 ei->key_len = len - 1;
617 memcpy(ei->key, elem, ei->key_len);
618 elem+=ei->key_len;
619
620 return (int)(elem - old_elem);
621}
Philipp Maier783047e2017-03-29 11:35:50 +0200622
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200623/*! Encode TS 08.08 Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +0200624 * \param[out] msg Message Buffer to which IE is to be appended
625 * \param[in] cil Cell ID List to be encoded
626 * \returns number of bytes appended to \a msg */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100627uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg,
628 const struct gsm0808_cell_id_list2 *cil)
629{
630 uint8_t *old_tail;
631 uint8_t *tlv_len;
632 unsigned int i;
633
634 OSMO_ASSERT(msg);
635 OSMO_ASSERT(cil);
636
637 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
638 tlv_len = msgb_put(msg, 1);
639 old_tail = msg->tail;
640
641 msgb_put_u8(msg, cil->id_discr & 0x0f);
642
643 OSMO_ASSERT(cil->id_list_len <= GSM0808_CELL_ID_LIST2_MAXLEN)
644 switch (cil->id_discr) {
645 case CELL_IDENT_WHOLE_GLOBAL:
646 for (i = 0; i < cil->id_list_len; i++) {
647 const struct osmo_cell_global_id *id = &cil->id_list[i].global;
648 struct gsm48_loc_area_id lai;
Neels Hofmeyr8b8cd932018-03-23 01:47:37 +0100649 gsm48_generate_lai2(&lai, &id->lai);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100650 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
651 msgb_put_u16(msg, id->cell_identity);
652 }
653 break;
654 case CELL_IDENT_LAC_AND_CI:
655 for (i = 0; i < cil->id_list_len; i++) {
656 const struct osmo_lac_and_ci_id *id = &cil->id_list[i].lac_and_ci;
657 msgb_put_u16(msg, id->lac);
658 msgb_put_u16(msg, id->ci);
659 }
660 break;
661 case CELL_IDENT_CI:
662 for (i = 0; i < cil->id_list_len; i++)
663 msgb_put_u16(msg, cil->id_list[i].ci);
664 break;
665 case CELL_IDENT_LAI_AND_LAC:
666 for (i = 0; i < cil->id_list_len; i++) {
667 const struct osmo_location_area_id *id = &cil->id_list[i].lai_and_lac;
668 struct gsm48_loc_area_id lai;
Neels Hofmeyr8b8cd932018-03-23 01:47:37 +0100669 gsm48_generate_lai2(&lai, id);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100670 memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai));
671 }
672 break;
673 case CELL_IDENT_LAC:
674 for (i = 0; i < cil->id_list_len; i++)
675 msgb_put_u16(msg, cil->id_list[i].lac);
676 break;
677 case CELL_IDENT_BSS:
678 case CELL_IDENT_NO_CELL:
679 /* Does not have any list items */
680 break;
681 default:
682 /* Support for other identifier list types is not implemented. */
683 OSMO_ASSERT(false);
684 }
685
686 *tlv_len = (uint8_t) (msg->tail - old_tail);
687 return *tlv_len + 2;
688}
689
690/*! DEPRECATED: Use gsm0808_enc_cell_id_list2 instead.
691 *
692 * Encode TS 08.08 Cell Identifier List IE
693 * \param[out] msg Message Buffer to which IE is to be appended
694 * \param[in] cil Cell ID List to be encoded
695 * \returns number of bytes appended to \a msg */
Philipp Maier783047e2017-03-29 11:35:50 +0200696uint8_t gsm0808_enc_cell_id_list(struct msgb *msg,
697 const struct gsm0808_cell_id_list *cil)
698{
699 uint8_t *old_tail;
700 uint8_t *tlv_len;
701 unsigned int i;
702
703 OSMO_ASSERT(msg);
704 OSMO_ASSERT(cil);
705
706 msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST);
707 tlv_len = msgb_put(msg, 1);
708 old_tail = msg->tail;
709
710 msgb_put_u8(msg, cil->id_discr & 0x0f);
711
712 switch (cil->id_discr) {
713 case CELL_IDENT_LAC:
714 OSMO_ASSERT(cil->id_list_len <= CELL_ID_LIST_LAC_MAXLEN)
715 for (i=0;i<cil->id_list_len;i++) {
716 msgb_put_u16(msg, cil->id_list_lac[i]);
717 }
718 break;
719 case CELL_IDENT_BSS:
720 /* Does not have any list items */
721 break;
722 default:
723 /* FIXME: Implement support for all identifier list elements */
724 OSMO_ASSERT(false);
725 }
726
727 *tlv_len = (uint8_t) (msg->tail - old_tail);
728 return *tlv_len + 2;
729}
730
Stefan Sperling23381452018-03-15 19:38:15 +0100731/* Decode 5-byte LAI list element data (see TS 08.08 3.2.2.27) into MCC/MNC/LAC. */
732static void decode_lai(const uint8_t *data, struct osmo_location_area_id *decoded)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100733{
734 struct gsm48_loc_area_id lai;
735
Stefan Sperling23381452018-03-15 19:38:15 +0100736 /* Copy data to stack to prevent unaligned access in gsm48_decode_lai2(). */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100737 memcpy(&lai, data, sizeof(lai)); /* don't byte swap yet */
738
Stefan Sperling23381452018-03-15 19:38:15 +0100739 gsm48_decode_lai2(&lai, decoded);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100740}
741
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100742static 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 +0100743 size_t *consumed)
744{
745 struct osmo_cell_global_id *id;
746 uint16_t *ci_be;
747 size_t lai_offset;
748 int i = 0;
749 const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*ci_be);
750
751 *consumed = 0;
752 while (remain >= elemlen) {
753 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
754 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100755 id = &cil->id_list[i].global;
Stefan Sperling2873bf12018-03-14 18:38:41 +0100756 lai_offset = i * elemlen;
Stefan Sperling23381452018-03-15 19:38:15 +0100757 decode_lai(&data[lai_offset], &id->lai);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100758 ci_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]);
759 id->cell_identity = osmo_load16be(ci_be);
760 *consumed += elemlen;
761 remain -= elemlen;
762 i++;
763 }
764
765 return i;
766}
767
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100768static 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 +0100769 size_t *consumed)
770{
771 uint16_t *lacp_be, *ci_be;
772 struct osmo_lac_and_ci_id *id;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100773 int i = 0, j = 0;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100774 const size_t elemlen = sizeof(*lacp_be) + sizeof(*ci_be);
775
776 *consumed = 0;
777
778 if (remain < elemlen)
779 return -EINVAL;
780
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100781 lacp_be = (uint16_t *)(&data[j]);
782 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100783 while (remain >= elemlen) {
784 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
785 return -ENOSPC;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100786 id = &cil->id_list[i++].lac_and_ci;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100787 id->lac = osmo_load16be(lacp_be);
788 id->ci = osmo_load16be(ci_be);
789 *consumed += elemlen;
790 remain -= elemlen;
Stefan Sperlinged4327c2018-03-16 11:02:59 +0100791 j += elemlen;
792 lacp_be = (uint16_t *)(&data[j]);
793 ci_be = (uint16_t *)(&data[j + elemlen/2]);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100794 }
795
796 return i;
797}
798
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100799static int parse_cell_id_ci_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain,
800 size_t *consumed)
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100801{
802 const uint16_t *ci_be = (const uint16_t *)data;
803 int i = 0;
804 const size_t elemlen = sizeof(*ci_be);
805
806 *consumed = 0;
807 while (remain >= elemlen) {
808 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
809 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100810 cil->id_list[i++].ci = osmo_load16be(ci_be++);
Stefan Sperling9c62fc62018-03-16 10:23:34 +0100811 *consumed += elemlen;
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100812 remain -= elemlen;
813 }
814 return i;
815}
816
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100817static 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 +0100818 size_t *consumed)
819{
820 struct osmo_location_area_id *id;
821 int i = 0;
822 const size_t elemlen = sizeof(struct gsm48_loc_area_id);
823
824 *consumed = 0;
825 while (remain >= elemlen) {
826 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
827 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100828 id = &cil->id_list[i].lai_and_lac;
Stefan Sperling23381452018-03-15 19:38:15 +0100829 decode_lai(&data[i * elemlen], id);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100830 *consumed += elemlen;
831 remain -= elemlen;
832 i++;
833 }
834
835 return i;
836}
837
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100838static 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 +0100839{
840 const uint16_t *lac_be = (const uint16_t *)data;
841 int i = 0;
842 const size_t elemlen = sizeof(*lac_be);
843
844 *consumed = 0;
845 while (remain >= elemlen) {
846 if (i >= GSM0808_CELL_ID_LIST2_MAXLEN)
847 return -ENOSPC;
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100848 cil->id_list[i++].lac = osmo_load16be(lac_be++);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100849 *consumed += elemlen;
850 remain -= elemlen;
851 }
852 return i;
853}
854
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200855/*! Decode Cell Identifier List IE
Harald Welte96e2a002017-06-12 21:44:18 +0200856 * \param[out] cil Caller-provided memory to store Cell ID list
857 * \param[in] elem IE value to be decoded
858 * \param[in] len Length of \a elem in bytes
859 * \returns number of bytes parsed; negative on error */
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100860int gsm0808_dec_cell_id_list2(struct gsm0808_cell_id_list2 *cil,
861 const uint8_t *elem, uint8_t len)
862{
863 uint8_t id_discr;
864 size_t bytes_elem = 0;
865 int list_len = 0;
866
867 OSMO_ASSERT(cil);
868 if (!elem)
869 return -EINVAL;
870 if (len == 0)
871 return -EINVAL;
872
873 memset(cil, 0, sizeof(*cil));
874
875 id_discr = *elem & 0x0f;
876 elem++;
877 len--;
878
879 switch (id_discr) {
880 case CELL_IDENT_WHOLE_GLOBAL:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100881 list_len = parse_cell_id_global_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100882 break;
883 case CELL_IDENT_LAC_AND_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100884 list_len = parse_cell_id_lac_and_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100885 break;
886 case CELL_IDENT_CI:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100887 list_len = parse_cell_id_ci_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100888 break;
889 case CELL_IDENT_LAI_AND_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100890 list_len = parse_cell_id_lai_and_lac(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100891 break;
892 case CELL_IDENT_LAC:
Stefan Sperlinge1a86742018-03-15 18:05:02 +0100893 list_len = parse_cell_id_lac_list(cil, elem, len, &bytes_elem);
Stefan Sperling11a4d9d2018-02-15 18:28:04 +0100894 break;
895 case CELL_IDENT_BSS:
896 case CELL_IDENT_NO_CELL:
897 /* Does not have any list items */
898 break;
899 default:
900 /* Remaining cell identification types are not implemented. */
901 return -EINVAL;
902 }
903
904 if (list_len < 0) /* parsing error */
905 return list_len;
906
907 cil->id_discr = id_discr;
908 cil->id_list_len = list_len;
909
910 /* One byte for the cell ID discriminator + any remaining bytes in
911 * the IE which were consumed by the parser functions above. */
912 return 1 + (int)bytes_elem;
913}
914
915/*! DEPRECATED: Use gsm0808_dec_cell_id_list2 instead.
916 *
917 * Decode Cell Identifier List IE
918 * \param[out] cil Caller-provided memory to store Cell ID list
919 * \param[in] elem IE value to be decoded
920 * \param[in] len Length of \a elem in bytes
921 * \returns number of bytes parsed; negative on error */
Philipp Maier783047e2017-03-29 11:35:50 +0200922int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil,
923 const uint8_t *elem, uint8_t len)
924{
925 uint8_t id_discr;
926 const uint8_t *old_elem = elem;
927 unsigned int item_count = 0;
928
929 OSMO_ASSERT(cil);
930 if (!elem)
931 return -EINVAL;
Philipp Maier17778bd2017-04-28 11:05:44 +0200932 if (len == 0)
Philipp Maier783047e2017-03-29 11:35:50 +0200933 return -EINVAL;
934
935 memset(cil, 0, sizeof(*cil));
936
937 id_discr = *elem & 0x0f;
938 elem++;
939 len--;
940
941 cil->id_discr = id_discr;
942
943 switch (id_discr) {
944 case CELL_IDENT_LAC:
945 while (len >= 2) {
946 cil->id_list_lac[item_count] = osmo_load16be(elem);
947 elem += 2;
948 item_count++;
949 len -= 2;
950 }
951 case CELL_IDENT_BSS:
952 /* Does not have any list items */
953 break;
954 default:
955 /* FIXME: Implement support for all identifier list elements */
956 return -EINVAL;
957 }
958
959 cil->id_list_len = item_count;
960 return (int)(elem - old_elem);
961}
Harald Welte96e2a002017-06-12 21:44:18 +0200962
Neels Hofmeyr74663d92018-03-23 01:46:42 +0100963static bool same_cell_id_list_entries(const struct gsm0808_cell_id_list2 *a, int ai,
964 const struct gsm0808_cell_id_list2 *b, int bi)
965{
966 struct gsm0808_cell_id_list2 tmp = {
967 .id_discr = a->id_discr,
968 .id_list_len = 1,
969 };
970 uint8_t buf_a[32 + sizeof(struct msgb)];
971 uint8_t buf_b[32 + sizeof(struct msgb)];
972 struct msgb *msg_a = (void*)buf_a;
973 struct msgb *msg_b = (void*)buf_b;
974
975 msg_a->data_len = 32;
976 msg_b->data_len = 32;
977 msgb_reset(msg_a);
978 msgb_reset(msg_b);
979
980 if (a->id_discr != b->id_discr)
981 return false;
982 if (ai >= a->id_list_len
983 || bi >= b->id_list_len)
984 return false;
985
986 tmp.id_list[0] = a->id_list[ai];
987 gsm0808_enc_cell_id_list2(msg_a, &tmp);
988
989 tmp.id_list[0] = b->id_list[bi];
990 gsm0808_enc_cell_id_list2(msg_b, &tmp);
991
992 if (msg_a->len != msg_b->len)
993 return false;
994 if (memcmp(msg_a->data, msg_b->data, msg_a->len))
995 return false;
996
997 return true;
998}
999
1000/*! Append entries from one Cell Identifier List to another.
1001 * The cell identifier types must be identical between the two lists.
1002 * \param dst[out] Append entries to this list.
1003 * \param src[in] Append these entries to \a dst.
1004 * \returns the nr of items added, or negative on error: -EINVAL if the id_discr mismatch
1005 * between the lists, -ENOSPC if the destination list does not have enough space. If an error is
1006 * returned, \a dst may have already been changed (particularly on -ENOSPC). Note that a return value
1007 * of zero may occur when the src->id_list_len is zero, or when all entries from \a src already exist
1008 * in \a dst, and does not indicate error per se. */
1009int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src)
1010{
1011 int i, j;
1012 int added = 0;
1013
1014 if (dst->id_list_len == 0
1015 && dst->id_discr != CELL_IDENT_BSS)
1016 dst->id_discr = src->id_discr;
1017 else if (dst->id_discr != src->id_discr)
1018 return -EINVAL;
1019
1020 for (i = 0; i < src->id_list_len; i++) {
1021 /* don't add duplicate entries */
1022 bool skip = false;
1023 for (j = 0; j < dst->id_list_len; j++) {
1024 if (same_cell_id_list_entries(dst, j, src, i)) {
1025 skip = true;
1026 break;
1027 }
1028 }
1029 if (skip)
1030 continue;
1031
1032 if (dst->id_list_len >= ARRAY_SIZE(dst->id_list))
1033 return -ENOSPC;
1034
1035 dst->id_list[dst->id_list_len++] = src->id_list[i];
1036 added ++;
1037 }
1038
1039 return added;
1040}
1041
Neels Hofmeyr38e58412018-05-25 16:56:35 +02001042/*! Convert a single Cell Identifier to a Cell Identifier List with one entry.
1043 * \param dst[out] Overwrite this list.
1044 * \param src[in] Set \a dst to contain exactly this item.
1045 */
1046void gsm0808_cell_id_to_list(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id *src)
1047{
1048 if (!dst)
1049 return;
1050 if (!src) {
1051 *dst = (struct gsm0808_cell_id_list2){
1052 .id_discr = CELL_IDENT_NO_CELL,
1053 };
1054 return;
1055 }
1056
1057 *dst = (struct gsm0808_cell_id_list2){
1058 .id_discr = src->id_discr,
1059 .id_list = { src->id },
1060 .id_list_len = 1,
1061 };
1062
1063 switch (src->id_discr) {
1064 case CELL_IDENT_NO_CELL:
1065 case CELL_IDENT_BSS:
1066 dst->id_list_len = 0;
1067 break;
1068 default:
1069 break;
1070 }
1071}
1072
Neels Hofmeyr250e7f72018-04-13 03:30:14 +02001073/*! Encode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1074 * \param[out] msg Message Buffer to which IE is to be appended
1075 * \param[in] ci Cell ID to be encoded
1076 * \returns number of bytes appended to \a msg */
1077uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci)
1078{
1079 uint8_t rc;
1080 uint8_t *ie_tag;
1081 struct gsm0808_cell_id_list2 cil = {
1082 .id_discr = ci->id_discr,
1083 .id_list = { ci->id },
1084 .id_list_len = 1,
1085 };
1086
1087 OSMO_ASSERT(msg);
1088 OSMO_ASSERT(ci);
1089
1090 ie_tag = msg->tail;
1091 rc = gsm0808_enc_cell_id_list2(msg, &cil);
1092
1093 if (rc <= 0)
1094 return rc;
1095
1096 *ie_tag = GSM0808_IE_CELL_IDENTIFIER;
1097 return rc;
1098}
1099
1100/*! Decode Cell Identifier IE (3GPP TS 48.008 3.2.2.17).
1101 * \param[out] ci Caller-provided memory to store Cell ID.
1102 * \param[in] elem IE value to be decoded.
1103 * \param[in] len Length of \a elem in bytes.
1104 * \returns number of bytes parsed; negative on error */
1105int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len)
1106{
1107 struct gsm0808_cell_id_list2 cil;
1108 int rc;
1109 rc = gsm0808_dec_cell_id_list2(&cil, elem, len);
1110 if (rc < 0)
1111 return rc;
1112 if (cil.id_discr == CELL_IDENT_BSS || cil.id_discr == CELL_IDENT_NO_CELL) {
1113 if (cil.id_list_len != 0)
1114 return -EINVAL;
1115 } else {
1116 if (cil.id_list_len != 1)
1117 return -EINVAL;
1118 }
1119 ci->id_discr = cil.id_discr;
1120 ci->id = cil.id_list[0];
1121 return rc;
1122}
1123
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001124/*! Convert the representation of the permitted speech codec identifier
Philipp Maier3149b0d2017-06-02 13:22:34 +02001125 * that is used in struct gsm0808_channel_type to the speech codec
1126 * representation we use in struct gsm0808_speech_codec.
1127 * \param[in] perm_spch to be converted (see also gsm0808_permitted_speech)
1128 * \returns GSM speech codec type; negative on error */
1129int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch)
1130{
1131 /*! The speech codec type, which is used in the channel type field to
1132 * signal the permitted speech versions (codecs) has a different
1133 * encoding than the type field in the speech codec type element
1134 * (See also 3GPP TS 48.008, 3.2.2.11 and 3.2.2.103) */
1135
1136 switch (perm_spch) {
1137 case GSM0808_PERM_FR1:
1138 return GSM0808_SCT_FR1;
1139 case GSM0808_PERM_FR2:
1140 return GSM0808_SCT_FR2;
1141 case GSM0808_PERM_FR3:
1142 return GSM0808_SCT_FR3;
1143 case GSM0808_PERM_FR4:
1144 return GSM0808_SCT_FR4;
1145 case GSM0808_PERM_FR5:
1146 return GSM0808_SCT_FR5;
1147 case GSM0808_PERM_HR1:
1148 return GSM0808_SCT_HR1;
1149 case GSM0808_PERM_HR3:
1150 return GSM0808_SCT_HR3;
1151 case GSM0808_PERM_HR4:
1152 return GSM0808_SCT_HR4;
1153 case GSM0808_PERM_HR6:
1154 return GSM0808_SCT_HR6;
1155 }
1156
1157 /* Invalid input */
1158 return -EINVAL;
1159}
1160
Neels Hofmeyr87e45502017-06-20 00:17:59 +02001161/*! Extrapolate a speech codec field from a given permitted speech
Philipp Maier884ba0f2017-06-02 13:49:16 +02001162 * parameter (channel type).
1163 * \param[out] sc Caller provided memory to store the resulting speech codec
1164 * \param[in] perm_spch value that is used to derive the speech codec info
1165 * (see also: enum gsm0808_speech_codec_type in gsm0808_utils.h)
1166 * \returns zero when successful; negative on error */
1167int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc,
1168 uint8_t perm_spch)
1169{
1170 int rc;
1171
1172 memset(sc, 0, sizeof(*sc));
1173
1174 /* Determine codec type */
1175 rc = gsm0808_chan_type_to_speech_codec(perm_spch);
1176 if (rc < 0)
1177 return -EINVAL;
1178 sc->type = (uint8_t) rc;
1179
1180 /* Depending on the speech codec type, pick a default codec
1181 * configuration that exactly matches the configuration on the
1182 * air interface. */
1183 switch (sc->type) {
1184 case GSM0808_SCT_FR3:
1185 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR;
1186 break;
1187 case GSM0808_SCT_FR4:
1188 sc->cfg = GSM0808_SC_CFG_DEFAULT_OFR_AMR_WB;
1189 break;
1190 case GSM0808_SCT_FR5:
1191 sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR_WB;
1192 break;
1193 case GSM0808_SCT_HR3:
1194 sc->cfg = GSM0808_SC_CFG_DEFAULT_HR_AMR;
1195 break;
1196 case GSM0808_SCT_HR4:
1197 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR_WB;
1198 break;
1199 case GSM0808_SCT_HR6:
1200 sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR;
1201 break;
1202 default:
1203 /* Note: Not all codec types specify a default setting,
1204 * in this case, we just set the field to zero. */
1205 sc->cfg = 0;
1206 }
1207
1208 /* Tag all codecs as "Full IP"
1209 * (see als 3GPP TS 48.008 3.2.2.103) */
1210 sc->fi = true;
1211
1212 return 0;
1213}
1214
Philipp Maier5f2eb152018-09-19 13:40:21 +02001215/*! Determine a set of AMR speech codec configuration bits (S0-S15) from a
1216 * given GSM 04.08 AMR configuration struct.
1217 * \param[in] cfg AMR configuration in GSM 04.08 format.
1218 * \param[in] hint if the resulting configuration shall be used with a FR or HR TCH.
1219 * \returns configuration bits (S0-S15) */
Philipp Maier369015c2018-09-21 09:07:20 +02001220uint16_t gsm0808_sc_cfg_from_gsm48_mr_cfg(const struct gsm48_multi_rate_conf *cfg,
Philipp Maier5f2eb152018-09-19 13:40:21 +02001221 bool fr)
1222{
1223 uint16_t s15_s0 = 0;
1224
1225 /* Check each rate bit in the AMR multirate configuration and pick the
1226 * matching default configuration as specified in 3GPP TS 28.062,
1227 * Table 7.11.3.1.3-2. */
1228 if (cfg->m4_75)
1229 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_4_75;
1230 if (cfg->m5_15)
1231 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_15;
1232 if (cfg->m5_90)
1233 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_90;
1234 if (cfg->m6_70)
1235 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_6_70;
1236 if (cfg->m7_40)
1237 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_40;
1238 if (cfg->m7_95)
1239 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_95;
1240 if (cfg->m10_2)
1241 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_10_2;
1242 if (cfg->m12_2)
1243 s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_12_2;
1244
1245 /* Note: 3GPP TS 48.008, chapter 3GPP TS 48.008 states that for AMR
1246 * some of the configuration bits must be coded as zeros. The applied
1247 * bitmask matches the default codec settings. See also the definition
1248 * of enum gsm0808_speech_codec_defaults in gsm_08_08.h and
1249 * 3GPP TS 28.062, Table 7.11.3.1.3-2. */
1250 if (fr)
1251 s15_s0 &= GSM0808_SC_CFG_DEFAULT_FR_AMR;
1252 else
1253 s15_s0 &= GSM0808_SC_CFG_DEFAULT_HR_AMR;
1254
1255 return s15_s0;
1256}
1257
Philipp Maier8515d032018-09-25 15:57:49 +02001258/*! Determine a GSM 04.08 AMR configuration struct from a set of speech codec
1259 * configuration bits (S0-S15)
1260 * \param[out] cfg AMR configuration in GSM 04.08 format.
1261 * \param[in] s15_s0 configuration bits (S0-S15). */
1262void gsm48_mr_cfg_from_gsm0808_sc_cfg(struct gsm48_multi_rate_conf *cfg,
1263 uint16_t s15_s0)
1264{
1265 memset(cfg, 0, sizeof(*cfg));
1266
1267 /* Strip option bits */
1268 s15_s0 &= 0x00ff;
1269
1270 /* Rate 5,15k must always be present */
1271 cfg->m5_15 = 1;
1272
1273 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_4_75 & 0xff) ==
1274 (GSM0808_SC_CFG_DEFAULT_AMR_4_75 & 0xff))
1275 cfg->m4_75 = 1;
1276 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_5_90 & 0xff) ==
1277 (GSM0808_SC_CFG_DEFAULT_AMR_5_90 & 0xff))
1278 cfg->m5_90 = 1;
1279 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_6_70 & 0xff) ==
1280 (GSM0808_SC_CFG_DEFAULT_AMR_6_70 & 0xff))
1281 cfg->m6_70 = 1;
1282 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_7_40 & 0xff) ==
1283 (GSM0808_SC_CFG_DEFAULT_AMR_7_40 & 0xff))
1284 cfg->m7_40 = 1;
1285 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_7_95 & 0xff) ==
1286 (GSM0808_SC_CFG_DEFAULT_AMR_7_95 & 0xff))
1287 cfg->m7_95 = 1;
1288 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_10_2 & 0xff) ==
1289 (GSM0808_SC_CFG_DEFAULT_AMR_10_2 & 0xff))
1290 cfg->m10_2 = 1;
1291 if ((s15_s0 & GSM0808_SC_CFG_DEFAULT_AMR_12_2 & 0xff) ==
1292 (GSM0808_SC_CFG_DEFAULT_AMR_12_2 & 0xff))
1293 cfg->m12_2 = 1;
1294
1295 cfg->ver = 1;
1296 cfg->icmi = 1;
1297}
1298
Maxed651d22018-11-07 15:25:05 +01001299int gsm0808_get_cipher_reject_cause(const struct tlv_parsed *tp)
1300{
1301 const uint8_t *buf = TLVP_VAL_MINLEN(tp, GSM0808_IE_CAUSE, 1);
1302
1303 if (!buf)
1304 return -EBADMSG;
1305
1306 if (TLVP_LEN(tp, GSM0808_IE_CAUSE) > 1) {
1307 if (!gsm0808_cause_ext(buf[0]))
1308 return -EINVAL;
1309 return buf[1];
1310 }
1311
1312 return buf[0];
1313}
1314
Neels Hofmeyra4399c82018-04-17 02:26:10 +02001315/*! Print a human readable name of the cell identifier to the char buffer.
1316 * This is useful both for struct gsm0808_cell_id and struct gsm0808_cell_id_list2.
1317 * See also gsm0808_cell_id_name() and gsm0808_cell_id_list_name().
1318 * \param[out] buf Destination buffer to write string representation to.
1319 * \param[in] buflen Amount of memory available in \a buf.
1320 * \param[in] id_discr Cell Identifier type.
1321 * \param[in] u Cell Identifer value.
1322 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1323 * or that would have been written if the buffer were large enough.
1324 */
1325int gsm0808_cell_id_u_name(char *buf, size_t buflen,
1326 enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u)
1327{
1328 switch (id_discr) {
1329 case CELL_IDENT_LAC:
1330 return snprintf(buf, buflen, "%u", u->lac);
1331 case CELL_IDENT_CI:
1332 return snprintf(buf, buflen, "%u", u->ci);
1333 case CELL_IDENT_LAC_AND_CI:
1334 return snprintf(buf, buflen, "%u-%u", u->lac_and_ci.lac, u->lac_and_ci.ci);
1335 case CELL_IDENT_LAI_AND_LAC:
1336 return snprintf(buf, buflen, "%s", osmo_lai_name(&u->lai_and_lac));
1337 case CELL_IDENT_WHOLE_GLOBAL:
1338 return snprintf(buf, buflen, "%s", osmo_cgi_name(&u->global));
1339 default:
1340 /* For CELL_IDENT_BSS and CELL_IDENT_NO_CELL, just print the discriminator.
1341 * Same for kinds we have no string representation of yet. */
1342 return snprintf(buf, buflen, "%s", gsm0808_cell_id_discr_name(id_discr));
1343 }
1344}
1345
1346/*! value_string[] for enum CELL_IDENT. */
1347const struct value_string gsm0808_cell_id_discr_names[] = {
1348 { CELL_IDENT_WHOLE_GLOBAL, "CGI" },
1349 { CELL_IDENT_LAC_AND_CI, "LAC-CI" },
1350 { CELL_IDENT_CI, "CI" },
1351 { CELL_IDENT_NO_CELL, "NO-CELL" },
1352 { CELL_IDENT_LAI_AND_LAC, "LAI" },
1353 { CELL_IDENT_LAC, "LAC" },
1354 { CELL_IDENT_BSS, "BSS" },
1355 { CELL_IDENT_UTRAN_PLMN_LAC_RNC, "UTRAN-PLMN-LAC-RNC" },
1356 { CELL_IDENT_UTRAN_RNC, "UTRAN-RNC" },
1357 { CELL_IDENT_UTRAN_LAC_RNC, "UTRAN-LAC-RNC" },
1358 { 0, NULL }
1359};
1360
1361#define APPEND_THING(func, args...) do { \
1362 int remain = buflen - (pos - buf); \
1363 int l = func(pos, remain, ##args); \
1364 if (l < 0 || l > remain) \
1365 pos = buf + buflen; \
1366 else \
1367 pos += l; \
1368 if (l > 0) \
1369 total_len += l; \
1370 } while(0)
1371#define APPEND_STR(fmt, args...) APPEND_THING(snprintf, fmt, ##args)
1372#define APPEND_CELL_ID_U(DISCR, U) APPEND_THING(gsm0808_cell_id_u_name, DISCR, U)
1373
1374static const char *gsm0808_cell_id_name_buf(const struct gsm0808_cell_id *cid,
1375 char *buf, size_t buflen)
1376{
1377 char *pos = buf;
1378 int total_len = 0;
1379 APPEND_STR("%s:", gsm0808_cell_id_discr_name(cid->id_discr));
1380 APPEND_CELL_ID_U(cid->id_discr, &cid->id);
1381 return buf;
1382}
1383
1384/*! Return a human readable representation of a Cell Identifier, like "LAC:123"
1385 * or "CGI:001-01-42-23".
1386 * \param[in] cid Cell Identifer.
1387 * \returns String in a static buffer.
1388 */
1389const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid)
1390{
1391 static char buf[64];
1392 return gsm0808_cell_id_name_buf(cid, buf, sizeof(buf));
1393}
1394
1395/*! Like gsm0808_cell_id_name() but uses a different static buffer.
1396 * \param[in] cid Cell Identifer.
1397 * \returns String in a static buffer.
1398 */
1399const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid)
1400{
1401 static char buf[64];
1402 return gsm0808_cell_id_name_buf(cid, buf, sizeof(buf));
1403}
1404
1405/*! Return a human readable representation of the Cell Identifier List, like
1406 * "LAC[2]:{123, 456}".
1407 * The return value semantics are like snprintf() and thus allow ensuring a complete
1408 * untruncated string by determining the required string length from the return value.
1409 * If buflen > 0, always nul-terminate the string in buf, also when it is truncated.
1410 * If buflen == 0, do not modify buf, just return the would-be length.
1411 * \param[out] buf Destination buffer to write string representation to.
1412 * \param[in] buflen Amount of memory available in \a buf.
1413 * \param[in] cil Cell Identifer List.
1414 * \returns Like snprintf(): the amount of characters (excluding terminating nul) written,
1415 * or that would have been written if the buffer were large enough.
1416 */
1417int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil)
1418{
1419 char *pos = buf;
1420 int total_len = 0;
1421 int i;
1422
1423 APPEND_STR("%s[%u]", gsm0808_cell_id_discr_name(cil->id_discr), cil->id_list_len);
1424
1425 switch (cil->id_discr) {
1426 case CELL_IDENT_BSS:
1427 case CELL_IDENT_NO_CELL:
1428 return total_len;
1429 default:
1430 break;
1431 }
1432
1433 APPEND_STR(":{");
1434
1435 for (i = 0; i < cil->id_list_len; i++) {
1436 if (i)
1437 APPEND_STR(", ");
1438 APPEND_CELL_ID_U(cil->id_discr, &cil->id_list[i]);
1439 }
1440
1441 APPEND_STR("}");
1442 return total_len;
1443}
1444
1445/*! Return a human-readable representation of \a cil in a static buffer.
1446 * If the list is too long, the output may be truncated.
1447 * See also gsm0808_cell_id_list_name_buf(). */
1448const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil)
1449{
1450 static char buf[1024];
1451 gsm0808_cell_id_list_name_buf(buf, sizeof(buf), cil);
1452 return buf;
1453}
1454
1455#undef APPEND_STR
1456#undef APPEND_CELL_ID_U
1457
Neels Hofmeyrafacc2b2018-04-16 22:41:51 +02001458const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct)
1459{
1460 static char buf[128];
1461 snprintf(buf, sizeof(buf), "ch_indctr=0x%x ch_rate_type=0x%x perm_spch=%s",
1462 ct->ch_indctr, ct->ch_rate_type,
1463 osmo_hexdump(ct->perm_spch, ct->perm_spch_len));
1464 return buf;
1465}
1466
Harald Welte96e2a002017-06-12 21:44:18 +02001467/*! @} */