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