blob: 49f0480afe099ac4643bf895eb05d68256b563a6 [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>
30
31#include <asm/atomic.h>
32
Harald Weltefbcd9232005-08-09 20:22:10 -070033#ifdef CONFIG_BRIDGE_NETFILTER
34#include "../bridge/br_private.h"
35#endif
36
Harald Welte7af4cc32005-08-09 19:44:15 -070037#define NFQNL_QMAX_DEFAULT 1024
38
39#if 0
40#define QDEBUG(x, args ...) printk(KERN_DEBUG "%s(%d):%s(): " x, \
41 __FILE__, __LINE__, __FUNCTION__, \
42 ## args)
43#else
44#define QDEBUG(x, ...)
45#endif
46
47struct nfqnl_queue_entry {
48 struct list_head list;
49 struct nf_info *info;
50 struct sk_buff *skb;
51 unsigned int id;
52};
53
54struct nfqnl_instance {
55 struct hlist_node hlist; /* global list of queues */
Harald Welte838ab632005-08-09 19:50:45 -070056 atomic_t use;
Harald Welte7af4cc32005-08-09 19:44:15 -070057
58 int peer_pid;
59 unsigned int queue_maxlen;
60 unsigned int copy_range;
61 unsigned int queue_total;
62 unsigned int queue_dropped;
63 unsigned int queue_user_dropped;
64
65 atomic_t id_sequence; /* 'sequence' of pkt ids */
66
67 u_int16_t queue_num; /* number of this queue */
68 u_int8_t copy_mode;
69
70 spinlock_t lock;
71
72 struct list_head queue_list; /* packets in queue */
73};
74
75typedef int (*nfqnl_cmpfn)(struct nfqnl_queue_entry *, unsigned long);
76
77static DEFINE_RWLOCK(instances_lock);
78
Harald Welte7af4cc32005-08-09 19:44:15 -070079#define INSTANCE_BUCKETS 16
80static struct hlist_head instance_table[INSTANCE_BUCKETS];
81
82static inline u_int8_t instance_hashfn(u_int16_t queue_num)
83{
84 return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
85}
86
87static struct nfqnl_instance *
88__instance_lookup(u_int16_t queue_num)
89{
90 struct hlist_head *head;
91 struct hlist_node *pos;
92 struct nfqnl_instance *inst;
93
94 head = &instance_table[instance_hashfn(queue_num)];
95 hlist_for_each_entry(inst, pos, head, hlist) {
96 if (inst->queue_num == queue_num)
97 return inst;
98 }
99 return NULL;
100}
101
102static struct nfqnl_instance *
Harald Welte838ab632005-08-09 19:50:45 -0700103instance_lookup_get(u_int16_t queue_num)
Harald Welte7af4cc32005-08-09 19:44:15 -0700104{
105 struct nfqnl_instance *inst;
106
107 read_lock_bh(&instances_lock);
108 inst = __instance_lookup(queue_num);
Harald Welte838ab632005-08-09 19:50:45 -0700109 if (inst)
110 atomic_inc(&inst->use);
Harald Welte7af4cc32005-08-09 19:44:15 -0700111 read_unlock_bh(&instances_lock);
112
113 return inst;
114}
115
Harald Welte838ab632005-08-09 19:50:45 -0700116static void
117instance_put(struct nfqnl_instance *inst)
118{
119 if (inst && atomic_dec_and_test(&inst->use)) {
120 QDEBUG("kfree(inst=%p)\n", inst);
121 kfree(inst);
122 }
123}
124
Harald Welte7af4cc32005-08-09 19:44:15 -0700125static struct nfqnl_instance *
126instance_create(u_int16_t queue_num, int pid)
127{
128 struct nfqnl_instance *inst;
129
130 QDEBUG("entering for queue_num=%u, pid=%d\n", queue_num, pid);
131
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800132 write_lock_bh(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700133 if (__instance_lookup(queue_num)) {
134 inst = NULL;
135 QDEBUG("aborting, instance already exists\n");
136 goto out_unlock;
137 }
138
Harald Welte10dfdc62005-11-03 19:20:07 +0100139 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
Harald Welte7af4cc32005-08-09 19:44:15 -0700140 if (!inst)
141 goto out_unlock;
142
Harald Welte7af4cc32005-08-09 19:44:15 -0700143 inst->queue_num = queue_num;
144 inst->peer_pid = pid;
145 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
146 inst->copy_range = 0xfffff;
147 inst->copy_mode = NFQNL_COPY_NONE;
148 atomic_set(&inst->id_sequence, 0);
Harald Welte838ab632005-08-09 19:50:45 -0700149 /* needs to be two, since we _put() after creation */
150 atomic_set(&inst->use, 2);
YOSHIFUJI Hideaki181a46a2006-01-04 13:56:54 -0800151 spin_lock_init(&inst->lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700152 INIT_LIST_HEAD(&inst->queue_list);
153
154 if (!try_module_get(THIS_MODULE))
155 goto out_free;
156
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800157 hlist_add_head(&inst->hlist,
Harald Welte7af4cc32005-08-09 19:44:15 -0700158 &instance_table[instance_hashfn(queue_num)]);
159
160 write_unlock_bh(&instances_lock);
161
162 QDEBUG("successfully created new instance\n");
163
164 return inst;
165
166out_free:
167 kfree(inst);
168out_unlock:
169 write_unlock_bh(&instances_lock);
170 return NULL;
171}
172
173static void nfqnl_flush(struct nfqnl_instance *queue, int verdict);
174
175static void
176_instance_destroy2(struct nfqnl_instance *inst, int lock)
177{
178 /* first pull it out of the global list */
179 if (lock)
180 write_lock_bh(&instances_lock);
181
182 QDEBUG("removing instance %p (queuenum=%u) from hash\n",
183 inst, inst->queue_num);
184 hlist_del(&inst->hlist);
185
186 if (lock)
187 write_unlock_bh(&instances_lock);
188
189 /* then flush all pending skbs from the queue */
190 nfqnl_flush(inst, NF_DROP);
191
Harald Welte838ab632005-08-09 19:50:45 -0700192 /* and finally put the refcount */
193 instance_put(inst);
Harald Welte7af4cc32005-08-09 19:44:15 -0700194
195 module_put(THIS_MODULE);
196}
197
198static inline void
199__instance_destroy(struct nfqnl_instance *inst)
200{
201 _instance_destroy2(inst, 0);
202}
203
204static inline void
205instance_destroy(struct nfqnl_instance *inst)
206{
207 _instance_destroy2(inst, 1);
208}
209
210
211
212static void
213issue_verdict(struct nfqnl_queue_entry *entry, int verdict)
214{
215 QDEBUG("entering for entry %p, verdict %u\n", entry, verdict);
216
217 /* TCP input path (and probably other bits) assume to be called
218 * from softirq context, not from syscall, like issue_verdict is
219 * called. TCP input path deadlocks with locks taken from timer
220 * softirq, e.g. We therefore emulate this by local_bh_disable() */
221
222 local_bh_disable();
223 nf_reinject(entry->skb, entry->info, verdict);
224 local_bh_enable();
225
226 kfree(entry);
227}
228
229static inline void
230__enqueue_entry(struct nfqnl_instance *queue,
231 struct nfqnl_queue_entry *entry)
232{
233 list_add(&entry->list, &queue->queue_list);
234 queue->queue_total++;
235}
236
237/*
238 * Find and return a queued entry matched by cmpfn, or return the last
239 * entry if cmpfn is NULL.
240 */
241static inline struct nfqnl_queue_entry *
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800242__find_entry(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
Harald Welte7af4cc32005-08-09 19:44:15 -0700243 unsigned long data)
244{
245 struct list_head *p;
246
247 list_for_each_prev(p, &queue->queue_list) {
248 struct nfqnl_queue_entry *entry = (struct nfqnl_queue_entry *)p;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800249
Harald Welte7af4cc32005-08-09 19:44:15 -0700250 if (!cmpfn || cmpfn(entry, data))
251 return entry;
252 }
253 return NULL;
254}
255
256static inline void
257__dequeue_entry(struct nfqnl_instance *q, struct nfqnl_queue_entry *entry)
258{
259 list_del(&entry->list);
260 q->queue_total--;
261}
262
263static inline struct nfqnl_queue_entry *
264__find_dequeue_entry(struct nfqnl_instance *queue,
265 nfqnl_cmpfn cmpfn, unsigned long data)
266{
267 struct nfqnl_queue_entry *entry;
268
269 entry = __find_entry(queue, cmpfn, data);
270 if (entry == NULL)
271 return NULL;
272
273 __dequeue_entry(queue, entry);
274 return entry;
275}
276
277
278static inline void
279__nfqnl_flush(struct nfqnl_instance *queue, int verdict)
280{
281 struct nfqnl_queue_entry *entry;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800282
Harald Welte7af4cc32005-08-09 19:44:15 -0700283 while ((entry = __find_dequeue_entry(queue, NULL, 0)))
284 issue_verdict(entry, verdict);
285}
286
287static inline int
288__nfqnl_set_mode(struct nfqnl_instance *queue,
289 unsigned char mode, unsigned int range)
290{
291 int status = 0;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800292
Harald Welte7af4cc32005-08-09 19:44:15 -0700293 switch (mode) {
294 case NFQNL_COPY_NONE:
295 case NFQNL_COPY_META:
296 queue->copy_mode = mode;
297 queue->copy_range = 0;
298 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800299
Harald Welte7af4cc32005-08-09 19:44:15 -0700300 case NFQNL_COPY_PACKET:
301 queue->copy_mode = mode;
Patrick McHardydf6fb862007-09-28 14:37:03 -0700302 /* we're using struct nlattr which has 16bit nla_len */
Harald Welte7af4cc32005-08-09 19:44:15 -0700303 if (range > 0xffff)
304 queue->copy_range = 0xffff;
305 else
306 queue->copy_range = range;
307 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800308
Harald Welte7af4cc32005-08-09 19:44:15 -0700309 default:
310 status = -EINVAL;
311
312 }
313 return status;
314}
315
316static struct nfqnl_queue_entry *
317find_dequeue_entry(struct nfqnl_instance *queue,
318 nfqnl_cmpfn cmpfn, unsigned long data)
319{
320 struct nfqnl_queue_entry *entry;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800321
Harald Welte7af4cc32005-08-09 19:44:15 -0700322 spin_lock_bh(&queue->lock);
323 entry = __find_dequeue_entry(queue, cmpfn, data);
324 spin_unlock_bh(&queue->lock);
325
326 return entry;
327}
328
329static void
330nfqnl_flush(struct nfqnl_instance *queue, int verdict)
331{
332 spin_lock_bh(&queue->lock);
333 __nfqnl_flush(queue, verdict);
334 spin_unlock_bh(&queue->lock);
335}
336
337static struct sk_buff *
338nfqnl_build_packet_message(struct nfqnl_instance *queue,
339 struct nfqnl_queue_entry *entry, int *errp)
340{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700341 sk_buff_data_t old_tail;
Harald Welte7af4cc32005-08-09 19:44:15 -0700342 size_t size;
343 size_t data_len = 0;
344 struct sk_buff *skb;
345 struct nfqnl_msg_packet_hdr pmsg;
346 struct nlmsghdr *nlh;
347 struct nfgenmsg *nfmsg;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800348 struct nf_info *entinf = entry->info;
349 struct sk_buff *entskb = entry->skb;
350 struct net_device *indev;
351 struct net_device *outdev;
Al Viro98a4a862006-11-08 00:26:51 -0800352 __be32 tmp_uint;
Harald Welte7af4cc32005-08-09 19:44:15 -0700353
354 QDEBUG("entered\n");
355
Patrick McHardydf6fb862007-09-28 14:37:03 -0700356 size = NLMSG_ALIGN(sizeof(struct nfgenmsg))
357 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
358 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
359 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700360#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700361 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
362 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700363#endif
Patrick McHardydf6fb862007-09-28 14:37:03 -0700364 + nla_total_size(sizeof(u_int32_t)) /* mark */
365 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
366 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
Harald Welte7af4cc32005-08-09 19:44:15 -0700367
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800368 outdev = entinf->outdev;
369
Harald Welte7af4cc32005-08-09 19:44:15 -0700370 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800371
Harald Welte7af4cc32005-08-09 19:44:15 -0700372 switch (queue->copy_mode) {
373 case NFQNL_COPY_META:
374 case NFQNL_COPY_NONE:
375 data_len = 0;
376 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800377
Harald Welte7af4cc32005-08-09 19:44:15 -0700378 case NFQNL_COPY_PACKET:
Patrick McHardy84fa7932006-08-29 16:44:56 -0700379 if ((entskb->ip_summed == CHECKSUM_PARTIAL ||
380 entskb->ip_summed == CHECKSUM_COMPLETE) &&
381 (*errp = skb_checksum_help(entskb))) {
Patrick McHardye7dfb092005-09-06 15:10:00 -0700382 spin_unlock_bh(&queue->lock);
383 return NULL;
384 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800385 if (queue->copy_range == 0
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800386 || queue->copy_range > entskb->len)
387 data_len = entskb->len;
Harald Welte7af4cc32005-08-09 19:44:15 -0700388 else
389 data_len = queue->copy_range;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800390
Patrick McHardydf6fb862007-09-28 14:37:03 -0700391 size += nla_total_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700392 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800393
Harald Welte7af4cc32005-08-09 19:44:15 -0700394 default:
395 *errp = -EINVAL;
396 spin_unlock_bh(&queue->lock);
397 return NULL;
398 }
399
400 spin_unlock_bh(&queue->lock);
401
402 skb = alloc_skb(size, GFP_ATOMIC);
403 if (!skb)
404 goto nlmsg_failure;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800405
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700406 old_tail = skb->tail;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800407 nlh = NLMSG_PUT(skb, 0, 0,
Harald Welte7af4cc32005-08-09 19:44:15 -0700408 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
409 sizeof(struct nfgenmsg));
410 nfmsg = NLMSG_DATA(nlh);
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800411 nfmsg->nfgen_family = entinf->pf;
Harald Welte7af4cc32005-08-09 19:44:15 -0700412 nfmsg->version = NFNETLINK_V0;
413 nfmsg->res_id = htons(queue->queue_num);
414
415 pmsg.packet_id = htonl(entry->id);
Al Virofebf0a42006-11-03 00:59:17 -0800416 pmsg.hw_protocol = entskb->protocol;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800417 pmsg.hook = entinf->hook;
Harald Welte7af4cc32005-08-09 19:44:15 -0700418
Patrick McHardydf6fb862007-09-28 14:37:03 -0700419 NLA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
Harald Welte7af4cc32005-08-09 19:44:15 -0700420
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800421 indev = entinf->indev;
422 if (indev) {
423 tmp_uint = htonl(indev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700424#ifndef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700425 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint);
Harald Weltefbcd9232005-08-09 20:22:10 -0700426#else
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800427 if (entinf->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700428 /* Case 1: indev is physical input device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800429 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700430 * netfilter_bridge) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700431 NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700432 &tmp_uint);
433 /* this is the bridge group "brX" */
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800434 tmp_uint = htonl(indev->br_port->br->dev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700435 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700436 &tmp_uint);
437 } else {
438 /* Case 2: indev is bridge group, we need to look for
439 * physical device (when called from ipv4) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700440 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700441 &tmp_uint);
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800442 if (entskb->nf_bridge
443 && entskb->nf_bridge->physindev) {
444 tmp_uint = htonl(entskb->nf_bridge->physindev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700445 NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV,
Harald Weltefbcd9232005-08-09 20:22:10 -0700446 sizeof(tmp_uint), &tmp_uint);
447 }
448 }
449#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700450 }
451
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800452 if (outdev) {
453 tmp_uint = htonl(outdev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700454#ifndef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700455 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint);
Harald Weltefbcd9232005-08-09 20:22:10 -0700456#else
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800457 if (entinf->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700458 /* Case 1: outdev is physical output device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800459 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700460 * netfilter_bridge) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700461 NLA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700462 &tmp_uint);
463 /* this is the bridge group "brX" */
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800464 tmp_uint = htonl(outdev->br_port->br->dev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700465 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700466 &tmp_uint);
467 } else {
468 /* Case 2: outdev is bridge group, we need to look for
469 * physical output device (when called from ipv4) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700470 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700471 &tmp_uint);
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800472 if (entskb->nf_bridge
473 && entskb->nf_bridge->physoutdev) {
474 tmp_uint = htonl(entskb->nf_bridge->physoutdev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700475 NLA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV,
Harald Weltefbcd9232005-08-09 20:22:10 -0700476 sizeof(tmp_uint), &tmp_uint);
477 }
478 }
479#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700480 }
481
Thomas Graf82e91ff2006-11-09 15:19:14 -0800482 if (entskb->mark) {
483 tmp_uint = htonl(entskb->mark);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700484 NLA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint);
Harald Welte7af4cc32005-08-09 19:44:15 -0700485 }
486
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700487 if (indev && entskb->dev) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700488 struct nfqnl_msg_packet_hw phw;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700489 int len = dev_parse_header(entskb, phw.hw_addr);
490 if (len) {
491 phw.hw_addrlen = htons(len);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700492 NLA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700493 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700494 }
495
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700496 if (entskb->tstamp.tv64) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700497 struct nfqnl_msg_packet_timestamp ts;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700498 struct timeval tv = ktime_to_timeval(entskb->tstamp);
499 ts.sec = cpu_to_be64(tv.tv_sec);
500 ts.usec = cpu_to_be64(tv.tv_usec);
Harald Welte7af4cc32005-08-09 19:44:15 -0700501
Patrick McHardydf6fb862007-09-28 14:37:03 -0700502 NLA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
Harald Welte7af4cc32005-08-09 19:44:15 -0700503 }
504
505 if (data_len) {
Patrick McHardydf6fb862007-09-28 14:37:03 -0700506 struct nlattr *nla;
507 int size = nla_attr_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700508
Patrick McHardydf6fb862007-09-28 14:37:03 -0700509 if (skb_tailroom(skb) < nla_total_size(data_len)) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700510 printk(KERN_WARNING "nf_queue: no tailroom!\n");
511 goto nlmsg_failure;
512 }
513
Patrick McHardydf6fb862007-09-28 14:37:03 -0700514 nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));
515 nla->nla_type = NFQA_PAYLOAD;
516 nla->nla_len = size;
Harald Welte7af4cc32005-08-09 19:44:15 -0700517
Patrick McHardydf6fb862007-09-28 14:37:03 -0700518 if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700519 BUG();
520 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800521
Harald Welte7af4cc32005-08-09 19:44:15 -0700522 nlh->nlmsg_len = skb->tail - old_tail;
523 return skb;
524
525nlmsg_failure:
Patrick McHardydf6fb862007-09-28 14:37:03 -0700526nla_put_failure:
Harald Welte7af4cc32005-08-09 19:44:15 -0700527 if (skb)
528 kfree_skb(skb);
529 *errp = -EINVAL;
530 if (net_ratelimit())
531 printk(KERN_ERR "nf_queue: error creating packet message\n");
532 return NULL;
533}
534
535static int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800536nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
Harald Welte7af4cc32005-08-09 19:44:15 -0700537 unsigned int queuenum, void *data)
538{
539 int status = -EINVAL;
540 struct sk_buff *nskb;
541 struct nfqnl_instance *queue;
542 struct nfqnl_queue_entry *entry;
543
544 QDEBUG("entered\n");
545
Harald Welte838ab632005-08-09 19:50:45 -0700546 queue = instance_lookup_get(queuenum);
Harald Welte7af4cc32005-08-09 19:44:15 -0700547 if (!queue) {
548 QDEBUG("no queue instance matching\n");
549 return -EINVAL;
550 }
551
552 if (queue->copy_mode == NFQNL_COPY_NONE) {
553 QDEBUG("mode COPY_NONE, aborting\n");
Harald Welte838ab632005-08-09 19:50:45 -0700554 status = -EAGAIN;
555 goto err_out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700556 }
557
558 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
559 if (entry == NULL) {
560 if (net_ratelimit())
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800561 printk(KERN_ERR
Harald Welte7af4cc32005-08-09 19:44:15 -0700562 "nf_queue: OOM in nfqnl_enqueue_packet()\n");
Harald Welte838ab632005-08-09 19:50:45 -0700563 status = -ENOMEM;
564 goto err_out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700565 }
566
567 entry->info = info;
568 entry->skb = skb;
569 entry->id = atomic_inc_return(&queue->id_sequence);
570
571 nskb = nfqnl_build_packet_message(queue, entry, &status);
572 if (nskb == NULL)
573 goto err_out_free;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800574
Harald Welte7af4cc32005-08-09 19:44:15 -0700575 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800576
Harald Welte7af4cc32005-08-09 19:44:15 -0700577 if (!queue->peer_pid)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800578 goto err_out_free_nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700579
580 if (queue->queue_total >= queue->queue_maxlen) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800581 queue->queue_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700582 status = -ENOSPC;
583 if (net_ratelimit())
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800584 printk(KERN_WARNING "nf_queue: full at %d entries, "
585 "dropping packets(s). Dropped: %d\n",
Harald Welte7af4cc32005-08-09 19:44:15 -0700586 queue->queue_total, queue->queue_dropped);
587 goto err_out_free_nskb;
588 }
589
590 /* nfnetlink_unicast will either free the nskb or add it to a socket */
591 status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
592 if (status < 0) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800593 queue->queue_user_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700594 goto err_out_unlock;
595 }
596
597 __enqueue_entry(queue, entry);
598
599 spin_unlock_bh(&queue->lock);
Harald Welte838ab632005-08-09 19:50:45 -0700600 instance_put(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700601 return status;
602
603err_out_free_nskb:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800604 kfree_skb(nskb);
605
Harald Welte7af4cc32005-08-09 19:44:15 -0700606err_out_unlock:
607 spin_unlock_bh(&queue->lock);
608
609err_out_free:
610 kfree(entry);
Harald Welte838ab632005-08-09 19:50:45 -0700611err_out_put:
612 instance_put(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700613 return status;
614}
615
616static int
617nfqnl_mangle(void *data, int data_len, struct nfqnl_queue_entry *e)
618{
619 int diff;
620
621 diff = data_len - e->skb->len;
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800622 if (diff < 0) {
623 if (pskb_trim(e->skb, data_len))
624 return -ENOMEM;
625 } else if (diff > 0) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700626 if (data_len > 0xFFFF)
627 return -EINVAL;
628 if (diff > skb_tailroom(e->skb)) {
629 struct sk_buff *newskb;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800630
Harald Welte7af4cc32005-08-09 19:44:15 -0700631 newskb = skb_copy_expand(e->skb,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800632 skb_headroom(e->skb),
633 diff,
634 GFP_ATOMIC);
Harald Welte7af4cc32005-08-09 19:44:15 -0700635 if (newskb == NULL) {
Patrick McHardy1158ba22006-08-22 00:32:47 -0700636 printk(KERN_WARNING "nf_queue: OOM "
Harald Welte7af4cc32005-08-09 19:44:15 -0700637 "in mangle, dropping packet\n");
638 return -ENOMEM;
639 }
640 if (e->skb->sk)
641 skb_set_owner_w(newskb, e->skb->sk);
642 kfree_skb(e->skb);
643 e->skb = newskb;
644 }
645 skb_put(e->skb, diff);
646 }
647 if (!skb_make_writable(&e->skb, data_len))
648 return -ENOMEM;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300649 skb_copy_to_linear_data(e->skb, data, data_len);
Patrick McHardye7dfb092005-09-06 15:10:00 -0700650 e->skb->ip_summed = CHECKSUM_NONE;
Harald Welte7af4cc32005-08-09 19:44:15 -0700651 return 0;
652}
653
654static inline int
655id_cmp(struct nfqnl_queue_entry *e, unsigned long id)
656{
657 return (id == e->id);
658}
659
660static int
661nfqnl_set_mode(struct nfqnl_instance *queue,
662 unsigned char mode, unsigned int range)
663{
664 int status;
665
666 spin_lock_bh(&queue->lock);
667 status = __nfqnl_set_mode(queue, mode, range);
668 spin_unlock_bh(&queue->lock);
669
670 return status;
671}
672
673static int
674dev_cmp(struct nfqnl_queue_entry *entry, unsigned long ifindex)
675{
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800676 struct nf_info *entinf = entry->info;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800677
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800678 if (entinf->indev)
679 if (entinf->indev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700680 return 1;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800681 if (entinf->outdev)
682 if (entinf->outdev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700683 return 1;
Patrick McHardyef47c6a2006-06-27 03:01:48 -0700684#ifdef CONFIG_BRIDGE_NETFILTER
685 if (entry->skb->nf_bridge) {
686 if (entry->skb->nf_bridge->physindev &&
687 entry->skb->nf_bridge->physindev->ifindex == ifindex)
688 return 1;
689 if (entry->skb->nf_bridge->physoutdev &&
690 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
691 return 1;
692 }
693#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700694 return 0;
695}
696
697/* drop all packets with either indev or outdev == ifindex from all queue
698 * instances */
699static void
700nfqnl_dev_drop(int ifindex)
701{
702 int i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800703
Harald Welte7af4cc32005-08-09 19:44:15 -0700704 QDEBUG("entering for ifindex %u\n", ifindex);
705
706 /* this only looks like we have to hold the readlock for a way too long
707 * time, issue_verdict(), nf_reinject(), ... - but we always only
708 * issue NF_DROP, which is processed directly in nf_reinject() */
709 read_lock_bh(&instances_lock);
710
711 for (i = 0; i < INSTANCE_BUCKETS; i++) {
712 struct hlist_node *tmp;
713 struct nfqnl_instance *inst;
714 struct hlist_head *head = &instance_table[i];
715
716 hlist_for_each_entry(inst, tmp, head, hlist) {
717 struct nfqnl_queue_entry *entry;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800718 while ((entry = find_dequeue_entry(inst, dev_cmp,
Harald Welte7af4cc32005-08-09 19:44:15 -0700719 ifindex)) != NULL)
720 issue_verdict(entry, NF_DROP);
721 }
722 }
723
724 read_unlock_bh(&instances_lock);
725}
726
727#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
728
729static int
730nfqnl_rcv_dev_event(struct notifier_block *this,
731 unsigned long event, void *ptr)
732{
733 struct net_device *dev = ptr;
734
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200735 if (dev->nd_net != &init_net)
736 return NOTIFY_DONE;
737
Harald Welte7af4cc32005-08-09 19:44:15 -0700738 /* Drop any packets associated with the downed device */
739 if (event == NETDEV_DOWN)
740 nfqnl_dev_drop(dev->ifindex);
741 return NOTIFY_DONE;
742}
743
744static struct notifier_block nfqnl_dev_notifier = {
745 .notifier_call = nfqnl_rcv_dev_event,
746};
747
748static int
749nfqnl_rcv_nl_event(struct notifier_block *this,
750 unsigned long event, void *ptr)
751{
752 struct netlink_notify *n = ptr;
753
754 if (event == NETLINK_URELEASE &&
755 n->protocol == NETLINK_NETFILTER && n->pid) {
756 int i;
757
758 /* destroy all instances for this pid */
759 write_lock_bh(&instances_lock);
760 for (i = 0; i < INSTANCE_BUCKETS; i++) {
761 struct hlist_node *tmp, *t2;
762 struct nfqnl_instance *inst;
763 struct hlist_head *head = &instance_table[i];
764
765 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200766 if ((n->net == &init_net) &&
767 (n->pid == inst->peer_pid))
Harald Welte7af4cc32005-08-09 19:44:15 -0700768 __instance_destroy(inst);
769 }
770 }
771 write_unlock_bh(&instances_lock);
772 }
773 return NOTIFY_DONE;
774}
775
776static struct notifier_block nfqnl_rtnl_notifier = {
777 .notifier_call = nfqnl_rcv_nl_event,
778};
779
Patrick McHardy5bf75852007-09-28 14:39:26 -0700780static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
781 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
782 [NFQA_MARK] = { .type = NLA_U32 },
783 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
Harald Welte838ab632005-08-09 19:50:45 -0700784};
785
Harald Welte7af4cc32005-08-09 19:44:15 -0700786static int
787nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700788 struct nlmsghdr *nlh, struct nlattr *nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700789{
790 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
791 u_int16_t queue_num = ntohs(nfmsg->res_id);
792
793 struct nfqnl_msg_verdict_hdr *vhdr;
794 struct nfqnl_instance *queue;
795 unsigned int verdict;
796 struct nfqnl_queue_entry *entry;
Harald Welte838ab632005-08-09 19:50:45 -0700797 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700798
Harald Welte838ab632005-08-09 19:50:45 -0700799 queue = instance_lookup_get(queue_num);
Harald Welte7af4cc32005-08-09 19:44:15 -0700800 if (!queue)
801 return -ENODEV;
802
Harald Welte838ab632005-08-09 19:50:45 -0700803 if (queue->peer_pid != NETLINK_CB(skb).pid) {
804 err = -EPERM;
805 goto err_out_put;
806 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700807
Patrick McHardydf6fb862007-09-28 14:37:03 -0700808 if (!nfqa[NFQA_VERDICT_HDR]) {
Harald Welte838ab632005-08-09 19:50:45 -0700809 err = -EINVAL;
810 goto err_out_put;
811 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700812
Patrick McHardydf6fb862007-09-28 14:37:03 -0700813 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700814 verdict = ntohl(vhdr->verdict);
815
Harald Welte838ab632005-08-09 19:50:45 -0700816 if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
817 err = -EINVAL;
818 goto err_out_put;
819 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700820
821 entry = find_dequeue_entry(queue, id_cmp, ntohl(vhdr->id));
Harald Welte838ab632005-08-09 19:50:45 -0700822 if (entry == NULL) {
823 err = -ENOENT;
824 goto err_out_put;
825 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700826
Patrick McHardydf6fb862007-09-28 14:37:03 -0700827 if (nfqa[NFQA_PAYLOAD]) {
828 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
829 nla_len(nfqa[NFQA_PAYLOAD]), entry) < 0)
Harald Welte7af4cc32005-08-09 19:44:15 -0700830 verdict = NF_DROP;
831 }
832
Patrick McHardydf6fb862007-09-28 14:37:03 -0700833 if (nfqa[NFQA_MARK])
Thomas Graf82e91ff2006-11-09 15:19:14 -0800834 entry->skb->mark = ntohl(*(__be32 *)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700835 nla_data(nfqa[NFQA_MARK]));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800836
Harald Welte7af4cc32005-08-09 19:44:15 -0700837 issue_verdict(entry, verdict);
Harald Welte838ab632005-08-09 19:50:45 -0700838 instance_put(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700839 return 0;
Harald Welte838ab632005-08-09 19:50:45 -0700840
841err_out_put:
842 instance_put(queue);
843 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700844}
845
846static int
847nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700848 struct nlmsghdr *nlh, struct nlattr *nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700849{
850 return -ENOTSUPP;
851}
852
Patrick McHardy5bf75852007-09-28 14:39:26 -0700853static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
854 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
855 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
Harald Welte838ab632005-08-09 19:50:45 -0700856};
857
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700858static struct nf_queue_handler nfqh = {
859 .name = "nf_queue",
860 .outfn = &nfqnl_enqueue_packet,
861};
862
Harald Welte7af4cc32005-08-09 19:44:15 -0700863static int
864nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700865 struct nlmsghdr *nlh, struct nlattr *nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700866{
867 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
868 u_int16_t queue_num = ntohs(nfmsg->res_id);
869 struct nfqnl_instance *queue;
Harald Welte838ab632005-08-09 19:50:45 -0700870 int ret = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700871
872 QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type));
873
Harald Welte838ab632005-08-09 19:50:45 -0700874 queue = instance_lookup_get(queue_num);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700875 if (nfqa[NFQA_CFG_CMD]) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700876 struct nfqnl_msg_config_cmd *cmd;
Patrick McHardydf6fb862007-09-28 14:37:03 -0700877 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700878 QDEBUG("found CFG_CMD\n");
879
880 switch (cmd->command) {
881 case NFQNL_CFG_CMD_BIND:
882 if (queue)
883 return -EBUSY;
884
885 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
886 if (!queue)
887 return -EINVAL;
888 break;
889 case NFQNL_CFG_CMD_UNBIND:
890 if (!queue)
891 return -ENODEV;
892
Harald Welte838ab632005-08-09 19:50:45 -0700893 if (queue->peer_pid != NETLINK_CB(skb).pid) {
894 ret = -EPERM;
895 goto out_put;
896 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700897
898 instance_destroy(queue);
899 break;
900 case NFQNL_CFG_CMD_PF_BIND:
901 QDEBUG("registering queue handler for pf=%u\n",
902 ntohs(cmd->pf));
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700903 ret = nf_register_queue_handler(ntohs(cmd->pf), &nfqh);
Harald Welte7af4cc32005-08-09 19:44:15 -0700904 break;
905 case NFQNL_CFG_CMD_PF_UNBIND:
906 QDEBUG("unregistering queue handler for pf=%u\n",
907 ntohs(cmd->pf));
Yasuyuki Kozakaice7663d2007-07-07 22:40:08 -0700908 ret = nf_unregister_queue_handler(ntohs(cmd->pf), &nfqh);
Harald Welte7af4cc32005-08-09 19:44:15 -0700909 break;
910 default:
Harald Welte838ab632005-08-09 19:50:45 -0700911 ret = -EINVAL;
912 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700913 }
914 } else {
915 if (!queue) {
916 QDEBUG("no config command, and no instance ENOENT\n");
Harald Welte838ab632005-08-09 19:50:45 -0700917 ret = -ENOENT;
918 goto out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700919 }
920
921 if (queue->peer_pid != NETLINK_CB(skb).pid) {
922 QDEBUG("no config command, and wrong pid\n");
Harald Welte838ab632005-08-09 19:50:45 -0700923 ret = -EPERM;
924 goto out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700925 }
926 }
927
Patrick McHardydf6fb862007-09-28 14:37:03 -0700928 if (nfqa[NFQA_CFG_PARAMS]) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700929 struct nfqnl_msg_config_params *params;
Harald Welte7af4cc32005-08-09 19:44:15 -0700930
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800931 if (!queue) {
932 ret = -ENOENT;
933 goto out_put;
934 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700935 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700936 nfqnl_set_mode(queue, params->copy_mode,
937 ntohl(params->copy_range));
938 }
939
Patrick McHardydf6fb862007-09-28 14:37:03 -0700940 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
Eric Leblond829e17a2006-11-29 02:35:33 +0100941 __be32 *queue_maxlen;
Patrick McHardydf6fb862007-09-28 14:37:03 -0700942 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
Eric Leblond829e17a2006-11-29 02:35:33 +0100943 spin_lock_bh(&queue->lock);
944 queue->queue_maxlen = ntohl(*queue_maxlen);
945 spin_unlock_bh(&queue->lock);
946 }
947
Harald Welte838ab632005-08-09 19:50:45 -0700948out_put:
949 instance_put(queue);
950 return ret;
Harald Welte7af4cc32005-08-09 19:44:15 -0700951}
952
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700953static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
Harald Welte7af4cc32005-08-09 19:44:15 -0700954 [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp,
Harald Welte37d2e7a2005-11-14 15:24:59 -0800955 .attr_count = NFQA_MAX, },
Harald Welte7af4cc32005-08-09 19:44:15 -0700956 [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700957 .attr_count = NFQA_MAX,
958 .policy = nfqa_verdict_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700959 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700960 .attr_count = NFQA_CFG_MAX,
961 .policy = nfqa_cfg_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700962};
963
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700964static const struct nfnetlink_subsystem nfqnl_subsys = {
Harald Welte7af4cc32005-08-09 19:44:15 -0700965 .name = "nf_queue",
966 .subsys_id = NFNL_SUBSYS_QUEUE,
967 .cb_count = NFQNL_MSG_MAX,
Harald Welte7af4cc32005-08-09 19:44:15 -0700968 .cb = nfqnl_cb,
969};
970
Harald Welte838ab632005-08-09 19:50:45 -0700971#ifdef CONFIG_PROC_FS
972struct iter_state {
973 unsigned int bucket;
974};
975
976static struct hlist_node *get_first(struct seq_file *seq)
977{
978 struct iter_state *st = seq->private;
979
980 if (!st)
981 return NULL;
982
983 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
984 if (!hlist_empty(&instance_table[st->bucket]))
985 return instance_table[st->bucket].first;
986 }
987 return NULL;
988}
989
990static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
991{
992 struct iter_state *st = seq->private;
993
994 h = h->next;
995 while (!h) {
996 if (++st->bucket >= INSTANCE_BUCKETS)
997 return NULL;
998
999 h = instance_table[st->bucket].first;
1000 }
1001 return h;
1002}
1003
1004static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
1005{
1006 struct hlist_node *head;
1007 head = get_first(seq);
1008
1009 if (head)
1010 while (pos && (head = get_next(seq, head)))
1011 pos--;
1012 return pos ? NULL : head;
1013}
1014
1015static void *seq_start(struct seq_file *seq, loff_t *pos)
1016{
1017 read_lock_bh(&instances_lock);
1018 return get_idx(seq, *pos);
1019}
1020
1021static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1022{
1023 (*pos)++;
1024 return get_next(s, v);
1025}
1026
1027static void seq_stop(struct seq_file *s, void *v)
1028{
1029 read_unlock_bh(&instances_lock);
1030}
1031
1032static int seq_show(struct seq_file *s, void *v)
1033{
1034 const struct nfqnl_instance *inst = v;
1035
1036 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
1037 inst->queue_num,
1038 inst->peer_pid, inst->queue_total,
1039 inst->copy_mode, inst->copy_range,
1040 inst->queue_dropped, inst->queue_user_dropped,
1041 atomic_read(&inst->id_sequence),
1042 atomic_read(&inst->use));
1043}
1044
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001045static const struct seq_operations nfqnl_seq_ops = {
Harald Welte838ab632005-08-09 19:50:45 -07001046 .start = seq_start,
1047 .next = seq_next,
1048 .stop = seq_stop,
1049 .show = seq_show,
1050};
1051
1052static int nfqnl_open(struct inode *inode, struct file *file)
1053{
Pavel Emelyanove2da5912007-10-10 02:29:58 -07001054 return seq_open_private(file, &nfqnl_seq_ops,
1055 sizeof(struct iter_state));
Harald Welte838ab632005-08-09 19:50:45 -07001056}
1057
Arjan van de Venda7071d2007-02-12 00:55:36 -08001058static const struct file_operations nfqnl_file_ops = {
Harald Welte838ab632005-08-09 19:50:45 -07001059 .owner = THIS_MODULE,
1060 .open = nfqnl_open,
1061 .read = seq_read,
1062 .llseek = seq_lseek,
1063 .release = seq_release_private,
1064};
1065
1066#endif /* PROC_FS */
1067
Patrick McHardy32292a72006-04-06 14:11:30 -07001068static int __init nfnetlink_queue_init(void)
Harald Welte7af4cc32005-08-09 19:44:15 -07001069{
Harald Welte838ab632005-08-09 19:50:45 -07001070 int i, status = -ENOMEM;
1071#ifdef CONFIG_PROC_FS
1072 struct proc_dir_entry *proc_nfqueue;
1073#endif
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001074
Harald Welte838ab632005-08-09 19:50:45 -07001075 for (i = 0; i < INSTANCE_BUCKETS; i++)
1076 INIT_HLIST_HEAD(&instance_table[i]);
1077
Harald Welte7af4cc32005-08-09 19:44:15 -07001078 netlink_register_notifier(&nfqnl_rtnl_notifier);
1079 status = nfnetlink_subsys_register(&nfqnl_subsys);
1080 if (status < 0) {
1081 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
1082 goto cleanup_netlink_notifier;
1083 }
1084
Harald Welte838ab632005-08-09 19:50:45 -07001085#ifdef CONFIG_PROC_FS
1086 proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440,
1087 proc_net_netfilter);
1088 if (!proc_nfqueue)
1089 goto cleanup_subsys;
1090 proc_nfqueue->proc_fops = &nfqnl_file_ops;
1091#endif
1092
Harald Welte7af4cc32005-08-09 19:44:15 -07001093 register_netdevice_notifier(&nfqnl_dev_notifier);
1094 return status;
1095
Harald Welte838ab632005-08-09 19:50:45 -07001096#ifdef CONFIG_PROC_FS
1097cleanup_subsys:
Harald Welte7af4cc32005-08-09 19:44:15 -07001098 nfnetlink_subsys_unregister(&nfqnl_subsys);
Patrick McHardy32292a72006-04-06 14:11:30 -07001099#endif
Harald Welte7af4cc32005-08-09 19:44:15 -07001100cleanup_netlink_notifier:
1101 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1102 return status;
1103}
1104
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001105static void __exit nfnetlink_queue_fini(void)
Harald Welte7af4cc32005-08-09 19:44:15 -07001106{
Patrick McHardy32292a72006-04-06 14:11:30 -07001107 nf_unregister_queue_handlers(&nfqh);
1108 unregister_netdevice_notifier(&nfqnl_dev_notifier);
1109#ifdef CONFIG_PROC_FS
1110 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
1111#endif
1112 nfnetlink_subsys_unregister(&nfqnl_subsys);
1113 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
Harald Welte7af4cc32005-08-09 19:44:15 -07001114}
1115
1116MODULE_DESCRIPTION("netfilter packet queue handler");
1117MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1118MODULE_LICENSE("GPL");
1119MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1120
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001121module_init(nfnetlink_queue_init);
1122module_exit(nfnetlink_queue_fini);