blob: 8d6bcf32c0ed93ec8903e267e95b8271d98b4b9a [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
Arun Sharma600634972011-07-26 16:09:06 -070034#include <linux/atomic.h>
Harald Welte7af4cc32005-08-09 19:44:15 -070035
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;
Harald Welte7af4cc32005-08-09 19:44:15 -070049 unsigned int queue_dropped;
50 unsigned int queue_user_dropped;
51
Harald Welte7af4cc32005-08-09 19:44:15 -070052
53 u_int16_t queue_num; /* number of this queue */
54 u_int8_t copy_mode;
Eric Dumazetc463ac92010-06-09 18:07:06 +020055/*
56 * Following fields are dirtied for each queued packet,
57 * keep them in same cache line if possible.
58 */
59 spinlock_t lock;
60 unsigned int queue_total;
Eric Dumazet58637022011-07-19 11:44:17 +020061 unsigned int id_sequence; /* 'sequence' of pkt ids */
Harald Welte7af4cc32005-08-09 19:44:15 -070062 struct list_head queue_list; /* packets in queue */
63};
64
Patrick McHardy02f014d2007-12-05 01:26:33 -080065typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
Harald Welte7af4cc32005-08-09 19:44:15 -070066
Patrick McHardy9872bec2007-12-05 01:28:50 -080067static DEFINE_SPINLOCK(instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -070068
Harald Welte7af4cc32005-08-09 19:44:15 -070069#define INSTANCE_BUCKETS 16
Patrick McHardy9d6023a2007-12-05 01:29:38 -080070static struct hlist_head instance_table[INSTANCE_BUCKETS] __read_mostly;
Harald Welte7af4cc32005-08-09 19:44:15 -070071
72static inline u_int8_t instance_hashfn(u_int16_t queue_num)
73{
74 return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
75}
76
77static struct nfqnl_instance *
Patrick McHardy9872bec2007-12-05 01:28:50 -080078instance_lookup(u_int16_t queue_num)
Harald Welte7af4cc32005-08-09 19:44:15 -070079{
80 struct hlist_head *head;
81 struct hlist_node *pos;
82 struct nfqnl_instance *inst;
83
84 head = &instance_table[instance_hashfn(queue_num)];
Patrick McHardy9872bec2007-12-05 01:28:50 -080085 hlist_for_each_entry_rcu(inst, pos, head, hlist) {
Harald Welte7af4cc32005-08-09 19:44:15 -070086 if (inst->queue_num == queue_num)
87 return inst;
88 }
89 return NULL;
90}
91
92static struct nfqnl_instance *
Harald Welte7af4cc32005-08-09 19:44:15 -070093instance_create(u_int16_t queue_num, int pid)
94{
Patrick McHardybaab2ce2007-12-17 22:41:21 -080095 struct nfqnl_instance *inst;
Patrick McHardy9872bec2007-12-05 01:28:50 -080096 unsigned int h;
Patrick McHardybaab2ce2007-12-17 22:41:21 -080097 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -070098
Patrick McHardy9872bec2007-12-05 01:28:50 -080099 spin_lock(&instances_lock);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800100 if (instance_lookup(queue_num)) {
101 err = -EEXIST;
Harald Welte7af4cc32005-08-09 19:44:15 -0700102 goto out_unlock;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800103 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700104
Harald Welte10dfdc62005-11-03 19:20:07 +0100105 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800106 if (!inst) {
107 err = -ENOMEM;
Harald Welte7af4cc32005-08-09 19:44:15 -0700108 goto out_unlock;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800109 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700110
Harald Welte7af4cc32005-08-09 19:44:15 -0700111 inst->queue_num = queue_num;
112 inst->peer_pid = pid;
113 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
114 inst->copy_range = 0xfffff;
115 inst->copy_mode = NFQNL_COPY_NONE;
YOSHIFUJI Hideaki181a46a2006-01-04 13:56:54 -0800116 spin_lock_init(&inst->lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700117 INIT_LIST_HEAD(&inst->queue_list);
118
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800119 if (!try_module_get(THIS_MODULE)) {
120 err = -EAGAIN;
Harald Welte7af4cc32005-08-09 19:44:15 -0700121 goto out_free;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800122 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700123
Patrick McHardy9872bec2007-12-05 01:28:50 -0800124 h = instance_hashfn(queue_num);
125 hlist_add_head_rcu(&inst->hlist, &instance_table[h]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700126
Patrick McHardy9872bec2007-12-05 01:28:50 -0800127 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700128
Harald Welte7af4cc32005-08-09 19:44:15 -0700129 return inst;
130
131out_free:
132 kfree(inst);
133out_unlock:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800134 spin_unlock(&instances_lock);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800135 return ERR_PTR(err);
Harald Welte7af4cc32005-08-09 19:44:15 -0700136}
137
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800138static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
139 unsigned long data);
Harald Welte7af4cc32005-08-09 19:44:15 -0700140
141static void
Patrick McHardy9872bec2007-12-05 01:28:50 -0800142instance_destroy_rcu(struct rcu_head *head)
Harald Welte7af4cc32005-08-09 19:44:15 -0700143{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800144 struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
145 rcu);
Harald Welte7af4cc32005-08-09 19:44:15 -0700146
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800147 nfqnl_flush(inst, NULL, 0);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800148 kfree(inst);
Harald Welte7af4cc32005-08-09 19:44:15 -0700149 module_put(THIS_MODULE);
150}
151
Patrick McHardy9872bec2007-12-05 01:28:50 -0800152static void
Harald Welte7af4cc32005-08-09 19:44:15 -0700153__instance_destroy(struct nfqnl_instance *inst)
154{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800155 hlist_del_rcu(&inst->hlist);
156 call_rcu(&inst->rcu, instance_destroy_rcu);
Harald Welte7af4cc32005-08-09 19:44:15 -0700157}
158
Patrick McHardy9872bec2007-12-05 01:28:50 -0800159static void
Harald Welte7af4cc32005-08-09 19:44:15 -0700160instance_destroy(struct nfqnl_instance *inst)
161{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800162 spin_lock(&instances_lock);
163 __instance_destroy(inst);
164 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700165}
166
Harald Welte7af4cc32005-08-09 19:44:15 -0700167static inline void
Patrick McHardy02f014d2007-12-05 01:26:33 -0800168__enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
Harald Welte7af4cc32005-08-09 19:44:15 -0700169{
Patrick McHardy0ac41e82007-12-05 01:25:03 -0800170 list_add_tail(&entry->list, &queue->queue_list);
Harald Welte7af4cc32005-08-09 19:44:15 -0700171 queue->queue_total++;
172}
173
Florian Westphal97d32cf2011-07-19 11:46:33 +0200174static void
175__dequeue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
176{
177 list_del(&entry->list);
178 queue->queue_total--;
179}
180
Patrick McHardy02f014d2007-12-05 01:26:33 -0800181static struct nf_queue_entry *
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800182find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
Harald Welte7af4cc32005-08-09 19:44:15 -0700183{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800184 struct nf_queue_entry *entry = NULL, *i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800185
Harald Welte7af4cc32005-08-09 19:44:15 -0700186 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800187
188 list_for_each_entry(i, &queue->queue_list, list) {
189 if (i->id == id) {
190 entry = i;
191 break;
192 }
193 }
194
Florian Westphal97d32cf2011-07-19 11:46:33 +0200195 if (entry)
196 __dequeue_entry(queue, entry);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800197
Harald Welte7af4cc32005-08-09 19:44:15 -0700198 spin_unlock_bh(&queue->lock);
199
200 return entry;
201}
202
203static void
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800204nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
Harald Welte7af4cc32005-08-09 19:44:15 -0700205{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800206 struct nf_queue_entry *entry, *next;
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800207
Harald Welte7af4cc32005-08-09 19:44:15 -0700208 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800209 list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
210 if (!cmpfn || cmpfn(entry, data)) {
211 list_del(&entry->list);
212 queue->queue_total--;
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800213 nf_reinject(entry, NF_DROP);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800214 }
215 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700216 spin_unlock_bh(&queue->lock);
217}
218
219static struct sk_buff *
220nfqnl_build_packet_message(struct nfqnl_instance *queue,
Eric Dumazet58637022011-07-19 11:44:17 +0200221 struct nf_queue_entry *entry,
222 __be32 **packet_id_ptr)
Harald Welte7af4cc32005-08-09 19:44:15 -0700223{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700224 sk_buff_data_t old_tail;
Harald Welte7af4cc32005-08-09 19:44:15 -0700225 size_t size;
226 size_t data_len = 0;
227 struct sk_buff *skb;
Eric Dumazet58637022011-07-19 11:44:17 +0200228 struct nlattr *nla;
229 struct nfqnl_msg_packet_hdr *pmsg;
Harald Welte7af4cc32005-08-09 19:44:15 -0700230 struct nlmsghdr *nlh;
231 struct nfgenmsg *nfmsg;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800232 struct sk_buff *entskb = entry->skb;
233 struct net_device *indev;
234 struct net_device *outdev;
Harald Welte7af4cc32005-08-09 19:44:15 -0700235
Eric Leblondcabaa9b2008-03-10 16:41:43 -0700236 size = NLMSG_SPACE(sizeof(struct nfgenmsg))
Patrick McHardydf6fb862007-09-28 14:37:03 -0700237 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
238 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
239 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700240#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700241 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
242 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700243#endif
Patrick McHardydf6fb862007-09-28 14:37:03 -0700244 + nla_total_size(sizeof(u_int32_t)) /* mark */
245 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
246 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
Harald Welte7af4cc32005-08-09 19:44:15 -0700247
Patrick McHardy02f014d2007-12-05 01:26:33 -0800248 outdev = entry->outdev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800249
Eric Dumazetc463ac92010-06-09 18:07:06 +0200250 switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700251 case NFQNL_COPY_META:
252 case NFQNL_COPY_NONE:
Harald Welte7af4cc32005-08-09 19:44:15 -0700253 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800254
Harald Welte7af4cc32005-08-09 19:44:15 -0700255 case NFQNL_COPY_PACKET:
Herbert Xue9f13ca2010-04-08 14:54:35 +0200256 if (entskb->ip_summed == CHECKSUM_PARTIAL &&
Eric Dumazetc463ac92010-06-09 18:07:06 +0200257 skb_checksum_help(entskb))
Patrick McHardye7dfb092005-09-06 15:10:00 -0700258 return NULL;
Eric Dumazetc463ac92010-06-09 18:07:06 +0200259
260 data_len = ACCESS_ONCE(queue->copy_range);
261 if (data_len == 0 || data_len > entskb->len)
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800262 data_len = entskb->len;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800263
Patrick McHardydf6fb862007-09-28 14:37:03 -0700264 size += nla_total_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700265 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700266 }
267
Harald Welte7af4cc32005-08-09 19:44:15 -0700268
269 skb = alloc_skb(size, GFP_ATOMIC);
270 if (!skb)
271 goto nlmsg_failure;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800272
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700273 old_tail = skb->tail;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800274 nlh = NLMSG_PUT(skb, 0, 0,
Harald Welte7af4cc32005-08-09 19:44:15 -0700275 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
276 sizeof(struct nfgenmsg));
277 nfmsg = NLMSG_DATA(nlh);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800278 nfmsg->nfgen_family = entry->pf;
Harald Welte7af4cc32005-08-09 19:44:15 -0700279 nfmsg->version = NFNETLINK_V0;
280 nfmsg->res_id = htons(queue->queue_num);
281
Eric Dumazet58637022011-07-19 11:44:17 +0200282 nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg));
283 pmsg = nla_data(nla);
284 pmsg->hw_protocol = entskb->protocol;
285 pmsg->hook = entry->hook;
286 *packet_id_ptr = &pmsg->packet_id;
Harald Welte7af4cc32005-08-09 19:44:15 -0700287
Patrick McHardy02f014d2007-12-05 01:26:33 -0800288 indev = entry->indev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800289 if (indev) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700290#ifndef CONFIG_BRIDGE_NETFILTER
David S. Millera4471892012-03-29 23:27:38 -0400291 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex)))
292 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700293#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800294 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700295 /* Case 1: indev is physical input device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800296 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700297 * netfilter_bridge) */
David S. Millera4471892012-03-29 23:27:38 -0400298 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
299 htonl(indev->ifindex)) ||
Harald Weltefbcd9232005-08-09 20:22:10 -0700300 /* this is the bridge group "brX" */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000301 /* rcu_read_lock()ed by __nf_queue */
David S. Millera4471892012-03-29 23:27:38 -0400302 nla_put_be32(skb, NFQA_IFINDEX_INDEV,
303 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
304 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700305 } else {
306 /* Case 2: indev is bridge group, we need to look for
307 * physical device (when called from ipv4) */
David S. Millera4471892012-03-29 23:27:38 -0400308 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV,
309 htonl(indev->ifindex)))
310 goto nla_put_failure;
311 if (entskb->nf_bridge && entskb->nf_bridge->physindev &&
312 nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
313 htonl(entskb->nf_bridge->physindev->ifindex)))
314 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700315 }
316#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700317 }
318
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800319 if (outdev) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700320#ifndef CONFIG_BRIDGE_NETFILTER
David S. Millera4471892012-03-29 23:27:38 -0400321 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex)))
322 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700323#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800324 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700325 /* Case 1: outdev is physical output device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800326 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700327 * netfilter_bridge) */
David S. Millera4471892012-03-29 23:27:38 -0400328 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
329 htonl(outdev->ifindex)) ||
Harald Weltefbcd9232005-08-09 20:22:10 -0700330 /* this is the bridge group "brX" */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000331 /* rcu_read_lock()ed by __nf_queue */
David S. Millera4471892012-03-29 23:27:38 -0400332 nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
333 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
334 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700335 } else {
336 /* Case 2: outdev is bridge group, we need to look for
337 * physical output device (when called from ipv4) */
David S. Millera4471892012-03-29 23:27:38 -0400338 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
339 htonl(outdev->ifindex)))
340 goto nla_put_failure;
341 if (entskb->nf_bridge && entskb->nf_bridge->physoutdev &&
342 nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
343 htonl(entskb->nf_bridge->physoutdev->ifindex)))
344 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700345 }
346#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700347 }
348
David S. Millera4471892012-03-29 23:27:38 -0400349 if (entskb->mark &&
350 nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark)))
351 goto nla_put_failure;
Harald Welte7af4cc32005-08-09 19:44:15 -0700352
Nicolas Cavallari2c38de42011-06-16 17:27:04 +0200353 if (indev && entskb->dev &&
354 entskb->mac_header != entskb->network_header) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700355 struct nfqnl_msg_packet_hw phw;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700356 int len = dev_parse_header(entskb, phw.hw_addr);
357 if (len) {
358 phw.hw_addrlen = htons(len);
David S. Millera4471892012-03-29 23:27:38 -0400359 if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw))
360 goto nla_put_failure;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700361 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700362 }
363
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700364 if (entskb->tstamp.tv64) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700365 struct nfqnl_msg_packet_timestamp ts;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700366 struct timeval tv = ktime_to_timeval(entskb->tstamp);
367 ts.sec = cpu_to_be64(tv.tv_sec);
368 ts.usec = cpu_to_be64(tv.tv_usec);
Harald Welte7af4cc32005-08-09 19:44:15 -0700369
David S. Millera4471892012-03-29 23:27:38 -0400370 if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts))
371 goto nla_put_failure;
Harald Welte7af4cc32005-08-09 19:44:15 -0700372 }
373
374 if (data_len) {
Patrick McHardydf6fb862007-09-28 14:37:03 -0700375 struct nlattr *nla;
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800376 int sz = nla_attr_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700377
Patrick McHardydf6fb862007-09-28 14:37:03 -0700378 if (skb_tailroom(skb) < nla_total_size(data_len)) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700379 printk(KERN_WARNING "nf_queue: no tailroom!\n");
380 goto nlmsg_failure;
381 }
382
Patrick McHardydf6fb862007-09-28 14:37:03 -0700383 nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));
384 nla->nla_type = NFQA_PAYLOAD;
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800385 nla->nla_len = sz;
Harald Welte7af4cc32005-08-09 19:44:15 -0700386
Patrick McHardydf6fb862007-09-28 14:37:03 -0700387 if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700388 BUG();
389 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800390
Harald Welte7af4cc32005-08-09 19:44:15 -0700391 nlh->nlmsg_len = skb->tail - old_tail;
392 return skb;
393
394nlmsg_failure:
Patrick McHardydf6fb862007-09-28 14:37:03 -0700395nla_put_failure:
Harald Welte7af4cc32005-08-09 19:44:15 -0700396 if (skb)
397 kfree_skb(skb);
Harald Welte7af4cc32005-08-09 19:44:15 -0700398 if (net_ratelimit())
399 printk(KERN_ERR "nf_queue: error creating packet message\n");
400 return NULL;
401}
402
403static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800404nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
Harald Welte7af4cc32005-08-09 19:44:15 -0700405{
Harald Welte7af4cc32005-08-09 19:44:15 -0700406 struct sk_buff *nskb;
407 struct nfqnl_instance *queue;
Florian Westphalf1585082011-01-18 15:27:28 +0100408 int err = -ENOBUFS;
Eric Dumazet58637022011-07-19 11:44:17 +0200409 __be32 *packet_id_ptr;
Harald Welte7af4cc32005-08-09 19:44:15 -0700410
Patrick McHardy9872bec2007-12-05 01:28:50 -0800411 /* rcu_read_lock()ed by nf_hook_slow() */
412 queue = instance_lookup(queuenum);
Florian Westphalf1585082011-01-18 15:27:28 +0100413 if (!queue) {
414 err = -ESRCH;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800415 goto err_out;
Florian Westphalf1585082011-01-18 15:27:28 +0100416 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700417
Florian Westphalf1585082011-01-18 15:27:28 +0100418 if (queue->copy_mode == NFQNL_COPY_NONE) {
419 err = -EINVAL;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800420 goto err_out;
Florian Westphalf1585082011-01-18 15:27:28 +0100421 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700422
Eric Dumazet58637022011-07-19 11:44:17 +0200423 nskb = nfqnl_build_packet_message(queue, entry, &packet_id_ptr);
Florian Westphalf1585082011-01-18 15:27:28 +0100424 if (nskb == NULL) {
425 err = -ENOMEM;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800426 goto err_out;
Florian Westphalf1585082011-01-18 15:27:28 +0100427 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700428 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800429
Florian Westphalf1585082011-01-18 15:27:28 +0100430 if (!queue->peer_pid) {
431 err = -EINVAL;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800432 goto err_out_free_nskb;
Florian Westphalf1585082011-01-18 15:27:28 +0100433 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700434 if (queue->queue_total >= queue->queue_maxlen) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800435 queue->queue_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700436 if (net_ratelimit())
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800437 printk(KERN_WARNING "nf_queue: full at %d entries, "
Eric Leblonda5d896ad2010-01-18 09:44:39 +0100438 "dropping packets(s).\n",
439 queue->queue_total);
Harald Welte7af4cc32005-08-09 19:44:15 -0700440 goto err_out_free_nskb;
441 }
Eric Dumazet58637022011-07-19 11:44:17 +0200442 entry->id = ++queue->id_sequence;
443 *packet_id_ptr = htonl(entry->id);
Harald Welte7af4cc32005-08-09 19:44:15 -0700444
445 /* nfnetlink_unicast will either free the nskb or add it to a socket */
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100446 err = nfnetlink_unicast(nskb, &init_net, queue->peer_pid, MSG_DONTWAIT);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800447 if (err < 0) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800448 queue->queue_user_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700449 goto err_out_unlock;
450 }
451
452 __enqueue_entry(queue, entry);
453
454 spin_unlock_bh(&queue->lock);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800455 return 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700456
457err_out_free_nskb:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800458 kfree_skb(nskb);
Harald Welte7af4cc32005-08-09 19:44:15 -0700459err_out_unlock:
460 spin_unlock_bh(&queue->lock);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800461err_out:
Florian Westphalf1585082011-01-18 15:27:28 +0100462 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700463}
464
465static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800466nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e)
Harald Welte7af4cc32005-08-09 19:44:15 -0700467{
Patrick McHardye2b58a62008-02-19 17:17:52 -0800468 struct sk_buff *nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700469 int diff;
470
471 diff = data_len - e->skb->len;
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800472 if (diff < 0) {
473 if (pskb_trim(e->skb, data_len))
474 return -ENOMEM;
475 } else if (diff > 0) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700476 if (data_len > 0xFFFF)
477 return -EINVAL;
478 if (diff > skb_tailroom(e->skb)) {
Arnaud Ebalard9a732ed2008-04-29 03:16:34 -0700479 nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
480 diff, GFP_ATOMIC);
Patrick McHardye2b58a62008-02-19 17:17:52 -0800481 if (!nskb) {
Patrick McHardy1158ba22006-08-22 00:32:47 -0700482 printk(KERN_WARNING "nf_queue: OOM "
Harald Welte7af4cc32005-08-09 19:44:15 -0700483 "in mangle, dropping packet\n");
Patrick McHardye2b58a62008-02-19 17:17:52 -0800484 return -ENOMEM;
Harald Welte7af4cc32005-08-09 19:44:15 -0700485 }
Patrick McHardye2b58a62008-02-19 17:17:52 -0800486 kfree_skb(e->skb);
487 e->skb = nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700488 }
489 skb_put(e->skb, diff);
490 }
Herbert Xu37d41872007-10-14 00:39:18 -0700491 if (!skb_make_writable(e->skb, data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700492 return -ENOMEM;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300493 skb_copy_to_linear_data(e->skb, data, data_len);
Patrick McHardye7dfb092005-09-06 15:10:00 -0700494 e->skb->ip_summed = CHECKSUM_NONE;
Harald Welte7af4cc32005-08-09 19:44:15 -0700495 return 0;
496}
497
Harald Welte7af4cc32005-08-09 19:44:15 -0700498static int
499nfqnl_set_mode(struct nfqnl_instance *queue,
500 unsigned char mode, unsigned int range)
501{
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800502 int status = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700503
504 spin_lock_bh(&queue->lock);
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800505 switch (mode) {
506 case NFQNL_COPY_NONE:
507 case NFQNL_COPY_META:
508 queue->copy_mode = mode;
509 queue->copy_range = 0;
510 break;
511
512 case NFQNL_COPY_PACKET:
513 queue->copy_mode = mode;
514 /* we're using struct nlattr which has 16bit nla_len */
515 if (range > 0xffff)
516 queue->copy_range = 0xffff;
517 else
518 queue->copy_range = range;
519 break;
520
521 default:
522 status = -EINVAL;
523
524 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700525 spin_unlock_bh(&queue->lock);
526
527 return status;
528}
529
530static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800531dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700532{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800533 if (entry->indev)
534 if (entry->indev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700535 return 1;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800536 if (entry->outdev)
537 if (entry->outdev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700538 return 1;
Patrick McHardyef47c6a72006-06-27 03:01:48 -0700539#ifdef CONFIG_BRIDGE_NETFILTER
540 if (entry->skb->nf_bridge) {
541 if (entry->skb->nf_bridge->physindev &&
542 entry->skb->nf_bridge->physindev->ifindex == ifindex)
543 return 1;
544 if (entry->skb->nf_bridge->physoutdev &&
545 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
546 return 1;
547 }
548#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700549 return 0;
550}
551
552/* drop all packets with either indev or outdev == ifindex from all queue
553 * instances */
554static void
555nfqnl_dev_drop(int ifindex)
556{
557 int i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800558
Patrick McHardy9872bec2007-12-05 01:28:50 -0800559 rcu_read_lock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700560
Patrick McHardy9872bec2007-12-05 01:28:50 -0800561 for (i = 0; i < INSTANCE_BUCKETS; i++) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700562 struct hlist_node *tmp;
563 struct nfqnl_instance *inst;
564 struct hlist_head *head = &instance_table[i];
565
Patrick McHardy9872bec2007-12-05 01:28:50 -0800566 hlist_for_each_entry_rcu(inst, tmp, head, hlist)
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800567 nfqnl_flush(inst, dev_cmp, ifindex);
Harald Welte7af4cc32005-08-09 19:44:15 -0700568 }
569
Patrick McHardy9872bec2007-12-05 01:28:50 -0800570 rcu_read_unlock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700571}
572
573#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
574
575static int
576nfqnl_rcv_dev_event(struct notifier_block *this,
577 unsigned long event, void *ptr)
578{
579 struct net_device *dev = ptr;
580
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700581 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200582 return NOTIFY_DONE;
583
Harald Welte7af4cc32005-08-09 19:44:15 -0700584 /* Drop any packets associated with the downed device */
585 if (event == NETDEV_DOWN)
586 nfqnl_dev_drop(dev->ifindex);
587 return NOTIFY_DONE;
588}
589
590static struct notifier_block nfqnl_dev_notifier = {
591 .notifier_call = nfqnl_rcv_dev_event,
592};
593
594static int
595nfqnl_rcv_nl_event(struct notifier_block *this,
596 unsigned long event, void *ptr)
597{
598 struct netlink_notify *n = ptr;
599
Patrick McHardydee58172009-11-06 17:04:00 +0100600 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700601 int i;
602
603 /* destroy all instances for this pid */
Patrick McHardy9872bec2007-12-05 01:28:50 -0800604 spin_lock(&instances_lock);
605 for (i = 0; i < INSTANCE_BUCKETS; i++) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700606 struct hlist_node *tmp, *t2;
607 struct nfqnl_instance *inst;
608 struct hlist_head *head = &instance_table[i];
609
610 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200611 if ((n->net == &init_net) &&
612 (n->pid == inst->peer_pid))
Harald Welte7af4cc32005-08-09 19:44:15 -0700613 __instance_destroy(inst);
614 }
615 }
Patrick McHardy9872bec2007-12-05 01:28:50 -0800616 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700617 }
618 return NOTIFY_DONE;
619}
620
621static struct notifier_block nfqnl_rtnl_notifier = {
622 .notifier_call = nfqnl_rcv_nl_event,
623};
624
Patrick McHardy5bf75852007-09-28 14:39:26 -0700625static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
626 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
627 [NFQA_MARK] = { .type = NLA_U32 },
628 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
Harald Welte838ab632005-08-09 19:50:45 -0700629};
630
Florian Westphal97d32cf2011-07-19 11:46:33 +0200631static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = {
632 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
633 [NFQA_MARK] = { .type = NLA_U32 },
634};
635
636static struct nfqnl_instance *verdict_instance_lookup(u16 queue_num, int nlpid)
637{
638 struct nfqnl_instance *queue;
639
640 queue = instance_lookup(queue_num);
641 if (!queue)
642 return ERR_PTR(-ENODEV);
643
644 if (queue->peer_pid != nlpid)
645 return ERR_PTR(-EPERM);
646
647 return queue;
648}
649
650static struct nfqnl_msg_verdict_hdr*
651verdicthdr_get(const struct nlattr * const nfqa[])
652{
653 struct nfqnl_msg_verdict_hdr *vhdr;
654 unsigned int verdict;
655
656 if (!nfqa[NFQA_VERDICT_HDR])
657 return NULL;
658
659 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
Florian Westphalc6675232011-08-30 15:01:20 +0200660 verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK;
661 if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN)
Florian Westphal97d32cf2011-07-19 11:46:33 +0200662 return NULL;
663 return vhdr;
664}
665
666static int nfq_id_after(unsigned int id, unsigned int max)
667{
668 return (int)(id - max) > 0;
669}
670
671static int
672nfqnl_recv_verdict_batch(struct sock *ctnl, struct sk_buff *skb,
673 const struct nlmsghdr *nlh,
674 const struct nlattr * const nfqa[])
675{
676 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
677 struct nf_queue_entry *entry, *tmp;
678 unsigned int verdict, maxid;
679 struct nfqnl_msg_verdict_hdr *vhdr;
680 struct nfqnl_instance *queue;
681 LIST_HEAD(batch_list);
682 u16 queue_num = ntohs(nfmsg->res_id);
683
684 queue = verdict_instance_lookup(queue_num, NETLINK_CB(skb).pid);
685 if (IS_ERR(queue))
686 return PTR_ERR(queue);
687
688 vhdr = verdicthdr_get(nfqa);
689 if (!vhdr)
690 return -EINVAL;
691
692 verdict = ntohl(vhdr->verdict);
693 maxid = ntohl(vhdr->id);
694
695 spin_lock_bh(&queue->lock);
696
697 list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) {
698 if (nfq_id_after(entry->id, maxid))
699 break;
700 __dequeue_entry(queue, entry);
701 list_add_tail(&entry->list, &batch_list);
702 }
703
704 spin_unlock_bh(&queue->lock);
705
706 if (list_empty(&batch_list))
707 return -ENOENT;
708
709 list_for_each_entry_safe(entry, tmp, &batch_list, list) {
710 if (nfqa[NFQA_MARK])
711 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
712 nf_reinject(entry, verdict);
713 }
714 return 0;
715}
716
Harald Welte7af4cc32005-08-09 19:44:15 -0700717static int
718nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200719 const struct nlmsghdr *nlh,
720 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700721{
722 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
723 u_int16_t queue_num = ntohs(nfmsg->res_id);
724
725 struct nfqnl_msg_verdict_hdr *vhdr;
726 struct nfqnl_instance *queue;
727 unsigned int verdict;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800728 struct nf_queue_entry *entry;
Harald Welte7af4cc32005-08-09 19:44:15 -0700729
Patrick McHardy9872bec2007-12-05 01:28:50 -0800730 queue = instance_lookup(queue_num);
Eric Dumazet84a797d2011-07-18 16:08:27 +0200731 if (!queue)
Harald Welte7af4cc32005-08-09 19:44:15 -0700732
Florian Westphal97d32cf2011-07-19 11:46:33 +0200733 queue = verdict_instance_lookup(queue_num, NETLINK_CB(skb).pid);
734 if (IS_ERR(queue))
735 return PTR_ERR(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700736
Florian Westphal97d32cf2011-07-19 11:46:33 +0200737 vhdr = verdicthdr_get(nfqa);
738 if (!vhdr)
Eric Dumazet84a797d2011-07-18 16:08:27 +0200739 return -EINVAL;
Harald Welte7af4cc32005-08-09 19:44:15 -0700740
Harald Welte7af4cc32005-08-09 19:44:15 -0700741 verdict = ntohl(vhdr->verdict);
742
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800743 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
Eric Dumazet84a797d2011-07-18 16:08:27 +0200744 if (entry == NULL)
745 return -ENOENT;
Harald Welte7af4cc32005-08-09 19:44:15 -0700746
Patrick McHardydf6fb862007-09-28 14:37:03 -0700747 if (nfqa[NFQA_PAYLOAD]) {
748 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
749 nla_len(nfqa[NFQA_PAYLOAD]), entry) < 0)
Harald Welte7af4cc32005-08-09 19:44:15 -0700750 verdict = NF_DROP;
751 }
752
Patrick McHardydf6fb862007-09-28 14:37:03 -0700753 if (nfqa[NFQA_MARK])
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800754 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800755
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800756 nf_reinject(entry, verdict);
Harald Welte7af4cc32005-08-09 19:44:15 -0700757 return 0;
758}
759
760static int
761nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200762 const struct nlmsghdr *nlh,
763 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700764{
765 return -ENOTSUPP;
766}
767
Patrick McHardy5bf75852007-09-28 14:39:26 -0700768static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
769 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
770 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
Harald Welte838ab632005-08-09 19:50:45 -0700771};
772
Patrick McHardye3ac5292007-12-05 01:23:57 -0800773static const struct nf_queue_handler nfqh = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700774 .name = "nf_queue",
775 .outfn = &nfqnl_enqueue_packet,
776};
777
Harald Welte7af4cc32005-08-09 19:44:15 -0700778static int
779nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200780 const struct nlmsghdr *nlh,
781 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700782{
783 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
784 u_int16_t queue_num = ntohs(nfmsg->res_id);
785 struct nfqnl_instance *queue;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800786 struct nfqnl_msg_config_cmd *cmd = NULL;
Harald Welte838ab632005-08-09 19:50:45 -0700787 int ret = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700788
Patrick McHardy9872bec2007-12-05 01:28:50 -0800789 if (nfqa[NFQA_CFG_CMD]) {
790 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
791
792 /* Commands without queue context - might sleep */
793 switch (cmd->command) {
794 case NFQNL_CFG_CMD_PF_BIND:
Patrick McHardy914afea2008-03-10 16:44:36 -0700795 return nf_register_queue_handler(ntohs(cmd->pf),
796 &nfqh);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800797 case NFQNL_CFG_CMD_PF_UNBIND:
Patrick McHardy914afea2008-03-10 16:44:36 -0700798 return nf_unregister_queue_handler(ntohs(cmd->pf),
799 &nfqh);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800800 }
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800801 }
802
Patrick McHardy9872bec2007-12-05 01:28:50 -0800803 rcu_read_lock();
804 queue = instance_lookup(queue_num);
805 if (queue && queue->peer_pid != NETLINK_CB(skb).pid) {
806 ret = -EPERM;
807 goto err_out_unlock;
808 }
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800809
Patrick McHardy9872bec2007-12-05 01:28:50 -0800810 if (cmd != NULL) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700811 switch (cmd->command) {
812 case NFQNL_CFG_CMD_BIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800813 if (queue) {
814 ret = -EBUSY;
815 goto err_out_unlock;
816 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700817 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800818 if (IS_ERR(queue)) {
819 ret = PTR_ERR(queue);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800820 goto err_out_unlock;
821 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700822 break;
823 case NFQNL_CFG_CMD_UNBIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800824 if (!queue) {
825 ret = -ENODEV;
826 goto err_out_unlock;
827 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700828 instance_destroy(queue);
829 break;
830 case NFQNL_CFG_CMD_PF_BIND:
Harald Welte7af4cc32005-08-09 19:44:15 -0700831 case NFQNL_CFG_CMD_PF_UNBIND:
Harald Welte7af4cc32005-08-09 19:44:15 -0700832 break;
833 default:
Patrick McHardycd21f0a2007-12-17 22:40:19 -0800834 ret = -ENOTSUPP;
Harald Welte838ab632005-08-09 19:50:45 -0700835 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700836 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700837 }
838
Patrick McHardydf6fb862007-09-28 14:37:03 -0700839 if (nfqa[NFQA_CFG_PARAMS]) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700840 struct nfqnl_msg_config_params *params;
Harald Welte7af4cc32005-08-09 19:44:15 -0700841
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800842 if (!queue) {
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800843 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800844 goto err_out_unlock;
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800845 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700846 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700847 nfqnl_set_mode(queue, params->copy_mode,
848 ntohl(params->copy_range));
849 }
850
Patrick McHardydf6fb862007-09-28 14:37:03 -0700851 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
Eric Leblond829e17a2006-11-29 02:35:33 +0100852 __be32 *queue_maxlen;
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800853
854 if (!queue) {
855 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800856 goto err_out_unlock;
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800857 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700858 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
Eric Leblond829e17a2006-11-29 02:35:33 +0100859 spin_lock_bh(&queue->lock);
860 queue->queue_maxlen = ntohl(*queue_maxlen);
861 spin_unlock_bh(&queue->lock);
862 }
863
Patrick McHardy9872bec2007-12-05 01:28:50 -0800864err_out_unlock:
865 rcu_read_unlock();
Harald Welte838ab632005-08-09 19:50:45 -0700866 return ret;
Harald Welte7af4cc32005-08-09 19:44:15 -0700867}
868
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700869static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
Eric Dumazet84a797d2011-07-18 16:08:27 +0200870 [NFQNL_MSG_PACKET] = { .call_rcu = nfqnl_recv_unsupp,
Harald Welte37d2e7a2005-11-14 15:24:59 -0800871 .attr_count = NFQA_MAX, },
Eric Dumazet84a797d2011-07-18 16:08:27 +0200872 [NFQNL_MSG_VERDICT] = { .call_rcu = nfqnl_recv_verdict,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700873 .attr_count = NFQA_MAX,
874 .policy = nfqa_verdict_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700875 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700876 .attr_count = NFQA_CFG_MAX,
877 .policy = nfqa_cfg_policy },
Florian Westphal97d32cf2011-07-19 11:46:33 +0200878 [NFQNL_MSG_VERDICT_BATCH]={ .call_rcu = nfqnl_recv_verdict_batch,
879 .attr_count = NFQA_MAX,
880 .policy = nfqa_verdict_batch_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700881};
882
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700883static const struct nfnetlink_subsystem nfqnl_subsys = {
Harald Welte7af4cc32005-08-09 19:44:15 -0700884 .name = "nf_queue",
885 .subsys_id = NFNL_SUBSYS_QUEUE,
886 .cb_count = NFQNL_MSG_MAX,
Harald Welte7af4cc32005-08-09 19:44:15 -0700887 .cb = nfqnl_cb,
888};
889
Harald Welte838ab632005-08-09 19:50:45 -0700890#ifdef CONFIG_PROC_FS
891struct iter_state {
892 unsigned int bucket;
893};
894
895static struct hlist_node *get_first(struct seq_file *seq)
896{
897 struct iter_state *st = seq->private;
898
899 if (!st)
900 return NULL;
901
902 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
903 if (!hlist_empty(&instance_table[st->bucket]))
904 return instance_table[st->bucket].first;
905 }
906 return NULL;
907}
908
909static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
910{
911 struct iter_state *st = seq->private;
912
913 h = h->next;
914 while (!h) {
915 if (++st->bucket >= INSTANCE_BUCKETS)
916 return NULL;
917
918 h = instance_table[st->bucket].first;
919 }
920 return h;
921}
922
923static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
924{
925 struct hlist_node *head;
926 head = get_first(seq);
927
928 if (head)
929 while (pos && (head = get_next(seq, head)))
930 pos--;
931 return pos ? NULL : head;
932}
933
934static void *seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800935 __acquires(instances_lock)
Harald Welte838ab632005-08-09 19:50:45 -0700936{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800937 spin_lock(&instances_lock);
Harald Welte838ab632005-08-09 19:50:45 -0700938 return get_idx(seq, *pos);
939}
940
941static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
942{
943 (*pos)++;
944 return get_next(s, v);
945}
946
947static void seq_stop(struct seq_file *s, void *v)
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800948 __releases(instances_lock)
Harald Welte838ab632005-08-09 19:50:45 -0700949{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800950 spin_unlock(&instances_lock);
Harald Welte838ab632005-08-09 19:50:45 -0700951}
952
953static int seq_show(struct seq_file *s, void *v)
954{
955 const struct nfqnl_instance *inst = v;
956
957 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
958 inst->queue_num,
959 inst->peer_pid, inst->queue_total,
960 inst->copy_mode, inst->copy_range,
961 inst->queue_dropped, inst->queue_user_dropped,
Eric Dumazet58637022011-07-19 11:44:17 +0200962 inst->id_sequence, 1);
Harald Welte838ab632005-08-09 19:50:45 -0700963}
964
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700965static const struct seq_operations nfqnl_seq_ops = {
Harald Welte838ab632005-08-09 19:50:45 -0700966 .start = seq_start,
967 .next = seq_next,
968 .stop = seq_stop,
969 .show = seq_show,
970};
971
972static int nfqnl_open(struct inode *inode, struct file *file)
973{
Pavel Emelyanove2da5912007-10-10 02:29:58 -0700974 return seq_open_private(file, &nfqnl_seq_ops,
975 sizeof(struct iter_state));
Harald Welte838ab632005-08-09 19:50:45 -0700976}
977
Arjan van de Venda7071d2007-02-12 00:55:36 -0800978static const struct file_operations nfqnl_file_ops = {
Harald Welte838ab632005-08-09 19:50:45 -0700979 .owner = THIS_MODULE,
980 .open = nfqnl_open,
981 .read = seq_read,
982 .llseek = seq_lseek,
983 .release = seq_release_private,
984};
985
986#endif /* PROC_FS */
987
Patrick McHardy32292a72006-04-06 14:11:30 -0700988static int __init nfnetlink_queue_init(void)
Harald Welte7af4cc32005-08-09 19:44:15 -0700989{
Harald Welte838ab632005-08-09 19:50:45 -0700990 int i, status = -ENOMEM;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800991
Harald Welte838ab632005-08-09 19:50:45 -0700992 for (i = 0; i < INSTANCE_BUCKETS; i++)
993 INIT_HLIST_HEAD(&instance_table[i]);
994
Harald Welte7af4cc32005-08-09 19:44:15 -0700995 netlink_register_notifier(&nfqnl_rtnl_notifier);
996 status = nfnetlink_subsys_register(&nfqnl_subsys);
997 if (status < 0) {
998 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
999 goto cleanup_netlink_notifier;
1000 }
1001
Harald Welte838ab632005-08-09 19:50:45 -07001002#ifdef CONFIG_PROC_FS
Denis V. Lunev8eeee8b2008-03-27 16:55:53 -07001003 if (!proc_create("nfnetlink_queue", 0440,
1004 proc_net_netfilter, &nfqnl_file_ops))
Harald Welte838ab632005-08-09 19:50:45 -07001005 goto cleanup_subsys;
Harald Welte838ab632005-08-09 19:50:45 -07001006#endif
1007
Harald Welte7af4cc32005-08-09 19:44:15 -07001008 register_netdevice_notifier(&nfqnl_dev_notifier);
1009 return status;
1010
Harald Welte838ab632005-08-09 19:50:45 -07001011#ifdef CONFIG_PROC_FS
1012cleanup_subsys:
Harald Welte7af4cc32005-08-09 19:44:15 -07001013 nfnetlink_subsys_unregister(&nfqnl_subsys);
Patrick McHardy32292a72006-04-06 14:11:30 -07001014#endif
Harald Welte7af4cc32005-08-09 19:44:15 -07001015cleanup_netlink_notifier:
1016 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1017 return status;
1018}
1019
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001020static void __exit nfnetlink_queue_fini(void)
Harald Welte7af4cc32005-08-09 19:44:15 -07001021{
Patrick McHardy32292a72006-04-06 14:11:30 -07001022 nf_unregister_queue_handlers(&nfqh);
1023 unregister_netdevice_notifier(&nfqnl_dev_notifier);
1024#ifdef CONFIG_PROC_FS
1025 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
1026#endif
1027 nfnetlink_subsys_unregister(&nfqnl_subsys);
1028 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
Jesper Dangaard Brouer67137f32009-06-08 03:11:33 +00001029
1030 rcu_barrier(); /* Wait for completion of call_rcu()'s */
Harald Welte7af4cc32005-08-09 19:44:15 -07001031}
1032
1033MODULE_DESCRIPTION("netfilter packet queue handler");
1034MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1035MODULE_LICENSE("GPL");
1036MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1037
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001038module_init(nfnetlink_queue_init);
1039module_exit(nfnetlink_queue_fini);