blob: 0b99e252c51432dccda87063256a68b37fbd203b [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 Hofmeyr7c5346c2019-02-19 02:36:35 +010017#define VSUB_USE_CONN "conn"
18
Neels Hofmeyra8945ce2018-11-30 00:44:32 +010019enum 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
38enum 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
48enum integrity_protection_state {
49 INTEGRITY_PROTECTION_NONE = 0,
50 INTEGRITY_PROTECTION_IK = 1,
51 INTEGRITY_PROTECTION_IK_CK = 2,
52};
53
54enum 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
63struct geran_encr {
64 uint8_t alg_id;
65 uint8_t key_len;
66 uint8_t key[MAX_A5_KEY_LEN];
67};
68
69extern const struct value_string complete_layer3_type_names[];
70static 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
75struct 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 */
85struct 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 Hofmeyra8945ce2018-11-30 00:44:32 +0100107
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);
Harald Welte0df904d2018-12-03 11:00:04 +0100213void ran_conn_sgs_release_sent(struct ran_conn *conn);
Neels Hofmeyra8945ce2018-11-30 00:44:32 +0100214
215enum 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
228extern const struct value_string ran_conn_use_names[];
229static 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__)
236struct ran_conn * _ran_conn_get(struct ran_conn *conn, enum ran_conn_use balance_token,
237 const char *file, int line);
238void _ran_conn_put(struct ran_conn *conn, enum ran_conn_use balance_token,
239 const char *file, int line);
240bool ran_conn_used_by(struct ran_conn *conn, enum ran_conn_use token);