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