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 | { |
| 332 | struct bsc_nat *nat; |
| 333 | struct bsc_connection *con; |
| 334 | struct bsc_nat_parsed *parsed; |
| 335 | struct msgb *msg; |
| 336 | |
| 337 | fprintf(stderr, "Testing paging by lac.\n"); |
| 338 | |
| 339 | nat = bsc_nat_alloc(); |
| 340 | con = bsc_connection_alloc(nat); |
| 341 | con->lac = 23; |
| 342 | con->authenticated = 1; |
| 343 | llist_add(&con->list_entry, &nat->bsc_connections); |
| 344 | msg = msgb_alloc(4096, "test"); |
| 345 | |
| 346 | /* Test completely bad input */ |
| 347 | copy_to_msg(msg, paging_by_lac_cmd, sizeof(paging_by_lac_cmd)); |
| 348 | if (bsc_nat_find_bsc(nat, msg) != 0) { |
| 349 | fprintf(stderr, "Should have not found anything.\n"); |
| 350 | abort(); |
| 351 | } |
| 352 | |
| 353 | /* Test it by not finding it */ |
| 354 | copy_to_msg(msg, paging_by_lac_cmd, sizeof(paging_by_lac_cmd)); |
| 355 | parsed = bsc_nat_parse(msg); |
| 356 | if (bsc_nat_find_bsc(nat, msg) != 0) { |
| 357 | fprintf(stderr, "Should have not found aynthing.\n"); |
| 358 | abort(); |
| 359 | } |
| 360 | talloc_free(parsed); |
| 361 | |
| 362 | /* Test by finding it */ |
| 363 | con->lac = 8213; |
| 364 | copy_to_msg(msg, paging_by_lac_cmd, sizeof(paging_by_lac_cmd)); |
| 365 | parsed = bsc_nat_parse(msg); |
| 366 | if (bsc_nat_find_bsc(nat, msg) != con) { |
| 367 | fprintf(stderr, "Should have found it.\n"); |
| 368 | abort(); |
| 369 | } |
| 370 | talloc_free(parsed); |
| 371 | } |
| 372 | |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 373 | static void test_mgcp_ass_tracking(void) |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 374 | { |
| 375 | struct sccp_connections con; |
| 376 | struct bsc_nat_parsed *parsed; |
| 377 | struct msgb *msg; |
| 378 | |
| 379 | fprintf(stderr, "Testing MGCP.\n"); |
| 380 | memset(&con, 0, sizeof(con)); |
| 381 | |
| 382 | msg = msgb_alloc(4096, "foo"); |
| 383 | copy_to_msg(msg, ass_cmd, sizeof(ass_cmd)); |
| 384 | parsed = bsc_nat_parse(msg); |
| 385 | if (bsc_mgcp_assign(&con, msg) != 0) { |
| 386 | fprintf(stderr, "Failed to handle assignment.\n"); |
| 387 | abort(); |
| 388 | } |
| 389 | |
| 390 | if (con.msc_timeslot != 21) { |
| 391 | fprintf(stderr, "Timeslot should be 21.\n"); |
| 392 | abort(); |
| 393 | } |
| 394 | |
| 395 | if (con.bsc_timeslot != 21) { |
| 396 | fprintf(stderr, "Assigned timeslot should have been 21.\n"); |
| 397 | abort(); |
| 398 | } |
| 399 | talloc_free(parsed); |
| 400 | |
| 401 | bsc_mgcp_clear(&con); |
| 402 | if (con.bsc_timeslot != -1 || con.msc_timeslot != -1) { |
| 403 | fprintf(stderr, "Clearing should remove the mapping.\n"); |
| 404 | abort(); |
| 405 | } |
| 406 | } |
| 407 | |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 408 | /* test the code to find a given connection */ |
| 409 | static void test_mgcp_find(void) |
| 410 | { |
| 411 | struct bsc_nat *nat; |
| 412 | struct bsc_connection *con; |
| 413 | struct sccp_connections *sccp_con; |
| 414 | |
| 415 | fprintf(stderr, "Testing finding of a BSC Connection\n"); |
| 416 | |
| 417 | nat = bsc_nat_alloc(); |
| 418 | con = bsc_connection_alloc(nat); |
| 419 | llist_add(&con->list_entry, &nat->bsc_connections); |
| 420 | |
| 421 | sccp_con = talloc_zero(con, struct sccp_connections); |
| 422 | sccp_con->msc_timeslot = 12; |
| 423 | sccp_con->bsc_timeslot = 12; |
| 424 | sccp_con->bsc = con; |
| 425 | llist_add(&sccp_con->list_entry, &nat->sccp_connections); |
| 426 | |
| 427 | if (bsc_mgcp_find_con(nat, 11) != NULL) { |
| 428 | fprintf(stderr, "Found the wrong connection.\n"); |
| 429 | abort(); |
| 430 | } |
| 431 | |
| 432 | if (bsc_mgcp_find_con(nat, 12) != con) { |
| 433 | fprintf(stderr, "Didn't find the connection\n"); |
| 434 | abort(); |
| 435 | } |
| 436 | |
| 437 | sccp_con->msc_timeslot = 0; |
| 438 | sccp_con->bsc_timeslot = 0; |
| 439 | if (bsc_mgcp_find_con(nat, 1) != con) { |
| 440 | fprintf(stderr, "Didn't find the connection\n"); |
| 441 | abort(); |
| 442 | } |
| 443 | |
| 444 | /* free everything */ |
| 445 | talloc_free(nat); |
| 446 | } |
| 447 | |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 448 | static void test_mgcp_rewrite(void) |
| 449 | { |
| 450 | int i; |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 451 | struct msgb *output; |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 452 | fprintf(stderr, "Test rewriting MGCP messages.\n"); |
| 453 | |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 454 | for (i = 0; i < ARRAY_SIZE(mgcp_messages); ++i) { |
| 455 | const char *orig = mgcp_messages[i].orig; |
| 456 | const char *patc = mgcp_messages[i].patch; |
| 457 | const char *ip = mgcp_messages[i].ip; |
| 458 | const int port = mgcp_messages[i].port; |
| 459 | |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 460 | char *input = strdup(orig); |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 461 | |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 462 | output = bsc_mgcp_rewrite(input, strlen(input), ip, port); |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 463 | if (msgb_l2len(output) != strlen(patc)) { |
| 464 | fprintf(stderr, "Wrong sizes for test: %d %d != %d != %d\n", i, msgb_l2len(output), strlen(patc), strlen(orig)); |
| 465 | fprintf(stderr, "String '%s' vs '%s'\n", (const char *) output->l2h, patc); |
| 466 | abort(); |
| 467 | } |
| 468 | |
| 469 | if (memcmp(output->l2h, patc, msgb_l2len(output)) != 0) { |
| 470 | fprintf(stderr, "Broken on %d msg: '%s'\n", i, (const char *) output->l2h); |
| 471 | abort(); |
| 472 | } |
| 473 | |
| 474 | msgb_free(output); |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 475 | free(input); |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 476 | } |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 477 | } |
| 478 | |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 479 | static void test_mgcp_parse(void) |
| 480 | { |
| 481 | int code, ci; |
| 482 | char transaction[60]; |
| 483 | |
| 484 | fprintf(stderr, "Test MGCP response parsing.\n"); |
| 485 | |
| 486 | if (bsc_mgcp_parse_response(crcx_resp, &code, transaction) != 0) { |
| 487 | fprintf(stderr, "Failed to parse CRCX resp.\n"); |
| 488 | abort(); |
| 489 | } |
| 490 | |
| 491 | if (code != 200) { |
| 492 | fprintf(stderr, "Failed to parse the CODE properly. Got: %d\n", code); |
| 493 | abort(); |
| 494 | } |
| 495 | |
| 496 | if (strcmp(transaction, "23265295") != 0) { |
| 497 | fprintf(stderr, "Failed to parse transaction id: '%s'\n", transaction); |
| 498 | abort(); |
| 499 | } |
| 500 | |
| 501 | ci = bsc_mgcp_extract_ci(crcx_resp); |
| 502 | if (ci != 1) { |
| 503 | fprintf(stderr, "Failed to parse the CI. Got: %d\n", ci); |
| 504 | abort(); |
| 505 | } |
| 506 | } |
| 507 | |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 508 | int main(int argc, char **argv) |
| 509 | { |
| 510 | struct debug_target *stderr_target; |
| 511 | |
| 512 | stderr_target = debug_target_create_stderr(); |
| 513 | debug_add_target(stderr_target); |
| 514 | debug_set_all_filter(stderr_target, 1); |
| 515 | |
| 516 | test_filter(); |
Holger Hans Peter Freyther | 04fd992 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 517 | test_contrack(); |
Holger Hans Peter Freyther | 04fd992 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 518 | test_paging(); |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 519 | test_mgcp_ass_tracking(); |
| 520 | test_mgcp_find(); |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 521 | test_mgcp_rewrite(); |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 522 | test_mgcp_parse(); |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 523 | return 0; |
| 524 | } |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 525 | |
| 526 | void input_event() |
| 527 | {} |
| 528 | int nm_state_event() |
| 529 | { |
| 530 | return -1; |
| 531 | } |
| 532 | |
| 533 | int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id) |
| 534 | { |
| 535 | return -1; |
| 536 | } |