ppp_generic.c 76.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Generic PPP layer for Linux.
 *
 * Copyright 1999-2002 Paul Mackerras.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version
 *  2 of the License, or (at your option) any later version.
 *
 * The generic PPP layer handles the PPP network interfaces, the
 * /dev/ppp device, packet and VJ compression, and multilink.
 * It talks to PPP `channels' via the interface defined in
 * include/linux/ppp_channel.h.  Channels provide the basic means for
 * sending and receiving PPP frames on some kind of communications
 * channel.
 *
 * Part of the code in this driver was inspired by the old async-only
 * PPP driver, written by Michael Callahan and Al Longyear, and
 * subsequently hacked by Paul Mackerras.
 *
 * ==FILEVERSION 20041108==
 */

#include <linux/module.h>
#include <linux/kernel.h>
27
#include <linux/sched/signal.h>
L
Linus Torvalds 已提交
28 29 30
#include <linux/kmod.h>
#include <linux/init.h>
#include <linux/list.h>
31
#include <linux/idr.h>
L
Linus Torvalds 已提交
32 33 34 35
#include <linux/netdevice.h>
#include <linux/poll.h>
#include <linux/ppp_defs.h>
#include <linux/filter.h>
36
#include <linux/ppp-ioctl.h>
L
Linus Torvalds 已提交
37 38 39 40 41 42 43 44 45 46 47
#include <linux/ppp_channel.h>
#include <linux/ppp-comp.h>
#include <linux/skbuff.h>
#include <linux/rtnetlink.h>
#include <linux/if_arp.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/spinlock.h>
#include <linux/rwsem.h>
#include <linux/stddef.h>
#include <linux/device.h>
48
#include <linux/mutex.h>
49
#include <linux/slab.h>
50
#include <linux/file.h>
51
#include <asm/unaligned.h>
L
Linus Torvalds 已提交
52
#include <net/slhc_vj.h>
A
Arun Sharma 已提交
53
#include <linux/atomic.h>
54
#include <linux/refcount.h>
L
Linus Torvalds 已提交
55

56 57 58 59
#include <linux/nsproxy.h>
#include <net/net_namespace.h>
#include <net/netns/generic.h>

L
Linus Torvalds 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#define PPP_VERSION	"2.4.2"

/*
 * Network protocols we support.
 */
#define NP_IP	0		/* Internet Protocol V4 */
#define NP_IPV6	1		/* Internet Protocol V6 */
#define NP_IPX	2		/* IPX protocol */
#define NP_AT	3		/* Appletalk protocol */
#define NP_MPLS_UC 4		/* MPLS unicast */
#define NP_MPLS_MC 5		/* MPLS multicast */
#define NUM_NP	6		/* Number of NPs. */

#define MPHDRLEN	6	/* multilink protocol header length */
#define MPHDRLEN_SSN	4	/* ditto with short sequence numbers */

/*
 * An instance of /dev/ppp can be associated with either a ppp
 * interface unit or a ppp channel.  In both cases, file->private_data
 * points to one of these.
 */
struct ppp_file {
	enum {
		INTERFACE=1, CHANNEL
	}		kind;
	struct sk_buff_head xq;		/* pppd transmit queue */
	struct sk_buff_head rq;		/* receive queue for pppd */
	wait_queue_head_t rwait;	/* for poll on reading /dev/ppp */
88
	refcount_t	refcnt;		/* # refs (incl /dev/ppp attached) */
L
Linus Torvalds 已提交
89 90 91 92 93
	int		hdrlen;		/* space to leave for headers */
	int		index;		/* interface unit / channel number */
	int		dead;		/* unit/channel has been shut down */
};

94
#define PF_TO_X(pf, X)		container_of(pf, X, file)
L
Linus Torvalds 已提交
95 96 97 98

#define PF_TO_PPP(pf)		PF_TO_X(pf, struct ppp)
#define PF_TO_CHANNEL(pf)	PF_TO_X(pf, struct channel)

K
Kevin Groeneveld 已提交
99 100 101 102 103 104 105 106 107 108 109 110
/*
 * Data structure to hold primary network stats for which
 * we want to use 64 bit storage.  Other network stats
 * are stored in dev->stats of the ppp strucute.
 */
struct ppp_link_stats {
	u64 rx_packets;
	u64 tx_packets;
	u64 rx_bytes;
	u64 tx_bytes;
};

L
Linus Torvalds 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123
/*
 * Data structure describing one ppp unit.
 * A ppp unit corresponds to a ppp network interface device
 * and represents a multilink bundle.
 * It can have 0 or more ppp channels connected to it.
 */
struct ppp {
	struct ppp_file	file;		/* stuff for read/write/poll 0 */
	struct file	*owner;		/* file that owns this unit 48 */
	struct list_head channels;	/* list of attached channels 4c */
	int		n_channels;	/* how many channels are attached 54 */
	spinlock_t	rlock;		/* lock for receive side 58 */
	spinlock_t	wlock;		/* lock for transmit side 5c */
G
Guillaume Nault 已提交
124
	int __percpu	*xmit_recursion; /* xmit recursion detect */
L
Linus Torvalds 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	int		mru;		/* max receive unit 60 */
	unsigned int	flags;		/* control bits 64 */
	unsigned int	xstate;		/* transmit state bits 68 */
	unsigned int	rstate;		/* receive state bits 6c */
	int		debug;		/* debug flags 70 */
	struct slcompress *vj;		/* state for VJ header compression */
	enum NPmode	npmode[NUM_NP];	/* what to do with each net proto 78 */
	struct sk_buff	*xmit_pending;	/* a packet ready to go out 88 */
	struct compressor *xcomp;	/* transmit packet compressor 8c */
	void		*xc_state;	/* its internal state 90 */
	struct compressor *rcomp;	/* receive decompressor 94 */
	void		*rc_state;	/* its internal state 98 */
	unsigned long	last_xmit;	/* jiffies when last pkt sent 9c */
	unsigned long	last_recv;	/* jiffies when last pkt rcvd a0 */
	struct net_device *dev;		/* network interface device a4 */
140
	int		closing;	/* is device closing down? a8 */
L
Linus Torvalds 已提交
141 142 143 144 145 146 147 148 149
#ifdef CONFIG_PPP_MULTILINK
	int		nxchan;		/* next channel to send something on */
	u32		nxseq;		/* next sequence number to send */
	int		mrru;		/* MP: max reconst. receive unit */
	u32		nextseq;	/* MP: seq no of next packet */
	u32		minseq;		/* MP: min of most recent seqnos */
	struct sk_buff_head mrq;	/* MP: receive reconstruction queue */
#endif /* CONFIG_PPP_MULTILINK */
#ifdef CONFIG_PPP_FILTER
150 151
	struct bpf_prog *pass_filter;	/* filter for packets to pass */
	struct bpf_prog *active_filter; /* filter for pkts to reset idle */
L
Linus Torvalds 已提交
152
#endif /* CONFIG_PPP_FILTER */
153
	struct net	*ppp_net;	/* the net we belong to */
K
Kevin Groeneveld 已提交
154
	struct ppp_link_stats stats64;	/* 64 bit network stats */
L
Linus Torvalds 已提交
155 156 157 158
};

/*
 * Bits in flags: SC_NO_TCP_CCID, SC_CCP_OPEN, SC_CCP_UP, SC_LOOP_TRAFFIC,
159 160
 * SC_MULTILINK, SC_MP_SHORTSEQ, SC_MP_XSHORTSEQ, SC_COMP_TCP, SC_REJ_COMP_TCP,
 * SC_MUST_COMP
L
Linus Torvalds 已提交
161 162 163 164 165
 * Bits in rstate: SC_DECOMP_RUN, SC_DC_ERROR, SC_DC_FERROR.
 * Bits in xstate: SC_COMP_RUN
 */
#define SC_FLAG_BITS	(SC_NO_TCP_CCID|SC_CCP_OPEN|SC_CCP_UP|SC_LOOP_TRAFFIC \
			 |SC_MULTILINK|SC_MP_SHORTSEQ|SC_MP_XSHORTSEQ \
166
			 |SC_COMP_TCP|SC_REJ_COMP_TCP|SC_MUST_COMP)
L
Linus Torvalds 已提交
167 168 169 170 171 172 173 174 175 176 177 178

/*
 * Private data structure for each channel.
 * This includes the data structure used for multilink.
 */
struct channel {
	struct ppp_file	file;		/* stuff for read/write/poll */
	struct list_head list;		/* link in all/new_channels list */
	struct ppp_channel *chan;	/* public channel data structure */
	struct rw_semaphore chan_sem;	/* protects `chan' during chan ioctl */
	spinlock_t	downl;		/* protects `chan', file.xq dequeue */
	struct ppp	*ppp;		/* ppp unit we're connected to */
179
	struct net	*chan_net;	/* the net channel belongs to */
L
Linus Torvalds 已提交
180 181 182 183 184 185
	struct list_head clist;		/* link in list of channels per unit */
	rwlock_t	upl;		/* protects `ppp' */
#ifdef CONFIG_PPP_MULTILINK
	u8		avail;		/* flag used in multilink stuff */
	u8		had_frag;	/* >= 1 fragments have been sent */
	u32		lastseq;	/* MP: last sequence # received */
186
	int		speed;		/* speed of the corresponding ppp channel*/
L
Linus Torvalds 已提交
187 188 189
#endif /* CONFIG_PPP_MULTILINK */
};

190 191 192
struct ppp_config {
	struct file *file;
	s32 unit;
193
	bool ifname_is_set;
194 195
};

L
Linus Torvalds 已提交
196 197 198 199 200 201 202 203 204
/*
 * SMP locking issues:
 * Both the ppp.rlock and ppp.wlock locks protect the ppp.channels
 * list and the ppp.n_channels field, you need to take both locks
 * before you modify them.
 * The lock ordering is: channel.upl -> ppp.wlock -> ppp.rlock ->
 * channel.downl.
 */

205
static DEFINE_MUTEX(ppp_mutex);
L
Linus Torvalds 已提交
206 207 208
static atomic_t ppp_unit_count = ATOMIC_INIT(0);
static atomic_t channel_count = ATOMIC_INIT(0);

209
/* per-net private data for this module */
210
static unsigned int ppp_net_id __read_mostly;
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
struct ppp_net {
	/* units to ppp mapping */
	struct idr units_idr;

	/*
	 * all_ppp_mutex protects the units_idr mapping.
	 * It also ensures that finding a ppp unit in the units_idr
	 * map and updating its file.refcnt field is atomic.
	 */
	struct mutex all_ppp_mutex;

	/* channels */
	struct list_head all_channels;
	struct list_head new_channels;
	int last_channel_index;

	/*
	 * all_channels_lock protects all_channels and
	 * last_channel_index, and the atomicity of find
	 * a channel and updating its file.refcnt field.
	 */
	spinlock_t all_channels_lock;
};

L
Linus Torvalds 已提交
235
/* Get the PPP protocol number from a skb */
236
#define PPP_PROTO(skb)	get_unaligned_be16((skb)->data)
L
Linus Torvalds 已提交
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257

/* We limit the length of ppp->file.rq to this (arbitrary) value */
#define PPP_MAX_RQLEN	32

/*
 * Maximum number of multilink fragments queued up.
 * This has to be large enough to cope with the maximum latency of
 * the slowest channel relative to the others.  Strictly it should
 * depend on the number of channels and their characteristics.
 */
#define PPP_MP_MAX_QLEN	128

/* Multilink header bits. */
#define B	0x80		/* this fragment begins a packet */
#define E	0x40		/* this fragment ends a packet */

/* Compare multilink sequence numbers (assumed to be 32 bits wide) */
#define seq_before(a, b)	((s32)((a) - (b)) < 0)
#define seq_after(a, b)		((s32)((a) - (b)) > 0)

/* Prototypes. */
258 259
static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
			struct file *file, unsigned int cmd, unsigned long arg);
260
static void ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb);
L
Linus Torvalds 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
static void ppp_send_frame(struct ppp *ppp, struct sk_buff *skb);
static void ppp_push(struct ppp *ppp);
static void ppp_channel_push(struct channel *pch);
static void ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb,
			      struct channel *pch);
static void ppp_receive_error(struct ppp *ppp);
static void ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb);
static struct sk_buff *ppp_decompress_frame(struct ppp *ppp,
					    struct sk_buff *skb);
#ifdef CONFIG_PPP_MULTILINK
static void ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb,
				struct channel *pch);
static void ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb);
static struct sk_buff *ppp_mp_reconstruct(struct ppp *ppp);
static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb);
#endif /* CONFIG_PPP_MULTILINK */
static int ppp_set_compress(struct ppp *ppp, unsigned long arg);
static void ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound);
static void ppp_ccp_closed(struct ppp *ppp);
static struct compressor *find_compressor(int type);
static void ppp_get_stats(struct ppp *ppp, struct ppp_stats *st);
282
static int ppp_create_interface(struct net *net, struct file *file, int *unit);
L
Linus Torvalds 已提交
283 284
static void init_ppp_file(struct ppp_file *pf, int kind);
static void ppp_destroy_interface(struct ppp *ppp);
285 286
static struct ppp *ppp_find_unit(struct ppp_net *pn, int unit);
static struct channel *ppp_find_channel(struct ppp_net *pn, int unit);
L
Linus Torvalds 已提交
287 288 289
static int ppp_connect_channel(struct channel *pch, int unit);
static int ppp_disconnect_channel(struct channel *pch);
static void ppp_destroy_channel(struct channel *pch);
290
static int unit_get(struct idr *p, void *ptr);
291
static int unit_set(struct idr *p, void *ptr, int n);
292 293
static void unit_put(struct idr *p, int n);
static void *unit_find(struct idr *p, int n);
294
static void ppp_setup(struct net_device *dev);
L
Linus Torvalds 已提交
295

G
Guillaume Nault 已提交
296 297
static const struct net_device_ops ppp_netdev_ops;

298
static struct class *ppp_class;
L
Linus Torvalds 已提交
299

300 301 302 303 304 305 306 307
/* per net-namespace data */
static inline struct ppp_net *ppp_pernet(struct net *net)
{
	BUG_ON(!net);

	return net_generic(net, ppp_net_id);
}

L
Linus Torvalds 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
/* Translates a PPP protocol number to a NP index (NP == network protocol) */
static inline int proto_to_npindex(int proto)
{
	switch (proto) {
	case PPP_IP:
		return NP_IP;
	case PPP_IPV6:
		return NP_IPV6;
	case PPP_IPX:
		return NP_IPX;
	case PPP_AT:
		return NP_AT;
	case PPP_MPLS_UC:
		return NP_MPLS_UC;
	case PPP_MPLS_MC:
		return NP_MPLS_MC;
	}
	return -EINVAL;
}

/* Translates an NP index into a PPP protocol number */
static const int npindex_to_proto[NUM_NP] = {
	PPP_IP,
	PPP_IPV6,
	PPP_IPX,
	PPP_AT,
	PPP_MPLS_UC,
	PPP_MPLS_MC,
};
337

L
Linus Torvalds 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
/* Translates an ethertype into an NP index */
static inline int ethertype_to_npindex(int ethertype)
{
	switch (ethertype) {
	case ETH_P_IP:
		return NP_IP;
	case ETH_P_IPV6:
		return NP_IPV6;
	case ETH_P_IPX:
		return NP_IPX;
	case ETH_P_PPPTALK:
	case ETH_P_ATALK:
		return NP_AT;
	case ETH_P_MPLS_UC:
		return NP_MPLS_UC;
	case ETH_P_MPLS_MC:
		return NP_MPLS_MC;
	}
	return -1;
}

/* Translates an NP index into an ethertype */
static const int npindex_to_ethertype[NUM_NP] = {
	ETH_P_IP,
	ETH_P_IPV6,
	ETH_P_IPX,
	ETH_P_PPPTALK,
	ETH_P_MPLS_UC,
	ETH_P_MPLS_MC,
};

/*
 * Locking shorthand.
 */
#define ppp_xmit_lock(ppp)	spin_lock_bh(&(ppp)->wlock)
#define ppp_xmit_unlock(ppp)	spin_unlock_bh(&(ppp)->wlock)
#define ppp_recv_lock(ppp)	spin_lock_bh(&(ppp)->rlock)
#define ppp_recv_unlock(ppp)	spin_unlock_bh(&(ppp)->rlock)
#define ppp_lock(ppp)		do { ppp_xmit_lock(ppp); \
				     ppp_recv_lock(ppp); } while (0)
#define ppp_unlock(ppp)		do { ppp_recv_unlock(ppp); \
				     ppp_xmit_unlock(ppp); } while (0)

/*
 * /dev/ppp device routines.
 * The /dev/ppp device is used by pppd to control the ppp unit.
 * It supports the read, write, ioctl and poll functions.
 * Open instances of /dev/ppp can be in one of three states:
 * unattached, attached to a ppp unit, or attached to a ppp channel.
 */
static int ppp_open(struct inode *inode, struct file *file)
{
	/*
	 * This could (should?) be enforced by the permissions on /dev/ppp.
	 */
M
Matteo Croce 已提交
393
	if (!ns_capable(file->f_cred->user_ns, CAP_NET_ADMIN))
L
Linus Torvalds 已提交
394 395 396 397
		return -EPERM;
	return 0;
}

A
Alan Cox 已提交
398
static int ppp_release(struct inode *unused, struct file *file)
L
Linus Torvalds 已提交
399 400 401 402
{
	struct ppp_file *pf = file->private_data;
	struct ppp *ppp;

J
Joe Perches 已提交
403
	if (pf) {
L
Linus Torvalds 已提交
404 405 406
		file->private_data = NULL;
		if (pf->kind == INTERFACE) {
			ppp = PF_TO_PPP(pf);
407
			rtnl_lock();
L
Linus Torvalds 已提交
408
			if (file == ppp->owner)
409 410
				unregister_netdevice(ppp->dev);
			rtnl_unlock();
L
Linus Torvalds 已提交
411
		}
412
		if (refcount_dec_and_test(&pf->refcnt)) {
L
Linus Torvalds 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
			switch (pf->kind) {
			case INTERFACE:
				ppp_destroy_interface(PF_TO_PPP(pf));
				break;
			case CHANNEL:
				ppp_destroy_channel(PF_TO_CHANNEL(pf));
				break;
			}
		}
	}
	return 0;
}

static ssize_t ppp_read(struct file *file, char __user *buf,
			size_t count, loff_t *ppos)
{
	struct ppp_file *pf = file->private_data;
	DECLARE_WAITQUEUE(wait, current);
	ssize_t ret;
	struct sk_buff *skb = NULL;
433
	struct iovec iov;
434
	struct iov_iter to;
L
Linus Torvalds 已提交
435 436 437

	ret = count;

J
Joe Perches 已提交
438
	if (!pf)
L
Linus Torvalds 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
		return -ENXIO;
	add_wait_queue(&pf->rwait, &wait);
	for (;;) {
		set_current_state(TASK_INTERRUPTIBLE);
		skb = skb_dequeue(&pf->rq);
		if (skb)
			break;
		ret = 0;
		if (pf->dead)
			break;
		if (pf->kind == INTERFACE) {
			/*
			 * Return 0 (EOF) on an interface that has no
			 * channels connected, unless it is looping
			 * network traffic (demand mode).
			 */
			struct ppp *ppp = PF_TO_PPP(pf);
456 457

			ppp_recv_lock(ppp);
458
			if (ppp->n_channels == 0 &&
459 460
			    (ppp->flags & SC_LOOP_TRAFFIC) == 0) {
				ppp_recv_unlock(ppp);
L
Linus Torvalds 已提交
461
				break;
462 463
			}
			ppp_recv_unlock(ppp);
L
Linus Torvalds 已提交
464 465 466 467 468 469 470 471 472 473 474 475
		}
		ret = -EAGAIN;
		if (file->f_flags & O_NONBLOCK)
			break;
		ret = -ERESTARTSYS;
		if (signal_pending(current))
			break;
		schedule();
	}
	set_current_state(TASK_RUNNING);
	remove_wait_queue(&pf->rwait, &wait);

J
Joe Perches 已提交
476
	if (!skb)
L
Linus Torvalds 已提交
477 478 479 480 481 482
		goto out;

	ret = -EOVERFLOW;
	if (skb->len > count)
		goto outf;
	ret = -EFAULT;
483 484
	iov.iov_base = buf;
	iov.iov_len = count;
485 486
	iov_iter_init(&to, READ, &iov, 1, count);
	if (skb_copy_datagram_iter(skb, 0, &to, skb->len))
L
Linus Torvalds 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
		goto outf;
	ret = skb->len;

 outf:
	kfree_skb(skb);
 out:
	return ret;
}

static ssize_t ppp_write(struct file *file, const char __user *buf,
			 size_t count, loff_t *ppos)
{
	struct ppp_file *pf = file->private_data;
	struct sk_buff *skb;
	ssize_t ret;

J
Joe Perches 已提交
503
	if (!pf)
L
Linus Torvalds 已提交
504 505 506
		return -ENXIO;
	ret = -ENOMEM;
	skb = alloc_skb(count + pf->hdrlen, GFP_KERNEL);
J
Joe Perches 已提交
507
	if (!skb)
L
Linus Torvalds 已提交
508 509 510 511 512 513 514 515 516 517
		goto out;
	skb_reserve(skb, pf->hdrlen);
	ret = -EFAULT;
	if (copy_from_user(skb_put(skb, count), buf, count)) {
		kfree_skb(skb);
		goto out;
	}

	switch (pf->kind) {
	case INTERFACE:
518
		ppp_xmit_process(PF_TO_PPP(pf), skb);
L
Linus Torvalds 已提交
519 520
		break;
	case CHANNEL:
521
		skb_queue_tail(&pf->xq, skb);
L
Linus Torvalds 已提交
522 523 524 525 526 527 528 529 530 531 532
		ppp_channel_push(PF_TO_CHANNEL(pf));
		break;
	}

	ret = count;

 out:
	return ret;
}

/* No kernel lock - fine */
533
static __poll_t ppp_poll(struct file *file, poll_table *wait)
L
Linus Torvalds 已提交
534 535
{
	struct ppp_file *pf = file->private_data;
536
	__poll_t mask;
L
Linus Torvalds 已提交
537

J
Joe Perches 已提交
538
	if (!pf)
L
Linus Torvalds 已提交
539 540
		return 0;
	poll_wait(file, &pf->rwait, wait);
541
	mask = EPOLLOUT | EPOLLWRNORM;
J
Joe Perches 已提交
542
	if (skb_peek(&pf->rq))
543
		mask |= EPOLLIN | EPOLLRDNORM;
L
Linus Torvalds 已提交
544
	if (pf->dead)
545
		mask |= EPOLLHUP;
L
Linus Torvalds 已提交
546 547 548
	else if (pf->kind == INTERFACE) {
		/* see comment in ppp_read */
		struct ppp *ppp = PF_TO_PPP(pf);
549 550

		ppp_recv_lock(ppp);
551 552
		if (ppp->n_channels == 0 &&
		    (ppp->flags & SC_LOOP_TRAFFIC) == 0)
553
			mask |= EPOLLIN | EPOLLRDNORM;
554
		ppp_recv_unlock(ppp);
L
Linus Torvalds 已提交
555 556 557 558 559 560 561 562 563 564
	}

	return mask;
}

#ifdef CONFIG_PPP_FILTER
static int get_filter(void __user *arg, struct sock_filter **p)
{
	struct sock_fprog uprog;
	struct sock_filter *code = NULL;
565
	int len;
L
Linus Torvalds 已提交
566 567 568 569 570 571 572 573 574 575

	if (copy_from_user(&uprog, arg, sizeof(uprog)))
		return -EFAULT;

	if (!uprog.len) {
		*p = NULL;
		return 0;
	}

	len = uprog.len * sizeof(struct sock_filter);
J
Julia Lawall 已提交
576 577 578
	code = memdup_user(uprog.filter, len);
	if (IS_ERR(code))
		return PTR_ERR(code);
L
Linus Torvalds 已提交
579 580 581 582 583 584

	*p = code;
	return uprog.len;
}
#endif /* CONFIG_PPP_FILTER */

A
Alan Cox 已提交
585
static long ppp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
586
{
587
	struct ppp_file *pf;
L
Linus Torvalds 已提交
588 589 590 591 592 593 594 595 596
	struct ppp *ppp;
	int err = -EFAULT, val, val2, i;
	struct ppp_idle idle;
	struct npioctl npi;
	int unit, cflags;
	struct slcompress *vj;
	void __user *argp = (void __user *)arg;
	int __user *p = argp;

597 598 599 600 601 602 603 604
	mutex_lock(&ppp_mutex);

	pf = file->private_data;
	if (!pf) {
		err = ppp_unattached_ioctl(current->nsproxy->net_ns,
					   pf, file, cmd, arg);
		goto out;
	}
L
Linus Torvalds 已提交
605 606 607

	if (cmd == PPPIOCDETACH) {
		/*
608 609 610
		 * PPPIOCDETACH is no longer supported as it was heavily broken,
		 * and is only known to have been used by pppd older than
		 * ppp-2.4.2 (released November 2003).
L
Linus Torvalds 已提交
611
		 */
612 613
		pr_warn_once("%s (%d) used obsolete PPPIOCDETACH ioctl\n",
			     current->comm, current->pid);
L
Linus Torvalds 已提交
614
		err = -EINVAL;
615
		goto out;
L
Linus Torvalds 已提交
616 617 618
	}

	if (pf->kind == CHANNEL) {
A
Alan Cox 已提交
619
		struct channel *pch;
L
Linus Torvalds 已提交
620 621
		struct ppp_channel *chan;

A
Alan Cox 已提交
622 623
		pch = PF_TO_CHANNEL(pf);

L
Linus Torvalds 已提交
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
		switch (cmd) {
		case PPPIOCCONNECT:
			if (get_user(unit, p))
				break;
			err = ppp_connect_channel(pch, unit);
			break;

		case PPPIOCDISCONN:
			err = ppp_disconnect_channel(pch);
			break;

		default:
			down_read(&pch->chan_sem);
			chan = pch->chan;
			err = -ENOTTY;
			if (chan && chan->ops->ioctl)
				err = chan->ops->ioctl(chan, cmd, arg);
			up_read(&pch->chan_sem);
		}
643
		goto out;
L
Linus Torvalds 已提交
644 645 646 647
	}

	if (pf->kind != INTERFACE) {
		/* can't happen */
648
		pr_err("PPP: not interface or channel??\n");
649 650
		err = -EINVAL;
		goto out;
L
Linus Torvalds 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
	}

	ppp = PF_TO_PPP(pf);
	switch (cmd) {
	case PPPIOCSMRU:
		if (get_user(val, p))
			break;
		ppp->mru = val;
		err = 0;
		break;

	case PPPIOCSFLAGS:
		if (get_user(val, p))
			break;
		ppp_lock(ppp);
		cflags = ppp->flags & ~val;
667
#ifdef CONFIG_PPP_MULTILINK
668 669
		if (!(ppp->flags & SC_MULTILINK) && (val & SC_MULTILINK))
			ppp->nextseq = 0;
670
#endif
L
Linus Torvalds 已提交
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
		ppp->flags = val & SC_FLAG_BITS;
		ppp_unlock(ppp);
		if (cflags & SC_CCP_OPEN)
			ppp_ccp_closed(ppp);
		err = 0;
		break;

	case PPPIOCGFLAGS:
		val = ppp->flags | ppp->xstate | ppp->rstate;
		if (put_user(val, p))
			break;
		err = 0;
		break;

	case PPPIOCSCOMPRESS:
		err = ppp_set_compress(ppp, arg);
		break;

	case PPPIOCGUNIT:
		if (put_user(ppp->file.index, p))
			break;
		err = 0;
		break;

	case PPPIOCSDEBUG:
		if (get_user(val, p))
			break;
		ppp->debug = val;
		err = 0;
		break;

	case PPPIOCGDEBUG:
		if (put_user(ppp->debug, p))
			break;
		err = 0;
		break;

	case PPPIOCGIDLE:
		idle.xmit_idle = (jiffies - ppp->last_xmit) / HZ;
		idle.recv_idle = (jiffies - ppp->last_recv) / HZ;
		if (copy_to_user(argp, &idle, sizeof(idle)))
			break;
		err = 0;
		break;

	case PPPIOCSMAXCID:
		if (get_user(val, p))
			break;
		val2 = 15;
		if ((val >> 16) != 0) {
			val2 = val >> 16;
			val &= 0xffff;
		}
		vj = slhc_init(val2+1, val+1);
725 726
		if (IS_ERR(vj)) {
			err = PTR_ERR(vj);
L
Linus Torvalds 已提交
727 728 729
			break;
		}
		ppp_lock(ppp);
J
Joe Perches 已提交
730
		if (ppp->vj)
L
Linus Torvalds 已提交
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
			slhc_free(ppp->vj);
		ppp->vj = vj;
		ppp_unlock(ppp);
		err = 0;
		break;

	case PPPIOCGNPMODE:
	case PPPIOCSNPMODE:
		if (copy_from_user(&npi, argp, sizeof(npi)))
			break;
		err = proto_to_npindex(npi.protocol);
		if (err < 0)
			break;
		i = err;
		if (cmd == PPPIOCGNPMODE) {
			err = -EFAULT;
			npi.mode = ppp->npmode[i];
			if (copy_to_user(argp, &npi, sizeof(npi)))
				break;
		} else {
			ppp->npmode[i] = npi.mode;
			/* we may be able to transmit more packets now (??) */
			netif_wake_queue(ppp->dev);
		}
		err = 0;
		break;

#ifdef CONFIG_PPP_FILTER
	case PPPIOCSPASS:
	{
		struct sock_filter *code;
762

L
Linus Torvalds 已提交
763 764
		err = get_filter(argp, &code);
		if (err >= 0) {
765
			struct bpf_prog *pass_filter = NULL;
766
			struct sock_fprog_kern fprog = {
767 768 769 770
				.len = err,
				.filter = code,
			};

771 772 773 774 775 776 777 778 779
			err = 0;
			if (fprog.filter)
				err = bpf_prog_create(&pass_filter, &fprog);
			if (!err) {
				ppp_lock(ppp);
				if (ppp->pass_filter)
					bpf_prog_destroy(ppp->pass_filter);
				ppp->pass_filter = pass_filter;
				ppp_unlock(ppp);
780
			}
781
			kfree(code);
L
Linus Torvalds 已提交
782 783 784 785 786 787
		}
		break;
	}
	case PPPIOCSACTIVE:
	{
		struct sock_filter *code;
788

L
Linus Torvalds 已提交
789 790
		err = get_filter(argp, &code);
		if (err >= 0) {
791
			struct bpf_prog *active_filter = NULL;
792
			struct sock_fprog_kern fprog = {
793 794 795 796
				.len = err,
				.filter = code,
			};

797 798 799 800 801 802 803 804 805
			err = 0;
			if (fprog.filter)
				err = bpf_prog_create(&active_filter, &fprog);
			if (!err) {
				ppp_lock(ppp);
				if (ppp->active_filter)
					bpf_prog_destroy(ppp->active_filter);
				ppp->active_filter = active_filter;
				ppp_unlock(ppp);
806
			}
807
			kfree(code);
L
Linus Torvalds 已提交
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
		}
		break;
	}
#endif /* CONFIG_PPP_FILTER */

#ifdef CONFIG_PPP_MULTILINK
	case PPPIOCSMRRU:
		if (get_user(val, p))
			break;
		ppp_recv_lock(ppp);
		ppp->mrru = val;
		ppp_recv_unlock(ppp);
		err = 0;
		break;
#endif /* CONFIG_PPP_MULTILINK */

	default:
		err = -ENOTTY;
	}
827 828

out:
829
	mutex_unlock(&ppp_mutex);
830

L
Linus Torvalds 已提交
831 832 833
	return err;
}

834 835
static int ppp_unattached_ioctl(struct net *net, struct ppp_file *pf,
			struct file *file, unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
836 837 838 839
{
	int unit, err = -EFAULT;
	struct ppp *ppp;
	struct channel *chan;
840
	struct ppp_net *pn;
L
Linus Torvalds 已提交
841 842 843 844 845 846 847
	int __user *p = (int __user *)arg;

	switch (cmd) {
	case PPPIOCNEWUNIT:
		/* Create a new ppp unit */
		if (get_user(unit, p))
			break;
848 849
		err = ppp_create_interface(net, file, &unit);
		if (err < 0)
L
Linus Torvalds 已提交
850
			break;
851

L
Linus Torvalds 已提交
852
		err = -EFAULT;
853
		if (put_user(unit, p))
L
Linus Torvalds 已提交
854 855 856 857 858 859 860 861 862
			break;
		err = 0;
		break;

	case PPPIOCATTACH:
		/* Attach to an existing ppp unit */
		if (get_user(unit, p))
			break;
		err = -ENXIO;
863 864 865
		pn = ppp_pernet(net);
		mutex_lock(&pn->all_ppp_mutex);
		ppp = ppp_find_unit(pn, unit);
J
Joe Perches 已提交
866
		if (ppp) {
867
			refcount_inc(&ppp->file.refcnt);
L
Linus Torvalds 已提交
868 869 870
			file->private_data = &ppp->file;
			err = 0;
		}
871
		mutex_unlock(&pn->all_ppp_mutex);
L
Linus Torvalds 已提交
872 873 874 875 876 877
		break;

	case PPPIOCATTCHAN:
		if (get_user(unit, p))
			break;
		err = -ENXIO;
878 879 880
		pn = ppp_pernet(net);
		spin_lock_bh(&pn->all_channels_lock);
		chan = ppp_find_channel(pn, unit);
J
Joe Perches 已提交
881
		if (chan) {
882
			refcount_inc(&chan->file.refcnt);
L
Linus Torvalds 已提交
883 884 885
			file->private_data = &chan->file;
			err = 0;
		}
886
		spin_unlock_bh(&pn->all_channels_lock);
L
Linus Torvalds 已提交
887 888 889 890 891
		break;

	default:
		err = -ENOTTY;
	}
892

L
Linus Torvalds 已提交
893 894 895
	return err;
}

896
static const struct file_operations ppp_device_fops = {
L
Linus Torvalds 已提交
897 898 899 900
	.owner		= THIS_MODULE,
	.read		= ppp_read,
	.write		= ppp_write,
	.poll		= ppp_poll,
A
Alan Cox 已提交
901
	.unlocked_ioctl	= ppp_ioctl,
L
Linus Torvalds 已提交
902
	.open		= ppp_open,
903 904
	.release	= ppp_release,
	.llseek		= noop_llseek,
L
Linus Torvalds 已提交
905 906
};

907 908
static __net_init int ppp_init_net(struct net *net)
{
909
	struct ppp_net *pn = net_generic(net, ppp_net_id);
910 911 912 913 914 915 916 917 918 919 920 921 922 923

	idr_init(&pn->units_idr);
	mutex_init(&pn->all_ppp_mutex);

	INIT_LIST_HEAD(&pn->all_channels);
	INIT_LIST_HEAD(&pn->new_channels);

	spin_lock_init(&pn->all_channels_lock);

	return 0;
}

static __net_exit void ppp_exit_net(struct net *net)
{
924
	struct ppp_net *pn = net_generic(net, ppp_net_id);
G
Guillaume Nault 已提交
925 926
	struct net_device *dev;
	struct net_device *aux;
927 928 929 930 931
	struct ppp *ppp;
	LIST_HEAD(list);
	int id;

	rtnl_lock();
G
Guillaume Nault 已提交
932 933 934 935 936
	for_each_netdev_safe(net, dev, aux) {
		if (dev->netdev_ops == &ppp_netdev_ops)
			unregister_netdevice_queue(dev, &list);
	}

937
	idr_for_each_entry(&pn->units_idr, ppp, id)
G
Guillaume Nault 已提交
938 939 940
		/* Skip devices already unregistered by previous loop */
		if (!net_eq(dev_net(ppp->dev), net))
			unregister_netdevice_queue(ppp->dev, &list);
941 942 943

	unregister_netdevice_many(&list);
	rtnl_unlock();
944

G
Gao Feng 已提交
945
	mutex_destroy(&pn->all_ppp_mutex);
946
	idr_destroy(&pn->units_idr);
V
Vasily Averin 已提交
947 948
	WARN_ON_ONCE(!list_empty(&pn->all_channels));
	WARN_ON_ONCE(!list_empty(&pn->new_channels));
949 950
}

A
Alexey Dobriyan 已提交
951
static struct pernet_operations ppp_net_ops = {
952 953
	.init = ppp_init_net,
	.exit = ppp_exit_net,
954 955
	.id   = &ppp_net_id,
	.size = sizeof(struct ppp_net),
956 957
};

958
static int ppp_unit_register(struct ppp *ppp, int unit, bool ifname_is_set)
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
{
	struct ppp_net *pn = ppp_pernet(ppp->ppp_net);
	int ret;

	mutex_lock(&pn->all_ppp_mutex);

	if (unit < 0) {
		ret = unit_get(&pn->units_idr, ppp);
		if (ret < 0)
			goto err;
	} else {
		/* Caller asked for a specific unit number. Fail with -EEXIST
		 * if unavailable. For backward compatibility, return -EEXIST
		 * too if idr allocation fails; this makes pppd retry without
		 * requesting a specific unit number.
		 */
		if (unit_find(&pn->units_idr, unit)) {
			ret = -EEXIST;
			goto err;
		}
		ret = unit_set(&pn->units_idr, ppp, unit);
		if (ret < 0) {
			/* Rewrite error for backward compatibility */
			ret = -EEXIST;
			goto err;
		}
	}
	ppp->file.index = ret;

988 989
	if (!ifname_is_set)
		snprintf(ppp->dev->name, IFNAMSIZ, "ppp%i", ppp->file.index);
990

991 992
	mutex_unlock(&pn->all_ppp_mutex);

993 994 995 996 997 998 999 1000 1001
	ret = register_netdevice(ppp->dev);
	if (ret < 0)
		goto err_unit;

	atomic_inc(&ppp_unit_count);

	return 0;

err_unit:
1002
	mutex_lock(&pn->all_ppp_mutex);
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
	unit_put(&pn->units_idr, ppp->file.index);
err:
	mutex_unlock(&pn->all_ppp_mutex);

	return ret;
}

static int ppp_dev_configure(struct net *src_net, struct net_device *dev,
			     const struct ppp_config *conf)
{
	struct ppp *ppp = netdev_priv(dev);
	int indx;
	int err;
1016
	int cpu;
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030

	ppp->dev = dev;
	ppp->ppp_net = src_net;
	ppp->mru = PPP_MRU;
	ppp->owner = conf->file;

	init_ppp_file(&ppp->file, INTERFACE);
	ppp->file.hdrlen = PPP_HDRLEN - 2; /* don't count proto bytes */

	for (indx = 0; indx < NUM_NP; ++indx)
		ppp->npmode[indx] = NPMODE_PASS;
	INIT_LIST_HEAD(&ppp->channels);
	spin_lock_init(&ppp->rlock);
	spin_lock_init(&ppp->wlock);
1031 1032 1033 1034 1035 1036 1037 1038 1039

	ppp->xmit_recursion = alloc_percpu(int);
	if (!ppp->xmit_recursion) {
		err = -ENOMEM;
		goto err1;
	}
	for_each_possible_cpu(cpu)
		(*per_cpu_ptr(ppp->xmit_recursion, cpu)) = 0;

1040 1041 1042 1043 1044 1045 1046 1047 1048
#ifdef CONFIG_PPP_MULTILINK
	ppp->minseq = -1;
	skb_queue_head_init(&ppp->mrq);
#endif /* CONFIG_PPP_MULTILINK */
#ifdef CONFIG_PPP_FILTER
	ppp->pass_filter = NULL;
	ppp->active_filter = NULL;
#endif /* CONFIG_PPP_FILTER */

1049
	err = ppp_unit_register(ppp, conf->unit, conf->ifname_is_set);
1050
	if (err < 0)
1051
		goto err2;
1052 1053 1054 1055

	conf->file->private_data = &ppp->file;

	return 0;
1056 1057 1058 1059
err2:
	free_percpu(ppp->xmit_recursion);
err1:
	return err;
1060 1061
}

1062 1063 1064 1065
static const struct nla_policy ppp_nl_policy[IFLA_PPP_MAX + 1] = {
	[IFLA_PPP_DEV_FD]	= { .type = NLA_S32 },
};

1066 1067
static int ppp_nl_validate(struct nlattr *tb[], struct nlattr *data[],
			   struct netlink_ext_ack *extack)
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
{
	if (!data)
		return -EINVAL;

	if (!data[IFLA_PPP_DEV_FD])
		return -EINVAL;
	if (nla_get_s32(data[IFLA_PPP_DEV_FD]) < 0)
		return -EBADF;

	return 0;
}

static int ppp_nl_newlink(struct net *src_net, struct net_device *dev,
1081 1082
			  struct nlattr *tb[], struct nlattr *data[],
			  struct netlink_ext_ack *extack)
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
{
	struct ppp_config conf = {
		.unit = -1,
		.ifname_is_set = true,
	};
	struct file *file;
	int err;

	file = fget(nla_get_s32(data[IFLA_PPP_DEV_FD]));
	if (!file)
		return -EBADF;

	/* rtnl_lock is already held here, but ppp_create_interface() locks
	 * ppp_mutex before holding rtnl_lock. Using mutex_trylock() avoids
	 * possible deadlock due to lock order inversion, at the cost of
	 * pushing the problem back to userspace.
	 */
	if (!mutex_trylock(&ppp_mutex)) {
		err = -EBUSY;
		goto out;
	}

	if (file->f_op != &ppp_device_fops || file->private_data) {
		err = -EBADF;
		goto out_unlock;
	}

	conf.file = file;
1111 1112 1113 1114 1115 1116 1117 1118 1119

	/* Don't use device name generated by the rtnetlink layer when ifname
	 * isn't specified. Let ppp_dev_configure() set the device name using
	 * the PPP unit identifer as suffix (i.e. ppp<unit_id>). This allows
	 * userspace to infer the device name using to the PPPIOCGUNIT ioctl.
	 */
	if (!tb[IFLA_IFNAME])
		conf.ifname_is_set = false;

1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
	err = ppp_dev_configure(src_net, dev, &conf);

out_unlock:
	mutex_unlock(&ppp_mutex);
out:
	fput(file);

	return err;
}

static void ppp_nl_dellink(struct net_device *dev, struct list_head *head)
{
	unregister_netdevice_queue(dev, head);
}

static size_t ppp_nl_get_size(const struct net_device *dev)
{
	return 0;
}

static int ppp_nl_fill_info(struct sk_buff *skb, const struct net_device *dev)
{
	return 0;
}

static struct net *ppp_nl_get_link_net(const struct net_device *dev)
{
	struct ppp *ppp = netdev_priv(dev);

	return ppp->ppp_net;
}

static struct rtnl_link_ops ppp_link_ops __read_mostly = {
	.kind		= "ppp",
	.maxtype	= IFLA_PPP_MAX,
	.policy		= ppp_nl_policy,
	.priv_size	= sizeof(struct ppp),
	.setup		= ppp_setup,
	.validate	= ppp_nl_validate,
	.newlink	= ppp_nl_newlink,
	.dellink	= ppp_nl_dellink,
	.get_size	= ppp_nl_get_size,
	.fill_info	= ppp_nl_fill_info,
	.get_link_net	= ppp_nl_get_link_net,
};

L
Linus Torvalds 已提交
1166 1167 1168 1169 1170 1171 1172 1173
#define PPP_MAJOR	108

/* Called at boot time if ppp is compiled into the kernel,
   or at module load time (from init_module) if compiled as a module. */
static int __init ppp_init(void)
{
	int err;

1174
	pr_info("PPP generic driver version " PPP_VERSION "\n");
1175

1176
	err = register_pernet_device(&ppp_net_ops);
1177
	if (err) {
1178
		pr_err("failed to register PPP pernet device (%d)\n", err);
1179
		goto out;
L
Linus Torvalds 已提交
1180 1181
	}

1182 1183
	err = register_chrdev(PPP_MAJOR, "ppp", &ppp_device_fops);
	if (err) {
1184
		pr_err("failed to register PPP device (%d)\n", err);
1185 1186 1187 1188 1189 1190 1191 1192 1193
		goto out_net;
	}

	ppp_class = class_create(THIS_MODULE, "ppp");
	if (IS_ERR(ppp_class)) {
		err = PTR_ERR(ppp_class);
		goto out_chrdev;
	}

1194 1195 1196 1197 1198 1199
	err = rtnl_link_register(&ppp_link_ops);
	if (err) {
		pr_err("failed to register rtnetlink PPP handler\n");
		goto out_class;
	}

1200 1201 1202 1203
	/* not a big deal if we fail here :-) */
	device_create(ppp_class, NULL, MKDEV(PPP_MAJOR, 0), NULL, "ppp");

	return 0;
L
Linus Torvalds 已提交
1204

1205 1206
out_class:
	class_destroy(ppp_class);
L
Linus Torvalds 已提交
1207 1208
out_chrdev:
	unregister_chrdev(PPP_MAJOR, "ppp");
1209
out_net:
1210
	unregister_pernet_device(&ppp_net_ops);
1211 1212
out:
	return err;
L
Linus Torvalds 已提交
1213 1214 1215 1216 1217
}

/*
 * Network interface unit routines.
 */
1218
static netdev_tx_t
L
Linus Torvalds 已提交
1219 1220
ppp_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
1221
	struct ppp *ppp = netdev_priv(dev);
L
Linus Torvalds 已提交
1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
	int npi, proto;
	unsigned char *pp;

	npi = ethertype_to_npindex(ntohs(skb->protocol));
	if (npi < 0)
		goto outf;

	/* Drop, accept or reject the packet */
	switch (ppp->npmode[npi]) {
	case NPMODE_PASS:
		break;
	case NPMODE_QUEUE:
		/* it would be nice to have a way to tell the network
		   system to queue this one up for later. */
		goto outf;
	case NPMODE_DROP:
	case NPMODE_ERROR:
		goto outf;
	}

	/* Put the 2-byte PPP protocol number on the front,
	   making sure there is room for the address and control fields. */
1244 1245 1246
	if (skb_cow_head(skb, PPP_HDRLEN))
		goto outf;

L
Linus Torvalds 已提交
1247 1248
	pp = skb_push(skb, 2);
	proto = npindex_to_proto[npi];
1249
	put_unaligned_be16(proto, pp);
L
Linus Torvalds 已提交
1250

G
Guillaume Nault 已提交
1251
	skb_scrub_packet(skb, !net_eq(ppp->ppp_net, dev_net(dev)));
1252 1253
	ppp_xmit_process(ppp, skb);

1254
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
1255 1256 1257

 outf:
	kfree_skb(skb);
1258
	++dev->stats.tx_dropped;
1259
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
1260 1261 1262 1263 1264
}

static int
ppp_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
{
1265
	struct ppp *ppp = netdev_priv(dev);
L
Linus Torvalds 已提交
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
	int err = -EFAULT;
	void __user *addr = (void __user *) ifr->ifr_ifru.ifru_data;
	struct ppp_stats stats;
	struct ppp_comp_stats cstats;
	char *vers;

	switch (cmd) {
	case SIOCGPPPSTATS:
		ppp_get_stats(ppp, &stats);
		if (copy_to_user(addr, &stats, sizeof(stats)))
			break;
		err = 0;
		break;

	case SIOCGPPPCSTATS:
		memset(&cstats, 0, sizeof(cstats));
J
Joe Perches 已提交
1282
		if (ppp->xc_state)
L
Linus Torvalds 已提交
1283
			ppp->xcomp->comp_stat(ppp->xc_state, &cstats.c);
J
Joe Perches 已提交
1284
		if (ppp->rc_state)
L
Linus Torvalds 已提交
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
			ppp->rcomp->decomp_stat(ppp->rc_state, &cstats.d);
		if (copy_to_user(addr, &cstats, sizeof(cstats)))
			break;
		err = 0;
		break;

	case SIOCGPPPVER:
		vers = PPP_VERSION;
		if (copy_to_user(addr, vers, strlen(vers) + 1))
			break;
		err = 0;
		break;

	default:
		err = -EINVAL;
	}

	return err;
}

1305
static void
K
Kevin Groeneveld 已提交
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
ppp_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats64)
{
	struct ppp *ppp = netdev_priv(dev);

	ppp_recv_lock(ppp);
	stats64->rx_packets = ppp->stats64.rx_packets;
	stats64->rx_bytes   = ppp->stats64.rx_bytes;
	ppp_recv_unlock(ppp);

	ppp_xmit_lock(ppp);
	stats64->tx_packets = ppp->stats64.tx_packets;
	stats64->tx_bytes   = ppp->stats64.tx_bytes;
	ppp_xmit_unlock(ppp);

	stats64->rx_errors        = dev->stats.rx_errors;
	stats64->tx_errors        = dev->stats.tx_errors;
	stats64->rx_dropped       = dev->stats.rx_dropped;
	stats64->tx_dropped       = dev->stats.tx_dropped;
	stats64->rx_length_errors = dev->stats.rx_length_errors;
}

1327 1328
static int ppp_dev_init(struct net_device *dev)
{
1329 1330
	struct ppp *ppp;

1331
	netdev_lockdep_set_classes(dev);
1332 1333 1334 1335 1336 1337

	ppp = netdev_priv(dev);
	/* Let the netdevice take a reference on the ppp file. This ensures
	 * that ppp_destroy_interface() won't run before the device gets
	 * unregistered.
	 */
1338
	refcount_inc(&ppp->file.refcnt);
1339

1340 1341 1342
	return 0;
}

1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361
static void ppp_dev_uninit(struct net_device *dev)
{
	struct ppp *ppp = netdev_priv(dev);
	struct ppp_net *pn = ppp_pernet(ppp->ppp_net);

	ppp_lock(ppp);
	ppp->closing = 1;
	ppp_unlock(ppp);

	mutex_lock(&pn->all_ppp_mutex);
	unit_put(&pn->units_idr, ppp->file.index);
	mutex_unlock(&pn->all_ppp_mutex);

	ppp->owner = NULL;

	ppp->file.dead = 1;
	wake_up_interruptible(&ppp->file.rwait);
}

1362 1363 1364 1365 1366
static void ppp_dev_priv_destructor(struct net_device *dev)
{
	struct ppp *ppp;

	ppp = netdev_priv(dev);
1367
	if (refcount_dec_and_test(&ppp->file.refcnt))
1368 1369 1370
		ppp_destroy_interface(ppp);
}

S
Stephen Hemminger 已提交
1371
static const struct net_device_ops ppp_netdev_ops = {
1372
	.ndo_init	 = ppp_dev_init,
1373
	.ndo_uninit      = ppp_dev_uninit,
K
Kevin Groeneveld 已提交
1374 1375 1376
	.ndo_start_xmit  = ppp_start_xmit,
	.ndo_do_ioctl    = ppp_net_ioctl,
	.ndo_get_stats64 = ppp_get_stats64,
S
Stephen Hemminger 已提交
1377 1378
};

G
Guillaume Nault 已提交
1379 1380 1381 1382
static struct device_type ppp_type = {
	.name = "ppp",
};

L
Linus Torvalds 已提交
1383 1384
static void ppp_setup(struct net_device *dev)
{
S
Stephen Hemminger 已提交
1385
	dev->netdev_ops = &ppp_netdev_ops;
G
Guillaume Nault 已提交
1386 1387
	SET_NETDEV_DEVTYPE(dev, &ppp_type);

1388 1389
	dev->features |= NETIF_F_LLTX;

L
Linus Torvalds 已提交
1390
	dev->hard_header_len = PPP_HDRLEN;
1391
	dev->mtu = PPP_MRU;
L
Linus Torvalds 已提交
1392 1393 1394 1395
	dev->addr_len = 0;
	dev->tx_queue_len = 3;
	dev->type = ARPHRD_PPP;
	dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
1396
	dev->priv_destructor = ppp_dev_priv_destructor;
1397
	netif_keep_dst(dev);
L
Linus Torvalds 已提交
1398 1399 1400 1401 1402 1403
}

/*
 * Transmit-side routines.
 */

1404
/* Called to do any work queued up on the transmit side that can now be done */
1405
static void __ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb)
L
Linus Torvalds 已提交
1406 1407
{
	ppp_xmit_lock(ppp);
1408
	if (!ppp->closing) {
L
Linus Torvalds 已提交
1409
		ppp_push(ppp);
1410 1411 1412

		if (skb)
			skb_queue_tail(&ppp->file.xq, skb);
1413 1414
		while (!ppp->xmit_pending &&
		       (skb = skb_dequeue(&ppp->file.xq)))
L
Linus Torvalds 已提交
1415 1416 1417
			ppp_send_frame(ppp, skb);
		/* If there's no work left to do, tell the core net
		   code that we can accept some more. */
1418
		if (!ppp->xmit_pending && !skb_peek(&ppp->file.xq))
L
Linus Torvalds 已提交
1419
			netif_wake_queue(ppp->dev);
1420 1421
		else
			netif_stop_queue(ppp->dev);
L
Linus Torvalds 已提交
1422 1423 1424 1425
	}
	ppp_xmit_unlock(ppp);
}

1426
static void ppp_xmit_process(struct ppp *ppp, struct sk_buff *skb)
1427 1428 1429
{
	local_bh_disable();

1430
	if (unlikely(*this_cpu_ptr(ppp->xmit_recursion)))
1431 1432
		goto err;

1433
	(*this_cpu_ptr(ppp->xmit_recursion))++;
1434
	__ppp_xmit_process(ppp, skb);
1435
	(*this_cpu_ptr(ppp->xmit_recursion))--;
1436 1437 1438 1439 1440 1441 1442 1443

	local_bh_enable();

	return;

err:
	local_bh_enable();

1444 1445
	kfree_skb(skb);

1446 1447 1448 1449
	if (net_ratelimit())
		netdev_err(ppp->dev, "recursion detected\n");
}

1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
static inline struct sk_buff *
pad_compress_skb(struct ppp *ppp, struct sk_buff *skb)
{
	struct sk_buff *new_skb;
	int len;
	int new_skb_size = ppp->dev->mtu +
		ppp->xcomp->comp_extra + ppp->dev->hard_header_len;
	int compressor_skb_size = ppp->dev->mtu +
		ppp->xcomp->comp_extra + PPP_HDRLEN;
	new_skb = alloc_skb(new_skb_size, GFP_ATOMIC);
	if (!new_skb) {
		if (net_ratelimit())
1462
			netdev_err(ppp->dev, "PPP: no memory (comp pkt)\n");
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
		return NULL;
	}
	if (ppp->dev->hard_header_len > PPP_HDRLEN)
		skb_reserve(new_skb,
			    ppp->dev->hard_header_len - PPP_HDRLEN);

	/* compressor still expects A/C bytes in hdr */
	len = ppp->xcomp->compress(ppp->xc_state, skb->data - 2,
				   new_skb->data, skb->len + 2,
				   compressor_skb_size);
	if (len > 0 && (ppp->flags & SC_CCP_UP)) {
1474
		consume_skb(skb);
1475 1476 1477 1478 1479
		skb = new_skb;
		skb_put(skb, len);
		skb_pull(skb, 2);	/* pull off A/C bytes */
	} else if (len == 0) {
		/* didn't compress, or CCP not up yet */
1480
		consume_skb(new_skb);
1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
		new_skb = skb;
	} else {
		/*
		 * (len < 0)
		 * MPPE requires that we do not send unencrypted
		 * frames.  The compressor will return -1 if we
		 * should drop the frame.  We cannot simply test
		 * the compress_proto because MPPE and MPPC share
		 * the same number.
		 */
		if (net_ratelimit())
1492
			netdev_err(ppp->dev, "ppp: compressor dropped pkt\n");
1493
		kfree_skb(skb);
1494
		consume_skb(new_skb);
1495 1496 1497 1498 1499
		new_skb = NULL;
	}
	return new_skb;
}

L
Linus Torvalds 已提交
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
/*
 * Compress and send a frame.
 * The caller should have locked the xmit path,
 * and xmit_pending should be 0.
 */
static void
ppp_send_frame(struct ppp *ppp, struct sk_buff *skb)
{
	int proto = PPP_PROTO(skb);
	struct sk_buff *new_skb;
	int len;
	unsigned char *cp;

	if (proto < 0x8000) {
#ifdef CONFIG_PPP_FILTER
		/* check if we should pass this packet */
		/* the filter instructions are constructed assuming
		   a four-byte PPP header on each packet */
1518
		*(u8 *)skb_push(skb, 2) = 1;
1519
		if (ppp->pass_filter &&
1520
		    BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
L
Linus Torvalds 已提交
1521
			if (ppp->debug & 1)
1522 1523 1524
				netdev_printk(KERN_DEBUG, ppp->dev,
					      "PPP: outbound frame "
					      "not passed\n");
L
Linus Torvalds 已提交
1525 1526 1527 1528
			kfree_skb(skb);
			return;
		}
		/* if this packet passes the active filter, record the time */
1529
		if (!(ppp->active_filter &&
1530
		      BPF_PROG_RUN(ppp->active_filter, skb) == 0))
L
Linus Torvalds 已提交
1531 1532 1533 1534 1535 1536 1537 1538
			ppp->last_xmit = jiffies;
		skb_pull(skb, 2);
#else
		/* for data packets, record the time */
		ppp->last_xmit = jiffies;
#endif /* CONFIG_PPP_FILTER */
	}

K
Kevin Groeneveld 已提交
1539 1540
	++ppp->stats64.tx_packets;
	ppp->stats64.tx_bytes += skb->len - 2;
L
Linus Torvalds 已提交
1541 1542 1543

	switch (proto) {
	case PPP_IP:
J
Joe Perches 已提交
1544
		if (!ppp->vj || (ppp->flags & SC_COMP_TCP) == 0)
L
Linus Torvalds 已提交
1545 1546 1547 1548
			break;
		/* try to do VJ TCP header compression */
		new_skb = alloc_skb(skb->len + ppp->dev->hard_header_len - 2,
				    GFP_ATOMIC);
J
Joe Perches 已提交
1549
		if (!new_skb) {
1550
			netdev_err(ppp->dev, "PPP: no memory (VJ comp pkt)\n");
L
Linus Torvalds 已提交
1551 1552 1553 1554 1555 1556 1557 1558 1559
			goto drop;
		}
		skb_reserve(new_skb, ppp->dev->hard_header_len - 2);
		cp = skb->data + 2;
		len = slhc_compress(ppp->vj, cp, skb->len - 2,
				    new_skb->data + 2, &cp,
				    !(ppp->flags & SC_NO_TCP_CCID));
		if (cp == skb->data + 2) {
			/* didn't compress */
1560
			consume_skb(new_skb);
L
Linus Torvalds 已提交
1561 1562 1563 1564 1565 1566 1567 1568
		} else {
			if (cp[0] & SL_TYPE_COMPRESSED_TCP) {
				proto = PPP_VJC_COMP;
				cp[0] &= ~SL_TYPE_COMPRESSED_TCP;
			} else {
				proto = PPP_VJC_UNCOMP;
				cp[0] = skb->data[2];
			}
1569
			consume_skb(skb);
L
Linus Torvalds 已提交
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583
			skb = new_skb;
			cp = skb_put(skb, len + 2);
			cp[0] = 0;
			cp[1] = proto;
		}
		break;

	case PPP_CCP:
		/* peek at outbound CCP frames */
		ppp_ccp_peek(ppp, skb, 0);
		break;
	}

	/* try to do packet compression */
1584 1585
	if ((ppp->xstate & SC_COMP_RUN) && ppp->xc_state &&
	    proto != PPP_LCP && proto != PPP_CCP) {
1586 1587
		if (!(ppp->flags & SC_CCP_UP) && (ppp->flags & SC_MUST_COMP)) {
			if (net_ratelimit())
1588 1589 1590
				netdev_err(ppp->dev,
					   "ppp: compression required but "
					   "down - pkt dropped.\n");
L
Linus Torvalds 已提交
1591 1592
			goto drop;
		}
1593 1594 1595
		skb = pad_compress_skb(ppp, skb);
		if (!skb)
			goto drop;
L
Linus Torvalds 已提交
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
	}

	/*
	 * If we are waiting for traffic (demand dialling),
	 * queue it up for pppd to receive.
	 */
	if (ppp->flags & SC_LOOP_TRAFFIC) {
		if (ppp->file.rq.qlen > PPP_MAX_RQLEN)
			goto drop;
		skb_queue_tail(&ppp->file.rq, skb);
		wake_up_interruptible(&ppp->file.rwait);
		return;
	}

	ppp->xmit_pending = skb;
	ppp_push(ppp);
	return;

 drop:
1615
	kfree_skb(skb);
1616
	++ppp->dev->stats.tx_errors;
L
Linus Torvalds 已提交
1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
}

/*
 * Try to send the frame in xmit_pending.
 * The caller should have the xmit path locked.
 */
static void
ppp_push(struct ppp *ppp)
{
	struct list_head *list;
	struct channel *pch;
	struct sk_buff *skb = ppp->xmit_pending;

J
Joe Perches 已提交
1630
	if (!skb)
L
Linus Torvalds 已提交
1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
		return;

	list = &ppp->channels;
	if (list_empty(list)) {
		/* nowhere to send the packet, just drop it */
		ppp->xmit_pending = NULL;
		kfree_skb(skb);
		return;
	}

	if ((ppp->flags & SC_MULTILINK) == 0) {
		/* not doing multilink: send it down the first channel */
		list = list->next;
		pch = list_entry(list, struct channel, clist);

1646
		spin_lock(&pch->downl);
L
Linus Torvalds 已提交
1647 1648 1649 1650 1651 1652 1653 1654
		if (pch->chan) {
			if (pch->chan->ops->start_xmit(pch->chan, skb))
				ppp->xmit_pending = NULL;
		} else {
			/* channel got unregistered */
			kfree_skb(skb);
			ppp->xmit_pending = NULL;
		}
1655
		spin_unlock(&pch->downl);
L
Linus Torvalds 已提交
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
		return;
	}

#ifdef CONFIG_PPP_MULTILINK
	/* Multilink: fragment the packet over as many links
	   as can take the packet at the moment. */
	if (!ppp_mp_explode(ppp, skb))
		return;
#endif /* CONFIG_PPP_MULTILINK */

	ppp->xmit_pending = NULL;
	kfree_skb(skb);
}

#ifdef CONFIG_PPP_MULTILINK
1671
static bool mp_protocol_compress __read_mostly = true;
1672
module_param(mp_protocol_compress, bool, 0644);
1673 1674 1675
MODULE_PARM_DESC(mp_protocol_compress,
		 "compress protocol id in multilink fragments");

L
Linus Torvalds 已提交
1676 1677 1678 1679 1680 1681
/*
 * Divide a packet to be transmitted into fragments and
 * send them out the individual links.
 */
static int ppp_mp_explode(struct ppp *ppp, struct sk_buff *skb)
{
1682 1683 1684 1685 1686 1687 1688
	int len, totlen;
	int i, bits, hdrlen, mtu;
	int flen;
	int navail, nfree, nzero;
	int nbigger;
	int totspeed;
	int totfree;
L
Linus Torvalds 已提交
1689 1690 1691 1692 1693 1694
	unsigned char *p, *q;
	struct list_head *list;
	struct channel *pch;
	struct sk_buff *frag;
	struct ppp_channel *chan;

1695
	totspeed = 0; /*total bitrate of the bundle*/
1696 1697 1698 1699
	nfree = 0; /* # channels which have no packet already queued */
	navail = 0; /* total # of usable channels (not deregistered) */
	nzero = 0; /* number of channels with zero speed associated*/
	totfree = 0; /*total # of channels available and
1700 1701 1702
				  *having no queued packets before
				  *starting the fragmentation*/

L
Linus Torvalds 已提交
1703
	hdrlen = (ppp->flags & SC_MP_XSHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;
1704 1705
	i = 0;
	list_for_each_entry(pch, &ppp->channels, clist) {
1706 1707 1708 1709 1710 1711 1712
		if (pch->chan) {
			pch->avail = 1;
			navail++;
			pch->speed = pch->chan->speed;
		} else {
			pch->avail = 0;
		}
1713
		if (pch->avail) {
1714
			if (skb_queue_empty(&pch->file.xq) ||
1715
				!pch->had_frag) {
1716 1717 1718 1719 1720 1721 1722 1723 1724
					if (pch->speed == 0)
						nzero++;
					else
						totspeed += pch->speed;

					pch->avail = 2;
					++nfree;
					++totfree;
				}
1725 1726
			if (!pch->had_frag && i < ppp->nxchan)
				ppp->nxchan = i;
L
Linus Torvalds 已提交
1727
		}
1728
		++i;
L
Linus Torvalds 已提交
1729
	}
1730
	/*
1731 1732 1733
	 * Don't start sending this packet unless at least half of
	 * the channels are free.  This gives much better TCP
	 * performance if we have a lot of channels.
1734
	 */
1735 1736
	if (nfree == 0 || nfree < navail / 2)
		return 0; /* can't take now, leave it in xmit_pending */
L
Linus Torvalds 已提交
1737

1738
	/* Do protocol field compression */
1739 1740
	p = skb->data;
	len = skb->len;
1741
	if (*p == 0 && mp_protocol_compress) {
L
Linus Torvalds 已提交
1742 1743 1744 1745
		++p;
		--len;
	}

1746
	totlen = len;
1747
	nbigger = len % nfree;
1748

1749 1750
	/* skip to the channel after the one we last used
	   and start at that one */
1751
	list = &ppp->channels;
1752
	for (i = 0; i < ppp->nxchan; ++i) {
L
Linus Torvalds 已提交
1753
		list = list->next;
1754 1755
		if (list == &ppp->channels) {
			i = 0;
L
Linus Torvalds 已提交
1756 1757 1758 1759
			break;
		}
	}

1760
	/* create a fragment for each channel */
L
Linus Torvalds 已提交
1761
	bits = B;
1762
	while (len > 0) {
L
Linus Torvalds 已提交
1763
		list = list->next;
1764 1765
		if (list == &ppp->channels) {
			i = 0;
L
Linus Torvalds 已提交
1766 1767
			continue;
		}
1768
		pch = list_entry(list, struct channel, clist);
L
Linus Torvalds 已提交
1769 1770 1771 1772
		++i;
		if (!pch->avail)
			continue;

1773
		/*
1774 1775
		 * Skip this channel if it has a fragment pending already and
		 * we haven't given a fragment to all of the free channels.
1776 1777
		 */
		if (pch->avail == 1) {
1778
			if (nfree > 0)
1779 1780 1781 1782 1783
				continue;
		} else {
			pch->avail = 1;
		}

L
Linus Torvalds 已提交
1784
		/* check the channel's mtu and whether it is still attached. */
1785
		spin_lock(&pch->downl);
1786
		if (pch->chan == NULL) {
1787
			/* can't use this channel, it's being deregistered */
1788 1789 1790
			if (pch->speed == 0)
				nzero--;
			else
1791
				totspeed -= pch->speed;
1792

1793
			spin_unlock(&pch->downl);
L
Linus Torvalds 已提交
1794
			pch->avail = 0;
1795 1796 1797
			totlen = len;
			totfree--;
			nfree--;
1798
			if (--navail == 0)
L
Linus Torvalds 已提交
1799 1800 1801 1802 1803
				break;
			continue;
		}

		/*
1804
		*if the channel speed is not set divide
1805
		*the packet evenly among the free channels;
1806 1807 1808
		*otherwise divide it according to the speed
		*of the channel we are going to transmit on
		*/
1809
		flen = len;
1810 1811
		if (nfree > 0) {
			if (pch->speed == 0) {
1812
				flen = len/nfree;
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822
				if (nbigger > 0) {
					flen++;
					nbigger--;
				}
			} else {
				flen = (((totfree - nzero)*(totlen + hdrlen*totfree)) /
					((totspeed*totfree)/pch->speed)) - hdrlen;
				if (nbigger > 0) {
					flen += ((totfree - nzero)*pch->speed)/totspeed;
					nbigger -= ((totfree - nzero)*pch->speed)/
1823
							totspeed;
1824
				}
1825
			}
1826
			nfree--;
1827 1828 1829
		}

		/*
1830
		 *check if we are on the last channel or
L
Lucas De Marchi 已提交
1831
		 *we exceded the length of the data to
1832 1833
		 *fragment
		 */
1834
		if ((nfree <= 0) || (flen > len))
1835 1836 1837 1838 1839 1840
			flen = len;
		/*
		 *it is not worth to tx on slow channels:
		 *in that case from the resulting flen according to the
		 *above formula will be equal or less than zero.
		 *Skip the channel in this case
L
Linus Torvalds 已提交
1841
		 */
1842
		if (flen <= 0) {
1843
			pch->avail = 2;
1844
			spin_unlock(&pch->downl);
1845 1846 1847
			continue;
		}

1848 1849 1850 1851 1852 1853
		/*
		 * hdrlen includes the 2-byte PPP protocol field, but the
		 * MTU counts only the payload excluding the protocol field.
		 * (RFC1661 Section 2)
		 */
		mtu = pch->chan->mtu - (hdrlen - 2);
1854 1855
		if (mtu < 4)
			mtu = 4;
1856 1857
		if (flen > mtu)
			flen = mtu;
1858 1859 1860
		if (flen == len)
			bits |= E;
		frag = alloc_skb(flen + hdrlen + (flen == 0), GFP_ATOMIC);
J
Joe Perches 已提交
1861
		if (!frag)
1862
			goto noskb;
1863
		q = skb_put(frag, flen + hdrlen);
1864

1865
		/* make the MP header */
1866
		put_unaligned_be16(PPP_MP, q);
1867
		if (ppp->flags & SC_MP_XSHORTSEQ) {
1868
			q[2] = bits + ((ppp->nxseq >> 8) & 0xf);
1869 1870 1871 1872 1873 1874
			q[3] = ppp->nxseq;
		} else {
			q[2] = bits;
			q[3] = ppp->nxseq >> 16;
			q[4] = ppp->nxseq >> 8;
			q[5] = ppp->nxseq;
L
Linus Torvalds 已提交
1875
		}
1876

1877
		memcpy(q + hdrlen, p, flen);
1878 1879 1880

		/* try to send it down the channel */
		chan = pch->chan;
1881
		if (!skb_queue_empty(&pch->file.xq) ||
1882
			!chan->ops->start_xmit(chan, frag))
1883
			skb_queue_tail(&pch->file.xq, frag);
1884
		pch->had_frag = 1;
1885
		p += flen;
1886
		len -= flen;
1887 1888
		++ppp->nxseq;
		bits = 0;
1889
		spin_unlock(&pch->downl);
1890
	}
1891
	ppp->nxchan = i;
L
Linus Torvalds 已提交
1892 1893 1894 1895

	return 1;

 noskb:
1896
	spin_unlock(&pch->downl);
L
Linus Torvalds 已提交
1897
	if (ppp->debug & 1)
1898
		netdev_err(ppp->dev, "PPP: no memory (fragment)\n");
1899
	++ppp->dev->stats.tx_errors;
L
Linus Torvalds 已提交
1900 1901 1902 1903 1904
	++ppp->nxseq;
	return 1;	/* abandon the frame */
}
#endif /* CONFIG_PPP_MULTILINK */

1905 1906
/* Try to send data out on a channel */
static void __ppp_channel_push(struct channel *pch)
L
Linus Torvalds 已提交
1907 1908 1909 1910
{
	struct sk_buff *skb;
	struct ppp *ppp;

1911
	spin_lock(&pch->downl);
J
Joe Perches 已提交
1912
	if (pch->chan) {
1913
		while (!skb_queue_empty(&pch->file.xq)) {
L
Linus Torvalds 已提交
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
			skb = skb_dequeue(&pch->file.xq);
			if (!pch->chan->ops->start_xmit(pch->chan, skb)) {
				/* put the packet back and try again later */
				skb_queue_head(&pch->file.xq, skb);
				break;
			}
		}
	} else {
		/* channel got deregistered */
		skb_queue_purge(&pch->file.xq);
	}
1925
	spin_unlock(&pch->downl);
L
Linus Torvalds 已提交
1926
	/* see if there is anything from the attached unit to be sent */
1927
	if (skb_queue_empty(&pch->file.xq)) {
L
Linus Torvalds 已提交
1928
		ppp = pch->ppp;
J
Joe Perches 已提交
1929
		if (ppp)
1930
			__ppp_xmit_process(ppp, NULL);
L
Linus Torvalds 已提交
1931 1932 1933
	}
}

1934 1935
static void ppp_channel_push(struct channel *pch)
{
1936 1937 1938 1939 1940 1941 1942 1943 1944
	read_lock_bh(&pch->upl);
	if (pch->ppp) {
		(*this_cpu_ptr(pch->ppp->xmit_recursion))++;
		__ppp_channel_push(pch);
		(*this_cpu_ptr(pch->ppp->xmit_recursion))--;
	} else {
		__ppp_channel_push(pch);
	}
	read_unlock_bh(&pch->upl);
1945 1946
}

L
Linus Torvalds 已提交
1947 1948 1949 1950
/*
 * Receive-side routines.
 */

1951 1952 1953 1954 1955
struct ppp_mp_skb_parm {
	u32		sequence;
	u8		BEbits;
};
#define PPP_MP_CB(skb)	((struct ppp_mp_skb_parm *)((skb)->cb))
L
Linus Torvalds 已提交
1956 1957 1958 1959 1960

static inline void
ppp_do_recv(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
{
	ppp_recv_lock(ppp);
1961
	if (!ppp->closing)
L
Linus Torvalds 已提交
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973
		ppp_receive_frame(ppp, skb, pch);
	else
		kfree_skb(skb);
	ppp_recv_unlock(ppp);
}

void
ppp_input(struct ppp_channel *chan, struct sk_buff *skb)
{
	struct channel *pch = chan->ppp;
	int proto;

1974
	if (!pch) {
L
Linus Torvalds 已提交
1975 1976 1977
		kfree_skb(skb);
		return;
	}
1978

L
Linus Torvalds 已提交
1979
	read_lock_bh(&pch->upl);
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
	if (!pskb_may_pull(skb, 2)) {
		kfree_skb(skb);
		if (pch->ppp) {
			++pch->ppp->dev->stats.rx_length_errors;
			ppp_receive_error(pch->ppp);
		}
		goto done;
	}

	proto = PPP_PROTO(skb);
J
Joe Perches 已提交
1990
	if (!pch->ppp || proto >= 0xc000 || proto == PPP_CCPFRAG) {
L
Linus Torvalds 已提交
1991 1992 1993
		/* put it on the channel queue */
		skb_queue_tail(&pch->file.rq, skb);
		/* drop old frames if queue too long */
1994 1995
		while (pch->file.rq.qlen > PPP_MAX_RQLEN &&
		       (skb = skb_dequeue(&pch->file.rq)))
L
Linus Torvalds 已提交
1996 1997 1998 1999 2000
			kfree_skb(skb);
		wake_up_interruptible(&pch->file.rwait);
	} else {
		ppp_do_recv(pch->ppp, skb, pch);
	}
2001 2002

done:
L
Linus Torvalds 已提交
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012
	read_unlock_bh(&pch->upl);
}

/* Put a 0-length skb in the receive queue as an error indication */
void
ppp_input_error(struct ppp_channel *chan, int code)
{
	struct channel *pch = chan->ppp;
	struct sk_buff *skb;

J
Joe Perches 已提交
2013
	if (!pch)
L
Linus Torvalds 已提交
2014 2015 2016
		return;

	read_lock_bh(&pch->upl);
J
Joe Perches 已提交
2017
	if (pch->ppp) {
L
Linus Torvalds 已提交
2018
		skb = alloc_skb(0, GFP_ATOMIC);
J
Joe Perches 已提交
2019
		if (skb) {
L
Linus Torvalds 已提交
2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
			skb->len = 0;		/* probably unnecessary */
			skb->cb[0] = code;
			ppp_do_recv(pch->ppp, skb, pch);
		}
	}
	read_unlock_bh(&pch->upl);
}

/*
 * We come in here to process a received frame.
 * The receive side of the ppp unit is locked.
 */
static void
ppp_receive_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
{
2035 2036
	/* note: a 0-length skb is used as an error indication */
	if (skb->len > 0) {
2037
		skb_checksum_complete_unset(skb);
L
Linus Torvalds 已提交
2038 2039 2040 2041 2042 2043 2044
#ifdef CONFIG_PPP_MULTILINK
		/* XXX do channel-level decompression here */
		if (PPP_PROTO(skb) == PPP_MP)
			ppp_receive_mp_frame(ppp, skb, pch);
		else
#endif /* CONFIG_PPP_MULTILINK */
			ppp_receive_nonmp_frame(ppp, skb);
2045 2046 2047
	} else {
		kfree_skb(skb);
		ppp_receive_error(ppp);
L
Linus Torvalds 已提交
2048 2049 2050 2051 2052 2053
	}
}

static void
ppp_receive_error(struct ppp *ppp)
{
2054
	++ppp->dev->stats.rx_errors;
J
Joe Perches 已提交
2055
	if (ppp->vj)
L
Linus Torvalds 已提交
2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069
		slhc_toss(ppp->vj);
}

static void
ppp_receive_nonmp_frame(struct ppp *ppp, struct sk_buff *skb)
{
	struct sk_buff *ns;
	int proto, len, npi;

	/*
	 * Decompress the frame, if compressed.
	 * Note that some decompressors need to see uncompressed frames
	 * that come in as well as compressed frames.
	 */
2070 2071
	if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN) &&
	    (ppp->rstate & (SC_DC_FERROR | SC_DC_ERROR)) == 0)
L
Linus Torvalds 已提交
2072 2073
		skb = ppp_decompress_frame(ppp, skb);

2074 2075 2076
	if (ppp->flags & SC_MUST_COMP && ppp->rstate & SC_DC_FERROR)
		goto err;

L
Linus Torvalds 已提交
2077 2078 2079 2080
	proto = PPP_PROTO(skb);
	switch (proto) {
	case PPP_VJC_COMP:
		/* decompress VJ compressed packets */
J
Joe Perches 已提交
2081
		if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
L
Linus Torvalds 已提交
2082 2083
			goto err;

2084
		if (skb_tailroom(skb) < 124 || skb_cloned(skb)) {
L
Linus Torvalds 已提交
2085 2086
			/* copy to a new sk_buff with more tailroom */
			ns = dev_alloc_skb(skb->len + 128);
J
Joe Perches 已提交
2087
			if (!ns) {
2088 2089
				netdev_err(ppp->dev, "PPP: no memory "
					   "(VJ decomp)\n");
L
Linus Torvalds 已提交
2090 2091 2092 2093
				goto err;
			}
			skb_reserve(ns, 2);
			skb_copy_bits(skb, 0, skb_put(ns, skb->len), skb->len);
2094
			consume_skb(skb);
L
Linus Torvalds 已提交
2095 2096
			skb = ns;
		}
2097 2098
		else
			skb->ip_summed = CHECKSUM_NONE;
L
Linus Torvalds 已提交
2099 2100 2101

		len = slhc_uncompress(ppp->vj, skb->data + 2, skb->len - 2);
		if (len <= 0) {
2102 2103
			netdev_printk(KERN_DEBUG, ppp->dev,
				      "PPP: VJ decompression error\n");
L
Linus Torvalds 已提交
2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114
			goto err;
		}
		len += 2;
		if (len > skb->len)
			skb_put(skb, len - skb->len);
		else if (len < skb->len)
			skb_trim(skb, len);
		proto = PPP_IP;
		break;

	case PPP_VJC_UNCOMP:
J
Joe Perches 已提交
2115
		if (!ppp->vj || (ppp->flags & SC_REJ_COMP_TCP))
L
Linus Torvalds 已提交
2116
			goto err;
2117

L
Linus Torvalds 已提交
2118 2119 2120
		/* Until we fix the decompressor need to make sure
		 * data portion is linear.
		 */
2121
		if (!pskb_may_pull(skb, skb->len))
L
Linus Torvalds 已提交
2122 2123 2124
			goto err;

		if (slhc_remember(ppp->vj, skb->data + 2, skb->len - 2) <= 0) {
2125
			netdev_err(ppp->dev, "PPP: VJ uncompressed error\n");
L
Linus Torvalds 已提交
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
			goto err;
		}
		proto = PPP_IP;
		break;

	case PPP_CCP:
		ppp_ccp_peek(ppp, skb, 1);
		break;
	}

K
Kevin Groeneveld 已提交
2136 2137
	++ppp->stats64.rx_packets;
	ppp->stats64.rx_bytes += skb->len - 2;
L
Linus Torvalds 已提交
2138 2139 2140 2141 2142 2143

	npi = proto_to_npindex(proto);
	if (npi < 0) {
		/* control or unknown frame - pass it to pppd */
		skb_queue_tail(&ppp->file.rq, skb);
		/* limit queue length by dropping old frames */
2144 2145
		while (ppp->file.rq.qlen > PPP_MAX_RQLEN &&
		       (skb = skb_dequeue(&ppp->file.rq)))
L
Linus Torvalds 已提交
2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156
			kfree_skb(skb);
		/* wake up any process polling or blocking on read */
		wake_up_interruptible(&ppp->file.rwait);

	} else {
		/* network protocol frame - give it to the kernel */

#ifdef CONFIG_PPP_FILTER
		/* check if the packet passes the pass and active filters */
		/* the filter instructions are constructed assuming
		   a four-byte PPP header on each packet */
2157
		if (ppp->pass_filter || ppp->active_filter) {
2158
			if (skb_unclone(skb, GFP_ATOMIC))
2159 2160
				goto err;

2161
			*(u8 *)skb_push(skb, 2) = 0;
2162
			if (ppp->pass_filter &&
2163
			    BPF_PROG_RUN(ppp->pass_filter, skb) == 0) {
2164
				if (ppp->debug & 1)
2165 2166 2167
					netdev_printk(KERN_DEBUG, ppp->dev,
						      "PPP: inbound frame "
						      "not passed\n");
2168 2169 2170
				kfree_skb(skb);
				return;
			}
2171
			if (!(ppp->active_filter &&
2172
			      BPF_PROG_RUN(ppp->active_filter, skb) == 0))
2173 2174 2175
				ppp->last_recv = jiffies;
			__skb_pull(skb, 2);
		} else
L
Linus Torvalds 已提交
2176
#endif /* CONFIG_PPP_FILTER */
2177
			ppp->last_recv = jiffies;
L
Linus Torvalds 已提交
2178

2179 2180
		if ((ppp->dev->flags & IFF_UP) == 0 ||
		    ppp->npmode[npi] != NPMODE_PASS) {
L
Linus Torvalds 已提交
2181 2182
			kfree_skb(skb);
		} else {
2183 2184
			/* chop off protocol */
			skb_pull_rcsum(skb, 2);
L
Linus Torvalds 已提交
2185 2186
			skb->dev = ppp->dev;
			skb->protocol = htons(npindex_to_ethertype[npi]);
2187
			skb_reset_mac_header(skb);
G
Guillaume Nault 已提交
2188 2189
			skb_scrub_packet(skb, !net_eq(ppp->ppp_net,
						      dev_net(ppp->dev)));
L
Linus Torvalds 已提交
2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213
			netif_rx(skb);
		}
	}
	return;

 err:
	kfree_skb(skb);
	ppp_receive_error(ppp);
}

static struct sk_buff *
ppp_decompress_frame(struct ppp *ppp, struct sk_buff *skb)
{
	int proto = PPP_PROTO(skb);
	struct sk_buff *ns;
	int len;

	/* Until we fix all the decompressor's need to make sure
	 * data portion is linear.
	 */
	if (!pskb_may_pull(skb, skb->len))
		goto err;

	if (proto == PPP_COMP) {
2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
		int obuff_size;

		switch(ppp->rcomp->compress_proto) {
		case CI_MPPE:
			obuff_size = ppp->mru + PPP_HDRLEN + 1;
			break;
		default:
			obuff_size = ppp->mru + PPP_HDRLEN;
			break;
		}

		ns = dev_alloc_skb(obuff_size);
J
Joe Perches 已提交
2226
		if (!ns) {
2227 2228
			netdev_err(ppp->dev, "ppp_decompress_frame: "
				   "no memory\n");
L
Linus Torvalds 已提交
2229 2230 2231 2232
			goto err;
		}
		/* the decompressor still expects the A/C bytes in the hdr */
		len = ppp->rcomp->decompress(ppp->rc_state, skb->data - 2,
2233
				skb->len + 2, ns->data, obuff_size);
L
Linus Torvalds 已提交
2234 2235 2236 2237 2238 2239 2240 2241 2242
		if (len < 0) {
			/* Pass the compressed frame to pppd as an
			   error indication. */
			if (len == DECOMP_FATALERROR)
				ppp->rstate |= SC_DC_FERROR;
			kfree_skb(ns);
			goto err;
		}

2243
		consume_skb(skb);
L
Linus Torvalds 已提交
2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273
		skb = ns;
		skb_put(skb, len);
		skb_pull(skb, 2);	/* pull off the A/C bytes */

	} else {
		/* Uncompressed frame - pass to decompressor so it
		   can update its dictionary if necessary. */
		if (ppp->rcomp->incomp)
			ppp->rcomp->incomp(ppp->rc_state, skb->data - 2,
					   skb->len + 2);
	}

	return skb;

 err:
	ppp->rstate |= SC_DC_ERROR;
	ppp_receive_error(ppp);
	return skb;
}

#ifdef CONFIG_PPP_MULTILINK
/*
 * Receive a multilink frame.
 * We put it on the reconstruction queue and then pull off
 * as many completed frames as we can.
 */
static void
ppp_receive_mp_frame(struct ppp *ppp, struct sk_buff *skb, struct channel *pch)
{
	u32 mask, seq;
2274
	struct channel *ch;
L
Linus Torvalds 已提交
2275 2276
	int mphdrlen = (ppp->flags & SC_MP_SHORTSEQ)? MPHDRLEN_SSN: MPHDRLEN;

2277
	if (!pskb_may_pull(skb, mphdrlen + 1) || ppp->mrru == 0)
L
Linus Torvalds 已提交
2278 2279 2280 2281 2282 2283 2284 2285 2286 2287
		goto err;		/* no good, throw it away */

	/* Decode sequence number and begin/end bits */
	if (ppp->flags & SC_MP_SHORTSEQ) {
		seq = ((skb->data[2] & 0x0f) << 8) | skb->data[3];
		mask = 0xfff;
	} else {
		seq = (skb->data[3] << 16) | (skb->data[4] << 8)| skb->data[5];
		mask = 0xffffff;
	}
2288
	PPP_MP_CB(skb)->BEbits = skb->data[2];
L
Linus Torvalds 已提交
2289 2290 2291 2292 2293
	skb_pull(skb, mphdrlen);	/* pull off PPP and MP headers */

	/*
	 * Do protocol ID decompression on the first fragment of each packet.
	 */
2294
	if ((PPP_MP_CB(skb)->BEbits & B) && (skb->data[0] & 1))
2295
		*(u8 *)skb_push(skb, 1) = 0;
L
Linus Torvalds 已提交
2296 2297 2298 2299 2300 2301 2302 2303 2304 2305

	/*
	 * Expand sequence number to 32 bits, making it as close
	 * as possible to ppp->minseq.
	 */
	seq |= ppp->minseq & ~mask;
	if ((int)(ppp->minseq - seq) > (int)(mask >> 1))
		seq += mask + 1;
	else if ((int)(seq - ppp->minseq) > (int)(mask >> 1))
		seq -= mask + 1;	/* should never happen */
2306
	PPP_MP_CB(skb)->sequence = seq;
L
Linus Torvalds 已提交
2307 2308 2309 2310 2311 2312 2313 2314
	pch->lastseq = seq;

	/*
	 * If this packet comes before the next one we were expecting,
	 * drop it.
	 */
	if (seq_before(seq, ppp->nextseq)) {
		kfree_skb(skb);
2315
		++ppp->dev->stats.rx_dropped;
L
Linus Torvalds 已提交
2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327
		ppp_receive_error(ppp);
		return;
	}

	/*
	 * Reevaluate minseq, the minimum over all channels of the
	 * last sequence number received on each channel.  Because of
	 * the increasing sequence number rule, we know that any fragment
	 * before `minseq' which hasn't arrived is never going to arrive.
	 * The list of channels can't change because we have the receive
	 * side of the ppp unit locked.
	 */
2328
	list_for_each_entry(ch, &ppp->channels, clist) {
L
Linus Torvalds 已提交
2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339
		if (seq_before(ch->lastseq, seq))
			seq = ch->lastseq;
	}
	if (seq_before(ppp->minseq, seq))
		ppp->minseq = seq;

	/* Put the fragment on the reconstruction queue */
	ppp_mp_insert(ppp, skb);

	/* If the queue is getting long, don't wait any longer for packets
	   before the start of the queue. */
2340
	if (skb_queue_len(&ppp->mrq) >= PPP_MP_MAX_QLEN) {
2341
		struct sk_buff *mskb = skb_peek(&ppp->mrq);
2342 2343
		if (seq_before(ppp->minseq, PPP_MP_CB(mskb)->sequence))
			ppp->minseq = PPP_MP_CB(mskb)->sequence;
2344
	}
L
Linus Torvalds 已提交
2345 2346

	/* Pull completed packets off the queue and receive them. */
2347 2348 2349 2350 2351 2352 2353 2354 2355
	while ((skb = ppp_mp_reconstruct(ppp))) {
		if (pskb_may_pull(skb, 2))
			ppp_receive_nonmp_frame(ppp, skb);
		else {
			++ppp->dev->stats.rx_length_errors;
			kfree_skb(skb);
			ppp_receive_error(ppp);
		}
	}
L
Linus Torvalds 已提交
2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372

	return;

 err:
	kfree_skb(skb);
	ppp_receive_error(ppp);
}

/*
 * Insert a fragment on the MP reconstruction queue.
 * The queue is ordered by increasing sequence number.
 */
static void
ppp_mp_insert(struct ppp *ppp, struct sk_buff *skb)
{
	struct sk_buff *p;
	struct sk_buff_head *list = &ppp->mrq;
2373
	u32 seq = PPP_MP_CB(skb)->sequence;
L
Linus Torvalds 已提交
2374 2375 2376

	/* N.B. we don't need to lock the list lock because we have the
	   ppp unit receive-side lock. */
2377
	skb_queue_walk(list, p) {
2378
		if (seq_before(seq, PPP_MP_CB(p)->sequence))
L
Linus Torvalds 已提交
2379
			break;
2380
	}
2381
	__skb_queue_before(list, p, skb);
L
Linus Torvalds 已提交
2382 2383 2384 2385 2386 2387 2388 2389
}

/*
 * Reconstruct a packet from the MP fragment queue.
 * We go through increasing sequence numbers until we find a
 * complete packet, or we get to the sequence number for a fragment
 * which hasn't arrived but might still do so.
 */
S
Stephen Hemminger 已提交
2390
static struct sk_buff *
L
Linus Torvalds 已提交
2391 2392 2393 2394 2395
ppp_mp_reconstruct(struct ppp *ppp)
{
	u32 seq = ppp->nextseq;
	u32 minseq = ppp->minseq;
	struct sk_buff_head *list = &ppp->mrq;
2396
	struct sk_buff *p, *tmp;
L
Linus Torvalds 已提交
2397 2398 2399 2400 2401 2402
	struct sk_buff *head, *tail;
	struct sk_buff *skb = NULL;
	int lost = 0, len = 0;

	if (ppp->mrru == 0)	/* do nothing until mrru is set */
		return NULL;
2403
	head = __skb_peek(list);
L
Linus Torvalds 已提交
2404
	tail = NULL;
2405 2406
	skb_queue_walk_safe(list, p, tmp) {
	again:
2407
		if (seq_before(PPP_MP_CB(p)->sequence, seq)) {
L
Linus Torvalds 已提交
2408
			/* this can't happen, anyway ignore the skb */
2409 2410 2411
			netdev_err(ppp->dev, "ppp_mp_reconstruct bad "
				   "seq %u < %u\n",
				   PPP_MP_CB(p)->sequence, seq);
2412 2413
			__skb_unlink(p, list);
			kfree_skb(p);
L
Linus Torvalds 已提交
2414 2415
			continue;
		}
2416
		if (PPP_MP_CB(p)->sequence != seq) {
2417
			u32 oldseq;
L
Linus Torvalds 已提交
2418 2419 2420 2421 2422 2423
			/* Fragment `seq' is missing.  If it is after
			   minseq, it might arrive later, so stop here. */
			if (seq_after(seq, minseq))
				break;
			/* Fragment `seq' is lost, keep going. */
			lost = 1;
2424
			oldseq = seq;
2425 2426
			seq = seq_before(minseq, PPP_MP_CB(p)->sequence)?
				minseq + 1: PPP_MP_CB(p)->sequence;
2427 2428 2429 2430 2431 2432

			if (ppp->debug & 1)
				netdev_printk(KERN_DEBUG, ppp->dev,
					      "lost frag %u..%u\n",
					      oldseq, seq-1);

2433
			goto again;
L
Linus Torvalds 已提交
2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444
		}

		/*
		 * At this point we know that all the fragments from
		 * ppp->nextseq to seq are either present or lost.
		 * Also, there are no complete packets in the queue
		 * that have no missing fragments and end before this
		 * fragment.
		 */

		/* B bit set indicates this fragment starts a packet */
2445
		if (PPP_MP_CB(p)->BEbits & B) {
L
Linus Torvalds 已提交
2446 2447 2448 2449 2450 2451 2452 2453
			head = p;
			lost = 0;
			len = 0;
		}

		len += p->len;

		/* Got a complete packet yet? */
2454 2455
		if (lost == 0 && (PPP_MP_CB(p)->BEbits & E) &&
		    (PPP_MP_CB(head)->BEbits & B)) {
L
Linus Torvalds 已提交
2456
			if (len > ppp->mrru + 2) {
2457
				++ppp->dev->stats.rx_length_errors;
2458 2459 2460
				netdev_printk(KERN_DEBUG, ppp->dev,
					      "PPP: reconstructed packet"
					      " is too long (%d)\n", len);
L
Linus Torvalds 已提交
2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472
			} else {
				tail = p;
				break;
			}
			ppp->nextseq = seq + 1;
		}

		/*
		 * If this is the ending fragment of a packet,
		 * and we haven't found a complete valid packet yet,
		 * we can discard up to and including this fragment.
		 */
2473 2474
		if (PPP_MP_CB(p)->BEbits & E) {
			struct sk_buff *tmp2;
L
Linus Torvalds 已提交
2475

2476
			skb_queue_reverse_walk_from_safe(list, p, tmp2) {
2477 2478 2479 2480
				if (ppp->debug & 1)
					netdev_printk(KERN_DEBUG, ppp->dev,
						      "discarding frag %u\n",
						      PPP_MP_CB(p)->sequence);
2481 2482 2483 2484 2485 2486 2487
				__skb_unlink(p, list);
				kfree_skb(p);
			}
			head = skb_peek(list);
			if (!head)
				break;
		}
L
Linus Torvalds 已提交
2488 2489 2490 2491 2492 2493 2494
		++seq;
	}

	/* If we have a complete packet, copy it all into one skb. */
	if (tail != NULL) {
		/* If we have discarded any fragments,
		   signal a receive error. */
2495
		if (PPP_MP_CB(head)->sequence != ppp->nextseq) {
2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506
			skb_queue_walk_safe(list, p, tmp) {
				if (p == head)
					break;
				if (ppp->debug & 1)
					netdev_printk(KERN_DEBUG, ppp->dev,
						      "discarding frag %u\n",
						      PPP_MP_CB(p)->sequence);
				__skb_unlink(p, list);
				kfree_skb(p);
			}

L
Linus Torvalds 已提交
2507
			if (ppp->debug & 1)
2508 2509 2510 2511
				netdev_printk(KERN_DEBUG, ppp->dev,
					      "  missed pkts %u..%u\n",
					      ppp->nextseq,
					      PPP_MP_CB(head)->sequence-1);
2512
			++ppp->dev->stats.rx_dropped;
L
Linus Torvalds 已提交
2513 2514 2515
			ppp_receive_error(ppp);
		}

2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528
		skb = head;
		if (head != tail) {
			struct sk_buff **fragpp = &skb_shinfo(skb)->frag_list;
			p = skb_queue_next(list, head);
			__skb_unlink(skb, list);
			skb_queue_walk_from_safe(list, p, tmp) {
				__skb_unlink(p, list);
				*fragpp = p;
				p->next = NULL;
				fragpp = &p->next;

				skb->len += p->len;
				skb->data_len += p->len;
E
Eric Dumazet 已提交
2529
				skb->truesize += p->truesize;
2530 2531 2532 2533 2534 2535 2536 2537

				if (p == tail)
					break;
			}
		} else {
			__skb_unlink(skb, list);
		}

2538
		ppp->nextseq = PPP_MP_CB(tail)->sequence + 1;
L
Linus Torvalds 已提交
2539 2540 2541 2542 2543 2544 2545 2546 2547 2548
	}

	return skb;
}
#endif /* CONFIG_PPP_MULTILINK */

/*
 * Channel interface.
 */

2549 2550 2551 2552 2553 2554 2555 2556
/* Create a new, unattached ppp channel. */
int ppp_register_channel(struct ppp_channel *chan)
{
	return ppp_register_net_channel(current->nsproxy->net_ns, chan);
}

/* Create a new, unattached ppp channel for specified net. */
int ppp_register_net_channel(struct net *net, struct ppp_channel *chan)
L
Linus Torvalds 已提交
2557 2558
{
	struct channel *pch;
2559
	struct ppp_net *pn;
L
Linus Torvalds 已提交
2560

2561
	pch = kzalloc(sizeof(struct channel), GFP_KERNEL);
J
Joe Perches 已提交
2562
	if (!pch)
L
Linus Torvalds 已提交
2563
		return -ENOMEM;
2564 2565 2566

	pn = ppp_pernet(net);

L
Linus Torvalds 已提交
2567 2568
	pch->ppp = NULL;
	pch->chan = chan;
2569
	pch->chan_net = get_net(net);
L
Linus Torvalds 已提交
2570 2571 2572 2573 2574 2575 2576 2577 2578
	chan->ppp = pch;
	init_ppp_file(&pch->file, CHANNEL);
	pch->file.hdrlen = chan->hdrlen;
#ifdef CONFIG_PPP_MULTILINK
	pch->lastseq = -1;
#endif /* CONFIG_PPP_MULTILINK */
	init_rwsem(&pch->chan_sem);
	spin_lock_init(&pch->downl);
	rwlock_init(&pch->upl);
2579 2580 2581 2582

	spin_lock_bh(&pn->all_channels_lock);
	pch->file.index = ++pn->last_channel_index;
	list_add(&pch->list, &pn->new_channels);
L
Linus Torvalds 已提交
2583
	atomic_inc(&channel_count);
2584 2585
	spin_unlock_bh(&pn->all_channels_lock);

L
Linus Torvalds 已提交
2586 2587 2588 2589 2590 2591 2592 2593 2594 2595
	return 0;
}

/*
 * Return the index of a channel.
 */
int ppp_channel_index(struct ppp_channel *chan)
{
	struct channel *pch = chan->ppp;

J
Joe Perches 已提交
2596
	if (pch)
L
Linus Torvalds 已提交
2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608
		return pch->file.index;
	return -1;
}

/*
 * Return the PPP unit number to which a channel is connected.
 */
int ppp_unit_number(struct ppp_channel *chan)
{
	struct channel *pch = chan->ppp;
	int unit = -1;

J
Joe Perches 已提交
2609
	if (pch) {
L
Linus Torvalds 已提交
2610
		read_lock_bh(&pch->upl);
J
Joe Perches 已提交
2611
		if (pch->ppp)
L
Linus Torvalds 已提交
2612 2613 2614 2615 2616 2617
			unit = pch->ppp->file.index;
		read_unlock_bh(&pch->upl);
	}
	return unit;
}

2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635
/*
 * Return the PPP device interface name of a channel.
 */
char *ppp_dev_name(struct ppp_channel *chan)
{
	struct channel *pch = chan->ppp;
	char *name = NULL;

	if (pch) {
		read_lock_bh(&pch->upl);
		if (pch->ppp && pch->ppp->dev)
			name = pch->ppp->dev->name;
		read_unlock_bh(&pch->upl);
	}
	return name;
}


L
Linus Torvalds 已提交
2636 2637 2638 2639 2640 2641 2642 2643
/*
 * Disconnect a channel from the generic layer.
 * This must be called in process context.
 */
void
ppp_unregister_channel(struct ppp_channel *chan)
{
	struct channel *pch = chan->ppp;
2644
	struct ppp_net *pn;
L
Linus Torvalds 已提交
2645

J
Joe Perches 已提交
2646
	if (!pch)
L
Linus Torvalds 已提交
2647
		return;		/* should never happen */
2648

L
Linus Torvalds 已提交
2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660
	chan->ppp = NULL;

	/*
	 * This ensures that we have returned from any calls into the
	 * the channel's start_xmit or ioctl routine before we proceed.
	 */
	down_write(&pch->chan_sem);
	spin_lock_bh(&pch->downl);
	pch->chan = NULL;
	spin_unlock_bh(&pch->downl);
	up_write(&pch->chan_sem);
	ppp_disconnect_channel(pch);
2661 2662 2663

	pn = ppp_pernet(pch->chan_net);
	spin_lock_bh(&pn->all_channels_lock);
L
Linus Torvalds 已提交
2664
	list_del(&pch->list);
2665 2666
	spin_unlock_bh(&pn->all_channels_lock);

L
Linus Torvalds 已提交
2667 2668
	pch->file.dead = 1;
	wake_up_interruptible(&pch->file.rwait);
2669
	if (refcount_dec_and_test(&pch->file.refcnt))
L
Linus Torvalds 已提交
2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681
		ppp_destroy_channel(pch);
}

/*
 * Callback from a channel when it can accept more to transmit.
 * This should be called at BH/softirq level, not interrupt level.
 */
void
ppp_output_wakeup(struct ppp_channel *chan)
{
	struct channel *pch = chan->ppp;

J
Joe Perches 已提交
2682
	if (!pch)
L
Linus Torvalds 已提交
2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701
		return;
	ppp_channel_push(pch);
}

/*
 * Compression control.
 */

/* Process the PPPIOCSCOMPRESS ioctl. */
static int
ppp_set_compress(struct ppp *ppp, unsigned long arg)
{
	int err;
	struct compressor *cp, *ocomp;
	struct ppp_option_data data;
	void *state, *ostate;
	unsigned char ccp_option[CCP_MAX_OPTION_LENGTH];

	err = -EFAULT;
2702
	if (copy_from_user(&data, (void __user *) arg, sizeof(data)))
L
Linus Torvalds 已提交
2703
		goto out;
2704 2705 2706 2707 2708
	if (data.length > CCP_MAX_OPTION_LENGTH)
		goto out;
	if (copy_from_user(ccp_option, (void __user *) data.ptr, data.length))
		goto out;

L
Linus Torvalds 已提交
2709
	err = -EINVAL;
2710
	if (data.length < 2 || ccp_option[1] < 2 || ccp_option[1] > data.length)
L
Linus Torvalds 已提交
2711 2712
		goto out;

J
Johannes Berg 已提交
2713 2714 2715
	cp = try_then_request_module(
		find_compressor(ccp_option[0]),
		"ppp-compress-%d", ccp_option[0]);
J
Joe Perches 已提交
2716
	if (!cp)
L
Linus Torvalds 已提交
2717 2718 2719 2720 2721
		goto out;

	err = -ENOBUFS;
	if (data.transmit) {
		state = cp->comp_alloc(ccp_option, data.length);
J
Joe Perches 已提交
2722
		if (state) {
L
Linus Torvalds 已提交
2723 2724 2725 2726 2727 2728 2729
			ppp_xmit_lock(ppp);
			ppp->xstate &= ~SC_COMP_RUN;
			ocomp = ppp->xcomp;
			ostate = ppp->xc_state;
			ppp->xcomp = cp;
			ppp->xc_state = state;
			ppp_xmit_unlock(ppp);
J
Joe Perches 已提交
2730
			if (ostate) {
L
Linus Torvalds 已提交
2731 2732 2733 2734 2735 2736 2737 2738 2739
				ocomp->comp_free(ostate);
				module_put(ocomp->owner);
			}
			err = 0;
		} else
			module_put(cp->owner);

	} else {
		state = cp->decomp_alloc(ccp_option, data.length);
J
Joe Perches 已提交
2740
		if (state) {
L
Linus Torvalds 已提交
2741 2742 2743 2744 2745 2746 2747
			ppp_recv_lock(ppp);
			ppp->rstate &= ~SC_DECOMP_RUN;
			ocomp = ppp->rcomp;
			ostate = ppp->rc_state;
			ppp->rcomp = cp;
			ppp->rc_state = state;
			ppp_recv_unlock(ppp);
J
Joe Perches 已提交
2748
			if (ostate) {
L
Linus Torvalds 已提交
2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777
				ocomp->decomp_free(ostate);
				module_put(ocomp->owner);
			}
			err = 0;
		} else
			module_put(cp->owner);
	}

 out:
	return err;
}

/*
 * Look at a CCP packet and update our state accordingly.
 * We assume the caller has the xmit or recv path locked.
 */
static void
ppp_ccp_peek(struct ppp *ppp, struct sk_buff *skb, int inbound)
{
	unsigned char *dp;
	int len;

	if (!pskb_may_pull(skb, CCP_HDRLEN + 2))
		return;	/* no header */
	dp = skb->data + 2;

	switch (CCP_CODE(dp)) {
	case CCP_CONFREQ:

2778
		/* A ConfReq starts negotiation of compression
L
Linus Torvalds 已提交
2779 2780 2781 2782 2783 2784 2785 2786 2787
		 * in one direction of transmission,
		 * and hence brings it down...but which way?
		 *
		 * Remember:
		 * A ConfReq indicates what the sender would like to receive
		 */
		if(inbound)
			/* He is proposing what I should send */
			ppp->xstate &= ~SC_COMP_RUN;
2788
		else
L
Linus Torvalds 已提交
2789 2790
			/* I am proposing to what he should send */
			ppp->rstate &= ~SC_DECOMP_RUN;
2791

L
Linus Torvalds 已提交
2792
		break;
2793

L
Linus Torvalds 已提交
2794 2795 2796
	case CCP_TERMREQ:
	case CCP_TERMACK:
		/*
2797
		 * CCP is going down, both directions of transmission
L
Linus Torvalds 已提交
2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814
		 */
		ppp->rstate &= ~SC_DECOMP_RUN;
		ppp->xstate &= ~SC_COMP_RUN;
		break;

	case CCP_CONFACK:
		if ((ppp->flags & (SC_CCP_OPEN | SC_CCP_UP)) != SC_CCP_OPEN)
			break;
		len = CCP_LENGTH(dp);
		if (!pskb_may_pull(skb, len + 2))
			return;		/* too short */
		dp += CCP_HDRLEN;
		len -= CCP_HDRLEN;
		if (len < CCP_OPT_MINLEN || len < CCP_OPT_LENGTH(dp))
			break;
		if (inbound) {
			/* we will start receiving compressed packets */
J
Joe Perches 已提交
2815
			if (!ppp->rc_state)
L
Linus Torvalds 已提交
2816 2817 2818 2819 2820 2821 2822 2823
				break;
			if (ppp->rcomp->decomp_init(ppp->rc_state, dp, len,
					ppp->file.index, 0, ppp->mru, ppp->debug)) {
				ppp->rstate |= SC_DECOMP_RUN;
				ppp->rstate &= ~(SC_DC_ERROR | SC_DC_FERROR);
			}
		} else {
			/* we will soon start sending compressed packets */
J
Joe Perches 已提交
2824
			if (!ppp->xc_state)
L
Linus Torvalds 已提交
2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891
				break;
			if (ppp->xcomp->comp_init(ppp->xc_state, dp, len,
					ppp->file.index, 0, ppp->debug))
				ppp->xstate |= SC_COMP_RUN;
		}
		break;

	case CCP_RESETACK:
		/* reset the [de]compressor */
		if ((ppp->flags & SC_CCP_UP) == 0)
			break;
		if (inbound) {
			if (ppp->rc_state && (ppp->rstate & SC_DECOMP_RUN)) {
				ppp->rcomp->decomp_reset(ppp->rc_state);
				ppp->rstate &= ~SC_DC_ERROR;
			}
		} else {
			if (ppp->xc_state && (ppp->xstate & SC_COMP_RUN))
				ppp->xcomp->comp_reset(ppp->xc_state);
		}
		break;
	}
}

/* Free up compression resources. */
static void
ppp_ccp_closed(struct ppp *ppp)
{
	void *xstate, *rstate;
	struct compressor *xcomp, *rcomp;

	ppp_lock(ppp);
	ppp->flags &= ~(SC_CCP_OPEN | SC_CCP_UP);
	ppp->xstate = 0;
	xcomp = ppp->xcomp;
	xstate = ppp->xc_state;
	ppp->xc_state = NULL;
	ppp->rstate = 0;
	rcomp = ppp->rcomp;
	rstate = ppp->rc_state;
	ppp->rc_state = NULL;
	ppp_unlock(ppp);

	if (xstate) {
		xcomp->comp_free(xstate);
		module_put(xcomp->owner);
	}
	if (rstate) {
		rcomp->decomp_free(rstate);
		module_put(rcomp->owner);
	}
}

/* List of compressors. */
static LIST_HEAD(compressor_list);
static DEFINE_SPINLOCK(compressor_list_lock);

struct compressor_entry {
	struct list_head list;
	struct compressor *comp;
};

static struct compressor_entry *
find_comp_entry(int proto)
{
	struct compressor_entry *ce;

2892
	list_for_each_entry(ce, &compressor_list, list) {
L
Linus Torvalds 已提交
2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906
		if (ce->comp->compress_proto == proto)
			return ce;
	}
	return NULL;
}

/* Register a compressor */
int
ppp_register_compressor(struct compressor *cp)
{
	struct compressor_entry *ce;
	int ret;
	spin_lock(&compressor_list_lock);
	ret = -EEXIST;
J
Joe Perches 已提交
2907
	if (find_comp_entry(cp->compress_proto))
L
Linus Torvalds 已提交
2908 2909 2910
		goto out;
	ret = -ENOMEM;
	ce = kmalloc(sizeof(struct compressor_entry), GFP_ATOMIC);
J
Joe Perches 已提交
2911
	if (!ce)
L
Linus Torvalds 已提交
2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928
		goto out;
	ret = 0;
	ce->comp = cp;
	list_add(&ce->list, &compressor_list);
 out:
	spin_unlock(&compressor_list_lock);
	return ret;
}

/* Unregister a compressor */
void
ppp_unregister_compressor(struct compressor *cp)
{
	struct compressor_entry *ce;

	spin_lock(&compressor_list_lock);
	ce = find_comp_entry(cp->compress_proto);
J
Joe Perches 已提交
2929
	if (ce && ce->comp == cp) {
L
Linus Torvalds 已提交
2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944
		list_del(&ce->list);
		kfree(ce);
	}
	spin_unlock(&compressor_list_lock);
}

/* Find a compressor. */
static struct compressor *
find_compressor(int type)
{
	struct compressor_entry *ce;
	struct compressor *cp = NULL;

	spin_lock(&compressor_list_lock);
	ce = find_comp_entry(type);
J
Joe Perches 已提交
2945
	if (ce) {
L
Linus Torvalds 已提交
2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963
		cp = ce->comp;
		if (!try_module_get(cp->owner))
			cp = NULL;
	}
	spin_unlock(&compressor_list_lock);
	return cp;
}

/*
 * Miscelleneous stuff.
 */

static void
ppp_get_stats(struct ppp *ppp, struct ppp_stats *st)
{
	struct slcompress *vj = ppp->vj;

	memset(st, 0, sizeof(*st));
K
Kevin Groeneveld 已提交
2964
	st->p.ppp_ipackets = ppp->stats64.rx_packets;
2965
	st->p.ppp_ierrors = ppp->dev->stats.rx_errors;
K
Kevin Groeneveld 已提交
2966 2967
	st->p.ppp_ibytes = ppp->stats64.rx_bytes;
	st->p.ppp_opackets = ppp->stats64.tx_packets;
2968
	st->p.ppp_oerrors = ppp->dev->stats.tx_errors;
K
Kevin Groeneveld 已提交
2969
	st->p.ppp_obytes = ppp->stats64.tx_bytes;
J
Joe Perches 已提交
2970
	if (!vj)
L
Linus Torvalds 已提交
2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991
		return;
	st->vj.vjs_packets = vj->sls_o_compressed + vj->sls_o_uncompressed;
	st->vj.vjs_compressed = vj->sls_o_compressed;
	st->vj.vjs_searches = vj->sls_o_searches;
	st->vj.vjs_misses = vj->sls_o_misses;
	st->vj.vjs_errorin = vj->sls_i_error;
	st->vj.vjs_tossed = vj->sls_i_tossed;
	st->vj.vjs_uncompressedin = vj->sls_i_uncompressed;
	st->vj.vjs_compressedin = vj->sls_i_compressed;
}

/*
 * Stuff for handling the lists of ppp units and channels
 * and for initialization.
 */

/*
 * Create a new ppp interface unit.  Fails if it can't allocate memory
 * or if there is already a unit with the requested number.
 * unit == -1 means allocate a new number.
 */
2992
static int ppp_create_interface(struct net *net, struct file *file, int *unit)
L
Linus Torvalds 已提交
2993
{
2994 2995 2996
	struct ppp_config conf = {
		.file = file,
		.unit = *unit,
2997
		.ifname_is_set = false,
2998 2999
	};
	struct net_device *dev;
L
Linus Torvalds 已提交
3000
	struct ppp *ppp;
3001
	int err;
L
Linus Torvalds 已提交
3002

3003
	dev = alloc_netdev(sizeof(struct ppp), "", NET_NAME_ENUM, ppp_setup);
3004 3005 3006 3007
	if (!dev) {
		err = -ENOMEM;
		goto err;
	}
3008
	dev_net_set(dev, net);
3009
	dev->rtnl_link_ops = &ppp_link_ops;
3010

3011
	rtnl_lock();
L
Linus Torvalds 已提交
3012

3013 3014 3015 3016 3017
	err = ppp_dev_configure(net, dev, &conf);
	if (err < 0)
		goto err_dev;
	ppp = netdev_priv(dev);
	*unit = ppp->file.index;
3018

3019
	rtnl_unlock();
3020

3021
	return 0;
L
Linus Torvalds 已提交
3022

3023
err_dev:
3024
	rtnl_unlock();
L
Linus Torvalds 已提交
3025
	free_netdev(dev);
3026 3027
err:
	return err;
L
Linus Torvalds 已提交
3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038
}

/*
 * Initialize a ppp_file structure.
 */
static void
init_ppp_file(struct ppp_file *pf, int kind)
{
	pf->kind = kind;
	skb_queue_head_init(&pf->xq);
	skb_queue_head_init(&pf->rq);
3039
	refcount_set(&pf->refcnt, 1);
L
Linus Torvalds 已提交
3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053
	init_waitqueue_head(&pf->rwait);
}

/*
 * Free the memory used by a ppp unit.  This is only called once
 * there are no channels connected to the unit and no file structs
 * that reference the unit.
 */
static void ppp_destroy_interface(struct ppp *ppp)
{
	atomic_dec(&ppp_unit_count);

	if (!ppp->file.dead || ppp->n_channels) {
		/* "can't happen" */
3054 3055 3056
		netdev_err(ppp->dev, "ppp: destroying ppp struct %p "
			   "but dead=%d n_channels=%d !\n",
			   ppp, ppp->file.dead, ppp->n_channels);
L
Linus Torvalds 已提交
3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070
		return;
	}

	ppp_ccp_closed(ppp);
	if (ppp->vj) {
		slhc_free(ppp->vj);
		ppp->vj = NULL;
	}
	skb_queue_purge(&ppp->file.xq);
	skb_queue_purge(&ppp->file.rq);
#ifdef CONFIG_PPP_MULTILINK
	skb_queue_purge(&ppp->mrq);
#endif /* CONFIG_PPP_MULTILINK */
#ifdef CONFIG_PPP_FILTER
3071
	if (ppp->pass_filter) {
3072
		bpf_prog_destroy(ppp->pass_filter);
3073 3074 3075 3076
		ppp->pass_filter = NULL;
	}

	if (ppp->active_filter) {
3077
		bpf_prog_destroy(ppp->active_filter);
3078 3079
		ppp->active_filter = NULL;
	}
L
Linus Torvalds 已提交
3080 3081
#endif /* CONFIG_PPP_FILTER */

3082
	kfree_skb(ppp->xmit_pending);
3083
	free_percpu(ppp->xmit_recursion);
3084

3085
	free_netdev(ppp->dev);
L
Linus Torvalds 已提交
3086 3087 3088 3089
}

/*
 * Locate an existing ppp unit.
3090
 * The caller should have locked the all_ppp_mutex.
L
Linus Torvalds 已提交
3091 3092
 */
static struct ppp *
3093
ppp_find_unit(struct ppp_net *pn, int unit)
L
Linus Torvalds 已提交
3094
{
3095
	return unit_find(&pn->units_idr, unit);
L
Linus Torvalds 已提交
3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106
}

/*
 * Locate an existing ppp channel.
 * The caller should have locked the all_channels_lock.
 * First we look in the new_channels list, then in the
 * all_channels list.  If found in the new_channels list,
 * we move it to the all_channels list.  This is for speed
 * when we have a lot of channels in use.
 */
static struct channel *
3107
ppp_find_channel(struct ppp_net *pn, int unit)
L
Linus Torvalds 已提交
3108 3109 3110
{
	struct channel *pch;

3111
	list_for_each_entry(pch, &pn->new_channels, list) {
L
Linus Torvalds 已提交
3112
		if (pch->file.index == unit) {
3113
			list_move(&pch->list, &pn->all_channels);
L
Linus Torvalds 已提交
3114 3115 3116
			return pch;
		}
	}
3117 3118

	list_for_each_entry(pch, &pn->all_channels, list) {
L
Linus Torvalds 已提交
3119 3120 3121
		if (pch->file.index == unit)
			return pch;
	}
3122

L
Linus Torvalds 已提交
3123 3124 3125 3126 3127 3128 3129 3130 3131 3132
	return NULL;
}

/*
 * Connect a PPP channel to a PPP interface unit.
 */
static int
ppp_connect_channel(struct channel *pch, int unit)
{
	struct ppp *ppp;
3133
	struct ppp_net *pn;
L
Linus Torvalds 已提交
3134 3135 3136
	int ret = -ENXIO;
	int hdrlen;

3137 3138 3139 3140
	pn = ppp_pernet(pch->chan_net);

	mutex_lock(&pn->all_ppp_mutex);
	ppp = ppp_find_unit(pn, unit);
J
Joe Perches 已提交
3141
	if (!ppp)
L
Linus Torvalds 已提交
3142 3143 3144
		goto out;
	write_lock_bh(&pch->upl);
	ret = -EINVAL;
J
Joe Perches 已提交
3145
	if (pch->ppp)
L
Linus Torvalds 已提交
3146 3147 3148
		goto outl;

	ppp_lock(ppp);
3149 3150 3151 3152 3153 3154 3155 3156 3157
	spin_lock_bh(&pch->downl);
	if (!pch->chan) {
		/* Don't connect unregistered channels */
		spin_unlock_bh(&pch->downl);
		ppp_unlock(ppp);
		ret = -ENOTCONN;
		goto outl;
	}
	spin_unlock_bh(&pch->downl);
L
Linus Torvalds 已提交
3158 3159 3160
	if (pch->file.hdrlen > ppp->file.hdrlen)
		ppp->file.hdrlen = pch->file.hdrlen;
	hdrlen = pch->file.hdrlen + 2;	/* for protocol bytes */
3161
	if (hdrlen > ppp->dev->hard_header_len)
L
Linus Torvalds 已提交
3162 3163 3164 3165
		ppp->dev->hard_header_len = hdrlen;
	list_add_tail(&pch->clist, &ppp->channels);
	++ppp->n_channels;
	pch->ppp = ppp;
3166
	refcount_inc(&ppp->file.refcnt);
L
Linus Torvalds 已提交
3167 3168 3169 3170 3171 3172
	ppp_unlock(ppp);
	ret = 0;

 outl:
	write_unlock_bh(&pch->upl);
 out:
3173
	mutex_unlock(&pn->all_ppp_mutex);
L
Linus Torvalds 已提交
3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189
	return ret;
}

/*
 * Disconnect a channel from its ppp unit.
 */
static int
ppp_disconnect_channel(struct channel *pch)
{
	struct ppp *ppp;
	int err = -EINVAL;

	write_lock_bh(&pch->upl);
	ppp = pch->ppp;
	pch->ppp = NULL;
	write_unlock_bh(&pch->upl);
J
Joe Perches 已提交
3190
	if (ppp) {
L
Linus Torvalds 已提交
3191 3192 3193 3194 3195 3196
		/* remove it from the ppp unit's list */
		ppp_lock(ppp);
		list_del(&pch->clist);
		if (--ppp->n_channels == 0)
			wake_up_interruptible(&ppp->file.rwait);
		ppp_unlock(ppp);
3197
		if (refcount_dec_and_test(&ppp->file.refcnt))
L
Linus Torvalds 已提交
3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208
			ppp_destroy_interface(ppp);
		err = 0;
	}
	return err;
}

/*
 * Free up the resources used by a ppp channel.
 */
static void ppp_destroy_channel(struct channel *pch)
{
3209 3210 3211
	put_net(pch->chan_net);
	pch->chan_net = NULL;

L
Linus Torvalds 已提交
3212 3213 3214 3215
	atomic_dec(&channel_count);

	if (!pch->file.dead) {
		/* "can't happen" */
3216
		pr_err("ppp: destroying undead channel %p !\n", pch);
L
Linus Torvalds 已提交
3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227
		return;
	}
	skb_queue_purge(&pch->file.xq);
	skb_queue_purge(&pch->file.rq);
	kfree(pch);
}

static void __exit ppp_cleanup(void)
{
	/* should never happen */
	if (atomic_read(&ppp_unit_count) || atomic_read(&channel_count))
3228
		pr_err("PPP: removing module but units remain!\n");
3229
	rtnl_link_unregister(&ppp_link_ops);
3230
	unregister_chrdev(PPP_MAJOR, "ppp");
3231
	device_destroy(ppp_class, MKDEV(PPP_MAJOR, 0));
3232
	class_destroy(ppp_class);
3233
	unregister_pernet_device(&ppp_net_ops);
L
Linus Torvalds 已提交
3234 3235 3236
}

/*
3237 3238
 * Units handling. Caller must protect concurrent access
 * by holding all_ppp_mutex
L
Linus Torvalds 已提交
3239
 */
3240

3241 3242 3243 3244
/* associate pointer with specified number */
static int unit_set(struct idr *p, void *ptr, int n)
{
	int unit;
3245

T
Tejun Heo 已提交
3246 3247 3248
	unit = idr_alloc(p, ptr, n, n + 1, GFP_KERNEL);
	if (unit == -ENOSPC)
		unit = -EINVAL;
3249 3250 3251
	return unit;
}

3252 3253
/* get new free unit number and associate pointer with it */
static int unit_get(struct idr *p, void *ptr)
L
Linus Torvalds 已提交
3254
{
T
Tejun Heo 已提交
3255
	return idr_alloc(p, ptr, 0, 0, GFP_KERNEL);
L
Linus Torvalds 已提交
3256 3257
}

3258 3259
/* put unit number back to a pool */
static void unit_put(struct idr *p, int n)
L
Linus Torvalds 已提交
3260
{
3261
	idr_remove(p, n);
L
Linus Torvalds 已提交
3262 3263
}

3264 3265
/* get pointer associated with the number */
static void *unit_find(struct idr *p, int n)
L
Linus Torvalds 已提交
3266
{
3267
	return idr_find(p, n);
L
Linus Torvalds 已提交
3268 3269 3270 3271 3272 3273 3274
}

/* Module/initialization stuff */

module_init(ppp_init);
module_exit(ppp_cleanup);

3275
EXPORT_SYMBOL(ppp_register_net_channel);
L
Linus Torvalds 已提交
3276 3277 3278 3279
EXPORT_SYMBOL(ppp_register_channel);
EXPORT_SYMBOL(ppp_unregister_channel);
EXPORT_SYMBOL(ppp_channel_index);
EXPORT_SYMBOL(ppp_unit_number);
3280
EXPORT_SYMBOL(ppp_dev_name);
L
Linus Torvalds 已提交
3281 3282 3283 3284 3285 3286
EXPORT_SYMBOL(ppp_input);
EXPORT_SYMBOL(ppp_input_error);
EXPORT_SYMBOL(ppp_output_wakeup);
EXPORT_SYMBOL(ppp_register_compressor);
EXPORT_SYMBOL(ppp_unregister_compressor);
MODULE_LICENSE("GPL");
3287
MODULE_ALIAS_CHARDEV(PPP_MAJOR, 0);
3288
MODULE_ALIAS_RTNL_LINK("ppp");
3289
MODULE_ALIAS("devname:ppp");