blob: 6ec22c5015ba7bdf61799066b500bebeb0d7eb06 [file] [log] [blame]
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001/*
2 * BSC NAT Message filtering
3 *
4 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Holger Hans Peter Freytherdf6143a2010-06-15 18:46:56 +08005 * (C) 2010 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 Freyther0b8f69d2010-06-15 18:45:38 +080029
Pablo Neira Ayuso928cb332011-03-26 22:08:53 +010030#include <osmocom/core/talloc.h>
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +080031
Harald Welted5db12c2010-08-03 15:11:51 +020032#include <osmocom/sccp/sccp.h>
Pablo Neira Ayuso928cb332011-03-26 22:08:53 +010033#include <osmocom/gsm/protocol/gsm_08_08.h>
Holger Hans Peter Freyther30e1ae92010-07-30 02:53:14 +080034
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080035#include <stdio.h>
36
37/* test messages for ipa */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080038static uint8_t ipa_id[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080039 0x00, 0x01, 0xfe, 0x06,
40};
41
42/* SCCP messages are below */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080043static uint8_t gsm_reset[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080044 0x00, 0x12, 0xfd,
45 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
46 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
47 0x01, 0x20,
48};
49
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080050static const uint8_t gsm_reset_ack[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080051 0x00, 0x13, 0xfd,
52 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
53 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
54 0x00, 0x01, 0x31,
55};
56
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080057static const uint8_t gsm_paging[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080058 0x00, 0x20, 0xfd,
59 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
60 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x10,
61 0x00, 0x0e, 0x52, 0x08, 0x08, 0x29, 0x47, 0x10,
62 0x02, 0x01, 0x31, 0x97, 0x61, 0x1a, 0x01, 0x06,
63};
64
65/* BSC -> MSC connection open */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080066static const uint8_t bssmap_cr[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080067 0x00, 0x2c, 0xfd,
68 0x01, 0x01, 0x02, 0x03, 0x02, 0x02, 0x04, 0x02,
69 0x42, 0xfe, 0x0f, 0x1f, 0x00, 0x1d, 0x57, 0x05,
70 0x08, 0x00, 0x72, 0xf4, 0x80, 0x20, 0x12, 0xc3,
71 0x50, 0x17, 0x10, 0x05, 0x24, 0x11, 0x03, 0x33,
72 0x19, 0xa2, 0x08, 0x29, 0x47, 0x10, 0x02, 0x01,
73 0x31, 0x97, 0x61, 0x00
74};
75
76/* MSC -> BSC connection confirm */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080077static const uint8_t bssmap_cc[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080078 0x00, 0x0a, 0xfd,
79 0x02, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00,
80};
81
82/* MSC -> BSC released */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080083static const uint8_t bssmap_released[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080084 0x00, 0x0e, 0xfd,
85 0x04, 0x00, 0x00, 0x03, 0x01, 0x02, 0x03, 0x00, 0x01, 0x0f,
86 0x02, 0x23, 0x42, 0x00,
87};
88
89/* BSC -> MSC released */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080090static const uint8_t bssmap_release_complete[] = {
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080091 0x00, 0x07, 0xfd,
92 0x05, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03
93};
94
Holger Hans Peter Freyther0a6f62f2010-04-06 10:22:01 +020095/* both directions IT timer */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +080096static const uint8_t connnection_it[] = {
Holger Hans Peter Freyther0a6f62f2010-04-06 10:22:01 +020097 0x00, 0x0b, 0xfd,
98 0x10, 0x01, 0x02, 0x03, 0x01, 0x02, 0x03,
99 0x00, 0x00, 0x00, 0x00,
100};
101
Holger Hans Peter Freyther5e63f922010-04-21 15:45:26 +0800102/* error in both directions */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800103static const uint8_t proto_error[] = {
Holger Hans Peter Freyther5e63f922010-04-21 15:45:26 +0800104 0x00, 0x05, 0xfd,
105 0x0f, 0x22, 0x33, 0x44, 0x00,
106};
107
Holger Hans Peter Freythera128d912010-04-01 08:47:12 +0200108/* MGCP wrap... */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800109static const uint8_t mgcp_msg[] = {
Holger Hans Peter Freythera128d912010-04-01 08:47:12 +0200110 0x00, 0x03, 0xfc,
111 0x20, 0x20, 0x20,
112};
113
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800114/* location updating request */
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800115static const uint8_t bss_lu[] = {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800116 0x00, 0x2e, 0xfd,
117 0x01, 0x91, 0x45, 0x14, 0x02, 0x02, 0x04, 0x02,
118 0x42, 0xfe, 0x0f, 0x21, 0x00, 0x1f, 0x57, 0x05,
119 0x08, 0x00, 0x72, 0xf4, 0x80, 0x20, 0x14, 0xc3,
120 0x50, 0x17, 0x12, 0x05, 0x08, 0x70, 0x72, 0xf4,
121 0x80, 0xff, 0xfe, 0x30, 0x08, 0x29, 0x44, 0x50,
122 0x12, 0x03, 0x24, 0x01, 0x95, 0x00
123};
124
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800125/* paging response */
126static const uint8_t pag_resp[] = {
127 0x00, 0x2c, 0xfd, 0x01, 0xe5, 0x68,
128 0x14, 0x02, 0x02, 0x04, 0x02, 0x42, 0xfe, 0x0f,
129 0x1f, 0x00, 0x1d, 0x57, 0x05, 0x08, 0x00, 0x72,
130 0xf4, 0x80, 0x20, 0x16, 0xc3, 0x50, 0x17, 0x10,
131 0x06, 0x27, 0x01, 0x03, 0x30, 0x18, 0x96, 0x08,
132 0x29, 0x26, 0x30, 0x32, 0x11, 0x42, 0x01, 0x19,
133 0x00
134};
135
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800136struct filter_result {
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800137 const uint8_t *data;
Holger Hans Peter Freythere2c15202010-07-23 19:09:21 +0800138 const uint16_t length;
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100139 const int dir;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800140 const int result;
141};
142
143static const struct filter_result results[] = {
144 {
145 .data = ipa_id,
146 .length = ARRAY_SIZE(ipa_id),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100147 .dir = DIR_MSC,
148 .result = 1,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800149 },
150 {
151 .data = gsm_reset,
152 .length = ARRAY_SIZE(gsm_reset),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100153 .dir = DIR_MSC,
154 .result = 1,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800155 },
156 {
157 .data = gsm_reset_ack,
158 .length = ARRAY_SIZE(gsm_reset_ack),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100159 .dir = DIR_BSC,
160 .result = 1,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800161 },
162 {
163 .data = gsm_paging,
164 .length = ARRAY_SIZE(gsm_paging),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100165 .dir = DIR_BSC,
166 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800167 },
168 {
169 .data = bssmap_cr,
170 .length = ARRAY_SIZE(bssmap_cr),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100171 .dir = DIR_MSC,
172 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800173 },
174 {
175 .data = bssmap_cc,
176 .length = ARRAY_SIZE(bssmap_cc),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100177 .dir = DIR_BSC,
178 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800179 },
180 {
181 .data = bssmap_released,
182 .length = ARRAY_SIZE(bssmap_released),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100183 .dir = DIR_MSC,
184 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800185 },
186 {
187 .data = bssmap_release_complete,
188 .length = ARRAY_SIZE(bssmap_release_complete),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100189 .dir = DIR_BSC,
190 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800191 },
Holger Hans Peter Freythera128d912010-04-01 08:47:12 +0200192 {
193 .data = mgcp_msg,
194 .length = ARRAY_SIZE(mgcp_msg),
195 .dir = DIR_MSC,
196 .result = 0,
197 },
Holger Hans Peter Freyther0a6f62f2010-04-06 10:22:01 +0200198 {
199 .data = connnection_it,
200 .length = ARRAY_SIZE(connnection_it),
201 .dir = DIR_BSC,
202 .result = 0,
203 },
204 {
205 .data = connnection_it,
206 .length = ARRAY_SIZE(connnection_it),
207 .dir = DIR_MSC,
208 .result = 0,
209 },
Holger Hans Peter Freyther5e63f922010-04-21 15:45:26 +0800210 {
211 .data = proto_error,
212 .length = ARRAY_SIZE(proto_error),
213 .dir = DIR_BSC,
214 .result = 0,
215 },
216 {
217 .data = proto_error,
218 .length = ARRAY_SIZE(proto_error),
219 .dir = DIR_MSC,
220 .result = 0,
221 },
222
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800223};
224
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800225static void test_filter(void)
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800226{
227 int i;
228
229
230 /* start testinh with proper messages */
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800231 fprintf(stderr, "Testing BSS Filtering.\n");
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800232 for (i = 0; i < ARRAY_SIZE(results); ++i) {
233 int result;
234 struct bsc_nat_parsed *parsed;
235 struct msgb *msg = msgb_alloc(4096, "test-message");
236
237 fprintf(stderr, "Going to test item: %d\n", i);
238 memcpy(msg->data, results[i].data, results[i].length);
239 msg->l2h = msgb_put(msg, results[i].length);
240
241 parsed = bsc_nat_parse(msg);
242 if (!parsed) {
243 fprintf(stderr, "FAIL: Failed to parse the message\n");
244 continue;
245 }
246
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100247 result = bsc_nat_filter_ipa(results[i].dir, msg, parsed);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800248 if (result != results[i].result) {
249 fprintf(stderr, "FAIL: Not the expected result got: %d wanted: %d\n",
250 result, results[i].result);
251 }
252
253 msgb_free(msg);
254 }
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800255}
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800256
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800257#include "bsc_data.c"
258
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800259static void copy_to_msg(struct msgb *msg, const uint8_t *data, unsigned int length)
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800260{
261 msgb_reset(msg);
262 msg->l2h = msgb_put(msg, length);
263 memcpy(msg->l2h, data, msgb_l2len(msg));
264}
265
266#define VERIFY(con_found, con, msg, ver, str) \
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800267 if (!con_found || con_found->bsc != con) { \
Holger Hans Peter Freyther72ba1622010-06-15 18:48:44 +0800268 fprintf(stderr, "Failed to find the con: %p\n", con_found); \
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800269 abort(); \
270 } \
271 if (memcmp(msg->data, ver, sizeof(ver)) != 0) { \
272 fprintf(stderr, "Failed to patch the %s msg.\n", str); \
273 abort(); \
274 }
275
276/* test conn tracking once */
277static void test_contrack()
278{
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800279 struct bsc_nat *nat;
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800280 struct bsc_connection *con;
281 struct sccp_connections *con_found;
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +0800282 struct sccp_connections *rc_con;
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800283 struct bsc_nat_parsed *parsed;
284 struct msgb *msg;
285
286 fprintf(stderr, "Testing connection tracking.\n");
287 nat = bsc_nat_alloc();
288 con = bsc_connection_alloc(nat);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800289 con->cfg = bsc_config_alloc(nat, "foo");
290 bsc_config_add_lac(con->cfg, 23);
291 bsc_config_add_lac(con->cfg, 49);
292 bsc_config_add_lac(con->cfg, 42);
293 bsc_config_del_lac(con->cfg, 49);
294 bsc_config_add_lac(con->cfg, 1111);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800295 msg = msgb_alloc(4096, "test");
296
297 /* 1.) create a connection */
298 copy_to_msg(msg, bsc_cr, sizeof(bsc_cr));
299 parsed = bsc_nat_parse(msg);
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800300 con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800301 if (con_found != NULL) {
Holger Hans Peter Freyther72ba1622010-06-15 18:48:44 +0800302 fprintf(stderr, "Con should not exist %p\n", con_found);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800303 abort();
304 }
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +0800305 rc_con = create_sccp_src_ref(con, parsed);
306 if (!rc_con) {
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800307 fprintf(stderr, "Failed to create a ref\n");
308 abort();
309 }
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800310 con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800311 if (!con_found || con_found->bsc != con) {
Holger Hans Peter Freyther72ba1622010-06-15 18:48:44 +0800312 fprintf(stderr, "Failed to find the con: %p\n", con_found);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800313 abort();
314 }
Holger Hans Peter Freytherfa20c942010-05-16 16:51:31 +0800315 if (con_found != rc_con) {
316 fprintf(stderr, "Failed to find the right connection.\n");
317 abort();
318 }
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800319 if (memcmp(msg->data, bsc_cr_patched, sizeof(bsc_cr_patched)) != 0) {
320 fprintf(stderr, "Failed to patch the BSC CR msg.\n");
321 abort();
322 }
323 talloc_free(parsed);
324
325 /* 2.) get the cc */
326 copy_to_msg(msg, msc_cc, sizeof(msc_cc));
327 parsed = bsc_nat_parse(msg);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800328 con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
329 VERIFY(con_found, con, msg, msc_cc_patched, "MSC CC");
330 if (update_sccp_src_ref(con_found, parsed) != 0) {
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800331 fprintf(stderr, "Failed to update the SCCP con.\n");
332 abort();
333 }
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800334
335 /* 3.) send some data */
336 copy_to_msg(msg, bsc_dtap, sizeof(bsc_dtap));
337 parsed = bsc_nat_parse(msg);
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800338 con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800339 VERIFY(con_found, con, msg, bsc_dtap_patched, "BSC DTAP");
340
341 /* 4.) receive some data */
342 copy_to_msg(msg, msc_dtap, sizeof(msc_dtap));
343 parsed = bsc_nat_parse(msg);
344 con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
345 VERIFY(con_found, con, msg, msc_dtap_patched, "MSC DTAP");
346
347 /* 5.) close the connection */
348 copy_to_msg(msg, msc_rlsd, sizeof(msc_rlsd));
349 parsed = bsc_nat_parse(msg);
350 con_found = patch_sccp_src_ref_to_bsc(msg, parsed, nat);
351 VERIFY(con_found, con, msg, msc_rlsd_patched, "MSC RLSD");
352
353 /* 6.) confirm the connection close */
354 copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc));
355 parsed = bsc_nat_parse(msg);
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800356 con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
Holger Hans Peter Freyther49c7fb52010-06-15 18:48:55 +0800357 if (!con_found || con_found->bsc != con) {
Holger Hans Peter Freyther72ba1622010-06-15 18:48:44 +0800358 fprintf(stderr, "Failed to find the con: %p\n", con_found);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800359 abort();
360 }
361 if (memcmp(msg->data, bsc_rlc_patched, sizeof(bsc_rlc_patched)) != 0) {
362 fprintf(stderr, "Failed to patch the BSC CR msg.\n");
363 abort();
364 }
365 remove_sccp_src_ref(con, msg, parsed);
Holger Hans Peter Freyther9d518552010-04-05 21:44:51 +0200366 talloc_free(parsed);
367
368 copy_to_msg(msg, bsc_rlc, sizeof(bsc_rlc));
369 parsed = bsc_nat_parse(msg);
Holger Hans Peter Freytherb5513ca2010-04-21 18:56:12 +0800370 con_found = patch_sccp_src_ref_to_msc(msg, parsed, con);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800371
372 /* verify that it is gone */
373 if (con_found != NULL) {
Holger Hans Peter Freyther72ba1622010-06-15 18:48:44 +0800374 fprintf(stderr, "Con should be gone. %p\n", con_found);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800375 abort();
376 }
377 talloc_free(parsed);
378
379
Holger Hans Peter Freyther9212d9d2011-02-27 11:18:41 +0100380 bsc_config_free(con->cfg);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800381 talloc_free(nat);
382 msgb_free(msg);
383}
384
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200385static void test_paging(void)
386{
387 struct bsc_nat *nat;
388 struct bsc_connection *con;
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800389 struct bsc_config *cfg;
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200390
391 fprintf(stderr, "Testing paging by lac.\n");
392
393 nat = bsc_nat_alloc();
394 con = bsc_connection_alloc(nat);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800395 cfg = bsc_config_alloc(nat, "unknown");
396 con->cfg = cfg;
397 bsc_config_add_lac(cfg, 23);
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200398 con->authenticated = 1;
399 llist_add(&con->list_entry, &nat->bsc_connections);
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200400
401 /* Test it by not finding it */
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200402 if (bsc_config_handles_lac(cfg, 8213) != 0) {
403 fprintf(stderr, "Should not be handled.\n");
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200404 abort();
405 }
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200406
407 /* Test by finding it */
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800408 bsc_config_del_lac(cfg, 23);
409 bsc_config_add_lac(cfg, 8213);
Holger Hans Peter Freyther1ffe98c2011-05-02 16:20:32 +0200410 if (bsc_config_handles_lac(cfg, 8213) == 0) {
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200411 fprintf(stderr, "Should have found it.\n");
412 abort();
413 }
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200414}
415
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100416static void test_mgcp_allocations(void)
417{
418#if 0
419 struct bsc_connection *bsc;
420 struct bsc_nat *nat;
421 struct sccp_connections con;
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100422 int i, j, multiplex;
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100423
424 fprintf(stderr, "Testing MGCP.\n");
425 memset(&con, 0, sizeof(con));
426
427 nat = bsc_nat_alloc();
428 nat->bsc_endpoints = talloc_zero_array(nat,
429 struct bsc_endpoint,
430 65);
431 nat->mgcp_cfg = mgcp_config_alloc();
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100432 nat->mgcp_cfg->trunk.number_endpoints = 64;
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100433
434 bsc = bsc_connection_alloc(nat);
435 bsc->cfg = bsc_config_alloc(nat, "foo");
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100436 bsc->cfg->max_endpoints = 60;
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100437 bsc_config_add_lac(bsc->cfg, 2323);
438 bsc->last_endpoint = 0x22;
439 con.bsc = bsc;
440
441 bsc_init_endps_if_needed(bsc);
442
443 i = 1;
444 do {
445 if (bsc_assign_endpoint(bsc, &con) != 0) {
446 fprintf(stderr, "failed to allocate... on iteration %d\n", i);
447 break;
448 }
449 ++i;
450 } while(1);
451
Holger Hans Peter Freyther9ec030d2011-02-27 11:04:27 +0100452 multiplex = bsc_mgcp_nr_multiplexes(bsc->cfg->max_endpoints);
453 for (i = 0; i < multiplex; ++i) {
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +0100454 for (j = 0; j < 32; ++j)
455 printf("%d", bsc->_endpoint_status[i*32 + j]);
456 printf(": %d of %d\n", i*32 + 32, 32 * 8);
457 }
458#endif
459}
460
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200461static void test_mgcp_ass_tracking(void)
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800462{
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800463 struct bsc_connection *bsc;
464 struct bsc_nat *nat;
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800465 struct sccp_connections con;
466 struct bsc_nat_parsed *parsed;
467 struct msgb *msg;
468
469 fprintf(stderr, "Testing MGCP.\n");
470 memset(&con, 0, sizeof(con));
471
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800472 nat = bsc_nat_alloc();
473 nat->bsc_endpoints = talloc_zero_array(nat,
474 struct bsc_endpoint,
475 33);
Holger Hans Peter Freyther7e0cc502011-02-25 12:43:58 +0100476 nat->mgcp_cfg = mgcp_config_alloc();
Holger Hans Peter Freyther88ad7722011-02-28 00:56:17 +0100477 nat->mgcp_cfg->trunk.number_endpoints = 64;
Holger Hans Peter Freyther7e0cc502011-02-25 12:43:58 +0100478
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800479 bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800480 bsc->cfg = bsc_config_alloc(nat, "foo");
481 bsc_config_add_lac(bsc->cfg, 2323);
Holger Hans Peter Freyther86c1db62011-02-25 17:10:25 +0100482 bsc->last_endpoint = 0x1e;
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800483 con.bsc = bsc;
484
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800485 msg = msgb_alloc(4096, "foo");
486 copy_to_msg(msg, ass_cmd, sizeof(ass_cmd));
487 parsed = bsc_nat_parse(msg);
Holger Hans Peter Freythercd702372010-09-20 01:21:51 +0800488
489 if (msg->l2h[16] != 0 ||
490 msg->l2h[17] != 0x1) {
491 fprintf(stderr, "Input is not as expected.. %s 0x%x\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200492 osmo_hexdump(msg->l2h, msgb_l2len(msg)),
Holger Hans Peter Freythercd702372010-09-20 01:21:51 +0800493 msg->l2h[17]);
494 abort();
495 }
496
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800497 if (bsc_mgcp_assign_patch(&con, msg) != 0) {
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800498 fprintf(stderr, "Failed to handle assignment.\n");
499 abort();
500 }
501
Holger Hans Peter Freythercd702372010-09-20 01:21:51 +0800502 if (con.msc_endp != 1) {
503 fprintf(stderr, "Timeslot should be 1.\n");
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800504 abort();
505 }
506
Holger Hans Peter Freyther86c1db62011-02-25 17:10:25 +0100507 if (con.bsc_endp != 0x1) {
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800508 fprintf(stderr, "Assigned timeslot should have been 1.\n");
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800509 abort();
510 }
Holger Hans Peter Freyther86c1db62011-02-25 17:10:25 +0100511 if (con.bsc->_endpoint_status[0x1] != 1) {
Holger Hans Peter Freyther45fd07d2010-08-28 18:22:14 +0800512 fprintf(stderr, "The status on the BSC is wrong.\n");
513 abort();
514 }
515
Holger Hans Peter Freythercd702372010-09-20 01:21:51 +0800516 int multiplex, timeslot;
Holger Hans Peter Freyther86c1db62011-02-25 17:10:25 +0100517 mgcp_endpoint_to_timeslot(0x1, &multiplex, &timeslot);
Holger Hans Peter Freythercd702372010-09-20 01:21:51 +0800518
519 uint16_t cic = htons(timeslot & 0x1f);
520 if (memcmp(&cic, &msg->l2h[16], sizeof(cic)) != 0) {
521 fprintf(stderr, "Message was not patched properly\n");
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200522 fprintf(stderr, "data cic: 0x%x %s\n", cic, osmo_hexdump(msg->l2h, msgb_l2len(msg)));
Holger Hans Peter Freythercd702372010-09-20 01:21:51 +0800523 abort();
524 }
525
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800526 talloc_free(parsed);
527
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800528 bsc_mgcp_dlcx(&con);
Holger Hans Peter Freytherd2df4ca2010-09-19 20:54:21 +0800529 if (con.bsc_endp != -1 || con.msc_endp != -1 ||
Holger Hans Peter Freyther86c1db62011-02-25 17:10:25 +0100530 con.bsc->_endpoint_status[1] != 0 || con.bsc->last_endpoint != 0x1) {
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800531 fprintf(stderr, "Clearing should remove the mapping.\n");
532 abort();
533 }
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800534
Holger Hans Peter Freyther9212d9d2011-02-27 11:18:41 +0100535 bsc_config_free(bsc->cfg);
Holger Hans Peter Freyther7b7eef62010-04-22 12:08:17 +0800536 talloc_free(nat);
Holger Hans Peter Freyther465313e2010-06-15 18:49:53 +0800537}
538
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200539/* test the code to find a given connection */
540static void test_mgcp_find(void)
541{
542 struct bsc_nat *nat;
543 struct bsc_connection *con;
544 struct sccp_connections *sccp_con;
545
546 fprintf(stderr, "Testing finding of a BSC Connection\n");
547
548 nat = bsc_nat_alloc();
549 con = bsc_connection_alloc(nat);
550 llist_add(&con->list_entry, &nat->bsc_connections);
551
552 sccp_con = talloc_zero(con, struct sccp_connections);
Holger Hans Peter Freytherf4b34392010-08-28 16:08:39 +0800553 sccp_con->msc_endp = 12;
554 sccp_con->bsc_endp = 12;
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200555 sccp_con->bsc = con;
556 llist_add(&sccp_con->list_entry, &nat->sccp_connections);
557
558 if (bsc_mgcp_find_con(nat, 11) != NULL) {
559 fprintf(stderr, "Found the wrong connection.\n");
560 abort();
561 }
562
Holger Hans Peter Freyther08a1b162010-04-18 02:26:16 +0800563 if (bsc_mgcp_find_con(nat, 12) != sccp_con) {
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200564 fprintf(stderr, "Didn't find the connection\n");
565 abort();
566 }
567
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200568 /* free everything */
569 talloc_free(nat);
570}
571
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200572static void test_mgcp_rewrite(void)
573{
574 int i;
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200575 struct msgb *output;
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200576 fprintf(stderr, "Test rewriting MGCP messages.\n");
577
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200578 for (i = 0; i < ARRAY_SIZE(mgcp_messages); ++i) {
579 const char *orig = mgcp_messages[i].orig;
580 const char *patc = mgcp_messages[i].patch;
581 const char *ip = mgcp_messages[i].ip;
582 const int port = mgcp_messages[i].port;
583
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200584 char *input = strdup(orig);
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200585
Holger Hans Peter Freytherf7c86c52010-08-30 13:44:32 +0800586 output = bsc_mgcp_rewrite(input, strlen(input), 0x1e, ip, port);
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200587 if (msgb_l2len(output) != strlen(patc)) {
588 fprintf(stderr, "Wrong sizes for test: %d %d != %d != %d\n", i, msgb_l2len(output), strlen(patc), strlen(orig));
589 fprintf(stderr, "String '%s' vs '%s'\n", (const char *) output->l2h, patc);
590 abort();
591 }
592
593 if (memcmp(output->l2h, patc, msgb_l2len(output)) != 0) {
594 fprintf(stderr, "Broken on %d msg: '%s'\n", i, (const char *) output->l2h);
595 abort();
596 }
597
598 msgb_free(output);
Holger Hans Peter Freyther8d200652010-04-04 18:09:10 +0200599 free(input);
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200600 }
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200601}
602
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200603static void test_mgcp_parse(void)
604{
605 int code, ci;
606 char transaction[60];
607
608 fprintf(stderr, "Test MGCP response parsing.\n");
609
610 if (bsc_mgcp_parse_response(crcx_resp, &code, transaction) != 0) {
611 fprintf(stderr, "Failed to parse CRCX resp.\n");
612 abort();
613 }
614
615 if (code != 200) {
616 fprintf(stderr, "Failed to parse the CODE properly. Got: %d\n", code);
617 abort();
618 }
619
620 if (strcmp(transaction, "23265295") != 0) {
621 fprintf(stderr, "Failed to parse transaction id: '%s'\n", transaction);
622 abort();
623 }
624
625 ci = bsc_mgcp_extract_ci(crcx_resp);
626 if (ci != 1) {
627 fprintf(stderr, "Failed to parse the CI. Got: %d\n", ci);
628 abort();
629 }
630}
631
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800632struct cr_filter {
Holger Hans Peter Freytherdbd16fe2010-07-23 19:08:55 +0800633 const uint8_t *data;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800634 int length;
635 int result;
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800636 int contype;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800637
638 const char *bsc_imsi_allow;
639 const char *bsc_imsi_deny;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800640 const char *nat_imsi_deny;
641};
642
643static struct cr_filter cr_filter[] = {
644 {
645 .data = bssmap_cr,
646 .length = sizeof(bssmap_cr),
Holger Hans Peter Freytherd880f542010-09-15 01:42:07 +0800647 .result = 1,
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800648 .contype = NAT_CON_TYPE_CM_SERV_REQ,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800649 },
650 {
651 .data = bss_lu,
652 .length = sizeof(bss_lu),
Holger Hans Peter Freytherd880f542010-09-15 01:42:07 +0800653 .result = 1,
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800654 .contype = NAT_CON_TYPE_LU,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800655 },
656 {
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800657 .data = pag_resp,
658 .length = sizeof(pag_resp),
Holger Hans Peter Freytherd880f542010-09-15 01:42:07 +0800659 .result = 1,
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800660 .contype = NAT_CON_TYPE_PAG_RESP,
Holger Hans Peter Freytherf1924982010-05-15 23:54:04 +0800661 },
662 {
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800663 /* nat deny is before blank/null BSC */
664 .data = bss_lu,
665 .length = sizeof(bss_lu),
666 .result = -3,
667 .nat_imsi_deny = "[0-9]*",
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800668 .contype = NAT_CON_TYPE_LU,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800669 },
670 {
Holger Hans Peter Freythera0aeaa72010-05-14 23:07:58 +0800671 /* BSC allow is before NAT deny */
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800672 .data = bss_lu,
673 .length = sizeof(bss_lu),
Holger Hans Peter Freytherd880f542010-09-15 01:42:07 +0800674 .result = 1,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800675 .nat_imsi_deny = "[0-9]*",
Holger Hans Peter Freythera0aeaa72010-05-14 23:07:58 +0800676 .bsc_imsi_allow = "2440[0-9]*",
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800677 .contype = NAT_CON_TYPE_LU,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800678 },
679 {
680 /* BSC allow is before NAT deny */
681 .data = bss_lu,
682 .length = sizeof(bss_lu),
Holger Hans Peter Freytherd880f542010-09-15 01:42:07 +0800683 .result = 1,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800684 .bsc_imsi_allow = "[0-9]*",
685 .nat_imsi_deny = "[0-9]*",
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800686 .contype = NAT_CON_TYPE_LU,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800687 },
688 {
689 /* filter as deny is first */
690 .data = bss_lu,
691 .length = sizeof(bss_lu),
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200692 .result = 1,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800693 .bsc_imsi_deny = "[0-9]*",
694 .bsc_imsi_allow = "[0-9]*",
695 .nat_imsi_deny = "[0-9]*",
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800696 .contype = NAT_CON_TYPE_LU,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800697 },
Holger Hans Peter Freyther1fd60632010-10-19 20:55:33 +0200698 {
699 /* deny by nat rule */
700 .data = bss_lu,
701 .length = sizeof(bss_lu),
702 .result = -3,
703 .bsc_imsi_deny = "000[0-9]*",
704 .nat_imsi_deny = "[0-9]*",
705 .contype = NAT_CON_TYPE_LU,
706 },
707 {
708 /* deny by bsc rule */
709 .data = bss_lu,
710 .length = sizeof(bss_lu),
711 .result = -2,
712 .bsc_imsi_deny = "[0-9]*",
713 .contype = NAT_CON_TYPE_LU,
714 },
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800715
716};
717
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800718static void test_cr_filter()
719{
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800720 int i, res, contype;
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800721 struct msgb *msg = msgb_alloc(4096, "test_cr_filter");
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800722 struct bsc_nat_parsed *parsed;
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800723 struct bsc_nat_acc_lst *nat_lst, *bsc_lst;
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800724 struct bsc_nat_acc_lst_entry *nat_entry, *bsc_entry;
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800725
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800726 struct bsc_nat *nat = bsc_nat_alloc();
727 struct bsc_connection *bsc = bsc_connection_alloc(nat);
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800728 bsc->cfg = bsc_config_alloc(nat, "foo");
729 bsc_config_add_lac(bsc->cfg, 1234);
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800730 bsc->cfg->acc_lst_name = "bsc";
731 nat->acc_lst_name = "nat";
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800732
Holger Hans Peter Freytherd77c8172010-06-08 10:53:39 +0800733 nat_lst = bsc_nat_acc_lst_get(nat, "nat");
734 bsc_lst = bsc_nat_acc_lst_get(nat, "bsc");
735
736 bsc_entry = bsc_nat_acc_lst_entry_create(bsc_lst);
737 nat_entry = bsc_nat_acc_lst_entry_create(nat_lst);
738
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800739 for (i = 0; i < ARRAY_SIZE(cr_filter); ++i) {
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800740 char *imsi;
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800741 msgb_reset(msg);
742 copy_to_msg(msg, cr_filter[i].data, cr_filter[i].length);
743
Holger Hans Peter Freyther29c67032010-06-08 10:14:44 +0800744 nat_lst = bsc_nat_acc_lst_get(nat, "nat");
745 bsc_lst = bsc_nat_acc_lst_get(nat, "bsc");
Holger Hans Peter Freyther8affef52010-06-01 01:03:13 +0800746
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200747 if (bsc_parse_reg(nat_entry, &nat_entry->imsi_deny_re, &nat_entry->imsi_deny,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800748 cr_filter[i].nat_imsi_deny ? 1 : 0,
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200749 &cr_filter[i].nat_imsi_deny) != 0)
750 abort();
751 if (bsc_parse_reg(bsc_entry, &bsc_entry->imsi_allow_re, &bsc_entry->imsi_allow,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800752 cr_filter[i].bsc_imsi_allow ? 1 : 0,
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200753 &cr_filter[i].bsc_imsi_allow) != 0)
754 abort();
755 if (bsc_parse_reg(bsc_entry, &bsc_entry->imsi_deny_re, &bsc_entry->imsi_deny,
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800756 cr_filter[i].bsc_imsi_deny ? 1 : 0,
Holger Hans Peter Freyther4c9557e2011-04-04 19:19:26 +0200757 &cr_filter[i].bsc_imsi_deny) != 0)
758 abort();
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800759
760 parsed = bsc_nat_parse(msg);
761 if (!parsed) {
762 fprintf(stderr, "FAIL: Failed to parse the message\n");
763 abort();
764 }
765
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800766 res = bsc_nat_filter_sccp_cr(bsc, msg, parsed, &contype, &imsi);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800767 if (res != cr_filter[i].result) {
768 fprintf(stderr, "FAIL: Wrong result %d for test %d.\n", res, i);
769 abort();
770 }
771
Holger Hans Peter Freyther19c0a842010-05-16 02:00:40 +0800772 if (contype != cr_filter[i].contype) {
773 fprintf(stderr, "FAIL: Wrong contype %d for test %d.\n", res, contype);
774 abort();
775 }
776
Holger Hans Peter Freyther749497e2010-09-29 01:19:42 +0800777 talloc_steal(parsed, imsi);
Holger Hans Peter Freyther34a96ae2010-05-14 19:49:35 +0800778 talloc_free(parsed);
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800779 }
780
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800781 msgb_free(msg);
782}
783
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800784static void test_dt_filter()
785{
786 int i;
787 struct msgb *msg = msgb_alloc(4096, "test_dt_filter");
788 struct bsc_nat_parsed *parsed;
789
790 struct bsc_nat *nat = bsc_nat_alloc();
791 struct bsc_connection *bsc = bsc_connection_alloc(nat);
792 struct sccp_connections *con = talloc_zero(0, struct sccp_connections);
793
Holger Hans Peter Freyther0bd60f32010-10-08 22:08:29 +0800794 bsc->cfg = bsc_config_alloc(nat, "foo");
795 bsc_config_add_lac(bsc->cfg, 23);
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800796 con->bsc = bsc;
797
798 msgb_reset(msg);
799 copy_to_msg(msg, id_resp, ARRAY_SIZE(id_resp));
800
801 parsed = bsc_nat_parse(msg);
802 if (!parsed) {
803 fprintf(stderr, "FAIL: Could not parse ID resp\n");
804 abort();
805 }
806
807 if (parsed->bssap != BSSAP_MSG_DTAP) {
808 fprintf(stderr, "FAIL: It should be dtap\n");
809 abort();
810 }
811
812 /* gsm_type is actually the size of the dtap */
813 if (parsed->gsm_type < msgb_l3len(msg) - 3) {
814 fprintf(stderr, "FAIL: Not enough space for the content\n");
815 abort();
816 }
817
818 if (bsc_nat_filter_dt(bsc, msg, con, parsed) != 1) {
819 fprintf(stderr, "FAIL: Should have passed..\n");
820 abort();
821 }
822
823 /* just some basic length checking... */
824 for (i = ARRAY_SIZE(id_resp); i >= 0; --i) {
825 msgb_reset(msg);
826 copy_to_msg(msg, id_resp, ARRAY_SIZE(id_resp));
827
828 parsed = bsc_nat_parse(msg);
829 if (!parsed)
830 continue;
831
832 con->imsi_checked = 0;
833 bsc_nat_filter_dt(bsc, msg, con, parsed);
834 }
835}
836
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200837static void test_setup_rewrite()
838{
839 struct msgb *msg = msgb_alloc(4096, "test_dt_filter");
840 struct msgb *out;
841 struct bsc_nat_parsed *parsed;
842 const char *imsi = "27408000001234";
843
844 struct bsc_nat *nat = bsc_nat_alloc();
845
846 /* a fake list */
Pablo Neira Ayusoab46cf32011-05-07 13:11:20 +0200847 struct osmo_config_list entries;
848 struct osmo_config_entry entry;
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200849
850 INIT_LLIST_HEAD(&entries.entry);
851 entry.mcc = "274";
852 entry.mnc = "08";
853 entry.option = "^0([1-9])";
854 entry.text = "0049";
855 llist_add_tail(&entry.list, &entries.entry);
856 nat->num_rewr = &entries;
857
858 /* verify that nothing changed */
859 msgb_reset(msg);
860 copy_to_msg(msg, cc_setup_international, ARRAY_SIZE(cc_setup_international));
861 parsed = bsc_nat_parse(msg);
862 if (!parsed) {
863 fprintf(stderr, "FAIL: Could not parse ID resp\n");
864 abort();
865 }
866
867 out = bsc_nat_rewrite_setup(nat, msg, parsed, imsi);
868 if (msg != out) {
869 fprintf(stderr, "FAIL: The message should not have been changed\n");
870 abort();
871 }
872
873 if (out->len != ARRAY_SIZE(cc_setup_international)) {
874 fprintf(stderr, "FAIL: Length of message changed\n");
875 abort();
876 }
877
878 if (memcmp(out->data, cc_setup_international, out->len) != 0) {
879 fprintf(stderr, "FAIL: Content modified..\n");
880 abort();
881 }
882 talloc_free(parsed);
883
884 /* verify that something in the message changes */
885 msgb_reset(msg);
886 copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national));
887 parsed = bsc_nat_parse(msg);
888 if (!parsed) {
889 fprintf(stderr, "FAIL: Could not parse ID resp\n");
890 abort();
891 }
892
893 out = bsc_nat_rewrite_setup(nat, msg, parsed, imsi);
894 if (!out) {
895 fprintf(stderr, "FAIL: A new message should be created.\n");
896 abort();
897 }
898
899 if (msg == out) {
900 fprintf(stderr, "FAIL: The message should have changed\n");
901 abort();
902 }
903
904 if (out->len != ARRAY_SIZE(cc_setup_national_patched)) {
905 fprintf(stderr, "FAIL: Length is wrong.\n");
906 abort();
907 }
908
909 if (memcmp(cc_setup_national_patched, out->data, out->len) != 0) {
910 fprintf(stderr, "FAIL: Data is wrong.\n");
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200911 fprintf(stderr, "Data was: %s\n", osmo_hexdump(out->data, out->len));
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200912 abort();
913 }
914
915 msgb_free(out);
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +0200916
917 /* Make sure that a wildcard is matching */
918 entry.mnc = "*";
919 msg = msgb_alloc(4096, "test_dt_filter");
920 copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national));
921 parsed = bsc_nat_parse(msg);
922 if (!parsed) {
923 fprintf(stderr, "FAIL: Could not parse ID resp\n");
924 abort();
925 }
926
927 out = bsc_nat_rewrite_setup(nat, msg, parsed, imsi);
928 if (!out) {
929 fprintf(stderr, "FAIL: A new message should be created.\n");
930 abort();
931 }
932
933 if (msg == out) {
934 fprintf(stderr, "FAIL: The message should have changed\n");
935 abort();
936 }
937
938 if (out->len != ARRAY_SIZE(cc_setup_national_patched)) {
939 fprintf(stderr, "FAIL: Length is wrong.\n");
940 abort();
941 }
942
943 if (memcmp(cc_setup_national_patched, out->data, out->len) != 0) {
944 fprintf(stderr, "FAIL: Data is wrong.\n");
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200945 fprintf(stderr, "Data was: %s\n", osmo_hexdump(out->data, out->len));
Holger Hans Peter Freyther74779c62010-10-28 15:27:00 +0200946 abort();
947 }
948
949 msgb_free(out);
950
951 /* Make sure that a wildcard is matching */
952 entry.mnc = "09";
953 msg = msgb_alloc(4096, "test_dt_filter");
954 copy_to_msg(msg, cc_setup_national, ARRAY_SIZE(cc_setup_national));
955 parsed = bsc_nat_parse(msg);
956 if (!parsed) {
957 fprintf(stderr, "FAIL: Could not parse ID resp\n");
958 abort();
959 }
960
961 out = bsc_nat_rewrite_setup(nat, msg, parsed, imsi);
962 if (out != msg) {
963 fprintf(stderr, "FAIL: The message should be unchanged.\n");
964 abort();
965 }
966
967 if (out->len != ARRAY_SIZE(cc_setup_national)) {
968 fprintf(stderr, "FAIL: Foo\n");
969 abort();
970 }
971
972 if (memcmp(out->data, cc_setup_national, ARRAY_SIZE(cc_setup_national)) != 0) {
973 fprintf(stderr, "FAIL: The message should really be unchanged.\n");
974 abort();
975 }
976
977 msgb_free(out);
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200978}
979
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800980int main(int argc, char **argv)
981{
Holger Hans Peter Freyther434a1fd2010-06-15 20:18:40 +0800982 struct log_target *stderr_target;
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800983
Holger Hans Peter Freyther30e1ae92010-07-30 02:53:14 +0800984 sccp_set_log_area(DSCCP);
Holger Hans Peter Freytherf0945402010-07-05 13:29:42 +0800985 log_init(&log_info);
Holger Hans Peter Freyther434a1fd2010-06-15 20:18:40 +0800986 stderr_target = log_target_create_stderr();
987 log_add_target(stderr_target);
988 log_set_all_filter(stderr_target, 1);
Holger Hans Peter Freyther38f7c752010-06-15 18:48:36 +0800989
990 test_filter();
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200991 test_contrack();
Holger Hans Peter Freyther04fd9922010-03-30 06:51:53 +0200992 test_paging();
Holger Hans Peter Freytherfc9bd232010-04-01 03:55:27 +0200993 test_mgcp_ass_tracking();
994 test_mgcp_find();
Holger Hans Peter Freyther76c83542010-04-01 06:48:52 +0200995 test_mgcp_rewrite();
Holger Hans Peter Freyther3c3bce12010-04-01 10:16:28 +0200996 test_mgcp_parse();
Holger Hans Peter Freyther0c08db12010-05-14 08:12:57 +0800997 test_cr_filter();
Holger Hans Peter Freyther32685402010-09-15 05:20:40 +0800998 test_dt_filter();
Holger Hans Peter Freyther73bbf892010-10-21 14:46:57 +0200999 test_setup_rewrite();
Holger Hans Peter Freythera9e93312011-02-26 11:38:00 +01001000 test_mgcp_allocations();
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +08001001 return 0;
1002}