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