iptables-1.2.9-2.3.1.src.rpm
[iptables.git] / extensions / libipt_limit.c
1 /* Shared library add-on to iptables to add limit support.
2  *
3  * Jérôme de Vivie   <devivie@info.enserb.u-bordeaux.fr>
4  * Hervé Eychenne    <rv@wallfire.org>
5  */
6
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <getopt.h>
11 #include <iptables.h>
12 #include <stddef.h>
13 #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. */
20 static void
21 help(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", IPTABLES_VERSION, IPT_LIMIT_BURST);
30 }
31
32 static struct option opts[] = {
33         { "limit", 1, 0, '%' },
34         { "limit-burst", 1, 0, '$' },
35         { 0 }
36 };
37
38 static
39 int 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. */
75 static void
76 init(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 */
95 static int
96 parse(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;
102         unsigned int num;
103
104         switch(c) {
105         case '%':
106                 if (check_inverse(optarg, &invert, NULL, 0))
107                         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);
112                 break;
113
114         case '$':
115                 if (check_inverse(optarg, &invert, NULL, 0))
116                         exit_error(PARAMETER_PROBLEM,
117                                    "Unexpected `!' after --limit-burst");
118
119                 if (string_to_number(optarg, 0, 10000, &num) == -1)
120                         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. */
133 static void final_check(unsigned int flags)
134 {
135 }
136
137 static 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
146 static 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
152             || rates[i].mult/period < rates[i].mult%period)
153                         break;
154         }
155
156         printf("%u/%s ", rates[i-1].mult / period, rates[i-1].name);
157 }
158
159 /* Prints out the matchinfo. */
160 static void
161 print(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 */
171 static 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
180 static
181 struct iptables_match limit
182 = { NULL,
183     "limit",
184     IPTABLES_VERSION,
185     IPT_ALIGN(sizeof(struct ipt_rateinfo)),
186     offsetof(struct ipt_rateinfo, prev),
187     &help,
188     &init,
189     &parse,
190     &final_check,
191     &print,
192     &save,
193     opts
194 };
195
196 void _init(void)
197 {
198         register_match(&limit);
199 }