blob: 8e178fe7005e8ecffad27261ac46a496b6912182 [file] [log] [blame]
Harald Weltee746abb2001-12-03 22:22:55 +00001/*
2 * Shared library add-on to iptables to add quota support
3 *
4 * Sam Johnston <samj@samj.net>
5 */
Phil Oesterae353092006-08-08 09:59:59 +00006#include <stddef.h>
Harald Weltee746abb2001-12-03 22:22:55 +00007#include <stdio.h>
8#include <stdlib.h>
9#include <getopt.h>
Yasuyuki KOZAKAI2bcb1602007-07-24 07:03:59 +000010#include <xtables.h>
Harald Weltee746abb2001-12-03 22:22:55 +000011
Phil Oesterae353092006-08-08 09:59:59 +000012#include <linux/netfilter/xt_quota.h>
Harald Weltee746abb2001-12-03 22:22:55 +000013
Jan Engelhardt181dead2007-10-04 16:27:07 +000014static const struct option quota_opts[] = {
Max Kellermann5b76f682008-01-29 13:42:48 +000015 {"quota", 1, NULL, '1'},
Max Kellermann9ee386a2008-01-29 13:48:05 +000016 { .name = NULL }
Harald Weltee746abb2001-12-03 22:22:55 +000017};
18
19/* print usage */
Jan Engelhardt181dead2007-10-04 16:27:07 +000020static void quota_help(void)
Harald Weltee746abb2001-12-03 22:22:55 +000021{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020022 printf("quota match options:\n"
23 " --quota quota quota (bytes)\n");
Harald Weltee746abb2001-12-03 22:22:55 +000024}
25
Harald Weltee746abb2001-12-03 22:22:55 +000026/* print matchinfo */
27static void
Jan Engelhardt181dead2007-10-04 16:27:07 +000028quota_print(const void *ip, const struct xt_entry_match *match, int numeric)
Harald Weltee746abb2001-12-03 22:22:55 +000029{
Max Kellermann5b76f682008-01-29 13:42:48 +000030 struct xt_quota_info *q = (struct xt_quota_info *) match->data;
31 printf("quota: %llu bytes", (unsigned long long) q->quota);
Harald Weltee746abb2001-12-03 22:22:55 +000032}
33
34/* save matchinfo */
35static void
Jan Engelhardt181dead2007-10-04 16:27:07 +000036quota_save(const void *ip, const struct xt_entry_match *match)
Harald Weltee746abb2001-12-03 22:22:55 +000037{
Max Kellermann5b76f682008-01-29 13:42:48 +000038 struct xt_quota_info *q = (struct xt_quota_info *) match->data;
39 printf("--quota %llu ", (unsigned long long) q->quota);
Harald Weltee746abb2001-12-03 22:22:55 +000040}
41
42/* parse quota option */
43static int
44parse_quota(const char *s, u_int64_t * quota)
45{
Jan Engelhardt7a236f42008-03-03 12:30:41 +010046 *quota = strtoull(s, NULL, 10);
Harald Weltee746abb2001-12-03 22:22:55 +000047
Yasuyuki KOZAKAI2bcb1602007-07-24 07:03:59 +000048#ifdef DEBUG_XT_QUOTA
Max Kellermann5b76f682008-01-29 13:42:48 +000049 printf("Quota: %llu\n", *quota);
Harald Weltee746abb2001-12-03 22:22:55 +000050#endif
51
Max Kellermann9ee386a2008-01-29 13:48:05 +000052 if (*quota == (u_int64_t)-1)
Max Kellermann5b76f682008-01-29 13:42:48 +000053 exit_error(PARAMETER_PROBLEM, "quota invalid: '%s'\n", s);
54 else
55 return 1;
Harald Weltee746abb2001-12-03 22:22:55 +000056}
57
58/* parse all options, returning true if we found any for us */
59static int
Jan Engelhardt181dead2007-10-04 16:27:07 +000060quota_parse(int c, char **argv, int invert, unsigned int *flags,
Max Kellermann5b76f682008-01-29 13:42:48 +000061 const void *entry, struct xt_entry_match **match)
Harald Weltee746abb2001-12-03 22:22:55 +000062{
Max Kellermann5b76f682008-01-29 13:42:48 +000063 struct xt_quota_info *info = (struct xt_quota_info *) (*match)->data;
Harald Weltee746abb2001-12-03 22:22:55 +000064
Max Kellermann5b76f682008-01-29 13:42:48 +000065 switch (c) {
66 case '1':
67 if (check_inverse(optarg, &invert, NULL, 0))
68 exit_error(PARAMETER_PROBLEM, "quota: unexpected '!'");
69 if (!parse_quota(optarg, &info->quota))
70 exit_error(PARAMETER_PROBLEM,
71 "bad quota: '%s'", optarg);
72 break;
Harald Weltee746abb2001-12-03 22:22:55 +000073
Max Kellermann5b76f682008-01-29 13:42:48 +000074 default:
75 return 0;
76 }
77 return 1;
Harald Weltee746abb2001-12-03 22:22:55 +000078}
79
Patrick McHardy0ea82bc2008-06-07 15:15:29 +020080static struct xtables_match quota_match = {
Jan Engelhardt23545c22008-02-14 04:23:04 +010081 .family = AF_UNSPEC,
Yasuyuki KOZAKAI2bcb1602007-07-24 07:03:59 +000082 .name = "quota",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020083 .version = XTABLES_VERSION,
Yasuyuki KOZAKAI2bcb1602007-07-24 07:03:59 +000084 .size = XT_ALIGN(sizeof (struct xt_quota_info)),
Phil Oesterae353092006-08-08 09:59:59 +000085 .userspacesize = offsetof(struct xt_quota_info, quota),
Jan Engelhardt181dead2007-10-04 16:27:07 +000086 .help = quota_help,
87 .parse = quota_parse,
88 .print = quota_print,
89 .save = quota_save,
90 .extra_opts = quota_opts,
Harald Weltee746abb2001-12-03 22:22:55 +000091};
92
93void
94_init(void)
95{
Jan Engelhardt181dead2007-10-04 16:27:07 +000096 xtables_register_match(&quota_match);
Harald Weltee746abb2001-12-03 22:22:55 +000097}