Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 1 | /* |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 2 | * (C) 2010-2011 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 3 | * (C) 2010-2011 by On-Waves |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #ifndef BSC_NAT_H |
| 22 | #define BSC_NAT_H |
| 23 | |
| 24 | #include "mgcp.h" |
| 25 | |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 26 | |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 27 | #include <osmocom/core/select.h> |
| 28 | #include <osmocom/core/msgb.h> |
| 29 | #include <osmocom/core/msgfile.h> |
| 30 | #include <osmocom/core/timer.h> |
| 31 | #include <osmocom/core/write_queue.h> |
| 32 | #include <osmocom/core/rate_ctr.h> |
| 33 | #include <osmocom/core/statistics.h> |
| 34 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 35 | |
| 36 | #include <regex.h> |
| 37 | |
| 38 | #define DIR_BSC 1 |
| 39 | #define DIR_MSC 2 |
| 40 | |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 41 | #define PAGIN_GROUP_UNASSIGNED -1 |
| 42 | |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 43 | struct sccp_source_reference; |
| 44 | struct sccp_connections; |
| 45 | struct bsc_nat_parsed; |
| 46 | struct bsc_nat; |
| 47 | struct bsc_nat_ussd_con; |
| 48 | |
| 49 | enum { |
| 50 | NAT_CON_TYPE_NONE, |
| 51 | NAT_CON_TYPE_LU, |
| 52 | NAT_CON_TYPE_CM_SERV_REQ, |
| 53 | NAT_CON_TYPE_PAG_RESP, |
| 54 | NAT_CON_TYPE_SSA, |
| 55 | NAT_CON_TYPE_LOCAL_REJECT, |
| 56 | NAT_CON_TYPE_OTHER, |
| 57 | }; |
| 58 | |
| 59 | /* |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 60 | * Is this terminated to the MSC, to the local machine (release |
| 61 | * handling for IMSI filtering) or to a USSD provider? |
| 62 | */ |
| 63 | enum { |
| 64 | NAT_CON_END_MSC, |
| 65 | NAT_CON_END_LOCAL, |
| 66 | NAT_CON_END_USSD, |
| 67 | }; |
| 68 | |
| 69 | /* |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 70 | * Per BSC data structure |
| 71 | */ |
| 72 | struct bsc_connection { |
| 73 | struct llist_head list_entry; |
| 74 | |
| 75 | /* do we know anything about this BSC? */ |
| 76 | int authenticated; |
| 77 | |
| 78 | /* the fd we use to communicate */ |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 79 | struct osmo_wqueue write_queue; |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 80 | |
| 81 | /* the BSS associated */ |
| 82 | struct bsc_config *cfg; |
| 83 | |
| 84 | /* a timeout node */ |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 85 | struct osmo_timer_list id_timeout; |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 86 | |
| 87 | /* pong timeout */ |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 88 | struct osmo_timer_list ping_timeout; |
| 89 | struct osmo_timer_list pong_timeout; |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 90 | |
| 91 | /* mgcp related code */ |
| 92 | char *_endpoint_status; |
| 93 | int number_multiplexes; |
| 94 | int max_endpoints; |
| 95 | int last_endpoint; |
| 96 | |
| 97 | /* a back pointer */ |
| 98 | struct bsc_nat *nat; |
| 99 | }; |
| 100 | |
| 101 | /** |
| 102 | * Stats per BSC |
| 103 | */ |
| 104 | struct bsc_config_stats { |
| 105 | struct rate_ctr_group *ctrg; |
| 106 | }; |
| 107 | |
| 108 | enum bsc_cfg_ctr { |
| 109 | BCFG_CTR_SCCP_CONN, |
| 110 | BCFG_CTR_SCCP_CALLS, |
| 111 | BCFG_CTR_NET_RECONN, |
| 112 | BCFG_CTR_DROPPED_SCCP, |
| 113 | BCFG_CTR_DROPPED_CALLS, |
| 114 | BCFG_CTR_REJECTED_CR, |
| 115 | BCFG_CTR_REJECTED_MSG, |
| 116 | BCFG_CTR_ILL_PACKET, |
| 117 | BCFG_CTR_CON_TYPE_LU, |
| 118 | BCFG_CTR_CON_CMSERV_RQ, |
| 119 | BCFG_CTR_CON_PAG_RESP, |
| 120 | BCFG_CTR_CON_SSA, |
| 121 | BCFG_CTR_CON_OTHER, |
| 122 | }; |
| 123 | |
| 124 | /** |
| 125 | * One BSC entry in the config |
| 126 | */ |
| 127 | struct bsc_config { |
| 128 | struct llist_head entry; |
| 129 | |
| 130 | char *token; |
| 131 | int nr; |
| 132 | |
| 133 | char *description; |
| 134 | |
| 135 | /* imsi white and blacklist */ |
| 136 | char *acc_lst_name; |
| 137 | |
| 138 | int forbid_paging; |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 139 | int paging_group; |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 140 | |
| 141 | /* audio handling */ |
| 142 | int max_endpoints; |
| 143 | |
| 144 | /* backpointer */ |
| 145 | struct bsc_nat *nat; |
| 146 | |
| 147 | struct bsc_config_stats stats; |
| 148 | |
| 149 | struct llist_head lac_list; |
| 150 | }; |
| 151 | |
| 152 | struct bsc_lac_entry { |
| 153 | struct llist_head entry; |
| 154 | uint16_t lac; |
| 155 | }; |
| 156 | |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 157 | struct bsc_nat_paging_group { |
| 158 | struct llist_head entry; |
| 159 | |
| 160 | /* list of lac entries */ |
| 161 | struct llist_head lists; |
| 162 | int nr; |
| 163 | }; |
| 164 | |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 165 | /** |
| 166 | * BSCs point of view of endpoints |
| 167 | */ |
| 168 | struct bsc_endpoint { |
| 169 | /* the operation that is carried out */ |
| 170 | int transaction_state; |
| 171 | /* the pending transaction id */ |
| 172 | char *transaction_id; |
| 173 | /* the bsc we are talking to */ |
| 174 | struct bsc_connection *bsc; |
| 175 | }; |
| 176 | |
| 177 | /** |
| 178 | * Statistic for the nat. |
| 179 | */ |
| 180 | struct bsc_nat_statistics { |
| 181 | struct { |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 182 | struct osmo_counter *conn; |
| 183 | struct osmo_counter *calls; |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 184 | } sccp; |
| 185 | |
| 186 | struct { |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 187 | struct osmo_counter *reconn; |
| 188 | struct osmo_counter *auth_fail; |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 189 | } bsc; |
| 190 | |
| 191 | struct { |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 192 | struct osmo_counter *reconn; |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 193 | } msc; |
| 194 | |
| 195 | struct { |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 196 | struct osmo_counter *reconn; |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 197 | } ussd; |
| 198 | }; |
| 199 | |
| 200 | enum bsc_nat_acc_ctr { |
| 201 | ACC_LIST_BSC_FILTER, |
| 202 | ACC_LIST_NAT_FILTER, |
| 203 | }; |
| 204 | |
| 205 | struct bsc_nat_acc_lst { |
| 206 | struct llist_head list; |
| 207 | |
| 208 | /* counter */ |
| 209 | struct rate_ctr_group *stats; |
| 210 | |
| 211 | /* the name of the list */ |
| 212 | const char *name; |
| 213 | struct llist_head fltr_list; |
| 214 | }; |
| 215 | |
| 216 | struct bsc_nat_acc_lst_entry { |
| 217 | struct llist_head list; |
| 218 | |
| 219 | /* the filter */ |
| 220 | char *imsi_allow; |
| 221 | regex_t imsi_allow_re; |
| 222 | char *imsi_deny; |
| 223 | regex_t imsi_deny_re; |
| 224 | }; |
| 225 | |
| 226 | /** |
| 227 | * the structure of the "nat" network |
| 228 | */ |
| 229 | struct bsc_nat { |
| 230 | /* active SCCP connections that need patching */ |
| 231 | struct llist_head sccp_connections; |
| 232 | |
| 233 | /* active BSC connections that need patching */ |
| 234 | struct llist_head bsc_connections; |
| 235 | |
| 236 | /* access lists */ |
| 237 | struct llist_head access_lists; |
| 238 | |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 239 | /* paging groups */ |
| 240 | struct llist_head paging_groups; |
| 241 | |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 242 | /* known BSC's */ |
| 243 | struct llist_head bsc_configs; |
| 244 | int num_bsc; |
| 245 | int bsc_ip_dscp; |
| 246 | |
| 247 | /* MGCP config */ |
| 248 | struct mgcp_config *mgcp_cfg; |
| 249 | uint8_t mgcp_msg[4096]; |
| 250 | int mgcp_length; |
| 251 | |
| 252 | /* msc things */ |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 253 | struct llist_head dests; |
| 254 | struct bsc_msc_dest *main_dest; |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 255 | struct bsc_msc_connection *msc_con; |
| 256 | char *token; |
| 257 | |
| 258 | /* timeouts */ |
| 259 | int auth_timeout; |
| 260 | int ping_timeout; |
| 261 | int pong_timeout; |
| 262 | |
| 263 | struct bsc_endpoint *bsc_endpoints; |
| 264 | |
| 265 | /* filter */ |
| 266 | char *acc_lst_name; |
| 267 | |
| 268 | /* number rewriting */ |
| 269 | char *num_rewr_name; |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 270 | struct llist_head num_rewr; |
| 271 | |
| 272 | char *smsc_rewr_name; |
| 273 | struct llist_head smsc_rewr; |
| 274 | char *tpdest_match_name; |
| 275 | struct llist_head tpdest_match; |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 276 | |
| 277 | /* USSD messages we want to match */ |
| 278 | char *ussd_lst_name; |
| 279 | char *ussd_query; |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 280 | regex_t ussd_query_re; |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 281 | char *ussd_token; |
| 282 | char *ussd_local; |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 283 | struct osmo_fd ussd_listen; |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 284 | struct bsc_nat_ussd_con *ussd_con; |
| 285 | |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 286 | /* for maintainenance */ |
| 287 | int blocked; |
| 288 | |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 289 | /* statistics */ |
| 290 | struct bsc_nat_statistics stats; |
| 291 | }; |
| 292 | |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 293 | struct bsc_nat_ussd_con { |
| 294 | struct osmo_wqueue queue; |
| 295 | struct bsc_nat *nat; |
| 296 | int authorized; |
| 297 | |
| 298 | struct osmo_timer_list auth_timeout; |
| 299 | }; |
| 300 | |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 301 | /* create and init the structures */ |
| 302 | struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token); |
| 303 | struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num); |
| 304 | void bsc_config_free(struct bsc_config *); |
| 305 | void bsc_config_add_lac(struct bsc_config *cfg, int lac); |
| 306 | void bsc_config_del_lac(struct bsc_config *cfg, int lac); |
| 307 | int bsc_config_handles_lac(struct bsc_config *cfg, int lac); |
| 308 | |
| 309 | struct bsc_nat *bsc_nat_alloc(void); |
| 310 | struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat); |
| 311 | void bsc_nat_set_msc_ip(struct bsc_nat *bsc, const char *ip); |
| 312 | |
| 313 | void sccp_connection_destroy(struct sccp_connections *); |
| 314 | void bsc_close_connection(struct bsc_connection *); |
| 315 | |
| 316 | const char *bsc_con_type_to_string(int type); |
| 317 | |
| 318 | /** |
| 319 | * parse the given message into the above structure |
| 320 | */ |
| 321 | struct bsc_nat_parsed *bsc_nat_parse(struct msgb *msg); |
| 322 | |
| 323 | /** |
| 324 | * filter based on IP Access header in both directions |
| 325 | */ |
| 326 | int bsc_nat_filter_ipa(int direction, struct msgb *msg, struct bsc_nat_parsed *parsed); |
| 327 | int bsc_nat_vty_init(struct bsc_nat *nat); |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 328 | int bsc_nat_find_paging(struct msgb *msg, const uint8_t **,int *len); |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 329 | |
| 330 | /** |
| 331 | * Content filtering. |
| 332 | */ |
| 333 | int bsc_nat_filter_sccp_cr(struct bsc_connection *bsc, struct msgb *msg, |
| 334 | struct bsc_nat_parsed *, int *con_type, char **imsi); |
| 335 | int bsc_nat_filter_dt(struct bsc_connection *bsc, struct msgb *msg, |
| 336 | struct sccp_connections *con, struct bsc_nat_parsed *parsed); |
| 337 | |
| 338 | /** |
| 339 | * SCCP patching and handling |
| 340 | */ |
| 341 | struct sccp_connections *create_sccp_src_ref(struct bsc_connection *bsc, struct bsc_nat_parsed *parsed); |
| 342 | int update_sccp_src_ref(struct sccp_connections *sccp, struct bsc_nat_parsed *parsed); |
| 343 | void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed); |
| 344 | struct sccp_connections *patch_sccp_src_ref_to_bsc(struct msgb *, struct bsc_nat_parsed *, struct bsc_nat *); |
| 345 | struct sccp_connections *patch_sccp_src_ref_to_msc(struct msgb *, struct bsc_nat_parsed *, struct bsc_connection *); |
| 346 | struct sccp_connections *bsc_nat_find_con_by_bsc(struct bsc_nat *, struct sccp_source_reference *); |
| 347 | |
| 348 | /** |
| 349 | * MGCP/Audio handling |
| 350 | */ |
| 351 | int bsc_mgcp_nr_multiplexes(int max_endpoints); |
| 352 | int bsc_write_mgcp(struct bsc_connection *bsc, const uint8_t *data, unsigned int length); |
| 353 | int bsc_mgcp_assign_patch(struct sccp_connections *, struct msgb *msg); |
| 354 | void bsc_mgcp_init(struct sccp_connections *); |
| 355 | void bsc_mgcp_dlcx(struct sccp_connections *); |
| 356 | void bsc_mgcp_free_endpoints(struct bsc_nat *nat); |
| 357 | int bsc_mgcp_nat_init(struct bsc_nat *nat); |
| 358 | |
| 359 | struct sccp_connections *bsc_mgcp_find_con(struct bsc_nat *, int endpoint_number); |
| 360 | struct msgb *bsc_mgcp_rewrite(char *input, int length, int endp, const char *ip, int port); |
| 361 | void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg); |
| 362 | |
| 363 | void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc); |
| 364 | int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60]); |
| 365 | uint32_t bsc_mgcp_extract_ci(const char *resp); |
| 366 | |
| 367 | |
| 368 | int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int id); |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 369 | int bsc_do_write(struct osmo_wqueue *queue, struct msgb *msg, int id); |
| 370 | int bsc_write_msg(struct osmo_wqueue *queue, struct msgb *msg); |
| 371 | int bsc_write_cb(struct osmo_fd *bfd, struct msgb *msg); |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 372 | |
| 373 | /* IMSI allow/deny handling */ |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 374 | int bsc_parse_reg(void *ctx, regex_t *reg, char **imsi, int argc, const char **argv) __attribute__ ((warn_unused_result)); |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 375 | struct bsc_nat_acc_lst *bsc_nat_acc_lst_find(struct bsc_nat *nat, const char *name); |
| 376 | struct bsc_nat_acc_lst *bsc_nat_acc_lst_get(struct bsc_nat *nat, const char *name); |
| 377 | void bsc_nat_acc_lst_delete(struct bsc_nat_acc_lst *lst); |
| 378 | |
| 379 | struct bsc_nat_acc_lst_entry *bsc_nat_acc_lst_entry_create(struct bsc_nat_acc_lst *); |
| 380 | int bsc_nat_lst_check_allow(struct bsc_nat_acc_lst *lst, const char *imsi); |
| 381 | |
| 382 | int bsc_nat_msc_is_connected(struct bsc_nat *nat); |
| 383 | |
| 384 | int bsc_conn_type_to_ctr(struct sccp_connections *conn); |
| 385 | |
| 386 | struct gsm48_hdr *bsc_unpack_dtap(struct bsc_nat_parsed *parsed, struct msgb *msg, uint32_t *len); |
| 387 | |
| 388 | /** USSD filtering */ |
| 389 | int bsc_ussd_init(struct bsc_nat *nat); |
| 390 | int bsc_check_ussd(struct sccp_connections *con, struct bsc_nat_parsed *parsed, struct msgb *msg); |
| 391 | int bsc_close_ussd_connections(struct bsc_nat *nat); |
| 392 | |
Jonathan Santos | 5a45b15 | 2011-08-17 15:33:57 -0400 | [diff] [blame^] | 393 | struct msgb *bsc_nat_rewrite_msg(struct bsc_nat *nat, struct msgb *msg, struct bsc_nat_parsed *, const char *imsi); |
| 394 | |
| 395 | /** paging group handling */ |
| 396 | struct bsc_nat_paging_group *bsc_nat_paging_group_num(struct bsc_nat *nat, int group); |
| 397 | struct bsc_nat_paging_group *bsc_nat_paging_group_create(struct bsc_nat *nat, int group); |
| 398 | void bsc_nat_paging_group_delete(struct bsc_nat_paging_group *); |
| 399 | void bsc_nat_paging_group_add_lac(struct bsc_nat_paging_group *grp, int lac); |
| 400 | void bsc_nat_paging_group_del_lac(struct bsc_nat_paging_group *grp, int lac); |
| 401 | |
| 402 | /** |
| 403 | * Number rewriting support below |
| 404 | */ |
| 405 | struct bsc_nat_num_rewr_entry { |
| 406 | struct llist_head list; |
| 407 | |
| 408 | regex_t msisdn_reg; |
| 409 | regex_t num_reg; |
| 410 | |
| 411 | char *replace; |
| 412 | }; |
| 413 | |
| 414 | void bsc_nat_num_rewr_entry_adapt(void *ctx, struct llist_head *head, const struct osmo_config_list *); |
Jonathan Santos | 03fd8d0 | 2011-05-25 13:54:02 -0400 | [diff] [blame] | 415 | |
| 416 | #endif |