blob: 0dadd4f3ac60ff6571e339d041c91de3af325a5e [file] [log] [blame]
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001/* 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
32struct msgb;
33struct 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. */
43enum 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,
Andreas Eversberg2d27e2c2023-04-23 12:05:44 +020072 RAN_MSG_VGCS_VBS_SETUP,
73 RAN_MSG_VGCS_VBS_SETUP_ACK,
74 RAN_MSG_VGCS_VBS_SETUP_REFUSE,
75 RAN_MSG_VGCS_VBS_ASSIGN_REQ,
76 RAN_MSG_VGCS_VBS_ASSIGN_RES,
77 RAN_MSG_VGCS_VBS_ASSIGN_FAIL,
78 RAN_MSG_VGCS_VBS_QUEUING_IND,
79 RAN_MSG_UPLINK_REQUEST,
80 RAN_MSG_UPLINK_REQUEST_ACK,
81 RAN_MSG_UPLINK_REQUEST_CNF,
82 RAN_MSG_UPLINK_APPLICATION_DATA,
83 RAN_MSG_UPLINK_RELEASE_IND,
84 RAN_MSG_UPLINK_REJECT_CMD,
85 RAN_MSG_UPLINK_RELEASE_CMD,
86 RAN_MSG_UPLINK_SEIZED_CMD,
87 RAN_MSG_VGCS_ADDITIONAL_INFO,
88 RAN_MSG_VGCS_VBS_AREA_CELL_INFO,
89 RAN_MSG_VGCS_VBS_ASSIGN_STATUS,
90 RAN_MSG_VGCS_SMS,
91 RAN_MSG_NOTIFICATION_DATA,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010092};
93
94extern const struct value_string ran_msg_type_names[];
95static inline const char *ran_msg_type_name(enum ran_msg_type val)
96{ return get_value_string(ran_msg_type_names, val); }
97
98struct ran_clear_command {
99 enum gsm0808_cause gsm0808_cause;
100 bool csfb_ind;
101};
102
103struct ran_assignment_command {
104 const struct osmo_sockaddr_str *cn_rtp;
105 const struct gsm0808_channel_type *channel_type;
106 enum nsap_addr_enc rab_assign_addr_enc;
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200107 bool osmux_present;
108 uint8_t osmux_cid;
Philipp Maierf34d9452020-06-05 15:49:35 +0200109 bool call_id_present;
110 uint32_t call_id;
Keith Whytea1a70be2021-05-16 02:59:52 +0200111 struct osmo_lcls *lcls;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100112};
113
114struct ran_cipher_mode_command {
115 const struct osmo_auth_vector *vec;
116 const struct osmo_gsm48_classmark *classmark;
117 struct {
118 bool umts_aka;
119 bool retrieve_imeisv;
120 uint8_t a5_encryption_mask;
121
122 /* out-argument to return the key to the caller, pass NULL if not needed. */
123 struct geran_encr *chosen_key;
124 } geran;
Harald Welte274b70f2021-02-06 16:47:39 +0100125 struct {
Harald Welte505a94a2021-02-06 17:12:20 +0100126 uint8_t uea_encryption_mask;
Harald Welte274b70f2021-02-06 16:47:39 +0100127 } utran;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100128};
129
130struct ran_handover_request {
131 const char *imsi;
132 const struct osmo_gsm48_classmark *classmark;
133 /* A Handover Request on GERAN-A sends separate IEs for
134 * - permitted algorithms, here composed from the a5_encryption_mask,
135 * - the key, here taken from chosen_encryption->key iff chosen_encryption is present,
136 * - the actually chosen algorithm ("Serving"), here taken from chosen_encryption->alg_id.
137 */
138 struct {
139 struct gsm0808_channel_type *channel_type;
140 uint8_t a5_encryption_mask;
141 /*! chosen_encryption->alg_id is in encoded format:
142 * alg_id == 1 means A5/0 i.e. no encryption, alg_id == 4 means A5/3.
143 * alg_id == 0 means no such IE was present. */
144 struct geran_encr *chosen_encryption;
145 } geran;
146 struct gsm0808_cell_id cell_id_serving;
147 struct gsm0808_cell_id cell_id_target;
148
149 enum gsm0808_cause bssap_cause;
150
151 bool current_channel_type_1_present;
152 uint8_t current_channel_type_1;
153
154 enum gsm0808_permitted_speech speech_version_used;
155
156 const uint8_t *old_bss_to_new_bss_info_raw;
157 uint8_t old_bss_to_new_bss_info_raw_len;
158
159 struct osmo_sockaddr_str *rtp_ran_local;
160
161 struct gsm0808_speech_codec_list *codec_list_msc_preferred;
162
163 bool call_id_present;
164 uint32_t call_id;
165
166 const uint8_t *global_call_reference;
167 uint8_t global_call_reference_len;
168};
169
170struct ran_handover_request_ack {
171 const uint8_t *rr_ho_command;
172 uint8_t rr_ho_command_len;
173 bool chosen_channel_present;
174 uint8_t chosen_channel;
175 /*! chosen_encr_alg is in encoded format:
176 * chosen_encr_alg == 1 means A5/0 i.e. no encryption, chosen_encr_alg == 4 means A5/3.
177 * chosen_encr_alg == 0 means no such IE was present. */
178 uint8_t chosen_encr_alg;
179
180 /* chosen_speech_version == 0 means "not present" */
181 enum gsm0808_permitted_speech chosen_speech_version;
182
183 struct osmo_sockaddr_str remote_rtp;
184 bool codec_present;
Neels Hofmeyr7934e0d2022-10-31 18:13:47 +0100185 struct gsm0808_speech_codec codec;
186 bool codec_with_iuup;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100187};
188
189struct ran_handover_command {
190 const uint8_t *rr_ho_command;
191 uint8_t rr_ho_command_len;
192
193 const uint8_t *new_bss_to_old_bss_info_raw;
194 uint8_t new_bss_to_old_bss_info_raw_len;
195};
196
197struct ran_handover_required {
198 uint16_t cause;
199 struct gsm0808_cell_id_list2 cil;
200
201 bool current_channel_type_1_present;
202 /*! See gsm0808_chosen_channel() */
203 uint8_t current_channel_type_1;
204
205 enum gsm0808_permitted_speech speech_version_used;
206
207 uint8_t *old_bss_to_new_bss_info_raw;
208 size_t old_bss_to_new_bss_info_raw_len;
209};
210
211struct ran_msg {
212 enum ran_msg_type msg_type;
213
214 /* Since different RAN implementations feed these messages, they should place here an implementation specific
215 * string constant to name the actual message (e.g. "BSSMAP Assignment Complete" vs. "RANAP RAB Assignment
216 * Response") */
217 const char *msg_name;
218
219 union {
220 struct {
221 const struct gsm0808_cell_id *cell_id;
Neels Hofmeyr8a50cfb2019-10-21 03:01:00 +0200222 const struct gsm0808_speech_codec_list *codec_list_bss_supported;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100223 struct msgb *msg;
224 } compl_l3;
225 struct msgb *dtap;
226 struct {
227 enum gsm0808_cause bssap_cause;
228#define RAN_MSG_BSSAP_CAUSE_UNSET 0xffff
229 } clear_request;
230 struct ran_clear_command clear_command;
231 struct {
232 const struct osmo_gsm48_classmark *classmark;
233 } classmark_update;
234 struct ran_cipher_mode_command cipher_mode_command;
235 struct {
236 /*! alg_id is in encoded format:
237 * alg_id == 1 means A5/0 i.e. no encryption, alg_id == 4 means A5/3.
238 * alg_id == 0 means no such IE was present. */
239 uint8_t alg_id;
Alexander Couzens2aaff752021-10-19 17:09:11 +0200240 /*! utran integrity protection. 0..15 */
241 int16_t utran_integrity;
242 /*! utran_integrity is in encoded format:
243 * utran_integrity == -1 means no such IE was present
244 * utran_integrity == 0 means no encryption. */
245 int16_t utran_encryption;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100246 const char *imeisv;
Neels Hofmeyre9a39112019-08-29 00:10:49 +0200247 const struct tlv_p_entry *l3_msg;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100248 } cipher_mode_complete;
249 struct {
250 enum gsm0808_cause bssap_cause;
251 } cipher_mode_reject;
252 struct {
253 const char *imsi;
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200254 bool last_eutran_plmn_present;
255 struct osmo_plmn_id last_eutran_plmn;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100256 } common_id;
257 struct {
258 enum gsm48_reject_value cause;
259 } cm_service_reject;
260 struct ran_assignment_command assignment_command;
261 struct {
262 struct osmo_sockaddr_str remote_rtp;
263 bool codec_present;
Neels Hofmeyrcec51b32023-03-01 03:47:45 +0100264 struct gsm0808_speech_codec codec;
265 bool codec_with_iuup;
Neels Hofmeyr8a50cfb2019-10-21 03:01:00 +0200266 const struct gsm0808_speech_codec_list *codec_list_bss_supported;
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200267 bool osmux_present;
268 uint8_t osmux_cid;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100269 } assignment_complete;
270 struct {
271 enum gsm0808_cause bssap_cause;
272 uint8_t rr_cause;
273 const struct gsm0808_speech_codec_list *scl_bss_supported;
274 } assignment_failure;
275 struct {
276 enum gsm0808_cause bssap_cause;
277 uint8_t dlci;
278 } sapi_n_reject;
279 struct {
280 enum gsm0808_lcls_status status;
281 } lcls_status;
282 struct {
283 int todo;
284 } lcls_break_req;
285 struct ran_handover_required handover_required;
286 struct gsm0808_handover_required_reject handover_required_reject;
287 struct ran_handover_command handover_command;
288 struct {
289 enum gsm0808_cause cause;
290 } handover_failure;
291 struct ran_handover_request handover_request;
292 struct ran_handover_request_ack handover_request_ack;
Andreas Eversberg2d27e2c2023-04-23 12:05:44 +0200293 struct gsm0808_vgcs_vbs_setup vgcs_vbs_setup;
294 struct gsm0808_vgcs_vbs_setup_ack vgcs_vbs_setup_ack;
295 struct {
296 enum gsm0808_cause cause;
297 } vgcs_vbs_setup_refuse;
298 struct gsm0808_vgcs_vbs_assign_req vgcs_vbs_assign_req;
299 struct gsm0808_vgcs_vbs_assign_res vgcs_vbs_assign_res;
300 struct gsm0808_vgcs_vbs_assign_fail vgcs_vbs_assign_fail;
301 struct gsm0808_uplink_request uplink_request;
302 struct gsm0808_uplink_request_ack uplink_request_ack;
303 struct gsm0808_uplink_request_cnf uplink_request_cnf;
304 struct gsm0808_uplink_app_data uplink_app_data;
305 struct gsm0808_uplink_release_ind uplink_release_ind;
306 struct gsm0808_uplink_seized_cmd uplink_seized_cmd;
307 struct gsm0808_uplink_reject_cmd uplink_reject_cmd;
308 struct {
309 enum gsm0808_cause cause;
310 } uplink_release_cmd;
311 struct {
312 struct gsm0808_talker_identity talker_identity;
313 } vgcs_additional_info;
314 struct gsm0808_vgcs_vbs_area_cell_info vgcs_vbs_area_cell_info;
315 struct gsm0808_vgcs_vbs_assign_stat vgcs_vbs_assign_stat;
316 struct {
317 struct gsm0808_sms_to_vgcs sms_to_vgcs;
318 } vgcs_sms;
319 struct gsm0808_notification_data notification_data;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100320 };
321};
322
323/* 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
324 * RAN type implementation. */
325typedef int (* ran_decode_cb_t )(struct osmo_fsm_inst *caller_fi, void *caller_data, const struct ran_msg *msg);
326
327struct ran_dec {
328 /* caller provided osmo_fsm_inst, used both for logging from within decoding of NAS events, as well as caller's
329 * context in decode_cb(). */
330 struct osmo_fsm_inst *caller_fi;
331 void *caller_data;
332
333 /* Callback receives the decoded NAS messages */
334 ran_decode_cb_t decode_cb;
335};
336
337/* NAS decoders (BSSAP/RANAP) implement this to turn a msgb into a struct ran_msg.
338 * An implementation typically calls ran_decoded() when done decoding.
339 * NAS decoding is modeled with a callback instead of a plain decoding, because some L2 messages by design contain more
340 * than one NAS event, e.g. Ciphering Mode Complete may include another L3 message for Identity Response, and LCLS
341 * Information messages can contain Status and Break Req events. */
342typedef int (* ran_dec_l2_t )(struct ran_dec *ran_dec, struct msgb *l2);
343
344int ran_decoded(struct ran_dec *ran_dec, struct ran_msg *msg);
345
346/* An MSC-A/I/T role that receives NAS events containing DTAP buffers may use this to detect DTAP duplicates as in TS
347 * 24.007 11.2.3.2 Message Type Octet / Duplicate Detection */
348bool ran_dec_dtap_undup_is_duplicate(struct osmo_fsm_inst *log_fi, uint8_t *n_sd_next, bool is_r99, struct msgb *l3);
349
350/* Implemented by individual RAN implementations, see ran_a_encode() and ran_iu_encode(). */
351typedef struct msgb *(* ran_encode_t )(struct osmo_fsm_inst *caller_fi, const struct ran_msg *ran_enc_msg);