blob: a405763bc061a862653a1f56cc2aa5463e61cd39 [file] [log] [blame]
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001/*
2 * BSC NAT Message filtering
3 *
Holger Hans Peter Freytherdbd94492013-04-02 12:34:11 +02004 * (C) 2010-2013 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2010-2013 by On-Waves
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * 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 Freyther0b8f69d2010-06-15 18:45:38 +080012 * (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 Welte9af6ddf2011-01-01 15:25:50 +010019 * 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 Freyther0b8f69d2010-06-15 18:45:38 +080021 *
22 */
23
24
25#include <openbsc/debug.h>
26#include <openbsc/gsm_data.h>
27#include <openbsc/bsc_nat.h>
Holger Hans Peter Freytherc2b31ed2010-07-31 05:17:17 +080028#include <openbsc/bsc_nat_sccp.h>
Holger Hans Peter Freyther973dbae2015-04-04 20:47:03 +020029#include <openbsc/bsc_msg_filter.h>
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +020030#include <openbsc/nat_rewrite_trie.h>
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080031
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020032#include <osmocom/core/application.h>
Holger Hans Peter Freytherb2b291d2013-04-16 13:23:43 +020033#include <osmocom/core/backtrace.h>
Pablo Neira Ayuso928cb332011-03-26 22:08:53 +010034#include <osmocom/core/talloc.h>
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +080035
Harald Welted5db12c2010-08-03 15:11:51 +020036#include <osmocom/sccp/sccp.h>
Pablo Neira Ayuso928cb332011-03-26 22:08:53 +010037#include <osmocom/gsm/protocol/gsm_08_08.h>
Holger Hans Peter Freyther30e1ae92010-07-30 02:53:14 +080038
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080039#include <stdio.h>
40
41/* test messages for ipa */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080042static uint8_t ipa_id[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080043 0x00, 0x01, 0xfe, 0x06,
44};
45
46/* SCCP messages are below */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080047static uint8_t gsm_reset[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080048 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 Freytherdbd16fe2010-07-23 19:08:55 +080054static const uint8_t gsm_reset_ack[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080055 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 Freytherdbd16fe2010-07-23 19:08:55 +080061static const uint8_t gsm_paging[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080062 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 Freytherdbd16fe2010-07-23 19:08:55 +080070static const uint8_t bssmap_cr[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080071 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 Freytherdbd16fe2010-07-23 19:08:55 +080081static const uint8_t bssmap_cc[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080082 0x00, 0x0a, 0xfd,
83 0x02, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00,
84};
85
86/* MSC -> BSC released */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080087static const uint8_t bssmap_released[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080088 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 Freytherdbd16fe2010-07-23 19:08:55 +080094static const uint8_t bssmap_release_complete[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080095 0x00, 0x07, 0xfd,
96 0x05, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03
97};
98
Holger Hans Peter Freyther0a6f62f2010-04-06 10:22:01 +020099/* both directions IT timer */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800100static const uint8_t connnection_it[] = {
Holger Hans Peter Freyther0a6f62f2010-04-06 10:22:01 +0200101 0x00, 0x0b, 0xfd,
102 0x10, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03,
103 0x00, 0x00, 0x00, 0x00,
104};
105
Holger Hans Peter Freyther5e63f922010-04-21 15:45:26 +0800106/* error in both directions */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800107static const uint8_t proto_error[] = {
Holger Hans Peter Freyther5e63f922010-04-21 15:45:26 +0800108 0x00, 0x05, 0xfd,
109 0x0f, 0x22, 0x33, 0x44, 0x00,
110};
111
Holger Hans Peter Freythera128d912010-04-01 08:47:12 +0200112/* MGCP wrap... */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800113static const uint8_t mgcp_msg[] = {
Holger Hans Peter Freythera128d912010-04-01 08:47:12 +0200114 0x00, 0x03, 0xfc,
115 0x20, 0x20, 0x20,
116};
117
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800118/* location updating request */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800119static const uint8_t bss_lu[] = {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800120 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 Freytherf1924982010-05-15 23:54:04 +0800129/* paging response */
130static 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 Freyther0b8f69d2010-06-15 18:45:38 +0800140struct filter_result {
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800141 const uint8_t *data;
Holger Hans Peter Freythere2c15202010-07-23 19:09:21 +0800142 const uint16_t length;
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100143 const int dir;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800144 const int result;
145};
146
147static const struct filter_result results[] = {
148 {
149 .data = ipa_id,
150 .length = ARRAY_SIZE(ipa_id),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100151 .dir = DIR_MSC,
152 .result = 1,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800153 },
154 {
155 .data = gsm_reset,
156 .length = ARRAY_SIZE(gsm_reset),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100157 .dir = DIR_MSC,
158 .result = 1,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800159 },
160 {
161 .data = gsm_reset_ack,
162 .length = ARRAY_SIZE(gsm_reset_ack),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100163 .dir = DIR_BSC,
164 .result = 1,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800165 },
166 {
167 .data = gsm_paging,
168 .length = ARRAY_SIZE(gsm_paging),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100169 .dir = DIR_BSC,
170 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800171 },
172 {
173 .data = bssmap_cr,
174 .length = ARRAY_SIZE(bssmap_cr),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100175 .dir = DIR_MSC,
176 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800177 },
178 {
179 .data = bssmap_cc,
180 .length = ARRAY_SIZE(bssmap_cc),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100181 .dir = DIR_BSC,
182 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800183 },
184 {
185 .data = bssmap_released,
186 .length = ARRAY_SIZE(bssmap_released),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100187 .dir = DIR_MSC,
188 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800189 },
190 {
191 .data = bssmap_release_complete,
192 .length = ARRAY_SIZE(bssmap_release_complete),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100193 .dir = DIR_BSC,
194 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800195 },
Holger Hans Peter Freythera128d912010-04-01 08:47:12 +0200196 {
197 .data = mgcp_msg,
198 .length = ARRAY_SIZE(mgcp_msg),
199 .dir = DIR_MSC,
200 .result = 0,
201 },
Holger Hans Peter Freyther0a6f62f2010-04-06 10:22:01 +0200202 {
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 Freyther5e63f922010-04-21 15:45:26 +0800214 {
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 Freyther0b8f69d2010-06-15 18:45:38 +0800227};
228
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800229static void test_filter(void)
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800230{
231 int i;
232
233
234 /* start testinh with proper messages */
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100235 printf("Testing BSS Filtering.\n");
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800236 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 Freytherf5ede522012-01-06 13:56:12 +0100241 printf("Going to test item: %d\n", i);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800242 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 Freytherf5ede522012-01-06 13:56:12 +0100247 printf("FAIL: Failed to parse the message\n");
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800248 continue;
249 }
250
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100251 result = bsc_nat_filter_ipa(results[i].dir, msg, parsed);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800252 if (result != results[i].result) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100253 printf("FAIL: Not the expected result got: %d wanted: %d\n",
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800254 result, results[i].result);
255 }
256
257 msgb_free(msg);
258 }
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800259}
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800260
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800261#include "bsc_data.c"
262
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800263static void copy_to_msg(struct msgb *msg, const uint8_t *data, unsigned int length)
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800264{
265 msgb_reset(msg);
266 msg->l2h = msgb_put(msg, length);
267 memcpy(msg->l2h, data, msgb_l2len(msg));
268}
269
Holger Hans Peter Freyther50be1a92012-01-10 22:31:39 +0100270static void verify_msg(struct msgb *out, const uint8_t *ref, int ref_len)
271{
272 if (out->len != ref_len) {
Holger Hans Peter Freytherbe530122012-01-18 17:20:23 +0100273 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 Freyther50be1a92012-01-10 22:31:39 +0100278 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 Freyther38f7c752010-06-15 18:48:36 +0800291#define VERIFY(con_found, con, msg, ver, str) \
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100292 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 Freyther38f7c752010-06-15 18:48:36 +0800301 if (memcmp(msg->data, ver, sizeof(ver)) != 0) { \
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100302 printf("Failed to patch the %s msg.\n", str); \
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800303 abort(); \
304 }
305
306/* test conn tracking once */
307static void test_contrack()
308{
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800309 struct bsc_nat *nat;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800310 struct bsc_connection *con;
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200311 struct nat_sccp_connection *con_found;
312 struct nat_sccp_connection *rc_con;
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800313 struct bsc_nat_parsed *parsed;
314 struct msgb *msg;
315
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100316 printf("Testing connection tracking.\n");
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800317 nat = bsc_nat_alloc();
318 con = bsc_connection_alloc(nat);
Max714b1702016-04-07 12:27:11 +0200319 con->cfg = bsc_config_alloc(nat, "foo", 0);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800320 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 Freyther38f7c752010-06-15 18:48:36 +0800325 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 Freytherb5513ca2010-04-21 18:56:12 +0800330 con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800331 if (con_found != NULL) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100332 printf("Con should not exist realref(%u)\n",
333 sccp_src_ref_to_int(&con_found->real_ref));
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800334 abort();
335 }
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +0800336 rc_con = create_sccp_src_ref(con, parsed);
337 if (!rc_con) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100338 printf("Failed to create a ref\n");
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800339 abort();
340 }
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800341 con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100342 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 Freyther38f7c752010-06-15 18:48:36 +0800349 abort();
350 }
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +0800351 if (con_found != rc_con) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100352 printf("Failed to find the right connection.\n");
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +0800353 abort();
354 }
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800355 if (memcmp(msg->data, bsc_cr_patched, sizeof(bsc_cr_patched)) != 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100356 printf("Failed to patch the BSC CR msg.\n");
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800357 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 Freyther49c7fb52010-06-15 18:48:55 +0800364 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 Freytherf5ede522012-01-06 13:56:12 +0100367 printf("Failed to update the SCCP con.\n");
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800368 abort();
369 }
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800370
371 /* 3.) send some data */
372 copy_to_msg(msg, bsc_dtap, sizeof(bsc_dtap));
373 parsed = bsc_nat_parse(msg);
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800374 con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800375 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 Freytherb5513ca2010-04-21 18:56:12 +0800392 con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100393 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 Freyther38f7c752010-06-15 18:48:36 +0800400 abort();
401 }
402 if (memcmp(msg->data, bsc_rlc_patched, sizeof(bsc_rlc_patched)) != 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100403 printf("Failed to patch the BSC CR msg.\n");
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800404 abort();
405 }
406 remove_sccp_src_ref(con, msg, parsed);
Holger Hans Peter Freyther9d518552010-04-05 21:44:51 +0200407 talloc_free(parsed);
408
409 copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc));
410 parsed = bsc_nat_parse(msg);
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800411 con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800412
413 /* verify that it is gone */
414 if (con_found != NULL) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100415 printf("Con should not exist real_ref(%u)\n",
416 sccp_src_ref_to_int(&con_found->real_ref));
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800417 abort();
418 }
419 talloc_free(parsed);
420
421
Holger Hans Peter Freyther9212d9d2011-02-27 11:18:41 +0100422 bsc_config_free(con->cfg);
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +0200423 bsc_nat_free(nat);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800424 msgb_free(msg);
425}
426
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200427static void test_paging(void)
428{
429 struct bsc_nat *nat;
430 struct bsc_connection *con;
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800431 struct bsc_config *cfg;
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200432
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100433 printf("Testing paging by lac.\n");
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200434
435 nat = bsc_nat_alloc();
436 con = bsc_connection_alloc(nat);
Max714b1702016-04-07 12:27:11 +0200437 cfg = bsc_config_alloc(nat, "unknown", 0);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800438 con->cfg = cfg;
439 bsc_config_add_lac(cfg, 23);
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200440 con->authenticated = 1;
441 llist_add(&con->list_entry, &nat->bsc_connections);
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200442
443 /* Test it by not finding it */
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200444 if (bsc_config_handles_lac(cfg, 8213) != 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100445 printf("Should not be handled.\n");
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200446 abort();
447 }
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200448
449 /* Test by finding it */
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800450 bsc_config_del_lac(cfg, 23);
451 bsc_config_add_lac(cfg, 8213);
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200452 if (bsc_config_handles_lac(cfg, 8213) == 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100453 printf("Should have found it.\n");
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200454 abort();
455 }
Holger Hans Peter Freyther3615a302013-06-25 12:03:36 +0200456
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +0200457 bsc_nat_free(nat);
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200458}
459
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100460static void test_mgcp_allocations(void)
461{
462#if 0
463 struct bsc_connection *bsc;
464 struct bsc_nat *nat;
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200465 struct nat_sccp_connection con;
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100466 int i, j, multiplex;
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100467
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100468 printf("Testing MGCP.\n");
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100469 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 Freyther88ad7722011-02-28 00:56:17 +0100476 nat->mgcp_cfg->trunk.number_endpoints = 64;
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100477
478 bsc = bsc_connection_alloc(nat);
Max714b1702016-04-07 12:27:11 +0200479 bsc->cfg = bsc_config_alloc(nat, "foo", 0);
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100480 bsc->cfg->max_endpoints = 60;
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100481 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 Freytherf5ede522012-01-06 13:56:12 +0100490 printf("failed to allocate... on iteration %d\n", i);
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100491 break;
492 }
493 ++i;
494 } while(1);
495
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100496 multiplex = bsc_mgcp_nr_multiplexes(bsc->cfg->max_endpoints);
497 for (i = 0; i < multiplex; ++i) {
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100498 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 Freytherfc9bd232010-04-01 03:55:27 +0200505static void test_mgcp_ass_tracking(void)
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800506{
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800507 struct bsc_connection *bsc;
508 struct bsc_nat *nat;
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200509 struct nat_sccp_connection con;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800510 struct bsc_nat_parsed *parsed;
511 struct msgb *msg;
512
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100513 printf("Testing MGCP.\n");
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800514 memset(&con, 0, sizeof(con));
515
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800516 nat = bsc_nat_alloc();
517 nat->bsc_endpoints = talloc_zero_array(nat,
518 struct bsc_endpoint,
519 33);
Holger Hans Peter Freyther7e0cc502011-02-25 12:43:58 +0100520 nat->mgcp_cfg = mgcp_config_alloc();
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100521 nat->mgcp_cfg->trunk.number_endpoints = 64;
Holger Hans Peter Freyther462b7d72012-10-24 21:53:40 +0200522 mgcp_endpoints_allocate(&nat->mgcp_cfg->trunk);
Holger Hans Peter Freyther7e0cc502011-02-25 12:43:58 +0100523
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800524 bsc = bsc_connection_alloc(nat);
Max714b1702016-04-07 12:27:11 +0200525 bsc->cfg = bsc_config_alloc(nat, "foo", 0);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800526 bsc_config_add_lac(bsc->cfg, 2323);
Holger Hans Peter Freyther86c1db62011-02-25 17:10:25 +0100527 bsc->last_endpoint = 0x1e;
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800528 con.bsc = bsc;
529
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800530 msg = msgb_alloc(4096, "foo");
531 copy_to_msg(msg, ass_cmd, sizeof(ass_cmd));
532 parsed = bsc_nat_parse(msg);
Holger Hans Peter Freythercd702372010-09-20 01:21:51 +0800533
534 if (msg->l2h[16] != 0 ||
535 msg->l2h[17] != 0x1) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100536 printf("Input is not as expected.. %s 0x%x\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200537 osmo_hexdump(msg->l2h, msgb_l2len(msg)),
Holger Hans Peter Freythercd702372010-09-20 01:21:51 +0800538 msg->l2h[17]);
539 abort();
540 }
541
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800542 if (bsc_mgcp_assign_patch(&con, msg) != 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100543 printf("Failed to handle assignment.\n");
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800544 abort();
545 }
546
Holger Hans Peter Freythercd702372010-09-20 01:21:51 +0800547 if (con.msc_endp != 1) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100548 printf("Timeslot should be 1.\n");
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800549 abort();
550 }
551
Holger Hans Peter Freyther86c1db62011-02-25 17:10:25 +0100552 if (con.bsc_endp != 0x1) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100553 printf("Assigned timeslot should have been 1.\n");
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800554 abort();
555 }
Holger Hans Peter Freyther86c1db62011-02-25 17:10:25 +0100556 if (con.bsc->_endpoint_status[0x1] != 1) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100557 printf("The status on the BSC is wrong.\n");
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800558 abort();
559 }
560
Holger Hans Peter Freythercd702372010-09-20 01:21:51 +0800561 int multiplex, timeslot;
Holger Hans Peter Freyther86c1db62011-02-25 17:10:25 +0100562 mgcp_endpoint_to_timeslot(0x1, &multiplex, &timeslot);
Holger Hans Peter Freythercd702372010-09-20 01:21:51 +0800563
564 uint16_t cic = htons(timeslot & 0x1f);
565 if (memcmp(&cic, &msg->l2h[16], sizeof(cic)) != 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100566 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 Freythercd702372010-09-20 01:21:51 +0800568 abort();
569 }
570
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800571 talloc_free(parsed);
572
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800573 bsc_mgcp_dlcx(&con);
Holger Hans Peter Freytherd2df4ca2010-09-19 20:54:21 +0800574 if (con.bsc_endp != -1 || con.msc_endp != -1 ||
Holger Hans Peter Freyther86c1db62011-02-25 17:10:25 +0100575 con.bsc->_endpoint_status[1] != 0 || con.bsc->last_endpoint != 0x1) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100576 printf("Clearing should remove the mapping.\n");
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800577 abort();
578 }
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800579
Holger Hans Peter Freyther9212d9d2011-02-27 11:18:41 +0100580 bsc_config_free(bsc->cfg);
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +0200581 bsc_nat_free(nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800582}
583
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200584/* test the code to find a given connection */
585static void test_mgcp_find(void)
586{
587 struct bsc_nat *nat;
588 struct bsc_connection *con;
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200589 struct nat_sccp_connection *sccp_con;
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200590
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100591 printf("Testing finding of a BSC Connection\n");
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200592
593 nat = bsc_nat_alloc();
594 con = bsc_connection_alloc(nat);
595 llist_add(&con->list_entry, &nat->bsc_connections);
596
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200597 sccp_con = talloc_zero(con, struct nat_sccp_connection);
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800598 sccp_con->msc_endp = 12;
599 sccp_con->bsc_endp = 12;
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200600 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 Freytherf5ede522012-01-06 13:56:12 +0100604 printf("Found the wrong connection.\n");
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200605 abort();
606 }
607
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800608 if (bsc_mgcp_find_con(nat, 12) != sccp_con) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100609 printf("Didn't find the connection\n");
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200610 abort();
611 }
612
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200613 /* free everything */
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +0200614 bsc_nat_free(nat);
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200615}
616
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200617static void test_mgcp_rewrite(void)
618{
619 int i;
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200620 struct msgb *output;
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100621 printf("Testing rewriting MGCP messages.\n");
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200622
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200623 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 Erlbecka52ac662013-11-29 13:43:45 +0100628 const int expected_payload_type = mgcp_messages[i].payload_type;
Holger Hans Peter Freyther7f100c92015-04-23 20:25:17 -0400629 const int ensure_mode_set = mgcp_messages[i].ensure_mode_set;
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100630 int payload_type = -1;
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200631
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200632 char *input = strdup(orig);
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200633
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100634 output = bsc_mgcp_rewrite(input, strlen(input), 0x1e,
Holger Hans Peter Freyther7f100c92015-04-23 20:25:17 -0400635 ip, port, -1, &payload_type, ensure_mode_set);
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100636
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 Freyther76c83542010-04-01 06:48:52 +0200647 if (msgb_l2len(output) != strlen(patc)) {
Holger Hans Peter Freytherdaaea0c2015-08-03 09:28:41 +0200648 printf("Wrong sizes for test: %d %u != %zu != %zu\n", i, msgb_l2len(output), strlen(patc), strlen(orig));
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100649 printf("String '%s' vs '%s'\n", (const char *) output->l2h, patc);
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200650 abort();
651 }
652
653 if (memcmp(output->l2h, patc, msgb_l2len(output)) != 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100654 printf("Broken on %d msg: '%s'\n", i, (const char *) output->l2h);
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200655 abort();
656 }
657
658 msgb_free(output);
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200659 free(input);
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200660 }
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200661}
662
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200663static void test_mgcp_parse(void)
664{
665 int code, ci;
666 char transaction[60];
667
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100668 printf("Testing MGCP response parsing.\n");
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200669
670 if (bsc_mgcp_parse_response(crcx_resp, &code, transaction) != 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100671 printf("Failed to parse CRCX resp.\n");
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200672 abort();
673 }
674
675 if (code != 200) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100676 printf("Failed to parse the CODE properly. Got: %d\n", code);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200677 abort();
678 }
679
680 if (strcmp(transaction, "23265295") != 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100681 printf("Failed to parse transaction id: '%s'\n", transaction);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200682 abort();
683 }
684
685 ci = bsc_mgcp_extract_ci(crcx_resp);
686 if (ci != 1) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100687 printf("Failed to parse the CI. Got: %d\n", ci);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200688 abort();
689 }
690}
691
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800692struct cr_filter {
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800693 const uint8_t *data;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800694 int length;
695 int result;
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800696 int contype;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800697
698 const char *bsc_imsi_allow;
699 const char *bsc_imsi_deny;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800700 const char *nat_imsi_deny;
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100701 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 Freyther34a96ae2010-05-14 19:49:35 +0800707};
708
709static struct cr_filter cr_filter[] = {
710 {
711 .data = bssmap_cr,
712 .length = sizeof(bssmap_cr),
Holger Hans Peter Freytherd880f542010-09-15 01:42:07 +0800713 .result = 1,
Holger Hans Peter Freytherc6529132015-04-05 21:03:49 +0200714 .contype = FLT_CON_TYPE_CM_SERV_REQ,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100715 .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 Freyther34a96ae2010-05-14 19:49:35 +0800721 },
722 {
723 .data = bss_lu,
724 .length = sizeof(bss_lu),
Holger Hans Peter Freytherd880f542010-09-15 01:42:07 +0800725 .result = 1,
Holger Hans Peter Freytherc6529132015-04-05 21:03:49 +0200726 .contype = FLT_CON_TYPE_LU,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100727 .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 Freyther34a96ae2010-05-14 19:49:35 +0800733 },
734 {
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800735 .data = pag_resp,
736 .length = sizeof(pag_resp),
Holger Hans Peter Freytherd880f542010-09-15 01:42:07 +0800737 .result = 1,
Holger Hans Peter Freytherc6529132015-04-05 21:03:49 +0200738 .contype = FLT_CON_TYPE_PAG_RESP,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100739 .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 Freytherf1924982010-05-15 23:54:04 +0800745 },
746 {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800747 /* 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 Freytherc6529132015-04-05 21:03:49 +0200752 .contype = FLT_CON_TYPE_LU,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100753 .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 Freyther34a96ae2010-05-14 19:49:35 +0800759 },
760 {
Holger Hans Peter Freythera0aeaa72010-05-14 23:07:58 +0800761 /* BSC allow is before NAT deny */
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800762 .data = bss_lu,
763 .length = sizeof(bss_lu),
Holger Hans Peter Freytherd880f542010-09-15 01:42:07 +0800764 .result = 1,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800765 .nat_imsi_deny = "[0-9]*",
Holger Hans Peter Freythera0aeaa72010-05-14 23:07:58 +0800766 .bsc_imsi_allow = "2440[0-9]*",
Holger Hans Peter Freytherc6529132015-04-05 21:03:49 +0200767 .contype = FLT_CON_TYPE_LU,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100768 .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 Freyther34a96ae2010-05-14 19:49:35 +0800774 },
775 {
776 /* BSC allow is before NAT deny */
777 .data = bss_lu,
778 .length = sizeof(bss_lu),
Holger Hans Peter Freytherd880f542010-09-15 01:42:07 +0800779 .result = 1,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800780 .bsc_imsi_allow = "[0-9]*",
781 .nat_imsi_deny = "[0-9]*",
Holger Hans Peter Freytherc6529132015-04-05 21:03:49 +0200782 .contype = FLT_CON_TYPE_LU,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100783 .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 Freyther34a96ae2010-05-14 19:49:35 +0800789 },
790 {
791 /* filter as deny is first */
792 .data = bss_lu,
793 .length = sizeof(bss_lu),
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200794 .result = 1,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800795 .bsc_imsi_deny = "[0-9]*",
796 .bsc_imsi_allow = "[0-9]*",
797 .nat_imsi_deny = "[0-9]*",
Holger Hans Peter Freytherc6529132015-04-05 21:03:49 +0200798 .contype = FLT_CON_TYPE_LU,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100799 .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 Freyther34a96ae2010-05-14 19:49:35 +0800805 },
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200806 {
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 Freytherc6529132015-04-05 21:03:49 +0200813 .contype = FLT_CON_TYPE_LU,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100814 .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 Freytherc6529132015-04-05 21:03:49 +0200828 .contype = FLT_CON_TYPE_LU,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100829 .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 Freyther1fd60632010-10-19 20:55:33 +0200835 },
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 Freytherc6529132015-04-05 21:03:49 +0200842 .contype = FLT_CON_TYPE_LU,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100843 .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 Freyther1fd60632010-10-19 20:55:33 +0200849 },
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100850 {
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 Freytherc6529132015-04-05 21:03:49 +0200856 .contype = FLT_CON_TYPE_LU,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100857 .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 Freyther34a96ae2010-05-14 19:49:35 +0800864};
865
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800866static void test_cr_filter()
867{
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800868 int i, res, contype;
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800869 struct msgb *msg = msgb_alloc(4096, "test_cr_filter");
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800870 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +0200871 struct bsc_msg_acc_lst *nat_lst, *bsc_lst;
872 struct bsc_msg_acc_lst_entry *nat_entry, *bsc_entry;
Holger Hans Peter Freytherc36a6d52015-04-05 16:55:28 +0200873 struct bsc_filter_reject_cause cause;
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800874
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800875 struct bsc_nat *nat = bsc_nat_alloc();
876 struct bsc_connection *bsc = bsc_connection_alloc(nat);
Max714b1702016-04-07 12:27:11 +0200877 bsc->cfg = bsc_config_alloc(nat, "foo", 0);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800878 bsc_config_add_lac(bsc->cfg, 1234);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800879 bsc->cfg->acc_lst_name = "bsc";
880 nat->acc_lst_name = "nat";
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800881
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +0200882 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 Freytherd77c8172010-06-08 10:53:39 +0800884
Holger Hans Peter Freythera1e6bd62015-04-04 22:40:12 +0200885 bsc_entry = bsc_msg_acc_lst_entry_create(bsc_lst);
886 nat_entry = bsc_msg_acc_lst_entry_create(nat_lst);
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800887
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100888 /* 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 Freyther34a96ae2010-05-14 19:49:35 +0800892 for (i = 0; i < ARRAY_SIZE(cr_filter); ++i) {
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800893 char *imsi;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800894 msgb_reset(msg);
895 copy_to_msg(msg, cr_filter[i].data, cr_filter[i].length);
896
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100897 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 Freyther06c9da62011-06-09 21:48:49 +0200902 if (gsm_parse_reg(nat_entry, &nat_entry->imsi_deny_re, &nat_entry->imsi_deny,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800903 cr_filter[i].nat_imsi_deny ? 1 : 0,
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200904 &cr_filter[i].nat_imsi_deny) != 0)
905 abort();
Holger Hans Peter Freyther06c9da62011-06-09 21:48:49 +0200906 if (gsm_parse_reg(bsc_entry, &bsc_entry->imsi_allow_re, &bsc_entry->imsi_allow,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800907 cr_filter[i].bsc_imsi_allow ? 1 : 0,
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200908 &cr_filter[i].bsc_imsi_allow) != 0)
909 abort();
Holger Hans Peter Freyther06c9da62011-06-09 21:48:49 +0200910 if (gsm_parse_reg(bsc_entry, &bsc_entry->imsi_deny_re, &bsc_entry->imsi_deny,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800911 cr_filter[i].bsc_imsi_deny ? 1 : 0,
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200912 &cr_filter[i].bsc_imsi_deny) != 0)
913 abort();
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800914
915 parsed = bsc_nat_parse(msg);
916 if (!parsed) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100917 printf("FAIL: Failed to parse the message\n");
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800918 abort();
919 }
920
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100921 memset(&cause, 0, sizeof(cause));
922 res = bsc_nat_filter_sccp_cr(bsc, msg, parsed, &contype, &imsi, &cause);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800923 if (res != cr_filter[i].result) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100924 printf("FAIL: Wrong result %d for test %d.\n", res, i);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800925 abort();
926 }
927
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100928
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 Freyther19c0a842010-05-16 02:00:40 +0800932 if (contype != cr_filter[i].contype) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100933 printf("FAIL: Wrong contype %d for test %d.\n", res, contype);
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800934 abort();
935 }
936
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800937 talloc_steal(parsed, imsi);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800938 talloc_free(parsed);
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800939 }
940
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800941 msgb_free(msg);
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +0200942 bsc_nat_free(nat);
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800943}
944
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800945static 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 Freytherc36a6d52015-04-05 16:55:28 +0200950 struct bsc_filter_reject_cause cause;
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800951
952 struct bsc_nat *nat = bsc_nat_alloc();
953 struct bsc_connection *bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200954 struct nat_sccp_connection *con = talloc_zero(0, struct nat_sccp_connection);
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800955
Max714b1702016-04-07 12:27:11 +0200956 bsc->cfg = bsc_config_alloc(nat, "foo", 0);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800957 bsc_config_add_lac(bsc->cfg, 23);
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800958 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 Freytherf5ede522012-01-06 13:56:12 +0100965 printf("FAIL: Could not parse ID resp\n");
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800966 abort();
967 }
968
969 if (parsed->bssap != BSSAP_MSG_DTAP) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100970 printf("FAIL: It should be dtap\n");
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800971 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 Freytherf5ede522012-01-06 13:56:12 +0100976 printf("FAIL: Not enough space for the content\n");
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800977 abort();
978 }
979
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100980 memset(&cause, 0, sizeof(cause));
981 if (bsc_nat_filter_dt(bsc, msg, con, parsed, &cause) != 1) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100982 printf("FAIL: Should have passed..\n");
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800983 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 Freytherc09f8a32015-04-05 19:13:27 +0200995 con->filter_state.imsi_checked = 0;
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100996 memset(&cause, 0, sizeof(cause));
997 bsc_nat_filter_dt(bsc, msg, con, parsed, &cause);
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800998 }
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +0200999
1000 msgb_free(msg);
1001 bsc_nat_free(nat);
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +08001002}
1003
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001004static 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 Ayusoab46cf32011-05-07 13:11:20 +02001014 struct osmo_config_list entries;
1015 struct osmo_config_entry entry;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001016
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 Freyther9c205712011-05-27 17:14:15 +02001023 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001024
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 Freytherf5ede522012-01-06 13:56:12 +01001030 printf("FAIL: Could not parse ID resp\n");
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001031 abort();
1032 }
1033
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001034 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001035 if (msg != out) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001036 printf("FAIL: The message should not have been changed\n");
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001037 abort();
1038 }
1039
Holger Hans Peter Freyther50be1a92012-01-10 22:31:39 +01001040 verify_msg(out, cc_setup_international, ARRAY_SIZE(cc_setup_international));
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001041 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 Freytherf5ede522012-01-06 13:56:12 +01001048 printf("FAIL: Could not parse ID resp\n");
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001049 abort();
1050 }
1051
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001052 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001053 if (!out) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001054 printf("FAIL: A new message should be created.\n");
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001055 abort();
1056 }
1057
1058 if (msg == out) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001059 printf("FAIL: The message should have changed\n");
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001060 abort();
1061 }
1062
Holger Hans Peter Freyther50be1a92012-01-10 22:31:39 +01001063 verify_msg(out, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched));
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001064 msgb_free(out);
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001065
1066 /* Make sure that a wildcard is matching */
1067 entry.mnc = "*";
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001068 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001069 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 Freytherf5ede522012-01-06 13:56:12 +01001073 printf("FAIL: Could not parse ID resp\n");
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001074 abort();
1075 }
1076
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001077 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001078 if (!out) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001079 printf("FAIL: A new message should be created.\n");
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001080 abort();
1081 }
1082
1083 if (msg == out) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001084 printf("FAIL: The message should have changed\n");
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001085 abort();
1086 }
1087
Holger Hans Peter Freyther50be1a92012-01-10 22:31:39 +01001088 verify_msg(out, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched));
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001089 msgb_free(out);
1090
1091 /* Make sure that a wildcard is matching */
1092 entry.mnc = "09";
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001093 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001094 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 Freytherf5ede522012-01-06 13:56:12 +01001098 printf("FAIL: Could not parse ID resp\n");
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001099 abort();
1100 }
1101
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001102 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001103 if (out != msg) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001104 printf("FAIL: The message should be unchanged.\n");
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001105 abort();
1106 }
1107
Holger Hans Peter Freyther50be1a92012-01-10 22:31:39 +01001108 verify_msg(out, cc_setup_national, ARRAY_SIZE(cc_setup_national));
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001109 msgb_free(out);
Holger Hans Peter Freytherdbd94492013-04-02 12:34:11 +02001110
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 Freyther3615a302013-06-25 12:03:36 +02001166 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, NULL);
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +02001167 bsc_nat_free(nat);
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001168}
1169
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +02001170static 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 Freyther73ec6982014-08-29 09:56:58 +02001216 bsc_nat_free(nat);
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +02001217}
1218
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +02001219static 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 Freyther73ec6982014-08-29 09:56:58 +02001275 bsc_nat_free(nat);
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +02001276}
1277
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001278static void test_sms_smsc_rewrite()
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001279{
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 Freyther68368dd2012-01-10 22:39:07 +01001287 struct osmo_config_list smsc_entries, dest_entries, clear_entries;
1288 struct osmo_config_entry smsc_entry, dest_entry, clear_entry;
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001289
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +02001290 INIT_LLIST_HEAD(&smsc_entries.entry);
1291 INIT_LLIST_HEAD(&dest_entries.entry);
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001292 INIT_LLIST_HEAD(&clear_entries.entry);
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +02001293 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 Freyther68368dd2012-01-10 22:39:07 +01001302 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 Freytheracc40312011-05-27 19:21:24 +02001306
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 Freyther68368dd2012-01-10 22:39:07 +01001309 bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_clear_tp_srr, &clear_entries);
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001310
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001311 printf("Testing SMSC rewriting.\n");
1312
1313 /*
1314 * Check if the SMSC address is changed
1315 */
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001316 copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite));
1317 parsed = bsc_nat_parse(msg);
1318 if (!parsed) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001319 printf("FAIL: Could not parse SMS\n");
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001320 abort();
1321 }
1322
1323 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
1324 if (out == msg) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001325 printf("FAIL: This should have changed.\n");
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001326 abort();
1327 }
1328
Holger Hans Peter Freyther50be1a92012-01-10 22:31:39 +01001329 verify_msg(out, smsc_rewrite_patched, ARRAY_SIZE(smsc_rewrite_patched));
1330 msgb_free(out);
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001331
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 Freyther73ec6982014-08-29 09:56:58 +02001371 bsc_nat_free(nat);
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001372}
1373
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001374static void test_sms_number_rewrite(void)
1375{
Holger Hans Peter Freyther09db1a42012-03-26 16:21:42 +02001376 struct msgb *msg, *out;
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001377 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 Freyther09db1a42012-03-26 16:21:42 +02001383 struct osmo_config_list num_entries, clear_entries;
1384 struct osmo_config_entry num_entry, clear_entry;
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001385
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 Freyther09db1a42012-03-26 16:21:42 +02001399 msg = msgb_alloc(4096, "SMSC rewrite");
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001400 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 Freyther09db1a42012-03-26 16:21:42 +02001416
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 Freyther73ec6982014-08-29 09:56:58 +02001444 bsc_nat_free(nat);
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001445}
1446
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001447static 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 Freytherc36a6d52015-04-05 16:55:28 +02001457 rc = bsc_filter_barr_adapt(NULL, &root, lst);
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001458 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 Freytherc36a6d52015-04-05 16:55:28 +02001464 struct bsc_filter_barr_entry *entry;
1465 entry = rb_entry(node, struct bsc_filter_barr_entry, node);
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001466 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 Freytherc36a6d52015-04-05 16:55:28 +02001471 rc = bsc_filter_barr_find(&root, "12123119", &cm, &lu);
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001472 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 Freytherc36a6d52015-04-05 16:55:28 +02001483 bsc_filter_barr_adapt(NULL, &root, NULL);
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001484 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 Freytherc36a6d52015-04-05 16:55:28 +02001496 rc = bsc_filter_barr_adapt(NULL, &root, lst);
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001497 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 Freytherc36a6d52015-04-05 16:55:28 +02001505 struct bsc_filter_barr_entry *entry;
1506 entry = rb_entry(node, struct bsc_filter_barr_entry, node);
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001507 printf("IMSI: %s CM: %d LU: %d\n", entry->imsi,
1508 entry->cm_reject_cause, entry->lu_reject_cause);
1509
1510 }
Holger Hans Peter Freytherc36a6d52015-04-05 16:55:28 +02001511 rc = bsc_filter_barr_adapt(NULL, &root, NULL);
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001512}
1513
Holger Hans Peter Freytherb2b291d2013-04-16 13:23:43 +02001514static 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);
Max714b1702016-04-07 12:27:11 +02001528 bsc->cfg = bsc_config_alloc(nat, "foo", 0);
Holger Hans Peter Freytherb2b291d2013-04-16 13:23:43 +02001529
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 Freyther73ec6982014-08-29 09:56:58 +02001545
1546 bsc_nat_free(nat);
Holger Hans Peter Freytherb2b291d2013-04-16 13:23:43 +02001547}
1548
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +08001549int main(int argc, char **argv)
1550{
Holger Hans Peter Freyther30e1ae92010-07-30 02:53:14 +08001551 sccp_set_log_area(DSCCP);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001552 osmo_init_logging(&log_info);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +08001553
1554 test_filter();
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +02001555 test_contrack();
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +02001556 test_paging();
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +02001557 test_mgcp_ass_tracking();
1558 test_mgcp_find();
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +02001559 test_mgcp_rewrite();
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +02001560 test_mgcp_parse();
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +08001561 test_cr_filter();
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +08001562 test_dt_filter();
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001563 test_setup_rewrite();
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +02001564 test_setup_rewrite_prefix();
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +02001565 test_setup_rewrite_post();
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001566 test_sms_smsc_rewrite();
1567 test_sms_number_rewrite();
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +01001568 test_mgcp_allocations();
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001569 test_barr_list_parsing();
Holger Hans Peter Freytherb2b291d2013-04-16 13:23:43 +02001570 test_nat_extract_lac();
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001571
1572 printf("Testing execution completed.\n");
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001573 return 0;
1574}
Holger Hans Peter Freytherc3271872012-11-05 14:54:56 +01001575
1576/* stub */
1577void bsc_nat_send_mgcp_to_msc(struct bsc_nat *nat, struct msgb *msg)
1578{
1579 abort();
1580}