blob: 12e1ab37fcd8f400151f8bced1b13a873b328e77 [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
Jesper Dangaard Brouer67137f32009-06-08 03:11:33 +00003 * userspace via nfnetlink.
Harald Welte7af4cc32005-08-09 19:44:15 -07004 *
5 * (C) 2005 by Harald Welte <laforge@netfilter.org>
Patrick McHardy4ad9d4f2007-12-05 01:31:17 -08006 * (C) 2007 by Patrick McHardy <kaber@trash.net>
Harald Welte7af4cc32005-08-09 19:44:15 -07007 *
8 * Based on the old ipv4-only ip_queue.c:
9 * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
10 * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
16 */
17#include <linux/module.h>
18#include <linux/skbuff.h>
19#include <linux/init.h>
20#include <linux/spinlock.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Harald Welte7af4cc32005-08-09 19:44:15 -070022#include <linux/notifier.h>
23#include <linux/netdevice.h>
24#include <linux/netfilter.h>
Harald Welte838ab632005-08-09 19:50:45 -070025#include <linux/proc_fs.h>
Harald Welte7af4cc32005-08-09 19:44:15 -070026#include <linux/netfilter_ipv4.h>
27#include <linux/netfilter_ipv6.h>
28#include <linux/netfilter/nfnetlink.h>
29#include <linux/netfilter/nfnetlink_queue.h>
30#include <linux/list.h>
31#include <net/sock.h>
Patrick McHardyc01cd422007-12-05 01:24:48 -080032#include <net/netfilter/nf_queue.h>
Harald Welte7af4cc32005-08-09 19:44:15 -070033
34#include <asm/atomic.h>
35
Harald Weltefbcd9232005-08-09 20:22:10 -070036#ifdef CONFIG_BRIDGE_NETFILTER
37#include "../bridge/br_private.h"
38#endif
39
Harald Welte7af4cc32005-08-09 19:44:15 -070040#define NFQNL_QMAX_DEFAULT 1024
41
Harald Welte7af4cc32005-08-09 19:44:15 -070042struct nfqnl_instance {
43 struct hlist_node hlist; /* global list of queues */
Patrick McHardy9872bec2007-12-05 01:28:50 -080044 struct rcu_head rcu;
Harald Welte7af4cc32005-08-09 19:44:15 -070045
46 int peer_pid;
47 unsigned int queue_maxlen;
48 unsigned int copy_range;
49 unsigned int queue_total;
50 unsigned int queue_dropped;
51 unsigned int queue_user_dropped;
52
Patrick McHardye48b9b22007-12-05 01:28:10 -080053 unsigned int id_sequence; /* 'sequence' of pkt ids */
Harald Welte7af4cc32005-08-09 19:44:15 -070054
55 u_int16_t queue_num; /* number of this queue */
56 u_int8_t copy_mode;
57
58 spinlock_t lock;
59
60 struct list_head queue_list; /* packets in queue */
61};
62
Patrick McHardy02f014d2007-12-05 01:26:33 -080063typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
Harald Welte7af4cc32005-08-09 19:44:15 -070064
Patrick McHardy9872bec2007-12-05 01:28:50 -080065static DEFINE_SPINLOCK(instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -070066
Harald Welte7af4cc32005-08-09 19:44:15 -070067#define INSTANCE_BUCKETS 16
Patrick McHardy9d6023a2007-12-05 01:29:38 -080068static struct hlist_head instance_table[INSTANCE_BUCKETS] __read_mostly;
Harald Welte7af4cc32005-08-09 19:44:15 -070069
70static inline u_int8_t instance_hashfn(u_int16_t queue_num)
71{
72 return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
73}
74
75static struct nfqnl_instance *
Patrick McHardy9872bec2007-12-05 01:28:50 -080076instance_lookup(u_int16_t queue_num)
Harald Welte7af4cc32005-08-09 19:44:15 -070077{
78 struct hlist_head *head;
79 struct hlist_node *pos;
80 struct nfqnl_instance *inst;
81
82 head = &instance_table[instance_hashfn(queue_num)];
Patrick McHardy9872bec2007-12-05 01:28:50 -080083 hlist_for_each_entry_rcu(inst, pos, head, hlist) {
Harald Welte7af4cc32005-08-09 19:44:15 -070084 if (inst->queue_num == queue_num)
85 return inst;
86 }
87 return NULL;
88}
89
90static struct nfqnl_instance *
Harald Welte7af4cc32005-08-09 19:44:15 -070091instance_create(u_int16_t queue_num, int pid)
92{
Patrick McHardybaab2ce2007-12-17 22:41:21 -080093 struct nfqnl_instance *inst;
Patrick McHardy9872bec2007-12-05 01:28:50 -080094 unsigned int h;
Patrick McHardybaab2ce2007-12-17 22:41:21 -080095 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -070096
Patrick McHardy9872bec2007-12-05 01:28:50 -080097 spin_lock(&instances_lock);
Patrick McHardybaab2ce2007-12-17 22:41:21 -080098 if (instance_lookup(queue_num)) {
99 err = -EEXIST;
Harald Welte7af4cc32005-08-09 19:44:15 -0700100 goto out_unlock;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800101 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700102
Harald Welte10dfdc62005-11-03 19:20:07 +0100103 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800104 if (!inst) {
105 err = -ENOMEM;
Harald Welte7af4cc32005-08-09 19:44:15 -0700106 goto out_unlock;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800107 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700108
Harald Welte7af4cc32005-08-09 19:44:15 -0700109 inst->queue_num = queue_num;
110 inst->peer_pid = pid;
111 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
112 inst->copy_range = 0xfffff;
113 inst->copy_mode = NFQNL_COPY_NONE;
YOSHIFUJI Hideaki181a46a2006-01-04 13:56:54 -0800114 spin_lock_init(&inst->lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700115 INIT_LIST_HEAD(&inst->queue_list);
116
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800117 if (!try_module_get(THIS_MODULE)) {
118 err = -EAGAIN;
Harald Welte7af4cc32005-08-09 19:44:15 -0700119 goto out_free;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800120 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700121
Patrick McHardy9872bec2007-12-05 01:28:50 -0800122 h = instance_hashfn(queue_num);
123 hlist_add_head_rcu(&inst->hlist, &instance_table[h]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700124
Patrick McHardy9872bec2007-12-05 01:28:50 -0800125 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700126
Harald Welte7af4cc32005-08-09 19:44:15 -0700127 return inst;
128
129out_free:
130 kfree(inst);
131out_unlock:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800132 spin_unlock(&instances_lock);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800133 return ERR_PTR(err);
Harald Welte7af4cc32005-08-09 19:44:15 -0700134}
135
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800136static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
137 unsigned long data);
Harald Welte7af4cc32005-08-09 19:44:15 -0700138
139static void
Patrick McHardy9872bec2007-12-05 01:28:50 -0800140instance_destroy_rcu(struct rcu_head *head)
Harald Welte7af4cc32005-08-09 19:44:15 -0700141{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800142 struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
143 rcu);
Harald Welte7af4cc32005-08-09 19:44:15 -0700144
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800145 nfqnl_flush(inst, NULL, 0);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800146 kfree(inst);
Harald Welte7af4cc32005-08-09 19:44:15 -0700147 module_put(THIS_MODULE);
148}
149
Patrick McHardy9872bec2007-12-05 01:28:50 -0800150static void
Harald Welte7af4cc32005-08-09 19:44:15 -0700151__instance_destroy(struct nfqnl_instance *inst)
152{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800153 hlist_del_rcu(&inst->hlist);
154 call_rcu(&inst->rcu, instance_destroy_rcu);
Harald Welte7af4cc32005-08-09 19:44:15 -0700155}
156
Patrick McHardy9872bec2007-12-05 01:28:50 -0800157static void
Harald Welte7af4cc32005-08-09 19:44:15 -0700158instance_destroy(struct nfqnl_instance *inst)
159{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800160 spin_lock(&instances_lock);
161 __instance_destroy(inst);
162 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700163}
164
Harald Welte7af4cc32005-08-09 19:44:15 -0700165static inline void
Patrick McHardy02f014d2007-12-05 01:26:33 -0800166__enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
Harald Welte7af4cc32005-08-09 19:44:15 -0700167{
Patrick McHardy0ac41e82007-12-05 01:25:03 -0800168 list_add_tail(&entry->list, &queue->queue_list);
Harald Welte7af4cc32005-08-09 19:44:15 -0700169 queue->queue_total++;
170}
171
Patrick McHardy02f014d2007-12-05 01:26:33 -0800172static struct nf_queue_entry *
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800173find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
Harald Welte7af4cc32005-08-09 19:44:15 -0700174{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800175 struct nf_queue_entry *entry = NULL, *i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800176
Harald Welte7af4cc32005-08-09 19:44:15 -0700177 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800178
179 list_for_each_entry(i, &queue->queue_list, list) {
180 if (i->id == id) {
181 entry = i;
182 break;
183 }
184 }
185
186 if (entry) {
187 list_del(&entry->list);
188 queue->queue_total--;
189 }
190
Harald Welte7af4cc32005-08-09 19:44:15 -0700191 spin_unlock_bh(&queue->lock);
192
193 return entry;
194}
195
196static void
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800197nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
Harald Welte7af4cc32005-08-09 19:44:15 -0700198{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800199 struct nf_queue_entry *entry, *next;
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800200
Harald Welte7af4cc32005-08-09 19:44:15 -0700201 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800202 list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
203 if (!cmpfn || cmpfn(entry, data)) {
204 list_del(&entry->list);
205 queue->queue_total--;
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800206 nf_reinject(entry, NF_DROP);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800207 }
208 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700209 spin_unlock_bh(&queue->lock);
210}
211
212static struct sk_buff *
213nfqnl_build_packet_message(struct nfqnl_instance *queue,
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800214 struct nf_queue_entry *entry)
Harald Welte7af4cc32005-08-09 19:44:15 -0700215{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700216 sk_buff_data_t old_tail;
Harald Welte7af4cc32005-08-09 19:44:15 -0700217 size_t size;
218 size_t data_len = 0;
219 struct sk_buff *skb;
220 struct nfqnl_msg_packet_hdr pmsg;
221 struct nlmsghdr *nlh;
222 struct nfgenmsg *nfmsg;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800223 struct sk_buff *entskb = entry->skb;
224 struct net_device *indev;
225 struct net_device *outdev;
Harald Welte7af4cc32005-08-09 19:44:15 -0700226
Eric Leblondcabaa9b2008-03-10 16:41:43 -0700227 size = NLMSG_SPACE(sizeof(struct nfgenmsg))
Patrick McHardydf6fb862007-09-28 14:37:03 -0700228 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
229 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
230 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700231#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700232 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
233 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700234#endif
Patrick McHardydf6fb862007-09-28 14:37:03 -0700235 + nla_total_size(sizeof(u_int32_t)) /* mark */
236 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
237 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
Harald Welte7af4cc32005-08-09 19:44:15 -0700238
Patrick McHardy02f014d2007-12-05 01:26:33 -0800239 outdev = entry->outdev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800240
Harald Welte7af4cc32005-08-09 19:44:15 -0700241 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800242
Patrick McHardy861934c2007-12-05 01:30:29 -0800243 switch ((enum nfqnl_config_mode)queue->copy_mode) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700244 case NFQNL_COPY_META:
245 case NFQNL_COPY_NONE:
Harald Welte7af4cc32005-08-09 19:44:15 -0700246 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800247
Harald Welte7af4cc32005-08-09 19:44:15 -0700248 case NFQNL_COPY_PACKET:
Herbert Xue9f13ca2010-04-08 14:54:35 +0200249 if (entskb->ip_summed == CHECKSUM_PARTIAL &&
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800250 skb_checksum_help(entskb)) {
Patrick McHardye7dfb092005-09-06 15:10:00 -0700251 spin_unlock_bh(&queue->lock);
252 return NULL;
253 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800254 if (queue->copy_range == 0
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800255 || queue->copy_range > entskb->len)
256 data_len = entskb->len;
Harald Welte7af4cc32005-08-09 19:44:15 -0700257 else
258 data_len = queue->copy_range;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800259
Patrick McHardydf6fb862007-09-28 14:37:03 -0700260 size += nla_total_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700261 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700262 }
263
Patrick McHardye48b9b22007-12-05 01:28:10 -0800264 entry->id = queue->id_sequence++;
265
Harald Welte7af4cc32005-08-09 19:44:15 -0700266 spin_unlock_bh(&queue->lock);
267
268 skb = alloc_skb(size, GFP_ATOMIC);
269 if (!skb)
270 goto nlmsg_failure;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800271
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700272 old_tail = skb->tail;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800273 nlh = NLMSG_PUT(skb, 0, 0,
Harald Welte7af4cc32005-08-09 19:44:15 -0700274 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
275 sizeof(struct nfgenmsg));
276 nfmsg = NLMSG_DATA(nlh);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800277 nfmsg->nfgen_family = entry->pf;
Harald Welte7af4cc32005-08-09 19:44:15 -0700278 nfmsg->version = NFNETLINK_V0;
279 nfmsg->res_id = htons(queue->queue_num);
280
281 pmsg.packet_id = htonl(entry->id);
Al Virofebf0a42006-11-03 00:59:17 -0800282 pmsg.hw_protocol = entskb->protocol;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800283 pmsg.hook = entry->hook;
Harald Welte7af4cc32005-08-09 19:44:15 -0700284
Patrick McHardydf6fb862007-09-28 14:37:03 -0700285 NLA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
Harald Welte7af4cc32005-08-09 19:44:15 -0700286
Patrick McHardy02f014d2007-12-05 01:26:33 -0800287 indev = entry->indev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800288 if (indev) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700289#ifndef CONFIG_BRIDGE_NETFILTER
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800290 NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700291#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800292 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700293 /* Case 1: indev is physical input device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800294 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700295 * netfilter_bridge) */
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800296 NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSINDEV,
297 htonl(indev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700298 /* this is the bridge group "brX" */
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800299 NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV,
300 htonl(indev->br_port->br->dev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700301 } else {
302 /* Case 2: indev is bridge group, we need to look for
303 * physical device (when called from ipv4) */
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800304 NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV,
305 htonl(indev->ifindex));
306 if (entskb->nf_bridge && entskb->nf_bridge->physindev)
307 NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSINDEV,
308 htonl(entskb->nf_bridge->physindev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700309 }
310#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700311 }
312
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800313 if (outdev) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700314#ifndef CONFIG_BRIDGE_NETFILTER
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800315 NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700316#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800317 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700318 /* Case 1: outdev is physical output device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800319 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700320 * netfilter_bridge) */
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800321 NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSOUTDEV,
322 htonl(outdev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700323 /* this is the bridge group "brX" */
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800324 NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV,
325 htonl(outdev->br_port->br->dev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700326 } else {
327 /* Case 2: outdev is bridge group, we need to look for
328 * physical output device (when called from ipv4) */
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800329 NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV,
330 htonl(outdev->ifindex));
331 if (entskb->nf_bridge && entskb->nf_bridge->physoutdev)
332 NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSOUTDEV,
333 htonl(entskb->nf_bridge->physoutdev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700334 }
335#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700336 }
337
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800338 if (entskb->mark)
339 NLA_PUT_BE32(skb, NFQA_MARK, htonl(entskb->mark));
Harald Welte7af4cc32005-08-09 19:44:15 -0700340
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700341 if (indev && entskb->dev) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700342 struct nfqnl_msg_packet_hw phw;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700343 int len = dev_parse_header(entskb, phw.hw_addr);
344 if (len) {
345 phw.hw_addrlen = htons(len);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700346 NLA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700347 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700348 }
349
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700350 if (entskb->tstamp.tv64) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700351 struct nfqnl_msg_packet_timestamp ts;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700352 struct timeval tv = ktime_to_timeval(entskb->tstamp);
353 ts.sec = cpu_to_be64(tv.tv_sec);
354 ts.usec = cpu_to_be64(tv.tv_usec);
Harald Welte7af4cc32005-08-09 19:44:15 -0700355
Patrick McHardydf6fb862007-09-28 14:37:03 -0700356 NLA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
Harald Welte7af4cc32005-08-09 19:44:15 -0700357 }
358
359 if (data_len) {
Patrick McHardydf6fb862007-09-28 14:37:03 -0700360 struct nlattr *nla;
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800361 int sz = nla_attr_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700362
Patrick McHardydf6fb862007-09-28 14:37:03 -0700363 if (skb_tailroom(skb) < nla_total_size(data_len)) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700364 printk(KERN_WARNING "nf_queue: no tailroom!\n");
365 goto nlmsg_failure;
366 }
367
Patrick McHardydf6fb862007-09-28 14:37:03 -0700368 nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));
369 nla->nla_type = NFQA_PAYLOAD;
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800370 nla->nla_len = sz;
Harald Welte7af4cc32005-08-09 19:44:15 -0700371
Patrick McHardydf6fb862007-09-28 14:37:03 -0700372 if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700373 BUG();
374 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800375
Harald Welte7af4cc32005-08-09 19:44:15 -0700376 nlh->nlmsg_len = skb->tail - old_tail;
377 return skb;
378
379nlmsg_failure:
Patrick McHardydf6fb862007-09-28 14:37:03 -0700380nla_put_failure:
Harald Welte7af4cc32005-08-09 19:44:15 -0700381 if (skb)
382 kfree_skb(skb);
Harald Welte7af4cc32005-08-09 19:44:15 -0700383 if (net_ratelimit())
384 printk(KERN_ERR "nf_queue: error creating packet message\n");
385 return NULL;
386}
387
388static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800389nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
Harald Welte7af4cc32005-08-09 19:44:15 -0700390{
Harald Welte7af4cc32005-08-09 19:44:15 -0700391 struct sk_buff *nskb;
392 struct nfqnl_instance *queue;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800393 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700394
Patrick McHardy9872bec2007-12-05 01:28:50 -0800395 /* rcu_read_lock()ed by nf_hook_slow() */
396 queue = instance_lookup(queuenum);
Patrick McHardy2bd01192007-12-05 01:29:23 -0800397 if (!queue)
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800398 goto err_out;
Harald Welte7af4cc32005-08-09 19:44:15 -0700399
Patrick McHardy2bd01192007-12-05 01:29:23 -0800400 if (queue->copy_mode == NFQNL_COPY_NONE)
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800401 goto err_out;
Harald Welte7af4cc32005-08-09 19:44:15 -0700402
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800403 nskb = nfqnl_build_packet_message(queue, entry);
Harald Welte7af4cc32005-08-09 19:44:15 -0700404 if (nskb == NULL)
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800405 goto err_out;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800406
Harald Welte7af4cc32005-08-09 19:44:15 -0700407 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800408
Harald Welte7af4cc32005-08-09 19:44:15 -0700409 if (!queue->peer_pid)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800410 goto err_out_free_nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700411
412 if (queue->queue_total >= queue->queue_maxlen) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800413 queue->queue_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700414 if (net_ratelimit())
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800415 printk(KERN_WARNING "nf_queue: full at %d entries, "
Eric Leblonda5d896ad2010-01-18 09:44:39 +0100416 "dropping packets(s).\n",
417 queue->queue_total);
Harald Welte7af4cc32005-08-09 19:44:15 -0700418 goto err_out_free_nskb;
419 }
420
421 /* nfnetlink_unicast will either free the nskb or add it to a socket */
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100422 err = nfnetlink_unicast(nskb, &init_net, queue->peer_pid, MSG_DONTWAIT);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800423 if (err < 0) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800424 queue->queue_user_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700425 goto err_out_unlock;
426 }
427
428 __enqueue_entry(queue, entry);
429
430 spin_unlock_bh(&queue->lock);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800431 return 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700432
433err_out_free_nskb:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800434 kfree_skb(nskb);
Harald Welte7af4cc32005-08-09 19:44:15 -0700435err_out_unlock:
436 spin_unlock_bh(&queue->lock);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800437err_out:
438 return -1;
Harald Welte7af4cc32005-08-09 19:44:15 -0700439}
440
441static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800442nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e)
Harald Welte7af4cc32005-08-09 19:44:15 -0700443{
Patrick McHardye2b58a62008-02-19 17:17:52 -0800444 struct sk_buff *nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700445 int diff;
446
447 diff = data_len - e->skb->len;
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800448 if (diff < 0) {
449 if (pskb_trim(e->skb, data_len))
450 return -ENOMEM;
451 } else if (diff > 0) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700452 if (data_len > 0xFFFF)
453 return -EINVAL;
454 if (diff > skb_tailroom(e->skb)) {
Arnaud Ebalard9a732ed2008-04-29 03:16:34 -0700455 nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
456 diff, GFP_ATOMIC);
Patrick McHardye2b58a62008-02-19 17:17:52 -0800457 if (!nskb) {
Patrick McHardy1158ba22006-08-22 00:32:47 -0700458 printk(KERN_WARNING "nf_queue: OOM "
Harald Welte7af4cc32005-08-09 19:44:15 -0700459 "in mangle, dropping packet\n");
Patrick McHardye2b58a62008-02-19 17:17:52 -0800460 return -ENOMEM;
Harald Welte7af4cc32005-08-09 19:44:15 -0700461 }
Patrick McHardye2b58a62008-02-19 17:17:52 -0800462 kfree_skb(e->skb);
463 e->skb = nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700464 }
465 skb_put(e->skb, diff);
466 }
Herbert Xu37d41872007-10-14 00:39:18 -0700467 if (!skb_make_writable(e->skb, data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700468 return -ENOMEM;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300469 skb_copy_to_linear_data(e->skb, data, data_len);
Patrick McHardye7dfb092005-09-06 15:10:00 -0700470 e->skb->ip_summed = CHECKSUM_NONE;
Harald Welte7af4cc32005-08-09 19:44:15 -0700471 return 0;
472}
473
Harald Welte7af4cc32005-08-09 19:44:15 -0700474static int
475nfqnl_set_mode(struct nfqnl_instance *queue,
476 unsigned char mode, unsigned int range)
477{
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800478 int status = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700479
480 spin_lock_bh(&queue->lock);
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800481 switch (mode) {
482 case NFQNL_COPY_NONE:
483 case NFQNL_COPY_META:
484 queue->copy_mode = mode;
485 queue->copy_range = 0;
486 break;
487
488 case NFQNL_COPY_PACKET:
489 queue->copy_mode = mode;
490 /* we're using struct nlattr which has 16bit nla_len */
491 if (range > 0xffff)
492 queue->copy_range = 0xffff;
493 else
494 queue->copy_range = range;
495 break;
496
497 default:
498 status = -EINVAL;
499
500 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700501 spin_unlock_bh(&queue->lock);
502
503 return status;
504}
505
506static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800507dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700508{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800509 if (entry->indev)
510 if (entry->indev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700511 return 1;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800512 if (entry->outdev)
513 if (entry->outdev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700514 return 1;
Patrick McHardyef47c6a2006-06-27 03:01:48 -0700515#ifdef CONFIG_BRIDGE_NETFILTER
516 if (entry->skb->nf_bridge) {
517 if (entry->skb->nf_bridge->physindev &&
518 entry->skb->nf_bridge->physindev->ifindex == ifindex)
519 return 1;
520 if (entry->skb->nf_bridge->physoutdev &&
521 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
522 return 1;
523 }
524#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700525 return 0;
526}
527
528/* drop all packets with either indev or outdev == ifindex from all queue
529 * instances */
530static void
531nfqnl_dev_drop(int ifindex)
532{
533 int i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800534
Patrick McHardy9872bec2007-12-05 01:28:50 -0800535 rcu_read_lock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700536
Patrick McHardy9872bec2007-12-05 01:28:50 -0800537 for (i = 0; i < INSTANCE_BUCKETS; i++) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700538 struct hlist_node *tmp;
539 struct nfqnl_instance *inst;
540 struct hlist_head *head = &instance_table[i];
541
Patrick McHardy9872bec2007-12-05 01:28:50 -0800542 hlist_for_each_entry_rcu(inst, tmp, head, hlist)
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800543 nfqnl_flush(inst, dev_cmp, ifindex);
Harald Welte7af4cc32005-08-09 19:44:15 -0700544 }
545
Patrick McHardy9872bec2007-12-05 01:28:50 -0800546 rcu_read_unlock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700547}
548
549#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
550
551static int
552nfqnl_rcv_dev_event(struct notifier_block *this,
553 unsigned long event, void *ptr)
554{
555 struct net_device *dev = ptr;
556
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700557 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200558 return NOTIFY_DONE;
559
Harald Welte7af4cc32005-08-09 19:44:15 -0700560 /* Drop any packets associated with the downed device */
561 if (event == NETDEV_DOWN)
562 nfqnl_dev_drop(dev->ifindex);
563 return NOTIFY_DONE;
564}
565
566static struct notifier_block nfqnl_dev_notifier = {
567 .notifier_call = nfqnl_rcv_dev_event,
568};
569
570static int
571nfqnl_rcv_nl_event(struct notifier_block *this,
572 unsigned long event, void *ptr)
573{
574 struct netlink_notify *n = ptr;
575
Patrick McHardydee58172009-11-06 17:04:00 +0100576 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700577 int i;
578
579 /* destroy all instances for this pid */
Patrick McHardy9872bec2007-12-05 01:28:50 -0800580 spin_lock(&instances_lock);
581 for (i = 0; i < INSTANCE_BUCKETS; i++) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700582 struct hlist_node *tmp, *t2;
583 struct nfqnl_instance *inst;
584 struct hlist_head *head = &instance_table[i];
585
586 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200587 if ((n->net == &init_net) &&
588 (n->pid == inst->peer_pid))
Harald Welte7af4cc32005-08-09 19:44:15 -0700589 __instance_destroy(inst);
590 }
591 }
Patrick McHardy9872bec2007-12-05 01:28:50 -0800592 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700593 }
594 return NOTIFY_DONE;
595}
596
597static struct notifier_block nfqnl_rtnl_notifier = {
598 .notifier_call = nfqnl_rcv_nl_event,
599};
600
Patrick McHardy5bf75852007-09-28 14:39:26 -0700601static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
602 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
603 [NFQA_MARK] = { .type = NLA_U32 },
604 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
Harald Welte838ab632005-08-09 19:50:45 -0700605};
606
Harald Welte7af4cc32005-08-09 19:44:15 -0700607static int
608nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200609 const struct nlmsghdr *nlh,
610 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700611{
612 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
613 u_int16_t queue_num = ntohs(nfmsg->res_id);
614
615 struct nfqnl_msg_verdict_hdr *vhdr;
616 struct nfqnl_instance *queue;
617 unsigned int verdict;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800618 struct nf_queue_entry *entry;
Harald Welte838ab632005-08-09 19:50:45 -0700619 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700620
Patrick McHardy9872bec2007-12-05 01:28:50 -0800621 rcu_read_lock();
622 queue = instance_lookup(queue_num);
623 if (!queue) {
624 err = -ENODEV;
625 goto err_out_unlock;
626 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700627
Harald Welte838ab632005-08-09 19:50:45 -0700628 if (queue->peer_pid != NETLINK_CB(skb).pid) {
629 err = -EPERM;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800630 goto err_out_unlock;
Harald Welte838ab632005-08-09 19:50:45 -0700631 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700632
Patrick McHardydf6fb862007-09-28 14:37:03 -0700633 if (!nfqa[NFQA_VERDICT_HDR]) {
Harald Welte838ab632005-08-09 19:50:45 -0700634 err = -EINVAL;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800635 goto err_out_unlock;
Harald Welte838ab632005-08-09 19:50:45 -0700636 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700637
Patrick McHardydf6fb862007-09-28 14:37:03 -0700638 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700639 verdict = ntohl(vhdr->verdict);
640
Harald Welte838ab632005-08-09 19:50:45 -0700641 if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
642 err = -EINVAL;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800643 goto err_out_unlock;
Harald Welte838ab632005-08-09 19:50:45 -0700644 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700645
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800646 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
Harald Welte838ab632005-08-09 19:50:45 -0700647 if (entry == NULL) {
648 err = -ENOENT;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800649 goto err_out_unlock;
Harald Welte838ab632005-08-09 19:50:45 -0700650 }
Patrick McHardy9872bec2007-12-05 01:28:50 -0800651 rcu_read_unlock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700652
Patrick McHardydf6fb862007-09-28 14:37:03 -0700653 if (nfqa[NFQA_PAYLOAD]) {
654 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
655 nla_len(nfqa[NFQA_PAYLOAD]), entry) < 0)
Harald Welte7af4cc32005-08-09 19:44:15 -0700656 verdict = NF_DROP;
657 }
658
Patrick McHardydf6fb862007-09-28 14:37:03 -0700659 if (nfqa[NFQA_MARK])
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800660 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800661
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800662 nf_reinject(entry, verdict);
Harald Welte7af4cc32005-08-09 19:44:15 -0700663 return 0;
Harald Welte838ab632005-08-09 19:50:45 -0700664
Patrick McHardy9872bec2007-12-05 01:28:50 -0800665err_out_unlock:
666 rcu_read_unlock();
Harald Welte838ab632005-08-09 19:50:45 -0700667 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700668}
669
670static int
671nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200672 const struct nlmsghdr *nlh,
673 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700674{
675 return -ENOTSUPP;
676}
677
Patrick McHardy5bf75852007-09-28 14:39:26 -0700678static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
679 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
680 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
Harald Welte838ab632005-08-09 19:50:45 -0700681};
682
Patrick McHardye3ac5292007-12-05 01:23:57 -0800683static const struct nf_queue_handler nfqh = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700684 .name = "nf_queue",
685 .outfn = &nfqnl_enqueue_packet,
686};
687
Harald Welte7af4cc32005-08-09 19:44:15 -0700688static int
689nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200690 const struct nlmsghdr *nlh,
691 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700692{
693 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
694 u_int16_t queue_num = ntohs(nfmsg->res_id);
695 struct nfqnl_instance *queue;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800696 struct nfqnl_msg_config_cmd *cmd = NULL;
Harald Welte838ab632005-08-09 19:50:45 -0700697 int ret = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700698
Patrick McHardy9872bec2007-12-05 01:28:50 -0800699 if (nfqa[NFQA_CFG_CMD]) {
700 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
701
702 /* Commands without queue context - might sleep */
703 switch (cmd->command) {
704 case NFQNL_CFG_CMD_PF_BIND:
Patrick McHardy914afea2008-03-10 16:44:36 -0700705 return nf_register_queue_handler(ntohs(cmd->pf),
706 &nfqh);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800707 case NFQNL_CFG_CMD_PF_UNBIND:
Patrick McHardy914afea2008-03-10 16:44:36 -0700708 return nf_unregister_queue_handler(ntohs(cmd->pf),
709 &nfqh);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800710 }
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800711 }
712
Patrick McHardy9872bec2007-12-05 01:28:50 -0800713 rcu_read_lock();
714 queue = instance_lookup(queue_num);
715 if (queue && queue->peer_pid != NETLINK_CB(skb).pid) {
716 ret = -EPERM;
717 goto err_out_unlock;
718 }
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800719
Patrick McHardy9872bec2007-12-05 01:28:50 -0800720 if (cmd != NULL) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700721 switch (cmd->command) {
722 case NFQNL_CFG_CMD_BIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800723 if (queue) {
724 ret = -EBUSY;
725 goto err_out_unlock;
726 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700727 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800728 if (IS_ERR(queue)) {
729 ret = PTR_ERR(queue);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800730 goto err_out_unlock;
731 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700732 break;
733 case NFQNL_CFG_CMD_UNBIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800734 if (!queue) {
735 ret = -ENODEV;
736 goto err_out_unlock;
737 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700738 instance_destroy(queue);
739 break;
740 case NFQNL_CFG_CMD_PF_BIND:
Harald Welte7af4cc32005-08-09 19:44:15 -0700741 case NFQNL_CFG_CMD_PF_UNBIND:
Harald Welte7af4cc32005-08-09 19:44:15 -0700742 break;
743 default:
Patrick McHardycd21f0a2007-12-17 22:40:19 -0800744 ret = -ENOTSUPP;
Harald Welte838ab632005-08-09 19:50:45 -0700745 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700746 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700747 }
748
Patrick McHardydf6fb862007-09-28 14:37:03 -0700749 if (nfqa[NFQA_CFG_PARAMS]) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700750 struct nfqnl_msg_config_params *params;
Harald Welte7af4cc32005-08-09 19:44:15 -0700751
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800752 if (!queue) {
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800753 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800754 goto err_out_unlock;
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800755 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700756 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700757 nfqnl_set_mode(queue, params->copy_mode,
758 ntohl(params->copy_range));
759 }
760
Patrick McHardydf6fb862007-09-28 14:37:03 -0700761 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
Eric Leblond829e17a2006-11-29 02:35:33 +0100762 __be32 *queue_maxlen;
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800763
764 if (!queue) {
765 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800766 goto err_out_unlock;
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800767 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700768 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
Eric Leblond829e17a2006-11-29 02:35:33 +0100769 spin_lock_bh(&queue->lock);
770 queue->queue_maxlen = ntohl(*queue_maxlen);
771 spin_unlock_bh(&queue->lock);
772 }
773
Patrick McHardy9872bec2007-12-05 01:28:50 -0800774err_out_unlock:
775 rcu_read_unlock();
Harald Welte838ab632005-08-09 19:50:45 -0700776 return ret;
Harald Welte7af4cc32005-08-09 19:44:15 -0700777}
778
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700779static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
Harald Welte7af4cc32005-08-09 19:44:15 -0700780 [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp,
Harald Welte37d2e7a2005-11-14 15:24:59 -0800781 .attr_count = NFQA_MAX, },
Harald Welte7af4cc32005-08-09 19:44:15 -0700782 [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700783 .attr_count = NFQA_MAX,
784 .policy = nfqa_verdict_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700785 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700786 .attr_count = NFQA_CFG_MAX,
787 .policy = nfqa_cfg_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700788};
789
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700790static const struct nfnetlink_subsystem nfqnl_subsys = {
Harald Welte7af4cc32005-08-09 19:44:15 -0700791 .name = "nf_queue",
792 .subsys_id = NFNL_SUBSYS_QUEUE,
793 .cb_count = NFQNL_MSG_MAX,
Harald Welte7af4cc32005-08-09 19:44:15 -0700794 .cb = nfqnl_cb,
795};
796
Harald Welte838ab632005-08-09 19:50:45 -0700797#ifdef CONFIG_PROC_FS
798struct iter_state {
799 unsigned int bucket;
800};
801
802static struct hlist_node *get_first(struct seq_file *seq)
803{
804 struct iter_state *st = seq->private;
805
806 if (!st)
807 return NULL;
808
809 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
810 if (!hlist_empty(&instance_table[st->bucket]))
811 return instance_table[st->bucket].first;
812 }
813 return NULL;
814}
815
816static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
817{
818 struct iter_state *st = seq->private;
819
820 h = h->next;
821 while (!h) {
822 if (++st->bucket >= INSTANCE_BUCKETS)
823 return NULL;
824
825 h = instance_table[st->bucket].first;
826 }
827 return h;
828}
829
830static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
831{
832 struct hlist_node *head;
833 head = get_first(seq);
834
835 if (head)
836 while (pos && (head = get_next(seq, head)))
837 pos--;
838 return pos ? NULL : head;
839}
840
841static void *seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800842 __acquires(instances_lock)
Harald Welte838ab632005-08-09 19:50:45 -0700843{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800844 spin_lock(&instances_lock);
Harald Welte838ab632005-08-09 19:50:45 -0700845 return get_idx(seq, *pos);
846}
847
848static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
849{
850 (*pos)++;
851 return get_next(s, v);
852}
853
854static void seq_stop(struct seq_file *s, void *v)
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800855 __releases(instances_lock)
Harald Welte838ab632005-08-09 19:50:45 -0700856{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800857 spin_unlock(&instances_lock);
Harald Welte838ab632005-08-09 19:50:45 -0700858}
859
860static int seq_show(struct seq_file *s, void *v)
861{
862 const struct nfqnl_instance *inst = v;
863
864 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
865 inst->queue_num,
866 inst->peer_pid, inst->queue_total,
867 inst->copy_mode, inst->copy_range,
868 inst->queue_dropped, inst->queue_user_dropped,
Patrick McHardy9872bec2007-12-05 01:28:50 -0800869 inst->id_sequence, 1);
Harald Welte838ab632005-08-09 19:50:45 -0700870}
871
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700872static const struct seq_operations nfqnl_seq_ops = {
Harald Welte838ab632005-08-09 19:50:45 -0700873 .start = seq_start,
874 .next = seq_next,
875 .stop = seq_stop,
876 .show = seq_show,
877};
878
879static int nfqnl_open(struct inode *inode, struct file *file)
880{
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700881 return seq_open_private(file, &nfqnl_seq_ops,
882 sizeof(struct iter_state));
Harald Welte838ab632005-08-09 19:50:45 -0700883}
884
Arjan van de Venda7071d2007-02-12 00:55:36 -0800885static const struct file_operations nfqnl_file_ops = {
Harald Welte838ab632005-08-09 19:50:45 -0700886 .owner = THIS_MODULE,
887 .open = nfqnl_open,
888 .read = seq_read,
889 .llseek = seq_lseek,
890 .release = seq_release_private,
891};
892
893#endif /* PROC_FS */
894
Patrick McHardy32292a72006-04-06 14:11:30 -0700895static int __init nfnetlink_queue_init(void)
Harald Welte7af4cc32005-08-09 19:44:15 -0700896{
Harald Welte838ab632005-08-09 19:50:45 -0700897 int i, status = -ENOMEM;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800898
Harald Welte838ab632005-08-09 19:50:45 -0700899 for (i = 0; i < INSTANCE_BUCKETS; i++)
900 INIT_HLIST_HEAD(&instance_table[i]);
901
Harald Welte7af4cc32005-08-09 19:44:15 -0700902 netlink_register_notifier(&nfqnl_rtnl_notifier);
903 status = nfnetlink_subsys_register(&nfqnl_subsys);
904 if (status < 0) {
905 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
906 goto cleanup_netlink_notifier;
907 }
908
Harald Welte838ab632005-08-09 19:50:45 -0700909#ifdef CONFIG_PROC_FS
Denis V. Lunev8eeee8b2008-03-27 16:55:53 -0700910 if (!proc_create("nfnetlink_queue", 0440,
911 proc_net_netfilter, &nfqnl_file_ops))
Harald Welte838ab632005-08-09 19:50:45 -0700912 goto cleanup_subsys;
Harald Welte838ab632005-08-09 19:50:45 -0700913#endif
914
Harald Welte7af4cc32005-08-09 19:44:15 -0700915 register_netdevice_notifier(&nfqnl_dev_notifier);
916 return status;
917
Harald Welte838ab632005-08-09 19:50:45 -0700918#ifdef CONFIG_PROC_FS
919cleanup_subsys:
Harald Welte7af4cc32005-08-09 19:44:15 -0700920 nfnetlink_subsys_unregister(&nfqnl_subsys);
Patrick McHardy32292a72006-04-06 14:11:30 -0700921#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700922cleanup_netlink_notifier:
923 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
924 return status;
925}
926
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800927static void __exit nfnetlink_queue_fini(void)
Harald Welte7af4cc32005-08-09 19:44:15 -0700928{
Patrick McHardy32292a72006-04-06 14:11:30 -0700929 nf_unregister_queue_handlers(&nfqh);
930 unregister_netdevice_notifier(&nfqnl_dev_notifier);
931#ifdef CONFIG_PROC_FS
932 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
933#endif
934 nfnetlink_subsys_unregister(&nfqnl_subsys);
935 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
Jesper Dangaard Brouer67137f32009-06-08 03:11:33 +0000936
937 rcu_barrier(); /* Wait for completion of call_rcu()'s */
Harald Welte7af4cc32005-08-09 19:44:15 -0700938}
939
940MODULE_DESCRIPTION("netfilter packet queue handler");
941MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
942MODULE_LICENSE("GPL");
943MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
944
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800945module_init(nfnetlink_queue_init);
946module_exit(nfnetlink_queue_fini);