blob: 4abf62a6c05723f5a8070d3701b6b58a62f9d88b [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
Patrick McHardye48b9b22007-12-05 01:28:10 -080059 unsigned int id_sequence; /* 'sequence' of pkt ids */
Harald Welte7af4cc32005-08-09 19:44:15 -070060
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;
Harald Welte838ab632005-08-09 19:50:45 -0700142 /* needs to be two, since we _put() after creation */
143 atomic_set(&inst->use, 2);
YOSHIFUJI Hideaki181a46a2006-01-04 13:56:54 -0800144 spin_lock_init(&inst->lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700145 INIT_LIST_HEAD(&inst->queue_list);
146
147 if (!try_module_get(THIS_MODULE))
148 goto out_free;
149
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800150 hlist_add_head(&inst->hlist,
Harald Welte7af4cc32005-08-09 19:44:15 -0700151 &instance_table[instance_hashfn(queue_num)]);
152
153 write_unlock_bh(&instances_lock);
154
155 QDEBUG("successfully created new instance\n");
156
157 return inst;
158
159out_free:
160 kfree(inst);
161out_unlock:
162 write_unlock_bh(&instances_lock);
163 return NULL;
164}
165
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800166static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
167 unsigned long data);
Harald Welte7af4cc32005-08-09 19:44:15 -0700168
169static void
170_instance_destroy2(struct nfqnl_instance *inst, int lock)
171{
172 /* first pull it out of the global list */
173 if (lock)
174 write_lock_bh(&instances_lock);
175
176 QDEBUG("removing instance %p (queuenum=%u) from hash\n",
177 inst, inst->queue_num);
178 hlist_del(&inst->hlist);
179
180 if (lock)
181 write_unlock_bh(&instances_lock);
182
183 /* then flush all pending skbs from the queue */
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800184 nfqnl_flush(inst, NULL, 0);
Harald Welte7af4cc32005-08-09 19:44:15 -0700185
Harald Welte838ab632005-08-09 19:50:45 -0700186 /* and finally put the refcount */
187 instance_put(inst);
Harald Welte7af4cc32005-08-09 19:44:15 -0700188
189 module_put(THIS_MODULE);
190}
191
192static inline void
193__instance_destroy(struct nfqnl_instance *inst)
194{
195 _instance_destroy2(inst, 0);
196}
197
198static inline void
199instance_destroy(struct nfqnl_instance *inst)
200{
201 _instance_destroy2(inst, 1);
202}
203
Harald Welte7af4cc32005-08-09 19:44:15 -0700204static inline void
Patrick McHardy02f014d2007-12-05 01:26:33 -0800205__enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
Harald Welte7af4cc32005-08-09 19:44:15 -0700206{
Patrick McHardy0ac41e82007-12-05 01:25:03 -0800207 list_add_tail(&entry->list, &queue->queue_list);
Harald Welte7af4cc32005-08-09 19:44:15 -0700208 queue->queue_total++;
209}
210
Harald Welte7af4cc32005-08-09 19:44:15 -0700211static inline int
212__nfqnl_set_mode(struct nfqnl_instance *queue,
213 unsigned char mode, unsigned int range)
214{
215 int status = 0;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800216
Harald Welte7af4cc32005-08-09 19:44:15 -0700217 switch (mode) {
218 case NFQNL_COPY_NONE:
219 case NFQNL_COPY_META:
220 queue->copy_mode = mode;
221 queue->copy_range = 0;
222 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800223
Harald Welte7af4cc32005-08-09 19:44:15 -0700224 case NFQNL_COPY_PACKET:
225 queue->copy_mode = mode;
Patrick McHardydf6fb862007-09-28 14:37:03 -0700226 /* we're using struct nlattr which has 16bit nla_len */
Harald Welte7af4cc32005-08-09 19:44:15 -0700227 if (range > 0xffff)
228 queue->copy_range = 0xffff;
229 else
230 queue->copy_range = range;
231 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800232
Harald Welte7af4cc32005-08-09 19:44:15 -0700233 default:
234 status = -EINVAL;
235
236 }
237 return status;
238}
239
Patrick McHardy02f014d2007-12-05 01:26:33 -0800240static struct nf_queue_entry *
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800241find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
Harald Welte7af4cc32005-08-09 19:44:15 -0700242{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800243 struct nf_queue_entry *entry = NULL, *i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800244
Harald Welte7af4cc32005-08-09 19:44:15 -0700245 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800246
247 list_for_each_entry(i, &queue->queue_list, list) {
248 if (i->id == id) {
249 entry = i;
250 break;
251 }
252 }
253
254 if (entry) {
255 list_del(&entry->list);
256 queue->queue_total--;
257 }
258
Harald Welte7af4cc32005-08-09 19:44:15 -0700259 spin_unlock_bh(&queue->lock);
260
261 return entry;
262}
263
264static void
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800265nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
Harald Welte7af4cc32005-08-09 19:44:15 -0700266{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800267 struct nf_queue_entry *entry, *next;
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800268
Harald Welte7af4cc32005-08-09 19:44:15 -0700269 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800270 list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
271 if (!cmpfn || cmpfn(entry, data)) {
272 list_del(&entry->list);
273 queue->queue_total--;
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800274 nf_reinject(entry, NF_DROP);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800275 }
276 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700277 spin_unlock_bh(&queue->lock);
278}
279
280static struct sk_buff *
281nfqnl_build_packet_message(struct nfqnl_instance *queue,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800282 struct nf_queue_entry *entry, int *errp)
Harald Welte7af4cc32005-08-09 19:44:15 -0700283{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700284 sk_buff_data_t old_tail;
Harald Welte7af4cc32005-08-09 19:44:15 -0700285 size_t size;
286 size_t data_len = 0;
287 struct sk_buff *skb;
288 struct nfqnl_msg_packet_hdr pmsg;
289 struct nlmsghdr *nlh;
290 struct nfgenmsg *nfmsg;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800291 struct sk_buff *entskb = entry->skb;
292 struct net_device *indev;
293 struct net_device *outdev;
Al Viro98a4a862006-11-08 00:26:51 -0800294 __be32 tmp_uint;
Harald Welte7af4cc32005-08-09 19:44:15 -0700295
296 QDEBUG("entered\n");
297
Patrick McHardydf6fb862007-09-28 14:37:03 -0700298 size = NLMSG_ALIGN(sizeof(struct nfgenmsg))
299 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
300 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
301 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700302#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700303 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
304 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700305#endif
Patrick McHardydf6fb862007-09-28 14:37:03 -0700306 + nla_total_size(sizeof(u_int32_t)) /* mark */
307 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
308 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
Harald Welte7af4cc32005-08-09 19:44:15 -0700309
Patrick McHardy02f014d2007-12-05 01:26:33 -0800310 outdev = entry->outdev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800311
Harald Welte7af4cc32005-08-09 19:44:15 -0700312 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800313
Harald Welte7af4cc32005-08-09 19:44:15 -0700314 switch (queue->copy_mode) {
315 case NFQNL_COPY_META:
316 case NFQNL_COPY_NONE:
317 data_len = 0;
318 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800319
Harald Welte7af4cc32005-08-09 19:44:15 -0700320 case NFQNL_COPY_PACKET:
Patrick McHardy84fa7932006-08-29 16:44:56 -0700321 if ((entskb->ip_summed == CHECKSUM_PARTIAL ||
322 entskb->ip_summed == CHECKSUM_COMPLETE) &&
323 (*errp = skb_checksum_help(entskb))) {
Patrick McHardye7dfb092005-09-06 15:10:00 -0700324 spin_unlock_bh(&queue->lock);
325 return NULL;
326 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800327 if (queue->copy_range == 0
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800328 || queue->copy_range > entskb->len)
329 data_len = entskb->len;
Harald Welte7af4cc32005-08-09 19:44:15 -0700330 else
331 data_len = queue->copy_range;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800332
Patrick McHardydf6fb862007-09-28 14:37:03 -0700333 size += nla_total_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700334 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800335
Harald Welte7af4cc32005-08-09 19:44:15 -0700336 default:
337 *errp = -EINVAL;
338 spin_unlock_bh(&queue->lock);
339 return NULL;
340 }
341
Patrick McHardye48b9b22007-12-05 01:28:10 -0800342 entry->id = queue->id_sequence++;
343
Harald Welte7af4cc32005-08-09 19:44:15 -0700344 spin_unlock_bh(&queue->lock);
345
346 skb = alloc_skb(size, GFP_ATOMIC);
347 if (!skb)
348 goto nlmsg_failure;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800349
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700350 old_tail = skb->tail;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800351 nlh = NLMSG_PUT(skb, 0, 0,
Harald Welte7af4cc32005-08-09 19:44:15 -0700352 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
353 sizeof(struct nfgenmsg));
354 nfmsg = NLMSG_DATA(nlh);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800355 nfmsg->nfgen_family = entry->pf;
Harald Welte7af4cc32005-08-09 19:44:15 -0700356 nfmsg->version = NFNETLINK_V0;
357 nfmsg->res_id = htons(queue->queue_num);
358
359 pmsg.packet_id = htonl(entry->id);
Al Virofebf0a42006-11-03 00:59:17 -0800360 pmsg.hw_protocol = entskb->protocol;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800361 pmsg.hook = entry->hook;
Harald Welte7af4cc32005-08-09 19:44:15 -0700362
Patrick McHardydf6fb862007-09-28 14:37:03 -0700363 NLA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
Harald Welte7af4cc32005-08-09 19:44:15 -0700364
Patrick McHardy02f014d2007-12-05 01:26:33 -0800365 indev = entry->indev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800366 if (indev) {
367 tmp_uint = htonl(indev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700368#ifndef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700369 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint);
Harald Weltefbcd9232005-08-09 20:22:10 -0700370#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800371 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700372 /* Case 1: indev is physical input device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800373 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700374 * netfilter_bridge) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700375 NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700376 &tmp_uint);
377 /* this is the bridge group "brX" */
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800378 tmp_uint = htonl(indev->br_port->br->dev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700379 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700380 &tmp_uint);
381 } else {
382 /* Case 2: indev is bridge group, we need to look for
383 * physical device (when called from ipv4) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700384 NLA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700385 &tmp_uint);
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800386 if (entskb->nf_bridge
387 && entskb->nf_bridge->physindev) {
388 tmp_uint = htonl(entskb->nf_bridge->physindev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700389 NLA_PUT(skb, NFQA_IFINDEX_PHYSINDEV,
Harald Weltefbcd9232005-08-09 20:22:10 -0700390 sizeof(tmp_uint), &tmp_uint);
391 }
392 }
393#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700394 }
395
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800396 if (outdev) {
397 tmp_uint = htonl(outdev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700398#ifndef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700399 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint);
Harald Weltefbcd9232005-08-09 20:22:10 -0700400#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800401 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700402 /* Case 1: outdev is physical output device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800403 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700404 * netfilter_bridge) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700405 NLA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700406 &tmp_uint);
407 /* this is the bridge group "brX" */
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800408 tmp_uint = htonl(outdev->br_port->br->dev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700409 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700410 &tmp_uint);
411 } else {
412 /* Case 2: outdev is bridge group, we need to look for
413 * physical output device (when called from ipv4) */
Patrick McHardydf6fb862007-09-28 14:37:03 -0700414 NLA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
Harald Weltefbcd9232005-08-09 20:22:10 -0700415 &tmp_uint);
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800416 if (entskb->nf_bridge
417 && entskb->nf_bridge->physoutdev) {
418 tmp_uint = htonl(entskb->nf_bridge->physoutdev->ifindex);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700419 NLA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV,
Harald Weltefbcd9232005-08-09 20:22:10 -0700420 sizeof(tmp_uint), &tmp_uint);
421 }
422 }
423#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700424 }
425
Thomas Graf82e91ff2006-11-09 15:19:14 -0800426 if (entskb->mark) {
427 tmp_uint = htonl(entskb->mark);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700428 NLA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint);
Harald Welte7af4cc32005-08-09 19:44:15 -0700429 }
430
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700431 if (indev && entskb->dev) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700432 struct nfqnl_msg_packet_hw phw;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700433 int len = dev_parse_header(entskb, phw.hw_addr);
434 if (len) {
435 phw.hw_addrlen = htons(len);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700436 NLA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700437 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700438 }
439
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700440 if (entskb->tstamp.tv64) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700441 struct nfqnl_msg_packet_timestamp ts;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700442 struct timeval tv = ktime_to_timeval(entskb->tstamp);
443 ts.sec = cpu_to_be64(tv.tv_sec);
444 ts.usec = cpu_to_be64(tv.tv_usec);
Harald Welte7af4cc32005-08-09 19:44:15 -0700445
Patrick McHardydf6fb862007-09-28 14:37:03 -0700446 NLA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
Harald Welte7af4cc32005-08-09 19:44:15 -0700447 }
448
449 if (data_len) {
Patrick McHardydf6fb862007-09-28 14:37:03 -0700450 struct nlattr *nla;
451 int size = nla_attr_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700452
Patrick McHardydf6fb862007-09-28 14:37:03 -0700453 if (skb_tailroom(skb) < nla_total_size(data_len)) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700454 printk(KERN_WARNING "nf_queue: no tailroom!\n");
455 goto nlmsg_failure;
456 }
457
Patrick McHardydf6fb862007-09-28 14:37:03 -0700458 nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));
459 nla->nla_type = NFQA_PAYLOAD;
460 nla->nla_len = size;
Harald Welte7af4cc32005-08-09 19:44:15 -0700461
Patrick McHardydf6fb862007-09-28 14:37:03 -0700462 if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700463 BUG();
464 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800465
Harald Welte7af4cc32005-08-09 19:44:15 -0700466 nlh->nlmsg_len = skb->tail - old_tail;
467 return skb;
468
469nlmsg_failure:
Patrick McHardydf6fb862007-09-28 14:37:03 -0700470nla_put_failure:
Harald Welte7af4cc32005-08-09 19:44:15 -0700471 if (skb)
472 kfree_skb(skb);
473 *errp = -EINVAL;
474 if (net_ratelimit())
475 printk(KERN_ERR "nf_queue: error creating packet message\n");
476 return NULL;
477}
478
479static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800480nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
Harald Welte7af4cc32005-08-09 19:44:15 -0700481{
482 int status = -EINVAL;
483 struct sk_buff *nskb;
484 struct nfqnl_instance *queue;
Harald Welte7af4cc32005-08-09 19:44:15 -0700485
486 QDEBUG("entered\n");
487
Harald Welte838ab632005-08-09 19:50:45 -0700488 queue = instance_lookup_get(queuenum);
Harald Welte7af4cc32005-08-09 19:44:15 -0700489 if (!queue) {
490 QDEBUG("no queue instance matching\n");
491 return -EINVAL;
492 }
493
494 if (queue->copy_mode == NFQNL_COPY_NONE) {
495 QDEBUG("mode COPY_NONE, aborting\n");
Harald Welte838ab632005-08-09 19:50:45 -0700496 status = -EAGAIN;
497 goto err_out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700498 }
499
Harald Welte7af4cc32005-08-09 19:44:15 -0700500 nskb = nfqnl_build_packet_message(queue, entry, &status);
501 if (nskb == NULL)
Patrick McHardy02f014d2007-12-05 01:26:33 -0800502 goto err_out_put;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800503
Harald Welte7af4cc32005-08-09 19:44:15 -0700504 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800505
Harald Welte7af4cc32005-08-09 19:44:15 -0700506 if (!queue->peer_pid)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800507 goto err_out_free_nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700508
509 if (queue->queue_total >= queue->queue_maxlen) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800510 queue->queue_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700511 status = -ENOSPC;
512 if (net_ratelimit())
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800513 printk(KERN_WARNING "nf_queue: full at %d entries, "
514 "dropping packets(s). Dropped: %d\n",
Harald Welte7af4cc32005-08-09 19:44:15 -0700515 queue->queue_total, queue->queue_dropped);
516 goto err_out_free_nskb;
517 }
518
519 /* nfnetlink_unicast will either free the nskb or add it to a socket */
520 status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
521 if (status < 0) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800522 queue->queue_user_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700523 goto err_out_unlock;
524 }
525
526 __enqueue_entry(queue, entry);
527
528 spin_unlock_bh(&queue->lock);
Harald Welte838ab632005-08-09 19:50:45 -0700529 instance_put(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700530 return status;
531
532err_out_free_nskb:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800533 kfree_skb(nskb);
534
Harald Welte7af4cc32005-08-09 19:44:15 -0700535err_out_unlock:
536 spin_unlock_bh(&queue->lock);
537
Harald Welte838ab632005-08-09 19:50:45 -0700538err_out_put:
539 instance_put(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700540 return status;
541}
542
543static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800544nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e)
Harald Welte7af4cc32005-08-09 19:44:15 -0700545{
546 int diff;
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700547 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700548
549 diff = data_len - e->skb->len;
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800550 if (diff < 0) {
551 if (pskb_trim(e->skb, data_len))
552 return -ENOMEM;
553 } else if (diff > 0) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700554 if (data_len > 0xFFFF)
555 return -EINVAL;
556 if (diff > skb_tailroom(e->skb)) {
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700557 err = pskb_expand_head(e->skb, 0,
558 diff - skb_tailroom(e->skb),
559 GFP_ATOMIC);
560 if (err) {
Patrick McHardy1158ba22006-08-22 00:32:47 -0700561 printk(KERN_WARNING "nf_queue: OOM "
Harald Welte7af4cc32005-08-09 19:44:15 -0700562 "in mangle, dropping packet\n");
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700563 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700564 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700565 }
566 skb_put(e->skb, diff);
567 }
Herbert Xu37d41872007-10-14 00:39:18 -0700568 if (!skb_make_writable(e->skb, data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700569 return -ENOMEM;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300570 skb_copy_to_linear_data(e->skb, data, data_len);
Patrick McHardye7dfb092005-09-06 15:10:00 -0700571 e->skb->ip_summed = CHECKSUM_NONE;
Harald Welte7af4cc32005-08-09 19:44:15 -0700572 return 0;
573}
574
Harald Welte7af4cc32005-08-09 19:44:15 -0700575static int
576nfqnl_set_mode(struct nfqnl_instance *queue,
577 unsigned char mode, unsigned int range)
578{
579 int status;
580
581 spin_lock_bh(&queue->lock);
582 status = __nfqnl_set_mode(queue, mode, range);
583 spin_unlock_bh(&queue->lock);
584
585 return status;
586}
587
588static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800589dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700590{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800591 if (entry->indev)
592 if (entry->indev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700593 return 1;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800594 if (entry->outdev)
595 if (entry->outdev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700596 return 1;
Patrick McHardyef47c6a72006-06-27 03:01:48 -0700597#ifdef CONFIG_BRIDGE_NETFILTER
598 if (entry->skb->nf_bridge) {
599 if (entry->skb->nf_bridge->physindev &&
600 entry->skb->nf_bridge->physindev->ifindex == ifindex)
601 return 1;
602 if (entry->skb->nf_bridge->physoutdev &&
603 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
604 return 1;
605 }
606#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700607 return 0;
608}
609
610/* drop all packets with either indev or outdev == ifindex from all queue
611 * instances */
612static void
613nfqnl_dev_drop(int ifindex)
614{
615 int i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800616
Harald Welte7af4cc32005-08-09 19:44:15 -0700617 QDEBUG("entering for ifindex %u\n", ifindex);
618
619 /* this only looks like we have to hold the readlock for a way too long
620 * time, issue_verdict(), nf_reinject(), ... - but we always only
621 * issue NF_DROP, which is processed directly in nf_reinject() */
622 read_lock_bh(&instances_lock);
623
624 for (i = 0; i < INSTANCE_BUCKETS; i++) {
625 struct hlist_node *tmp;
626 struct nfqnl_instance *inst;
627 struct hlist_head *head = &instance_table[i];
628
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800629 hlist_for_each_entry(inst, tmp, head, hlist)
630 nfqnl_flush(inst, dev_cmp, ifindex);
Harald Welte7af4cc32005-08-09 19:44:15 -0700631 }
632
633 read_unlock_bh(&instances_lock);
634}
635
636#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
637
638static int
639nfqnl_rcv_dev_event(struct notifier_block *this,
640 unsigned long event, void *ptr)
641{
642 struct net_device *dev = ptr;
643
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200644 if (dev->nd_net != &init_net)
645 return NOTIFY_DONE;
646
Harald Welte7af4cc32005-08-09 19:44:15 -0700647 /* Drop any packets associated with the downed device */
648 if (event == NETDEV_DOWN)
649 nfqnl_dev_drop(dev->ifindex);
650 return NOTIFY_DONE;
651}
652
653static struct notifier_block nfqnl_dev_notifier = {
654 .notifier_call = nfqnl_rcv_dev_event,
655};
656
657static int
658nfqnl_rcv_nl_event(struct notifier_block *this,
659 unsigned long event, void *ptr)
660{
661 struct netlink_notify *n = ptr;
662
663 if (event == NETLINK_URELEASE &&
664 n->protocol == NETLINK_NETFILTER && n->pid) {
665 int i;
666
667 /* destroy all instances for this pid */
668 write_lock_bh(&instances_lock);
669 for (i = 0; i < INSTANCE_BUCKETS; i++) {
670 struct hlist_node *tmp, *t2;
671 struct nfqnl_instance *inst;
672 struct hlist_head *head = &instance_table[i];
673
674 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200675 if ((n->net == &init_net) &&
676 (n->pid == inst->peer_pid))
Harald Welte7af4cc32005-08-09 19:44:15 -0700677 __instance_destroy(inst);
678 }
679 }
680 write_unlock_bh(&instances_lock);
681 }
682 return NOTIFY_DONE;
683}
684
685static struct notifier_block nfqnl_rtnl_notifier = {
686 .notifier_call = nfqnl_rcv_nl_event,
687};
688
Patrick McHardy5bf75852007-09-28 14:39:26 -0700689static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
690 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
691 [NFQA_MARK] = { .type = NLA_U32 },
692 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
Harald Welte838ab632005-08-09 19:50:45 -0700693};
694
Harald Welte7af4cc32005-08-09 19:44:15 -0700695static int
696nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700697 struct nlmsghdr *nlh, struct nlattr *nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700698{
699 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
700 u_int16_t queue_num = ntohs(nfmsg->res_id);
701
702 struct nfqnl_msg_verdict_hdr *vhdr;
703 struct nfqnl_instance *queue;
704 unsigned int verdict;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800705 struct nf_queue_entry *entry;
Harald Welte838ab632005-08-09 19:50:45 -0700706 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700707
Harald Welte838ab632005-08-09 19:50:45 -0700708 queue = instance_lookup_get(queue_num);
Harald Welte7af4cc32005-08-09 19:44:15 -0700709 if (!queue)
710 return -ENODEV;
711
Harald Welte838ab632005-08-09 19:50:45 -0700712 if (queue->peer_pid != NETLINK_CB(skb).pid) {
713 err = -EPERM;
714 goto err_out_put;
715 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700716
Patrick McHardydf6fb862007-09-28 14:37:03 -0700717 if (!nfqa[NFQA_VERDICT_HDR]) {
Harald Welte838ab632005-08-09 19:50:45 -0700718 err = -EINVAL;
719 goto err_out_put;
720 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700721
Patrick McHardydf6fb862007-09-28 14:37:03 -0700722 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700723 verdict = ntohl(vhdr->verdict);
724
Harald Welte838ab632005-08-09 19:50:45 -0700725 if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
726 err = -EINVAL;
727 goto err_out_put;
728 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700729
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800730 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
Harald Welte838ab632005-08-09 19:50:45 -0700731 if (entry == NULL) {
732 err = -ENOENT;
733 goto err_out_put;
734 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700735
Patrick McHardydf6fb862007-09-28 14:37:03 -0700736 if (nfqa[NFQA_PAYLOAD]) {
737 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
738 nla_len(nfqa[NFQA_PAYLOAD]), entry) < 0)
Harald Welte7af4cc32005-08-09 19:44:15 -0700739 verdict = NF_DROP;
740 }
741
Patrick McHardydf6fb862007-09-28 14:37:03 -0700742 if (nfqa[NFQA_MARK])
Thomas Graf82e91ff2006-11-09 15:19:14 -0800743 entry->skb->mark = ntohl(*(__be32 *)
Patrick McHardydf6fb862007-09-28 14:37:03 -0700744 nla_data(nfqa[NFQA_MARK]));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800745
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800746 nf_reinject(entry, verdict);
Harald Welte838ab632005-08-09 19:50:45 -0700747 instance_put(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700748 return 0;
Harald Welte838ab632005-08-09 19:50:45 -0700749
750err_out_put:
751 instance_put(queue);
752 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700753}
754
755static int
756nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700757 struct nlmsghdr *nlh, struct nlattr *nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700758{
759 return -ENOTSUPP;
760}
761
Patrick McHardy5bf75852007-09-28 14:39:26 -0700762static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
763 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
764 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
Harald Welte838ab632005-08-09 19:50:45 -0700765};
766
Patrick McHardye3ac5292007-12-05 01:23:57 -0800767static const struct nf_queue_handler nfqh = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700768 .name = "nf_queue",
769 .outfn = &nfqnl_enqueue_packet,
770};
771
Harald Welte7af4cc32005-08-09 19:44:15 -0700772static int
773nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700774 struct nlmsghdr *nlh, struct nlattr *nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700775{
776 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
777 u_int16_t queue_num = ntohs(nfmsg->res_id);
778 struct nfqnl_instance *queue;
Harald Welte838ab632005-08-09 19:50:45 -0700779 int ret = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700780
781 QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type));
782
Harald Welte838ab632005-08-09 19:50:45 -0700783 queue = instance_lookup_get(queue_num);
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800784 if (queue && queue->peer_pid != NETLINK_CB(skb).pid) {
785 ret = -EPERM;
786 goto out_put;
787 }
788
Patrick McHardydf6fb862007-09-28 14:37:03 -0700789 if (nfqa[NFQA_CFG_CMD]) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700790 struct nfqnl_msg_config_cmd *cmd;
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800791
Patrick McHardydf6fb862007-09-28 14:37:03 -0700792 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700793 QDEBUG("found CFG_CMD\n");
794
795 switch (cmd->command) {
796 case NFQNL_CFG_CMD_BIND:
797 if (queue)
798 return -EBUSY;
799
800 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
801 if (!queue)
802 return -EINVAL;
803 break;
804 case NFQNL_CFG_CMD_UNBIND:
805 if (!queue)
806 return -ENODEV;
Harald Welte7af4cc32005-08-09 19:44:15 -0700807 instance_destroy(queue);
808 break;
809 case NFQNL_CFG_CMD_PF_BIND:
810 QDEBUG("registering queue handler for pf=%u\n",
811 ntohs(cmd->pf));
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700812 ret = nf_register_queue_handler(ntohs(cmd->pf), &nfqh);
Harald Welte7af4cc32005-08-09 19:44:15 -0700813 break;
814 case NFQNL_CFG_CMD_PF_UNBIND:
815 QDEBUG("unregistering queue handler for pf=%u\n",
816 ntohs(cmd->pf));
Yasuyuki Kozakaice7663d2007-07-07 22:40:08 -0700817 ret = nf_unregister_queue_handler(ntohs(cmd->pf), &nfqh);
Harald Welte7af4cc32005-08-09 19:44:15 -0700818 break;
819 default:
Harald Welte838ab632005-08-09 19:50:45 -0700820 ret = -EINVAL;
821 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700822 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700823 }
824
Patrick McHardydf6fb862007-09-28 14:37:03 -0700825 if (nfqa[NFQA_CFG_PARAMS]) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700826 struct nfqnl_msg_config_params *params;
Harald Welte7af4cc32005-08-09 19:44:15 -0700827
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800828 if (!queue) {
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800829 ret = -ENODEV;
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800830 goto out_put;
831 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700832 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700833 nfqnl_set_mode(queue, params->copy_mode,
834 ntohl(params->copy_range));
835 }
836
Patrick McHardydf6fb862007-09-28 14:37:03 -0700837 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
Eric Leblond829e17a2006-11-29 02:35:33 +0100838 __be32 *queue_maxlen;
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800839
840 if (!queue) {
841 ret = -ENODEV;
842 goto out_put;
843 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700844 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
Eric Leblond829e17a2006-11-29 02:35:33 +0100845 spin_lock_bh(&queue->lock);
846 queue->queue_maxlen = ntohl(*queue_maxlen);
847 spin_unlock_bh(&queue->lock);
848 }
849
Harald Welte838ab632005-08-09 19:50:45 -0700850out_put:
851 instance_put(queue);
852 return ret;
Harald Welte7af4cc32005-08-09 19:44:15 -0700853}
854
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700855static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
Harald Welte7af4cc32005-08-09 19:44:15 -0700856 [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp,
Harald Welte37d2e7a2005-11-14 15:24:59 -0800857 .attr_count = NFQA_MAX, },
Harald Welte7af4cc32005-08-09 19:44:15 -0700858 [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700859 .attr_count = NFQA_MAX,
860 .policy = nfqa_verdict_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700861 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700862 .attr_count = NFQA_CFG_MAX,
863 .policy = nfqa_cfg_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700864};
865
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700866static const struct nfnetlink_subsystem nfqnl_subsys = {
Harald Welte7af4cc32005-08-09 19:44:15 -0700867 .name = "nf_queue",
868 .subsys_id = NFNL_SUBSYS_QUEUE,
869 .cb_count = NFQNL_MSG_MAX,
Harald Welte7af4cc32005-08-09 19:44:15 -0700870 .cb = nfqnl_cb,
871};
872
Harald Welte838ab632005-08-09 19:50:45 -0700873#ifdef CONFIG_PROC_FS
874struct iter_state {
875 unsigned int bucket;
876};
877
878static struct hlist_node *get_first(struct seq_file *seq)
879{
880 struct iter_state *st = seq->private;
881
882 if (!st)
883 return NULL;
884
885 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
886 if (!hlist_empty(&instance_table[st->bucket]))
887 return instance_table[st->bucket].first;
888 }
889 return NULL;
890}
891
892static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
893{
894 struct iter_state *st = seq->private;
895
896 h = h->next;
897 while (!h) {
898 if (++st->bucket >= INSTANCE_BUCKETS)
899 return NULL;
900
901 h = instance_table[st->bucket].first;
902 }
903 return h;
904}
905
906static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
907{
908 struct hlist_node *head;
909 head = get_first(seq);
910
911 if (head)
912 while (pos && (head = get_next(seq, head)))
913 pos--;
914 return pos ? NULL : head;
915}
916
917static void *seq_start(struct seq_file *seq, loff_t *pos)
918{
919 read_lock_bh(&instances_lock);
920 return get_idx(seq, *pos);
921}
922
923static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
924{
925 (*pos)++;
926 return get_next(s, v);
927}
928
929static void seq_stop(struct seq_file *s, void *v)
930{
931 read_unlock_bh(&instances_lock);
932}
933
934static int seq_show(struct seq_file *s, void *v)
935{
936 const struct nfqnl_instance *inst = v;
937
938 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
939 inst->queue_num,
940 inst->peer_pid, inst->queue_total,
941 inst->copy_mode, inst->copy_range,
942 inst->queue_dropped, inst->queue_user_dropped,
Patrick McHardye48b9b22007-12-05 01:28:10 -0800943 inst->id_sequence,
Harald Welte838ab632005-08-09 19:50:45 -0700944 atomic_read(&inst->use));
945}
946
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700947static const struct seq_operations nfqnl_seq_ops = {
Harald Welte838ab632005-08-09 19:50:45 -0700948 .start = seq_start,
949 .next = seq_next,
950 .stop = seq_stop,
951 .show = seq_show,
952};
953
954static int nfqnl_open(struct inode *inode, struct file *file)
955{
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700956 return seq_open_private(file, &nfqnl_seq_ops,
957 sizeof(struct iter_state));
Harald Welte838ab632005-08-09 19:50:45 -0700958}
959
Arjan van de Venda7071d2007-02-12 00:55:36 -0800960static const struct file_operations nfqnl_file_ops = {
Harald Welte838ab632005-08-09 19:50:45 -0700961 .owner = THIS_MODULE,
962 .open = nfqnl_open,
963 .read = seq_read,
964 .llseek = seq_lseek,
965 .release = seq_release_private,
966};
967
968#endif /* PROC_FS */
969
Patrick McHardy32292a72006-04-06 14:11:30 -0700970static int __init nfnetlink_queue_init(void)
Harald Welte7af4cc32005-08-09 19:44:15 -0700971{
Harald Welte838ab632005-08-09 19:50:45 -0700972 int i, status = -ENOMEM;
973#ifdef CONFIG_PROC_FS
974 struct proc_dir_entry *proc_nfqueue;
975#endif
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800976
Harald Welte838ab632005-08-09 19:50:45 -0700977 for (i = 0; i < INSTANCE_BUCKETS; i++)
978 INIT_HLIST_HEAD(&instance_table[i]);
979
Harald Welte7af4cc32005-08-09 19:44:15 -0700980 netlink_register_notifier(&nfqnl_rtnl_notifier);
981 status = nfnetlink_subsys_register(&nfqnl_subsys);
982 if (status < 0) {
983 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
984 goto cleanup_netlink_notifier;
985 }
986
Harald Welte838ab632005-08-09 19:50:45 -0700987#ifdef CONFIG_PROC_FS
988 proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440,
989 proc_net_netfilter);
990 if (!proc_nfqueue)
991 goto cleanup_subsys;
992 proc_nfqueue->proc_fops = &nfqnl_file_ops;
993#endif
994
Harald Welte7af4cc32005-08-09 19:44:15 -0700995 register_netdevice_notifier(&nfqnl_dev_notifier);
996 return status;
997
Harald Welte838ab632005-08-09 19:50:45 -0700998#ifdef CONFIG_PROC_FS
999cleanup_subsys:
Harald Welte7af4cc32005-08-09 19:44:15 -07001000 nfnetlink_subsys_unregister(&nfqnl_subsys);
Patrick McHardy32292a72006-04-06 14:11:30 -07001001#endif
Harald Welte7af4cc32005-08-09 19:44:15 -07001002cleanup_netlink_notifier:
1003 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1004 return status;
1005}
1006
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001007static void __exit nfnetlink_queue_fini(void)
Harald Welte7af4cc32005-08-09 19:44:15 -07001008{
Patrick McHardy32292a72006-04-06 14:11:30 -07001009 nf_unregister_queue_handlers(&nfqh);
1010 unregister_netdevice_notifier(&nfqnl_dev_notifier);
1011#ifdef CONFIG_PROC_FS
1012 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
1013#endif
1014 nfnetlink_subsys_unregister(&nfqnl_subsys);
1015 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
Harald Welte7af4cc32005-08-09 19:44:15 -07001016}
1017
1018MODULE_DESCRIPTION("netfilter packet queue handler");
1019MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1020MODULE_LICENSE("GPL");
1021MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1022
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001023module_init(nfnetlink_queue_init);
1024module_exit(nfnetlink_queue_fini);