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