blob: 591ff251f8806827fd84cbb300c12996a58abb39 [file] [log] [blame]
Harald Welte07958e42019-05-03 09:39:10 +02001/*
2 * Copyright (C) 2019 Harald Welte <laforge@gnumonks.org>
3 *
4 * All Rights Reserved
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#include "config.h"
24
25#include <errno.h>
26
27#include <sys/types.h>
28
29#include <osmocom/core/linuxlist.h>
30#include <osmocom/core/msgb.h>
31
32#include <osmocom/gsm/tlv.h>
33#include <osmocom/gsm/cbsp.h>
34#include <osmocom/gsm/gsm0808_utils.h>
35
Harald Weltef72155a2019-06-15 23:05:19 +020036const __thread char *osmo_cbsp_errstr;
37
Harald Welte07958e42019-05-03 09:39:10 +020038struct msgb *osmo_cbsp_msgb_alloc(void *ctx, const char *name)
39{
40 /* make the messages rather large as the cell lists can be long! */
41 return msgb_alloc_headroom_c(ctx, 65535, 16, name);
42}
43
44/***********************************************************************
45 * IE Encoding
46 ***********************************************************************/
47
48/* 8.2.6 Cell List */
49static void msgb_put_cbsp_cell_list(struct msgb *msg, const struct osmo_cbsp_cell_list *cl)
50{
51 const struct osmo_cbsp_cell_ent *ent;
52 uint8_t *lenptr;
53
54 /* put tag; reserve space for length; put discriminator */
55 msgb_put_u8(msg, CBSP_IEI_CELL_LIST);
56 lenptr = msgb_put(msg, sizeof(uint16_t));
57 msgb_put_u8(msg, cl->id_discr);
58 /* put list elements */
59 llist_for_each_entry(ent, &cl->list, list) {
60 gsm0808_msgb_put_cell_id_u(msg, cl->id_discr, &ent->cell_id);
61 }
62 /* update IE length */
63 osmo_store16be(msg->tail - (lenptr+2), lenptr);
64}
65
66/* 8.2.11 Failure List (discriminator per entry) */
67static void msgb_put_cbsp_fail_list(struct msgb *msg, const struct llist_head *fl)
68{
69 const struct osmo_cbsp_fail_ent *ent;
70 uint8_t *lenptr;
71
72 /* put tag; reserve space for length; put discriminator */
73 msgb_put_u8(msg, CBSP_IEI_FAILURE_LIST);
74 lenptr = msgb_put(msg, sizeof(uint16_t));
75 /* put list elements */
76 llist_for_each_entry(ent, fl, list) {
77 msgb_put_u8(msg, ent->id_discr);
78 gsm0808_msgb_put_cell_id_u(msg, ent->id_discr, &ent->cell_id);
79 msgb_put_u8(msg, ent->cause);
80 }
81 /* update IE length */
82 osmo_store16be(msg->tail - (lenptr+2), lenptr);
83}
84
85/* 8.2.12 Radio Resource Loading List */
86static void msgb_put_cbsp_loading_list(struct msgb *msg, const struct osmo_cbsp_loading_list *ll)
87{
88 const struct osmo_cbsp_loading_ent *ent;
89 uint8_t *lenptr;
90
91 /* put tag; reserve space for length; put discriminator */
92 msgb_put_u8(msg, CBSP_IEI_RR_LOADING_LIST);
93 lenptr = msgb_put(msg, sizeof(uint16_t));
94 msgb_put_u8(msg, ll->id_discr);
95 /* put list elements */
96 llist_for_each_entry(ent, &ll->list, list) {
97 gsm0808_msgb_put_cell_id_u(msg, ll->id_discr, &ent->cell_id);
98 msgb_put_u8(msg, ent->load[0]);
99 msgb_put_u8(msg, ent->load[1]);
100 }
101 /* update IE length */
102 osmo_store16be(msg->tail - (lenptr+2), lenptr);
103}
104
105/* 8.2.10 Completed List */
106static void msgb_put_cbsp_num_compl_list(struct msgb *msg, const struct osmo_cbsp_num_compl_list *cl)
107{
108 const struct osmo_cbsp_num_compl_ent *ent;
109 uint8_t *lenptr;
110
111 /* put tag; reserve space for length; put discriminator */
112 msgb_put_u8(msg, CBSP_IEI_NUM_BCAST_COMPL_LIST);
113 lenptr = msgb_put(msg, sizeof(uint16_t));
114 msgb_put_u8(msg, cl->id_discr);
115 /* put list elements */
116 llist_for_each_entry(ent, &cl->list, list) {
117 gsm0808_msgb_put_cell_id_u(msg, cl->id_discr, &ent->cell_id);
118 msgb_put_u16(msg, ent->num_compl);
119 msgb_put_u8(msg, ent->num_bcast_info);
120 }
121 /* update IE length */
122 osmo_store16be(msg->tail - (lenptr+2), lenptr);
123}
124
125static int encode_wperiod(uint32_t secs)
126{
127 if (secs == 0xffffffff)
128 return 0; /* infinite */
129 if (secs <= 10)
130 return secs;
131 if (secs <= 30)
132 return (secs-10)/2;
133 if (secs <= 120)
134 return (secs-30)/5;
135 if (secs <= 600)
136 return (secs-120)/10;
137 if (secs <= 60*60)
138 return (secs-600)/30;
Harald Weltef72155a2019-06-15 23:05:19 +0200139 osmo_cbsp_errstr = "warning period out of range";
Harald Welte07958e42019-05-03 09:39:10 +0200140 return -1;
141}
142
143/***********************************************************************
144 * Message Encoding
145 ***********************************************************************/
146
147/* 8.1.3.1 WRITE REPLACE */
148static int cbsp_enc_write_repl(struct msgb *msg, const struct osmo_cbsp_write_replace *in)
149{
150 msgb_tv16_put(msg, CBSP_IEI_MSG_ID, in->msg_id);
151 msgb_tv16_put(msg, CBSP_IEI_NEW_SERIAL_NR, in->new_serial_nr);
152 if (in->old_serial_nr)
153 msgb_tv16_put(msg, CBSP_IEI_OLD_SERIAL_NR, *in->old_serial_nr);
154 msgb_put_cbsp_cell_list(msg, &in->cell_list);
155 if (in->is_cbs) {
156 int num_of_pages = llist_count(&in->u.cbs.msg_content);
157 struct osmo_cbsp_content *ce;
Harald Weltef72155a2019-06-15 23:05:19 +0200158 if (num_of_pages == 0 || num_of_pages > 15) {
159 osmo_cbsp_errstr = "invalid number of pages";
Harald Welte07958e42019-05-03 09:39:10 +0200160 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +0200161 }
Harald Welte07958e42019-05-03 09:39:10 +0200162 msgb_tv_put(msg, CBSP_IEI_CHANNEL_IND, in->u.cbs.channel_ind);
163 msgb_tv_put(msg, CBSP_IEI_CATEGORY, in->u.cbs.category);
164 msgb_tv16_put(msg, CBSP_IEI_REP_PERIOD, in->u.cbs.rep_period);
165 msgb_tv16_put(msg, CBSP_IEI_NUM_BCAST_REQ, in->u.cbs.num_bcast_req);
166 msgb_tv_put(msg, CBSP_IEI_NUM_OF_PAGES, num_of_pages);
167 msgb_tv_put(msg, CBSP_IEI_DCS, in->u.cbs.dcs);
168 llist_for_each_entry(ce, &in->u.cbs.msg_content, list) {
169 uint8_t *out;
170 /* cannot use msgb_tlv_put() as 'len' isn't actually the length of
171 * the data field */
172 msgb_put_u8(msg, CBSP_IEI_MSG_CONTENT);
173 msgb_put_u8(msg, ce->user_len);
174 out = msgb_put(msg, sizeof(ce->data));
175 memcpy(out, ce->data, sizeof(ce->data));
176 }
177 } else {
178 int wperiod = encode_wperiod(in->u.emergency.warning_period);
179 if (wperiod < 0)
180 return -EINVAL;
181 msgb_tv_put(msg, CBSP_IEI_EMERG_IND, in->u.emergency.indicator);
182 msgb_tv16_put(msg, CBSP_IEI_WARN_TYPE, in->u.emergency.warning_type);
183 msgb_tlv_put(msg, CBSP_IEI_WARN_SEC_INFO, sizeof(in->u.emergency.warning_sec_info),
184 in->u.emergency.warning_sec_info);
185 msgb_tv_put(msg, CBSP_IEI_WARNING_PERIOD, wperiod);
186 }
187 return 0;
188}
189
190/* 8.1.3.2 WRITE REPLACE COMPLETE*/
191static int cbsp_enc_write_repl_compl(struct msgb *msg, const struct osmo_cbsp_write_replace_complete *in)
192{
193 msgb_tv16_put(msg, CBSP_IEI_MSG_ID, in->msg_id);
194 msgb_tv16_put(msg, CBSP_IEI_NEW_SERIAL_NR, in->new_serial_nr);
195 if (in->old_serial_nr)
196 msgb_tv16_put(msg, CBSP_IEI_OLD_SERIAL_NR, *in->old_serial_nr);
197
198 if (!llist_empty(&in->num_compl_list.list))
199 msgb_put_cbsp_num_compl_list(msg, &in->num_compl_list);
200 if (!llist_empty(&in->cell_list.list))
201 msgb_put_cbsp_cell_list(msg, &in->cell_list);
202 if (in->channel_ind)
203 msgb_tv_put(msg, CBSP_IEI_CHANNEL_IND, *in->channel_ind);
204 return 0;
205}
206
207/* 8.1.3.3 WRITE REPLACE FAILURE */
208static int cbsp_enc_write_repl_fail(struct msgb *msg, const struct osmo_cbsp_write_replace_failure *in)
209{
210 msgb_tv16_put(msg, CBSP_IEI_MSG_ID, in->msg_id);
211 msgb_tv16_put(msg, CBSP_IEI_NEW_SERIAL_NR, in->new_serial_nr);
212 if (in->old_serial_nr)
213 msgb_tv16_put(msg, CBSP_IEI_OLD_SERIAL_NR, *in->old_serial_nr);
214
215 msgb_put_cbsp_fail_list(msg, &in->fail_list);
216 if (!llist_empty(&in->num_compl_list.list))
217 msgb_put_cbsp_num_compl_list(msg, &in->num_compl_list);
218 if (!llist_empty(&in->cell_list.list))
219 msgb_put_cbsp_cell_list(msg, &in->cell_list);
220 if (in->channel_ind)
221 msgb_tv_put(msg, CBSP_IEI_CHANNEL_IND, *in->channel_ind);
222 return 0;
223}
224
225/* 8.1.3.4 KILL */
226static int cbsp_enc_kill(struct msgb *msg, const struct osmo_cbsp_kill *in)
227{
228 msgb_tv16_put(msg, CBSP_IEI_MSG_ID, in->msg_id);
229 msgb_tv16_put(msg, CBSP_IEI_OLD_SERIAL_NR, in->old_serial_nr);
230 msgb_put_cbsp_cell_list(msg, &in->cell_list);
231 if (in->channel_ind)
232 msgb_tv_put(msg, CBSP_IEI_CHANNEL_IND, *in->channel_ind);
233 return 0;
234}
235
236/* 8.1.3.5 KILL COMPLETE */
237static int cbsp_enc_kill_compl(struct msgb *msg, const struct osmo_cbsp_kill_complete *in)
238{
239 msgb_tv16_put(msg, CBSP_IEI_MSG_ID, in->msg_id);
240 msgb_tv16_put(msg, CBSP_IEI_OLD_SERIAL_NR, in->old_serial_nr);
241 if (!llist_empty(&in->num_compl_list.list))
242 msgb_put_cbsp_num_compl_list(msg, &in->num_compl_list);
243 if (!llist_empty(&in->cell_list.list))
244 msgb_put_cbsp_cell_list(msg, &in->cell_list);
245 if (in->channel_ind)
246 msgb_tv_put(msg, CBSP_IEI_CHANNEL_IND, *in->channel_ind);
247 return 0;
248}
249
250/* 8.1.3.6 KILL FAILURE */
251static int cbsp_enc_kill_fail(struct msgb *msg, const struct osmo_cbsp_kill_failure *in)
252{
253 msgb_tv16_put(msg, CBSP_IEI_MSG_ID, in->msg_id);
254 msgb_tv16_put(msg, CBSP_IEI_OLD_SERIAL_NR, in->old_serial_nr);
255 msgb_put_cbsp_fail_list(msg, &in->fail_list);
256 if (!llist_empty(&in->num_compl_list.list))
257 msgb_put_cbsp_num_compl_list(msg, &in->num_compl_list);
258 if (!llist_empty(&in->cell_list.list))
259 msgb_put_cbsp_cell_list(msg, &in->cell_list);
260 if (in->channel_ind)
261 msgb_tv_put(msg, CBSP_IEI_CHANNEL_IND, *in->channel_ind);
262 return 0;
263}
264
265/* 8.1.3.7 LOAD QUERY */
266static int cbsp_enc_load_query(struct msgb *msg, const struct osmo_cbsp_load_query *in)
267{
268 msgb_put_cbsp_cell_list(msg, &in->cell_list);
269 msgb_tv_put(msg, CBSP_IEI_CHANNEL_IND, in->channel_ind);
270 return 0;
271}
272
273/* 8.1.3.8 LOAD QUERY COMPLETE */
274static int cbsp_enc_load_query_compl(struct msgb *msg, const struct osmo_cbsp_load_query_complete *in)
275{
276 msgb_put_cbsp_loading_list(msg, &in->loading_list);
277 msgb_tv_put(msg, CBSP_IEI_CHANNEL_IND, in->channel_ind);
278 return 0;
279}
280
281/* 8.1.3.9 LOAD QUERY FAILURE */
282static int cbsp_enc_load_query_fail(struct msgb *msg, const struct osmo_cbsp_load_query_failure *in)
283{
284 msgb_put_cbsp_fail_list(msg, &in->fail_list);
285 msgb_tv_put(msg, CBSP_IEI_CHANNEL_IND, in->channel_ind);
286 if (!llist_empty(&in->loading_list.list))
287 msgb_put_cbsp_loading_list(msg, &in->loading_list);
288 return 0;
289}
290
291/* 8.1.3.10 STATUS QUERY */
292static int cbsp_enc_msg_status_query(struct msgb *msg, const struct osmo_cbsp_msg_status_query *in)
293{
294 msgb_tv16_put(msg, CBSP_IEI_MSG_ID, in->msg_id);
295 msgb_tv16_put(msg, CBSP_IEI_OLD_SERIAL_NR, in->old_serial_nr);
296 msgb_put_cbsp_cell_list(msg, &in->cell_list);
297 msgb_tv_put(msg, CBSP_IEI_CHANNEL_IND, in->channel_ind);
298 return 0;
299}
300
301/* 8.1.3.11 STATUS QUERY COMPLETE */
302static int cbsp_enc_msg_status_query_compl(struct msgb *msg,
303 const struct osmo_cbsp_msg_status_query_complete *in)
304{
305 msgb_tv16_put(msg, CBSP_IEI_MSG_ID, in->msg_id);
306 msgb_tv16_put(msg, CBSP_IEI_OLD_SERIAL_NR, in->old_serial_nr);
307 msgb_put_cbsp_num_compl_list(msg, &in->num_compl_list);
308 msgb_tv_put(msg, CBSP_IEI_CHANNEL_IND, in->channel_ind);
309 return 0;
310}
311
312/* 8.1.3.12 STATUS QUERY FAILURE */
313static int cbsp_enc_msg_status_query_fail(struct msgb *msg,
314 const struct osmo_cbsp_msg_status_query_failure *in)
315{
316 msgb_tv16_put(msg, CBSP_IEI_MSG_ID, in->msg_id);
317 msgb_tv16_put(msg, CBSP_IEI_OLD_SERIAL_NR, in->old_serial_nr);
318 msgb_put_cbsp_fail_list(msg, &in->fail_list);
319 msgb_tv_put(msg, CBSP_IEI_CHANNEL_IND, in->channel_ind);
320 if (!llist_empty(&in->num_compl_list.list))
321 msgb_put_cbsp_num_compl_list(msg, &in->num_compl_list);
322 return 0;
323}
324
325/* 8.1.3.16 RESET */
326static int cbsp_enc_reset(struct msgb *msg, const struct osmo_cbsp_reset *in)
327{
328 msgb_put_cbsp_cell_list(msg, &in->cell_list);
329 return 0;
330}
331
332/* 8.1.3.17 RESET COMPLETE */
333static int cbsp_enc_reset_compl(struct msgb *msg, const struct osmo_cbsp_reset_complete *in)
334{
335 msgb_put_cbsp_cell_list(msg, &in->cell_list);
336 return 0;
337}
338
339/* 8.1.3.18 RESET FAILURE */
340static int cbsp_enc_reset_fail(struct msgb *msg, const struct osmo_cbsp_reset_failure *in)
341{
342 msgb_put_cbsp_fail_list(msg, &in->fail_list);
343 if (!llist_empty(&in->cell_list.list))
344 msgb_put_cbsp_cell_list(msg, &in->cell_list);
345 return 0;
346}
347
348/* 8.1.3.18a KEEP ALIVE */
349static int cbsp_enc_keep_alive(struct msgb *msg, const struct osmo_cbsp_keep_alive *in)
350{
351 msgb_tv_put(msg, CBSP_IEI_KEEP_ALIVE_REP_PERIOD, in->repetition_period);
352 return 0;
353}
354
355/* 8.1.3.18b KEEP ALIVE COMPLETE */
356static int cbsp_enc_keep_alive_compl(struct msgb *msg, const struct osmo_cbsp_keep_alive_complete *in)
357{
358 return 0;
359}
360
361/* 8.1.3.19 RESTART */
362static int cbsp_enc_restart(struct msgb *msg, const struct osmo_cbsp_restart *in)
363{
364 msgb_put_cbsp_cell_list(msg, &in->cell_list);
365 msgb_tv_put(msg, CBSP_IEI_BCAST_MSG_TYPE, in->bcast_msg_type);
366 msgb_tv_put(msg, CBSP_IEI_RECOVERY_IND, in->recovery_ind);
367 return 0;
368}
369
370/* 8.1.3.20 FAILURE */
371static int cbsp_enc_failure(struct msgb *msg, const struct osmo_cbsp_failure *in)
372{
373 msgb_put_cbsp_fail_list(msg, &in->fail_list);
374 msgb_tv_put(msg, CBSP_IEI_BCAST_MSG_TYPE, in->bcast_msg_type);
375 return 0;
376}
377
378/* 8.1.3.21 ERROR INDICATION */
379static int cbsp_enc_error_ind(struct msgb *msg, const struct osmo_cbsp_error_ind *in)
380{
381 msgb_tv_put(msg, CBSP_IEI_CAUSE, in->cause);
382 if (in->msg_id)
383 msgb_tv16_put(msg, CBSP_IEI_MSG_ID, *in->msg_id);
384 if (in->new_serial_nr)
385 msgb_tv16_put(msg, CBSP_IEI_NEW_SERIAL_NR, *in->new_serial_nr);
386 if (in->old_serial_nr)
387 msgb_tv16_put(msg, CBSP_IEI_OLD_SERIAL_NR, *in->old_serial_nr);
388 if (in->channel_ind)
389 msgb_tv_put(msg, CBSP_IEI_CHANNEL_IND, *in->channel_ind);
390 return 0;
391}
392
393/*! Encode a CBSP message from the decoded/parsed structure representation to binary PDU.
394 * \param[in] ctx talloc context from which to allocate returned msgb.
395 * \param[in] in decoded CBSP message which is to be encoded. Ownership not transferred.
396 * \return callee-allocated message buffer containing binary CBSP PDU; NULL on error */
397struct msgb *osmo_cbsp_encode(void *ctx, const struct osmo_cbsp_decoded *in)
398{
399 struct msgb *msg = osmo_cbsp_msgb_alloc(ctx, __func__);
400 unsigned int len;
401 int rc;
402
Harald Weltef72155a2019-06-15 23:05:19 +0200403 osmo_cbsp_errstr = NULL;
404
Harald Welte07958e42019-05-03 09:39:10 +0200405 if (!msg)
406 return NULL;
407
408 switch (in->msg_type) {
409 case CBSP_MSGT_WRITE_REPLACE:
410 rc = cbsp_enc_write_repl(msg, &in->u.write_replace);
411 break;
412 case CBSP_MSGT_WRITE_REPLACE_COMPL:
413 rc = cbsp_enc_write_repl_compl(msg, &in->u.write_replace_compl);
414 break;
415 case CBSP_MSGT_WRITE_REPLACE_FAIL:
416 rc = cbsp_enc_write_repl_fail(msg, &in->u.write_replace_fail);
417 break;
418 case CBSP_MSGT_KILL:
419 rc = cbsp_enc_kill(msg, &in->u.kill);
420 break;
421 case CBSP_MSGT_KILL_COMPL:
422 rc = cbsp_enc_kill_compl(msg, &in->u.kill_compl);
423 break;
424 case CBSP_MSGT_KILL_FAIL:
425 rc = cbsp_enc_kill_fail(msg, &in->u.kill_fail);
426 break;
427 case CBSP_MSGT_LOAD_QUERY:
428 rc = cbsp_enc_load_query(msg, &in->u.load_query);
429 break;
430 case CBSP_MSGT_LOAD_QUERY_COMPL:
431 rc = cbsp_enc_load_query_compl(msg, &in->u.load_query_compl);
432 break;
433 case CBSP_MSGT_LOAD_QUERY_FAIL:
434 rc = cbsp_enc_load_query_fail(msg, &in->u.load_query_fail);
435 break;
436 case CBSP_MSGT_MSG_STATUS_QUERY:
437 rc = cbsp_enc_msg_status_query(msg, &in->u.msg_status_query);
438 break;
439 case CBSP_MSGT_MSG_STATUS_QUERY_COMPL:
440 rc = cbsp_enc_msg_status_query_compl(msg, &in->u.msg_status_query_compl);
441 break;
442 case CBSP_MSGT_MSG_STATUS_QUERY_FAIL:
443 rc = cbsp_enc_msg_status_query_fail(msg, &in->u.msg_status_query_fail);
444 break;
445 case CBSP_MSGT_RESET:
446 rc = cbsp_enc_reset(msg, &in->u.reset);
447 break;
448 case CBSP_MSGT_RESET_COMPL:
449 rc = cbsp_enc_reset_compl(msg, &in->u.reset_compl);
450 break;
451 case CBSP_MSGT_RESET_FAIL:
452 rc = cbsp_enc_reset_fail(msg, &in->u.reset_fail);
453 break;
454 case CBSP_MSGT_RESTART:
455 rc = cbsp_enc_restart(msg, &in->u.restart);
456 break;
457 case CBSP_MSGT_FAILURE:
458 rc = cbsp_enc_failure(msg, &in->u.failure);
459 break;
460 case CBSP_MSGT_ERROR_IND:
461 rc = cbsp_enc_error_ind(msg, &in->u.error_ind);
462 break;
463 case CBSP_MSGT_KEEP_ALIVE:
464 rc = cbsp_enc_keep_alive(msg, &in->u.keep_alive);
465 break;
466 case CBSP_MSGT_KEEP_ALIVE_COMPL:
467 rc = cbsp_enc_keep_alive_compl(msg, &in->u.keep_alive_compl);
468 break;
469 case CBSP_MSGT_SET_DRX:
470 case CBSP_MSGT_SET_DRX_COMPL:
471 case CBSP_MSGT_SET_DRX_FAIL:
Harald Weltef72155a2019-06-15 23:05:19 +0200472 osmo_cbsp_errstr = "message type not implemented";
Harald Welte07958e42019-05-03 09:39:10 +0200473 rc = -1;
474 break;
475 default:
Harald Weltef72155a2019-06-15 23:05:19 +0200476 osmo_cbsp_errstr = "message type not known in spec";
Harald Welte07958e42019-05-03 09:39:10 +0200477 rc = -1;
478 break;
479 }
480
481 if (rc < 0) {
482 msgb_free(msg);
483 return NULL;
484 }
485
486 /* push header in front */
487 len = msgb_length(msg);
488 msgb_push_u8(msg, len & 0xff);
489 msgb_push_u8(msg, (len >> 8) & 0xff);
490 msgb_push_u8(msg, (len >> 16) & 0xff);
491 msgb_push_u8(msg, in->msg_type);
492
493 return msg;
494}
495
496/***********************************************************************
497 * IE Decoding
498 ***********************************************************************/
499
500/* 8.2.6 Cell List */
501static int cbsp_decode_cell_list(struct osmo_cbsp_cell_list *cl, void *ctx,
502 const uint8_t *buf, unsigned int len)
503{
504 const uint8_t *cur = buf;
505 int rc;
506
507 cl->id_discr = *cur++;
508
509 while (cur < buf + len) {
510 struct osmo_cbsp_cell_ent *ent = talloc_zero(ctx, struct osmo_cbsp_cell_ent);
511 unsigned int len_remain = len - (cur - buf);
512 OSMO_ASSERT(ent);
513 rc = gsm0808_decode_cell_id_u(&ent->cell_id, cl->id_discr, cur, len_remain);
Harald Weltef72155a2019-06-15 23:05:19 +0200514 if (rc < 0) {
515 osmo_cbsp_errstr = "cell list: error decoding cell_id_union";
Harald Welte07958e42019-05-03 09:39:10 +0200516 return rc;
Harald Weltef72155a2019-06-15 23:05:19 +0200517 }
Harald Weltef2210032019-08-31 21:25:05 +0200518 cur += gsm0808_cell_id_size(cl->id_discr);
Harald Welte07958e42019-05-03 09:39:10 +0200519 llist_add_tail(&ent->list, &cl->list);
520 }
521 return 0;
522}
523
524/* 8.2.11 Failure List (discriminator per entry) */
525static int cbsp_decode_fail_list(struct llist_head *fl, void *ctx,
526 const uint8_t *buf, unsigned int len)
527{
528 const uint8_t *cur = buf;
529 int rc;
530
531 while (cur < buf + len) {
532 struct osmo_cbsp_fail_ent *ent = talloc_zero(ctx, struct osmo_cbsp_fail_ent);
533 unsigned int len_remain = len - (cur - buf);
534 OSMO_ASSERT(ent);
535 ent->id_discr = cur[0];
536 rc = gsm0808_decode_cell_id_u(&ent->cell_id, ent->id_discr, cur+1, len_remain-1);
Harald Weltef72155a2019-06-15 23:05:19 +0200537 if (rc < 0) {
538 osmo_cbsp_errstr = "fail list: error decoding cell_id_union";
Harald Welte07958e42019-05-03 09:39:10 +0200539 return rc;
Harald Weltef72155a2019-06-15 23:05:19 +0200540 }
Harald Weltef2210032019-08-31 21:25:05 +0200541 cur += gsm0808_cell_id_size(ent->id_discr);
Harald Welte07958e42019-05-03 09:39:10 +0200542 ent->cause = *cur++;
543 llist_add_tail(&ent->list, fl);
544 }
545 return 0;
546}
547
548/* 8.2.12 Radio Resource Loading List */
549static int cbsp_decode_loading_list(struct osmo_cbsp_loading_list *ll, void *ctx,
550 const uint8_t *buf, unsigned int len)
551{
552 const uint8_t *cur = buf;
553 int rc;
554
555 ll->id_discr = *cur++;
556 while (cur < buf + len) {
557 struct osmo_cbsp_loading_ent *ent = talloc_zero(ctx, struct osmo_cbsp_loading_ent);
558 unsigned int len_remain = len - (cur - buf);
559 OSMO_ASSERT(ent);
560 rc = gsm0808_decode_cell_id_u(&ent->cell_id, ll->id_discr, cur, len_remain);
Harald Weltef72155a2019-06-15 23:05:19 +0200561 if (rc < 0) {
562 osmo_cbsp_errstr = "load list: error decoding cell_id_union";
Harald Welte07958e42019-05-03 09:39:10 +0200563 return rc;
Harald Weltef72155a2019-06-15 23:05:19 +0200564 }
Harald Weltef2210032019-08-31 21:25:05 +0200565 cur += gsm0808_cell_id_size(ll->id_discr);
Harald Welte07958e42019-05-03 09:39:10 +0200566 if (cur + 2 > buf + len) {
567 talloc_free(ent);
Harald Weltef72155a2019-06-15 23:05:19 +0200568 osmo_cbsp_errstr = "load list: truncated IE";
Harald Welte07958e42019-05-03 09:39:10 +0200569 return -EINVAL;
570 }
571 ent->load[0] = *cur++;
572 ent->load[1] = *cur++;
573 llist_add_tail(&ent->list, &ll->list);
574 }
575 return 0;
576}
577
578/* 8.2.10 Completed List */
579static int cbsp_decode_num_compl_list(struct osmo_cbsp_num_compl_list *cl, void *ctx,
580 const uint8_t *buf, unsigned int len)
581{
582 const uint8_t *cur = buf;
583 int rc;
584
585 cl->id_discr = *cur++;
586 while (cur < buf + len) {
587 struct osmo_cbsp_num_compl_ent *ent = talloc_zero(ctx, struct osmo_cbsp_num_compl_ent);
588 unsigned int len_remain = len - (cur - buf);
589 OSMO_ASSERT(ent);
590 rc = gsm0808_decode_cell_id_u(&ent->cell_id, cl->id_discr, cur, len_remain);
Harald Weltef72155a2019-06-15 23:05:19 +0200591 if (rc < 0) {
592 osmo_cbsp_errstr = "completed list: error decoding cell_id_union";
Harald Welte07958e42019-05-03 09:39:10 +0200593 return rc;
Harald Weltef72155a2019-06-15 23:05:19 +0200594 }
Harald Weltef2210032019-08-31 21:25:05 +0200595 cur += gsm0808_cell_id_size(cl->id_discr);
Harald Welte07958e42019-05-03 09:39:10 +0200596 if (cur + 3 > buf + len) {
597 talloc_free(ent);
Harald Weltef72155a2019-06-15 23:05:19 +0200598 osmo_cbsp_errstr = "completed list: truncated IE";
Harald Welte07958e42019-05-03 09:39:10 +0200599 return -EINVAL;
600 }
601 ent->num_compl = osmo_load16be(cur); cur += 2;
602 ent->num_bcast_info = *cur++;
603 llist_add_tail(&ent->list, &cl->list);
604 }
605 return 0;
606}
607
608/* 8.2.25 */
609static uint32_t decode_wperiod(uint8_t in)
610{
611 if (in == 0x00)
612 return 0xffffffff; /* infinite */
613 if (in <= 10)
614 return in;
615 if (in <= 20)
616 return 10 + (in - 10)*2;
617 if (in <= 38)
618 return 30 + (in - 20)*5;
619 if (in <= 86)
620 return 120 + (in - 38)*10;
621 if (in <= 186)
622 return 600 + (in - 86)*30;
623 else
624 return 0;
625}
626
627
628/***********************************************************************
629 * Message Decoding
630 ***********************************************************************/
631
632/* 8.1.3.1 WRITE REPLACE */
633static int cbsp_dec_write_repl(struct osmo_cbsp_write_replace *out, const struct tlv_parsed *tp,
634 struct msgb *in, void *ctx)
635{
636 unsigned int i;
637
638 /* check for mandatory IEs */
639 if (!TLVP_PRESENT(tp, CBSP_IEI_MSG_ID) ||
640 !TLVP_PRESENT(tp, CBSP_IEI_NEW_SERIAL_NR) ||
Harald Weltef72155a2019-06-15 23:05:19 +0200641 !TLVP_PRESENT(tp, CBSP_IEI_CELL_LIST)) {
642 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +0200643 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +0200644 }
Harald Welte07958e42019-05-03 09:39:10 +0200645
646 out->msg_id = tlvp_val16be(tp, CBSP_IEI_MSG_ID);
647 out->new_serial_nr = tlvp_val16be(tp, CBSP_IEI_NEW_SERIAL_NR);
648 if (TLVP_PRESENT(tp, CBSP_IEI_OLD_SERIAL_NR)) {
649 out->old_serial_nr = talloc(ctx, uint16_t);
650 *out->old_serial_nr = tlvp_val16be(tp, CBSP_IEI_OLD_SERIAL_NR);
651 }
652
653 INIT_LLIST_HEAD(&out->cell_list.list);
654 cbsp_decode_cell_list(&out->cell_list, ctx, TLVP_VAL(tp, CBSP_IEI_CELL_LIST),
655 TLVP_LEN(tp, CBSP_IEI_CELL_LIST));
656
657 if (TLVP_PRESENT(tp, CBSP_IEI_CHANNEL_IND)) {
658 uint8_t num_of_pages;
659 INIT_LLIST_HEAD(&out->u.cbs.msg_content);
Harald Weltef72155a2019-06-15 23:05:19 +0200660 if (TLVP_PRESENT(tp, CBSP_IEI_EMERG_IND)) {
661 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +0200662 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +0200663 }
Harald Welte07958e42019-05-03 09:39:10 +0200664 if (!TLVP_PRESENT(tp, CBSP_IEI_CATEGORY) ||
665 !TLVP_PRESENT(tp, CBSP_IEI_REP_PERIOD) ||
666 !TLVP_PRESENT(tp, CBSP_IEI_NUM_BCAST_REQ) ||
667 !TLVP_PRESENT(tp, CBSP_IEI_NUM_OF_PAGES) ||
Harald Weltef72155a2019-06-15 23:05:19 +0200668 !TLVP_PRESENT(tp, CBSP_IEI_DCS)) {
669 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +0200670 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +0200671 }
Harald Welte07958e42019-05-03 09:39:10 +0200672 out->is_cbs = true;
673 out->u.cbs.channel_ind = *TLVP_VAL(tp, CBSP_IEI_CHANNEL_IND);
674 out->u.cbs.category = *TLVP_VAL(tp, CBSP_IEI_CATEGORY);
675 out->u.cbs.rep_period = tlvp_val16be(tp, CBSP_IEI_REP_PERIOD);
676 out->u.cbs.num_bcast_req = tlvp_val16be(tp, CBSP_IEI_NUM_BCAST_REQ);
677 num_of_pages = *TLVP_VAL(tp, CBSP_IEI_NUM_OF_PAGES);
678 if (num_of_pages < 1)
679 return -EINVAL;
680 /* parse pages */
681 for (i = 0; i < num_of_pages; i++) {
682 const uint8_t *ie = TLVP_VAL(&tp[i], CBSP_IEI_MSG_CONTENT);
683 struct osmo_cbsp_content *page;
Harald Weltef72155a2019-06-15 23:05:19 +0200684 if (!ie) {
685 osmo_cbsp_errstr = "insufficient message content IEs";
Harald Welte07958e42019-05-03 09:39:10 +0200686 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +0200687 }
Harald Welte07958e42019-05-03 09:39:10 +0200688 page = talloc_zero(ctx, struct osmo_cbsp_content);
689 OSMO_ASSERT(page);
690 page->user_len = *(ie-1); /* length byte before payload */
691 memcpy(page->data, ie, sizeof(page->data));
692 llist_add_tail(&page->list, &out->u.cbs.msg_content);
693 }
694 } else {
695 if (!TLVP_PRES_LEN(tp, CBSP_IEI_EMERG_IND, 1) ||
696 !TLVP_PRES_LEN(tp, CBSP_IEI_WARN_TYPE, 2) ||
697 !TLVP_PRES_LEN(tp, CBSP_IEI_WARN_SEC_INFO, 50) ||
Harald Weltef72155a2019-06-15 23:05:19 +0200698 !TLVP_PRES_LEN(tp, CBSP_IEI_WARNING_PERIOD, 1)) {
699 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +0200700 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +0200701 }
Harald Welte07958e42019-05-03 09:39:10 +0200702 out->u.emergency.indicator = *TLVP_VAL(tp, CBSP_IEI_EMERG_IND);
703 out->u.emergency.warning_type = tlvp_val16be(tp, CBSP_IEI_WARN_TYPE);
704 memcpy(&out->u.emergency.warning_sec_info, TLVP_VAL(tp, CBSP_IEI_WARN_SEC_INFO),
705 sizeof(out->u.emergency.warning_sec_info));
706 out->u.emergency.warning_period = decode_wperiod(*TLVP_VAL(tp, CBSP_IEI_WARNING_PERIOD));
707 }
708 return 0;
709}
710
711/* 8.1.3.2 WRITE REPLACE COMPLETE*/
712static int cbsp_dec_write_repl_compl(struct osmo_cbsp_write_replace_complete *out,
713 const struct tlv_parsed *tp, struct msgb *in, void *ctx)
714{
715 if (!TLVP_PRES_LEN(tp, CBSP_IEI_MSG_ID, 2) ||
Harald Weltef72155a2019-06-15 23:05:19 +0200716 !TLVP_PRES_LEN(tp, CBSP_IEI_NEW_SERIAL_NR, 2)) {
717 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +0200718 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +0200719 }
Harald Welte07958e42019-05-03 09:39:10 +0200720
721 out->msg_id = tlvp_val16be(tp, CBSP_IEI_MSG_ID);
722 out->new_serial_nr = tlvp_val16be(tp, CBSP_IEI_NEW_SERIAL_NR);
723 if (TLVP_PRES_LEN(tp, CBSP_IEI_OLD_SERIAL_NR, 2)) {
724 out->old_serial_nr = talloc(ctx, uint16_t);
725 *out->old_serial_nr = tlvp_val16be(tp, CBSP_IEI_OLD_SERIAL_NR);
726 }
727
728 INIT_LLIST_HEAD(&out->num_compl_list.list);
729 if (TLVP_PRES_LEN(tp, CBSP_IEI_NUM_BCAST_COMPL_LIST, 7)) {
730 cbsp_decode_num_compl_list(&out->num_compl_list, ctx,
731 TLVP_VAL(tp, CBSP_IEI_NUM_BCAST_COMPL_LIST),
732 TLVP_LEN(tp, CBSP_IEI_NUM_BCAST_COMPL_LIST));
733 }
734
735 INIT_LLIST_HEAD(&out->cell_list.list);
736 cbsp_decode_cell_list(&out->cell_list, ctx, TLVP_VAL(tp, CBSP_IEI_CELL_LIST),
737 TLVP_LEN(tp, CBSP_IEI_CELL_LIST));
738
739 if (TLVP_PRES_LEN(tp, CBSP_IEI_CHANNEL_IND, 1)) {
740 out->channel_ind = talloc(ctx, enum cbsp_channel_ind);
741 *out->channel_ind = *TLVP_VAL(tp, CBSP_IEI_CHANNEL_IND);
742 }
743 return 0;
744}
745
746/* 8.1.3.3 WRITE REPLACE FAILURE */
747static int cbsp_dec_write_repl_fail(struct osmo_cbsp_write_replace_failure *out,
748 const struct tlv_parsed *tp, struct msgb *in, void *ctx)
749{
750 if (!TLVP_PRES_LEN(tp, CBSP_IEI_MSG_ID, 2) ||
751 !TLVP_PRES_LEN(tp, CBSP_IEI_NEW_SERIAL_NR, 2) ||
Harald Weltef72155a2019-06-15 23:05:19 +0200752 !TLVP_PRES_LEN(tp, CBSP_IEI_FAILURE_LIST, 5)) {
753 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +0200754 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +0200755 }
Harald Welte07958e42019-05-03 09:39:10 +0200756
757 out->msg_id = tlvp_val16be(tp, CBSP_IEI_MSG_ID);
758 out->new_serial_nr = tlvp_val16be(tp, CBSP_IEI_NEW_SERIAL_NR);
759 if (TLVP_PRES_LEN(tp, CBSP_IEI_OLD_SERIAL_NR, 2)) {
760 out->old_serial_nr = talloc(ctx, uint16_t);
761 *out->old_serial_nr = tlvp_val16be(tp, CBSP_IEI_OLD_SERIAL_NR);
762 }
763
764 INIT_LLIST_HEAD(&out->fail_list);
765 cbsp_decode_fail_list(&out->fail_list, ctx,
766 TLVP_VAL(tp, CBSP_IEI_FAILURE_LIST),
767 TLVP_LEN(tp, CBSP_IEI_FAILURE_LIST));
768
769 INIT_LLIST_HEAD(&out->num_compl_list.list);
770 if (TLVP_PRES_LEN(tp, CBSP_IEI_NUM_BCAST_COMPL_LIST, 7)) {
771 cbsp_decode_num_compl_list(&out->num_compl_list, ctx,
772 TLVP_VAL(tp, CBSP_IEI_NUM_BCAST_COMPL_LIST),
773 TLVP_LEN(tp, CBSP_IEI_NUM_BCAST_COMPL_LIST));
774 }
775
776 INIT_LLIST_HEAD(&out->cell_list.list);
777 if (TLVP_PRES_LEN(tp, CBSP_IEI_CELL_LIST, 1)) {
778 cbsp_decode_cell_list(&out->cell_list, ctx, TLVP_VAL(tp, CBSP_IEI_CELL_LIST),
779 TLVP_LEN(tp, CBSP_IEI_CELL_LIST));
780 }
781
782 if (TLVP_PRES_LEN(tp, CBSP_IEI_CHANNEL_IND, 1)) {
783 out->channel_ind = talloc(ctx, enum cbsp_channel_ind);
784 *out->channel_ind = *TLVP_VAL(tp, CBSP_IEI_CHANNEL_IND);
785 }
786 return 0;
787}
788
789/* 8.1.3.4 KILL */
790static int cbsp_dec_kill(struct osmo_cbsp_kill *out, const struct tlv_parsed *tp,
791 struct msgb *in, void *ctx)
792{
793 if (!TLVP_PRES_LEN(tp, CBSP_IEI_MSG_ID, 2) ||
794 !TLVP_PRES_LEN(tp, CBSP_IEI_OLD_SERIAL_NR, 2) ||
Harald Weltef72155a2019-06-15 23:05:19 +0200795 !TLVP_PRES_LEN(tp, CBSP_IEI_CELL_LIST, 1)) {
796 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +0200797 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +0200798 }
799
Harald Welte07958e42019-05-03 09:39:10 +0200800 out->msg_id = tlvp_val16be(tp, CBSP_IEI_MSG_ID);
801 out->old_serial_nr = tlvp_val16be(tp, CBSP_IEI_OLD_SERIAL_NR);
802
803 INIT_LLIST_HEAD(&out->cell_list.list);
804 cbsp_decode_cell_list(&out->cell_list, ctx, TLVP_VAL(tp, CBSP_IEI_CELL_LIST),
805 TLVP_LEN(tp, CBSP_IEI_CELL_LIST));
806
807 if (TLVP_PRES_LEN(tp, CBSP_IEI_CHANNEL_IND, 1)) {
808 out->channel_ind = talloc(ctx, enum cbsp_channel_ind);
809 *out->channel_ind = *TLVP_VAL(tp, CBSP_IEI_CHANNEL_IND);
810 }
811 return 0;
812}
813
814/* 8.1.3.5 KILL COMPLETE */
815static int cbsp_dec_kill_compl(struct osmo_cbsp_kill_complete *out, const struct tlv_parsed *tp,
816 struct msgb *in, void *ctx)
817{
818 if (!TLVP_PRES_LEN(tp, CBSP_IEI_MSG_ID, 2) ||
819 !TLVP_PRES_LEN(tp, CBSP_IEI_OLD_SERIAL_NR, 2) ||
Harald Weltef72155a2019-06-15 23:05:19 +0200820 !TLVP_PRES_LEN(tp, CBSP_IEI_CELL_LIST, 1)) {
821 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +0200822 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +0200823 }
Harald Welte07958e42019-05-03 09:39:10 +0200824
825 out->msg_id = tlvp_val16be(tp, CBSP_IEI_MSG_ID);
826 out->old_serial_nr = tlvp_val16be(tp, CBSP_IEI_OLD_SERIAL_NR);
827
828 INIT_LLIST_HEAD(&out->num_compl_list.list);
829 if (TLVP_PRES_LEN(tp, CBSP_IEI_NUM_BCAST_COMPL_LIST, 7)) {
830 cbsp_decode_num_compl_list(&out->num_compl_list, ctx,
831 TLVP_VAL(tp, CBSP_IEI_NUM_BCAST_COMPL_LIST),
832 TLVP_LEN(tp, CBSP_IEI_NUM_BCAST_COMPL_LIST));
833 }
834
835 INIT_LLIST_HEAD(&out->cell_list.list);
836 cbsp_decode_cell_list(&out->cell_list, ctx, TLVP_VAL(tp, CBSP_IEI_CELL_LIST),
837 TLVP_LEN(tp, CBSP_IEI_CELL_LIST));
838
839 if (TLVP_PRES_LEN(tp, CBSP_IEI_CHANNEL_IND, 1)) {
840 out->channel_ind = talloc(ctx, enum cbsp_channel_ind);
841 *out->channel_ind = *TLVP_VAL(tp, CBSP_IEI_CHANNEL_IND);
842 }
843 return 0;
844}
845
846/* 8.1.3.6 KILL FAILURE */
847static int cbsp_dec_kill_fail(struct osmo_cbsp_kill_failure *out, const struct tlv_parsed *tp,
848 struct msgb *in, void *ctx)
849{
850 if (!TLVP_PRES_LEN(tp, CBSP_IEI_MSG_ID, 2) ||
851 !TLVP_PRES_LEN(tp, CBSP_IEI_OLD_SERIAL_NR, 2) ||
Harald Weltef72155a2019-06-15 23:05:19 +0200852 !TLVP_PRES_LEN(tp, CBSP_IEI_FAILURE_LIST, 5)) {
853 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +0200854 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +0200855 }
Harald Welte07958e42019-05-03 09:39:10 +0200856
857 out->msg_id = tlvp_val16be(tp, CBSP_IEI_MSG_ID);
858 out->old_serial_nr = tlvp_val16be(tp, CBSP_IEI_OLD_SERIAL_NR);
859
860 INIT_LLIST_HEAD(&out->fail_list);
861 cbsp_decode_fail_list(&out->fail_list, ctx,
862 TLVP_VAL(tp, CBSP_IEI_FAILURE_LIST),
863 TLVP_LEN(tp, CBSP_IEI_FAILURE_LIST));
864
865 INIT_LLIST_HEAD(&out->num_compl_list.list);
866 if (TLVP_PRES_LEN(tp, CBSP_IEI_NUM_BCAST_COMPL_LIST, 7)) {
867 cbsp_decode_num_compl_list(&out->num_compl_list, ctx,
868 TLVP_VAL(tp, CBSP_IEI_NUM_BCAST_COMPL_LIST),
869 TLVP_LEN(tp, CBSP_IEI_NUM_BCAST_COMPL_LIST));
870 }
871
872 INIT_LLIST_HEAD(&out->cell_list.list);
873 if (TLVP_PRES_LEN(tp, CBSP_IEI_CELL_LIST, 1)) {
874 cbsp_decode_cell_list(&out->cell_list, ctx, TLVP_VAL(tp, CBSP_IEI_CELL_LIST),
875 TLVP_LEN(tp, CBSP_IEI_CELL_LIST));
876 }
877
878 if (TLVP_PRES_LEN(tp, CBSP_IEI_CHANNEL_IND, 1)) {
879 out->channel_ind = talloc(ctx, enum cbsp_channel_ind);
880 *out->channel_ind = *TLVP_VAL(tp, CBSP_IEI_CHANNEL_IND);
881 }
882 return 0;
883}
884
885/* 8.1.3.7 LOAD QUERY */
886static int cbsp_dec_load_query(struct osmo_cbsp_load_query *out, const struct tlv_parsed *tp,
887 struct msgb *in, void *ctx)
888{
889 if (!TLVP_PRES_LEN(tp, CBSP_IEI_CELL_LIST, 1) ||
Harald Weltef72155a2019-06-15 23:05:19 +0200890 !TLVP_PRES_LEN(tp, CBSP_IEI_CHANNEL_IND, 1)) {
891 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +0200892 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +0200893 }
Harald Welte07958e42019-05-03 09:39:10 +0200894
895 INIT_LLIST_HEAD(&out->cell_list.list);
896 cbsp_decode_cell_list(&out->cell_list, ctx, TLVP_VAL(tp, CBSP_IEI_CELL_LIST),
897 TLVP_LEN(tp, CBSP_IEI_CELL_LIST));
898
899 out->channel_ind = *TLVP_VAL(tp, CBSP_IEI_CHANNEL_IND);
900 return 0;
901}
902
903/* 8.1.3.8 LOAD QUERY COMPLETE */
904static int cbsp_dec_load_query_compl(struct osmo_cbsp_load_query_complete *out,
905 const struct tlv_parsed *tp, struct msgb *in, void *ctx)
906{
907 if (!TLVP_PRES_LEN(tp, CBSP_IEI_RR_LOADING_LIST, 6) ||
Harald Weltef72155a2019-06-15 23:05:19 +0200908 !TLVP_PRES_LEN(tp, CBSP_IEI_CHANNEL_IND, 1)) {
909 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +0200910 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +0200911 }
Harald Welte07958e42019-05-03 09:39:10 +0200912
913 INIT_LLIST_HEAD(&out->loading_list.list);
914 cbsp_decode_loading_list(&out->loading_list, ctx,
915 TLVP_VAL(tp, CBSP_IEI_RR_LOADING_LIST),
916 TLVP_LEN(tp, CBSP_IEI_RR_LOADING_LIST));
917
918 out->channel_ind = *TLVP_VAL(tp, CBSP_IEI_CHANNEL_IND);
919 return 0;
920}
921
922/* 8.1.3.9 LOAD QUERY FAILURE */
923static int cbsp_dec_load_query_fail(struct osmo_cbsp_load_query_failure *out,
924 const struct tlv_parsed *tp, struct msgb *in, void *ctx)
925{
926 if (!TLVP_PRES_LEN(tp, CBSP_IEI_FAILURE_LIST, 5) ||
Harald Weltef72155a2019-06-15 23:05:19 +0200927 !TLVP_PRES_LEN(tp, CBSP_IEI_CHANNEL_IND, 1)) {
928 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +0200929 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +0200930 }
Harald Welte07958e42019-05-03 09:39:10 +0200931
932 INIT_LLIST_HEAD(&out->fail_list);
933 cbsp_decode_fail_list(&out->fail_list, ctx,
934 TLVP_VAL(tp, CBSP_IEI_FAILURE_LIST),
935 TLVP_LEN(tp, CBSP_IEI_FAILURE_LIST));
936
937 out->channel_ind = *TLVP_VAL(tp, CBSP_IEI_CHANNEL_IND);
938
939 INIT_LLIST_HEAD(&out->loading_list.list);
940 if (TLVP_PRES_LEN(tp, CBSP_IEI_RR_LOADING_LIST, 6)) {
941 cbsp_decode_loading_list(&out->loading_list, ctx,
942 TLVP_VAL(tp, CBSP_IEI_RR_LOADING_LIST),
943 TLVP_LEN(tp, CBSP_IEI_RR_LOADING_LIST));
944 }
945 return 0;
946}
947
948/* 8.1.3.10 STATUS QUERY */
949static int cbsp_dec_msg_status_query(struct osmo_cbsp_msg_status_query *out,
950 const struct tlv_parsed *tp, struct msgb *in, void *ctx)
951{
952 if (!TLVP_PRES_LEN(tp, CBSP_IEI_MSG_ID, 2) ||
953 !TLVP_PRES_LEN(tp, CBSP_IEI_OLD_SERIAL_NR, 2) ||
954 !TLVP_PRES_LEN(tp, CBSP_IEI_CELL_LIST, 1) ||
Harald Weltef72155a2019-06-15 23:05:19 +0200955 !TLVP_PRES_LEN(tp, CBSP_IEI_CHANNEL_IND, 1)) {
956 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +0200957 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +0200958 }
959
Harald Welte07958e42019-05-03 09:39:10 +0200960 out->msg_id = tlvp_val16be(tp, CBSP_IEI_MSG_ID);
961 out->old_serial_nr = tlvp_val16be(tp, CBSP_IEI_OLD_SERIAL_NR);
962
963 INIT_LLIST_HEAD(&out->cell_list.list);
964 cbsp_decode_cell_list(&out->cell_list, ctx, TLVP_VAL(tp, CBSP_IEI_CELL_LIST),
965 TLVP_LEN(tp, CBSP_IEI_CELL_LIST));
966
967 out->channel_ind = *TLVP_VAL(tp, CBSP_IEI_CHANNEL_IND);
968 return 0;
969}
970
971/* 8.1.3.11 STATUS QUERY COMPLETE */
972static int cbsp_dec_msg_status_query_compl(struct osmo_cbsp_msg_status_query_complete *out,
973 const struct tlv_parsed *tp, struct msgb *in, void *ctx)
974{
975 if (!TLVP_PRES_LEN(tp, CBSP_IEI_MSG_ID, 2) ||
976 !TLVP_PRES_LEN(tp, CBSP_IEI_OLD_SERIAL_NR, 2) ||
977 !TLVP_PRES_LEN(tp, CBSP_IEI_NUM_BCAST_COMPL_LIST, 7) ||
Harald Weltef72155a2019-06-15 23:05:19 +0200978 !TLVP_PRES_LEN(tp, CBSP_IEI_CHANNEL_IND, 1)) {
979 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +0200980 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +0200981 }
Harald Welte07958e42019-05-03 09:39:10 +0200982
983 out->msg_id = tlvp_val16be(tp, CBSP_IEI_MSG_ID);
984 out->old_serial_nr = tlvp_val16be(tp, CBSP_IEI_OLD_SERIAL_NR);
985
986 INIT_LLIST_HEAD(&out->num_compl_list.list);
987 cbsp_decode_num_compl_list(&out->num_compl_list, ctx,
988 TLVP_VAL(tp, CBSP_IEI_NUM_BCAST_COMPL_LIST),
989 TLVP_LEN(tp, CBSP_IEI_NUM_BCAST_COMPL_LIST));
990 out->channel_ind = *TLVP_VAL(tp, CBSP_IEI_CHANNEL_IND);
991 return 0;
992}
993
994/* 8.1.3.12 STATUS QUERY FAILURE */
995static int cbsp_dec_msg_status_query_fail(struct osmo_cbsp_msg_status_query_failure *out,
996 const struct tlv_parsed *tp, struct msgb *in, void *ctx)
997{
998 if (!TLVP_PRES_LEN(tp, CBSP_IEI_MSG_ID, 2) ||
999 !TLVP_PRES_LEN(tp, CBSP_IEI_OLD_SERIAL_NR, 2) ||
1000 !TLVP_PRES_LEN(tp, CBSP_IEI_FAILURE_LIST, 5) ||
Harald Weltef72155a2019-06-15 23:05:19 +02001001 !TLVP_PRES_LEN(tp, CBSP_IEI_CHANNEL_IND, 1)) {
1002 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +02001003 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +02001004 }
Harald Welte07958e42019-05-03 09:39:10 +02001005
1006 out->msg_id = tlvp_val16be(tp, CBSP_IEI_MSG_ID);
1007 out->old_serial_nr = tlvp_val16be(tp, CBSP_IEI_OLD_SERIAL_NR);
1008
1009 INIT_LLIST_HEAD(&out->fail_list);
1010 cbsp_decode_fail_list(&out->fail_list, ctx,
1011 TLVP_VAL(tp, CBSP_IEI_FAILURE_LIST),
1012 TLVP_LEN(tp, CBSP_IEI_FAILURE_LIST));
1013
1014 out->channel_ind = *TLVP_VAL(tp, CBSP_IEI_CHANNEL_IND);
1015
1016 INIT_LLIST_HEAD(&out->num_compl_list.list);
1017 if (TLVP_PRES_LEN(tp, CBSP_IEI_NUM_BCAST_COMPL_LIST, 7)) {
1018 cbsp_decode_num_compl_list(&out->num_compl_list, ctx,
1019 TLVP_VAL(tp, CBSP_IEI_NUM_BCAST_COMPL_LIST),
1020 TLVP_LEN(tp, CBSP_IEI_NUM_BCAST_COMPL_LIST));
1021 }
1022 return 0;
1023}
1024
1025/* 8.1.3.16 RESET */
1026static int cbsp_dec_reset(struct osmo_cbsp_reset *out, const struct tlv_parsed *tp,
1027 struct msgb *in, void *ctx)
1028{
Harald Weltef72155a2019-06-15 23:05:19 +02001029 if (!TLVP_PRES_LEN(tp, CBSP_IEI_CELL_LIST, 1)) {
1030 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +02001031 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +02001032 }
Harald Welte07958e42019-05-03 09:39:10 +02001033
1034 INIT_LLIST_HEAD(&out->cell_list.list);
1035 cbsp_decode_cell_list(&out->cell_list, ctx, TLVP_VAL(tp, CBSP_IEI_CELL_LIST),
1036 TLVP_LEN(tp, CBSP_IEI_CELL_LIST));
1037 return 0;
1038}
1039
1040/* 8.1.3.17 RESET COMPLETE */
1041static int cbsp_dec_reset_compl(struct osmo_cbsp_reset_complete *out, const struct tlv_parsed *tp,
1042 struct msgb *in, void *ctx)
1043{
Harald Weltef72155a2019-06-15 23:05:19 +02001044 if (!TLVP_PRES_LEN(tp, CBSP_IEI_CELL_LIST, 1)) {
1045 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +02001046 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +02001047 }
Harald Welte07958e42019-05-03 09:39:10 +02001048
1049 INIT_LLIST_HEAD(&out->cell_list.list);
1050 cbsp_decode_cell_list(&out->cell_list, ctx, TLVP_VAL(tp, CBSP_IEI_CELL_LIST),
1051 TLVP_LEN(tp, CBSP_IEI_CELL_LIST));
1052 return 0;
1053}
1054
1055/* 8.1.3.18 RESET FAILURE */
1056static int cbsp_dec_reset_fail(struct osmo_cbsp_reset_failure *out, const struct tlv_parsed *tp,
1057 struct msgb *in, void *ctx)
1058{
Harald Weltef72155a2019-06-15 23:05:19 +02001059 if (!TLVP_PRES_LEN(tp, CBSP_IEI_FAILURE_LIST, 5)) {
1060 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +02001061 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +02001062 }
Harald Welte07958e42019-05-03 09:39:10 +02001063
1064 INIT_LLIST_HEAD(&out->fail_list);
1065 cbsp_decode_fail_list(&out->fail_list, ctx,
1066 TLVP_VAL(tp, CBSP_IEI_FAILURE_LIST),
1067 TLVP_LEN(tp, CBSP_IEI_FAILURE_LIST));
1068
1069 INIT_LLIST_HEAD(&out->cell_list.list);
1070 if (TLVP_PRES_LEN(tp, CBSP_IEI_CELL_LIST, 1)) {
1071 cbsp_decode_cell_list(&out->cell_list, ctx, TLVP_VAL(tp, CBSP_IEI_CELL_LIST),
1072 TLVP_LEN(tp, CBSP_IEI_CELL_LIST));
1073 }
1074 return 0;
1075}
1076
1077/* 8.1.3.18a KEEP ALIVE */
1078static int cbsp_dec_keep_alive(struct osmo_cbsp_keep_alive *out, const struct tlv_parsed *tp,
1079 struct msgb *in, void *ctx)
1080{
Harald Weltef72155a2019-06-15 23:05:19 +02001081 if (!TLVP_PRES_LEN(tp, CBSP_IEI_KEEP_ALIVE_REP_PERIOD, 1)) {
1082 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +02001083 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +02001084 }
Harald Welte07958e42019-05-03 09:39:10 +02001085
1086 out->repetition_period = *TLVP_VAL(tp, CBSP_IEI_KEEP_ALIVE_REP_PERIOD);
1087 return 0;
1088}
1089
1090/* 8.1.3.18b KEEP ALIVE COMPLETE */
1091static int cbsp_dec_keep_alive_compl(struct osmo_cbsp_keep_alive_complete *out,
1092 const struct tlv_parsed *tp, struct msgb *in, void *ctx)
1093{
1094 return 0;
1095}
1096
1097/* 8.1.3.19 RESTART */
1098static int cbsp_dec_restart(struct osmo_cbsp_restart *out, const struct tlv_parsed *tp,
1099 struct msgb *in, void *ctx)
1100{
1101 if (!TLVP_PRES_LEN(tp, CBSP_IEI_CELL_LIST, 1) ||
1102 !TLVP_PRES_LEN(tp, CBSP_IEI_BCAST_MSG_TYPE, 1) ||
Harald Weltef72155a2019-06-15 23:05:19 +02001103 !TLVP_PRES_LEN(tp, CBSP_IEI_RECOVERY_IND, 1)) {
1104 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +02001105 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +02001106 }
Harald Welte07958e42019-05-03 09:39:10 +02001107
1108 INIT_LLIST_HEAD(&out->cell_list.list);
1109 cbsp_decode_cell_list(&out->cell_list, ctx, TLVP_VAL(tp, CBSP_IEI_CELL_LIST),
1110 TLVP_LEN(tp, CBSP_IEI_CELL_LIST));
1111
1112 out->bcast_msg_type = *TLVP_VAL(tp, CBSP_IEI_BCAST_MSG_TYPE);
1113 out->recovery_ind = *TLVP_VAL(tp, CBSP_IEI_RECOVERY_IND);
1114 return 0;
1115}
1116
1117/* 8.1.3.20 FAILURE */
1118static int cbsp_dec_failure(struct osmo_cbsp_failure *out, const struct tlv_parsed *tp,
1119 struct msgb *in, void *ctx)
1120{
1121 if (!TLVP_PRES_LEN(tp, CBSP_IEI_FAILURE_LIST, 5) ||
Harald Weltef72155a2019-06-15 23:05:19 +02001122 !TLVP_PRES_LEN(tp, CBSP_IEI_BCAST_MSG_TYPE, 1)) {
1123 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +02001124 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +02001125 }
Harald Welte07958e42019-05-03 09:39:10 +02001126
1127 INIT_LLIST_HEAD(&out->fail_list);
1128 cbsp_decode_fail_list(&out->fail_list, ctx,
1129 TLVP_VAL(tp, CBSP_IEI_FAILURE_LIST),
1130 TLVP_LEN(tp, CBSP_IEI_FAILURE_LIST));
1131
1132 out->bcast_msg_type = *TLVP_VAL(tp, CBSP_IEI_BCAST_MSG_TYPE);
1133 return 0;
1134}
1135
1136/* 8.1.3.21 ERROR INDICATION */
1137static int cbsp_dec_error_ind(struct osmo_cbsp_error_ind *out, const struct tlv_parsed *tp,
1138 struct msgb *in, void *ctx)
1139{
Harald Weltef72155a2019-06-15 23:05:19 +02001140 if (!TLVP_PRES_LEN(tp, CBSP_IEI_CAUSE, 1)) {
1141 osmo_cbsp_errstr = "missing/short mandatory IE";
Harald Welte07958e42019-05-03 09:39:10 +02001142 return -EINVAL;
Harald Weltef72155a2019-06-15 23:05:19 +02001143 }
Harald Welte07958e42019-05-03 09:39:10 +02001144
1145 out->cause = *TLVP_VAL(tp, CBSP_IEI_CAUSE);
1146 if (TLVP_PRES_LEN(tp, CBSP_IEI_MSG_ID, 2)) {
1147 out->msg_id = talloc(ctx, uint16_t);
1148 *out->msg_id = tlvp_val16be(tp, CBSP_IEI_MSG_ID);
1149 }
1150 if (TLVP_PRES_LEN(tp, CBSP_IEI_NEW_SERIAL_NR, 2)) {
1151 out->new_serial_nr = talloc(ctx, uint16_t);
1152 *out->new_serial_nr = tlvp_val16be(tp, CBSP_IEI_NEW_SERIAL_NR);
1153 }
1154 if (TLVP_PRES_LEN(tp, CBSP_IEI_OLD_SERIAL_NR, 2)) {
1155 out->old_serial_nr = talloc(ctx, uint16_t);
1156 *out->old_serial_nr = tlvp_val16be(tp, CBSP_IEI_OLD_SERIAL_NR);
1157 }
1158 if (TLVP_PRES_LEN(tp, CBSP_IEI_CHANNEL_IND, 1)) {
1159 out->channel_ind = talloc(ctx, enum cbsp_channel_ind);
1160 *out->channel_ind = *TLVP_VAL(tp, CBSP_IEI_CHANNEL_IND);
1161 }
1162 return 0;
1163}
1164
1165/*! Decode a CBSP message from wire formwat to pased structure.
1166 * \param[in] ctx talloc context from which to allocate decoded output.
1167 * \param[in] in message buffer contiaining binary CBSP message.
1168 * \returns callee-allocated decoded representation of CBSP message; NULL on error */
1169struct osmo_cbsp_decoded *osmo_cbsp_decode(void *ctx, struct msgb *in)
1170{
1171 struct osmo_cbsp_decoded *out = talloc_zero(ctx, struct osmo_cbsp_decoded);
1172 const struct cbsp_header *h = msgb_l1(in);
1173 struct tlv_parsed tp[16]; /* max. number of pages in a given CBS message */
1174 unsigned int len;
1175 int rc;
1176
Harald Weltef72155a2019-06-15 23:05:19 +02001177 osmo_cbsp_errstr = NULL;
1178
Harald Welte07958e42019-05-03 09:39:10 +02001179 if (!out)
1180 return NULL;
1181
1182 if (msgb_l1len(in) < sizeof(*h)) {
1183 goto out_err;
1184 }
1185 len = h->len[0] << 16 | h->len[1] << 8 | h->len[2];
1186
1187 /* discard messages where indicated length is more than we have */
1188 if (len > msgb_l2len(in)) {
1189 goto out_err;
1190 }
1191
1192 /* trim any messages with extra payload at the end */
1193 if (len < msgb_l2len(in))
1194 msgb_trim(in, (in->l2h - in->data) + msgb_l2len(in));
1195 out->msg_type = h->msg_type;
1196
1197 rc = tlv_parse2(tp, ARRAY_SIZE(tp), &cbsp_att_tlvdef, msgb_l2(in), msgb_l2len(in), 0, 0);
1198 if (rc < 0) {
1199 goto out_err;
1200 }
1201
1202 switch (h->msg_type) {
1203 case CBSP_MSGT_WRITE_REPLACE:
1204 rc = cbsp_dec_write_repl(&out->u.write_replace, tp, in, out);
1205 break;
1206 case CBSP_MSGT_WRITE_REPLACE_COMPL:
1207 rc = cbsp_dec_write_repl_compl(&out->u.write_replace_compl, tp, in, out);
1208 break;
1209 case CBSP_MSGT_WRITE_REPLACE_FAIL:
1210 rc = cbsp_dec_write_repl_fail(&out->u.write_replace_fail, tp, in, out);
1211 break;
1212 case CBSP_MSGT_KILL:
1213 rc = cbsp_dec_kill(&out->u.kill, tp, in, out);
1214 break;
1215 case CBSP_MSGT_KILL_COMPL:
1216 rc = cbsp_dec_kill_compl(&out->u.kill_compl, tp, in, out);
1217 break;
1218 case CBSP_MSGT_KILL_FAIL:
1219 rc = cbsp_dec_kill_fail(&out->u.kill_fail, tp, in, out);
1220 break;
1221 case CBSP_MSGT_LOAD_QUERY:
1222 rc = cbsp_dec_load_query(&out->u.load_query, tp, in, out);
1223 break;
1224 case CBSP_MSGT_LOAD_QUERY_COMPL:
1225 rc = cbsp_dec_load_query_compl(&out->u.load_query_compl, tp, in, out);
1226 break;
1227 case CBSP_MSGT_LOAD_QUERY_FAIL:
1228 rc = cbsp_dec_load_query_fail(&out->u.load_query_fail, tp, in, out);
1229 break;
1230 case CBSP_MSGT_MSG_STATUS_QUERY:
1231 rc = cbsp_dec_msg_status_query(&out->u.msg_status_query, tp, in, out);
1232 break;
1233 case CBSP_MSGT_MSG_STATUS_QUERY_COMPL:
1234 rc = cbsp_dec_msg_status_query_compl(&out->u.msg_status_query_compl, tp, in, out);
1235 break;
1236 case CBSP_MSGT_MSG_STATUS_QUERY_FAIL:
1237 rc = cbsp_dec_msg_status_query_fail(&out->u.msg_status_query_fail, tp, in, out);
1238 break;
1239 case CBSP_MSGT_RESET:
1240 rc = cbsp_dec_reset(&out->u.reset, tp, in, out);
1241 break;
1242 case CBSP_MSGT_RESET_COMPL:
1243 rc = cbsp_dec_reset_compl(&out->u.reset_compl, tp, in, out);
1244 break;
1245 case CBSP_MSGT_RESET_FAIL:
1246 rc = cbsp_dec_reset_fail(&out->u.reset_fail, tp, in, out);
1247 break;
1248 case CBSP_MSGT_RESTART:
1249 rc = cbsp_dec_restart(&out->u.restart, tp, in, out);
1250 break;
1251 case CBSP_MSGT_FAILURE:
1252 rc = cbsp_dec_failure(&out->u.failure, tp, in, out);
1253 break;
1254 case CBSP_MSGT_ERROR_IND:
1255 rc = cbsp_dec_error_ind(&out->u.error_ind, tp, in, out);
1256 break;
1257 case CBSP_MSGT_KEEP_ALIVE:
1258 rc = cbsp_dec_keep_alive(&out->u.keep_alive, tp, in, out);
1259 break;
1260 case CBSP_MSGT_KEEP_ALIVE_COMPL:
1261 rc = cbsp_dec_keep_alive_compl(&out->u.keep_alive_compl, tp, in, out);
1262 break;
1263 case CBSP_MSGT_SET_DRX:
1264 case CBSP_MSGT_SET_DRX_COMPL:
1265 case CBSP_MSGT_SET_DRX_FAIL:
Harald Weltef72155a2019-06-15 23:05:19 +02001266 osmo_cbsp_errstr = "message type not implemented";
Harald Welte07958e42019-05-03 09:39:10 +02001267 rc = -1;
1268 break;
1269 default:
Harald Weltef72155a2019-06-15 23:05:19 +02001270 osmo_cbsp_errstr = "message type not known in spec";
Harald Welte07958e42019-05-03 09:39:10 +02001271 rc = -1;
1272 break;
1273 }
1274
1275 if (rc < 0) {
1276 goto out_err;
1277 }
1278
1279 return out;
1280
1281out_err:
1282 talloc_free(out);
1283 return NULL;
1284}
1285
1286/* initialization of 'decoded' structure of given message type */
1287void osmo_cbsp_init_struct(struct osmo_cbsp_decoded *cbsp, enum cbsp_msg_type msg_type)
1288{
1289 memset(cbsp, 0, sizeof(*cbsp));
1290 cbsp->msg_type = msg_type;
1291
1292 switch (msg_type) {
1293 case CBSP_MSGT_WRITE_REPLACE:
1294 INIT_LLIST_HEAD(&cbsp->u.write_replace.cell_list.list);
1295 break;
1296 case CBSP_MSGT_WRITE_REPLACE_COMPL:
1297 INIT_LLIST_HEAD(&cbsp->u.write_replace_compl.num_compl_list.list);
1298 INIT_LLIST_HEAD(&cbsp->u.write_replace_compl.cell_list.list);
1299 break;
1300 case CBSP_MSGT_WRITE_REPLACE_FAIL:
1301 INIT_LLIST_HEAD(&cbsp->u.write_replace_fail.fail_list);
1302 INIT_LLIST_HEAD(&cbsp->u.write_replace_fail.num_compl_list.list);
1303 INIT_LLIST_HEAD(&cbsp->u.write_replace_fail.cell_list.list);
1304 break;
1305 case CBSP_MSGT_KILL:
1306 INIT_LLIST_HEAD(&cbsp->u.kill.cell_list.list);
1307 break;
1308 case CBSP_MSGT_KILL_COMPL:
1309 INIT_LLIST_HEAD(&cbsp->u.kill_compl.num_compl_list.list);
1310 INIT_LLIST_HEAD(&cbsp->u.kill_compl.cell_list.list);
1311 break;
1312 case CBSP_MSGT_KILL_FAIL:
1313 INIT_LLIST_HEAD(&cbsp->u.kill_fail.fail_list);
1314 INIT_LLIST_HEAD(&cbsp->u.kill_fail.num_compl_list.list);
1315 INIT_LLIST_HEAD(&cbsp->u.kill_fail.cell_list.list);
1316 break;
1317 case CBSP_MSGT_LOAD_QUERY:
1318 INIT_LLIST_HEAD(&cbsp->u.load_query.cell_list.list);
1319 break;
1320 case CBSP_MSGT_LOAD_QUERY_COMPL:
1321 INIT_LLIST_HEAD(&cbsp->u.load_query_compl.loading_list.list);
1322 break;
1323 case CBSP_MSGT_LOAD_QUERY_FAIL:
1324 INIT_LLIST_HEAD(&cbsp->u.load_query_fail.fail_list);
1325 break;
1326 case CBSP_MSGT_MSG_STATUS_QUERY:
1327 INIT_LLIST_HEAD(&cbsp->u.msg_status_query.cell_list.list);
1328 break;
1329 case CBSP_MSGT_MSG_STATUS_QUERY_COMPL:
1330 INIT_LLIST_HEAD(&cbsp->u.msg_status_query_compl.num_compl_list.list);
1331 break;
1332 case CBSP_MSGT_MSG_STATUS_QUERY_FAIL:
1333 INIT_LLIST_HEAD(&cbsp->u.msg_status_query_fail.fail_list);
1334 INIT_LLIST_HEAD(&cbsp->u.msg_status_query_fail.num_compl_list.list);
1335 break;
1336 case CBSP_MSGT_RESET:
1337 INIT_LLIST_HEAD(&cbsp->u.reset.cell_list.list);
1338 break;
1339 case CBSP_MSGT_RESET_COMPL:
1340 INIT_LLIST_HEAD(&cbsp->u.reset_compl.cell_list.list);
1341 break;
1342 case CBSP_MSGT_RESET_FAIL:
1343 INIT_LLIST_HEAD(&cbsp->u.reset_fail.fail_list);
1344 INIT_LLIST_HEAD(&cbsp->u.reset_fail.cell_list.list);
1345 break;
1346 case CBSP_MSGT_RESTART:
1347 INIT_LLIST_HEAD(&cbsp->u.restart.cell_list.list);
1348 break;
1349 case CBSP_MSGT_FAILURE:
1350 INIT_LLIST_HEAD(&cbsp->u.failure.fail_list);
1351 break;
1352 default:
1353 break;
1354 }
1355}
1356
1357/*! Dynamically allocate and initialize decoded CBSP structure.
1358 * \param[in] ctx talloc context from which to allocate
1359 * \param[in] msg_type CBSP message type for which to initialize result
1360 * \returns allocated + initialized decoded CBSP structure; NULL on talloc failure */
1361struct osmo_cbsp_decoded *osmo_cbsp_decoded_alloc(void *ctx, enum cbsp_msg_type msg_type)
1362{
1363 struct osmo_cbsp_decoded *cbsp = talloc_zero(ctx, struct osmo_cbsp_decoded);
1364 if (!cbsp)
1365 return NULL;
1366 osmo_cbsp_init_struct(cbsp, msg_type);
1367 return cbsp;
1368}
1369
1370/***********************************************************************
1371 * Message Reception
1372 ***********************************************************************/
1373
1374#ifdef HAVE_SYS_SOCKET_H
1375#include <sys/socket.h>
1376
1377/*! Read one CBSP message from socket fd or store part if still not fully received.
1378 * \param[in] ctx talloc context from which to allocate new msgb.
1379 * \param[in] fd The fd for the socket to read from.
1380 * \param[out] rmsg internally allocated msgb containing a fully received CBSP message.
1381 * \param[inout] tmp_msg internally allocated msgb caching data for not yet fully received message.
1382 *
1383 * Function is designed just like ipa_msg_recv_buffered()
1384 */
1385int osmo_cbsp_recv_buffered(void *ctx, int fd, struct msgb **rmsg, struct msgb **tmp_msg)
1386{
1387 struct msgb *msg = tmp_msg ? *tmp_msg : NULL;
1388 struct cbsp_header *h;
1389 int len, rc;
1390 int needed;
1391
1392 if (!msg) {
1393 msg = osmo_cbsp_msgb_alloc(ctx, __func__);
Harald Weltec30d8be2019-07-21 07:51:33 +02001394 if (!msg)
Harald Welte07958e42019-05-03 09:39:10 +02001395 return -ENOMEM;
Harald Welte07958e42019-05-03 09:39:10 +02001396 msg->l1h = msg->tail;
1397 }
1398
1399 if (msg->l2h == NULL) {
1400 /* first read the [missing part of the] header */
1401 needed = sizeof(*h) - msg->len;
1402 rc = recv(fd, msg->tail, needed, 0);
1403 if (rc == 0)
1404 goto discard_msg;
1405 else if (rc < 0) {
1406 if (errno == EAGAIN || errno == EINTR)
1407 rc = 0;
1408 else {
1409 rc = -errno;
1410 goto discard_msg;
1411 }
1412 }
1413 msgb_put(msg, rc);
1414 if (rc < needed) {
1415 if (msg->len == 0) {
1416 rc = -EAGAIN;
1417 goto discard_msg;
1418 }
1419
1420 if (!tmp_msg) {
1421 rc = -EIO;
1422 goto discard_msg;
1423 }
1424 *tmp_msg = msg;
1425 return -EAGAIN;
1426 }
1427 msg->l2h = msg->tail;
1428 }
1429
1430 h = (struct cbsp_header *) msg->data;
1431 /* then read the length as specified in the header */
1432 len = h->len[0] << 16 | h->len[1] << 8 | h->len[2];
1433
1434 needed = len - msgb_l2len(msg);
1435 if (needed > 0) {
1436 rc = recv(fd, msg->tail, needed, 0);
1437 if (rc == 0)
1438 goto discard_msg;
1439 else if (rc < 0) {
1440 if (errno == EAGAIN || errno == EINTR)
1441 rc = 0;
1442 else {
1443 rc = -errno;
1444 goto discard_msg;
1445 }
1446 }
1447 msgb_put(msg, rc);
1448 /* still not all of payload received? */
1449 if (rc < needed) {
1450 if (!tmp_msg) {
1451 rc = -EIO;
1452 goto discard_msg;
1453 }
1454 *tmp_msg = msg;
1455 return -EAGAIN;
1456 }
1457 }
1458 /* else: complete message received */
1459 rc = msgb_l2len(msg);
1460 if (rc == 0) {
1461 /* drop empty message */
1462 rc = -EAGAIN;
1463 goto discard_msg;
1464 }
1465 if (tmp_msg)
1466 *tmp_msg = NULL;
1467 *rmsg = msg;
1468 return rc;
1469
1470discard_msg:
Harald Welte07958e42019-05-03 09:39:10 +02001471 if (tmp_msg)
1472 *tmp_msg = NULL;
1473 msgb_free(msg);
1474 return rc;
1475}
1476
1477#endif /* HAVE_SYS_SOCKET_H */