blob: 4466f155ad5c05fbc1684e797318ddf90f8c91bf [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>
Alexander Couzens951e1332020-09-22 13:21:46 +02006#include <osmocom/core/timer.h>
Neels Hofmeyr6179f0c2018-02-21 00:39:36 +01007#include <osmocom/gsm/gsm23003.h>
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +02008
Alexander Couzens951e1332020-09-22 13:21:46 +02009#include <osmocom/gprs/gprs_ns2.h>
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020010#include <osmocom/vty/command.h>
11
12#include <sys/types.h>
13#include <regex.h>
Harald Weltebec70412019-03-22 09:44:42 +010014#include <stdbool.h>
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020015
16#define GBPROXY_INIT_VU_GEN_TX 256
17
18struct rate_ctr_group;
19struct gprs_gb_parse_context;
20struct tlv_parsed;
21
22enum gbproxy_global_ctr {
23 GBPROX_GLOB_CTR_INV_BVCI,
24 GBPROX_GLOB_CTR_INV_LAI,
25 GBPROX_GLOB_CTR_INV_RAI,
26 GBPROX_GLOB_CTR_INV_NSEI,
27 GBPROX_GLOB_CTR_PROTO_ERR_BSS,
28 GBPROX_GLOB_CTR_PROTO_ERR_SGSN,
29 GBPROX_GLOB_CTR_NOT_SUPPORTED_BSS,
30 GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN,
31 GBPROX_GLOB_CTR_RESTART_RESET_SGSN,
32 GBPROX_GLOB_CTR_TX_ERR_SGSN,
33 GBPROX_GLOB_CTR_OTHER_ERR,
34 GBPROX_GLOB_CTR_PATCH_PEER_ERR,
35};
36
37enum gbproxy_peer_ctr {
38 GBPROX_PEER_CTR_BLOCKED,
39 GBPROX_PEER_CTR_UNBLOCKED,
40 GBPROX_PEER_CTR_DROPPED,
41 GBPROX_PEER_CTR_INV_NSEI,
42 GBPROX_PEER_CTR_TX_ERR,
43 GBPROX_PEER_CTR_RAID_PATCHED_BSS,
44 GBPROX_PEER_CTR_RAID_PATCHED_SGSN,
45 GBPROX_PEER_CTR_APN_PATCHED,
46 GBPROX_PEER_CTR_TLLI_PATCHED_BSS,
47 GBPROX_PEER_CTR_TLLI_PATCHED_SGSN,
48 GBPROX_PEER_CTR_PTMSI_PATCHED_BSS,
49 GBPROX_PEER_CTR_PTMSI_PATCHED_SGSN,
50 GBPROX_PEER_CTR_PATCH_CRYPT_ERR,
51 GBPROX_PEER_CTR_PATCH_ERR,
52 GBPROX_PEER_CTR_ATTACH_REQS,
53 GBPROX_PEER_CTR_ATTACH_REJS,
54 GBPROX_PEER_CTR_ATTACH_ACKS,
55 GBPROX_PEER_CTR_ATTACH_COMPLS,
56 GBPROX_PEER_CTR_RA_UPD_REQS,
57 GBPROX_PEER_CTR_RA_UPD_REJS,
58 GBPROX_PEER_CTR_RA_UPD_ACKS,
59 GBPROX_PEER_CTR_RA_UPD_COMPLS,
60 GBPROX_PEER_CTR_GMM_STATUS_BSS,
61 GBPROX_PEER_CTR_GMM_STATUS_SGSN,
62 GBPROX_PEER_CTR_DETACH_REQS,
63 GBPROX_PEER_CTR_DETACH_ACKS,
64 GBPROX_PEER_CTR_PDP_ACT_REQS,
65 GBPROX_PEER_CTR_PDP_ACT_REJS,
66 GBPROX_PEER_CTR_PDP_ACT_ACKS,
67 GBPROX_PEER_CTR_PDP_DEACT_REQS,
68 GBPROX_PEER_CTR_PDP_DEACT_ACKS,
69 GBPROX_PEER_CTR_TLLI_UNKNOWN,
70 GBPROX_PEER_CTR_TLLI_CACHE_SIZE,
71 GBPROX_PEER_CTR_LAST,
72};
73
74enum gbproxy_keep_mode {
Harald Welte4a8769a2019-03-22 08:26:45 +010075 GBPROX_KEEP_NEVER, /* don't ever keep TLLI/IMSI state of de-registered subscribers */
76 GBPROX_KEEP_REATTACH, /* keep if re-attach has been requested by SGSN */
77 GBPROX_KEEP_IDENTIFIED, /* keep if we had resolved an IMSI */
78 GBPROX_KEEP_ALWAYS, /* always keep */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020079};
80
81enum gbproxy_match_id {
Harald Welte4a8769a2019-03-22 08:26:45 +010082 GBPROX_MATCH_PATCHING, /* match rule on whether or not we should patch */
83 GBPROX_MATCH_ROUTING, /* match rule on whether or not we should route (2-SGSN) */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020084 GBPROX_MATCH_LAST
85};
86
87struct gbproxy_match {
Harald Weltebec70412019-03-22 09:44:42 +010088 bool enable; /* is this match enabled? */
Harald Welte4a8769a2019-03-22 08:26:45 +010089 char *re_str; /* regular expression (for IMSI) in string format */
90 regex_t re_comp; /* compiled regular expression (for IMSI) */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020091};
92
Harald Welte4a8769a2019-03-22 08:26:45 +010093/* global gb-proxy configuration */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +020094struct gbproxy_config {
95 /* parsed from config file */
96 uint16_t nsip_sgsn_nsei;
97
Harald Welte4a8769a2019-03-22 08:26:45 +010098 /* NS instance of libosmogb */
Alexander Couzens951e1332020-09-22 13:21:46 +020099 struct gprs_ns2_inst *nsi;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200100
101 /* Linked list of all Gb peers (except SGSN) */
102 struct llist_head bts_peers;
103
104 /* Counter */
105 struct rate_ctr_group *ctrg;
106
Harald Welte4a8769a2019-03-22 08:26:45 +0100107 /* MCC/MNC to be patched into RA-ID on the way from BSS to SGSN? */
Neels Hofmeyr6179f0c2018-02-21 00:39:36 +0100108 struct osmo_plmn_id core_plmn;
Harald Welte4a8769a2019-03-22 08:26:45 +0100109
110 /* APN to be patched into PDP CTX ACT REQ on the way from BSS to SGSN */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200111 uint8_t* core_apn;
112 size_t core_apn_size;
Harald Welte4a8769a2019-03-22 08:26:45 +0100113
Pau Espin Pedrole0d647f2018-08-17 13:13:27 +0200114 /* Frequency (sec) at which timer to clean stale links is fired (0 disabled) */
115 unsigned int clean_stale_timer_freq;
Pau Espin Pedrol02485242018-08-16 12:11:46 +0200116 /* If !0, Max age to consider a struct gbproxy_link_info as stale */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200117 int tlli_max_age;
Pau Espin Pedrol02485242018-08-16 12:11:46 +0200118 /* If !0, Max len of gbproxy_peer->list (list of struct gbproxy_link_info) */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200119 int tlli_max_len;
Pau Espin Pedrol02485242018-08-16 12:11:46 +0200120 /* If !0, Max len of gbproxy_link_info->stored_msgs (list of msgb) */
121 uint32_t stored_msgs_max_len;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200122
Harald Welte4a8769a2019-03-22 08:26:45 +0100123 /* Should the P-TMSI be patched on the fly (required for 2-SGSN config) */
Harald Weltebec70412019-03-22 09:44:42 +0100124 bool patch_ptmsi;
Harald Welte4a8769a2019-03-22 08:26:45 +0100125 /* Should the IMSI be acquired by the proxy (required for 2-SGSN config) */
Harald Weltebec70412019-03-22 09:44:42 +0100126 bool acquire_imsi;
Harald Welte4a8769a2019-03-22 08:26:45 +0100127 /* Should we route subscribers to two different SGSNs? */
Harald Weltebec70412019-03-22 09:44:42 +0100128 bool route_to_sgsn2;
Harald Welte4a8769a2019-03-22 08:26:45 +0100129 /* NSEI of the second SGSN */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200130 uint16_t nsip_sgsn2_nsei;
Harald Welte4a8769a2019-03-22 08:26:45 +0100131 /* should we keep a cache of per-subscriber state even after de-registration? */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200132 enum gbproxy_keep_mode keep_link_infos;
133
Harald Welte4a8769a2019-03-22 08:26:45 +0100134 /* IMSI checking/matching for 2-SGSN routing and patching */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200135 struct gbproxy_match matches[GBPROX_MATCH_LAST];
136};
137
138struct gbproxy_patch_state {
Neels Hofmeyr6179f0c2018-02-21 00:39:36 +0100139 struct osmo_plmn_id local_plmn;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200140
141 /* List of TLLIs for which patching is enabled */
142 struct llist_head logical_links;
143 int logical_link_count;
144};
145
Harald Welte4a8769a2019-03-22 08:26:45 +0100146/* one peer at NS level that we interact with (BSS/PCU) */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200147struct gbproxy_peer {
Harald Welte4a8769a2019-03-22 08:26:45 +0100148 /* linked to gbproxy_config.bts_peers */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200149 struct llist_head list;
150
151 /* point back to the config */
152 struct gbproxy_config *cfg;
153
154 /* NSEI of the peer entity */
155 uint16_t nsei;
156
157 /* BVCI used for Point-to-Point to this peer */
158 uint16_t bvci;
Harald Weltebec70412019-03-22 09:44:42 +0100159 bool blocked;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200160
161 /* Routeing Area that this peer is part of (raw 04.08 encoding) */
162 uint8_t ra[6];
163
164 /* Counter */
165 struct rate_ctr_group *ctrg;
166
Harald Welte4a8769a2019-03-22 08:26:45 +0100167 /* State related to on-the-fly patching of certain messages */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200168 struct gbproxy_patch_state patch_state;
Pau Espin Pedrole0d647f2018-08-17 13:13:27 +0200169
170 /* Fired periodically to clean up stale links from list */
171 struct osmo_timer_list clean_stale_timer;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200172};
173
174struct gbproxy_tlli_state {
Harald Welte4a8769a2019-03-22 08:26:45 +0100175 /* currently active TLLI */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200176 uint32_t current;
Harald Welte4a8769a2019-03-22 08:26:45 +0100177 /* newly-assigned TLLI (e.g. during P-TMSI allocation procedure) */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200178 uint32_t assigned;
Harald Welte4a8769a2019-03-22 08:26:45 +0100179 /* has the BSS side validated (confirmed) the new TLLI? */
Harald Weltebec70412019-03-22 09:44:42 +0100180 bool bss_validated;
Harald Welte4a8769a2019-03-22 08:26:45 +0100181 /* has the SGSN side validated (confirmed) the new TLLI? */
Harald Weltebec70412019-03-22 09:44:42 +0100182 bool net_validated;
Harald Welte4a8769a2019-03-22 08:26:45 +0100183 /* NOTE: once both are validated, we set current = assigned and assigned = 0 */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200184
Harald Welte4a8769a2019-03-22 08:26:45 +0100185 /* The P-TMSI for this subscriber */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200186 uint32_t ptmsi;
187};
188
Harald Welte4a8769a2019-03-22 08:26:45 +0100189/* One TLLI (= UE, = Subscriber) served via this proxy */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200190struct gbproxy_link_info {
Harald Welte4a8769a2019-03-22 08:26:45 +0100191 /* link to gbproxy_peer.patch_state.logical_links */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200192 struct llist_head list;
193
Harald Welte4a8769a2019-03-22 08:26:45 +0100194 /* TLLI on the BSS/PCU side */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200195 struct gbproxy_tlli_state tlli;
Harald Welte4a8769a2019-03-22 08:26:45 +0100196 /* TLLI on the SGSN side (can be different in case of P-TMSI patching) */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200197 struct gbproxy_tlli_state sgsn_tlli;
Harald Welte4a8769a2019-03-22 08:26:45 +0100198 /* NSEI of the SGSN serving this link */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200199 uint32_t sgsn_nsei;
200
Harald Welte4a8769a2019-03-22 08:26:45 +0100201 /* timestamp when we last had any contact with this UE */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200202 time_t timestamp;
Harald Welte4a8769a2019-03-22 08:26:45 +0100203
204 /* IMSI of the subscriber (if/once known) */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200205 uint8_t *imsi;
206 size_t imsi_len;
207
Harald Welte4a8769a2019-03-22 08:26:45 +0100208 /* is the IMSI acquisition still pending? */
Harald Weltebec70412019-03-22 09:44:42 +0100209 bool imsi_acq_pending;
Harald Welte4a8769a2019-03-22 08:26:45 +0100210
211 /* queue of stored UL messages (until IMSI acquisition completes and we can
212 * determine which of the SGSNs we should route this to */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200213 struct llist_head stored_msgs;
Pau Espin Pedrol02485242018-08-16 12:11:46 +0200214 uint32_t stored_msgs_len;
Harald Welte4a8769a2019-03-22 08:26:45 +0100215
216 /* generated N(U) we use (required due to IMSI acquisition */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200217 unsigned vu_gen_tx_bss;
218
Harald Welte4a8769a2019-03-22 08:26:45 +0100219 /* is this subscriber deregistered (TLLI invalidated)? */
Harald Weltebec70412019-03-22 09:44:42 +0100220 bool is_deregistered;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200221
Harald Welte4a8769a2019-03-22 08:26:45 +0100222 /* does this link match either the (2-SGSN) routing or the patching rule? */
Harald Weltebec70412019-03-22 09:44:42 +0100223 bool is_matching[GBPROX_MATCH_LAST];
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200224};
225
226
227/* gb_proxy_vty .c */
228
229int gbproxy_vty_init(void);
230int gbproxy_parse_config(const char *config_file, struct gbproxy_config *cfg);
231
Daniel Willmann13404b72018-06-01 07:21:20 +0200232/* gb_proxy_ctrl.c */
233int gb_ctrl_cmds_install(void);
234
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200235
236/* gb_proxy.c */
237int gbproxy_init_config(struct gbproxy_config *cfg);
238
239/* Main input function for Gb proxy */
Alexander Couzens951e1332020-09-22 13:21:46 +0200240int gbprox_rcvmsg(void *ctx, struct msgb *msg);
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200241
242int gbprox_signal(unsigned int subsys, unsigned int signal,
243 void *handler_data, void *signal_data);
244
Alexander Couzens951e1332020-09-22 13:21:46 +0200245
246int gprs_ns2_prim_cb(struct osmo_prim_hdr *oph, void *ctx);
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200247
248void gbprox_reset(struct gbproxy_config *cfg);
249
250/* TLLI info handling */
251void gbproxy_delete_link_infos(struct gbproxy_peer *peer);
252struct gbproxy_link_info *gbproxy_update_link_state_ul(
253 struct gbproxy_peer *peer, time_t now,
254 struct gprs_gb_parse_context *parse_ctx);
255struct gbproxy_link_info *gbproxy_update_link_state_dl(
256 struct gbproxy_peer *peer, time_t now,
257 struct gprs_gb_parse_context *parse_ctx);
258int gbproxy_update_link_state_after(
259 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
260 time_t now, struct gprs_gb_parse_context *parse_ctx);
261int gbproxy_remove_stale_link_infos(struct gbproxy_peer *peer, time_t now);
262void gbproxy_delete_link_info(struct gbproxy_peer *peer,
263 struct gbproxy_link_info *link_info);
264void gbproxy_link_info_discard_messages(struct gbproxy_link_info *link_info);
265
266void gbproxy_attach_link_info(struct gbproxy_peer *peer, time_t now,
267 struct gbproxy_link_info *link_info);
268void gbproxy_update_link_info(struct gbproxy_link_info *link_info,
269 const uint8_t *imsi, size_t imsi_len);
270void gbproxy_detach_link_info(struct gbproxy_peer *peer,
271 struct gbproxy_link_info *link_info);
272struct gbproxy_link_info *gbproxy_link_info_alloc( struct gbproxy_peer *peer);
273
274struct gbproxy_link_info *gbproxy_link_info_by_tlli(
275 struct gbproxy_peer *peer, uint32_t tlli);
276struct gbproxy_link_info *gbproxy_link_info_by_imsi(
277 struct gbproxy_peer *peer, const uint8_t *imsi, size_t imsi_len);
278struct gbproxy_link_info *gbproxy_link_info_by_any_sgsn_tlli(
279 struct gbproxy_peer *peer, uint32_t tlli);
280struct gbproxy_link_info *gbproxy_link_info_by_sgsn_tlli(
281 struct gbproxy_peer *peer,
282 uint32_t tlli, uint32_t sgsn_nsei);
283struct gbproxy_link_info *gbproxy_link_info_by_ptmsi(
284 struct gbproxy_peer *peer,
285 uint32_t ptmsi);
286
287int gbproxy_imsi_matches(
288 struct gbproxy_config *cfg,
289 enum gbproxy_match_id match_id,
290 struct gbproxy_link_info *link_info);
291uint32_t gbproxy_map_tlli(
292 uint32_t other_tlli, struct gbproxy_link_info *link_info, int to_bss);
293
294/* needed by gb_proxy_tlli.h */
295uint32_t gbproxy_make_bss_ptmsi(struct gbproxy_peer *peer, uint32_t sgsn_ptmsi);
296uint32_t gbproxy_make_sgsn_tlli(
297 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
298 uint32_t bss_tlli);
299void gbproxy_reset_link(struct gbproxy_link_info *link_info);
300int gbproxy_check_imsi(
301 struct gbproxy_match *match, const uint8_t *imsi, size_t imsi_len);
302
303/* Message patching */
304void gbproxy_patch_bssgp(
305 struct msgb *msg, uint8_t *bssgp, size_t bssgp_len,
306 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
307 int *len_change, struct gprs_gb_parse_context *parse_ctx);
308
309int gbproxy_patch_llc(
310 struct msgb *msg, uint8_t *llc, size_t llc_len,
311 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
312 int *len_change, struct gprs_gb_parse_context *parse_ctx);
313
314int gbproxy_set_patch_filter(
315 struct gbproxy_match *match, const char *filter, const char **err_msg);
316void gbproxy_clear_patch_filter(struct gbproxy_match *match);
317
318/* Peer handling */
319struct gbproxy_peer *gbproxy_peer_by_bvci(
320 struct gbproxy_config *cfg, uint16_t bvci);
321struct gbproxy_peer *gbproxy_peer_by_nsei(
322 struct gbproxy_config *cfg, uint16_t nsei);
323struct gbproxy_peer *gbproxy_peer_by_rai(
324 struct gbproxy_config *cfg, const uint8_t *ra);
325struct gbproxy_peer *gbproxy_peer_by_lai(
326 struct gbproxy_config *cfg, const uint8_t *la);
327struct gbproxy_peer *gbproxy_peer_by_lac(
328 struct gbproxy_config *cfg, const uint8_t *la);
329struct gbproxy_peer *gbproxy_peer_by_bssgp_tlv(
330 struct gbproxy_config *cfg, struct tlv_parsed *tp);
331struct gbproxy_peer *gbproxy_peer_alloc(struct gbproxy_config *cfg, uint16_t bvci);
332void gbproxy_peer_free(struct gbproxy_peer *peer);
333int gbproxy_cleanup_peers(struct gbproxy_config *cfg, uint16_t nsei, uint16_t bvci);
334
335#endif