Holger Hans Peter Freyther | f75a680 | 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 | 98e49d4 | 2010-06-15 18:46:56 +0800 | [diff] [blame] | 5 | * (C) 2010 by On-Waves |
Holger Hans Peter Freyther | f75a680 | 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 | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 30 | #include <osmocore/talloc.h> |
| 31 | |
Holger Hans Peter Freyther | f75a680 | 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 | |
Holger Hans Peter Freyther | 55e8f55 | 2010-04-06 10:22:01 +0200 | [diff] [blame] | 92 | /* both directions IT timer */ |
| 93 | static const u_int8_t connnection_it[] = { |
| 94 | 0x00, 0x0b, 0xfd, |
| 95 | 0x10, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03, |
| 96 | 0x00, 0x00, 0x00, 0x00, |
| 97 | }; |
| 98 | |
Holger Hans Peter Freyther | dd6c51b | 2010-04-21 15:45:26 +0800 | [diff] [blame] | 99 | /* error in both directions */ |
| 100 | static const u_int8_t proto_error[] = { |
| 101 | 0x00, 0x05, 0xfd, |
| 102 | 0x0f, 0x22, 0x33, 0x44, 0x00, |
| 103 | }; |
| 104 | |
Holger Hans Peter Freyther | 6b99828 | 2010-04-01 08:47:12 +0200 | [diff] [blame] | 105 | /* MGCP wrap... */ |
| 106 | static const u_int8_t mgcp_msg[] = { |
| 107 | 0x00, 0x03, 0xfc, |
| 108 | 0x20, 0x20, 0x20, |
| 109 | }; |
| 110 | |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 111 | struct filter_result { |
| 112 | const u_int8_t *data; |
| 113 | const u_int16_t length; |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 114 | const int dir; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 115 | const int result; |
| 116 | }; |
| 117 | |
| 118 | static const struct filter_result results[] = { |
| 119 | { |
| 120 | .data = ipa_id, |
| 121 | .length = ARRAY_SIZE(ipa_id), |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 122 | .dir = DIR_MSC, |
| 123 | .result = 1, |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 124 | }, |
| 125 | { |
| 126 | .data = gsm_reset, |
| 127 | .length = ARRAY_SIZE(gsm_reset), |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 128 | .dir = DIR_MSC, |
| 129 | .result = 1, |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 130 | }, |
| 131 | { |
| 132 | .data = gsm_reset_ack, |
| 133 | .length = ARRAY_SIZE(gsm_reset_ack), |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 134 | .dir = DIR_BSC, |
| 135 | .result = 1, |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 136 | }, |
| 137 | { |
| 138 | .data = gsm_paging, |
| 139 | .length = ARRAY_SIZE(gsm_paging), |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 140 | .dir = DIR_BSC, |
| 141 | .result = 0, |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 142 | }, |
| 143 | { |
| 144 | .data = bssmap_cr, |
| 145 | .length = ARRAY_SIZE(bssmap_cr), |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 146 | .dir = DIR_MSC, |
| 147 | .result = 0, |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 148 | }, |
| 149 | { |
| 150 | .data = bssmap_cc, |
| 151 | .length = ARRAY_SIZE(bssmap_cc), |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 152 | .dir = DIR_BSC, |
| 153 | .result = 0, |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 154 | }, |
| 155 | { |
| 156 | .data = bssmap_released, |
| 157 | .length = ARRAY_SIZE(bssmap_released), |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 158 | .dir = DIR_MSC, |
| 159 | .result = 0, |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 160 | }, |
| 161 | { |
| 162 | .data = bssmap_release_complete, |
| 163 | .length = ARRAY_SIZE(bssmap_release_complete), |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 164 | .dir = DIR_BSC, |
| 165 | .result = 0, |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 166 | }, |
Holger Hans Peter Freyther | 6b99828 | 2010-04-01 08:47:12 +0200 | [diff] [blame] | 167 | { |
| 168 | .data = mgcp_msg, |
| 169 | .length = ARRAY_SIZE(mgcp_msg), |
| 170 | .dir = DIR_MSC, |
| 171 | .result = 0, |
| 172 | }, |
Holger Hans Peter Freyther | 55e8f55 | 2010-04-06 10:22:01 +0200 | [diff] [blame] | 173 | { |
| 174 | .data = connnection_it, |
| 175 | .length = ARRAY_SIZE(connnection_it), |
| 176 | .dir = DIR_BSC, |
| 177 | .result = 0, |
| 178 | }, |
| 179 | { |
| 180 | .data = connnection_it, |
| 181 | .length = ARRAY_SIZE(connnection_it), |
| 182 | .dir = DIR_MSC, |
| 183 | .result = 0, |
| 184 | }, |
Holger Hans Peter Freyther | dd6c51b | 2010-04-21 15:45:26 +0800 | [diff] [blame] | 185 | { |
| 186 | .data = proto_error, |
| 187 | .length = ARRAY_SIZE(proto_error), |
| 188 | .dir = DIR_BSC, |
| 189 | .result = 0, |
| 190 | }, |
| 191 | { |
| 192 | .data = proto_error, |
| 193 | .length = ARRAY_SIZE(proto_error), |
| 194 | .dir = DIR_MSC, |
| 195 | .result = 0, |
| 196 | }, |
| 197 | |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 198 | }; |
| 199 | |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 200 | static void test_filter(void) |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 201 | { |
| 202 | int i; |
| 203 | |
| 204 | |
| 205 | /* start testinh with proper messages */ |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 206 | fprintf(stderr, "Testing BSS Filtering.\n"); |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 207 | for (i = 0; i < ARRAY_SIZE(results); ++i) { |
| 208 | int result; |
| 209 | struct bsc_nat_parsed *parsed; |
| 210 | struct msgb *msg = msgb_alloc(4096, "test-message"); |
| 211 | |
| 212 | fprintf(stderr, "Going to test item: %d\n", i); |
| 213 | memcpy(msg->data, results[i].data, results[i].length); |
| 214 | msg->l2h = msgb_put(msg, results[i].length); |
| 215 | |
| 216 | parsed = bsc_nat_parse(msg); |
| 217 | if (!parsed) { |
| 218 | fprintf(stderr, "FAIL: Failed to parse the message\n"); |
| 219 | continue; |
| 220 | } |
| 221 | |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 222 | result = bsc_nat_filter_ipa(results[i].dir, msg, parsed); |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 223 | if (result != results[i].result) { |
| 224 | fprintf(stderr, "FAIL: Not the expected result got: %d wanted: %d\n", |
| 225 | result, results[i].result); |
| 226 | } |
| 227 | |
| 228 | msgb_free(msg); |
| 229 | } |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 230 | } |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 231 | |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 232 | #include "bsc_data.c" |
| 233 | |
| 234 | static void copy_to_msg(struct msgb *msg, const u_int8_t *data, unsigned int length) |
| 235 | { |
| 236 | msgb_reset(msg); |
| 237 | msg->l2h = msgb_put(msg, length); |
| 238 | memcpy(msg->l2h, data, msgb_l2len(msg)); |
| 239 | } |
| 240 | |
| 241 | #define VERIFY(con_found, con, msg, ver, str) \ |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 242 | if (!con_found || con_found->bsc != con) { \ |
Holger Hans Peter Freyther | d062c52 | 2010-06-15 18:48:44 +0800 | [diff] [blame] | 243 | fprintf(stderr, "Failed to find the con: %p\n", con_found); \ |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 244 | abort(); \ |
| 245 | } \ |
| 246 | if (memcmp(msg->data, ver, sizeof(ver)) != 0) { \ |
| 247 | fprintf(stderr, "Failed to patch the %s msg.\n", str); \ |
| 248 | abort(); \ |
| 249 | } |
| 250 | |
| 251 | /* test conn tracking once */ |
| 252 | static void test_contrack() |
| 253 | { |
| 254 | int rc; |
| 255 | struct bsc_nat *nat; |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 256 | struct bsc_connection *con; |
| 257 | struct sccp_connections *con_found; |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 258 | struct bsc_nat_parsed *parsed; |
| 259 | struct msgb *msg; |
| 260 | |
| 261 | fprintf(stderr, "Testing connection tracking.\n"); |
| 262 | nat = bsc_nat_alloc(); |
| 263 | con = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | 7d841e6 | 2010-04-18 02:30:57 +0800 | [diff] [blame] | 264 | con->cfg = bsc_config_alloc(nat, "foo", 23); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 265 | msg = msgb_alloc(4096, "test"); |
| 266 | |
| 267 | /* 1.) create a connection */ |
| 268 | copy_to_msg(msg, bsc_cr, sizeof(bsc_cr)); |
| 269 | parsed = bsc_nat_parse(msg); |
| 270 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
| 271 | if (con_found != NULL) { |
Holger Hans Peter Freyther | d062c52 | 2010-06-15 18:48:44 +0800 | [diff] [blame] | 272 | fprintf(stderr, "Con should not exist %p\n", con_found); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 273 | abort(); |
| 274 | } |
| 275 | rc = create_sccp_src_ref(con, msg, parsed); |
| 276 | if (rc != 0) { |
| 277 | fprintf(stderr, "Failed to create a ref\n"); |
| 278 | abort(); |
| 279 | } |
| 280 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 281 | if (!con_found || con_found->bsc != con) { |
Holger Hans Peter Freyther | d062c52 | 2010-06-15 18:48:44 +0800 | [diff] [blame] | 282 | fprintf(stderr, "Failed to find the con: %p\n", con_found); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 283 | abort(); |
| 284 | } |
| 285 | if (memcmp(msg->data, bsc_cr_patched, sizeof(bsc_cr_patched)) != 0) { |
| 286 | fprintf(stderr, "Failed to patch the BSC CR msg.\n"); |
| 287 | abort(); |
| 288 | } |
| 289 | talloc_free(parsed); |
| 290 | |
| 291 | /* 2.) get the cc */ |
| 292 | copy_to_msg(msg, msc_cc, sizeof(msc_cc)); |
| 293 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 294 | con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 295 | VERIFY(con_found, con, msg, msc_cc_patched, "MSC CC"); |
| 296 | if (update_sccp_src_ref(con_found, parsed) != 0) { |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 297 | fprintf(stderr, "Failed to update the SCCP con.\n"); |
| 298 | abort(); |
| 299 | } |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 300 | |
| 301 | /* 3.) send some data */ |
| 302 | copy_to_msg(msg, bsc_dtap, sizeof(bsc_dtap)); |
| 303 | parsed = bsc_nat_parse(msg); |
| 304 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
| 305 | VERIFY(con_found, con, msg, bsc_dtap_patched, "BSC DTAP"); |
| 306 | |
| 307 | /* 4.) receive some data */ |
| 308 | copy_to_msg(msg, msc_dtap, sizeof(msc_dtap)); |
| 309 | parsed = bsc_nat_parse(msg); |
| 310 | con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 311 | VERIFY(con_found, con, msg, msc_dtap_patched, "MSC DTAP"); |
| 312 | |
| 313 | /* 5.) close the connection */ |
| 314 | copy_to_msg(msg, msc_rlsd, sizeof(msc_rlsd)); |
| 315 | parsed = bsc_nat_parse(msg); |
| 316 | con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 317 | VERIFY(con_found, con, msg, msc_rlsd_patched, "MSC RLSD"); |
| 318 | |
| 319 | /* 6.) confirm the connection close */ |
| 320 | copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc)); |
| 321 | parsed = bsc_nat_parse(msg); |
| 322 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 323 | if (!con_found || con_found->bsc != con) { |
Holger Hans Peter Freyther | d062c52 | 2010-06-15 18:48:44 +0800 | [diff] [blame] | 324 | fprintf(stderr, "Failed to find the con: %p\n", con_found); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 325 | abort(); |
| 326 | } |
| 327 | if (memcmp(msg->data, bsc_rlc_patched, sizeof(bsc_rlc_patched)) != 0) { |
| 328 | fprintf(stderr, "Failed to patch the BSC CR msg.\n"); |
| 329 | abort(); |
| 330 | } |
| 331 | remove_sccp_src_ref(con, msg, parsed); |
Holger Hans Peter Freyther | bedecf3 | 2010-04-05 21:44:51 +0200 | [diff] [blame] | 332 | talloc_free(parsed); |
| 333 | |
| 334 | copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc)); |
| 335 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 336 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
| 337 | |
| 338 | /* verify that it is gone */ |
| 339 | if (con_found != NULL) { |
Holger Hans Peter Freyther | d062c52 | 2010-06-15 18:48:44 +0800 | [diff] [blame] | 340 | fprintf(stderr, "Con should be gone. %p\n", con_found); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 341 | abort(); |
| 342 | } |
| 343 | talloc_free(parsed); |
| 344 | |
| 345 | |
| 346 | talloc_free(nat); |
| 347 | msgb_free(msg); |
| 348 | } |
| 349 | |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 350 | static void test_paging(void) |
| 351 | { |
Holger Hans Peter Freyther | 935b2df | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 352 | int lac; |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 353 | struct bsc_nat *nat; |
| 354 | struct bsc_connection *con; |
| 355 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | bfcf519 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 356 | struct bsc_config cfg; |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 357 | struct msgb *msg; |
| 358 | |
| 359 | fprintf(stderr, "Testing paging by lac.\n"); |
| 360 | |
| 361 | nat = bsc_nat_alloc(); |
| 362 | con = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | bfcf519 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 363 | con->cfg = &cfg; |
| 364 | cfg.lac = 23; |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 365 | con->authenticated = 1; |
| 366 | llist_add(&con->list_entry, &nat->bsc_connections); |
| 367 | msg = msgb_alloc(4096, "test"); |
| 368 | |
| 369 | /* Test completely bad input */ |
| 370 | copy_to_msg(msg, paging_by_lac_cmd, sizeof(paging_by_lac_cmd)); |
Holger Hans Peter Freyther | 935b2df | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 371 | if (bsc_nat_find_bsc(nat, msg, &lac) != 0) { |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 372 | fprintf(stderr, "Should have not found anything.\n"); |
| 373 | abort(); |
| 374 | } |
| 375 | |
| 376 | /* Test it by not finding it */ |
| 377 | copy_to_msg(msg, paging_by_lac_cmd, sizeof(paging_by_lac_cmd)); |
| 378 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | 935b2df | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 379 | if (bsc_nat_find_bsc(nat, msg, &lac) != 0) { |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 380 | fprintf(stderr, "Should have not found aynthing.\n"); |
| 381 | abort(); |
| 382 | } |
| 383 | talloc_free(parsed); |
| 384 | |
| 385 | /* Test by finding it */ |
Holger Hans Peter Freyther | bfcf519 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 386 | cfg.lac = 8213; |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 387 | copy_to_msg(msg, paging_by_lac_cmd, sizeof(paging_by_lac_cmd)); |
| 388 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | 935b2df | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 389 | if (bsc_nat_find_bsc(nat, msg, &lac) != con) { |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 390 | fprintf(stderr, "Should have found it.\n"); |
| 391 | abort(); |
| 392 | } |
| 393 | talloc_free(parsed); |
| 394 | } |
| 395 | |
Holger Hans Peter Freyther | 8917998 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 396 | static void test_mgcp_ass_tracking(void) |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 397 | { |
| 398 | struct sccp_connections con; |
| 399 | struct bsc_nat_parsed *parsed; |
| 400 | struct msgb *msg; |
| 401 | |
| 402 | fprintf(stderr, "Testing MGCP.\n"); |
| 403 | memset(&con, 0, sizeof(con)); |
| 404 | |
| 405 | msg = msgb_alloc(4096, "foo"); |
| 406 | copy_to_msg(msg, ass_cmd, sizeof(ass_cmd)); |
| 407 | parsed = bsc_nat_parse(msg); |
| 408 | if (bsc_mgcp_assign(&con, msg) != 0) { |
| 409 | fprintf(stderr, "Failed to handle assignment.\n"); |
| 410 | abort(); |
| 411 | } |
| 412 | |
| 413 | if (con.msc_timeslot != 21) { |
| 414 | fprintf(stderr, "Timeslot should be 21.\n"); |
| 415 | abort(); |
| 416 | } |
| 417 | |
| 418 | if (con.bsc_timeslot != 21) { |
| 419 | fprintf(stderr, "Assigned timeslot should have been 21.\n"); |
| 420 | abort(); |
| 421 | } |
| 422 | talloc_free(parsed); |
| 423 | |
| 424 | bsc_mgcp_clear(&con); |
| 425 | if (con.bsc_timeslot != -1 || con.msc_timeslot != -1) { |
| 426 | fprintf(stderr, "Clearing should remove the mapping.\n"); |
| 427 | abort(); |
| 428 | } |
| 429 | } |
| 430 | |
Holger Hans Peter Freyther | 8917998 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 431 | /* test the code to find a given connection */ |
| 432 | static void test_mgcp_find(void) |
| 433 | { |
| 434 | struct bsc_nat *nat; |
| 435 | struct bsc_connection *con; |
| 436 | struct sccp_connections *sccp_con; |
| 437 | |
| 438 | fprintf(stderr, "Testing finding of a BSC Connection\n"); |
| 439 | |
| 440 | nat = bsc_nat_alloc(); |
| 441 | con = bsc_connection_alloc(nat); |
| 442 | llist_add(&con->list_entry, &nat->bsc_connections); |
| 443 | |
| 444 | sccp_con = talloc_zero(con, struct sccp_connections); |
| 445 | sccp_con->msc_timeslot = 12; |
| 446 | sccp_con->bsc_timeslot = 12; |
| 447 | sccp_con->bsc = con; |
| 448 | llist_add(&sccp_con->list_entry, &nat->sccp_connections); |
| 449 | |
| 450 | if (bsc_mgcp_find_con(nat, 11) != NULL) { |
| 451 | fprintf(stderr, "Found the wrong connection.\n"); |
| 452 | abort(); |
| 453 | } |
| 454 | |
Holger Hans Peter Freyther | 98d15ef | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 455 | if (bsc_mgcp_find_con(nat, 12) != sccp_con) { |
Holger Hans Peter Freyther | 8917998 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 456 | fprintf(stderr, "Didn't find the connection\n"); |
| 457 | abort(); |
| 458 | } |
| 459 | |
| 460 | sccp_con->msc_timeslot = 0; |
| 461 | sccp_con->bsc_timeslot = 0; |
Holger Hans Peter Freyther | 98d15ef | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 462 | if (bsc_mgcp_find_con(nat, 1) != sccp_con) { |
Holger Hans Peter Freyther | 8917998 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 463 | fprintf(stderr, "Didn't find the connection\n"); |
| 464 | abort(); |
| 465 | } |
| 466 | |
| 467 | /* free everything */ |
| 468 | talloc_free(nat); |
| 469 | } |
| 470 | |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 471 | static void test_mgcp_rewrite(void) |
| 472 | { |
| 473 | int i; |
Holger Hans Peter Freyther | 0d0aaa6 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 474 | struct msgb *output; |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 475 | fprintf(stderr, "Test rewriting MGCP messages.\n"); |
| 476 | |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 477 | for (i = 0; i < ARRAY_SIZE(mgcp_messages); ++i) { |
| 478 | const char *orig = mgcp_messages[i].orig; |
| 479 | const char *patc = mgcp_messages[i].patch; |
| 480 | const char *ip = mgcp_messages[i].ip; |
| 481 | const int port = mgcp_messages[i].port; |
| 482 | |
Holger Hans Peter Freyther | 0d0aaa6 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 483 | char *input = strdup(orig); |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 484 | |
Holger Hans Peter Freyther | 0d0aaa6 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 485 | output = bsc_mgcp_rewrite(input, strlen(input), ip, port); |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 486 | if (msgb_l2len(output) != strlen(patc)) { |
| 487 | fprintf(stderr, "Wrong sizes for test: %d %d != %d != %d\n", i, msgb_l2len(output), strlen(patc), strlen(orig)); |
| 488 | fprintf(stderr, "String '%s' vs '%s'\n", (const char *) output->l2h, patc); |
| 489 | abort(); |
| 490 | } |
| 491 | |
| 492 | if (memcmp(output->l2h, patc, msgb_l2len(output)) != 0) { |
| 493 | fprintf(stderr, "Broken on %d msg: '%s'\n", i, (const char *) output->l2h); |
| 494 | abort(); |
| 495 | } |
| 496 | |
| 497 | msgb_free(output); |
Holger Hans Peter Freyther | 0d0aaa6 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 498 | free(input); |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 499 | } |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 500 | } |
| 501 | |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 502 | static void test_mgcp_parse(void) |
| 503 | { |
| 504 | int code, ci; |
| 505 | char transaction[60]; |
| 506 | |
| 507 | fprintf(stderr, "Test MGCP response parsing.\n"); |
| 508 | |
| 509 | if (bsc_mgcp_parse_response(crcx_resp, &code, transaction) != 0) { |
| 510 | fprintf(stderr, "Failed to parse CRCX resp.\n"); |
| 511 | abort(); |
| 512 | } |
| 513 | |
| 514 | if (code != 200) { |
| 515 | fprintf(stderr, "Failed to parse the CODE properly. Got: %d\n", code); |
| 516 | abort(); |
| 517 | } |
| 518 | |
| 519 | if (strcmp(transaction, "23265295") != 0) { |
| 520 | fprintf(stderr, "Failed to parse transaction id: '%s'\n", transaction); |
| 521 | abort(); |
| 522 | } |
| 523 | |
| 524 | ci = bsc_mgcp_extract_ci(crcx_resp); |
| 525 | if (ci != 1) { |
| 526 | fprintf(stderr, "Failed to parse the CI. Got: %d\n", ci); |
| 527 | abort(); |
| 528 | } |
| 529 | } |
| 530 | |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 531 | int main(int argc, char **argv) |
| 532 | { |
| 533 | struct debug_target *stderr_target; |
| 534 | |
| 535 | stderr_target = debug_target_create_stderr(); |
| 536 | debug_add_target(stderr_target); |
| 537 | debug_set_all_filter(stderr_target, 1); |
| 538 | |
| 539 | test_filter(); |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 540 | test_contrack(); |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 541 | test_paging(); |
Holger Hans Peter Freyther | 8917998 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 542 | test_mgcp_ass_tracking(); |
| 543 | test_mgcp_find(); |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 544 | test_mgcp_rewrite(); |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 545 | test_mgcp_parse(); |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 546 | return 0; |
| 547 | } |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 548 | |
| 549 | void input_event() |
| 550 | {} |
| 551 | int nm_state_event() |
| 552 | { |
| 553 | return -1; |
| 554 | } |
| 555 | |
| 556 | int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id) |
| 557 | { |
| 558 | return -1; |
| 559 | } |