blob: 02b8f77f90f00cd1204e07d6eb4b51c9a326ee53 [file] [log] [blame]
Bart De Schuymer6697e432003-09-18 19:08:03 +00001/* Shared library add-on to ebtables to add limit support.
2 *
3 * Swipted from iptables' limit match.
4 */
5
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <getopt.h>
10#include "../include/ebtables_u.h"
11#include <linux/netfilter_bridge/ebt_limit.h>
12
13#define EBT_LIMIT_AVG "3/hour"
14#define EBT_LIMIT_BURST 5
15
16/* from iptables.c */
17#include <errno.h>
18int
19string_to_number(const char *s, unsigned int min, unsigned int max,
20 unsigned int *ret)
21{
22 long number;
23 char *end;
24
25 /* Handle hex, octal, etc. */
26 errno = 0;
27 number = strtol(s, &end, 0);
28 if (*end == '\0' && end != s) {
29 /* we parsed a number, let's see if we want this */
30 if (errno != ERANGE && min <= number && number <= max) {
31 *ret = number;
32 return 0;
33 }
34 }
35 return -1;
36}
37
38#define FLAG_LIMIT 0x01
39#define FLAG_LIMIT_BURST 0x02
40
41#define ARG_LIMIT '1'
42#define ARG_LIMIT_BURST '2'
43
44static struct option opts[] =
45{
46 { "limit", required_argument, 0, ARG_LIMIT },
47 { "limit-burst", required_argument, 0, ARG_LIMIT_BURST },
48 { 0 }
49};
50
51static void print_help(void)
52{
53 printf(
54"limit options:\n"
55"--limit avg max average match rate: default "EBT_LIMIT_AVG"\n"
56" [Packets per second unless followed by \n"
57" /sec /minute /hour /day postfixes]\n"
58"--limit-burst number number to match in a burst, -1 < number < 10001,\n"
59" default %u\n"
60"\n", EBT_LIMIT_BURST);
61}
62
63static int parse_rate(const char *rate, u_int32_t *val)
64{
65 const char *delim;
66 u_int32_t r;
67 u_int32_t mult = 1; /* Seconds by default. */
68
69 delim = strchr(rate, '/');
70 if (delim) {
71 if (strlen(delim+1) == 0)
72 return 0;
73
74 if (strncasecmp(delim+1, "second", strlen(delim+1)) == 0)
75 mult = 1;
76 else if (strncasecmp(delim+1, "minute", strlen(delim+1)) == 0)
77 mult = 60;
78 else if (strncasecmp(delim+1, "hour", strlen(delim+1)) == 0)
79 mult = 60*60;
80 else if (strncasecmp(delim+1, "day", strlen(delim+1)) == 0)
81 mult = 24*60*60;
82 else
83 return 0;
84 }
85 r = atoi(rate);
86 if (!r)
87 return 0;
88
89 /* This would get mapped to infinite (1/day is minimum they
90 can specify, so we're ok at that end). */
91 if (r / mult > EBT_LIMIT_SCALE)
92 return 0;
93
94 *val = EBT_LIMIT_SCALE * mult / r;
95 return 1;
96}
97
98/* Initialize the match. */
99static void init(struct ebt_entry_match *m)
100{
101 struct ebt_limit_info *r = (struct ebt_limit_info *)m->data;
102
103 parse_rate(EBT_LIMIT_AVG, &r->avg);
104 r->burst = EBT_LIMIT_BURST;
105}
106
107/* FIXME: handle overflow:
108 if (r->avg*r->burst/r->burst != r->avg)
109 exit_error(PARAMETER_PROBLEM,
110 "Sorry: burst too large for that avg rate.\n");
111*/
112
113static int parse(int c, char **argv, int argc,
114 const struct ebt_u_entry *entry,
115 unsigned int *flags,
116 struct ebt_entry_match **match)
117{
118 struct ebt_limit_info *r = (struct ebt_limit_info *)(*match)->data;
119 unsigned int num;
120
121 switch(c) {
122 case ARG_LIMIT:
123 check_option(flags, FLAG_LIMIT);
124 if (check_inverse(optarg))
125 print_error("Unexpected `!' after --limit");
126 if (!parse_rate(optarg, &r->avg))
127 print_error("bad rate `%s'", optarg);
128 break;
129
130 case ARG_LIMIT_BURST:
131 check_option(flags, FLAG_LIMIT_BURST);
132 if (check_inverse(optarg))
133 print_error("Unexpected `!' after --limit-burst");
134
135 if (string_to_number(optarg, 0, 10000, &num) == -1)
136 print_error("bad --limit-burst `%s'", optarg);
137 r->burst = num;
138 break;
139
140 default:
141 return 0;
142 }
143
144 return 1;
145}
146
147static void final_check(unsigned int flags)
148{
149 /* empty */
150}
151
152struct rates
153{
154 const char *name;
155 u_int32_t mult;
156};
157
158static struct rates g_rates[] =
159{
160 { "day", EBT_LIMIT_SCALE*24*60*60 },
161 { "hour", EBT_LIMIT_SCALE*60*60 },
162 { "min", EBT_LIMIT_SCALE*60 },
163 { "sec", EBT_LIMIT_SCALE }
164};
165
166static void print_rate(u_int32_t period)
167{
168 unsigned int i;
169
170 for (i = 1; i < sizeof(g_rates)/sizeof(struct rates); i++)
171 if (period > g_rates[i].mult ||
172 g_rates[i].mult/period < g_rates[i].mult%period)
173 break;
174
175 printf("%u/%s ", g_rates[i-1].mult / period, g_rates[i-1].name);
176}
177
178/* Prints out the matchinfo. */
179static void
180print(const struct ebt_u_entry *entry, const struct ebt_entry_match *match)
181{
182 struct ebt_limit_info *r = (struct ebt_limit_info *)match->data;
183
184 printf("limit: avg "); print_rate(r->avg);
185 printf("burst %u ", r->burst);
186}
187
188static int compare(const struct ebt_entry_match* m1, const struct ebt_entry_match *m2)
189{
190 struct ebt_limit_info* li1 = (struct ebt_limit_info*)m1->data;
191 struct ebt_limit_info* li2 = (struct ebt_limit_info*)m2->data;
192
193 if (li1->avg != li2->avg)
194 return 0;
195
196 if (li1->burst != li2->burst)
197 return 0;
198
199 return 1;
200}
201
202static struct ebt_u_match limit_match =
203{
204 .name EBT_LIMIT_MATCH,
205 .size sizeof(struct ebt_limit_info),
206 .help print_help,
207 .init init,
208 .parse parse,
209 .final_check final_check,
210 .print print,
211 .compare compare,
212 .extra_ops opts,
213};
214
215static void _init(void) __attribute((constructor));
216static void _init(void)
217{
218 register_match(&limit_match);
219}