Neels Hofmeyr | a8945ce | 2018-11-30 00:44:32 +0100 | [diff] [blame] | 1 | #pragma once |
| 2 | /* MSC RAN connection implementation */ |
| 3 | |
| 4 | #include <stdint.h> |
| 5 | |
| 6 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
| 7 | #include <osmocom/sigtran/sccp_sap.h> |
| 8 | #include <osmocom/mgcp_client/mgcp_client.h> |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 9 | #include <osmocom/gsm/gsm_utils.h> |
Neels Hofmeyr | a8945ce | 2018-11-30 00:44:32 +0100 | [diff] [blame] | 10 | |
Neels Hofmeyr | 46c06e2 | 2019-01-04 17:42:05 +0100 | [diff] [blame] | 11 | #define LOG_RAN_CONN(conn, level, fmt, args ...) \ |
| 12 | LOG_RAN_CONN_CAT(conn, (conn) ? (conn)->log_subsys : DMSC, level, fmt, ## args) |
| 13 | |
| 14 | #define LOG_RAN_CONN_CAT(conn, subsys, level, fmt, args ...) \ |
| 15 | LOGPFSMSL((conn)? (conn)->fi : NULL, subsys, level, fmt, ## args) |
| 16 | |
Neels Hofmeyr | 7c5346c | 2019-02-19 02:36:35 +0100 | [diff] [blame] | 17 | #define VSUB_USE_CONN "conn" |
| 18 | |
Neels Hofmeyr | a8945ce | 2018-11-30 00:44:32 +0100 | [diff] [blame] | 19 | enum ran_conn_fsm_event { |
| 20 | /* Accepted the initial Complete Layer 3 (starting to evaluate Authentication and Ciphering) */ |
| 21 | RAN_CONN_E_COMPLETE_LAYER_3, |
| 22 | /* Received Classmark Update, typically neede for Ciphering Mode Command */ |
| 23 | RAN_CONN_E_CLASSMARK_UPDATE, |
| 24 | /* LU or Process Access FSM has determined that this conn is good */ |
| 25 | RAN_CONN_E_ACCEPTED, |
| 26 | /* received first reply from MS in "real" CC, SMS, USSD communication */ |
| 27 | RAN_CONN_E_COMMUNICATING, |
| 28 | /* Some async action has completed, check again whether all is done */ |
| 29 | RAN_CONN_E_RELEASE_WHEN_UNUSED, |
| 30 | /* MS/BTS/BSC originated close request */ |
| 31 | RAN_CONN_E_MO_CLOSE, |
| 32 | /* MSC originated close request, e.g. failed authentication */ |
| 33 | RAN_CONN_E_CN_CLOSE, |
| 34 | /* The usage count for the conn has reached zero */ |
| 35 | RAN_CONN_E_UNUSED, |
| 36 | }; |
| 37 | |
| 38 | enum ran_conn_fsm_state { |
| 39 | RAN_CONN_S_NEW, |
| 40 | RAN_CONN_S_AUTH_CIPH, |
| 41 | RAN_CONN_S_WAIT_CLASSMARK_UPDATE, |
| 42 | RAN_CONN_S_ACCEPTED, |
| 43 | RAN_CONN_S_COMMUNICATING, |
| 44 | RAN_CONN_S_RELEASING, |
| 45 | RAN_CONN_S_RELEASED, |
| 46 | }; |
| 47 | |
| 48 | enum integrity_protection_state { |
| 49 | INTEGRITY_PROTECTION_NONE = 0, |
| 50 | INTEGRITY_PROTECTION_IK = 1, |
| 51 | INTEGRITY_PROTECTION_IK_CK = 2, |
| 52 | }; |
| 53 | |
| 54 | enum complete_layer3_type { |
| 55 | COMPLETE_LAYER3_NONE, |
| 56 | COMPLETE_LAYER3_LU, |
| 57 | COMPLETE_LAYER3_CM_SERVICE_REQ, |
| 58 | COMPLETE_LAYER3_PAGING_RESP, |
| 59 | }; |
| 60 | |
| 61 | #define MAX_A5_KEY_LEN (128/8) |
| 62 | |
| 63 | struct geran_encr { |
| 64 | uint8_t alg_id; |
| 65 | uint8_t key_len; |
| 66 | uint8_t key[MAX_A5_KEY_LEN]; |
| 67 | }; |
| 68 | |
| 69 | extern const struct value_string complete_layer3_type_names[]; |
| 70 | static inline const char *complete_layer3_type_name(enum complete_layer3_type val) |
| 71 | { |
| 72 | return get_value_string(complete_layer3_type_names, val); |
| 73 | } |
| 74 | |
| 75 | struct gsm_classmark { |
| 76 | bool classmark1_set; |
| 77 | struct gsm48_classmark1 classmark1; |
| 78 | uint8_t classmark2_len; |
| 79 | uint8_t classmark2[3]; |
| 80 | uint8_t classmark3_len; |
| 81 | uint8_t classmark3[14]; /* if cm3 gets extended by spec, it will be truncated */ |
| 82 | }; |
| 83 | |
| 84 | /* active radio connection of a mobile subscriber */ |
| 85 | struct ran_conn { |
| 86 | /* global linked list of ran_conn instances */ |
| 87 | struct llist_head entry; |
| 88 | |
| 89 | /* FSM instance to control the RAN connection's permissions and lifetime. */ |
| 90 | struct osmo_fsm_inst *fi; |
| 91 | enum complete_layer3_type complete_layer3_type; |
| 92 | |
| 93 | /* usage count. If this drops to zero, we start the release |
| 94 | * towards A/Iu */ |
| 95 | uint32_t use_count; |
| 96 | uint32_t use_tokens; |
| 97 | |
| 98 | /* The MS has opened the conn with a CM Service Request, and we shall |
| 99 | * keep it open for an actual request (or until timeout). */ |
| 100 | bool received_cm_service_request; |
| 101 | |
| 102 | /* libmsc/libvlr subscriber information (if available) */ |
| 103 | struct vlr_subscr *vsub; |
| 104 | |
| 105 | /* LU expiration handling */ |
| 106 | uint8_t expire_timer_stopped; |
Neels Hofmeyr | a8945ce | 2018-11-30 00:44:32 +0100 | [diff] [blame] | 107 | |
| 108 | /* Are we part of a special "silent" call */ |
| 109 | int silent_call; |
| 110 | |
| 111 | /* back pointers */ |
| 112 | struct gsm_network *network; |
| 113 | |
| 114 | /* connected via 2G or 3G? */ |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 115 | enum osmo_rat_type via_ran; |
Neels Hofmeyr | 46c06e2 | 2019-01-04 17:42:05 +0100 | [diff] [blame] | 116 | /* whether to log on DBSSAP, DIUCS, ... */ |
| 117 | int log_subsys; |
Neels Hofmeyr | a8945ce | 2018-11-30 00:44:32 +0100 | [diff] [blame] | 118 | |
| 119 | uint16_t lac; |
| 120 | struct geran_encr geran_encr; |
| 121 | |
| 122 | /* "Temporary" storage for the case the VLR asked for Cipher Mode Command, but the MSC still |
| 123 | * wants to request a Classmark Update first. */ |
| 124 | struct { |
| 125 | bool umts_aka; |
| 126 | bool retrieve_imeisv; |
| 127 | } geran_set_cipher_mode; |
| 128 | |
| 129 | /* N(SD) expected in the received frame, per flow (TS 24.007 11.2.3.2.3.2.2) */ |
| 130 | uint8_t n_sd_next[4]; |
| 131 | |
| 132 | struct { |
| 133 | struct mgcp_ctx *mgcp_ctx; |
| 134 | unsigned int mgcp_rtp_endpoint; |
| 135 | |
| 136 | uint16_t local_port_ran; |
| 137 | char local_addr_ran[INET_ADDRSTRLEN]; |
| 138 | uint16_t remote_port_ran; |
| 139 | char remote_addr_ran[INET_ADDRSTRLEN]; |
| 140 | enum mgcp_codecs codec_ran; |
| 141 | |
| 142 | uint16_t local_port_cn; |
| 143 | char local_addr_cn[INET_ADDRSTRLEN]; |
| 144 | uint16_t remote_port_cn; |
| 145 | char remote_addr_cn[INET_ADDRSTRLEN]; |
| 146 | enum mgcp_codecs codec_cn; |
| 147 | } rtp; |
| 148 | |
| 149 | /* which Iu-CS connection, if any. */ |
| 150 | struct { |
| 151 | struct ranap_ue_conn_ctx *ue_ctx; |
| 152 | uint8_t rab_id; |
| 153 | bool waiting_for_release_complete; |
| 154 | } iu; |
| 155 | |
| 156 | struct { |
| 157 | /* A pointer to the SCCP user that handles |
| 158 | * the SCCP connections for this subscriber |
| 159 | * connection */ |
| 160 | struct osmo_sccp_user *scu; |
| 161 | |
| 162 | /* The address of the BSC that is associated |
| 163 | * with this RAN connection */ |
| 164 | struct osmo_sccp_addr bsc_addr; |
| 165 | |
| 166 | /* The connection identifier that is used |
| 167 | * to reference the SCCP connection that is |
| 168 | * associated with this RAN connection */ |
| 169 | uint32_t conn_id; |
| 170 | |
| 171 | bool waiting_for_clear_complete; |
| 172 | } a; |
| 173 | |
| 174 | /* Temporary storage for Classmark Information for times when a connection has no VLR subscriber |
| 175 | * associated yet. It will get copied to the VLR subscriber upon msc_vlr_subscr_assoc(). */ |
| 176 | struct gsm_classmark temporary_classmark; |
| 177 | }; |
| 178 | |
Neels Hofmeyr | 7814a83 | 2018-12-26 00:40:18 +0100 | [diff] [blame] | 179 | struct ran_conn *ran_conn_alloc(struct gsm_network *network, enum osmo_rat_type via_ran, uint16_t lac); |
Neels Hofmeyr | a8945ce | 2018-11-30 00:44:32 +0100 | [diff] [blame] | 180 | |
Neels Hofmeyr | 361e571 | 2019-01-03 02:32:14 +0100 | [diff] [blame] | 181 | void ran_conn_update_id_from_mi(struct ran_conn *conn, const uint8_t *mi, uint8_t mi_len); |
| 182 | void ran_conn_update_id(struct ran_conn *conn); |
| 183 | const char *ran_conn_get_conn_id(struct ran_conn *conn); |
| 184 | void ran_conn_update_id_for_vsub(struct vlr_subscr *for_vsub); |
Neels Hofmeyr | a8945ce | 2018-11-30 00:44:32 +0100 | [diff] [blame] | 185 | |
| 186 | void ran_conn_complete_layer_3(struct ran_conn *conn); |
| 187 | |
| 188 | void ran_conn_sapi_n_reject(struct ran_conn *conn, int dlci); |
| 189 | int ran_conn_clear_request(struct ran_conn *conn, uint32_t cause); |
| 190 | void ran_conn_compl_l3(struct ran_conn *conn, |
| 191 | struct msgb *msg, uint16_t chosen_channel); |
| 192 | void ran_conn_dtap(struct ran_conn *conn, struct msgb *msg); |
| 193 | int ran_conn_classmark_request_then_cipher_mode_cmd(struct ran_conn *conn, bool umts_aka, |
| 194 | bool retrieve_imeisv); |
| 195 | int ran_conn_geran_set_cipher_mode(struct ran_conn *conn, bool umts_aka, bool retrieve_imeisv); |
| 196 | void ran_conn_cipher_mode_compl(struct ran_conn *conn, struct msgb *msg, uint8_t alg_id); |
| 197 | void ran_conn_rx_sec_mode_compl(struct ran_conn *conn); |
| 198 | void ran_conn_classmark_chg(struct ran_conn *conn, |
| 199 | const uint8_t *cm2, uint8_t cm2_len, |
| 200 | const uint8_t *cm3, uint8_t cm3_len); |
Neels Hofmeyr | a8945ce | 2018-11-30 00:44:32 +0100 | [diff] [blame] | 201 | void ran_conn_assign_fail(struct ran_conn *conn, uint8_t cause, uint8_t *rr_cause); |
| 202 | |
| 203 | void ran_conn_init(void); |
| 204 | bool ran_conn_is_accepted(const struct ran_conn *conn); |
| 205 | bool ran_conn_is_establishing_auth_ciph(const struct ran_conn *conn); |
| 206 | void ran_conn_communicating(struct ran_conn *conn); |
| 207 | void ran_conn_close(struct ran_conn *conn, uint32_t cause); |
| 208 | void ran_conn_mo_close(struct ran_conn *conn, uint32_t cause); |
| 209 | bool ran_conn_in_release(struct ran_conn *conn); |
| 210 | |
| 211 | void ran_conn_rx_bssmap_clear_complete(struct ran_conn *conn); |
| 212 | void ran_conn_rx_iu_release_complete(struct ran_conn *conn); |
Harald Welte | 0df904d | 2018-12-03 11:00:04 +0100 | [diff] [blame] | 213 | void ran_conn_sgs_release_sent(struct ran_conn *conn); |
Neels Hofmeyr | a8945ce | 2018-11-30 00:44:32 +0100 | [diff] [blame] | 214 | |
| 215 | enum ran_conn_use { |
| 216 | RAN_CONN_USE_UNTRACKED = -1, |
| 217 | RAN_CONN_USE_COMPL_L3, |
| 218 | RAN_CONN_USE_DTAP, |
| 219 | RAN_CONN_USE_AUTH_CIPH, |
| 220 | RAN_CONN_USE_CM_SERVICE, |
| 221 | RAN_CONN_USE_TRANS_CC, |
| 222 | RAN_CONN_USE_TRANS_SMS, |
| 223 | RAN_CONN_USE_TRANS_NC_SS, |
| 224 | RAN_CONN_USE_SILENT_CALL, |
| 225 | RAN_CONN_USE_RELEASE, |
| 226 | }; |
| 227 | |
| 228 | extern const struct value_string ran_conn_use_names[]; |
| 229 | static inline const char *ran_conn_use_name(enum ran_conn_use val) |
| 230 | { return get_value_string(ran_conn_use_names, val); } |
| 231 | |
| 232 | #define ran_conn_get(conn, balance_token) \ |
| 233 | _ran_conn_get(conn, balance_token, __FILE__, __LINE__) |
| 234 | #define ran_conn_put(conn, balance_token) \ |
| 235 | _ran_conn_put(conn, balance_token, __FILE__, __LINE__) |
| 236 | struct ran_conn * _ran_conn_get(struct ran_conn *conn, enum ran_conn_use balance_token, |
| 237 | const char *file, int line); |
| 238 | void _ran_conn_put(struct ran_conn *conn, enum ran_conn_use balance_token, |
| 239 | const char *file, int line); |
| 240 | bool ran_conn_used_by(struct ran_conn *conn, enum ran_conn_use token); |