libxt_quota.c revision e4540fcb86c2d7f4cdf51c49872847a03a11b433
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	{ .name = NULL }
17};
18
19static void quota_help(void)
20{
21	printf("quota match options:\n"
22	       " --quota quota			quota (bytes)\n");
23}
24
25static void
26quota_print(const void *ip, const struct xt_entry_match *match, int numeric)
27{
28	const struct xt_quota_info *q = (const void *)match->data;
29	printf("quota: %llu bytes", (unsigned long long) q->quota);
30}
31
32static void
33quota_save(const void *ip, const struct xt_entry_match *match)
34{
35	const struct xt_quota_info *q = (const void *)match->data;
36	printf("--quota %llu ", (unsigned long long) q->quota);
37}
38
39/* parse quota option */
40static int
41parse_quota(const char *s, u_int64_t * quota)
42{
43	*quota = strtoull(s, NULL, 10);
44
45#ifdef DEBUG_XT_QUOTA
46	printf("Quota: %llu\n", *quota);
47#endif
48
49	if (*quota == UINT64_MAX)
50		xtables_error(PARAMETER_PROBLEM, "quota invalid: '%s'\n", s);
51	else
52		return 1;
53}
54
55static int
56quota_parse(int c, char **argv, int invert, unsigned int *flags,
57	    const void *entry, struct xt_entry_match **match)
58{
59	struct xt_quota_info *info = (struct xt_quota_info *) (*match)->data;
60
61	switch (c) {
62	case '1':
63		if (xtables_check_inverse(optarg, &invert, NULL, 0, argv))
64			xtables_error(PARAMETER_PROBLEM, "quota: unexpected '!'");
65		if (!parse_quota(optarg, &info->quota))
66			xtables_error(PARAMETER_PROBLEM,
67				   "bad quota: '%s'", optarg);
68
69		if (invert)
70			info->flags |= XT_QUOTA_INVERT;
71
72		break;
73
74	default:
75		return 0;
76	}
77	return 1;
78}
79
80static struct xtables_match quota_match = {
81	.family		= NFPROTO_UNSPEC,
82	.name		= "quota",
83	.version	= XTABLES_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
93void
94_init(void)
95{
96	xtables_register_match(&quota_match);
97}
98