Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1 | /* |
| 2 | * BSC NAT Message filtering |
| 3 | * |
Holger Hans Peter Freyther | dbd9449 | 2013-04-02 12:34:11 +0200 | [diff] [blame] | 4 | * (C) 2010-2013 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 5 | * (C) 2010-2013 by On-Waves |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 9af6ddf | 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 | 0b8f69d | 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 | 9af6ddf | 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 | 0b8f69d | 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 | c2b31ed | 2010-07-31 05:17:17 +0800 | [diff] [blame] | 28 | #include <openbsc/bsc_nat_sccp.h> |
Holger Hans Peter Freyther | ddf191e | 2013-06-25 11:44:01 +0200 | [diff] [blame] | 29 | #include <openbsc/nat_rewrite_trie.h> |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 30 | |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 31 | #include <osmocom/core/application.h> |
Holger Hans Peter Freyther | b2b291d | 2013-04-16 13:23:43 +0200 | [diff] [blame] | 32 | #include <osmocom/core/backtrace.h> |
Pablo Neira Ayuso | 928cb33 | 2011-03-26 22:08:53 +0100 | [diff] [blame] | 33 | #include <osmocom/core/talloc.h> |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 34 | |
Harald Welte | d5db12c | 2010-08-03 15:11:51 +0200 | [diff] [blame] | 35 | #include <osmocom/sccp/sccp.h> |
Pablo Neira Ayuso | 928cb33 | 2011-03-26 22:08:53 +0100 | [diff] [blame] | 36 | #include <osmocom/gsm/protocol/gsm_08_08.h> |
Holger Hans Peter Freyther | 30e1ae9 | 2010-07-30 02:53:14 +0800 | [diff] [blame] | 37 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 38 | #include <stdio.h> |
| 39 | |
| 40 | /* test messages for ipa */ |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 41 | static uint8_t ipa_id[] = { |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 42 | 0x00, 0x01, 0xfe, 0x06, |
| 43 | }; |
| 44 | |
| 45 | /* SCCP messages are below */ |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 46 | static uint8_t gsm_reset[] = { |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 47 | 0x00, 0x12, 0xfd, |
| 48 | 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe, |
| 49 | 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04, |
| 50 | 0x01, 0x20, |
| 51 | }; |
| 52 | |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 53 | static const uint8_t gsm_reset_ack[] = { |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 54 | 0x00, 0x13, 0xfd, |
| 55 | 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01, |
| 56 | 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03, |
| 57 | 0x00, 0x01, 0x31, |
| 58 | }; |
| 59 | |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 60 | static const uint8_t gsm_paging[] = { |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 61 | 0x00, 0x20, 0xfd, |
| 62 | 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01, |
| 63 | 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x10, |
| 64 | 0x00, 0x0e, 0x52, 0x08, 0x08, 0x29, 0x47, 0x10, |
| 65 | 0x02, 0x01, 0x31, 0x97, 0x61, 0x1a, 0x01, 0x06, |
| 66 | }; |
| 67 | |
| 68 | /* BSC -> MSC connection open */ |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 69 | static const uint8_t bssmap_cr[] = { |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 70 | 0x00, 0x2c, 0xfd, |
| 71 | 0x01, 0x01, 0x02, 0x03, 0x02, 0x02, 0x04, 0x02, |
| 72 | 0x42, 0xfe, 0x0f, 0x1f, 0x00, 0x1d, 0x57, 0x05, |
| 73 | 0x08, 0x00, 0x72, 0xf4, 0x80, 0x20, 0x12, 0xc3, |
| 74 | 0x50, 0x17, 0x10, 0x05, 0x24, 0x11, 0x03, 0x33, |
| 75 | 0x19, 0xa2, 0x08, 0x29, 0x47, 0x10, 0x02, 0x01, |
| 76 | 0x31, 0x97, 0x61, 0x00 |
| 77 | }; |
| 78 | |
| 79 | /* MSC -> BSC connection confirm */ |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 80 | static const uint8_t bssmap_cc[] = { |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 81 | 0x00, 0x0a, 0xfd, |
| 82 | 0x02, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, |
| 83 | }; |
| 84 | |
| 85 | /* MSC -> BSC released */ |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 86 | static const uint8_t bssmap_released[] = { |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 87 | 0x00, 0x0e, 0xfd, |
| 88 | 0x04, 0x00, 0x00, 0x03, 0x01, 0x02, 0x03, 0x00, 0x01, 0x0f, |
| 89 | 0x02, 0x23, 0x42, 0x00, |
| 90 | }; |
| 91 | |
| 92 | /* BSC -> MSC released */ |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 93 | static const uint8_t bssmap_release_complete[] = { |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 94 | 0x00, 0x07, 0xfd, |
| 95 | 0x05, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03 |
| 96 | }; |
| 97 | |
Holger Hans Peter Freyther | 0a6f62f | 2010-04-06 10:22:01 +0200 | [diff] [blame] | 98 | /* both directions IT timer */ |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 99 | static const uint8_t connnection_it[] = { |
Holger Hans Peter Freyther | 0a6f62f | 2010-04-06 10:22:01 +0200 | [diff] [blame] | 100 | 0x00, 0x0b, 0xfd, |
| 101 | 0x10, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03, |
| 102 | 0x00, 0x00, 0x00, 0x00, |
| 103 | }; |
| 104 | |
Holger Hans Peter Freyther | 5e63f92 | 2010-04-21 15:45:26 +0800 | [diff] [blame] | 105 | /* error in both directions */ |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 106 | static const uint8_t proto_error[] = { |
Holger Hans Peter Freyther | 5e63f92 | 2010-04-21 15:45:26 +0800 | [diff] [blame] | 107 | 0x00, 0x05, 0xfd, |
| 108 | 0x0f, 0x22, 0x33, 0x44, 0x00, |
| 109 | }; |
| 110 | |
Holger Hans Peter Freyther | a128d91 | 2010-04-01 08:47:12 +0200 | [diff] [blame] | 111 | /* MGCP wrap... */ |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 112 | static const uint8_t mgcp_msg[] = { |
Holger Hans Peter Freyther | a128d91 | 2010-04-01 08:47:12 +0200 | [diff] [blame] | 113 | 0x00, 0x03, 0xfc, |
| 114 | 0x20, 0x20, 0x20, |
| 115 | }; |
| 116 | |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 117 | /* location updating request */ |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 118 | static const uint8_t bss_lu[] = { |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 119 | 0x00, 0x2e, 0xfd, |
| 120 | 0x01, 0x91, 0x45, 0x14, 0x02, 0x02, 0x04, 0x02, |
| 121 | 0x42, 0xfe, 0x0f, 0x21, 0x00, 0x1f, 0x57, 0x05, |
| 122 | 0x08, 0x00, 0x72, 0xf4, 0x80, 0x20, 0x14, 0xc3, |
| 123 | 0x50, 0x17, 0x12, 0x05, 0x08, 0x70, 0x72, 0xf4, |
| 124 | 0x80, 0xff, 0xfe, 0x30, 0x08, 0x29, 0x44, 0x50, |
| 125 | 0x12, 0x03, 0x24, 0x01, 0x95, 0x00 |
| 126 | }; |
| 127 | |
Holger Hans Peter Freyther | f192498 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 128 | /* paging response */ |
| 129 | static const uint8_t pag_resp[] = { |
| 130 | 0x00, 0x2c, 0xfd, 0x01, 0xe5, 0x68, |
| 131 | 0x14, 0x02, 0x02, 0x04, 0x02, 0x42, 0xfe, 0x0f, |
| 132 | 0x1f, 0x00, 0x1d, 0x57, 0x05, 0x08, 0x00, 0x72, |
| 133 | 0xf4, 0x80, 0x20, 0x16, 0xc3, 0x50, 0x17, 0x10, |
| 134 | 0x06, 0x27, 0x01, 0x03, 0x30, 0x18, 0x96, 0x08, |
| 135 | 0x29, 0x26, 0x30, 0x32, 0x11, 0x42, 0x01, 0x19, |
| 136 | 0x00 |
| 137 | }; |
| 138 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 139 | struct filter_result { |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 140 | const uint8_t *data; |
Holger Hans Peter Freyther | e2c1520 | 2010-07-23 19:09:21 +0800 | [diff] [blame] | 141 | const uint16_t length; |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 142 | const int dir; |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 143 | const int result; |
| 144 | }; |
| 145 | |
| 146 | static const struct filter_result results[] = { |
| 147 | { |
| 148 | .data = ipa_id, |
| 149 | .length = ARRAY_SIZE(ipa_id), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 150 | .dir = DIR_MSC, |
| 151 | .result = 1, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 152 | }, |
| 153 | { |
| 154 | .data = gsm_reset, |
| 155 | .length = ARRAY_SIZE(gsm_reset), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 156 | .dir = DIR_MSC, |
| 157 | .result = 1, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 158 | }, |
| 159 | { |
| 160 | .data = gsm_reset_ack, |
| 161 | .length = ARRAY_SIZE(gsm_reset_ack), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 162 | .dir = DIR_BSC, |
| 163 | .result = 1, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 164 | }, |
| 165 | { |
| 166 | .data = gsm_paging, |
| 167 | .length = ARRAY_SIZE(gsm_paging), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 168 | .dir = DIR_BSC, |
| 169 | .result = 0, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 170 | }, |
| 171 | { |
| 172 | .data = bssmap_cr, |
| 173 | .length = ARRAY_SIZE(bssmap_cr), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 174 | .dir = DIR_MSC, |
| 175 | .result = 0, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 176 | }, |
| 177 | { |
| 178 | .data = bssmap_cc, |
| 179 | .length = ARRAY_SIZE(bssmap_cc), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 180 | .dir = DIR_BSC, |
| 181 | .result = 0, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 182 | }, |
| 183 | { |
| 184 | .data = bssmap_released, |
| 185 | .length = ARRAY_SIZE(bssmap_released), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 186 | .dir = DIR_MSC, |
| 187 | .result = 0, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 188 | }, |
| 189 | { |
| 190 | .data = bssmap_release_complete, |
| 191 | .length = ARRAY_SIZE(bssmap_release_complete), |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 192 | .dir = DIR_BSC, |
| 193 | .result = 0, |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 194 | }, |
Holger Hans Peter Freyther | a128d91 | 2010-04-01 08:47:12 +0200 | [diff] [blame] | 195 | { |
| 196 | .data = mgcp_msg, |
| 197 | .length = ARRAY_SIZE(mgcp_msg), |
| 198 | .dir = DIR_MSC, |
| 199 | .result = 0, |
| 200 | }, |
Holger Hans Peter Freyther | 0a6f62f | 2010-04-06 10:22:01 +0200 | [diff] [blame] | 201 | { |
| 202 | .data = connnection_it, |
| 203 | .length = ARRAY_SIZE(connnection_it), |
| 204 | .dir = DIR_BSC, |
| 205 | .result = 0, |
| 206 | }, |
| 207 | { |
| 208 | .data = connnection_it, |
| 209 | .length = ARRAY_SIZE(connnection_it), |
| 210 | .dir = DIR_MSC, |
| 211 | .result = 0, |
| 212 | }, |
Holger Hans Peter Freyther | 5e63f92 | 2010-04-21 15:45:26 +0800 | [diff] [blame] | 213 | { |
| 214 | .data = proto_error, |
| 215 | .length = ARRAY_SIZE(proto_error), |
| 216 | .dir = DIR_BSC, |
| 217 | .result = 0, |
| 218 | }, |
| 219 | { |
| 220 | .data = proto_error, |
| 221 | .length = ARRAY_SIZE(proto_error), |
| 222 | .dir = DIR_MSC, |
| 223 | .result = 0, |
| 224 | }, |
| 225 | |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 226 | }; |
| 227 | |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 228 | static void test_filter(void) |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 229 | { |
| 230 | int i; |
| 231 | |
| 232 | |
| 233 | /* start testinh with proper messages */ |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 234 | printf("Testing BSS Filtering.\n"); |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 235 | for (i = 0; i < ARRAY_SIZE(results); ++i) { |
| 236 | int result; |
| 237 | struct bsc_nat_parsed *parsed; |
| 238 | struct msgb *msg = msgb_alloc(4096, "test-message"); |
| 239 | |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 240 | printf("Going to test item: %d\n", i); |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 241 | memcpy(msg->data, results[i].data, results[i].length); |
| 242 | msg->l2h = msgb_put(msg, results[i].length); |
| 243 | |
| 244 | parsed = bsc_nat_parse(msg); |
| 245 | if (!parsed) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 246 | printf("FAIL: Failed to parse the message\n"); |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 247 | continue; |
| 248 | } |
| 249 | |
Holger Hans Peter Freyther | 1d6fb18 | 2010-01-30 11:53:30 +0100 | [diff] [blame] | 250 | result = bsc_nat_filter_ipa(results[i].dir, msg, parsed); |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 251 | if (result != results[i].result) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 252 | printf("FAIL: Not the expected result got: %d wanted: %d\n", |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 253 | result, results[i].result); |
| 254 | } |
| 255 | |
| 256 | msgb_free(msg); |
| 257 | } |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 258 | } |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 259 | |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 260 | #include "bsc_data.c" |
| 261 | |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 262 | static void copy_to_msg(struct msgb *msg, const uint8_t *data, unsigned int length) |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 263 | { |
| 264 | msgb_reset(msg); |
| 265 | msg->l2h = msgb_put(msg, length); |
| 266 | memcpy(msg->l2h, data, msgb_l2len(msg)); |
| 267 | } |
| 268 | |
Holger Hans Peter Freyther | 50be1a9 | 2012-01-10 22:31:39 +0100 | [diff] [blame] | 269 | static void verify_msg(struct msgb *out, const uint8_t *ref, int ref_len) |
| 270 | { |
| 271 | if (out->len != ref_len) { |
Holger Hans Peter Freyther | be53012 | 2012-01-18 17:20:23 +0100 | [diff] [blame] | 272 | printf("FAIL: The size should match: %d vs. %d\n", |
| 273 | out->len, ref_len); |
| 274 | printf("%s\n", osmo_hexdump(out->data, out->len)); |
| 275 | printf("Wanted\n"); |
| 276 | printf("%s\n", osmo_hexdump(ref, ref_len)); |
Holger Hans Peter Freyther | 50be1a9 | 2012-01-10 22:31:39 +0100 | [diff] [blame] | 277 | abort(); |
| 278 | } |
| 279 | |
| 280 | if (memcmp(out->data, ref, out->len) != 0) { |
| 281 | printf("FAIL: the data should be changed.\n"); |
| 282 | printf("%s\n", osmo_hexdump(out->data, out->len)); |
| 283 | printf("Wanted\n"); |
| 284 | printf("%s\n", osmo_hexdump(ref, ref_len)); |
| 285 | abort(); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 290 | #define VERIFY(con_found, con, msg, ver, str) \ |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 291 | if (!con_found) { \ |
| 292 | printf("Failed to find connection.\n"); \ |
| 293 | abort(); \ |
| 294 | } \ |
| 295 | if (con_found->bsc != con) { \ |
| 296 | printf("Got connection of the wrong BSC: %d\n", \ |
| 297 | con_found->bsc->cfg->nr); \ |
| 298 | abort(); \ |
| 299 | } \ |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 300 | if (memcmp(msg->data, ver, sizeof(ver)) != 0) { \ |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 301 | printf("Failed to patch the %s msg.\n", str); \ |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 302 | abort(); \ |
| 303 | } |
| 304 | |
| 305 | /* test conn tracking once */ |
| 306 | static void test_contrack() |
| 307 | { |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 308 | struct bsc_nat *nat; |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 309 | struct bsc_connection *con; |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 310 | struct nat_sccp_connection *con_found; |
| 311 | struct nat_sccp_connection *rc_con; |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 312 | struct bsc_nat_parsed *parsed; |
| 313 | struct msgb *msg; |
| 314 | |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 315 | printf("Testing connection tracking.\n"); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 316 | nat = bsc_nat_alloc(); |
| 317 | con = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 318 | con->cfg = bsc_config_alloc(nat, "foo"); |
| 319 | bsc_config_add_lac(con->cfg, 23); |
| 320 | bsc_config_add_lac(con->cfg, 49); |
| 321 | bsc_config_add_lac(con->cfg, 42); |
| 322 | bsc_config_del_lac(con->cfg, 49); |
| 323 | bsc_config_add_lac(con->cfg, 1111); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 324 | msg = msgb_alloc(4096, "test"); |
| 325 | |
| 326 | /* 1.) create a connection */ |
| 327 | copy_to_msg(msg, bsc_cr, sizeof(bsc_cr)); |
| 328 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | b5513ca | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 329 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, con); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 330 | if (con_found != NULL) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 331 | printf("Con should not exist realref(%u)\n", |
| 332 | sccp_src_ref_to_int(&con_found->real_ref)); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 333 | abort(); |
| 334 | } |
Holger Hans Peter Freyther | fa20c94 | 2010-05-16 16:51:31 +0800 | [diff] [blame] | 335 | rc_con = create_sccp_src_ref(con, parsed); |
| 336 | if (!rc_con) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 337 | printf("Failed to create a ref\n"); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 338 | abort(); |
| 339 | } |
Holger Hans Peter Freyther | b5513ca | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 340 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, con); |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 341 | if (!con_found) { |
| 342 | printf("Failed to find connection.\n"); |
| 343 | abort(); |
| 344 | } |
| 345 | if (con_found->bsc != con) { |
| 346 | printf("Got connection of the wrong BSC: %d\n", |
| 347 | con_found->bsc->cfg->nr); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 348 | abort(); |
| 349 | } |
Holger Hans Peter Freyther | fa20c94 | 2010-05-16 16:51:31 +0800 | [diff] [blame] | 350 | if (con_found != rc_con) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 351 | printf("Failed to find the right connection.\n"); |
Holger Hans Peter Freyther | fa20c94 | 2010-05-16 16:51:31 +0800 | [diff] [blame] | 352 | abort(); |
| 353 | } |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 354 | if (memcmp(msg->data, bsc_cr_patched, sizeof(bsc_cr_patched)) != 0) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 355 | printf("Failed to patch the BSC CR msg.\n"); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 356 | abort(); |
| 357 | } |
| 358 | talloc_free(parsed); |
| 359 | |
| 360 | /* 2.) get the cc */ |
| 361 | copy_to_msg(msg, msc_cc, sizeof(msc_cc)); |
| 362 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | 49c7fb5 | 2010-06-15 18:48:55 +0800 | [diff] [blame] | 363 | con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 364 | VERIFY(con_found, con, msg, msc_cc_patched, "MSC CC"); |
| 365 | if (update_sccp_src_ref(con_found, parsed) != 0) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 366 | printf("Failed to update the SCCP con.\n"); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 367 | abort(); |
| 368 | } |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 369 | |
| 370 | /* 3.) send some data */ |
| 371 | copy_to_msg(msg, bsc_dtap, sizeof(bsc_dtap)); |
| 372 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | b5513ca | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 373 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, con); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 374 | VERIFY(con_found, con, msg, bsc_dtap_patched, "BSC DTAP"); |
| 375 | |
| 376 | /* 4.) receive some data */ |
| 377 | copy_to_msg(msg, msc_dtap, sizeof(msc_dtap)); |
| 378 | parsed = bsc_nat_parse(msg); |
| 379 | con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 380 | VERIFY(con_found, con, msg, msc_dtap_patched, "MSC DTAP"); |
| 381 | |
| 382 | /* 5.) close the connection */ |
| 383 | copy_to_msg(msg, msc_rlsd, sizeof(msc_rlsd)); |
| 384 | parsed = bsc_nat_parse(msg); |
| 385 | con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat); |
| 386 | VERIFY(con_found, con, msg, msc_rlsd_patched, "MSC RLSD"); |
| 387 | |
| 388 | /* 6.) confirm the connection close */ |
| 389 | copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc)); |
| 390 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | b5513ca | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 391 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, con); |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 392 | if (!con_found) { |
| 393 | printf("Failed to find connection.\n"); |
| 394 | abort(); |
| 395 | } |
| 396 | if (con_found->bsc != con) { |
| 397 | printf("Got connection of the wrong BSC: %d\n", |
| 398 | con_found->bsc->cfg->nr); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 399 | abort(); |
| 400 | } |
| 401 | if (memcmp(msg->data, bsc_rlc_patched, sizeof(bsc_rlc_patched)) != 0) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 402 | printf("Failed to patch the BSC CR msg.\n"); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 403 | abort(); |
| 404 | } |
| 405 | remove_sccp_src_ref(con, msg, parsed); |
Holger Hans Peter Freyther | 9d51855 | 2010-04-05 21:44:51 +0200 | [diff] [blame] | 406 | talloc_free(parsed); |
| 407 | |
| 408 | copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc)); |
| 409 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | b5513ca | 2010-04-21 18:56:12 +0800 | [diff] [blame] | 410 | con_found = patch_sccp_src_ref_to_msc(msg, parsed, con); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 411 | |
| 412 | /* verify that it is gone */ |
| 413 | if (con_found != NULL) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 414 | printf("Con should not exist real_ref(%u)\n", |
| 415 | sccp_src_ref_to_int(&con_found->real_ref)); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 416 | abort(); |
| 417 | } |
| 418 | talloc_free(parsed); |
| 419 | |
| 420 | |
Holger Hans Peter Freyther | 9212d9d | 2011-02-27 11:18:41 +0100 | [diff] [blame] | 421 | bsc_config_free(con->cfg); |
Holger Hans Peter Freyther | 73ec698 | 2014-08-29 09:56:58 +0200 | [diff] [blame] | 422 | bsc_nat_free(nat); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 423 | msgb_free(msg); |
| 424 | } |
| 425 | |
Holger Hans Peter Freyther | 04fd992 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 426 | static void test_paging(void) |
| 427 | { |
| 428 | struct bsc_nat *nat; |
| 429 | struct bsc_connection *con; |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 430 | struct bsc_config *cfg; |
Holger Hans Peter Freyther | 04fd992 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 431 | |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 432 | printf("Testing paging by lac.\n"); |
Holger Hans Peter Freyther | 04fd992 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 433 | |
| 434 | nat = bsc_nat_alloc(); |
| 435 | con = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 436 | cfg = bsc_config_alloc(nat, "unknown"); |
| 437 | con->cfg = cfg; |
| 438 | bsc_config_add_lac(cfg, 23); |
Holger Hans Peter Freyther | 04fd992 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 439 | con->authenticated = 1; |
| 440 | llist_add(&con->list_entry, &nat->bsc_connections); |
Holger Hans Peter Freyther | 04fd992 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 441 | |
| 442 | /* Test it by not finding it */ |
Holger Hans Peter Freyther | 1ffe98c | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 443 | if (bsc_config_handles_lac(cfg, 8213) != 0) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 444 | printf("Should not be handled.\n"); |
Holger Hans Peter Freyther | 04fd992 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 445 | abort(); |
| 446 | } |
Holger Hans Peter Freyther | 04fd992 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 447 | |
| 448 | /* Test by finding it */ |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 449 | bsc_config_del_lac(cfg, 23); |
| 450 | bsc_config_add_lac(cfg, 8213); |
Holger Hans Peter Freyther | 1ffe98c | 2011-05-02 16:20:32 +0200 | [diff] [blame] | 451 | if (bsc_config_handles_lac(cfg, 8213) == 0) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 452 | printf("Should have found it.\n"); |
Holger Hans Peter Freyther | 04fd992 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 453 | abort(); |
| 454 | } |
Holger Hans Peter Freyther | 3615a30 | 2013-06-25 12:03:36 +0200 | [diff] [blame] | 455 | |
Holger Hans Peter Freyther | 73ec698 | 2014-08-29 09:56:58 +0200 | [diff] [blame] | 456 | bsc_nat_free(nat); |
Holger Hans Peter Freyther | 04fd992 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 457 | } |
| 458 | |
Holger Hans Peter Freyther | a9e9331 | 2011-02-26 11:38:00 +0100 | [diff] [blame] | 459 | static void test_mgcp_allocations(void) |
| 460 | { |
| 461 | #if 0 |
| 462 | struct bsc_connection *bsc; |
| 463 | struct bsc_nat *nat; |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 464 | struct nat_sccp_connection con; |
Holger Hans Peter Freyther | 9ec030d | 2011-02-27 11:04:27 +0100 | [diff] [blame] | 465 | int i, j, multiplex; |
Holger Hans Peter Freyther | a9e9331 | 2011-02-26 11:38:00 +0100 | [diff] [blame] | 466 | |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 467 | printf("Testing MGCP.\n"); |
Holger Hans Peter Freyther | a9e9331 | 2011-02-26 11:38:00 +0100 | [diff] [blame] | 468 | memset(&con, 0, sizeof(con)); |
| 469 | |
| 470 | nat = bsc_nat_alloc(); |
| 471 | nat->bsc_endpoints = talloc_zero_array(nat, |
| 472 | struct bsc_endpoint, |
| 473 | 65); |
| 474 | nat->mgcp_cfg = mgcp_config_alloc(); |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 475 | nat->mgcp_cfg->trunk.number_endpoints = 64; |
Holger Hans Peter Freyther | a9e9331 | 2011-02-26 11:38:00 +0100 | [diff] [blame] | 476 | |
| 477 | bsc = bsc_connection_alloc(nat); |
| 478 | bsc->cfg = bsc_config_alloc(nat, "foo"); |
Holger Hans Peter Freyther | 9ec030d | 2011-02-27 11:04:27 +0100 | [diff] [blame] | 479 | bsc->cfg->max_endpoints = 60; |
Holger Hans Peter Freyther | a9e9331 | 2011-02-26 11:38:00 +0100 | [diff] [blame] | 480 | bsc_config_add_lac(bsc->cfg, 2323); |
| 481 | bsc->last_endpoint = 0x22; |
| 482 | con.bsc = bsc; |
| 483 | |
| 484 | bsc_init_endps_if_needed(bsc); |
| 485 | |
| 486 | i = 1; |
| 487 | do { |
| 488 | if (bsc_assign_endpoint(bsc, &con) != 0) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 489 | printf("failed to allocate... on iteration %d\n", i); |
Holger Hans Peter Freyther | a9e9331 | 2011-02-26 11:38:00 +0100 | [diff] [blame] | 490 | break; |
| 491 | } |
| 492 | ++i; |
| 493 | } while(1); |
| 494 | |
Holger Hans Peter Freyther | 9ec030d | 2011-02-27 11:04:27 +0100 | [diff] [blame] | 495 | multiplex = bsc_mgcp_nr_multiplexes(bsc->cfg->max_endpoints); |
| 496 | for (i = 0; i < multiplex; ++i) { |
Holger Hans Peter Freyther | a9e9331 | 2011-02-26 11:38:00 +0100 | [diff] [blame] | 497 | for (j = 0; j < 32; ++j) |
| 498 | printf("%d", bsc->_endpoint_status[i*32 + j]); |
| 499 | printf(": %d of %d\n", i*32 + 32, 32 * 8); |
| 500 | } |
| 501 | #endif |
| 502 | } |
| 503 | |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 504 | static void test_mgcp_ass_tracking(void) |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 505 | { |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 506 | struct bsc_connection *bsc; |
| 507 | struct bsc_nat *nat; |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 508 | struct nat_sccp_connection con; |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 509 | struct bsc_nat_parsed *parsed; |
| 510 | struct msgb *msg; |
| 511 | |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 512 | printf("Testing MGCP.\n"); |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 513 | memset(&con, 0, sizeof(con)); |
| 514 | |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 515 | nat = bsc_nat_alloc(); |
| 516 | nat->bsc_endpoints = talloc_zero_array(nat, |
| 517 | struct bsc_endpoint, |
| 518 | 33); |
Holger Hans Peter Freyther | 7e0cc50 | 2011-02-25 12:43:58 +0100 | [diff] [blame] | 519 | nat->mgcp_cfg = mgcp_config_alloc(); |
Holger Hans Peter Freyther | 88ad772 | 2011-02-28 00:56:17 +0100 | [diff] [blame] | 520 | nat->mgcp_cfg->trunk.number_endpoints = 64; |
Holger Hans Peter Freyther | 462b7d7 | 2012-10-24 21:53:40 +0200 | [diff] [blame] | 521 | mgcp_endpoints_allocate(&nat->mgcp_cfg->trunk); |
Holger Hans Peter Freyther | 7e0cc50 | 2011-02-25 12:43:58 +0100 | [diff] [blame] | 522 | |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 523 | bsc = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 524 | bsc->cfg = bsc_config_alloc(nat, "foo"); |
| 525 | bsc_config_add_lac(bsc->cfg, 2323); |
Holger Hans Peter Freyther | 86c1db6 | 2011-02-25 17:10:25 +0100 | [diff] [blame] | 526 | bsc->last_endpoint = 0x1e; |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 527 | con.bsc = bsc; |
| 528 | |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 529 | msg = msgb_alloc(4096, "foo"); |
| 530 | copy_to_msg(msg, ass_cmd, sizeof(ass_cmd)); |
| 531 | parsed = bsc_nat_parse(msg); |
Holger Hans Peter Freyther | cd70237 | 2010-09-20 01:21:51 +0800 | [diff] [blame] | 532 | |
| 533 | if (msg->l2h[16] != 0 || |
| 534 | msg->l2h[17] != 0x1) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 535 | printf("Input is not as expected.. %s 0x%x\n", |
Pablo Neira Ayuso | c0d17f2 | 2011-05-07 12:12:48 +0200 | [diff] [blame] | 536 | osmo_hexdump(msg->l2h, msgb_l2len(msg)), |
Holger Hans Peter Freyther | cd70237 | 2010-09-20 01:21:51 +0800 | [diff] [blame] | 537 | msg->l2h[17]); |
| 538 | abort(); |
| 539 | } |
| 540 | |
Holger Hans Peter Freyther | 45fd07d | 2010-08-28 18:22:14 +0800 | [diff] [blame] | 541 | if (bsc_mgcp_assign_patch(&con, msg) != 0) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 542 | printf("Failed to handle assignment.\n"); |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 543 | abort(); |
| 544 | } |
| 545 | |
Holger Hans Peter Freyther | cd70237 | 2010-09-20 01:21:51 +0800 | [diff] [blame] | 546 | if (con.msc_endp != 1) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 547 | printf("Timeslot should be 1.\n"); |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 548 | abort(); |
| 549 | } |
| 550 | |
Holger Hans Peter Freyther | 86c1db6 | 2011-02-25 17:10:25 +0100 | [diff] [blame] | 551 | if (con.bsc_endp != 0x1) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 552 | printf("Assigned timeslot should have been 1.\n"); |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 553 | abort(); |
| 554 | } |
Holger Hans Peter Freyther | 86c1db6 | 2011-02-25 17:10:25 +0100 | [diff] [blame] | 555 | if (con.bsc->_endpoint_status[0x1] != 1) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 556 | printf("The status on the BSC is wrong.\n"); |
Holger Hans Peter Freyther | 45fd07d | 2010-08-28 18:22:14 +0800 | [diff] [blame] | 557 | abort(); |
| 558 | } |
| 559 | |
Holger Hans Peter Freyther | cd70237 | 2010-09-20 01:21:51 +0800 | [diff] [blame] | 560 | int multiplex, timeslot; |
Holger Hans Peter Freyther | 86c1db6 | 2011-02-25 17:10:25 +0100 | [diff] [blame] | 561 | mgcp_endpoint_to_timeslot(0x1, &multiplex, ×lot); |
Holger Hans Peter Freyther | cd70237 | 2010-09-20 01:21:51 +0800 | [diff] [blame] | 562 | |
| 563 | uint16_t cic = htons(timeslot & 0x1f); |
| 564 | if (memcmp(&cic, &msg->l2h[16], sizeof(cic)) != 0) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 565 | printf("Message was not patched properly\n"); |
| 566 | printf("data cic: 0x%x %s\n", cic, osmo_hexdump(msg->l2h, msgb_l2len(msg))); |
Holger Hans Peter Freyther | cd70237 | 2010-09-20 01:21:51 +0800 | [diff] [blame] | 567 | abort(); |
| 568 | } |
| 569 | |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 570 | talloc_free(parsed); |
| 571 | |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 572 | bsc_mgcp_dlcx(&con); |
Holger Hans Peter Freyther | d2df4ca | 2010-09-19 20:54:21 +0800 | [diff] [blame] | 573 | if (con.bsc_endp != -1 || con.msc_endp != -1 || |
Holger Hans Peter Freyther | 86c1db6 | 2011-02-25 17:10:25 +0100 | [diff] [blame] | 574 | con.bsc->_endpoint_status[1] != 0 || con.bsc->last_endpoint != 0x1) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 575 | printf("Clearing should remove the mapping.\n"); |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 576 | abort(); |
| 577 | } |
Holger Hans Peter Freyther | 7b7eef6 | 2010-04-22 12:08:17 +0800 | [diff] [blame] | 578 | |
Holger Hans Peter Freyther | 9212d9d | 2011-02-27 11:18:41 +0100 | [diff] [blame] | 579 | bsc_config_free(bsc->cfg); |
Holger Hans Peter Freyther | 73ec698 | 2014-08-29 09:56:58 +0200 | [diff] [blame] | 580 | bsc_nat_free(nat); |
Holger Hans Peter Freyther | 465313e | 2010-06-15 18:49:53 +0800 | [diff] [blame] | 581 | } |
| 582 | |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 583 | /* test the code to find a given connection */ |
| 584 | static void test_mgcp_find(void) |
| 585 | { |
| 586 | struct bsc_nat *nat; |
| 587 | struct bsc_connection *con; |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 588 | struct nat_sccp_connection *sccp_con; |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 589 | |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 590 | printf("Testing finding of a BSC Connection\n"); |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 591 | |
| 592 | nat = bsc_nat_alloc(); |
| 593 | con = bsc_connection_alloc(nat); |
| 594 | llist_add(&con->list_entry, &nat->bsc_connections); |
| 595 | |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 596 | sccp_con = talloc_zero(con, struct nat_sccp_connection); |
Holger Hans Peter Freyther | f4b3439 | 2010-08-28 16:08:39 +0800 | [diff] [blame] | 597 | sccp_con->msc_endp = 12; |
| 598 | sccp_con->bsc_endp = 12; |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 599 | sccp_con->bsc = con; |
| 600 | llist_add(&sccp_con->list_entry, &nat->sccp_connections); |
| 601 | |
| 602 | if (bsc_mgcp_find_con(nat, 11) != NULL) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 603 | printf("Found the wrong connection.\n"); |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 604 | abort(); |
| 605 | } |
| 606 | |
Holger Hans Peter Freyther | 08a1b16 | 2010-04-18 02:26:16 +0800 | [diff] [blame] | 607 | if (bsc_mgcp_find_con(nat, 12) != sccp_con) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 608 | printf("Didn't find the connection\n"); |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 609 | abort(); |
| 610 | } |
| 611 | |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 612 | /* free everything */ |
Holger Hans Peter Freyther | 73ec698 | 2014-08-29 09:56:58 +0200 | [diff] [blame] | 613 | bsc_nat_free(nat); |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 614 | } |
| 615 | |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 616 | static void test_mgcp_rewrite(void) |
| 617 | { |
| 618 | int i; |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 619 | struct msgb *output; |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 620 | printf("Testing rewriting MGCP messages.\n"); |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 621 | |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 622 | for (i = 0; i < ARRAY_SIZE(mgcp_messages); ++i) { |
| 623 | const char *orig = mgcp_messages[i].orig; |
| 624 | const char *patc = mgcp_messages[i].patch; |
| 625 | const char *ip = mgcp_messages[i].ip; |
| 626 | const int port = mgcp_messages[i].port; |
Jacob Erlbeck | a52ac66 | 2013-11-29 13:43:45 +0100 | [diff] [blame] | 627 | const int expected_payload_type = mgcp_messages[i].payload_type; |
| 628 | int payload_type = -1; |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 629 | |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 630 | char *input = strdup(orig); |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 631 | |
Jacob Erlbeck | a52ac66 | 2013-11-29 13:43:45 +0100 | [diff] [blame] | 632 | output = bsc_mgcp_rewrite(input, strlen(input), 0x1e, |
Pablo Neira Ayuso | dfa91df | 2014-08-29 09:12:41 +0200 | [diff] [blame] | 633 | ip, port, -1, &payload_type); |
Jacob Erlbeck | a52ac66 | 2013-11-29 13:43:45 +0100 | [diff] [blame] | 634 | |
| 635 | if (payload_type != -1) { |
| 636 | fprintf(stderr, "Found media payload type %d in SDP data\n", |
| 637 | payload_type); |
| 638 | if (payload_type != expected_payload_type) { |
| 639 | printf("Wrong payload type %d (expected %d)\n", |
| 640 | payload_type, expected_payload_type); |
| 641 | abort(); |
| 642 | } |
| 643 | } |
| 644 | |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 645 | if (msgb_l2len(output) != strlen(patc)) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 646 | printf("Wrong sizes for test: %d %d != %d != %d\n", i, msgb_l2len(output), strlen(patc), strlen(orig)); |
| 647 | printf("String '%s' vs '%s'\n", (const char *) output->l2h, patc); |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 648 | abort(); |
| 649 | } |
| 650 | |
| 651 | if (memcmp(output->l2h, patc, msgb_l2len(output)) != 0) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 652 | printf("Broken on %d msg: '%s'\n", i, (const char *) output->l2h); |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 653 | abort(); |
| 654 | } |
| 655 | |
| 656 | msgb_free(output); |
Holger Hans Peter Freyther | 8d20065 | 2010-04-04 18:09:10 +0200 | [diff] [blame] | 657 | free(input); |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 658 | } |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 659 | } |
| 660 | |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 661 | static void test_mgcp_parse(void) |
| 662 | { |
| 663 | int code, ci; |
| 664 | char transaction[60]; |
| 665 | |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 666 | printf("Testing MGCP response parsing.\n"); |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 667 | |
| 668 | if (bsc_mgcp_parse_response(crcx_resp, &code, transaction) != 0) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 669 | printf("Failed to parse CRCX resp.\n"); |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 670 | abort(); |
| 671 | } |
| 672 | |
| 673 | if (code != 200) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 674 | printf("Failed to parse the CODE properly. Got: %d\n", code); |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 675 | abort(); |
| 676 | } |
| 677 | |
| 678 | if (strcmp(transaction, "23265295") != 0) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 679 | printf("Failed to parse transaction id: '%s'\n", transaction); |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 680 | abort(); |
| 681 | } |
| 682 | |
| 683 | ci = bsc_mgcp_extract_ci(crcx_resp); |
| 684 | if (ci != 1) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 685 | printf("Failed to parse the CI. Got: %d\n", ci); |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 686 | abort(); |
| 687 | } |
| 688 | } |
| 689 | |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 690 | struct cr_filter { |
Holger Hans Peter Freyther | dbd16fe | 2010-07-23 19:08:55 +0800 | [diff] [blame] | 691 | const uint8_t *data; |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 692 | int length; |
| 693 | int result; |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 694 | int contype; |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 695 | |
| 696 | const char *bsc_imsi_allow; |
| 697 | const char *bsc_imsi_deny; |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 698 | const char *nat_imsi_deny; |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 699 | int nat_cm_reject_cause; |
| 700 | int nat_lu_reject_cause; |
| 701 | int bsc_cm_reject_cause; |
| 702 | int bsc_lu_reject_cause; |
| 703 | int want_cm_reject_cause; |
| 704 | int want_lu_reject_cause; |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 705 | }; |
| 706 | |
| 707 | static struct cr_filter cr_filter[] = { |
| 708 | { |
| 709 | .data = bssmap_cr, |
| 710 | .length = sizeof(bssmap_cr), |
Holger Hans Peter Freyther | d880f54 | 2010-09-15 01:42:07 +0800 | [diff] [blame] | 711 | .result = 1, |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 712 | .contype = NAT_CON_TYPE_CM_SERV_REQ, |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 713 | .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 714 | .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 715 | .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 716 | .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 717 | .want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 718 | .want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 719 | }, |
| 720 | { |
| 721 | .data = bss_lu, |
| 722 | .length = sizeof(bss_lu), |
Holger Hans Peter Freyther | d880f54 | 2010-09-15 01:42:07 +0800 | [diff] [blame] | 723 | .result = 1, |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 724 | .contype = NAT_CON_TYPE_LU, |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 725 | .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 726 | .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 727 | .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 728 | .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 729 | .want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 730 | .want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 731 | }, |
| 732 | { |
Holger Hans Peter Freyther | f192498 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 733 | .data = pag_resp, |
| 734 | .length = sizeof(pag_resp), |
Holger Hans Peter Freyther | d880f54 | 2010-09-15 01:42:07 +0800 | [diff] [blame] | 735 | .result = 1, |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 736 | .contype = NAT_CON_TYPE_PAG_RESP, |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 737 | .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 738 | .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 739 | .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 740 | .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 741 | .want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 742 | .want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
Holger Hans Peter Freyther | f192498 | 2010-05-15 23:54:04 +0800 | [diff] [blame] | 743 | }, |
| 744 | { |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 745 | /* nat deny is before blank/null BSC */ |
| 746 | .data = bss_lu, |
| 747 | .length = sizeof(bss_lu), |
| 748 | .result = -3, |
| 749 | .nat_imsi_deny = "[0-9]*", |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 750 | .contype = NAT_CON_TYPE_LU, |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 751 | .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 752 | .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 753 | .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 754 | .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 755 | .want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 756 | .want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 757 | }, |
| 758 | { |
Holger Hans Peter Freyther | a0aeaa7 | 2010-05-14 23:07:58 +0800 | [diff] [blame] | 759 | /* BSC allow is before NAT deny */ |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 760 | .data = bss_lu, |
| 761 | .length = sizeof(bss_lu), |
Holger Hans Peter Freyther | d880f54 | 2010-09-15 01:42:07 +0800 | [diff] [blame] | 762 | .result = 1, |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 763 | .nat_imsi_deny = "[0-9]*", |
Holger Hans Peter Freyther | a0aeaa7 | 2010-05-14 23:07:58 +0800 | [diff] [blame] | 764 | .bsc_imsi_allow = "2440[0-9]*", |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 765 | .contype = NAT_CON_TYPE_LU, |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 766 | .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 767 | .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 768 | .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 769 | .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 770 | .want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 771 | .want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 772 | }, |
| 773 | { |
| 774 | /* BSC allow is before NAT deny */ |
| 775 | .data = bss_lu, |
| 776 | .length = sizeof(bss_lu), |
Holger Hans Peter Freyther | d880f54 | 2010-09-15 01:42:07 +0800 | [diff] [blame] | 777 | .result = 1, |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 778 | .bsc_imsi_allow = "[0-9]*", |
| 779 | .nat_imsi_deny = "[0-9]*", |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 780 | .contype = NAT_CON_TYPE_LU, |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 781 | .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 782 | .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 783 | .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 784 | .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 785 | .want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 786 | .want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 787 | }, |
| 788 | { |
| 789 | /* filter as deny is first */ |
| 790 | .data = bss_lu, |
| 791 | .length = sizeof(bss_lu), |
Holger Hans Peter Freyther | 1fd6063 | 2010-10-19 20:55:33 +0200 | [diff] [blame] | 792 | .result = 1, |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 793 | .bsc_imsi_deny = "[0-9]*", |
| 794 | .bsc_imsi_allow = "[0-9]*", |
| 795 | .nat_imsi_deny = "[0-9]*", |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 796 | .contype = NAT_CON_TYPE_LU, |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 797 | .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 798 | .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 799 | .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 800 | .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 801 | .want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 802 | .want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 803 | }, |
Holger Hans Peter Freyther | 1fd6063 | 2010-10-19 20:55:33 +0200 | [diff] [blame] | 804 | { |
| 805 | /* deny by nat rule */ |
| 806 | .data = bss_lu, |
| 807 | .length = sizeof(bss_lu), |
| 808 | .result = -3, |
| 809 | .bsc_imsi_deny = "000[0-9]*", |
| 810 | .nat_imsi_deny = "[0-9]*", |
| 811 | .contype = NAT_CON_TYPE_LU, |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 812 | .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 813 | .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 814 | .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 815 | .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 816 | .want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 817 | .want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 818 | }, |
| 819 | { |
| 820 | /* deny by nat rule */ |
| 821 | .data = bss_lu, |
| 822 | .length = sizeof(bss_lu), |
| 823 | .result = -3, |
| 824 | .bsc_imsi_deny = "000[0-9]*", |
| 825 | .nat_imsi_deny = "[0-9]*", |
| 826 | .contype = NAT_CON_TYPE_LU, |
| 827 | .nat_cm_reject_cause = 0x23, |
| 828 | .nat_lu_reject_cause = 0x42, |
| 829 | .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 830 | .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 831 | .want_lu_reject_cause = 0x42, |
| 832 | .want_cm_reject_cause = 0x23, |
Holger Hans Peter Freyther | 1fd6063 | 2010-10-19 20:55:33 +0200 | [diff] [blame] | 833 | }, |
| 834 | { |
| 835 | /* deny by bsc rule */ |
| 836 | .data = bss_lu, |
| 837 | .length = sizeof(bss_lu), |
| 838 | .result = -2, |
| 839 | .bsc_imsi_deny = "[0-9]*", |
| 840 | .contype = NAT_CON_TYPE_LU, |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 841 | .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 842 | .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 843 | .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 844 | .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 845 | .want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 846 | .want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
Holger Hans Peter Freyther | 1fd6063 | 2010-10-19 20:55:33 +0200 | [diff] [blame] | 847 | }, |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 848 | { |
| 849 | /* deny by bsc rule */ |
| 850 | .data = bss_lu, |
| 851 | .length = sizeof(bss_lu), |
| 852 | .result = -2, |
| 853 | .bsc_imsi_deny = "[0-9]*", |
| 854 | .contype = NAT_CON_TYPE_LU, |
| 855 | .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 856 | .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED, |
| 857 | .bsc_cm_reject_cause = 0x42, |
| 858 | .bsc_lu_reject_cause = 0x23, |
| 859 | .want_lu_reject_cause = 0x23, |
| 860 | .want_cm_reject_cause = 0x42, |
| 861 | }, |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 862 | }; |
| 863 | |
Holger Hans Peter Freyther | 0c08db1 | 2010-05-14 08:12:57 +0800 | [diff] [blame] | 864 | static void test_cr_filter() |
| 865 | { |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 866 | int i, res, contype; |
Holger Hans Peter Freyther | 0c08db1 | 2010-05-14 08:12:57 +0800 | [diff] [blame] | 867 | struct msgb *msg = msgb_alloc(4096, "test_cr_filter"); |
Holger Hans Peter Freyther | 0c08db1 | 2010-05-14 08:12:57 +0800 | [diff] [blame] | 868 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | 29c6703 | 2010-06-08 10:14:44 +0800 | [diff] [blame] | 869 | struct bsc_nat_acc_lst *nat_lst, *bsc_lst; |
Holger Hans Peter Freyther | d77c817 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 870 | struct bsc_nat_acc_lst_entry *nat_entry, *bsc_entry; |
Holger Hans Peter Freyther | bdf764a | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 871 | struct bsc_nat_reject_cause cause; |
Holger Hans Peter Freyther | 0c08db1 | 2010-05-14 08:12:57 +0800 | [diff] [blame] | 872 | |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 873 | struct bsc_nat *nat = bsc_nat_alloc(); |
| 874 | struct bsc_connection *bsc = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 875 | bsc->cfg = bsc_config_alloc(nat, "foo"); |
| 876 | bsc_config_add_lac(bsc->cfg, 1234); |
Holger Hans Peter Freyther | 8affef5 | 2010-06-01 01:03:13 +0800 | [diff] [blame] | 877 | bsc->cfg->acc_lst_name = "bsc"; |
| 878 | nat->acc_lst_name = "nat"; |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 879 | |
Holger Hans Peter Freyther | d77c817 | 2010-06-08 10:53:39 +0800 | [diff] [blame] | 880 | nat_lst = bsc_nat_acc_lst_get(nat, "nat"); |
| 881 | bsc_lst = bsc_nat_acc_lst_get(nat, "bsc"); |
| 882 | |
| 883 | bsc_entry = bsc_nat_acc_lst_entry_create(bsc_lst); |
| 884 | nat_entry = bsc_nat_acc_lst_entry_create(nat_lst); |
| 885 | |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 886 | /* test the default value as we are going to overwrite it */ |
| 887 | OSMO_ASSERT(bsc_entry->cm_reject_cause == GSM48_REJECT_PLMN_NOT_ALLOWED); |
| 888 | OSMO_ASSERT(bsc_entry->lu_reject_cause == GSM48_REJECT_PLMN_NOT_ALLOWED); |
| 889 | |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 890 | for (i = 0; i < ARRAY_SIZE(cr_filter); ++i) { |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 891 | char *imsi; |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 892 | msgb_reset(msg); |
| 893 | copy_to_msg(msg, cr_filter[i].data, cr_filter[i].length); |
| 894 | |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 895 | bsc_entry->cm_reject_cause = cr_filter[i].bsc_cm_reject_cause; |
| 896 | bsc_entry->lu_reject_cause = cr_filter[i].bsc_lu_reject_cause; |
| 897 | nat_entry->cm_reject_cause = cr_filter[i].nat_cm_reject_cause; |
| 898 | nat_entry->lu_reject_cause = cr_filter[i].nat_lu_reject_cause; |
| 899 | |
Holger Hans Peter Freyther | 06c9da6 | 2011-06-09 21:48:49 +0200 | [diff] [blame] | 900 | if (gsm_parse_reg(nat_entry, &nat_entry->imsi_deny_re, &nat_entry->imsi_deny, |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 901 | cr_filter[i].nat_imsi_deny ? 1 : 0, |
Holger Hans Peter Freyther | 4c9557e | 2011-04-04 19:19:26 +0200 | [diff] [blame] | 902 | &cr_filter[i].nat_imsi_deny) != 0) |
| 903 | abort(); |
Holger Hans Peter Freyther | 06c9da6 | 2011-06-09 21:48:49 +0200 | [diff] [blame] | 904 | if (gsm_parse_reg(bsc_entry, &bsc_entry->imsi_allow_re, &bsc_entry->imsi_allow, |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 905 | cr_filter[i].bsc_imsi_allow ? 1 : 0, |
Holger Hans Peter Freyther | 4c9557e | 2011-04-04 19:19:26 +0200 | [diff] [blame] | 906 | &cr_filter[i].bsc_imsi_allow) != 0) |
| 907 | abort(); |
Holger Hans Peter Freyther | 06c9da6 | 2011-06-09 21:48:49 +0200 | [diff] [blame] | 908 | if (gsm_parse_reg(bsc_entry, &bsc_entry->imsi_deny_re, &bsc_entry->imsi_deny, |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 909 | cr_filter[i].bsc_imsi_deny ? 1 : 0, |
Holger Hans Peter Freyther | 4c9557e | 2011-04-04 19:19:26 +0200 | [diff] [blame] | 910 | &cr_filter[i].bsc_imsi_deny) != 0) |
| 911 | abort(); |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 912 | |
| 913 | parsed = bsc_nat_parse(msg); |
| 914 | if (!parsed) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 915 | printf("FAIL: Failed to parse the message\n"); |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 916 | abort(); |
| 917 | } |
| 918 | |
Holger Hans Peter Freyther | bdf764a | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 919 | memset(&cause, 0, sizeof(cause)); |
| 920 | res = bsc_nat_filter_sccp_cr(bsc, msg, parsed, &contype, &imsi, &cause); |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 921 | if (res != cr_filter[i].result) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 922 | printf("FAIL: Wrong result %d for test %d.\n", res, i); |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 923 | abort(); |
| 924 | } |
| 925 | |
Holger Hans Peter Freyther | 415cd2e | 2014-01-20 09:55:46 +0100 | [diff] [blame] | 926 | |
| 927 | OSMO_ASSERT(cause.cm_reject_cause == cr_filter[i].want_cm_reject_cause); |
| 928 | OSMO_ASSERT(cause.lu_reject_cause == cr_filter[i].want_lu_reject_cause); |
| 929 | |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 930 | if (contype != cr_filter[i].contype) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 931 | printf("FAIL: Wrong contype %d for test %d.\n", res, contype); |
Holger Hans Peter Freyther | 19c0a84 | 2010-05-16 02:00:40 +0800 | [diff] [blame] | 932 | abort(); |
| 933 | } |
| 934 | |
Holger Hans Peter Freyther | 749497e | 2010-09-29 01:19:42 +0800 | [diff] [blame] | 935 | talloc_steal(parsed, imsi); |
Holger Hans Peter Freyther | 34a96ae | 2010-05-14 19:49:35 +0800 | [diff] [blame] | 936 | talloc_free(parsed); |
Holger Hans Peter Freyther | 0c08db1 | 2010-05-14 08:12:57 +0800 | [diff] [blame] | 937 | } |
| 938 | |
Holger Hans Peter Freyther | 0c08db1 | 2010-05-14 08:12:57 +0800 | [diff] [blame] | 939 | msgb_free(msg); |
Holger Hans Peter Freyther | 73ec698 | 2014-08-29 09:56:58 +0200 | [diff] [blame] | 940 | bsc_nat_free(nat); |
Holger Hans Peter Freyther | 0c08db1 | 2010-05-14 08:12:57 +0800 | [diff] [blame] | 941 | } |
| 942 | |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 943 | static void test_dt_filter() |
| 944 | { |
| 945 | int i; |
| 946 | struct msgb *msg = msgb_alloc(4096, "test_dt_filter"); |
| 947 | struct bsc_nat_parsed *parsed; |
Holger Hans Peter Freyther | bdf764a | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 948 | struct bsc_nat_reject_cause cause; |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 949 | |
| 950 | struct bsc_nat *nat = bsc_nat_alloc(); |
| 951 | struct bsc_connection *bsc = bsc_connection_alloc(nat); |
Holger Hans Peter Freyther | c279e39 | 2013-04-16 09:53:13 +0200 | [diff] [blame] | 952 | struct nat_sccp_connection *con = talloc_zero(0, struct nat_sccp_connection); |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 953 | |
Holger Hans Peter Freyther | 0bd60f3 | 2010-10-08 22:08:29 +0800 | [diff] [blame] | 954 | bsc->cfg = bsc_config_alloc(nat, "foo"); |
| 955 | bsc_config_add_lac(bsc->cfg, 23); |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 956 | con->bsc = bsc; |
| 957 | |
| 958 | msgb_reset(msg); |
| 959 | copy_to_msg(msg, id_resp, ARRAY_SIZE(id_resp)); |
| 960 | |
| 961 | parsed = bsc_nat_parse(msg); |
| 962 | if (!parsed) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 963 | printf("FAIL: Could not parse ID resp\n"); |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 964 | abort(); |
| 965 | } |
| 966 | |
| 967 | if (parsed->bssap != BSSAP_MSG_DTAP) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 968 | printf("FAIL: It should be dtap\n"); |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 969 | abort(); |
| 970 | } |
| 971 | |
| 972 | /* gsm_type is actually the size of the dtap */ |
| 973 | if (parsed->gsm_type < msgb_l3len(msg) - 3) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 974 | printf("FAIL: Not enough space for the content\n"); |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 975 | abort(); |
| 976 | } |
| 977 | |
Holger Hans Peter Freyther | bdf764a | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 978 | memset(&cause, 0, sizeof(cause)); |
| 979 | if (bsc_nat_filter_dt(bsc, msg, con, parsed, &cause) != 1) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 980 | printf("FAIL: Should have passed..\n"); |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 981 | abort(); |
| 982 | } |
| 983 | |
| 984 | /* just some basic length checking... */ |
| 985 | for (i = ARRAY_SIZE(id_resp); i >= 0; --i) { |
| 986 | msgb_reset(msg); |
| 987 | copy_to_msg(msg, id_resp, ARRAY_SIZE(id_resp)); |
| 988 | |
| 989 | parsed = bsc_nat_parse(msg); |
| 990 | if (!parsed) |
| 991 | continue; |
| 992 | |
| 993 | con->imsi_checked = 0; |
Holger Hans Peter Freyther | bdf764a | 2012-12-17 14:35:03 +0100 | [diff] [blame] | 994 | memset(&cause, 0, sizeof(cause)); |
| 995 | bsc_nat_filter_dt(bsc, msg, con, parsed, &cause); |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 996 | } |
Holger Hans Peter Freyther | 73ec698 | 2014-08-29 09:56:58 +0200 | [diff] [blame] | 997 | |
| 998 | msgb_free(msg); |
| 999 | bsc_nat_free(nat); |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 1000 | } |
| 1001 | |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 1002 | static void test_setup_rewrite() |
| 1003 | { |
| 1004 | struct msgb *msg = msgb_alloc(4096, "test_dt_filter"); |
| 1005 | struct msgb *out; |
| 1006 | struct bsc_nat_parsed *parsed; |
| 1007 | const char *imsi = "27408000001234"; |
| 1008 | |
| 1009 | struct bsc_nat *nat = bsc_nat_alloc(); |
| 1010 | |
| 1011 | /* a fake list */ |
Pablo Neira Ayuso | ab46cf3 | 2011-05-07 13:11:20 +0200 | [diff] [blame] | 1012 | struct osmo_config_list entries; |
| 1013 | struct osmo_config_entry entry; |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 1014 | |
| 1015 | INIT_LLIST_HEAD(&entries.entry); |
| 1016 | entry.mcc = "274"; |
| 1017 | entry.mnc = "08"; |
| 1018 | entry.option = "^0([1-9])"; |
| 1019 | entry.text = "0049"; |
| 1020 | llist_add_tail(&entry.list, &entries.entry); |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1021 | bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries); |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 1022 | |
| 1023 | /* verify that nothing changed */ |
| 1024 | msgb_reset(msg); |
| 1025 | copy_to_msg(msg, cc_setup_international, ARRAY_SIZE(cc_setup_international)); |
| 1026 | parsed = bsc_nat_parse(msg); |
| 1027 | if (!parsed) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 1028 | printf("FAIL: Could not parse ID resp\n"); |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 1029 | abort(); |
| 1030 | } |
| 1031 | |
Holger Hans Peter Freyther | df8e6e9 | 2011-05-27 14:09:55 +0200 | [diff] [blame] | 1032 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 1033 | if (msg != out) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 1034 | printf("FAIL: The message should not have been changed\n"); |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 1035 | abort(); |
| 1036 | } |
| 1037 | |
Holger Hans Peter Freyther | 50be1a9 | 2012-01-10 22:31:39 +0100 | [diff] [blame] | 1038 | verify_msg(out, cc_setup_international, ARRAY_SIZE(cc_setup_international)); |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 1039 | talloc_free(parsed); |
| 1040 | |
| 1041 | /* verify that something in the message changes */ |
| 1042 | msgb_reset(msg); |
| 1043 | copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national)); |
| 1044 | parsed = bsc_nat_parse(msg); |
| 1045 | if (!parsed) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 1046 | printf("FAIL: Could not parse ID resp\n"); |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 1047 | abort(); |
| 1048 | } |
| 1049 | |
Holger Hans Peter Freyther | df8e6e9 | 2011-05-27 14:09:55 +0200 | [diff] [blame] | 1050 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 1051 | if (!out) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 1052 | printf("FAIL: A new message should be created.\n"); |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 1053 | abort(); |
| 1054 | } |
| 1055 | |
| 1056 | if (msg == out) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 1057 | printf("FAIL: The message should have changed\n"); |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 1058 | abort(); |
| 1059 | } |
| 1060 | |
Holger Hans Peter Freyther | 50be1a9 | 2012-01-10 22:31:39 +0100 | [diff] [blame] | 1061 | verify_msg(out, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched)); |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 1062 | msgb_free(out); |
Holger Hans Peter Freyther | 74779c6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 1063 | |
| 1064 | /* Make sure that a wildcard is matching */ |
| 1065 | entry.mnc = "*"; |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1066 | bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries); |
Holger Hans Peter Freyther | 74779c6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 1067 | msg = msgb_alloc(4096, "test_dt_filter"); |
| 1068 | copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national)); |
| 1069 | parsed = bsc_nat_parse(msg); |
| 1070 | if (!parsed) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 1071 | printf("FAIL: Could not parse ID resp\n"); |
Holger Hans Peter Freyther | 74779c6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 1072 | abort(); |
| 1073 | } |
| 1074 | |
Holger Hans Peter Freyther | df8e6e9 | 2011-05-27 14:09:55 +0200 | [diff] [blame] | 1075 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
Holger Hans Peter Freyther | 74779c6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 1076 | if (!out) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 1077 | printf("FAIL: A new message should be created.\n"); |
Holger Hans Peter Freyther | 74779c6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 1078 | abort(); |
| 1079 | } |
| 1080 | |
| 1081 | if (msg == out) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 1082 | printf("FAIL: The message should have changed\n"); |
Holger Hans Peter Freyther | 74779c6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 1083 | abort(); |
| 1084 | } |
| 1085 | |
Holger Hans Peter Freyther | 50be1a9 | 2012-01-10 22:31:39 +0100 | [diff] [blame] | 1086 | verify_msg(out, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched)); |
Holger Hans Peter Freyther | 74779c6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 1087 | msgb_free(out); |
| 1088 | |
| 1089 | /* Make sure that a wildcard is matching */ |
| 1090 | entry.mnc = "09"; |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1091 | bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries); |
Holger Hans Peter Freyther | 74779c6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 1092 | msg = msgb_alloc(4096, "test_dt_filter"); |
| 1093 | copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national)); |
| 1094 | parsed = bsc_nat_parse(msg); |
| 1095 | if (!parsed) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 1096 | printf("FAIL: Could not parse ID resp\n"); |
Holger Hans Peter Freyther | 74779c6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 1097 | abort(); |
| 1098 | } |
| 1099 | |
Holger Hans Peter Freyther | df8e6e9 | 2011-05-27 14:09:55 +0200 | [diff] [blame] | 1100 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
Holger Hans Peter Freyther | 74779c6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 1101 | if (out != msg) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 1102 | printf("FAIL: The message should be unchanged.\n"); |
Holger Hans Peter Freyther | 74779c6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 1103 | abort(); |
| 1104 | } |
| 1105 | |
Holger Hans Peter Freyther | 50be1a9 | 2012-01-10 22:31:39 +0100 | [diff] [blame] | 1106 | verify_msg(out, cc_setup_national, ARRAY_SIZE(cc_setup_national)); |
Holger Hans Peter Freyther | 74779c6 | 2010-10-28 15:27:00 +0200 | [diff] [blame] | 1107 | msgb_free(out); |
Holger Hans Peter Freyther | dbd9449 | 2013-04-02 12:34:11 +0200 | [diff] [blame] | 1108 | |
| 1109 | /* Now see what happens to an international number */ |
| 1110 | entry.mnc = "*"; |
| 1111 | entry.option = "^\\+[0-9][0-9]([1-9])"; |
| 1112 | entry.text = "0036"; |
| 1113 | bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries); |
| 1114 | msg = msgb_alloc(4096, "test_dt_filter"); |
| 1115 | copy_to_msg(msg, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched)); |
| 1116 | parsed = bsc_nat_parse(msg); |
| 1117 | if (!parsed) { |
| 1118 | printf("FAIL: Could not parse ID resp %d\n", __LINE__); |
| 1119 | abort(); |
| 1120 | } |
| 1121 | |
| 1122 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
| 1123 | if (!out) { |
| 1124 | printf("FAIL: A new message should be created %d.\n", __LINE__); |
| 1125 | abort(); |
| 1126 | } |
| 1127 | |
| 1128 | if (msg == out) { |
| 1129 | printf("FAIL: The message should have changed %d\n", __LINE__); |
| 1130 | abort(); |
| 1131 | } |
| 1132 | |
| 1133 | verify_msg(out, cc_setup_national_patched_patched, |
| 1134 | ARRAY_SIZE(cc_setup_national_patched_patched)); |
| 1135 | msgb_free(out); |
| 1136 | |
| 1137 | /* go from international back to national */ |
| 1138 | entry.mnc = "*"; |
| 1139 | entry.option = "^\\+([0-9])"; |
| 1140 | entry.text = "36"; |
| 1141 | bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries); |
| 1142 | msg = msgb_alloc(4096, "test_dt_filter"); |
| 1143 | copy_to_msg(msg, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched)); |
| 1144 | parsed = bsc_nat_parse(msg); |
| 1145 | if (!parsed) { |
| 1146 | printf("FAIL: Could not parse ID resp %d\n", __LINE__); |
| 1147 | abort(); |
| 1148 | } |
| 1149 | |
| 1150 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
| 1151 | if (!out) { |
| 1152 | printf("FAIL: A new message should be created %d.\n", __LINE__); |
| 1153 | abort(); |
| 1154 | } |
| 1155 | |
| 1156 | if (msg == out) { |
| 1157 | printf("FAIL: The message should have changed %d\n", __LINE__); |
| 1158 | abort(); |
| 1159 | } |
| 1160 | |
| 1161 | verify_msg(out, cc_setup_national_again, |
| 1162 | ARRAY_SIZE(cc_setup_national_again)); |
| 1163 | msgb_free(out); |
Holger Hans Peter Freyther | 3615a30 | 2013-06-25 12:03:36 +0200 | [diff] [blame] | 1164 | bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, NULL); |
Holger Hans Peter Freyther | 73ec698 | 2014-08-29 09:56:58 +0200 | [diff] [blame] | 1165 | bsc_nat_free(nat); |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 1166 | } |
| 1167 | |
Holger Hans Peter Freyther | ddf191e | 2013-06-25 11:44:01 +0200 | [diff] [blame] | 1168 | static void test_setup_rewrite_prefix(void) |
| 1169 | { |
| 1170 | struct msgb *msg = msgb_alloc(4096, "test_dt_filter"); |
| 1171 | struct msgb *out; |
| 1172 | struct bsc_nat_parsed *parsed; |
| 1173 | const char *imsi = "27408000001234"; |
| 1174 | |
| 1175 | struct bsc_nat *nat = bsc_nat_alloc(); |
| 1176 | |
| 1177 | /* a fake list */ |
| 1178 | struct osmo_config_list entries; |
| 1179 | struct osmo_config_entry entry; |
| 1180 | |
| 1181 | INIT_LLIST_HEAD(&entries.entry); |
| 1182 | entry.mcc = "274"; |
| 1183 | entry.mnc = "08"; |
| 1184 | entry.option = "^0([1-9])"; |
| 1185 | entry.text = "prefix_lookup"; |
| 1186 | llist_add_tail(&entry.list, &entries.entry); |
| 1187 | bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries); |
| 1188 | |
| 1189 | nat->num_rewr_trie = nat_rewrite_parse(nat, "prefixes.csv"); |
| 1190 | |
| 1191 | msgb_reset(msg); |
| 1192 | copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national)); |
| 1193 | parsed = bsc_nat_parse(msg); |
| 1194 | if (!parsed) { |
| 1195 | printf("FAIL: Could not parse ID resp\n"); |
| 1196 | abort(); |
| 1197 | } |
| 1198 | |
| 1199 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
| 1200 | if (!out) { |
| 1201 | printf("FAIL: A new message should be created.\n"); |
| 1202 | abort(); |
| 1203 | } |
| 1204 | |
| 1205 | if (msg == out) { |
| 1206 | printf("FAIL: The message should have changed\n"); |
| 1207 | abort(); |
| 1208 | } |
| 1209 | |
| 1210 | verify_msg(out, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched)); |
| 1211 | msgb_free(out); |
| 1212 | |
| 1213 | bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, NULL); |
Holger Hans Peter Freyther | 73ec698 | 2014-08-29 09:56:58 +0200 | [diff] [blame] | 1214 | bsc_nat_free(nat); |
Holger Hans Peter Freyther | ddf191e | 2013-06-25 11:44:01 +0200 | [diff] [blame] | 1215 | } |
| 1216 | |
Holger Hans Peter Freyther | 67e423c | 2013-06-25 15:38:31 +0200 | [diff] [blame] | 1217 | static void test_setup_rewrite_post(void) |
| 1218 | { |
| 1219 | struct msgb *msg = msgb_alloc(4096, "test_dt_filter"); |
| 1220 | struct msgb *out; |
| 1221 | struct bsc_nat_parsed *parsed; |
| 1222 | const char *imsi = "27408000001234"; |
| 1223 | |
| 1224 | struct bsc_nat *nat = bsc_nat_alloc(); |
| 1225 | |
| 1226 | /* a fake list */ |
| 1227 | struct osmo_config_list entries; |
| 1228 | struct osmo_config_entry entry; |
| 1229 | struct osmo_config_list entries_post; |
| 1230 | struct osmo_config_entry entry_post; |
| 1231 | |
| 1232 | INIT_LLIST_HEAD(&entries.entry); |
| 1233 | entry.mcc = "274"; |
| 1234 | entry.mnc = "08"; |
| 1235 | entry.option = "^0([1-9])"; |
| 1236 | entry.text = "0049"; |
| 1237 | llist_add_tail(&entry.list, &entries.entry); |
| 1238 | bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries); |
| 1239 | |
| 1240 | /* attempt to undo the previous one */ |
| 1241 | INIT_LLIST_HEAD(&entries_post.entry); |
| 1242 | entry_post.mcc = "274"; |
| 1243 | entry_post.mnc = "08"; |
| 1244 | entry_post.option = "^\\+49([1-9])"; |
| 1245 | entry_post.text = "prefix_lookup"; |
| 1246 | llist_add_tail(&entry_post.list, &entries_post.entry); |
| 1247 | bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr_post, &entries_post); |
| 1248 | |
| 1249 | nat->num_rewr_trie = nat_rewrite_parse(nat, "prefixes.csv"); |
| 1250 | |
| 1251 | msgb_reset(msg); |
| 1252 | copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national)); |
| 1253 | parsed = bsc_nat_parse(msg); |
| 1254 | if (!parsed) { |
| 1255 | printf("FAIL: Could not parse ID resp\n"); |
| 1256 | abort(); |
| 1257 | } |
| 1258 | |
| 1259 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
| 1260 | if (!out) { |
| 1261 | printf("FAIL: A new message should be created.\n"); |
| 1262 | abort(); |
| 1263 | } |
| 1264 | |
| 1265 | if (msg == out) { |
| 1266 | printf("FAIL: The message should have changed\n"); |
| 1267 | abort(); |
| 1268 | } |
| 1269 | |
| 1270 | verify_msg(out, cc_setup_national, ARRAY_SIZE(cc_setup_national)); |
| 1271 | msgb_free(out); |
| 1272 | |
Holger Hans Peter Freyther | 73ec698 | 2014-08-29 09:56:58 +0200 | [diff] [blame] | 1273 | bsc_nat_free(nat); |
Holger Hans Peter Freyther | 67e423c | 2013-06-25 15:38:31 +0200 | [diff] [blame] | 1274 | } |
| 1275 | |
Holger Hans Peter Freyther | 8e60f62 | 2012-01-18 20:00:28 +0100 | [diff] [blame] | 1276 | static void test_sms_smsc_rewrite() |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1277 | { |
| 1278 | struct msgb *msg = msgb_alloc(4096, "SMSC rewrite"), *out; |
| 1279 | struct bsc_nat_parsed *parsed; |
| 1280 | const char *imsi = "515039900406700"; |
| 1281 | |
| 1282 | struct bsc_nat *nat = bsc_nat_alloc(); |
| 1283 | |
| 1284 | /* a fake list */ |
Holger Hans Peter Freyther | 68368dd | 2012-01-10 22:39:07 +0100 | [diff] [blame] | 1285 | struct osmo_config_list smsc_entries, dest_entries, clear_entries; |
| 1286 | struct osmo_config_entry smsc_entry, dest_entry, clear_entry; |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1287 | |
Holger Hans Peter Freyther | acc4031 | 2011-05-27 19:21:24 +0200 | [diff] [blame] | 1288 | INIT_LLIST_HEAD(&smsc_entries.entry); |
| 1289 | INIT_LLIST_HEAD(&dest_entries.entry); |
Holger Hans Peter Freyther | 68368dd | 2012-01-10 22:39:07 +0100 | [diff] [blame] | 1290 | INIT_LLIST_HEAD(&clear_entries.entry); |
Holger Hans Peter Freyther | acc4031 | 2011-05-27 19:21:24 +0200 | [diff] [blame] | 1291 | smsc_entry.mcc = "^515039"; |
| 1292 | smsc_entry.option = "639180000105()"; |
| 1293 | smsc_entry.text = "6666666666667"; |
| 1294 | llist_add_tail(&smsc_entry.list, &smsc_entries.entry); |
| 1295 | dest_entry.mcc = "515"; |
| 1296 | dest_entry.mnc = "03"; |
| 1297 | dest_entry.option = "^0049"; |
| 1298 | dest_entry.text = ""; |
| 1299 | llist_add_tail(&dest_entry.list, &dest_entries.entry); |
Holger Hans Peter Freyther | 68368dd | 2012-01-10 22:39:07 +0100 | [diff] [blame] | 1300 | clear_entry.mcc = "^515039"; |
| 1301 | clear_entry.option = "^0049"; |
| 1302 | clear_entry.text = ""; |
| 1303 | llist_add_tail(&clear_entry.list, &clear_entries.entry); |
Holger Hans Peter Freyther | acc4031 | 2011-05-27 19:21:24 +0200 | [diff] [blame] | 1304 | |
| 1305 | bsc_nat_num_rewr_entry_adapt(nat, &nat->smsc_rewr, &smsc_entries); |
| 1306 | bsc_nat_num_rewr_entry_adapt(nat, &nat->tpdest_match, &dest_entries); |
Holger Hans Peter Freyther | 68368dd | 2012-01-10 22:39:07 +0100 | [diff] [blame] | 1307 | bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_clear_tp_srr, &clear_entries); |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1308 | |
Holger Hans Peter Freyther | 68368dd | 2012-01-10 22:39:07 +0100 | [diff] [blame] | 1309 | printf("Testing SMSC rewriting.\n"); |
| 1310 | |
| 1311 | /* |
| 1312 | * Check if the SMSC address is changed |
| 1313 | */ |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1314 | copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite)); |
| 1315 | parsed = bsc_nat_parse(msg); |
| 1316 | if (!parsed) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 1317 | printf("FAIL: Could not parse SMS\n"); |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1318 | abort(); |
| 1319 | } |
| 1320 | |
| 1321 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
| 1322 | if (out == msg) { |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 1323 | printf("FAIL: This should have changed.\n"); |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1324 | abort(); |
| 1325 | } |
| 1326 | |
Holger Hans Peter Freyther | 50be1a9 | 2012-01-10 22:31:39 +0100 | [diff] [blame] | 1327 | verify_msg(out, smsc_rewrite_patched, ARRAY_SIZE(smsc_rewrite_patched)); |
| 1328 | msgb_free(out); |
Holger Hans Peter Freyther | 68368dd | 2012-01-10 22:39:07 +0100 | [diff] [blame] | 1329 | |
| 1330 | /* clear out the filter for SMSC */ |
| 1331 | printf("Attempting to only rewrite the HDR\n"); |
| 1332 | bsc_nat_num_rewr_entry_adapt(nat, &nat->smsc_rewr, NULL); |
| 1333 | msg = msgb_alloc(4096, "SMSC rewrite"); |
| 1334 | copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite)); |
| 1335 | parsed = bsc_nat_parse(msg); |
| 1336 | if (!parsed) { |
| 1337 | printf("FAIL: Could not parse SMS\n"); |
| 1338 | abort(); |
| 1339 | } |
| 1340 | |
| 1341 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
| 1342 | if (out == msg) { |
| 1343 | printf("FAIL: This should have changed.\n"); |
| 1344 | abort(); |
| 1345 | } |
| 1346 | |
| 1347 | verify_msg(out, smsc_rewrite_patched_hdr, ARRAY_SIZE(smsc_rewrite_patched_hdr)); |
| 1348 | msgb_free(out); |
| 1349 | |
| 1350 | /* clear out the next filter */ |
| 1351 | printf("Attempting to change nothing.\n"); |
| 1352 | bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_clear_tp_srr, NULL); |
| 1353 | msg = msgb_alloc(4096, "SMSC rewrite"); |
| 1354 | copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite)); |
| 1355 | parsed = bsc_nat_parse(msg); |
| 1356 | if (!parsed) { |
| 1357 | printf("FAIL: Could not parse SMS\n"); |
| 1358 | abort(); |
| 1359 | } |
| 1360 | |
| 1361 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
| 1362 | if (out != msg) { |
| 1363 | printf("FAIL: This should not have changed.\n"); |
| 1364 | abort(); |
| 1365 | } |
| 1366 | |
| 1367 | verify_msg(out, smsc_rewrite, ARRAY_SIZE(smsc_rewrite)); |
| 1368 | msgb_free(out); |
Holger Hans Peter Freyther | 73ec698 | 2014-08-29 09:56:58 +0200 | [diff] [blame] | 1369 | bsc_nat_free(nat); |
Holger Hans Peter Freyther | 9c20571 | 2011-05-27 17:14:15 +0200 | [diff] [blame] | 1370 | } |
| 1371 | |
Holger Hans Peter Freyther | 8e60f62 | 2012-01-18 20:00:28 +0100 | [diff] [blame] | 1372 | static void test_sms_number_rewrite(void) |
| 1373 | { |
Holger Hans Peter Freyther | 09db1a4 | 2012-03-26 16:21:42 +0200 | [diff] [blame] | 1374 | struct msgb *msg, *out; |
Holger Hans Peter Freyther | 8e60f62 | 2012-01-18 20:00:28 +0100 | [diff] [blame] | 1375 | struct bsc_nat_parsed *parsed; |
| 1376 | const char *imsi = "515039900406700"; |
| 1377 | |
| 1378 | struct bsc_nat *nat = bsc_nat_alloc(); |
| 1379 | |
| 1380 | /* a fake list */ |
Holger Hans Peter Freyther | 09db1a4 | 2012-03-26 16:21:42 +0200 | [diff] [blame] | 1381 | struct osmo_config_list num_entries, clear_entries; |
| 1382 | struct osmo_config_entry num_entry, clear_entry; |
Holger Hans Peter Freyther | 8e60f62 | 2012-01-18 20:00:28 +0100 | [diff] [blame] | 1383 | |
| 1384 | INIT_LLIST_HEAD(&num_entries.entry); |
| 1385 | num_entry.mcc = "^515039"; |
| 1386 | num_entry.option = "^0049()"; |
| 1387 | num_entry.text = "0032"; |
| 1388 | llist_add_tail(&num_entry.list, &num_entries.entry); |
| 1389 | |
| 1390 | bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_num_rewr, &num_entries); |
| 1391 | |
| 1392 | printf("Testing SMS TP-DA rewriting.\n"); |
| 1393 | |
| 1394 | /* |
| 1395 | * Check if the SMSC address is changed |
| 1396 | */ |
Holger Hans Peter Freyther | 09db1a4 | 2012-03-26 16:21:42 +0200 | [diff] [blame] | 1397 | msg = msgb_alloc(4096, "SMSC rewrite"); |
Holger Hans Peter Freyther | 8e60f62 | 2012-01-18 20:00:28 +0100 | [diff] [blame] | 1398 | copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite)); |
| 1399 | parsed = bsc_nat_parse(msg); |
| 1400 | if (!parsed) { |
| 1401 | printf("FAIL: Could not parse SMS\n"); |
| 1402 | abort(); |
| 1403 | } |
| 1404 | |
| 1405 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
| 1406 | if (out == msg) { |
| 1407 | printf("FAIL: This should have changed.\n"); |
| 1408 | abort(); |
| 1409 | } |
| 1410 | |
| 1411 | verify_msg(out, smsc_rewrite_num_patched, |
| 1412 | ARRAY_SIZE(smsc_rewrite_num_patched)); |
| 1413 | msgb_free(out); |
Holger Hans Peter Freyther | 09db1a4 | 2012-03-26 16:21:42 +0200 | [diff] [blame] | 1414 | |
| 1415 | /* |
| 1416 | * Now with TP-SRR rewriting enabled |
| 1417 | */ |
| 1418 | INIT_LLIST_HEAD(&clear_entries.entry); |
| 1419 | clear_entry.mcc = "^515039"; |
| 1420 | clear_entry.option = ""; |
| 1421 | clear_entry.text = ""; |
| 1422 | llist_add_tail(&clear_entry.list, &clear_entries.entry); |
| 1423 | bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_clear_tp_srr, &clear_entries); |
| 1424 | |
| 1425 | msg = msgb_alloc(4096, "SMSC rewrite"); |
| 1426 | copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite)); |
| 1427 | parsed = bsc_nat_parse(msg); |
| 1428 | if (!parsed) { |
| 1429 | printf("FAIL: Could not parse SMS\n"); |
| 1430 | abort(); |
| 1431 | } |
| 1432 | |
| 1433 | out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi); |
| 1434 | if (out == msg) { |
| 1435 | printf("FAIL: This should have changed.\n"); |
| 1436 | abort(); |
| 1437 | } |
| 1438 | |
| 1439 | verify_msg(out, smsc_rewrite_num_patched_tp_srr, |
| 1440 | ARRAY_SIZE(smsc_rewrite_num_patched_tp_srr)); |
| 1441 | msgb_free(out); |
Holger Hans Peter Freyther | 73ec698 | 2014-08-29 09:56:58 +0200 | [diff] [blame] | 1442 | bsc_nat_free(nat); |
Holger Hans Peter Freyther | 8e60f62 | 2012-01-18 20:00:28 +0100 | [diff] [blame] | 1443 | } |
| 1444 | |
Holger Hans Peter Freyther | 1f8276e | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 1445 | static void test_barr_list_parsing(void) |
| 1446 | { |
| 1447 | int rc; |
| 1448 | int cm, lu; |
| 1449 | struct rb_node *node; |
| 1450 | struct rb_root root = RB_ROOT; |
| 1451 | struct osmo_config_list *lst = osmo_config_list_parse(NULL, "barr.cfg"); |
| 1452 | if (lst == NULL) |
| 1453 | abort(); |
| 1454 | |
| 1455 | rc = bsc_nat_barr_adapt(NULL, &root, lst); |
| 1456 | if (rc != 0) |
| 1457 | abort(); |
| 1458 | talloc_free(lst); |
| 1459 | |
| 1460 | |
| 1461 | for (node = rb_first(&root); node; node = rb_next(node)) { |
| 1462 | struct bsc_nat_barr_entry *entry; |
| 1463 | entry = rb_entry(node, struct bsc_nat_barr_entry, node); |
| 1464 | printf("IMSI: %s CM: %d LU: %d\n", entry->imsi, |
| 1465 | entry->cm_reject_cause, entry->lu_reject_cause); |
| 1466 | } |
| 1467 | |
| 1468 | /* do the look up now.. */ |
| 1469 | rc = bsc_nat_barr_find(&root, "12123119", &cm, &lu); |
| 1470 | if (!rc) { |
| 1471 | printf("Failed to find the IMSI.\n"); |
| 1472 | abort(); |
| 1473 | } |
| 1474 | |
| 1475 | if (cm != 3 || lu != 4) { |
| 1476 | printf("Found CM(%d) and LU(%d)\n", cm, lu); |
| 1477 | abort(); |
| 1478 | } |
| 1479 | |
| 1480 | /* empty and check that it is empty */ |
| 1481 | bsc_nat_barr_adapt(NULL, &root, NULL); |
| 1482 | if (!RB_EMPTY_ROOT(&root)) { |
| 1483 | printf("Failed to empty the list.\n"); |
| 1484 | abort(); |
| 1485 | } |
| 1486 | |
| 1487 | /* check that dup results in an error */ |
| 1488 | lst = osmo_config_list_parse(NULL, "barr_dup.cfg"); |
| 1489 | if (lst == NULL) { |
| 1490 | printf("Failed to parse list with dups\n"); |
| 1491 | abort(); |
| 1492 | } |
| 1493 | |
| 1494 | rc = bsc_nat_barr_adapt(NULL, &root, lst); |
| 1495 | if (rc != -1) { |
| 1496 | printf("It should have failed due dup\n"); |
| 1497 | abort(); |
| 1498 | } |
| 1499 | talloc_free(lst); |
| 1500 | |
| 1501 | /* dump for reference */ |
| 1502 | for (node = rb_first(&root); node; node = rb_next(node)) { |
| 1503 | struct bsc_nat_barr_entry *entry; |
| 1504 | entry = rb_entry(node, struct bsc_nat_barr_entry, node); |
| 1505 | printf("IMSI: %s CM: %d LU: %d\n", entry->imsi, |
| 1506 | entry->cm_reject_cause, entry->lu_reject_cause); |
| 1507 | |
| 1508 | } |
Holger Hans Peter Freyther | 73ec698 | 2014-08-29 09:56:58 +0200 | [diff] [blame] | 1509 | rc = bsc_nat_barr_adapt(NULL, &root, NULL); |
Holger Hans Peter Freyther | 1f8276e | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 1510 | } |
| 1511 | |
Holger Hans Peter Freyther | b2b291d | 2013-04-16 13:23:43 +0200 | [diff] [blame] | 1512 | static void test_nat_extract_lac() |
| 1513 | { |
| 1514 | int res; |
| 1515 | struct bsc_connection *bsc; |
| 1516 | struct bsc_nat *nat; |
| 1517 | struct nat_sccp_connection con; |
| 1518 | struct bsc_nat_parsed *parsed; |
| 1519 | struct msgb *msg = msgb_alloc(4096, "test-message"); |
| 1520 | |
| 1521 | printf("Testing LAC extraction from SCCP CR\n"); |
| 1522 | |
| 1523 | /* initialize the testcase */ |
| 1524 | nat = bsc_nat_alloc(); |
| 1525 | bsc = bsc_connection_alloc(nat); |
| 1526 | bsc->cfg = bsc_config_alloc(nat, "foo"); |
| 1527 | |
| 1528 | memset(&con, 0, sizeof(con)); |
| 1529 | con.bsc = bsc; |
| 1530 | |
| 1531 | /* create the SCCP CR */ |
| 1532 | msg->l2h = msgb_put(msg, ARRAY_SIZE(bssmap_cr)); |
| 1533 | memcpy(msg->l2h, bssmap_cr, ARRAY_SIZE(bssmap_cr)); |
| 1534 | |
| 1535 | /* parse it and pass it on */ |
| 1536 | parsed = bsc_nat_parse(msg); |
| 1537 | res = bsc_nat_extract_lac(bsc, &con, parsed, msg); |
| 1538 | OSMO_ASSERT(res == 0); |
| 1539 | |
| 1540 | /* verify the LAC */ |
| 1541 | OSMO_ASSERT(con.lac == 8210); |
| 1542 | OSMO_ASSERT(con.ci == 50000); |
Holger Hans Peter Freyther | 73ec698 | 2014-08-29 09:56:58 +0200 | [diff] [blame] | 1543 | |
| 1544 | bsc_nat_free(nat); |
Holger Hans Peter Freyther | b2b291d | 2013-04-16 13:23:43 +0200 | [diff] [blame] | 1545 | } |
| 1546 | |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 1547 | int main(int argc, char **argv) |
| 1548 | { |
Holger Hans Peter Freyther | 30e1ae9 | 2010-07-30 02:53:14 +0800 | [diff] [blame] | 1549 | sccp_set_log_area(DSCCP); |
Holger Hans Peter Freyther | 67cd75f | 2011-05-12 16:02:07 +0200 | [diff] [blame] | 1550 | osmo_init_logging(&log_info); |
Holger Hans Peter Freyther | 38f7c75 | 2010-06-15 18:48:36 +0800 | [diff] [blame] | 1551 | |
| 1552 | test_filter(); |
Holger Hans Peter Freyther | 04fd992 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 1553 | test_contrack(); |
Holger Hans Peter Freyther | 04fd992 | 2010-03-30 06:51:53 +0200 | [diff] [blame] | 1554 | test_paging(); |
Holger Hans Peter Freyther | fc9bd23 | 2010-04-01 03:55:27 +0200 | [diff] [blame] | 1555 | test_mgcp_ass_tracking(); |
| 1556 | test_mgcp_find(); |
Holger Hans Peter Freyther | 76c8354 | 2010-04-01 06:48:52 +0200 | [diff] [blame] | 1557 | test_mgcp_rewrite(); |
Holger Hans Peter Freyther | 3c3bce1 | 2010-04-01 10:16:28 +0200 | [diff] [blame] | 1558 | test_mgcp_parse(); |
Holger Hans Peter Freyther | 0c08db1 | 2010-05-14 08:12:57 +0800 | [diff] [blame] | 1559 | test_cr_filter(); |
Holger Hans Peter Freyther | 3268540 | 2010-09-15 05:20:40 +0800 | [diff] [blame] | 1560 | test_dt_filter(); |
Holger Hans Peter Freyther | 73bbf89 | 2010-10-21 14:46:57 +0200 | [diff] [blame] | 1561 | test_setup_rewrite(); |
Holger Hans Peter Freyther | ddf191e | 2013-06-25 11:44:01 +0200 | [diff] [blame] | 1562 | test_setup_rewrite_prefix(); |
Holger Hans Peter Freyther | 67e423c | 2013-06-25 15:38:31 +0200 | [diff] [blame] | 1563 | test_setup_rewrite_post(); |
Holger Hans Peter Freyther | 8e60f62 | 2012-01-18 20:00:28 +0100 | [diff] [blame] | 1564 | test_sms_smsc_rewrite(); |
| 1565 | test_sms_number_rewrite(); |
Holger Hans Peter Freyther | a9e9331 | 2011-02-26 11:38:00 +0100 | [diff] [blame] | 1566 | test_mgcp_allocations(); |
Holger Hans Peter Freyther | 1f8276e | 2013-01-01 11:25:09 +0100 | [diff] [blame] | 1567 | test_barr_list_parsing(); |
Holger Hans Peter Freyther | b2b291d | 2013-04-16 13:23:43 +0200 | [diff] [blame] | 1568 | test_nat_extract_lac(); |
Holger Hans Peter Freyther | f5ede52 | 2012-01-06 13:56:12 +0100 | [diff] [blame] | 1569 | |
| 1570 | printf("Testing execution completed.\n"); |
Holger Hans Peter Freyther | 0b8f69d | 2010-06-15 18:45:38 +0800 | [diff] [blame] | 1571 | return 0; |
| 1572 | } |
Holger Hans Peter Freyther | c327187 | 2012-11-05 14:54:56 +0100 | [diff] [blame] | 1573 | |
| 1574 | /* stub */ |
| 1575 | void bsc_nat_send_mgcp_to_msc(struct bsc_nat *nat, struct msgb *msg) |
| 1576 | { |
| 1577 | abort(); |
| 1578 | } |