blob: 64590a1fe945161baba04deac11888adafebce45 [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>
28
29#include <osmocom/gsm/tlv.h>
30#include <osmocom/gsm/gsm0808.h>
31#include <osmocom/gsm/mncc.h>
32#include <osmocom/gsm/gsm48.h>
33
34#include <osmocom/msc/debug.h>
35#include <osmocom/msc/ran_msg_a.h>
36#include <osmocom/msc/sccp_ran.h>
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +020037#include <osmocom/msc/gsm_data.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010038
39#define LOG_RAN_A_DEC(RAN_DEC, level, fmt, args...) \
40 LOG_RAN_DEC(RAN_DEC, DBSSAP, level, "BSSMAP: " fmt, ## args)
41
42/* Assumes presence of struct ran_dec *ran_dec and ran_dec_msg.msg_name (set) in the local scope. */
43#define LOG_RAN_A_DEC_MSG(level, fmt, args...) \
44 LOG_RAN_DEC(ran_dec, DBSSAP, level, "%s: " fmt, ran_dec_msg.msg_name, ## args)
45
46#define LOG_RAN_A_ENC(FI, level, fmt, args...) \
47 LOG_RAN_ENC(FI, DBSSAP, level, "BSSMAP: " fmt, ## args)
48
49static int ran_a_decode_l3_compl(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
50{
51 struct gsm0808_cell_id_list2 cil;
52 struct gsm0808_cell_id cell_id;
53 struct tlv_p_entry *ie_cell_id = TLVP_GET(tp, GSM0808_IE_CELL_IDENTIFIER);
54 struct tlv_p_entry *ie_l3_info = TLVP_GET(tp, GSM0808_IE_LAYER_3_INFORMATION);
55 struct ran_msg ran_dec_msg = {
56 .msg_type = RAN_MSG_COMPL_L3,
57 .msg_name = "BSSMAP Complete Layer 3",
58 .compl_l3 = {
59 .cell_id = &cell_id,
60 .msg = msg,
61 },
62 };
63 int rc;
64
65 if (!ie_cell_id) {
66 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory CELL IDENTIFIER not present, discarding message\n");
67 return -EINVAL;
68 }
69 if (!ie_l3_info) {
70 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory LAYER 3 INFORMATION not present, discarding message\n");
71 return -EINVAL;
72 }
73
74 /* Parse Cell ID element -- this should yield a cell identifier "list" with 1 element. */
75
76 rc = gsm0808_dec_cell_id_list2(&cil, ie_cell_id->val, ie_cell_id->len);
77 if (rc < 0) {
78 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Decoding CELL IDENTIFIER gave rc=%d\n", rc);
79 return -EINVAL;
80 }
81 if (cil.id_list_len != 1) {
82 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Unable to parse element CELL IDENTIFIER, discarding message\n");
83 return -EINVAL;
84 }
85
86 /* Sanity check the Cell Identity */
87 switch (cil.id_discr) {
88 case CELL_IDENT_WHOLE_GLOBAL:
89 case CELL_IDENT_LAI_AND_LAC:
90 case CELL_IDENT_LAC_AND_CI:
91 case CELL_IDENT_LAC:
92 break;
93
94 case CELL_IDENT_CI:
95 case CELL_IDENT_NO_CELL:
96 case CELL_IDENT_BSS:
97 default:
98 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "CELL IDENTIFIER does not specify a LAC, discarding message: %s\n",
99 gsm0808_cell_id_list_name(&cil));
100 return -EINVAL;
101 }
102
103 cell_id = (struct gsm0808_cell_id){
104 .id_discr = cil.id_discr,
105 .id = cil.id_list[0],
106 };
107
108 /* Parse Layer 3 Information element */
109 msg->l3h = (uint8_t*)ie_l3_info->val;
110 msgb_l3trim(msg, ie_l3_info->len);
111
112 if (msgb_l3len(msg) < sizeof(struct gsm48_hdr)) {
113 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "too short L3 info (%d), discarding message\n", msgb_l3len(msg));
114 return -ENODATA;
115 }
116
117 return ran_decoded(ran_dec, &ran_dec_msg);
118}
119
120static int ran_a_decode_clear_request(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
121{
122 struct tlv_p_entry *ie_cause = TLVP_GET(tp, GSM0808_IE_CAUSE);
123 struct ran_msg ran_dec_msg = {
124 .msg_type = RAN_MSG_CLEAR_REQUEST,
125 .msg_name = "BSSMAP Clear Request",
126 };
127
128 if (!ie_cause) {
129 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Cause code is missing, using GSM0808_CAUSE_EQUIPMENT_FAILURE\n");
130 ran_dec_msg.clear_request.bssap_cause = GSM0808_CAUSE_EQUIPMENT_FAILURE;
131 } else {
132 ran_dec_msg.clear_request.bssap_cause = ie_cause->val[0];
133 }
134
135 return ran_decoded(ran_dec, &ran_dec_msg);
136}
137
138static int ran_a_decode_clear_complete(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
139{
140 struct ran_msg ran_dec_msg = {
141 .msg_type = RAN_MSG_CLEAR_COMPLETE,
142 .msg_name = "BSSMAP Clear Complete",
143 };
144 return ran_decoded(ran_dec, &ran_dec_msg);
145}
146
147static int ran_a_decode_classmark_update(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
148{
149 struct tlv_p_entry *ie_cm2 = TLVP_GET(tp, GSM0808_IE_CLASSMARK_INFORMATION_T2);
150 struct tlv_p_entry *ie_cm3 = TLVP_GET(tp, GSM0808_IE_CLASSMARK_INFORMATION_T3);
151 struct osmo_gsm48_classmark cm = {};
152 struct ran_msg ran_dec_msg = {
153 .msg_type = RAN_MSG_CLASSMARK_UPDATE,
154 .msg_name = "BSSMAP Classmark Update",
155 .classmark_update = {
156 .classmark = &cm,
157 },
158 };
159
160 if (!ie_cm2) {
161 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "mandatory Classmark Information Type 2 not present, discarding message\n");
162 return -EINVAL;
163 }
164
165 cm.classmark2_len = OSMO_MIN(sizeof(cm.classmark2), ie_cm2->len);
166 memcpy(&cm.classmark2, ie_cm2->val, cm.classmark2_len);
167
168 if (ie_cm3) {
169 cm.classmark3_len = OSMO_MIN(sizeof(cm.classmark3), ie_cm3->len);
170 memcpy(&cm.classmark3, ie_cm3->val, cm.classmark3_len);
171 }
172
173 return ran_decoded(ran_dec, &ran_dec_msg);
174}
175
176static int ran_a_decode_cipher_mode_complete(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
177{
178 struct tlv_p_entry *ie_chosen_encr_alg = TLVP_GET(tp, GSM0808_IE_CHOSEN_ENCR_ALG);
179 struct tlv_p_entry *ie_l3_msg = TLVP_GET(tp, GSM0808_IE_LAYER_3_MESSAGE_CONTENTS);
180 int rc;
181 struct ran_msg ran_dec_msg = {
182 .msg_type = RAN_MSG_CIPHER_MODE_COMPLETE,
183 .msg_name = "BSSMAP Ciphering Mode Complete",
184 };
185
186 if (ie_chosen_encr_alg) {
187 uint8_t ie_val = ie_chosen_encr_alg->val[0];
188 /* 3GPP TS 48.008 3.2.2.44 Chosen Encryption Algorithm encodes as 1 = no encryption, 2 = A5/1, 4 = A5/3.
189 * Internally we handle without this weird off-by-one. */
190 if (ie_val < 1 || ie_val > 8)
191 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Unsupported value for 3.2.2.44 Chosen Encryption Algorithm: %u\n",
192 ie_val);
193 else
194 ran_dec_msg.cipher_mode_complete.alg_id = ie_chosen_encr_alg->val[0];
195 }
196
197 rc = ran_decoded(ran_dec, &ran_dec_msg);
198
199 if (ie_l3_msg) {
200 msg->l3h = (uint8_t*)ie_l3_msg->val;
201 msgb_l3trim(msg, ie_l3_msg->len);
202 ran_dec_msg = (struct ran_msg){
203 .msg_type = RAN_MSG_DTAP,
204 .msg_name = "BSSMAP Ciphering Mode Complete (L3 Message Contents)",
205 .dtap = msg,
206 };
207 ran_decoded(ran_dec, &ran_dec_msg);
208 }
209
210 return rc;
211}
212
213static int ran_a_decode_cipher_mode_reject(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
214{
215 int rc;
216 struct ran_msg ran_dec_msg = {
217 .msg_type = RAN_MSG_CIPHER_MODE_REJECT,
218 .msg_name = "BSSMAP Ciphering Mode Reject",
219 };
220
221 rc = gsm0808_get_cipher_reject_cause(tp);
222 if (rc < 0) {
223 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "failed to extract Cause\n");
224 ran_dec_msg.cipher_mode_reject.bssap_cause = GSM0808_CAUSE_EQUIPMENT_FAILURE;
225 } else {
226 ran_dec_msg.cipher_mode_reject.bssap_cause = (enum gsm0808_cause)rc;
227 }
228
229 return ran_decoded(ran_dec, &ran_dec_msg);
230}
231
232enum mgcp_codecs ran_a_mgcp_codec_from_sc(const struct gsm0808_speech_codec *sc)
233{
234 switch (sc->type) {
235 case GSM0808_SCT_FR1:
236 return CODEC_GSM_8000_1;
237 break;
238 case GSM0808_SCT_FR2:
239 return CODEC_GSMEFR_8000_1;
240 break;
241 case GSM0808_SCT_FR3:
242 return CODEC_AMR_8000_1;
243 break;
244 case GSM0808_SCT_FR4:
245 return CODEC_AMRWB_16000_1;
246 break;
247 case GSM0808_SCT_FR5:
248 return CODEC_AMRWB_16000_1;
249 break;
250 case GSM0808_SCT_HR1:
251 return CODEC_GSMHR_8000_1;
252 break;
253 case GSM0808_SCT_HR3:
254 return CODEC_AMR_8000_1;
255 break;
256 case GSM0808_SCT_HR4:
257 return CODEC_AMRWB_16000_1;
258 break;
259 case GSM0808_SCT_HR6:
260 return CODEC_AMRWB_16000_1;
261 break;
262 default:
263 return CODEC_PCMU_8000_1;
264 break;
265 }
266}
267
268static int ran_a_decode_assignment_complete(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
269{
270 struct tlv_p_entry *ie_aoip_transp_addr = TLVP_GET(tp, GSM0808_IE_AOIP_TRASP_ADDR);
271 struct tlv_p_entry *ie_speech_codec = TLVP_GET(tp, GSM0808_IE_SPEECH_CODEC);
272 struct sockaddr_storage rtp_addr;
273 struct sockaddr_in *rtp_addr_in;
274 struct gsm0808_speech_codec sc;
275 int rc;
276 struct ran_msg ran_dec_msg = {
277 .msg_type = RAN_MSG_ASSIGNMENT_COMPLETE,
278 .msg_name = "BSSMAP Assignment Complete",
279 };
280
281 if (ie_aoip_transp_addr) {
282 /* Decode AoIP transport address element */
283 rc = gsm0808_dec_aoip_trasp_addr(&rtp_addr, ie_aoip_transp_addr->val, ie_aoip_transp_addr->len);
284 if (rc < 0) {
285 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Unable to decode AoIP Transport Layer Address\n");
286 return -EINVAL;
287 }
288
289 rtp_addr_in = (struct sockaddr_in*)&rtp_addr;
290
291 if (rtp_addr.ss_family != AF_INET) {
292 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Assignment Complete: IE AoIP Transport Address:"
293 " unsupported addressing scheme (only IPV4 supported)\n");
294 return -EINVAL;
295 }
296
297 if (osmo_sockaddr_str_from_sockaddr_in(&ran_dec_msg.assignment_complete.remote_rtp, rtp_addr_in)) {
298 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Assignment Complete: unable to decode remote RTP IP address\n");
299 return -EINVAL;
300 }
301 }
302
303 if (ie_speech_codec) {
304 /* Decode Speech Codec (Chosen) element */
305 rc = gsm0808_dec_speech_codec(&sc, ie_speech_codec->val, ie_speech_codec->len);
306 if (rc < 0) {
307 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Assignment Complete: unable to decode IE Speech Codec (Chosen)"
308 " (rc=%d).\n", rc);
309 return -EINVAL;
310 }
311 ran_dec_msg.assignment_complete.codec_present = true;
312 ran_dec_msg.assignment_complete.codec = ran_a_mgcp_codec_from_sc(&sc);
313 }
314
315 return ran_decoded(ran_dec, &ran_dec_msg);
316}
317
318static int ran_a_decode_assignment_failure(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
319{
320 struct tlv_p_entry *ie_cause = TLVP_GET(tp, GSM0808_IE_CAUSE);
321 struct tlv_p_entry *ie_rr_cause = TLVP_GET(tp, GSM0808_IE_RR_CAUSE);
322 struct tlv_p_entry *ie_speech_codec_list = TLVP_GET(tp, GSM0808_IE_SPEECH_CODEC_LIST);
323 struct gsm0808_speech_codec_list scl;
324 struct ran_msg ran_dec_msg = {
325 .msg_type = RAN_MSG_ASSIGNMENT_FAILURE,
326 .msg_name = "BSSMAP Assignment Failure",
327 .assignment_failure = {
328 .bssap_cause = GSM0808_CAUSE_EQUIPMENT_FAILURE,
329 .rr_cause = GSM48_RR_CAUSE_ABNORMAL_UNSPEC,
330 },
331 };
332
333 if (ie_cause)
334 ran_dec_msg.assignment_failure.bssap_cause = ie_cause->val[0];
335 if (ie_rr_cause)
336 ran_dec_msg.assignment_failure.rr_cause = ie_rr_cause->val[0];
337
338 if (ie_speech_codec_list
339 && gsm0808_dec_speech_codec_list(&scl, ie_speech_codec_list->val, ie_speech_codec_list->len) == 0)
340 ran_dec_msg.assignment_failure.scl_bss_supported = &scl;
341
342 return ran_decoded(ran_dec, &ran_dec_msg);
343}
344
345static int ran_a_decode_sapi_n_reject(struct ran_dec *ran_dec, struct msgb *msg, struct tlv_parsed *tp)
346{
347 struct tlv_p_entry *ie_cause = TLVP_GET(tp, GSM0808_IE_CAUSE);
348 struct tlv_p_entry *ie_dlci = TLVP_GET(tp, GSM0808_IE_DLCI);
349 struct ran_msg ran_dec_msg = {
350 .msg_type = RAN_MSG_SAPI_N_REJECT,
351 .msg_name = "BSSMAP SAPI-N Reject",
352 };
353
354 /* Note: The MSC code seems not to care about the cause code, but by
355 * the specification it is mandatory, so we check its presence. See
356 * also 3GPP TS 48.008 3.2.1.34 SAPI "n" REJECT */
357 if (!ie_cause) {
358 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "SAPI-N Reject: cause code IE is missing, discarding message\n");
359 return -EINVAL;
360 }
361 ran_dec_msg.sapi_n_reject.bssap_cause = ie_cause->val[0];
362
363 if (!ie_dlci) {
364 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "SAPI-N Reject: DLCI IE is missing, discarding message\n");
365 return -EINVAL;
366 }
367 ran_dec_msg.sapi_n_reject.dlci = ie_dlci->val[0];
368
369 return ran_decoded(ran_dec, &ran_dec_msg);
370}
371
372static int ran_a_decode_lcls_notification(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
373{
374 const struct tlv_p_entry *ie_lcls_bss_status = TLVP_GET(tp, GSM0808_IE_LCLS_BSS_STATUS);
375 const struct tlv_p_entry *ie_lcls_break_req = TLVP_GET(tp, GSM0808_IE_LCLS_BREAK_REQ);
376 struct ran_msg ran_dec_msg;
377
378 /* 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 +0700379 if (ie_lcls_bss_status && !ie_lcls_break_req) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100380 ran_dec_msg = (struct ran_msg){
381 .msg_type = RAN_MSG_LCLS_STATUS,
382 .msg_name = "BSSMAP LCLS Notification (LCLS Status)",
383 .lcls_status = {
384 .status = ie_lcls_bss_status->len ?
385 ie_lcls_bss_status->val[0] : GSM0808_LCLS_STS_NA,
386 },
387 };
388 return ran_decoded(ran_dec, &ran_dec_msg);
Vadim Yanitskiy18e8b392019-05-11 04:22:55 +0700389 } else if (ie_lcls_break_req && !ie_lcls_bss_status) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100390 ran_dec_msg = (struct ran_msg){
391 .msg_type = RAN_MSG_LCLS_BREAK_REQ,
392 .msg_name = "BSSMAP LCLS Notification (LCLS Break Req)",
393 .lcls_break_req = {
394 .todo = 23,
395 },
396 };
397 return ran_decoded(ran_dec, &ran_dec_msg);
398 }
399
Vadim Yanitskiy18e8b392019-05-11 04:22:55 +0700400 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "Ignoring broken LCLS Notification message\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100401 return -EINVAL;
402}
403
404static int ran_a_decode_handover_required(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
405{
406 const struct tlv_p_entry *ie_cause = TLVP_GET(tp, GSM0808_IE_CAUSE);
407 const struct tlv_p_entry *ie_cil = TLVP_GET(tp, GSM0808_IE_CELL_IDENTIFIER_LIST);
408 struct ran_msg ran_dec_msg = {
409 .msg_type = RAN_MSG_HANDOVER_REQUIRED,
410 .msg_name = "BSSMAP Handover Required",
411 };
412 /* On decoding failures, dispatch an invalid RAN_MSG_HANDOVER_REQUIRED so msc_a can pass down a
413 * BSS_MAP_MSG_HANDOVER_REQUIRED_REJECT message. */
414
415 if (ie_cause)
416 ran_dec_msg.handover_required.cause = ie_cause->val[0];
417 else
418 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Cause IE missing\n");
419
420 if (!ie_cil
421 || gsm0808_dec_cell_id_list2(&ran_dec_msg.handover_required.cil, ie_cil->val, ie_cil->len) <= 0) {
422 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "No or invalid Cell Identifier List IE\n");
423 ran_dec_msg.handover_required.cil = (struct gsm0808_cell_id_list2){};
424 }
425
426 return ran_decoded(ran_dec, &ran_dec_msg);
427}
428
429static uint8_t a5_encryption_mask_from_gsm0808_chosen_enc_alg(enum gsm0808_chosen_enc_alg val)
430{
431 return 1 << val;
432}
433
434static int ran_a_decode_handover_request(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
435{
436 struct osmo_gsm48_classmark classmark = {};
437 struct ran_msg ran_dec_msg = {
438 .msg_type = RAN_MSG_HANDOVER_REQUEST,
439 .msg_name = "BSSMAP Handover Request",
440 .handover_request = {
441 .classmark = &classmark,
442 },
443 };
444 struct ran_handover_request *r = &ran_dec_msg.handover_request;
445
446 const struct tlv_p_entry *ie_channel_type = TLVP_GET(tp, GSM0808_IE_CHANNEL_TYPE);
447 const struct tlv_p_entry *ie_encryption_information = TLVP_GET(tp, GSM0808_IE_ENCRYPTION_INFORMATION);
448 const struct tlv_p_entry *ie_classmark1 = TLVP_GET(tp, GSM0808_IE_CLASSMARK_INFORMATION_TYPE_1);
449 const struct tlv_p_entry *ie_classmark2 = TLVP_GET(tp, GSM0808_IE_CLASSMARK_INFORMATION_T2);
450 const struct tlv_p_entry *ie_cell_id_serving = TLVP_GET(&tp[0], GSM0808_IE_CELL_IDENTIFIER);
451 const struct tlv_p_entry *ie_cell_id_target = TLVP_GET(&tp[1], GSM0808_IE_CELL_IDENTIFIER);
452 const struct tlv_p_entry *ie_cause = TLVP_GET(tp, GSM0808_IE_CAUSE);
453 const struct tlv_p_entry *ie_classmark3 = TLVP_GET(tp, GSM0808_IE_CLASSMARK_INFORMATION_T3);
454 const struct tlv_p_entry *ie_current_channel_type_1 = TLVP_GET(tp, GSM0808_IE_CURRENT_CHANNEL_TYPE_1);
455 const struct tlv_p_entry *ie_speech_version_used = TLVP_GET(tp, GSM0808_IE_SPEECH_VERSION);
456 const struct tlv_p_entry *ie_chosen_encr_alg_serving = TLVP_GET(tp, GSM0808_IE_CHOSEN_ENCR_ALG);
457 const struct tlv_p_entry *ie_old_bss_to_new_bss_info = TLVP_GET(tp, GSM0808_IE_OLD_BSS_TO_NEW_BSS_INFORMATION);
458 const struct tlv_p_entry *ie_imsi = TLVP_GET(tp, GSM0808_IE_IMSI);
459 const struct tlv_p_entry *ie_aoip_transp_addr = TLVP_GET(tp, GSM0808_IE_AOIP_TRASP_ADDR);
460 const struct tlv_p_entry *ie_codec_list_msc_preferred = TLVP_GET(tp, GSM0808_IE_SPEECH_CODEC_LIST);
461 const struct tlv_p_entry *ie_call_id = TLVP_GET(tp, GSM0808_IE_CALL_ID);
462 const struct tlv_p_entry *ie_global_call_ref = TLVP_GET(tp, GSM0808_IE_GLOBAL_CALL_REF);
463
464 struct gsm0808_channel_type channel_type;
465 struct gsm0808_encrypt_info encr_info;
466 struct gsm0808_speech_codec_list scl;
467 struct geran_encr geran_encr = {};
468 char imsi[OSMO_IMSI_BUF_SIZE];
469 struct osmo_sockaddr_str rtp_ran_local;
470
471 if (!ie_channel_type) {
472 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: Channel Type\n");
473 return -EINVAL;
474 }
475 if (gsm0808_dec_channel_type(&channel_type, ie_channel_type->val, ie_channel_type->len) <= 0) {
476 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Failed to decode Channel Type IE\n");
477 return -EINVAL;
478 }
479 r->geran.channel_type = &channel_type;
480
481 if (ie_encryption_information) {
482 int i;
483 if (gsm0808_dec_encrypt_info(&encr_info, ie_encryption_information->val, ie_encryption_information->len)
484 <= 0) {
485 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Failed to decode Encryption Informaiton IE\n");
486 return -EINVAL;
487 }
488
489 for (i = 0; i < encr_info.perm_algo_len; i++) {
490 r->geran.a5_encryption_mask |=
491 a5_encryption_mask_from_gsm0808_chosen_enc_alg(encr_info.perm_algo[i]);
492 }
493
494 if (encr_info.key_len > sizeof(geran_encr.key)) {
495 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Failed to decode Encryption Informaiton IE:"
496 " encryption key is too long: %u\n", geran_encr.key_len);
497 return -EINVAL;
498 }
499
500 if (encr_info.key_len) {
501 memcpy(geran_encr.key, encr_info.key, encr_info.key_len);
502 geran_encr.key_len = encr_info.key_len;
503 }
504
505 r->geran.chosen_encryption = &geran_encr;
506 }
507
508 if (!ie_classmark1 && !ie_classmark2) {
509 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: either Classmark Information 1"
510 " or Classmark Information 2 must be included\n");
511 return -EINVAL;
512 }
513
514 if (ie_classmark1) {
515 if (ie_classmark1->len != sizeof(classmark.classmark1)) {
516 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Invalid size for Classmark 1: %u, expected %zu\n",
517 ie_classmark1->len, sizeof(classmark.classmark1));
518 return -EINVAL;
519 }
520 memcpy((uint8_t*)&classmark.classmark1, ie_classmark1->val, ie_classmark1->len);
521 classmark.classmark1_set = true;
522 }
523
524 if (ie_classmark2) {
525 uint8_t len = OSMO_MIN(ie_classmark2->len, sizeof(classmark.classmark2));
526 memcpy((uint8_t*)&classmark.classmark2, ie_classmark2->val, len);
527 classmark.classmark2_len = len;
528 }
529
530 if (!ie_cell_id_serving) {
531 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: Cell Identifier (Serving)\n");
532 return -EINVAL;
533 }
534 if (gsm0808_dec_cell_id(&r->cell_id_serving, ie_cell_id_serving->val,
535 ie_cell_id_serving->len) <= 0) {
536 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Failed to decode Cell Identifier (Serving) IE\n");
537 return -EINVAL;
538 }
539
540 if (!ie_cell_id_target) {
541 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Mandatory IE missing: Cell Identifier (Target)\n");
542 return -EINVAL;
543 }
544 if (gsm0808_dec_cell_id(&r->cell_id_target, ie_cell_id_target->val,
545 ie_cell_id_target->len) <= 0) {
546 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "Failed to decode Cell Identifier (Target) IE\n");
547 return -EINVAL;
548 }
549
550 if (ie_cause)
551 r->bssap_cause = ie_cause->val[0];
552
553 if (ie_classmark3) {
554 uint8_t len = OSMO_MIN(ie_classmark3->len, sizeof(classmark.classmark3));
555 memcpy(classmark.classmark3, ie_classmark3->val, len);
556 classmark.classmark3_len = len;
557 }
558
559 if (ie_current_channel_type_1) {
560 r->current_channel_type_1 = ie_current_channel_type_1->val[0];
561 r->current_channel_type_1_present = true;
562 }
563
564 if (ie_speech_version_used) {
565 r->speech_version_used = ie_speech_version_used->val[0];
566 }
567
568 if (ie_chosen_encr_alg_serving && ie_chosen_encr_alg_serving->len) {
569 geran_encr.alg_id = ie_chosen_encr_alg_serving->val[0];
570 r->geran.chosen_encryption = &geran_encr;
571 }
572
573 if (ie_old_bss_to_new_bss_info) {
574 r->old_bss_to_new_bss_info_raw = ie_old_bss_to_new_bss_info->val;
575 r->old_bss_to_new_bss_info_raw_len = ie_old_bss_to_new_bss_info->len;
576 }
577
578 if (ie_imsi) {
579 gsm48_mi_to_string(imsi, sizeof(imsi), ie_imsi->val, ie_imsi->len);
580 r->imsi = imsi;
581 }
582
583 if (ie_aoip_transp_addr) {
584 do {
585 struct sockaddr_storage rtp_addr;
586 if (gsm0808_dec_aoip_trasp_addr(&rtp_addr, ie_aoip_transp_addr->val, ie_aoip_transp_addr->len) < 0) {
587 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "unable to decode AoIP transport address\n");
588 break;
589 }
590 if (rtp_addr.ss_family != AF_INET) {
591 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "IE AoIP Transport Address:"
592 " unsupported addressing scheme (only IPV4 supported)\n");
593 break;
594 }
595 if (osmo_sockaddr_str_from_sockaddr_in(&rtp_ran_local, (struct sockaddr_in*)&rtp_addr)) {
596 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "unable to decode remote RTP IP address\n");
597 break;
598 }
599 r->rtp_ran_local = &rtp_ran_local;
600 } while(0);
601 }
602
603 if (ie_codec_list_msc_preferred
604 && gsm0808_dec_speech_codec_list(&scl, ie_codec_list_msc_preferred->val,
605 ie_codec_list_msc_preferred->len) == 0)
606 r->codec_list_msc_preferred = &scl;
607
608 if (ie_call_id && ie_call_id->len == 4) {
609 r->call_id = osmo_load32le(ie_call_id->val);
610 r->call_id_present = true;
611 }
612
613 if (ie_global_call_ref) {
614 r->global_call_reference = ie_global_call_ref->val;
615 r->global_call_reference_len = ie_global_call_ref->len;
616 }
617
618 return ran_decoded(ran_dec, &ran_dec_msg);
619}
620
621static int ran_a_decode_handover_request_ack(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
622{
623 struct ran_msg ran_dec_msg = {
624 .msg_type = RAN_MSG_HANDOVER_REQUEST_ACK,
625 .msg_name = "BSSMAP Handover Request Acknowledge",
626 };
627 const struct tlv_p_entry *ie_l3_info = TLVP_GET(tp, GSM0808_IE_LAYER_3_INFORMATION);
628 const struct tlv_p_entry *ie_aoip_transp_addr = TLVP_GET(tp, GSM0808_IE_AOIP_TRASP_ADDR);
629 const struct tlv_p_entry *ie_speech_codec = TLVP_GET(tp, GSM0808_IE_SPEECH_CODEC);
630 const struct tlv_p_entry *ie_chosen_channel = TLVP_GET(tp, GSM0808_IE_CHOSEN_CHANNEL);
631 const struct tlv_p_entry *ie_chosen_encr_alg = TLVP_GET(tp, GSM0808_IE_CHOSEN_ENCR_ALG);
632 const struct tlv_p_entry *ie_chosen_speech_version = TLVP_GET(tp, GSM0808_IE_SPEECH_VERSION);
633
634 /* On missing mandatory IEs, dispatch an invalid RAN_MSG_HANDOVER_REQUEST_ACK so msc_a can act on the failure. */
635
636 if (ie_l3_info) {
637 ran_dec_msg.handover_request_ack.rr_ho_command = ie_l3_info->val;
638 ran_dec_msg.handover_request_ack.rr_ho_command_len = ie_l3_info->len;
639 }
640
641 if (ie_chosen_channel) {
642 ran_dec_msg.handover_request_ack.chosen_channel_present = true;
643 ran_dec_msg.handover_request_ack.chosen_channel = *ie_chosen_channel->val;
644 }
645
646 if (ie_chosen_encr_alg) {
647 ran_dec_msg.handover_request_ack.chosen_encr_alg = *ie_chosen_encr_alg->val;
648 if (ran_dec_msg.handover_request_ack.chosen_encr_alg < 1
649 || ran_dec_msg.handover_request_ack.chosen_encr_alg > 8) {
650 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "invalid Chosen Encryption Algorithm: %u\n",
651 ran_dec_msg.handover_request_ack.chosen_encr_alg);
652 }
653 }
654
655 if (ie_chosen_speech_version) {
656 struct gsm0808_speech_codec sc;
657 ran_dec_msg.handover_request_ack.chosen_speech_version = ie_chosen_speech_version->val[0];
658
659 /* the codec may be extrapolated from this Speech Version or below from Speech Codec */
660 gsm0808_speech_codec_from_chan_type(&sc, ran_dec_msg.handover_request_ack.chosen_speech_version);
661 ran_dec_msg.handover_request_ack.codec_present = true;
662 ran_dec_msg.handover_request_ack.codec = ran_a_mgcp_codec_from_sc(&sc);
663 }
664
665 if (ie_aoip_transp_addr) {
666 do {
667 struct sockaddr_storage rtp_addr;
668 if (gsm0808_dec_aoip_trasp_addr(&rtp_addr, ie_aoip_transp_addr->val, ie_aoip_transp_addr->len) < 0) {
669 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "unable to decode AoIP transport address\n");
670 break;
671 }
672 if (rtp_addr.ss_family != AF_INET) {
673 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "IE AoIP Transport Address:"
674 " unsupported addressing scheme (only IPV4 supported)\n");
675 break;
676 }
677 if (osmo_sockaddr_str_from_sockaddr_in(&ran_dec_msg.handover_request_ack.remote_rtp,
678 (struct sockaddr_in*)&rtp_addr)) {
679 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "unable to decode remote RTP IP address\n");
680 ran_dec_msg.handover_request_ack.remote_rtp = (struct osmo_sockaddr_str){};
681 break;
682 }
683 } while(0);
684 }
685
686 if (ie_speech_codec) {
687 struct gsm0808_speech_codec sc;
688 if (gsm0808_dec_speech_codec(&sc, ie_speech_codec->val, ie_speech_codec->len) < 0)
689 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "unable to decode IE Speech Codec (Chosen)\n");
690 else {
691 /* the codec may be extrapolated from above Speech Version or from this Speech Codec */
692 ran_dec_msg.handover_request_ack.codec_present = true;
693 ran_dec_msg.handover_request_ack.codec = ran_a_mgcp_codec_from_sc(&sc);
694 }
695 }
696
697 return ran_decoded(ran_dec, &ran_dec_msg);
698}
699
700static int ran_a_decode_handover_detect(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
701{
702 struct ran_msg ran_dec_msg = {
703 .msg_type = RAN_MSG_HANDOVER_DETECT,
704 .msg_name = "BSSMAP Handover Detect",
705 };
706
707 return ran_decoded(ran_dec, &ran_dec_msg);
708}
709
710static int ran_a_decode_handover_succeeded(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
711{
712 struct ran_msg ran_dec_msg = {
713 .msg_type = RAN_MSG_HANDOVER_SUCCEEDED,
714 .msg_name = "BSSMAP Handover Succeeded",
715 };
716
717 return ran_decoded(ran_dec, &ran_dec_msg);
718}
719
720static int ran_a_decode_handover_complete(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
721{
722 struct ran_msg ran_dec_msg = {
723 .msg_type = RAN_MSG_HANDOVER_COMPLETE,
724 .msg_name = "BSSMAP Handover Complete",
725 };
726
727 return ran_decoded(ran_dec, &ran_dec_msg);
728}
729
730static int ran_a_decode_handover_failure(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
731{
732 struct ran_msg ran_dec_msg = {
733 .msg_type = RAN_MSG_HANDOVER_FAILURE,
734 .msg_name = "BSSMAP Handover Failure",
735 };
736
737 return ran_decoded(ran_dec, &ran_dec_msg);
738}
739
740static int ran_a_decode_bssmap(struct ran_dec *ran_dec, struct msgb *bssmap)
741{
742 struct tlv_parsed tp[2];
743 int rc;
744 struct bssmap_header *h = msgb_l2(bssmap);
745 uint8_t msg_type;
746 bssmap->l3h = bssmap->l2h + sizeof(*h);
747
748 if (msgb_l3len(bssmap) < 1) {
749 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "No data received, discarding message\n");
750 return -1;
751 }
752
753 if (msgb_l3len(bssmap) < h->length) {
754 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "BSSMAP data truncated, discarding message\n");
755 return -1;
756 }
757
758 if (msgb_l3len(bssmap) > h->length) {
759 LOG_RAN_A_DEC(ran_dec, LOGL_NOTICE, "There are %u extra bytes after the BSSMAP data, truncating\n",
760 msgb_l3len(bssmap) - h->length);
761 msgb_l3trim(bssmap, h->length);
762 }
763
764 /* h->type == BSSAP_MSG_BSS_MANAGEMENT; h->length is the data length,
765 * which starts with the MAP msg_type, followed by IEs. */
766 msg_type = bssmap->l3h[0];
767 rc = osmo_bssap_tlv_parse2(tp, ARRAY_SIZE(tp), bssmap->l3h + 1, h->length - 1);
768 if (rc < 0) {
769 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "Failed parsing TLV, discarding message\n");
770 return -EINVAL;
771 }
772
773 LOG_RAN_A_DEC(ran_dec, LOGL_DEBUG, "Rx BSSMAP DT1 %s\n", gsm0808_bssmap_name(msg_type));
774
775 switch (msg_type) {
776 case BSS_MAP_MSG_COMPLETE_LAYER_3:
777 return ran_a_decode_l3_compl(ran_dec, bssmap, tp);
778 case BSS_MAP_MSG_CLEAR_RQST:
779 return ran_a_decode_clear_request(ran_dec, bssmap, tp);
780 case BSS_MAP_MSG_CLEAR_COMPLETE:
781 return ran_a_decode_clear_complete(ran_dec, bssmap, tp);
782 case BSS_MAP_MSG_CLASSMARK_UPDATE:
783 return ran_a_decode_classmark_update(ran_dec, bssmap, tp);
784 case BSS_MAP_MSG_CIPHER_MODE_COMPLETE:
785 return ran_a_decode_cipher_mode_complete(ran_dec, bssmap, tp);
786 case BSS_MAP_MSG_CIPHER_MODE_REJECT:
787 return ran_a_decode_cipher_mode_reject(ran_dec, bssmap, tp);
788 case BSS_MAP_MSG_ASSIGMENT_COMPLETE:
789 rc = ran_a_decode_assignment_complete(ran_dec, bssmap, tp);
790 if (rc < 0) {
791 struct ran_msg ran_dec_msg = {
792 .msg_type = RAN_MSG_ASSIGNMENT_FAILURE,
793 .msg_name = "BSSMAP Assignment Complete but failed to decode",
794 .clear_request = {
795 .bssap_cause = GSM0808_CAUSE_EQUIPMENT_FAILURE,
796 },
797 };
798 ran_decoded(ran_dec, &ran_dec_msg);
799 }
800 return rc;
801 case BSS_MAP_MSG_ASSIGMENT_FAILURE:
802 return ran_a_decode_assignment_failure(ran_dec, bssmap, tp);
803 case BSS_MAP_MSG_SAPI_N_REJECT:
804 return ran_a_decode_sapi_n_reject(ran_dec, bssmap, tp);
805 case BSS_MAP_MSG_LCLS_NOTIFICATION:
806 return ran_a_decode_lcls_notification(ran_dec, bssmap, tp);
807
808 /* From current RAN peer, the Handover origin: */
809 case BSS_MAP_MSG_HANDOVER_REQUIRED:
810 return ran_a_decode_handover_required(ran_dec, bssmap, tp);
811
812 /* From current MSC to remote handover target MSC */
813 case BSS_MAP_MSG_HANDOVER_RQST:
814 return ran_a_decode_handover_request(ran_dec, bssmap, tp);
815
816 /* From potential new RAN peer, the Handover target: */
817 case BSS_MAP_MSG_HANDOVER_RQST_ACKNOWLEDGE:
818 return ran_a_decode_handover_request_ack(ran_dec, bssmap, tp);
819 case BSS_MAP_MSG_HANDOVER_DETECT:
820 return ran_a_decode_handover_detect(ran_dec, bssmap, tp);
821 case BSS_MAP_MSG_HANDOVER_SUCCEEDED:
822 return ran_a_decode_handover_succeeded(ran_dec, bssmap, tp);
823 case BSS_MAP_MSG_HANDOVER_COMPLETE:
824 return ran_a_decode_handover_complete(ran_dec, bssmap, tp);
825
826 /* From any Handover peer: */
827 case BSS_MAP_MSG_HANDOVER_FAILURE:
828 return ran_a_decode_handover_failure(ran_dec, bssmap, tp);
829
830 default:
831 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "Unimplemented msg type: %s\n", gsm0808_bssmap_name(msg_type));
832 return -EINVAL;
833 }
834
835 return -EINVAL;
836}
837
838static int ran_a_decode_l3(struct ran_dec *ran_dec, struct msgb *l3)
839{
840 struct dtap_header *dtap = msgb_l2(l3);
841 struct ran_msg ran_dec_msg = {
842 .msg_type = RAN_MSG_DTAP,
843 .msg_name = "BSSAP DTAP",
844 .dtap = l3,
845 };
846 l3->l3h = l3->l2h + sizeof(struct dtap_header);
847 OMSC_LINKID_CB(l3) = dtap->link_id;
848 return ran_decoded(ran_dec, &ran_dec_msg);
849}
850
851int ran_a_decode_l2(struct ran_dec *ran_dec, struct msgb *bssap)
852{
853 uint8_t bssap_type;
854 OSMO_ASSERT(bssap);
855
856 if (!msgb_l2(bssap) || !msgb_l2len(bssap)) {
857 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "Cannot decode L2, msg->l2h is unset / empty: %s\n",
858 msgb_hexdump(bssap));
859 return -EINVAL;
860 }
861
862 if (msgb_l2len(bssap) < sizeof(struct bssmap_header)) {
863 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "The header is too short -- discarding message\n");
864 return -EINVAL;
865 }
866
867 bssap_type = bssap->l2h[0];
868 switch (bssap_type) {
869 case BSSAP_MSG_BSS_MANAGEMENT:
870 return ran_a_decode_bssmap(ran_dec, bssap);
871 case BSSAP_MSG_DTAP:
872 return ran_a_decode_l3(ran_dec, bssap);
873 default:
874 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "Unimplemented BSSAP msg type: %s\n", gsm0808_bssap_name(bssap_type));
875 return -EINVAL;
876 }
877}
878
879static struct msgb *ran_a_wrap_dtap(struct msgb *dtap)
880{
881 struct msgb *an_apdu;
882 dtap->l3h = dtap->data;
883 an_apdu = gsm0808_create_dtap(dtap, OMSC_LINKID_CB(dtap));
884 an_apdu->l2h = an_apdu->data;
885 msgb_free(dtap);
886 return an_apdu;
887}
888
889static int ran_a_channel_type_to_speech_codec_list(struct gsm0808_speech_codec_list *scl, const struct gsm0808_channel_type *ct)
890{
891 unsigned int i;
892 int rc;
893
894 memset(scl, 0, sizeof(*scl));
895 for (i = 0; i < ct->perm_spch_len; i++) {
896 rc = gsm0808_speech_codec_from_chan_type(&scl->codec[i], ct->perm_spch[i]);
897 if (rc != 0)
898 return -EINVAL;
899 }
900 scl->len = i;
901
902 return 0;
903}
904
905/* Compose a BSSAP Assignment Command.
906 * Passing an RTP address is optional.
907 * The msub is passed merely for error logging. */
908static struct msgb *ran_a_make_assignment_command(struct osmo_fsm_inst *log_fi,
909 const struct ran_assignment_command *ac)
910{
911 struct gsm0808_speech_codec_list scl;
912 struct gsm0808_speech_codec_list *use_scl = NULL;
913 struct sockaddr_storage rtp_addr;
914 struct sockaddr_storage *use_rtp_addr = NULL;
915 int rc;
916
917 if (!ac->channel_type) {
918 LOG_RAN_A_ENC(log_fi, LOGL_ERROR, "Assignment Command: missing Channel Type\n");
919 return NULL;
920 }
921
922 if (ac->channel_type->ch_indctr == GSM0808_CHAN_SPEECH) {
923 rc = ran_a_channel_type_to_speech_codec_list(&scl, ac->channel_type);
924 if (rc < 0) {
925 LOG_RAN_A_ENC(log_fi, LOGL_ERROR, "Assignment Command: Cannot translate Channel Type to Speech Codec List\n");
926 return NULL;
927 }
928 use_scl = &scl;
929
930 /* Package RTP-Address data */
931 if (osmo_sockaddr_str_is_set(ac->cn_rtp)) {
932 struct sockaddr_in rtp_addr_in;
933
934 memset(&rtp_addr_in, 0, sizeof(rtp_addr_in));
935 rtp_addr_in.sin_family = AF_INET;
936 rtp_addr_in.sin_port = osmo_htons(ac->cn_rtp->port),
937 rtp_addr_in.sin_addr.s_addr = inet_addr(ac->cn_rtp->ip);
938
939 if (rtp_addr_in.sin_addr.s_addr == INADDR_NONE) {
940 LOG_RAN_A_ENC(log_fi, LOGL_ERROR, "Assignment Command: Invalid RTP-Address\n");
941 return NULL;
942 }
943 if (rtp_addr_in.sin_port == 0) {
944 LOG_RAN_A_ENC(log_fi, LOGL_ERROR, "Assignment Command: Invalid RTP-Port\n");
945 return NULL;
946 }
947
948 memset(&rtp_addr, 0, sizeof(rtp_addr));
949 memcpy(&rtp_addr, &rtp_addr_in, sizeof(rtp_addr_in));
950
951 use_rtp_addr = &rtp_addr;
952 }
953 }
954
955 return gsm0808_create_ass(ac->channel_type, NULL, use_rtp_addr, use_scl, NULL);
956}
957
958/* For an A5/N number a5_n set dst to the matching GSM0808_ALG_ID_A5_<n>. */
959static int a5_n_to_gsm0808_chosen_enc_alg(uint8_t *dst, int a5_n)
960{
961 switch (a5_n) {
962 case 0:
963 *dst = GSM0808_ALG_ID_A5_0;
964 return 0;
965 case 1:
966 *dst = GSM0808_ALG_ID_A5_1;
967 return 0;
968 case 2:
969 *dst = GSM0808_ALG_ID_A5_2;
970 return 0;
971 case 3:
972 *dst = GSM0808_ALG_ID_A5_3;
973 return 0;
974 default:
975 return -ENOTSUP;
976 }
977}
978
979static int make_encrypt_info_perm_algo(struct osmo_fsm_inst *fi, struct gsm0808_encrypt_info *ei,
980 uint8_t a5_encryption_mask, const struct osmo_gsm48_classmark *cm)
981{
982 int i;
983 int j = 0;
984 for (i = 0; i < 8; i++) {
985 int supported;
986
987 /* A5/n permitted by osmo-msc.cfg? */
988 if (!(a5_encryption_mask & (1 << i)))
989 continue;
990
991 /* A5/n supported by MS? */
992 supported = osmo_gsm48_classmark_supports_a5(cm, i);
993 if (supported != 1)
994 continue;
995
996 if (a5_n_to_gsm0808_chosen_enc_alg(&ei->perm_algo[j], i)) {
997 LOG_RAN_A_ENC(fi, LOGL_ERROR, "Not supported: A5/%d algorithm\n", i);
998 return -1;
999 }
1000 j++;
1001 ei->perm_algo_len = j;
1002 }
1003 return 0;
1004}
1005
1006/* For ran_a_make_cipher_mode_command(), for
1007 * memcpy(ei.key, cm->vec->kc, sizeof(cm->vec->kc));
1008 */
1009osmo_static_assert(sizeof(((struct gsm0808_encrypt_info*)0)->key) >= sizeof(((struct osmo_auth_vector*)0)->kc),
1010 gsm0808_encrypt_info_key_fits_osmo_auth_vec_kc);
1011static struct msgb *ran_a_make_cipher_mode_command(struct osmo_fsm_inst *fi, const struct ran_cipher_mode_command *cm)
1012{
1013 struct gsm0808_encrypt_info ei = {};
1014 char buf[16 * 2 + 1];
1015 const uint8_t cipher_response_mode = 1;
1016
1017 if (make_encrypt_info_perm_algo(fi, &ei, cm->geran.a5_encryption_mask, cm->classmark))
1018 return NULL;
1019
1020 if (ei.perm_algo_len == 0) {
1021 LOG_RAN_A_ENC(fi, LOGL_ERROR, "cannot start ciphering, no intersection between MSC-configured"
1022 " and MS-supported A5 algorithms. MSC: 0x%02x MS: %s\n",
1023 cm->geran.a5_encryption_mask, osmo_gsm48_classmark_a5_name(cm->classmark));
1024 return NULL;
1025 }
1026
1027 /* In case of UMTS AKA, the Kc for ciphering must be derived from the 3G auth
1028 * tokens. vec->kc was calculated from the GSM algorithm and is not
1029 * necessarily a match for the UMTS AKA tokens. */
1030 if (cm->geran.umts_aka)
1031 osmo_auth_c3(ei.key, cm->vec->ck, cm->vec->ik);
1032 else
1033 memcpy(ei.key, cm->vec->kc, sizeof(cm->vec->kc));
1034 ei.key_len = sizeof(cm->vec->kc);
1035
1036 /* Store chosen GERAN key where the caller asked it to be stored.
1037 * alg_id remains unknown until we receive a Cipher Mode Complete from the BSC */
1038 if (cm->geran.chosen_key) {
1039 if (ei.key_len > sizeof(cm->geran.chosen_key->key)) {
1040 LOG_RAN_A_ENC(fi, LOGL_ERROR, "Chosen key is larger than I can store\n");
1041 return NULL;
1042 }
1043 memcpy(cm->geran.chosen_key->key, ei.key, ei.key_len);
1044 cm->geran.chosen_key->key_len = ei.key_len;
1045 }
1046
1047 LOG_RAN_A_ENC(fi, LOGL_DEBUG, "Tx BSSMAP CIPHER MODE COMMAND to BSC, %u ciphers (%s) key %s\n",
1048 ei.perm_algo_len, osmo_hexdump_nospc(ei.perm_algo, ei.perm_algo_len),
1049 osmo_hexdump_buf(buf, sizeof(buf), ei.key, ei.key_len, NULL, false));
1050 return gsm0808_create_cipher(&ei, cm->geran.retrieve_imeisv ? &cipher_response_mode : NULL);
1051}
1052
1053struct msgb *ran_a_make_handover_request(struct osmo_fsm_inst *log_fi, const struct ran_handover_request *n)
1054{
1055 struct sockaddr_storage ss;
1056 struct gsm0808_handover_request r = {
1057 .cell_identifier_serving = n->cell_id_serving,
1058 .cell_identifier_target = n->cell_id_target,
1059 .cause = n->bssap_cause,
1060 .current_channel_type_1_present = n->current_channel_type_1_present,
1061 .current_channel_type_1 = n->current_channel_type_1,
1062
1063 .speech_version_used = n->speech_version_used,
1064
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001065 .old_bss_to_new_bss_info_raw = n->old_bss_to_new_bss_info_raw,
1066 .old_bss_to_new_bss_info_raw_len = n->old_bss_to_new_bss_info_raw_len,
1067
1068 .imsi = n->imsi,
1069 .codec_list_msc_preferred = n->codec_list_msc_preferred,
1070 .call_id = n->call_id,
1071 .global_call_reference = n->global_call_reference,
1072 .global_call_reference_len = n->global_call_reference_len,
1073 };
1074
1075 if (!n->geran.channel_type) {
1076 LOG_RAN_A_ENC(log_fi, LOGL_ERROR, "Channel Type required for encoding Handover Request in BSSAP\n");
1077 return NULL;
1078 }
1079 r.channel_type = *n->geran.channel_type;
1080
1081 /* Encryption Information */
1082 make_encrypt_info_perm_algo(log_fi, &r.encryption_information, n->geran.a5_encryption_mask, n->classmark);
1083 if (n->geran.chosen_encryption && n->geran.chosen_encryption->key_len) {
Vadim Yanitskiy444771d2019-05-11 04:46:24 +07001084 /* Prevent both source / destination buffer overrun / overflow */
1085 if (n->geran.chosen_encryption->key_len > sizeof(r.encryption_information.key)
1086 || n->geran.chosen_encryption->key_len > sizeof(n->geran.chosen_encryption->key)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001087 LOG_RAN_A_ENC(log_fi, LOGL_ERROR, "Handover Request: invalid chosen encryption key size %u\n",
1088 n->geran.chosen_encryption->key_len);
1089 return NULL;
1090 }
1091 memcpy(r.encryption_information.key,
1092 n->geran.chosen_encryption->key, n->geran.chosen_encryption->key_len);
1093 r.encryption_information.key_len = n->geran.chosen_encryption->key_len;
Vadim Yanitskiybfe8eb72019-05-11 03:52:28 +07001094 r.chosen_encryption_algorithm_serving = n->geran.chosen_encryption->alg_id;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001095 }
1096
1097 if (n->classmark)
1098 r.classmark_information = *n->classmark;
1099
1100 if (osmo_sockaddr_str_is_set(n->rtp_ran_local)) {
1101 if (osmo_sockaddr_str_to_sockaddr(n->rtp_ran_local, &ss)) {
1102 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
1103 "Handover Request: invalid AoIP Transport Layer address/port: "
1104 OSMO_SOCKADDR_STR_FMT "\n", OSMO_SOCKADDR_STR_FMT_ARGS(n->rtp_ran_local));
1105 return NULL;
1106 }
1107 r.aoip_transport_layer = &ss;
1108 }
1109
1110 return gsm0808_create_handover_request(&r);
1111}
1112
1113static struct msgb *ran_a_make_handover_request_ack(struct osmo_fsm_inst *caller_fi, const struct ran_handover_request_ack *r)
1114{
1115 struct sockaddr_storage ss;
1116 struct gsm0808_handover_request_ack params = {
1117 .l3_info = r->rr_ho_command,
1118 .l3_info_len = r->rr_ho_command_len,
1119 .chosen_channel_present = r->chosen_channel_present,
1120 .chosen_channel = r->chosen_channel,
1121 .chosen_encr_alg = r->chosen_encr_alg,
1122 .chosen_speech_version = r->chosen_speech_version,
1123 };
1124
1125 if (osmo_sockaddr_str_is_set(&r->remote_rtp)) {
1126 osmo_sockaddr_str_to_sockaddr(&r->remote_rtp, &ss);
1127 params.aoip_transport_layer = &ss;
1128 }
1129
1130 return gsm0808_create_handover_request_ack2(&params);
1131}
1132
1133struct msgb *ran_a_make_handover_command(struct osmo_fsm_inst *log_fi, const struct ran_handover_command *n)
1134{
1135 struct gsm0808_handover_command c = {
1136 .l3_info = n->rr_ho_command,
1137 .l3_info_len = n->rr_ho_command_len,
1138 };
1139
1140 return gsm0808_create_handover_command(&c);
1141}
1142
1143struct msgb *ran_a_make_handover_failure(struct osmo_fsm_inst *log_fi, const struct ran_msg *msg)
1144{
1145 struct gsm0808_handover_failure params = {
1146 .cause = msg->handover_failure.cause,
1147 };
1148 return gsm0808_create_handover_failure(&params);
1149}
1150
1151static struct msgb *_ran_a_encode(struct osmo_fsm_inst *caller_fi, const struct ran_msg *ran_enc_msg)
1152{
1153
1154 LOG_RAN_A_ENC(caller_fi, LOGL_DEBUG, "%s\n", ran_msg_type_name(ran_enc_msg->msg_type));
1155
1156 switch (ran_enc_msg->msg_type) {
1157
1158 case RAN_MSG_DTAP:
1159 return ran_a_wrap_dtap(ran_enc_msg->dtap);
1160
1161 case RAN_MSG_CLASSMARK_REQUEST:
1162 return gsm0808_create_classmark_request();
1163
1164 case RAN_MSG_CLEAR_COMMAND:
1165 return gsm0808_create_clear_command2(ran_enc_msg->clear_command.gsm0808_cause,
1166 ran_enc_msg->clear_command.csfb_ind);
1167
1168 case RAN_MSG_ASSIGNMENT_COMMAND:
1169 return ran_a_make_assignment_command(caller_fi, &ran_enc_msg->assignment_command);
1170
1171 case RAN_MSG_CIPHER_MODE_COMMAND:
1172 return ran_a_make_cipher_mode_command(caller_fi, &ran_enc_msg->cipher_mode_command);
1173
1174 case RAN_MSG_HANDOVER_REQUIRED_REJECT:
1175 return gsm0808_create_handover_required_reject(&ran_enc_msg->handover_required_reject);
1176
1177 case RAN_MSG_HANDOVER_REQUEST:
1178 return ran_a_make_handover_request(caller_fi, &ran_enc_msg->handover_request);
1179
1180 case RAN_MSG_HANDOVER_REQUEST_ACK:
1181 return ran_a_make_handover_request_ack(caller_fi, &ran_enc_msg->handover_request_ack);
1182
1183 case RAN_MSG_HANDOVER_COMMAND:
1184 return ran_a_make_handover_command(caller_fi, &ran_enc_msg->handover_command);
1185
1186 case RAN_MSG_HANDOVER_SUCCEEDED:
1187 return gsm0808_create_handover_succeeded();
1188
1189 case RAN_MSG_HANDOVER_FAILURE:
1190 return ran_a_make_handover_failure(caller_fi, ran_enc_msg);
1191
1192 default:
1193 LOG_RAN_A_ENC(caller_fi, LOGL_ERROR, "Unimplemented RAN-encode message type: %s\n",
1194 ran_msg_type_name(ran_enc_msg->msg_type));
1195 return NULL;
1196 }
1197}
1198
1199struct msgb *ran_a_encode(struct osmo_fsm_inst *caller_fi, const struct ran_msg *ran_enc_msg)
1200{
1201 struct msgb *msg = _ran_a_encode(caller_fi, ran_enc_msg);
1202
1203 if (!msg)
1204 return NULL;
1205
1206 msg->l2h = msg->data;
1207
1208 /* some consistency checks to ensure we don't send invalid length */
1209 switch (msg->l2h[0]) {
1210 case BSSAP_MSG_DTAP:
1211 OSMO_ASSERT(msgb_l2len(msg) == msg->l2h[2] + 3);
1212 break;
1213 case BSSAP_MSG_BSS_MANAGEMENT:
1214 OSMO_ASSERT(msgb_l2len(msg) == msg->l2h[1] + 2);
1215 break;
1216 default:
1217 break;
1218 }
1219
1220 return msg;
1221}
1222
1223/* Return 1 for a RESET, 2 for a RESET ACK message, 0 otherwise */
1224enum reset_msg_type bssmap_is_reset_msg(const struct sccp_ran_inst *sri, const struct msgb *l2)
1225{
1226 struct bssmap_header *bs = (struct bssmap_header *)msgb_l2(l2);
1227
1228 if (!bs
1229 || msgb_l2len(l2) < (sizeof(*bs) + 1)
1230 || bs->type != BSSAP_MSG_BSS_MANAGEMENT)
1231 return SCCP_RAN_MSG_NON_RESET;
1232
1233 switch (l2->l2h[sizeof(*bs)]) {
1234 case BSS_MAP_MSG_RESET:
1235 return SCCP_RAN_MSG_RESET;
1236 case BSS_MAP_MSG_RESET_ACKNOWLEDGE:
1237 return SCCP_RAN_MSG_RESET_ACK;
1238 default:
1239 return SCCP_RAN_MSG_NON_RESET;
1240 }
1241}
1242
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001243/* Patch regular BSSMAP RESET to add extra T to announce Osmux support (osmocom extension) */
1244static void _gsm0808_extend_announce_osmux(struct msgb *msg)
1245{
1246 OSMO_ASSERT(msg->l3h[1] == msgb_l3len(msg) - 2); /*TL not in len */
1247 msgb_put_u8(msg, GSM0808_IE_OSMO_OSMUX_SUPPORT);
1248 msg->l3h[1] = msgb_l3len(msg) - 2;
1249}
1250
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001251struct msgb *bssmap_make_reset_msg(const struct sccp_ran_inst *sri, enum reset_msg_type type)
1252{
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001253 struct gsm_network *net = sri->user_data;
1254 struct msgb *msg;
1255
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001256 switch (type) {
1257 case SCCP_RAN_MSG_RESET:
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001258 msg = gsm0808_create_reset();
1259 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001260 case SCCP_RAN_MSG_RESET_ACK:
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001261 msg = gsm0808_create_reset_ack();
1262 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001263 default:
1264 return NULL;
1265 }
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001266
1267 if (!msg)
1268 return NULL;
1269
1270 if (net->use_osmux != OSMUX_USAGE_OFF)
1271 _gsm0808_extend_announce_osmux(msg);
1272
1273 return msg;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001274}
1275
1276struct msgb *bssmap_make_paging_msg(const struct sccp_ran_inst *sri, const struct gsm0808_cell_id *page_cell_id,
1277 const char *imsi, uint32_t tmsi, enum paging_cause cause)
1278{
1279 struct gsm0808_cell_id_list2 cil;
1280 gsm0808_cell_id_to_list(&cil, page_cell_id);
1281 return gsm0808_create_paging2(imsi, tmsi == GSM_RESERVED_TMSI ? NULL : &tmsi, &cil, NULL);
1282}
1283
1284const char *bssmap_msg_name(const struct sccp_ran_inst *sri, const struct msgb *l2)
1285{
1286 struct bssmap_header *bs;
1287
1288 if (!l2->l2h)
1289 return "?";
1290
1291 bs = (struct bssmap_header *)msgb_l2(l2);
1292 switch (bs->type) {
1293 case BSSAP_MSG_BSS_MANAGEMENT:
1294 return gsm0808_bssmap_name(l2->l2h[0]);
1295 case BSSAP_MSG_DTAP:
1296 return "DTAP";
1297 default:
1298 return "?";
1299 }
1300}