blob: 350c50fbfd4ded03cf4485ddd6d6a2cb87e433a6 [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>
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +020033#include <net/netfilter/nfnetlink_queue.h>
Harald Welte7af4cc32005-08-09 19:44:15 -070034
Arun Sharma600634972011-07-26 16:09:06 -070035#include <linux/atomic.h>
Harald Welte7af4cc32005-08-09 19:44:15 -070036
Harald Weltefbcd9232005-08-09 20:22:10 -070037#ifdef CONFIG_BRIDGE_NETFILTER
38#include "../bridge/br_private.h"
39#endif
40
Harald Welte7af4cc32005-08-09 19:44:15 -070041#define NFQNL_QMAX_DEFAULT 1024
42
Harald Welte7af4cc32005-08-09 19:44:15 -070043struct nfqnl_instance {
44 struct hlist_node hlist; /* global list of queues */
Patrick McHardy9872bec2007-12-05 01:28:50 -080045 struct rcu_head rcu;
Harald Welte7af4cc32005-08-09 19:44:15 -070046
Eric W. Biederman15e47302012-09-07 20:12:54 +000047 int peer_portid;
Harald Welte7af4cc32005-08-09 19:44:15 -070048 unsigned int queue_maxlen;
49 unsigned int copy_range;
Harald Welte7af4cc32005-08-09 19:44:15 -070050 unsigned int queue_dropped;
51 unsigned int queue_user_dropped;
52
Harald Welte7af4cc32005-08-09 19:44:15 -070053
54 u_int16_t queue_num; /* number of this queue */
55 u_int8_t copy_mode;
Krishna Kumarfdb694a2012-05-24 03:56:44 +000056 u_int32_t flags; /* Set using NFQA_CFG_FLAGS */
Eric Dumazetc463ac92010-06-09 18:07:06 +020057/*
58 * Following fields are dirtied for each queued packet,
59 * keep them in same cache line if possible.
60 */
61 spinlock_t lock;
62 unsigned int queue_total;
Eric Dumazet58637022011-07-19 11:44:17 +020063 unsigned int id_sequence; /* 'sequence' of pkt ids */
Harald Welte7af4cc32005-08-09 19:44:15 -070064 struct list_head queue_list; /* packets in queue */
65};
66
Patrick McHardy02f014d2007-12-05 01:26:33 -080067typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
Harald Welte7af4cc32005-08-09 19:44:15 -070068
Patrick McHardy9872bec2007-12-05 01:28:50 -080069static DEFINE_SPINLOCK(instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -070070
Harald Welte7af4cc32005-08-09 19:44:15 -070071#define INSTANCE_BUCKETS 16
Patrick McHardy9d6023a2007-12-05 01:29:38 -080072static struct hlist_head instance_table[INSTANCE_BUCKETS] __read_mostly;
Harald Welte7af4cc32005-08-09 19:44:15 -070073
74static inline u_int8_t instance_hashfn(u_int16_t queue_num)
75{
Pablo Neira Ayuso1cdb0902013-03-14 06:03:17 +000076 return ((queue_num >> 8) ^ queue_num) % INSTANCE_BUCKETS;
Harald Welte7af4cc32005-08-09 19:44:15 -070077}
78
79static struct nfqnl_instance *
Patrick McHardy9872bec2007-12-05 01:28:50 -080080instance_lookup(u_int16_t queue_num)
Harald Welte7af4cc32005-08-09 19:44:15 -070081{
82 struct hlist_head *head;
Harald Welte7af4cc32005-08-09 19:44:15 -070083 struct nfqnl_instance *inst;
84
85 head = &instance_table[instance_hashfn(queue_num)];
Sasha Levinb67bfe02013-02-27 17:06:00 -080086 hlist_for_each_entry_rcu(inst, 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 *
Eric W. Biederman15e47302012-09-07 20:12:54 +000094instance_create(u_int16_t queue_num, int portid)
Harald Welte7af4cc32005-08-09 19:44:15 -070095{
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;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000113 inst->peer_portid = portid;
Harald Welte7af4cc32005-08-09 19:44:15 -0700114 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;
Pablo Neira Ayuso6ee584b2012-09-24 14:52:12 +0200227 size_t data_len = 0, cap_len = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700228 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;
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200236 struct nf_conn *ct = NULL;
237 enum ip_conntrack_info uninitialized_var(ctinfo);
Harald Welte7af4cc32005-08-09 19:44:15 -0700238
Eric Leblondcabaa9b2008-03-10 16:41:43 -0700239 size = NLMSG_SPACE(sizeof(struct nfgenmsg))
Patrick McHardydf6fb862007-09-28 14:37:03 -0700240 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
241 + 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#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700244 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
245 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700246#endif
Patrick McHardydf6fb862007-09-28 14:37:03 -0700247 + nla_total_size(sizeof(u_int32_t)) /* mark */
248 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
Pablo Neira Ayuso6ee584b2012-09-24 14:52:12 +0200249 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp)
250 + nla_total_size(sizeof(u_int32_t))); /* cap_len */
Harald Welte7af4cc32005-08-09 19:44:15 -0700251
Patrick McHardy02f014d2007-12-05 01:26:33 -0800252 outdev = entry->outdev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800253
Eric Dumazetc463ac92010-06-09 18:07:06 +0200254 switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700255 case NFQNL_COPY_META:
256 case NFQNL_COPY_NONE:
Harald Welte7af4cc32005-08-09 19:44:15 -0700257 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800258
Harald Welte7af4cc32005-08-09 19:44:15 -0700259 case NFQNL_COPY_PACKET:
Herbert Xue9f13ca2010-04-08 14:54:35 +0200260 if (entskb->ip_summed == CHECKSUM_PARTIAL &&
Eric Dumazetc463ac92010-06-09 18:07:06 +0200261 skb_checksum_help(entskb))
Patrick McHardye7dfb092005-09-06 15:10:00 -0700262 return NULL;
Eric Dumazetc463ac92010-06-09 18:07:06 +0200263
264 data_len = ACCESS_ONCE(queue->copy_range);
265 if (data_len == 0 || data_len > entskb->len)
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800266 data_len = entskb->len;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800267
Patrick McHardydf6fb862007-09-28 14:37:03 -0700268 size += nla_total_size(data_len);
Pablo Neira Ayuso6ee584b2012-09-24 14:52:12 +0200269 cap_len = entskb->len;
Harald Welte7af4cc32005-08-09 19:44:15 -0700270 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700271 }
272
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +0200273 if (queue->flags & NFQA_CFG_F_CONNTRACK)
274 ct = nfqnl_ct_get(entskb, &size, &ctinfo);
Harald Welte7af4cc32005-08-09 19:44:15 -0700275
276 skb = alloc_skb(size, GFP_ATOMIC);
277 if (!skb)
David S. Miller3da07c02012-06-26 21:35:27 -0700278 return NULL;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800279
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700280 old_tail = skb->tail;
David S. Miller3da07c02012-06-26 21:35:27 -0700281 nlh = nlmsg_put(skb, 0, 0,
Harald Welte7af4cc32005-08-09 19:44:15 -0700282 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
David S. Miller3da07c02012-06-26 21:35:27 -0700283 sizeof(struct nfgenmsg), 0);
284 if (!nlh) {
285 kfree_skb(skb);
286 return NULL;
287 }
288 nfmsg = nlmsg_data(nlh);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800289 nfmsg->nfgen_family = entry->pf;
Harald Welte7af4cc32005-08-09 19:44:15 -0700290 nfmsg->version = NFNETLINK_V0;
291 nfmsg->res_id = htons(queue->queue_num);
292
Eric Dumazet58637022011-07-19 11:44:17 +0200293 nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg));
294 pmsg = nla_data(nla);
295 pmsg->hw_protocol = entskb->protocol;
296 pmsg->hook = entry->hook;
297 *packet_id_ptr = &pmsg->packet_id;
Harald Welte7af4cc32005-08-09 19:44:15 -0700298
Patrick McHardy02f014d2007-12-05 01:26:33 -0800299 indev = entry->indev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800300 if (indev) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700301#ifndef CONFIG_BRIDGE_NETFILTER
David S. Millera4471892012-03-29 23:27:38 -0400302 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex)))
303 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700304#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800305 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700306 /* Case 1: indev is physical input device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800307 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700308 * netfilter_bridge) */
David S. Millera4471892012-03-29 23:27:38 -0400309 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
310 htonl(indev->ifindex)) ||
Harald Weltefbcd9232005-08-09 20:22:10 -0700311 /* this is the bridge group "brX" */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000312 /* rcu_read_lock()ed by __nf_queue */
David S. Millera4471892012-03-29 23:27:38 -0400313 nla_put_be32(skb, NFQA_IFINDEX_INDEV,
314 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
315 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700316 } else {
317 /* Case 2: indev is bridge group, we need to look for
318 * physical device (when called from ipv4) */
David S. Millera4471892012-03-29 23:27:38 -0400319 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV,
320 htonl(indev->ifindex)))
321 goto nla_put_failure;
322 if (entskb->nf_bridge && entskb->nf_bridge->physindev &&
323 nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
324 htonl(entskb->nf_bridge->physindev->ifindex)))
325 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700326 }
327#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700328 }
329
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800330 if (outdev) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700331#ifndef CONFIG_BRIDGE_NETFILTER
David S. Millera4471892012-03-29 23:27:38 -0400332 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex)))
333 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700334#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800335 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700336 /* Case 1: outdev is physical output device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800337 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700338 * netfilter_bridge) */
David S. Millera4471892012-03-29 23:27:38 -0400339 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
340 htonl(outdev->ifindex)) ||
Harald Weltefbcd9232005-08-09 20:22:10 -0700341 /* this is the bridge group "brX" */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000342 /* rcu_read_lock()ed by __nf_queue */
David S. Millera4471892012-03-29 23:27:38 -0400343 nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
344 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
345 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700346 } else {
347 /* Case 2: outdev is bridge group, we need to look for
348 * physical output device (when called from ipv4) */
David S. Millera4471892012-03-29 23:27:38 -0400349 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
350 htonl(outdev->ifindex)))
351 goto nla_put_failure;
352 if (entskb->nf_bridge && entskb->nf_bridge->physoutdev &&
353 nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
354 htonl(entskb->nf_bridge->physoutdev->ifindex)))
355 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700356 }
357#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700358 }
359
David S. Millera4471892012-03-29 23:27:38 -0400360 if (entskb->mark &&
361 nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark)))
362 goto nla_put_failure;
Harald Welte7af4cc32005-08-09 19:44:15 -0700363
Nicolas Cavallari2c38de42011-06-16 17:27:04 +0200364 if (indev && entskb->dev &&
365 entskb->mac_header != entskb->network_header) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700366 struct nfqnl_msg_packet_hw phw;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700367 int len = dev_parse_header(entskb, phw.hw_addr);
368 if (len) {
369 phw.hw_addrlen = htons(len);
David S. Millera4471892012-03-29 23:27:38 -0400370 if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw))
371 goto nla_put_failure;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700372 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700373 }
374
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700375 if (entskb->tstamp.tv64) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700376 struct nfqnl_msg_packet_timestamp ts;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700377 struct timeval tv = ktime_to_timeval(entskb->tstamp);
378 ts.sec = cpu_to_be64(tv.tv_sec);
379 ts.usec = cpu_to_be64(tv.tv_usec);
Harald Welte7af4cc32005-08-09 19:44:15 -0700380
David S. Millera4471892012-03-29 23:27:38 -0400381 if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts))
382 goto nla_put_failure;
Harald Welte7af4cc32005-08-09 19:44:15 -0700383 }
384
385 if (data_len) {
Patrick McHardydf6fb862007-09-28 14:37:03 -0700386 struct nlattr *nla;
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800387 int sz = nla_attr_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700388
Patrick McHardydf6fb862007-09-28 14:37:03 -0700389 if (skb_tailroom(skb) < nla_total_size(data_len)) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700390 printk(KERN_WARNING "nf_queue: no tailroom!\n");
David S. Miller3da07c02012-06-26 21:35:27 -0700391 kfree_skb(skb);
392 return NULL;
Harald Welte7af4cc32005-08-09 19:44:15 -0700393 }
394
Patrick McHardydf6fb862007-09-28 14:37:03 -0700395 nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));
396 nla->nla_type = NFQA_PAYLOAD;
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800397 nla->nla_len = sz;
Harald Welte7af4cc32005-08-09 19:44:15 -0700398
Patrick McHardydf6fb862007-09-28 14:37:03 -0700399 if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700400 BUG();
401 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800402
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +0200403 if (ct && nfqnl_ct_put(skb, ct, ctinfo) < 0)
404 goto nla_put_failure;
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200405
Pablo Neira Ayuso6ee584b2012-09-24 14:52:12 +0200406 if (cap_len > 0 && nla_put_be32(skb, NFQA_CAP_LEN, htonl(cap_len)))
407 goto nla_put_failure;
408
Harald Welte7af4cc32005-08-09 19:44:15 -0700409 nlh->nlmsg_len = skb->tail - old_tail;
410 return skb;
411
Patrick McHardydf6fb862007-09-28 14:37:03 -0700412nla_put_failure:
Wei Yongjuna6729952012-08-28 03:14:15 +0000413 kfree_skb(skb);
Joe Perchese87cc472012-05-13 21:56:26 +0000414 net_err_ratelimited("nf_queue: error creating packet message\n");
Harald Welte7af4cc32005-08-09 19:44:15 -0700415 return NULL;
416}
417
418static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800419nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
Harald Welte7af4cc32005-08-09 19:44:15 -0700420{
Harald Welte7af4cc32005-08-09 19:44:15 -0700421 struct sk_buff *nskb;
422 struct nfqnl_instance *queue;
Florian Westphalf1585082011-01-18 15:27:28 +0100423 int err = -ENOBUFS;
Eric Dumazet58637022011-07-19 11:44:17 +0200424 __be32 *packet_id_ptr;
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000425 int failopen = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700426
Patrick McHardy9872bec2007-12-05 01:28:50 -0800427 /* rcu_read_lock()ed by nf_hook_slow() */
428 queue = instance_lookup(queuenum);
Florian Westphalf1585082011-01-18 15:27:28 +0100429 if (!queue) {
430 err = -ESRCH;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800431 goto err_out;
Florian Westphalf1585082011-01-18 15:27:28 +0100432 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700433
Florian Westphalf1585082011-01-18 15:27:28 +0100434 if (queue->copy_mode == NFQNL_COPY_NONE) {
435 err = -EINVAL;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800436 goto err_out;
Florian Westphalf1585082011-01-18 15:27:28 +0100437 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700438
Eric Dumazet58637022011-07-19 11:44:17 +0200439 nskb = nfqnl_build_packet_message(queue, entry, &packet_id_ptr);
Florian Westphalf1585082011-01-18 15:27:28 +0100440 if (nskb == NULL) {
441 err = -ENOMEM;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800442 goto err_out;
Florian Westphalf1585082011-01-18 15:27:28 +0100443 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700444 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800445
Eric W. Biederman15e47302012-09-07 20:12:54 +0000446 if (!queue->peer_portid) {
Florian Westphalf1585082011-01-18 15:27:28 +0100447 err = -EINVAL;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800448 goto err_out_free_nskb;
Florian Westphalf1585082011-01-18 15:27:28 +0100449 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700450 if (queue->queue_total >= queue->queue_maxlen) {
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000451 if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
452 failopen = 1;
453 err = 0;
454 } else {
455 queue->queue_dropped++;
456 net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n",
457 queue->queue_total);
458 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700459 goto err_out_free_nskb;
460 }
Eric Dumazet58637022011-07-19 11:44:17 +0200461 entry->id = ++queue->id_sequence;
462 *packet_id_ptr = htonl(entry->id);
Harald Welte7af4cc32005-08-09 19:44:15 -0700463
464 /* nfnetlink_unicast will either free the nskb or add it to a socket */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000465 err = nfnetlink_unicast(nskb, &init_net, queue->peer_portid, MSG_DONTWAIT);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800466 if (err < 0) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800467 queue->queue_user_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700468 goto err_out_unlock;
469 }
470
471 __enqueue_entry(queue, entry);
472
473 spin_unlock_bh(&queue->lock);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800474 return 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700475
476err_out_free_nskb:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800477 kfree_skb(nskb);
Harald Welte7af4cc32005-08-09 19:44:15 -0700478err_out_unlock:
479 spin_unlock_bh(&queue->lock);
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000480 if (failopen)
481 nf_reinject(entry, NF_ACCEPT);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800482err_out:
Florian Westphalf1585082011-01-18 15:27:28 +0100483 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700484}
485
486static int
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200487nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e, int diff)
Harald Welte7af4cc32005-08-09 19:44:15 -0700488{
Patrick McHardye2b58a62008-02-19 17:17:52 -0800489 struct sk_buff *nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700490
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800491 if (diff < 0) {
492 if (pskb_trim(e->skb, data_len))
493 return -ENOMEM;
494 } else if (diff > 0) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700495 if (data_len > 0xFFFF)
496 return -EINVAL;
497 if (diff > skb_tailroom(e->skb)) {
Arnaud Ebalard9a732ed2008-04-29 03:16:34 -0700498 nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
499 diff, GFP_ATOMIC);
Patrick McHardye2b58a62008-02-19 17:17:52 -0800500 if (!nskb) {
Patrick McHardy1158ba22006-08-22 00:32:47 -0700501 printk(KERN_WARNING "nf_queue: OOM "
Harald Welte7af4cc32005-08-09 19:44:15 -0700502 "in mangle, dropping packet\n");
Patrick McHardye2b58a62008-02-19 17:17:52 -0800503 return -ENOMEM;
Harald Welte7af4cc32005-08-09 19:44:15 -0700504 }
Patrick McHardye2b58a62008-02-19 17:17:52 -0800505 kfree_skb(e->skb);
506 e->skb = nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700507 }
508 skb_put(e->skb, diff);
509 }
Herbert Xu37d41872007-10-14 00:39:18 -0700510 if (!skb_make_writable(e->skb, data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700511 return -ENOMEM;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300512 skb_copy_to_linear_data(e->skb, data, data_len);
Patrick McHardye7dfb092005-09-06 15:10:00 -0700513 e->skb->ip_summed = CHECKSUM_NONE;
Harald Welte7af4cc32005-08-09 19:44:15 -0700514 return 0;
515}
516
Harald Welte7af4cc32005-08-09 19:44:15 -0700517static int
518nfqnl_set_mode(struct nfqnl_instance *queue,
519 unsigned char mode, unsigned int range)
520{
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800521 int status = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700522
523 spin_lock_bh(&queue->lock);
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800524 switch (mode) {
525 case NFQNL_COPY_NONE:
526 case NFQNL_COPY_META:
527 queue->copy_mode = mode;
528 queue->copy_range = 0;
529 break;
530
531 case NFQNL_COPY_PACKET:
532 queue->copy_mode = mode;
Pablo Neira Ayusoba8d3b02012-09-06 17:09:26 +0200533 /* We're using struct nlattr which has 16bit nla_len. Note that
534 * nla_len includes the header length. Thus, the maximum packet
535 * length that we support is 65531 bytes. We send truncated
536 * packets if the specified length is larger than that.
537 */
538 if (range > 0xffff - NLA_HDRLEN)
539 queue->copy_range = 0xffff - NLA_HDRLEN;
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800540 else
541 queue->copy_range = range;
542 break;
543
544 default:
545 status = -EINVAL;
546
547 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700548 spin_unlock_bh(&queue->lock);
549
550 return status;
551}
552
553static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800554dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700555{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800556 if (entry->indev)
557 if (entry->indev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700558 return 1;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800559 if (entry->outdev)
560 if (entry->outdev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700561 return 1;
Patrick McHardyef47c6a72006-06-27 03:01:48 -0700562#ifdef CONFIG_BRIDGE_NETFILTER
563 if (entry->skb->nf_bridge) {
564 if (entry->skb->nf_bridge->physindev &&
565 entry->skb->nf_bridge->physindev->ifindex == ifindex)
566 return 1;
567 if (entry->skb->nf_bridge->physoutdev &&
568 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
569 return 1;
570 }
571#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700572 return 0;
573}
574
575/* drop all packets with either indev or outdev == ifindex from all queue
576 * instances */
577static void
578nfqnl_dev_drop(int ifindex)
579{
580 int i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800581
Patrick McHardy9872bec2007-12-05 01:28:50 -0800582 rcu_read_lock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700583
Patrick McHardy9872bec2007-12-05 01:28:50 -0800584 for (i = 0; i < INSTANCE_BUCKETS; i++) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700585 struct nfqnl_instance *inst;
586 struct hlist_head *head = &instance_table[i];
587
Sasha Levinb67bfe02013-02-27 17:06:00 -0800588 hlist_for_each_entry_rcu(inst, head, hlist)
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800589 nfqnl_flush(inst, dev_cmp, ifindex);
Harald Welte7af4cc32005-08-09 19:44:15 -0700590 }
591
Patrick McHardy9872bec2007-12-05 01:28:50 -0800592 rcu_read_unlock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700593}
594
595#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
596
597static int
598nfqnl_rcv_dev_event(struct notifier_block *this,
599 unsigned long event, void *ptr)
600{
601 struct net_device *dev = ptr;
602
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700603 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200604 return NOTIFY_DONE;
605
Harald Welte7af4cc32005-08-09 19:44:15 -0700606 /* Drop any packets associated with the downed device */
607 if (event == NETDEV_DOWN)
608 nfqnl_dev_drop(dev->ifindex);
609 return NOTIFY_DONE;
610}
611
612static struct notifier_block nfqnl_dev_notifier = {
613 .notifier_call = nfqnl_rcv_dev_event,
614};
615
616static int
617nfqnl_rcv_nl_event(struct notifier_block *this,
618 unsigned long event, void *ptr)
619{
620 struct netlink_notify *n = ptr;
621
Patrick McHardydee58172009-11-06 17:04:00 +0100622 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700623 int i;
624
Eric W. Biederman15e47302012-09-07 20:12:54 +0000625 /* destroy all instances for this portid */
Patrick McHardy9872bec2007-12-05 01:28:50 -0800626 spin_lock(&instances_lock);
627 for (i = 0; i < INSTANCE_BUCKETS; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800628 struct hlist_node *t2;
Harald Welte7af4cc32005-08-09 19:44:15 -0700629 struct nfqnl_instance *inst;
630 struct hlist_head *head = &instance_table[i];
631
Sasha Levinb67bfe02013-02-27 17:06:00 -0800632 hlist_for_each_entry_safe(inst, t2, head, hlist) {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200633 if ((n->net == &init_net) &&
Eric W. Biederman15e47302012-09-07 20:12:54 +0000634 (n->portid == inst->peer_portid))
Harald Welte7af4cc32005-08-09 19:44:15 -0700635 __instance_destroy(inst);
636 }
637 }
Patrick McHardy9872bec2007-12-05 01:28:50 -0800638 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700639 }
640 return NOTIFY_DONE;
641}
642
643static struct notifier_block nfqnl_rtnl_notifier = {
644 .notifier_call = nfqnl_rcv_nl_event,
645};
646
Patrick McHardy5bf75852007-09-28 14:39:26 -0700647static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
648 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
649 [NFQA_MARK] = { .type = NLA_U32 },
650 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200651 [NFQA_CT] = { .type = NLA_UNSPEC },
Harald Welte838ab632005-08-09 19:50:45 -0700652};
653
Florian Westphal97d32cf2011-07-19 11:46:33 +0200654static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = {
655 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
656 [NFQA_MARK] = { .type = NLA_U32 },
657};
658
Eric W. Biederman15e47302012-09-07 20:12:54 +0000659static struct nfqnl_instance *verdict_instance_lookup(u16 queue_num, int nlportid)
Florian Westphal97d32cf2011-07-19 11:46:33 +0200660{
661 struct nfqnl_instance *queue;
662
663 queue = instance_lookup(queue_num);
664 if (!queue)
665 return ERR_PTR(-ENODEV);
666
Eric W. Biederman15e47302012-09-07 20:12:54 +0000667 if (queue->peer_portid != nlportid)
Florian Westphal97d32cf2011-07-19 11:46:33 +0200668 return ERR_PTR(-EPERM);
669
670 return queue;
671}
672
673static struct nfqnl_msg_verdict_hdr*
674verdicthdr_get(const struct nlattr * const nfqa[])
675{
676 struct nfqnl_msg_verdict_hdr *vhdr;
677 unsigned int verdict;
678
679 if (!nfqa[NFQA_VERDICT_HDR])
680 return NULL;
681
682 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
Florian Westphalc6675232011-08-30 15:01:20 +0200683 verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK;
684 if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN)
Florian Westphal97d32cf2011-07-19 11:46:33 +0200685 return NULL;
686 return vhdr;
687}
688
689static int nfq_id_after(unsigned int id, unsigned int max)
690{
691 return (int)(id - max) > 0;
692}
693
694static int
695nfqnl_recv_verdict_batch(struct sock *ctnl, struct sk_buff *skb,
696 const struct nlmsghdr *nlh,
697 const struct nlattr * const nfqa[])
698{
David S. Miller3da07c02012-06-26 21:35:27 -0700699 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Florian Westphal97d32cf2011-07-19 11:46:33 +0200700 struct nf_queue_entry *entry, *tmp;
701 unsigned int verdict, maxid;
702 struct nfqnl_msg_verdict_hdr *vhdr;
703 struct nfqnl_instance *queue;
704 LIST_HEAD(batch_list);
705 u16 queue_num = ntohs(nfmsg->res_id);
706
Eric W. Biederman15e47302012-09-07 20:12:54 +0000707 queue = verdict_instance_lookup(queue_num, NETLINK_CB(skb).portid);
Florian Westphal97d32cf2011-07-19 11:46:33 +0200708 if (IS_ERR(queue))
709 return PTR_ERR(queue);
710
711 vhdr = verdicthdr_get(nfqa);
712 if (!vhdr)
713 return -EINVAL;
714
715 verdict = ntohl(vhdr->verdict);
716 maxid = ntohl(vhdr->id);
717
718 spin_lock_bh(&queue->lock);
719
720 list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) {
721 if (nfq_id_after(entry->id, maxid))
722 break;
723 __dequeue_entry(queue, entry);
724 list_add_tail(&entry->list, &batch_list);
725 }
726
727 spin_unlock_bh(&queue->lock);
728
729 if (list_empty(&batch_list))
730 return -ENOENT;
731
732 list_for_each_entry_safe(entry, tmp, &batch_list, list) {
733 if (nfqa[NFQA_MARK])
734 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
735 nf_reinject(entry, verdict);
736 }
737 return 0;
738}
739
Harald Welte7af4cc32005-08-09 19:44:15 -0700740static int
741nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200742 const struct nlmsghdr *nlh,
743 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700744{
David S. Miller3da07c02012-06-26 21:35:27 -0700745 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Harald Welte7af4cc32005-08-09 19:44:15 -0700746 u_int16_t queue_num = ntohs(nfmsg->res_id);
747
748 struct nfqnl_msg_verdict_hdr *vhdr;
749 struct nfqnl_instance *queue;
750 unsigned int verdict;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800751 struct nf_queue_entry *entry;
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200752 enum ip_conntrack_info uninitialized_var(ctinfo);
753 struct nf_conn *ct = NULL;
Harald Welte7af4cc32005-08-09 19:44:15 -0700754
Patrick McHardy9872bec2007-12-05 01:28:50 -0800755 queue = instance_lookup(queue_num);
Eric Dumazet84a797d2011-07-18 16:08:27 +0200756 if (!queue)
Harald Welte7af4cc32005-08-09 19:44:15 -0700757
Eric W. Biederman15e47302012-09-07 20:12:54 +0000758 queue = verdict_instance_lookup(queue_num, NETLINK_CB(skb).portid);
Florian Westphal97d32cf2011-07-19 11:46:33 +0200759 if (IS_ERR(queue))
760 return PTR_ERR(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700761
Florian Westphal97d32cf2011-07-19 11:46:33 +0200762 vhdr = verdicthdr_get(nfqa);
763 if (!vhdr)
Eric Dumazet84a797d2011-07-18 16:08:27 +0200764 return -EINVAL;
Harald Welte7af4cc32005-08-09 19:44:15 -0700765
Harald Welte7af4cc32005-08-09 19:44:15 -0700766 verdict = ntohl(vhdr->verdict);
767
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800768 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
Eric Dumazet84a797d2011-07-18 16:08:27 +0200769 if (entry == NULL)
770 return -ENOENT;
Harald Welte7af4cc32005-08-09 19:44:15 -0700771
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200772 rcu_read_lock();
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +0200773 if (nfqa[NFQA_CT] && (queue->flags & NFQA_CFG_F_CONNTRACK))
774 ct = nfqnl_ct_parse(entry->skb, nfqa[NFQA_CT], &ctinfo);
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200775
Patrick McHardydf6fb862007-09-28 14:37:03 -0700776 if (nfqa[NFQA_PAYLOAD]) {
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200777 u16 payload_len = nla_len(nfqa[NFQA_PAYLOAD]);
778 int diff = payload_len - entry->skb->len;
779
Patrick McHardydf6fb862007-09-28 14:37:03 -0700780 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200781 payload_len, entry, diff) < 0)
Harald Welte7af4cc32005-08-09 19:44:15 -0700782 verdict = NF_DROP;
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200783
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +0200784 if (ct)
785 nfqnl_ct_seq_adjust(skb, ct, ctinfo, diff);
Harald Welte7af4cc32005-08-09 19:44:15 -0700786 }
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200787 rcu_read_unlock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700788
Patrick McHardydf6fb862007-09-28 14:37:03 -0700789 if (nfqa[NFQA_MARK])
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800790 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800791
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800792 nf_reinject(entry, verdict);
Harald Welte7af4cc32005-08-09 19:44:15 -0700793 return 0;
794}
795
796static int
797nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200798 const struct nlmsghdr *nlh,
799 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700800{
801 return -ENOTSUPP;
802}
803
Patrick McHardy5bf75852007-09-28 14:39:26 -0700804static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
805 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
806 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
Harald Welte838ab632005-08-09 19:50:45 -0700807};
808
Patrick McHardye3ac5292007-12-05 01:23:57 -0800809static const struct nf_queue_handler nfqh = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700810 .outfn = &nfqnl_enqueue_packet,
811};
812
Harald Welte7af4cc32005-08-09 19:44:15 -0700813static int
814nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200815 const struct nlmsghdr *nlh,
816 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700817{
David S. Miller3da07c02012-06-26 21:35:27 -0700818 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Harald Welte7af4cc32005-08-09 19:44:15 -0700819 u_int16_t queue_num = ntohs(nfmsg->res_id);
820 struct nfqnl_instance *queue;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800821 struct nfqnl_msg_config_cmd *cmd = NULL;
Harald Welte838ab632005-08-09 19:50:45 -0700822 int ret = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700823
Patrick McHardy9872bec2007-12-05 01:28:50 -0800824 if (nfqa[NFQA_CFG_CMD]) {
825 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
826
Florian Westphal0360ae42012-11-23 06:22:21 +0000827 /* Obsolete commands without queue context */
Patrick McHardy9872bec2007-12-05 01:28:50 -0800828 switch (cmd->command) {
Florian Westphal0360ae42012-11-23 06:22:21 +0000829 case NFQNL_CFG_CMD_PF_BIND: return 0;
830 case NFQNL_CFG_CMD_PF_UNBIND: return 0;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800831 }
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800832 }
833
Patrick McHardy9872bec2007-12-05 01:28:50 -0800834 rcu_read_lock();
835 queue = instance_lookup(queue_num);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000836 if (queue && queue->peer_portid != NETLINK_CB(skb).portid) {
Patrick McHardy9872bec2007-12-05 01:28:50 -0800837 ret = -EPERM;
838 goto err_out_unlock;
839 }
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800840
Patrick McHardy9872bec2007-12-05 01:28:50 -0800841 if (cmd != NULL) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700842 switch (cmd->command) {
843 case NFQNL_CFG_CMD_BIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800844 if (queue) {
845 ret = -EBUSY;
846 goto err_out_unlock;
847 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000848 queue = instance_create(queue_num, NETLINK_CB(skb).portid);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800849 if (IS_ERR(queue)) {
850 ret = PTR_ERR(queue);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800851 goto err_out_unlock;
852 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700853 break;
854 case NFQNL_CFG_CMD_UNBIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800855 if (!queue) {
856 ret = -ENODEV;
857 goto err_out_unlock;
858 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700859 instance_destroy(queue);
860 break;
861 case NFQNL_CFG_CMD_PF_BIND:
Harald Welte7af4cc32005-08-09 19:44:15 -0700862 case NFQNL_CFG_CMD_PF_UNBIND:
Harald Welte7af4cc32005-08-09 19:44:15 -0700863 break;
864 default:
Patrick McHardycd21f0a2007-12-17 22:40:19 -0800865 ret = -ENOTSUPP;
Harald Welte838ab632005-08-09 19:50:45 -0700866 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700867 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700868 }
869
Patrick McHardydf6fb862007-09-28 14:37:03 -0700870 if (nfqa[NFQA_CFG_PARAMS]) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700871 struct nfqnl_msg_config_params *params;
Harald Welte7af4cc32005-08-09 19:44:15 -0700872
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800873 if (!queue) {
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800874 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800875 goto err_out_unlock;
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800876 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700877 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700878 nfqnl_set_mode(queue, params->copy_mode,
879 ntohl(params->copy_range));
880 }
881
Patrick McHardydf6fb862007-09-28 14:37:03 -0700882 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
Eric Leblond829e17a2006-11-29 02:35:33 +0100883 __be32 *queue_maxlen;
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800884
885 if (!queue) {
886 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800887 goto err_out_unlock;
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800888 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700889 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
Eric Leblond829e17a2006-11-29 02:35:33 +0100890 spin_lock_bh(&queue->lock);
891 queue->queue_maxlen = ntohl(*queue_maxlen);
892 spin_unlock_bh(&queue->lock);
893 }
894
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000895 if (nfqa[NFQA_CFG_FLAGS]) {
896 __u32 flags, mask;
897
898 if (!queue) {
899 ret = -ENODEV;
900 goto err_out_unlock;
901 }
902
903 if (!nfqa[NFQA_CFG_MASK]) {
904 /* A mask is needed to specify which flags are being
905 * changed.
906 */
907 ret = -EINVAL;
908 goto err_out_unlock;
909 }
910
911 flags = ntohl(nla_get_be32(nfqa[NFQA_CFG_FLAGS]));
912 mask = ntohl(nla_get_be32(nfqa[NFQA_CFG_MASK]));
913
Krishna Kumar46ba5a22012-06-27 00:59:56 +0000914 if (flags >= NFQA_CFG_F_MAX) {
915 ret = -EOPNOTSUPP;
916 goto err_out_unlock;
917 }
918
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000919 spin_lock_bh(&queue->lock);
920 queue->flags &= ~mask;
921 queue->flags |= flags & mask;
922 spin_unlock_bh(&queue->lock);
923 }
924
Patrick McHardy9872bec2007-12-05 01:28:50 -0800925err_out_unlock:
926 rcu_read_unlock();
Harald Welte838ab632005-08-09 19:50:45 -0700927 return ret;
Harald Welte7af4cc32005-08-09 19:44:15 -0700928}
929
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700930static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
Eric Dumazet84a797d2011-07-18 16:08:27 +0200931 [NFQNL_MSG_PACKET] = { .call_rcu = nfqnl_recv_unsupp,
Harald Welte37d2e7a2005-11-14 15:24:59 -0800932 .attr_count = NFQA_MAX, },
Eric Dumazet84a797d2011-07-18 16:08:27 +0200933 [NFQNL_MSG_VERDICT] = { .call_rcu = nfqnl_recv_verdict,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700934 .attr_count = NFQA_MAX,
935 .policy = nfqa_verdict_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700936 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700937 .attr_count = NFQA_CFG_MAX,
938 .policy = nfqa_cfg_policy },
Florian Westphal97d32cf2011-07-19 11:46:33 +0200939 [NFQNL_MSG_VERDICT_BATCH]={ .call_rcu = nfqnl_recv_verdict_batch,
940 .attr_count = NFQA_MAX,
941 .policy = nfqa_verdict_batch_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700942};
943
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700944static const struct nfnetlink_subsystem nfqnl_subsys = {
Harald Welte7af4cc32005-08-09 19:44:15 -0700945 .name = "nf_queue",
946 .subsys_id = NFNL_SUBSYS_QUEUE,
947 .cb_count = NFQNL_MSG_MAX,
Harald Welte7af4cc32005-08-09 19:44:15 -0700948 .cb = nfqnl_cb,
949};
950
Harald Welte838ab632005-08-09 19:50:45 -0700951#ifdef CONFIG_PROC_FS
952struct iter_state {
953 unsigned int bucket;
954};
955
956static struct hlist_node *get_first(struct seq_file *seq)
957{
958 struct iter_state *st = seq->private;
959
960 if (!st)
961 return NULL;
962
963 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
964 if (!hlist_empty(&instance_table[st->bucket]))
965 return instance_table[st->bucket].first;
966 }
967 return NULL;
968}
969
970static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
971{
972 struct iter_state *st = seq->private;
973
974 h = h->next;
975 while (!h) {
976 if (++st->bucket >= INSTANCE_BUCKETS)
977 return NULL;
978
979 h = instance_table[st->bucket].first;
980 }
981 return h;
982}
983
984static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
985{
986 struct hlist_node *head;
987 head = get_first(seq);
988
989 if (head)
990 while (pos && (head = get_next(seq, head)))
991 pos--;
992 return pos ? NULL : head;
993}
994
995static void *seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800996 __acquires(instances_lock)
Harald Welte838ab632005-08-09 19:50:45 -0700997{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800998 spin_lock(&instances_lock);
Harald Welte838ab632005-08-09 19:50:45 -0700999 return get_idx(seq, *pos);
1000}
1001
1002static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1003{
1004 (*pos)++;
1005 return get_next(s, v);
1006}
1007
1008static void seq_stop(struct seq_file *s, void *v)
Eric Dumazetca7c48c2008-01-31 03:53:27 -08001009 __releases(instances_lock)
Harald Welte838ab632005-08-09 19:50:45 -07001010{
Patrick McHardy9872bec2007-12-05 01:28:50 -08001011 spin_unlock(&instances_lock);
Harald Welte838ab632005-08-09 19:50:45 -07001012}
1013
1014static int seq_show(struct seq_file *s, void *v)
1015{
1016 const struct nfqnl_instance *inst = v;
1017
1018 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
1019 inst->queue_num,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001020 inst->peer_portid, inst->queue_total,
Harald Welte838ab632005-08-09 19:50:45 -07001021 inst->copy_mode, inst->copy_range,
1022 inst->queue_dropped, inst->queue_user_dropped,
Eric Dumazet58637022011-07-19 11:44:17 +02001023 inst->id_sequence, 1);
Harald Welte838ab632005-08-09 19:50:45 -07001024}
1025
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001026static const struct seq_operations nfqnl_seq_ops = {
Harald Welte838ab632005-08-09 19:50:45 -07001027 .start = seq_start,
1028 .next = seq_next,
1029 .stop = seq_stop,
1030 .show = seq_show,
1031};
1032
1033static int nfqnl_open(struct inode *inode, struct file *file)
1034{
Pavel Emelyanove2da5912007-10-10 02:29:58 -07001035 return seq_open_private(file, &nfqnl_seq_ops,
1036 sizeof(struct iter_state));
Harald Welte838ab632005-08-09 19:50:45 -07001037}
1038
Arjan van de Venda7071d2007-02-12 00:55:36 -08001039static const struct file_operations nfqnl_file_ops = {
Harald Welte838ab632005-08-09 19:50:45 -07001040 .owner = THIS_MODULE,
1041 .open = nfqnl_open,
1042 .read = seq_read,
1043 .llseek = seq_lseek,
1044 .release = seq_release_private,
1045};
1046
1047#endif /* PROC_FS */
1048
Patrick McHardy32292a72006-04-06 14:11:30 -07001049static int __init nfnetlink_queue_init(void)
Harald Welte7af4cc32005-08-09 19:44:15 -07001050{
Harald Welte838ab632005-08-09 19:50:45 -07001051 int i, status = -ENOMEM;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001052
Harald Welte838ab632005-08-09 19:50:45 -07001053 for (i = 0; i < INSTANCE_BUCKETS; i++)
1054 INIT_HLIST_HEAD(&instance_table[i]);
1055
Harald Welte7af4cc32005-08-09 19:44:15 -07001056 netlink_register_notifier(&nfqnl_rtnl_notifier);
1057 status = nfnetlink_subsys_register(&nfqnl_subsys);
1058 if (status < 0) {
1059 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
1060 goto cleanup_netlink_notifier;
1061 }
1062
Harald Welte838ab632005-08-09 19:50:45 -07001063#ifdef CONFIG_PROC_FS
Denis V. Lunev8eeee8b2008-03-27 16:55:53 -07001064 if (!proc_create("nfnetlink_queue", 0440,
1065 proc_net_netfilter, &nfqnl_file_ops))
Harald Welte838ab632005-08-09 19:50:45 -07001066 goto cleanup_subsys;
Harald Welte838ab632005-08-09 19:50:45 -07001067#endif
1068
Harald Welte7af4cc32005-08-09 19:44:15 -07001069 register_netdevice_notifier(&nfqnl_dev_notifier);
Florian Westphal0360ae42012-11-23 06:22:21 +00001070 nf_register_queue_handler(&nfqh);
Harald Welte7af4cc32005-08-09 19:44:15 -07001071 return status;
1072
Harald Welte838ab632005-08-09 19:50:45 -07001073#ifdef CONFIG_PROC_FS
1074cleanup_subsys:
Harald Welte7af4cc32005-08-09 19:44:15 -07001075 nfnetlink_subsys_unregister(&nfqnl_subsys);
Patrick McHardy32292a72006-04-06 14:11:30 -07001076#endif
Harald Welte7af4cc32005-08-09 19:44:15 -07001077cleanup_netlink_notifier:
1078 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1079 return status;
1080}
1081
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001082static void __exit nfnetlink_queue_fini(void)
Harald Welte7af4cc32005-08-09 19:44:15 -07001083{
Florian Westphal0360ae42012-11-23 06:22:21 +00001084 nf_unregister_queue_handler();
Patrick McHardy32292a72006-04-06 14:11:30 -07001085 unregister_netdevice_notifier(&nfqnl_dev_notifier);
1086#ifdef CONFIG_PROC_FS
1087 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
1088#endif
1089 nfnetlink_subsys_unregister(&nfqnl_subsys);
1090 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
Jesper Dangaard Brouer67137f32009-06-08 03:11:33 +00001091
1092 rcu_barrier(); /* Wait for completion of call_rcu()'s */
Harald Welte7af4cc32005-08-09 19:44:15 -07001093}
1094
1095MODULE_DESCRIPTION("netfilter packet queue handler");
1096MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1097MODULE_LICENSE("GPL");
1098MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1099
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001100module_init(nfnetlink_queue_init);
1101module_exit(nfnetlink_queue_fini);