blob: 2914a01b537a674149d967a892a045da23a201af [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));
Holger Hans Peter Freyther5b692d02016-05-23 17:58:03 +0200981 OSMO_ASSERT(!con->filter_state.imsi);
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100982 if (bsc_nat_filter_dt(bsc, msg, con, parsed, &cause) != 1) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100983 printf("FAIL: Should have passed..\n");
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800984 abort();
985 }
Holger Hans Peter Freyther5b692d02016-05-23 17:58:03 +0200986 OSMO_ASSERT(con->filter_state.imsi);
987 OSMO_ASSERT(talloc_parent(con->filter_state.imsi) == con);
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800988
989 /* just some basic length checking... */
990 for (i = ARRAY_SIZE(id_resp); i >= 0; --i) {
991 msgb_reset(msg);
992 copy_to_msg(msg, id_resp, ARRAY_SIZE(id_resp));
993
994 parsed = bsc_nat_parse(msg);
995 if (!parsed)
996 continue;
997
Holger Hans Peter Freytherc09f8a32015-04-05 19:13:27 +0200998 con->filter_state.imsi_checked = 0;
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100999 memset(&cause, 0, sizeof(cause));
1000 bsc_nat_filter_dt(bsc, msg, con, parsed, &cause);
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +08001001 }
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +02001002
1003 msgb_free(msg);
1004 bsc_nat_free(nat);
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +08001005}
1006
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001007static void test_setup_rewrite()
1008{
1009 struct msgb *msg = msgb_alloc(4096, "test_dt_filter");
1010 struct msgb *out;
1011 struct bsc_nat_parsed *parsed;
1012 const char *imsi = "27408000001234";
1013
1014 struct bsc_nat *nat = bsc_nat_alloc();
1015
1016 /* a fake list */
Pablo Neira Ayusoab46cf32011-05-07 13:11:20 +02001017 struct osmo_config_list entries;
1018 struct osmo_config_entry entry;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001019
1020 INIT_LLIST_HEAD(&entries.entry);
1021 entry.mcc = "274";
1022 entry.mnc = "08";
1023 entry.option = "^0([1-9])";
1024 entry.text = "0049";
1025 llist_add_tail(&entry.list, &entries.entry);
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001026 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001027
1028 /* verify that nothing changed */
1029 msgb_reset(msg);
1030 copy_to_msg(msg, cc_setup_international, ARRAY_SIZE(cc_setup_international));
1031 parsed = bsc_nat_parse(msg);
1032 if (!parsed) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001033 printf("FAIL: Could not parse ID resp\n");
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001034 abort();
1035 }
1036
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001037 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001038 if (msg != out) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001039 printf("FAIL: The message should not have been changed\n");
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001040 abort();
1041 }
1042
Holger Hans Peter Freyther50be1a92012-01-10 22:31:39 +01001043 verify_msg(out, cc_setup_international, ARRAY_SIZE(cc_setup_international));
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001044 talloc_free(parsed);
1045
1046 /* verify that something in the message changes */
1047 msgb_reset(msg);
1048 copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national));
1049 parsed = bsc_nat_parse(msg);
1050 if (!parsed) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001051 printf("FAIL: Could not parse ID resp\n");
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001052 abort();
1053 }
1054
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001055 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001056 if (!out) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001057 printf("FAIL: A new message should be created.\n");
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001058 abort();
1059 }
1060
1061 if (msg == out) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001062 printf("FAIL: The message should have changed\n");
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001063 abort();
1064 }
1065
Holger Hans Peter Freyther50be1a92012-01-10 22:31:39 +01001066 verify_msg(out, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched));
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001067 msgb_free(out);
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001068
1069 /* Make sure that a wildcard is matching */
1070 entry.mnc = "*";
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001071 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001072 msg = msgb_alloc(4096, "test_dt_filter");
1073 copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national));
1074 parsed = bsc_nat_parse(msg);
1075 if (!parsed) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001076 printf("FAIL: Could not parse ID resp\n");
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001077 abort();
1078 }
1079
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001080 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001081 if (!out) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001082 printf("FAIL: A new message should be created.\n");
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001083 abort();
1084 }
1085
1086 if (msg == out) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001087 printf("FAIL: The message should have changed\n");
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001088 abort();
1089 }
1090
Holger Hans Peter Freyther50be1a92012-01-10 22:31:39 +01001091 verify_msg(out, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched));
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001092 msgb_free(out);
1093
1094 /* Make sure that a wildcard is matching */
1095 entry.mnc = "09";
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001096 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001097 msg = msgb_alloc(4096, "test_dt_filter");
1098 copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national));
1099 parsed = bsc_nat_parse(msg);
1100 if (!parsed) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001101 printf("FAIL: Could not parse ID resp\n");
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001102 abort();
1103 }
1104
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001105 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001106 if (out != msg) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001107 printf("FAIL: The message should be unchanged.\n");
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001108 abort();
1109 }
1110
Holger Hans Peter Freyther50be1a92012-01-10 22:31:39 +01001111 verify_msg(out, cc_setup_national, ARRAY_SIZE(cc_setup_national));
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001112 msgb_free(out);
Holger Hans Peter Freytherdbd94492013-04-02 12:34:11 +02001113
1114 /* Now see what happens to an international number */
1115 entry.mnc = "*";
1116 entry.option = "^\\+[0-9][0-9]([1-9])";
1117 entry.text = "0036";
1118 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
1119 msg = msgb_alloc(4096, "test_dt_filter");
1120 copy_to_msg(msg, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched));
1121 parsed = bsc_nat_parse(msg);
1122 if (!parsed) {
1123 printf("FAIL: Could not parse ID resp %d\n", __LINE__);
1124 abort();
1125 }
1126
1127 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
1128 if (!out) {
1129 printf("FAIL: A new message should be created %d.\n", __LINE__);
1130 abort();
1131 }
1132
1133 if (msg == out) {
1134 printf("FAIL: The message should have changed %d\n", __LINE__);
1135 abort();
1136 }
1137
1138 verify_msg(out, cc_setup_national_patched_patched,
1139 ARRAY_SIZE(cc_setup_national_patched_patched));
1140 msgb_free(out);
1141
1142 /* go from international back to national */
1143 entry.mnc = "*";
1144 entry.option = "^\\+([0-9])";
1145 entry.text = "36";
1146 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
1147 msg = msgb_alloc(4096, "test_dt_filter");
1148 copy_to_msg(msg, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched));
1149 parsed = bsc_nat_parse(msg);
1150 if (!parsed) {
1151 printf("FAIL: Could not parse ID resp %d\n", __LINE__);
1152 abort();
1153 }
1154
1155 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
1156 if (!out) {
1157 printf("FAIL: A new message should be created %d.\n", __LINE__);
1158 abort();
1159 }
1160
1161 if (msg == out) {
1162 printf("FAIL: The message should have changed %d\n", __LINE__);
1163 abort();
1164 }
1165
1166 verify_msg(out, cc_setup_national_again,
1167 ARRAY_SIZE(cc_setup_national_again));
1168 msgb_free(out);
Holger Hans Peter Freyther3615a302013-06-25 12:03:36 +02001169 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, NULL);
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +02001170 bsc_nat_free(nat);
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001171}
1172
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +02001173static void test_setup_rewrite_prefix(void)
1174{
1175 struct msgb *msg = msgb_alloc(4096, "test_dt_filter");
1176 struct msgb *out;
1177 struct bsc_nat_parsed *parsed;
1178 const char *imsi = "27408000001234";
1179
1180 struct bsc_nat *nat = bsc_nat_alloc();
1181
1182 /* a fake list */
1183 struct osmo_config_list entries;
1184 struct osmo_config_entry entry;
1185
1186 INIT_LLIST_HEAD(&entries.entry);
1187 entry.mcc = "274";
1188 entry.mnc = "08";
1189 entry.option = "^0([1-9])";
1190 entry.text = "prefix_lookup";
1191 llist_add_tail(&entry.list, &entries.entry);
1192 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
1193
1194 nat->num_rewr_trie = nat_rewrite_parse(nat, "prefixes.csv");
1195
1196 msgb_reset(msg);
1197 copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national));
1198 parsed = bsc_nat_parse(msg);
1199 if (!parsed) {
1200 printf("FAIL: Could not parse ID resp\n");
1201 abort();
1202 }
1203
1204 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
1205 if (!out) {
1206 printf("FAIL: A new message should be created.\n");
1207 abort();
1208 }
1209
1210 if (msg == out) {
1211 printf("FAIL: The message should have changed\n");
1212 abort();
1213 }
1214
1215 verify_msg(out, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched));
1216 msgb_free(out);
1217
1218 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, NULL);
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +02001219 bsc_nat_free(nat);
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +02001220}
1221
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +02001222static void test_setup_rewrite_post(void)
1223{
1224 struct msgb *msg = msgb_alloc(4096, "test_dt_filter");
1225 struct msgb *out;
1226 struct bsc_nat_parsed *parsed;
1227 const char *imsi = "27408000001234";
1228
1229 struct bsc_nat *nat = bsc_nat_alloc();
1230
1231 /* a fake list */
1232 struct osmo_config_list entries;
1233 struct osmo_config_entry entry;
1234 struct osmo_config_list entries_post;
1235 struct osmo_config_entry entry_post;
1236
1237 INIT_LLIST_HEAD(&entries.entry);
1238 entry.mcc = "274";
1239 entry.mnc = "08";
1240 entry.option = "^0([1-9])";
1241 entry.text = "0049";
1242 llist_add_tail(&entry.list, &entries.entry);
1243 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
1244
1245 /* attempt to undo the previous one */
1246 INIT_LLIST_HEAD(&entries_post.entry);
1247 entry_post.mcc = "274";
1248 entry_post.mnc = "08";
1249 entry_post.option = "^\\+49([1-9])";
1250 entry_post.text = "prefix_lookup";
1251 llist_add_tail(&entry_post.list, &entries_post.entry);
1252 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr_post, &entries_post);
1253
1254 nat->num_rewr_trie = nat_rewrite_parse(nat, "prefixes.csv");
1255
1256 msgb_reset(msg);
1257 copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national));
1258 parsed = bsc_nat_parse(msg);
1259 if (!parsed) {
1260 printf("FAIL: Could not parse ID resp\n");
1261 abort();
1262 }
1263
1264 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
1265 if (!out) {
1266 printf("FAIL: A new message should be created.\n");
1267 abort();
1268 }
1269
1270 if (msg == out) {
1271 printf("FAIL: The message should have changed\n");
1272 abort();
1273 }
1274
1275 verify_msg(out, cc_setup_national, ARRAY_SIZE(cc_setup_national));
1276 msgb_free(out);
1277
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +02001278 bsc_nat_free(nat);
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +02001279}
1280
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001281static void test_sms_smsc_rewrite()
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001282{
1283 struct msgb *msg = msgb_alloc(4096, "SMSC rewrite"), *out;
1284 struct bsc_nat_parsed *parsed;
1285 const char *imsi = "515039900406700";
1286
1287 struct bsc_nat *nat = bsc_nat_alloc();
1288
1289 /* a fake list */
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001290 struct osmo_config_list smsc_entries, dest_entries, clear_entries;
1291 struct osmo_config_entry smsc_entry, dest_entry, clear_entry;
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001292
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +02001293 INIT_LLIST_HEAD(&smsc_entries.entry);
1294 INIT_LLIST_HEAD(&dest_entries.entry);
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001295 INIT_LLIST_HEAD(&clear_entries.entry);
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +02001296 smsc_entry.mcc = "^515039";
1297 smsc_entry.option = "639180000105()";
1298 smsc_entry.text = "6666666666667";
1299 llist_add_tail(&smsc_entry.list, &smsc_entries.entry);
1300 dest_entry.mcc = "515";
1301 dest_entry.mnc = "03";
1302 dest_entry.option = "^0049";
1303 dest_entry.text = "";
1304 llist_add_tail(&dest_entry.list, &dest_entries.entry);
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001305 clear_entry.mcc = "^515039";
1306 clear_entry.option = "^0049";
1307 clear_entry.text = "";
1308 llist_add_tail(&clear_entry.list, &clear_entries.entry);
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +02001309
1310 bsc_nat_num_rewr_entry_adapt(nat, &nat->smsc_rewr, &smsc_entries);
1311 bsc_nat_num_rewr_entry_adapt(nat, &nat->tpdest_match, &dest_entries);
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001312 bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_clear_tp_srr, &clear_entries);
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001313
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001314 printf("Testing SMSC rewriting.\n");
1315
1316 /*
1317 * Check if the SMSC address is changed
1318 */
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001319 copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite));
1320 parsed = bsc_nat_parse(msg);
1321 if (!parsed) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001322 printf("FAIL: Could not parse SMS\n");
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001323 abort();
1324 }
1325
1326 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
1327 if (out == msg) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001328 printf("FAIL: This should have changed.\n");
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001329 abort();
1330 }
1331
Holger Hans Peter Freyther50be1a92012-01-10 22:31:39 +01001332 verify_msg(out, smsc_rewrite_patched, ARRAY_SIZE(smsc_rewrite_patched));
1333 msgb_free(out);
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001334
1335 /* clear out the filter for SMSC */
1336 printf("Attempting to only rewrite the HDR\n");
1337 bsc_nat_num_rewr_entry_adapt(nat, &nat->smsc_rewr, NULL);
1338 msg = msgb_alloc(4096, "SMSC rewrite");
1339 copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite));
1340 parsed = bsc_nat_parse(msg);
1341 if (!parsed) {
1342 printf("FAIL: Could not parse SMS\n");
1343 abort();
1344 }
1345
1346 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
1347 if (out == msg) {
1348 printf("FAIL: This should have changed.\n");
1349 abort();
1350 }
1351
1352 verify_msg(out, smsc_rewrite_patched_hdr, ARRAY_SIZE(smsc_rewrite_patched_hdr));
1353 msgb_free(out);
1354
1355 /* clear out the next filter */
1356 printf("Attempting to change nothing.\n");
1357 bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_clear_tp_srr, NULL);
1358 msg = msgb_alloc(4096, "SMSC rewrite");
1359 copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite));
1360 parsed = bsc_nat_parse(msg);
1361 if (!parsed) {
1362 printf("FAIL: Could not parse SMS\n");
1363 abort();
1364 }
1365
1366 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
1367 if (out != msg) {
1368 printf("FAIL: This should not have changed.\n");
1369 abort();
1370 }
1371
1372 verify_msg(out, smsc_rewrite, ARRAY_SIZE(smsc_rewrite));
1373 msgb_free(out);
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +02001374 bsc_nat_free(nat);
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001375}
1376
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001377static void test_sms_number_rewrite(void)
1378{
Holger Hans Peter Freyther09db1a42012-03-26 16:21:42 +02001379 struct msgb *msg, *out;
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001380 struct bsc_nat_parsed *parsed;
1381 const char *imsi = "515039900406700";
1382
1383 struct bsc_nat *nat = bsc_nat_alloc();
1384
1385 /* a fake list */
Holger Hans Peter Freyther09db1a42012-03-26 16:21:42 +02001386 struct osmo_config_list num_entries, clear_entries;
1387 struct osmo_config_entry num_entry, clear_entry;
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001388
1389 INIT_LLIST_HEAD(&num_entries.entry);
1390 num_entry.mcc = "^515039";
1391 num_entry.option = "^0049()";
1392 num_entry.text = "0032";
1393 llist_add_tail(&num_entry.list, &num_entries.entry);
1394
1395 bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_num_rewr, &num_entries);
1396
1397 printf("Testing SMS TP-DA rewriting.\n");
1398
1399 /*
1400 * Check if the SMSC address is changed
1401 */
Holger Hans Peter Freyther09db1a42012-03-26 16:21:42 +02001402 msg = msgb_alloc(4096, "SMSC rewrite");
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001403 copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite));
1404 parsed = bsc_nat_parse(msg);
1405 if (!parsed) {
1406 printf("FAIL: Could not parse SMS\n");
1407 abort();
1408 }
1409
1410 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
1411 if (out == msg) {
1412 printf("FAIL: This should have changed.\n");
1413 abort();
1414 }
1415
1416 verify_msg(out, smsc_rewrite_num_patched,
1417 ARRAY_SIZE(smsc_rewrite_num_patched));
1418 msgb_free(out);
Holger Hans Peter Freyther09db1a42012-03-26 16:21:42 +02001419
1420 /*
1421 * Now with TP-SRR rewriting enabled
1422 */
1423 INIT_LLIST_HEAD(&clear_entries.entry);
1424 clear_entry.mcc = "^515039";
1425 clear_entry.option = "";
1426 clear_entry.text = "";
1427 llist_add_tail(&clear_entry.list, &clear_entries.entry);
1428 bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_clear_tp_srr, &clear_entries);
1429
1430 msg = msgb_alloc(4096, "SMSC rewrite");
1431 copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite));
1432 parsed = bsc_nat_parse(msg);
1433 if (!parsed) {
1434 printf("FAIL: Could not parse SMS\n");
1435 abort();
1436 }
1437
1438 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
1439 if (out == msg) {
1440 printf("FAIL: This should have changed.\n");
1441 abort();
1442 }
1443
1444 verify_msg(out, smsc_rewrite_num_patched_tp_srr,
1445 ARRAY_SIZE(smsc_rewrite_num_patched_tp_srr));
1446 msgb_free(out);
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +02001447 bsc_nat_free(nat);
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001448}
1449
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001450static void test_barr_list_parsing(void)
1451{
1452 int rc;
1453 int cm, lu;
1454 struct rb_node *node;
1455 struct rb_root root = RB_ROOT;
1456 struct osmo_config_list *lst = osmo_config_list_parse(NULL, "barr.cfg");
1457 if (lst == NULL)
1458 abort();
1459
Holger Hans Peter Freytherc36a6d52015-04-05 16:55:28 +02001460 rc = bsc_filter_barr_adapt(NULL, &root, lst);
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001461 if (rc != 0)
1462 abort();
1463 talloc_free(lst);
1464
1465
1466 for (node = rb_first(&root); node; node = rb_next(node)) {
Holger Hans Peter Freytherc36a6d52015-04-05 16:55:28 +02001467 struct bsc_filter_barr_entry *entry;
1468 entry = rb_entry(node, struct bsc_filter_barr_entry, node);
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001469 printf("IMSI: %s CM: %d LU: %d\n", entry->imsi,
1470 entry->cm_reject_cause, entry->lu_reject_cause);
1471 }
1472
1473 /* do the look up now.. */
Holger Hans Peter Freytherc36a6d52015-04-05 16:55:28 +02001474 rc = bsc_filter_barr_find(&root, "12123119", &cm, &lu);
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001475 if (!rc) {
1476 printf("Failed to find the IMSI.\n");
1477 abort();
1478 }
1479
1480 if (cm != 3 || lu != 4) {
1481 printf("Found CM(%d) and LU(%d)\n", cm, lu);
1482 abort();
1483 }
1484
1485 /* empty and check that it is empty */
Holger Hans Peter Freytherc36a6d52015-04-05 16:55:28 +02001486 bsc_filter_barr_adapt(NULL, &root, NULL);
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001487 if (!RB_EMPTY_ROOT(&root)) {
1488 printf("Failed to empty the list.\n");
1489 abort();
1490 }
1491
1492 /* check that dup results in an error */
1493 lst = osmo_config_list_parse(NULL, "barr_dup.cfg");
1494 if (lst == NULL) {
1495 printf("Failed to parse list with dups\n");
1496 abort();
1497 }
1498
Holger Hans Peter Freytherc36a6d52015-04-05 16:55:28 +02001499 rc = bsc_filter_barr_adapt(NULL, &root, lst);
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001500 if (rc != -1) {
1501 printf("It should have failed due dup\n");
1502 abort();
1503 }
1504 talloc_free(lst);
1505
1506 /* dump for reference */
1507 for (node = rb_first(&root); node; node = rb_next(node)) {
Holger Hans Peter Freytherc36a6d52015-04-05 16:55:28 +02001508 struct bsc_filter_barr_entry *entry;
1509 entry = rb_entry(node, struct bsc_filter_barr_entry, node);
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001510 printf("IMSI: %s CM: %d LU: %d\n", entry->imsi,
1511 entry->cm_reject_cause, entry->lu_reject_cause);
1512
1513 }
Holger Hans Peter Freytherc36a6d52015-04-05 16:55:28 +02001514 rc = bsc_filter_barr_adapt(NULL, &root, NULL);
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001515}
1516
Holger Hans Peter Freytherb2b291d2013-04-16 13:23:43 +02001517static void test_nat_extract_lac()
1518{
1519 int res;
1520 struct bsc_connection *bsc;
1521 struct bsc_nat *nat;
1522 struct nat_sccp_connection con;
1523 struct bsc_nat_parsed *parsed;
1524 struct msgb *msg = msgb_alloc(4096, "test-message");
1525
1526 printf("Testing LAC extraction from SCCP CR\n");
1527
1528 /* initialize the testcase */
1529 nat = bsc_nat_alloc();
1530 bsc = bsc_connection_alloc(nat);
Max714b1702016-04-07 12:27:11 +02001531 bsc->cfg = bsc_config_alloc(nat, "foo", 0);
Holger Hans Peter Freytherb2b291d2013-04-16 13:23:43 +02001532
1533 memset(&con, 0, sizeof(con));
1534 con.bsc = bsc;
1535
1536 /* create the SCCP CR */
1537 msg->l2h = msgb_put(msg, ARRAY_SIZE(bssmap_cr));
1538 memcpy(msg->l2h, bssmap_cr, ARRAY_SIZE(bssmap_cr));
1539
1540 /* parse it and pass it on */
1541 parsed = bsc_nat_parse(msg);
1542 res = bsc_nat_extract_lac(bsc, &con, parsed, msg);
1543 OSMO_ASSERT(res == 0);
1544
1545 /* verify the LAC */
1546 OSMO_ASSERT(con.lac == 8210);
1547 OSMO_ASSERT(con.ci == 50000);
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +02001548
1549 bsc_nat_free(nat);
Holger Hans Peter Freytherb2b291d2013-04-16 13:23:43 +02001550}
1551
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +08001552int main(int argc, char **argv)
1553{
Neels Hofmeyr4c2d4ab2016-09-16 02:31:17 +02001554 msgb_talloc_ctx_init(NULL, 0);
Holger Hans Peter Freyther30e1ae92010-07-30 02:53:14 +08001555 sccp_set_log_area(DSCCP);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001556 osmo_init_logging(&log_info);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +08001557
1558 test_filter();
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +02001559 test_contrack();
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +02001560 test_paging();
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +02001561 test_mgcp_ass_tracking();
1562 test_mgcp_find();
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +02001563 test_mgcp_rewrite();
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +02001564 test_mgcp_parse();
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +08001565 test_cr_filter();
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +08001566 test_dt_filter();
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001567 test_setup_rewrite();
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +02001568 test_setup_rewrite_prefix();
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +02001569 test_setup_rewrite_post();
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001570 test_sms_smsc_rewrite();
1571 test_sms_number_rewrite();
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +01001572 test_mgcp_allocations();
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001573 test_barr_list_parsing();
Holger Hans Peter Freytherb2b291d2013-04-16 13:23:43 +02001574 test_nat_extract_lac();
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001575
1576 printf("Testing execution completed.\n");
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001577 return 0;
1578}
Holger Hans Peter Freytherc3271872012-11-05 14:54:56 +01001579
1580/* stub */
1581void bsc_nat_send_mgcp_to_msc(struct bsc_nat *nat, struct msgb *msg)
1582{
1583 abort();
1584}