1 /* Shared library add-on to iptables for NFQ
3 * (C) 2005 by Harald Welte <laforge@netfilter.org>
5 * This program is distributed under the terms of GNU GPL v2, 1991
14 #include <linux/netfilter/x_tables.h>
15 #include <linux/netfilter/xt_NFQUEUE.h>
17 static void NFQUEUE_help(void)
20 "NFQUEUE target options\n"
21 " --queue-num value Send packet to QUEUE number <value>.\n"
22 " Valid queue numbers are 0-65535\n"
26 static void NFQUEUE_help_v1(void)
30 " --queue-balance first:last Balance flows between queues <value> to <value>.\n");
33 static const struct option NFQUEUE_opts[] = {
34 { "queue-num", 1, NULL, 'F' },
35 { "queue-balance", 1, NULL, 'B' },
39 static void exit_badqueue(const char *s)
41 xtables_error(PARAMETER_PROBLEM, "Invalid queue number `%s'\n", s);
45 parse_num(const char *s, struct xt_NFQ_info *tinfo)
49 if (!xtables_strtoui(s, NULL, &num, 0, UINT16_MAX))
52 tinfo->queuenum = num;
56 NFQUEUE_parse(int c, char **argv, int invert, unsigned int *flags,
57 const void *entry, struct xt_entry_target **target)
59 struct xt_NFQ_info *tinfo
60 = (struct xt_NFQ_info *)(*target)->data;
65 xtables_error(PARAMETER_PROBLEM, "NFQUEUE target: "
66 "Only use --queue-num ONCE!");
67 parse_num(optarg, tinfo);
70 xtables_error(PARAMETER_PROBLEM, "NFQUEUE target: "
71 "--queue-balance not supported (kernel too old?)");
80 NFQUEUE_parse_v1(int c, char **argv, int invert, unsigned int *flags,
81 const void *entry, struct xt_entry_target **target)
83 struct xt_NFQ_info_v1 *info = (void *)(*target)->data;
85 unsigned int firstqueue, lastqueue;
88 case 'F': /* fallthrough */
91 xtables_error(PARAMETER_PROBLEM, "NFQUEUE target: "
92 "Only use --queue-num ONCE!");
94 if (!xtables_strtoui(optarg, &colon, &firstqueue, 0, UINT16_MAX))
95 exit_badqueue(optarg);
97 info->queuenum = firstqueue;
101 exit_badqueue(optarg);
106 xtables_error(PARAMETER_PROBLEM, "Bad range \"%s\"", optarg);
108 if (!xtables_strtoui(colon + 1, NULL, &lastqueue, 1, UINT16_MAX))
109 exit_badqueue(optarg);
111 if (firstqueue >= lastqueue)
112 xtables_error(PARAMETER_PROBLEM, "%u should be less than %u",
113 firstqueue, lastqueue);
114 info->queues_total = lastqueue - firstqueue + 1;
123 static void NFQUEUE_print(const void *ip,
124 const struct xt_entry_target *target, int numeric)
126 const struct xt_NFQ_info *tinfo =
127 (const struct xt_NFQ_info *)target->data;
128 printf("NFQUEUE num %u", tinfo->queuenum);
131 static void NFQUEUE_print_v1(const void *ip,
132 const struct xt_entry_target *target, int numeric)
134 const struct xt_NFQ_info_v1 *tinfo = (const void *)target->data;
135 unsigned int last = tinfo->queues_total;
138 last += tinfo->queuenum - 1;
139 printf("NFQUEUE balance %u:%u", tinfo->queuenum, last);
141 printf("NFQUEUE num %u", tinfo->queuenum);
145 static void NFQUEUE_save(const void *ip, const struct xt_entry_target *target)
147 const struct xt_NFQ_info *tinfo =
148 (const struct xt_NFQ_info *)target->data;
150 printf("--queue-num %u ", tinfo->queuenum);
153 static void NFQUEUE_save_v1(const void *ip, const struct xt_entry_target *target)
155 const struct xt_NFQ_info_v1 *tinfo = (const void *)target->data;
156 unsigned int last = tinfo->queues_total;
159 last += tinfo->queuenum - 1;
160 printf("--queue-balance %u:%u ", tinfo->queuenum, last);
162 printf("--queue-num %u ", tinfo->queuenum);
166 static void NFQUEUE_init_v1(struct xt_entry_target *t)
168 struct xt_NFQ_info_v1 *tinfo = (void *)t->data;
169 tinfo->queues_total = 1;
172 static struct xtables_target nfqueue_target = {
173 .family = NFPROTO_UNSPEC,
175 .version = XTABLES_VERSION,
176 .size = XT_ALIGN(sizeof(struct xt_NFQ_info)),
177 .userspacesize = XT_ALIGN(sizeof(struct xt_NFQ_info)),
178 .help = NFQUEUE_help,
179 .parse = NFQUEUE_parse,
180 .print = NFQUEUE_print,
181 .save = NFQUEUE_save,
182 .extra_opts = NFQUEUE_opts
185 static struct xtables_target nfqueue_target_v1 = {
186 .family = NFPROTO_UNSPEC,
189 .version = XTABLES_VERSION,
190 .size = XT_ALIGN(sizeof(struct xt_NFQ_info_v1)),
191 .userspacesize = XT_ALIGN(sizeof(struct xt_NFQ_info_v1)),
192 .help = NFQUEUE_help_v1,
193 .init = NFQUEUE_init_v1,
194 .parse = NFQUEUE_parse_v1,
195 .print = NFQUEUE_print_v1,
196 .save = NFQUEUE_save_v1,
197 .extra_opts = NFQUEUE_opts,
202 xtables_register_target(&nfqueue_target);
203 xtables_register_target(&nfqueue_target_v1);