blob: 0ccc94bbc98df093019632846db7ed6f0717a72c [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 */
Phil Oesterae353092006-08-08 09:59:59 +00006#include <stddef.h>
Harald Weltee746abb2001-12-03 22:22:55 +00007#include <stdio.h>
8#include <stdlib.h>
9#include <getopt.h>
Yasuyuki KOZAKAI2bcb1602007-07-24 07:03:59 +000010#include <xtables.h>
Harald Weltee746abb2001-12-03 22:22:55 +000011
Phil Oesterae353092006-08-08 09:59:59 +000012#include <linux/netfilter/xt_quota.h>
Harald Weltee746abb2001-12-03 22:22:55 +000013
Jan Engelhardt181dead2007-10-04 16:27:07 +000014static const struct option quota_opts[] = {
Max Kellermann5b76f682008-01-29 13:42:48 +000015 {"quota", 1, NULL, '1'},
Max Kellermann9ee386a2008-01-29 13:48:05 +000016 { .name = NULL }
Harald Weltee746abb2001-12-03 22:22:55 +000017};
18
Jan Engelhardt181dead2007-10-04 16:27:07 +000019static void quota_help(void)
Harald Weltee746abb2001-12-03 22:22:55 +000020{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020021 printf("quota match options:\n"
22 " --quota quota quota (bytes)\n");
Harald Weltee746abb2001-12-03 22:22:55 +000023}
24
Harald Weltee746abb2001-12-03 22:22:55 +000025static void
Jan Engelhardt181dead2007-10-04 16:27:07 +000026quota_print(const void *ip, const struct xt_entry_match *match, int numeric)
Harald Weltee746abb2001-12-03 22:22:55 +000027{
Jan Engelhardt69f564e2009-05-26 13:14:06 +020028 const struct xt_quota_info *q = (const void *)match->data;
Max Kellermann5b76f682008-01-29 13:42:48 +000029 printf("quota: %llu bytes", (unsigned long long) q->quota);
Harald Weltee746abb2001-12-03 22:22:55 +000030}
31
Harald Weltee746abb2001-12-03 22:22:55 +000032static void
Jan Engelhardt181dead2007-10-04 16:27:07 +000033quota_save(const void *ip, const struct xt_entry_match *match)
Harald Weltee746abb2001-12-03 22:22:55 +000034{
Jan Engelhardt69f564e2009-05-26 13:14:06 +020035 const struct xt_quota_info *q = (const void *)match->data;
Max Kellermann5b76f682008-01-29 13:42:48 +000036 printf("--quota %llu ", (unsigned long long) q->quota);
Harald Weltee746abb2001-12-03 22:22:55 +000037}
38
39/* parse quota option */
40static int
41parse_quota(const char *s, u_int64_t * quota)
42{
Jan Engelhardt7a236f42008-03-03 12:30:41 +010043 *quota = strtoull(s, NULL, 10);
Harald Weltee746abb2001-12-03 22:22:55 +000044
Yasuyuki KOZAKAI2bcb1602007-07-24 07:03:59 +000045#ifdef DEBUG_XT_QUOTA
Max Kellermann5b76f682008-01-29 13:42:48 +000046 printf("Quota: %llu\n", *quota);
Harald Weltee746abb2001-12-03 22:22:55 +000047#endif
48
Jan Engelhardta8097542009-01-27 17:39:01 +010049 if (*quota == UINT64_MAX)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010050 xtables_error(PARAMETER_PROBLEM, "quota invalid: '%s'\n", s);
Max Kellermann5b76f682008-01-29 13:42:48 +000051 else
52 return 1;
Harald Weltee746abb2001-12-03 22:22:55 +000053}
54
Harald Weltee746abb2001-12-03 22:22:55 +000055static int
Jan Engelhardt181dead2007-10-04 16:27:07 +000056quota_parse(int c, char **argv, int invert, unsigned int *flags,
Max Kellermann5b76f682008-01-29 13:42:48 +000057 const void *entry, struct xt_entry_match **match)
Harald Weltee746abb2001-12-03 22:22:55 +000058{
Max Kellermann5b76f682008-01-29 13:42:48 +000059 struct xt_quota_info *info = (struct xt_quota_info *) (*match)->data;
Harald Weltee746abb2001-12-03 22:22:55 +000060
Max Kellermann5b76f682008-01-29 13:42:48 +000061 switch (c) {
62 case '1':
Jan Engelhardt0f16c722009-01-30 04:55:38 +010063 if (xtables_check_inverse(optarg, &invert, NULL, 0))
Jan Engelhardt1829ed42009-02-21 03:29:44 +010064 xtables_error(PARAMETER_PROBLEM, "quota: unexpected '!'");
Max Kellermann5b76f682008-01-29 13:42:48 +000065 if (!parse_quota(optarg, &info->quota))
Jan Engelhardt1829ed42009-02-21 03:29:44 +010066 xtables_error(PARAMETER_PROBLEM,
Max Kellermann5b76f682008-01-29 13:42:48 +000067 "bad quota: '%s'", optarg);
68 break;
Harald Weltee746abb2001-12-03 22:22:55 +000069
Max Kellermann5b76f682008-01-29 13:42:48 +000070 default:
71 return 0;
72 }
73 return 1;
Harald Weltee746abb2001-12-03 22:22:55 +000074}
75
Patrick McHardy0ea82bc2008-06-07 15:15:29 +020076static struct xtables_match quota_match = {
Jan Engelhardt42979362009-06-01 11:56:23 +020077 .family = NFPROTO_UNSPEC,
Yasuyuki KOZAKAI2bcb1602007-07-24 07:03:59 +000078 .name = "quota",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020079 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI2bcb1602007-07-24 07:03:59 +000080 .size = XT_ALIGN(sizeof (struct xt_quota_info)),
Phil Oesterae353092006-08-08 09:59:59 +000081 .userspacesize = offsetof(struct xt_quota_info, quota),
Jan Engelhardt181dead2007-10-04 16:27:07 +000082 .help = quota_help,
83 .parse = quota_parse,
84 .print = quota_print,
85 .save = quota_save,
86 .extra_opts = quota_opts,
Harald Weltee746abb2001-12-03 22:22:55 +000087};
88
89void
90_init(void)
91{
Jan Engelhardt181dead2007-10-04 16:27:07 +000092 xtables_register_match(&quota_match);
Harald Weltee746abb2001-12-03 22:22:55 +000093}