blob: d20c52cc80c054f82c58d5fd4406fd6d0199b6b9 [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>
Gao fenge8179612013-03-24 23:50:47 +000033#include <net/netns/generic.h>
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +020034#include <net/netfilter/nfnetlink_queue.h>
Harald Welte7af4cc32005-08-09 19:44:15 -070035
Arun Sharma600634972011-07-26 16:09:06 -070036#include <linux/atomic.h>
Harald Welte7af4cc32005-08-09 19:44:15 -070037
Harald Weltefbcd9232005-08-09 20:22:10 -070038#ifdef CONFIG_BRIDGE_NETFILTER
39#include "../bridge/br_private.h"
40#endif
41
Harald Welte7af4cc32005-08-09 19:44:15 -070042#define NFQNL_QMAX_DEFAULT 1024
43
Harald Welte7af4cc32005-08-09 19:44:15 -070044struct nfqnl_instance {
45 struct hlist_node hlist; /* global list of queues */
Patrick McHardy9872bec2007-12-05 01:28:50 -080046 struct rcu_head rcu;
Harald Welte7af4cc32005-08-09 19:44:15 -070047
Eric W. Biederman15e47302012-09-07 20:12:54 +000048 int peer_portid;
Harald Welte7af4cc32005-08-09 19:44:15 -070049 unsigned int queue_maxlen;
50 unsigned int copy_range;
Harald Welte7af4cc32005-08-09 19:44:15 -070051 unsigned int queue_dropped;
52 unsigned int queue_user_dropped;
53
Harald Welte7af4cc32005-08-09 19:44:15 -070054
55 u_int16_t queue_num; /* number of this queue */
56 u_int8_t copy_mode;
Krishna Kumarfdb694a2012-05-24 03:56:44 +000057 u_int32_t flags; /* Set using NFQA_CFG_FLAGS */
Eric Dumazetc463ac92010-06-09 18:07:06 +020058/*
59 * Following fields are dirtied for each queued packet,
60 * keep them in same cache line if possible.
61 */
62 spinlock_t lock;
63 unsigned int queue_total;
Eric Dumazet58637022011-07-19 11:44:17 +020064 unsigned int id_sequence; /* 'sequence' of pkt ids */
Harald Welte7af4cc32005-08-09 19:44:15 -070065 struct list_head queue_list; /* packets in queue */
66};
67
Patrick McHardy02f014d2007-12-05 01:26:33 -080068typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
Harald Welte7af4cc32005-08-09 19:44:15 -070069
Gao fenge8179612013-03-24 23:50:47 +000070static int nfnl_queue_net_id __read_mostly;
Harald Welte7af4cc32005-08-09 19:44:15 -070071
Harald Welte7af4cc32005-08-09 19:44:15 -070072#define INSTANCE_BUCKETS 16
Gao fenge8179612013-03-24 23:50:47 +000073struct nfnl_queue_net {
74 spinlock_t instances_lock;
75 struct hlist_head instance_table[INSTANCE_BUCKETS];
76};
77
78static struct nfnl_queue_net *nfnl_queue_pernet(struct net *net)
79{
80 return net_generic(net, nfnl_queue_net_id);
81}
Harald Welte7af4cc32005-08-09 19:44:15 -070082
83static inline u_int8_t instance_hashfn(u_int16_t queue_num)
84{
Pablo Neira Ayuso1cdb0902013-03-14 06:03:17 +000085 return ((queue_num >> 8) ^ queue_num) % INSTANCE_BUCKETS;
Harald Welte7af4cc32005-08-09 19:44:15 -070086}
87
88static struct nfqnl_instance *
Gao fenge8179612013-03-24 23:50:47 +000089instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num)
Harald Welte7af4cc32005-08-09 19:44:15 -070090{
91 struct hlist_head *head;
Harald Welte7af4cc32005-08-09 19:44:15 -070092 struct nfqnl_instance *inst;
93
Gao fenge8179612013-03-24 23:50:47 +000094 head = &q->instance_table[instance_hashfn(queue_num)];
Sasha Levinb67bfe02013-02-27 17:06:00 -080095 hlist_for_each_entry_rcu(inst, head, hlist) {
Harald Welte7af4cc32005-08-09 19:44:15 -070096 if (inst->queue_num == queue_num)
97 return inst;
98 }
99 return NULL;
100}
101
102static struct nfqnl_instance *
Gao fenge8179612013-03-24 23:50:47 +0000103instance_create(struct nfnl_queue_net *q, u_int16_t queue_num,
104 int portid)
Harald Welte7af4cc32005-08-09 19:44:15 -0700105{
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800106 struct nfqnl_instance *inst;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800107 unsigned int h;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800108 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700109
Gao fenge8179612013-03-24 23:50:47 +0000110 spin_lock(&q->instances_lock);
111 if (instance_lookup(q, queue_num)) {
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800112 err = -EEXIST;
Harald Welte7af4cc32005-08-09 19:44:15 -0700113 goto out_unlock;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800114 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700115
Harald Welte10dfdc62005-11-03 19:20:07 +0100116 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800117 if (!inst) {
118 err = -ENOMEM;
Harald Welte7af4cc32005-08-09 19:44:15 -0700119 goto out_unlock;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800120 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700121
Harald Welte7af4cc32005-08-09 19:44:15 -0700122 inst->queue_num = queue_num;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000123 inst->peer_portid = portid;
Harald Welte7af4cc32005-08-09 19:44:15 -0700124 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
Pablo Neira Ayusobae99f72013-03-14 06:03:18 +0000125 inst->copy_range = 0xffff;
Harald Welte7af4cc32005-08-09 19:44:15 -0700126 inst->copy_mode = NFQNL_COPY_NONE;
YOSHIFUJI Hideaki181a46a2006-01-04 13:56:54 -0800127 spin_lock_init(&inst->lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700128 INIT_LIST_HEAD(&inst->queue_list);
129
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800130 if (!try_module_get(THIS_MODULE)) {
131 err = -EAGAIN;
Harald Welte7af4cc32005-08-09 19:44:15 -0700132 goto out_free;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800133 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700134
Patrick McHardy9872bec2007-12-05 01:28:50 -0800135 h = instance_hashfn(queue_num);
Gao fenge8179612013-03-24 23:50:47 +0000136 hlist_add_head_rcu(&inst->hlist, &q->instance_table[h]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700137
Gao fenge8179612013-03-24 23:50:47 +0000138 spin_unlock(&q->instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700139
Harald Welte7af4cc32005-08-09 19:44:15 -0700140 return inst;
141
142out_free:
143 kfree(inst);
144out_unlock:
Gao fenge8179612013-03-24 23:50:47 +0000145 spin_unlock(&q->instances_lock);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800146 return ERR_PTR(err);
Harald Welte7af4cc32005-08-09 19:44:15 -0700147}
148
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800149static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
150 unsigned long data);
Harald Welte7af4cc32005-08-09 19:44:15 -0700151
152static void
Patrick McHardy9872bec2007-12-05 01:28:50 -0800153instance_destroy_rcu(struct rcu_head *head)
Harald Welte7af4cc32005-08-09 19:44:15 -0700154{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800155 struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
156 rcu);
Harald Welte7af4cc32005-08-09 19:44:15 -0700157
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800158 nfqnl_flush(inst, NULL, 0);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800159 kfree(inst);
Harald Welte7af4cc32005-08-09 19:44:15 -0700160 module_put(THIS_MODULE);
161}
162
Patrick McHardy9872bec2007-12-05 01:28:50 -0800163static void
Harald Welte7af4cc32005-08-09 19:44:15 -0700164__instance_destroy(struct nfqnl_instance *inst)
165{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800166 hlist_del_rcu(&inst->hlist);
167 call_rcu(&inst->rcu, instance_destroy_rcu);
Harald Welte7af4cc32005-08-09 19:44:15 -0700168}
169
Patrick McHardy9872bec2007-12-05 01:28:50 -0800170static void
Gao fenge8179612013-03-24 23:50:47 +0000171instance_destroy(struct nfnl_queue_net *q, struct nfqnl_instance *inst)
Harald Welte7af4cc32005-08-09 19:44:15 -0700172{
Gao fenge8179612013-03-24 23:50:47 +0000173 spin_lock(&q->instances_lock);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800174 __instance_destroy(inst);
Gao fenge8179612013-03-24 23:50:47 +0000175 spin_unlock(&q->instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700176}
177
Harald Welte7af4cc32005-08-09 19:44:15 -0700178static inline void
Patrick McHardy02f014d2007-12-05 01:26:33 -0800179__enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
Harald Welte7af4cc32005-08-09 19:44:15 -0700180{
Patrick McHardy0ac41e82007-12-05 01:25:03 -0800181 list_add_tail(&entry->list, &queue->queue_list);
Harald Welte7af4cc32005-08-09 19:44:15 -0700182 queue->queue_total++;
183}
184
Florian Westphal97d32cf2011-07-19 11:46:33 +0200185static void
186__dequeue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
187{
188 list_del(&entry->list);
189 queue->queue_total--;
190}
191
Patrick McHardy02f014d2007-12-05 01:26:33 -0800192static struct nf_queue_entry *
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800193find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
Harald Welte7af4cc32005-08-09 19:44:15 -0700194{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800195 struct nf_queue_entry *entry = NULL, *i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800196
Harald Welte7af4cc32005-08-09 19:44:15 -0700197 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800198
199 list_for_each_entry(i, &queue->queue_list, list) {
200 if (i->id == id) {
201 entry = i;
202 break;
203 }
204 }
205
Florian Westphal97d32cf2011-07-19 11:46:33 +0200206 if (entry)
207 __dequeue_entry(queue, entry);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800208
Harald Welte7af4cc32005-08-09 19:44:15 -0700209 spin_unlock_bh(&queue->lock);
210
211 return entry;
212}
213
214static void
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800215nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
Harald Welte7af4cc32005-08-09 19:44:15 -0700216{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800217 struct nf_queue_entry *entry, *next;
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800218
Harald Welte7af4cc32005-08-09 19:44:15 -0700219 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800220 list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
221 if (!cmpfn || cmpfn(entry, data)) {
222 list_del(&entry->list);
223 queue->queue_total--;
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800224 nf_reinject(entry, NF_DROP);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800225 }
226 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700227 spin_unlock_bh(&queue->lock);
228}
229
Eric Dumazetae08ce02013-03-17 17:15:55 +0000230static void
231nfqnl_zcopy(struct sk_buff *to, const struct sk_buff *from, int len, int hlen)
232{
233 int i, j = 0;
234 int plen = 0; /* length of skb->head fragment */
235 struct page *page;
236 unsigned int offset;
237
238 /* dont bother with small payloads */
239 if (len <= skb_tailroom(to)) {
240 skb_copy_bits(from, 0, skb_put(to, len), len);
241 return;
242 }
243
244 if (hlen) {
245 skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
246 len -= hlen;
247 } else {
248 plen = min_t(int, skb_headlen(from), len);
249 if (plen) {
250 page = virt_to_head_page(from->head);
251 offset = from->data - (unsigned char *)page_address(page);
252 __skb_fill_page_desc(to, 0, page, offset, plen);
253 get_page(page);
254 j = 1;
255 len -= plen;
256 }
257 }
258
259 to->truesize += len + plen;
260 to->len += len + plen;
261 to->data_len += len + plen;
262
263 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
264 if (!len)
265 break;
266 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
267 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
268 len -= skb_shinfo(to)->frags[j].size;
269 skb_frag_ref(to, j);
270 j++;
271 }
272 skb_shinfo(to)->nr_frags = j;
273}
274
Harald Welte7af4cc32005-08-09 19:44:15 -0700275static struct sk_buff *
276nfqnl_build_packet_message(struct nfqnl_instance *queue,
Eric Dumazet58637022011-07-19 11:44:17 +0200277 struct nf_queue_entry *entry,
278 __be32 **packet_id_ptr)
Harald Welte7af4cc32005-08-09 19:44:15 -0700279{
Harald Welte7af4cc32005-08-09 19:44:15 -0700280 size_t size;
Pablo Neira Ayuso6ee584b2012-09-24 14:52:12 +0200281 size_t data_len = 0, cap_len = 0;
Eric Dumazetae08ce02013-03-17 17:15:55 +0000282 int hlen = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700283 struct sk_buff *skb;
Eric Dumazet58637022011-07-19 11:44:17 +0200284 struct nlattr *nla;
285 struct nfqnl_msg_packet_hdr *pmsg;
Harald Welte7af4cc32005-08-09 19:44:15 -0700286 struct nlmsghdr *nlh;
287 struct nfgenmsg *nfmsg;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800288 struct sk_buff *entskb = entry->skb;
289 struct net_device *indev;
290 struct net_device *outdev;
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200291 struct nf_conn *ct = NULL;
292 enum ip_conntrack_info uninitialized_var(ctinfo);
Harald Welte7af4cc32005-08-09 19:44:15 -0700293
Eric Leblondcabaa9b2008-03-10 16:41:43 -0700294 size = NLMSG_SPACE(sizeof(struct nfgenmsg))
Patrick McHardydf6fb862007-09-28 14:37:03 -0700295 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
296 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
297 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700298#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700299 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
300 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700301#endif
Patrick McHardydf6fb862007-09-28 14:37:03 -0700302 + nla_total_size(sizeof(u_int32_t)) /* mark */
303 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
Eric Dumazetae08ce02013-03-17 17:15:55 +0000304 + nla_total_size(sizeof(u_int32_t)); /* cap_len */
305
306 if (entskb->tstamp.tv64)
307 size += nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
Harald Welte7af4cc32005-08-09 19:44:15 -0700308
Patrick McHardy02f014d2007-12-05 01:26:33 -0800309 outdev = entry->outdev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800310
Eric Dumazetc463ac92010-06-09 18:07:06 +0200311 switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700312 case NFQNL_COPY_META:
313 case NFQNL_COPY_NONE:
Harald Welte7af4cc32005-08-09 19:44:15 -0700314 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800315
Harald Welte7af4cc32005-08-09 19:44:15 -0700316 case NFQNL_COPY_PACKET:
Herbert Xue9f13ca2010-04-08 14:54:35 +0200317 if (entskb->ip_summed == CHECKSUM_PARTIAL &&
Eric Dumazetc463ac92010-06-09 18:07:06 +0200318 skb_checksum_help(entskb))
Patrick McHardye7dfb092005-09-06 15:10:00 -0700319 return NULL;
Eric Dumazetc463ac92010-06-09 18:07:06 +0200320
321 data_len = ACCESS_ONCE(queue->copy_range);
322 if (data_len == 0 || data_len > entskb->len)
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800323 data_len = entskb->len;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800324
Eric Dumazetae08ce02013-03-17 17:15:55 +0000325
326 if (!entskb->head_frag ||
327 skb_headlen(entskb) < L1_CACHE_BYTES ||
328 skb_shinfo(entskb)->nr_frags >= MAX_SKB_FRAGS)
329 hlen = skb_headlen(entskb);
330
331 if (skb_has_frag_list(entskb))
332 hlen = entskb->len;
333 hlen = min_t(int, data_len, hlen);
334 size += sizeof(struct nlattr) + hlen;
Pablo Neira Ayuso6ee584b2012-09-24 14:52:12 +0200335 cap_len = entskb->len;
Harald Welte7af4cc32005-08-09 19:44:15 -0700336 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700337 }
338
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +0200339 if (queue->flags & NFQA_CFG_F_CONNTRACK)
340 ct = nfqnl_ct_get(entskb, &size, &ctinfo);
Harald Welte7af4cc32005-08-09 19:44:15 -0700341
342 skb = alloc_skb(size, GFP_ATOMIC);
343 if (!skb)
David S. Miller3da07c02012-06-26 21:35:27 -0700344 return NULL;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800345
David S. Miller3da07c02012-06-26 21:35:27 -0700346 nlh = nlmsg_put(skb, 0, 0,
Harald Welte7af4cc32005-08-09 19:44:15 -0700347 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
David S. Miller3da07c02012-06-26 21:35:27 -0700348 sizeof(struct nfgenmsg), 0);
349 if (!nlh) {
350 kfree_skb(skb);
351 return NULL;
352 }
353 nfmsg = nlmsg_data(nlh);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800354 nfmsg->nfgen_family = entry->pf;
Harald Welte7af4cc32005-08-09 19:44:15 -0700355 nfmsg->version = NFNETLINK_V0;
356 nfmsg->res_id = htons(queue->queue_num);
357
Eric Dumazet58637022011-07-19 11:44:17 +0200358 nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg));
359 pmsg = nla_data(nla);
360 pmsg->hw_protocol = entskb->protocol;
361 pmsg->hook = entry->hook;
362 *packet_id_ptr = &pmsg->packet_id;
Harald Welte7af4cc32005-08-09 19:44:15 -0700363
Patrick McHardy02f014d2007-12-05 01:26:33 -0800364 indev = entry->indev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800365 if (indev) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700366#ifndef CONFIG_BRIDGE_NETFILTER
David S. Millera4471892012-03-29 23:27:38 -0400367 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex)))
368 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700369#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800370 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700371 /* Case 1: indev is physical input device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800372 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700373 * netfilter_bridge) */
David S. Millera4471892012-03-29 23:27:38 -0400374 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
375 htonl(indev->ifindex)) ||
Harald Weltefbcd9232005-08-09 20:22:10 -0700376 /* this is the bridge group "brX" */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000377 /* rcu_read_lock()ed by __nf_queue */
David S. Millera4471892012-03-29 23:27:38 -0400378 nla_put_be32(skb, NFQA_IFINDEX_INDEV,
379 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
380 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700381 } else {
382 /* Case 2: indev is bridge group, we need to look for
383 * physical device (when called from ipv4) */
David S. Millera4471892012-03-29 23:27:38 -0400384 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV,
385 htonl(indev->ifindex)))
386 goto nla_put_failure;
387 if (entskb->nf_bridge && entskb->nf_bridge->physindev &&
388 nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
389 htonl(entskb->nf_bridge->physindev->ifindex)))
390 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700391 }
392#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700393 }
394
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800395 if (outdev) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700396#ifndef CONFIG_BRIDGE_NETFILTER
David S. Millera4471892012-03-29 23:27:38 -0400397 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex)))
398 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700399#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800400 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700401 /* Case 1: outdev is physical output device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800402 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700403 * netfilter_bridge) */
David S. Millera4471892012-03-29 23:27:38 -0400404 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
405 htonl(outdev->ifindex)) ||
Harald Weltefbcd9232005-08-09 20:22:10 -0700406 /* this is the bridge group "brX" */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000407 /* rcu_read_lock()ed by __nf_queue */
David S. Millera4471892012-03-29 23:27:38 -0400408 nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
409 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
410 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700411 } else {
412 /* Case 2: outdev is bridge group, we need to look for
413 * physical output device (when called from ipv4) */
David S. Millera4471892012-03-29 23:27:38 -0400414 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
415 htonl(outdev->ifindex)))
416 goto nla_put_failure;
417 if (entskb->nf_bridge && entskb->nf_bridge->physoutdev &&
418 nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
419 htonl(entskb->nf_bridge->physoutdev->ifindex)))
420 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700421 }
422#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700423 }
424
David S. Millera4471892012-03-29 23:27:38 -0400425 if (entskb->mark &&
426 nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark)))
427 goto nla_put_failure;
Harald Welte7af4cc32005-08-09 19:44:15 -0700428
Nicolas Cavallari2c38de42011-06-16 17:27:04 +0200429 if (indev && entskb->dev &&
430 entskb->mac_header != entskb->network_header) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700431 struct nfqnl_msg_packet_hw phw;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700432 int len = dev_parse_header(entskb, phw.hw_addr);
433 if (len) {
434 phw.hw_addrlen = htons(len);
David S. Millera4471892012-03-29 23:27:38 -0400435 if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw))
436 goto nla_put_failure;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700437 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700438 }
439
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700440 if (entskb->tstamp.tv64) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700441 struct nfqnl_msg_packet_timestamp ts;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700442 struct timeval tv = ktime_to_timeval(entskb->tstamp);
443 ts.sec = cpu_to_be64(tv.tv_sec);
444 ts.usec = cpu_to_be64(tv.tv_usec);
Harald Welte7af4cc32005-08-09 19:44:15 -0700445
David S. Millera4471892012-03-29 23:27:38 -0400446 if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts))
447 goto nla_put_failure;
Harald Welte7af4cc32005-08-09 19:44:15 -0700448 }
449
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +0200450 if (ct && nfqnl_ct_put(skb, ct, ctinfo) < 0)
451 goto nla_put_failure;
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200452
Pablo Neira Ayuso6ee584b2012-09-24 14:52:12 +0200453 if (cap_len > 0 && nla_put_be32(skb, NFQA_CAP_LEN, htonl(cap_len)))
454 goto nla_put_failure;
455
Eric Dumazetae08ce02013-03-17 17:15:55 +0000456 if (data_len) {
457 struct nlattr *nla;
458
459 if (skb_tailroom(skb) < sizeof(*nla) + hlen)
460 goto nla_put_failure;
461
462 nla = (struct nlattr *)skb_put(skb, sizeof(*nla));
463 nla->nla_type = NFQA_PAYLOAD;
464 nla->nla_len = nla_attr_size(data_len);
465
466 nfqnl_zcopy(skb, entskb, data_len, hlen);
467 }
468
469 nlh->nlmsg_len = skb->len;
Harald Welte7af4cc32005-08-09 19:44:15 -0700470 return skb;
471
Patrick McHardydf6fb862007-09-28 14:37:03 -0700472nla_put_failure:
Wei Yongjuna6729952012-08-28 03:14:15 +0000473 kfree_skb(skb);
Joe Perchese87cc472012-05-13 21:56:26 +0000474 net_err_ratelimited("nf_queue: error creating packet message\n");
Harald Welte7af4cc32005-08-09 19:44:15 -0700475 return NULL;
476}
477
478static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800479nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
Harald Welte7af4cc32005-08-09 19:44:15 -0700480{
Harald Welte7af4cc32005-08-09 19:44:15 -0700481 struct sk_buff *nskb;
482 struct nfqnl_instance *queue;
Florian Westphalf1585082011-01-18 15:27:28 +0100483 int err = -ENOBUFS;
Eric Dumazet58637022011-07-19 11:44:17 +0200484 __be32 *packet_id_ptr;
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000485 int failopen = 0;
Gao fenge8179612013-03-24 23:50:47 +0000486 struct net *net = dev_net(entry->indev ?
487 entry->indev : entry->outdev);
488 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
Harald Welte7af4cc32005-08-09 19:44:15 -0700489
Patrick McHardy9872bec2007-12-05 01:28:50 -0800490 /* rcu_read_lock()ed by nf_hook_slow() */
Gao fenge8179612013-03-24 23:50:47 +0000491 queue = instance_lookup(q, queuenum);
Florian Westphalf1585082011-01-18 15:27:28 +0100492 if (!queue) {
493 err = -ESRCH;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800494 goto err_out;
Florian Westphalf1585082011-01-18 15:27:28 +0100495 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700496
Florian Westphalf1585082011-01-18 15:27:28 +0100497 if (queue->copy_mode == NFQNL_COPY_NONE) {
498 err = -EINVAL;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800499 goto err_out;
Florian Westphalf1585082011-01-18 15:27:28 +0100500 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700501
Eric Dumazet58637022011-07-19 11:44:17 +0200502 nskb = nfqnl_build_packet_message(queue, entry, &packet_id_ptr);
Florian Westphalf1585082011-01-18 15:27:28 +0100503 if (nskb == NULL) {
504 err = -ENOMEM;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800505 goto err_out;
Florian Westphalf1585082011-01-18 15:27:28 +0100506 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700507 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800508
Eric W. Biederman15e47302012-09-07 20:12:54 +0000509 if (!queue->peer_portid) {
Florian Westphalf1585082011-01-18 15:27:28 +0100510 err = -EINVAL;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800511 goto err_out_free_nskb;
Florian Westphalf1585082011-01-18 15:27:28 +0100512 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700513 if (queue->queue_total >= queue->queue_maxlen) {
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000514 if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
515 failopen = 1;
516 err = 0;
517 } else {
518 queue->queue_dropped++;
519 net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n",
520 queue->queue_total);
521 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700522 goto err_out_free_nskb;
523 }
Eric Dumazet58637022011-07-19 11:44:17 +0200524 entry->id = ++queue->id_sequence;
525 *packet_id_ptr = htonl(entry->id);
Harald Welte7af4cc32005-08-09 19:44:15 -0700526
527 /* nfnetlink_unicast will either free the nskb or add it to a socket */
Gao fenge8179612013-03-24 23:50:47 +0000528 err = nfnetlink_unicast(nskb, net, queue->peer_portid, MSG_DONTWAIT);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800529 if (err < 0) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800530 queue->queue_user_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700531 goto err_out_unlock;
532 }
533
534 __enqueue_entry(queue, entry);
535
536 spin_unlock_bh(&queue->lock);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800537 return 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700538
539err_out_free_nskb:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800540 kfree_skb(nskb);
Harald Welte7af4cc32005-08-09 19:44:15 -0700541err_out_unlock:
542 spin_unlock_bh(&queue->lock);
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000543 if (failopen)
544 nf_reinject(entry, NF_ACCEPT);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800545err_out:
Florian Westphalf1585082011-01-18 15:27:28 +0100546 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700547}
548
549static int
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200550nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e, int diff)
Harald Welte7af4cc32005-08-09 19:44:15 -0700551{
Patrick McHardye2b58a62008-02-19 17:17:52 -0800552 struct sk_buff *nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700553
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800554 if (diff < 0) {
555 if (pskb_trim(e->skb, data_len))
556 return -ENOMEM;
557 } else if (diff > 0) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700558 if (data_len > 0xFFFF)
559 return -EINVAL;
560 if (diff > skb_tailroom(e->skb)) {
Arnaud Ebalard9a732ed2008-04-29 03:16:34 -0700561 nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
562 diff, GFP_ATOMIC);
Patrick McHardye2b58a62008-02-19 17:17:52 -0800563 if (!nskb) {
Patrick McHardy1158ba22006-08-22 00:32:47 -0700564 printk(KERN_WARNING "nf_queue: OOM "
Harald Welte7af4cc32005-08-09 19:44:15 -0700565 "in mangle, dropping packet\n");
Patrick McHardye2b58a62008-02-19 17:17:52 -0800566 return -ENOMEM;
Harald Welte7af4cc32005-08-09 19:44:15 -0700567 }
Patrick McHardye2b58a62008-02-19 17:17:52 -0800568 kfree_skb(e->skb);
569 e->skb = nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700570 }
571 skb_put(e->skb, diff);
572 }
Herbert Xu37d41872007-10-14 00:39:18 -0700573 if (!skb_make_writable(e->skb, data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700574 return -ENOMEM;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300575 skb_copy_to_linear_data(e->skb, data, data_len);
Patrick McHardye7dfb092005-09-06 15:10:00 -0700576 e->skb->ip_summed = CHECKSUM_NONE;
Harald Welte7af4cc32005-08-09 19:44:15 -0700577 return 0;
578}
579
Harald Welte7af4cc32005-08-09 19:44:15 -0700580static int
581nfqnl_set_mode(struct nfqnl_instance *queue,
582 unsigned char mode, unsigned int range)
583{
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800584 int status = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700585
586 spin_lock_bh(&queue->lock);
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800587 switch (mode) {
588 case NFQNL_COPY_NONE:
589 case NFQNL_COPY_META:
590 queue->copy_mode = mode;
591 queue->copy_range = 0;
592 break;
593
594 case NFQNL_COPY_PACKET:
595 queue->copy_mode = mode;
Pablo Neira Ayusoba8d3b02012-09-06 17:09:26 +0200596 /* We're using struct nlattr which has 16bit nla_len. Note that
597 * nla_len includes the header length. Thus, the maximum packet
598 * length that we support is 65531 bytes. We send truncated
599 * packets if the specified length is larger than that.
600 */
601 if (range > 0xffff - NLA_HDRLEN)
602 queue->copy_range = 0xffff - NLA_HDRLEN;
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800603 else
604 queue->copy_range = range;
605 break;
606
607 default:
608 status = -EINVAL;
609
610 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700611 spin_unlock_bh(&queue->lock);
612
613 return status;
614}
615
616static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800617dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700618{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800619 if (entry->indev)
620 if (entry->indev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700621 return 1;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800622 if (entry->outdev)
623 if (entry->outdev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700624 return 1;
Patrick McHardyef47c6a72006-06-27 03:01:48 -0700625#ifdef CONFIG_BRIDGE_NETFILTER
626 if (entry->skb->nf_bridge) {
627 if (entry->skb->nf_bridge->physindev &&
628 entry->skb->nf_bridge->physindev->ifindex == ifindex)
629 return 1;
630 if (entry->skb->nf_bridge->physoutdev &&
631 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
632 return 1;
633 }
634#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700635 return 0;
636}
637
638/* drop all packets with either indev or outdev == ifindex from all queue
639 * instances */
640static void
Gao fenge8179612013-03-24 23:50:47 +0000641nfqnl_dev_drop(struct net *net, int ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700642{
643 int i;
Gao fenge8179612013-03-24 23:50:47 +0000644 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800645
Patrick McHardy9872bec2007-12-05 01:28:50 -0800646 rcu_read_lock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700647
Patrick McHardy9872bec2007-12-05 01:28:50 -0800648 for (i = 0; i < INSTANCE_BUCKETS; i++) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700649 struct nfqnl_instance *inst;
Gao fenge8179612013-03-24 23:50:47 +0000650 struct hlist_head *head = &q->instance_table[i];
Harald Welte7af4cc32005-08-09 19:44:15 -0700651
Sasha Levinb67bfe02013-02-27 17:06:00 -0800652 hlist_for_each_entry_rcu(inst, head, hlist)
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800653 nfqnl_flush(inst, dev_cmp, ifindex);
Harald Welte7af4cc32005-08-09 19:44:15 -0700654 }
655
Patrick McHardy9872bec2007-12-05 01:28:50 -0800656 rcu_read_unlock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700657}
658
659#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
660
661static int
662nfqnl_rcv_dev_event(struct notifier_block *this,
663 unsigned long event, void *ptr)
664{
665 struct net_device *dev = ptr;
666
667 /* Drop any packets associated with the downed device */
668 if (event == NETDEV_DOWN)
Gao fenge8179612013-03-24 23:50:47 +0000669 nfqnl_dev_drop(dev_net(dev), dev->ifindex);
Harald Welte7af4cc32005-08-09 19:44:15 -0700670 return NOTIFY_DONE;
671}
672
673static struct notifier_block nfqnl_dev_notifier = {
674 .notifier_call = nfqnl_rcv_dev_event,
675};
676
677static int
678nfqnl_rcv_nl_event(struct notifier_block *this,
679 unsigned long event, void *ptr)
680{
681 struct netlink_notify *n = ptr;
Gao fenge8179612013-03-24 23:50:47 +0000682 struct nfnl_queue_net *q = nfnl_queue_pernet(n->net);
Harald Welte7af4cc32005-08-09 19:44:15 -0700683
Patrick McHardydee58172009-11-06 17:04:00 +0100684 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700685 int i;
686
Eric W. Biederman15e47302012-09-07 20:12:54 +0000687 /* destroy all instances for this portid */
Gao fenge8179612013-03-24 23:50:47 +0000688 spin_lock(&q->instances_lock);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800689 for (i = 0; i < INSTANCE_BUCKETS; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800690 struct hlist_node *t2;
Harald Welte7af4cc32005-08-09 19:44:15 -0700691 struct nfqnl_instance *inst;
Gao fenge8179612013-03-24 23:50:47 +0000692 struct hlist_head *head = &q->instance_table[i];
Harald Welte7af4cc32005-08-09 19:44:15 -0700693
Sasha Levinb67bfe02013-02-27 17:06:00 -0800694 hlist_for_each_entry_safe(inst, t2, head, hlist) {
Gao fenge8179612013-03-24 23:50:47 +0000695 if (n->portid == inst->peer_portid)
Harald Welte7af4cc32005-08-09 19:44:15 -0700696 __instance_destroy(inst);
697 }
698 }
Gao fenge8179612013-03-24 23:50:47 +0000699 spin_unlock(&q->instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700700 }
701 return NOTIFY_DONE;
702}
703
704static struct notifier_block nfqnl_rtnl_notifier = {
705 .notifier_call = nfqnl_rcv_nl_event,
706};
707
Patrick McHardy5bf75852007-09-28 14:39:26 -0700708static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
709 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
710 [NFQA_MARK] = { .type = NLA_U32 },
711 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200712 [NFQA_CT] = { .type = NLA_UNSPEC },
Harald Welte838ab632005-08-09 19:50:45 -0700713};
714
Florian Westphal97d32cf2011-07-19 11:46:33 +0200715static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = {
716 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
717 [NFQA_MARK] = { .type = NLA_U32 },
718};
719
Gao fenge8179612013-03-24 23:50:47 +0000720static struct nfqnl_instance *
721verdict_instance_lookup(struct nfnl_queue_net *q, u16 queue_num, int nlportid)
Florian Westphal97d32cf2011-07-19 11:46:33 +0200722{
723 struct nfqnl_instance *queue;
724
Gao fenge8179612013-03-24 23:50:47 +0000725 queue = instance_lookup(q, queue_num);
Florian Westphal97d32cf2011-07-19 11:46:33 +0200726 if (!queue)
727 return ERR_PTR(-ENODEV);
728
Eric W. Biederman15e47302012-09-07 20:12:54 +0000729 if (queue->peer_portid != nlportid)
Florian Westphal97d32cf2011-07-19 11:46:33 +0200730 return ERR_PTR(-EPERM);
731
732 return queue;
733}
734
735static struct nfqnl_msg_verdict_hdr*
736verdicthdr_get(const struct nlattr * const nfqa[])
737{
738 struct nfqnl_msg_verdict_hdr *vhdr;
739 unsigned int verdict;
740
741 if (!nfqa[NFQA_VERDICT_HDR])
742 return NULL;
743
744 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
Florian Westphalc6675232011-08-30 15:01:20 +0200745 verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK;
746 if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN)
Florian Westphal97d32cf2011-07-19 11:46:33 +0200747 return NULL;
748 return vhdr;
749}
750
751static int nfq_id_after(unsigned int id, unsigned int max)
752{
753 return (int)(id - max) > 0;
754}
755
756static int
757nfqnl_recv_verdict_batch(struct sock *ctnl, struct sk_buff *skb,
758 const struct nlmsghdr *nlh,
759 const struct nlattr * const nfqa[])
760{
David S. Miller3da07c02012-06-26 21:35:27 -0700761 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Florian Westphal97d32cf2011-07-19 11:46:33 +0200762 struct nf_queue_entry *entry, *tmp;
763 unsigned int verdict, maxid;
764 struct nfqnl_msg_verdict_hdr *vhdr;
765 struct nfqnl_instance *queue;
766 LIST_HEAD(batch_list);
767 u16 queue_num = ntohs(nfmsg->res_id);
768
Gao fenge8179612013-03-24 23:50:47 +0000769 struct net *net = sock_net(ctnl);
770 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
771
772 queue = verdict_instance_lookup(q, queue_num,
773 NETLINK_CB(skb).portid);
Florian Westphal97d32cf2011-07-19 11:46:33 +0200774 if (IS_ERR(queue))
775 return PTR_ERR(queue);
776
777 vhdr = verdicthdr_get(nfqa);
778 if (!vhdr)
779 return -EINVAL;
780
781 verdict = ntohl(vhdr->verdict);
782 maxid = ntohl(vhdr->id);
783
784 spin_lock_bh(&queue->lock);
785
786 list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) {
787 if (nfq_id_after(entry->id, maxid))
788 break;
789 __dequeue_entry(queue, entry);
790 list_add_tail(&entry->list, &batch_list);
791 }
792
793 spin_unlock_bh(&queue->lock);
794
795 if (list_empty(&batch_list))
796 return -ENOENT;
797
798 list_for_each_entry_safe(entry, tmp, &batch_list, list) {
799 if (nfqa[NFQA_MARK])
800 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
801 nf_reinject(entry, verdict);
802 }
803 return 0;
804}
805
Harald Welte7af4cc32005-08-09 19:44:15 -0700806static int
807nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200808 const struct nlmsghdr *nlh,
809 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700810{
David S. Miller3da07c02012-06-26 21:35:27 -0700811 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Harald Welte7af4cc32005-08-09 19:44:15 -0700812 u_int16_t queue_num = ntohs(nfmsg->res_id);
813
814 struct nfqnl_msg_verdict_hdr *vhdr;
815 struct nfqnl_instance *queue;
816 unsigned int verdict;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800817 struct nf_queue_entry *entry;
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200818 enum ip_conntrack_info uninitialized_var(ctinfo);
819 struct nf_conn *ct = NULL;
Harald Welte7af4cc32005-08-09 19:44:15 -0700820
Gao fenge8179612013-03-24 23:50:47 +0000821 struct net *net = sock_net(ctnl);
822 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
Harald Welte7af4cc32005-08-09 19:44:15 -0700823
Gao fenge8179612013-03-24 23:50:47 +0000824 queue = instance_lookup(q, queue_num);
825 if (!queue)
826 queue = verdict_instance_lookup(q, queue_num,
827 NETLINK_CB(skb).portid);
Florian Westphal97d32cf2011-07-19 11:46:33 +0200828 if (IS_ERR(queue))
829 return PTR_ERR(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700830
Florian Westphal97d32cf2011-07-19 11:46:33 +0200831 vhdr = verdicthdr_get(nfqa);
832 if (!vhdr)
Eric Dumazet84a797d2011-07-18 16:08:27 +0200833 return -EINVAL;
Harald Welte7af4cc32005-08-09 19:44:15 -0700834
Harald Welte7af4cc32005-08-09 19:44:15 -0700835 verdict = ntohl(vhdr->verdict);
836
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800837 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
Eric Dumazet84a797d2011-07-18 16:08:27 +0200838 if (entry == NULL)
839 return -ENOENT;
Harald Welte7af4cc32005-08-09 19:44:15 -0700840
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200841 rcu_read_lock();
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +0200842 if (nfqa[NFQA_CT] && (queue->flags & NFQA_CFG_F_CONNTRACK))
843 ct = nfqnl_ct_parse(entry->skb, nfqa[NFQA_CT], &ctinfo);
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200844
Patrick McHardydf6fb862007-09-28 14:37:03 -0700845 if (nfqa[NFQA_PAYLOAD]) {
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200846 u16 payload_len = nla_len(nfqa[NFQA_PAYLOAD]);
847 int diff = payload_len - entry->skb->len;
848
Patrick McHardydf6fb862007-09-28 14:37:03 -0700849 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200850 payload_len, entry, diff) < 0)
Harald Welte7af4cc32005-08-09 19:44:15 -0700851 verdict = NF_DROP;
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200852
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +0200853 if (ct)
854 nfqnl_ct_seq_adjust(skb, ct, ctinfo, diff);
Harald Welte7af4cc32005-08-09 19:44:15 -0700855 }
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200856 rcu_read_unlock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700857
Patrick McHardydf6fb862007-09-28 14:37:03 -0700858 if (nfqa[NFQA_MARK])
Patrick McHardyea3a66f2007-12-05 01:30:02 -0800859 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800860
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800861 nf_reinject(entry, verdict);
Harald Welte7af4cc32005-08-09 19:44:15 -0700862 return 0;
863}
864
865static int
866nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200867 const struct nlmsghdr *nlh,
868 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700869{
870 return -ENOTSUPP;
871}
872
Patrick McHardy5bf75852007-09-28 14:39:26 -0700873static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
874 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
875 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
Harald Welte838ab632005-08-09 19:50:45 -0700876};
877
Patrick McHardye3ac5292007-12-05 01:23:57 -0800878static const struct nf_queue_handler nfqh = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700879 .outfn = &nfqnl_enqueue_packet,
880};
881
Harald Welte7af4cc32005-08-09 19:44:15 -0700882static int
883nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200884 const struct nlmsghdr *nlh,
885 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700886{
David S. Miller3da07c02012-06-26 21:35:27 -0700887 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Harald Welte7af4cc32005-08-09 19:44:15 -0700888 u_int16_t queue_num = ntohs(nfmsg->res_id);
889 struct nfqnl_instance *queue;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800890 struct nfqnl_msg_config_cmd *cmd = NULL;
Gao fenge8179612013-03-24 23:50:47 +0000891 struct net *net = sock_net(ctnl);
892 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
Harald Welte838ab632005-08-09 19:50:45 -0700893 int ret = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700894
Patrick McHardy9872bec2007-12-05 01:28:50 -0800895 if (nfqa[NFQA_CFG_CMD]) {
896 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
897
Florian Westphal0360ae42012-11-23 06:22:21 +0000898 /* Obsolete commands without queue context */
Patrick McHardy9872bec2007-12-05 01:28:50 -0800899 switch (cmd->command) {
Florian Westphal0360ae42012-11-23 06:22:21 +0000900 case NFQNL_CFG_CMD_PF_BIND: return 0;
901 case NFQNL_CFG_CMD_PF_UNBIND: return 0;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800902 }
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800903 }
904
Patrick McHardy9872bec2007-12-05 01:28:50 -0800905 rcu_read_lock();
Gao fenge8179612013-03-24 23:50:47 +0000906 queue = instance_lookup(q, queue_num);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000907 if (queue && queue->peer_portid != NETLINK_CB(skb).portid) {
Patrick McHardy9872bec2007-12-05 01:28:50 -0800908 ret = -EPERM;
909 goto err_out_unlock;
910 }
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800911
Patrick McHardy9872bec2007-12-05 01:28:50 -0800912 if (cmd != NULL) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700913 switch (cmd->command) {
914 case NFQNL_CFG_CMD_BIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800915 if (queue) {
916 ret = -EBUSY;
917 goto err_out_unlock;
918 }
Gao fenge8179612013-03-24 23:50:47 +0000919 queue = instance_create(q, queue_num,
920 NETLINK_CB(skb).portid);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800921 if (IS_ERR(queue)) {
922 ret = PTR_ERR(queue);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800923 goto err_out_unlock;
924 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700925 break;
926 case NFQNL_CFG_CMD_UNBIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -0800927 if (!queue) {
928 ret = -ENODEV;
929 goto err_out_unlock;
930 }
Gao fenge8179612013-03-24 23:50:47 +0000931 instance_destroy(q, queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700932 break;
933 case NFQNL_CFG_CMD_PF_BIND:
Harald Welte7af4cc32005-08-09 19:44:15 -0700934 case NFQNL_CFG_CMD_PF_UNBIND:
Harald Welte7af4cc32005-08-09 19:44:15 -0700935 break;
936 default:
Patrick McHardycd21f0a2007-12-17 22:40:19 -0800937 ret = -ENOTSUPP;
Harald Welte838ab632005-08-09 19:50:45 -0700938 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700939 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700940 }
941
Patrick McHardydf6fb862007-09-28 14:37:03 -0700942 if (nfqa[NFQA_CFG_PARAMS]) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700943 struct nfqnl_msg_config_params *params;
Harald Welte7af4cc32005-08-09 19:44:15 -0700944
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800945 if (!queue) {
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800946 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800947 goto err_out_unlock;
Patrick McHardy406dbfc2006-03-12 20:32:47 -0800948 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700949 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700950 nfqnl_set_mode(queue, params->copy_mode,
951 ntohl(params->copy_range));
952 }
953
Patrick McHardydf6fb862007-09-28 14:37:03 -0700954 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
Eric Leblond829e17a2006-11-29 02:35:33 +0100955 __be32 *queue_maxlen;
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800956
957 if (!queue) {
958 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800959 goto err_out_unlock;
Patrick McHardya3c8e7fd2007-12-05 01:28:30 -0800960 }
Patrick McHardydf6fb862007-09-28 14:37:03 -0700961 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
Eric Leblond829e17a2006-11-29 02:35:33 +0100962 spin_lock_bh(&queue->lock);
963 queue->queue_maxlen = ntohl(*queue_maxlen);
964 spin_unlock_bh(&queue->lock);
965 }
966
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000967 if (nfqa[NFQA_CFG_FLAGS]) {
968 __u32 flags, mask;
969
970 if (!queue) {
971 ret = -ENODEV;
972 goto err_out_unlock;
973 }
974
975 if (!nfqa[NFQA_CFG_MASK]) {
976 /* A mask is needed to specify which flags are being
977 * changed.
978 */
979 ret = -EINVAL;
980 goto err_out_unlock;
981 }
982
983 flags = ntohl(nla_get_be32(nfqa[NFQA_CFG_FLAGS]));
984 mask = ntohl(nla_get_be32(nfqa[NFQA_CFG_MASK]));
985
Krishna Kumar46ba5a22012-06-27 00:59:56 +0000986 if (flags >= NFQA_CFG_F_MAX) {
987 ret = -EOPNOTSUPP;
988 goto err_out_unlock;
989 }
990
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000991 spin_lock_bh(&queue->lock);
992 queue->flags &= ~mask;
993 queue->flags |= flags & mask;
994 spin_unlock_bh(&queue->lock);
995 }
996
Patrick McHardy9872bec2007-12-05 01:28:50 -0800997err_out_unlock:
998 rcu_read_unlock();
Harald Welte838ab632005-08-09 19:50:45 -0700999 return ret;
Harald Welte7af4cc32005-08-09 19:44:15 -07001000}
1001
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -07001002static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
Eric Dumazet84a797d2011-07-18 16:08:27 +02001003 [NFQNL_MSG_PACKET] = { .call_rcu = nfqnl_recv_unsupp,
Harald Welte37d2e7a2005-11-14 15:24:59 -08001004 .attr_count = NFQA_MAX, },
Eric Dumazet84a797d2011-07-18 16:08:27 +02001005 [NFQNL_MSG_VERDICT] = { .call_rcu = nfqnl_recv_verdict,
Patrick McHardy5bf75852007-09-28 14:39:26 -07001006 .attr_count = NFQA_MAX,
1007 .policy = nfqa_verdict_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -07001008 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
Patrick McHardy5bf75852007-09-28 14:39:26 -07001009 .attr_count = NFQA_CFG_MAX,
1010 .policy = nfqa_cfg_policy },
Florian Westphal97d32cf2011-07-19 11:46:33 +02001011 [NFQNL_MSG_VERDICT_BATCH]={ .call_rcu = nfqnl_recv_verdict_batch,
1012 .attr_count = NFQA_MAX,
1013 .policy = nfqa_verdict_batch_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -07001014};
1015
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -07001016static const struct nfnetlink_subsystem nfqnl_subsys = {
Harald Welte7af4cc32005-08-09 19:44:15 -07001017 .name = "nf_queue",
1018 .subsys_id = NFNL_SUBSYS_QUEUE,
1019 .cb_count = NFQNL_MSG_MAX,
Harald Welte7af4cc32005-08-09 19:44:15 -07001020 .cb = nfqnl_cb,
1021};
1022
Harald Welte838ab632005-08-09 19:50:45 -07001023#ifdef CONFIG_PROC_FS
1024struct iter_state {
Gao fenge8179612013-03-24 23:50:47 +00001025 struct seq_net_private p;
Harald Welte838ab632005-08-09 19:50:45 -07001026 unsigned int bucket;
1027};
1028
1029static struct hlist_node *get_first(struct seq_file *seq)
1030{
1031 struct iter_state *st = seq->private;
Gao fenge8179612013-03-24 23:50:47 +00001032 struct net *net;
1033 struct nfnl_queue_net *q;
Harald Welte838ab632005-08-09 19:50:45 -07001034
1035 if (!st)
1036 return NULL;
1037
Gao fenge8179612013-03-24 23:50:47 +00001038 net = seq_file_net(seq);
1039 q = nfnl_queue_pernet(net);
Harald Welte838ab632005-08-09 19:50:45 -07001040 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
Gao fenge8179612013-03-24 23:50:47 +00001041 if (!hlist_empty(&q->instance_table[st->bucket]))
1042 return q->instance_table[st->bucket].first;
Harald Welte838ab632005-08-09 19:50:45 -07001043 }
1044 return NULL;
1045}
1046
1047static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
1048{
1049 struct iter_state *st = seq->private;
Gao fenge8179612013-03-24 23:50:47 +00001050 struct net *net = seq_file_net(seq);
Harald Welte838ab632005-08-09 19:50:45 -07001051
1052 h = h->next;
1053 while (!h) {
Gao fenge8179612013-03-24 23:50:47 +00001054 struct nfnl_queue_net *q;
1055
Harald Welte838ab632005-08-09 19:50:45 -07001056 if (++st->bucket >= INSTANCE_BUCKETS)
1057 return NULL;
1058
Gao fenge8179612013-03-24 23:50:47 +00001059 q = nfnl_queue_pernet(net);
1060 h = q->instance_table[st->bucket].first;
Harald Welte838ab632005-08-09 19:50:45 -07001061 }
1062 return h;
1063}
1064
1065static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
1066{
1067 struct hlist_node *head;
1068 head = get_first(seq);
1069
1070 if (head)
1071 while (pos && (head = get_next(seq, head)))
1072 pos--;
1073 return pos ? NULL : head;
1074}
1075
Gao fenge8179612013-03-24 23:50:47 +00001076static void *seq_start(struct seq_file *s, loff_t *pos)
1077 __acquires(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
Harald Welte838ab632005-08-09 19:50:45 -07001078{
Gao fenge8179612013-03-24 23:50:47 +00001079 spin_lock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
1080 return get_idx(s, *pos);
Harald Welte838ab632005-08-09 19:50:45 -07001081}
1082
1083static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1084{
1085 (*pos)++;
1086 return get_next(s, v);
1087}
1088
1089static void seq_stop(struct seq_file *s, void *v)
Gao fenge8179612013-03-24 23:50:47 +00001090 __releases(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
Harald Welte838ab632005-08-09 19:50:45 -07001091{
Gao fenge8179612013-03-24 23:50:47 +00001092 spin_unlock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
Harald Welte838ab632005-08-09 19:50:45 -07001093}
1094
1095static int seq_show(struct seq_file *s, void *v)
1096{
1097 const struct nfqnl_instance *inst = v;
1098
1099 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
1100 inst->queue_num,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001101 inst->peer_portid, inst->queue_total,
Harald Welte838ab632005-08-09 19:50:45 -07001102 inst->copy_mode, inst->copy_range,
1103 inst->queue_dropped, inst->queue_user_dropped,
Eric Dumazet58637022011-07-19 11:44:17 +02001104 inst->id_sequence, 1);
Harald Welte838ab632005-08-09 19:50:45 -07001105}
1106
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001107static const struct seq_operations nfqnl_seq_ops = {
Harald Welte838ab632005-08-09 19:50:45 -07001108 .start = seq_start,
1109 .next = seq_next,
1110 .stop = seq_stop,
1111 .show = seq_show,
1112};
1113
1114static int nfqnl_open(struct inode *inode, struct file *file)
1115{
Gao fenge8179612013-03-24 23:50:47 +00001116 return seq_open_net(inode, file, &nfqnl_seq_ops,
Pavel Emelyanove2da5912007-10-10 02:29:58 -07001117 sizeof(struct iter_state));
Harald Welte838ab632005-08-09 19:50:45 -07001118}
1119
Arjan van de Venda7071d2007-02-12 00:55:36 -08001120static const struct file_operations nfqnl_file_ops = {
Harald Welte838ab632005-08-09 19:50:45 -07001121 .owner = THIS_MODULE,
1122 .open = nfqnl_open,
1123 .read = seq_read,
1124 .llseek = seq_lseek,
Gao fenge8179612013-03-24 23:50:47 +00001125 .release = seq_release_net,
Harald Welte838ab632005-08-09 19:50:45 -07001126};
1127
1128#endif /* PROC_FS */
1129
Gao fenge8179612013-03-24 23:50:47 +00001130static int __net_init nfnl_queue_net_init(struct net *net)
Harald Welte7af4cc32005-08-09 19:44:15 -07001131{
Gao fenge8179612013-03-24 23:50:47 +00001132 unsigned int i;
1133 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001134
Harald Welte838ab632005-08-09 19:50:45 -07001135 for (i = 0; i < INSTANCE_BUCKETS; i++)
Gao fenge8179612013-03-24 23:50:47 +00001136 INIT_HLIST_HEAD(&q->instance_table[i]);
1137
1138 spin_lock_init(&q->instances_lock);
1139
1140#ifdef CONFIG_PROC_FS
1141 if (!proc_create("nfnetlink_queue", 0440,
1142 net->nf.proc_netfilter, &nfqnl_file_ops))
1143 return -ENOMEM;
1144#endif
1145 return 0;
1146}
1147
1148static void __net_exit nfnl_queue_net_exit(struct net *net)
1149{
1150 remove_proc_entry("nfnetlink_queue", net->nf.proc_netfilter);
1151}
1152
1153static struct pernet_operations nfnl_queue_net_ops = {
1154 .init = nfnl_queue_net_init,
1155 .exit = nfnl_queue_net_exit,
1156 .id = &nfnl_queue_net_id,
1157 .size = sizeof(struct nfnl_queue_net),
1158};
1159
1160static int __init nfnetlink_queue_init(void)
1161{
1162 int status = -ENOMEM;
Harald Welte838ab632005-08-09 19:50:45 -07001163
Harald Welte7af4cc32005-08-09 19:44:15 -07001164 netlink_register_notifier(&nfqnl_rtnl_notifier);
1165 status = nfnetlink_subsys_register(&nfqnl_subsys);
1166 if (status < 0) {
Gao fenge8179612013-03-24 23:50:47 +00001167 pr_err("nf_queue: failed to create netlink socket\n");
Harald Welte7af4cc32005-08-09 19:44:15 -07001168 goto cleanup_netlink_notifier;
1169 }
1170
Gao fenge8179612013-03-24 23:50:47 +00001171 status = register_pernet_subsys(&nfnl_queue_net_ops);
1172 if (status < 0) {
1173 pr_err("nf_queue: failed to register pernet ops\n");
Harald Welte838ab632005-08-09 19:50:45 -07001174 goto cleanup_subsys;
Gao fenge8179612013-03-24 23:50:47 +00001175 }
Harald Welte7af4cc32005-08-09 19:44:15 -07001176 register_netdevice_notifier(&nfqnl_dev_notifier);
Florian Westphal0360ae42012-11-23 06:22:21 +00001177 nf_register_queue_handler(&nfqh);
Harald Welte7af4cc32005-08-09 19:44:15 -07001178 return status;
1179
Harald Welte838ab632005-08-09 19:50:45 -07001180cleanup_subsys:
Harald Welte7af4cc32005-08-09 19:44:15 -07001181 nfnetlink_subsys_unregister(&nfqnl_subsys);
Harald Welte7af4cc32005-08-09 19:44:15 -07001182cleanup_netlink_notifier:
1183 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1184 return status;
1185}
1186
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001187static void __exit nfnetlink_queue_fini(void)
Harald Welte7af4cc32005-08-09 19:44:15 -07001188{
Florian Westphal0360ae42012-11-23 06:22:21 +00001189 nf_unregister_queue_handler();
Patrick McHardy32292a72006-04-06 14:11:30 -07001190 unregister_netdevice_notifier(&nfqnl_dev_notifier);
Gao fenge8179612013-03-24 23:50:47 +00001191 unregister_pernet_subsys(&nfnl_queue_net_ops);
Patrick McHardy32292a72006-04-06 14:11:30 -07001192 nfnetlink_subsys_unregister(&nfqnl_subsys);
1193 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
Jesper Dangaard Brouer67137f32009-06-08 03:11:33 +00001194
1195 rcu_barrier(); /* Wait for completion of call_rcu()'s */
Harald Welte7af4cc32005-08-09 19:44:15 -07001196}
1197
1198MODULE_DESCRIPTION("netfilter packet queue handler");
1199MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1200MODULE_LICENSE("GPL");
1201MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1202
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001203module_init(nfnetlink_queue_init);
1204module_exit(nfnetlink_queue_fini);