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 | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 34 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 35 | #define DIR_BSC 1 |
| 36 | #define DIR_MSC 2 |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 37 | |
Holger Hans Peter Freyther | d131b79 | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 38 | #define NAT_IPAC_PROTO_MGCP 0xfc |
Holger Hans Peter Freyther | f7d3335 | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 39 | |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 40 | struct bsc_nat; |
| 41 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 42 | /* |
| 43 | * For the NAT we will need to analyze and later patch |
| 44 | * the received message. This would require us to parse |
| 45 | * the IPA and SCCP header twice. Instead of doing this |
| 46 | * we will have one analyze structure and have the patching |
| 47 | * and filter operate on the same structure. |
| 48 | */ |
| 49 | struct bsc_nat_parsed { |
| 50 | /* ip access prototype */ |
| 51 | int ipa_proto; |
| 52 | |
| 53 | /* source local reference */ |
| 54 | struct sccp_source_reference *src_local_ref; |
| 55 | |
| 56 | /* destination local reference */ |
| 57 | struct sccp_source_reference *dest_local_ref; |
| 58 | |
| 59 | /* called ssn number */ |
| 60 | int called_ssn; |
| 61 | |
| 62 | /* calling ssn number */ |
| 63 | int calling_ssn; |
| 64 | |
| 65 | /* sccp message type */ |
| 66 | int sccp_type; |
| 67 | |
| 68 | /* bssap type, e.g. 0 for BSS Management */ |
| 69 | int bssap; |
| 70 | |
| 71 | /* the gsm0808 message type */ |
| 72 | int gsm_type; |
| 73 | }; |
| 74 | |
Holger Hans Peter Freyther | 9f8f3d0 | 2010-02-07 13:08:09 +0100 | [diff] [blame] | 75 | /* |
| 76 | * Per BSC data structure |
| 77 | */ |
| 78 | struct bsc_connection { |
| 79 | struct llist_head list_entry; |
| 80 | |
| 81 | /* do we know anything about this BSC? */ |
| 82 | int authenticated; |
| 83 | |
| 84 | /* the fd we use to communicate */ |
Holger Hans Peter Freyther | ed07a3f | 2010-06-15 18:47:10 +0800 | [diff] [blame] | 85 | struct write_queue write_queue; |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 86 | |
| 87 | /* the LAC assigned to this connection */ |
| 88 | unsigned int lac; |
| 89 | |
| 90 | /* a timeout node */ |
| 91 | struct timer_list id_timeout; |
Holger Hans Peter Freyther | aa69824 | 2010-06-15 18:46:19 +0800 | [diff] [blame] | 92 | |
| 93 | /* a back pointer */ |
| 94 | struct bsc_nat *nat; |
Holger Hans Peter Freyther | 9f8f3d0 | 2010-02-07 13:08:09 +0100 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | /* |
| 98 | * Per SCCP source local reference patch table. It needs to |
| 99 | * be updated on new SCCP connections, connection confirm and reject, |
| 100 | * and on the loss of the BSC connection. |
| 101 | */ |
| 102 | struct sccp_connections { |
| 103 | struct llist_head list_entry; |
| 104 | |
| 105 | struct bsc_connection *bsc; |
| 106 | |
| 107 | struct sccp_source_reference real_ref; |
| 108 | struct sccp_source_reference patched_ref; |
Holger Hans Peter Freyther | 16a6f70 | 2010-03-29 17:18:42 +0200 | [diff] [blame] | 109 | struct sccp_source_reference remote_ref; |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 110 | |
| 111 | /* GSM audio handling. That is 32 * multiplex + ts */ |
| 112 | int msc_timeslot; |
| 113 | int bsc_timeslot; |
Holger Hans Peter Freyther | 9f8f3d0 | 2010-02-07 13:08:09 +0100 | [diff] [blame] | 114 | }; |
| 115 | |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 116 | /** |
| 117 | * One BSC entry in the config |
| 118 | */ |
| 119 | struct bsc_config { |
| 120 | struct llist_head entry; |
| 121 | |
| 122 | char *token; |
| 123 | unsigned int lac; |
| 124 | int nr; |
| 125 | |
| 126 | struct bsc_nat *nat; |
| 127 | }; |
| 128 | |
| 129 | /** |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame^] | 130 | * BSCs point of view of endpoints |
| 131 | */ |
| 132 | struct bsc_endpoint { |
| 133 | /* the pending transaction id */ |
| 134 | char *transaction_id; |
| 135 | /* the bsc we are talking to */ |
| 136 | struct bsc_connection *bsc; |
| 137 | }; |
| 138 | |
| 139 | /** |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 140 | * the structure of the "nat" network |
| 141 | */ |
| 142 | struct bsc_nat { |
| 143 | /* active SCCP connections that need patching */ |
| 144 | struct llist_head sccp_connections; |
| 145 | |
| 146 | /* active BSC connections that need patching */ |
| 147 | struct llist_head bsc_connections; |
| 148 | |
| 149 | /* known BSC's */ |
| 150 | struct llist_head bsc_configs; |
| 151 | int num_bsc; |
Holger Hans Peter Freyther | f7d3335 | 2010-06-15 18:50:26 +0800 | [diff] [blame] | 152 | |
| 153 | /* MGCP config */ |
| 154 | struct mgcp_config *mgcp_cfg; |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 155 | struct write_queue mgcp_queue; |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame^] | 156 | struct msgb *mgcp_msg; |
| 157 | |
| 158 | struct bsc_endpoint *bsc_endpoints; |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | /* create and init the structures */ |
| 162 | struct bsc_config *bsc_config_alloc(struct bsc_nat *nat, const char *token, unsigned int lac); |
| 163 | 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] | 164 | struct bsc_nat *bsc_nat_alloc(void); |
| 165 | struct bsc_connection *bsc_connection_alloc(struct bsc_nat *nat); |
Holger Hans Peter Freyther | 9a85ef3 | 2010-06-15 18:46:11 +0800 | [diff] [blame] | 166 | |
Holger Hans Peter Freyther | 23fe7be | 2010-03-30 10:45:48 +0200 | [diff] [blame] | 167 | void sccp_connection_destroy(struct sccp_connections *); |
Holger Hans Peter Freyther | 9f8f3d0 | 2010-02-07 13:08:09 +0100 | [diff] [blame] | 168 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 169 | /** |
| 170 | * parse the given message into the above structure |
| 171 | */ |
| 172 | struct bsc_nat_parsed *bsc_nat_parse(struct msgb *msg); |
| 173 | |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 174 | /** |
| 175 | * filter based on IP Access header in both directions |
| 176 | */ |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 177 | 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] | 178 | int bsc_nat_vty_init(struct bsc_nat *nat); |
Holger Hans Peter Freyther | a5784b5 | 2010-03-30 06:39:37 +0200 | [diff] [blame] | 179 | struct bsc_connection *bsc_nat_find_bsc(struct bsc_nat *nat, struct msgb *msg); |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 180 | |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 181 | /** |
| 182 | * SCCP patching and handling |
| 183 | */ |
| 184 | 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] | 185 | 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] | 186 | 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] | 187 | struct sccp_connections *patch_sccp_src_ref_to_bsc(struct msgb *, struct bsc_nat_parsed *, struct bsc_nat *); |
| 188 | struct sccp_connections *patch_sccp_src_ref_to_msc(struct msgb *, struct bsc_nat_parsed *, struct bsc_nat *); |
Holger Hans Peter Freyther | 0ab6bab | 2010-06-15 18:47:49 +0800 | [diff] [blame] | 189 | |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 190 | /** |
| 191 | * MGCP/Audio handling |
| 192 | */ |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame^] | 193 | int bsc_write_mgcp_msg(struct bsc_connection *bsc, struct msgb *msg); |
Holger Hans Peter Freyther | d131b79 | 2010-03-31 07:30:58 +0200 | [diff] [blame] | 194 | 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] | 195 | int bsc_mgcp_assign(struct sccp_connections *, struct msgb *msg); |
| 196 | void bsc_mgcp_clear(struct sccp_connections *); |
Holger Hans Peter Freyther | a0df82d | 2010-04-01 08:21:33 +0200 | [diff] [blame^] | 197 | void bsc_mgcp_free_endpoint(struct bsc_nat *nat, int); |
Holger Hans Peter Freyther | 241e130 | 2010-03-31 09:16:56 +0200 | [diff] [blame] | 198 | void bsc_mgcp_free_endpoints(struct bsc_nat *nat); |
Holger Hans Peter Freyther | a7f8018 | 2010-03-31 13:02:22 +0200 | [diff] [blame] | 199 | int bsc_mgcp_init(struct bsc_nat *nat); |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 200 | |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 201 | struct bsc_connection *bsc_mgcp_find_con(struct bsc_nat *, int endpoint_number); |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 202 | struct msgb *bsc_mgcp_rewrite(struct msgb *msg, const char *ip, int port); |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 203 | |
Holger Hans Peter Freyther | 6a97b8d | 2010-06-15 18:45:26 +0800 | [diff] [blame] | 204 | #endif |