Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1 | /* |
| 2 | * BSC NAT Message filtering |
| 3 | * |
| 4 | * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
Holger Hans Peter Freyther | df6143a | 2010-06-15 18:46:56 +0800 | [diff] [blame] | 5 | * (C) 2010 by On-Waves |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include <openbsc/debug.h> |
| 27 | #include <openbsc/gsm_data.h> |
| 28 | #include <openbsc/bsc_nat.h> |
| 29 | |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 30 | #include <osmocore/talloc.h> |
| 31 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | |
| 34 | /* test messages for ipa */ |
| 35 | static u_int8_t ipa_id[] = { |
| 36 | 0x00, 0x01, 0xfe, 0x06, |
| 37 | }; |
| 38 | |
| 39 | /* SCCP messages are below */ |
| 40 | static u_int8_t gsm_reset[] = { |
| 41 | 0x00, 0x12, 0xfd, |
| 42 | 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe, |
| 43 | 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04, |
| 44 | 0x01, 0x20, |
| 45 | }; |
| 46 | |
| 47 | static const u_int8_t gsm_reset_ack[] = { |
| 48 | 0x00, 0x13, 0xfd, |
| 49 | 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01, |
| 50 | 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03, |
| 51 | 0x00, 0x01, 0x31, |
| 52 | }; |
| 53 | |
| 54 | static const u_int8_t gsm_paging[] = { |
| 55 | 0x00, 0x20, 0xfd, |
| 56 | 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01, |
| 57 | 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x10, |
| 58 | 0x00, 0x0e, 0x52, 0x08, 0x08, 0x29, 0x47, 0x10, |
| 59 | 0x02, 0x01, 0x31, 0x97, 0x61, 0x1a, 0x01, 0x06, |
| 60 | }; |
| 61 | |
| 62 | /* BSC -> MSC connection open */ |
| 63 | static const u_int8_t bssmap_cr[] = { |
| 64 | 0x00, 0x2c, 0xfd, |
| 65 | 0x01, 0x01, 0x02, 0x03, 0x02, 0x02, 0x04, 0x02, |
| 66 | 0x42, 0xfe, 0x0f, 0x1f, 0x00, 0x1d, 0x57, 0x05, |
| 67 | 0x08, 0x00, 0x72, 0xf4, 0x80, 0x20, 0x12, 0xc3, |
| 68 | 0x50, 0x17, 0x10, 0x05, 0x24, 0x11, 0x03, 0x33, |
| 69 | 0x19, 0xa2, 0x08, 0x29, 0x47, 0x10, 0x02, 0x01, |
| 70 | 0x31, 0x97, 0x61, 0x00 |
| 71 | }; |
| 72 | |
| 73 | /* MSC -> BSC connection confirm */ |
| 74 | static const u_int8_t bssmap_cc[] = { |
| 75 | 0x00, 0x0a, 0xfd, |
| 76 | 0x02, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, |
| 77 | }; |
| 78 | |
| 79 | /* MSC -> BSC released */ |
| 80 | static const u_int8_t bssmap_released[] = { |
| 81 | 0x00, 0x0e, 0xfd, |
| 82 | 0x04, 0x00, 0x00, 0x03, 0x01, 0x02, 0x03, 0x00, 0x01, 0x0f, |
| 83 | 0x02, 0x23, 0x42, 0x00, |
| 84 | }; |
| 85 | |
| 86 | /* BSC -> MSC released */ |
| 87 | static const u_int8_t bssmap_release_complete[] = { |
| 88 | 0x00, 0x07, 0xfd, |
| 89 | 0x05, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03 |
| 90 | }; |
| 91 | |
| 92 | struct filter_result { |
| 93 | const u_int8_t *data; |
| 94 | const u_int16_t length; |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 95 | const int dir; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 96 | const int result; |
| 97 | }; |
| 98 | |
| 99 | static const struct filter_result results[] = { |
| 100 | { |
| 101 | .data = ipa_id, |
| 102 | .length = ARRAY_SIZE(ipa_id), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 103 | .dir = DIR_MSC, |
| 104 | .result = 1, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 105 | }, |
| 106 | { |
| 107 | .data = gsm_reset, |
| 108 | .length = ARRAY_SIZE(gsm_reset), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 109 | .dir = DIR_MSC, |
| 110 | .result = 1, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 111 | }, |
| 112 | { |
| 113 | .data = gsm_reset_ack, |
| 114 | .length = ARRAY_SIZE(gsm_reset_ack), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 115 | .dir = DIR_BSC, |
| 116 | .result = 1, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 117 | }, |
| 118 | { |
| 119 | .data = gsm_paging, |
| 120 | .length = ARRAY_SIZE(gsm_paging), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 121 | .dir = DIR_BSC, |
| 122 | .result = 0, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 123 | }, |
| 124 | { |
| 125 | .data = bssmap_cr, |
| 126 | .length = ARRAY_SIZE(bssmap_cr), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 127 | .dir = DIR_MSC, |
| 128 | .result = 0, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 129 | }, |
| 130 | { |
| 131 | .data = bssmap_cc, |
| 132 | .length = ARRAY_SIZE(bssmap_cc), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 133 | .dir = DIR_BSC, |
| 134 | .result = 0, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 135 | }, |
| 136 | { |
| 137 | .data = bssmap_released, |
| 138 | .length = ARRAY_SIZE(bssmap_released), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 139 | .dir = DIR_MSC, |
| 140 | .result = 0, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 141 | }, |
| 142 | { |
| 143 | .data = bssmap_release_complete, |
| 144 | .length = ARRAY_SIZE(bssmap_release_complete), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 145 | .dir = DIR_BSC, |
| 146 | .result = 0, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 147 | }, |
| 148 | }; |
| 149 | |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 150 | static void test_filter(void) |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 151 | { |
| 152 | int i; |
| 153 | |
| 154 | |
| 155 | /* start testinh with proper messages */ |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 156 | fprintf(stderr, "Testing BSS Filtering.\n"); |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 157 | for (i = 0; i < ARRAY_SIZE(results); ++i) { |
| 158 | int result; |
| 159 | struct bsc_nat_parsed *parsed; |
| 160 | struct msgb *msg = msgb_alloc(4096, "test-message"); |
| 161 | |
| 162 | fprintf(stderr, "Going to test item: %d\n", i); |
| 163 | memcpy(msg->data, results[i].data, results[i].length); |
| 164 | msg->l2h = msgb_put(msg, results[i].length); |
| 165 | |
| 166 | parsed = bsc_nat_parse(msg); |
| 167 | if (!parsed) { |
| 168 | fprintf(stderr, "FAIL: Failed to parse the message\n"); |
| 169 | continue; |
| 170 | } |
| 171 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 172 | result = bsc_nat_filter_ipa(results[i].dir, msg, parsed); |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 173 | if (result != results[i].result) { |
| 174 | fprintf(stderr, "FAIL: Not the expected result got: %d wanted: %d\n", |
| 175 | result, results[i].result); |
| 176 | } |
| 177 | |
| 178 | msgb_free(msg); |
| 179 | } |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 180 | } |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 181 | |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 182 | #include "bsc_data.c" |
| 183 | |
| 184 | static void copy_to_msg(struct msgb *msg, const u_int8_t *data, unsigned int length) |
| 185 | { |
| 186 | msgb_reset(msg); |
| 187 | msg->l2h = msgb_put(msg, length); |
| 188 | memcpy(msg->l2h, data, msgb_l2len(msg)); |
| 189 | } |
| 190 | |
| 191 | #define VERIFY(con_found, con, msg, ver, str) \ |
| 192 | if (con_found != con) { \ |
| 193 | fprintf(stderr, "Failed to find the con: 0x%p\n", con_found); \ |
| 194 | abort(); \ |
| 195 | } \ |
| 196 | if (memcmp(msg->data, ver, sizeof(ver)) != 0) { \ |
| 197 | fprintf(stderr, "Failed to patch the %s msg.\n", str); \ |
| 198 | abort(); \ |
| 199 | } |
| 200 | |
| 201 | /* test conn tracking once */ |
| 202 | static void test_contrack() |
| 203 | { |
| 204 | int rc; |
| 205 | struct bsc_nat *nat; |
| 206 | struct bsc_connection *con, *con_found; |
| 207 | struct bsc_nat_parsed *parsed; |
| 208 | struct msgb *msg; |
| 209 | |
| 210 | fprintf(stderr, "Testing connection tracking.\n"); |
| 211 | nat = bsc_nat_alloc(); |
| 212 | con = bsc_connection_alloc(nat); |
| 213 | msg = msgb_alloc(4096, "test"); |
| 214 | |
| 215 | /* 1.) create a connection */ |
| 216 | copy_to_msg(msg, bsc_cr, sizeof(bsc_cr)); |
| 217 | parsed = bsc_nat_parse(msg); |
| 218 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
| 219 | if (con_found != NULL) { |
| 220 | fprintf(stderr, "Con should not exist 0x%p\n", con_found); |
| 221 | abort(); |
| 222 | } |
| 223 | rc = create_sccp_src_ref(con, msg, parsed); |
| 224 | if (rc != 0) { |
| 225 | fprintf(stderr, "Failed to create a ref\n"); |
| 226 | abort(); |
| 227 | } |
| 228 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
| 229 | if (con_found != con) { |
| 230 | fprintf(stderr, "Failed to find the con: 0x%p\n", con_found); |
| 231 | abort(); |
| 232 | } |
| 233 | if (memcmp(msg->data, bsc_cr_patched, sizeof(bsc_cr_patched)) != 0) { |
| 234 | fprintf(stderr, "Failed to patch the BSC CR msg.\n"); |
| 235 | abort(); |
| 236 | } |
| 237 | talloc_free(parsed); |
| 238 | |
| 239 | /* 2.) get the cc */ |
| 240 | copy_to_msg(msg, msc_cc, sizeof(msc_cc)); |
| 241 | parsed = bsc_nat_parse(msg); |
| 242 | if (update_sccp_src_ref(con, msg, parsed) != 0) { |
| 243 | fprintf(stderr, "Failed to update the SCCP con.\n"); |
| 244 | abort(); |
| 245 | } |
| 246 | con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 247 | VERIFY(con_found, con, msg, msc_cc_patched, "MSC CC"); |
| 248 | |
| 249 | /* 3.) send some data */ |
| 250 | copy_to_msg(msg, bsc_dtap, sizeof(bsc_dtap)); |
| 251 | parsed = bsc_nat_parse(msg); |
| 252 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
| 253 | VERIFY(con_found, con, msg, bsc_dtap_patched, "BSC DTAP"); |
| 254 | |
| 255 | /* 4.) receive some data */ |
| 256 | copy_to_msg(msg, msc_dtap, sizeof(msc_dtap)); |
| 257 | parsed = bsc_nat_parse(msg); |
| 258 | con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 259 | VERIFY(con_found, con, msg, msc_dtap_patched, "MSC DTAP"); |
| 260 | |
| 261 | /* 5.) close the connection */ |
| 262 | copy_to_msg(msg, msc_rlsd, sizeof(msc_rlsd)); |
| 263 | parsed = bsc_nat_parse(msg); |
| 264 | con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 265 | VERIFY(con_found, con, msg, msc_rlsd_patched, "MSC RLSD"); |
| 266 | |
| 267 | /* 6.) confirm the connection close */ |
| 268 | copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc)); |
| 269 | parsed = bsc_nat_parse(msg); |
| 270 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
| 271 | if (con_found != con) { |
| 272 | fprintf(stderr, "Failed to find the con: 0x%p\n", con_found); |
| 273 | abort(); |
| 274 | } |
| 275 | if (memcmp(msg->data, bsc_rlc_patched, sizeof(bsc_rlc_patched)) != 0) { |
| 276 | fprintf(stderr, "Failed to patch the BSC CR msg.\n"); |
| 277 | abort(); |
| 278 | } |
| 279 | remove_sccp_src_ref(con, msg, parsed); |
| 280 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
| 281 | |
| 282 | /* verify that it is gone */ |
| 283 | if (con_found != NULL) { |
| 284 | fprintf(stderr, "Con should be gone. 0x%p\n", con_found); |
| 285 | abort(); |
| 286 | } |
| 287 | talloc_free(parsed); |
| 288 | |
| 289 | |
| 290 | talloc_free(nat); |
| 291 | msgb_free(msg); |
| 292 | } |
| 293 | |
| 294 | int main(int argc, char **argv) |
| 295 | { |
| 296 | struct debug_target *stderr_target; |
| 297 | |
| 298 | stderr_target = debug_target_create_stderr(); |
| 299 | debug_add_target(stderr_target); |
| 300 | debug_set_all_filter(stderr_target, 1); |
| 301 | |
| 302 | test_filter(); |
| 303 | |
| 304 | debug_set_log_level(stderr_target, 1); |
| 305 | test_contrack(); |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 306 | return 0; |
| 307 | } |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 308 | |
| 309 | void input_event() |
| 310 | {} |
| 311 | int nm_state_event() |
| 312 | { |
| 313 | return -1; |
| 314 | } |
| 315 | |
| 316 | int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id) |
| 317 | { |
| 318 | return -1; |
| 319 | } |