blob: 282f2515bad6f1d95b2eeabfae2eecaadbced2a7 [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;
93 const int result;
94};
95
96static const struct filter_result results[] = {
97 {
98 .data = ipa_id,
99 .length = ARRAY_SIZE(ipa_id),
100 .result = FILTER_TO_MSC,
101 },
102 {
103 .data = gsm_reset,
104 .length = ARRAY_SIZE(gsm_reset),
105 .result = FILTER_TO_MSC,
106 },
107 {
108 .data = gsm_reset_ack,
109 .length = ARRAY_SIZE(gsm_reset_ack),
110 .result = FILTER_TO_BSC,
111 },
112 {
113 .data = gsm_paging,
114 .length = ARRAY_SIZE(gsm_paging),
115 .result = FILTER_NONE,
116 },
117 {
118 .data = bssmap_cr,
119 .length = ARRAY_SIZE(bssmap_cr),
120 .result = FILTER_NONE,
121 },
122 {
123 .data = bssmap_cc,
124 .length = ARRAY_SIZE(bssmap_cc),
125 .result = FILTER_NONE,
126 },
127 {
128 .data = bssmap_released,
129 .length = ARRAY_SIZE(bssmap_released),
130 .result = FILTER_NONE,
131 },
132 {
133 .data = bssmap_release_complete,
134 .length = ARRAY_SIZE(bssmap_release_complete),
135 .result = FILTER_NONE,
136 },
137};
138
139int main(int argc, char **argv)
140{
141 int i;
142
143
144 /* start testinh with proper messages */
145 for (i = 0; i < ARRAY_SIZE(results); ++i) {
146 int result;
147 struct bsc_nat_parsed *parsed;
148 struct msgb *msg = msgb_alloc(4096, "test-message");
149
150 fprintf(stderr, "Going to test item: %d\n", i);
151 memcpy(msg->data, results[i].data, results[i].length);
152 msg->l2h = msgb_put(msg, results[i].length);
153
154 parsed = bsc_nat_parse(msg);
155 if (!parsed) {
156 fprintf(stderr, "FAIL: Failed to parse the message\n");
157 continue;
158 }
159
160 result = bsc_nat_filter_ipa(msg, parsed);
161 if (result != results[i].result) {
162 fprintf(stderr, "FAIL: Not the expected result got: %d wanted: %d\n",
163 result, results[i].result);
164 }
165
166 msgb_free(msg);
167 }
168
169 return 0;
170}