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