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