2005-08-09 20:44:15 -06:00
|
|
|
/*
|
|
|
|
* This is a module which is used for queueing packets and communicating with
|
|
|
|
* userspace via nfetlink.
|
|
|
|
*
|
|
|
|
* (C) 2005 by Harald Welte <laforge@netfilter.org>
|
2007-12-05 02:31:17 -07:00
|
|
|
* (C) 2007 by Patrick McHardy <kaber@trash.net>
|
2005-08-09 20:44:15 -06:00
|
|
|
*
|
|
|
|
* Based on the old ipv4-only ip_queue.c:
|
|
|
|
* (C) 2000-2002 James Morris <jmorris@intercode.com.au>
|
|
|
|
* (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/notifier.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/netfilter.h>
|
2005-08-09 20:50:45 -06:00
|
|
|
#include <linux/proc_fs.h>
|
2005-08-09 20:44:15 -06:00
|
|
|
#include <linux/netfilter_ipv4.h>
|
|
|
|
#include <linux/netfilter_ipv6.h>
|
|
|
|
#include <linux/netfilter/nfnetlink.h>
|
|
|
|
#include <linux/netfilter/nfnetlink_queue.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <net/sock.h>
|
2007-12-05 02:24:48 -07:00
|
|
|
#include <net/netfilter/nf_queue.h>
|
2005-08-09 20:44:15 -06:00
|
|
|
|
|
|
|
#include <asm/atomic.h>
|
|
|
|
|
2005-08-09 21:22:10 -06:00
|
|
|
#ifdef CONFIG_BRIDGE_NETFILTER
|
|
|
|
#include "../bridge/br_private.h"
|
|
|
|
#endif
|
|
|
|
|
2005-08-09 20:44:15 -06:00
|
|
|
#define NFQNL_QMAX_DEFAULT 1024
|
|
|
|
|
|
|
|
struct nfqnl_instance {
|
|
|
|
struct hlist_node hlist; /* global list of queues */
|
2007-12-05 02:28:50 -07:00
|
|
|
struct rcu_head rcu;
|
2005-08-09 20:44:15 -06:00
|
|
|
|
|
|
|
int peer_pid;
|
|
|
|
unsigned int queue_maxlen;
|
|
|
|
unsigned int copy_range;
|
|
|
|
unsigned int queue_total;
|
|
|
|
unsigned int queue_dropped;
|
|
|
|
unsigned int queue_user_dropped;
|
|
|
|
|
2007-12-05 02:28:10 -07:00
|
|
|
unsigned int id_sequence; /* 'sequence' of pkt ids */
|
2005-08-09 20:44:15 -06:00
|
|
|
|
|
|
|
u_int16_t queue_num; /* number of this queue */
|
|
|
|
u_int8_t copy_mode;
|
|
|
|
|
|
|
|
spinlock_t lock;
|
|
|
|
|
|
|
|
struct list_head queue_list; /* packets in queue */
|
|
|
|
};
|
|
|
|
|
2007-12-05 02:26:33 -07:00
|
|
|
typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-12-05 02:28:50 -07:00
|
|
|
static DEFINE_SPINLOCK(instances_lock);
|
2005-08-09 20:44:15 -06:00
|
|
|
|
|
|
|
#define INSTANCE_BUCKETS 16
|
2007-12-05 02:29:38 -07:00
|
|
|
static struct hlist_head instance_table[INSTANCE_BUCKETS] __read_mostly;
|
2005-08-09 20:44:15 -06:00
|
|
|
|
|
|
|
static inline u_int8_t instance_hashfn(u_int16_t queue_num)
|
|
|
|
{
|
|
|
|
return ((queue_num >> 8) | queue_num) % INSTANCE_BUCKETS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct nfqnl_instance *
|
2007-12-05 02:28:50 -07:00
|
|
|
instance_lookup(u_int16_t queue_num)
|
2005-08-09 20:44:15 -06:00
|
|
|
{
|
|
|
|
struct hlist_head *head;
|
|
|
|
struct hlist_node *pos;
|
|
|
|
struct nfqnl_instance *inst;
|
|
|
|
|
|
|
|
head = &instance_table[instance_hashfn(queue_num)];
|
2007-12-05 02:28:50 -07:00
|
|
|
hlist_for_each_entry_rcu(inst, pos, head, hlist) {
|
2005-08-09 20:44:15 -06:00
|
|
|
if (inst->queue_num == queue_num)
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct nfqnl_instance *
|
|
|
|
instance_create(u_int16_t queue_num, int pid)
|
|
|
|
{
|
2007-12-17 23:41:21 -07:00
|
|
|
struct nfqnl_instance *inst;
|
2007-12-05 02:28:50 -07:00
|
|
|
unsigned int h;
|
2007-12-17 23:41:21 -07:00
|
|
|
int err;
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-12-05 02:28:50 -07:00
|
|
|
spin_lock(&instances_lock);
|
2007-12-17 23:41:21 -07:00
|
|
|
if (instance_lookup(queue_num)) {
|
|
|
|
err = -EEXIST;
|
2005-08-09 20:44:15 -06:00
|
|
|
goto out_unlock;
|
2007-12-17 23:41:21 -07:00
|
|
|
}
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2005-11-03 11:20:07 -07:00
|
|
|
inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
|
2007-12-17 23:41:21 -07:00
|
|
|
if (!inst) {
|
|
|
|
err = -ENOMEM;
|
2005-08-09 20:44:15 -06:00
|
|
|
goto out_unlock;
|
2007-12-17 23:41:21 -07:00
|
|
|
}
|
2005-08-09 20:44:15 -06:00
|
|
|
|
|
|
|
inst->queue_num = queue_num;
|
|
|
|
inst->peer_pid = pid;
|
|
|
|
inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
|
|
|
|
inst->copy_range = 0xfffff;
|
|
|
|
inst->copy_mode = NFQNL_COPY_NONE;
|
2006-01-04 14:56:54 -07:00
|
|
|
spin_lock_init(&inst->lock);
|
2005-08-09 20:44:15 -06:00
|
|
|
INIT_LIST_HEAD(&inst->queue_list);
|
2007-12-05 02:28:50 -07:00
|
|
|
INIT_RCU_HEAD(&inst->rcu);
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-12-17 23:41:21 -07:00
|
|
|
if (!try_module_get(THIS_MODULE)) {
|
|
|
|
err = -EAGAIN;
|
2005-08-09 20:44:15 -06:00
|
|
|
goto out_free;
|
2007-12-17 23:41:21 -07:00
|
|
|
}
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-12-05 02:28:50 -07:00
|
|
|
h = instance_hashfn(queue_num);
|
|
|
|
hlist_add_head_rcu(&inst->hlist, &instance_table[h]);
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-12-05 02:28:50 -07:00
|
|
|
spin_unlock(&instances_lock);
|
2005-08-09 20:44:15 -06:00
|
|
|
|
|
|
|
return inst;
|
|
|
|
|
|
|
|
out_free:
|
|
|
|
kfree(inst);
|
|
|
|
out_unlock:
|
2007-12-05 02:28:50 -07:00
|
|
|
spin_unlock(&instances_lock);
|
2007-12-17 23:41:21 -07:00
|
|
|
return ERR_PTR(err);
|
2005-08-09 20:44:15 -06:00
|
|
|
}
|
|
|
|
|
2007-12-05 02:25:30 -07:00
|
|
|
static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
|
|
|
|
unsigned long data);
|
2005-08-09 20:44:15 -06:00
|
|
|
|
|
|
|
static void
|
2007-12-05 02:28:50 -07:00
|
|
|
instance_destroy_rcu(struct rcu_head *head)
|
2005-08-09 20:44:15 -06:00
|
|
|
{
|
2007-12-05 02:28:50 -07:00
|
|
|
struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
|
|
|
|
rcu);
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-12-05 02:25:30 -07:00
|
|
|
nfqnl_flush(inst, NULL, 0);
|
2007-12-05 02:28:50 -07:00
|
|
|
kfree(inst);
|
2005-08-09 20:44:15 -06:00
|
|
|
module_put(THIS_MODULE);
|
|
|
|
}
|
|
|
|
|
2007-12-05 02:28:50 -07:00
|
|
|
static void
|
2005-08-09 20:44:15 -06:00
|
|
|
__instance_destroy(struct nfqnl_instance *inst)
|
|
|
|
{
|
2007-12-05 02:28:50 -07:00
|
|
|
hlist_del_rcu(&inst->hlist);
|
|
|
|
call_rcu(&inst->rcu, instance_destroy_rcu);
|
2005-08-09 20:44:15 -06:00
|
|
|
}
|
|
|
|
|
2007-12-05 02:28:50 -07:00
|
|
|
static void
|
2005-08-09 20:44:15 -06:00
|
|
|
instance_destroy(struct nfqnl_instance *inst)
|
|
|
|
{
|
2007-12-05 02:28:50 -07:00
|
|
|
spin_lock(&instances_lock);
|
|
|
|
__instance_destroy(inst);
|
|
|
|
spin_unlock(&instances_lock);
|
2005-08-09 20:44:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
2007-12-05 02:26:33 -07:00
|
|
|
__enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
|
2005-08-09 20:44:15 -06:00
|
|
|
{
|
2007-12-05 02:25:03 -07:00
|
|
|
list_add_tail(&entry->list, &queue->queue_list);
|
2005-08-09 20:44:15 -06:00
|
|
|
queue->queue_total++;
|
|
|
|
}
|
|
|
|
|
2007-12-05 02:26:33 -07:00
|
|
|
static struct nf_queue_entry *
|
2007-12-05 02:25:30 -07:00
|
|
|
find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
|
2005-08-09 20:44:15 -06:00
|
|
|
{
|
2007-12-05 02:26:33 -07:00
|
|
|
struct nf_queue_entry *entry = NULL, *i;
|
2007-02-12 12:15:49 -07:00
|
|
|
|
2005-08-09 20:44:15 -06:00
|
|
|
spin_lock_bh(&queue->lock);
|
2007-12-05 02:25:30 -07:00
|
|
|
|
|
|
|
list_for_each_entry(i, &queue->queue_list, list) {
|
|
|
|
if (i->id == id) {
|
|
|
|
entry = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry) {
|
|
|
|
list_del(&entry->list);
|
|
|
|
queue->queue_total--;
|
|
|
|
}
|
|
|
|
|
2005-08-09 20:44:15 -06:00
|
|
|
spin_unlock_bh(&queue->lock);
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-12-05 02:25:30 -07:00
|
|
|
nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
|
2005-08-09 20:44:15 -06:00
|
|
|
{
|
2007-12-05 02:26:33 -07:00
|
|
|
struct nf_queue_entry *entry, *next;
|
2007-12-05 02:25:30 -07:00
|
|
|
|
2005-08-09 20:44:15 -06:00
|
|
|
spin_lock_bh(&queue->lock);
|
2007-12-05 02:25:30 -07:00
|
|
|
list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
|
|
|
|
if (!cmpfn || cmpfn(entry, data)) {
|
|
|
|
list_del(&entry->list);
|
|
|
|
queue->queue_total--;
|
2007-12-05 02:27:02 -07:00
|
|
|
nf_reinject(entry, NF_DROP);
|
2007-12-05 02:25:30 -07:00
|
|
|
}
|
|
|
|
}
|
2005-08-09 20:44:15 -06:00
|
|
|
spin_unlock_bh(&queue->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct sk_buff *
|
|
|
|
nfqnl_build_packet_message(struct nfqnl_instance *queue,
|
2007-12-05 02:31:01 -07:00
|
|
|
struct nf_queue_entry *entry)
|
2005-08-09 20:44:15 -06:00
|
|
|
{
|
2007-04-19 21:29:13 -06:00
|
|
|
sk_buff_data_t old_tail;
|
2005-08-09 20:44:15 -06:00
|
|
|
size_t size;
|
|
|
|
size_t data_len = 0;
|
|
|
|
struct sk_buff *skb;
|
|
|
|
struct nfqnl_msg_packet_hdr pmsg;
|
|
|
|
struct nlmsghdr *nlh;
|
|
|
|
struct nfgenmsg *nfmsg;
|
2006-01-05 13:15:58 -07:00
|
|
|
struct sk_buff *entskb = entry->skb;
|
|
|
|
struct net_device *indev;
|
|
|
|
struct net_device *outdev;
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2008-03-10 17:41:43 -06:00
|
|
|
size = NLMSG_SPACE(sizeof(struct nfgenmsg))
|
2007-09-28 15:37:03 -06:00
|
|
|
+ nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
|
|
|
|
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */
|
|
|
|
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */
|
2005-08-09 21:22:10 -06:00
|
|
|
#ifdef CONFIG_BRIDGE_NETFILTER
|
2007-09-28 15:37:03 -06:00
|
|
|
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */
|
|
|
|
+ nla_total_size(sizeof(u_int32_t)) /* ifindex */
|
2005-08-09 21:22:10 -06:00
|
|
|
#endif
|
2007-09-28 15:37:03 -06:00
|
|
|
+ nla_total_size(sizeof(u_int32_t)) /* mark */
|
|
|
|
+ nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
|
|
|
|
+ nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-12-05 02:26:33 -07:00
|
|
|
outdev = entry->outdev;
|
2006-01-05 13:15:58 -07:00
|
|
|
|
2005-08-09 20:44:15 -06:00
|
|
|
spin_lock_bh(&queue->lock);
|
2007-02-12 12:15:49 -07:00
|
|
|
|
2007-12-05 02:30:29 -07:00
|
|
|
switch ((enum nfqnl_config_mode)queue->copy_mode) {
|
2005-08-09 20:44:15 -06:00
|
|
|
case NFQNL_COPY_META:
|
|
|
|
case NFQNL_COPY_NONE:
|
|
|
|
break;
|
2007-02-12 12:15:49 -07:00
|
|
|
|
2005-08-09 20:44:15 -06:00
|
|
|
case NFQNL_COPY_PACKET:
|
2006-08-29 17:44:56 -06:00
|
|
|
if ((entskb->ip_summed == CHECKSUM_PARTIAL ||
|
|
|
|
entskb->ip_summed == CHECKSUM_COMPLETE) &&
|
2007-12-05 02:31:01 -07:00
|
|
|
skb_checksum_help(entskb)) {
|
2005-09-06 16:10:00 -06:00
|
|
|
spin_unlock_bh(&queue->lock);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-02-12 12:15:49 -07:00
|
|
|
if (queue->copy_range == 0
|
2006-01-05 13:15:58 -07:00
|
|
|
|| queue->copy_range > entskb->len)
|
|
|
|
data_len = entskb->len;
|
2005-08-09 20:44:15 -06:00
|
|
|
else
|
|
|
|
data_len = queue->copy_range;
|
2007-02-12 12:15:49 -07:00
|
|
|
|
2007-09-28 15:37:03 -06:00
|
|
|
size += nla_total_size(data_len);
|
2005-08-09 20:44:15 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-12-05 02:28:10 -07:00
|
|
|
entry->id = queue->id_sequence++;
|
|
|
|
|
2005-08-09 20:44:15 -06:00
|
|
|
spin_unlock_bh(&queue->lock);
|
|
|
|
|
|
|
|
skb = alloc_skb(size, GFP_ATOMIC);
|
|
|
|
if (!skb)
|
|
|
|
goto nlmsg_failure;
|
2007-02-12 12:15:49 -07:00
|
|
|
|
2007-04-19 21:29:13 -06:00
|
|
|
old_tail = skb->tail;
|
2007-02-12 12:15:49 -07:00
|
|
|
nlh = NLMSG_PUT(skb, 0, 0,
|
2005-08-09 20:44:15 -06:00
|
|
|
NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
|
|
|
|
sizeof(struct nfgenmsg));
|
|
|
|
nfmsg = NLMSG_DATA(nlh);
|
2007-12-05 02:26:33 -07:00
|
|
|
nfmsg->nfgen_family = entry->pf;
|
2005-08-09 20:44:15 -06:00
|
|
|
nfmsg->version = NFNETLINK_V0;
|
|
|
|
nfmsg->res_id = htons(queue->queue_num);
|
|
|
|
|
|
|
|
pmsg.packet_id = htonl(entry->id);
|
2006-11-03 01:59:17 -07:00
|
|
|
pmsg.hw_protocol = entskb->protocol;
|
2007-12-05 02:26:33 -07:00
|
|
|
pmsg.hook = entry->hook;
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-09-28 15:37:03 -06:00
|
|
|
NLA_PUT(skb, NFQA_PACKET_HDR, sizeof(pmsg), &pmsg);
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-12-05 02:26:33 -07:00
|
|
|
indev = entry->indev;
|
2006-01-05 13:15:58 -07:00
|
|
|
if (indev) {
|
2005-08-09 21:22:10 -06:00
|
|
|
#ifndef CONFIG_BRIDGE_NETFILTER
|
2007-12-05 02:30:02 -07:00
|
|
|
NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex));
|
2005-08-09 21:22:10 -06:00
|
|
|
#else
|
2007-12-05 02:26:33 -07:00
|
|
|
if (entry->pf == PF_BRIDGE) {
|
2005-08-09 21:22:10 -06:00
|
|
|
/* Case 1: indev is physical input device, we need to
|
2007-02-12 12:15:49 -07:00
|
|
|
* look for bridge group (when called from
|
2005-08-09 21:22:10 -06:00
|
|
|
* netfilter_bridge) */
|
2007-12-05 02:30:02 -07:00
|
|
|
NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSINDEV,
|
|
|
|
htonl(indev->ifindex));
|
2005-08-09 21:22:10 -06:00
|
|
|
/* this is the bridge group "brX" */
|
2007-12-05 02:30:02 -07:00
|
|
|
NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV,
|
|
|
|
htonl(indev->br_port->br->dev->ifindex));
|
2005-08-09 21:22:10 -06:00
|
|
|
} else {
|
|
|
|
/* Case 2: indev is bridge group, we need to look for
|
|
|
|
* physical device (when called from ipv4) */
|
2007-12-05 02:30:02 -07:00
|
|
|
NLA_PUT_BE32(skb, NFQA_IFINDEX_INDEV,
|
|
|
|
htonl(indev->ifindex));
|
|
|
|
if (entskb->nf_bridge && entskb->nf_bridge->physindev)
|
|
|
|
NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSINDEV,
|
|
|
|
htonl(entskb->nf_bridge->physindev->ifindex));
|
2005-08-09 21:22:10 -06:00
|
|
|
}
|
|
|
|
#endif
|
2005-08-09 20:44:15 -06:00
|
|
|
}
|
|
|
|
|
2006-01-05 13:15:58 -07:00
|
|
|
if (outdev) {
|
2005-08-09 21:22:10 -06:00
|
|
|
#ifndef CONFIG_BRIDGE_NETFILTER
|
2007-12-05 02:30:02 -07:00
|
|
|
NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex));
|
2005-08-09 21:22:10 -06:00
|
|
|
#else
|
2007-12-05 02:26:33 -07:00
|
|
|
if (entry->pf == PF_BRIDGE) {
|
2005-08-09 21:22:10 -06:00
|
|
|
/* Case 1: outdev is physical output device, we need to
|
2007-02-12 12:15:49 -07:00
|
|
|
* look for bridge group (when called from
|
2005-08-09 21:22:10 -06:00
|
|
|
* netfilter_bridge) */
|
2007-12-05 02:30:02 -07:00
|
|
|
NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSOUTDEV,
|
|
|
|
htonl(outdev->ifindex));
|
2005-08-09 21:22:10 -06:00
|
|
|
/* this is the bridge group "brX" */
|
2007-12-05 02:30:02 -07:00
|
|
|
NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV,
|
|
|
|
htonl(outdev->br_port->br->dev->ifindex));
|
2005-08-09 21:22:10 -06:00
|
|
|
} else {
|
|
|
|
/* Case 2: outdev is bridge group, we need to look for
|
|
|
|
* physical output device (when called from ipv4) */
|
2007-12-05 02:30:02 -07:00
|
|
|
NLA_PUT_BE32(skb, NFQA_IFINDEX_OUTDEV,
|
|
|
|
htonl(outdev->ifindex));
|
|
|
|
if (entskb->nf_bridge && entskb->nf_bridge->physoutdev)
|
|
|
|
NLA_PUT_BE32(skb, NFQA_IFINDEX_PHYSOUTDEV,
|
|
|
|
htonl(entskb->nf_bridge->physoutdev->ifindex));
|
2005-08-09 21:22:10 -06:00
|
|
|
}
|
|
|
|
#endif
|
2005-08-09 20:44:15 -06:00
|
|
|
}
|
|
|
|
|
2007-12-05 02:30:02 -07:00
|
|
|
if (entskb->mark)
|
|
|
|
NLA_PUT_BE32(skb, NFQA_MARK, htonl(entskb->mark));
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-09-26 23:13:38 -06:00
|
|
|
if (indev && entskb->dev) {
|
2005-08-09 20:44:15 -06:00
|
|
|
struct nfqnl_msg_packet_hw phw;
|
2007-09-26 23:13:38 -06:00
|
|
|
int len = dev_parse_header(entskb, phw.hw_addr);
|
|
|
|
if (len) {
|
|
|
|
phw.hw_addrlen = htons(len);
|
2007-09-28 15:37:03 -06:00
|
|
|
NLA_PUT(skb, NFQA_HWADDR, sizeof(phw), &phw);
|
2007-09-26 23:13:38 -06:00
|
|
|
}
|
2005-08-09 20:44:15 -06:00
|
|
|
}
|
|
|
|
|
2007-04-19 17:16:32 -06:00
|
|
|
if (entskb->tstamp.tv64) {
|
2005-08-09 20:44:15 -06:00
|
|
|
struct nfqnl_msg_packet_timestamp ts;
|
2007-04-19 17:16:32 -06:00
|
|
|
struct timeval tv = ktime_to_timeval(entskb->tstamp);
|
|
|
|
ts.sec = cpu_to_be64(tv.tv_sec);
|
|
|
|
ts.usec = cpu_to_be64(tv.tv_usec);
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-09-28 15:37:03 -06:00
|
|
|
NLA_PUT(skb, NFQA_TIMESTAMP, sizeof(ts), &ts);
|
2005-08-09 20:44:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (data_len) {
|
2007-09-28 15:37:03 -06:00
|
|
|
struct nlattr *nla;
|
2008-01-31 04:53:27 -07:00
|
|
|
int sz = nla_attr_size(data_len);
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-09-28 15:37:03 -06:00
|
|
|
if (skb_tailroom(skb) < nla_total_size(data_len)) {
|
2005-08-09 20:44:15 -06:00
|
|
|
printk(KERN_WARNING "nf_queue: no tailroom!\n");
|
|
|
|
goto nlmsg_failure;
|
|
|
|
}
|
|
|
|
|
2007-09-28 15:37:03 -06:00
|
|
|
nla = (struct nlattr *)skb_put(skb, nla_total_size(data_len));
|
|
|
|
nla->nla_type = NFQA_PAYLOAD;
|
2008-01-31 04:53:27 -07:00
|
|
|
nla->nla_len = sz;
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-09-28 15:37:03 -06:00
|
|
|
if (skb_copy_bits(entskb, 0, nla_data(nla), data_len))
|
2005-08-09 20:44:15 -06:00
|
|
|
BUG();
|
|
|
|
}
|
2007-02-12 12:15:49 -07:00
|
|
|
|
2005-08-09 20:44:15 -06:00
|
|
|
nlh->nlmsg_len = skb->tail - old_tail;
|
|
|
|
return skb;
|
|
|
|
|
|
|
|
nlmsg_failure:
|
2007-09-28 15:37:03 -06:00
|
|
|
nla_put_failure:
|
2005-08-09 20:44:15 -06:00
|
|
|
if (skb)
|
|
|
|
kfree_skb(skb);
|
|
|
|
if (net_ratelimit())
|
|
|
|
printk(KERN_ERR "nf_queue: error creating packet message\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-12-05 02:26:33 -07:00
|
|
|
nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
|
2005-08-09 20:44:15 -06:00
|
|
|
{
|
|
|
|
struct sk_buff *nskb;
|
|
|
|
struct nfqnl_instance *queue;
|
2007-12-05 02:31:01 -07:00
|
|
|
int err;
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-12-05 02:28:50 -07:00
|
|
|
/* rcu_read_lock()ed by nf_hook_slow() */
|
|
|
|
queue = instance_lookup(queuenum);
|
2007-12-05 02:29:23 -07:00
|
|
|
if (!queue)
|
2007-12-05 02:31:01 -07:00
|
|
|
goto err_out;
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-12-05 02:29:23 -07:00
|
|
|
if (queue->copy_mode == NFQNL_COPY_NONE)
|
2007-12-05 02:31:01 -07:00
|
|
|
goto err_out;
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-12-05 02:31:01 -07:00
|
|
|
nskb = nfqnl_build_packet_message(queue, entry);
|
2005-08-09 20:44:15 -06:00
|
|
|
if (nskb == NULL)
|
2007-12-05 02:31:01 -07:00
|
|
|
goto err_out;
|
2007-02-12 12:15:49 -07:00
|
|
|
|
2005-08-09 20:44:15 -06:00
|
|
|
spin_lock_bh(&queue->lock);
|
2007-02-12 12:15:49 -07:00
|
|
|
|
2005-08-09 20:44:15 -06:00
|
|
|
if (!queue->peer_pid)
|
2007-02-12 12:15:49 -07:00
|
|
|
goto err_out_free_nskb;
|
2005-08-09 20:44:15 -06:00
|
|
|
|
|
|
|
if (queue->queue_total >= queue->queue_maxlen) {
|
2007-02-12 12:15:49 -07:00
|
|
|
queue->queue_dropped++;
|
2005-08-09 20:44:15 -06:00
|
|
|
if (net_ratelimit())
|
2007-02-12 12:15:49 -07:00
|
|
|
printk(KERN_WARNING "nf_queue: full at %d entries, "
|
|
|
|
"dropping packets(s). Dropped: %d\n",
|
2005-08-09 20:44:15 -06:00
|
|
|
queue->queue_total, queue->queue_dropped);
|
|
|
|
goto err_out_free_nskb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* nfnetlink_unicast will either free the nskb or add it to a socket */
|
2007-12-05 02:31:01 -07:00
|
|
|
err = nfnetlink_unicast(nskb, queue->peer_pid, MSG_DONTWAIT);
|
|
|
|
if (err < 0) {
|
2007-02-12 12:15:49 -07:00
|
|
|
queue->queue_user_dropped++;
|
2005-08-09 20:44:15 -06:00
|
|
|
goto err_out_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
__enqueue_entry(queue, entry);
|
|
|
|
|
|
|
|
spin_unlock_bh(&queue->lock);
|
2007-12-05 02:31:01 -07:00
|
|
|
return 0;
|
2005-08-09 20:44:15 -06:00
|
|
|
|
|
|
|
err_out_free_nskb:
|
2007-02-12 12:15:49 -07:00
|
|
|
kfree_skb(nskb);
|
2005-08-09 20:44:15 -06:00
|
|
|
err_out_unlock:
|
|
|
|
spin_unlock_bh(&queue->lock);
|
2007-12-05 02:31:01 -07:00
|
|
|
err_out:
|
|
|
|
return -1;
|
2005-08-09 20:44:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-12-05 02:26:33 -07:00
|
|
|
nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e)
|
2005-08-09 20:44:15 -06:00
|
|
|
{
|
2008-02-19 18:17:52 -07:00
|
|
|
struct sk_buff *nskb;
|
2005-08-09 20:44:15 -06:00
|
|
|
int diff;
|
|
|
|
|
|
|
|
diff = data_len - e->skb->len;
|
2006-11-14 20:48:09 -07:00
|
|
|
if (diff < 0) {
|
|
|
|
if (pskb_trim(e->skb, data_len))
|
|
|
|
return -ENOMEM;
|
|
|
|
} else if (diff > 0) {
|
2005-08-09 20:44:15 -06:00
|
|
|
if (data_len > 0xFFFF)
|
|
|
|
return -EINVAL;
|
|
|
|
if (diff > skb_tailroom(e->skb)) {
|
netfilter: {nfnetlink,ip,ip6}_queue: fix skb_over_panic when enlarging packets
While reinjecting *bigger* modified versions of IPv6 packets using
libnetfilter_queue, things work fine on a 2.6.24 kernel (2.6.22 too)
but I get the following on recents kernels (2.6.25, trace below is
against today's net-2.6 git tree):
skb_over_panic: text:c04fddb0 len:696 put:632 head:f7592c00 data:f7592c00 tail:0xf7592eb8 end:0xf7592e80 dev:eth0
------------[ cut here ]------------
invalid opcode: 0000 [#1] PREEMPT
Process sendd (pid: 3657, ti=f6014000 task=f77c31d0 task.ti=f6014000)
Stack: c071e638 c04fddb0 000002b8 00000278 f7592c00 f7592c00 f7592eb8 f7592e80
f763c000 f6bc5200 f7592c40 f6015c34 c04cdbfc f6bc5200 00000278 f6015c60
c04fddb0 00000020 f72a10c0 f751b420 00000001 0000000a 000002b8 c065582c
Call Trace:
[<c04fddb0>] ? nfqnl_recv_verdict+0x1c0/0x2e0
[<c04cdbfc>] ? skb_put+0x3c/0x40
[<c04fddb0>] ? nfqnl_recv_verdict+0x1c0/0x2e0
[<c04fd115>] ? nfnetlink_rcv_msg+0xf5/0x160
[<c04fd03e>] ? nfnetlink_rcv_msg+0x1e/0x160
[<c04fd020>] ? nfnetlink_rcv_msg+0x0/0x160
[<c04f8ed7>] ? netlink_rcv_skb+0x77/0xa0
[<c04fcefc>] ? nfnetlink_rcv+0x1c/0x30
[<c04f8c73>] ? netlink_unicast+0x243/0x2b0
[<c04cfaba>] ? memcpy_fromiovec+0x4a/0x70
[<c04f9406>] ? netlink_sendmsg+0x1c6/0x270
[<c04c8244>] ? sock_sendmsg+0xc4/0xf0
[<c011970d>] ? set_next_entity+0x1d/0x50
[<c0133a80>] ? autoremove_wake_function+0x0/0x40
[<c0118f9e>] ? __wake_up_common+0x3e/0x70
[<c0342fbf>] ? n_tty_receive_buf+0x34f/0x1280
[<c011d308>] ? __wake_up+0x68/0x70
[<c02cea47>] ? copy_from_user+0x37/0x70
[<c04cfd7c>] ? verify_iovec+0x2c/0x90
[<c04c837a>] ? sys_sendmsg+0x10a/0x230
[<c011967a>] ? __dequeue_entity+0x2a/0xa0
[<c011970d>] ? set_next_entity+0x1d/0x50
[<c0345397>] ? pty_write+0x47/0x60
[<c033d59b>] ? tty_default_put_char+0x1b/0x20
[<c011d2e9>] ? __wake_up+0x49/0x70
[<c033df99>] ? tty_ldisc_deref+0x39/0x90
[<c033ff20>] ? tty_write+0x1a0/0x1b0
[<c04c93af>] ? sys_socketcall+0x7f/0x260
[<c0102ff9>] ? sysenter_past_esp+0x6a/0x91
[<c05f0000>] ? snd_intel8x0m_probe+0x270/0x6e0
=======================
Code: 00 00 89 5c 24 14 8b 98 9c 00 00 00 89 54 24 0c 89 5c 24 10 8b 40 50 89 4c 24 04 c7 04 24 38 e6 71 c0 89 44 24 08 e8 c4 46 c5 ff <0f> 0b eb fe 55 89 e5 56 89 d6 53 89 c3 83 ec 0c 8b 40 50 39 d0
EIP: [<c04ccdfc>] skb_over_panic+0x5c/0x60 SS:ESP 0068:f6015bf8
Looking at the code, I ended up in nfq_mangle() function (called by
nfqnl_recv_verdict()) which performs a call to skb_copy_expand() due to
the increased size of data passed to the function. AFAICT, it should ask
for 'diff' instead of 'diff - skb_tailroom(e->skb)'. Because the
resulting sk_buff has not enough space to support the skb_put(skb, diff)
call a few lines later, this results in the call to skb_over_panic().
The patch below asks for allocation of a copy with enough space for
mangled packet and the same amount of headroom as old sk_buff. While
looking at how the regression appeared (e2b58a67), I noticed the same
pattern in ipq_mangle_ipv6() and ipq_mangle_ipv4(). The patch corrects
those locations too.
Tested with bigger reinjected IPv6 packets (nfqnl_mangle() path), things
are ok (2.6.25 and today's net-2.6 git tree).
Signed-off-by: Arnaud Ebalard <arno@natisbad.org>
Signed-off-by: Patrick McHardy <kaber@trash.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
2008-04-29 04:16:34 -06:00
|
|
|
nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
|
|
|
|
diff, GFP_ATOMIC);
|
2008-02-19 18:17:52 -07:00
|
|
|
if (!nskb) {
|
2006-08-22 01:32:47 -06:00
|
|
|
printk(KERN_WARNING "nf_queue: OOM "
|
2005-08-09 20:44:15 -06:00
|
|
|
"in mangle, dropping packet\n");
|
2008-02-19 18:17:52 -07:00
|
|
|
return -ENOMEM;
|
2005-08-09 20:44:15 -06:00
|
|
|
}
|
2008-02-19 18:17:52 -07:00
|
|
|
kfree_skb(e->skb);
|
|
|
|
e->skb = nskb;
|
2005-08-09 20:44:15 -06:00
|
|
|
}
|
|
|
|
skb_put(e->skb, diff);
|
|
|
|
}
|
2007-10-14 01:39:18 -06:00
|
|
|
if (!skb_make_writable(e->skb, data_len))
|
2005-08-09 20:44:15 -06:00
|
|
|
return -ENOMEM;
|
2007-03-31 08:55:19 -06:00
|
|
|
skb_copy_to_linear_data(e->skb, data, data_len);
|
2005-09-06 16:10:00 -06:00
|
|
|
e->skb->ip_summed = CHECKSUM_NONE;
|
2005-08-09 20:44:15 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfqnl_set_mode(struct nfqnl_instance *queue,
|
|
|
|
unsigned char mode, unsigned int range)
|
|
|
|
{
|
2007-12-05 02:29:05 -07:00
|
|
|
int status = 0;
|
2005-08-09 20:44:15 -06:00
|
|
|
|
|
|
|
spin_lock_bh(&queue->lock);
|
2007-12-05 02:29:05 -07:00
|
|
|
switch (mode) {
|
|
|
|
case NFQNL_COPY_NONE:
|
|
|
|
case NFQNL_COPY_META:
|
|
|
|
queue->copy_mode = mode;
|
|
|
|
queue->copy_range = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NFQNL_COPY_PACKET:
|
|
|
|
queue->copy_mode = mode;
|
|
|
|
/* we're using struct nlattr which has 16bit nla_len */
|
|
|
|
if (range > 0xffff)
|
|
|
|
queue->copy_range = 0xffff;
|
|
|
|
else
|
|
|
|
queue->copy_range = range;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
status = -EINVAL;
|
|
|
|
|
|
|
|
}
|
2005-08-09 20:44:15 -06:00
|
|
|
spin_unlock_bh(&queue->lock);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-12-05 02:26:33 -07:00
|
|
|
dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
|
2005-08-09 20:44:15 -06:00
|
|
|
{
|
2007-12-05 02:26:33 -07:00
|
|
|
if (entry->indev)
|
|
|
|
if (entry->indev->ifindex == ifindex)
|
2005-08-09 20:44:15 -06:00
|
|
|
return 1;
|
2007-12-05 02:26:33 -07:00
|
|
|
if (entry->outdev)
|
|
|
|
if (entry->outdev->ifindex == ifindex)
|
2005-08-09 20:44:15 -06:00
|
|
|
return 1;
|
2006-06-27 04:01:48 -06:00
|
|
|
#ifdef CONFIG_BRIDGE_NETFILTER
|
|
|
|
if (entry->skb->nf_bridge) {
|
|
|
|
if (entry->skb->nf_bridge->physindev &&
|
|
|
|
entry->skb->nf_bridge->physindev->ifindex == ifindex)
|
|
|
|
return 1;
|
|
|
|
if (entry->skb->nf_bridge->physoutdev &&
|
|
|
|
entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
2005-08-09 20:44:15 -06:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* drop all packets with either indev or outdev == ifindex from all queue
|
|
|
|
* instances */
|
|
|
|
static void
|
|
|
|
nfqnl_dev_drop(int ifindex)
|
|
|
|
{
|
|
|
|
int i;
|
2007-02-12 12:15:49 -07:00
|
|
|
|
2007-12-05 02:28:50 -07:00
|
|
|
rcu_read_lock();
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-12-05 02:28:50 -07:00
|
|
|
for (i = 0; i < INSTANCE_BUCKETS; i++) {
|
2005-08-09 20:44:15 -06:00
|
|
|
struct hlist_node *tmp;
|
|
|
|
struct nfqnl_instance *inst;
|
|
|
|
struct hlist_head *head = &instance_table[i];
|
|
|
|
|
2007-12-05 02:28:50 -07:00
|
|
|
hlist_for_each_entry_rcu(inst, tmp, head, hlist)
|
2007-12-05 02:25:30 -07:00
|
|
|
nfqnl_flush(inst, dev_cmp, ifindex);
|
2005-08-09 20:44:15 -06:00
|
|
|
}
|
|
|
|
|
2007-12-05 02:28:50 -07:00
|
|
|
rcu_read_unlock();
|
2005-08-09 20:44:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfqnl_rcv_dev_event(struct notifier_block *this,
|
|
|
|
unsigned long event, void *ptr)
|
|
|
|
{
|
|
|
|
struct net_device *dev = ptr;
|
|
|
|
|
2008-03-25 06:47:49 -06:00
|
|
|
if (dev_net(dev) != &init_net)
|
2007-09-12 05:02:17 -06:00
|
|
|
return NOTIFY_DONE;
|
|
|
|
|
2005-08-09 20:44:15 -06:00
|
|
|
/* Drop any packets associated with the downed device */
|
|
|
|
if (event == NETDEV_DOWN)
|
|
|
|
nfqnl_dev_drop(dev->ifindex);
|
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct notifier_block nfqnl_dev_notifier = {
|
|
|
|
.notifier_call = nfqnl_rcv_dev_event,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfqnl_rcv_nl_event(struct notifier_block *this,
|
|
|
|
unsigned long event, void *ptr)
|
|
|
|
{
|
|
|
|
struct netlink_notify *n = ptr;
|
|
|
|
|
|
|
|
if (event == NETLINK_URELEASE &&
|
|
|
|
n->protocol == NETLINK_NETFILTER && n->pid) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* destroy all instances for this pid */
|
2007-12-05 02:28:50 -07:00
|
|
|
spin_lock(&instances_lock);
|
|
|
|
for (i = 0; i < INSTANCE_BUCKETS; i++) {
|
2005-08-09 20:44:15 -06:00
|
|
|
struct hlist_node *tmp, *t2;
|
|
|
|
struct nfqnl_instance *inst;
|
|
|
|
struct hlist_head *head = &instance_table[i];
|
|
|
|
|
|
|
|
hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
|
2007-09-12 05:05:38 -06:00
|
|
|
if ((n->net == &init_net) &&
|
|
|
|
(n->pid == inst->peer_pid))
|
2005-08-09 20:44:15 -06:00
|
|
|
__instance_destroy(inst);
|
|
|
|
}
|
|
|
|
}
|
2007-12-05 02:28:50 -07:00
|
|
|
spin_unlock(&instances_lock);
|
2005-08-09 20:44:15 -06:00
|
|
|
}
|
|
|
|
return NOTIFY_DONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct notifier_block nfqnl_rtnl_notifier = {
|
|
|
|
.notifier_call = nfqnl_rcv_nl_event,
|
|
|
|
};
|
|
|
|
|
2007-09-28 15:39:26 -06:00
|
|
|
static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
|
|
|
|
[NFQA_VERDICT_HDR] = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
|
|
|
|
[NFQA_MARK] = { .type = NLA_U32 },
|
|
|
|
[NFQA_PAYLOAD] = { .type = NLA_UNSPEC },
|
2005-08-09 20:50:45 -06:00
|
|
|
};
|
|
|
|
|
2005-08-09 20:44:15 -06:00
|
|
|
static int
|
|
|
|
nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
|
2007-09-28 15:37:03 -06:00
|
|
|
struct nlmsghdr *nlh, struct nlattr *nfqa[])
|
2005-08-09 20:44:15 -06:00
|
|
|
{
|
|
|
|
struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
|
|
|
|
u_int16_t queue_num = ntohs(nfmsg->res_id);
|
|
|
|
|
|
|
|
struct nfqnl_msg_verdict_hdr *vhdr;
|
|
|
|
struct nfqnl_instance *queue;
|
|
|
|
unsigned int verdict;
|
2007-12-05 02:26:33 -07:00
|
|
|
struct nf_queue_entry *entry;
|
2005-08-09 20:50:45 -06:00
|
|
|
int err;
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-12-05 02:28:50 -07:00
|
|
|
rcu_read_lock();
|
|
|
|
queue = instance_lookup(queue_num);
|
|
|
|
if (!queue) {
|
|
|
|
err = -ENODEV;
|
|
|
|
goto err_out_unlock;
|
|
|
|
}
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2005-08-09 20:50:45 -06:00
|
|
|
if (queue->peer_pid != NETLINK_CB(skb).pid) {
|
|
|
|
err = -EPERM;
|
2007-12-05 02:28:50 -07:00
|
|
|
goto err_out_unlock;
|
2005-08-09 20:50:45 -06:00
|
|
|
}
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-09-28 15:37:03 -06:00
|
|
|
if (!nfqa[NFQA_VERDICT_HDR]) {
|
2005-08-09 20:50:45 -06:00
|
|
|
err = -EINVAL;
|
2007-12-05 02:28:50 -07:00
|
|
|
goto err_out_unlock;
|
2005-08-09 20:50:45 -06:00
|
|
|
}
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-09-28 15:37:03 -06:00
|
|
|
vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
|
2005-08-09 20:44:15 -06:00
|
|
|
verdict = ntohl(vhdr->verdict);
|
|
|
|
|
2005-08-09 20:50:45 -06:00
|
|
|
if ((verdict & NF_VERDICT_MASK) > NF_MAX_VERDICT) {
|
|
|
|
err = -EINVAL;
|
2007-12-05 02:28:50 -07:00
|
|
|
goto err_out_unlock;
|
2005-08-09 20:50:45 -06:00
|
|
|
}
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-12-05 02:25:30 -07:00
|
|
|
entry = find_dequeue_entry(queue, ntohl(vhdr->id));
|
2005-08-09 20:50:45 -06:00
|
|
|
if (entry == NULL) {
|
|
|
|
err = -ENOENT;
|
2007-12-05 02:28:50 -07:00
|
|
|
goto err_out_unlock;
|
2005-08-09 20:50:45 -06:00
|
|
|
}
|
2007-12-05 02:28:50 -07:00
|
|
|
rcu_read_unlock();
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-09-28 15:37:03 -06:00
|
|
|
if (nfqa[NFQA_PAYLOAD]) {
|
|
|
|
if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
|
|
|
|
nla_len(nfqa[NFQA_PAYLOAD]), entry) < 0)
|
2005-08-09 20:44:15 -06:00
|
|
|
verdict = NF_DROP;
|
|
|
|
}
|
|
|
|
|
2007-09-28 15:37:03 -06:00
|
|
|
if (nfqa[NFQA_MARK])
|
2007-12-05 02:30:02 -07:00
|
|
|
entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
|
2007-02-12 12:15:49 -07:00
|
|
|
|
2007-12-05 02:27:02 -07:00
|
|
|
nf_reinject(entry, verdict);
|
2005-08-09 20:44:15 -06:00
|
|
|
return 0;
|
2005-08-09 20:50:45 -06:00
|
|
|
|
2007-12-05 02:28:50 -07:00
|
|
|
err_out_unlock:
|
|
|
|
rcu_read_unlock();
|
2005-08-09 20:50:45 -06:00
|
|
|
return err;
|
2005-08-09 20:44:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
|
2007-09-28 15:37:03 -06:00
|
|
|
struct nlmsghdr *nlh, struct nlattr *nfqa[])
|
2005-08-09 20:44:15 -06:00
|
|
|
{
|
|
|
|
return -ENOTSUPP;
|
|
|
|
}
|
|
|
|
|
2007-09-28 15:39:26 -06:00
|
|
|
static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
|
|
|
|
[NFQA_CFG_CMD] = { .len = sizeof(struct nfqnl_msg_config_cmd) },
|
|
|
|
[NFQA_CFG_PARAMS] = { .len = sizeof(struct nfqnl_msg_config_params) },
|
2005-08-09 20:50:45 -06:00
|
|
|
};
|
|
|
|
|
2007-12-05 02:23:57 -07:00
|
|
|
static const struct nf_queue_handler nfqh = {
|
2005-08-09 21:23:11 -06:00
|
|
|
.name = "nf_queue",
|
|
|
|
.outfn = &nfqnl_enqueue_packet,
|
|
|
|
};
|
|
|
|
|
2005-08-09 20:44:15 -06:00
|
|
|
static int
|
|
|
|
nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
|
2007-09-28 15:37:03 -06:00
|
|
|
struct nlmsghdr *nlh, struct nlattr *nfqa[])
|
2005-08-09 20:44:15 -06:00
|
|
|
{
|
|
|
|
struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
|
|
|
|
u_int16_t queue_num = ntohs(nfmsg->res_id);
|
|
|
|
struct nfqnl_instance *queue;
|
2007-12-05 02:28:50 -07:00
|
|
|
struct nfqnl_msg_config_cmd *cmd = NULL;
|
2005-08-09 20:50:45 -06:00
|
|
|
int ret = 0;
|
2005-08-09 20:44:15 -06:00
|
|
|
|
2007-12-05 02:28:50 -07:00
|
|
|
if (nfqa[NFQA_CFG_CMD]) {
|
|
|
|
cmd = nla_data(nfqa[NFQA_CFG_CMD]);
|
|
|
|
|
|
|
|
/* Commands without queue context - might sleep */
|
|
|
|
switch (cmd->command) {
|
|
|
|
case NFQNL_CFG_CMD_PF_BIND:
|
2008-03-10 17:44:36 -06:00
|
|
|
return nf_register_queue_handler(ntohs(cmd->pf),
|
|
|
|
&nfqh);
|
2007-12-05 02:28:50 -07:00
|
|
|
case NFQNL_CFG_CMD_PF_UNBIND:
|
2008-03-10 17:44:36 -06:00
|
|
|
return nf_unregister_queue_handler(ntohs(cmd->pf),
|
|
|
|
&nfqh);
|
2007-12-05 02:28:50 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
queue = instance_lookup(queue_num);
|
2007-12-05 02:28:30 -07:00
|
|
|
if (queue && queue->peer_pid != NETLINK_CB(skb).pid) {
|
|
|
|
ret = -EPERM;
|
2007-12-05 02:28:50 -07:00
|
|
|
goto err_out_unlock;
|
2007-12-05 02:28:30 -07:00
|
|
|
}
|
|
|
|
|
2007-12-05 02:28:50 -07:00
|
|
|
if (cmd != NULL) {
|
2005-08-09 20:44:15 -06:00
|
|
|
switch (cmd->command) {
|
|
|
|
case NFQNL_CFG_CMD_BIND:
|
2007-12-05 02:28:50 -07:00
|
|
|
if (queue) {
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto err_out_unlock;
|
|
|
|
}
|
2005-08-09 20:44:15 -06:00
|
|
|
queue = instance_create(queue_num, NETLINK_CB(skb).pid);
|
2007-12-17 23:41:21 -07:00
|
|
|
if (IS_ERR(queue)) {
|
|
|
|
ret = PTR_ERR(queue);
|
2007-12-05 02:28:50 -07:00
|
|
|
goto err_out_unlock;
|
|
|
|
}
|
2005-08-09 20:44:15 -06:00
|
|
|
break;
|
|
|
|
case NFQNL_CFG_CMD_UNBIND:
|
2007-12-05 02:28:50 -07:00
|
|
|
if (!queue) {
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto err_out_unlock;
|
|
|
|
}
|
2005-08-09 20:44:15 -06:00
|
|
|
instance_destroy(queue);
|
|
|
|
break;
|
|
|
|
case NFQNL_CFG_CMD_PF_BIND:
|
|
|
|
case NFQNL_CFG_CMD_PF_UNBIND:
|
|
|
|
break;
|
|
|
|
default:
|
2007-12-17 23:40:19 -07:00
|
|
|
ret = -ENOTSUPP;
|
2005-08-09 20:50:45 -06:00
|
|
|
break;
|
2005-08-09 20:44:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-28 15:37:03 -06:00
|
|
|
if (nfqa[NFQA_CFG_PARAMS]) {
|
2005-08-09 20:44:15 -06:00
|
|
|
struct nfqnl_msg_config_params *params;
|
|
|
|
|
2006-03-12 21:32:47 -07:00
|
|
|
if (!queue) {
|
2007-12-05 02:28:30 -07:00
|
|
|
ret = -ENODEV;
|
2007-12-05 02:28:50 -07:00
|
|
|
goto err_out_unlock;
|
2006-03-12 21:32:47 -07:00
|
|
|
}
|
2007-09-28 15:37:03 -06:00
|
|
|
params = nla_data(nfqa[NFQA_CFG_PARAMS]);
|
2005-08-09 20:44:15 -06:00
|
|
|
nfqnl_set_mode(queue, params->copy_mode,
|
|
|
|
ntohl(params->copy_range));
|
|
|
|
}
|
|
|
|
|
2007-09-28 15:37:03 -06:00
|
|
|
if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
|
2006-11-28 18:35:33 -07:00
|
|
|
__be32 *queue_maxlen;
|
2007-12-05 02:28:30 -07:00
|
|
|
|
|
|
|
if (!queue) {
|
|
|
|
ret = -ENODEV;
|
2007-12-05 02:28:50 -07:00
|
|
|
goto err_out_unlock;
|
2007-12-05 02:28:30 -07:00
|
|
|
}
|
2007-09-28 15:37:03 -06:00
|
|
|
queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
|
2006-11-28 18:35:33 -07:00
|
|
|
spin_lock_bh(&queue->lock);
|
|
|
|
queue->queue_maxlen = ntohl(*queue_maxlen);
|
|
|
|
spin_unlock_bh(&queue->lock);
|
|
|
|
}
|
|
|
|
|
2007-12-05 02:28:50 -07:00
|
|
|
err_out_unlock:
|
|
|
|
rcu_read_unlock();
|
2005-08-09 20:50:45 -06:00
|
|
|
return ret;
|
2005-08-09 20:44:15 -06:00
|
|
|
}
|
|
|
|
|
2007-09-28 15:15:45 -06:00
|
|
|
static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
|
2005-08-09 20:44:15 -06:00
|
|
|
[NFQNL_MSG_PACKET] = { .call = nfqnl_recv_unsupp,
|
2005-11-14 16:24:59 -07:00
|
|
|
.attr_count = NFQA_MAX, },
|
2005-08-09 20:44:15 -06:00
|
|
|
[NFQNL_MSG_VERDICT] = { .call = nfqnl_recv_verdict,
|
2007-09-28 15:39:26 -06:00
|
|
|
.attr_count = NFQA_MAX,
|
|
|
|
.policy = nfqa_verdict_policy },
|
2005-08-09 20:44:15 -06:00
|
|
|
[NFQNL_MSG_CONFIG] = { .call = nfqnl_recv_config,
|
2007-09-28 15:39:26 -06:00
|
|
|
.attr_count = NFQA_CFG_MAX,
|
|
|
|
.policy = nfqa_cfg_policy },
|
2005-08-09 20:44:15 -06:00
|
|
|
};
|
|
|
|
|
2007-09-28 15:15:45 -06:00
|
|
|
static const struct nfnetlink_subsystem nfqnl_subsys = {
|
2005-08-09 20:44:15 -06:00
|
|
|
.name = "nf_queue",
|
|
|
|
.subsys_id = NFNL_SUBSYS_QUEUE,
|
|
|
|
.cb_count = NFQNL_MSG_MAX,
|
|
|
|
.cb = nfqnl_cb,
|
|
|
|
};
|
|
|
|
|
2005-08-09 20:50:45 -06:00
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
struct iter_state {
|
|
|
|
unsigned int bucket;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct hlist_node *get_first(struct seq_file *seq)
|
|
|
|
{
|
|
|
|
struct iter_state *st = seq->private;
|
|
|
|
|
|
|
|
if (!st)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
|
|
|
|
if (!hlist_empty(&instance_table[st->bucket]))
|
|
|
|
return instance_table[st->bucket].first;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
|
|
|
|
{
|
|
|
|
struct iter_state *st = seq->private;
|
|
|
|
|
|
|
|
h = h->next;
|
|
|
|
while (!h) {
|
|
|
|
if (++st->bucket >= INSTANCE_BUCKETS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
h = instance_table[st->bucket].first;
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
|
|
|
|
{
|
|
|
|
struct hlist_node *head;
|
|
|
|
head = get_first(seq);
|
|
|
|
|
|
|
|
if (head)
|
|
|
|
while (pos && (head = get_next(seq, head)))
|
|
|
|
pos--;
|
|
|
|
return pos ? NULL : head;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *seq_start(struct seq_file *seq, loff_t *pos)
|
2008-01-31 04:53:27 -07:00
|
|
|
__acquires(instances_lock)
|
2005-08-09 20:50:45 -06:00
|
|
|
{
|
2007-12-05 02:28:50 -07:00
|
|
|
spin_lock(&instances_lock);
|
2005-08-09 20:50:45 -06:00
|
|
|
return get_idx(seq, *pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
|
|
|
|
{
|
|
|
|
(*pos)++;
|
|
|
|
return get_next(s, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void seq_stop(struct seq_file *s, void *v)
|
2008-01-31 04:53:27 -07:00
|
|
|
__releases(instances_lock)
|
2005-08-09 20:50:45 -06:00
|
|
|
{
|
2007-12-05 02:28:50 -07:00
|
|
|
spin_unlock(&instances_lock);
|
2005-08-09 20:50:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static int seq_show(struct seq_file *s, void *v)
|
|
|
|
{
|
|
|
|
const struct nfqnl_instance *inst = v;
|
|
|
|
|
|
|
|
return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
|
|
|
|
inst->queue_num,
|
|
|
|
inst->peer_pid, inst->queue_total,
|
|
|
|
inst->copy_mode, inst->copy_range,
|
|
|
|
inst->queue_dropped, inst->queue_user_dropped,
|
2007-12-05 02:28:50 -07:00
|
|
|
inst->id_sequence, 1);
|
2005-08-09 20:50:45 -06:00
|
|
|
}
|
|
|
|
|
2007-07-11 00:07:31 -06:00
|
|
|
static const struct seq_operations nfqnl_seq_ops = {
|
2005-08-09 20:50:45 -06:00
|
|
|
.start = seq_start,
|
|
|
|
.next = seq_next,
|
|
|
|
.stop = seq_stop,
|
|
|
|
.show = seq_show,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int nfqnl_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
2007-10-10 03:29:58 -06:00
|
|
|
return seq_open_private(file, &nfqnl_seq_ops,
|
|
|
|
sizeof(struct iter_state));
|
2005-08-09 20:50:45 -06:00
|
|
|
}
|
|
|
|
|
2007-02-12 01:55:36 -07:00
|
|
|
static const struct file_operations nfqnl_file_ops = {
|
2005-08-09 20:50:45 -06:00
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.open = nfqnl_open,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
|
|
|
.release = seq_release_private,
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* PROC_FS */
|
|
|
|
|
2006-04-06 15:11:30 -06:00
|
|
|
static int __init nfnetlink_queue_init(void)
|
2005-08-09 20:44:15 -06:00
|
|
|
{
|
2005-08-09 20:50:45 -06:00
|
|
|
int i, status = -ENOMEM;
|
2007-02-12 12:15:49 -07:00
|
|
|
|
2005-08-09 20:50:45 -06:00
|
|
|
for (i = 0; i < INSTANCE_BUCKETS; i++)
|
|
|
|
INIT_HLIST_HEAD(&instance_table[i]);
|
|
|
|
|
2005-08-09 20:44:15 -06:00
|
|
|
netlink_register_notifier(&nfqnl_rtnl_notifier);
|
|
|
|
status = nfnetlink_subsys_register(&nfqnl_subsys);
|
|
|
|
if (status < 0) {
|
|
|
|
printk(KERN_ERR "nf_queue: failed to create netlink socket\n");
|
|
|
|
goto cleanup_netlink_notifier;
|
|
|
|
}
|
|
|
|
|
2005-08-09 20:50:45 -06:00
|
|
|
#ifdef CONFIG_PROC_FS
|
2008-03-27 17:55:53 -06:00
|
|
|
if (!proc_create("nfnetlink_queue", 0440,
|
|
|
|
proc_net_netfilter, &nfqnl_file_ops))
|
2005-08-09 20:50:45 -06:00
|
|
|
goto cleanup_subsys;
|
|
|
|
#endif
|
|
|
|
|
2005-08-09 20:44:15 -06:00
|
|
|
register_netdevice_notifier(&nfqnl_dev_notifier);
|
|
|
|
return status;
|
|
|
|
|
2005-08-09 20:50:45 -06:00
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
cleanup_subsys:
|
2005-08-09 20:44:15 -06:00
|
|
|
nfnetlink_subsys_unregister(&nfqnl_subsys);
|
2006-04-06 15:11:30 -06:00
|
|
|
#endif
|
2005-08-09 20:44:15 -06:00
|
|
|
cleanup_netlink_notifier:
|
|
|
|
netlink_unregister_notifier(&nfqnl_rtnl_notifier);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2006-03-28 17:37:06 -07:00
|
|
|
static void __exit nfnetlink_queue_fini(void)
|
2005-08-09 20:44:15 -06:00
|
|
|
{
|
2006-04-06 15:11:30 -06:00
|
|
|
nf_unregister_queue_handlers(&nfqh);
|
|
|
|
unregister_netdevice_notifier(&nfqnl_dev_notifier);
|
|
|
|
#ifdef CONFIG_PROC_FS
|
|
|
|
remove_proc_entry("nfnetlink_queue", proc_net_netfilter);
|
|
|
|
#endif
|
|
|
|
nfnetlink_subsys_unregister(&nfqnl_subsys);
|
|
|
|
netlink_unregister_notifier(&nfqnl_rtnl_notifier);
|
2005-08-09 20:44:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
MODULE_DESCRIPTION("netfilter packet queue handler");
|
|
|
|
MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
|
|
|
|
|
2006-03-28 17:37:06 -07:00
|
|
|
module_init(nfnetlink_queue_init);
|
|
|
|
module_exit(nfnetlink_queue_fini);
|