ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / ppp_generic.c
1 /*
2  * Generic PPP layer for Linux.
3  *
4  * Copyright 1999-2002 Paul Mackerras.
5  *
6  *  This program is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU General Public License
8  *  as published by the Free Software Foundation; either version
9  *  2 of the License, or (at your option) any later version.
10  *
11  * The generic PPP layer handles the PPP network interfaces, the
12  * /dev/ppp device, packet and VJ compression, and multilink.
13  * It talks to PPP `channels' via the interface defined in
14  * include/linux/ppp_channel.h.  Channels provide the basic means for
15  * sending and receiving PPP frames on some kind of communications
16  * channel.
17  *
18  * Part of the code in this driver was inspired by the old async-only
19  * PPP driver, written by Michael Callahan and Al Longyear, and
20  * subsequently hacked by Paul Mackerras.
21  *
22  * ==FILEVERSION 20020217==
23  */
24
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/kmod.h>
29 #include <linux/init.h>
30 #include <linux/list.h>
31 #include <linux/devfs_fs_kernel.h>
32 #include <linux/netdevice.h>
33 #include <linux/poll.h>
34 #include <linux/ppp_defs.h>
35 #include <linux/filter.h>
36 #include <linux/if_ppp.h>
37 #include <linux/ppp_channel.h>
38 #include <linux/ppp-comp.h>
39 #include <linux/skbuff.h>
40 #include <linux/rtnetlink.h>
41 #include <linux/if_arp.h>
42 #include <linux/ip.h>
43 #include <linux/tcp.h>
44 #include <linux/spinlock.h>
45 #include <linux/smp_lock.h>
46 #include <linux/rwsem.h>
47 #include <linux/stddef.h>
48 #include <linux/device.h>
49 #include <net/slhc_vj.h>
50 #include <asm/atomic.h>
51
52 #define PPP_VERSION     "2.4.2"
53
54 /*
55  * Network protocols we support.
56  */
57 #define NP_IP   0               /* Internet Protocol V4 */
58 #define NP_IPV6 1               /* Internet Protocol V6 */
59 #define NP_IPX  2               /* IPX protocol */
60 #define NP_AT   3               /* Appletalk protocol */
61 #define NP_MPLS_UC 4            /* MPLS unicast */
62 #define NP_MPLS_MC 5            /* MPLS multicast */
63 #define NUM_NP  6               /* Number of NPs. */
64
65 #define MPHDRLEN        6       /* multilink protocol header length */
66 #define MPHDRLEN_SSN    4       /* ditto with short sequence numbers */
67 #define MIN_FRAG_SIZE   64
68
69 /*
70  * An instance of /dev/ppp can be associated with either a ppp
71  * interface unit or a ppp channel.  In both cases, file->private_data
72  * points to one of these.
73  */
74 struct ppp_file {
75         enum {
76                 INTERFACE=1, CHANNEL
77         }               kind;
78         struct sk_buff_head xq;         /* pppd transmit queue */
79         struct sk_buff_head rq;         /* receive queue for pppd */
80         wait_queue_head_t rwait;        /* for poll on reading /dev/ppp */
81         atomic_t        refcnt;         /* # refs (incl /dev/ppp attached) */
82         int             hdrlen;         /* space to leave for headers */
83         int             index;          /* interface unit / channel number */
84         int             dead;           /* unit/channel has been shut down */
85 };
86
87 #define PF_TO_X(pf, X)          ((X *)((char *)(pf) - offsetof(X, file)))
88
89 #define PF_TO_PPP(pf)           PF_TO_X(pf, struct ppp)
90 #define PF_TO_CHANNEL(pf)       PF_TO_X(pf, struct channel)
91
92 #define ROUNDUP(n, x)           (((n) + (x) - 1) / (x))
93
94 /*
95  * Data structure describing one ppp unit.
96  * A ppp unit corresponds to a ppp network interface device
97  * and represents a multilink bundle.
98  * It can have 0 or more ppp channels connected to it.
99  */
100 struct ppp {
101         struct ppp_file file;           /* stuff for read/write/poll 0 */
102         struct file     *owner;         /* file that owns this unit 48 */
103         struct list_head channels;      /* list of attached channels 4c */
104         int             n_channels;     /* how many channels are attached 54 */
105         spinlock_t      rlock;          /* lock for receive side 58 */
106         spinlock_t      wlock;          /* lock for transmit side 5c */
107         int             mru;            /* max receive unit 60 */
108         unsigned int    flags;          /* control bits 64 */
109         unsigned int    xstate;         /* transmit state bits 68 */
110         unsigned int    rstate;         /* receive state bits 6c */
111         int             debug;          /* debug flags 70 */
112         struct slcompress *vj;          /* state for VJ header compression */
113         enum NPmode     npmode[NUM_NP]; /* what to do with each net proto 78 */
114         struct sk_buff  *xmit_pending;  /* a packet ready to go out 88 */
115         struct compressor *xcomp;       /* transmit packet compressor 8c */
116         void            *xc_state;      /* its internal state 90 */
117         struct compressor *rcomp;       /* receive decompressor 94 */
118         void            *rc_state;      /* its internal state 98 */
119         unsigned long   last_xmit;      /* jiffies when last pkt sent 9c */
120         unsigned long   last_recv;      /* jiffies when last pkt rcvd a0 */
121         struct net_device *dev;         /* network interface device a4 */
122 #ifdef CONFIG_PPP_MULTILINK
123         int             nxchan;         /* next channel to send something on */
124         u32             nxseq;          /* next sequence number to send */
125         int             mrru;           /* MP: max reconst. receive unit */
126         u32             nextseq;        /* MP: seq no of next packet */
127         u32             minseq;         /* MP: min of most recent seqnos */
128         struct sk_buff_head mrq;        /* MP: receive reconstruction queue */
129 #endif /* CONFIG_PPP_MULTILINK */
130         struct net_device_stats stats;  /* statistics */
131 #ifdef CONFIG_PPP_FILTER
132         struct sock_fprog pass_filter;  /* filter for packets to pass */
133         struct sock_fprog active_filter;/* filter for pkts to reset idle */
134 #endif /* CONFIG_PPP_FILTER */
135 };
136
137 /*
138  * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
139  * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP.
140  * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
141  * Bits in xstate: SC_COMP_RUN
142  */
143 #define SC_FLAG_BITS    (SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
144                          |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
145                          |SC_COMP_TCP|SC_REJ_COMP_TCP)
146
147 /*
148  * Private data structure for each channel.
149  * This includes the data structure used for multilink.
150  */
151 struct channel {
152         struct ppp_file file;           /* stuff for read/write/poll */
153         struct list_head list;          /* link in all/new_channels list */
154         struct ppp_channel *chan;       /* public channel data structure */
155         struct rw_semaphore chan_sem;   /* protects `chan' during chan ioctl */
156         spinlock_t      downl;          /* protects `chan', file.xq dequeue */
157         struct ppp      *ppp;           /* ppp unit we're connected to */
158         struct list_head clist;         /* link in list of channels per unit */
159         rwlock_t        upl;            /* protects `ppp' */
160 #ifdef CONFIG_PPP_MULTILINK
161         u8              avail;          /* flag used in multilink stuff */
162         u8              had_frag;       /* >= 1 fragments have been sent */
163         u32             lastseq;        /* MP: last sequence # received */
164 #endif /* CONFIG_PPP_MULTILINK */
165 };
166
167 /*
168  * SMP locking issues:
169  * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
170  * list and the ppp.n_channels field, you need to take both locks
171  * before you modify them.
172  * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
173  * channel.downl.
174  */
175
176 /*
177  * A cardmap represents a mapping from unsigned integers to pointers,
178  * and provides a fast "find lowest unused number" operation.
179  * It uses a broad (32-way) tree with a bitmap at each level.
180  * It is designed to be space-efficient for small numbers of entries
181  * and time-efficient for large numbers of entries.
182  */
183 #define CARDMAP_ORDER   5
184 #define CARDMAP_WIDTH   (1U << CARDMAP_ORDER)
185 #define CARDMAP_MASK    (CARDMAP_WIDTH - 1)
186
187 struct cardmap {
188         int shift;
189         unsigned long inuse;
190         struct cardmap *parent;
191         void *ptr[CARDMAP_WIDTH];
192 };
193 static void *cardmap_get(struct cardmap *map, unsigned int nr);
194 static void cardmap_set(struct cardmap **map, unsigned int nr, void *ptr);
195 static unsigned int cardmap_find_first_free(struct cardmap *map);
196 static void cardmap_destroy(struct cardmap **map);
197
198 /*
199  * all_ppp_sem protects the all_ppp_units mapping.
200  * It also ensures that finding a ppp unit in the all_ppp_units map
201  * and updating its file.refcnt field is atomic.
202  */
203 static DECLARE_MUTEX(all_ppp_sem);
204 static struct cardmap *all_ppp_units;
205 static atomic_t ppp_unit_count = ATOMIC_INIT(0);
206
207 /*
208  * all_channels_lock protects all_channels and last_channel_index,
209  * and the atomicity of find a channel and updating its file.refcnt
210  * field.
211  */
212 static spinlock_t all_channels_lock = SPIN_LOCK_UNLOCKED;
213 static LIST_HEAD(all_channels);
214 static LIST_HEAD(new_channels);
215 static int last_channel_index;
216 static atomic_t channel_count = ATOMIC_INIT(0);
217
218 /* Get the PPP protocol number from a skb */
219 #define PPP_PROTO(skb)  (((skb)->data[0] << 8) + (skb)->data[1])
220
221 /* We limit the length of ppp->file.rq to this (arbitrary) value */
222 #define PPP_MAX_RQLEN   32
223
224 /*
225  * Maximum number of multilink fragments queued up.
226  * This has to be large enough to cope with the maximum latency of
227  * the slowest channel relative to the others.  Strictly it should
228  * depend on the number of channels and their characteristics.
229  */
230 #define PPP_MP_MAX_QLEN 128
231
232 /* Multilink header bits. */
233 #define B       0x80            /* this fragment begins a packet */
234 #define E       0x40            /* this fragment ends a packet */
235
236 /* Compare multilink sequence numbers (assumed to be 32 bits wide) */
237 #define seq_before(a, b)        ((s32)((a) - (b)) < 0)
238 #define seq_after(a, b)         ((s32)((a) - (b)) > 0)
239
240 /* Prototypes. */
241 static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
242                                 unsigned int cmd, unsigned long arg);
243 static void ppp_xmit_process(struct ppp *ppp);
244 static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
245 static void ppp_push(struct ppp *ppp);
246 static void ppp_channel_push(struct channel *pch);
247 static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
248                               struct channel *pch);
249 static void ppp_receive_error(struct ppp *ppp);
250 static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
251 static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
252                                             struct sk_buff *skb);
253 #ifdef CONFIG_PPP_MULTILINK
254 static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
255                                 struct channel *pch);
256 static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
257 static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
258 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
259 #endif /* CONFIG_PPP_MULTILINK */
260 static int ppp_set_compress(struct ppp *ppp, unsigned long arg);
261 static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
262 static void ppp_ccp_closed(struct ppp *ppp);
263 static struct compressor *find_compressor(int type);
264 static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
265 static struct ppp *ppp_create_interface(int unit, int *retp);
266 static void init_ppp_file(struct ppp_file *pf, int kind);
267 static void ppp_shutdown_interface(struct ppp *ppp);
268 static void ppp_destroy_interface(struct ppp *ppp);
269 static struct ppp *ppp_find_unit(int unit);
270 static struct channel *ppp_find_channel(int unit);
271 static int ppp_connect_channel(struct channel *pch, int unit);
272 static int ppp_disconnect_channel(struct channel *pch);
273 static void ppp_destroy_channel(struct channel *pch);
274
275 static struct class_simple *ppp_class;
276
277 /* Translates a PPP protocol number to a NP index (NP == network protocol) */
278 static inline int proto_to_npindex(int proto)
279 {
280         switch (proto) {
281         case PPP_IP:
282                 return NP_IP;
283         case PPP_IPV6:
284                 return NP_IPV6;
285         case PPP_IPX:
286                 return NP_IPX;
287         case PPP_AT:
288                 return NP_AT;
289         case PPP_MPLS_UC:
290                 return NP_MPLS_UC;
291         case PPP_MPLS_MC:
292                 return NP_MPLS_MC;
293         }
294         return -EINVAL;
295 }
296
297 /* Translates an NP index into a PPP protocol number */
298 static const int npindex_to_proto[NUM_NP] = {
299         PPP_IP,
300         PPP_IPV6,
301         PPP_IPX,
302         PPP_AT,
303         PPP_MPLS_UC,
304         PPP_MPLS_MC,
305 };
306         
307 /* Translates an ethertype into an NP index */
308 static inline int ethertype_to_npindex(int ethertype)
309 {
310         switch (ethertype) {
311         case ETH_P_IP:
312                 return NP_IP;
313         case ETH_P_IPV6:
314                 return NP_IPV6;
315         case ETH_P_IPX:
316                 return NP_IPX;
317         case ETH_P_PPPTALK:
318         case ETH_P_ATALK:
319                 return NP_AT;
320         case ETH_P_MPLS_UC:
321                 return NP_MPLS_UC;
322         case ETH_P_MPLS_MC:
323                 return NP_MPLS_MC;
324         }
325         return -1;
326 }
327
328 /* Translates an NP index into an ethertype */
329 static const int npindex_to_ethertype[NUM_NP] = {
330         ETH_P_IP,
331         ETH_P_IPV6,
332         ETH_P_IPX,
333         ETH_P_PPPTALK,
334         ETH_P_MPLS_UC,
335         ETH_P_MPLS_MC,
336 };
337
338 /*
339  * Locking shorthand.
340  */
341 #define ppp_xmit_lock(ppp)      spin_lock_bh(&(ppp)->wlock)
342 #define ppp_xmit_unlock(ppp)    spin_unlock_bh(&(ppp)->wlock)
343 #define ppp_recv_lock(ppp)      spin_lock_bh(&(ppp)->rlock)
344 #define ppp_recv_unlock(ppp)    spin_unlock_bh(&(ppp)->rlock)
345 #define ppp_lock(ppp)           do { ppp_xmit_lock(ppp); \
346                                      ppp_recv_lock(ppp); } while (0)
347 #define ppp_unlock(ppp)         do { ppp_recv_unlock(ppp); \
348                                      ppp_xmit_unlock(ppp); } while (0)
349
350 /*
351  * /dev/ppp device routines.
352  * The /dev/ppp device is used by pppd to control the ppp unit.
353  * It supports the read, write, ioctl and poll functions.
354  * Open instances of /dev/ppp can be in one of three states:
355  * unattached, attached to a ppp unit, or attached to a ppp channel.
356  */
357 static int ppp_open(struct inode *inode, struct file *file)
358 {
359         /*
360          * This could (should?) be enforced by the permissions on /dev/ppp.
361          */
362         if (!capable(CAP_NET_ADMIN))
363                 return -EPERM;
364         return 0;
365 }
366
367 static int ppp_release(struct inode *inode, struct file *file)
368 {
369         struct ppp_file *pf = file->private_data;
370         struct ppp *ppp;
371
372         if (pf != 0) {
373                 file->private_data = 0;
374                 if (pf->kind == INTERFACE) {
375                         ppp = PF_TO_PPP(pf);
376                         if (file == ppp->owner)
377                                 ppp_shutdown_interface(ppp);
378                 }
379                 if (atomic_dec_and_test(&pf->refcnt)) {
380                         switch (pf->kind) {
381                         case INTERFACE:
382                                 ppp_destroy_interface(PF_TO_PPP(pf));
383                                 break;
384                         case CHANNEL:
385                                 ppp_destroy_channel(PF_TO_CHANNEL(pf));
386                                 break;
387                         }
388                 }
389         }
390         return 0;
391 }
392
393 static ssize_t ppp_read(struct file *file, char __user *buf,
394                         size_t count, loff_t *ppos)
395 {
396         struct ppp_file *pf = file->private_data;
397         DECLARE_WAITQUEUE(wait, current);
398         ssize_t ret;
399         struct sk_buff *skb = 0;
400
401         ret = count;
402
403         if (pf == 0)
404                 return -ENXIO;
405         add_wait_queue(&pf->rwait, &wait);
406         for (;;) {
407                 set_current_state(TASK_INTERRUPTIBLE);
408                 skb = skb_dequeue(&pf->rq);
409                 if (skb)
410                         break;
411                 ret = 0;
412                 if (pf->dead)
413                         break;
414                 ret = -EAGAIN;
415                 if (file->f_flags & O_NONBLOCK)
416                         break;
417                 ret = -ERESTARTSYS;
418                 if (signal_pending(current))
419                         break;
420                 schedule();
421         }
422         set_current_state(TASK_RUNNING);
423         remove_wait_queue(&pf->rwait, &wait);
424
425         if (skb == 0)
426                 goto out;
427
428         ret = -EOVERFLOW;
429         if (skb->len > count)
430                 goto outf;
431         ret = -EFAULT;
432         if (copy_to_user(buf, skb->data, skb->len))
433                 goto outf;
434         ret = skb->len;
435
436  outf:
437         kfree_skb(skb);
438  out:
439         return ret;
440 }
441
442 static ssize_t ppp_write(struct file *file, const char __user *buf,
443                          size_t count, loff_t *ppos)
444 {
445         struct ppp_file *pf = file->private_data;
446         struct sk_buff *skb;
447         ssize_t ret;
448
449         if (pf == 0)
450                 return -ENXIO;
451         ret = -ENOMEM;
452         skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
453         if (skb == 0)
454                 goto out;
455         skb_reserve(skb, pf->hdrlen);
456         ret = -EFAULT;
457         if (copy_from_user(skb_put(skb, count), buf, count)) {
458                 kfree_skb(skb);
459                 goto out;
460         }
461
462         skb_queue_tail(&pf->xq, skb);
463
464         switch (pf->kind) {
465         case INTERFACE:
466                 ppp_xmit_process(PF_TO_PPP(pf));
467                 break;
468         case CHANNEL:
469                 ppp_channel_push(PF_TO_CHANNEL(pf));
470                 break;
471         }
472
473         ret = count;
474
475  out:
476         return ret;
477 }
478
479 /* No kernel lock - fine */
480 static unsigned int ppp_poll(struct file *file, poll_table *wait)
481 {
482         struct ppp_file *pf = file->private_data;
483         unsigned int mask;
484
485         if (pf == 0)
486                 return 0;
487         poll_wait(file, &pf->rwait, wait);
488         mask = POLLOUT | POLLWRNORM;
489         if (skb_peek(&pf->rq) != 0)
490                 mask |= POLLIN | POLLRDNORM;
491         if (pf->dead)
492                 mask |= POLLHUP;
493         return mask;
494 }
495
496 static int ppp_ioctl(struct inode *inode, struct file *file,
497                      unsigned int cmd, unsigned long arg)
498 {
499         struct ppp_file *pf = file->private_data;
500         struct ppp *ppp;
501         int err = -EFAULT, val, val2, i;
502         struct ppp_idle idle;
503         struct npioctl npi;
504         int unit, cflags;
505         struct slcompress *vj;
506
507         if (pf == 0)
508                 return ppp_unattached_ioctl(pf, file, cmd, arg);
509
510         if (cmd == PPPIOCDETACH) {
511                 /*
512                  * We have to be careful here... if the file descriptor
513                  * has been dup'd, we could have another process in the
514                  * middle of a poll using the same file *, so we had
515                  * better not free the interface data structures -
516                  * instead we fail the ioctl.  Even in this case, we
517                  * shut down the interface if we are the owner of it.
518                  * Actually, we should get rid of PPPIOCDETACH, userland
519                  * (i.e. pppd) could achieve the same effect by closing
520                  * this fd and reopening /dev/ppp.
521                  */
522                 err = -EINVAL;
523                 if (pf->kind == INTERFACE) {
524                         ppp = PF_TO_PPP(pf);
525                         if (file == ppp->owner)
526                                 ppp_shutdown_interface(ppp);
527                 }
528                 if (atomic_read(&file->f_count) <= 2) {
529                         ppp_release(inode, file);
530                         err = 0;
531                 } else
532                         printk(KERN_DEBUG "PPPIOCDETACH file->f_count=%d\n",
533                                atomic_read(&file->f_count));
534                 return err;
535         }
536
537         if (pf->kind == CHANNEL) {
538                 struct channel *pch = PF_TO_CHANNEL(pf);
539                 struct ppp_channel *chan;
540
541                 switch (cmd) {
542                 case PPPIOCCONNECT:
543                         if (get_user(unit, (int *) arg))
544                                 break;
545                         err = ppp_connect_channel(pch, unit);
546                         break;
547
548                 case PPPIOCDISCONN:
549                         err = ppp_disconnect_channel(pch);
550                         break;
551
552                 default:
553                         down_read(&pch->chan_sem);
554                         chan = pch->chan;
555                         err = -ENOTTY;
556                         if (chan && chan->ops->ioctl)
557                                 err = chan->ops->ioctl(chan, cmd, arg);
558                         up_read(&pch->chan_sem);
559                 }
560                 return err;
561         }
562
563         if (pf->kind != INTERFACE) {
564                 /* can't happen */
565                 printk(KERN_ERR "PPP: not interface or channel??\n");
566                 return -EINVAL;
567         }
568
569         ppp = PF_TO_PPP(pf);
570         switch (cmd) {
571         case PPPIOCSMRU:
572                 if (get_user(val, (int *) arg))
573                         break;
574                 ppp->mru = val;
575                 err = 0;
576                 break;
577
578         case PPPIOCSFLAGS:
579                 if (get_user(val, (int *) arg))
580                         break;
581                 ppp_lock(ppp);
582                 cflags = ppp->flags & ~val;
583                 ppp->flags = val & SC_FLAG_BITS;
584                 ppp_unlock(ppp);
585                 if (cflags & SC_CCP_OPEN)
586                         ppp_ccp_closed(ppp);
587                 err = 0;
588                 break;
589
590         case PPPIOCGFLAGS:
591                 val = ppp->flags | ppp->xstate | ppp->rstate;
592                 if (put_user(val, (int *) arg))
593                         break;
594                 err = 0;
595                 break;
596
597         case PPPIOCSCOMPRESS:
598                 err = ppp_set_compress(ppp, arg);
599                 break;
600
601         case PPPIOCGUNIT:
602                 if (put_user(ppp->file.index, (int *) arg))
603                         break;
604                 err = 0;
605                 break;
606
607         case PPPIOCSDEBUG:
608                 if (get_user(val, (int *) arg))
609                         break;
610                 ppp->debug = val;
611                 err = 0;
612                 break;
613
614         case PPPIOCGDEBUG:
615                 if (put_user(ppp->debug, (int *) arg))
616                         break;
617                 err = 0;
618                 break;
619
620         case PPPIOCGIDLE:
621                 idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
622                 idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
623                 if (copy_to_user((void __user *) arg, &idle, sizeof(idle)))
624                         break;
625                 err = 0;
626                 break;
627
628         case PPPIOCSMAXCID:
629                 if (get_user(val, (int *) arg))
630                         break;
631                 val2 = 15;
632                 if ((val >> 16) != 0) {
633                         val2 = val >> 16;
634                         val &= 0xffff;
635                 }
636                 vj = slhc_init(val2+1, val+1);
637                 if (vj == 0) {
638                         printk(KERN_ERR "PPP: no memory (VJ compressor)\n");
639                         err = -ENOMEM;
640                         break;
641                 }
642                 ppp_lock(ppp);
643                 if (ppp->vj != 0)
644                         slhc_free(ppp->vj);
645                 ppp->vj = vj;
646                 ppp_unlock(ppp);
647                 err = 0;
648                 break;
649
650         case PPPIOCGNPMODE:
651         case PPPIOCSNPMODE:
652                 if (copy_from_user(&npi, (void __user *) arg, sizeof(npi)))
653                         break;
654                 err = proto_to_npindex(npi.protocol);
655                 if (err < 0)
656                         break;
657                 i = err;
658                 if (cmd == PPPIOCGNPMODE) {
659                         err = -EFAULT;
660                         npi.mode = ppp->npmode[i];
661                         if (copy_to_user((void __user *) arg, &npi, sizeof(npi)))
662                                 break;
663                 } else {
664                         ppp->npmode[i] = npi.mode;
665                         /* we may be able to transmit more packets now (??) */
666                         netif_wake_queue(ppp->dev);
667                 }
668                 err = 0;
669                 break;
670
671 #ifdef CONFIG_PPP_FILTER
672         case PPPIOCSPASS:
673         case PPPIOCSACTIVE:
674         {
675                 struct sock_fprog uprog, *filtp;
676                 struct sock_filter *code = NULL;
677                 int len;
678
679                 if (copy_from_user(&uprog, (void __user *) arg, sizeof(uprog)))
680                         break;
681                 err = -EINVAL;
682                 if (uprog.len > BPF_MAXINSNS)
683                         break;
684                 err = -ENOMEM;
685                 if (uprog.len > 0) {
686                         len = uprog.len * sizeof(struct sock_filter);
687                         code = kmalloc(len, GFP_KERNEL);
688                         if (code == NULL)
689                                 break;
690                         err = -EFAULT;
691                         if (copy_from_user(code, (void __user *) uprog.filter, len)) {
692                                 kfree(code);
693                                 break;
694                         }
695                         err = sk_chk_filter(code, uprog.len);
696                         if (err) {
697                                 kfree(code);
698                                 break;
699                         }
700                 }
701                 filtp = (cmd == PPPIOCSPASS)? &ppp->pass_filter: &ppp->active_filter;
702                 ppp_lock(ppp);
703                 if (filtp->filter)
704                         kfree(filtp->filter);
705                 filtp->filter = code;
706                 filtp->len = uprog.len;
707                 ppp_unlock(ppp);
708                 err = 0;
709                 break;
710         }
711 #endif /* CONFIG_PPP_FILTER */
712
713 #ifdef CONFIG_PPP_MULTILINK
714         case PPPIOCSMRRU:
715                 if (get_user(val, (int *) arg))
716                         break;
717                 ppp_recv_lock(ppp);
718                 ppp->mrru = val;
719                 ppp_recv_unlock(ppp);
720                 err = 0;
721                 break;
722 #endif /* CONFIG_PPP_MULTILINK */
723
724         default:
725                 err = -ENOTTY;
726         }
727
728         return err;
729 }
730
731 static int ppp_unattached_ioctl(struct ppp_file *pf, struct file *file,
732                                 unsigned int cmd, unsigned long arg)
733 {
734         int unit, err = -EFAULT;
735         struct ppp *ppp;
736         struct channel *chan;
737
738         switch (cmd) {
739         case PPPIOCNEWUNIT:
740                 /* Create a new ppp unit */
741                 if (get_user(unit, (int *) arg))
742                         break;
743                 ppp = ppp_create_interface(unit, &err);
744                 if (ppp == 0)
745                         break;
746                 file->private_data = &ppp->file;
747                 ppp->owner = file;
748                 err = -EFAULT;
749                 if (put_user(ppp->file.index, (int *) arg))
750                         break;
751                 err = 0;
752                 break;
753
754         case PPPIOCATTACH:
755                 /* Attach to an existing ppp unit */
756                 if (get_user(unit, (int *) arg))
757                         break;
758                 down(&all_ppp_sem);
759                 err = -ENXIO;
760                 ppp = ppp_find_unit(unit);
761                 if (ppp != 0) {
762                         atomic_inc(&ppp->file.refcnt);
763                         file->private_data = &ppp->file;
764                         err = 0;
765                 }
766                 up(&all_ppp_sem);
767                 break;
768
769         case PPPIOCATTCHAN:
770                 if (get_user(unit, (int *) arg))
771                         break;
772                 spin_lock_bh(&all_channels_lock);
773                 err = -ENXIO;
774                 chan = ppp_find_channel(unit);
775                 if (chan != 0) {
776                         atomic_inc(&chan->file.refcnt);
777                         file->private_data = &chan->file;
778                         err = 0;
779                 }
780                 spin_unlock_bh(&all_channels_lock);
781                 break;
782
783         default:
784                 err = -ENOTTY;
785         }
786         return err;
787 }
788
789 static struct file_operations ppp_device_fops = {
790         .owner          = THIS_MODULE,
791         .read           = ppp_read,
792         .write          = ppp_write,
793         .poll           = ppp_poll,
794         .ioctl          = ppp_ioctl,
795         .open           = ppp_open,
796         .release        = ppp_release
797 };
798
799 #define PPP_MAJOR       108
800
801 /* Called at boot time if ppp is compiled into the kernel,
802    or at module load time (from init_module) if compiled as a module. */
803 static int __init ppp_init(void)
804 {
805         int err;
806
807         printk(KERN_INFO "PPP generic driver version " PPP_VERSION "\n");
808         err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
809         if (!err) {
810                 ppp_class = class_simple_create(THIS_MODULE, "ppp");
811                 if (IS_ERR(ppp_class)) {
812                         err = PTR_ERR(ppp_class);
813                         goto out_chrdev;
814                 }
815                 class_simple_device_add(ppp_class, MKDEV(PPP_MAJOR, 0), NULL, "ppp");
816                 err = devfs_mk_cdev(MKDEV(PPP_MAJOR, 0),
817                                 S_IFCHR|S_IRUSR|S_IWUSR, "ppp");
818                 if (err)
819                         goto out_class;
820         }
821
822 out:
823         if (err)
824                 printk(KERN_ERR "failed to register PPP device (%d)\n", err);
825         return err;
826
827 out_class:
828         class_simple_device_remove(MKDEV(PPP_MAJOR,0));
829         class_simple_destroy(ppp_class);
830 out_chrdev:
831         unregister_chrdev(PPP_MAJOR, "ppp");
832         goto out;
833 }
834
835 /*
836  * Network interface unit routines.
837  */
838 static int
839 ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
840 {
841         struct ppp *ppp = (struct ppp *) dev->priv;
842         int npi, proto;
843         unsigned char *pp;
844
845         npi = ethertype_to_npindex(ntohs(skb->protocol));
846         if (npi < 0)
847                 goto outf;
848
849         /* Drop, accept or reject the packet */
850         switch (ppp->npmode[npi]) {
851         case NPMODE_PASS:
852                 break;
853         case NPMODE_QUEUE:
854                 /* it would be nice to have a way to tell the network
855                    system to queue this one up for later. */
856                 goto outf;
857         case NPMODE_DROP:
858         case NPMODE_ERROR:
859                 goto outf;
860         }
861
862         /* Put the 2-byte PPP protocol number on the front,
863            making sure there is room for the address and control fields. */
864         if (skb_headroom(skb) < PPP_HDRLEN) {
865                 struct sk_buff *ns;
866
867                 ns = alloc_skb(skb->len + dev->hard_header_len, GFP_ATOMIC);
868                 if (ns == 0)
869                         goto outf;
870                 skb_reserve(ns, dev->hard_header_len);
871                 skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
872                 kfree_skb(skb);
873                 skb = ns;
874         }
875         pp = skb_push(skb, 2);
876         proto = npindex_to_proto[npi];
877         pp[0] = proto >> 8;
878         pp[1] = proto;
879
880         netif_stop_queue(dev);
881         skb_queue_tail(&ppp->file.xq, skb);
882         ppp_xmit_process(ppp);
883         return 0;
884
885  outf:
886         kfree_skb(skb);
887         ++ppp->stats.tx_dropped;
888         return 0;
889 }
890
891 static struct net_device_stats *
892 ppp_net_stats(struct net_device *dev)
893 {
894         struct ppp *ppp = (struct ppp *) dev->priv;
895
896         return &ppp->stats;
897 }
898
899 static int
900 ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
901 {
902         struct ppp *ppp = dev->priv;
903         int err = -EFAULT;
904         void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
905         struct ppp_stats stats;
906         struct ppp_comp_stats cstats;
907         char *vers;
908
909         switch (cmd) {
910         case SIOCGPPPSTATS:
911                 ppp_get_stats(ppp, &stats);
912                 if (copy_to_user(addr, &stats, sizeof(stats)))
913                         break;
914                 err = 0;
915                 break;
916
917         case SIOCGPPPCSTATS:
918                 memset(&cstats, 0, sizeof(cstats));
919                 if (ppp->xc_state != 0)
920                         ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
921                 if (ppp->rc_state != 0)
922                         ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
923                 if (copy_to_user(addr, &cstats, sizeof(cstats)))
924                         break;
925                 err = 0;
926                 break;
927
928         case SIOCGPPPVER:
929                 vers = PPP_VERSION;
930                 if (copy_to_user(addr, vers, strlen(vers) + 1))
931                         break;
932                 err = 0;
933                 break;
934
935         default:
936                 err = -EINVAL;
937         }
938
939         return err;
940 }
941
942 static void ppp_setup(struct net_device *dev)
943 {
944         dev->hard_header_len = PPP_HDRLEN;
945         dev->mtu = PPP_MTU;
946         dev->addr_len = 0;
947         dev->tx_queue_len = 3;
948         dev->type = ARPHRD_PPP;
949         dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
950 }
951
952 /*
953  * Transmit-side routines.
954  */
955
956 /*
957  * Called to do any work queued up on the transmit side
958  * that can now be done.
959  */
960 static void
961 ppp_xmit_process(struct ppp *ppp)
962 {
963         struct sk_buff *skb;
964
965         ppp_xmit_lock(ppp);
966         if (ppp->dev != 0) {
967                 ppp_push(ppp);
968                 while (ppp->xmit_pending == 0
969                        && (skb = skb_dequeue(&ppp->file.xq)) != 0)
970                         ppp_send_frame(ppp, skb);
971                 /* If there's no work left to do, tell the core net
972                    code that we can accept some more. */
973                 if (ppp->xmit_pending == 0 && skb_peek(&ppp->file.xq) == 0)
974                         netif_wake_queue(ppp->dev);
975         }
976         ppp_xmit_unlock(ppp);
977 }
978
979 /*
980  * Compress and send a frame.
981  * The caller should have locked the xmit path,
982  * and xmit_pending should be 0.
983  */
984 static void
985 ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
986 {
987         int proto = PPP_PROTO(skb);
988         struct sk_buff *new_skb;
989         int len;
990         unsigned char *cp;
991
992         if (proto < 0x8000) {
993 #ifdef CONFIG_PPP_FILTER
994                 /* check if we should pass this packet */
995                 /* the filter instructions are constructed assuming
996                    a four-byte PPP header on each packet */
997                 {
998                         u_int16_t *p = (u_int16_t *) skb_push(skb, 2);
999
1000                         *p = htons(4); /* indicate outbound in DLT_LINUX_SLL */;
1001                 }
1002                 if (ppp->pass_filter.filter
1003                     && sk_run_filter(skb, ppp->pass_filter.filter,
1004                                      ppp->pass_filter.len) == 0) {
1005                         if (ppp->debug & 1)
1006                                 printk(KERN_DEBUG "PPP: outbound frame not passed\n");
1007                         kfree_skb(skb);
1008                         return;
1009                 }
1010                 /* if this packet passes the active filter, record the time */
1011                 if (!(ppp->active_filter.filter
1012                       && sk_run_filter(skb, ppp->active_filter.filter,
1013                                        ppp->active_filter.len) == 0))
1014                         ppp->last_xmit = jiffies;
1015                 skb_pull(skb, 2);
1016 #else
1017                 /* for data packets, record the time */
1018                 ppp->last_xmit = jiffies;
1019 #endif /* CONFIG_PPP_FILTER */
1020         }
1021
1022         ++ppp->stats.tx_packets;
1023         ppp->stats.tx_bytes += skb->len - 2;
1024
1025         switch (proto) {
1026         case PPP_IP:
1027                 if (ppp->vj == 0 || (ppp->flags & SC_COMP_TCP) == 0)
1028                         break;
1029                 /* try to do VJ TCP header compression */
1030                 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
1031                                     GFP_ATOMIC);
1032                 if (new_skb == 0) {
1033                         printk(KERN_ERR "PPP: no memory (VJ comp pkt)\n");
1034                         goto drop;
1035                 }
1036                 skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
1037                 cp = skb->data + 2;
1038                 len = slhc_compress(ppp->vj, cp, skb->len - 2,
1039                                     new_skb->data + 2, &cp,
1040                                     !(ppp->flags & SC_NO_TCP_CCID));
1041                 if (cp == skb->data + 2) {
1042                         /* didn't compress */
1043                         kfree_skb(new_skb);
1044                 } else {
1045                         if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
1046                                 proto = PPP_VJC_COMP;
1047                                 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
1048                         } else {
1049                                 proto = PPP_VJC_UNCOMP;
1050                                 cp[0] = skb->data[2];
1051                         }
1052                         kfree_skb(skb);
1053                         skb = new_skb;
1054                         cp = skb_put(skb, len + 2);
1055                         cp[0] = 0;
1056                         cp[1] = proto;
1057                 }
1058                 break;
1059
1060         case PPP_CCP:
1061                 /* peek at outbound CCP frames */
1062                 ppp_ccp_peek(ppp, skb, 0);
1063                 break;
1064         }
1065
1066         /* try to do packet compression */
1067         if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state != 0
1068             && proto != PPP_LCP && proto != PPP_CCP) {
1069                 new_skb = alloc_skb(ppp->dev->mtu + ppp->dev->hard_header_len,
1070                                     GFP_ATOMIC);
1071                 if (new_skb == 0) {
1072                         printk(KERN_ERR "PPP: no memory (comp pkt)\n");
1073                         goto drop;
1074                 }
1075                 if (ppp->dev->hard_header_len > PPP_HDRLEN)
1076                         skb_reserve(new_skb,
1077                                     ppp->dev->hard_header_len - PPP_HDRLEN);
1078
1079                 /* compressor still expects A/C bytes in hdr */
1080                 len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
1081                                            new_skb->data, skb->len + 2,
1082                                            ppp->dev->mtu + PPP_HDRLEN);
1083                 if (len > 0 && (ppp->flags & SC_CCP_UP)) {
1084                         kfree_skb(skb);
1085                         skb = new_skb;
1086                         skb_put(skb, len);
1087                         skb_pull(skb, 2);       /* pull off A/C bytes */
1088                 } else {
1089                         /* didn't compress, or CCP not up yet */
1090                         kfree_skb(new_skb);
1091                 }
1092         }
1093
1094         /*
1095          * If we are waiting for traffic (demand dialling),
1096          * queue it up for pppd to receive.
1097          */
1098         if (ppp->flags & SC_LOOP_TRAFFIC) {
1099                 if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
1100                         goto drop;
1101                 skb_queue_tail(&ppp->file.rq, skb);
1102                 wake_up_interruptible(&ppp->file.rwait);
1103                 return;
1104         }
1105
1106         ppp->xmit_pending = skb;
1107         ppp_push(ppp);
1108         return;
1109
1110  drop:
1111         kfree_skb(skb);
1112         ++ppp->stats.tx_errors;
1113 }
1114
1115 /*
1116  * Try to send the frame in xmit_pending.
1117  * The caller should have the xmit path locked.
1118  */
1119 static void
1120 ppp_push(struct ppp *ppp)
1121 {
1122         struct list_head *list;
1123         struct channel *pch;
1124         struct sk_buff *skb = ppp->xmit_pending;
1125
1126         if (skb == 0)
1127                 return;
1128
1129         list = &ppp->channels;
1130         if (list_empty(list)) {
1131                 /* nowhere to send the packet, just drop it */
1132                 ppp->xmit_pending = 0;
1133                 kfree_skb(skb);
1134                 return;
1135         }
1136
1137         if ((ppp->flags & SC_MULTILINK) == 0) {
1138                 /* not doing multilink: send it down the first channel */
1139                 list = list->next;
1140                 pch = list_entry(list, struct channel, clist);
1141
1142                 spin_lock_bh(&pch->downl);
1143                 if (pch->chan) {
1144                         if (pch->chan->ops->start_xmit(pch->chan, skb))
1145                                 ppp->xmit_pending = 0;
1146                 } else {
1147                         /* channel got unregistered */
1148                         kfree_skb(skb);
1149                         ppp->xmit_pending = 0;
1150                 }
1151                 spin_unlock_bh(&pch->downl);
1152                 return;
1153         }
1154
1155 #ifdef CONFIG_PPP_MULTILINK
1156         /* Multilink: fragment the packet over as many links
1157            as can take the packet at the moment. */
1158         if (!ppp_mp_explode(ppp, skb))
1159                 return;
1160 #endif /* CONFIG_PPP_MULTILINK */
1161
1162         ppp->xmit_pending = 0;
1163         kfree_skb(skb);
1164 }
1165
1166 #ifdef CONFIG_PPP_MULTILINK
1167 /*
1168  * Divide a packet to be transmitted into fragments and
1169  * send them out the individual links.
1170  */
1171 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1172 {
1173         int nch, len, fragsize;
1174         int i, bits, hdrlen, mtu;
1175         int flen, fnb;
1176         unsigned char *p, *q;
1177         struct list_head *list;
1178         struct channel *pch;
1179         struct sk_buff *frag;
1180         struct ppp_channel *chan;
1181
1182         nch = 0;
1183         hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1184         list = &ppp->channels;
1185         while ((list = list->next) != &ppp->channels) {
1186                 pch = list_entry(list, struct channel, clist);
1187                 nch += pch->avail = (skb_queue_len(&pch->file.xq) == 0);
1188                 /*
1189                  * If a channel hasn't had a fragment yet, it has to get
1190                  * one before we send any fragments on later channels.
1191                  * If it can't take a fragment now, don't give any
1192                  * to subsequent channels.
1193                  */
1194                 if (!pch->had_frag && !pch->avail) {
1195                         while ((list = list->next) != &ppp->channels) {
1196                                 pch = list_entry(list, struct channel, clist);
1197                                 pch->avail = 0;
1198                         }
1199                         break;
1200                 }
1201         }
1202         if (nch == 0)
1203                 return 0;       /* can't take now, leave it in xmit_pending */
1204
1205         /* Do protocol field compression (XXX this should be optional) */
1206         p = skb->data;
1207         len = skb->len;
1208         if (*p == 0) {
1209                 ++p;
1210                 --len;
1211         }
1212
1213         /* decide on fragment size */
1214         fragsize = len;
1215         if (nch > 1) {
1216                 int maxch = ROUNDUP(len, MIN_FRAG_SIZE);
1217                 if (nch > maxch)
1218                         nch = maxch;
1219                 fragsize = ROUNDUP(fragsize, nch);
1220         }
1221
1222         /* skip to the channel after the one we last used
1223            and start at that one */
1224         for (i = 0; i < ppp->nxchan; ++i) {
1225                 list = list->next;
1226                 if (list == &ppp->channels) {
1227                         i = 0;
1228                         break;
1229                 }
1230         }
1231
1232         /* create a fragment for each channel */
1233         bits = B;
1234         do {
1235                 list = list->next;
1236                 if (list == &ppp->channels) {
1237                         i = 0;
1238                         continue;
1239                 }
1240                 pch = list_entry(list, struct channel, clist);
1241                 ++i;
1242                 if (!pch->avail)
1243                         continue;
1244
1245                 /* check the channel's mtu and whether it is still attached. */
1246                 spin_lock_bh(&pch->downl);
1247                 if (pch->chan == 0 || (mtu = pch->chan->mtu) < hdrlen) {
1248                         /* can't use this channel */
1249                         spin_unlock_bh(&pch->downl);
1250                         pch->avail = 0;
1251                         if (--nch == 0)
1252                                 break;
1253                         continue;
1254                 }
1255
1256                 /*
1257                  * We have to create multiple fragments for this channel
1258                  * if fragsize is greater than the channel's mtu.
1259                  */
1260                 if (fragsize > len)
1261                         fragsize = len;
1262                 for (flen = fragsize; flen > 0; flen -= fnb) {
1263                         fnb = flen;
1264                         if (fnb > mtu + 2 - hdrlen)
1265                                 fnb = mtu + 2 - hdrlen;
1266                         if (fnb >= len)
1267                                 bits |= E;
1268                         frag = alloc_skb(fnb + hdrlen, GFP_ATOMIC);
1269                         if (frag == 0)
1270                                 goto noskb;
1271                         q = skb_put(frag, fnb + hdrlen);
1272                         /* make the MP header */
1273                         q[0] = PPP_MP >> 8;
1274                         q[1] = PPP_MP;
1275                         if (ppp->flags & SC_MP_XSHORTSEQ) {
1276                                 q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
1277                                 q[3] = ppp->nxseq;
1278                         } else {
1279                                 q[2] = bits;
1280                                 q[3] = ppp->nxseq >> 16;
1281                                 q[4] = ppp->nxseq >> 8;
1282                                 q[5] = ppp->nxseq;
1283                         }
1284
1285                         /* copy the data in */
1286                         memcpy(q + hdrlen, p, fnb);
1287
1288                         /* try to send it down the channel */
1289                         chan = pch->chan;
1290                         if (!chan->ops->start_xmit(chan, frag))
1291                                 skb_queue_tail(&pch->file.xq, frag);
1292                         pch->had_frag = 1;
1293                         p += fnb;
1294                         len -= fnb;
1295                         ++ppp->nxseq;
1296                         bits = 0;
1297                 }
1298                 spin_unlock_bh(&pch->downl);
1299         } while (len > 0);
1300         ppp->nxchan = i;
1301
1302         return 1;
1303
1304  noskb:
1305         spin_unlock_bh(&pch->downl);
1306         if (ppp->debug & 1)
1307                 printk(KERN_ERR "PPP: no memory (fragment)\n");
1308         ++ppp->stats.tx_errors;
1309         ++ppp->nxseq;
1310         return 1;       /* abandon the frame */
1311 }
1312 #endif /* CONFIG_PPP_MULTILINK */
1313
1314 /*
1315  * Try to send data out on a channel.
1316  */
1317 static void
1318 ppp_channel_push(struct channel *pch)
1319 {
1320         struct sk_buff *skb;
1321         struct ppp *ppp;
1322
1323         spin_lock_bh(&pch->downl);
1324         if (pch->chan != 0) {
1325                 while (skb_queue_len(&pch->file.xq) > 0) {
1326                         skb = skb_dequeue(&pch->file.xq);
1327                         if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
1328                                 /* put the packet back and try again later */
1329                                 skb_queue_head(&pch->file.xq, skb);
1330                                 break;
1331                         }
1332                 }
1333         } else {
1334                 /* channel got deregistered */
1335                 skb_queue_purge(&pch->file.xq);
1336         }
1337         spin_unlock_bh(&pch->downl);
1338         /* see if there is anything from the attached unit to be sent */
1339         if (skb_queue_len(&pch->file.xq) == 0) {
1340                 read_lock_bh(&pch->upl);
1341                 ppp = pch->ppp;
1342                 if (ppp != 0)
1343                         ppp_xmit_process(ppp);
1344                 read_unlock_bh(&pch->upl);
1345         }
1346 }
1347
1348 /*
1349  * Receive-side routines.
1350  */
1351
1352 /* misuse a few fields of the skb for MP reconstruction */
1353 #define sequence        priority
1354 #define BEbits          cb[0]
1355
1356 static inline void
1357 ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1358 {
1359         ppp_recv_lock(ppp);
1360         /* ppp->dev == 0 means interface is closing down */
1361         if (ppp->dev != 0)
1362                 ppp_receive_frame(ppp, skb, pch);
1363         else
1364                 kfree_skb(skb);
1365         ppp_recv_unlock(ppp);
1366 }
1367
1368 void
1369 ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
1370 {
1371         struct channel *pch = chan->ppp;
1372         int proto;
1373
1374         if (pch == 0 || skb->len == 0) {
1375                 kfree_skb(skb);
1376                 return;
1377         }
1378         
1379         proto = PPP_PROTO(skb);
1380         read_lock_bh(&pch->upl);
1381         if (pch->ppp == 0 || proto >= 0xc000 || proto == PPP_CCPFRAG) {
1382                 /* put it on the channel queue */
1383                 skb_queue_tail(&pch->file.rq, skb);
1384                 /* drop old frames if queue too long */
1385                 while (pch->file.rq.qlen > PPP_MAX_RQLEN
1386                        && (skb = skb_dequeue(&pch->file.rq)) != 0)
1387                         kfree_skb(skb);
1388                 wake_up_interruptible(&pch->file.rwait);
1389         } else {
1390                 ppp_do_recv(pch->ppp, skb, pch);
1391         }
1392         read_unlock_bh(&pch->upl);
1393 }
1394
1395 /* Put a 0-length skb in the receive queue as an error indication */
1396 void
1397 ppp_input_error(struct ppp_channel *chan, int code)
1398 {
1399         struct channel *pch = chan->ppp;
1400         struct sk_buff *skb;
1401
1402         if (pch == 0)
1403                 return;
1404
1405         read_lock_bh(&pch->upl);
1406         if (pch->ppp != 0) {
1407                 skb = alloc_skb(0, GFP_ATOMIC);
1408                 if (skb != 0) {
1409                         skb->len = 0;           /* probably unnecessary */
1410                         skb->cb[0] = code;
1411                         ppp_do_recv(pch->ppp, skb, pch);
1412                 }
1413         }
1414         read_unlock_bh(&pch->upl);
1415 }
1416
1417 /*
1418  * We come in here to process a received frame.
1419  * The receive side of the ppp unit is locked.
1420  */
1421 static void
1422 ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1423 {
1424         if (skb->len >= 2) {
1425 #ifdef CONFIG_PPP_MULTILINK
1426                 /* XXX do channel-level decompression here */
1427                 if (PPP_PROTO(skb) == PPP_MP)
1428                         ppp_receive_mp_frame(ppp, skb, pch);
1429                 else
1430 #endif /* CONFIG_PPP_MULTILINK */
1431                         ppp_receive_nonmp_frame(ppp, skb);
1432                 return;
1433         }
1434
1435         if (skb->len > 0)
1436                 /* note: a 0-length skb is used as an error indication */
1437                 ++ppp->stats.rx_length_errors;
1438
1439         kfree_skb(skb);
1440         ppp_receive_error(ppp);
1441 }
1442
1443 static void
1444 ppp_receive_error(struct ppp *ppp)
1445 {
1446         ++ppp->stats.rx_errors;
1447         if (ppp->vj != 0)
1448                 slhc_toss(ppp->vj);
1449 }
1450
1451 static void
1452 ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
1453 {
1454         struct sk_buff *ns;
1455         int proto, len, npi;
1456
1457         /*
1458          * Decompress the frame, if compressed.
1459          * Note that some decompressors need to see uncompressed frames
1460          * that come in as well as compressed frames.
1461          */
1462         if (ppp->rc_state != 0 && (ppp->rstate & SC_DECOMP_RUN)
1463             && (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
1464                 skb = ppp_decompress_frame(ppp, skb);
1465
1466         proto = PPP_PROTO(skb);
1467         switch (proto) {
1468         case PPP_VJC_COMP:
1469                 /* decompress VJ compressed packets */
1470                 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1471                         goto err;
1472
1473                 if (skb_tailroom(skb) < 124) {
1474                         /* copy to a new sk_buff with more tailroom */
1475                         ns = dev_alloc_skb(skb->len + 128);
1476                         if (ns == 0) {
1477                                 printk(KERN_ERR"PPP: no memory (VJ decomp)\n");
1478                                 goto err;
1479                         }
1480                         skb_reserve(ns, 2);
1481                         skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
1482                         kfree_skb(skb);
1483                         skb = ns;
1484                 }
1485                 else if (!pskb_may_pull(skb, skb->len))
1486                         goto err;
1487
1488                 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
1489                 if (len <= 0) {
1490                         printk(KERN_DEBUG "PPP: VJ decompression error\n");
1491                         goto err;
1492                 }
1493                 len += 2;
1494                 if (len > skb->len)
1495                         skb_put(skb, len - skb->len);
1496                 else if (len < skb->len)
1497                         skb_trim(skb, len);
1498                 proto = PPP_IP;
1499                 break;
1500
1501         case PPP_VJC_UNCOMP:
1502                 if (ppp->vj == 0 || (ppp->flags & SC_REJ_COMP_TCP))
1503                         goto err;
1504                 
1505                 /* Until we fix the decompressor need to make sure
1506                  * data portion is linear.
1507                  */
1508                 if (!pskb_may_pull(skb, skb->len)) 
1509                         goto err;
1510
1511                 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
1512                         printk(KERN_ERR "PPP: VJ uncompressed error\n");
1513                         goto err;
1514                 }
1515                 proto = PPP_IP;
1516                 break;
1517
1518         case PPP_CCP:
1519                 ppp_ccp_peek(ppp, skb, 1);
1520                 break;
1521         }
1522
1523         ++ppp->stats.rx_packets;
1524         ppp->stats.rx_bytes += skb->len - 2;
1525
1526         npi = proto_to_npindex(proto);
1527         if (npi < 0) {
1528                 /* control or unknown frame - pass it to pppd */
1529                 skb_queue_tail(&ppp->file.rq, skb);
1530                 /* limit queue length by dropping old frames */
1531                 while (ppp->file.rq.qlen > PPP_MAX_RQLEN
1532                        && (skb = skb_dequeue(&ppp->file.rq)) != 0)
1533                         kfree_skb(skb);
1534                 /* wake up any process polling or blocking on read */
1535                 wake_up_interruptible(&ppp->file.rwait);
1536
1537         } else {
1538                 /* network protocol frame - give it to the kernel */
1539
1540 #ifdef CONFIG_PPP_FILTER
1541                 /* check if the packet passes the pass and active filters */
1542                 /* the filter instructions are constructed assuming
1543                    a four-byte PPP header on each packet */
1544                 {
1545                         u_int16_t *p = (u_int16_t *) skb_push(skb, 2);
1546
1547                         *p = 0; /* indicate inbound in DLT_LINUX_SLL */
1548                 }
1549                 if (ppp->pass_filter.filter
1550                     && sk_run_filter(skb, ppp->pass_filter.filter,
1551                                      ppp->pass_filter.len) == 0) {
1552                         if (ppp->debug & 1)
1553                                 printk(KERN_DEBUG "PPP: inbound frame not passed\n");
1554                         kfree_skb(skb);
1555                         return;
1556                 }
1557                 if (!(ppp->active_filter.filter
1558                       && sk_run_filter(skb, ppp->active_filter.filter,
1559                                        ppp->active_filter.len) == 0))
1560                         ppp->last_recv = jiffies;
1561                 skb_pull(skb, 2);
1562 #else
1563                 ppp->last_recv = jiffies;
1564 #endif /* CONFIG_PPP_FILTER */
1565
1566                 if ((ppp->dev->flags & IFF_UP) == 0
1567                     || ppp->npmode[npi] != NPMODE_PASS) {
1568                         kfree_skb(skb);
1569                 } else {
1570                         skb_pull(skb, 2);       /* chop off protocol */
1571                         skb->dev = ppp->dev;
1572                         skb->protocol = htons(npindex_to_ethertype[npi]);
1573                         skb->mac.raw = skb->data;
1574                         netif_rx(skb);
1575                         ppp->dev->last_rx = jiffies;
1576                 }
1577         }
1578         return;
1579
1580  err:
1581         kfree_skb(skb);
1582         ppp_receive_error(ppp);
1583 }
1584
1585 static struct sk_buff *
1586 ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
1587 {
1588         int proto = PPP_PROTO(skb);
1589         struct sk_buff *ns;
1590         int len;
1591
1592         /* Until we fix all the decompressor's need to make sure
1593          * data portion is linear.
1594          */
1595         if (!pskb_may_pull(skb, skb->len))
1596                 goto err;
1597
1598         if (proto == PPP_COMP) {
1599                 ns = dev_alloc_skb(ppp->mru + PPP_HDRLEN);
1600                 if (ns == 0) {
1601                         printk(KERN_ERR "ppp_decompress_frame: no memory\n");
1602                         goto err;
1603                 }
1604                 /* the decompressor still expects the A/C bytes in the hdr */
1605                 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
1606                                 skb->len + 2, ns->data, ppp->mru + PPP_HDRLEN);
1607                 if (len < 0) {
1608                         /* Pass the compressed frame to pppd as an
1609                            error indication. */
1610                         if (len == DECOMP_FATALERROR)
1611                                 ppp->rstate |= SC_DC_FERROR;
1612                         kfree_skb(ns);
1613                         goto err;
1614                 }
1615
1616                 kfree_skb(skb);
1617                 skb = ns;
1618                 skb_put(skb, len);
1619                 skb_pull(skb, 2);       /* pull off the A/C bytes */
1620
1621         } else {
1622                 /* Uncompressed frame - pass to decompressor so it
1623                    can update its dictionary if necessary. */
1624                 if (ppp->rcomp->incomp)
1625                         ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
1626                                            skb->len + 2);
1627         }
1628
1629         return skb;
1630
1631  err:
1632         ppp->rstate |= SC_DC_ERROR;
1633         ppp_receive_error(ppp);
1634         return skb;
1635 }
1636
1637 #ifdef CONFIG_PPP_MULTILINK
1638 /*
1639  * Receive a multilink frame.
1640  * We put it on the reconstruction queue and then pull off
1641  * as many completed frames as we can.
1642  */
1643 static void
1644 ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1645 {
1646         u32 mask, seq;
1647         struct list_head *l;
1648         int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1649
1650         if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
1651                 goto err;               /* no good, throw it away */
1652
1653         /* Decode sequence number and begin/end bits */
1654         if (ppp->flags & SC_MP_SHORTSEQ) {
1655                 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
1656                 mask = 0xfff;
1657         } else {
1658                 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
1659                 mask = 0xffffff;
1660         }
1661         skb->BEbits = skb->data[2];
1662         skb_pull(skb, mphdrlen);        /* pull off PPP and MP headers */
1663
1664         /*
1665          * Do protocol ID decompression on the first fragment of each packet.
1666          */
1667         if ((skb->BEbits & B) && (skb->data[0] & 1))
1668                 *skb_push(skb, 1) = 0;
1669
1670         /*
1671          * Expand sequence number to 32 bits, making it as close
1672          * as possible to ppp->minseq.
1673          */
1674         seq |= ppp->minseq & ~mask;
1675         if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
1676                 seq += mask + 1;
1677         else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
1678                 seq -= mask + 1;        /* should never happen */
1679         skb->sequence = seq;
1680         pch->lastseq = seq;
1681
1682         /*
1683          * If this packet comes before the next one we were expecting,
1684          * drop it.
1685          */
1686         if (seq_before(seq, ppp->nextseq)) {
1687                 kfree_skb(skb);
1688                 ++ppp->stats.rx_dropped;
1689                 ppp_receive_error(ppp);
1690                 return;
1691         }
1692
1693         /*
1694          * Reevaluate minseq, the minimum over all channels of the
1695          * last sequence number received on each channel.  Because of
1696          * the increasing sequence number rule, we know that any fragment
1697          * before `minseq' which hasn't arrived is never going to arrive.
1698          * The list of channels can't change because we have the receive
1699          * side of the ppp unit locked.
1700          */
1701         for (l = ppp->channels.next; l != &ppp->channels; l = l->next) {
1702                 struct channel *ch = list_entry(l, struct channel, clist);
1703                 if (seq_before(ch->lastseq, seq))
1704                         seq = ch->lastseq;
1705         }
1706         if (seq_before(ppp->minseq, seq))
1707                 ppp->minseq = seq;
1708
1709         /* Put the fragment on the reconstruction queue */
1710         ppp_mp_insert(ppp, skb);
1711
1712         /* If the queue is getting long, don't wait any longer for packets
1713            before the start of the queue. */
1714         if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN
1715             && seq_before(ppp->minseq, ppp->mrq.next->sequence))
1716                 ppp->minseq = ppp->mrq.next->sequence;
1717
1718         /* Pull completed packets off the queue and receive them. */
1719         while ((skb = ppp_mp_reconstruct(ppp)) != 0)
1720                 ppp_receive_nonmp_frame(ppp, skb);
1721
1722         return;
1723
1724  err:
1725         kfree_skb(skb);
1726         ppp_receive_error(ppp);
1727 }
1728
1729 /*
1730  * Insert a fragment on the MP reconstruction queue.
1731  * The queue is ordered by increasing sequence number.
1732  */
1733 static void
1734 ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
1735 {
1736         struct sk_buff *p;
1737         struct sk_buff_head *list = &ppp->mrq;
1738         u32 seq = skb->sequence;
1739
1740         /* N.B. we don't need to lock the list lock because we have the
1741            ppp unit receive-side lock. */
1742         for (p = list->next; p != (struct sk_buff *)list; p = p->next)
1743                 if (seq_before(seq, p->sequence))
1744                         break;
1745         __skb_insert(skb, p->prev, p, list);
1746 }
1747
1748 /*
1749  * Reconstruct a packet from the MP fragment queue.
1750  * We go through increasing sequence numbers until we find a
1751  * complete packet, or we get to the sequence number for a fragment
1752  * which hasn't arrived but might still do so.
1753  */
1754 struct sk_buff *
1755 ppp_mp_reconstruct(struct ppp *ppp)
1756 {
1757         u32 seq = ppp->nextseq;
1758         u32 minseq = ppp->minseq;
1759         struct sk_buff_head *list = &ppp->mrq;
1760         struct sk_buff *p, *next;
1761         struct sk_buff *head, *tail;
1762         struct sk_buff *skb = NULL;
1763         int lost = 0, len = 0;
1764
1765         if (ppp->mrru == 0)     /* do nothing until mrru is set */
1766                 return NULL;
1767         head = list->next;
1768         tail = NULL;
1769         for (p = head; p != (struct sk_buff *) list; p = next) {
1770                 next = p->next;
1771                 if (seq_before(p->sequence, seq)) {
1772                         /* this can't happen, anyway ignore the skb */
1773                         printk(KERN_ERR "ppp_mp_reconstruct bad seq %u < %u\n",
1774                                p->sequence, seq);
1775                         head = next;
1776                         continue;
1777                 }
1778                 if (p->sequence != seq) {
1779                         /* Fragment `seq' is missing.  If it is after
1780                            minseq, it might arrive later, so stop here. */
1781                         if (seq_after(seq, minseq))
1782                                 break;
1783                         /* Fragment `seq' is lost, keep going. */
1784                         lost = 1;
1785                         seq = seq_before(minseq, p->sequence)?
1786                                 minseq + 1: p->sequence;
1787                         next = p;
1788                         continue;
1789                 }
1790
1791                 /*
1792                  * At this point we know that all the fragments from
1793                  * ppp->nextseq to seq are either present or lost.
1794                  * Also, there are no complete packets in the queue
1795                  * that have no missing fragments and end before this
1796                  * fragment.
1797                  */
1798
1799                 /* B bit set indicates this fragment starts a packet */
1800                 if (p->BEbits & B) {
1801                         head = p;
1802                         lost = 0;
1803                         len = 0;
1804                 }
1805
1806                 len += p->len;
1807
1808                 /* Got a complete packet yet? */
1809                 if (lost == 0 && (p->BEbits & E) && (head->BEbits & B)) {
1810                         if (len > ppp->mrru + 2) {
1811                                 ++ppp->stats.rx_length_errors;
1812                                 printk(KERN_DEBUG "PPP: reconstructed packet"
1813                                        " is too long (%d)\n", len);
1814                         } else if (p == head) {
1815                                 /* fragment is complete packet - reuse skb */
1816                                 tail = p;
1817                                 skb = skb_get(p);
1818                                 break;
1819                         } else if ((skb = dev_alloc_skb(len)) == NULL) {
1820                                 ++ppp->stats.rx_missed_errors;
1821                                 printk(KERN_DEBUG "PPP: no memory for "
1822                                        "reconstructed packet");
1823                         } else {
1824                                 tail = p;
1825                                 break;
1826                         }
1827                         ppp->nextseq = seq + 1;
1828                 }
1829
1830                 /*
1831                  * If this is the ending fragment of a packet,
1832                  * and we haven't found a complete valid packet yet,
1833                  * we can discard up to and including this fragment.
1834                  */
1835                 if (p->BEbits & E)
1836                         head = next;
1837
1838                 ++seq;
1839         }
1840
1841         /* If we have a complete packet, copy it all into one skb. */
1842         if (tail != NULL) {
1843                 /* If we have discarded any fragments,
1844                    signal a receive error. */
1845                 if (head->sequence != ppp->nextseq) {
1846                         if (ppp->debug & 1)
1847                                 printk(KERN_DEBUG "  missed pkts %u..%u\n",
1848                                        ppp->nextseq, head->sequence-1);
1849                         ++ppp->stats.rx_dropped;
1850                         ppp_receive_error(ppp);
1851                 }
1852
1853                 if (head != tail)
1854                         /* copy to a single skb */
1855                         for (p = head; p != tail->next; p = p->next)
1856                                 skb_copy_bits(p, 0, skb_put(skb, p->len), p->len);
1857                 ppp->nextseq = tail->sequence + 1;
1858                 head = tail->next;
1859         }
1860
1861         /* Discard all the skbuffs that we have copied the data out of
1862            or that we can't use. */
1863         while ((p = list->next) != head) {
1864                 __skb_unlink(p, list);
1865                 kfree_skb(p);
1866         }
1867
1868         return skb;
1869 }
1870 #endif /* CONFIG_PPP_MULTILINK */
1871
1872 /*
1873  * Channel interface.
1874  */
1875
1876 /*
1877  * Create a new, unattached ppp channel.
1878  */
1879 int
1880 ppp_register_channel(struct ppp_channel *chan)
1881 {
1882         struct channel *pch;
1883
1884         pch = kmalloc(sizeof(struct channel), GFP_KERNEL);
1885         if (pch == 0)
1886                 return -ENOMEM;
1887         memset(pch, 0, sizeof(struct channel));
1888         pch->ppp = NULL;
1889         pch->chan = chan;
1890         chan->ppp = pch;
1891         init_ppp_file(&pch->file, CHANNEL);
1892         pch->file.hdrlen = chan->hdrlen;
1893 #ifdef CONFIG_PPP_MULTILINK
1894         pch->lastseq = -1;
1895 #endif /* CONFIG_PPP_MULTILINK */
1896         init_rwsem(&pch->chan_sem);
1897         spin_lock_init(&pch->downl);
1898         pch->upl = RW_LOCK_UNLOCKED;
1899         spin_lock_bh(&all_channels_lock);
1900         pch->file.index = ++last_channel_index;
1901         list_add(&pch->list, &new_channels);
1902         atomic_inc(&channel_count);
1903         spin_unlock_bh(&all_channels_lock);
1904         return 0;
1905 }
1906
1907 /*
1908  * Return the index of a channel.
1909  */
1910 int ppp_channel_index(struct ppp_channel *chan)
1911 {
1912         struct channel *pch = chan->ppp;
1913
1914         if (pch != 0)
1915                 return pch->file.index;
1916         return -1;
1917 }
1918
1919 /*
1920  * Return the PPP unit number to which a channel is connected.
1921  */
1922 int ppp_unit_number(struct ppp_channel *chan)
1923 {
1924         struct channel *pch = chan->ppp;
1925         int unit = -1;
1926
1927         if (pch != 0) {
1928                 read_lock_bh(&pch->upl);
1929                 if (pch->ppp != 0)
1930                         unit = pch->ppp->file.index;
1931                 read_unlock_bh(&pch->upl);
1932         }
1933         return unit;
1934 }
1935
1936 /*
1937  * Disconnect a channel from the generic layer.
1938  * This must be called in process context.
1939  */
1940 void
1941 ppp_unregister_channel(struct ppp_channel *chan)
1942 {
1943         struct channel *pch = chan->ppp;
1944
1945         if (pch == 0)
1946                 return;         /* should never happen */
1947         chan->ppp = 0;
1948
1949         /*
1950          * This ensures that we have returned from any calls into the
1951          * the channel's start_xmit or ioctl routine before we proceed.
1952          */
1953         down_write(&pch->chan_sem);
1954         spin_lock_bh(&pch->downl);
1955         pch->chan = 0;
1956         spin_unlock_bh(&pch->downl);
1957         up_write(&pch->chan_sem);
1958         ppp_disconnect_channel(pch);
1959         spin_lock_bh(&all_channels_lock);
1960         list_del(&pch->list);
1961         spin_unlock_bh(&all_channels_lock);
1962         pch->file.dead = 1;
1963         wake_up_interruptible(&pch->file.rwait);
1964         if (atomic_dec_and_test(&pch->file.refcnt))
1965                 ppp_destroy_channel(pch);
1966 }
1967
1968 /*
1969  * Callback from a channel when it can accept more to transmit.
1970  * This should be called at BH/softirq level, not interrupt level.
1971  */
1972 void
1973 ppp_output_wakeup(struct ppp_channel *chan)
1974 {
1975         struct channel *pch = chan->ppp;
1976
1977         if (pch == 0)
1978                 return;
1979         ppp_channel_push(pch);
1980 }
1981
1982 /*
1983  * Compression control.
1984  */
1985
1986 /* Process the PPPIOCSCOMPRESS ioctl. */
1987 static int
1988 ppp_set_compress(struct ppp *ppp, unsigned long arg)
1989 {
1990         int err;
1991         struct compressor *cp, *ocomp;
1992         struct ppp_option_data data;
1993         void *state, *ostate;
1994         unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
1995
1996         err = -EFAULT;
1997         if (copy_from_user(&data, (void __user *) arg, sizeof(data))
1998             || (data.length <= CCP_MAX_OPTION_LENGTH
1999                 && copy_from_user(ccp_option, (void __user *) data.ptr, data.length)))
2000                 goto out;
2001         err = -EINVAL;
2002         if (data.length > CCP_MAX_OPTION_LENGTH
2003             || ccp_option[1] < 2 || ccp_option[1] > data.length)
2004                 goto out;
2005
2006         cp = find_compressor(ccp_option[0]);
2007 #ifdef CONFIG_KMOD
2008         if (cp == 0) {
2009                 request_module("ppp-compress-%d", ccp_option[0]);
2010                 cp = find_compressor(ccp_option[0]);
2011         }
2012 #endif /* CONFIG_KMOD */
2013         if (cp == 0)
2014                 goto out;
2015
2016         err = -ENOBUFS;
2017         if (data.transmit) {
2018                 state = cp->comp_alloc(ccp_option, data.length);
2019                 if (state != 0) {
2020                         ppp_xmit_lock(ppp);
2021                         ppp->xstate &= ~SC_COMP_RUN;
2022                         ocomp = ppp->xcomp;
2023                         ostate = ppp->xc_state;
2024                         ppp->xcomp = cp;
2025                         ppp->xc_state = state;
2026                         ppp_xmit_unlock(ppp);
2027                         if (ostate != 0) {
2028                                 ocomp->comp_free(ostate);
2029                                 module_put(ocomp->owner);
2030                         }
2031                         err = 0;
2032                 } else
2033                         module_put(cp->owner);
2034
2035         } else {
2036                 state = cp->decomp_alloc(ccp_option, data.length);
2037                 if (state != 0) {
2038                         ppp_recv_lock(ppp);
2039                         ppp->rstate &= ~SC_DECOMP_RUN;
2040                         ocomp = ppp->rcomp;
2041                         ostate = ppp->rc_state;
2042                         ppp->rcomp = cp;
2043                         ppp->rc_state = state;
2044                         ppp_recv_unlock(ppp);
2045                         if (ostate != 0) {
2046                                 ocomp->decomp_free(ostate);
2047                                 module_put(ocomp->owner);
2048                         }
2049                         err = 0;
2050                 } else
2051                         module_put(cp->owner);
2052         }
2053
2054  out:
2055         return err;
2056 }
2057
2058 /*
2059  * Look at a CCP packet and update our state accordingly.
2060  * We assume the caller has the xmit or recv path locked.
2061  */
2062 static void
2063 ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
2064 {
2065         unsigned char *dp;
2066         int len;
2067
2068         if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
2069                 return; /* no header */
2070         dp = skb->data + 2;
2071
2072         switch (CCP_CODE(dp)) {
2073         case CCP_CONFREQ:
2074
2075                 /* A ConfReq starts negotiation of compression 
2076                  * in one direction of transmission,
2077                  * and hence brings it down...but which way?
2078                  *
2079                  * Remember:
2080                  * A ConfReq indicates what the sender would like to receive
2081                  */
2082                 if(inbound)
2083                         /* He is proposing what I should send */
2084                         ppp->xstate &= ~SC_COMP_RUN;
2085                 else    
2086                         /* I am proposing to what he should send */
2087                         ppp->rstate &= ~SC_DECOMP_RUN;
2088                 
2089                 break;
2090                 
2091         case CCP_TERMREQ:
2092         case CCP_TERMACK:
2093                 /*
2094                  * CCP is going down, both directions of transmission 
2095                  */
2096                 ppp->rstate &= ~SC_DECOMP_RUN;
2097                 ppp->xstate &= ~SC_COMP_RUN;
2098                 break;
2099
2100         case CCP_CONFACK:
2101                 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
2102                         break;
2103                 len = CCP_LENGTH(dp);
2104                 if (!pskb_may_pull(skb, len + 2))
2105                         return;         /* too short */
2106                 dp += CCP_HDRLEN;
2107                 len -= CCP_HDRLEN;
2108                 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
2109                         break;
2110                 if (inbound) {
2111                         /* we will start receiving compressed packets */
2112                         if (ppp->rc_state == 0)
2113                                 break;
2114                         if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
2115                                         ppp->file.index, 0, ppp->mru, ppp->debug)) {
2116                                 ppp->rstate |= SC_DECOMP_RUN;
2117                                 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
2118                         }
2119                 } else {
2120                         /* we will soon start sending compressed packets */
2121                         if (ppp->xc_state == 0)
2122                                 break;
2123                         if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2124                                         ppp->file.index, 0, ppp->debug))
2125                                 ppp->xstate |= SC_COMP_RUN;
2126                 }
2127                 break;
2128
2129         case CCP_RESETACK:
2130                 /* reset the [de]compressor */
2131                 if ((ppp->flags & SC_CCP_UP) == 0)
2132                         break;
2133                 if (inbound) {
2134                         if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2135                                 ppp->rcomp->decomp_reset(ppp->rc_state);
2136                                 ppp->rstate &= ~SC_DC_ERROR;
2137                         }
2138                 } else {
2139                         if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2140                                 ppp->xcomp->comp_reset(ppp->xc_state);
2141                 }
2142                 break;
2143         }
2144 }
2145
2146 /* Free up compression resources. */
2147 static void
2148 ppp_ccp_closed(struct ppp *ppp)
2149 {
2150         void *xstate, *rstate;
2151         struct compressor *xcomp, *rcomp;
2152
2153         ppp_lock(ppp);
2154         ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2155         ppp->xstate = 0;
2156         xcomp = ppp->xcomp;
2157         xstate = ppp->xc_state;
2158         ppp->xc_state = 0;
2159         ppp->rstate = 0;
2160         rcomp = ppp->rcomp;
2161         rstate = ppp->rc_state;
2162         ppp->rc_state = 0;
2163         ppp_unlock(ppp);
2164
2165         if (xstate) {
2166                 xcomp->comp_free(xstate);
2167                 module_put(xcomp->owner);
2168         }
2169         if (rstate) {
2170                 rcomp->decomp_free(rstate);
2171                 module_put(rcomp->owner);
2172         }
2173 }
2174
2175 /* List of compressors. */
2176 static LIST_HEAD(compressor_list);
2177 static spinlock_t compressor_list_lock = SPIN_LOCK_UNLOCKED;
2178
2179 struct compressor_entry {
2180         struct list_head list;
2181         struct compressor *comp;
2182 };
2183
2184 static struct compressor_entry *
2185 find_comp_entry(int proto)
2186 {
2187         struct compressor_entry *ce;
2188         struct list_head *list = &compressor_list;
2189
2190         while ((list = list->next) != &compressor_list) {
2191                 ce = list_entry(list, struct compressor_entry, list);
2192                 if (ce->comp->compress_proto == proto)
2193                         return ce;
2194         }
2195         return 0;
2196 }
2197
2198 /* Register a compressor */
2199 int
2200 ppp_register_compressor(struct compressor *cp)
2201 {
2202         struct compressor_entry *ce;
2203         int ret;
2204         spin_lock(&compressor_list_lock);
2205         ret = -EEXIST;
2206         if (find_comp_entry(cp->compress_proto) != 0)
2207                 goto out;
2208         ret = -ENOMEM;
2209         ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
2210         if (ce == 0)
2211                 goto out;
2212         ret = 0;
2213         ce->comp = cp;
2214         list_add(&ce->list, &compressor_list);
2215  out:
2216         spin_unlock(&compressor_list_lock);
2217         return ret;
2218 }
2219
2220 /* Unregister a compressor */
2221 void
2222 ppp_unregister_compressor(struct compressor *cp)
2223 {
2224         struct compressor_entry *ce;
2225
2226         spin_lock(&compressor_list_lock);
2227         ce = find_comp_entry(cp->compress_proto);
2228         if (ce != 0 && ce->comp == cp) {
2229                 list_del(&ce->list);
2230                 kfree(ce);
2231         }
2232         spin_unlock(&compressor_list_lock);
2233 }
2234
2235 /* Find a compressor. */
2236 static struct compressor *
2237 find_compressor(int type)
2238 {
2239         struct compressor_entry *ce;
2240         struct compressor *cp = 0;
2241
2242         spin_lock(&compressor_list_lock);
2243         ce = find_comp_entry(type);
2244         if (ce != 0) {
2245                 cp = ce->comp;
2246                 if (!try_module_get(cp->owner))
2247                         cp = NULL;
2248         }
2249         spin_unlock(&compressor_list_lock);
2250         return cp;
2251 }
2252
2253 /*
2254  * Miscelleneous stuff.
2255  */
2256
2257 static void
2258 ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2259 {
2260         struct slcompress *vj = ppp->vj;
2261
2262         memset(st, 0, sizeof(*st));
2263         st->p.ppp_ipackets = ppp->stats.rx_packets;
2264         st->p.ppp_ierrors = ppp->stats.rx_errors;
2265         st->p.ppp_ibytes = ppp->stats.rx_bytes;
2266         st->p.ppp_opackets = ppp->stats.tx_packets;
2267         st->p.ppp_oerrors = ppp->stats.tx_errors;
2268         st->p.ppp_obytes = ppp->stats.tx_bytes;
2269         if (vj == 0)
2270                 return;
2271         st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
2272         st->vj.vjs_compressed = vj->sls_o_compressed;
2273         st->vj.vjs_searches = vj->sls_o_searches;
2274         st->vj.vjs_misses = vj->sls_o_misses;
2275         st->vj.vjs_errorin = vj->sls_i_error;
2276         st->vj.vjs_tossed = vj->sls_i_tossed;
2277         st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
2278         st->vj.vjs_compressedin = vj->sls_i_compressed;
2279 }
2280
2281 /*
2282  * Stuff for handling the lists of ppp units and channels
2283  * and for initialization.
2284  */
2285
2286 /*
2287  * Create a new ppp interface unit.  Fails if it can't allocate memory
2288  * or if there is already a unit with the requested number.
2289  * unit == -1 means allocate a new number.
2290  */
2291 static struct ppp *
2292 ppp_create_interface(int unit, int *retp)
2293 {
2294         struct ppp *ppp;
2295         struct net_device *dev = NULL;
2296         int ret = -ENOMEM;
2297         int i;
2298
2299         ppp = kmalloc(sizeof(struct ppp), GFP_KERNEL);
2300         if (!ppp)
2301                 goto out;
2302         dev = alloc_netdev(0, "", ppp_setup);
2303         if (!dev)
2304                 goto out1;
2305         memset(ppp, 0, sizeof(struct ppp));
2306
2307         ppp->mru = PPP_MRU;
2308         init_ppp_file(&ppp->file, INTERFACE);
2309         ppp->file.hdrlen = PPP_HDRLEN - 2;      /* don't count proto bytes */
2310         for (i = 0; i < NUM_NP; ++i)
2311                 ppp->npmode[i] = NPMODE_PASS;
2312         INIT_LIST_HEAD(&ppp->channels);
2313         spin_lock_init(&ppp->rlock);
2314         spin_lock_init(&ppp->wlock);
2315 #ifdef CONFIG_PPP_MULTILINK
2316         ppp->minseq = -1;
2317         skb_queue_head_init(&ppp->mrq);
2318 #endif /* CONFIG_PPP_MULTILINK */
2319         ppp->dev = dev;
2320         dev->priv = ppp;
2321
2322         dev->hard_start_xmit = ppp_start_xmit;
2323         dev->get_stats = ppp_net_stats;
2324         dev->do_ioctl = ppp_net_ioctl;
2325
2326         ret = -EEXIST;
2327         down(&all_ppp_sem);
2328         if (unit < 0)
2329                 unit = cardmap_find_first_free(all_ppp_units);
2330         else if (cardmap_get(all_ppp_units, unit) != NULL)
2331                 goto out2;      /* unit already exists */
2332
2333         /* Initialize the new ppp unit */
2334         ppp->file.index = unit;
2335         sprintf(dev->name, "ppp%d", unit);
2336
2337         ret = register_netdev(dev);
2338         if (ret != 0) {
2339                 printk(KERN_ERR "PPP: couldn't register device %s (%d)\n",
2340                        dev->name, ret);
2341                 goto out2;
2342         }
2343
2344         atomic_inc(&ppp_unit_count);
2345         cardmap_set(&all_ppp_units, unit, ppp);
2346         up(&all_ppp_sem);
2347         *retp = 0;
2348         return ppp;
2349
2350 out2:
2351         up(&all_ppp_sem);
2352         free_netdev(dev);
2353 out1:
2354         kfree(ppp);
2355 out:
2356         *retp = ret;
2357         return NULL;
2358 }
2359
2360 /*
2361  * Initialize a ppp_file structure.
2362  */
2363 static void
2364 init_ppp_file(struct ppp_file *pf, int kind)
2365 {
2366         pf->kind = kind;
2367         skb_queue_head_init(&pf->xq);
2368         skb_queue_head_init(&pf->rq);
2369         atomic_set(&pf->refcnt, 1);
2370         init_waitqueue_head(&pf->rwait);
2371 }
2372
2373 /*
2374  * Take down a ppp interface unit - called when the owning file
2375  * (the one that created the unit) is closed or detached.
2376  */
2377 static void ppp_shutdown_interface(struct ppp *ppp)
2378 {
2379         struct net_device *dev;
2380
2381         down(&all_ppp_sem);
2382         ppp_lock(ppp);
2383         dev = ppp->dev;
2384         ppp->dev = 0;
2385         ppp_unlock(ppp);
2386         /* This will call dev_close() for us. */
2387         if (dev) {
2388                 unregister_netdev(dev);
2389                 free_netdev(dev);
2390         }
2391         cardmap_set(&all_ppp_units, ppp->file.index, NULL);
2392         ppp->file.dead = 1;
2393         ppp->owner = NULL;
2394         wake_up_interruptible(&ppp->file.rwait);
2395         up(&all_ppp_sem);
2396 }
2397
2398 /*
2399  * Free the memory used by a ppp unit.  This is only called once
2400  * there are no channels connected to the unit and no file structs
2401  * that reference the unit.
2402  */
2403 static void ppp_destroy_interface(struct ppp *ppp)
2404 {
2405         atomic_dec(&ppp_unit_count);
2406
2407         if (!ppp->file.dead || ppp->n_channels) {
2408                 /* "can't happen" */
2409                 printk(KERN_ERR "ppp: destroying ppp struct %p but dead=%d "
2410                        "n_channels=%d !\n", ppp, ppp->file.dead,
2411                        ppp->n_channels);
2412                 return;
2413         }
2414
2415         ppp_ccp_closed(ppp);
2416         if (ppp->vj) {
2417                 slhc_free(ppp->vj);
2418                 ppp->vj = 0;
2419         }
2420         skb_queue_purge(&ppp->file.xq);
2421         skb_queue_purge(&ppp->file.rq);
2422 #ifdef CONFIG_PPP_MULTILINK
2423         skb_queue_purge(&ppp->mrq);
2424 #endif /* CONFIG_PPP_MULTILINK */
2425 #ifdef CONFIG_PPP_FILTER
2426         if (ppp->pass_filter.filter) {
2427                 kfree(ppp->pass_filter.filter);
2428                 ppp->pass_filter.filter = NULL;
2429         }
2430         if (ppp->active_filter.filter) {
2431                 kfree(ppp->active_filter.filter);
2432                 ppp->active_filter.filter = 0;
2433         }
2434 #endif /* CONFIG_PPP_FILTER */
2435
2436         kfree(ppp);
2437 }
2438
2439 /*
2440  * Locate an existing ppp unit.
2441  * The caller should have locked the all_ppp_sem.
2442  */
2443 static struct ppp *
2444 ppp_find_unit(int unit)
2445 {
2446         return cardmap_get(all_ppp_units, unit);
2447 }
2448
2449 /*
2450  * Locate an existing ppp channel.
2451  * The caller should have locked the all_channels_lock.
2452  * First we look in the new_channels list, then in the
2453  * all_channels list.  If found in the new_channels list,
2454  * we move it to the all_channels list.  This is for speed
2455  * when we have a lot of channels in use.
2456  */
2457 static struct channel *
2458 ppp_find_channel(int unit)
2459 {
2460         struct channel *pch;
2461         struct list_head *list;
2462
2463         list = &new_channels;
2464         while ((list = list->next) != &new_channels) {
2465                 pch = list_entry(list, struct channel, list);
2466                 if (pch->file.index == unit) {
2467                         list_del(&pch->list);
2468                         list_add(&pch->list, &all_channels);
2469                         return pch;
2470                 }
2471         }
2472         list = &all_channels;
2473         while ((list = list->next) != &all_channels) {
2474                 pch = list_entry(list, struct channel, list);
2475                 if (pch->file.index == unit)
2476                         return pch;
2477         }
2478         return 0;
2479 }
2480
2481 /*
2482  * Connect a PPP channel to a PPP interface unit.
2483  */
2484 static int
2485 ppp_connect_channel(struct channel *pch, int unit)
2486 {
2487         struct ppp *ppp;
2488         int ret = -ENXIO;
2489         int hdrlen;
2490
2491         down(&all_ppp_sem);
2492         ppp = ppp_find_unit(unit);
2493         if (ppp == 0)
2494                 goto out;
2495         write_lock_bh(&pch->upl);
2496         ret = -EINVAL;
2497         if (pch->ppp != 0)
2498                 goto outl;
2499
2500         ppp_lock(ppp);
2501         if (pch->file.hdrlen > ppp->file.hdrlen)
2502                 ppp->file.hdrlen = pch->file.hdrlen;
2503         hdrlen = pch->file.hdrlen + 2;  /* for protocol bytes */
2504         if (ppp->dev && hdrlen > ppp->dev->hard_header_len)
2505                 ppp->dev->hard_header_len = hdrlen;
2506         list_add_tail(&pch->clist, &ppp->channels);
2507         ++ppp->n_channels;
2508         pch->ppp = ppp;
2509         atomic_inc(&ppp->file.refcnt);
2510         ppp_unlock(ppp);
2511         ret = 0;
2512
2513  outl:
2514         write_unlock_bh(&pch->upl);
2515  out:
2516         up(&all_ppp_sem);
2517         return ret;
2518 }
2519
2520 /*
2521  * Disconnect a channel from its ppp unit.
2522  */
2523 static int
2524 ppp_disconnect_channel(struct channel *pch)
2525 {
2526         struct ppp *ppp;
2527         int err = -EINVAL;
2528
2529         write_lock_bh(&pch->upl);
2530         ppp = pch->ppp;
2531         pch->ppp = NULL;
2532         write_unlock_bh(&pch->upl);
2533         if (ppp != 0) {
2534                 /* remove it from the ppp unit's list */
2535                 ppp_lock(ppp);
2536                 list_del(&pch->clist);
2537                 --ppp->n_channels;
2538                 ppp_unlock(ppp);
2539                 if (atomic_dec_and_test(&ppp->file.refcnt))
2540                         ppp_destroy_interface(ppp);
2541                 err = 0;
2542         }
2543         return err;
2544 }
2545
2546 /*
2547  * Free up the resources used by a ppp channel.
2548  */
2549 static void ppp_destroy_channel(struct channel *pch)
2550 {
2551         atomic_dec(&channel_count);
2552
2553         if (!pch->file.dead) {
2554                 /* "can't happen" */
2555                 printk(KERN_ERR "ppp: destroying undead channel %p !\n",
2556                        pch);
2557                 return;
2558         }
2559         skb_queue_purge(&pch->file.xq);
2560         skb_queue_purge(&pch->file.rq);
2561         kfree(pch);
2562 }
2563
2564 static void __exit ppp_cleanup(void)
2565 {
2566         /* should never happen */
2567         if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
2568                 printk(KERN_ERR "PPP: removing module but units remain!\n");
2569         cardmap_destroy(&all_ppp_units);
2570         if (unregister_chrdev(PPP_MAJOR, "ppp") != 0)
2571                 printk(KERN_ERR "PPP: failed to unregister PPP device\n");
2572         devfs_remove("ppp");
2573         class_simple_device_remove(MKDEV(PPP_MAJOR, 0));
2574         class_simple_destroy(ppp_class);
2575 }
2576
2577 /*
2578  * Cardmap implementation.
2579  */
2580 static void *cardmap_get(struct cardmap *map, unsigned int nr)
2581 {
2582         struct cardmap *p;
2583         int i;
2584
2585         for (p = map; p != NULL; ) {
2586                 if ((i = nr >> p->shift) >= CARDMAP_WIDTH)
2587                         return NULL;
2588                 if (p->shift == 0)
2589                         return p->ptr[i];
2590                 nr &= ~(CARDMAP_MASK << p->shift);
2591                 p = p->ptr[i];
2592         }
2593         return NULL;
2594 }
2595
2596 static void cardmap_set(struct cardmap **pmap, unsigned int nr, void *ptr)
2597 {
2598         struct cardmap *p;
2599         int i;
2600
2601         p = *pmap;
2602         if (p == NULL || (nr >> p->shift) >= CARDMAP_WIDTH) {
2603                 do {
2604                         /* need a new top level */
2605                         struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
2606                         memset(np, 0, sizeof(*np));
2607                         np->ptr[0] = p;
2608                         if (p != NULL) {
2609                                 np->shift = p->shift + CARDMAP_ORDER;
2610                                 p->parent = np;
2611                         } else
2612                                 np->shift = 0;
2613                         p = np;
2614                 } while ((nr >> p->shift) >= CARDMAP_WIDTH);
2615                 *pmap = p;
2616         }
2617         while (p->shift > 0) {
2618                 i = (nr >> p->shift) & CARDMAP_MASK;
2619                 if (p->ptr[i] == NULL) {
2620                         struct cardmap *np = kmalloc(sizeof(*np), GFP_KERNEL);
2621                         memset(np, 0, sizeof(*np));
2622                         np->shift = p->shift - CARDMAP_ORDER;
2623                         np->parent = p;
2624                         p->ptr[i] = np;
2625                 }
2626                 if (ptr == NULL)
2627                         clear_bit(i, &p->inuse);
2628                 p = p->ptr[i];
2629         }
2630         i = nr & CARDMAP_MASK;
2631         p->ptr[i] = ptr;
2632         if (ptr != NULL)
2633                 set_bit(i, &p->inuse);
2634         else
2635                 clear_bit(i, &p->inuse);
2636 }
2637
2638 static unsigned int cardmap_find_first_free(struct cardmap *map)
2639 {
2640         struct cardmap *p;
2641         unsigned int nr = 0;
2642         int i;
2643
2644         if ((p = map) == NULL)
2645                 return 0;
2646         for (;;) {
2647                 i = find_first_zero_bit(&p->inuse, CARDMAP_WIDTH);
2648                 if (i >= CARDMAP_WIDTH) {
2649                         if (p->parent == NULL)
2650                                 return CARDMAP_WIDTH << p->shift;
2651                         p = p->parent;
2652                         i = (nr >> p->shift) & CARDMAP_MASK;
2653                         set_bit(i, &p->inuse);
2654                         continue;
2655                 }
2656                 nr = (nr & (~CARDMAP_MASK << p->shift)) | (i << p->shift);
2657                 if (p->shift == 0 || p->ptr[i] == NULL)
2658                         return nr;
2659                 p = p->ptr[i];
2660         }
2661 }
2662
2663 static void cardmap_destroy(struct cardmap **pmap)
2664 {
2665         struct cardmap *p, *np;
2666         int i;
2667
2668         for (p = *pmap; p != NULL; p = np) {
2669                 if (p->shift != 0) {
2670                         for (i = 0; i < CARDMAP_WIDTH; ++i)
2671                                 if (p->ptr[i] != NULL)
2672                                         break;
2673                         if (i < CARDMAP_WIDTH) {
2674                                 np = p->ptr[i];
2675                                 p->ptr[i] = NULL;
2676                                 continue;
2677                         }
2678                 }
2679                 np = p->parent;
2680                 kfree(p);
2681         }
2682         *pmap = NULL;
2683 }
2684
2685 /* Module/initialization stuff */
2686
2687 module_init(ppp_init);
2688 module_exit(ppp_cleanup);
2689
2690 EXPORT_SYMBOL(ppp_register_channel);
2691 EXPORT_SYMBOL(ppp_unregister_channel);
2692 EXPORT_SYMBOL(ppp_channel_index);
2693 EXPORT_SYMBOL(ppp_unit_number);
2694 EXPORT_SYMBOL(ppp_input);
2695 EXPORT_SYMBOL(ppp_input_error);
2696 EXPORT_SYMBOL(ppp_output_wakeup);
2697 EXPORT_SYMBOL(ppp_register_compressor);
2698 EXPORT_SYMBOL(ppp_unregister_compressor);
2699 EXPORT_SYMBOL(all_ppp_units); /* for debugging */
2700 EXPORT_SYMBOL(all_channels); /* for debugging */
2701 MODULE_LICENSE("GPL");
2702 MODULE_ALIAS_CHARDEV_MAJOR(PPP_MAJOR);
2703 MODULE_ALIAS("/dev/ppp");