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