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