Neels Hofmeyr | 4b4c586 | 2017-09-04 15:13:25 +0200 | [diff] [blame^] | 1 | #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 | |
| 15 | struct rate_ctr_group; |
| 16 | struct gprs_gb_parse_context; |
| 17 | struct tlv_parsed; |
| 18 | |
| 19 | enum 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 | |
| 34 | enum 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 | |
| 71 | enum gbproxy_keep_mode { |
| 72 | GBPROX_KEEP_NEVER, |
| 73 | GBPROX_KEEP_REATTACH, |
| 74 | GBPROX_KEEP_IDENTIFIED, |
| 75 | GBPROX_KEEP_ALWAYS, |
| 76 | }; |
| 77 | |
| 78 | enum gbproxy_match_id { |
| 79 | GBPROX_MATCH_PATCHING, |
| 80 | GBPROX_MATCH_ROUTING, |
| 81 | GBPROX_MATCH_LAST |
| 82 | }; |
| 83 | |
| 84 | struct gbproxy_match { |
| 85 | int enable; |
| 86 | char *re_str; |
| 87 | regex_t re_comp; |
| 88 | }; |
| 89 | |
| 90 | struct 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 | |
| 122 | struct 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 | |
| 131 | struct 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 | |
| 153 | struct 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 | |
| 162 | struct 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 | |
| 185 | int gbproxy_vty_init(void); |
| 186 | int gbproxy_parse_config(const char *config_file, struct gbproxy_config *cfg); |
| 187 | |
| 188 | |
| 189 | /* gb_proxy.c */ |
| 190 | int gbproxy_init_config(struct gbproxy_config *cfg); |
| 191 | |
| 192 | /* Main input function for Gb proxy */ |
| 193 | int gbprox_rcvmsg(struct gbproxy_config *cfg, struct msgb *msg, uint16_t nsei, uint16_t ns_bvci, uint16_t nsvci); |
| 194 | |
| 195 | int gbprox_signal(unsigned int subsys, unsigned int signal, |
| 196 | void *handler_data, void *signal_data); |
| 197 | |
| 198 | /* Reset all persistent NS-VC's */ |
| 199 | int gbprox_reset_persistent_nsvcs(struct gprs_ns_inst *nsi); |
| 200 | |
| 201 | void gbprox_reset(struct gbproxy_config *cfg); |
| 202 | |
| 203 | /* TLLI info handling */ |
| 204 | void gbproxy_delete_link_infos(struct gbproxy_peer *peer); |
| 205 | struct gbproxy_link_info *gbproxy_update_link_state_ul( |
| 206 | struct gbproxy_peer *peer, time_t now, |
| 207 | struct gprs_gb_parse_context *parse_ctx); |
| 208 | struct gbproxy_link_info *gbproxy_update_link_state_dl( |
| 209 | struct gbproxy_peer *peer, time_t now, |
| 210 | struct gprs_gb_parse_context *parse_ctx); |
| 211 | int 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); |
| 214 | int gbproxy_remove_stale_link_infos(struct gbproxy_peer *peer, time_t now); |
| 215 | void gbproxy_delete_link_info(struct gbproxy_peer *peer, |
| 216 | struct gbproxy_link_info *link_info); |
| 217 | void gbproxy_link_info_discard_messages(struct gbproxy_link_info *link_info); |
| 218 | |
| 219 | void gbproxy_attach_link_info(struct gbproxy_peer *peer, time_t now, |
| 220 | struct gbproxy_link_info *link_info); |
| 221 | void gbproxy_update_link_info(struct gbproxy_link_info *link_info, |
| 222 | const uint8_t *imsi, size_t imsi_len); |
| 223 | void gbproxy_detach_link_info(struct gbproxy_peer *peer, |
| 224 | struct gbproxy_link_info *link_info); |
| 225 | struct gbproxy_link_info *gbproxy_link_info_alloc( struct gbproxy_peer *peer); |
| 226 | |
| 227 | struct gbproxy_link_info *gbproxy_link_info_by_tlli( |
| 228 | struct gbproxy_peer *peer, uint32_t tlli); |
| 229 | struct gbproxy_link_info *gbproxy_link_info_by_imsi( |
| 230 | struct gbproxy_peer *peer, const uint8_t *imsi, size_t imsi_len); |
| 231 | struct gbproxy_link_info *gbproxy_link_info_by_any_sgsn_tlli( |
| 232 | struct gbproxy_peer *peer, uint32_t tlli); |
| 233 | struct gbproxy_link_info *gbproxy_link_info_by_sgsn_tlli( |
| 234 | struct gbproxy_peer *peer, |
| 235 | uint32_t tlli, uint32_t sgsn_nsei); |
| 236 | struct gbproxy_link_info *gbproxy_link_info_by_ptmsi( |
| 237 | struct gbproxy_peer *peer, |
| 238 | uint32_t ptmsi); |
| 239 | |
| 240 | int gbproxy_imsi_matches( |
| 241 | struct gbproxy_config *cfg, |
| 242 | enum gbproxy_match_id match_id, |
| 243 | struct gbproxy_link_info *link_info); |
| 244 | uint32_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 */ |
| 248 | uint32_t gbproxy_make_bss_ptmsi(struct gbproxy_peer *peer, uint32_t sgsn_ptmsi); |
| 249 | uint32_t gbproxy_make_sgsn_tlli( |
| 250 | struct gbproxy_peer *peer, struct gbproxy_link_info *link_info, |
| 251 | uint32_t bss_tlli); |
| 252 | void gbproxy_reset_link(struct gbproxy_link_info *link_info); |
| 253 | int gbproxy_check_imsi( |
| 254 | struct gbproxy_match *match, const uint8_t *imsi, size_t imsi_len); |
| 255 | |
| 256 | /* Message patching */ |
| 257 | void 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 | |
| 262 | int 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 | |
| 267 | int gbproxy_set_patch_filter( |
| 268 | struct gbproxy_match *match, const char *filter, const char **err_msg); |
| 269 | void gbproxy_clear_patch_filter(struct gbproxy_match *match); |
| 270 | |
| 271 | /* Peer handling */ |
| 272 | struct gbproxy_peer *gbproxy_peer_by_bvci( |
| 273 | struct gbproxy_config *cfg, uint16_t bvci); |
| 274 | struct gbproxy_peer *gbproxy_peer_by_nsei( |
| 275 | struct gbproxy_config *cfg, uint16_t nsei); |
| 276 | struct gbproxy_peer *gbproxy_peer_by_rai( |
| 277 | struct gbproxy_config *cfg, const uint8_t *ra); |
| 278 | struct gbproxy_peer *gbproxy_peer_by_lai( |
| 279 | struct gbproxy_config *cfg, const uint8_t *la); |
| 280 | struct gbproxy_peer *gbproxy_peer_by_lac( |
| 281 | struct gbproxy_config *cfg, const uint8_t *la); |
| 282 | struct gbproxy_peer *gbproxy_peer_by_bssgp_tlv( |
| 283 | struct gbproxy_config *cfg, struct tlv_parsed *tp); |
| 284 | struct gbproxy_peer *gbproxy_peer_alloc(struct gbproxy_config *cfg, uint16_t bvci); |
| 285 | void gbproxy_peer_free(struct gbproxy_peer *peer); |
| 286 | int gbproxy_cleanup_peers(struct gbproxy_config *cfg, uint16_t nsei, uint16_t bvci); |
| 287 | |
| 288 | #endif |