blob: d9ce3942af2a8255f62f4ed65bfa54eecd2619dd [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
Harald Welte7af4cc32005-08-09 19:44:15 -070048struct nfqnl_instance {
49 struct hlist_node hlist; /* global list of queues */
Harald Welte838ab632005-08-09 19:50:45 -070050 atomic_t use;
Harald Welte7af4cc32005-08-09 19:44:15 -070051
52 int peer_pid;
53 unsigned int queue_maxlen;
54 unsigned int copy_range;
55 unsigned int queue_total;
56 unsigned int queue_dropped;
57 unsigned int queue_user_dropped;
58
59 atomic_t id_sequence; /* 'sequence' of pkt ids */
60
61 u_int16_t queue_num; /* number of this queue */
62 u_int8_t copy_mode;
63
64 spinlock_t lock;
65
66 struct list_head queue_list; /* packets in queue */
67};
68
Patrick McHardy02f014d2007-12-05 01:26:33 -080069typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
Harald Welte7af4cc32005-08-09 19:44:15 -070070
71static DEFINE_RWLOCK(instances_lock);
72
Harald Welte7af4cc32005-08-09 19:44:15 -070073#define INSTANCE_BUCKETS 16
74static struct hlist_head instance_table[INSTANCE_BUCKETS];
75
76static inline u_int8_t instance_hashfn(u_int16_t queue_num)
77{
78 return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
79}
80
81static struct nfqnl_instance *
82__instance_lookup(u_int16_t queue_num)
83{
84 struct hlist_head *head;
85 struct hlist_node *pos;
86 struct nfqnl_instance *inst;
87
88 head = &instance_table[instance_hashfn(queue_num)];
89 hlist_for_each_entry(inst, pos, head, hlist) {
90 if (inst->queue_num == queue_num)
91 return inst;
92 }
93 return NULL;
94}
95
96static struct nfqnl_instance *
Harald Welte838ab632005-08-09 19:50:45 -070097instance_lookup_get(u_int16_t queue_num)
Harald Welte7af4cc32005-08-09 19:44:15 -070098{
99 struct nfqnl_instance *inst;
100
101 read_lock_bh(&instances_lock);
102 inst = __instance_lookup(queue_num);
Harald Welte838ab632005-08-09 19:50:45 -0700103 if (inst)
104 atomic_inc(&inst->use);
Harald Welte7af4cc32005-08-09 19:44:15 -0700105 read_unlock_bh(&instances_lock);
106
107 return inst;
108}
109
Harald Welte838ab632005-08-09 19:50:45 -0700110static void
111instance_put(struct nfqnl_instance *inst)
112{
113 if (inst && atomic_dec_and_test(&inst->use)) {
114 QDEBUG("kfree(inst=%p)\n", inst);
115 kfree(inst);
116 }
117}
118
Harald Welte7af4cc32005-08-09 19:44:15 -0700119static struct nfqnl_instance *
120instance_create(u_int16_t queue_num, int pid)
121{
122 struct nfqnl_instance *inst;
123
124 QDEBUG("entering for queue_num=%u, pid=%d\n", queue_num, pid);
125
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800126 write_lock_bh(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700127 if (__instance_lookup(queue_num)) {
128 inst = NULL;
129 QDEBUG("aborting, instance already exists\n");
130 goto out_unlock;
131 }
132
Harald Welte10dfdc62005-11-03 19:20:07 +0100133 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
Harald Welte7af4cc32005-08-09 19:44:15 -0700134 if (!inst)
135 goto out_unlock;
136
Harald Welte7af4cc32005-08-09 19:44:15 -0700137 inst->queue_num = queue_num;
138 inst->peer_pid = pid;
139 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
140 inst->copy_range = 0xfffff;
141 inst->copy_mode = NFQNL_COPY_NONE;
142 atomic_set(&inst->id_sequence, 0);
Harald Welte838ab632005-08-09 19:50:45 -0700143 /* needs to be two, since we _put() after creation */
144 atomic_set(&inst->use, 2);
YOSHIFUJI Hideaki181a46a2006-01-04 13:56:54 -0800145 spin_lock_init(&inst->lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700146 INIT_LIST_HEAD(&inst->queue_list);
147
148 if (!try_module_get(THIS_MODULE))
149 goto out_free;
150
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800151 hlist_add_head(&inst->hlist,
Harald Welte7af4cc32005-08-09 19:44:15 -0700152 &instance_table[instance_hashfn(queue_num)]);
153
154 write_unlock_bh(&instances_lock);
155
156 QDEBUG("successfully created new instance\n");
157
158 return inst;
159
160out_free:
161 kfree(inst);
162out_unlock:
163 write_unlock_bh(&instances_lock);
164 return NULL;
165}
166
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800167static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
168 unsigned long data);
Harald Welte7af4cc32005-08-09 19:44:15 -0700169
170static void
171_instance_destroy2(struct nfqnl_instance *inst, int lock)
172{
173 /* first pull it out of the global list */
174 if (lock)
175 write_lock_bh(&instances_lock);
176
177 QDEBUG("removing instance %p (queuenum=%u) from hash\n",
178 inst, inst->queue_num);
179 hlist_del(&inst->hlist);
180
181 if (lock)
182 write_unlock_bh(&instances_lock);
183
184 /* then flush all pending skbs from the queue */
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800185 nfqnl_flush(inst, NULL, 0);
Harald Welte7af4cc32005-08-09 19:44:15 -0700186
Harald Welte838ab632005-08-09 19:50:45 -0700187 /* and finally put the refcount */
188 instance_put(inst);
Harald Welte7af4cc32005-08-09 19:44:15 -0700189
190 module_put(THIS_MODULE);
191}
192
193static inline void
194__instance_destroy(struct nfqnl_instance *inst)
195{
196 _instance_destroy2(inst, 0);
197}
198
199static inline void
200instance_destroy(struct nfqnl_instance *inst)
201{
202 _instance_destroy2(inst, 1);
203}
204
Harald Welte7af4cc32005-08-09 19:44:15 -0700205static inline void
Patrick McHardy02f014d2007-12-05 01:26:33 -0800206__enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
Harald Welte7af4cc32005-08-09 19:44:15 -0700207{
Patrick McHardy0ac41e82007-12-05 01:25:03 -0800208 list_add_tail(&entry->list, &queue->queue_list);
Harald Welte7af4cc32005-08-09 19:44:15 -0700209 queue->queue_total++;
210}
211
Harald Welte7af4cc32005-08-09 19:44:15 -0700212static inline int
213__nfqnl_set_mode(struct nfqnl_instance *queue,
214 unsigned char mode, unsigned int range)
215{
216 int status = 0;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800217
Harald Welte7af4cc32005-08-09 19:44:15 -0700218 switch (mode) {
219 case NFQNL_COPY_NONE:
220 case NFQNL_COPY_META:
221 queue->copy_mode = mode;
222 queue->copy_range = 0;
223 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800224
Harald Welte7af4cc32005-08-09 19:44:15 -0700225 case NFQNL_COPY_PACKET:
226 queue->copy_mode = mode;
Patrick McHardydf6fb862007-09-28 14:37:03 -0700227 /* we're using struct nlattr which has 16bit nla_len */
Harald Welte7af4cc32005-08-09 19:44:15 -0700228 if (range > 0xffff)
229 queue->copy_range = 0xffff;
230 else
231 queue->copy_range = range;
232 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800233
Harald Welte7af4cc32005-08-09 19:44:15 -0700234 default:
235 status = -EINVAL;
236
237 }
238 return status;
239}
240
Patrick McHardy02f014d2007-12-05 01:26:33 -0800241static struct nf_queue_entry *
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800242find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
Harald Welte7af4cc32005-08-09 19:44:15 -0700243{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800244 struct nf_queue_entry *entry = NULL, *i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800245
Harald Welte7af4cc32005-08-09 19:44:15 -0700246 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800247
248 list_for_each_entry(i, &queue->queue_list, list) {
249 if (i->id == id) {
250 entry = i;
251 break;
252 }
253 }
254
255 if (entry) {
256 list_del(&entry->list);
257 queue->queue_total--;
258 }
259
Harald Welte7af4cc32005-08-09 19:44:15 -0700260 spin_unlock_bh(&queue->lock);
261
262 return entry;
263}
264
265static void
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800266nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
Harald Welte7af4cc32005-08-09 19:44:15 -0700267{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800268 struct nf_queue_entry *entry, *next;
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800269
Harald Welte7af4cc32005-08-09 19:44:15 -0700270 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800271 list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
272 if (!cmpfn || cmpfn(entry, data)) {
273 list_del(&entry->list);
274 queue->queue_total--;
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800275 nf_reinject(entry, NF_DROP);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800276 }
277 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700278 spin_unlock_bh(&queue->lock);
279}
280
281static struct sk_buff *
282nfqnl_build_packet_message(struct nfqnl_instance *queue,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800283 struct nf_queue_entry *entry, int *errp)
Harald Welte7af4cc32005-08-09 19:44:15 -0700284{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700285 sk_buff_data_t old_tail;
Harald Welte7af4cc32005-08-09 19:44:15 -0700286 size_t size;
287 size_t data_len = 0;
288 struct sk_buff *skb;
289 struct nfqnl_msg_packet_hdr pmsg;
290 struct nlmsghdr *nlh;
291 struct nfgenmsg *nfmsg;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800292 struct sk_buff *entskb = entry->skb;
293 struct net_device *indev;
294 struct net_device *outdev;
Al Viro98a4a862006-11-08 00:26:51 -0800295 __be32 tmp_uint;
Harald Welte7af4cc32005-08-09 19:44:15 -0700296
297 QDEBUG("entered\n");
298
Patrick McHardydf6fb862007-09-28 14:37:03 -0700299 size = NLMSG_ALIGN(sizeof(struct nfgenmsg))
300 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
301 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
302 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700303#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700304 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
305 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700306#endif
Patrick McHardydf6fb862007-09-28 14:37:03 -0700307 + nla_total_size(sizeof(u_int32_t)) /* mark */
308 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
309 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
Harald Welte7af4cc32005-08-09 19:44:15 -0700310
Patrick McHardy02f014d2007-12-05 01:26:33 -0800311 outdev = entry->outdev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800312
Harald Welte7af4cc32005-08-09 19:44:15 -0700313 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800314
Harald Welte7af4cc32005-08-09 19:44:15 -0700315 switch (queue->copy_mode) {
316 case NFQNL_COPY_META:
317 case NFQNL_COPY_NONE:
318 data_len = 0;
319 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800320
Harald Welte7af4cc32005-08-09 19:44:15 -0700321 case NFQNL_COPY_PACKET:
Patrick McHardy84fa7932006-08-29 16:44:56 -0700322 if ((entskb->ip_summed == CHECKSUM_PARTIAL ||
323 entskb->ip_summed == CHECKSUM_COMPLETE) &&
324 (*errp = skb_checksum_help(entskb))) {
Patrick McHardye7dfb092005-09-06 15:10:00 -0700325 spin_unlock_bh(&queue->lock);
326 return NULL;
327 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800328 if (queue->copy_range == 0
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800329 || queue->copy_range > entskb->len)
330 data_len = entskb->len;
Harald Welte7af4cc32005-08-09 19:44:15 -0700331 else
332 data_len = queue->copy_range;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800333
Patrick McHardydf6fb862007-09-28 14:37:03 -0700334 size += nla_total_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700335 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800336
Harald Welte7af4cc32005-08-09 19:44:15 -0700337 default:
338 *errp = -EINVAL;
339 spin_unlock_bh(&queue->lock);
340 return NULL;
341 }
342
343 spin_unlock_bh(&queue->lock);
344
345 skb = alloc_skb(size, GFP_ATOMIC);
346 if (!skb)
347 goto nlmsg_failure;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800348
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700349 old_tail = skb->tail;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800350 nlh = NLMSG_PUT(skb, 0, 0,
Harald Welte7af4cc32005-08-09 19:44:15 -0700351 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
352 sizeof(struct nfgenmsg));
353 nfmsg = NLMSG_DATA(nlh);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800354 nfmsg->nfgen_family = entry->pf;
Harald Welte7af4cc32005-08-09 19:44:15 -0700355 nfmsg->version = NFNETLINK_V0;
356 nfmsg->res_id = htons(queue->queue_num);
357
358 pmsg.packet_id = htonl(entry->id);
Al Virofebf0a42006-11-03 00:59:17 -0800359 pmsg.hw_protocol = entskb->protocol;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800360 pmsg.hook = entry->hook;
Harald Welte7af4cc32005-08-09 19:44:15 -0700361
Patrick McHardydf6fb862007-09-28 14:37:03 -0700362 NLA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
Harald Welte7af4cc32005-08-09 19:44:15 -0700363
Patrick McHardy02f014d2007-12-05 01:26:33 -0800364 indev = entry->indev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800365 if (indev) {
366 tmp_uint = htonl(indev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700367#ifndef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700368 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint);
Harald Weltefbcd9232005-08-09 20:22:10 -0700369#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800370 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700371 /* Case 1: indev is physical input device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800372 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700373 * netfilter_bridge) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700374 NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700375 &tmp_uint);
376 /* this is the bridge group "brX" */
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800377 tmp_uint = htonl(indev->br_port->br->dev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700378 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700379 &tmp_uint);
380 } else {
381 /* Case 2: indev is bridge group, we need to look for
382 * physical device (when called from ipv4) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700383 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700384 &tmp_uint);
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800385 if (entskb->nf_bridge
386 && entskb->nf_bridge->physindev) {
387 tmp_uint = htonl(entskb->nf_bridge->physindev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700388 NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV,
Harald Weltefbcd9232005-08-09 20:22:10 -0700389 sizeof(tmp_uint), &tmp_uint);
390 }
391 }
392#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700393 }
394
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800395 if (outdev) {
396 tmp_uint = htonl(outdev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700397#ifndef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700398 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint);
Harald Weltefbcd9232005-08-09 20:22:10 -0700399#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800400 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700401 /* Case 1: outdev is physical output device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800402 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700403 * netfilter_bridge) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700404 NLA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700405 &tmp_uint);
406 /* this is the bridge group "brX" */
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800407 tmp_uint = htonl(outdev->br_port->br->dev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700408 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700409 &tmp_uint);
410 } else {
411 /* Case 2: outdev is bridge group, we need to look for
412 * physical output device (when called from ipv4) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700413 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700414 &tmp_uint);
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800415 if (entskb->nf_bridge
416 && entskb->nf_bridge->physoutdev) {
417 tmp_uint = htonl(entskb->nf_bridge->physoutdev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700418 NLA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV,
Harald Weltefbcd9232005-08-09 20:22:10 -0700419 sizeof(tmp_uint), &tmp_uint);
420 }
421 }
422#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700423 }
424
Thomas Graf82e91ff2006-11-09 15:19:14 -0800425 if (entskb->mark) {
426 tmp_uint = htonl(entskb->mark);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700427 NLA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint);
Harald Welte7af4cc32005-08-09 19:44:15 -0700428 }
429
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700430 if (indev && entskb->dev) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700431 struct nfqnl_msg_packet_hw phw;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700432 int len = dev_parse_header(entskb, phw.hw_addr);
433 if (len) {
434 phw.hw_addrlen = htons(len);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700435 NLA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700436 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700437 }
438
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700439 if (entskb->tstamp.tv64) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700440 struct nfqnl_msg_packet_timestamp ts;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700441 struct timeval tv = ktime_to_timeval(entskb->tstamp);
442 ts.sec = cpu_to_be64(tv.tv_sec);
443 ts.usec = cpu_to_be64(tv.tv_usec);
Harald Welte7af4cc32005-08-09 19:44:15 -0700444
Patrick McHardydf6fb862007-09-28 14:37:03 -0700445 NLA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
Harald Welte7af4cc32005-08-09 19:44:15 -0700446 }
447
448 if (data_len) {
Patrick McHardydf6fb862007-09-28 14:37:03 -0700449 struct nlattr *nla;
450 int size = nla_attr_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700451
Patrick McHardydf6fb862007-09-28 14:37:03 -0700452 if (skb_tailroom(skb) < nla_total_size(data_len)) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700453 printk(KERN_WARNING "nf_queue: no tailroom!\n");
454 goto nlmsg_failure;
455 }
456
Patrick McHardydf6fb862007-09-28 14:37:03 -0700457 nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));
458 nla->nla_type = NFQA_PAYLOAD;
459 nla->nla_len = size;
Harald Welte7af4cc32005-08-09 19:44:15 -0700460
Patrick McHardydf6fb862007-09-28 14:37:03 -0700461 if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700462 BUG();
463 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800464
Harald Welte7af4cc32005-08-09 19:44:15 -0700465 nlh->nlmsg_len = skb->tail - old_tail;
466 return skb;
467
468nlmsg_failure:
Patrick McHardydf6fb862007-09-28 14:37:03 -0700469nla_put_failure:
Harald Welte7af4cc32005-08-09 19:44:15 -0700470 if (skb)
471 kfree_skb(skb);
472 *errp = -EINVAL;
473 if (net_ratelimit())
474 printk(KERN_ERR "nf_queue: error creating packet message\n");
475 return NULL;
476}
477
478static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800479nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
Harald Welte7af4cc32005-08-09 19:44:15 -0700480{
481 int status = -EINVAL;
482 struct sk_buff *nskb;
483 struct nfqnl_instance *queue;
Harald Welte7af4cc32005-08-09 19:44:15 -0700484
485 QDEBUG("entered\n");
486
Harald Welte838ab632005-08-09 19:50:45 -0700487 queue = instance_lookup_get(queuenum);
Harald Welte7af4cc32005-08-09 19:44:15 -0700488 if (!queue) {
489 QDEBUG("no queue instance matching\n");
490 return -EINVAL;
491 }
492
493 if (queue->copy_mode == NFQNL_COPY_NONE) {
494 QDEBUG("mode COPY_NONE, aborting\n");
Harald Welte838ab632005-08-09 19:50:45 -0700495 status = -EAGAIN;
496 goto err_out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700497 }
498
Harald Welte7af4cc32005-08-09 19:44:15 -0700499 entry->id = atomic_inc_return(&queue->id_sequence);
500
501 nskb = nfqnl_build_packet_message(queue, entry, &status);
502 if (nskb == NULL)
Patrick McHardy02f014d2007-12-05 01:26:33 -0800503 goto err_out_put;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800504
Harald Welte7af4cc32005-08-09 19:44:15 -0700505 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800506
Harald Welte7af4cc32005-08-09 19:44:15 -0700507 if (!queue->peer_pid)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800508 goto err_out_free_nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700509
510 if (queue->queue_total >= queue->queue_maxlen) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800511 queue->queue_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700512 status = -ENOSPC;
513 if (net_ratelimit())
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800514 printk(KERN_WARNING "nf_queue: full at %d entries, "
515 "dropping packets(s). Dropped: %d\n",
Harald Welte7af4cc32005-08-09 19:44:15 -0700516 queue->queue_total, queue->queue_dropped);
517 goto err_out_free_nskb;
518 }
519
520 /* nfnetlink_unicast will either free the nskb or add it to a socket */
521 status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
522 if (status < 0) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800523 queue->queue_user_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700524 goto err_out_unlock;
525 }
526
527 __enqueue_entry(queue, entry);
528
529 spin_unlock_bh(&queue->lock);
Harald Welte838ab632005-08-09 19:50:45 -0700530 instance_put(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700531 return status;
532
533err_out_free_nskb:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800534 kfree_skb(nskb);
535
Harald Welte7af4cc32005-08-09 19:44:15 -0700536err_out_unlock:
537 spin_unlock_bh(&queue->lock);
538
Harald Welte838ab632005-08-09 19:50:45 -0700539err_out_put:
540 instance_put(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700541 return status;
542}
543
544static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800545nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e)
Harald Welte7af4cc32005-08-09 19:44:15 -0700546{
547 int diff;
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700548 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700549
550 diff = data_len - e->skb->len;
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800551 if (diff < 0) {
552 if (pskb_trim(e->skb, data_len))
553 return -ENOMEM;
554 } else if (diff > 0) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700555 if (data_len > 0xFFFF)
556 return -EINVAL;
557 if (diff > skb_tailroom(e->skb)) {
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700558 err = pskb_expand_head(e->skb, 0,
559 diff - skb_tailroom(e->skb),
560 GFP_ATOMIC);
561 if (err) {
Patrick McHardy1158ba22006-08-22 00:32:47 -0700562 printk(KERN_WARNING "nf_queue: OOM "
Harald Welte7af4cc32005-08-09 19:44:15 -0700563 "in mangle, dropping packet\n");
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700564 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700565 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700566 }
567 skb_put(e->skb, diff);
568 }
Herbert Xu37d41872007-10-14 00:39:18 -0700569 if (!skb_make_writable(e->skb, data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700570 return -ENOMEM;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300571 skb_copy_to_linear_data(e->skb, data, data_len);
Patrick McHardye7dfb092005-09-06 15:10:00 -0700572 e->skb->ip_summed = CHECKSUM_NONE;
Harald Welte7af4cc32005-08-09 19:44:15 -0700573 return 0;
574}
575
Harald Welte7af4cc32005-08-09 19:44:15 -0700576static int
577nfqnl_set_mode(struct nfqnl_instance *queue,
578 unsigned char mode, unsigned int range)
579{
580 int status;
581
582 spin_lock_bh(&queue->lock);
583 status = __nfqnl_set_mode(queue, mode, range);
584 spin_unlock_bh(&queue->lock);
585
586 return status;
587}
588
589static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800590dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700591{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800592 if (entry->indev)
593 if (entry->indev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700594 return 1;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800595 if (entry->outdev)
596 if (entry->outdev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700597 return 1;
Patrick McHardyef47c6a2006-06-27 03:01:48 -0700598#ifdef CONFIG_BRIDGE_NETFILTER
599 if (entry->skb->nf_bridge) {
600 if (entry->skb->nf_bridge->physindev &&
601 entry->skb->nf_bridge->physindev->ifindex == ifindex)
602 return 1;
603 if (entry->skb->nf_bridge->physoutdev &&
604 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
605 return 1;
606 }
607#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700608 return 0;
609}
610
611/* drop all packets with either indev or outdev == ifindex from all queue
612 * instances */
613static void
614nfqnl_dev_drop(int ifindex)
615{
616 int i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800617
Harald Welte7af4cc32005-08-09 19:44:15 -0700618 QDEBUG("entering for ifindex %u\n", ifindex);
619
620 /* this only looks like we have to hold the readlock for a way too long
621 * time, issue_verdict(), nf_reinject(), ... - but we always only
622 * issue NF_DROP, which is processed directly in nf_reinject() */
623 read_lock_bh(&instances_lock);
624
625 for (i = 0; i < INSTANCE_BUCKETS; i++) {
626 struct hlist_node *tmp;
627 struct nfqnl_instance *inst;
628 struct hlist_head *head = &instance_table[i];
629
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800630 hlist_for_each_entry(inst, tmp, head, hlist)
631 nfqnl_flush(inst, dev_cmp, ifindex);
Harald Welte7af4cc32005-08-09 19:44:15 -0700632 }
633
634 read_unlock_bh(&instances_lock);
635}
636
637#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
638
639static int
640nfqnl_rcv_dev_event(struct notifier_block *this,
641 unsigned long event, void *ptr)
642{
643 struct net_device *dev = ptr;
644
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200645 if (dev->nd_net != &init_net)
646 return NOTIFY_DONE;
647
Harald Welte7af4cc32005-08-09 19:44:15 -0700648 /* Drop any packets associated with the downed device */
649 if (event == NETDEV_DOWN)
650 nfqnl_dev_drop(dev->ifindex);
651 return NOTIFY_DONE;
652}
653
654static struct notifier_block nfqnl_dev_notifier = {
655 .notifier_call = nfqnl_rcv_dev_event,
656};
657
658static int
659nfqnl_rcv_nl_event(struct notifier_block *this,
660 unsigned long event, void *ptr)
661{
662 struct netlink_notify *n = ptr;
663
664 if (event == NETLINK_URELEASE &&
665 n->protocol == NETLINK_NETFILTER && n->pid) {
666 int i;
667
668 /* destroy all instances for this pid */
669 write_lock_bh(&instances_lock);
670 for (i = 0; i < INSTANCE_BUCKETS; i++) {
671 struct hlist_node *tmp, *t2;
672 struct nfqnl_instance *inst;
673 struct hlist_head *head = &instance_table[i];
674
675 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200676 if ((n->net == &init_net) &&
677 (n->pid == inst->peer_pid))
Harald Welte7af4cc32005-08-09 19:44:15 -0700678 __instance_destroy(inst);
679 }
680 }
681 write_unlock_bh(&instances_lock);
682 }
683 return NOTIFY_DONE;
684}
685
686static struct notifier_block nfqnl_rtnl_notifier = {
687 .notifier_call = nfqnl_rcv_nl_event,
688};
689
Patrick McHardy5bf75852007-09-28 14:39:26 -0700690static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
691 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
692 [NFQA_MARK] = { .type = NLA_U32 },
693 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
Harald Welte838ab632005-08-09 19:50:45 -0700694};
695
Harald Welte7af4cc32005-08-09 19:44:15 -0700696static int
697nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700698 struct nlmsghdr *nlh, struct nlattr *nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700699{
700 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
701 u_int16_t queue_num = ntohs(nfmsg->res_id);
702
703 struct nfqnl_msg_verdict_hdr *vhdr;
704 struct nfqnl_instance *queue;
705 unsigned int verdict;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800706 struct nf_queue_entry *entry;
Harald Welte838ab632005-08-09 19:50:45 -0700707 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700708
Harald Welte838ab632005-08-09 19:50:45 -0700709 queue = instance_lookup_get(queue_num);
Harald Welte7af4cc32005-08-09 19:44:15 -0700710 if (!queue)
711 return -ENODEV;
712
Harald Welte838ab632005-08-09 19:50:45 -0700713 if (queue->peer_pid != NETLINK_CB(skb).pid) {
714 err = -EPERM;
715 goto err_out_put;
716 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700717
Patrick McHardydf6fb862007-09-28 14:37:03 -0700718 if (!nfqa[NFQA_VERDICT_HDR]) {
Harald Welte838ab632005-08-09 19:50:45 -0700719 err = -EINVAL;
720 goto err_out_put;
721 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700722
Patrick McHardydf6fb862007-09-28 14:37:03 -0700723 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700724 verdict = ntohl(vhdr->verdict);
725
Harald Welte838ab632005-08-09 19:50:45 -0700726 if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
727 err = -EINVAL;
728 goto err_out_put;
729 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700730
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800731 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
Harald Welte838ab632005-08-09 19:50:45 -0700732 if (entry == NULL) {
733 err = -ENOENT;
734 goto err_out_put;
735 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700736
Patrick McHardydf6fb862007-09-28 14:37:03 -0700737 if (nfqa[NFQA_PAYLOAD]) {
738 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
739 nla_len(nfqa[NFQA_PAYLOAD]), entry) < 0)
Harald Welte7af4cc32005-08-09 19:44:15 -0700740 verdict = NF_DROP;
741 }
742
Patrick McHardydf6fb862007-09-28 14:37:03 -0700743 if (nfqa[NFQA_MARK])
Thomas Graf82e91ff2006-11-09 15:19:14 -0800744 entry->skb->mark = ntohl(*(__be32 *)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700745 nla_data(nfqa[NFQA_MARK]));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800746
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800747 nf_reinject(entry, verdict);
Harald Welte838ab632005-08-09 19:50:45 -0700748 instance_put(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700749 return 0;
Harald Welte838ab632005-08-09 19:50:45 -0700750
751err_out_put:
752 instance_put(queue);
753 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700754}
755
756static int
757nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700758 struct nlmsghdr *nlh, struct nlattr *nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700759{
760 return -ENOTSUPP;
761}
762
Patrick McHardy5bf75852007-09-28 14:39:26 -0700763static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
764 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
765 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
Harald Welte838ab632005-08-09 19:50:45 -0700766};
767
Patrick McHardye3ac5292007-12-05 01:23:57 -0800768static const struct nf_queue_handler nfqh = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700769 .name = "nf_queue",
770 .outfn = &nfqnl_enqueue_packet,
771};
772
Harald Welte7af4cc32005-08-09 19:44:15 -0700773static int
774nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700775 struct nlmsghdr *nlh, struct nlattr *nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700776{
777 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
778 u_int16_t queue_num = ntohs(nfmsg->res_id);
779 struct nfqnl_instance *queue;
Harald Welte838ab632005-08-09 19:50:45 -0700780 int ret = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700781
782 QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type));
783
Harald Welte838ab632005-08-09 19:50:45 -0700784 queue = instance_lookup_get(queue_num);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700785 if (nfqa[NFQA_CFG_CMD]) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700786 struct nfqnl_msg_config_cmd *cmd;
Patrick McHardydf6fb862007-09-28 14:37:03 -0700787 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700788 QDEBUG("found CFG_CMD\n");
789
790 switch (cmd->command) {
791 case NFQNL_CFG_CMD_BIND:
792 if (queue)
793 return -EBUSY;
794
795 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
796 if (!queue)
797 return -EINVAL;
798 break;
799 case NFQNL_CFG_CMD_UNBIND:
800 if (!queue)
801 return -ENODEV;
802
Harald Welte838ab632005-08-09 19:50:45 -0700803 if (queue->peer_pid != NETLINK_CB(skb).pid) {
804 ret = -EPERM;
805 goto out_put;
806 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700807
808 instance_destroy(queue);
809 break;
810 case NFQNL_CFG_CMD_PF_BIND:
811 QDEBUG("registering queue handler for pf=%u\n",
812 ntohs(cmd->pf));
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700813 ret = nf_register_queue_handler(ntohs(cmd->pf), &nfqh);
Harald Welte7af4cc32005-08-09 19:44:15 -0700814 break;
815 case NFQNL_CFG_CMD_PF_UNBIND:
816 QDEBUG("unregistering queue handler for pf=%u\n",
817 ntohs(cmd->pf));
Yasuyuki Kozakaice7663d2007-07-07 22:40:08 -0700818 ret = nf_unregister_queue_handler(ntohs(cmd->pf), &nfqh);
Harald Welte7af4cc32005-08-09 19:44:15 -0700819 break;
820 default:
Harald Welte838ab632005-08-09 19:50:45 -0700821 ret = -EINVAL;
822 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700823 }
824 } else {
825 if (!queue) {
826 QDEBUG("no config command, and no instance ENOENT\n");
Harald Welte838ab632005-08-09 19:50:45 -0700827 ret = -ENOENT;
828 goto out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700829 }
830
831 if (queue->peer_pid != NETLINK_CB(skb).pid) {
832 QDEBUG("no config command, and wrong pid\n");
Harald Welte838ab632005-08-09 19:50:45 -0700833 ret = -EPERM;
834 goto out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700835 }
836 }
837
Patrick McHardydf6fb862007-09-28 14:37:03 -0700838 if (nfqa[NFQA_CFG_PARAMS]) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700839 struct nfqnl_msg_config_params *params;
Harald Welte7af4cc32005-08-09 19:44:15 -0700840
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800841 if (!queue) {
842 ret = -ENOENT;
843 goto out_put;
844 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700845 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700846 nfqnl_set_mode(queue, params->copy_mode,
847 ntohl(params->copy_range));
848 }
849
Patrick McHardydf6fb862007-09-28 14:37:03 -0700850 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
Eric Leblond829e17a2006-11-29 02:35:33 +0100851 __be32 *queue_maxlen;
Patrick McHardydf6fb862007-09-28 14:37:03 -0700852 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
Eric Leblond829e17a2006-11-29 02:35:33 +0100853 spin_lock_bh(&queue->lock);
854 queue->queue_maxlen = ntohl(*queue_maxlen);
855 spin_unlock_bh(&queue->lock);
856 }
857
Harald Welte838ab632005-08-09 19:50:45 -0700858out_put:
859 instance_put(queue);
860 return ret;
Harald Welte7af4cc32005-08-09 19:44:15 -0700861}
862
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700863static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
Harald Welte7af4cc32005-08-09 19:44:15 -0700864 [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp,
Harald Welte37d2e7a2005-11-14 15:24:59 -0800865 .attr_count = NFQA_MAX, },
Harald Welte7af4cc32005-08-09 19:44:15 -0700866 [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700867 .attr_count = NFQA_MAX,
868 .policy = nfqa_verdict_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700869 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700870 .attr_count = NFQA_CFG_MAX,
871 .policy = nfqa_cfg_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700872};
873
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700874static const struct nfnetlink_subsystem nfqnl_subsys = {
Harald Welte7af4cc32005-08-09 19:44:15 -0700875 .name = "nf_queue",
876 .subsys_id = NFNL_SUBSYS_QUEUE,
877 .cb_count = NFQNL_MSG_MAX,
Harald Welte7af4cc32005-08-09 19:44:15 -0700878 .cb = nfqnl_cb,
879};
880
Harald Welte838ab632005-08-09 19:50:45 -0700881#ifdef CONFIG_PROC_FS
882struct iter_state {
883 unsigned int bucket;
884};
885
886static struct hlist_node *get_first(struct seq_file *seq)
887{
888 struct iter_state *st = seq->private;
889
890 if (!st)
891 return NULL;
892
893 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
894 if (!hlist_empty(&instance_table[st->bucket]))
895 return instance_table[st->bucket].first;
896 }
897 return NULL;
898}
899
900static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
901{
902 struct iter_state *st = seq->private;
903
904 h = h->next;
905 while (!h) {
906 if (++st->bucket >= INSTANCE_BUCKETS)
907 return NULL;
908
909 h = instance_table[st->bucket].first;
910 }
911 return h;
912}
913
914static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
915{
916 struct hlist_node *head;
917 head = get_first(seq);
918
919 if (head)
920 while (pos && (head = get_next(seq, head)))
921 pos--;
922 return pos ? NULL : head;
923}
924
925static void *seq_start(struct seq_file *seq, loff_t *pos)
926{
927 read_lock_bh(&instances_lock);
928 return get_idx(seq, *pos);
929}
930
931static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
932{
933 (*pos)++;
934 return get_next(s, v);
935}
936
937static void seq_stop(struct seq_file *s, void *v)
938{
939 read_unlock_bh(&instances_lock);
940}
941
942static int seq_show(struct seq_file *s, void *v)
943{
944 const struct nfqnl_instance *inst = v;
945
946 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
947 inst->queue_num,
948 inst->peer_pid, inst->queue_total,
949 inst->copy_mode, inst->copy_range,
950 inst->queue_dropped, inst->queue_user_dropped,
951 atomic_read(&inst->id_sequence),
952 atomic_read(&inst->use));
953}
954
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700955static const struct seq_operations nfqnl_seq_ops = {
Harald Welte838ab632005-08-09 19:50:45 -0700956 .start = seq_start,
957 .next = seq_next,
958 .stop = seq_stop,
959 .show = seq_show,
960};
961
962static int nfqnl_open(struct inode *inode, struct file *file)
963{
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700964 return seq_open_private(file, &nfqnl_seq_ops,
965 sizeof(struct iter_state));
Harald Welte838ab632005-08-09 19:50:45 -0700966}
967
Arjan van de Venda7071d2007-02-12 00:55:36 -0800968static const struct file_operations nfqnl_file_ops = {
Harald Welte838ab632005-08-09 19:50:45 -0700969 .owner = THIS_MODULE,
970 .open = nfqnl_open,
971 .read = seq_read,
972 .llseek = seq_lseek,
973 .release = seq_release_private,
974};
975
976#endif /* PROC_FS */
977
Patrick McHardy32292a72006-04-06 14:11:30 -0700978static int __init nfnetlink_queue_init(void)
Harald Welte7af4cc32005-08-09 19:44:15 -0700979{
Harald Welte838ab632005-08-09 19:50:45 -0700980 int i, status = -ENOMEM;
981#ifdef CONFIG_PROC_FS
982 struct proc_dir_entry *proc_nfqueue;
983#endif
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800984
Harald Welte838ab632005-08-09 19:50:45 -0700985 for (i = 0; i < INSTANCE_BUCKETS; i++)
986 INIT_HLIST_HEAD(&instance_table[i]);
987
Harald Welte7af4cc32005-08-09 19:44:15 -0700988 netlink_register_notifier(&nfqnl_rtnl_notifier);
989 status = nfnetlink_subsys_register(&nfqnl_subsys);
990 if (status < 0) {
991 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
992 goto cleanup_netlink_notifier;
993 }
994
Harald Welte838ab632005-08-09 19:50:45 -0700995#ifdef CONFIG_PROC_FS
996 proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440,
997 proc_net_netfilter);
998 if (!proc_nfqueue)
999 goto cleanup_subsys;
1000 proc_nfqueue->proc_fops = &nfqnl_file_ops;
1001#endif
1002
Harald Welte7af4cc32005-08-09 19:44:15 -07001003 register_netdevice_notifier(&nfqnl_dev_notifier);
1004 return status;
1005
Harald Welte838ab632005-08-09 19:50:45 -07001006#ifdef CONFIG_PROC_FS
1007cleanup_subsys:
Harald Welte7af4cc32005-08-09 19:44:15 -07001008 nfnetlink_subsys_unregister(&nfqnl_subsys);
Patrick McHardy32292a72006-04-06 14:11:30 -07001009#endif
Harald Welte7af4cc32005-08-09 19:44:15 -07001010cleanup_netlink_notifier:
1011 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1012 return status;
1013}
1014
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001015static void __exit nfnetlink_queue_fini(void)
Harald Welte7af4cc32005-08-09 19:44:15 -07001016{
Patrick McHardy32292a72006-04-06 14:11:30 -07001017 nf_unregister_queue_handlers(&nfqh);
1018 unregister_netdevice_notifier(&nfqnl_dev_notifier);
1019#ifdef CONFIG_PROC_FS
1020 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
1021#endif
1022 nfnetlink_subsys_unregister(&nfqnl_subsys);
1023 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
Harald Welte7af4cc32005-08-09 19:44:15 -07001024}
1025
1026MODULE_DESCRIPTION("netfilter packet queue handler");
1027MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1028MODULE_LICENSE("GPL");
1029MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1030
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001031module_init(nfnetlink_queue_init);
1032module_exit(nfnetlink_queue_fini);