blob: 28e16e6140da27e080528c2de595715eb3814e6a [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>
7#include <stdlib.h>
8#include <getopt.h>
9#include <iptables.h>
10
11#include <linux/netfilter_ipv4/ipt_quota.h>
12#include <linux/netfilter_ipv4/ip_tables.h>
13
14static struct option opts[] = {
15 {"quota", 1, 0, '1'},
16 {0}
17};
18
19/* print usage */
20static void
21help(void)
22{
23 printf("quota options:\n"
24 " --quota quota quota (bytes)\n" "\n");
25}
26
27/* initialise match */
28static void
29init(struct ipt_entry_match *m, unsigned int *nfcache)
30{
31 /* no can cache */
32 *nfcache |= NFC_UNKNOWN;
33}
34
35/* print matchinfo */
36static void
37print(const struct ipt_ip *ip, const struct ipt_entry_match *match, int numeric)
38{
39 struct ipt_quota_info *q = (struct ipt_quota_info *) match->data;
40 printf("quota: %llu bytes", (unsigned long long) q->quota);
41}
42
43/* save matchinfo */
44static void
45save(const struct ipt_ip *ip, const struct ipt_entry_match *match)
46{
47 struct ipt_quota_info *q = (struct ipt_quota_info *) match->data;
48 printf("--quota %llu ", (unsigned long long) q->quota);
49}
50
51/* parse quota option */
52static int
53parse_quota(const char *s, u_int64_t * quota)
54{
55 *quota = strtoull(s, (char **) NULL, 10);
56
57#ifdef DEBUG_IPT_QUOTA
58 printf("Quota: %llu\n", *quota);
59#endif
60
61 if (*quota == -1)
62 exit_error(PARAMETER_PROBLEM, "quota invalid: '%s'\n", s);
63 else
64 return 1;
65}
66
67/* parse all options, returning true if we found any for us */
68static int
69parse(int c, char **argv, int invert, unsigned int *flags,
70 const struct ipt_entry *entry,
71 unsigned int *nfcache, struct ipt_entry_match **match)
72{
73 struct ipt_quota_info *info = (struct ipt_quota_info *) (*match)->data;
74
75 switch (c) {
76 case '1':
77 if (check_inverse(optarg, &invert))
78 exit_error(PARAMETER_PROBLEM, "quota: unexpected '!'");
79 if (!parse_quota(optarg, &info->quota))
80 exit_error(PARAMETER_PROBLEM,
81 "bad quota: '%s'", optarg);
82 break;
83
84 default:
85 return 0;
86 }
87 return 1;
88}
89
90/* no final check */
91static void
92final_check(unsigned int flags)
93{
94}
95
96struct iptables_match quota = { NULL,
97 "quota",
98 NETFILTER_VERSION,
99 IPT_ALIGN(sizeof (struct ipt_quota_info)),
100 IPT_ALIGN(sizeof (struct ipt_quota_info)),
101 &help,
102 &init,
103 &parse,
104 &final_check,
105 &print,
106 &save,
107 opts
108};
109
110void
111_init(void)
112{
113 register_match(&quota);
114}