blob: cb901cf757760ce4118fc08ea97374c160ffa020 [file] [log] [blame]
Harald Welte7af4cc32005-08-09 19:44:15 -07001/*
2 * This is a module which is used for queueing packets and communicating with
3 * userspace via nfetlink.
4 *
5 * (C) 2005 by Harald Welte <laforge@netfilter.org>
6 *
7 * Based on the old ipv4-only ip_queue.c:
8 * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
9 * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 */
16#include <linux/module.h>
17#include <linux/skbuff.h>
18#include <linux/init.h>
19#include <linux/spinlock.h>
20#include <linux/notifier.h>
21#include <linux/netdevice.h>
22#include <linux/netfilter.h>
Harald Welte838ab632005-08-09 19:50:45 -070023#include <linux/proc_fs.h>
Harald Welte7af4cc32005-08-09 19:44:15 -070024#include <linux/netfilter_ipv4.h>
25#include <linux/netfilter_ipv6.h>
26#include <linux/netfilter/nfnetlink.h>
27#include <linux/netfilter/nfnetlink_queue.h>
28#include <linux/list.h>
29#include <net/sock.h>
Patrick McHardyc01cd422007-12-05 01:24:48 -080030#include <net/netfilter/nf_queue.h>
Harald Welte7af4cc32005-08-09 19:44:15 -070031
32#include <asm/atomic.h>
33
Harald Weltefbcd9232005-08-09 20:22:10 -070034#ifdef CONFIG_BRIDGE_NETFILTER
35#include "../bridge/br_private.h"
36#endif
37
Harald Welte7af4cc32005-08-09 19:44:15 -070038#define NFQNL_QMAX_DEFAULT 1024
39
40#if 0
41#define QDEBUG(x, args ...) printk(KERN_DEBUG "%s(%d):%s(): " x, \
42 __FILE__, __LINE__, __FUNCTION__, \
43 ## args)
44#else
45#define QDEBUG(x, ...)
46#endif
47
48struct nfqnl_queue_entry {
49 struct list_head list;
50 struct nf_info *info;
51 struct sk_buff *skb;
52 unsigned int id;
53};
54
55struct nfqnl_instance {
56 struct hlist_node hlist; /* global list of queues */
Harald Welte838ab632005-08-09 19:50:45 -070057 atomic_t use;
Harald Welte7af4cc32005-08-09 19:44:15 -070058
59 int peer_pid;
60 unsigned int queue_maxlen;
61 unsigned int copy_range;
62 unsigned int queue_total;
63 unsigned int queue_dropped;
64 unsigned int queue_user_dropped;
65
66 atomic_t id_sequence; /* 'sequence' of pkt ids */
67
68 u_int16_t queue_num; /* number of this queue */
69 u_int8_t copy_mode;
70
71 spinlock_t lock;
72
73 struct list_head queue_list; /* packets in queue */
74};
75
76typedef int (*nfqnl_cmpfn)(struct nfqnl_queue_entry *, unsigned long);
77
78static DEFINE_RWLOCK(instances_lock);
79
Harald Welte7af4cc32005-08-09 19:44:15 -070080#define INSTANCE_BUCKETS 16
81static struct hlist_head instance_table[INSTANCE_BUCKETS];
82
83static inline u_int8_t instance_hashfn(u_int16_t queue_num)
84{
85 return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
86}
87
88static struct nfqnl_instance *
89__instance_lookup(u_int16_t queue_num)
90{
91 struct hlist_head *head;
92 struct hlist_node *pos;
93 struct nfqnl_instance *inst;
94
95 head = &instance_table[instance_hashfn(queue_num)];
96 hlist_for_each_entry(inst, pos, head, hlist) {
97 if (inst->queue_num == queue_num)
98 return inst;
99 }
100 return NULL;
101}
102
103static struct nfqnl_instance *
Harald Welte838ab632005-08-09 19:50:45 -0700104instance_lookup_get(u_int16_t queue_num)
Harald Welte7af4cc32005-08-09 19:44:15 -0700105{
106 struct nfqnl_instance *inst;
107
108 read_lock_bh(&instances_lock);
109 inst = __instance_lookup(queue_num);
Harald Welte838ab632005-08-09 19:50:45 -0700110 if (inst)
111 atomic_inc(&inst->use);
Harald Welte7af4cc32005-08-09 19:44:15 -0700112 read_unlock_bh(&instances_lock);
113
114 return inst;
115}
116
Harald Welte838ab632005-08-09 19:50:45 -0700117static void
118instance_put(struct nfqnl_instance *inst)
119{
120 if (inst && atomic_dec_and_test(&inst->use)) {
121 QDEBUG("kfree(inst=%p)\n", inst);
122 kfree(inst);
123 }
124}
125
Harald Welte7af4cc32005-08-09 19:44:15 -0700126static struct nfqnl_instance *
127instance_create(u_int16_t queue_num, int pid)
128{
129 struct nfqnl_instance *inst;
130
131 QDEBUG("entering for queue_num=%u, pid=%d\n", queue_num, pid);
132
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800133 write_lock_bh(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700134 if (__instance_lookup(queue_num)) {
135 inst = NULL;
136 QDEBUG("aborting, instance already exists\n");
137 goto out_unlock;
138 }
139
Harald Welte10dfdc62005-11-03 19:20:07 +0100140 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
Harald Welte7af4cc32005-08-09 19:44:15 -0700141 if (!inst)
142 goto out_unlock;
143
Harald Welte7af4cc32005-08-09 19:44:15 -0700144 inst->queue_num = queue_num;
145 inst->peer_pid = pid;
146 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
147 inst->copy_range = 0xfffff;
148 inst->copy_mode = NFQNL_COPY_NONE;
149 atomic_set(&inst->id_sequence, 0);
Harald Welte838ab632005-08-09 19:50:45 -0700150 /* needs to be two, since we _put() after creation */
151 atomic_set(&inst->use, 2);
YOSHIFUJI Hideaki181a46a2006-01-04 13:56:54 -0800152 spin_lock_init(&inst->lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700153 INIT_LIST_HEAD(&inst->queue_list);
154
155 if (!try_module_get(THIS_MODULE))
156 goto out_free;
157
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800158 hlist_add_head(&inst->hlist,
Harald Welte7af4cc32005-08-09 19:44:15 -0700159 &instance_table[instance_hashfn(queue_num)]);
160
161 write_unlock_bh(&instances_lock);
162
163 QDEBUG("successfully created new instance\n");
164
165 return inst;
166
167out_free:
168 kfree(inst);
169out_unlock:
170 write_unlock_bh(&instances_lock);
171 return NULL;
172}
173
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800174static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
175 unsigned long data);
Harald Welte7af4cc32005-08-09 19:44:15 -0700176
177static void
178_instance_destroy2(struct nfqnl_instance *inst, int lock)
179{
180 /* first pull it out of the global list */
181 if (lock)
182 write_lock_bh(&instances_lock);
183
184 QDEBUG("removing instance %p (queuenum=%u) from hash\n",
185 inst, inst->queue_num);
186 hlist_del(&inst->hlist);
187
188 if (lock)
189 write_unlock_bh(&instances_lock);
190
191 /* then flush all pending skbs from the queue */
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800192 nfqnl_flush(inst, NULL, 0);
Harald Welte7af4cc32005-08-09 19:44:15 -0700193
Harald Welte838ab632005-08-09 19:50:45 -0700194 /* and finally put the refcount */
195 instance_put(inst);
Harald Welte7af4cc32005-08-09 19:44:15 -0700196
197 module_put(THIS_MODULE);
198}
199
200static inline void
201__instance_destroy(struct nfqnl_instance *inst)
202{
203 _instance_destroy2(inst, 0);
204}
205
206static inline void
207instance_destroy(struct nfqnl_instance *inst)
208{
209 _instance_destroy2(inst, 1);
210}
211
212
213
214static void
215issue_verdict(struct nfqnl_queue_entry *entry, int verdict)
216{
217 QDEBUG("entering for entry %p, verdict %u\n", entry, verdict);
218
219 /* TCP input path (and probably other bits) assume to be called
220 * from softirq context, not from syscall, like issue_verdict is
221 * called. TCP input path deadlocks with locks taken from timer
222 * softirq, e.g. We therefore emulate this by local_bh_disable() */
223
224 local_bh_disable();
225 nf_reinject(entry->skb, entry->info, verdict);
226 local_bh_enable();
227
228 kfree(entry);
229}
230
231static inline void
232__enqueue_entry(struct nfqnl_instance *queue,
233 struct nfqnl_queue_entry *entry)
234{
Patrick McHardy0ac41e82007-12-05 01:25:03 -0800235 list_add_tail(&entry->list, &queue->queue_list);
Harald Welte7af4cc32005-08-09 19:44:15 -0700236 queue->queue_total++;
237}
238
Harald Welte7af4cc32005-08-09 19:44:15 -0700239static inline int
240__nfqnl_set_mode(struct nfqnl_instance *queue,
241 unsigned char mode, unsigned int range)
242{
243 int status = 0;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800244
Harald Welte7af4cc32005-08-09 19:44:15 -0700245 switch (mode) {
246 case NFQNL_COPY_NONE:
247 case NFQNL_COPY_META:
248 queue->copy_mode = mode;
249 queue->copy_range = 0;
250 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800251
Harald Welte7af4cc32005-08-09 19:44:15 -0700252 case NFQNL_COPY_PACKET:
253 queue->copy_mode = mode;
Patrick McHardydf6fb862007-09-28 14:37:03 -0700254 /* we're using struct nlattr which has 16bit nla_len */
Harald Welte7af4cc32005-08-09 19:44:15 -0700255 if (range > 0xffff)
256 queue->copy_range = 0xffff;
257 else
258 queue->copy_range = range;
259 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800260
Harald Welte7af4cc32005-08-09 19:44:15 -0700261 default:
262 status = -EINVAL;
263
264 }
265 return status;
266}
267
268static struct nfqnl_queue_entry *
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800269find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
Harald Welte7af4cc32005-08-09 19:44:15 -0700270{
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800271 struct nfqnl_queue_entry *entry = NULL, *i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800272
Harald Welte7af4cc32005-08-09 19:44:15 -0700273 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800274
275 list_for_each_entry(i, &queue->queue_list, list) {
276 if (i->id == id) {
277 entry = i;
278 break;
279 }
280 }
281
282 if (entry) {
283 list_del(&entry->list);
284 queue->queue_total--;
285 }
286
Harald Welte7af4cc32005-08-09 19:44:15 -0700287 spin_unlock_bh(&queue->lock);
288
289 return entry;
290}
291
292static void
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800293nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
Harald Welte7af4cc32005-08-09 19:44:15 -0700294{
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800295 struct nfqnl_queue_entry *entry, *next;
296
Harald Welte7af4cc32005-08-09 19:44:15 -0700297 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800298 list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
299 if (!cmpfn || cmpfn(entry, data)) {
300 list_del(&entry->list);
301 queue->queue_total--;
302 issue_verdict(entry, NF_DROP);
303 }
304 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700305 spin_unlock_bh(&queue->lock);
306}
307
308static struct sk_buff *
309nfqnl_build_packet_message(struct nfqnl_instance *queue,
310 struct nfqnl_queue_entry *entry, int *errp)
311{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700312 sk_buff_data_t old_tail;
Harald Welte7af4cc32005-08-09 19:44:15 -0700313 size_t size;
314 size_t data_len = 0;
315 struct sk_buff *skb;
316 struct nfqnl_msg_packet_hdr pmsg;
317 struct nlmsghdr *nlh;
318 struct nfgenmsg *nfmsg;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800319 struct nf_info *entinf = entry->info;
320 struct sk_buff *entskb = entry->skb;
321 struct net_device *indev;
322 struct net_device *outdev;
Al Viro98a4a862006-11-08 00:26:51 -0800323 __be32 tmp_uint;
Harald Welte7af4cc32005-08-09 19:44:15 -0700324
325 QDEBUG("entered\n");
326
Patrick McHardydf6fb862007-09-28 14:37:03 -0700327 size = NLMSG_ALIGN(sizeof(struct nfgenmsg))
328 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
329 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
330 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700331#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700332 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
333 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700334#endif
Patrick McHardydf6fb862007-09-28 14:37:03 -0700335 + nla_total_size(sizeof(u_int32_t)) /* mark */
336 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
337 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
Harald Welte7af4cc32005-08-09 19:44:15 -0700338
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800339 outdev = entinf->outdev;
340
Harald Welte7af4cc32005-08-09 19:44:15 -0700341 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800342
Harald Welte7af4cc32005-08-09 19:44:15 -0700343 switch (queue->copy_mode) {
344 case NFQNL_COPY_META:
345 case NFQNL_COPY_NONE:
346 data_len = 0;
347 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800348
Harald Welte7af4cc32005-08-09 19:44:15 -0700349 case NFQNL_COPY_PACKET:
Patrick McHardy84fa7932006-08-29 16:44:56 -0700350 if ((entskb->ip_summed == CHECKSUM_PARTIAL ||
351 entskb->ip_summed == CHECKSUM_COMPLETE) &&
352 (*errp = skb_checksum_help(entskb))) {
Patrick McHardye7dfb092005-09-06 15:10:00 -0700353 spin_unlock_bh(&queue->lock);
354 return NULL;
355 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800356 if (queue->copy_range == 0
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800357 || queue->copy_range > entskb->len)
358 data_len = entskb->len;
Harald Welte7af4cc32005-08-09 19:44:15 -0700359 else
360 data_len = queue->copy_range;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800361
Patrick McHardydf6fb862007-09-28 14:37:03 -0700362 size += nla_total_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700363 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800364
Harald Welte7af4cc32005-08-09 19:44:15 -0700365 default:
366 *errp = -EINVAL;
367 spin_unlock_bh(&queue->lock);
368 return NULL;
369 }
370
371 spin_unlock_bh(&queue->lock);
372
373 skb = alloc_skb(size, GFP_ATOMIC);
374 if (!skb)
375 goto nlmsg_failure;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800376
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700377 old_tail = skb->tail;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800378 nlh = NLMSG_PUT(skb, 0, 0,
Harald Welte7af4cc32005-08-09 19:44:15 -0700379 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
380 sizeof(struct nfgenmsg));
381 nfmsg = NLMSG_DATA(nlh);
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800382 nfmsg->nfgen_family = entinf->pf;
Harald Welte7af4cc32005-08-09 19:44:15 -0700383 nfmsg->version = NFNETLINK_V0;
384 nfmsg->res_id = htons(queue->queue_num);
385
386 pmsg.packet_id = htonl(entry->id);
Al Virofebf0a42006-11-03 00:59:17 -0800387 pmsg.hw_protocol = entskb->protocol;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800388 pmsg.hook = entinf->hook;
Harald Welte7af4cc32005-08-09 19:44:15 -0700389
Patrick McHardydf6fb862007-09-28 14:37:03 -0700390 NLA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
Harald Welte7af4cc32005-08-09 19:44:15 -0700391
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800392 indev = entinf->indev;
393 if (indev) {
394 tmp_uint = htonl(indev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700395#ifndef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700396 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint);
Harald Weltefbcd9232005-08-09 20:22:10 -0700397#else
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800398 if (entinf->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700399 /* Case 1: indev is physical input device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800400 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700401 * netfilter_bridge) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700402 NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700403 &tmp_uint);
404 /* this is the bridge group "brX" */
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800405 tmp_uint = htonl(indev->br_port->br->dev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700406 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700407 &tmp_uint);
408 } else {
409 /* Case 2: indev is bridge group, we need to look for
410 * physical device (when called from ipv4) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700411 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700412 &tmp_uint);
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800413 if (entskb->nf_bridge
414 && entskb->nf_bridge->physindev) {
415 tmp_uint = htonl(entskb->nf_bridge->physindev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700416 NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV,
Harald Weltefbcd9232005-08-09 20:22:10 -0700417 sizeof(tmp_uint), &tmp_uint);
418 }
419 }
420#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700421 }
422
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800423 if (outdev) {
424 tmp_uint = htonl(outdev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700425#ifndef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700426 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint);
Harald Weltefbcd9232005-08-09 20:22:10 -0700427#else
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800428 if (entinf->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700429 /* Case 1: outdev is physical output device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800430 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700431 * netfilter_bridge) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700432 NLA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700433 &tmp_uint);
434 /* this is the bridge group "brX" */
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800435 tmp_uint = htonl(outdev->br_port->br->dev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700436 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700437 &tmp_uint);
438 } else {
439 /* Case 2: outdev is bridge group, we need to look for
440 * physical output device (when called from ipv4) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700441 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700442 &tmp_uint);
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800443 if (entskb->nf_bridge
444 && entskb->nf_bridge->physoutdev) {
445 tmp_uint = htonl(entskb->nf_bridge->physoutdev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700446 NLA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV,
Harald Weltefbcd9232005-08-09 20:22:10 -0700447 sizeof(tmp_uint), &tmp_uint);
448 }
449 }
450#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700451 }
452
Thomas Graf82e91ff2006-11-09 15:19:14 -0800453 if (entskb->mark) {
454 tmp_uint = htonl(entskb->mark);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700455 NLA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint);
Harald Welte7af4cc32005-08-09 19:44:15 -0700456 }
457
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700458 if (indev && entskb->dev) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700459 struct nfqnl_msg_packet_hw phw;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700460 int len = dev_parse_header(entskb, phw.hw_addr);
461 if (len) {
462 phw.hw_addrlen = htons(len);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700463 NLA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700464 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700465 }
466
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700467 if (entskb->tstamp.tv64) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700468 struct nfqnl_msg_packet_timestamp ts;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700469 struct timeval tv = ktime_to_timeval(entskb->tstamp);
470 ts.sec = cpu_to_be64(tv.tv_sec);
471 ts.usec = cpu_to_be64(tv.tv_usec);
Harald Welte7af4cc32005-08-09 19:44:15 -0700472
Patrick McHardydf6fb862007-09-28 14:37:03 -0700473 NLA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
Harald Welte7af4cc32005-08-09 19:44:15 -0700474 }
475
476 if (data_len) {
Patrick McHardydf6fb862007-09-28 14:37:03 -0700477 struct nlattr *nla;
478 int size = nla_attr_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700479
Patrick McHardydf6fb862007-09-28 14:37:03 -0700480 if (skb_tailroom(skb) < nla_total_size(data_len)) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700481 printk(KERN_WARNING "nf_queue: no tailroom!\n");
482 goto nlmsg_failure;
483 }
484
Patrick McHardydf6fb862007-09-28 14:37:03 -0700485 nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));
486 nla->nla_type = NFQA_PAYLOAD;
487 nla->nla_len = size;
Harald Welte7af4cc32005-08-09 19:44:15 -0700488
Patrick McHardydf6fb862007-09-28 14:37:03 -0700489 if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700490 BUG();
491 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800492
Harald Welte7af4cc32005-08-09 19:44:15 -0700493 nlh->nlmsg_len = skb->tail - old_tail;
494 return skb;
495
496nlmsg_failure:
Patrick McHardydf6fb862007-09-28 14:37:03 -0700497nla_put_failure:
Harald Welte7af4cc32005-08-09 19:44:15 -0700498 if (skb)
499 kfree_skb(skb);
500 *errp = -EINVAL;
501 if (net_ratelimit())
502 printk(KERN_ERR "nf_queue: error creating packet message\n");
503 return NULL;
504}
505
506static int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800507nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
Patrick McHardyf9d89282007-12-05 01:24:30 -0800508 unsigned int queuenum)
Harald Welte7af4cc32005-08-09 19:44:15 -0700509{
510 int status = -EINVAL;
511 struct sk_buff *nskb;
512 struct nfqnl_instance *queue;
513 struct nfqnl_queue_entry *entry;
514
515 QDEBUG("entered\n");
516
Harald Welte838ab632005-08-09 19:50:45 -0700517 queue = instance_lookup_get(queuenum);
Harald Welte7af4cc32005-08-09 19:44:15 -0700518 if (!queue) {
519 QDEBUG("no queue instance matching\n");
520 return -EINVAL;
521 }
522
523 if (queue->copy_mode == NFQNL_COPY_NONE) {
524 QDEBUG("mode COPY_NONE, aborting\n");
Harald Welte838ab632005-08-09 19:50:45 -0700525 status = -EAGAIN;
526 goto err_out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700527 }
528
529 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
530 if (entry == NULL) {
531 if (net_ratelimit())
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800532 printk(KERN_ERR
Harald Welte7af4cc32005-08-09 19:44:15 -0700533 "nf_queue: OOM in nfqnl_enqueue_packet()\n");
Harald Welte838ab632005-08-09 19:50:45 -0700534 status = -ENOMEM;
535 goto err_out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700536 }
537
538 entry->info = info;
539 entry->skb = skb;
540 entry->id = atomic_inc_return(&queue->id_sequence);
541
542 nskb = nfqnl_build_packet_message(queue, entry, &status);
543 if (nskb == NULL)
544 goto err_out_free;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800545
Harald Welte7af4cc32005-08-09 19:44:15 -0700546 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800547
Harald Welte7af4cc32005-08-09 19:44:15 -0700548 if (!queue->peer_pid)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800549 goto err_out_free_nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700550
551 if (queue->queue_total >= queue->queue_maxlen) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800552 queue->queue_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700553 status = -ENOSPC;
554 if (net_ratelimit())
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800555 printk(KERN_WARNING "nf_queue: full at %d entries, "
556 "dropping packets(s). Dropped: %d\n",
Harald Welte7af4cc32005-08-09 19:44:15 -0700557 queue->queue_total, queue->queue_dropped);
558 goto err_out_free_nskb;
559 }
560
561 /* nfnetlink_unicast will either free the nskb or add it to a socket */
562 status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
563 if (status < 0) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800564 queue->queue_user_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700565 goto err_out_unlock;
566 }
567
568 __enqueue_entry(queue, entry);
569
570 spin_unlock_bh(&queue->lock);
Harald Welte838ab632005-08-09 19:50:45 -0700571 instance_put(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700572 return status;
573
574err_out_free_nskb:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800575 kfree_skb(nskb);
576
Harald Welte7af4cc32005-08-09 19:44:15 -0700577err_out_unlock:
578 spin_unlock_bh(&queue->lock);
579
580err_out_free:
581 kfree(entry);
Harald Welte838ab632005-08-09 19:50:45 -0700582err_out_put:
583 instance_put(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700584 return status;
585}
586
587static int
588nfqnl_mangle(void *data, int data_len, struct nfqnl_queue_entry *e)
589{
590 int diff;
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700591 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700592
593 diff = data_len - e->skb->len;
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800594 if (diff < 0) {
595 if (pskb_trim(e->skb, data_len))
596 return -ENOMEM;
597 } else if (diff > 0) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700598 if (data_len > 0xFFFF)
599 return -EINVAL;
600 if (diff > skb_tailroom(e->skb)) {
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700601 err = pskb_expand_head(e->skb, 0,
602 diff - skb_tailroom(e->skb),
603 GFP_ATOMIC);
604 if (err) {
Patrick McHardy1158ba22006-08-22 00:32:47 -0700605 printk(KERN_WARNING "nf_queue: OOM "
Harald Welte7af4cc32005-08-09 19:44:15 -0700606 "in mangle, dropping packet\n");
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700607 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700608 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700609 }
610 skb_put(e->skb, diff);
611 }
Herbert Xu37d41872007-10-14 00:39:18 -0700612 if (!skb_make_writable(e->skb, data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700613 return -ENOMEM;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300614 skb_copy_to_linear_data(e->skb, data, data_len);
Patrick McHardye7dfb092005-09-06 15:10:00 -0700615 e->skb->ip_summed = CHECKSUM_NONE;
Harald Welte7af4cc32005-08-09 19:44:15 -0700616 return 0;
617}
618
Harald Welte7af4cc32005-08-09 19:44:15 -0700619static int
620nfqnl_set_mode(struct nfqnl_instance *queue,
621 unsigned char mode, unsigned int range)
622{
623 int status;
624
625 spin_lock_bh(&queue->lock);
626 status = __nfqnl_set_mode(queue, mode, range);
627 spin_unlock_bh(&queue->lock);
628
629 return status;
630}
631
632static int
633dev_cmp(struct nfqnl_queue_entry *entry, unsigned long ifindex)
634{
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800635 struct nf_info *entinf = entry->info;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800636
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800637 if (entinf->indev)
638 if (entinf->indev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700639 return 1;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800640 if (entinf->outdev)
641 if (entinf->outdev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700642 return 1;
Patrick McHardyef47c6a72006-06-27 03:01:48 -0700643#ifdef CONFIG_BRIDGE_NETFILTER
644 if (entry->skb->nf_bridge) {
645 if (entry->skb->nf_bridge->physindev &&
646 entry->skb->nf_bridge->physindev->ifindex == ifindex)
647 return 1;
648 if (entry->skb->nf_bridge->physoutdev &&
649 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
650 return 1;
651 }
652#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700653 return 0;
654}
655
656/* drop all packets with either indev or outdev == ifindex from all queue
657 * instances */
658static void
659nfqnl_dev_drop(int ifindex)
660{
661 int i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800662
Harald Welte7af4cc32005-08-09 19:44:15 -0700663 QDEBUG("entering for ifindex %u\n", ifindex);
664
665 /* this only looks like we have to hold the readlock for a way too long
666 * time, issue_verdict(), nf_reinject(), ... - but we always only
667 * issue NF_DROP, which is processed directly in nf_reinject() */
668 read_lock_bh(&instances_lock);
669
670 for (i = 0; i < INSTANCE_BUCKETS; i++) {
671 struct hlist_node *tmp;
672 struct nfqnl_instance *inst;
673 struct hlist_head *head = &instance_table[i];
674
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800675 hlist_for_each_entry(inst, tmp, head, hlist)
676 nfqnl_flush(inst, dev_cmp, ifindex);
Harald Welte7af4cc32005-08-09 19:44:15 -0700677 }
678
679 read_unlock_bh(&instances_lock);
680}
681
682#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
683
684static int
685nfqnl_rcv_dev_event(struct notifier_block *this,
686 unsigned long event, void *ptr)
687{
688 struct net_device *dev = ptr;
689
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200690 if (dev->nd_net != &init_net)
691 return NOTIFY_DONE;
692
Harald Welte7af4cc32005-08-09 19:44:15 -0700693 /* Drop any packets associated with the downed device */
694 if (event == NETDEV_DOWN)
695 nfqnl_dev_drop(dev->ifindex);
696 return NOTIFY_DONE;
697}
698
699static struct notifier_block nfqnl_dev_notifier = {
700 .notifier_call = nfqnl_rcv_dev_event,
701};
702
703static int
704nfqnl_rcv_nl_event(struct notifier_block *this,
705 unsigned long event, void *ptr)
706{
707 struct netlink_notify *n = ptr;
708
709 if (event == NETLINK_URELEASE &&
710 n->protocol == NETLINK_NETFILTER && n->pid) {
711 int i;
712
713 /* destroy all instances for this pid */
714 write_lock_bh(&instances_lock);
715 for (i = 0; i < INSTANCE_BUCKETS; i++) {
716 struct hlist_node *tmp, *t2;
717 struct nfqnl_instance *inst;
718 struct hlist_head *head = &instance_table[i];
719
720 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200721 if ((n->net == &init_net) &&
722 (n->pid == inst->peer_pid))
Harald Welte7af4cc32005-08-09 19:44:15 -0700723 __instance_destroy(inst);
724 }
725 }
726 write_unlock_bh(&instances_lock);
727 }
728 return NOTIFY_DONE;
729}
730
731static struct notifier_block nfqnl_rtnl_notifier = {
732 .notifier_call = nfqnl_rcv_nl_event,
733};
734
Patrick McHardy5bf75852007-09-28 14:39:26 -0700735static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
736 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
737 [NFQA_MARK] = { .type = NLA_U32 },
738 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
Harald Welte838ab632005-08-09 19:50:45 -0700739};
740
Harald Welte7af4cc32005-08-09 19:44:15 -0700741static int
742nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700743 struct nlmsghdr *nlh, struct nlattr *nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700744{
745 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
746 u_int16_t queue_num = ntohs(nfmsg->res_id);
747
748 struct nfqnl_msg_verdict_hdr *vhdr;
749 struct nfqnl_instance *queue;
750 unsigned int verdict;
751 struct nfqnl_queue_entry *entry;
Harald Welte838ab632005-08-09 19:50:45 -0700752 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700753
Harald Welte838ab632005-08-09 19:50:45 -0700754 queue = instance_lookup_get(queue_num);
Harald Welte7af4cc32005-08-09 19:44:15 -0700755 if (!queue)
756 return -ENODEV;
757
Harald Welte838ab632005-08-09 19:50:45 -0700758 if (queue->peer_pid != NETLINK_CB(skb).pid) {
759 err = -EPERM;
760 goto err_out_put;
761 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700762
Patrick McHardydf6fb862007-09-28 14:37:03 -0700763 if (!nfqa[NFQA_VERDICT_HDR]) {
Harald Welte838ab632005-08-09 19:50:45 -0700764 err = -EINVAL;
765 goto err_out_put;
766 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700767
Patrick McHardydf6fb862007-09-28 14:37:03 -0700768 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700769 verdict = ntohl(vhdr->verdict);
770
Harald Welte838ab632005-08-09 19:50:45 -0700771 if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
772 err = -EINVAL;
773 goto err_out_put;
774 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700775
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800776 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
Harald Welte838ab632005-08-09 19:50:45 -0700777 if (entry == NULL) {
778 err = -ENOENT;
779 goto err_out_put;
780 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700781
Patrick McHardydf6fb862007-09-28 14:37:03 -0700782 if (nfqa[NFQA_PAYLOAD]) {
783 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
784 nla_len(nfqa[NFQA_PAYLOAD]), entry) < 0)
Harald Welte7af4cc32005-08-09 19:44:15 -0700785 verdict = NF_DROP;
786 }
787
Patrick McHardydf6fb862007-09-28 14:37:03 -0700788 if (nfqa[NFQA_MARK])
Thomas Graf82e91ff2006-11-09 15:19:14 -0800789 entry->skb->mark = ntohl(*(__be32 *)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700790 nla_data(nfqa[NFQA_MARK]));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800791
Harald Welte7af4cc32005-08-09 19:44:15 -0700792 issue_verdict(entry, verdict);
Harald Welte838ab632005-08-09 19:50:45 -0700793 instance_put(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700794 return 0;
Harald Welte838ab632005-08-09 19:50:45 -0700795
796err_out_put:
797 instance_put(queue);
798 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700799}
800
801static int
802nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700803 struct nlmsghdr *nlh, struct nlattr *nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700804{
805 return -ENOTSUPP;
806}
807
Patrick McHardy5bf75852007-09-28 14:39:26 -0700808static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
809 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
810 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
Harald Welte838ab632005-08-09 19:50:45 -0700811};
812
Patrick McHardye3ac5292007-12-05 01:23:57 -0800813static const struct nf_queue_handler nfqh = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700814 .name = "nf_queue",
815 .outfn = &nfqnl_enqueue_packet,
816};
817
Harald Welte7af4cc32005-08-09 19:44:15 -0700818static int
819nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700820 struct nlmsghdr *nlh, struct nlattr *nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700821{
822 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
823 u_int16_t queue_num = ntohs(nfmsg->res_id);
824 struct nfqnl_instance *queue;
Harald Welte838ab632005-08-09 19:50:45 -0700825 int ret = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700826
827 QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type));
828
Harald Welte838ab632005-08-09 19:50:45 -0700829 queue = instance_lookup_get(queue_num);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700830 if (nfqa[NFQA_CFG_CMD]) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700831 struct nfqnl_msg_config_cmd *cmd;
Patrick McHardydf6fb862007-09-28 14:37:03 -0700832 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700833 QDEBUG("found CFG_CMD\n");
834
835 switch (cmd->command) {
836 case NFQNL_CFG_CMD_BIND:
837 if (queue)
838 return -EBUSY;
839
840 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
841 if (!queue)
842 return -EINVAL;
843 break;
844 case NFQNL_CFG_CMD_UNBIND:
845 if (!queue)
846 return -ENODEV;
847
Harald Welte838ab632005-08-09 19:50:45 -0700848 if (queue->peer_pid != NETLINK_CB(skb).pid) {
849 ret = -EPERM;
850 goto out_put;
851 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700852
853 instance_destroy(queue);
854 break;
855 case NFQNL_CFG_CMD_PF_BIND:
856 QDEBUG("registering queue handler for pf=%u\n",
857 ntohs(cmd->pf));
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700858 ret = nf_register_queue_handler(ntohs(cmd->pf), &nfqh);
Harald Welte7af4cc32005-08-09 19:44:15 -0700859 break;
860 case NFQNL_CFG_CMD_PF_UNBIND:
861 QDEBUG("unregistering queue handler for pf=%u\n",
862 ntohs(cmd->pf));
Yasuyuki Kozakaice7663d2007-07-07 22:40:08 -0700863 ret = nf_unregister_queue_handler(ntohs(cmd->pf), &nfqh);
Harald Welte7af4cc32005-08-09 19:44:15 -0700864 break;
865 default:
Harald Welte838ab632005-08-09 19:50:45 -0700866 ret = -EINVAL;
867 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700868 }
869 } else {
870 if (!queue) {
871 QDEBUG("no config command, and no instance ENOENT\n");
Harald Welte838ab632005-08-09 19:50:45 -0700872 ret = -ENOENT;
873 goto out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700874 }
875
876 if (queue->peer_pid != NETLINK_CB(skb).pid) {
877 QDEBUG("no config command, and wrong pid\n");
Harald Welte838ab632005-08-09 19:50:45 -0700878 ret = -EPERM;
879 goto out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700880 }
881 }
882
Patrick McHardydf6fb862007-09-28 14:37:03 -0700883 if (nfqa[NFQA_CFG_PARAMS]) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700884 struct nfqnl_msg_config_params *params;
Harald Welte7af4cc32005-08-09 19:44:15 -0700885
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800886 if (!queue) {
887 ret = -ENOENT;
888 goto out_put;
889 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700890 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700891 nfqnl_set_mode(queue, params->copy_mode,
892 ntohl(params->copy_range));
893 }
894
Patrick McHardydf6fb862007-09-28 14:37:03 -0700895 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
Eric Leblond829e17a2006-11-29 02:35:33 +0100896 __be32 *queue_maxlen;
Patrick McHardydf6fb862007-09-28 14:37:03 -0700897 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
Eric Leblond829e17a2006-11-29 02:35:33 +0100898 spin_lock_bh(&queue->lock);
899 queue->queue_maxlen = ntohl(*queue_maxlen);
900 spin_unlock_bh(&queue->lock);
901 }
902
Harald Welte838ab632005-08-09 19:50:45 -0700903out_put:
904 instance_put(queue);
905 return ret;
Harald Welte7af4cc32005-08-09 19:44:15 -0700906}
907
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700908static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
Harald Welte7af4cc32005-08-09 19:44:15 -0700909 [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp,
Harald Welte37d2e7a2005-11-14 15:24:59 -0800910 .attr_count = NFQA_MAX, },
Harald Welte7af4cc32005-08-09 19:44:15 -0700911 [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700912 .attr_count = NFQA_MAX,
913 .policy = nfqa_verdict_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700914 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700915 .attr_count = NFQA_CFG_MAX,
916 .policy = nfqa_cfg_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700917};
918
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700919static const struct nfnetlink_subsystem nfqnl_subsys = {
Harald Welte7af4cc32005-08-09 19:44:15 -0700920 .name = "nf_queue",
921 .subsys_id = NFNL_SUBSYS_QUEUE,
922 .cb_count = NFQNL_MSG_MAX,
Harald Welte7af4cc32005-08-09 19:44:15 -0700923 .cb = nfqnl_cb,
924};
925
Harald Welte838ab632005-08-09 19:50:45 -0700926#ifdef CONFIG_PROC_FS
927struct iter_state {
928 unsigned int bucket;
929};
930
931static struct hlist_node *get_first(struct seq_file *seq)
932{
933 struct iter_state *st = seq->private;
934
935 if (!st)
936 return NULL;
937
938 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
939 if (!hlist_empty(&instance_table[st->bucket]))
940 return instance_table[st->bucket].first;
941 }
942 return NULL;
943}
944
945static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
946{
947 struct iter_state *st = seq->private;
948
949 h = h->next;
950 while (!h) {
951 if (++st->bucket >= INSTANCE_BUCKETS)
952 return NULL;
953
954 h = instance_table[st->bucket].first;
955 }
956 return h;
957}
958
959static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
960{
961 struct hlist_node *head;
962 head = get_first(seq);
963
964 if (head)
965 while (pos && (head = get_next(seq, head)))
966 pos--;
967 return pos ? NULL : head;
968}
969
970static void *seq_start(struct seq_file *seq, loff_t *pos)
971{
972 read_lock_bh(&instances_lock);
973 return get_idx(seq, *pos);
974}
975
976static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
977{
978 (*pos)++;
979 return get_next(s, v);
980}
981
982static void seq_stop(struct seq_file *s, void *v)
983{
984 read_unlock_bh(&instances_lock);
985}
986
987static int seq_show(struct seq_file *s, void *v)
988{
989 const struct nfqnl_instance *inst = v;
990
991 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
992 inst->queue_num,
993 inst->peer_pid, inst->queue_total,
994 inst->copy_mode, inst->copy_range,
995 inst->queue_dropped, inst->queue_user_dropped,
996 atomic_read(&inst->id_sequence),
997 atomic_read(&inst->use));
998}
999
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001000static const struct seq_operations nfqnl_seq_ops = {
Harald Welte838ab632005-08-09 19:50:45 -07001001 .start = seq_start,
1002 .next = seq_next,
1003 .stop = seq_stop,
1004 .show = seq_show,
1005};
1006
1007static int nfqnl_open(struct inode *inode, struct file *file)
1008{
Pavel Emelyanove2da5912007-10-10 02:29:58 -07001009 return seq_open_private(file, &nfqnl_seq_ops,
1010 sizeof(struct iter_state));
Harald Welte838ab632005-08-09 19:50:45 -07001011}
1012
Arjan van de Venda7071d2007-02-12 00:55:36 -08001013static const struct file_operations nfqnl_file_ops = {
Harald Welte838ab632005-08-09 19:50:45 -07001014 .owner = THIS_MODULE,
1015 .open = nfqnl_open,
1016 .read = seq_read,
1017 .llseek = seq_lseek,
1018 .release = seq_release_private,
1019};
1020
1021#endif /* PROC_FS */
1022
Patrick McHardy32292a72006-04-06 14:11:30 -07001023static int __init nfnetlink_queue_init(void)
Harald Welte7af4cc32005-08-09 19:44:15 -07001024{
Harald Welte838ab632005-08-09 19:50:45 -07001025 int i, status = -ENOMEM;
1026#ifdef CONFIG_PROC_FS
1027 struct proc_dir_entry *proc_nfqueue;
1028#endif
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001029
Harald Welte838ab632005-08-09 19:50:45 -07001030 for (i = 0; i < INSTANCE_BUCKETS; i++)
1031 INIT_HLIST_HEAD(&instance_table[i]);
1032
Harald Welte7af4cc32005-08-09 19:44:15 -07001033 netlink_register_notifier(&nfqnl_rtnl_notifier);
1034 status = nfnetlink_subsys_register(&nfqnl_subsys);
1035 if (status < 0) {
1036 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
1037 goto cleanup_netlink_notifier;
1038 }
1039
Harald Welte838ab632005-08-09 19:50:45 -07001040#ifdef CONFIG_PROC_FS
1041 proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440,
1042 proc_net_netfilter);
1043 if (!proc_nfqueue)
1044 goto cleanup_subsys;
1045 proc_nfqueue->proc_fops = &nfqnl_file_ops;
1046#endif
1047
Harald Welte7af4cc32005-08-09 19:44:15 -07001048 register_netdevice_notifier(&nfqnl_dev_notifier);
1049 return status;
1050
Harald Welte838ab632005-08-09 19:50:45 -07001051#ifdef CONFIG_PROC_FS
1052cleanup_subsys:
Harald Welte7af4cc32005-08-09 19:44:15 -07001053 nfnetlink_subsys_unregister(&nfqnl_subsys);
Patrick McHardy32292a72006-04-06 14:11:30 -07001054#endif
Harald Welte7af4cc32005-08-09 19:44:15 -07001055cleanup_netlink_notifier:
1056 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1057 return status;
1058}
1059
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001060static void __exit nfnetlink_queue_fini(void)
Harald Welte7af4cc32005-08-09 19:44:15 -07001061{
Patrick McHardy32292a72006-04-06 14:11:30 -07001062 nf_unregister_queue_handlers(&nfqh);
1063 unregister_netdevice_notifier(&nfqnl_dev_notifier);
1064#ifdef CONFIG_PROC_FS
1065 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
1066#endif
1067 nfnetlink_subsys_unregister(&nfqnl_subsys);
1068 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
Harald Welte7af4cc32005-08-09 19:44:15 -07001069}
1070
1071MODULE_DESCRIPTION("netfilter packet queue handler");
1072MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1073MODULE_LICENSE("GPL");
1074MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1075
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001076module_init(nfnetlink_queue_init);
1077module_exit(nfnetlink_queue_fini);