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.
15 * Module reference count is decremented in the right spot now,
16 * guards against sock_put not actually freeing the sk
19 * Author: Michal Ostrowski <mostrows@styx.uwaterloo.ca>
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License
24 * as published by the Free Software Foundation; either version
25 * 2 of the License, or (at your option) any later version.
29 #include <linux/string.h>
30 #include <linux/module.h>
32 #include <asm/uaccess.h>
34 #include <linux/kernel.h>
35 #include <linux/sched.h>
36 #include <linux/malloc.h>
37 #include <linux/errno.h>
39 #include <linux/netdevice.h>
40 #include <linux/net.h>
41 #include <linux/inetdevice.h>
42 #include <linux/etherdevice.h>
43 #include <linux/skbuff.h>
44 #include <linux/init.h>
45 #include <linux/if_ether.h>
46 #include <linux/if_pppox.h>
48 #include <linux/ppp_channel.h>
49 #include <linux/ppp_defs.h>
50 #include <linux/if_ppp.h>
51 #include <linux/if_pppvar.h>
52 #include <linux/notifier.h>
53 #include <linux/file.h>
54 #include <linux/proc_fs.h>
58 static int __attribute__((unused
)) pppoe_debug
= 7;
59 #define PPPOE_HASH_BITS 4
60 #define PPPOE_HASH_SIZE (1<<PPPOE_HASH_BITS)
62 int pppoe_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
);
63 int pppoe_xmit(struct ppp_channel
*chan
, struct sk_buff
*skb
);
64 int __pppoe_xmit(struct sock
*sk
, struct sk_buff
*skb
);
66 struct proto_ops pppoe_ops
;
70 #define CHECKPTR(x,y) { if (!(x) && pppoe_debug &7 ){ printk(KERN_CRIT "PPPoE Invalid pointer : %s , %p\n",#x,(x)); error=-EINVAL; goto y; }}
71 #define DEBUG(s,args...) if( pppoe_debug & (s) ) printk(KERN_CRIT args );
73 #define CHECKPTR(x,y) do {} while (0);
74 #define DEBUG(s,args...) do { } while (0);
79 static rwlock_t pppoe_hash_lock
= RW_LOCK_UNLOCKED
;
82 static inline int cmp_2_addr(struct pppoe_addr
*a
, struct pppoe_addr
*b
)
84 return (a
->sid
== b
->sid
&&
85 (memcmp(a
->remote
, b
->remote
, ETH_ALEN
) == 0));
88 static inline int cmp_addr(struct pppoe_addr
*a
, unsigned long sid
, char *addr
)
90 return (a
->sid
== sid
&&
91 (memcmp(a
->remote
,addr
,ETH_ALEN
) == 0));
94 static int hash_item(unsigned long sid
, unsigned char *addr
)
98 for (i
= 0; i
< ETH_ALEN
; ++i
){
99 for (j
= 0; j
< 8/PPPOE_HASH_BITS
; ++j
){
100 hash
^= addr
[i
] >> ( j
* PPPOE_HASH_BITS
);
104 for (i
= 0; i
< (sizeof(unsigned long)*8) / PPPOE_HASH_BITS
; ++i
)
105 hash
^= sid
>> (i
*PPPOE_HASH_BITS
);
107 return hash
& ( PPPOE_HASH_SIZE
- 1 );
110 static struct pppox_opt
*item_hash_table
[PPPOE_HASH_SIZE
] = { 0, };
112 /**********************************************************************
114 * Set/get/delete/rehash items (internal versions)
116 **********************************************************************/
117 static struct pppox_opt
*__get_item(unsigned long sid
, unsigned char *addr
)
119 int hash
= hash_item(sid
, addr
);
120 struct pppox_opt
*ret
;
122 ret
= item_hash_table
[hash
];
124 while (ret
&& !cmp_addr(&ret
->pppoe_pa
, sid
, addr
))
130 static int __set_item(struct pppox_opt
*po
)
132 int hash
= hash_item(po
->pppoe_pa
.sid
, po
->pppoe_pa
.remote
);
133 struct pppox_opt
*ret
;
135 ret
= item_hash_table
[hash
];
137 if (cmp_2_addr(&ret
->pppoe_pa
, &po
->pppoe_pa
))
144 po
->next
= item_hash_table
[hash
];
145 item_hash_table
[hash
] = po
;
151 static struct pppox_opt
*__delete_item(unsigned long sid
, char *addr
)
153 int hash
= hash_item(sid
, addr
);
154 struct pppox_opt
*ret
, **src
;
156 ret
= item_hash_table
[hash
];
157 src
= &item_hash_table
[hash
];
160 if (cmp_addr(&ret
->pppoe_pa
, sid
, addr
)) {
172 static struct pppox_opt
*__find_on_dev(struct net_device
*dev
,
173 struct pppox_opt
*start
)
175 struct pppox_opt
*po
;
179 hash
= hash_item(start
->pppoe_pa
.sid
, start
->pppoe_pa
.remote
);
185 while (!po
&& ++hash
< PPPOE_HASH_SIZE
)
186 po
= item_hash_table
[hash
];
189 while (po
&& (po
->pppoe_dev
!= dev
)){
194 while (!po
&& ++hash
< PPPOE_HASH_SIZE
)
195 po
= item_hash_table
[hash
];
202 /**********************************************************************
204 * Set/get/delete/rehash items
206 **********************************************************************/
207 static inline struct pppox_opt
*get_item(unsigned long sid
,
210 struct pppox_opt
*po
;
212 read_lock_bh(&pppoe_hash_lock
);
213 po
= __get_item(sid
, addr
);
216 read_unlock_bh(&pppoe_hash_lock
);
221 static inline struct pppox_opt
*get_item_by_addr(struct sockaddr_pppox
*sp
)
223 return get_item(sp
->sa_addr
.pppoe
.sid
, sp
->sa_addr
.pppoe
.remote
);
226 static inline int set_item(struct pppox_opt
*po
)
233 write_lock_bh(&pppoe_hash_lock
);
235 write_unlock_bh(&pppoe_hash_lock
);
240 static inline struct pppox_opt
*delete_item(unsigned long sid
, char *addr
)
242 struct pppox_opt
*ret
;
244 write_lock_bh(&pppoe_hash_lock
);
245 ret
= __delete_item(sid
, addr
);
246 write_unlock_bh(&pppoe_hash_lock
);
251 static struct pppox_opt
*find_on_dev(struct net_device
*dev
,
252 struct pppox_opt
*start
)
254 struct pppox_opt
*po
;
255 read_lock_bh(&pppoe_hash_lock
);
256 po
= __find_on_dev(dev
,start
);
259 read_unlock_bh(&pppoe_hash_lock
);
263 /***************************************************************************
265 * Handler for device events.
266 * Certain device events require that sockets be unconnected.
268 **************************************************************************/
269 static int pppoe_device_event(struct notifier_block
*this,
270 unsigned long event
, void *ptr
)
272 int error
= NOTIFY_DONE
;
273 struct net_device
*dev
= (struct net_device
*) ptr
;
274 struct pppox_opt
*po
= NULL
;
276 /* Only look at sockets that are using this specific device. */
278 case NETDEV_CHANGEMTU
:
279 /* A change in mtu is a bad thing, requiring
280 * LCP re-negotiation.
282 case NETDEV_GOING_DOWN
:
285 po
= find_on_dev(dev
, po
);
290 if (po
->sk
->state
& PPPOX_CONNECTED
)
291 pppox_unbind_sock(po
->sk
);
293 release_sock(po
->sk
);
307 static struct notifier_block pppoe_notifier
= {
316 /************************************************************************
318 * Do the real work of receiving a PPPoE Session frame.
320 ***********************************************************************/
321 int pppoe_rcv_core(struct sock
*sk
, struct sk_buff
*skb
){
322 struct pppox_opt
*po
=sk
->protinfo
.pppox
;
324 if (sk
->state
& PPPOX_BOUND
) {
325 skb_pull(skb
, sizeof(struct pppoe_hdr
));
327 ppp_input(&po
->chan
, skb
);
328 } else if( sk
->state
& PPPOX_RELAY
){
329 struct pppox_opt
*relay_po
;
331 relay_po
= get_item_by_addr( &po
->pppoe_relay
);
333 if( relay_po
== NULL
||
334 !( relay_po
->sk
->state
& PPPOX_CONNECTED
) ){
335 sock_put(relay_po
->sk
);
339 skb_pull(skb
, sizeof(struct pppoe_hdr
));
340 if( !__pppoe_xmit( relay_po
->sk
, skb
) ){
341 sock_put(relay_po
->sk
);
346 sock_queue_rcv_skb(sk
, skb
);
358 /************************************************************************
360 * Receive wrapper called in BH context.
362 ***********************************************************************/
363 static int pppoe_rcv(struct sk_buff
*skb
,
364 struct net_device
*dev
,
365 struct packet_type
*pt
)
368 struct pppoe_hdr
*ph
= (struct pppoe_hdr
*) skb
->nh
.raw
;
369 struct pppox_opt
*po
;
373 po
= get_item((unsigned long) ph
->sid
, skb
->mac
.ethernet
->h_source
);
383 /* Socket state is unknown, must put skb into backlog. */
384 if( sk
->lock
.users
!= 0 ){
385 sk_add_backlog( sk
, skb
);
388 ret
= pppoe_rcv_core(sk
, skb
);
397 /************************************************************************
399 * Receive wrapper called in process context.
401 ***********************************************************************/
402 int pppoe_backlog_rcv(struct sock
*sk
, struct sk_buff
*skb
)
405 pppoe_rcv_core(sk
, skb
);
412 /************************************************************************
414 * Receive a PPPoE Discovery frame.
415 * This is solely for detection of PADT frames
417 ***********************************************************************/
418 static int pppoe_disc_rcv(struct sk_buff
*skb
,
419 struct net_device
*dev
,
420 struct packet_type
*pt
)
423 struct pppoe_hdr
*ph
= (struct pppoe_hdr
*) skb
->nh
.raw
;
424 struct pppox_opt
*po
;
425 struct sock
*sk
= NULL
;
427 if (ph
->code
!= PADT_CODE
)
430 po
= get_item((unsigned long) ph
->sid
, skb
->mac
.ethernet
->h_source
);
437 pppox_unbind_sock(sk
);
449 struct packet_type pppoes_ptype
= {
450 __constant_htons(ETH_P_PPP_SES
),
457 struct packet_type pppoed_ptype
= {
458 __constant_htons(ETH_P_PPP_DISC
),
465 /**********************************************************************
467 * The destruct hook --- this can be trashed if there is no need for
468 * the sock to clear its receive queue?
470 *********************************************************************/
471 void sock_pppoe_destruct(struct sock
*sk
)
473 if (sk
->protinfo
.destruct_hook
)
474 kfree(sk
->protinfo
.destruct_hook
);
479 /***********************************************************************
481 * Initialize a new struct sock.
483 **********************************************************************/
484 static int pppoe_create(struct socket
*sock
)
491 sk
= sk_alloc(PF_PPPOX
, GFP_KERNEL
, 1);
495 sock_init_data(sock
, sk
);
497 sock
->state
= SS_UNCONNECTED
;
498 sock
->ops
= &pppoe_ops
;
500 sk
->protocol
= PX_PROTO_OE
;
501 sk
->family
= PF_PPPOX
;
503 sk
->backlog_rcv
= pppoe_backlog_rcv
;
506 sk
->state
= PPPOX_NONE
;
507 sk
->type
= SOCK_STREAM
;
509 sk
->protinfo
.pppox
= kmalloc(sizeof(struct pppox_opt
), GFP_KERNEL
);
510 if (!sk
->protinfo
.pppox
) {
515 memset((void *) sk
->protinfo
.pppox
, 0, sizeof(struct pppox_opt
));
516 sk
->protinfo
.pppox
->sk
= sk
;
518 /* Delete the protinfo when it is time to do so. */
519 sk
->protinfo
.destruct_hook
= sk
->protinfo
.pppox
;
529 int pppoe_release(struct socket
*sock
)
531 struct sock
*sk
= sock
->sk
;
532 struct pppox_opt
*po
;
541 pppox_unbind_sock(sk
);
543 /* Signal the death of the socket. */
544 sk
->state
= PPPOX_DEAD
;
546 po
= sk
->protinfo
.pppox
;
547 if (po
->pppoe_pa
.sid
)
548 delete_item(po
->pppoe_pa
.sid
, po
->pppoe_pa
.remote
);
550 /* Should also do a queue purge here */
555 skb_queue_purge(&sk
->receive_queue
);
564 int pppoe_connect(struct socket
*sock
, struct sockaddr
*uservaddr
,
565 int sockaddr_len
, int flags
)
567 struct sock
*sk
= sock
->sk
;
568 struct net_device
*dev
= NULL
;
569 struct sockaddr_pppox
*sp
= (struct sockaddr_pppox
*) uservaddr
;
570 struct pppox_opt
*po
=sk
->protinfo
.pppox
;
576 if (sp
->sa_protocol
!= PX_PROTO_OE
)
580 if ((sk
->state
& PPPOX_CONNECTED
) && sp
->sa_addr
.pppoe
.sid
)
583 dev
= dev_get_by_name(sp
->sa_addr
.pppoe
.dev
);
590 if (po
->pppoe_pa
.sid
) {
591 pppox_unbind_sock(sk
);
593 /* Delete the old binding */
594 delete_item(po
->pppoe_pa
.sid
,po
->pppoe_pa
.remote
);
596 memset(po
, 0, sizeof(struct pppox_opt
));
599 sk
->state
= PPPOX_NONE
;
602 /* Don't re-bind if sid==0 */
603 if (sp
->sa_addr
.pppoe
.sid
!= 0) {
604 memcpy(&po
->pppoe_pa
,
606 sizeof(struct pppoe_addr
));
608 error
= set_item(po
);
614 po
->chan
.hdrlen
= (sizeof(struct pppoe_hdr
) +
615 dev
->hard_header_len
);
617 po
->chan
.private = sk
;
618 po
->chan
.ops
= &pppoe_chan_ops
;
620 error
= ppp_register_channel(&po
->chan
);
622 sk
->state
= PPPOX_CONNECTED
;
625 sk
->num
= sp
->sa_addr
.pppoe
.sid
;
633 int pppoe_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
634 int *usockaddr_len
, int peer
)
636 int len
= sizeof(struct sockaddr_pppox
);
637 struct sockaddr_pppox sp
;
639 sp
.sa_family
= AF_PPPOX
;
640 sp
.sa_protocol
= PX_PROTO_OE
;
641 memcpy(&sp
.sa_addr
.pppoe
, &sock
->sk
->protinfo
.pppox
->pppoe_pa
,
642 sizeof(struct pppoe_addr
));
644 memcpy(uaddr
, &sp
, len
);
646 *usockaddr_len
= len
;
652 int pppoe_ioctl(struct socket
*sock
, unsigned int cmd
,
655 struct sock
*sk
= sock
->sk
;
656 struct pppox_opt
*po
;
660 po
= sk
->protinfo
.pppox
;
665 if (!(sk
->state
& PPPOX_CONNECTED
))
669 if (put_user(po
->pppoe_dev
->mtu
-
670 sizeof(struct pppoe_hdr
) -
679 if (!(sk
->state
& PPPOX_CONNECTED
))
683 if (get_user(val
,(int *) arg
))
686 if (val
< (po
->pppoe_dev
->mtu
687 - sizeof(struct pppoe_hdr
)
696 if (get_user(val
, (int *) arg
))
703 struct pppox_opt
*relay_po
;
706 if (sk
->state
& PPPOX_BOUND
)
710 if (!(sk
->state
& PPPOX_CONNECTED
))
713 /* PPPoE address from the user specifies an outbound
714 PPPoE address to which frames are forwarded to */
716 if( copy_from_user(&po
->pppoe_relay
,
718 sizeof(struct sockaddr_pppox
)))
722 if (po
->pppoe_relay
.sa_family
!= AF_PPPOX
||
723 po
->pppoe_relay
.sa_protocol
!= PX_PROTO_OE
)
726 /* Check that the socket referenced by the address
728 relay_po
= get_item_by_addr(&po
->pppoe_relay
);
733 sk
->state
|= PPPOX_RELAY
;
740 if (!(sk
->state
& PPPOX_RELAY
))
743 sk
->state
&= ~PPPOX_RELAY
;
754 int pppoe_sendmsg(struct socket
*sock
, struct msghdr
*m
,
755 int total_len
, struct scm_cookie
*scm
)
757 struct sk_buff
*skb
= NULL
;
758 struct sock
*sk
= sock
->sk
;
760 struct pppoe_hdr hdr
;
761 struct pppoe_hdr
*ph
;
762 struct net_device
*dev
;
766 if (sk
->dead
|| !(sk
->state
& PPPOX_CONNECTED
)) {
776 dev
= sk
->protinfo
.pppox
->pppoe_dev
;
778 skb
= sock_wmalloc(sk
, total_len
+ dev
->hard_header_len
+ 32,
785 /* Reserve space for headers. */
786 skb_reserve(skb
, dev
->hard_header_len
);
787 skb
->nh
.raw
= skb
->data
;
789 skb
->priority
= sk
->priority
;
790 skb
->protocol
= __constant_htons(ETH_P_PPP_SES
);
792 ph
= (struct pppoe_hdr
*) skb_put(skb
, total_len
+ sizeof(struct pppoe_hdr
));
793 start
= (char *) &ph
->tag
[0];
795 copied
= memcpy_fromiovec( start
, m
->msg_iov
, m
->msg_iovlen
);
797 dev
->hard_header(skb
, dev
, ETH_P_PPP_SES
,
798 sk
->protinfo
.pppox
->pppoe_pa
.remote
,
801 memcpy(ph
, &hdr
, sizeof(struct pppoe_hdr
));
803 ph
->length
= htons(copied
);
812 /************************************************************************
814 * xmit function for internal use.
816 ***********************************************************************/
817 int __pppoe_xmit(struct sock
*sk
, struct sk_buff
*skb
)
819 struct net_device
*dev
= sk
->protinfo
.pppox
->pppoe_dev
;
820 struct pppoe_hdr hdr
;
821 struct pppoe_hdr
*ph
;
822 int headroom
= skb_headroom(skb
);
823 int data_len
= skb
->len
;
825 if (sk
->dead
|| !(sk
->state
& PPPOX_CONNECTED
)) {
833 hdr
.length
= htons(skb
->len
);
839 /* Copy the skb if there is no space for the header. */
840 if (headroom
< (sizeof(struct pppoe_hdr
) + dev
->hard_header_len
)) {
841 struct sk_buff
*skb2
;
843 skb2
= dev_alloc_skb(32+skb
->len
+
844 sizeof(struct pppoe_hdr
) +
845 dev
->hard_header_len
);
850 skb_reserve(skb2
, dev
->hard_header_len
+ sizeof(struct pppoe_hdr
));
851 memcpy(skb_put(skb2
, skb
->len
), skb
->data
, skb
->len
);
858 ph
= (struct pppoe_hdr
*) skb_push(skb
, sizeof(struct pppoe_hdr
));
859 memcpy(ph
, &hdr
, sizeof(struct pppoe_hdr
));
860 skb
->protocol
= __constant_htons(ETH_P_PPP_SES
);
862 skb
->nh
.raw
= skb
->data
;
865 dev
->hard_header(skb
, dev
, ETH_P_PPP_SES
,
866 sk
->protinfo
.pppox
->pppoe_pa
.remote
,
869 if (dev_queue_xmit(skb
) < 0)
878 /************************************************************************
880 * xmit function called by generic PPP driver
881 * sends PPP frame over PPPoE socket
883 ***********************************************************************/
884 int pppoe_xmit(struct ppp_channel
*chan
, struct sk_buff
*skb
)
886 struct sock
*sk
= (struct sock
*) chan
->private;
887 return __pppoe_xmit(sk
, skb
);
891 struct ppp_channel_ops pppoe_chan_ops
= { pppoe_xmit
, NULL
};
893 int pppoe_rcvmsg(struct socket
*sock
, struct msghdr
*m
, int total_len
, int flags
, struct scm_cookie
*scm
)
895 struct sock
*sk
= sock
->sk
;
896 struct sk_buff
*skb
= NULL
;
899 struct pppoe_hdr
*ph
= NULL
;
901 if (sk
->state
& PPPOX_BOUND
) {
906 skb
= skb_recv_datagram(sk
, flags
, 0, &error
);
916 ph
= (struct pppoe_hdr
*) skb
->nh
.raw
;
917 len
= ntohs(ph
->length
);
919 error
= memcpy_toiovec(m
->msg_iov
, (unsigned char *) &ph
->tag
[0], len
);
932 int pppoe_proc_info(char *buffer
, char **start
, off_t offset
, int length
)
934 struct pppox_opt
*po
;
941 len
+= sprintf(buffer
,
942 "Id Address Device\n");
945 write_lock_bh(&pppoe_hash_lock
);
947 for (i
= 0; i
< PPPOE_HASH_SIZE
; i
++) {
948 po
= item_hash_table
[i
];
950 char *dev
= po
->pppoe_pa
.dev
;
952 size
= sprintf(buffer
+ len
,
953 "%08X %02X:%02X:%02X:%02X:%02X:%02X %8s\n",
955 po
->pppoe_pa
.remote
[0],
956 po
->pppoe_pa
.remote
[1],
957 po
->pppoe_pa
.remote
[2],
958 po
->pppoe_pa
.remote
[3],
959 po
->pppoe_pa
.remote
[4],
960 po
->pppoe_pa
.remote
[5],
969 if (pos
> offset
+ length
)
978 write_unlock_bh(&pppoe_hash_lock
);
980 *start
= buffer
+ (offset
- begin
);
981 len
-= (offset
- begin
);
990 struct proto_ops pppoe_ops
= {
992 release
: pppoe_release
,
994 connect
: pppoe_connect
,
995 socketpair
: sock_no_socketpair
,
996 accept
: sock_no_accept
,
997 getname
: pppoe_getname
,
1000 listen
: sock_no_listen
,
1001 shutdown
: sock_no_shutdown
,
1002 setsockopt
: sock_no_setsockopt
,
1003 getsockopt
: sock_no_getsockopt
,
1004 sendmsg
: pppoe_sendmsg
,
1005 recvmsg
: pppoe_rcvmsg
,
1009 struct pppox_proto pppoe_proto
= {
1010 create
: pppoe_create
,
1015 int __init
pppoe_init(void)
1017 int err
= register_pppox_proto(PX_PROTO_OE
, &pppoe_proto
);
1020 printk(KERN_INFO
"Registered PPPoE v0.5\n");
1022 dev_add_pack(&pppoes_ptype
);
1023 register_netdevice_notifier(&pppoe_notifier
);
1024 proc_net_create("pppoe", 0, pppoe_proc_info
);
1031 MODULE_PARM(debug
, "i");
1032 int init_module(void)
1034 return pppoe_init();
1037 void cleanup_module(void)
1039 unregister_pppox_proto(PX_PROTO_OE
);
1040 dev_remove_pack(&pppoes_ptype
);
1041 unregister_netdevice_notifier(&pppoe_notifier
);
1042 proc_net_remove("pppoe");
1047 int pppoe_proto_init(struct net_proto
*np
)
1049 return pppoe_init();