| 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 | 661f112 | 2007-07-30 14:46:51 +0000 | [diff] [blame^] | 14 | static const struct option opts[] = { |
| Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 15 | {"quota", 1, 0, '1'}, |
| 16 | {0} |
| 17 | }; |
| 18 | |
| 19 | /* print usage */ |
| 20 | static void |
| 21 | help(void) |
| 22 | { |
| 23 | printf("quota options:\n" |
| 24 | " --quota quota quota (bytes)\n" "\n"); |
| 25 | } |
| 26 | |
| Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 27 | /* print matchinfo */ |
| 28 | static void |
| Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 29 | print(const void *ip, const struct xt_entry_match *match, int numeric) |
| Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 30 | { |
| Phil Oester | ae35309 | 2006-08-08 09:59:59 +0000 | [diff] [blame] | 31 | struct xt_quota_info *q = (struct xt_quota_info *) match->data; |
| Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 32 | printf("quota: %llu bytes", (unsigned long long) q->quota); |
| 33 | } |
| 34 | |
| 35 | /* save matchinfo */ |
| 36 | static void |
| Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 37 | save(const void *ip, const struct xt_entry_match *match) |
| Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 38 | { |
| Phil Oester | ae35309 | 2006-08-08 09:59:59 +0000 | [diff] [blame] | 39 | struct xt_quota_info *q = (struct xt_quota_info *) match->data; |
| Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 40 | printf("--quota %llu ", (unsigned long long) q->quota); |
| 41 | } |
| 42 | |
| 43 | /* parse quota option */ |
| 44 | static int |
| 45 | parse_quota(const char *s, u_int64_t * quota) |
| 46 | { |
| 47 | *quota = strtoull(s, (char **) NULL, 10); |
| 48 | |
| Yasuyuki KOZAKAI | 2bcb160 | 2007-07-24 07:03:59 +0000 | [diff] [blame] | 49 | #ifdef DEBUG_XT_QUOTA |
| Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 50 | 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 */ |
| 60 | static int |
| 61 | parse(int c, char **argv, int invert, unsigned int *flags, |
| Yasuyuki KOZAKAI | c0a9ab9 | 2007-07-24 06:02:05 +0000 | [diff] [blame] | 62 | const void *entry, |
| Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 63 | unsigned int *nfcache, struct xt_entry_match **match) |
| Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 64 | { |
| Phil Oester | ae35309 | 2006-08-08 09:59:59 +0000 | [diff] [blame] | 65 | struct xt_quota_info *info = (struct xt_quota_info *) (*match)->data; |
| Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 66 | |
| 67 | switch (c) { |
| 68 | case '1': |
| Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 69 | if (check_inverse(optarg, &invert, NULL, 0)) |
| Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 70 | 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 */ |
| 83 | static void |
| 84 | final_check(unsigned int flags) |
| 85 | { |
| 86 | } |
| 87 | |
| Yasuyuki KOZAKAI | 2bcb160 | 2007-07-24 07:03:59 +0000 | [diff] [blame] | 88 | struct xtables_match quota = { |
| Yasuyuki KOZAKAI | 2bcb160 | 2007-07-24 07:03:59 +0000 | [diff] [blame] | 89 | .family = AF_INET, |
| Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 90 | .name = "quota", |
| 91 | .version = IPTABLES_VERSION, |
| Yasuyuki KOZAKAI | 2bcb160 | 2007-07-24 07:03:59 +0000 | [diff] [blame] | 92 | .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 | |
| 102 | struct xtables_match quota6 = { |
| Yasuyuki KOZAKAI | 2bcb160 | 2007-07-24 07:03:59 +0000 | [diff] [blame] | 103 | .family = AF_INET6, |
| 104 | .name = "quota", |
| 105 | .version = IPTABLES_VERSION, |
| 106 | .size = XT_ALIGN(sizeof (struct xt_quota_info)), |
| Phil Oester | ae35309 | 2006-08-08 09:59:59 +0000 | [diff] [blame] | 107 | .userspacesize = offsetof(struct xt_quota_info, quota), |
| Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 108 | .help = &help, |
| Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 109 | .parse = &parse, |
| 110 | .final_check = &final_check, |
| 111 | .print = &print, |
| 112 | .save = &save, |
| 113 | .extra_opts = opts |
| Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | void |
| 117 | _init(void) |
| 118 | { |
| Yasuyuki KOZAKAI | 2bcb160 | 2007-07-24 07:03:59 +0000 | [diff] [blame] | 119 | xtables_register_match("a); |
| 120 | xtables_register_match("a6); |
| Harald Welte | e746abb | 2001-12-03 22:22:55 +0000 | [diff] [blame] | 121 | } |