fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / net / ppp_generic.c
index 0245e40..ddf94ea 100644 (file)
  * ==FILEVERSION 20041108==
  */
 
-#include <linux/config.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/kmod.h>
 #include <linux/init.h>
 #include <linux/list.h>
-#include <linux/devfs_fs_kernel.h>
 #include <linux/netdevice.h>
 #include <linux/poll.h>
 #include <linux/ppp_defs.h>
@@ -46,6 +44,7 @@
 #include <linux/rwsem.h>
 #include <linux/stddef.h>
 #include <linux/device.h>
+#include <linux/mutex.h>
 #include <net/slhc_vj.h>
 #include <asm/atomic.h>
 
@@ -193,16 +192,16 @@ struct cardmap {
        void *ptr[CARDMAP_WIDTH];
 };
 static void *cardmap_get(struct cardmap *map, unsigned int nr);
-static void cardmap_set(struct cardmap **map, unsigned int nr, void *ptr);
+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_sem protects the all_ppp_units mapping.
+ * 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 DECLARE_MUTEX(all_ppp_sem);
+static DEFINE_MUTEX(all_ppp_mutex);
 static struct cardmap *all_ppp_units;
 static atomic_t ppp_unit_count = ATOMIC_INIT(0);
 
@@ -305,7 +304,7 @@ static const int npindex_to_proto[NUM_NP] = {
        PPP_MPLS_UC,
        PPP_MPLS_MC,
 };
-       
+
 /* Translates an ethertype into an NP index */
 static inline int ethertype_to_npindex(int ethertype)
 {
@@ -804,7 +803,7 @@ static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
                /* Attach to an existing ppp unit */
                if (get_user(unit, p))
                        break;
-               down(&all_ppp_sem);
+               mutex_lock(&all_ppp_mutex);
                err = -ENXIO;
                ppp = ppp_find_unit(unit);
                if (ppp != 0) {
@@ -812,7 +811,7 @@ static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
                        file->private_data = &ppp->file;
                        err = 0;
                }
-               up(&all_ppp_sem);
+               mutex_unlock(&all_ppp_mutex);
                break;
 
        case PPPIOCATTCHAN:
@@ -861,11 +860,7 @@ static int __init ppp_init(void)
                        err = PTR_ERR(ppp_class);
                        goto out_chrdev;
                }
-               class_device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), NULL, "ppp");
-               err = devfs_mk_cdev(MKDEV(PPP_MAJOR, 0),
-                               S_IFCHR|S_IRUSR|S_IWUSR, "ppp");
-               if (err)
-                       goto out_class;
+               device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), "ppp");
        }
 
 out:
@@ -873,9 +868,6 @@ out:
                printk(KERN_ERR "failed to register PPP device (%d)\n", err);
        return err;
 
-out_class:
-       class_device_destroy(ppp_class, MKDEV(PPP_MAJOR,0));
-       class_destroy(ppp_class);
 out_chrdev:
        unregister_chrdev(PPP_MAJOR, "ppp");
        goto out;
@@ -1608,8 +1600,6 @@ ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
                        kfree_skb(skb);
                        skb = ns;
                }
-               else if (!pskb_may_pull(skb, skb->len))
-                       goto err;
                else
                        skb->ip_summed = CHECKSUM_NONE;
 
@@ -1629,11 +1619,11 @@ ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
        case PPP_VJC_UNCOMP:
                if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
                        goto err;
-               
+
                /* Until we fix the decompressor need to make sure
                 * data portion is linear.
                 */
-               if (!pskb_may_pull(skb, skb->len)) 
+               if (!pskb_may_pull(skb, skb->len))
                        goto err;
 
                if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
@@ -1691,8 +1681,8 @@ ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
                    || ppp->npmode[npi] != NPMODE_PASS) {
                        kfree_skb(skb);
                } else {
-                       skb_pull(skb, 2);       /* chop off protocol */
-                       skb_postpull_rcsum(skb, skb->data - 2, 2);
+                       /* chop off protocol */
+                       skb_pull_rcsum(skb, 2);
                        skb->dev = ppp->dev;
                        skb->protocol = htons(npindex_to_ethertype[npi]);
                        skb->mac.raw = skb->data;
@@ -2005,10 +1995,9 @@ ppp_register_channel(struct ppp_channel *chan)
 {
        struct channel *pch;
 
-       pch = kmalloc(sizeof(struct channel), GFP_KERNEL);
+       pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
        if (pch == 0)
                return -ENOMEM;
-       memset(pch, 0, sizeof(struct channel));
        pch->ppp = NULL;
        pch->chan = chan;
        chan->ppp = pch;
@@ -2196,7 +2185,7 @@ ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
        switch (CCP_CODE(dp)) {
        case CCP_CONFREQ:
 
-               /* A ConfReq starts negotiation of compression 
+               /* A ConfReq starts negotiation of compression
                 * in one direction of transmission,
                 * and hence brings it down...but which way?
                 *
@@ -2206,16 +2195,16 @@ ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
                if(inbound)
                        /* He is proposing what I should send */
                        ppp->xstate &= ~SC_COMP_RUN;
-               else    
+               else
                        /* I am proposing to what he should send */
                        ppp->rstate &= ~SC_DECOMP_RUN;
-               
+
                break;
-               
+
        case CCP_TERMREQ:
        case CCP_TERMACK:
                /*
-                * CCP is going down, both directions of transmission 
+                * CCP is going down, both directions of transmission
                 */
                ppp->rstate &= ~SC_DECOMP_RUN;
                ppp->xstate &= ~SC_COMP_RUN;
@@ -2418,13 +2407,12 @@ ppp_create_interface(int unit, int *retp)
        int ret = -ENOMEM;
        int i;
 
-       ppp = kmalloc(sizeof(struct ppp), GFP_KERNEL);
+       ppp = kzalloc(sizeof(struct ppp), GFP_KERNEL);
        if (!ppp)
                goto out;
        dev = alloc_netdev(0, "", ppp_setup);
        if (!dev)
                goto out1;
-       memset(ppp, 0, sizeof(struct ppp));
 
        ppp->mru = PPP_MRU;
        init_ppp_file(&ppp->file, INTERFACE);
@@ -2446,7 +2434,7 @@ ppp_create_interface(int unit, int *retp)
        dev->do_ioctl = ppp_net_ioctl;
 
        ret = -EEXIST;
-       down(&all_ppp_sem);
+       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)
@@ -2464,13 +2452,18 @@ ppp_create_interface(int unit, int *retp)
        }
 
        atomic_inc(&ppp_unit_count);
-       cardmap_set(&all_ppp_units, unit, ppp);
-       up(&all_ppp_sem);
+       ret = cardmap_set(&all_ppp_units, unit, ppp);
+       if (ret != 0)
+               goto out3;
+
+       mutex_unlock(&all_ppp_mutex);
        *retp = 0;
        return ppp;
 
+out3:
+       atomic_dec(&ppp_unit_count);
 out2:
-       up(&all_ppp_sem);
+       mutex_unlock(&all_ppp_mutex);
        free_netdev(dev);
 out1:
        kfree(ppp);
@@ -2500,7 +2493,7 @@ static void ppp_shutdown_interface(struct ppp *ppp)
 {
        struct net_device *dev;
 
-       down(&all_ppp_sem);
+       mutex_lock(&all_ppp_mutex);
        ppp_lock(ppp);
        dev = ppp->dev;
        ppp->dev = NULL;
@@ -2514,7 +2507,7 @@ static void ppp_shutdown_interface(struct ppp *ppp)
        ppp->file.dead = 1;
        ppp->owner = NULL;
        wake_up_interruptible(&ppp->file.rwait);
-       up(&all_ppp_sem);
+       mutex_unlock(&all_ppp_mutex);
 }
 
 /*
@@ -2551,12 +2544,15 @@ 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(ppp);
 }
 
 /*
  * Locate an existing ppp unit.
- * The caller should have locked the all_ppp_sem.
+ * The caller should have locked the all_ppp_mutex.
  */
 static struct ppp *
 ppp_find_unit(int unit)
@@ -2579,8 +2575,7 @@ ppp_find_channel(int unit)
 
        list_for_each_entry(pch, &new_channels, list) {
                if (pch->file.index == unit) {
-                       list_del(&pch->list);
-                       list_add(&pch->list, &all_channels);
+                       list_move(&pch->list, &all_channels);
                        return pch;
                }
        }
@@ -2601,7 +2596,7 @@ ppp_connect_channel(struct channel *pch, int unit)
        int ret = -ENXIO;
        int hdrlen;
 
-       down(&all_ppp_sem);
+       mutex_lock(&all_ppp_mutex);
        ppp = ppp_find_unit(unit);
        if (ppp == 0)
                goto out;
@@ -2626,7 +2621,7 @@ ppp_connect_channel(struct channel *pch, int unit)
  outl:
        write_unlock_bh(&pch->upl);
  out:
-       up(&all_ppp_sem);
+       mutex_unlock(&all_ppp_mutex);
        return ret;
 }
 
@@ -2683,8 +2678,7 @@ static void __exit ppp_cleanup(void)
        cardmap_destroy(&all_ppp_units);
        if (unregister_chrdev(PPP_MAJOR, "ppp") != 0)
                printk(KERN_ERR "PPP: failed to unregister PPP device\n");
-       devfs_remove("ppp");
-       class_device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
+       device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
        class_destroy(ppp_class);
 }
 
@@ -2707,7 +2701,7 @@ static void *cardmap_get(struct cardmap *map, unsigned int nr)
        return NULL;
 }
 
-static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
+static int cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
 {
        struct cardmap *p;
        int i;
@@ -2716,8 +2710,9 @@ static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
        if (p == NULL || (nr >> p->shift) >= CARDMAP_WIDTH) {
                do {
                        /* need a new top level */
-                       struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
-                       memset(np, 0, sizeof(*np));
+                       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;
@@ -2731,8 +2726,9 @@ static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
        while (p->shift > 0) {
                i = (nr >> p->shift) & CARDMAP_MASK;
                if (p->ptr[i] == NULL) {
-                       struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
-                       memset(np, 0, sizeof(*np));
+                       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;
@@ -2747,6 +2743,9 @@ static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
                set_bit(i, &p->inuse);
        else
                clear_bit(i, &p->inuse);
+       return 0;
+ enomem:
+       return -ENOMEM;
 }
 
 static unsigned int cardmap_find_first_free(struct cardmap *map)