Philipp Maier | 9828d28 | 2021-01-06 20:40:23 +0100 | [diff] [blame] | 1 | /*! \file gprs_bssgp.c |
| 2 | * GPRS BSSGP RIM protocol implementation as per 3GPP TS 48.018. */ |
| 3 | /* |
| 4 | * (C) 2020-2021 by sysmocom - s.f.m.c. GmbH |
| 5 | * Author: Philipp Maier <pmaier@sysmocom.de> |
| 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <errno.h> |
| 27 | #include <osmocom/gprs/gprs_bssgp.h> |
| 28 | #include <osmocom/gprs/gprs_bssgp_rim.h> |
| 29 | #include <osmocom/gsm/gsm0808_utils.h> |
| 30 | |
| 31 | /* TVLV IEs use a variable length field. To be sure we will do all buffer |
| 32 | * length checks with the maximum possible header length, which is |
| 33 | * 1 octet tag + 2 octets length = 3 */ |
| 34 | #define TVLV_HDR_MAXLEN 3 |
| 35 | |
| 36 | /* Usually RIM application containers and their surrounding RIM containers |
| 37 | * are not likely to exceed 128 octets, so the usual header length will be 2 */ |
| 38 | #define TVLV_HDR_LEN 2 |
| 39 | |
| 40 | /* The reporting cell identifier is encoded as a cell identifier IE |
| 41 | * (3GPP TS 48.018, sub-clause 11.3.9) but without IE and length octets. */ |
| 42 | #define REP_CELL_ID_LEN 8 |
| 43 | |
Philipp Maier | 7450f77 | 2021-01-06 20:56:43 +0100 | [diff] [blame] | 44 | /*! Parse a RIM Routing information IE (3GPP TS 48.018, chapter 11.3.70). |
| 45 | * \param[out] ri user provided memory to store the parsed results. |
| 46 | * \param[in] buf input buffer of the value part of the IE. |
| 47 | * \returns length of parsed octets, -EINVAL on error. */ |
| 48 | int bssgp_parse_rim_ri(struct bssgp_rim_routing_info *ri, const uint8_t *buf, |
| 49 | unsigned int len) |
| 50 | { |
| 51 | struct gprs_ra_id raid_temp; |
| 52 | |
| 53 | memset(ri, 0, sizeof(*ri)); |
| 54 | if (len < 2) |
| 55 | return -EINVAL; |
| 56 | |
| 57 | ri->discr = buf[0] & 0x0f; |
| 58 | buf++; |
| 59 | |
| 60 | switch (ri->discr) { |
| 61 | case BSSGP_RIM_ROUTING_INFO_GERAN: |
| 62 | if (len < 9) |
| 63 | return -EINVAL; |
| 64 | ri->geran.cid = bssgp_parse_cell_id(&ri->geran.raid, buf); |
| 65 | return 9; |
| 66 | case BSSGP_RIM_ROUTING_INFO_UTRAN: |
| 67 | if (len < 9) |
| 68 | return -EINVAL; |
| 69 | gsm48_parse_ra(&ri->utran.raid, buf); |
| 70 | ri->utran.rncid = osmo_load16be(buf + 6); |
| 71 | return 9; |
| 72 | case BSSGP_RIM_ROUTING_INFO_EUTRAN: |
| 73 | if (len < 7 || len > 14) |
| 74 | return -EINVAL; |
| 75 | /* Note: 3GPP TS 24.301 Figure 9.9.3.32.1 and 3GPP TS 24.008 |
| 76 | * Figure 10.5.130 specify MCC/MNC encoding in the same way, |
| 77 | * so we can re-use gsm48_parse_ra() for that. */ |
| 78 | gsm48_parse_ra(&raid_temp, buf); |
| 79 | ri->eutran.tai.mcc = raid_temp.mcc; |
| 80 | ri->eutran.tai.mnc = raid_temp.mnc; |
| 81 | ri->eutran.tai.mnc_3_digits = raid_temp.mnc_3_digits; |
| 82 | ri->eutran.tai.tac = osmo_load16be(buf + 3); |
| 83 | memcpy(ri->eutran.global_enb_id, buf + 5, len - 6); |
| 84 | ri->eutran.global_enb_id_len = len - 6; |
| 85 | return len; |
| 86 | default: |
| 87 | return -EINVAL; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /*! Encode a RIM Routing information IE (3GPP TS 48.018, chapter 11.3.70). |
| 92 | * \param[out] buf user provided memory (at least 14 byte) for the generated value part of the IE. |
| 93 | * \param[in] ri user provided input data struct. |
| 94 | * \returns length of encoded octets, -EINVAL on error. */ |
| 95 | int bssgp_create_rim_ri(uint8_t *buf, const struct bssgp_rim_routing_info *ri) |
| 96 | { |
| 97 | int rc; |
| 98 | struct gprs_ra_id raid_temp; |
Philipp Maier | 7741bc3 | 2021-01-07 21:55:48 +0100 | [diff] [blame] | 99 | int len; |
Philipp Maier | 7450f77 | 2021-01-06 20:56:43 +0100 | [diff] [blame] | 100 | |
| 101 | buf[0] = ri->discr & 0x0f; |
| 102 | buf++; |
| 103 | |
| 104 | switch (ri->discr) { |
| 105 | case BSSGP_RIM_ROUTING_INFO_GERAN: |
| 106 | rc = bssgp_create_cell_id(buf, &ri->geran.raid, ri->geran.cid); |
| 107 | if (rc < 0) |
| 108 | return -EINVAL; |
Philipp Maier | 7741bc3 | 2021-01-07 21:55:48 +0100 | [diff] [blame] | 109 | len = rc + 1; |
| 110 | break; |
Philipp Maier | 7450f77 | 2021-01-06 20:56:43 +0100 | [diff] [blame] | 111 | case BSSGP_RIM_ROUTING_INFO_UTRAN: |
| 112 | gsm48_encode_ra((struct gsm48_ra_id *)buf, &ri->utran.raid); |
| 113 | osmo_store16be(ri->utran.rncid, buf + 6); |
Philipp Maier | 7741bc3 | 2021-01-07 21:55:48 +0100 | [diff] [blame] | 114 | len = 9; |
| 115 | break; |
Philipp Maier | 7450f77 | 2021-01-06 20:56:43 +0100 | [diff] [blame] | 116 | case BSSGP_RIM_ROUTING_INFO_EUTRAN: |
| 117 | /* Note: 3GPP TS 24.301 Figure 9.9.3.32.1 and 3GPP TS 24.008 |
| 118 | * Figure 10.5.130 specify MCC/MNC encoding in the same way, |
| 119 | * so we can re-use gsm48_encode_ra() for that. */ |
| 120 | raid_temp = (struct gprs_ra_id) { |
| 121 | .mcc = ri->eutran.tai.mcc, |
| 122 | .mnc = ri->eutran.tai.mnc, |
| 123 | .mnc_3_digits = ri->eutran.tai.mnc_3_digits, |
| 124 | }; |
| 125 | |
| 126 | gsm48_encode_ra((struct gsm48_ra_id *)buf, &raid_temp); |
| 127 | osmo_store16be(ri->eutran.tai.tac, buf + 3); |
| 128 | OSMO_ASSERT(ri->eutran.global_enb_id_len <= |
| 129 | sizeof(ri->eutran.global_enb_id)); |
| 130 | memcpy(buf + 5, ri->eutran.global_enb_id, |
| 131 | ri->eutran.global_enb_id_len); |
Philipp Maier | 7741bc3 | 2021-01-07 21:55:48 +0100 | [diff] [blame] | 132 | len = ri->eutran.global_enb_id_len + 6; |
| 133 | break; |
Philipp Maier | 7450f77 | 2021-01-06 20:56:43 +0100 | [diff] [blame] | 134 | default: |
| 135 | return -EINVAL; |
| 136 | } |
Philipp Maier | 7741bc3 | 2021-01-07 21:55:48 +0100 | [diff] [blame] | 137 | |
| 138 | OSMO_ASSERT(len <= BSSGP_RIM_ROUTING_INFO_MAXLEN); |
| 139 | return len; |
Philipp Maier | 7450f77 | 2021-01-06 20:56:43 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Philipp Maier | 9828d28 | 2021-01-06 20:40:23 +0100 | [diff] [blame] | 142 | /*! Decode a RAN Information Request Application Container for NACC (3GPP TS 48.018, section 11.3.63.1.1). |
| 143 | * \param[out] user provided memory for decoded data struct. |
| 144 | * \param[in] buf user provided memory with the encoded value data of the IE. |
| 145 | * \returns 0 on success, -EINVAL on error. */ |
| 146 | int bssgp_dec_ran_inf_req_app_cont_nacc(struct bssgp_ran_inf_req_app_cont_nacc *cont, const uint8_t *buf, size_t len) |
| 147 | { |
| 148 | int rc; |
| 149 | |
| 150 | if (len < REP_CELL_ID_LEN) |
| 151 | return -EINVAL; |
| 152 | |
| 153 | rc = gsm0808_decode_cell_id_u((union gsm0808_cell_id_u*)&cont->reprt_cell, |
| 154 | CELL_IDENT_WHOLE_GLOBAL_PS, buf, len); |
| 155 | if (rc < 0) |
| 156 | return -EINVAL; |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /*! Encode a RAN Information Request Application Container for NACC (3GPP TS 48.018, section 11.3.63.1.1). |
| 162 | * \param[out] buf user provided memory for the generated value part of the IE. |
| 163 | * \param[in] cont user provided input data struct. |
| 164 | * \returns length of encoded octets, -EINVAL on error. */ |
| 165 | int bssgp_enc_ran_inf_req_app_cont_nacc(uint8_t *buf, size_t len, const struct bssgp_ran_inf_req_app_cont_nacc *cont) |
| 166 | { |
| 167 | int rc; |
| 168 | struct gprs_ra_id *raid; |
| 169 | |
| 170 | if (len < REP_CELL_ID_LEN) |
| 171 | return -EINVAL; |
| 172 | |
| 173 | raid = (struct gprs_ra_id *)&cont->reprt_cell.rai; |
| 174 | rc = bssgp_create_cell_id(buf, raid, cont->reprt_cell.cell_identity); |
| 175 | if (rc < 0) |
| 176 | return -EINVAL; |
| 177 | return rc; |
| 178 | } |
| 179 | |
| 180 | /*! Decode a RAN Information Application Container (3GPP TS 48.018, section 11.3.63.2.1). |
| 181 | * \param[out] user provided memory for decoded data struct. |
| 182 | * \param[in] buf user provided memory with the encoded value data of the IE. |
| 183 | * \returns 0 on success, -EINVAL on error. */ |
| 184 | int bssgp_dec_ran_inf_app_cont_nacc(struct bssgp_ran_inf_app_cont_nacc *cont, const uint8_t *buf, size_t len) |
| 185 | { |
| 186 | unsigned int i; |
| 187 | int remaining_buf_len; |
| 188 | int rc; |
| 189 | |
| 190 | /* The given buffer must at least contain a reporting cell identifer |
| 191 | * plus one octet that defines number/type of attached sysinfo messages. */ |
| 192 | if (len < REP_CELL_ID_LEN + 1) |
| 193 | return -EINVAL; |
| 194 | |
| 195 | rc = gsm0808_decode_cell_id_u((union gsm0808_cell_id_u*)&cont->reprt_cell, |
| 196 | CELL_IDENT_WHOLE_GLOBAL_PS, buf, len); |
| 197 | if (rc < 0) |
| 198 | return -EINVAL; |
| 199 | |
| 200 | buf += REP_CELL_ID_LEN; |
| 201 | |
| 202 | cont->type_psi = buf[0] & 1; |
| 203 | cont->num_si = buf[0] >> 1; |
| 204 | buf++; |
| 205 | |
| 206 | /* The number of sysinfo messages may be zero */ |
| 207 | if (cont->num_si == 0) |
| 208 | return 0; |
| 209 | |
| 210 | /* Check if the prospected system information messages fit in the |
| 211 | * remaining buffer space */ |
| 212 | remaining_buf_len = len - REP_CELL_ID_LEN - 1; |
| 213 | if (remaining_buf_len <= 0) |
| 214 | return -EINVAL; |
| 215 | if (cont->type_psi && remaining_buf_len / BSSGP_RIM_PSI_LEN < cont->num_si) |
| 216 | return -EINVAL; |
| 217 | else if (remaining_buf_len / BSSGP_RIM_SI_LEN < cont->num_si) |
| 218 | return -EINVAL; |
| 219 | |
| 220 | for (i = 0; i < cont->num_si; i++) { |
| 221 | cont->si[i] = buf; |
| 222 | if (cont->type_psi) |
| 223 | buf += BSSGP_RIM_PSI_LEN; |
| 224 | else |
| 225 | buf += BSSGP_RIM_SI_LEN; |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | /*! Encode a RAN Information Application Container (3GPP TS 48.018, section 11.3.63.2.1). |
| 232 | * \param[out] buf user provided memory for the generated value part of the IE. |
| 233 | * \param[in] cont user provided input data struct. |
| 234 | * \returns length of encoded octets, -EINVAL on error. */ |
| 235 | int bssgp_enc_ran_inf_app_cont_nacc(uint8_t *buf, size_t len, const struct bssgp_ran_inf_app_cont_nacc *cont) |
| 236 | { |
| 237 | uint8_t *buf_ptr = buf; |
| 238 | int rc; |
| 239 | unsigned int silen; |
| 240 | unsigned int i; |
| 241 | struct gprs_ra_id *raid; |
| 242 | |
| 243 | if (cont->type_psi) |
| 244 | silen = BSSGP_RIM_PSI_LEN; |
| 245 | else |
| 246 | silen = BSSGP_RIM_SI_LEN; |
| 247 | |
| 248 | /* The buffer must accept the reporting cell id, plus 1 byte to define |
| 249 | * the type and number of sysinfo messages. */ |
| 250 | if (len < REP_CELL_ID_LEN + 1 + silen * cont->num_si) |
| 251 | return -EINVAL; |
| 252 | |
| 253 | raid = (struct gprs_ra_id *)&cont->reprt_cell.rai; |
| 254 | rc = bssgp_create_cell_id(buf_ptr, raid, cont->reprt_cell.cell_identity); |
| 255 | if (rc < 0) |
| 256 | return -EINVAL; |
| 257 | buf_ptr += rc; |
| 258 | |
| 259 | buf_ptr[0] = 0x00; |
| 260 | if (cont->type_psi) |
| 261 | buf_ptr[0] |= 0x01; |
| 262 | buf_ptr[0] |= (cont->num_si << 1); |
| 263 | buf_ptr++; |
| 264 | |
| 265 | for (i = 0; i < cont->num_si; i++) { |
| 266 | memcpy(buf_ptr, cont->si[i], silen); |
| 267 | buf_ptr += silen; |
| 268 | } |
| 269 | |
| 270 | return (int)(buf_ptr - buf); |
| 271 | } |
| 272 | |
Philipp Maier | 139c4ae | 2021-01-22 17:19:05 +0100 | [diff] [blame^] | 273 | /* 3GPP TS 48.018, table 11.3.64.1.b, NACC Cause coding */ |
| 274 | const struct value_string bssgp_nacc_cause_strs[] = { |
| 275 | { BSSGP_NACC_CAUSE_UNSPEC, "unspecified error" }, |
| 276 | { BSSGP_NACC_CAUSE_SYNTAX_ERR, "syntax error in app container" }, |
| 277 | { BSSGP_NACC_CAUSE_RPRT_CELL_MISSMTCH, "reporting cell id mismatch" }, |
| 278 | { BSSGP_NACC_CAUSE_SIPSI_TYPE_ERR, "SI/PSI type error" }, |
| 279 | { BSSGP_NACC_CAUSE_SIPSI_LEN_ERR, "SI/PSI inconsistent length" }, |
| 280 | { BSSGP_NACC_CAUSE_SIPSI_SET_ERR, "inconsistent set of msg" }, |
| 281 | { 0, NULL } |
| 282 | }; |
| 283 | |
Philipp Maier | 9828d28 | 2021-01-06 20:40:23 +0100 | [diff] [blame] | 284 | /*! Decode a Application Error Container for NACC (3GPP TS 48.018, section 11.3.64.1). |
| 285 | * \param[out] user provided memory for decoded data struct. |
| 286 | * \param[in] buf user provided memory with the encoded value data of the IE. |
| 287 | * \returns 0 on success, -EINVAL on error. */ |
| 288 | int bssgp_dec_app_err_cont_nacc(struct bssgp_app_err_cont_nacc *cont, const uint8_t *buf, size_t len) |
| 289 | { |
| 290 | /* The buffer must at least contain the NACC cause code, it should also |
| 291 | * contain the application container, but we won't error if it is missing. */ |
| 292 | if (len < 1) |
| 293 | return -EINVAL; |
| 294 | |
| 295 | cont->nacc_cause = buf[0]; |
| 296 | |
| 297 | if (len > 1) { |
| 298 | cont->err_app_cont = buf + 1; |
| 299 | cont->err_app_cont_len = len - 1; |
| 300 | } else { |
| 301 | cont->err_app_cont = NULL; |
| 302 | cont->err_app_cont_len = 0; |
| 303 | } |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | /*! Encode Application Error Container for NACC (3GPP TS 48.018, section 11.3.64.1). |
| 309 | * \param[out] buf user provided memory for the generated value part of the IE. |
| 310 | * \param[in] cont user provided input data struct. |
| 311 | * \returns length of encoded octets, -EINVAL on error. */ |
| 312 | int bssgp_enc_app_err_cont_nacc(uint8_t *buf, size_t len, const struct bssgp_app_err_cont_nacc *cont) |
| 313 | { |
| 314 | uint8_t *buf_ptr = buf; |
| 315 | |
| 316 | /* The buffer must accept the length of the application container and the NACC |
| 317 | * cause code, which is one octet in length. */ |
| 318 | if (len < cont->err_app_cont_len + 1) |
| 319 | return -EINVAL; |
| 320 | |
| 321 | buf_ptr[0] = cont->nacc_cause; |
| 322 | buf_ptr++; |
| 323 | |
| 324 | memcpy(buf_ptr, cont->err_app_cont, cont->err_app_cont_len); |
| 325 | buf_ptr += cont->err_app_cont_len; |
| 326 | |
| 327 | return (int)(buf_ptr - buf); |
| 328 | } |
| 329 | |
| 330 | /* The structs bssgp_ran_inf_req_rim_cont, bssgp_ran_inf_rim_cont and bssgp_ran_inf_app_err_rim_cont *cont |
| 331 | * share four common fields at the beginning, we use the following struct as parameter type for the common |
| 332 | * encoder/decoder functions. (See also 3GPP TS 48.018 table 11.3.62a.1.b, table 11.3.62a.2.b, and |
| 333 | * table 11.3.62a.5.b) */ |
| 334 | struct bssgp_ran_inf_x_cont { |
| 335 | enum bssgp_ran_inf_app_id app_id; |
| 336 | uint32_t seq_num; |
| 337 | struct bssgp_rim_pdu_ind pdu_ind; |
| 338 | uint8_t prot_ver; |
| 339 | }; |
| 340 | |
| 341 | static int dec_rim_cont_common(struct bssgp_ran_inf_x_cont *cont, struct tlv_parsed *tp) |
| 342 | { |
| 343 | if (TLVP_PRES_LEN(tp, BSSGP_IE_RIM_APP_IDENTITY, sizeof(uint8_t))) |
| 344 | cont->app_id = TLVP_VAL(tp, BSSGP_IE_RIM_APP_IDENTITY)[0]; |
| 345 | else |
| 346 | return -EINVAL; |
| 347 | |
| 348 | if (TLVP_PRES_LEN(tp, BSSGP_IE_RIM_SEQ_NR, sizeof(cont->seq_num))) |
| 349 | cont->seq_num = tlvp_val32be(tp, BSSGP_IE_RIM_SEQ_NR); |
| 350 | else |
| 351 | return -EINVAL; |
| 352 | |
| 353 | if (TLVP_PRES_LEN(tp, BSSGP_IE_RIM_PDU_INDICATIONS, sizeof(cont->pdu_ind))) |
| 354 | memcpy(&cont->pdu_ind, TLVP_VAL(tp, BSSGP_IE_RIM_PDU_INDICATIONS), sizeof(cont->pdu_ind)); |
| 355 | else |
| 356 | return -EINVAL; |
| 357 | |
| 358 | if (TLVP_PRES_LEN(tp, BSSGP_IE_RIM_PROTOCOL_VERSION, sizeof(cont->prot_ver))) |
| 359 | cont->prot_ver = TLVP_VAL(tp, BSSGP_IE_RIM_PROTOCOL_VERSION)[0]; |
| 360 | else |
| 361 | cont->prot_ver = 1; |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | static uint8_t *enc_rim_cont_common(uint8_t *buf, size_t len, const struct bssgp_ran_inf_x_cont *cont) |
| 367 | { |
| 368 | |
| 369 | uint32_t seq_num = osmo_htonl(cont->seq_num); |
| 370 | uint8_t app_id_temp; |
| 371 | uint8_t *buf_ptr = buf; |
| 372 | |
| 373 | if (len < |
| 374 | TVLV_HDR_MAXLEN * 4 + sizeof(app_id_temp) + sizeof(seq_num) + sizeof(cont->pdu_ind) + |
| 375 | sizeof(cont->prot_ver)) |
| 376 | return NULL; |
| 377 | |
| 378 | app_id_temp = cont->app_id; |
| 379 | buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_RIM_APP_IDENTITY, sizeof(app_id_temp), &app_id_temp); |
| 380 | buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_RIM_SEQ_NR, sizeof(seq_num), (uint8_t *) & seq_num); |
| 381 | buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_RIM_PDU_INDICATIONS, sizeof(cont->pdu_ind), (uint8_t *) & cont->pdu_ind); |
| 382 | if (cont->prot_ver > 0) |
| 383 | buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_RIM_PROTOCOL_VERSION, sizeof(cont->prot_ver), &cont->prot_ver); |
| 384 | |
| 385 | return buf_ptr; |
| 386 | } |
| 387 | |
| 388 | /*! Decode a RAN Information Request RIM Container (3GPP TS 48.018, table 11.3.62a.1.b). |
| 389 | * \param[out] user provided memory for decoded data struct. |
| 390 | * \param[in] buf user provided memory with the encoded value data of the IE. |
| 391 | * \returns 0 on success, -EINVAL on error. */ |
| 392 | int bssgp_dec_ran_inf_req_rim_cont(struct bssgp_ran_inf_req_rim_cont *cont, const uint8_t *buf, size_t len) |
| 393 | { |
| 394 | int rc; |
| 395 | struct tlv_parsed tp; |
| 396 | |
| 397 | memset(cont, 0, sizeof(*cont)); |
| 398 | |
| 399 | rc = tlv_parse(&tp, &tvlv_att_def, buf, len, 0, 0); |
| 400 | if (rc < 0) |
| 401 | return -EINVAL; |
| 402 | |
| 403 | rc = dec_rim_cont_common((struct bssgp_ran_inf_x_cont *)cont, &tp); |
| 404 | if (rc < 0) |
| 405 | return -EINVAL; |
| 406 | |
| 407 | if (TLVP_PRESENT(&tp, BSSGP_IE_RIM_REQ_APP_CONTAINER)) { |
| 408 | switch (cont->app_id) { |
| 409 | case BSSGP_RAN_INF_APP_ID_NACC: |
| 410 | rc = bssgp_dec_ran_inf_req_app_cont_nacc(&cont->u.app_cont_nacc, |
| 411 | TLVP_VAL(&tp, BSSGP_IE_RIM_REQ_APP_CONTAINER), |
| 412 | TLVP_LEN(&tp, BSSGP_IE_RIM_REQ_APP_CONTAINER)); |
| 413 | break; |
| 414 | case BSSGP_RAN_INF_APP_ID_SI3: |
| 415 | case BSSGP_RAN_INF_APP_ID_MBMS: |
| 416 | case BSSGP_RAN_INF_APP_ID_SON: |
| 417 | case BSSGP_RAN_INF_APP_ID_UTRA_SI: |
| 418 | /* TODO: add parsers for Si3, MBMS, SON, UTRA-SI app containers */ |
Philipp Maier | 836c6da | 2021-01-19 20:03:50 +0100 | [diff] [blame] | 419 | return -EOPNOTSUPP; |
Philipp Maier | 9828d28 | 2021-01-06 20:40:23 +0100 | [diff] [blame] | 420 | default: |
| 421 | return -EINVAL; |
| 422 | } |
| 423 | |
| 424 | if (rc < 0) |
| 425 | return rc; |
| 426 | } |
| 427 | |
| 428 | if (TLVP_PRES_LEN(&tp, BSSGP_IE_SON_TRANSFER_APP_ID, 1)) { |
| 429 | cont->son_trans_app_id = TLVP_VAL(&tp, BSSGP_IE_SON_TRANSFER_APP_ID); |
| 430 | cont->son_trans_app_id_len = TLVP_LEN(&tp, BSSGP_IE_SON_TRANSFER_APP_ID); |
| 431 | } |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | /* Dub a TLVP header into a given buffer. The value part of the IE must start |
| 437 | * at the 2nd octet. Should the length field make a 3 octet TLVP header |
| 438 | * necessary (unlikely, but possible) the value part is moved ahead by one |
| 439 | * octet. The function returns a pointer to the end of value part. */ |
| 440 | static uint8_t *dub_tlvp_header(uint8_t *buf, uint8_t iei, uint16_t len) |
| 441 | { |
| 442 | uint8_t *buf_ptr = buf; |
| 443 | |
| 444 | buf_ptr[0] = iei; |
| 445 | if (len <= TVLV_MAX_ONEBYTE) { |
| 446 | buf_ptr[1] = (uint8_t) len; |
| 447 | buf_ptr[1] |= 0x80; |
| 448 | buf_ptr += TVLV_HDR_LEN; |
| 449 | } else { |
| 450 | memmove(buf_ptr + 1, buf_ptr, len); |
| 451 | buf_ptr[1] = len >> 8; |
Philipp Maier | 2b11fa9 | 2021-01-20 15:55:40 +0100 | [diff] [blame] | 452 | buf_ptr[2] = len & 0xff; |
Philipp Maier | 9828d28 | 2021-01-06 20:40:23 +0100 | [diff] [blame] | 453 | buf_ptr += TVLV_HDR_MAXLEN; |
| 454 | } |
| 455 | buf_ptr += len; |
| 456 | |
| 457 | return buf_ptr; |
| 458 | } |
| 459 | |
| 460 | /*! Encode a RAN Information Request RIM Container (3GPP TS 48.018, table 11.3.62a.1.b). |
| 461 | * \param[out] buf user provided memory for the generated value part of the IE. |
| 462 | * \param[in] cont user provided input data struct. |
| 463 | * \returns length of encoded octets, -EINVAL on error. */ |
| 464 | int bssgp_enc_ran_inf_req_rim_cont(uint8_t *buf, size_t len, const struct bssgp_ran_inf_req_rim_cont *cont) |
| 465 | { |
| 466 | uint8_t *buf_ptr = buf; |
| 467 | int app_cont_len = 0; |
| 468 | int remaining_buf_len; |
| 469 | |
| 470 | buf_ptr = enc_rim_cont_common(buf_ptr, len, (struct bssgp_ran_inf_x_cont *)cont); |
| 471 | if (!buf_ptr) |
| 472 | return -EINVAL; |
| 473 | |
| 474 | remaining_buf_len = len - (int)(buf_ptr - buf); |
| 475 | if (remaining_buf_len <= 0) |
| 476 | return -EINVAL; |
| 477 | |
| 478 | switch (cont->app_id) { |
| 479 | case BSSGP_RAN_INF_APP_ID_NACC: |
| 480 | app_cont_len = |
| 481 | bssgp_enc_ran_inf_req_app_cont_nacc(buf_ptr + TVLV_HDR_LEN, remaining_buf_len - TVLV_HDR_MAXLEN, |
| 482 | &cont->u.app_cont_nacc); |
| 483 | break; |
| 484 | case BSSGP_RAN_INF_APP_ID_SI3: |
| 485 | case BSSGP_RAN_INF_APP_ID_MBMS: |
| 486 | case BSSGP_RAN_INF_APP_ID_SON: |
| 487 | case BSSGP_RAN_INF_APP_ID_UTRA_SI: |
| 488 | /* TODO: add encoders for Si3, MBMS, SON, UTRA-SI app containers */ |
Philipp Maier | 836c6da | 2021-01-19 20:03:50 +0100 | [diff] [blame] | 489 | return -EOPNOTSUPP; |
Philipp Maier | 9828d28 | 2021-01-06 20:40:23 +0100 | [diff] [blame] | 490 | default: |
| 491 | return -EINVAL; |
| 492 | } |
| 493 | |
| 494 | if (app_cont_len < 0) |
| 495 | return -EINVAL; |
| 496 | buf_ptr = dub_tlvp_header(buf_ptr, BSSGP_IE_RIM_REQ_APP_CONTAINER, app_cont_len); |
| 497 | |
| 498 | remaining_buf_len = len - (int)(buf_ptr - buf); |
| 499 | if (remaining_buf_len < 0) |
| 500 | return -EINVAL; |
| 501 | |
| 502 | if (cont->son_trans_app_id && cont->son_trans_app_id_len > 0) { |
| 503 | if (remaining_buf_len < cont->son_trans_app_id_len + TVLV_HDR_MAXLEN) |
| 504 | return -EINVAL; |
| 505 | buf_ptr = |
| 506 | tvlv_put(buf_ptr, BSSGP_IE_SON_TRANSFER_APP_ID, cont->son_trans_app_id_len, cont->son_trans_app_id); |
| 507 | } |
| 508 | return (int)(buf_ptr - buf); |
| 509 | } |
| 510 | |
| 511 | /*! Decode a RAN Information RIM Container (3GPP TS 48.018, table 11.3.62a.2.b). |
| 512 | * \param[out] user provided memory for decoded data struct. |
| 513 | * \param[in] buf user provided memory with the encoded value data of the IE. |
| 514 | * \returns 0 on success, -EINVAL on error. */ |
| 515 | int bssgp_dec_ran_inf_rim_cont(struct bssgp_ran_inf_rim_cont *cont, const uint8_t *buf, size_t len) |
| 516 | { |
| 517 | int rc; |
| 518 | struct tlv_parsed tp; |
| 519 | |
| 520 | memset(cont, 0, sizeof(*cont)); |
| 521 | |
| 522 | rc = tlv_parse(&tp, &tvlv_att_def, buf, len, 0, 0); |
| 523 | if (rc < 0) |
| 524 | return -EINVAL; |
| 525 | |
| 526 | rc = dec_rim_cont_common((struct bssgp_ran_inf_x_cont *)cont, &tp); |
| 527 | if (rc < 0) |
| 528 | return -EINVAL; |
| 529 | |
| 530 | if (TLVP_PRESENT(&tp, BSSGP_IE_RAN_INFO_APP_CONTAINER)) { |
| 531 | switch (cont->app_id) { |
| 532 | case BSSGP_RAN_INF_APP_ID_NACC: |
| 533 | rc = bssgp_dec_ran_inf_app_cont_nacc(&cont->u.app_cont_nacc, |
| 534 | TLVP_VAL(&tp, BSSGP_IE_RAN_INFO_APP_CONTAINER), |
| 535 | TLVP_LEN(&tp, BSSGP_IE_RAN_INFO_APP_CONTAINER)); |
| 536 | break; |
| 537 | case BSSGP_RAN_INF_APP_ID_SI3: |
| 538 | case BSSGP_RAN_INF_APP_ID_MBMS: |
| 539 | case BSSGP_RAN_INF_APP_ID_SON: |
| 540 | case BSSGP_RAN_INF_APP_ID_UTRA_SI: |
| 541 | /* TODO: add parsers for Si3, MBMS, SON, UTRA-SI app containers */ |
Philipp Maier | 836c6da | 2021-01-19 20:03:50 +0100 | [diff] [blame] | 542 | return -EOPNOTSUPP; |
Philipp Maier | 9828d28 | 2021-01-06 20:40:23 +0100 | [diff] [blame] | 543 | default: |
| 544 | return -EINVAL; |
| 545 | } |
| 546 | |
| 547 | if (rc < 0) |
| 548 | return rc; |
| 549 | } else if (TLVP_PRESENT(&tp, BSSGP_IE_APP_ERROR_CONTAINER)) { |
| 550 | switch (cont->app_id) { |
| 551 | case BSSGP_RAN_INF_APP_ID_NACC: |
| 552 | rc = bssgp_dec_app_err_cont_nacc(&cont->u.app_err_cont_nacc, |
| 553 | TLVP_VAL(&tp, BSSGP_IE_APP_ERROR_CONTAINER), TLVP_LEN(&tp, |
| 554 | BSSGP_IE_APP_ERROR_CONTAINER)); |
| 555 | break; |
| 556 | case BSSGP_RAN_INF_APP_ID_SI3: |
| 557 | case BSSGP_RAN_INF_APP_ID_MBMS: |
| 558 | case BSSGP_RAN_INF_APP_ID_SON: |
| 559 | case BSSGP_RAN_INF_APP_ID_UTRA_SI: |
| 560 | /* TODO: add parsers for Si3, MBMS, SON, UTRA-SI app containers */ |
Philipp Maier | 836c6da | 2021-01-19 20:03:50 +0100 | [diff] [blame] | 561 | return -EOPNOTSUPP; |
Philipp Maier | 9828d28 | 2021-01-06 20:40:23 +0100 | [diff] [blame] | 562 | default: |
| 563 | return -EINVAL; |
| 564 | } |
| 565 | if (rc < 0) |
| 566 | return rc; |
| 567 | cont->app_err = true; |
| 568 | } |
| 569 | |
| 570 | if (TLVP_PRES_LEN(&tp, BSSGP_IE_SON_TRANSFER_APP_ID, 1)) { |
| 571 | cont->son_trans_app_id = TLVP_VAL(&tp, BSSGP_IE_SON_TRANSFER_APP_ID); |
| 572 | cont->son_trans_app_id_len = TLVP_LEN(&tp, BSSGP_IE_SON_TRANSFER_APP_ID); |
| 573 | } |
| 574 | |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | /*! Encode a RAN Information RIM Container (3GPP TS 48.018, table 11.3.62a.2.b). |
| 579 | * \param[out] buf user provided memory for the generated value part of the IE. |
| 580 | * \param[in] cont user provided input data struct. |
| 581 | * \returns length of encoded octets, -EINVAL on error. */ |
| 582 | int bssgp_enc_ran_inf_rim_cont(uint8_t *buf, size_t len, const struct bssgp_ran_inf_rim_cont *cont) |
| 583 | { |
| 584 | uint8_t *buf_ptr = buf; |
| 585 | int app_cont_len = 0; |
| 586 | int remaining_buf_len; |
| 587 | |
| 588 | buf_ptr = enc_rim_cont_common(buf_ptr, len, (struct bssgp_ran_inf_x_cont *)cont); |
| 589 | if (!buf_ptr) |
| 590 | return -EINVAL; |
| 591 | |
| 592 | remaining_buf_len = len - (int)(buf_ptr - buf); |
| 593 | if (remaining_buf_len <= 0) |
| 594 | return -EINVAL; |
| 595 | |
| 596 | if (cont->app_err) { |
| 597 | switch (cont->app_id) { |
| 598 | case BSSGP_RAN_INF_APP_ID_NACC: |
| 599 | app_cont_len = |
| 600 | bssgp_enc_app_err_cont_nacc(buf_ptr + TVLV_HDR_LEN, remaining_buf_len - TVLV_HDR_MAXLEN, |
| 601 | &cont->u.app_err_cont_nacc); |
| 602 | break; |
| 603 | case BSSGP_RAN_INF_APP_ID_SI3: |
| 604 | case BSSGP_RAN_INF_APP_ID_MBMS: |
| 605 | case BSSGP_RAN_INF_APP_ID_SON: |
| 606 | case BSSGP_RAN_INF_APP_ID_UTRA_SI: |
| 607 | /* TODO: add encoders for Si3, MBMS, SON, UTRA-SI app containers */ |
Philipp Maier | 836c6da | 2021-01-19 20:03:50 +0100 | [diff] [blame] | 608 | return -EOPNOTSUPP; |
Philipp Maier | 9828d28 | 2021-01-06 20:40:23 +0100 | [diff] [blame] | 609 | default: |
| 610 | return -EINVAL; |
| 611 | } |
| 612 | if (app_cont_len < 0) |
| 613 | return -EINVAL; |
| 614 | buf_ptr = dub_tlvp_header(buf_ptr, BSSGP_IE_APP_ERROR_CONTAINER, app_cont_len); |
| 615 | } else { |
| 616 | switch (cont->app_id) { |
| 617 | case BSSGP_RAN_INF_APP_ID_NACC: |
| 618 | app_cont_len = |
| 619 | bssgp_enc_ran_inf_app_cont_nacc(buf_ptr + TVLV_HDR_LEN, remaining_buf_len - TVLV_HDR_MAXLEN, |
| 620 | &cont->u.app_cont_nacc); |
| 621 | break; |
| 622 | case BSSGP_RAN_INF_APP_ID_SI3: |
| 623 | case BSSGP_RAN_INF_APP_ID_MBMS: |
| 624 | case BSSGP_RAN_INF_APP_ID_SON: |
| 625 | case BSSGP_RAN_INF_APP_ID_UTRA_SI: |
| 626 | /* TODO: add encoders for Si3, MBMS, SON, UTRA-SI app containers */ |
Philipp Maier | 836c6da | 2021-01-19 20:03:50 +0100 | [diff] [blame] | 627 | return -EOPNOTSUPP; |
Philipp Maier | 9828d28 | 2021-01-06 20:40:23 +0100 | [diff] [blame] | 628 | default: |
| 629 | return -EINVAL; |
| 630 | } |
| 631 | if (app_cont_len < 0) |
| 632 | return -EINVAL; |
| 633 | buf_ptr = dub_tlvp_header(buf_ptr, BSSGP_IE_RAN_INFO_APP_CONTAINER, app_cont_len); |
| 634 | } |
| 635 | |
| 636 | remaining_buf_len = len - (int)(buf_ptr - buf); |
| 637 | if (remaining_buf_len < 0) |
| 638 | return -EINVAL; |
| 639 | |
| 640 | if (cont->son_trans_app_id && cont->son_trans_app_id_len > 0) { |
| 641 | if (remaining_buf_len < cont->son_trans_app_id_len + TVLV_HDR_MAXLEN) |
| 642 | return -EINVAL; |
| 643 | buf_ptr = |
| 644 | tvlv_put(buf_ptr, BSSGP_IE_SON_TRANSFER_APP_ID, cont->son_trans_app_id_len, cont->son_trans_app_id); |
| 645 | } |
| 646 | return (int)(buf_ptr - buf); |
| 647 | } |
| 648 | |
| 649 | /*! Decode a RAN Information ACK RIM Container (3GPP TS 48.018, table 11.3.62a.3.b). |
| 650 | * \param[out] user provided memory for decoded data struct. |
| 651 | * \param[in] buf user provided memory with the encoded value data of the IE. |
| 652 | * \returns 0 on success, -EINVAL on error. */ |
| 653 | int bssgp_dec_ran_inf_ack_rim_cont(struct bssgp_ran_inf_ack_rim_cont *cont, const uint8_t *buf, size_t len) |
| 654 | { |
| 655 | int rc; |
| 656 | struct tlv_parsed tp; |
| 657 | |
| 658 | memset(cont, 0, sizeof(*cont)); |
| 659 | |
| 660 | rc = tlv_parse(&tp, &tvlv_att_def, buf, len, 0, 0); |
| 661 | if (rc < 0) |
| 662 | return -EINVAL; |
| 663 | |
| 664 | if (TLVP_PRES_LEN(&tp, BSSGP_IE_RIM_APP_IDENTITY, sizeof(uint8_t))) |
| 665 | cont->app_id = TLVP_VAL(&tp, BSSGP_IE_RIM_APP_IDENTITY)[0]; |
| 666 | else |
| 667 | return -EINVAL; |
| 668 | |
| 669 | if (TLVP_PRES_LEN(&tp, BSSGP_IE_RIM_SEQ_NR, sizeof(cont->seq_num))) |
| 670 | cont->seq_num = tlvp_val32be(&tp, BSSGP_IE_RIM_SEQ_NR); |
| 671 | else |
| 672 | return -EINVAL; |
| 673 | |
| 674 | if (TLVP_PRES_LEN(&tp, BSSGP_IE_RIM_PROTOCOL_VERSION, sizeof(cont->prot_ver))) |
| 675 | cont->prot_ver = TLVP_VAL(&tp, BSSGP_IE_RIM_PROTOCOL_VERSION)[0]; |
| 676 | else |
| 677 | cont->prot_ver = 1; |
| 678 | |
| 679 | if (TLVP_PRES_LEN(&tp, BSSGP_IE_SON_TRANSFER_APP_ID, 1)) { |
| 680 | cont->son_trans_app_id = TLVP_VAL(&tp, BSSGP_IE_SON_TRANSFER_APP_ID); |
| 681 | cont->son_trans_app_id_len = TLVP_LEN(&tp, BSSGP_IE_SON_TRANSFER_APP_ID); |
| 682 | } |
| 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | |
| 687 | /*! Encode a RAN Information ACK RIM Container (3GPP TS 48.018, table 11.3.62a.3.b). |
| 688 | * \param[out] buf user provided memory for the generated value part of the IE. |
| 689 | * \param[in] cont user provided input data struct. |
| 690 | * \returns length of encoded octets, -EINVAL on error. */ |
| 691 | int bssgp_enc_ran_inf_ack_rim_cont(uint8_t *buf, size_t len, const struct bssgp_ran_inf_ack_rim_cont *cont) |
| 692 | { |
| 693 | uint8_t *buf_ptr = buf; |
| 694 | uint32_t seq_num = osmo_htonl(cont->seq_num); |
| 695 | uint8_t app_id_temp; |
| 696 | |
| 697 | if (len < |
| 698 | 4 * TVLV_HDR_MAXLEN + sizeof(app_id_temp) + sizeof(seq_num) + sizeof(cont->prot_ver) + |
| 699 | cont->son_trans_app_id_len) |
| 700 | return -EINVAL; |
| 701 | |
| 702 | app_id_temp = cont->app_id; |
| 703 | buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_RIM_APP_IDENTITY, sizeof(app_id_temp), &app_id_temp); |
| 704 | buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_RIM_SEQ_NR, sizeof(seq_num), (uint8_t *) & seq_num); |
| 705 | |
| 706 | if (cont->prot_ver > 0) |
| 707 | buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_RIM_PROTOCOL_VERSION, sizeof(cont->prot_ver), &cont->prot_ver); |
| 708 | |
| 709 | if (cont->son_trans_app_id && cont->son_trans_app_id_len > 0) |
| 710 | buf_ptr = |
| 711 | tvlv_put(buf_ptr, BSSGP_IE_SON_TRANSFER_APP_ID, cont->son_trans_app_id_len, cont->son_trans_app_id); |
| 712 | |
| 713 | return (int)(buf_ptr - buf); |
| 714 | } |
| 715 | |
| 716 | /*! Decode a RAN Information Error RIM Container (3GPP TS 48.018, table 11.3.62a.4.b). |
| 717 | * \param[out] user provided memory for decoded data struct. |
| 718 | * \param[in] buf user provided memory with the encoded value data of the IE. |
| 719 | * \returns 0 on success, -EINVAL on error. */ |
| 720 | int bssgp_dec_ran_inf_err_rim_cont(struct bssgp_ran_inf_err_rim_cont *cont, const uint8_t *buf, size_t len) |
| 721 | { |
| 722 | int rc; |
| 723 | struct tlv_parsed tp; |
| 724 | |
| 725 | memset(cont, 0, sizeof(*cont)); |
| 726 | |
| 727 | rc = tlv_parse(&tp, &tvlv_att_def, buf, len, 0, 0); |
| 728 | if (rc < 0) |
| 729 | return -EINVAL; |
| 730 | |
| 731 | if (TLVP_PRES_LEN(&tp, BSSGP_IE_RIM_APP_IDENTITY, sizeof(uint8_t))) |
| 732 | cont->app_id = TLVP_VAL(&tp, BSSGP_IE_RIM_APP_IDENTITY)[0]; |
| 733 | else |
| 734 | return -EINVAL; |
| 735 | |
| 736 | if (TLVP_PRES_LEN(&tp, BSSGP_IE_CAUSE, sizeof(cont->cause))) |
| 737 | cont->cause = TLVP_VAL(&tp, BSSGP_IE_CAUSE)[0]; |
| 738 | else |
| 739 | return -EINVAL; |
| 740 | |
| 741 | if (TLVP_PRES_LEN(&tp, BSSGP_IE_RIM_PROTOCOL_VERSION, sizeof(cont->prot_ver))) |
| 742 | cont->prot_ver = TLVP_VAL(&tp, BSSGP_IE_RIM_PROTOCOL_VERSION)[0]; |
| 743 | else |
| 744 | cont->prot_ver = 1; |
| 745 | |
| 746 | if (TLVP_PRESENT(&tp, BSSGP_IE_PDU_IN_ERROR)) { |
| 747 | cont->err_pdu = TLVP_VAL(&tp, BSSGP_IE_PDU_IN_ERROR); |
| 748 | cont->err_pdu_len = TLVP_LEN(&tp, BSSGP_IE_PDU_IN_ERROR); |
| 749 | } else { |
| 750 | return -EINVAL; |
| 751 | } |
| 752 | |
| 753 | if (TLVP_PRES_LEN(&tp, BSSGP_IE_SON_TRANSFER_APP_ID, 1)) { |
| 754 | cont->son_trans_app_id = TLVP_VAL(&tp, BSSGP_IE_SON_TRANSFER_APP_ID); |
| 755 | cont->son_trans_app_id_len = TLVP_LEN(&tp, BSSGP_IE_SON_TRANSFER_APP_ID); |
| 756 | } |
| 757 | |
| 758 | return 0; |
| 759 | } |
| 760 | |
| 761 | /*! Encode a RAN Information Error RIM Container (3GPP TS 48.018, table 11.3.62a.4.b). |
| 762 | * \param[out] buf user provided memory for the generated value part of the IE. |
| 763 | * \param[in] cont user provided input data struct. |
| 764 | * \returns length of encoded octets, -EINVAL on error. */ |
| 765 | int bssgp_enc_ran_inf_err_rim_cont(uint8_t *buf, size_t len, const struct bssgp_ran_inf_err_rim_cont *cont) |
| 766 | { |
| 767 | uint8_t *buf_ptr = buf; |
| 768 | uint8_t app_id_temp; |
| 769 | |
| 770 | if (len < |
| 771 | TVLV_HDR_MAXLEN * 5 + sizeof(app_id_temp) + sizeof(cont->cause) + sizeof(cont->prot_ver) + |
| 772 | cont->err_pdu_len + cont->son_trans_app_id_len) |
| 773 | return -EINVAL; |
| 774 | |
| 775 | app_id_temp = cont->app_id; |
| 776 | buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_RIM_APP_IDENTITY, sizeof(app_id_temp), &app_id_temp); |
| 777 | buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_CAUSE, sizeof(cont->cause), &cont->cause); |
| 778 | |
| 779 | if (cont->prot_ver > 0) |
| 780 | buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_RIM_PROTOCOL_VERSION, sizeof(cont->prot_ver), &cont->prot_ver); |
| 781 | |
| 782 | if (cont->err_pdu && cont->err_pdu_len > 0) |
| 783 | buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_PDU_IN_ERROR, cont->err_pdu_len, cont->err_pdu); |
| 784 | else |
| 785 | return -EINVAL; |
| 786 | |
| 787 | if (cont->son_trans_app_id && cont->son_trans_app_id_len > 0) |
| 788 | buf_ptr = |
| 789 | tvlv_put(buf_ptr, BSSGP_IE_SON_TRANSFER_APP_ID, cont->son_trans_app_id_len, cont->son_trans_app_id); |
| 790 | |
| 791 | return (int)(buf_ptr - buf); |
| 792 | } |
| 793 | |
| 794 | /*! Decode a RAN Information Application Error RIM Container (3GPP TS 48.018, table 11.3.62a.5.b). |
| 795 | * \param[out] user provided memory for decoded data struct. |
| 796 | * \param[in] buf user provided memory with the encoded value data of the IE. |
| 797 | * \returns 0 on success, -EINVAL on error. */ |
| 798 | int bssgp_dec_ran_inf_app_err_rim_cont(struct bssgp_ran_inf_app_err_rim_cont *cont, const uint8_t *buf, size_t len) |
| 799 | { |
| 800 | int rc; |
| 801 | struct tlv_parsed tp; |
| 802 | |
| 803 | memset(cont, 0, sizeof(*cont)); |
| 804 | |
| 805 | rc = tlv_parse(&tp, &tvlv_att_def, buf, len, 0, 0); |
| 806 | if (rc < 0) |
| 807 | return -EINVAL; |
| 808 | |
| 809 | rc = dec_rim_cont_common((struct bssgp_ran_inf_x_cont *)cont, &tp); |
| 810 | if (rc < 0) |
| 811 | return -EINVAL; |
| 812 | |
| 813 | switch (cont->app_id) { |
| 814 | case BSSGP_RAN_INF_APP_ID_NACC: |
| 815 | rc = bssgp_dec_app_err_cont_nacc(&cont->u.app_err_cont_nacc, |
| 816 | TLVP_VAL(&tp, BSSGP_IE_APP_ERROR_CONTAINER), TLVP_LEN(&tp, |
| 817 | BSSGP_IE_APP_ERROR_CONTAINER)); |
| 818 | break; |
| 819 | case BSSGP_RAN_INF_APP_ID_SI3: |
| 820 | case BSSGP_RAN_INF_APP_ID_MBMS: |
| 821 | case BSSGP_RAN_INF_APP_ID_SON: |
| 822 | case BSSGP_RAN_INF_APP_ID_UTRA_SI: |
| 823 | /* TODO: add parsers for Si3, MBMS, SON, UTRA-SI app containers */ |
Philipp Maier | 836c6da | 2021-01-19 20:03:50 +0100 | [diff] [blame] | 824 | return -EOPNOTSUPP; |
Philipp Maier | 9828d28 | 2021-01-06 20:40:23 +0100 | [diff] [blame] | 825 | default: |
| 826 | return -EINVAL; |
| 827 | } |
| 828 | if (rc < 0) |
| 829 | return rc; |
| 830 | |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | /*! Encode a RAN Information Application Error RIM Container (3GPP TS 48.018, table 11.3.62a.5.b). |
| 835 | * \param[out] buf user provided memory for the generated value part of the IE. |
| 836 | * \param[in] cont user provided input data struct. |
| 837 | * \returns length of encoded octets, -EINVAL on error. */ |
| 838 | int bssgp_enc_ran_inf_app_err_rim_cont(uint8_t *buf, size_t len, const struct bssgp_ran_inf_app_err_rim_cont *cont) |
| 839 | { |
| 840 | uint8_t *buf_ptr = buf; |
| 841 | int app_cont_len = 0; |
| 842 | int remaining_buf_len; |
| 843 | |
| 844 | buf_ptr = enc_rim_cont_common(buf_ptr, len, (struct bssgp_ran_inf_x_cont *)cont); |
| 845 | if (!buf_ptr) |
| 846 | return -EINVAL; |
| 847 | |
| 848 | remaining_buf_len = len - (int)(buf_ptr - buf); |
| 849 | if (remaining_buf_len <= 0) |
| 850 | return -EINVAL; |
| 851 | |
| 852 | switch (cont->app_id) { |
| 853 | case BSSGP_RAN_INF_APP_ID_NACC: |
| 854 | app_cont_len = |
| 855 | bssgp_enc_app_err_cont_nacc(buf_ptr + TVLV_HDR_LEN, remaining_buf_len - TVLV_HDR_MAXLEN, |
| 856 | &cont->u.app_err_cont_nacc); |
| 857 | break; |
| 858 | case BSSGP_RAN_INF_APP_ID_SI3: |
| 859 | case BSSGP_RAN_INF_APP_ID_MBMS: |
| 860 | case BSSGP_RAN_INF_APP_ID_SON: |
| 861 | case BSSGP_RAN_INF_APP_ID_UTRA_SI: |
| 862 | /* TODO: add encoders for Si3, MBMS, SON, UTRA-SI app containers */ |
Philipp Maier | 836c6da | 2021-01-19 20:03:50 +0100 | [diff] [blame] | 863 | return -EOPNOTSUPP; |
Philipp Maier | 9828d28 | 2021-01-06 20:40:23 +0100 | [diff] [blame] | 864 | default: |
| 865 | return -EINVAL; |
| 866 | } |
| 867 | if (app_cont_len < 0) |
| 868 | return -EINVAL; |
| 869 | buf_ptr = dub_tlvp_header(buf_ptr, BSSGP_IE_APP_ERROR_CONTAINER, app_cont_len); |
| 870 | |
| 871 | return (int)(buf_ptr - buf); |
| 872 | } |