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