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)
10 * 030700 : Fixed connect logic to allow for disconnect.
11 * 270700 : Fixed potential SMP problems; we must protect against
12 * simultaneous invocation of ppp_input
13 * and ppp_unregister_channel.
14 * 040800 : Respect reference count mechanisms on net-devices.
15 * 200800 : fix kfree(skb) in pppoe_rcv (acme)
16 * Module reference count is decremented in the right spot now,
17 * guards against sock_put not actually freeing the sk
19 * 051000 : Initialization cleanup.
20 * 111100 : Fix recvmsg.
21 * 050101 : Fix PADT procesing.
22 * 140501 : Use pppoe_rcv_core to handle all backlog. (Alexey)
23 * 170701 : Do not lock_sock with rwlock held. (DaveM)
24 * Ignore discovery frames if user has socket
26 * Ignore return value of dev_queue_xmit in __pppoe_xmit
27 * or else we may kfree an SKB twice. (DaveM)
28 * 190701 : When doing copies of skb's in __pppoe_xmit, always delete
29 * the original skb that was passed in on success, never on
30 * failure. Delete the copy of the skb on failure to avoid
32 * 081001 : Misc. cleanup (licence string, non-blocking, prevent
33 * reference of device on close).
34 * 121301 : New ppp channels interface; cannot unregister a channel
35 * from interrupts. Thus, we mark the socket as a ZOMBIE
36 * and do the unregistration later.
37 * 071502 : When a connection is being torn down, we must remember that
38 * ZOMBIE state connections are still connected and thus
39 * pppox_unbind_sock must unbind them (in pppoe_release).
41 * Author: Michal Ostrowski <mostrows@speakeasy.net>
43 * Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
44 * David S. Miller (davem@redhat.com)
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>
57 #include <asm/uaccess.h>
59 #include <linux/kernel.h>
60 #include <linux/sched.h>
61 #include <linux/slab.h>
62 #include <linux/errno.h>
64 #include <linux/netdevice.h>
65 #include <linux/net.h>
66 #include <linux/inetdevice.h>
67 #include <linux/etherdevice.h>
68 #include <linux/skbuff.h>
69 #include <linux/init.h>
70 #include <linux/if_ether.h>
71 #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/if_pppvar.h>
77 #include <linux/notifier.h>
78 #include <linux/file.h>
79 #include <linux/proc_fs.h>
83 static int __attribute__((unused)) pppoe_debug = 7;
84 #define PPPOE_HASH_BITS 4
85 #define PPPOE_HASH_SIZE (1<<PPPOE_HASH_BITS)
87 int pppoe_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
88 int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb);
89 int __pppoe_xmit(struct sock *sk, struct sk_buff *skb);
91 struct proto_ops pppoe_ops;
95 #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)
96 #define DEBUG(s,args...) do { if( pppoe_debug & (s) ) printk(KERN_CRIT args ); } while (0)
98 #define CHECKPTR(x,y) do { } while (0)
99 #define DEBUG(s,args...) do { } while (0)
104 static rwlock_t pppoe_hash_lock = RW_LOCK_UNLOCKED;
107 static inline int cmp_2_addr(struct pppoe_addr *a, struct pppoe_addr *b)
109 return (a->sid == b->sid &&
110 (memcmp(a->remote, b->remote, ETH_ALEN) == 0));
113 static inline int cmp_addr(struct pppoe_addr *a, unsigned long sid, char *addr)
115 return (a->sid == sid &&
116 (memcmp(a->remote,addr,ETH_ALEN) == 0));
119 static int hash_item(unsigned long sid, unsigned char *addr)
124 for (i = 0; i < ETH_ALEN ; ++i) {
125 for (j = 0; j < 8/PPPOE_HASH_BITS ; ++j) {
126 hash ^= addr[i] >> ( j * PPPOE_HASH_BITS );
130 for (i = 0; i < (sizeof(unsigned long)*8) / PPPOE_HASH_BITS ; ++i)
131 hash ^= sid >> (i*PPPOE_HASH_BITS);
133 return hash & ( PPPOE_HASH_SIZE - 1 );
136 static struct pppox_opt *item_hash_table[PPPOE_HASH_SIZE] = { 0, };
138 /**********************************************************************
140 * Set/get/delete/rehash items (internal versions)
142 **********************************************************************/
143 static struct pppox_opt *__get_item(unsigned long sid, unsigned char *addr)
145 int hash = hash_item(sid, addr);
146 struct pppox_opt *ret;
148 ret = item_hash_table[hash];
150 while (ret && !cmp_addr(&ret->pppoe_pa, sid, addr))
156 static int __set_item(struct pppox_opt *po)
158 int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
159 struct pppox_opt *ret;
161 ret = item_hash_table[hash];
163 if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa))
170 po->next = item_hash_table[hash];
171 item_hash_table[hash] = po;
177 static struct pppox_opt *__delete_item(unsigned long sid, char *addr)
179 int hash = hash_item(sid, addr);
180 struct pppox_opt *ret, **src;
182 ret = item_hash_table[hash];
183 src = &item_hash_table[hash];
186 if (cmp_addr(&ret->pppoe_pa, sid, addr)) {
198 /**********************************************************************
200 * Set/get/delete/rehash items
202 **********************************************************************/
203 static inline struct pppox_opt *get_item(unsigned long sid,
206 struct pppox_opt *po;
208 read_lock_bh(&pppoe_hash_lock);
209 po = __get_item(sid, addr);
212 read_unlock_bh(&pppoe_hash_lock);
217 static inline struct pppox_opt *get_item_by_addr(struct sockaddr_pppox *sp)
219 return get_item(sp->sa_addr.pppoe.sid, sp->sa_addr.pppoe.remote);
222 static inline int set_item(struct pppox_opt *po)
229 write_lock_bh(&pppoe_hash_lock);
231 write_unlock_bh(&pppoe_hash_lock);
236 static inline struct pppox_opt *delete_item(unsigned long sid, char *addr)
238 struct pppox_opt *ret;
240 write_lock_bh(&pppoe_hash_lock);
241 ret = __delete_item(sid, addr);
242 write_unlock_bh(&pppoe_hash_lock);
249 /***************************************************************************
251 * Handler for device events.
252 * Certain device events require that sockets be unconnected.
254 **************************************************************************/
256 static void pppoe_flush_dev(struct net_device *dev)
263 read_lock_bh(&pppoe_hash_lock);
264 for (hash = 0; hash < PPPOE_HASH_SIZE; hash++) {
265 struct pppox_opt *po = item_hash_table[hash];
268 if (po->pppoe_dev == dev) {
269 struct sock *sk = po->sk;
272 po->pppoe_dev = NULL;
274 /* We hold a reference to SK, now drop the
275 * hash table lock so that we may attempt
276 * to lock the socket (which can sleep).
278 read_unlock_bh(&pppoe_hash_lock);
282 if (sk->state & (PPPOX_CONNECTED|PPPOX_BOUND)){
283 pppox_unbind_sock(sk);
285 sk->state = PPPOX_ZOMBIE;
286 sk->state_change(sk);
293 read_lock_bh(&pppoe_hash_lock);
295 /* Now restart from the beginning of this
296 * hash chain. We always NULL out pppoe_dev
297 * so we are guarenteed to make forward
300 po = item_hash_table[hash];
306 read_unlock_bh(&pppoe_hash_lock);
309 static int pppoe_device_event(struct notifier_block *this,
310 unsigned long event, void *ptr)
312 struct net_device *dev = (struct net_device *) ptr;
314 /* Only look at sockets that are using this specific device. */
316 case NETDEV_CHANGEMTU:
317 /* A change in mtu is a bad thing, requiring
318 * LCP re-negotiation.
321 case NETDEV_GOING_DOWN:
323 /* Find every socket on this device and kill it. */
324 pppoe_flush_dev(dev);
335 static struct notifier_block pppoe_notifier = {
336 notifier_call: pppoe_device_event,
342 /************************************************************************
344 * Do the real work of receiving a PPPoE Session frame.
346 ***********************************************************************/
347 int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb)
349 struct pppox_opt *po = sk->protinfo.pppox;
350 struct pppox_opt *relay_po = NULL;
352 if (sk->state & PPPOX_BOUND) {
353 ppp_input(&po->chan, skb);
354 } else if (sk->state & PPPOX_RELAY) {
355 relay_po = get_item_by_addr(&po->pppoe_relay);
357 if (relay_po == NULL)
360 if ((relay_po->sk->state & PPPOX_CONNECTED) == 0)
363 if (!__pppoe_xmit( relay_po->sk , skb))
366 if (sock_queue_rcv_skb(sk, skb))
370 return NET_RX_SUCCESS;
373 sock_put(relay_po->sk);
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 int len = ntohs(ph->length);
392 struct pppox_opt *po;
396 skb_pull(skb, sizeof(*ph));
400 po = get_item((unsigned long) ph->sid, skb->mac.ethernet->h_source);
404 if (pskb_trim(skb, len))
410 /* Socket state is unknown, must put skb into backlog. */
411 if (sk->lock.users != 0) {
412 sk_add_backlog(sk, skb);
413 ret = NET_RX_SUCCESS;
415 ret = pppoe_rcv_core(sk, skb);
427 /************************************************************************
429 * Receive a PPPoE Discovery frame.
430 * This is solely for detection of PADT frames
432 ***********************************************************************/
433 static int pppoe_disc_rcv(struct sk_buff *skb,
434 struct net_device *dev,
435 struct packet_type *pt)
438 struct pppoe_hdr *ph = (struct pppoe_hdr *) skb->nh.raw;
439 struct pppox_opt *po;
441 if (ph->code != PADT_CODE)
444 po = get_item((unsigned long) ph->sid, skb->mac.ethernet->h_source);
446 struct sock *sk = po->sk;
450 /* If the user has locked the socket, just ignore
451 * the packet. With the way two rcv protocols hook into
452 * one socket family type, we cannot (easily) distinguish
453 * what kind of SKB it is during backlog rcv.
455 if (sk->lock.users == 0) {
456 /* We're no longer connect at the PPPOE layer,
457 * and must wait for ppp channel to disconnect us.
459 sk->state = PPPOX_ZOMBIE;
468 return NET_RX_SUCCESS; /* Lies... :-) */
471 struct packet_type pppoes_ptype = {
472 type: __constant_htons(ETH_P_PPP_SES),
476 struct packet_type pppoed_ptype = {
477 type: __constant_htons(ETH_P_PPP_DISC),
478 func: pppoe_disc_rcv,
481 /***********************************************************************
483 * Really kill the socket. (Called from sock_put if refcnt == 0.)
485 **********************************************************************/
486 void pppoe_sock_destruct(struct sock *sk)
488 if (sk->protinfo.destruct_hook)
489 kfree(sk->protinfo.destruct_hook);
494 /***********************************************************************
496 * Initialize a new struct sock.
498 **********************************************************************/
499 static int pppoe_create(struct socket *sock)
506 sk = sk_alloc(PF_PPPOX, GFP_KERNEL, 1);
510 sock_init_data(sock, sk);
512 sock->state = SS_UNCONNECTED;
513 sock->ops = &pppoe_ops;
515 sk->protocol = PX_PROTO_OE;
516 sk->family = PF_PPPOX;
518 sk->backlog_rcv = pppoe_rcv_core;
521 sk->state = PPPOX_NONE;
522 sk->type = SOCK_STREAM;
523 sk->destruct = pppoe_sock_destruct;
525 sk->protinfo.pppox = kmalloc(sizeof(struct pppox_opt), GFP_KERNEL);
526 if (!sk->protinfo.pppox) {
531 memset((void *) sk->protinfo.pppox, 0, sizeof(struct pppox_opt));
532 sk->protinfo.pppox->sk = sk;
534 /* Delete the protinfo when it is time to do so. */
535 sk->protinfo.destruct_hook = sk->protinfo.pppox;
545 int pppoe_release(struct socket *sock)
547 struct sock *sk = sock->sk;
548 struct pppox_opt *po;
557 pppox_unbind_sock(sk);
559 /* Signal the death of the socket. */
560 sk->state = PPPOX_DEAD;
562 po = sk->protinfo.pppox;
563 if (po->pppoe_pa.sid) {
564 delete_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
568 dev_put(po->pppoe_dev);
570 po->pppoe_dev = NULL;
575 skb_queue_purge(&sk->receive_queue);
582 int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
583 int sockaddr_len, int flags)
585 struct sock *sk = sock->sk;
586 struct net_device *dev = NULL;
587 struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
588 struct pppox_opt *po = sk->protinfo.pppox;
594 if (sp->sa_protocol != PX_PROTO_OE)
597 /* Check for already bound sockets */
599 if ((sk->state & PPPOX_CONNECTED) && sp->sa_addr.pppoe.sid)
602 /* Check for already disconnected sockets,
603 on attempts to disconnect */
605 if((sk->state & PPPOX_DEAD) && !sp->sa_addr.pppoe.sid )
609 if (po->pppoe_pa.sid) {
610 pppox_unbind_sock(sk);
612 /* Delete the old binding */
613 delete_item(po->pppoe_pa.sid,po->pppoe_pa.remote);
616 dev_put(po->pppoe_dev);
618 memset(po, 0, sizeof(struct pppox_opt));
621 sk->state = PPPOX_NONE;
624 /* Don't re-bind if sid==0 */
625 if (sp->sa_addr.pppoe.sid != 0) {
626 dev = dev_get_by_name(sp->sa_addr.pppoe.dev);
634 if (!(dev->flags & IFF_UP))
637 memcpy(&po->pppoe_pa,
639 sizeof(struct pppoe_addr));
641 error = set_item(po);
645 po->chan.hdrlen = (sizeof(struct pppoe_hdr) +
646 dev->hard_header_len);
648 po->chan.mtu = dev->mtu - sizeof(struct pppoe_hdr);
649 po->chan.private = sk;
650 po->chan.ops = &pppoe_chan_ops;
652 error = ppp_register_channel(&po->chan);
656 sk->state = PPPOX_CONNECTED;
659 sk->num = sp->sa_addr.pppoe.sid;
666 dev_put(po->pppoe_dev);
667 po->pppoe_dev = NULL;
673 int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
674 int *usockaddr_len, int peer)
676 int len = sizeof(struct sockaddr_pppox);
677 struct sockaddr_pppox sp;
679 sp.sa_family = AF_PPPOX;
680 sp.sa_protocol = PX_PROTO_OE;
681 memcpy(&sp.sa_addr.pppoe, &sock->sk->protinfo.pppox->pppoe_pa,
682 sizeof(struct pppoe_addr));
684 memcpy(uaddr, &sp, len);
686 *usockaddr_len = len;
692 int pppoe_ioctl(struct socket *sock, unsigned int cmd,
695 struct sock *sk = sock->sk;
696 struct pppox_opt *po;
700 po = sk->protinfo.pppox;
705 if (!(sk->state & PPPOX_CONNECTED))
709 if (put_user(po->pppoe_dev->mtu -
710 sizeof(struct pppoe_hdr) -
719 if (!(sk->state & PPPOX_CONNECTED))
723 if (get_user(val,(int *) arg))
726 if (val < (po->pppoe_dev->mtu
727 - sizeof(struct pppoe_hdr)
736 if (get_user(val, (int *) arg))
743 struct pppox_opt *relay_po;
746 if (sk->state & (PPPOX_BOUND|PPPOX_ZOMBIE|PPPOX_DEAD))
750 if (!(sk->state & PPPOX_CONNECTED))
753 /* PPPoE address from the user specifies an outbound
754 PPPoE address to which frames are forwarded to */
756 if (copy_from_user(&po->pppoe_relay,
758 sizeof(struct sockaddr_pppox)))
762 if (po->pppoe_relay.sa_family != AF_PPPOX ||
763 po->pppoe_relay.sa_protocol!= PX_PROTO_OE)
766 /* Check that the socket referenced by the address
768 relay_po = get_item_by_addr(&po->pppoe_relay);
773 sock_put(relay_po->sk);
774 sk->state |= PPPOX_RELAY;
781 if (!(sk->state & PPPOX_RELAY))
784 sk->state &= ~PPPOX_RELAY;
795 int pppoe_sendmsg(struct socket *sock, struct msghdr *m,
796 int total_len, struct scm_cookie *scm)
798 struct sk_buff *skb = NULL;
799 struct sock *sk = sock->sk;
801 struct pppoe_hdr hdr;
802 struct pppoe_hdr *ph;
803 struct net_device *dev;
806 if (sk->dead || !(sk->state & PPPOX_CONNECTED)) {
818 dev = sk->protinfo.pppox->pppoe_dev;
821 if (total_len > (dev->mtu + dev->hard_header_len))
825 skb = sock_wmalloc(sk, total_len + dev->hard_header_len + 32,
832 /* Reserve space for headers. */
833 skb_reserve(skb, dev->hard_header_len);
834 skb->nh.raw = skb->data;
838 skb->priority = sk->priority;
839 skb->protocol = __constant_htons(ETH_P_PPP_SES);
841 ph = (struct pppoe_hdr *) skb_put(skb, total_len + sizeof(struct pppoe_hdr));
842 start = (char *) &ph->tag[0];
844 error = memcpy_fromiovec(start, m->msg_iov, total_len);
852 dev->hard_header(skb, dev, ETH_P_PPP_SES,
853 sk->protinfo.pppox->pppoe_pa.remote,
856 memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
858 ph->length = htons(total_len);
868 /************************************************************************
870 * xmit function for internal use.
872 ***********************************************************************/
873 int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
875 struct net_device *dev = sk->protinfo.pppox->pppoe_dev;
876 struct pppoe_hdr hdr;
877 struct pppoe_hdr *ph;
878 int headroom = skb_headroom(skb);
879 int data_len = skb->len;
880 struct sk_buff *skb2;
882 if (sk->dead || !(sk->state & PPPOX_CONNECTED))
889 hdr.length = htons(skb->len);
894 /* Copy the skb if there is no space for the header. */
895 if (headroom < (sizeof(struct pppoe_hdr) + dev->hard_header_len)) {
896 skb2 = dev_alloc_skb(32+skb->len +
897 sizeof(struct pppoe_hdr) +
898 dev->hard_header_len);
903 skb_reserve(skb2, dev->hard_header_len + sizeof(struct pppoe_hdr));
904 memcpy(skb_put(skb2, skb->len), skb->data, skb->len);
906 /* Make a clone so as to not disturb the original skb,
907 * give dev_queue_xmit something it can free.
909 skb2 = skb_clone(skb, GFP_ATOMIC);
915 ph = (struct pppoe_hdr *) skb_push(skb2, sizeof(struct pppoe_hdr));
916 memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
917 skb2->protocol = __constant_htons(ETH_P_PPP_SES);
919 skb2->nh.raw = skb2->data;
923 dev->hard_header(skb2, dev, ETH_P_PPP_SES,
924 sk->protinfo.pppox->pppoe_pa.remote,
927 /* We're transmitting skb2, and assuming that dev_queue_xmit
928 * will free it. The generic ppp layer however, is expecting
929 * that we give back 'skb' (not 'skb2') in case of failure,
930 * but free it in case of success.
933 if (dev_queue_xmit(skb2) < 0)
944 /************************************************************************
946 * xmit function called by generic PPP driver
947 * sends PPP frame over PPPoE socket
949 ***********************************************************************/
950 int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
952 struct sock *sk = (struct sock *) chan->private;
953 return __pppoe_xmit(sk, skb);
957 struct ppp_channel_ops pppoe_chan_ops = { pppoe_xmit , NULL };
959 int pppoe_rcvmsg(struct socket *sock, struct msghdr *m, int total_len, int flags, struct scm_cookie *scm)
961 struct sock *sk = sock->sk;
962 struct sk_buff *skb = NULL;
965 if (sk->state & PPPOX_BOUND) {
970 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
971 flags & MSG_DONTWAIT, &error);
980 total_len = min_t(int, total_len, skb->len);
981 error = skb_copy_datagram_iovec(skb, 0, m->msg_iov, total_len);
992 int pppoe_proc_info(char *buffer, char **start, off_t offset, int length)
994 struct pppox_opt *po;
1001 len += sprintf(buffer,
1002 "Id Address Device\n");
1005 write_lock_bh(&pppoe_hash_lock);
1007 for (i = 0; i < PPPOE_HASH_SIZE; i++) {
1008 po = item_hash_table[i];
1010 char *dev = po->pppoe_pa.dev;
1012 size = sprintf(buffer + len,
1013 "%08X %02X:%02X:%02X:%02X:%02X:%02X %8s\n",
1015 po->pppoe_pa.remote[0],
1016 po->pppoe_pa.remote[1],
1017 po->pppoe_pa.remote[2],
1018 po->pppoe_pa.remote[3],
1019 po->pppoe_pa.remote[4],
1020 po->pppoe_pa.remote[5],
1029 if (pos > offset + length)
1038 write_unlock_bh(&pppoe_hash_lock);
1040 *start = buffer + (offset - begin);
1041 len -= (offset - begin);
1050 struct proto_ops pppoe_ops = {
1052 release: pppoe_release,
1054 connect: pppoe_connect,
1055 socketpair: sock_no_socketpair,
1056 accept: sock_no_accept,
1057 getname: pppoe_getname,
1058 poll: datagram_poll,
1060 listen: sock_no_listen,
1061 shutdown: sock_no_shutdown,
1062 setsockopt: sock_no_setsockopt,
1063 getsockopt: sock_no_getsockopt,
1064 sendmsg: pppoe_sendmsg,
1065 recvmsg: pppoe_rcvmsg,
1069 struct pppox_proto pppoe_proto = {
1070 create: pppoe_create,
1075 int __init pppoe_init(void)
1077 int err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
1080 dev_add_pack(&pppoes_ptype);
1081 dev_add_pack(&pppoed_ptype);
1082 register_netdevice_notifier(&pppoe_notifier);
1083 proc_net_create("pppoe", 0, pppoe_proc_info);
1088 void __exit pppoe_exit(void)
1090 unregister_pppox_proto(PX_PROTO_OE);
1091 dev_remove_pack(&pppoes_ptype);
1092 dev_remove_pack(&pppoed_ptype);
1093 unregister_netdevice_notifier(&pppoe_notifier);
1094 proc_net_remove("pppoe");
1097 module_init(pppoe_init);
1098 module_exit(pppoe_exit);
1100 MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
1101 MODULE_DESCRIPTION("PPP over Ethernet driver");
1102 MODULE_LICENSE("GPL");