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