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