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