blob: 988f404f5f128cac4f24dfa752d7591bee04bd51 [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,
16 .flags = XTOPT_MAND | XTOPT_INVERT},
17 XTOPT_TABLEEND,
Harald Weltee746abb2001-12-03 22:22:55 +000018};
19
Jan Engelhardt181dead2007-10-04 16:27:07 +000020static void quota_help(void)
Harald Weltee746abb2001-12-03 22:22:55 +000021{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020022 printf("quota match options:\n"
Jan Engelhardtce06c992010-07-02 12:19:18 +020023 "[!] --quota quota quota (bytes)\n");
Harald Weltee746abb2001-12-03 22:22:55 +000024}
25
Harald Weltee746abb2001-12-03 22:22:55 +000026static void
Jan Engelhardt181dead2007-10-04 16:27:07 +000027quota_print(const void *ip, const struct xt_entry_match *match, int numeric)
Harald Weltee746abb2001-12-03 22:22:55 +000028{
Jan Engelhardt69f564e2009-05-26 13:14:06 +020029 const struct xt_quota_info *q = (const void *)match->data;
Jan Engelhardt73866352010-12-18 02:04:59 +010030 printf(" quota: %llu bytes", (unsigned long long)q->quota);
Harald Weltee746abb2001-12-03 22:22:55 +000031}
32
Harald Weltee746abb2001-12-03 22:22:55 +000033static void
Jan Engelhardt181dead2007-10-04 16:27:07 +000034quota_save(const void *ip, const struct xt_entry_match *match)
Harald Weltee746abb2001-12-03 22:22:55 +000035{
Jan Engelhardt69f564e2009-05-26 13:14:06 +020036 const struct xt_quota_info *q = (const void *)match->data;
Jan Engelhardt9c603652011-01-18 11:02:04 +010037
38 if (q->flags & XT_QUOTA_INVERT)
39 printf("! ");
Jan Engelhardt73866352010-12-18 02:04:59 +010040 printf(" --quota %llu", (unsigned long long) q->quota);
Harald Weltee746abb2001-12-03 22:22:55 +000041}
42
Jan Engelhardt1e6c1ee2011-03-06 16:58:24 +010043static void quota_parse(struct xt_option_call *cb)
Harald Weltee746abb2001-12-03 22:22:55 +000044{
Jan Engelhardt1e6c1ee2011-03-06 16:58:24 +010045 struct xt_quota_info *info = cb->data;
Harald Weltee746abb2001-12-03 22:22:55 +000046
Jan Engelhardt1e6c1ee2011-03-06 16:58:24 +010047 xtables_option_parse(cb);
48 if (cb->invert)
49 info->flags |= XT_QUOTA_INVERT;
Jan Engelhardt8a5270b2011-02-20 20:30:56 +010050}
51
Patrick McHardy0ea82bc2008-06-07 15:15:29 +020052static struct xtables_match quota_match = {
Jan Engelhardt42979362009-06-01 11:56:23 +020053 .family = NFPROTO_UNSPEC,
Yasuyuki KOZAKAI2bcb1602007-07-24 07:03:59 +000054 .name = "quota",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020055 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI2bcb1602007-07-24 07:03:59 +000056 .size = XT_ALIGN(sizeof (struct xt_quota_info)),
Changli Gao422342e2010-08-02 18:03:20 +020057 .userspacesize = offsetof(struct xt_quota_info, master),
Jan Engelhardt181dead2007-10-04 16:27:07 +000058 .help = quota_help,
Jan Engelhardt181dead2007-10-04 16:27:07 +000059 .print = quota_print,
60 .save = quota_save,
Jan Engelhardt1e6c1ee2011-03-06 16:58:24 +010061 .x6_parse = quota_parse,
62 .x6_options = quota_opts,
Harald Weltee746abb2001-12-03 22:22:55 +000063};
64
65void
66_init(void)
67{
Jan Engelhardt181dead2007-10-04 16:27:07 +000068 xtables_register_match(&quota_match);
Harald Weltee746abb2001-12-03 22:22:55 +000069}