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 skb_pull(skb, sizeof(struct pppoe_hdr));
354 ppp_input(&po->chan, skb);
355 } else if (sk->state & PPPOX_RELAY) {
356 relay_po = get_item_by_addr(&po->pppoe_relay);
358 if (relay_po == NULL)
361 if ((relay_po->sk->state & PPPOX_CONNECTED) == 0)
364 skb_pull(skb, sizeof(struct pppoe_hdr));
365 if (!__pppoe_xmit( relay_po->sk , skb))
368 if (sock_queue_rcv_skb(sk, skb))
372 return NET_RX_SUCCESS;
375 sock_put(relay_po->sk);
382 /************************************************************************
384 * Receive wrapper called in BH context.
386 ***********************************************************************/
387 static int pppoe_rcv(struct sk_buff *skb,
388 struct net_device *dev,
389 struct packet_type *pt)
392 struct pppoe_hdr *ph = (struct pppoe_hdr *) skb->nh.raw;
393 struct pppox_opt *po;
397 po = get_item((unsigned long) ph->sid, skb->mac.ethernet->h_source);
407 /* Socket state is unknown, must put skb into backlog. */
408 if (sk->lock.users != 0) {
409 sk_add_backlog(sk, skb);
410 ret = NET_RX_SUCCESS;
412 ret = pppoe_rcv_core(sk, skb);
421 /************************************************************************
423 * Receive a PPPoE Discovery frame.
424 * This is solely for detection of PADT frames
426 ***********************************************************************/
427 static int pppoe_disc_rcv(struct sk_buff *skb,
428 struct net_device *dev,
429 struct packet_type *pt)
432 struct pppoe_hdr *ph = (struct pppoe_hdr *) skb->nh.raw;
433 struct pppox_opt *po;
435 if (ph->code != PADT_CODE)
438 po = get_item((unsigned long) ph->sid, skb->mac.ethernet->h_source);
440 struct sock *sk = po->sk;
444 /* If the user has locked the socket, just ignore
445 * the packet. With the way two rcv protocols hook into
446 * one socket family type, we cannot (easily) distinguish
447 * what kind of SKB it is during backlog rcv.
449 if (sk->lock.users == 0) {
450 /* We're no longer connect at the PPPOE layer,
451 * and must wait for ppp channel to disconnect us.
453 sk->state = PPPOX_ZOMBIE;
462 return NET_RX_SUCCESS; /* Lies... :-) */
465 struct packet_type pppoes_ptype = {
466 type: __constant_htons(ETH_P_PPP_SES),
470 struct packet_type pppoed_ptype = {
471 type: __constant_htons(ETH_P_PPP_DISC),
472 func: pppoe_disc_rcv,
475 /***********************************************************************
477 * Really kill the socket. (Called from sock_put if refcnt == 0.)
479 **********************************************************************/
480 void pppoe_sock_destruct(struct sock *sk)
482 if (sk->protinfo.destruct_hook)
483 kfree(sk->protinfo.destruct_hook);
488 /***********************************************************************
490 * Initialize a new struct sock.
492 **********************************************************************/
493 static int pppoe_create(struct socket *sock)
500 sk = sk_alloc(PF_PPPOX, GFP_KERNEL, 1);
504 sock_init_data(sock, sk);
506 sock->state = SS_UNCONNECTED;
507 sock->ops = &pppoe_ops;
509 sk->protocol = PX_PROTO_OE;
510 sk->family = PF_PPPOX;
512 sk->backlog_rcv = pppoe_rcv_core;
515 sk->state = PPPOX_NONE;
516 sk->type = SOCK_STREAM;
517 sk->destruct = pppoe_sock_destruct;
519 sk->protinfo.pppox = kmalloc(sizeof(struct pppox_opt), GFP_KERNEL);
520 if (!sk->protinfo.pppox) {
525 memset((void *) sk->protinfo.pppox, 0, sizeof(struct pppox_opt));
526 sk->protinfo.pppox->sk = sk;
528 /* Delete the protinfo when it is time to do so. */
529 sk->protinfo.destruct_hook = sk->protinfo.pppox;
539 int pppoe_release(struct socket *sock)
541 struct sock *sk = sock->sk;
542 struct pppox_opt *po;
551 pppox_unbind_sock(sk);
553 /* Signal the death of the socket. */
554 sk->state = PPPOX_DEAD;
556 po = sk->protinfo.pppox;
557 if (po->pppoe_pa.sid) {
558 delete_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
562 dev_put(po->pppoe_dev);
564 po->pppoe_dev = NULL;
569 skb_queue_purge(&sk->receive_queue);
576 int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
577 int sockaddr_len, int flags)
579 struct sock *sk = sock->sk;
580 struct net_device *dev = NULL;
581 struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
582 struct pppox_opt *po = sk->protinfo.pppox;
588 if (sp->sa_protocol != PX_PROTO_OE)
591 /* Check for already bound sockets */
593 if ((sk->state & PPPOX_CONNECTED) && sp->sa_addr.pppoe.sid)
596 /* Check for already disconnected sockets,
597 on attempts to disconnect */
599 if((sk->state & PPPOX_DEAD) && !sp->sa_addr.pppoe.sid )
603 if (po->pppoe_pa.sid) {
604 pppox_unbind_sock(sk);
606 /* Delete the old binding */
607 delete_item(po->pppoe_pa.sid,po->pppoe_pa.remote);
610 dev_put(po->pppoe_dev);
612 memset(po, 0, sizeof(struct pppox_opt));
615 sk->state = PPPOX_NONE;
618 /* Don't re-bind if sid==0 */
619 if (sp->sa_addr.pppoe.sid != 0) {
620 dev = dev_get_by_name(sp->sa_addr.pppoe.dev);
628 if (!(dev->flags & IFF_UP))
631 memcpy(&po->pppoe_pa,
633 sizeof(struct pppoe_addr));
635 error = set_item(po);
639 po->chan.hdrlen = (sizeof(struct pppoe_hdr) +
640 dev->hard_header_len);
642 po->chan.mtu = dev->mtu - sizeof(struct pppoe_hdr);
643 po->chan.private = sk;
644 po->chan.ops = &pppoe_chan_ops;
646 error = ppp_register_channel(&po->chan);
650 sk->state = PPPOX_CONNECTED;
653 sk->num = sp->sa_addr.pppoe.sid;
660 dev_put(po->pppoe_dev);
661 po->pppoe_dev = NULL;
667 int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
668 int *usockaddr_len, int peer)
670 int len = sizeof(struct sockaddr_pppox);
671 struct sockaddr_pppox sp;
673 sp.sa_family = AF_PPPOX;
674 sp.sa_protocol = PX_PROTO_OE;
675 memcpy(&sp.sa_addr.pppoe, &sock->sk->protinfo.pppox->pppoe_pa,
676 sizeof(struct pppoe_addr));
678 memcpy(uaddr, &sp, len);
680 *usockaddr_len = len;
686 int pppoe_ioctl(struct socket *sock, unsigned int cmd,
689 struct sock *sk = sock->sk;
690 struct pppox_opt *po;
694 po = sk->protinfo.pppox;
699 if (!(sk->state & PPPOX_CONNECTED))
703 if (put_user(po->pppoe_dev->mtu -
704 sizeof(struct pppoe_hdr) -
713 if (!(sk->state & PPPOX_CONNECTED))
717 if (get_user(val,(int *) arg))
720 if (val < (po->pppoe_dev->mtu
721 - sizeof(struct pppoe_hdr)
730 if (get_user(val, (int *) arg))
737 struct pppox_opt *relay_po;
740 if (sk->state & (PPPOX_BOUND|PPPOX_ZOMBIE|PPPOX_DEAD))
744 if (!(sk->state & PPPOX_CONNECTED))
747 /* PPPoE address from the user specifies an outbound
748 PPPoE address to which frames are forwarded to */
750 if (copy_from_user(&po->pppoe_relay,
752 sizeof(struct sockaddr_pppox)))
756 if (po->pppoe_relay.sa_family != AF_PPPOX ||
757 po->pppoe_relay.sa_protocol!= PX_PROTO_OE)
760 /* Check that the socket referenced by the address
762 relay_po = get_item_by_addr(&po->pppoe_relay);
767 sock_put(relay_po->sk);
768 sk->state |= PPPOX_RELAY;
775 if (!(sk->state & PPPOX_RELAY))
778 sk->state &= ~PPPOX_RELAY;
789 int pppoe_sendmsg(struct socket *sock, struct msghdr *m,
790 int total_len, struct scm_cookie *scm)
792 struct sk_buff *skb = NULL;
793 struct sock *sk = sock->sk;
795 struct pppoe_hdr hdr;
796 struct pppoe_hdr *ph;
797 struct net_device *dev;
800 if (sk->dead || !(sk->state & PPPOX_CONNECTED)) {
812 dev = sk->protinfo.pppox->pppoe_dev;
815 if (total_len > (dev->mtu + dev->hard_header_len))
819 skb = sock_wmalloc(sk, total_len + dev->hard_header_len + 32,
826 /* Reserve space for headers. */
827 skb_reserve(skb, dev->hard_header_len);
828 skb->nh.raw = skb->data;
832 skb->priority = sk->priority;
833 skb->protocol = __constant_htons(ETH_P_PPP_SES);
835 ph = (struct pppoe_hdr *) skb_put(skb, total_len + sizeof(struct pppoe_hdr));
836 start = (char *) &ph->tag[0];
838 error = memcpy_fromiovec(start, m->msg_iov, total_len);
846 dev->hard_header(skb, dev, ETH_P_PPP_SES,
847 sk->protinfo.pppox->pppoe_pa.remote,
850 memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
852 ph->length = htons(total_len);
862 /************************************************************************
864 * xmit function for internal use.
866 ***********************************************************************/
867 int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
869 struct net_device *dev = sk->protinfo.pppox->pppoe_dev;
870 struct pppoe_hdr hdr;
871 struct pppoe_hdr *ph;
872 int headroom = skb_headroom(skb);
873 int data_len = skb->len;
874 struct sk_buff *skb2;
876 if (sk->dead || !(sk->state & PPPOX_CONNECTED))
883 hdr.length = htons(skb->len);
888 /* Copy the skb if there is no space for the header. */
889 if (headroom < (sizeof(struct pppoe_hdr) + dev->hard_header_len)) {
890 skb2 = dev_alloc_skb(32+skb->len +
891 sizeof(struct pppoe_hdr) +
892 dev->hard_header_len);
897 skb_reserve(skb2, dev->hard_header_len + sizeof(struct pppoe_hdr));
898 memcpy(skb_put(skb2, skb->len), skb->data, skb->len);
900 /* Make a clone so as to not disturb the original skb,
901 * give dev_queue_xmit something it can free.
903 skb2 = skb_clone(skb, GFP_ATOMIC);
906 ph = (struct pppoe_hdr *) skb_push(skb2, sizeof(struct pppoe_hdr));
907 memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
908 skb2->protocol = __constant_htons(ETH_P_PPP_SES);
910 skb2->nh.raw = skb2->data;
914 dev->hard_header(skb2, dev, ETH_P_PPP_SES,
915 sk->protinfo.pppox->pppoe_pa.remote,
918 /* We're transmitting skb2, and assuming that dev_queue_xmit
919 * will free it. The generic ppp layer however, is expecting
920 * that we give back 'skb' (not 'skb2') in case of failure,
921 * but free it in case of success.
924 if (dev_queue_xmit(skb2) < 0)
935 /************************************************************************
937 * xmit function called by generic PPP driver
938 * sends PPP frame over PPPoE socket
940 ***********************************************************************/
941 int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
943 struct sock *sk = (struct sock *) chan->private;
944 return __pppoe_xmit(sk, skb);
948 struct ppp_channel_ops pppoe_chan_ops = { pppoe_xmit , NULL };
950 int pppoe_rcvmsg(struct socket *sock, struct msghdr *m, int total_len, int flags, struct scm_cookie *scm)
952 struct sock *sk = sock->sk;
953 struct sk_buff *skb = NULL;
956 struct pppoe_hdr *ph = NULL;
958 if (sk->state & PPPOX_BOUND) {
963 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
964 flags & MSG_DONTWAIT, &error);
974 ph = (struct pppoe_hdr *) skb->nh.raw;
975 len = ntohs(ph->length);
977 error = memcpy_toiovec(m->msg_iov, (unsigned char *) &ph->tag[0], len);
990 int pppoe_proc_info(char *buffer, char **start, off_t offset, int length)
992 struct pppox_opt *po;
999 len += sprintf(buffer,
1000 "Id Address Device\n");
1003 write_lock_bh(&pppoe_hash_lock);
1005 for (i = 0; i < PPPOE_HASH_SIZE; i++) {
1006 po = item_hash_table[i];
1008 char *dev = po->pppoe_pa.dev;
1010 size = sprintf(buffer + len,
1011 "%08X %02X:%02X:%02X:%02X:%02X:%02X %8s\n",
1013 po->pppoe_pa.remote[0],
1014 po->pppoe_pa.remote[1],
1015 po->pppoe_pa.remote[2],
1016 po->pppoe_pa.remote[3],
1017 po->pppoe_pa.remote[4],
1018 po->pppoe_pa.remote[5],
1027 if (pos > offset + length)
1036 write_unlock_bh(&pppoe_hash_lock);
1038 *start = buffer + (offset - begin);
1039 len -= (offset - begin);
1048 struct proto_ops pppoe_ops = {
1050 release: pppoe_release,
1052 connect: pppoe_connect,
1053 socketpair: sock_no_socketpair,
1054 accept: sock_no_accept,
1055 getname: pppoe_getname,
1056 poll: datagram_poll,
1058 listen: sock_no_listen,
1059 shutdown: sock_no_shutdown,
1060 setsockopt: sock_no_setsockopt,
1061 getsockopt: sock_no_getsockopt,
1062 sendmsg: pppoe_sendmsg,
1063 recvmsg: pppoe_rcvmsg,
1067 struct pppox_proto pppoe_proto = {
1068 create: pppoe_create,
1073 int __init pppoe_init(void)
1075 int err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
1078 dev_add_pack(&pppoes_ptype);
1079 dev_add_pack(&pppoed_ptype);
1080 register_netdevice_notifier(&pppoe_notifier);
1081 proc_net_create("pppoe", 0, pppoe_proc_info);
1086 void __exit pppoe_exit(void)
1088 unregister_pppox_proto(PX_PROTO_OE);
1089 dev_remove_pack(&pppoes_ptype);
1090 dev_remove_pack(&pppoed_ptype);
1091 unregister_netdevice_notifier(&pppoe_notifier);
1092 proc_net_remove("pppoe");
1095 module_init(pppoe_init);
1096 module_exit(pppoe_exit);
1098 MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
1099 MODULE_DESCRIPTION("PPP over Ethernet driver");
1100 MODULE_LICENSE("GPL");