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, |
| 72 | }; |
| 73 | |
| 74 | extern const struct value_string ran_msg_type_names[]; |
| 75 | static inline const char *ran_msg_type_name(enum ran_msg_type val) |
| 76 | { return get_value_string(ran_msg_type_names, val); } |
| 77 | |
| 78 | struct ran_clear_command { |
| 79 | enum gsm0808_cause gsm0808_cause; |
| 80 | bool csfb_ind; |
| 81 | }; |
| 82 | |
| 83 | struct ran_assignment_command { |
| 84 | const struct osmo_sockaddr_str *cn_rtp; |
| 85 | const struct gsm0808_channel_type *channel_type; |
| 86 | enum nsap_addr_enc rab_assign_addr_enc; |
Pau Espin Pedrol | a3cdab4 | 2019-05-09 17:54:08 +0200 | [diff] [blame] | 87 | bool osmux_present; |
| 88 | uint8_t osmux_cid; |
Philipp Maier | f34d945 | 2020-06-05 15:49:35 +0200 | [diff] [blame] | 89 | bool call_id_present; |
| 90 | uint32_t call_id; |
Keith Whyte | a1a70be | 2021-05-16 02:59:52 +0200 | [diff] [blame] | 91 | struct osmo_lcls *lcls; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | struct ran_cipher_mode_command { |
| 95 | const struct osmo_auth_vector *vec; |
| 96 | const struct osmo_gsm48_classmark *classmark; |
| 97 | struct { |
| 98 | bool umts_aka; |
| 99 | bool retrieve_imeisv; |
| 100 | uint8_t a5_encryption_mask; |
| 101 | |
| 102 | /* out-argument to return the key to the caller, pass NULL if not needed. */ |
| 103 | struct geran_encr *chosen_key; |
| 104 | } geran; |
Harald Welte | 274b70f | 2021-02-06 16:47:39 +0100 | [diff] [blame] | 105 | struct { |
Harald Welte | 505a94a | 2021-02-06 17:12:20 +0100 | [diff] [blame] | 106 | uint8_t uea_encryption_mask; |
Harald Welte | 274b70f | 2021-02-06 16:47:39 +0100 | [diff] [blame] | 107 | } utran; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | struct ran_handover_request { |
| 111 | const char *imsi; |
| 112 | const struct osmo_gsm48_classmark *classmark; |
| 113 | /* A Handover Request on GERAN-A sends separate IEs for |
| 114 | * - permitted algorithms, here composed from the a5_encryption_mask, |
| 115 | * - the key, here taken from chosen_encryption->key iff chosen_encryption is present, |
| 116 | * - the actually chosen algorithm ("Serving"), here taken from chosen_encryption->alg_id. |
| 117 | */ |
| 118 | struct { |
| 119 | struct gsm0808_channel_type *channel_type; |
| 120 | uint8_t a5_encryption_mask; |
| 121 | /*! chosen_encryption->alg_id is in encoded format: |
| 122 | * alg_id == 1 means A5/0 i.e. no encryption, alg_id == 4 means A5/3. |
| 123 | * alg_id == 0 means no such IE was present. */ |
| 124 | struct geran_encr *chosen_encryption; |
| 125 | } geran; |
| 126 | struct gsm0808_cell_id cell_id_serving; |
| 127 | struct gsm0808_cell_id cell_id_target; |
| 128 | |
| 129 | enum gsm0808_cause bssap_cause; |
| 130 | |
| 131 | bool current_channel_type_1_present; |
| 132 | uint8_t current_channel_type_1; |
| 133 | |
| 134 | enum gsm0808_permitted_speech speech_version_used; |
| 135 | |
| 136 | const uint8_t *old_bss_to_new_bss_info_raw; |
| 137 | uint8_t old_bss_to_new_bss_info_raw_len; |
| 138 | |
| 139 | struct osmo_sockaddr_str *rtp_ran_local; |
| 140 | |
| 141 | struct gsm0808_speech_codec_list *codec_list_msc_preferred; |
| 142 | |
| 143 | bool call_id_present; |
| 144 | uint32_t call_id; |
| 145 | |
| 146 | const uint8_t *global_call_reference; |
| 147 | uint8_t global_call_reference_len; |
| 148 | }; |
| 149 | |
| 150 | struct ran_handover_request_ack { |
| 151 | const uint8_t *rr_ho_command; |
| 152 | uint8_t rr_ho_command_len; |
| 153 | bool chosen_channel_present; |
| 154 | uint8_t chosen_channel; |
| 155 | /*! chosen_encr_alg is in encoded format: |
| 156 | * chosen_encr_alg == 1 means A5/0 i.e. no encryption, chosen_encr_alg == 4 means A5/3. |
| 157 | * chosen_encr_alg == 0 means no such IE was present. */ |
| 158 | uint8_t chosen_encr_alg; |
| 159 | |
| 160 | /* chosen_speech_version == 0 means "not present" */ |
| 161 | enum gsm0808_permitted_speech chosen_speech_version; |
| 162 | |
| 163 | struct osmo_sockaddr_str remote_rtp; |
| 164 | bool codec_present; |
| 165 | enum mgcp_codecs codec; |
| 166 | }; |
| 167 | |
| 168 | struct ran_handover_command { |
| 169 | const uint8_t *rr_ho_command; |
| 170 | uint8_t rr_ho_command_len; |
| 171 | |
| 172 | const uint8_t *new_bss_to_old_bss_info_raw; |
| 173 | uint8_t new_bss_to_old_bss_info_raw_len; |
| 174 | }; |
| 175 | |
| 176 | struct ran_handover_required { |
| 177 | uint16_t cause; |
| 178 | struct gsm0808_cell_id_list2 cil; |
| 179 | |
| 180 | bool current_channel_type_1_present; |
| 181 | /*! See gsm0808_chosen_channel() */ |
| 182 | uint8_t current_channel_type_1; |
| 183 | |
| 184 | enum gsm0808_permitted_speech speech_version_used; |
| 185 | |
| 186 | uint8_t *old_bss_to_new_bss_info_raw; |
| 187 | size_t old_bss_to_new_bss_info_raw_len; |
| 188 | }; |
| 189 | |
| 190 | struct ran_msg { |
| 191 | enum ran_msg_type msg_type; |
| 192 | |
| 193 | /* Since different RAN implementations feed these messages, they should place here an implementation specific |
| 194 | * string constant to name the actual message (e.g. "BSSMAP Assignment Complete" vs. "RANAP RAB Assignment |
| 195 | * Response") */ |
| 196 | const char *msg_name; |
| 197 | |
| 198 | union { |
| 199 | struct { |
| 200 | const struct gsm0808_cell_id *cell_id; |
Neels Hofmeyr | 8a50cfb | 2019-10-21 03:01:00 +0200 | [diff] [blame] | 201 | const struct gsm0808_speech_codec_list *codec_list_bss_supported; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 202 | struct msgb *msg; |
| 203 | } compl_l3; |
| 204 | struct msgb *dtap; |
| 205 | struct { |
| 206 | enum gsm0808_cause bssap_cause; |
| 207 | #define RAN_MSG_BSSAP_CAUSE_UNSET 0xffff |
| 208 | } clear_request; |
| 209 | struct ran_clear_command clear_command; |
| 210 | struct { |
| 211 | const struct osmo_gsm48_classmark *classmark; |
| 212 | } classmark_update; |
| 213 | struct ran_cipher_mode_command cipher_mode_command; |
| 214 | struct { |
| 215 | /*! alg_id is in encoded format: |
| 216 | * alg_id == 1 means A5/0 i.e. no encryption, alg_id == 4 means A5/3. |
| 217 | * alg_id == 0 means no such IE was present. */ |
| 218 | uint8_t alg_id; |
Alexander Couzens | 2aaff75 | 2021-10-19 17:09:11 +0200 | [diff] [blame] | 219 | /*! utran integrity protection. 0..15 */ |
| 220 | int16_t utran_integrity; |
| 221 | /*! utran_integrity is in encoded format: |
| 222 | * utran_integrity == -1 means no such IE was present |
| 223 | * utran_integrity == 0 means no encryption. */ |
| 224 | int16_t utran_encryption; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 225 | const char *imeisv; |
Neels Hofmeyr | e9a3911 | 2019-08-29 00:10:49 +0200 | [diff] [blame] | 226 | const struct tlv_p_entry *l3_msg; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 227 | } cipher_mode_complete; |
| 228 | struct { |
| 229 | enum gsm0808_cause bssap_cause; |
| 230 | } cipher_mode_reject; |
| 231 | struct { |
| 232 | const char *imsi; |
Pau Espin Pedrol | 6710670 | 2021-04-27 18:20:15 +0200 | [diff] [blame] | 233 | bool last_eutran_plmn_present; |
| 234 | struct osmo_plmn_id last_eutran_plmn; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 235 | } common_id; |
| 236 | struct { |
| 237 | enum gsm48_reject_value cause; |
| 238 | } cm_service_reject; |
| 239 | struct ran_assignment_command assignment_command; |
| 240 | struct { |
| 241 | struct osmo_sockaddr_str remote_rtp; |
| 242 | bool codec_present; |
| 243 | enum mgcp_codecs codec; |
Neels Hofmeyr | 8a50cfb | 2019-10-21 03:01:00 +0200 | [diff] [blame] | 244 | const struct gsm0808_speech_codec_list *codec_list_bss_supported; |
Pau Espin Pedrol | a3cdab4 | 2019-05-09 17:54:08 +0200 | [diff] [blame] | 245 | bool osmux_present; |
| 246 | uint8_t osmux_cid; |
Neels Hofmeyr | c4628a3 | 2018-12-07 14:47:34 +0100 | [diff] [blame] | 247 | } assignment_complete; |
| 248 | struct { |
| 249 | enum gsm0808_cause bssap_cause; |
| 250 | uint8_t rr_cause; |
| 251 | const struct gsm0808_speech_codec_list *scl_bss_supported; |
| 252 | } assignment_failure; |
| 253 | struct { |
| 254 | enum gsm0808_cause bssap_cause; |
| 255 | uint8_t dlci; |
| 256 | } sapi_n_reject; |
| 257 | struct { |
| 258 | enum gsm0808_lcls_status status; |
| 259 | } lcls_status; |
| 260 | struct { |
| 261 | int todo; |
| 262 | } lcls_break_req; |
| 263 | struct ran_handover_required handover_required; |
| 264 | struct gsm0808_handover_required_reject handover_required_reject; |
| 265 | struct ran_handover_command handover_command; |
| 266 | struct { |
| 267 | enum gsm0808_cause cause; |
| 268 | } handover_failure; |
| 269 | struct ran_handover_request handover_request; |
| 270 | struct ran_handover_request_ack handover_request_ack; |
| 271 | }; |
| 272 | }; |
| 273 | |
| 274 | /* 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 |
| 275 | * RAN type implementation. */ |
| 276 | typedef int (* ran_decode_cb_t )(struct osmo_fsm_inst *caller_fi, void *caller_data, const struct ran_msg *msg); |
| 277 | |
| 278 | struct ran_dec { |
| 279 | /* caller provided osmo_fsm_inst, used both for logging from within decoding of NAS events, as well as caller's |
| 280 | * context in decode_cb(). */ |
| 281 | struct osmo_fsm_inst *caller_fi; |
| 282 | void *caller_data; |
| 283 | |
| 284 | /* Callback receives the decoded NAS messages */ |
| 285 | ran_decode_cb_t decode_cb; |
| 286 | }; |
| 287 | |
| 288 | /* NAS decoders (BSSAP/RANAP) implement this to turn a msgb into a struct ran_msg. |
| 289 | * An implementation typically calls ran_decoded() when done decoding. |
| 290 | * NAS decoding is modeled with a callback instead of a plain decoding, because some L2 messages by design contain more |
| 291 | * than one NAS event, e.g. Ciphering Mode Complete may include another L3 message for Identity Response, and LCLS |
| 292 | * Information messages can contain Status and Break Req events. */ |
| 293 | typedef int (* ran_dec_l2_t )(struct ran_dec *ran_dec, struct msgb *l2); |
| 294 | |
| 295 | int ran_decoded(struct ran_dec *ran_dec, struct ran_msg *msg); |
| 296 | |
| 297 | /* An MSC-A/I/T role that receives NAS events containing DTAP buffers may use this to detect DTAP duplicates as in TS |
| 298 | * 24.007 11.2.3.2 Message Type Octet / Duplicate Detection */ |
| 299 | bool ran_dec_dtap_undup_is_duplicate(struct osmo_fsm_inst *log_fi, uint8_t *n_sd_next, bool is_r99, struct msgb *l3); |
| 300 | |
| 301 | /* Implemented by individual RAN implementations, see ran_a_encode() and ran_iu_encode(). */ |
| 302 | typedef struct msgb *(* ran_encode_t )(struct osmo_fsm_inst *caller_fi, const struct ran_msg *ran_enc_msg); |