pppoe.c 27.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9
/** -*- linux-c -*- ***********************************************************
 * Linux PPP over Ethernet (PPPoX/PPPoE) Sockets
 *
 * PPPoX --- Generic PPP encapsulation socket family
 * PPPoE --- PPP over Ethernet (RFC 2516)
 *
 *
 * Version:	0.7.0
 *
10 11 12 13 14 15
 * 070228 :	Fix to allow multiple sessions with same remote MAC and same
 *		session id by including the local device ifindex in the
 *		tuple identifying a session. This also ensures packets can't
 *		be injected into a session from interfaces other than the one
 *		specified by userspace. Florian Zumbiehl <florz@florz.de>
 *		(Oh, BTW, this one is YYMMDD, in case you were wondering ...)
L
Linus Torvalds 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
 * 220102 :	Fix module use count on failure in pppoe_create, pppox_sk -acme
 * 030700 :	Fixed connect logic to allow for disconnect.
 * 270700 :	Fixed potential SMP problems; we must protect against
 *		simultaneous invocation of ppp_input
 *		and ppp_unregister_channel.
 * 040800 :	Respect reference count mechanisms on net-devices.
 * 200800 :	fix kfree(skb) in pppoe_rcv (acme)
 *		Module reference count is decremented in the right spot now,
 *		guards against sock_put not actually freeing the sk
 *		in pppoe_release.
 * 051000 :	Initialization cleanup.
 * 111100 :	Fix recvmsg.
 * 050101 :	Fix PADT procesing.
 * 140501 :	Use pppoe_rcv_core to handle all backlog. (Alexey)
 * 170701 :	Do not lock_sock with rwlock held. (DaveM)
 *		Ignore discovery frames if user has socket
 *		locked. (DaveM)
 *		Ignore return value of dev_queue_xmit in __pppoe_xmit
 *		or else we may kfree an SKB twice. (DaveM)
 * 190701 :	When doing copies of skb's in __pppoe_xmit, always delete
 *		the original skb that was passed in on success, never on
 *		failure.  Delete the copy of the skb on failure to avoid
 *		a memory leak.
 * 081001 :	Misc. cleanup (licence string, non-blocking, prevent
 *		reference of device on close).
 * 121301 :	New ppp channels interface; cannot unregister a channel
 *		from interrupts.  Thus, we mark the socket as a ZOMBIE
 *		and do the unregistration later.
 * 081002 :	seq_file support for proc stuff -acme
 * 111602 :	Merge all 2.4 fixes into 2.5/2.6 tree.  Label 2.5/2.6
 *		as version 0.7.  Spacing cleanup.
 * Author:	Michal Ostrowski <mostrows@speakeasy.net>
 * Contributors:
 * 		Arnaldo Carvalho de Melo <acme@conectiva.com.br>
 *		David S. Miller (davem@redhat.com)
 *
 * License:
 *		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.
 *
 */

#include <linux/string.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/netdevice.h>
#include <linux/net.h>
#include <linux/inetdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/init.h>
#include <linux/if_ether.h>
#include <linux/if_pppox.h>
#include <linux/ppp_channel.h>
#include <linux/ppp_defs.h>
75
#include <linux/ppp-ioctl.h>
L
Linus Torvalds 已提交
76 77 78 79 80
#include <linux/notifier.h>
#include <linux/file.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>

81
#include <linux/nsproxy.h>
82
#include <net/net_namespace.h>
83
#include <net/netns/generic.h>
L
Linus Torvalds 已提交
84 85 86 87 88
#include <net/sock.h>

#include <asm/uaccess.h>

#define PPPOE_HASH_BITS 4
89 90
#define PPPOE_HASH_SIZE (1 << PPPOE_HASH_BITS)
#define PPPOE_HASH_MASK	(PPPOE_HASH_SIZE - 1)
L
Linus Torvalds 已提交
91 92 93

static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb);

94
static const struct proto_ops pppoe_ops;
S
stephen hemminger 已提交
95
static const struct ppp_channel_ops pppoe_chan_ops;
96 97

/* per-net private data for this module */
98
static int pppoe_net_id __read_mostly;
99 100 101 102 103 104 105 106 107 108 109 110 111
struct pppoe_net {
	/*
	 * we could use _single_ hash table for all
	 * nets by injecting net id into the hash but
	 * it would increase hash chains and add
	 * a few additional math comparations messy
	 * as well, moreover in case of SMP less locking
	 * controversy here
	 */
	struct pppox_sock *hash_table[PPPOE_HASH_SIZE];
	rwlock_t hash_lock;
};

112 113 114 115 116 117
/*
 * PPPoE could be in the following stages:
 * 1) Discovery stage (to obtain remote MAC and Session ID)
 * 2) Session stage (MAC and SID are known)
 *
 * Ethernet frames have a special tag for this but
L
Lucas De Marchi 已提交
118
 * we use simpler approach based on session id
119 120 121 122 123
 */
static inline bool stage_session(__be16 sid)
{
	return sid != 0;
}
L
Linus Torvalds 已提交
124

125 126 127 128 129 130 131
static inline struct pppoe_net *pppoe_pernet(struct net *net)
{
	BUG_ON(!net);

	return net_generic(net, pppoe_net_id);
}

L
Linus Torvalds 已提交
132 133
static inline int cmp_2_addr(struct pppoe_addr *a, struct pppoe_addr *b)
{
134
	return a->sid == b->sid && ether_addr_equal(a->remote, b->remote);
L
Linus Torvalds 已提交
135 136
}

A
Al Viro 已提交
137
static inline int cmp_addr(struct pppoe_addr *a, __be16 sid, char *addr)
L
Linus Torvalds 已提交
138
{
139
	return a->sid == sid && ether_addr_equal(a->remote, addr);
L
Linus Torvalds 已提交
140 141
}

142
#if 8 % PPPOE_HASH_BITS
143 144 145
#error 8 must be a multiple of PPPOE_HASH_BITS
#endif

A
Al Viro 已提交
146
static int hash_item(__be16 sid, unsigned char *addr)
L
Linus Torvalds 已提交
147
{
148 149
	unsigned char hash = 0;
	unsigned int i;
L
Linus Torvalds 已提交
150

151
	for (i = 0; i < ETH_ALEN; i++)
152
		hash ^= addr[i];
153 154 155 156
	for (i = 0; i < sizeof(sid_t) * 8; i += 8)
		hash ^= (__force __u32)sid >> i;
	for (i = 8; (i >>= 1) >= PPPOE_HASH_BITS;)
		hash ^= hash >> i;
L
Linus Torvalds 已提交
157

158
	return hash & PPPOE_HASH_MASK;
L
Linus Torvalds 已提交
159 160 161 162 163 164 165
}

/**********************************************************************
 *
 *  Set/get/delete/rehash items  (internal versions)
 *
 **********************************************************************/
166 167
static struct pppox_sock *__get_item(struct pppoe_net *pn, __be16 sid,
				unsigned char *addr, int ifindex)
L
Linus Torvalds 已提交
168 169 170 171
{
	int hash = hash_item(sid, addr);
	struct pppox_sock *ret;

172
	ret = pn->hash_table[hash];
173 174 175 176 177
	while (ret) {
		if (cmp_addr(&ret->pppoe_pa, sid, addr) &&
		    ret->pppoe_ifindex == ifindex)
			return ret;

L
Linus Torvalds 已提交
178
		ret = ret->next;
179
	}
L
Linus Torvalds 已提交
180

181
	return NULL;
L
Linus Torvalds 已提交
182 183
}

184
static int __set_item(struct pppoe_net *pn, struct pppox_sock *po)
L
Linus Torvalds 已提交
185 186 187 188
{
	int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
	struct pppox_sock *ret;

189
	ret = pn->hash_table[hash];
L
Linus Torvalds 已提交
190
	while (ret) {
191 192
		if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa) &&
		    ret->pppoe_ifindex == po->pppoe_ifindex)
L
Linus Torvalds 已提交
193 194 195 196 197
			return -EALREADY;

		ret = ret->next;
	}

198 199
	po->next = pn->hash_table[hash];
	pn->hash_table[hash] = po;
L
Linus Torvalds 已提交
200 201 202 203

	return 0;
}

204
static void __delete_item(struct pppoe_net *pn, __be16 sid,
205
					char *addr, int ifindex)
L
Linus Torvalds 已提交
206 207 208 209
{
	int hash = hash_item(sid, addr);
	struct pppox_sock *ret, **src;

210 211
	ret = pn->hash_table[hash];
	src = &pn->hash_table[hash];
L
Linus Torvalds 已提交
212 213

	while (ret) {
214 215
		if (cmp_addr(&ret->pppoe_pa, sid, addr) &&
		    ret->pppoe_ifindex == ifindex) {
L
Linus Torvalds 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229
			*src = ret->next;
			break;
		}

		src = &ret->next;
		ret = ret->next;
	}
}

/**********************************************************************
 *
 *  Set/get/delete/rehash items
 *
 **********************************************************************/
230 231
static inline struct pppox_sock *get_item(struct pppoe_net *pn, __be16 sid,
					unsigned char *addr, int ifindex)
L
Linus Torvalds 已提交
232 233 234
{
	struct pppox_sock *po;

235 236
	read_lock_bh(&pn->hash_lock);
	po = __get_item(pn, sid, addr, ifindex);
L
Linus Torvalds 已提交
237 238
	if (po)
		sock_hold(sk_pppox(po));
239
	read_unlock_bh(&pn->hash_lock);
L
Linus Torvalds 已提交
240 241 242 243

	return po;
}

244 245
static inline struct pppox_sock *get_item_by_addr(struct net *net,
						struct sockaddr_pppox *sp)
L
Linus Torvalds 已提交
246
{
247
	struct net_device *dev;
248
	struct pppoe_net *pn;
249
	struct pppox_sock *pppox_sock = NULL;
250

251 252
	int ifindex;

253 254 255 256
	rcu_read_lock();
	dev = dev_get_by_name_rcu(net, sp->sa_addr.pppoe.dev);
	if (dev) {
		ifindex = dev->ifindex;
257
		pn = pppoe_pernet(net);
258
		pppox_sock = get_item(pn, sp->sa_addr.pppoe.sid,
259
				sp->sa_addr.pppoe.remote, ifindex);
260 261
	}
	rcu_read_unlock();
262
	return pppox_sock;
L
Linus Torvalds 已提交
263 264
}

265
static inline void delete_item(struct pppoe_net *pn, __be16 sid,
266
					char *addr, int ifindex)
L
Linus Torvalds 已提交
267
{
268
	write_lock_bh(&pn->hash_lock);
269
	__delete_item(pn, sid, addr, ifindex);
270
	write_unlock_bh(&pn->hash_lock);
L
Linus Torvalds 已提交
271 272 273 274 275 276 277 278 279 280 281
}

/***************************************************************************
 *
 *  Handler for device events.
 *  Certain device events require that sockets be unconnected.
 *
 **************************************************************************/

static void pppoe_flush_dev(struct net_device *dev)
{
282 283 284
	struct pppoe_net *pn;
	int i;

285
	pn = pppoe_pernet(dev_net(dev));
286 287 288
	write_lock_bh(&pn->hash_lock);
	for (i = 0; i < PPPOE_HASH_SIZE; i++) {
		struct pppox_sock *po = pn->hash_table[i];
M
Michal Ostrowski 已提交
289
		struct sock *sk;
L
Linus Torvalds 已提交
290

M
Michal Ostrowski 已提交
291 292
		while (po) {
			while (po && po->pppoe_dev != dev) {
293 294
				po = po->next;
			}
M
Michal Ostrowski 已提交
295 296 297 298

			if (!po)
				break;

299
			sk = sk_pppox(po);
L
Linus Torvalds 已提交
300

301
			/* We always grab the socket lock, followed by the
M
Michal Ostrowski 已提交
302 303 304 305 306
			 * hash_lock, in that order.  Since we should hold the
			 * sock lock while doing any unbinding, we need to
			 * release the lock we're holding.  Hold a reference to
			 * the sock so it doesn't disappear as we're jumping
			 * between locks.
307
			 */
L
Linus Torvalds 已提交
308

309
			sock_hold(sk);
310
			write_unlock_bh(&pn->hash_lock);
311
			lock_sock(sk);
L
Linus Torvalds 已提交
312

313
			if (po->pppoe_dev == dev &&
314
			    sk->sk_state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
315 316
				pppox_unbind_sock(sk);
				sk->sk_state_change(sk);
M
Michal Ostrowski 已提交
317 318
				po->pppoe_dev = NULL;
				dev_put(dev);
319
			}
L
Linus Torvalds 已提交
320

321 322
			release_sock(sk);
			sock_put(sk);
L
Linus Torvalds 已提交
323

M
Michal Ostrowski 已提交
324 325 326
			/* Restart the process from the start of the current
			 * hash chain. We dropped locks so the world may have
			 * change from underneath us.
327
			 */
M
Michal Ostrowski 已提交
328 329

			BUG_ON(pppoe_pernet(dev_net(dev)) == NULL);
330 331
			write_lock_bh(&pn->hash_lock);
			po = pn->hash_table[i];
L
Linus Torvalds 已提交
332 333
		}
	}
334
	write_unlock_bh(&pn->hash_lock);
L
Linus Torvalds 已提交
335 336 337 338 339
}

static int pppoe_device_event(struct notifier_block *this,
			      unsigned long event, void *ptr)
{
340
	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
341

L
Linus Torvalds 已提交
342 343
	/* Only look at sockets that are using this specific device. */
	switch (event) {
344
	case NETDEV_CHANGEADDR:
L
Linus Torvalds 已提交
345
	case NETDEV_CHANGEMTU:
346
		/* A change in mtu or address is a bad thing, requiring
L
Linus Torvalds 已提交
347 348 349 350 351 352 353 354 355 356 357
		 * LCP re-negotiation.
		 */

	case NETDEV_GOING_DOWN:
	case NETDEV_DOWN:
		/* Find every socket on this device and kill it. */
		pppoe_flush_dev(dev);
		break;

	default:
		break;
358
	}
L
Linus Torvalds 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

	return NOTIFY_DONE;
}

static struct notifier_block pppoe_notifier = {
	.notifier_call = pppoe_device_event,
};

/************************************************************************
 *
 * Do the real work of receiving a PPPoE Session frame.
 *
 ***********************************************************************/
static int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb)
{
	struct pppox_sock *po = pppox_sk(sk);
375
	struct pppox_sock *relay_po;
L
Linus Torvalds 已提交
376

M
Michal Ostrowski 已提交
377 378 379 380 381
	/* Backlog receive. Semantics of backlog rcv preclude any code from
	 * executing in lock_sock()/release_sock() bounds; meaning sk->sk_state
	 * can't change.
	 */

382 383 384
	if (skb->pkt_type == PACKET_OTHERHOST)
		goto abort_kfree;

L
Linus Torvalds 已提交
385 386 387
	if (sk->sk_state & PPPOX_BOUND) {
		ppp_input(&po->chan, skb);
	} else if (sk->sk_state & PPPOX_RELAY) {
M
Michal Ostrowski 已提交
388 389
		relay_po = get_item_by_addr(sock_net(sk),
					    &po->pppoe_relay);
L
Linus Torvalds 已提交
390 391 392 393 394 395 396 397
		if (relay_po == NULL)
			goto abort_kfree;

		if ((sk_pppox(relay_po)->sk_state & PPPOX_CONNECTED) == 0)
			goto abort_put;

		if (!__pppoe_xmit(sk_pppox(relay_po), skb))
			goto abort_put;
398 399

		sock_put(sk_pppox(relay_po));
L
Linus Torvalds 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
	} else {
		if (sock_queue_rcv_skb(sk, skb))
			goto abort_kfree;
	}

	return NET_RX_SUCCESS;

abort_put:
	sock_put(sk_pppox(relay_po));

abort_kfree:
	kfree_skb(skb);
	return NET_RX_DROP;
}

/************************************************************************
 *
 * Receive wrapper called in BH context.
 *
 ***********************************************************************/
420 421
static int pppoe_rcv(struct sk_buff *skb, struct net_device *dev,
		     struct packet_type *pt, struct net_device *orig_dev)
L
Linus Torvalds 已提交
422 423 424
{
	struct pppoe_hdr *ph;
	struct pppox_sock *po;
425
	struct pppoe_net *pn;
426
	int len;
L
Linus Torvalds 已提交
427

428 429
	skb = skb_share_check(skb, GFP_ATOMIC);
	if (!skb)
L
Linus Torvalds 已提交
430 431
		goto out;

432 433 434
	if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
		goto drop;

435
	ph = pppoe_hdr(skb);
436 437 438 439 440
	len = ntohs(ph->length);

	skb_pull_rcsum(skb, sizeof(*ph));
	if (skb->len < len)
		goto drop;
L
Linus Torvalds 已提交
441

D
David S. Miller 已提交
442
	if (pskb_trim_rcsum(skb, len))
443 444
		goto drop;

445
	pn = pppoe_pernet(dev_net(dev));
M
Michal Ostrowski 已提交
446 447 448 449

	/* Note that get_item does a sock_hold(), so sk_pppox(po)
	 * is known to be safe.
	 */
450
	po = get_item(pn, ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
D
David S. Miller 已提交
451
	if (!po)
452 453 454 455
		goto drop;

	return sk_receive_skb(sk_pppox(po), skb, 0);

L
Linus Torvalds 已提交
456 457 458 459 460 461
drop:
	kfree_skb(skb);
out:
	return NET_RX_DROP;
}

462 463 464 465 466 467 468
static void pppoe_unbind_sock_work(struct work_struct *work)
{
	struct pppox_sock *po = container_of(work, struct pppox_sock,
					     proto.pppoe.padt_work);
	struct sock *sk = sk_pppox(po);

	lock_sock(sk);
469 470 471 472
	if (po->pppoe_dev) {
		dev_put(po->pppoe_dev);
		po->pppoe_dev = NULL;
	}
473 474 475 476 477
	pppox_unbind_sock(sk);
	release_sock(sk);
	sock_put(sk);
}

L
Linus Torvalds 已提交
478 479 480 481 482 483
/************************************************************************
 *
 * Receive a PPPoE Discovery frame.
 * This is solely for detection of PADT frames
 *
 ***********************************************************************/
484 485
static int pppoe_disc_rcv(struct sk_buff *skb, struct net_device *dev,
			  struct packet_type *pt, struct net_device *orig_dev)
L
Linus Torvalds 已提交
486 487 488 489

{
	struct pppoe_hdr *ph;
	struct pppox_sock *po;
490
	struct pppoe_net *pn;
491

492 493
	skb = skb_share_check(skb, GFP_ATOMIC);
	if (!skb)
L
Linus Torvalds 已提交
494 495
		goto out;

496 497 498
	if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
		goto abort;

499
	ph = pppoe_hdr(skb);
L
Linus Torvalds 已提交
500 501 502
	if (ph->code != PADT_CODE)
		goto abort;

503 504
	pn = pppoe_pernet(dev_net(dev));
	po = get_item(pn, ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
505
	if (po)
506
		if (!schedule_work(&po->proto.pppoe.padt_work))
507
			sock_put(sk_pppox(po));
L
Linus Torvalds 已提交
508 509 510 511 512 513 514

abort:
	kfree_skb(skb);
out:
	return NET_RX_SUCCESS; /* Lies... :-) */
}

515
static struct packet_type pppoes_ptype __read_mostly = {
516
	.type	= cpu_to_be16(ETH_P_PPP_SES),
L
Linus Torvalds 已提交
517 518 519
	.func	= pppoe_rcv,
};

520
static struct packet_type pppoed_ptype __read_mostly = {
521
	.type	= cpu_to_be16(ETH_P_PPP_DISC),
L
Linus Torvalds 已提交
522 523 524
	.func	= pppoe_disc_rcv,
};

525
static struct proto pppoe_sk_proto __read_mostly = {
L
Linus Torvalds 已提交
526 527 528 529 530 531 532 533 534 535
	.name	  = "PPPOE",
	.owner	  = THIS_MODULE,
	.obj_size = sizeof(struct pppox_sock),
};

/***********************************************************************
 *
 * Initialize a new struct sock.
 *
 **********************************************************************/
536
static int pppoe_create(struct net *net, struct socket *sock, int kern)
L
Linus Torvalds 已提交
537 538 539
{
	struct sock *sk;

540
	sk = sk_alloc(net, PF_PPPOX, GFP_KERNEL, &pppoe_sk_proto, kern);
L
Linus Torvalds 已提交
541
	if (!sk)
542
		return -ENOMEM;
L
Linus Torvalds 已提交
543 544 545

	sock_init_data(sock, sk);

546 547
	sock->state	= SS_UNCONNECTED;
	sock->ops	= &pppoe_ops;
L
Linus Torvalds 已提交
548

549 550 551 552 553
	sk->sk_backlog_rcv	= pppoe_rcv_core;
	sk->sk_state		= PPPOX_NONE;
	sk->sk_type		= SOCK_STREAM;
	sk->sk_family		= PF_PPPOX;
	sk->sk_protocol		= PX_PROTO_OE;
L
Linus Torvalds 已提交
554

555 556 557
	INIT_WORK(&pppox_sk(sk)->proto.pppoe.padt_work,
		  pppoe_unbind_sock_work);

558
	return 0;
L
Linus Torvalds 已提交
559 560 561 562 563 564
}

static int pppoe_release(struct socket *sock)
{
	struct sock *sk = sock->sk;
	struct pppox_sock *po;
565
	struct pppoe_net *pn;
M
Michal Ostrowski 已提交
566
	struct net *net = NULL;
L
Linus Torvalds 已提交
567 568 569 570

	if (!sk)
		return 0;

571
	lock_sock(sk);
572
	if (sock_flag(sk, SOCK_DEAD)) {
573
		release_sock(sk);
L
Linus Torvalds 已提交
574
		return -EBADF;
575
	}
L
Linus Torvalds 已提交
576

M
Michal Ostrowski 已提交
577 578
	po = pppox_sk(sk);

579
	if (po->pppoe_dev) {
M
Michal Ostrowski 已提交
580 581 582 583
		dev_put(po->pppoe_dev);
		po->pppoe_dev = NULL;
	}

L
Linus Torvalds 已提交
584 585 586 587 588
	pppox_unbind_sock(sk);

	/* Signal the death of the socket. */
	sk->sk_state = PPPOX_DEAD;

M
Michal Ostrowski 已提交
589 590
	net = sock_net(sk);
	pn = pppoe_pernet(net);
591

592 593 594 595
	/*
	 * protect "po" from concurrent updates
	 * on pppoe_flush_dev
	 */
M
Michal Ostrowski 已提交
596 597
	delete_item(pn, po->pppoe_pa.sid, po->pppoe_pa.remote,
		    po->pppoe_ifindex);
598

L
Linus Torvalds 已提交
599 600 601 602
	sock_orphan(sk);
	sock->sk = NULL;

	skb_queue_purge(&sk->sk_receive_queue);
603
	release_sock(sk);
L
Linus Torvalds 已提交
604 605
	sock_put(sk);

606
	return 0;
L
Linus Torvalds 已提交
607 608 609 610 611 612
}

static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
		  int sockaddr_len, int flags)
{
	struct sock *sk = sock->sk;
613
	struct sockaddr_pppox *sp = (struct sockaddr_pppox *)uservaddr;
L
Linus Torvalds 已提交
614
	struct pppox_sock *po = pppox_sk(sk);
M
Michal Ostrowski 已提交
615
	struct net_device *dev = NULL;
616
	struct pppoe_net *pn;
M
Michal Ostrowski 已提交
617
	struct net *net = NULL;
L
Linus Torvalds 已提交
618 619 620 621 622 623 624 625 626 627
	int error;

	lock_sock(sk);

	error = -EINVAL;
	if (sp->sa_protocol != PX_PROTO_OE)
		goto end;

	/* Check for already bound sockets */
	error = -EBUSY;
628 629
	if ((sk->sk_state & PPPOX_CONNECTED) &&
	     stage_session(sp->sa_addr.pppoe.sid))
L
Linus Torvalds 已提交
630 631 632 633
		goto end;

	/* Check for already disconnected sockets, on attempts to disconnect */
	error = -EALREADY;
634 635
	if ((sk->sk_state & PPPOX_DEAD) &&
	     !stage_session(sp->sa_addr.pppoe.sid))
L
Linus Torvalds 已提交
636 637 638 639
		goto end;

	error = 0;

640 641 642
	/* Delete the old binding */
	if (stage_session(po->pppoe_pa.sid)) {
		pppox_unbind_sock(sk);
M
Michal Ostrowski 已提交
643 644 645
		pn = pppoe_pernet(sock_net(sk));
		delete_item(pn, po->pppoe_pa.sid,
			    po->pppoe_pa.remote, po->pppoe_ifindex);
646
		if (po->pppoe_dev) {
L
Linus Torvalds 已提交
647
			dev_put(po->pppoe_dev);
M
Michal Ostrowski 已提交
648
			po->pppoe_dev = NULL;
649
		}
M
Michal Ostrowski 已提交
650

651 652 653 654 655 656 657
		po->pppoe_ifindex = 0;
		memset(&po->pppoe_pa, 0, sizeof(po->pppoe_pa));
		memset(&po->pppoe_relay, 0, sizeof(po->pppoe_relay));
		memset(&po->chan, 0, sizeof(po->chan));
		po->next = NULL;
		po->num = 0;

L
Linus Torvalds 已提交
658 659 660
		sk->sk_state = PPPOX_NONE;
	}

661 662
	/* Re-bind in session stage only */
	if (stage_session(sp->sa_addr.pppoe.sid)) {
L
Linus Torvalds 已提交
663
		error = -ENODEV;
M
Michal Ostrowski 已提交
664 665
		net = sock_net(sk);
		dev = dev_get_by_name(net, sp->sa_addr.pppoe.dev);
L
Linus Torvalds 已提交
666
		if (!dev)
M
Michal Ostrowski 已提交
667
			goto err_put;
L
Linus Torvalds 已提交
668 669

		po->pppoe_dev = dev;
670
		po->pppoe_ifindex = dev->ifindex;
M
Michal Ostrowski 已提交
671
		pn = pppoe_pernet(net);
672
		if (!(dev->flags & IFF_UP)) {
L
Linus Torvalds 已提交
673
			goto err_put;
674
		}
L
Linus Torvalds 已提交
675 676 677 678 679

		memcpy(&po->pppoe_pa,
		       &sp->sa_addr.pppoe,
		       sizeof(struct pppoe_addr));

M
Michal Ostrowski 已提交
680
		write_lock_bh(&pn->hash_lock);
681 682
		error = __set_item(pn, po);
		write_unlock_bh(&pn->hash_lock);
L
Linus Torvalds 已提交
683 684 685 686 687 688
		if (error < 0)
			goto err_put;

		po->chan.hdrlen = (sizeof(struct pppoe_hdr) +
				   dev->hard_header_len);

689
		po->chan.mtu = dev->mtu - sizeof(struct pppoe_hdr) - 2;
L
Linus Torvalds 已提交
690 691 692
		po->chan.private = sk;
		po->chan.ops = &pppoe_chan_ops;

693
		error = ppp_register_net_channel(dev_net(dev), &po->chan);
M
Michal Ostrowski 已提交
694 695 696
		if (error) {
			delete_item(pn, po->pppoe_pa.sid,
				    po->pppoe_pa.remote, po->pppoe_ifindex);
L
Linus Torvalds 已提交
697
			goto err_put;
M
Michal Ostrowski 已提交
698
		}
L
Linus Torvalds 已提交
699 700 701 702 703 704

		sk->sk_state = PPPOX_CONNECTED;
	}

	po->num = sp->sa_addr.pppoe.sid;

705
end:
L
Linus Torvalds 已提交
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
	release_sock(sk);
	return error;
err_put:
	if (po->pppoe_dev) {
		dev_put(po->pppoe_dev);
		po->pppoe_dev = NULL;
	}
	goto end;
}

static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
		  int *usockaddr_len, int peer)
{
	int len = sizeof(struct sockaddr_pppox);
	struct sockaddr_pppox sp;

	sp.sa_family	= AF_PPPOX;
	sp.sa_protocol	= PX_PROTO_OE;
	memcpy(&sp.sa_addr.pppoe, &pppox_sk(sock->sk)->pppoe_pa,
	       sizeof(struct pppoe_addr));

	memcpy(uaddr, &sp, len);

	*usockaddr_len = len;

	return 0;
}

static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
		unsigned long arg)
{
	struct sock *sk = sock->sk;
	struct pppox_sock *po = pppox_sk(sk);
739 740
	int val;
	int err;
L
Linus Torvalds 已提交
741 742 743 744 745 746 747 748 749 750 751

	switch (cmd) {
	case PPPIOCGMRU:
		err = -ENXIO;
		if (!(sk->sk_state & PPPOX_CONNECTED))
			break;

		err = -EFAULT;
		if (put_user(po->pppoe_dev->mtu -
			     sizeof(struct pppoe_hdr) -
			     PPP_HDRLEN,
752
			     (int __user *)arg))
L
Linus Torvalds 已提交
753 754 755 756 757 758 759 760 761 762
			break;
		err = 0;
		break;

	case PPPIOCSMRU:
		err = -ENXIO;
		if (!(sk->sk_state & PPPOX_CONNECTED))
			break;

		err = -EFAULT;
763
		if (get_user(val, (int __user *)arg))
L
Linus Torvalds 已提交
764 765 766 767 768 769 770 771 772 773 774 775
			break;

		if (val < (po->pppoe_dev->mtu
			   - sizeof(struct pppoe_hdr)
			   - PPP_HDRLEN))
			err = 0;
		else
			err = -EINVAL;
		break;

	case PPPIOCSFLAGS:
		err = -EFAULT;
776
		if (get_user(val, (int __user *)arg))
L
Linus Torvalds 已提交
777 778 779 780 781 782 783 784 785
			break;
		err = 0;
		break;

	case PPPOEIOCSFWD:
	{
		struct pppox_sock *relay_po;

		err = -EBUSY;
786
		if (sk->sk_state & (PPPOX_BOUND | PPPOX_DEAD))
L
Linus Torvalds 已提交
787 788 789 790 791 792 793
			break;

		err = -ENOTCONN;
		if (!(sk->sk_state & PPPOX_CONNECTED))
			break;

		/* PPPoE address from the user specifies an outbound
794
		   PPPoE address which frames are forwarded to */
L
Linus Torvalds 已提交
795 796 797 798 799 800 801 802
		err = -EFAULT;
		if (copy_from_user(&po->pppoe_relay,
				   (void __user *)arg,
				   sizeof(struct sockaddr_pppox)))
			break;

		err = -EINVAL;
		if (po->pppoe_relay.sa_family != AF_PPPOX ||
803
		    po->pppoe_relay.sa_protocol != PX_PROTO_OE)
L
Linus Torvalds 已提交
804 805 806 807
			break;

		/* Check that the socket referenced by the address
		   actually exists. */
808
		relay_po = get_item_by_addr(sock_net(sk), &po->pppoe_relay);
L
Linus Torvalds 已提交
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
		if (!relay_po)
			break;

		sock_put(sk_pppox(relay_po));
		sk->sk_state |= PPPOX_RELAY;
		err = 0;
		break;
	}

	case PPPOEIOCDFWD:
		err = -EALREADY;
		if (!(sk->sk_state & PPPOX_RELAY))
			break;

		sk->sk_state &= ~PPPOX_RELAY;
		err = 0;
		break;

827 828 829
	default:
		err = -ENOTTY;
	}
L
Linus Torvalds 已提交
830 831 832 833

	return err;
}

834 835
static int pppoe_sendmsg(struct socket *sock, struct msghdr *m,
			 size_t total_len)
L
Linus Torvalds 已提交
836
{
837
	struct sk_buff *skb;
L
Linus Torvalds 已提交
838 839
	struct sock *sk = sock->sk;
	struct pppox_sock *po = pppox_sk(sk);
840
	int error;
L
Linus Torvalds 已提交
841 842 843 844 845
	struct pppoe_hdr hdr;
	struct pppoe_hdr *ph;
	struct net_device *dev;
	char *start;

846
	lock_sock(sk);
L
Linus Torvalds 已提交
847 848 849 850 851 852 853 854 855 856 857 858 859
	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
		error = -ENOTCONN;
		goto end;
	}

	hdr.ver = 1;
	hdr.type = 1;
	hdr.code = 0;
	hdr.sid = po->num;

	dev = po->pppoe_dev;

	error = -EMSGSIZE;
860
	if (total_len > (dev->mtu + dev->hard_header_len))
L
Linus Torvalds 已提交
861 862 863 864 865 866 867 868 869 870 871 872
		goto end;


	skb = sock_wmalloc(sk, total_len + dev->hard_header_len + 32,
			   0, GFP_KERNEL);
	if (!skb) {
		error = -ENOMEM;
		goto end;
	}

	/* Reserve space for headers. */
	skb_reserve(skb, dev->hard_header_len);
873
	skb_reset_network_header(skb);
L
Linus Torvalds 已提交
874 875 876 877

	skb->dev = dev;

	skb->priority = sk->sk_priority;
878
	skb->protocol = cpu_to_be16(ETH_P_PPP_SES);
L
Linus Torvalds 已提交
879

880 881
	ph = (struct pppoe_hdr *)skb_put(skb, total_len + sizeof(struct pppoe_hdr));
	start = (char *)&ph->tag[0];
L
Linus Torvalds 已提交
882

A
Al Viro 已提交
883
	error = memcpy_from_msg(start, m, total_len);
L
Linus Torvalds 已提交
884 885 886 887 888 889
	if (error < 0) {
		kfree_skb(skb);
		goto end;
	}

	error = total_len;
890 891
	dev_hard_header(skb, dev, ETH_P_PPP_SES,
			po->pppoe_pa.remote, NULL, total_len);
L
Linus Torvalds 已提交
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915

	memcpy(ph, &hdr, sizeof(struct pppoe_hdr));

	ph->length = htons(total_len);

	dev_queue_xmit(skb);

end:
	release_sock(sk);
	return error;
}

/************************************************************************
 *
 * xmit function for internal use.
 *
 ***********************************************************************/
static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
{
	struct pppox_sock *po = pppox_sk(sk);
	struct net_device *dev = po->pppoe_dev;
	struct pppoe_hdr *ph;
	int data_len = skb->len;

M
Michal Ostrowski 已提交
916 917 918 919 920 921 922 923
	/* The higher-level PPP code (ppp_unregister_channel()) ensures the PPP
	 * xmit operations conclude prior to an unregistration call.  Thus
	 * sk->sk_state cannot change, so we don't need to do lock_sock().
	 * But, we also can't do a lock_sock since that introduces a potential
	 * deadlock as we'd reverse the lock ordering used when calling
	 * ppp_unregister_channel().
	 */

L
Linus Torvalds 已提交
924 925 926 927 928 929
	if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
		goto abort;

	if (!dev)
		goto abort;

930 931 932
	/* Copy the data if there is no space for the header or if it's
	 * read-only.
	 */
H
Herbert Xu 已提交
933
	if (skb_cow_head(skb, sizeof(*ph) + dev->hard_header_len))
934
		goto abort;
L
Linus Torvalds 已提交
935

936
	__skb_push(skb, sizeof(*ph));
937
	skb_reset_network_header(skb);
L
Linus Torvalds 已提交
938

939 940 941 942 943 944 945
	ph = pppoe_hdr(skb);
	ph->ver	= 1;
	ph->type = 1;
	ph->code = 0;
	ph->sid	= po->num;
	ph->length = htons(data_len);

946
	skb->protocol = cpu_to_be16(ETH_P_PPP_SES);
947
	skb->dev = dev;
L
Linus Torvalds 已提交
948

949 950
	dev_hard_header(skb, dev, ETH_P_PPP_SES,
			po->pppoe_pa.remote, NULL, data_len);
L
Linus Torvalds 已提交
951

952
	dev_queue_xmit(skb);
L
Linus Torvalds 已提交
953 954 955
	return 1;

abort:
956
	kfree_skb(skb);
957
	return 1;
L
Linus Torvalds 已提交
958 959 960 961 962 963 964 965 966 967
}

/************************************************************************
 *
 * xmit function called by generic PPP driver
 * sends PPP frame over PPPoE socket
 *
 ***********************************************************************/
static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
{
968
	struct sock *sk = (struct sock *)chan->private;
L
Linus Torvalds 已提交
969 970 971
	return __pppoe_xmit(sk, skb);
}

S
stephen hemminger 已提交
972
static const struct ppp_channel_ops pppoe_chan_ops = {
973
	.start_xmit = pppoe_xmit,
L
Linus Torvalds 已提交
974 975
};

976 977
static int pppoe_recvmsg(struct socket *sock, struct msghdr *m,
			 size_t total_len, int flags)
L
Linus Torvalds 已提交
978 979
{
	struct sock *sk = sock->sk;
980
	struct sk_buff *skb;
L
Linus Torvalds 已提交
981 982 983 984 985 986 987 988 989
	int error = 0;

	if (sk->sk_state & PPPOX_BOUND) {
		error = -EIO;
		goto end;
	}

	skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
				flags & MSG_DONTWAIT, &error);
990
	if (error < 0)
L
Linus Torvalds 已提交
991 992 993
		goto end;

	if (skb) {
S
Stephen Hemminger 已提交
994
		total_len = min_t(size_t, total_len, skb->len);
995
		error = skb_copy_datagram_msg(skb, 0, m, total_len);
996 997 998 999
		if (error == 0) {
			consume_skb(skb);
			return total_len;
		}
L
Linus Torvalds 已提交
1000 1001
	}

1002
	kfree_skb(skb);
L
Linus Torvalds 已提交
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
end:
	return error;
}

#ifdef CONFIG_PROC_FS
static int pppoe_seq_show(struct seq_file *seq, void *v)
{
	struct pppox_sock *po;
	char *dev_name;

	if (v == SEQ_START_TOKEN) {
		seq_puts(seq, "Id       Address              Device\n");
		goto out;
	}

	po = v;
	dev_name = po->pppoe_pa.dev;

J
Johannes Berg 已提交
1021
	seq_printf(seq, "%08X %pM %8s\n",
1022
		po->pppoe_pa.sid, po->pppoe_pa.remote, dev_name);
L
Linus Torvalds 已提交
1023 1024 1025 1026
out:
	return 0;
}

1027
static inline struct pppox_sock *pppoe_get_idx(struct pppoe_net *pn, loff_t pos)
L
Linus Torvalds 已提交
1028
{
1029
	struct pppox_sock *po;
1030
	int i;
L
Linus Torvalds 已提交
1031

1032
	for (i = 0; i < PPPOE_HASH_SIZE; i++) {
1033
		po = pn->hash_table[i];
L
Linus Torvalds 已提交
1034 1035 1036 1037 1038 1039
		while (po) {
			if (!pos--)
				goto out;
			po = po->next;
		}
	}
1040

L
Linus Torvalds 已提交
1041 1042 1043 1044 1045
out:
	return po;
}

static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos)
1046
	__acquires(pn->hash_lock)
L
Linus Torvalds 已提交
1047
{
1048
	struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
L
Linus Torvalds 已提交
1049 1050
	loff_t l = *pos;

1051 1052
	read_lock_bh(&pn->hash_lock);
	return l ? pppoe_get_idx(pn, --l) : SEQ_START_TOKEN;
L
Linus Torvalds 已提交
1053 1054 1055 1056
}

static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
{
1057
	struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
L
Linus Torvalds 已提交
1058 1059 1060 1061
	struct pppox_sock *po;

	++*pos;
	if (v == SEQ_START_TOKEN) {
1062
		po = pppoe_get_idx(pn, 0);
L
Linus Torvalds 已提交
1063 1064 1065
		goto out;
	}
	po = v;
1066
	if (po->next)
L
Linus Torvalds 已提交
1067 1068 1069 1070
		po = po->next;
	else {
		int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);

E
Eric Dumazet 已提交
1071
		po = NULL;
L
Linus Torvalds 已提交
1072
		while (++hash < PPPOE_HASH_SIZE) {
1073
			po = pn->hash_table[hash];
L
Linus Torvalds 已提交
1074 1075 1076 1077
			if (po)
				break;
		}
	}
1078

L
Linus Torvalds 已提交
1079 1080 1081 1082 1083
out:
	return po;
}

static void pppoe_seq_stop(struct seq_file *seq, void *v)
1084
	__releases(pn->hash_lock)
L
Linus Torvalds 已提交
1085
{
1086
	struct pppoe_net *pn = pppoe_pernet(seq_file_net(seq));
1087
	read_unlock_bh(&pn->hash_lock);
L
Linus Torvalds 已提交
1088 1089
}

J
Jan Engelhardt 已提交
1090
static const struct seq_operations pppoe_seq_ops = {
L
Linus Torvalds 已提交
1091 1092 1093 1094 1095 1096 1097 1098
	.start		= pppoe_seq_start,
	.next		= pppoe_seq_next,
	.stop		= pppoe_seq_stop,
	.show		= pppoe_seq_show,
};

static int pppoe_seq_open(struct inode *inode, struct file *file)
{
1099 1100
	return seq_open_net(inode, file, &pppoe_seq_ops,
			sizeof(struct seq_net_private));
L
Linus Torvalds 已提交
1101 1102
}

1103
static const struct file_operations pppoe_seq_fops = {
L
Linus Torvalds 已提交
1104 1105 1106 1107
	.owner		= THIS_MODULE,
	.open		= pppoe_seq_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
1108
	.release	= seq_release_net,
L
Linus Torvalds 已提交
1109 1110 1111 1112
};

#endif /* CONFIG_PROC_FS */

1113
static const struct proto_ops pppoe_ops = {
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
	.family		= AF_PPPOX,
	.owner		= THIS_MODULE,
	.release	= pppoe_release,
	.bind		= sock_no_bind,
	.connect	= pppoe_connect,
	.socketpair	= sock_no_socketpair,
	.accept		= sock_no_accept,
	.getname	= pppoe_getname,
	.poll		= datagram_poll,
	.listen		= sock_no_listen,
	.shutdown	= sock_no_shutdown,
	.setsockopt	= sock_no_setsockopt,
	.getsockopt	= sock_no_getsockopt,
	.sendmsg	= pppoe_sendmsg,
	.recvmsg	= pppoe_recvmsg,
	.mmap		= sock_no_mmap,
	.ioctl		= pppox_ioctl,
L
Linus Torvalds 已提交
1131 1132
};

1133
static const struct pppox_proto pppoe_proto = {
1134 1135 1136
	.create	= pppoe_create,
	.ioctl	= pppoe_ioctl,
	.owner	= THIS_MODULE,
L
Linus Torvalds 已提交
1137 1138
};

1139 1140
static __net_init int pppoe_init_net(struct net *net)
{
1141
	struct pppoe_net *pn = pppoe_pernet(net);
1142 1143 1144 1145
	struct proc_dir_entry *pde;

	rwlock_init(&pn->hash_lock);

1146
	pde = proc_create("pppoe", S_IRUGO, net->proc_net, &pppoe_seq_fops);
1147
#ifdef CONFIG_PROC_FS
1148 1149
	if (!pde)
		return -ENOMEM;
1150 1151 1152 1153 1154 1155 1156
#endif

	return 0;
}

static __net_exit void pppoe_exit_net(struct net *net)
{
1157
	remove_proc_entry("pppoe", net->proc_net);
1158 1159
}

A
Alexey Dobriyan 已提交
1160
static struct pernet_operations pppoe_net_ops = {
1161 1162
	.init = pppoe_init_net,
	.exit = pppoe_exit_net,
1163 1164
	.id   = &pppoe_net_id,
	.size = sizeof(struct pppoe_net),
1165 1166
};

L
Linus Torvalds 已提交
1167 1168
static int __init pppoe_init(void)
{
1169
	int err;
L
Linus Torvalds 已提交
1170

1171
	err = register_pernet_device(&pppoe_net_ops);
L
Linus Torvalds 已提交
1172 1173 1174
	if (err)
		goto out;

1175
	err = proto_register(&pppoe_sk_proto, 0);
L
Linus Torvalds 已提交
1176
	if (err)
1177
		goto out_unregister_net_ops;
L
Linus Torvalds 已提交
1178

1179
	err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
L
Linus Torvalds 已提交
1180
	if (err)
1181
		goto out_unregister_pppoe_proto;
1182

L
Linus Torvalds 已提交
1183 1184 1185
	dev_add_pack(&pppoes_ptype);
	dev_add_pack(&pppoed_ptype);
	register_netdevice_notifier(&pppoe_notifier);
1186 1187 1188

	return 0;

L
Linus Torvalds 已提交
1189 1190
out_unregister_pppoe_proto:
	proto_unregister(&pppoe_sk_proto);
1191
out_unregister_net_ops:
1192
	unregister_pernet_device(&pppoe_net_ops);
1193 1194
out:
	return err;
L
Linus Torvalds 已提交
1195 1196 1197 1198 1199
}

static void __exit pppoe_exit(void)
{
	unregister_netdevice_notifier(&pppoe_notifier);
1200 1201 1202
	dev_remove_pack(&pppoed_ptype);
	dev_remove_pack(&pppoes_ptype);
	unregister_pppox_proto(PX_PROTO_OE);
L
Linus Torvalds 已提交
1203
	proto_unregister(&pppoe_sk_proto);
1204
	unregister_pernet_device(&pppoe_net_ops);
L
Linus Torvalds 已提交
1205 1206 1207 1208 1209 1210 1211 1212
}

module_init(pppoe_init);
module_exit(pppoe_exit);

MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
MODULE_DESCRIPTION("PPP over Ethernet driver");
MODULE_LICENSE("GPL");
1213
MODULE_ALIAS_NET_PF_PROTO(PF_PPPOX, PX_PROTO_OE);