libxt_NFQUEUE.c 10.7 KB
Newer Older
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
/* Shared library add-on to iptables for NFQ
 *
 * (C) 2005 by Harald Welte <laforge@netfilter.org>
 *
 * This program is distributed under the terms of GNU GPL v2, 1991
 *
 */
#include <stdio.h>
#include <xtables.h>
#include <linux/netfilter/xt_NFQUEUE.h>

enum {
	O_QUEUE_NUM = 0,
	O_QUEUE_BALANCE,
	O_QUEUE_BYPASS,
	O_QUEUE_CPU_FANOUT,
	F_QUEUE_NUM     = 1 << O_QUEUE_NUM,
	F_QUEUE_BALANCE = 1 << O_QUEUE_BALANCE,
	F_QUEUE_CPU_FANOUT = 1 << O_QUEUE_CPU_FANOUT,
};

static void NFQUEUE_help(void)
{
	printf(
"NFQUEUE target options\n"
"  --queue-num value		Send packet to QUEUE number <value>.\n"
"  		                Valid queue numbers are 0-65535\n"
);
}

static void NFQUEUE_help_v1(void)
{
	printf(
34
35
36
"NFQUEUE target options\n"
"  --queue-num value            Send packet to QUEUE number <value>.\n"
"                               Valid queue numbers are 0-65535\n"
37
38
39
40
41
42
"  --queue-balance first:last	Balance flows between queues <value> to <value>.\n");
}

static void NFQUEUE_help_v2(void)
{
	printf(
43
44
45
46
"NFQUEUE target options\n"
"  --queue-num value            Send packet to QUEUE number <value>.\n"
"                               Valid queue numbers are 0-65535\n"
"  --queue-balance first:last   Balance flows between queues <value> to <value>.\n"
47
48
49
50
51
52
53
"  --queue-bypass		Bypass Queueing if no queue instance exists.\n"
"  --queue-cpu-fanout	Use current CPU (no hashing)\n");
}

static void NFQUEUE_help_v3(void)
{
	printf(
54
55
56
57
58
"NFQUEUE target options\n"
"  --queue-num value            Send packet to QUEUE number <value>.\n"
"                               Valid queue numbers are 0-65535\n"
"  --queue-balance first:last   Balance flows between queues <value> to <value>.\n"
"  --queue-bypass               Bypass Queueing if no queue instance exists.\n"
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
"  --queue-cpu-fanout	Use current CPU (no hashing)\n");
}

#define s struct xt_NFQ_info
static const struct xt_option_entry NFQUEUE_opts[] = {
	{.name = "queue-num", .id = O_QUEUE_NUM, .type = XTTYPE_UINT16,
	 .flags = XTOPT_PUT, XTOPT_POINTER(s, queuenum),
	 .excl = F_QUEUE_BALANCE},
	{.name = "queue-balance", .id = O_QUEUE_BALANCE,
	 .type = XTTYPE_UINT16RC, .excl = F_QUEUE_NUM},
	{.name = "queue-bypass", .id = O_QUEUE_BYPASS, .type = XTTYPE_NONE},
	{.name = "queue-cpu-fanout", .id = O_QUEUE_CPU_FANOUT,
	 .type = XTTYPE_NONE, .also = F_QUEUE_BALANCE},
	XTOPT_TABLEEND,
};
#undef s

static void NFQUEUE_parse(struct xt_option_call *cb)
{
	xtables_option_parse(cb);
	if (cb->entry->id == O_QUEUE_BALANCE)
		xtables_error(PARAMETER_PROBLEM, "NFQUEUE target: "
				   "--queue-balance not supported (kernel too old?)");
}

static void NFQUEUE_parse_v1(struct xt_option_call *cb)
{
	struct xt_NFQ_info_v1 *info = cb->data;
	const uint16_t *r = cb->val.u16_range;

	xtables_option_parse(cb);
	switch (cb->entry->id) {
	case O_QUEUE_BALANCE:
		if (cb->nvals != 2)
			xtables_error(PARAMETER_PROBLEM,
				"Bad range \"%s\"", cb->arg);
		if (r[0] >= r[1])
			xtables_error(PARAMETER_PROBLEM, "%u should be less than %u",
				r[0], r[1]);
		info->queuenum = r[0];
		info->queues_total = r[1] - r[0] + 1;
		break;
	}
}

static void NFQUEUE_parse_v2(struct xt_option_call *cb)
{
	struct xt_NFQ_info_v2 *info = cb->data;
107
	const uint16_t *r = cb->val.u16_range;
108

109
	xtables_option_parse(cb);
110
	switch (cb->entry->id) {
111
112
113
114
115
116
117
118
119
120
121
	case O_QUEUE_BALANCE:
		if (cb->nvals != 2)
			xtables_error(PARAMETER_PROBLEM,
				"Bad range \"%s\"", cb->arg);
		if (r[0] >= r[1])
			xtables_error(PARAMETER_PROBLEM,
				      "%u should be less than %u",
				      r[0], r[1]);
		info->queuenum = r[0];
		info->queues_total = r[1] - r[0] + 1;
		break;
122
	case O_QUEUE_BYPASS:
123
		info->bypass |= NFQ_FLAG_BYPASS;
124
125
126
127
128
129
130
		break;
	}
}

static void NFQUEUE_parse_v3(struct xt_option_call *cb)
{
	struct xt_NFQ_info_v3 *info = cb->data;
131
	const uint16_t *r = cb->val.u16_range;
132

133
	xtables_option_parse(cb);
134
	switch (cb->entry->id) {
135
136
137
138
139
140
141
142
143
144
145
146
147
148
	case O_QUEUE_BALANCE:
		if (cb->nvals != 2)
			xtables_error(PARAMETER_PROBLEM,
				"Bad range \"%s\"", cb->arg);
		if (r[0] >= r[1])
			xtables_error(PARAMETER_PROBLEM,
				      "%u should be less than %u",
				      r[0], r[1]);
		info->queuenum = r[0];
		info->queues_total = r[1] - r[0] + 1;
		break;
	case O_QUEUE_BYPASS:
		info->flags |= NFQ_FLAG_BYPASS;
		break;
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
	case O_QUEUE_CPU_FANOUT:
		info->flags |= NFQ_FLAG_CPU_FANOUT;
		break;
	}
}

static void NFQUEUE_print(const void *ip,
                          const struct xt_entry_target *target, int numeric)
{
	const struct xt_NFQ_info *tinfo =
		(const struct xt_NFQ_info *)target->data;
	printf(" NFQUEUE num %u", tinfo->queuenum);
}

static void NFQUEUE_print_v1(const void *ip,
                             const struct xt_entry_target *target, int numeric)
{
	const struct xt_NFQ_info_v1 *tinfo = (const void *)target->data;
	unsigned int last = tinfo->queues_total;

	if (last > 1) {
		last += tinfo->queuenum - 1;
		printf(" NFQUEUE balance %u:%u", tinfo->queuenum, last);
	} else {
		printf(" NFQUEUE num %u", tinfo->queuenum);
	}
}

static void NFQUEUE_print_v2(const void *ip,
                             const struct xt_entry_target *target, int numeric)
{
	const struct xt_NFQ_info_v2 *info = (void *) target->data;
181
182
183
184
185
186
187
	unsigned int last = info->queues_total;

	if (last > 1) {
		last += info->queuenum - 1;
		printf(" NFQUEUE balance %u:%u", info->queuenum, last);
	} else
		printf(" NFQUEUE num %u", info->queuenum);
188
189
190
191
192
193
194
195
196

	if (info->bypass & NFQ_FLAG_BYPASS)
		printf(" bypass");
}

static void NFQUEUE_print_v3(const void *ip,
                             const struct xt_entry_target *target, int numeric)
{
	const struct xt_NFQ_info_v3 *info = (void *)target->data;
197
198
199
200
201
202
203
204
205
206
	unsigned int last = info->queues_total;

	if (last > 1) {
		last += info->queuenum - 1;
		printf(" NFQUEUE balance %u:%u", info->queuenum, last);
	} else
		printf(" NFQUEUE num %u", info->queuenum);

	if (info->flags & NFQ_FLAG_BYPASS)
		printf(" bypass");
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

	if (info->flags & NFQ_FLAG_CPU_FANOUT)
		printf(" cpu-fanout");
}

static void NFQUEUE_save(const void *ip, const struct xt_entry_target *target)
{
	const struct xt_NFQ_info *tinfo =
		(const struct xt_NFQ_info *)target->data;

	printf(" --queue-num %u", tinfo->queuenum);
}

static void NFQUEUE_save_v1(const void *ip, const struct xt_entry_target *target)
{
	const struct xt_NFQ_info_v1 *tinfo = (const void *)target->data;
	unsigned int last = tinfo->queues_total;

	if (last > 1) {
		last += tinfo->queuenum - 1;
		printf(" --queue-balance %u:%u", tinfo->queuenum, last);
	} else {
		printf(" --queue-num %u", tinfo->queuenum);
	}
}

static void NFQUEUE_save_v2(const void *ip, const struct xt_entry_target *target)
{
	const struct xt_NFQ_info_v2 *info = (void *) target->data;
236
	unsigned int last = info->queues_total;
237

238
239
240
241
242
	if (last > 1) {
		last += info->queuenum - 1;
		printf(" --queue-balance %u:%u", info->queuenum, last);
	} else
		printf(" --queue-num %u", info->queuenum);
243
244
245
246
247
248
249
250
251

	if (info->bypass & NFQ_FLAG_BYPASS)
		printf(" --queue-bypass");
}

static void NFQUEUE_save_v3(const void *ip,
			    const struct xt_entry_target *target)
{
	const struct xt_NFQ_info_v3 *info = (void *)target->data;
252
253
254
255
256
257
258
259
260
261
	unsigned int last = info->queues_total;

	if (last > 1) {
		last += info->queuenum - 1;
		printf(" --queue-balance %u:%u", info->queuenum, last);
	} else
		printf(" --queue-num %u", info->queuenum);

	if (info->flags & NFQ_FLAG_BYPASS)
		printf(" --queue-bypass");
262
263
264
265
266
267
268
269
270
271
272

	if (info->flags & NFQ_FLAG_CPU_FANOUT)
		printf(" --queue-cpu-fanout");
}

static void NFQUEUE_init_v1(struct xt_entry_target *t)
{
	struct xt_NFQ_info_v1 *tinfo = (void *)t->data;
	tinfo->queues_total = 1;
}

273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
static int NFQUEUE_xlate(struct xt_xlate *xl,
			 const struct xt_xlate_tg_params *params)
{
	const struct xt_NFQ_info *tinfo =
		(const struct xt_NFQ_info *)params->target->data;

	xt_xlate_add(xl, "queue num %u ", tinfo->queuenum);

	return 1;
}

static int NFQUEUE_xlate_v1(struct xt_xlate *xl,
			    const struct xt_xlate_tg_params *params)
{
	const struct xt_NFQ_info_v1 *tinfo = (const void *)params->target->data;
	unsigned int last = tinfo->queues_total;

	if (last > 1) {
		last += tinfo->queuenum - 1;
		xt_xlate_add(xl, "queue num %u-%u ", tinfo->queuenum, last);
	} else {
		xt_xlate_add(xl, "queue num %u ", tinfo->queuenum);
	}

	return 1;
}

static int NFQUEUE_xlate_v2(struct xt_xlate *xl,
			    const struct xt_xlate_tg_params *params)
{
	const struct xt_NFQ_info_v2 *info = (void *)params->target->data;
	unsigned int last = info->queues_total;

	if (last > 1) {
		last += info->queuenum - 1;
		xt_xlate_add(xl, "queue num %u-%u ", info->queuenum, last);
	} else
		xt_xlate_add(xl, "queue num %u ", info->queuenum);

	if (info->bypass & NFQ_FLAG_BYPASS)
		xt_xlate_add(xl, "bypass");

	return 1;
}

static int NFQUEUE_xlate_v3(struct xt_xlate *xl,
			    const struct xt_xlate_tg_params *params)
{
	const struct xt_NFQ_info_v3 *info = (void *)params->target->data;
	unsigned int last = info->queues_total;

	if (last > 1) {
		last += info->queuenum - 1;
		xt_xlate_add(xl, "queue num %u-%u ", info->queuenum, last);
	} else
		xt_xlate_add(xl, "queue num %u ", info->queuenum);

	if (info->flags & NFQ_FLAG_BYPASS)
		xt_xlate_add(xl, "bypass");

	if (info->flags & NFQ_FLAG_CPU_FANOUT)
		xt_xlate_add(xl, "%sfanout ",
			     info->flags & NFQ_FLAG_BYPASS ? "," : "");

	return 1;
}

340
341
342
343
344
345
346
347
348
349
350
static struct xtables_target nfqueue_targets[] = {
{
	.family		= NFPROTO_UNSPEC,
	.name		= "NFQUEUE",
	.version	= XTABLES_VERSION,
	.size		= XT_ALIGN(sizeof(struct xt_NFQ_info)),
	.userspacesize	= XT_ALIGN(sizeof(struct xt_NFQ_info)),
	.help		= NFQUEUE_help,
	.print		= NFQUEUE_print,
	.save		= NFQUEUE_save,
	.x6_parse	= NFQUEUE_parse,
351
352
	.x6_options	= NFQUEUE_opts,
	.xlate		= NFQUEUE_xlate,
353
354
355
356
357
358
359
360
361
362
363
364
365
},{
	.family		= NFPROTO_UNSPEC,
	.revision	= 1,
	.name		= "NFQUEUE",
	.version	= XTABLES_VERSION,
	.size		= XT_ALIGN(sizeof(struct xt_NFQ_info_v1)),
	.userspacesize	= XT_ALIGN(sizeof(struct xt_NFQ_info_v1)),
	.help		= NFQUEUE_help_v1,
	.init		= NFQUEUE_init_v1,
	.print		= NFQUEUE_print_v1,
	.save		= NFQUEUE_save_v1,
	.x6_parse	= NFQUEUE_parse_v1,
	.x6_options	= NFQUEUE_opts,
366
	.xlate		= NFQUEUE_xlate_v1,
367
368
369
370
371
372
373
374
375
376
377
378
379
},{
	.family		= NFPROTO_UNSPEC,
	.revision	= 2,
	.name		= "NFQUEUE",
	.version	= XTABLES_VERSION,
	.size		= XT_ALIGN(sizeof(struct xt_NFQ_info_v2)),
	.userspacesize	= XT_ALIGN(sizeof(struct xt_NFQ_info_v2)),
	.help		= NFQUEUE_help_v2,
	.init		= NFQUEUE_init_v1,
	.print		= NFQUEUE_print_v2,
	.save		= NFQUEUE_save_v2,
	.x6_parse	= NFQUEUE_parse_v2,
	.x6_options	= NFQUEUE_opts,
380
	.xlate		= NFQUEUE_xlate_v2,
381
382
383
384
385
386
387
388
389
390
391
392
393
},{
	.family		= NFPROTO_UNSPEC,
	.revision	= 3,
	.name		= "NFQUEUE",
	.version	= XTABLES_VERSION,
	.size		= XT_ALIGN(sizeof(struct xt_NFQ_info_v3)),
	.userspacesize	= XT_ALIGN(sizeof(struct xt_NFQ_info_v3)),
	.help		= NFQUEUE_help_v3,
	.init		= NFQUEUE_init_v1,
	.print		= NFQUEUE_print_v3,
	.save		= NFQUEUE_save_v3,
	.x6_parse	= NFQUEUE_parse_v3,
	.x6_options	= NFQUEUE_opts,
394
	.xlate		= NFQUEUE_xlate_v3,
395
396
397
398
399
400
401
}
};

void _init(void)
{
	xtables_register_targets(nfqueue_targets, ARRAY_SIZE(nfqueue_targets));
}