libxt_quota.c revision 1e6c1ee1bf2822d5fdf61725148700a410fb8b86
1/*
2 * Shared library add-on to iptables to add quota support
3 *
4 * Sam Johnston <samj@samj.net>
5 */
6#include <stdio.h>
7#include <xtables.h>
8#include <linux/netfilter/xt_quota.h>
9
10enum {
11	O_QUOTA = 0,
12};
13
14static const struct xt_option_entry quota_opts[] = {
15	{.name = "quota", .id = O_QUOTA, .type = XTTYPE_UINT64,
16	 .flags = XTOPT_MAND | XTOPT_INVERT},
17	XTOPT_TABLEEND,
18};
19
20static void quota_help(void)
21{
22	printf("quota match options:\n"
23	       "[!] --quota quota		quota (bytes)\n");
24}
25
26static void
27quota_print(const void *ip, const struct xt_entry_match *match, int numeric)
28{
29	const struct xt_quota_info *q = (const void *)match->data;
30	printf(" quota: %llu bytes", (unsigned long long)q->quota);
31}
32
33static void
34quota_save(const void *ip, const struct xt_entry_match *match)
35{
36	const struct xt_quota_info *q = (const void *)match->data;
37
38	if (q->flags & XT_QUOTA_INVERT)
39		printf("! ");
40	printf(" --quota %llu", (unsigned long long) q->quota);
41}
42
43static void quota_parse(struct xt_option_call *cb)
44{
45	struct xt_quota_info *info = cb->data;
46
47	xtables_option_parse(cb);
48	if (cb->invert)
49		info->flags |= XT_QUOTA_INVERT;
50}
51
52static struct xtables_match quota_match = {
53	.family		= NFPROTO_UNSPEC,
54	.name		= "quota",
55	.version	= XTABLES_VERSION,
56	.size		= XT_ALIGN(sizeof (struct xt_quota_info)),
57	.userspacesize	= offsetof(struct xt_quota_info, master),
58	.help		= quota_help,
59	.print		= quota_print,
60	.save		= quota_save,
61	.x6_parse	= quota_parse,
62	.x6_options	= quota_opts,
63};
64
65void
66_init(void)
67{
68	xtables_register_match(&quota_match);
69}
70