blob: 2540a7e5b79e45d91e0d74c28873beca553d0945 [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
187
188/* gb_proxy.c */
189int gbproxy_init_config(struct gbproxy_config *cfg);
190
191/* Main input function for Gb proxy */
192int gbprox_rcvmsg(struct gbproxy_config *cfg, struct msgb *msg, uint16_t nsei, uint16_t ns_bvci, uint16_t nsvci);
193
194int gbprox_signal(unsigned int subsys, unsigned int signal,
195 void *handler_data, void *signal_data);
196
197/* Reset all persistent NS-VC's */
198int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi);
199
200void gbprox_reset(struct gbproxy_config *cfg);
201
202/* TLLI info handling */
203void gbproxy_delete_link_infos(struct gbproxy_peer *peer);
204struct gbproxy_link_info *gbproxy_update_link_state_ul(
205 struct gbproxy_peer *peer, time_t now,
206 struct gprs_gb_parse_context *parse_ctx);
207struct gbproxy_link_info *gbproxy_update_link_state_dl(
208 struct gbproxy_peer *peer, time_t now,
209 struct gprs_gb_parse_context *parse_ctx);
210int gbproxy_update_link_state_after(
211 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
212 time_t now, struct gprs_gb_parse_context *parse_ctx);
213int gbproxy_remove_stale_link_infos(struct gbproxy_peer *peer, time_t now);
214void gbproxy_delete_link_info(struct gbproxy_peer *peer,
215 struct gbproxy_link_info *link_info);
216void gbproxy_link_info_discard_messages(struct gbproxy_link_info *link_info);
217
218void gbproxy_attach_link_info(struct gbproxy_peer *peer, time_t now,
219 struct gbproxy_link_info *link_info);
220void gbproxy_update_link_info(struct gbproxy_link_info *link_info,
221 const uint8_t *imsi, size_t imsi_len);
222void gbproxy_detach_link_info(struct gbproxy_peer *peer,
223 struct gbproxy_link_info *link_info);
224struct gbproxy_link_info *gbproxy_link_info_alloc( struct gbproxy_peer *peer);
225
226struct gbproxy_link_info *gbproxy_link_info_by_tlli(
227 struct gbproxy_peer *peer, uint32_t tlli);
228struct gbproxy_link_info *gbproxy_link_info_by_imsi(
229 struct gbproxy_peer *peer, const uint8_t *imsi, size_t imsi_len);
230struct gbproxy_link_info *gbproxy_link_info_by_any_sgsn_tlli(
231 struct gbproxy_peer *peer, uint32_t tlli);
232struct gbproxy_link_info *gbproxy_link_info_by_sgsn_tlli(
233 struct gbproxy_peer *peer,
234 uint32_t tlli, uint32_t sgsn_nsei);
235struct gbproxy_link_info *gbproxy_link_info_by_ptmsi(
236 struct gbproxy_peer *peer,
237 uint32_t ptmsi);
238
239int gbproxy_imsi_matches(
240 struct gbproxy_config *cfg,
241 enum gbproxy_match_id match_id,
242 struct gbproxy_link_info *link_info);
243uint32_t gbproxy_map_tlli(
244 uint32_t other_tlli, struct gbproxy_link_info *link_info, int to_bss);
245
246/* needed by gb_proxy_tlli.h */
247uint32_t gbproxy_make_bss_ptmsi(struct gbproxy_peer *peer, uint32_t sgsn_ptmsi);
248uint32_t gbproxy_make_sgsn_tlli(
249 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
250 uint32_t bss_tlli);
251void gbproxy_reset_link(struct gbproxy_link_info *link_info);
252int gbproxy_check_imsi(
253 struct gbproxy_match *match, const uint8_t *imsi, size_t imsi_len);
254
255/* Message patching */
256void gbproxy_patch_bssgp(
257 struct msgb *msg, uint8_t *bssgp, size_t bssgp_len,
258 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
259 int *len_change, struct gprs_gb_parse_context *parse_ctx);
260
261int gbproxy_patch_llc(
262 struct msgb *msg, uint8_t *llc, size_t llc_len,
263 struct gbproxy_peer *peer, struct gbproxy_link_info *link_info,
264 int *len_change, struct gprs_gb_parse_context *parse_ctx);
265
266int gbproxy_set_patch_filter(
267 struct gbproxy_match *match, const char *filter, const char **err_msg);
268void gbproxy_clear_patch_filter(struct gbproxy_match *match);
269
270/* Peer handling */
271struct gbproxy_peer *gbproxy_peer_by_bvci(
272 struct gbproxy_config *cfg, uint16_t bvci);
273struct gbproxy_peer *gbproxy_peer_by_nsei(
274 struct gbproxy_config *cfg, uint16_t nsei);
275struct gbproxy_peer *gbproxy_peer_by_rai(
276 struct gbproxy_config *cfg, const uint8_t *ra);
277struct gbproxy_peer *gbproxy_peer_by_lai(
278 struct gbproxy_config *cfg, const uint8_t *la);
279struct gbproxy_peer *gbproxy_peer_by_lac(
280 struct gbproxy_config *cfg, const uint8_t *la);
281struct gbproxy_peer *gbproxy_peer_by_bssgp_tlv(
282 struct gbproxy_config *cfg, struct tlv_parsed *tp);
283struct gbproxy_peer *gbproxy_peer_alloc(struct gbproxy_config *cfg, uint16_t bvci);
284void gbproxy_peer_free(struct gbproxy_peer *peer);
285int gbproxy_cleanup_peers(struct gbproxy_config *cfg, uint16_t nsei, uint16_t bvci);
286
287#endif