Merge branch 'master' of /home/davem/src/GIT/linux-2.6/
[safe/jmp/linux-2.6] / net / packet / af_packet.c
index 6da9f38..031a5e6 100644 (file)
@@ -79,6 +79,8 @@
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/mutex.h>
+#include <linux/if_vlan.h>
+#include <linux/virtio_net.h>
 
 #ifdef CONFIG_INET
 #include <net/inet_common.h>
@@ -137,8 +139,7 @@ dev->hard_header == NULL (ll header is added by device, we cannot control it)
 
 /* Private packet socket structures. */
 
-struct packet_mclist
-{
+struct packet_mclist {
        struct packet_mclist    *next;
        int                     ifindex;
        int                     count;
@@ -149,20 +150,18 @@ struct packet_mclist
 /* identical to struct packet_mreq except it has
  * a longer address field.
  */
-struct packet_mreq_max
-{
+struct packet_mreq_max {
        int             mr_ifindex;
        unsigned short  mr_type;
        unsigned short  mr_alen;
        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);
 
 struct packet_ring_buffer {
-       char *                  *pg_vec;
+       char                    **pg_vec;
        unsigned int            head;
        unsigned int            frames_per_block;
        unsigned int            frame_size;
@@ -177,7 +176,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,27 +183,24 @@ 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
-       struct packet_type      prot_hook;
        spinlock_t              bind_lock;
        struct mutex            pg_vec_lock;
        unsigned int            running:1,      /* prot_hook is attached*/
                                auxdata:1,
-                               origdev:1;
+                               origdev:1,
+                               has_vnet_hdr:1;
        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;
 };
 
 struct packet_skb_cb {
@@ -218,8 +213,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 {
@@ -239,7 +232,7 @@ static void __packet_set_status(struct packet_sock *po, void *frame, int status)
                flush_dcache_page(virt_to_page(&h.h2->tp_status));
                break;
        default:
-               printk(KERN_ERR "TPACKET version not supported\n");
+               pr_err("TPACKET version not supported\n");
                BUG();
        }
 
@@ -265,7 +258,7 @@ static int __packet_get_status(struct packet_sock *po, void *frame)
                flush_dcache_page(virt_to_page(&h.h2->tp_status));
                return h.h2->tp_status;
        default:
-               printk(KERN_ERR "TPACKET version not supported\n");
+               pr_err("TPACKET version not supported\n");
                BUG();
                return 0;
        }
@@ -314,8 +307,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;
@@ -327,7 +318,7 @@ static void packet_sock_destruct(struct sock *sk)
        WARN_ON(atomic_read(&sk->sk_wmem_alloc));
 
        if (!sock_flag(sk, SOCK_DEAD)) {
-               printk("Attempt to release alive packet socket: %p\n", sk);
+               pr_err("Attempt to release alive packet socket: %p\n", sk);
                return;
        }
 
@@ -339,7 +330,8 @@ static const struct proto_ops packet_ops;
 
 static const struct proto_ops packet_ops_spkt;
 
-static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,  struct packet_type *pt, struct net_device *orig_dev)
+static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,
+                          struct packet_type *pt, struct net_device *orig_dev)
 {
        struct sock *sk;
        struct sockaddr_pkt *spkt;
@@ -365,15 +357,15 @@ static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,  struct
        if (skb->pkt_type == PACKET_LOOPBACK)
                goto out;
 
-       if (dev_net(dev) != sock_net(sk))
+       if (!net_eq(dev_net(dev), sock_net(sk)))
                goto out;
 
-       if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
+       skb = skb_share_check(skb, GFP_ATOMIC);
+       if (skb == NULL)
                goto oom;
 
        /* drop any routing info */
-       dst_release(skb->dst);
-       skb->dst = NULL;
+       skb_dst_drop(skb);
 
        /* drop conntrack reference */
        nf_reset(skb);
@@ -395,7 +387,7 @@ static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,  struct
         *      to prevent sockets using all the memory up.
         */
 
-       if (sock_queue_rcv_skb(sk,skb) == 0)
+       if (sock_queue_rcv_skb(sk, skb) == 0)
                return 0;
 
 out:
@@ -414,32 +406,32 @@ static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
                               struct msghdr *msg, size_t len)
 {
        struct sock *sk = sock->sk;
-       struct sockaddr_pkt *saddr=(struct sockaddr_pkt *)msg->msg_name;
-       struct sk_buff *skb;
+       struct sockaddr_pkt *saddr = (struct sockaddr_pkt *)msg->msg_name;
+       struct sk_buff *skb = NULL;
        struct net_device *dev;
-       __be16 proto=0;
+       __be16 proto = 0;
        int err;
 
        /*
         *      Get and verify the address.
         */
 
-       if (saddr)
-       {
+       if (saddr) {
                if (msg->msg_namelen < sizeof(struct sockaddr))
-                       return(-EINVAL);
-               if (msg->msg_namelen==sizeof(struct sockaddr_pkt))
-                       proto=saddr->spkt_protocol;
-       }
-       else
-               return(-ENOTCONN);      /* SOCK_PACKET must be sent giving an address */
+                       return -EINVAL;
+               if (msg->msg_namelen == sizeof(struct sockaddr_pkt))
+                       proto = saddr->spkt_protocol;
+       } else
+               return -ENOTCONN;       /* SOCK_PACKET must be sent giving an address */
 
        /*
         *      Find the device first to size check it
         */
 
        saddr->spkt_device[13] = 0;
-       dev = dev_get_by_name(sock_net(sk), saddr->spkt_device);
+retry:
+       rcu_read_lock();
+       dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device);
        err = -ENODEV;
        if (dev == NULL)
                goto out_unlock;
@@ -449,66 +441,56 @@ static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock,
                goto out_unlock;
 
        /*
-        *      You may not queue a frame bigger than the mtu. This is the lowest level
-        *      raw protocol and you must do your own fragmentation at this level.
+        * You may not queue a frame bigger than the mtu. This is the lowest level
+        * raw protocol and you must do your own fragmentation at this level.
         */
 
        err = -EMSGSIZE;
        if (len > dev->mtu + dev->hard_header_len)
                goto out_unlock;
 
-       err = -ENOBUFS;
-       skb = sock_wmalloc(sk, len + LL_RESERVED_SPACE(dev), 0, GFP_KERNEL);
-
-       /*
-        *      If the write buffer is full, then tough. At this level the user gets to
-        *      deal with the problem - do your own algorithmic backoffs. That's far
-        *      more flexible.
-        */
-
-       if (skb == NULL)
-               goto out_unlock;
-
-       /*
-        *      Fill it in
-        */
-
-       /* FIXME: Save some space for broken drivers that write a
-        * hard header at transmission time by themselves. PPP is the
-        * notable one here. This should really be fixed at the driver level.
-        */
-       skb_reserve(skb, LL_RESERVED_SPACE(dev));
-       skb_reset_network_header(skb);
-
-       /* Try to align data part correctly */
-       if (dev->header_ops) {
-               skb->data -= dev->hard_header_len;
-               skb->tail -= dev->hard_header_len;
-               if (len < dev->hard_header_len)
-                       skb_reset_network_header(skb);
+       if (!skb) {
+               size_t reserved = LL_RESERVED_SPACE(dev);
+               unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0;
+
+               rcu_read_unlock();
+               skb = sock_wmalloc(sk, len + reserved, 0, GFP_KERNEL);
+               if (skb == NULL)
+                       return -ENOBUFS;
+               /* FIXME: Save some space for broken drivers that write a hard
+                * header at transmission time by themselves. PPP is the notable
+                * one here. This should really be fixed at the driver level.
+                */
+               skb_reserve(skb, reserved);
+               skb_reset_network_header(skb);
+
+               /* Try to align data part correctly */
+               if (hhlen) {
+                       skb->data -= hhlen;
+                       skb->tail -= hhlen;
+                       if (len < hhlen)
+                               skb_reset_network_header(skb);
+               }
+               err = memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len);
+               if (err)
+                       goto out_free;
+               goto retry;
        }
 
-       /* Returns -EFAULT on error */
-       err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
+
        skb->protocol = proto;
        skb->dev = dev;
        skb->priority = sk->sk_priority;
-       if (err)
-               goto out_free;
-
-       /*
-        *      Now send it
-        */
+       skb->mark = sk->sk_mark;
 
        dev_queue_xmit(skb);
-       dev_put(dev);
-       return(len);
+       rcu_read_unlock();
+       return len;
 
+out_unlock:
+       rcu_read_unlock();
 out_free:
        kfree_skb(skb);
-out_unlock:
-       if (dev)
-               dev_put(dev);
        return err;
 }
 
@@ -518,7 +500,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();
@@ -538,12 +520,13 @@ static inline unsigned int run_filter(struct sk_buff *skb, struct sock *sk,
    we will not harm anyone.
  */
 
-static int packet_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt, struct net_device *orig_dev)
+static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
+                     struct packet_type *pt, struct net_device *orig_dev)
 {
        struct sock *sk;
        struct sockaddr_ll *sll;
        struct packet_sock *po;
-       u8 * skb_head = skb->data;
+       u8 *skb_head = skb->data;
        int skb_len = skb->len;
        unsigned int snaplen, res;
 
@@ -553,7 +536,7 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev, struct packet
        sk = pt->af_packet_priv;
        po = pkt_sk(sk);
 
-       if (dev_net(dev) != sock_net(sk))
+       if (!net_eq(dev_net(dev), sock_net(sk)))
                goto drop;
 
        skb->dev = dev;
@@ -621,23 +604,21 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev, struct packet
 
        skb_set_owner_r(skb, sk);
        skb->dev = NULL;
-       dst_release(skb->dst);
-       skb->dst = NULL;
+       skb_dst_drop(skb);
 
        /* drop conntrack reference */
        nf_reset(skb);
 
        spin_lock(&sk->sk_receive_queue.lock);
        po->stats.tp_packets++;
+       skb->dropcount = atomic_read(&sk->sk_drops);
        __skb_queue_tail(&sk->sk_receive_queue, skb);
        spin_unlock(&sk->sk_receive_queue.lock);
        sk->sk_data_ready(sk, skb->len);
        return 0;
 
 drop_n_acct:
-       spin_lock(&sk->sk_receive_queue.lock);
-       po->stats.tp_drops++;
-       spin_unlock(&sk->sk_receive_queue.lock);
+       po->stats.tp_drops = atomic_inc_return(&sk->sk_drops);
 
 drop_n_restore:
        if (skb_head != skb->data && skb_shared(skb)) {
@@ -649,8 +630,8 @@ 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)
+static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
+                      struct packet_type *pt, struct net_device *orig_dev)
 {
        struct sock *sk;
        struct packet_sock *po;
@@ -660,7 +641,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, struct packe
                struct tpacket2_hdr *h2;
                void *raw;
        } h;
-       u8 * skb_head = skb->data;
+       u8 *skb_head = skb->data;
        int skb_len = skb->len;
        unsigned int snaplen, res;
        unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
@@ -675,7 +656,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, struct packe
        sk = pt->af_packet_priv;
        po = pkt_sk(sk);
 
-       if (dev_net(dev) != sock_net(sk))
+       if (!net_eq(dev_net(dev), sock_net(sk)))
                goto drop;
 
        if (dev->header_ops) {
@@ -768,7 +749,7 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev, struct packe
                        getnstimeofday(&ts);
                h.h2->tp_sec = ts.tv_sec;
                h.h2->tp_nsec = ts.tv_nsec;
-               h.h2->tp_vlan_tci = skb->vlan_tci;
+               h.h2->tp_vlan_tci = vlan_tx_tag_get(skb);
                hdrlen = sizeof(*h.h2);
                break;
        default:
@@ -823,7 +804,7 @@ ring_is_full:
 static void tpacket_destruct_skb(struct sk_buff *skb)
 {
        struct packet_sock *po = pkt_sk(skb->sk);
-       void * ph;
+       void *ph;
 
        BUG_ON(skb == NULL);
 
@@ -838,9 +819,9 @@ static void tpacket_destruct_skb(struct sk_buff *skb)
        sock_wfree(skb);
 }
 
-static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff * skb,
-               void * frame, struct net_device *dev, int size_max,
-               __be16 proto, unsigned char * addr)
+static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff *skb,
+               void *frame, struct net_device *dev, int size_max,
+               __be16 proto, unsigned char *addr)
 {
        union {
                struct tpacket_hdr *h1;
@@ -858,6 +839,7 @@ static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff * skb,
        skb->protocol = proto;
        skb->dev = dev;
        skb->priority = po->sk.sk_priority;
+       skb->mark = po->sk.sk_mark;
        skb_shinfo(skb)->destructor_arg = ph.raw;
 
        switch (po->tp_version) {
@@ -869,8 +851,7 @@ static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff * skb,
                break;
        }
        if (unlikely(tp_len > size_max)) {
-               printk(KERN_ERR "packet size is too long (%d > %d)\n",
-                               tp_len, size_max);
+               pr_err("packet size is too long (%d > %d)\n", tp_len, size_max);
                return -EMSGSIZE;
        }
 
@@ -885,12 +866,11 @@ static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff * skb,
                                NULL, tp_len);
                if (unlikely(err < 0))
                        return -EINVAL;
-       } else if (dev->hard_header_len ) {
+       } else if (dev->hard_header_len) {
                /* net device doesn't like empty head */
                if (unlikely(tp_len <= dev->hard_header_len)) {
-                       printk(KERN_ERR "packet size is too short "
-                                       "(%d < %d)\n", tp_len,
-                                       dev->hard_header_len);
+                       pr_err("packet size is too short (%d < %d)\n",
+                              tp_len, dev->hard_header_len);
                        return -EINVAL;
                }
 
@@ -919,9 +899,8 @@ static int tpacket_fill_skb(struct packet_sock *po, struct sk_buff * skb,
                nr_frags = skb_shinfo(skb)->nr_frags;
 
                if (unlikely(nr_frags >= MAX_SKB_FRAGS)) {
-                       printk(KERN_ERR "Packet exceed the number "
-                                       "of skb frags(%lu)\n",
-                                       MAX_SKB_FRAGS);
+                       pr_err("Packet exceed the number of skb frags(%lu)\n",
+                              MAX_SKB_FRAGS);
                        return -EFAULT;
                }
 
@@ -946,8 +925,8 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
        struct net_device *dev;
        __be16 proto;
        int ifindex, err, reserve = 0;
-       void * ph;
-       struct sockaddr_ll *saddr=(struct sockaddr_ll *)msg->msg_name;
+       void *ph;
+       struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
        int tp_len, size_max;
        unsigned char *addr;
        int len_sum = 0;
@@ -987,10 +966,7 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
                goto out_put;
 
        size_max = po->tx_ring.frame_size
-               - sizeof(struct skb_shared_info)
-               - po->tp_hdrlen
-               - LL_ALLOCATED_SPACE(dev)
-               - sizeof(struct sockaddr_ll);
+               - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
 
        if (size_max > dev->mtu + reserve)
                size_max = dev->mtu + reserve;
@@ -1036,21 +1012,30 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
 
                status = TP_STATUS_SEND_REQUEST;
                err = dev_queue_xmit(skb);
-               if (unlikely(err > 0 && (err = net_xmit_errno(err)) != 0))
-                       goto out_xmit;
+               if (unlikely(err > 0)) {
+                       err = net_xmit_errno(err);
+                       if (err && __packet_get_status(po, ph) ==
+                                  TP_STATUS_AVAILABLE) {
+                               /* skb was destructed already */
+                               skb = NULL;
+                               goto out_status;
+                       }
+                       /*
+                        * skb was dropped but not destructed yet;
+                        * let's treat it like congestion or err < 0
+                        */
+                       err = 0;
+               }
                packet_increment_head(&po->tx_ring);
                len_sum += tp_len;
-       }
-       while (likely((ph != NULL) || ((!(msg->msg_flags & MSG_DONTWAIT))
-                                       && (atomic_read(&po->tx_ring.pending))))
-             );
+       } while (likely((ph != NULL) ||
+                       ((!(msg->msg_flags & MSG_DONTWAIT)) &&
+                        (atomic_read(&po->tx_ring.pending))))
+               );
 
        err = len_sum;
        goto out_put;
 
-out_xmit:
-       skb->destructor = sock_wfree;
-       atomic_dec(&po->tx_ring.pending);
 out_status:
        __packet_set_status(po, ph, status);
        kfree_skb(skb);
@@ -1060,26 +1045,52 @@ 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,
+                                              size_t linear, int noblock,
+                                              int *err)
+{
+       struct sk_buff *skb;
+
+       /* Under a page?  Don't bother with paged skb. */
+       if (prepad + len < PAGE_SIZE || !linear)
+               linear = len;
+
+       skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock,
+                                  err);
+       if (!skb)
+               return NULL;
+
+       skb_reserve(skb, reserve);
+       skb_put(skb, linear);
+       skb->data_len = len - linear;
+       skb->len += len - linear;
+
+       return skb;
+}
 
 static int packet_snd(struct socket *sock,
                          struct msghdr *msg, size_t len)
 {
        struct sock *sk = sock->sk;
-       struct sockaddr_ll *saddr=(struct sockaddr_ll *)msg->msg_name;
+       struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
        struct sk_buff *skb;
        struct net_device *dev;
        __be16 proto;
        unsigned char *addr;
        int ifindex, err, reserve = 0;
+       struct virtio_net_hdr vnet_hdr = { 0 };
+       int offset = 0;
+       int vnet_hdr_len;
+       struct packet_sock *po = pkt_sk(sk);
+       unsigned short gso_type = 0;
 
        /*
         *      Get and verify the address.
         */
 
        if (saddr == NULL) {
-               struct packet_sock *po = pkt_sk(sk);
-
                ifindex = po->ifindex;
                proto   = po->num;
                addr    = NULL;
@@ -1106,31 +1117,100 @@ static int packet_snd(struct socket *sock,
        if (!(dev->flags & IFF_UP))
                goto out_unlock;
 
+       if (po->has_vnet_hdr) {
+               vnet_hdr_len = sizeof(vnet_hdr);
+
+               err = -EINVAL;
+               if (len < vnet_hdr_len)
+                       goto out_unlock;
+
+               len -= vnet_hdr_len;
+
+               err = memcpy_fromiovec((void *)&vnet_hdr, msg->msg_iov,
+                                      vnet_hdr_len);
+               if (err < 0)
+                       goto out_unlock;
+
+               if ((vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
+                   (vnet_hdr.csum_start + vnet_hdr.csum_offset + 2 >
+                     vnet_hdr.hdr_len))
+                       vnet_hdr.hdr_len = vnet_hdr.csum_start +
+                                                vnet_hdr.csum_offset + 2;
+
+               err = -EINVAL;
+               if (vnet_hdr.hdr_len > len)
+                       goto out_unlock;
+
+               if (vnet_hdr.gso_type != VIRTIO_NET_HDR_GSO_NONE) {
+                       switch (vnet_hdr.gso_type & ~VIRTIO_NET_HDR_GSO_ECN) {
+                       case VIRTIO_NET_HDR_GSO_TCPV4:
+                               gso_type = SKB_GSO_TCPV4;
+                               break;
+                       case VIRTIO_NET_HDR_GSO_TCPV6:
+                               gso_type = SKB_GSO_TCPV6;
+                               break;
+                       case VIRTIO_NET_HDR_GSO_UDP:
+                               gso_type = SKB_GSO_UDP;
+                               break;
+                       default:
+                               goto out_unlock;
+                       }
+
+                       if (vnet_hdr.gso_type & VIRTIO_NET_HDR_GSO_ECN)
+                               gso_type |= SKB_GSO_TCP_ECN;
+
+                       if (vnet_hdr.gso_size == 0)
+                               goto out_unlock;
+
+               }
+       }
+
        err = -EMSGSIZE;
-       if (len > dev->mtu+reserve)
+       if (!gso_type && (len > dev->mtu+reserve))
                goto out_unlock;
 
-       skb = sock_alloc_send_skb(sk, len + LL_ALLOCATED_SPACE(dev),
-                               msg->msg_flags & MSG_DONTWAIT, &err);
-       if (skb==NULL)
+       err = -ENOBUFS;
+       skb = packet_alloc_skb(sk, LL_ALLOCATED_SPACE(dev),
+                              LL_RESERVED_SPACE(dev), len, vnet_hdr.hdr_len,
+                              msg->msg_flags & MSG_DONTWAIT, &err);
+       if (skb == NULL)
                goto out_unlock;
 
-       skb_reserve(skb, LL_RESERVED_SPACE(dev));
-       skb_reset_network_header(skb);
+       skb_set_network_header(skb, reserve);
 
        err = -EINVAL;
        if (sock->type == SOCK_DGRAM &&
-           dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len) < 0)
+           (offset = dev_hard_header(skb, dev, ntohs(proto), addr, NULL, len)) < 0)
                goto out_free;
 
        /* Returns -EFAULT on error */
-       err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
+       err = skb_copy_datagram_from_iovec(skb, offset, msg->msg_iov, 0, len);
        if (err)
                goto out_free;
 
        skb->protocol = proto;
        skb->dev = dev;
        skb->priority = sk->sk_priority;
+       skb->mark = sk->sk_mark;
+
+       if (po->has_vnet_hdr) {
+               if (vnet_hdr.flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
+                       if (!skb_partial_csum_set(skb, vnet_hdr.csum_start,
+                                                 vnet_hdr.csum_offset)) {
+                               err = -EINVAL;
+                               goto out_free;
+                       }
+               }
+
+               skb_shinfo(skb)->gso_size = vnet_hdr.gso_size;
+               skb_shinfo(skb)->gso_type = gso_type;
+
+               /* Header must be checked, and gso_segs computed. */
+               skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
+               skb_shinfo(skb)->gso_segs = 0;
+
+               len += vnet_hdr_len;
+       }
 
        /*
         *      Now send it
@@ -1142,7 +1222,7 @@ static int packet_snd(struct socket *sock,
 
        dev_put(dev);
 
-       return(len);
+       return len;
 
 out_free:
        kfree_skb(skb);
@@ -1156,13 +1236,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);
 }
 
@@ -1176,9 +1254,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;
@@ -1186,28 +1262,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)
@@ -1215,12 +1288,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;
 
@@ -1285,9 +1357,10 @@ out_unlock:
  *     Bind a packet socket to a device
  */
 
-static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr, int addr_len)
+static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
+                           int addr_len)
 {
-       struct sock *sk=sock->sk;
+       struct sock *sk = sock->sk;
        char name[15];
        struct net_device *dev;
        int err = -ENODEV;
@@ -1298,7 +1371,7 @@ static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr, int add
 
        if (addr_len != sizeof(struct sockaddr))
                return -EINVAL;
-       strlcpy(name,uaddr->sa_data,sizeof(name));
+       strlcpy(name, uaddr->sa_data, sizeof(name));
 
        dev = dev_get_by_name(sock_net(sk), name);
        if (dev) {
@@ -1310,8 +1383,8 @@ static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr, int add
 
 static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
 {
-       struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
-       struct sock *sk=sock->sk;
+       struct sockaddr_ll *sll = (struct sockaddr_ll *)uaddr;
+       struct sock *sk = sock->sk;
        struct net_device *dev = NULL;
        int err;
 
@@ -1349,7 +1422,8 @@ static struct proto packet_proto = {
  *     Create a packet of type SOCK_PACKET.
  */
 
-static int packet_create(struct net *net, struct socket *sock, int protocol)
+static int packet_create(struct net *net, struct socket *sock, int protocol,
+                        int kern)
 {
        struct sock *sk;
        struct packet_sock *po;
@@ -1402,11 +1476,12 @@ 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);
-       return(0);
+       spin_unlock_bh(&net->packet.sklist_lock);
+
+       return 0;
 out:
        return err;
 }
@@ -1423,6 +1498,7 @@ static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
        struct sk_buff *skb;
        int copied, err;
        struct sockaddr_ll *sll;
+       int vnet_hdr_len = 0;
 
        err = -EINVAL;
        if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
@@ -1443,7 +1519,7 @@ static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
         *      but then it will block.
         */
 
-       skb=skb_recv_datagram(sk,flags,flags&MSG_DONTWAIT,&err);
+       skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
 
        /*
         *      An error occurred so return it. Because skb_recv_datagram()
@@ -1454,6 +1530,48 @@ static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
        if (skb == NULL)
                goto out;
 
+       if (pkt_sk(sk)->has_vnet_hdr) {
+               struct virtio_net_hdr vnet_hdr = { 0 };
+
+               err = -EINVAL;
+               vnet_hdr_len = sizeof(vnet_hdr);
+               if ((len -= vnet_hdr_len) < 0)
+                       goto out_free;
+
+               if (skb_is_gso(skb)) {
+                       struct skb_shared_info *sinfo = skb_shinfo(skb);
+
+                       /* This is a hint as to how much should be linear. */
+                       vnet_hdr.hdr_len = skb_headlen(skb);
+                       vnet_hdr.gso_size = sinfo->gso_size;
+                       if (sinfo->gso_type & SKB_GSO_TCPV4)
+                               vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
+                       else if (sinfo->gso_type & SKB_GSO_TCPV6)
+                               vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
+                       else if (sinfo->gso_type & SKB_GSO_UDP)
+                               vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_UDP;
+                       else if (sinfo->gso_type & SKB_GSO_FCOE)
+                               goto out_free;
+                       else
+                               BUG();
+                       if (sinfo->gso_type & SKB_GSO_TCP_ECN)
+                               vnet_hdr.gso_type |= VIRTIO_NET_HDR_GSO_ECN;
+               } else
+                       vnet_hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE;
+
+               if (skb->ip_summed == CHECKSUM_PARTIAL) {
+                       vnet_hdr.flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
+                       vnet_hdr.csum_start = skb->csum_start -
+                                                       skb_headroom(skb);
+                       vnet_hdr.csum_offset = skb->csum_offset;
+               } /* else everything is zero */
+
+               err = memcpy_toiovec(msg->msg_iov, (void *)&vnet_hdr,
+                                    vnet_hdr_len);
+               if (err < 0)
+                       goto out_free;
+       }
+
        /*
         *      If the address length field is there to be filled in, we fill
         *      it in now.
@@ -1471,17 +1589,16 @@ static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
         */
 
        copied = skb->len;
-       if (copied > len)
-       {
-               copied=len;
-               msg->msg_flags|=MSG_TRUNC;
+       if (copied > len) {
+               copied = len;
+               msg->msg_flags |= MSG_TRUNC;
        }
 
        err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
        if (err)
                goto out_free;
 
-       sock_recv_timestamp(msg, sk, skb);
+       sock_recv_ts_and_drops(msg, sk, skb);
 
        if (msg->msg_name)
                memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
@@ -1497,7 +1614,7 @@ static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
                aux.tp_snaplen = skb->len;
                aux.tp_mac = 0;
                aux.tp_net = skb_network_offset(skb);
-               aux.tp_vlan_tci = skb->vlan_tci;
+               aux.tp_vlan_tci = vlan_tx_tag_get(skb);
 
                put_cmsg(msg, SOL_PACKET, PACKET_AUXDATA, sizeof(aux), &aux);
        }
@@ -1506,7 +1623,7 @@ static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
         *      Free or return the buffer as appropriate. Again this
         *      hides all the races and re-entrancy issues from us.
         */
-       err = (flags&MSG_TRUNC) ? skb->len : copied;
+       err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied);
 
 out_free:
        skb_free_datagram(sk, skb);
@@ -1524,12 +1641,13 @@ static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
                return -EOPNOTSUPP;
 
        uaddr->sa_family = AF_PACKET;
-       dev = dev_get_by_index(sock_net(sk), pkt_sk(sk)->ifindex);
-       if (dev) {
+       rcu_read_lock();
+       dev = dev_get_by_index_rcu(sock_net(sk), pkt_sk(sk)->ifindex);
+       if (dev)
                strlcpy(uaddr->sa_data, dev->name, 15);
-               dev_put(dev);
-       } else
+       else
                memset(uaddr->sa_data, 0, 14);
+       rcu_read_unlock();
        *uaddr_len = sizeof(*uaddr);
 
        return 0;
@@ -1541,7 +1659,7 @@ static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
        struct net_device *dev;
        struct sock *sk = sock->sk;
        struct packet_sock *po = pkt_sk(sk);
-       struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
+       DECLARE_SOCKADDR(struct sockaddr_ll *, sll, uaddr);
 
        if (peer)
                return -EOPNOTSUPP;
@@ -1549,16 +1667,17 @@ static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
        sll->sll_family = AF_PACKET;
        sll->sll_ifindex = po->ifindex;
        sll->sll_protocol = po->num;
-       dev = dev_get_by_index(sock_net(sk), po->ifindex);
+       rcu_read_lock();
+       dev = dev_get_by_index_rcu(sock_net(sk), po->ifindex);
        if (dev) {
                sll->sll_hatype = dev->type;
                sll->sll_halen = dev->addr_len;
                memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
-               dev_put(dev);
        } else {
                sll->sll_hatype = 0;    /* Bad: we have no ARPHRD_UNSPEC */
                sll->sll_halen = 0;
        }
+       rcu_read_unlock();
        *uaddr_len = offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen;
 
        return 0;
@@ -1586,14 +1705,15 @@ static int packet_dev_mc(struct net_device *dev, struct packet_mclist *i,
                else
                        return dev_unicast_delete(dev, i->addr);
                break;
-       default:;
+       default:
+               break;
        }
        return 0;
 }
 
 static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
 {
-       for ( ; i; i=i->next) {
+       for ( ; i; i = i->next) {
                if (i->ifindex == dev->ifindex)
                        packet_dev_mc(dev, i, what);
        }
@@ -1614,7 +1734,7 @@ static int packet_mc_add(struct sock *sk, struct packet_mreq_max *mreq)
                goto done;
 
        err = -EINVAL;
-       if (mreq->mr_alen > dev->addr_len)
+       if (mreq->mr_alen != dev->addr_len)
                goto done;
 
        err = -ENOBUFS;
@@ -1667,11 +1787,9 @@ static int packet_mc_drop(struct sock *sk, struct packet_mreq_max *mreq)
                        if (--ml->count == 0) {
                                struct net_device *dev;
                                *mlp = ml->next;
-                               dev = dev_get_by_index(sock_net(sk), ml->ifindex);
-                               if (dev) {
+                               dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
+                               if (dev)
                                        packet_dev_mc(dev, ml, -1);
-                                       dev_put(dev);
-                               }
                                kfree(ml);
                        }
                        rtnl_unlock();
@@ -1695,17 +1813,16 @@ static void packet_flush_mclist(struct sock *sk)
                struct net_device *dev;
 
                po->mclist = ml->next;
-               if ((dev = dev_get_by_index(sock_net(sk), ml->ifindex)) != NULL) {
+               dev = __dev_get_by_index(sock_net(sk), ml->ifindex);
+               if (dev != NULL)
                        packet_dev_mc(dev, ml, -1);
-                       dev_put(dev);
-               }
                kfree(ml);
        }
        rtnl_unlock();
 }
 
 static int
-packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, int optlen)
+packet_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
 {
        struct sock *sk = sock->sk;
        struct packet_sock *po = pkt_sk(sk);
@@ -1725,7 +1842,7 @@ packet_setsockopt(struct socket *sock, int level, int optname, char __user *optv
                        return -EINVAL;
                if (len > sizeof(mreq))
                        len = sizeof(mreq);
-               if (copy_from_user(&mreq,optval,len))
+               if (copy_from_user(&mreq, optval, len))
                        return -EFAULT;
                if (len < (mreq.mr_alen + offsetof(struct packet_mreq, mr_address)))
                        return -EINVAL;
@@ -1736,15 +1853,16 @@ 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:
        {
                struct tpacket_req req;
 
-               if (optlen<sizeof(req))
+               if (optlen < sizeof(req))
+                       return -EINVAL;
+               if (pkt_sk(sk)->has_vnet_hdr)
                        return -EINVAL;
-               if (copy_from_user(&req,optval,sizeof(req)))
+               if (copy_from_user(&req, optval, sizeof(req)))
                        return -EFAULT;
                return packet_set_ring(sk, &req, 0, optname == PACKET_TX_RING);
        }
@@ -1752,9 +1870,9 @@ packet_setsockopt(struct socket *sock, int level, int optname, char __user *optv
        {
                int val;
 
-               if (optlen!=sizeof(val))
+               if (optlen != sizeof(val))
                        return -EINVAL;
-               if (copy_from_user(&val,optval,sizeof(val)))
+               if (copy_from_user(&val, optval, sizeof(val)))
                        return -EFAULT;
 
                pkt_sk(sk)->copy_thresh = val;
@@ -1805,7 +1923,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;
@@ -1830,6 +1947,22 @@ packet_setsockopt(struct socket *sock, int level, int optname, char __user *optv
                po->origdev = !!val;
                return 0;
        }
+       case PACKET_VNET_HDR:
+       {
+               int val;
+
+               if (sock->type != SOCK_RAW)
+                       return -EINVAL;
+               if (po->rx_ring.pg_vec || po->tx_ring.pg_vec)
+                       return -EBUSY;
+               if (optlen < sizeof(val))
+                       return -EINVAL;
+               if (copy_from_user(&val, optval, sizeof(val)))
+                       return -EFAULT;
+
+               po->has_vnet_hdr = !!val;
+               return 0;
+       }
        default:
                return -ENOPROTOOPT;
        }
@@ -1880,7 +2013,13 @@ static int packet_getsockopt(struct socket *sock, int level, int optname,
 
                data = &val;
                break;
-#ifdef CONFIG_PACKET_MMAP
+       case PACKET_VNET_HDR:
+               if (len > sizeof(int))
+                       len = sizeof(int);
+               val = po->has_vnet_hdr;
+
+               data = &val;
+               break;
        case PACKET_VERSION:
                if (len > sizeof(int))
                        len = sizeof(int);
@@ -1916,7 +2055,6 @@ static int packet_getsockopt(struct socket *sock, int level, int optname,
                val = po->tp_loss;
                data = &val;
                break;
-#endif
        default:
                return -ENOPROTOOPT;
        }
@@ -1936,8 +2074,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) {
@@ -1965,18 +2103,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;
 }
 
@@ -1987,60 +2126,56 @@ static int packet_ioctl(struct socket *sock, unsigned int cmd,
        struct sock *sk = sock->sk;
 
        switch (cmd) {
-               case SIOCOUTQ:
-               {
-                       int amount = atomic_read(&sk->sk_wmem_alloc);
-                       return put_user(amount, (int __user *)arg);
-               }
-               case SIOCINQ:
-               {
-                       struct sk_buff *skb;
-                       int amount = 0;
-
-                       spin_lock_bh(&sk->sk_receive_queue.lock);
-                       skb = skb_peek(&sk->sk_receive_queue);
-                       if (skb)
-                               amount = skb->len;
-                       spin_unlock_bh(&sk->sk_receive_queue.lock);
-                       return put_user(amount, (int __user *)arg);
-               }
-               case SIOCGSTAMP:
-                       return sock_get_timestamp(sk, (struct timeval __user *)arg);
-               case SIOCGSTAMPNS:
-                       return sock_get_timestampns(sk, (struct timespec __user *)arg);
+       case SIOCOUTQ:
+       {
+               int amount = sk_wmem_alloc_get(sk);
+
+               return put_user(amount, (int __user *)arg);
+       }
+       case SIOCINQ:
+       {
+               struct sk_buff *skb;
+               int amount = 0;
+
+               spin_lock_bh(&sk->sk_receive_queue.lock);
+               skb = skb_peek(&sk->sk_receive_queue);
+               if (skb)
+                       amount = skb->len;
+               spin_unlock_bh(&sk->sk_receive_queue.lock);
+               return put_user(amount, (int __user *)arg);
+       }
+       case SIOCGSTAMP:
+               return sock_get_timestamp(sk, (struct timeval __user *)arg);
+       case SIOCGSTAMPNS:
+               return sock_get_timestampns(sk, (struct timespec __user *)arg);
 
 #ifdef CONFIG_INET
-               case SIOCADDRT:
-               case SIOCDELRT:
-               case SIOCDARP:
-               case SIOCGARP:
-               case SIOCSARP:
-               case SIOCGIFADDR:
-               case SIOCSIFADDR:
-               case SIOCGIFBRDADDR:
-               case SIOCSIFBRDADDR:
-               case SIOCGIFNETMASK:
-               case SIOCSIFNETMASK:
-               case SIOCGIFDSTADDR:
-               case SIOCSIFDSTADDR:
-               case SIOCSIFFLAGS:
-                       if (!net_eq(sock_net(sk), &init_net))
-                               return -ENOIOCTLCMD;
-                       return inet_dgram_ops.ioctl(sock, cmd, arg);
+       case SIOCADDRT:
+       case SIOCDELRT:
+       case SIOCDARP:
+       case SIOCGARP:
+       case SIOCSARP:
+       case SIOCGIFADDR:
+       case SIOCSIFADDR:
+       case SIOCGIFBRDADDR:
+       case SIOCSIFBRDADDR:
+       case SIOCGIFNETMASK:
+       case SIOCSIFNETMASK:
+       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
 
-               default:
-                       return -ENOIOCTLCMD;
+       default:
+               return -ENOIOCTLCMD;
        }
        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,
+static unsigned int packet_poll(struct file *file, struct socket *sock,
                                poll_table *wait)
 {
        struct sock *sk = sock->sk;
@@ -2070,7 +2205,7 @@ static unsigned int packet_poll(struct file * file, struct socket *sock,
 static void packet_mm_open(struct vm_area_struct *vma)
 {
        struct file *file = vma->vm_file;
-       struct socket * sock = file->private_data;
+       struct socket *sock = file->private_data;
        struct sock *sk = sock->sk;
 
        if (sk)
@@ -2080,16 +2215,16 @@ static void packet_mm_open(struct vm_area_struct *vma)
 static void packet_mm_close(struct vm_area_struct *vma)
 {
        struct file *file = vma->vm_file;
-       struct socket * sock = file->private_data;
+       struct socket *sock = file->private_data;
        struct sock *sk = sock->sk;
 
        if (sk)
                atomic_dec(&pkt_sk(sk)->mapped);
 }
 
-static struct vm_operations_struct packet_mmap_ops = {
-       .open = packet_mm_open,
-       .close =packet_mm_close,
+static const struct vm_operations_struct packet_mmap_ops = {
+       .open   =       packet_mm_open,
+       .close  =       packet_mm_close,
 };
 
 static void free_pg_vec(char **pg_vec, unsigned int order, unsigned int len)
@@ -2240,8 +2375,8 @@ static int packet_set_ring(struct sock *sk, struct tpacket_req *req,
                skb_queue_purge(rb_queue);
 #undef XC
                if (atomic_read(&po->mapped))
-                       printk(KERN_DEBUG "packet_mmap: vma is busy: %d\n",
-                                               atomic_read(&po->mapped));
+                       pr_err("packet_mmap: vma is busy: %d\n",
+                              atomic_read(&po->mapped));
        }
        mutex_unlock(&po->pg_vec_lock);
 
@@ -2304,7 +2439,7 @@ static int packet_mmap(struct file *file, struct socket *sock,
                        int pg_num;
 
                        for (pg_num = 0; pg_num < rb->pg_vec_pages;
-                                       pg_num++,page++) {
+                                       pg_num++, page++) {
                                err = vm_insert_page(vma, start, page);
                                if (unlikely(err))
                                        goto out;
@@ -2321,8 +2456,6 @@ out:
        mutex_unlock(&po->pg_vec_lock);
        return err;
 }
-#endif
-
 
 static const struct proto_ops packet_ops_spkt = {
        .family =       PF_PACKET,
@@ -2366,51 +2499,37 @@ static const struct proto_ops packet_ops = {
        .sendpage =     sock_no_sendpage,
 };
 
-static struct net_proto_family packet_family_ops = {
+static const struct net_proto_family packet_family_ops = {
        .family =       PF_PACKET,
        .create =       packet_create,
        .owner  =       THIS_MODULE,
 };
 
 static struct notifier_block packet_netdev_notifier = {
-       .notifier_call =packet_notifier,
+       .notifier_call =        packet_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)
@@ -2418,7 +2537,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,
@@ -2431,7 +2550,7 @@ static int packet_seq_show(struct seq_file *seq, void *v)
                           po->running,
                           atomic_read(&s->sk_rmem_alloc),
                           sock_i_uid(s),
-                          sock_i_ino(s) );
+                          sock_i_ino(s));
        }
 
        return 0;
@@ -2460,9 +2579,9 @@ static const struct file_operations packet_seq_fops = {
 
 #endif
 
-static int packet_net_init(struct net *net)
+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))
@@ -2471,7 +2590,7 @@ static int packet_net_init(struct net *net)
        return 0;
 }
 
-static void packet_net_exit(struct net *net)
+static void __net_exit packet_net_exit(struct net *net)
 {
        proc_net_remove(net, "packet");
 }