summaryrefslogtreecommitdiffstats
path: root/extensions/libipt_account.c
blob: 86af85d1c0bdef722a69e10e0dd58bc7b7ee45f5 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* 
 * accounting match helper (libipt_account.c)
 * (C) 2003,2004 by Piotr Gasid³o (quaker@barbara.eu.org)
 *
 * Version: 0.1.6
 *
 * This software is distributed under the terms of GNU GPL
 */

#include <stdio.h>
#include <stdlib.h>
#include <iptables.h>
#include <string.h>
#include <getopt.h>

#include <linux/netfilter_ipv4/ipt_account.h>

#ifndef HIPQUAD
#define HIPQUAD(addr) \
	((unsigned char *)&addr)[3], \
	((unsigned char *)&addr)[2], \
	((unsigned char *)&addr)[1], \
	((unsigned char *)&addr)[0]
#endif
				
static void help(void) {
	printf(
			"account v%s options:\n"
			"--aaddr network/netmask\n"
			"	defines network/netmask for which make statistics.\n"
			"--aname name\n"
			"	defines name of list where statistics will be kept. If no is\n"
			"	specified DEFAULT will be used.\n"
			"--ashort\n"
			"       table will colect only short statistics (only total counters\n"
			"       without splitting it into protocols.\n"
	, 
	IPTABLES_VERSION);
};

static struct option opts[] = {
	{ .name = "aaddr",  .has_arg = 1, .flag = NULL, .val = 201 },
	{ .name = "aname",  .has_arg = 1, .flag = NULL, .val = 202 },
	{ .name = "ashort", .has_arg = 0, .flag = NULL, .val = 203 },
	{ .name = 0, .has_arg = 0, .flag = 0, .val = 0 }
};

/* Helper functions for parse_network */
int parseip(const char *parameter, u_int32_t *ip) {
	
	char buffer[16], *bufferptr, *dot;
	unsigned int i, shift, part;

	if (strlen(parameter) > 15)
		return 0;

	strncpy(buffer, parameter, 15);
	buffer[15] = 0;

	bufferptr = buffer;

	for (i = 0, shift = 24, *ip = 0; i < 3; i++, shift -= 8) {
		/* no dot */
		if ((dot = strchr(bufferptr, '.')) == NULL)
			return 0;
		/* not a number */
		if ((part = strtol(bufferptr, (char**)NULL, 10)) < 0) 
			return 0;	
		/* to big number */
		if (part > 255)
			return 0;
		*ip |= part << shift;		
		bufferptr = dot + 1;
	}
	/* not a number */
	if ((part = strtol(bufferptr, (char**)NULL, 10)) < 0) 
		return 0;
	/* to big number */
	if (part > 255)
		return 0;
	*ip |= part;
	return 1;
}

static void parsenetwork(const char *parameter, u_int32_t *network) {
	if (!parseip(parameter, network))
		exit_error(PARAMETER_PROBLEM, "account: wrong ip in network");
}

static void parsenetmaskasbits(const char *parameter, u_int32_t *netmask) {
	
	u_int32_t bits;
	
	if ((bits = strtol(parameter, (char **)NULL, 10)) < 0 || bits > 32)
		exit_error(PARAMETER_PROBLEM, "account: wrong netmask");

	*netmask = 0xffffffff << (32 - bits);
}

static void parsenetmaskasip(const char *parameter, u_int32_t *netmask) {
	if (!parseip(parameter, netmask))
		exit_error(PARAMETER_PROBLEM, "account: wrong ip in netmask");
}

static void parsenetmask(const char *parameter, u_int32_t *netmask) 
{
	if (strchr(parameter, '.') != NULL)
		parsenetmaskasip(parameter, netmask);
	else
		parsenetmaskasbits(parameter, netmask);
}

static void parsenetworkandnetmask(const char *parameter, u_int32_t *network, u_int32_t *netmask) 
{
	
	char buffer[32], *slash;

	if (strlen(parameter) > 31)
		/* text is to long, even for 255.255.255.255/255.255.255.255 */
		exit_error(PARAMETER_PROBLEM, "account: wrong network/netmask");

	strncpy(buffer, parameter, 31);
	buffer[31] = 0;

	/* check whether netmask is given */
	if ((slash = strchr(buffer, '/')) != NULL) {
		parsenetmask(slash + 1, netmask);
		*slash = 0;
	} else
		*netmask = 0xffffffff;
	parsenetwork(buffer, network);

	if ((*network & *netmask) != *network)
		exit_error(PARAMETER_PROBLEM, "account: wrong network/netmask");
}


/* Function gets network & netmask from argument after --aaddr */
static void parse_network(const char *parameter, struct t_ipt_account_info *info) {

	parsenetworkandnetmask(parameter, &info->network, &info->netmask);
	
}

/* validate netmask */
inline int valid_netmask(u_int32_t netmask) {
	while (netmask & 0x80000000)
		netmask <<= 1;
	if (netmask != 0)
		return 0;
        return 1;
}

/* validate network/netmask pair */
inline int valid_network_and_netmask(struct t_ipt_account_info *info) {
	if (!valid_netmask(info->netmask))
		return 0;
	if ((info->network & info->netmask) != info->network)
		return 0;
	return 1;
}



/* Function initializes match */
static void init(struct ipt_entry_match *match, 
		 unsigned int *nfcache) {
	
	struct t_ipt_account_info *info = (struct t_ipt_account_info *)(match)->data;

	*nfcache |= NFC_UNKNOWN;

	/* set default table name to DEFAULT */
	strncpy(info->name, "DEFAULT", IPT_ACCOUNT_NAME_LEN);
	info->shortlisting = 0;
	
}

/* Function parses match's arguments */
static int parse(int c, char **argv, 
		  int invert, 
		  unsigned int *flags,
                  const struct ipt_entry *entry,
                  unsigned int *nfcache,
                  struct ipt_entry_match **match) {
	
	struct t_ipt_account_info *info = (struct t_ipt_account_info *)(*match)->data;

	switch (c) {
		
		/* --aaddr */
		case 201:
			parse_network(optarg, info);
			if (!valid_network_and_netmask(info))
				exit_error(PARAMETER_PROBLEM, "account: wrong network/netmask");
			*flags = 1;
			break;
			
		/* --aname */
		case 202:
			if (strlen(optarg) < IPT_ACCOUNT_NAME_LEN)
				strncpy(info->name, optarg, IPT_ACCOUNT_NAME_LEN);
			else
				exit_error(PARAMETER_PROBLEM, "account: Too long table name");			
			break;	
		/* --ashort */
		case 203:
			info->shortlisting = 1;
			break;
		default:
			return 0;			
	}
	return 1;	
}

/* Final check whether network/netmask was specified */
static void final_check(unsigned int flags) {
	if (!flags)
		exit_error(PARAMETER_PROBLEM, "account: You need specify '--aaddr' parameter");
}

/* Function used for printing rule with account match for iptables -L */
static void print(const struct ipt_ip *ip,
                  const struct ipt_entry_match *match, 
		  int numeric) {
	
	struct t_ipt_account_info *info = (struct t_ipt_account_info *)match->data;
	
	printf("account: ");
	printf("network/netmask: ");
	printf("%u.%u.%u.%u/%u.%u.%u.%u ",
			HIPQUAD(info->network),
			HIPQUAD(info->netmask)
	      );
	
	printf("name: %s ", info->name);
	if (info->shortlisting)
		printf("short-listing ");
}

/* Function used for saving rule containing account match */
static void save(const struct ipt_ip *ip, 
		 const struct ipt_entry_match *match) {

	struct t_ipt_account_info *info = (struct t_ipt_account_info *)match->data;
	
	printf("--aaddr ");
	printf("%u.%u.%u.%u/%u.%u.%u.%u ",
			 HIPQUAD(info->network),
			 HIPQUAD(info->netmask)
	       );
	
	printf("--aname %s ", info->name);
	if (info->shortlisting)
		printf("--ashort ");
}
	
static struct iptables_match account = {
	.next = NULL,
	.name = "account",
	.version = IPTABLES_VERSION,
	.size = IPT_ALIGN(sizeof(struct t_ipt_account_info)),
	.userspacesize = IPT_ALIGN(sizeof(struct t_ipt_account_info)),
	.help = &help,
	.init = &init,
	.parse = &parse,
	.final_check = &final_check,
	.print = &print,
	.save = &save,
	.extra_opts = opts
};

/* Function which registers match */
void _init(void)
{
	register_match(&account);
}