-
Notifications
You must be signed in to change notification settings - Fork 2
/
l2tp_test.c
305 lines (274 loc) · 8.14 KB
/
l2tp_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*****************************************************************************
* Copyright (C) 2004,2005,2006,2007,2008 Katalix Systems Ltd
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*****************************************************************************/
/*
* Test code
*/
#include "usl.h"
#include "l2tp_private.h"
#include "l2tp_rpc.h"
#ifdef L2TP_TEST
extern int l2tp_xprt_test(uint16_t tunnel_id);
struct l2tp_test_data {
struct {
int fake_rx_drop:1;
int fake_tx_drop:1;
int hold_tunnels:1;
int hold_sessions:1;
int no_random_ids:1;
int show_profile_usage:1;
} flags;
enum l2tp_api_test_trigger_type fake_trigger_type;
int fake_trigger_fired;
int num_rx_drops;
int num_tx_drops;
int alloc_tunnel_id;
int alloc_session_id;
int tunnel_id;
int session_id;
int num_tunnel_id_hash_hits;
int num_tunnel_id_hash_misses;
int num_tunnel_name_hash_hits;
int num_tunnel_name_hash_misses;
int num_session_id_hash_hits;
int num_session_id_hash_misses;
};
static struct l2tp_test_data l2tp_test_data = { { 0, } };
static int l2tp_test_is_faked_this_time(void)
{
int result = FALSE;
uint32_t rand_val;
static int contiguous_count = 0;
switch (l2tp_test_data.fake_trigger_type) {
case L2TP_API_TEST_FAKE_TRIGGER_OFF:
break;
case L2TP_API_TEST_FAKE_TRIGGER_ON:
result = TRUE;
break;
case L2TP_API_TEST_FAKE_TRIGGER_ONCE:
result = l2tp_test_data.fake_trigger_fired ? FALSE : TRUE;
break;
case L2TP_API_TEST_FAKE_TRIGGER_LOW: /* 1% */
l2tp_make_random_vector(&rand_val, sizeof(rand_val));
if (rand_val < 0x28f5c28) {
result = TRUE;
}
break;
case L2TP_API_TEST_FAKE_TRIGGER_MEDIUM: /* 5% */
l2tp_make_random_vector(&rand_val, sizeof(rand_val));
if (rand_val < 0xccccccc) {
result = TRUE;
}
break;
case L2TP_API_TEST_FAKE_TRIGGER_HIGH: /* 20% */
l2tp_make_random_vector(&rand_val, sizeof(rand_val));
if (rand_val < 0x33333333) {
result = TRUE;
}
break;
}
if (result) {
l2tp_test_data.fake_trigger_fired = TRUE;
contiguous_count++;
/* Max 4 contiguous errors */
if (l2tp_test_data.fake_trigger_type != L2TP_API_TEST_FAKE_TRIGGER_ON) {
if (contiguous_count > 4) {
result = FALSE;
contiguous_count = 0;
}
}
} else {
contiguous_count = 0;
}
return result;
}
int l2tp_test_is_fake_rx_drop(uint16_t tunnel_id, uint16_t session_id)
{
/* If a tunnel_id/session_id have been specified for the test, check for match */
if (((l2tp_test_data.tunnel_id != 0) && (l2tp_test_data.tunnel_id != tunnel_id)) ||
((l2tp_test_data.session_id != 0) && (l2tp_test_data.session_id != session_id))) {
return FALSE;
}
if (l2tp_test_data.flags.fake_rx_drop && l2tp_test_is_faked_this_time()) {
l2tp_test_data.num_rx_drops++;
return TRUE;
}
return FALSE;
}
int l2tp_test_is_fake_tx_drop(uint16_t tunnel_id, uint16_t session_id)
{
if (l2tp_test_data.flags.fake_tx_drop && l2tp_test_is_faked_this_time()) {
l2tp_test_data.num_tx_drops++;
return TRUE;
}
return FALSE;
}
int l2tp_test_is_hold_tunnels(void)
{
if (l2tp_test_data.flags.hold_tunnels) {
return TRUE;
}
return FALSE;
}
int l2tp_test_is_hold_sessions(void)
{
if (l2tp_test_data.flags.hold_sessions) {
return TRUE;
}
return FALSE;
}
int l2tp_test_is_no_random_ids(void)
{
if (l2tp_test_data.flags.no_random_ids) {
return TRUE;
}
return FALSE;
}
int l2tp_test_is_show_profile_usage(void)
{
if (l2tp_test_data.flags.show_profile_usage) {
return TRUE;
}
return FALSE;
}
uint16_t l2tp_test_alloc_tunnel_id(void)
{
return ++l2tp_test_data.alloc_tunnel_id;
}
uint16_t l2tp_test_alloc_session_id(void)
{
return ++l2tp_test_data.alloc_session_id;
}
void l2tp_test_tunnel_id_hash_inc_stats(int hit)
{
if (hit) {
l2tp_test_data.num_tunnel_id_hash_hits++;
} else {
l2tp_test_data.num_tunnel_id_hash_misses++;
}
}
void l2tp_test_tunnel_name_hash_inc_stats(int hit)
{
if (hit) {
l2tp_test_data.num_tunnel_name_hash_hits++;
} else {
l2tp_test_data.num_tunnel_name_hash_misses++;
}
}
void l2tp_test_session_id_hash_inc_stats(int hit)
{
if (hit) {
l2tp_test_data.num_session_id_hash_hits++;
} else {
l2tp_test_data.num_session_id_hash_misses++;
}
}
#endif /* L2TP_TEST */
bool_t l2tp_test_modify_1_svc(l2tp_api_test_msg_data msg, int *result, struct svc_req *req)
{
#ifdef L2TP_TEST
*result = 0;
if (msg.flags & L2TP_API_TEST_FLAG_DEFAULT_CONFIG) {
l2tp_restore_default_config();
}
if (msg.flags & L2TP_API_TEST_FLAG_FAKE_RX_DROP) {
l2tp_test_data.flags.fake_rx_drop = msg.fake_rx_drop ? -1 : 0;
}
if (msg.flags & L2TP_API_TEST_FLAG_FAKE_TX_DROP) {
l2tp_test_data.flags.fake_tx_drop = msg.fake_tx_drop ? -1 : 0;
}
if (msg.flags & L2TP_API_TEST_FLAG_FAKE_TRIGGER_TYPE) {
l2tp_test_data.fake_trigger_type = msg.fake_trigger_type;
}
if (msg.flags & L2TP_API_TEST_FLAG_CLEAR_FAKE_TRIGGER) {
l2tp_test_data.fake_trigger_fired = FALSE;
}
if (msg.flags & L2TP_API_TEST_FLAG_HOLD_TUNNELS) {
l2tp_test_data.flags.hold_tunnels = msg.hold_tunnels ? -1 : 0;
}
if (msg.flags & L2TP_API_TEST_FLAG_HOLD_SESSIONS) {
l2tp_test_data.flags.hold_sessions = msg.hold_sessions ? -1 : 0;
}
if (msg.flags & L2TP_API_TEST_FLAG_NO_RANDOM_IDS) {
l2tp_test_data.flags.no_random_ids = msg.no_random_ids ? -1 : 0;
}
if (msg.flags & L2TP_API_TEST_FLAG_RESET_IDS) {
l2tp_test_data.alloc_tunnel_id = 0;
l2tp_test_data.alloc_session_id = 0;
}
if (msg.flags & L2TP_API_TEST_FLAG_SHOW_PROFILE_USAGE) {
l2tp_test_data.flags.show_profile_usage = msg.show_profile_usage ? -1 : 0;
}
if (msg.flags & L2TP_API_TEST_FLAG_DO_TRANSPORT_TEST) {
if (msg.flags & L2TP_API_TEST_FLAG_TUNNEL_ID) {
*result = l2tp_xprt_test(msg.tunnel_id);
} else {
*result = -EINVAL;
}
goto out;
}
if (msg.flags & L2TP_API_TEST_FLAG_TUNNEL_ID) {
l2tp_test_data.tunnel_id = msg.tunnel_id;
}
if (msg.flags & L2TP_API_TEST_FLAG_SESSION_ID) {
l2tp_test_data.session_id = msg.session_id;
}
out:
return TRUE;
#else /* L2TP_TEST */
*result = -EOPNOTSUPP;
return FALSE;
#endif /* L2TP_TEST */
}
bool_t l2tp_test_get_1_svc(l2tp_api_test_msg_data *result, struct svc_req *req)
{
#ifdef L2TP_TEST
result->fake_rx_drop = l2tp_test_data.flags.fake_rx_drop;
result->fake_tx_drop = l2tp_test_data.flags.fake_tx_drop;
result->fake_trigger_fired = l2tp_test_data.fake_trigger_fired;
result->fake_trigger_type = l2tp_test_data.fake_trigger_type;
result->num_rx_drops = l2tp_test_data.num_rx_drops;
result->num_tx_drops = l2tp_test_data.num_tx_drops;
result->hold_tunnels = l2tp_test_data.flags.hold_tunnels;
result->hold_sessions = l2tp_test_data.flags.hold_sessions;
result->no_random_ids = l2tp_test_data.flags.no_random_ids;
result->show_profile_usage = l2tp_test_data.flags.show_profile_usage;
result->tunnel_id = l2tp_test_data.tunnel_id;
result->session_id = l2tp_test_data.session_id;
result->num_tunnel_id_hash_hits = l2tp_test_data.num_tunnel_id_hash_hits;
result->num_tunnel_id_hash_misses = l2tp_test_data.num_tunnel_id_hash_misses;
result->num_tunnel_name_hash_hits = l2tp_test_data.num_tunnel_name_hash_hits;
result->num_tunnel_name_hash_misses = l2tp_test_data.num_tunnel_name_hash_misses;
result->num_session_id_hash_hits = l2tp_test_data.num_session_id_hash_hits;
result->num_session_id_hash_misses = l2tp_test_data.num_session_id_hash_misses;
result->flags = 0;
return TRUE;
#else /* L2TP_TEST */
return FALSE;
#endif /* L2TP_TEST */
}
bool_t l2tp_test_log_1_svc(char *message, int *result, struct svc_req *req)
{
#ifdef L2TP_TEST
l2tp_log(LOG_INFO, "APP: %s", message);
*result = 0;
return TRUE;
#else /* L2TP_TEST */
return FALSE;
#endif /* L2TP_TEST */
}