1 /*
2 * (C) 2011 by Pablo Neira Ayuso <pablo@netfilter.org>
3 * (C) 2011 by Intra2Net AG <http://www.intra2net.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 (or
7 * any later at your option) as published by the Free Software Foundation.
8 */
9 #include <stdbool.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <getopt.h>
14 #include <xtables.h>
15
16 #include <linux/netfilter/x_tables.h>
17 #include <linux/netfilter/xt_nfacct.h>
18
19 enum {
20 O_NAME = 0,
21 };
22
23 #define s struct xt_nfacct_match_info
24 static const struct xt_option_entry nfacct_opts[] = {
25 {.name = "nfacct-name", .id = O_NAME, .type = XTTYPE_STRING,
26 .min = 1, .flags = XTOPT_MAND|XTOPT_PUT, XTOPT_POINTER(s, name)},
27 XTOPT_TABLEEND,
28 };
29 #undef s
30
nfacct_help(void)31 static void nfacct_help(void)
32 {
33 printf("nfacct match options:\n"
34 " --nfacct-name STRING Name of accouting area\n");
35 }
36
nfacct_parse(struct xt_option_call * cb)37 static void nfacct_parse(struct xt_option_call *cb)
38 {
39 xtables_option_parse(cb);
40 switch (cb->entry->id) {
41 case O_NAME:
42 if (strchr(cb->arg, '\n') != NULL)
43 xtables_error(PARAMETER_PROBLEM,
44 "Newlines not allowed in --nfacct-name");
45 break;
46 }
47 }
48
49 static void
nfacct_print_name(const struct xt_nfacct_match_info * info,char * name)50 nfacct_print_name(const struct xt_nfacct_match_info *info, char *name)
51 {
52 printf(" %snfacct-name ", name);
53 xtables_save_string(info->name);
54 }
55
nfacct_print(const void * ip,const struct xt_entry_match * match,int numeric)56 static void nfacct_print(const void *ip, const struct xt_entry_match *match,
57 int numeric)
58 {
59 const struct xt_nfacct_match_info *info =
60 (struct xt_nfacct_match_info *)match->data;
61
62 nfacct_print_name(info, "");
63 }
64
nfacct_save(const void * ip,const struct xt_entry_match * match)65 static void nfacct_save(const void *ip, const struct xt_entry_match *match)
66 {
67 const struct xt_nfacct_match_info *info =
68 (struct xt_nfacct_match_info *)match->data;
69
70 nfacct_print_name(info, "--");
71 }
72
73 static struct xtables_match nfacct_matches[] = {
74 {
75 .family = NFPROTO_UNSPEC,
76 .revision = 0,
77 .name = "nfacct",
78 .version = XTABLES_VERSION,
79 .size = XT_ALIGN(sizeof(struct xt_nfacct_match_info)),
80 .userspacesize = offsetof(struct xt_nfacct_match_info, nfacct),
81 .help = nfacct_help,
82 .x6_parse = nfacct_parse,
83 .print = nfacct_print,
84 .save = nfacct_save,
85 .x6_options = nfacct_opts,
86 },
87 {
88 .family = NFPROTO_UNSPEC,
89 .revision = 1,
90 .name = "nfacct",
91 .version = XTABLES_VERSION,
92 .size = XT_ALIGN(sizeof(struct xt_nfacct_match_info_v1)),
93 .userspacesize = offsetof(struct xt_nfacct_match_info_v1, nfacct),
94 .help = nfacct_help,
95 .x6_parse = nfacct_parse,
96 .print = nfacct_print,
97 .save = nfacct_save,
98 .x6_options = nfacct_opts,
99 },
100 };
101
_init(void)102 void _init(void)
103 {
104 xtables_register_matches(nfacct_matches, ARRAY_SIZE(nfacct_matches));
105 }
106