blob: ae2e5c11d01ac9887c5158d0084f193c624373ef [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
Florian Westphal9cefbbc2013-06-04 22:22:15 +000044/* We're using struct nlattr which has 16bit nla_len. Note that nla_len
45 * includes the header length. Thus, the maximum packet length that we
46 * support is 65531 bytes. We send truncated packets if the specified length
47 * is larger than that. Userspace can check for presence of NFQA_CAP_LEN
48 * attribute to detect truncation.
49 */
50#define NFQNL_MAX_COPY_RANGE (0xffff - NLA_HDRLEN)
51
Harald Welte7af4cc32005-08-09 19:44:15 -070052struct nfqnl_instance {
53 struct hlist_node hlist; /* global list of queues */
Patrick McHardy9872bec2007-12-05 01:28:50 -080054 struct rcu_head rcu;
Harald Welte7af4cc32005-08-09 19:44:15 -070055
Eric W. Biederman15e47302012-09-07 20:12:54 +000056 int peer_portid;
Harald Welte7af4cc32005-08-09 19:44:15 -070057 unsigned int queue_maxlen;
58 unsigned int copy_range;
Harald Welte7af4cc32005-08-09 19:44:15 -070059 unsigned int queue_dropped;
60 unsigned int queue_user_dropped;
61
Harald Welte7af4cc32005-08-09 19:44:15 -070062
63 u_int16_t queue_num; /* number of this queue */
64 u_int8_t copy_mode;
Krishna Kumarfdb694a2012-05-24 03:56:44 +000065 u_int32_t flags; /* Set using NFQA_CFG_FLAGS */
Eric Dumazetc463ac92010-06-09 18:07:06 +020066/*
67 * Following fields are dirtied for each queued packet,
68 * keep them in same cache line if possible.
69 */
70 spinlock_t lock;
71 unsigned int queue_total;
Eric Dumazet58637022011-07-19 11:44:17 +020072 unsigned int id_sequence; /* 'sequence' of pkt ids */
Harald Welte7af4cc32005-08-09 19:44:15 -070073 struct list_head queue_list; /* packets in queue */
74};
75
Patrick McHardy02f014d2007-12-05 01:26:33 -080076typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
Harald Welte7af4cc32005-08-09 19:44:15 -070077
Gao fenge8179612013-03-24 23:50:47 +000078static int nfnl_queue_net_id __read_mostly;
Harald Welte7af4cc32005-08-09 19:44:15 -070079
Harald Welte7af4cc32005-08-09 19:44:15 -070080#define INSTANCE_BUCKETS 16
Gao fenge8179612013-03-24 23:50:47 +000081struct nfnl_queue_net {
82 spinlock_t instances_lock;
83 struct hlist_head instance_table[INSTANCE_BUCKETS];
84};
85
86static struct nfnl_queue_net *nfnl_queue_pernet(struct net *net)
87{
88 return net_generic(net, nfnl_queue_net_id);
89}
Harald Welte7af4cc32005-08-09 19:44:15 -070090
91static inline u_int8_t instance_hashfn(u_int16_t queue_num)
92{
Pablo Neira Ayuso1cdb0902013-03-14 06:03:17 +000093 return ((queue_num >> 8) ^ queue_num) % INSTANCE_BUCKETS;
Harald Welte7af4cc32005-08-09 19:44:15 -070094}
95
96static struct nfqnl_instance *
Gao fenge8179612013-03-24 23:50:47 +000097instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num)
Harald Welte7af4cc32005-08-09 19:44:15 -070098{
99 struct hlist_head *head;
Harald Welte7af4cc32005-08-09 19:44:15 -0700100 struct nfqnl_instance *inst;
101
Gao fenge8179612013-03-24 23:50:47 +0000102 head = &q->instance_table[instance_hashfn(queue_num)];
Sasha Levinb67bfe02013-02-27 17:06:00 -0800103 hlist_for_each_entry_rcu(inst, head, hlist) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700104 if (inst->queue_num == queue_num)
105 return inst;
106 }
107 return NULL;
108}
109
110static struct nfqnl_instance *
Gao fenge8179612013-03-24 23:50:47 +0000111instance_create(struct nfnl_queue_net *q, u_int16_t queue_num,
112 int portid)
Harald Welte7af4cc32005-08-09 19:44:15 -0700113{
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800114 struct nfqnl_instance *inst;
Patrick McHardy9872bec2007-12-05 01:28:50 -0800115 unsigned int h;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800116 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700117
Gao fenge8179612013-03-24 23:50:47 +0000118 spin_lock(&q->instances_lock);
119 if (instance_lookup(q, queue_num)) {
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800120 err = -EEXIST;
Harald Welte7af4cc32005-08-09 19:44:15 -0700121 goto out_unlock;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800122 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700123
Harald Welte10dfdc62005-11-03 19:20:07 +0100124 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800125 if (!inst) {
126 err = -ENOMEM;
Harald Welte7af4cc32005-08-09 19:44:15 -0700127 goto out_unlock;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800128 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700129
Harald Welte7af4cc32005-08-09 19:44:15 -0700130 inst->queue_num = queue_num;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000131 inst->peer_portid = portid;
Harald Welte7af4cc32005-08-09 19:44:15 -0700132 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
Florian Westphal9cefbbc2013-06-04 22:22:15 +0000133 inst->copy_range = NFQNL_MAX_COPY_RANGE;
Harald Welte7af4cc32005-08-09 19:44:15 -0700134 inst->copy_mode = NFQNL_COPY_NONE;
YOSHIFUJI Hideaki181a46a2006-01-04 13:56:54 -0800135 spin_lock_init(&inst->lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700136 INIT_LIST_HEAD(&inst->queue_list);
137
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800138 if (!try_module_get(THIS_MODULE)) {
139 err = -EAGAIN;
Harald Welte7af4cc32005-08-09 19:44:15 -0700140 goto out_free;
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800141 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700142
Patrick McHardy9872bec2007-12-05 01:28:50 -0800143 h = instance_hashfn(queue_num);
Gao fenge8179612013-03-24 23:50:47 +0000144 hlist_add_head_rcu(&inst->hlist, &q->instance_table[h]);
Harald Welte7af4cc32005-08-09 19:44:15 -0700145
Gao fenge8179612013-03-24 23:50:47 +0000146 spin_unlock(&q->instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700147
Harald Welte7af4cc32005-08-09 19:44:15 -0700148 return inst;
149
150out_free:
151 kfree(inst);
152out_unlock:
Gao fenge8179612013-03-24 23:50:47 +0000153 spin_unlock(&q->instances_lock);
Patrick McHardybaab2ce2007-12-17 22:41:21 -0800154 return ERR_PTR(err);
Harald Welte7af4cc32005-08-09 19:44:15 -0700155}
156
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800157static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
158 unsigned long data);
Harald Welte7af4cc32005-08-09 19:44:15 -0700159
160static void
Patrick McHardy9872bec2007-12-05 01:28:50 -0800161instance_destroy_rcu(struct rcu_head *head)
Harald Welte7af4cc32005-08-09 19:44:15 -0700162{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800163 struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
164 rcu);
Harald Welte7af4cc32005-08-09 19:44:15 -0700165
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800166 nfqnl_flush(inst, NULL, 0);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800167 kfree(inst);
Harald Welte7af4cc32005-08-09 19:44:15 -0700168 module_put(THIS_MODULE);
169}
170
Patrick McHardy9872bec2007-12-05 01:28:50 -0800171static void
Harald Welte7af4cc32005-08-09 19:44:15 -0700172__instance_destroy(struct nfqnl_instance *inst)
173{
Patrick McHardy9872bec2007-12-05 01:28:50 -0800174 hlist_del_rcu(&inst->hlist);
175 call_rcu(&inst->rcu, instance_destroy_rcu);
Harald Welte7af4cc32005-08-09 19:44:15 -0700176}
177
Patrick McHardy9872bec2007-12-05 01:28:50 -0800178static void
Gao fenge8179612013-03-24 23:50:47 +0000179instance_destroy(struct nfnl_queue_net *q, struct nfqnl_instance *inst)
Harald Welte7af4cc32005-08-09 19:44:15 -0700180{
Gao fenge8179612013-03-24 23:50:47 +0000181 spin_lock(&q->instances_lock);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800182 __instance_destroy(inst);
Gao fenge8179612013-03-24 23:50:47 +0000183 spin_unlock(&q->instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700184}
185
Harald Welte7af4cc32005-08-09 19:44:15 -0700186static inline void
Patrick McHardy02f014d2007-12-05 01:26:33 -0800187__enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
Harald Welte7af4cc32005-08-09 19:44:15 -0700188{
Patrick McHardy0ac41e82007-12-05 01:25:03 -0800189 list_add_tail(&entry->list, &queue->queue_list);
Harald Welte7af4cc32005-08-09 19:44:15 -0700190 queue->queue_total++;
191}
192
Florian Westphal97d32cf2011-07-19 11:46:33 +0200193static void
194__dequeue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
195{
196 list_del(&entry->list);
197 queue->queue_total--;
198}
199
Patrick McHardy02f014d2007-12-05 01:26:33 -0800200static struct nf_queue_entry *
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800201find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
Harald Welte7af4cc32005-08-09 19:44:15 -0700202{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800203 struct nf_queue_entry *entry = NULL, *i;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800204
Harald Welte7af4cc32005-08-09 19:44:15 -0700205 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800206
207 list_for_each_entry(i, &queue->queue_list, list) {
208 if (i->id == id) {
209 entry = i;
210 break;
211 }
212 }
213
Florian Westphal97d32cf2011-07-19 11:46:33 +0200214 if (entry)
215 __dequeue_entry(queue, entry);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800216
Harald Welte7af4cc32005-08-09 19:44:15 -0700217 spin_unlock_bh(&queue->lock);
218
219 return entry;
220}
221
222static void
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800223nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
Harald Welte7af4cc32005-08-09 19:44:15 -0700224{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800225 struct nf_queue_entry *entry, *next;
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800226
Harald Welte7af4cc32005-08-09 19:44:15 -0700227 spin_lock_bh(&queue->lock);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800228 list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
229 if (!cmpfn || cmpfn(entry, data)) {
230 list_del(&entry->list);
231 queue->queue_total--;
Patrick McHardy4b3d15e2007-12-05 01:27:02 -0800232 nf_reinject(entry, NF_DROP);
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800233 }
234 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700235 spin_unlock_bh(&queue->lock);
236}
237
Eric Dumazetae08ce02013-03-17 17:15:55 +0000238static void
239nfqnl_zcopy(struct sk_buff *to, const struct sk_buff *from, int len, int hlen)
240{
241 int i, j = 0;
242 int plen = 0; /* length of skb->head fragment */
243 struct page *page;
244 unsigned int offset;
245
246 /* dont bother with small payloads */
247 if (len <= skb_tailroom(to)) {
248 skb_copy_bits(from, 0, skb_put(to, len), len);
249 return;
250 }
251
252 if (hlen) {
253 skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
254 len -= hlen;
255 } else {
256 plen = min_t(int, skb_headlen(from), len);
257 if (plen) {
258 page = virt_to_head_page(from->head);
259 offset = from->data - (unsigned char *)page_address(page);
260 __skb_fill_page_desc(to, 0, page, offset, plen);
261 get_page(page);
262 j = 1;
263 len -= plen;
264 }
265 }
266
267 to->truesize += len + plen;
268 to->len += len + plen;
269 to->data_len += len + plen;
270
271 for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
272 if (!len)
273 break;
274 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
275 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
276 len -= skb_shinfo(to)->frags[j].size;
277 skb_frag_ref(to, j);
278 j++;
279 }
280 skb_shinfo(to)->nr_frags = j;
281}
282
Florian Westphal496e4ae2013-06-29 14:15:47 +0200283static int
284nfqnl_put_packet_info(struct sk_buff *nlskb, struct sk_buff *packet,
285 bool csum_verify)
Florian Westphal72371902013-04-19 04:58:26 +0000286{
287 __u32 flags = 0;
288
289 if (packet->ip_summed == CHECKSUM_PARTIAL)
290 flags = NFQA_SKB_CSUMNOTREADY;
Florian Westphal496e4ae2013-06-29 14:15:47 +0200291 else if (csum_verify)
292 flags = NFQA_SKB_CSUM_NOTVERIFIED;
293
Florian Westphal72371902013-04-19 04:58:26 +0000294 if (skb_is_gso(packet))
295 flags |= NFQA_SKB_GSO;
296
297 return flags ? nla_put_be32(nlskb, NFQA_SKB_INFO, htonl(flags)) : 0;
298}
299
Harald Welte7af4cc32005-08-09 19:44:15 -0700300static struct sk_buff *
301nfqnl_build_packet_message(struct nfqnl_instance *queue,
Eric Dumazet58637022011-07-19 11:44:17 +0200302 struct nf_queue_entry *entry,
303 __be32 **packet_id_ptr)
Harald Welte7af4cc32005-08-09 19:44:15 -0700304{
Harald Welte7af4cc32005-08-09 19:44:15 -0700305 size_t size;
Pablo Neira Ayuso6ee584b2012-09-24 14:52:12 +0200306 size_t data_len = 0, cap_len = 0;
Eric Dumazetae08ce02013-03-17 17:15:55 +0000307 int hlen = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700308 struct sk_buff *skb;
Eric Dumazet58637022011-07-19 11:44:17 +0200309 struct nlattr *nla;
310 struct nfqnl_msg_packet_hdr *pmsg;
Harald Welte7af4cc32005-08-09 19:44:15 -0700311 struct nlmsghdr *nlh;
312 struct nfgenmsg *nfmsg;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800313 struct sk_buff *entskb = entry->skb;
314 struct net_device *indev;
315 struct net_device *outdev;
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200316 struct nf_conn *ct = NULL;
317 enum ip_conntrack_info uninitialized_var(ctinfo);
Florian Westphal496e4ae2013-06-29 14:15:47 +0200318 bool csum_verify;
Harald Welte7af4cc32005-08-09 19:44:15 -0700319
Hong zhi guo573ce262013-03-27 06:47:04 +0000320 size = nlmsg_total_size(sizeof(struct nfgenmsg))
Patrick McHardydf6fb862007-09-28 14:37:03 -0700321 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
322 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
323 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700324#ifdef CONFIG_BRIDGE_NETFILTER
Patrick McHardydf6fb862007-09-28 14:37:03 -0700325 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
326 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700327#endif
Patrick McHardydf6fb862007-09-28 14:37:03 -0700328 + nla_total_size(sizeof(u_int32_t)) /* mark */
329 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
Florian Westphal72371902013-04-19 04:58:26 +0000330 + nla_total_size(sizeof(u_int32_t)) /* skbinfo */
Eric Dumazetae08ce02013-03-17 17:15:55 +0000331 + nla_total_size(sizeof(u_int32_t)); /* cap_len */
332
333 if (entskb->tstamp.tv64)
334 size += nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
Harald Welte7af4cc32005-08-09 19:44:15 -0700335
Florian Westphal496e4ae2013-06-29 14:15:47 +0200336 if (entry->hook <= NF_INET_FORWARD ||
337 (entry->hook == NF_INET_POST_ROUTING && entskb->sk == NULL))
338 csum_verify = !skb_csum_unnecessary(entskb);
339 else
340 csum_verify = false;
341
Patrick McHardy02f014d2007-12-05 01:26:33 -0800342 outdev = entry->outdev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800343
Eric Dumazetc463ac92010-06-09 18:07:06 +0200344 switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700345 case NFQNL_COPY_META:
346 case NFQNL_COPY_NONE:
Harald Welte7af4cc32005-08-09 19:44:15 -0700347 break;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800348
Harald Welte7af4cc32005-08-09 19:44:15 -0700349 case NFQNL_COPY_PACKET:
Florian Westphal00bd1cc2013-04-19 04:58:27 +0000350 if (!(queue->flags & NFQA_CFG_F_GSO) &&
351 entskb->ip_summed == CHECKSUM_PARTIAL &&
Eric Dumazetc463ac92010-06-09 18:07:06 +0200352 skb_checksum_help(entskb))
Patrick McHardye7dfb092005-09-06 15:10:00 -0700353 return NULL;
Eric Dumazetc463ac92010-06-09 18:07:06 +0200354
355 data_len = ACCESS_ONCE(queue->copy_range);
Florian Westphal9cefbbc2013-06-04 22:22:15 +0000356 if (data_len > entskb->len)
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800357 data_len = entskb->len;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800358
Eric Dumazetae08ce02013-03-17 17:15:55 +0000359 if (!entskb->head_frag ||
360 skb_headlen(entskb) < L1_CACHE_BYTES ||
361 skb_shinfo(entskb)->nr_frags >= MAX_SKB_FRAGS)
362 hlen = skb_headlen(entskb);
363
364 if (skb_has_frag_list(entskb))
365 hlen = entskb->len;
366 hlen = min_t(int, data_len, hlen);
367 size += sizeof(struct nlattr) + hlen;
Pablo Neira Ayuso6ee584b2012-09-24 14:52:12 +0200368 cap_len = entskb->len;
Harald Welte7af4cc32005-08-09 19:44:15 -0700369 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700370 }
371
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +0200372 if (queue->flags & NFQA_CFG_F_CONNTRACK)
373 ct = nfqnl_ct_get(entskb, &size, &ctinfo);
Harald Welte7af4cc32005-08-09 19:44:15 -0700374
Patrick McHardy3ab1f682013-04-17 06:47:09 +0000375 skb = nfnetlink_alloc_skb(&init_net, size, queue->peer_portid,
376 GFP_ATOMIC);
Harald Welte7af4cc32005-08-09 19:44:15 -0700377 if (!skb)
David S. Miller3da07c02012-06-26 21:35:27 -0700378 return NULL;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800379
David S. Miller3da07c02012-06-26 21:35:27 -0700380 nlh = nlmsg_put(skb, 0, 0,
Harald Welte7af4cc32005-08-09 19:44:15 -0700381 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
David S. Miller3da07c02012-06-26 21:35:27 -0700382 sizeof(struct nfgenmsg), 0);
383 if (!nlh) {
384 kfree_skb(skb);
385 return NULL;
386 }
387 nfmsg = nlmsg_data(nlh);
Patrick McHardy02f014d2007-12-05 01:26:33 -0800388 nfmsg->nfgen_family = entry->pf;
Harald Welte7af4cc32005-08-09 19:44:15 -0700389 nfmsg->version = NFNETLINK_V0;
390 nfmsg->res_id = htons(queue->queue_num);
391
Eric Dumazet58637022011-07-19 11:44:17 +0200392 nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg));
393 pmsg = nla_data(nla);
394 pmsg->hw_protocol = entskb->protocol;
395 pmsg->hook = entry->hook;
396 *packet_id_ptr = &pmsg->packet_id;
Harald Welte7af4cc32005-08-09 19:44:15 -0700397
Patrick McHardy02f014d2007-12-05 01:26:33 -0800398 indev = entry->indev;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800399 if (indev) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700400#ifndef CONFIG_BRIDGE_NETFILTER
David S. Millera4471892012-03-29 23:27:38 -0400401 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex)))
402 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700403#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800404 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700405 /* Case 1: indev is physical input device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800406 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700407 * netfilter_bridge) */
David S. Millera4471892012-03-29 23:27:38 -0400408 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
409 htonl(indev->ifindex)) ||
Harald Weltefbcd9232005-08-09 20:22:10 -0700410 /* this is the bridge group "brX" */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000411 /* rcu_read_lock()ed by __nf_queue */
David S. Millera4471892012-03-29 23:27:38 -0400412 nla_put_be32(skb, NFQA_IFINDEX_INDEV,
413 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
414 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700415 } else {
416 /* Case 2: indev is bridge group, we need to look for
417 * physical device (when called from ipv4) */
David S. Millera4471892012-03-29 23:27:38 -0400418 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV,
419 htonl(indev->ifindex)))
420 goto nla_put_failure;
421 if (entskb->nf_bridge && entskb->nf_bridge->physindev &&
422 nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
423 htonl(entskb->nf_bridge->physindev->ifindex)))
424 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700425 }
426#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700427 }
428
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800429 if (outdev) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700430#ifndef CONFIG_BRIDGE_NETFILTER
David S. Millera4471892012-03-29 23:27:38 -0400431 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex)))
432 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700433#else
Patrick McHardy02f014d2007-12-05 01:26:33 -0800434 if (entry->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700435 /* Case 1: outdev is physical output device, we need to
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800436 * look for bridge group (when called from
Harald Weltefbcd9232005-08-09 20:22:10 -0700437 * netfilter_bridge) */
David S. Millera4471892012-03-29 23:27:38 -0400438 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
439 htonl(outdev->ifindex)) ||
Harald Weltefbcd9232005-08-09 20:22:10 -0700440 /* this is the bridge group "brX" */
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000441 /* rcu_read_lock()ed by __nf_queue */
David S. Millera4471892012-03-29 23:27:38 -0400442 nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
443 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
444 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700445 } else {
446 /* Case 2: outdev is bridge group, we need to look for
447 * physical output device (when called from ipv4) */
David S. Millera4471892012-03-29 23:27:38 -0400448 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
449 htonl(outdev->ifindex)))
450 goto nla_put_failure;
451 if (entskb->nf_bridge && entskb->nf_bridge->physoutdev &&
452 nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
453 htonl(entskb->nf_bridge->physoutdev->ifindex)))
454 goto nla_put_failure;
Harald Weltefbcd9232005-08-09 20:22:10 -0700455 }
456#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700457 }
458
David S. Millera4471892012-03-29 23:27:38 -0400459 if (entskb->mark &&
460 nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark)))
461 goto nla_put_failure;
Harald Welte7af4cc32005-08-09 19:44:15 -0700462
Nicolas Cavallari2c38de42011-06-16 17:27:04 +0200463 if (indev && entskb->dev &&
464 entskb->mac_header != entskb->network_header) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700465 struct nfqnl_msg_packet_hw phw;
Dan Carpentere4d091d2013-08-01 12:36:57 +0300466 int len;
467
468 memset(&phw, 0, sizeof(phw));
469 len = dev_parse_header(entskb, phw.hw_addr);
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700470 if (len) {
471 phw.hw_addrlen = htons(len);
David S. Millera4471892012-03-29 23:27:38 -0400472 if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw))
473 goto nla_put_failure;
Stephen Hemmingerb95cce32007-09-26 22:13:38 -0700474 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700475 }
476
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700477 if (entskb->tstamp.tv64) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700478 struct nfqnl_msg_packet_timestamp ts;
Eric Dumazetb7aa0bf2007-04-19 16:16:32 -0700479 struct timeval tv = ktime_to_timeval(entskb->tstamp);
480 ts.sec = cpu_to_be64(tv.tv_sec);
481 ts.usec = cpu_to_be64(tv.tv_usec);
Harald Welte7af4cc32005-08-09 19:44:15 -0700482
David S. Millera4471892012-03-29 23:27:38 -0400483 if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts))
484 goto nla_put_failure;
Harald Welte7af4cc32005-08-09 19:44:15 -0700485 }
486
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +0200487 if (ct && nfqnl_ct_put(skb, ct, ctinfo) < 0)
488 goto nla_put_failure;
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200489
Florian Westphal7f877122013-06-04 22:22:16 +0000490 if (cap_len > data_len &&
491 nla_put_be32(skb, NFQA_CAP_LEN, htonl(cap_len)))
Pablo Neira Ayuso6ee584b2012-09-24 14:52:12 +0200492 goto nla_put_failure;
493
Florian Westphal496e4ae2013-06-29 14:15:47 +0200494 if (nfqnl_put_packet_info(skb, entskb, csum_verify))
Florian Westphal72371902013-04-19 04:58:26 +0000495 goto nla_put_failure;
496
Eric Dumazetae08ce02013-03-17 17:15:55 +0000497 if (data_len) {
498 struct nlattr *nla;
499
500 if (skb_tailroom(skb) < sizeof(*nla) + hlen)
501 goto nla_put_failure;
502
503 nla = (struct nlattr *)skb_put(skb, sizeof(*nla));
504 nla->nla_type = NFQA_PAYLOAD;
505 nla->nla_len = nla_attr_size(data_len);
506
507 nfqnl_zcopy(skb, entskb, data_len, hlen);
508 }
509
510 nlh->nlmsg_len = skb->len;
Harald Welte7af4cc32005-08-09 19:44:15 -0700511 return skb;
512
Patrick McHardydf6fb862007-09-28 14:37:03 -0700513nla_put_failure:
Wei Yongjuna6729952012-08-28 03:14:15 +0000514 kfree_skb(skb);
Joe Perchese87cc472012-05-13 21:56:26 +0000515 net_err_ratelimited("nf_queue: error creating packet message\n");
Harald Welte7af4cc32005-08-09 19:44:15 -0700516 return NULL;
517}
518
519static int
Florian Westphala5fedd432013-04-19 04:58:25 +0000520__nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue,
521 struct nf_queue_entry *entry)
Harald Welte7af4cc32005-08-09 19:44:15 -0700522{
Harald Welte7af4cc32005-08-09 19:44:15 -0700523 struct sk_buff *nskb;
Florian Westphalf1585082011-01-18 15:27:28 +0100524 int err = -ENOBUFS;
Eric Dumazet58637022011-07-19 11:44:17 +0200525 __be32 *packet_id_ptr;
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000526 int failopen = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700527
Eric Dumazet58637022011-07-19 11:44:17 +0200528 nskb = nfqnl_build_packet_message(queue, entry, &packet_id_ptr);
Florian Westphalf1585082011-01-18 15:27:28 +0100529 if (nskb == NULL) {
530 err = -ENOMEM;
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800531 goto err_out;
Florian Westphalf1585082011-01-18 15:27:28 +0100532 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700533 spin_lock_bh(&queue->lock);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800534
Harald Welte7af4cc32005-08-09 19:44:15 -0700535 if (queue->queue_total >= queue->queue_maxlen) {
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000536 if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
537 failopen = 1;
538 err = 0;
539 } else {
540 queue->queue_dropped++;
541 net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n",
542 queue->queue_total);
543 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700544 goto err_out_free_nskb;
545 }
Eric Dumazet58637022011-07-19 11:44:17 +0200546 entry->id = ++queue->id_sequence;
547 *packet_id_ptr = htonl(entry->id);
Harald Welte7af4cc32005-08-09 19:44:15 -0700548
549 /* nfnetlink_unicast will either free the nskb or add it to a socket */
Gao fenge8179612013-03-24 23:50:47 +0000550 err = nfnetlink_unicast(nskb, net, queue->peer_portid, MSG_DONTWAIT);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800551 if (err < 0) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800552 queue->queue_user_dropped++;
Harald Welte7af4cc32005-08-09 19:44:15 -0700553 goto err_out_unlock;
554 }
555
556 __enqueue_entry(queue, entry);
557
558 spin_unlock_bh(&queue->lock);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800559 return 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700560
561err_out_free_nskb:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800562 kfree_skb(nskb);
Harald Welte7af4cc32005-08-09 19:44:15 -0700563err_out_unlock:
564 spin_unlock_bh(&queue->lock);
Krishna Kumarfdb694a2012-05-24 03:56:44 +0000565 if (failopen)
566 nf_reinject(entry, NF_ACCEPT);
Patrick McHardy0ef0f462007-12-05 01:31:01 -0800567err_out:
Florian Westphalf1585082011-01-18 15:27:28 +0100568 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700569}
570
Florian Westphala5fedd432013-04-19 04:58:25 +0000571static struct nf_queue_entry *
572nf_queue_entry_dup(struct nf_queue_entry *e)
573{
574 struct nf_queue_entry *entry = kmemdup(e, e->size, GFP_ATOMIC);
575 if (entry) {
576 if (nf_queue_entry_get_refs(entry))
577 return entry;
578 kfree(entry);
579 }
580 return NULL;
581}
582
583#ifdef CONFIG_BRIDGE_NETFILTER
584/* When called from bridge netfilter, skb->data must point to MAC header
585 * before calling skb_gso_segment(). Else, original MAC header is lost
586 * and segmented skbs will be sent to wrong destination.
587 */
588static void nf_bridge_adjust_skb_data(struct sk_buff *skb)
589{
590 if (skb->nf_bridge)
591 __skb_push(skb, skb->network_header - skb->mac_header);
592}
593
594static void nf_bridge_adjust_segmented_data(struct sk_buff *skb)
595{
596 if (skb->nf_bridge)
597 __skb_pull(skb, skb->network_header - skb->mac_header);
598}
599#else
600#define nf_bridge_adjust_skb_data(s) do {} while (0)
601#define nf_bridge_adjust_segmented_data(s) do {} while (0)
602#endif
603
604static void free_entry(struct nf_queue_entry *entry)
605{
606 nf_queue_entry_release_refs(entry);
607 kfree(entry);
608}
609
610static int
611__nfqnl_enqueue_packet_gso(struct net *net, struct nfqnl_instance *queue,
612 struct sk_buff *skb, struct nf_queue_entry *entry)
613{
614 int ret = -ENOMEM;
615 struct nf_queue_entry *entry_seg;
616
617 nf_bridge_adjust_segmented_data(skb);
618
619 if (skb->next == NULL) { /* last packet, no need to copy entry */
620 struct sk_buff *gso_skb = entry->skb;
621 entry->skb = skb;
622 ret = __nfqnl_enqueue_packet(net, queue, entry);
623 if (ret)
624 entry->skb = gso_skb;
625 return ret;
626 }
627
628 skb->next = NULL;
629
630 entry_seg = nf_queue_entry_dup(entry);
631 if (entry_seg) {
632 entry_seg->skb = skb;
633 ret = __nfqnl_enqueue_packet(net, queue, entry_seg);
634 if (ret)
635 free_entry(entry_seg);
636 }
637 return ret;
638}
639
640static int
641nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
642{
643 unsigned int queued;
644 struct nfqnl_instance *queue;
645 struct sk_buff *skb, *segs;
646 int err = -ENOBUFS;
647 struct net *net = dev_net(entry->indev ?
648 entry->indev : entry->outdev);
649 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
650
651 /* rcu_read_lock()ed by nf_hook_slow() */
652 queue = instance_lookup(q, queuenum);
653 if (!queue)
654 return -ESRCH;
655
656 if (queue->copy_mode == NFQNL_COPY_NONE)
657 return -EINVAL;
658
Florian Westphala5fedd432013-04-19 04:58:25 +0000659 skb = entry->skb;
660
661 switch (entry->pf) {
662 case NFPROTO_IPV4:
663 skb->protocol = htons(ETH_P_IP);
664 break;
665 case NFPROTO_IPV6:
666 skb->protocol = htons(ETH_P_IPV6);
667 break;
668 }
669
Pablo Neira Ayuso7b8dfe22013-06-07 18:42:00 +0200670 if ((queue->flags & NFQA_CFG_F_GSO) || !skb_is_gso(skb))
671 return __nfqnl_enqueue_packet(net, queue, entry);
672
Florian Westphala5fedd432013-04-19 04:58:25 +0000673 nf_bridge_adjust_skb_data(skb);
674 segs = skb_gso_segment(skb, 0);
675 /* Does not use PTR_ERR to limit the number of error codes that can be
676 * returned by nf_queue. For instance, callers rely on -ECANCELED to
677 * mean 'ignore this hook'.
678 */
679 if (IS_ERR(segs))
680 goto out_err;
681 queued = 0;
682 err = 0;
683 do {
684 struct sk_buff *nskb = segs->next;
685 if (err == 0)
686 err = __nfqnl_enqueue_packet_gso(net, queue,
687 segs, entry);
688 if (err == 0)
689 queued++;
690 else
691 kfree_skb(segs);
692 segs = nskb;
693 } while (segs);
694
695 if (queued) {
696 if (err) /* some segments are already queued */
697 free_entry(entry);
698 kfree_skb(skb);
699 return 0;
700 }
701 out_err:
702 nf_bridge_adjust_segmented_data(skb);
703 return err;
704}
705
Harald Welte7af4cc32005-08-09 19:44:15 -0700706static int
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200707nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e, int diff)
Harald Welte7af4cc32005-08-09 19:44:15 -0700708{
Patrick McHardye2b58a62008-02-19 17:17:52 -0800709 struct sk_buff *nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700710
Patrick McHardyd8a585d2006-11-14 19:48:09 -0800711 if (diff < 0) {
712 if (pskb_trim(e->skb, data_len))
713 return -ENOMEM;
714 } else if (diff > 0) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700715 if (data_len > 0xFFFF)
716 return -EINVAL;
717 if (diff > skb_tailroom(e->skb)) {
Arnaud Ebalard9a732ed2008-04-29 03:16:34 -0700718 nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
719 diff, GFP_ATOMIC);
Patrick McHardye2b58a62008-02-19 17:17:52 -0800720 if (!nskb) {
Patrick McHardy1158ba22006-08-22 00:32:47 -0700721 printk(KERN_WARNING "nf_queue: OOM "
Harald Welte7af4cc32005-08-09 19:44:15 -0700722 "in mangle, dropping packet\n");
Patrick McHardye2b58a62008-02-19 17:17:52 -0800723 return -ENOMEM;
Harald Welte7af4cc32005-08-09 19:44:15 -0700724 }
Patrick McHardye2b58a62008-02-19 17:17:52 -0800725 kfree_skb(e->skb);
726 e->skb = nskb;
Harald Welte7af4cc32005-08-09 19:44:15 -0700727 }
728 skb_put(e->skb, diff);
729 }
Herbert Xu37d41872007-10-14 00:39:18 -0700730 if (!skb_make_writable(e->skb, data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700731 return -ENOMEM;
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300732 skb_copy_to_linear_data(e->skb, data, data_len);
Patrick McHardye7dfb092005-09-06 15:10:00 -0700733 e->skb->ip_summed = CHECKSUM_NONE;
Harald Welte7af4cc32005-08-09 19:44:15 -0700734 return 0;
735}
736
Harald Welte7af4cc32005-08-09 19:44:15 -0700737static int
738nfqnl_set_mode(struct nfqnl_instance *queue,
739 unsigned char mode, unsigned int range)
740{
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800741 int status = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700742
743 spin_lock_bh(&queue->lock);
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800744 switch (mode) {
745 case NFQNL_COPY_NONE:
746 case NFQNL_COPY_META:
747 queue->copy_mode = mode;
748 queue->copy_range = 0;
749 break;
750
751 case NFQNL_COPY_PACKET:
752 queue->copy_mode = mode;
Florian Westphal9cefbbc2013-06-04 22:22:15 +0000753 if (range == 0 || range > NFQNL_MAX_COPY_RANGE)
754 queue->copy_range = NFQNL_MAX_COPY_RANGE;
Patrick McHardyc5de0df2007-12-05 01:29:05 -0800755 else
756 queue->copy_range = range;
757 break;
758
759 default:
760 status = -EINVAL;
761
762 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700763 spin_unlock_bh(&queue->lock);
764
765 return status;
766}
767
768static int
Patrick McHardy02f014d2007-12-05 01:26:33 -0800769dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700770{
Patrick McHardy02f014d2007-12-05 01:26:33 -0800771 if (entry->indev)
772 if (entry->indev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700773 return 1;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800774 if (entry->outdev)
775 if (entry->outdev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700776 return 1;
Patrick McHardyef47c6a2006-06-27 03:01:48 -0700777#ifdef CONFIG_BRIDGE_NETFILTER
778 if (entry->skb->nf_bridge) {
779 if (entry->skb->nf_bridge->physindev &&
780 entry->skb->nf_bridge->physindev->ifindex == ifindex)
781 return 1;
782 if (entry->skb->nf_bridge->physoutdev &&
783 entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
784 return 1;
785 }
786#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700787 return 0;
788}
789
790/* drop all packets with either indev or outdev == ifindex from all queue
791 * instances */
792static void
Gao fenge8179612013-03-24 23:50:47 +0000793nfqnl_dev_drop(struct net *net, int ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700794{
795 int i;
Gao fenge8179612013-03-24 23:50:47 +0000796 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800797
Patrick McHardy9872bec2007-12-05 01:28:50 -0800798 rcu_read_lock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700799
Patrick McHardy9872bec2007-12-05 01:28:50 -0800800 for (i = 0; i < INSTANCE_BUCKETS; i++) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700801 struct nfqnl_instance *inst;
Gao fenge8179612013-03-24 23:50:47 +0000802 struct hlist_head *head = &q->instance_table[i];
Harald Welte7af4cc32005-08-09 19:44:15 -0700803
Sasha Levinb67bfe02013-02-27 17:06:00 -0800804 hlist_for_each_entry_rcu(inst, head, hlist)
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800805 nfqnl_flush(inst, dev_cmp, ifindex);
Harald Welte7af4cc32005-08-09 19:44:15 -0700806 }
807
Patrick McHardy9872bec2007-12-05 01:28:50 -0800808 rcu_read_unlock();
Harald Welte7af4cc32005-08-09 19:44:15 -0700809}
810
811#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
812
813static int
814nfqnl_rcv_dev_event(struct notifier_block *this,
815 unsigned long event, void *ptr)
816{
Jiri Pirko351638e2013-05-28 01:30:21 +0000817 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Harald Welte7af4cc32005-08-09 19:44:15 -0700818
819 /* Drop any packets associated with the downed device */
820 if (event == NETDEV_DOWN)
Gao fenge8179612013-03-24 23:50:47 +0000821 nfqnl_dev_drop(dev_net(dev), dev->ifindex);
Harald Welte7af4cc32005-08-09 19:44:15 -0700822 return NOTIFY_DONE;
823}
824
825static struct notifier_block nfqnl_dev_notifier = {
826 .notifier_call = nfqnl_rcv_dev_event,
827};
828
829static int
830nfqnl_rcv_nl_event(struct notifier_block *this,
831 unsigned long event, void *ptr)
832{
833 struct netlink_notify *n = ptr;
Gao fenge8179612013-03-24 23:50:47 +0000834 struct nfnl_queue_net *q = nfnl_queue_pernet(n->net);
Harald Welte7af4cc32005-08-09 19:44:15 -0700835
Patrick McHardydee58172009-11-06 17:04:00 +0100836 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700837 int i;
838
Eric W. Biederman15e47302012-09-07 20:12:54 +0000839 /* destroy all instances for this portid */
Gao fenge8179612013-03-24 23:50:47 +0000840 spin_lock(&q->instances_lock);
Patrick McHardy9872bec2007-12-05 01:28:50 -0800841 for (i = 0; i < INSTANCE_BUCKETS; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800842 struct hlist_node *t2;
Harald Welte7af4cc32005-08-09 19:44:15 -0700843 struct nfqnl_instance *inst;
Gao fenge8179612013-03-24 23:50:47 +0000844 struct hlist_head *head = &q->instance_table[i];
Harald Welte7af4cc32005-08-09 19:44:15 -0700845
Sasha Levinb67bfe02013-02-27 17:06:00 -0800846 hlist_for_each_entry_safe(inst, t2, head, hlist) {
Gao fenge8179612013-03-24 23:50:47 +0000847 if (n->portid == inst->peer_portid)
Harald Welte7af4cc32005-08-09 19:44:15 -0700848 __instance_destroy(inst);
849 }
850 }
Gao fenge8179612013-03-24 23:50:47 +0000851 spin_unlock(&q->instances_lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700852 }
853 return NOTIFY_DONE;
854}
855
856static struct notifier_block nfqnl_rtnl_notifier = {
857 .notifier_call = nfqnl_rcv_nl_event,
858};
859
Patrick McHardy5bf75852007-09-28 14:39:26 -0700860static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
861 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
862 [NFQA_MARK] = { .type = NLA_U32 },
863 [NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +0200864 [NFQA_CT] = { .type = NLA_UNSPEC },
Pablo Neira Ayusobd077932013-08-07 18:13:20 +0200865 [NFQA_EXP] = { .type = NLA_UNSPEC },
Harald Welte838ab632005-08-09 19:50:45 -0700866};
867
Florian Westphal97d32cf2011-07-19 11:46:33 +0200868static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = {
869 [NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
870 [NFQA_MARK] = { .type = NLA_U32 },
871};
872
Gao fenge8179612013-03-24 23:50:47 +0000873static struct nfqnl_instance *
874verdict_instance_lookup(struct nfnl_queue_net *q, u16 queue_num, int nlportid)
Florian Westphal97d32cf2011-07-19 11:46:33 +0200875{
876 struct nfqnl_instance *queue;
877
Gao fenge8179612013-03-24 23:50:47 +0000878 queue = instance_lookup(q, queue_num);
Florian Westphal97d32cf2011-07-19 11:46:33 +0200879 if (!queue)
880 return ERR_PTR(-ENODEV);
881
Eric W. Biederman15e47302012-09-07 20:12:54 +0000882 if (queue->peer_portid != nlportid)
Florian Westphal97d32cf2011-07-19 11:46:33 +0200883 return ERR_PTR(-EPERM);
884
885 return queue;
886}
887
888static struct nfqnl_msg_verdict_hdr*
889verdicthdr_get(const struct nlattr * const nfqa[])
890{
891 struct nfqnl_msg_verdict_hdr *vhdr;
892 unsigned int verdict;
893
894 if (!nfqa[NFQA_VERDICT_HDR])
895 return NULL;
896
897 vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
Florian Westphalc6675232011-08-30 15:01:20 +0200898 verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK;
899 if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN)
Florian Westphal97d32cf2011-07-19 11:46:33 +0200900 return NULL;
901 return vhdr;
902}
903
904static int nfq_id_after(unsigned int id, unsigned int max)
905{
906 return (int)(id - max) > 0;
907}
908
909static int
910nfqnl_recv_verdict_batch(struct sock *ctnl, struct sk_buff *skb,
911 const struct nlmsghdr *nlh,
912 const struct nlattr * const nfqa[])
913{
David S. Miller3da07c02012-06-26 21:35:27 -0700914 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Florian Westphal97d32cf2011-07-19 11:46:33 +0200915 struct nf_queue_entry *entry, *tmp;
916 unsigned int verdict, maxid;
917 struct nfqnl_msg_verdict_hdr *vhdr;
918 struct nfqnl_instance *queue;
919 LIST_HEAD(batch_list);
920 u16 queue_num = ntohs(nfmsg->res_id);
921
Gao fenge8179612013-03-24 23:50:47 +0000922 struct net *net = sock_net(ctnl);
923 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
924
925 queue = verdict_instance_lookup(q, queue_num,
926 NETLINK_CB(skb).portid);
Florian Westphal97d32cf2011-07-19 11:46:33 +0200927 if (IS_ERR(queue))
928 return PTR_ERR(queue);
929
930 vhdr = verdicthdr_get(nfqa);
931 if (!vhdr)
932 return -EINVAL;
933
934 verdict = ntohl(vhdr->verdict);
935 maxid = ntohl(vhdr->id);
936
937 spin_lock_bh(&queue->lock);
938
939 list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) {
940 if (nfq_id_after(entry->id, maxid))
941 break;
942 __dequeue_entry(queue, entry);
943 list_add_tail(&entry->list, &batch_list);
944 }
945
946 spin_unlock_bh(&queue->lock);
947
948 if (list_empty(&batch_list))
949 return -ENOENT;
950
951 list_for_each_entry_safe(entry, tmp, &batch_list, list) {
952 if (nfqa[NFQA_MARK])
953 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
954 nf_reinject(entry, verdict);
955 }
956 return 0;
957}
958
Harald Welte7af4cc32005-08-09 19:44:15 -0700959static int
960nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +0200961 const struct nlmsghdr *nlh,
962 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -0700963{
David S. Miller3da07c02012-06-26 21:35:27 -0700964 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Harald Welte7af4cc32005-08-09 19:44:15 -0700965 u_int16_t queue_num = ntohs(nfmsg->res_id);
966
967 struct nfqnl_msg_verdict_hdr *vhdr;
968 struct nfqnl_instance *queue;
969 unsigned int verdict;
Patrick McHardy02f014d2007-12-05 01:26:33 -0800970 struct nf_queue_entry *entry;
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +0200971 enum ip_conntrack_info uninitialized_var(ctinfo);
972 struct nf_conn *ct = NULL;
Harald Welte7af4cc32005-08-09 19:44:15 -0700973
Gao fenge8179612013-03-24 23:50:47 +0000974 struct net *net = sock_net(ctnl);
975 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
Harald Welte7af4cc32005-08-09 19:44:15 -0700976
Gao fenge8179612013-03-24 23:50:47 +0000977 queue = instance_lookup(q, queue_num);
978 if (!queue)
979 queue = verdict_instance_lookup(q, queue_num,
980 NETLINK_CB(skb).portid);
Florian Westphal97d32cf2011-07-19 11:46:33 +0200981 if (IS_ERR(queue))
982 return PTR_ERR(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700983
Florian Westphal97d32cf2011-07-19 11:46:33 +0200984 vhdr = verdicthdr_get(nfqa);
985 if (!vhdr)
Eric Dumazet84a797d2011-07-18 16:08:27 +0200986 return -EINVAL;
Harald Welte7af4cc32005-08-09 19:44:15 -0700987
Harald Welte7af4cc32005-08-09 19:44:15 -0700988 verdict = ntohl(vhdr->verdict);
989
Patrick McHardyb43d8d82007-12-05 01:25:30 -0800990 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
Eric Dumazet84a797d2011-07-18 16:08:27 +0200991 if (entry == NULL)
992 return -ENOENT;
Harald Welte7af4cc32005-08-09 19:44:15 -0700993
Pablo Neira Ayusobd077932013-08-07 18:13:20 +0200994 if (nfqa[NFQA_CT]) {
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +0200995 ct = nfqnl_ct_parse(entry->skb, nfqa[NFQA_CT], &ctinfo);
Pablo Neira Ayusobd077932013-08-07 18:13:20 +0200996 if (ct && nfqa[NFQA_EXP]) {
997 nfqnl_attach_expect(ct, nfqa[NFQA_EXP],
998 NETLINK_CB(skb).portid,
999 nlmsg_report(nlh));
1000 }
1001 }
Pablo Neira Ayuso9cb01762012-06-07 12:13:39 +02001002
Patrick McHardydf6fb862007-09-28 14:37:03 -07001003 if (nfqa[NFQA_PAYLOAD]) {
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +02001004 u16 payload_len = nla_len(nfqa[NFQA_PAYLOAD]);
1005 int diff = payload_len - entry->skb->len;
1006
Patrick McHardydf6fb862007-09-28 14:37:03 -07001007 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +02001008 payload_len, entry, diff) < 0)
Harald Welte7af4cc32005-08-09 19:44:15 -07001009 verdict = NF_DROP;
Pablo Neira Ayuso8c88f872012-06-07 13:31:25 +02001010
Pablo Neira Ayuso7c622342012-06-19 02:10:57 +02001011 if (ct)
Gao feng0a0d80e2013-09-17 13:03:47 +02001012 nfqnl_ct_seq_adjust(entry->skb, ct, ctinfo, diff);
Harald Welte7af4cc32005-08-09 19:44:15 -07001013 }
1014
Patrick McHardydf6fb862007-09-28 14:37:03 -07001015 if (nfqa[NFQA_MARK])
Patrick McHardyea3a66f2007-12-05 01:30:02 -08001016 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001017
Patrick McHardy4b3d15e2007-12-05 01:27:02 -08001018 nf_reinject(entry, verdict);
Harald Welte7af4cc32005-08-09 19:44:15 -07001019 return 0;
1020}
1021
1022static int
1023nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +02001024 const struct nlmsghdr *nlh,
1025 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -07001026{
1027 return -ENOTSUPP;
1028}
1029
Patrick McHardy5bf75852007-09-28 14:39:26 -07001030static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
1031 [NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
1032 [NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
Harald Welte838ab632005-08-09 19:50:45 -07001033};
1034
Patrick McHardye3ac5292007-12-05 01:23:57 -08001035static const struct nf_queue_handler nfqh = {
Harald Weltebbd86b9f2005-08-09 20:23:11 -07001036 .outfn = &nfqnl_enqueue_packet,
1037};
1038
Harald Welte7af4cc32005-08-09 19:44:15 -07001039static int
1040nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
Patrick McHardy39938322009-08-25 16:07:58 +02001041 const struct nlmsghdr *nlh,
1042 const struct nlattr * const nfqa[])
Harald Welte7af4cc32005-08-09 19:44:15 -07001043{
David S. Miller3da07c02012-06-26 21:35:27 -07001044 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
Harald Welte7af4cc32005-08-09 19:44:15 -07001045 u_int16_t queue_num = ntohs(nfmsg->res_id);
1046 struct nfqnl_instance *queue;
Patrick McHardy9872bec2007-12-05 01:28:50 -08001047 struct nfqnl_msg_config_cmd *cmd = NULL;
Gao fenge8179612013-03-24 23:50:47 +00001048 struct net *net = sock_net(ctnl);
1049 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
Harald Welte838ab632005-08-09 19:50:45 -07001050 int ret = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -07001051
Patrick McHardy9872bec2007-12-05 01:28:50 -08001052 if (nfqa[NFQA_CFG_CMD]) {
1053 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
1054
Florian Westphal0360ae42012-11-23 06:22:21 +00001055 /* Obsolete commands without queue context */
Patrick McHardy9872bec2007-12-05 01:28:50 -08001056 switch (cmd->command) {
Florian Westphal0360ae42012-11-23 06:22:21 +00001057 case NFQNL_CFG_CMD_PF_BIND: return 0;
1058 case NFQNL_CFG_CMD_PF_UNBIND: return 0;
Patrick McHardy9872bec2007-12-05 01:28:50 -08001059 }
Patrick McHardya3c8e7f2007-12-05 01:28:30 -08001060 }
1061
Patrick McHardy9872bec2007-12-05 01:28:50 -08001062 rcu_read_lock();
Gao fenge8179612013-03-24 23:50:47 +00001063 queue = instance_lookup(q, queue_num);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001064 if (queue && queue->peer_portid != NETLINK_CB(skb).portid) {
Patrick McHardy9872bec2007-12-05 01:28:50 -08001065 ret = -EPERM;
1066 goto err_out_unlock;
1067 }
Patrick McHardya3c8e7f2007-12-05 01:28:30 -08001068
Patrick McHardy9872bec2007-12-05 01:28:50 -08001069 if (cmd != NULL) {
Harald Welte7af4cc32005-08-09 19:44:15 -07001070 switch (cmd->command) {
1071 case NFQNL_CFG_CMD_BIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -08001072 if (queue) {
1073 ret = -EBUSY;
1074 goto err_out_unlock;
1075 }
Gao fenge8179612013-03-24 23:50:47 +00001076 queue = instance_create(q, queue_num,
1077 NETLINK_CB(skb).portid);
Patrick McHardybaab2ce2007-12-17 22:41:21 -08001078 if (IS_ERR(queue)) {
1079 ret = PTR_ERR(queue);
Patrick McHardy9872bec2007-12-05 01:28:50 -08001080 goto err_out_unlock;
1081 }
Harald Welte7af4cc32005-08-09 19:44:15 -07001082 break;
1083 case NFQNL_CFG_CMD_UNBIND:
Patrick McHardy9872bec2007-12-05 01:28:50 -08001084 if (!queue) {
1085 ret = -ENODEV;
1086 goto err_out_unlock;
1087 }
Gao fenge8179612013-03-24 23:50:47 +00001088 instance_destroy(q, queue);
Harald Welte7af4cc32005-08-09 19:44:15 -07001089 break;
1090 case NFQNL_CFG_CMD_PF_BIND:
Harald Welte7af4cc32005-08-09 19:44:15 -07001091 case NFQNL_CFG_CMD_PF_UNBIND:
Harald Welte7af4cc32005-08-09 19:44:15 -07001092 break;
1093 default:
Patrick McHardycd21f0a2007-12-17 22:40:19 -08001094 ret = -ENOTSUPP;
Harald Welte838ab632005-08-09 19:50:45 -07001095 break;
Harald Welte7af4cc32005-08-09 19:44:15 -07001096 }
Harald Welte7af4cc32005-08-09 19:44:15 -07001097 }
1098
Patrick McHardydf6fb862007-09-28 14:37:03 -07001099 if (nfqa[NFQA_CFG_PARAMS]) {
Harald Welte7af4cc32005-08-09 19:44:15 -07001100 struct nfqnl_msg_config_params *params;
Harald Welte7af4cc32005-08-09 19:44:15 -07001101
Patrick McHardy406dbfc2006-03-12 20:32:47 -08001102 if (!queue) {
Patrick McHardya3c8e7f2007-12-05 01:28:30 -08001103 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -08001104 goto err_out_unlock;
Patrick McHardy406dbfc2006-03-12 20:32:47 -08001105 }
Patrick McHardydf6fb862007-09-28 14:37:03 -07001106 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
Harald Welte7af4cc32005-08-09 19:44:15 -07001107 nfqnl_set_mode(queue, params->copy_mode,
1108 ntohl(params->copy_range));
1109 }
1110
Patrick McHardydf6fb862007-09-28 14:37:03 -07001111 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
Eric Leblond829e17a2006-11-29 02:35:33 +01001112 __be32 *queue_maxlen;
Patrick McHardya3c8e7f2007-12-05 01:28:30 -08001113
1114 if (!queue) {
1115 ret = -ENODEV;
Patrick McHardy9872bec2007-12-05 01:28:50 -08001116 goto err_out_unlock;
Patrick McHardya3c8e7f2007-12-05 01:28:30 -08001117 }
Patrick McHardydf6fb862007-09-28 14:37:03 -07001118 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
Eric Leblond829e17a2006-11-29 02:35:33 +01001119 spin_lock_bh(&queue->lock);
1120 queue->queue_maxlen = ntohl(*queue_maxlen);
1121 spin_unlock_bh(&queue->lock);
1122 }
1123
Krishna Kumarfdb694a2012-05-24 03:56:44 +00001124 if (nfqa[NFQA_CFG_FLAGS]) {
1125 __u32 flags, mask;
1126
1127 if (!queue) {
1128 ret = -ENODEV;
1129 goto err_out_unlock;
1130 }
1131
1132 if (!nfqa[NFQA_CFG_MASK]) {
1133 /* A mask is needed to specify which flags are being
1134 * changed.
1135 */
1136 ret = -EINVAL;
1137 goto err_out_unlock;
1138 }
1139
1140 flags = ntohl(nla_get_be32(nfqa[NFQA_CFG_FLAGS]));
1141 mask = ntohl(nla_get_be32(nfqa[NFQA_CFG_MASK]));
1142
Krishna Kumar46ba5a22012-06-27 00:59:56 +00001143 if (flags >= NFQA_CFG_F_MAX) {
1144 ret = -EOPNOTSUPP;
1145 goto err_out_unlock;
1146 }
1147
Krishna Kumarfdb694a2012-05-24 03:56:44 +00001148 spin_lock_bh(&queue->lock);
1149 queue->flags &= ~mask;
1150 queue->flags |= flags & mask;
1151 spin_unlock_bh(&queue->lock);
1152 }
1153
Patrick McHardy9872bec2007-12-05 01:28:50 -08001154err_out_unlock:
1155 rcu_read_unlock();
Harald Welte838ab632005-08-09 19:50:45 -07001156 return ret;
Harald Welte7af4cc32005-08-09 19:44:15 -07001157}
1158
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -07001159static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
Eric Dumazet84a797d2011-07-18 16:08:27 +02001160 [NFQNL_MSG_PACKET] = { .call_rcu = nfqnl_recv_unsupp,
Harald Welte37d2e7a2005-11-14 15:24:59 -08001161 .attr_count = NFQA_MAX, },
Eric Dumazet84a797d2011-07-18 16:08:27 +02001162 [NFQNL_MSG_VERDICT] = { .call_rcu = nfqnl_recv_verdict,
Patrick McHardy5bf75852007-09-28 14:39:26 -07001163 .attr_count = NFQA_MAX,
1164 .policy = nfqa_verdict_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -07001165 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
Patrick McHardy5bf75852007-09-28 14:39:26 -07001166 .attr_count = NFQA_CFG_MAX,
1167 .policy = nfqa_cfg_policy },
Florian Westphal97d32cf2011-07-19 11:46:33 +02001168 [NFQNL_MSG_VERDICT_BATCH]={ .call_rcu = nfqnl_recv_verdict_batch,
1169 .attr_count = NFQA_MAX,
1170 .policy = nfqa_verdict_batch_policy },
Harald Welte7af4cc32005-08-09 19:44:15 -07001171};
1172
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -07001173static const struct nfnetlink_subsystem nfqnl_subsys = {
Harald Welte7af4cc32005-08-09 19:44:15 -07001174 .name = "nf_queue",
1175 .subsys_id = NFNL_SUBSYS_QUEUE,
1176 .cb_count = NFQNL_MSG_MAX,
Harald Welte7af4cc32005-08-09 19:44:15 -07001177 .cb = nfqnl_cb,
1178};
1179
Harald Welte838ab632005-08-09 19:50:45 -07001180#ifdef CONFIG_PROC_FS
1181struct iter_state {
Gao fenge8179612013-03-24 23:50:47 +00001182 struct seq_net_private p;
Harald Welte838ab632005-08-09 19:50:45 -07001183 unsigned int bucket;
1184};
1185
1186static struct hlist_node *get_first(struct seq_file *seq)
1187{
1188 struct iter_state *st = seq->private;
Gao fenge8179612013-03-24 23:50:47 +00001189 struct net *net;
1190 struct nfnl_queue_net *q;
Harald Welte838ab632005-08-09 19:50:45 -07001191
1192 if (!st)
1193 return NULL;
1194
Gao fenge8179612013-03-24 23:50:47 +00001195 net = seq_file_net(seq);
1196 q = nfnl_queue_pernet(net);
Harald Welte838ab632005-08-09 19:50:45 -07001197 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
Gao fenge8179612013-03-24 23:50:47 +00001198 if (!hlist_empty(&q->instance_table[st->bucket]))
1199 return q->instance_table[st->bucket].first;
Harald Welte838ab632005-08-09 19:50:45 -07001200 }
1201 return NULL;
1202}
1203
1204static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
1205{
1206 struct iter_state *st = seq->private;
Gao fenge8179612013-03-24 23:50:47 +00001207 struct net *net = seq_file_net(seq);
Harald Welte838ab632005-08-09 19:50:45 -07001208
1209 h = h->next;
1210 while (!h) {
Gao fenge8179612013-03-24 23:50:47 +00001211 struct nfnl_queue_net *q;
1212
Harald Welte838ab632005-08-09 19:50:45 -07001213 if (++st->bucket >= INSTANCE_BUCKETS)
1214 return NULL;
1215
Gao fenge8179612013-03-24 23:50:47 +00001216 q = nfnl_queue_pernet(net);
1217 h = q->instance_table[st->bucket].first;
Harald Welte838ab632005-08-09 19:50:45 -07001218 }
1219 return h;
1220}
1221
1222static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
1223{
1224 struct hlist_node *head;
1225 head = get_first(seq);
1226
1227 if (head)
1228 while (pos && (head = get_next(seq, head)))
1229 pos--;
1230 return pos ? NULL : head;
1231}
1232
Gao fenge8179612013-03-24 23:50:47 +00001233static void *seq_start(struct seq_file *s, loff_t *pos)
1234 __acquires(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
Harald Welte838ab632005-08-09 19:50:45 -07001235{
Gao fenge8179612013-03-24 23:50:47 +00001236 spin_lock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
1237 return get_idx(s, *pos);
Harald Welte838ab632005-08-09 19:50:45 -07001238}
1239
1240static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1241{
1242 (*pos)++;
1243 return get_next(s, v);
1244}
1245
1246static void seq_stop(struct seq_file *s, void *v)
Gao fenge8179612013-03-24 23:50:47 +00001247 __releases(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
Harald Welte838ab632005-08-09 19:50:45 -07001248{
Gao fenge8179612013-03-24 23:50:47 +00001249 spin_unlock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
Harald Welte838ab632005-08-09 19:50:45 -07001250}
1251
1252static int seq_show(struct seq_file *s, void *v)
1253{
1254 const struct nfqnl_instance *inst = v;
1255
1256 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
1257 inst->queue_num,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001258 inst->peer_portid, inst->queue_total,
Harald Welte838ab632005-08-09 19:50:45 -07001259 inst->copy_mode, inst->copy_range,
1260 inst->queue_dropped, inst->queue_user_dropped,
Eric Dumazet58637022011-07-19 11:44:17 +02001261 inst->id_sequence, 1);
Harald Welte838ab632005-08-09 19:50:45 -07001262}
1263
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001264static const struct seq_operations nfqnl_seq_ops = {
Harald Welte838ab632005-08-09 19:50:45 -07001265 .start = seq_start,
1266 .next = seq_next,
1267 .stop = seq_stop,
1268 .show = seq_show,
1269};
1270
1271static int nfqnl_open(struct inode *inode, struct file *file)
1272{
Gao fenge8179612013-03-24 23:50:47 +00001273 return seq_open_net(inode, file, &nfqnl_seq_ops,
Pavel Emelyanove2da5912007-10-10 02:29:58 -07001274 sizeof(struct iter_state));
Harald Welte838ab632005-08-09 19:50:45 -07001275}
1276
Arjan van de Venda7071d2007-02-12 00:55:36 -08001277static const struct file_operations nfqnl_file_ops = {
Harald Welte838ab632005-08-09 19:50:45 -07001278 .owner = THIS_MODULE,
1279 .open = nfqnl_open,
1280 .read = seq_read,
1281 .llseek = seq_lseek,
Gao fenge8179612013-03-24 23:50:47 +00001282 .release = seq_release_net,
Harald Welte838ab632005-08-09 19:50:45 -07001283};
1284
1285#endif /* PROC_FS */
1286
Gao fenge8179612013-03-24 23:50:47 +00001287static int __net_init nfnl_queue_net_init(struct net *net)
Harald Welte7af4cc32005-08-09 19:44:15 -07001288{
Gao fenge8179612013-03-24 23:50:47 +00001289 unsigned int i;
1290 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001291
Harald Welte838ab632005-08-09 19:50:45 -07001292 for (i = 0; i < INSTANCE_BUCKETS; i++)
Gao fenge8179612013-03-24 23:50:47 +00001293 INIT_HLIST_HEAD(&q->instance_table[i]);
1294
1295 spin_lock_init(&q->instances_lock);
1296
1297#ifdef CONFIG_PROC_FS
1298 if (!proc_create("nfnetlink_queue", 0440,
1299 net->nf.proc_netfilter, &nfqnl_file_ops))
1300 return -ENOMEM;
1301#endif
1302 return 0;
1303}
1304
1305static void __net_exit nfnl_queue_net_exit(struct net *net)
1306{
Pablo Neira Ayusoe778f562013-04-30 08:01:18 +00001307#ifdef CONFIG_PROC_FS
Gao fenge8179612013-03-24 23:50:47 +00001308 remove_proc_entry("nfnetlink_queue", net->nf.proc_netfilter);
Pablo Neira Ayusoe778f562013-04-30 08:01:18 +00001309#endif
Gao fenge8179612013-03-24 23:50:47 +00001310}
1311
1312static struct pernet_operations nfnl_queue_net_ops = {
1313 .init = nfnl_queue_net_init,
1314 .exit = nfnl_queue_net_exit,
1315 .id = &nfnl_queue_net_id,
1316 .size = sizeof(struct nfnl_queue_net),
1317};
1318
1319static int __init nfnetlink_queue_init(void)
1320{
1321 int status = -ENOMEM;
Harald Welte838ab632005-08-09 19:50:45 -07001322
Harald Welte7af4cc32005-08-09 19:44:15 -07001323 netlink_register_notifier(&nfqnl_rtnl_notifier);
1324 status = nfnetlink_subsys_register(&nfqnl_subsys);
1325 if (status < 0) {
Gao fenge8179612013-03-24 23:50:47 +00001326 pr_err("nf_queue: failed to create netlink socket\n");
Harald Welte7af4cc32005-08-09 19:44:15 -07001327 goto cleanup_netlink_notifier;
1328 }
1329
Gao fenge8179612013-03-24 23:50:47 +00001330 status = register_pernet_subsys(&nfnl_queue_net_ops);
1331 if (status < 0) {
1332 pr_err("nf_queue: failed to register pernet ops\n");
Harald Welte838ab632005-08-09 19:50:45 -07001333 goto cleanup_subsys;
Gao fenge8179612013-03-24 23:50:47 +00001334 }
Harald Welte7af4cc32005-08-09 19:44:15 -07001335 register_netdevice_notifier(&nfqnl_dev_notifier);
Florian Westphal0360ae42012-11-23 06:22:21 +00001336 nf_register_queue_handler(&nfqh);
Harald Welte7af4cc32005-08-09 19:44:15 -07001337 return status;
1338
Harald Welte838ab632005-08-09 19:50:45 -07001339cleanup_subsys:
Harald Welte7af4cc32005-08-09 19:44:15 -07001340 nfnetlink_subsys_unregister(&nfqnl_subsys);
Harald Welte7af4cc32005-08-09 19:44:15 -07001341cleanup_netlink_notifier:
1342 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1343 return status;
1344}
1345
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001346static void __exit nfnetlink_queue_fini(void)
Harald Welte7af4cc32005-08-09 19:44:15 -07001347{
Florian Westphal0360ae42012-11-23 06:22:21 +00001348 nf_unregister_queue_handler();
Patrick McHardy32292a72006-04-06 14:11:30 -07001349 unregister_netdevice_notifier(&nfqnl_dev_notifier);
Gao fenge8179612013-03-24 23:50:47 +00001350 unregister_pernet_subsys(&nfnl_queue_net_ops);
Patrick McHardy32292a72006-04-06 14:11:30 -07001351 nfnetlink_subsys_unregister(&nfqnl_subsys);
1352 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
Jesper Dangaard Brouer67137f32009-06-08 03:11:33 +00001353
1354 rcu_barrier(); /* Wait for completion of call_rcu()'s */
Harald Welte7af4cc32005-08-09 19:44:15 -07001355}
1356
1357MODULE_DESCRIPTION("netfilter packet queue handler");
1358MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1359MODULE_LICENSE("GPL");
1360MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1361
Andrew Morton65b4b4e2006-03-28 16:37:06 -08001362module_init(nfnetlink_queue_init);
1363module_exit(nfnetlink_queue_fini);