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