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