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