Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 1 | /* API to forward upcoming NAS events, e.g. from BSSAP and RANAP, to be handled by MSC-A or MSC-I. */ |
| 2 | /* |
| 3 | * (C) 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 4 | * |
| 5 | * Author: Neels Hofmeyr <neels@hofmeyr.de> |
| 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU Affero General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | */ |
| 22 | #pragma once |
| 23 | |
| 24 | #include <osmocom/core/utils.h> |
| 25 | #include <osmocom/core/fsm.h> |
| 26 | #include <osmocom/core/sockaddr_str.h> |
| 27 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
| 28 | #include <osmocom/mgcp_client/mgcp_client.h> |
| 29 | |
| 30 | #include <osmocom/msc/msc_common.h> |
| 31 | |
| 32 | struct msgb; |
| 33 | struct osmo_fsm_inst; |
| 34 | |
| 35 | #define LOG_RAN_DEC(NAS_DEC, subsys, level, fmt, args...) \ |
| 36 | LOGPFSMSL((NAS_DEC)? (NAS_DEC)->caller_fi : NULL, subsys, level, "RAN decode: " fmt, ## args) |
| 37 | |
| 38 | #define LOG_RAN_ENC(FI, subsys, level, fmt, args...) \ |
| 39 | LOGPFSMSL(FI, subsys, level, "RAN encode: " fmt, ## args) |
| 40 | |
| 41 | /* These message types are named after the BSSAP procedures in nas_a.h; most are also used for RANAP procedures of |
| 42 | * similar meaning in nas_iu.h. */ |
| 43 | enum ran_msg_type { |
| 44 | RAN_MSG_NONE = 0, |
| 45 | RAN_MSG_COMPL_L3, |
| 46 | RAN_MSG_DTAP, |
| 47 | RAN_MSG_CLEAR_COMMAND, |
| 48 | RAN_MSG_CLEAR_REQUEST, |
| 49 | RAN_MSG_CLEAR_COMPLETE, |
| 50 | RAN_MSG_CLASSMARK_REQUEST, |
| 51 | RAN_MSG_CLASSMARK_UPDATE, |
| 52 | RAN_MSG_CIPHER_MODE_COMMAND, |
| 53 | RAN_MSG_CIPHER_MODE_COMPLETE, |
| 54 | RAN_MSG_CIPHER_MODE_REJECT, |
| 55 | RAN_MSG_COMMON_ID, |
| 56 | RAN_MSG_ASSIGNMENT_COMMAND, |
| 57 | RAN_MSG_ASSIGNMENT_COMPLETE, |
| 58 | RAN_MSG_ASSIGNMENT_FAILURE, |
| 59 | RAN_MSG_SAPI_N_REJECT, |
| 60 | RAN_MSG_LCLS_STATUS, |
| 61 | RAN_MSG_LCLS_BREAK_REQ, |
| 62 | RAN_MSG_HANDOVER_COMMAND, |
| 63 | RAN_MSG_HANDOVER_PERFORMED, |
| 64 | RAN_MSG_HANDOVER_REQUIRED, |
| 65 | RAN_MSG_HANDOVER_REQUIRED_REJECT, |
| 66 | RAN_MSG_HANDOVER_REQUEST, |
| 67 | RAN_MSG_HANDOVER_REQUEST_ACK, |
| 68 | RAN_MSG_HANDOVER_DETECT, |
| 69 | RAN_MSG_HANDOVER_SUCCEEDED, |
| 70 | RAN_MSG_HANDOVER_COMPLETE, |
| 71 | RAN_MSG_HANDOVER_FAILURE, |
Andreas Eversberg | 2d27e2c | 2023-04-23 12:05:44 +0200 | [diff] [blame] | 72 | RAN_MSG_VGCS_VBS_SETUP, |
| 73 | RAN_MSG_VGCS_VBS_SETUP_ACK, |
| 74 | RAN_MSG_VGCS_VBS_SETUP_REFUSE, |
| 75 | RAN_MSG_VGCS_VBS_ASSIGN_REQ, |
| 76 | RAN_MSG_VGCS_VBS_ASSIGN_RES, |
| 77 | RAN_MSG_VGCS_VBS_ASSIGN_FAIL, |
| 78 | RAN_MSG_VGCS_VBS_QUEUING_IND, |
| 79 | RAN_MSG_UPLINK_REQUEST, |
| 80 | RAN_MSG_UPLINK_REQUEST_ACK, |
| 81 | RAN_MSG_UPLINK_REQUEST_CNF, |
| 82 | RAN_MSG_UPLINK_APPLICATION_DATA, |
| 83 | RAN_MSG_UPLINK_RELEASE_IND, |
| 84 | RAN_MSG_UPLINK_REJECT_CMD, |
| 85 | RAN_MSG_UPLINK_RELEASE_CMD, |
| 86 | RAN_MSG_UPLINK_SEIZED_CMD, |
| 87 | RAN_MSG_VGCS_ADDITIONAL_INFO, |
| 88 | RAN_MSG_VGCS_VBS_AREA_CELL_INFO, |
| 89 | RAN_MSG_VGCS_VBS_ASSIGN_STATUS, |
| 90 | RAN_MSG_VGCS_SMS, |
| 91 | RAN_MSG_NOTIFICATION_DATA, |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | extern const struct value_string ran_msg_type_names[]; |
| 95 | static inline const char *ran_msg_type_name(enum ran_msg_type val) |
| 96 | { return get_value_string(ran_msg_type_names, val); } |
| 97 | |
| 98 | struct ran_clear_command { |
| 99 | enum gsm0808_cause gsm0808_cause; |
| 100 | bool csfb_ind; |
| 101 | }; |
| 102 | |
| 103 | struct ran_assignment_command { |
| 104 | const struct osmo_sockaddr_str *cn_rtp; |
| 105 | const struct gsm0808_channel_type *channel_type; |
| 106 | enum nsap_addr_enc rab_assign_addr_enc; |
Pau Espin Pedrol | a3cdab4 | 2019-05-09 17:54:08 +0200 | [diff] [blame] | 107 | bool osmux_present; |
| 108 | uint8_t osmux_cid; |
Philipp Maier | f34d945 | 2020-06-05 15:49:35 +0200 | [diff] [blame] | 109 | bool call_id_present; |
| 110 | uint32_t call_id; |
Keith Whyte | a1a70be | 2021-05-16 02:59:52 +0200 | [diff] [blame] | 111 | struct osmo_lcls *lcls; |
Andreas Eversberg | 9bbdc34 | 2023-06-21 13:35:38 +0200 | [diff] [blame] | 112 | bool callref_present; |
| 113 | struct gsm0808_group_callref callref; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | struct ran_cipher_mode_command { |
| 117 | const struct osmo_auth_vector *vec; |
| 118 | const struct osmo_gsm48_classmark *classmark; |
| 119 | struct { |
| 120 | bool umts_aka; |
| 121 | bool retrieve_imeisv; |
| 122 | uint8_t a5_encryption_mask; |
| 123 | |
| 124 | /* out-argument to return the key to the caller, pass NULL if not needed. */ |
| 125 | struct geran_encr *chosen_key; |
| 126 | } geran; |
Harald Welte | 274b70f | 2021-02-06 16:47:39 +0100 | [diff] [blame] | 127 | struct { |
Harald Welte | 505a94a | 2021-02-06 17:12:20 +0100 | [diff] [blame] | 128 | uint8_t uea_encryption_mask; |
Harald Welte | 274b70f | 2021-02-06 16:47:39 +0100 | [diff] [blame] | 129 | } utran; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | struct ran_handover_request { |
| 133 | const char *imsi; |
| 134 | const struct osmo_gsm48_classmark *classmark; |
| 135 | /* A Handover Request on GERAN-A sends separate IEs for |
| 136 | * - permitted algorithms, here composed from the a5_encryption_mask, |
| 137 | * - the key, here taken from chosen_encryption->key iff chosen_encryption is present, |
| 138 | * - the actually chosen algorithm ("Serving"), here taken from chosen_encryption->alg_id. |
| 139 | */ |
| 140 | struct { |
| 141 | struct gsm0808_channel_type *channel_type; |
| 142 | uint8_t a5_encryption_mask; |
| 143 | /*! chosen_encryption->alg_id is in encoded format: |
| 144 | * alg_id == 1 means A5/0 i.e. no encryption, alg_id == 4 means A5/3. |
| 145 | * alg_id == 0 means no such IE was present. */ |
| 146 | struct geran_encr *chosen_encryption; |
| 147 | } geran; |
| 148 | struct gsm0808_cell_id cell_id_serving; |
| 149 | struct gsm0808_cell_id cell_id_target; |
| 150 | |
| 151 | enum gsm0808_cause bssap_cause; |
| 152 | |
| 153 | bool current_channel_type_1_present; |
| 154 | uint8_t current_channel_type_1; |
| 155 | |
| 156 | enum gsm0808_permitted_speech speech_version_used; |
| 157 | |
| 158 | const uint8_t *old_bss_to_new_bss_info_raw; |
| 159 | uint8_t old_bss_to_new_bss_info_raw_len; |
| 160 | |
| 161 | struct osmo_sockaddr_str *rtp_ran_local; |
| 162 | |
| 163 | struct gsm0808_speech_codec_list *codec_list_msc_preferred; |
| 164 | |
| 165 | bool call_id_present; |
| 166 | uint32_t call_id; |
| 167 | |
| 168 | const uint8_t *global_call_reference; |
| 169 | uint8_t global_call_reference_len; |
| 170 | }; |
| 171 | |
| 172 | struct ran_handover_request_ack { |
| 173 | const uint8_t *rr_ho_command; |
| 174 | uint8_t rr_ho_command_len; |
| 175 | bool chosen_channel_present; |
| 176 | uint8_t chosen_channel; |
| 177 | /*! chosen_encr_alg is in encoded format: |
| 178 | * chosen_encr_alg == 1 means A5/0 i.e. no encryption, chosen_encr_alg == 4 means A5/3. |
| 179 | * chosen_encr_alg == 0 means no such IE was present. */ |
| 180 | uint8_t chosen_encr_alg; |
| 181 | |
| 182 | /* chosen_speech_version == 0 means "not present" */ |
| 183 | enum gsm0808_permitted_speech chosen_speech_version; |
| 184 | |
| 185 | struct osmo_sockaddr_str remote_rtp; |
| 186 | bool codec_present; |
Neels Hofmeyr | 7934e0d | 2022-10-31 18:13:47 +0100 | [diff] [blame] | 187 | struct gsm0808_speech_codec codec; |
| 188 | bool codec_with_iuup; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | struct ran_handover_command { |
| 192 | const uint8_t *rr_ho_command; |
| 193 | uint8_t rr_ho_command_len; |
| 194 | |
| 195 | const uint8_t *new_bss_to_old_bss_info_raw; |
| 196 | uint8_t new_bss_to_old_bss_info_raw_len; |
| 197 | }; |
| 198 | |
| 199 | struct ran_handover_required { |
| 200 | uint16_t cause; |
| 201 | struct gsm0808_cell_id_list2 cil; |
| 202 | |
| 203 | bool current_channel_type_1_present; |
| 204 | /*! See gsm0808_chosen_channel() */ |
| 205 | uint8_t current_channel_type_1; |
| 206 | |
| 207 | enum gsm0808_permitted_speech speech_version_used; |
| 208 | |
| 209 | uint8_t *old_bss_to_new_bss_info_raw; |
| 210 | size_t old_bss_to_new_bss_info_raw_len; |
| 211 | }; |
| 212 | |
| 213 | struct ran_msg { |
| 214 | enum ran_msg_type msg_type; |
| 215 | |
| 216 | /* Since different RAN implementations feed these messages, they should place here an implementation specific |
| 217 | * string constant to name the actual message (e.g. "BSSMAP Assignment Complete" vs. "RANAP RAB Assignment |
| 218 | * Response") */ |
| 219 | const char *msg_name; |
| 220 | |
| 221 | union { |
| 222 | struct { |
| 223 | const struct gsm0808_cell_id *cell_id; |
Neels Hofmeyr | 8a50cfb | 2019-10-21 03:01:00 +0200 | [diff] [blame] | 224 | const struct gsm0808_speech_codec_list *codec_list_bss_supported; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 225 | struct msgb *msg; |
| 226 | } compl_l3; |
| 227 | struct msgb *dtap; |
| 228 | struct { |
| 229 | enum gsm0808_cause bssap_cause; |
| 230 | #define RAN_MSG_BSSAP_CAUSE_UNSET 0xffff |
| 231 | } clear_request; |
| 232 | struct ran_clear_command clear_command; |
| 233 | struct { |
| 234 | const struct osmo_gsm48_classmark *classmark; |
| 235 | } classmark_update; |
| 236 | struct ran_cipher_mode_command cipher_mode_command; |
| 237 | struct { |
| 238 | /*! alg_id is in encoded format: |
| 239 | * alg_id == 1 means A5/0 i.e. no encryption, alg_id == 4 means A5/3. |
| 240 | * alg_id == 0 means no such IE was present. */ |
| 241 | uint8_t alg_id; |
Alexander Couzens | 2aaff75 | 2021-10-19 17:09:11 +0200 | [diff] [blame] | 242 | /*! utran integrity protection. 0..15 */ |
| 243 | int16_t utran_integrity; |
| 244 | /*! utran_integrity is in encoded format: |
| 245 | * utran_integrity == -1 means no such IE was present |
| 246 | * utran_integrity == 0 means no encryption. */ |
| 247 | int16_t utran_encryption; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 248 | const char *imeisv; |
Neels Hofmeyr | e9a3911 | 2019-08-29 00:10:49 +0200 | [diff] [blame] | 249 | const struct tlv_p_entry *l3_msg; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 250 | } cipher_mode_complete; |
| 251 | struct { |
| 252 | enum gsm0808_cause bssap_cause; |
| 253 | } cipher_mode_reject; |
| 254 | struct { |
| 255 | const char *imsi; |
Pau Espin Pedrol | 6710670 | 2021-04-27 18:20:15 +0200 | [diff] [blame] | 256 | bool last_eutran_plmn_present; |
| 257 | struct osmo_plmn_id last_eutran_plmn; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 258 | } common_id; |
| 259 | struct { |
| 260 | enum gsm48_reject_value cause; |
| 261 | } cm_service_reject; |
| 262 | struct ran_assignment_command assignment_command; |
| 263 | struct { |
| 264 | struct osmo_sockaddr_str remote_rtp; |
| 265 | bool codec_present; |
Neels Hofmeyr | cec51b3 | 2023-03-01 03:47:45 +0100 | [diff] [blame] | 266 | struct gsm0808_speech_codec codec; |
| 267 | bool codec_with_iuup; |
Neels Hofmeyr | 8a50cfb | 2019-10-21 03:01:00 +0200 | [diff] [blame] | 268 | const struct gsm0808_speech_codec_list *codec_list_bss_supported; |
Pau Espin Pedrol | a3cdab4 | 2019-05-09 17:54:08 +0200 | [diff] [blame] | 269 | bool osmux_present; |
| 270 | uint8_t osmux_cid; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 271 | } assignment_complete; |
| 272 | struct { |
| 273 | enum gsm0808_cause bssap_cause; |
| 274 | uint8_t rr_cause; |
| 275 | const struct gsm0808_speech_codec_list *scl_bss_supported; |
| 276 | } assignment_failure; |
| 277 | struct { |
| 278 | enum gsm0808_cause bssap_cause; |
| 279 | uint8_t dlci; |
| 280 | } sapi_n_reject; |
| 281 | struct { |
| 282 | enum gsm0808_lcls_status status; |
| 283 | } lcls_status; |
| 284 | struct { |
| 285 | int todo; |
| 286 | } lcls_break_req; |
| 287 | struct ran_handover_required handover_required; |
| 288 | struct gsm0808_handover_required_reject handover_required_reject; |
| 289 | struct ran_handover_command handover_command; |
| 290 | struct { |
| 291 | enum gsm0808_cause cause; |
| 292 | } handover_failure; |
| 293 | struct ran_handover_request handover_request; |
| 294 | struct ran_handover_request_ack handover_request_ack; |
Andreas Eversberg | 2d27e2c | 2023-04-23 12:05:44 +0200 | [diff] [blame] | 295 | struct gsm0808_vgcs_vbs_setup vgcs_vbs_setup; |
| 296 | struct gsm0808_vgcs_vbs_setup_ack vgcs_vbs_setup_ack; |
| 297 | struct { |
| 298 | enum gsm0808_cause cause; |
| 299 | } vgcs_vbs_setup_refuse; |
| 300 | struct gsm0808_vgcs_vbs_assign_req vgcs_vbs_assign_req; |
| 301 | struct gsm0808_vgcs_vbs_assign_res vgcs_vbs_assign_res; |
| 302 | struct gsm0808_vgcs_vbs_assign_fail vgcs_vbs_assign_fail; |
| 303 | struct gsm0808_uplink_request uplink_request; |
| 304 | struct gsm0808_uplink_request_ack uplink_request_ack; |
| 305 | struct gsm0808_uplink_request_cnf uplink_request_cnf; |
| 306 | struct gsm0808_uplink_app_data uplink_app_data; |
| 307 | struct gsm0808_uplink_release_ind uplink_release_ind; |
| 308 | struct gsm0808_uplink_seized_cmd uplink_seized_cmd; |
| 309 | struct gsm0808_uplink_reject_cmd uplink_reject_cmd; |
| 310 | struct { |
| 311 | enum gsm0808_cause cause; |
| 312 | } uplink_release_cmd; |
| 313 | struct { |
| 314 | struct gsm0808_talker_identity talker_identity; |
| 315 | } vgcs_additional_info; |
| 316 | struct gsm0808_vgcs_vbs_area_cell_info vgcs_vbs_area_cell_info; |
| 317 | struct gsm0808_vgcs_vbs_assign_stat vgcs_vbs_assign_stat; |
| 318 | struct { |
| 319 | struct gsm0808_sms_to_vgcs sms_to_vgcs; |
| 320 | } vgcs_sms; |
| 321 | struct gsm0808_notification_data notification_data; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 322 | }; |
| 323 | }; |
| 324 | |
| 325 | /* MSC-A/I/T roles implement this to receive decoded NAS messages, upon feeding an L2 msgb to a ran_dec_l2_t matching the |
| 326 | * RAN type implementation. */ |
| 327 | typedef int (* ran_decode_cb_t )(struct osmo_fsm_inst *caller_fi, void *caller_data, const struct ran_msg *msg); |
| 328 | |
| 329 | struct ran_dec { |
| 330 | /* caller provided osmo_fsm_inst, used both for logging from within decoding of NAS events, as well as caller's |
| 331 | * context in decode_cb(). */ |
| 332 | struct osmo_fsm_inst *caller_fi; |
| 333 | void *caller_data; |
| 334 | |
| 335 | /* Callback receives the decoded NAS messages */ |
| 336 | ran_decode_cb_t decode_cb; |
| 337 | }; |
| 338 | |
| 339 | /* NAS decoders (BSSAP/RANAP) implement this to turn a msgb into a struct ran_msg. |
| 340 | * An implementation typically calls ran_decoded() when done decoding. |
| 341 | * NAS decoding is modeled with a callback instead of a plain decoding, because some L2 messages by design contain more |
| 342 | * than one NAS event, e.g. Ciphering Mode Complete may include another L3 message for Identity Response, and LCLS |
| 343 | * Information messages can contain Status and Break Req events. */ |
| 344 | typedef int (* ran_dec_l2_t )(struct ran_dec *ran_dec, struct msgb *l2); |
| 345 | |
| 346 | int ran_decoded(struct ran_dec *ran_dec, struct ran_msg *msg); |
| 347 | |
| 348 | /* An MSC-A/I/T role that receives NAS events containing DTAP buffers may use this to detect DTAP duplicates as in TS |
| 349 | * 24.007 11.2.3.2 Message Type Octet / Duplicate Detection */ |
| 350 | bool ran_dec_dtap_undup_is_duplicate(struct osmo_fsm_inst *log_fi, uint8_t *n_sd_next, bool is_r99, struct msgb *l3); |
| 351 | |
| 352 | /* Implemented by individual RAN implementations, see ran_a_encode() and ran_iu_encode(). */ |
| 353 | typedef struct msgb *(* ran_encode_t )(struct osmo_fsm_inst *caller_fi, const struct ran_msg *ran_enc_msg); |