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