blob: 28395472c0301e3eea07f7d92b1e231e772291b8 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Shared library add-on to iptables to add limit support.
2 *
3 * Jérôme de Vivie <devivie@info.enserb.u-bordeaux.fr>
Harald Welteb77f1da2002-03-14 11:35:58 +00004 * Hervé Eychenne <rv@wallfire.org>
Marc Bouchere6869a82000-03-20 06:03:29 +00005 */
Harald Welteb77f1da2002-03-14 11:35:58 +00006
Marc Bouchere6869a82000-03-20 06:03:29 +00007#include <stdio.h>
8#include <string.h>
9#include <stdlib.h>
10#include <getopt.h>
11#include <iptables.h>
Rusty Russell849779c2000-04-23 15:51:51 +000012#include <stddef.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000013#include <linux/netfilter_ipv4/ip_tables.h>
14#include <linux/netfilter_ipv4/ipt_limit.h>
15
16#define IPT_LIMIT_AVG "3/hour"
17#define IPT_LIMIT_BURST 5
18
19/* Function which prints out usage message. */
20static void
21help(void)
22{
23 printf(
24"limit v%s options:\n"
25"--limit avg max average match rate: default "IPT_LIMIT_AVG"\n"
26" [Packets per second unless followed by \n"
27" /sec /minute /hour /day postfixes]\n"
28"--limit-burst number number to match in a burst, default %u\n"
29"\n", NETFILTER_VERSION, IPT_LIMIT_BURST);
30}
31
32static struct option opts[] = {
33 { "limit", 1, 0, '%' },
34 { "limit-burst", 1, 0, '$' },
35 { 0 }
36};
37
38static
39int parse_rate(const char *rate, u_int32_t *val)
40{
41 const char *delim;
42 u_int32_t r;
43 u_int32_t mult = 1; /* Seconds by default. */
44
45 delim = strchr(rate, '/');
46 if (delim) {
47 if (strlen(delim+1) == 0)
48 return 0;
49
50 if (strncasecmp(delim+1, "second", strlen(delim+1)) == 0)
51 mult = 1;
52 else if (strncasecmp(delim+1, "minute", strlen(delim+1)) == 0)
53 mult = 60;
54 else if (strncasecmp(delim+1, "hour", strlen(delim+1)) == 0)
55 mult = 60*60;
56 else if (strncasecmp(delim+1, "day", strlen(delim+1)) == 0)
57 mult = 24*60*60;
58 else
59 return 0;
60 }
61 r = atoi(rate);
62 if (!r)
63 return 0;
64
65 /* This would get mapped to infinite (1/day is minimum they
66 can specify, so we're ok at that end). */
67 if (r / mult > IPT_LIMIT_SCALE)
68 exit_error(PARAMETER_PROBLEM, "Rate too fast `%s'\n", rate);
69
70 *val = IPT_LIMIT_SCALE * mult / r;
71 return 1;
72}
73
74/* Initialize the match. */
75static void
76init(struct ipt_entry_match *m, unsigned int *nfcache)
77{
78 struct ipt_rateinfo *r = (struct ipt_rateinfo *)m->data;
79
80 parse_rate(IPT_LIMIT_AVG, &r->avg);
81 r->burst = IPT_LIMIT_BURST;
82
83 /* Can't cache this */
84 *nfcache |= NFC_UNKNOWN;
85}
86
87/* FIXME: handle overflow:
88 if (r->avg*r->burst/r->burst != r->avg)
89 exit_error(PARAMETER_PROBLEM,
90 "Sorry: burst too large for that avg rate.\n");
91*/
92
93/* Function which parses command options; returns true if it
94 ate an option */
95static int
96parse(int c, char **argv, int invert, unsigned int *flags,
97 const struct ipt_entry *entry,
98 unsigned int *nfcache,
99 struct ipt_entry_match **match)
100{
101 struct ipt_rateinfo *r = (struct ipt_rateinfo *)(*match)->data;
Harald Welteb4719762001-07-23 02:14:22 +0000102 unsigned int num;
Marc Bouchere6869a82000-03-20 06:03:29 +0000103
104 switch(c) {
105 case '%':
Harald Welteb77f1da2002-03-14 11:35:58 +0000106 if (check_inverse(optarg, &invert, NULL, 0))
Marc Bouchere6869a82000-03-20 06:03:29 +0000107 exit_error(PARAMETER_PROBLEM,
108 "Unexpected `!' after --limit");
109 if (!parse_rate(optarg, &r->avg))
110 exit_error(PARAMETER_PROBLEM,
111 "bad rate `%s'", optarg);
Rusty Russell7e53bf92000-03-20 07:03:28 +0000112 break;
Marc Bouchere6869a82000-03-20 06:03:29 +0000113
114 case '$':
Harald Welteb77f1da2002-03-14 11:35:58 +0000115 if (check_inverse(optarg, &invert, NULL, 0))
Marc Bouchere6869a82000-03-20 06:03:29 +0000116 exit_error(PARAMETER_PROBLEM,
117 "Unexpected `!' after --limit-burst");
118
Marc Boucher06e63942001-07-23 15:34:46 +0000119 if (string_to_number(optarg, 0, 10000, &num) == -1)
Marc Bouchere6869a82000-03-20 06:03:29 +0000120 exit_error(PARAMETER_PROBLEM,
121 "bad --limit-burst `%s'", optarg);
122 r->burst = num;
123 break;
124
125 default:
126 return 0;
127 }
128
129 return 1;
130}
131
132/* Final check; nothing. */
133static void final_check(unsigned int flags)
134{
135}
136
137static struct rates
138{
139 const char *name;
140 u_int32_t mult;
141} rates[] = { { "day", IPT_LIMIT_SCALE*24*60*60 },
142 { "hour", IPT_LIMIT_SCALE*60*60 },
143 { "min", IPT_LIMIT_SCALE*60 },
144 { "sec", IPT_LIMIT_SCALE } };
145
146static void print_rate(u_int32_t period)
147{
148 unsigned int i;
149
150 for (i = 1; i < sizeof(rates)/sizeof(struct rates); i++) {
151 if (period > rates[i].mult
Harald Welte1412e452001-10-16 08:26:37 +0000152 || rates[i].mult/period < rates[i].mult%period)
Marc Bouchere6869a82000-03-20 06:03:29 +0000153 break;
154 }
155
156 printf("%u/%s ", rates[i-1].mult / period, rates[i-1].name);
157}
158
159/* Prints out the matchinfo. */
160static void
161print(const struct ipt_ip *ip,
162 const struct ipt_entry_match *match,
163 int numeric)
164{
165 struct ipt_rateinfo *r = (struct ipt_rateinfo *)match->data;
166 printf("limit: avg "); print_rate(r->avg);
167 printf("burst %u ", r->burst);
168}
169
170/* FIXME: Make minimalist: only print rate if not default --RR */
171static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
172{
173 struct ipt_rateinfo *r = (struct ipt_rateinfo *)match->data;
174
175 printf("--limit "); print_rate(r->avg);
176 if (r->burst != IPT_LIMIT_BURST)
177 printf("--limit-burst %u ", r->burst);
178}
179
Harald Welte3efb6ea2001-08-06 18:50:21 +0000180static
Marc Bouchere6869a82000-03-20 06:03:29 +0000181struct iptables_match limit
182= { NULL,
183 "limit",
184 NETFILTER_VERSION,
Rusty Russell73f72f52000-07-03 10:17:57 +0000185 IPT_ALIGN(sizeof(struct ipt_rateinfo)),
Rusty Russelledf14cf2000-04-19 11:26:44 +0000186 offsetof(struct ipt_rateinfo, prev),
Marc Bouchere6869a82000-03-20 06:03:29 +0000187 &help,
188 &init,
189 &parse,
190 &final_check,
191 &print,
192 &save,
193 opts
194};
195
196void _init(void)
197{
198 register_match(&limit);
199}