libxt_quota.c revision 9c60365e043a430f74115bbfaf58ce0df7585f49
1/*
2 * Shared library add-on to iptables to add quota support
3 *
4 * Sam Johnston <samj@samj.net>
5 */
6#include <stdbool.h>
7#include <stddef.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <getopt.h>
11#include <xtables.h>
12
13#include <linux/netfilter/xt_quota.h>
14
15static const struct option quota_opts[] = {
16	{.name = "quota", .has_arg = true, .val = '1'},
17	XT_GETOPT_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
43/* parse quota option */
44static int
45parse_quota(const char *s, uint64_t * quota)
46{
47	*quota = strtoull(s, NULL, 10);
48
49#ifdef DEBUG_XT_QUOTA
50	printf("Quota: %llu\n", *quota);
51#endif
52
53	if (*quota == UINT64_MAX)
54		xtables_error(PARAMETER_PROBLEM, "quota invalid: '%s'\n", s);
55	else
56		return 1;
57}
58
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 (xtables_check_inverse(optarg, &invert, NULL, 0, argv))
68			xtables_error(PARAMETER_PROBLEM, "quota: unexpected '!'");
69		if (!parse_quota(optarg, &info->quota))
70			xtables_error(PARAMETER_PROBLEM,
71				   "bad quota: '%s'", optarg);
72
73		if (invert)
74			info->flags |= XT_QUOTA_INVERT;
75
76		break;
77	}
78	return 1;
79}
80
81static struct xtables_match quota_match = {
82	.family		= NFPROTO_UNSPEC,
83	.name		= "quota",
84	.version	= XTABLES_VERSION,
85	.size		= XT_ALIGN(sizeof (struct xt_quota_info)),
86	.userspacesize	= offsetof(struct xt_quota_info, master),
87	.help		= quota_help,
88	.parse		= quota_parse,
89	.print		= quota_print,
90	.save		= quota_save,
91	.extra_opts	= quota_opts,
92};
93
94void
95_init(void)
96{
97	xtables_register_match(&quota_match);
98}
99