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