blob: f761a9aae4c81fc10dd17852275c2c68c0036223 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/** -*- linux-c -*- ***********************************************************
2 * Linux PPP over Ethernet (PPPoX/PPPoE) Sockets
3 *
4 * PPPoX --- Generic PPP encapsulation socket family
5 * PPPoE --- PPP over Ethernet (RFC 2516)
6 *
7 *
8 * Version: 0.7.0
9 *
Florian Zumbiehl90719db2007-03-02 13:16:56 -080010 * 070228 : Fix to allow multiple sessions with same remote MAC and same
11 * session id by including the local device ifindex in the
12 * tuple identifying a session. This also ensures packets can't
13 * be injected into a session from interfaces other than the one
14 * specified by userspace. Florian Zumbiehl <florz@florz.de>
15 * (Oh, BTW, this one is YYMMDD, in case you were wondering ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * 220102 : Fix module use count on failure in pppoe_create, pppox_sk -acme
17 * 030700 : Fixed connect logic to allow for disconnect.
18 * 270700 : Fixed potential SMP problems; we must protect against
19 * simultaneous invocation of ppp_input
20 * and ppp_unregister_channel.
21 * 040800 : Respect reference count mechanisms on net-devices.
22 * 200800 : fix kfree(skb) in pppoe_rcv (acme)
23 * Module reference count is decremented in the right spot now,
24 * guards against sock_put not actually freeing the sk
25 * in pppoe_release.
26 * 051000 : Initialization cleanup.
27 * 111100 : Fix recvmsg.
28 * 050101 : Fix PADT procesing.
29 * 140501 : Use pppoe_rcv_core to handle all backlog. (Alexey)
30 * 170701 : Do not lock_sock with rwlock held. (DaveM)
31 * Ignore discovery frames if user has socket
32 * locked. (DaveM)
33 * Ignore return value of dev_queue_xmit in __pppoe_xmit
34 * or else we may kfree an SKB twice. (DaveM)
35 * 190701 : When doing copies of skb's in __pppoe_xmit, always delete
36 * the original skb that was passed in on success, never on
37 * failure. Delete the copy of the skb on failure to avoid
38 * a memory leak.
39 * 081001 : Misc. cleanup (licence string, non-blocking, prevent
40 * reference of device on close).
41 * 121301 : New ppp channels interface; cannot unregister a channel
42 * from interrupts. Thus, we mark the socket as a ZOMBIE
43 * and do the unregistration later.
44 * 081002 : seq_file support for proc stuff -acme
45 * 111602 : Merge all 2.4 fixes into 2.5/2.6 tree. Label 2.5/2.6
46 * as version 0.7. Spacing cleanup.
47 * Author: Michal Ostrowski <mostrows@speakeasy.net>
48 * Contributors:
49 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
50 * David S. Miller (davem@redhat.com)
51 *
52 * License:
53 * This program is free software; you can redistribute it and/or
54 * modify it under the terms of the GNU General Public License
55 * as published by the Free Software Foundation; either version
56 * 2 of the License, or (at your option) any later version.
57 *
58 */
59
60#include <linux/string.h>
61#include <linux/module.h>
62#include <linux/kernel.h>
63#include <linux/slab.h>
64#include <linux/errno.h>
65#include <linux/netdevice.h>
66#include <linux/net.h>
67#include <linux/inetdevice.h>
68#include <linux/etherdevice.h>
69#include <linux/skbuff.h>
70#include <linux/init.h>
71#include <linux/if_ether.h>
72#include <linux/if_pppox.h>
73#include <linux/ppp_channel.h>
74#include <linux/ppp_defs.h>
75#include <linux/if_ppp.h>
76#include <linux/notifier.h>
77#include <linux/file.h>
78#include <linux/proc_fs.h>
79#include <linux/seq_file.h>
80
81#include <net/sock.h>
82
83#include <asm/uaccess.h>
84
85#define PPPOE_HASH_BITS 4
86#define PPPOE_HASH_SIZE (1<<PPPOE_HASH_BITS)
87
88static struct ppp_channel_ops pppoe_chan_ops;
89
90static int pppoe_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
91static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb);
92static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb);
93
David S. Miller17ba15f2005-12-27 20:57:40 -080094static const struct proto_ops pppoe_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static DEFINE_RWLOCK(pppoe_hash_lock);
96
97static struct ppp_channel_ops pppoe_chan_ops;
98
99static inline int cmp_2_addr(struct pppoe_addr *a, struct pppoe_addr *b)
100{
101 return (a->sid == b->sid &&
102 (memcmp(a->remote, b->remote, ETH_ALEN) == 0));
103}
104
105static inline int cmp_addr(struct pppoe_addr *a, unsigned long sid, char *addr)
106{
107 return (a->sid == sid &&
108 (memcmp(a->remote,addr,ETH_ALEN) == 0));
109}
110
111static int hash_item(unsigned long sid, unsigned char *addr)
112{
113 char hash = 0;
114 int i, j;
115
116 for (i = 0; i < ETH_ALEN ; ++i) {
117 for (j = 0; j < 8/PPPOE_HASH_BITS ; ++j) {
118 hash ^= addr[i] >> ( j * PPPOE_HASH_BITS );
119 }
120 }
121
122 for (i = 0; i < (sizeof(unsigned long)*8) / PPPOE_HASH_BITS ; ++i)
123 hash ^= sid >> (i*PPPOE_HASH_BITS);
124
125 return hash & ( PPPOE_HASH_SIZE - 1 );
126}
127
128/* zeroed because its in .bss */
129static struct pppox_sock *item_hash_table[PPPOE_HASH_SIZE];
130
131/**********************************************************************
132 *
133 * Set/get/delete/rehash items (internal versions)
134 *
135 **********************************************************************/
Florian Zumbiehl90719db2007-03-02 13:16:56 -0800136static struct pppox_sock *__get_item(unsigned long sid, unsigned char *addr, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 int hash = hash_item(sid, addr);
139 struct pppox_sock *ret;
140
141 ret = item_hash_table[hash];
142
Florian Zumbiehl6f30e182007-03-04 16:03:22 -0800143 while (ret && !(cmp_addr(&ret->pppoe_pa, sid, addr) && ret->pppoe_ifindex == ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 ret = ret->next;
145
146 return ret;
147}
148
149static int __set_item(struct pppox_sock *po)
150{
151 int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
152 struct pppox_sock *ret;
153
154 ret = item_hash_table[hash];
155 while (ret) {
Florian Zumbiehl6f30e182007-03-04 16:03:22 -0800156 if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa) && ret->pppoe_ifindex == po->pppoe_ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return -EALREADY;
158
159 ret = ret->next;
160 }
161
Florian Zumbiehl90719db2007-03-02 13:16:56 -0800162 po->next = item_hash_table[hash];
163 item_hash_table[hash] = po;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 return 0;
166}
167
Florian Zumbiehl90719db2007-03-02 13:16:56 -0800168static struct pppox_sock *__delete_item(unsigned long sid, char *addr, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 int hash = hash_item(sid, addr);
171 struct pppox_sock *ret, **src;
172
173 ret = item_hash_table[hash];
174 src = &item_hash_table[hash];
175
176 while (ret) {
Florian Zumbiehl6f30e182007-03-04 16:03:22 -0800177 if (cmp_addr(&ret->pppoe_pa, sid, addr) && ret->pppoe_ifindex == ifindex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 *src = ret->next;
179 break;
180 }
181
182 src = &ret->next;
183 ret = ret->next;
184 }
185
186 return ret;
187}
188
189/**********************************************************************
190 *
191 * Set/get/delete/rehash items
192 *
193 **********************************************************************/
194static inline struct pppox_sock *get_item(unsigned long sid,
Florian Zumbiehl90719db2007-03-02 13:16:56 -0800195 unsigned char *addr, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 struct pppox_sock *po;
198
199 read_lock_bh(&pppoe_hash_lock);
Florian Zumbiehl90719db2007-03-02 13:16:56 -0800200 po = __get_item(sid, addr, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (po)
202 sock_hold(sk_pppox(po));
203 read_unlock_bh(&pppoe_hash_lock);
204
205 return po;
206}
207
208static inline struct pppox_sock *get_item_by_addr(struct sockaddr_pppox *sp)
209{
Florian Zumbiehlbfafb262007-04-20 16:56:31 -0700210 struct net_device *dev;
Florian Zumbiehl90719db2007-03-02 13:16:56 -0800211 int ifindex;
212
213 dev = dev_get_by_name(sp->sa_addr.pppoe.dev);
214 if(!dev)
215 return NULL;
216 ifindex = dev->ifindex;
217 dev_put(dev);
218 return get_item(sp->sa_addr.pppoe.sid, sp->sa_addr.pppoe.remote, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221static inline int set_item(struct pppox_sock *po)
222{
223 int i;
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 write_lock_bh(&pppoe_hash_lock);
226 i = __set_item(po);
227 write_unlock_bh(&pppoe_hash_lock);
228
229 return i;
230}
231
Florian Zumbiehl90719db2007-03-02 13:16:56 -0800232static inline struct pppox_sock *delete_item(unsigned long sid, char *addr, int ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 struct pppox_sock *ret;
235
236 write_lock_bh(&pppoe_hash_lock);
Florian Zumbiehl90719db2007-03-02 13:16:56 -0800237 ret = __delete_item(sid, addr, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 write_unlock_bh(&pppoe_hash_lock);
239
240 return ret;
241}
242
243
244
245/***************************************************************************
246 *
247 * Handler for device events.
248 * Certain device events require that sockets be unconnected.
249 *
250 **************************************************************************/
251
252static void pppoe_flush_dev(struct net_device *dev)
253{
254 int hash;
255
256 BUG_ON(dev == NULL);
257
258 read_lock_bh(&pppoe_hash_lock);
259 for (hash = 0; hash < PPPOE_HASH_SIZE; hash++) {
260 struct pppox_sock *po = item_hash_table[hash];
261
262 while (po != NULL) {
263 if (po->pppoe_dev == dev) {
264 struct sock *sk = sk_pppox(po);
265
266 sock_hold(sk);
267 po->pppoe_dev = NULL;
268
269 /* We hold a reference to SK, now drop the
270 * hash table lock so that we may attempt
271 * to lock the socket (which can sleep).
272 */
273 read_unlock_bh(&pppoe_hash_lock);
274
275 lock_sock(sk);
276
277 if (sk->sk_state &
278 (PPPOX_CONNECTED | PPPOX_BOUND)) {
279 pppox_unbind_sock(sk);
280 dev_put(dev);
281 sk->sk_state = PPPOX_ZOMBIE;
282 sk->sk_state_change(sk);
283 }
284
285 release_sock(sk);
286
287 sock_put(sk);
288
289 read_lock_bh(&pppoe_hash_lock);
290
291 /* Now restart from the beginning of this
292 * hash chain. We always NULL out pppoe_dev
293 * so we are guaranteed to make forward
294 * progress.
295 */
296 po = item_hash_table[hash];
297 continue;
298 }
299 po = po->next;
300 }
301 }
302 read_unlock_bh(&pppoe_hash_lock);
303}
304
305static int pppoe_device_event(struct notifier_block *this,
306 unsigned long event, void *ptr)
307{
308 struct net_device *dev = (struct net_device *) ptr;
309
310 /* Only look at sockets that are using this specific device. */
311 switch (event) {
312 case NETDEV_CHANGEMTU:
313 /* A change in mtu is a bad thing, requiring
314 * LCP re-negotiation.
315 */
316
317 case NETDEV_GOING_DOWN:
318 case NETDEV_DOWN:
319 /* Find every socket on this device and kill it. */
320 pppoe_flush_dev(dev);
321 break;
322
323 default:
324 break;
325 };
326
327 return NOTIFY_DONE;
328}
329
330
331static struct notifier_block pppoe_notifier = {
332 .notifier_call = pppoe_device_event,
333};
334
335
336/************************************************************************
337 *
338 * Do the real work of receiving a PPPoE Session frame.
339 *
340 ***********************************************************************/
341static int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb)
342{
343 struct pppox_sock *po = pppox_sk(sk);
Florian Zumbiehlbfafb262007-04-20 16:56:31 -0700344 struct pppox_sock *relay_po;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 if (sk->sk_state & PPPOX_BOUND) {
Arnaldo Carvalho de Melo797659f2007-03-10 15:56:08 -0300347 struct pppoe_hdr *ph = pppoe_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 int len = ntohs(ph->length);
Herbert Xucbb042f2006-03-20 22:43:56 -0800349 skb_pull_rcsum(skb, sizeof(struct pppoe_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (pskb_trim_rcsum(skb, len))
351 goto abort_kfree;
352
353 ppp_input(&po->chan, skb);
354 } else if (sk->sk_state & PPPOX_RELAY) {
355 relay_po = get_item_by_addr(&po->pppoe_relay);
356
357 if (relay_po == NULL)
358 goto abort_kfree;
359
360 if ((sk_pppox(relay_po)->sk_state & PPPOX_CONNECTED) == 0)
361 goto abort_put;
362
363 skb_pull(skb, sizeof(struct pppoe_hdr));
364 if (!__pppoe_xmit(sk_pppox(relay_po), skb))
365 goto abort_put;
366 } else {
367 if (sock_queue_rcv_skb(sk, skb))
368 goto abort_kfree;
369 }
370
371 return NET_RX_SUCCESS;
372
373abort_put:
374 sock_put(sk_pppox(relay_po));
375
376abort_kfree:
377 kfree_skb(skb);
378 return NET_RX_DROP;
379}
380
381/************************************************************************
382 *
383 * Receive wrapper called in BH context.
384 *
385 ***********************************************************************/
386static int pppoe_rcv(struct sk_buff *skb,
387 struct net_device *dev,
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700388 struct packet_type *pt,
389 struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391{
392 struct pppoe_hdr *ph;
393 struct pppox_sock *po;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
396 goto drop;
397
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400398 if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 goto out;
400
Arnaldo Carvalho de Melo797659f2007-03-10 15:56:08 -0300401 ph = pppoe_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Florian Zumbiehl90719db2007-03-02 13:16:56 -0800403 po = get_item((unsigned long) ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400404 if (po != NULL)
Arnaldo Carvalho de Melo58a5a7b2006-11-16 14:06:06 -0200405 return sk_receive_skb(sk_pppox(po), skb, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406drop:
407 kfree_skb(skb);
408out:
409 return NET_RX_DROP;
410}
411
412/************************************************************************
413 *
414 * Receive a PPPoE Discovery frame.
415 * This is solely for detection of PADT frames
416 *
417 ***********************************************************************/
418static int pppoe_disc_rcv(struct sk_buff *skb,
419 struct net_device *dev,
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700420 struct packet_type *pt,
421 struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423{
424 struct pppoe_hdr *ph;
425 struct pppox_sock *po;
426
427 if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
428 goto abort;
429
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400430 if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 goto out;
432
Arnaldo Carvalho de Melo797659f2007-03-10 15:56:08 -0300433 ph = pppoe_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (ph->code != PADT_CODE)
435 goto abort;
436
Florian Zumbiehl90719db2007-03-02 13:16:56 -0800437 po = get_item((unsigned long) ph->sid, eth_hdr(skb)->h_source, dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (po) {
439 struct sock *sk = sk_pppox(po);
440
441 bh_lock_sock(sk);
442
443 /* If the user has locked the socket, just ignore
444 * the packet. With the way two rcv protocols hook into
445 * one socket family type, we cannot (easily) distinguish
446 * what kind of SKB it is during backlog rcv.
447 */
448 if (sock_owned_by_user(sk) == 0) {
449 /* We're no longer connect at the PPPOE layer,
450 * and must wait for ppp channel to disconnect us.
451 */
452 sk->sk_state = PPPOX_ZOMBIE;
453 }
454
455 bh_unlock_sock(sk);
456 sock_put(sk);
457 }
458
459abort:
460 kfree_skb(skb);
461out:
462 return NET_RX_SUCCESS; /* Lies... :-) */
463}
464
465static struct packet_type pppoes_ptype = {
466 .type = __constant_htons(ETH_P_PPP_SES),
467 .func = pppoe_rcv,
468};
469
470static struct packet_type pppoed_ptype = {
471 .type = __constant_htons(ETH_P_PPP_DISC),
472 .func = pppoe_disc_rcv,
473};
474
475static struct proto pppoe_sk_proto = {
476 .name = "PPPOE",
477 .owner = THIS_MODULE,
478 .obj_size = sizeof(struct pppox_sock),
479};
480
481/***********************************************************************
482 *
483 * Initialize a new struct sock.
484 *
485 **********************************************************************/
486static int pppoe_create(struct socket *sock)
487{
488 int error = -ENOMEM;
489 struct sock *sk;
490
491 sk = sk_alloc(PF_PPPOX, GFP_KERNEL, &pppoe_sk_proto, 1);
492 if (!sk)
493 goto out;
494
495 sock_init_data(sock, sk);
496
497 sock->state = SS_UNCONNECTED;
498 sock->ops = &pppoe_ops;
499
500 sk->sk_backlog_rcv = pppoe_rcv_core;
501 sk->sk_state = PPPOX_NONE;
502 sk->sk_type = SOCK_STREAM;
503 sk->sk_family = PF_PPPOX;
504 sk->sk_protocol = PX_PROTO_OE;
505
506 error = 0;
507out: return error;
508}
509
510static int pppoe_release(struct socket *sock)
511{
512 struct sock *sk = sock->sk;
513 struct pppox_sock *po;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 if (!sk)
516 return 0;
517
518 if (sock_flag(sk, SOCK_DEAD))
519 return -EBADF;
520
521 pppox_unbind_sock(sk);
522
523 /* Signal the death of the socket. */
524 sk->sk_state = PPPOX_DEAD;
525
526 po = pppox_sk(sk);
527 if (po->pppoe_pa.sid) {
Florian Zumbiehl6f30e182007-03-04 16:03:22 -0800528 delete_item(po->pppoe_pa.sid, po->pppoe_pa.remote, po->pppoe_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530
531 if (po->pppoe_dev)
532 dev_put(po->pppoe_dev);
533
534 po->pppoe_dev = NULL;
535
536 sock_orphan(sk);
537 sock->sk = NULL;
538
539 skb_queue_purge(&sk->sk_receive_queue);
540 sock_put(sk);
541
Florian Zumbiehlbfafb262007-04-20 16:56:31 -0700542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545
546static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
547 int sockaddr_len, int flags)
548{
549 struct sock *sk = sock->sk;
Florian Zumbiehl90719db2007-03-02 13:16:56 -0800550 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
552 struct pppox_sock *po = pppox_sk(sk);
553 int error;
554
555 lock_sock(sk);
556
557 error = -EINVAL;
558 if (sp->sa_protocol != PX_PROTO_OE)
559 goto end;
560
561 /* Check for already bound sockets */
562 error = -EBUSY;
563 if ((sk->sk_state & PPPOX_CONNECTED) && sp->sa_addr.pppoe.sid)
564 goto end;
565
566 /* Check for already disconnected sockets, on attempts to disconnect */
567 error = -EALREADY;
568 if ((sk->sk_state & PPPOX_DEAD) && !sp->sa_addr.pppoe.sid )
569 goto end;
570
571 error = 0;
572 if (po->pppoe_pa.sid) {
573 pppox_unbind_sock(sk);
574
575 /* Delete the old binding */
Florian Zumbiehl6f30e182007-03-04 16:03:22 -0800576 delete_item(po->pppoe_pa.sid,po->pppoe_pa.remote,po->pppoe_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 if(po->pppoe_dev)
579 dev_put(po->pppoe_dev);
580
581 memset(sk_pppox(po) + 1, 0,
582 sizeof(struct pppox_sock) - sizeof(struct sock));
583
584 sk->sk_state = PPPOX_NONE;
585 }
586
587 /* Don't re-bind if sid==0 */
588 if (sp->sa_addr.pppoe.sid != 0) {
589 dev = dev_get_by_name(sp->sa_addr.pppoe.dev);
590
591 error = -ENODEV;
592 if (!dev)
593 goto end;
594
595 po->pppoe_dev = dev;
Florian Zumbiehl6f30e182007-03-04 16:03:22 -0800596 po->pppoe_ifindex = dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 if (!(dev->flags & IFF_UP))
599 goto err_put;
600
601 memcpy(&po->pppoe_pa,
602 &sp->sa_addr.pppoe,
603 sizeof(struct pppoe_addr));
604
605 error = set_item(po);
606 if (error < 0)
607 goto err_put;
608
609 po->chan.hdrlen = (sizeof(struct pppoe_hdr) +
610 dev->hard_header_len);
611
Michal Ostrowskic9aa6892006-09-27 16:11:25 -0700612 po->chan.mtu = dev->mtu - sizeof(struct pppoe_hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 po->chan.private = sk;
614 po->chan.ops = &pppoe_chan_ops;
615
616 error = ppp_register_channel(&po->chan);
617 if (error)
618 goto err_put;
619
620 sk->sk_state = PPPOX_CONNECTED;
621 }
622
623 po->num = sp->sa_addr.pppoe.sid;
624
625 end:
626 release_sock(sk);
627 return error;
628err_put:
629 if (po->pppoe_dev) {
630 dev_put(po->pppoe_dev);
631 po->pppoe_dev = NULL;
632 }
633 goto end;
634}
635
636
637static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
638 int *usockaddr_len, int peer)
639{
640 int len = sizeof(struct sockaddr_pppox);
641 struct sockaddr_pppox sp;
642
643 sp.sa_family = AF_PPPOX;
644 sp.sa_protocol = PX_PROTO_OE;
645 memcpy(&sp.sa_addr.pppoe, &pppox_sk(sock->sk)->pppoe_pa,
646 sizeof(struct pppoe_addr));
647
648 memcpy(uaddr, &sp, len);
649
650 *usockaddr_len = len;
651
652 return 0;
653}
654
655
656static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
657 unsigned long arg)
658{
659 struct sock *sk = sock->sk;
660 struct pppox_sock *po = pppox_sk(sk);
661 int val = 0;
662 int err = 0;
663
664 switch (cmd) {
665 case PPPIOCGMRU:
666 err = -ENXIO;
667
668 if (!(sk->sk_state & PPPOX_CONNECTED))
669 break;
670
671 err = -EFAULT;
672 if (put_user(po->pppoe_dev->mtu -
673 sizeof(struct pppoe_hdr) -
674 PPP_HDRLEN,
675 (int __user *) arg))
676 break;
677 err = 0;
678 break;
679
680 case PPPIOCSMRU:
681 err = -ENXIO;
682 if (!(sk->sk_state & PPPOX_CONNECTED))
683 break;
684
685 err = -EFAULT;
686 if (get_user(val,(int __user *) arg))
687 break;
688
689 if (val < (po->pppoe_dev->mtu
690 - sizeof(struct pppoe_hdr)
691 - PPP_HDRLEN))
692 err = 0;
693 else
694 err = -EINVAL;
695 break;
696
697 case PPPIOCSFLAGS:
698 err = -EFAULT;
699 if (get_user(val, (int __user *) arg))
700 break;
701 err = 0;
702 break;
703
704 case PPPOEIOCSFWD:
705 {
706 struct pppox_sock *relay_po;
707
708 err = -EBUSY;
709 if (sk->sk_state & (PPPOX_BOUND | PPPOX_ZOMBIE | PPPOX_DEAD))
710 break;
711
712 err = -ENOTCONN;
713 if (!(sk->sk_state & PPPOX_CONNECTED))
714 break;
715
716 /* PPPoE address from the user specifies an outbound
Florian Zumbiehl90719db2007-03-02 13:16:56 -0800717 PPPoE address which frames are forwarded to */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 err = -EFAULT;
719 if (copy_from_user(&po->pppoe_relay,
720 (void __user *)arg,
721 sizeof(struct sockaddr_pppox)))
722 break;
723
724 err = -EINVAL;
725 if (po->pppoe_relay.sa_family != AF_PPPOX ||
726 po->pppoe_relay.sa_protocol!= PX_PROTO_OE)
727 break;
728
729 /* Check that the socket referenced by the address
730 actually exists. */
731 relay_po = get_item_by_addr(&po->pppoe_relay);
732
733 if (!relay_po)
734 break;
735
736 sock_put(sk_pppox(relay_po));
737 sk->sk_state |= PPPOX_RELAY;
738 err = 0;
739 break;
740 }
741
742 case PPPOEIOCDFWD:
743 err = -EALREADY;
744 if (!(sk->sk_state & PPPOX_RELAY))
745 break;
746
747 sk->sk_state &= ~PPPOX_RELAY;
748 err = 0;
749 break;
750
751 default:;
752 };
753
754 return err;
755}
756
757
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400758static int pppoe_sendmsg(struct kiocb *iocb, struct socket *sock,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 struct msghdr *m, size_t total_len)
760{
Florian Zumbiehlbfafb262007-04-20 16:56:31 -0700761 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 struct sock *sk = sock->sk;
763 struct pppox_sock *po = pppox_sk(sk);
Florian Zumbiehlbfafb262007-04-20 16:56:31 -0700764 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 struct pppoe_hdr hdr;
766 struct pppoe_hdr *ph;
767 struct net_device *dev;
768 char *start;
769
770 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
771 error = -ENOTCONN;
772 goto end;
773 }
774
775 hdr.ver = 1;
776 hdr.type = 1;
777 hdr.code = 0;
778 hdr.sid = po->num;
779
780 lock_sock(sk);
781
782 dev = po->pppoe_dev;
783
784 error = -EMSGSIZE;
785 if (total_len > (dev->mtu + dev->hard_header_len))
786 goto end;
787
788
789 skb = sock_wmalloc(sk, total_len + dev->hard_header_len + 32,
790 0, GFP_KERNEL);
791 if (!skb) {
792 error = -ENOMEM;
793 goto end;
794 }
795
796 /* Reserve space for headers. */
797 skb_reserve(skb, dev->hard_header_len);
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700798 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 skb->dev = dev;
801
802 skb->priority = sk->sk_priority;
803 skb->protocol = __constant_htons(ETH_P_PPP_SES);
804
805 ph = (struct pppoe_hdr *) skb_put(skb, total_len + sizeof(struct pppoe_hdr));
806 start = (char *) &ph->tag[0];
807
808 error = memcpy_fromiovec(start, m->msg_iov, total_len);
809
810 if (error < 0) {
811 kfree_skb(skb);
812 goto end;
813 }
814
815 error = total_len;
816 dev->hard_header(skb, dev, ETH_P_PPP_SES,
817 po->pppoe_pa.remote, NULL, total_len);
818
819 memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
820
821 ph->length = htons(total_len);
822
823 dev_queue_xmit(skb);
824
825end:
826 release_sock(sk);
827 return error;
828}
829
830
831/************************************************************************
832 *
833 * xmit function for internal use.
834 *
835 ***********************************************************************/
836static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
837{
838 struct pppox_sock *po = pppox_sk(sk);
839 struct net_device *dev = po->pppoe_dev;
840 struct pppoe_hdr hdr;
841 struct pppoe_hdr *ph;
842 int headroom = skb_headroom(skb);
843 int data_len = skb->len;
844 struct sk_buff *skb2;
845
846 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
847 goto abort;
848
849 hdr.ver = 1;
850 hdr.type = 1;
851 hdr.code = 0;
852 hdr.sid = po->num;
853 hdr.length = htons(skb->len);
854
855 if (!dev)
856 goto abort;
857
858 /* Copy the skb if there is no space for the header. */
859 if (headroom < (sizeof(struct pppoe_hdr) + dev->hard_header_len)) {
860 skb2 = dev_alloc_skb(32+skb->len +
861 sizeof(struct pppoe_hdr) +
862 dev->hard_header_len);
863
864 if (skb2 == NULL)
865 goto abort;
866
867 skb_reserve(skb2, dev->hard_header_len + sizeof(struct pppoe_hdr));
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300868 skb_copy_from_linear_data(skb, skb_put(skb2, skb->len),
869 skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 } else {
871 /* Make a clone so as to not disturb the original skb,
872 * give dev_queue_xmit something it can free.
873 */
874 skb2 = skb_clone(skb, GFP_ATOMIC);
Florin Malita9bc18092006-06-05 15:34:33 -0700875
876 if (skb2 == NULL)
877 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879
880 ph = (struct pppoe_hdr *) skb_push(skb2, sizeof(struct pppoe_hdr));
881 memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
882 skb2->protocol = __constant_htons(ETH_P_PPP_SES);
883
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700884 skb_reset_network_header(skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 skb2->dev = dev;
887
888 dev->hard_header(skb2, dev, ETH_P_PPP_SES,
889 po->pppoe_pa.remote, NULL, data_len);
890
891 /* We're transmitting skb2, and assuming that dev_queue_xmit
892 * will free it. The generic ppp layer however, is expecting
893 * that we give back 'skb' (not 'skb2') in case of failure,
894 * but free it in case of success.
895 */
896
897 if (dev_queue_xmit(skb2) < 0)
898 goto abort;
899
900 kfree_skb(skb);
901 return 1;
902
903abort:
904 return 0;
905}
906
907
908/************************************************************************
909 *
910 * xmit function called by generic PPP driver
911 * sends PPP frame over PPPoE socket
912 *
913 ***********************************************************************/
914static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
915{
916 struct sock *sk = (struct sock *) chan->private;
917 return __pppoe_xmit(sk, skb);
918}
919
920
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400921static struct ppp_channel_ops pppoe_chan_ops = {
922 .start_xmit = pppoe_xmit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923};
924
925static int pppoe_recvmsg(struct kiocb *iocb, struct socket *sock,
926 struct msghdr *m, size_t total_len, int flags)
927{
928 struct sock *sk = sock->sk;
Florian Zumbiehlbfafb262007-04-20 16:56:31 -0700929 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 if (sk->sk_state & PPPOX_BOUND) {
933 error = -EIO;
934 goto end;
935 }
936
937 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
938 flags & MSG_DONTWAIT, &error);
939
Florian Zumbiehlbfafb262007-04-20 16:56:31 -0700940 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 m->msg_namelen = 0;
944
945 if (skb) {
Arnaldo Carvalho de Melo797659f2007-03-10 15:56:08 -0300946 struct pppoe_hdr *ph = pppoe_hdr(skb);
947 const int len = ntohs(ph->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 error = memcpy_toiovec(m->msg_iov, (unsigned char *) &ph->tag[0], len);
Arnaldo Carvalho de Melo797659f2007-03-10 15:56:08 -0300950 if (error == 0)
951 error = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
953
Arnaldo Carvalho de Melo797659f2007-03-10 15:56:08 -0300954 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955end:
956 return error;
957}
958
959#ifdef CONFIG_PROC_FS
960static int pppoe_seq_show(struct seq_file *seq, void *v)
961{
962 struct pppox_sock *po;
963 char *dev_name;
964
965 if (v == SEQ_START_TOKEN) {
966 seq_puts(seq, "Id Address Device\n");
967 goto out;
968 }
969
970 po = v;
971 dev_name = po->pppoe_pa.dev;
972
973 seq_printf(seq, "%08X %02X:%02X:%02X:%02X:%02X:%02X %8s\n",
974 po->pppoe_pa.sid,
975 po->pppoe_pa.remote[0], po->pppoe_pa.remote[1],
976 po->pppoe_pa.remote[2], po->pppoe_pa.remote[3],
977 po->pppoe_pa.remote[4], po->pppoe_pa.remote[5], dev_name);
978out:
979 return 0;
980}
981
982static __inline__ struct pppox_sock *pppoe_get_idx(loff_t pos)
983{
Florian Zumbiehlbfafb262007-04-20 16:56:31 -0700984 struct pppox_sock *po;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 int i = 0;
986
987 for (; i < PPPOE_HASH_SIZE; i++) {
988 po = item_hash_table[i];
989 while (po) {
990 if (!pos--)
991 goto out;
992 po = po->next;
993 }
994 }
995out:
996 return po;
997}
998
999static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos)
1000{
1001 loff_t l = *pos;
1002
1003 read_lock_bh(&pppoe_hash_lock);
1004 return l ? pppoe_get_idx(--l) : SEQ_START_TOKEN;
1005}
1006
1007static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1008{
1009 struct pppox_sock *po;
1010
1011 ++*pos;
1012 if (v == SEQ_START_TOKEN) {
1013 po = pppoe_get_idx(0);
1014 goto out;
1015 }
1016 po = v;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001017 if (po->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 po = po->next;
1019 else {
1020 int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
1021
1022 while (++hash < PPPOE_HASH_SIZE) {
1023 po = item_hash_table[hash];
1024 if (po)
1025 break;
1026 }
1027 }
1028out:
1029 return po;
1030}
1031
1032static void pppoe_seq_stop(struct seq_file *seq, void *v)
1033{
1034 read_unlock_bh(&pppoe_hash_lock);
1035}
1036
1037static struct seq_operations pppoe_seq_ops = {
1038 .start = pppoe_seq_start,
1039 .next = pppoe_seq_next,
1040 .stop = pppoe_seq_stop,
1041 .show = pppoe_seq_show,
1042};
1043
1044static int pppoe_seq_open(struct inode *inode, struct file *file)
1045{
1046 return seq_open(file, &pppoe_seq_ops);
1047}
1048
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001049static const struct file_operations pppoe_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 .owner = THIS_MODULE,
1051 .open = pppoe_seq_open,
1052 .read = seq_read,
1053 .llseek = seq_lseek,
1054 .release = seq_release,
1055};
1056
1057static int __init pppoe_proc_init(void)
1058{
1059 struct proc_dir_entry *p;
1060
Al Viro66600222005-09-28 22:32:57 +01001061 p = create_proc_entry("net/pppoe", S_IRUGO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (!p)
1063 return -ENOMEM;
1064
1065 p->proc_fops = &pppoe_seq_fops;
1066 return 0;
1067}
1068#else /* CONFIG_PROC_FS */
1069static inline int pppoe_proc_init(void) { return 0; }
1070#endif /* CONFIG_PROC_FS */
1071
David S. Miller17ba15f2005-12-27 20:57:40 -08001072static const struct proto_ops pppoe_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 .family = AF_PPPOX,
1074 .owner = THIS_MODULE,
1075 .release = pppoe_release,
1076 .bind = sock_no_bind,
1077 .connect = pppoe_connect,
1078 .socketpair = sock_no_socketpair,
1079 .accept = sock_no_accept,
1080 .getname = pppoe_getname,
1081 .poll = datagram_poll,
1082 .listen = sock_no_listen,
1083 .shutdown = sock_no_shutdown,
1084 .setsockopt = sock_no_setsockopt,
1085 .getsockopt = sock_no_getsockopt,
1086 .sendmsg = pppoe_sendmsg,
1087 .recvmsg = pppoe_recvmsg,
David S. Miller17ba15f2005-12-27 20:57:40 -08001088 .mmap = sock_no_mmap,
1089 .ioctl = pppox_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090};
1091
1092static struct pppox_proto pppoe_proto = {
1093 .create = pppoe_create,
1094 .ioctl = pppoe_ioctl,
1095 .owner = THIS_MODULE,
1096};
1097
1098
1099static int __init pppoe_init(void)
1100{
1101 int err = proto_register(&pppoe_sk_proto, 0);
1102
1103 if (err)
1104 goto out;
1105
1106 err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
1107 if (err)
1108 goto out_unregister_pppoe_proto;
1109
1110 err = pppoe_proc_init();
1111 if (err)
1112 goto out_unregister_pppox_proto;
Jeff Garzik6aa20a22006-09-13 13:24:59 -04001113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 dev_add_pack(&pppoes_ptype);
1115 dev_add_pack(&pppoed_ptype);
1116 register_netdevice_notifier(&pppoe_notifier);
1117out:
1118 return err;
1119out_unregister_pppox_proto:
1120 unregister_pppox_proto(PX_PROTO_OE);
1121out_unregister_pppoe_proto:
1122 proto_unregister(&pppoe_sk_proto);
1123 goto out;
1124}
1125
1126static void __exit pppoe_exit(void)
1127{
1128 unregister_pppox_proto(PX_PROTO_OE);
1129 dev_remove_pack(&pppoes_ptype);
1130 dev_remove_pack(&pppoed_ptype);
1131 unregister_netdevice_notifier(&pppoe_notifier);
Al Viro66600222005-09-28 22:32:57 +01001132 remove_proc_entry("net/pppoe", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 proto_unregister(&pppoe_sk_proto);
1134}
1135
1136module_init(pppoe_init);
1137module_exit(pppoe_exit);
1138
1139MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
1140MODULE_DESCRIPTION("PPP over Ethernet driver");
1141MODULE_LICENSE("GPL");
1142MODULE_ALIAS_NETPROTO(PF_PPPOX);