blob: ab4fb96fb34537b6c4409b6476077d78eef10e3c [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 Engelhardt661f1122007-07-30 14:46:51 +000014static const struct option opts[] = {
Harald Weltee746abb2001-12-03 22:22:55 +000015 {"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
Harald Weltee746abb2001-12-03 22:22:55 +000027/* print matchinfo */
28static void
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +000029print(const void *ip, const struct xt_entry_match *match, int numeric)
Harald Weltee746abb2001-12-03 22:22:55 +000030{
Phil Oesterae353092006-08-08 09:59:59 +000031 struct xt_quota_info *q = (struct xt_quota_info *) match->data;
Harald Weltee746abb2001-12-03 22:22:55 +000032 printf("quota: %llu bytes", (unsigned long long) q->quota);
33}
34
35/* save matchinfo */
36static void
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +000037save(const void *ip, const struct xt_entry_match *match)
Harald Weltee746abb2001-12-03 22:22:55 +000038{
Phil Oesterae353092006-08-08 09:59:59 +000039 struct xt_quota_info *q = (struct xt_quota_info *) match->data;
Harald Weltee746abb2001-12-03 22:22:55 +000040 printf("--quota %llu ", (unsigned long long) q->quota);
41}
42
43/* parse quota option */
44static int
45parse_quota(const char *s, u_int64_t * quota)
46{
47 *quota = strtoull(s, (char **) NULL, 10);
48
Yasuyuki KOZAKAI2bcb1602007-07-24 07:03:59 +000049#ifdef DEBUG_XT_QUOTA
Harald Weltee746abb2001-12-03 22:22:55 +000050 printf("Quota: %llu\n", *quota);
51#endif
52
53 if (*quota == -1)
54 exit_error(PARAMETER_PROBLEM, "quota invalid: '%s'\n", s);
55 else
56 return 1;
57}
58
59/* parse all options, returning true if we found any for us */
60static int
61parse(int c, char **argv, int invert, unsigned int *flags,
Yasuyuki KOZAKAIc0a9ab92007-07-24 06:02:05 +000062 const void *entry,
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +000063 unsigned int *nfcache, struct xt_entry_match **match)
Harald Weltee746abb2001-12-03 22:22:55 +000064{
Phil Oesterae353092006-08-08 09:59:59 +000065 struct xt_quota_info *info = (struct xt_quota_info *) (*match)->data;
Harald Weltee746abb2001-12-03 22:22:55 +000066
67 switch (c) {
68 case '1':
Harald Welteb77f1da2002-03-14 11:35:58 +000069 if (check_inverse(optarg, &invert, NULL, 0))
Harald Weltee746abb2001-12-03 22:22:55 +000070 exit_error(PARAMETER_PROBLEM, "quota: unexpected '!'");
71 if (!parse_quota(optarg, &info->quota))
72 exit_error(PARAMETER_PROBLEM,
73 "bad quota: '%s'", optarg);
74 break;
75
76 default:
77 return 0;
78 }
79 return 1;
80}
81
82/* no final check */
83static void
84final_check(unsigned int flags)
85{
86}
87
Yasuyuki KOZAKAI2bcb1602007-07-24 07:03:59 +000088struct xtables_match quota = {
Yasuyuki KOZAKAI2bcb1602007-07-24 07:03:59 +000089 .family = AF_INET,
Pablo Neira8caee8b2004-12-28 13:11:59 +000090 .name = "quota",
91 .version = IPTABLES_VERSION,
Yasuyuki KOZAKAI2bcb1602007-07-24 07:03:59 +000092 .size = XT_ALIGN(sizeof (struct xt_quota_info)),
93 .userspacesize = offsetof(struct xt_quota_info, quota),
94 .help = &help,
95 .parse = &parse,
96 .final_check = &final_check,
97 .print = &print,
98 .save = &save,
99 .extra_opts = opts
100};
101
102struct xtables_match quota6 = {
Yasuyuki KOZAKAI2bcb1602007-07-24 07:03:59 +0000103 .family = AF_INET6,
104 .name = "quota",
105 .version = IPTABLES_VERSION,
106 .size = XT_ALIGN(sizeof (struct xt_quota_info)),
Phil Oesterae353092006-08-08 09:59:59 +0000107 .userspacesize = offsetof(struct xt_quota_info, quota),
Pablo Neira8caee8b2004-12-28 13:11:59 +0000108 .help = &help,
Pablo Neira8caee8b2004-12-28 13:11:59 +0000109 .parse = &parse,
110 .final_check = &final_check,
111 .print = &print,
112 .save = &save,
113 .extra_opts = opts
Harald Weltee746abb2001-12-03 22:22:55 +0000114};
115
116void
117_init(void)
118{
Yasuyuki KOZAKAI2bcb1602007-07-24 07:03:59 +0000119 xtables_register_match(&quota);
120 xtables_register_match(&quota6);
Harald Weltee746abb2001-12-03 22:22:55 +0000121}