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