staging: memrar depends on RAR_REGISTER
[safe/jmp/linux-2.6] / net / packet / af_packet.c
index 178e293..243946d 100644 (file)
@@ -60,6 +60,7 @@
 #include <linux/wireless.h>
 #include <linux/kernel.h>
 #include <linux/kmod.h>
+#include <linux/slab.h>
 #include <net/net_namespace.h>
 #include <net/ip.h>
 #include <net/protocol.h>
@@ -157,7 +158,6 @@ struct packet_mreq_max {
        unsigned char   mr_address[MAX_ADDR_LEN];
 };
 
-#ifdef CONFIG_PACKET_MMAP
 static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
                int closing, int tx_ring);
 
@@ -177,7 +177,6 @@ struct packet_ring_buffer {
 
 struct packet_sock;
 static int tpacket_snd(struct packet_sock *po, struct msghdr *msg);
-#endif
 
 static void packet_flush_mclist(struct sock *sk);
 
@@ -185,11 +184,9 @@ struct packet_sock {
        /* struct sock has to be the first member of packet_sock */
        struct sock             sk;
        struct tpacket_stats    stats;
-#ifdef CONFIG_PACKET_MMAP
        struct packet_ring_buffer       rx_ring;
        struct packet_ring_buffer       tx_ring;
        int                     copy_thresh;
-#endif
        spinlock_t              bind_lock;
        struct mutex            pg_vec_lock;
        unsigned int            running:1,      /* prot_hook is attached*/
@@ -199,13 +196,11 @@ struct packet_sock {
        int                     ifindex;        /* bound device         */
        __be16                  num;
        struct packet_mclist    *mclist;
-#ifdef CONFIG_PACKET_MMAP
        atomic_t                mapped;
        enum tpacket_versions   tp_version;
        unsigned int            tp_hdrlen;
        unsigned int            tp_reserve;
        unsigned int            tp_loss:1;
-#endif
        struct packet_type      prot_hook ____cacheline_aligned_in_smp;
 };
 
@@ -219,8 +214,6 @@ struct packet_skb_cb {
 
 #define PACKET_SKB_CB(__skb)   ((struct packet_skb_cb *)((__skb)->cb))
 
-#ifdef CONFIG_PACKET_MMAP
-
 static void __packet_set_status(struct packet_sock *po, void *frame, int status)
 {
        union {
@@ -315,8 +308,6 @@ static inline void packet_increment_head(struct packet_ring_buffer *buff)
        buff->head = buff->head != buff->frame_max ? buff->head+1 : 0;
 }
 
-#endif
-
 static inline struct packet_sock *pkt_sk(struct sock *sk)
 {
        return (struct packet_sock *)sk;
@@ -510,7 +501,7 @@ static inline unsigned int run_filter(struct sk_buff *skb, struct sock *sk,
        struct sk_filter *filter;
 
        rcu_read_lock_bh();
-       filter = rcu_dereference(sk->sk_filter);
+       filter = rcu_dereference_bh(sk->sk_filter);
        if (filter != NULL)
                res = sk_run_filter(skb, filter->insns, filter->len);
        rcu_read_unlock_bh();
@@ -640,7 +631,6 @@ drop:
        return 0;
 }
 
-#ifdef CONFIG_PACKET_MMAP
 static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
                       struct packet_type *pt, struct net_device *orig_dev)
 {
@@ -1056,7 +1046,6 @@ out:
        mutex_unlock(&po->pg_vec_lock);
        return err;
 }
-#endif
 
 static inline struct sk_buff *packet_alloc_skb(struct sock *sk, size_t prepad,
                                               size_t reserve, size_t len,
@@ -1248,13 +1237,11 @@ out:
 static int packet_sendmsg(struct kiocb *iocb, struct socket *sock,
                struct msghdr *msg, size_t len)
 {
-#ifdef CONFIG_PACKET_MMAP
        struct sock *sk = sock->sk;
        struct packet_sock *po = pkt_sk(sk);
        if (po->tx_ring.pg_vec)
                return tpacket_snd(po, msg);
        else
-#endif
                return packet_snd(sock, msg, len);
 }
 
@@ -1268,9 +1255,7 @@ static int packet_release(struct socket *sock)
        struct sock *sk = sock->sk;
        struct packet_sock *po;
        struct net *net;
-#ifdef CONFIG_PACKET_MMAP
        struct tpacket_req req;
-#endif
 
        if (!sk)
                return 0;
@@ -1278,28 +1263,25 @@ static int packet_release(struct socket *sock)
        net = sock_net(sk);
        po = pkt_sk(sk);
 
-       write_lock_bh(&net->packet.sklist_lock);
-       sk_del_node_init(sk);
+       spin_lock_bh(&net->packet.sklist_lock);
+       sk_del_node_init_rcu(sk);
        sock_prot_inuse_add(net, sk->sk_prot, -1);
-       write_unlock_bh(&net->packet.sklist_lock);
-
-       /*
-        *      Unhook packet receive handler.
-        */
+       spin_unlock_bh(&net->packet.sklist_lock);
 
+       spin_lock(&po->bind_lock);
        if (po->running) {
                /*
-                *      Remove the protocol hook
+                * Remove from protocol table
                 */
-               dev_remove_pack(&po->prot_hook);
                po->running = 0;
                po->num = 0;
+               __dev_remove_pack(&po->prot_hook);
                __sock_put(sk);
        }
+       spin_unlock(&po->bind_lock);
 
        packet_flush_mclist(sk);
 
-#ifdef CONFIG_PACKET_MMAP
        memset(&req, 0, sizeof(req));
 
        if (po->rx_ring.pg_vec)
@@ -1307,12 +1289,11 @@ static int packet_release(struct socket *sock)
 
        if (po->tx_ring.pg_vec)
                packet_set_ring(sk, &req, 1, 1);
-#endif
 
+       synchronize_net();
        /*
         *      Now the socket is dead. No more input will appear.
         */
-
        sock_orphan(sk);
        sock->sk = NULL;
 
@@ -1496,10 +1477,11 @@ static int packet_create(struct net *net, struct socket *sock, int protocol,
                po->running = 1;
        }
 
-       write_lock_bh(&net->packet.sklist_lock);
-       sk_add_node(sk, &net->packet.sklist);
+       spin_lock_bh(&net->packet.sklist_lock);
+       sk_add_node_rcu(sk, &net->packet.sklist);
        sock_prot_inuse_add(net, &packet_proto, 1);
-       write_unlock_bh(&net->packet.sklist_lock);
+       spin_unlock_bh(&net->packet.sklist_lock);
+
        return 0;
 out:
        return err;
@@ -1707,6 +1689,8 @@ static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
 {
        switch (i->type) {
        case PACKET_MR_MULTICAST:
+               if (i->alen != dev->addr_len)
+                       return -EINVAL;
                if (what > 0)
                        return dev_mc_add(dev, i->addr, i->alen, 0);
                else
@@ -1719,6 +1703,8 @@ static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
                return dev_set_allmulti(dev, what);
                break;
        case PACKET_MR_UNICAST:
+               if (i->alen != dev->addr_len)
+                       return -EINVAL;
                if (what > 0)
                        return dev_unicast_add(dev, i->addr);
                else
@@ -1872,7 +1858,6 @@ packet_setsockopt(struct socket *sock, int level, int optname, char __user *optv
                return ret;
        }
 
-#ifdef CONFIG_PACKET_MMAP
        case PACKET_RX_RING:
        case PACKET_TX_RING:
        {
@@ -1943,7 +1928,6 @@ packet_setsockopt(struct socket *sock, int level, int optname, char __user *optv
                po->tp_loss = !!val;
                return 0;
        }
-#endif
        case PACKET_AUXDATA:
        {
                int val;
@@ -2041,7 +2025,6 @@ static int packet_getsockopt(struct socket *sock, int level, int optname,
 
                data = &val;
                break;
-#ifdef CONFIG_PACKET_MMAP
        case PACKET_VERSION:
                if (len > sizeof(int))
                        len = sizeof(int);
@@ -2077,7 +2060,6 @@ static int packet_getsockopt(struct socket *sock, int level, int optname,
                val = po->tp_loss;
                data = &val;
                break;
-#endif
        default:
                return -ENOPROTOOPT;
        }
@@ -2097,8 +2079,8 @@ static int packet_notifier(struct notifier_block *this, unsigned long msg, void
        struct net_device *dev = data;
        struct net *net = dev_net(dev);
 
-       read_lock(&net->packet.sklist_lock);
-       sk_for_each(sk, node, &net->packet.sklist) {
+       rcu_read_lock();
+       sk_for_each_rcu(sk, node, &net->packet.sklist) {
                struct packet_sock *po = pkt_sk(sk);
 
                switch (msg) {
@@ -2126,18 +2108,19 @@ static int packet_notifier(struct notifier_block *this, unsigned long msg, void
                        }
                        break;
                case NETDEV_UP:
-                       spin_lock(&po->bind_lock);
-                       if (dev->ifindex == po->ifindex && po->num &&
-                           !po->running) {
-                               dev_add_pack(&po->prot_hook);
-                               sock_hold(sk);
-                               po->running = 1;
+                       if (dev->ifindex == po->ifindex) {
+                               spin_lock(&po->bind_lock);
+                               if (po->num && !po->running) {
+                                       dev_add_pack(&po->prot_hook);
+                                       sock_hold(sk);
+                                       po->running = 1;
+                               }
+                               spin_unlock(&po->bind_lock);
                        }
-                       spin_unlock(&po->bind_lock);
                        break;
                }
        }
-       read_unlock(&net->packet.sklist_lock);
+       rcu_read_unlock();
        return NOTIFY_DONE;
 }
 
@@ -2186,8 +2169,6 @@ static int packet_ioctl(struct socket *sock, unsigned int cmd,
        case SIOCGIFDSTADDR:
        case SIOCSIFDSTADDR:
        case SIOCSIFFLAGS:
-               if (!net_eq(sock_net(sk), &init_net))
-                       return -ENOIOCTLCMD;
                return inet_dgram_ops.ioctl(sock, cmd, arg);
 #endif
 
@@ -2197,11 +2178,6 @@ static int packet_ioctl(struct socket *sock, unsigned int cmd,
        return 0;
 }
 
-#ifndef CONFIG_PACKET_MMAP
-#define packet_mmap sock_no_mmap
-#define packet_poll datagram_poll
-#else
-
 static unsigned int packet_poll(struct file *file, struct socket *sock,
                                poll_table *wait)
 {
@@ -2483,8 +2459,6 @@ out:
        mutex_unlock(&po->pg_vec_lock);
        return err;
 }
-#endif
-
 
 static const struct proto_ops packet_ops_spkt = {
        .family =       PF_PACKET,
@@ -2539,40 +2513,26 @@ static struct notifier_block packet_netdev_notifier = {
 };
 
 #ifdef CONFIG_PROC_FS
-static inline struct sock *packet_seq_idx(struct net *net, loff_t off)
-{
-       struct sock *s;
-       struct hlist_node *node;
-
-       sk_for_each(s, node, &net->packet.sklist) {
-               if (!off--)
-                       return s;
-       }
-       return NULL;
-}
 
 static void *packet_seq_start(struct seq_file *seq, loff_t *pos)
-       __acquires(seq_file_net(seq)->packet.sklist_lock)
+       __acquires(RCU)
 {
        struct net *net = seq_file_net(seq);
-       read_lock(&net->packet.sklist_lock);
-       return *pos ? packet_seq_idx(net, *pos - 1) : SEQ_START_TOKEN;
+
+       rcu_read_lock();
+       return seq_hlist_start_head_rcu(&net->packet.sklist, *pos);
 }
 
 static void *packet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 {
        struct net *net = seq_file_net(seq);
-       ++*pos;
-       return  (v == SEQ_START_TOKEN)
-               ? sk_head(&net->packet.sklist)
-               : sk_next((struct sock *)v) ;
+       return seq_hlist_next_rcu(v, &net->packet.sklist, pos);
 }
 
 static void packet_seq_stop(struct seq_file *seq, void *v)
-       __releases(seq_file_net(seq)->packet.sklist_lock)
+       __releases(RCU)
 {
-       struct net *net = seq_file_net(seq);
-       read_unlock(&net->packet.sklist_lock);
+       rcu_read_unlock();
 }
 
 static int packet_seq_show(struct seq_file *seq, void *v)
@@ -2580,7 +2540,7 @@ static int packet_seq_show(struct seq_file *seq, void *v)
        if (v == SEQ_START_TOKEN)
                seq_puts(seq, "sk       RefCnt Type Proto  Iface R Rmem   User   Inode\n");
        else {
-               struct sock *s = v;
+               struct sock *s = sk_entry(v);
                const struct packet_sock *po = pkt_sk(s);
 
                seq_printf(seq,
@@ -2624,7 +2584,7 @@ static const struct file_operations packet_seq_fops = {
 
 static int __net_init packet_net_init(struct net *net)
 {
-       rwlock_init(&net->packet.sklist_lock);
+       spin_lock_init(&net->packet.sklist_lock);
        INIT_HLIST_HEAD(&net->packet.sklist);
 
        if (!proc_net_fops_create(net, "packet", 0, &packet_seq_fops))