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