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