Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1 | /* |
| 2 | * BSC NAT Message filtering |
| 3 | * |
Holger Hans Peter Freyther | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 4 | * (C) 2010-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * (C) 2010-2012 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 |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 10 | * it under the terms of the GNU Affero General Public License as published by |
| 11 | * the Free Software Foundation; either version 3 of the License, or |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 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 | * |
Harald Welte | 0e3e88e | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 21 | * |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | #include <openbsc/debug.h> |
| 26 | #include <openbsc/gsm_data.h> |
| 27 | #include <openbsc/bsc_nat.h> |
Holger Hans Peter Freyther | 4a9dd3b | 2010-07-31 05:17:17 +0800 | [diff] [blame] | 28 | #include <openbsc/bsc_nat_sccp.h> |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 29 | |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 30 | #include <osmocom/core/application.h> |
Pablo Neira Ayuso | 93bcc47 | 2011-03-26 22:08:53 +0100 | [diff] [blame] | 31 | #include <osmocom/core/talloc.h> |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 32 | |
Harald Welte | 90e5eae | 2010-08-03 15:11:51 +0200 | [diff] [blame] | 33 | #include <osmocom/sccp/sccp.h> |
Pablo Neira Ayuso | 93bcc47 | 2011-03-26 22:08:53 +0100 | [diff] [blame] | 34 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
Holger Hans Peter Freyther | a8dffc1 | 2010-07-30 02:53:14 +0800 | [diff] [blame] | 35 | |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 36 | #include <stdio.h> |
| 37 | |
| 38 | /* test messages for ipa */ |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 39 | static uint8_t ipa_id[] = { |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 40 | 0x00, 0x01, 0xfe, 0x06, |
| 41 | }; |
| 42 | |
| 43 | /* SCCP messages are below */ |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 44 | static uint8_t gsm_reset[] = { |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 45 | 0x00, 0x12, 0xfd, |
| 46 | 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe, |
| 47 | 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04, |
| 48 | 0x01, 0x20, |
| 49 | }; |
| 50 | |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 51 | static const uint8_t gsm_reset_ack[] = { |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 52 | 0x00, 0x13, 0xfd, |
| 53 | 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01, |
| 54 | 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03, |
| 55 | 0x00, 0x01, 0x31, |
| 56 | }; |
| 57 | |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 58 | static const uint8_t gsm_paging[] = { |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 59 | 0x00, 0x20, 0xfd, |
| 60 | 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01, |
| 61 | 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x10, |
| 62 | 0x00, 0x0e, 0x52, 0x08, 0x08, 0x29, 0x47, 0x10, |
| 63 | 0x02, 0x01, 0x31, 0x97, 0x61, 0x1a, 0x01, 0x06, |
| 64 | }; |
| 65 | |
| 66 | /* BSC -> MSC connection open */ |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 67 | static const uint8_t bssmap_cr[] = { |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 68 | 0x00, 0x2c, 0xfd, |
| 69 | 0x01, 0x01, 0x02, 0x03, 0x02, 0x02, 0x04, 0x02, |
| 70 | 0x42, 0xfe, 0x0f, 0x1f, 0x00, 0x1d, 0x57, 0x05, |
| 71 | 0x08, 0x00, 0x72, 0xf4, 0x80, 0x20, 0x12, 0xc3, |
| 72 | 0x50, 0x17, 0x10, 0x05, 0x24, 0x11, 0x03, 0x33, |
| 73 | 0x19, 0xa2, 0x08, 0x29, 0x47, 0x10, 0x02, 0x01, |
| 74 | 0x31, 0x97, 0x61, 0x00 |
| 75 | }; |
| 76 | |
| 77 | /* MSC -> BSC connection confirm */ |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 78 | static const uint8_t bssmap_cc[] = { |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 79 | 0x00, 0x0a, 0xfd, |
| 80 | 0x02, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, |
| 81 | }; |
| 82 | |
| 83 | /* MSC -> BSC released */ |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 84 | static const uint8_t bssmap_released[] = { |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 85 | 0x00, 0x0e, 0xfd, |
| 86 | 0x04, 0x00, 0x00, 0x03, 0x01, 0x02, 0x03, 0x00, 0x01, 0x0f, |
| 87 | 0x02, 0x23, 0x42, 0x00, |
| 88 | }; |
| 89 | |
| 90 | /* BSC -> MSC released */ |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 91 | static const uint8_t bssmap_release_complete[] = { |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 92 | 0x00, 0x07, 0xfd, |
| 93 | 0x05, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03 |
| 94 | }; |
| 95 | |
Holger Hans Peter Freyther | 55e8f55 | 2010-04-06 10:22:01 +0200 | [diff] [blame] | 96 | /* both directions IT timer */ |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 97 | static const uint8_t connnection_it[] = { |
Holger Hans Peter Freyther | 55e8f55 | 2010-04-06 10:22:01 +0200 | [diff] [blame] | 98 | 0x00, 0x0b, 0xfd, |
| 99 | 0x10, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03, |
| 100 | 0x00, 0x00, 0x00, 0x00, |
| 101 | }; |
| 102 | |
Holger Hans Peter Freyther | dd6c51b | 2010-04-21 15:45:26 +0800 | [diff] [blame] | 103 | /* error in both directions */ |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 104 | static const uint8_t proto_error[] = { |
Holger Hans Peter Freyther | dd6c51b | 2010-04-21 15:45:26 +0800 | [diff] [blame] | 105 | 0x00, 0x05, 0xfd, |
| 106 | 0x0f, 0x22, 0x33, 0x44, 0x00, |
| 107 | }; |
| 108 | |
Holger Hans Peter Freyther | 6b99828 | 2010-04-01 08:47:12 +0200 | [diff] [blame] | 109 | /* MGCP wrap... */ |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 110 | static const uint8_t mgcp_msg[] = { |
Holger Hans Peter Freyther | 6b99828 | 2010-04-01 08:47:12 +0200 | [diff] [blame] | 111 | 0x00, 0x03, 0xfc, |
| 112 | 0x20, 0x20, 0x20, |
| 113 | }; |
| 114 | |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 115 | /* location updating request */ |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 116 | static const uint8_t bss_lu[] = { |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 117 | 0x00, 0x2e, 0xfd, |
| 118 | 0x01, 0x91, 0x45, 0x14, 0x02, 0x02, 0x04, 0x02, |
| 119 | 0x42, 0xfe, 0x0f, 0x21, 0x00, 0x1f, 0x57, 0x05, |
| 120 | 0x08, 0x00, 0x72, 0xf4, 0x80, 0x20, 0x14, 0xc3, |
| 121 | 0x50, 0x17, 0x12, 0x05, 0x08, 0x70, 0x72, 0xf4, |
| 122 | 0x80, 0xff, 0xfe, 0x30, 0x08, 0x29, 0x44, 0x50, |
| 123 | 0x12, 0x03, 0x24, 0x01, 0x95, 0x00 |
| 124 | }; |
| 125 | |
Holger Hans Peter Freyther | ecaa0b1 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 126 | /* paging response */ |
| 127 | static const uint8_t pag_resp[] = { |
| 128 | 0x00, 0x2c, 0xfd, 0x01, 0xe5, 0x68, |
| 129 | 0x14, 0x02, 0x02, 0x04, 0x02, 0x42, 0xfe, 0x0f, |
| 130 | 0x1f, 0x00, 0x1d, 0x57, 0x05, 0x08, 0x00, 0x72, |
| 131 | 0xf4, 0x80, 0x20, 0x16, 0xc3, 0x50, 0x17, 0x10, |
| 132 | 0x06, 0x27, 0x01, 0x03, 0x30, 0x18, 0x96, 0x08, |
| 133 | 0x29, 0x26, 0x30, 0x32, 0x11, 0x42, 0x01, 0x19, |
| 134 | 0x00 |
| 135 | }; |
| 136 | |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 137 | struct filter_result { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 138 | const uint8_t *data; |
Holger Hans Peter Freyther | bd11639 | 2010-07-23 19:09:21 +0800 | [diff] [blame] | 139 | const uint16_t length; |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 140 | const int dir; |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 141 | const int result; |
| 142 | }; |
| 143 | |
| 144 | static const struct filter_result results[] = { |
| 145 | { |
| 146 | .data = ipa_id, |
| 147 | .length = ARRAY_SIZE(ipa_id), |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 148 | .dir = DIR_MSC, |
| 149 | .result = 1, |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 150 | }, |
| 151 | { |
| 152 | .data = gsm_reset, |
| 153 | .length = ARRAY_SIZE(gsm_reset), |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 154 | .dir = DIR_MSC, |
| 155 | .result = 1, |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 156 | }, |
| 157 | { |
| 158 | .data = gsm_reset_ack, |
| 159 | .length = ARRAY_SIZE(gsm_reset_ack), |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 160 | .dir = DIR_BSC, |
| 161 | .result = 1, |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 162 | }, |
| 163 | { |
| 164 | .data = gsm_paging, |
| 165 | .length = ARRAY_SIZE(gsm_paging), |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 166 | .dir = DIR_BSC, |
| 167 | .result = 0, |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 168 | }, |
| 169 | { |
| 170 | .data = bssmap_cr, |
| 171 | .length = ARRAY_SIZE(bssmap_cr), |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 172 | .dir = DIR_MSC, |
| 173 | .result = 0, |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 174 | }, |
| 175 | { |
| 176 | .data = bssmap_cc, |
| 177 | .length = ARRAY_SIZE(bssmap_cc), |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 178 | .dir = DIR_BSC, |
| 179 | .result = 0, |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 180 | }, |
| 181 | { |
| 182 | .data = bssmap_released, |
| 183 | .length = ARRAY_SIZE(bssmap_released), |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 184 | .dir = DIR_MSC, |
| 185 | .result = 0, |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 186 | }, |
| 187 | { |
| 188 | .data = bssmap_release_complete, |
| 189 | .length = ARRAY_SIZE(bssmap_release_complete), |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 190 | .dir = DIR_BSC, |
| 191 | .result = 0, |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 192 | }, |
Holger Hans Peter Freyther | 6b99828 | 2010-04-01 08:47:12 +0200 | [diff] [blame] | 193 | { |
| 194 | .data = mgcp_msg, |
| 195 | .length = ARRAY_SIZE(mgcp_msg), |
| 196 | .dir = DIR_MSC, |
| 197 | .result = 0, |
| 198 | }, |
Holger Hans Peter Freyther | 55e8f55 | 2010-04-06 10:22:01 +0200 | [diff] [blame] | 199 | { |
| 200 | .data = connnection_it, |
| 201 | .length = ARRAY_SIZE(connnection_it), |
| 202 | .dir = DIR_BSC, |
| 203 | .result = 0, |
| 204 | }, |
| 205 | { |
| 206 | .data = connnection_it, |
| 207 | .length = ARRAY_SIZE(connnection_it), |
| 208 | .dir = DIR_MSC, |
| 209 | .result = 0, |
| 210 | }, |
Holger Hans Peter Freyther | dd6c51b | 2010-04-21 15:45:26 +0800 | [diff] [blame] | 211 | { |
| 212 | .data = proto_error, |
| 213 | .length = ARRAY_SIZE(proto_error), |
| 214 | .dir = DIR_BSC, |
| 215 | .result = 0, |
| 216 | }, |
| 217 | { |
| 218 | .data = proto_error, |
| 219 | .length = ARRAY_SIZE(proto_error), |
| 220 | .dir = DIR_MSC, |
| 221 | .result = 0, |
| 222 | }, |
| 223 | |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 224 | }; |
| 225 | |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 226 | static void test_filter(void) |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 227 | { |
| 228 | int i; |
| 229 | |
| 230 | |
| 231 | /* start testinh with proper messages */ |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 232 | printf("Testing BSS Filtering.\n"); |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 233 | for (i = 0; i < ARRAY_SIZE(results); ++i) { |
| 234 | int result; |
| 235 | struct bsc_nat_parsed *parsed; |
| 236 | struct msgb *msg = msgb_alloc(4096, "test-message"); |
| 237 | |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 238 | printf("Going to test item: %d\n", i); |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 239 | memcpy(msg->data, results[i].data, results[i].length); |
| 240 | msg->l2h = msgb_put(msg, results[i].length); |
| 241 | |
| 242 | parsed = bsc_nat_parse(msg); |
| 243 | if (!parsed) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 244 | printf("FAIL: Failed to parse the message\n"); |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 245 | continue; |
| 246 | } |
| 247 | |
Holger Hans Peter Freyther | bbf6b65 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 248 | result = bsc_nat_filter_ipa(results[i].dir, msg, parsed); |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 249 | if (result != results[i].result) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 250 | printf("FAIL: Not the expected result got: %d wanted: %d\n", |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 251 | result, results[i].result); |
| 252 | } |
| 253 | |
| 254 | msgb_free(msg); |
| 255 | } |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 256 | } |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 257 | |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 258 | #include "bsc_data.c" |
| 259 | |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 260 | static void copy_to_msg(struct msgb *msg, const uint8_t *data, unsigned int length) |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 261 | { |
| 262 | msgb_reset(msg); |
| 263 | msg->l2h = msgb_put(msg, length); |
| 264 | memcpy(msg->l2h, data, msgb_l2len(msg)); |
| 265 | } |
| 266 | |
Holger Hans Peter Freyther | 75e6122 | 2012-01-10 22:31:39 +0100 | [diff] [blame] | 267 | static void verify_msg(struct msgb *out, const uint8_t *ref, int ref_len) |
| 268 | { |
| 269 | if (out->len != ref_len) { |
Holger Hans Peter Freyther | bbb102c | 2012-01-18 17:20:23 +0100 | [diff] [blame] | 270 | printf("FAIL: The size should match: %d vs. %d\n", |
| 271 | out->len, ref_len); |
| 272 | printf("%s\n", osmo_hexdump(out->data, out->len)); |
| 273 | printf("Wanted\n"); |
| 274 | printf("%s\n", osmo_hexdump(ref, ref_len)); |
Holger Hans Peter Freyther | 75e6122 | 2012-01-10 22:31:39 +0100 | [diff] [blame] | 275 | abort(); |
| 276 | } |
| 277 | |
| 278 | if (memcmp(out->data, ref, out->len) != 0) { |
| 279 | printf("FAIL: the data should be changed.\n"); |
| 280 | printf("%s\n", osmo_hexdump(out->data, out->len)); |
| 281 | printf("Wanted\n"); |
| 282 | printf("%s\n", osmo_hexdump(ref, ref_len)); |
| 283 | abort(); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 288 | #define VERIFY(con_found, con, msg, ver, str) \ |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 289 | if (!con_found) { \ |
| 290 | printf("Failed to find connection.\n"); \ |
| 291 | abort(); \ |
| 292 | } \ |
| 293 | if (con_found->bsc != con) { \ |
| 294 | printf("Got connection of the wrong BSC: %d\n", \ |
| 295 | con_found->bsc->cfg->nr); \ |
| 296 | abort(); \ |
| 297 | } \ |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 298 | if (memcmp(msg->data, ver, sizeof(ver)) != 0) { \ |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 299 | printf("Failed to patch the %s msg.\n", str); \ |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 300 | abort(); \ |
| 301 | } |
| 302 | |
| 303 | /* test conn tracking once */ |
| 304 | static void test_contrack() |
| 305 | { |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 306 | struct bsc_nat *nat; |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 307 | struct bsc_connection *con; |
| 308 | struct sccp_connections *con_found; |
Holger Hans Peter Freyther | e1a1648 | 2010-05-16 16:51:31 +0800 | [diff] [blame] | 309 | struct sccp_connections *rc_con; |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 310 | struct bsc_nat_parsed *parsed; |
| 311 | struct msgb *msg; |
| 312 | |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 313 | printf("Testing connection tracking.\n"); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 314 | nat = bsc_nat_alloc(); |
| 315 | con = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 316 | con->cfg = bsc_config_alloc(nat, "foo"); |
| 317 | bsc_config_add_lac(con->cfg, 23); |
| 318 | bsc_config_add_lac(con->cfg, 49); |
| 319 | bsc_config_add_lac(con->cfg, 42); |
| 320 | bsc_config_del_lac(con->cfg, 49); |
| 321 | bsc_config_add_lac(con->cfg, 1111); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 322 | msg = msgb_alloc(4096, "test"); |
| 323 | |
| 324 | /* 1.) create a connection */ |
| 325 | copy_to_msg(msg, bsc_cr, sizeof(bsc_cr)); |
| 326 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | da30c4b | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 327 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, con); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 328 | if (con_found != NULL) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 329 | printf("Con should not exist realref(%u)\n", |
| 330 | sccp_src_ref_to_int(&con_found->real_ref)); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 331 | abort(); |
| 332 | } |
Holger Hans Peter Freyther | e1a1648 | 2010-05-16 16:51:31 +0800 | [diff] [blame] | 333 | rc_con = create_sccp_src_ref(con, parsed); |
| 334 | if (!rc_con) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 335 | printf("Failed to create a ref\n"); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 336 | abort(); |
| 337 | } |
Holger Hans Peter Freyther | da30c4b | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 338 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, con); |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 339 | if (!con_found) { |
| 340 | printf("Failed to find connection.\n"); |
| 341 | abort(); |
| 342 | } |
| 343 | if (con_found->bsc != con) { |
| 344 | printf("Got connection of the wrong BSC: %d\n", |
| 345 | con_found->bsc->cfg->nr); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 346 | abort(); |
| 347 | } |
Holger Hans Peter Freyther | e1a1648 | 2010-05-16 16:51:31 +0800 | [diff] [blame] | 348 | if (con_found != rc_con) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 349 | printf("Failed to find the right connection.\n"); |
Holger Hans Peter Freyther | e1a1648 | 2010-05-16 16:51:31 +0800 | [diff] [blame] | 350 | abort(); |
| 351 | } |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 352 | if (memcmp(msg->data, bsc_cr_patched, sizeof(bsc_cr_patched)) != 0) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 353 | printf("Failed to patch the BSC CR msg.\n"); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 354 | abort(); |
| 355 | } |
| 356 | talloc_free(parsed); |
| 357 | |
| 358 | /* 2.) get the cc */ |
| 359 | copy_to_msg(msg, msc_cc, sizeof(msc_cc)); |
| 360 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | 75ee841 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 361 | con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 362 | VERIFY(con_found, con, msg, msc_cc_patched, "MSC CC"); |
| 363 | if (update_sccp_src_ref(con_found, parsed) != 0) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 364 | printf("Failed to update the SCCP con.\n"); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 365 | abort(); |
| 366 | } |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 367 | |
| 368 | /* 3.) send some data */ |
| 369 | copy_to_msg(msg, bsc_dtap, sizeof(bsc_dtap)); |
| 370 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | da30c4b | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 371 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, con); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 372 | VERIFY(con_found, con, msg, bsc_dtap_patched, "BSC DTAP"); |
| 373 | |
| 374 | /* 4.) receive some data */ |
| 375 | copy_to_msg(msg, msc_dtap, sizeof(msc_dtap)); |
| 376 | parsed = bsc_nat_parse(msg); |
| 377 | con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 378 | VERIFY(con_found, con, msg, msc_dtap_patched, "MSC DTAP"); |
| 379 | |
| 380 | /* 5.) close the connection */ |
| 381 | copy_to_msg(msg, msc_rlsd, sizeof(msc_rlsd)); |
| 382 | parsed = bsc_nat_parse(msg); |
| 383 | con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 384 | VERIFY(con_found, con, msg, msc_rlsd_patched, "MSC RLSD"); |
| 385 | |
| 386 | /* 6.) confirm the connection close */ |
| 387 | copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc)); |
| 388 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | da30c4b | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 389 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, con); |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 390 | if (!con_found) { |
| 391 | printf("Failed to find connection.\n"); |
| 392 | abort(); |
| 393 | } |
| 394 | if (con_found->bsc != con) { |
| 395 | printf("Got connection of the wrong BSC: %d\n", |
| 396 | con_found->bsc->cfg->nr); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 397 | abort(); |
| 398 | } |
| 399 | if (memcmp(msg->data, bsc_rlc_patched, sizeof(bsc_rlc_patched)) != 0) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 400 | printf("Failed to patch the BSC CR msg.\n"); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 401 | abort(); |
| 402 | } |
| 403 | remove_sccp_src_ref(con, msg, parsed); |
Holger Hans Peter Freyther | bedecf3 | 2010-04-05 21:44:51 +0200 | [diff] [blame] | 404 | talloc_free(parsed); |
| 405 | |
| 406 | copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc)); |
| 407 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | da30c4b | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 408 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, con); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 409 | |
| 410 | /* verify that it is gone */ |
| 411 | if (con_found != NULL) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 412 | printf("Con should not exist real_ref(%u)\n", |
| 413 | sccp_src_ref_to_int(&con_found->real_ref)); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 414 | abort(); |
| 415 | } |
| 416 | talloc_free(parsed); |
| 417 | |
| 418 | |
Holger Hans Peter Freyther | 187221e | 2011-02-27 11:18:41 +0100 | [diff] [blame] | 419 | bsc_config_free(con->cfg); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 420 | talloc_free(nat); |
| 421 | msgb_free(msg); |
| 422 | } |
| 423 | |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 424 | static void test_paging(void) |
| 425 | { |
| 426 | struct bsc_nat *nat; |
| 427 | struct bsc_connection *con; |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 428 | struct bsc_config *cfg; |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 429 | |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 430 | printf("Testing paging by lac.\n"); |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 431 | |
| 432 | nat = bsc_nat_alloc(); |
| 433 | con = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 434 | cfg = bsc_config_alloc(nat, "unknown"); |
| 435 | con->cfg = cfg; |
| 436 | bsc_config_add_lac(cfg, 23); |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 437 | con->authenticated = 1; |
| 438 | llist_add(&con->list_entry, &nat->bsc_connections); |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 439 | |
| 440 | /* Test it by not finding it */ |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 441 | if (bsc_config_handles_lac(cfg, 8213) != 0) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 442 | printf("Should not be handled.\n"); |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 443 | abort(); |
| 444 | } |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 445 | |
| 446 | /* Test by finding it */ |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 447 | bsc_config_del_lac(cfg, 23); |
| 448 | bsc_config_add_lac(cfg, 8213); |
Holger Hans Peter Freyther | b1eb94f | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 449 | if (bsc_config_handles_lac(cfg, 8213) == 0) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 450 | printf("Should have found it.\n"); |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 451 | abort(); |
| 452 | } |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 453 | } |
| 454 | |
Holger Hans Peter Freyther | 473b38a | 2011-02-26 11:38:00 +0100 | [diff] [blame] | 455 | static void test_mgcp_allocations(void) |
| 456 | { |
| 457 | #if 0 |
| 458 | struct bsc_connection *bsc; |
| 459 | struct bsc_nat *nat; |
| 460 | struct sccp_connections con; |
Holger Hans Peter Freyther | b28f5a0 | 2011-02-27 11:04:27 +0100 | [diff] [blame] | 461 | int i, j, multiplex; |
Holger Hans Peter Freyther | 473b38a | 2011-02-26 11:38:00 +0100 | [diff] [blame] | 462 | |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 463 | printf("Testing MGCP.\n"); |
Holger Hans Peter Freyther | 473b38a | 2011-02-26 11:38:00 +0100 | [diff] [blame] | 464 | memset(&con, 0, sizeof(con)); |
| 465 | |
| 466 | nat = bsc_nat_alloc(); |
| 467 | nat->bsc_endpoints = talloc_zero_array(nat, |
| 468 | struct bsc_endpoint, |
| 469 | 65); |
| 470 | nat->mgcp_cfg = mgcp_config_alloc(); |
Holger Hans Peter Freyther | ab56ce1 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 471 | nat->mgcp_cfg->trunk.number_endpoints = 64; |
Holger Hans Peter Freyther | 473b38a | 2011-02-26 11:38:00 +0100 | [diff] [blame] | 472 | |
| 473 | bsc = bsc_connection_alloc(nat); |
| 474 | bsc->cfg = bsc_config_alloc(nat, "foo"); |
Holger Hans Peter Freyther | b28f5a0 | 2011-02-27 11:04:27 +0100 | [diff] [blame] | 475 | bsc->cfg->max_endpoints = 60; |
Holger Hans Peter Freyther | 473b38a | 2011-02-26 11:38:00 +0100 | [diff] [blame] | 476 | bsc_config_add_lac(bsc->cfg, 2323); |
| 477 | bsc->last_endpoint = 0x22; |
| 478 | con.bsc = bsc; |
| 479 | |
| 480 | bsc_init_endps_if_needed(bsc); |
| 481 | |
| 482 | i = 1; |
| 483 | do { |
| 484 | if (bsc_assign_endpoint(bsc, &con) != 0) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 485 | printf("failed to allocate... on iteration %d\n", i); |
Holger Hans Peter Freyther | 473b38a | 2011-02-26 11:38:00 +0100 | [diff] [blame] | 486 | break; |
| 487 | } |
| 488 | ++i; |
| 489 | } while(1); |
| 490 | |
Holger Hans Peter Freyther | b28f5a0 | 2011-02-27 11:04:27 +0100 | [diff] [blame] | 491 | multiplex = bsc_mgcp_nr_multiplexes(bsc->cfg->max_endpoints); |
| 492 | for (i = 0; i < multiplex; ++i) { |
Holger Hans Peter Freyther | 473b38a | 2011-02-26 11:38:00 +0100 | [diff] [blame] | 493 | for (j = 0; j < 32; ++j) |
| 494 | printf("%d", bsc->_endpoint_status[i*32 + j]); |
| 495 | printf(": %d of %d\n", i*32 + 32, 32 * 8); |
| 496 | } |
| 497 | #endif |
| 498 | } |
| 499 | |
Holger Hans Peter Freyther | 8917998 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 500 | static void test_mgcp_ass_tracking(void) |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 501 | { |
Holger Hans Peter Freyther | 3633095 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 502 | struct bsc_connection *bsc; |
| 503 | struct bsc_nat *nat; |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 504 | struct sccp_connections con; |
| 505 | struct bsc_nat_parsed *parsed; |
| 506 | struct msgb *msg; |
| 507 | |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 508 | printf("Testing MGCP.\n"); |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 509 | memset(&con, 0, sizeof(con)); |
| 510 | |
Holger Hans Peter Freyther | 3633095 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 511 | nat = bsc_nat_alloc(); |
| 512 | nat->bsc_endpoints = talloc_zero_array(nat, |
| 513 | struct bsc_endpoint, |
| 514 | 33); |
Holger Hans Peter Freyther | 55971c3 | 2011-02-25 12:43:58 +0100 | [diff] [blame] | 515 | nat->mgcp_cfg = mgcp_config_alloc(); |
Holger Hans Peter Freyther | ab56ce1 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 516 | nat->mgcp_cfg->trunk.number_endpoints = 64; |
Holger Hans Peter Freyther | 55971c3 | 2011-02-25 12:43:58 +0100 | [diff] [blame] | 517 | |
Holger Hans Peter Freyther | 3633095 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 518 | bsc = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 519 | bsc->cfg = bsc_config_alloc(nat, "foo"); |
| 520 | bsc_config_add_lac(bsc->cfg, 2323); |
Holger Hans Peter Freyther | ab80bd8 | 2011-02-25 17:10:25 +0100 | [diff] [blame] | 521 | bsc->last_endpoint = 0x1e; |
Holger Hans Peter Freyther | 3633095 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 522 | con.bsc = bsc; |
| 523 | |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 524 | msg = msgb_alloc(4096, "foo"); |
| 525 | copy_to_msg(msg, ass_cmd, sizeof(ass_cmd)); |
| 526 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | 4bcb770 | 2010-09-20 01:21:51 +0800 | [diff] [blame] | 527 | |
| 528 | if (msg->l2h[16] != 0 || |
| 529 | msg->l2h[17] != 0x1) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 530 | printf("Input is not as expected.. %s 0x%x\n", |
Pablo Neira Ayuso | b1d5a69 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 531 | osmo_hexdump(msg->l2h, msgb_l2len(msg)), |
Holger Hans Peter Freyther | 4bcb770 | 2010-09-20 01:21:51 +0800 | [diff] [blame] | 532 | msg->l2h[17]); |
| 533 | abort(); |
| 534 | } |
| 535 | |
Holger Hans Peter Freyther | 926c5fd | 2010-08-28 18:22:14 +0800 | [diff] [blame] | 536 | if (bsc_mgcp_assign_patch(&con, msg) != 0) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 537 | printf("Failed to handle assignment.\n"); |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 538 | abort(); |
| 539 | } |
| 540 | |
Holger Hans Peter Freyther | 4bcb770 | 2010-09-20 01:21:51 +0800 | [diff] [blame] | 541 | if (con.msc_endp != 1) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 542 | printf("Timeslot should be 1.\n"); |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 543 | abort(); |
| 544 | } |
| 545 | |
Holger Hans Peter Freyther | ab80bd8 | 2011-02-25 17:10:25 +0100 | [diff] [blame] | 546 | if (con.bsc_endp != 0x1) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 547 | printf("Assigned timeslot should have been 1.\n"); |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 548 | abort(); |
| 549 | } |
Holger Hans Peter Freyther | ab80bd8 | 2011-02-25 17:10:25 +0100 | [diff] [blame] | 550 | if (con.bsc->_endpoint_status[0x1] != 1) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 551 | printf("The status on the BSC is wrong.\n"); |
Holger Hans Peter Freyther | 926c5fd | 2010-08-28 18:22:14 +0800 | [diff] [blame] | 552 | abort(); |
| 553 | } |
| 554 | |
Holger Hans Peter Freyther | 4bcb770 | 2010-09-20 01:21:51 +0800 | [diff] [blame] | 555 | int multiplex, timeslot; |
Holger Hans Peter Freyther | ab80bd8 | 2011-02-25 17:10:25 +0100 | [diff] [blame] | 556 | mgcp_endpoint_to_timeslot(0x1, &multiplex, ×lot); |
Holger Hans Peter Freyther | 4bcb770 | 2010-09-20 01:21:51 +0800 | [diff] [blame] | 557 | |
| 558 | uint16_t cic = htons(timeslot & 0x1f); |
| 559 | if (memcmp(&cic, &msg->l2h[16], sizeof(cic)) != 0) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 560 | printf("Message was not patched properly\n"); |
| 561 | printf("data cic: 0x%x %s\n", cic, osmo_hexdump(msg->l2h, msgb_l2len(msg))); |
Holger Hans Peter Freyther | 4bcb770 | 2010-09-20 01:21:51 +0800 | [diff] [blame] | 562 | abort(); |
| 563 | } |
| 564 | |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 565 | talloc_free(parsed); |
| 566 | |
Holger Hans Peter Freyther | 3633095 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 567 | bsc_mgcp_dlcx(&con); |
Holger Hans Peter Freyther | e62b766 | 2010-09-19 20:54:21 +0800 | [diff] [blame] | 568 | if (con.bsc_endp != -1 || con.msc_endp != -1 || |
Holger Hans Peter Freyther | ab80bd8 | 2011-02-25 17:10:25 +0100 | [diff] [blame] | 569 | con.bsc->_endpoint_status[1] != 0 || con.bsc->last_endpoint != 0x1) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 570 | printf("Clearing should remove the mapping.\n"); |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 571 | abort(); |
| 572 | } |
Holger Hans Peter Freyther | 3633095 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 573 | |
Holger Hans Peter Freyther | 187221e | 2011-02-27 11:18:41 +0100 | [diff] [blame] | 574 | bsc_config_free(bsc->cfg); |
Holger Hans Peter Freyther | 3633095 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 575 | talloc_free(nat); |
Holger Hans Peter Freyther | dccb915 | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 576 | } |
| 577 | |
Holger Hans Peter Freyther | 8917998 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 578 | /* test the code to find a given connection */ |
| 579 | static void test_mgcp_find(void) |
| 580 | { |
| 581 | struct bsc_nat *nat; |
| 582 | struct bsc_connection *con; |
| 583 | struct sccp_connections *sccp_con; |
| 584 | |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 585 | printf("Testing finding of a BSC Connection\n"); |
Holger Hans Peter Freyther | 8917998 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 586 | |
| 587 | nat = bsc_nat_alloc(); |
| 588 | con = bsc_connection_alloc(nat); |
| 589 | llist_add(&con->list_entry, &nat->bsc_connections); |
| 590 | |
| 591 | sccp_con = talloc_zero(con, struct sccp_connections); |
Holger Hans Peter Freyther | 7089d53 | 2010-08-28 16:08:39 +0800 | [diff] [blame] | 592 | sccp_con->msc_endp = 12; |
| 593 | sccp_con->bsc_endp = 12; |
Holger Hans Peter Freyther | 8917998 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 594 | sccp_con->bsc = con; |
| 595 | llist_add(&sccp_con->list_entry, &nat->sccp_connections); |
| 596 | |
| 597 | if (bsc_mgcp_find_con(nat, 11) != NULL) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 598 | printf("Found the wrong connection.\n"); |
Holger Hans Peter Freyther | 8917998 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 599 | abort(); |
| 600 | } |
| 601 | |
Holger Hans Peter Freyther | 98d15ef | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 602 | if (bsc_mgcp_find_con(nat, 12) != sccp_con) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 603 | printf("Didn't find the connection\n"); |
Holger Hans Peter Freyther | 8917998 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 604 | abort(); |
| 605 | } |
| 606 | |
Holger Hans Peter Freyther | 8917998 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 607 | /* free everything */ |
| 608 | talloc_free(nat); |
| 609 | } |
| 610 | |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 611 | static void test_mgcp_rewrite(void) |
| 612 | { |
| 613 | int i; |
Holger Hans Peter Freyther | 0d0aaa6 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 614 | struct msgb *output; |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 615 | printf("Testing rewriting MGCP messages.\n"); |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 616 | |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 617 | for (i = 0; i < ARRAY_SIZE(mgcp_messages); ++i) { |
| 618 | const char *orig = mgcp_messages[i].orig; |
| 619 | const char *patc = mgcp_messages[i].patch; |
| 620 | const char *ip = mgcp_messages[i].ip; |
| 621 | const int port = mgcp_messages[i].port; |
| 622 | |
Holger Hans Peter Freyther | 0d0aaa6 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 623 | char *input = strdup(orig); |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 624 | |
Holger Hans Peter Freyther | 4e6129a | 2010-08-30 13:44:32 +0800 | [diff] [blame] | 625 | output = bsc_mgcp_rewrite(input, strlen(input), 0x1e, ip, port); |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 626 | if (msgb_l2len(output) != strlen(patc)) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 627 | printf("Wrong sizes for test: %d %d != %d != %d\n", i, msgb_l2len(output), strlen(patc), strlen(orig)); |
| 628 | printf("String '%s' vs '%s'\n", (const char *) output->l2h, patc); |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 629 | abort(); |
| 630 | } |
| 631 | |
| 632 | if (memcmp(output->l2h, patc, msgb_l2len(output)) != 0) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 633 | printf("Broken on %d msg: '%s'\n", i, (const char *) output->l2h); |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 634 | abort(); |
| 635 | } |
| 636 | |
| 637 | msgb_free(output); |
Holger Hans Peter Freyther | 0d0aaa6 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 638 | free(input); |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 639 | } |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 640 | } |
| 641 | |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 642 | static void test_mgcp_parse(void) |
| 643 | { |
| 644 | int code, ci; |
| 645 | char transaction[60]; |
| 646 | |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 647 | printf("Testing MGCP response parsing.\n"); |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 648 | |
| 649 | if (bsc_mgcp_parse_response(crcx_resp, &code, transaction) != 0) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 650 | printf("Failed to parse CRCX resp.\n"); |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 651 | abort(); |
| 652 | } |
| 653 | |
| 654 | if (code != 200) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 655 | printf("Failed to parse the CODE properly. Got: %d\n", code); |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 656 | abort(); |
| 657 | } |
| 658 | |
| 659 | if (strcmp(transaction, "23265295") != 0) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 660 | printf("Failed to parse transaction id: '%s'\n", transaction); |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 661 | abort(); |
| 662 | } |
| 663 | |
| 664 | ci = bsc_mgcp_extract_ci(crcx_resp); |
| 665 | if (ci != 1) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 666 | printf("Failed to parse the CI. Got: %d\n", ci); |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 667 | abort(); |
| 668 | } |
| 669 | } |
| 670 | |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 671 | struct cr_filter { |
Holger Hans Peter Freyther | 0b77635 | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 672 | const uint8_t *data; |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 673 | int length; |
| 674 | int result; |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 675 | int contype; |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 676 | |
| 677 | const char *bsc_imsi_allow; |
| 678 | const char *bsc_imsi_deny; |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 679 | const char *nat_imsi_deny; |
| 680 | }; |
| 681 | |
| 682 | static struct cr_filter cr_filter[] = { |
| 683 | { |
| 684 | .data = bssmap_cr, |
| 685 | .length = sizeof(bssmap_cr), |
Holger Hans Peter Freyther | f4cf1e8 | 2010-09-15 01:42:07 +0800 | [diff] [blame] | 686 | .result = 1, |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 687 | .contype = NAT_CON_TYPE_CM_SERV_REQ, |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 688 | }, |
| 689 | { |
| 690 | .data = bss_lu, |
| 691 | .length = sizeof(bss_lu), |
Holger Hans Peter Freyther | f4cf1e8 | 2010-09-15 01:42:07 +0800 | [diff] [blame] | 692 | .result = 1, |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 693 | .contype = NAT_CON_TYPE_LU, |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 694 | }, |
| 695 | { |
Holger Hans Peter Freyther | ecaa0b1 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 696 | .data = pag_resp, |
| 697 | .length = sizeof(pag_resp), |
Holger Hans Peter Freyther | f4cf1e8 | 2010-09-15 01:42:07 +0800 | [diff] [blame] | 698 | .result = 1, |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 699 | .contype = NAT_CON_TYPE_PAG_RESP, |
Holger Hans Peter Freyther | ecaa0b1 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 700 | }, |
| 701 | { |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 702 | /* nat deny is before blank/null BSC */ |
| 703 | .data = bss_lu, |
| 704 | .length = sizeof(bss_lu), |
| 705 | .result = -3, |
| 706 | .nat_imsi_deny = "[0-9]*", |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 707 | .contype = NAT_CON_TYPE_LU, |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 708 | }, |
| 709 | { |
Holger Hans Peter Freyther | 58bce3f | 2010-05-14 23:07:58 +0800 | [diff] [blame] | 710 | /* BSC allow is before NAT deny */ |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 711 | .data = bss_lu, |
| 712 | .length = sizeof(bss_lu), |
Holger Hans Peter Freyther | f4cf1e8 | 2010-09-15 01:42:07 +0800 | [diff] [blame] | 713 | .result = 1, |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 714 | .nat_imsi_deny = "[0-9]*", |
Holger Hans Peter Freyther | 58bce3f | 2010-05-14 23:07:58 +0800 | [diff] [blame] | 715 | .bsc_imsi_allow = "2440[0-9]*", |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 716 | .contype = NAT_CON_TYPE_LU, |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 717 | }, |
| 718 | { |
| 719 | /* BSC allow is before NAT deny */ |
| 720 | .data = bss_lu, |
| 721 | .length = sizeof(bss_lu), |
Holger Hans Peter Freyther | f4cf1e8 | 2010-09-15 01:42:07 +0800 | [diff] [blame] | 722 | .result = 1, |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 723 | .bsc_imsi_allow = "[0-9]*", |
| 724 | .nat_imsi_deny = "[0-9]*", |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 725 | .contype = NAT_CON_TYPE_LU, |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 726 | }, |
| 727 | { |
| 728 | /* filter as deny is first */ |
| 729 | .data = bss_lu, |
| 730 | .length = sizeof(bss_lu), |
Holger Hans Peter Freyther | 6b340cc | 2010-10-19 20:55:33 +0200 | [diff] [blame] | 731 | .result = 1, |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 732 | .bsc_imsi_deny = "[0-9]*", |
| 733 | .bsc_imsi_allow = "[0-9]*", |
| 734 | .nat_imsi_deny = "[0-9]*", |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 735 | .contype = NAT_CON_TYPE_LU, |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 736 | }, |
Holger Hans Peter Freyther | 6b340cc | 2010-10-19 20:55:33 +0200 | [diff] [blame] | 737 | { |
| 738 | /* deny by nat rule */ |
| 739 | .data = bss_lu, |
| 740 | .length = sizeof(bss_lu), |
| 741 | .result = -3, |
| 742 | .bsc_imsi_deny = "000[0-9]*", |
| 743 | .nat_imsi_deny = "[0-9]*", |
| 744 | .contype = NAT_CON_TYPE_LU, |
| 745 | }, |
| 746 | { |
| 747 | /* deny by bsc rule */ |
| 748 | .data = bss_lu, |
| 749 | .length = sizeof(bss_lu), |
| 750 | .result = -2, |
| 751 | .bsc_imsi_deny = "[0-9]*", |
| 752 | .contype = NAT_CON_TYPE_LU, |
| 753 | }, |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 754 | |
| 755 | }; |
| 756 | |
Holger Hans Peter Freyther | 28b08d6 | 2010-05-14 08:12:57 +0800 | [diff] [blame] | 757 | static void test_cr_filter() |
| 758 | { |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 759 | int i, res, contype; |
Holger Hans Peter Freyther | 28b08d6 | 2010-05-14 08:12:57 +0800 | [diff] [blame] | 760 | struct msgb *msg = msgb_alloc(4096, "test_cr_filter"); |
Holger Hans Peter Freyther | 28b08d6 | 2010-05-14 08:12:57 +0800 | [diff] [blame] | 761 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 762 | struct bsc_nat_acc_lst *nat_lst, *bsc_lst; |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 763 | struct bsc_nat_acc_lst_entry *nat_entry, *bsc_entry; |
Holger Hans Peter Freyther | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 764 | struct bsc_nat_reject_cause cause; |
Holger Hans Peter Freyther | 28b08d6 | 2010-05-14 08:12:57 +0800 | [diff] [blame] | 765 | |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 766 | struct bsc_nat *nat = bsc_nat_alloc(); |
| 767 | struct bsc_connection *bsc = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 768 | bsc->cfg = bsc_config_alloc(nat, "foo"); |
| 769 | bsc_config_add_lac(bsc->cfg, 1234); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 770 | bsc->cfg->acc_lst_name = "bsc"; |
| 771 | nat->acc_lst_name = "nat"; |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 772 | |
Holger Hans Peter Freyther | 8eb4589 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 773 | nat_lst = bsc_nat_acc_lst_get(nat, "nat"); |
| 774 | bsc_lst = bsc_nat_acc_lst_get(nat, "bsc"); |
| 775 | |
| 776 | bsc_entry = bsc_nat_acc_lst_entry_create(bsc_lst); |
| 777 | nat_entry = bsc_nat_acc_lst_entry_create(nat_lst); |
| 778 | |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 779 | for (i = 0; i < ARRAY_SIZE(cr_filter); ++i) { |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 780 | char *imsi; |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 781 | msgb_reset(msg); |
| 782 | copy_to_msg(msg, cr_filter[i].data, cr_filter[i].length); |
| 783 | |
Holger Hans Peter Freyther | b604471 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 784 | nat_lst = bsc_nat_acc_lst_get(nat, "nat"); |
| 785 | bsc_lst = bsc_nat_acc_lst_get(nat, "bsc"); |
Holger Hans Peter Freyther | 67b6d52 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 786 | |
Holger Hans Peter Freyther | 1c14d96 | 2011-06-09 21:48:49 +0200 | [diff] [blame] | 787 | if (gsm_parse_reg(nat_entry, &nat_entry->imsi_deny_re, &nat_entry->imsi_deny, |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 788 | cr_filter[i].nat_imsi_deny ? 1 : 0, |
Holger Hans Peter Freyther | dd7e85b | 2011-04-04 19:19:26 +0200 | [diff] [blame] | 789 | &cr_filter[i].nat_imsi_deny) != 0) |
| 790 | abort(); |
Holger Hans Peter Freyther | 1c14d96 | 2011-06-09 21:48:49 +0200 | [diff] [blame] | 791 | if (gsm_parse_reg(bsc_entry, &bsc_entry->imsi_allow_re, &bsc_entry->imsi_allow, |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 792 | cr_filter[i].bsc_imsi_allow ? 1 : 0, |
Holger Hans Peter Freyther | dd7e85b | 2011-04-04 19:19:26 +0200 | [diff] [blame] | 793 | &cr_filter[i].bsc_imsi_allow) != 0) |
| 794 | abort(); |
Holger Hans Peter Freyther | 1c14d96 | 2011-06-09 21:48:49 +0200 | [diff] [blame] | 795 | if (gsm_parse_reg(bsc_entry, &bsc_entry->imsi_deny_re, &bsc_entry->imsi_deny, |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 796 | cr_filter[i].bsc_imsi_deny ? 1 : 0, |
Holger Hans Peter Freyther | dd7e85b | 2011-04-04 19:19:26 +0200 | [diff] [blame] | 797 | &cr_filter[i].bsc_imsi_deny) != 0) |
| 798 | abort(); |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 799 | |
| 800 | parsed = bsc_nat_parse(msg); |
| 801 | if (!parsed) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 802 | printf("FAIL: Failed to parse the message\n"); |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 803 | abort(); |
| 804 | } |
| 805 | |
Holger Hans Peter Freyther | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 806 | memset(&cause, 0, sizeof(cause)); |
| 807 | res = bsc_nat_filter_sccp_cr(bsc, msg, parsed, &contype, &imsi, &cause); |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 808 | if (res != cr_filter[i].result) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 809 | printf("FAIL: Wrong result %d for test %d.\n", res, i); |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 810 | abort(); |
| 811 | } |
| 812 | |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 813 | if (contype != cr_filter[i].contype) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 814 | printf("FAIL: Wrong contype %d for test %d.\n", res, contype); |
Holger Hans Peter Freyther | d5a2bc4 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 815 | abort(); |
| 816 | } |
| 817 | |
Holger Hans Peter Freyther | 9bd8bd7 | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 818 | talloc_steal(parsed, imsi); |
Holger Hans Peter Freyther | 13abb82 | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 819 | talloc_free(parsed); |
Holger Hans Peter Freyther | 28b08d6 | 2010-05-14 08:12:57 +0800 | [diff] [blame] | 820 | } |
| 821 | |
Holger Hans Peter Freyther | 28b08d6 | 2010-05-14 08:12:57 +0800 | [diff] [blame] | 822 | msgb_free(msg); |
| 823 | } |
| 824 | |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 825 | static void test_dt_filter() |
| 826 | { |
| 827 | int i; |
| 828 | struct msgb *msg = msgb_alloc(4096, "test_dt_filter"); |
| 829 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 830 | struct bsc_nat_reject_cause cause; |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 831 | |
| 832 | struct bsc_nat *nat = bsc_nat_alloc(); |
| 833 | struct bsc_connection *bsc = bsc_connection_alloc(nat); |
| 834 | struct sccp_connections *con = talloc_zero(0, struct sccp_connections); |
| 835 | |
Holger Hans Peter Freyther | 1f04083 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 836 | bsc->cfg = bsc_config_alloc(nat, "foo"); |
| 837 | bsc_config_add_lac(bsc->cfg, 23); |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 838 | con->bsc = bsc; |
| 839 | |
| 840 | msgb_reset(msg); |
| 841 | copy_to_msg(msg, id_resp, ARRAY_SIZE(id_resp)); |
| 842 | |
| 843 | parsed = bsc_nat_parse(msg); |
| 844 | if (!parsed) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 845 | printf("FAIL: Could not parse ID resp\n"); |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 846 | abort(); |
| 847 | } |
| 848 | |
| 849 | if (parsed->bssap != BSSAP_MSG_DTAP) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 850 | printf("FAIL: It should be dtap\n"); |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 851 | abort(); |
| 852 | } |
| 853 | |
| 854 | /* gsm_type is actually the size of the dtap */ |
| 855 | if (parsed->gsm_type < msgb_l3len(msg) - 3) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 856 | printf("FAIL: Not enough space for the content\n"); |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 857 | abort(); |
| 858 | } |
| 859 | |
Holger Hans Peter Freyther | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 860 | memset(&cause, 0, sizeof(cause)); |
| 861 | if (bsc_nat_filter_dt(bsc, msg, con, parsed, &cause) != 1) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 862 | printf("FAIL: Should have passed..\n"); |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 863 | abort(); |
| 864 | } |
| 865 | |
| 866 | /* just some basic length checking... */ |
| 867 | for (i = ARRAY_SIZE(id_resp); i >= 0; --i) { |
| 868 | msgb_reset(msg); |
| 869 | copy_to_msg(msg, id_resp, ARRAY_SIZE(id_resp)); |
| 870 | |
| 871 | parsed = bsc_nat_parse(msg); |
| 872 | if (!parsed) |
| 873 | continue; |
| 874 | |
| 875 | con->imsi_checked = 0; |
Holger Hans Peter Freyther | e2abbbe | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 876 | memset(&cause, 0, sizeof(cause)); |
| 877 | bsc_nat_filter_dt(bsc, msg, con, parsed, &cause); |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 881 | static void test_setup_rewrite() |
| 882 | { |
| 883 | struct msgb *msg = msgb_alloc(4096, "test_dt_filter"); |
| 884 | struct msgb *out; |
| 885 | struct bsc_nat_parsed *parsed; |
| 886 | const char *imsi = "27408000001234"; |
| 887 | |
| 888 | struct bsc_nat *nat = bsc_nat_alloc(); |
| 889 | |
| 890 | /* a fake list */ |
Pablo Neira Ayuso | 1a91e67 | 2011-05-07 13:11:20 +0200 | [diff] [blame] | 891 | struct osmo_config_list entries; |
| 892 | struct osmo_config_entry entry; |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 893 | |
| 894 | INIT_LLIST_HEAD(&entries.entry); |
| 895 | entry.mcc = "274"; |
| 896 | entry.mnc = "08"; |
| 897 | entry.option = "^0([1-9])"; |
| 898 | entry.text = "0049"; |
| 899 | llist_add_tail(&entry.list, &entries.entry); |
Holger Hans Peter Freyther | d240407 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 900 | bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries); |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 901 | |
| 902 | /* verify that nothing changed */ |
| 903 | msgb_reset(msg); |
| 904 | copy_to_msg(msg, cc_setup_international, ARRAY_SIZE(cc_setup_international)); |
| 905 | parsed = bsc_nat_parse(msg); |
| 906 | if (!parsed) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 907 | printf("FAIL: Could not parse ID resp\n"); |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 908 | abort(); |
| 909 | } |
| 910 | |
Holger Hans Peter Freyther | 1f58606 | 2011-05-27 14:09:55 +0200 | [diff] [blame] | 911 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 912 | if (msg != out) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 913 | printf("FAIL: The message should not have been changed\n"); |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 914 | abort(); |
| 915 | } |
| 916 | |
Holger Hans Peter Freyther | 75e6122 | 2012-01-10 22:31:39 +0100 | [diff] [blame] | 917 | verify_msg(out, cc_setup_international, ARRAY_SIZE(cc_setup_international)); |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 918 | talloc_free(parsed); |
| 919 | |
| 920 | /* verify that something in the message changes */ |
| 921 | msgb_reset(msg); |
| 922 | copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national)); |
| 923 | parsed = bsc_nat_parse(msg); |
| 924 | if (!parsed) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 925 | printf("FAIL: Could not parse ID resp\n"); |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 926 | abort(); |
| 927 | } |
| 928 | |
Holger Hans Peter Freyther | 1f58606 | 2011-05-27 14:09:55 +0200 | [diff] [blame] | 929 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 930 | if (!out) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 931 | printf("FAIL: A new message should be created.\n"); |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 932 | abort(); |
| 933 | } |
| 934 | |
| 935 | if (msg == out) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 936 | printf("FAIL: The message should have changed\n"); |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 937 | abort(); |
| 938 | } |
| 939 | |
Holger Hans Peter Freyther | 75e6122 | 2012-01-10 22:31:39 +0100 | [diff] [blame] | 940 | verify_msg(out, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched)); |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 941 | msgb_free(out); |
Holger Hans Peter Freyther | 34c48a6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 942 | |
| 943 | /* Make sure that a wildcard is matching */ |
| 944 | entry.mnc = "*"; |
Holger Hans Peter Freyther | d240407 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 945 | bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries); |
Holger Hans Peter Freyther | 34c48a6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 946 | msg = msgb_alloc(4096, "test_dt_filter"); |
| 947 | copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national)); |
| 948 | parsed = bsc_nat_parse(msg); |
| 949 | if (!parsed) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 950 | printf("FAIL: Could not parse ID resp\n"); |
Holger Hans Peter Freyther | 34c48a6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 951 | abort(); |
| 952 | } |
| 953 | |
Holger Hans Peter Freyther | 1f58606 | 2011-05-27 14:09:55 +0200 | [diff] [blame] | 954 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
Holger Hans Peter Freyther | 34c48a6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 955 | if (!out) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 956 | printf("FAIL: A new message should be created.\n"); |
Holger Hans Peter Freyther | 34c48a6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 957 | abort(); |
| 958 | } |
| 959 | |
| 960 | if (msg == out) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 961 | printf("FAIL: The message should have changed\n"); |
Holger Hans Peter Freyther | 34c48a6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 962 | abort(); |
| 963 | } |
| 964 | |
Holger Hans Peter Freyther | 75e6122 | 2012-01-10 22:31:39 +0100 | [diff] [blame] | 965 | verify_msg(out, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched)); |
Holger Hans Peter Freyther | 34c48a6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 966 | msgb_free(out); |
| 967 | |
| 968 | /* Make sure that a wildcard is matching */ |
| 969 | entry.mnc = "09"; |
Holger Hans Peter Freyther | d240407 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 970 | bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries); |
Holger Hans Peter Freyther | 34c48a6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 971 | msg = msgb_alloc(4096, "test_dt_filter"); |
| 972 | copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national)); |
| 973 | parsed = bsc_nat_parse(msg); |
| 974 | if (!parsed) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 975 | printf("FAIL: Could not parse ID resp\n"); |
Holger Hans Peter Freyther | 34c48a6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 976 | abort(); |
| 977 | } |
| 978 | |
Holger Hans Peter Freyther | 1f58606 | 2011-05-27 14:09:55 +0200 | [diff] [blame] | 979 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
Holger Hans Peter Freyther | 34c48a6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 980 | if (out != msg) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 981 | printf("FAIL: The message should be unchanged.\n"); |
Holger Hans Peter Freyther | 34c48a6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 982 | abort(); |
| 983 | } |
| 984 | |
Holger Hans Peter Freyther | 75e6122 | 2012-01-10 22:31:39 +0100 | [diff] [blame] | 985 | verify_msg(out, cc_setup_national, ARRAY_SIZE(cc_setup_national)); |
Holger Hans Peter Freyther | 34c48a6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 986 | msgb_free(out); |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 987 | } |
| 988 | |
Holger Hans Peter Freyther | 21e5c61 | 2012-01-18 20:00:28 +0100 | [diff] [blame] | 989 | static void test_sms_smsc_rewrite() |
Holger Hans Peter Freyther | d240407 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 990 | { |
| 991 | struct msgb *msg = msgb_alloc(4096, "SMSC rewrite"), *out; |
| 992 | struct bsc_nat_parsed *parsed; |
| 993 | const char *imsi = "515039900406700"; |
| 994 | |
| 995 | struct bsc_nat *nat = bsc_nat_alloc(); |
| 996 | |
| 997 | /* a fake list */ |
Holger Hans Peter Freyther | 4c2822e | 2012-01-10 22:39:07 +0100 | [diff] [blame] | 998 | struct osmo_config_list smsc_entries, dest_entries, clear_entries; |
| 999 | struct osmo_config_entry smsc_entry, dest_entry, clear_entry; |
Holger Hans Peter Freyther | d240407 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1000 | |
Holger Hans Peter Freyther | 1ed3927 | 2011-05-27 19:21:24 +0200 | [diff] [blame] | 1001 | INIT_LLIST_HEAD(&smsc_entries.entry); |
| 1002 | INIT_LLIST_HEAD(&dest_entries.entry); |
Holger Hans Peter Freyther | 4c2822e | 2012-01-10 22:39:07 +0100 | [diff] [blame] | 1003 | INIT_LLIST_HEAD(&clear_entries.entry); |
Holger Hans Peter Freyther | 1ed3927 | 2011-05-27 19:21:24 +0200 | [diff] [blame] | 1004 | smsc_entry.mcc = "^515039"; |
| 1005 | smsc_entry.option = "639180000105()"; |
| 1006 | smsc_entry.text = "6666666666667"; |
| 1007 | llist_add_tail(&smsc_entry.list, &smsc_entries.entry); |
| 1008 | dest_entry.mcc = "515"; |
| 1009 | dest_entry.mnc = "03"; |
| 1010 | dest_entry.option = "^0049"; |
| 1011 | dest_entry.text = ""; |
| 1012 | llist_add_tail(&dest_entry.list, &dest_entries.entry); |
Holger Hans Peter Freyther | 4c2822e | 2012-01-10 22:39:07 +0100 | [diff] [blame] | 1013 | clear_entry.mcc = "^515039"; |
| 1014 | clear_entry.option = "^0049"; |
| 1015 | clear_entry.text = ""; |
| 1016 | llist_add_tail(&clear_entry.list, &clear_entries.entry); |
Holger Hans Peter Freyther | 1ed3927 | 2011-05-27 19:21:24 +0200 | [diff] [blame] | 1017 | |
| 1018 | bsc_nat_num_rewr_entry_adapt(nat, &nat->smsc_rewr, &smsc_entries); |
| 1019 | bsc_nat_num_rewr_entry_adapt(nat, &nat->tpdest_match, &dest_entries); |
Holger Hans Peter Freyther | 4c2822e | 2012-01-10 22:39:07 +0100 | [diff] [blame] | 1020 | bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_clear_tp_srr, &clear_entries); |
Holger Hans Peter Freyther | d240407 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1021 | |
Holger Hans Peter Freyther | 4c2822e | 2012-01-10 22:39:07 +0100 | [diff] [blame] | 1022 | printf("Testing SMSC rewriting.\n"); |
| 1023 | |
| 1024 | /* |
| 1025 | * Check if the SMSC address is changed |
| 1026 | */ |
Holger Hans Peter Freyther | d240407 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1027 | copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite)); |
| 1028 | parsed = bsc_nat_parse(msg); |
| 1029 | if (!parsed) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 1030 | printf("FAIL: Could not parse SMS\n"); |
Holger Hans Peter Freyther | d240407 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1031 | abort(); |
| 1032 | } |
| 1033 | |
| 1034 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
| 1035 | if (out == msg) { |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 1036 | printf("FAIL: This should have changed.\n"); |
Holger Hans Peter Freyther | d240407 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1037 | abort(); |
| 1038 | } |
| 1039 | |
Holger Hans Peter Freyther | 75e6122 | 2012-01-10 22:31:39 +0100 | [diff] [blame] | 1040 | verify_msg(out, smsc_rewrite_patched, ARRAY_SIZE(smsc_rewrite_patched)); |
| 1041 | msgb_free(out); |
Holger Hans Peter Freyther | 4c2822e | 2012-01-10 22:39:07 +0100 | [diff] [blame] | 1042 | |
| 1043 | /* clear out the filter for SMSC */ |
| 1044 | printf("Attempting to only rewrite the HDR\n"); |
| 1045 | bsc_nat_num_rewr_entry_adapt(nat, &nat->smsc_rewr, NULL); |
| 1046 | msg = msgb_alloc(4096, "SMSC rewrite"); |
| 1047 | copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite)); |
| 1048 | parsed = bsc_nat_parse(msg); |
| 1049 | if (!parsed) { |
| 1050 | printf("FAIL: Could not parse SMS\n"); |
| 1051 | abort(); |
| 1052 | } |
| 1053 | |
| 1054 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
| 1055 | if (out == msg) { |
| 1056 | printf("FAIL: This should have changed.\n"); |
| 1057 | abort(); |
| 1058 | } |
| 1059 | |
| 1060 | verify_msg(out, smsc_rewrite_patched_hdr, ARRAY_SIZE(smsc_rewrite_patched_hdr)); |
| 1061 | msgb_free(out); |
| 1062 | |
| 1063 | /* clear out the next filter */ |
| 1064 | printf("Attempting to change nothing.\n"); |
| 1065 | bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_clear_tp_srr, NULL); |
| 1066 | msg = msgb_alloc(4096, "SMSC rewrite"); |
| 1067 | copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite)); |
| 1068 | parsed = bsc_nat_parse(msg); |
| 1069 | if (!parsed) { |
| 1070 | printf("FAIL: Could not parse SMS\n"); |
| 1071 | abort(); |
| 1072 | } |
| 1073 | |
| 1074 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
| 1075 | if (out != msg) { |
| 1076 | printf("FAIL: This should not have changed.\n"); |
| 1077 | abort(); |
| 1078 | } |
| 1079 | |
| 1080 | verify_msg(out, smsc_rewrite, ARRAY_SIZE(smsc_rewrite)); |
| 1081 | msgb_free(out); |
Holger Hans Peter Freyther | d240407 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1082 | } |
| 1083 | |
Holger Hans Peter Freyther | 21e5c61 | 2012-01-18 20:00:28 +0100 | [diff] [blame] | 1084 | static void test_sms_number_rewrite(void) |
| 1085 | { |
Holger Hans Peter Freyther | d99e463 | 2012-03-26 16:21:42 +0200 | [diff] [blame] | 1086 | struct msgb *msg, *out; |
Holger Hans Peter Freyther | 21e5c61 | 2012-01-18 20:00:28 +0100 | [diff] [blame] | 1087 | struct bsc_nat_parsed *parsed; |
| 1088 | const char *imsi = "515039900406700"; |
| 1089 | |
| 1090 | struct bsc_nat *nat = bsc_nat_alloc(); |
| 1091 | |
| 1092 | /* a fake list */ |
Holger Hans Peter Freyther | d99e463 | 2012-03-26 16:21:42 +0200 | [diff] [blame] | 1093 | struct osmo_config_list num_entries, clear_entries; |
| 1094 | struct osmo_config_entry num_entry, clear_entry; |
Holger Hans Peter Freyther | 21e5c61 | 2012-01-18 20:00:28 +0100 | [diff] [blame] | 1095 | |
| 1096 | INIT_LLIST_HEAD(&num_entries.entry); |
| 1097 | num_entry.mcc = "^515039"; |
| 1098 | num_entry.option = "^0049()"; |
| 1099 | num_entry.text = "0032"; |
| 1100 | llist_add_tail(&num_entry.list, &num_entries.entry); |
| 1101 | |
| 1102 | bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_num_rewr, &num_entries); |
| 1103 | |
| 1104 | printf("Testing SMS TP-DA rewriting.\n"); |
| 1105 | |
| 1106 | /* |
| 1107 | * Check if the SMSC address is changed |
| 1108 | */ |
Holger Hans Peter Freyther | d99e463 | 2012-03-26 16:21:42 +0200 | [diff] [blame] | 1109 | msg = msgb_alloc(4096, "SMSC rewrite"); |
Holger Hans Peter Freyther | 21e5c61 | 2012-01-18 20:00:28 +0100 | [diff] [blame] | 1110 | copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite)); |
| 1111 | parsed = bsc_nat_parse(msg); |
| 1112 | if (!parsed) { |
| 1113 | printf("FAIL: Could not parse SMS\n"); |
| 1114 | abort(); |
| 1115 | } |
| 1116 | |
| 1117 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
| 1118 | if (out == msg) { |
| 1119 | printf("FAIL: This should have changed.\n"); |
| 1120 | abort(); |
| 1121 | } |
| 1122 | |
| 1123 | verify_msg(out, smsc_rewrite_num_patched, |
| 1124 | ARRAY_SIZE(smsc_rewrite_num_patched)); |
| 1125 | msgb_free(out); |
Holger Hans Peter Freyther | d99e463 | 2012-03-26 16:21:42 +0200 | [diff] [blame] | 1126 | |
| 1127 | /* |
| 1128 | * Now with TP-SRR rewriting enabled |
| 1129 | */ |
| 1130 | INIT_LLIST_HEAD(&clear_entries.entry); |
| 1131 | clear_entry.mcc = "^515039"; |
| 1132 | clear_entry.option = ""; |
| 1133 | clear_entry.text = ""; |
| 1134 | llist_add_tail(&clear_entry.list, &clear_entries.entry); |
| 1135 | bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_clear_tp_srr, &clear_entries); |
| 1136 | |
| 1137 | msg = msgb_alloc(4096, "SMSC rewrite"); |
| 1138 | copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite)); |
| 1139 | parsed = bsc_nat_parse(msg); |
| 1140 | if (!parsed) { |
| 1141 | printf("FAIL: Could not parse SMS\n"); |
| 1142 | abort(); |
| 1143 | } |
| 1144 | |
| 1145 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
| 1146 | if (out == msg) { |
| 1147 | printf("FAIL: This should have changed.\n"); |
| 1148 | abort(); |
| 1149 | } |
| 1150 | |
| 1151 | verify_msg(out, smsc_rewrite_num_patched_tp_srr, |
| 1152 | ARRAY_SIZE(smsc_rewrite_num_patched_tp_srr)); |
| 1153 | msgb_free(out); |
Holger Hans Peter Freyther | 21e5c61 | 2012-01-18 20:00:28 +0100 | [diff] [blame] | 1154 | } |
| 1155 | |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 1156 | static void test_barr_list_parsing(void) |
| 1157 | { |
| 1158 | int rc; |
| 1159 | int cm, lu; |
| 1160 | struct rb_node *node; |
| 1161 | struct rb_root root = RB_ROOT; |
| 1162 | struct osmo_config_list *lst = osmo_config_list_parse(NULL, "barr.cfg"); |
| 1163 | if (lst == NULL) |
| 1164 | abort(); |
| 1165 | |
| 1166 | rc = bsc_nat_barr_adapt(NULL, &root, lst); |
| 1167 | if (rc != 0) |
| 1168 | abort(); |
| 1169 | talloc_free(lst); |
| 1170 | |
| 1171 | |
| 1172 | for (node = rb_first(&root); node; node = rb_next(node)) { |
| 1173 | struct bsc_nat_barr_entry *entry; |
| 1174 | entry = rb_entry(node, struct bsc_nat_barr_entry, node); |
| 1175 | printf("IMSI: %s CM: %d LU: %d\n", entry->imsi, |
| 1176 | entry->cm_reject_cause, entry->lu_reject_cause); |
| 1177 | } |
| 1178 | |
| 1179 | /* do the look up now.. */ |
| 1180 | rc = bsc_nat_barr_find(&root, "12123119", &cm, &lu); |
| 1181 | if (!rc) { |
| 1182 | printf("Failed to find the IMSI.\n"); |
| 1183 | abort(); |
| 1184 | } |
| 1185 | |
| 1186 | if (cm != 3 || lu != 4) { |
| 1187 | printf("Found CM(%d) and LU(%d)\n", cm, lu); |
| 1188 | abort(); |
| 1189 | } |
| 1190 | |
| 1191 | /* empty and check that it is empty */ |
| 1192 | bsc_nat_barr_adapt(NULL, &root, NULL); |
| 1193 | if (!RB_EMPTY_ROOT(&root)) { |
| 1194 | printf("Failed to empty the list.\n"); |
| 1195 | abort(); |
| 1196 | } |
| 1197 | |
| 1198 | /* check that dup results in an error */ |
| 1199 | lst = osmo_config_list_parse(NULL, "barr_dup.cfg"); |
| 1200 | if (lst == NULL) { |
| 1201 | printf("Failed to parse list with dups\n"); |
| 1202 | abort(); |
| 1203 | } |
| 1204 | |
| 1205 | rc = bsc_nat_barr_adapt(NULL, &root, lst); |
| 1206 | if (rc != -1) { |
| 1207 | printf("It should have failed due dup\n"); |
| 1208 | abort(); |
| 1209 | } |
| 1210 | talloc_free(lst); |
| 1211 | |
| 1212 | /* dump for reference */ |
| 1213 | for (node = rb_first(&root); node; node = rb_next(node)) { |
| 1214 | struct bsc_nat_barr_entry *entry; |
| 1215 | entry = rb_entry(node, struct bsc_nat_barr_entry, node); |
| 1216 | printf("IMSI: %s CM: %d LU: %d\n", entry->imsi, |
| 1217 | entry->cm_reject_cause, entry->lu_reject_cause); |
| 1218 | |
| 1219 | } |
| 1220 | } |
| 1221 | |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 1222 | int main(int argc, char **argv) |
| 1223 | { |
Holger Hans Peter Freyther | a8dffc1 | 2010-07-30 02:53:14 +0800 | [diff] [blame] | 1224 | sccp_set_log_area(DSCCP); |
Holger Hans Peter Freyther | 2d59cc6 | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1225 | osmo_init_logging(&log_info); |
Holger Hans Peter Freyther | b63a9f8 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 1226 | |
| 1227 | test_filter(); |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 1228 | test_contrack(); |
Holger Hans Peter Freyther | d5deb33 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 1229 | test_paging(); |
Holger Hans Peter Freyther | 8917998 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 1230 | test_mgcp_ass_tracking(); |
| 1231 | test_mgcp_find(); |
Holger Hans Peter Freyther | acd6420 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 1232 | test_mgcp_rewrite(); |
Holger Hans Peter Freyther | 9fec010 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 1233 | test_mgcp_parse(); |
Holger Hans Peter Freyther | 28b08d6 | 2010-05-14 08:12:57 +0800 | [diff] [blame] | 1234 | test_cr_filter(); |
Holger Hans Peter Freyther | a15e257 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 1235 | test_dt_filter(); |
Holger Hans Peter Freyther | 0d5fdbb | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 1236 | test_setup_rewrite(); |
Holger Hans Peter Freyther | 21e5c61 | 2012-01-18 20:00:28 +0100 | [diff] [blame] | 1237 | test_sms_smsc_rewrite(); |
| 1238 | test_sms_number_rewrite(); |
Holger Hans Peter Freyther | 473b38a | 2011-02-26 11:38:00 +0100 | [diff] [blame] | 1239 | test_mgcp_allocations(); |
Holger Hans Peter Freyther | 1fa71b2 | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 1240 | test_barr_list_parsing(); |
Holger Hans Peter Freyther | a302db3 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 1241 | |
| 1242 | printf("Testing execution completed.\n"); |
Holger Hans Peter Freyther | f75a680 | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1243 | return 0; |
| 1244 | } |