Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /* |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 2 | * (C) 2016 by sysmocom - s.f.m.c. GmbH, Author: Philipp Maier |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 3 | * All Rights Reserved |
| 4 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * 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 Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 10 | * (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 Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 15 | * GNU General Public License for more details. |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 16 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 17 | * You should have received a copy of the GNU General Public License |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | */ |
| 21 | |
Harald Welte | 20725b9 | 2017-05-15 12:50:04 +0200 | [diff] [blame] | 22 | #include "config.h" |
| 23 | |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 24 | #include <osmocom/core/utils.h> |
| 25 | #include <osmocom/core/msgb.h> |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 26 | #include <osmocom/core/byteswap.h> |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 27 | #include <string.h> |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 28 | #include <errno.h> |
| 29 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 30 | #include <osmocom/gsm/gsm48.h> |
Max | 5ec0cf5 | 2019-01-15 16:37:09 +0100 | [diff] [blame] | 31 | #include <osmocom/gsm/gsm0808.h> |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 32 | #include <osmocom/gsm/gsm0808_utils.h> |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 33 | |
| 34 | #define IP_V4_ADDR_LEN 4 |
| 35 | #define IP_V6_ADDR_LEN 16 |
| 36 | #define IP_PORT_LEN 2 |
| 37 | |
Philipp Maier | e0c6530 | 2017-03-28 17:05:40 +0200 | [diff] [blame] | 38 | #define CHANNEL_TYPE_ELEMENT_MAXLEN 11 |
| 39 | #define CHANNEL_TYPE_ELEMENT_MINLEN 3 |
Philipp Maier | 14e76b9 | 2017-03-28 18:36:52 +0200 | [diff] [blame] | 40 | #define ENCRYPT_INFO_ELEMENT_MINLEN 1 |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 41 | |
Harald Welte | 20725b9 | 2017-05-15 12:50:04 +0200 | [diff] [blame] | 42 | #ifdef HAVE_SYS_SOCKET_H |
| 43 | |
| 44 | #include <sys/socket.h> |
| 45 | #include <netinet/in.h> |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 46 | |
| 47 | /*! \addtogroup gsm0808 |
| 48 | * @{ |
Harald Welte | 37b6165 | 2017-10-16 18:46:03 +0200 | [diff] [blame] | 49 | * \file gsm0808_utils.c |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 50 | */ |
| 51 | |
Philipp Maier | 4f4905f | 2018-11-30 13:36:12 +0100 | [diff] [blame] | 52 | /*! Encode TS 08.08 AoIP Cause IE |
| 53 | * \param[out] msg Message Buffer to which to append IE |
| 54 | * \param[in] cause Cause code to be used in IE |
| 55 | * \returns number of bytes added to \a msg */ |
| 56 | uint8_t gsm0808_enc_cause(struct msgb *msg, uint16_t cause) |
| 57 | { |
| 58 | /* See also 3GPP TS 48.008 3.2.2.5 Cause */ |
| 59 | uint8_t *old_tail; |
| 60 | bool extended; |
| 61 | |
| 62 | old_tail = msg->tail; |
| 63 | |
| 64 | extended = gsm0808_cause_ext(cause >> 8); |
| 65 | |
| 66 | msgb_put_u8(msg, GSM0808_IE_CAUSE); |
| 67 | if (extended) { |
| 68 | msgb_put_u8(msg, 2); |
| 69 | msgb_put_u16(msg, cause); |
| 70 | } else { |
| 71 | msgb_put_u8(msg, 1); |
| 72 | msgb_put_u8(msg, (uint8_t) (cause & 0xFF)); |
| 73 | } |
| 74 | |
| 75 | return (uint8_t) (msg->tail - old_tail); |
| 76 | } |
| 77 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 78 | /*! Encode TS 08.08 AoIP transport address IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 79 | * \param[out] msg Message Buffer to which to append IE |
| 80 | * \param[in] ss Socket Address to be used in IE |
| 81 | * \returns number of bytes added to \a msg */ |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 82 | uint8_t gsm0808_enc_aoip_trasp_addr(struct msgb *msg, |
| 83 | const struct sockaddr_storage *ss) |
| 84 | { |
| 85 | /* See also 3GPP TS 48.008 3.2.2.102 AoIP Transport Layer Address */ |
| 86 | struct sockaddr_in *sin; |
| 87 | struct sockaddr_in6 *sin6; |
| 88 | uint16_t port = 0; |
| 89 | uint8_t *ptr; |
| 90 | uint8_t *old_tail; |
| 91 | uint8_t *tlv_len; |
| 92 | |
| 93 | OSMO_ASSERT(msg); |
| 94 | OSMO_ASSERT(ss); |
| 95 | OSMO_ASSERT(ss->ss_family == AF_INET || ss->ss_family == AF_INET6); |
| 96 | |
| 97 | msgb_put_u8(msg, GSM0808_IE_AOIP_TRASP_ADDR); |
| 98 | tlv_len = msgb_put(msg,1); |
| 99 | old_tail = msg->tail; |
| 100 | |
| 101 | switch (ss->ss_family) { |
| 102 | case AF_INET: |
| 103 | sin = (struct sockaddr_in *)ss; |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 104 | port = osmo_ntohs(sin->sin_port); |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 105 | ptr = msgb_put(msg, IP_V4_ADDR_LEN); |
| 106 | memcpy(ptr, &sin->sin_addr.s_addr, IP_V4_ADDR_LEN); |
| 107 | break; |
| 108 | case AF_INET6: |
| 109 | sin6 = (struct sockaddr_in6 *)ss; |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 110 | port = osmo_ntohs(sin6->sin6_port); |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 111 | ptr = msgb_put(msg, IP_V6_ADDR_LEN); |
| 112 | memcpy(ptr, sin6->sin6_addr.s6_addr, IP_V6_ADDR_LEN); |
| 113 | break; |
| 114 | } |
| 115 | |
| 116 | msgb_put_u16(msg, port); |
| 117 | |
| 118 | *tlv_len = (uint8_t) (msg->tail - old_tail); |
| 119 | return *tlv_len + 2; |
| 120 | } |
| 121 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 122 | /*! Decode TS 08.08 AoIP transport address IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 123 | * \param[out] ss Caller-provided memory where decoded socket addr is stored |
| 124 | * \param[in] elem pointer to IE value |
| 125 | * \param[in] len length of \a elem in bytes |
| 126 | * \returns number of bytes parsed */ |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 127 | int gsm0808_dec_aoip_trasp_addr(struct sockaddr_storage *ss, |
| 128 | const uint8_t *elem, uint8_t len) |
| 129 | { |
| 130 | /* See also 3GPP TS 48.008 3.2.2.102 AoIP Transport Layer Address */ |
| 131 | struct sockaddr_in sin; |
| 132 | struct sockaddr_in6 sin6; |
| 133 | const uint8_t *old_elem = elem; |
| 134 | |
| 135 | OSMO_ASSERT(ss); |
| 136 | if (!elem) |
| 137 | return -EINVAL; |
Philipp Maier | 17778bd | 2017-04-28 11:05:44 +0200 | [diff] [blame] | 138 | if (len == 0) |
Philipp Maier | 2240143 | 2017-03-24 17:59:26 +0100 | [diff] [blame] | 139 | return -EINVAL; |
| 140 | |
| 141 | memset(ss, 0, sizeof(*ss)); |
| 142 | |
| 143 | switch (len) { |
| 144 | case IP_V4_ADDR_LEN + IP_PORT_LEN: |
| 145 | memset(&sin, 0, sizeof(sin)); |
| 146 | sin.sin_family = AF_INET; |
| 147 | |
| 148 | memcpy(&sin.sin_addr.s_addr, elem, IP_V4_ADDR_LEN); |
| 149 | elem += IP_V4_ADDR_LEN; |
| 150 | sin.sin_port = osmo_load16le(elem); |
| 151 | elem += IP_PORT_LEN; |
| 152 | |
| 153 | memcpy(ss, &sin, sizeof(sin)); |
| 154 | break; |
| 155 | case IP_V6_ADDR_LEN + IP_PORT_LEN: |
| 156 | memset(&sin6, 0, sizeof(sin6)); |
| 157 | sin6.sin6_family = AF_INET6; |
| 158 | |
| 159 | memcpy(sin6.sin6_addr.s6_addr, elem, IP_V6_ADDR_LEN); |
| 160 | elem += IP_V6_ADDR_LEN; |
| 161 | sin6.sin6_port = osmo_load16le(elem); |
| 162 | elem += IP_PORT_LEN; |
| 163 | |
| 164 | memcpy(ss, &sin6, sizeof(sin6)); |
| 165 | break; |
| 166 | default: |
| 167 | /* Malformed element! */ |
| 168 | return -EINVAL; |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | return (int)(elem - old_elem); |
| 173 | } |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 174 | |
Harald Welte | 20725b9 | 2017-05-15 12:50:04 +0200 | [diff] [blame] | 175 | #endif /* HAVE_SYS_SOCKET_H */ |
| 176 | |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 177 | /* Helper function for gsm0808_enc_speech_codec() |
| 178 | * and gsm0808_enc_speech_codec_list() */ |
| 179 | static uint8_t enc_speech_codec(struct msgb *msg, |
| 180 | const struct gsm0808_speech_codec *sc) |
| 181 | { |
| 182 | /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */ |
| 183 | uint8_t header = 0; |
| 184 | uint8_t *old_tail; |
Harald Welte | 459a180 | 2018-06-28 09:24:17 +0200 | [diff] [blame] | 185 | bool type_extended = false; |
Philipp Maier | bb83966 | 2017-06-01 17:11:19 +0200 | [diff] [blame] | 186 | |
| 187 | /* Note: Extended codec types are codec types that require 8 instead |
| 188 | * of 4 bit to fully specify the selected codec. In the following, |
| 189 | * we check if we work with an extended type or not. We also check |
| 190 | * if the codec type is valid at all. */ |
| 191 | switch(sc->type) { |
| 192 | case GSM0808_SCT_FR1: |
| 193 | case GSM0808_SCT_FR2: |
| 194 | case GSM0808_SCT_FR3: |
| 195 | case GSM0808_SCT_FR4: |
| 196 | case GSM0808_SCT_FR5: |
| 197 | case GSM0808_SCT_HR1: |
| 198 | case GSM0808_SCT_HR3: |
| 199 | case GSM0808_SCT_HR4: |
| 200 | case GSM0808_SCT_HR6: |
| 201 | type_extended = false; |
| 202 | break; |
| 203 | case GSM0808_SCT_CSD: |
| 204 | type_extended = true; |
| 205 | break; |
| 206 | default: |
| 207 | /* Invalid codec type specified */ |
| 208 | OSMO_ASSERT(false); |
| 209 | break; |
| 210 | } |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 211 | |
| 212 | old_tail = msg->tail; |
| 213 | |
| 214 | if (sc->fi) |
| 215 | header |= (1 << 7); |
| 216 | if (sc->pi) |
| 217 | header |= (1 << 6); |
| 218 | if (sc->pt) |
| 219 | header |= (1 << 5); |
| 220 | if (sc->tf) |
| 221 | header |= (1 << 4); |
Philipp Maier | bb83966 | 2017-06-01 17:11:19 +0200 | [diff] [blame] | 222 | |
| 223 | if (type_extended) { |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 224 | header |= 0x0f; |
| 225 | msgb_put_u8(msg, header); |
Philipp Maier | bb83966 | 2017-06-01 17:11:19 +0200 | [diff] [blame] | 226 | msgb_put_u8(msg, sc->type); |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 227 | } else { |
| 228 | OSMO_ASSERT(sc->type < 0x0f); |
| 229 | header |= sc->type; |
| 230 | msgb_put_u8(msg, header); |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Philipp Maier | bb83966 | 2017-06-01 17:11:19 +0200 | [diff] [blame] | 233 | /* Note: Whether a configuration is present or not depends on the |
| 234 | * selected codec type. If present, it can either consist of one |
| 235 | * or two octets, depending on the codec type */ |
| 236 | switch (sc->type) { |
| 237 | case GSM0808_SCT_FR3: |
| 238 | case GSM0808_SCT_HR3: |
| 239 | case GSM0808_SCT_HR6: |
Philipp Maier | 7e27b14 | 2018-03-22 17:26:46 +0100 | [diff] [blame] | 240 | msgb_put_u16(msg, osmo_ntohs(sc->cfg)); |
Philipp Maier | bb83966 | 2017-06-01 17:11:19 +0200 | [diff] [blame] | 241 | break; |
| 242 | case GSM0808_SCT_FR4: |
| 243 | case GSM0808_SCT_FR5: |
| 244 | case GSM0808_SCT_HR4: |
| 245 | case GSM0808_SCT_CSD: |
| 246 | OSMO_ASSERT((sc->cfg & 0xff00) == 0) |
| 247 | msgb_put_u8(msg, (uint8_t) sc->cfg & 0xff); |
| 248 | break; |
| 249 | default: |
| 250 | OSMO_ASSERT(sc->cfg == 0); |
| 251 | break; |
| 252 | } |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 253 | |
| 254 | return (uint8_t) (msg->tail - old_tail); |
| 255 | } |
| 256 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 257 | /*! Encode TS 08.08 Speech Codec IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 258 | * \param[out] msg Message Buffer to which IE will be appended |
| 259 | * \param[in] sc Speech Codec to be encoded into IE |
| 260 | * \returns number of bytes appended to \a msg */ |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 261 | uint8_t gsm0808_enc_speech_codec(struct msgb *msg, |
| 262 | const struct gsm0808_speech_codec *sc) |
| 263 | { |
Philipp Maier | 452a6bb | 2017-06-23 00:29:34 +0200 | [diff] [blame] | 264 | /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */ |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 265 | uint8_t *old_tail; |
| 266 | uint8_t *tlv_len; |
| 267 | |
| 268 | OSMO_ASSERT(msg); |
| 269 | OSMO_ASSERT(sc); |
| 270 | |
| 271 | msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC); |
| 272 | tlv_len = msgb_put(msg, 1); |
| 273 | old_tail = msg->tail; |
| 274 | |
| 275 | enc_speech_codec(msg, sc); |
| 276 | |
| 277 | *tlv_len = (uint8_t) (msg->tail - old_tail); |
| 278 | return *tlv_len + 2; |
| 279 | } |
| 280 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 281 | /*! Decode TS 08.08 Speech Codec IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 282 | * \param[out] sc Caller-allocated memory for Speech Codec |
| 283 | * \param[in] elem IE value to be decoded |
| 284 | * \param[in] len Length of \a elem in bytes |
| 285 | * \returns number of bytes parsed; negative on error */ |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 286 | int gsm0808_dec_speech_codec(struct gsm0808_speech_codec *sc, |
| 287 | const uint8_t *elem, uint8_t len) |
| 288 | { |
| 289 | /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */ |
| 290 | uint8_t header; |
| 291 | const uint8_t *old_elem = elem; |
| 292 | |
| 293 | OSMO_ASSERT(sc); |
| 294 | if (!elem) |
| 295 | return -EINVAL; |
Philipp Maier | 17778bd | 2017-04-28 11:05:44 +0200 | [diff] [blame] | 296 | if (len == 0) |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 297 | return -EINVAL; |
| 298 | |
| 299 | memset(sc, 0, sizeof(*sc)); |
| 300 | |
| 301 | header = *elem; |
| 302 | |
Philipp Maier | 85a6af2 | 2017-04-28 10:55:05 +0200 | [diff] [blame] | 303 | /* An extended codec type needs at least two fields, |
| 304 | * bail if the input data length is not sufficient. */ |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 305 | if ((header & 0x0F) == 0x0F && len < 2) |
| 306 | return -EINVAL; |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 307 | |
| 308 | elem++; |
| 309 | len--; |
| 310 | |
| 311 | if (header & (1 << 7)) |
| 312 | sc->fi = true; |
| 313 | if (header & (1 << 6)) |
| 314 | sc->pi = true; |
| 315 | if (header & (1 << 5)) |
| 316 | sc->pt = true; |
| 317 | if (header & (1 << 4)) |
| 318 | sc->tf = true; |
| 319 | |
| 320 | if ((header & 0x0F) != 0x0F) { |
| 321 | sc->type = (header & 0x0F); |
Philipp Maier | bb83966 | 2017-06-01 17:11:19 +0200 | [diff] [blame] | 322 | } else { |
| 323 | sc->type = *elem; |
| 324 | elem++; |
| 325 | len--; |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 326 | } |
| 327 | |
Philipp Maier | bb83966 | 2017-06-01 17:11:19 +0200 | [diff] [blame] | 328 | /* Note: Whether a configuration is present or not depends on the |
| 329 | * selected codec type. If present, it can either consist of one or |
| 330 | * two octets depending on the codec type */ |
| 331 | switch (sc->type) { |
| 332 | case GSM0808_SCT_FR1: |
| 333 | case GSM0808_SCT_FR2: |
| 334 | case GSM0808_SCT_HR1: |
| 335 | break; |
| 336 | case GSM0808_SCT_HR4: |
| 337 | case GSM0808_SCT_CSD: |
| 338 | case GSM0808_SCT_FR4: |
| 339 | case GSM0808_SCT_FR5: |
| 340 | if (len < 1) |
| 341 | return -EINVAL; |
| 342 | sc->cfg = *elem; |
| 343 | elem++; |
| 344 | break; |
| 345 | case GSM0808_SCT_FR3: |
| 346 | case GSM0808_SCT_HR3: |
| 347 | case GSM0808_SCT_HR6: |
| 348 | if (len < 2) |
| 349 | return -EINVAL; |
Philipp Maier | 7e27b14 | 2018-03-22 17:26:46 +0100 | [diff] [blame] | 350 | sc->cfg = osmo_load16le(elem); |
Philipp Maier | bb83966 | 2017-06-01 17:11:19 +0200 | [diff] [blame] | 351 | elem += 2; |
| 352 | break; |
| 353 | default: |
| 354 | /* Invalid codec type => malformed speech codec element! */ |
| 355 | return -EINVAL; |
| 356 | break; |
| 357 | } |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 358 | |
| 359 | return (int)(elem - old_elem); |
| 360 | } |
| 361 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 362 | /*! Encode TS 08.08 Speech Codec list |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 363 | * \param[out] msg Message Buffer to which IE is to be appended |
| 364 | * \param[in] scl Speech Codec List to be encoded into IE |
| 365 | * \returns number of bytes added to \a msg */ |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 366 | uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg, |
| 367 | const struct gsm0808_speech_codec_list *scl) |
| 368 | { |
Philipp Maier | 452a6bb | 2017-06-23 00:29:34 +0200 | [diff] [blame] | 369 | /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */ |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 370 | uint8_t *old_tail; |
| 371 | uint8_t *tlv_len; |
| 372 | unsigned int i; |
| 373 | uint8_t rc; |
| 374 | unsigned int bytes_used = 0; |
| 375 | |
| 376 | OSMO_ASSERT(msg); |
| 377 | OSMO_ASSERT(scl); |
| 378 | |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 379 | msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC_LIST); |
| 380 | tlv_len = msgb_put(msg, 1); |
| 381 | old_tail = msg->tail; |
| 382 | |
| 383 | for (i = 0; i < scl->len; i++) { |
| 384 | rc = enc_speech_codec(msg, &scl->codec[i]); |
| 385 | OSMO_ASSERT(rc >= 1); |
| 386 | bytes_used += rc; |
| 387 | OSMO_ASSERT(bytes_used <= 255); |
| 388 | } |
| 389 | |
| 390 | *tlv_len = (uint8_t) (msg->tail - old_tail); |
| 391 | return *tlv_len + 2; |
| 392 | } |
| 393 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 394 | /*! Decode TS 08.08 Speech Codec list IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 395 | * \param[out] scl Caller-provided memory to store codec list |
| 396 | * \param[in] elem IE value to be decoded |
| 397 | * \param[in] len Length of \a elem in bytes |
| 398 | * \returns number of bytes parsed; negative on error */ |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 399 | int gsm0808_dec_speech_codec_list(struct gsm0808_speech_codec_list *scl, |
| 400 | const uint8_t *elem, uint8_t len) |
| 401 | { |
Philipp Maier | 452a6bb | 2017-06-23 00:29:34 +0200 | [diff] [blame] | 402 | /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */ |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 403 | const uint8_t *old_elem = elem; |
| 404 | unsigned int i; |
| 405 | int rc; |
| 406 | uint8_t decoded = 0; |
| 407 | |
| 408 | OSMO_ASSERT(scl); |
| 409 | if (!elem) |
| 410 | return -EINVAL; |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 411 | |
| 412 | memset(scl, 0, sizeof(*scl)); |
| 413 | |
| 414 | for (i = 0; i < ARRAY_SIZE(scl->codec); i++) { |
| 415 | if (len <= 0) |
| 416 | break; |
| 417 | |
| 418 | rc = gsm0808_dec_speech_codec(&scl->codec[i], elem, len); |
| 419 | if (rc < 1) |
| 420 | return -EINVAL; |
| 421 | |
| 422 | elem+=rc; |
| 423 | len -= rc; |
| 424 | decoded++; |
| 425 | } |
| 426 | |
| 427 | scl->len = decoded; |
| 428 | |
Philipp Maier | 6f725d6 | 2017-03-24 18:03:17 +0100 | [diff] [blame] | 429 | return (int)(elem - old_elem); |
| 430 | } |
Philipp Maier | e0c6530 | 2017-03-28 17:05:40 +0200 | [diff] [blame] | 431 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 432 | /*! Encode TS 08.08 Channel Type IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 433 | * \param[out] msg Message Buffer to which IE is to be appended |
| 434 | * \param[in] ct Channel Type to be encoded |
| 435 | * \returns number of bytes added to \a msg */ |
Philipp Maier | e0c6530 | 2017-03-28 17:05:40 +0200 | [diff] [blame] | 436 | uint8_t gsm0808_enc_channel_type(struct msgb *msg, |
| 437 | const struct gsm0808_channel_type *ct) |
| 438 | { |
Philipp Maier | 452a6bb | 2017-06-23 00:29:34 +0200 | [diff] [blame] | 439 | /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */ |
Philipp Maier | e0c6530 | 2017-03-28 17:05:40 +0200 | [diff] [blame] | 440 | unsigned int i; |
| 441 | uint8_t byte; |
| 442 | uint8_t *old_tail; |
| 443 | uint8_t *tlv_len; |
| 444 | |
| 445 | OSMO_ASSERT(msg); |
| 446 | OSMO_ASSERT(ct); |
| 447 | OSMO_ASSERT(ct->perm_spch_len <= CHANNEL_TYPE_ELEMENT_MAXLEN - 2); |
| 448 | |
| 449 | /* FIXME: Implement encoding support for Data |
| 450 | * and Speech + CTM Text Telephony */ |
| 451 | if ((ct->ch_indctr & 0x0f) != GSM0808_CHAN_SPEECH |
| 452 | && (ct->ch_indctr & 0x0f) != GSM0808_CHAN_SIGN) |
| 453 | OSMO_ASSERT(false); |
| 454 | |
| 455 | msgb_put_u8(msg, GSM0808_IE_CHANNEL_TYPE); |
| 456 | tlv_len = msgb_put(msg, 1); |
| 457 | old_tail = msg->tail; |
| 458 | |
| 459 | msgb_put_u8(msg, ct->ch_indctr & 0x0f); |
| 460 | msgb_put_u8(msg, ct->ch_rate_type); |
| 461 | |
| 462 | for (i = 0; i < ct->perm_spch_len; i++) { |
| 463 | byte = ct->perm_spch[i]; |
| 464 | |
| 465 | if (i < ct->perm_spch_len - 1) |
| 466 | byte |= 0x80; |
| 467 | msgb_put_u8(msg, byte); |
| 468 | } |
| 469 | |
| 470 | *tlv_len = (uint8_t) (msg->tail - old_tail); |
| 471 | return *tlv_len + 2; |
| 472 | } |
| 473 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 474 | /*! Decode TS 08.08 Channel Type IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 475 | * \param[out] ct Caller-provided memory to store channel type |
| 476 | * \param[in] elem IE Value to be decoded |
| 477 | * \param[in] len Length of \a elem in bytes |
| 478 | * \returns number of bytes parsed; negative on error */ |
Philipp Maier | e0c6530 | 2017-03-28 17:05:40 +0200 | [diff] [blame] | 479 | int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct, |
| 480 | const uint8_t *elem, uint8_t len) |
| 481 | { |
Philipp Maier | 452a6bb | 2017-06-23 00:29:34 +0200 | [diff] [blame] | 482 | /*! See also 3GPP TS 48.008 3.2.2.11 Channel Type */ |
Philipp Maier | e0c6530 | 2017-03-28 17:05:40 +0200 | [diff] [blame] | 483 | unsigned int i; |
| 484 | uint8_t byte; |
| 485 | const uint8_t *old_elem = elem; |
| 486 | |
| 487 | OSMO_ASSERT(ct); |
| 488 | if (!elem) |
| 489 | return -EINVAL; |
Philipp Maier | 17778bd | 2017-04-28 11:05:44 +0200 | [diff] [blame] | 490 | if (len < 3 || len > 11) |
Philipp Maier | e0c6530 | 2017-03-28 17:05:40 +0200 | [diff] [blame] | 491 | return -EINVAL; |
| 492 | |
| 493 | memset(ct, 0, sizeof(*ct)); |
| 494 | |
| 495 | ct->ch_indctr = (*elem) & 0x0f; |
| 496 | elem++; |
| 497 | ct->ch_rate_type = (*elem) & 0x0f; |
| 498 | elem++; |
| 499 | |
| 500 | for (i = 0; i < ARRAY_SIZE(ct->perm_spch); i++) { |
| 501 | byte = *elem; |
| 502 | elem++; |
| 503 | ct->perm_spch[i] = byte & 0x7f; |
| 504 | if ((byte & 0x80) == 0x00) |
| 505 | break; |
| 506 | } |
| 507 | ct->perm_spch_len = i + 1; |
| 508 | |
| 509 | return (int)(elem - old_elem); |
| 510 | } |
Philipp Maier | 14e76b9 | 2017-03-28 18:36:52 +0200 | [diff] [blame] | 511 | |
Max | 969fb2e | 2018-12-10 11:01:10 +0100 | [diff] [blame] | 512 | /*! Create BSSMAP Global Call Reference, 3GPP TS 48.008 §3.2.2.115. |
| 513 | * \param[out] msg Message Buffer for appending IE |
| 514 | * \param[in] g Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1 |
| 515 | * \returns number of bytes added to \a msg or 0 on error */ |
Max | 4702215 | 2018-12-19 18:51:00 +0100 | [diff] [blame] | 516 | static uint8_t gsm0808_enc_gcr(struct msgb *msg, const struct osmo_gcr_parsed *g) |
Max | 969fb2e | 2018-12-10 11:01:10 +0100 | [diff] [blame] | 517 | { |
| 518 | uint8_t enc, *len = msgb_tl_put(msg, GSM0808_IE_GLOBAL_CALL_REF); |
| 519 | |
| 520 | enc = osmo_enc_gcr(msg, g); |
| 521 | if (!enc) |
| 522 | return 0; |
| 523 | |
| 524 | *len = enc; |
| 525 | return enc + 2; /* type (1 byte) + length (1 byte) */ |
| 526 | } |
| 527 | |
| 528 | /*! Decode BSSMAP Global Call Reference, 3GPP TS 29.205 Table B 2.1.9.1. |
| 529 | * \param[out] gcr Caller-provided memory to store Global Call Reference |
Max | 036012b | 2018-12-19 17:48:56 +0100 | [diff] [blame] | 530 | * \param[in] tp IE values to be decoded |
Max | 969fb2e | 2018-12-10 11:01:10 +0100 | [diff] [blame] | 531 | * \returns number of bytes parsed; negative on error */ |
Max | 4702215 | 2018-12-19 18:51:00 +0100 | [diff] [blame] | 532 | static int gsm0808_dec_gcr(struct osmo_gcr_parsed *gcr, const struct tlv_parsed *tp) |
Max | 969fb2e | 2018-12-10 11:01:10 +0100 | [diff] [blame] | 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 | |
Max | 4702215 | 2018-12-19 18:51:00 +0100 | [diff] [blame] | 546 | /*! Add LCLS parameters to a given msgb, 3GPP TS 48.008 §3.2.2.115 - 3.2.2.120. |
| 547 | * \param[out] msg Message Buffer for appending IE |
| 548 | * \param[in] lcls LCLS-related data |
| 549 | * \returns number of bytes added to \a msg or 0 on error */ |
| 550 | uint8_t gsm0808_enc_lcls(struct msgb *msg, const struct osmo_lcls *lcls) |
| 551 | { |
| 552 | uint8_t enc = 0; |
| 553 | |
| 554 | /* LCLS: §3.2.2.115 Global Call Reference */ |
Max | 3b90125 | 2019-01-15 14:15:11 +0100 | [diff] [blame] | 555 | if (lcls->gcr_available) |
| 556 | enc = gsm0808_enc_gcr(msg, &lcls->gcr); |
Max | 4702215 | 2018-12-19 18:51:00 +0100 | [diff] [blame] | 557 | |
| 558 | /* LCLS: §3.2.2.116 Configuration */ |
| 559 | if (lcls->config != GSM0808_LCLS_CFG_NA) { |
| 560 | msgb_tv_put(msg, GSM0808_IE_LCLS_CONFIG, lcls->config); |
| 561 | enc += 2; |
| 562 | } |
| 563 | |
| 564 | /* LCLS: §3.2.2.117 Connection Status Control */ |
| 565 | if (lcls->control != GSM0808_LCLS_CSC_NA) { |
| 566 | msgb_tv_put(msg, GSM0808_IE_LCLS_CONN_STATUS_CTRL, lcls->control); |
| 567 | enc += 2; |
| 568 | } |
| 569 | |
| 570 | /* LCLS: §3.2.2.118 Correlation-Not-Needed */ |
| 571 | if (!lcls->corr_needed) { |
| 572 | msgb_v_put(msg, GSM0808_IE_LCLS_CORR_NOT_NEEDED); |
| 573 | enc++; |
| 574 | } |
| 575 | |
| 576 | return enc; |
| 577 | } |
| 578 | |
| 579 | /*! Decode LCLS parameters to a given msgb, 3GPP TS 48.008 §3.2.2.115 - 3.2.2.120. |
| 580 | * \param[out] lcls Caller-provided memory to store LCLS-related data |
| 581 | * \param[in] tp IE values to be decoded |
| 582 | * \returns GCR size or negative on error */ |
| 583 | int gsm0808_dec_lcls(struct osmo_lcls *lcls, const struct tlv_parsed *tp) |
| 584 | { |
Max | 3b90125 | 2019-01-15 14:15:11 +0100 | [diff] [blame] | 585 | int ret = gsm0808_dec_gcr(&lcls->gcr, tp); |
Max | 4702215 | 2018-12-19 18:51:00 +0100 | [diff] [blame] | 586 | |
Max | 3b90125 | 2019-01-15 14:15:11 +0100 | [diff] [blame] | 587 | lcls->gcr_available = (ret < 0) ? false : true; |
Max | 4702215 | 2018-12-19 18:51:00 +0100 | [diff] [blame] | 588 | lcls->config = tlvp_val8(tp, GSM0808_IE_LCLS_CONFIG, GSM0808_LCLS_CFG_NA); |
| 589 | lcls->control = tlvp_val8(tp, GSM0808_IE_LCLS_CONN_STATUS_CTRL, GSM0808_LCLS_CSC_NA); |
| 590 | lcls->corr_needed = TLVP_PRESENT(tp, GSM0808_IE_LCLS_CORR_NOT_NEEDED) ? false : true; |
| 591 | |
| 592 | return ret; |
| 593 | } |
| 594 | |
Max | 5ec0cf5 | 2019-01-15 16:37:09 +0100 | [diff] [blame] | 595 | static char dbuf[256]; |
| 596 | |
| 597 | /*! Dump LCLS parameters (GCR excluded) into string for printing. |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame^] | 598 | * \param[out] buf caller-allocated output string buffer |
| 599 | * \param[in] buf_len size of buf in bytes |
Max | 5ec0cf5 | 2019-01-15 16:37:09 +0100 | [diff] [blame] | 600 | * \param[in] lcls pointer to the struct to print. |
| 601 | * \returns string representation of LCLS or NULL on error. */ |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame^] | 602 | char *osmo_lcls_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls) |
Max | 5ec0cf5 | 2019-01-15 16:37:09 +0100 | [diff] [blame] | 603 | { |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame^] | 604 | struct osmo_strbuf s = { .buf = buf, .len = buf_len }; |
Max | 5ec0cf5 | 2019-01-15 16:37:09 +0100 | [diff] [blame] | 605 | |
| 606 | if (!lcls) |
| 607 | return NULL; |
| 608 | |
| 609 | OSMO_STRBUF_PRINTF(s, "LCLS Config: %s, Control: %s, Correlation-Needed: %u", |
| 610 | gsm0808_lcls_config_name(lcls->config), |
| 611 | gsm0808_lcls_control_name(lcls->control), |
| 612 | lcls->corr_needed); |
| 613 | |
| 614 | return dbuf; |
| 615 | } |
| 616 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame^] | 617 | /*! Dump LCLS parameters (GCR excluded) into static string buffer for printing. |
| 618 | * \param[in] lcls pointer to the struct to print. |
| 619 | * \returns string representation of LCLS in static buffer or NULL on error. */ |
| 620 | char *osmo_lcls_dump(const struct osmo_lcls *lcls) |
| 621 | { |
| 622 | return osmo_lcls_dump_buf(dbuf, sizeof(dbuf), lcls); |
| 623 | } |
| 624 | |
Max | 5ec0cf5 | 2019-01-15 16:37:09 +0100 | [diff] [blame] | 625 | /*! Dump GCR struct into string for printing. |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame^] | 626 | * \param[out] buf caller-allocated output string buffer |
| 627 | * \param[in] buf_len size of buf in bytes |
Max | 5ec0cf5 | 2019-01-15 16:37:09 +0100 | [diff] [blame] | 628 | * \param[in] lcls pointer to the struct to print. |
| 629 | * \returns string representation of GCR or NULL on error. */ |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame^] | 630 | char *osmo_gcr_dump_buf(char *buf, size_t buf_len, const struct osmo_lcls *lcls) |
Max | 5ec0cf5 | 2019-01-15 16:37:09 +0100 | [diff] [blame] | 631 | { |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame^] | 632 | struct osmo_strbuf s = { .buf = buf, .len = buf_len }; |
Max | 5ec0cf5 | 2019-01-15 16:37:09 +0100 | [diff] [blame] | 633 | |
| 634 | if (!lcls) |
| 635 | return NULL; |
| 636 | |
| 637 | if (lcls->gcr_available) { |
| 638 | OSMO_STRBUF_PRINTF(s, "GCR NetID 0x%s, ", osmo_hexdump_nospc(lcls->gcr.net, lcls->gcr.net_len)); |
| 639 | /* osmo_hexdump() uses static buffers so we can't call it twice withing the same parameter list */ |
| 640 | OSMO_STRBUF_PRINTF(s, "Node 0x%x, CallRefID 0x%s", lcls->gcr.node, osmo_hexdump_nospc(lcls->gcr.cr, 5)); |
| 641 | } |
| 642 | |
| 643 | return dbuf; |
| 644 | } |
| 645 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame^] | 646 | /*! Dump GCR struct into static string buffer for printing. |
| 647 | * \param[in] lcls pointer to the struct to print. |
| 648 | * \returns string representation of GCR in static buffer or NULL on error. */ |
| 649 | char *osmo_gcr_dump(const struct osmo_lcls *lcls) |
| 650 | { |
| 651 | return osmo_gcr_dump_buf(dbuf, sizeof(dbuf), lcls); |
| 652 | } |
| 653 | |
| 654 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 655 | /*! Encode TS 08.08 Encryption Information IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 656 | * \param[out] msg Message Buffer to which IE is to be appended |
| 657 | * \param[in] ei Encryption Information to be encoded |
| 658 | * \returns number of bytes appended to \a msg */ |
Philipp Maier | 14e76b9 | 2017-03-28 18:36:52 +0200 | [diff] [blame] | 659 | uint8_t gsm0808_enc_encrypt_info(struct msgb *msg, |
| 660 | const struct gsm0808_encrypt_info *ei) |
| 661 | { |
| 662 | unsigned int i; |
| 663 | uint8_t perm_algo = 0; |
| 664 | uint8_t *ptr; |
| 665 | uint8_t *old_tail; |
| 666 | uint8_t *tlv_len; |
| 667 | |
| 668 | OSMO_ASSERT(msg); |
| 669 | OSMO_ASSERT(ei); |
| 670 | OSMO_ASSERT(ei->key_len <= ARRAY_SIZE(ei->key)); |
| 671 | OSMO_ASSERT(ei->perm_algo_len <= ENCRY_INFO_PERM_ALGO_MAXLEN); |
| 672 | |
| 673 | msgb_put_u8(msg, GSM0808_IE_ENCRYPTION_INFORMATION); |
| 674 | tlv_len = msgb_put(msg, 1); |
| 675 | old_tail = msg->tail; |
| 676 | |
| 677 | for (i = 0; i < ei->perm_algo_len; i++) { |
| 678 | /* Note: gsm_08_08.h defines the permitted algorithms |
| 679 | * as an enum which ranges from 0x01 to 0x08 */ |
| 680 | OSMO_ASSERT(ei->perm_algo[i] != 0); |
| 681 | OSMO_ASSERT(ei->perm_algo[i] <= ENCRY_INFO_PERM_ALGO_MAXLEN); |
| 682 | perm_algo |= (1 << (ei->perm_algo[i] - 1)); |
| 683 | } |
| 684 | |
| 685 | msgb_put_u8(msg, perm_algo); |
| 686 | ptr = msgb_put(msg, ei->key_len); |
| 687 | memcpy(ptr, ei->key, ei->key_len); |
| 688 | |
| 689 | *tlv_len = (uint8_t) (msg->tail - old_tail); |
| 690 | return *tlv_len + 2; |
| 691 | } |
| 692 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 693 | /*! Decode TS 08.08 Encryption Information IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 694 | * \param[out] ei Caller-provided memory to store encryption information |
| 695 | * \param[in] elem IE value to be decoded |
| 696 | * \param[in] len Length of \a elem in bytes |
| 697 | * \returns number of bytes parsed; negative on error */ |
Philipp Maier | 14e76b9 | 2017-03-28 18:36:52 +0200 | [diff] [blame] | 698 | int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei, |
| 699 | const uint8_t *elem, uint8_t len) |
| 700 | { |
| 701 | uint8_t perm_algo; |
| 702 | unsigned int i; |
| 703 | unsigned int perm_algo_len = 0; |
| 704 | const uint8_t *old_elem = elem; |
| 705 | |
| 706 | OSMO_ASSERT(ei); |
| 707 | if (!elem) |
| 708 | return -EINVAL; |
Philipp Maier | 17778bd | 2017-04-28 11:05:44 +0200 | [diff] [blame] | 709 | if (len == 0) |
Philipp Maier | 14e76b9 | 2017-03-28 18:36:52 +0200 | [diff] [blame] | 710 | return -EINVAL; |
| 711 | |
| 712 | memset(ei, 0, sizeof(*ei)); |
| 713 | |
| 714 | perm_algo = *elem; |
| 715 | elem++; |
| 716 | |
| 717 | for (i = 0; i < ENCRY_INFO_PERM_ALGO_MAXLEN; i++) { |
| 718 | if (perm_algo & (1 << i)) { |
| 719 | ei->perm_algo[perm_algo_len] = i + 1; |
| 720 | perm_algo_len++; |
| 721 | } |
| 722 | } |
| 723 | ei->perm_algo_len = perm_algo_len; |
| 724 | |
| 725 | ei->key_len = len - 1; |
| 726 | memcpy(ei->key, elem, ei->key_len); |
| 727 | elem+=ei->key_len; |
| 728 | |
| 729 | return (int)(elem - old_elem); |
| 730 | } |
Philipp Maier | 783047e | 2017-03-29 11:35:50 +0200 | [diff] [blame] | 731 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 732 | /*! Encode TS 08.08 Cell Identifier List IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 733 | * \param[out] msg Message Buffer to which IE is to be appended |
| 734 | * \param[in] cil Cell ID List to be encoded |
| 735 | * \returns number of bytes appended to \a msg */ |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 736 | uint8_t gsm0808_enc_cell_id_list2(struct msgb *msg, |
| 737 | const struct gsm0808_cell_id_list2 *cil) |
| 738 | { |
| 739 | uint8_t *old_tail; |
| 740 | uint8_t *tlv_len; |
| 741 | unsigned int i; |
| 742 | |
| 743 | OSMO_ASSERT(msg); |
| 744 | OSMO_ASSERT(cil); |
| 745 | |
| 746 | msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 747 | tlv_len = msgb_put(msg, 1); |
| 748 | old_tail = msg->tail; |
| 749 | |
| 750 | msgb_put_u8(msg, cil->id_discr & 0x0f); |
| 751 | |
| 752 | OSMO_ASSERT(cil->id_list_len <= GSM0808_CELL_ID_LIST2_MAXLEN) |
| 753 | switch (cil->id_discr) { |
| 754 | case CELL_IDENT_WHOLE_GLOBAL: |
| 755 | for (i = 0; i < cil->id_list_len; i++) { |
| 756 | const struct osmo_cell_global_id *id = &cil->id_list[i].global; |
| 757 | struct gsm48_loc_area_id lai; |
Neels Hofmeyr | 8b8cd93 | 2018-03-23 01:47:37 +0100 | [diff] [blame] | 758 | gsm48_generate_lai2(&lai, &id->lai); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 759 | memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai)); |
| 760 | msgb_put_u16(msg, id->cell_identity); |
| 761 | } |
| 762 | break; |
| 763 | case CELL_IDENT_LAC_AND_CI: |
| 764 | for (i = 0; i < cil->id_list_len; i++) { |
| 765 | const struct osmo_lac_and_ci_id *id = &cil->id_list[i].lac_and_ci; |
| 766 | msgb_put_u16(msg, id->lac); |
| 767 | msgb_put_u16(msg, id->ci); |
| 768 | } |
| 769 | break; |
| 770 | case CELL_IDENT_CI: |
| 771 | for (i = 0; i < cil->id_list_len; i++) |
| 772 | msgb_put_u16(msg, cil->id_list[i].ci); |
| 773 | break; |
| 774 | case CELL_IDENT_LAI_AND_LAC: |
| 775 | for (i = 0; i < cil->id_list_len; i++) { |
| 776 | const struct osmo_location_area_id *id = &cil->id_list[i].lai_and_lac; |
| 777 | struct gsm48_loc_area_id lai; |
Neels Hofmeyr | 8b8cd93 | 2018-03-23 01:47:37 +0100 | [diff] [blame] | 778 | gsm48_generate_lai2(&lai, id); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 779 | memcpy(msgb_put(msg, sizeof(lai)), &lai, sizeof(lai)); |
| 780 | } |
| 781 | break; |
| 782 | case CELL_IDENT_LAC: |
| 783 | for (i = 0; i < cil->id_list_len; i++) |
| 784 | msgb_put_u16(msg, cil->id_list[i].lac); |
| 785 | break; |
| 786 | case CELL_IDENT_BSS: |
| 787 | case CELL_IDENT_NO_CELL: |
| 788 | /* Does not have any list items */ |
| 789 | break; |
| 790 | default: |
| 791 | /* Support for other identifier list types is not implemented. */ |
| 792 | OSMO_ASSERT(false); |
| 793 | } |
| 794 | |
| 795 | *tlv_len = (uint8_t) (msg->tail - old_tail); |
| 796 | return *tlv_len + 2; |
| 797 | } |
| 798 | |
| 799 | /*! DEPRECATED: Use gsm0808_enc_cell_id_list2 instead. |
| 800 | * |
| 801 | * Encode TS 08.08 Cell Identifier List IE |
| 802 | * \param[out] msg Message Buffer to which IE is to be appended |
| 803 | * \param[in] cil Cell ID List to be encoded |
| 804 | * \returns number of bytes appended to \a msg */ |
Philipp Maier | 783047e | 2017-03-29 11:35:50 +0200 | [diff] [blame] | 805 | uint8_t gsm0808_enc_cell_id_list(struct msgb *msg, |
| 806 | const struct gsm0808_cell_id_list *cil) |
| 807 | { |
| 808 | uint8_t *old_tail; |
| 809 | uint8_t *tlv_len; |
| 810 | unsigned int i; |
| 811 | |
| 812 | OSMO_ASSERT(msg); |
| 813 | OSMO_ASSERT(cil); |
| 814 | |
| 815 | msgb_put_u8(msg, GSM0808_IE_CELL_IDENTIFIER_LIST); |
| 816 | tlv_len = msgb_put(msg, 1); |
| 817 | old_tail = msg->tail; |
| 818 | |
| 819 | msgb_put_u8(msg, cil->id_discr & 0x0f); |
| 820 | |
| 821 | switch (cil->id_discr) { |
| 822 | case CELL_IDENT_LAC: |
| 823 | OSMO_ASSERT(cil->id_list_len <= CELL_ID_LIST_LAC_MAXLEN) |
| 824 | for (i=0;i<cil->id_list_len;i++) { |
| 825 | msgb_put_u16(msg, cil->id_list_lac[i]); |
| 826 | } |
| 827 | break; |
| 828 | case CELL_IDENT_BSS: |
| 829 | /* Does not have any list items */ |
| 830 | break; |
| 831 | default: |
| 832 | /* FIXME: Implement support for all identifier list elements */ |
| 833 | OSMO_ASSERT(false); |
| 834 | } |
| 835 | |
| 836 | *tlv_len = (uint8_t) (msg->tail - old_tail); |
| 837 | return *tlv_len + 2; |
| 838 | } |
| 839 | |
Stefan Sperling | 2338145 | 2018-03-15 19:38:15 +0100 | [diff] [blame] | 840 | /* Decode 5-byte LAI list element data (see TS 08.08 3.2.2.27) into MCC/MNC/LAC. */ |
| 841 | static void decode_lai(const uint8_t *data, struct osmo_location_area_id *decoded) |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 842 | { |
| 843 | struct gsm48_loc_area_id lai; |
| 844 | |
Stefan Sperling | 2338145 | 2018-03-15 19:38:15 +0100 | [diff] [blame] | 845 | /* Copy data to stack to prevent unaligned access in gsm48_decode_lai2(). */ |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 846 | memcpy(&lai, data, sizeof(lai)); /* don't byte swap yet */ |
| 847 | |
Stefan Sperling | 2338145 | 2018-03-15 19:38:15 +0100 | [diff] [blame] | 848 | gsm48_decode_lai2(&lai, decoded); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 849 | } |
| 850 | |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 851 | static int parse_cell_id_global_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain, |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 852 | size_t *consumed) |
| 853 | { |
| 854 | struct osmo_cell_global_id *id; |
| 855 | uint16_t *ci_be; |
| 856 | size_t lai_offset; |
| 857 | int i = 0; |
| 858 | const size_t elemlen = sizeof(struct gsm48_loc_area_id) + sizeof(*ci_be); |
| 859 | |
| 860 | *consumed = 0; |
| 861 | while (remain >= elemlen) { |
| 862 | if (i >= GSM0808_CELL_ID_LIST2_MAXLEN) |
| 863 | return -ENOSPC; |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 864 | id = &cil->id_list[i].global; |
Stefan Sperling | 2873bf1 | 2018-03-14 18:38:41 +0100 | [diff] [blame] | 865 | lai_offset = i * elemlen; |
Stefan Sperling | 2338145 | 2018-03-15 19:38:15 +0100 | [diff] [blame] | 866 | decode_lai(&data[lai_offset], &id->lai); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 867 | ci_be = (uint16_t *)(&data[lai_offset + sizeof(struct gsm48_loc_area_id)]); |
| 868 | id->cell_identity = osmo_load16be(ci_be); |
| 869 | *consumed += elemlen; |
| 870 | remain -= elemlen; |
| 871 | i++; |
| 872 | } |
| 873 | |
| 874 | return i; |
| 875 | } |
| 876 | |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 877 | static int parse_cell_id_lac_and_ci_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain, |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 878 | size_t *consumed) |
| 879 | { |
| 880 | uint16_t *lacp_be, *ci_be; |
| 881 | struct osmo_lac_and_ci_id *id; |
Stefan Sperling | ed4327c | 2018-03-16 11:02:59 +0100 | [diff] [blame] | 882 | int i = 0, j = 0; |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 883 | const size_t elemlen = sizeof(*lacp_be) + sizeof(*ci_be); |
| 884 | |
| 885 | *consumed = 0; |
| 886 | |
| 887 | if (remain < elemlen) |
| 888 | return -EINVAL; |
| 889 | |
Stefan Sperling | ed4327c | 2018-03-16 11:02:59 +0100 | [diff] [blame] | 890 | lacp_be = (uint16_t *)(&data[j]); |
| 891 | ci_be = (uint16_t *)(&data[j + elemlen/2]); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 892 | while (remain >= elemlen) { |
| 893 | if (i >= GSM0808_CELL_ID_LIST2_MAXLEN) |
| 894 | return -ENOSPC; |
Stefan Sperling | ed4327c | 2018-03-16 11:02:59 +0100 | [diff] [blame] | 895 | id = &cil->id_list[i++].lac_and_ci; |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 896 | id->lac = osmo_load16be(lacp_be); |
| 897 | id->ci = osmo_load16be(ci_be); |
| 898 | *consumed += elemlen; |
| 899 | remain -= elemlen; |
Stefan Sperling | ed4327c | 2018-03-16 11:02:59 +0100 | [diff] [blame] | 900 | j += elemlen; |
| 901 | lacp_be = (uint16_t *)(&data[j]); |
| 902 | ci_be = (uint16_t *)(&data[j + elemlen/2]); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | return i; |
| 906 | } |
| 907 | |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 908 | static int parse_cell_id_ci_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain, |
| 909 | size_t *consumed) |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 910 | { |
| 911 | const uint16_t *ci_be = (const uint16_t *)data; |
| 912 | int i = 0; |
| 913 | const size_t elemlen = sizeof(*ci_be); |
| 914 | |
| 915 | *consumed = 0; |
| 916 | while (remain >= elemlen) { |
| 917 | if (i >= GSM0808_CELL_ID_LIST2_MAXLEN) |
| 918 | return -ENOSPC; |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 919 | cil->id_list[i++].ci = osmo_load16be(ci_be++); |
Stefan Sperling | 9c62fc6 | 2018-03-16 10:23:34 +0100 | [diff] [blame] | 920 | *consumed += elemlen; |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 921 | remain -= elemlen; |
| 922 | } |
| 923 | return i; |
| 924 | } |
| 925 | |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 926 | static int parse_cell_id_lai_and_lac(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain, |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 927 | size_t *consumed) |
| 928 | { |
| 929 | struct osmo_location_area_id *id; |
| 930 | int i = 0; |
| 931 | const size_t elemlen = sizeof(struct gsm48_loc_area_id); |
| 932 | |
| 933 | *consumed = 0; |
| 934 | while (remain >= elemlen) { |
| 935 | if (i >= GSM0808_CELL_ID_LIST2_MAXLEN) |
| 936 | return -ENOSPC; |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 937 | id = &cil->id_list[i].lai_and_lac; |
Stefan Sperling | 2338145 | 2018-03-15 19:38:15 +0100 | [diff] [blame] | 938 | decode_lai(&data[i * elemlen], id); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 939 | *consumed += elemlen; |
| 940 | remain -= elemlen; |
| 941 | i++; |
| 942 | } |
| 943 | |
| 944 | return i; |
| 945 | } |
| 946 | |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 947 | static int parse_cell_id_lac_list(struct gsm0808_cell_id_list2 *cil, const uint8_t *data, size_t remain, size_t *consumed) |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 948 | { |
| 949 | const uint16_t *lac_be = (const uint16_t *)data; |
| 950 | int i = 0; |
| 951 | const size_t elemlen = sizeof(*lac_be); |
| 952 | |
| 953 | *consumed = 0; |
| 954 | while (remain >= elemlen) { |
| 955 | if (i >= GSM0808_CELL_ID_LIST2_MAXLEN) |
| 956 | return -ENOSPC; |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 957 | cil->id_list[i++].lac = osmo_load16be(lac_be++); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 958 | *consumed += elemlen; |
| 959 | remain -= elemlen; |
| 960 | } |
| 961 | return i; |
| 962 | } |
| 963 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 964 | /*! Decode Cell Identifier List IE |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 965 | * \param[out] cil Caller-provided memory to store Cell ID list |
| 966 | * \param[in] elem IE value to be decoded |
| 967 | * \param[in] len Length of \a elem in bytes |
| 968 | * \returns number of bytes parsed; negative on error */ |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 969 | int gsm0808_dec_cell_id_list2(struct gsm0808_cell_id_list2 *cil, |
| 970 | const uint8_t *elem, uint8_t len) |
| 971 | { |
| 972 | uint8_t id_discr; |
| 973 | size_t bytes_elem = 0; |
| 974 | int list_len = 0; |
| 975 | |
| 976 | OSMO_ASSERT(cil); |
| 977 | if (!elem) |
| 978 | return -EINVAL; |
| 979 | if (len == 0) |
| 980 | return -EINVAL; |
| 981 | |
| 982 | memset(cil, 0, sizeof(*cil)); |
| 983 | |
| 984 | id_discr = *elem & 0x0f; |
| 985 | elem++; |
| 986 | len--; |
| 987 | |
| 988 | switch (id_discr) { |
| 989 | case CELL_IDENT_WHOLE_GLOBAL: |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 990 | list_len = parse_cell_id_global_list(cil, elem, len, &bytes_elem); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 991 | break; |
| 992 | case CELL_IDENT_LAC_AND_CI: |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 993 | list_len = parse_cell_id_lac_and_ci_list(cil, elem, len, &bytes_elem); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 994 | break; |
| 995 | case CELL_IDENT_CI: |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 996 | list_len = parse_cell_id_ci_list(cil, elem, len, &bytes_elem); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 997 | break; |
| 998 | case CELL_IDENT_LAI_AND_LAC: |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 999 | list_len = parse_cell_id_lai_and_lac(cil, elem, len, &bytes_elem); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 1000 | break; |
| 1001 | case CELL_IDENT_LAC: |
Stefan Sperling | e1a8674 | 2018-03-15 18:05:02 +0100 | [diff] [blame] | 1002 | list_len = parse_cell_id_lac_list(cil, elem, len, &bytes_elem); |
Stefan Sperling | 11a4d9d | 2018-02-15 18:28:04 +0100 | [diff] [blame] | 1003 | break; |
| 1004 | case CELL_IDENT_BSS: |
| 1005 | case CELL_IDENT_NO_CELL: |
| 1006 | /* Does not have any list items */ |
| 1007 | break; |
| 1008 | default: |
| 1009 | /* Remaining cell identification types are not implemented. */ |
| 1010 | return -EINVAL; |
| 1011 | } |
| 1012 | |
| 1013 | if (list_len < 0) /* parsing error */ |
| 1014 | return list_len; |
| 1015 | |
| 1016 | cil->id_discr = id_discr; |
| 1017 | cil->id_list_len = list_len; |
| 1018 | |
| 1019 | /* One byte for the cell ID discriminator + any remaining bytes in |
| 1020 | * the IE which were consumed by the parser functions above. */ |
| 1021 | return 1 + (int)bytes_elem; |
| 1022 | } |
| 1023 | |
| 1024 | /*! DEPRECATED: Use gsm0808_dec_cell_id_list2 instead. |
| 1025 | * |
| 1026 | * Decode Cell Identifier List IE |
| 1027 | * \param[out] cil Caller-provided memory to store Cell ID list |
| 1028 | * \param[in] elem IE value to be decoded |
| 1029 | * \param[in] len Length of \a elem in bytes |
| 1030 | * \returns number of bytes parsed; negative on error */ |
Philipp Maier | 783047e | 2017-03-29 11:35:50 +0200 | [diff] [blame] | 1031 | int gsm0808_dec_cell_id_list(struct gsm0808_cell_id_list *cil, |
| 1032 | const uint8_t *elem, uint8_t len) |
| 1033 | { |
| 1034 | uint8_t id_discr; |
| 1035 | const uint8_t *old_elem = elem; |
| 1036 | unsigned int item_count = 0; |
| 1037 | |
| 1038 | OSMO_ASSERT(cil); |
| 1039 | if (!elem) |
| 1040 | return -EINVAL; |
Philipp Maier | 17778bd | 2017-04-28 11:05:44 +0200 | [diff] [blame] | 1041 | if (len == 0) |
Philipp Maier | 783047e | 2017-03-29 11:35:50 +0200 | [diff] [blame] | 1042 | return -EINVAL; |
| 1043 | |
| 1044 | memset(cil, 0, sizeof(*cil)); |
| 1045 | |
| 1046 | id_discr = *elem & 0x0f; |
| 1047 | elem++; |
| 1048 | len--; |
| 1049 | |
| 1050 | cil->id_discr = id_discr; |
| 1051 | |
| 1052 | switch (id_discr) { |
| 1053 | case CELL_IDENT_LAC: |
| 1054 | while (len >= 2) { |
| 1055 | cil->id_list_lac[item_count] = osmo_load16be(elem); |
| 1056 | elem += 2; |
| 1057 | item_count++; |
| 1058 | len -= 2; |
| 1059 | } |
| 1060 | case CELL_IDENT_BSS: |
| 1061 | /* Does not have any list items */ |
| 1062 | break; |
| 1063 | default: |
| 1064 | /* FIXME: Implement support for all identifier list elements */ |
| 1065 | return -EINVAL; |
| 1066 | } |
| 1067 | |
| 1068 | cil->id_list_len = item_count; |
| 1069 | return (int)(elem - old_elem); |
| 1070 | } |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 1071 | |
Neels Hofmeyr | 74663d9 | 2018-03-23 01:46:42 +0100 | [diff] [blame] | 1072 | static bool same_cell_id_list_entries(const struct gsm0808_cell_id_list2 *a, int ai, |
| 1073 | const struct gsm0808_cell_id_list2 *b, int bi) |
| 1074 | { |
| 1075 | struct gsm0808_cell_id_list2 tmp = { |
| 1076 | .id_discr = a->id_discr, |
| 1077 | .id_list_len = 1, |
| 1078 | }; |
| 1079 | uint8_t buf_a[32 + sizeof(struct msgb)]; |
| 1080 | uint8_t buf_b[32 + sizeof(struct msgb)]; |
| 1081 | struct msgb *msg_a = (void*)buf_a; |
| 1082 | struct msgb *msg_b = (void*)buf_b; |
| 1083 | |
| 1084 | msg_a->data_len = 32; |
| 1085 | msg_b->data_len = 32; |
| 1086 | msgb_reset(msg_a); |
| 1087 | msgb_reset(msg_b); |
| 1088 | |
| 1089 | if (a->id_discr != b->id_discr) |
| 1090 | return false; |
| 1091 | if (ai >= a->id_list_len |
| 1092 | || bi >= b->id_list_len) |
| 1093 | return false; |
| 1094 | |
| 1095 | tmp.id_list[0] = a->id_list[ai]; |
| 1096 | gsm0808_enc_cell_id_list2(msg_a, &tmp); |
| 1097 | |
| 1098 | tmp.id_list[0] = b->id_list[bi]; |
| 1099 | gsm0808_enc_cell_id_list2(msg_b, &tmp); |
| 1100 | |
| 1101 | if (msg_a->len != msg_b->len) |
| 1102 | return false; |
| 1103 | if (memcmp(msg_a->data, msg_b->data, msg_a->len)) |
| 1104 | return false; |
| 1105 | |
| 1106 | return true; |
| 1107 | } |
| 1108 | |
| 1109 | /*! Append entries from one Cell Identifier List to another. |
| 1110 | * The cell identifier types must be identical between the two lists. |
| 1111 | * \param dst[out] Append entries to this list. |
| 1112 | * \param src[in] Append these entries to \a dst. |
| 1113 | * \returns the nr of items added, or negative on error: -EINVAL if the id_discr mismatch |
| 1114 | * between the lists, -ENOSPC if the destination list does not have enough space. If an error is |
| 1115 | * returned, \a dst may have already been changed (particularly on -ENOSPC). Note that a return value |
| 1116 | * of zero may occur when the src->id_list_len is zero, or when all entries from \a src already exist |
| 1117 | * in \a dst, and does not indicate error per se. */ |
| 1118 | int gsm0808_cell_id_list_add(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id_list2 *src) |
| 1119 | { |
| 1120 | int i, j; |
| 1121 | int added = 0; |
| 1122 | |
| 1123 | if (dst->id_list_len == 0 |
| 1124 | && dst->id_discr != CELL_IDENT_BSS) |
| 1125 | dst->id_discr = src->id_discr; |
| 1126 | else if (dst->id_discr != src->id_discr) |
| 1127 | return -EINVAL; |
| 1128 | |
| 1129 | for (i = 0; i < src->id_list_len; i++) { |
| 1130 | /* don't add duplicate entries */ |
| 1131 | bool skip = false; |
| 1132 | for (j = 0; j < dst->id_list_len; j++) { |
| 1133 | if (same_cell_id_list_entries(dst, j, src, i)) { |
| 1134 | skip = true; |
| 1135 | break; |
| 1136 | } |
| 1137 | } |
| 1138 | if (skip) |
| 1139 | continue; |
| 1140 | |
| 1141 | if (dst->id_list_len >= ARRAY_SIZE(dst->id_list)) |
| 1142 | return -ENOSPC; |
| 1143 | |
| 1144 | dst->id_list[dst->id_list_len++] = src->id_list[i]; |
| 1145 | added ++; |
| 1146 | } |
| 1147 | |
| 1148 | return added; |
| 1149 | } |
| 1150 | |
Neels Hofmeyr | 38e5841 | 2018-05-25 16:56:35 +0200 | [diff] [blame] | 1151 | /*! Convert a single Cell Identifier to a Cell Identifier List with one entry. |
| 1152 | * \param dst[out] Overwrite this list. |
| 1153 | * \param src[in] Set \a dst to contain exactly this item. |
| 1154 | */ |
| 1155 | void gsm0808_cell_id_to_list(struct gsm0808_cell_id_list2 *dst, const struct gsm0808_cell_id *src) |
| 1156 | { |
| 1157 | if (!dst) |
| 1158 | return; |
| 1159 | if (!src) { |
| 1160 | *dst = (struct gsm0808_cell_id_list2){ |
| 1161 | .id_discr = CELL_IDENT_NO_CELL, |
| 1162 | }; |
| 1163 | return; |
| 1164 | } |
| 1165 | |
| 1166 | *dst = (struct gsm0808_cell_id_list2){ |
| 1167 | .id_discr = src->id_discr, |
| 1168 | .id_list = { src->id }, |
| 1169 | .id_list_len = 1, |
| 1170 | }; |
| 1171 | |
| 1172 | switch (src->id_discr) { |
| 1173 | case CELL_IDENT_NO_CELL: |
| 1174 | case CELL_IDENT_BSS: |
| 1175 | dst->id_list_len = 0; |
| 1176 | break; |
| 1177 | default: |
| 1178 | break; |
| 1179 | } |
| 1180 | } |
| 1181 | |
Neels Hofmeyr | 250e7f7 | 2018-04-13 03:30:14 +0200 | [diff] [blame] | 1182 | /*! Encode Cell Identifier IE (3GPP TS 48.008 3.2.2.17). |
| 1183 | * \param[out] msg Message Buffer to which IE is to be appended |
| 1184 | * \param[in] ci Cell ID to be encoded |
| 1185 | * \returns number of bytes appended to \a msg */ |
| 1186 | uint8_t gsm0808_enc_cell_id(struct msgb *msg, const struct gsm0808_cell_id *ci) |
| 1187 | { |
| 1188 | uint8_t rc; |
| 1189 | uint8_t *ie_tag; |
| 1190 | struct gsm0808_cell_id_list2 cil = { |
| 1191 | .id_discr = ci->id_discr, |
| 1192 | .id_list = { ci->id }, |
| 1193 | .id_list_len = 1, |
| 1194 | }; |
| 1195 | |
| 1196 | OSMO_ASSERT(msg); |
| 1197 | OSMO_ASSERT(ci); |
| 1198 | |
| 1199 | ie_tag = msg->tail; |
| 1200 | rc = gsm0808_enc_cell_id_list2(msg, &cil); |
| 1201 | |
| 1202 | if (rc <= 0) |
| 1203 | return rc; |
| 1204 | |
| 1205 | *ie_tag = GSM0808_IE_CELL_IDENTIFIER; |
| 1206 | return rc; |
| 1207 | } |
| 1208 | |
| 1209 | /*! Decode Cell Identifier IE (3GPP TS 48.008 3.2.2.17). |
| 1210 | * \param[out] ci Caller-provided memory to store Cell ID. |
| 1211 | * \param[in] elem IE value to be decoded. |
| 1212 | * \param[in] len Length of \a elem in bytes. |
| 1213 | * \returns number of bytes parsed; negative on error */ |
| 1214 | int gsm0808_dec_cell_id(struct gsm0808_cell_id *ci, const uint8_t *elem, uint8_t len) |
| 1215 | { |
| 1216 | struct gsm0808_cell_id_list2 cil; |
| 1217 | int rc; |
| 1218 | rc = gsm0808_dec_cell_id_list2(&cil, elem, len); |
| 1219 | if (rc < 0) |
| 1220 | return rc; |
| 1221 | if (cil.id_discr == CELL_IDENT_BSS || cil.id_discr == CELL_IDENT_NO_CELL) { |
| 1222 | if (cil.id_list_len != 0) |
| 1223 | return -EINVAL; |
| 1224 | } else { |
| 1225 | if (cil.id_list_len != 1) |
| 1226 | return -EINVAL; |
| 1227 | } |
| 1228 | ci->id_discr = cil.id_discr; |
| 1229 | ci->id = cil.id_list[0]; |
| 1230 | return rc; |
| 1231 | } |
| 1232 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1233 | /*! Convert the representation of the permitted speech codec identifier |
Philipp Maier | 3149b0d | 2017-06-02 13:22:34 +0200 | [diff] [blame] | 1234 | * that is used in struct gsm0808_channel_type to the speech codec |
| 1235 | * representation we use in struct gsm0808_speech_codec. |
| 1236 | * \param[in] perm_spch to be converted (see also gsm0808_permitted_speech) |
| 1237 | * \returns GSM speech codec type; negative on error */ |
| 1238 | int gsm0808_chan_type_to_speech_codec(uint8_t perm_spch) |
| 1239 | { |
| 1240 | /*! The speech codec type, which is used in the channel type field to |
| 1241 | * signal the permitted speech versions (codecs) has a different |
| 1242 | * encoding than the type field in the speech codec type element |
| 1243 | * (See also 3GPP TS 48.008, 3.2.2.11 and 3.2.2.103) */ |
| 1244 | |
| 1245 | switch (perm_spch) { |
| 1246 | case GSM0808_PERM_FR1: |
| 1247 | return GSM0808_SCT_FR1; |
| 1248 | case GSM0808_PERM_FR2: |
| 1249 | return GSM0808_SCT_FR2; |
| 1250 | case GSM0808_PERM_FR3: |
| 1251 | return GSM0808_SCT_FR3; |
| 1252 | case GSM0808_PERM_FR4: |
| 1253 | return GSM0808_SCT_FR4; |
| 1254 | case GSM0808_PERM_FR5: |
| 1255 | return GSM0808_SCT_FR5; |
| 1256 | case GSM0808_PERM_HR1: |
| 1257 | return GSM0808_SCT_HR1; |
| 1258 | case GSM0808_PERM_HR3: |
| 1259 | return GSM0808_SCT_HR3; |
| 1260 | case GSM0808_PERM_HR4: |
| 1261 | return GSM0808_SCT_HR4; |
| 1262 | case GSM0808_PERM_HR6: |
| 1263 | return GSM0808_SCT_HR6; |
| 1264 | } |
| 1265 | |
| 1266 | /* Invalid input */ |
| 1267 | return -EINVAL; |
| 1268 | } |
| 1269 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1270 | /*! Extrapolate a speech codec field from a given permitted speech |
Philipp Maier | 884ba0f | 2017-06-02 13:49:16 +0200 | [diff] [blame] | 1271 | * parameter (channel type). |
| 1272 | * \param[out] sc Caller provided memory to store the resulting speech codec |
| 1273 | * \param[in] perm_spch value that is used to derive the speech codec info |
| 1274 | * (see also: enum gsm0808_speech_codec_type in gsm0808_utils.h) |
| 1275 | * \returns zero when successful; negative on error */ |
| 1276 | int gsm0808_speech_codec_from_chan_type(struct gsm0808_speech_codec *sc, |
| 1277 | uint8_t perm_spch) |
| 1278 | { |
| 1279 | int rc; |
| 1280 | |
| 1281 | memset(sc, 0, sizeof(*sc)); |
| 1282 | |
| 1283 | /* Determine codec type */ |
| 1284 | rc = gsm0808_chan_type_to_speech_codec(perm_spch); |
| 1285 | if (rc < 0) |
| 1286 | return -EINVAL; |
| 1287 | sc->type = (uint8_t) rc; |
| 1288 | |
| 1289 | /* Depending on the speech codec type, pick a default codec |
| 1290 | * configuration that exactly matches the configuration on the |
| 1291 | * air interface. */ |
| 1292 | switch (sc->type) { |
| 1293 | case GSM0808_SCT_FR3: |
| 1294 | sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR; |
| 1295 | break; |
| 1296 | case GSM0808_SCT_FR4: |
| 1297 | sc->cfg = GSM0808_SC_CFG_DEFAULT_OFR_AMR_WB; |
| 1298 | break; |
| 1299 | case GSM0808_SCT_FR5: |
| 1300 | sc->cfg = GSM0808_SC_CFG_DEFAULT_FR_AMR_WB; |
| 1301 | break; |
| 1302 | case GSM0808_SCT_HR3: |
| 1303 | sc->cfg = GSM0808_SC_CFG_DEFAULT_HR_AMR; |
| 1304 | break; |
| 1305 | case GSM0808_SCT_HR4: |
| 1306 | sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR_WB; |
| 1307 | break; |
| 1308 | case GSM0808_SCT_HR6: |
| 1309 | sc->cfg = GSM0808_SC_CFG_DEFAULT_OHR_AMR; |
| 1310 | break; |
| 1311 | default: |
| 1312 | /* Note: Not all codec types specify a default setting, |
| 1313 | * in this case, we just set the field to zero. */ |
| 1314 | sc->cfg = 0; |
| 1315 | } |
| 1316 | |
| 1317 | /* Tag all codecs as "Full IP" |
| 1318 | * (see als 3GPP TS 48.008 3.2.2.103) */ |
| 1319 | sc->fi = true; |
| 1320 | |
| 1321 | return 0; |
| 1322 | } |
| 1323 | |
Philipp Maier | 5f2eb15 | 2018-09-19 13:40:21 +0200 | [diff] [blame] | 1324 | /*! Determine a set of AMR speech codec configuration bits (S0-S15) from a |
| 1325 | * given GSM 04.08 AMR configuration struct. |
| 1326 | * \param[in] cfg AMR configuration in GSM 04.08 format. |
| 1327 | * \param[in] hint if the resulting configuration shall be used with a FR or HR TCH. |
| 1328 | * \returns configuration bits (S0-S15) */ |
Philipp Maier | 369015c | 2018-09-21 09:07:20 +0200 | [diff] [blame] | 1329 | uint16_t gsm0808_sc_cfg_from_gsm48_mr_cfg(const struct gsm48_multi_rate_conf *cfg, |
Philipp Maier | 5f2eb15 | 2018-09-19 13:40:21 +0200 | [diff] [blame] | 1330 | bool fr) |
| 1331 | { |
| 1332 | uint16_t s15_s0 = 0; |
| 1333 | |
| 1334 | /* Check each rate bit in the AMR multirate configuration and pick the |
| 1335 | * matching default configuration as specified in 3GPP TS 28.062, |
| 1336 | * Table 7.11.3.1.3-2. */ |
| 1337 | if (cfg->m4_75) |
| 1338 | s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_4_75; |
| 1339 | if (cfg->m5_15) |
| 1340 | s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_15; |
| 1341 | if (cfg->m5_90) |
| 1342 | s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_5_90; |
| 1343 | if (cfg->m6_70) |
| 1344 | s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_6_70; |
| 1345 | if (cfg->m7_40) |
| 1346 | s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_40; |
| 1347 | if (cfg->m7_95) |
| 1348 | s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_7_95; |
| 1349 | if (cfg->m10_2) |
| 1350 | s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_10_2; |
| 1351 | if (cfg->m12_2) |
| 1352 | s15_s0 |= GSM0808_SC_CFG_DEFAULT_AMR_12_2; |
| 1353 | |
| 1354 | /* Note: 3GPP TS 48.008, chapter 3GPP TS 48.008 states that for AMR |
| 1355 | * some of the configuration bits must be coded as zeros. The applied |
| 1356 | * bitmask matches the default codec settings. See also the definition |
| 1357 | * of enum gsm0808_speech_codec_defaults in gsm_08_08.h and |
| 1358 | * 3GPP TS 28.062, Table 7.11.3.1.3-2. */ |
| 1359 | if (fr) |
| 1360 | s15_s0 &= GSM0808_SC_CFG_DEFAULT_FR_AMR; |
| 1361 | else |
| 1362 | s15_s0 &= GSM0808_SC_CFG_DEFAULT_HR_AMR; |
| 1363 | |
Philipp Maier | 94d79fd | 2019-03-01 10:40:48 +0100 | [diff] [blame] | 1364 | /* The mode that is encoded by S1 (Config-NB-Code = 1), takes a special |
| 1365 | * role as it does not stand for a single rate, but for up to four rates |
| 1366 | * at once (12.2, 7.4, 5.9, 4.75). We must check if the supplied cfg |
| 1367 | * covers this mode. If not, we need to make sure that the related |
| 1368 | * bit is removed. (See also 3GPP TS 28.062, Table 7.11.3.1.3-2) */ |
| 1369 | if (!(cfg->m12_2 && cfg->m7_40 && cfg->m5_90 && cfg->m4_75) && fr) |
| 1370 | s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20; |
| 1371 | else if (!(cfg->m7_40 && cfg->m5_90 && cfg->m4_75)) |
| 1372 | s15_s0 &= ~GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20; |
| 1373 | |
Philipp Maier | 5f2eb15 | 2018-09-19 13:40:21 +0200 | [diff] [blame] | 1374 | return s15_s0; |
| 1375 | } |
| 1376 | |
Philipp Maier | 8515d03 | 2018-09-25 15:57:49 +0200 | [diff] [blame] | 1377 | /*! Determine a GSM 04.08 AMR configuration struct from a set of speech codec |
| 1378 | * configuration bits (S0-S15) |
| 1379 | * \param[out] cfg AMR configuration in GSM 04.08 format. |
Philipp Maier | 3713af8 | 2019-02-27 16:48:25 +0100 | [diff] [blame] | 1380 | * \param[in] s15_s0 configuration bits (S15-S0, non-ambiguous). |
| 1381 | * \returns zero when successful; negative on error */ |
| 1382 | int gsm48_mr_cfg_from_gsm0808_sc_cfg(struct gsm48_multi_rate_conf *cfg, |
| 1383 | uint16_t s15_s0) |
Philipp Maier | 8515d03 | 2018-09-25 15:57:49 +0200 | [diff] [blame] | 1384 | { |
Philipp Maier | 3713af8 | 2019-02-27 16:48:25 +0100 | [diff] [blame] | 1385 | unsigned int count = 0; |
| 1386 | |
| 1387 | /* Note: See also: 3GPP TS 28.062 |
| 1388 | * Table 7.11.3.1.3-2: Preferred Configurations for the Adaptive |
| 1389 | * Multi-Rate Codec Types */ |
| 1390 | |
| 1391 | /* Note: The resulting multirate-configuration must not contain an |
| 1392 | * active set of more than four codec rates. The active set also |
| 1393 | * must contain at least one rate. */ |
| 1394 | |
Philipp Maier | 8515d03 | 2018-09-25 15:57:49 +0200 | [diff] [blame] | 1395 | memset(cfg, 0, sizeof(*cfg)); |
Philipp Maier | 3713af8 | 2019-02-27 16:48:25 +0100 | [diff] [blame] | 1396 | cfg->ver = 1; |
| 1397 | cfg->icmi = 1; |
Philipp Maier | 8515d03 | 2018-09-25 15:57:49 +0200 | [diff] [blame] | 1398 | |
| 1399 | /* Strip option bits */ |
| 1400 | s15_s0 &= 0x00ff; |
| 1401 | |
Philipp Maier | 3713af8 | 2019-02-27 16:48:25 +0100 | [diff] [blame] | 1402 | /* Rate 5,15k can never be selected (see table) */ |
| 1403 | cfg->m5_15 = 0; |
Philipp Maier | 8515d03 | 2018-09-25 15:57:49 +0200 | [diff] [blame] | 1404 | |
Philipp Maier | 3713af8 | 2019-02-27 16:48:25 +0100 | [diff] [blame] | 1405 | if (s15_s0 & GSM0808_SC_CFG_AMR_4_75_5_90_7_40_12_20 & 0xff) { |
| 1406 | /* Table Table 7.11.3.1.3-2 lists one mode that selects 4 |
| 1407 | * rates at once (Config-NB-Code = 1). The rates selected |
| 1408 | * are known to be compatible between GERAN and UTRAN, since |
| 1409 | * an active set must not contain more than four rates at |
| 1410 | * a time, we ignore all other settings as they are either |
| 1411 | * redundaned or excess settings (invalid) */ |
Philipp Maier | 8515d03 | 2018-09-25 15:57:49 +0200 | [diff] [blame] | 1412 | cfg->m4_75 = 1; |
Philipp Maier | 8515d03 | 2018-09-25 15:57:49 +0200 | [diff] [blame] | 1413 | cfg->m5_90 = 1; |
Philipp Maier | 8515d03 | 2018-09-25 15:57:49 +0200 | [diff] [blame] | 1414 | cfg->m7_40 = 1; |
Philipp Maier | 8515d03 | 2018-09-25 15:57:49 +0200 | [diff] [blame] | 1415 | cfg->m12_2 = 1; |
Philipp Maier | 3713af8 | 2019-02-27 16:48:25 +0100 | [diff] [blame] | 1416 | count += 4; |
| 1417 | } |
Philipp Maier | 8515d03 | 2018-09-25 15:57:49 +0200 | [diff] [blame] | 1418 | |
Philipp Maier | 3713af8 | 2019-02-27 16:48:25 +0100 | [diff] [blame] | 1419 | /* Check the bits in s15_s0 and set the flags for the |
| 1420 | * respective rates. */ |
| 1421 | if (s15_s0 & GSM0808_SC_CFG_AMR_4_75 && !cfg->m4_75) { |
| 1422 | if (count >= 4) |
| 1423 | return -EINVAL; |
| 1424 | cfg->m4_75 = 1; |
| 1425 | count++; |
| 1426 | } |
| 1427 | if (s15_s0 & GSM0808_SC_CFG_AMR_5_90 && !cfg->m5_90) { |
| 1428 | if (count >= 4) |
| 1429 | return -EINVAL; |
| 1430 | cfg->m5_90 = 1; |
| 1431 | count++; |
| 1432 | } |
| 1433 | if (s15_s0 & GSM0808_SC_CFG_AMR_6_70) { |
| 1434 | if (count >= 4) |
| 1435 | return -EINVAL; |
| 1436 | cfg->m6_70 = 1; |
| 1437 | count++; |
| 1438 | } |
| 1439 | if (s15_s0 & GSM0808_SC_CFG_AMR_7_40 && !cfg->m7_40) { |
| 1440 | if (count >= 4) |
| 1441 | return -EINVAL; |
| 1442 | cfg->m7_40 = 1; |
| 1443 | count++; |
| 1444 | } |
| 1445 | if (s15_s0 & GSM0808_SC_CFG_AMR_7_95) { |
| 1446 | if (count >= 4) |
| 1447 | return -EINVAL; |
| 1448 | cfg->m7_95 = 1; |
| 1449 | count++; |
| 1450 | } |
| 1451 | if (s15_s0 & GSM0808_SC_CFG_AMR_10_2) { |
| 1452 | if (count >= 4) |
| 1453 | return -EINVAL; |
| 1454 | cfg->m10_2 = 1; |
| 1455 | count++; |
| 1456 | } |
| 1457 | if (s15_s0 & GSM0808_SC_CFG_AMR_12_2 && !cfg->m12_2) { |
| 1458 | if (count >= 4) |
| 1459 | return -EINVAL; |
| 1460 | cfg->m12_2 = 1; |
| 1461 | count++; |
| 1462 | } |
| 1463 | |
| 1464 | if (count == 0) |
| 1465 | return -EINVAL; |
| 1466 | |
| 1467 | return 0; |
Philipp Maier | 8515d03 | 2018-09-25 15:57:49 +0200 | [diff] [blame] | 1468 | } |
| 1469 | |
Max | ed651d2 | 2018-11-07 15:25:05 +0100 | [diff] [blame] | 1470 | int gsm0808_get_cipher_reject_cause(const struct tlv_parsed *tp) |
| 1471 | { |
| 1472 | const uint8_t *buf = TLVP_VAL_MINLEN(tp, GSM0808_IE_CAUSE, 1); |
| 1473 | |
| 1474 | if (!buf) |
| 1475 | return -EBADMSG; |
| 1476 | |
| 1477 | if (TLVP_LEN(tp, GSM0808_IE_CAUSE) > 1) { |
| 1478 | if (!gsm0808_cause_ext(buf[0])) |
| 1479 | return -EINVAL; |
| 1480 | return buf[1]; |
| 1481 | } |
| 1482 | |
| 1483 | return buf[0]; |
| 1484 | } |
| 1485 | |
Neels Hofmeyr | a4399c8 | 2018-04-17 02:26:10 +0200 | [diff] [blame] | 1486 | /*! Print a human readable name of the cell identifier to the char buffer. |
| 1487 | * This is useful both for struct gsm0808_cell_id and struct gsm0808_cell_id_list2. |
| 1488 | * See also gsm0808_cell_id_name() and gsm0808_cell_id_list_name(). |
| 1489 | * \param[out] buf Destination buffer to write string representation to. |
| 1490 | * \param[in] buflen Amount of memory available in \a buf. |
| 1491 | * \param[in] id_discr Cell Identifier type. |
| 1492 | * \param[in] u Cell Identifer value. |
| 1493 | * \returns Like snprintf(): the amount of characters (excluding terminating nul) written, |
| 1494 | * or that would have been written if the buffer were large enough. |
| 1495 | */ |
| 1496 | int gsm0808_cell_id_u_name(char *buf, size_t buflen, |
| 1497 | enum CELL_IDENT id_discr, const union gsm0808_cell_id_u *u) |
| 1498 | { |
| 1499 | switch (id_discr) { |
| 1500 | case CELL_IDENT_LAC: |
| 1501 | return snprintf(buf, buflen, "%u", u->lac); |
| 1502 | case CELL_IDENT_CI: |
| 1503 | return snprintf(buf, buflen, "%u", u->ci); |
| 1504 | case CELL_IDENT_LAC_AND_CI: |
| 1505 | return snprintf(buf, buflen, "%u-%u", u->lac_and_ci.lac, u->lac_and_ci.ci); |
| 1506 | case CELL_IDENT_LAI_AND_LAC: |
| 1507 | return snprintf(buf, buflen, "%s", osmo_lai_name(&u->lai_and_lac)); |
| 1508 | case CELL_IDENT_WHOLE_GLOBAL: |
| 1509 | return snprintf(buf, buflen, "%s", osmo_cgi_name(&u->global)); |
| 1510 | default: |
| 1511 | /* For CELL_IDENT_BSS and CELL_IDENT_NO_CELL, just print the discriminator. |
| 1512 | * Same for kinds we have no string representation of yet. */ |
| 1513 | return snprintf(buf, buflen, "%s", gsm0808_cell_id_discr_name(id_discr)); |
| 1514 | } |
| 1515 | } |
| 1516 | |
Neels Hofmeyr | d01ef75 | 2018-09-21 15:57:26 +0200 | [diff] [blame] | 1517 | /* Store individual Cell Identifier information in a CGI, without clearing the remaining ones. |
| 1518 | * This is useful to supplement one CGI with information from more than one Cell Identifier, |
| 1519 | * which in turn is useful to match Cell Identifiers of differing kinds to each other. |
| 1520 | * Before first invocation, clear the *dst struct externally, this function does only write those members |
| 1521 | * that are present in parameter u. |
| 1522 | */ |
| 1523 | static void cell_id_to_cgi(struct osmo_cell_global_id *dst, |
| 1524 | enum CELL_IDENT discr, const union gsm0808_cell_id_u *u) |
| 1525 | { |
| 1526 | switch (discr) { |
| 1527 | case CELL_IDENT_WHOLE_GLOBAL: |
| 1528 | *dst = u->global; |
| 1529 | return; |
| 1530 | |
| 1531 | case CELL_IDENT_LAC_AND_CI: |
| 1532 | dst->lai.lac = u->lac_and_ci.lac; |
| 1533 | dst->cell_identity = u->lac_and_ci.ci; |
| 1534 | return; |
| 1535 | |
| 1536 | case CELL_IDENT_CI: |
| 1537 | dst->cell_identity = u->ci; |
| 1538 | return; |
| 1539 | |
| 1540 | case CELL_IDENT_LAI_AND_LAC: |
| 1541 | dst->lai = u->lai_and_lac; |
| 1542 | return; |
| 1543 | |
| 1544 | case CELL_IDENT_LAC: |
| 1545 | dst->lai.lac = u->lac; |
| 1546 | return; |
| 1547 | |
| 1548 | case CELL_IDENT_NO_CELL: |
| 1549 | case CELL_IDENT_BSS: |
| 1550 | case CELL_IDENT_UTRAN_PLMN_LAC_RNC: |
| 1551 | case CELL_IDENT_UTRAN_RNC: |
| 1552 | case CELL_IDENT_UTRAN_LAC_RNC: |
| 1553 | /* No values to set. */ |
| 1554 | return; |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | /*! Return true if the common information between the two Cell Identifiers match. |
| 1559 | * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same. |
| 1560 | * Note that CELL_IDENT_NO_CELL will always return false. |
| 1561 | * Also CELL_IDENT_BSS will always return false, since this function cannot possibly |
| 1562 | * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially. |
| 1563 | * \param[in] discr1 Cell Identifier type. |
| 1564 | * \param[in] u1 Cell Identifier value. |
| 1565 | * \param[in] discr2 Other Cell Identifier type. |
| 1566 | * \param[in] u2 Other Cell Identifier value. |
| 1567 | * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical. |
| 1568 | * \returns True if the common fields of the above match. |
| 1569 | */ |
| 1570 | static bool gsm0808_cell_id_u_match(enum CELL_IDENT discr1, const union gsm0808_cell_id_u *u1, |
| 1571 | enum CELL_IDENT discr2, const union gsm0808_cell_id_u *u2, |
| 1572 | bool exact_match) |
| 1573 | { |
| 1574 | struct osmo_cell_global_id a = {}; |
| 1575 | struct osmo_cell_global_id b = {}; |
| 1576 | |
| 1577 | if (exact_match && discr1 != discr2) |
| 1578 | return false; |
| 1579 | |
| 1580 | /* First handle the odd wildcard like CELL_IDENT kinds. We can't really match any of these. */ |
| 1581 | switch (discr1) { |
| 1582 | case CELL_IDENT_NO_CELL: |
| 1583 | case CELL_IDENT_BSS: |
| 1584 | return discr1 == discr2; |
| 1585 | case CELL_IDENT_UTRAN_PLMN_LAC_RNC: |
| 1586 | case CELL_IDENT_UTRAN_RNC: |
| 1587 | case CELL_IDENT_UTRAN_LAC_RNC: |
| 1588 | return false; |
| 1589 | default: |
| 1590 | break; |
| 1591 | } |
| 1592 | switch (discr2) { |
| 1593 | case CELL_IDENT_NO_CELL: |
| 1594 | case CELL_IDENT_UTRAN_PLMN_LAC_RNC: |
| 1595 | case CELL_IDENT_UTRAN_RNC: |
| 1596 | case CELL_IDENT_UTRAN_LAC_RNC: |
| 1597 | case CELL_IDENT_BSS: |
| 1598 | return false; |
| 1599 | default: |
| 1600 | break; |
| 1601 | } |
| 1602 | |
| 1603 | /* Enrich both sides to full CGI, then compare those. First set the *other* ID's values in case |
| 1604 | * they assign more items. For example: |
| 1605 | * u1 = LAC:42 |
| 1606 | * u2 = LAC+CI:23+5 |
| 1607 | * 1) a <- LAC+CI:23+5 |
| 1608 | * 2) a <- LAC:42 so that a = LAC+CI:42+5 |
| 1609 | * Now we can compare those two and find a mismatch. If the LAC were the same, we would get |
| 1610 | * identical LAC+CI and hence a match. */ |
| 1611 | |
| 1612 | cell_id_to_cgi(&a, discr2, u2); |
| 1613 | cell_id_to_cgi(&a, discr1, u1); |
| 1614 | |
| 1615 | cell_id_to_cgi(&b, discr1, u1); |
| 1616 | cell_id_to_cgi(&b, discr2, u2); |
| 1617 | |
| 1618 | return osmo_cgi_cmp(&a, &b) == 0; |
| 1619 | } |
| 1620 | |
| 1621 | /*! Return true if the common information between the two Cell Identifiers match. |
| 1622 | * For example, if a LAC+CI is compared to LAC, return true if the LAC are the same. |
| 1623 | * Note that CELL_IDENT_NO_CELL will always return false. |
| 1624 | * Also CELL_IDENT_BSS will always return false, since this function cannot possibly |
| 1625 | * know the bounds of the BSS, so the caller must handle CELL_IDENT_BSS specially. |
| 1626 | * \param[in] id1 Cell Identifier. |
| 1627 | * \param[in] id2 Other Cell Identifier. |
| 1628 | * \param[in] exact_match If true, return true only if the CELL_IDENT types and all values are identical. |
| 1629 | * \returns True if the common fields of the above match. |
| 1630 | */ |
| 1631 | bool gsm0808_cell_ids_match(const struct gsm0808_cell_id *id1, const struct gsm0808_cell_id *id2, bool exact_match) |
| 1632 | { |
| 1633 | return gsm0808_cell_id_u_match(id1->id_discr, &id1->id, id2->id_discr, &id2->id, exact_match); |
| 1634 | } |
| 1635 | |
| 1636 | /*! Find an index in a Cell Identifier list that matches a given single Cell Identifer. |
| 1637 | * Compare \a id against each entry in \a list using gsm0808_cell_ids_match(), and return the list index |
| 1638 | * if a match is found. \a match_nr allows iterating all matches in the list. A match_nr <= 0 returns the |
| 1639 | * first match in the list, match_nr == 1 the second match, etc., and if match_nr exceeds the available |
| 1640 | * matches in the list, -1 is returned. |
| 1641 | * \param[in] id Cell Identifier to match. |
| 1642 | * \param[in] list Cell Identifier list to search in. |
| 1643 | * \param[in] match_nr Ignore this many matches. |
| 1644 | * \param[in] exact_match If true, consider as match only if the CELL_IDENT types and all values are identical. |
Neels Hofmeyr | 8aa691f | 2019-02-26 02:59:37 +0100 | [diff] [blame] | 1645 | * \returns -1 if no match is found, list index if a match is found (i.e. rc == 0 means a match was found on the first |
| 1646 | * entry). |
Neels Hofmeyr | d01ef75 | 2018-09-21 15:57:26 +0200 | [diff] [blame] | 1647 | */ |
| 1648 | int gsm0808_cell_id_matches_list(const struct gsm0808_cell_id *id, const struct gsm0808_cell_id_list2 *list, |
| 1649 | unsigned int match_nr, bool exact_match) |
| 1650 | { |
| 1651 | int i; |
| 1652 | for (i = 0; i < list->id_list_len; i++) { |
| 1653 | if (gsm0808_cell_id_u_match(id->id_discr, &id->id, list->id_discr, &list->id_list[i], exact_match)) { |
| 1654 | if (match_nr) |
| 1655 | match_nr--; |
| 1656 | else |
| 1657 | return i; |
| 1658 | } |
| 1659 | } |
| 1660 | return -1; |
| 1661 | } |
| 1662 | |
Neels Hofmeyr | 3a50453 | 2019-02-10 22:28:27 +0100 | [diff] [blame] | 1663 | /*! Copy information from a CGI to form a Cell Identifier of the specified kind. |
| 1664 | * \param [out] cid Compose new Cell Identifier here. |
| 1665 | * \param [in] id_discr Which kind of Cell Identifier to compose. |
| 1666 | * \param [in] cgi Cell Global Identifier to form the Cell Identifier from. |
| 1667 | */ |
| 1668 | void gsm0808_cell_id_from_cgi(struct gsm0808_cell_id *cid, enum CELL_IDENT id_discr, |
| 1669 | const struct osmo_cell_global_id *cgi) |
| 1670 | { |
| 1671 | *cid = (struct gsm0808_cell_id){ |
| 1672 | .id_discr = id_discr, |
| 1673 | }; |
| 1674 | |
| 1675 | switch (id_discr) { |
| 1676 | case CELL_IDENT_WHOLE_GLOBAL: |
| 1677 | cid->id.global = *cgi; |
| 1678 | return; |
| 1679 | |
| 1680 | case CELL_IDENT_LAC_AND_CI: |
| 1681 | cid->id.lac_and_ci = (struct osmo_lac_and_ci_id){ |
| 1682 | .lac = cgi->lai.lac, |
| 1683 | .ci = cgi->cell_identity, |
| 1684 | }; |
| 1685 | return; |
| 1686 | |
| 1687 | case CELL_IDENT_CI: |
| 1688 | cid->id.ci = cgi->cell_identity; |
| 1689 | return; |
| 1690 | |
| 1691 | case CELL_IDENT_LAI: |
| 1692 | cid->id.lai_and_lac = cgi->lai; |
| 1693 | return; |
| 1694 | |
| 1695 | case CELL_IDENT_LAC: |
| 1696 | cid->id.lac = cgi->lai.lac; |
| 1697 | return; |
| 1698 | |
| 1699 | case CELL_IDENT_NO_CELL: |
| 1700 | case CELL_IDENT_BSS: |
| 1701 | case CELL_IDENT_UTRAN_PLMN_LAC_RNC: |
| 1702 | case CELL_IDENT_UTRAN_RNC: |
| 1703 | case CELL_IDENT_UTRAN_LAC_RNC: |
| 1704 | default: |
| 1705 | return; |
| 1706 | }; |
| 1707 | } |
| 1708 | |
| 1709 | /*! Overwrite parts of cgi with values from a Cell Identifier. |
| 1710 | * Place only those items given in cid into cgi, leaving other values unchanged. |
| 1711 | * \param[out] cgi Cell Global Identity to write to. |
| 1712 | * \param[in] cid Cell Identity to read from. |
| 1713 | * \return a bitmask of items that were set: OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI; 0 if nothing was |
| 1714 | * written to cgi. |
| 1715 | */ |
| 1716 | int gsm0808_cell_id_to_cgi(struct osmo_cell_global_id *cgi, const struct gsm0808_cell_id *cid) |
| 1717 | { |
| 1718 | switch (cid->id_discr) { |
| 1719 | case CELL_IDENT_WHOLE_GLOBAL: |
| 1720 | *cgi = cid->id.global; |
| 1721 | return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI; |
| 1722 | |
| 1723 | case CELL_IDENT_LAC_AND_CI: |
| 1724 | cgi->lai.lac = cid->id.lac_and_ci.lac; |
| 1725 | cgi->cell_identity = cid->id.lac_and_ci.ci; |
| 1726 | return OSMO_CGI_PART_LAC | OSMO_CGI_PART_CI; |
| 1727 | |
| 1728 | case CELL_IDENT_CI: |
| 1729 | cgi->cell_identity = cid->id.ci; |
| 1730 | return OSMO_CGI_PART_CI; |
| 1731 | |
| 1732 | case CELL_IDENT_LAI: |
| 1733 | cgi->lai = cid->id.lai_and_lac; |
| 1734 | return OSMO_CGI_PART_PLMN | OSMO_CGI_PART_LAC; |
| 1735 | |
| 1736 | case CELL_IDENT_LAC: |
| 1737 | cgi->lai.lac = cid->id.lac; |
| 1738 | return OSMO_CGI_PART_LAC; |
| 1739 | |
| 1740 | case CELL_IDENT_NO_CELL: |
| 1741 | case CELL_IDENT_BSS: |
| 1742 | case CELL_IDENT_UTRAN_PLMN_LAC_RNC: |
| 1743 | case CELL_IDENT_UTRAN_RNC: |
| 1744 | case CELL_IDENT_UTRAN_LAC_RNC: |
| 1745 | default: |
| 1746 | return 0; |
| 1747 | }; |
| 1748 | } |
| 1749 | |
Neels Hofmeyr | a4399c8 | 2018-04-17 02:26:10 +0200 | [diff] [blame] | 1750 | /*! value_string[] for enum CELL_IDENT. */ |
| 1751 | const struct value_string gsm0808_cell_id_discr_names[] = { |
| 1752 | { CELL_IDENT_WHOLE_GLOBAL, "CGI" }, |
| 1753 | { CELL_IDENT_LAC_AND_CI, "LAC-CI" }, |
| 1754 | { CELL_IDENT_CI, "CI" }, |
| 1755 | { CELL_IDENT_NO_CELL, "NO-CELL" }, |
| 1756 | { CELL_IDENT_LAI_AND_LAC, "LAI" }, |
| 1757 | { CELL_IDENT_LAC, "LAC" }, |
| 1758 | { CELL_IDENT_BSS, "BSS" }, |
| 1759 | { CELL_IDENT_UTRAN_PLMN_LAC_RNC, "UTRAN-PLMN-LAC-RNC" }, |
| 1760 | { CELL_IDENT_UTRAN_RNC, "UTRAN-RNC" }, |
| 1761 | { CELL_IDENT_UTRAN_LAC_RNC, "UTRAN-LAC-RNC" }, |
| 1762 | { 0, NULL } |
| 1763 | }; |
| 1764 | |
| 1765 | #define APPEND_THING(func, args...) do { \ |
| 1766 | int remain = buflen - (pos - buf); \ |
| 1767 | int l = func(pos, remain, ##args); \ |
| 1768 | if (l < 0 || l > remain) \ |
| 1769 | pos = buf + buflen; \ |
| 1770 | else \ |
| 1771 | pos += l; \ |
| 1772 | if (l > 0) \ |
| 1773 | total_len += l; \ |
| 1774 | } while(0) |
| 1775 | #define APPEND_STR(fmt, args...) APPEND_THING(snprintf, fmt, ##args) |
| 1776 | #define APPEND_CELL_ID_U(DISCR, U) APPEND_THING(gsm0808_cell_id_u_name, DISCR, U) |
| 1777 | |
| 1778 | static const char *gsm0808_cell_id_name_buf(const struct gsm0808_cell_id *cid, |
| 1779 | char *buf, size_t buflen) |
| 1780 | { |
| 1781 | char *pos = buf; |
| 1782 | int total_len = 0; |
| 1783 | APPEND_STR("%s:", gsm0808_cell_id_discr_name(cid->id_discr)); |
| 1784 | APPEND_CELL_ID_U(cid->id_discr, &cid->id); |
| 1785 | return buf; |
| 1786 | } |
| 1787 | |
| 1788 | /*! Return a human readable representation of a Cell Identifier, like "LAC:123" |
| 1789 | * or "CGI:001-01-42-23". |
| 1790 | * \param[in] cid Cell Identifer. |
| 1791 | * \returns String in a static buffer. |
| 1792 | */ |
| 1793 | const char *gsm0808_cell_id_name(const struct gsm0808_cell_id *cid) |
| 1794 | { |
| 1795 | static char buf[64]; |
| 1796 | return gsm0808_cell_id_name_buf(cid, buf, sizeof(buf)); |
| 1797 | } |
| 1798 | |
| 1799 | /*! Like gsm0808_cell_id_name() but uses a different static buffer. |
| 1800 | * \param[in] cid Cell Identifer. |
| 1801 | * \returns String in a static buffer. |
| 1802 | */ |
| 1803 | const char *gsm0808_cell_id_name2(const struct gsm0808_cell_id *cid) |
| 1804 | { |
| 1805 | static char buf[64]; |
| 1806 | return gsm0808_cell_id_name_buf(cid, buf, sizeof(buf)); |
| 1807 | } |
| 1808 | |
| 1809 | /*! Return a human readable representation of the Cell Identifier List, like |
| 1810 | * "LAC[2]:{123, 456}". |
| 1811 | * The return value semantics are like snprintf() and thus allow ensuring a complete |
| 1812 | * untruncated string by determining the required string length from the return value. |
| 1813 | * If buflen > 0, always nul-terminate the string in buf, also when it is truncated. |
| 1814 | * If buflen == 0, do not modify buf, just return the would-be length. |
| 1815 | * \param[out] buf Destination buffer to write string representation to. |
| 1816 | * \param[in] buflen Amount of memory available in \a buf. |
| 1817 | * \param[in] cil Cell Identifer List. |
| 1818 | * \returns Like snprintf(): the amount of characters (excluding terminating nul) written, |
| 1819 | * or that would have been written if the buffer were large enough. |
| 1820 | */ |
| 1821 | int gsm0808_cell_id_list_name_buf(char *buf, size_t buflen, const struct gsm0808_cell_id_list2 *cil) |
| 1822 | { |
| 1823 | char *pos = buf; |
| 1824 | int total_len = 0; |
| 1825 | int i; |
| 1826 | |
| 1827 | APPEND_STR("%s[%u]", gsm0808_cell_id_discr_name(cil->id_discr), cil->id_list_len); |
| 1828 | |
| 1829 | switch (cil->id_discr) { |
| 1830 | case CELL_IDENT_BSS: |
| 1831 | case CELL_IDENT_NO_CELL: |
| 1832 | return total_len; |
| 1833 | default: |
| 1834 | break; |
| 1835 | } |
| 1836 | |
| 1837 | APPEND_STR(":{"); |
| 1838 | |
| 1839 | for (i = 0; i < cil->id_list_len; i++) { |
| 1840 | if (i) |
| 1841 | APPEND_STR(", "); |
| 1842 | APPEND_CELL_ID_U(cil->id_discr, &cil->id_list[i]); |
| 1843 | } |
| 1844 | |
| 1845 | APPEND_STR("}"); |
| 1846 | return total_len; |
| 1847 | } |
| 1848 | |
| 1849 | /*! Return a human-readable representation of \a cil in a static buffer. |
| 1850 | * If the list is too long, the output may be truncated. |
| 1851 | * See also gsm0808_cell_id_list_name_buf(). */ |
| 1852 | const char *gsm0808_cell_id_list_name(const struct gsm0808_cell_id_list2 *cil) |
| 1853 | { |
| 1854 | static char buf[1024]; |
| 1855 | gsm0808_cell_id_list_name_buf(buf, sizeof(buf), cil); |
| 1856 | return buf; |
| 1857 | } |
| 1858 | |
| 1859 | #undef APPEND_STR |
| 1860 | #undef APPEND_CELL_ID_U |
| 1861 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame^] | 1862 | char *gsm0808_channel_type_name_buf(char *buf, size_t buf_len, const struct gsm0808_channel_type *ct) |
Neels Hofmeyr | afacc2b | 2018-04-16 22:41:51 +0200 | [diff] [blame] | 1863 | { |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame^] | 1864 | snprintf(buf, buf_len, "ch_indctr=0x%x ch_rate_type=0x%x perm_spch=%s", |
Neels Hofmeyr | afacc2b | 2018-04-16 22:41:51 +0200 | [diff] [blame] | 1865 | ct->ch_indctr, ct->ch_rate_type, |
| 1866 | osmo_hexdump(ct->perm_spch, ct->perm_spch_len)); |
| 1867 | return buf; |
| 1868 | } |
| 1869 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame^] | 1870 | const char *gsm0808_channel_type_name(const struct gsm0808_channel_type *ct) |
| 1871 | { |
| 1872 | static char buf[128]; |
| 1873 | return gsm0808_channel_type_name_buf(buf, sizeof(buf), ct); |
| 1874 | } |
| 1875 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 1876 | /*! @} */ |