blob: 630da3d2c62a5473894c03d52896a27bc35b49ea [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;
Krishna Kumarfdb694a2012-05-24 03:56:44 +000055 u_int32_t flags; /* Set using NFQA_CFG_FLAGS */
Eric Dumazetc463ac92010-06-09 18:07:06 +020056/*
57 * Following fields are dirtied for each queued packet,
58 * keep them in same cache line if possible.
59 */
60 spinlock_t lock;
61 unsigned int queue_total;
Eric Dumazet58637022011-07-19 11:44:17 +020062 unsigned int id_sequence; /* 'sequence' of pkt ids */
Harald Welte7af4cc32005-08-09 19:44:15 -070063 struct list_head queue_list; /* packets in queue */
64};
65
Patrick McHardy02f014d2007-12-05 01:26:33 -080066typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
Harald Welte7af4cc32005-08-09 19:44:15 -070067
Patrick McHardy9872bec2007-12-05 01:28:50 -080068static DEFINE_SPINLOCK(instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -070069
Harald Welte7af4cc32005-08-09 19:44:15 -070070#define INSTANCE_BUCKETS 16
Patrick McHardy9d6023a2007-12-05 01:29:38 -080071static struct hlist_head instance_table[INSTANCE_BUCKETS] __read_mostly;
Harald Welte7af4cc32005-08-09 19:44:15 -070072
73static inline u_int8_t instance_hashfn(u_int16_t queue_num)
74{
75 return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
76}
77
78static struct nfqnl_instance *
Patrick McHardy9872bec2007-12-05 01:28:50 -080079instance_lookup(u_int16_t queue_num)
Harald Welte7af4cc32005-08-09 19:44:15 -070080{
81 struct hlist_head *head;
82 struct hlist_node *pos;
83 struct nfqnl_instance *inst;
84
85 head = &instance_table[instance_hashfn(queue_num)];
Patrick McHardy9872bec2007-12-05 01:28:50 -080086 hlist_for_each_entry_rcu(inst, pos, head, hlist) {
Harald Welte7af4cc32005-08-09 19:44:15 -070087 if (inst->queue_num == queue_num)
88 return inst;
89 }
90 return NULL;
91}
92
93static struct nfqnl_instance *
Harald Welte7af4cc32005-08-09 19:44:15 -070094instance_create(u_int16_t queue_num, int pid)
95{
Patrick McHardybaab2ce2007-12-17 22:41:21 -080096 struct nfqnl_instance *inst;
Patrick McHardy9872bec2007-12-05 01:28:50 -080097 unsigned int h;
Patrick McHardybaab2ce2007-12-17 22:41:21 -080098 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -070099
Patrick McHardy9872bec2007-12-05 01:28:50 -0800100 spin_lock(&instances_lock);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800101 if (instance_lookup(queue_num)) {
102 err = -EEXIST;
Harald Welte7af4cc32005-08-09 19:44:15 -0700103 goto out_unlock;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800104 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700105
Harald Welte10dfdc62005-11-03 19:20:07 +0100106 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800107 if (!inst) {
108 err = -ENOMEM;
Harald Welte7af4cc32005-08-09 19:44:15 -0700109 goto out_unlock;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800110 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700111
Harald Welte7af4cc32005-08-09 19:44:15 -0700112 inst->queue_num = queue_num;
113 inst->peer_pid = pid;
114 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
115 inst->copy_range = 0xfffff;
116 inst->copy_mode = NFQNL_COPY_NONE;
YOSHIFUJI Hideaki181a46a2006-01-04 13:56:54 -0800117 spin_lock_init(&inst->lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700118 INIT_LIST_HEAD(&inst->queue_list);
119
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800120 if (!try_module_get(THIS_MODULE)) {
121 err = -EAGAIN;
Harald Welte7af4cc32005-08-09 19:44:15 -0700122 goto out_free;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800123 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700124
Patrick McHardy9872bec2007-12-05 01:28:50 -0800125 h = instance_hashfn(queue_num);
126 hlist_add_head_rcu(&inst->hlist, &instance_table[h]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700127
Patrick McHardy9872bec2007-12-05 01:28:50 -0800128 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700129
Harald Welte7af4cc32005-08-09 19:44:15 -0700130 return inst;
131
132out_free:
133 kfree(inst);
134out_unlock:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800135 spin_unlock(&instances_lock);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800136 return ERR_PTR(err);
Harald Welte7af4cc32005-08-09 19:44:15 -0700137}
138
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800139static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
140 unsigned long data);
Harald Welte7af4cc32005-08-09 19:44:15 -0700141
142static void
Patrick McHardy9872bec2007-12-05 01:28:50 -0800143instance_destroy_rcu(struct rcu_head *head)
Harald Welte7af4cc32005-08-09 19:44:15 -0700144{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800145 struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
146 rcu);
Harald Welte7af4cc32005-08-09 19:44:15 -0700147
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800148 nfqnl_flush(inst, NULL, 0);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800149 kfree(inst);
Harald Welte7af4cc32005-08-09 19:44:15 -0700150 module_put(THIS_MODULE);
151}
152
Patrick McHardy9872bec2007-12-05 01:28:50 -0800153static void
Harald Welte7af4cc32005-08-09 19:44:15 -0700154__instance_destroy(struct nfqnl_instance *inst)
155{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800156 hlist_del_rcu(&inst->hlist);
157 call_rcu(&inst->rcu, instance_destroy_rcu);
Harald Welte7af4cc32005-08-09 19:44:15 -0700158}
159
Patrick McHardy9872bec2007-12-05 01:28:50 -0800160static void
Harald Welte7af4cc32005-08-09 19:44:15 -0700161instance_destroy(struct nfqnl_instance *inst)
162{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800163 spin_lock(&instances_lock);
164 __instance_destroy(inst);
165 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700166}
167
Harald Welte7af4cc32005-08-09 19:44:15 -0700168static inline void
Patrick McHardy02f014d2007-12-05 01:26:33 -0800169__enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
Harald Welte7af4cc32005-08-09 19:44:15 -0700170{
Patrick McHardy0ac41e82007-12-05 01:25:03 -0800171 list_add_tail(&entry->list, &queue->queue_list);
Harald Welte7af4cc32005-08-09 19:44:15 -0700172 queue->queue_total++;
173}
174
Florian Westphal97d32cf2011-07-19 11:46:33 +0200175static void
176__dequeue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
177{
178 list_del(&entry->list);
179 queue->queue_total--;
180}
181
Patrick McHardy02f014d2007-12-05 01:26:33 -0800182static struct nf_queue_entry *
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800183find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
Harald Welte7af4cc32005-08-09 19:44:15 -0700184{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800185 struct nf_queue_entry *entry = NULL, *i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800186
Harald Welte7af4cc32005-08-09 19:44:15 -0700187 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800188
189 list_for_each_entry(i, &queue->queue_list, list) {
190 if (i->id == id) {
191 entry = i;
192 break;
193 }
194 }
195
Florian Westphal97d32cf2011-07-19 11:46:33 +0200196 if (entry)
197 __dequeue_entry(queue, entry);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800198
Harald Welte7af4cc32005-08-09 19:44:15 -0700199 spin_unlock_bh(&queue->lock);
200
201 return entry;
202}
203
204static void
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800205nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
Harald Welte7af4cc32005-08-09 19:44:15 -0700206{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800207 struct nf_queue_entry *entry, *next;
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800208
Harald Welte7af4cc32005-08-09 19:44:15 -0700209 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800210 list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
211 if (!cmpfn || cmpfn(entry, data)) {
212 list_del(&entry->list);
213 queue->queue_total--;
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800214 nf_reinject(entry, NF_DROP);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800215 }
216 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700217 spin_unlock_bh(&queue->lock);
218}
219
220static struct sk_buff *
221nfqnl_build_packet_message(struct nfqnl_instance *queue,
Eric Dumazet58637022011-07-19 11:44:17 +0200222 struct nf_queue_entry *entry,
223 __be32 **packet_id_ptr)
Harald Welte7af4cc32005-08-09 19:44:15 -0700224{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700225 sk_buff_data_t old_tail;
Harald Welte7af4cc32005-08-09 19:44:15 -0700226 size_t size;
227 size_t data_len = 0;
228 struct sk_buff *skb;
Eric Dumazet58637022011-07-19 11:44:17 +0200229 struct nlattr *nla;
230 struct nfqnl_msg_packet_hdr *pmsg;
Harald Welte7af4cc32005-08-09 19:44:15 -0700231 struct nlmsghdr *nlh;
232 struct nfgenmsg *nfmsg;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800233 struct sk_buff *entskb = entry->skb;
234 struct net_device *indev;
235 struct net_device *outdev;
Harald Welte7af4cc32005-08-09 19:44:15 -0700236
Eric Leblondcabaa9b2008-03-10 16:41:43 -0700237 size = NLMSG_SPACE(sizeof(struct nfgenmsg))
Patrick McHardydf6fb862007-09-28 14:37:03 -0700238 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
239 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
240 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700241#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700242 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
243 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700244#endif
Patrick McHardydf6fb862007-09-28 14:37:03 -0700245 + nla_total_size(sizeof(u_int32_t)) /* mark */
246 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
247 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
Harald Welte7af4cc32005-08-09 19:44:15 -0700248
Patrick McHardy02f014d2007-12-05 01:26:33 -0800249 outdev = entry->outdev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800250
Eric Dumazetc463ac92010-06-09 18:07:06 +0200251 switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700252 case NFQNL_COPY_META:
253 case NFQNL_COPY_NONE:
Harald Welte7af4cc32005-08-09 19:44:15 -0700254 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800255
Harald Welte7af4cc32005-08-09 19:44:15 -0700256 case NFQNL_COPY_PACKET:
Herbert Xue9f13ca2010-04-08 14:54:35 +0200257 if (entskb->ip_summed == CHECKSUM_PARTIAL &&
Eric Dumazetc463ac92010-06-09 18:07:06 +0200258 skb_checksum_help(entskb))
Patrick McHardye7dfb092005-09-06 15:10:00 -0700259 return NULL;
Eric Dumazetc463ac92010-06-09 18:07:06 +0200260
261 data_len = ACCESS_ONCE(queue->copy_range);
262 if (data_len == 0 || data_len > entskb->len)
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800263 data_len = entskb->len;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800264
Patrick McHardydf6fb862007-09-28 14:37:03 -0700265 size += nla_total_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700266 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700267 }
268
Harald Welte7af4cc32005-08-09 19:44:15 -0700269
270 skb = alloc_skb(size, GFP_ATOMIC);
271 if (!skb)
272 goto nlmsg_failure;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800273
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700274 old_tail = skb->tail;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800275 nlh = NLMSG_PUT(skb, 0, 0,
Harald Welte7af4cc32005-08-09 19:44:15 -0700276 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
277 sizeof(struct nfgenmsg));
278 nfmsg = NLMSG_DATA(nlh);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800279 nfmsg->nfgen_family = entry->pf;
Harald Welte7af4cc32005-08-09 19:44:15 -0700280 nfmsg->version = NFNETLINK_V0;
281 nfmsg->res_id = htons(queue->queue_num);
282
Eric Dumazet58637022011-07-19 11:44:17 +0200283 nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg));
284 pmsg = nla_data(nla);
285 pmsg->hw_protocol = entskb->protocol;
286 pmsg->hook = entry->hook;
287 *packet_id_ptr = &pmsg->packet_id;
Harald Welte7af4cc32005-08-09 19:44:15 -0700288
Patrick McHardy02f014d2007-12-05 01:26:33 -0800289 indev = entry->indev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800290 if (indev) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700291#ifndef CONFIG_BRIDGE_NETFILTER
David S. Millera4471892012-03-29 23:27:38 -0400292 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex)))
293 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700294#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800295 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700296 /* Case 1: indev is physical input device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800297 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700298 * netfilter_bridge) */
David S. Millera4471892012-03-29 23:27:38 -0400299 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
300 htonl(indev->ifindex)) ||
Harald Weltefbcd9232005-08-09 20:22:10 -0700301 /* this is the bridge group "brX" */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000302 /* rcu_read_lock()ed by __nf_queue */
David S. Millera4471892012-03-29 23:27:38 -0400303 nla_put_be32(skb, NFQA_IFINDEX_INDEV,
304 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
305 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700306 } else {
307 /* Case 2: indev is bridge group, we need to look for
308 * physical device (when called from ipv4) */
David S. Millera4471892012-03-29 23:27:38 -0400309 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV,
310 htonl(indev->ifindex)))
311 goto nla_put_failure;
312 if (entskb->nf_bridge && entskb->nf_bridge->physindev &&
313 nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
314 htonl(entskb->nf_bridge->physindev->ifindex)))
315 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700316 }
317#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700318 }
319
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800320 if (outdev) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700321#ifndef CONFIG_BRIDGE_NETFILTER
David S. Millera4471892012-03-29 23:27:38 -0400322 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex)))
323 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700324#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800325 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700326 /* Case 1: outdev is physical output device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800327 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700328 * netfilter_bridge) */
David S. Millera4471892012-03-29 23:27:38 -0400329 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
330 htonl(outdev->ifindex)) ||
Harald Weltefbcd9232005-08-09 20:22:10 -0700331 /* this is the bridge group "brX" */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000332 /* rcu_read_lock()ed by __nf_queue */
David S. Millera4471892012-03-29 23:27:38 -0400333 nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
334 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
335 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700336 } else {
337 /* Case 2: outdev is bridge group, we need to look for
338 * physical output device (when called from ipv4) */
David S. Millera4471892012-03-29 23:27:38 -0400339 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
340 htonl(outdev->ifindex)))
341 goto nla_put_failure;
342 if (entskb->nf_bridge && entskb->nf_bridge->physoutdev &&
343 nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
344 htonl(entskb->nf_bridge->physoutdev->ifindex)))
345 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700346 }
347#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700348 }
349
David S. Millera4471892012-03-29 23:27:38 -0400350 if (entskb->mark &&
351 nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark)))
352 goto nla_put_failure;
Harald Welte7af4cc32005-08-09 19:44:15 -0700353
Nicolas Cavallari2c38de42011-06-16 17:27:04 +0200354 if (indev && entskb->dev &&
355 entskb->mac_header != entskb->network_header) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700356 struct nfqnl_msg_packet_hw phw;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700357 int len = dev_parse_header(entskb, phw.hw_addr);
358 if (len) {
359 phw.hw_addrlen = htons(len);
David S. Millera4471892012-03-29 23:27:38 -0400360 if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw))
361 goto nla_put_failure;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700362 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700363 }
364
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700365 if (entskb->tstamp.tv64) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700366 struct nfqnl_msg_packet_timestamp ts;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700367 struct timeval tv = ktime_to_timeval(entskb->tstamp);
368 ts.sec = cpu_to_be64(tv.tv_sec);
369 ts.usec = cpu_to_be64(tv.tv_usec);
Harald Welte7af4cc32005-08-09 19:44:15 -0700370
David S. Millera4471892012-03-29 23:27:38 -0400371 if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts))
372 goto nla_put_failure;
Harald Welte7af4cc32005-08-09 19:44:15 -0700373 }
374
375 if (data_len) {
Patrick McHardydf6fb862007-09-28 14:37:03 -0700376 struct nlattr *nla;
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800377 int sz = nla_attr_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700378
Patrick McHardydf6fb862007-09-28 14:37:03 -0700379 if (skb_tailroom(skb) < nla_total_size(data_len)) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700380 printk(KERN_WARNING "nf_queue: no tailroom!\n");
381 goto nlmsg_failure;
382 }
383
Patrick McHardydf6fb862007-09-28 14:37:03 -0700384 nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));
385 nla->nla_type = NFQA_PAYLOAD;
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800386 nla->nla_len = sz;
Harald Welte7af4cc32005-08-09 19:44:15 -0700387
Patrick McHardydf6fb862007-09-28 14:37:03 -0700388 if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700389 BUG();
390 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800391
Harald Welte7af4cc32005-08-09 19:44:15 -0700392 nlh->nlmsg_len = skb->tail - old_tail;
393 return skb;
394
395nlmsg_failure:
Patrick McHardydf6fb862007-09-28 14:37:03 -0700396nla_put_failure:
Harald Welte7af4cc32005-08-09 19:44:15 -0700397 if (skb)
398 kfree_skb(skb);
Joe Perchese87cc472012-05-13 21:56:26 +0000399 net_err_ratelimited("nf_queue: error creating packet message\n");
Harald Welte7af4cc32005-08-09 19:44:15 -0700400 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;
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000410 int failopen = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700411
Patrick McHardy9872bec2007-12-05 01:28:50 -0800412 /* rcu_read_lock()ed by nf_hook_slow() */
413 queue = instance_lookup(queuenum);
Florian Westphalf1585082011-01-18 15:27:28 +0100414 if (!queue) {
415 err = -ESRCH;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800416 goto err_out;
Florian Westphalf1585082011-01-18 15:27:28 +0100417 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700418
Florian Westphalf1585082011-01-18 15:27:28 +0100419 if (queue->copy_mode == NFQNL_COPY_NONE) {
420 err = -EINVAL;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800421 goto err_out;
Florian Westphalf1585082011-01-18 15:27:28 +0100422 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700423
Eric Dumazet58637022011-07-19 11:44:17 +0200424 nskb = nfqnl_build_packet_message(queue, entry, &packet_id_ptr);
Florian Westphalf1585082011-01-18 15:27:28 +0100425 if (nskb == NULL) {
426 err = -ENOMEM;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800427 goto err_out;
Florian Westphalf1585082011-01-18 15:27:28 +0100428 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700429 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800430
Florian Westphalf1585082011-01-18 15:27:28 +0100431 if (!queue->peer_pid) {
432 err = -EINVAL;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800433 goto err_out_free_nskb;
Florian Westphalf1585082011-01-18 15:27:28 +0100434 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700435 if (queue->queue_total >= queue->queue_maxlen) {
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000436 if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
437 failopen = 1;
438 err = 0;
439 } else {
440 queue->queue_dropped++;
441 net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n",
442 queue->queue_total);
443 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700444 goto err_out_free_nskb;
445 }
Eric Dumazet58637022011-07-19 11:44:17 +0200446 entry->id = ++queue->id_sequence;
447 *packet_id_ptr = htonl(entry->id);
Harald Welte7af4cc32005-08-09 19:44:15 -0700448
449 /* nfnetlink_unicast will either free the nskb or add it to a socket */
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100450 err = nfnetlink_unicast(nskb, &init_net, queue->peer_pid, MSG_DONTWAIT);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800451 if (err < 0) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800452 queue->queue_user_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700453 goto err_out_unlock;
454 }
455
456 __enqueue_entry(queue, entry);
457
458 spin_unlock_bh(&queue->lock);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800459 return 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700460
461err_out_free_nskb:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800462 kfree_skb(nskb);
Harald Welte7af4cc32005-08-09 19:44:15 -0700463err_out_unlock:
464 spin_unlock_bh(&queue->lock);
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000465 if (failopen)
466 nf_reinject(entry, NF_ACCEPT);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800467err_out:
Florian Westphalf1585082011-01-18 15:27:28 +0100468 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700469}
470
471static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800472nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e)
Harald Welte7af4cc32005-08-09 19:44:15 -0700473{
Patrick McHardye2b58a62008-02-19 17:17:52 -0800474 struct sk_buff *nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700475 int diff;
476
477 diff = data_len - e->skb->len;
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800478 if (diff < 0) {
479 if (pskb_trim(e->skb, data_len))
480 return -ENOMEM;
481 } else if (diff > 0) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700482 if (data_len > 0xFFFF)
483 return -EINVAL;
484 if (diff > skb_tailroom(e->skb)) {
Arnaud Ebalard9a732ed2008-04-29 03:16:34 -0700485 nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
486 diff, GFP_ATOMIC);
Patrick McHardye2b58a62008-02-19 17:17:52 -0800487 if (!nskb) {
Patrick McHardy1158ba22006-08-22 00:32:47 -0700488 printk(KERN_WARNING "nf_queue: OOM "
Harald Welte7af4cc32005-08-09 19:44:15 -0700489 "in mangle, dropping packet\n");
Patrick McHardye2b58a62008-02-19 17:17:52 -0800490 return -ENOMEM;
Harald Welte7af4cc32005-08-09 19:44:15 -0700491 }
Patrick McHardye2b58a62008-02-19 17:17:52 -0800492 kfree_skb(e->skb);
493 e->skb = nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700494 }
495 skb_put(e->skb, diff);
496 }
Herbert Xu37d41872007-10-14 00:39:18 -0700497 if (!skb_make_writable(e->skb, data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700498 return -ENOMEM;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300499 skb_copy_to_linear_data(e->skb, data, data_len);
Patrick McHardye7dfb092005-09-06 15:10:00 -0700500 e->skb->ip_summed = CHECKSUM_NONE;
Harald Welte7af4cc32005-08-09 19:44:15 -0700501 return 0;
502}
503
Harald Welte7af4cc32005-08-09 19:44:15 -0700504static int
505nfqnl_set_mode(struct nfqnl_instance *queue,
506 unsigned char mode, unsigned int range)
507{
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800508 int status = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700509
510 spin_lock_bh(&queue->lock);
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800511 switch (mode) {
512 case NFQNL_COPY_NONE:
513 case NFQNL_COPY_META:
514 queue->copy_mode = mode;
515 queue->copy_range = 0;
516 break;
517
518 case NFQNL_COPY_PACKET:
519 queue->copy_mode = mode;
520 /* we're using struct nlattr which has 16bit nla_len */
521 if (range > 0xffff)
522 queue->copy_range = 0xffff;
523 else
524 queue->copy_range = range;
525 break;
526
527 default:
528 status = -EINVAL;
529
530 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700531 spin_unlock_bh(&queue->lock);
532
533 return status;
534}
535
536static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800537dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700538{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800539 if (entry->indev)
540 if (entry->indev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700541 return 1;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800542 if (entry->outdev)
543 if (entry->outdev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700544 return 1;
Patrick McHardyef47c6a2006-06-27 03:01:48 -0700545#ifdef CONFIG_BRIDGE_NETFILTER
546 if (entry->skb->nf_bridge) {
547 if (entry->skb->nf_bridge->physindev &&
548 entry->skb->nf_bridge->physindev->ifindex == ifindex)
549 return 1;
550 if (entry->skb->nf_bridge->physoutdev &&
551 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
552 return 1;
553 }
554#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700555 return 0;
556}
557
558/* drop all packets with either indev or outdev == ifindex from all queue
559 * instances */
560static void
561nfqnl_dev_drop(int ifindex)
562{
563 int i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800564
Patrick McHardy9872bec2007-12-05 01:28:50 -0800565 rcu_read_lock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700566
Patrick McHardy9872bec2007-12-05 01:28:50 -0800567 for (i = 0; i < INSTANCE_BUCKETS; i++) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700568 struct hlist_node *tmp;
569 struct nfqnl_instance *inst;
570 struct hlist_head *head = &instance_table[i];
571
Patrick McHardy9872bec2007-12-05 01:28:50 -0800572 hlist_for_each_entry_rcu(inst, tmp, head, hlist)
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800573 nfqnl_flush(inst, dev_cmp, ifindex);
Harald Welte7af4cc32005-08-09 19:44:15 -0700574 }
575
Patrick McHardy9872bec2007-12-05 01:28:50 -0800576 rcu_read_unlock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700577}
578
579#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
580
581static int
582nfqnl_rcv_dev_event(struct notifier_block *this,
583 unsigned long event, void *ptr)
584{
585 struct net_device *dev = ptr;
586
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700587 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200588 return NOTIFY_DONE;
589
Harald Welte7af4cc32005-08-09 19:44:15 -0700590 /* Drop any packets associated with the downed device */
591 if (event == NETDEV_DOWN)
592 nfqnl_dev_drop(dev->ifindex);
593 return NOTIFY_DONE;
594}
595
596static struct notifier_block nfqnl_dev_notifier = {
597 .notifier_call = nfqnl_rcv_dev_event,
598};
599
600static int
601nfqnl_rcv_nl_event(struct notifier_block *this,
602 unsigned long event, void *ptr)
603{
604 struct netlink_notify *n = ptr;
605
Patrick McHardydee58172009-11-06 17:04:00 +0100606 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700607 int i;
608
609 /* destroy all instances for this pid */
Patrick McHardy9872bec2007-12-05 01:28:50 -0800610 spin_lock(&instances_lock);
611 for (i = 0; i < INSTANCE_BUCKETS; i++) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700612 struct hlist_node *tmp, *t2;
613 struct nfqnl_instance *inst;
614 struct hlist_head *head = &instance_table[i];
615
616 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200617 if ((n->net == &init_net) &&
618 (n->pid == inst->peer_pid))
Harald Welte7af4cc32005-08-09 19:44:15 -0700619 __instance_destroy(inst);
620 }
621 }
Patrick McHardy9872bec2007-12-05 01:28:50 -0800622 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700623 }
624 return NOTIFY_DONE;
625}
626
627static struct notifier_block nfqnl_rtnl_notifier = {
628 .notifier_call = nfqnl_rcv_nl_event,
629};
630
Patrick McHardy5bf75852007-09-28 14:39:26 -0700631static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
632 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
633 [NFQA_MARK] = { .type = NLA_U32 },
634 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
Harald Welte838ab632005-08-09 19:50:45 -0700635};
636
Florian Westphal97d32cf2011-07-19 11:46:33 +0200637static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = {
638 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
639 [NFQA_MARK] = { .type = NLA_U32 },
640};
641
642static struct nfqnl_instance *verdict_instance_lookup(u16 queue_num, int nlpid)
643{
644 struct nfqnl_instance *queue;
645
646 queue = instance_lookup(queue_num);
647 if (!queue)
648 return ERR_PTR(-ENODEV);
649
650 if (queue->peer_pid != nlpid)
651 return ERR_PTR(-EPERM);
652
653 return queue;
654}
655
656static struct nfqnl_msg_verdict_hdr*
657verdicthdr_get(const struct nlattr * const nfqa[])
658{
659 struct nfqnl_msg_verdict_hdr *vhdr;
660 unsigned int verdict;
661
662 if (!nfqa[NFQA_VERDICT_HDR])
663 return NULL;
664
665 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
Florian Westphalc6675232011-08-30 15:01:20 +0200666 verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK;
667 if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN)
Florian Westphal97d32cf2011-07-19 11:46:33 +0200668 return NULL;
669 return vhdr;
670}
671
672static int nfq_id_after(unsigned int id, unsigned int max)
673{
674 return (int)(id - max) > 0;
675}
676
677static int
678nfqnl_recv_verdict_batch(struct sock *ctnl, struct sk_buff *skb,
679 const struct nlmsghdr *nlh,
680 const struct nlattr * const nfqa[])
681{
682 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
683 struct nf_queue_entry *entry, *tmp;
684 unsigned int verdict, maxid;
685 struct nfqnl_msg_verdict_hdr *vhdr;
686 struct nfqnl_instance *queue;
687 LIST_HEAD(batch_list);
688 u16 queue_num = ntohs(nfmsg->res_id);
689
690 queue = verdict_instance_lookup(queue_num, NETLINK_CB(skb).pid);
691 if (IS_ERR(queue))
692 return PTR_ERR(queue);
693
694 vhdr = verdicthdr_get(nfqa);
695 if (!vhdr)
696 return -EINVAL;
697
698 verdict = ntohl(vhdr->verdict);
699 maxid = ntohl(vhdr->id);
700
701 spin_lock_bh(&queue->lock);
702
703 list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) {
704 if (nfq_id_after(entry->id, maxid))
705 break;
706 __dequeue_entry(queue, entry);
707 list_add_tail(&entry->list, &batch_list);
708 }
709
710 spin_unlock_bh(&queue->lock);
711
712 if (list_empty(&batch_list))
713 return -ENOENT;
714
715 list_for_each_entry_safe(entry, tmp, &batch_list, list) {
716 if (nfqa[NFQA_MARK])
717 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
718 nf_reinject(entry, verdict);
719 }
720 return 0;
721}
722
Harald Welte7af4cc32005-08-09 19:44:15 -0700723static int
724nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200725 const struct nlmsghdr *nlh,
726 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700727{
728 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
729 u_int16_t queue_num = ntohs(nfmsg->res_id);
730
731 struct nfqnl_msg_verdict_hdr *vhdr;
732 struct nfqnl_instance *queue;
733 unsigned int verdict;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800734 struct nf_queue_entry *entry;
Harald Welte7af4cc32005-08-09 19:44:15 -0700735
Patrick McHardy9872bec2007-12-05 01:28:50 -0800736 queue = instance_lookup(queue_num);
Eric Dumazet84a797d2011-07-18 16:08:27 +0200737 if (!queue)
Harald Welte7af4cc32005-08-09 19:44:15 -0700738
Florian Westphal97d32cf2011-07-19 11:46:33 +0200739 queue = verdict_instance_lookup(queue_num, NETLINK_CB(skb).pid);
740 if (IS_ERR(queue))
741 return PTR_ERR(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700742
Florian Westphal97d32cf2011-07-19 11:46:33 +0200743 vhdr = verdicthdr_get(nfqa);
744 if (!vhdr)
Eric Dumazet84a797d2011-07-18 16:08:27 +0200745 return -EINVAL;
Harald Welte7af4cc32005-08-09 19:44:15 -0700746
Harald Welte7af4cc32005-08-09 19:44:15 -0700747 verdict = ntohl(vhdr->verdict);
748
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800749 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
Eric Dumazet84a797d2011-07-18 16:08:27 +0200750 if (entry == NULL)
751 return -ENOENT;
Harald Welte7af4cc32005-08-09 19:44:15 -0700752
Patrick McHardydf6fb862007-09-28 14:37:03 -0700753 if (nfqa[NFQA_PAYLOAD]) {
754 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
755 nla_len(nfqa[NFQA_PAYLOAD]), entry) < 0)
Harald Welte7af4cc32005-08-09 19:44:15 -0700756 verdict = NF_DROP;
757 }
758
Patrick McHardydf6fb862007-09-28 14:37:03 -0700759 if (nfqa[NFQA_MARK])
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800760 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800761
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800762 nf_reinject(entry, verdict);
Harald Welte7af4cc32005-08-09 19:44:15 -0700763 return 0;
764}
765
766static int
767nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200768 const struct nlmsghdr *nlh,
769 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700770{
771 return -ENOTSUPP;
772}
773
Patrick McHardy5bf75852007-09-28 14:39:26 -0700774static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
775 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
776 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
Harald Welte838ab632005-08-09 19:50:45 -0700777};
778
Patrick McHardye3ac5292007-12-05 01:23:57 -0800779static const struct nf_queue_handler nfqh = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700780 .name = "nf_queue",
781 .outfn = &nfqnl_enqueue_packet,
782};
783
Harald Welte7af4cc32005-08-09 19:44:15 -0700784static int
785nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200786 const struct nlmsghdr *nlh,
787 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700788{
789 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
790 u_int16_t queue_num = ntohs(nfmsg->res_id);
791 struct nfqnl_instance *queue;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800792 struct nfqnl_msg_config_cmd *cmd = NULL;
Harald Welte838ab632005-08-09 19:50:45 -0700793 int ret = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700794
Patrick McHardy9872bec2007-12-05 01:28:50 -0800795 if (nfqa[NFQA_CFG_CMD]) {
796 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
797
798 /* Commands without queue context - might sleep */
799 switch (cmd->command) {
800 case NFQNL_CFG_CMD_PF_BIND:
Patrick McHardy914afea2008-03-10 16:44:36 -0700801 return nf_register_queue_handler(ntohs(cmd->pf),
802 &nfqh);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800803 case NFQNL_CFG_CMD_PF_UNBIND:
Patrick McHardy914afea2008-03-10 16:44:36 -0700804 return nf_unregister_queue_handler(ntohs(cmd->pf),
805 &nfqh);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800806 }
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800807 }
808
Patrick McHardy9872bec2007-12-05 01:28:50 -0800809 rcu_read_lock();
810 queue = instance_lookup(queue_num);
811 if (queue && queue->peer_pid != NETLINK_CB(skb).pid) {
812 ret = -EPERM;
813 goto err_out_unlock;
814 }
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800815
Patrick McHardy9872bec2007-12-05 01:28:50 -0800816 if (cmd != NULL) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700817 switch (cmd->command) {
818 case NFQNL_CFG_CMD_BIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800819 if (queue) {
820 ret = -EBUSY;
821 goto err_out_unlock;
822 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700823 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800824 if (IS_ERR(queue)) {
825 ret = PTR_ERR(queue);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800826 goto err_out_unlock;
827 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700828 break;
829 case NFQNL_CFG_CMD_UNBIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800830 if (!queue) {
831 ret = -ENODEV;
832 goto err_out_unlock;
833 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700834 instance_destroy(queue);
835 break;
836 case NFQNL_CFG_CMD_PF_BIND:
Harald Welte7af4cc32005-08-09 19:44:15 -0700837 case NFQNL_CFG_CMD_PF_UNBIND:
Harald Welte7af4cc32005-08-09 19:44:15 -0700838 break;
839 default:
Patrick McHardycd21f0a2007-12-17 22:40:19 -0800840 ret = -ENOTSUPP;
Harald Welte838ab632005-08-09 19:50:45 -0700841 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700842 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700843 }
844
Patrick McHardydf6fb862007-09-28 14:37:03 -0700845 if (nfqa[NFQA_CFG_PARAMS]) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700846 struct nfqnl_msg_config_params *params;
Harald Welte7af4cc32005-08-09 19:44:15 -0700847
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800848 if (!queue) {
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800849 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800850 goto err_out_unlock;
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800851 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700852 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700853 nfqnl_set_mode(queue, params->copy_mode,
854 ntohl(params->copy_range));
855 }
856
Patrick McHardydf6fb862007-09-28 14:37:03 -0700857 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
Eric Leblond829e17a2006-11-29 02:35:33 +0100858 __be32 *queue_maxlen;
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800859
860 if (!queue) {
861 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800862 goto err_out_unlock;
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800863 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700864 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
Eric Leblond829e17a2006-11-29 02:35:33 +0100865 spin_lock_bh(&queue->lock);
866 queue->queue_maxlen = ntohl(*queue_maxlen);
867 spin_unlock_bh(&queue->lock);
868 }
869
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000870 if (nfqa[NFQA_CFG_FLAGS]) {
871 __u32 flags, mask;
872
873 if (!queue) {
874 ret = -ENODEV;
875 goto err_out_unlock;
876 }
877
878 if (!nfqa[NFQA_CFG_MASK]) {
879 /* A mask is needed to specify which flags are being
880 * changed.
881 */
882 ret = -EINVAL;
883 goto err_out_unlock;
884 }
885
886 flags = ntohl(nla_get_be32(nfqa[NFQA_CFG_FLAGS]));
887 mask = ntohl(nla_get_be32(nfqa[NFQA_CFG_MASK]));
888
889 spin_lock_bh(&queue->lock);
890 queue->flags &= ~mask;
891 queue->flags |= flags & mask;
892 spin_unlock_bh(&queue->lock);
893 }
894
Patrick McHardy9872bec2007-12-05 01:28:50 -0800895err_out_unlock:
896 rcu_read_unlock();
Harald Welte838ab632005-08-09 19:50:45 -0700897 return ret;
Harald Welte7af4cc32005-08-09 19:44:15 -0700898}
899
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700900static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
Eric Dumazet84a797d2011-07-18 16:08:27 +0200901 [NFQNL_MSG_PACKET] = { .call_rcu = nfqnl_recv_unsupp,
Harald Welte37d2e7a2005-11-14 15:24:59 -0800902 .attr_count = NFQA_MAX, },
Eric Dumazet84a797d2011-07-18 16:08:27 +0200903 [NFQNL_MSG_VERDICT] = { .call_rcu = nfqnl_recv_verdict,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700904 .attr_count = NFQA_MAX,
905 .policy = nfqa_verdict_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700906 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700907 .attr_count = NFQA_CFG_MAX,
908 .policy = nfqa_cfg_policy },
Florian Westphal97d32cf2011-07-19 11:46:33 +0200909 [NFQNL_MSG_VERDICT_BATCH]={ .call_rcu = nfqnl_recv_verdict_batch,
910 .attr_count = NFQA_MAX,
911 .policy = nfqa_verdict_batch_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700912};
913
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700914static const struct nfnetlink_subsystem nfqnl_subsys = {
Harald Welte7af4cc32005-08-09 19:44:15 -0700915 .name = "nf_queue",
916 .subsys_id = NFNL_SUBSYS_QUEUE,
917 .cb_count = NFQNL_MSG_MAX,
Harald Welte7af4cc32005-08-09 19:44:15 -0700918 .cb = nfqnl_cb,
919};
920
Harald Welte838ab632005-08-09 19:50:45 -0700921#ifdef CONFIG_PROC_FS
922struct iter_state {
923 unsigned int bucket;
924};
925
926static struct hlist_node *get_first(struct seq_file *seq)
927{
928 struct iter_state *st = seq->private;
929
930 if (!st)
931 return NULL;
932
933 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
934 if (!hlist_empty(&instance_table[st->bucket]))
935 return instance_table[st->bucket].first;
936 }
937 return NULL;
938}
939
940static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
941{
942 struct iter_state *st = seq->private;
943
944 h = h->next;
945 while (!h) {
946 if (++st->bucket >= INSTANCE_BUCKETS)
947 return NULL;
948
949 h = instance_table[st->bucket].first;
950 }
951 return h;
952}
953
954static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
955{
956 struct hlist_node *head;
957 head = get_first(seq);
958
959 if (head)
960 while (pos && (head = get_next(seq, head)))
961 pos--;
962 return pos ? NULL : head;
963}
964
965static void *seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800966 __acquires(instances_lock)
Harald Welte838ab632005-08-09 19:50:45 -0700967{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800968 spin_lock(&instances_lock);
Harald Welte838ab632005-08-09 19:50:45 -0700969 return get_idx(seq, *pos);
970}
971
972static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
973{
974 (*pos)++;
975 return get_next(s, v);
976}
977
978static void seq_stop(struct seq_file *s, void *v)
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800979 __releases(instances_lock)
Harald Welte838ab632005-08-09 19:50:45 -0700980{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800981 spin_unlock(&instances_lock);
Harald Welte838ab632005-08-09 19:50:45 -0700982}
983
984static int seq_show(struct seq_file *s, void *v)
985{
986 const struct nfqnl_instance *inst = v;
987
988 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
989 inst->queue_num,
990 inst->peer_pid, inst->queue_total,
991 inst->copy_mode, inst->copy_range,
992 inst->queue_dropped, inst->queue_user_dropped,
Eric Dumazet58637022011-07-19 11:44:17 +0200993 inst->id_sequence, 1);
Harald Welte838ab632005-08-09 19:50:45 -0700994}
995
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700996static const struct seq_operations nfqnl_seq_ops = {
Harald Welte838ab632005-08-09 19:50:45 -0700997 .start = seq_start,
998 .next = seq_next,
999 .stop = seq_stop,
1000 .show = seq_show,
1001};
1002
1003static int nfqnl_open(struct inode *inode, struct file *file)
1004{
Pavel Emelyanove2da5912007-10-10 02:29:58 -07001005 return seq_open_private(file, &nfqnl_seq_ops,
1006 sizeof(struct iter_state));
Harald Welte838ab632005-08-09 19:50:45 -07001007}
1008
Arjan van de Venda7071d2007-02-12 00:55:36 -08001009static const struct file_operations nfqnl_file_ops = {
Harald Welte838ab632005-08-09 19:50:45 -07001010 .owner = THIS_MODULE,
1011 .open = nfqnl_open,
1012 .read = seq_read,
1013 .llseek = seq_lseek,
1014 .release = seq_release_private,
1015};
1016
1017#endif /* PROC_FS */
1018
Patrick McHardy32292a72006-04-06 14:11:30 -07001019static int __init nfnetlink_queue_init(void)
Harald Welte7af4cc32005-08-09 19:44:15 -07001020{
Harald Welte838ab632005-08-09 19:50:45 -07001021 int i, status = -ENOMEM;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001022
Harald Welte838ab632005-08-09 19:50:45 -07001023 for (i = 0; i < INSTANCE_BUCKETS; i++)
1024 INIT_HLIST_HEAD(&instance_table[i]);
1025
Harald Welte7af4cc32005-08-09 19:44:15 -07001026 netlink_register_notifier(&nfqnl_rtnl_notifier);
1027 status = nfnetlink_subsys_register(&nfqnl_subsys);
1028 if (status < 0) {
1029 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
1030 goto cleanup_netlink_notifier;
1031 }
1032
Harald Welte838ab632005-08-09 19:50:45 -07001033#ifdef CONFIG_PROC_FS
Denis V. Lunev8eeee8b2008-03-27 16:55:53 -07001034 if (!proc_create("nfnetlink_queue", 0440,
1035 proc_net_netfilter, &nfqnl_file_ops))
Harald Welte838ab632005-08-09 19:50:45 -07001036 goto cleanup_subsys;
Harald Welte838ab632005-08-09 19:50:45 -07001037#endif
1038
Harald Welte7af4cc32005-08-09 19:44:15 -07001039 register_netdevice_notifier(&nfqnl_dev_notifier);
1040 return status;
1041
Harald Welte838ab632005-08-09 19:50:45 -07001042#ifdef CONFIG_PROC_FS
1043cleanup_subsys:
Harald Welte7af4cc32005-08-09 19:44:15 -07001044 nfnetlink_subsys_unregister(&nfqnl_subsys);
Patrick McHardy32292a72006-04-06 14:11:30 -07001045#endif
Harald Welte7af4cc32005-08-09 19:44:15 -07001046cleanup_netlink_notifier:
1047 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1048 return status;
1049}
1050
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001051static void __exit nfnetlink_queue_fini(void)
Harald Welte7af4cc32005-08-09 19:44:15 -07001052{
Patrick McHardy32292a72006-04-06 14:11:30 -07001053 nf_unregister_queue_handlers(&nfqh);
1054 unregister_netdevice_notifier(&nfqnl_dev_notifier);
1055#ifdef CONFIG_PROC_FS
1056 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
1057#endif
1058 nfnetlink_subsys_unregister(&nfqnl_subsys);
1059 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
Jesper Dangaard Brouer67137f32009-06-08 03:11:33 +00001060
1061 rcu_barrier(); /* Wait for completion of call_rcu()'s */
Harald Welte7af4cc32005-08-09 19:44:15 -07001062}
1063
1064MODULE_DESCRIPTION("netfilter packet queue handler");
1065MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1066MODULE_LICENSE("GPL");
1067MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1068
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001069module_init(nfnetlink_queue_init);
1070module_exit(nfnetlink_queue_fini);