blob: f63db64e26f51ea652b530a66fe9aa7b6413f4c5 [file] [log] [blame]
Patrick McHardy00524b22006-11-13 20:31:42 +00001/* ip6tables match extension for limiting packets per destination
2 *
3 * (C) 2003-2004 by Harald Welte <laforge@netfilter.org>
4 *
5 * Development of this code was funded by Astaro AG, http://www.astaro.com/
6 *
7 * Based on ipt_limit.c by
Jan Engelhardt81bd5882008-09-04 17:49:18 +02008 * Jérôme de Vivie <devivie@info.enserb.u-bordeaux.fr>
9 * Hervé Eychenne <rv@wallfire.org>
Patrick McHardy00524b22006-11-13 20:31:42 +000010 *
11 * Error corections by nmalykh@bilim.com (22.01.2005)
12 */
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +010013#include <stdbool.h>
Jan Engelhardtef18e812008-08-04 12:47:48 +020014#include <stdint.h>
Patrick McHardy00524b22006-11-13 20:31:42 +000015#include <stdio.h>
16#include <string.h>
17#include <stdlib.h>
18#include <getopt.h>
Yasuyuki KOZAKAId62a9db2007-08-04 08:08:20 +000019#include <xtables.h>
Patrick McHardy00524b22006-11-13 20:31:42 +000020#include <stddef.h>
Yasuyuki KOZAKAId62a9db2007-08-04 08:08:20 +000021#include <linux/netfilter/x_tables.h>
Patrick McHardy00524b22006-11-13 20:31:42 +000022#include <linux/netfilter/xt_hashlimit.h>
23
24#define XT_HASHLIMIT_BURST 5
25
26/* miliseconds */
27#define XT_HASHLIMIT_GCINTERVAL 1000
28#define XT_HASHLIMIT_EXPIRE 10000
29
Jan Engelhardt181dead2007-10-04 16:27:07 +000030static void hashlimit_help(void)
Patrick McHardy00524b22006-11-13 20:31:42 +000031{
32 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020033"hashlimit match options:\n"
Patrick McHardy00524b22006-11-13 20:31:42 +000034"--hashlimit <avg> max average match rate\n"
35" [Packets per second unless followed by \n"
36" /sec /minute /hour /day postfixes]\n"
37"--hashlimit-mode <mode> mode is a comma-separated list of\n"
38" dstip,srcip,dstport,srcport\n"
39"--hashlimit-name <name> name for /proc/net/ipt_hashlimit/\n"
40"[--hashlimit-burst <num>] number to match in a burst, default %u\n"
41"[--hashlimit-htable-size <num>] number of hashtable buckets\n"
42"[--hashlimit-htable-max <num>] number of hashtable entries\n"
43"[--hashlimit-htable-gcinterval] interval between garbage collection runs\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020044"[--hashlimit-htable-expire] after which time are idle entries expired?\n",
45XT_HASHLIMIT_BURST);
Patrick McHardy00524b22006-11-13 20:31:42 +000046}
47
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +010048static void hashlimit_mt_help(void)
49{
50 printf(
51"hashlimit match options:\n"
52" --hashlimit-upto <avg> max average match rate\n"
53" [Packets per second unless followed by \n"
54" /sec /minute /hour /day postfixes]\n"
55" --hashlimit-above <avg> min average match rate\n"
56" --hashlimit-mode <mode> mode is a comma-separated list of\n"
57" dstip,srcip,dstport,srcport (or none)\n"
58" --hashlimit-srcmask <length> source address grouping prefix length\n"
59" --hashlimit-dstmask <length> destination address grouping prefix length\n"
60" --hashlimit-name <name> name for /proc/net/ipt_hashlimit\n"
61" --hashlimit-burst <num> number to match in a burst, default %u\n"
62" --hashlimit-htable-size <num> number of hashtable buckets\n"
63" --hashlimit-htable-max <num> number of hashtable entries\n"
64" --hashlimit-htable-gcinterval interval between garbage collection runs\n"
65" --hashlimit-htable-expire after which time are idle entries expired?\n"
66"\n", XT_HASHLIMIT_BURST);
67}
68
Jan Engelhardt181dead2007-10-04 16:27:07 +000069static const struct option hashlimit_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000070 { "hashlimit", 1, NULL, '%' },
71 { "hashlimit-burst", 1, NULL, '$' },
72 { "hashlimit-htable-size", 1, NULL, '&' },
73 { "hashlimit-htable-max", 1, NULL, '*' },
74 { "hashlimit-htable-gcinterval", 1, NULL, '(' },
75 { "hashlimit-htable-expire", 1, NULL, ')' },
76 { "hashlimit-mode", 1, NULL, '_' },
77 { "hashlimit-name", 1, NULL, '"' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000078 { .name = NULL }
Patrick McHardy00524b22006-11-13 20:31:42 +000079};
80
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +010081static const struct option hashlimit_mt_opts[] = {
82 {.name = "hashlimit-upto", .has_arg = true, .val = '%'},
83 {.name = "hashlimit-above", .has_arg = true, .val = '^'},
84 {.name = "hashlimit", .has_arg = true, .val = '%'},
85 {.name = "hashlimit-srcmask", .has_arg = true, .val = '<'},
86 {.name = "hashlimit-dstmask", .has_arg = true, .val = '>'},
87 {.name = "hashlimit-burst", .has_arg = true, .val = '$'},
88 {.name = "hashlimit-htable-size", .has_arg = true, .val = '&'},
89 {.name = "hashlimit-htable-max", .has_arg = true, .val = '*'},
90 {.name = "hashlimit-htable-gcinterval", .has_arg = true, .val = '('},
91 {.name = "hashlimit-htable-expire", .has_arg = true, .val = ')'},
92 {.name = "hashlimit-mode", .has_arg = true, .val = '_'},
93 {.name = "hashlimit-name", .has_arg = true, .val = '"'},
94 {},
95};
96
Patrick McHardy00524b22006-11-13 20:31:42 +000097static
98int parse_rate(const char *rate, u_int32_t *val)
99{
100 const char *delim;
101 u_int32_t r;
102 u_int32_t mult = 1; /* Seconds by default. */
103
104 delim = strchr(rate, '/');
105 if (delim) {
106 if (strlen(delim+1) == 0)
107 return 0;
108
109 if (strncasecmp(delim+1, "second", strlen(delim+1)) == 0)
110 mult = 1;
111 else if (strncasecmp(delim+1, "minute", strlen(delim+1)) == 0)
112 mult = 60;
113 else if (strncasecmp(delim+1, "hour", strlen(delim+1)) == 0)
114 mult = 60*60;
115 else if (strncasecmp(delim+1, "day", strlen(delim+1)) == 0)
116 mult = 24*60*60;
117 else
118 return 0;
119 }
120 r = atoi(rate);
121 if (!r)
122 return 0;
123
124 /* This would get mapped to infinite (1/day is minimum they
125 can specify, so we're ok at that end). */
126 if (r / mult > XT_HASHLIMIT_SCALE)
127 exit_error(PARAMETER_PROBLEM, "Rate too fast `%s'\n", rate);
128
129 *val = XT_HASHLIMIT_SCALE * mult / r;
130 return 1;
131}
132
Jan Engelhardt181dead2007-10-04 16:27:07 +0000133static void hashlimit_init(struct xt_entry_match *m)
Patrick McHardy00524b22006-11-13 20:31:42 +0000134{
135 struct xt_hashlimit_info *r = (struct xt_hashlimit_info *)m->data;
136
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100137 r->cfg.mode = 0;
Patrick McHardy00524b22006-11-13 20:31:42 +0000138 r->cfg.burst = XT_HASHLIMIT_BURST;
139 r->cfg.gc_interval = XT_HASHLIMIT_GCINTERVAL;
140 r->cfg.expire = XT_HASHLIMIT_EXPIRE;
141
142}
143
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100144static void hashlimit_mt4_init(struct xt_entry_match *match)
145{
146 struct xt_hashlimit_mtinfo1 *info = (void *)match->data;
147
148 info->cfg.mode = 0;
149 info->cfg.burst = XT_HASHLIMIT_BURST;
150 info->cfg.gc_interval = XT_HASHLIMIT_GCINTERVAL;
151 info->cfg.expire = XT_HASHLIMIT_EXPIRE;
152 info->cfg.srcmask = 32;
153 info->cfg.dstmask = 32;
154}
155
156static void hashlimit_mt6_init(struct xt_entry_match *match)
157{
158 struct xt_hashlimit_mtinfo1 *info = (void *)match->data;
159
160 info->cfg.mode = 0;
161 info->cfg.burst = XT_HASHLIMIT_BURST;
162 info->cfg.gc_interval = XT_HASHLIMIT_GCINTERVAL;
163 info->cfg.expire = XT_HASHLIMIT_EXPIRE;
164 info->cfg.srcmask = 128;
165 info->cfg.dstmask = 128;
166}
Patrick McHardy00524b22006-11-13 20:31:42 +0000167
168/* Parse a 'mode' parameter into the required bitmask */
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100169static int parse_mode(uint32_t *mode, char *option_arg)
Patrick McHardy00524b22006-11-13 20:31:42 +0000170{
171 char *tok;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100172 char *arg = strdup(option_arg);
Patrick McHardy00524b22006-11-13 20:31:42 +0000173
174 if (!arg)
175 return -1;
176
Patrick McHardy00524b22006-11-13 20:31:42 +0000177 for (tok = strtok(arg, ",|");
178 tok;
179 tok = strtok(NULL, ",|")) {
180 if (!strcmp(tok, "dstip"))
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100181 *mode |= XT_HASHLIMIT_HASH_DIP;
Patrick McHardy00524b22006-11-13 20:31:42 +0000182 else if (!strcmp(tok, "srcip"))
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100183 *mode |= XT_HASHLIMIT_HASH_SIP;
Patrick McHardy00524b22006-11-13 20:31:42 +0000184 else if (!strcmp(tok, "srcport"))
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100185 *mode |= XT_HASHLIMIT_HASH_SPT;
Patrick McHardy00524b22006-11-13 20:31:42 +0000186 else if (!strcmp(tok, "dstport"))
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100187 *mode |= XT_HASHLIMIT_HASH_DPT;
Patrick McHardy00524b22006-11-13 20:31:42 +0000188 else {
189 free(arg);
190 return -1;
191 }
192 }
193 free(arg);
194 return 0;
195}
196
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100197enum {
198 PARAM_LIMIT = 1 << 0,
199 PARAM_BURST = 1 << 1,
200 PARAM_MODE = 1 << 2,
201 PARAM_NAME = 1 << 3,
202 PARAM_SIZE = 1 << 4,
203 PARAM_MAX = 1 << 5,
204 PARAM_GCINTERVAL = 1 << 6,
205 PARAM_EXPIRE = 1 << 7,
206 PARAM_SRCMASK = 1 << 8,
207 PARAM_DSTMASK = 1 << 9,
208};
Patrick McHardy00524b22006-11-13 20:31:42 +0000209
Patrick McHardy00524b22006-11-13 20:31:42 +0000210static int
Jan Engelhardt181dead2007-10-04 16:27:07 +0000211hashlimit_parse(int c, char **argv, int invert, unsigned int *flags,
212 const void *entry, struct xt_entry_match **match)
Patrick McHardy00524b22006-11-13 20:31:42 +0000213{
214 struct xt_hashlimit_info *r =
215 (struct xt_hashlimit_info *)(*match)->data;
216 unsigned int num;
217
218 switch(c) {
219 case '%':
Jan Engelhardta41545c2009-01-27 21:27:19 +0100220 xtables_param_act(XTF_ONLY_ONCE, "hashlimit", "--hashlimit",
Jan Engelhardtda75a5a2008-01-20 13:39:11 +0000221 *flags & PARAM_LIMIT);
Patrick McHardy00524b22006-11-13 20:31:42 +0000222 if (check_inverse(argv[optind-1], &invert, &optind, 0)) break;
223 if (!parse_rate(optarg, &r->cfg.avg))
224 exit_error(PARAMETER_PROBLEM,
225 "bad rate `%s'", optarg);
226 *flags |= PARAM_LIMIT;
227 break;
228
229 case '$':
Jan Engelhardta41545c2009-01-27 21:27:19 +0100230 xtables_param_act(XTF_ONLY_ONCE, "hashlimit", "--hashlimit-burst",
Jan Engelhardtda75a5a2008-01-20 13:39:11 +0000231 *flags & PARAM_BURST);
Patrick McHardy00524b22006-11-13 20:31:42 +0000232 if (check_inverse(argv[optind-1], &invert, &optind, 0)) break;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100233 if (!xtables_strtoui(optarg, NULL, &num, 0, 10000))
Patrick McHardy00524b22006-11-13 20:31:42 +0000234 exit_error(PARAMETER_PROBLEM,
235 "bad --hashlimit-burst `%s'", optarg);
236 r->cfg.burst = num;
237 *flags |= PARAM_BURST;
238 break;
239 case '&':
Jan Engelhardta41545c2009-01-27 21:27:19 +0100240 xtables_param_act(XTF_ONLY_ONCE, "hashlimit", "--hashlimit-htable-size",
Jan Engelhardtda75a5a2008-01-20 13:39:11 +0000241 *flags & PARAM_SIZE);
Patrick McHardy00524b22006-11-13 20:31:42 +0000242 if (check_inverse(argv[optind-1], &invert, &optind, 0)) break;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100243 if (!xtables_strtoui(optarg, NULL, &num, 0, UINT32_MAX))
Patrick McHardy00524b22006-11-13 20:31:42 +0000244 exit_error(PARAMETER_PROBLEM,
245 "bad --hashlimit-htable-size: `%s'", optarg);
246 r->cfg.size = num;
247 *flags |= PARAM_SIZE;
248 break;
249 case '*':
Jan Engelhardta41545c2009-01-27 21:27:19 +0100250 xtables_param_act(XTF_ONLY_ONCE, "hashlimit", "--hashlimit-htable-max",
Jan Engelhardtda75a5a2008-01-20 13:39:11 +0000251 *flags & PARAM_MAX);
Patrick McHardy00524b22006-11-13 20:31:42 +0000252 if (check_inverse(argv[optind-1], &invert, &optind, 0)) break;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100253 if (!xtables_strtoui(optarg, NULL, &num, 0, UINT32_MAX))
Patrick McHardy00524b22006-11-13 20:31:42 +0000254 exit_error(PARAMETER_PROBLEM,
255 "bad --hashlimit-htable-max: `%s'", optarg);
256 r->cfg.max = num;
257 *flags |= PARAM_MAX;
258 break;
259 case '(':
Jan Engelhardta41545c2009-01-27 21:27:19 +0100260 xtables_param_act(XTF_ONLY_ONCE, "hashlimit",
Jan Engelhardtda75a5a2008-01-20 13:39:11 +0000261 "--hashlimit-htable-gcinterval",
262 *flags & PARAM_GCINTERVAL);
Patrick McHardy00524b22006-11-13 20:31:42 +0000263 if (check_inverse(argv[optind-1], &invert, &optind, 0)) break;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100264 if (!xtables_strtoui(optarg, NULL, &num, 0, UINT32_MAX))
Patrick McHardy00524b22006-11-13 20:31:42 +0000265 exit_error(PARAMETER_PROBLEM,
266 "bad --hashlimit-htable-gcinterval: `%s'",
267 optarg);
268 /* FIXME: not HZ dependent!! */
269 r->cfg.gc_interval = num;
270 *flags |= PARAM_GCINTERVAL;
271 break;
272 case ')':
Jan Engelhardta41545c2009-01-27 21:27:19 +0100273 xtables_param_act(XTF_ONLY_ONCE, "hashlimit",
Jan Engelhardtda75a5a2008-01-20 13:39:11 +0000274 "--hashlimit-htable-expire", *flags & PARAM_EXPIRE);
Patrick McHardy00524b22006-11-13 20:31:42 +0000275 if (check_inverse(argv[optind-1], &invert, &optind, 0)) break;
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100276 if (!xtables_strtoui(optarg, NULL, &num, 0, UINT32_MAX))
Patrick McHardy00524b22006-11-13 20:31:42 +0000277 exit_error(PARAMETER_PROBLEM,
278 "bad --hashlimit-htable-expire: `%s'", optarg);
279 /* FIXME: not HZ dependent */
280 r->cfg.expire = num;
281 *flags |= PARAM_EXPIRE;
282 break;
283 case '_':
Jan Engelhardta41545c2009-01-27 21:27:19 +0100284 xtables_param_act(XTF_ONLY_ONCE, "hashlimit", "--hashlimit-mode",
Jan Engelhardtda75a5a2008-01-20 13:39:11 +0000285 *flags & PARAM_MODE);
Patrick McHardy00524b22006-11-13 20:31:42 +0000286 if (check_inverse(argv[optind-1], &invert, &optind, 0)) break;
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100287 if (parse_mode(&r->cfg.mode, optarg) < 0)
Patrick McHardy00524b22006-11-13 20:31:42 +0000288 exit_error(PARAMETER_PROBLEM,
289 "bad --hashlimit-mode: `%s'\n", optarg);
290 *flags |= PARAM_MODE;
291 break;
292 case '"':
Jan Engelhardta41545c2009-01-27 21:27:19 +0100293 xtables_param_act(XTF_ONLY_ONCE, "hashlimit", "--hashlimit-name",
Jan Engelhardtda75a5a2008-01-20 13:39:11 +0000294 *flags & PARAM_NAME);
Patrick McHardy00524b22006-11-13 20:31:42 +0000295 if (check_inverse(argv[optind-1], &invert, &optind, 0)) break;
296 if (strlen(optarg) == 0)
297 exit_error(PARAMETER_PROBLEM, "Zero-length name?");
298 strncpy(r->name, optarg, sizeof(r->name));
299 *flags |= PARAM_NAME;
300 break;
301 default:
302 return 0;
303 }
304
305 if (invert)
306 exit_error(PARAMETER_PROBLEM,
307 "hashlimit does not support invert");
308
309 return 1;
310}
311
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100312static int
313hashlimit_mt_parse(struct xt_hashlimit_mtinfo1 *info, unsigned int *flags,
314 int c, int invert, unsigned int maxmask)
315{
316 unsigned int num;
317
318 switch(c) {
319 case '%': /* --hashlimit / --hashlimit-below */
Jan Engelhardta41545c2009-01-27 21:27:19 +0100320 xtables_param_act(XTF_ONLY_ONCE, "hashlimit", "--hashlimit-upto",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100321 *flags & PARAM_LIMIT);
322 if (invert)
323 info->cfg.mode |= XT_HASHLIMIT_INVERT;
324 if (!parse_rate(optarg, &info->cfg.avg))
Jan Engelhardta41545c2009-01-27 21:27:19 +0100325 xtables_param_act(XTF_BAD_VALUE, "hashlimit",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100326 "--hashlimit-upto", optarg);
327 *flags |= PARAM_LIMIT;
328 return true;
329
330 case '^': /* --hashlimit-above == !--hashlimit-below */
Jan Engelhardta41545c2009-01-27 21:27:19 +0100331 xtables_param_act(XTF_ONLY_ONCE, "hashlimit", "--hashlimit-above",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100332 *flags & PARAM_LIMIT);
333 if (!invert)
334 info->cfg.mode |= XT_HASHLIMIT_INVERT;
335 if (!parse_rate(optarg, &info->cfg.avg))
Jan Engelhardta41545c2009-01-27 21:27:19 +0100336 xtables_param_act(XTF_BAD_VALUE, "hashlimit",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100337 "--hashlimit-above", optarg);
338 *flags |= PARAM_LIMIT;
339 return true;
340
341 case '$': /* --hashlimit-burst */
Jan Engelhardta41545c2009-01-27 21:27:19 +0100342 xtables_param_act(XTF_ONLY_ONCE, "hashlimit", "--hashlimit-burst",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100343 *flags & PARAM_BURST);
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100344 if (!xtables_strtoui(optarg, NULL, &num, 0, 10000))
Jan Engelhardta41545c2009-01-27 21:27:19 +0100345 xtables_param_act(XTF_BAD_VALUE, "hashlimit",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100346 "--hashlimit-burst", optarg);
347 info->cfg.burst = num;
348 *flags |= PARAM_BURST;
349 return true;
350
351 case '&': /* --hashlimit-htable-size */
Jan Engelhardta41545c2009-01-27 21:27:19 +0100352 xtables_param_act(XTF_ONLY_ONCE, "hashlimit", "--hashlimit-htable-size",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100353 *flags & PARAM_SIZE);
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100354 if (!xtables_strtoui(optarg, NULL, &num, 0, UINT32_MAX))
Jan Engelhardta41545c2009-01-27 21:27:19 +0100355 xtables_param_act(XTF_BAD_VALUE, "hashlimit",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100356 "--hashlimit-htable-size", optarg);
357 info->cfg.size = num;
358 *flags |= PARAM_SIZE;
359 return true;
360
361 case '*': /* --hashlimit-htable-max */
Jan Engelhardta41545c2009-01-27 21:27:19 +0100362 xtables_param_act(XTF_ONLY_ONCE, "hashlimit", "--hashlimit-htable-max",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100363 *flags & PARAM_MAX);
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100364 if (!xtables_strtoui(optarg, NULL, &num, 0, UINT32_MAX))
Jan Engelhardta41545c2009-01-27 21:27:19 +0100365 xtables_param_act(XTF_BAD_VALUE, "hashlimit",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100366 "--hashlimit-htable-max", optarg);
367 info->cfg.max = num;
368 *flags |= PARAM_MAX;
369 return true;
370
371 case '(': /* --hashlimit-htable-gcinterval */
Jan Engelhardta41545c2009-01-27 21:27:19 +0100372 xtables_param_act(XTF_ONLY_ONCE, "hashlimit",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100373 "--hashlimit-htable-gcinterval",
374 *flags & PARAM_GCINTERVAL);
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100375 if (!xtables_strtoui(optarg, NULL, &num, 0, UINT32_MAX))
Jan Engelhardta41545c2009-01-27 21:27:19 +0100376 xtables_param_act(XTF_BAD_VALUE, "hashlimit",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100377 "--hashlimit-htable-gcinterval", optarg);
378 /* FIXME: not HZ dependent!! */
379 info->cfg.gc_interval = num;
380 *flags |= PARAM_GCINTERVAL;
381 return true;
382
383 case ')': /* --hashlimit-htable-expire */
Jan Engelhardta41545c2009-01-27 21:27:19 +0100384 xtables_param_act(XTF_ONLY_ONCE, "hashlimit",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100385 "--hashlimit-htable-expire", *flags & PARAM_EXPIRE);
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100386 if (!xtables_strtoui(optarg, NULL, &num, 0, UINT32_MAX))
Jan Engelhardta41545c2009-01-27 21:27:19 +0100387 xtables_param_act(XTF_BAD_VALUE, "hashlimit",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100388 "--hashlimit-htable-expire", optarg);
389 /* FIXME: not HZ dependent */
390 info->cfg.expire = num;
391 *flags |= PARAM_EXPIRE;
392 return true;
393
394 case '_':
Jan Engelhardta41545c2009-01-27 21:27:19 +0100395 xtables_param_act(XTF_ONLY_ONCE, "hashlimit", "--hashlimit-mode",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100396 *flags & PARAM_MODE);
397 if (parse_mode(&info->cfg.mode, optarg) < 0)
Jan Engelhardta41545c2009-01-27 21:27:19 +0100398 xtables_param_act(XTF_BAD_VALUE, "hashlimit",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100399 "--hashlimit-mode", optarg);
400 *flags |= PARAM_MODE;
401 return true;
402
403 case '"': /* --hashlimit-name */
Jan Engelhardta41545c2009-01-27 21:27:19 +0100404 xtables_param_act(XTF_ONLY_ONCE, "hashlimit", "--hashlimit-name",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100405 *flags & PARAM_NAME);
406 if (strlen(optarg) == 0)
407 exit_error(PARAMETER_PROBLEM, "Zero-length name?");
408 strncpy(info->name, optarg, sizeof(info->name));
409 info->name[sizeof(info->name)-1] = '\0';
410 *flags |= PARAM_NAME;
411 return true;
412
413 case '<': /* --hashlimit-srcmask */
Jan Engelhardta41545c2009-01-27 21:27:19 +0100414 xtables_param_act(XTF_ONLY_ONCE, "hashlimit", "--hashlimit-srcmask",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100415 *flags & PARAM_SRCMASK);
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100416 if (!xtables_strtoui(optarg, NULL, &num, 0, maxmask))
Jan Engelhardta41545c2009-01-27 21:27:19 +0100417 xtables_param_act(XTF_BAD_VALUE, "hashlimit",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100418 "--hashlimit-srcmask", optarg);
419 info->cfg.srcmask = num;
420 *flags |= PARAM_SRCMASK;
421 return true;
422
423 case '>': /* --hashlimit-dstmask */
Jan Engelhardta41545c2009-01-27 21:27:19 +0100424 xtables_param_act(XTF_ONLY_ONCE, "hashlimit", "--hashlimit-dstmask",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100425 *flags & PARAM_DSTMASK);
Jan Engelhardt5f2922c2009-01-27 18:43:01 +0100426 if (!xtables_strtoui(optarg, NULL, &num, 0, maxmask))
Jan Engelhardta41545c2009-01-27 21:27:19 +0100427 xtables_param_act(XTF_BAD_VALUE, "hashlimit",
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100428 "--hashlimit-dstmask", optarg);
429 info->cfg.dstmask = num;
430 *flags |= PARAM_DSTMASK;
431 return true;
432 }
433 return false;
434}
435
436static int
437hashlimit_mt4_parse(int c, char **argv, int invert, unsigned int *flags,
438 const void *entry, struct xt_entry_match **match)
439{
440 return hashlimit_mt_parse((void *)(*match)->data,
441 flags, c, invert, 32);
442}
443
444static int
445hashlimit_mt6_parse(int c, char **argv, int invert, unsigned int *flags,
446 const void *entry, struct xt_entry_match **match)
447{
448 return hashlimit_mt_parse((void *)(*match)->data,
449 flags, c, invert, 128);
450}
451
Jan Engelhardt181dead2007-10-04 16:27:07 +0000452static void hashlimit_check(unsigned int flags)
Patrick McHardy00524b22006-11-13 20:31:42 +0000453{
454 if (!(flags & PARAM_LIMIT))
455 exit_error(PARAMETER_PROBLEM,
456 "You have to specify --hashlimit");
457 if (!(flags & PARAM_MODE))
458 exit_error(PARAMETER_PROBLEM,
459 "You have to specify --hashlimit-mode");
460 if (!(flags & PARAM_NAME))
461 exit_error(PARAMETER_PROBLEM,
462 "You have to specify --hashlimit-name");
463}
464
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100465static void hashlimit_mt_check(unsigned int flags)
466{
467 if (!(flags & PARAM_LIMIT))
468 exit_error(PARAMETER_PROBLEM, "You have to specify "
469 "--hashlimit-upto or --hashlimit-above");
470 if (!(flags & PARAM_NAME))
471 exit_error(PARAMETER_PROBLEM,
472 "You have to specify --hashlimit-name");
473}
474
Jan Engelhardt0e2abed2007-10-04 16:25:58 +0000475static const struct rates
Patrick McHardy00524b22006-11-13 20:31:42 +0000476{
477 const char *name;
478 u_int32_t mult;
479} rates[] = { { "day", XT_HASHLIMIT_SCALE*24*60*60 },
480 { "hour", XT_HASHLIMIT_SCALE*60*60 },
481 { "min", XT_HASHLIMIT_SCALE*60 },
482 { "sec", XT_HASHLIMIT_SCALE } };
483
484static void print_rate(u_int32_t period)
485{
486 unsigned int i;
487
488 for (i = 1; i < sizeof(rates)/sizeof(struct rates); i++) {
489 if (period > rates[i].mult
490 || rates[i].mult/period < rates[i].mult%period)
491 break;
492 }
493
494 printf("%u/%s ", rates[i-1].mult / period, rates[i-1].name);
495}
496
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100497static void print_mode(unsigned int mode, char separator)
Patrick McHardy00524b22006-11-13 20:31:42 +0000498{
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100499 bool prevmode = false;
Patrick McHardy00524b22006-11-13 20:31:42 +0000500
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100501 if (mode & XT_HASHLIMIT_HASH_SIP) {
Patrick McHardy00524b22006-11-13 20:31:42 +0000502 fputs("srcip", stdout);
503 prevmode = 1;
504 }
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100505 if (mode & XT_HASHLIMIT_HASH_SPT) {
Patrick McHardy00524b22006-11-13 20:31:42 +0000506 if (prevmode)
507 putchar(separator);
508 fputs("srcport", stdout);
509 prevmode = 1;
510 }
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100511 if (mode & XT_HASHLIMIT_HASH_DIP) {
Patrick McHardy00524b22006-11-13 20:31:42 +0000512 if (prevmode)
513 putchar(separator);
514 fputs("dstip", stdout);
515 prevmode = 1;
516 }
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100517 if (mode & XT_HASHLIMIT_HASH_DPT) {
Patrick McHardy00524b22006-11-13 20:31:42 +0000518 if (prevmode)
519 putchar(separator);
520 fputs("dstport", stdout);
521 }
522 putchar(' ');
523}
524
Jan Engelhardt181dead2007-10-04 16:27:07 +0000525static void hashlimit_print(const void *ip,
526 const struct xt_entry_match *match, int numeric)
Patrick McHardy00524b22006-11-13 20:31:42 +0000527{
528 struct xt_hashlimit_info *r =
529 (struct xt_hashlimit_info *)match->data;
530 fputs("limit: avg ", stdout); print_rate(r->cfg.avg);
531 printf("burst %u ", r->cfg.burst);
532 fputs("mode ", stdout);
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100533 print_mode(r->cfg.mode, '-');
Patrick McHardy00524b22006-11-13 20:31:42 +0000534 if (r->cfg.size)
535 printf("htable-size %u ", r->cfg.size);
536 if (r->cfg.max)
537 printf("htable-max %u ", r->cfg.max);
538 if (r->cfg.gc_interval != XT_HASHLIMIT_GCINTERVAL)
539 printf("htable-gcinterval %u ", r->cfg.gc_interval);
540 if (r->cfg.expire != XT_HASHLIMIT_EXPIRE)
541 printf("htable-expire %u ", r->cfg.expire);
542}
543
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100544static void
545hashlimit_mt_print(const struct xt_hashlimit_mtinfo1 *info, unsigned int dmask)
546{
547 if (info->cfg.mode & XT_HASHLIMIT_INVERT)
548 fputs("limit: above ", stdout);
549 else
550 fputs("limit: up to ", stdout);
551 print_rate(info->cfg.avg);
552 printf("burst %u ", info->cfg.burst);
553 if (info->cfg.mode & (XT_HASHLIMIT_HASH_SIP | XT_HASHLIMIT_HASH_SPT |
554 XT_HASHLIMIT_HASH_DIP | XT_HASHLIMIT_HASH_DPT)) {
555 fputs("mode ", stdout);
556 print_mode(info->cfg.mode, '-');
557 }
558 if (info->cfg.size != 0)
559 printf("htable-size %u ", info->cfg.size);
560 if (info->cfg.max != 0)
561 printf("htable-max %u ", info->cfg.max);
562 if (info->cfg.gc_interval != XT_HASHLIMIT_GCINTERVAL)
563 printf("htable-gcinterval %u ", info->cfg.gc_interval);
564 if (info->cfg.expire != XT_HASHLIMIT_EXPIRE)
565 printf("htable-expire %u ", info->cfg.expire);
566
567 if (info->cfg.srcmask != dmask)
568 printf("srcmask %u ", info->cfg.srcmask);
569 if (info->cfg.dstmask != dmask)
570 printf("dstmask %u ", info->cfg.dstmask);
571}
572
573static void
574hashlimit_mt4_print(const void *ip, const struct xt_entry_match *match,
575 int numeric)
576{
577 const struct xt_hashlimit_mtinfo1 *info = (const void *)match->data;
578
579 hashlimit_mt_print(info, 32);
580}
581
582static void
583hashlimit_mt6_print(const void *ip, const struct xt_entry_match *match,
584 int numeric)
585{
586 const struct xt_hashlimit_mtinfo1 *info = (const void *)match->data;
587
588 hashlimit_mt_print(info, 128);
589}
590
Jan Engelhardt181dead2007-10-04 16:27:07 +0000591static void hashlimit_save(const void *ip, const struct xt_entry_match *match)
Patrick McHardy00524b22006-11-13 20:31:42 +0000592{
593 struct xt_hashlimit_info *r =
594 (struct xt_hashlimit_info *)match->data;
595
596 fputs("--hashlimit ", stdout); print_rate(r->cfg.avg);
597 if (r->cfg.burst != XT_HASHLIMIT_BURST)
598 printf("--hashlimit-burst %u ", r->cfg.burst);
599
600 fputs("--hashlimit-mode ", stdout);
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100601 print_mode(r->cfg.mode, ',');
Patrick McHardy00524b22006-11-13 20:31:42 +0000602
603 printf("--hashlimit-name %s ", r->name);
604
605 if (r->cfg.size)
606 printf("--hashlimit-htable-size %u ", r->cfg.size);
607 if (r->cfg.max)
608 printf("--hashlimit-htable-max %u ", r->cfg.max);
609 if (r->cfg.gc_interval != XT_HASHLIMIT_GCINTERVAL)
Phil Oester04c97952008-08-04 13:28:07 +0200610 printf("--hashlimit-htable-gcinterval %u ", r->cfg.gc_interval);
Patrick McHardy00524b22006-11-13 20:31:42 +0000611 if (r->cfg.expire != XT_HASHLIMIT_EXPIRE)
612 printf("--hashlimit-htable-expire %u ", r->cfg.expire);
613}
614
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100615static void
616hashlimit_mt_save(const struct xt_hashlimit_mtinfo1 *info, unsigned int dmask)
617{
618 if (info->cfg.mode & XT_HASHLIMIT_INVERT)
619 fputs("--hashlimit-above ", stdout);
620 else
621 fputs("--hashlimit-upto ", stdout);
622 print_rate(info->cfg.avg);
623 if (info->cfg.burst != XT_HASHLIMIT_BURST)
624 printf("--hashlimit-burst %u ", info->cfg.burst);
625
626 if (info->cfg.mode & (XT_HASHLIMIT_HASH_SIP | XT_HASHLIMIT_HASH_SPT |
627 XT_HASHLIMIT_HASH_DIP | XT_HASHLIMIT_HASH_DPT)) {
628 fputs("--hashlimit-mode ", stdout);
629 print_mode(info->cfg.mode, ',');
630 }
631
632 printf("--hashlimit-name %s ", info->name);
633
634 if (info->cfg.size != 0)
635 printf("--hashlimit-htable-size %u ", info->cfg.size);
636 if (info->cfg.max != 0)
637 printf("--hashlimit-htable-max %u ", info->cfg.max);
638 if (info->cfg.gc_interval != XT_HASHLIMIT_GCINTERVAL)
639 printf("--hashlimit-htable-gcinterval %u", info->cfg.gc_interval);
640 if (info->cfg.expire != XT_HASHLIMIT_EXPIRE)
641 printf("--hashlimit-htable-expire %u ", info->cfg.expire);
642
643 if (info->cfg.srcmask != dmask)
644 printf("--hashlimit-srcmask %u ", info->cfg.srcmask);
645 if (info->cfg.dstmask != dmask)
646 printf("--hashlimit-dstmask %u ", info->cfg.dstmask);
647}
648
649static void
650hashlimit_mt4_save(const void *ip, const struct xt_entry_match *match)
651{
652 const struct xt_hashlimit_mtinfo1 *info = (const void *)match->data;
653
654 hashlimit_mt_save(info, 32);
655}
656
657static void
658hashlimit_mt6_save(const void *ip, const struct xt_entry_match *match)
659{
660 const struct xt_hashlimit_mtinfo1 *info = (const void *)match->data;
661
662 hashlimit_mt_save(info, 128);
663}
664
Jan Engelhardt181dead2007-10-04 16:27:07 +0000665static struct xtables_match hashlimit_match = {
Jan Engelhardt03d99482008-11-18 12:27:54 +0100666 .family = NFPROTO_IPV4,
Patrick McHardy00524b22006-11-13 20:31:42 +0000667 .name = "hashlimit",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200668 .version = XTABLES_VERSION,
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100669 .revision = 0,
Yasuyuki KOZAKAId62a9db2007-08-04 08:08:20 +0000670 .size = XT_ALIGN(sizeof(struct xt_hashlimit_info)),
Patrick McHardy00524b22006-11-13 20:31:42 +0000671 .userspacesize = offsetof(struct xt_hashlimit_info, hinfo),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000672 .help = hashlimit_help,
673 .init = hashlimit_init,
674 .parse = hashlimit_parse,
675 .final_check = hashlimit_check,
676 .print = hashlimit_print,
677 .save = hashlimit_save,
678 .extra_opts = hashlimit_opts,
Yasuyuki KOZAKAId62a9db2007-08-04 08:08:20 +0000679};
680
Jan Engelhardt181dead2007-10-04 16:27:07 +0000681static struct xtables_match hashlimit_match6 = {
Jan Engelhardt03d99482008-11-18 12:27:54 +0100682 .family = NFPROTO_IPV6,
Yasuyuki KOZAKAId62a9db2007-08-04 08:08:20 +0000683 .name = "hashlimit",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200684 .version = XTABLES_VERSION,
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100685 .revision = 0,
Yasuyuki KOZAKAId62a9db2007-08-04 08:08:20 +0000686 .size = XT_ALIGN(sizeof(struct xt_hashlimit_info)),
687 .userspacesize = offsetof(struct xt_hashlimit_info, hinfo),
Jan Engelhardt181dead2007-10-04 16:27:07 +0000688 .help = hashlimit_help,
689 .init = hashlimit_init,
690 .parse = hashlimit_parse,
691 .final_check = hashlimit_check,
692 .print = hashlimit_print,
693 .save = hashlimit_save,
694 .extra_opts = hashlimit_opts,
Patrick McHardy00524b22006-11-13 20:31:42 +0000695};
696
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100697static struct xtables_match hashlimit_mt_reg = {
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200698 .version = XTABLES_VERSION,
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100699 .name = "hashlimit",
700 .revision = 1,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100701 .family = NFPROTO_IPV4,
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100702 .size = XT_ALIGN(sizeof(struct xt_hashlimit_mtinfo1)),
703 .userspacesize = offsetof(struct xt_hashlimit_mtinfo1, hinfo),
704 .help = hashlimit_mt_help,
705 .init = hashlimit_mt4_init,
706 .parse = hashlimit_mt4_parse,
707 .final_check = hashlimit_mt_check,
708 .print = hashlimit_mt4_print,
709 .save = hashlimit_mt4_save,
710 .extra_opts = hashlimit_mt_opts,
711};
712
713static struct xtables_match hashlimit_mt6_reg = {
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200714 .version = XTABLES_VERSION,
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100715 .name = "hashlimit",
716 .revision = 1,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100717 .family = NFPROTO_IPV6,
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100718 .size = XT_ALIGN(sizeof(struct xt_hashlimit_mtinfo1)),
719 .userspacesize = offsetof(struct xt_hashlimit_mtinfo1, hinfo),
720 .help = hashlimit_mt_help,
721 .init = hashlimit_mt6_init,
722 .parse = hashlimit_mt6_parse,
723 .final_check = hashlimit_mt_check,
724 .print = hashlimit_mt6_print,
725 .save = hashlimit_mt6_save,
726 .extra_opts = hashlimit_mt_opts,
727};
728
Patrick McHardy00524b22006-11-13 20:31:42 +0000729void _init(void)
730{
Jan Engelhardt181dead2007-10-04 16:27:07 +0000731 xtables_register_match(&hashlimit_match);
732 xtables_register_match(&hashlimit_match6);
Jan Engelhardt9a8c77f2008-02-11 00:55:33 +0100733 xtables_register_match(&hashlimit_mt_reg);
734 xtables_register_match(&hashlimit_mt6_reg);
Patrick McHardy00524b22006-11-13 20:31:42 +0000735}