netfilter: rename netlink related "pid" variables to "portid"
[deliverable/linux.git] / net / netfilter / nfnetlink_queue_core.c
CommitLineData
7af4cc3f
HW
1/*
2 * This is a module which is used for queueing packets and communicating with
67137f3c 3 * userspace via nfnetlink.
7af4cc3f
HW
4 *
5 * (C) 2005 by Harald Welte <laforge@netfilter.org>
4ad9d4fa 6 * (C) 2007 by Patrick McHardy <kaber@trash.net>
7af4cc3f
HW
7 *
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>
5a0e3ad6 21#include <linux/slab.h>
7af4cc3f
HW
22#include <linux/notifier.h>
23#include <linux/netdevice.h>
24#include <linux/netfilter.h>
838ab636 25#include <linux/proc_fs.h>
7af4cc3f
HW
26#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>
c01cd429 32#include <net/netfilter/nf_queue.h>
e8179610 33#include <net/netns/generic.h>
7c622345 34#include <net/netfilter/nfnetlink_queue.h>
7af4cc3f 35
60063497 36#include <linux/atomic.h>
7af4cc3f 37
fbcd923c
HW
38#ifdef CONFIG_BRIDGE_NETFILTER
39#include "../bridge/br_private.h"
40#endif
41
7af4cc3f
HW
42#define NFQNL_QMAX_DEFAULT 1024
43
7af4cc3f
HW
44struct nfqnl_instance {
45 struct hlist_node hlist; /* global list of queues */
9872bec7 46 struct rcu_head rcu;
7af4cc3f 47
15e47304 48 int peer_portid;
7af4cc3f
HW
49 unsigned int queue_maxlen;
50 unsigned int copy_range;
7af4cc3f
HW
51 unsigned int queue_dropped;
52 unsigned int queue_user_dropped;
53
7af4cc3f
HW
54
55 u_int16_t queue_num; /* number of this queue */
56 u_int8_t copy_mode;
fdb694a0 57 u_int32_t flags; /* Set using NFQA_CFG_FLAGS */
c463ac97
ED
58/*
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;
5863702a 64 unsigned int id_sequence; /* 'sequence' of pkt ids */
7af4cc3f
HW
65 struct list_head queue_list; /* packets in queue */
66};
67
02f014d8 68typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
7af4cc3f 69
e8179610 70static int nfnl_queue_net_id __read_mostly;
7af4cc3f 71
7af4cc3f 72#define INSTANCE_BUCKETS 16
e8179610
G
73struct 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}
7af4cc3f
HW
82
83static inline u_int8_t instance_hashfn(u_int16_t queue_num)
84{
1cdb0905 85 return ((queue_num >> 8) ^ queue_num) % INSTANCE_BUCKETS;
7af4cc3f
HW
86}
87
88static struct nfqnl_instance *
e8179610 89instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num)
7af4cc3f
HW
90{
91 struct hlist_head *head;
7af4cc3f
HW
92 struct nfqnl_instance *inst;
93
e8179610 94 head = &q->instance_table[instance_hashfn(queue_num)];
b67bfe0d 95 hlist_for_each_entry_rcu(inst, head, hlist) {
7af4cc3f
HW
96 if (inst->queue_num == queue_num)
97 return inst;
98 }
99 return NULL;
100}
101
7af4cc3f 102static struct nfqnl_instance *
e8179610
G
103instance_create(struct nfnl_queue_net *q, u_int16_t queue_num,
104 int portid)
7af4cc3f 105{
baab2ce7 106 struct nfqnl_instance *inst;
9872bec7 107 unsigned int h;
baab2ce7 108 int err;
7af4cc3f 109
e8179610
G
110 spin_lock(&q->instances_lock);
111 if (instance_lookup(q, queue_num)) {
baab2ce7 112 err = -EEXIST;
7af4cc3f 113 goto out_unlock;
baab2ce7 114 }
7af4cc3f 115
10dfdc69 116 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
baab2ce7
PM
117 if (!inst) {
118 err = -ENOMEM;
7af4cc3f 119 goto out_unlock;
baab2ce7 120 }
7af4cc3f 121
7af4cc3f 122 inst->queue_num = queue_num;
15e47304 123 inst->peer_portid = portid;
7af4cc3f 124 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
bae99f7a 125 inst->copy_range = 0xffff;
7af4cc3f 126 inst->copy_mode = NFQNL_COPY_NONE;
181a46a5 127 spin_lock_init(&inst->lock);
7af4cc3f
HW
128 INIT_LIST_HEAD(&inst->queue_list);
129
baab2ce7
PM
130 if (!try_module_get(THIS_MODULE)) {
131 err = -EAGAIN;
7af4cc3f 132 goto out_free;
baab2ce7 133 }
7af4cc3f 134
9872bec7 135 h = instance_hashfn(queue_num);
e8179610 136 hlist_add_head_rcu(&inst->hlist, &q->instance_table[h]);
7af4cc3f 137
e8179610 138 spin_unlock(&q->instances_lock);
7af4cc3f 139
7af4cc3f
HW
140 return inst;
141
142out_free:
143 kfree(inst);
144out_unlock:
e8179610 145 spin_unlock(&q->instances_lock);
baab2ce7 146 return ERR_PTR(err);
7af4cc3f
HW
147}
148
b43d8d85
PM
149static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
150 unsigned long data);
7af4cc3f
HW
151
152static void
9872bec7 153instance_destroy_rcu(struct rcu_head *head)
7af4cc3f 154{
9872bec7
PM
155 struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
156 rcu);
7af4cc3f 157
b43d8d85 158 nfqnl_flush(inst, NULL, 0);
9872bec7 159 kfree(inst);
7af4cc3f
HW
160 module_put(THIS_MODULE);
161}
162
9872bec7 163static void
7af4cc3f
HW
164__instance_destroy(struct nfqnl_instance *inst)
165{
9872bec7
PM
166 hlist_del_rcu(&inst->hlist);
167 call_rcu(&inst->rcu, instance_destroy_rcu);
7af4cc3f
HW
168}
169
9872bec7 170static void
e8179610 171instance_destroy(struct nfnl_queue_net *q, struct nfqnl_instance *inst)
7af4cc3f 172{
e8179610 173 spin_lock(&q->instances_lock);
9872bec7 174 __instance_destroy(inst);
e8179610 175 spin_unlock(&q->instances_lock);
7af4cc3f
HW
176}
177
7af4cc3f 178static inline void
02f014d8 179__enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
7af4cc3f 180{
0ac41e81 181 list_add_tail(&entry->list, &queue->queue_list);
7af4cc3f
HW
182 queue->queue_total++;
183}
184
97d32cf9
FW
185static 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
02f014d8 192static struct nf_queue_entry *
b43d8d85 193find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
7af4cc3f 194{
02f014d8 195 struct nf_queue_entry *entry = NULL, *i;
601e68e1 196
7af4cc3f 197 spin_lock_bh(&queue->lock);
b43d8d85
PM
198
199 list_for_each_entry(i, &queue->queue_list, list) {
200 if (i->id == id) {
201 entry = i;
202 break;
203 }
204 }
205
97d32cf9
FW
206 if (entry)
207 __dequeue_entry(queue, entry);
b43d8d85 208
7af4cc3f
HW
209 spin_unlock_bh(&queue->lock);
210
211 return entry;
212}
213
214static void
b43d8d85 215nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
7af4cc3f 216{
02f014d8 217 struct nf_queue_entry *entry, *next;
b43d8d85 218
7af4cc3f 219 spin_lock_bh(&queue->lock);
b43d8d85
PM
220 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--;
4b3d15ef 224 nf_reinject(entry, NF_DROP);
b43d8d85
PM
225 }
226 }
7af4cc3f
HW
227 spin_unlock_bh(&queue->lock);
228}
229
ae08ce00
ED
230static 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
7af4cc3f
HW
275static struct sk_buff *
276nfqnl_build_packet_message(struct nfqnl_instance *queue,
5863702a
ED
277 struct nf_queue_entry *entry,
278 __be32 **packet_id_ptr)
7af4cc3f 279{
7af4cc3f 280 size_t size;
6ee584be 281 size_t data_len = 0, cap_len = 0;
ae08ce00 282 int hlen = 0;
7af4cc3f 283 struct sk_buff *skb;
5863702a
ED
284 struct nlattr *nla;
285 struct nfqnl_msg_packet_hdr *pmsg;
7af4cc3f
HW
286 struct nlmsghdr *nlh;
287 struct nfgenmsg *nfmsg;
3e4ead4f
JJ
288 struct sk_buff *entskb = entry->skb;
289 struct net_device *indev;
290 struct net_device *outdev;
9cb01766
PNA
291 struct nf_conn *ct = NULL;
292 enum ip_conntrack_info uninitialized_var(ctinfo);
7af4cc3f 293
573ce260 294 size = nlmsg_total_size(sizeof(struct nfgenmsg))
df6fb868
PM
295 + 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 */
fbcd923c 298#ifdef CONFIG_BRIDGE_NETFILTER
df6fb868
PM
299 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
300 + nla_total_size(sizeof(u_int32_t)) /* ifindex */
fbcd923c 301#endif
df6fb868
PM
302 + nla_total_size(sizeof(u_int32_t)) /* mark */
303 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
ae08ce00
ED
304 + 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));
7af4cc3f 308
02f014d8 309 outdev = entry->outdev;
3e4ead4f 310
c463ac97 311 switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) {
7af4cc3f
HW
312 case NFQNL_COPY_META:
313 case NFQNL_COPY_NONE:
7af4cc3f 314 break;
601e68e1 315
7af4cc3f 316 case NFQNL_COPY_PACKET:
e9f13cab 317 if (entskb->ip_summed == CHECKSUM_PARTIAL &&
c463ac97 318 skb_checksum_help(entskb))
e7dfb09a 319 return NULL;
c463ac97
ED
320
321 data_len = ACCESS_ONCE(queue->copy_range);
322 if (data_len == 0 || data_len > entskb->len)
3e4ead4f 323 data_len = entskb->len;
601e68e1 324
ae08ce00
ED
325
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;
6ee584be 335 cap_len = entskb->len;
7af4cc3f 336 break;
7af4cc3f
HW
337 }
338
7c622345
PNA
339 if (queue->flags & NFQA_CFG_F_CONNTRACK)
340 ct = nfqnl_ct_get(entskb, &size, &ctinfo);
7af4cc3f
HW
341
342 skb = alloc_skb(size, GFP_ATOMIC);
343 if (!skb)
3da07c0c 344 return NULL;
601e68e1 345
3da07c0c 346 nlh = nlmsg_put(skb, 0, 0,
7af4cc3f 347 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
3da07c0c
DM
348 sizeof(struct nfgenmsg), 0);
349 if (!nlh) {
350 kfree_skb(skb);
351 return NULL;
352 }
353 nfmsg = nlmsg_data(nlh);
02f014d8 354 nfmsg->nfgen_family = entry->pf;
7af4cc3f
HW
355 nfmsg->version = NFNETLINK_V0;
356 nfmsg->res_id = htons(queue->queue_num);
357
5863702a
ED
358 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;
7af4cc3f 363
02f014d8 364 indev = entry->indev;
3e4ead4f 365 if (indev) {
fbcd923c 366#ifndef CONFIG_BRIDGE_NETFILTER
a447189e
DM
367 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex)))
368 goto nla_put_failure;
fbcd923c 369#else
02f014d8 370 if (entry->pf == PF_BRIDGE) {
fbcd923c 371 /* Case 1: indev is physical input device, we need to
601e68e1 372 * look for bridge group (when called from
fbcd923c 373 * netfilter_bridge) */
a447189e
DM
374 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
375 htonl(indev->ifindex)) ||
fbcd923c 376 /* this is the bridge group "brX" */
f350a0a8 377 /* rcu_read_lock()ed by __nf_queue */
a447189e
DM
378 nla_put_be32(skb, NFQA_IFINDEX_INDEV,
379 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
380 goto nla_put_failure;
fbcd923c
HW
381 } else {
382 /* Case 2: indev is bridge group, we need to look for
383 * physical device (when called from ipv4) */
a447189e
DM
384 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;
fbcd923c
HW
391 }
392#endif
7af4cc3f
HW
393 }
394
3e4ead4f 395 if (outdev) {
fbcd923c 396#ifndef CONFIG_BRIDGE_NETFILTER
a447189e
DM
397 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex)))
398 goto nla_put_failure;
fbcd923c 399#else
02f014d8 400 if (entry->pf == PF_BRIDGE) {
fbcd923c 401 /* Case 1: outdev is physical output device, we need to
601e68e1 402 * look for bridge group (when called from
fbcd923c 403 * netfilter_bridge) */
a447189e
DM
404 if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
405 htonl(outdev->ifindex)) ||
fbcd923c 406 /* this is the bridge group "brX" */
f350a0a8 407 /* rcu_read_lock()ed by __nf_queue */
a447189e
DM
408 nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
409 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
410 goto nla_put_failure;
fbcd923c
HW
411 } else {
412 /* Case 2: outdev is bridge group, we need to look for
413 * physical output device (when called from ipv4) */
a447189e
DM
414 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;
fbcd923c
HW
421 }
422#endif
7af4cc3f
HW
423 }
424
a447189e
DM
425 if (entskb->mark &&
426 nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark)))
427 goto nla_put_failure;
7af4cc3f 428
2c38de4c
NC
429 if (indev && entskb->dev &&
430 entskb->mac_header != entskb->network_header) {
7af4cc3f 431 struct nfqnl_msg_packet_hw phw;
b95cce35
SH
432 int len = dev_parse_header(entskb, phw.hw_addr);
433 if (len) {
434 phw.hw_addrlen = htons(len);
a447189e
DM
435 if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw))
436 goto nla_put_failure;
b95cce35 437 }
7af4cc3f
HW
438 }
439
b7aa0bf7 440 if (entskb->tstamp.tv64) {
7af4cc3f 441 struct nfqnl_msg_packet_timestamp ts;
b7aa0bf7
ED
442 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);
7af4cc3f 445
a447189e
DM
446 if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts))
447 goto nla_put_failure;
7af4cc3f
HW
448 }
449
ae08ce00
ED
450 if (ct && nfqnl_ct_put(skb, ct, ctinfo) < 0)
451 goto nla_put_failure;
452
453 if (cap_len > 0 && nla_put_be32(skb, NFQA_CAP_LEN, htonl(cap_len)))
454 goto nla_put_failure;
455
7af4cc3f 456 if (data_len) {
df6fb868 457 struct nlattr *nla;
7af4cc3f 458
ae08ce00
ED
459 if (skb_tailroom(skb) < sizeof(*nla) + hlen)
460 goto nla_put_failure;
7af4cc3f 461
ae08ce00 462 nla = (struct nlattr *)skb_put(skb, sizeof(*nla));
df6fb868 463 nla->nla_type = NFQA_PAYLOAD;
ae08ce00 464 nla->nla_len = nla_attr_size(data_len);
7af4cc3f 465
ae08ce00 466 nfqnl_zcopy(skb, entskb, data_len, hlen);
7af4cc3f 467 }
601e68e1 468
ae08ce00 469 nlh->nlmsg_len = skb->len;
7af4cc3f
HW
470 return skb;
471
df6fb868 472nla_put_failure:
a6729955 473 kfree_skb(skb);
e87cc472 474 net_err_ratelimited("nf_queue: error creating packet message\n");
7af4cc3f
HW
475 return NULL;
476}
477
478static int
02f014d8 479nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
7af4cc3f 480{
7af4cc3f
HW
481 struct sk_buff *nskb;
482 struct nfqnl_instance *queue;
f1585086 483 int err = -ENOBUFS;
5863702a 484 __be32 *packet_id_ptr;
fdb694a0 485 int failopen = 0;
e8179610
G
486 struct net *net = dev_net(entry->indev ?
487 entry->indev : entry->outdev);
488 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
7af4cc3f 489
9872bec7 490 /* rcu_read_lock()ed by nf_hook_slow() */
e8179610 491 queue = instance_lookup(q, queuenum);
f1585086
FW
492 if (!queue) {
493 err = -ESRCH;
0ef0f465 494 goto err_out;
f1585086 495 }
7af4cc3f 496
f1585086
FW
497 if (queue->copy_mode == NFQNL_COPY_NONE) {
498 err = -EINVAL;
0ef0f465 499 goto err_out;
f1585086 500 }
7af4cc3f 501
5863702a 502 nskb = nfqnl_build_packet_message(queue, entry, &packet_id_ptr);
f1585086
FW
503 if (nskb == NULL) {
504 err = -ENOMEM;
0ef0f465 505 goto err_out;
f1585086 506 }
7af4cc3f 507 spin_lock_bh(&queue->lock);
601e68e1 508
15e47304 509 if (!queue->peer_portid) {
f1585086 510 err = -EINVAL;
601e68e1 511 goto err_out_free_nskb;
f1585086 512 }
7af4cc3f 513 if (queue->queue_total >= queue->queue_maxlen) {
fdb694a0
KK
514 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 }
7af4cc3f
HW
522 goto err_out_free_nskb;
523 }
5863702a
ED
524 entry->id = ++queue->id_sequence;
525 *packet_id_ptr = htonl(entry->id);
7af4cc3f
HW
526
527 /* nfnetlink_unicast will either free the nskb or add it to a socket */
e8179610 528 err = nfnetlink_unicast(nskb, net, queue->peer_portid, MSG_DONTWAIT);
0ef0f465 529 if (err < 0) {
601e68e1 530 queue->queue_user_dropped++;
7af4cc3f
HW
531 goto err_out_unlock;
532 }
533
534 __enqueue_entry(queue, entry);
535
536 spin_unlock_bh(&queue->lock);
0ef0f465 537 return 0;
7af4cc3f
HW
538
539err_out_free_nskb:
601e68e1 540 kfree_skb(nskb);
7af4cc3f
HW
541err_out_unlock:
542 spin_unlock_bh(&queue->lock);
fdb694a0
KK
543 if (failopen)
544 nf_reinject(entry, NF_ACCEPT);
0ef0f465 545err_out:
f1585086 546 return err;
7af4cc3f
HW
547}
548
549static int
8c88f87c 550nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e, int diff)
7af4cc3f 551{
e2b58a67 552 struct sk_buff *nskb;
7af4cc3f 553
d8a585d7
PM
554 if (diff < 0) {
555 if (pskb_trim(e->skb, data_len))
556 return -ENOMEM;
557 } else if (diff > 0) {
7af4cc3f
HW
558 if (data_len > 0xFFFF)
559 return -EINVAL;
560 if (diff > skb_tailroom(e->skb)) {
9a732ed6
AE
561 nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
562 diff, GFP_ATOMIC);
e2b58a67 563 if (!nskb) {
1158ba27 564 printk(KERN_WARNING "nf_queue: OOM "
7af4cc3f 565 "in mangle, dropping packet\n");
e2b58a67 566 return -ENOMEM;
7af4cc3f 567 }
e2b58a67
PM
568 kfree_skb(e->skb);
569 e->skb = nskb;
7af4cc3f
HW
570 }
571 skb_put(e->skb, diff);
572 }
37d41879 573 if (!skb_make_writable(e->skb, data_len))
7af4cc3f 574 return -ENOMEM;
27d7ff46 575 skb_copy_to_linear_data(e->skb, data, data_len);
e7dfb09a 576 e->skb->ip_summed = CHECKSUM_NONE;
7af4cc3f
HW
577 return 0;
578}
579
7af4cc3f
HW
580static int
581nfqnl_set_mode(struct nfqnl_instance *queue,
582 unsigned char mode, unsigned int range)
583{
c5de0dfd 584 int status = 0;
7af4cc3f
HW
585
586 spin_lock_bh(&queue->lock);
c5de0dfd
PM
587 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;
ba8d3b0b
PNA
596 /* 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;
c5de0dfd
PM
603 else
604 queue->copy_range = range;
605 break;
606
607 default:
608 status = -EINVAL;
609
610 }
7af4cc3f
HW
611 spin_unlock_bh(&queue->lock);
612
613 return status;
614}
615
616static int
02f014d8 617dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
7af4cc3f 618{
02f014d8
PM
619 if (entry->indev)
620 if (entry->indev->ifindex == ifindex)
7af4cc3f 621 return 1;
02f014d8
PM
622 if (entry->outdev)
623 if (entry->outdev->ifindex == ifindex)
7af4cc3f 624 return 1;
ef47c6a7
PM
625#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
7af4cc3f
HW
635 return 0;
636}
637
638/* drop all packets with either indev or outdev == ifindex from all queue
639 * instances */
640static void
e8179610 641nfqnl_dev_drop(struct net *net, int ifindex)
7af4cc3f
HW
642{
643 int i;
e8179610 644 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
601e68e1 645
9872bec7 646 rcu_read_lock();
7af4cc3f 647
9872bec7 648 for (i = 0; i < INSTANCE_BUCKETS; i++) {
7af4cc3f 649 struct nfqnl_instance *inst;
e8179610 650 struct hlist_head *head = &q->instance_table[i];
7af4cc3f 651
b67bfe0d 652 hlist_for_each_entry_rcu(inst, head, hlist)
b43d8d85 653 nfqnl_flush(inst, dev_cmp, ifindex);
7af4cc3f
HW
654 }
655
9872bec7 656 rcu_read_unlock();
7af4cc3f
HW
657}
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)
e8179610 669 nfqnl_dev_drop(dev_net(dev), dev->ifindex);
7af4cc3f
HW
670 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;
e8179610 682 struct nfnl_queue_net *q = nfnl_queue_pernet(n->net);
7af4cc3f 683
dee5817e 684 if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
7af4cc3f
HW
685 int i;
686
15e47304 687 /* destroy all instances for this portid */
e8179610 688 spin_lock(&q->instances_lock);
9872bec7 689 for (i = 0; i < INSTANCE_BUCKETS; i++) {
b67bfe0d 690 struct hlist_node *t2;
7af4cc3f 691 struct nfqnl_instance *inst;
e8179610 692 struct hlist_head *head = &q->instance_table[i];
7af4cc3f 693
b67bfe0d 694 hlist_for_each_entry_safe(inst, t2, head, hlist) {
e8179610 695 if (n->portid == inst->peer_portid)
7af4cc3f
HW
696 __instance_destroy(inst);
697 }
698 }
e8179610 699 spin_unlock(&q->instances_lock);
7af4cc3f
HW
700 }
701 return NOTIFY_DONE;
702}
703
704static struct notifier_block nfqnl_rtnl_notifier = {
705 .notifier_call = nfqnl_rcv_nl_event,
706};
707
5bf75853
PM
708static 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 },
9cb01766 712 [NFQA_CT] = { .type = NLA_UNSPEC },
838ab636
HW
713};
714
97d32cf9
FW
715static 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
e8179610
G
720static struct nfqnl_instance *
721verdict_instance_lookup(struct nfnl_queue_net *q, u16 queue_num, int nlportid)
97d32cf9
FW
722{
723 struct nfqnl_instance *queue;
724
e8179610 725 queue = instance_lookup(q, queue_num);
97d32cf9
FW
726 if (!queue)
727 return ERR_PTR(-ENODEV);
728
15e47304 729 if (queue->peer_portid != nlportid)
97d32cf9
FW
730 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]);
c6675233
FW
745 verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK;
746 if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN)
97d32cf9
FW
747 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{
3da07c0c 761 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
97d32cf9
FW
762 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
e8179610
G
769 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);
97d32cf9
FW
774 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
7af4cc3f
HW
806static int
807nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
808 const struct nlmsghdr *nlh,
809 const struct nlattr * const nfqa[])
7af4cc3f 810{
3da07c0c 811 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7af4cc3f
HW
812 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;
02f014d8 817 struct nf_queue_entry *entry;
8c88f87c
PNA
818 enum ip_conntrack_info uninitialized_var(ctinfo);
819 struct nf_conn *ct = NULL;
7af4cc3f 820
e8179610
G
821 struct net *net = sock_net(ctnl);
822 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
7af4cc3f 823
e8179610
G
824 queue = instance_lookup(q, queue_num);
825 if (!queue)
826 queue = verdict_instance_lookup(q, queue_num,
827 NETLINK_CB(skb).portid);
97d32cf9
FW
828 if (IS_ERR(queue))
829 return PTR_ERR(queue);
7af4cc3f 830
97d32cf9
FW
831 vhdr = verdicthdr_get(nfqa);
832 if (!vhdr)
84a797dd 833 return -EINVAL;
7af4cc3f 834
7af4cc3f
HW
835 verdict = ntohl(vhdr->verdict);
836
b43d8d85 837 entry = find_dequeue_entry(queue, ntohl(vhdr->id));
84a797dd
ED
838 if (entry == NULL)
839 return -ENOENT;
7af4cc3f 840
9cb01766 841 rcu_read_lock();
7c622345
PNA
842 if (nfqa[NFQA_CT] && (queue->flags & NFQA_CFG_F_CONNTRACK))
843 ct = nfqnl_ct_parse(entry->skb, nfqa[NFQA_CT], &ctinfo);
9cb01766 844
df6fb868 845 if (nfqa[NFQA_PAYLOAD]) {
8c88f87c
PNA
846 u16 payload_len = nla_len(nfqa[NFQA_PAYLOAD]);
847 int diff = payload_len - entry->skb->len;
848
df6fb868 849 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
8c88f87c 850 payload_len, entry, diff) < 0)
7af4cc3f 851 verdict = NF_DROP;
8c88f87c 852
7c622345
PNA
853 if (ct)
854 nfqnl_ct_seq_adjust(skb, ct, ctinfo, diff);
7af4cc3f 855 }
8c88f87c 856 rcu_read_unlock();
7af4cc3f 857
df6fb868 858 if (nfqa[NFQA_MARK])
ea3a66ff 859 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
601e68e1 860
4b3d15ef 861 nf_reinject(entry, verdict);
7af4cc3f
HW
862 return 0;
863}
864
865static int
866nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
867 const struct nlmsghdr *nlh,
868 const struct nlattr * const nfqa[])
7af4cc3f
HW
869{
870 return -ENOTSUPP;
871}
872
5bf75853
PM
873static 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) },
838ab636
HW
876};
877
e3ac5298 878static const struct nf_queue_handler nfqh = {
bbd86b9f
HW
879 .outfn = &nfqnl_enqueue_packet,
880};
881
7af4cc3f
HW
882static int
883nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
39938324
PM
884 const struct nlmsghdr *nlh,
885 const struct nlattr * const nfqa[])
7af4cc3f 886{
3da07c0c 887 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
7af4cc3f
HW
888 u_int16_t queue_num = ntohs(nfmsg->res_id);
889 struct nfqnl_instance *queue;
9872bec7 890 struct nfqnl_msg_config_cmd *cmd = NULL;
e8179610
G
891 struct net *net = sock_net(ctnl);
892 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
838ab636 893 int ret = 0;
7af4cc3f 894
9872bec7
PM
895 if (nfqa[NFQA_CFG_CMD]) {
896 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
897
0360ae41 898 /* Obsolete commands without queue context */
9872bec7 899 switch (cmd->command) {
0360ae41
FW
900 case NFQNL_CFG_CMD_PF_BIND: return 0;
901 case NFQNL_CFG_CMD_PF_UNBIND: return 0;
9872bec7 902 }
9872bec7
PM
903 }
904
905 rcu_read_lock();
e8179610 906 queue = instance_lookup(q, queue_num);
15e47304 907 if (queue && queue->peer_portid != NETLINK_CB(skb).portid) {
a3c8e7fd 908 ret = -EPERM;
9872bec7 909 goto err_out_unlock;
a3c8e7fd
PM
910 }
911
9872bec7 912 if (cmd != NULL) {
7af4cc3f
HW
913 switch (cmd->command) {
914 case NFQNL_CFG_CMD_BIND:
9872bec7
PM
915 if (queue) {
916 ret = -EBUSY;
917 goto err_out_unlock;
918 }
e8179610
G
919 queue = instance_create(q, queue_num,
920 NETLINK_CB(skb).portid);
baab2ce7
PM
921 if (IS_ERR(queue)) {
922 ret = PTR_ERR(queue);
9872bec7
PM
923 goto err_out_unlock;
924 }
7af4cc3f
HW
925 break;
926 case NFQNL_CFG_CMD_UNBIND:
9872bec7
PM
927 if (!queue) {
928 ret = -ENODEV;
929 goto err_out_unlock;
930 }
e8179610 931 instance_destroy(q, queue);
7af4cc3f
HW
932 break;
933 case NFQNL_CFG_CMD_PF_BIND:
7af4cc3f 934 case NFQNL_CFG_CMD_PF_UNBIND:
7af4cc3f
HW
935 break;
936 default:
cd21f0ac 937 ret = -ENOTSUPP;
838ab636 938 break;
7af4cc3f 939 }
7af4cc3f
HW
940 }
941
df6fb868 942 if (nfqa[NFQA_CFG_PARAMS]) {
7af4cc3f 943 struct nfqnl_msg_config_params *params;
7af4cc3f 944
406dbfc9 945 if (!queue) {
a3c8e7fd 946 ret = -ENODEV;
9872bec7 947 goto err_out_unlock;
406dbfc9 948 }
df6fb868 949 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
7af4cc3f
HW
950 nfqnl_set_mode(queue, params->copy_mode,
951 ntohl(params->copy_range));
952 }
953
df6fb868 954 if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
829e17a1 955 __be32 *queue_maxlen;
a3c8e7fd
PM
956
957 if (!queue) {
958 ret = -ENODEV;
9872bec7 959 goto err_out_unlock;
a3c8e7fd 960 }
df6fb868 961 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
829e17a1
EL
962 spin_lock_bh(&queue->lock);
963 queue->queue_maxlen = ntohl(*queue_maxlen);
964 spin_unlock_bh(&queue->lock);
965 }
966
fdb694a0
KK
967 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
46ba5a25
KK
986 if (flags >= NFQA_CFG_F_MAX) {
987 ret = -EOPNOTSUPP;
988 goto err_out_unlock;
989 }
990
fdb694a0
KK
991 spin_lock_bh(&queue->lock);
992 queue->flags &= ~mask;
993 queue->flags |= flags & mask;
994 spin_unlock_bh(&queue->lock);
995 }
996
9872bec7
PM
997err_out_unlock:
998 rcu_read_unlock();
838ab636 999 return ret;
7af4cc3f
HW
1000}
1001
7c8d4cb4 1002static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
84a797dd 1003 [NFQNL_MSG_PACKET] = { .call_rcu = nfqnl_recv_unsupp,
37d2e7a2 1004 .attr_count = NFQA_MAX, },
84a797dd 1005 [NFQNL_MSG_VERDICT] = { .call_rcu = nfqnl_recv_verdict,
5bf75853
PM
1006 .attr_count = NFQA_MAX,
1007 .policy = nfqa_verdict_policy },
7af4cc3f 1008 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
5bf75853
PM
1009 .attr_count = NFQA_CFG_MAX,
1010 .policy = nfqa_cfg_policy },
97d32cf9
FW
1011 [NFQNL_MSG_VERDICT_BATCH]={ .call_rcu = nfqnl_recv_verdict_batch,
1012 .attr_count = NFQA_MAX,
1013 .policy = nfqa_verdict_batch_policy },
7af4cc3f
HW
1014};
1015
7c8d4cb4 1016static const struct nfnetlink_subsystem nfqnl_subsys = {
7af4cc3f
HW
1017 .name = "nf_queue",
1018 .subsys_id = NFNL_SUBSYS_QUEUE,
1019 .cb_count = NFQNL_MSG_MAX,
7af4cc3f
HW
1020 .cb = nfqnl_cb,
1021};
1022
838ab636
HW
1023#ifdef CONFIG_PROC_FS
1024struct iter_state {
e8179610 1025 struct seq_net_private p;
838ab636
HW
1026 unsigned int bucket;
1027};
1028
1029static struct hlist_node *get_first(struct seq_file *seq)
1030{
1031 struct iter_state *st = seq->private;
e8179610
G
1032 struct net *net;
1033 struct nfnl_queue_net *q;
838ab636
HW
1034
1035 if (!st)
1036 return NULL;
1037
e8179610
G
1038 net = seq_file_net(seq);
1039 q = nfnl_queue_pernet(net);
838ab636 1040 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
e8179610
G
1041 if (!hlist_empty(&q->instance_table[st->bucket]))
1042 return q->instance_table[st->bucket].first;
838ab636
HW
1043 }
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;
e8179610 1050 struct net *net = seq_file_net(seq);
838ab636
HW
1051
1052 h = h->next;
1053 while (!h) {
e8179610
G
1054 struct nfnl_queue_net *q;
1055
838ab636
HW
1056 if (++st->bucket >= INSTANCE_BUCKETS)
1057 return NULL;
1058
e8179610
G
1059 q = nfnl_queue_pernet(net);
1060 h = q->instance_table[st->bucket].first;
838ab636
HW
1061 }
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
e8179610
G
1076static void *seq_start(struct seq_file *s, loff_t *pos)
1077 __acquires(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
838ab636 1078{
e8179610
G
1079 spin_lock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
1080 return get_idx(s, *pos);
838ab636
HW
1081}
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)
e8179610 1090 __releases(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
838ab636 1091{
e8179610 1092 spin_unlock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
838ab636
HW
1093}
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,
15e47304 1101 inst->peer_portid, inst->queue_total,
838ab636
HW
1102 inst->copy_mode, inst->copy_range,
1103 inst->queue_dropped, inst->queue_user_dropped,
5863702a 1104 inst->id_sequence, 1);
838ab636
HW
1105}
1106
56b3d975 1107static const struct seq_operations nfqnl_seq_ops = {
838ab636
HW
1108 .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{
e8179610 1116 return seq_open_net(inode, file, &nfqnl_seq_ops,
e2da5913 1117 sizeof(struct iter_state));
838ab636
HW
1118}
1119
da7071d7 1120static const struct file_operations nfqnl_file_ops = {
838ab636
HW
1121 .owner = THIS_MODULE,
1122 .open = nfqnl_open,
1123 .read = seq_read,
1124 .llseek = seq_lseek,
e8179610 1125 .release = seq_release_net,
838ab636
HW
1126};
1127
1128#endif /* PROC_FS */
1129
e8179610 1130static int __net_init nfnl_queue_net_init(struct net *net)
7af4cc3f 1131{
e8179610
G
1132 unsigned int i;
1133 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
601e68e1 1134
838ab636 1135 for (i = 0; i < INSTANCE_BUCKETS; i++)
e8179610
G
1136 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;
838ab636 1163
7af4cc3f
HW
1164 netlink_register_notifier(&nfqnl_rtnl_notifier);
1165 status = nfnetlink_subsys_register(&nfqnl_subsys);
1166 if (status < 0) {
e8179610 1167 pr_err("nf_queue: failed to create netlink socket\n");
7af4cc3f
HW
1168 goto cleanup_netlink_notifier;
1169 }
1170
e8179610
G
1171 status = register_pernet_subsys(&nfnl_queue_net_ops);
1172 if (status < 0) {
1173 pr_err("nf_queue: failed to register pernet ops\n");
838ab636 1174 goto cleanup_subsys;
e8179610 1175 }
7af4cc3f 1176 register_netdevice_notifier(&nfqnl_dev_notifier);
0360ae41 1177 nf_register_queue_handler(&nfqh);
7af4cc3f
HW
1178 return status;
1179
838ab636 1180cleanup_subsys:
7af4cc3f 1181 nfnetlink_subsys_unregister(&nfqnl_subsys);
7af4cc3f
HW
1182cleanup_netlink_notifier:
1183 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1184 return status;
1185}
1186
65b4b4e8 1187static void __exit nfnetlink_queue_fini(void)
7af4cc3f 1188{
0360ae41 1189 nf_unregister_queue_handler();
32292a7f 1190 unregister_netdevice_notifier(&nfqnl_dev_notifier);
e8179610 1191 unregister_pernet_subsys(&nfnl_queue_net_ops);
32292a7f
PM
1192 nfnetlink_subsys_unregister(&nfqnl_subsys);
1193 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
67137f3c
JDB
1194
1195 rcu_barrier(); /* Wait for completion of call_rcu()'s */
7af4cc3f
HW
1196}
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
65b4b4e8
AM
1203module_init(nfnetlink_queue_init);
1204module_exit(nfnetlink_queue_fini);
This page took 0.818321 seconds and 5 git commands to generate.