libxt_quota.c revision 5b76f682f722bebc2f0616fca4600eee2c08dfe2
1/*
2 * Shared library add-on to iptables to add quota support
3 *
4 * Sam Johnston <samj@samj.net>
5 */
6#include <stddef.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <getopt.h>
10#include <xtables.h>
11
12#include <linux/netfilter/xt_quota.h>
13
14static const struct option quota_opts[] = {
15	{"quota", 1, NULL, '1'},
16	{ }
17};
18
19/* print usage */
20static void quota_help(void)
21{
22	printf("quota options:\n"
23	       " --quota quota			quota (bytes)\n" "\n");
24}
25
26/* print matchinfo */
27static void
28quota_print(const void *ip, const struct xt_entry_match *match, int numeric)
29{
30	struct xt_quota_info *q = (struct xt_quota_info *) match->data;
31	printf("quota: %llu bytes", (unsigned long long) q->quota);
32}
33
34/* save matchinfo */
35static void
36quota_save(const void *ip, const struct xt_entry_match *match)
37{
38	struct xt_quota_info *q = (struct xt_quota_info *) match->data;
39	printf("--quota %llu ", (unsigned long long) q->quota);
40}
41
42/* parse quota option */
43static int
44parse_quota(const char *s, u_int64_t * quota)
45{
46	*quota = strtoull(s, (char **) NULL, 10);
47
48#ifdef DEBUG_XT_QUOTA
49	printf("Quota: %llu\n", *quota);
50#endif
51
52	if (*quota == -1)
53		exit_error(PARAMETER_PROBLEM, "quota invalid: '%s'\n", s);
54	else
55		return 1;
56}
57
58/* parse all options, returning true if we found any for us */
59static int
60quota_parse(int c, char **argv, int invert, unsigned int *flags,
61	    const void *entry, struct xt_entry_match **match)
62{
63	struct xt_quota_info *info = (struct xt_quota_info *) (*match)->data;
64
65	switch (c) {
66	case '1':
67		if (check_inverse(optarg, &invert, NULL, 0))
68			exit_error(PARAMETER_PROBLEM, "quota: unexpected '!'");
69		if (!parse_quota(optarg, &info->quota))
70			exit_error(PARAMETER_PROBLEM,
71				   "bad quota: '%s'", optarg);
72		break;
73
74	default:
75		return 0;
76	}
77	return 1;
78}
79
80struct xtables_match quota_match = {
81	.family		= AF_INET,
82	.name		= "quota",
83	.version	= IPTABLES_VERSION,
84	.size		= XT_ALIGN(sizeof (struct xt_quota_info)),
85	.userspacesize	= offsetof(struct xt_quota_info, quota),
86	.help		= quota_help,
87	.parse		= quota_parse,
88	.print		= quota_print,
89	.save		= quota_save,
90	.extra_opts	= quota_opts,
91};
92
93struct xtables_match quota_match6 = {
94	.family		= AF_INET6,
95	.name		= "quota",
96	.version	= IPTABLES_VERSION,
97	.size		= XT_ALIGN(sizeof (struct xt_quota_info)),
98	.userspacesize	= offsetof(struct xt_quota_info, quota),
99	.help		= quota_help,
100	.parse		= quota_parse,
101	.print		= quota_print,
102	.save		= quota_save,
103	.extra_opts	= quota_opts,
104};
105
106void
107_init(void)
108{
109	xtables_register_match(&quota_match);
110	xtables_register_match(&quota_match6);
111}
112