blob: 106c2dff03e879653a8eb3e5a1c540595b5a7ce9 [file] [log] [blame]
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001/* BSSAP/BSSMAP encoding and decoding for MSC */
2/*
3 * (C) 2019 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de>
4 * 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.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 */
24
25#include <osmocom/core/byteswap.h>
26
27#include <osmocom/crypt/auth.h>
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +020028#include <osmocom/crypt/kdf.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010029
30#include <osmocom/gsm/tlv.h>
31#include <osmocom/gsm/gsm0808.h>
32#include <osmocom/gsm/mncc.h>
33#include <osmocom/gsm/gsm48.h>
34
35#include <osmocom/msc/debug.h>
36#include <osmocom/msc/ran_msg_a.h>
37#include <osmocom/msc/sccp_ran.h>
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +020038#include <osmocom/msc/gsm_data.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010039
40#define LOG_RAN_A_DEC(RAN_DEC, level, fmt, args...) \
41 LOG_RAN_DEC(RAN_DEC, DBSSAP, level, "BSSMAP: " fmt, ## args)
42
43/* Assumes presence of struct ran_dec *ran_dec and ran_dec_msg.msg_name (set) in the local scope. */
44#define LOG_RAN_A_DEC_MSG(level, fmt, args...) \
45 LOG_RAN_DEC(ran_dec, DBSSAP, level, "%s: " fmt, ran_dec_msg.msg_name, ## args)
46
47#define LOG_RAN_A_ENC(FI, level, fmt, args...) \
48 LOG_RAN_ENC(FI, DBSSAP, level, "BSSMAP: " fmt, ## args)
49
50static int ran_a_decode_l3_compl(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
51{
52 struct gsm0808_cell_id_list2 cil;
53 struct gsm0808_cell_id cell_id;
54 struct tlv_p_entry *ie_cell_id = TLVP_GET(tp, GSM0808_IE_CELL_IDENTIFIER);
55 struct tlv_p_entry *ie_l3_info = TLVP_GET(tp, GSM0808_IE_LAYER_3_INFORMATION);
Neels Hofmeyr8a50cfb2019-10-21 03:01:00 +020056 struct tlv_p_entry *ie_codec_list_bss_supported = TLVP_GET(tp, GSM0808_IE_SPEECH_CODEC_LIST);
57 struct gsm0808_speech_codec_list codec_list_bss_supported;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010058 struct ran_msg ran_dec_msg = {
59 .msg_type = RAN_MSG_COMPL_L3,
Neels Hofmeyr0c1ed152019-10-21 03:12:58 +020060 .msg_name = "BSSMAP Complete Layer 3 Information",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010061 .compl_l3 = {
62 .cell_id = &cell_id,
63 .msg = msg,
64 },
65 };
66 int rc;
67
68 if (!ie_cell_id) {
69 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory CELL IDENTIFIER not present, discarding message\n");
70 return -EINVAL;
71 }
72 if (!ie_l3_info) {
73 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory LAYER 3 INFORMATION not present, discarding message\n");
74 return -EINVAL;
75 }
76
77 /* Parse Cell ID element -- this should yield a cell identifier "list" with 1 element. */
78
79 rc = gsm0808_dec_cell_id_list2(&cil, ie_cell_id->val, ie_cell_id->len);
80 if (rc < 0) {
81 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Decoding CELL IDENTIFIER gave rc=%d\n", rc);
82 return -EINVAL;
83 }
84 if (cil.id_list_len != 1) {
85 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Unable to parse element CELL IDENTIFIER, discarding message\n");
86 return -EINVAL;
87 }
88
89 /* Sanity check the Cell Identity */
90 switch (cil.id_discr) {
91 case CELL_IDENT_WHOLE_GLOBAL:
92 case CELL_IDENT_LAI_AND_LAC:
93 case CELL_IDENT_LAC_AND_CI:
94 case CELL_IDENT_LAC:
95 break;
96
97 case CELL_IDENT_CI:
98 case CELL_IDENT_NO_CELL:
99 case CELL_IDENT_BSS:
100 default:
101 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "CELL IDENTIFIER does not specify a LAC, discarding message: %s\n",
102 gsm0808_cell_id_list_name(&cil));
103 return -EINVAL;
104 }
105
106 cell_id = (struct gsm0808_cell_id){
107 .id_discr = cil.id_discr,
108 .id = cil.id_list[0],
109 };
110
111 /* Parse Layer 3 Information element */
112 msg->l3h = (uint8_t*)ie_l3_info->val;
113 msgb_l3trim(msg, ie_l3_info->len);
114
115 if (msgb_l3len(msg) < sizeof(struct gsm48_hdr)) {
116 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "too short L3 info (%d), discarding message\n", msgb_l3len(msg));
117 return -ENODATA;
118 }
119
Neels Hofmeyr8a50cfb2019-10-21 03:01:00 +0200120 /* Decode Codec List (BSS Supported) */
121 if (ie_codec_list_bss_supported) {
122 rc = gsm0808_dec_speech_codec_list(&codec_list_bss_supported,
123 ie_codec_list_bss_supported->val, ie_codec_list_bss_supported->len);
124 if (rc < 0) {
125 LOG_RAN_A_DEC_MSG(LOGL_ERROR,
126 "Complete Layer 3 Information: unable to decode IE Codec List (BSS Supported)"
127 " (rc=%d), continuing anyway\n", rc);
128 /* This IE is not critical, do not abort with error. */
129 } else
130 ran_dec_msg.compl_l3.codec_list_bss_supported = &codec_list_bss_supported;
131 }
132
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100133 return ran_decoded(ran_dec, &ran_dec_msg);
134}
135
136static int ran_a_decode_clear_request(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
137{
138 struct tlv_p_entry *ie_cause = TLVP_GET(tp, GSM0808_IE_CAUSE);
139 struct ran_msg ran_dec_msg = {
140 .msg_type = RAN_MSG_CLEAR_REQUEST,
141 .msg_name = "BSSMAP Clear Request",
142 };
143
144 if (!ie_cause) {
145 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Cause code is missing, using GSM0808_CAUSE_EQUIPMENT_FAILURE\n");
146 ran_dec_msg.clear_request.bssap_cause = GSM0808_CAUSE_EQUIPMENT_FAILURE;
147 } else {
148 ran_dec_msg.clear_request.bssap_cause = ie_cause->val[0];
149 }
150
151 return ran_decoded(ran_dec, &ran_dec_msg);
152}
153
154static int ran_a_decode_clear_complete(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
155{
156 struct ran_msg ran_dec_msg = {
157 .msg_type = RAN_MSG_CLEAR_COMPLETE,
158 .msg_name = "BSSMAP Clear Complete",
159 };
160 return ran_decoded(ran_dec, &ran_dec_msg);
161}
162
163static int ran_a_decode_classmark_update(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
164{
165 struct tlv_p_entry *ie_cm2 = TLVP_GET(tp, GSM0808_IE_CLASSMARK_INFORMATION_T2);
166 struct tlv_p_entry *ie_cm3 = TLVP_GET(tp, GSM0808_IE_CLASSMARK_INFORMATION_T3);
167 struct osmo_gsm48_classmark cm = {};
168 struct ran_msg ran_dec_msg = {
169 .msg_type = RAN_MSG_CLASSMARK_UPDATE,
170 .msg_name = "BSSMAP Classmark Update",
171 .classmark_update = {
172 .classmark = &cm,
173 },
174 };
175
176 if (!ie_cm2) {
177 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "mandatory Classmark Information Type 2 not present, discarding message\n");
178 return -EINVAL;
179 }
180
181 cm.classmark2_len = OSMO_MIN(sizeof(cm.classmark2), ie_cm2->len);
182 memcpy(&cm.classmark2, ie_cm2->val, cm.classmark2_len);
183
184 if (ie_cm3) {
185 cm.classmark3_len = OSMO_MIN(sizeof(cm.classmark3), ie_cm3->len);
186 memcpy(&cm.classmark3, ie_cm3->val, cm.classmark3_len);
187 }
188
189 return ran_decoded(ran_dec, &ran_dec_msg);
190}
191
192static int ran_a_decode_cipher_mode_complete(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
193{
194 struct tlv_p_entry *ie_chosen_encr_alg = TLVP_GET(tp, GSM0808_IE_CHOSEN_ENCR_ALG);
195 struct tlv_p_entry *ie_l3_msg = TLVP_GET(tp, GSM0808_IE_LAYER_3_MESSAGE_CONTENTS);
196 int rc;
197 struct ran_msg ran_dec_msg = {
198 .msg_type = RAN_MSG_CIPHER_MODE_COMPLETE,
199 .msg_name = "BSSMAP Ciphering Mode Complete",
200 };
201
202 if (ie_chosen_encr_alg) {
203 uint8_t ie_val = ie_chosen_encr_alg->val[0];
204 /* 3GPP TS 48.008 3.2.2.44 Chosen Encryption Algorithm encodes as 1 = no encryption, 2 = A5/1, 4 = A5/3.
205 * Internally we handle without this weird off-by-one. */
206 if (ie_val < 1 || ie_val > 8)
207 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Unsupported value for 3.2.2.44 Chosen Encryption Algorithm: %u\n",
208 ie_val);
209 else
210 ran_dec_msg.cipher_mode_complete.alg_id = ie_chosen_encr_alg->val[0];
211 }
212
Neels Hofmeyre9a39112019-08-29 00:10:49 +0200213 if (ie_l3_msg)
214 ran_dec_msg.cipher_mode_complete.l3_msg = ie_l3_msg;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100215
Neels Hofmeyre9a39112019-08-29 00:10:49 +0200216 rc = ran_decoded(ran_dec, &ran_dec_msg);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100217
218 return rc;
219}
220
221static int ran_a_decode_cipher_mode_reject(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
222{
223 int rc;
224 struct ran_msg ran_dec_msg = {
225 .msg_type = RAN_MSG_CIPHER_MODE_REJECT,
226 .msg_name = "BSSMAP Ciphering Mode Reject",
227 };
228
Vadim Yanitskiy33144f12021-02-05 20:14:19 +0100229 rc = gsm0808_get_cause(tp);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100230 if (rc < 0) {
231 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "failed to extract Cause\n");
232 ran_dec_msg.cipher_mode_reject.bssap_cause = GSM0808_CAUSE_EQUIPMENT_FAILURE;
233 } else {
234 ran_dec_msg.cipher_mode_reject.bssap_cause = (enum gsm0808_cause)rc;
235 }
236
237 return ran_decoded(ran_dec, &ran_dec_msg);
238}
239
240enum mgcp_codecs ran_a_mgcp_codec_from_sc(const struct gsm0808_speech_codec *sc)
241{
242 switch (sc->type) {
243 case GSM0808_SCT_FR1:
244 return CODEC_GSM_8000_1;
245 break;
246 case GSM0808_SCT_FR2:
247 return CODEC_GSMEFR_8000_1;
248 break;
249 case GSM0808_SCT_FR3:
250 return CODEC_AMR_8000_1;
251 break;
252 case GSM0808_SCT_FR4:
253 return CODEC_AMRWB_16000_1;
254 break;
255 case GSM0808_SCT_FR5:
256 return CODEC_AMRWB_16000_1;
257 break;
258 case GSM0808_SCT_HR1:
259 return CODEC_GSMHR_8000_1;
260 break;
261 case GSM0808_SCT_HR3:
262 return CODEC_AMR_8000_1;
263 break;
264 case GSM0808_SCT_HR4:
265 return CODEC_AMRWB_16000_1;
266 break;
267 case GSM0808_SCT_HR6:
268 return CODEC_AMRWB_16000_1;
269 break;
270 default:
271 return CODEC_PCMU_8000_1;
272 break;
273 }
274}
275
276static int ran_a_decode_assignment_complete(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
277{
278 struct tlv_p_entry *ie_aoip_transp_addr = TLVP_GET(tp, GSM0808_IE_AOIP_TRASP_ADDR);
279 struct tlv_p_entry *ie_speech_codec = TLVP_GET(tp, GSM0808_IE_SPEECH_CODEC);
Neels Hofmeyr8a50cfb2019-10-21 03:01:00 +0200280 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 +0200281 struct tlv_p_entry *ie_osmux_cid = TLVP_GET(tp, GSM0808_IE_OSMO_OSMUX_CID);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100282 struct sockaddr_storage rtp_addr;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100283 struct gsm0808_speech_codec sc;
Neels Hofmeyr8a50cfb2019-10-21 03:01:00 +0200284 struct gsm0808_speech_codec_list codec_list_bss_supported;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100285 int rc;
286 struct ran_msg ran_dec_msg = {
287 .msg_type = RAN_MSG_ASSIGNMENT_COMPLETE,
288 .msg_name = "BSSMAP Assignment Complete",
289 };
290
291 if (ie_aoip_transp_addr) {
292 /* Decode AoIP transport address element */
293 rc = gsm0808_dec_aoip_trasp_addr(&rtp_addr, ie_aoip_transp_addr->val, ie_aoip_transp_addr->len);
294 if (rc < 0) {
295 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Unable to decode AoIP Transport Layer Address\n");
296 return -EINVAL;
297 }
298
Pau Espin Pedrolf9c76e32020-09-02 19:25:55 +0200299 if (osmo_sockaddr_str_from_sockaddr(&ran_dec_msg.assignment_complete.remote_rtp, &rtp_addr)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100300 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Assignment Complete: unable to decode remote RTP IP address\n");
301 return -EINVAL;
302 }
303 }
304
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200305 if (ie_osmux_cid) {
306 rc = gsm0808_dec_osmux_cid(&ran_dec_msg.assignment_complete.osmux_cid, ie_osmux_cid->val, ie_osmux_cid->len);
307 if (rc < 0) {
308 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Unable to decode Osmux CID\n");
309 return -EINVAL;
310 }
311 ran_dec_msg.assignment_complete.osmux_present = true;
312 }
313
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100314 if (ie_speech_codec) {
315 /* Decode Speech Codec (Chosen) element */
316 rc = gsm0808_dec_speech_codec(&sc, ie_speech_codec->val, ie_speech_codec->len);
317 if (rc < 0) {
318 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Assignment Complete: unable to decode IE Speech Codec (Chosen)"
319 " (rc=%d).\n", rc);
320 return -EINVAL;
321 }
322 ran_dec_msg.assignment_complete.codec_present = true;
323 ran_dec_msg.assignment_complete.codec = ran_a_mgcp_codec_from_sc(&sc);
324 }
325
Neels Hofmeyr8a50cfb2019-10-21 03:01:00 +0200326 if (ie_codec_list_bss_supported) {
327 /* Decode Codec List (BSS Supported) */
328 rc = gsm0808_dec_speech_codec_list(&codec_list_bss_supported,
329 ie_codec_list_bss_supported->val, ie_codec_list_bss_supported->len);
330 if (rc < 0) {
331 LOG_RAN_A_DEC_MSG(LOGL_ERROR,
332 "Assignment Complete: unable to decode IE Codec List (BSS Supported)"
333 " (rc=%d), continuing anyway\n", rc);
334 /* This IE is not critical, do not abort with error. */
335 } else
336 ran_dec_msg.assignment_complete.codec_list_bss_supported = &codec_list_bss_supported;
337 }
338
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100339 return ran_decoded(ran_dec, &ran_dec_msg);
340}
341
342static int ran_a_decode_assignment_failure(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
343{
344 struct tlv_p_entry *ie_cause = TLVP_GET(tp, GSM0808_IE_CAUSE);
345 struct tlv_p_entry *ie_rr_cause = TLVP_GET(tp, GSM0808_IE_RR_CAUSE);
346 struct tlv_p_entry *ie_speech_codec_list = TLVP_GET(tp, GSM0808_IE_SPEECH_CODEC_LIST);
347 struct gsm0808_speech_codec_list scl;
348 struct ran_msg ran_dec_msg = {
349 .msg_type = RAN_MSG_ASSIGNMENT_FAILURE,
350 .msg_name = "BSSMAP Assignment Failure",
351 .assignment_failure = {
352 .bssap_cause = GSM0808_CAUSE_EQUIPMENT_FAILURE,
353 .rr_cause = GSM48_RR_CAUSE_ABNORMAL_UNSPEC,
354 },
355 };
356
357 if (ie_cause)
358 ran_dec_msg.assignment_failure.bssap_cause = ie_cause->val[0];
359 if (ie_rr_cause)
360 ran_dec_msg.assignment_failure.rr_cause = ie_rr_cause->val[0];
361
362 if (ie_speech_codec_list
363 && gsm0808_dec_speech_codec_list(&scl, ie_speech_codec_list->val, ie_speech_codec_list->len) == 0)
364 ran_dec_msg.assignment_failure.scl_bss_supported = &scl;
365
366 return ran_decoded(ran_dec, &ran_dec_msg);
367}
368
369static int ran_a_decode_sapi_n_reject(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
370{
371 struct tlv_p_entry *ie_cause = TLVP_GET(tp, GSM0808_IE_CAUSE);
372 struct tlv_p_entry *ie_dlci = TLVP_GET(tp, GSM0808_IE_DLCI);
373 struct ran_msg ran_dec_msg = {
374 .msg_type = RAN_MSG_SAPI_N_REJECT,
375 .msg_name = "BSSMAP SAPI-N Reject",
376 };
377
378 /* Note: The MSC code seems not to care about the cause code, but by
379 * the specification it is mandatory, so we check its presence. See
380 * also 3GPP TS 48.008 3.2.1.34 SAPI "n" REJECT */
381 if (!ie_cause) {
382 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "SAPI-N Reject: cause code IE is missing, discarding message\n");
383 return -EINVAL;
384 }
385 ran_dec_msg.sapi_n_reject.bssap_cause = ie_cause->val[0];
386
387 if (!ie_dlci) {
388 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "SAPI-N Reject: DLCI IE is missing, discarding message\n");
389 return -EINVAL;
390 }
391 ran_dec_msg.sapi_n_reject.dlci = ie_dlci->val[0];
392
393 return ran_decoded(ran_dec, &ran_dec_msg);
394}
395
396static int ran_a_decode_lcls_notification(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
397{
398 const struct tlv_p_entry *ie_lcls_bss_status = TLVP_GET(tp, GSM0808_IE_LCLS_BSS_STATUS);
399 const struct tlv_p_entry *ie_lcls_break_req = TLVP_GET(tp, GSM0808_IE_LCLS_BREAK_REQ);
400 struct ran_msg ran_dec_msg;
401
402 /* 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 +0700403 if (ie_lcls_bss_status && !ie_lcls_break_req) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100404 ran_dec_msg = (struct ran_msg){
405 .msg_type = RAN_MSG_LCLS_STATUS,
406 .msg_name = "BSSMAP LCLS Notification (LCLS Status)",
407 .lcls_status = {
408 .status = ie_lcls_bss_status->len ?
409 ie_lcls_bss_status->val[0] : GSM0808_LCLS_STS_NA,
410 },
411 };
412 return ran_decoded(ran_dec, &ran_dec_msg);
Vadim Yanitskiy18e8b392019-05-11 04:22:55 +0700413 } else if (ie_lcls_break_req && !ie_lcls_bss_status) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100414 ran_dec_msg = (struct ran_msg){
415 .msg_type = RAN_MSG_LCLS_BREAK_REQ,
416 .msg_name = "BSSMAP LCLS Notification (LCLS Break Req)",
417 .lcls_break_req = {
418 .todo = 23,
419 },
420 };
421 return ran_decoded(ran_dec, &ran_dec_msg);
422 }
423
Vadim Yanitskiy18e8b392019-05-11 04:22:55 +0700424 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "Ignoring broken LCLS Notification message\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100425 return -EINVAL;
426}
427
428static int ran_a_decode_handover_required(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
429{
430 const struct tlv_p_entry *ie_cause = TLVP_GET(tp, GSM0808_IE_CAUSE);
431 const struct tlv_p_entry *ie_cil = TLVP_GET(tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
432 struct ran_msg ran_dec_msg = {
433 .msg_type = RAN_MSG_HANDOVER_REQUIRED,
434 .msg_name = "BSSMAP Handover Required",
435 };
436 /* On decoding failures, dispatch an invalid RAN_MSG_HANDOVER_REQUIRED so msc_a can pass down a
437 * BSS_MAP_MSG_HANDOVER_REQUIRED_REJECT message. */
438
439 if (ie_cause)
440 ran_dec_msg.handover_required.cause = ie_cause->val[0];
441 else
442 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Cause IE missing\n");
443
444 if (!ie_cil
445 || gsm0808_dec_cell_id_list2(&ran_dec_msg.handover_required.cil, ie_cil->val, ie_cil->len) <= 0) {
446 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "No or invalid Cell Identifier List IE\n");
447 ran_dec_msg.handover_required.cil = (struct gsm0808_cell_id_list2){};
448 }
449
450 return ran_decoded(ran_dec, &ran_dec_msg);
451}
452
453static uint8_t a5_encryption_mask_from_gsm0808_chosen_enc_alg(enum gsm0808_chosen_enc_alg val)
454{
455 return 1 << val;
456}
457
458static int ran_a_decode_handover_request(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
459{
460 struct osmo_gsm48_classmark classmark = {};
461 struct ran_msg ran_dec_msg = {
462 .msg_type = RAN_MSG_HANDOVER_REQUEST,
463 .msg_name = "BSSMAP Handover Request",
464 .handover_request = {
465 .classmark = &classmark,
466 },
467 };
468 struct ran_handover_request *r = &ran_dec_msg.handover_request;
469
470 const struct tlv_p_entry *ie_channel_type = TLVP_GET(tp, GSM0808_IE_CHANNEL_TYPE);
471 const struct tlv_p_entry *ie_encryption_information = TLVP_GET(tp, GSM0808_IE_ENCRYPTION_INFORMATION);
472 const struct tlv_p_entry *ie_classmark1 = TLVP_GET(tp, GSM0808_IE_CLASSMARK_INFORMATION_TYPE_1);
473 const struct tlv_p_entry *ie_classmark2 = TLVP_GET(tp, GSM0808_IE_CLASSMARK_INFORMATION_T2);
474 const struct tlv_p_entry *ie_cell_id_serving = TLVP_GET(&tp[0], GSM0808_IE_CELL_IDENTIFIER);
475 const struct tlv_p_entry *ie_cell_id_target = TLVP_GET(&tp[1], GSM0808_IE_CELL_IDENTIFIER);
476 const struct tlv_p_entry *ie_cause = TLVP_GET(tp, GSM0808_IE_CAUSE);
477 const struct tlv_p_entry *ie_classmark3 = TLVP_GET(tp, GSM0808_IE_CLASSMARK_INFORMATION_T3);
478 const struct tlv_p_entry *ie_current_channel_type_1 = TLVP_GET(tp, GSM0808_IE_CURRENT_CHANNEL_TYPE_1);
479 const struct tlv_p_entry *ie_speech_version_used = TLVP_GET(tp, GSM0808_IE_SPEECH_VERSION);
480 const struct tlv_p_entry *ie_chosen_encr_alg_serving = TLVP_GET(tp, GSM0808_IE_CHOSEN_ENCR_ALG);
481 const struct tlv_p_entry *ie_old_bss_to_new_bss_info = TLVP_GET(tp, GSM0808_IE_OLD_BSS_TO_NEW_BSS_INFORMATION);
482 const struct tlv_p_entry *ie_imsi = TLVP_GET(tp, GSM0808_IE_IMSI);
483 const struct tlv_p_entry *ie_aoip_transp_addr = TLVP_GET(tp, GSM0808_IE_AOIP_TRASP_ADDR);
484 const struct tlv_p_entry *ie_codec_list_msc_preferred = TLVP_GET(tp, GSM0808_IE_SPEECH_CODEC_LIST);
485 const struct tlv_p_entry *ie_call_id = TLVP_GET(tp, GSM0808_IE_CALL_ID);
486 const struct tlv_p_entry *ie_global_call_ref = TLVP_GET(tp, GSM0808_IE_GLOBAL_CALL_REF);
487
488 struct gsm0808_channel_type channel_type;
489 struct gsm0808_encrypt_info encr_info;
490 struct gsm0808_speech_codec_list scl;
491 struct geran_encr geran_encr = {};
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100492 struct osmo_sockaddr_str rtp_ran_local;
493
494 if (!ie_channel_type) {
495 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: Channel Type\n");
496 return -EINVAL;
497 }
498 if (gsm0808_dec_channel_type(&channel_type, ie_channel_type->val, ie_channel_type->len) <= 0) {
499 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Failed to decode Channel Type IE\n");
500 return -EINVAL;
501 }
502 r->geran.channel_type = &channel_type;
503
504 if (ie_encryption_information) {
505 int i;
506 if (gsm0808_dec_encrypt_info(&encr_info, ie_encryption_information->val, ie_encryption_information->len)
507 <= 0) {
Martin Hauke3f07dac2019-11-14 17:49:08 +0100508 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Failed to decode Encryption Information IE\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100509 return -EINVAL;
510 }
511
512 for (i = 0; i < encr_info.perm_algo_len; i++) {
513 r->geran.a5_encryption_mask |=
514 a5_encryption_mask_from_gsm0808_chosen_enc_alg(encr_info.perm_algo[i]);
515 }
516
517 if (encr_info.key_len > sizeof(geran_encr.key)) {
Martin Hauke3f07dac2019-11-14 17:49:08 +0100518 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Failed to decode Encryption Information IE:"
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100519 " encryption key is too long: %u\n", geran_encr.key_len);
520 return -EINVAL;
521 }
522
523 if (encr_info.key_len) {
524 memcpy(geran_encr.key, encr_info.key, encr_info.key_len);
525 geran_encr.key_len = encr_info.key_len;
526 }
527
528 r->geran.chosen_encryption = &geran_encr;
529 }
530
531 if (!ie_classmark1 && !ie_classmark2) {
532 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: either Classmark Information 1"
533 " or Classmark Information 2 must be included\n");
534 return -EINVAL;
535 }
536
537 if (ie_classmark1) {
538 if (ie_classmark1->len != sizeof(classmark.classmark1)) {
539 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Invalid size for Classmark 1: %u, expected %zu\n",
540 ie_classmark1->len, sizeof(classmark.classmark1));
541 return -EINVAL;
542 }
543 memcpy((uint8_t*)&classmark.classmark1, ie_classmark1->val, ie_classmark1->len);
544 classmark.classmark1_set = true;
545 }
546
547 if (ie_classmark2) {
548 uint8_t len = OSMO_MIN(ie_classmark2->len, sizeof(classmark.classmark2));
549 memcpy((uint8_t*)&classmark.classmark2, ie_classmark2->val, len);
550 classmark.classmark2_len = len;
551 }
552
553 if (!ie_cell_id_serving) {
554 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: Cell Identifier (Serving)\n");
555 return -EINVAL;
556 }
557 if (gsm0808_dec_cell_id(&r->cell_id_serving, ie_cell_id_serving->val,
558 ie_cell_id_serving->len) <= 0) {
559 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Failed to decode Cell Identifier (Serving) IE\n");
560 return -EINVAL;
561 }
562
563 if (!ie_cell_id_target) {
564 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: Cell Identifier (Target)\n");
565 return -EINVAL;
566 }
567 if (gsm0808_dec_cell_id(&r->cell_id_target, ie_cell_id_target->val,
568 ie_cell_id_target->len) <= 0) {
569 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Failed to decode Cell Identifier (Target) IE\n");
570 return -EINVAL;
571 }
572
573 if (ie_cause)
574 r->bssap_cause = ie_cause->val[0];
575
576 if (ie_classmark3) {
577 uint8_t len = OSMO_MIN(ie_classmark3->len, sizeof(classmark.classmark3));
578 memcpy(classmark.classmark3, ie_classmark3->val, len);
579 classmark.classmark3_len = len;
580 }
581
582 if (ie_current_channel_type_1) {
583 r->current_channel_type_1 = ie_current_channel_type_1->val[0];
584 r->current_channel_type_1_present = true;
585 }
586
587 if (ie_speech_version_used) {
588 r->speech_version_used = ie_speech_version_used->val[0];
589 }
590
591 if (ie_chosen_encr_alg_serving && ie_chosen_encr_alg_serving->len) {
592 geran_encr.alg_id = ie_chosen_encr_alg_serving->val[0];
593 r->geran.chosen_encryption = &geran_encr;
594 }
595
596 if (ie_old_bss_to_new_bss_info) {
597 r->old_bss_to_new_bss_info_raw = ie_old_bss_to_new_bss_info->val;
598 r->old_bss_to_new_bss_info_raw_len = ie_old_bss_to_new_bss_info->len;
599 }
600
601 if (ie_imsi) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200602 struct osmo_mobile_identity mi;
603 if (osmo_mobile_identity_decode(&mi, ie_imsi->val, ie_imsi->len, false)
604 || mi.type != GSM_MI_TYPE_IMSI)
605 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "IE IMSI: cannot decode IMSI identity\n");
606 else
607 r->imsi = mi.imsi;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100608 }
609
610 if (ie_aoip_transp_addr) {
Pau Espin Pedrol06327172020-09-04 16:37:14 +0200611 struct sockaddr_storage rtp_addr;
612 if (gsm0808_dec_aoip_trasp_addr(&rtp_addr, ie_aoip_transp_addr->val, ie_aoip_transp_addr->len) < 0)
613 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "unable to decode AoIP transport address\n");
614 else if (osmo_sockaddr_str_from_sockaddr(&rtp_ran_local, &rtp_addr) < 0)
615 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "unable to decode remote RTP IP address\n");
616 else
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100617 r->rtp_ran_local = &rtp_ran_local;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100618 }
619
620 if (ie_codec_list_msc_preferred
621 && gsm0808_dec_speech_codec_list(&scl, ie_codec_list_msc_preferred->val,
622 ie_codec_list_msc_preferred->len) == 0)
623 r->codec_list_msc_preferred = &scl;
624
625 if (ie_call_id && ie_call_id->len == 4) {
626 r->call_id = osmo_load32le(ie_call_id->val);
627 r->call_id_present = true;
628 }
629
630 if (ie_global_call_ref) {
631 r->global_call_reference = ie_global_call_ref->val;
632 r->global_call_reference_len = ie_global_call_ref->len;
633 }
634
635 return ran_decoded(ran_dec, &ran_dec_msg);
636}
637
638static int ran_a_decode_handover_request_ack(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
639{
640 struct ran_msg ran_dec_msg = {
641 .msg_type = RAN_MSG_HANDOVER_REQUEST_ACK,
642 .msg_name = "BSSMAP Handover Request Acknowledge",
643 };
644 const struct tlv_p_entry *ie_l3_info = TLVP_GET(tp, GSM0808_IE_LAYER_3_INFORMATION);
645 const struct tlv_p_entry *ie_aoip_transp_addr = TLVP_GET(tp, GSM0808_IE_AOIP_TRASP_ADDR);
646 const struct tlv_p_entry *ie_speech_codec = TLVP_GET(tp, GSM0808_IE_SPEECH_CODEC);
647 const struct tlv_p_entry *ie_chosen_channel = TLVP_GET(tp, GSM0808_IE_CHOSEN_CHANNEL);
648 const struct tlv_p_entry *ie_chosen_encr_alg = TLVP_GET(tp, GSM0808_IE_CHOSEN_ENCR_ALG);
649 const struct tlv_p_entry *ie_chosen_speech_version = TLVP_GET(tp, GSM0808_IE_SPEECH_VERSION);
650
651 /* On missing mandatory IEs, dispatch an invalid RAN_MSG_HANDOVER_REQUEST_ACK so msc_a can act on the failure. */
652
653 if (ie_l3_info) {
654 ran_dec_msg.handover_request_ack.rr_ho_command = ie_l3_info->val;
655 ran_dec_msg.handover_request_ack.rr_ho_command_len = ie_l3_info->len;
656 }
657
658 if (ie_chosen_channel) {
659 ran_dec_msg.handover_request_ack.chosen_channel_present = true;
660 ran_dec_msg.handover_request_ack.chosen_channel = *ie_chosen_channel->val;
661 }
662
663 if (ie_chosen_encr_alg) {
664 ran_dec_msg.handover_request_ack.chosen_encr_alg = *ie_chosen_encr_alg->val;
665 if (ran_dec_msg.handover_request_ack.chosen_encr_alg < 1
666 || ran_dec_msg.handover_request_ack.chosen_encr_alg > 8) {
667 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "invalid Chosen Encryption Algorithm: %u\n",
668 ran_dec_msg.handover_request_ack.chosen_encr_alg);
669 }
670 }
671
672 if (ie_chosen_speech_version) {
673 struct gsm0808_speech_codec sc;
674 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 */
677 gsm0808_speech_codec_from_chan_type(&sc, ran_dec_msg.handover_request_ack.chosen_speech_version);
678 ran_dec_msg.handover_request_ack.codec_present = true;
679 ran_dec_msg.handover_request_ack.codec = ran_a_mgcp_codec_from_sc(&sc);
680 }
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) {
694 struct gsm0808_speech_codec sc;
695 if (gsm0808_dec_speech_codec(&sc, ie_speech_codec->val, ie_speech_codec->len) < 0)
696 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "unable to decode IE Speech Codec (Chosen)\n");
697 else {
698 /* the codec may be extrapolated from above Speech Version or from this Speech Codec */
699 ran_dec_msg.handover_request_ack.codec_present = true;
700 ran_dec_msg.handover_request_ack.codec = ran_a_mgcp_codec_from_sc(&sc);
701 }
702 }
703
704 return ran_decoded(ran_dec, &ran_dec_msg);
705}
706
707static int ran_a_decode_handover_detect(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
708{
709 struct ran_msg ran_dec_msg = {
710 .msg_type = RAN_MSG_HANDOVER_DETECT,
711 .msg_name = "BSSMAP Handover Detect",
712 };
713
714 return ran_decoded(ran_dec, &ran_dec_msg);
715}
716
717static int ran_a_decode_handover_succeeded(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
718{
719 struct ran_msg ran_dec_msg = {
720 .msg_type = RAN_MSG_HANDOVER_SUCCEEDED,
721 .msg_name = "BSSMAP Handover Succeeded",
722 };
723
724 return ran_decoded(ran_dec, &ran_dec_msg);
725}
726
727static int ran_a_decode_handover_complete(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
728{
729 struct ran_msg ran_dec_msg = {
730 .msg_type = RAN_MSG_HANDOVER_COMPLETE,
731 .msg_name = "BSSMAP Handover Complete",
732 };
733
734 return ran_decoded(ran_dec, &ran_dec_msg);
735}
736
737static int ran_a_decode_handover_failure(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
738{
739 struct ran_msg ran_dec_msg = {
740 .msg_type = RAN_MSG_HANDOVER_FAILURE,
741 .msg_name = "BSSMAP Handover Failure",
742 };
743
744 return ran_decoded(ran_dec, &ran_dec_msg);
745}
746
747static int ran_a_decode_bssmap(struct ran_dec *ran_dec, struct msgb *bssmap)
748{
749 struct tlv_parsed tp[2];
750 int rc;
751 struct bssmap_header *h = msgb_l2(bssmap);
752 uint8_t msg_type;
753 bssmap->l3h = bssmap->l2h + sizeof(*h);
754
755 if (msgb_l3len(bssmap) < 1) {
756 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "No data received, discarding message\n");
757 return -1;
758 }
759
760 if (msgb_l3len(bssmap) < h->length) {
Neels Hofmeyrf0923012019-08-22 17:19:49 +0200761 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "BSSMAP data truncated, discarding message:"
762 " msgb_l3len(bssmap) == %u < bssmap_header->length == %u\n",
763 msgb_l3len(bssmap), h->length);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100764 return -1;
765 }
766
767 if (msgb_l3len(bssmap) > h->length) {
Neels Hofmeyrf0923012019-08-22 17:19:49 +0200768 LOG_RAN_A_DEC(ran_dec, LOGL_NOTICE, "There are %u extra bytes after the BSSMAP data, truncating:"
769 " msgb_l3len(bssmap) == %u > bssmap_header->length == %u\n",
770 msgb_l3len(bssmap) - h->length,
771 msgb_l3len(bssmap), h->length);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100772 msgb_l3trim(bssmap, h->length);
773 }
774
775 /* h->type == BSSAP_MSG_BSS_MANAGEMENT; h->length is the data length,
776 * which starts with the MAP msg_type, followed by IEs. */
777 msg_type = bssmap->l3h[0];
778 rc = osmo_bssap_tlv_parse2(tp, ARRAY_SIZE(tp), bssmap->l3h + 1, h->length - 1);
779 if (rc < 0) {
780 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "Failed parsing TLV, discarding message\n");
781 return -EINVAL;
782 }
783
Neels Hofmeyr72fc7062019-10-08 06:24:17 +0200784 LOG_RAN_A_DEC(ran_dec, LOGL_DEBUG, "%s\n", gsm0808_bssmap_name(msg_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100785
786 switch (msg_type) {
787 case BSS_MAP_MSG_COMPLETE_LAYER_3:
788 return ran_a_decode_l3_compl(ran_dec, bssmap, tp);
789 case BSS_MAP_MSG_CLEAR_RQST:
790 return ran_a_decode_clear_request(ran_dec, bssmap, tp);
791 case BSS_MAP_MSG_CLEAR_COMPLETE:
792 return ran_a_decode_clear_complete(ran_dec, bssmap, tp);
793 case BSS_MAP_MSG_CLASSMARK_UPDATE:
794 return ran_a_decode_classmark_update(ran_dec, bssmap, tp);
795 case BSS_MAP_MSG_CIPHER_MODE_COMPLETE:
796 return ran_a_decode_cipher_mode_complete(ran_dec, bssmap, tp);
797 case BSS_MAP_MSG_CIPHER_MODE_REJECT:
798 return ran_a_decode_cipher_mode_reject(ran_dec, bssmap, tp);
799 case BSS_MAP_MSG_ASSIGMENT_COMPLETE:
800 rc = ran_a_decode_assignment_complete(ran_dec, bssmap, tp);
801 if (rc < 0) {
802 struct ran_msg ran_dec_msg = {
803 .msg_type = RAN_MSG_ASSIGNMENT_FAILURE,
804 .msg_name = "BSSMAP Assignment Complete but failed to decode",
805 .clear_request = {
806 .bssap_cause = GSM0808_CAUSE_EQUIPMENT_FAILURE,
807 },
808 };
809 ran_decoded(ran_dec, &ran_dec_msg);
810 }
811 return rc;
812 case BSS_MAP_MSG_ASSIGMENT_FAILURE:
813 return ran_a_decode_assignment_failure(ran_dec, bssmap, tp);
814 case BSS_MAP_MSG_SAPI_N_REJECT:
815 return ran_a_decode_sapi_n_reject(ran_dec, bssmap, tp);
816 case BSS_MAP_MSG_LCLS_NOTIFICATION:
817 return ran_a_decode_lcls_notification(ran_dec, bssmap, tp);
818
819 /* From current RAN peer, the Handover origin: */
820 case BSS_MAP_MSG_HANDOVER_REQUIRED:
821 return ran_a_decode_handover_required(ran_dec, bssmap, tp);
822
823 /* From current MSC to remote handover target MSC */
824 case BSS_MAP_MSG_HANDOVER_RQST:
825 return ran_a_decode_handover_request(ran_dec, bssmap, tp);
826
827 /* From potential new RAN peer, the Handover target: */
828 case BSS_MAP_MSG_HANDOVER_RQST_ACKNOWLEDGE:
829 return ran_a_decode_handover_request_ack(ran_dec, bssmap, tp);
830 case BSS_MAP_MSG_HANDOVER_DETECT:
831 return ran_a_decode_handover_detect(ran_dec, bssmap, tp);
832 case BSS_MAP_MSG_HANDOVER_SUCCEEDED:
833 return ran_a_decode_handover_succeeded(ran_dec, bssmap, tp);
834 case BSS_MAP_MSG_HANDOVER_COMPLETE:
835 return ran_a_decode_handover_complete(ran_dec, bssmap, tp);
836
837 /* From any Handover peer: */
838 case BSS_MAP_MSG_HANDOVER_FAILURE:
839 return ran_a_decode_handover_failure(ran_dec, bssmap, tp);
840
841 default:
842 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "Unimplemented msg type: %s\n", gsm0808_bssmap_name(msg_type));
843 return -EINVAL;
844 }
845
846 return -EINVAL;
847}
848
849static int ran_a_decode_l3(struct ran_dec *ran_dec, struct msgb *l3)
850{
851 struct dtap_header *dtap = msgb_l2(l3);
852 struct ran_msg ran_dec_msg = {
853 .msg_type = RAN_MSG_DTAP,
854 .msg_name = "BSSAP DTAP",
855 .dtap = l3,
856 };
857 l3->l3h = l3->l2h + sizeof(struct dtap_header);
858 OMSC_LINKID_CB(l3) = dtap->link_id;
859 return ran_decoded(ran_dec, &ran_dec_msg);
860}
861
862int ran_a_decode_l2(struct ran_dec *ran_dec, struct msgb *bssap)
863{
864 uint8_t bssap_type;
865 OSMO_ASSERT(bssap);
866
867 if (!msgb_l2(bssap) || !msgb_l2len(bssap)) {
868 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "Cannot decode L2, msg->l2h is unset / empty: %s\n",
869 msgb_hexdump(bssap));
870 return -EINVAL;
871 }
872
873 if (msgb_l2len(bssap) < sizeof(struct bssmap_header)) {
874 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "The header is too short -- discarding message\n");
875 return -EINVAL;
876 }
877
878 bssap_type = bssap->l2h[0];
879 switch (bssap_type) {
880 case BSSAP_MSG_BSS_MANAGEMENT:
881 return ran_a_decode_bssmap(ran_dec, bssap);
882 case BSSAP_MSG_DTAP:
883 return ran_a_decode_l3(ran_dec, bssap);
884 default:
885 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "Unimplemented BSSAP msg type: %s\n", gsm0808_bssap_name(bssap_type));
886 return -EINVAL;
887 }
888}
889
890static struct msgb *ran_a_wrap_dtap(struct msgb *dtap)
891{
892 struct msgb *an_apdu;
893 dtap->l3h = dtap->data;
894 an_apdu = gsm0808_create_dtap(dtap, OMSC_LINKID_CB(dtap));
895 an_apdu->l2h = an_apdu->data;
896 msgb_free(dtap);
897 return an_apdu;
898}
899
900static int ran_a_channel_type_to_speech_codec_list(struct gsm0808_speech_codec_list *scl, const struct gsm0808_channel_type *ct)
901{
902 unsigned int i;
903 int rc;
904
905 memset(scl, 0, sizeof(*scl));
906 for (i = 0; i < ct->perm_spch_len; i++) {
907 rc = gsm0808_speech_codec_from_chan_type(&scl->codec[i], ct->perm_spch[i]);
908 if (rc != 0)
909 return -EINVAL;
910 }
911 scl->len = i;
912
913 return 0;
914}
915
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200916static void _gsm0808_assignment_extend_osmux(struct msgb *msg, uint8_t cid)
917{
918 OSMO_ASSERT(msg->l3h[1] == msgb_l3len(msg) - 2); /*TL not in len */
919 msgb_tv_put(msg, GSM0808_IE_OSMO_OSMUX_CID, cid);
920 msg->l3h[1] = msgb_l3len(msg) - 2;
921}
922
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100923/* Compose a BSSAP Assignment Command.
924 * Passing an RTP address is optional.
925 * The msub is passed merely for error logging. */
926static struct msgb *ran_a_make_assignment_command(struct osmo_fsm_inst *log_fi,
927 const struct ran_assignment_command *ac)
928{
929 struct gsm0808_speech_codec_list scl;
930 struct gsm0808_speech_codec_list *use_scl = NULL;
931 struct sockaddr_storage rtp_addr;
932 struct sockaddr_storage *use_rtp_addr = NULL;
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200933 struct msgb *msg;
Philipp Maierf34d9452020-06-05 15:49:35 +0200934 const uint32_t *call_id = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100935 int rc;
936
937 if (!ac->channel_type) {
938 LOG_RAN_A_ENC(log_fi, LOGL_ERROR, "Assignment Command: missing Channel Type\n");
939 return NULL;
940 }
941
942 if (ac->channel_type->ch_indctr == GSM0808_CHAN_SPEECH) {
943 rc = ran_a_channel_type_to_speech_codec_list(&scl, ac->channel_type);
944 if (rc < 0) {
945 LOG_RAN_A_ENC(log_fi, LOGL_ERROR, "Assignment Command: Cannot translate Channel Type to Speech Codec List\n");
946 return NULL;
947 }
948 use_scl = &scl;
949
950 /* Package RTP-Address data */
Neels Hofmeyr84ce2062019-10-05 05:15:25 +0200951 if (osmo_sockaddr_str_is_nonzero(ac->cn_rtp)) {
Pau Espin Pedrold35abfa2020-08-31 20:44:50 +0200952 struct sockaddr_in *sin;
953 struct sockaddr_in6 *sin6;
954 int family = osmo_ip_str_type(ac->cn_rtp->ip);
955 switch (family) {
956 case AF_INET:
957 sin = (struct sockaddr_in *)&rtp_addr;
958 sin->sin_family = AF_INET;
959 sin->sin_port = osmo_htons(ac->cn_rtp->port);
960 if (inet_pton(AF_INET, ac->cn_rtp->ip, &sin->sin_addr) != 1) {
961 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
962 "Assignment Command: Invalid RTP-Address %s\n",
963 ac->cn_rtp->ip);
964 return NULL;
965 }
966 if (sin->sin_port == 0) {
967 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
968 "Assignment Command: Invalid RTP-Port\n");
969 return NULL;
970 }
971 break;
972 case AF_INET6:
973 sin6 = (struct sockaddr_in6 *)&rtp_addr;
974 sin6->sin6_family = AF_INET6;
975 sin6->sin6_port = osmo_htons(ac->cn_rtp->port);
976 if (inet_pton(AF_INET6, ac->cn_rtp->ip, &sin6->sin6_addr) != 1) {
977 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
978 "Assignment Command: Invalid RTP-Address %s\n",
979 ac->cn_rtp->ip);
980 return NULL;
981 }
982 if (sin6->sin6_port == 0) {
983 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
984 "Assignment Command: Invalid RTP-Port\n");
985 return NULL;
986 }
987 break;
988 default:
989 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
990 "Assignment Command: Invalid RTP-Address type for %s\n",
991 ac->cn_rtp->ip);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100992 return NULL;
993 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100994 use_rtp_addr = &rtp_addr;
995 }
996 }
997
Philipp Maierf34d9452020-06-05 15:49:35 +0200998 if(ac->call_id_present == true)
999 call_id = &ac->call_id;
1000
1001 msg = gsm0808_create_ass(ac->channel_type, NULL, use_rtp_addr, use_scl, call_id);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001002 if (ac->osmux_present)
1003 _gsm0808_assignment_extend_osmux(msg, ac->osmux_cid);
1004 return msg;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001005}
1006
1007/* For an A5/N number a5_n set dst to the matching GSM0808_ALG_ID_A5_<n>. */
1008static int a5_n_to_gsm0808_chosen_enc_alg(uint8_t *dst, int a5_n)
1009{
1010 switch (a5_n) {
1011 case 0:
1012 *dst = GSM0808_ALG_ID_A5_0;
1013 return 0;
1014 case 1:
1015 *dst = GSM0808_ALG_ID_A5_1;
1016 return 0;
1017 case 2:
1018 *dst = GSM0808_ALG_ID_A5_2;
1019 return 0;
1020 case 3:
1021 *dst = GSM0808_ALG_ID_A5_3;
1022 return 0;
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +02001023 case 4:
1024 *dst = GSM0808_ALG_ID_A5_4;
1025 return 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001026 default:
1027 return -ENOTSUP;
1028 }
1029}
1030
1031static int make_encrypt_info_perm_algo(struct osmo_fsm_inst *fi, struct gsm0808_encrypt_info *ei,
1032 uint8_t a5_encryption_mask, const struct osmo_gsm48_classmark *cm)
1033{
1034 int i;
1035 int j = 0;
1036 for (i = 0; i < 8; i++) {
1037 int supported;
1038
1039 /* A5/n permitted by osmo-msc.cfg? */
1040 if (!(a5_encryption_mask & (1 << i)))
1041 continue;
1042
1043 /* A5/n supported by MS? */
1044 supported = osmo_gsm48_classmark_supports_a5(cm, i);
1045 if (supported != 1)
1046 continue;
1047
1048 if (a5_n_to_gsm0808_chosen_enc_alg(&ei->perm_algo[j], i)) {
1049 LOG_RAN_A_ENC(fi, LOGL_ERROR, "Not supported: A5/%d algorithm\n", i);
1050 return -1;
1051 }
1052 j++;
1053 ei->perm_algo_len = j;
1054 }
1055 return 0;
1056}
1057
1058/* For ran_a_make_cipher_mode_command(), for
1059 * memcpy(ei.key, cm->vec->kc, sizeof(cm->vec->kc));
1060 */
1061osmo_static_assert(sizeof(((struct gsm0808_encrypt_info*)0)->key) >= sizeof(((struct osmo_auth_vector*)0)->kc),
1062 gsm0808_encrypt_info_key_fits_osmo_auth_vec_kc);
1063static struct msgb *ran_a_make_cipher_mode_command(struct osmo_fsm_inst *fi, const struct ran_cipher_mode_command *cm)
1064{
Neels Hofmeyrcdcfc802021-06-09 22:26:11 +02001065 struct gsm0808_cipher_mode_command cmc = {
1066 .cipher_response_mode_present = true,
1067 .cipher_response_mode = 1, /* 1: include IMEISV (3GPP TS 48.008 3.2.2.34) */
1068 };
1069 struct gsm0808_encrypt_info *ei = &cmc.ei;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001070 char buf[16 * 2 + 1];
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001071
Neels Hofmeyrcdcfc802021-06-09 22:26:11 +02001072 if (make_encrypt_info_perm_algo(fi, ei, cm->geran.a5_encryption_mask, cm->classmark))
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001073 return NULL;
1074
Neels Hofmeyrcdcfc802021-06-09 22:26:11 +02001075 if (ei->perm_algo_len == 0) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001076 LOG_RAN_A_ENC(fi, LOGL_ERROR, "cannot start ciphering, no intersection between MSC-configured"
1077 " and MS-supported A5 algorithms. MSC: 0x%02x MS: %s\n",
1078 cm->geran.a5_encryption_mask, osmo_gsm48_classmark_a5_name(cm->classmark));
1079 return NULL;
1080 }
1081
1082 /* In case of UMTS AKA, the Kc for ciphering must be derived from the 3G auth
1083 * tokens. vec->kc was calculated from the GSM algorithm and is not
1084 * necessarily a match for the UMTS AKA tokens. */
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +02001085 if (cm->geran.umts_aka) {
1086 int i;
Neels Hofmeyrcdcfc802021-06-09 22:26:11 +02001087 osmo_auth_c3(ei->key, cm->vec->ck, cm->vec->ik);
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +02001088
1089 for (i = 0; i < ei->perm_algo_len; i++) {
1090 if (ei->perm_algo[i] != GSM0808_ALG_ID_A5_4)
1091 continue;
1092 /* A5/4 is included, so need to generate Kc128 */
1093 osmo_kdf_kc128(cm->vec->ck, cm->vec->ik, cmc.kc128);
1094 cmc.kc128_present = true;
1095 break;
1096 }
1097 } else {
Neels Hofmeyrcdcfc802021-06-09 22:26:11 +02001098 memcpy(ei->key, cm->vec->kc, sizeof(cm->vec->kc));
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +02001099 }
Neels Hofmeyrcdcfc802021-06-09 22:26:11 +02001100 ei->key_len = sizeof(cm->vec->kc);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001101
1102 /* Store chosen GERAN key where the caller asked it to be stored.
1103 * alg_id remains unknown until we receive a Cipher Mode Complete from the BSC */
1104 if (cm->geran.chosen_key) {
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +02001105 *cm->geran.chosen_key = (struct geran_encr){0};
1106
Neels Hofmeyrcdcfc802021-06-09 22:26:11 +02001107 if (ei->key_len > sizeof(cm->geran.chosen_key->key)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001108 LOG_RAN_A_ENC(fi, LOGL_ERROR, "Chosen key is larger than I can store\n");
1109 return NULL;
1110 }
Neels Hofmeyrcdcfc802021-06-09 22:26:11 +02001111 memcpy(cm->geran.chosen_key->key, ei->key, ei->key_len);
1112 cm->geran.chosen_key->key_len = ei->key_len;
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +02001113
1114 if (cmc.kc128_present) {
1115 memcpy(cm->geran.chosen_key->kc128, cmc.kc128, 16);
1116 cm->geran.chosen_key->kc128_present = true;
1117 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001118 }
1119
1120 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 +02001121 ei->perm_algo_len, osmo_hexdump_nospc(ei->perm_algo, ei->perm_algo_len),
1122 osmo_hexdump_buf(buf, sizeof(buf), ei->key, ei->key_len, NULL, false));
1123 return gsm0808_create_cipher2(&cmc);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001124}
1125
1126struct msgb *ran_a_make_handover_request(struct osmo_fsm_inst *log_fi, const struct ran_handover_request *n)
1127{
1128 struct sockaddr_storage ss;
1129 struct gsm0808_handover_request r = {
1130 .cell_identifier_serving = n->cell_id_serving,
1131 .cell_identifier_target = n->cell_id_target,
1132 .cause = n->bssap_cause,
1133 .current_channel_type_1_present = n->current_channel_type_1_present,
1134 .current_channel_type_1 = n->current_channel_type_1,
1135
1136 .speech_version_used = n->speech_version_used,
1137
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001138 .old_bss_to_new_bss_info_raw = n->old_bss_to_new_bss_info_raw,
1139 .old_bss_to_new_bss_info_raw_len = n->old_bss_to_new_bss_info_raw_len,
1140
1141 .imsi = n->imsi,
1142 .codec_list_msc_preferred = n->codec_list_msc_preferred,
Philipp Maier7da956e2020-06-09 14:34:40 +02001143 .call_id_present = n->call_id_present,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001144 .call_id = n->call_id,
1145 .global_call_reference = n->global_call_reference,
1146 .global_call_reference_len = n->global_call_reference_len,
1147 };
1148
1149 if (!n->geran.channel_type) {
1150 LOG_RAN_A_ENC(log_fi, LOGL_ERROR, "Channel Type required for encoding Handover Request in BSSAP\n");
1151 return NULL;
1152 }
1153 r.channel_type = *n->geran.channel_type;
1154
1155 /* Encryption Information */
1156 make_encrypt_info_perm_algo(log_fi, &r.encryption_information, n->geran.a5_encryption_mask, n->classmark);
1157 if (n->geran.chosen_encryption && n->geran.chosen_encryption->key_len) {
Vadim Yanitskiy444771d2019-05-11 04:46:24 +07001158 /* Prevent both source / destination buffer overrun / overflow */
1159 if (n->geran.chosen_encryption->key_len > sizeof(r.encryption_information.key)
1160 || n->geran.chosen_encryption->key_len > sizeof(n->geran.chosen_encryption->key)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001161 LOG_RAN_A_ENC(log_fi, LOGL_ERROR, "Handover Request: invalid chosen encryption key size %u\n",
1162 n->geran.chosen_encryption->key_len);
1163 return NULL;
1164 }
1165 memcpy(r.encryption_information.key,
1166 n->geran.chosen_encryption->key, n->geran.chosen_encryption->key_len);
1167 r.encryption_information.key_len = n->geran.chosen_encryption->key_len;
Vadim Yanitskiybfe8eb72019-05-11 03:52:28 +07001168 r.chosen_encryption_algorithm_serving = n->geran.chosen_encryption->alg_id;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001169 }
1170
1171 if (n->classmark)
1172 r.classmark_information = *n->classmark;
1173
Neels Hofmeyr84ce2062019-10-05 05:15:25 +02001174 if (osmo_sockaddr_str_is_nonzero(n->rtp_ran_local)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001175 if (osmo_sockaddr_str_to_sockaddr(n->rtp_ran_local, &ss)) {
1176 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
1177 "Handover Request: invalid AoIP Transport Layer address/port: "
1178 OSMO_SOCKADDR_STR_FMT "\n", OSMO_SOCKADDR_STR_FMT_ARGS(n->rtp_ran_local));
1179 return NULL;
1180 }
1181 r.aoip_transport_layer = &ss;
1182 }
1183
1184 return gsm0808_create_handover_request(&r);
1185}
1186
1187static struct msgb *ran_a_make_handover_request_ack(struct osmo_fsm_inst *caller_fi, const struct ran_handover_request_ack *r)
1188{
1189 struct sockaddr_storage ss;
1190 struct gsm0808_handover_request_ack params = {
1191 .l3_info = r->rr_ho_command,
1192 .l3_info_len = r->rr_ho_command_len,
1193 .chosen_channel_present = r->chosen_channel_present,
1194 .chosen_channel = r->chosen_channel,
1195 .chosen_encr_alg = r->chosen_encr_alg,
1196 .chosen_speech_version = r->chosen_speech_version,
1197 };
1198
Neels Hofmeyr84ce2062019-10-05 05:15:25 +02001199 if (osmo_sockaddr_str_is_nonzero(&r->remote_rtp)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001200 osmo_sockaddr_str_to_sockaddr(&r->remote_rtp, &ss);
1201 params.aoip_transport_layer = &ss;
1202 }
1203
1204 return gsm0808_create_handover_request_ack2(&params);
1205}
1206
1207struct msgb *ran_a_make_handover_command(struct osmo_fsm_inst *log_fi, const struct ran_handover_command *n)
1208{
1209 struct gsm0808_handover_command c = {
1210 .l3_info = n->rr_ho_command,
1211 .l3_info_len = n->rr_ho_command_len,
1212 };
1213
1214 return gsm0808_create_handover_command(&c);
1215}
1216
1217struct msgb *ran_a_make_handover_failure(struct osmo_fsm_inst *log_fi, const struct ran_msg *msg)
1218{
1219 struct gsm0808_handover_failure params = {
1220 .cause = msg->handover_failure.cause,
1221 };
1222 return gsm0808_create_handover_failure(&params);
1223}
1224
1225static struct msgb *_ran_a_encode(struct osmo_fsm_inst *caller_fi, const struct ran_msg *ran_enc_msg)
1226{
1227
1228 LOG_RAN_A_ENC(caller_fi, LOGL_DEBUG, "%s\n", ran_msg_type_name(ran_enc_msg->msg_type));
1229
1230 switch (ran_enc_msg->msg_type) {
1231
1232 case RAN_MSG_DTAP:
1233 return ran_a_wrap_dtap(ran_enc_msg->dtap);
1234
1235 case RAN_MSG_CLASSMARK_REQUEST:
1236 return gsm0808_create_classmark_request();
1237
1238 case RAN_MSG_CLEAR_COMMAND:
1239 return gsm0808_create_clear_command2(ran_enc_msg->clear_command.gsm0808_cause,
1240 ran_enc_msg->clear_command.csfb_ind);
1241
1242 case RAN_MSG_ASSIGNMENT_COMMAND:
1243 return ran_a_make_assignment_command(caller_fi, &ran_enc_msg->assignment_command);
1244
Harald Welte544a32f2020-06-21 22:15:53 +02001245 case RAN_MSG_COMMON_ID:
Pau Espin Pedrol67106702021-04-27 18:20:15 +02001246 return gsm0808_create_common_id(ran_enc_msg->common_id.imsi, NULL,
1247 ran_enc_msg->common_id.last_eutran_plmn_present ?
1248 &ran_enc_msg->common_id.last_eutran_plmn :
1249 NULL
1250 );
Harald Welte544a32f2020-06-21 22:15:53 +02001251
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001252 case RAN_MSG_CIPHER_MODE_COMMAND:
1253 return ran_a_make_cipher_mode_command(caller_fi, &ran_enc_msg->cipher_mode_command);
1254
1255 case RAN_MSG_HANDOVER_REQUIRED_REJECT:
1256 return gsm0808_create_handover_required_reject(&ran_enc_msg->handover_required_reject);
1257
1258 case RAN_MSG_HANDOVER_REQUEST:
1259 return ran_a_make_handover_request(caller_fi, &ran_enc_msg->handover_request);
1260
1261 case RAN_MSG_HANDOVER_REQUEST_ACK:
1262 return ran_a_make_handover_request_ack(caller_fi, &ran_enc_msg->handover_request_ack);
1263
1264 case RAN_MSG_HANDOVER_COMMAND:
1265 return ran_a_make_handover_command(caller_fi, &ran_enc_msg->handover_command);
1266
1267 case RAN_MSG_HANDOVER_SUCCEEDED:
1268 return gsm0808_create_handover_succeeded();
1269
1270 case RAN_MSG_HANDOVER_FAILURE:
1271 return ran_a_make_handover_failure(caller_fi, ran_enc_msg);
1272
1273 default:
1274 LOG_RAN_A_ENC(caller_fi, LOGL_ERROR, "Unimplemented RAN-encode message type: %s\n",
1275 ran_msg_type_name(ran_enc_msg->msg_type));
1276 return NULL;
1277 }
1278}
1279
1280struct msgb *ran_a_encode(struct osmo_fsm_inst *caller_fi, const struct ran_msg *ran_enc_msg)
1281{
1282 struct msgb *msg = _ran_a_encode(caller_fi, ran_enc_msg);
1283
1284 if (!msg)
1285 return NULL;
1286
1287 msg->l2h = msg->data;
1288
1289 /* some consistency checks to ensure we don't send invalid length */
1290 switch (msg->l2h[0]) {
1291 case BSSAP_MSG_DTAP:
1292 OSMO_ASSERT(msgb_l2len(msg) == msg->l2h[2] + 3);
1293 break;
1294 case BSSAP_MSG_BSS_MANAGEMENT:
1295 OSMO_ASSERT(msgb_l2len(msg) == msg->l2h[1] + 2);
1296 break;
1297 default:
1298 break;
1299 }
1300
1301 return msg;
1302}
1303
Neels Hofmeyrb6972742020-06-26 15:20:51 +02001304static void cl_parse_osmux(struct osmo_fsm_inst *log_fi, struct msgb *msg, int *supports_osmux)
1305{
1306 struct tlv_parsed tp;
1307 int rc;
1308
1309 if (supports_osmux == NULL)
1310 return;
1311
1312 rc = tlv_parse(&tp, gsm0808_att_tlvdef(), msgb_l3(msg) + 1, msgb_l3len(msg) - 1, 0, 0);
1313 if (rc < 0) {
1314 LOGPFSMSL(log_fi, DBSSAP, LOGL_ERROR, "BSSMAP: Failed parsing TLV looking for Osmux support\n");
1315 return;
1316 }
1317
1318 if (TLVP_PRESENT(&tp, GSM0808_IE_OSMO_OSMUX_SUPPORT)) {
1319 *supports_osmux = true;
1320 } else {
1321 *supports_osmux = false;
1322 }
1323}
1324
1325/* Return 1 for a RESET, 2 for a RESET ACK message, 0 otherwise.
1326 * In supports_osmux, return 0 for no information, 1 for support detected, -1 for non-support detected. */
1327enum reset_msg_type bssmap_is_reset_msg(const struct sccp_ran_inst *sri, struct osmo_fsm_inst *log_fi,
1328 struct msgb *l2, int *supports_osmux)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001329{
1330 struct bssmap_header *bs = (struct bssmap_header *)msgb_l2(l2);
1331
Neels Hofmeyrb6972742020-06-26 15:20:51 +02001332 if (supports_osmux != NULL)
1333 *supports_osmux = 0;
1334
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001335 if (!bs
1336 || msgb_l2len(l2) < (sizeof(*bs) + 1)
1337 || bs->type != BSSAP_MSG_BSS_MANAGEMENT)
1338 return SCCP_RAN_MSG_NON_RESET;
1339
Neels Hofmeyrb6972742020-06-26 15:20:51 +02001340 l2->l3h = l2->l2h + sizeof(struct bssmap_header);
1341
1342 switch (l2->l3h[0]) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001343 case BSS_MAP_MSG_RESET:
Neels Hofmeyrb6972742020-06-26 15:20:51 +02001344 cl_parse_osmux(log_fi, l2, supports_osmux);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001345 return SCCP_RAN_MSG_RESET;
1346 case BSS_MAP_MSG_RESET_ACKNOWLEDGE:
Neels Hofmeyrb6972742020-06-26 15:20:51 +02001347 cl_parse_osmux(log_fi, l2, supports_osmux);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001348 return SCCP_RAN_MSG_RESET_ACK;
1349 default:
1350 return SCCP_RAN_MSG_NON_RESET;
1351 }
1352}
1353
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001354/* Patch regular BSSMAP RESET to add extra T to announce Osmux support (osmocom extension) */
1355static void _gsm0808_extend_announce_osmux(struct msgb *msg)
1356{
1357 OSMO_ASSERT(msg->l3h[1] == msgb_l3len(msg) - 2); /*TL not in len */
1358 msgb_put_u8(msg, GSM0808_IE_OSMO_OSMUX_SUPPORT);
1359 msg->l3h[1] = msgb_l3len(msg) - 2;
1360}
1361
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001362struct msgb *bssmap_make_reset_msg(const struct sccp_ran_inst *sri, enum reset_msg_type type)
1363{
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001364 struct gsm_network *net = sri->user_data;
1365 struct msgb *msg;
1366
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001367 switch (type) {
1368 case SCCP_RAN_MSG_RESET:
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001369 msg = gsm0808_create_reset();
1370 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001371 case SCCP_RAN_MSG_RESET_ACK:
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001372 msg = gsm0808_create_reset_ack();
1373 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001374 default:
1375 return NULL;
1376 }
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001377
1378 if (!msg)
1379 return NULL;
1380
1381 if (net->use_osmux != OSMUX_USAGE_OFF)
1382 _gsm0808_extend_announce_osmux(msg);
1383
1384 return msg;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001385}
1386
1387struct msgb *bssmap_make_paging_msg(const struct sccp_ran_inst *sri, const struct gsm0808_cell_id *page_cell_id,
1388 const char *imsi, uint32_t tmsi, enum paging_cause cause)
1389{
1390 struct gsm0808_cell_id_list2 cil;
1391 gsm0808_cell_id_to_list(&cil, page_cell_id);
1392 return gsm0808_create_paging2(imsi, tmsi == GSM_RESERVED_TMSI ? NULL : &tmsi, &cil, NULL);
1393}
1394
1395const char *bssmap_msg_name(const struct sccp_ran_inst *sri, const struct msgb *l2)
1396{
1397 struct bssmap_header *bs;
1398
1399 if (!l2->l2h)
1400 return "?";
1401
1402 bs = (struct bssmap_header *)msgb_l2(l2);
1403 switch (bs->type) {
1404 case BSSAP_MSG_BSS_MANAGEMENT:
1405 return gsm0808_bssmap_name(l2->l2h[0]);
1406 case BSSAP_MSG_DTAP:
1407 return "DTAP";
1408 default:
1409 return "?";
1410 }
1411}