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