blob: 8d1bd9b885e16215aa0f54cfec1aff9491c079d2 [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>
5 * (C) 2010 by on-waves.com
6 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25
26#include <openbsc/debug.h>
27#include <openbsc/gsm_data.h>
28#include <openbsc/bsc_nat.h>
29
30#include <stdio.h>
31
32/* test messages for ipa */
33static u_int8_t ipa_id[] = {
34 0x00, 0x01, 0xfe, 0x06,
35};
36
37/* SCCP messages are below */
38static u_int8_t gsm_reset[] = {
39 0x00, 0x12, 0xfd,
40 0x09, 0x00, 0x03, 0x05, 0x07, 0x02, 0x42, 0xfe,
41 0x02, 0x42, 0xfe, 0x06, 0x00, 0x04, 0x30, 0x04,
42 0x01, 0x20,
43};
44
45static const u_int8_t gsm_reset_ack[] = {
46 0x00, 0x13, 0xfd,
47 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
48 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x03,
49 0x00, 0x01, 0x31,
50};
51
52static const u_int8_t gsm_paging[] = {
53 0x00, 0x20, 0xfd,
54 0x09, 0x00, 0x03, 0x07, 0x0b, 0x04, 0x43, 0x01,
55 0x00, 0xfe, 0x04, 0x43, 0x5c, 0x00, 0xfe, 0x10,
56 0x00, 0x0e, 0x52, 0x08, 0x08, 0x29, 0x47, 0x10,
57 0x02, 0x01, 0x31, 0x97, 0x61, 0x1a, 0x01, 0x06,
58};
59
60/* BSC -> MSC connection open */
61static const u_int8_t bssmap_cr[] = {
62 0x00, 0x2c, 0xfd,
63 0x01, 0x01, 0x02, 0x03, 0x02, 0x02, 0x04, 0x02,
64 0x42, 0xfe, 0x0f, 0x1f, 0x00, 0x1d, 0x57, 0x05,
65 0x08, 0x00, 0x72, 0xf4, 0x80, 0x20, 0x12, 0xc3,
66 0x50, 0x17, 0x10, 0x05, 0x24, 0x11, 0x03, 0x33,
67 0x19, 0xa2, 0x08, 0x29, 0x47, 0x10, 0x02, 0x01,
68 0x31, 0x97, 0x61, 0x00
69};
70
71/* MSC -> BSC connection confirm */
72static const u_int8_t bssmap_cc[] = {
73 0x00, 0x0a, 0xfd,
74 0x02, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00,
75};
76
77/* MSC -> BSC released */
78static const u_int8_t bssmap_released[] = {
79 0x00, 0x0e, 0xfd,
80 0x04, 0x00, 0x00, 0x03, 0x01, 0x02, 0x03, 0x00, 0x01, 0x0f,
81 0x02, 0x23, 0x42, 0x00,
82};
83
84/* BSC -> MSC released */
85static const u_int8_t bssmap_release_complete[] = {
86 0x00, 0x07, 0xfd,
87 0x05, 0x01, 0x02, 0x03, 0x00, 0x00, 0x03
88};
89
90struct filter_result {
91 const u_int8_t *data;
92 const u_int16_t length;
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +010093 const int dir;
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +080094 const int result;
95};
96
97static const struct filter_result results[] = {
98 {
99 .data = ipa_id,
100 .length = ARRAY_SIZE(ipa_id),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100101 .dir = DIR_MSC,
102 .result = 1,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800103 },
104 {
105 .data = gsm_reset,
106 .length = ARRAY_SIZE(gsm_reset),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100107 .dir = DIR_MSC,
108 .result = 1,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800109 },
110 {
111 .data = gsm_reset_ack,
112 .length = ARRAY_SIZE(gsm_reset_ack),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100113 .dir = DIR_BSC,
114 .result = 1,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800115 },
116 {
117 .data = gsm_paging,
118 .length = ARRAY_SIZE(gsm_paging),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100119 .dir = DIR_BSC,
120 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800121 },
122 {
123 .data = bssmap_cr,
124 .length = ARRAY_SIZE(bssmap_cr),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100125 .dir = DIR_MSC,
126 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800127 },
128 {
129 .data = bssmap_cc,
130 .length = ARRAY_SIZE(bssmap_cc),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100131 .dir = DIR_BSC,
132 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800133 },
134 {
135 .data = bssmap_released,
136 .length = ARRAY_SIZE(bssmap_released),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100137 .dir = DIR_MSC,
138 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800139 },
140 {
141 .data = bssmap_release_complete,
142 .length = ARRAY_SIZE(bssmap_release_complete),
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100143 .dir = DIR_BSC,
144 .result = 0,
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800145 },
146};
147
148int main(int argc, char **argv)
149{
150 int i;
151
152
153 /* start testinh with proper messages */
154 for (i = 0; i < ARRAY_SIZE(results); ++i) {
155 int result;
156 struct bsc_nat_parsed *parsed;
157 struct msgb *msg = msgb_alloc(4096, "test-message");
158
159 fprintf(stderr, "Going to test item: %d\n", i);
160 memcpy(msg->data, results[i].data, results[i].length);
161 msg->l2h = msgb_put(msg, results[i].length);
162
163 parsed = bsc_nat_parse(msg);
164 if (!parsed) {
165 fprintf(stderr, "FAIL: Failed to parse the message\n");
166 continue;
167 }
168
Holger Hans Peter Freyther1d6fb182010-01-30 11:53:30 +0100169 result = bsc_nat_filter_ipa(results[i].dir, msg, parsed);
Holger Hans Peter Freyther0b8f69d2010-06-15 18:45:38 +0800170 if (result != results[i].result) {
171 fprintf(stderr, "FAIL: Not the expected result got: %d wanted: %d\n",
172 result, results[i].result);
173 }
174
175 msgb_free(msg);
176 }
177
178 return 0;
179}