1 /* Shared library add-on to iptables to add limit support.
3 * Jérôme de Vivie <devivie@info.enserb.u-bordeaux.fr>
4 * Hervé Eychenne <rv@wallfire.org>
13 #include <linux/netfilter/x_tables.h>
14 /* For 64bit kernel / 32bit userspace */
15 #include <linux/netfilter/xt_limit.h>
17 #define XT_LIMIT_AVG "3/hour"
18 #define XT_LIMIT_BURST 5
20 static void limit_help(void)
23 "limit match options:\n"
24 "--limit avg max average match rate: default "XT_LIMIT_AVG"\n"
25 " [Packets per second unless followed by \n"
26 " /sec /minute /hour /day postfixes]\n"
27 "--limit-burst number number to match in a burst, default %u\n",
31 static const struct option limit_opts[] = {
32 { "limit", 1, NULL, '%' },
33 { "limit-burst", 1, NULL, '$' },
38 int parse_rate(const char *rate, u_int32_t *val)
42 u_int32_t mult = 1; /* Seconds by default. */
44 delim = strchr(rate, '/');
46 if (strlen(delim+1) == 0)
49 if (strncasecmp(delim+1, "second", strlen(delim+1)) == 0)
51 else if (strncasecmp(delim+1, "minute", strlen(delim+1)) == 0)
53 else if (strncasecmp(delim+1, "hour", strlen(delim+1)) == 0)
55 else if (strncasecmp(delim+1, "day", strlen(delim+1)) == 0)
64 /* This would get mapped to infinite (1/day is minimum they
65 can specify, so we're ok at that end). */
66 if (r / mult > XT_LIMIT_SCALE)
67 xtables_error(PARAMETER_PROBLEM, "Rate too fast \"%s\"\n", rate);
69 *val = XT_LIMIT_SCALE * mult / r;
73 static void limit_init(struct xt_entry_match *m)
75 struct xt_rateinfo *r = (struct xt_rateinfo *)m->data;
77 parse_rate(XT_LIMIT_AVG, &r->avg);
78 r->burst = XT_LIMIT_BURST;
82 /* FIXME: handle overflow:
83 if (r->avg*r->burst/r->burst != r->avg)
84 xtables_error(PARAMETER_PROBLEM,
85 "Sorry: burst too large for that avg rate.\n");
89 limit_parse(int c, char **argv, int invert, unsigned int *flags,
90 const void *entry, struct xt_entry_match **match)
92 struct xt_rateinfo *r = (struct xt_rateinfo *)(*match)->data;
97 if (xtables_check_inverse(optarg, &invert, &optind, 0, argv)) break;
98 if (!parse_rate(optarg, &r->avg))
99 xtables_error(PARAMETER_PROBLEM,
100 "bad rate `%s'", optarg);
104 if (xtables_check_inverse(optarg, &invert, &optind, 0, argv)) break;
105 if (!xtables_strtoui(optarg, NULL, &num, 0, 10000))
106 xtables_error(PARAMETER_PROBLEM,
107 "bad --limit-burst `%s'", optarg);
116 xtables_error(PARAMETER_PROBLEM,
117 "limit does not support invert");
122 static const struct rates
126 } rates[] = { { "day", XT_LIMIT_SCALE*24*60*60 },
127 { "hour", XT_LIMIT_SCALE*60*60 },
128 { "min", XT_LIMIT_SCALE*60 },
129 { "sec", XT_LIMIT_SCALE } };
131 static void print_rate(u_int32_t period)
135 for (i = 1; i < ARRAY_SIZE(rates); ++i)
136 if (period > rates[i].mult
137 || rates[i].mult/period < rates[i].mult%period)
140 printf("%u/%s ", rates[i-1].mult / period, rates[i-1].name);
144 limit_print(const void *ip, const struct xt_entry_match *match, int numeric)
146 const struct xt_rateinfo *r = (const void *)match->data;
147 printf("limit: avg "); print_rate(r->avg);
148 printf("burst %u ", r->burst);
151 static void limit_save(const void *ip, const struct xt_entry_match *match)
153 const struct xt_rateinfo *r = (const void *)match->data;
155 printf("--limit "); print_rate(r->avg);
156 if (r->burst != XT_LIMIT_BURST)
157 printf("--limit-burst %u ", r->burst);
160 static struct xtables_match limit_match = {
161 .family = NFPROTO_UNSPEC,
163 .version = XTABLES_VERSION,
164 .size = XT_ALIGN(sizeof(struct xt_rateinfo)),
165 .userspacesize = offsetof(struct xt_rateinfo, prev),
168 .parse = limit_parse,
169 .print = limit_print,
171 .extra_opts = limit_opts,
176 xtables_register_match(&limit_match);