More meth updates.
[linux-2.6/linux-mips.git] / drivers / net / pppoe.c
blob8b45fc142d6bc540fb4dd7993e2e2e8dadca0f2d
1 /** -*- linux-c -*- ***********************************************************
2 * Linux PPP over Ethernet (PPPoX/PPPoE) Sockets
4 * PPPoX --- Generic PPP encapsulation socket family
5 * PPPoE --- PPP over Ethernet (RFC 2516)
8 * Version: 0.7.0
10 * 220102 : Fix module use count on failure in pppoe_create, pppox_sk -acme
11 * 030700 : Fixed connect logic to allow for disconnect.
12 * 270700 : Fixed potential SMP problems; we must protect against
13 * simultaneous invocation of ppp_input
14 * and ppp_unregister_channel.
15 * 040800 : Respect reference count mechanisms on net-devices.
16 * 200800 : fix kfree(skb) in pppoe_rcv (acme)
17 * Module reference count is decremented in the right spot now,
18 * guards against sock_put not actually freeing the sk
19 * in pppoe_release.
20 * 051000 : Initialization cleanup.
21 * 111100 : Fix recvmsg.
22 * 050101 : Fix PADT procesing.
23 * 140501 : Use pppoe_rcv_core to handle all backlog. (Alexey)
24 * 170701 : Do not lock_sock with rwlock held. (DaveM)
25 * Ignore discovery frames if user has socket
26 * locked. (DaveM)
27 * Ignore return value of dev_queue_xmit in __pppoe_xmit
28 * or else we may kfree an SKB twice. (DaveM)
29 * 190701 : When doing copies of skb's in __pppoe_xmit, always delete
30 * the original skb that was passed in on success, never on
31 * failure. Delete the copy of the skb on failure to avoid
32 * a memory leak.
33 * 081001 : Misc. cleanup (licence string, non-blocking, prevent
34 * reference of device on close).
35 * 121301 : New ppp channels interface; cannot unregister a channel
36 * from interrupts. Thus, we mark the socket as a ZOMBIE
37 * and do the unregistration later.
38 * 081002 : seq_file support for proc stuff -acme
39 * 111602 : Merge all 2.4 fixes into 2.5/2.6 tree. Label 2.5/2.6
40 * as version 0.7. Spacing cleanup.
41 * Author: Michal Ostrowski <mostrows@speakeasy.net>
42 * Contributors:
43 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
44 * David S. Miller (davem@redhat.com)
46 * License:
47 * This program is free software; you can redistribute it and/or
48 * modify it under the terms of the GNU General Public License
49 * as published by the Free Software Foundation; either version
50 * 2 of the License, or (at your option) any later version.
54 #include <linux/string.h>
55 #include <linux/module.h>
56 #include <linux/kernel.h>
57 #include <linux/slab.h>
58 #include <linux/errno.h>
59 #include <linux/netdevice.h>
60 #include <linux/net.h>
61 #include <linux/inetdevice.h>
62 #include <linux/etherdevice.h>
63 #include <linux/skbuff.h>
64 #include <linux/init.h>
65 #include <linux/if_ether.h>
66 #include <linux/if_pppox.h>
67 #include <linux/ppp_channel.h>
68 #include <linux/ppp_defs.h>
69 #include <linux/if_ppp.h>
70 #include <linux/if_pppvar.h>
71 #include <linux/notifier.h>
72 #include <linux/file.h>
73 #include <linux/proc_fs.h>
74 #include <linux/seq_file.h>
76 #include <net/sock.h>
78 #include <asm/uaccess.h>
80 static int __attribute__((unused)) pppoe_debug = 7;
81 #define PPPOE_HASH_BITS 4
82 #define PPPOE_HASH_SIZE (1<<PPPOE_HASH_BITS)
84 int pppoe_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
85 int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb);
86 int __pppoe_xmit(struct sock *sk, struct sk_buff *skb);
88 struct proto_ops pppoe_ops;
91 #if 0
92 #define CHECKPTR(x,y) do { if (!(x) && pppoe_debug &7 ){ printk(KERN_CRIT "PPPoE Invalid pointer : %s , %p\n",#x,(x)); error=-EINVAL; goto y; }} while (0)
93 #define DEBUG(s,args...) do { if( pppoe_debug & (s) ) printk(KERN_CRIT args ); } while (0)
94 #else
95 #define CHECKPTR(x,y) do { } while (0)
96 #define DEBUG(s,args...) do { } while (0)
97 #endif
101 static rwlock_t pppoe_hash_lock = RW_LOCK_UNLOCKED;
104 static inline int cmp_2_addr(struct pppoe_addr *a, struct pppoe_addr *b)
106 return (a->sid == b->sid &&
107 (memcmp(a->remote, b->remote, ETH_ALEN) == 0));
110 static inline int cmp_addr(struct pppoe_addr *a, unsigned long sid, char *addr)
112 return (a->sid == sid &&
113 (memcmp(a->remote,addr,ETH_ALEN) == 0));
116 static int hash_item(unsigned long sid, unsigned char *addr)
118 char hash = 0;
119 int i, j;
121 for (i = 0; i < ETH_ALEN ; ++i) {
122 for (j = 0; j < 8/PPPOE_HASH_BITS ; ++j) {
123 hash ^= addr[i] >> ( j * PPPOE_HASH_BITS );
127 for (i = 0; i < (sizeof(unsigned long)*8) / PPPOE_HASH_BITS ; ++i)
128 hash ^= sid >> (i*PPPOE_HASH_BITS);
130 return hash & ( PPPOE_HASH_SIZE - 1 );
133 /* zeroed because its in .bss */
134 static struct pppox_opt *item_hash_table[PPPOE_HASH_SIZE];
136 /**********************************************************************
138 * Set/get/delete/rehash items (internal versions)
140 **********************************************************************/
141 static struct pppox_opt *__get_item(unsigned long sid, unsigned char *addr)
143 int hash = hash_item(sid, addr);
144 struct pppox_opt *ret;
146 ret = item_hash_table[hash];
148 while (ret && !cmp_addr(&ret->pppoe_pa, sid, addr))
149 ret = ret->next;
151 return ret;
154 static int __set_item(struct pppox_opt *po)
156 int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
157 struct pppox_opt *ret;
159 ret = item_hash_table[hash];
160 while (ret) {
161 if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa))
162 return -EALREADY;
164 ret = ret->next;
167 if (!ret) {
168 po->next = item_hash_table[hash];
169 item_hash_table[hash] = po;
172 return 0;
175 static struct pppox_opt *__delete_item(unsigned long sid, char *addr)
177 int hash = hash_item(sid, addr);
178 struct pppox_opt *ret, **src;
180 ret = item_hash_table[hash];
181 src = &item_hash_table[hash];
183 while (ret) {
184 if (cmp_addr(&ret->pppoe_pa, sid, addr)) {
185 *src = ret->next;
186 break;
189 src = &ret->next;
190 ret = ret->next;
193 return ret;
196 /**********************************************************************
198 * Set/get/delete/rehash items
200 **********************************************************************/
201 static inline struct pppox_opt *get_item(unsigned long sid,
202 unsigned char *addr)
204 struct pppox_opt *po;
206 read_lock_bh(&pppoe_hash_lock);
207 po = __get_item(sid, addr);
208 if (po)
209 sock_hold(po->sk);
210 read_unlock_bh(&pppoe_hash_lock);
212 return po;
215 static inline struct pppox_opt *get_item_by_addr(struct sockaddr_pppox *sp)
217 return get_item(sp->sa_addr.pppoe.sid, sp->sa_addr.pppoe.remote);
220 static inline int set_item(struct pppox_opt *po)
222 int i;
224 if (!po)
225 return -EINVAL;
227 write_lock_bh(&pppoe_hash_lock);
228 i = __set_item(po);
229 write_unlock_bh(&pppoe_hash_lock);
231 return i;
234 static inline struct pppox_opt *delete_item(unsigned long sid, char *addr)
236 struct pppox_opt *ret;
238 write_lock_bh(&pppoe_hash_lock);
239 ret = __delete_item(sid, addr);
240 write_unlock_bh(&pppoe_hash_lock);
242 return ret;
247 /***************************************************************************
249 * Handler for device events.
250 * Certain device events require that sockets be unconnected.
252 **************************************************************************/
254 static void pppoe_flush_dev(struct net_device *dev)
256 int hash;
258 if (dev == NULL)
259 BUG();
261 read_lock_bh(&pppoe_hash_lock);
262 for (hash = 0; hash < PPPOE_HASH_SIZE; hash++) {
263 struct pppox_opt *po = item_hash_table[hash];
265 while (po != NULL) {
266 if (po->pppoe_dev == dev) {
267 struct sock *sk = po->sk;
269 sock_hold(sk);
270 po->pppoe_dev = NULL;
272 /* We hold a reference to SK, now drop the
273 * hash table lock so that we may attempt
274 * to lock the socket (which can sleep).
276 read_unlock_bh(&pppoe_hash_lock);
278 lock_sock(sk);
280 if (sk->sk_state &
281 (PPPOX_CONNECTED | PPPOX_BOUND)) {
282 pppox_unbind_sock(sk);
283 dev_put(dev);
284 sk->sk_state = PPPOX_ZOMBIE;
285 sk->sk_state_change(sk);
288 release_sock(sk);
290 sock_put(sk);
292 read_lock_bh(&pppoe_hash_lock);
294 /* Now restart from the beginning of this
295 * hash chain. We always NULL out pppoe_dev
296 * so we are guaranteed to make forward
297 * progress.
299 po = item_hash_table[hash];
300 continue;
302 po = po->next;
305 read_unlock_bh(&pppoe_hash_lock);
308 static int pppoe_device_event(struct notifier_block *this,
309 unsigned long event, void *ptr)
311 struct net_device *dev = (struct net_device *) ptr;
313 /* Only look at sockets that are using this specific device. */
314 switch (event) {
315 case NETDEV_CHANGEMTU:
316 /* A change in mtu is a bad thing, requiring
317 * LCP re-negotiation.
320 case NETDEV_GOING_DOWN:
321 case NETDEV_DOWN:
322 /* Find every socket on this device and kill it. */
323 pppoe_flush_dev(dev);
324 break;
326 default:
327 break;
330 return NOTIFY_DONE;
334 static struct notifier_block pppoe_notifier = {
335 .notifier_call = pppoe_device_event,
341 /************************************************************************
343 * Do the real work of receiving a PPPoE Session frame.
345 ***********************************************************************/
346 int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb)
348 struct pppox_opt *po = pppox_sk(sk);
349 struct pppox_opt *relay_po = NULL;
351 if (sk->sk_state & PPPOX_BOUND) {
352 skb_pull(skb, sizeof(struct pppoe_hdr));
353 ppp_input(&po->chan, skb);
354 } else if (sk->sk_state & PPPOX_RELAY) {
355 relay_po = get_item_by_addr(&po->pppoe_relay);
357 if (relay_po == NULL)
358 goto abort_kfree;
360 if ((relay_po->sk->sk_state & PPPOX_CONNECTED) == 0)
361 goto abort_put;
363 skb_pull(skb, sizeof(struct pppoe_hdr));
364 if (!__pppoe_xmit( relay_po->sk , skb))
365 goto abort_put;
366 } else {
367 sock_queue_rcv_skb(sk, skb);
370 return NET_RX_SUCCESS;
372 abort_put:
373 sock_put(relay_po->sk);
375 abort_kfree:
376 kfree_skb(skb);
377 return NET_RX_DROP;
380 /************************************************************************
382 * Receive wrapper called in BH context.
384 ***********************************************************************/
385 static int pppoe_rcv(struct sk_buff *skb,
386 struct net_device *dev,
387 struct packet_type *pt)
390 struct pppoe_hdr *ph = (struct pppoe_hdr *) skb->nh.raw;
391 struct pppox_opt *po;
392 struct sock *sk ;
393 int ret;
395 po = get_item((unsigned long) ph->sid, skb->mac.ethernet->h_source);
397 if (!po) {
398 kfree_skb(skb);
399 return NET_RX_DROP;
402 sk = po->sk;
403 bh_lock_sock(sk);
405 /* Socket state is unknown, must put skb into backlog. */
406 if (sock_owned_by_user(sk) != 0) {
407 sk_add_backlog(sk, skb);
408 ret = NET_RX_SUCCESS;
409 } else {
410 ret = pppoe_rcv_core(sk, skb);
413 bh_unlock_sock(sk);
414 sock_put(sk);
416 return ret;
419 /************************************************************************
421 * Receive a PPPoE Discovery frame.
422 * This is solely for detection of PADT frames
424 ***********************************************************************/
425 static int pppoe_disc_rcv(struct sk_buff *skb,
426 struct net_device *dev,
427 struct packet_type *pt)
430 struct pppoe_hdr *ph = (struct pppoe_hdr *) skb->nh.raw;
431 struct pppox_opt *po;
433 if (ph->code != PADT_CODE)
434 goto abort;
436 po = get_item((unsigned long) ph->sid, skb->mac.ethernet->h_source);
437 if (po) {
438 struct sock *sk = po->sk;
440 bh_lock_sock(sk);
442 /* If the user has locked the socket, just ignore
443 * the packet. With the way two rcv protocols hook into
444 * one socket family type, we cannot (easily) distinguish
445 * what kind of SKB it is during backlog rcv.
447 if (sock_owned_by_user(sk) == 0) {
448 /* We're no longer connect at the PPPOE layer,
449 * and must wait for ppp channel to disconnect us.
451 sk->sk_state = PPPOX_ZOMBIE;
454 bh_unlock_sock(sk);
455 sock_put(sk);
458 abort:
459 kfree_skb(skb);
460 return NET_RX_SUCCESS; /* Lies... :-) */
463 struct packet_type pppoes_ptype = {
464 .type = __constant_htons(ETH_P_PPP_SES),
465 .func = pppoe_rcv,
468 struct packet_type pppoed_ptype = {
469 .type = __constant_htons(ETH_P_PPP_DISC),
470 .func = pppoe_disc_rcv,
473 /***********************************************************************
475 * Really kill the socket. (Called from pppox_sk_free if refcnt == 0.)
477 **********************************************************************/
478 static void pppoe_sk_free(struct sock *sk)
480 struct pppox_opt *po = pppox_sk(sk);
482 if (po)
483 kfree(po);
487 /***********************************************************************
489 * Initialize a new struct sock.
491 **********************************************************************/
492 static int pppoe_create(struct socket *sock)
494 int error = -ENOMEM;
495 struct sock *sk;
496 struct pppox_opt *po;
498 sk = sk_alloc(PF_PPPOX, GFP_KERNEL, 1, NULL);
499 if (!sk)
500 goto out;
502 sock_init_data(sock, sk);
503 sk_set_owner(sk, THIS_MODULE);
504 sock->state = SS_UNCONNECTED;
505 sock->ops = &pppoe_ops;
507 sk->sk_backlog_rcv = pppoe_rcv_core;
508 sk->sk_state = PPPOX_NONE;
509 sk->sk_type = SOCK_STREAM;
510 sk->sk_family = PF_PPPOX;
511 sk->sk_protocol = PX_PROTO_OE;
512 sk->sk_destruct = pppoe_sk_free;
514 po = pppox_sk(sk) = kmalloc(sizeof(*po), GFP_KERNEL);
515 if (!po)
516 goto frees;
517 memset(po, 0, sizeof(*po));
518 po->sk = sk;
519 error = 0;
520 out: return error;
521 frees: sk_free(sk);
522 goto out;
525 int pppoe_release(struct socket *sock)
527 struct sock *sk = sock->sk;
528 struct pppox_opt *po;
529 int error = 0;
531 if (!sk)
532 return 0;
534 if (sock_flag(sk, SOCK_DEAD))
535 return -EBADF;
537 pppox_unbind_sock(sk);
539 /* Signal the death of the socket. */
540 sk->sk_state = PPPOX_DEAD;
542 po = pppox_sk(sk);
543 if (po->pppoe_pa.sid) {
544 delete_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
547 if (po->pppoe_dev)
548 dev_put(po->pppoe_dev);
550 po->pppoe_dev = NULL;
552 sock_orphan(sk);
553 sock->sk = NULL;
555 skb_queue_purge(&sk->sk_receive_queue);
556 sock_put(sk);
558 return error;
562 int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
563 int sockaddr_len, int flags)
565 struct sock *sk = sock->sk;
566 struct net_device *dev = NULL;
567 struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
568 struct pppox_opt *po = pppox_sk(sk);
569 int error;
571 lock_sock(sk);
573 error = -EINVAL;
574 if (sp->sa_protocol != PX_PROTO_OE)
575 goto end;
577 /* Check for already bound sockets */
578 error = -EBUSY;
579 if ((sk->sk_state & PPPOX_CONNECTED) && sp->sa_addr.pppoe.sid)
580 goto end;
582 /* Check for already disconnected sockets, on attempts to disconnect */
583 error = -EALREADY;
584 if ((sk->sk_state & PPPOX_DEAD) && !sp->sa_addr.pppoe.sid )
585 goto end;
587 error = 0;
588 if (po->pppoe_pa.sid) {
589 pppox_unbind_sock(sk);
591 /* Delete the old binding */
592 delete_item(po->pppoe_pa.sid,po->pppoe_pa.remote);
594 if(po->pppoe_dev)
595 dev_put(po->pppoe_dev);
597 memset(po, 0, sizeof(struct pppox_opt));
598 po->sk = sk;
600 sk->sk_state = PPPOX_NONE;
603 /* Don't re-bind if sid==0 */
604 if (sp->sa_addr.pppoe.sid != 0) {
605 dev = dev_get_by_name(sp->sa_addr.pppoe.dev);
607 error = -ENODEV;
608 if (!dev)
609 goto end;
611 po->pppoe_dev = dev;
613 if (!(dev->flags & IFF_UP))
614 goto err_put;
616 memcpy(&po->pppoe_pa,
617 &sp->sa_addr.pppoe,
618 sizeof(struct pppoe_addr));
620 error = set_item(po);
621 if (error < 0)
622 goto err_put;
624 po->chan.hdrlen = (sizeof(struct pppoe_hdr) +
625 dev->hard_header_len);
627 po->chan.private = sk;
628 po->chan.ops = &pppoe_chan_ops;
630 error = ppp_register_channel(&po->chan);
631 if (error)
632 goto err_put;
634 sk->sk_state = PPPOX_CONNECTED;
637 po->num = sp->sa_addr.pppoe.sid;
639 end:
640 release_sock(sk);
641 return error;
642 err_put:
643 if (po->pppoe_dev) {
644 dev_put(po->pppoe_dev);
645 po->pppoe_dev = NULL;
647 goto end;
651 int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
652 int *usockaddr_len, int peer)
654 int len = sizeof(struct sockaddr_pppox);
655 struct sockaddr_pppox sp;
657 sp.sa_family = AF_PPPOX;
658 sp.sa_protocol = PX_PROTO_OE;
659 memcpy(&sp.sa_addr.pppoe, &pppox_sk(sock->sk)->pppoe_pa,
660 sizeof(struct pppoe_addr));
662 memcpy(uaddr, &sp, len);
664 *usockaddr_len = len;
666 return 0;
670 int pppoe_ioctl(struct socket *sock, unsigned int cmd,
671 unsigned long arg)
673 struct sock *sk = sock->sk;
674 struct pppox_opt *po = pppox_sk(sk);
675 int val = 0;
676 int err = 0;
678 switch (cmd) {
679 case PPPIOCGMRU:
680 err = -ENXIO;
682 if (!(sk->sk_state & PPPOX_CONNECTED))
683 break;
685 err = -EFAULT;
686 if (put_user(po->pppoe_dev->mtu -
687 sizeof(struct pppoe_hdr) -
688 PPP_HDRLEN,
689 (int *) arg))
690 break;
691 err = 0;
692 break;
694 case PPPIOCSMRU:
695 err = -ENXIO;
696 if (!(sk->sk_state & PPPOX_CONNECTED))
697 break;
699 err = -EFAULT;
700 if (get_user(val,(int *) arg))
701 break;
703 if (val < (po->pppoe_dev->mtu
704 - sizeof(struct pppoe_hdr)
705 - PPP_HDRLEN))
706 err = 0;
707 else
708 err = -EINVAL;
709 break;
711 case PPPIOCSFLAGS:
712 err = -EFAULT;
713 if (get_user(val, (int *) arg))
714 break;
715 err = 0;
716 break;
718 case PPPOEIOCSFWD:
720 struct pppox_opt *relay_po;
722 err = -EBUSY;
723 if (sk->sk_state & (PPPOX_BOUND | PPPOX_ZOMBIE | PPPOX_DEAD))
724 break;
726 err = -ENOTCONN;
727 if (!(sk->sk_state & PPPOX_CONNECTED))
728 break;
730 /* PPPoE address from the user specifies an outbound
731 PPPoE address to which frames are forwarded to */
732 err = -EFAULT;
733 if (copy_from_user(&po->pppoe_relay,
734 (void*)arg,
735 sizeof(struct sockaddr_pppox)))
736 break;
738 err = -EINVAL;
739 if (po->pppoe_relay.sa_family != AF_PPPOX ||
740 po->pppoe_relay.sa_protocol!= PX_PROTO_OE)
741 break;
743 /* Check that the socket referenced by the address
744 actually exists. */
745 relay_po = get_item_by_addr(&po->pppoe_relay);
747 if (!relay_po)
748 break;
750 sock_put(relay_po->sk);
751 sk->sk_state |= PPPOX_RELAY;
752 err = 0;
753 break;
756 case PPPOEIOCDFWD:
757 err = -EALREADY;
758 if (!(sk->sk_state & PPPOX_RELAY))
759 break;
761 sk->sk_state &= ~PPPOX_RELAY;
762 err = 0;
763 break;
765 default:;
768 return err;
772 int pppoe_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m,
773 int total_len)
775 struct sk_buff *skb = NULL;
776 struct sock *sk = sock->sk;
777 struct pppox_opt *po = pppox_sk(sk);
778 int error = 0;
779 struct pppoe_hdr hdr;
780 struct pppoe_hdr *ph;
781 struct net_device *dev;
782 char *start;
784 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
785 error = -ENOTCONN;
786 goto end;
789 hdr.ver = 1;
790 hdr.type = 1;
791 hdr.code = 0;
792 hdr.sid = po->num;
794 lock_sock(sk);
796 dev = po->pppoe_dev;
798 error = -EMSGSIZE;
799 if (total_len > (dev->mtu + dev->hard_header_len))
800 goto end;
803 skb = sock_wmalloc(sk, total_len + dev->hard_header_len + 32,
804 0, GFP_KERNEL);
805 if (!skb) {
806 error = -ENOMEM;
807 goto end;
810 /* Reserve space for headers. */
811 skb_reserve(skb, dev->hard_header_len);
812 skb->nh.raw = skb->data;
814 skb->dev = dev;
816 skb->priority = sk->sk_priority;
817 skb->protocol = __constant_htons(ETH_P_PPP_SES);
819 ph = (struct pppoe_hdr *) skb_put(skb, total_len + sizeof(struct pppoe_hdr));
820 start = (char *) &ph->tag[0];
822 error = memcpy_fromiovec(start, m->msg_iov, total_len);
824 if (error < 0) {
825 kfree_skb(skb);
826 goto end;
829 error = total_len;
830 dev->hard_header(skb, dev, ETH_P_PPP_SES,
831 po->pppoe_pa.remote, NULL, total_len);
833 memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
835 ph->length = htons(total_len);
837 dev_queue_xmit(skb);
839 end:
840 release_sock(sk);
841 return error;
845 /************************************************************************
847 * xmit function for internal use.
849 ***********************************************************************/
850 int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
852 struct pppox_opt *po = pppox_sk(sk);
853 struct net_device *dev = po->pppoe_dev;
854 struct pppoe_hdr hdr;
855 struct pppoe_hdr *ph;
856 int headroom = skb_headroom(skb);
857 int data_len = skb->len;
858 struct sk_buff *skb2;
860 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
861 goto abort;
863 hdr.ver = 1;
864 hdr.type = 1;
865 hdr.code = 0;
866 hdr.sid = po->num;
867 hdr.length = htons(skb->len);
869 if (!dev)
870 goto abort;
872 /* Copy the skb if there is no space for the header. */
873 if (headroom < (sizeof(struct pppoe_hdr) + dev->hard_header_len)) {
874 skb2 = dev_alloc_skb(32+skb->len +
875 sizeof(struct pppoe_hdr) +
876 dev->hard_header_len);
878 if (skb2 == NULL)
879 goto abort;
881 skb_reserve(skb2, dev->hard_header_len + sizeof(struct pppoe_hdr));
882 memcpy(skb_put(skb2, skb->len), skb->data, skb->len);
883 } else {
884 /* Make a clone so as to not disturb the original skb,
885 * give dev_queue_xmit something it can free.
887 skb2 = skb_clone(skb, GFP_ATOMIC);
890 ph = (struct pppoe_hdr *) skb_push(skb2, sizeof(struct pppoe_hdr));
891 memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
892 skb2->protocol = __constant_htons(ETH_P_PPP_SES);
894 skb2->nh.raw = skb2->data;
896 skb2->dev = dev;
898 dev->hard_header(skb2, dev, ETH_P_PPP_SES,
899 po->pppoe_pa.remote, NULL, data_len);
901 /* We're transmitting skb2, and assuming that dev_queue_xmit
902 * will free it. The generic ppp layer however, is expecting
903 * that we give back 'skb' (not 'skb2') in case of failure,
904 * but free it in case of success.
907 if (dev_queue_xmit(skb2) < 0)
908 goto abort;
910 kfree_skb(skb);
911 return 1;
913 abort:
914 return 0;
918 /************************************************************************
920 * xmit function called by generic PPP driver
921 * sends PPP frame over PPPoE socket
923 ***********************************************************************/
924 int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
926 struct sock *sk = (struct sock *) chan->private;
927 return __pppoe_xmit(sk, skb);
931 struct ppp_channel_ops pppoe_chan_ops = { pppoe_xmit , NULL };
933 int pppoe_recvmsg(struct kiocb *iocb, struct socket *sock,
934 struct msghdr *m, int total_len, int flags)
936 struct sock *sk = sock->sk;
937 struct sk_buff *skb = NULL;
938 int error = 0;
939 int len;
940 struct pppoe_hdr *ph = NULL;
942 if (sk->sk_state & PPPOX_BOUND) {
943 error = -EIO;
944 goto end;
947 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
948 flags & MSG_DONTWAIT, &error);
950 if (error < 0) {
951 goto end;
954 m->msg_namelen = 0;
956 if (skb) {
957 error = 0;
958 ph = (struct pppoe_hdr *) skb->nh.raw;
959 len = ntohs(ph->length);
961 error = memcpy_toiovec(m->msg_iov, (unsigned char *) &ph->tag[0], len);
962 if (error < 0)
963 goto do_skb_free;
964 error = len;
967 do_skb_free:
968 if (skb)
969 kfree_skb(skb);
970 end:
971 return error;
974 #ifdef CONFIG_PROC_FS
975 static int pppoe_seq_show(struct seq_file *seq, void *v)
977 struct pppox_opt *po;
978 char *dev_name;
980 if (v == (void *)1) {
981 seq_puts(seq, "Id Address Device\n");
982 goto out;
985 po = v;
986 dev_name = po->pppoe_pa.dev;
988 seq_printf(seq, "%08X %02X:%02X:%02X:%02X:%02X:%02X %8s\n",
989 po->pppoe_pa.sid,
990 po->pppoe_pa.remote[0], po->pppoe_pa.remote[1],
991 po->pppoe_pa.remote[2], po->pppoe_pa.remote[3],
992 po->pppoe_pa.remote[4], po->pppoe_pa.remote[5], dev_name);
993 out:
994 return 0;
997 static __inline__ struct pppox_opt *pppoe_get_idx(loff_t pos)
999 struct pppox_opt *po = NULL;
1000 int i = 0;
1002 for (; i < PPPOE_HASH_SIZE; i++) {
1003 po = item_hash_table[i];
1004 while (po) {
1005 if (!pos--)
1006 goto out;
1007 po = po->next;
1010 out:
1011 return po;
1014 static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos)
1016 loff_t l = *pos;
1018 read_lock_bh(&pppoe_hash_lock);
1019 return l ? pppoe_get_idx(--l) : (void *)1;
1022 static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1024 struct pppox_opt *po;
1026 ++*pos;
1027 if (v == (void *)1) {
1028 po = pppoe_get_idx(0);
1029 goto out;
1031 po = v;
1032 po = po->next;
1033 if (!po) {
1034 int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
1036 while (++hash < PPPOE_HASH_SIZE) {
1037 po = item_hash_table[hash];
1038 if (po)
1039 break;
1042 out:
1043 return po;
1046 static void pppoe_seq_stop(struct seq_file *seq, void *v)
1048 read_unlock_bh(&pppoe_hash_lock);
1051 struct seq_operations pppoe_seq_ops = {
1052 .start = pppoe_seq_start,
1053 .next = pppoe_seq_next,
1054 .stop = pppoe_seq_stop,
1055 .show = pppoe_seq_show,
1058 static int pppoe_seq_open(struct inode *inode, struct file *file)
1060 return seq_open(file, &pppoe_seq_ops);
1063 static struct file_operations pppoe_seq_fops = {
1064 .owner = THIS_MODULE,
1065 .open = pppoe_seq_open,
1066 .read = seq_read,
1067 .llseek = seq_lseek,
1068 .release = seq_release,
1070 #endif /* CONFIG_PROC_FS */
1072 /* ->ioctl are set at pppox_create */
1074 struct proto_ops pppoe_ops = {
1075 .family = AF_PPPOX,
1076 .owner = THIS_MODULE,
1077 .release = pppoe_release,
1078 .bind = sock_no_bind,
1079 .connect = pppoe_connect,
1080 .socketpair = sock_no_socketpair,
1081 .accept = sock_no_accept,
1082 .getname = pppoe_getname,
1083 .poll = datagram_poll,
1084 .listen = sock_no_listen,
1085 .shutdown = sock_no_shutdown,
1086 .setsockopt = sock_no_setsockopt,
1087 .getsockopt = sock_no_getsockopt,
1088 .sendmsg = pppoe_sendmsg,
1089 .recvmsg = pppoe_recvmsg,
1090 .mmap = sock_no_mmap
1093 struct pppox_proto pppoe_proto = {
1094 .create = pppoe_create,
1095 .ioctl = pppoe_ioctl,
1096 .owner = THIS_MODULE,
1100 int __init pppoe_init(void)
1102 int err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
1104 if (err)
1105 goto out;
1106 #ifdef CONFIG_PROC_FS
1108 struct proc_dir_entry *p = create_proc_entry("pppoe", S_IRUGO,
1109 proc_net);
1110 err = -ENOMEM;
1111 if (!p)
1112 goto out_unregister;
1114 p->proc_fops = &pppoe_seq_fops;
1115 err = 0;
1117 #endif /* CONFIG_PROC_FS */
1118 dev_add_pack(&pppoes_ptype);
1119 dev_add_pack(&pppoed_ptype);
1120 register_netdevice_notifier(&pppoe_notifier);
1121 out:
1122 return err;
1123 out_unregister:
1124 unregister_pppox_proto(PX_PROTO_OE);
1125 goto out;
1128 void __exit pppoe_exit(void)
1130 unregister_pppox_proto(PX_PROTO_OE);
1131 dev_remove_pack(&pppoes_ptype);
1132 dev_remove_pack(&pppoed_ptype);
1133 unregister_netdevice_notifier(&pppoe_notifier);
1134 #ifdef CONFIG_PROC_FS
1135 remove_proc_entry("pppoe", proc_net);
1136 #endif
1139 module_init(pppoe_init);
1140 module_exit(pppoe_exit);
1142 MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
1143 MODULE_DESCRIPTION("PPP over Ethernet driver");
1144 MODULE_LICENSE("GPL");