blob: 90da1cd439c284d12ef99b5709cb83c07e24faca [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{
Max Kellermann5b76f682008-01-29 13:42:48 +000028 struct xt_quota_info *q = (struct xt_quota_info *) match->data;
29 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{
Max Kellermann5b76f682008-01-29 13:42:48 +000035 struct xt_quota_info *q = (struct xt_quota_info *) match->data;
36 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)
Max Kellermann5b76f682008-01-29 13:42:48 +000050 exit_error(PARAMETER_PROBLEM, "quota invalid: '%s'\n", s);
51 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':
63 if (check_inverse(optarg, &invert, NULL, 0))
64 exit_error(PARAMETER_PROBLEM, "quota: unexpected '!'");
65 if (!parse_quota(optarg, &info->quota))
66 exit_error(PARAMETER_PROBLEM,
67 "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 Engelhardt23545c22008-02-14 04:23:04 +010077 .family = AF_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}