blob: bad77d236e6b8899e35acc94b957d740d885fead [file] [log] [blame]
Harald Weltee746abb2001-12-03 22:22:55 +00001/*
2 * Shared library add-on to iptables to add quota support
3 *
4 * Sam Johnston <samj@samj.net>
5 */
6#include <stdio.h>
Yasuyuki KOZAKAI2bcb1602007-07-24 07:03:59 +00007#include <xtables.h>
Phil Oesterae353092006-08-08 09:59:59 +00008#include <linux/netfilter/xt_quota.h>
Harald Weltee746abb2001-12-03 22:22:55 +00009
Jan Engelhardt1e6c1ee2011-03-06 16:58:24 +010010enum {
11 O_QUOTA = 0,
12};
13
14static const struct xt_option_entry quota_opts[] = {
15 {.name = "quota", .id = O_QUOTA, .type = XTTYPE_UINT64,
Jan Engelhardt67db7612011-05-20 16:01:18 +020016 .flags = XTOPT_MAND | XTOPT_INVERT | XTOPT_PUT,
17 XTOPT_POINTER(struct xt_quota_info, quota)},
Jan Engelhardt1e6c1ee2011-03-06 16:58:24 +010018 XTOPT_TABLEEND,
Harald Weltee746abb2001-12-03 22:22:55 +000019};
20
Jan Engelhardt181dead2007-10-04 16:27:07 +000021static void quota_help(void)
Harald Weltee746abb2001-12-03 22:22:55 +000022{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020023 printf("quota match options:\n"
Jan Engelhardtce06c992010-07-02 12:19:18 +020024 "[!] --quota quota quota (bytes)\n");
Harald Weltee746abb2001-12-03 22:22:55 +000025}
26
Harald Weltee746abb2001-12-03 22:22:55 +000027static void
Jan Engelhardt181dead2007-10-04 16:27:07 +000028quota_print(const void *ip, const struct xt_entry_match *match, int numeric)
Harald Weltee746abb2001-12-03 22:22:55 +000029{
Jan Engelhardt69f564e2009-05-26 13:14:06 +020030 const struct xt_quota_info *q = (const void *)match->data;
Jan Engelhardt73866352010-12-18 02:04:59 +010031 printf(" quota: %llu bytes", (unsigned long long)q->quota);
Harald Weltee746abb2001-12-03 22:22:55 +000032}
33
Harald Weltee746abb2001-12-03 22:22:55 +000034static void
Jan Engelhardt181dead2007-10-04 16:27:07 +000035quota_save(const void *ip, const struct xt_entry_match *match)
Harald Weltee746abb2001-12-03 22:22:55 +000036{
Jan Engelhardt69f564e2009-05-26 13:14:06 +020037 const struct xt_quota_info *q = (const void *)match->data;
Jan Engelhardt9c603652011-01-18 11:02:04 +010038
39 if (q->flags & XT_QUOTA_INVERT)
Arturo Borrero379bd7b2015-02-23 11:31:11 +010040 printf(" !");
Jan Engelhardt73866352010-12-18 02:04:59 +010041 printf(" --quota %llu", (unsigned long long) q->quota);
Harald Weltee746abb2001-12-03 22:22:55 +000042}
43
Jan Engelhardt1e6c1ee2011-03-06 16:58:24 +010044static void quota_parse(struct xt_option_call *cb)
Harald Weltee746abb2001-12-03 22:22:55 +000045{
Jan Engelhardt1e6c1ee2011-03-06 16:58:24 +010046 struct xt_quota_info *info = cb->data;
Harald Weltee746abb2001-12-03 22:22:55 +000047
Jan Engelhardt1e6c1ee2011-03-06 16:58:24 +010048 xtables_option_parse(cb);
49 if (cb->invert)
50 info->flags |= XT_QUOTA_INVERT;
Jan Engelhardt8a5270b2011-02-20 20:30:56 +010051}
52
Liping Zhang92a4ff62016-10-07 19:08:54 +080053static 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
Patrick McHardy0ea82bc2008-06-07 15:15:29 +020064static struct xtables_match quota_match = {
Jan Engelhardt42979362009-06-01 11:56:23 +020065 .family = NFPROTO_UNSPEC,
Yasuyuki KOZAKAI2bcb1602007-07-24 07:03:59 +000066 .name = "quota",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020067 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI2bcb1602007-07-24 07:03:59 +000068 .size = XT_ALIGN(sizeof (struct xt_quota_info)),
Changli Gao422342e2010-08-02 18:03:20 +020069 .userspacesize = offsetof(struct xt_quota_info, master),
Jan Engelhardt181dead2007-10-04 16:27:07 +000070 .help = quota_help,
Jan Engelhardt181dead2007-10-04 16:27:07 +000071 .print = quota_print,
72 .save = quota_save,
Jan Engelhardt1e6c1ee2011-03-06 16:58:24 +010073 .x6_parse = quota_parse,
74 .x6_options = quota_opts,
Liping Zhang92a4ff62016-10-07 19:08:54 +080075 .xlate = quota_xlate,
Harald Weltee746abb2001-12-03 22:22:55 +000076};
77
78void
79_init(void)
80{
Jan Engelhardt181dead2007-10-04 16:27:07 +000081 xtables_register_match(&quota_match);
Harald Weltee746abb2001-12-03 22:22:55 +000082}