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 | |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 19 | static void quota_help(void) |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 20 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 21 | printf("quota match options:\n" |
| 22 | " --quota quota quota (bytes)\n"); |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 23 | } |
| 24 | |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 25 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 26 | 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] | 27 | { |
Jan Engelhardt | 69f564e | 2009-05-26 13:14:06 +0200 | [diff] [blame] | 28 | const struct xt_quota_info *q = (const void *)match->data; |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 29 | printf("quota: %llu bytes", (unsigned long long) q->quota); |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 32 | static void |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 33 | quota_save(const void *ip, const struct xt_entry_match *match) |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 34 | { |
Jan Engelhardt | 69f564e | 2009-05-26 13:14:06 +0200 | [diff] [blame] | 35 | const struct xt_quota_info *q = (const void *)match->data; |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 36 | printf("--quota %llu ", (unsigned long long) q->quota); |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /* parse quota option */ |
| 40 | static int |
| 41 | parse_quota(const char *s, u_int64_t * quota) |
| 42 | { |
Jan Engelhardt | 7a236f4 | 2008-03-03 12:30:41 +0100 | [diff] [blame] | 43 | *quota = strtoull(s, NULL, 10); |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 44 | |
Yasuyuki KOZAKAI | 2bcb160 | 2007-07-24 07:03:59 +0000 | [diff] [blame] | 45 | #ifdef DEBUG_XT_QUOTA |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 46 | printf("Quota: %llu\n", *quota); |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 47 | #endif |
| 48 | |
Jan Engelhardt | a809754 | 2009-01-27 17:39:01 +0100 | [diff] [blame] | 49 | if (*quota == UINT64_MAX) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 50 | xtables_error(PARAMETER_PROBLEM, "quota invalid: '%s'\n", s); |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 51 | else |
| 52 | return 1; |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 55 | static int |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 56 | quota_parse(int c, char **argv, int invert, unsigned int *flags, |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 57 | const void *entry, struct xt_entry_match **match) |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 58 | { |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 59 | struct xt_quota_info *info = (struct xt_quota_info *) (*match)->data; |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 60 | |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 61 | switch (c) { |
| 62 | case '1': |
Jan Engelhardt | 0f16c72 | 2009-01-30 04:55:38 +0100 | [diff] [blame] | 63 | if (xtables_check_inverse(optarg, &invert, NULL, 0)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 64 | xtables_error(PARAMETER_PROBLEM, "quota: unexpected '!'"); |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 65 | if (!parse_quota(optarg, &info->quota)) |
Jan Engelhardt | 1829ed4 | 2009-02-21 03:29:44 +0100 | [diff] [blame] | 66 | xtables_error(PARAMETER_PROBLEM, |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 67 | "bad quota: '%s'", optarg); |
| 68 | break; |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 69 | |
Max Kellermann | 5b76f68 | 2008-01-29 13:42:48 +0000 | [diff] [blame] | 70 | default: |
| 71 | return 0; |
| 72 | } |
| 73 | return 1; |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Patrick McHardy | 0ea82bc | 2008-06-07 15:15:29 +0200 | [diff] [blame] | 76 | static struct xtables_match quota_match = { |
Jan Engelhardt | 4297936 | 2009-06-01 11:56:23 +0200 | [diff] [blame^] | 77 | .family = NFPROTO_UNSPEC, |
Yasuyuki KOZAKAI | 2bcb160 | 2007-07-24 07:03:59 +0000 | [diff] [blame] | 78 | .name = "quota", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 79 | .version = XTABLES_VERSION, |
Yasuyuki KOZAKAI | 2bcb160 | 2007-07-24 07:03:59 +0000 | [diff] [blame] | 80 | .size = XT_ALIGN(sizeof (struct xt_quota_info)), |
Phil Oester | ae35309 | 2006-08-08 09:59:59 +0000 | [diff] [blame] | 81 | .userspacesize = offsetof(struct xt_quota_info, quota), |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 82 | .help = quota_help, |
| 83 | .parse = quota_parse, |
| 84 | .print = quota_print, |
| 85 | .save = quota_save, |
| 86 | .extra_opts = quota_opts, |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | void |
| 90 | _init(void) |
| 91 | { |
Jan Engelhardt | 181dead | 2007-10-04 16:27:07 +0000 | [diff] [blame] | 92 | xtables_register_match("a_match); |
Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 93 | } |