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