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