blob: 6a337044365d97b59c57eab871bc02b65ea55a41 [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) {
Pau Espin Pedrol06327172020-09-04 16:37:14 +0200610 struct sockaddr_storage rtp_addr;
611 if (gsm0808_dec_aoip_trasp_addr(&rtp_addr, ie_aoip_transp_addr->val, ie_aoip_transp_addr->len) < 0)
612 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "unable to decode AoIP transport address\n");
613 else if (osmo_sockaddr_str_from_sockaddr(&rtp_ran_local, &rtp_addr) < 0)
614 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "unable to decode remote RTP IP address\n");
615 else
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100616 r->rtp_ran_local = &rtp_ran_local;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100617 }
618
619 if (ie_codec_list_msc_preferred
620 && gsm0808_dec_speech_codec_list(&scl, ie_codec_list_msc_preferred->val,
621 ie_codec_list_msc_preferred->len) == 0)
622 r->codec_list_msc_preferred = &scl;
623
624 if (ie_call_id && ie_call_id->len == 4) {
625 r->call_id = osmo_load32le(ie_call_id->val);
626 r->call_id_present = true;
627 }
628
629 if (ie_global_call_ref) {
630 r->global_call_reference = ie_global_call_ref->val;
631 r->global_call_reference_len = ie_global_call_ref->len;
632 }
633
634 return ran_decoded(ran_dec, &ran_dec_msg);
635}
636
637static int ran_a_decode_handover_request_ack(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
638{
639 struct ran_msg ran_dec_msg = {
640 .msg_type = RAN_MSG_HANDOVER_REQUEST_ACK,
641 .msg_name = "BSSMAP Handover Request Acknowledge",
642 };
643 const struct tlv_p_entry *ie_l3_info = TLVP_GET(tp, GSM0808_IE_LAYER_3_INFORMATION);
644 const struct tlv_p_entry *ie_aoip_transp_addr = TLVP_GET(tp, GSM0808_IE_AOIP_TRASP_ADDR);
645 const struct tlv_p_entry *ie_speech_codec = TLVP_GET(tp, GSM0808_IE_SPEECH_CODEC);
646 const struct tlv_p_entry *ie_chosen_channel = TLVP_GET(tp, GSM0808_IE_CHOSEN_CHANNEL);
647 const struct tlv_p_entry *ie_chosen_encr_alg = TLVP_GET(tp, GSM0808_IE_CHOSEN_ENCR_ALG);
648 const struct tlv_p_entry *ie_chosen_speech_version = TLVP_GET(tp, GSM0808_IE_SPEECH_VERSION);
649
650 /* On missing mandatory IEs, dispatch an invalid RAN_MSG_HANDOVER_REQUEST_ACK so msc_a can act on the failure. */
651
652 if (ie_l3_info) {
653 ran_dec_msg.handover_request_ack.rr_ho_command = ie_l3_info->val;
654 ran_dec_msg.handover_request_ack.rr_ho_command_len = ie_l3_info->len;
655 }
656
657 if (ie_chosen_channel) {
658 ran_dec_msg.handover_request_ack.chosen_channel_present = true;
659 ran_dec_msg.handover_request_ack.chosen_channel = *ie_chosen_channel->val;
660 }
661
662 if (ie_chosen_encr_alg) {
663 ran_dec_msg.handover_request_ack.chosen_encr_alg = *ie_chosen_encr_alg->val;
664 if (ran_dec_msg.handover_request_ack.chosen_encr_alg < 1
665 || ran_dec_msg.handover_request_ack.chosen_encr_alg > 8) {
666 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "invalid Chosen Encryption Algorithm: %u\n",
667 ran_dec_msg.handover_request_ack.chosen_encr_alg);
668 }
669 }
670
671 if (ie_chosen_speech_version) {
672 struct gsm0808_speech_codec sc;
673 ran_dec_msg.handover_request_ack.chosen_speech_version = ie_chosen_speech_version->val[0];
674
675 /* the codec may be extrapolated from this Speech Version or below from Speech Codec */
676 gsm0808_speech_codec_from_chan_type(&sc, ran_dec_msg.handover_request_ack.chosen_speech_version);
677 ran_dec_msg.handover_request_ack.codec_present = true;
678 ran_dec_msg.handover_request_ack.codec = ran_a_mgcp_codec_from_sc(&sc);
679 }
680
681 if (ie_aoip_transp_addr) {
Pau Espin Pedrol06327172020-09-04 16:37:14 +0200682 struct sockaddr_storage rtp_addr;
683 if (gsm0808_dec_aoip_trasp_addr(&rtp_addr, ie_aoip_transp_addr->val, ie_aoip_transp_addr->len) < 0) {
684 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "unable to decode AoIP transport address\n");
685 } else if (osmo_sockaddr_str_from_sockaddr(&ran_dec_msg.handover_request_ack.remote_rtp,
686 &rtp_addr)) {
687 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "unable to decode remote RTP IP address\n");
688 ran_dec_msg.handover_request_ack.remote_rtp = (struct osmo_sockaddr_str){};
689 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100690 }
691
692 if (ie_speech_codec) {
693 struct gsm0808_speech_codec sc;
694 if (gsm0808_dec_speech_codec(&sc, ie_speech_codec->val, ie_speech_codec->len) < 0)
695 LOG_RAN_A_DEC_MSG(LOGL_ERROR, "unable to decode IE Speech Codec (Chosen)\n");
696 else {
697 /* the codec may be extrapolated from above Speech Version or from this Speech Codec */
698 ran_dec_msg.handover_request_ack.codec_present = true;
699 ran_dec_msg.handover_request_ack.codec = ran_a_mgcp_codec_from_sc(&sc);
700 }
701 }
702
703 return ran_decoded(ran_dec, &ran_dec_msg);
704}
705
706static int ran_a_decode_handover_detect(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
707{
708 struct ran_msg ran_dec_msg = {
709 .msg_type = RAN_MSG_HANDOVER_DETECT,
710 .msg_name = "BSSMAP Handover Detect",
711 };
712
713 return ran_decoded(ran_dec, &ran_dec_msg);
714}
715
716static int ran_a_decode_handover_succeeded(struct ran_dec *ran_dec, const struct msgb *msg, const struct tlv_parsed *tp)
717{
718 struct ran_msg ran_dec_msg = {
719 .msg_type = RAN_MSG_HANDOVER_SUCCEEDED,
720 .msg_name = "BSSMAP Handover Succeeded",
721 };
722
723 return ran_decoded(ran_dec, &ran_dec_msg);
724}
725
726static int ran_a_decode_handover_complete(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_COMPLETE,
730 .msg_name = "BSSMAP Handover Complete",
731 };
732
733 return ran_decoded(ran_dec, &ran_dec_msg);
734}
735
736static int ran_a_decode_handover_failure(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_FAILURE,
740 .msg_name = "BSSMAP Handover Failure",
741 };
742
743 return ran_decoded(ran_dec, &ran_dec_msg);
744}
745
746static int ran_a_decode_bssmap(struct ran_dec *ran_dec, struct msgb *bssmap)
747{
748 struct tlv_parsed tp[2];
749 int rc;
750 struct bssmap_header *h = msgb_l2(bssmap);
751 uint8_t msg_type;
752 bssmap->l3h = bssmap->l2h + sizeof(*h);
753
754 if (msgb_l3len(bssmap) < 1) {
755 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "No data received, discarding message\n");
756 return -1;
757 }
758
759 if (msgb_l3len(bssmap) < h->length) {
Neels Hofmeyrf0923012019-08-22 17:19:49 +0200760 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "BSSMAP data truncated, discarding message:"
761 " msgb_l3len(bssmap) == %u < bssmap_header->length == %u\n",
762 msgb_l3len(bssmap), h->length);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100763 return -1;
764 }
765
766 if (msgb_l3len(bssmap) > h->length) {
Neels Hofmeyrf0923012019-08-22 17:19:49 +0200767 LOG_RAN_A_DEC(ran_dec, LOGL_NOTICE, "There are %u extra bytes after the BSSMAP data, truncating:"
768 " msgb_l3len(bssmap) == %u > bssmap_header->length == %u\n",
769 msgb_l3len(bssmap) - h->length,
770 msgb_l3len(bssmap), h->length);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100771 msgb_l3trim(bssmap, h->length);
772 }
773
774 /* h->type == BSSAP_MSG_BSS_MANAGEMENT; h->length is the data length,
775 * which starts with the MAP msg_type, followed by IEs. */
776 msg_type = bssmap->l3h[0];
777 rc = osmo_bssap_tlv_parse2(tp, ARRAY_SIZE(tp), bssmap->l3h + 1, h->length - 1);
778 if (rc < 0) {
779 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "Failed parsing TLV, discarding message\n");
780 return -EINVAL;
781 }
782
Neels Hofmeyr72fc7062019-10-08 06:24:17 +0200783 LOG_RAN_A_DEC(ran_dec, LOGL_DEBUG, "%s\n", gsm0808_bssmap_name(msg_type));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100784
785 switch (msg_type) {
786 case BSS_MAP_MSG_COMPLETE_LAYER_3:
787 return ran_a_decode_l3_compl(ran_dec, bssmap, tp);
788 case BSS_MAP_MSG_CLEAR_RQST:
789 return ran_a_decode_clear_request(ran_dec, bssmap, tp);
790 case BSS_MAP_MSG_CLEAR_COMPLETE:
791 return ran_a_decode_clear_complete(ran_dec, bssmap, tp);
792 case BSS_MAP_MSG_CLASSMARK_UPDATE:
793 return ran_a_decode_classmark_update(ran_dec, bssmap, tp);
794 case BSS_MAP_MSG_CIPHER_MODE_COMPLETE:
795 return ran_a_decode_cipher_mode_complete(ran_dec, bssmap, tp);
796 case BSS_MAP_MSG_CIPHER_MODE_REJECT:
797 return ran_a_decode_cipher_mode_reject(ran_dec, bssmap, tp);
798 case BSS_MAP_MSG_ASSIGMENT_COMPLETE:
799 rc = ran_a_decode_assignment_complete(ran_dec, bssmap, tp);
800 if (rc < 0) {
801 struct ran_msg ran_dec_msg = {
802 .msg_type = RAN_MSG_ASSIGNMENT_FAILURE,
803 .msg_name = "BSSMAP Assignment Complete but failed to decode",
804 .clear_request = {
805 .bssap_cause = GSM0808_CAUSE_EQUIPMENT_FAILURE,
806 },
807 };
808 ran_decoded(ran_dec, &ran_dec_msg);
809 }
810 return rc;
811 case BSS_MAP_MSG_ASSIGMENT_FAILURE:
812 return ran_a_decode_assignment_failure(ran_dec, bssmap, tp);
813 case BSS_MAP_MSG_SAPI_N_REJECT:
814 return ran_a_decode_sapi_n_reject(ran_dec, bssmap, tp);
815 case BSS_MAP_MSG_LCLS_NOTIFICATION:
816 return ran_a_decode_lcls_notification(ran_dec, bssmap, tp);
817
818 /* From current RAN peer, the Handover origin: */
819 case BSS_MAP_MSG_HANDOVER_REQUIRED:
820 return ran_a_decode_handover_required(ran_dec, bssmap, tp);
821
822 /* From current MSC to remote handover target MSC */
823 case BSS_MAP_MSG_HANDOVER_RQST:
824 return ran_a_decode_handover_request(ran_dec, bssmap, tp);
825
826 /* From potential new RAN peer, the Handover target: */
827 case BSS_MAP_MSG_HANDOVER_RQST_ACKNOWLEDGE:
828 return ran_a_decode_handover_request_ack(ran_dec, bssmap, tp);
829 case BSS_MAP_MSG_HANDOVER_DETECT:
830 return ran_a_decode_handover_detect(ran_dec, bssmap, tp);
831 case BSS_MAP_MSG_HANDOVER_SUCCEEDED:
832 return ran_a_decode_handover_succeeded(ran_dec, bssmap, tp);
833 case BSS_MAP_MSG_HANDOVER_COMPLETE:
834 return ran_a_decode_handover_complete(ran_dec, bssmap, tp);
835
836 /* From any Handover peer: */
837 case BSS_MAP_MSG_HANDOVER_FAILURE:
838 return ran_a_decode_handover_failure(ran_dec, bssmap, tp);
839
840 default:
841 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "Unimplemented msg type: %s\n", gsm0808_bssmap_name(msg_type));
842 return -EINVAL;
843 }
844
845 return -EINVAL;
846}
847
848static int ran_a_decode_l3(struct ran_dec *ran_dec, struct msgb *l3)
849{
850 struct dtap_header *dtap = msgb_l2(l3);
851 struct ran_msg ran_dec_msg = {
852 .msg_type = RAN_MSG_DTAP,
853 .msg_name = "BSSAP DTAP",
854 .dtap = l3,
855 };
856 l3->l3h = l3->l2h + sizeof(struct dtap_header);
857 OMSC_LINKID_CB(l3) = dtap->link_id;
858 return ran_decoded(ran_dec, &ran_dec_msg);
859}
860
861int ran_a_decode_l2(struct ran_dec *ran_dec, struct msgb *bssap)
862{
863 uint8_t bssap_type;
864 OSMO_ASSERT(bssap);
865
866 if (!msgb_l2(bssap) || !msgb_l2len(bssap)) {
867 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "Cannot decode L2, msg->l2h is unset / empty: %s\n",
868 msgb_hexdump(bssap));
869 return -EINVAL;
870 }
871
872 if (msgb_l2len(bssap) < sizeof(struct bssmap_header)) {
873 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "The header is too short -- discarding message\n");
874 return -EINVAL;
875 }
876
877 bssap_type = bssap->l2h[0];
878 switch (bssap_type) {
879 case BSSAP_MSG_BSS_MANAGEMENT:
880 return ran_a_decode_bssmap(ran_dec, bssap);
881 case BSSAP_MSG_DTAP:
882 return ran_a_decode_l3(ran_dec, bssap);
883 default:
884 LOG_RAN_A_DEC(ran_dec, LOGL_ERROR, "Unimplemented BSSAP msg type: %s\n", gsm0808_bssap_name(bssap_type));
885 return -EINVAL;
886 }
887}
888
889static struct msgb *ran_a_wrap_dtap(struct msgb *dtap)
890{
891 struct msgb *an_apdu;
892 dtap->l3h = dtap->data;
893 an_apdu = gsm0808_create_dtap(dtap, OMSC_LINKID_CB(dtap));
894 an_apdu->l2h = an_apdu->data;
895 msgb_free(dtap);
896 return an_apdu;
897}
898
899static int ran_a_channel_type_to_speech_codec_list(struct gsm0808_speech_codec_list *scl, const struct gsm0808_channel_type *ct)
900{
901 unsigned int i;
902 int rc;
903
904 memset(scl, 0, sizeof(*scl));
905 for (i = 0; i < ct->perm_spch_len; i++) {
906 rc = gsm0808_speech_codec_from_chan_type(&scl->codec[i], ct->perm_spch[i]);
907 if (rc != 0)
908 return -EINVAL;
909 }
910 scl->len = i;
911
912 return 0;
913}
914
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200915static void _gsm0808_assignment_extend_osmux(struct msgb *msg, uint8_t cid)
916{
917 OSMO_ASSERT(msg->l3h[1] == msgb_l3len(msg) - 2); /*TL not in len */
918 msgb_tv_put(msg, GSM0808_IE_OSMO_OSMUX_CID, cid);
919 msg->l3h[1] = msgb_l3len(msg) - 2;
920}
921
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100922/* Compose a BSSAP Assignment Command.
923 * Passing an RTP address is optional.
924 * The msub is passed merely for error logging. */
925static struct msgb *ran_a_make_assignment_command(struct osmo_fsm_inst *log_fi,
926 const struct ran_assignment_command *ac)
927{
928 struct gsm0808_speech_codec_list scl;
929 struct gsm0808_speech_codec_list *use_scl = NULL;
930 struct sockaddr_storage rtp_addr;
931 struct sockaddr_storage *use_rtp_addr = NULL;
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200932 struct msgb *msg;
Philipp Maierf34d9452020-06-05 15:49:35 +0200933 const uint32_t *call_id = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100934 int rc;
935
936 if (!ac->channel_type) {
937 LOG_RAN_A_ENC(log_fi, LOGL_ERROR, "Assignment Command: missing Channel Type\n");
938 return NULL;
939 }
940
941 if (ac->channel_type->ch_indctr == GSM0808_CHAN_SPEECH) {
942 rc = ran_a_channel_type_to_speech_codec_list(&scl, ac->channel_type);
943 if (rc < 0) {
944 LOG_RAN_A_ENC(log_fi, LOGL_ERROR, "Assignment Command: Cannot translate Channel Type to Speech Codec List\n");
945 return NULL;
946 }
947 use_scl = &scl;
948
949 /* Package RTP-Address data */
Neels Hofmeyr84ce2062019-10-05 05:15:25 +0200950 if (osmo_sockaddr_str_is_nonzero(ac->cn_rtp)) {
Pau Espin Pedrold35abfa2020-08-31 20:44:50 +0200951 struct sockaddr_in *sin;
952 struct sockaddr_in6 *sin6;
953 int family = osmo_ip_str_type(ac->cn_rtp->ip);
954 switch (family) {
955 case AF_INET:
956 sin = (struct sockaddr_in *)&rtp_addr;
957 sin->sin_family = AF_INET;
958 sin->sin_port = osmo_htons(ac->cn_rtp->port);
959 if (inet_pton(AF_INET, ac->cn_rtp->ip, &sin->sin_addr) != 1) {
960 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
961 "Assignment Command: Invalid RTP-Address %s\n",
962 ac->cn_rtp->ip);
963 return NULL;
964 }
965 if (sin->sin_port == 0) {
966 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
967 "Assignment Command: Invalid RTP-Port\n");
968 return NULL;
969 }
970 break;
971 case AF_INET6:
972 sin6 = (struct sockaddr_in6 *)&rtp_addr;
973 sin6->sin6_family = AF_INET6;
974 sin6->sin6_port = osmo_htons(ac->cn_rtp->port);
975 if (inet_pton(AF_INET6, ac->cn_rtp->ip, &sin6->sin6_addr) != 1) {
976 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
977 "Assignment Command: Invalid RTP-Address %s\n",
978 ac->cn_rtp->ip);
979 return NULL;
980 }
981 if (sin6->sin6_port == 0) {
982 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
983 "Assignment Command: Invalid RTP-Port\n");
984 return NULL;
985 }
986 break;
987 default:
988 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
989 "Assignment Command: Invalid RTP-Address type for %s\n",
990 ac->cn_rtp->ip);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100991 return NULL;
992 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100993 use_rtp_addr = &rtp_addr;
994 }
995 }
996
Philipp Maierf34d9452020-06-05 15:49:35 +0200997 if(ac->call_id_present == true)
998 call_id = &ac->call_id;
999
1000 msg = gsm0808_create_ass(ac->channel_type, NULL, use_rtp_addr, use_scl, call_id);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001001 if (ac->osmux_present)
1002 _gsm0808_assignment_extend_osmux(msg, ac->osmux_cid);
1003 return msg;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001004}
1005
1006/* For an A5/N number a5_n set dst to the matching GSM0808_ALG_ID_A5_<n>. */
1007static int a5_n_to_gsm0808_chosen_enc_alg(uint8_t *dst, int a5_n)
1008{
1009 switch (a5_n) {
1010 case 0:
1011 *dst = GSM0808_ALG_ID_A5_0;
1012 return 0;
1013 case 1:
1014 *dst = GSM0808_ALG_ID_A5_1;
1015 return 0;
1016 case 2:
1017 *dst = GSM0808_ALG_ID_A5_2;
1018 return 0;
1019 case 3:
1020 *dst = GSM0808_ALG_ID_A5_3;
1021 return 0;
1022 default:
1023 return -ENOTSUP;
1024 }
1025}
1026
1027static int make_encrypt_info_perm_algo(struct osmo_fsm_inst *fi, struct gsm0808_encrypt_info *ei,
1028 uint8_t a5_encryption_mask, const struct osmo_gsm48_classmark *cm)
1029{
1030 int i;
1031 int j = 0;
1032 for (i = 0; i < 8; i++) {
1033 int supported;
1034
1035 /* A5/n permitted by osmo-msc.cfg? */
1036 if (!(a5_encryption_mask & (1 << i)))
1037 continue;
1038
1039 /* A5/n supported by MS? */
1040 supported = osmo_gsm48_classmark_supports_a5(cm, i);
1041 if (supported != 1)
1042 continue;
1043
1044 if (a5_n_to_gsm0808_chosen_enc_alg(&ei->perm_algo[j], i)) {
1045 LOG_RAN_A_ENC(fi, LOGL_ERROR, "Not supported: A5/%d algorithm\n", i);
1046 return -1;
1047 }
1048 j++;
1049 ei->perm_algo_len = j;
1050 }
1051 return 0;
1052}
1053
1054/* For ran_a_make_cipher_mode_command(), for
1055 * memcpy(ei.key, cm->vec->kc, sizeof(cm->vec->kc));
1056 */
1057osmo_static_assert(sizeof(((struct gsm0808_encrypt_info*)0)->key) >= sizeof(((struct osmo_auth_vector*)0)->kc),
1058 gsm0808_encrypt_info_key_fits_osmo_auth_vec_kc);
1059static struct msgb *ran_a_make_cipher_mode_command(struct osmo_fsm_inst *fi, const struct ran_cipher_mode_command *cm)
1060{
1061 struct gsm0808_encrypt_info ei = {};
1062 char buf[16 * 2 + 1];
1063 const uint8_t cipher_response_mode = 1;
1064
1065 if (make_encrypt_info_perm_algo(fi, &ei, cm->geran.a5_encryption_mask, cm->classmark))
1066 return NULL;
1067
1068 if (ei.perm_algo_len == 0) {
1069 LOG_RAN_A_ENC(fi, LOGL_ERROR, "cannot start ciphering, no intersection between MSC-configured"
1070 " and MS-supported A5 algorithms. MSC: 0x%02x MS: %s\n",
1071 cm->geran.a5_encryption_mask, osmo_gsm48_classmark_a5_name(cm->classmark));
1072 return NULL;
1073 }
1074
1075 /* In case of UMTS AKA, the Kc for ciphering must be derived from the 3G auth
1076 * tokens. vec->kc was calculated from the GSM algorithm and is not
1077 * necessarily a match for the UMTS AKA tokens. */
1078 if (cm->geran.umts_aka)
1079 osmo_auth_c3(ei.key, cm->vec->ck, cm->vec->ik);
1080 else
1081 memcpy(ei.key, cm->vec->kc, sizeof(cm->vec->kc));
1082 ei.key_len = sizeof(cm->vec->kc);
1083
1084 /* Store chosen GERAN key where the caller asked it to be stored.
1085 * alg_id remains unknown until we receive a Cipher Mode Complete from the BSC */
1086 if (cm->geran.chosen_key) {
1087 if (ei.key_len > sizeof(cm->geran.chosen_key->key)) {
1088 LOG_RAN_A_ENC(fi, LOGL_ERROR, "Chosen key is larger than I can store\n");
1089 return NULL;
1090 }
1091 memcpy(cm->geran.chosen_key->key, ei.key, ei.key_len);
1092 cm->geran.chosen_key->key_len = ei.key_len;
1093 }
1094
1095 LOG_RAN_A_ENC(fi, LOGL_DEBUG, "Tx BSSMAP CIPHER MODE COMMAND to BSC, %u ciphers (%s) key %s\n",
1096 ei.perm_algo_len, osmo_hexdump_nospc(ei.perm_algo, ei.perm_algo_len),
1097 osmo_hexdump_buf(buf, sizeof(buf), ei.key, ei.key_len, NULL, false));
1098 return gsm0808_create_cipher(&ei, cm->geran.retrieve_imeisv ? &cipher_response_mode : NULL);
1099}
1100
1101struct msgb *ran_a_make_handover_request(struct osmo_fsm_inst *log_fi, const struct ran_handover_request *n)
1102{
1103 struct sockaddr_storage ss;
1104 struct gsm0808_handover_request r = {
1105 .cell_identifier_serving = n->cell_id_serving,
1106 .cell_identifier_target = n->cell_id_target,
1107 .cause = n->bssap_cause,
1108 .current_channel_type_1_present = n->current_channel_type_1_present,
1109 .current_channel_type_1 = n->current_channel_type_1,
1110
1111 .speech_version_used = n->speech_version_used,
1112
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001113 .old_bss_to_new_bss_info_raw = n->old_bss_to_new_bss_info_raw,
1114 .old_bss_to_new_bss_info_raw_len = n->old_bss_to_new_bss_info_raw_len,
1115
1116 .imsi = n->imsi,
1117 .codec_list_msc_preferred = n->codec_list_msc_preferred,
Philipp Maier7da956e2020-06-09 14:34:40 +02001118 .call_id_present = n->call_id_present,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001119 .call_id = n->call_id,
1120 .global_call_reference = n->global_call_reference,
1121 .global_call_reference_len = n->global_call_reference_len,
1122 };
1123
1124 if (!n->geran.channel_type) {
1125 LOG_RAN_A_ENC(log_fi, LOGL_ERROR, "Channel Type required for encoding Handover Request in BSSAP\n");
1126 return NULL;
1127 }
1128 r.channel_type = *n->geran.channel_type;
1129
1130 /* Encryption Information */
1131 make_encrypt_info_perm_algo(log_fi, &r.encryption_information, n->geran.a5_encryption_mask, n->classmark);
1132 if (n->geran.chosen_encryption && n->geran.chosen_encryption->key_len) {
Vadim Yanitskiy444771d2019-05-11 04:46:24 +07001133 /* Prevent both source / destination buffer overrun / overflow */
1134 if (n->geran.chosen_encryption->key_len > sizeof(r.encryption_information.key)
1135 || n->geran.chosen_encryption->key_len > sizeof(n->geran.chosen_encryption->key)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001136 LOG_RAN_A_ENC(log_fi, LOGL_ERROR, "Handover Request: invalid chosen encryption key size %u\n",
1137 n->geran.chosen_encryption->key_len);
1138 return NULL;
1139 }
1140 memcpy(r.encryption_information.key,
1141 n->geran.chosen_encryption->key, n->geran.chosen_encryption->key_len);
1142 r.encryption_information.key_len = n->geran.chosen_encryption->key_len;
Vadim Yanitskiybfe8eb72019-05-11 03:52:28 +07001143 r.chosen_encryption_algorithm_serving = n->geran.chosen_encryption->alg_id;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001144 }
1145
1146 if (n->classmark)
1147 r.classmark_information = *n->classmark;
1148
Neels Hofmeyr84ce2062019-10-05 05:15:25 +02001149 if (osmo_sockaddr_str_is_nonzero(n->rtp_ran_local)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001150 if (osmo_sockaddr_str_to_sockaddr(n->rtp_ran_local, &ss)) {
1151 LOG_RAN_A_ENC(log_fi, LOGL_ERROR,
1152 "Handover Request: invalid AoIP Transport Layer address/port: "
1153 OSMO_SOCKADDR_STR_FMT "\n", OSMO_SOCKADDR_STR_FMT_ARGS(n->rtp_ran_local));
1154 return NULL;
1155 }
1156 r.aoip_transport_layer = &ss;
1157 }
1158
1159 return gsm0808_create_handover_request(&r);
1160}
1161
1162static struct msgb *ran_a_make_handover_request_ack(struct osmo_fsm_inst *caller_fi, const struct ran_handover_request_ack *r)
1163{
1164 struct sockaddr_storage ss;
1165 struct gsm0808_handover_request_ack params = {
1166 .l3_info = r->rr_ho_command,
1167 .l3_info_len = r->rr_ho_command_len,
1168 .chosen_channel_present = r->chosen_channel_present,
1169 .chosen_channel = r->chosen_channel,
1170 .chosen_encr_alg = r->chosen_encr_alg,
1171 .chosen_speech_version = r->chosen_speech_version,
1172 };
1173
Neels Hofmeyr84ce2062019-10-05 05:15:25 +02001174 if (osmo_sockaddr_str_is_nonzero(&r->remote_rtp)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001175 osmo_sockaddr_str_to_sockaddr(&r->remote_rtp, &ss);
1176 params.aoip_transport_layer = &ss;
1177 }
1178
1179 return gsm0808_create_handover_request_ack2(&params);
1180}
1181
1182struct msgb *ran_a_make_handover_command(struct osmo_fsm_inst *log_fi, const struct ran_handover_command *n)
1183{
1184 struct gsm0808_handover_command c = {
1185 .l3_info = n->rr_ho_command,
1186 .l3_info_len = n->rr_ho_command_len,
1187 };
1188
1189 return gsm0808_create_handover_command(&c);
1190}
1191
1192struct msgb *ran_a_make_handover_failure(struct osmo_fsm_inst *log_fi, const struct ran_msg *msg)
1193{
1194 struct gsm0808_handover_failure params = {
1195 .cause = msg->handover_failure.cause,
1196 };
1197 return gsm0808_create_handover_failure(&params);
1198}
1199
1200static struct msgb *_ran_a_encode(struct osmo_fsm_inst *caller_fi, const struct ran_msg *ran_enc_msg)
1201{
1202
1203 LOG_RAN_A_ENC(caller_fi, LOGL_DEBUG, "%s\n", ran_msg_type_name(ran_enc_msg->msg_type));
1204
1205 switch (ran_enc_msg->msg_type) {
1206
1207 case RAN_MSG_DTAP:
1208 return ran_a_wrap_dtap(ran_enc_msg->dtap);
1209
1210 case RAN_MSG_CLASSMARK_REQUEST:
1211 return gsm0808_create_classmark_request();
1212
1213 case RAN_MSG_CLEAR_COMMAND:
1214 return gsm0808_create_clear_command2(ran_enc_msg->clear_command.gsm0808_cause,
1215 ran_enc_msg->clear_command.csfb_ind);
1216
1217 case RAN_MSG_ASSIGNMENT_COMMAND:
1218 return ran_a_make_assignment_command(caller_fi, &ran_enc_msg->assignment_command);
1219
Harald Welte544a32f2020-06-21 22:15:53 +02001220 case RAN_MSG_COMMON_ID:
1221 return gsm0808_create_common_id(ran_enc_msg->common_id.imsi, NULL, NULL);
1222
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001223 case RAN_MSG_CIPHER_MODE_COMMAND:
1224 return ran_a_make_cipher_mode_command(caller_fi, &ran_enc_msg->cipher_mode_command);
1225
1226 case RAN_MSG_HANDOVER_REQUIRED_REJECT:
1227 return gsm0808_create_handover_required_reject(&ran_enc_msg->handover_required_reject);
1228
1229 case RAN_MSG_HANDOVER_REQUEST:
1230 return ran_a_make_handover_request(caller_fi, &ran_enc_msg->handover_request);
1231
1232 case RAN_MSG_HANDOVER_REQUEST_ACK:
1233 return ran_a_make_handover_request_ack(caller_fi, &ran_enc_msg->handover_request_ack);
1234
1235 case RAN_MSG_HANDOVER_COMMAND:
1236 return ran_a_make_handover_command(caller_fi, &ran_enc_msg->handover_command);
1237
1238 case RAN_MSG_HANDOVER_SUCCEEDED:
1239 return gsm0808_create_handover_succeeded();
1240
1241 case RAN_MSG_HANDOVER_FAILURE:
1242 return ran_a_make_handover_failure(caller_fi, ran_enc_msg);
1243
1244 default:
1245 LOG_RAN_A_ENC(caller_fi, LOGL_ERROR, "Unimplemented RAN-encode message type: %s\n",
1246 ran_msg_type_name(ran_enc_msg->msg_type));
1247 return NULL;
1248 }
1249}
1250
1251struct msgb *ran_a_encode(struct osmo_fsm_inst *caller_fi, const struct ran_msg *ran_enc_msg)
1252{
1253 struct msgb *msg = _ran_a_encode(caller_fi, ran_enc_msg);
1254
1255 if (!msg)
1256 return NULL;
1257
1258 msg->l2h = msg->data;
1259
1260 /* some consistency checks to ensure we don't send invalid length */
1261 switch (msg->l2h[0]) {
1262 case BSSAP_MSG_DTAP:
1263 OSMO_ASSERT(msgb_l2len(msg) == msg->l2h[2] + 3);
1264 break;
1265 case BSSAP_MSG_BSS_MANAGEMENT:
1266 OSMO_ASSERT(msgb_l2len(msg) == msg->l2h[1] + 2);
1267 break;
1268 default:
1269 break;
1270 }
1271
1272 return msg;
1273}
1274
Neels Hofmeyrb6972742020-06-26 15:20:51 +02001275static void cl_parse_osmux(struct osmo_fsm_inst *log_fi, struct msgb *msg, int *supports_osmux)
1276{
1277 struct tlv_parsed tp;
1278 int rc;
1279
1280 if (supports_osmux == NULL)
1281 return;
1282
1283 rc = tlv_parse(&tp, gsm0808_att_tlvdef(), msgb_l3(msg) + 1, msgb_l3len(msg) - 1, 0, 0);
1284 if (rc < 0) {
1285 LOGPFSMSL(log_fi, DBSSAP, LOGL_ERROR, "BSSMAP: Failed parsing TLV looking for Osmux support\n");
1286 return;
1287 }
1288
1289 if (TLVP_PRESENT(&tp, GSM0808_IE_OSMO_OSMUX_SUPPORT)) {
1290 *supports_osmux = true;
1291 } else {
1292 *supports_osmux = false;
1293 }
1294}
1295
1296/* Return 1 for a RESET, 2 for a RESET ACK message, 0 otherwise.
1297 * In supports_osmux, return 0 for no information, 1 for support detected, -1 for non-support detected. */
1298enum reset_msg_type bssmap_is_reset_msg(const struct sccp_ran_inst *sri, struct osmo_fsm_inst *log_fi,
1299 struct msgb *l2, int *supports_osmux)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001300{
1301 struct bssmap_header *bs = (struct bssmap_header *)msgb_l2(l2);
1302
Neels Hofmeyrb6972742020-06-26 15:20:51 +02001303 if (supports_osmux != NULL)
1304 *supports_osmux = 0;
1305
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001306 if (!bs
1307 || msgb_l2len(l2) < (sizeof(*bs) + 1)
1308 || bs->type != BSSAP_MSG_BSS_MANAGEMENT)
1309 return SCCP_RAN_MSG_NON_RESET;
1310
Neels Hofmeyrb6972742020-06-26 15:20:51 +02001311 l2->l3h = l2->l2h + sizeof(struct bssmap_header);
1312
1313 switch (l2->l3h[0]) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001314 case BSS_MAP_MSG_RESET:
Neels Hofmeyrb6972742020-06-26 15:20:51 +02001315 cl_parse_osmux(log_fi, l2, supports_osmux);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001316 return SCCP_RAN_MSG_RESET;
1317 case BSS_MAP_MSG_RESET_ACKNOWLEDGE:
Neels Hofmeyrb6972742020-06-26 15:20:51 +02001318 cl_parse_osmux(log_fi, l2, supports_osmux);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001319 return SCCP_RAN_MSG_RESET_ACK;
1320 default:
1321 return SCCP_RAN_MSG_NON_RESET;
1322 }
1323}
1324
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001325/* Patch regular BSSMAP RESET to add extra T to announce Osmux support (osmocom extension) */
1326static void _gsm0808_extend_announce_osmux(struct msgb *msg)
1327{
1328 OSMO_ASSERT(msg->l3h[1] == msgb_l3len(msg) - 2); /*TL not in len */
1329 msgb_put_u8(msg, GSM0808_IE_OSMO_OSMUX_SUPPORT);
1330 msg->l3h[1] = msgb_l3len(msg) - 2;
1331}
1332
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001333struct msgb *bssmap_make_reset_msg(const struct sccp_ran_inst *sri, enum reset_msg_type type)
1334{
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001335 struct gsm_network *net = sri->user_data;
1336 struct msgb *msg;
1337
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001338 switch (type) {
1339 case SCCP_RAN_MSG_RESET:
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001340 msg = gsm0808_create_reset();
1341 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001342 case SCCP_RAN_MSG_RESET_ACK:
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001343 msg = gsm0808_create_reset_ack();
1344 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001345 default:
1346 return NULL;
1347 }
Pau Espin Pedrolc9ba7542019-05-07 12:23:49 +02001348
1349 if (!msg)
1350 return NULL;
1351
1352 if (net->use_osmux != OSMUX_USAGE_OFF)
1353 _gsm0808_extend_announce_osmux(msg);
1354
1355 return msg;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001356}
1357
1358struct msgb *bssmap_make_paging_msg(const struct sccp_ran_inst *sri, const struct gsm0808_cell_id *page_cell_id,
1359 const char *imsi, uint32_t tmsi, enum paging_cause cause)
1360{
1361 struct gsm0808_cell_id_list2 cil;
1362 gsm0808_cell_id_to_list(&cil, page_cell_id);
1363 return gsm0808_create_paging2(imsi, tmsi == GSM_RESERVED_TMSI ? NULL : &tmsi, &cil, NULL);
1364}
1365
1366const char *bssmap_msg_name(const struct sccp_ran_inst *sri, const struct msgb *l2)
1367{
1368 struct bssmap_header *bs;
1369
1370 if (!l2->l2h)
1371 return "?";
1372
1373 bs = (struct bssmap_header *)msgb_l2(l2);
1374 switch (bs->type) {
1375 case BSSAP_MSG_BSS_MANAGEMENT:
1376 return gsm0808_bssmap_name(l2->l2h[0]);
1377 case BSSAP_MSG_DTAP:
1378 return "DTAP";
1379 default:
1380 return "?";
1381 }
1382}