blob: fa0f254e257dee083eb3bc463253b46f2a068457 [file] [log] [blame]
Neels Hofmeyr02de87b2020-09-18 18:00:50 +02001/* 3GPP TS 49.031 BSSMAP-LE protocol definitions */
2/*
3 * (C) 2020 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr <neels@hofmeyr.de>
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
Neels Hofmeyr02de87b2020-09-18 18:00:50 +020020 */
21
22#include <string.h>
23
24#include <osmocom/core/byteswap.h>
25#include <osmocom/core/endian.h>
26#include <osmocom/core/msgb.h>
27#include <osmocom/gsm/bssmap_le.h>
28#include <osmocom/gsm/bsslap.h>
29#include <osmocom/gsm/gad.h>
30#include <osmocom/gsm/gsm48.h>
31#include <osmocom/gsm/gsm0808.h>
32
33/*! \addtogroup bssmap_le
34 * @{
35 * \file bssmap_le.c
36 * Message encoding and decoding for 3GPP TS 49.031 BSSMAP-LE.
37 */
38
39#define BSSAP_LE_MSG_SIZE BSSMAP_MSG_SIZE
40#define BSSAP_LE_MSG_HEADROOM BSSMAP_MSG_HEADROOM
41
42static const struct tlv_definition osmo_bssmap_le_tlvdef = {
43 .def = {
44 [BSSMAP_LE_IEI_LCS_QoS] = { TLV_TYPE_TLV },
45 [BSSMAP_LE_IEI_LCS_PRIORITY] = { TLV_TYPE_TLV },
46 [BSSMAP_LE_IEI_LOCATION_TYPE] = { TLV_TYPE_TLV },
47 [BSSMAP_LE_IEI_GANSS_LOCATION_TYPE] = { TLV_TYPE_TLV },
48 [BSSMAP_LE_IEI_GEO_LOCATION] = { TLV_TYPE_TLV },
49 [BSSMAP_LE_IEI_POSITIONING_DATA] = { TLV_TYPE_TLV },
50 [BSSMAP_LE_IEI_GANSS_POS_DATA] = { TLV_TYPE_TLV },
51 [BSSMAP_LE_IEI_VELOCITY_DATA] = { TLV_TYPE_TLV },
52 [BSSMAP_LE_IEI_LCS_CAUSE] = { TLV_TYPE_TLV },
53 [BSSMAP_LE_IEI_LCS_CLIENT_TYPE] = { TLV_TYPE_TLV },
54 [BSSMAP_LE_IEI_APDU] = { TLV_TYPE_TL16V },
55 [BSSMAP_LE_IEI_NET_ELEM_ID] = { TLV_TYPE_TLV },
56 [BSSMAP_LE_IEI_REQ_GPS_ASS_D] = { TLV_TYPE_TLV },
57 [BSSMAP_LE_IEI_REQ_GANSS_ASS_D] = { TLV_TYPE_TLV },
58 [BSSMAP_LE_IEI_DECIPH_KEYS] = { TLV_TYPE_TLV },
59 [BSSMAP_LE_IEI_RET_ERR_REQ] = { TLV_TYPE_TLV },
60 [BSSMAP_LE_IEI_RET_ERR_CAUSE] = { TLV_TYPE_TLV },
61 [BSSMAP_LE_IEI_SEGMENTATION] = { TLV_TYPE_TLV },
62 [BSSMAP_LE_IEI_CLASSMARK3_INFO] = { TLV_TYPE_TLV },
63 [BSSMAP_LE_IEI_CAUSE] = { TLV_TYPE_TLV },
64 [BSSMAP_LE_IEI_CELL_ID] = { TLV_TYPE_TLV },
65 [BSSMAP_LE_IEI_CHOSEN_CHAN] = { TLV_TYPE_TLV },
66 [BSSMAP_LE_IEI_IMSI] = { TLV_TYPE_TLV },
67 [BSSMAP_LE_IEI_LCS_CAPABILITY] = { TLV_TYPE_TLV },
68 [BSSMAP_LE_IEI_PKT_MEAS_REP] = { TLV_TYPE_TLV },
69 [BSSMAP_LE_IEI_CELL_ID_LIST] = { TLV_TYPE_TLV },
70 [BSSMAP_LE_IEI_IMEI] = { TLV_TYPE_TLV },
71 [BSSMAP_LE_IEI_BSS_MLAT_CAP] = { TLV_TYPE_TLV },
72 [BSSMAP_LE_IEI_CELL_INFO_LIST] = { TLV_TYPE_TLV },
73 [BSSMAP_LE_IEI_BTS_RX_ACC_LVL] = { TLV_TYPE_TLV },
74 [BSSMAP_LE_IEI_MLAT_METHOD] = { TLV_TYPE_TLV },
75 [BSSMAP_LE_IEI_MLAT_TA] = { TLV_TYPE_TLV },
76 [BSSMAP_LE_IEI_MS_SYNC_ACC] = { TLV_TYPE_TLV },
77 [BSSMAP_LE_IEI_SHORT_ID_SET] = { TLV_TYPE_TLV },
78 [BSSMAP_LE_IEI_RANDOM_ID_SET] = { TLV_TYPE_TLV },
79 [BSSMAP_LE_IEI_SHORT_BSS_ID] = { TLV_TYPE_TLV },
80 [BSSMAP_LE_IEI_RANDOM_ID] = { TLV_TYPE_TLV },
81 [BSSMAP_LE_IEI_SHORT_ID] = { TLV_TYPE_TLV },
82 [BSSMAP_LE_IEI_COVERAGE_CLASS] = { TLV_TYPE_TLV },
83 [BSSMAP_LE_IEI_MTA_ACC_SEC_RQD] = { TLV_TYPE_TLV },
84 },
85};
86
87#define DEC_ERR_NO_RETURN(RC, MSG_TYPE, IEI, CAUSE, fmt, args...) do { \
88 if (err && !*err) { \
89 *err = talloc_zero(err_ctx, struct osmo_bssmap_le_err); \
90 **err = (struct osmo_bssmap_le_err){ \
91 .rc = (RC), \
92 .msg_type = (MSG_TYPE), \
93 .iei = (IEI), \
94 .cause = (CAUSE), \
95 }; \
96 (*err)->logmsg = talloc_asprintf(*err, "Error decoding BSSMAP-LE%s%s%s%s%s: " fmt, \
97 (MSG_TYPE) >= 0 ? " " : "", \
98 (MSG_TYPE) >= 0 ? osmo_bssmap_le_msgt_name(MSG_TYPE) : "", \
99 (IEI) >= 0 ? ": " : "", \
100 (IEI) >= 0 ? osmo_bssmap_le_iei_name(IEI) : "", \
101 (IEI) >= 0 ? " IE" : "", \
102 ##args); \
103 } \
104 } while(0)
105
106#define DEC_ERR(RC, MSG_TYPE, IEI, CAUSE, fmt, args...) do { \
107 DEC_ERR_NO_RETURN(RC, MSG_TYPE, IEI, CAUSE, fmt, ##args); \
108 return RC; \
109 } while(0)
110
111#define DEC_IE_MANDATORY(MSG_TYPE, IEI, DEC_FUN, DEC_FUN_ARG) do { \
112 const struct tlv_p_entry *e; \
113 int rc; \
114 if (!(e = TLVP_GET(tp, IEI))) \
115 DEC_ERR(-EINVAL, MSG_TYPE, IEI, LCS_CAUSE_DATA_MISSING_IN_REQ, "missing mandatory IE"); \
116 rc = DEC_FUN(DEC_FUN_ARG, MSG_TYPE, IEI, err, err_ctx, e->val, e->len); \
117 if (rc) \
118 DEC_ERR(rc, MSG_TYPE, IEI, LCS_CAUSE_UNSPECIFIED, "cannot parse IE"); \
119 } while (0)
120
121#define DEC_IE_OPTIONAL_FLAG(MSG_TYPE, IEI, DEC_FUN, DEC_FUN_ARG, PRESENCE_FLAG) do { \
122 const struct tlv_p_entry *e; \
123 int rc; \
124 if ((e = TLVP_GET(tp, IEI))) {\
125 rc = DEC_FUN(DEC_FUN_ARG, MSG_TYPE, IEI, err, err_ctx, e->val, e->len); \
126 if (rc) \
127 DEC_ERR(rc, MSG_TYPE, IEI, LCS_CAUSE_UNSPECIFIED, "cannot parse IE"); \
128 PRESENCE_FLAG = true; \
129 } \
130 } while (0)
131
132#define DEC_IE_OPTIONAL(MSG_TYPE, IEI, DEC_FUN, DEC_FUN_ARG) do { \
133 const struct tlv_p_entry *e; \
134 int rc; \
135 if ((e = TLVP_GET(tp, IEI))) {\
136 rc = DEC_FUN(DEC_FUN_ARG, MSG_TYPE, IEI, err, err_ctx, e->val, e->len); \
137 if (rc) \
138 DEC_ERR(rc, MSG_TYPE, IEI, LCS_CAUSE_UNSPECIFIED, "cannot parse IE"); \
139 } \
140 } while (0)
141
142/*! Encode full BSSMAP-LE Location Type IE, including IEI tag and length.
143 * \param[inout] msg Message buffer to append to.
144 * \param[in] location_type Values to enconde.
145 * \returns length of bytes written to the msgb.
146 */
147uint8_t osmo_bssmap_le_ie_enc_location_type(struct msgb *msg,
148 const struct bssmap_le_location_type *location_type)
149{
150 uint8_t *old_tail;
151 uint8_t *tlv_len;
152 OSMO_ASSERT(msg);
153 msgb_put_u8(msg, BSSMAP_LE_IEI_LOCATION_TYPE);
154 tlv_len = msgb_put(msg, 1);
155 old_tail = msg->tail;
156 msgb_put_u8(msg, location_type->location_information);
157
158 switch (location_type->location_information) {
159 case BSSMAP_LE_LOC_INFO_ASSIST_TARGET_MS:
160 case BSSMAP_LE_LOC_INFO_BC_DECIPHER_KEYS:
161 msgb_put_u8(msg, location_type->positioning_method);
162 break;
163 default:
164 break;
165 }
166
167 *tlv_len = (uint8_t) (msg->tail - old_tail);
168 return *tlv_len + 2;
169}
170
171/*! Decode BSSMAP-LE Location Type IE value part.
172 * \param[out] lt Buffer to write decoded values to.
173 * \param[in] elem Pointer to the value part, the V of a TLV.
174 * \param[in] len Length, the L of a TLV.
175 * \returns 0 on success, negative on error; lt is always overwritten: cleared on error, populated with values on
176 * success.
177 */
178int osmo_bssmap_le_ie_dec_location_type(struct bssmap_le_location_type *lt,
179 enum bssmap_le_msgt msgt, enum bssmap_le_iei iei,
180 struct osmo_bssmap_le_err **err, void *err_ctx,
181 const uint8_t *elem, uint8_t len)
182{
183 *lt = (struct bssmap_le_location_type){};
184
185 if (!elem || len < 1)
186 DEC_ERR(-EINVAL, msgt, iei, LCS_CAUSE_UNSPECIFIED, "zero length");
187
188 lt->location_information = elem[0];
189 switch (lt->location_information) {
190
191 case BSSMAP_LE_LOC_INFO_CURRENT_GEOGRAPHIC:
192 if (len != 1)
193 DEC_ERR(-EINVAL, msgt, iei, LCS_CAUSE_UNSPECIFIED,
194 "location info type 'Current Geographic': length should be 1 byte, got %u", len);
195 lt->positioning_method = BSSMAP_LE_POS_METHOD_OMITTED;
196 return 0;
197
198 case BSSMAP_LE_LOC_INFO_ASSIST_TARGET_MS:
199 case BSSMAP_LE_LOC_INFO_BC_DECIPHER_KEYS:
200 if (len != 2)
201 DEC_ERR(-EINVAL, msgt, iei, LCS_CAUSE_UNSPECIFIED,
202 "location info type %d: length should be 2 bytes, got %u",
203 lt->location_information, len);
204 lt->positioning_method = elem[1];
205 switch (lt->positioning_method) {
206 case BSSMAP_LE_POS_METHOD_MOBILE_ASSISTED_E_OTD:
207 case BSSMAP_LE_POS_METHOD_MOBILE_BASED_E_OTD:
208 case BSSMAP_LE_POS_METHOD_ASSISTED_GPS:
209 return 0;
210 default:
211 DEC_ERR(-EINVAL, msgt, iei, LCS_CAUSE_UNSPECIFIED,
212 "location info type %d: unknown Positioning Method: %d",
213 lt->location_information, lt->positioning_method);
214 }
215
216 default:
217 DEC_ERR(-EINVAL, msgt, iei, LCS_CAUSE_UNSPECIFIED, "unknown location info type %d",
218 lt->location_information);
219 }
220}
221
222/*! Encode full BSSMAP-LE LCS Client Type IE, including IEI tag and length.
223 * \param[inout] msg Message buffer to append to.
224 * \param[in] client_type Value to enconde.
225 * \returns length of bytes written to the msgb.
226 */
227static uint8_t osmo_bssmap_le_ie_enc_lcs_client_type(struct msgb *msg, enum bssmap_le_lcs_client_type client_type)
228{
229 OSMO_ASSERT(msg);
230 msgb_put_u8(msg, BSSMAP_LE_IEI_LCS_CLIENT_TYPE);
231 /* length */
232 msgb_put_u8(msg, 1);
233 msgb_put_u8(msg, client_type);
234 return 3;
235}
236
237static int osmo_bssmap_le_ie_dec_lcs_client_type(enum bssmap_le_lcs_client_type *client_type,
238 enum bssmap_le_msgt msgt, enum bssmap_le_iei iei,
239 struct osmo_bssmap_le_err **err, void *err_ctx,
240 const uint8_t *elem, uint8_t len)
241{
242 *client_type = 0;
243
244 if (!elem || len < 1)
245 DEC_ERR(-EINVAL, msgt, iei, LCS_CAUSE_UNSPECIFIED, "zero length");
246
247 *client_type = elem[0];
248
249 switch (*client_type) {
250 case BSSMAP_LE_LCS_CTYPE_VALUE_ADDED_UNSPECIFIED:
251 case BSSMAP_LE_LCS_CTYPE_PLMN_OPER_UNSPECIFIED:
252 case BSSMAP_LE_LCS_CTYPE_PLMN_OPER_BCAST_SERVICE:
253 case BSSMAP_LE_LCS_CTYPE_PLMN_OPER_OAM:
254 case BSSMAP_LE_LCS_CTYPE_PLMN_OPER_ANON_STATS:
255 case BSSMAP_LE_LCS_CTYPE_PLMN_OPER_TGT_MS_SVC:
256 case BSSMAP_LE_LCS_CTYPE_EMERG_SVC_UNSPECIFIED:
257 case BSSMAP_LE_LCS_CTYPE_LI_UNSPECIFIED:
258 return 0;
259 default:
260 DEC_ERR(-EINVAL, msgt, iei, LCS_CAUSE_UNSPECIFIED, "unknown LCS Client Type: %d", *client_type);
261 }
262}
263
264/*! Encode the value part of 3GPP TS 49.031 10.13 LCS Cause, without IEI and len.
265 * Identically used in 3GPP TS 48.008 3.2.2.66. Usage example:
266 *
267 * uint8_t *l = msgb_tl_put(msg, BSSMAP_LE_IEI_LCS_CAUSE);
268 * int rc = osmo_lcs_cause_enc(msg, &lcs_cause);
269 * if (rc < 0)
270 * goto error;
271 * *l = rc;
272 *
273 * \param[inout] msg Message buffer to append the LCS Cause values to.
274 * \param[in] lcs_cause LCS Cause values to enconde.
275 * \returns length of bytes written to the msgb.
276 */
277int osmo_lcs_cause_enc(struct msgb *msg, const struct lcs_cause_ie *lcs_cause)
278{
279 msgb_put_u8(msg, lcs_cause->cause_val);
280 if (lcs_cause->cause_val == LCS_CAUSE_POS_METH_FAILURE && lcs_cause->diag_val_present) {
281 msgb_put_u8(msg, lcs_cause->diag_val);
282 return 2;
283 }
284 return 1;
285}
286
287/*! Decode the value part of 3GPP TS 49.031 10.13 LCS Cause, without IEI and len.
288 * Identically used in 3GPP TS 48.008 3.2.2.66.
289 *
290 * \param[out] lcs_cause Write decoded LCS Cause values here.
291 * \param[in] data Encoded cause bytes.
292 * \param[in] len Length of data in bytes.
293 * \returns 0 on success, negative on error.
294 */
295int osmo_lcs_cause_dec(struct lcs_cause_ie *lcs_cause,
296 enum bssmap_le_msgt msgt, enum bssmap_le_iei iei,
297 struct osmo_bssmap_le_err **err, void *err_ctx,
298 const uint8_t *data, uint8_t len)
299{
300 *lcs_cause = (struct lcs_cause_ie){};
301
302 if (!data || len < 1)
303 DEC_ERR(-EINVAL, msgt, iei, LCS_CAUSE_UNSPECIFIED, "zero length");
304
305 lcs_cause->present = true;
306 lcs_cause->cause_val = data[0];
307 if (len > 1) {
308 lcs_cause->diag_val_present = true;
309 lcs_cause->diag_val = data[1];
310 }
311 if (len > 2)
312 DEC_ERR(-EINVAL, msgt, iei, LCS_CAUSE_UNSPECIFIED, "expected length <= 2, got %u", len);
313
314 return 0;
315}
316
317static int osmo_bssmap_le_ie_enc_apdu(struct msgb *msg, const struct bsslap_pdu *bsslap)
318{
319 uint8_t *old_tail;
320 void *l;
321 msgb_put_u8(msg, BSSMAP_LE_IEI_APDU);
322 l = msgb_put(msg, 2);
323 old_tail = msg->tail;
324 msgb_put_u8(msg, BSSMAP_LE_APDU_PROT_BSSLAP);
325 int rc = osmo_bsslap_enc(msg, bsslap);
326 if (rc <= 0)
327 return -EINVAL;
328 osmo_store16be(msg->tail - old_tail, l);
329 return 0;
330}
331
332static int osmo_bssmap_le_ie_dec_apdu(struct bsslap_pdu *bsslap,
333 enum bssmap_le_msgt msgt, enum bssmap_le_iei iei,
334 struct osmo_bssmap_le_err **err, void *err_ctx,
335 const uint8_t *data, size_t len)
336{
337 enum bssmap_le_apdu_proto proto;
338 struct osmo_bsslap_err *bsslap_err;
339
340 if (!data || len < 1)
341 DEC_ERR(-EINVAL, msgt, iei, LCS_CAUSE_UNSPECIFIED, "zero length");
342
343 proto = data[0];
344
345 switch (proto) {
346 case BSSMAP_LE_APDU_PROT_BSSLAP:
347 if (osmo_bsslap_dec(bsslap, &bsslap_err, err_ctx, data + 1, len - 1)) {
348 DEC_ERR_NO_RETURN(bsslap_err ? bsslap_err->rc : -EINVAL,
349 msgt, iei, LCS_CAUSE_UNSPECIFIED,
350 "Error decoding BSSLAP%s%s",
351 bsslap_err && bsslap_err->logmsg ? ": " : "",
352 bsslap_err && bsslap_err->logmsg ? bsslap_err->logmsg : "");
353 (*err)->bsslap_err = bsslap_err;
354 return (*err)->rc;
355 }
356 return 0;
357 case BSSMAP_LE_APDU_PROT_LLP:
358 case BSSMAP_LE_APDU_PROT_SMLCPP:
359 DEC_ERR(-EINVAL, msgt, iei, LCS_CAUSE_UNSPECIFIED, "Unimplemented APDU type: %d", proto);
360 default:
361 DEC_ERR(-EINVAL, msgt, iei, LCS_CAUSE_UNSPECIFIED, "Invalid APDU type: %d", proto);
362 }
363}
364
365static int osmo_bssmap_le_ie_dec_cell_id(struct gsm0808_cell_id *cell_id,
366 enum bssmap_le_msgt msgt, enum bssmap_le_iei iei,
367 struct osmo_bssmap_le_err **err, void *err_ctx,
368 const uint8_t *elem, uint8_t len)
369{
370 int rc;
371 rc = gsm0808_dec_cell_id(cell_id, elem, len);
372 if (rc <= 0)
373 DEC_ERR(rc, msgt, iei, LCS_CAUSE_UNSPECIFIED, "Error decoding Cell Identifier %s",
374 osmo_hexdump_c(err_ctx, elem, len));
375 return 0;
376}
377
378static int osmo_bssmap_le_ie_dec_imsi(struct osmo_mobile_identity *imsi,
379 enum bssmap_le_msgt msgt, enum bssmap_le_iei iei,
380 struct osmo_bssmap_le_err **err, void *err_ctx,
381 const uint8_t *elem, uint8_t len)
382{
383 int rc;
384 rc = osmo_mobile_identity_decode(imsi, elem, len, false);
385 if (rc || imsi->type != GSM_MI_TYPE_IMSI)
386 DEC_ERR(-EINVAL, msgt, iei, LCS_CAUSE_UNSPECIFIED,
387 "cannot parse IMSI identity %s", osmo_hexdump_c(err_ctx, elem, len));
388 return 0;
389}
390
391static int osmo_bssmap_le_ie_dec_imei(struct osmo_mobile_identity *imei,
392 enum bssmap_le_msgt msgt, enum bssmap_le_iei iei,
393 struct osmo_bssmap_le_err **err, void *err_ctx,
394 const uint8_t *elem, uint8_t len)
395{
396 int rc;
397 rc = osmo_mobile_identity_decode(imei, elem, len, false);
398 if (rc || imei->type != GSM_MI_TYPE_IMEI)
399 DEC_ERR(-EINVAL, msgt, iei, LCS_CAUSE_UNSPECIFIED,
400 "cannot parse IMEI identity %s", osmo_hexdump_c(err_ctx, elem, len));
401 return 0;
402}
403
404static int osmo_bssmap_le_ie_dec_gad(union gad_raw *gad,
405 enum bssmap_le_msgt msgt, enum bssmap_le_iei iei,
406 struct osmo_bssmap_le_err **err, void *err_ctx,
407 const uint8_t *elem, uint8_t len)
408{
409 struct osmo_gad_err *gad_err;
410 if (osmo_gad_raw_read(gad, &gad_err, err_ctx, elem, len)) {
411 DEC_ERR_NO_RETURN(gad_err ? gad_err->rc : -EINVAL,
412 msgt, BSSMAP_LE_IEI_GEO_LOCATION, LCS_CAUSE_UNSPECIFIED,
413 "Error decoding GAD%s%s",
414 gad_err && gad_err->logmsg ? ": " : "",
415 gad_err && gad_err->logmsg ? gad_err->logmsg : "");
416 (*err)->gad_err = gad_err;
417 return (*err)->rc;
418 }
419 return 0;
420}
421
422struct osmo_bssap_le_header {
423 uint8_t type;
424 uint8_t length;
425 uint8_t data[0];
426} __attribute__((packed));
427
428/*! Return the BSSMAP-LE msg_type from a BSSAP-LE PDU, e.g. from a msgb_l3().
429 * \param[in] data BSSAP-LE PDU data, starting with BSSAP-LE discriminator.
430 * \param[in] len Length of data in bytes.
431 * \returns bssmap_le_msgt or negative on error or non-BSSMAP-LE discriminator. */
432enum bssmap_le_msgt osmo_bssmap_le_msgt(const uint8_t *data, uint8_t len)
433{
434 const struct osmo_bssap_le_header *h = (void*)data;
435 if (!data || len < sizeof(struct osmo_bssap_le_header) + 1)
436 return -1;
437 if (h->type != BSSAP_LE_MSG_DISCR_BSSMAP_LE)
438 return -1;
439 return h->data[0];
440}
441
442static int osmo_bssmap_le_enc_reset(struct msgb *msg, enum gsm0808_cause cause)
443{
444 /* The BSSMAP-LE Reset Cause is defined as identical to the 3GPP TS 48.008 Cause. */
445 gsm0808_enc_cause(msg, cause);
446 return 0;
447}
448
449static int osmo_bssmap_le_dec_reset(enum gsm0808_cause *cause,
450 enum bssmap_le_msgt msgt,
451 struct osmo_bssmap_le_err **err, void *err_ctx,
452 const struct tlv_parsed *tp)
453{
454 const struct tlv_p_entry *e;
455
456 if (!(e = TLVP_GET(tp, BSSMAP_LE_IEI_CAUSE)))
457 DEC_ERR(-EINVAL, msgt, BSSMAP_LE_IEI_CAUSE, LCS_CAUSE_DATA_MISSING_IN_REQ, "missing mandatory IE");
458
459 *cause = gsm0808_get_cause(tp);
460 if (*cause < 0)
461 DEC_ERR(-EINVAL, msgt, BSSMAP_LE_IEI_CAUSE, LCS_CAUSE_UNSPECIFIED, "cannot parse IE");
462
463 return 0;
464}
465
466static int osmo_bssmap_le_enc_perform_loc_req(struct msgb *msg, const struct bssmap_le_perform_loc_req *params)
467{
468 osmo_bssmap_le_ie_enc_location_type(msg, &params->location_type);
469
470 gsm0808_enc_cell_id(msg, &params->cell_id);
471
472 if (params->lcs_client_type_present)
473 osmo_bssmap_le_ie_enc_lcs_client_type(msg, params->lcs_client_type);
474
475 if (params->apdu_present) {
476 int rc = osmo_bssmap_le_ie_enc_apdu(msg, &params->apdu);
477 if (rc < 0)
478 return rc;
479 }
480
481 if (params->imsi.type == GSM_MI_TYPE_IMSI) {
482 uint8_t *l = msgb_tl_put(msg, BSSMAP_LE_IEI_IMSI);
483 int rc = osmo_mobile_identity_encode_msgb(msg, &params->imsi, false);
484 if (rc < 0)
485 return rc;
486 *l = rc;
487 }
488
489 if (params->imei.type == GSM_MI_TYPE_IMEI) {
490 uint8_t *l = msgb_tl_put(msg, BSSMAP_LE_IEI_IMEI);
491 int rc = osmo_mobile_identity_encode_msgb(msg, &params->imei, false);
492 if (rc < 0)
493 return rc;
494 *l = rc;
495 }
496 return 0;
497}
498
499static int osmo_bssmap_le_dec_perform_loc_req(struct bssmap_le_perform_loc_req *params,
500 enum bssmap_le_msgt msgt,
501 struct osmo_bssmap_le_err **err, void *err_ctx,
502 const struct tlv_parsed *tp)
503{
504 *params = (struct bssmap_le_perform_loc_req){};
505
506 DEC_IE_MANDATORY(msgt, BSSMAP_LE_IEI_LOCATION_TYPE, osmo_bssmap_le_ie_dec_location_type,
507 &params->location_type);
508 DEC_IE_MANDATORY(msgt, BSSMAP_LE_IEI_CELL_ID, osmo_bssmap_le_ie_dec_cell_id,
509 &params->cell_id);
510 DEC_IE_OPTIONAL_FLAG(msgt, BSSMAP_LE_IEI_LCS_CLIENT_TYPE, osmo_bssmap_le_ie_dec_lcs_client_type,
511 &params->lcs_client_type, params->lcs_client_type_present);
512 DEC_IE_OPTIONAL_FLAG(msgt, BSSMAP_LE_IEI_APDU, osmo_bssmap_le_ie_dec_apdu, &params->apdu,
513 params->apdu_present);
514 DEC_IE_OPTIONAL(msgt, BSSMAP_LE_IEI_IMSI, osmo_bssmap_le_ie_dec_imsi, &params->imsi);
515 DEC_IE_OPTIONAL(msgt, BSSMAP_LE_IEI_IMEI, osmo_bssmap_le_ie_dec_imei, &params->imei);
516
517 return 0;
518}
519
520static int osmo_bssmap_le_enc_perform_loc_resp(struct msgb *msg, const struct bssmap_le_perform_loc_resp *params)
521{
522 if (params->location_estimate_present) {
523 uint8_t *l = msgb_tl_put(msg, BSSMAP_LE_IEI_GEO_LOCATION);
524 int rc = osmo_gad_raw_write(msg, &params->location_estimate);
525 if (rc < 0)
526 return rc;
527 *l = rc;
528 }
529
530 if (params->lcs_cause.present) {
531 uint8_t *l = msgb_tl_put(msg, BSSMAP_LE_IEI_LCS_CAUSE);
532 int rc = osmo_lcs_cause_enc(msg, &params->lcs_cause);
533 if (rc < 0)
534 return rc;
535 *l = rc;
536 }
537 return 0;
538}
539
540static int osmo_bssmap_le_dec_perform_loc_resp(struct bssmap_le_perform_loc_resp *params,
541 enum bssmap_le_msgt msgt,
542 struct osmo_bssmap_le_err **err, void *err_ctx,
543 const struct tlv_parsed *tp)
544{
545 *params = (struct bssmap_le_perform_loc_resp){};
546
547 DEC_IE_OPTIONAL_FLAG(msgt, BSSMAP_LE_IEI_GEO_LOCATION, osmo_bssmap_le_ie_dec_gad, &params->location_estimate,
548 params->location_estimate_present);
549 DEC_IE_OPTIONAL(msgt, BSSMAP_LE_IEI_LCS_CAUSE, osmo_lcs_cause_dec, &params->lcs_cause);
550
551 return 0;
552}
553
554static int osmo_bssmap_le_enc_perform_loc_abort(struct msgb *msg, const struct lcs_cause_ie *params)
555{
556 uint8_t *l = msgb_tl_put(msg, BSSMAP_LE_IEI_LCS_CAUSE);
557 int rc = osmo_lcs_cause_enc(msg, params);
558 if (rc < 0)
559 return rc;
560 *l = rc;
561 return 0;
562}
563
564static int osmo_bssmap_le_dec_perform_loc_abort(struct lcs_cause_ie *params,
565 enum bssmap_le_msgt msgt,
566 struct osmo_bssmap_le_err **err, void *err_ctx,
567 const struct tlv_parsed *tp)
568{
569 *params = (struct lcs_cause_ie){};
570
571 DEC_IE_MANDATORY(msgt, BSSMAP_LE_IEI_LCS_CAUSE, osmo_lcs_cause_dec, params);
572 return 0;
573}
574
575static int osmo_bssmap_le_enc_conn_oriented_info(struct msgb *msg,
576 const struct bssmap_le_conn_oriented_info *params)
577{
578 return osmo_bssmap_le_ie_enc_apdu(msg, &params->apdu);
579}
580
581static int osmo_bssmap_le_dec_conn_oriented_info(struct bssmap_le_conn_oriented_info *params,
582 enum bssmap_le_msgt msgt,
583 struct osmo_bssmap_le_err **err, void *err_ctx,
584 const struct tlv_parsed *tp)
585{
586 *params = (struct bssmap_le_conn_oriented_info){};
587 DEC_IE_MANDATORY(msgt, BSSMAP_LE_IEI_APDU, osmo_bssmap_le_ie_dec_apdu, &params->apdu);
588 return 0;
589}
590
591/*! Encode BSSMAP-LE PDU and add to msgb (3GPP TS 49.031).
592 * See also osmo_bssap_le_enc().
593 * \param[out] msg msgb to append to.
594 * \param[in] pdu PDU data to encode.
595 * \return number of bytes written, negative on error.
596 */
597static int osmo_bssmap_le_enc(struct msgb *msg, const struct bssmap_le_pdu *pdu)
598{
599 int rc;
600 uint8_t *old_tail;
601 old_tail = msg->tail;
602
603 msgb_v_put(msg, pdu->msg_type);
604
605 switch (pdu->msg_type) {
606 case BSSMAP_LE_MSGT_RESET:
607 rc = osmo_bssmap_le_enc_reset(msg, pdu->reset);
608 break;
609 case BSSMAP_LE_MSGT_RESET_ACK:
610 /* Consists only of the message type. */
611 rc = 0;
612 break;
613 case BSSMAP_LE_MSGT_PERFORM_LOC_REQ:
614 rc = osmo_bssmap_le_enc_perform_loc_req(msg, &pdu->perform_loc_req);
615 break;
616 case BSSMAP_LE_MSGT_PERFORM_LOC_RESP:
617 rc = osmo_bssmap_le_enc_perform_loc_resp(msg, &pdu->perform_loc_resp);
618 break;
619 case BSSMAP_LE_MSGT_PERFORM_LOC_ABORT:
620 rc = osmo_bssmap_le_enc_perform_loc_abort(msg, &pdu->perform_loc_abort);
621 break;
622 case BSSMAP_LE_MSGT_CONN_ORIENTED_INFO:
623 rc = osmo_bssmap_le_enc_conn_oriented_info(msg, &pdu->conn_oriented_info);
624 break;
625 default:
626 rc = -ENOTSUP;
627 }
628
629 if (rc < 0)
630 return rc;
631
632 return (msg->tail - old_tail);
633}
634
635/*! Decode BSSMAP-LE PDU (3GPP TS 49.031).
636 * See also osmo_bssap_le_dec().
637 * \param[out] pdu Write decoded values here.
638 * \param[in] data Pointer to BSSMAP-LE PDU raw data.
639 * \param[in] len Data length to decode.
640 * \return NULL upon success, a human readable error message on failure.
641 */
642static int osmo_bssmap_le_dec(struct bssmap_le_pdu *pdu,
643 struct osmo_bssmap_le_err **err, void *err_ctx,
644 const uint8_t *data, size_t len)
645{
646 const uint8_t *ies_start;
647 int ies_len;
648 struct tlv_parsed tp;
649
650 *pdu = (struct bssmap_le_pdu){};
651
652 if (len < 1)
653 DEC_ERR(-EINVAL, -1, -1, LCS_CAUSE_UNSPECIFIED, "zero length");
654 pdu->msg_type = data[0];
655
656 /* BSSMAP-LE IEs */
657 ies_start = &data[1];
658 ies_len = len - 1;
659
660 if (tlv_parse(&tp, &osmo_bssmap_le_tlvdef, ies_start, ies_len, 0, 0) < 0)
661 DEC_ERR(-EINVAL, pdu->msg_type, -1, LCS_CAUSE_UNSPECIFIED, "failed to parse TLV structure");
662
663 switch (pdu->msg_type) {
664 case BSSMAP_LE_MSGT_RESET:
665 return osmo_bssmap_le_dec_reset(&pdu->reset, pdu->msg_type, err, err_ctx, &tp);
666 case BSSMAP_LE_MSGT_RESET_ACK:
667 /* Consists only of the message type. */
668 return 0;
669 case BSSMAP_LE_MSGT_PERFORM_LOC_REQ:
670 return osmo_bssmap_le_dec_perform_loc_req(&pdu->perform_loc_req, pdu->msg_type, err, err_ctx, &tp);
671 case BSSMAP_LE_MSGT_PERFORM_LOC_RESP:
672 return osmo_bssmap_le_dec_perform_loc_resp(&pdu->perform_loc_resp, pdu->msg_type, err, err_ctx, &tp);
673 case BSSMAP_LE_MSGT_PERFORM_LOC_ABORT:
674 return osmo_bssmap_le_dec_perform_loc_abort(&pdu->perform_loc_abort, pdu->msg_type, err, err_ctx, &tp);
675 case BSSMAP_LE_MSGT_CONN_ORIENTED_INFO:
676 return osmo_bssmap_le_dec_conn_oriented_info(&pdu->conn_oriented_info, pdu->msg_type, err, err_ctx,
677 &tp);
678 default:
679 DEC_ERR(-EINVAL, pdu->msg_type, -1, LCS_CAUSE_UNSPECIFIED, "Unsupported BSSMAP-LE message type");
680 }
681}
682
683/*! Encode BSSAP-LE PDU returned in new msgb (3GPP TS 49.031).
684 * By spec, BSSAP-LE contains either BSSMAP-LE or DTAP.
685 * \param[in] pdu PDU data to encode.
686 * \return msgb with encoded data and l2h set to the start.
687 */
688struct msgb *osmo_bssap_le_enc(const struct bssap_le_pdu *pdu)
689{
690 struct msgb *msg;
691 int rc;
692
693 if (pdu->discr != BSSAP_LE_MSG_DISCR_BSSMAP_LE)
694 return NULL;
695
696 msg = msgb_alloc_headroom(BSSAP_LE_MSG_SIZE, BSSAP_LE_MSG_HEADROOM,
697 osmo_bssmap_le_msgt_name(pdu->bssmap_le.msg_type));
698 if (!msg)
699 return NULL;
700
701 rc = osmo_bssmap_le_enc(msg, &pdu->bssmap_le);
702 if (rc <= 0) {
703 msgb_free(msg);
704 return NULL;
705 }
706
707 /* prepend header with final length */
708 msg->l2h = msgb_tv_push(msg, pdu->discr, msgb_length(msg));
709
710 return msg;
711}
712
713/*! Decode BSSAP-LE PDU (3GPP TS 49.031).
714 * \param[out] pdu Write decoded values here.
715 * \param[in] data Pointer to BSSMAP-LE PDU raw data.
716 * \param[in] len Data length to decode.
717 * \return NULL upon success, a human readable error message on failure.
718 */
719int osmo_bssap_le_dec(struct bssap_le_pdu *pdu, struct osmo_bssap_le_err **err, void *err_ctx, struct msgb *msg)
720{
721 struct osmo_bssap_le_header *h;
722 unsigned int check_len;
723 struct osmo_bssmap_le_err *bssmap_le_err = NULL;
724 int rc;
725
726#define BSSAP_LE_DEC_ERR(RC, fmt, args...) do { \
727 if (err && !*err) { \
728 *err = talloc_zero(err_ctx, struct osmo_bssap_le_err); \
729 **err = (struct osmo_bssap_le_err){ \
730 .rc = (RC), \
731 .logmsg = talloc_asprintf(*err, "Error decoding BSSAP-LE: " fmt, ##args), \
732 }; \
733 } \
734 return RC; \
735 } while(0)
736
737 *pdu = (struct bssap_le_pdu){};
738
739 h = msgb_l2(msg);
740 if (!h)
741 BSSAP_LE_DEC_ERR(-EINVAL, "missing msgb_l2() pointer");
742 if (msgb_l2len(msg) < sizeof(*h))
743 BSSAP_LE_DEC_ERR(-EINVAL, "message too short for header");
744 check_len = msgb_l2len(msg) - sizeof(*h);
745 if (h->length < check_len)
746 BSSAP_LE_DEC_ERR(-EINVAL, "message truncated, header length (%u) longer than message (%u)",
747 h->length, check_len);
748
749 switch (h->type) {
750 case BSSAP_LE_MSG_DISCR_BSSMAP_LE:
751 break;
752 default:
753 BSSAP_LE_DEC_ERR(-EINVAL, "unsupported discr %u, only BSSMAP-LE is implemented", h->type);
754 }
755
756 rc = osmo_bssmap_le_dec(&pdu->bssmap_le, err ? &bssmap_le_err : NULL, err_ctx,
757 h->data, h->length);
758 if (rc)
759 BSSAP_LE_DEC_ERR(rc, "%s",
760 (bssmap_le_err && bssmap_le_err->logmsg) ?
761 bssmap_le_err->logmsg : "unknown error in BSSMAP-LE part");
762 return 0;
763}
764
765const struct value_string osmo_bssmap_le_msgt_names[] = {
766 { BSSMAP_LE_MSGT_PERFORM_LOC_REQ, "PERFORM LOCATION REQUEST" },
767 { BSSMAP_LE_MSGT_PERFORM_LOC_RESP, "PERFORM LOCATION RESPONSE" },
768 { BSSMAP_LE_MSGT_PERFORM_LOC_ABORT, "PERFORM LOCATION ABORT" },
769 { BSSMAP_LE_MSGT_PERFORM_LOC_INFO, "PERFORM LOCATION INFO" },
770 { BSSMAP_LE_MSGT_ASSIST_INFO_REQ, "ASSISTANCE INFORMATION REQUEST" },
771 { BSSMAP_LE_MSGT_ASSIST_INFO_RESP, "ASSISTANCE INFORMATION RESPONSE" },
772 { BSSMAP_LE_MSGT_CONN_ORIENTED_INFO, "CONNECTION ORIENTED INFORMATON" },
773 { BSSMAP_LE_MSGT_CONN_LESS_INFO, "CONNECTIONLESS INFORMATION" },
774 { BSSMAP_LE_MSGT_RESET, "RESET" },
775 { BSSMAP_LE_MSGT_RESET_ACK, "RESET ACKNOWLEDGE" },
776 {}
777};
778
779const struct value_string osmo_bssmap_le_iei_names[] = {
780 { BSSMAP_LE_IEI_LCS_QoS, "LCS_QoS" },
781 { BSSMAP_LE_IEI_LCS_PRIORITY, "LCS_PRIORITY" },
782 { BSSMAP_LE_IEI_LOCATION_TYPE, "LOCATION_TYPE" },
783 { BSSMAP_LE_IEI_GANSS_LOCATION_TYPE, "GANSS_LOCATION_TYPE" },
784 { BSSMAP_LE_IEI_GEO_LOCATION, "GEO_LOCATION" },
785 { BSSMAP_LE_IEI_POSITIONING_DATA, "POSITIONING_DATA" },
786 { BSSMAP_LE_IEI_GANSS_POS_DATA, "GANSS_POS_DATA" },
787 { BSSMAP_LE_IEI_VELOCITY_DATA, "VELOCITY_DATA" },
788 { BSSMAP_LE_IEI_LCS_CAUSE, "LCS_CAUSE" },
789 { BSSMAP_LE_IEI_LCS_CLIENT_TYPE, "LCS_CLIENT_TYPE" },
790 { BSSMAP_LE_IEI_APDU, "APDU" },
791 { BSSMAP_LE_IEI_NET_ELEM_ID, "NET_ELEM_ID" },
792 { BSSMAP_LE_IEI_REQ_GPS_ASS_D, "REQ_GPS_ASS_D" },
793 { BSSMAP_LE_IEI_REQ_GANSS_ASS_D, "REQ_GANSS_ASS_D" },
794 { BSSMAP_LE_IEI_DECIPH_KEYS, "DECIPH_KEYS" },
795 { BSSMAP_LE_IEI_RET_ERR_REQ, "RET_ERR_REQ" },
796 { BSSMAP_LE_IEI_RET_ERR_CAUSE, "RET_ERR_CAUSE" },
797 { BSSMAP_LE_IEI_SEGMENTATION, "SEGMENTATION" },
798 { BSSMAP_LE_IEI_CLASSMARK3_INFO, "CLASSMARK3_INFO" },
799 { BSSMAP_LE_IEI_CAUSE, "CAUSE" },
800 { BSSMAP_LE_IEI_CELL_ID, "CELL_ID" },
801 { BSSMAP_LE_IEI_CHOSEN_CHAN, "CHOSEN_CHAN" },
802 { BSSMAP_LE_IEI_IMSI, "IMSI" },
803 { BSSMAP_LE_IEI_LCS_CAPABILITY, "LCS_CAPABILITY" },
804 { BSSMAP_LE_IEI_PKT_MEAS_REP, "PKT_MEAS_REP" },
805 { BSSMAP_LE_IEI_CELL_ID_LIST, "CELL_ID_LIST" },
806 { BSSMAP_LE_IEI_IMEI, "IMEI" },
807 { BSSMAP_LE_IEI_BSS_MLAT_CAP, "BSS_MLAT_CAP" },
808 { BSSMAP_LE_IEI_CELL_INFO_LIST, "CELL_INFO_LIST" },
809 { BSSMAP_LE_IEI_BTS_RX_ACC_LVL, "BTS_RX_ACC_LVL" },
810 { BSSMAP_LE_IEI_MLAT_METHOD, "MLAT_METHOD" },
811 { BSSMAP_LE_IEI_MLAT_TA, "MLAT_TA" },
812 { BSSMAP_LE_IEI_MS_SYNC_ACC, "MS_SYNC_ACC" },
813 { BSSMAP_LE_IEI_SHORT_ID_SET, "SHORT_ID_SET" },
814 { BSSMAP_LE_IEI_RANDOM_ID_SET, "RANDOM_ID_SET" },
815 { BSSMAP_LE_IEI_SHORT_BSS_ID, "SHORT_BSS_ID" },
816 { BSSMAP_LE_IEI_RANDOM_ID, "RANDOM_ID" },
817 { BSSMAP_LE_IEI_SHORT_ID, "SHORT_ID" },
818 { BSSMAP_LE_IEI_COVERAGE_CLASS, "COVERAGE_CLASS" },
819 { BSSMAP_LE_IEI_MTA_ACC_SEC_RQD, "MTA_ACC_SEC_RQD" },
820 {}
821};
822
823/*! Return a human readable string describing a BSSAP-LE PDU.
824 * \param[out] buf String buffer to write to.
825 * \param[in] buflen sizeof(buf).
826 * \param[in] bssap_le Decoded BSSAP-LE PDU data.
827 * \returns number of chars that would be written, like snprintf().
828 */
829int osmo_bssap_le_pdu_to_str_buf(char *buf, size_t buflen, const struct bssap_le_pdu *bssap_le)
830{
831 struct osmo_strbuf sb = { .buf = buf, .len = buflen };
832 const struct bssmap_le_pdu *bssmap_le;
833
834 switch (bssap_le->discr) {
835 case BSSAP_LE_MSG_DISCR_BSSMAP_LE:
836 bssmap_le = &bssap_le->bssmap_le;
837 OSMO_STRBUF_PRINTF(sb, "BSSMAP-LE %s", osmo_bssmap_le_msgt_name(bssmap_le->msg_type));
838 switch (bssmap_le->msg_type) {
839 case BSSMAP_LE_MSGT_PERFORM_LOC_REQ:
840 if (bssmap_le->perform_loc_req.apdu_present)
841 OSMO_STRBUF_PRINTF(sb, " with BSSLAP %s",
842 osmo_bsslap_msgt_name(bssmap_le->perform_loc_req.apdu.msg_type));
843 break;
844
845 case BSSMAP_LE_MSGT_CONN_ORIENTED_INFO:
846 OSMO_STRBUF_PRINTF(sb, " with BSSLAP %s",
847 osmo_bsslap_msgt_name(bssmap_le->conn_oriented_info.apdu.msg_type));
848 break;
849
850 default:
851 break;
852 }
853 break;
854 default:
855 OSMO_STRBUF_PRINTF(sb, "BSSAP-LE discr %d not implemented", bssap_le->discr);
856 break;
857 }
858
859 return sb.chars_needed;
860}
861
862/*! Return a human readable string describing a BSSAP-LE PDU.
863 * \param[in] ctx Talloc context to allocate string buffer from.
864 * \param[in] bssap_le Decoded BSSAP-LE PDU data.
865 * \returns string.
866 */
867char *osmo_bssap_le_pdu_to_str_c(void *ctx, const struct bssap_le_pdu *bssap_le)
868{
869 OSMO_NAME_C_IMPL(ctx, 32, "ERROR", osmo_bssap_le_pdu_to_str_buf, bssap_le)
870}
871
872/*! @} */