ALSA: opl4 - Fix a wrong argument in proc write callback
[safe/jmp/linux-2.6] / drivers / net / ppp_generic.c
1 /*
2  * Generic PPP layer for Linux.
3  *
4  * Copyright 1999-2002 Paul Mackerras.
5  *
6  *  This program is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU General Public License
8  *  as published by the Free Software Foundation; either version
9  *  2 of the License, or (at your option) any later version.
10  *
11  * The generic PPP layer handles the PPP network interfaces, the
12  * /dev/ppp device, packet and VJ compression, and multilink.
13  * It talks to PPP `channels' via the interface defined in
14  * include/linux/ppp_channel.h.  Channels provide the basic means for
15  * sending and receiving PPP frames on some kind of communications
16  * channel.
17  *
18  * Part of the code in this driver was inspired by the old async-only
19  * PPP driver, written by Michael Callahan and Al Longyear, and
20  * subsequently hacked by Paul Mackerras.
21  *
22  * ==FILEVERSION 20041108==
23  */
24
25 #include <linux/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  * Disconnect a channel from the generic layer.
2168  * This must be called in process context.
2169  */
2170 void
2171 ppp_unregister_channel(struct ppp_channel *chan)
2172 {
2173         struct channel *pch = chan->ppp;
2174         struct ppp_net *pn;
2175
2176         if (!pch)
2177                 return;         /* should never happen */
2178
2179         chan->ppp = NULL;
2180
2181         /*
2182          * This ensures that we have returned from any calls into the
2183          * the channel's start_xmit or ioctl routine before we proceed.
2184          */
2185         down_write(&pch->chan_sem);
2186         spin_lock_bh(&pch->downl);
2187         pch->chan = NULL;
2188         spin_unlock_bh(&pch->downl);
2189         up_write(&pch->chan_sem);
2190         ppp_disconnect_channel(pch);
2191
2192         pn = ppp_pernet(pch->chan_net);
2193         spin_lock_bh(&pn->all_channels_lock);
2194         list_del(&pch->list);
2195         spin_unlock_bh(&pn->all_channels_lock);
2196
2197         pch->file.dead = 1;
2198         wake_up_interruptible(&pch->file.rwait);
2199         if (atomic_dec_and_test(&pch->file.refcnt))
2200                 ppp_destroy_channel(pch);
2201 }
2202
2203 /*
2204  * Callback from a channel when it can accept more to transmit.
2205  * This should be called at BH/softirq level, not interrupt level.
2206  */
2207 void
2208 ppp_output_wakeup(struct ppp_channel *chan)
2209 {
2210         struct channel *pch = chan->ppp;
2211
2212         if (!pch)
2213                 return;
2214         ppp_channel_push(pch);
2215 }
2216
2217 /*
2218  * Compression control.
2219  */
2220
2221 /* Process the PPPIOCSCOMPRESS ioctl. */
2222 static int
2223 ppp_set_compress(struct ppp *ppp, unsigned long arg)
2224 {
2225         int err;
2226         struct compressor *cp, *ocomp;
2227         struct ppp_option_data data;
2228         void *state, *ostate;
2229         unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
2230
2231         err = -EFAULT;
2232         if (copy_from_user(&data, (void __user *) arg, sizeof(data)) ||
2233             (data.length <= CCP_MAX_OPTION_LENGTH &&
2234              copy_from_user(ccp_option, (void __user *) data.ptr, data.length)))
2235                 goto out;
2236         err = -EINVAL;
2237         if (data.length > CCP_MAX_OPTION_LENGTH ||
2238             ccp_option[1] < 2 || ccp_option[1] > data.length)
2239                 goto out;
2240
2241         cp = try_then_request_module(
2242                 find_compressor(ccp_option[0]),
2243                 "ppp-compress-%d", ccp_option[0]);
2244         if (!cp)
2245                 goto out;
2246
2247         err = -ENOBUFS;
2248         if (data.transmit) {
2249                 state = cp->comp_alloc(ccp_option, data.length);
2250                 if (state) {
2251                         ppp_xmit_lock(ppp);
2252                         ppp->xstate &= ~SC_COMP_RUN;
2253                         ocomp = ppp->xcomp;
2254                         ostate = ppp->xc_state;
2255                         ppp->xcomp = cp;
2256                         ppp->xc_state = state;
2257                         ppp_xmit_unlock(ppp);
2258                         if (ostate) {
2259                                 ocomp->comp_free(ostate);
2260                                 module_put(ocomp->owner);
2261                         }
2262                         err = 0;
2263                 } else
2264                         module_put(cp->owner);
2265
2266         } else {
2267                 state = cp->decomp_alloc(ccp_option, data.length);
2268                 if (state) {
2269                         ppp_recv_lock(ppp);
2270                         ppp->rstate &= ~SC_DECOMP_RUN;
2271                         ocomp = ppp->rcomp;
2272                         ostate = ppp->rc_state;
2273                         ppp->rcomp = cp;
2274                         ppp->rc_state = state;
2275                         ppp_recv_unlock(ppp);
2276                         if (ostate) {
2277                                 ocomp->decomp_free(ostate);
2278                                 module_put(ocomp->owner);
2279                         }
2280                         err = 0;
2281                 } else
2282                         module_put(cp->owner);
2283         }
2284
2285  out:
2286         return err;
2287 }
2288
2289 /*
2290  * Look at a CCP packet and update our state accordingly.
2291  * We assume the caller has the xmit or recv path locked.
2292  */
2293 static void
2294 ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
2295 {
2296         unsigned char *dp;
2297         int len;
2298
2299         if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
2300                 return; /* no header */
2301         dp = skb->data + 2;
2302
2303         switch (CCP_CODE(dp)) {
2304         case CCP_CONFREQ:
2305
2306                 /* A ConfReq starts negotiation of compression
2307                  * in one direction of transmission,
2308                  * and hence brings it down...but which way?
2309                  *
2310                  * Remember:
2311                  * A ConfReq indicates what the sender would like to receive
2312                  */
2313                 if(inbound)
2314                         /* He is proposing what I should send */
2315                         ppp->xstate &= ~SC_COMP_RUN;
2316                 else
2317                         /* I am proposing to what he should send */
2318                         ppp->rstate &= ~SC_DECOMP_RUN;
2319
2320                 break;
2321
2322         case CCP_TERMREQ:
2323         case CCP_TERMACK:
2324                 /*
2325                  * CCP is going down, both directions of transmission
2326                  */
2327                 ppp->rstate &= ~SC_DECOMP_RUN;
2328                 ppp->xstate &= ~SC_COMP_RUN;
2329                 break;
2330
2331         case CCP_CONFACK:
2332                 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
2333                         break;
2334                 len = CCP_LENGTH(dp);
2335                 if (!pskb_may_pull(skb, len + 2))
2336                         return;         /* too short */
2337                 dp += CCP_HDRLEN;
2338                 len -= CCP_HDRLEN;
2339                 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
2340                         break;
2341                 if (inbound) {
2342                         /* we will start receiving compressed packets */
2343                         if (!ppp->rc_state)
2344                                 break;
2345                         if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
2346                                         ppp->file.index, 0, ppp->mru, ppp->debug)) {
2347                                 ppp->rstate |= SC_DECOMP_RUN;
2348                                 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
2349                         }
2350                 } else {
2351                         /* we will soon start sending compressed packets */
2352                         if (!ppp->xc_state)
2353                                 break;
2354                         if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2355                                         ppp->file.index, 0, ppp->debug))
2356                                 ppp->xstate |= SC_COMP_RUN;
2357                 }
2358                 break;
2359
2360         case CCP_RESETACK:
2361                 /* reset the [de]compressor */
2362                 if ((ppp->flags & SC_CCP_UP) == 0)
2363                         break;
2364                 if (inbound) {
2365                         if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2366                                 ppp->rcomp->decomp_reset(ppp->rc_state);
2367                                 ppp->rstate &= ~SC_DC_ERROR;
2368                         }
2369                 } else {
2370                         if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2371                                 ppp->xcomp->comp_reset(ppp->xc_state);
2372                 }
2373                 break;
2374         }
2375 }
2376
2377 /* Free up compression resources. */
2378 static void
2379 ppp_ccp_closed(struct ppp *ppp)
2380 {
2381         void *xstate, *rstate;
2382         struct compressor *xcomp, *rcomp;
2383
2384         ppp_lock(ppp);
2385         ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2386         ppp->xstate = 0;
2387         xcomp = ppp->xcomp;
2388         xstate = ppp->xc_state;
2389         ppp->xc_state = NULL;
2390         ppp->rstate = 0;
2391         rcomp = ppp->rcomp;
2392         rstate = ppp->rc_state;
2393         ppp->rc_state = NULL;
2394         ppp_unlock(ppp);
2395
2396         if (xstate) {
2397                 xcomp->comp_free(xstate);
2398                 module_put(xcomp->owner);
2399         }
2400         if (rstate) {
2401                 rcomp->decomp_free(rstate);
2402                 module_put(rcomp->owner);
2403         }
2404 }
2405
2406 /* List of compressors. */
2407 static LIST_HEAD(compressor_list);
2408 static DEFINE_SPINLOCK(compressor_list_lock);
2409
2410 struct compressor_entry {
2411         struct list_head list;
2412         struct compressor *comp;
2413 };
2414
2415 static struct compressor_entry *
2416 find_comp_entry(int proto)
2417 {
2418         struct compressor_entry *ce;
2419
2420         list_for_each_entry(ce, &compressor_list, list) {
2421                 if (ce->comp->compress_proto == proto)
2422                         return ce;
2423         }
2424         return NULL;
2425 }
2426
2427 /* Register a compressor */
2428 int
2429 ppp_register_compressor(struct compressor *cp)
2430 {
2431         struct compressor_entry *ce;
2432         int ret;
2433         spin_lock(&compressor_list_lock);
2434         ret = -EEXIST;
2435         if (find_comp_entry(cp->compress_proto))
2436                 goto out;
2437         ret = -ENOMEM;
2438         ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
2439         if (!ce)
2440                 goto out;
2441         ret = 0;
2442         ce->comp = cp;
2443         list_add(&ce->list, &compressor_list);
2444  out:
2445         spin_unlock(&compressor_list_lock);
2446         return ret;
2447 }
2448
2449 /* Unregister a compressor */
2450 void
2451 ppp_unregister_compressor(struct compressor *cp)
2452 {
2453         struct compressor_entry *ce;
2454
2455         spin_lock(&compressor_list_lock);
2456         ce = find_comp_entry(cp->compress_proto);
2457         if (ce && ce->comp == cp) {
2458                 list_del(&ce->list);
2459                 kfree(ce);
2460         }
2461         spin_unlock(&compressor_list_lock);
2462 }
2463
2464 /* Find a compressor. */
2465 static struct compressor *
2466 find_compressor(int type)
2467 {
2468         struct compressor_entry *ce;
2469         struct compressor *cp = NULL;
2470
2471         spin_lock(&compressor_list_lock);
2472         ce = find_comp_entry(type);
2473         if (ce) {
2474                 cp = ce->comp;
2475                 if (!try_module_get(cp->owner))
2476                         cp = NULL;
2477         }
2478         spin_unlock(&compressor_list_lock);
2479         return cp;
2480 }
2481
2482 /*
2483  * Miscelleneous stuff.
2484  */
2485
2486 static void
2487 ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2488 {
2489         struct slcompress *vj = ppp->vj;
2490
2491         memset(st, 0, sizeof(*st));
2492         st->p.ppp_ipackets = ppp->dev->stats.rx_packets;
2493         st->p.ppp_ierrors = ppp->dev->stats.rx_errors;
2494         st->p.ppp_ibytes = ppp->dev->stats.rx_bytes;
2495         st->p.ppp_opackets = ppp->dev->stats.tx_packets;
2496         st->p.ppp_oerrors = ppp->dev->stats.tx_errors;
2497         st->p.ppp_obytes = ppp->dev->stats.tx_bytes;
2498         if (!vj)
2499                 return;
2500         st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
2501         st->vj.vjs_compressed = vj->sls_o_compressed;
2502         st->vj.vjs_searches = vj->sls_o_searches;
2503         st->vj.vjs_misses = vj->sls_o_misses;
2504         st->vj.vjs_errorin = vj->sls_i_error;
2505         st->vj.vjs_tossed = vj->sls_i_tossed;
2506         st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
2507         st->vj.vjs_compressedin = vj->sls_i_compressed;
2508 }
2509
2510 /*
2511  * Stuff for handling the lists of ppp units and channels
2512  * and for initialization.
2513  */
2514
2515 /*
2516  * Create a new ppp interface unit.  Fails if it can't allocate memory
2517  * or if there is already a unit with the requested number.
2518  * unit == -1 means allocate a new number.
2519  */
2520 static struct ppp *
2521 ppp_create_interface(struct net *net, int unit, int *retp)
2522 {
2523         struct ppp *ppp;
2524         struct ppp_net *pn;
2525         struct net_device *dev = NULL;
2526         int ret = -ENOMEM;
2527         int i;
2528
2529         dev = alloc_netdev(sizeof(struct ppp), "", ppp_setup);
2530         if (!dev)
2531                 goto out1;
2532
2533         pn = ppp_pernet(net);
2534
2535         ppp = netdev_priv(dev);
2536         ppp->dev = dev;
2537         ppp->mru = PPP_MRU;
2538         init_ppp_file(&ppp->file, INTERFACE);
2539         ppp->file.hdrlen = PPP_HDRLEN - 2;      /* don't count proto bytes */
2540         for (i = 0; i < NUM_NP; ++i)
2541                 ppp->npmode[i] = NPMODE_PASS;
2542         INIT_LIST_HEAD(&ppp->channels);
2543         spin_lock_init(&ppp->rlock);
2544         spin_lock_init(&ppp->wlock);
2545 #ifdef CONFIG_PPP_MULTILINK
2546         ppp->minseq = -1;
2547         skb_queue_head_init(&ppp->mrq);
2548 #endif /* CONFIG_PPP_MULTILINK */
2549
2550         /*
2551          * drum roll: don't forget to set
2552          * the net device is belong to
2553          */
2554         dev_net_set(dev, net);
2555
2556         ret = -EEXIST;
2557         mutex_lock(&pn->all_ppp_mutex);
2558
2559         if (unit < 0) {
2560                 unit = unit_get(&pn->units_idr, ppp);
2561                 if (unit < 0) {
2562                         *retp = unit;
2563                         goto out2;
2564                 }
2565         } else {
2566                 if (unit_find(&pn->units_idr, unit))
2567                         goto out2; /* unit already exists */
2568                 /*
2569                  * if caller need a specified unit number
2570                  * lets try to satisfy him, otherwise --
2571                  * he should better ask us for new unit number
2572                  *
2573                  * NOTE: yes I know that returning EEXIST it's not
2574                  * fair but at least pppd will ask us to allocate
2575                  * new unit in this case so user is happy :)
2576                  */
2577                 unit = unit_set(&pn->units_idr, ppp, unit);
2578                 if (unit < 0)
2579                         goto out2;
2580         }
2581
2582         /* Initialize the new ppp unit */
2583         ppp->file.index = unit;
2584         sprintf(dev->name, "ppp%d", unit);
2585
2586         ret = register_netdev(dev);
2587         if (ret != 0) {
2588                 unit_put(&pn->units_idr, unit);
2589                 printk(KERN_ERR "PPP: couldn't register device %s (%d)\n",
2590                        dev->name, ret);
2591                 goto out2;
2592         }
2593
2594         ppp->ppp_net = net;
2595
2596         atomic_inc(&ppp_unit_count);
2597         mutex_unlock(&pn->all_ppp_mutex);
2598
2599         *retp = 0;
2600         return ppp;
2601
2602 out2:
2603         mutex_unlock(&pn->all_ppp_mutex);
2604         free_netdev(dev);
2605 out1:
2606         *retp = ret;
2607         return NULL;
2608 }
2609
2610 /*
2611  * Initialize a ppp_file structure.
2612  */
2613 static void
2614 init_ppp_file(struct ppp_file *pf, int kind)
2615 {
2616         pf->kind = kind;
2617         skb_queue_head_init(&pf->xq);
2618         skb_queue_head_init(&pf->rq);
2619         atomic_set(&pf->refcnt, 1);
2620         init_waitqueue_head(&pf->rwait);
2621 }
2622
2623 /*
2624  * Take down a ppp interface unit - called when the owning file
2625  * (the one that created the unit) is closed or detached.
2626  */
2627 static void ppp_shutdown_interface(struct ppp *ppp)
2628 {
2629         struct ppp_net *pn;
2630
2631         pn = ppp_pernet(ppp->ppp_net);
2632         mutex_lock(&pn->all_ppp_mutex);
2633
2634         /* This will call dev_close() for us. */
2635         ppp_lock(ppp);
2636         if (!ppp->closing) {
2637                 ppp->closing = 1;
2638                 ppp_unlock(ppp);
2639                 unregister_netdev(ppp->dev);
2640         } else
2641                 ppp_unlock(ppp);
2642
2643         unit_put(&pn->units_idr, ppp->file.index);
2644         ppp->file.dead = 1;
2645         ppp->owner = NULL;
2646         wake_up_interruptible(&ppp->file.rwait);
2647
2648         mutex_unlock(&pn->all_ppp_mutex);
2649 }
2650
2651 /*
2652  * Free the memory used by a ppp unit.  This is only called once
2653  * there are no channels connected to the unit and no file structs
2654  * that reference the unit.
2655  */
2656 static void ppp_destroy_interface(struct ppp *ppp)
2657 {
2658         atomic_dec(&ppp_unit_count);
2659
2660         if (!ppp->file.dead || ppp->n_channels) {
2661                 /* "can't happen" */
2662                 printk(KERN_ERR "ppp: destroying ppp struct %p but dead=%d "
2663                        "n_channels=%d !\n", ppp, ppp->file.dead,
2664                        ppp->n_channels);
2665                 return;
2666         }
2667
2668         ppp_ccp_closed(ppp);
2669         if (ppp->vj) {
2670                 slhc_free(ppp->vj);
2671                 ppp->vj = NULL;
2672         }
2673         skb_queue_purge(&ppp->file.xq);
2674         skb_queue_purge(&ppp->file.rq);
2675 #ifdef CONFIG_PPP_MULTILINK
2676         skb_queue_purge(&ppp->mrq);
2677 #endif /* CONFIG_PPP_MULTILINK */
2678 #ifdef CONFIG_PPP_FILTER
2679         kfree(ppp->pass_filter);
2680         ppp->pass_filter = NULL;
2681         kfree(ppp->active_filter);
2682         ppp->active_filter = NULL;
2683 #endif /* CONFIG_PPP_FILTER */
2684
2685         kfree_skb(ppp->xmit_pending);
2686
2687         free_netdev(ppp->dev);
2688 }
2689
2690 /*
2691  * Locate an existing ppp unit.
2692  * The caller should have locked the all_ppp_mutex.
2693  */
2694 static struct ppp *
2695 ppp_find_unit(struct ppp_net *pn, int unit)
2696 {
2697         return unit_find(&pn->units_idr, unit);
2698 }
2699
2700 /*
2701  * Locate an existing ppp channel.
2702  * The caller should have locked the all_channels_lock.
2703  * First we look in the new_channels list, then in the
2704  * all_channels list.  If found in the new_channels list,
2705  * we move it to the all_channels list.  This is for speed
2706  * when we have a lot of channels in use.
2707  */
2708 static struct channel *
2709 ppp_find_channel(struct ppp_net *pn, int unit)
2710 {
2711         struct channel *pch;
2712
2713         list_for_each_entry(pch, &pn->new_channels, list) {
2714                 if (pch->file.index == unit) {
2715                         list_move(&pch->list, &pn->all_channels);
2716                         return pch;
2717                 }
2718         }
2719
2720         list_for_each_entry(pch, &pn->all_channels, list) {
2721                 if (pch->file.index == unit)
2722                         return pch;
2723         }
2724
2725         return NULL;
2726 }
2727
2728 /*
2729  * Connect a PPP channel to a PPP interface unit.
2730  */
2731 static int
2732 ppp_connect_channel(struct channel *pch, int unit)
2733 {
2734         struct ppp *ppp;
2735         struct ppp_net *pn;
2736         int ret = -ENXIO;
2737         int hdrlen;
2738
2739         pn = ppp_pernet(pch->chan_net);
2740
2741         mutex_lock(&pn->all_ppp_mutex);
2742         ppp = ppp_find_unit(pn, unit);
2743         if (!ppp)
2744                 goto out;
2745         write_lock_bh(&pch->upl);
2746         ret = -EINVAL;
2747         if (pch->ppp)
2748                 goto outl;
2749
2750         ppp_lock(ppp);
2751         if (pch->file.hdrlen > ppp->file.hdrlen)
2752                 ppp->file.hdrlen = pch->file.hdrlen;
2753         hdrlen = pch->file.hdrlen + 2;  /* for protocol bytes */
2754         if (hdrlen > ppp->dev->hard_header_len)
2755                 ppp->dev->hard_header_len = hdrlen;
2756         list_add_tail(&pch->clist, &ppp->channels);
2757         ++ppp->n_channels;
2758         pch->ppp = ppp;
2759         atomic_inc(&ppp->file.refcnt);
2760         ppp_unlock(ppp);
2761         ret = 0;
2762
2763  outl:
2764         write_unlock_bh(&pch->upl);
2765  out:
2766         mutex_unlock(&pn->all_ppp_mutex);
2767         return ret;
2768 }
2769
2770 /*
2771  * Disconnect a channel from its ppp unit.
2772  */
2773 static int
2774 ppp_disconnect_channel(struct channel *pch)
2775 {
2776         struct ppp *ppp;
2777         int err = -EINVAL;
2778
2779         write_lock_bh(&pch->upl);
2780         ppp = pch->ppp;
2781         pch->ppp = NULL;
2782         write_unlock_bh(&pch->upl);
2783         if (ppp) {
2784                 /* remove it from the ppp unit's list */
2785                 ppp_lock(ppp);
2786                 list_del(&pch->clist);
2787                 if (--ppp->n_channels == 0)
2788                         wake_up_interruptible(&ppp->file.rwait);
2789                 ppp_unlock(ppp);
2790                 if (atomic_dec_and_test(&ppp->file.refcnt))
2791                         ppp_destroy_interface(ppp);
2792                 err = 0;
2793         }
2794         return err;
2795 }
2796
2797 /*
2798  * Free up the resources used by a ppp channel.
2799  */
2800 static void ppp_destroy_channel(struct channel *pch)
2801 {
2802         atomic_dec(&channel_count);
2803
2804         if (!pch->file.dead) {
2805                 /* "can't happen" */
2806                 printk(KERN_ERR "ppp: destroying undead channel %p !\n",
2807                        pch);
2808                 return;
2809         }
2810         skb_queue_purge(&pch->file.xq);
2811         skb_queue_purge(&pch->file.rq);
2812         kfree(pch);
2813 }
2814
2815 static void __exit ppp_cleanup(void)
2816 {
2817         /* should never happen */
2818         if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
2819                 printk(KERN_ERR "PPP: removing module but units remain!\n");
2820         unregister_chrdev(PPP_MAJOR, "ppp");
2821         device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
2822         class_destroy(ppp_class);
2823         unregister_pernet_device(&ppp_net_ops);
2824 }
2825
2826 /*
2827  * Units handling. Caller must protect concurrent access
2828  * by holding all_ppp_mutex
2829  */
2830
2831 /* associate pointer with specified number */
2832 static int unit_set(struct idr *p, void *ptr, int n)
2833 {
2834         int unit, err;
2835
2836 again:
2837         if (!idr_pre_get(p, GFP_KERNEL)) {
2838                 printk(KERN_ERR "PPP: No free memory for idr\n");
2839                 return -ENOMEM;
2840         }
2841
2842         err = idr_get_new_above(p, ptr, n, &unit);
2843         if (err == -EAGAIN)
2844                 goto again;
2845
2846         if (unit != n) {
2847                 idr_remove(p, unit);
2848                 return -EINVAL;
2849         }
2850
2851         return unit;
2852 }
2853
2854 /* get new free unit number and associate pointer with it */
2855 static int unit_get(struct idr *p, void *ptr)
2856 {
2857         int unit, err;
2858
2859 again:
2860         if (!idr_pre_get(p, GFP_KERNEL)) {
2861                 printk(KERN_ERR "PPP: No free memory for idr\n");
2862                 return -ENOMEM;
2863         }
2864
2865         err = idr_get_new_above(p, ptr, 0, &unit);
2866         if (err == -EAGAIN)
2867                 goto again;
2868
2869         return unit;
2870 }
2871
2872 /* put unit number back to a pool */
2873 static void unit_put(struct idr *p, int n)
2874 {
2875         idr_remove(p, n);
2876 }
2877
2878 /* get pointer associated with the number */
2879 static void *unit_find(struct idr *p, int n)
2880 {
2881         return idr_find(p, n);
2882 }
2883
2884 /* Module/initialization stuff */
2885
2886 module_init(ppp_init);
2887 module_exit(ppp_cleanup);
2888
2889 EXPORT_SYMBOL(ppp_register_net_channel);
2890 EXPORT_SYMBOL(ppp_register_channel);
2891 EXPORT_SYMBOL(ppp_unregister_channel);
2892 EXPORT_SYMBOL(ppp_channel_index);
2893 EXPORT_SYMBOL(ppp_unit_number);
2894 EXPORT_SYMBOL(ppp_input);
2895 EXPORT_SYMBOL(ppp_input_error);
2896 EXPORT_SYMBOL(ppp_output_wakeup);
2897 EXPORT_SYMBOL(ppp_register_compressor);
2898 EXPORT_SYMBOL(ppp_unregister_compressor);
2899 MODULE_LICENSE("GPL");
2900 MODULE_ALIAS_CHARDEV_MAJOR(PPP_MAJOR);
2901 MODULE_ALIAS("/dev/ppp");