blob: c0496a55ad0ceffb5470872cadc83c218a4b70c9 [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
47 int peer_pid;
48 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{
76 return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
77}
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;
83 struct hlist_node *pos;
84 struct nfqnl_instance *inst;
85
86 head = &instance_table[instance_hashfn(queue_num)];
Patrick McHardy9872bec2007-12-05 01:28:50 -080087 hlist_for_each_entry_rcu(inst, pos, head, hlist) {
Harald Welte7af4cc32005-08-09 19:44:15 -070088 if (inst->queue_num == queue_num)
89 return inst;
90 }
91 return NULL;
92}
93
94static struct nfqnl_instance *
Harald Welte7af4cc32005-08-09 19:44:15 -070095instance_create(u_int16_t queue_num, int pid)
96{
Patrick McHardybaab2ce2007-12-17 22:41:21 -080097 struct nfqnl_instance *inst;
Patrick McHardy9872bec2007-12-05 01:28:50 -080098 unsigned int h;
Patrick McHardybaab2ce2007-12-17 22:41:21 -080099 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700100
Patrick McHardy9872bec2007-12-05 01:28:50 -0800101 spin_lock(&instances_lock);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800102 if (instance_lookup(queue_num)) {
103 err = -EEXIST;
Harald Welte7af4cc32005-08-09 19:44:15 -0700104 goto out_unlock;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800105 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700106
Harald Welte10dfdc62005-11-03 19:20:07 +0100107 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800108 if (!inst) {
109 err = -ENOMEM;
Harald Welte7af4cc32005-08-09 19:44:15 -0700110 goto out_unlock;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800111 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700112
Harald Welte7af4cc32005-08-09 19:44:15 -0700113 inst->queue_num = queue_num;
114 inst->peer_pid = pid;
115 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
116 inst->copy_range = 0xfffff;
117 inst->copy_mode = NFQNL_COPY_NONE;
YOSHIFUJI Hideaki181a46a2006-01-04 13:56:54 -0800118 spin_lock_init(&inst->lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700119 INIT_LIST_HEAD(&inst->queue_list);
120
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800121 if (!try_module_get(THIS_MODULE)) {
122 err = -EAGAIN;
Harald Welte7af4cc32005-08-09 19:44:15 -0700123 goto out_free;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800124 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700125
Patrick McHardy9872bec2007-12-05 01:28:50 -0800126 h = instance_hashfn(queue_num);
127 hlist_add_head_rcu(&inst->hlist, &instance_table[h]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700128
Patrick McHardy9872bec2007-12-05 01:28:50 -0800129 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700130
Harald Welte7af4cc32005-08-09 19:44:15 -0700131 return inst;
132
133out_free:
134 kfree(inst);
135out_unlock:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800136 spin_unlock(&instances_lock);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800137 return ERR_PTR(err);
Harald Welte7af4cc32005-08-09 19:44:15 -0700138}
139
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800140static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
141 unsigned long data);
Harald Welte7af4cc32005-08-09 19:44:15 -0700142
143static void
Patrick McHardy9872bec2007-12-05 01:28:50 -0800144instance_destroy_rcu(struct rcu_head *head)
Harald Welte7af4cc32005-08-09 19:44:15 -0700145{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800146 struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
147 rcu);
Harald Welte7af4cc32005-08-09 19:44:15 -0700148
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800149 nfqnl_flush(inst, NULL, 0);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800150 kfree(inst);
Harald Welte7af4cc32005-08-09 19:44:15 -0700151 module_put(THIS_MODULE);
152}
153
Patrick McHardy9872bec2007-12-05 01:28:50 -0800154static void
Harald Welte7af4cc32005-08-09 19:44:15 -0700155__instance_destroy(struct nfqnl_instance *inst)
156{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800157 hlist_del_rcu(&inst->hlist);
158 call_rcu(&inst->rcu, instance_destroy_rcu);
Harald Welte7af4cc32005-08-09 19:44:15 -0700159}
160
Patrick McHardy9872bec2007-12-05 01:28:50 -0800161static void
Harald Welte7af4cc32005-08-09 19:44:15 -0700162instance_destroy(struct nfqnl_instance *inst)
163{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800164 spin_lock(&instances_lock);
165 __instance_destroy(inst);
166 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700167}
168
Harald Welte7af4cc32005-08-09 19:44:15 -0700169static inline void
Patrick McHardy02f014d2007-12-05 01:26:33 -0800170__enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
Harald Welte7af4cc32005-08-09 19:44:15 -0700171{
Patrick McHardy0ac41e82007-12-05 01:25:03 -0800172 list_add_tail(&entry->list, &queue->queue_list);
Harald Welte7af4cc32005-08-09 19:44:15 -0700173 queue->queue_total++;
174}
175
Florian Westphal97d32cf2011-07-19 11:46:33 +0200176static void
177__dequeue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
178{
179 list_del(&entry->list);
180 queue->queue_total--;
181}
182
Patrick McHardy02f014d2007-12-05 01:26:33 -0800183static struct nf_queue_entry *
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800184find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
Harald Welte7af4cc32005-08-09 19:44:15 -0700185{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800186 struct nf_queue_entry *entry = NULL, *i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800187
Harald Welte7af4cc32005-08-09 19:44:15 -0700188 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800189
190 list_for_each_entry(i, &queue->queue_list, list) {
191 if (i->id == id) {
192 entry = i;
193 break;
194 }
195 }
196
Florian Westphal97d32cf2011-07-19 11:46:33 +0200197 if (entry)
198 __dequeue_entry(queue, entry);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800199
Harald Welte7af4cc32005-08-09 19:44:15 -0700200 spin_unlock_bh(&queue->lock);
201
202 return entry;
203}
204
205static void
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800206nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
Harald Welte7af4cc32005-08-09 19:44:15 -0700207{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800208 struct nf_queue_entry *entry, *next;
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800209
Harald Welte7af4cc32005-08-09 19:44:15 -0700210 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800211 list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
212 if (!cmpfn || cmpfn(entry, data)) {
213 list_del(&entry->list);
214 queue->queue_total--;
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800215 nf_reinject(entry, NF_DROP);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800216 }
217 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700218 spin_unlock_bh(&queue->lock);
219}
220
221static struct sk_buff *
222nfqnl_build_packet_message(struct nfqnl_instance *queue,
Eric Dumazet58637022011-07-19 11:44:17 +0200223 struct nf_queue_entry *entry,
224 __be32 **packet_id_ptr)
Harald Welte7af4cc32005-08-09 19:44:15 -0700225{
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700226 sk_buff_data_t old_tail;
Harald Welte7af4cc32005-08-09 19:44:15 -0700227 size_t size;
228 size_t data_len = 0;
229 struct sk_buff *skb;
Eric Dumazet58637022011-07-19 11:44:17 +0200230 struct nlattr *nla;
231 struct nfqnl_msg_packet_hdr *pmsg;
Harald Welte7af4cc32005-08-09 19:44:15 -0700232 struct nlmsghdr *nlh;
233 struct nfgenmsg *nfmsg;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800234 struct sk_buff *entskb = entry->skb;
235 struct net_device *indev;
236 struct net_device *outdev;
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200237 struct nf_conn *ct = NULL;
238 enum ip_conntrack_info uninitialized_var(ctinfo);
Harald Welte7af4cc32005-08-09 19:44:15 -0700239
Eric Leblondcabaa9b2008-03-10 16:41:43 -0700240 size = NLMSG_SPACE(sizeof(struct nfgenmsg))
Patrick McHardydf6fb862007-09-28 14:37:03 -0700241 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
242 + 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#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700245 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
246 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700247#endif
Patrick McHardydf6fb862007-09-28 14:37:03 -0700248 + nla_total_size(sizeof(u_int32_t)) /* mark */
249 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
250 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
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);
Harald Welte7af4cc32005-08-09 19:44:15 -0700269 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700270 }
271
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +0200272 if (queue->flags & NFQA_CFG_F_CONNTRACK)
273 ct = nfqnl_ct_get(entskb, &size, &ctinfo);
Harald Welte7af4cc32005-08-09 19:44:15 -0700274
275 skb = alloc_skb(size, GFP_ATOMIC);
276 if (!skb)
David S. Miller3da07c02012-06-26 21:35:27 -0700277 return NULL;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800278
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700279 old_tail = skb->tail;
David S. Miller3da07c02012-06-26 21:35:27 -0700280 nlh = nlmsg_put(skb, 0, 0,
Harald Welte7af4cc32005-08-09 19:44:15 -0700281 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
David S. Miller3da07c02012-06-26 21:35:27 -0700282 sizeof(struct nfgenmsg), 0);
283 if (!nlh) {
284 kfree_skb(skb);
285 return NULL;
286 }
287 nfmsg = nlmsg_data(nlh);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800288 nfmsg->nfgen_family = entry->pf;
Harald Welte7af4cc32005-08-09 19:44:15 -0700289 nfmsg->version = NFNETLINK_V0;
290 nfmsg->res_id = htons(queue->queue_num);
291
Eric Dumazet58637022011-07-19 11:44:17 +0200292 nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg));
293 pmsg = nla_data(nla);
294 pmsg->hw_protocol = entskb->protocol;
295 pmsg->hook = entry->hook;
296 *packet_id_ptr = &pmsg->packet_id;
Harald Welte7af4cc32005-08-09 19:44:15 -0700297
Patrick McHardy02f014d2007-12-05 01:26:33 -0800298 indev = entry->indev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800299 if (indev) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700300#ifndef CONFIG_BRIDGE_NETFILTER
David S. Millera4471892012-03-29 23:27:38 -0400301 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex)))
302 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700303#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800304 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700305 /* Case 1: indev is physical input device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800306 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700307 * netfilter_bridge) */
David S. Millera4471892012-03-29 23:27:38 -0400308 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
309 htonl(indev->ifindex)) ||
Harald Weltefbcd9232005-08-09 20:22:10 -0700310 /* this is the bridge group "brX" */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000311 /* rcu_read_lock()ed by __nf_queue */
David S. Millera4471892012-03-29 23:27:38 -0400312 nla_put_be32(skb, NFQA_IFINDEX_INDEV,
313 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
314 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700315 } else {
316 /* Case 2: indev is bridge group, we need to look for
317 * physical device (when called from ipv4) */
David S. Millera4471892012-03-29 23:27:38 -0400318 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV,
319 htonl(indev->ifindex)))
320 goto nla_put_failure;
321 if (entskb->nf_bridge && entskb->nf_bridge->physindev &&
322 nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
323 htonl(entskb->nf_bridge->physindev->ifindex)))
324 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700325 }
326#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700327 }
328
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800329 if (outdev) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700330#ifndef CONFIG_BRIDGE_NETFILTER
David S. Millera4471892012-03-29 23:27:38 -0400331 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex)))
332 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700333#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800334 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700335 /* Case 1: outdev is physical output device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800336 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700337 * netfilter_bridge) */
David S. Millera4471892012-03-29 23:27:38 -0400338 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
339 htonl(outdev->ifindex)) ||
Harald Weltefbcd9232005-08-09 20:22:10 -0700340 /* this is the bridge group "brX" */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000341 /* rcu_read_lock()ed by __nf_queue */
David S. Millera4471892012-03-29 23:27:38 -0400342 nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
343 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
344 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700345 } else {
346 /* Case 2: outdev is bridge group, we need to look for
347 * physical output device (when called from ipv4) */
David S. Millera4471892012-03-29 23:27:38 -0400348 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
349 htonl(outdev->ifindex)))
350 goto nla_put_failure;
351 if (entskb->nf_bridge && entskb->nf_bridge->physoutdev &&
352 nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
353 htonl(entskb->nf_bridge->physoutdev->ifindex)))
354 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700355 }
356#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700357 }
358
David S. Millera4471892012-03-29 23:27:38 -0400359 if (entskb->mark &&
360 nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark)))
361 goto nla_put_failure;
Harald Welte7af4cc32005-08-09 19:44:15 -0700362
Nicolas Cavallari2c38de42011-06-16 17:27:04 +0200363 if (indev && entskb->dev &&
364 entskb->mac_header != entskb->network_header) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700365 struct nfqnl_msg_packet_hw phw;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700366 int len = dev_parse_header(entskb, phw.hw_addr);
367 if (len) {
368 phw.hw_addrlen = htons(len);
David S. Millera4471892012-03-29 23:27:38 -0400369 if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw))
370 goto nla_put_failure;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700371 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700372 }
373
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700374 if (entskb->tstamp.tv64) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700375 struct nfqnl_msg_packet_timestamp ts;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700376 struct timeval tv = ktime_to_timeval(entskb->tstamp);
377 ts.sec = cpu_to_be64(tv.tv_sec);
378 ts.usec = cpu_to_be64(tv.tv_usec);
Harald Welte7af4cc32005-08-09 19:44:15 -0700379
David S. Millera4471892012-03-29 23:27:38 -0400380 if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts))
381 goto nla_put_failure;
Harald Welte7af4cc32005-08-09 19:44:15 -0700382 }
383
384 if (data_len) {
Patrick McHardydf6fb862007-09-28 14:37:03 -0700385 struct nlattr *nla;
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800386 int sz = nla_attr_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700387
Patrick McHardydf6fb862007-09-28 14:37:03 -0700388 if (skb_tailroom(skb) < nla_total_size(data_len)) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700389 printk(KERN_WARNING "nf_queue: no tailroom!\n");
David S. Miller3da07c02012-06-26 21:35:27 -0700390 kfree_skb(skb);
391 return NULL;
Harald Welte7af4cc32005-08-09 19:44:15 -0700392 }
393
Patrick McHardydf6fb862007-09-28 14:37:03 -0700394 nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));
395 nla->nla_type = NFQA_PAYLOAD;
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800396 nla->nla_len = sz;
Harald Welte7af4cc32005-08-09 19:44:15 -0700397
Patrick McHardydf6fb862007-09-28 14:37:03 -0700398 if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700399 BUG();
400 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800401
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +0200402 if (ct && nfqnl_ct_put(skb, ct, ctinfo) < 0)
403 goto nla_put_failure;
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200404
Harald Welte7af4cc32005-08-09 19:44:15 -0700405 nlh->nlmsg_len = skb->tail - old_tail;
406 return skb;
407
Patrick McHardydf6fb862007-09-28 14:37:03 -0700408nla_put_failure:
Harald Welte7af4cc32005-08-09 19:44:15 -0700409 if (skb)
410 kfree_skb(skb);
Joe Perchese87cc472012-05-13 21:56:26 +0000411 net_err_ratelimited("nf_queue: error creating packet message\n");
Harald Welte7af4cc32005-08-09 19:44:15 -0700412 return NULL;
413}
414
415static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800416nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
Harald Welte7af4cc32005-08-09 19:44:15 -0700417{
Harald Welte7af4cc32005-08-09 19:44:15 -0700418 struct sk_buff *nskb;
419 struct nfqnl_instance *queue;
Florian Westphalf1585082011-01-18 15:27:28 +0100420 int err = -ENOBUFS;
Eric Dumazet58637022011-07-19 11:44:17 +0200421 __be32 *packet_id_ptr;
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000422 int failopen = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700423
Patrick McHardy9872bec2007-12-05 01:28:50 -0800424 /* rcu_read_lock()ed by nf_hook_slow() */
425 queue = instance_lookup(queuenum);
Florian Westphalf1585082011-01-18 15:27:28 +0100426 if (!queue) {
427 err = -ESRCH;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800428 goto err_out;
Florian Westphalf1585082011-01-18 15:27:28 +0100429 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700430
Florian Westphalf1585082011-01-18 15:27:28 +0100431 if (queue->copy_mode == NFQNL_COPY_NONE) {
432 err = -EINVAL;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800433 goto err_out;
Florian Westphalf1585082011-01-18 15:27:28 +0100434 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700435
Eric Dumazet58637022011-07-19 11:44:17 +0200436 nskb = nfqnl_build_packet_message(queue, entry, &packet_id_ptr);
Florian Westphalf1585082011-01-18 15:27:28 +0100437 if (nskb == NULL) {
438 err = -ENOMEM;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800439 goto err_out;
Florian Westphalf1585082011-01-18 15:27:28 +0100440 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700441 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800442
Florian Westphalf1585082011-01-18 15:27:28 +0100443 if (!queue->peer_pid) {
444 err = -EINVAL;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800445 goto err_out_free_nskb;
Florian Westphalf1585082011-01-18 15:27:28 +0100446 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700447 if (queue->queue_total >= queue->queue_maxlen) {
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000448 if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
449 failopen = 1;
450 err = 0;
451 } else {
452 queue->queue_dropped++;
453 net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n",
454 queue->queue_total);
455 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700456 goto err_out_free_nskb;
457 }
Eric Dumazet58637022011-07-19 11:44:17 +0200458 entry->id = ++queue->id_sequence;
459 *packet_id_ptr = htonl(entry->id);
Harald Welte7af4cc32005-08-09 19:44:15 -0700460
461 /* nfnetlink_unicast will either free the nskb or add it to a socket */
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100462 err = nfnetlink_unicast(nskb, &init_net, queue->peer_pid, MSG_DONTWAIT);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800463 if (err < 0) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800464 queue->queue_user_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700465 goto err_out_unlock;
466 }
467
468 __enqueue_entry(queue, entry);
469
470 spin_unlock_bh(&queue->lock);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800471 return 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700472
473err_out_free_nskb:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800474 kfree_skb(nskb);
Harald Welte7af4cc32005-08-09 19:44:15 -0700475err_out_unlock:
476 spin_unlock_bh(&queue->lock);
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000477 if (failopen)
478 nf_reinject(entry, NF_ACCEPT);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800479err_out:
Florian Westphalf1585082011-01-18 15:27:28 +0100480 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700481}
482
483static int
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200484nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e, int diff)
Harald Welte7af4cc32005-08-09 19:44:15 -0700485{
Patrick McHardye2b58a62008-02-19 17:17:52 -0800486 struct sk_buff *nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700487
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800488 if (diff < 0) {
489 if (pskb_trim(e->skb, data_len))
490 return -ENOMEM;
491 } else if (diff > 0) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700492 if (data_len > 0xFFFF)
493 return -EINVAL;
494 if (diff > skb_tailroom(e->skb)) {
Arnaud Ebalard9a732ed2008-04-29 03:16:34 -0700495 nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
496 diff, GFP_ATOMIC);
Patrick McHardye2b58a62008-02-19 17:17:52 -0800497 if (!nskb) {
Patrick McHardy1158ba22006-08-22 00:32:47 -0700498 printk(KERN_WARNING "nf_queue: OOM "
Harald Welte7af4cc32005-08-09 19:44:15 -0700499 "in mangle, dropping packet\n");
Patrick McHardye2b58a62008-02-19 17:17:52 -0800500 return -ENOMEM;
Harald Welte7af4cc32005-08-09 19:44:15 -0700501 }
Patrick McHardye2b58a62008-02-19 17:17:52 -0800502 kfree_skb(e->skb);
503 e->skb = nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700504 }
505 skb_put(e->skb, diff);
506 }
Herbert Xu37d41872007-10-14 00:39:18 -0700507 if (!skb_make_writable(e->skb, data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700508 return -ENOMEM;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300509 skb_copy_to_linear_data(e->skb, data, data_len);
Patrick McHardye7dfb092005-09-06 15:10:00 -0700510 e->skb->ip_summed = CHECKSUM_NONE;
Harald Welte7af4cc32005-08-09 19:44:15 -0700511 return 0;
512}
513
Harald Welte7af4cc32005-08-09 19:44:15 -0700514static int
515nfqnl_set_mode(struct nfqnl_instance *queue,
516 unsigned char mode, unsigned int range)
517{
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800518 int status = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700519
520 spin_lock_bh(&queue->lock);
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800521 switch (mode) {
522 case NFQNL_COPY_NONE:
523 case NFQNL_COPY_META:
524 queue->copy_mode = mode;
525 queue->copy_range = 0;
526 break;
527
528 case NFQNL_COPY_PACKET:
529 queue->copy_mode = mode;
530 /* we're using struct nlattr which has 16bit nla_len */
531 if (range > 0xffff)
532 queue->copy_range = 0xffff;
533 else
534 queue->copy_range = range;
535 break;
536
537 default:
538 status = -EINVAL;
539
540 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700541 spin_unlock_bh(&queue->lock);
542
543 return status;
544}
545
546static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800547dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700548{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800549 if (entry->indev)
550 if (entry->indev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700551 return 1;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800552 if (entry->outdev)
553 if (entry->outdev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700554 return 1;
Patrick McHardyef47c6a72006-06-27 03:01:48 -0700555#ifdef CONFIG_BRIDGE_NETFILTER
556 if (entry->skb->nf_bridge) {
557 if (entry->skb->nf_bridge->physindev &&
558 entry->skb->nf_bridge->physindev->ifindex == ifindex)
559 return 1;
560 if (entry->skb->nf_bridge->physoutdev &&
561 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
562 return 1;
563 }
564#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700565 return 0;
566}
567
568/* drop all packets with either indev or outdev == ifindex from all queue
569 * instances */
570static void
571nfqnl_dev_drop(int ifindex)
572{
573 int i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800574
Patrick McHardy9872bec2007-12-05 01:28:50 -0800575 rcu_read_lock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700576
Patrick McHardy9872bec2007-12-05 01:28:50 -0800577 for (i = 0; i < INSTANCE_BUCKETS; i++) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700578 struct hlist_node *tmp;
579 struct nfqnl_instance *inst;
580 struct hlist_head *head = &instance_table[i];
581
Patrick McHardy9872bec2007-12-05 01:28:50 -0800582 hlist_for_each_entry_rcu(inst, tmp, head, hlist)
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800583 nfqnl_flush(inst, dev_cmp, ifindex);
Harald Welte7af4cc32005-08-09 19:44:15 -0700584 }
585
Patrick McHardy9872bec2007-12-05 01:28:50 -0800586 rcu_read_unlock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700587}
588
589#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
590
591static int
592nfqnl_rcv_dev_event(struct notifier_block *this,
593 unsigned long event, void *ptr)
594{
595 struct net_device *dev = ptr;
596
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700597 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200598 return NOTIFY_DONE;
599
Harald Welte7af4cc32005-08-09 19:44:15 -0700600 /* Drop any packets associated with the downed device */
601 if (event == NETDEV_DOWN)
602 nfqnl_dev_drop(dev->ifindex);
603 return NOTIFY_DONE;
604}
605
606static struct notifier_block nfqnl_dev_notifier = {
607 .notifier_call = nfqnl_rcv_dev_event,
608};
609
610static int
611nfqnl_rcv_nl_event(struct notifier_block *this,
612 unsigned long event, void *ptr)
613{
614 struct netlink_notify *n = ptr;
615
Patrick McHardydee58172009-11-06 17:04:00 +0100616 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700617 int i;
618
619 /* destroy all instances for this pid */
Patrick McHardy9872bec2007-12-05 01:28:50 -0800620 spin_lock(&instances_lock);
621 for (i = 0; i < INSTANCE_BUCKETS; i++) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700622 struct hlist_node *tmp, *t2;
623 struct nfqnl_instance *inst;
624 struct hlist_head *head = &instance_table[i];
625
626 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200627 if ((n->net == &init_net) &&
628 (n->pid == inst->peer_pid))
Harald Welte7af4cc32005-08-09 19:44:15 -0700629 __instance_destroy(inst);
630 }
631 }
Patrick McHardy9872bec2007-12-05 01:28:50 -0800632 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700633 }
634 return NOTIFY_DONE;
635}
636
637static struct notifier_block nfqnl_rtnl_notifier = {
638 .notifier_call = nfqnl_rcv_nl_event,
639};
640
Patrick McHardy5bf75852007-09-28 14:39:26 -0700641static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
642 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
643 [NFQA_MARK] = { .type = NLA_U32 },
644 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200645 [NFQA_CT] = { .type = NLA_UNSPEC },
Harald Welte838ab632005-08-09 19:50:45 -0700646};
647
Florian Westphal97d32cf2011-07-19 11:46:33 +0200648static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = {
649 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
650 [NFQA_MARK] = { .type = NLA_U32 },
651};
652
653static struct nfqnl_instance *verdict_instance_lookup(u16 queue_num, int nlpid)
654{
655 struct nfqnl_instance *queue;
656
657 queue = instance_lookup(queue_num);
658 if (!queue)
659 return ERR_PTR(-ENODEV);
660
661 if (queue->peer_pid != nlpid)
662 return ERR_PTR(-EPERM);
663
664 return queue;
665}
666
667static struct nfqnl_msg_verdict_hdr*
668verdicthdr_get(const struct nlattr * const nfqa[])
669{
670 struct nfqnl_msg_verdict_hdr *vhdr;
671 unsigned int verdict;
672
673 if (!nfqa[NFQA_VERDICT_HDR])
674 return NULL;
675
676 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
Florian Westphalc6675232011-08-30 15:01:20 +0200677 verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK;
678 if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN)
Florian Westphal97d32cf2011-07-19 11:46:33 +0200679 return NULL;
680 return vhdr;
681}
682
683static int nfq_id_after(unsigned int id, unsigned int max)
684{
685 return (int)(id - max) > 0;
686}
687
688static int
689nfqnl_recv_verdict_batch(struct sock *ctnl, struct sk_buff *skb,
690 const struct nlmsghdr *nlh,
691 const struct nlattr * const nfqa[])
692{
David S. Miller3da07c02012-06-26 21:35:27 -0700693 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Florian Westphal97d32cf2011-07-19 11:46:33 +0200694 struct nf_queue_entry *entry, *tmp;
695 unsigned int verdict, maxid;
696 struct nfqnl_msg_verdict_hdr *vhdr;
697 struct nfqnl_instance *queue;
698 LIST_HEAD(batch_list);
699 u16 queue_num = ntohs(nfmsg->res_id);
700
701 queue = verdict_instance_lookup(queue_num, NETLINK_CB(skb).pid);
702 if (IS_ERR(queue))
703 return PTR_ERR(queue);
704
705 vhdr = verdicthdr_get(nfqa);
706 if (!vhdr)
707 return -EINVAL;
708
709 verdict = ntohl(vhdr->verdict);
710 maxid = ntohl(vhdr->id);
711
712 spin_lock_bh(&queue->lock);
713
714 list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) {
715 if (nfq_id_after(entry->id, maxid))
716 break;
717 __dequeue_entry(queue, entry);
718 list_add_tail(&entry->list, &batch_list);
719 }
720
721 spin_unlock_bh(&queue->lock);
722
723 if (list_empty(&batch_list))
724 return -ENOENT;
725
726 list_for_each_entry_safe(entry, tmp, &batch_list, list) {
727 if (nfqa[NFQA_MARK])
728 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
729 nf_reinject(entry, verdict);
730 }
731 return 0;
732}
733
Harald Welte7af4cc32005-08-09 19:44:15 -0700734static int
735nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200736 const struct nlmsghdr *nlh,
737 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700738{
David S. Miller3da07c02012-06-26 21:35:27 -0700739 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Harald Welte7af4cc32005-08-09 19:44:15 -0700740 u_int16_t queue_num = ntohs(nfmsg->res_id);
741
742 struct nfqnl_msg_verdict_hdr *vhdr;
743 struct nfqnl_instance *queue;
744 unsigned int verdict;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800745 struct nf_queue_entry *entry;
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200746 enum ip_conntrack_info uninitialized_var(ctinfo);
747 struct nf_conn *ct = NULL;
Harald Welte7af4cc32005-08-09 19:44:15 -0700748
Patrick McHardy9872bec2007-12-05 01:28:50 -0800749 queue = instance_lookup(queue_num);
Eric Dumazet84a797d2011-07-18 16:08:27 +0200750 if (!queue)
Harald Welte7af4cc32005-08-09 19:44:15 -0700751
Florian Westphal97d32cf2011-07-19 11:46:33 +0200752 queue = verdict_instance_lookup(queue_num, NETLINK_CB(skb).pid);
753 if (IS_ERR(queue))
754 return PTR_ERR(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700755
Florian Westphal97d32cf2011-07-19 11:46:33 +0200756 vhdr = verdicthdr_get(nfqa);
757 if (!vhdr)
Eric Dumazet84a797d2011-07-18 16:08:27 +0200758 return -EINVAL;
Harald Welte7af4cc32005-08-09 19:44:15 -0700759
Harald Welte7af4cc32005-08-09 19:44:15 -0700760 verdict = ntohl(vhdr->verdict);
761
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800762 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
Eric Dumazet84a797d2011-07-18 16:08:27 +0200763 if (entry == NULL)
764 return -ENOENT;
Harald Welte7af4cc32005-08-09 19:44:15 -0700765
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200766 rcu_read_lock();
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +0200767 if (nfqa[NFQA_CT] && (queue->flags & NFQA_CFG_F_CONNTRACK))
768 ct = nfqnl_ct_parse(entry->skb, nfqa[NFQA_CT], &ctinfo);
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200769
Patrick McHardydf6fb862007-09-28 14:37:03 -0700770 if (nfqa[NFQA_PAYLOAD]) {
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200771 u16 payload_len = nla_len(nfqa[NFQA_PAYLOAD]);
772 int diff = payload_len - entry->skb->len;
773
Patrick McHardydf6fb862007-09-28 14:37:03 -0700774 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200775 payload_len, entry, diff) < 0)
Harald Welte7af4cc32005-08-09 19:44:15 -0700776 verdict = NF_DROP;
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200777
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +0200778 if (ct)
779 nfqnl_ct_seq_adjust(skb, ct, ctinfo, diff);
Harald Welte7af4cc32005-08-09 19:44:15 -0700780 }
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200781 rcu_read_unlock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700782
Patrick McHardydf6fb862007-09-28 14:37:03 -0700783 if (nfqa[NFQA_MARK])
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800784 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800785
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800786 nf_reinject(entry, verdict);
Harald Welte7af4cc32005-08-09 19:44:15 -0700787 return 0;
788}
789
790static int
791nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200792 const struct nlmsghdr *nlh,
793 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700794{
795 return -ENOTSUPP;
796}
797
Patrick McHardy5bf75852007-09-28 14:39:26 -0700798static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
799 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
800 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
Harald Welte838ab632005-08-09 19:50:45 -0700801};
802
Patrick McHardye3ac5292007-12-05 01:23:57 -0800803static const struct nf_queue_handler nfqh = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700804 .name = "nf_queue",
805 .outfn = &nfqnl_enqueue_packet,
806};
807
Harald Welte7af4cc32005-08-09 19:44:15 -0700808static int
809nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200810 const struct nlmsghdr *nlh,
811 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700812{
David S. Miller3da07c02012-06-26 21:35:27 -0700813 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Harald Welte7af4cc32005-08-09 19:44:15 -0700814 u_int16_t queue_num = ntohs(nfmsg->res_id);
815 struct nfqnl_instance *queue;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800816 struct nfqnl_msg_config_cmd *cmd = NULL;
Harald Welte838ab632005-08-09 19:50:45 -0700817 int ret = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700818
Patrick McHardy9872bec2007-12-05 01:28:50 -0800819 if (nfqa[NFQA_CFG_CMD]) {
820 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
821
822 /* Commands without queue context - might sleep */
823 switch (cmd->command) {
824 case NFQNL_CFG_CMD_PF_BIND:
Patrick McHardy914afea2008-03-10 16:44:36 -0700825 return nf_register_queue_handler(ntohs(cmd->pf),
826 &nfqh);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800827 case NFQNL_CFG_CMD_PF_UNBIND:
Patrick McHardy914afea2008-03-10 16:44:36 -0700828 return nf_unregister_queue_handler(ntohs(cmd->pf),
829 &nfqh);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800830 }
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800831 }
832
Patrick McHardy9872bec2007-12-05 01:28:50 -0800833 rcu_read_lock();
834 queue = instance_lookup(queue_num);
835 if (queue && queue->peer_pid != NETLINK_CB(skb).pid) {
836 ret = -EPERM;
837 goto err_out_unlock;
838 }
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800839
Patrick McHardy9872bec2007-12-05 01:28:50 -0800840 if (cmd != NULL) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700841 switch (cmd->command) {
842 case NFQNL_CFG_CMD_BIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800843 if (queue) {
844 ret = -EBUSY;
845 goto err_out_unlock;
846 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700847 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800848 if (IS_ERR(queue)) {
849 ret = PTR_ERR(queue);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800850 goto err_out_unlock;
851 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700852 break;
853 case NFQNL_CFG_CMD_UNBIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800854 if (!queue) {
855 ret = -ENODEV;
856 goto err_out_unlock;
857 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700858 instance_destroy(queue);
859 break;
860 case NFQNL_CFG_CMD_PF_BIND:
Harald Welte7af4cc32005-08-09 19:44:15 -0700861 case NFQNL_CFG_CMD_PF_UNBIND:
Harald Welte7af4cc32005-08-09 19:44:15 -0700862 break;
863 default:
Patrick McHardycd21f0a2007-12-17 22:40:19 -0800864 ret = -ENOTSUPP;
Harald Welte838ab632005-08-09 19:50:45 -0700865 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700866 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700867 }
868
Patrick McHardydf6fb862007-09-28 14:37:03 -0700869 if (nfqa[NFQA_CFG_PARAMS]) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700870 struct nfqnl_msg_config_params *params;
Harald Welte7af4cc32005-08-09 19:44:15 -0700871
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800872 if (!queue) {
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800873 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800874 goto err_out_unlock;
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800875 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700876 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700877 nfqnl_set_mode(queue, params->copy_mode,
878 ntohl(params->copy_range));
879 }
880
Patrick McHardydf6fb862007-09-28 14:37:03 -0700881 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
Eric Leblond829e17a2006-11-29 02:35:33 +0100882 __be32 *queue_maxlen;
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800883
884 if (!queue) {
885 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800886 goto err_out_unlock;
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800887 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700888 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
Eric Leblond829e17a2006-11-29 02:35:33 +0100889 spin_lock_bh(&queue->lock);
890 queue->queue_maxlen = ntohl(*queue_maxlen);
891 spin_unlock_bh(&queue->lock);
892 }
893
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000894 if (nfqa[NFQA_CFG_FLAGS]) {
895 __u32 flags, mask;
896
897 if (!queue) {
898 ret = -ENODEV;
899 goto err_out_unlock;
900 }
901
902 if (!nfqa[NFQA_CFG_MASK]) {
903 /* A mask is needed to specify which flags are being
904 * changed.
905 */
906 ret = -EINVAL;
907 goto err_out_unlock;
908 }
909
910 flags = ntohl(nla_get_be32(nfqa[NFQA_CFG_FLAGS]));
911 mask = ntohl(nla_get_be32(nfqa[NFQA_CFG_MASK]));
912
Krishna Kumar46ba5a22012-06-27 00:59:56 +0000913 if (flags >= NFQA_CFG_F_MAX) {
914 ret = -EOPNOTSUPP;
915 goto err_out_unlock;
916 }
917
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000918 spin_lock_bh(&queue->lock);
919 queue->flags &= ~mask;
920 queue->flags |= flags & mask;
921 spin_unlock_bh(&queue->lock);
922 }
923
Patrick McHardy9872bec2007-12-05 01:28:50 -0800924err_out_unlock:
925 rcu_read_unlock();
Harald Welte838ab632005-08-09 19:50:45 -0700926 return ret;
Harald Welte7af4cc32005-08-09 19:44:15 -0700927}
928
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700929static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
Eric Dumazet84a797d2011-07-18 16:08:27 +0200930 [NFQNL_MSG_PACKET] = { .call_rcu = nfqnl_recv_unsupp,
Harald Welte37d2e7a2005-11-14 15:24:59 -0800931 .attr_count = NFQA_MAX, },
Eric Dumazet84a797d2011-07-18 16:08:27 +0200932 [NFQNL_MSG_VERDICT] = { .call_rcu = nfqnl_recv_verdict,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700933 .attr_count = NFQA_MAX,
934 .policy = nfqa_verdict_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700935 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700936 .attr_count = NFQA_CFG_MAX,
937 .policy = nfqa_cfg_policy },
Florian Westphal97d32cf2011-07-19 11:46:33 +0200938 [NFQNL_MSG_VERDICT_BATCH]={ .call_rcu = nfqnl_recv_verdict_batch,
939 .attr_count = NFQA_MAX,
940 .policy = nfqa_verdict_batch_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700941};
942
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700943static const struct nfnetlink_subsystem nfqnl_subsys = {
Harald Welte7af4cc32005-08-09 19:44:15 -0700944 .name = "nf_queue",
945 .subsys_id = NFNL_SUBSYS_QUEUE,
946 .cb_count = NFQNL_MSG_MAX,
Harald Welte7af4cc32005-08-09 19:44:15 -0700947 .cb = nfqnl_cb,
948};
949
Harald Welte838ab632005-08-09 19:50:45 -0700950#ifdef CONFIG_PROC_FS
951struct iter_state {
952 unsigned int bucket;
953};
954
955static struct hlist_node *get_first(struct seq_file *seq)
956{
957 struct iter_state *st = seq->private;
958
959 if (!st)
960 return NULL;
961
962 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
963 if (!hlist_empty(&instance_table[st->bucket]))
964 return instance_table[st->bucket].first;
965 }
966 return NULL;
967}
968
969static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
970{
971 struct iter_state *st = seq->private;
972
973 h = h->next;
974 while (!h) {
975 if (++st->bucket >= INSTANCE_BUCKETS)
976 return NULL;
977
978 h = instance_table[st->bucket].first;
979 }
980 return h;
981}
982
983static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
984{
985 struct hlist_node *head;
986 head = get_first(seq);
987
988 if (head)
989 while (pos && (head = get_next(seq, head)))
990 pos--;
991 return pos ? NULL : head;
992}
993
994static void *seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800995 __acquires(instances_lock)
Harald Welte838ab632005-08-09 19:50:45 -0700996{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800997 spin_lock(&instances_lock);
Harald Welte838ab632005-08-09 19:50:45 -0700998 return get_idx(seq, *pos);
999}
1000
1001static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1002{
1003 (*pos)++;
1004 return get_next(s, v);
1005}
1006
1007static void seq_stop(struct seq_file *s, void *v)
Eric Dumazetca7c48c2008-01-31 03:53:27 -08001008 __releases(instances_lock)
Harald Welte838ab632005-08-09 19:50:45 -07001009{
Patrick McHardy9872bec2007-12-05 01:28:50 -08001010 spin_unlock(&instances_lock);
Harald Welte838ab632005-08-09 19:50:45 -07001011}
1012
1013static int seq_show(struct seq_file *s, void *v)
1014{
1015 const struct nfqnl_instance *inst = v;
1016
1017 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
1018 inst->queue_num,
1019 inst->peer_pid, inst->queue_total,
1020 inst->copy_mode, inst->copy_range,
1021 inst->queue_dropped, inst->queue_user_dropped,
Eric Dumazet58637022011-07-19 11:44:17 +02001022 inst->id_sequence, 1);
Harald Welte838ab632005-08-09 19:50:45 -07001023}
1024
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001025static const struct seq_operations nfqnl_seq_ops = {
Harald Welte838ab632005-08-09 19:50:45 -07001026 .start = seq_start,
1027 .next = seq_next,
1028 .stop = seq_stop,
1029 .show = seq_show,
1030};
1031
1032static int nfqnl_open(struct inode *inode, struct file *file)
1033{
Pavel Emelyanove2da5912007-10-10 02:29:58 -07001034 return seq_open_private(file, &nfqnl_seq_ops,
1035 sizeof(struct iter_state));
Harald Welte838ab632005-08-09 19:50:45 -07001036}
1037
Arjan van de Venda7071d2007-02-12 00:55:36 -08001038static const struct file_operations nfqnl_file_ops = {
Harald Welte838ab632005-08-09 19:50:45 -07001039 .owner = THIS_MODULE,
1040 .open = nfqnl_open,
1041 .read = seq_read,
1042 .llseek = seq_lseek,
1043 .release = seq_release_private,
1044};
1045
1046#endif /* PROC_FS */
1047
Patrick McHardy32292a72006-04-06 14:11:30 -07001048static int __init nfnetlink_queue_init(void)
Harald Welte7af4cc32005-08-09 19:44:15 -07001049{
Harald Welte838ab632005-08-09 19:50:45 -07001050 int i, status = -ENOMEM;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001051
Harald Welte838ab632005-08-09 19:50:45 -07001052 for (i = 0; i < INSTANCE_BUCKETS; i++)
1053 INIT_HLIST_HEAD(&instance_table[i]);
1054
Harald Welte7af4cc32005-08-09 19:44:15 -07001055 netlink_register_notifier(&nfqnl_rtnl_notifier);
1056 status = nfnetlink_subsys_register(&nfqnl_subsys);
1057 if (status < 0) {
1058 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
1059 goto cleanup_netlink_notifier;
1060 }
1061
Harald Welte838ab632005-08-09 19:50:45 -07001062#ifdef CONFIG_PROC_FS
Denis V. Lunev8eeee8b2008-03-27 16:55:53 -07001063 if (!proc_create("nfnetlink_queue", 0440,
1064 proc_net_netfilter, &nfqnl_file_ops))
Harald Welte838ab632005-08-09 19:50:45 -07001065 goto cleanup_subsys;
Harald Welte838ab632005-08-09 19:50:45 -07001066#endif
1067
Harald Welte7af4cc32005-08-09 19:44:15 -07001068 register_netdevice_notifier(&nfqnl_dev_notifier);
1069 return status;
1070
Harald Welte838ab632005-08-09 19:50:45 -07001071#ifdef CONFIG_PROC_FS
1072cleanup_subsys:
Harald Welte7af4cc32005-08-09 19:44:15 -07001073 nfnetlink_subsys_unregister(&nfqnl_subsys);
Patrick McHardy32292a72006-04-06 14:11:30 -07001074#endif
Harald Welte7af4cc32005-08-09 19:44:15 -07001075cleanup_netlink_notifier:
1076 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1077 return status;
1078}
1079
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001080static void __exit nfnetlink_queue_fini(void)
Harald Welte7af4cc32005-08-09 19:44:15 -07001081{
Patrick McHardy32292a72006-04-06 14:11:30 -07001082 nf_unregister_queue_handlers(&nfqh);
1083 unregister_netdevice_notifier(&nfqnl_dev_notifier);
1084#ifdef CONFIG_PROC_FS
1085 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
1086#endif
1087 nfnetlink_subsys_unregister(&nfqnl_subsys);
1088 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
Jesper Dangaard Brouer67137f32009-06-08 03:11:33 +00001089
1090 rcu_barrier(); /* Wait for completion of call_rcu()'s */
Harald Welte7af4cc32005-08-09 19:44:15 -07001091}
1092
1093MODULE_DESCRIPTION("netfilter packet queue handler");
1094MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1095MODULE_LICENSE("GPL");
1096MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1097
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001098module_init(nfnetlink_queue_init);
1099module_exit(nfnetlink_queue_fini);