GNU Linux-libre 4.14.265-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         if (proto < 0x8000) {
1546 #ifdef CONFIG_PPP_FILTER
1547                 /* check if we should pass this packet */
1548                 /* the filter instructions are constructed assuming
1549                    a four-byte PPP header on each packet */
1550                 *(u8 *)skb_push(skb, 2) = 1;
1551                 if (ppp->pass_filter &&
1552                     BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
1553                         if (ppp->debug & 1)
1554                                 netdev_printk(KERN_DEBUG, ppp->dev,
1555                                               "PPP: outbound frame "
1556                                               "not passed\n");
1557                         kfree_skb(skb);
1558                         return;
1559                 }
1560                 /* if this packet passes the active filter, record the time */
1561                 if (!(ppp->active_filter &&
1562                       BPF_PROG_RUN(ppp->active_filter, skb) == 0))
1563                         ppp->last_xmit = jiffies;
1564                 skb_pull(skb, 2);
1565 #else
1566                 /* for data packets, record the time */
1567                 ppp->last_xmit = jiffies;
1568 #endif /* CONFIG_PPP_FILTER */
1569         }
1570
1571         ++ppp->stats64.tx_packets;
1572         ppp->stats64.tx_bytes += skb->len - PPP_PROTO_LEN;
1573
1574         switch (proto) {
1575         case PPP_IP:
1576                 if (!ppp->vj || (ppp->flags & SC_COMP_TCP) == 0)
1577                         break;
1578                 /* try to do VJ TCP header compression */
1579                 new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
1580                                     GFP_ATOMIC);
1581                 if (!new_skb) {
1582                         netdev_err(ppp->dev, "PPP: no memory (VJ comp pkt)\n");
1583                         goto drop;
1584                 }
1585                 skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
1586                 cp = skb->data + 2;
1587                 len = slhc_compress(ppp->vj, cp, skb->len - 2,
1588                                     new_skb->data + 2, &cp,
1589                                     !(ppp->flags & SC_NO_TCP_CCID));
1590                 if (cp == skb->data + 2) {
1591                         /* didn't compress */
1592                         consume_skb(new_skb);
1593                 } else {
1594                         if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
1595                                 proto = PPP_VJC_COMP;
1596                                 cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
1597                         } else {
1598                                 proto = PPP_VJC_UNCOMP;
1599                                 cp[0] = skb->data[2];
1600                         }
1601                         consume_skb(skb);
1602                         skb = new_skb;
1603                         cp = skb_put(skb, len + 2);
1604                         cp[0] = 0;
1605                         cp[1] = proto;
1606                 }
1607                 break;
1608
1609         case PPP_CCP:
1610                 /* peek at outbound CCP frames */
1611                 ppp_ccp_peek(ppp, skb, 0);
1612                 break;
1613         }
1614
1615         /* try to do packet compression */
1616         if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state &&
1617             proto != PPP_LCP && proto != PPP_CCP) {
1618                 if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
1619                         if (net_ratelimit())
1620                                 netdev_err(ppp->dev,
1621                                            "ppp: compression required but "
1622                                            "down - pkt dropped.\n");
1623                         goto drop;
1624                 }
1625                 skb = pad_compress_skb(ppp, skb);
1626                 if (!skb)
1627                         goto drop;
1628         }
1629
1630         /*
1631          * If we are waiting for traffic (demand dialling),
1632          * queue it up for pppd to receive.
1633          */
1634         if (ppp->flags & SC_LOOP_TRAFFIC) {
1635                 if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
1636                         goto drop;
1637                 skb_queue_tail(&ppp->file.rq, skb);
1638                 wake_up_interruptible(&ppp->file.rwait);
1639                 return;
1640         }
1641
1642         ppp->xmit_pending = skb;
1643         ppp_push(ppp);
1644         return;
1645
1646  drop:
1647         kfree_skb(skb);
1648         ++ppp->dev->stats.tx_errors;
1649 }
1650
1651 /*
1652  * Try to send the frame in xmit_pending.
1653  * The caller should have the xmit path locked.
1654  */
1655 static void
1656 ppp_push(struct ppp *ppp)
1657 {
1658         struct list_head *list;
1659         struct channel *pch;
1660         struct sk_buff *skb = ppp->xmit_pending;
1661
1662         if (!skb)
1663                 return;
1664
1665         list = &ppp->channels;
1666         if (list_empty(list)) {
1667                 /* nowhere to send the packet, just drop it */
1668                 ppp->xmit_pending = NULL;
1669                 kfree_skb(skb);
1670                 return;
1671         }
1672
1673         if ((ppp->flags & SC_MULTILINK) == 0) {
1674                 /* not doing multilink: send it down the first channel */
1675                 list = list->next;
1676                 pch = list_entry(list, struct channel, clist);
1677
1678                 spin_lock(&pch->downl);
1679                 if (pch->chan) {
1680                         if (pch->chan->ops->start_xmit(pch->chan, skb))
1681                                 ppp->xmit_pending = NULL;
1682                 } else {
1683                         /* channel got unregistered */
1684                         kfree_skb(skb);
1685                         ppp->xmit_pending = NULL;
1686                 }
1687                 spin_unlock(&pch->downl);
1688                 return;
1689         }
1690
1691 #ifdef CONFIG_PPP_MULTILINK
1692         /* Multilink: fragment the packet over as many links
1693            as can take the packet at the moment. */
1694         if (!ppp_mp_explode(ppp, skb))
1695                 return;
1696 #endif /* CONFIG_PPP_MULTILINK */
1697
1698         ppp->xmit_pending = NULL;
1699         kfree_skb(skb);
1700 }
1701
1702 #ifdef CONFIG_PPP_MULTILINK
1703 static bool mp_protocol_compress __read_mostly = true;
1704 module_param(mp_protocol_compress, bool, S_IRUGO | S_IWUSR);
1705 MODULE_PARM_DESC(mp_protocol_compress,
1706                  "compress protocol id in multilink fragments");
1707
1708 /*
1709  * Divide a packet to be transmitted into fragments and
1710  * send them out the individual links.
1711  */
1712 static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
1713 {
1714         int len, totlen;
1715         int i, bits, hdrlen, mtu;
1716         int flen;
1717         int navail, nfree, nzero;
1718         int nbigger;
1719         int totspeed;
1720         int totfree;
1721         unsigned char *p, *q;
1722         struct list_head *list;
1723         struct channel *pch;
1724         struct sk_buff *frag;
1725         struct ppp_channel *chan;
1726
1727         totspeed = 0; /*total bitrate of the bundle*/
1728         nfree = 0; /* # channels which have no packet already queued */
1729         navail = 0; /* total # of usable channels (not deregistered) */
1730         nzero = 0; /* number of channels with zero speed associated*/
1731         totfree = 0; /*total # of channels available and
1732                                   *having no queued packets before
1733                                   *starting the fragmentation*/
1734
1735         hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1736         i = 0;
1737         list_for_each_entry(pch, &ppp->channels, clist) {
1738                 if (pch->chan) {
1739                         pch->avail = 1;
1740                         navail++;
1741                         pch->speed = pch->chan->speed;
1742                 } else {
1743                         pch->avail = 0;
1744                 }
1745                 if (pch->avail) {
1746                         if (skb_queue_empty(&pch->file.xq) ||
1747                                 !pch->had_frag) {
1748                                         if (pch->speed == 0)
1749                                                 nzero++;
1750                                         else
1751                                                 totspeed += pch->speed;
1752
1753                                         pch->avail = 2;
1754                                         ++nfree;
1755                                         ++totfree;
1756                                 }
1757                         if (!pch->had_frag && i < ppp->nxchan)
1758                                 ppp->nxchan = i;
1759                 }
1760                 ++i;
1761         }
1762         /*
1763          * Don't start sending this packet unless at least half of
1764          * the channels are free.  This gives much better TCP
1765          * performance if we have a lot of channels.
1766          */
1767         if (nfree == 0 || nfree < navail / 2)
1768                 return 0; /* can't take now, leave it in xmit_pending */
1769
1770         /* Do protocol field compression */
1771         p = skb->data;
1772         len = skb->len;
1773         if (*p == 0 && mp_protocol_compress) {
1774                 ++p;
1775                 --len;
1776         }
1777
1778         totlen = len;
1779         nbigger = len % nfree;
1780
1781         /* skip to the channel after the one we last used
1782            and start at that one */
1783         list = &ppp->channels;
1784         for (i = 0; i < ppp->nxchan; ++i) {
1785                 list = list->next;
1786                 if (list == &ppp->channels) {
1787                         i = 0;
1788                         break;
1789                 }
1790         }
1791
1792         /* create a fragment for each channel */
1793         bits = B;
1794         while (len > 0) {
1795                 list = list->next;
1796                 if (list == &ppp->channels) {
1797                         i = 0;
1798                         continue;
1799                 }
1800                 pch = list_entry(list, struct channel, clist);
1801                 ++i;
1802                 if (!pch->avail)
1803                         continue;
1804
1805                 /*
1806                  * Skip this channel if it has a fragment pending already and
1807                  * we haven't given a fragment to all of the free channels.
1808                  */
1809                 if (pch->avail == 1) {
1810                         if (nfree > 0)
1811                                 continue;
1812                 } else {
1813                         pch->avail = 1;
1814                 }
1815
1816                 /* check the channel's mtu and whether it is still attached. */
1817                 spin_lock(&pch->downl);
1818                 if (pch->chan == NULL) {
1819                         /* can't use this channel, it's being deregistered */
1820                         if (pch->speed == 0)
1821                                 nzero--;
1822                         else
1823                                 totspeed -= pch->speed;
1824
1825                         spin_unlock(&pch->downl);
1826                         pch->avail = 0;
1827                         totlen = len;
1828                         totfree--;
1829                         nfree--;
1830                         if (--navail == 0)
1831                                 break;
1832                         continue;
1833                 }
1834
1835                 /*
1836                 *if the channel speed is not set divide
1837                 *the packet evenly among the free channels;
1838                 *otherwise divide it according to the speed
1839                 *of the channel we are going to transmit on
1840                 */
1841                 flen = len;
1842                 if (nfree > 0) {
1843                         if (pch->speed == 0) {
1844                                 flen = len/nfree;
1845                                 if (nbigger > 0) {
1846                                         flen++;
1847                                         nbigger--;
1848                                 }
1849                         } else {
1850                                 flen = (((totfree - nzero)*(totlen + hdrlen*totfree)) /
1851                                         ((totspeed*totfree)/pch->speed)) - hdrlen;
1852                                 if (nbigger > 0) {
1853                                         flen += ((totfree - nzero)*pch->speed)/totspeed;
1854                                         nbigger -= ((totfree - nzero)*pch->speed)/
1855                                                         totspeed;
1856                                 }
1857                         }
1858                         nfree--;
1859                 }
1860
1861                 /*
1862                  *check if we are on the last channel or
1863                  *we exceded the length of the data to
1864                  *fragment
1865                  */
1866                 if ((nfree <= 0) || (flen > len))
1867                         flen = len;
1868                 /*
1869                  *it is not worth to tx on slow channels:
1870                  *in that case from the resulting flen according to the
1871                  *above formula will be equal or less than zero.
1872                  *Skip the channel in this case
1873                  */
1874                 if (flen <= 0) {
1875                         pch->avail = 2;
1876                         spin_unlock(&pch->downl);
1877                         continue;
1878                 }
1879
1880                 /*
1881                  * hdrlen includes the 2-byte PPP protocol field, but the
1882                  * MTU counts only the payload excluding the protocol field.
1883                  * (RFC1661 Section 2)
1884                  */
1885                 mtu = pch->chan->mtu - (hdrlen - 2);
1886                 if (mtu < 4)
1887                         mtu = 4;
1888                 if (flen > mtu)
1889                         flen = mtu;
1890                 if (flen == len)
1891                         bits |= E;
1892                 frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
1893                 if (!frag)
1894                         goto noskb;
1895                 q = skb_put(frag, flen + hdrlen);
1896
1897                 /* make the MP header */
1898                 put_unaligned_be16(PPP_MP, q);
1899                 if (ppp->flags & SC_MP_XSHORTSEQ) {
1900                         q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
1901                         q[3] = ppp->nxseq;
1902                 } else {
1903                         q[2] = bits;
1904                         q[3] = ppp->nxseq >> 16;
1905                         q[4] = ppp->nxseq >> 8;
1906                         q[5] = ppp->nxseq;
1907                 }
1908
1909                 memcpy(q + hdrlen, p, flen);
1910
1911                 /* try to send it down the channel */
1912                 chan = pch->chan;
1913                 if (!skb_queue_empty(&pch->file.xq) ||
1914                         !chan->ops->start_xmit(chan, frag))
1915                         skb_queue_tail(&pch->file.xq, frag);
1916                 pch->had_frag = 1;
1917                 p += flen;
1918                 len -= flen;
1919                 ++ppp->nxseq;
1920                 bits = 0;
1921                 spin_unlock(&pch->downl);
1922         }
1923         ppp->nxchan = i;
1924
1925         return 1;
1926
1927  noskb:
1928         spin_unlock(&pch->downl);
1929         if (ppp->debug & 1)
1930                 netdev_err(ppp->dev, "PPP: no memory (fragment)\n");
1931         ++ppp->dev->stats.tx_errors;
1932         ++ppp->nxseq;
1933         return 1;       /* abandon the frame */
1934 }
1935 #endif /* CONFIG_PPP_MULTILINK */
1936
1937 /* Try to send data out on a channel */
1938 static void __ppp_channel_push(struct channel *pch)
1939 {
1940         struct sk_buff *skb;
1941         struct ppp *ppp;
1942
1943         spin_lock(&pch->downl);
1944         if (pch->chan) {
1945                 while (!skb_queue_empty(&pch->file.xq)) {
1946                         skb = skb_dequeue(&pch->file.xq);
1947                         if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
1948                                 /* put the packet back and try again later */
1949                                 skb_queue_head(&pch->file.xq, skb);
1950                                 break;
1951                         }
1952                 }
1953         } else {
1954                 /* channel got deregistered */
1955                 skb_queue_purge(&pch->file.xq);
1956         }
1957         spin_unlock(&pch->downl);
1958         /* see if there is anything from the attached unit to be sent */
1959         if (skb_queue_empty(&pch->file.xq)) {
1960                 ppp = pch->ppp;
1961                 if (ppp)
1962                         __ppp_xmit_process(ppp, NULL);
1963         }
1964 }
1965
1966 static void ppp_channel_push(struct channel *pch)
1967 {
1968         read_lock_bh(&pch->upl);
1969         if (pch->ppp) {
1970                 (*this_cpu_ptr(pch->ppp->xmit_recursion))++;
1971                 __ppp_channel_push(pch);
1972                 (*this_cpu_ptr(pch->ppp->xmit_recursion))--;
1973         } else {
1974                 __ppp_channel_push(pch);
1975         }
1976         read_unlock_bh(&pch->upl);
1977 }
1978
1979 /*
1980  * Receive-side routines.
1981  */
1982
1983 struct ppp_mp_skb_parm {
1984         u32             sequence;
1985         u8              BEbits;
1986 };
1987 #define PPP_MP_CB(skb)  ((struct ppp_mp_skb_parm *)((skb)->cb))
1988
1989 static inline void
1990 ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
1991 {
1992         ppp_recv_lock(ppp);
1993         if (!ppp->closing)
1994                 ppp_receive_frame(ppp, skb, pch);
1995         else
1996                 kfree_skb(skb);
1997         ppp_recv_unlock(ppp);
1998 }
1999
2000 void
2001 ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
2002 {
2003         struct channel *pch = chan->ppp;
2004         int proto;
2005
2006         if (!pch) {
2007                 kfree_skb(skb);
2008                 return;
2009         }
2010
2011         read_lock_bh(&pch->upl);
2012         if (!pskb_may_pull(skb, 2)) {
2013                 kfree_skb(skb);
2014                 if (pch->ppp) {
2015                         ++pch->ppp->dev->stats.rx_length_errors;
2016                         ppp_receive_error(pch->ppp);
2017                 }
2018                 goto done;
2019         }
2020
2021         proto = PPP_PROTO(skb);
2022         if (!pch->ppp || proto >= 0xc000 || proto == PPP_CCPFRAG) {
2023                 /* put it on the channel queue */
2024                 skb_queue_tail(&pch->file.rq, skb);
2025                 /* drop old frames if queue too long */
2026                 while (pch->file.rq.qlen > PPP_MAX_RQLEN &&
2027                        (skb = skb_dequeue(&pch->file.rq)))
2028                         kfree_skb(skb);
2029                 wake_up_interruptible(&pch->file.rwait);
2030         } else {
2031                 ppp_do_recv(pch->ppp, skb, pch);
2032         }
2033
2034 done:
2035         read_unlock_bh(&pch->upl);
2036 }
2037
2038 /* Put a 0-length skb in the receive queue as an error indication */
2039 void
2040 ppp_input_error(struct ppp_channel *chan, int code)
2041 {
2042         struct channel *pch = chan->ppp;
2043         struct sk_buff *skb;
2044
2045         if (!pch)
2046                 return;
2047
2048         read_lock_bh(&pch->upl);
2049         if (pch->ppp) {
2050                 skb = alloc_skb(0, GFP_ATOMIC);
2051                 if (skb) {
2052                         skb->len = 0;           /* probably unnecessary */
2053                         skb->cb[0] = code;
2054                         ppp_do_recv(pch->ppp, skb, pch);
2055                 }
2056         }
2057         read_unlock_bh(&pch->upl);
2058 }
2059
2060 /*
2061  * We come in here to process a received frame.
2062  * The receive side of the ppp unit is locked.
2063  */
2064 static void
2065 ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2066 {
2067         /* note: a 0-length skb is used as an error indication */
2068         if (skb->len > 0) {
2069                 skb_checksum_complete_unset(skb);
2070 #ifdef CONFIG_PPP_MULTILINK
2071                 /* XXX do channel-level decompression here */
2072                 if (PPP_PROTO(skb) == PPP_MP)
2073                         ppp_receive_mp_frame(ppp, skb, pch);
2074                 else
2075 #endif /* CONFIG_PPP_MULTILINK */
2076                         ppp_receive_nonmp_frame(ppp, skb);
2077         } else {
2078                 kfree_skb(skb);
2079                 ppp_receive_error(ppp);
2080         }
2081 }
2082
2083 static void
2084 ppp_receive_error(struct ppp *ppp)
2085 {
2086         ++ppp->dev->stats.rx_errors;
2087         if (ppp->vj)
2088                 slhc_toss(ppp->vj);
2089 }
2090
2091 static void
2092 ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
2093 {
2094         struct sk_buff *ns;
2095         int proto, len, npi;
2096
2097         /*
2098          * Decompress the frame, if compressed.
2099          * Note that some decompressors need to see uncompressed frames
2100          * that come in as well as compressed frames.
2101          */
2102         if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN) &&
2103             (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
2104                 skb = ppp_decompress_frame(ppp, skb);
2105
2106         if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
2107                 goto err;
2108
2109         proto = PPP_PROTO(skb);
2110         switch (proto) {
2111         case PPP_VJC_COMP:
2112                 /* decompress VJ compressed packets */
2113                 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
2114                         goto err;
2115
2116                 if (skb_tailroom(skb) < 124 || skb_cloned(skb)) {
2117                         /* copy to a new sk_buff with more tailroom */
2118                         ns = dev_alloc_skb(skb->len + 128);
2119                         if (!ns) {
2120                                 netdev_err(ppp->dev, "PPP: no memory "
2121                                            "(VJ decomp)\n");
2122                                 goto err;
2123                         }
2124                         skb_reserve(ns, 2);
2125                         skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
2126                         consume_skb(skb);
2127                         skb = ns;
2128                 }
2129                 else
2130                         skb->ip_summed = CHECKSUM_NONE;
2131
2132                 len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
2133                 if (len <= 0) {
2134                         netdev_printk(KERN_DEBUG, ppp->dev,
2135                                       "PPP: VJ decompression error\n");
2136                         goto err;
2137                 }
2138                 len += 2;
2139                 if (len > skb->len)
2140                         skb_put(skb, len - skb->len);
2141                 else if (len < skb->len)
2142                         skb_trim(skb, len);
2143                 proto = PPP_IP;
2144                 break;
2145
2146         case PPP_VJC_UNCOMP:
2147                 if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
2148                         goto err;
2149
2150                 /* Until we fix the decompressor need to make sure
2151                  * data portion is linear.
2152                  */
2153                 if (!pskb_may_pull(skb, skb->len))
2154                         goto err;
2155
2156                 if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
2157                         netdev_err(ppp->dev, "PPP: VJ uncompressed error\n");
2158                         goto err;
2159                 }
2160                 proto = PPP_IP;
2161                 break;
2162
2163         case PPP_CCP:
2164                 ppp_ccp_peek(ppp, skb, 1);
2165                 break;
2166         }
2167
2168         ++ppp->stats64.rx_packets;
2169         ppp->stats64.rx_bytes += skb->len - 2;
2170
2171         npi = proto_to_npindex(proto);
2172         if (npi < 0) {
2173                 /* control or unknown frame - pass it to pppd */
2174                 skb_queue_tail(&ppp->file.rq, skb);
2175                 /* limit queue length by dropping old frames */
2176                 while (ppp->file.rq.qlen > PPP_MAX_RQLEN &&
2177                        (skb = skb_dequeue(&ppp->file.rq)))
2178                         kfree_skb(skb);
2179                 /* wake up any process polling or blocking on read */
2180                 wake_up_interruptible(&ppp->file.rwait);
2181
2182         } else {
2183                 /* network protocol frame - give it to the kernel */
2184
2185 #ifdef CONFIG_PPP_FILTER
2186                 /* check if the packet passes the pass and active filters */
2187                 /* the filter instructions are constructed assuming
2188                    a four-byte PPP header on each packet */
2189                 if (ppp->pass_filter || ppp->active_filter) {
2190                         if (skb_unclone(skb, GFP_ATOMIC))
2191                                 goto err;
2192
2193                         *(u8 *)skb_push(skb, 2) = 0;
2194                         if (ppp->pass_filter &&
2195                             BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
2196                                 if (ppp->debug & 1)
2197                                         netdev_printk(KERN_DEBUG, ppp->dev,
2198                                                       "PPP: inbound frame "
2199                                                       "not passed\n");
2200                                 kfree_skb(skb);
2201                                 return;
2202                         }
2203                         if (!(ppp->active_filter &&
2204                               BPF_PROG_RUN(ppp->active_filter, skb) == 0))
2205                                 ppp->last_recv = jiffies;
2206                         __skb_pull(skb, 2);
2207                 } else
2208 #endif /* CONFIG_PPP_FILTER */
2209                         ppp->last_recv = jiffies;
2210
2211                 if ((ppp->dev->flags & IFF_UP) == 0 ||
2212                     ppp->npmode[npi] != NPMODE_PASS) {
2213                         kfree_skb(skb);
2214                 } else {
2215                         /* chop off protocol */
2216                         skb_pull_rcsum(skb, 2);
2217                         skb->dev = ppp->dev;
2218                         skb->protocol = htons(npindex_to_ethertype[npi]);
2219                         skb_reset_mac_header(skb);
2220                         skb_scrub_packet(skb, !net_eq(ppp->ppp_net,
2221                                                       dev_net(ppp->dev)));
2222                         netif_rx(skb);
2223                 }
2224         }
2225         return;
2226
2227  err:
2228         kfree_skb(skb);
2229         ppp_receive_error(ppp);
2230 }
2231
2232 static struct sk_buff *
2233 ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
2234 {
2235         int proto = PPP_PROTO(skb);
2236         struct sk_buff *ns;
2237         int len;
2238
2239         /* Until we fix all the decompressor's need to make sure
2240          * data portion is linear.
2241          */
2242         if (!pskb_may_pull(skb, skb->len))
2243                 goto err;
2244
2245         if (proto == PPP_COMP) {
2246                 int obuff_size;
2247
2248                 switch(ppp->rcomp->compress_proto) {
2249                 case CI_MPPE:
2250                         obuff_size = ppp->mru + PPP_HDRLEN + 1;
2251                         break;
2252                 default:
2253                         obuff_size = ppp->mru + PPP_HDRLEN;
2254                         break;
2255                 }
2256
2257                 ns = dev_alloc_skb(obuff_size);
2258                 if (!ns) {
2259                         netdev_err(ppp->dev, "ppp_decompress_frame: "
2260                                    "no memory\n");
2261                         goto err;
2262                 }
2263                 /* the decompressor still expects the A/C bytes in the hdr */
2264                 len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
2265                                 skb->len + 2, ns->data, obuff_size);
2266                 if (len < 0) {
2267                         /* Pass the compressed frame to pppd as an
2268                            error indication. */
2269                         if (len == DECOMP_FATALERROR)
2270                                 ppp->rstate |= SC_DC_FERROR;
2271                         kfree_skb(ns);
2272                         goto err;
2273                 }
2274
2275                 consume_skb(skb);
2276                 skb = ns;
2277                 skb_put(skb, len);
2278                 skb_pull(skb, 2);       /* pull off the A/C bytes */
2279
2280         } else {
2281                 /* Uncompressed frame - pass to decompressor so it
2282                    can update its dictionary if necessary. */
2283                 if (ppp->rcomp->incomp)
2284                         ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
2285                                            skb->len + 2);
2286         }
2287
2288         return skb;
2289
2290  err:
2291         ppp->rstate |= SC_DC_ERROR;
2292         ppp_receive_error(ppp);
2293         return skb;
2294 }
2295
2296 #ifdef CONFIG_PPP_MULTILINK
2297 /*
2298  * Receive a multilink frame.
2299  * We put it on the reconstruction queue and then pull off
2300  * as many completed frames as we can.
2301  */
2302 static void
2303 ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
2304 {
2305         u32 mask, seq;
2306         struct channel *ch;
2307         int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
2308
2309         if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
2310                 goto err;               /* no good, throw it away */
2311
2312         /* Decode sequence number and begin/end bits */
2313         if (ppp->flags & SC_MP_SHORTSEQ) {
2314                 seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
2315                 mask = 0xfff;
2316         } else {
2317                 seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
2318                 mask = 0xffffff;
2319         }
2320         PPP_MP_CB(skb)->BEbits = skb->data[2];
2321         skb_pull(skb, mphdrlen);        /* pull off PPP and MP headers */
2322
2323         /*
2324          * Do protocol ID decompression on the first fragment of each packet.
2325          */
2326         if ((PPP_MP_CB(skb)->BEbits & B) && (skb->data[0] & 1))
2327                 *(u8 *)skb_push(skb, 1) = 0;
2328
2329         /*
2330          * Expand sequence number to 32 bits, making it as close
2331          * as possible to ppp->minseq.
2332          */
2333         seq |= ppp->minseq & ~mask;
2334         if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
2335                 seq += mask + 1;
2336         else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
2337                 seq -= mask + 1;        /* should never happen */
2338         PPP_MP_CB(skb)->sequence = seq;
2339         pch->lastseq = seq;
2340
2341         /*
2342          * If this packet comes before the next one we were expecting,
2343          * drop it.
2344          */
2345         if (seq_before(seq, ppp->nextseq)) {
2346                 kfree_skb(skb);
2347                 ++ppp->dev->stats.rx_dropped;
2348                 ppp_receive_error(ppp);
2349                 return;
2350         }
2351
2352         /*
2353          * Reevaluate minseq, the minimum over all channels of the
2354          * last sequence number received on each channel.  Because of
2355          * the increasing sequence number rule, we know that any fragment
2356          * before `minseq' which hasn't arrived is never going to arrive.
2357          * The list of channels can't change because we have the receive
2358          * side of the ppp unit locked.
2359          */
2360         list_for_each_entry(ch, &ppp->channels, clist) {
2361                 if (seq_before(ch->lastseq, seq))
2362                         seq = ch->lastseq;
2363         }
2364         if (seq_before(ppp->minseq, seq))
2365                 ppp->minseq = seq;
2366
2367         /* Put the fragment on the reconstruction queue */
2368         ppp_mp_insert(ppp, skb);
2369
2370         /* If the queue is getting long, don't wait any longer for packets
2371            before the start of the queue. */
2372         if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN) {
2373                 struct sk_buff *mskb = skb_peek(&ppp->mrq);
2374                 if (seq_before(ppp->minseq, PPP_MP_CB(mskb)->sequence))
2375                         ppp->minseq = PPP_MP_CB(mskb)->sequence;
2376         }
2377
2378         /* Pull completed packets off the queue and receive them. */
2379         while ((skb = ppp_mp_reconstruct(ppp))) {
2380                 if (pskb_may_pull(skb, 2))
2381                         ppp_receive_nonmp_frame(ppp, skb);
2382                 else {
2383                         ++ppp->dev->stats.rx_length_errors;
2384                         kfree_skb(skb);
2385                         ppp_receive_error(ppp);
2386                 }
2387         }
2388
2389         return;
2390
2391  err:
2392         kfree_skb(skb);
2393         ppp_receive_error(ppp);
2394 }
2395
2396 /*
2397  * Insert a fragment on the MP reconstruction queue.
2398  * The queue is ordered by increasing sequence number.
2399  */
2400 static void
2401 ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
2402 {
2403         struct sk_buff *p;
2404         struct sk_buff_head *list = &ppp->mrq;
2405         u32 seq = PPP_MP_CB(skb)->sequence;
2406
2407         /* N.B. we don't need to lock the list lock because we have the
2408            ppp unit receive-side lock. */
2409         skb_queue_walk(list, p) {
2410                 if (seq_before(seq, PPP_MP_CB(p)->sequence))
2411                         break;
2412         }
2413         __skb_queue_before(list, p, skb);
2414 }
2415
2416 /*
2417  * Reconstruct a packet from the MP fragment queue.
2418  * We go through increasing sequence numbers until we find a
2419  * complete packet, or we get to the sequence number for a fragment
2420  * which hasn't arrived but might still do so.
2421  */
2422 static struct sk_buff *
2423 ppp_mp_reconstruct(struct ppp *ppp)
2424 {
2425         u32 seq = ppp->nextseq;
2426         u32 minseq = ppp->minseq;
2427         struct sk_buff_head *list = &ppp->mrq;
2428         struct sk_buff *p, *tmp;
2429         struct sk_buff *head, *tail;
2430         struct sk_buff *skb = NULL;
2431         int lost = 0, len = 0;
2432
2433         if (ppp->mrru == 0)     /* do nothing until mrru is set */
2434                 return NULL;
2435         head = list->next;
2436         tail = NULL;
2437         skb_queue_walk_safe(list, p, tmp) {
2438         again:
2439                 if (seq_before(PPP_MP_CB(p)->sequence, seq)) {
2440                         /* this can't happen, anyway ignore the skb */
2441                         netdev_err(ppp->dev, "ppp_mp_reconstruct bad "
2442                                    "seq %u < %u\n",
2443                                    PPP_MP_CB(p)->sequence, seq);
2444                         __skb_unlink(p, list);
2445                         kfree_skb(p);
2446                         continue;
2447                 }
2448                 if (PPP_MP_CB(p)->sequence != seq) {
2449                         u32 oldseq;
2450                         /* Fragment `seq' is missing.  If it is after
2451                            minseq, it might arrive later, so stop here. */
2452                         if (seq_after(seq, minseq))
2453                                 break;
2454                         /* Fragment `seq' is lost, keep going. */
2455                         lost = 1;
2456                         oldseq = seq;
2457                         seq = seq_before(minseq, PPP_MP_CB(p)->sequence)?
2458                                 minseq + 1: PPP_MP_CB(p)->sequence;
2459
2460                         if (ppp->debug & 1)
2461                                 netdev_printk(KERN_DEBUG, ppp->dev,
2462                                               "lost frag %u..%u\n",
2463                                               oldseq, seq-1);
2464
2465                         goto again;
2466                 }
2467
2468                 /*
2469                  * At this point we know that all the fragments from
2470                  * ppp->nextseq to seq are either present or lost.
2471                  * Also, there are no complete packets in the queue
2472                  * that have no missing fragments and end before this
2473                  * fragment.
2474                  */
2475
2476                 /* B bit set indicates this fragment starts a packet */
2477                 if (PPP_MP_CB(p)->BEbits & B) {
2478                         head = p;
2479                         lost = 0;
2480                         len = 0;
2481                 }
2482
2483                 len += p->len;
2484
2485                 /* Got a complete packet yet? */
2486                 if (lost == 0 && (PPP_MP_CB(p)->BEbits & E) &&
2487                     (PPP_MP_CB(head)->BEbits & B)) {
2488                         if (len > ppp->mrru + 2) {
2489                                 ++ppp->dev->stats.rx_length_errors;
2490                                 netdev_printk(KERN_DEBUG, ppp->dev,
2491                                               "PPP: reconstructed packet"
2492                                               " is too long (%d)\n", len);
2493                         } else {
2494                                 tail = p;
2495                                 break;
2496                         }
2497                         ppp->nextseq = seq + 1;
2498                 }
2499
2500                 /*
2501                  * If this is the ending fragment of a packet,
2502                  * and we haven't found a complete valid packet yet,
2503                  * we can discard up to and including this fragment.
2504                  */
2505                 if (PPP_MP_CB(p)->BEbits & E) {
2506                         struct sk_buff *tmp2;
2507
2508                         skb_queue_reverse_walk_from_safe(list, p, tmp2) {
2509                                 if (ppp->debug & 1)
2510                                         netdev_printk(KERN_DEBUG, ppp->dev,
2511                                                       "discarding frag %u\n",
2512                                                       PPP_MP_CB(p)->sequence);
2513                                 __skb_unlink(p, list);
2514                                 kfree_skb(p);
2515                         }
2516                         head = skb_peek(list);
2517                         if (!head)
2518                                 break;
2519                 }
2520                 ++seq;
2521         }
2522
2523         /* If we have a complete packet, copy it all into one skb. */
2524         if (tail != NULL) {
2525                 /* If we have discarded any fragments,
2526                    signal a receive error. */
2527                 if (PPP_MP_CB(head)->sequence != ppp->nextseq) {
2528                         skb_queue_walk_safe(list, p, tmp) {
2529                                 if (p == head)
2530                                         break;
2531                                 if (ppp->debug & 1)
2532                                         netdev_printk(KERN_DEBUG, ppp->dev,
2533                                                       "discarding frag %u\n",
2534                                                       PPP_MP_CB(p)->sequence);
2535                                 __skb_unlink(p, list);
2536                                 kfree_skb(p);
2537                         }
2538
2539                         if (ppp->debug & 1)
2540                                 netdev_printk(KERN_DEBUG, ppp->dev,
2541                                               "  missed pkts %u..%u\n",
2542                                               ppp->nextseq,
2543                                               PPP_MP_CB(head)->sequence-1);
2544                         ++ppp->dev->stats.rx_dropped;
2545                         ppp_receive_error(ppp);
2546                 }
2547
2548                 skb = head;
2549                 if (head != tail) {
2550                         struct sk_buff **fragpp = &skb_shinfo(skb)->frag_list;
2551                         p = skb_queue_next(list, head);
2552                         __skb_unlink(skb, list);
2553                         skb_queue_walk_from_safe(list, p, tmp) {
2554                                 __skb_unlink(p, list);
2555                                 *fragpp = p;
2556                                 p->next = NULL;
2557                                 fragpp = &p->next;
2558
2559                                 skb->len += p->len;
2560                                 skb->data_len += p->len;
2561                                 skb->truesize += p->truesize;
2562
2563                                 if (p == tail)
2564                                         break;
2565                         }
2566                 } else {
2567                         __skb_unlink(skb, list);
2568                 }
2569
2570                 ppp->nextseq = PPP_MP_CB(tail)->sequence + 1;
2571         }
2572
2573         return skb;
2574 }
2575 #endif /* CONFIG_PPP_MULTILINK */
2576
2577 /*
2578  * Channel interface.
2579  */
2580
2581 /* Create a new, unattached ppp channel. */
2582 int ppp_register_channel(struct ppp_channel *chan)
2583 {
2584         return ppp_register_net_channel(current->nsproxy->net_ns, chan);
2585 }
2586
2587 /* Create a new, unattached ppp channel for specified net. */
2588 int ppp_register_net_channel(struct net *net, struct ppp_channel *chan)
2589 {
2590         struct channel *pch;
2591         struct ppp_net *pn;
2592
2593         pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
2594         if (!pch)
2595                 return -ENOMEM;
2596
2597         pn = ppp_pernet(net);
2598
2599         pch->ppp = NULL;
2600         pch->chan = chan;
2601         pch->chan_net = get_net(net);
2602         chan->ppp = pch;
2603         init_ppp_file(&pch->file, CHANNEL);
2604         pch->file.hdrlen = chan->hdrlen;
2605 #ifdef CONFIG_PPP_MULTILINK
2606         pch->lastseq = -1;
2607 #endif /* CONFIG_PPP_MULTILINK */
2608         init_rwsem(&pch->chan_sem);
2609         spin_lock_init(&pch->downl);
2610         rwlock_init(&pch->upl);
2611
2612         spin_lock_bh(&pn->all_channels_lock);
2613         pch->file.index = ++pn->last_channel_index;
2614         list_add(&pch->list, &pn->new_channels);
2615         atomic_inc(&channel_count);
2616         spin_unlock_bh(&pn->all_channels_lock);
2617
2618         return 0;
2619 }
2620
2621 /*
2622  * Return the index of a channel.
2623  */
2624 int ppp_channel_index(struct ppp_channel *chan)
2625 {
2626         struct channel *pch = chan->ppp;
2627
2628         if (pch)
2629                 return pch->file.index;
2630         return -1;
2631 }
2632
2633 /*
2634  * Return the PPP unit number to which a channel is connected.
2635  */
2636 int ppp_unit_number(struct ppp_channel *chan)
2637 {
2638         struct channel *pch = chan->ppp;
2639         int unit = -1;
2640
2641         if (pch) {
2642                 read_lock_bh(&pch->upl);
2643                 if (pch->ppp)
2644                         unit = pch->ppp->file.index;
2645                 read_unlock_bh(&pch->upl);
2646         }
2647         return unit;
2648 }
2649
2650 /*
2651  * Return the PPP device interface name of a channel.
2652  */
2653 char *ppp_dev_name(struct ppp_channel *chan)
2654 {
2655         struct channel *pch = chan->ppp;
2656         char *name = NULL;
2657
2658         if (pch) {
2659                 read_lock_bh(&pch->upl);
2660                 if (pch->ppp && pch->ppp->dev)
2661                         name = pch->ppp->dev->name;
2662                 read_unlock_bh(&pch->upl);
2663         }
2664         return name;
2665 }
2666
2667
2668 /*
2669  * Disconnect a channel from the generic layer.
2670  * This must be called in process context.
2671  */
2672 void
2673 ppp_unregister_channel(struct ppp_channel *chan)
2674 {
2675         struct channel *pch = chan->ppp;
2676         struct ppp_net *pn;
2677
2678         if (!pch)
2679                 return;         /* should never happen */
2680
2681         chan->ppp = NULL;
2682
2683         /*
2684          * This ensures that we have returned from any calls into the
2685          * the channel's start_xmit or ioctl routine before we proceed.
2686          */
2687         down_write(&pch->chan_sem);
2688         spin_lock_bh(&pch->downl);
2689         pch->chan = NULL;
2690         spin_unlock_bh(&pch->downl);
2691         up_write(&pch->chan_sem);
2692         ppp_disconnect_channel(pch);
2693
2694         pn = ppp_pernet(pch->chan_net);
2695         spin_lock_bh(&pn->all_channels_lock);
2696         list_del(&pch->list);
2697         spin_unlock_bh(&pn->all_channels_lock);
2698
2699         pch->file.dead = 1;
2700         wake_up_interruptible(&pch->file.rwait);
2701         if (atomic_dec_and_test(&pch->file.refcnt))
2702                 ppp_destroy_channel(pch);
2703 }
2704
2705 /*
2706  * Callback from a channel when it can accept more to transmit.
2707  * This should be called at BH/softirq level, not interrupt level.
2708  */
2709 void
2710 ppp_output_wakeup(struct ppp_channel *chan)
2711 {
2712         struct channel *pch = chan->ppp;
2713
2714         if (!pch)
2715                 return;
2716         ppp_channel_push(pch);
2717 }
2718
2719 /*
2720  * Compression control.
2721  */
2722
2723 /* Process the PPPIOCSCOMPRESS ioctl. */
2724 static int
2725 ppp_set_compress(struct ppp *ppp, unsigned long arg)
2726 {
2727         int err;
2728         struct compressor *cp, *ocomp;
2729         struct ppp_option_data data;
2730         void *state, *ostate;
2731         unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];
2732
2733         err = -EFAULT;
2734         if (copy_from_user(&data, (void __user *) arg, sizeof(data)))
2735                 goto out;
2736         if (data.length > CCP_MAX_OPTION_LENGTH)
2737                 goto out;
2738         if (copy_from_user(ccp_option, (void __user *) data.ptr, data.length))
2739                 goto out;
2740
2741         err = -EINVAL;
2742         if (data.length < 2 || ccp_option[1] < 2 || ccp_option[1] > data.length)
2743                 goto out;
2744
2745         cp = try_then_request_module(
2746                 find_compressor(ccp_option[0]),
2747                 "ppp-compress-%d", ccp_option[0]);
2748         if (!cp)
2749                 goto out;
2750
2751         err = -ENOBUFS;
2752         if (data.transmit) {
2753                 state = cp->comp_alloc(ccp_option, data.length);
2754                 if (state) {
2755                         ppp_xmit_lock(ppp);
2756                         ppp->xstate &= ~SC_COMP_RUN;
2757                         ocomp = ppp->xcomp;
2758                         ostate = ppp->xc_state;
2759                         ppp->xcomp = cp;
2760                         ppp->xc_state = state;
2761                         ppp_xmit_unlock(ppp);
2762                         if (ostate) {
2763                                 ocomp->comp_free(ostate);
2764                                 module_put(ocomp->owner);
2765                         }
2766                         err = 0;
2767                 } else
2768                         module_put(cp->owner);
2769
2770         } else {
2771                 state = cp->decomp_alloc(ccp_option, data.length);
2772                 if (state) {
2773                         ppp_recv_lock(ppp);
2774                         ppp->rstate &= ~SC_DECOMP_RUN;
2775                         ocomp = ppp->rcomp;
2776                         ostate = ppp->rc_state;
2777                         ppp->rcomp = cp;
2778                         ppp->rc_state = state;
2779                         ppp_recv_unlock(ppp);
2780                         if (ostate) {
2781                                 ocomp->decomp_free(ostate);
2782                                 module_put(ocomp->owner);
2783                         }
2784                         err = 0;
2785                 } else
2786                         module_put(cp->owner);
2787         }
2788
2789  out:
2790         return err;
2791 }
2792
2793 /*
2794  * Look at a CCP packet and update our state accordingly.
2795  * We assume the caller has the xmit or recv path locked.
2796  */
2797 static void
2798 ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
2799 {
2800         unsigned char *dp;
2801         int len;
2802
2803         if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
2804                 return; /* no header */
2805         dp = skb->data + 2;
2806
2807         switch (CCP_CODE(dp)) {
2808         case CCP_CONFREQ:
2809
2810                 /* A ConfReq starts negotiation of compression
2811                  * in one direction of transmission,
2812                  * and hence brings it down...but which way?
2813                  *
2814                  * Remember:
2815                  * A ConfReq indicates what the sender would like to receive
2816                  */
2817                 if(inbound)
2818                         /* He is proposing what I should send */
2819                         ppp->xstate &= ~SC_COMP_RUN;
2820                 else
2821                         /* I am proposing to what he should send */
2822                         ppp->rstate &= ~SC_DECOMP_RUN;
2823
2824                 break;
2825
2826         case CCP_TERMREQ:
2827         case CCP_TERMACK:
2828                 /*
2829                  * CCP is going down, both directions of transmission
2830                  */
2831                 ppp->rstate &= ~SC_DECOMP_RUN;
2832                 ppp->xstate &= ~SC_COMP_RUN;
2833                 break;
2834
2835         case CCP_CONFACK:
2836                 if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
2837                         break;
2838                 len = CCP_LENGTH(dp);
2839                 if (!pskb_may_pull(skb, len + 2))
2840                         return;         /* too short */
2841                 dp += CCP_HDRLEN;
2842                 len -= CCP_HDRLEN;
2843                 if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
2844                         break;
2845                 if (inbound) {
2846                         /* we will start receiving compressed packets */
2847                         if (!ppp->rc_state)
2848                                 break;
2849                         if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
2850                                         ppp->file.index, 0, ppp->mru, ppp->debug)) {
2851                                 ppp->rstate |= SC_DECOMP_RUN;
2852                                 ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
2853                         }
2854                 } else {
2855                         /* we will soon start sending compressed packets */
2856                         if (!ppp->xc_state)
2857                                 break;
2858                         if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
2859                                         ppp->file.index, 0, ppp->debug))
2860                                 ppp->xstate |= SC_COMP_RUN;
2861                 }
2862                 break;
2863
2864         case CCP_RESETACK:
2865                 /* reset the [de]compressor */
2866                 if ((ppp->flags & SC_CCP_UP) == 0)
2867                         break;
2868                 if (inbound) {
2869                         if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
2870                                 ppp->rcomp->decomp_reset(ppp->rc_state);
2871                                 ppp->rstate &= ~SC_DC_ERROR;
2872                         }
2873                 } else {
2874                         if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
2875                                 ppp->xcomp->comp_reset(ppp->xc_state);
2876                 }
2877                 break;
2878         }
2879 }
2880
2881 /* Free up compression resources. */
2882 static void
2883 ppp_ccp_closed(struct ppp *ppp)
2884 {
2885         void *xstate, *rstate;
2886         struct compressor *xcomp, *rcomp;
2887
2888         ppp_lock(ppp);
2889         ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
2890         ppp->xstate = 0;
2891         xcomp = ppp->xcomp;
2892         xstate = ppp->xc_state;
2893         ppp->xc_state = NULL;
2894         ppp->rstate = 0;
2895         rcomp = ppp->rcomp;
2896         rstate = ppp->rc_state;
2897         ppp->rc_state = NULL;
2898         ppp_unlock(ppp);
2899
2900         if (xstate) {
2901                 xcomp->comp_free(xstate);
2902                 module_put(xcomp->owner);
2903         }
2904         if (rstate) {
2905                 rcomp->decomp_free(rstate);
2906                 module_put(rcomp->owner);
2907         }
2908 }
2909
2910 /* List of compressors. */
2911 static LIST_HEAD(compressor_list);
2912 static DEFINE_SPINLOCK(compressor_list_lock);
2913
2914 struct compressor_entry {
2915         struct list_head list;
2916         struct compressor *comp;
2917 };
2918
2919 static struct compressor_entry *
2920 find_comp_entry(int proto)
2921 {
2922         struct compressor_entry *ce;
2923
2924         list_for_each_entry(ce, &compressor_list, list) {
2925                 if (ce->comp->compress_proto == proto)
2926                         return ce;
2927         }
2928         return NULL;
2929 }
2930
2931 /* Register a compressor */
2932 int
2933 ppp_register_compressor(struct compressor *cp)
2934 {
2935         struct compressor_entry *ce;
2936         int ret;
2937         spin_lock(&compressor_list_lock);
2938         ret = -EEXIST;
2939         if (find_comp_entry(cp->compress_proto))
2940                 goto out;
2941         ret = -ENOMEM;
2942         ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
2943         if (!ce)
2944                 goto out;
2945         ret = 0;
2946         ce->comp = cp;
2947         list_add(&ce->list, &compressor_list);
2948  out:
2949         spin_unlock(&compressor_list_lock);
2950         return ret;
2951 }
2952
2953 /* Unregister a compressor */
2954 void
2955 ppp_unregister_compressor(struct compressor *cp)
2956 {
2957         struct compressor_entry *ce;
2958
2959         spin_lock(&compressor_list_lock);
2960         ce = find_comp_entry(cp->compress_proto);
2961         if (ce && ce->comp == cp) {
2962                 list_del(&ce->list);
2963                 kfree(ce);
2964         }
2965         spin_unlock(&compressor_list_lock);
2966 }
2967
2968 /* Find a compressor. */
2969 static struct compressor *
2970 find_compressor(int type)
2971 {
2972         struct compressor_entry *ce;
2973         struct compressor *cp = NULL;
2974
2975         spin_lock(&compressor_list_lock);
2976         ce = find_comp_entry(type);
2977         if (ce) {
2978                 cp = ce->comp;
2979                 if (!try_module_get(cp->owner))
2980                         cp = NULL;
2981         }
2982         spin_unlock(&compressor_list_lock);
2983         return cp;
2984 }
2985
2986 /*
2987  * Miscelleneous stuff.
2988  */
2989
2990 static void
2991 ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
2992 {
2993         struct slcompress *vj = ppp->vj;
2994
2995         memset(st, 0, sizeof(*st));
2996         st->p.ppp_ipackets = ppp->stats64.rx_packets;
2997         st->p.ppp_ierrors = ppp->dev->stats.rx_errors;
2998         st->p.ppp_ibytes = ppp->stats64.rx_bytes;
2999         st->p.ppp_opackets = ppp->stats64.tx_packets;
3000         st->p.ppp_oerrors = ppp->dev->stats.tx_errors;
3001         st->p.ppp_obytes = ppp->stats64.tx_bytes;
3002         if (!vj)
3003                 return;
3004         st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
3005         st->vj.vjs_compressed = vj->sls_o_compressed;
3006         st->vj.vjs_searches = vj->sls_o_searches;
3007         st->vj.vjs_misses = vj->sls_o_misses;
3008         st->vj.vjs_errorin = vj->sls_i_error;
3009         st->vj.vjs_tossed = vj->sls_i_tossed;
3010         st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
3011         st->vj.vjs_compressedin = vj->sls_i_compressed;
3012 }
3013
3014 /*
3015  * Stuff for handling the lists of ppp units and channels
3016  * and for initialization.
3017  */
3018
3019 /*
3020  * Create a new ppp interface unit.  Fails if it can't allocate memory
3021  * or if there is already a unit with the requested number.
3022  * unit == -1 means allocate a new number.
3023  */
3024 static int ppp_create_interface(struct net *net, struct file *file, int *unit)
3025 {
3026         struct ppp_config conf = {
3027                 .file = file,
3028                 .unit = *unit,
3029                 .ifname_is_set = false,
3030         };
3031         struct net_device *dev;
3032         struct ppp *ppp;
3033         int err;
3034
3035         dev = alloc_netdev(sizeof(struct ppp), "", NET_NAME_ENUM, ppp_setup);
3036         if (!dev) {
3037                 err = -ENOMEM;
3038                 goto err;
3039         }
3040         dev_net_set(dev, net);
3041         dev->rtnl_link_ops = &ppp_link_ops;
3042
3043         rtnl_lock();
3044
3045         err = ppp_dev_configure(net, dev, &conf);
3046         if (err < 0)
3047                 goto err_dev;
3048         ppp = netdev_priv(dev);
3049         *unit = ppp->file.index;
3050
3051         rtnl_unlock();
3052
3053         return 0;
3054
3055 err_dev:
3056         rtnl_unlock();
3057         free_netdev(dev);
3058 err:
3059         return err;
3060 }
3061
3062 /*
3063  * Initialize a ppp_file structure.
3064  */
3065 static void
3066 init_ppp_file(struct ppp_file *pf, int kind)
3067 {
3068         pf->kind = kind;
3069         skb_queue_head_init(&pf->xq);
3070         skb_queue_head_init(&pf->rq);
3071         atomic_set(&pf->refcnt, 1);
3072         init_waitqueue_head(&pf->rwait);
3073 }
3074
3075 /*
3076  * Free the memory used by a ppp unit.  This is only called once
3077  * there are no channels connected to the unit and no file structs
3078  * that reference the unit.
3079  */
3080 static void ppp_destroy_interface(struct ppp *ppp)
3081 {
3082         atomic_dec(&ppp_unit_count);
3083
3084         if (!ppp->file.dead || ppp->n_channels) {
3085                 /* "can't happen" */
3086                 netdev_err(ppp->dev, "ppp: destroying ppp struct %p "
3087                            "but dead=%d n_channels=%d !\n",
3088                            ppp, ppp->file.dead, ppp->n_channels);
3089                 return;
3090         }
3091
3092         ppp_ccp_closed(ppp);
3093         if (ppp->vj) {
3094                 slhc_free(ppp->vj);
3095                 ppp->vj = NULL;
3096         }
3097         skb_queue_purge(&ppp->file.xq);
3098         skb_queue_purge(&ppp->file.rq);
3099 #ifdef CONFIG_PPP_MULTILINK
3100         skb_queue_purge(&ppp->mrq);
3101 #endif /* CONFIG_PPP_MULTILINK */
3102 #ifdef CONFIG_PPP_FILTER
3103         if (ppp->pass_filter) {
3104                 bpf_prog_destroy(ppp->pass_filter);
3105                 ppp->pass_filter = NULL;
3106         }
3107
3108         if (ppp->active_filter) {
3109                 bpf_prog_destroy(ppp->active_filter);
3110                 ppp->active_filter = NULL;
3111         }
3112 #endif /* CONFIG_PPP_FILTER */
3113
3114         kfree_skb(ppp->xmit_pending);
3115         free_percpu(ppp->xmit_recursion);
3116
3117         free_netdev(ppp->dev);
3118 }
3119
3120 /*
3121  * Locate an existing ppp unit.
3122  * The caller should have locked the all_ppp_mutex.
3123  */
3124 static struct ppp *
3125 ppp_find_unit(struct ppp_net *pn, int unit)
3126 {
3127         return unit_find(&pn->units_idr, unit);
3128 }
3129
3130 /*
3131  * Locate an existing ppp channel.
3132  * The caller should have locked the all_channels_lock.
3133  * First we look in the new_channels list, then in the
3134  * all_channels list.  If found in the new_channels list,
3135  * we move it to the all_channels list.  This is for speed
3136  * when we have a lot of channels in use.
3137  */
3138 static struct channel *
3139 ppp_find_channel(struct ppp_net *pn, int unit)
3140 {
3141         struct channel *pch;
3142
3143         list_for_each_entry(pch, &pn->new_channels, list) {
3144                 if (pch->file.index == unit) {
3145                         list_move(&pch->list, &pn->all_channels);
3146                         return pch;
3147                 }
3148         }
3149
3150         list_for_each_entry(pch, &pn->all_channels, list) {
3151                 if (pch->file.index == unit)
3152                         return pch;
3153         }
3154
3155         return NULL;
3156 }
3157
3158 /*
3159  * Connect a PPP channel to a PPP interface unit.
3160  */
3161 static int
3162 ppp_connect_channel(struct channel *pch, int unit)
3163 {
3164         struct ppp *ppp;
3165         struct ppp_net *pn;
3166         int ret = -ENXIO;
3167         int hdrlen;
3168
3169         pn = ppp_pernet(pch->chan_net);
3170
3171         mutex_lock(&pn->all_ppp_mutex);
3172         ppp = ppp_find_unit(pn, unit);
3173         if (!ppp)
3174                 goto out;
3175         write_lock_bh(&pch->upl);
3176         ret = -EINVAL;
3177         if (pch->ppp)
3178                 goto outl;
3179
3180         ppp_lock(ppp);
3181         spin_lock_bh(&pch->downl);
3182         if (!pch->chan) {
3183                 /* Don't connect unregistered channels */
3184                 spin_unlock_bh(&pch->downl);
3185                 ppp_unlock(ppp);
3186                 ret = -ENOTCONN;
3187                 goto outl;
3188         }
3189         spin_unlock_bh(&pch->downl);
3190         if (pch->file.hdrlen > ppp->file.hdrlen)
3191                 ppp->file.hdrlen = pch->file.hdrlen;
3192         hdrlen = pch->file.hdrlen + 2;  /* for protocol bytes */
3193         if (hdrlen > ppp->dev->hard_header_len)
3194                 ppp->dev->hard_header_len = hdrlen;
3195         list_add_tail(&pch->clist, &ppp->channels);
3196         ++ppp->n_channels;
3197         pch->ppp = ppp;
3198         atomic_inc(&ppp->file.refcnt);
3199         ppp_unlock(ppp);
3200         ret = 0;
3201
3202  outl:
3203         write_unlock_bh(&pch->upl);
3204  out:
3205         mutex_unlock(&pn->all_ppp_mutex);
3206         return ret;
3207 }
3208
3209 /*
3210  * Disconnect a channel from its ppp unit.
3211  */
3212 static int
3213 ppp_disconnect_channel(struct channel *pch)
3214 {
3215         struct ppp *ppp;
3216         int err = -EINVAL;
3217
3218         write_lock_bh(&pch->upl);
3219         ppp = pch->ppp;
3220         pch->ppp = NULL;
3221         write_unlock_bh(&pch->upl);
3222         if (ppp) {
3223                 /* remove it from the ppp unit's list */
3224                 ppp_lock(ppp);
3225                 list_del(&pch->clist);
3226                 if (--ppp->n_channels == 0)
3227                         wake_up_interruptible(&ppp->file.rwait);
3228                 ppp_unlock(ppp);
3229                 if (atomic_dec_and_test(&ppp->file.refcnt))
3230                         ppp_destroy_interface(ppp);
3231                 err = 0;
3232         }
3233         return err;
3234 }
3235
3236 /*
3237  * Free up the resources used by a ppp channel.
3238  */
3239 static void ppp_destroy_channel(struct channel *pch)
3240 {
3241         put_net(pch->chan_net);
3242         pch->chan_net = NULL;
3243
3244         atomic_dec(&channel_count);
3245
3246         if (!pch->file.dead) {
3247                 /* "can't happen" */
3248                 pr_err("ppp: destroying undead channel %p !\n", pch);
3249                 return;
3250         }
3251         skb_queue_purge(&pch->file.xq);
3252         skb_queue_purge(&pch->file.rq);
3253         kfree(pch);
3254 }
3255
3256 static void __exit ppp_cleanup(void)
3257 {
3258         /* should never happen */
3259         if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
3260                 pr_err("PPP: removing module but units remain!\n");
3261         rtnl_link_unregister(&ppp_link_ops);
3262         unregister_chrdev(PPP_MAJOR, "ppp");
3263         device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
3264         class_destroy(ppp_class);
3265         unregister_pernet_device(&ppp_net_ops);
3266 }
3267
3268 /*
3269  * Units handling. Caller must protect concurrent access
3270  * by holding all_ppp_mutex
3271  */
3272
3273 /* associate pointer with specified number */
3274 static int unit_set(struct idr *p, void *ptr, int n)
3275 {
3276         int unit;
3277
3278         unit = idr_alloc(p, ptr, n, n + 1, GFP_KERNEL);
3279         if (unit == -ENOSPC)
3280                 unit = -EINVAL;
3281         return unit;
3282 }
3283
3284 /* get new free unit number and associate pointer with it */
3285 static int unit_get(struct idr *p, void *ptr, int min)
3286 {
3287         return idr_alloc(p, ptr, min, 0, GFP_KERNEL);
3288 }
3289
3290 /* put unit number back to a pool */
3291 static void unit_put(struct idr *p, int n)
3292 {
3293         idr_remove(p, n);
3294 }
3295
3296 /* get pointer associated with the number */
3297 static void *unit_find(struct idr *p, int n)
3298 {
3299         return idr_find(p, n);
3300 }
3301
3302 /* Module/initialization stuff */
3303
3304 module_init(ppp_init);
3305 module_exit(ppp_cleanup);
3306
3307 EXPORT_SYMBOL(ppp_register_net_channel);
3308 EXPORT_SYMBOL(ppp_register_channel);
3309 EXPORT_SYMBOL(ppp_unregister_channel);
3310 EXPORT_SYMBOL(ppp_channel_index);
3311 EXPORT_SYMBOL(ppp_unit_number);
3312 EXPORT_SYMBOL(ppp_dev_name);
3313 EXPORT_SYMBOL(ppp_input);
3314 EXPORT_SYMBOL(ppp_input_error);
3315 EXPORT_SYMBOL(ppp_output_wakeup);
3316 EXPORT_SYMBOL(ppp_register_compressor);
3317 EXPORT_SYMBOL(ppp_unregister_compressor);
3318 MODULE_LICENSE("GPL");
3319 MODULE_ALIAS_CHARDEV(PPP_MAJOR, 0);
3320 MODULE_ALIAS_RTNL_LINK("ppp");
3321 MODULE_ALIAS("devname:ppp");