blob: e12d44e75b21f79f266b3dc580fd283aa81b58f9 [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{
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 *
Eric W. Biederman15e47302012-09-07 20:12:54 +000095instance_create(u_int16_t queue_num, int portid)
Harald Welte7af4cc32005-08-09 19:44:15 -070096{
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;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000114 inst->peer_portid = portid;
Harald Welte7af4cc32005-08-09 19:44:15 -0700115 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;
Pablo Neira Ayuso6ee584b2012-09-24 14:52:12 +0200228 size_t data_len = 0, cap_len = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700229 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))
Pablo Neira Ayuso6ee584b2012-09-24 14:52:12 +0200250 + nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp)
251 + nla_total_size(sizeof(u_int32_t))); /* cap_len */
Harald Welte7af4cc32005-08-09 19:44:15 -0700252
Patrick McHardy02f014d2007-12-05 01:26:33 -0800253 outdev = entry->outdev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800254
Eric Dumazetc463ac92010-06-09 18:07:06 +0200255 switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700256 case NFQNL_COPY_META:
257 case NFQNL_COPY_NONE:
Harald Welte7af4cc32005-08-09 19:44:15 -0700258 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800259
Harald Welte7af4cc32005-08-09 19:44:15 -0700260 case NFQNL_COPY_PACKET:
Herbert Xue9f13ca2010-04-08 14:54:35 +0200261 if (entskb->ip_summed == CHECKSUM_PARTIAL &&
Eric Dumazetc463ac92010-06-09 18:07:06 +0200262 skb_checksum_help(entskb))
Patrick McHardye7dfb092005-09-06 15:10:00 -0700263 return NULL;
Eric Dumazetc463ac92010-06-09 18:07:06 +0200264
265 data_len = ACCESS_ONCE(queue->copy_range);
266 if (data_len == 0 || data_len > entskb->len)
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800267 data_len = entskb->len;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800268
Patrick McHardydf6fb862007-09-28 14:37:03 -0700269 size += nla_total_size(data_len);
Pablo Neira Ayuso6ee584b2012-09-24 14:52:12 +0200270 cap_len = entskb->len;
Harald Welte7af4cc32005-08-09 19:44:15 -0700271 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700272 }
273
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +0200274 if (queue->flags & NFQA_CFG_F_CONNTRACK)
275 ct = nfqnl_ct_get(entskb, &size, &ctinfo);
Harald Welte7af4cc32005-08-09 19:44:15 -0700276
277 skb = alloc_skb(size, GFP_ATOMIC);
278 if (!skb)
David S. Miller3da07c02012-06-26 21:35:27 -0700279 return NULL;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800280
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700281 old_tail = skb->tail;
David S. Miller3da07c02012-06-26 21:35:27 -0700282 nlh = nlmsg_put(skb, 0, 0,
Harald Welte7af4cc32005-08-09 19:44:15 -0700283 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
David S. Miller3da07c02012-06-26 21:35:27 -0700284 sizeof(struct nfgenmsg), 0);
285 if (!nlh) {
286 kfree_skb(skb);
287 return NULL;
288 }
289 nfmsg = nlmsg_data(nlh);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800290 nfmsg->nfgen_family = entry->pf;
Harald Welte7af4cc32005-08-09 19:44:15 -0700291 nfmsg->version = NFNETLINK_V0;
292 nfmsg->res_id = htons(queue->queue_num);
293
Eric Dumazet58637022011-07-19 11:44:17 +0200294 nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg));
295 pmsg = nla_data(nla);
296 pmsg->hw_protocol = entskb->protocol;
297 pmsg->hook = entry->hook;
298 *packet_id_ptr = &pmsg->packet_id;
Harald Welte7af4cc32005-08-09 19:44:15 -0700299
Patrick McHardy02f014d2007-12-05 01:26:33 -0800300 indev = entry->indev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800301 if (indev) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700302#ifndef CONFIG_BRIDGE_NETFILTER
David S. Millera4471892012-03-29 23:27:38 -0400303 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex)))
304 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700305#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800306 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700307 /* Case 1: indev is physical input device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800308 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700309 * netfilter_bridge) */
David S. Millera4471892012-03-29 23:27:38 -0400310 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
311 htonl(indev->ifindex)) ||
Harald Weltefbcd9232005-08-09 20:22:10 -0700312 /* this is the bridge group "brX" */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000313 /* rcu_read_lock()ed by __nf_queue */
David S. Millera4471892012-03-29 23:27:38 -0400314 nla_put_be32(skb, NFQA_IFINDEX_INDEV,
315 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
316 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700317 } else {
318 /* Case 2: indev is bridge group, we need to look for
319 * physical device (when called from ipv4) */
David S. Millera4471892012-03-29 23:27:38 -0400320 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV,
321 htonl(indev->ifindex)))
322 goto nla_put_failure;
323 if (entskb->nf_bridge && entskb->nf_bridge->physindev &&
324 nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
325 htonl(entskb->nf_bridge->physindev->ifindex)))
326 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700327 }
328#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700329 }
330
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800331 if (outdev) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700332#ifndef CONFIG_BRIDGE_NETFILTER
David S. Millera4471892012-03-29 23:27:38 -0400333 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex)))
334 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700335#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800336 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700337 /* Case 1: outdev is physical output device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800338 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700339 * netfilter_bridge) */
David S. Millera4471892012-03-29 23:27:38 -0400340 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
341 htonl(outdev->ifindex)) ||
Harald Weltefbcd9232005-08-09 20:22:10 -0700342 /* this is the bridge group "brX" */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000343 /* rcu_read_lock()ed by __nf_queue */
David S. Millera4471892012-03-29 23:27:38 -0400344 nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
345 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
346 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700347 } else {
348 /* Case 2: outdev is bridge group, we need to look for
349 * physical output device (when called from ipv4) */
David S. Millera4471892012-03-29 23:27:38 -0400350 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
351 htonl(outdev->ifindex)))
352 goto nla_put_failure;
353 if (entskb->nf_bridge && entskb->nf_bridge->physoutdev &&
354 nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
355 htonl(entskb->nf_bridge->physoutdev->ifindex)))
356 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700357 }
358#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700359 }
360
David S. Millera4471892012-03-29 23:27:38 -0400361 if (entskb->mark &&
362 nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark)))
363 goto nla_put_failure;
Harald Welte7af4cc32005-08-09 19:44:15 -0700364
Nicolas Cavallari2c38de42011-06-16 17:27:04 +0200365 if (indev && entskb->dev &&
366 entskb->mac_header != entskb->network_header) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700367 struct nfqnl_msg_packet_hw phw;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700368 int len = dev_parse_header(entskb, phw.hw_addr);
369 if (len) {
370 phw.hw_addrlen = htons(len);
David S. Millera4471892012-03-29 23:27:38 -0400371 if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw))
372 goto nla_put_failure;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700373 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700374 }
375
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700376 if (entskb->tstamp.tv64) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700377 struct nfqnl_msg_packet_timestamp ts;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700378 struct timeval tv = ktime_to_timeval(entskb->tstamp);
379 ts.sec = cpu_to_be64(tv.tv_sec);
380 ts.usec = cpu_to_be64(tv.tv_usec);
Harald Welte7af4cc32005-08-09 19:44:15 -0700381
David S. Millera4471892012-03-29 23:27:38 -0400382 if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts))
383 goto nla_put_failure;
Harald Welte7af4cc32005-08-09 19:44:15 -0700384 }
385
386 if (data_len) {
Patrick McHardydf6fb862007-09-28 14:37:03 -0700387 struct nlattr *nla;
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800388 int sz = nla_attr_size(data_len);
Harald Welte7af4cc32005-08-09 19:44:15 -0700389
Patrick McHardydf6fb862007-09-28 14:37:03 -0700390 if (skb_tailroom(skb) < nla_total_size(data_len)) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700391 printk(KERN_WARNING "nf_queue: no tailroom!\n");
David S. Miller3da07c02012-06-26 21:35:27 -0700392 kfree_skb(skb);
393 return NULL;
Harald Welte7af4cc32005-08-09 19:44:15 -0700394 }
395
Patrick McHardydf6fb862007-09-28 14:37:03 -0700396 nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));
397 nla->nla_type = NFQA_PAYLOAD;
Eric Dumazetca7c48c2008-01-31 03:53:27 -0800398 nla->nla_len = sz;
Harald Welte7af4cc32005-08-09 19:44:15 -0700399
Patrick McHardydf6fb862007-09-28 14:37:03 -0700400 if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700401 BUG();
402 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800403
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +0200404 if (ct && nfqnl_ct_put(skb, ct, ctinfo) < 0)
405 goto nla_put_failure;
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200406
Pablo Neira Ayuso6ee584b2012-09-24 14:52:12 +0200407 if (cap_len > 0 && nla_put_be32(skb, NFQA_CAP_LEN, htonl(cap_len)))
408 goto nla_put_failure;
409
Harald Welte7af4cc32005-08-09 19:44:15 -0700410 nlh->nlmsg_len = skb->tail - old_tail;
411 return skb;
412
Patrick McHardydf6fb862007-09-28 14:37:03 -0700413nla_put_failure:
Wei Yongjuna6729952012-08-28 03:14:15 +0000414 kfree_skb(skb);
Joe Perchese87cc472012-05-13 21:56:26 +0000415 net_err_ratelimited("nf_queue: error creating packet message\n");
Harald Welte7af4cc32005-08-09 19:44:15 -0700416 return NULL;
417}
418
419static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800420nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
Harald Welte7af4cc32005-08-09 19:44:15 -0700421{
Harald Welte7af4cc32005-08-09 19:44:15 -0700422 struct sk_buff *nskb;
423 struct nfqnl_instance *queue;
Florian Westphalf1585082011-01-18 15:27:28 +0100424 int err = -ENOBUFS;
Eric Dumazet58637022011-07-19 11:44:17 +0200425 __be32 *packet_id_ptr;
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000426 int failopen = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700427
Patrick McHardy9872bec2007-12-05 01:28:50 -0800428 /* rcu_read_lock()ed by nf_hook_slow() */
429 queue = instance_lookup(queuenum);
Florian Westphalf1585082011-01-18 15:27:28 +0100430 if (!queue) {
431 err = -ESRCH;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800432 goto err_out;
Florian Westphalf1585082011-01-18 15:27:28 +0100433 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700434
Florian Westphalf1585082011-01-18 15:27:28 +0100435 if (queue->copy_mode == NFQNL_COPY_NONE) {
436 err = -EINVAL;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800437 goto err_out;
Florian Westphalf1585082011-01-18 15:27:28 +0100438 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700439
Eric Dumazet58637022011-07-19 11:44:17 +0200440 nskb = nfqnl_build_packet_message(queue, entry, &packet_id_ptr);
Florian Westphalf1585082011-01-18 15:27:28 +0100441 if (nskb == NULL) {
442 err = -ENOMEM;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800443 goto err_out;
Florian Westphalf1585082011-01-18 15:27:28 +0100444 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700445 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800446
Eric W. Biederman15e47302012-09-07 20:12:54 +0000447 if (!queue->peer_portid) {
Florian Westphalf1585082011-01-18 15:27:28 +0100448 err = -EINVAL;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800449 goto err_out_free_nskb;
Florian Westphalf1585082011-01-18 15:27:28 +0100450 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700451 if (queue->queue_total >= queue->queue_maxlen) {
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000452 if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
453 failopen = 1;
454 err = 0;
455 } else {
456 queue->queue_dropped++;
457 net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n",
458 queue->queue_total);
459 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700460 goto err_out_free_nskb;
461 }
Eric Dumazet58637022011-07-19 11:44:17 +0200462 entry->id = ++queue->id_sequence;
463 *packet_id_ptr = htonl(entry->id);
Harald Welte7af4cc32005-08-09 19:44:15 -0700464
465 /* nfnetlink_unicast will either free the nskb or add it to a socket */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000466 err = nfnetlink_unicast(nskb, &init_net, queue->peer_portid, MSG_DONTWAIT);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800467 if (err < 0) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800468 queue->queue_user_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700469 goto err_out_unlock;
470 }
471
472 __enqueue_entry(queue, entry);
473
474 spin_unlock_bh(&queue->lock);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800475 return 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700476
477err_out_free_nskb:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800478 kfree_skb(nskb);
Harald Welte7af4cc32005-08-09 19:44:15 -0700479err_out_unlock:
480 spin_unlock_bh(&queue->lock);
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000481 if (failopen)
482 nf_reinject(entry, NF_ACCEPT);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800483err_out:
Florian Westphalf1585082011-01-18 15:27:28 +0100484 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700485}
486
487static int
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200488nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e, int diff)
Harald Welte7af4cc32005-08-09 19:44:15 -0700489{
Patrick McHardye2b58a62008-02-19 17:17:52 -0800490 struct sk_buff *nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700491
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800492 if (diff < 0) {
493 if (pskb_trim(e->skb, data_len))
494 return -ENOMEM;
495 } else if (diff > 0) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700496 if (data_len > 0xFFFF)
497 return -EINVAL;
498 if (diff > skb_tailroom(e->skb)) {
Arnaud Ebalard9a732ed2008-04-29 03:16:34 -0700499 nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
500 diff, GFP_ATOMIC);
Patrick McHardye2b58a62008-02-19 17:17:52 -0800501 if (!nskb) {
Patrick McHardy1158ba22006-08-22 00:32:47 -0700502 printk(KERN_WARNING "nf_queue: OOM "
Harald Welte7af4cc32005-08-09 19:44:15 -0700503 "in mangle, dropping packet\n");
Patrick McHardye2b58a62008-02-19 17:17:52 -0800504 return -ENOMEM;
Harald Welte7af4cc32005-08-09 19:44:15 -0700505 }
Patrick McHardye2b58a62008-02-19 17:17:52 -0800506 kfree_skb(e->skb);
507 e->skb = nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700508 }
509 skb_put(e->skb, diff);
510 }
Herbert Xu37d41872007-10-14 00:39:18 -0700511 if (!skb_make_writable(e->skb, data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700512 return -ENOMEM;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300513 skb_copy_to_linear_data(e->skb, data, data_len);
Patrick McHardye7dfb092005-09-06 15:10:00 -0700514 e->skb->ip_summed = CHECKSUM_NONE;
Harald Welte7af4cc32005-08-09 19:44:15 -0700515 return 0;
516}
517
Harald Welte7af4cc32005-08-09 19:44:15 -0700518static int
519nfqnl_set_mode(struct nfqnl_instance *queue,
520 unsigned char mode, unsigned int range)
521{
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800522 int status = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700523
524 spin_lock_bh(&queue->lock);
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800525 switch (mode) {
526 case NFQNL_COPY_NONE:
527 case NFQNL_COPY_META:
528 queue->copy_mode = mode;
529 queue->copy_range = 0;
530 break;
531
532 case NFQNL_COPY_PACKET:
533 queue->copy_mode = mode;
Pablo Neira Ayusoba8d3b02012-09-06 17:09:26 +0200534 /* We're using struct nlattr which has 16bit nla_len. Note that
535 * nla_len includes the header length. Thus, the maximum packet
536 * length that we support is 65531 bytes. We send truncated
537 * packets if the specified length is larger than that.
538 */
539 if (range > 0xffff - NLA_HDRLEN)
540 queue->copy_range = 0xffff - NLA_HDRLEN;
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800541 else
542 queue->copy_range = range;
543 break;
544
545 default:
546 status = -EINVAL;
547
548 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700549 spin_unlock_bh(&queue->lock);
550
551 return status;
552}
553
554static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800555dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700556{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800557 if (entry->indev)
558 if (entry->indev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700559 return 1;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800560 if (entry->outdev)
561 if (entry->outdev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700562 return 1;
Patrick McHardyef47c6a2006-06-27 03:01:48 -0700563#ifdef CONFIG_BRIDGE_NETFILTER
564 if (entry->skb->nf_bridge) {
565 if (entry->skb->nf_bridge->physindev &&
566 entry->skb->nf_bridge->physindev->ifindex == ifindex)
567 return 1;
568 if (entry->skb->nf_bridge->physoutdev &&
569 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
570 return 1;
571 }
572#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700573 return 0;
574}
575
576/* drop all packets with either indev or outdev == ifindex from all queue
577 * instances */
578static void
579nfqnl_dev_drop(int ifindex)
580{
581 int i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800582
Patrick McHardy9872bec2007-12-05 01:28:50 -0800583 rcu_read_lock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700584
Patrick McHardy9872bec2007-12-05 01:28:50 -0800585 for (i = 0; i < INSTANCE_BUCKETS; i++) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700586 struct hlist_node *tmp;
587 struct nfqnl_instance *inst;
588 struct hlist_head *head = &instance_table[i];
589
Patrick McHardy9872bec2007-12-05 01:28:50 -0800590 hlist_for_each_entry_rcu(inst, tmp, head, hlist)
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800591 nfqnl_flush(inst, dev_cmp, ifindex);
Harald Welte7af4cc32005-08-09 19:44:15 -0700592 }
593
Patrick McHardy9872bec2007-12-05 01:28:50 -0800594 rcu_read_unlock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700595}
596
597#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
598
599static int
600nfqnl_rcv_dev_event(struct notifier_block *this,
601 unsigned long event, void *ptr)
602{
603 struct net_device *dev = ptr;
604
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700605 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200606 return NOTIFY_DONE;
607
Harald Welte7af4cc32005-08-09 19:44:15 -0700608 /* Drop any packets associated with the downed device */
609 if (event == NETDEV_DOWN)
610 nfqnl_dev_drop(dev->ifindex);
611 return NOTIFY_DONE;
612}
613
614static struct notifier_block nfqnl_dev_notifier = {
615 .notifier_call = nfqnl_rcv_dev_event,
616};
617
618static int
619nfqnl_rcv_nl_event(struct notifier_block *this,
620 unsigned long event, void *ptr)
621{
622 struct netlink_notify *n = ptr;
623
Patrick McHardydee58172009-11-06 17:04:00 +0100624 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700625 int i;
626
Eric W. Biederman15e47302012-09-07 20:12:54 +0000627 /* destroy all instances for this portid */
Patrick McHardy9872bec2007-12-05 01:28:50 -0800628 spin_lock(&instances_lock);
629 for (i = 0; i < INSTANCE_BUCKETS; i++) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700630 struct hlist_node *tmp, *t2;
631 struct nfqnl_instance *inst;
632 struct hlist_head *head = &instance_table[i];
633
634 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200635 if ((n->net == &init_net) &&
Eric W. Biederman15e47302012-09-07 20:12:54 +0000636 (n->portid == inst->peer_portid))
Harald Welte7af4cc32005-08-09 19:44:15 -0700637 __instance_destroy(inst);
638 }
639 }
Patrick McHardy9872bec2007-12-05 01:28:50 -0800640 spin_unlock(&instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700641 }
642 return NOTIFY_DONE;
643}
644
645static struct notifier_block nfqnl_rtnl_notifier = {
646 .notifier_call = nfqnl_rcv_nl_event,
647};
648
Patrick McHardy5bf75852007-09-28 14:39:26 -0700649static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
650 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
651 [NFQA_MARK] = { .type = NLA_U32 },
652 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200653 [NFQA_CT] = { .type = NLA_UNSPEC },
Harald Welte838ab632005-08-09 19:50:45 -0700654};
655
Florian Westphal97d32cf2011-07-19 11:46:33 +0200656static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = {
657 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
658 [NFQA_MARK] = { .type = NLA_U32 },
659};
660
Eric W. Biederman15e47302012-09-07 20:12:54 +0000661static struct nfqnl_instance *verdict_instance_lookup(u16 queue_num, int nlportid)
Florian Westphal97d32cf2011-07-19 11:46:33 +0200662{
663 struct nfqnl_instance *queue;
664
665 queue = instance_lookup(queue_num);
666 if (!queue)
667 return ERR_PTR(-ENODEV);
668
Eric W. Biederman15e47302012-09-07 20:12:54 +0000669 if (queue->peer_portid != nlportid)
Florian Westphal97d32cf2011-07-19 11:46:33 +0200670 return ERR_PTR(-EPERM);
671
672 return queue;
673}
674
675static struct nfqnl_msg_verdict_hdr*
676verdicthdr_get(const struct nlattr * const nfqa[])
677{
678 struct nfqnl_msg_verdict_hdr *vhdr;
679 unsigned int verdict;
680
681 if (!nfqa[NFQA_VERDICT_HDR])
682 return NULL;
683
684 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
Florian Westphalc6675232011-08-30 15:01:20 +0200685 verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK;
686 if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN)
Florian Westphal97d32cf2011-07-19 11:46:33 +0200687 return NULL;
688 return vhdr;
689}
690
691static int nfq_id_after(unsigned int id, unsigned int max)
692{
693 return (int)(id - max) > 0;
694}
695
696static int
697nfqnl_recv_verdict_batch(struct sock *ctnl, struct sk_buff *skb,
698 const struct nlmsghdr *nlh,
699 const struct nlattr * const nfqa[])
700{
David S. Miller3da07c02012-06-26 21:35:27 -0700701 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Florian Westphal97d32cf2011-07-19 11:46:33 +0200702 struct nf_queue_entry *entry, *tmp;
703 unsigned int verdict, maxid;
704 struct nfqnl_msg_verdict_hdr *vhdr;
705 struct nfqnl_instance *queue;
706 LIST_HEAD(batch_list);
707 u16 queue_num = ntohs(nfmsg->res_id);
708
Eric W. Biederman15e47302012-09-07 20:12:54 +0000709 queue = verdict_instance_lookup(queue_num, NETLINK_CB(skb).portid);
Florian Westphal97d32cf2011-07-19 11:46:33 +0200710 if (IS_ERR(queue))
711 return PTR_ERR(queue);
712
713 vhdr = verdicthdr_get(nfqa);
714 if (!vhdr)
715 return -EINVAL;
716
717 verdict = ntohl(vhdr->verdict);
718 maxid = ntohl(vhdr->id);
719
720 spin_lock_bh(&queue->lock);
721
722 list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) {
723 if (nfq_id_after(entry->id, maxid))
724 break;
725 __dequeue_entry(queue, entry);
726 list_add_tail(&entry->list, &batch_list);
727 }
728
729 spin_unlock_bh(&queue->lock);
730
731 if (list_empty(&batch_list))
732 return -ENOENT;
733
734 list_for_each_entry_safe(entry, tmp, &batch_list, list) {
735 if (nfqa[NFQA_MARK])
736 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
737 nf_reinject(entry, verdict);
738 }
739 return 0;
740}
741
Harald Welte7af4cc32005-08-09 19:44:15 -0700742static int
743nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200744 const struct nlmsghdr *nlh,
745 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700746{
David S. Miller3da07c02012-06-26 21:35:27 -0700747 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Harald Welte7af4cc32005-08-09 19:44:15 -0700748 u_int16_t queue_num = ntohs(nfmsg->res_id);
749
750 struct nfqnl_msg_verdict_hdr *vhdr;
751 struct nfqnl_instance *queue;
752 unsigned int verdict;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800753 struct nf_queue_entry *entry;
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200754 enum ip_conntrack_info uninitialized_var(ctinfo);
755 struct nf_conn *ct = NULL;
Harald Welte7af4cc32005-08-09 19:44:15 -0700756
Patrick McHardy9872bec2007-12-05 01:28:50 -0800757 queue = instance_lookup(queue_num);
Eric Dumazet84a797d2011-07-18 16:08:27 +0200758 if (!queue)
Harald Welte7af4cc32005-08-09 19:44:15 -0700759
Eric W. Biederman15e47302012-09-07 20:12:54 +0000760 queue = verdict_instance_lookup(queue_num, NETLINK_CB(skb).portid);
Florian Westphal97d32cf2011-07-19 11:46:33 +0200761 if (IS_ERR(queue))
762 return PTR_ERR(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700763
Florian Westphal97d32cf2011-07-19 11:46:33 +0200764 vhdr = verdicthdr_get(nfqa);
765 if (!vhdr)
Eric Dumazet84a797d2011-07-18 16:08:27 +0200766 return -EINVAL;
Harald Welte7af4cc32005-08-09 19:44:15 -0700767
Harald Welte7af4cc32005-08-09 19:44:15 -0700768 verdict = ntohl(vhdr->verdict);
769
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800770 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
Eric Dumazet84a797d2011-07-18 16:08:27 +0200771 if (entry == NULL)
772 return -ENOENT;
Harald Welte7af4cc32005-08-09 19:44:15 -0700773
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200774 rcu_read_lock();
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +0200775 if (nfqa[NFQA_CT] && (queue->flags & NFQA_CFG_F_CONNTRACK))
776 ct = nfqnl_ct_parse(entry->skb, nfqa[NFQA_CT], &ctinfo);
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200777
Patrick McHardydf6fb862007-09-28 14:37:03 -0700778 if (nfqa[NFQA_PAYLOAD]) {
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200779 u16 payload_len = nla_len(nfqa[NFQA_PAYLOAD]);
780 int diff = payload_len - entry->skb->len;
781
Patrick McHardydf6fb862007-09-28 14:37:03 -0700782 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200783 payload_len, entry, diff) < 0)
Harald Welte7af4cc32005-08-09 19:44:15 -0700784 verdict = NF_DROP;
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200785
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +0200786 if (ct)
787 nfqnl_ct_seq_adjust(skb, ct, ctinfo, diff);
Harald Welte7af4cc32005-08-09 19:44:15 -0700788 }
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200789 rcu_read_unlock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700790
Patrick McHardydf6fb862007-09-28 14:37:03 -0700791 if (nfqa[NFQA_MARK])
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800792 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800793
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800794 nf_reinject(entry, verdict);
Harald Welte7af4cc32005-08-09 19:44:15 -0700795 return 0;
796}
797
798static int
799nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200800 const struct nlmsghdr *nlh,
801 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700802{
803 return -ENOTSUPP;
804}
805
Patrick McHardy5bf75852007-09-28 14:39:26 -0700806static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
807 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
808 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
Harald Welte838ab632005-08-09 19:50:45 -0700809};
810
Patrick McHardye3ac5292007-12-05 01:23:57 -0800811static const struct nf_queue_handler nfqh = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700812 .name = "nf_queue",
813 .outfn = &nfqnl_enqueue_packet,
814};
815
Harald Welte7af4cc32005-08-09 19:44:15 -0700816static int
817nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200818 const struct nlmsghdr *nlh,
819 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700820{
David S. Miller3da07c02012-06-26 21:35:27 -0700821 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Harald Welte7af4cc32005-08-09 19:44:15 -0700822 u_int16_t queue_num = ntohs(nfmsg->res_id);
823 struct nfqnl_instance *queue;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800824 struct nfqnl_msg_config_cmd *cmd = NULL;
Harald Welte838ab632005-08-09 19:50:45 -0700825 int ret = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700826
Patrick McHardy9872bec2007-12-05 01:28:50 -0800827 if (nfqa[NFQA_CFG_CMD]) {
828 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
829
830 /* Commands without queue context - might sleep */
831 switch (cmd->command) {
832 case NFQNL_CFG_CMD_PF_BIND:
Patrick McHardy914afea2008-03-10 16:44:36 -0700833 return nf_register_queue_handler(ntohs(cmd->pf),
834 &nfqh);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800835 case NFQNL_CFG_CMD_PF_UNBIND:
Patrick McHardy914afea2008-03-10 16:44:36 -0700836 return nf_unregister_queue_handler(ntohs(cmd->pf),
837 &nfqh);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800838 }
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800839 }
840
Patrick McHardy9872bec2007-12-05 01:28:50 -0800841 rcu_read_lock();
842 queue = instance_lookup(queue_num);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000843 if (queue && queue->peer_portid != NETLINK_CB(skb).portid) {
Patrick McHardy9872bec2007-12-05 01:28:50 -0800844 ret = -EPERM;
845 goto err_out_unlock;
846 }
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800847
Patrick McHardy9872bec2007-12-05 01:28:50 -0800848 if (cmd != NULL) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700849 switch (cmd->command) {
850 case NFQNL_CFG_CMD_BIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800851 if (queue) {
852 ret = -EBUSY;
853 goto err_out_unlock;
854 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000855 queue = instance_create(queue_num, NETLINK_CB(skb).portid);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800856 if (IS_ERR(queue)) {
857 ret = PTR_ERR(queue);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800858 goto err_out_unlock;
859 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700860 break;
861 case NFQNL_CFG_CMD_UNBIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800862 if (!queue) {
863 ret = -ENODEV;
864 goto err_out_unlock;
865 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700866 instance_destroy(queue);
867 break;
868 case NFQNL_CFG_CMD_PF_BIND:
Harald Welte7af4cc32005-08-09 19:44:15 -0700869 case NFQNL_CFG_CMD_PF_UNBIND:
Harald Welte7af4cc32005-08-09 19:44:15 -0700870 break;
871 default:
Patrick McHardycd21f0a2007-12-17 22:40:19 -0800872 ret = -ENOTSUPP;
Harald Welte838ab632005-08-09 19:50:45 -0700873 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700874 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700875 }
876
Patrick McHardydf6fb862007-09-28 14:37:03 -0700877 if (nfqa[NFQA_CFG_PARAMS]) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700878 struct nfqnl_msg_config_params *params;
Harald Welte7af4cc32005-08-09 19:44:15 -0700879
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800880 if (!queue) {
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800881 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800882 goto err_out_unlock;
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800883 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700884 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700885 nfqnl_set_mode(queue, params->copy_mode,
886 ntohl(params->copy_range));
887 }
888
Patrick McHardydf6fb862007-09-28 14:37:03 -0700889 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
Eric Leblond829e17a2006-11-29 02:35:33 +0100890 __be32 *queue_maxlen;
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800891
892 if (!queue) {
893 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800894 goto err_out_unlock;
Patrick McHardya3c8e7f2007-12-05 01:28:30 -0800895 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700896 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
Eric Leblond829e17a2006-11-29 02:35:33 +0100897 spin_lock_bh(&queue->lock);
898 queue->queue_maxlen = ntohl(*queue_maxlen);
899 spin_unlock_bh(&queue->lock);
900 }
901
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000902 if (nfqa[NFQA_CFG_FLAGS]) {
903 __u32 flags, mask;
904
905 if (!queue) {
906 ret = -ENODEV;
907 goto err_out_unlock;
908 }
909
910 if (!nfqa[NFQA_CFG_MASK]) {
911 /* A mask is needed to specify which flags are being
912 * changed.
913 */
914 ret = -EINVAL;
915 goto err_out_unlock;
916 }
917
918 flags = ntohl(nla_get_be32(nfqa[NFQA_CFG_FLAGS]));
919 mask = ntohl(nla_get_be32(nfqa[NFQA_CFG_MASK]));
920
Krishna Kumar46ba5a22012-06-27 00:59:56 +0000921 if (flags >= NFQA_CFG_F_MAX) {
922 ret = -EOPNOTSUPP;
923 goto err_out_unlock;
924 }
925
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000926 spin_lock_bh(&queue->lock);
927 queue->flags &= ~mask;
928 queue->flags |= flags & mask;
929 spin_unlock_bh(&queue->lock);
930 }
931
Patrick McHardy9872bec2007-12-05 01:28:50 -0800932err_out_unlock:
933 rcu_read_unlock();
Harald Welte838ab632005-08-09 19:50:45 -0700934 return ret;
Harald Welte7af4cc32005-08-09 19:44:15 -0700935}
936
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700937static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
Eric Dumazet84a797d2011-07-18 16:08:27 +0200938 [NFQNL_MSG_PACKET] = { .call_rcu = nfqnl_recv_unsupp,
Harald Welte37d2e7a2005-11-14 15:24:59 -0800939 .attr_count = NFQA_MAX, },
Eric Dumazet84a797d2011-07-18 16:08:27 +0200940 [NFQNL_MSG_VERDICT] = { .call_rcu = nfqnl_recv_verdict,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700941 .attr_count = NFQA_MAX,
942 .policy = nfqa_verdict_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700943 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
Patrick McHardy5bf75852007-09-28 14:39:26 -0700944 .attr_count = NFQA_CFG_MAX,
945 .policy = nfqa_cfg_policy },
Florian Westphal97d32cf2011-07-19 11:46:33 +0200946 [NFQNL_MSG_VERDICT_BATCH]={ .call_rcu = nfqnl_recv_verdict_batch,
947 .attr_count = NFQA_MAX,
948 .policy = nfqa_verdict_batch_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -0700949};
950
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700951static const struct nfnetlink_subsystem nfqnl_subsys = {
Harald Welte7af4cc32005-08-09 19:44:15 -0700952 .name = "nf_queue",
953 .subsys_id = NFNL_SUBSYS_QUEUE,
954 .cb_count = NFQNL_MSG_MAX,
Harald Welte7af4cc32005-08-09 19:44:15 -0700955 .cb = nfqnl_cb,
956};
957
Harald Welte838ab632005-08-09 19:50:45 -0700958#ifdef CONFIG_PROC_FS
959struct iter_state {
960 unsigned int bucket;
961};
962
963static struct hlist_node *get_first(struct seq_file *seq)
964{
965 struct iter_state *st = seq->private;
966
967 if (!st)
968 return NULL;
969
970 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
971 if (!hlist_empty(&instance_table[st->bucket]))
972 return instance_table[st->bucket].first;
973 }
974 return NULL;
975}
976
977static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
978{
979 struct iter_state *st = seq->private;
980
981 h = h->next;
982 while (!h) {
983 if (++st->bucket >= INSTANCE_BUCKETS)
984 return NULL;
985
986 h = instance_table[st->bucket].first;
987 }
988 return h;
989}
990
991static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
992{
993 struct hlist_node *head;
994 head = get_first(seq);
995
996 if (head)
997 while (pos && (head = get_next(seq, head)))
998 pos--;
999 return pos ? NULL : head;
1000}
1001
1002static void *seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazetca7c48c2008-01-31 03:53:27 -08001003 __acquires(instances_lock)
Harald Welte838ab632005-08-09 19:50:45 -07001004{
Patrick McHardy9872bec2007-12-05 01:28:50 -08001005 spin_lock(&instances_lock);
Harald Welte838ab632005-08-09 19:50:45 -07001006 return get_idx(seq, *pos);
1007}
1008
1009static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1010{
1011 (*pos)++;
1012 return get_next(s, v);
1013}
1014
1015static void seq_stop(struct seq_file *s, void *v)
Eric Dumazetca7c48c2008-01-31 03:53:27 -08001016 __releases(instances_lock)
Harald Welte838ab632005-08-09 19:50:45 -07001017{
Patrick McHardy9872bec2007-12-05 01:28:50 -08001018 spin_unlock(&instances_lock);
Harald Welte838ab632005-08-09 19:50:45 -07001019}
1020
1021static int seq_show(struct seq_file *s, void *v)
1022{
1023 const struct nfqnl_instance *inst = v;
1024
1025 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
1026 inst->queue_num,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001027 inst->peer_portid, inst->queue_total,
Harald Welte838ab632005-08-09 19:50:45 -07001028 inst->copy_mode, inst->copy_range,
1029 inst->queue_dropped, inst->queue_user_dropped,
Eric Dumazet58637022011-07-19 11:44:17 +02001030 inst->id_sequence, 1);
Harald Welte838ab632005-08-09 19:50:45 -07001031}
1032
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001033static const struct seq_operations nfqnl_seq_ops = {
Harald Welte838ab632005-08-09 19:50:45 -07001034 .start = seq_start,
1035 .next = seq_next,
1036 .stop = seq_stop,
1037 .show = seq_show,
1038};
1039
1040static int nfqnl_open(struct inode *inode, struct file *file)
1041{
Pavel Emelyanove2da5912007-10-10 02:29:58 -07001042 return seq_open_private(file, &nfqnl_seq_ops,
1043 sizeof(struct iter_state));
Harald Welte838ab632005-08-09 19:50:45 -07001044}
1045
Arjan van de Venda7071d2007-02-12 00:55:36 -08001046static const struct file_operations nfqnl_file_ops = {
Harald Welte838ab632005-08-09 19:50:45 -07001047 .owner = THIS_MODULE,
1048 .open = nfqnl_open,
1049 .read = seq_read,
1050 .llseek = seq_lseek,
1051 .release = seq_release_private,
1052};
1053
1054#endif /* PROC_FS */
1055
Patrick McHardy32292a72006-04-06 14:11:30 -07001056static int __init nfnetlink_queue_init(void)
Harald Welte7af4cc32005-08-09 19:44:15 -07001057{
Harald Welte838ab632005-08-09 19:50:45 -07001058 int i, status = -ENOMEM;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001059
Harald Welte838ab632005-08-09 19:50:45 -07001060 for (i = 0; i < INSTANCE_BUCKETS; i++)
1061 INIT_HLIST_HEAD(&instance_table[i]);
1062
Harald Welte7af4cc32005-08-09 19:44:15 -07001063 netlink_register_notifier(&nfqnl_rtnl_notifier);
1064 status = nfnetlink_subsys_register(&nfqnl_subsys);
1065 if (status < 0) {
1066 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
1067 goto cleanup_netlink_notifier;
1068 }
1069
Harald Welte838ab632005-08-09 19:50:45 -07001070#ifdef CONFIG_PROC_FS
Denis V. Lunev8eeee8b2008-03-27 16:55:53 -07001071 if (!proc_create("nfnetlink_queue", 0440,
1072 proc_net_netfilter, &nfqnl_file_ops))
Harald Welte838ab632005-08-09 19:50:45 -07001073 goto cleanup_subsys;
Harald Welte838ab632005-08-09 19:50:45 -07001074#endif
1075
Harald Welte7af4cc32005-08-09 19:44:15 -07001076 register_netdevice_notifier(&nfqnl_dev_notifier);
1077 return status;
1078
Harald Welte838ab632005-08-09 19:50:45 -07001079#ifdef CONFIG_PROC_FS
1080cleanup_subsys:
Harald Welte7af4cc32005-08-09 19:44:15 -07001081 nfnetlink_subsys_unregister(&nfqnl_subsys);
Patrick McHardy32292a72006-04-06 14:11:30 -07001082#endif
Harald Welte7af4cc32005-08-09 19:44:15 -07001083cleanup_netlink_notifier:
1084 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1085 return status;
1086}
1087
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001088static void __exit nfnetlink_queue_fini(void)
Harald Welte7af4cc32005-08-09 19:44:15 -07001089{
Patrick McHardy32292a72006-04-06 14:11:30 -07001090 nf_unregister_queue_handlers(&nfqh);
1091 unregister_netdevice_notifier(&nfqnl_dev_notifier);
1092#ifdef CONFIG_PROC_FS
1093 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
1094#endif
1095 nfnetlink_subsys_unregister(&nfqnl_subsys);
1096 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
Jesper Dangaard Brouer67137f32009-06-08 03:11:33 +00001097
1098 rcu_barrier(); /* Wait for completion of call_rcu()'s */
Harald Welte7af4cc32005-08-09 19:44:15 -07001099}
1100
1101MODULE_DESCRIPTION("netfilter packet queue handler");
1102MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1103MODULE_LICENSE("GPL");
1104MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1105
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001106module_init(nfnetlink_queue_init);
1107module_exit(nfnetlink_queue_fini);