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