blob: 70ded13f0c92398cd72d4e7ef8424382d8202459 [file] [log] [blame]
Neels Hofmeyrc6848f42020-09-18 18:00:50 +02001/* 3GPP TS 48.071 BSSLAP 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 Hofmeyrc6848f42020-09-18 18:00:50 +020020 */
21
22#include <errno.h>
23
24#include <osmocom/core/msgb.h>
25#include <osmocom/gsm/bsslap.h>
26#include <osmocom/gsm/tlv.h>
27
28#include <osmocom/core/logging.h>
29
30/*! \addtogroup bsslap
31 * @{
32 * \file bsslap.c
33 * Message encoding and decoding for 3GPP TS 48.071 BSSLAP protocol.
34 */
35
36static const struct tlv_definition osmo_bsslap_tlvdef = {
37 .def = {
38 [BSSLAP_IEI_TA] = { TLV_TYPE_TV },
39 [BSSLAP_IEI_CELL_ID] = { TLV_TYPE_FIXED, 2 },
40 [BSSLAP_IEI_CHAN_DESC] = { TLV_TYPE_FIXED, 3 },
41 [BSSLAP_IEI_MEAS_REP] = { TLV_TYPE_TLV },
42 [BSSLAP_IEI_CAUSE] = { TLV_TYPE_TV },
43 [BSSLAP_IEI_RRLP_FLAG] = { TLV_TYPE_TV },
44 [BSSLAP_IEI_RRLP] = { TLV_TYPE_TLV },
45 [BSSLAP_IEI_CELL_ID_LIST] = { TLV_TYPE_TLV },
46 [BSSLAP_IEI_ENH_MEAS_REP] = { TLV_TYPE_TLV },
47 [BSSLAP_IEI_LAC] = { TLV_TYPE_TLV },
48 [BSSLAP_IEI_FREQ_LIST] = { TLV_TYPE_TLV },
49 [BSSLAP_IEI_MS_POWER] = { TLV_TYPE_TV },
50 [BSSLAP_IEI_DELTA_TIMER] = { TLV_TYPE_TV },
51 [BSSLAP_IEI_SERVING_CELL_ID] = { TLV_TYPE_TLV },
52 [BSSLAP_IEI_ENCR_KEY] = { TLV_TYPE_FIXED, 8 },
53 [BSSLAP_IEI_CIPH_MODE_SET] = { TLV_TYPE_TV },
54 [BSSLAP_IEI_CHAN_MODE] = { TLV_TYPE_TV, 2 },
55 [BSSLAP_IEI_MR_CONFIG] = { TLV_TYPE_TLV },
56 [BSSLAP_IEI_POLLING_REPETITION] = { TLV_TYPE_TV },
57 [BSSLAP_IEI_PACKET_CHAN_DESC] = { TLV_TYPE_FIXED, 4 },
58 [BSSLAP_IEI_TLLI] = { TLV_TYPE_FIXED, 4 },
59 [BSSLAP_IEI_TFI] = { TLV_TYPE_TLV },
60 [BSSLAP_IEI_TBF_START_TIME] = { TLV_TYPE_FIXED, 2 },
61 [BSSLAP_IEI_PWRUP_START_TIME] = { TLV_TYPE_TLV },
62 [BSSLAP_IEI_LONG_ENCR_KEY] = { TLV_TYPE_FIXED, 16 },
63 [BSSLAP_IEI_CONCUR_POS_PROC_F] = { TLV_TYPE_TV },
64 },
65};
66
67#define DEC_ERR(RC, MSG_TYPE, IEI, CAUSE, fmt, args...) do { \
68 if (err && !*err) { \
69 *err = talloc_zero(err_ctx, struct osmo_bsslap_err); \
70 **err = (struct osmo_bsslap_err){ \
71 .rc = (RC), \
72 .msg_type = (MSG_TYPE), \
73 .iei = (IEI), \
74 .cause = (CAUSE), \
75 .logmsg = talloc_asprintf(*err, "Error decoding BSSLAP%s%s%s%s%s: " fmt, \
76 (MSG_TYPE) >= 0 ? " " : "", \
77 (MSG_TYPE) >= 0 ? osmo_bsslap_msgt_name(MSG_TYPE) : "", \
78 (IEI) >= 0 ? ": " : "", \
79 (IEI) >= 0 ? osmo_bsslap_iei_name(IEI) : "", \
80 (IEI) >= 0 ? " IE" : "", \
81##args), \
82 }; \
83 } \
84 return RC; \
85 } while(0)
86
87static void osmo_bsslap_ie_enc_cell_id(struct msgb *msg, uint16_t cell_id)
88{
89 msgb_put_u8(msg, BSSLAP_IEI_CELL_ID);
90 msgb_put_u16(msg, cell_id);
91}
92
93static int osmo_bsslap_ie_dec_cell_id(uint16_t *cell_id,
94 enum bsslap_msgt msgt, enum bsslap_iei iei,
95 struct osmo_bsslap_err **err, void *err_ctx,
96 const uint8_t *data, size_t len)
97{
98 if (len != 2)
99 DEC_ERR(-EINVAL, msgt, iei, LCS_CAUSE_UNSPECIFIED, "Expected 2 bytes, got %zu", len);
100 *cell_id = osmo_load16be(data);
101 return 0;
102}
103
104static void osmo_bsslap_ie_enc_ta(struct msgb *msg, uint8_t ta)
105{
106 msgb_put_u8(msg, BSSLAP_IEI_TA);
107 msgb_put_u8(msg, ta);
108}
109
110static int osmo_bsslap_ie_dec_ta(uint8_t *ta,
111 enum bsslap_msgt msgt, enum bsslap_iei iei,
112 struct osmo_bsslap_err **err, void *err_ctx,
113 const uint8_t *data, size_t len)
114{
115 if (len != 1)
116 DEC_ERR(-EINVAL, msgt, iei, LCS_CAUSE_UNSPECIFIED, "Expected 1 byte, got %zu", len);
117 *ta = data[0];
118 return 0;
119}
120
121static void osmo_bsslap_ie_enc_cause(struct msgb *msg, enum bsslap_cause cause)
122{
123 msgb_put_u8(msg, BSSLAP_IEI_CAUSE);
124 msgb_put_u8(msg, cause);
125}
126
127static int osmo_bsslap_ie_dec_cause(enum bsslap_cause *cause,
128 enum bsslap_msgt msgt, enum bsslap_iei iei,
129 struct osmo_bsslap_err **err, void *err_ctx,
130 const uint8_t *data, size_t len)
131{
132 if (len != 1)
133 DEC_ERR(-EINVAL, msgt, iei, LCS_CAUSE_UNSPECIFIED, "Expected 1 byte, got %zu", len);
134 *cause = data[0];
135 return 0;
136}
137
138static void osmo_bsslap_ie_enc_chan_desc(struct msgb *msg, const struct gsm48_chan_desc *chan_desc)
139{
140 struct gsm48_chan_desc *put_chan_desc;
141 msgb_put_u8(msg, BSSLAP_IEI_CHAN_DESC);
142 put_chan_desc = (void*)msgb_put(msg, sizeof(*chan_desc));
143 *put_chan_desc = *chan_desc;
144}
145
146static int osmo_bsslap_ie_dec_chan_desc(struct gsm48_chan_desc *chan_desc,
147 enum bsslap_msgt msgt, enum bsslap_iei iei,
148 struct osmo_bsslap_err **err, void *err_ctx,
149 const uint8_t *data, size_t len)
150{
151 if (len != sizeof(*chan_desc))
152 DEC_ERR(-EINVAL, msgt, iei, LCS_CAUSE_UNSPECIFIED, "Expected %zu bytes, got %zu",
153 sizeof(*chan_desc), len);
154 *chan_desc = *(struct gsm48_chan_desc*)data;
155 return 0;
156}
157
158/*! Encode BSSLAP PDU and append to msgb (3GPP TS 48.071).
159 * \param[out] msg msgb to append to.
160 * \param[in] pdu PDU data to encode.
161 * \return number of bytes written, negative on error.
162 */
163int osmo_bsslap_enc(struct msgb *msg, const struct bsslap_pdu *pdu)
164{
165 uint8_t *old_tail = msg->tail;
166
167 msgb_put_u8(msg, pdu->msg_type);
168
169 switch (pdu->msg_type) {
170 case BSSLAP_MSGT_TA_REQUEST:
171 /* The TA Request message contains only the message type. */
172 break;
173
174 case BSSLAP_MSGT_TA_RESPONSE:
175 osmo_bsslap_ie_enc_cell_id(msg, pdu->ta_response.cell_id);
176 osmo_bsslap_ie_enc_ta(msg, pdu->ta_response.ta);
177 break;
178
179 case BSSLAP_MSGT_REJECT:
180 osmo_bsslap_ie_enc_cause(msg, pdu->reject);
181 break;
182
183 case BSSLAP_MSGT_RESET:
184 osmo_bsslap_ie_enc_cell_id(msg, pdu->reset.cell_id);
185 osmo_bsslap_ie_enc_ta(msg, pdu->reset.ta);
186 osmo_bsslap_ie_enc_chan_desc(msg, &pdu->reset.chan_desc);
187 osmo_bsslap_ie_enc_cause(msg, pdu->reset.cause);
188 break;
189
190 case BSSLAP_MSGT_ABORT:
191 osmo_bsslap_ie_enc_cause(msg, pdu->abort);
192 break;
193
194 case BSSLAP_MSGT_TA_LAYER3:
195 osmo_bsslap_ie_enc_ta(msg, pdu->ta_layer3.ta);
196 break;
197
198 default:
199 return -ENOTSUP;
200 }
201 return (msg->tail - old_tail);
202}
203
204/*! Decode BSSLAP PDU (3GPP TS 48.071).
205 * \param[out] pdu Write decoded values here.
206 * \param[out] err Returned pointer to error info, dynamically allocated; NULL to not return any.
207 * \param[in] err_ctx Talloc context to allocate err from, if required.
208 * \param[in] data Pointer to BSSLAP PDU raw data.
209 * \param[in] len Data length to decode.
210 * \return 0 on success, negative on error.
211 */
212int osmo_bsslap_dec(struct bsslap_pdu *pdu,
213 struct osmo_bsslap_err **err, void *err_ctx,
214 const uint8_t *data, size_t len)
215{
216 const uint8_t *ies_start;
217 int ies_len;
218 struct tlv_parsed tp;
219
Vadim Yanitskiy7b9b3072023-02-25 05:52:37 +0700220 memset(pdu, 0x00, sizeof(*pdu));
Neels Hofmeyrc6848f42020-09-18 18:00:50 +0200221 if (err)
222 *err = NULL;
223
224#define DEC_IE_MANDATORY(IEI, DEC_FUN, DEC_FUN_ARG) do { \
225 const struct tlv_p_entry *e; \
226 int rc; \
227 if (!(e = TLVP_GET(&tp, IEI))) \
228 DEC_ERR(-EINVAL, pdu->msg_type, IEI, LCS_CAUSE_DATA_MISSING_IN_REQ, "missing mandatory IE"); \
229 rc = DEC_FUN(DEC_FUN_ARG, pdu->msg_type, IEI, err, err_ctx, e->val, e->len); \
230 if (rc) \
231 DEC_ERR(rc, pdu->msg_type, IEI, LCS_CAUSE_UNSPECIFIED, "cannot parse IE"); \
232 } while (0)
233
234 if (len < 1)
235 DEC_ERR(-EINVAL, -1, -1, LCS_CAUSE_UNSPECIFIED, "PDU too short: %zu b", len);
236
237 pdu->msg_type = data[0];
238
239 if (pdu->msg_type == BSSLAP_MSGT_TA_REQUEST) {
240 /* The TA Request message contains only the message type. */
241 return 0;
242 }
243
244 ies_start = &data[1];
245 ies_len = len - 1;
246
247 if (tlv_parse2(&tp, 1, &osmo_bsslap_tlvdef, ies_start, ies_len, 0, 0) <= 0)
248 DEC_ERR(-EINVAL, pdu->msg_type, -1, LCS_CAUSE_UNSPECIFIED, "failed to parse TLV structure");
249
250 switch (pdu->msg_type) {
251
252 case BSSLAP_MSGT_TA_RESPONSE:
253 DEC_IE_MANDATORY(BSSLAP_IEI_CELL_ID, osmo_bsslap_ie_dec_cell_id, &pdu->ta_response.cell_id);
254 DEC_IE_MANDATORY(BSSLAP_IEI_TA, osmo_bsslap_ie_dec_ta, &pdu->ta_response.ta);
255 return 0;
256
257 case BSSLAP_MSGT_REJECT:
258 DEC_IE_MANDATORY(BSSLAP_IEI_CAUSE, osmo_bsslap_ie_dec_cause, &pdu->reject);
259 return 0;
260
261 case BSSLAP_MSGT_RESET:
262 DEC_IE_MANDATORY(BSSLAP_IEI_CELL_ID, osmo_bsslap_ie_dec_cell_id, &pdu->reset.cell_id);
263 DEC_IE_MANDATORY(BSSLAP_IEI_TA, osmo_bsslap_ie_dec_ta, &pdu->reset.ta);
264 DEC_IE_MANDATORY(BSSLAP_IEI_CHAN_DESC, osmo_bsslap_ie_dec_chan_desc, &pdu->reset.chan_desc);
265 DEC_IE_MANDATORY(BSSLAP_IEI_CAUSE, osmo_bsslap_ie_dec_cause, &pdu->reset.cause);
266 return 0;
267
268 case BSSLAP_MSGT_ABORT:
269 DEC_IE_MANDATORY(BSSLAP_IEI_CAUSE, osmo_bsslap_ie_dec_cause, &pdu->abort);
270 return 0;
271
272 case BSSLAP_MSGT_TA_LAYER3:
273 DEC_IE_MANDATORY(BSSLAP_IEI_TA, osmo_bsslap_ie_dec_ta, &pdu->ta_layer3.ta);
274 return 0;
275
276 default:
277 DEC_ERR(-EINVAL, pdu->msg_type, -1, LCS_CAUSE_UNSPECIFIED, "Unsupported message type");
278 }
279}
280
281const struct value_string osmo_bsslap_msgt_names[] = {
282 { BSSLAP_MSGT_TA_REQUEST, "TA Request" },
283 { BSSLAP_MSGT_TA_RESPONSE, "TA Response" },
284 { BSSLAP_MSGT_REJECT, "Reject" },
285 { BSSLAP_MSGT_RESET, "Reset" },
286 { BSSLAP_MSGT_ABORT, "Abort" },
287 { BSSLAP_MSGT_TA_LAYER3, "TA Layer3" },
288 { BSSLAP_MSGT_MS_POS_CMD, "MS Position Command" },
289 { BSSLAP_MSGT_MS_POS_RESP, "MS Position Response" },
290 { BSSLAP_MSGT_UTDOA_REQ, "U-TDOA Request" },
291 { BSSLAP_MSGT_UTDOA_RESP, "U-TDOA Response" },
292 {}
293};
294
295const struct value_string osmo_bsslap_iei_names[] = {
296 { BSSLAP_IEI_TA, "Timing Advance" },
297 { BSSLAP_IEI_CELL_ID, "Cell Identity" },
298 { BSSLAP_IEI_CHAN_DESC, "Channel Description" },
299 { BSSLAP_IEI_MEAS_REP, "Measurement Report" },
300 { BSSLAP_IEI_CAUSE, "Cause" },
301 { BSSLAP_IEI_RRLP_FLAG, "RRLP Flag" },
302 { BSSLAP_IEI_RRLP, "RRLP" },
303 { BSSLAP_IEI_CELL_ID_LIST, "Cell Identity List" },
304 { BSSLAP_IEI_ENH_MEAS_REP, "Enhanced Measurement Report" },
305 { BSSLAP_IEI_LAC, "Location Area Code" },
306 { BSSLAP_IEI_FREQ_LIST, "Frequency List" },
307 { BSSLAP_IEI_MS_POWER, "MS Power" },
308 { BSSLAP_IEI_DELTA_TIMER, "Delta Timer" },
309 { BSSLAP_IEI_SERVING_CELL_ID, "Serving Cell Identifier" },
310 { BSSLAP_IEI_ENCR_KEY, "Encryption Key" },
311 { BSSLAP_IEI_CIPH_MODE_SET, "Cipher Mode Setting" },
312 { BSSLAP_IEI_CHAN_MODE, "Channel Mode" },
313 { BSSLAP_IEI_MR_CONFIG, "MultiRate Configuration" },
314 { BSSLAP_IEI_POLLING_REPETITION, "Polling Repetition" },
315 { BSSLAP_IEI_PACKET_CHAN_DESC, "Packet Channel Description" },
316 { BSSLAP_IEI_TLLI, "TLLI" },
317 { BSSLAP_IEI_TFI, "TFI" },
318 { BSSLAP_IEI_TBF_START_TIME, "TBF Starting Time" },
319 { BSSLAP_IEI_PWRUP_START_TIME, "Powerup Starting Time" },
320 { BSSLAP_IEI_LONG_ENCR_KEY, "Long Encryption Key" },
321 { BSSLAP_IEI_CONCUR_POS_PROC_F, "Concurrent Positioning Flag" },
322 {}
323};
324
325/*! @} */