Merge branch 'linus' into cont_syslog
[safe/jmp/linux-2.6] / drivers / net / ppp_generic.c
index 83625fd..1b2c291 100644 (file)
@@ -27,6 +27,7 @@
 #include <linux/kmod.h>
 #include <linux/init.h>
 #include <linux/list.h>
+#include <linux/idr.h>
 #include <linux/netdevice.h>
 #include <linux/poll.h>
 #include <linux/ppp_defs.h>
 #include <linux/stddef.h>
 #include <linux/device.h>
 #include <linux/mutex.h>
+#include <linux/slab.h>
 #include <net/slhc_vj.h>
 #include <asm/atomic.h>
 
+#include <linux/nsproxy.h>
+#include <net/net_namespace.h>
+#include <net/netns/generic.h>
+
 #define PPP_VERSION    "2.4.2"
 
 /*
@@ -116,6 +122,7 @@ struct ppp {
        unsigned long   last_xmit;      /* jiffies when last pkt sent 9c */
        unsigned long   last_recv;      /* jiffies when last pkt rcvd a0 */
        struct net_device *dev;         /* network interface device a4 */
+       int             closing;        /* is device closing down? a8 */
 #ifdef CONFIG_PPP_MULTILINK
        int             nxchan;         /* next channel to send something on */
        u32             nxseq;          /* next sequence number to send */
@@ -129,6 +136,7 @@ struct ppp {
        struct sock_filter *active_filter;/* filter for pkts to reset idle */
        unsigned pass_len, active_len;
 #endif /* CONFIG_PPP_FILTER */
+       struct net      *ppp_net;       /* the net we belong to */
 };
 
 /*
@@ -153,12 +161,14 @@ struct channel {
        struct rw_semaphore chan_sem;   /* protects `chan' during chan ioctl */
        spinlock_t      downl;          /* protects `chan', file.xq dequeue */
        struct ppp      *ppp;           /* ppp unit we're connected to */
+       struct net      *chan_net;      /* the net channel belongs to */
        struct list_head clist;         /* link in list of channels per unit */
        rwlock_t        upl;            /* protects `ppp' */
 #ifdef CONFIG_PPP_MULTILINK
        u8              avail;          /* flag used in multilink stuff */
        u8              had_frag;       /* >= 1 fragments have been sent */
        u32             lastseq;        /* MP: last sequence # received */
+       int             speed;          /* speed of the corresponding ppp channel*/
 #endif /* CONFIG_PPP_MULTILINK */
 };
 
@@ -171,48 +181,35 @@ struct channel {
  * channel.downl.
  */
 
-/*
- * A cardmap represents a mapping from unsigned integers to pointers,
- * and provides a fast "find lowest unused number" operation.
- * It uses a broad (32-way) tree with a bitmap at each level.
- * It is designed to be space-efficient for small numbers of entries
- * and time-efficient for large numbers of entries.
- */
-#define CARDMAP_ORDER  5
-#define CARDMAP_WIDTH  (1U << CARDMAP_ORDER)
-#define CARDMAP_MASK   (CARDMAP_WIDTH - 1)
-
-struct cardmap {
-       int shift;
-       unsigned long inuse;
-       struct cardmap *parent;
-       void *ptr[CARDMAP_WIDTH];
-};
-static void *cardmap_get(struct cardmap *map, unsigned int nr);
-static int cardmap_set(struct cardmap **map, unsigned int nr, void *ptr);
-static unsigned int cardmap_find_first_free(struct cardmap *map);
-static void cardmap_destroy(struct cardmap **map);
-
-/*
- * all_ppp_mutex protects the all_ppp_units mapping.
- * It also ensures that finding a ppp unit in the all_ppp_units map
- * and updating its file.refcnt field is atomic.
- */
-static DEFINE_MUTEX(all_ppp_mutex);
-static struct cardmap *all_ppp_units;
 static atomic_t ppp_unit_count = ATOMIC_INIT(0);
-
-/*
- * all_channels_lock protects all_channels and last_channel_index,
- * and the atomicity of find a channel and updating its file.refcnt
- * field.
- */
-static DEFINE_SPINLOCK(all_channels_lock);
-static LIST_HEAD(all_channels);
-static LIST_HEAD(new_channels);
-static int last_channel_index;
 static atomic_t channel_count = ATOMIC_INIT(0);
 
+/* per-net private data for this module */
+static int ppp_net_id __read_mostly;
+struct ppp_net {
+       /* units to ppp mapping */
+       struct idr units_idr;
+
+       /*
+        * all_ppp_mutex protects the units_idr mapping.
+        * It also ensures that finding a ppp unit in the units_idr
+        * map and updating its file.refcnt field is atomic.
+        */
+       struct mutex all_ppp_mutex;
+
+       /* channels */
+       struct list_head all_channels;
+       struct list_head new_channels;
+       int last_channel_index;
+
+       /*
+        * all_channels_lock protects all_channels and
+        * last_channel_index, and the atomicity of find
+        * a channel and updating its file.refcnt field.
+        */
+       spinlock_t all_channels_lock;
+};
+
 /* Get the PPP protocol number from a skb */
 #define PPP_PROTO(skb) (((skb)->data[0] << 8) + (skb)->data[1])
 
@@ -236,8 +233,8 @@ static atomic_t channel_count = ATOMIC_INIT(0);
 #define seq_after(a, b)                ((s32)((a) - (b)) > 0)
 
 /* Prototypes. */
-static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
-                               unsigned int cmd, unsigned long arg);
+static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
+                       struct file *file, unsigned int cmd, unsigned long arg);
 static void ppp_xmit_process(struct ppp *ppp);
 static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
 static void ppp_push(struct ppp *ppp);
@@ -260,18 +257,30 @@ static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
 static void ppp_ccp_closed(struct ppp *ppp);
 static struct compressor *find_compressor(int type);
 static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
-static struct ppp *ppp_create_interface(int unit, int *retp);
+static struct ppp *ppp_create_interface(struct net *net, int unit, int *retp);
 static void init_ppp_file(struct ppp_file *pf, int kind);
 static void ppp_shutdown_interface(struct ppp *ppp);
 static void ppp_destroy_interface(struct ppp *ppp);
-static struct ppp *ppp_find_unit(int unit);
-static struct channel *ppp_find_channel(int unit);
+static struct ppp *ppp_find_unit(struct ppp_net *pn, int unit);
+static struct channel *ppp_find_channel(struct ppp_net *pn, int unit);
 static int ppp_connect_channel(struct channel *pch, int unit);
 static int ppp_disconnect_channel(struct channel *pch);
 static void ppp_destroy_channel(struct channel *pch);
+static int unit_get(struct idr *p, void *ptr);
+static int unit_set(struct idr *p, void *ptr, int n);
+static void unit_put(struct idr *p, int n);
+static void *unit_find(struct idr *p, int n);
 
 static struct class *ppp_class;
 
+/* per net-namespace data */
+static inline struct ppp_net *ppp_pernet(struct net *net)
+{
+       BUG_ON(!net);
+
+       return net_generic(net, ppp_net_id);
+}
+
 /* Translates a PPP protocol number to a NP index (NP == network protocol) */
 static inline int proto_to_npindex(int proto)
 {
@@ -363,7 +372,7 @@ static int ppp_open(struct inode *inode, struct file *file)
        return 0;
 }
 
-static int ppp_release(struct inode *inode, struct file *file)
+static int ppp_release(struct inode *unused, struct file *file)
 {
        struct ppp_file *pf = file->private_data;
        struct ppp *ppp;
@@ -396,6 +405,7 @@ static ssize_t ppp_read(struct file *file, char __user *buf,
        DECLARE_WAITQUEUE(wait, current);
        ssize_t ret;
        struct sk_buff *skb = NULL;
+       struct iovec iov;
 
        ret = count;
 
@@ -417,8 +427,8 @@ static ssize_t ppp_read(struct file *file, char __user *buf,
                         * network traffic (demand mode).
                         */
                        struct ppp *ppp = PF_TO_PPP(pf);
-                       if (ppp->n_channels == 0
-                           && (ppp->flags & SC_LOOP_TRAFFIC) == 0)
+                       if (ppp->n_channels == 0 &&
+                           (ppp->flags & SC_LOOP_TRAFFIC) == 0)
                                break;
                }
                ret = -EAGAIN;
@@ -439,7 +449,9 @@ static ssize_t ppp_read(struct file *file, char __user *buf,
        if (skb->len > count)
                goto outf;
        ret = -EFAULT;
-       if (copy_to_user(buf, skb->data, skb->len))
+       iov.iov_base = buf;
+       iov.iov_len = count;
+       if (skb_copy_datagram_iovec(skb, 0, &iov, skb->len))
                goto outf;
        ret = skb->len;
 
@@ -503,8 +515,8 @@ static unsigned int ppp_poll(struct file *file, poll_table *wait)
        else if (pf->kind == INTERFACE) {
                /* see comment in ppp_read */
                struct ppp *ppp = PF_TO_PPP(pf);
-               if (ppp->n_channels == 0
-                   && (ppp->flags & SC_LOOP_TRAFFIC) == 0)
+               if (ppp->n_channels == 0 &&
+                   (ppp->flags & SC_LOOP_TRAFFIC) == 0)
                        mask |= POLLIN | POLLRDNORM;
        }
 
@@ -547,8 +559,7 @@ static int get_filter(void __user *arg, struct sock_filter **p)
 }
 #endif /* CONFIG_PPP_FILTER */
 
-static int ppp_ioctl(struct inode *inode, struct file *file,
-                    unsigned int cmd, unsigned long arg)
+static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
        struct ppp_file *pf = file->private_data;
        struct ppp *ppp;
@@ -561,7 +572,8 @@ static int ppp_ioctl(struct inode *inode, struct file *file,
        int __user *p = argp;
 
        if (!pf)
-               return ppp_unattached_ioctl(pf, file, cmd, arg);
+               return ppp_unattached_ioctl(current->nsproxy->net_ns,
+                                       pf, file, cmd, arg);
 
        if (cmd == PPPIOCDETACH) {
                /*
@@ -576,24 +588,29 @@ static int ppp_ioctl(struct inode *inode, struct file *file,
                 * this fd and reopening /dev/ppp.
                 */
                err = -EINVAL;
+               lock_kernel();
                if (pf->kind == INTERFACE) {
                        ppp = PF_TO_PPP(pf);
                        if (file == ppp->owner)
                                ppp_shutdown_interface(ppp);
                }
-               if (atomic_read(&file->f_count) <= 2) {
-                       ppp_release(inode, file);
+               if (atomic_long_read(&file->f_count) <= 2) {
+                       ppp_release(NULL, file);
                        err = 0;
                } else
-                       printk(KERN_DEBUG "PPPIOCDETACH file->f_count=%d\n",
-                              atomic_read(&file->f_count));
+                       printk(KERN_DEBUG "PPPIOCDETACH file->f_count=%ld\n",
+                              atomic_long_read(&file->f_count));
+               unlock_kernel();
                return err;
        }
 
        if (pf->kind == CHANNEL) {
-               struct channel *pch = PF_TO_CHANNEL(pf);
+               struct channel *pch;
                struct ppp_channel *chan;
 
+               lock_kernel();
+               pch = PF_TO_CHANNEL(pf);
+
                switch (cmd) {
                case PPPIOCCONNECT:
                        if (get_user(unit, p))
@@ -613,6 +630,7 @@ static int ppp_ioctl(struct inode *inode, struct file *file,
                                err = chan->ops->ioctl(chan, cmd, arg);
                        up_read(&pch->chan_sem);
                }
+               unlock_kernel();
                return err;
        }
 
@@ -622,6 +640,7 @@ static int ppp_ioctl(struct inode *inode, struct file *file,
                return -EINVAL;
        }
 
+       lock_kernel();
        ppp = PF_TO_PPP(pf);
        switch (cmd) {
        case PPPIOCSMRU:
@@ -769,24 +788,26 @@ static int ppp_ioctl(struct inode *inode, struct file *file,
        default:
                err = -ENOTTY;
        }
-
+       unlock_kernel();
        return err;
 }
 
-static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
-                               unsigned int cmd, unsigned long arg)
+static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
+                       struct file *file, unsigned int cmd, unsigned long arg)
 {
        int unit, err = -EFAULT;
        struct ppp *ppp;
        struct channel *chan;
+       struct ppp_net *pn;
        int __user *p = (int __user *)arg;
 
+       lock_kernel();
        switch (cmd) {
        case PPPIOCNEWUNIT:
                /* Create a new ppp unit */
                if (get_user(unit, p))
                        break;
-               ppp = ppp_create_interface(unit, &err);
+               ppp = ppp_create_interface(net, unit, &err);
                if (!ppp)
                        break;
                file->private_data = &ppp->file;
@@ -801,34 +822,37 @@ static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
                /* Attach to an existing ppp unit */
                if (get_user(unit, p))
                        break;
-               mutex_lock(&all_ppp_mutex);
                err = -ENXIO;
-               ppp = ppp_find_unit(unit);
+               pn = ppp_pernet(net);
+               mutex_lock(&pn->all_ppp_mutex);
+               ppp = ppp_find_unit(pn, unit);
                if (ppp) {
                        atomic_inc(&ppp->file.refcnt);
                        file->private_data = &ppp->file;
                        err = 0;
                }
-               mutex_unlock(&all_ppp_mutex);
+               mutex_unlock(&pn->all_ppp_mutex);
                break;
 
        case PPPIOCATTCHAN:
                if (get_user(unit, p))
                        break;
-               spin_lock_bh(&all_channels_lock);
                err = -ENXIO;
-               chan = ppp_find_channel(unit);
+               pn = ppp_pernet(net);
+               spin_lock_bh(&pn->all_channels_lock);
+               chan = ppp_find_channel(pn, unit);
                if (chan) {
                        atomic_inc(&chan->file.refcnt);
                        file->private_data = &chan->file;
                        err = 0;
                }
-               spin_unlock_bh(&all_channels_lock);
+               spin_unlock_bh(&pn->all_channels_lock);
                break;
 
        default:
                err = -ENOTTY;
        }
+       unlock_kernel();
        return err;
 }
 
@@ -837,11 +861,40 @@ static const struct file_operations ppp_device_fops = {
        .read           = ppp_read,
        .write          = ppp_write,
        .poll           = ppp_poll,
-       .ioctl          = ppp_ioctl,
+       .unlocked_ioctl = ppp_ioctl,
        .open           = ppp_open,
        .release        = ppp_release
 };
 
+static __net_init int ppp_init_net(struct net *net)
+{
+       struct ppp_net *pn = net_generic(net, ppp_net_id);
+
+       idr_init(&pn->units_idr);
+       mutex_init(&pn->all_ppp_mutex);
+
+       INIT_LIST_HEAD(&pn->all_channels);
+       INIT_LIST_HEAD(&pn->new_channels);
+
+       spin_lock_init(&pn->all_channels_lock);
+
+       return 0;
+}
+
+static __net_exit void ppp_exit_net(struct net *net)
+{
+       struct ppp_net *pn = net_generic(net, ppp_net_id);
+
+       idr_destroy(&pn->units_idr);
+}
+
+static struct pernet_operations ppp_net_ops = {
+       .init = ppp_init_net,
+       .exit = ppp_exit_net,
+       .id   = &ppp_net_id,
+       .size = sizeof(struct ppp_net),
+};
+
 #define PPP_MAJOR      108
 
 /* Called at boot time if ppp is compiled into the kernel,
@@ -851,33 +904,45 @@ static int __init ppp_init(void)
        int err;
 
        printk(KERN_INFO "PPP generic driver version " PPP_VERSION "\n");
-       err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
-       if (!err) {
-               ppp_class = class_create(THIS_MODULE, "ppp");
-               if (IS_ERR(ppp_class)) {
-                       err = PTR_ERR(ppp_class);
-                       goto out_chrdev;
-               }
-               device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), "ppp");
+
+       err = register_pernet_device(&ppp_net_ops);
+       if (err) {
+               printk(KERN_ERR "failed to register PPP pernet device (%d)\n", err);
+               goto out;
        }
 
-out:
-       if (err)
+       err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
+       if (err) {
                printk(KERN_ERR "failed to register PPP device (%d)\n", err);
-       return err;
+               goto out_net;
+       }
+
+       ppp_class = class_create(THIS_MODULE, "ppp");
+       if (IS_ERR(ppp_class)) {
+               err = PTR_ERR(ppp_class);
+               goto out_chrdev;
+       }
+
+       /* not a big deal if we fail here :-) */
+       device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), NULL, "ppp");
+
+       return 0;
 
 out_chrdev:
        unregister_chrdev(PPP_MAJOR, "ppp");
-       goto out;
+out_net:
+       unregister_pernet_device(&ppp_net_ops);
+out:
+       return err;
 }
 
 /*
  * Network interface unit routines.
  */
-static int
+static netdev_tx_t
 ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
 {
-       struct ppp *ppp = (struct ppp *) dev->priv;
+       struct ppp *ppp = netdev_priv(dev);
        int npi, proto;
        unsigned char *pp;
 
@@ -911,18 +976,18 @@ ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
        netif_stop_queue(dev);
        skb_queue_tail(&ppp->file.xq, skb);
        ppp_xmit_process(ppp);
-       return 0;
+       return NETDEV_TX_OK;
 
  outf:
        kfree_skb(skb);
-       ++ppp->dev->stats.tx_dropped;
-       return 0;
+       ++dev->stats.tx_dropped;
+       return NETDEV_TX_OK;
 }
 
 static int
 ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
 {
-       struct ppp *ppp = dev->priv;
+       struct ppp *ppp = netdev_priv(dev);
        int err = -EFAULT;
        void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
        struct ppp_stats stats;
@@ -962,14 +1027,22 @@ ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
        return err;
 }
 
+static const struct net_device_ops ppp_netdev_ops = {
+       .ndo_start_xmit = ppp_start_xmit,
+       .ndo_do_ioctl   = ppp_net_ioctl,
+};
+
 static void ppp_setup(struct net_device *dev)
 {
+       dev->netdev_ops = &ppp_netdev_ops;
        dev->hard_header_len = PPP_HDRLEN;
        dev->mtu = PPP_MTU;
        dev->addr_len = 0;
        dev->tx_queue_len = 3;
        dev->type = ARPHRD_PPP;
        dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
+       dev->features |= NETIF_F_NETNS_LOCAL;
+       dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
 }
 
 /*
@@ -986,10 +1059,10 @@ ppp_xmit_process(struct ppp *ppp)
        struct sk_buff *skb;
 
        ppp_xmit_lock(ppp);
-       if (ppp->dev) {
+       if (!ppp->closing) {
                ppp_push(ppp);
-               while (!ppp->xmit_pending
-                      && (skb = skb_dequeue(&ppp->file.xq)))
+               while (!ppp->xmit_pending &&
+                      (skb = skb_dequeue(&ppp->file.xq)))
                        ppp_send_frame(ppp, skb);
                /* If there's no work left to do, tell the core net
                   code that we can accept some more. */
@@ -1068,18 +1141,18 @@ ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
                /* the filter instructions are constructed assuming
                   a four-byte PPP header on each packet */
                *skb_push(skb, 2) = 1;
-               if (ppp->pass_filter
-                   && sk_run_filter(skb, ppp->pass_filter,
-                                    ppp->pass_len) == 0) {
+               if (ppp->pass_filter &&
+                   sk_run_filter(skb, ppp->pass_filter,
+                                 ppp->pass_len) == 0) {
                        if (ppp->debug & 1)
                                printk(KERN_DEBUG "PPP: outbound frame not passed\n");
                        kfree_skb(skb);
                        return;
                }
                /* if this packet passes the active filter, record the time */
-               if (!(ppp->active_filter
-                     && sk_run_filter(skb, ppp->active_filter,
-                                      ppp->active_len) == 0))
+               if (!(ppp->active_filter &&
+                     sk_run_filter(skb, ppp->active_filter,
+                                   ppp->active_len) == 0))
                        ppp->last_xmit = jiffies;
                skb_pull(skb, 2);
 #else
@@ -1133,8 +1206,8 @@ ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
        }
 
        /* try to do packet compression */
-       if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state
-           && proto != PPP_LCP && proto != PPP_CCP) {
+       if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state &&
+           proto != PPP_LCP && proto != PPP_CCP) {
                if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
                        if (net_ratelimit())
                                printk(KERN_ERR "ppp: compression required but down - pkt dropped.\n");
@@ -1162,8 +1235,7 @@ ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
        return;
 
  drop:
-       if (skb)
-               kfree_skb(skb);
+       kfree_skb(skb);
        ++ppp->dev->stats.tx_errors;
 }
 
@@ -1225,42 +1297,56 @@ ppp_push(struct ppp *ppp)
  */
 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
 {
-       int len, fragsize;
+       int len, totlen;
        int i, bits, hdrlen, mtu;
        int flen;
-       int navail, nfree;
+       int navail, nfree, nzero;
        int nbigger;
+       int totspeed;
+       int totfree;
        unsigned char *p, *q;
        struct list_head *list;
        struct channel *pch;
        struct sk_buff *frag;
        struct ppp_channel *chan;
 
-       nfree = 0;      /* # channels which have no packet already queued */
-       navail = 0;     /* total # of usable channels (not deregistered) */
+       totspeed = 0; /*total bitrate of the bundle*/
+       nfree = 0; /* # channels which have no packet already queued */
+       navail = 0; /* total # of usable channels (not deregistered) */
+       nzero = 0; /* number of channels with zero speed associated*/
+       totfree = 0; /*total # of channels available and
+                                 *having no queued packets before
+                                 *starting the fragmentation*/
+
        hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
        i = 0;
        list_for_each_entry(pch, &ppp->channels, clist) {
                navail += pch->avail = (pch->chan != NULL);
+               pch->speed = pch->chan->speed;
                if (pch->avail) {
                        if (skb_queue_empty(&pch->file.xq) ||
-                           !pch->had_frag) {
-                               pch->avail = 2;
-                               ++nfree;
-                       }
+                               !pch->had_frag) {
+                                       if (pch->speed == 0)
+                                               nzero++;
+                                       else
+                                               totspeed += pch->speed;
+
+                                       pch->avail = 2;
+                                       ++nfree;
+                                       ++totfree;
+                               }
                        if (!pch->had_frag && i < ppp->nxchan)
                                ppp->nxchan = i;
                }
                ++i;
        }
-
        /*
         * Don't start sending this packet unless at least half of
         * the channels are free.  This gives much better TCP
         * performance if we have a lot of channels.
         */
        if (nfree == 0 || nfree < navail / 2)
-               return 0;       /* can't take now, leave it in xmit_pending */
+               return 0; /* can't take now, leave it in xmit_pending */
 
        /* Do protocol field compression (XXX this should be optional) */
        p = skb->data;
@@ -1270,18 +1356,7 @@ static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
                --len;
        }
 
-       /*
-        * Decide on fragment size.
-        * We create a fragment for each free channel regardless of
-        * how small they are (i.e. even 0 length) in order to minimize
-        * the time that it will take to detect when a channel drops
-        * a fragment.
-        */
-       fragsize = len;
-       if (nfree > 1)
-               fragsize = DIV_ROUND_UP(fragsize, nfree);
-       /* nbigger channels get fragsize bytes, the rest get fragsize-1,
-          except if nbigger==0, then they all get fragsize. */
+       totlen = len;
        nbigger = len % nfree;
 
        /* skip to the channel after the one we last used
@@ -1297,7 +1372,7 @@ static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
 
        /* create a fragment for each channel */
        bits = B;
-       while (nfree > 0 || len > 0) {
+       while (len > 0) {
                list = list->next;
                if (list == &ppp->channels) {
                        i = 0;
@@ -1316,7 +1391,6 @@ static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
                        if (nfree > 0)
                                continue;
                } else {
-                       --nfree;
                        pch->avail = 1;
                }
 
@@ -1324,28 +1398,72 @@ static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
                spin_lock_bh(&pch->downl);
                if (pch->chan == NULL) {
                        /* can't use this channel, it's being deregistered */
+                       if (pch->speed == 0)
+                               nzero--;
+                       else
+                               totspeed -= pch->speed;
+
                        spin_unlock_bh(&pch->downl);
                        pch->avail = 0;
+                       totlen = len;
+                       totfree--;
+                       nfree--;
                        if (--navail == 0)
                                break;
                        continue;
                }
 
                /*
-                * Create a fragment for this channel of
-                * min(max(mtu+2-hdrlen, 4), fragsize, len) bytes.
-                * If mtu+2-hdrlen < 4, that is a ridiculously small
-                * MTU, so we use mtu = 2 + hdrlen.
+               *if the channel speed is not set divide
+               *the packet evenly among the free channels;
+               *otherwise divide it according to the speed
+               *of the channel we are going to transmit on
+               */
+               flen = len;
+               if (nfree > 0) {
+                       if (pch->speed == 0) {
+                               flen = len/nfree;
+                               if (nbigger > 0) {
+                                       flen++;
+                                       nbigger--;
+                               }
+                       } else {
+                               flen = (((totfree - nzero)*(totlen + hdrlen*totfree)) /
+                                       ((totspeed*totfree)/pch->speed)) - hdrlen;
+                               if (nbigger > 0) {
+                                       flen += ((totfree - nzero)*pch->speed)/totspeed;
+                                       nbigger -= ((totfree - nzero)*pch->speed)/
+                                                       totspeed;
+                               }
+                       }
+                       nfree--;
+               }
+
+               /*
+                *check if we are on the last channel or
+                *we exceded the lenght of the data to
+                *fragment
+                */
+               if ((nfree <= 0) || (flen > len))
+                       flen = len;
+               /*
+                *it is not worth to tx on slow channels:
+                *in that case from the resulting flen according to the
+                *above formula will be equal or less than zero.
+                *Skip the channel in this case
                 */
-               if (fragsize > len)
-                       fragsize = len;
-               flen = fragsize;
-               mtu = pch->chan->mtu + 2 - hdrlen;
+               if (flen <= 0) {
+                       pch->avail = 2;
+                       spin_unlock_bh(&pch->downl);
+                       continue;
+               }
+
+               mtu = pch->chan->mtu - hdrlen;
                if (mtu < 4)
                        mtu = 4;
                if (flen > mtu)
                        flen = mtu;
-               if (flen == len && nfree == 0)
+               if (flen == len)
                        bits |= E;
                frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
                if (!frag)
@@ -1365,24 +1483,12 @@ static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
                        q[5] = ppp->nxseq;
                }
 
-               /*
-                * Copy the data in.
-                * Unfortunately there is a bug in older versions of
-                * the Linux PPP multilink reconstruction code where it
-                * drops 0-length fragments.  Therefore we make sure the
-                * fragment has at least one byte of data.  Any bytes
-                * we add in this situation will end up as padding on the
-                * end of the reconstructed packet.
-                */
-               if (flen == 0)
-                       *skb_put(frag, 1) = 0;
-               else
-                       memcpy(q + hdrlen, p, flen);
+               memcpy(q + hdrlen, p, flen);
 
                /* try to send it down the channel */
                chan = pch->chan;
                if (!skb_queue_empty(&pch->file.xq) ||
-                   !chan->ops->start_xmit(chan, frag))
+                       !chan->ops->start_xmit(chan, frag))
                        skb_queue_tail(&pch->file.xq, frag);
                pch->had_frag = 1;
                p += flen;
@@ -1390,9 +1496,6 @@ static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
                ++ppp->nxseq;
                bits = 0;
                spin_unlock_bh(&pch->downl);
-
-               if (--nbigger == 0 && fragsize > 0)
-                       --fragsize;
        }
        ppp->nxchan = i;
 
@@ -1454,8 +1557,7 @@ static inline void
 ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
 {
        ppp_recv_lock(ppp);
-       /* ppp->dev == 0 means interface is closing down */
-       if (ppp->dev)
+       if (!ppp->closing)
                ppp_receive_frame(ppp, skb, pch);
        else
                kfree_skb(skb);
@@ -1468,24 +1570,35 @@ ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
        struct channel *pch = chan->ppp;
        int proto;
 
-       if (!pch || skb->len == 0) {
+       if (!pch) {
                kfree_skb(skb);
                return;
        }
 
-       proto = PPP_PROTO(skb);
        read_lock_bh(&pch->upl);
+       if (!pskb_may_pull(skb, 2)) {
+               kfree_skb(skb);
+               if (pch->ppp) {
+                       ++pch->ppp->dev->stats.rx_length_errors;
+                       ppp_receive_error(pch->ppp);
+               }
+               goto done;
+       }
+
+       proto = PPP_PROTO(skb);
        if (!pch->ppp || proto >= 0xc000 || proto == PPP_CCPFRAG) {
                /* put it on the channel queue */
                skb_queue_tail(&pch->file.rq, skb);
                /* drop old frames if queue too long */
-               while (pch->file.rq.qlen > PPP_MAX_RQLEN
-                      && (skb = skb_dequeue(&pch->file.rq)))
+               while (pch->file.rq.qlen > PPP_MAX_RQLEN &&
+                      (skb = skb_dequeue(&pch->file.rq)))
                        kfree_skb(skb);
                wake_up_interruptible(&pch->file.rwait);
        } else {
                ppp_do_recv(pch->ppp, skb, pch);
        }
+
+done:
        read_unlock_bh(&pch->upl);
 }
 
@@ -1518,7 +1631,8 @@ ppp_input_error(struct ppp_channel *chan, int code)
 static void
 ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
 {
-       if (pskb_may_pull(skb, 2)) {
+       /* note: a 0-length skb is used as an error indication */
+       if (skb->len > 0) {
 #ifdef CONFIG_PPP_MULTILINK
                /* XXX do channel-level decompression here */
                if (PPP_PROTO(skb) == PPP_MP)
@@ -1526,15 +1640,10 @@ ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
                else
 #endif /* CONFIG_PPP_MULTILINK */
                        ppp_receive_nonmp_frame(ppp, skb);
-               return;
+       } else {
+               kfree_skb(skb);
+               ppp_receive_error(ppp);
        }
-
-       if (skb->len > 0)
-               /* note: a 0-length skb is used as an error indication */
-               ++ppp->dev->stats.rx_length_errors;
-
-       kfree_skb(skb);
-       ppp_receive_error(ppp);
 }
 
 static void
@@ -1556,8 +1665,8 @@ ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
         * Note that some decompressors need to see uncompressed frames
         * that come in as well as compressed frames.
         */
-       if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)
-           && (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
+       if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN) &&
+           (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
                skb = ppp_decompress_frame(ppp, skb);
 
        if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
@@ -1628,8 +1737,8 @@ ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
                /* control or unknown frame - pass it to pppd */
                skb_queue_tail(&ppp->file.rq, skb);
                /* limit queue length by dropping old frames */
-               while (ppp->file.rq.qlen > PPP_MAX_RQLEN
-                      && (skb = skb_dequeue(&ppp->file.rq)))
+               while (ppp->file.rq.qlen > PPP_MAX_RQLEN &&
+                      (skb = skb_dequeue(&ppp->file.rq)))
                        kfree_skb(skb);
                /* wake up any process polling or blocking on read */
                wake_up_interruptible(&ppp->file.rwait);
@@ -1647,26 +1756,26 @@ ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
                                goto err;
 
                        *skb_push(skb, 2) = 0;
-                       if (ppp->pass_filter
-                           && sk_run_filter(skb, ppp->pass_filter,
-                                            ppp->pass_len) == 0) {
+                       if (ppp->pass_filter &&
+                           sk_run_filter(skb, ppp->pass_filter,
+                                         ppp->pass_len) == 0) {
                                if (ppp->debug & 1)
                                        printk(KERN_DEBUG "PPP: inbound frame "
                                               "not passed\n");
                                kfree_skb(skb);
                                return;
                        }
-                       if (!(ppp->active_filter
-                             && sk_run_filter(skb, ppp->active_filter,
-                                              ppp->active_len) == 0))
+                       if (!(ppp->active_filter &&
+                             sk_run_filter(skb, ppp->active_filter,
+                                           ppp->active_len) == 0))
                                ppp->last_recv = jiffies;
                        __skb_pull(skb, 2);
                } else
 #endif /* CONFIG_PPP_FILTER */
                        ppp->last_recv = jiffies;
 
-               if ((ppp->dev->flags & IFF_UP) == 0
-                   || ppp->npmode[npi] != NPMODE_PASS) {
+               if ((ppp->dev->flags & IFF_UP) == 0 ||
+                   ppp->npmode[npi] != NPMODE_PASS) {
                        kfree_skb(skb);
                } else {
                        /* chop off protocol */
@@ -1675,7 +1784,6 @@ ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
                        skb->protocol = htons(npindex_to_ethertype[npi]);
                        skb_reset_mac_header(skb);
                        netif_rx(skb);
-                       ppp->dev->last_rx = jiffies;
                }
        }
        return;
@@ -1824,13 +1932,22 @@ ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
 
        /* If the queue is getting long, don't wait any longer for packets
           before the start of the queue. */
-       if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN
-           && seq_before(ppp->minseq, ppp->mrq.next->sequence))
-               ppp->minseq = ppp->mrq.next->sequence;
+       if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN) {
+               struct sk_buff *skb = skb_peek(&ppp->mrq);
+               if (seq_before(ppp->minseq, skb->sequence))
+                       ppp->minseq = skb->sequence;
+       }
 
        /* Pull completed packets off the queue and receive them. */
-       while ((skb = ppp_mp_reconstruct(ppp)))
-               ppp_receive_nonmp_frame(ppp, skb);
+       while ((skb = ppp_mp_reconstruct(ppp))) {
+               if (pskb_may_pull(skb, 2))
+                       ppp_receive_nonmp_frame(ppp, skb);
+               else {
+                       ++ppp->dev->stats.rx_length_errors;
+                       kfree_skb(skb);
+                       ppp_receive_error(ppp);
+               }
+       }
 
        return;
 
@@ -1852,10 +1969,11 @@ ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
 
        /* N.B. we don't need to lock the list lock because we have the
           ppp unit receive-side lock. */
-       for (p = list->next; p != (struct sk_buff *)list; p = p->next)
+       skb_queue_walk(list, p) {
                if (seq_before(seq, p->sequence))
                        break;
-       __skb_insert(skb, p->prev, p, list);
+       }
+       __skb_queue_before(list, p, skb);
 }
 
 /*
@@ -1986,19 +2104,27 @@ ppp_mp_reconstruct(struct ppp *ppp)
  * Channel interface.
  */
 
-/*
- * Create a new, unattached ppp channel.
- */
-int
-ppp_register_channel(struct ppp_channel *chan)
+/* Create a new, unattached ppp channel. */
+int ppp_register_channel(struct ppp_channel *chan)
+{
+       return ppp_register_net_channel(current->nsproxy->net_ns, chan);
+}
+
+/* Create a new, unattached ppp channel for specified net. */
+int ppp_register_net_channel(struct net *net, struct ppp_channel *chan)
 {
        struct channel *pch;
+       struct ppp_net *pn;
 
        pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
        if (!pch)
                return -ENOMEM;
+
+       pn = ppp_pernet(net);
+
        pch->ppp = NULL;
        pch->chan = chan;
+       pch->chan_net = net;
        chan->ppp = pch;
        init_ppp_file(&pch->file, CHANNEL);
        pch->file.hdrlen = chan->hdrlen;
@@ -2008,11 +2134,13 @@ ppp_register_channel(struct ppp_channel *chan)
        init_rwsem(&pch->chan_sem);
        spin_lock_init(&pch->downl);
        rwlock_init(&pch->upl);
-       spin_lock_bh(&all_channels_lock);
-       pch->file.index = ++last_channel_index;
-       list_add(&pch->list, &new_channels);
+
+       spin_lock_bh(&pn->all_channels_lock);
+       pch->file.index = ++pn->last_channel_index;
+       list_add(&pch->list, &pn->new_channels);
        atomic_inc(&channel_count);
-       spin_unlock_bh(&all_channels_lock);
+       spin_unlock_bh(&pn->all_channels_lock);
+
        return 0;
 }
 
@@ -2046,6 +2174,24 @@ int ppp_unit_number(struct ppp_channel *chan)
 }
 
 /*
+ * Return the PPP device interface name of a channel.
+ */
+char *ppp_dev_name(struct ppp_channel *chan)
+{
+       struct channel *pch = chan->ppp;
+       char *name = NULL;
+
+       if (pch) {
+               read_lock_bh(&pch->upl);
+               if (pch->ppp && pch->ppp->dev)
+                       name = pch->ppp->dev->name;
+               read_unlock_bh(&pch->upl);
+       }
+       return name;
+}
+
+
+/*
  * Disconnect a channel from the generic layer.
  * This must be called in process context.
  */
@@ -2053,9 +2199,11 @@ void
 ppp_unregister_channel(struct ppp_channel *chan)
 {
        struct channel *pch = chan->ppp;
+       struct ppp_net *pn;
 
        if (!pch)
                return;         /* should never happen */
+
        chan->ppp = NULL;
 
        /*
@@ -2068,9 +2216,12 @@ ppp_unregister_channel(struct ppp_channel *chan)
        spin_unlock_bh(&pch->downl);
        up_write(&pch->chan_sem);
        ppp_disconnect_channel(pch);
-       spin_lock_bh(&all_channels_lock);
+
+       pn = ppp_pernet(pch->chan_net);
+       spin_lock_bh(&pn->all_channels_lock);
        list_del(&pch->list);
-       spin_unlock_bh(&all_channels_lock);
+       spin_unlock_bh(&pn->all_channels_lock);
+
        pch->file.dead = 1;
        wake_up_interruptible(&pch->file.rwait);
        if (atomic_dec_and_test(&pch->file.refcnt))
@@ -2106,22 +2257,18 @@ ppp_set_compress(struct ppp *ppp, unsigned long arg)
        unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
 
        err = -EFAULT;
-       if (copy_from_user(&data, (void __user *) arg, sizeof(data))
-           || (data.length <= CCP_MAX_OPTION_LENGTH
-               && copy_from_user(ccp_option, (void __user *) data.ptr, data.length)))
+       if (copy_from_user(&data, (void __user *) arg, sizeof(data)) ||
+           (data.length <= CCP_MAX_OPTION_LENGTH &&
+            copy_from_user(ccp_option, (void __user *) data.ptr, data.length)))
                goto out;
        err = -EINVAL;
-       if (data.length > CCP_MAX_OPTION_LENGTH
-           || ccp_option[1] < 2 || ccp_option[1] > data.length)
+       if (data.length > CCP_MAX_OPTION_LENGTH ||
+           ccp_option[1] < 2 || ccp_option[1] > data.length)
                goto out;
 
-       cp = find_compressor(ccp_option[0]);
-#ifdef CONFIG_KMOD
-       if (!cp) {
-               request_module("ppp-compress-%d", ccp_option[0]);
-               cp = find_compressor(ccp_option[0]);
-       }
-#endif /* CONFIG_KMOD */
+       cp = try_then_request_module(
+               find_compressor(ccp_option[0]),
+               "ppp-compress-%d", ccp_option[0]);
        if (!cp)
                goto out;
 
@@ -2399,20 +2546,22 @@ ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
  * unit == -1 means allocate a new number.
  */
 static struct ppp *
-ppp_create_interface(int unit, int *retp)
+ppp_create_interface(struct net *net, int unit, int *retp)
 {
        struct ppp *ppp;
+       struct ppp_net *pn;
        struct net_device *dev = NULL;
        int ret = -ENOMEM;
        int i;
 
-       ppp = kzalloc(sizeof(struct ppp), GFP_KERNEL);
-       if (!ppp)
-               goto out;
-       dev = alloc_netdev(0, "", ppp_setup);
+       dev = alloc_netdev(sizeof(struct ppp), "", ppp_setup);
        if (!dev)
                goto out1;
 
+       pn = ppp_pernet(net);
+
+       ppp = netdev_priv(dev);
+       ppp->dev = dev;
        ppp->mru = PPP_MRU;
        init_ppp_file(&ppp->file, INTERFACE);
        ppp->file.hdrlen = PPP_HDRLEN - 2;      /* don't count proto bytes */
@@ -2425,18 +2574,38 @@ ppp_create_interface(int unit, int *retp)
        ppp->minseq = -1;
        skb_queue_head_init(&ppp->mrq);
 #endif /* CONFIG_PPP_MULTILINK */
-       ppp->dev = dev;
-       dev->priv = ppp;
 
-       dev->hard_start_xmit = ppp_start_xmit;
-       dev->do_ioctl = ppp_net_ioctl;
+       /*
+        * drum roll: don't forget to set
+        * the net device is belong to
+        */
+       dev_net_set(dev, net);
 
        ret = -EEXIST;
-       mutex_lock(&all_ppp_mutex);
-       if (unit < 0)
-               unit = cardmap_find_first_free(all_ppp_units);
-       else if (cardmap_get(all_ppp_units, unit) != NULL)
-               goto out2;      /* unit already exists */
+       mutex_lock(&pn->all_ppp_mutex);
+
+       if (unit < 0) {
+               unit = unit_get(&pn->units_idr, ppp);
+               if (unit < 0) {
+                       *retp = unit;
+                       goto out2;
+               }
+       } else {
+               if (unit_find(&pn->units_idr, unit))
+                       goto out2; /* unit already exists */
+               /*
+                * if caller need a specified unit number
+                * lets try to satisfy him, otherwise --
+                * he should better ask us for new unit number
+                *
+                * NOTE: yes I know that returning EEXIST it's not
+                * fair but at least pppd will ask us to allocate
+                * new unit in this case so user is happy :)
+                */
+               unit = unit_set(&pn->units_idr, ppp, unit);
+               if (unit < 0)
+                       goto out2;
+       }
 
        /* Initialize the new ppp unit */
        ppp->file.index = unit;
@@ -2444,29 +2613,24 @@ ppp_create_interface(int unit, int *retp)
 
        ret = register_netdev(dev);
        if (ret != 0) {
+               unit_put(&pn->units_idr, unit);
                printk(KERN_ERR "PPP: couldn't register device %s (%d)\n",
                       dev->name, ret);
                goto out2;
        }
 
+       ppp->ppp_net = net;
+
        atomic_inc(&ppp_unit_count);
-       ret = cardmap_set(&all_ppp_units, unit, ppp);
-       if (ret != 0)
-               goto out3;
+       mutex_unlock(&pn->all_ppp_mutex);
 
-       mutex_unlock(&all_ppp_mutex);
        *retp = 0;
        return ppp;
 
-out3:
-       atomic_dec(&ppp_unit_count);
-       unregister_netdev(dev);
 out2:
-       mutex_unlock(&all_ppp_mutex);
+       mutex_unlock(&pn->all_ppp_mutex);
        free_netdev(dev);
 out1:
-       kfree(ppp);
-out:
        *retp = ret;
        return NULL;
 }
@@ -2490,23 +2654,26 @@ init_ppp_file(struct ppp_file *pf, int kind)
  */
 static void ppp_shutdown_interface(struct ppp *ppp)
 {
-       struct net_device *dev;
+       struct ppp_net *pn;
+
+       pn = ppp_pernet(ppp->ppp_net);
+       mutex_lock(&pn->all_ppp_mutex);
 
-       mutex_lock(&all_ppp_mutex);
-       ppp_lock(ppp);
-       dev = ppp->dev;
-       ppp->dev = NULL;
-       ppp_unlock(ppp);
        /* This will call dev_close() for us. */
-       if (dev) {
-               unregister_netdev(dev);
-               free_netdev(dev);
-       }
-       cardmap_set(&all_ppp_units, ppp->file.index, NULL);
+       ppp_lock(ppp);
+       if (!ppp->closing) {
+               ppp->closing = 1;
+               ppp_unlock(ppp);
+               unregister_netdev(ppp->dev);
+       } else
+               ppp_unlock(ppp);
+
+       unit_put(&pn->units_idr, ppp->file.index);
        ppp->file.dead = 1;
        ppp->owner = NULL;
        wake_up_interruptible(&ppp->file.rwait);
-       mutex_unlock(&all_ppp_mutex);
+
+       mutex_unlock(&pn->all_ppp_mutex);
 }
 
 /*
@@ -2543,10 +2710,9 @@ static void ppp_destroy_interface(struct ppp *ppp)
        ppp->active_filter = NULL;
 #endif /* CONFIG_PPP_FILTER */
 
-       if (ppp->xmit_pending)
-               kfree_skb(ppp->xmit_pending);
+       kfree_skb(ppp->xmit_pending);
 
-       kfree(ppp);
+       free_netdev(ppp->dev);
 }
 
 /*
@@ -2554,9 +2720,9 @@ static void ppp_destroy_interface(struct ppp *ppp)
  * The caller should have locked the all_ppp_mutex.
  */
 static struct ppp *
-ppp_find_unit(int unit)
+ppp_find_unit(struct ppp_net *pn, int unit)
 {
-       return cardmap_get(all_ppp_units, unit);
+       return unit_find(&pn->units_idr, unit);
 }
 
 /*
@@ -2568,20 +2734,22 @@ ppp_find_unit(int unit)
  * when we have a lot of channels in use.
  */
 static struct channel *
-ppp_find_channel(int unit)
+ppp_find_channel(struct ppp_net *pn, int unit)
 {
        struct channel *pch;
 
-       list_for_each_entry(pch, &new_channels, list) {
+       list_for_each_entry(pch, &pn->new_channels, list) {
                if (pch->file.index == unit) {
-                       list_move(&pch->list, &all_channels);
+                       list_move(&pch->list, &pn->all_channels);
                        return pch;
                }
        }
-       list_for_each_entry(pch, &all_channels, list) {
+
+       list_for_each_entry(pch, &pn->all_channels, list) {
                if (pch->file.index == unit)
                        return pch;
        }
+
        return NULL;
 }
 
@@ -2592,11 +2760,14 @@ static int
 ppp_connect_channel(struct channel *pch, int unit)
 {
        struct ppp *ppp;
+       struct ppp_net *pn;
        int ret = -ENXIO;
        int hdrlen;
 
-       mutex_lock(&all_ppp_mutex);
-       ppp = ppp_find_unit(unit);
+       pn = ppp_pernet(pch->chan_net);
+
+       mutex_lock(&pn->all_ppp_mutex);
+       ppp = ppp_find_unit(pn, unit);
        if (!ppp)
                goto out;
        write_lock_bh(&pch->upl);
@@ -2608,7 +2779,7 @@ ppp_connect_channel(struct channel *pch, int unit)
        if (pch->file.hdrlen > ppp->file.hdrlen)
                ppp->file.hdrlen = pch->file.hdrlen;
        hdrlen = pch->file.hdrlen + 2;  /* for protocol bytes */
-       if (ppp->dev && hdrlen > ppp->dev->hard_header_len)
+       if (hdrlen > ppp->dev->hard_header_len)
                ppp->dev->hard_header_len = hdrlen;
        list_add_tail(&pch->clist, &ppp->channels);
        ++ppp->n_channels;
@@ -2620,7 +2791,7 @@ ppp_connect_channel(struct channel *pch, int unit)
  outl:
        write_unlock_bh(&pch->upl);
  out:
-       mutex_unlock(&all_ppp_mutex);
+       mutex_unlock(&pn->all_ppp_mutex);
        return ret;
 }
 
@@ -2674,123 +2845,68 @@ static void __exit ppp_cleanup(void)
        /* should never happen */
        if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
                printk(KERN_ERR "PPP: removing module but units remain!\n");
-       cardmap_destroy(&all_ppp_units);
        unregister_chrdev(PPP_MAJOR, "ppp");
        device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
        class_destroy(ppp_class);
+       unregister_pernet_device(&ppp_net_ops);
 }
 
 /*
- * Cardmap implementation.
+ * Units handling. Caller must protect concurrent access
+ * by holding all_ppp_mutex
  */
-static void *cardmap_get(struct cardmap *map, unsigned int nr)
+
+/* associate pointer with specified number */
+static int unit_set(struct idr *p, void *ptr, int n)
 {
-       struct cardmap *p;
-       int i;
+       int unit, err;
 
-       for (p = map; p != NULL; ) {
-               if ((i = nr >> p->shift) >= CARDMAP_WIDTH)
-                       return NULL;
-               if (p->shift == 0)
-                       return p->ptr[i];
-               nr &= ~(CARDMAP_MASK << p->shift);
-               p = p->ptr[i];
+again:
+       if (!idr_pre_get(p, GFP_KERNEL)) {
+               printk(KERN_ERR "PPP: No free memory for idr\n");
+               return -ENOMEM;
        }
-       return NULL;
-}
 
-static int cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
-{
-       struct cardmap *p;
-       int i;
+       err = idr_get_new_above(p, ptr, n, &unit);
+       if (err == -EAGAIN)
+               goto again;
 
-       p = *pmap;
-       if (p == NULL || (nr >> p->shift) >= CARDMAP_WIDTH) {
-               do {
-                       /* need a new top level */
-                       struct cardmap *np = kzalloc(sizeof(*np), GFP_KERNEL);
-                       if (!np)
-                               goto enomem;
-                       np->ptr[0] = p;
-                       if (p != NULL) {
-                               np->shift = p->shift + CARDMAP_ORDER;
-                               p->parent = np;
-                       } else
-                               np->shift = 0;
-                       p = np;
-               } while ((nr >> p->shift) >= CARDMAP_WIDTH);
-               *pmap = p;
-       }
-       while (p->shift > 0) {
-               i = (nr >> p->shift) & CARDMAP_MASK;
-               if (p->ptr[i] == NULL) {
-                       struct cardmap *np = kzalloc(sizeof(*np), GFP_KERNEL);
-                       if (!np)
-                               goto enomem;
-                       np->shift = p->shift - CARDMAP_ORDER;
-                       np->parent = p;
-                       p->ptr[i] = np;
-               }
-               if (ptr == NULL)
-                       clear_bit(i, &p->inuse);
-               p = p->ptr[i];
-       }
-       i = nr & CARDMAP_MASK;
-       p->ptr[i] = ptr;
-       if (ptr != NULL)
-               set_bit(i, &p->inuse);
-       else
-               clear_bit(i, &p->inuse);
-       return 0;
- enomem:
-       return -ENOMEM;
+       if (unit != n) {
+               idr_remove(p, unit);
+               return -EINVAL;
+       }
+
+       return unit;
 }
 
-static unsigned int cardmap_find_first_free(struct cardmap *map)
+/* get new free unit number and associate pointer with it */
+static int unit_get(struct idr *p, void *ptr)
 {
-       struct cardmap *p;
-       unsigned int nr = 0;
-       int i;
+       int unit, err;
 
-       if ((p = map) == NULL)
-               return 0;
-       for (;;) {
-               i = find_first_zero_bit(&p->inuse, CARDMAP_WIDTH);
-               if (i >= CARDMAP_WIDTH) {
-                       if (p->parent == NULL)
-                               return CARDMAP_WIDTH << p->shift;
-                       p = p->parent;
-                       i = (nr >> p->shift) & CARDMAP_MASK;
-                       set_bit(i, &p->inuse);
-                       continue;
-               }
-               nr = (nr & (~CARDMAP_MASK << p->shift)) | (i << p->shift);
-               if (p->shift == 0 || p->ptr[i] == NULL)
-                       return nr;
-               p = p->ptr[i];
+again:
+       if (!idr_pre_get(p, GFP_KERNEL)) {
+               printk(KERN_ERR "PPP: No free memory for idr\n");
+               return -ENOMEM;
        }
+
+       err = idr_get_new_above(p, ptr, 0, &unit);
+       if (err == -EAGAIN)
+               goto again;
+
+       return unit;
 }
 
-static void cardmap_destroy(struct cardmap **pmap)
+/* put unit number back to a pool */
+static void unit_put(struct idr *p, int n)
 {
-       struct cardmap *p, *np;
-       int i;
+       idr_remove(p, n);
+}
 
-       for (p = *pmap; p != NULL; p = np) {
-               if (p->shift != 0) {
-                       for (i = 0; i < CARDMAP_WIDTH; ++i)
-                               if (p->ptr[i] != NULL)
-                                       break;
-                       if (i < CARDMAP_WIDTH) {
-                               np = p->ptr[i];
-                               p->ptr[i] = NULL;
-                               continue;
-                       }
-               }
-               np = p->parent;
-               kfree(p);
-       }
-       *pmap = NULL;
+/* get pointer associated with the number */
+static void *unit_find(struct idr *p, int n)
+{
+       return idr_find(p, n);
 }
 
 /* Module/initialization stuff */
@@ -2798,15 +2914,17 @@ static void cardmap_destroy(struct cardmap **pmap)
 module_init(ppp_init);
 module_exit(ppp_cleanup);
 
+EXPORT_SYMBOL(ppp_register_net_channel);
 EXPORT_SYMBOL(ppp_register_channel);
 EXPORT_SYMBOL(ppp_unregister_channel);
 EXPORT_SYMBOL(ppp_channel_index);
 EXPORT_SYMBOL(ppp_unit_number);
+EXPORT_SYMBOL(ppp_dev_name);
 EXPORT_SYMBOL(ppp_input);
 EXPORT_SYMBOL(ppp_input_error);
 EXPORT_SYMBOL(ppp_output_wakeup);
 EXPORT_SYMBOL(ppp_register_compressor);
 EXPORT_SYMBOL(ppp_unregister_compressor);
 MODULE_LICENSE("GPL");
-MODULE_ALIAS_CHARDEV_MAJOR(PPP_MAJOR);
-MODULE_ALIAS("/dev/ppp");
+MODULE_ALIAS_CHARDEV(PPP_MAJOR, 0);
+MODULE_ALIAS("devname:ppp");