blob: 838075413a6287198bec0c7cba6fbdd7ef27100d [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>
10#include <iptables.h>
11
Phil Oesterae353092006-08-08 09:59:59 +000012#include <linux/netfilter/xt_quota.h>
Harald Weltee746abb2001-12-03 22:22:55 +000013#include <linux/netfilter_ipv4/ip_tables.h>
14
15static struct option opts[] = {
16 {"quota", 1, 0, '1'},
17 {0}
18};
19
20/* print usage */
21static void
22help(void)
23{
24 printf("quota options:\n"
25 " --quota quota quota (bytes)\n" "\n");
26}
27
Harald Weltee746abb2001-12-03 22:22:55 +000028/* print matchinfo */
29static void
30print(const struct ipt_ip *ip, const struct ipt_entry_match *match, int numeric)
31{
Phil Oesterae353092006-08-08 09:59:59 +000032 struct xt_quota_info *q = (struct xt_quota_info *) match->data;
Harald Weltee746abb2001-12-03 22:22:55 +000033 printf("quota: %llu bytes", (unsigned long long) q->quota);
34}
35
36/* save matchinfo */
37static void
38save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
39{
Phil Oesterae353092006-08-08 09:59:59 +000040 struct xt_quota_info *q = (struct xt_quota_info *) match->data;
Harald Weltee746abb2001-12-03 22:22:55 +000041 printf("--quota %llu ", (unsigned long long) q->quota);
42}
43
44/* parse quota option */
45static int
46parse_quota(const char *s, u_int64_t * quota)
47{
48 *quota = strtoull(s, (char **) NULL, 10);
49
50#ifdef DEBUG_IPT_QUOTA
51 printf("Quota: %llu\n", *quota);
52#endif
53
54 if (*quota == -1)
55 exit_error(PARAMETER_PROBLEM, "quota invalid: '%s'\n", s);
56 else
57 return 1;
58}
59
60/* parse all options, returning true if we found any for us */
61static int
62parse(int c, char **argv, int invert, unsigned int *flags,
63 const struct ipt_entry *entry,
64 unsigned int *nfcache, struct ipt_entry_match **match)
65{
Phil Oesterae353092006-08-08 09:59:59 +000066 struct xt_quota_info *info = (struct xt_quota_info *) (*match)->data;
Harald Weltee746abb2001-12-03 22:22:55 +000067
68 switch (c) {
69 case '1':
Harald Welteb77f1da2002-03-14 11:35:58 +000070 if (check_inverse(optarg, &invert, NULL, 0))
Harald Weltee746abb2001-12-03 22:22:55 +000071 exit_error(PARAMETER_PROBLEM, "quota: unexpected '!'");
72 if (!parse_quota(optarg, &info->quota))
73 exit_error(PARAMETER_PROBLEM,
74 "bad quota: '%s'", optarg);
75 break;
76
77 default:
78 return 0;
79 }
80 return 1;
81}
82
83/* no final check */
84static void
85final_check(unsigned int flags)
86{
87}
88
Pablo Neira8caee8b2004-12-28 13:11:59 +000089struct iptables_match quota = {
90 .next = NULL,
91 .name = "quota",
92 .version = IPTABLES_VERSION,
Phil Oesterae353092006-08-08 09:59:59 +000093 .size = IPT_ALIGN(sizeof (struct xt_quota_info)),
94 .userspacesize = offsetof(struct xt_quota_info, quota),
Pablo Neira8caee8b2004-12-28 13:11:59 +000095 .help = &help,
Pablo Neira8caee8b2004-12-28 13:11:59 +000096 .parse = &parse,
97 .final_check = &final_check,
98 .print = &print,
99 .save = &save,
100 .extra_opts = opts
Harald Weltee746abb2001-12-03 22:22:55 +0000101};
102
103void
104_init(void)
105{
106 register_match(&quota);
107}