blob: 23fb3a087ffa81ab71eac7b16c24f5be72bd73f6 [file] [log] [blame]
Philipp Maier9828d282021-01-06 20:40:23 +01001/*! \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 Maier7450f772021-01-06 20:56:43 +010044/*! 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. */
48int 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. */
95int 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 Maier7741bc32021-01-07 21:55:48 +010099 int len;
Philipp Maier7450f772021-01-06 20:56:43 +0100100
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 Maier7741bc32021-01-07 21:55:48 +0100109 len = rc + 1;
110 break;
Philipp Maier7450f772021-01-06 20:56:43 +0100111 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 Maier7741bc32021-01-07 21:55:48 +0100114 len = 9;
115 break;
Philipp Maier7450f772021-01-06 20:56:43 +0100116 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 Maier7741bc32021-01-07 21:55:48 +0100132 len = ri->eutran.global_enb_id_len + 6;
133 break;
Philipp Maier7450f772021-01-06 20:56:43 +0100134 default:
135 return -EINVAL;
136 }
Philipp Maier7741bc32021-01-07 21:55:48 +0100137
138 OSMO_ASSERT(len <= BSSGP_RIM_ROUTING_INFO_MAXLEN);
139 return len;
Philipp Maier7450f772021-01-06 20:56:43 +0100140}
141
Philipp Maier9828d282021-01-06 20:40:23 +0100142/*! 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. */
146int 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. */
165int 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. */
184int 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. */
235int 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
273/*! Decode a Application Error Container for NACC (3GPP TS 48.018, section 11.3.64.1).
274 * \param[out] user provided memory for decoded data struct.
275 * \param[in] buf user provided memory with the encoded value data of the IE.
276 * \returns 0 on success, -EINVAL on error. */
277int bssgp_dec_app_err_cont_nacc(struct bssgp_app_err_cont_nacc *cont, const uint8_t *buf, size_t len)
278{
279 /* The buffer must at least contain the NACC cause code, it should also
280 * contain the application container, but we won't error if it is missing. */
281 if (len < 1)
282 return -EINVAL;
283
284 cont->nacc_cause = buf[0];
285
286 if (len > 1) {
287 cont->err_app_cont = buf + 1;
288 cont->err_app_cont_len = len - 1;
289 } else {
290 cont->err_app_cont = NULL;
291 cont->err_app_cont_len = 0;
292 }
293
294 return 0;
295}
296
297/*! Encode Application Error Container for NACC (3GPP TS 48.018, section 11.3.64.1).
298 * \param[out] buf user provided memory for the generated value part of the IE.
299 * \param[in] cont user provided input data struct.
300 * \returns length of encoded octets, -EINVAL on error. */
301int bssgp_enc_app_err_cont_nacc(uint8_t *buf, size_t len, const struct bssgp_app_err_cont_nacc *cont)
302{
303 uint8_t *buf_ptr = buf;
304
305 /* The buffer must accept the length of the application container and the NACC
306 * cause code, which is one octet in length. */
307 if (len < cont->err_app_cont_len + 1)
308 return -EINVAL;
309
310 buf_ptr[0] = cont->nacc_cause;
311 buf_ptr++;
312
313 memcpy(buf_ptr, cont->err_app_cont, cont->err_app_cont_len);
314 buf_ptr += cont->err_app_cont_len;
315
316 return (int)(buf_ptr - buf);
317}
318
319/* The structs bssgp_ran_inf_req_rim_cont, bssgp_ran_inf_rim_cont and bssgp_ran_inf_app_err_rim_cont *cont
320 * share four common fields at the beginning, we use the following struct as parameter type for the common
321 * encoder/decoder functions. (See also 3GPP TS 48.018 table 11.3.62a.1.b, table 11.3.62a.2.b, and
322 * table 11.3.62a.5.b) */
323struct bssgp_ran_inf_x_cont {
324 enum bssgp_ran_inf_app_id app_id;
325 uint32_t seq_num;
326 struct bssgp_rim_pdu_ind pdu_ind;
327 uint8_t prot_ver;
328};
329
330static int dec_rim_cont_common(struct bssgp_ran_inf_x_cont *cont, struct tlv_parsed *tp)
331{
332 if (TLVP_PRES_LEN(tp, BSSGP_IE_RIM_APP_IDENTITY, sizeof(uint8_t)))
333 cont->app_id = TLVP_VAL(tp, BSSGP_IE_RIM_APP_IDENTITY)[0];
334 else
335 return -EINVAL;
336
337 if (TLVP_PRES_LEN(tp, BSSGP_IE_RIM_SEQ_NR, sizeof(cont->seq_num)))
338 cont->seq_num = tlvp_val32be(tp, BSSGP_IE_RIM_SEQ_NR);
339 else
340 return -EINVAL;
341
342 if (TLVP_PRES_LEN(tp, BSSGP_IE_RIM_PDU_INDICATIONS, sizeof(cont->pdu_ind)))
343 memcpy(&cont->pdu_ind, TLVP_VAL(tp, BSSGP_IE_RIM_PDU_INDICATIONS), sizeof(cont->pdu_ind));
344 else
345 return -EINVAL;
346
347 if (TLVP_PRES_LEN(tp, BSSGP_IE_RIM_PROTOCOL_VERSION, sizeof(cont->prot_ver)))
348 cont->prot_ver = TLVP_VAL(tp, BSSGP_IE_RIM_PROTOCOL_VERSION)[0];
349 else
350 cont->prot_ver = 1;
351
352 return 0;
353}
354
355static uint8_t *enc_rim_cont_common(uint8_t *buf, size_t len, const struct bssgp_ran_inf_x_cont *cont)
356{
357
358 uint32_t seq_num = osmo_htonl(cont->seq_num);
359 uint8_t app_id_temp;
360 uint8_t *buf_ptr = buf;
361
362 if (len <
363 TVLV_HDR_MAXLEN * 4 + sizeof(app_id_temp) + sizeof(seq_num) + sizeof(cont->pdu_ind) +
364 sizeof(cont->prot_ver))
365 return NULL;
366
367 app_id_temp = cont->app_id;
368 buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_RIM_APP_IDENTITY, sizeof(app_id_temp), &app_id_temp);
369 buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_RIM_SEQ_NR, sizeof(seq_num), (uint8_t *) & seq_num);
370 buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_RIM_PDU_INDICATIONS, sizeof(cont->pdu_ind), (uint8_t *) & cont->pdu_ind);
371 if (cont->prot_ver > 0)
372 buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_RIM_PROTOCOL_VERSION, sizeof(cont->prot_ver), &cont->prot_ver);
373
374 return buf_ptr;
375}
376
377/*! Decode a RAN Information Request RIM Container (3GPP TS 48.018, table 11.3.62a.1.b).
378 * \param[out] user provided memory for decoded data struct.
379 * \param[in] buf user provided memory with the encoded value data of the IE.
380 * \returns 0 on success, -EINVAL on error. */
381int bssgp_dec_ran_inf_req_rim_cont(struct bssgp_ran_inf_req_rim_cont *cont, const uint8_t *buf, size_t len)
382{
383 int rc;
384 struct tlv_parsed tp;
385
386 memset(cont, 0, sizeof(*cont));
387
388 rc = tlv_parse(&tp, &tvlv_att_def, buf, len, 0, 0);
389 if (rc < 0)
390 return -EINVAL;
391
392 rc = dec_rim_cont_common((struct bssgp_ran_inf_x_cont *)cont, &tp);
393 if (rc < 0)
394 return -EINVAL;
395
396 if (TLVP_PRESENT(&tp, BSSGP_IE_RIM_REQ_APP_CONTAINER)) {
397 switch (cont->app_id) {
398 case BSSGP_RAN_INF_APP_ID_NACC:
399 rc = bssgp_dec_ran_inf_req_app_cont_nacc(&cont->u.app_cont_nacc,
400 TLVP_VAL(&tp, BSSGP_IE_RIM_REQ_APP_CONTAINER),
401 TLVP_LEN(&tp, BSSGP_IE_RIM_REQ_APP_CONTAINER));
402 break;
403 case BSSGP_RAN_INF_APP_ID_SI3:
404 case BSSGP_RAN_INF_APP_ID_MBMS:
405 case BSSGP_RAN_INF_APP_ID_SON:
406 case BSSGP_RAN_INF_APP_ID_UTRA_SI:
407 /* TODO: add parsers for Si3, MBMS, SON, UTRA-SI app containers */
Philipp Maier836c6da2021-01-19 20:03:50 +0100408 return -EOPNOTSUPP;
Philipp Maier9828d282021-01-06 20:40:23 +0100409 default:
410 return -EINVAL;
411 }
412
413 if (rc < 0)
414 return rc;
415 }
416
417 if (TLVP_PRES_LEN(&tp, BSSGP_IE_SON_TRANSFER_APP_ID, 1)) {
418 cont->son_trans_app_id = TLVP_VAL(&tp, BSSGP_IE_SON_TRANSFER_APP_ID);
419 cont->son_trans_app_id_len = TLVP_LEN(&tp, BSSGP_IE_SON_TRANSFER_APP_ID);
420 }
421
422 return 0;
423}
424
425/* Dub a TLVP header into a given buffer. The value part of the IE must start
426 * at the 2nd octet. Should the length field make a 3 octet TLVP header
427 * necessary (unlikely, but possible) the value part is moved ahead by one
428 * octet. The function returns a pointer to the end of value part. */
429static uint8_t *dub_tlvp_header(uint8_t *buf, uint8_t iei, uint16_t len)
430{
431 uint8_t *buf_ptr = buf;
432
433 buf_ptr[0] = iei;
434 if (len <= TVLV_MAX_ONEBYTE) {
435 buf_ptr[1] = (uint8_t) len;
436 buf_ptr[1] |= 0x80;
437 buf_ptr += TVLV_HDR_LEN;
438 } else {
439 memmove(buf_ptr + 1, buf_ptr, len);
440 buf_ptr[1] = len >> 8;
Philipp Maier2b11fa92021-01-20 15:55:40 +0100441 buf_ptr[2] = len & 0xff;
Philipp Maier9828d282021-01-06 20:40:23 +0100442 buf_ptr += TVLV_HDR_MAXLEN;
443 }
444 buf_ptr += len;
445
446 return buf_ptr;
447}
448
449/*! Encode a RAN Information Request RIM Container (3GPP TS 48.018, table 11.3.62a.1.b).
450 * \param[out] buf user provided memory for the generated value part of the IE.
451 * \param[in] cont user provided input data struct.
452 * \returns length of encoded octets, -EINVAL on error. */
453int bssgp_enc_ran_inf_req_rim_cont(uint8_t *buf, size_t len, const struct bssgp_ran_inf_req_rim_cont *cont)
454{
455 uint8_t *buf_ptr = buf;
456 int app_cont_len = 0;
457 int remaining_buf_len;
458
459 buf_ptr = enc_rim_cont_common(buf_ptr, len, (struct bssgp_ran_inf_x_cont *)cont);
460 if (!buf_ptr)
461 return -EINVAL;
462
463 remaining_buf_len = len - (int)(buf_ptr - buf);
464 if (remaining_buf_len <= 0)
465 return -EINVAL;
466
467 switch (cont->app_id) {
468 case BSSGP_RAN_INF_APP_ID_NACC:
469 app_cont_len =
470 bssgp_enc_ran_inf_req_app_cont_nacc(buf_ptr + TVLV_HDR_LEN, remaining_buf_len - TVLV_HDR_MAXLEN,
471 &cont->u.app_cont_nacc);
472 break;
473 case BSSGP_RAN_INF_APP_ID_SI3:
474 case BSSGP_RAN_INF_APP_ID_MBMS:
475 case BSSGP_RAN_INF_APP_ID_SON:
476 case BSSGP_RAN_INF_APP_ID_UTRA_SI:
477 /* TODO: add encoders for Si3, MBMS, SON, UTRA-SI app containers */
Philipp Maier836c6da2021-01-19 20:03:50 +0100478 return -EOPNOTSUPP;
Philipp Maier9828d282021-01-06 20:40:23 +0100479 default:
480 return -EINVAL;
481 }
482
483 if (app_cont_len < 0)
484 return -EINVAL;
485 buf_ptr = dub_tlvp_header(buf_ptr, BSSGP_IE_RIM_REQ_APP_CONTAINER, app_cont_len);
486
487 remaining_buf_len = len - (int)(buf_ptr - buf);
488 if (remaining_buf_len < 0)
489 return -EINVAL;
490
491 if (cont->son_trans_app_id && cont->son_trans_app_id_len > 0) {
492 if (remaining_buf_len < cont->son_trans_app_id_len + TVLV_HDR_MAXLEN)
493 return -EINVAL;
494 buf_ptr =
495 tvlv_put(buf_ptr, BSSGP_IE_SON_TRANSFER_APP_ID, cont->son_trans_app_id_len, cont->son_trans_app_id);
496 }
497 return (int)(buf_ptr - buf);
498}
499
500/*! Decode a RAN Information RIM Container (3GPP TS 48.018, table 11.3.62a.2.b).
501 * \param[out] user provided memory for decoded data struct.
502 * \param[in] buf user provided memory with the encoded value data of the IE.
503 * \returns 0 on success, -EINVAL on error. */
504int bssgp_dec_ran_inf_rim_cont(struct bssgp_ran_inf_rim_cont *cont, const uint8_t *buf, size_t len)
505{
506 int rc;
507 struct tlv_parsed tp;
508
509 memset(cont, 0, sizeof(*cont));
510
511 rc = tlv_parse(&tp, &tvlv_att_def, buf, len, 0, 0);
512 if (rc < 0)
513 return -EINVAL;
514
515 rc = dec_rim_cont_common((struct bssgp_ran_inf_x_cont *)cont, &tp);
516 if (rc < 0)
517 return -EINVAL;
518
519 if (TLVP_PRESENT(&tp, BSSGP_IE_RAN_INFO_APP_CONTAINER)) {
520 switch (cont->app_id) {
521 case BSSGP_RAN_INF_APP_ID_NACC:
522 rc = bssgp_dec_ran_inf_app_cont_nacc(&cont->u.app_cont_nacc,
523 TLVP_VAL(&tp, BSSGP_IE_RAN_INFO_APP_CONTAINER),
524 TLVP_LEN(&tp, BSSGP_IE_RAN_INFO_APP_CONTAINER));
525 break;
526 case BSSGP_RAN_INF_APP_ID_SI3:
527 case BSSGP_RAN_INF_APP_ID_MBMS:
528 case BSSGP_RAN_INF_APP_ID_SON:
529 case BSSGP_RAN_INF_APP_ID_UTRA_SI:
530 /* TODO: add parsers for Si3, MBMS, SON, UTRA-SI app containers */
Philipp Maier836c6da2021-01-19 20:03:50 +0100531 return -EOPNOTSUPP;
Philipp Maier9828d282021-01-06 20:40:23 +0100532 default:
533 return -EINVAL;
534 }
535
536 if (rc < 0)
537 return rc;
538 } else if (TLVP_PRESENT(&tp, BSSGP_IE_APP_ERROR_CONTAINER)) {
539 switch (cont->app_id) {
540 case BSSGP_RAN_INF_APP_ID_NACC:
541 rc = bssgp_dec_app_err_cont_nacc(&cont->u.app_err_cont_nacc,
542 TLVP_VAL(&tp, BSSGP_IE_APP_ERROR_CONTAINER), TLVP_LEN(&tp,
543 BSSGP_IE_APP_ERROR_CONTAINER));
544 break;
545 case BSSGP_RAN_INF_APP_ID_SI3:
546 case BSSGP_RAN_INF_APP_ID_MBMS:
547 case BSSGP_RAN_INF_APP_ID_SON:
548 case BSSGP_RAN_INF_APP_ID_UTRA_SI:
549 /* TODO: add parsers for Si3, MBMS, SON, UTRA-SI app containers */
Philipp Maier836c6da2021-01-19 20:03:50 +0100550 return -EOPNOTSUPP;
Philipp Maier9828d282021-01-06 20:40:23 +0100551 default:
552 return -EINVAL;
553 }
554 if (rc < 0)
555 return rc;
556 cont->app_err = true;
557 }
558
559 if (TLVP_PRES_LEN(&tp, BSSGP_IE_SON_TRANSFER_APP_ID, 1)) {
560 cont->son_trans_app_id = TLVP_VAL(&tp, BSSGP_IE_SON_TRANSFER_APP_ID);
561 cont->son_trans_app_id_len = TLVP_LEN(&tp, BSSGP_IE_SON_TRANSFER_APP_ID);
562 }
563
564 return 0;
565}
566
567/*! Encode a RAN Information RIM Container (3GPP TS 48.018, table 11.3.62a.2.b).
568 * \param[out] buf user provided memory for the generated value part of the IE.
569 * \param[in] cont user provided input data struct.
570 * \returns length of encoded octets, -EINVAL on error. */
571int bssgp_enc_ran_inf_rim_cont(uint8_t *buf, size_t len, const struct bssgp_ran_inf_rim_cont *cont)
572{
573 uint8_t *buf_ptr = buf;
574 int app_cont_len = 0;
575 int remaining_buf_len;
576
577 buf_ptr = enc_rim_cont_common(buf_ptr, len, (struct bssgp_ran_inf_x_cont *)cont);
578 if (!buf_ptr)
579 return -EINVAL;
580
581 remaining_buf_len = len - (int)(buf_ptr - buf);
582 if (remaining_buf_len <= 0)
583 return -EINVAL;
584
585 if (cont->app_err) {
586 switch (cont->app_id) {
587 case BSSGP_RAN_INF_APP_ID_NACC:
588 app_cont_len =
589 bssgp_enc_app_err_cont_nacc(buf_ptr + TVLV_HDR_LEN, remaining_buf_len - TVLV_HDR_MAXLEN,
590 &cont->u.app_err_cont_nacc);
591 break;
592 case BSSGP_RAN_INF_APP_ID_SI3:
593 case BSSGP_RAN_INF_APP_ID_MBMS:
594 case BSSGP_RAN_INF_APP_ID_SON:
595 case BSSGP_RAN_INF_APP_ID_UTRA_SI:
596 /* TODO: add encoders for Si3, MBMS, SON, UTRA-SI app containers */
Philipp Maier836c6da2021-01-19 20:03:50 +0100597 return -EOPNOTSUPP;
Philipp Maier9828d282021-01-06 20:40:23 +0100598 default:
599 return -EINVAL;
600 }
601 if (app_cont_len < 0)
602 return -EINVAL;
603 buf_ptr = dub_tlvp_header(buf_ptr, BSSGP_IE_APP_ERROR_CONTAINER, app_cont_len);
604 } else {
605 switch (cont->app_id) {
606 case BSSGP_RAN_INF_APP_ID_NACC:
607 app_cont_len =
608 bssgp_enc_ran_inf_app_cont_nacc(buf_ptr + TVLV_HDR_LEN, remaining_buf_len - TVLV_HDR_MAXLEN,
609 &cont->u.app_cont_nacc);
610 break;
611 case BSSGP_RAN_INF_APP_ID_SI3:
612 case BSSGP_RAN_INF_APP_ID_MBMS:
613 case BSSGP_RAN_INF_APP_ID_SON:
614 case BSSGP_RAN_INF_APP_ID_UTRA_SI:
615 /* TODO: add encoders for Si3, MBMS, SON, UTRA-SI app containers */
Philipp Maier836c6da2021-01-19 20:03:50 +0100616 return -EOPNOTSUPP;
Philipp Maier9828d282021-01-06 20:40:23 +0100617 default:
618 return -EINVAL;
619 }
620 if (app_cont_len < 0)
621 return -EINVAL;
622 buf_ptr = dub_tlvp_header(buf_ptr, BSSGP_IE_RAN_INFO_APP_CONTAINER, app_cont_len);
623 }
624
625 remaining_buf_len = len - (int)(buf_ptr - buf);
626 if (remaining_buf_len < 0)
627 return -EINVAL;
628
629 if (cont->son_trans_app_id && cont->son_trans_app_id_len > 0) {
630 if (remaining_buf_len < cont->son_trans_app_id_len + TVLV_HDR_MAXLEN)
631 return -EINVAL;
632 buf_ptr =
633 tvlv_put(buf_ptr, BSSGP_IE_SON_TRANSFER_APP_ID, cont->son_trans_app_id_len, cont->son_trans_app_id);
634 }
635 return (int)(buf_ptr - buf);
636}
637
638/*! Decode a RAN Information ACK RIM Container (3GPP TS 48.018, table 11.3.62a.3.b).
639 * \param[out] user provided memory for decoded data struct.
640 * \param[in] buf user provided memory with the encoded value data of the IE.
641 * \returns 0 on success, -EINVAL on error. */
642int bssgp_dec_ran_inf_ack_rim_cont(struct bssgp_ran_inf_ack_rim_cont *cont, const uint8_t *buf, size_t len)
643{
644 int rc;
645 struct tlv_parsed tp;
646
647 memset(cont, 0, sizeof(*cont));
648
649 rc = tlv_parse(&tp, &tvlv_att_def, buf, len, 0, 0);
650 if (rc < 0)
651 return -EINVAL;
652
653 if (TLVP_PRES_LEN(&tp, BSSGP_IE_RIM_APP_IDENTITY, sizeof(uint8_t)))
654 cont->app_id = TLVP_VAL(&tp, BSSGP_IE_RIM_APP_IDENTITY)[0];
655 else
656 return -EINVAL;
657
658 if (TLVP_PRES_LEN(&tp, BSSGP_IE_RIM_SEQ_NR, sizeof(cont->seq_num)))
659 cont->seq_num = tlvp_val32be(&tp, BSSGP_IE_RIM_SEQ_NR);
660 else
661 return -EINVAL;
662
663 if (TLVP_PRES_LEN(&tp, BSSGP_IE_RIM_PROTOCOL_VERSION, sizeof(cont->prot_ver)))
664 cont->prot_ver = TLVP_VAL(&tp, BSSGP_IE_RIM_PROTOCOL_VERSION)[0];
665 else
666 cont->prot_ver = 1;
667
668 if (TLVP_PRES_LEN(&tp, BSSGP_IE_SON_TRANSFER_APP_ID, 1)) {
669 cont->son_trans_app_id = TLVP_VAL(&tp, BSSGP_IE_SON_TRANSFER_APP_ID);
670 cont->son_trans_app_id_len = TLVP_LEN(&tp, BSSGP_IE_SON_TRANSFER_APP_ID);
671 }
672
673 return 0;
674}
675
676/*! Encode a RAN Information ACK RIM Container (3GPP TS 48.018, table 11.3.62a.3.b).
677 * \param[out] buf user provided memory for the generated value part of the IE.
678 * \param[in] cont user provided input data struct.
679 * \returns length of encoded octets, -EINVAL on error. */
680int bssgp_enc_ran_inf_ack_rim_cont(uint8_t *buf, size_t len, const struct bssgp_ran_inf_ack_rim_cont *cont)
681{
682 uint8_t *buf_ptr = buf;
683 uint32_t seq_num = osmo_htonl(cont->seq_num);
684 uint8_t app_id_temp;
685
686 if (len <
687 4 * TVLV_HDR_MAXLEN + sizeof(app_id_temp) + sizeof(seq_num) + sizeof(cont->prot_ver) +
688 cont->son_trans_app_id_len)
689 return -EINVAL;
690
691 app_id_temp = cont->app_id;
692 buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_RIM_APP_IDENTITY, sizeof(app_id_temp), &app_id_temp);
693 buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_RIM_SEQ_NR, sizeof(seq_num), (uint8_t *) & seq_num);
694
695 if (cont->prot_ver > 0)
696 buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_RIM_PROTOCOL_VERSION, sizeof(cont->prot_ver), &cont->prot_ver);
697
698 if (cont->son_trans_app_id && cont->son_trans_app_id_len > 0)
699 buf_ptr =
700 tvlv_put(buf_ptr, BSSGP_IE_SON_TRANSFER_APP_ID, cont->son_trans_app_id_len, cont->son_trans_app_id);
701
702 return (int)(buf_ptr - buf);
703}
704
705/*! Decode a RAN Information Error RIM Container (3GPP TS 48.018, table 11.3.62a.4.b).
706 * \param[out] user provided memory for decoded data struct.
707 * \param[in] buf user provided memory with the encoded value data of the IE.
708 * \returns 0 on success, -EINVAL on error. */
709int bssgp_dec_ran_inf_err_rim_cont(struct bssgp_ran_inf_err_rim_cont *cont, const uint8_t *buf, size_t len)
710{
711 int rc;
712 struct tlv_parsed tp;
713
714 memset(cont, 0, sizeof(*cont));
715
716 rc = tlv_parse(&tp, &tvlv_att_def, buf, len, 0, 0);
717 if (rc < 0)
718 return -EINVAL;
719
720 if (TLVP_PRES_LEN(&tp, BSSGP_IE_RIM_APP_IDENTITY, sizeof(uint8_t)))
721 cont->app_id = TLVP_VAL(&tp, BSSGP_IE_RIM_APP_IDENTITY)[0];
722 else
723 return -EINVAL;
724
725 if (TLVP_PRES_LEN(&tp, BSSGP_IE_CAUSE, sizeof(cont->cause)))
726 cont->cause = TLVP_VAL(&tp, BSSGP_IE_CAUSE)[0];
727 else
728 return -EINVAL;
729
730 if (TLVP_PRES_LEN(&tp, BSSGP_IE_RIM_PROTOCOL_VERSION, sizeof(cont->prot_ver)))
731 cont->prot_ver = TLVP_VAL(&tp, BSSGP_IE_RIM_PROTOCOL_VERSION)[0];
732 else
733 cont->prot_ver = 1;
734
735 if (TLVP_PRESENT(&tp, BSSGP_IE_PDU_IN_ERROR)) {
736 cont->err_pdu = TLVP_VAL(&tp, BSSGP_IE_PDU_IN_ERROR);
737 cont->err_pdu_len = TLVP_LEN(&tp, BSSGP_IE_PDU_IN_ERROR);
738 } else {
739 return -EINVAL;
740 }
741
742 if (TLVP_PRES_LEN(&tp, BSSGP_IE_SON_TRANSFER_APP_ID, 1)) {
743 cont->son_trans_app_id = TLVP_VAL(&tp, BSSGP_IE_SON_TRANSFER_APP_ID);
744 cont->son_trans_app_id_len = TLVP_LEN(&tp, BSSGP_IE_SON_TRANSFER_APP_ID);
745 }
746
747 return 0;
748}
749
750/*! Encode a RAN Information Error RIM Container (3GPP TS 48.018, table 11.3.62a.4.b).
751 * \param[out] buf user provided memory for the generated value part of the IE.
752 * \param[in] cont user provided input data struct.
753 * \returns length of encoded octets, -EINVAL on error. */
754int bssgp_enc_ran_inf_err_rim_cont(uint8_t *buf, size_t len, const struct bssgp_ran_inf_err_rim_cont *cont)
755{
756 uint8_t *buf_ptr = buf;
757 uint8_t app_id_temp;
758
759 if (len <
760 TVLV_HDR_MAXLEN * 5 + sizeof(app_id_temp) + sizeof(cont->cause) + sizeof(cont->prot_ver) +
761 cont->err_pdu_len + cont->son_trans_app_id_len)
762 return -EINVAL;
763
764 app_id_temp = cont->app_id;
765 buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_RIM_APP_IDENTITY, sizeof(app_id_temp), &app_id_temp);
766 buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_CAUSE, sizeof(cont->cause), &cont->cause);
767
768 if (cont->prot_ver > 0)
769 buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_RIM_PROTOCOL_VERSION, sizeof(cont->prot_ver), &cont->prot_ver);
770
771 if (cont->err_pdu && cont->err_pdu_len > 0)
772 buf_ptr = tvlv_put(buf_ptr, BSSGP_IE_PDU_IN_ERROR, cont->err_pdu_len, cont->err_pdu);
773 else
774 return -EINVAL;
775
776 if (cont->son_trans_app_id && cont->son_trans_app_id_len > 0)
777 buf_ptr =
778 tvlv_put(buf_ptr, BSSGP_IE_SON_TRANSFER_APP_ID, cont->son_trans_app_id_len, cont->son_trans_app_id);
779
780 return (int)(buf_ptr - buf);
781}
782
783/*! Decode a RAN Information Application Error RIM Container (3GPP TS 48.018, table 11.3.62a.5.b).
784 * \param[out] user provided memory for decoded data struct.
785 * \param[in] buf user provided memory with the encoded value data of the IE.
786 * \returns 0 on success, -EINVAL on error. */
787int bssgp_dec_ran_inf_app_err_rim_cont(struct bssgp_ran_inf_app_err_rim_cont *cont, const uint8_t *buf, size_t len)
788{
789 int rc;
790 struct tlv_parsed tp;
791
792 memset(cont, 0, sizeof(*cont));
793
794 rc = tlv_parse(&tp, &tvlv_att_def, buf, len, 0, 0);
795 if (rc < 0)
796 return -EINVAL;
797
798 rc = dec_rim_cont_common((struct bssgp_ran_inf_x_cont *)cont, &tp);
799 if (rc < 0)
800 return -EINVAL;
801
802 switch (cont->app_id) {
803 case BSSGP_RAN_INF_APP_ID_NACC:
804 rc = bssgp_dec_app_err_cont_nacc(&cont->u.app_err_cont_nacc,
805 TLVP_VAL(&tp, BSSGP_IE_APP_ERROR_CONTAINER), TLVP_LEN(&tp,
806 BSSGP_IE_APP_ERROR_CONTAINER));
807 break;
808 case BSSGP_RAN_INF_APP_ID_SI3:
809 case BSSGP_RAN_INF_APP_ID_MBMS:
810 case BSSGP_RAN_INF_APP_ID_SON:
811 case BSSGP_RAN_INF_APP_ID_UTRA_SI:
812 /* TODO: add parsers for Si3, MBMS, SON, UTRA-SI app containers */
Philipp Maier836c6da2021-01-19 20:03:50 +0100813 return -EOPNOTSUPP;
Philipp Maier9828d282021-01-06 20:40:23 +0100814 default:
815 return -EINVAL;
816 }
817 if (rc < 0)
818 return rc;
819
820 return 0;
821}
822
823/*! Encode a RAN Information Application Error RIM Container (3GPP TS 48.018, table 11.3.62a.5.b).
824 * \param[out] buf user provided memory for the generated value part of the IE.
825 * \param[in] cont user provided input data struct.
826 * \returns length of encoded octets, -EINVAL on error. */
827int bssgp_enc_ran_inf_app_err_rim_cont(uint8_t *buf, size_t len, const struct bssgp_ran_inf_app_err_rim_cont *cont)
828{
829 uint8_t *buf_ptr = buf;
830 int app_cont_len = 0;
831 int remaining_buf_len;
832
833 buf_ptr = enc_rim_cont_common(buf_ptr, len, (struct bssgp_ran_inf_x_cont *)cont);
834 if (!buf_ptr)
835 return -EINVAL;
836
837 remaining_buf_len = len - (int)(buf_ptr - buf);
838 if (remaining_buf_len <= 0)
839 return -EINVAL;
840
841 switch (cont->app_id) {
842 case BSSGP_RAN_INF_APP_ID_NACC:
843 app_cont_len =
844 bssgp_enc_app_err_cont_nacc(buf_ptr + TVLV_HDR_LEN, remaining_buf_len - TVLV_HDR_MAXLEN,
845 &cont->u.app_err_cont_nacc);
846 break;
847 case BSSGP_RAN_INF_APP_ID_SI3:
848 case BSSGP_RAN_INF_APP_ID_MBMS:
849 case BSSGP_RAN_INF_APP_ID_SON:
850 case BSSGP_RAN_INF_APP_ID_UTRA_SI:
851 /* TODO: add encoders for Si3, MBMS, SON, UTRA-SI app containers */
Philipp Maier836c6da2021-01-19 20:03:50 +0100852 return -EOPNOTSUPP;
Philipp Maier9828d282021-01-06 20:40:23 +0100853 default:
854 return -EINVAL;
855 }
856 if (app_cont_len < 0)
857 return -EINVAL;
858 buf_ptr = dub_tlvp_header(buf_ptr, BSSGP_IE_APP_ERROR_CONTAINER, app_cont_len);
859
860 return (int)(buf_ptr - buf);
861}