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