blob: 436b442d4edc07dfe87be8da239a9728c47f0855 [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
Harald Welte7af4cc32005-08-09 19:44:15 -070040struct nfqnl_instance {
41 struct hlist_node hlist; /* global list of queues */
Patrick McHardy9872bec2007-12-05 01:28:50 -080042 struct rcu_head rcu;
Harald Welte7af4cc32005-08-09 19:44:15 -070043
44 int peer_pid;
45 unsigned int queue_maxlen;
46 unsigned int copy_range;
47 unsigned int queue_total;
48 unsigned int queue_dropped;
49 unsigned int queue_user_dropped;
50
Patrick McHardye48b9b22007-12-05 01:28:10 -080051 unsigned int id_sequence; /* 'sequence' of pkt ids */
Harald Welte7af4cc32005-08-09 19:44:15 -070052
53 u_int16_t queue_num; /* number of this queue */
54 u_int8_t copy_mode;
55
56 spinlock_t lock;
57
58 struct list_head queue_list; /* packets in queue */
59};
60
Patrick McHardy02f014d2007-12-05 01:26:33 -080061typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
Harald Welte7af4cc32005-08-09 19:44:15 -070062
Patrick McHardy9872bec2007-12-05 01:28:50 -080063static DEFINE_SPINLOCK(instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -070064
Harald Welte7af4cc32005-08-09 19:44:15 -070065#define INSTANCE_BUCKETS 16
Patrick McHardy9d6023a2007-12-05 01:29:38 -080066static struct hlist_head instance_table[INSTANCE_BUCKETS] __read_mostly;
Harald Welte7af4cc32005-08-09 19:44:15 -070067
68static inline u_int8_t instance_hashfn(u_int16_t queue_num)
69{
70 return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
71}
72
73static struct nfqnl_instance *
Patrick McHardy9872bec2007-12-05 01:28:50 -080074instance_lookup(u_int16_t queue_num)
Harald Welte7af4cc32005-08-09 19:44:15 -070075{
76 struct hlist_head *head;
77 struct hlist_node *pos;
78 struct nfqnl_instance *inst;
79
80 head = &instance_table[instance_hashfn(queue_num)];
Patrick McHardy9872bec2007-12-05 01:28:50 -080081 hlist_for_each_entry_rcu(inst, pos, head, hlist) {
Harald Welte7af4cc32005-08-09 19:44:15 -070082 if (inst->queue_num == queue_num)
83 return inst;
84 }
85 return NULL;
86}
87
88static struct nfqnl_instance *
Harald Welte7af4cc32005-08-09 19:44:15 -070089instance_create(u_int16_t queue_num, int pid)
90{
Patrick McHardy2bd01192007-12-05 01:29:23 -080091 struct nfqnl_instance *inst = NULL;
Patrick McHardy9872bec2007-12-05 01:28:50 -080092 unsigned int h;
Harald Welte7af4cc32005-08-09 19:44:15 -070093
Patrick McHardy9872bec2007-12-05 01:28:50 -080094 spin_lock(&instances_lock);
Patrick McHardy2bd01192007-12-05 01:29:23 -080095 if (instance_lookup(queue_num))
Harald Welte7af4cc32005-08-09 19:44:15 -070096 goto out_unlock;
Harald Welte7af4cc32005-08-09 19:44:15 -070097
Harald Welte10dfdc62005-11-03 19:20:07 +010098 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
Harald Welte7af4cc32005-08-09 19:44:15 -070099 if (!inst)
100 goto out_unlock;
101
Harald Welte7af4cc32005-08-09 19:44:15 -0700102 inst->queue_num = queue_num;
103 inst->peer_pid = pid;
104 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
105 inst->copy_range = 0xfffff;
106 inst->copy_mode = NFQNL_COPY_NONE;
YOSHIFUJI Hideaki181a46a2006-01-04 13:56:54 -0800107 spin_lock_init(&inst->lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700108 INIT_LIST_HEAD(&inst->queue_list);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800109 INIT_RCU_HEAD(&inst->rcu);
Harald Welte7af4cc32005-08-09 19:44:15 -0700110
111 if (!try_module_get(THIS_MODULE))
112 goto out_free;
113
Patrick McHardy9872bec2007-12-05 01:28:50 -0800114 h = instance_hashfn(queue_num);
115 hlist_add_head_rcu(&inst->hlist, &instance_table[h]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700116
Patrick McHardy9872bec2007-12-05 01:28:50 -0800117 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700118
Harald Welte7af4cc32005-08-09 19:44:15 -0700119 return inst;
120
121out_free:
122 kfree(inst);
123out_unlock:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800124 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700125 return NULL;
126}
127
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800128static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
129 unsigned long data);
Harald Welte7af4cc32005-08-09 19:44:15 -0700130
131static void
Patrick McHardy9872bec2007-12-05 01:28:50 -0800132instance_destroy_rcu(struct rcu_head *head)
Harald Welte7af4cc32005-08-09 19:44:15 -0700133{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800134 struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
135 rcu);
Harald Welte7af4cc32005-08-09 19:44:15 -0700136
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800137 nfqnl_flush(inst, NULL, 0);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800138 kfree(inst);
Harald Welte7af4cc32005-08-09 19:44:15 -0700139 module_put(THIS_MODULE);
140}
141
Patrick McHardy9872bec2007-12-05 01:28:50 -0800142static void
Harald Welte7af4cc32005-08-09 19:44:15 -0700143__instance_destroy(struct nfqnl_instance *inst)
144{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800145 hlist_del_rcu(&inst->hlist);
146 call_rcu(&inst->rcu, instance_destroy_rcu);
Harald Welte7af4cc32005-08-09 19:44:15 -0700147}
148
Patrick McHardy9872bec2007-12-05 01:28:50 -0800149static void
Harald Welte7af4cc32005-08-09 19:44:15 -0700150instance_destroy(struct nfqnl_instance *inst)
151{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800152 spin_lock(&instances_lock);
153 __instance_destroy(inst);
154 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700155}
156
Harald Welte7af4cc32005-08-09 19:44:15 -0700157static inline void
Patrick McHardy02f014d2007-12-05 01:26:33 -0800158__enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
Harald Welte7af4cc32005-08-09 19:44:15 -0700159{
Patrick McHardy0ac41e82007-12-05 01:25:03 -0800160 list_add_tail(&entry->list, &queue->queue_list);
Harald Welte7af4cc32005-08-09 19:44:15 -0700161 queue->queue_total++;
162}
163
Patrick McHardy02f014d2007-12-05 01:26:33 -0800164static struct nf_queue_entry *
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800165find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
Harald Welte7af4cc32005-08-09 19:44:15 -0700166{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800167 struct nf_queue_entry *entry = NULL, *i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800168
Harald Welte7af4cc32005-08-09 19:44:15 -0700169 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800170
171 list_for_each_entry(i, &queue->queue_list, list) {
172 if (i->id == id) {
173 entry = i;
174 break;
175 }
176 }
177
178 if (entry) {
179 list_del(&entry->list);
180 queue->queue_total--;
181 }
182
Harald Welte7af4cc32005-08-09 19:44:15 -0700183 spin_unlock_bh(&queue->lock);
184
185 return entry;
186}
187
188static void
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800189nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
Harald Welte7af4cc32005-08-09 19:44:15 -0700190{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800191 struct nf_queue_entry *entry, *next;
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800192
Harald Welte7af4cc32005-08-09 19:44:15 -0700193 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800194 list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
195 if (!cmpfn || cmpfn(entry, data)) {
196 list_del(&entry->list);
197 queue->queue_total--;
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800198 nf_reinject(entry, NF_DROP);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800199 }
200 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700201 spin_unlock_bh(&queue->lock);
202}
203
204static struct sk_buff *
205nfqnl_build_packet_message(struct nfqnl_instance *queue,
Patrick McHardy02f014d2007-12-05 01:26:33 -0800206 struct nf_queue_entry *entry, int *errp)
Harald Welte7af4cc32005-08-09 19:44:15 -0700207{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700208 sk_buff_data_t old_tail;
Harald Welte7af4cc32005-08-09 19:44:15 -0700209 size_t size;
210 size_t data_len = 0;
211 struct sk_buff *skb;
212 struct nfqnl_msg_packet_hdr pmsg;
213 struct nlmsghdr *nlh;
214 struct nfgenmsg *nfmsg;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800215 struct sk_buff *entskb = entry->skb;
216 struct net_device *indev;
217 struct net_device *outdev;
Harald Welte7af4cc32005-08-09 19:44:15 -0700218
Patrick McHardydf6fb862007-09-28 14:37:03 -0700219 size = NLMSG_ALIGN(sizeof(struct nfgenmsg))
220 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
221 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
222 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700223#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700224 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
225 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700226#endif
Patrick McHardydf6fb862007-09-28 14:37:03 -0700227 + nla_total_size(sizeof(u_int32_t)) /* mark */
228 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
229 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
Harald Welte7af4cc32005-08-09 19:44:15 -0700230
Patrick McHardy02f014d2007-12-05 01:26:33 -0800231 outdev = entry->outdev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800232
Harald Welte7af4cc32005-08-09 19:44:15 -0700233 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800234
Harald Welte7af4cc32005-08-09 19:44:15 -0700235 switch (queue->copy_mode) {
236 case NFQNL_COPY_META:
237 case NFQNL_COPY_NONE:
238 data_len = 0;
239 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800240
Harald Welte7af4cc32005-08-09 19:44:15 -0700241 case NFQNL_COPY_PACKET:
Patrick McHardy84fa7932006-08-29 16:44:56 -0700242 if ((entskb->ip_summed == CHECKSUM_PARTIAL ||
243 entskb->ip_summed == CHECKSUM_COMPLETE) &&
244 (*errp = skb_checksum_help(entskb))) {
Patrick McHardye7dfb092005-09-06 15:10:00 -0700245 spin_unlock_bh(&queue->lock);
246 return NULL;
247 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800248 if (queue->copy_range == 0
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800249 || queue->copy_range > entskb->len)
250 data_len = entskb->len;
Harald Welte7af4cc32005-08-09 19:44:15 -0700251 else
252 data_len = queue->copy_range;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800253
Patrick McHardydf6fb862007-09-28 14:37:03 -0700254 size += nla_total_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700255 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800256
Harald Welte7af4cc32005-08-09 19:44:15 -0700257 default:
258 *errp = -EINVAL;
259 spin_unlock_bh(&queue->lock);
260 return NULL;
261 }
262
Patrick McHardye48b9b22007-12-05 01:28:10 -0800263 entry->id = queue->id_sequence++;
264
Harald Welte7af4cc32005-08-09 19:44:15 -0700265 spin_unlock_bh(&queue->lock);
266
267 skb = alloc_skb(size, GFP_ATOMIC);
268 if (!skb)
269 goto nlmsg_failure;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800270
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700271 old_tail = skb->tail;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800272 nlh = NLMSG_PUT(skb, 0, 0,
Harald Welte7af4cc32005-08-09 19:44:15 -0700273 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
274 sizeof(struct nfgenmsg));
275 nfmsg = NLMSG_DATA(nlh);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800276 nfmsg->nfgen_family = entry->pf;
Harald Welte7af4cc32005-08-09 19:44:15 -0700277 nfmsg->version = NFNETLINK_V0;
278 nfmsg->res_id = htons(queue->queue_num);
279
280 pmsg.packet_id = htonl(entry->id);
Al Virofebf0a42006-11-03 00:59:17 -0800281 pmsg.hw_protocol = entskb->protocol;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800282 pmsg.hook = entry->hook;
Harald Welte7af4cc32005-08-09 19:44:15 -0700283
Patrick McHardydf6fb862007-09-28 14:37:03 -0700284 NLA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
Harald Welte7af4cc32005-08-09 19:44:15 -0700285
Patrick McHardy02f014d2007-12-05 01:26:33 -0800286 indev = entry->indev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800287 if (indev) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700288#ifndef CONFIG_BRIDGE_NETFILTER
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800289 NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700290#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800291 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700292 /* Case 1: indev is physical input device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800293 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700294 * netfilter_bridge) */
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800295 NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSINDEV,
296 htonl(indev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700297 /* this is the bridge group "brX" */
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800298 NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV,
299 htonl(indev->br_port->br->dev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700300 } else {
301 /* Case 2: indev is bridge group, we need to look for
302 * physical device (when called from ipv4) */
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800303 NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV,
304 htonl(indev->ifindex));
305 if (entskb->nf_bridge && entskb->nf_bridge->physindev)
306 NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSINDEV,
307 htonl(entskb->nf_bridge->physindev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700308 }
309#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700310 }
311
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800312 if (outdev) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700313#ifndef CONFIG_BRIDGE_NETFILTER
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800314 NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700315#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800316 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700317 /* Case 1: outdev is physical output device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800318 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700319 * netfilter_bridge) */
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800320 NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSOUTDEV,
321 htonl(outdev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700322 /* this is the bridge group "brX" */
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800323 NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV,
324 htonl(outdev->br_port->br->dev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700325 } else {
326 /* Case 2: outdev is bridge group, we need to look for
327 * physical output device (when called from ipv4) */
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800328 NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV,
329 htonl(outdev->ifindex));
330 if (entskb->nf_bridge && entskb->nf_bridge->physoutdev)
331 NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSOUTDEV,
332 htonl(entskb->nf_bridge->physoutdev->ifindex));
Harald Weltefbcd9232005-08-09 20:22:10 -0700333 }
334#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700335 }
336
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800337 if (entskb->mark)
338 NLA_PUT_BE32(skb, NFQA_MARK, htonl(entskb->mark));
Harald Welte7af4cc32005-08-09 19:44:15 -0700339
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700340 if (indev && entskb->dev) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700341 struct nfqnl_msg_packet_hw phw;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700342 int len = dev_parse_header(entskb, phw.hw_addr);
343 if (len) {
344 phw.hw_addrlen = htons(len);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700345 NLA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700346 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700347 }
348
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700349 if (entskb->tstamp.tv64) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700350 struct nfqnl_msg_packet_timestamp ts;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700351 struct timeval tv = ktime_to_timeval(entskb->tstamp);
352 ts.sec = cpu_to_be64(tv.tv_sec);
353 ts.usec = cpu_to_be64(tv.tv_usec);
Harald Welte7af4cc32005-08-09 19:44:15 -0700354
Patrick McHardydf6fb862007-09-28 14:37:03 -0700355 NLA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
Harald Welte7af4cc32005-08-09 19:44:15 -0700356 }
357
358 if (data_len) {
Patrick McHardydf6fb862007-09-28 14:37:03 -0700359 struct nlattr *nla;
360 int size = nla_attr_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700361
Patrick McHardydf6fb862007-09-28 14:37:03 -0700362 if (skb_tailroom(skb) < nla_total_size(data_len)) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700363 printk(KERN_WARNING "nf_queue: no tailroom!\n");
364 goto nlmsg_failure;
365 }
366
Patrick McHardydf6fb862007-09-28 14:37:03 -0700367 nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));
368 nla->nla_type = NFQA_PAYLOAD;
369 nla->nla_len = size;
Harald Welte7af4cc32005-08-09 19:44:15 -0700370
Patrick McHardydf6fb862007-09-28 14:37:03 -0700371 if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700372 BUG();
373 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800374
Harald Welte7af4cc32005-08-09 19:44:15 -0700375 nlh->nlmsg_len = skb->tail - old_tail;
376 return skb;
377
378nlmsg_failure:
Patrick McHardydf6fb862007-09-28 14:37:03 -0700379nla_put_failure:
Harald Welte7af4cc32005-08-09 19:44:15 -0700380 if (skb)
381 kfree_skb(skb);
382 *errp = -EINVAL;
383 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{
391 int status = -EINVAL;
392 struct sk_buff *nskb;
393 struct nfqnl_instance *queue;
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)
Harald Welte7af4cc32005-08-09 19:44:15 -0700398 return -EINVAL;
Harald Welte7af4cc32005-08-09 19:44:15 -0700399
Patrick McHardy2bd01192007-12-05 01:29:23 -0800400 if (queue->copy_mode == NFQNL_COPY_NONE)
Patrick McHardy9872bec2007-12-05 01:28:50 -0800401 return -EAGAIN;
Harald Welte7af4cc32005-08-09 19:44:15 -0700402
Harald Welte7af4cc32005-08-09 19:44:15 -0700403 nskb = nfqnl_build_packet_message(queue, entry, &status);
404 if (nskb == NULL)
Patrick McHardy9872bec2007-12-05 01:28:50 -0800405 return status;
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 status = -ENOSPC;
415 if (net_ratelimit())
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800416 printk(KERN_WARNING "nf_queue: full at %d entries, "
417 "dropping packets(s). Dropped: %d\n",
Harald Welte7af4cc32005-08-09 19:44:15 -0700418 queue->queue_total, queue->queue_dropped);
419 goto err_out_free_nskb;
420 }
421
422 /* nfnetlink_unicast will either free the nskb or add it to a socket */
423 status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
424 if (status < 0) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800425 queue->queue_user_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700426 goto err_out_unlock;
427 }
428
429 __enqueue_entry(queue, entry);
430
431 spin_unlock_bh(&queue->lock);
432 return status;
433
434err_out_free_nskb:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800435 kfree_skb(nskb);
436
Harald Welte7af4cc32005-08-09 19:44:15 -0700437err_out_unlock:
438 spin_unlock_bh(&queue->lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700439 return status;
440}
441
442static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800443nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e)
Harald Welte7af4cc32005-08-09 19:44:15 -0700444{
445 int diff;
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700446 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700447
448 diff = data_len - e->skb->len;
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800449 if (diff < 0) {
450 if (pskb_trim(e->skb, data_len))
451 return -ENOMEM;
452 } else if (diff > 0) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700453 if (data_len > 0xFFFF)
454 return -EINVAL;
455 if (diff > skb_tailroom(e->skb)) {
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700456 err = pskb_expand_head(e->skb, 0,
457 diff - skb_tailroom(e->skb),
458 GFP_ATOMIC);
459 if (err) {
Patrick McHardy1158ba22006-08-22 00:32:47 -0700460 printk(KERN_WARNING "nf_queue: OOM "
Harald Welte7af4cc32005-08-09 19:44:15 -0700461 "in mangle, dropping packet\n");
Herbert Xu2ca7b0a2007-10-14 00:39:55 -0700462 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700463 }
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
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200557 if (dev->nd_net != &init_net)
558 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
576 if (event == NETLINK_URELEASE &&
577 n->protocol == NETLINK_NETFILTER && n->pid) {
578 int i;
579
580 /* destroy all instances for this pid */
Patrick McHardy9872bec2007-12-05 01:28:50 -0800581 spin_lock(&instances_lock);
582 for (i = 0; i < INSTANCE_BUCKETS; i++) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700583 struct hlist_node *tmp, *t2;
584 struct nfqnl_instance *inst;
585 struct hlist_head *head = &instance_table[i];
586
587 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200588 if ((n->net == &init_net) &&
589 (n->pid == inst->peer_pid))
Harald Welte7af4cc32005-08-09 19:44:15 -0700590 __instance_destroy(inst);
591 }
592 }
Patrick McHardy9872bec2007-12-05 01:28:50 -0800593 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700594 }
595 return NOTIFY_DONE;
596}
597
598static struct notifier_block nfqnl_rtnl_notifier = {
599 .notifier_call = nfqnl_rcv_nl_event,
600};
601
Patrick McHardy5bf75852007-09-28 14:39:26 -0700602static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
603 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
604 [NFQA_MARK] = { .type = NLA_U32 },
605 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
Harald Welte838ab632005-08-09 19:50:45 -0700606};
607
Harald Welte7af4cc32005-08-09 19:44:15 -0700608static int
609nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700610 struct nlmsghdr *nlh, struct nlattr *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 McHardydf6fb862007-09-28 14:37:03 -0700672 struct nlmsghdr *nlh, struct nlattr *nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700673{
674 return -ENOTSUPP;
675}
676
Patrick McHardy5bf75852007-09-28 14:39:26 -0700677static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
678 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
679 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
Harald Welte838ab632005-08-09 19:50:45 -0700680};
681
Patrick McHardye3ac5292007-12-05 01:23:57 -0800682static const struct nf_queue_handler nfqh = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700683 .name = "nf_queue",
684 .outfn = &nfqnl_enqueue_packet,
685};
686
Harald Welte7af4cc32005-08-09 19:44:15 -0700687static int
688nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700689 struct nlmsghdr *nlh, struct nlattr *nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700690{
691 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
692 u_int16_t queue_num = ntohs(nfmsg->res_id);
693 struct nfqnl_instance *queue;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800694 struct nfqnl_msg_config_cmd *cmd = NULL;
Harald Welte838ab632005-08-09 19:50:45 -0700695 int ret = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700696
Patrick McHardy9872bec2007-12-05 01:28:50 -0800697 if (nfqa[NFQA_CFG_CMD]) {
698 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
699
700 /* Commands without queue context - might sleep */
701 switch (cmd->command) {
702 case NFQNL_CFG_CMD_PF_BIND:
703 ret = nf_register_queue_handler(ntohs(cmd->pf),
704 &nfqh);
705 break;
706 case NFQNL_CFG_CMD_PF_UNBIND:
707 ret = nf_unregister_queue_handler(ntohs(cmd->pf),
708 &nfqh);
709 break;
710 default:
711 break;
712 }
713
714 if (ret < 0)
715 return ret;
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800716 }
717
Patrick McHardy9872bec2007-12-05 01:28:50 -0800718 rcu_read_lock();
719 queue = instance_lookup(queue_num);
720 if (queue && queue->peer_pid != NETLINK_CB(skb).pid) {
721 ret = -EPERM;
722 goto err_out_unlock;
723 }
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800724
Patrick McHardy9872bec2007-12-05 01:28:50 -0800725 if (cmd != NULL) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700726 switch (cmd->command) {
727 case NFQNL_CFG_CMD_BIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800728 if (queue) {
729 ret = -EBUSY;
730 goto err_out_unlock;
731 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700732 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800733 if (!queue) {
734 ret = -EINVAL;
735 goto err_out_unlock;
736 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700737 break;
738 case NFQNL_CFG_CMD_UNBIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800739 if (!queue) {
740 ret = -ENODEV;
741 goto err_out_unlock;
742 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700743 instance_destroy(queue);
744 break;
745 case NFQNL_CFG_CMD_PF_BIND:
Harald Welte7af4cc32005-08-09 19:44:15 -0700746 case NFQNL_CFG_CMD_PF_UNBIND:
Harald Welte7af4cc32005-08-09 19:44:15 -0700747 break;
748 default:
Harald Welte838ab632005-08-09 19:50:45 -0700749 ret = -EINVAL;
750 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700751 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700752 }
753
Patrick McHardydf6fb862007-09-28 14:37:03 -0700754 if (nfqa[NFQA_CFG_PARAMS]) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700755 struct nfqnl_msg_config_params *params;
Harald Welte7af4cc32005-08-09 19:44:15 -0700756
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800757 if (!queue) {
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800758 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800759 goto err_out_unlock;
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800760 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700761 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700762 nfqnl_set_mode(queue, params->copy_mode,
763 ntohl(params->copy_range));
764 }
765
Patrick McHardydf6fb862007-09-28 14:37:03 -0700766 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
Eric Leblond829e17a2006-11-29 02:35:33 +0100767 __be32 *queue_maxlen;
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800768
769 if (!queue) {
770 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800771 goto err_out_unlock;
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800772 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700773 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
Eric Leblond829e17a2006-11-29 02:35:33 +0100774 spin_lock_bh(&queue->lock);
775 queue->queue_maxlen = ntohl(*queue_maxlen);
776 spin_unlock_bh(&queue->lock);
777 }
778
Patrick McHardy9872bec2007-12-05 01:28:50 -0800779err_out_unlock:
780 rcu_read_unlock();
Harald Welte838ab632005-08-09 19:50:45 -0700781 return ret;
Harald Welte7af4cc32005-08-09 19:44:15 -0700782}
783
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700784static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
Harald Welte7af4cc32005-08-09 19:44:15 -0700785 [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp,
Harald Welte37d2e7a2005-11-14 15:24:59 -0800786 .attr_count = NFQA_MAX, },
Harald Welte7af4cc32005-08-09 19:44:15 -0700787 [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700788 .attr_count = NFQA_MAX,
789 .policy = nfqa_verdict_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700790 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700791 .attr_count = NFQA_CFG_MAX,
792 .policy = nfqa_cfg_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700793};
794
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700795static const struct nfnetlink_subsystem nfqnl_subsys = {
Harald Welte7af4cc32005-08-09 19:44:15 -0700796 .name = "nf_queue",
797 .subsys_id = NFNL_SUBSYS_QUEUE,
798 .cb_count = NFQNL_MSG_MAX,
Harald Welte7af4cc32005-08-09 19:44:15 -0700799 .cb = nfqnl_cb,
800};
801
Harald Welte838ab632005-08-09 19:50:45 -0700802#ifdef CONFIG_PROC_FS
803struct iter_state {
804 unsigned int bucket;
805};
806
807static struct hlist_node *get_first(struct seq_file *seq)
808{
809 struct iter_state *st = seq->private;
810
811 if (!st)
812 return NULL;
813
814 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
815 if (!hlist_empty(&instance_table[st->bucket]))
816 return instance_table[st->bucket].first;
817 }
818 return NULL;
819}
820
821static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
822{
823 struct iter_state *st = seq->private;
824
825 h = h->next;
826 while (!h) {
827 if (++st->bucket >= INSTANCE_BUCKETS)
828 return NULL;
829
830 h = instance_table[st->bucket].first;
831 }
832 return h;
833}
834
835static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
836{
837 struct hlist_node *head;
838 head = get_first(seq);
839
840 if (head)
841 while (pos && (head = get_next(seq, head)))
842 pos--;
843 return pos ? NULL : head;
844}
845
846static void *seq_start(struct seq_file *seq, loff_t *pos)
847{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800848 spin_lock(&instances_lock);
Harald Welte838ab632005-08-09 19:50:45 -0700849 return get_idx(seq, *pos);
850}
851
852static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
853{
854 (*pos)++;
855 return get_next(s, v);
856}
857
858static void seq_stop(struct seq_file *s, void *v)
859{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800860 spin_unlock(&instances_lock);
Harald Welte838ab632005-08-09 19:50:45 -0700861}
862
863static int seq_show(struct seq_file *s, void *v)
864{
865 const struct nfqnl_instance *inst = v;
866
867 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
868 inst->queue_num,
869 inst->peer_pid, inst->queue_total,
870 inst->copy_mode, inst->copy_range,
871 inst->queue_dropped, inst->queue_user_dropped,
Patrick McHardy9872bec2007-12-05 01:28:50 -0800872 inst->id_sequence, 1);
Harald Welte838ab632005-08-09 19:50:45 -0700873}
874
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700875static const struct seq_operations nfqnl_seq_ops = {
Harald Welte838ab632005-08-09 19:50:45 -0700876 .start = seq_start,
877 .next = seq_next,
878 .stop = seq_stop,
879 .show = seq_show,
880};
881
882static int nfqnl_open(struct inode *inode, struct file *file)
883{
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700884 return seq_open_private(file, &nfqnl_seq_ops,
885 sizeof(struct iter_state));
Harald Welte838ab632005-08-09 19:50:45 -0700886}
887
Arjan van de Venda7071d2007-02-12 00:55:36 -0800888static const struct file_operations nfqnl_file_ops = {
Harald Welte838ab632005-08-09 19:50:45 -0700889 .owner = THIS_MODULE,
890 .open = nfqnl_open,
891 .read = seq_read,
892 .llseek = seq_lseek,
893 .release = seq_release_private,
894};
895
896#endif /* PROC_FS */
897
Patrick McHardy32292a72006-04-06 14:11:30 -0700898static int __init nfnetlink_queue_init(void)
Harald Welte7af4cc32005-08-09 19:44:15 -0700899{
Harald Welte838ab632005-08-09 19:50:45 -0700900 int i, status = -ENOMEM;
901#ifdef CONFIG_PROC_FS
902 struct proc_dir_entry *proc_nfqueue;
903#endif
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800904
Harald Welte838ab632005-08-09 19:50:45 -0700905 for (i = 0; i < INSTANCE_BUCKETS; i++)
906 INIT_HLIST_HEAD(&instance_table[i]);
907
Harald Welte7af4cc32005-08-09 19:44:15 -0700908 netlink_register_notifier(&nfqnl_rtnl_notifier);
909 status = nfnetlink_subsys_register(&nfqnl_subsys);
910 if (status < 0) {
911 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
912 goto cleanup_netlink_notifier;
913 }
914
Harald Welte838ab632005-08-09 19:50:45 -0700915#ifdef CONFIG_PROC_FS
916 proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440,
917 proc_net_netfilter);
918 if (!proc_nfqueue)
919 goto cleanup_subsys;
920 proc_nfqueue->proc_fops = &nfqnl_file_ops;
921#endif
922
Harald Welte7af4cc32005-08-09 19:44:15 -0700923 register_netdevice_notifier(&nfqnl_dev_notifier);
924 return status;
925
Harald Welte838ab632005-08-09 19:50:45 -0700926#ifdef CONFIG_PROC_FS
927cleanup_subsys:
Harald Welte7af4cc32005-08-09 19:44:15 -0700928 nfnetlink_subsys_unregister(&nfqnl_subsys);
Patrick McHardy32292a72006-04-06 14:11:30 -0700929#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700930cleanup_netlink_notifier:
931 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
932 return status;
933}
934
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800935static void __exit nfnetlink_queue_fini(void)
Harald Welte7af4cc32005-08-09 19:44:15 -0700936{
Patrick McHardy32292a72006-04-06 14:11:30 -0700937 nf_unregister_queue_handlers(&nfqh);
938 unregister_netdevice_notifier(&nfqnl_dev_notifier);
939#ifdef CONFIG_PROC_FS
940 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
941#endif
942 nfnetlink_subsys_unregister(&nfqnl_subsys);
943 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
Harald Welte7af4cc32005-08-09 19:44:15 -0700944}
945
946MODULE_DESCRIPTION("netfilter packet queue handler");
947MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
948MODULE_LICENSE("GPL");
949MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
950
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800951module_init(nfnetlink_queue_init);
952module_exit(nfnetlink_queue_fini);