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/* For 64bit kernel / 32bit userspace */
15#include "../include/linux/netfilter_ipv4/ipt_limit.h"
16
17#define IPT_LIMIT_AVG	"3/hour"
18#define IPT_LIMIT_BURST	5
19
20/* Function which prints out usage message. */
21static void
22help(void)
23{
24	printf(
25"limit v%s options:\n"
26"--limit avg			max average match rate: default "IPT_LIMIT_AVG"\n"
27"                                [Packets per second unless followed by \n"
28"                                /sec /minute /hour /day postfixes]\n"
29"--limit-burst number		number to match in a burst, default %u\n"
30"\n", IPTABLES_VERSION, IPT_LIMIT_BURST);
31}
32
33static struct option opts[] = {
34	{ "limit", 1, 0, '%' },
35	{ "limit-burst", 1, 0, '$' },
36	{ 0 }
37};
38
39static
40int parse_rate(const char *rate, u_int32_t *val)
41{
42	const char *delim;
43	u_int32_t r;
44	u_int32_t mult = 1;  /* Seconds by default. */
45
46	delim = strchr(rate, '/');
47	if (delim) {
48		if (strlen(delim+1) == 0)
49			return 0;
50
51		if (strncasecmp(delim+1, "second", strlen(delim+1)) == 0)
52			mult = 1;
53		else if (strncasecmp(delim+1, "minute", strlen(delim+1)) == 0)
54			mult = 60;
55		else if (strncasecmp(delim+1, "hour", strlen(delim+1)) == 0)
56			mult = 60*60;
57		else if (strncasecmp(delim+1, "day", strlen(delim+1)) == 0)
58			mult = 24*60*60;
59		else
60			return 0;
61	}
62	r = atoi(rate);
63	if (!r)
64		return 0;
65
66	/* This would get mapped to infinite (1/day is minimum they
67           can specify, so we're ok at that end). */
68	if (r / mult > IPT_LIMIT_SCALE)
69		exit_error(PARAMETER_PROBLEM, "Rate too fast `%s'\n", rate);
70
71	*val = IPT_LIMIT_SCALE * mult / r;
72	return 1;
73}
74
75/* Initialize the match. */
76static void
77init(struct ipt_entry_match *m, unsigned int *nfcache)
78{
79	struct ipt_rateinfo *r = (struct ipt_rateinfo *)m->data;
80
81	parse_rate(IPT_LIMIT_AVG, &r->avg);
82	r->burst = IPT_LIMIT_BURST;
83
84}
85
86
87/* Function which parses command options; returns true if it
88   ate an option */
89static int
90parse(int c, char **argv, int invert, unsigned int *flags,
91      const struct ipt_entry *entry,
92      unsigned int *nfcache,
93      struct ipt_entry_match **match)
94{
95	struct ipt_rateinfo *r = (struct ipt_rateinfo *)(*match)->data;
96	unsigned int num;
97
98	switch(c) {
99	case '%':
100		if (check_inverse(argv[optind-1], &invert, &optind, 0)) break;
101		if (!parse_rate(optarg, &r->avg))
102			exit_error(PARAMETER_PROBLEM,
103				   "bad rate `%s'", optarg);
104		break;
105
106	case '$':
107		if (check_inverse(argv[optind-1], &invert, &optind, 0)) break;
108		if (string_to_number(optarg, 0, 10000, &num) == -1)
109			exit_error(PARAMETER_PROBLEM,
110				   "bad --limit-burst `%s'", optarg);
111		r->burst = num;
112		break;
113
114	default:
115		return 0;
116	}
117
118	if (invert)
119		exit_error(PARAMETER_PROBLEM,
120			   "limit does not support invert");
121
122	return 1;
123}
124
125/* Final check; nothing. */
126static void final_check(unsigned int flags)
127{
128}
129
130static struct rates
131{
132	const char *name;
133	u_int32_t mult;
134} rates[] = { { "day", IPT_LIMIT_SCALE*24*60*60 },
135	      { "hour", IPT_LIMIT_SCALE*60*60 },
136	      { "min", IPT_LIMIT_SCALE*60 },
137	      { "sec", IPT_LIMIT_SCALE } };
138
139static void print_rate(u_int32_t period)
140{
141	unsigned int i;
142
143	for (i = 1; i < sizeof(rates)/sizeof(struct rates); i++) {
144		if (period > rates[i].mult
145            || rates[i].mult/period < rates[i].mult%period)
146			break;
147	}
148
149	printf("%u/%s ", rates[i-1].mult / period, rates[i-1].name);
150}
151
152/* Prints out the matchinfo. */
153static void
154print(const struct ipt_ip *ip,
155      const struct ipt_entry_match *match,
156      int numeric)
157{
158	struct ipt_rateinfo *r = (struct ipt_rateinfo *)match->data;
159	printf("limit: avg "); print_rate(r->avg);
160	printf("burst %u ", r->burst);
161}
162
163static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
164{
165	struct ipt_rateinfo *r = (struct ipt_rateinfo *)match->data;
166
167	printf("--limit "); print_rate(r->avg);
168	if (r->burst != IPT_LIMIT_BURST)
169		printf("--limit-burst %u ", r->burst);
170}
171
172static struct iptables_match limit = {
173	.next		= NULL,
174	.name		= "limit",
175	.version	= IPTABLES_VERSION,
176	.size		= IPT_ALIGN(sizeof(struct ipt_rateinfo)),
177	.userspacesize	= offsetof(struct ipt_rateinfo, prev),
178	.help		= &help,
179	.init		= &init,
180	.parse		= &parse,
181	.final_check	= &final_check,
182	.print		= &print,
183	.save		= &save,
184	.extra_opts	= opts
185};
186
187void _init(void)
188{
189	register_match(&limit);
190}
191