blob: 70de3d7152ac955f4c98120cbb3e00bee1fde2db [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;
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 {
Neels Hofmeyr6179f0c2018-02-21 00:39:36 +0100123 struct osmo_plmn_id local_plmn;
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200124
125 /* List of TLLIs for which patching is enabled */
126 struct llist_head logical_links;
127 int logical_link_count;
128};
129
130struct gbproxy_peer {
131 struct llist_head list;
132
133 /* point back to the config */
134 struct gbproxy_config *cfg;
135
136 /* NSEI of the peer entity */
137 uint16_t nsei;
138
139 /* BVCI used for Point-to-Point to this peer */
140 uint16_t bvci;
141 int blocked;
142
143 /* Routeing Area that this peer is part of (raw 04.08 encoding) */
144 uint8_t ra[6];
145
146 /* Counter */
147 struct rate_ctr_group *ctrg;
148
149 struct gbproxy_patch_state patch_state;
150};
151
152struct gbproxy_tlli_state {
153 uint32_t current;
154 uint32_t assigned;
155 int bss_validated;
156 int net_validated;
157
158 uint32_t ptmsi;
159};
160
161struct gbproxy_link_info {
162 struct llist_head list;
163
164 struct gbproxy_tlli_state tlli;
165 struct gbproxy_tlli_state sgsn_tlli;
166 uint32_t sgsn_nsei;
167
168 time_t timestamp;
169 uint8_t *imsi;
170 size_t imsi_len;
171
172 int imsi_acq_pending;
173 struct llist_head stored_msgs;
174 unsigned vu_gen_tx_bss;
175
176 int is_deregistered;
177
178 int is_matching[GBPROX_MATCH_LAST];
179};
180
181
182/* gb_proxy_vty .c */
183
184int gbproxy_vty_init(void);
185int gbproxy_parse_config(const char *config_file, struct gbproxy_config *cfg);
186
Daniel Willmann13404b72018-06-01 07:21:20 +0200187/* gb_proxy_ctrl.c */
188int gb_ctrl_cmds_install(void);
189
Neels Hofmeyr4b4c5862017-09-04 15:13:25 +0200190
191/* gb_proxy.c */
192int gbproxy_init_config(struct gbproxy_config *cfg);
193
194/* Main input function for Gb proxy */
195int gbprox_rcvmsg(struct gbproxy_config *cfg, struct msgb *msg, uint16_t nsei, uint16_t ns_bvci, uint16_t nsvci);
196
197int gbprox_signal(unsigned int subsys, unsigned int signal,
198 void *handler_data, void *signal_data);
199
200/* Reset all persistent NS-VC's */
201int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi);
202
203void gbprox_reset(struct gbproxy_config *cfg);
204
205/* TLLI info handling */
206void gbproxy_delete_link_infos(struct gbproxy_peer *peer);
207struct gbproxy_link_info *gbproxy_update_link_state_ul(
208 struct gbproxy_peer *peer, time_t now,
209 struct gprs_gb_parse_context *parse_ctx);
210struct gbproxy_link_info *gbproxy_update_link_state_dl(
211 struct gbproxy_peer *peer, time_t now,
212 struct gprs_gb_parse_context *parse_ctx);
213int gbproxy_update_link_state_after(
214 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
215 time_t now, struct gprs_gb_parse_context *parse_ctx);
216int gbproxy_remove_stale_link_infos(struct gbproxy_peer *peer, time_t now);
217void gbproxy_delete_link_info(struct gbproxy_peer *peer,
218 struct gbproxy_link_info *link_info);
219void gbproxy_link_info_discard_messages(struct gbproxy_link_info *link_info);
220
221void gbproxy_attach_link_info(struct gbproxy_peer *peer, time_t now,
222 struct gbproxy_link_info *link_info);
223void gbproxy_update_link_info(struct gbproxy_link_info *link_info,
224 const uint8_t *imsi, size_t imsi_len);
225void gbproxy_detach_link_info(struct gbproxy_peer *peer,
226 struct gbproxy_link_info *link_info);
227struct gbproxy_link_info *gbproxy_link_info_alloc( struct gbproxy_peer *peer);
228
229struct gbproxy_link_info *gbproxy_link_info_by_tlli(
230 struct gbproxy_peer *peer, uint32_t tlli);
231struct gbproxy_link_info *gbproxy_link_info_by_imsi(
232 struct gbproxy_peer *peer, const uint8_t *imsi, size_t imsi_len);
233struct gbproxy_link_info *gbproxy_link_info_by_any_sgsn_tlli(
234 struct gbproxy_peer *peer, uint32_t tlli);
235struct gbproxy_link_info *gbproxy_link_info_by_sgsn_tlli(
236 struct gbproxy_peer *peer,
237 uint32_t tlli, uint32_t sgsn_nsei);
238struct gbproxy_link_info *gbproxy_link_info_by_ptmsi(
239 struct gbproxy_peer *peer,
240 uint32_t ptmsi);
241
242int gbproxy_imsi_matches(
243 struct gbproxy_config *cfg,
244 enum gbproxy_match_id match_id,
245 struct gbproxy_link_info *link_info);
246uint32_t gbproxy_map_tlli(
247 uint32_t other_tlli, struct gbproxy_link_info *link_info, int to_bss);
248
249/* needed by gb_proxy_tlli.h */
250uint32_t gbproxy_make_bss_ptmsi(struct gbproxy_peer *peer, uint32_t sgsn_ptmsi);
251uint32_t gbproxy_make_sgsn_tlli(
252 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
253 uint32_t bss_tlli);
254void gbproxy_reset_link(struct gbproxy_link_info *link_info);
255int gbproxy_check_imsi(
256 struct gbproxy_match *match, const uint8_t *imsi, size_t imsi_len);
257
258/* Message patching */
259void gbproxy_patch_bssgp(
260 struct msgb *msg, uint8_t *bssgp, size_t bssgp_len,
261 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
262 int *len_change, struct gprs_gb_parse_context *parse_ctx);
263
264int gbproxy_patch_llc(
265 struct msgb *msg, uint8_t *llc, size_t llc_len,
266 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
267 int *len_change, struct gprs_gb_parse_context *parse_ctx);
268
269int gbproxy_set_patch_filter(
270 struct gbproxy_match *match, const char *filter, const char **err_msg);
271void gbproxy_clear_patch_filter(struct gbproxy_match *match);
272
273/* Peer handling */
274struct gbproxy_peer *gbproxy_peer_by_bvci(
275 struct gbproxy_config *cfg, uint16_t bvci);
276struct gbproxy_peer *gbproxy_peer_by_nsei(
277 struct gbproxy_config *cfg, uint16_t nsei);
278struct gbproxy_peer *gbproxy_peer_by_rai(
279 struct gbproxy_config *cfg, const uint8_t *ra);
280struct gbproxy_peer *gbproxy_peer_by_lai(
281 struct gbproxy_config *cfg, const uint8_t *la);
282struct gbproxy_peer *gbproxy_peer_by_lac(
283 struct gbproxy_config *cfg, const uint8_t *la);
284struct gbproxy_peer *gbproxy_peer_by_bssgp_tlv(
285 struct gbproxy_config *cfg, struct tlv_parsed *tp);
286struct gbproxy_peer *gbproxy_peer_alloc(struct gbproxy_config *cfg, uint16_t bvci);
287void gbproxy_peer_free(struct gbproxy_peer *peer);
288int gbproxy_cleanup_peers(struct gbproxy_config *cfg, uint16_t nsei, uint16_t bvci);
289
290#endif