libxt_quota.c revision bf97128c7262f17a02fec41cdae75b472ba77f88
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		break;
69
70	default:
71		return 0;
72	}
73	return 1;
74}
75
76static struct xtables_match quota_match = {
77	.family		= NFPROTO_UNSPEC,
78	.name		= "quota",
79	.version	= XTABLES_VERSION,
80	.size		= XT_ALIGN(sizeof (struct xt_quota_info)),
81	.userspacesize	= offsetof(struct xt_quota_info, quota),
82	.help		= quota_help,
83	.parse		= quota_parse,
84	.print		= quota_print,
85	.save		= quota_save,
86	.extra_opts	= quota_opts,
87};
88
89void
90_init(void)
91{
92	xtables_register_match(&quota_match);
93}
94