blob: 7e2ae42fb847af15d7e54204bb1c83c31387cf2d [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>
13
14#define GBPROXY_INIT_VU_GEN_TX 256
15
16struct rate_ctr_group;
17struct gprs_gb_parse_context;
18struct tlv_parsed;
19
20enum gbproxy_global_ctr {
21 GBPROX_GLOB_CTR_INV_BVCI,
22 GBPROX_GLOB_CTR_INV_LAI,
23 GBPROX_GLOB_CTR_INV_RAI,
24 GBPROX_GLOB_CTR_INV_NSEI,
25 GBPROX_GLOB_CTR_PROTO_ERR_BSS,
26 GBPROX_GLOB_CTR_PROTO_ERR_SGSN,
27 GBPROX_GLOB_CTR_NOT_SUPPORTED_BSS,
28 GBPROX_GLOB_CTR_NOT_SUPPORTED_SGSN,
29 GBPROX_GLOB_CTR_RESTART_RESET_SGSN,
30 GBPROX_GLOB_CTR_TX_ERR_SGSN,
31 GBPROX_GLOB_CTR_OTHER_ERR,
32 GBPROX_GLOB_CTR_PATCH_PEER_ERR,
33};
34
35enum gbproxy_peer_ctr {
36 GBPROX_PEER_CTR_BLOCKED,
37 GBPROX_PEER_CTR_UNBLOCKED,
38 GBPROX_PEER_CTR_DROPPED,
39 GBPROX_PEER_CTR_INV_NSEI,
40 GBPROX_PEER_CTR_TX_ERR,
41 GBPROX_PEER_CTR_RAID_PATCHED_BSS,
42 GBPROX_PEER_CTR_RAID_PATCHED_SGSN,
43 GBPROX_PEER_CTR_APN_PATCHED,
44 GBPROX_PEER_CTR_TLLI_PATCHED_BSS,
45 GBPROX_PEER_CTR_TLLI_PATCHED_SGSN,
46 GBPROX_PEER_CTR_PTMSI_PATCHED_BSS,
47 GBPROX_PEER_CTR_PTMSI_PATCHED_SGSN,
48 GBPROX_PEER_CTR_PATCH_CRYPT_ERR,
49 GBPROX_PEER_CTR_PATCH_ERR,
50 GBPROX_PEER_CTR_ATTACH_REQS,
51 GBPROX_PEER_CTR_ATTACH_REJS,
52 GBPROX_PEER_CTR_ATTACH_ACKS,
53 GBPROX_PEER_CTR_ATTACH_COMPLS,
54 GBPROX_PEER_CTR_RA_UPD_REQS,
55 GBPROX_PEER_CTR_RA_UPD_REJS,
56 GBPROX_PEER_CTR_RA_UPD_ACKS,
57 GBPROX_PEER_CTR_RA_UPD_COMPLS,
58 GBPROX_PEER_CTR_GMM_STATUS_BSS,
59 GBPROX_PEER_CTR_GMM_STATUS_SGSN,
60 GBPROX_PEER_CTR_DETACH_REQS,
61 GBPROX_PEER_CTR_DETACH_ACKS,
62 GBPROX_PEER_CTR_PDP_ACT_REQS,
63 GBPROX_PEER_CTR_PDP_ACT_REJS,
64 GBPROX_PEER_CTR_PDP_ACT_ACKS,
65 GBPROX_PEER_CTR_PDP_DEACT_REQS,
66 GBPROX_PEER_CTR_PDP_DEACT_ACKS,
67 GBPROX_PEER_CTR_TLLI_UNKNOWN,
68 GBPROX_PEER_CTR_TLLI_CACHE_SIZE,
69 GBPROX_PEER_CTR_LAST,
70};
71
72enum gbproxy_keep_mode {
73 GBPROX_KEEP_NEVER,
74 GBPROX_KEEP_REATTACH,
75 GBPROX_KEEP_IDENTIFIED,
76 GBPROX_KEEP_ALWAYS,
77};
78
79enum gbproxy_match_id {
80 GBPROX_MATCH_PATCHING,
81 GBPROX_MATCH_ROUTING,
82 GBPROX_MATCH_LAST
83};
84
85struct gbproxy_match {
86 int enable;
87 char *re_str;
88 regex_t re_comp;
89};
90
91struct gbproxy_config {
92 /* parsed from config file */
93 uint16_t nsip_sgsn_nsei;
94
95 /* misc */
96 struct gprs_ns_inst *nsi;
97
98 /* Linked list of all Gb peers (except SGSN) */
99 struct llist_head bts_peers;
100
101 /* Counter */
102 struct rate_ctr_group *ctrg;
103
104 /* force mcc/mnc */
Neels Hofmeyr6179f0c2018-02-21 00:39:36 +0100105 struct osmo_plmn_id core_plmn;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200106 uint8_t* core_apn;
107 size_t core_apn_size;
Pau Espin Pedrole0d647f2018-08-17 13:13:27 +0200108 /* Frequency (sec) at which timer to clean stale links is fired (0 disabled) */
109 unsigned int clean_stale_timer_freq;
Pau Espin Pedrol02485242018-08-16 12:11:46 +0200110 /* If !0, Max age to consider a struct gbproxy_link_info as stale */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200111 int tlli_max_age;
Pau Espin Pedrol02485242018-08-16 12:11:46 +0200112 /* If !0, Max len of gbproxy_peer->list (list of struct gbproxy_link_info) */
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200113 int tlli_max_len;
Pau Espin Pedrol02485242018-08-16 12:11:46 +0200114 /* If !0, Max len of gbproxy_link_info->stored_msgs (list of msgb) */
115 uint32_t stored_msgs_max_len;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200116
117 /* Experimental config */
118 int patch_ptmsi;
119 int acquire_imsi;
120 int route_to_sgsn2;
121 uint16_t nsip_sgsn2_nsei;
122 enum gbproxy_keep_mode keep_link_infos;
123
124 /* IMSI checking/matching */
125 struct gbproxy_match matches[GBPROX_MATCH_LAST];
126};
127
128struct gbproxy_patch_state {
Neels Hofmeyr6179f0c2018-02-21 00:39:36 +0100129 struct osmo_plmn_id local_plmn;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200130
131 /* List of TLLIs for which patching is enabled */
132 struct llist_head logical_links;
133 int logical_link_count;
134};
135
136struct gbproxy_peer {
137 struct llist_head list;
138
139 /* point back to the config */
140 struct gbproxy_config *cfg;
141
142 /* NSEI of the peer entity */
143 uint16_t nsei;
144
145 /* BVCI used for Point-to-Point to this peer */
146 uint16_t bvci;
147 int blocked;
148
149 /* Routeing Area that this peer is part of (raw 04.08 encoding) */
150 uint8_t ra[6];
151
152 /* Counter */
153 struct rate_ctr_group *ctrg;
154
155 struct gbproxy_patch_state patch_state;
Pau Espin Pedrole0d647f2018-08-17 13:13:27 +0200156
157 /* Fired periodically to clean up stale links from list */
158 struct osmo_timer_list clean_stale_timer;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200159};
160
161struct gbproxy_tlli_state {
162 uint32_t current;
163 uint32_t assigned;
164 int bss_validated;
165 int net_validated;
166
167 uint32_t ptmsi;
168};
169
170struct gbproxy_link_info {
171 struct llist_head list;
172
173 struct gbproxy_tlli_state tlli;
174 struct gbproxy_tlli_state sgsn_tlli;
175 uint32_t sgsn_nsei;
176
177 time_t timestamp;
178 uint8_t *imsi;
179 size_t imsi_len;
180
181 int imsi_acq_pending;
182 struct llist_head stored_msgs;
Pau Espin Pedrol02485242018-08-16 12:11:46 +0200183 uint32_t stored_msgs_len;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200184 unsigned vu_gen_tx_bss;
185
186 int is_deregistered;
187
188 int is_matching[GBPROX_MATCH_LAST];
189};
190
191
192/* gb_proxy_vty .c */
193
194int gbproxy_vty_init(void);
195int gbproxy_parse_config(const char *config_file, struct gbproxy_config *cfg);
196
Daniel Willmann13404b72018-06-01 07:21:20 +0200197/* gb_proxy_ctrl.c */
198int gb_ctrl_cmds_install(void);
199
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200200
201/* gb_proxy.c */
202int gbproxy_init_config(struct gbproxy_config *cfg);
203
204/* Main input function for Gb proxy */
205int gbprox_rcvmsg(struct gbproxy_config *cfg, struct msgb *msg, uint16_t nsei, uint16_t ns_bvci, uint16_t nsvci);
206
207int gbprox_signal(unsigned int subsys, unsigned int signal,
208 void *handler_data, void *signal_data);
209
210/* Reset all persistent NS-VC's */
211int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi);
212
213void gbprox_reset(struct gbproxy_config *cfg);
214
215/* TLLI info handling */
216void gbproxy_delete_link_infos(struct gbproxy_peer *peer);
217struct gbproxy_link_info *gbproxy_update_link_state_ul(
218 struct gbproxy_peer *peer, time_t now,
219 struct gprs_gb_parse_context *parse_ctx);
220struct gbproxy_link_info *gbproxy_update_link_state_dl(
221 struct gbproxy_peer *peer, time_t now,
222 struct gprs_gb_parse_context *parse_ctx);
223int gbproxy_update_link_state_after(
224 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
225 time_t now, struct gprs_gb_parse_context *parse_ctx);
226int gbproxy_remove_stale_link_infos(struct gbproxy_peer *peer, time_t now);
227void gbproxy_delete_link_info(struct gbproxy_peer *peer,
228 struct gbproxy_link_info *link_info);
229void gbproxy_link_info_discard_messages(struct gbproxy_link_info *link_info);
230
231void gbproxy_attach_link_info(struct gbproxy_peer *peer, time_t now,
232 struct gbproxy_link_info *link_info);
233void gbproxy_update_link_info(struct gbproxy_link_info *link_info,
234 const uint8_t *imsi, size_t imsi_len);
235void gbproxy_detach_link_info(struct gbproxy_peer *peer,
236 struct gbproxy_link_info *link_info);
237struct gbproxy_link_info *gbproxy_link_info_alloc( struct gbproxy_peer *peer);
238
239struct gbproxy_link_info *gbproxy_link_info_by_tlli(
240 struct gbproxy_peer *peer, uint32_t tlli);
241struct gbproxy_link_info *gbproxy_link_info_by_imsi(
242 struct gbproxy_peer *peer, const uint8_t *imsi, size_t imsi_len);
243struct gbproxy_link_info *gbproxy_link_info_by_any_sgsn_tlli(
244 struct gbproxy_peer *peer, uint32_t tlli);
245struct gbproxy_link_info *gbproxy_link_info_by_sgsn_tlli(
246 struct gbproxy_peer *peer,
247 uint32_t tlli, uint32_t sgsn_nsei);
248struct gbproxy_link_info *gbproxy_link_info_by_ptmsi(
249 struct gbproxy_peer *peer,
250 uint32_t ptmsi);
251
252int gbproxy_imsi_matches(
253 struct gbproxy_config *cfg,
254 enum gbproxy_match_id match_id,
255 struct gbproxy_link_info *link_info);
256uint32_t gbproxy_map_tlli(
257 uint32_t other_tlli, struct gbproxy_link_info *link_info, int to_bss);
258
259/* needed by gb_proxy_tlli.h */
260uint32_t gbproxy_make_bss_ptmsi(struct gbproxy_peer *peer, uint32_t sgsn_ptmsi);
261uint32_t gbproxy_make_sgsn_tlli(
262 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
263 uint32_t bss_tlli);
264void gbproxy_reset_link(struct gbproxy_link_info *link_info);
265int gbproxy_check_imsi(
266 struct gbproxy_match *match, const uint8_t *imsi, size_t imsi_len);
267
268/* Message patching */
269void gbproxy_patch_bssgp(
270 struct msgb *msg, uint8_t *bssgp, size_t bssgp_len,
271 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
272 int *len_change, struct gprs_gb_parse_context *parse_ctx);
273
274int gbproxy_patch_llc(
275 struct msgb *msg, uint8_t *llc, size_t llc_len,
276 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
277 int *len_change, struct gprs_gb_parse_context *parse_ctx);
278
279int gbproxy_set_patch_filter(
280 struct gbproxy_match *match, const char *filter, const char **err_msg);
281void gbproxy_clear_patch_filter(struct gbproxy_match *match);
282
283/* Peer handling */
284struct gbproxy_peer *gbproxy_peer_by_bvci(
285 struct gbproxy_config *cfg, uint16_t bvci);
286struct gbproxy_peer *gbproxy_peer_by_nsei(
287 struct gbproxy_config *cfg, uint16_t nsei);
288struct gbproxy_peer *gbproxy_peer_by_rai(
289 struct gbproxy_config *cfg, const uint8_t *ra);
290struct gbproxy_peer *gbproxy_peer_by_lai(
291 struct gbproxy_config *cfg, const uint8_t *la);
292struct gbproxy_peer *gbproxy_peer_by_lac(
293 struct gbproxy_config *cfg, const uint8_t *la);
294struct gbproxy_peer *gbproxy_peer_by_bssgp_tlv(
295 struct gbproxy_config *cfg, struct tlv_parsed *tp);
296struct gbproxy_peer *gbproxy_peer_alloc(struct gbproxy_config *cfg, uint16_t bvci);
297void gbproxy_peer_free(struct gbproxy_peer *peer);
298int gbproxy_cleanup_peers(struct gbproxy_config *cfg, uint16_t nsei, uint16_t bvci);
299
300#endif