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