[PPPOX]: Fix assignment into const proto_ops.
[firewire-audio.git] / drivers / net / pppoe.c
blob9369f811075d123f245dc54bfac2a86f56607225
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/notifier.h>
71 #include <linux/file.h>
72 #include <linux/proc_fs.h>
73 #include <linux/seq_file.h>
75 #include <net/sock.h>
77 #include <asm/uaccess.h>
79 #define PPPOE_HASH_BITS 4
80 #define PPPOE_HASH_SIZE (1<<PPPOE_HASH_BITS)
82 static struct ppp_channel_ops pppoe_chan_ops;
84 static int pppoe_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
85 static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb);
86 static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb);
88 static const struct proto_ops pppoe_ops;
89 static DEFINE_RWLOCK(pppoe_hash_lock);
91 static struct ppp_channel_ops pppoe_chan_ops;
93 static inline int cmp_2_addr(struct pppoe_addr *a, struct pppoe_addr *b)
95 return (a->sid == b->sid &&
96 (memcmp(a->remote, b->remote, ETH_ALEN) == 0));
99 static inline int cmp_addr(struct pppoe_addr *a, unsigned long sid, char *addr)
101 return (a->sid == sid &&
102 (memcmp(a->remote,addr,ETH_ALEN) == 0));
105 static int hash_item(unsigned long sid, unsigned char *addr)
107 char hash = 0;
108 int i, j;
110 for (i = 0; i < ETH_ALEN ; ++i) {
111 for (j = 0; j < 8/PPPOE_HASH_BITS ; ++j) {
112 hash ^= addr[i] >> ( j * PPPOE_HASH_BITS );
116 for (i = 0; i < (sizeof(unsigned long)*8) / PPPOE_HASH_BITS ; ++i)
117 hash ^= sid >> (i*PPPOE_HASH_BITS);
119 return hash & ( PPPOE_HASH_SIZE - 1 );
122 /* zeroed because its in .bss */
123 static struct pppox_sock *item_hash_table[PPPOE_HASH_SIZE];
125 /**********************************************************************
127 * Set/get/delete/rehash items (internal versions)
129 **********************************************************************/
130 static struct pppox_sock *__get_item(unsigned long sid, unsigned char *addr)
132 int hash = hash_item(sid, addr);
133 struct pppox_sock *ret;
135 ret = item_hash_table[hash];
137 while (ret && !cmp_addr(&ret->pppoe_pa, sid, addr))
138 ret = ret->next;
140 return ret;
143 static int __set_item(struct pppox_sock *po)
145 int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
146 struct pppox_sock *ret;
148 ret = item_hash_table[hash];
149 while (ret) {
150 if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa))
151 return -EALREADY;
153 ret = ret->next;
156 if (!ret) {
157 po->next = item_hash_table[hash];
158 item_hash_table[hash] = po;
161 return 0;
164 static struct pppox_sock *__delete_item(unsigned long sid, char *addr)
166 int hash = hash_item(sid, addr);
167 struct pppox_sock *ret, **src;
169 ret = item_hash_table[hash];
170 src = &item_hash_table[hash];
172 while (ret) {
173 if (cmp_addr(&ret->pppoe_pa, sid, addr)) {
174 *src = ret->next;
175 break;
178 src = &ret->next;
179 ret = ret->next;
182 return ret;
185 /**********************************************************************
187 * Set/get/delete/rehash items
189 **********************************************************************/
190 static inline struct pppox_sock *get_item(unsigned long sid,
191 unsigned char *addr)
193 struct pppox_sock *po;
195 read_lock_bh(&pppoe_hash_lock);
196 po = __get_item(sid, addr);
197 if (po)
198 sock_hold(sk_pppox(po));
199 read_unlock_bh(&pppoe_hash_lock);
201 return po;
204 static inline struct pppox_sock *get_item_by_addr(struct sockaddr_pppox *sp)
206 return get_item(sp->sa_addr.pppoe.sid, sp->sa_addr.pppoe.remote);
209 static inline int set_item(struct pppox_sock *po)
211 int i;
213 if (!po)
214 return -EINVAL;
216 write_lock_bh(&pppoe_hash_lock);
217 i = __set_item(po);
218 write_unlock_bh(&pppoe_hash_lock);
220 return i;
223 static inline struct pppox_sock *delete_item(unsigned long sid, char *addr)
225 struct pppox_sock *ret;
227 write_lock_bh(&pppoe_hash_lock);
228 ret = __delete_item(sid, addr);
229 write_unlock_bh(&pppoe_hash_lock);
231 return ret;
236 /***************************************************************************
238 * Handler for device events.
239 * Certain device events require that sockets be unconnected.
241 **************************************************************************/
243 static void pppoe_flush_dev(struct net_device *dev)
245 int hash;
247 BUG_ON(dev == NULL);
249 read_lock_bh(&pppoe_hash_lock);
250 for (hash = 0; hash < PPPOE_HASH_SIZE; hash++) {
251 struct pppox_sock *po = item_hash_table[hash];
253 while (po != NULL) {
254 if (po->pppoe_dev == dev) {
255 struct sock *sk = sk_pppox(po);
257 sock_hold(sk);
258 po->pppoe_dev = NULL;
260 /* We hold a reference to SK, now drop the
261 * hash table lock so that we may attempt
262 * to lock the socket (which can sleep).
264 read_unlock_bh(&pppoe_hash_lock);
266 lock_sock(sk);
268 if (sk->sk_state &
269 (PPPOX_CONNECTED | PPPOX_BOUND)) {
270 pppox_unbind_sock(sk);
271 dev_put(dev);
272 sk->sk_state = PPPOX_ZOMBIE;
273 sk->sk_state_change(sk);
276 release_sock(sk);
278 sock_put(sk);
280 read_lock_bh(&pppoe_hash_lock);
282 /* Now restart from the beginning of this
283 * hash chain. We always NULL out pppoe_dev
284 * so we are guaranteed to make forward
285 * progress.
287 po = item_hash_table[hash];
288 continue;
290 po = po->next;
293 read_unlock_bh(&pppoe_hash_lock);
296 static int pppoe_device_event(struct notifier_block *this,
297 unsigned long event, void *ptr)
299 struct net_device *dev = (struct net_device *) ptr;
301 /* Only look at sockets that are using this specific device. */
302 switch (event) {
303 case NETDEV_CHANGEMTU:
304 /* A change in mtu is a bad thing, requiring
305 * LCP re-negotiation.
308 case NETDEV_GOING_DOWN:
309 case NETDEV_DOWN:
310 /* Find every socket on this device and kill it. */
311 pppoe_flush_dev(dev);
312 break;
314 default:
315 break;
318 return NOTIFY_DONE;
322 static struct notifier_block pppoe_notifier = {
323 .notifier_call = pppoe_device_event,
327 /************************************************************************
329 * Do the real work of receiving a PPPoE Session frame.
331 ***********************************************************************/
332 static int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb)
334 struct pppox_sock *po = pppox_sk(sk);
335 struct pppox_sock *relay_po = NULL;
337 if (sk->sk_state & PPPOX_BOUND) {
338 struct pppoe_hdr *ph = (struct pppoe_hdr *) skb->nh.raw;
339 int len = ntohs(ph->length);
340 skb_pull(skb, sizeof(struct pppoe_hdr));
341 skb_postpull_rcsum(skb, ph, sizeof(*ph));
342 if (pskb_trim_rcsum(skb, len))
343 goto abort_kfree;
345 ppp_input(&po->chan, skb);
346 } else if (sk->sk_state & PPPOX_RELAY) {
347 relay_po = get_item_by_addr(&po->pppoe_relay);
349 if (relay_po == NULL)
350 goto abort_kfree;
352 if ((sk_pppox(relay_po)->sk_state & PPPOX_CONNECTED) == 0)
353 goto abort_put;
355 skb_pull(skb, sizeof(struct pppoe_hdr));
356 if (!__pppoe_xmit(sk_pppox(relay_po), skb))
357 goto abort_put;
358 } else {
359 if (sock_queue_rcv_skb(sk, skb))
360 goto abort_kfree;
363 return NET_RX_SUCCESS;
365 abort_put:
366 sock_put(sk_pppox(relay_po));
368 abort_kfree:
369 kfree_skb(skb);
370 return NET_RX_DROP;
373 /************************************************************************
375 * Receive wrapper called in BH context.
377 ***********************************************************************/
378 static int pppoe_rcv(struct sk_buff *skb,
379 struct net_device *dev,
380 struct packet_type *pt,
381 struct net_device *orig_dev)
384 struct pppoe_hdr *ph;
385 struct pppox_sock *po;
387 if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
388 goto drop;
390 if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
391 goto out;
393 ph = (struct pppoe_hdr *) skb->nh.raw;
395 po = get_item((unsigned long) ph->sid, eth_hdr(skb)->h_source);
396 if (po != NULL)
397 return sk_receive_skb(sk_pppox(po), skb);
398 drop:
399 kfree_skb(skb);
400 out:
401 return NET_RX_DROP;
404 /************************************************************************
406 * Receive a PPPoE Discovery frame.
407 * This is solely for detection of PADT frames
409 ***********************************************************************/
410 static int pppoe_disc_rcv(struct sk_buff *skb,
411 struct net_device *dev,
412 struct packet_type *pt,
413 struct net_device *orig_dev)
416 struct pppoe_hdr *ph;
417 struct pppox_sock *po;
419 if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
420 goto abort;
422 if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
423 goto out;
425 ph = (struct pppoe_hdr *) skb->nh.raw;
426 if (ph->code != PADT_CODE)
427 goto abort;
429 po = get_item((unsigned long) ph->sid, eth_hdr(skb)->h_source);
430 if (po) {
431 struct sock *sk = sk_pppox(po);
433 bh_lock_sock(sk);
435 /* If the user has locked the socket, just ignore
436 * the packet. With the way two rcv protocols hook into
437 * one socket family type, we cannot (easily) distinguish
438 * what kind of SKB it is during backlog rcv.
440 if (sock_owned_by_user(sk) == 0) {
441 /* We're no longer connect at the PPPOE layer,
442 * and must wait for ppp channel to disconnect us.
444 sk->sk_state = PPPOX_ZOMBIE;
447 bh_unlock_sock(sk);
448 sock_put(sk);
451 abort:
452 kfree_skb(skb);
453 out:
454 return NET_RX_SUCCESS; /* Lies... :-) */
457 static struct packet_type pppoes_ptype = {
458 .type = __constant_htons(ETH_P_PPP_SES),
459 .func = pppoe_rcv,
462 static struct packet_type pppoed_ptype = {
463 .type = __constant_htons(ETH_P_PPP_DISC),
464 .func = pppoe_disc_rcv,
467 static struct proto pppoe_sk_proto = {
468 .name = "PPPOE",
469 .owner = THIS_MODULE,
470 .obj_size = sizeof(struct pppox_sock),
473 /***********************************************************************
475 * Initialize a new struct sock.
477 **********************************************************************/
478 static int pppoe_create(struct socket *sock)
480 int error = -ENOMEM;
481 struct sock *sk;
483 sk = sk_alloc(PF_PPPOX, GFP_KERNEL, &pppoe_sk_proto, 1);
484 if (!sk)
485 goto out;
487 sock_init_data(sock, sk);
489 sock->state = SS_UNCONNECTED;
490 sock->ops = &pppoe_ops;
492 sk->sk_backlog_rcv = pppoe_rcv_core;
493 sk->sk_state = PPPOX_NONE;
494 sk->sk_type = SOCK_STREAM;
495 sk->sk_family = PF_PPPOX;
496 sk->sk_protocol = PX_PROTO_OE;
498 error = 0;
499 out: return error;
502 static int pppoe_release(struct socket *sock)
504 struct sock *sk = sock->sk;
505 struct pppox_sock *po;
506 int error = 0;
508 if (!sk)
509 return 0;
511 if (sock_flag(sk, SOCK_DEAD))
512 return -EBADF;
514 pppox_unbind_sock(sk);
516 /* Signal the death of the socket. */
517 sk->sk_state = PPPOX_DEAD;
519 po = pppox_sk(sk);
520 if (po->pppoe_pa.sid) {
521 delete_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
524 if (po->pppoe_dev)
525 dev_put(po->pppoe_dev);
527 po->pppoe_dev = NULL;
529 sock_orphan(sk);
530 sock->sk = NULL;
532 skb_queue_purge(&sk->sk_receive_queue);
533 sock_put(sk);
535 return error;
539 static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
540 int sockaddr_len, int flags)
542 struct sock *sk = sock->sk;
543 struct net_device *dev = NULL;
544 struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
545 struct pppox_sock *po = pppox_sk(sk);
546 int error;
548 lock_sock(sk);
550 error = -EINVAL;
551 if (sp->sa_protocol != PX_PROTO_OE)
552 goto end;
554 /* Check for already bound sockets */
555 error = -EBUSY;
556 if ((sk->sk_state & PPPOX_CONNECTED) && sp->sa_addr.pppoe.sid)
557 goto end;
559 /* Check for already disconnected sockets, on attempts to disconnect */
560 error = -EALREADY;
561 if ((sk->sk_state & PPPOX_DEAD) && !sp->sa_addr.pppoe.sid )
562 goto end;
564 error = 0;
565 if (po->pppoe_pa.sid) {
566 pppox_unbind_sock(sk);
568 /* Delete the old binding */
569 delete_item(po->pppoe_pa.sid,po->pppoe_pa.remote);
571 if(po->pppoe_dev)
572 dev_put(po->pppoe_dev);
574 memset(sk_pppox(po) + 1, 0,
575 sizeof(struct pppox_sock) - sizeof(struct sock));
577 sk->sk_state = PPPOX_NONE;
580 /* Don't re-bind if sid==0 */
581 if (sp->sa_addr.pppoe.sid != 0) {
582 dev = dev_get_by_name(sp->sa_addr.pppoe.dev);
584 error = -ENODEV;
585 if (!dev)
586 goto end;
588 po->pppoe_dev = dev;
590 if (!(dev->flags & IFF_UP))
591 goto err_put;
593 memcpy(&po->pppoe_pa,
594 &sp->sa_addr.pppoe,
595 sizeof(struct pppoe_addr));
597 error = set_item(po);
598 if (error < 0)
599 goto err_put;
601 po->chan.hdrlen = (sizeof(struct pppoe_hdr) +
602 dev->hard_header_len);
604 po->chan.private = sk;
605 po->chan.ops = &pppoe_chan_ops;
607 error = ppp_register_channel(&po->chan);
608 if (error)
609 goto err_put;
611 sk->sk_state = PPPOX_CONNECTED;
614 po->num = sp->sa_addr.pppoe.sid;
616 end:
617 release_sock(sk);
618 return error;
619 err_put:
620 if (po->pppoe_dev) {
621 dev_put(po->pppoe_dev);
622 po->pppoe_dev = NULL;
624 goto end;
628 static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
629 int *usockaddr_len, int peer)
631 int len = sizeof(struct sockaddr_pppox);
632 struct sockaddr_pppox sp;
634 sp.sa_family = AF_PPPOX;
635 sp.sa_protocol = PX_PROTO_OE;
636 memcpy(&sp.sa_addr.pppoe, &pppox_sk(sock->sk)->pppoe_pa,
637 sizeof(struct pppoe_addr));
639 memcpy(uaddr, &sp, len);
641 *usockaddr_len = len;
643 return 0;
647 static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
648 unsigned long arg)
650 struct sock *sk = sock->sk;
651 struct pppox_sock *po = pppox_sk(sk);
652 int val = 0;
653 int err = 0;
655 switch (cmd) {
656 case PPPIOCGMRU:
657 err = -ENXIO;
659 if (!(sk->sk_state & PPPOX_CONNECTED))
660 break;
662 err = -EFAULT;
663 if (put_user(po->pppoe_dev->mtu -
664 sizeof(struct pppoe_hdr) -
665 PPP_HDRLEN,
666 (int __user *) arg))
667 break;
668 err = 0;
669 break;
671 case PPPIOCSMRU:
672 err = -ENXIO;
673 if (!(sk->sk_state & PPPOX_CONNECTED))
674 break;
676 err = -EFAULT;
677 if (get_user(val,(int __user *) arg))
678 break;
680 if (val < (po->pppoe_dev->mtu
681 - sizeof(struct pppoe_hdr)
682 - PPP_HDRLEN))
683 err = 0;
684 else
685 err = -EINVAL;
686 break;
688 case PPPIOCSFLAGS:
689 err = -EFAULT;
690 if (get_user(val, (int __user *) arg))
691 break;
692 err = 0;
693 break;
695 case PPPOEIOCSFWD:
697 struct pppox_sock *relay_po;
699 err = -EBUSY;
700 if (sk->sk_state & (PPPOX_BOUND | PPPOX_ZOMBIE | PPPOX_DEAD))
701 break;
703 err = -ENOTCONN;
704 if (!(sk->sk_state & PPPOX_CONNECTED))
705 break;
707 /* PPPoE address from the user specifies an outbound
708 PPPoE address to which frames are forwarded to */
709 err = -EFAULT;
710 if (copy_from_user(&po->pppoe_relay,
711 (void __user *)arg,
712 sizeof(struct sockaddr_pppox)))
713 break;
715 err = -EINVAL;
716 if (po->pppoe_relay.sa_family != AF_PPPOX ||
717 po->pppoe_relay.sa_protocol!= PX_PROTO_OE)
718 break;
720 /* Check that the socket referenced by the address
721 actually exists. */
722 relay_po = get_item_by_addr(&po->pppoe_relay);
724 if (!relay_po)
725 break;
727 sock_put(sk_pppox(relay_po));
728 sk->sk_state |= PPPOX_RELAY;
729 err = 0;
730 break;
733 case PPPOEIOCDFWD:
734 err = -EALREADY;
735 if (!(sk->sk_state & PPPOX_RELAY))
736 break;
738 sk->sk_state &= ~PPPOX_RELAY;
739 err = 0;
740 break;
742 default:;
745 return err;
749 static int pppoe_sendmsg(struct kiocb *iocb, struct socket *sock,
750 struct msghdr *m, size_t total_len)
752 struct sk_buff *skb = NULL;
753 struct sock *sk = sock->sk;
754 struct pppox_sock *po = pppox_sk(sk);
755 int error = 0;
756 struct pppoe_hdr hdr;
757 struct pppoe_hdr *ph;
758 struct net_device *dev;
759 char *start;
761 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
762 error = -ENOTCONN;
763 goto end;
766 hdr.ver = 1;
767 hdr.type = 1;
768 hdr.code = 0;
769 hdr.sid = po->num;
771 lock_sock(sk);
773 dev = po->pppoe_dev;
775 error = -EMSGSIZE;
776 if (total_len > (dev->mtu + dev->hard_header_len))
777 goto end;
780 skb = sock_wmalloc(sk, total_len + dev->hard_header_len + 32,
781 0, GFP_KERNEL);
782 if (!skb) {
783 error = -ENOMEM;
784 goto end;
787 /* Reserve space for headers. */
788 skb_reserve(skb, dev->hard_header_len);
789 skb->nh.raw = skb->data;
791 skb->dev = dev;
793 skb->priority = sk->sk_priority;
794 skb->protocol = __constant_htons(ETH_P_PPP_SES);
796 ph = (struct pppoe_hdr *) skb_put(skb, total_len + sizeof(struct pppoe_hdr));
797 start = (char *) &ph->tag[0];
799 error = memcpy_fromiovec(start, m->msg_iov, total_len);
801 if (error < 0) {
802 kfree_skb(skb);
803 goto end;
806 error = total_len;
807 dev->hard_header(skb, dev, ETH_P_PPP_SES,
808 po->pppoe_pa.remote, NULL, total_len);
810 memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
812 ph->length = htons(total_len);
814 dev_queue_xmit(skb);
816 end:
817 release_sock(sk);
818 return error;
822 /************************************************************************
824 * xmit function for internal use.
826 ***********************************************************************/
827 static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
829 struct pppox_sock *po = pppox_sk(sk);
830 struct net_device *dev = po->pppoe_dev;
831 struct pppoe_hdr hdr;
832 struct pppoe_hdr *ph;
833 int headroom = skb_headroom(skb);
834 int data_len = skb->len;
835 struct sk_buff *skb2;
837 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
838 goto abort;
840 hdr.ver = 1;
841 hdr.type = 1;
842 hdr.code = 0;
843 hdr.sid = po->num;
844 hdr.length = htons(skb->len);
846 if (!dev)
847 goto abort;
849 /* Copy the skb if there is no space for the header. */
850 if (headroom < (sizeof(struct pppoe_hdr) + dev->hard_header_len)) {
851 skb2 = dev_alloc_skb(32+skb->len +
852 sizeof(struct pppoe_hdr) +
853 dev->hard_header_len);
855 if (skb2 == NULL)
856 goto abort;
858 skb_reserve(skb2, dev->hard_header_len + sizeof(struct pppoe_hdr));
859 memcpy(skb_put(skb2, skb->len), skb->data, skb->len);
860 } else {
861 /* Make a clone so as to not disturb the original skb,
862 * give dev_queue_xmit something it can free.
864 skb2 = skb_clone(skb, GFP_ATOMIC);
867 ph = (struct pppoe_hdr *) skb_push(skb2, sizeof(struct pppoe_hdr));
868 memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
869 skb2->protocol = __constant_htons(ETH_P_PPP_SES);
871 skb2->nh.raw = skb2->data;
873 skb2->dev = dev;
875 dev->hard_header(skb2, dev, ETH_P_PPP_SES,
876 po->pppoe_pa.remote, NULL, data_len);
878 /* We're transmitting skb2, and assuming that dev_queue_xmit
879 * will free it. The generic ppp layer however, is expecting
880 * that we give back 'skb' (not 'skb2') in case of failure,
881 * but free it in case of success.
884 if (dev_queue_xmit(skb2) < 0)
885 goto abort;
887 kfree_skb(skb);
888 return 1;
890 abort:
891 return 0;
895 /************************************************************************
897 * xmit function called by generic PPP driver
898 * sends PPP frame over PPPoE socket
900 ***********************************************************************/
901 static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
903 struct sock *sk = (struct sock *) chan->private;
904 return __pppoe_xmit(sk, skb);
908 static struct ppp_channel_ops pppoe_chan_ops = {
909 .start_xmit = pppoe_xmit,
912 static int pppoe_recvmsg(struct kiocb *iocb, struct socket *sock,
913 struct msghdr *m, size_t total_len, int flags)
915 struct sock *sk = sock->sk;
916 struct sk_buff *skb = NULL;
917 int error = 0;
918 int len;
919 struct pppoe_hdr *ph = NULL;
921 if (sk->sk_state & PPPOX_BOUND) {
922 error = -EIO;
923 goto end;
926 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
927 flags & MSG_DONTWAIT, &error);
929 if (error < 0) {
930 goto end;
933 m->msg_namelen = 0;
935 if (skb) {
936 error = 0;
937 ph = (struct pppoe_hdr *) skb->nh.raw;
938 len = ntohs(ph->length);
940 error = memcpy_toiovec(m->msg_iov, (unsigned char *) &ph->tag[0], len);
941 if (error < 0)
942 goto do_skb_free;
943 error = len;
946 do_skb_free:
947 if (skb)
948 kfree_skb(skb);
949 end:
950 return error;
953 #ifdef CONFIG_PROC_FS
954 static int pppoe_seq_show(struct seq_file *seq, void *v)
956 struct pppox_sock *po;
957 char *dev_name;
959 if (v == SEQ_START_TOKEN) {
960 seq_puts(seq, "Id Address Device\n");
961 goto out;
964 po = v;
965 dev_name = po->pppoe_pa.dev;
967 seq_printf(seq, "%08X %02X:%02X:%02X:%02X:%02X:%02X %8s\n",
968 po->pppoe_pa.sid,
969 po->pppoe_pa.remote[0], po->pppoe_pa.remote[1],
970 po->pppoe_pa.remote[2], po->pppoe_pa.remote[3],
971 po->pppoe_pa.remote[4], po->pppoe_pa.remote[5], dev_name);
972 out:
973 return 0;
976 static __inline__ struct pppox_sock *pppoe_get_idx(loff_t pos)
978 struct pppox_sock *po = NULL;
979 int i = 0;
981 for (; i < PPPOE_HASH_SIZE; i++) {
982 po = item_hash_table[i];
983 while (po) {
984 if (!pos--)
985 goto out;
986 po = po->next;
989 out:
990 return po;
993 static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos)
995 loff_t l = *pos;
997 read_lock_bh(&pppoe_hash_lock);
998 return l ? pppoe_get_idx(--l) : SEQ_START_TOKEN;
1001 static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1003 struct pppox_sock *po;
1005 ++*pos;
1006 if (v == SEQ_START_TOKEN) {
1007 po = pppoe_get_idx(0);
1008 goto out;
1010 po = v;
1011 if (po->next)
1012 po = po->next;
1013 else {
1014 int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
1016 while (++hash < PPPOE_HASH_SIZE) {
1017 po = item_hash_table[hash];
1018 if (po)
1019 break;
1022 out:
1023 return po;
1026 static void pppoe_seq_stop(struct seq_file *seq, void *v)
1028 read_unlock_bh(&pppoe_hash_lock);
1031 static struct seq_operations pppoe_seq_ops = {
1032 .start = pppoe_seq_start,
1033 .next = pppoe_seq_next,
1034 .stop = pppoe_seq_stop,
1035 .show = pppoe_seq_show,
1038 static int pppoe_seq_open(struct inode *inode, struct file *file)
1040 return seq_open(file, &pppoe_seq_ops);
1043 static struct file_operations pppoe_seq_fops = {
1044 .owner = THIS_MODULE,
1045 .open = pppoe_seq_open,
1046 .read = seq_read,
1047 .llseek = seq_lseek,
1048 .release = seq_release,
1051 static int __init pppoe_proc_init(void)
1053 struct proc_dir_entry *p;
1055 p = create_proc_entry("net/pppoe", S_IRUGO, NULL);
1056 if (!p)
1057 return -ENOMEM;
1059 p->proc_fops = &pppoe_seq_fops;
1060 return 0;
1062 #else /* CONFIG_PROC_FS */
1063 static inline int pppoe_proc_init(void) { return 0; }
1064 #endif /* CONFIG_PROC_FS */
1066 static const struct proto_ops pppoe_ops = {
1067 .family = AF_PPPOX,
1068 .owner = THIS_MODULE,
1069 .release = pppoe_release,
1070 .bind = sock_no_bind,
1071 .connect = pppoe_connect,
1072 .socketpair = sock_no_socketpair,
1073 .accept = sock_no_accept,
1074 .getname = pppoe_getname,
1075 .poll = datagram_poll,
1076 .listen = sock_no_listen,
1077 .shutdown = sock_no_shutdown,
1078 .setsockopt = sock_no_setsockopt,
1079 .getsockopt = sock_no_getsockopt,
1080 .sendmsg = pppoe_sendmsg,
1081 .recvmsg = pppoe_recvmsg,
1082 .mmap = sock_no_mmap,
1083 .ioctl = pppox_ioctl,
1086 static struct pppox_proto pppoe_proto = {
1087 .create = pppoe_create,
1088 .ioctl = pppoe_ioctl,
1089 .owner = THIS_MODULE,
1093 static int __init pppoe_init(void)
1095 int err = proto_register(&pppoe_sk_proto, 0);
1097 if (err)
1098 goto out;
1100 err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
1101 if (err)
1102 goto out_unregister_pppoe_proto;
1104 err = pppoe_proc_init();
1105 if (err)
1106 goto out_unregister_pppox_proto;
1108 dev_add_pack(&pppoes_ptype);
1109 dev_add_pack(&pppoed_ptype);
1110 register_netdevice_notifier(&pppoe_notifier);
1111 out:
1112 return err;
1113 out_unregister_pppox_proto:
1114 unregister_pppox_proto(PX_PROTO_OE);
1115 out_unregister_pppoe_proto:
1116 proto_unregister(&pppoe_sk_proto);
1117 goto out;
1120 static void __exit pppoe_exit(void)
1122 unregister_pppox_proto(PX_PROTO_OE);
1123 dev_remove_pack(&pppoes_ptype);
1124 dev_remove_pack(&pppoed_ptype);
1125 unregister_netdevice_notifier(&pppoe_notifier);
1126 remove_proc_entry("net/pppoe", NULL);
1127 proto_unregister(&pppoe_sk_proto);
1130 module_init(pppoe_init);
1131 module_exit(pppoe_exit);
1133 MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
1134 MODULE_DESCRIPTION("PPP over Ethernet driver");
1135 MODULE_LICENSE("GPL");
1136 MODULE_ALIAS_NETPROTO(PF_PPPOX);