blob: 5b01cc3cdd81d492b524685e190b7a03d0ff0bc7 [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 Freytherddf191e2013-06-25 11:44:01 +020029#include <openbsc/nat_rewrite_trie.h>
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080030
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +020031#include <osmocom/core/application.h>
Holger Hans Peter Freytherb2b291d2013-04-16 13:23:43 +020032#include <osmocom/core/backtrace.h>
Pablo Neira Ayuso928cb332011-03-26 22:08:53 +010033#include <osmocom/core/talloc.h>
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +080034
Harald Welted5db12c2010-08-03 15:11:51 +020035#include <osmocom/sccp/sccp.h>
Pablo Neira Ayuso928cb332011-03-26 22:08:53 +010036#include <osmocom/gsm/protocol/gsm_08_08.h>
Holger Hans Peter Freyther30e1ae92010-07-30 02:53:14 +080037
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080038#include <stdio.h>
39
40/* test messages for ipa */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080041static uint8_t ipa_id[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080042 0x00, 0x01, 0xfe, 0x06,
43};
44
45/* SCCP messages are below */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080046static uint8_t gsm_reset[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080047 0x00, 0x12, 0xfd,
48 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
49 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
50 0x01, 0x20,
51};
52
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080053static const uint8_t gsm_reset_ack[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080054 0x00, 0x13, 0xfd,
55 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
56 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
57 0x00, 0x01, 0x31,
58};
59
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080060static const uint8_t gsm_paging[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080061 0x00, 0x20, 0xfd,
62 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
63 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x10,
64 0x00, 0x0e, 0x52, 0x08, 0x08, 0x29, 0x47, 0x10,
65 0x02, 0x01, 0x31, 0x97, 0x61, 0x1a, 0x01, 0x06,
66};
67
68/* BSC -> MSC connection open */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080069static const uint8_t bssmap_cr[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080070 0x00, 0x2c, 0xfd,
71 0x01, 0x01, 0x02, 0x03, 0x02, 0x02, 0x04, 0x02,
72 0x42, 0xfe, 0x0f, 0x1f, 0x00, 0x1d, 0x57, 0x05,
73 0x08, 0x00, 0x72, 0xf4, 0x80, 0x20, 0x12, 0xc3,
74 0x50, 0x17, 0x10, 0x05, 0x24, 0x11, 0x03, 0x33,
75 0x19, 0xa2, 0x08, 0x29, 0x47, 0x10, 0x02, 0x01,
76 0x31, 0x97, 0x61, 0x00
77};
78
79/* MSC -> BSC connection confirm */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080080static const uint8_t bssmap_cc[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080081 0x00, 0x0a, 0xfd,
82 0x02, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00,
83};
84
85/* MSC -> BSC released */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080086static const uint8_t bssmap_released[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080087 0x00, 0x0e, 0xfd,
88 0x04, 0x00, 0x00, 0x03, 0x01, 0x02, 0x03, 0x00, 0x01, 0x0f,
89 0x02, 0x23, 0x42, 0x00,
90};
91
92/* BSC -> MSC released */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080093static const uint8_t bssmap_release_complete[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080094 0x00, 0x07, 0xfd,
95 0x05, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03
96};
97
Holger Hans Peter Freyther0a6f62f2010-04-06 10:22:01 +020098/* both directions IT timer */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080099static const uint8_t connnection_it[] = {
Holger Hans Peter Freyther0a6f62f2010-04-06 10:22:01 +0200100 0x00, 0x0b, 0xfd,
101 0x10, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03,
102 0x00, 0x00, 0x00, 0x00,
103};
104
Holger Hans Peter Freyther5e63f922010-04-21 15:45:26 +0800105/* error in both directions */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800106static const uint8_t proto_error[] = {
Holger Hans Peter Freyther5e63f922010-04-21 15:45:26 +0800107 0x00, 0x05, 0xfd,
108 0x0f, 0x22, 0x33, 0x44, 0x00,
109};
110
Holger Hans Peter Freythera128d912010-04-01 08:47:12 +0200111/* MGCP wrap... */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800112static const uint8_t mgcp_msg[] = {
Holger Hans Peter Freythera128d912010-04-01 08:47:12 +0200113 0x00, 0x03, 0xfc,
114 0x20, 0x20, 0x20,
115};
116
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800117/* location updating request */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800118static const uint8_t bss_lu[] = {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800119 0x00, 0x2e, 0xfd,
120 0x01, 0x91, 0x45, 0x14, 0x02, 0x02, 0x04, 0x02,
121 0x42, 0xfe, 0x0f, 0x21, 0x00, 0x1f, 0x57, 0x05,
122 0x08, 0x00, 0x72, 0xf4, 0x80, 0x20, 0x14, 0xc3,
123 0x50, 0x17, 0x12, 0x05, 0x08, 0x70, 0x72, 0xf4,
124 0x80, 0xff, 0xfe, 0x30, 0x08, 0x29, 0x44, 0x50,
125 0x12, 0x03, 0x24, 0x01, 0x95, 0x00
126};
127
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800128/* paging response */
129static const uint8_t pag_resp[] = {
130 0x00, 0x2c, 0xfd, 0x01, 0xe5, 0x68,
131 0x14, 0x02, 0x02, 0x04, 0x02, 0x42, 0xfe, 0x0f,
132 0x1f, 0x00, 0x1d, 0x57, 0x05, 0x08, 0x00, 0x72,
133 0xf4, 0x80, 0x20, 0x16, 0xc3, 0x50, 0x17, 0x10,
134 0x06, 0x27, 0x01, 0x03, 0x30, 0x18, 0x96, 0x08,
135 0x29, 0x26, 0x30, 0x32, 0x11, 0x42, 0x01, 0x19,
136 0x00
137};
138
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800139struct filter_result {
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800140 const uint8_t *data;
Holger Hans Peter Freythere2c15202010-07-23 19:09:21 +0800141 const uint16_t length;
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100142 const int dir;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800143 const int result;
144};
145
146static const struct filter_result results[] = {
147 {
148 .data = ipa_id,
149 .length = ARRAY_SIZE(ipa_id),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100150 .dir = DIR_MSC,
151 .result = 1,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800152 },
153 {
154 .data = gsm_reset,
155 .length = ARRAY_SIZE(gsm_reset),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100156 .dir = DIR_MSC,
157 .result = 1,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800158 },
159 {
160 .data = gsm_reset_ack,
161 .length = ARRAY_SIZE(gsm_reset_ack),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100162 .dir = DIR_BSC,
163 .result = 1,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800164 },
165 {
166 .data = gsm_paging,
167 .length = ARRAY_SIZE(gsm_paging),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100168 .dir = DIR_BSC,
169 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800170 },
171 {
172 .data = bssmap_cr,
173 .length = ARRAY_SIZE(bssmap_cr),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100174 .dir = DIR_MSC,
175 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800176 },
177 {
178 .data = bssmap_cc,
179 .length = ARRAY_SIZE(bssmap_cc),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100180 .dir = DIR_BSC,
181 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800182 },
183 {
184 .data = bssmap_released,
185 .length = ARRAY_SIZE(bssmap_released),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100186 .dir = DIR_MSC,
187 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800188 },
189 {
190 .data = bssmap_release_complete,
191 .length = ARRAY_SIZE(bssmap_release_complete),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100192 .dir = DIR_BSC,
193 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800194 },
Holger Hans Peter Freythera128d912010-04-01 08:47:12 +0200195 {
196 .data = mgcp_msg,
197 .length = ARRAY_SIZE(mgcp_msg),
198 .dir = DIR_MSC,
199 .result = 0,
200 },
Holger Hans Peter Freyther0a6f62f2010-04-06 10:22:01 +0200201 {
202 .data = connnection_it,
203 .length = ARRAY_SIZE(connnection_it),
204 .dir = DIR_BSC,
205 .result = 0,
206 },
207 {
208 .data = connnection_it,
209 .length = ARRAY_SIZE(connnection_it),
210 .dir = DIR_MSC,
211 .result = 0,
212 },
Holger Hans Peter Freyther5e63f922010-04-21 15:45:26 +0800213 {
214 .data = proto_error,
215 .length = ARRAY_SIZE(proto_error),
216 .dir = DIR_BSC,
217 .result = 0,
218 },
219 {
220 .data = proto_error,
221 .length = ARRAY_SIZE(proto_error),
222 .dir = DIR_MSC,
223 .result = 0,
224 },
225
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800226};
227
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800228static void test_filter(void)
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800229{
230 int i;
231
232
233 /* start testinh with proper messages */
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100234 printf("Testing BSS Filtering.\n");
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800235 for (i = 0; i < ARRAY_SIZE(results); ++i) {
236 int result;
237 struct bsc_nat_parsed *parsed;
238 struct msgb *msg = msgb_alloc(4096, "test-message");
239
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100240 printf("Going to test item: %d\n", i);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800241 memcpy(msg->data, results[i].data, results[i].length);
242 msg->l2h = msgb_put(msg, results[i].length);
243
244 parsed = bsc_nat_parse(msg);
245 if (!parsed) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100246 printf("FAIL: Failed to parse the message\n");
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800247 continue;
248 }
249
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100250 result = bsc_nat_filter_ipa(results[i].dir, msg, parsed);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800251 if (result != results[i].result) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100252 printf("FAIL: Not the expected result got: %d wanted: %d\n",
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800253 result, results[i].result);
254 }
255
256 msgb_free(msg);
257 }
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800258}
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800259
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800260#include "bsc_data.c"
261
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800262static void copy_to_msg(struct msgb *msg, const uint8_t *data, unsigned int length)
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800263{
264 msgb_reset(msg);
265 msg->l2h = msgb_put(msg, length);
266 memcpy(msg->l2h, data, msgb_l2len(msg));
267}
268
Holger Hans Peter Freyther50be1a92012-01-10 22:31:39 +0100269static void verify_msg(struct msgb *out, const uint8_t *ref, int ref_len)
270{
271 if (out->len != ref_len) {
Holger Hans Peter Freytherbe530122012-01-18 17:20:23 +0100272 printf("FAIL: The size should match: %d vs. %d\n",
273 out->len, ref_len);
274 printf("%s\n", osmo_hexdump(out->data, out->len));
275 printf("Wanted\n");
276 printf("%s\n", osmo_hexdump(ref, ref_len));
Holger Hans Peter Freyther50be1a92012-01-10 22:31:39 +0100277 abort();
278 }
279
280 if (memcmp(out->data, ref, out->len) != 0) {
281 printf("FAIL: the data should be changed.\n");
282 printf("%s\n", osmo_hexdump(out->data, out->len));
283 printf("Wanted\n");
284 printf("%s\n", osmo_hexdump(ref, ref_len));
285 abort();
286 }
287}
288
289
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800290#define VERIFY(con_found, con, msg, ver, str) \
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100291 if (!con_found) { \
292 printf("Failed to find connection.\n"); \
293 abort(); \
294 } \
295 if (con_found->bsc != con) { \
296 printf("Got connection of the wrong BSC: %d\n", \
297 con_found->bsc->cfg->nr); \
298 abort(); \
299 } \
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800300 if (memcmp(msg->data, ver, sizeof(ver)) != 0) { \
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100301 printf("Failed to patch the %s msg.\n", str); \
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800302 abort(); \
303 }
304
305/* test conn tracking once */
306static void test_contrack()
307{
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800308 struct bsc_nat *nat;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800309 struct bsc_connection *con;
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200310 struct nat_sccp_connection *con_found;
311 struct nat_sccp_connection *rc_con;
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800312 struct bsc_nat_parsed *parsed;
313 struct msgb *msg;
314
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100315 printf("Testing connection tracking.\n");
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800316 nat = bsc_nat_alloc();
317 con = bsc_connection_alloc(nat);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800318 con->cfg = bsc_config_alloc(nat, "foo");
319 bsc_config_add_lac(con->cfg, 23);
320 bsc_config_add_lac(con->cfg, 49);
321 bsc_config_add_lac(con->cfg, 42);
322 bsc_config_del_lac(con->cfg, 49);
323 bsc_config_add_lac(con->cfg, 1111);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800324 msg = msgb_alloc(4096, "test");
325
326 /* 1.) create a connection */
327 copy_to_msg(msg, bsc_cr, sizeof(bsc_cr));
328 parsed = bsc_nat_parse(msg);
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800329 con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800330 if (con_found != NULL) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100331 printf("Con should not exist realref(%u)\n",
332 sccp_src_ref_to_int(&con_found->real_ref));
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800333 abort();
334 }
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +0800335 rc_con = create_sccp_src_ref(con, parsed);
336 if (!rc_con) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100337 printf("Failed to create a ref\n");
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800338 abort();
339 }
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800340 con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100341 if (!con_found) {
342 printf("Failed to find connection.\n");
343 abort();
344 }
345 if (con_found->bsc != con) {
346 printf("Got connection of the wrong BSC: %d\n",
347 con_found->bsc->cfg->nr);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800348 abort();
349 }
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +0800350 if (con_found != rc_con) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100351 printf("Failed to find the right connection.\n");
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +0800352 abort();
353 }
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800354 if (memcmp(msg->data, bsc_cr_patched, sizeof(bsc_cr_patched)) != 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100355 printf("Failed to patch the BSC CR msg.\n");
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800356 abort();
357 }
358 talloc_free(parsed);
359
360 /* 2.) get the cc */
361 copy_to_msg(msg, msc_cc, sizeof(msc_cc));
362 parsed = bsc_nat_parse(msg);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800363 con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
364 VERIFY(con_found, con, msg, msc_cc_patched, "MSC CC");
365 if (update_sccp_src_ref(con_found, parsed) != 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100366 printf("Failed to update the SCCP con.\n");
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800367 abort();
368 }
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800369
370 /* 3.) send some data */
371 copy_to_msg(msg, bsc_dtap, sizeof(bsc_dtap));
372 parsed = bsc_nat_parse(msg);
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800373 con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800374 VERIFY(con_found, con, msg, bsc_dtap_patched, "BSC DTAP");
375
376 /* 4.) receive some data */
377 copy_to_msg(msg, msc_dtap, sizeof(msc_dtap));
378 parsed = bsc_nat_parse(msg);
379 con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
380 VERIFY(con_found, con, msg, msc_dtap_patched, "MSC DTAP");
381
382 /* 5.) close the connection */
383 copy_to_msg(msg, msc_rlsd, sizeof(msc_rlsd));
384 parsed = bsc_nat_parse(msg);
385 con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
386 VERIFY(con_found, con, msg, msc_rlsd_patched, "MSC RLSD");
387
388 /* 6.) confirm the connection close */
389 copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc));
390 parsed = bsc_nat_parse(msg);
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800391 con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100392 if (!con_found) {
393 printf("Failed to find connection.\n");
394 abort();
395 }
396 if (con_found->bsc != con) {
397 printf("Got connection of the wrong BSC: %d\n",
398 con_found->bsc->cfg->nr);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800399 abort();
400 }
401 if (memcmp(msg->data, bsc_rlc_patched, sizeof(bsc_rlc_patched)) != 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100402 printf("Failed to patch the BSC CR msg.\n");
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800403 abort();
404 }
405 remove_sccp_src_ref(con, msg, parsed);
Holger Hans Peter Freyther9d518552010-04-05 21:44:51 +0200406 talloc_free(parsed);
407
408 copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc));
409 parsed = bsc_nat_parse(msg);
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800410 con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800411
412 /* verify that it is gone */
413 if (con_found != NULL) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100414 printf("Con should not exist real_ref(%u)\n",
415 sccp_src_ref_to_int(&con_found->real_ref));
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800416 abort();
417 }
418 talloc_free(parsed);
419
420
Holger Hans Peter Freyther9212d9d2011-02-27 11:18:41 +0100421 bsc_config_free(con->cfg);
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +0200422 bsc_nat_free(nat);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800423 msgb_free(msg);
424}
425
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200426static void test_paging(void)
427{
428 struct bsc_nat *nat;
429 struct bsc_connection *con;
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800430 struct bsc_config *cfg;
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200431
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100432 printf("Testing paging by lac.\n");
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200433
434 nat = bsc_nat_alloc();
435 con = bsc_connection_alloc(nat);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800436 cfg = bsc_config_alloc(nat, "unknown");
437 con->cfg = cfg;
438 bsc_config_add_lac(cfg, 23);
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200439 con->authenticated = 1;
440 llist_add(&con->list_entry, &nat->bsc_connections);
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200441
442 /* Test it by not finding it */
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200443 if (bsc_config_handles_lac(cfg, 8213) != 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100444 printf("Should not be handled.\n");
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200445 abort();
446 }
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200447
448 /* Test by finding it */
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800449 bsc_config_del_lac(cfg, 23);
450 bsc_config_add_lac(cfg, 8213);
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200451 if (bsc_config_handles_lac(cfg, 8213) == 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100452 printf("Should have found it.\n");
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200453 abort();
454 }
Holger Hans Peter Freyther3615a302013-06-25 12:03:36 +0200455
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +0200456 bsc_nat_free(nat);
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200457}
458
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100459static void test_mgcp_allocations(void)
460{
461#if 0
462 struct bsc_connection *bsc;
463 struct bsc_nat *nat;
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200464 struct nat_sccp_connection con;
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100465 int i, j, multiplex;
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100466
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100467 printf("Testing MGCP.\n");
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100468 memset(&con, 0, sizeof(con));
469
470 nat = bsc_nat_alloc();
471 nat->bsc_endpoints = talloc_zero_array(nat,
472 struct bsc_endpoint,
473 65);
474 nat->mgcp_cfg = mgcp_config_alloc();
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100475 nat->mgcp_cfg->trunk.number_endpoints = 64;
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100476
477 bsc = bsc_connection_alloc(nat);
478 bsc->cfg = bsc_config_alloc(nat, "foo");
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100479 bsc->cfg->max_endpoints = 60;
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100480 bsc_config_add_lac(bsc->cfg, 2323);
481 bsc->last_endpoint = 0x22;
482 con.bsc = bsc;
483
484 bsc_init_endps_if_needed(bsc);
485
486 i = 1;
487 do {
488 if (bsc_assign_endpoint(bsc, &con) != 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100489 printf("failed to allocate... on iteration %d\n", i);
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100490 break;
491 }
492 ++i;
493 } while(1);
494
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100495 multiplex = bsc_mgcp_nr_multiplexes(bsc->cfg->max_endpoints);
496 for (i = 0; i < multiplex; ++i) {
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100497 for (j = 0; j < 32; ++j)
498 printf("%d", bsc->_endpoint_status[i*32 + j]);
499 printf(": %d of %d\n", i*32 + 32, 32 * 8);
500 }
501#endif
502}
503
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200504static void test_mgcp_ass_tracking(void)
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800505{
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800506 struct bsc_connection *bsc;
507 struct bsc_nat *nat;
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200508 struct nat_sccp_connection con;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800509 struct bsc_nat_parsed *parsed;
510 struct msgb *msg;
511
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100512 printf("Testing MGCP.\n");
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800513 memset(&con, 0, sizeof(con));
514
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800515 nat = bsc_nat_alloc();
516 nat->bsc_endpoints = talloc_zero_array(nat,
517 struct bsc_endpoint,
518 33);
Holger Hans Peter Freyther7e0cc502011-02-25 12:43:58 +0100519 nat->mgcp_cfg = mgcp_config_alloc();
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100520 nat->mgcp_cfg->trunk.number_endpoints = 64;
Holger Hans Peter Freyther462b7d72012-10-24 21:53:40 +0200521 mgcp_endpoints_allocate(&nat->mgcp_cfg->trunk);
Holger Hans Peter Freyther7e0cc502011-02-25 12:43:58 +0100522
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800523 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800524 bsc->cfg = bsc_config_alloc(nat, "foo");
525 bsc_config_add_lac(bsc->cfg, 2323);
Holger Hans Peter Freyther86c1db62011-02-25 17:10:25 +0100526 bsc->last_endpoint = 0x1e;
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800527 con.bsc = bsc;
528
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800529 msg = msgb_alloc(4096, "foo");
530 copy_to_msg(msg, ass_cmd, sizeof(ass_cmd));
531 parsed = bsc_nat_parse(msg);
Holger Hans Peter Freythercd702372010-09-20 01:21:51 +0800532
533 if (msg->l2h[16] != 0 ||
534 msg->l2h[17] != 0x1) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100535 printf("Input is not as expected.. %s 0x%x\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200536 osmo_hexdump(msg->l2h, msgb_l2len(msg)),
Holger Hans Peter Freythercd702372010-09-20 01:21:51 +0800537 msg->l2h[17]);
538 abort();
539 }
540
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800541 if (bsc_mgcp_assign_patch(&con, msg) != 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100542 printf("Failed to handle assignment.\n");
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800543 abort();
544 }
545
Holger Hans Peter Freythercd702372010-09-20 01:21:51 +0800546 if (con.msc_endp != 1) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100547 printf("Timeslot should be 1.\n");
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800548 abort();
549 }
550
Holger Hans Peter Freyther86c1db62011-02-25 17:10:25 +0100551 if (con.bsc_endp != 0x1) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100552 printf("Assigned timeslot should have been 1.\n");
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800553 abort();
554 }
Holger Hans Peter Freyther86c1db62011-02-25 17:10:25 +0100555 if (con.bsc->_endpoint_status[0x1] != 1) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100556 printf("The status on the BSC is wrong.\n");
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800557 abort();
558 }
559
Holger Hans Peter Freythercd702372010-09-20 01:21:51 +0800560 int multiplex, timeslot;
Holger Hans Peter Freyther86c1db62011-02-25 17:10:25 +0100561 mgcp_endpoint_to_timeslot(0x1, &multiplex, &timeslot);
Holger Hans Peter Freythercd702372010-09-20 01:21:51 +0800562
563 uint16_t cic = htons(timeslot & 0x1f);
564 if (memcmp(&cic, &msg->l2h[16], sizeof(cic)) != 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100565 printf("Message was not patched properly\n");
566 printf("data cic: 0x%x %s\n", cic, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Holger Hans Peter Freythercd702372010-09-20 01:21:51 +0800567 abort();
568 }
569
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800570 talloc_free(parsed);
571
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800572 bsc_mgcp_dlcx(&con);
Holger Hans Peter Freytherd2df4ca2010-09-19 20:54:21 +0800573 if (con.bsc_endp != -1 || con.msc_endp != -1 ||
Holger Hans Peter Freyther86c1db62011-02-25 17:10:25 +0100574 con.bsc->_endpoint_status[1] != 0 || con.bsc->last_endpoint != 0x1) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100575 printf("Clearing should remove the mapping.\n");
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800576 abort();
577 }
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800578
Holger Hans Peter Freyther9212d9d2011-02-27 11:18:41 +0100579 bsc_config_free(bsc->cfg);
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +0200580 bsc_nat_free(nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800581}
582
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200583/* test the code to find a given connection */
584static void test_mgcp_find(void)
585{
586 struct bsc_nat *nat;
587 struct bsc_connection *con;
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200588 struct nat_sccp_connection *sccp_con;
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200589
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100590 printf("Testing finding of a BSC Connection\n");
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200591
592 nat = bsc_nat_alloc();
593 con = bsc_connection_alloc(nat);
594 llist_add(&con->list_entry, &nat->bsc_connections);
595
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200596 sccp_con = talloc_zero(con, struct nat_sccp_connection);
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800597 sccp_con->msc_endp = 12;
598 sccp_con->bsc_endp = 12;
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200599 sccp_con->bsc = con;
600 llist_add(&sccp_con->list_entry, &nat->sccp_connections);
601
602 if (bsc_mgcp_find_con(nat, 11) != NULL) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100603 printf("Found the wrong connection.\n");
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200604 abort();
605 }
606
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800607 if (bsc_mgcp_find_con(nat, 12) != sccp_con) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100608 printf("Didn't find the connection\n");
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200609 abort();
610 }
611
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200612 /* free everything */
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +0200613 bsc_nat_free(nat);
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200614}
615
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200616static void test_mgcp_rewrite(void)
617{
618 int i;
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200619 struct msgb *output;
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100620 printf("Testing rewriting MGCP messages.\n");
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200621
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200622 for (i = 0; i < ARRAY_SIZE(mgcp_messages); ++i) {
623 const char *orig = mgcp_messages[i].orig;
624 const char *patc = mgcp_messages[i].patch;
625 const char *ip = mgcp_messages[i].ip;
626 const int port = mgcp_messages[i].port;
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100627 const int expected_payload_type = mgcp_messages[i].payload_type;
Holger Hans Peter Freyther7f100c92015-04-23 20:25:17 -0400628 const int ensure_mode_set = mgcp_messages[i].ensure_mode_set;
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100629 int payload_type = -1;
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200630
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200631 char *input = strdup(orig);
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200632
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100633 output = bsc_mgcp_rewrite(input, strlen(input), 0x1e,
Holger Hans Peter Freyther7f100c92015-04-23 20:25:17 -0400634 ip, port, -1, &payload_type, ensure_mode_set);
Jacob Erlbecka52ac662013-11-29 13:43:45 +0100635
636 if (payload_type != -1) {
637 fprintf(stderr, "Found media payload type %d in SDP data\n",
638 payload_type);
639 if (payload_type != expected_payload_type) {
640 printf("Wrong payload type %d (expected %d)\n",
641 payload_type, expected_payload_type);
642 abort();
643 }
644 }
645
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200646 if (msgb_l2len(output) != strlen(patc)) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100647 printf("Wrong sizes for test: %d %d != %d != %d\n", i, msgb_l2len(output), strlen(patc), strlen(orig));
648 printf("String '%s' vs '%s'\n", (const char *) output->l2h, patc);
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200649 abort();
650 }
651
652 if (memcmp(output->l2h, patc, msgb_l2len(output)) != 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100653 printf("Broken on %d msg: '%s'\n", i, (const char *) output->l2h);
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200654 abort();
655 }
656
657 msgb_free(output);
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200658 free(input);
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200659 }
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200660}
661
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200662static void test_mgcp_parse(void)
663{
664 int code, ci;
665 char transaction[60];
666
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100667 printf("Testing MGCP response parsing.\n");
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200668
669 if (bsc_mgcp_parse_response(crcx_resp, &code, transaction) != 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100670 printf("Failed to parse CRCX resp.\n");
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200671 abort();
672 }
673
674 if (code != 200) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100675 printf("Failed to parse the CODE properly. Got: %d\n", code);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200676 abort();
677 }
678
679 if (strcmp(transaction, "23265295") != 0) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100680 printf("Failed to parse transaction id: '%s'\n", transaction);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200681 abort();
682 }
683
684 ci = bsc_mgcp_extract_ci(crcx_resp);
685 if (ci != 1) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100686 printf("Failed to parse the CI. Got: %d\n", ci);
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200687 abort();
688 }
689}
690
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800691struct cr_filter {
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800692 const uint8_t *data;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800693 int length;
694 int result;
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800695 int contype;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800696
697 const char *bsc_imsi_allow;
698 const char *bsc_imsi_deny;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800699 const char *nat_imsi_deny;
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100700 int nat_cm_reject_cause;
701 int nat_lu_reject_cause;
702 int bsc_cm_reject_cause;
703 int bsc_lu_reject_cause;
704 int want_cm_reject_cause;
705 int want_lu_reject_cause;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800706};
707
708static struct cr_filter cr_filter[] = {
709 {
710 .data = bssmap_cr,
711 .length = sizeof(bssmap_cr),
Holger Hans Peter Freytherd880f542010-09-15 01:42:07 +0800712 .result = 1,
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800713 .contype = NAT_CON_TYPE_CM_SERV_REQ,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100714 .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
715 .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
716 .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
717 .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
718 .want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
719 .want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800720 },
721 {
722 .data = bss_lu,
723 .length = sizeof(bss_lu),
Holger Hans Peter Freytherd880f542010-09-15 01:42:07 +0800724 .result = 1,
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800725 .contype = NAT_CON_TYPE_LU,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100726 .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
727 .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
728 .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
729 .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
730 .want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
731 .want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800732 },
733 {
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800734 .data = pag_resp,
735 .length = sizeof(pag_resp),
Holger Hans Peter Freytherd880f542010-09-15 01:42:07 +0800736 .result = 1,
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800737 .contype = NAT_CON_TYPE_PAG_RESP,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100738 .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
739 .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
740 .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
741 .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
742 .want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
743 .want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800744 },
745 {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800746 /* nat deny is before blank/null BSC */
747 .data = bss_lu,
748 .length = sizeof(bss_lu),
749 .result = -3,
750 .nat_imsi_deny = "[0-9]*",
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800751 .contype = NAT_CON_TYPE_LU,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100752 .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
753 .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
754 .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
755 .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
756 .want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
757 .want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800758 },
759 {
Holger Hans Peter Freythera0aeaa72010-05-14 23:07:58 +0800760 /* BSC allow is before NAT deny */
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800761 .data = bss_lu,
762 .length = sizeof(bss_lu),
Holger Hans Peter Freytherd880f542010-09-15 01:42:07 +0800763 .result = 1,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800764 .nat_imsi_deny = "[0-9]*",
Holger Hans Peter Freythera0aeaa72010-05-14 23:07:58 +0800765 .bsc_imsi_allow = "2440[0-9]*",
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800766 .contype = NAT_CON_TYPE_LU,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100767 .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
768 .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
769 .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
770 .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
771 .want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
772 .want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800773 },
774 {
775 /* BSC allow is before NAT deny */
776 .data = bss_lu,
777 .length = sizeof(bss_lu),
Holger Hans Peter Freytherd880f542010-09-15 01:42:07 +0800778 .result = 1,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800779 .bsc_imsi_allow = "[0-9]*",
780 .nat_imsi_deny = "[0-9]*",
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800781 .contype = NAT_CON_TYPE_LU,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100782 .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
783 .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
784 .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
785 .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
786 .want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
787 .want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800788 },
789 {
790 /* filter as deny is first */
791 .data = bss_lu,
792 .length = sizeof(bss_lu),
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200793 .result = 1,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800794 .bsc_imsi_deny = "[0-9]*",
795 .bsc_imsi_allow = "[0-9]*",
796 .nat_imsi_deny = "[0-9]*",
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800797 .contype = NAT_CON_TYPE_LU,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100798 .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
799 .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
800 .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
801 .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
802 .want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
803 .want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800804 },
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200805 {
806 /* deny by nat rule */
807 .data = bss_lu,
808 .length = sizeof(bss_lu),
809 .result = -3,
810 .bsc_imsi_deny = "000[0-9]*",
811 .nat_imsi_deny = "[0-9]*",
812 .contype = NAT_CON_TYPE_LU,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100813 .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
814 .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
815 .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
816 .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
817 .want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
818 .want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
819 },
820 {
821 /* deny by nat rule */
822 .data = bss_lu,
823 .length = sizeof(bss_lu),
824 .result = -3,
825 .bsc_imsi_deny = "000[0-9]*",
826 .nat_imsi_deny = "[0-9]*",
827 .contype = NAT_CON_TYPE_LU,
828 .nat_cm_reject_cause = 0x23,
829 .nat_lu_reject_cause = 0x42,
830 .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
831 .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
832 .want_lu_reject_cause = 0x42,
833 .want_cm_reject_cause = 0x23,
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200834 },
835 {
836 /* deny by bsc rule */
837 .data = bss_lu,
838 .length = sizeof(bss_lu),
839 .result = -2,
840 .bsc_imsi_deny = "[0-9]*",
841 .contype = NAT_CON_TYPE_LU,
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100842 .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
843 .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
844 .bsc_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
845 .bsc_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
846 .want_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
847 .want_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200848 },
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100849 {
850 /* deny by bsc rule */
851 .data = bss_lu,
852 .length = sizeof(bss_lu),
853 .result = -2,
854 .bsc_imsi_deny = "[0-9]*",
855 .contype = NAT_CON_TYPE_LU,
856 .nat_cm_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
857 .nat_lu_reject_cause = GSM48_REJECT_PLMN_NOT_ALLOWED,
858 .bsc_cm_reject_cause = 0x42,
859 .bsc_lu_reject_cause = 0x23,
860 .want_lu_reject_cause = 0x23,
861 .want_cm_reject_cause = 0x42,
862 },
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800863};
864
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800865static void test_cr_filter()
866{
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800867 int i, res, contype;
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800868 struct msgb *msg = msgb_alloc(4096, "test_cr_filter");
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800869 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800870 struct bsc_nat_acc_lst *nat_lst, *bsc_lst;
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800871 struct bsc_nat_acc_lst_entry *nat_entry, *bsc_entry;
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100872 struct bsc_nat_reject_cause cause;
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800873
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800874 struct bsc_nat *nat = bsc_nat_alloc();
875 struct bsc_connection *bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800876 bsc->cfg = bsc_config_alloc(nat, "foo");
877 bsc_config_add_lac(bsc->cfg, 1234);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800878 bsc->cfg->acc_lst_name = "bsc";
879 nat->acc_lst_name = "nat";
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800880
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800881 nat_lst = bsc_nat_acc_lst_get(nat, "nat");
882 bsc_lst = bsc_nat_acc_lst_get(nat, "bsc");
883
884 bsc_entry = bsc_nat_acc_lst_entry_create(bsc_lst);
885 nat_entry = bsc_nat_acc_lst_entry_create(nat_lst);
886
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100887 /* test the default value as we are going to overwrite it */
888 OSMO_ASSERT(bsc_entry->cm_reject_cause == GSM48_REJECT_PLMN_NOT_ALLOWED);
889 OSMO_ASSERT(bsc_entry->lu_reject_cause == GSM48_REJECT_PLMN_NOT_ALLOWED);
890
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800891 for (i = 0; i < ARRAY_SIZE(cr_filter); ++i) {
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800892 char *imsi;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800893 msgb_reset(msg);
894 copy_to_msg(msg, cr_filter[i].data, cr_filter[i].length);
895
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100896 bsc_entry->cm_reject_cause = cr_filter[i].bsc_cm_reject_cause;
897 bsc_entry->lu_reject_cause = cr_filter[i].bsc_lu_reject_cause;
898 nat_entry->cm_reject_cause = cr_filter[i].nat_cm_reject_cause;
899 nat_entry->lu_reject_cause = cr_filter[i].nat_lu_reject_cause;
900
Holger Hans Peter Freyther06c9da62011-06-09 21:48:49 +0200901 if (gsm_parse_reg(nat_entry, &nat_entry->imsi_deny_re, &nat_entry->imsi_deny,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800902 cr_filter[i].nat_imsi_deny ? 1 : 0,
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200903 &cr_filter[i].nat_imsi_deny) != 0)
904 abort();
Holger Hans Peter Freyther06c9da62011-06-09 21:48:49 +0200905 if (gsm_parse_reg(bsc_entry, &bsc_entry->imsi_allow_re, &bsc_entry->imsi_allow,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800906 cr_filter[i].bsc_imsi_allow ? 1 : 0,
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200907 &cr_filter[i].bsc_imsi_allow) != 0)
908 abort();
Holger Hans Peter Freyther06c9da62011-06-09 21:48:49 +0200909 if (gsm_parse_reg(bsc_entry, &bsc_entry->imsi_deny_re, &bsc_entry->imsi_deny,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800910 cr_filter[i].bsc_imsi_deny ? 1 : 0,
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200911 &cr_filter[i].bsc_imsi_deny) != 0)
912 abort();
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800913
914 parsed = bsc_nat_parse(msg);
915 if (!parsed) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100916 printf("FAIL: Failed to parse the message\n");
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800917 abort();
918 }
919
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100920 memset(&cause, 0, sizeof(cause));
921 res = bsc_nat_filter_sccp_cr(bsc, msg, parsed, &contype, &imsi, &cause);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800922 if (res != cr_filter[i].result) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100923 printf("FAIL: Wrong result %d for test %d.\n", res, i);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800924 abort();
925 }
926
Holger Hans Peter Freyther415cd2e2014-01-20 09:55:46 +0100927
928 OSMO_ASSERT(cause.cm_reject_cause == cr_filter[i].want_cm_reject_cause);
929 OSMO_ASSERT(cause.lu_reject_cause == cr_filter[i].want_lu_reject_cause);
930
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800931 if (contype != cr_filter[i].contype) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100932 printf("FAIL: Wrong contype %d for test %d.\n", res, contype);
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800933 abort();
934 }
935
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800936 talloc_steal(parsed, imsi);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800937 talloc_free(parsed);
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800938 }
939
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800940 msgb_free(msg);
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +0200941 bsc_nat_free(nat);
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800942}
943
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800944static void test_dt_filter()
945{
946 int i;
947 struct msgb *msg = msgb_alloc(4096, "test_dt_filter");
948 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100949 struct bsc_nat_reject_cause cause;
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800950
951 struct bsc_nat *nat = bsc_nat_alloc();
952 struct bsc_connection *bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freytherc279e392013-04-16 09:53:13 +0200953 struct nat_sccp_connection *con = talloc_zero(0, struct nat_sccp_connection);
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800954
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800955 bsc->cfg = bsc_config_alloc(nat, "foo");
956 bsc_config_add_lac(bsc->cfg, 23);
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800957 con->bsc = bsc;
958
959 msgb_reset(msg);
960 copy_to_msg(msg, id_resp, ARRAY_SIZE(id_resp));
961
962 parsed = bsc_nat_parse(msg);
963 if (!parsed) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100964 printf("FAIL: Could not parse ID resp\n");
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800965 abort();
966 }
967
968 if (parsed->bssap != BSSAP_MSG_DTAP) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100969 printf("FAIL: It should be dtap\n");
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800970 abort();
971 }
972
973 /* gsm_type is actually the size of the dtap */
974 if (parsed->gsm_type < msgb_l3len(msg) - 3) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100975 printf("FAIL: Not enough space for the content\n");
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800976 abort();
977 }
978
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100979 memset(&cause, 0, sizeof(cause));
980 if (bsc_nat_filter_dt(bsc, msg, con, parsed, &cause) != 1) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +0100981 printf("FAIL: Should have passed..\n");
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800982 abort();
983 }
984
985 /* just some basic length checking... */
986 for (i = ARRAY_SIZE(id_resp); i >= 0; --i) {
987 msgb_reset(msg);
988 copy_to_msg(msg, id_resp, ARRAY_SIZE(id_resp));
989
990 parsed = bsc_nat_parse(msg);
991 if (!parsed)
992 continue;
993
994 con->imsi_checked = 0;
Holger Hans Peter Freytherbdf764a2012-12-17 14:35:03 +0100995 memset(&cause, 0, sizeof(cause));
996 bsc_nat_filter_dt(bsc, msg, con, parsed, &cause);
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800997 }
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +0200998
999 msgb_free(msg);
1000 bsc_nat_free(nat);
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +08001001}
1002
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001003static void test_setup_rewrite()
1004{
1005 struct msgb *msg = msgb_alloc(4096, "test_dt_filter");
1006 struct msgb *out;
1007 struct bsc_nat_parsed *parsed;
1008 const char *imsi = "27408000001234";
1009
1010 struct bsc_nat *nat = bsc_nat_alloc();
1011
1012 /* a fake list */
Pablo Neira Ayusoab46cf32011-05-07 13:11:20 +02001013 struct osmo_config_list entries;
1014 struct osmo_config_entry entry;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001015
1016 INIT_LLIST_HEAD(&entries.entry);
1017 entry.mcc = "274";
1018 entry.mnc = "08";
1019 entry.option = "^0([1-9])";
1020 entry.text = "0049";
1021 llist_add_tail(&entry.list, &entries.entry);
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001022 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001023
1024 /* verify that nothing changed */
1025 msgb_reset(msg);
1026 copy_to_msg(msg, cc_setup_international, ARRAY_SIZE(cc_setup_international));
1027 parsed = bsc_nat_parse(msg);
1028 if (!parsed) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001029 printf("FAIL: Could not parse ID resp\n");
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001030 abort();
1031 }
1032
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001033 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001034 if (msg != out) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001035 printf("FAIL: The message should not have been changed\n");
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001036 abort();
1037 }
1038
Holger Hans Peter Freyther50be1a92012-01-10 22:31:39 +01001039 verify_msg(out, cc_setup_international, ARRAY_SIZE(cc_setup_international));
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001040 talloc_free(parsed);
1041
1042 /* verify that something in the message changes */
1043 msgb_reset(msg);
1044 copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national));
1045 parsed = bsc_nat_parse(msg);
1046 if (!parsed) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001047 printf("FAIL: Could not parse ID resp\n");
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001048 abort();
1049 }
1050
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001051 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001052 if (!out) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001053 printf("FAIL: A new message should be created.\n");
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001054 abort();
1055 }
1056
1057 if (msg == out) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001058 printf("FAIL: The message should have changed\n");
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001059 abort();
1060 }
1061
Holger Hans Peter Freyther50be1a92012-01-10 22:31:39 +01001062 verify_msg(out, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched));
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001063 msgb_free(out);
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001064
1065 /* Make sure that a wildcard is matching */
1066 entry.mnc = "*";
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001067 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001068 msg = msgb_alloc(4096, "test_dt_filter");
1069 copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national));
1070 parsed = bsc_nat_parse(msg);
1071 if (!parsed) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001072 printf("FAIL: Could not parse ID resp\n");
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001073 abort();
1074 }
1075
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001076 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001077 if (!out) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001078 printf("FAIL: A new message should be created.\n");
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001079 abort();
1080 }
1081
1082 if (msg == out) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001083 printf("FAIL: The message should have changed\n");
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001084 abort();
1085 }
1086
Holger Hans Peter Freyther50be1a92012-01-10 22:31:39 +01001087 verify_msg(out, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched));
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001088 msgb_free(out);
1089
1090 /* Make sure that a wildcard is matching */
1091 entry.mnc = "09";
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001092 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001093 msg = msgb_alloc(4096, "test_dt_filter");
1094 copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national));
1095 parsed = bsc_nat_parse(msg);
1096 if (!parsed) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001097 printf("FAIL: Could not parse ID resp\n");
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001098 abort();
1099 }
1100
Holger Hans Peter Freytherdf8e6e92011-05-27 14:09:55 +02001101 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001102 if (out != msg) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001103 printf("FAIL: The message should be unchanged.\n");
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001104 abort();
1105 }
1106
Holger Hans Peter Freyther50be1a92012-01-10 22:31:39 +01001107 verify_msg(out, cc_setup_national, ARRAY_SIZE(cc_setup_national));
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +02001108 msgb_free(out);
Holger Hans Peter Freytherdbd94492013-04-02 12:34:11 +02001109
1110 /* Now see what happens to an international number */
1111 entry.mnc = "*";
1112 entry.option = "^\\+[0-9][0-9]([1-9])";
1113 entry.text = "0036";
1114 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
1115 msg = msgb_alloc(4096, "test_dt_filter");
1116 copy_to_msg(msg, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched));
1117 parsed = bsc_nat_parse(msg);
1118 if (!parsed) {
1119 printf("FAIL: Could not parse ID resp %d\n", __LINE__);
1120 abort();
1121 }
1122
1123 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
1124 if (!out) {
1125 printf("FAIL: A new message should be created %d.\n", __LINE__);
1126 abort();
1127 }
1128
1129 if (msg == out) {
1130 printf("FAIL: The message should have changed %d\n", __LINE__);
1131 abort();
1132 }
1133
1134 verify_msg(out, cc_setup_national_patched_patched,
1135 ARRAY_SIZE(cc_setup_national_patched_patched));
1136 msgb_free(out);
1137
1138 /* go from international back to national */
1139 entry.mnc = "*";
1140 entry.option = "^\\+([0-9])";
1141 entry.text = "36";
1142 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
1143 msg = msgb_alloc(4096, "test_dt_filter");
1144 copy_to_msg(msg, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched));
1145 parsed = bsc_nat_parse(msg);
1146 if (!parsed) {
1147 printf("FAIL: Could not parse ID resp %d\n", __LINE__);
1148 abort();
1149 }
1150
1151 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
1152 if (!out) {
1153 printf("FAIL: A new message should be created %d.\n", __LINE__);
1154 abort();
1155 }
1156
1157 if (msg == out) {
1158 printf("FAIL: The message should have changed %d\n", __LINE__);
1159 abort();
1160 }
1161
1162 verify_msg(out, cc_setup_national_again,
1163 ARRAY_SIZE(cc_setup_national_again));
1164 msgb_free(out);
Holger Hans Peter Freyther3615a302013-06-25 12:03:36 +02001165 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, NULL);
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +02001166 bsc_nat_free(nat);
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001167}
1168
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +02001169static void test_setup_rewrite_prefix(void)
1170{
1171 struct msgb *msg = msgb_alloc(4096, "test_dt_filter");
1172 struct msgb *out;
1173 struct bsc_nat_parsed *parsed;
1174 const char *imsi = "27408000001234";
1175
1176 struct bsc_nat *nat = bsc_nat_alloc();
1177
1178 /* a fake list */
1179 struct osmo_config_list entries;
1180 struct osmo_config_entry entry;
1181
1182 INIT_LLIST_HEAD(&entries.entry);
1183 entry.mcc = "274";
1184 entry.mnc = "08";
1185 entry.option = "^0([1-9])";
1186 entry.text = "prefix_lookup";
1187 llist_add_tail(&entry.list, &entries.entry);
1188 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
1189
1190 nat->num_rewr_trie = nat_rewrite_parse(nat, "prefixes.csv");
1191
1192 msgb_reset(msg);
1193 copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national));
1194 parsed = bsc_nat_parse(msg);
1195 if (!parsed) {
1196 printf("FAIL: Could not parse ID resp\n");
1197 abort();
1198 }
1199
1200 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
1201 if (!out) {
1202 printf("FAIL: A new message should be created.\n");
1203 abort();
1204 }
1205
1206 if (msg == out) {
1207 printf("FAIL: The message should have changed\n");
1208 abort();
1209 }
1210
1211 verify_msg(out, cc_setup_national_patched, ARRAY_SIZE(cc_setup_national_patched));
1212 msgb_free(out);
1213
1214 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, NULL);
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +02001215 bsc_nat_free(nat);
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +02001216}
1217
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +02001218static void test_setup_rewrite_post(void)
1219{
1220 struct msgb *msg = msgb_alloc(4096, "test_dt_filter");
1221 struct msgb *out;
1222 struct bsc_nat_parsed *parsed;
1223 const char *imsi = "27408000001234";
1224
1225 struct bsc_nat *nat = bsc_nat_alloc();
1226
1227 /* a fake list */
1228 struct osmo_config_list entries;
1229 struct osmo_config_entry entry;
1230 struct osmo_config_list entries_post;
1231 struct osmo_config_entry entry_post;
1232
1233 INIT_LLIST_HEAD(&entries.entry);
1234 entry.mcc = "274";
1235 entry.mnc = "08";
1236 entry.option = "^0([1-9])";
1237 entry.text = "0049";
1238 llist_add_tail(&entry.list, &entries.entry);
1239 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr, &entries);
1240
1241 /* attempt to undo the previous one */
1242 INIT_LLIST_HEAD(&entries_post.entry);
1243 entry_post.mcc = "274";
1244 entry_post.mnc = "08";
1245 entry_post.option = "^\\+49([1-9])";
1246 entry_post.text = "prefix_lookup";
1247 llist_add_tail(&entry_post.list, &entries_post.entry);
1248 bsc_nat_num_rewr_entry_adapt(nat, &nat->num_rewr_post, &entries_post);
1249
1250 nat->num_rewr_trie = nat_rewrite_parse(nat, "prefixes.csv");
1251
1252 msgb_reset(msg);
1253 copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national));
1254 parsed = bsc_nat_parse(msg);
1255 if (!parsed) {
1256 printf("FAIL: Could not parse ID resp\n");
1257 abort();
1258 }
1259
1260 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
1261 if (!out) {
1262 printf("FAIL: A new message should be created.\n");
1263 abort();
1264 }
1265
1266 if (msg == out) {
1267 printf("FAIL: The message should have changed\n");
1268 abort();
1269 }
1270
1271 verify_msg(out, cc_setup_national, ARRAY_SIZE(cc_setup_national));
1272 msgb_free(out);
1273
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +02001274 bsc_nat_free(nat);
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +02001275}
1276
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001277static void test_sms_smsc_rewrite()
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001278{
1279 struct msgb *msg = msgb_alloc(4096, "SMSC rewrite"), *out;
1280 struct bsc_nat_parsed *parsed;
1281 const char *imsi = "515039900406700";
1282
1283 struct bsc_nat *nat = bsc_nat_alloc();
1284
1285 /* a fake list */
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001286 struct osmo_config_list smsc_entries, dest_entries, clear_entries;
1287 struct osmo_config_entry smsc_entry, dest_entry, clear_entry;
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001288
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +02001289 INIT_LLIST_HEAD(&smsc_entries.entry);
1290 INIT_LLIST_HEAD(&dest_entries.entry);
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001291 INIT_LLIST_HEAD(&clear_entries.entry);
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +02001292 smsc_entry.mcc = "^515039";
1293 smsc_entry.option = "639180000105()";
1294 smsc_entry.text = "6666666666667";
1295 llist_add_tail(&smsc_entry.list, &smsc_entries.entry);
1296 dest_entry.mcc = "515";
1297 dest_entry.mnc = "03";
1298 dest_entry.option = "^0049";
1299 dest_entry.text = "";
1300 llist_add_tail(&dest_entry.list, &dest_entries.entry);
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001301 clear_entry.mcc = "^515039";
1302 clear_entry.option = "^0049";
1303 clear_entry.text = "";
1304 llist_add_tail(&clear_entry.list, &clear_entries.entry);
Holger Hans Peter Freytheracc40312011-05-27 19:21:24 +02001305
1306 bsc_nat_num_rewr_entry_adapt(nat, &nat->smsc_rewr, &smsc_entries);
1307 bsc_nat_num_rewr_entry_adapt(nat, &nat->tpdest_match, &dest_entries);
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001308 bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_clear_tp_srr, &clear_entries);
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001309
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001310 printf("Testing SMSC rewriting.\n");
1311
1312 /*
1313 * Check if the SMSC address is changed
1314 */
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001315 copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite));
1316 parsed = bsc_nat_parse(msg);
1317 if (!parsed) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001318 printf("FAIL: Could not parse SMS\n");
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001319 abort();
1320 }
1321
1322 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
1323 if (out == msg) {
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001324 printf("FAIL: This should have changed.\n");
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001325 abort();
1326 }
1327
Holger Hans Peter Freyther50be1a92012-01-10 22:31:39 +01001328 verify_msg(out, smsc_rewrite_patched, ARRAY_SIZE(smsc_rewrite_patched));
1329 msgb_free(out);
Holger Hans Peter Freyther68368dd2012-01-10 22:39:07 +01001330
1331 /* clear out the filter for SMSC */
1332 printf("Attempting to only rewrite the HDR\n");
1333 bsc_nat_num_rewr_entry_adapt(nat, &nat->smsc_rewr, NULL);
1334 msg = msgb_alloc(4096, "SMSC rewrite");
1335 copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite));
1336 parsed = bsc_nat_parse(msg);
1337 if (!parsed) {
1338 printf("FAIL: Could not parse SMS\n");
1339 abort();
1340 }
1341
1342 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
1343 if (out == msg) {
1344 printf("FAIL: This should have changed.\n");
1345 abort();
1346 }
1347
1348 verify_msg(out, smsc_rewrite_patched_hdr, ARRAY_SIZE(smsc_rewrite_patched_hdr));
1349 msgb_free(out);
1350
1351 /* clear out the next filter */
1352 printf("Attempting to change nothing.\n");
1353 bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_clear_tp_srr, NULL);
1354 msg = msgb_alloc(4096, "SMSC rewrite");
1355 copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite));
1356 parsed = bsc_nat_parse(msg);
1357 if (!parsed) {
1358 printf("FAIL: Could not parse SMS\n");
1359 abort();
1360 }
1361
1362 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
1363 if (out != msg) {
1364 printf("FAIL: This should not have changed.\n");
1365 abort();
1366 }
1367
1368 verify_msg(out, smsc_rewrite, ARRAY_SIZE(smsc_rewrite));
1369 msgb_free(out);
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +02001370 bsc_nat_free(nat);
Holger Hans Peter Freyther9c205712011-05-27 17:14:15 +02001371}
1372
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001373static void test_sms_number_rewrite(void)
1374{
Holger Hans Peter Freyther09db1a42012-03-26 16:21:42 +02001375 struct msgb *msg, *out;
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001376 struct bsc_nat_parsed *parsed;
1377 const char *imsi = "515039900406700";
1378
1379 struct bsc_nat *nat = bsc_nat_alloc();
1380
1381 /* a fake list */
Holger Hans Peter Freyther09db1a42012-03-26 16:21:42 +02001382 struct osmo_config_list num_entries, clear_entries;
1383 struct osmo_config_entry num_entry, clear_entry;
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001384
1385 INIT_LLIST_HEAD(&num_entries.entry);
1386 num_entry.mcc = "^515039";
1387 num_entry.option = "^0049()";
1388 num_entry.text = "0032";
1389 llist_add_tail(&num_entry.list, &num_entries.entry);
1390
1391 bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_num_rewr, &num_entries);
1392
1393 printf("Testing SMS TP-DA rewriting.\n");
1394
1395 /*
1396 * Check if the SMSC address is changed
1397 */
Holger Hans Peter Freyther09db1a42012-03-26 16:21:42 +02001398 msg = msgb_alloc(4096, "SMSC rewrite");
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001399 copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite));
1400 parsed = bsc_nat_parse(msg);
1401 if (!parsed) {
1402 printf("FAIL: Could not parse SMS\n");
1403 abort();
1404 }
1405
1406 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
1407 if (out == msg) {
1408 printf("FAIL: This should have changed.\n");
1409 abort();
1410 }
1411
1412 verify_msg(out, smsc_rewrite_num_patched,
1413 ARRAY_SIZE(smsc_rewrite_num_patched));
1414 msgb_free(out);
Holger Hans Peter Freyther09db1a42012-03-26 16:21:42 +02001415
1416 /*
1417 * Now with TP-SRR rewriting enabled
1418 */
1419 INIT_LLIST_HEAD(&clear_entries.entry);
1420 clear_entry.mcc = "^515039";
1421 clear_entry.option = "";
1422 clear_entry.text = "";
1423 llist_add_tail(&clear_entry.list, &clear_entries.entry);
1424 bsc_nat_num_rewr_entry_adapt(nat, &nat->sms_clear_tp_srr, &clear_entries);
1425
1426 msg = msgb_alloc(4096, "SMSC rewrite");
1427 copy_to_msg(msg, smsc_rewrite, ARRAY_SIZE(smsc_rewrite));
1428 parsed = bsc_nat_parse(msg);
1429 if (!parsed) {
1430 printf("FAIL: Could not parse SMS\n");
1431 abort();
1432 }
1433
1434 out = bsc_nat_rewrite_msg(nat, msg, parsed, imsi);
1435 if (out == msg) {
1436 printf("FAIL: This should have changed.\n");
1437 abort();
1438 }
1439
1440 verify_msg(out, smsc_rewrite_num_patched_tp_srr,
1441 ARRAY_SIZE(smsc_rewrite_num_patched_tp_srr));
1442 msgb_free(out);
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +02001443 bsc_nat_free(nat);
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001444}
1445
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001446static void test_barr_list_parsing(void)
1447{
1448 int rc;
1449 int cm, lu;
1450 struct rb_node *node;
1451 struct rb_root root = RB_ROOT;
1452 struct osmo_config_list *lst = osmo_config_list_parse(NULL, "barr.cfg");
1453 if (lst == NULL)
1454 abort();
1455
1456 rc = bsc_nat_barr_adapt(NULL, &root, lst);
1457 if (rc != 0)
1458 abort();
1459 talloc_free(lst);
1460
1461
1462 for (node = rb_first(&root); node; node = rb_next(node)) {
1463 struct bsc_nat_barr_entry *entry;
1464 entry = rb_entry(node, struct bsc_nat_barr_entry, node);
1465 printf("IMSI: %s CM: %d LU: %d\n", entry->imsi,
1466 entry->cm_reject_cause, entry->lu_reject_cause);
1467 }
1468
1469 /* do the look up now.. */
1470 rc = bsc_nat_barr_find(&root, "12123119", &cm, &lu);
1471 if (!rc) {
1472 printf("Failed to find the IMSI.\n");
1473 abort();
1474 }
1475
1476 if (cm != 3 || lu != 4) {
1477 printf("Found CM(%d) and LU(%d)\n", cm, lu);
1478 abort();
1479 }
1480
1481 /* empty and check that it is empty */
1482 bsc_nat_barr_adapt(NULL, &root, NULL);
1483 if (!RB_EMPTY_ROOT(&root)) {
1484 printf("Failed to empty the list.\n");
1485 abort();
1486 }
1487
1488 /* check that dup results in an error */
1489 lst = osmo_config_list_parse(NULL, "barr_dup.cfg");
1490 if (lst == NULL) {
1491 printf("Failed to parse list with dups\n");
1492 abort();
1493 }
1494
1495 rc = bsc_nat_barr_adapt(NULL, &root, lst);
1496 if (rc != -1) {
1497 printf("It should have failed due dup\n");
1498 abort();
1499 }
1500 talloc_free(lst);
1501
1502 /* dump for reference */
1503 for (node = rb_first(&root); node; node = rb_next(node)) {
1504 struct bsc_nat_barr_entry *entry;
1505 entry = rb_entry(node, struct bsc_nat_barr_entry, node);
1506 printf("IMSI: %s CM: %d LU: %d\n", entry->imsi,
1507 entry->cm_reject_cause, entry->lu_reject_cause);
1508
1509 }
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +02001510 rc = bsc_nat_barr_adapt(NULL, &root, NULL);
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001511}
1512
Holger Hans Peter Freytherb2b291d2013-04-16 13:23:43 +02001513static void test_nat_extract_lac()
1514{
1515 int res;
1516 struct bsc_connection *bsc;
1517 struct bsc_nat *nat;
1518 struct nat_sccp_connection con;
1519 struct bsc_nat_parsed *parsed;
1520 struct msgb *msg = msgb_alloc(4096, "test-message");
1521
1522 printf("Testing LAC extraction from SCCP CR\n");
1523
1524 /* initialize the testcase */
1525 nat = bsc_nat_alloc();
1526 bsc = bsc_connection_alloc(nat);
1527 bsc->cfg = bsc_config_alloc(nat, "foo");
1528
1529 memset(&con, 0, sizeof(con));
1530 con.bsc = bsc;
1531
1532 /* create the SCCP CR */
1533 msg->l2h = msgb_put(msg, ARRAY_SIZE(bssmap_cr));
1534 memcpy(msg->l2h, bssmap_cr, ARRAY_SIZE(bssmap_cr));
1535
1536 /* parse it and pass it on */
1537 parsed = bsc_nat_parse(msg);
1538 res = bsc_nat_extract_lac(bsc, &con, parsed, msg);
1539 OSMO_ASSERT(res == 0);
1540
1541 /* verify the LAC */
1542 OSMO_ASSERT(con.lac == 8210);
1543 OSMO_ASSERT(con.ci == 50000);
Holger Hans Peter Freyther73ec6982014-08-29 09:56:58 +02001544
1545 bsc_nat_free(nat);
Holger Hans Peter Freytherb2b291d2013-04-16 13:23:43 +02001546}
1547
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +08001548int main(int argc, char **argv)
1549{
Holger Hans Peter Freyther30e1ae92010-07-30 02:53:14 +08001550 sccp_set_log_area(DSCCP);
Holger Hans Peter Freyther67cd75f2011-05-12 16:02:07 +02001551 osmo_init_logging(&log_info);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +08001552
1553 test_filter();
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +02001554 test_contrack();
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +02001555 test_paging();
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +02001556 test_mgcp_ass_tracking();
1557 test_mgcp_find();
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +02001558 test_mgcp_rewrite();
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +02001559 test_mgcp_parse();
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +08001560 test_cr_filter();
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +08001561 test_dt_filter();
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +02001562 test_setup_rewrite();
Holger Hans Peter Freytherddf191e2013-06-25 11:44:01 +02001563 test_setup_rewrite_prefix();
Holger Hans Peter Freyther67e423c2013-06-25 15:38:31 +02001564 test_setup_rewrite_post();
Holger Hans Peter Freyther8e60f622012-01-18 20:00:28 +01001565 test_sms_smsc_rewrite();
1566 test_sms_number_rewrite();
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +01001567 test_mgcp_allocations();
Holger Hans Peter Freyther1f8276e2013-01-01 11:25:09 +01001568 test_barr_list_parsing();
Holger Hans Peter Freytherb2b291d2013-04-16 13:23:43 +02001569 test_nat_extract_lac();
Holger Hans Peter Freytherf5ede522012-01-06 13:56:12 +01001570
1571 printf("Testing execution completed.\n");
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001572 return 0;
1573}
Holger Hans Peter Freytherc3271872012-11-05 14:54:56 +01001574
1575/* stub */
1576void bsc_nat_send_mgcp_to_msc(struct bsc_nat *nat, struct msgb *msg)
1577{
1578 abort();
1579}