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