lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Shared library add-on to iptables to add quota support |
| 3 | * |
| 4 | * Sam Johnston <samj@samj.net> |
| 5 | */ |
| 6 | #include <stddef.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <getopt.h> |
| 10 | #include <xtables.h> |
| 11 | |
| 12 | #include <linux/netfilter/xt_quota.h> |
| 13 | |
| 14 | static const struct option quota_opts[] = { |
| 15 | {"quota", 1, NULL, '1'}, |
| 16 | { .name = NULL } |
| 17 | }; |
| 18 | |
| 19 | static void quota_help(void) |
| 20 | { |
| 21 | printf("quota match options:\n" |
| 22 | " --quota quota quota (bytes)\n"); |
| 23 | } |
| 24 | |
| 25 | static void |
| 26 | quota_print(const void *ip, const struct xt_entry_match *match, int numeric) |
| 27 | { |
| 28 | struct xt_quota_info *q = (struct xt_quota_info *) match->data; |
| 29 | printf("quota: %llu bytes", (unsigned long long) q->quota); |
| 30 | } |
| 31 | |
| 32 | static void |
| 33 | quota_save(const void *ip, const struct xt_entry_match *match) |
| 34 | { |
| 35 | struct xt_quota_info *q = (struct xt_quota_info *) match->data; |
| 36 | printf("--quota %llu ", (unsigned long long) q->quota); |
| 37 | } |
| 38 | |
| 39 | /* parse quota option */ |
| 40 | static int |
| 41 | parse_quota(const char *s, u_int64_t * quota) |
| 42 | { |
| 43 | *quota = strtoull(s, NULL, 10); |
| 44 | |
| 45 | #ifdef DEBUG_XT_QUOTA |
| 46 | printf("Quota: %llu\n", *quota); |
| 47 | #endif |
| 48 | |
| 49 | if (*quota == UINT64_MAX) |
| 50 | xtables_error(PARAMETER_PROBLEM, "quota invalid: '%s'\n", s); |
| 51 | else |
| 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | static int |
| 56 | quota_parse(int c, char **argv, int invert, unsigned int *flags, |
| 57 | const void *entry, struct xt_entry_match **match) |
| 58 | { |
| 59 | struct xt_quota_info *info = (struct xt_quota_info *) (*match)->data; |
| 60 | |
| 61 | switch (c) { |
| 62 | case '1': |
| 63 | if (xtables_check_inverse(optarg, &invert, NULL, 0)) |
| 64 | xtables_error(PARAMETER_PROBLEM, "quota: unexpected '!'"); |
| 65 | if (!parse_quota(optarg, &info->quota)) |
| 66 | xtables_error(PARAMETER_PROBLEM, |
| 67 | "bad quota: '%s'", optarg); |
| 68 | break; |
| 69 | |
| 70 | default: |
| 71 | return 0; |
| 72 | } |
| 73 | return 1; |
| 74 | } |
| 75 | |
| 76 | static struct xtables_match quota_match = { |
| 77 | .family = AF_UNSPEC, |
| 78 | .name = "quota", |
| 79 | .version = XTABLES_VERSION, |
| 80 | .size = XT_ALIGN(sizeof (struct xt_quota_info)), |
| 81 | .userspacesize = offsetof(struct xt_quota_info, quota), |
| 82 | .help = quota_help, |
| 83 | .parse = quota_parse, |
| 84 | .print = quota_print, |
| 85 | .save = quota_save, |
| 86 | .extra_opts = quota_opts, |
| 87 | }; |
| 88 | |
| 89 | void |
| 90 | _init(void) |
| 91 | { |
| 92 | xtables_register_match("a_match); |
| 93 | } |