aboutsummaryrefslogtreecommitdiff
path: root/extensions/libxt_nfacct.c
blob: 506f6f518504610d12c021b964928be4b7e02f6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * (C) 2011 by Pablo Neira Ayuso <pablo@netfilter.org>
 * (C) 2011 by Intra2Net AG <http://www.intra2net.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 (or
 * any later at your option) as published by the Free Software Foundation.
 */
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <xtables.h>

#include <linux/netfilter/x_tables.h>
#include <linux/netfilter/xt_nfacct.h>

enum {
	O_NAME = 0,
	O_QUOTA,
	O_PACKET,
};

#define s struct xt_nfacct_match_info
static const struct xt_option_entry nfacct_opts[] = {
	{.name = "nfacct-name", .id = O_NAME, .type = XTTYPE_STRING,
	 .min = 1, .flags = XTOPT_MAND|XTOPT_PUT, XTOPT_POINTER(s, name)},
	XTOPT_TABLEEND,
};
#undef s

#define s1 struct xt_nfacct_match_info_v1
static const struct xt_option_entry nfacct_opts_v1[] = {
	{.name = "nfacct-name", .id = O_NAME, .type = XTTYPE_STRING,
	 .min = 1, .flags = XTOPT_MAND|XTOPT_PUT, XTOPT_POINTER(s1, name)},
	{.name = "quota", .id = O_QUOTA, .type = XTTYPE_UINT64,
	 .flags = XTOPT_PUT,
	 XTOPT_POINTER(s1, quota)},
	{.name = "packets", .id = O_PACKET, .type = XTTYPE_NONE},
	XTOPT_TABLEEND,
};
#undef s1

static void nfacct_help(void)
{
	printf("nfacct match options:\n"
	       " --nfacct-name STRING		Name of accounting area\n");
}

static void nfacct_help_v1(void)
{
	printf("nfacct match options:\n"
	       " --nfacct-name STRING		Name of accounting area\n"
	       " --quota quota			Initial quota (byte or packets)\n"
	       " --packets			Count packets instead of bytes\n");
}

static void nfacct_parse(struct xt_option_call *cb)
{
	xtables_option_parse(cb);
	switch (cb->entry->id) {
	case O_NAME:
		if (strchr(cb->arg, '\n') != NULL)
			xtables_error(PARAMETER_PROBLEM,
				   "Newlines not allowed in --nfacct-name");
		break;
	}
}

static void nfacct_parse_v1(struct xt_option_call *cb)
{
	struct xt_nfacct_match_info_v1 *info =
				(struct xt_nfacct_match_info_v1 *)cb->data;

	xtables_option_parse(cb);
	switch (cb->entry->id) {
	case O_NAME:
		if (strchr(cb->arg, '\n') != NULL)
			xtables_error(PARAMETER_PROBLEM,
				   "Newlines not allowed in --nfacct-name");
		break;
	case O_PACKET:
		info->flags |= XT_NFACCT_QUOTA_PKTS;
		break;
	case O_QUOTA:
		info->flags |= XT_NFACCT_QUOTA;
		break;
	}
}

static void
nfacct_print_name(const char *info_name, char *name)
{
	printf(" %snfacct-name ", name);
	xtables_save_string(info_name);
}

static void nfacct_print(const void *ip,
			 const struct xt_entry_match *match, int numeric)
{
	const struct xt_nfacct_match_info *info =
		(struct xt_nfacct_match_info *)match->data;

	nfacct_print_name(info->name, "");
}

static void nfacct_print_v1(const void *ip,
			    const struct xt_entry_match *match, int numeric)
{
	const struct xt_nfacct_match_info_v1 *info =
		(struct xt_nfacct_match_info_v1 *)match->data;

	nfacct_print_name(info->name, "");
	if (info->flags & XT_NFACCT_QUOTA) {
		if (info->flags & XT_NFACCT_QUOTA_PKTS)
			printf(" packets");
		printf(" quota: %llu", (unsigned long long)info->quota);
	}
}

static void nfacct_save(const void *ip, const struct xt_entry_match *match)
{
	const struct xt_nfacct_match_info *info =
		(struct xt_nfacct_match_info *)match->data;

	nfacct_print_name(info->name, "--");
}

static void nfacct_save_v1(const void *ip,
			   const struct xt_entry_match *match)
{
	const struct xt_nfacct_match_info_v1 *info =
		(struct xt_nfacct_match_info_v1 *)match->data;

	nfacct_print_name(info->name, "--");
	if (info->flags & XT_NFACCT_QUOTA) {
		if (info->flags & XT_NFACCT_QUOTA_PKTS)
			printf(" --packets");
		printf(" --quota %llu", (unsigned long long)info->quota);
	}
}
static struct xtables_match nfacct_matches[] = {
{
	.family		= NFPROTO_UNSPEC,
	.name		= "nfacct",
	.version	= XTABLES_VERSION,
	.size		= XT_ALIGN(sizeof(struct xt_nfacct_match_info)),
	.userspacesize	= offsetof(struct xt_nfacct_match_info, nfacct),
	.help		= nfacct_help,
	.x6_parse	= nfacct_parse,
	.print		= nfacct_print,
	.save		= nfacct_save,
	.x6_options	= nfacct_opts,
},{
	.family		= NFPROTO_UNSPEC,
	.revision	= 1,
	.name		= "nfacct",
	.version	= XTABLES_VERSION,
	.size		= XT_ALIGN(sizeof(struct xt_nfacct_match_info_v1)),
	.userspacesize	= XT_ALIGN(sizeof(struct xt_nfacct_match_info_v1)),
	.help		= nfacct_help_v1,
	.x6_parse	= nfacct_parse_v1,
	.print		= nfacct_print_v1,
	.save		= nfacct_save_v1,
	.x6_options	= nfacct_opts_v1,
},
};

void _init(void)
{
	xtables_register_matches(nfacct_matches, ARRAY_SIZE(nfacct_matches));
}