blob: bec7201a14942dad1f3ee0556827f30f7b35cea8 [file] [log] [blame]
Neels Hofmeyra8945ce2018-11-30 00:44:32 +01001#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 Hofmeyr7814a832018-12-26 00:40:18 +01009#include <osmocom/gsm/gsm_utils.h>
Neels Hofmeyra8945ce2018-11-30 00:44:32 +010010
Neels Hofmeyr46c06e22019-01-04 17:42:05 +010011#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 Hofmeyra8945ce2018-11-30 00:44:32 +010017enum ran_conn_fsm_event {
18 /* Accepted the initial Complete Layer 3 (starting to evaluate Authentication and Ciphering) */
19 RAN_CONN_E_COMPLETE_LAYER_3,
20 /* Received Classmark Update, typically neede for Ciphering Mode Command */
21 RAN_CONN_E_CLASSMARK_UPDATE,
22 /* LU or Process Access FSM has determined that this conn is good */
23 RAN_CONN_E_ACCEPTED,
24 /* received first reply from MS in "real" CC, SMS, USSD communication */
25 RAN_CONN_E_COMMUNICATING,
26 /* Some async action has completed, check again whether all is done */
27 RAN_CONN_E_RELEASE_WHEN_UNUSED,
28 /* MS/BTS/BSC originated close request */
29 RAN_CONN_E_MO_CLOSE,
30 /* MSC originated close request, e.g. failed authentication */
31 RAN_CONN_E_CN_CLOSE,
32 /* The usage count for the conn has reached zero */
33 RAN_CONN_E_UNUSED,
34};
35
36enum ran_conn_fsm_state {
37 RAN_CONN_S_NEW,
38 RAN_CONN_S_AUTH_CIPH,
39 RAN_CONN_S_WAIT_CLASSMARK_UPDATE,
40 RAN_CONN_S_ACCEPTED,
41 RAN_CONN_S_COMMUNICATING,
42 RAN_CONN_S_RELEASING,
43 RAN_CONN_S_RELEASED,
44};
45
46enum integrity_protection_state {
47 INTEGRITY_PROTECTION_NONE = 0,
48 INTEGRITY_PROTECTION_IK = 1,
49 INTEGRITY_PROTECTION_IK_CK = 2,
50};
51
52enum complete_layer3_type {
53 COMPLETE_LAYER3_NONE,
54 COMPLETE_LAYER3_LU,
55 COMPLETE_LAYER3_CM_SERVICE_REQ,
56 COMPLETE_LAYER3_PAGING_RESP,
57};
58
59#define MAX_A5_KEY_LEN (128/8)
60
61struct geran_encr {
62 uint8_t alg_id;
63 uint8_t key_len;
64 uint8_t key[MAX_A5_KEY_LEN];
65};
66
67extern const struct value_string complete_layer3_type_names[];
68static inline const char *complete_layer3_type_name(enum complete_layer3_type val)
69{
70 return get_value_string(complete_layer3_type_names, val);
71}
72
73struct gsm_classmark {
74 bool classmark1_set;
75 struct gsm48_classmark1 classmark1;
76 uint8_t classmark2_len;
77 uint8_t classmark2[3];
78 uint8_t classmark3_len;
79 uint8_t classmark3[14]; /* if cm3 gets extended by spec, it will be truncated */
80};
81
82/* active radio connection of a mobile subscriber */
83struct ran_conn {
84 /* global linked list of ran_conn instances */
85 struct llist_head entry;
86
87 /* FSM instance to control the RAN connection's permissions and lifetime. */
88 struct osmo_fsm_inst *fi;
89 enum complete_layer3_type complete_layer3_type;
90
91 /* usage count. If this drops to zero, we start the release
92 * towards A/Iu */
93 uint32_t use_count;
94 uint32_t use_tokens;
95
96 /* The MS has opened the conn with a CM Service Request, and we shall
97 * keep it open for an actual request (or until timeout). */
98 bool received_cm_service_request;
99
100 /* libmsc/libvlr subscriber information (if available) */
101 struct vlr_subscr *vsub;
102
103 /* LU expiration handling */
104 uint8_t expire_timer_stopped;
105 /* SMS helpers for libmsc */
106 uint8_t next_rp_ref;
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 Hofmeyr7814a832018-12-26 00:40:18 +0100115 enum osmo_rat_type via_ran;
Neels Hofmeyr46c06e22019-01-04 17:42:05 +0100116 /* whether to log on DBSSAP, DIUCS, ... */
117 int log_subsys;
Neels Hofmeyra8945ce2018-11-30 00:44:32 +0100118
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 Hofmeyr7814a832018-12-26 00:40:18 +0100179struct ran_conn *ran_conn_alloc(struct gsm_network *network, enum osmo_rat_type via_ran, uint16_t lac);
Neels Hofmeyra8945ce2018-11-30 00:44:32 +0100180
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100181void ran_conn_update_id_from_mi(struct ran_conn *conn, const uint8_t *mi, uint8_t mi_len);
182void ran_conn_update_id(struct ran_conn *conn);
183const char *ran_conn_get_conn_id(struct ran_conn *conn);
184void ran_conn_update_id_for_vsub(struct vlr_subscr *for_vsub);
Neels Hofmeyra8945ce2018-11-30 00:44:32 +0100185
186void ran_conn_complete_layer_3(struct ran_conn *conn);
187
188void ran_conn_sapi_n_reject(struct ran_conn *conn, int dlci);
189int ran_conn_clear_request(struct ran_conn *conn, uint32_t cause);
190void ran_conn_compl_l3(struct ran_conn *conn,
191 struct msgb *msg, uint16_t chosen_channel);
192void ran_conn_dtap(struct ran_conn *conn, struct msgb *msg);
193int ran_conn_classmark_request_then_cipher_mode_cmd(struct ran_conn *conn, bool umts_aka,
194 bool retrieve_imeisv);
195int ran_conn_geran_set_cipher_mode(struct ran_conn *conn, bool umts_aka, bool retrieve_imeisv);
196void ran_conn_cipher_mode_compl(struct ran_conn *conn, struct msgb *msg, uint8_t alg_id);
197void ran_conn_rx_sec_mode_compl(struct ran_conn *conn);
198void 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 Hofmeyra8945ce2018-11-30 00:44:32 +0100201void ran_conn_assign_fail(struct ran_conn *conn, uint8_t cause, uint8_t *rr_cause);
202
203void ran_conn_init(void);
204bool ran_conn_is_accepted(const struct ran_conn *conn);
205bool ran_conn_is_establishing_auth_ciph(const struct ran_conn *conn);
206void ran_conn_communicating(struct ran_conn *conn);
207void ran_conn_close(struct ran_conn *conn, uint32_t cause);
208void ran_conn_mo_close(struct ran_conn *conn, uint32_t cause);
209bool ran_conn_in_release(struct ran_conn *conn);
210
211void ran_conn_rx_bssmap_clear_complete(struct ran_conn *conn);
212void ran_conn_rx_iu_release_complete(struct ran_conn *conn);
213
214enum ran_conn_use {
215 RAN_CONN_USE_UNTRACKED = -1,
216 RAN_CONN_USE_COMPL_L3,
217 RAN_CONN_USE_DTAP,
218 RAN_CONN_USE_AUTH_CIPH,
219 RAN_CONN_USE_CM_SERVICE,
220 RAN_CONN_USE_TRANS_CC,
221 RAN_CONN_USE_TRANS_SMS,
222 RAN_CONN_USE_TRANS_NC_SS,
223 RAN_CONN_USE_SILENT_CALL,
224 RAN_CONN_USE_RELEASE,
225};
226
227extern const struct value_string ran_conn_use_names[];
228static inline const char *ran_conn_use_name(enum ran_conn_use val)
229{ return get_value_string(ran_conn_use_names, val); }
230
231#define ran_conn_get(conn, balance_token) \
232 _ran_conn_get(conn, balance_token, __FILE__, __LINE__)
233#define ran_conn_put(conn, balance_token) \
234 _ran_conn_put(conn, balance_token, __FILE__, __LINE__)
235struct ran_conn * _ran_conn_get(struct ran_conn *conn, enum ran_conn_use balance_token,
236 const char *file, int line);
237void _ran_conn_put(struct ran_conn *conn, enum ran_conn_use balance_token,
238 const char *file, int line);
239bool ran_conn_used_by(struct ran_conn *conn, enum ran_conn_use token);