libxt_NFQUEUE.c revision a2e89ccf65e8c881e77674cd2b15b9704b0c6822
1/* Shared library add-on to iptables for NFQ
2 *
3 * (C) 2005 by Harald Welte <laforge@netfilter.org>
4 *
5 * This program is distributed under the terms of GNU GPL v2, 1991
6 *
7 */
8#include <stdio.h>
9#include <string.h>
10#include <stdlib.h>
11#include <getopt.h>
12
13#include <xtables.h>
14#include <linux/netfilter/x_tables.h>
15#include <linux/netfilter/xt_NFQUEUE.h>
16
17static void init(struct xt_entry_target *t, unsigned int *nfcache)
18{
19}
20
21static void help(void)
22{
23	printf(
24"NFQUEUE target options\n"
25"  --queue-num value		Send packet to QUEUE number <value>.\n"
26"  		                Valid queue numbers are 0-65535\n"
27);
28}
29
30static struct option opts[] = {
31	{ "queue-num", 1, 0, 'F' },
32	{ 0 }
33};
34
35static void
36parse_num(const char *s, struct xt_NFQ_info *tinfo)
37{
38	unsigned int num;
39
40	if (string_to_number(s, 0, 65535, &num) == -1)
41		exit_error(PARAMETER_PROBLEM,
42			   "Invalid queue number `%s'\n", s);
43
44    	tinfo->queuenum = num & 0xffff;
45    	return;
46}
47
48static int
49parse(int c, char **argv, int invert, unsigned int *flags,
50      const void *entry,
51      struct xt_entry_target **target)
52{
53	struct xt_NFQ_info *tinfo
54		= (struct xt_NFQ_info *)(*target)->data;
55
56	switch (c) {
57	case 'F':
58		if (*flags)
59			exit_error(PARAMETER_PROBLEM, "NFQUEUE target: "
60				   "Only use --queue-num ONCE!");
61		parse_num(optarg, tinfo);
62		break;
63	default:
64		return 0;
65	}
66
67	return 1;
68}
69
70static void
71final_check(unsigned int flags)
72{
73}
74
75/* Prints out the targinfo. */
76static void
77print(const void *ip,
78      const struct xt_entry_target *target,
79      int numeric)
80{
81	const struct xt_NFQ_info *tinfo =
82		(const struct xt_NFQ_info *)target->data;
83	printf("NFQUEUE num %u", tinfo->queuenum);
84}
85
86/* Saves the union ipt_targinfo in parsable form to stdout. */
87static void
88save(const void *ip, const struct xt_entry_target *target)
89{
90	const struct xt_NFQ_info *tinfo =
91		(const struct xt_NFQ_info *)target->data;
92
93	printf("--queue-num %u ", tinfo->queuenum);
94}
95
96static struct xtables_target nfqueue = {
97	.next		= NULL,
98	.family		= AF_INET,
99	.name		= "NFQUEUE",
100	.version	= IPTABLES_VERSION,
101	.size		= XT_ALIGN(sizeof(struct xt_NFQ_info)),
102	.userspacesize	= XT_ALIGN(sizeof(struct xt_NFQ_info)),
103	.help		= &help,
104	.init		= &init,
105	.parse		= &parse,
106	.final_check	= &final_check,
107	.print		= &print,
108	.save		= &save,
109	.extra_opts	= opts
110};
111
112static struct xtables_target nfqueue6 = {
113	.next		= NULL,
114	.family		= AF_INET6,
115	.name		= "NFQUEUE",
116	.version	= IPTABLES_VERSION,
117	.size		= XT_ALIGN(sizeof(struct xt_NFQ_info)),
118	.userspacesize	= XT_ALIGN(sizeof(struct xt_NFQ_info)),
119	.help		= &help,
120	.init		= &init,
121	.parse		= &parse,
122	.final_check	= &final_check,
123	.print		= &print,
124	.save		= &save,
125	.extra_opts	= opts
126};
127
128void _init(void)
129{
130	xtables_register_target(&nfqueue);
131	xtables_register_target(&nfqueue6);
132}
133