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