blob: 1e8fb253cb1436cc8162930ae4c4cb4facc98d56 [file] [log] [blame]
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +02001#ifndef _GB_PROXY_H
2#define _GB_PROXY_H
3
4
5#include <osmocom/core/msgb.h>
Neels Hofmeyr6179f0c2018-02-21 00:39:36 +01006#include <osmocom/gsm/gsm23003.h>
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +02007
8#include <osmocom/gprs/gprs_ns.h>
9#include <osmocom/vty/command.h>
10
11#include <sys/types.h>
12#include <regex.h>
Harald Weltebec70412019-03-22 09:44:42 +010013#include <stdbool.h>
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020014
15#define GBPROXY_INIT_VU_GEN_TX 256
16
17struct rate_ctr_group;
18struct gprs_gb_parse_context;
19struct tlv_parsed;
20
21enum gbproxy_global_ctr {
22 GBPROX_GLOB_CTR_INV_BVCI,
23 GBPROX_GLOB_CTR_INV_LAI,
24 GBPROX_GLOB_CTR_INV_RAI,
25 GBPROX_GLOB_CTR_INV_NSEI,
26 GBPROX_GLOB_CTR_PROTO_ERR_BSS,
27 GBPROX_GLOB_CTR_PROTO_ERR_SGSN,
28 GBPROX_GLOB_CTR_NOT_SUPPORTED_BSS,
29 GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN,
30 GBPROX_GLOB_CTR_RESTART_RESET_SGSN,
31 GBPROX_GLOB_CTR_TX_ERR_SGSN,
32 GBPROX_GLOB_CTR_OTHER_ERR,
33 GBPROX_GLOB_CTR_PATCH_PEER_ERR,
34};
35
36enum gbproxy_peer_ctr {
37 GBPROX_PEER_CTR_BLOCKED,
38 GBPROX_PEER_CTR_UNBLOCKED,
39 GBPROX_PEER_CTR_DROPPED,
40 GBPROX_PEER_CTR_INV_NSEI,
41 GBPROX_PEER_CTR_TX_ERR,
42 GBPROX_PEER_CTR_RAID_PATCHED_BSS,
43 GBPROX_PEER_CTR_RAID_PATCHED_SGSN,
44 GBPROX_PEER_CTR_APN_PATCHED,
45 GBPROX_PEER_CTR_TLLI_PATCHED_BSS,
46 GBPROX_PEER_CTR_TLLI_PATCHED_SGSN,
47 GBPROX_PEER_CTR_PTMSI_PATCHED_BSS,
48 GBPROX_PEER_CTR_PTMSI_PATCHED_SGSN,
49 GBPROX_PEER_CTR_PATCH_CRYPT_ERR,
50 GBPROX_PEER_CTR_PATCH_ERR,
51 GBPROX_PEER_CTR_ATTACH_REQS,
52 GBPROX_PEER_CTR_ATTACH_REJS,
53 GBPROX_PEER_CTR_ATTACH_ACKS,
54 GBPROX_PEER_CTR_ATTACH_COMPLS,
55 GBPROX_PEER_CTR_RA_UPD_REQS,
56 GBPROX_PEER_CTR_RA_UPD_REJS,
57 GBPROX_PEER_CTR_RA_UPD_ACKS,
58 GBPROX_PEER_CTR_RA_UPD_COMPLS,
59 GBPROX_PEER_CTR_GMM_STATUS_BSS,
60 GBPROX_PEER_CTR_GMM_STATUS_SGSN,
61 GBPROX_PEER_CTR_DETACH_REQS,
62 GBPROX_PEER_CTR_DETACH_ACKS,
63 GBPROX_PEER_CTR_PDP_ACT_REQS,
64 GBPROX_PEER_CTR_PDP_ACT_REJS,
65 GBPROX_PEER_CTR_PDP_ACT_ACKS,
66 GBPROX_PEER_CTR_PDP_DEACT_REQS,
67 GBPROX_PEER_CTR_PDP_DEACT_ACKS,
68 GBPROX_PEER_CTR_TLLI_UNKNOWN,
69 GBPROX_PEER_CTR_TLLI_CACHE_SIZE,
70 GBPROX_PEER_CTR_LAST,
71};
72
73enum gbproxy_keep_mode {
Harald Welte4a8769a2019-03-22 08:26:45 +010074 GBPROX_KEEP_NEVER, /* don't ever keep TLLI/IMSI state of de-registered subscribers */
75 GBPROX_KEEP_REATTACH, /* keep if re-attach has been requested by SGSN */
76 GBPROX_KEEP_IDENTIFIED, /* keep if we had resolved an IMSI */
77 GBPROX_KEEP_ALWAYS, /* always keep */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020078};
79
80enum gbproxy_match_id {
Harald Welte4a8769a2019-03-22 08:26:45 +010081 GBPROX_MATCH_PATCHING, /* match rule on whether or not we should patch */
82 GBPROX_MATCH_ROUTING, /* match rule on whether or not we should route (2-SGSN) */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020083 GBPROX_MATCH_LAST
84};
85
86struct gbproxy_match {
Harald Weltebec70412019-03-22 09:44:42 +010087 bool enable; /* is this match enabled? */
Harald Welte4a8769a2019-03-22 08:26:45 +010088 char *re_str; /* regular expression (for IMSI) in string format */
89 regex_t re_comp; /* compiled regular expression (for IMSI) */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020090};
91
Harald Welte4a8769a2019-03-22 08:26:45 +010092/* global gb-proxy configuration */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020093struct gbproxy_config {
94 /* parsed from config file */
95 uint16_t nsip_sgsn_nsei;
96
Harald Welte4a8769a2019-03-22 08:26:45 +010097 /* NS instance of libosmogb */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020098 struct gprs_ns_inst *nsi;
99
100 /* Linked list of all Gb peers (except SGSN) */
101 struct llist_head bts_peers;
102
103 /* Counter */
104 struct rate_ctr_group *ctrg;
105
Harald Welte4a8769a2019-03-22 08:26:45 +0100106 /* MCC/MNC to be patched into RA-ID on the way from BSS to SGSN? */
Neels Hofmeyr6179f0c2018-02-21 00:39:36 +0100107 struct osmo_plmn_id core_plmn;
Harald Welte4a8769a2019-03-22 08:26:45 +0100108
109 /* APN to be patched into PDP CTX ACT REQ on the way from BSS to SGSN */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200110 uint8_t* core_apn;
111 size_t core_apn_size;
Harald Welte4a8769a2019-03-22 08:26:45 +0100112
Pau Espin Pedrole0d647f2018-08-17 13:13:27 +0200113 /* Frequency (sec) at which timer to clean stale links is fired (0 disabled) */
114 unsigned int clean_stale_timer_freq;
Pau Espin Pedrol02485242018-08-16 12:11:46 +0200115 /* If !0, Max age to consider a struct gbproxy_link_info as stale */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200116 int tlli_max_age;
Pau Espin Pedrol02485242018-08-16 12:11:46 +0200117 /* If !0, Max len of gbproxy_peer->list (list of struct gbproxy_link_info) */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200118 int tlli_max_len;
Pau Espin Pedrol02485242018-08-16 12:11:46 +0200119 /* If !0, Max len of gbproxy_link_info->stored_msgs (list of msgb) */
120 uint32_t stored_msgs_max_len;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200121
Harald Welte4a8769a2019-03-22 08:26:45 +0100122 /* Should the P-TMSI be patched on the fly (required for 2-SGSN config) */
Harald Weltebec70412019-03-22 09:44:42 +0100123 bool patch_ptmsi;
Harald Welte4a8769a2019-03-22 08:26:45 +0100124 /* Should the IMSI be acquired by the proxy (required for 2-SGSN config) */
Harald Weltebec70412019-03-22 09:44:42 +0100125 bool acquire_imsi;
Harald Welte4a8769a2019-03-22 08:26:45 +0100126 /* Should we route subscribers to two different SGSNs? */
Harald Weltebec70412019-03-22 09:44:42 +0100127 bool route_to_sgsn2;
Harald Welte4a8769a2019-03-22 08:26:45 +0100128 /* NSEI of the second SGSN */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200129 uint16_t nsip_sgsn2_nsei;
Harald Welte4a8769a2019-03-22 08:26:45 +0100130 /* should we keep a cache of per-subscriber state even after de-registration? */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200131 enum gbproxy_keep_mode keep_link_infos;
132
Harald Welte4a8769a2019-03-22 08:26:45 +0100133 /* IMSI checking/matching for 2-SGSN routing and patching */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200134 struct gbproxy_match matches[GBPROX_MATCH_LAST];
135};
136
137struct gbproxy_patch_state {
Neels Hofmeyr6179f0c2018-02-21 00:39:36 +0100138 struct osmo_plmn_id local_plmn;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200139
140 /* List of TLLIs for which patching is enabled */
141 struct llist_head logical_links;
142 int logical_link_count;
143};
144
Harald Welte4a8769a2019-03-22 08:26:45 +0100145/* one peer at NS level that we interact with (BSS/PCU) */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200146struct gbproxy_peer {
Harald Welte4a8769a2019-03-22 08:26:45 +0100147 /* linked to gbproxy_config.bts_peers */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200148 struct llist_head list;
149
150 /* point back to the config */
151 struct gbproxy_config *cfg;
152
153 /* NSEI of the peer entity */
154 uint16_t nsei;
155
156 /* BVCI used for Point-to-Point to this peer */
157 uint16_t bvci;
Harald Weltebec70412019-03-22 09:44:42 +0100158 bool blocked;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200159
160 /* Routeing Area that this peer is part of (raw 04.08 encoding) */
161 uint8_t ra[6];
162
163 /* Counter */
164 struct rate_ctr_group *ctrg;
165
Harald Welte4a8769a2019-03-22 08:26:45 +0100166 /* State related to on-the-fly patching of certain messages */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200167 struct gbproxy_patch_state patch_state;
Pau Espin Pedrole0d647f2018-08-17 13:13:27 +0200168
169 /* Fired periodically to clean up stale links from list */
170 struct osmo_timer_list clean_stale_timer;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200171};
172
173struct gbproxy_tlli_state {
Harald Welte4a8769a2019-03-22 08:26:45 +0100174 /* currently active TLLI */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200175 uint32_t current;
Harald Welte4a8769a2019-03-22 08:26:45 +0100176 /* newly-assigned TLLI (e.g. during P-TMSI allocation procedure) */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200177 uint32_t assigned;
Harald Welte4a8769a2019-03-22 08:26:45 +0100178 /* has the BSS side validated (confirmed) the new TLLI? */
Harald Weltebec70412019-03-22 09:44:42 +0100179 bool bss_validated;
Harald Welte4a8769a2019-03-22 08:26:45 +0100180 /* has the SGSN side validated (confirmed) the new TLLI? */
Harald Weltebec70412019-03-22 09:44:42 +0100181 bool net_validated;
Harald Welte4a8769a2019-03-22 08:26:45 +0100182 /* NOTE: once both are validated, we set current = assigned and assigned = 0 */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200183
Harald Welte4a8769a2019-03-22 08:26:45 +0100184 /* The P-TMSI for this subscriber */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200185 uint32_t ptmsi;
186};
187
Harald Welte4a8769a2019-03-22 08:26:45 +0100188/* One TLLI (= UE, = Subscriber) served via this proxy */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200189struct gbproxy_link_info {
Harald Welte4a8769a2019-03-22 08:26:45 +0100190 /* link to gbproxy_peer.patch_state.logical_links */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200191 struct llist_head list;
192
Harald Welte4a8769a2019-03-22 08:26:45 +0100193 /* TLLI on the BSS/PCU side */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200194 struct gbproxy_tlli_state tlli;
Harald Welte4a8769a2019-03-22 08:26:45 +0100195 /* TLLI on the SGSN side (can be different in case of P-TMSI patching) */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200196 struct gbproxy_tlli_state sgsn_tlli;
Harald Welte4a8769a2019-03-22 08:26:45 +0100197 /* NSEI of the SGSN serving this link */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200198 uint32_t sgsn_nsei;
199
Harald Welte4a8769a2019-03-22 08:26:45 +0100200 /* timestamp when we last had any contact with this UE */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200201 time_t timestamp;
Harald Welte4a8769a2019-03-22 08:26:45 +0100202
203 /* IMSI of the subscriber (if/once known) */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200204 uint8_t *imsi;
205 size_t imsi_len;
206
Harald Welte4a8769a2019-03-22 08:26:45 +0100207 /* is the IMSI acquisition still pending? */
Harald Weltebec70412019-03-22 09:44:42 +0100208 bool imsi_acq_pending;
Harald Welte4a8769a2019-03-22 08:26:45 +0100209
210 /* queue of stored UL messages (until IMSI acquisition completes and we can
211 * determine which of the SGSNs we should route this to */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200212 struct llist_head stored_msgs;
Pau Espin Pedrol02485242018-08-16 12:11:46 +0200213 uint32_t stored_msgs_len;
Harald Welte4a8769a2019-03-22 08:26:45 +0100214
215 /* generated N(U) we use (required due to IMSI acquisition */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200216 unsigned vu_gen_tx_bss;
217
Harald Welte4a8769a2019-03-22 08:26:45 +0100218 /* is this subscriber deregistered (TLLI invalidated)? */
Harald Weltebec70412019-03-22 09:44:42 +0100219 bool is_deregistered;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200220
Harald Welte4a8769a2019-03-22 08:26:45 +0100221 /* does this link match either the (2-SGSN) routing or the patching rule? */
Harald Weltebec70412019-03-22 09:44:42 +0100222 bool is_matching[GBPROX_MATCH_LAST];
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200223};
224
225
226/* gb_proxy_vty .c */
227
228int gbproxy_vty_init(void);
229int gbproxy_parse_config(const char *config_file, struct gbproxy_config *cfg);
230
Daniel Willmann13404b72018-06-01 07:21:20 +0200231/* gb_proxy_ctrl.c */
232int gb_ctrl_cmds_install(void);
233
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200234
235/* gb_proxy.c */
236int gbproxy_init_config(struct gbproxy_config *cfg);
237
238/* Main input function for Gb proxy */
239int gbprox_rcvmsg(struct gbproxy_config *cfg, struct msgb *msg, uint16_t nsei, uint16_t ns_bvci, uint16_t nsvci);
240
241int gbprox_signal(unsigned int subsys, unsigned int signal,
242 void *handler_data, void *signal_data);
243
244/* Reset all persistent NS-VC's */
245int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi);
246
247void gbprox_reset(struct gbproxy_config *cfg);
248
249/* TLLI info handling */
250void gbproxy_delete_link_infos(struct gbproxy_peer *peer);
251struct gbproxy_link_info *gbproxy_update_link_state_ul(
252 struct gbproxy_peer *peer, time_t now,
253 struct gprs_gb_parse_context *parse_ctx);
254struct gbproxy_link_info *gbproxy_update_link_state_dl(
255 struct gbproxy_peer *peer, time_t now,
256 struct gprs_gb_parse_context *parse_ctx);
257int gbproxy_update_link_state_after(
258 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
259 time_t now, struct gprs_gb_parse_context *parse_ctx);
260int gbproxy_remove_stale_link_infos(struct gbproxy_peer *peer, time_t now);
261void gbproxy_delete_link_info(struct gbproxy_peer *peer,
262 struct gbproxy_link_info *link_info);
263void gbproxy_link_info_discard_messages(struct gbproxy_link_info *link_info);
264
265void gbproxy_attach_link_info(struct gbproxy_peer *peer, time_t now,
266 struct gbproxy_link_info *link_info);
267void gbproxy_update_link_info(struct gbproxy_link_info *link_info,
268 const uint8_t *imsi, size_t imsi_len);
269void gbproxy_detach_link_info(struct gbproxy_peer *peer,
270 struct gbproxy_link_info *link_info);
271struct gbproxy_link_info *gbproxy_link_info_alloc( struct gbproxy_peer *peer);
272
273struct gbproxy_link_info *gbproxy_link_info_by_tlli(
274 struct gbproxy_peer *peer, uint32_t tlli);
275struct gbproxy_link_info *gbproxy_link_info_by_imsi(
276 struct gbproxy_peer *peer, const uint8_t *imsi, size_t imsi_len);
277struct gbproxy_link_info *gbproxy_link_info_by_any_sgsn_tlli(
278 struct gbproxy_peer *peer, uint32_t tlli);
279struct gbproxy_link_info *gbproxy_link_info_by_sgsn_tlli(
280 struct gbproxy_peer *peer,
281 uint32_t tlli, uint32_t sgsn_nsei);
282struct gbproxy_link_info *gbproxy_link_info_by_ptmsi(
283 struct gbproxy_peer *peer,
284 uint32_t ptmsi);
285
286int gbproxy_imsi_matches(
287 struct gbproxy_config *cfg,
288 enum gbproxy_match_id match_id,
289 struct gbproxy_link_info *link_info);
290uint32_t gbproxy_map_tlli(
291 uint32_t other_tlli, struct gbproxy_link_info *link_info, int to_bss);
292
293/* needed by gb_proxy_tlli.h */
294uint32_t gbproxy_make_bss_ptmsi(struct gbproxy_peer *peer, uint32_t sgsn_ptmsi);
295uint32_t gbproxy_make_sgsn_tlli(
296 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
297 uint32_t bss_tlli);
298void gbproxy_reset_link(struct gbproxy_link_info *link_info);
299int gbproxy_check_imsi(
300 struct gbproxy_match *match, const uint8_t *imsi, size_t imsi_len);
301
302/* Message patching */
303void gbproxy_patch_bssgp(
304 struct msgb *msg, uint8_t *bssgp, size_t bssgp_len,
305 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
306 int *len_change, struct gprs_gb_parse_context *parse_ctx);
307
308int gbproxy_patch_llc(
309 struct msgb *msg, uint8_t *llc, size_t llc_len,
310 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
311 int *len_change, struct gprs_gb_parse_context *parse_ctx);
312
313int gbproxy_set_patch_filter(
314 struct gbproxy_match *match, const char *filter, const char **err_msg);
315void gbproxy_clear_patch_filter(struct gbproxy_match *match);
316
317/* Peer handling */
318struct gbproxy_peer *gbproxy_peer_by_bvci(
319 struct gbproxy_config *cfg, uint16_t bvci);
320struct gbproxy_peer *gbproxy_peer_by_nsei(
321 struct gbproxy_config *cfg, uint16_t nsei);
322struct gbproxy_peer *gbproxy_peer_by_rai(
323 struct gbproxy_config *cfg, const uint8_t *ra);
324struct gbproxy_peer *gbproxy_peer_by_lai(
325 struct gbproxy_config *cfg, const uint8_t *la);
326struct gbproxy_peer *gbproxy_peer_by_lac(
327 struct gbproxy_config *cfg, const uint8_t *la);
328struct gbproxy_peer *gbproxy_peer_by_bssgp_tlv(
329 struct gbproxy_config *cfg, struct tlv_parsed *tp);
330struct gbproxy_peer *gbproxy_peer_alloc(struct gbproxy_config *cfg, uint16_t bvci);
331void gbproxy_peer_free(struct gbproxy_peer *peer);
332int gbproxy_cleanup_peers(struct gbproxy_config *cfg, uint16_t nsei, uint16_t bvci);
333
334#endif