NET: am79c961: fix assembler warnings
[linux-2.6.git] / drivers / net / pppoe.c
index 58a26a4..718879b 100644 (file)
 #include <linux/proc_fs.h>
 #include <linux/seq_file.h>
 
+#include <linux/nsproxy.h>
 #include <net/net_namespace.h>
+#include <net/netns/generic.h>
 #include <net/sock.h>
 
 #include <asm/uaccess.h>
 
 #define PPPOE_HASH_BITS 4
-#define PPPOE_HASH_SIZE (1<<PPPOE_HASH_BITS)
+#define PPPOE_HASH_SIZE (1 << PPPOE_HASH_BITS)
+#define PPPOE_HASH_MASK        (PPPOE_HASH_SIZE - 1)
 
-static struct ppp_channel_ops pppoe_chan_ops;
-
-static int pppoe_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
-static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb);
 static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb);
 
 static const struct proto_ops pppoe_ops;
-static DEFINE_RWLOCK(pppoe_hash_lock);
+static const struct ppp_channel_ops pppoe_chan_ops;
+
+/* per-net private data for this module */
+static int pppoe_net_id __read_mostly;
+struct pppoe_net {
+       /*
+        * we could use _single_ hash table for all
+        * nets by injecting net id into the hash but
+        * it would increase hash chains and add
+        * a few additional math comparations messy
+        * as well, moreover in case of SMP less locking
+        * controversy here
+        */
+       struct pppox_sock *hash_table[PPPOE_HASH_SIZE];
+       rwlock_t hash_lock;
+};
+
+/*
+ * PPPoE could be in the following stages:
+ * 1) Discovery stage (to obtain remote MAC and Session ID)
+ * 2) Session stage (MAC and SID are known)
+ *
+ * Ethernet frames have a special tag for this but
+ * we use simpler approach based on session id
+ */
+static inline bool stage_session(__be16 sid)
+{
+       return sid != 0;
+}
+
+static inline struct pppoe_net *pppoe_pernet(struct net *net)
+{
+       BUG_ON(!net);
 
-static struct ppp_channel_ops pppoe_chan_ops;
+       return net_generic(net, pppoe_net_id);
+}
 
 static inline int cmp_2_addr(struct pppoe_addr *a, struct pppoe_addr *b)
 {
-       return (a->sid == b->sid &&
-               (memcmp(a->remote, b->remote, ETH_ALEN) == 0));
+       return a->sid == b->sid && !memcmp(a->remote, b->remote, ETH_ALEN);
 }
 
 static inline int cmp_addr(struct pppoe_addr *a, __be16 sid, char *addr)
 {
-       return (a->sid == sid &&
-               (memcmp(a->remote,addr,ETH_ALEN) == 0));
+       return a->sid == sid && !memcmp(a->remote, addr, ETH_ALEN);
 }
 
-#if 8%PPPOE_HASH_BITS
+#if 8 % PPPOE_HASH_BITS
 #error 8 must be a multiple of PPPOE_HASH_BITS
 #endif
 
@@ -118,69 +148,71 @@ static int hash_item(__be16 sid, unsigned char *addr)
        unsigned char hash = 0;
        unsigned int i;
 
-       for (i = 0 ; i < ETH_ALEN ; i++) {
+       for (i = 0; i < ETH_ALEN; i++)
                hash ^= addr[i];
-       }
-       for (i = 0 ; i < sizeof(sid_t)*8 ; i += 8 ){
-               hash ^= (__force __u32)sid>>i;
-       }
-       for (i = 8 ; (i>>=1) >= PPPOE_HASH_BITS ; ) {
-               hash ^= hash>>i;
-       }
+       for (i = 0; i < sizeof(sid_t) * 8; i += 8)
+               hash ^= (__force __u32)sid >> i;
+       for (i = 8; (i >>= 1) >= PPPOE_HASH_BITS;)
+               hash ^= hash >> i;
 
-       return hash & ( PPPOE_HASH_SIZE - 1 );
+       return hash & PPPOE_HASH_MASK;
 }
 
-/* zeroed because its in .bss */
-static struct pppox_sock *item_hash_table[PPPOE_HASH_SIZE];
-
 /**********************************************************************
  *
  *  Set/get/delete/rehash items  (internal versions)
  *
  **********************************************************************/
-static struct pppox_sock *__get_item(__be16 sid, unsigned char *addr, int ifindex)
+static struct pppox_sock *__get_item(struct pppoe_net *pn, __be16 sid,
+                               unsigned char *addr, int ifindex)
 {
        int hash = hash_item(sid, addr);
        struct pppox_sock *ret;
 
-       ret = item_hash_table[hash];
+       ret = pn->hash_table[hash];
+       while (ret) {
+               if (cmp_addr(&ret->pppoe_pa, sid, addr) &&
+                   ret->pppoe_ifindex == ifindex)
+                       return ret;
 
-       while (ret && !(cmp_addr(&ret->pppoe_pa, sid, addr) && ret->pppoe_ifindex == ifindex))
                ret = ret->next;
+       }
 
-       return ret;
+       return NULL;
 }
 
-static int __set_item(struct pppox_sock *po)
+static int __set_item(struct pppoe_net *pn, struct pppox_sock *po)
 {
        int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
        struct pppox_sock *ret;
 
-       ret = item_hash_table[hash];
+       ret = pn->hash_table[hash];
        while (ret) {
-               if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa) && ret->pppoe_ifindex == po->pppoe_ifindex)
+               if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa) &&
+                   ret->pppoe_ifindex == po->pppoe_ifindex)
                        return -EALREADY;
 
                ret = ret->next;
        }
 
-       po->next = item_hash_table[hash];
-       item_hash_table[hash] = po;
+       po->next = pn->hash_table[hash];
+       pn->hash_table[hash] = po;
 
        return 0;
 }
 
-static struct pppox_sock *__delete_item(__be16 sid, char *addr, int ifindex)
+static struct pppox_sock *__delete_item(struct pppoe_net *pn, __be16 sid,
+                                       char *addr, int ifindex)
 {
        int hash = hash_item(sid, addr);
        struct pppox_sock *ret, **src;
 
-       ret = item_hash_table[hash];
-       src = &item_hash_table[hash];
+       ret = pn->hash_table[hash];
+       src = &pn->hash_table[hash];
 
        while (ret) {
-               if (cmp_addr(&ret->pppoe_pa, sid, addr) && ret->pppoe_ifindex == ifindex) {
+               if (cmp_addr(&ret->pppoe_pa, sid, addr) &&
+                   ret->pppoe_ifindex == ifindex) {
                        *src = ret->next;
                        break;
                }
@@ -197,46 +229,53 @@ static struct pppox_sock *__delete_item(__be16 sid, char *addr, int ifindex)
  *  Set/get/delete/rehash items
  *
  **********************************************************************/
-static inline struct pppox_sock *get_item(__be16 sid,
-                                        unsigned char *addr, int ifindex)
+static inline struct pppox_sock *get_item(struct pppoe_net *pn, __be16 sid,
+                                       unsigned char *addr, int ifindex)
 {
        struct pppox_sock *po;
 
-       read_lock_bh(&pppoe_hash_lock);
-       po = __get_item(sid, addr, ifindex);
+       read_lock_bh(&pn->hash_lock);
+       po = __get_item(pn, sid, addr, ifindex);
        if (po)
                sock_hold(sk_pppox(po));
-       read_unlock_bh(&pppoe_hash_lock);
+       read_unlock_bh(&pn->hash_lock);
 
        return po;
 }
 
-static inline struct pppox_sock *get_item_by_addr(struct sockaddr_pppox *sp)
+static inline struct pppox_sock *get_item_by_addr(struct net *net,
+                                               struct sockaddr_pppox *sp)
 {
        struct net_device *dev;
+       struct pppoe_net *pn;
+       struct pppox_sock *pppox_sock = NULL;
+
        int ifindex;
 
-       dev = dev_get_by_name(&init_net, sp->sa_addr.pppoe.dev);
-       if(!dev)
-               return NULL;
-       ifindex = dev->ifindex;
-       dev_put(dev);
-       return get_item(sp->sa_addr.pppoe.sid, sp->sa_addr.pppoe.remote, ifindex);
+       rcu_read_lock();
+       dev = dev_get_by_name_rcu(net, sp->sa_addr.pppoe.dev);
+       if (dev) {
+               ifindex = dev->ifindex;
+               pn = pppoe_pernet(net);
+               pppox_sock = get_item(pn, sp->sa_addr.pppoe.sid,
+                               sp->sa_addr.pppoe.remote, ifindex);
+       }
+       rcu_read_unlock();
+       return pppox_sock;
 }
 
-static inline struct pppox_sock *delete_item(__be16 sid, char *addr, int ifindex)
+static inline struct pppox_sock *delete_item(struct pppoe_net *pn, __be16 sid,
+                                       char *addr, int ifindex)
 {
        struct pppox_sock *ret;
 
-       write_lock_bh(&pppoe_hash_lock);
-       ret = __delete_item(sid, addr, ifindex);
-       write_unlock_bh(&pppoe_hash_lock);
+       write_lock_bh(&pn->hash_lock);
+       ret = __delete_item(pn, sid, addr, ifindex);
+       write_unlock_bh(&pn->hash_lock);
 
        return ret;
 }
 
-
-
 /***************************************************************************
  *
  *  Handler for device events.
@@ -246,63 +285,66 @@ static inline struct pppox_sock *delete_item(__be16 sid, char *addr, int ifindex
 
 static void pppoe_flush_dev(struct net_device *dev)
 {
-       int hash;
-       BUG_ON(dev == NULL);
+       struct pppoe_net *pn;
+       int i;
 
-       write_lock_bh(&pppoe_hash_lock);
-       for (hash = 0; hash < PPPOE_HASH_SIZE; hash++) {
-               struct pppox_sock *po = item_hash_table[hash];
+       pn = pppoe_pernet(dev_net(dev));
+       write_lock_bh(&pn->hash_lock);
+       for (i = 0; i < PPPOE_HASH_SIZE; i++) {
+               struct pppox_sock *po = pn->hash_table[i];
+               struct sock *sk;
 
-               while (po != NULL) {
-                       struct sock *sk = sk_pppox(po);
-                       if (po->pppoe_dev != dev) {
+               while (po) {
+                       while (po && po->pppoe_dev != dev) {
                                po = po->next;
-                               continue;
                        }
-                       po->pppoe_dev = NULL;
-                       dev_put(dev);
 
+                       if (!po)
+                               break;
+
+                       sk = sk_pppox(po);
 
                        /* We always grab the socket lock, followed by the
-                        * pppoe_hash_lock, in that order.  Since we should
-                        * hold the sock lock while doing any unbinding,
-                        * we need to release the lock we're holding.
-                        * Hold a reference to the sock so it doesn't disappear
-                        * as we're jumping between locks.
+                        * hash_lock, in that order.  Since we should hold the
+                        * sock lock while doing any unbinding, we need to
+                        * release the lock we're holding.  Hold a reference to
+                        * the sock so it doesn't disappear as we're jumping
+                        * between locks.
                         */
 
                        sock_hold(sk);
-
-                       write_unlock_bh(&pppoe_hash_lock);
+                       write_unlock_bh(&pn->hash_lock);
                        lock_sock(sk);
 
-                       if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
+                       if (po->pppoe_dev == dev &&
+                           sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND | PPPOX_ZOMBIE)) {
                                pppox_unbind_sock(sk);
                                sk->sk_state = PPPOX_ZOMBIE;
                                sk->sk_state_change(sk);
+                               po->pppoe_dev = NULL;
+                               dev_put(dev);
                        }
 
                        release_sock(sk);
                        sock_put(sk);
 
-                       /* Restart scan at the beginning of this hash chain.
-                        * While the lock was dropped the chain contents may
-                        * have changed.
+                       /* Restart the process from the start of the current
+                        * hash chain. We dropped locks so the world may have
+                        * change from underneath us.
                         */
-                       write_lock_bh(&pppoe_hash_lock);
-                       po = item_hash_table[hash];
+
+                       BUG_ON(pppoe_pernet(dev_net(dev)) == NULL);
+                       write_lock_bh(&pn->hash_lock);
+                       po = pn->hash_table[i];
                }
        }
-       write_unlock_bh(&pppoe_hash_lock);
+       write_unlock_bh(&pn->hash_lock);
 }
 
 static int pppoe_device_event(struct notifier_block *this,
                              unsigned long event, void *ptr)
 {
-       struct net_device *dev = (struct net_device *) ptr;
-
-       if (dev_net(dev) != &init_net)
-               return NOTIFY_DONE;
+       struct net_device *dev = (struct net_device *)ptr;
 
        /* Only look at sockets that are using this specific device. */
        switch (event) {
@@ -319,17 +361,15 @@ static int pppoe_device_event(struct notifier_block *this,
 
        default:
                break;
-       };
+       }
 
        return NOTIFY_DONE;
 }
 
-
 static struct notifier_block pppoe_notifier = {
        .notifier_call = pppoe_device_event,
 };
 
-
 /************************************************************************
  *
  * Do the real work of receiving a PPPoE Session frame.
@@ -340,24 +380,22 @@ static int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb)
        struct pppox_sock *po = pppox_sk(sk);
        struct pppox_sock *relay_po;
 
-       if (sk->sk_state & PPPOX_BOUND) {
-               struct pppoe_hdr *ph = pppoe_hdr(skb);
-               int len = ntohs(ph->length);
-               skb_pull_rcsum(skb, sizeof(struct pppoe_hdr));
-               if (pskb_trim_rcsum(skb, len))
-                       goto abort_kfree;
+       /* Backlog receive. Semantics of backlog rcv preclude any code from
+        * executing in lock_sock()/release_sock() bounds; meaning sk->sk_state
+        * can't change.
+        */
 
+       if (sk->sk_state & PPPOX_BOUND) {
                ppp_input(&po->chan, skb);
        } else if (sk->sk_state & PPPOX_RELAY) {
-               relay_po = get_item_by_addr(&po->pppoe_relay);
-
+               relay_po = get_item_by_addr(sock_net(sk),
+                                           &po->pppoe_relay);
                if (relay_po == NULL)
                        goto abort_kfree;
 
                if ((sk_pppox(relay_po)->sk_state & PPPOX_CONNECTED) == 0)
                        goto abort_put;
 
-               skb_pull(skb, sizeof(struct pppoe_hdr));
                if (!__pppoe_xmit(sk_pppox(relay_po), skb))
                        goto abort_put;
        } else {
@@ -380,29 +418,42 @@ abort_kfree:
  * Receive wrapper called in BH context.
  *
  ***********************************************************************/
-static int pppoe_rcv(struct sk_buff *skb,
-                    struct net_device *dev,
-                    struct packet_type *pt,
-                    struct net_device *orig_dev)
-
+static int pppoe_rcv(struct sk_buff *skb, struct net_device *dev,
+                    struct packet_type *pt, struct net_device *orig_dev)
 {
        struct pppoe_hdr *ph;
        struct pppox_sock *po;
+       struct pppoe_net *pn;
+       int len;
 
-       if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
+       skb = skb_share_check(skb, GFP_ATOMIC);
+       if (!skb)
                goto out;
 
-       if (dev_net(dev) != &init_net)
-               goto drop;
-
        if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
                goto drop;
 
        ph = pppoe_hdr(skb);
+       len = ntohs(ph->length);
+
+       skb_pull_rcsum(skb, sizeof(*ph));
+       if (skb->len < len)
+               goto drop;
+
+       if (pskb_trim_rcsum(skb, len))
+               goto drop;
+
+       pn = pppoe_pernet(dev_net(dev));
+
+       /* Note that get_item does a sock_hold(), so sk_pppox(po)
+        * is known to be safe.
+        */
+       po = get_item(pn, ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
+       if (!po)
+               goto drop;
+
+       return sk_receive_skb(sk_pppox(po), skb, 0);
 
-       po = get_item(ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
-       if (po != NULL)
-               return sk_receive_skb(sk_pppox(po), skb, 0);
 drop:
        kfree_skb(skb);
 out:
@@ -415,29 +466,27 @@ out:
  * This is solely for detection of PADT frames
  *
  ***********************************************************************/
-static int pppoe_disc_rcv(struct sk_buff *skb,
-                         struct net_device *dev,
-                         struct packet_type *pt,
-                         struct net_device *orig_dev)
+static int pppoe_disc_rcv(struct sk_buff *skb, struct net_device *dev,
+                         struct packet_type *pt, struct net_device *orig_dev)
 
 {
        struct pppoe_hdr *ph;
        struct pppox_sock *po;
+       struct pppoe_net *pn;
 
-       if (dev_net(dev) != &init_net)
-               goto abort;
+       skb = skb_share_check(skb, GFP_ATOMIC);
+       if (!skb)
+               goto out;
 
        if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
                goto abort;
 
-       if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
-               goto out;
-
        ph = pppoe_hdr(skb);
        if (ph->code != PADT_CODE)
                goto abort;
 
-       po = get_item(ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
+       pn = pppoe_pernet(dev_net(dev));
+       po = get_item(pn, ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
        if (po) {
                struct sock *sk = sk_pppox(po);
 
@@ -465,17 +514,17 @@ out:
        return NET_RX_SUCCESS; /* Lies... :-) */
 }
 
-static struct packet_type pppoes_ptype = {
-       .type   = __constant_htons(ETH_P_PPP_SES),
+static struct packet_type pppoes_ptype __read_mostly = {
+       .type   = cpu_to_be16(ETH_P_PPP_SES),
        .func   = pppoe_rcv,
 };
 
-static struct packet_type pppoed_ptype = {
-       .type   = __constant_htons(ETH_P_PPP_DISC),
+static struct packet_type pppoed_ptype __read_mostly = {
+       .type   = cpu_to_be16(ETH_P_PPP_DISC),
        .func   = pppoe_disc_rcv,
 };
 
-static struct proto pppoe_sk_proto = {
+static struct proto pppoe_sk_proto __read_mostly = {
        .name     = "PPPOE",
        .owner    = THIS_MODULE,
        .obj_size = sizeof(struct pppox_sock),
@@ -488,66 +537,63 @@ static struct proto pppoe_sk_proto = {
  **********************************************************************/
 static int pppoe_create(struct net *net, struct socket *sock)
 {
-       int error = -ENOMEM;
        struct sock *sk;
 
        sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppoe_sk_proto);
        if (!sk)
-               goto out;
+               return -ENOMEM;
 
        sock_init_data(sock, sk);
 
-       sock->state = SS_UNCONNECTED;
-       sock->ops   = &pppoe_ops;
+       sock->state     = SS_UNCONNECTED;
+       sock->ops       = &pppoe_ops;
 
-       sk->sk_backlog_rcv = pppoe_rcv_core;
-       sk->sk_state       = PPPOX_NONE;
-       sk->sk_type        = SOCK_STREAM;
-       sk->sk_family      = PF_PPPOX;
-       sk->sk_protocol    = PX_PROTO_OE;
+       sk->sk_backlog_rcv      = pppoe_rcv_core;
+       sk->sk_state            = PPPOX_NONE;
+       sk->sk_type             = SOCK_STREAM;
+       sk->sk_family           = PF_PPPOX;
+       sk->sk_protocol         = PX_PROTO_OE;
 
-       error = 0;
-out:   return error;
+       return 0;
 }
 
 static int pppoe_release(struct socket *sock)
 {
        struct sock *sk = sock->sk;
        struct pppox_sock *po;
+       struct pppoe_net *pn;
+       struct net *net = NULL;
 
        if (!sk)
                return 0;
 
        lock_sock(sk);
-       if (sock_flag(sk, SOCK_DEAD)){
+       if (sock_flag(sk, SOCK_DEAD)) {
                release_sock(sk);
                return -EBADF;
        }
 
-       pppox_unbind_sock(sk);
-
-       /* Signal the death of the socket. */
-       sk->sk_state = PPPOX_DEAD;
-
-
-       /* Write lock on hash lock protects the entire "po" struct from
-        * concurrent updates via pppoe_flush_dev. The "po" struct should
-        * be considered part of the hash table contents, thus protected
-        * by the hash table lock */
-       write_lock_bh(&pppoe_hash_lock);
-
        po = pppox_sk(sk);
-       if (po->pppoe_pa.sid) {
-               __delete_item(po->pppoe_pa.sid,
-                             po->pppoe_pa.remote, po->pppoe_ifindex);
-       }
 
-       if (po->pppoe_dev) {
+       if (sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
                dev_put(po->pppoe_dev);
                po->pppoe_dev = NULL;
        }
 
-       write_unlock_bh(&pppoe_hash_lock);
+       pppox_unbind_sock(sk);
+
+       /* Signal the death of the socket. */
+       sk->sk_state = PPPOX_DEAD;
+
+       net = sock_net(sk);
+       pn = pppoe_pernet(net);
+
+       /*
+        * protect "po" from concurrent updates
+        * on pppoe_flush_dev
+        */
+       delete_item(pn, po->pppoe_pa.sid, po->pppoe_pa.remote,
+                   po->pppoe_ifindex);
 
        sock_orphan(sk);
        sock->sk = NULL;
@@ -559,14 +605,15 @@ static int pppoe_release(struct socket *sock)
        return 0;
 }
 
-
 static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
                  int sockaddr_len, int flags)
 {
        struct sock *sk = sock->sk;
-       struct net_device *dev;
-       struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
+       struct sockaddr_pppox *sp = (struct sockaddr_pppox *)uservaddr;
        struct pppox_sock *po = pppox_sk(sk);
+       struct net_device *dev = NULL;
+       struct pppoe_net *pn;
+       struct net *net = NULL;
        int error;
 
        lock_sock(sk);
@@ -577,44 +624,46 @@ static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
 
        /* Check for already bound sockets */
        error = -EBUSY;
-       if ((sk->sk_state & PPPOX_CONNECTED) && sp->sa_addr.pppoe.sid)
+       if ((sk->sk_state & PPPOX_CONNECTED) &&
+            stage_session(sp->sa_addr.pppoe.sid))
                goto end;
 
        /* Check for already disconnected sockets, on attempts to disconnect */
        error = -EALREADY;
-       if ((sk->sk_state & PPPOX_DEAD) && !sp->sa_addr.pppoe.sid )
+       if ((sk->sk_state & PPPOX_DEAD) &&
+            !stage_session(sp->sa_addr.pppoe.sid))
                goto end;
 
        error = 0;
-       if (po->pppoe_pa.sid) {
-               pppox_unbind_sock(sk);
 
-               /* Delete the old binding */
-               delete_item(po->pppoe_pa.sid,po->pppoe_pa.remote,po->pppoe_ifindex);
-
-               if(po->pppoe_dev)
+       /* Delete the old binding */
+       if (stage_session(po->pppoe_pa.sid)) {
+               pppox_unbind_sock(sk);
+               pn = pppoe_pernet(sock_net(sk));
+               delete_item(pn, po->pppoe_pa.sid,
+                           po->pppoe_pa.remote, po->pppoe_ifindex);
+               if (po->pppoe_dev) {
                        dev_put(po->pppoe_dev);
+                       po->pppoe_dev = NULL;
+               }
 
                memset(sk_pppox(po) + 1, 0,
                       sizeof(struct pppox_sock) - sizeof(struct sock));
-
                sk->sk_state = PPPOX_NONE;
        }
 
-       /* Don't re-bind if sid==0 */
-       if (sp->sa_addr.pppoe.sid != 0) {
-               dev = dev_get_by_name(&init_net, sp->sa_addr.pppoe.dev);
-
+       /* Re-bind in session stage only */
+       if (stage_session(sp->sa_addr.pppoe.sid)) {
                error = -ENODEV;
+               net = sock_net(sk);
+               dev = dev_get_by_name(net, sp->sa_addr.pppoe.dev);
                if (!dev)
-                       goto end;
+                       goto err_put;
 
                po->pppoe_dev = dev;
                po->pppoe_ifindex = dev->ifindex;
-
-               write_lock_bh(&pppoe_hash_lock);
-               if (!(dev->flags & IFF_UP)){
-                       write_unlock_bh(&pppoe_hash_lock);
+               pn = pppoe_pernet(net);
+               if (!(dev->flags & IFF_UP)) {
                        goto err_put;
                }
 
@@ -622,8 +671,9 @@ static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
                       &sp->sa_addr.pppoe,
                       sizeof(struct pppoe_addr));
 
-               error = __set_item(po);
-               write_unlock_bh(&pppoe_hash_lock);
+               write_lock_bh(&pn->hash_lock);
+               error = __set_item(pn, po);
+               write_unlock_bh(&pn->hash_lock);
                if (error < 0)
                        goto err_put;
 
@@ -634,16 +684,19 @@ static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
                po->chan.private = sk;
                po->chan.ops = &pppoe_chan_ops;
 
-               error = ppp_register_channel(&po->chan);
-               if (error)
+               error = ppp_register_net_channel(dev_net(dev), &po->chan);
+               if (error) {
+                       delete_item(pn, po->pppoe_pa.sid,
+                                   po->pppoe_pa.remote, po->pppoe_ifindex);
                        goto err_put;
+               }
 
                sk->sk_state = PPPOX_CONNECTED;
        }
 
        po->num = sp->sa_addr.pppoe.sid;
 
- end:
+end:
        release_sock(sk);
        return error;
 err_put:
@@ -654,7 +707,6 @@ err_put:
        goto end;
 }
 
-
 static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
                  int *usockaddr_len, int peer)
 {
@@ -673,7 +725,6 @@ static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
        return 0;
 }
 
-
 static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
                unsigned long arg)
 {
@@ -685,7 +736,6 @@ static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
        switch (cmd) {
        case PPPIOCGMRU:
                err = -ENXIO;
-
                if (!(sk->sk_state & PPPOX_CONNECTED))
                        break;
 
@@ -693,7 +743,7 @@ static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
                if (put_user(po->pppoe_dev->mtu -
                             sizeof(struct pppoe_hdr) -
                             PPP_HDRLEN,
-                            (int __user *) arg))
+                            (int __user *)arg))
                        break;
                err = 0;
                break;
@@ -704,7 +754,7 @@ static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
                        break;
 
                err = -EFAULT;
-               if (get_user(val,(int __user *) arg))
+               if (get_user(val, (int __user *)arg))
                        break;
 
                if (val < (po->pppoe_dev->mtu
@@ -717,7 +767,7 @@ static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
 
        case PPPIOCSFLAGS:
                err = -EFAULT;
-               if (get_user(val, (int __user *) arg))
+               if (get_user(val, (int __user *)arg))
                        break;
                err = 0;
                break;
@@ -744,13 +794,12 @@ static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
 
                err = -EINVAL;
                if (po->pppoe_relay.sa_family != AF_PPPOX ||
-                   po->pppoe_relay.sa_protocol!= PX_PROTO_OE)
+                   po->pppoe_relay.sa_protocol != PX_PROTO_OE)
                        break;
 
                /* Check that the socket referenced by the address
                   actually exists. */
-               relay_po = get_item_by_addr(&po->pppoe_relay);
-
+               relay_po = get_item_by_addr(sock_net(sk), &po->pppoe_relay);
                if (!relay_po)
                        break;
 
@@ -776,7 +825,6 @@ static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
        return err;
 }
 
-
 static int pppoe_sendmsg(struct kiocb *iocb, struct socket *sock,
                  struct msghdr *m, size_t total_len)
 {
@@ -803,7 +851,7 @@ static int pppoe_sendmsg(struct kiocb *iocb, struct socket *sock,
        dev = po->pppoe_dev;
 
        error = -EMSGSIZE;
-       if (total_len > (dev->mtu + dev->hard_header_len))
+       if (total_len > (dev->mtu + dev->hard_header_len))
                goto end;
 
 
@@ -821,13 +869,12 @@ static int pppoe_sendmsg(struct kiocb *iocb, struct socket *sock,
        skb->dev = dev;
 
        skb->priority = sk->sk_priority;
-       skb->protocol = __constant_htons(ETH_P_PPP_SES);
+       skb->protocol = cpu_to_be16(ETH_P_PPP_SES);
 
-       ph = (struct pppoe_hdr *) skb_put(skb, total_len + sizeof(struct pppoe_hdr));
-       start = (char *) &ph->tag[0];
+       ph = (struct pppoe_hdr *)skb_put(skb, total_len + sizeof(struct pppoe_hdr));
+       start = (char *)&ph->tag[0];
 
        error = memcpy_fromiovec(start, m->msg_iov, total_len);
-
        if (error < 0) {
                kfree_skb(skb);
                goto end;
@@ -848,7 +895,6 @@ end:
        return error;
 }
 
-
 /************************************************************************
  *
  * xmit function for internal use.
@@ -861,6 +907,14 @@ static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
        struct pppoe_hdr *ph;
        int data_len = skb->len;
 
+       /* The higher-level PPP code (ppp_unregister_channel()) ensures the PPP
+        * xmit operations conclude prior to an unregistration call.  Thus
+        * sk->sk_state cannot change, so we don't need to do lock_sock().
+        * But, we also can't do a lock_sock since that introduces a potential
+        * deadlock as we'd reverse the lock ordering used when calling
+        * ppp_unregister_channel().
+        */
+
        if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
                goto abort;
 
@@ -883,14 +937,13 @@ static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
        ph->sid = po->num;
        ph->length = htons(data_len);
 
-       skb->protocol = __constant_htons(ETH_P_PPP_SES);
+       skb->protocol = cpu_to_be16(ETH_P_PPP_SES);
        skb->dev = dev;
 
        dev_hard_header(skb, dev, ETH_P_PPP_SES,
                        po->pppoe_pa.remote, NULL, data_len);
 
        dev_queue_xmit(skb);
-
        return 1;
 
 abort:
@@ -898,7 +951,6 @@ abort:
        return 1;
 }
 
-
 /************************************************************************
  *
  * xmit function called by generic PPP driver
@@ -907,12 +959,11 @@ abort:
  ***********************************************************************/
 static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
 {
-       struct sock *sk = (struct sock *) chan->private;
+       struct sock *sk = (struct sock *)chan->private;
        return __pppoe_xmit(sk, skb);
 }
 
-
-static struct ppp_channel_ops pppoe_chan_ops = {
+static const struct ppp_channel_ops pppoe_chan_ops = {
        .start_xmit = pppoe_xmit,
 };
 
@@ -930,19 +981,16 @@ static int pppoe_recvmsg(struct kiocb *iocb, struct socket *sock,
 
        skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
                                flags & MSG_DONTWAIT, &error);
-
        if (error < 0)
                goto end;
 
        m->msg_namelen = 0;
 
        if (skb) {
-               struct pppoe_hdr *ph = pppoe_hdr(skb);
-               const int len = ntohs(ph->length);
-
-               error = memcpy_toiovec(m->msg_iov, (unsigned char *) &ph->tag[0], len);
+               total_len = min_t(size_t, total_len, skb->len);
+               error = skb_copy_datagram_iovec(skb, 0, m->msg_iov, total_len);
                if (error == 0)
-                       error = len;
+                       error = total_len;
        }
 
        kfree_skb(skb);
@@ -955,7 +1003,6 @@ static int pppoe_seq_show(struct seq_file *seq, void *v)
 {
        struct pppox_sock *po;
        char *dev_name;
-       DECLARE_MAC_BUF(mac);
 
        if (v == SEQ_START_TOKEN) {
                seq_puts(seq, "Id       Address              Device\n");
@@ -965,45 +1012,48 @@ static int pppoe_seq_show(struct seq_file *seq, void *v)
        po = v;
        dev_name = po->pppoe_pa.dev;
 
-       seq_printf(seq, "%08X %s %8s\n",
-                  po->pppoe_pa.sid, print_mac(mac, po->pppoe_pa.remote), dev_name);
+       seq_printf(seq, "%08X %pM %8s\n",
+               po->pppoe_pa.sid, po->pppoe_pa.remote, dev_name);
 out:
        return 0;
 }
 
-static __inline__ struct pppox_sock *pppoe_get_idx(loff_t pos)
+static inline struct pppox_sock *pppoe_get_idx(struct pppoe_net *pn, loff_t pos)
 {
        struct pppox_sock *po;
-       int i = 0;
+       int i;
 
-       for (; i < PPPOE_HASH_SIZE; i++) {
-               po = item_hash_table[i];
+       for (i = 0; i < PPPOE_HASH_SIZE; i++) {
+               po = pn->hash_table[i];
                while (po) {
                        if (!pos--)
                                goto out;
                        po = po->next;
                }
        }
+
 out:
        return po;
 }
 
 static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos)
-       __acquires(pppoe_hash_lock)
+       __acquires(pn->hash_lock)
 {
+       struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
        loff_t l = *pos;
 
-       read_lock_bh(&pppoe_hash_lock);
-       return l ? pppoe_get_idx(--l) : SEQ_START_TOKEN;
+       read_lock_bh(&pn->hash_lock);
+       return l ? pppoe_get_idx(pn, --l) : SEQ_START_TOKEN;
 }
 
 static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 {
+       struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
        struct pppox_sock *po;
 
        ++*pos;
        if (v == SEQ_START_TOKEN) {
-               po = pppoe_get_idx(0);
+               po = pppoe_get_idx(pn, 0);
                goto out;
        }
        po = v;
@@ -1012,23 +1062,26 @@ static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
        else {
                int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
 
+               po = NULL;
                while (++hash < PPPOE_HASH_SIZE) {
-                       po = item_hash_table[hash];
+                       po = pn->hash_table[hash];
                        if (po)
                                break;
                }
        }
+
 out:
        return po;
 }
 
 static void pppoe_seq_stop(struct seq_file *seq, void *v)
-       __releases(pppoe_hash_lock)
+       __releases(pn->hash_lock)
 {
-       read_unlock_bh(&pppoe_hash_lock);
+       struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
+       read_unlock_bh(&pn->hash_lock);
 }
 
-static struct seq_operations pppoe_seq_ops = {
+static const struct seq_operations pppoe_seq_ops = {
        .start          = pppoe_seq_start,
        .next           = pppoe_seq_next,
        .stop           = pppoe_seq_stop,
@@ -1037,7 +1090,8 @@ static struct seq_operations pppoe_seq_ops = {
 
 static int pppoe_seq_open(struct inode *inode, struct file *file)
 {
-       return seq_open(file, &pppoe_seq_ops);
+       return seq_open_net(inode, file, &pppoe_seq_ops,
+                       sizeof(struct seq_net_private));
 }
 
 static const struct file_operations pppoe_seq_fops = {
@@ -1045,84 +1099,103 @@ static const struct file_operations pppoe_seq_fops = {
        .open           = pppoe_seq_open,
        .read           = seq_read,
        .llseek         = seq_lseek,
-       .release        = seq_release,
+       .release        = seq_release_net,
+};
+
+#endif /* CONFIG_PROC_FS */
+
+static const struct proto_ops pppoe_ops = {
+       .family         = AF_PPPOX,
+       .owner          = THIS_MODULE,
+       .release        = pppoe_release,
+       .bind           = sock_no_bind,
+       .connect        = pppoe_connect,
+       .socketpair     = sock_no_socketpair,
+       .accept         = sock_no_accept,
+       .getname        = pppoe_getname,
+       .poll           = datagram_poll,
+       .listen         = sock_no_listen,
+       .shutdown       = sock_no_shutdown,
+       .setsockopt     = sock_no_setsockopt,
+       .getsockopt     = sock_no_getsockopt,
+       .sendmsg        = pppoe_sendmsg,
+       .recvmsg        = pppoe_recvmsg,
+       .mmap           = sock_no_mmap,
+       .ioctl          = pppox_ioctl,
 };
 
-static int __init pppoe_proc_init(void)
+static const struct pppox_proto pppoe_proto = {
+       .create = pppoe_create,
+       .ioctl  = pppoe_ioctl,
+       .owner  = THIS_MODULE,
+};
+
+static __net_init int pppoe_init_net(struct net *net)
 {
-       struct proc_dir_entry *p;
+       struct pppoe_net *pn = pppoe_pernet(net);
+       struct proc_dir_entry *pde;
 
-       p = proc_net_fops_create(&init_net, "pppoe", S_IRUGO, &pppoe_seq_fops);
-       if (!p)
+       rwlock_init(&pn->hash_lock);
+
+       pde = proc_net_fops_create(net, "pppoe", S_IRUGO, &pppoe_seq_fops);
+#ifdef CONFIG_PROC_FS
+       if (!pde)
                return -ENOMEM;
+#endif
+
        return 0;
 }
-#else /* CONFIG_PROC_FS */
-static inline int pppoe_proc_init(void) { return 0; }
-#endif /* CONFIG_PROC_FS */
 
-static const struct proto_ops pppoe_ops = {
-    .family            = AF_PPPOX,
-    .owner             = THIS_MODULE,
-    .release           = pppoe_release,
-    .bind              = sock_no_bind,
-    .connect           = pppoe_connect,
-    .socketpair                = sock_no_socketpair,
-    .accept            = sock_no_accept,
-    .getname           = pppoe_getname,
-    .poll              = datagram_poll,
-    .listen            = sock_no_listen,
-    .shutdown          = sock_no_shutdown,
-    .setsockopt                = sock_no_setsockopt,
-    .getsockopt                = sock_no_getsockopt,
-    .sendmsg           = pppoe_sendmsg,
-    .recvmsg           = pppoe_recvmsg,
-    .mmap              = sock_no_mmap,
-    .ioctl             = pppox_ioctl,
-};
+static __net_exit void pppoe_exit_net(struct net *net)
+{
+       proc_net_remove(net, "pppoe");
+}
 
-static struct pppox_proto pppoe_proto = {
-    .create    = pppoe_create,
-    .ioctl     = pppoe_ioctl,
-    .owner     = THIS_MODULE,
+static struct pernet_operations pppoe_net_ops = {
+       .init = pppoe_init_net,
+       .exit = pppoe_exit_net,
+       .id   = &pppoe_net_id,
+       .size = sizeof(struct pppoe_net),
 };
 
-
 static int __init pppoe_init(void)
 {
-       int err = proto_register(&pppoe_sk_proto, 0);
+       int err;
 
+       err = register_pernet_device(&pppoe_net_ops);
        if (err)
                goto out;
 
-       err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
+       err = proto_register(&pppoe_sk_proto, 0);
        if (err)
-               goto out_unregister_pppoe_proto;
+               goto out_unregister_net_ops;
 
-       err = pppoe_proc_init();
+       err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
        if (err)
-               goto out_unregister_pppox_proto;
+               goto out_unregister_pppoe_proto;
 
        dev_add_pack(&pppoes_ptype);
        dev_add_pack(&pppoed_ptype);
        register_netdevice_notifier(&pppoe_notifier);
-out:
-       return err;
-out_unregister_pppox_proto:
-       unregister_pppox_proto(PX_PROTO_OE);
+
+       return 0;
+
 out_unregister_pppoe_proto:
        proto_unregister(&pppoe_sk_proto);
-       goto out;
+out_unregister_net_ops:
+       unregister_pernet_device(&pppoe_net_ops);
+out:
+       return err;
 }
 
 static void __exit pppoe_exit(void)
 {
-       unregister_pppox_proto(PX_PROTO_OE);
-       dev_remove_pack(&pppoes_ptype);
-       dev_remove_pack(&pppoed_ptype);
        unregister_netdevice_notifier(&pppoe_notifier);
-       remove_proc_entry("pppoe", init_net.proc_net);
+       dev_remove_pack(&pppoed_ptype);
+       dev_remove_pack(&pppoes_ptype);
+       unregister_pppox_proto(PX_PROTO_OE);
        proto_unregister(&pppoe_sk_proto);
+       unregister_pernet_device(&pppoe_net_ops);
 }
 
 module_init(pppoe_init);