blob: 18ed9c5d209ca8e13b1cfc7330862fc3534cd275 [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
3 * userspace via nfetlink.
4 *
5 * (C) 2005 by Harald Welte <laforge@netfilter.org>
6 *
7 * Based on the old ipv4-only ip_queue.c:
8 * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
9 * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
14 *
15 */
16#include <linux/module.h>
17#include <linux/skbuff.h>
18#include <linux/init.h>
19#include <linux/spinlock.h>
20#include <linux/notifier.h>
21#include <linux/netdevice.h>
22#include <linux/netfilter.h>
Harald Welte838ab632005-08-09 19:50:45 -070023#include <linux/proc_fs.h>
Harald Welte7af4cc32005-08-09 19:44:15 -070024#include <linux/netfilter_ipv4.h>
25#include <linux/netfilter_ipv6.h>
26#include <linux/netfilter/nfnetlink.h>
27#include <linux/netfilter/nfnetlink_queue.h>
28#include <linux/list.h>
29#include <net/sock.h>
30
31#include <asm/atomic.h>
32
Harald Weltefbcd9232005-08-09 20:22:10 -070033#ifdef CONFIG_BRIDGE_NETFILTER
34#include "../bridge/br_private.h"
35#endif
36
Harald Welte7af4cc32005-08-09 19:44:15 -070037#define NFQNL_QMAX_DEFAULT 1024
38
39#if 0
40#define QDEBUG(x, args ...) printk(KERN_DEBUG "%s(%d):%s(): " x, \
41 __FILE__, __LINE__, __FUNCTION__, \
42 ## args)
43#else
44#define QDEBUG(x, ...)
45#endif
46
47struct nfqnl_queue_entry {
48 struct list_head list;
49 struct nf_info *info;
50 struct sk_buff *skb;
51 unsigned int id;
52};
53
54struct nfqnl_instance {
55 struct hlist_node hlist; /* global list of queues */
Harald Welte838ab632005-08-09 19:50:45 -070056 atomic_t use;
Harald Welte7af4cc32005-08-09 19:44:15 -070057
58 int peer_pid;
59 unsigned int queue_maxlen;
60 unsigned int copy_range;
61 unsigned int queue_total;
62 unsigned int queue_dropped;
63 unsigned int queue_user_dropped;
64
65 atomic_t id_sequence; /* 'sequence' of pkt ids */
66
67 u_int16_t queue_num; /* number of this queue */
68 u_int8_t copy_mode;
69
70 spinlock_t lock;
71
72 struct list_head queue_list; /* packets in queue */
73};
74
75typedef int (*nfqnl_cmpfn)(struct nfqnl_queue_entry *, unsigned long);
76
77static DEFINE_RWLOCK(instances_lock);
78
Harald Welte7af4cc32005-08-09 19:44:15 -070079#define INSTANCE_BUCKETS 16
80static struct hlist_head instance_table[INSTANCE_BUCKETS];
81
82static inline u_int8_t instance_hashfn(u_int16_t queue_num)
83{
84 return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
85}
86
87static struct nfqnl_instance *
88__instance_lookup(u_int16_t queue_num)
89{
90 struct hlist_head *head;
91 struct hlist_node *pos;
92 struct nfqnl_instance *inst;
93
94 head = &instance_table[instance_hashfn(queue_num)];
95 hlist_for_each_entry(inst, pos, head, hlist) {
96 if (inst->queue_num == queue_num)
97 return inst;
98 }
99 return NULL;
100}
101
102static struct nfqnl_instance *
Harald Welte838ab632005-08-09 19:50:45 -0700103instance_lookup_get(u_int16_t queue_num)
Harald Welte7af4cc32005-08-09 19:44:15 -0700104{
105 struct nfqnl_instance *inst;
106
107 read_lock_bh(&instances_lock);
108 inst = __instance_lookup(queue_num);
Harald Welte838ab632005-08-09 19:50:45 -0700109 if (inst)
110 atomic_inc(&inst->use);
Harald Welte7af4cc32005-08-09 19:44:15 -0700111 read_unlock_bh(&instances_lock);
112
113 return inst;
114}
115
Harald Welte838ab632005-08-09 19:50:45 -0700116static void
117instance_put(struct nfqnl_instance *inst)
118{
119 if (inst && atomic_dec_and_test(&inst->use)) {
120 QDEBUG("kfree(inst=%p)\n", inst);
121 kfree(inst);
122 }
123}
124
Harald Welte7af4cc32005-08-09 19:44:15 -0700125static struct nfqnl_instance *
126instance_create(u_int16_t queue_num, int pid)
127{
128 struct nfqnl_instance *inst;
129
130 QDEBUG("entering for queue_num=%u, pid=%d\n", queue_num, pid);
131
132 write_lock_bh(&instances_lock);
133 if (__instance_lookup(queue_num)) {
134 inst = NULL;
135 QDEBUG("aborting, instance already exists\n");
136 goto out_unlock;
137 }
138
Harald Welte10dfdc62005-11-03 19:20:07 +0100139 inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
Harald Welte7af4cc32005-08-09 19:44:15 -0700140 if (!inst)
141 goto out_unlock;
142
Harald Welte7af4cc32005-08-09 19:44:15 -0700143 inst->queue_num = queue_num;
144 inst->peer_pid = pid;
145 inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
146 inst->copy_range = 0xfffff;
147 inst->copy_mode = NFQNL_COPY_NONE;
148 atomic_set(&inst->id_sequence, 0);
Harald Welte838ab632005-08-09 19:50:45 -0700149 /* needs to be two, since we _put() after creation */
150 atomic_set(&inst->use, 2);
YOSHIFUJI Hideaki181a46a2006-01-04 13:56:54 -0800151 spin_lock_init(&inst->lock);
Harald Welte7af4cc32005-08-09 19:44:15 -0700152 INIT_LIST_HEAD(&inst->queue_list);
153
154 if (!try_module_get(THIS_MODULE))
155 goto out_free;
156
157 hlist_add_head(&inst->hlist,
158 &instance_table[instance_hashfn(queue_num)]);
159
160 write_unlock_bh(&instances_lock);
161
162 QDEBUG("successfully created new instance\n");
163
164 return inst;
165
166out_free:
167 kfree(inst);
168out_unlock:
169 write_unlock_bh(&instances_lock);
170 return NULL;
171}
172
173static void nfqnl_flush(struct nfqnl_instance *queue, int verdict);
174
175static void
176_instance_destroy2(struct nfqnl_instance *inst, int lock)
177{
178 /* first pull it out of the global list */
179 if (lock)
180 write_lock_bh(&instances_lock);
181
182 QDEBUG("removing instance %p (queuenum=%u) from hash\n",
183 inst, inst->queue_num);
184 hlist_del(&inst->hlist);
185
186 if (lock)
187 write_unlock_bh(&instances_lock);
188
189 /* then flush all pending skbs from the queue */
190 nfqnl_flush(inst, NF_DROP);
191
Harald Welte838ab632005-08-09 19:50:45 -0700192 /* and finally put the refcount */
193 instance_put(inst);
Harald Welte7af4cc32005-08-09 19:44:15 -0700194
195 module_put(THIS_MODULE);
196}
197
198static inline void
199__instance_destroy(struct nfqnl_instance *inst)
200{
201 _instance_destroy2(inst, 0);
202}
203
204static inline void
205instance_destroy(struct nfqnl_instance *inst)
206{
207 _instance_destroy2(inst, 1);
208}
209
210
211
212static void
213issue_verdict(struct nfqnl_queue_entry *entry, int verdict)
214{
215 QDEBUG("entering for entry %p, verdict %u\n", entry, verdict);
216
217 /* TCP input path (and probably other bits) assume to be called
218 * from softirq context, not from syscall, like issue_verdict is
219 * called. TCP input path deadlocks with locks taken from timer
220 * softirq, e.g. We therefore emulate this by local_bh_disable() */
221
222 local_bh_disable();
223 nf_reinject(entry->skb, entry->info, verdict);
224 local_bh_enable();
225
226 kfree(entry);
227}
228
229static inline void
230__enqueue_entry(struct nfqnl_instance *queue,
231 struct nfqnl_queue_entry *entry)
232{
233 list_add(&entry->list, &queue->queue_list);
234 queue->queue_total++;
235}
236
237/*
238 * Find and return a queued entry matched by cmpfn, or return the last
239 * entry if cmpfn is NULL.
240 */
241static inline struct nfqnl_queue_entry *
242__find_entry(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
243 unsigned long data)
244{
245 struct list_head *p;
246
247 list_for_each_prev(p, &queue->queue_list) {
248 struct nfqnl_queue_entry *entry = (struct nfqnl_queue_entry *)p;
249
250 if (!cmpfn || cmpfn(entry, data))
251 return entry;
252 }
253 return NULL;
254}
255
256static inline void
257__dequeue_entry(struct nfqnl_instance *q, struct nfqnl_queue_entry *entry)
258{
259 list_del(&entry->list);
260 q->queue_total--;
261}
262
263static inline struct nfqnl_queue_entry *
264__find_dequeue_entry(struct nfqnl_instance *queue,
265 nfqnl_cmpfn cmpfn, unsigned long data)
266{
267 struct nfqnl_queue_entry *entry;
268
269 entry = __find_entry(queue, cmpfn, data);
270 if (entry == NULL)
271 return NULL;
272
273 __dequeue_entry(queue, entry);
274 return entry;
275}
276
277
278static inline void
279__nfqnl_flush(struct nfqnl_instance *queue, int verdict)
280{
281 struct nfqnl_queue_entry *entry;
282
283 while ((entry = __find_dequeue_entry(queue, NULL, 0)))
284 issue_verdict(entry, verdict);
285}
286
287static inline int
288__nfqnl_set_mode(struct nfqnl_instance *queue,
289 unsigned char mode, unsigned int range)
290{
291 int status = 0;
292
293 switch (mode) {
294 case NFQNL_COPY_NONE:
295 case NFQNL_COPY_META:
296 queue->copy_mode = mode;
297 queue->copy_range = 0;
298 break;
299
300 case NFQNL_COPY_PACKET:
301 queue->copy_mode = mode;
302 /* we're using struct nfattr which has 16bit nfa_len */
303 if (range > 0xffff)
304 queue->copy_range = 0xffff;
305 else
306 queue->copy_range = range;
307 break;
308
309 default:
310 status = -EINVAL;
311
312 }
313 return status;
314}
315
316static struct nfqnl_queue_entry *
317find_dequeue_entry(struct nfqnl_instance *queue,
318 nfqnl_cmpfn cmpfn, unsigned long data)
319{
320 struct nfqnl_queue_entry *entry;
321
322 spin_lock_bh(&queue->lock);
323 entry = __find_dequeue_entry(queue, cmpfn, data);
324 spin_unlock_bh(&queue->lock);
325
326 return entry;
327}
328
329static void
330nfqnl_flush(struct nfqnl_instance *queue, int verdict)
331{
332 spin_lock_bh(&queue->lock);
333 __nfqnl_flush(queue, verdict);
334 spin_unlock_bh(&queue->lock);
335}
336
337static struct sk_buff *
338nfqnl_build_packet_message(struct nfqnl_instance *queue,
339 struct nfqnl_queue_entry *entry, int *errp)
340{
341 unsigned char *old_tail;
342 size_t size;
343 size_t data_len = 0;
344 struct sk_buff *skb;
345 struct nfqnl_msg_packet_hdr pmsg;
346 struct nlmsghdr *nlh;
347 struct nfgenmsg *nfmsg;
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800348 struct nf_info *entinf = entry->info;
349 struct sk_buff *entskb = entry->skb;
350 struct net_device *indev;
351 struct net_device *outdev;
Harald Welte7af4cc32005-08-09 19:44:15 -0700352 unsigned int tmp_uint;
353
354 QDEBUG("entered\n");
355
356 /* all macros expand to constant values at compile time */
357 size = NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_hdr))
358 + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */
359 + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */
Harald Weltefbcd9232005-08-09 20:22:10 -0700360#ifdef CONFIG_BRIDGE_NETFILTER
361 + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */
362 + NLMSG_SPACE(sizeof(u_int32_t)) /* ifindex */
363#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700364 + NLMSG_SPACE(sizeof(u_int32_t)) /* mark */
365 + NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_hw))
366 + NLMSG_SPACE(sizeof(struct nfqnl_msg_packet_timestamp));
367
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800368 outdev = entinf->outdev;
369
Harald Welte7af4cc32005-08-09 19:44:15 -0700370 spin_lock_bh(&queue->lock);
371
372 switch (queue->copy_mode) {
373 case NFQNL_COPY_META:
374 case NFQNL_COPY_NONE:
375 data_len = 0;
376 break;
377
378 case NFQNL_COPY_PACKET:
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800379 if (entskb->ip_summed == CHECKSUM_HW &&
380 (*errp = skb_checksum_help(entskb,
381 outdev == NULL))) {
Patrick McHardye7dfb092005-09-06 15:10:00 -0700382 spin_unlock_bh(&queue->lock);
383 return NULL;
384 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700385 if (queue->copy_range == 0
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800386 || queue->copy_range > entskb->len)
387 data_len = entskb->len;
Harald Welte7af4cc32005-08-09 19:44:15 -0700388 else
389 data_len = queue->copy_range;
390
391 size += NLMSG_SPACE(data_len);
392 break;
393
394 default:
395 *errp = -EINVAL;
396 spin_unlock_bh(&queue->lock);
397 return NULL;
398 }
399
400 spin_unlock_bh(&queue->lock);
401
402 skb = alloc_skb(size, GFP_ATOMIC);
403 if (!skb)
404 goto nlmsg_failure;
405
406 old_tail= skb->tail;
407 nlh = NLMSG_PUT(skb, 0, 0,
408 NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
409 sizeof(struct nfgenmsg));
410 nfmsg = NLMSG_DATA(nlh);
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800411 nfmsg->nfgen_family = entinf->pf;
Harald Welte7af4cc32005-08-09 19:44:15 -0700412 nfmsg->version = NFNETLINK_V0;
413 nfmsg->res_id = htons(queue->queue_num);
414
415 pmsg.packet_id = htonl(entry->id);
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800416 pmsg.hw_protocol = htons(entskb->protocol);
417 pmsg.hook = entinf->hook;
Harald Welte7af4cc32005-08-09 19:44:15 -0700418
419 NFA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
420
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800421 indev = entinf->indev;
422 if (indev) {
423 tmp_uint = htonl(indev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700424#ifndef CONFIG_BRIDGE_NETFILTER
Harald Welte7af4cc32005-08-09 19:44:15 -0700425 NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint), &tmp_uint);
Harald Weltefbcd9232005-08-09 20:22:10 -0700426#else
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800427 if (entinf->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700428 /* Case 1: indev is physical input device, we need to
429 * look for bridge group (when called from
430 * netfilter_bridge) */
431 NFA_PUT(skb, NFQA_IFINDEX_PHYSINDEV, sizeof(tmp_uint),
432 &tmp_uint);
433 /* this is the bridge group "brX" */
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800434 tmp_uint = htonl(indev->br_port->br->dev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700435 NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
436 &tmp_uint);
437 } else {
438 /* Case 2: indev is bridge group, we need to look for
439 * physical device (when called from ipv4) */
440 NFA_PUT(skb, NFQA_IFINDEX_INDEV, sizeof(tmp_uint),
441 &tmp_uint);
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800442 if (entskb->nf_bridge
443 && entskb->nf_bridge->physindev) {
444 tmp_uint = htonl(entskb->nf_bridge->physindev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700445 NFA_PUT(skb, NFQA_IFINDEX_PHYSINDEV,
446 sizeof(tmp_uint), &tmp_uint);
447 }
448 }
449#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700450 }
451
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800452 if (outdev) {
453 tmp_uint = htonl(outdev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700454#ifndef CONFIG_BRIDGE_NETFILTER
Harald Welte7af4cc32005-08-09 19:44:15 -0700455 NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint), &tmp_uint);
Harald Weltefbcd9232005-08-09 20:22:10 -0700456#else
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800457 if (entinf->pf == PF_BRIDGE) {
Harald Weltefbcd9232005-08-09 20:22:10 -0700458 /* Case 1: outdev is physical output device, we need to
459 * look for bridge group (when called from
460 * netfilter_bridge) */
461 NFA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV, sizeof(tmp_uint),
462 &tmp_uint);
463 /* this is the bridge group "brX" */
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800464 tmp_uint = htonl(outdev->br_port->br->dev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700465 NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
466 &tmp_uint);
467 } else {
468 /* Case 2: outdev is bridge group, we need to look for
469 * physical output device (when called from ipv4) */
470 NFA_PUT(skb, NFQA_IFINDEX_OUTDEV, sizeof(tmp_uint),
471 &tmp_uint);
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800472 if (entskb->nf_bridge
473 && entskb->nf_bridge->physoutdev) {
474 tmp_uint = htonl(entskb->nf_bridge->physoutdev->ifindex);
Harald Weltefbcd9232005-08-09 20:22:10 -0700475 NFA_PUT(skb, NFQA_IFINDEX_PHYSOUTDEV,
476 sizeof(tmp_uint), &tmp_uint);
477 }
478 }
479#endif
Harald Welte7af4cc32005-08-09 19:44:15 -0700480 }
481
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800482 if (entskb->nfmark) {
483 tmp_uint = htonl(entskb->nfmark);
Harald Welte7af4cc32005-08-09 19:44:15 -0700484 NFA_PUT(skb, NFQA_MARK, sizeof(u_int32_t), &tmp_uint);
485 }
486
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800487 if (indev && entskb->dev
488 && entskb->dev->hard_header_parse) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700489 struct nfqnl_msg_packet_hw phw;
490
491 phw.hw_addrlen =
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800492 entskb->dev->hard_header_parse(entskb,
Harald Welte7af4cc32005-08-09 19:44:15 -0700493 phw.hw_addr);
494 phw.hw_addrlen = htons(phw.hw_addrlen);
495 NFA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
496 }
497
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800498 if (entskb->tstamp.off_sec) {
Harald Welte7af4cc32005-08-09 19:44:15 -0700499 struct nfqnl_msg_packet_timestamp ts;
500
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800501 ts.sec = cpu_to_be64(entskb->tstamp.off_sec);
502 ts.usec = cpu_to_be64(entskb->tstamp.off_usec);
Harald Welte7af4cc32005-08-09 19:44:15 -0700503
504 NFA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
505 }
506
507 if (data_len) {
508 struct nfattr *nfa;
509 int size = NFA_LENGTH(data_len);
510
511 if (skb_tailroom(skb) < (int)NFA_SPACE(data_len)) {
512 printk(KERN_WARNING "nf_queue: no tailroom!\n");
513 goto nlmsg_failure;
514 }
515
516 nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size));
517 nfa->nfa_type = NFQA_PAYLOAD;
518 nfa->nfa_len = size;
519
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800520 if (skb_copy_bits(entskb, 0, NFA_DATA(nfa), data_len))
Harald Welte7af4cc32005-08-09 19:44:15 -0700521 BUG();
522 }
523
524 nlh->nlmsg_len = skb->tail - old_tail;
525 return skb;
526
527nlmsg_failure:
528nfattr_failure:
529 if (skb)
530 kfree_skb(skb);
531 *errp = -EINVAL;
532 if (net_ratelimit())
533 printk(KERN_ERR "nf_queue: error creating packet message\n");
534 return NULL;
535}
536
537static int
538nfqnl_enqueue_packet(struct sk_buff *skb, struct nf_info *info,
539 unsigned int queuenum, void *data)
540{
541 int status = -EINVAL;
542 struct sk_buff *nskb;
543 struct nfqnl_instance *queue;
544 struct nfqnl_queue_entry *entry;
545
546 QDEBUG("entered\n");
547
Harald Welte838ab632005-08-09 19:50:45 -0700548 queue = instance_lookup_get(queuenum);
Harald Welte7af4cc32005-08-09 19:44:15 -0700549 if (!queue) {
550 QDEBUG("no queue instance matching\n");
551 return -EINVAL;
552 }
553
554 if (queue->copy_mode == NFQNL_COPY_NONE) {
555 QDEBUG("mode COPY_NONE, aborting\n");
Harald Welte838ab632005-08-09 19:50:45 -0700556 status = -EAGAIN;
557 goto err_out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700558 }
559
560 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
561 if (entry == NULL) {
562 if (net_ratelimit())
563 printk(KERN_ERR
564 "nf_queue: OOM in nfqnl_enqueue_packet()\n");
Harald Welte838ab632005-08-09 19:50:45 -0700565 status = -ENOMEM;
566 goto err_out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700567 }
568
569 entry->info = info;
570 entry->skb = skb;
571 entry->id = atomic_inc_return(&queue->id_sequence);
572
573 nskb = nfqnl_build_packet_message(queue, entry, &status);
574 if (nskb == NULL)
575 goto err_out_free;
576
577 spin_lock_bh(&queue->lock);
578
579 if (!queue->peer_pid)
580 goto err_out_free_nskb;
581
582 if (queue->queue_total >= queue->queue_maxlen) {
583 queue->queue_dropped++;
584 status = -ENOSPC;
585 if (net_ratelimit())
586 printk(KERN_WARNING "ip_queue: full at %d entries, "
587 "dropping packets(s). Dropped: %d\n",
588 queue->queue_total, queue->queue_dropped);
589 goto err_out_free_nskb;
590 }
591
592 /* nfnetlink_unicast will either free the nskb or add it to a socket */
593 status = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
594 if (status < 0) {
595 queue->queue_user_dropped++;
596 goto err_out_unlock;
597 }
598
599 __enqueue_entry(queue, entry);
600
601 spin_unlock_bh(&queue->lock);
Harald Welte838ab632005-08-09 19:50:45 -0700602 instance_put(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700603 return status;
604
605err_out_free_nskb:
606 kfree_skb(nskb);
607
608err_out_unlock:
609 spin_unlock_bh(&queue->lock);
610
611err_out_free:
612 kfree(entry);
Harald Welte838ab632005-08-09 19:50:45 -0700613err_out_put:
614 instance_put(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700615 return status;
616}
617
618static int
619nfqnl_mangle(void *data, int data_len, struct nfqnl_queue_entry *e)
620{
621 int diff;
622
623 diff = data_len - e->skb->len;
624 if (diff < 0)
625 skb_trim(e->skb, data_len);
626 else if (diff > 0) {
627 if (data_len > 0xFFFF)
628 return -EINVAL;
629 if (diff > skb_tailroom(e->skb)) {
630 struct sk_buff *newskb;
631
632 newskb = skb_copy_expand(e->skb,
633 skb_headroom(e->skb),
634 diff,
635 GFP_ATOMIC);
636 if (newskb == NULL) {
637 printk(KERN_WARNING "ip_queue: OOM "
638 "in mangle, dropping packet\n");
639 return -ENOMEM;
640 }
641 if (e->skb->sk)
642 skb_set_owner_w(newskb, e->skb->sk);
643 kfree_skb(e->skb);
644 e->skb = newskb;
645 }
646 skb_put(e->skb, diff);
647 }
648 if (!skb_make_writable(&e->skb, data_len))
649 return -ENOMEM;
650 memcpy(e->skb->data, data, data_len);
Patrick McHardye7dfb092005-09-06 15:10:00 -0700651 e->skb->ip_summed = CHECKSUM_NONE;
Harald Welte7af4cc32005-08-09 19:44:15 -0700652 return 0;
653}
654
655static inline int
656id_cmp(struct nfqnl_queue_entry *e, unsigned long id)
657{
658 return (id == e->id);
659}
660
661static int
662nfqnl_set_mode(struct nfqnl_instance *queue,
663 unsigned char mode, unsigned int range)
664{
665 int status;
666
667 spin_lock_bh(&queue->lock);
668 status = __nfqnl_set_mode(queue, mode, range);
669 spin_unlock_bh(&queue->lock);
670
671 return status;
672}
673
674static int
675dev_cmp(struct nfqnl_queue_entry *entry, unsigned long ifindex)
676{
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800677 struct nf_info *entinf = entry->info;
678
679 if (entinf->indev)
680 if (entinf->indev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700681 return 1;
682
Jesper Juhl3e4ead42006-01-05 12:15:58 -0800683 if (entinf->outdev)
684 if (entinf->outdev->ifindex == ifindex)
Harald Welte7af4cc32005-08-09 19:44:15 -0700685 return 1;
686
687 return 0;
688}
689
690/* drop all packets with either indev or outdev == ifindex from all queue
691 * instances */
692static void
693nfqnl_dev_drop(int ifindex)
694{
695 int i;
696
697 QDEBUG("entering for ifindex %u\n", ifindex);
698
699 /* this only looks like we have to hold the readlock for a way too long
700 * time, issue_verdict(), nf_reinject(), ... - but we always only
701 * issue NF_DROP, which is processed directly in nf_reinject() */
702 read_lock_bh(&instances_lock);
703
704 for (i = 0; i < INSTANCE_BUCKETS; i++) {
705 struct hlist_node *tmp;
706 struct nfqnl_instance *inst;
707 struct hlist_head *head = &instance_table[i];
708
709 hlist_for_each_entry(inst, tmp, head, hlist) {
710 struct nfqnl_queue_entry *entry;
711 while ((entry = find_dequeue_entry(inst, dev_cmp,
712 ifindex)) != NULL)
713 issue_verdict(entry, NF_DROP);
714 }
715 }
716
717 read_unlock_bh(&instances_lock);
718}
719
720#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
721
722static int
723nfqnl_rcv_dev_event(struct notifier_block *this,
724 unsigned long event, void *ptr)
725{
726 struct net_device *dev = ptr;
727
728 /* Drop any packets associated with the downed device */
729 if (event == NETDEV_DOWN)
730 nfqnl_dev_drop(dev->ifindex);
731 return NOTIFY_DONE;
732}
733
734static struct notifier_block nfqnl_dev_notifier = {
735 .notifier_call = nfqnl_rcv_dev_event,
736};
737
738static int
739nfqnl_rcv_nl_event(struct notifier_block *this,
740 unsigned long event, void *ptr)
741{
742 struct netlink_notify *n = ptr;
743
744 if (event == NETLINK_URELEASE &&
745 n->protocol == NETLINK_NETFILTER && n->pid) {
746 int i;
747
748 /* destroy all instances for this pid */
749 write_lock_bh(&instances_lock);
750 for (i = 0; i < INSTANCE_BUCKETS; i++) {
751 struct hlist_node *tmp, *t2;
752 struct nfqnl_instance *inst;
753 struct hlist_head *head = &instance_table[i];
754
755 hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
756 if (n->pid == inst->peer_pid)
757 __instance_destroy(inst);
758 }
759 }
760 write_unlock_bh(&instances_lock);
761 }
762 return NOTIFY_DONE;
763}
764
765static struct notifier_block nfqnl_rtnl_notifier = {
766 .notifier_call = nfqnl_rcv_nl_event,
767};
768
Harald Welte838ab632005-08-09 19:50:45 -0700769static const int nfqa_verdict_min[NFQA_MAX] = {
770 [NFQA_VERDICT_HDR-1] = sizeof(struct nfqnl_msg_verdict_hdr),
771 [NFQA_MARK-1] = sizeof(u_int32_t),
772 [NFQA_PAYLOAD-1] = 0,
773};
774
Harald Welte7af4cc32005-08-09 19:44:15 -0700775static int
776nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
777 struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
778{
779 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
780 u_int16_t queue_num = ntohs(nfmsg->res_id);
781
782 struct nfqnl_msg_verdict_hdr *vhdr;
783 struct nfqnl_instance *queue;
784 unsigned int verdict;
785 struct nfqnl_queue_entry *entry;
Harald Welte838ab632005-08-09 19:50:45 -0700786 int err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700787
Harald Welte838ab632005-08-09 19:50:45 -0700788 if (nfattr_bad_size(nfqa, NFQA_MAX, nfqa_verdict_min)) {
789 QDEBUG("bad attribute size\n");
790 return -EINVAL;
791 }
792
793 queue = instance_lookup_get(queue_num);
Harald Welte7af4cc32005-08-09 19:44:15 -0700794 if (!queue)
795 return -ENODEV;
796
Harald Welte838ab632005-08-09 19:50:45 -0700797 if (queue->peer_pid != NETLINK_CB(skb).pid) {
798 err = -EPERM;
799 goto err_out_put;
800 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700801
Harald Welte838ab632005-08-09 19:50:45 -0700802 if (!nfqa[NFQA_VERDICT_HDR-1]) {
803 err = -EINVAL;
804 goto err_out_put;
805 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700806
807 vhdr = NFA_DATA(nfqa[NFQA_VERDICT_HDR-1]);
808 verdict = ntohl(vhdr->verdict);
809
Harald Welte838ab632005-08-09 19:50:45 -0700810 if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
811 err = -EINVAL;
812 goto err_out_put;
813 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700814
815 entry = find_dequeue_entry(queue, id_cmp, ntohl(vhdr->id));
Harald Welte838ab632005-08-09 19:50:45 -0700816 if (entry == NULL) {
817 err = -ENOENT;
818 goto err_out_put;
819 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700820
821 if (nfqa[NFQA_PAYLOAD-1]) {
822 if (nfqnl_mangle(NFA_DATA(nfqa[NFQA_PAYLOAD-1]),
823 NFA_PAYLOAD(nfqa[NFQA_PAYLOAD-1]), entry) < 0)
824 verdict = NF_DROP;
825 }
826
827 if (nfqa[NFQA_MARK-1])
828 skb->nfmark = ntohl(*(u_int32_t *)NFA_DATA(nfqa[NFQA_MARK-1]));
829
830 issue_verdict(entry, verdict);
Harald Welte838ab632005-08-09 19:50:45 -0700831 instance_put(queue);
Harald Welte7af4cc32005-08-09 19:44:15 -0700832 return 0;
Harald Welte838ab632005-08-09 19:50:45 -0700833
834err_out_put:
835 instance_put(queue);
836 return err;
Harald Welte7af4cc32005-08-09 19:44:15 -0700837}
838
839static int
840nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
841 struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
842{
843 return -ENOTSUPP;
844}
845
Harald Welte838ab632005-08-09 19:50:45 -0700846static const int nfqa_cfg_min[NFQA_CFG_MAX] = {
847 [NFQA_CFG_CMD-1] = sizeof(struct nfqnl_msg_config_cmd),
848 [NFQA_CFG_PARAMS-1] = sizeof(struct nfqnl_msg_config_params),
849};
850
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700851static struct nf_queue_handler nfqh = {
852 .name = "nf_queue",
853 .outfn = &nfqnl_enqueue_packet,
854};
855
Harald Welte7af4cc32005-08-09 19:44:15 -0700856static int
857nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
858 struct nlmsghdr *nlh, struct nfattr *nfqa[], int *errp)
859{
860 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
861 u_int16_t queue_num = ntohs(nfmsg->res_id);
862 struct nfqnl_instance *queue;
Harald Welte838ab632005-08-09 19:50:45 -0700863 int ret = 0;
Harald Welte7af4cc32005-08-09 19:44:15 -0700864
865 QDEBUG("entering for msg %u\n", NFNL_MSG_TYPE(nlh->nlmsg_type));
866
Harald Welte838ab632005-08-09 19:50:45 -0700867 if (nfattr_bad_size(nfqa, NFQA_CFG_MAX, nfqa_cfg_min)) {
868 QDEBUG("bad attribute size\n");
869 return -EINVAL;
870 }
871
872 queue = instance_lookup_get(queue_num);
Harald Welte7af4cc32005-08-09 19:44:15 -0700873 if (nfqa[NFQA_CFG_CMD-1]) {
874 struct nfqnl_msg_config_cmd *cmd;
875 cmd = NFA_DATA(nfqa[NFQA_CFG_CMD-1]);
876 QDEBUG("found CFG_CMD\n");
877
878 switch (cmd->command) {
879 case NFQNL_CFG_CMD_BIND:
880 if (queue)
881 return -EBUSY;
882
883 queue = instance_create(queue_num, NETLINK_CB(skb).pid);
884 if (!queue)
885 return -EINVAL;
886 break;
887 case NFQNL_CFG_CMD_UNBIND:
888 if (!queue)
889 return -ENODEV;
890
Harald Welte838ab632005-08-09 19:50:45 -0700891 if (queue->peer_pid != NETLINK_CB(skb).pid) {
892 ret = -EPERM;
893 goto out_put;
894 }
Harald Welte7af4cc32005-08-09 19:44:15 -0700895
896 instance_destroy(queue);
897 break;
898 case NFQNL_CFG_CMD_PF_BIND:
899 QDEBUG("registering queue handler for pf=%u\n",
900 ntohs(cmd->pf));
Harald Weltebbd86b9f2005-08-09 20:23:11 -0700901 ret = nf_register_queue_handler(ntohs(cmd->pf), &nfqh);
Harald Welte7af4cc32005-08-09 19:44:15 -0700902 break;
903 case NFQNL_CFG_CMD_PF_UNBIND:
904 QDEBUG("unregistering queue handler for pf=%u\n",
905 ntohs(cmd->pf));
906 /* This is a bug and a feature. We can unregister
907 * other handlers(!) */
Harald Welte838ab632005-08-09 19:50:45 -0700908 ret = nf_unregister_queue_handler(ntohs(cmd->pf));
Harald Welte7af4cc32005-08-09 19:44:15 -0700909 break;
910 default:
Harald Welte838ab632005-08-09 19:50:45 -0700911 ret = -EINVAL;
912 break;
Harald Welte7af4cc32005-08-09 19:44:15 -0700913 }
914 } else {
915 if (!queue) {
916 QDEBUG("no config command, and no instance ENOENT\n");
Harald Welte838ab632005-08-09 19:50:45 -0700917 ret = -ENOENT;
918 goto out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700919 }
920
921 if (queue->peer_pid != NETLINK_CB(skb).pid) {
922 QDEBUG("no config command, and wrong pid\n");
Harald Welte838ab632005-08-09 19:50:45 -0700923 ret = -EPERM;
924 goto out_put;
Harald Welte7af4cc32005-08-09 19:44:15 -0700925 }
926 }
927
928 if (nfqa[NFQA_CFG_PARAMS-1]) {
929 struct nfqnl_msg_config_params *params;
930 params = NFA_DATA(nfqa[NFQA_CFG_PARAMS-1]);
931
932 nfqnl_set_mode(queue, params->copy_mode,
933 ntohl(params->copy_range));
934 }
935
Harald Welte838ab632005-08-09 19:50:45 -0700936out_put:
937 instance_put(queue);
938 return ret;
Harald Welte7af4cc32005-08-09 19:44:15 -0700939}
940
941static struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
942 [NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp,
Harald Welte37d2e7a2005-11-14 15:24:59 -0800943 .attr_count = NFQA_MAX, },
Harald Welte7af4cc32005-08-09 19:44:15 -0700944 [NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict,
Harald Welte37d2e7a2005-11-14 15:24:59 -0800945 .attr_count = NFQA_MAX, },
Harald Welte7af4cc32005-08-09 19:44:15 -0700946 [NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
Harald Welte37d2e7a2005-11-14 15:24:59 -0800947 .attr_count = NFQA_CFG_MAX, },
Harald Welte7af4cc32005-08-09 19:44:15 -0700948};
949
950static struct nfnetlink_subsystem nfqnl_subsys = {
951 .name = "nf_queue",
952 .subsys_id = NFNL_SUBSYS_QUEUE,
953 .cb_count = NFQNL_MSG_MAX,
Harald Welte7af4cc32005-08-09 19:44:15 -0700954 .cb = nfqnl_cb,
955};
956
Harald Welte838ab632005-08-09 19:50:45 -0700957#ifdef CONFIG_PROC_FS
958struct iter_state {
959 unsigned int bucket;
960};
961
962static struct hlist_node *get_first(struct seq_file *seq)
963{
964 struct iter_state *st = seq->private;
965
966 if (!st)
967 return NULL;
968
969 for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
970 if (!hlist_empty(&instance_table[st->bucket]))
971 return instance_table[st->bucket].first;
972 }
973 return NULL;
974}
975
976static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
977{
978 struct iter_state *st = seq->private;
979
980 h = h->next;
981 while (!h) {
982 if (++st->bucket >= INSTANCE_BUCKETS)
983 return NULL;
984
985 h = instance_table[st->bucket].first;
986 }
987 return h;
988}
989
990static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
991{
992 struct hlist_node *head;
993 head = get_first(seq);
994
995 if (head)
996 while (pos && (head = get_next(seq, head)))
997 pos--;
998 return pos ? NULL : head;
999}
1000
1001static void *seq_start(struct seq_file *seq, loff_t *pos)
1002{
1003 read_lock_bh(&instances_lock);
1004 return get_idx(seq, *pos);
1005}
1006
1007static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1008{
1009 (*pos)++;
1010 return get_next(s, v);
1011}
1012
1013static void seq_stop(struct seq_file *s, void *v)
1014{
1015 read_unlock_bh(&instances_lock);
1016}
1017
1018static int seq_show(struct seq_file *s, void *v)
1019{
1020 const struct nfqnl_instance *inst = v;
1021
1022 return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
1023 inst->queue_num,
1024 inst->peer_pid, inst->queue_total,
1025 inst->copy_mode, inst->copy_range,
1026 inst->queue_dropped, inst->queue_user_dropped,
1027 atomic_read(&inst->id_sequence),
1028 atomic_read(&inst->use));
1029}
1030
1031static struct seq_operations nfqnl_seq_ops = {
1032 .start = seq_start,
1033 .next = seq_next,
1034 .stop = seq_stop,
1035 .show = seq_show,
1036};
1037
1038static int nfqnl_open(struct inode *inode, struct file *file)
1039{
1040 struct seq_file *seq;
1041 struct iter_state *is;
1042 int ret;
1043
Harald Welte10dfdc62005-11-03 19:20:07 +01001044 is = kzalloc(sizeof(*is), GFP_KERNEL);
Harald Welte838ab632005-08-09 19:50:45 -07001045 if (!is)
1046 return -ENOMEM;
Harald Welte838ab632005-08-09 19:50:45 -07001047 ret = seq_open(file, &nfqnl_seq_ops);
1048 if (ret < 0)
1049 goto out_free;
1050 seq = file->private_data;
1051 seq->private = is;
1052 return ret;
1053out_free:
1054 kfree(is);
1055 return ret;
1056}
1057
1058static struct file_operations nfqnl_file_ops = {
1059 .owner = THIS_MODULE,
1060 .open = nfqnl_open,
1061 .read = seq_read,
1062 .llseek = seq_lseek,
1063 .release = seq_release_private,
1064};
1065
1066#endif /* PROC_FS */
1067
Harald Welte7af4cc32005-08-09 19:44:15 -07001068static int
1069init_or_cleanup(int init)
1070{
Harald Welte838ab632005-08-09 19:50:45 -07001071 int i, status = -ENOMEM;
1072#ifdef CONFIG_PROC_FS
1073 struct proc_dir_entry *proc_nfqueue;
1074#endif
Harald Welte7af4cc32005-08-09 19:44:15 -07001075
1076 if (!init)
1077 goto cleanup;
1078
Harald Welte838ab632005-08-09 19:50:45 -07001079 for (i = 0; i < INSTANCE_BUCKETS; i++)
1080 INIT_HLIST_HEAD(&instance_table[i]);
1081
Harald Welte7af4cc32005-08-09 19:44:15 -07001082 netlink_register_notifier(&nfqnl_rtnl_notifier);
1083 status = nfnetlink_subsys_register(&nfqnl_subsys);
1084 if (status < 0) {
1085 printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
1086 goto cleanup_netlink_notifier;
1087 }
1088
Harald Welte838ab632005-08-09 19:50:45 -07001089#ifdef CONFIG_PROC_FS
1090 proc_nfqueue = create_proc_entry("nfnetlink_queue", 0440,
1091 proc_net_netfilter);
1092 if (!proc_nfqueue)
1093 goto cleanup_subsys;
1094 proc_nfqueue->proc_fops = &nfqnl_file_ops;
1095#endif
1096
Harald Welte7af4cc32005-08-09 19:44:15 -07001097 register_netdevice_notifier(&nfqnl_dev_notifier);
Harald Welte838ab632005-08-09 19:50:45 -07001098
Harald Welte7af4cc32005-08-09 19:44:15 -07001099 return status;
1100
1101cleanup:
Harald Weltebbd86b9f2005-08-09 20:23:11 -07001102 nf_unregister_queue_handlers(&nfqh);
Harald Welte7af4cc32005-08-09 19:44:15 -07001103 unregister_netdevice_notifier(&nfqnl_dev_notifier);
Harald Welte838ab632005-08-09 19:50:45 -07001104#ifdef CONFIG_PROC_FS
Harald Welte0597f262005-08-09 19:58:39 -07001105 remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
Harald Welte838ab632005-08-09 19:50:45 -07001106cleanup_subsys:
1107#endif
Harald Welte7af4cc32005-08-09 19:44:15 -07001108 nfnetlink_subsys_unregister(&nfqnl_subsys);
Harald Welte7af4cc32005-08-09 19:44:15 -07001109cleanup_netlink_notifier:
1110 netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1111 return status;
1112}
1113
1114static int __init init(void)
1115{
1116
1117 return init_or_cleanup(1);
1118}
1119
1120static void __exit fini(void)
1121{
1122 init_or_cleanup(0);
1123}
1124
1125MODULE_DESCRIPTION("netfilter packet queue handler");
1126MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1127MODULE_LICENSE("GPL");
1128MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1129
1130module_init(init);
1131module_exit(fini);