libxt_quota.c revision 32b8e61e4e5bd405d9ad07bf9468498dfbb19f9e
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, u_int64_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 default: 76 return 0; 77 } 78 return 1; 79} 80 81static struct xtables_match quota_match = { 82 .family = NFPROTO_UNSPEC, 83 .name = "quota", 84 .version = XTABLES_VERSION, 85 .size = XT_ALIGN(sizeof (struct xt_quota_info)), 86 .userspacesize = offsetof(struct xt_quota_info, quota), 87 .help = quota_help, 88 .parse = quota_parse, 89 .print = quota_print, 90 .save = quota_save, 91 .extra_opts = quota_opts, 92}; 93 94void 95_init(void) 96{ 97 xtables_register_match("a_match); 98} 99