Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
Holger Hans Peter Freyther | df6143a | 2010-06-15 18:46:56 +0800 | [diff] [blame] | 3 | * (C) 2010 by On-Waves |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [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 General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 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 General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | #ifndef BSC_NAT_H |
| 23 | #define BSC_NAT_H |
| 24 | |
Holger Hans Peter Freyther | f7d3335 | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 25 | #include "mgcp.h" |
| 26 | |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 27 | #include <sys/types.h> |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 28 | #include <sccp/sccp_types.h> |
Holger Hans Peter Freyther | 9f8f3d0 | 2010-02-07 13:08:09 +0100 | [diff] [blame] | 29 | |
Holger Hans Peter Freyther | 6c45f2e | 2010-06-15 19:06:18 +0800 | [diff] [blame] | 30 | #include <osmocore/select.h> |
| 31 | #include <osmocore/msgb.h> |
| 32 | #include <osmocore/timer.h> |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 33 | #include <osmocore/write_queue.h> |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 34 | #include <osmocore/statistics.h> |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 35 | |
Holger Hans Peter Freyther | c16cf27 | 2010-04-13 09:24:37 +0200 | [diff] [blame] | 36 | #include <regex.h> |
| 37 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 38 | #define DIR_BSC 1 |
| 39 | #define DIR_MSC 2 |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 40 | |
Holger Hans Peter Freyther | d131b79 | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 41 | #define NAT_IPAC_PROTO_MGCP 0xfc |
Holger Hans Peter Freyther | f7d3335 | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 42 | |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 43 | struct bsc_nat; |
| 44 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 45 | /* |
| 46 | * For the NAT we will need to analyze and later patch |
| 47 | * the received message. This would require us to parse |
| 48 | * the IPA and SCCP header twice. Instead of doing this |
| 49 | * we will have one analyze structure and have the patching |
| 50 | * and filter operate on the same structure. |
| 51 | */ |
| 52 | struct bsc_nat_parsed { |
| 53 | /* ip access prototype */ |
| 54 | int ipa_proto; |
| 55 | |
| 56 | /* source local reference */ |
| 57 | struct sccp_source_reference *src_local_ref; |
| 58 | |
| 59 | /* destination local reference */ |
| 60 | struct sccp_source_reference *dest_local_ref; |
| 61 | |
| 62 | /* called ssn number */ |
| 63 | int called_ssn; |
| 64 | |
| 65 | /* calling ssn number */ |
| 66 | int calling_ssn; |
| 67 | |
| 68 | /* sccp message type */ |
| 69 | int sccp_type; |
| 70 | |
| 71 | /* bssap type, e.g. 0 for BSS Management */ |
| 72 | int bssap; |
| 73 | |
| 74 | /* the gsm0808 message type */ |
| 75 | int gsm_type; |
| 76 | }; |
| 77 | |
Holger Hans Peter Freyther | 9f8f3d0 | 2010-02-07 13:08:09 +0100 | [diff] [blame] | 78 | /* |
| 79 | * Per BSC data structure |
| 80 | */ |
| 81 | struct bsc_connection { |
| 82 | struct llist_head list_entry; |
| 83 | |
| 84 | /* do we know anything about this BSC? */ |
| 85 | int authenticated; |
| 86 | |
| 87 | /* the fd we use to communicate */ |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 88 | struct write_queue write_queue; |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 89 | |
Holger Hans Peter Freyther | 47dd494 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 90 | /* the BSS associated */ |
| 91 | struct bsc_config *cfg; |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 92 | |
| 93 | /* a timeout node */ |
| 94 | struct timer_list id_timeout; |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 95 | |
Holger Hans Peter Freyther | 906c15e | 2010-05-02 19:28:59 +0800 | [diff] [blame] | 96 | /* pong timeout */ |
| 97 | struct timer_list ping_timeout; |
| 98 | struct timer_list pong_timeout; |
| 99 | |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 100 | /* a back pointer */ |
| 101 | struct bsc_nat *nat; |
Holger Hans Peter Freyther | 9f8f3d0 | 2010-02-07 13:08:09 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | /* |
| 105 | * Per SCCP source local reference patch table. It needs to |
| 106 | * be updated on new SCCP connections, connection confirm and reject, |
| 107 | * and on the loss of the BSC connection. |
| 108 | */ |
| 109 | struct sccp_connections { |
| 110 | struct llist_head list_entry; |
| 111 | |
| 112 | struct bsc_connection *bsc; |
| 113 | |
| 114 | struct sccp_source_reference real_ref; |
| 115 | struct sccp_source_reference patched_ref; |
Holger Hans Peter Freyther | 16a6f70 | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 116 | struct sccp_source_reference remote_ref; |
Holger Hans Peter Freyther | 4c683d1 | 2010-04-23 14:13:27 +0800 | [diff] [blame] | 117 | int has_remote_ref; |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 118 | |
| 119 | /* GSM audio handling. That is 32 * multiplex + ts */ |
Holger Hans Peter Freyther | 959bbcf | 2010-04-22 20:12:13 +0800 | [diff] [blame] | 120 | int crcx; |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 121 | int msc_timeslot; |
| 122 | int bsc_timeslot; |
Holger Hans Peter Freyther | c14e09b | 2010-06-15 18:51:49 +0800 | [diff] [blame] | 123 | |
| 124 | /* timeout handling */ |
| 125 | struct timespec creation_time; |
Holger Hans Peter Freyther | 9f8f3d0 | 2010-02-07 13:08:09 +0100 | [diff] [blame] | 126 | }; |
| 127 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 128 | /** |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 129 | * Stats per BSC |
| 130 | */ |
| 131 | struct bsc_config_stats { |
| 132 | struct { |
| 133 | struct counter *conn; |
| 134 | struct counter *calls; |
| 135 | } sccp; |
| 136 | |
| 137 | struct { |
| 138 | struct counter *reconn; |
| 139 | } net; |
| 140 | }; |
| 141 | |
| 142 | /** |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 143 | * One BSC entry in the config |
| 144 | */ |
| 145 | struct bsc_config { |
| 146 | struct llist_head entry; |
| 147 | |
| 148 | char *token; |
| 149 | unsigned int lac; |
| 150 | int nr; |
| 151 | |
Holger Hans Peter Freyther | c16cf27 | 2010-04-13 09:24:37 +0200 | [diff] [blame] | 152 | /* imsi white and blacklist */ |
| 153 | char *imsi_allow; |
| 154 | regex_t imsi_allow_re; |
| 155 | char *imsi_deny; |
| 156 | regex_t imsi_deny_re; |
| 157 | |
Holger Hans Peter Freyther | 62e5843 | 2010-04-21 19:05:14 +0800 | [diff] [blame] | 158 | int forbid_paging; |
| 159 | |
Holger Hans Peter Freyther | c16cf27 | 2010-04-13 09:24:37 +0200 | [diff] [blame] | 160 | /* backpointer */ |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 161 | struct bsc_nat *nat; |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 162 | |
| 163 | struct bsc_config_stats stats; |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | /** |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 167 | * BSCs point of view of endpoints |
| 168 | */ |
| 169 | struct bsc_endpoint { |
| 170 | /* the pending transaction id */ |
| 171 | char *transaction_id; |
| 172 | /* the bsc we are talking to */ |
| 173 | struct bsc_connection *bsc; |
| 174 | }; |
| 175 | |
| 176 | /** |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 177 | * Statistic for the nat. |
| 178 | */ |
| 179 | struct bsc_nat_statistics { |
| 180 | struct { |
| 181 | struct counter *conn; |
| 182 | struct counter *calls; |
| 183 | } sccp; |
| 184 | |
| 185 | struct { |
| 186 | struct counter *reconn; |
| 187 | struct counter *auth_fail; |
| 188 | } bsc; |
| 189 | |
| 190 | struct { |
| 191 | struct counter *reconn; |
| 192 | } msc; |
| 193 | }; |
| 194 | |
| 195 | /** |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 196 | * the structure of the "nat" network |
| 197 | */ |
| 198 | struct bsc_nat { |
| 199 | /* active SCCP connections that need patching */ |
| 200 | struct llist_head sccp_connections; |
| 201 | |
| 202 | /* active BSC connections that need patching */ |
| 203 | struct llist_head bsc_connections; |
| 204 | |
| 205 | /* known BSC's */ |
| 206 | struct llist_head bsc_configs; |
| 207 | int num_bsc; |
Holger Hans Peter Freyther | f7d3335 | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 208 | |
| 209 | /* MGCP config */ |
| 210 | struct mgcp_config *mgcp_cfg; |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 211 | struct write_queue mgcp_queue; |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 212 | u_int8_t mgcp_msg[4096]; |
| 213 | int mgcp_length; |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 214 | |
Holger Hans Peter Freyther | b752761 | 2010-04-07 11:20:36 +0200 | [diff] [blame] | 215 | /* msc things */ |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 216 | char *msc_ip; |
Holger Hans Peter Freyther | 8139553 | 2010-04-17 07:48:45 +0200 | [diff] [blame] | 217 | int msc_port; |
Holger Hans Peter Freyther | b752761 | 2010-04-07 11:20:36 +0200 | [diff] [blame] | 218 | int first_contact; |
Holger Hans Peter Freyther | aad82ce | 2010-05-11 19:07:39 +0800 | [diff] [blame] | 219 | struct bsc_msc_connection *msc_con; |
Holger Hans Peter Freyther | b752761 | 2010-04-07 11:20:36 +0200 | [diff] [blame] | 220 | |
Holger Hans Peter Freyther | da35a8d | 2010-05-05 16:57:38 +0800 | [diff] [blame] | 221 | /* timeouts */ |
| 222 | int auth_timeout; |
| 223 | int ping_timeout; |
| 224 | int pong_timeout; |
| 225 | |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame] | 226 | struct bsc_endpoint *bsc_endpoints; |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 227 | |
Holger Hans Peter Freyther | c16cf27 | 2010-04-13 09:24:37 +0200 | [diff] [blame] | 228 | /* filter */ |
| 229 | char *imsi_allow; |
| 230 | regex_t imsi_allow_re; |
| 231 | char *imsi_deny; |
| 232 | regex_t imsi_deny_re; |
| 233 | |
Holger Hans Peter Freyther | d470286 | 2010-04-12 12:17:09 +0200 | [diff] [blame] | 234 | /* statistics */ |
| 235 | struct bsc_nat_statistics stats; |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | /* create and init the structures */ |
| 239 | struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac); |
| 240 | struct bsc_config *bsc_config_num(struct bsc_nat *nat, int num); |
Holger Hans Peter Freyther | dcf8a7d | 2010-06-15 18:48:01 +0800 | [diff] [blame] | 241 | struct bsc_nat *bsc_nat_alloc(void); |
| 242 | struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat); |
Holger Hans Peter Freyther | a88742c | 2010-06-15 18:51:04 +0800 | [diff] [blame] | 243 | void bsc_nat_set_msc_ip(struct bsc_nat *bsc, const char *ip); |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 244 | |
Holger Hans Peter Freyther | 23fe7be | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 245 | void sccp_connection_destroy(struct sccp_connections *); |
Holger Hans Peter Freyther | 2f9dcf0 | 2010-04-27 13:21:39 +0800 | [diff] [blame] | 246 | void bsc_close_connection(struct bsc_connection *); |
Holger Hans Peter Freyther | 9f8f3d0 | 2010-02-07 13:08:09 +0100 | [diff] [blame] | 247 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 248 | /** |
| 249 | * parse the given message into the above structure |
| 250 | */ |
| 251 | struct bsc_nat_parsed *bsc_nat_parse(struct msgb *msg); |
| 252 | |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 253 | /** |
| 254 | * filter based on IP Access header in both directions |
| 255 | */ |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 256 | int bsc_nat_filter_ipa(int direction, struct msgb *msg, struct bsc_nat_parsed *parsed); |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 257 | int bsc_nat_vty_init(struct bsc_nat *nat); |
Holger Hans Peter Freyther | 979a309 | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 258 | struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg, int *_lac); |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 259 | |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 260 | /** |
| 261 | * SCCP patching and handling |
| 262 | */ |
| 263 | int create_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed); |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 264 | int update_sccp_src_ref(struct sccp_connections *sccp, struct bsc_nat_parsed *parsed); |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 265 | void remove_sccp_src_ref(struct bsc_connection *bsc, struct msgb *msg, struct bsc_nat_parsed *parsed); |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 266 | struct sccp_connections *patch_sccp_src_ref_to_bsc(struct msgb *, struct bsc_nat_parsed *, struct bsc_nat *); |
Holger Hans Peter Freyther | b5513ca | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 267 | struct sccp_connections *patch_sccp_src_ref_to_msc(struct msgb *, struct bsc_nat_parsed *, struct bsc_connection *); |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 268 | |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 269 | /** |
| 270 | * MGCP/Audio handling |
| 271 | */ |
Holger Hans Peter Freyther | d131b79 | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 272 | int bsc_write_mgcp(struct bsc_connection *bsc, const u_int8_t *data, unsigned int length); |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 273 | int bsc_mgcp_assign(struct sccp_connections *, struct msgb *msg); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 274 | void bsc_mgcp_init(struct sccp_connections *); |
| 275 | void bsc_mgcp_dlcx(struct sccp_connections *); |
Holger Hans Peter Freyther | 241e130 | 2010-03-31 09:16:56 +0200 | [diff] [blame] | 276 | void bsc_mgcp_free_endpoints(struct bsc_nat *nat); |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 277 | int bsc_mgcp_nat_init(struct bsc_nat *nat); |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 278 | |
Holger Hans Peter Freyther | 08a1b16 | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 279 | struct sccp_connections *bsc_mgcp_find_con(struct bsc_nat *, int endpoint_number); |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 280 | struct msgb *bsc_mgcp_rewrite(char *input, int length, const char *ip, int port); |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 281 | void bsc_mgcp_forward(struct bsc_connection *bsc, struct msgb *msg); |
| 282 | |
Holger Hans Peter Freyther | 26a4389 | 2010-04-05 23:09:27 +0200 | [diff] [blame] | 283 | void bsc_mgcp_clear_endpoints_for(struct bsc_connection *bsc); |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 284 | int bsc_mgcp_parse_response(const char *str, int *code, char transaction[60]); |
| 285 | int bsc_mgcp_extract_ci(const char *resp); |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 286 | |
Holger Hans Peter Freyther | 2896df7 | 2010-04-08 10:24:57 +0200 | [diff] [blame] | 287 | |
| 288 | int bsc_write(struct bsc_connection *bsc, struct msgb *msg, int id); |
| 289 | |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 290 | #endif |