OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / user / openvpn / gremlin.c
1 /*
2  *  OpenVPN -- An application to securely tunnel IP networks
3  *             over a single UDP port, with support for SSL/TLS-based
4  *             session authentication and key exchange,
5  *             packet encryption, packet authentication, and
6  *             packet compression.
7  *
8  *  Copyright (C) 2002-2009 OpenVPN Technologies, Inc. <sales@openvpn.net>
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License version 2
12  *  as published by the Free Software Foundation.
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
20  *  along with this program (see the file COPYING included with this
21  *  distribution); if not, write to the Free Software Foundation, Inc.,
22  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 /*
26  * Test protocol robustness by simulating dropped packets and
27  * network outages when the --gremlin option is used.
28  */
29
30 #include "syshead.h"
31
32 #ifdef ENABLE_DEBUG
33
34 #include "error.h"
35 #include "common.h"
36 #include "misc.h"
37 #include "otime.h"
38 #include "gremlin.h"
39
40 #include "memdbg.h"
41
42 /*
43  * Parameters for packet corruption and droppage.
44  * Each parameter has 4 possible levels, 0 = disabled,
45  * while 1, 2, and 3 are enumerated in the below arrays.
46  * The parameter is a 2-bit field within the --gremlin
47  * parameter.
48  */
49
50 /*
51  * Probability that we will drop a packet is 1 / n
52  */
53 static const int drop_freq[] = { 500, 100, 50 };
54
55 /*
56  * Probability that we will corrupt a packet is 1 / n
57  */
58 static const int corrupt_freq[] = { 500, 100, 50 };
59
60 /*
61  * When network goes up, it will be up for between
62  * UP_LOW and UP_HIGH seconds.
63  */
64 static const int up_low[] =  {  60, 10,  5 };
65 static const int up_high[] = { 600, 60, 10 };
66
67 /*
68  * When network goes down, it will be down for between
69  * DOWN_LOW and DOWN_HIGH seconds.
70  */
71 static const int down_low[] =  {  5, 10,  10 };
72 static const int down_high[] = { 10, 60, 120 };
73
74 /*
75  * Packet flood levels:
76  *  { number of packets, packet size }
77  */
78 static const struct packet_flood_parms packet_flood_data[] =
79   {{10, 100}, {10, 1500}, {100, 1500}};
80
81 struct packet_flood_parms
82 get_packet_flood_parms (int level)
83 {
84   ASSERT (level > 0 && level < 4);
85   return packet_flood_data [level - 1];
86 }
87
88 /*
89  * Return true with probability 1/n
90  */
91 static bool flip(int n) {
92   return (get_random() % n) == 0;
93 }
94
95 /*
96  * Return uniformly distributed random number between
97  * low and high.
98  */
99 static int roll(int low, int high) {
100   int ret;
101   ASSERT (low <= high);
102   ret = low + (get_random() % (high - low + 1));
103   ASSERT (ret >= low && ret <= high);
104   return ret;
105 }
106
107 static bool initialized; /* GLOBAL */
108 static bool up;          /* GLOBAL */
109 static time_t next;      /* GLOBAL */
110
111 /*
112  * Return false if we should drop a packet.
113  */
114 bool
115 ask_gremlin (int flags)
116 {
117   const int up_down_level = GREMLIN_UP_DOWN_LEVEL (flags);
118   const int drop_level = GREMLIN_DROP_LEVEL (flags);
119
120   if (!initialized)
121     {
122       initialized = true;
123
124       if (up_down_level)
125         up = false;
126       else
127         up = true;
128
129       next = now;
130     }
131
132   if (up_down_level) /* change up/down state? */
133     {
134       if (now >= next)
135         {
136           int delta;
137           if (up)
138             {
139               delta = roll (down_low[up_down_level-1], down_high[up_down_level-1]);
140               up = false;
141             }
142           else
143             {
144               delta = roll (up_low[up_down_level-1], up_high[up_down_level-1]);
145               up = true;
146             }
147       
148           msg (D_GREMLIN,
149                "GREMLIN: CONNECTION GOING %s FOR %d SECONDS",
150                (up ? "UP" : "DOWN"),
151                delta);
152           next = now + delta;
153         }
154     }
155
156   if (drop_level)
157     {
158       if (up && flip (drop_freq[drop_level-1]))
159         {
160           dmsg (D_GREMLIN_VERBOSE, "GREMLIN: Random packet drop");
161           return false;
162         }
163     }
164
165   return up;
166 }
167
168 /*
169  * Possibly corrupt a packet.
170  */
171 void corrupt_gremlin (struct buffer *buf, int flags) {
172   const int corrupt_level = GREMLIN_CORRUPT_LEVEL (flags);
173   if (corrupt_level)
174     {
175       if (flip (corrupt_freq[corrupt_level-1]))
176         {
177           do
178             {
179               if (buf->len > 0)
180                 {
181                   uint8_t r = roll (0, 255);
182                   int method = roll (0, 5);
183
184                   switch (method) {
185                   case 0: /* corrupt the first byte */
186                     *BPTR (buf) = r;
187                     break;
188                   case 1: /* corrupt the last byte */
189                     *(BPTR (buf) + buf->len - 1) = r;
190                     break;
191                   case 2: /* corrupt a random byte */
192                     *(BPTR(buf) + roll (0, buf->len - 1)) = r;
193                     break;
194                   case 3: /* append a random byte */
195                     buf_write (buf, &r, 1);
196                     break;
197                   case 4: /* reduce length by 1 */
198                     --buf->len;
199                     break;
200                   case 5: /* reduce length by a random amount */
201                     buf->len -= roll (0, buf->len - 1);
202                     break;
203                   }
204                   dmsg (D_GREMLIN_VERBOSE, "GREMLIN: Packet Corruption, method=%d", method);
205                 }
206               else
207                 break;
208             } while (flip (2)); /* a 50% chance we will corrupt again */
209         }
210     }
211 }
212
213 #else
214 static void dummy(void) {}
215 #endif