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