]> bbs.cooldavid.org Git - net-next-2.6.git/blobdiff - net/ipv6/netfilter/ip6_queue.c
[NETFILTER]: Avoid skb_copy/pskb_copy/skb_realloc_headroom
[net-next-2.6.git] / net / ipv6 / netfilter / ip6_queue.c
index 58847d3b61e503d036481309eba777081eb18c55..6413a30d9f68858cea06a0f6bac64233bdfd8aa0 100644 (file)
  * 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.
- *
- * 2001-11-06: First try. Working with ip_queue.c for IPv4 and trying
- *             to adapt it to IPv6
- *             HEAVILY based in ipqueue.c by James Morris. It's just
- *             a little modified version of it, so he's nearly the
- *             real coder of this.
- *             Few changes needed, mainly the hard_routing code and
- *             the netlink socket protocol (we're NETLINK_IP6_FW).
- * 2002-06-25: Code cleanup. [JM: ported cleanup over from ip_queue.c]
- * 2005-02-04: Added /proc counter for dropped packets; fixed so
- *             packets aren't delivered to user space if they're going
- *             to be dropped.
  */
 #include <linux/module.h>
 #include <linux/skbuff.h>
@@ -36,6 +24,7 @@
 #include <linux/sysctl.h>
 #include <linux/proc_fs.h>
 #include <linux/mutex.h>
+#include <net/net_namespace.h>
 #include <net/sock.h>
 #include <net/ipv6.h>
 #include <net/ip6_route.h>
@@ -189,12 +178,13 @@ ipq_flush(int verdict)
 static struct sk_buff *
 ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
 {
-       unsigned char *old_tail;
+       sk_buff_data_t old_tail;
        size_t size = 0;
        size_t data_len = 0;
        struct sk_buff *skb;
        struct ipq_packet_msg *pmsg;
        struct nlmsghdr *nlh;
+       struct timeval tv;
 
        read_lock_bh(&queue_lock);
 
@@ -232,15 +222,16 @@ ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
        if (!skb)
                goto nlmsg_failure;
 
-       old_tail= skb->tail;
+       old_tail = skb->tail;
        nlh = NLMSG_PUT(skb, 0, 0, IPQM_PACKET, size - sizeof(*nlh));
        pmsg = NLMSG_DATA(nlh);
        memset(pmsg, 0, sizeof(*pmsg));
 
        pmsg->packet_id       = (unsigned long )entry;
        pmsg->data_len        = data_len;
-       pmsg->timestamp_sec   = entry->skb->tstamp.off_sec;
-       pmsg->timestamp_usec  = entry->skb->tstamp.off_usec;
+       tv = ktime_to_timeval(entry->skb->tstamp);
+       pmsg->timestamp_sec   = tv.tv_sec;
+       pmsg->timestamp_usec  = tv.tv_usec;
        pmsg->mark            = entry->skb->mark;
        pmsg->hook            = entry->info->hook;
        pmsg->hw_protocol     = entry->skb->protocol;
@@ -257,10 +248,7 @@ ipq_build_packet_message(struct ipq_queue_entry *entry, int *errp)
 
        if (entry->info->indev && entry->skb->dev) {
                pmsg->hw_type = entry->skb->dev->type;
-               if (entry->skb->dev->hard_header_parse)
-                       pmsg->hw_addrlen =
-                               entry->skb->dev->hard_header_parse(entry->skb,
-                                                                  pmsg->hw_addr);
+               pmsg->hw_addrlen = dev_parse_header(entry->skb, pmsg->hw_addr);
        }
 
        if (data_len)
@@ -344,6 +332,7 @@ static int
 ipq_mangle_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
 {
        int diff;
+       int err;
        struct ipv6hdr *user_iph = (struct ipv6hdr *)v->payload;
 
        if (v->data_len < sizeof(*user_iph))
@@ -356,27 +345,20 @@ ipq_mangle_ipv6(ipq_verdict_msg_t *v, struct ipq_queue_entry *e)
                if (v->data_len > 0xFFFF)
                        return -EINVAL;
                if (diff > skb_tailroom(e->skb)) {
-                       struct sk_buff *newskb;
-
-                       newskb = skb_copy_expand(e->skb,
-                                                skb_headroom(e->skb),
-                                                diff,
-                                                GFP_ATOMIC);
-                       if (newskb == NULL) {
+                       err = pskb_expand_head(e->skb, 0,
+                                              diff - skb_tailroom(e->skb),
+                                              GFP_ATOMIC);
+                       if (err) {
                                printk(KERN_WARNING "ip6_queue: OOM "
                                      "in mangle, dropping packet\n");
-                               return -ENOMEM;
+                               return err;
                        }
-                       if (e->skb->sk)
-                               skb_set_owner_w(newskb, e->skb->sk);
-                       kfree_skb(e->skb);
-                       e->skb = newskb;
                }
                skb_put(e->skb, diff);
        }
-       if (!skb_make_writable(&e->skb, v->data_len))
+       if (!skb_make_writable(e->skb, v->data_len))
                return -ENOMEM;
-       memcpy(e->skb->data, v->payload, v->data_len);
+       skb_copy_to_linear_data(e->skb, v->payload, v->data_len);
        e->skb->ip_summed = CHECKSUM_NONE;
 
        return 0;
@@ -476,7 +458,7 @@ ipq_dev_drop(int ifindex)
 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
 
 static inline void
-ipq_rcv_skb(struct sk_buff *skb)
+__ipq_rcv_skb(struct sk_buff *skb)
 {
        int status, type, pid, flags, nlmsglen, skblen;
        struct nlmsghdr *nlh;
@@ -485,7 +467,7 @@ ipq_rcv_skb(struct sk_buff *skb)
        if (skblen < sizeof(*nlh))
                return;
 
-       nlh = (struct nlmsghdr *)skb->data;
+       nlh = nlmsg_hdr(skb);
        nlmsglen = nlh->nlmsg_len;
        if (nlmsglen < sizeof(*nlh) || skblen < nlmsglen)
                return;
@@ -534,19 +516,10 @@ ipq_rcv_skb(struct sk_buff *skb)
 }
 
 static void
-ipq_rcv_sk(struct sock *sk, int len)
+ipq_rcv_skb(struct sk_buff *skb)
 {
-       struct sk_buff *skb;
-       unsigned int qlen;
-
        mutex_lock(&ipqnl_mutex);
-
-       for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
-               skb = skb_dequeue(&sk->sk_receive_queue);
-               ipq_rcv_skb(skb);
-               kfree_skb(skb);
-       }
-
+       __ipq_rcv_skb(skb);
        mutex_unlock(&ipqnl_mutex);
 }
 
@@ -556,6 +529,9 @@ ipq_rcv_dev_event(struct notifier_block *this,
 {
        struct net_device *dev = ptr;
 
+       if (dev->nd_net != &init_net)
+               return NOTIFY_DONE;
+
        /* Drop any packets associated with the downed device */
        if (event == NETDEV_DOWN)
                ipq_dev_drop(dev->ifindex);
@@ -575,7 +551,7 @@ ipq_rcv_nl_event(struct notifier_block *this,
        if (event == NETLINK_URELEASE &&
            n->protocol == NETLINK_IP6_FW && n->pid) {
                write_lock_bh(&queue_lock);
-               if (n->pid == peer_pid)
+               if ((n->net == &init_net) && (n->pid == peer_pid))
                        __ipq_reset();
                write_unlock_bh(&queue_lock);
        }
@@ -667,14 +643,14 @@ static int __init ip6_queue_init(void)
        struct proc_dir_entry *proc;
 
        netlink_register_notifier(&ipq_nl_notifier);
-       ipqnl = netlink_kernel_create(NETLINK_IP6_FW, 0, ipq_rcv_sk,
-                                     THIS_MODULE);
+       ipqnl = netlink_kernel_create(&init_net, NETLINK_IP6_FW, 0,
+                                     ipq_rcv_skb, NULL, THIS_MODULE);
        if (ipqnl == NULL) {
                printk(KERN_ERR "ip6_queue: failed to create netlink socket\n");
                goto cleanup_netlink_notifier;
        }
 
-       proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
+       proc = proc_net_create(&init_net, IPQ_PROC_FS_NAME, 0, ipq_get_info);
        if (proc)
                proc->owner = THIS_MODULE;
        else {
@@ -683,7 +659,7 @@ static int __init ip6_queue_init(void)
        }
 
        register_netdevice_notifier(&ipq_dev_notifier);
-       ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
+       ipq_sysctl_header = register_sysctl_table(ipq_root_table);
 
        status = nf_register_queue_handler(PF_INET6, &nfqh);
        if (status < 0) {
@@ -695,7 +671,7 @@ static int __init ip6_queue_init(void)
 cleanup_sysctl:
        unregister_sysctl_table(ipq_sysctl_header);
        unregister_netdevice_notifier(&ipq_dev_notifier);
-       proc_net_remove(IPQ_PROC_FS_NAME);
+       proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
 
 cleanup_ipqnl:
        sock_release(ipqnl->sk_socket);
@@ -715,7 +691,7 @@ static void __exit ip6_queue_fini(void)
 
        unregister_sysctl_table(ipq_sysctl_header);
        unregister_netdevice_notifier(&ipq_dev_notifier);
-       proc_net_remove(IPQ_PROC_FS_NAME);
+       proc_net_remove(&init_net, IPQ_PROC_FS_NAME);
 
        sock_release(ipqnl->sk_socket);
        mutex_lock(&ipqnl_mutex);