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 | 6b99828 | 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 | f75a680 | 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 | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 108 | const int dir; |
Holger Hans Peter Freyther | f75a680 | 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 | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 116 | .dir = DIR_MSC, |
| 117 | .result = 1, |
Holger Hans Peter Freyther | f75a680 | 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 | 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_ack, |
| 127 | .length = ARRAY_SIZE(gsm_reset_ack), |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 128 | .dir = DIR_BSC, |
| 129 | .result = 1, |
Holger Hans Peter Freyther | f75a680 | 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 | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 134 | .dir = DIR_BSC, |
| 135 | .result = 0, |
Holger Hans Peter Freyther | f75a680 | 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 | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 140 | .dir = DIR_MSC, |
| 141 | .result = 0, |
Holger Hans Peter Freyther | f75a680 | 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 | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 146 | .dir = DIR_BSC, |
| 147 | .result = 0, |
Holger Hans Peter Freyther | f75a680 | 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 | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 152 | .dir = DIR_MSC, |
| 153 | .result = 0, |
Holger Hans Peter Freyther | f75a680 | 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 | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 158 | .dir = DIR_BSC, |
| 159 | .result = 0, |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 160 | }, |
Holger Hans Peter Freyther | 6b99828 | 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 | 55e8f55 | 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 | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 179 | }; |
| 180 | |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 181 | static void test_filter(void) |
Holger Hans Peter Freyther | f75a680 | 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 | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 187 | fprintf(stderr, "Testing BSS Filtering.\n"); |
Holger Hans Peter Freyther | f75a680 | 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 | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 203 | result = bsc_nat_filter_ipa(results[i].dir, msg, parsed); |
Holger Hans Peter Freyther | f75a680 | 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 | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 211 | } |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 212 | |
Holger Hans Peter Freyther | b63a9f8 | 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 | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 223 | if (!con_found || con_found->bsc != con) { \ |
Holger Hans Peter Freyther | d062c52 | 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 | b63a9f8 | 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 | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 237 | struct bsc_connection *con; |
| 238 | struct sccp_connections *con_found; |
Holger Hans Peter Freyther | b63a9f8 | 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); |
Holger Hans Peter Freyther | 7d841e6 | 2010-04-18 02:30:57 +0800 | [diff] [blame] | 245 | con->cfg = bsc_config_alloc(nat, "foo", 23); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 246 | msg = msgb_alloc(4096, "test"); |
| 247 | |
| 248 | /* 1.) create a connection */ |
| 249 | copy_to_msg(msg, bsc_cr, sizeof(bsc_cr)); |
| 250 | parsed = bsc_nat_parse(msg); |
| 251 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
| 252 | if (con_found != NULL) { |
Holger Hans Peter Freyther | d062c52 | 2010-06-15 18:48:44 +0800 | [diff] [blame] | 253 | fprintf(stderr, "Con should not exist %p\n", con_found); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 254 | abort(); |
| 255 | } |
| 256 | rc = create_sccp_src_ref(con, msg, parsed); |
| 257 | if (rc != 0) { |
| 258 | fprintf(stderr, "Failed to create a ref\n"); |
| 259 | abort(); |
| 260 | } |
| 261 | 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] | 262 | if (!con_found || con_found->bsc != con) { |
Holger Hans Peter Freyther | d062c52 | 2010-06-15 18:48:44 +0800 | [diff] [blame] | 263 | 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] | 264 | abort(); |
| 265 | } |
| 266 | if (memcmp(msg->data, bsc_cr_patched, sizeof(bsc_cr_patched)) != 0) { |
| 267 | fprintf(stderr, "Failed to patch the BSC CR msg.\n"); |
| 268 | abort(); |
| 269 | } |
| 270 | talloc_free(parsed); |
| 271 | |
| 272 | /* 2.) get the cc */ |
| 273 | copy_to_msg(msg, msc_cc, sizeof(msc_cc)); |
| 274 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 275 | con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 276 | VERIFY(con_found, con, msg, msc_cc_patched, "MSC CC"); |
| 277 | if (update_sccp_src_ref(con_found, parsed) != 0) { |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 278 | fprintf(stderr, "Failed to update the SCCP con.\n"); |
| 279 | abort(); |
| 280 | } |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 281 | |
| 282 | /* 3.) send some data */ |
| 283 | copy_to_msg(msg, bsc_dtap, sizeof(bsc_dtap)); |
| 284 | parsed = bsc_nat_parse(msg); |
| 285 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
| 286 | VERIFY(con_found, con, msg, bsc_dtap_patched, "BSC DTAP"); |
| 287 | |
| 288 | /* 4.) receive some data */ |
| 289 | copy_to_msg(msg, msc_dtap, sizeof(msc_dtap)); |
| 290 | parsed = bsc_nat_parse(msg); |
| 291 | con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 292 | VERIFY(con_found, con, msg, msc_dtap_patched, "MSC DTAP"); |
| 293 | |
| 294 | /* 5.) close the connection */ |
| 295 | copy_to_msg(msg, msc_rlsd, sizeof(msc_rlsd)); |
| 296 | parsed = bsc_nat_parse(msg); |
| 297 | con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 298 | VERIFY(con_found, con, msg, msc_rlsd_patched, "MSC RLSD"); |
| 299 | |
| 300 | /* 6.) confirm the connection close */ |
| 301 | copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc)); |
| 302 | parsed = bsc_nat_parse(msg); |
| 303 | 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] | 304 | if (!con_found || con_found->bsc != con) { |
Holger Hans Peter Freyther | d062c52 | 2010-06-15 18:48:44 +0800 | [diff] [blame] | 305 | 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] | 306 | abort(); |
| 307 | } |
| 308 | if (memcmp(msg->data, bsc_rlc_patched, sizeof(bsc_rlc_patched)) != 0) { |
| 309 | fprintf(stderr, "Failed to patch the BSC CR msg.\n"); |
| 310 | abort(); |
| 311 | } |
| 312 | remove_sccp_src_ref(con, msg, parsed); |
Holger Hans Peter Freyther | bedecf3 | 2010-04-05 21:44:51 +0200 | [diff] [blame] | 313 | talloc_free(parsed); |
| 314 | |
| 315 | copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc)); |
| 316 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 317 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, nat); |
| 318 | |
| 319 | /* verify that it is gone */ |
| 320 | if (con_found != NULL) { |
Holger Hans Peter Freyther | d062c52 | 2010-06-15 18:48:44 +0800 | [diff] [blame] | 321 | fprintf(stderr, "Con should be gone. %p\n", con_found); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 322 | abort(); |
| 323 | } |
| 324 | talloc_free(parsed); |
| 325 | |
| 326 | |
| 327 | talloc_free(nat); |
| 328 | msgb_free(msg); |
| 329 | } |
| 330 | |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 331 | static void test_paging(void) |
| 332 | { |
Holger Hans Peter Freyther | 935b2df | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 333 | int lac; |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 334 | struct bsc_nat *nat; |
| 335 | struct bsc_connection *con; |
| 336 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | bfcf519 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 337 | struct bsc_config cfg; |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 338 | struct msgb *msg; |
| 339 | |
| 340 | fprintf(stderr, "Testing paging by lac.\n"); |
| 341 | |
| 342 | nat = bsc_nat_alloc(); |
| 343 | con = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | bfcf519 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 344 | con->cfg = &cfg; |
| 345 | cfg.lac = 23; |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 346 | con->authenticated = 1; |
| 347 | llist_add(&con->list_entry, &nat->bsc_connections); |
| 348 | msg = msgb_alloc(4096, "test"); |
| 349 | |
| 350 | /* Test completely bad input */ |
| 351 | 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] | 352 | if (bsc_nat_find_bsc(nat, msg, &lac) != 0) { |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 353 | fprintf(stderr, "Should have not found anything.\n"); |
| 354 | abort(); |
| 355 | } |
| 356 | |
| 357 | /* Test it by not finding it */ |
| 358 | copy_to_msg(msg, paging_by_lac_cmd, sizeof(paging_by_lac_cmd)); |
| 359 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | 935b2df | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 360 | if (bsc_nat_find_bsc(nat, msg, &lac) != 0) { |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 361 | fprintf(stderr, "Should have not found aynthing.\n"); |
| 362 | abort(); |
| 363 | } |
| 364 | talloc_free(parsed); |
| 365 | |
| 366 | /* Test by finding it */ |
Holger Hans Peter Freyther | bfcf519 | 2010-04-06 15:11:34 +0200 | [diff] [blame] | 367 | cfg.lac = 8213; |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 368 | copy_to_msg(msg, paging_by_lac_cmd, sizeof(paging_by_lac_cmd)); |
| 369 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | 935b2df | 2010-04-17 08:07:19 +0200 | [diff] [blame] | 370 | if (bsc_nat_find_bsc(nat, msg, &lac) != con) { |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 371 | fprintf(stderr, "Should have found it.\n"); |
| 372 | abort(); |
| 373 | } |
| 374 | talloc_free(parsed); |
| 375 | } |
| 376 | |
Holger Hans Peter Freyther | 8917998 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 377 | static void test_mgcp_ass_tracking(void) |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 378 | { |
| 379 | struct sccp_connections con; |
| 380 | struct bsc_nat_parsed *parsed; |
| 381 | struct msgb *msg; |
| 382 | |
| 383 | fprintf(stderr, "Testing MGCP.\n"); |
| 384 | memset(&con, 0, sizeof(con)); |
| 385 | |
| 386 | msg = msgb_alloc(4096, "foo"); |
| 387 | copy_to_msg(msg, ass_cmd, sizeof(ass_cmd)); |
| 388 | parsed = bsc_nat_parse(msg); |
| 389 | if (bsc_mgcp_assign(&con, msg) != 0) { |
| 390 | fprintf(stderr, "Failed to handle assignment.\n"); |
| 391 | abort(); |
| 392 | } |
| 393 | |
| 394 | if (con.msc_timeslot != 21) { |
| 395 | fprintf(stderr, "Timeslot should be 21.\n"); |
| 396 | abort(); |
| 397 | } |
| 398 | |
| 399 | if (con.bsc_timeslot != 21) { |
| 400 | fprintf(stderr, "Assigned timeslot should have been 21.\n"); |
| 401 | abort(); |
| 402 | } |
| 403 | talloc_free(parsed); |
| 404 | |
| 405 | bsc_mgcp_clear(&con); |
| 406 | if (con.bsc_timeslot != -1 || con.msc_timeslot != -1) { |
| 407 | fprintf(stderr, "Clearing should remove the mapping.\n"); |
| 408 | abort(); |
| 409 | } |
| 410 | } |
| 411 | |
Holger Hans Peter Freyther | 8917998 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 412 | /* test the code to find a given connection */ |
| 413 | static void test_mgcp_find(void) |
| 414 | { |
| 415 | struct bsc_nat *nat; |
| 416 | struct bsc_connection *con; |
| 417 | struct sccp_connections *sccp_con; |
| 418 | |
| 419 | fprintf(stderr, "Testing finding of a BSC Connection\n"); |
| 420 | |
| 421 | nat = bsc_nat_alloc(); |
| 422 | con = bsc_connection_alloc(nat); |
| 423 | llist_add(&con->list_entry, &nat->bsc_connections); |
| 424 | |
| 425 | sccp_con = talloc_zero(con, struct sccp_connections); |
| 426 | sccp_con->msc_timeslot = 12; |
| 427 | sccp_con->bsc_timeslot = 12; |
| 428 | sccp_con->bsc = con; |
| 429 | llist_add(&sccp_con->list_entry, &nat->sccp_connections); |
| 430 | |
| 431 | if (bsc_mgcp_find_con(nat, 11) != NULL) { |
| 432 | fprintf(stderr, "Found the wrong connection.\n"); |
| 433 | abort(); |
| 434 | } |
| 435 | |
Holger Hans Peter Freyther | 98d15ef | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 436 | if (bsc_mgcp_find_con(nat, 12) != sccp_con) { |
Holger Hans Peter Freyther | 8917998 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 437 | fprintf(stderr, "Didn't find the connection\n"); |
| 438 | abort(); |
| 439 | } |
| 440 | |
| 441 | sccp_con->msc_timeslot = 0; |
| 442 | sccp_con->bsc_timeslot = 0; |
Holger Hans Peter Freyther | 98d15ef | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 443 | if (bsc_mgcp_find_con(nat, 1) != sccp_con) { |
Holger Hans Peter Freyther | 8917998 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 444 | fprintf(stderr, "Didn't find the connection\n"); |
| 445 | abort(); |
| 446 | } |
| 447 | |
| 448 | /* free everything */ |
| 449 | talloc_free(nat); |
| 450 | } |
| 451 | |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 452 | static void test_mgcp_rewrite(void) |
| 453 | { |
| 454 | int i; |
Holger Hans Peter Freyther | 0d0aaa6 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 455 | struct msgb *output; |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 456 | fprintf(stderr, "Test rewriting MGCP messages.\n"); |
| 457 | |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 458 | for (i = 0; i < ARRAY_SIZE(mgcp_messages); ++i) { |
| 459 | const char *orig = mgcp_messages[i].orig; |
| 460 | const char *patc = mgcp_messages[i].patch; |
| 461 | const char *ip = mgcp_messages[i].ip; |
| 462 | const int port = mgcp_messages[i].port; |
| 463 | |
Holger Hans Peter Freyther | 0d0aaa6 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 464 | char *input = strdup(orig); |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 465 | |
Holger Hans Peter Freyther | 0d0aaa6 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 466 | output = bsc_mgcp_rewrite(input, strlen(input), ip, port); |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 467 | if (msgb_l2len(output) != strlen(patc)) { |
| 468 | fprintf(stderr, "Wrong sizes for test: %d %d != %d != %d\n", i, msgb_l2len(output), strlen(patc), strlen(orig)); |
| 469 | fprintf(stderr, "String '%s' vs '%s'\n", (const char *) output->l2h, patc); |
| 470 | abort(); |
| 471 | } |
| 472 | |
| 473 | if (memcmp(output->l2h, patc, msgb_l2len(output)) != 0) { |
| 474 | fprintf(stderr, "Broken on %d msg: '%s'\n", i, (const char *) output->l2h); |
| 475 | abort(); |
| 476 | } |
| 477 | |
| 478 | msgb_free(output); |
Holger Hans Peter Freyther | 0d0aaa6 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 479 | free(input); |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 480 | } |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 481 | } |
| 482 | |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 483 | static void test_mgcp_parse(void) |
| 484 | { |
| 485 | int code, ci; |
| 486 | char transaction[60]; |
| 487 | |
| 488 | fprintf(stderr, "Test MGCP response parsing.\n"); |
| 489 | |
| 490 | if (bsc_mgcp_parse_response(crcx_resp, &code, transaction) != 0) { |
| 491 | fprintf(stderr, "Failed to parse CRCX resp.\n"); |
| 492 | abort(); |
| 493 | } |
| 494 | |
| 495 | if (code != 200) { |
| 496 | fprintf(stderr, "Failed to parse the CODE properly. Got: %d\n", code); |
| 497 | abort(); |
| 498 | } |
| 499 | |
| 500 | if (strcmp(transaction, "23265295") != 0) { |
| 501 | fprintf(stderr, "Failed to parse transaction id: '%s'\n", transaction); |
| 502 | abort(); |
| 503 | } |
| 504 | |
| 505 | ci = bsc_mgcp_extract_ci(crcx_resp); |
| 506 | if (ci != 1) { |
| 507 | fprintf(stderr, "Failed to parse the CI. Got: %d\n", ci); |
| 508 | abort(); |
| 509 | } |
| 510 | } |
| 511 | |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 512 | int main(int argc, char **argv) |
| 513 | { |
| 514 | struct debug_target *stderr_target; |
| 515 | |
| 516 | stderr_target = debug_target_create_stderr(); |
| 517 | debug_add_target(stderr_target); |
| 518 | debug_set_all_filter(stderr_target, 1); |
| 519 | |
| 520 | test_filter(); |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 521 | test_contrack(); |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 522 | test_paging(); |
Holger Hans Peter Freyther | 8917998 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 523 | test_mgcp_ass_tracking(); |
| 524 | test_mgcp_find(); |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 525 | test_mgcp_rewrite(); |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 526 | test_mgcp_parse(); |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 527 | return 0; |
| 528 | } |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 529 | |
| 530 | void input_event() |
| 531 | {} |
| 532 | int nm_state_event() |
| 533 | { |
| 534 | return -1; |
| 535 | } |
| 536 | |
| 537 | int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id) |
| 538 | { |
| 539 | return -1; |
| 540 | } |