Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Shared library add-on to iptables to add quota support |
| 3 | * |
| 4 | * Sam Johnston <samj@samj.net> |
| 5 | */ |
Phil Oester | ae35309 | 2006-08-08 09:59:59 +0000 | [diff] [blame] | 6 | #include <stddef.h> |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <getopt.h> |
Yasuyuki KOZAKAI | 2bcb160 | 2007-07-24 07:03:59 +0000 | [diff] [blame] | 10 | #include <xtables.h> |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 11 | |
Phil Oester | ae35309 | 2006-08-08 09:59:59 +0000 | [diff] [blame] | 12 | #include <linux/netfilter/xt_quota.h> |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 13 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 14 | static const struct option quota_opts[] = { |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 15 | {"quota", 1, NULL, '1'}, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame^] | 16 | { .name = NULL } |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 17 | }; |
| 18 | |
| 19 | /* print usage */ |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 20 | static void quota_help(void) |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 21 | { |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 22 | printf("quota options:\n" |
| 23 | " --quota quota quota (bytes)\n" "\n"); |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 26 | /* print matchinfo */ |
| 27 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 28 | quota_print(const void *ip, const struct xt_entry_match *match, int numeric) |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 29 | { |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 30 | struct xt_quota_info *q = (struct xt_quota_info *) match->data; |
| 31 | printf("quota: %llu bytes", (unsigned long long) q->quota); |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /* save matchinfo */ |
| 35 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 36 | quota_save(const void *ip, const struct xt_entry_match *match) |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 37 | { |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 38 | struct xt_quota_info *q = (struct xt_quota_info *) match->data; |
| 39 | printf("--quota %llu ", (unsigned long long) q->quota); |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /* parse quota option */ |
| 43 | static int |
| 44 | parse_quota(const char *s, u_int64_t * quota) |
| 45 | { |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 46 | *quota = strtoull(s, (char **) NULL, 10); |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 47 | |
Yasuyuki KOZAKAI | 2bcb160 | 2007-07-24 07:03:59 +0000 | [diff] [blame] | 48 | #ifdef DEBUG_XT_QUOTA |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 49 | printf("Quota: %llu\n", *quota); |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 50 | #endif |
| 51 | |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame^] | 52 | if (*quota == (u_int64_t)-1) |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 53 | exit_error(PARAMETER_PROBLEM, "quota invalid: '%s'\n", s); |
| 54 | else |
| 55 | return 1; |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* parse all options, returning true if we found any for us */ |
| 59 | static int |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 60 | quota_parse(int c, char **argv, int invert, unsigned int *flags, |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 61 | const void *entry, struct xt_entry_match **match) |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 62 | { |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 63 | struct xt_quota_info *info = (struct xt_quota_info *) (*match)->data; |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 64 | |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 65 | switch (c) { |
| 66 | case '1': |
| 67 | if (check_inverse(optarg, &invert, NULL, 0)) |
| 68 | exit_error(PARAMETER_PROBLEM, "quota: unexpected '!'"); |
| 69 | if (!parse_quota(optarg, &info->quota)) |
| 70 | exit_error(PARAMETER_PROBLEM, |
| 71 | "bad quota: '%s'", optarg); |
| 72 | break; |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 73 | |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 74 | default: |
| 75 | return 0; |
| 76 | } |
| 77 | return 1; |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 80 | struct xtables_match quota_match = { |
Yasuyuki KOZAKAI | 2bcb160 | 2007-07-24 07:03:59 +0000 | [diff] [blame] | 81 | .family = AF_INET, |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 82 | .name = "quota", |
| 83 | .version = IPTABLES_VERSION, |
Yasuyuki KOZAKAI | 2bcb160 | 2007-07-24 07:03:59 +0000 | [diff] [blame] | 84 | .size = XT_ALIGN(sizeof (struct xt_quota_info)), |
| 85 | .userspacesize = offsetof(struct xt_quota_info, quota), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 86 | .help = quota_help, |
| 87 | .parse = quota_parse, |
| 88 | .print = quota_print, |
| 89 | .save = quota_save, |
| 90 | .extra_opts = quota_opts, |
Yasuyuki KOZAKAI | 2bcb160 | 2007-07-24 07:03:59 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 93 | struct xtables_match quota_match6 = { |
Yasuyuki KOZAKAI | 2bcb160 | 2007-07-24 07:03:59 +0000 | [diff] [blame] | 94 | .family = AF_INET6, |
| 95 | .name = "quota", |
| 96 | .version = IPTABLES_VERSION, |
| 97 | .size = XT_ALIGN(sizeof (struct xt_quota_info)), |
Phil Oester | ae35309 | 2006-08-08 09:59:59 +0000 | [diff] [blame] | 98 | .userspacesize = offsetof(struct xt_quota_info, quota), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 99 | .help = quota_help, |
| 100 | .parse = quota_parse, |
| 101 | .print = quota_print, |
| 102 | .save = quota_save, |
| 103 | .extra_opts = quota_opts, |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | void |
| 107 | _init(void) |
| 108 | { |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 109 | xtables_register_match("a_match); |
| 110 | xtables_register_match("a_match6); |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 111 | } |