blob: d2ab09f25cdb59527920622061bc9dff16cc91ba [file] [log] [blame]
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001/* BSSAP/BSSMAP encoding and decoding for MSC */
2/*
Vadim Yanitskiy999a5932023-05-18 17:22:26 +07003 * (C) 2019 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01004 * All Rights Reserved
5 *
6 * Author: Neels Hofmeyr
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.
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010019 */
20
21#include <osmocom/core/byteswap.h>
22
23#include <osmocom/crypt/auth.h>
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +020024#include <osmocom/crypt/kdf.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010025
26#include <osmocom/gsm/tlv.h>
27#include <osmocom/gsm/gsm0808.h>
28#include <osmocom/gsm/mncc.h>
29#include <osmocom/gsm/gsm48.h>
30
31#include <osmocom/msc/debug.h>
32#include <osmocom/msc/ran_msg_a.h>
33#include <osmocom/msc/sccp_ran.h>
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +020034#include <osmocom/msc/gsm_data.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010035
36#define LOG_RAN_A_DEC(RAN_DEC, level, fmt, args...) \
37 LOG_RAN_DEC(RAN_DEC, DBSSAP, level, "BSSMAP: " fmt, ## args)
38
39/* Assumes presence of struct ran_dec *ran_dec and ran_dec_msg.msg_name (set) in the local scope. */
40#define LOG_RAN_A_DEC_MSG(level, fmt, args...) \
41 LOG_RAN_DEC(ran_dec, DBSSAP, level, "%s: " fmt, ran_dec_msg.msg_name, ## args)
42
43#define LOG_RAN_A_ENC(FI, level, fmt, args...) \
44 LOG_RAN_ENC(FI, DBSSAP, level, "BSSMAP: " fmt, ## args)
45
46static int ran_a_decode_l3_compl(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
47{
48 struct gsm0808_cell_id_list2 cil;
49 struct gsm0808_cell_id cell_id;
50 struct tlv_p_entry *ie_cell_id = TLVP_GET(tp, GSM0808_IE_CELL_IDENTIFIER);
51 struct tlv_p_entry *ie_l3_info = TLVP_GET(tp, GSM0808_IE_LAYER_3_INFORMATION);
Neels Hofmeyr8a50cfb2019-10-21 03:01:00 +020052 struct tlv_p_entry *ie_codec_list_bss_supported = TLVP_GET(tp, GSM0808_IE_SPEECH_CODEC_LIST);
53 struct gsm0808_speech_codec_list codec_list_bss_supported;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010054 struct ran_msg ran_dec_msg = {
55 .msg_type = RAN_MSG_COMPL_L3,
Neels Hofmeyr0c1ed152019-10-21 03:12:58 +020056 .msg_name = "BSSMAP Complete Layer 3 Information",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010057 .compl_l3 = {
58 .cell_id = &cell_id,
59 .msg = msg,
60 },
61 };
62 int rc;
63
64 if (!ie_cell_id) {
65 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory CELL IDENTIFIER not present, discarding message\n");
66 return -EINVAL;
67 }
68 if (!ie_l3_info) {
69 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory LAYER 3 INFORMATION not present, discarding message\n");
70 return -EINVAL;
71 }
72
73 /* Parse Cell ID element -- this should yield a cell identifier "list" with 1 element. */
74
75 rc = gsm0808_dec_cell_id_list2(&cil, ie_cell_id->val, ie_cell_id->len);
76 if (rc < 0) {
77 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Decoding CELL IDENTIFIER gave rc=%d\n", rc);
78 return -EINVAL;
79 }
80 if (cil.id_list_len != 1) {
81 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Unable to parse element CELL IDENTIFIER, discarding message\n");
82 return -EINVAL;
83 }
84
85 /* Sanity check the Cell Identity */
86 switch (cil.id_discr) {
87 case CELL_IDENT_WHOLE_GLOBAL:
88 case CELL_IDENT_LAI_AND_LAC:
89 case CELL_IDENT_LAC_AND_CI:
90 case CELL_IDENT_LAC:
91 break;
92
93 case CELL_IDENT_CI:
94 case CELL_IDENT_NO_CELL:
95 case CELL_IDENT_BSS:
96 default:
97 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "CELL IDENTIFIER does not specify a LAC, discarding message: %s\n",
98 gsm0808_cell_id_list_name(&cil));
99 return -EINVAL;
100 }
101
102 cell_id = (struct gsm0808_cell_id){
103 .id_discr = cil.id_discr,
104 .id = cil.id_list[0],
105 };
106
Neels Hofmeyrede90832022-01-13 18:13:15 +0100107 /* Parse Layer 3 Information element; point ran_dec_msg->compl_l3.msg to the L3 Info data */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100108 msg->l3h = (uint8_t*)ie_l3_info->val;
109 msgb_l3trim(msg, ie_l3_info->len);
110
111 if (msgb_l3len(msg) < sizeof(struct gsm48_hdr)) {
112 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "too short L3 info (%d), discarding message\n", msgb_l3len(msg));
113 return -ENODATA;
114 }
115
Neels Hofmeyr8a50cfb2019-10-21 03:01:00 +0200116 /* Decode Codec List (BSS Supported) */
117 if (ie_codec_list_bss_supported) {
118 rc = gsm0808_dec_speech_codec_list(&codec_list_bss_supported,
119 ie_codec_list_bss_supported->val, ie_codec_list_bss_supported->len);
120 if (rc < 0) {
121 LOG_RAN_A_DEC_MSG(LOGL_ERROR,
122 "Complete Layer 3 Information: unable to decode IE Codec List (BSS Supported)"
123 " (rc=%d), continuing anyway\n", rc);
124 /* This IE is not critical, do not abort with error. */
125 } else
126 ran_dec_msg.compl_l3.codec_list_bss_supported = &codec_list_bss_supported;
127 }
128
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100129 return ran_decoded(ran_dec, &ran_dec_msg);
130}
131
132static int ran_a_decode_clear_request(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
133{
134 struct tlv_p_entry *ie_cause = TLVP_GET(tp, GSM0808_IE_CAUSE);
135 struct ran_msg ran_dec_msg = {
136 .msg_type = RAN_MSG_CLEAR_REQUEST,
137 .msg_name = "BSSMAP Clear Request",
138 };
139
140 if (!ie_cause) {
141 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Cause code is missing, using GSM0808_CAUSE_EQUIPMENT_FAILURE\n");
142 ran_dec_msg.clear_request.bssap_cause = GSM0808_CAUSE_EQUIPMENT_FAILURE;
143 } else {
144 ran_dec_msg.clear_request.bssap_cause = ie_cause->val[0];
145 }
146
147 return ran_decoded(ran_dec, &ran_dec_msg);
148}
149
150static int ran_a_decode_clear_complete(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
151{
152 struct ran_msg ran_dec_msg = {
153 .msg_type = RAN_MSG_CLEAR_COMPLETE,
154 .msg_name = "BSSMAP Clear Complete",
155 };
156 return ran_decoded(ran_dec, &ran_dec_msg);
157}
158
159static int ran_a_decode_classmark_update(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
160{
161 struct tlv_p_entry *ie_cm2 = TLVP_GET(tp, GSM0808_IE_CLASSMARK_INFORMATION_T2);
162 struct tlv_p_entry *ie_cm3 = TLVP_GET(tp, GSM0808_IE_CLASSMARK_INFORMATION_T3);
163 struct osmo_gsm48_classmark cm = {};
164 struct ran_msg ran_dec_msg = {
165 .msg_type = RAN_MSG_CLASSMARK_UPDATE,
166 .msg_name = "BSSMAP Classmark Update",
167 .classmark_update = {
168 .classmark = &cm,
169 },
170 };
171
172 if (!ie_cm2) {
173 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "mandatory Classmark Information Type 2 not present, discarding message\n");
174 return -EINVAL;
175 }
176
177 cm.classmark2_len = OSMO_MIN(sizeof(cm.classmark2), ie_cm2->len);
178 memcpy(&cm.classmark2, ie_cm2->val, cm.classmark2_len);
179
180 if (ie_cm3) {
181 cm.classmark3_len = OSMO_MIN(sizeof(cm.classmark3), ie_cm3->len);
182 memcpy(&cm.classmark3, ie_cm3->val, cm.classmark3_len);
183 }
184
185 return ran_decoded(ran_dec, &ran_dec_msg);
186}
187
188static int ran_a_decode_cipher_mode_complete(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
189{
190 struct tlv_p_entry *ie_chosen_encr_alg = TLVP_GET(tp, GSM0808_IE_CHOSEN_ENCR_ALG);
191 struct tlv_p_entry *ie_l3_msg = TLVP_GET(tp, GSM0808_IE_LAYER_3_MESSAGE_CONTENTS);
192 int rc;
193 struct ran_msg ran_dec_msg = {
194 .msg_type = RAN_MSG_CIPHER_MODE_COMPLETE,
195 .msg_name = "BSSMAP Ciphering Mode Complete",
196 };
197
198 if (ie_chosen_encr_alg) {
199 uint8_t ie_val = ie_chosen_encr_alg->val[0];
200 /* 3GPP TS 48.008 3.2.2.44 Chosen Encryption Algorithm encodes as 1 = no encryption, 2 = A5/1, 4 = A5/3.
201 * Internally we handle without this weird off-by-one. */
202 if (ie_val < 1 || ie_val > 8)
203 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Unsupported value for 3.2.2.44 Chosen Encryption Algorithm: %u\n",
204 ie_val);
205 else
206 ran_dec_msg.cipher_mode_complete.alg_id = ie_chosen_encr_alg->val[0];
207 }
208
Neels Hofmeyre9a39112019-08-29 00:10:49 +0200209 if (ie_l3_msg)
210 ran_dec_msg.cipher_mode_complete.l3_msg = ie_l3_msg;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100211
Neels Hofmeyre9a39112019-08-29 00:10:49 +0200212 rc = ran_decoded(ran_dec, &ran_dec_msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100213
214 return rc;
215}
216
217static int ran_a_decode_cipher_mode_reject(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
218{
219 int rc;
220 struct ran_msg ran_dec_msg = {
221 .msg_type = RAN_MSG_CIPHER_MODE_REJECT,
222 .msg_name = "BSSMAP Ciphering Mode Reject",
223 };
224
Vadim Yanitskiy33144f12021-02-05 20:14:19 +0100225 rc = gsm0808_get_cause(tp);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100226 if (rc < 0) {
227 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "failed to extract Cause\n");
228 ran_dec_msg.cipher_mode_reject.bssap_cause = GSM0808_CAUSE_EQUIPMENT_FAILURE;
229 } else {
230 ran_dec_msg.cipher_mode_reject.bssap_cause = (enum gsm0808_cause)rc;
231 }
232
233 return ran_decoded(ran_dec, &ran_dec_msg);
234}
235
236enum mgcp_codecs ran_a_mgcp_codec_from_sc(const struct gsm0808_speech_codec *sc)
237{
238 switch (sc->type) {
239 case GSM0808_SCT_FR1:
240 return CODEC_GSM_8000_1;
241 break;
242 case GSM0808_SCT_FR2:
243 return CODEC_GSMEFR_8000_1;
244 break;
245 case GSM0808_SCT_FR3:
246 return CODEC_AMR_8000_1;
247 break;
248 case GSM0808_SCT_FR4:
249 return CODEC_AMRWB_16000_1;
250 break;
251 case GSM0808_SCT_FR5:
252 return CODEC_AMRWB_16000_1;
253 break;
254 case GSM0808_SCT_HR1:
255 return CODEC_GSMHR_8000_1;
256 break;
257 case GSM0808_SCT_HR3:
258 return CODEC_AMR_8000_1;
259 break;
260 case GSM0808_SCT_HR4:
261 return CODEC_AMRWB_16000_1;
262 break;
263 case GSM0808_SCT_HR6:
264 return CODEC_AMRWB_16000_1;
265 break;
266 default:
267 return CODEC_PCMU_8000_1;
268 break;
269 }
270}
271
272static int ran_a_decode_assignment_complete(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
273{
274 struct tlv_p_entry *ie_aoip_transp_addr = TLVP_GET(tp, GSM0808_IE_AOIP_TRASP_ADDR);
275 struct tlv_p_entry *ie_speech_codec = TLVP_GET(tp, GSM0808_IE_SPEECH_CODEC);
Neels Hofmeyr8a50cfb2019-10-21 03:01:00 +0200276 struct tlv_p_entry *ie_codec_list_bss_supported = TLVP_GET(tp, GSM0808_IE_SPEECH_CODEC_LIST);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200277 struct tlv_p_entry *ie_osmux_cid = TLVP_GET(tp, GSM0808_IE_OSMO_OSMUX_CID);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100278 struct sockaddr_storage rtp_addr;
Neels Hofmeyr8a50cfb2019-10-21 03:01:00 +0200279 struct gsm0808_speech_codec_list codec_list_bss_supported;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100280 int rc;
281 struct ran_msg ran_dec_msg = {
282 .msg_type = RAN_MSG_ASSIGNMENT_COMPLETE,
283 .msg_name = "BSSMAP Assignment Complete",
284 };
285
286 if (ie_aoip_transp_addr) {
287 /* Decode AoIP transport address element */
288 rc = gsm0808_dec_aoip_trasp_addr(&rtp_addr, ie_aoip_transp_addr->val, ie_aoip_transp_addr->len);
289 if (rc < 0) {
290 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Unable to decode AoIP Transport Layer Address\n");
291 return -EINVAL;
292 }
293
Pau Espin Pedrolf9c76e32020-09-02 19:25:55 +0200294 if (osmo_sockaddr_str_from_sockaddr(&ran_dec_msg.assignment_complete.remote_rtp, &rtp_addr)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100295 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Assignment Complete: unable to decode remote RTP IP address\n");
296 return -EINVAL;
297 }
298 }
299
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200300 if (ie_osmux_cid) {
301 rc = gsm0808_dec_osmux_cid(&ran_dec_msg.assignment_complete.osmux_cid, ie_osmux_cid->val, ie_osmux_cid->len);
302 if (rc < 0) {
303 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Unable to decode Osmux CID\n");
304 return -EINVAL;
305 }
306 ran_dec_msg.assignment_complete.osmux_present = true;
307 }
308
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100309 if (ie_speech_codec) {
310 /* Decode Speech Codec (Chosen) element */
Neels Hofmeyrcec51b32023-03-01 03:47:45 +0100311 rc = gsm0808_dec_speech_codec(&ran_dec_msg.assignment_complete.codec,
312 ie_speech_codec->val, ie_speech_codec->len);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100313 if (rc < 0) {
314 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Assignment Complete: unable to decode IE Speech Codec (Chosen)"
315 " (rc=%d).\n", rc);
316 return -EINVAL;
317 }
318 ran_dec_msg.assignment_complete.codec_present = true;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100319 }
320
Neels Hofmeyr8a50cfb2019-10-21 03:01:00 +0200321 if (ie_codec_list_bss_supported) {
322 /* Decode Codec List (BSS Supported) */
323 rc = gsm0808_dec_speech_codec_list(&codec_list_bss_supported,
324 ie_codec_list_bss_supported->val, ie_codec_list_bss_supported->len);
325 if (rc < 0) {
326 LOG_RAN_A_DEC_MSG(LOGL_ERROR,
327 "Assignment Complete: unable to decode IE Codec List (BSS Supported)"
328 " (rc=%d), continuing anyway\n", rc);
329 /* This IE is not critical, do not abort with error. */
330 } else
331 ran_dec_msg.assignment_complete.codec_list_bss_supported = &codec_list_bss_supported;
332 }
333
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100334 return ran_decoded(ran_dec, &ran_dec_msg);
335}
336
337static int ran_a_decode_assignment_failure(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
338{
339 struct tlv_p_entry *ie_cause = TLVP_GET(tp, GSM0808_IE_CAUSE);
340 struct tlv_p_entry *ie_rr_cause = TLVP_GET(tp, GSM0808_IE_RR_CAUSE);
341 struct tlv_p_entry *ie_speech_codec_list = TLVP_GET(tp, GSM0808_IE_SPEECH_CODEC_LIST);
342 struct gsm0808_speech_codec_list scl;
343 struct ran_msg ran_dec_msg = {
344 .msg_type = RAN_MSG_ASSIGNMENT_FAILURE,
345 .msg_name = "BSSMAP Assignment Failure",
346 .assignment_failure = {
347 .bssap_cause = GSM0808_CAUSE_EQUIPMENT_FAILURE,
348 .rr_cause = GSM48_RR_CAUSE_ABNORMAL_UNSPEC,
349 },
350 };
351
352 if (ie_cause)
353 ran_dec_msg.assignment_failure.bssap_cause = ie_cause->val[0];
354 if (ie_rr_cause)
355 ran_dec_msg.assignment_failure.rr_cause = ie_rr_cause->val[0];
356
357 if (ie_speech_codec_list
358 && gsm0808_dec_speech_codec_list(&scl, ie_speech_codec_list->val, ie_speech_codec_list->len) == 0)
359 ran_dec_msg.assignment_failure.scl_bss_supported = &scl;
360
361 return ran_decoded(ran_dec, &ran_dec_msg);
362}
363
364static int ran_a_decode_sapi_n_reject(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
365{
366 struct tlv_p_entry *ie_cause = TLVP_GET(tp, GSM0808_IE_CAUSE);
367 struct tlv_p_entry *ie_dlci = TLVP_GET(tp, GSM0808_IE_DLCI);
368 struct ran_msg ran_dec_msg = {
369 .msg_type = RAN_MSG_SAPI_N_REJECT,
370 .msg_name = "BSSMAP SAPI-N Reject",
371 };
372
373 /* Note: The MSC code seems not to care about the cause code, but by
374 * the specification it is mandatory, so we check its presence. See
375 * also 3GPP TS 48.008 3.2.1.34 SAPI "n" REJECT */
376 if (!ie_cause) {
377 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "SAPI-N Reject: cause code IE is missing, discarding message\n");
378 return -EINVAL;
379 }
380 ran_dec_msg.sapi_n_reject.bssap_cause = ie_cause->val[0];
381
382 if (!ie_dlci) {
383 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "SAPI-N Reject: DLCI IE is missing, discarding message\n");
384 return -EINVAL;
385 }
386 ran_dec_msg.sapi_n_reject.dlci = ie_dlci->val[0];
387
388 return ran_decoded(ran_dec, &ran_dec_msg);
389}
390
391static int ran_a_decode_lcls_notification(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
392{
393 const struct tlv_p_entry *ie_lcls_bss_status = TLVP_GET(tp, GSM0808_IE_LCLS_BSS_STATUS);
394 const struct tlv_p_entry *ie_lcls_break_req = TLVP_GET(tp, GSM0808_IE_LCLS_BREAK_REQ);
395 struct ran_msg ran_dec_msg;
396
397 /* Either §3.2.2.119 LCLS-BSS-Status or §3.2.2.120 LCLS-Break-Request shall be present */
Vadim Yanitskiy18e8b392019-05-11 04:22:55 +0700398 if (ie_lcls_bss_status && !ie_lcls_break_req) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100399 ran_dec_msg = (struct ran_msg){
400 .msg_type = RAN_MSG_LCLS_STATUS,
401 .msg_name = "BSSMAP LCLS Notification (LCLS Status)",
402 .lcls_status = {
403 .status = ie_lcls_bss_status->len ?
404 ie_lcls_bss_status->val[0] : GSM0808_LCLS_STS_NA,
405 },
406 };
407 return ran_decoded(ran_dec, &ran_dec_msg);
Vadim Yanitskiy18e8b392019-05-11 04:22:55 +0700408 } else if (ie_lcls_break_req && !ie_lcls_bss_status) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100409 ran_dec_msg = (struct ran_msg){
410 .msg_type = RAN_MSG_LCLS_BREAK_REQ,
411 .msg_name = "BSSMAP LCLS Notification (LCLS Break Req)",
412 .lcls_break_req = {
413 .todo = 23,
414 },
415 };
416 return ran_decoded(ran_dec, &ran_dec_msg);
417 }
418
Vadim Yanitskiy18e8b392019-05-11 04:22:55 +0700419 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "Ignoring broken LCLS Notification message\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100420 return -EINVAL;
421}
422
423static int ran_a_decode_handover_required(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
424{
425 const struct tlv_p_entry *ie_cause = TLVP_GET(tp, GSM0808_IE_CAUSE);
426 const struct tlv_p_entry *ie_cil = TLVP_GET(tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
427 struct ran_msg ran_dec_msg = {
428 .msg_type = RAN_MSG_HANDOVER_REQUIRED,
429 .msg_name = "BSSMAP Handover Required",
430 };
431 /* On decoding failures, dispatch an invalid RAN_MSG_HANDOVER_REQUIRED so msc_a can pass down a
432 * BSS_MAP_MSG_HANDOVER_REQUIRED_REJECT message. */
433
434 if (ie_cause)
435 ran_dec_msg.handover_required.cause = ie_cause->val[0];
436 else
437 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Cause IE missing\n");
438
439 if (!ie_cil
440 || gsm0808_dec_cell_id_list2(&ran_dec_msg.handover_required.cil, ie_cil->val, ie_cil->len) <= 0) {
441 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "No or invalid Cell Identifier List IE\n");
442 ran_dec_msg.handover_required.cil = (struct gsm0808_cell_id_list2){};
443 }
444
445 return ran_decoded(ran_dec, &ran_dec_msg);
446}
447
448static uint8_t a5_encryption_mask_from_gsm0808_chosen_enc_alg(enum gsm0808_chosen_enc_alg val)
449{
450 return 1 << val;
451}
452
453static int ran_a_decode_handover_request(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
454{
455 struct osmo_gsm48_classmark classmark = {};
456 struct ran_msg ran_dec_msg = {
457 .msg_type = RAN_MSG_HANDOVER_REQUEST,
458 .msg_name = "BSSMAP Handover Request",
459 .handover_request = {
460 .classmark = &classmark,
461 },
462 };
463 struct ran_handover_request *r = &ran_dec_msg.handover_request;
464
465 const struct tlv_p_entry *ie_channel_type = TLVP_GET(tp, GSM0808_IE_CHANNEL_TYPE);
466 const struct tlv_p_entry *ie_encryption_information = TLVP_GET(tp, GSM0808_IE_ENCRYPTION_INFORMATION);
467 const struct tlv_p_entry *ie_classmark1 = TLVP_GET(tp, GSM0808_IE_CLASSMARK_INFORMATION_TYPE_1);
468 const struct tlv_p_entry *ie_classmark2 = TLVP_GET(tp, GSM0808_IE_CLASSMARK_INFORMATION_T2);
469 const struct tlv_p_entry *ie_cell_id_serving = TLVP_GET(&tp[0], GSM0808_IE_CELL_IDENTIFIER);
470 const struct tlv_p_entry *ie_cell_id_target = TLVP_GET(&tp[1], GSM0808_IE_CELL_IDENTIFIER);
471 const struct tlv_p_entry *ie_cause = TLVP_GET(tp, GSM0808_IE_CAUSE);
472 const struct tlv_p_entry *ie_classmark3 = TLVP_GET(tp, GSM0808_IE_CLASSMARK_INFORMATION_T3);
473 const struct tlv_p_entry *ie_current_channel_type_1 = TLVP_GET(tp, GSM0808_IE_CURRENT_CHANNEL_TYPE_1);
474 const struct tlv_p_entry *ie_speech_version_used = TLVP_GET(tp, GSM0808_IE_SPEECH_VERSION);
475 const struct tlv_p_entry *ie_chosen_encr_alg_serving = TLVP_GET(tp, GSM0808_IE_CHOSEN_ENCR_ALG);
476 const struct tlv_p_entry *ie_old_bss_to_new_bss_info = TLVP_GET(tp, GSM0808_IE_OLD_BSS_TO_NEW_BSS_INFORMATION);
477 const struct tlv_p_entry *ie_imsi = TLVP_GET(tp, GSM0808_IE_IMSI);
478 const struct tlv_p_entry *ie_aoip_transp_addr = TLVP_GET(tp, GSM0808_IE_AOIP_TRASP_ADDR);
479 const struct tlv_p_entry *ie_codec_list_msc_preferred = TLVP_GET(tp, GSM0808_IE_SPEECH_CODEC_LIST);
480 const struct tlv_p_entry *ie_call_id = TLVP_GET(tp, GSM0808_IE_CALL_ID);
Neels Hofmeyrdb07fdc2021-06-09 22:27:47 +0200481 const struct tlv_p_entry *ie_kc128 = TLVP_GET(tp, GSM0808_IE_KC_128);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100482 const struct tlv_p_entry *ie_global_call_ref = TLVP_GET(tp, GSM0808_IE_GLOBAL_CALL_REF);
483
484 struct gsm0808_channel_type channel_type;
485 struct gsm0808_encrypt_info encr_info;
486 struct gsm0808_speech_codec_list scl;
487 struct geran_encr geran_encr = {};
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100488 struct osmo_sockaddr_str rtp_ran_local;
489
490 if (!ie_channel_type) {
491 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: Channel Type\n");
492 return -EINVAL;
493 }
494 if (gsm0808_dec_channel_type(&channel_type, ie_channel_type->val, ie_channel_type->len) <= 0) {
495 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Failed to decode Channel Type IE\n");
496 return -EINVAL;
497 }
498 r->geran.channel_type = &channel_type;
499
500 if (ie_encryption_information) {
501 int i;
502 if (gsm0808_dec_encrypt_info(&encr_info, ie_encryption_information->val, ie_encryption_information->len)
503 <= 0) {
Martin Hauke3f07dac2019-11-14 17:49:08 +0100504 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Failed to decode Encryption Information IE\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100505 return -EINVAL;
506 }
507
508 for (i = 0; i < encr_info.perm_algo_len; i++) {
509 r->geran.a5_encryption_mask |=
510 a5_encryption_mask_from_gsm0808_chosen_enc_alg(encr_info.perm_algo[i]);
511 }
512
513 if (encr_info.key_len > sizeof(geran_encr.key)) {
Martin Hauke3f07dac2019-11-14 17:49:08 +0100514 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Failed to decode Encryption Information IE:"
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100515 " encryption key is too long: %u\n", geran_encr.key_len);
516 return -EINVAL;
517 }
518
519 if (encr_info.key_len) {
520 memcpy(geran_encr.key, encr_info.key, encr_info.key_len);
521 geran_encr.key_len = encr_info.key_len;
522 }
523
Neels Hofmeyrdb07fdc2021-06-09 22:27:47 +0200524 if (ie_kc128) {
525 memcpy(geran_encr.kc128, ie_kc128->val, 16);
526 geran_encr.kc128_present = true;
527 }
528
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100529 r->geran.chosen_encryption = &geran_encr;
530 }
531
532 if (!ie_classmark1 && !ie_classmark2) {
533 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: either Classmark Information 1"
534 " or Classmark Information 2 must be included\n");
535 return -EINVAL;
536 }
537
538 if (ie_classmark1) {
539 if (ie_classmark1->len != sizeof(classmark.classmark1)) {
540 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Invalid size for Classmark 1: %u, expected %zu\n",
541 ie_classmark1->len, sizeof(classmark.classmark1));
542 return -EINVAL;
543 }
544 memcpy((uint8_t*)&classmark.classmark1, ie_classmark1->val, ie_classmark1->len);
545 classmark.classmark1_set = true;
546 }
547
548 if (ie_classmark2) {
549 uint8_t len = OSMO_MIN(ie_classmark2->len, sizeof(classmark.classmark2));
550 memcpy((uint8_t*)&classmark.classmark2, ie_classmark2->val, len);
551 classmark.classmark2_len = len;
552 }
553
554 if (!ie_cell_id_serving) {
555 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: Cell Identifier (Serving)\n");
556 return -EINVAL;
557 }
558 if (gsm0808_dec_cell_id(&r->cell_id_serving, ie_cell_id_serving->val,
559 ie_cell_id_serving->len) <= 0) {
560 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Failed to decode Cell Identifier (Serving) IE\n");
561 return -EINVAL;
562 }
563
564 if (!ie_cell_id_target) {
565 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: Cell Identifier (Target)\n");
566 return -EINVAL;
567 }
568 if (gsm0808_dec_cell_id(&r->cell_id_target, ie_cell_id_target->val,
569 ie_cell_id_target->len) <= 0) {
570 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Failed to decode Cell Identifier (Target) IE\n");
571 return -EINVAL;
572 }
573
574 if (ie_cause)
575 r->bssap_cause = ie_cause->val[0];
576
577 if (ie_classmark3) {
578 uint8_t len = OSMO_MIN(ie_classmark3->len, sizeof(classmark.classmark3));
579 memcpy(classmark.classmark3, ie_classmark3->val, len);
580 classmark.classmark3_len = len;
581 }
582
583 if (ie_current_channel_type_1) {
584 r->current_channel_type_1 = ie_current_channel_type_1->val[0];
585 r->current_channel_type_1_present = true;
586 }
587
588 if (ie_speech_version_used) {
589 r->speech_version_used = ie_speech_version_used->val[0];
590 }
591
592 if (ie_chosen_encr_alg_serving && ie_chosen_encr_alg_serving->len) {
593 geran_encr.alg_id = ie_chosen_encr_alg_serving->val[0];
594 r->geran.chosen_encryption = &geran_encr;
595 }
596
597 if (ie_old_bss_to_new_bss_info) {
598 r->old_bss_to_new_bss_info_raw = ie_old_bss_to_new_bss_info->val;
599 r->old_bss_to_new_bss_info_raw_len = ie_old_bss_to_new_bss_info->len;
600 }
601
602 if (ie_imsi) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200603 struct osmo_mobile_identity mi;
604 if (osmo_mobile_identity_decode(&mi, ie_imsi->val, ie_imsi->len, false)
605 || mi.type != GSM_MI_TYPE_IMSI)
606 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "IE IMSI: cannot decode IMSI identity\n");
607 else
608 r->imsi = mi.imsi;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100609 }
610
611 if (ie_aoip_transp_addr) {
Pau Espin Pedrol06327172020-09-04 16:37:14 +0200612 struct sockaddr_storage rtp_addr;
613 if (gsm0808_dec_aoip_trasp_addr(&rtp_addr, ie_aoip_transp_addr->val, ie_aoip_transp_addr->len) < 0)
614 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "unable to decode AoIP transport address\n");
615 else if (osmo_sockaddr_str_from_sockaddr(&rtp_ran_local, &rtp_addr) < 0)
616 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "unable to decode remote RTP IP address\n");
617 else
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100618 r->rtp_ran_local = &rtp_ran_local;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100619 }
620
621 if (ie_codec_list_msc_preferred
622 && gsm0808_dec_speech_codec_list(&scl, ie_codec_list_msc_preferred->val,
623 ie_codec_list_msc_preferred->len) == 0)
624 r->codec_list_msc_preferred = &scl;
625
626 if (ie_call_id && ie_call_id->len == 4) {
627 r->call_id = osmo_load32le(ie_call_id->val);
628 r->call_id_present = true;
629 }
630
631 if (ie_global_call_ref) {
632 r->global_call_reference = ie_global_call_ref->val;
633 r->global_call_reference_len = ie_global_call_ref->len;
634 }
635
636 return ran_decoded(ran_dec, &ran_dec_msg);
637}
638
639static int ran_a_decode_handover_request_ack(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
640{
641 struct ran_msg ran_dec_msg = {
642 .msg_type = RAN_MSG_HANDOVER_REQUEST_ACK,
643 .msg_name = "BSSMAP Handover Request Acknowledge",
644 };
645 const struct tlv_p_entry *ie_l3_info = TLVP_GET(tp, GSM0808_IE_LAYER_3_INFORMATION);
646 const struct tlv_p_entry *ie_aoip_transp_addr = TLVP_GET(tp, GSM0808_IE_AOIP_TRASP_ADDR);
647 const struct tlv_p_entry *ie_speech_codec = TLVP_GET(tp, GSM0808_IE_SPEECH_CODEC);
648 const struct tlv_p_entry *ie_chosen_channel = TLVP_GET(tp, GSM0808_IE_CHOSEN_CHANNEL);
649 const struct tlv_p_entry *ie_chosen_encr_alg = TLVP_GET(tp, GSM0808_IE_CHOSEN_ENCR_ALG);
650 const struct tlv_p_entry *ie_chosen_speech_version = TLVP_GET(tp, GSM0808_IE_SPEECH_VERSION);
651
652 /* On missing mandatory IEs, dispatch an invalid RAN_MSG_HANDOVER_REQUEST_ACK so msc_a can act on the failure. */
653
654 if (ie_l3_info) {
655 ran_dec_msg.handover_request_ack.rr_ho_command = ie_l3_info->val;
656 ran_dec_msg.handover_request_ack.rr_ho_command_len = ie_l3_info->len;
657 }
658
659 if (ie_chosen_channel) {
660 ran_dec_msg.handover_request_ack.chosen_channel_present = true;
661 ran_dec_msg.handover_request_ack.chosen_channel = *ie_chosen_channel->val;
662 }
663
664 if (ie_chosen_encr_alg) {
665 ran_dec_msg.handover_request_ack.chosen_encr_alg = *ie_chosen_encr_alg->val;
666 if (ran_dec_msg.handover_request_ack.chosen_encr_alg < 1
667 || ran_dec_msg.handover_request_ack.chosen_encr_alg > 8) {
668 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "invalid Chosen Encryption Algorithm: %u\n",
669 ran_dec_msg.handover_request_ack.chosen_encr_alg);
670 }
671 }
672
673 if (ie_chosen_speech_version) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100674 ran_dec_msg.handover_request_ack.chosen_speech_version = ie_chosen_speech_version->val[0];
675
676 /* the codec may be extrapolated from this Speech Version or below from Speech Codec */
Neels Hofmeyr7934e0d2022-10-31 18:13:47 +0100677 if (gsm0808_speech_codec_from_chan_type(&ran_dec_msg.handover_request_ack.codec,
678 ran_dec_msg.handover_request_ack.chosen_speech_version) == 0)
679 ran_dec_msg.handover_request_ack.codec_present = true;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100680 }
681
682 if (ie_aoip_transp_addr) {
Pau Espin Pedrol06327172020-09-04 16:37:14 +0200683 struct sockaddr_storage rtp_addr;
684 if (gsm0808_dec_aoip_trasp_addr(&rtp_addr, ie_aoip_transp_addr->val, ie_aoip_transp_addr->len) < 0) {
685 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "unable to decode AoIP transport address\n");
686 } else if (osmo_sockaddr_str_from_sockaddr(&ran_dec_msg.handover_request_ack.remote_rtp,
687 &rtp_addr)) {
688 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "unable to decode remote RTP IP address\n");
689 ran_dec_msg.handover_request_ack.remote_rtp = (struct osmo_sockaddr_str){};
690 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100691 }
692
693 if (ie_speech_codec) {
Neels Hofmeyr7934e0d2022-10-31 18:13:47 +0100694 /* the codec may be extrapolated from above Speech Version or from this Speech Codec */
695 if (gsm0808_dec_speech_codec(&ran_dec_msg.handover_request_ack.codec,
696 ie_speech_codec->val, ie_speech_codec->len) < 0)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100697 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "unable to decode IE Speech Codec (Chosen)\n");
Neels Hofmeyr7934e0d2022-10-31 18:13:47 +0100698 else
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100699 ran_dec_msg.handover_request_ack.codec_present = true;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100700 }
701
702 return ran_decoded(ran_dec, &ran_dec_msg);
703}
704
705static int ran_a_decode_handover_detect(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
706{
707 struct ran_msg ran_dec_msg = {
708 .msg_type = RAN_MSG_HANDOVER_DETECT,
709 .msg_name = "BSSMAP Handover Detect",
710 };
711
712 return ran_decoded(ran_dec, &ran_dec_msg);
713}
714
715static int ran_a_decode_handover_succeeded(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
716{
717 struct ran_msg ran_dec_msg = {
718 .msg_type = RAN_MSG_HANDOVER_SUCCEEDED,
719 .msg_name = "BSSMAP Handover Succeeded",
720 };
721
722 return ran_decoded(ran_dec, &ran_dec_msg);
723}
724
725static int ran_a_decode_handover_complete(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
726{
727 struct ran_msg ran_dec_msg = {
728 .msg_type = RAN_MSG_HANDOVER_COMPLETE,
729 .msg_name = "BSSMAP Handover Complete",
730 };
731
732 return ran_decoded(ran_dec, &ran_dec_msg);
733}
734
735static int ran_a_decode_handover_failure(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
736{
737 struct ran_msg ran_dec_msg = {
738 .msg_type = RAN_MSG_HANDOVER_FAILURE,
739 .msg_name = "BSSMAP Handover Failure",
740 };
741
742 return ran_decoded(ran_dec, &ran_dec_msg);
743}
744
Andreas Eversberg2d27e2c2023-04-23 12:05:44 +0200745static int ran_a_decode_vgcs_vbs_setup_ack(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
746{
747 struct ran_msg ran_dec_msg = {
748 .msg_type = RAN_MSG_VGCS_VBS_SETUP_ACK,
749 .msg_name = "BSSMAP VGCS/VBS SETUP ACKNOWLEDGE",
750 };
751 struct gsm0808_vgcs_vbs_setup_ack *r = &ran_dec_msg.vgcs_vbs_setup_ack;
752 int rc;
753
754 const struct tlv_p_entry *ie_flags = TLVP_GET(tp, GSM0808_IE_VGCS_FEATURE_FLAGS);
755
756 /* VGCS Feature Flags, 3.2.2.88 */
757 if (ie_flags) {
758 rc = gsm0808_dec_vgcs_feature_flags(&r->flags, ie_flags->val, ie_flags->len);
759 if (rc < 0) {
760 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Unable to decode VGCS/VBS Feature Flags\n");
761 return -EINVAL;
762 }
763 r->vgcs_feature_flags_present = true;
764 }
765
766 return ran_decoded(ran_dec, &ran_dec_msg);
767}
768
769static int ran_a_decode_vgcs_vbs_setup_refuse(struct ran_dec *ran_dec, const struct msgb *msg,
770 const struct tlv_parsed *tp)
771{
772 struct ran_msg ran_dec_msg = {
773 .msg_type = RAN_MSG_VGCS_VBS_SETUP_REFUSE,
774 .msg_name = "BSSMAP VGCS/VBS SETUP REFUSE",
775 };
776
777 const struct tlv_p_entry *ie_cause = TLVP_GET(tp, GSM0808_IE_CAUSE);
778
779 /* Cause, 3.2.2.5 */
780 if (!ie_cause || ie_cause->len < 1) {
781 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: Cause\n");
782 return -EINVAL;
783 }
784 ran_dec_msg.vgcs_vbs_setup_refuse.cause = ie_cause->val[0];
785
786 return ran_decoded(ran_dec, &ran_dec_msg);
787}
788
789static int ran_a_decode_vgcs_vbs_assign_res(struct ran_dec *ran_dec, const struct msgb *msg,
790 const struct tlv_parsed *tp)
791{
792 struct ran_msg ran_dec_msg = {
793 .msg_type = RAN_MSG_VGCS_VBS_ASSIGN_RES,
794 .msg_name = "BSSMAP VGCS/VBS ASSIGNMENT RESULT",
795 };
796 struct gsm0808_vgcs_vbs_assign_res *r = &ran_dec_msg.vgcs_vbs_assign_res;
797 int rc;
798
799 const struct tlv_p_entry *ie_channel_type = TLVP_GET(tp, GSM0808_IE_CHANNEL_TYPE);
800 const struct tlv_p_entry *ie_cell_id = TLVP_GET(tp, GSM0808_IE_CELL_IDENTIFIER);
801 const struct tlv_p_entry *ie_chosen_channel = TLVP_GET(tp, GSM0808_IE_CHOSEN_CHANNEL);
802 const struct tlv_p_entry *ie_cic = TLVP_GET(tp, GSM0808_IE_CIRCUIT_IDENTITY_CODE);
803 const struct tlv_p_entry *ie_circuit_pool = TLVP_GET(tp, GSM0808_IE_CIRCUIT_POOL);
804 const struct tlv_p_entry *ie_aoip = TLVP_GET(tp, GSM0808_IE_AOIP_TRASP_ADDR);
805 const struct tlv_p_entry *ie_call_id = TLVP_GET(tp, GSM0808_IE_CALL_ID);
806
807 /* Channel Type, 3.2.2.11 */
808 if (!ie_channel_type) {
809 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: Channel Type\n");
810 return -EINVAL;
811 }
812 if (gsm0808_dec_channel_type(&r->channel_type, ie_channel_type->val, ie_channel_type->len) <= 0) {
813 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Failed to decode Channel Type IE\n");
814 return -EINVAL;
815 }
816
817 /* Cell Identifier, 3.2.2.17 */
818 if (!ie_cell_id) {
819 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: Cell Identifier\n");
820 return -EINVAL;
821 }
822 rc = gsm0808_dec_cell_id(&r->cell_identifier, ie_cell_id->val, ie_cell_id->len);
823 if (rc < 0) {
824 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Decoding Cell Identifier gave rc=%d\n", rc);
825 return -EINVAL;
826 }
827
828 /* Chosen Channel, 3.2.2.33 */
829 if (ie_chosen_channel) {
830 r->chosen_channel = ie_chosen_channel->val[0];
831 r->chosen_channel_present = true;
832 }
833
834 /* Circuit Identity Code, 3.2.2.2 */
835 if (ie_cic) {
836 if (ie_cic->len != 2) {
837 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Circuit Identity Code has invalid length.\n");
838 return -EINVAL;
839 }
840 r->cic = *(uint16_t *)ie_cic->val;
841 r->cic_present = true;
842 }
843
844 /* Circuit Pool, 3.2.2.45 */
845 if (ie_circuit_pool) {
846 r->circuit_pool = ie_circuit_pool->val[0];
847 r->circuit_pool_present = true;
848 }
849
850 /* AoIP Transport Layer Address (BSS), 3.2.2.102 */
851 if (ie_aoip) {
852 if (gsm0808_dec_aoip_trasp_addr(&r->aoip_transport_layer, ie_aoip->val, ie_aoip->len) < 0) {
853 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "unable to decode AoIP transport address\n");
854 return -EINVAL;
855 }
856 r->aoip_transport_layer_present = true;
857 }
858
859 if (ie_call_id) {
860 if (ie_call_id->len != 4) {
861 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Call Identifier has invalid length.\n");
862 return -EINVAL;
863 }
864 r->call_id = osmo_load32le(ie_call_id->val);
865 r->call_id_present = true;
866 }
867
868 return ran_decoded(ran_dec, &ran_dec_msg);
869}
870
871static int ran_a_decode_vgcs_vbs_assign_fail(struct ran_dec *ran_dec, const struct msgb *msg,
872 const struct tlv_parsed *tp)
873{
874 struct ran_msg ran_dec_msg = {
875 .msg_type = RAN_MSG_VGCS_VBS_ASSIGN_FAIL,
876 .msg_name = "BSSMAP VGCS/VBS ASSIGNMENT FAILURE",
877 };
878 struct gsm0808_vgcs_vbs_assign_fail *r = &ran_dec_msg.vgcs_vbs_assign_fail;
879 int rc;
880
881 const struct tlv_p_entry *ie_cause = TLVP_GET(tp, GSM0808_IE_CAUSE);
882 const struct tlv_p_entry *ie_circuit_pool = TLVP_GET(tp, GSM0808_IE_CIRCUIT_POOL);
883 const struct tlv_p_entry *ie_circuit_pool_list = TLVP_GET(tp, GSM0808_IE_CIRCUIT_POOL_LIST);
884 const struct tlv_p_entry *ie_codec_list_bss_supported = TLVP_GET(tp, GSM0808_IE_SPEECH_CODEC_LIST);
885
886 /* Cause, 3.2.2.5 */
887 if (!ie_cause || ie_cause->len < 1) {
888 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: Cause\n");
889 return -EINVAL;
890 }
891 r->cause = ie_cause->val[0];
892
893 /* Circuit Pool, 3.2.2.45 */
894 if (ie_circuit_pool) {
895 r->circuit_pool = ie_circuit_pool->val[0];
896 r->circuit_pool_present = true;
897 }
898
899 /* Circuit Pool List, 3.2.2.46 */
900 if (ie_circuit_pool_list && ie_circuit_pool_list->len) {
901 if (ie_circuit_pool_list->len > CIRCUIT_POOL_LIST_MAXLEN) {
902 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Circuit Pool List has invalid length.\n");
903 return -EINVAL;
904 }
905 memcpy(r->cpl.pool, ie_circuit_pool_list->val, ie_circuit_pool_list->len);
906 r->cpl.list_len = ie_circuit_pool_list->len;
907 r->cpl_present = true;
908 }
909
910 /* Codec List (BSS Supported) 3.2.2.103 */
911 if (ie_codec_list_bss_supported) {
912 rc = gsm0808_dec_speech_codec_list(&r->codec_list_bss_supported,
913 ie_codec_list_bss_supported->val, ie_codec_list_bss_supported->len);
914 if (rc < 0) {
915 LOG_RAN_A_DEC_MSG(LOGL_ERROR,
916 "Complete Layer 3 Information: unable to decode IE Codec List (BSS Supported)"
917 " (rc=%d), continuing anyway\n", rc);
918 /* This IE is not critical, do not abort with error. */
919 } else
920 r->codec_list_present = true;
921 }
922
923 return ran_decoded(ran_dec, &ran_dec_msg);
924}
925
926static int ran_a_decode_vgcs_vbs_queuing_ind(struct ran_dec *ran_dec, const struct msgb *msg,
927 const struct tlv_parsed *tp)
928{
929 struct ran_msg ran_dec_msg = {
930 .msg_type = RAN_MSG_VGCS_VBS_QUEUING_IND,
931 .msg_name = "BSSMAP VGCS/VBS QUEUING INDICATION",
932 };
933
934 return ran_decoded(ran_dec, &ran_dec_msg);
935}
936
937static int ran_a_decode_uplink_request(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
938{
939 struct ran_msg ran_dec_msg = {
940 .msg_type = RAN_MSG_UPLINK_REQUEST,
941 .msg_name = "BSSMAP UPLINK REQUEST",
942 };
943 struct gsm0808_uplink_request *r = &ran_dec_msg.uplink_request;
944 int rc;
945
946 const struct tlv_p_entry *ie_talker_priority = TLVP_GET(tp, GSM0808_IE_TALKER_PRIORITY);
947 const struct tlv_p_entry *ie_cell_id = TLVP_GET(tp, GSM0808_IE_CELL_IDENTIFIER);
948 const struct tlv_p_entry *ie_l3_info = TLVP_GET(tp, GSM0808_IE_LAYER_3_INFORMATION);
949 const struct tlv_p_entry *ie_mi = TLVP_GET(tp, GSM0808_IE_MOBILE_IDENTITY);
950
951 /* Talker Priority, 3.2.2.89 */
952 if (ie_talker_priority) {
953 r->talker_priority = ie_talker_priority->val[0] & 0x03;
954 r->talker_priority_present = true;
955 }
956
957 /* Cell Identifier, 3.2.2.17 */
958 if (ie_cell_id) {
959 rc = gsm0808_dec_cell_id(&r->cell_identifier, ie_cell_id->val, ie_cell_id->len);
960 if (rc < 0) {
961 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Decoding Cell Identifier gave rc=%d\n", rc);
962 return -EINVAL;
963 }
964 }
965
966 /* Layer 3 Information, 3.2.2.24 */
967 if (ie_l3_info && ie_l3_info->len) {
968 if (ie_l3_info->len > LAYER_3_INFORMATION_MAXLEN) {
969 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Call Identifier has invalid length.\n");
970 return -EINVAL;
971 }
972 memcpy(r->l3.l3, ie_l3_info->val, ie_l3_info->len);
973 r->l3.l3_len = ie_l3_info->len;
974 r->l3_present = true;
975 }
976
977 /* Mobile Identity, 3.2.2.41 */
978 if (ie_mi) {
Andreas Eversbergcc729292023-07-19 09:41:58 +0200979 rc = osmo_mobile_identity_decode(&r->mi, ie_mi->val, ie_mi->len, false);
980 if (rc < 0) {
Andreas Eversberg2d27e2c2023-04-23 12:05:44 +0200981 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Decoding Mobile Identity gave rc=%d\n", rc);
982 return -EINVAL;
983 }
984 r->mi_present = true;
985 }
986
987 return ran_decoded(ran_dec, &ran_dec_msg);
988}
989
990static int ran_a_decode_uplink_request_cnf(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
991{
992 struct ran_msg ran_dec_msg = {
993 .msg_type = RAN_MSG_UPLINK_REQUEST_CNF,
994 .msg_name = "BSSMAP UPLINK REQUEST CONFIRM",
995 };
996 struct gsm0808_uplink_request_cnf *r = &ran_dec_msg.uplink_request_cnf;
997 int rc;
998
999 const struct tlv_p_entry *ie_cell_id = TLVP_GET(tp, GSM0808_IE_CELL_IDENTIFIER);
1000 const struct tlv_p_entry *ie_talker_identity = TLVP_GET(tp, GSM0808_IE_TALKER_IDENTITY);
1001 const struct tlv_p_entry *ie_l3_info = TLVP_GET(tp, GSM0808_IE_LAYER_3_INFORMATION);
1002
1003 /* Cell Identifier, 3.2.2.17 */
1004 if (!ie_cell_id) {
1005 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: Cell Identifier\n");
1006 return -EINVAL;
1007 }
1008 rc = gsm0808_dec_cell_id(&r->cell_identifier, ie_cell_id->val, ie_cell_id->len);
1009 if (rc < 0) {
1010 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Decoding Cell Identifier gave rc=%d\n", rc);
1011 return -EINVAL;
1012 }
1013
1014 /* Talker Identity, 3.2.2.91 */
1015 if (ie_talker_identity) {
1016 rc = gsm0808_dec_talker_identity(&r->talker_identity, ie_talker_identity->val, ie_talker_identity->len);
1017 if (rc < 0) {
1018 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Decoding Talker Identity gave rc=%d\n", rc);
1019 return -EINVAL;
1020 }
1021 r->talker_identity_present = true;
1022 }
1023
1024 /* Layer 3 Information, 3.2.2.24 */
1025 if (!ie_l3_info) {
1026 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: Layer 3 Information\n");
1027 return -EINVAL;
1028 }
1029 if (ie_l3_info->len > LAYER_3_INFORMATION_MAXLEN) {
1030 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Call Identifier has invalid length.\n");
1031 return -EINVAL;
1032 }
1033 memcpy(r->l3.l3, ie_l3_info->val, ie_l3_info->len);
1034 r->l3.l3_len = ie_l3_info->len;
1035
1036 return ran_decoded(ran_dec, &ran_dec_msg);
1037}
1038
1039static int ran_a_decode_uplink_application_data(struct ran_dec *ran_dec, const struct msgb *msg,
1040 const struct tlv_parsed *tp)
1041{
1042 struct ran_msg ran_dec_msg = {
1043 .msg_type = RAN_MSG_UPLINK_APPLICATION_DATA,
1044 .msg_name = "BSSMAP UPLINK APPLICATION DATA",
1045 };
1046 struct gsm0808_uplink_app_data *r = &ran_dec_msg.uplink_app_data;
1047 int rc;
1048
1049 const struct tlv_p_entry *ie_cell_id = TLVP_GET(tp, GSM0808_IE_CELL_IDENTIFIER);
1050 const struct tlv_p_entry *ie_l3_info = TLVP_GET(tp, GSM0808_IE_LAYER_3_INFORMATION);
1051 const struct tlv_p_entry *ie_app_data = TLVP_GET(tp, GSM0808_IE_APP_DATA);
1052
1053 /* Cell Identifier, 3.2.2.17 */
1054 if (!ie_cell_id) {
1055 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: Cell Identifier\n");
1056 return -EINVAL;
1057 }
1058 rc = gsm0808_dec_cell_id(&r->cell_identifier, ie_cell_id->val, ie_cell_id->len);
1059 if (rc < 0) {
1060 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Decoding Cell Identifier gave rc=%d\n", rc);
1061 return -EINVAL;
1062 }
1063
1064 /* Layer 3 Information, 3.2.2.24 */
1065 if (!ie_l3_info) {
1066 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: Layer 3 Information\n");
1067 return -EINVAL;
1068 }
1069 if (ie_l3_info->len > LAYER_3_INFORMATION_MAXLEN) {
1070 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Call Identifier has invalid length.\n");
1071 return -EINVAL;
1072 }
1073 memcpy(r->l3.l3, ie_l3_info->val, ie_l3_info->len);
1074 r->l3.l3_len = ie_l3_info->len;
1075
1076 /* Application Data Information, 3.2.2.100 */
1077 if (!ie_app_data || ie_app_data->len < 1) {
1078 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: Application Data Information\n");
1079 return -EINVAL;
1080 }
1081 r->bt_ind = ie_app_data->val[0] & 0x01;
1082
1083 return ran_decoded(ran_dec, &ran_dec_msg);
1084}
1085
1086static int ran_a_decode_uplink_release_ind(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
1087{
1088 struct ran_msg ran_dec_msg = {
1089 .msg_type = RAN_MSG_UPLINK_RELEASE_IND,
1090 .msg_name = "BSSMAP UPLINK RELEASE INDICATION",
1091 };
1092 struct gsm0808_uplink_release_ind *r = &ran_dec_msg.uplink_release_ind;
1093
1094 const struct tlv_p_entry *ie_cause = TLVP_GET(tp, GSM0808_IE_CAUSE);
1095 const struct tlv_p_entry *ie_talker_priority = TLVP_GET(tp, GSM0808_IE_TALKER_PRIORITY);
1096
1097 /* Cause, 3.2.2.5 */
1098 if (!ie_cause || ie_cause->len < 1) {
1099 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: Cause\n");
1100 return -EINVAL;
1101 }
1102 r->cause = ie_cause->val[0];
1103
1104 /* Talker Priority, 3.2.2.89 */
1105 if (ie_talker_priority) {
1106 r->talker_priority = ie_talker_priority->val[0] & 0x03;
1107 r->talker_priority_present = true;
1108 }
1109
1110 return ran_decoded(ran_dec, &ran_dec_msg);
1111}
1112
1113static int ran_a_decode_vgcs_vbs_assign_status(struct ran_dec *ran_dec, const struct msgb *msg,
1114 const struct tlv_parsed *tp)
1115{
1116 struct ran_msg ran_dec_msg = {
1117 .msg_type = RAN_MSG_VGCS_VBS_ASSIGN_STATUS,
1118 .msg_name = "BSSMAP VGCS/VBS ASSIGNMENT STATUS",
1119 };
1120 struct gsm0808_vgcs_vbs_assign_stat *r = &ran_dec_msg.vgcs_vbs_assign_stat;
1121 int rc;
1122
1123 const struct tlv_p_entry *ie_cils_est = TLVP_GET(tp, GSM0808_IE_CELL_ID_LIST_SEG_EST_CELLS);
1124 const struct tlv_p_entry *ie_cils_tbe = TLVP_GET(tp, GSM0808_IE_CELL_ID_LIST_SEG_CELLS_TBE);
1125 const struct tlv_p_entry *ie_cils_rel = TLVP_GET(tp, GSM0808_IE_CELL_ID_LIST_SEG_REL_CELLS);
1126 const struct tlv_p_entry *ie_cils_ne = TLVP_GET(tp, GSM0808_IE_CELL_ID_LIST_SEG_NE_CELLS);
1127 const struct tlv_p_entry *ie_cell_status = TLVP_GET(tp, GSM0808_IE_VGCS_VBS_CELL_STATUS);
1128
1129 /* Cell Identifier List Segment, 3.2.2.27b */
1130 if (ie_cils_est) {
1131 rc = gsm0808_dec_cell_id_list_segment(&r->cils_est, ie_cils_est->val, ie_cils_est->len);
1132 if (rc < 0) {
1133 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Decoding Cell Identifier gave rc=%d\n", rc);
1134 return -EINVAL;
1135 }
1136 r->cils_est_present = true;
1137 }
1138
1139 /* Cell Identifier List Segment, 3.2.2.27c */
1140 if (ie_cils_tbe) {
1141 rc = gsm0808_dec_cell_id_list_segment(&r->cils_tbe, ie_cils_tbe->val, ie_cils_tbe->len);
1142 if (rc < 0) {
1143 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Decoding Cell Identifier gave rc=%d\n", rc);
1144 return -EINVAL;
1145 }
1146 r->cils_tbe_present = true;
1147 }
1148
1149 /* Cell Identifier List Segment, 3.2.2.27e */
1150 if (ie_cils_rel) {
1151 rc = gsm0808_dec_cell_id_list_segment(&r->cils_rel, ie_cils_rel->val, ie_cils_rel->len);
1152 if (rc < 0) {
1153 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Decoding Cell Identifier gave rc=%d\n", rc);
1154 return -EINVAL;
1155 }
1156 r->cils_rel_present = true;
1157 }
1158
1159 /* Cell Identifier List Segment, 3.2.2.27f */
1160 if (ie_cils_ne) {
1161 rc = gsm0808_dec_cell_id_list_segment(&r->cils_ne, ie_cils_ne->val, ie_cils_ne->len);
1162 if (rc < 0) {
1163 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Decoding Cell Identifier gave rc=%d\n", rc);
1164 return -EINVAL;
1165 }
1166 r->cils_ne_present = true;
1167 }
1168
1169 /* VGCS/VBS Cell Status, 3.2.2.94 */
1170 if (ie_cell_status && ie_cell_status->len) {
1171 r->cell_status = ie_cell_status->val[0] & 0x73;
1172 r->cell_status_present = true;
1173 }
1174
1175 return ran_decoded(ran_dec, &ran_dec_msg);
1176}
1177
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001178static int ran_a_decode_bssmap(struct ran_dec *ran_dec, struct msgb *bssmap)
1179{
1180 struct tlv_parsed tp[2];
1181 int rc;
1182 struct bssmap_header *h = msgb_l2(bssmap);
1183 uint8_t msg_type;
1184 bssmap->l3h = bssmap->l2h + sizeof(*h);
1185
1186 if (msgb_l3len(bssmap) < 1) {
1187 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "No data received, discarding message\n");
1188 return -1;
1189 }
1190
1191 if (msgb_l3len(bssmap) < h->length) {
Neels Hofmeyrf0923012019-08-22 17:19:49 +02001192 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "BSSMAP data truncated, discarding message:"
1193 " msgb_l3len(bssmap) == %u < bssmap_header->length == %u\n",
1194 msgb_l3len(bssmap), h->length);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001195 return -1;
1196 }
1197
1198 if (msgb_l3len(bssmap) > h->length) {
Neels Hofmeyrf0923012019-08-22 17:19:49 +02001199 LOG_RAN_A_DEC(ran_dec, LOGL_NOTICE, "There are %u extra bytes after the BSSMAP data, truncating:"
1200 " msgb_l3len(bssmap) == %u > bssmap_header->length == %u\n",
1201 msgb_l3len(bssmap) - h->length,
1202 msgb_l3len(bssmap), h->length);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001203 msgb_l3trim(bssmap, h->length);
1204 }
1205
1206 /* h->type == BSSAP_MSG_BSS_MANAGEMENT; h->length is the data length,
1207 * which starts with the MAP msg_type, followed by IEs. */
1208 msg_type = bssmap->l3h[0];
1209 rc = osmo_bssap_tlv_parse2(tp, ARRAY_SIZE(tp), bssmap->l3h + 1, h->length - 1);
1210 if (rc < 0) {
1211 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "Failed parsing TLV, discarding message\n");
1212 return -EINVAL;
1213 }
1214
Neels Hofmeyr72fc7062019-10-08 06:24:17 +02001215 LOG_RAN_A_DEC(ran_dec, LOGL_DEBUG, "%s\n", gsm0808_bssmap_name(msg_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001216
1217 switch (msg_type) {
1218 case BSS_MAP_MSG_COMPLETE_LAYER_3:
1219 return ran_a_decode_l3_compl(ran_dec, bssmap, tp);
1220 case BSS_MAP_MSG_CLEAR_RQST:
1221 return ran_a_decode_clear_request(ran_dec, bssmap, tp);
1222 case BSS_MAP_MSG_CLEAR_COMPLETE:
1223 return ran_a_decode_clear_complete(ran_dec, bssmap, tp);
1224 case BSS_MAP_MSG_CLASSMARK_UPDATE:
1225 return ran_a_decode_classmark_update(ran_dec, bssmap, tp);
1226 case BSS_MAP_MSG_CIPHER_MODE_COMPLETE:
1227 return ran_a_decode_cipher_mode_complete(ran_dec, bssmap, tp);
1228 case BSS_MAP_MSG_CIPHER_MODE_REJECT:
1229 return ran_a_decode_cipher_mode_reject(ran_dec, bssmap, tp);
1230 case BSS_MAP_MSG_ASSIGMENT_COMPLETE:
1231 rc = ran_a_decode_assignment_complete(ran_dec, bssmap, tp);
1232 if (rc < 0) {
1233 struct ran_msg ran_dec_msg = {
1234 .msg_type = RAN_MSG_ASSIGNMENT_FAILURE,
1235 .msg_name = "BSSMAP Assignment Complete but failed to decode",
1236 .clear_request = {
1237 .bssap_cause = GSM0808_CAUSE_EQUIPMENT_FAILURE,
1238 },
1239 };
1240 ran_decoded(ran_dec, &ran_dec_msg);
1241 }
1242 return rc;
1243 case BSS_MAP_MSG_ASSIGMENT_FAILURE:
1244 return ran_a_decode_assignment_failure(ran_dec, bssmap, tp);
1245 case BSS_MAP_MSG_SAPI_N_REJECT:
1246 return ran_a_decode_sapi_n_reject(ran_dec, bssmap, tp);
1247 case BSS_MAP_MSG_LCLS_NOTIFICATION:
1248 return ran_a_decode_lcls_notification(ran_dec, bssmap, tp);
Andreas Eversberg2d27e2c2023-04-23 12:05:44 +02001249 case BSS_MAP_MSG_VGCS_VBS_SETUP_ACK:
1250 return ran_a_decode_vgcs_vbs_setup_ack(ran_dec, bssmap, tp);
1251 case BSS_MAP_MSG_VGCS_VBS_SETUP_REFUSE:
1252 return ran_a_decode_vgcs_vbs_setup_refuse(ran_dec, bssmap, tp);
1253 case BSS_MAP_MSG_VGCS_VBS_ASSIGNMENT_RESULT:
1254 return ran_a_decode_vgcs_vbs_assign_res(ran_dec, bssmap, tp);
1255 case BSS_MAP_MSG_VGCS_VBS_ASSIGNMENT_FAILURE:
1256 return ran_a_decode_vgcs_vbs_assign_fail(ran_dec, bssmap, tp);
1257 case BSS_MAP_MSG_VGCS_VBS_QUEUING_INDICATION:
1258 return ran_a_decode_vgcs_vbs_queuing_ind(ran_dec, bssmap, tp);
1259 case BSS_MAP_MSG_UPLINK_RQST:
1260 return ran_a_decode_uplink_request(ran_dec, bssmap, tp);
1261 case BSS_MAP_MSG_UPLINK_RQST_CONFIRMATION:
1262 return ran_a_decode_uplink_request_cnf(ran_dec, bssmap, tp);
1263 case BSS_MAP_MSG_UPLINK_APP_DATA:
1264 return ran_a_decode_uplink_application_data(ran_dec, bssmap, tp);
1265 case BSS_MAP_MSG_UPLINK_RELEASE_INDICATION:
1266 return ran_a_decode_uplink_release_ind(ran_dec, bssmap, tp);
1267 case BSS_MAP_MSG_VGCS_VBS_ASSIGNMENT_STATUS:
1268 return ran_a_decode_vgcs_vbs_assign_status(ran_dec, bssmap, tp);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001269
1270 /* From current RAN peer, the Handover origin: */
1271 case BSS_MAP_MSG_HANDOVER_REQUIRED:
1272 return ran_a_decode_handover_required(ran_dec, bssmap, tp);
1273
1274 /* From current MSC to remote handover target MSC */
1275 case BSS_MAP_MSG_HANDOVER_RQST:
1276 return ran_a_decode_handover_request(ran_dec, bssmap, tp);
1277
1278 /* From potential new RAN peer, the Handover target: */
1279 case BSS_MAP_MSG_HANDOVER_RQST_ACKNOWLEDGE:
1280 return ran_a_decode_handover_request_ack(ran_dec, bssmap, tp);
1281 case BSS_MAP_MSG_HANDOVER_DETECT:
1282 return ran_a_decode_handover_detect(ran_dec, bssmap, tp);
1283 case BSS_MAP_MSG_HANDOVER_SUCCEEDED:
1284 return ran_a_decode_handover_succeeded(ran_dec, bssmap, tp);
1285 case BSS_MAP_MSG_HANDOVER_COMPLETE:
1286 return ran_a_decode_handover_complete(ran_dec, bssmap, tp);
1287
1288 /* From any Handover peer: */
1289 case BSS_MAP_MSG_HANDOVER_FAILURE:
1290 return ran_a_decode_handover_failure(ran_dec, bssmap, tp);
1291
1292 default:
1293 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "Unimplemented msg type: %s\n", gsm0808_bssmap_name(msg_type));
1294 return -EINVAL;
1295 }
1296
1297 return -EINVAL;
1298}
1299
1300static int ran_a_decode_l3(struct ran_dec *ran_dec, struct msgb *l3)
1301{
1302 struct dtap_header *dtap = msgb_l2(l3);
1303 struct ran_msg ran_dec_msg = {
1304 .msg_type = RAN_MSG_DTAP,
1305 .msg_name = "BSSAP DTAP",
1306 .dtap = l3,
1307 };
1308 l3->l3h = l3->l2h + sizeof(struct dtap_header);
1309 OMSC_LINKID_CB(l3) = dtap->link_id;
1310 return ran_decoded(ran_dec, &ran_dec_msg);
1311}
1312
1313int ran_a_decode_l2(struct ran_dec *ran_dec, struct msgb *bssap)
1314{
1315 uint8_t bssap_type;
1316 OSMO_ASSERT(bssap);
1317
1318 if (!msgb_l2(bssap) || !msgb_l2len(bssap)) {
1319 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "Cannot decode L2, msg->l2h is unset / empty: %s\n",
1320 msgb_hexdump(bssap));
1321 return -EINVAL;
1322 }
1323
1324 if (msgb_l2len(bssap) < sizeof(struct bssmap_header)) {
1325 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "The header is too short -- discarding message\n");
1326 return -EINVAL;
1327 }
1328
1329 bssap_type = bssap->l2h[0];
1330 switch (bssap_type) {
1331 case BSSAP_MSG_BSS_MANAGEMENT:
1332 return ran_a_decode_bssmap(ran_dec, bssap);
1333 case BSSAP_MSG_DTAP:
1334 return ran_a_decode_l3(ran_dec, bssap);
1335 default:
1336 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "Unimplemented BSSAP msg type: %s\n", gsm0808_bssap_name(bssap_type));
1337 return -EINVAL;
1338 }
1339}
1340
1341static struct msgb *ran_a_wrap_dtap(struct msgb *dtap)
1342{
1343 struct msgb *an_apdu;
1344 dtap->l3h = dtap->data;
1345 an_apdu = gsm0808_create_dtap(dtap, OMSC_LINKID_CB(dtap));
1346 an_apdu->l2h = an_apdu->data;
1347 msgb_free(dtap);
1348 return an_apdu;
1349}
1350
1351static int ran_a_channel_type_to_speech_codec_list(struct gsm0808_speech_codec_list *scl, const struct gsm0808_channel_type *ct)
1352{
1353 unsigned int i;
1354 int rc;
1355
1356 memset(scl, 0, sizeof(*scl));
Oliver Smithb1a15882023-05-23 13:54:36 +02001357
1358 switch (ct->ch_indctr) {
1359 case GSM0808_CHAN_DATA:
1360 scl->codec[0].type = GSM0808_SCT_CSD;
1361 scl->len = 1;
1362 break;
1363 case GSM0808_CHAN_SPEECH:
1364 for (i = 0; i < ct->perm_spch_len; i++) {
1365 rc = gsm0808_speech_codec_from_chan_type(&scl->codec[i], ct->perm_spch[i]);
1366 if (rc != 0)
1367 return -EINVAL;
1368 }
1369 scl->len = i;
1370 break;
1371 default:
1372 OSMO_ASSERT(0);
1373 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001374 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001375
1376 return 0;
1377}
1378
1379/* Compose a BSSAP Assignment Command.
1380 * Passing an RTP address is optional.
1381 * The msub is passed merely for error logging. */
1382static struct msgb *ran_a_make_assignment_command(struct osmo_fsm_inst *log_fi,
1383 const struct ran_assignment_command *ac)
1384{
1385 struct gsm0808_speech_codec_list scl;
1386 struct gsm0808_speech_codec_list *use_scl = NULL;
1387 struct sockaddr_storage rtp_addr;
1388 struct sockaddr_storage *use_rtp_addr = NULL;
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001389 struct msgb *msg;
Philipp Maierf34d9452020-06-05 15:49:35 +02001390 const uint32_t *call_id = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001391 int rc;
1392
1393 if (!ac->channel_type) {
1394 LOG_RAN_A_ENC(log_fi, LOGL_ERROR, "Assignment Command: missing Channel Type\n");
1395 return NULL;
1396 }
1397
Oliver Smithb1a15882023-05-23 13:54:36 +02001398 if (ac->channel_type->ch_indctr == GSM0808_CHAN_SPEECH || ac->channel_type->ch_indctr == GSM0808_CHAN_DATA) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001399 rc = ran_a_channel_type_to_speech_codec_list(&scl, ac->channel_type);
1400 if (rc < 0) {
1401 LOG_RAN_A_ENC(log_fi, LOGL_ERROR, "Assignment Command: Cannot translate Channel Type to Speech Codec List\n");
1402 return NULL;
1403 }
1404 use_scl = &scl;
1405
1406 /* Package RTP-Address data */
Neels Hofmeyr84ce2062019-10-05 05:15:25 +02001407 if (osmo_sockaddr_str_is_nonzero(ac->cn_rtp)) {
Pau Espin Pedrold35abfa2020-08-31 20:44:50 +02001408 struct sockaddr_in *sin;
1409 struct sockaddr_in6 *sin6;
1410 int family = osmo_ip_str_type(ac->cn_rtp->ip);
1411 switch (family) {
1412 case AF_INET:
1413 sin = (struct sockaddr_in *)&rtp_addr;
1414 sin->sin_family = AF_INET;
1415 sin->sin_port = osmo_htons(ac->cn_rtp->port);
1416 if (inet_pton(AF_INET, ac->cn_rtp->ip, &sin->sin_addr) != 1) {
1417 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
1418 "Assignment Command: Invalid RTP-Address %s\n",
1419 ac->cn_rtp->ip);
1420 return NULL;
1421 }
1422 if (sin->sin_port == 0) {
1423 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
1424 "Assignment Command: Invalid RTP-Port\n");
1425 return NULL;
1426 }
1427 break;
1428 case AF_INET6:
1429 sin6 = (struct sockaddr_in6 *)&rtp_addr;
1430 sin6->sin6_family = AF_INET6;
1431 sin6->sin6_port = osmo_htons(ac->cn_rtp->port);
1432 if (inet_pton(AF_INET6, ac->cn_rtp->ip, &sin6->sin6_addr) != 1) {
1433 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
1434 "Assignment Command: Invalid RTP-Address %s\n",
1435 ac->cn_rtp->ip);
1436 return NULL;
1437 }
1438 if (sin6->sin6_port == 0) {
1439 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
1440 "Assignment Command: Invalid RTP-Port\n");
1441 return NULL;
1442 }
1443 break;
1444 default:
1445 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
1446 "Assignment Command: Invalid RTP-Address type for %s\n",
1447 ac->cn_rtp->ip);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001448 return NULL;
1449 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001450 use_rtp_addr = &rtp_addr;
1451 }
1452 }
1453
Philipp Maierf34d9452020-06-05 15:49:35 +02001454 if(ac->call_id_present == true)
1455 call_id = &ac->call_id;
1456
Keith Whytea1a70be2021-05-16 02:59:52 +02001457 msg = gsm0808_create_ass2(ac->channel_type, NULL, use_rtp_addr, use_scl, call_id,
1458 NULL, ac->lcls);
Vadim Yanitskiy8284fb02022-12-14 04:47:58 +07001459 if (msg == NULL) {
1460 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
1461 "Failed to encode BSSMAP Assignment Request message\n");
1462 return NULL;
1463 }
1464
Andreas Eversberg9bbdc342023-06-21 13:35:38 +02001465 /* Append optional IEs: Group Call Reference and Osmux CID */
1466 OSMO_ASSERT(msg->l3h[1] == msgb_l3len(msg) - 2); /* TL not in len */
1467 if (ac->callref_present)
1468 gsm0808_enc_group_callref(msg, &ac->callref);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001469 if (ac->osmux_present)
Andreas Eversberg9bbdc342023-06-21 13:35:38 +02001470 msgb_tv_put(msg, GSM0808_IE_OSMO_OSMUX_CID, ac->osmux_cid);
1471 msg->l3h[1] = msgb_l3len(msg) - 2;
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001472 return msg;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001473}
1474
1475/* For an A5/N number a5_n set dst to the matching GSM0808_ALG_ID_A5_<n>. */
1476static int a5_n_to_gsm0808_chosen_enc_alg(uint8_t *dst, int a5_n)
1477{
1478 switch (a5_n) {
1479 case 0:
1480 *dst = GSM0808_ALG_ID_A5_0;
1481 return 0;
1482 case 1:
1483 *dst = GSM0808_ALG_ID_A5_1;
1484 return 0;
1485 case 2:
1486 *dst = GSM0808_ALG_ID_A5_2;
1487 return 0;
1488 case 3:
1489 *dst = GSM0808_ALG_ID_A5_3;
1490 return 0;
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +02001491 case 4:
1492 *dst = GSM0808_ALG_ID_A5_4;
1493 return 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001494 default:
1495 return -ENOTSUP;
1496 }
1497}
1498
1499static int make_encrypt_info_perm_algo(struct osmo_fsm_inst *fi, struct gsm0808_encrypt_info *ei,
1500 uint8_t a5_encryption_mask, const struct osmo_gsm48_classmark *cm)
1501{
1502 int i;
1503 int j = 0;
1504 for (i = 0; i < 8; i++) {
1505 int supported;
1506
1507 /* A5/n permitted by osmo-msc.cfg? */
1508 if (!(a5_encryption_mask & (1 << i)))
1509 continue;
1510
1511 /* A5/n supported by MS? */
1512 supported = osmo_gsm48_classmark_supports_a5(cm, i);
1513 if (supported != 1)
1514 continue;
1515
1516 if (a5_n_to_gsm0808_chosen_enc_alg(&ei->perm_algo[j], i)) {
1517 LOG_RAN_A_ENC(fi, LOGL_ERROR, "Not supported: A5/%d algorithm\n", i);
1518 return -1;
1519 }
1520 j++;
1521 ei->perm_algo_len = j;
1522 }
1523 return 0;
1524}
1525
1526/* For ran_a_make_cipher_mode_command(), for
1527 * memcpy(ei.key, cm->vec->kc, sizeof(cm->vec->kc));
1528 */
1529osmo_static_assert(sizeof(((struct gsm0808_encrypt_info*)0)->key) >= sizeof(((struct osmo_auth_vector*)0)->kc),
1530 gsm0808_encrypt_info_key_fits_osmo_auth_vec_kc);
1531static struct msgb *ran_a_make_cipher_mode_command(struct osmo_fsm_inst *fi, const struct ran_cipher_mode_command *cm)
1532{
Neels Hofmeyrcdcfc802021-06-09 22:26:11 +02001533 struct gsm0808_cipher_mode_command cmc = {
1534 .cipher_response_mode_present = true,
1535 .cipher_response_mode = 1, /* 1: include IMEISV (3GPP TS 48.008 3.2.2.34) */
1536 };
1537 struct gsm0808_encrypt_info *ei = &cmc.ei;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001538 char buf[16 * 2 + 1];
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001539
Neels Hofmeyrcdcfc802021-06-09 22:26:11 +02001540 if (make_encrypt_info_perm_algo(fi, ei, cm->geran.a5_encryption_mask, cm->classmark))
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001541 return NULL;
1542
Neels Hofmeyrcdcfc802021-06-09 22:26:11 +02001543 if (ei->perm_algo_len == 0) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001544 LOG_RAN_A_ENC(fi, LOGL_ERROR, "cannot start ciphering, no intersection between MSC-configured"
1545 " and MS-supported A5 algorithms. MSC: 0x%02x MS: %s\n",
1546 cm->geran.a5_encryption_mask, osmo_gsm48_classmark_a5_name(cm->classmark));
1547 return NULL;
1548 }
1549
1550 /* In case of UMTS AKA, the Kc for ciphering must be derived from the 3G auth
1551 * tokens. vec->kc was calculated from the GSM algorithm and is not
1552 * necessarily a match for the UMTS AKA tokens. */
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +02001553 if (cm->geran.umts_aka) {
1554 int i;
Neels Hofmeyrcdcfc802021-06-09 22:26:11 +02001555 osmo_auth_c3(ei->key, cm->vec->ck, cm->vec->ik);
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +02001556
1557 for (i = 0; i < ei->perm_algo_len; i++) {
1558 if (ei->perm_algo[i] != GSM0808_ALG_ID_A5_4)
1559 continue;
1560 /* A5/4 is included, so need to generate Kc128 */
1561 osmo_kdf_kc128(cm->vec->ck, cm->vec->ik, cmc.kc128);
1562 cmc.kc128_present = true;
1563 break;
1564 }
1565 } else {
Neels Hofmeyrcdcfc802021-06-09 22:26:11 +02001566 memcpy(ei->key, cm->vec->kc, sizeof(cm->vec->kc));
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +02001567 }
Neels Hofmeyrcdcfc802021-06-09 22:26:11 +02001568 ei->key_len = sizeof(cm->vec->kc);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001569
1570 /* Store chosen GERAN key where the caller asked it to be stored.
1571 * alg_id remains unknown until we receive a Cipher Mode Complete from the BSC */
1572 if (cm->geran.chosen_key) {
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +02001573 *cm->geran.chosen_key = (struct geran_encr){0};
1574
Neels Hofmeyrcdcfc802021-06-09 22:26:11 +02001575 if (ei->key_len > sizeof(cm->geran.chosen_key->key)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001576 LOG_RAN_A_ENC(fi, LOGL_ERROR, "Chosen key is larger than I can store\n");
1577 return NULL;
1578 }
Neels Hofmeyrcdcfc802021-06-09 22:26:11 +02001579 memcpy(cm->geran.chosen_key->key, ei->key, ei->key_len);
1580 cm->geran.chosen_key->key_len = ei->key_len;
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +02001581
1582 if (cmc.kc128_present) {
1583 memcpy(cm->geran.chosen_key->kc128, cmc.kc128, 16);
1584 cm->geran.chosen_key->kc128_present = true;
1585 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001586 }
1587
1588 LOG_RAN_A_ENC(fi, LOGL_DEBUG, "Tx BSSMAP CIPHER MODE COMMAND to BSC, %u ciphers (%s) key %s\n",
Neels Hofmeyrcdcfc802021-06-09 22:26:11 +02001589 ei->perm_algo_len, osmo_hexdump_nospc(ei->perm_algo, ei->perm_algo_len),
1590 osmo_hexdump_buf(buf, sizeof(buf), ei->key, ei->key_len, NULL, false));
1591 return gsm0808_create_cipher2(&cmc);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001592}
1593
1594struct msgb *ran_a_make_handover_request(struct osmo_fsm_inst *log_fi, const struct ran_handover_request *n)
1595{
1596 struct sockaddr_storage ss;
1597 struct gsm0808_handover_request r = {
1598 .cell_identifier_serving = n->cell_id_serving,
1599 .cell_identifier_target = n->cell_id_target,
1600 .cause = n->bssap_cause,
1601 .current_channel_type_1_present = n->current_channel_type_1_present,
1602 .current_channel_type_1 = n->current_channel_type_1,
1603
1604 .speech_version_used = n->speech_version_used,
1605
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001606 .old_bss_to_new_bss_info_raw = n->old_bss_to_new_bss_info_raw,
1607 .old_bss_to_new_bss_info_raw_len = n->old_bss_to_new_bss_info_raw_len,
1608
1609 .imsi = n->imsi,
1610 .codec_list_msc_preferred = n->codec_list_msc_preferred,
Philipp Maier7da956e2020-06-09 14:34:40 +02001611 .call_id_present = n->call_id_present,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001612 .call_id = n->call_id,
1613 .global_call_reference = n->global_call_reference,
1614 .global_call_reference_len = n->global_call_reference_len,
1615 };
1616
1617 if (!n->geran.channel_type) {
1618 LOG_RAN_A_ENC(log_fi, LOGL_ERROR, "Channel Type required for encoding Handover Request in BSSAP\n");
1619 return NULL;
1620 }
1621 r.channel_type = *n->geran.channel_type;
1622
1623 /* Encryption Information */
1624 make_encrypt_info_perm_algo(log_fi, &r.encryption_information, n->geran.a5_encryption_mask, n->classmark);
1625 if (n->geran.chosen_encryption && n->geran.chosen_encryption->key_len) {
Vadim Yanitskiy444771d2019-05-11 04:46:24 +07001626 /* Prevent both source / destination buffer overrun / overflow */
1627 if (n->geran.chosen_encryption->key_len > sizeof(r.encryption_information.key)
1628 || n->geran.chosen_encryption->key_len > sizeof(n->geran.chosen_encryption->key)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001629 LOG_RAN_A_ENC(log_fi, LOGL_ERROR, "Handover Request: invalid chosen encryption key size %u\n",
1630 n->geran.chosen_encryption->key_len);
1631 return NULL;
1632 }
1633 memcpy(r.encryption_information.key,
1634 n->geran.chosen_encryption->key, n->geran.chosen_encryption->key_len);
1635 r.encryption_information.key_len = n->geran.chosen_encryption->key_len;
Vadim Yanitskiybfe8eb72019-05-11 03:52:28 +07001636 r.chosen_encryption_algorithm_serving = n->geran.chosen_encryption->alg_id;
Neels Hofmeyrdb07fdc2021-06-09 22:27:47 +02001637
1638 if (n->geran.chosen_encryption->kc128_present) {
1639 r.more_items = true;
1640 memcpy(r.kc128, n->geran.chosen_encryption->kc128, sizeof(r.kc128));
1641 r.kc128_present = true;
1642 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001643 }
1644
1645 if (n->classmark)
1646 r.classmark_information = *n->classmark;
1647
Neels Hofmeyr84ce2062019-10-05 05:15:25 +02001648 if (osmo_sockaddr_str_is_nonzero(n->rtp_ran_local)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001649 if (osmo_sockaddr_str_to_sockaddr(n->rtp_ran_local, &ss)) {
1650 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
1651 "Handover Request: invalid AoIP Transport Layer address/port: "
1652 OSMO_SOCKADDR_STR_FMT "\n", OSMO_SOCKADDR_STR_FMT_ARGS(n->rtp_ran_local));
1653 return NULL;
1654 }
1655 r.aoip_transport_layer = &ss;
1656 }
1657
1658 return gsm0808_create_handover_request(&r);
1659}
1660
1661static struct msgb *ran_a_make_handover_request_ack(struct osmo_fsm_inst *caller_fi, const struct ran_handover_request_ack *r)
1662{
1663 struct sockaddr_storage ss;
1664 struct gsm0808_handover_request_ack params = {
1665 .l3_info = r->rr_ho_command,
1666 .l3_info_len = r->rr_ho_command_len,
1667 .chosen_channel_present = r->chosen_channel_present,
1668 .chosen_channel = r->chosen_channel,
1669 .chosen_encr_alg = r->chosen_encr_alg,
1670 .chosen_speech_version = r->chosen_speech_version,
1671 };
1672
Neels Hofmeyr84ce2062019-10-05 05:15:25 +02001673 if (osmo_sockaddr_str_is_nonzero(&r->remote_rtp)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001674 osmo_sockaddr_str_to_sockaddr(&r->remote_rtp, &ss);
1675 params.aoip_transport_layer = &ss;
1676 }
1677
1678 return gsm0808_create_handover_request_ack2(&params);
1679}
1680
1681struct msgb *ran_a_make_handover_command(struct osmo_fsm_inst *log_fi, const struct ran_handover_command *n)
1682{
1683 struct gsm0808_handover_command c = {
1684 .l3_info = n->rr_ho_command,
1685 .l3_info_len = n->rr_ho_command_len,
1686 };
1687
1688 return gsm0808_create_handover_command(&c);
1689}
1690
1691struct msgb *ran_a_make_handover_failure(struct osmo_fsm_inst *log_fi, const struct ran_msg *msg)
1692{
1693 struct gsm0808_handover_failure params = {
1694 .cause = msg->handover_failure.cause,
1695 };
1696 return gsm0808_create_handover_failure(&params);
1697}
1698
1699static struct msgb *_ran_a_encode(struct osmo_fsm_inst *caller_fi, const struct ran_msg *ran_enc_msg)
1700{
1701
1702 LOG_RAN_A_ENC(caller_fi, LOGL_DEBUG, "%s\n", ran_msg_type_name(ran_enc_msg->msg_type));
1703
1704 switch (ran_enc_msg->msg_type) {
1705
1706 case RAN_MSG_DTAP:
1707 return ran_a_wrap_dtap(ran_enc_msg->dtap);
1708
1709 case RAN_MSG_CLASSMARK_REQUEST:
1710 return gsm0808_create_classmark_request();
1711
1712 case RAN_MSG_CLEAR_COMMAND:
1713 return gsm0808_create_clear_command2(ran_enc_msg->clear_command.gsm0808_cause,
1714 ran_enc_msg->clear_command.csfb_ind);
1715
1716 case RAN_MSG_ASSIGNMENT_COMMAND:
1717 return ran_a_make_assignment_command(caller_fi, &ran_enc_msg->assignment_command);
1718
Harald Welte544a32f2020-06-21 22:15:53 +02001719 case RAN_MSG_COMMON_ID:
Pau Espin Pedrol67106702021-04-27 18:20:15 +02001720 return gsm0808_create_common_id(ran_enc_msg->common_id.imsi, NULL,
1721 ran_enc_msg->common_id.last_eutran_plmn_present ?
1722 &ran_enc_msg->common_id.last_eutran_plmn :
1723 NULL
1724 );
Harald Welte544a32f2020-06-21 22:15:53 +02001725
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001726 case RAN_MSG_CIPHER_MODE_COMMAND:
1727 return ran_a_make_cipher_mode_command(caller_fi, &ran_enc_msg->cipher_mode_command);
1728
1729 case RAN_MSG_HANDOVER_REQUIRED_REJECT:
1730 return gsm0808_create_handover_required_reject(&ran_enc_msg->handover_required_reject);
1731
1732 case RAN_MSG_HANDOVER_REQUEST:
1733 return ran_a_make_handover_request(caller_fi, &ran_enc_msg->handover_request);
1734
1735 case RAN_MSG_HANDOVER_REQUEST_ACK:
1736 return ran_a_make_handover_request_ack(caller_fi, &ran_enc_msg->handover_request_ack);
1737
1738 case RAN_MSG_HANDOVER_COMMAND:
1739 return ran_a_make_handover_command(caller_fi, &ran_enc_msg->handover_command);
1740
1741 case RAN_MSG_HANDOVER_SUCCEEDED:
1742 return gsm0808_create_handover_succeeded();
1743
1744 case RAN_MSG_HANDOVER_FAILURE:
1745 return ran_a_make_handover_failure(caller_fi, ran_enc_msg);
1746
Andreas Eversberg2d27e2c2023-04-23 12:05:44 +02001747 case RAN_MSG_VGCS_VBS_SETUP:
1748 return gsm0808_create_vgcs_vbs_setup(&ran_enc_msg->vgcs_vbs_setup);
1749
1750 case RAN_MSG_VGCS_VBS_ASSIGN_REQ:
1751 return gsm0808_create_vgcs_vbs_assign_req(&ran_enc_msg->vgcs_vbs_assign_req);
1752
1753 case RAN_MSG_UPLINK_REQUEST_ACK:
1754 return gsm0808_create_uplink_request_ack(&ran_enc_msg->uplink_request_ack);
1755
1756 case RAN_MSG_UPLINK_REJECT_CMD:
1757 return gsm0808_create_uplink_reject_cmd(&ran_enc_msg->uplink_reject_cmd);
1758
1759 case RAN_MSG_UPLINK_RELEASE_CMD:
1760 return gsm0808_create_uplink_release_cmd(ran_enc_msg->uplink_release_cmd.cause);
1761
1762 case RAN_MSG_UPLINK_SEIZED_CMD:
1763 return gsm0808_create_uplink_seized_cmd(&ran_enc_msg->uplink_seized_cmd);
1764
1765 case RAN_MSG_VGCS_ADDITIONAL_INFO:
1766 return gsm0808_create_vgcs_additional_info(&ran_enc_msg->vgcs_additional_info.talker_identity);
1767
1768 case RAN_MSG_VGCS_VBS_AREA_CELL_INFO:
1769 return gsm0808_create_vgcs_vbs_area_cell_info(&ran_enc_msg->vgcs_vbs_area_cell_info);
1770
1771 case RAN_MSG_VGCS_SMS:
1772 return gsm0808_create_vgcs_sms(&ran_enc_msg->vgcs_sms.sms_to_vgcs);
1773
1774 case RAN_MSG_NOTIFICATION_DATA:
1775 return gsm0808_create_notification_data(&ran_enc_msg->notification_data);
1776
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001777 default:
1778 LOG_RAN_A_ENC(caller_fi, LOGL_ERROR, "Unimplemented RAN-encode message type: %s\n",
1779 ran_msg_type_name(ran_enc_msg->msg_type));
1780 return NULL;
1781 }
1782}
1783
1784struct msgb *ran_a_encode(struct osmo_fsm_inst *caller_fi, const struct ran_msg *ran_enc_msg)
1785{
1786 struct msgb *msg = _ran_a_encode(caller_fi, ran_enc_msg);
1787
1788 if (!msg)
1789 return NULL;
1790
1791 msg->l2h = msg->data;
1792
1793 /* some consistency checks to ensure we don't send invalid length */
1794 switch (msg->l2h[0]) {
1795 case BSSAP_MSG_DTAP:
1796 OSMO_ASSERT(msgb_l2len(msg) == msg->l2h[2] + 3);
1797 break;
1798 case BSSAP_MSG_BSS_MANAGEMENT:
1799 OSMO_ASSERT(msgb_l2len(msg) == msg->l2h[1] + 2);
1800 break;
1801 default:
1802 break;
1803 }
1804
1805 return msg;
1806}
1807
Neels Hofmeyrb6972742020-06-26 15:20:51 +02001808static void cl_parse_osmux(struct osmo_fsm_inst *log_fi, struct msgb *msg, int *supports_osmux)
1809{
1810 struct tlv_parsed tp;
1811 int rc;
1812
1813 if (supports_osmux == NULL)
1814 return;
1815
1816 rc = tlv_parse(&tp, gsm0808_att_tlvdef(), msgb_l3(msg) + 1, msgb_l3len(msg) - 1, 0, 0);
1817 if (rc < 0) {
1818 LOGPFSMSL(log_fi, DBSSAP, LOGL_ERROR, "BSSMAP: Failed parsing TLV looking for Osmux support\n");
1819 return;
1820 }
1821
1822 if (TLVP_PRESENT(&tp, GSM0808_IE_OSMO_OSMUX_SUPPORT)) {
1823 *supports_osmux = true;
1824 } else {
1825 *supports_osmux = false;
1826 }
1827}
1828
1829/* Return 1 for a RESET, 2 for a RESET ACK message, 0 otherwise.
1830 * In supports_osmux, return 0 for no information, 1 for support detected, -1 for non-support detected. */
1831enum reset_msg_type bssmap_is_reset_msg(const struct sccp_ran_inst *sri, struct osmo_fsm_inst *log_fi,
1832 struct msgb *l2, int *supports_osmux)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001833{
1834 struct bssmap_header *bs = (struct bssmap_header *)msgb_l2(l2);
1835
Neels Hofmeyrb6972742020-06-26 15:20:51 +02001836 if (supports_osmux != NULL)
1837 *supports_osmux = 0;
1838
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001839 if (!bs
1840 || msgb_l2len(l2) < (sizeof(*bs) + 1)
1841 || bs->type != BSSAP_MSG_BSS_MANAGEMENT)
1842 return SCCP_RAN_MSG_NON_RESET;
1843
Neels Hofmeyrb6972742020-06-26 15:20:51 +02001844 l2->l3h = l2->l2h + sizeof(struct bssmap_header);
1845
1846 switch (l2->l3h[0]) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001847 case BSS_MAP_MSG_RESET:
Neels Hofmeyrb6972742020-06-26 15:20:51 +02001848 cl_parse_osmux(log_fi, l2, supports_osmux);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001849 return SCCP_RAN_MSG_RESET;
1850 case BSS_MAP_MSG_RESET_ACKNOWLEDGE:
Neels Hofmeyrb6972742020-06-26 15:20:51 +02001851 cl_parse_osmux(log_fi, l2, supports_osmux);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001852 return SCCP_RAN_MSG_RESET_ACK;
1853 default:
1854 return SCCP_RAN_MSG_NON_RESET;
1855 }
1856}
1857
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001858/* Patch regular BSSMAP RESET to add extra T to announce Osmux support (osmocom extension) */
1859static void _gsm0808_extend_announce_osmux(struct msgb *msg)
1860{
1861 OSMO_ASSERT(msg->l3h[1] == msgb_l3len(msg) - 2); /*TL not in len */
1862 msgb_put_u8(msg, GSM0808_IE_OSMO_OSMUX_SUPPORT);
1863 msg->l3h[1] = msgb_l3len(msg) - 2;
1864}
1865
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001866struct msgb *bssmap_make_reset_msg(const struct sccp_ran_inst *sri, enum reset_msg_type type)
1867{
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001868 struct gsm_network *net = sri->user_data;
1869 struct msgb *msg;
1870
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001871 switch (type) {
1872 case SCCP_RAN_MSG_RESET:
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001873 msg = gsm0808_create_reset();
1874 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001875 case SCCP_RAN_MSG_RESET_ACK:
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001876 msg = gsm0808_create_reset_ack();
1877 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001878 default:
1879 return NULL;
1880 }
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001881
1882 if (!msg)
1883 return NULL;
1884
1885 if (net->use_osmux != OSMUX_USAGE_OFF)
1886 _gsm0808_extend_announce_osmux(msg);
1887
1888 return msg;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001889}
1890
1891struct msgb *bssmap_make_paging_msg(const struct sccp_ran_inst *sri, const struct gsm0808_cell_id *page_cell_id,
1892 const char *imsi, uint32_t tmsi, enum paging_cause cause)
1893{
1894 struct gsm0808_cell_id_list2 cil;
1895 gsm0808_cell_id_to_list(&cil, page_cell_id);
1896 return gsm0808_create_paging2(imsi, tmsi == GSM_RESERVED_TMSI ? NULL : &tmsi, &cil, NULL);
1897}
1898
1899const char *bssmap_msg_name(const struct sccp_ran_inst *sri, const struct msgb *l2)
1900{
1901 struct bssmap_header *bs;
1902
1903 if (!l2->l2h)
1904 return "?";
1905
1906 bs = (struct bssmap_header *)msgb_l2(l2);
1907 switch (bs->type) {
1908 case BSSAP_MSG_BSS_MANAGEMENT:
1909 return gsm0808_bssmap_name(l2->l2h[0]);
1910 case BSSAP_MSG_DTAP:
1911 return "DTAP";
1912 default:
1913 return "?";
1914 }
1915}