File: /usr/src/linux/drivers/net/pppoe.c

1     /** -*- linux-c -*- ***********************************************************
2      * Linux PPP over Ethernet (PPPoX/PPPoE) Sockets
3      *
4      * PPPoX --- Generic PPP encapsulation socket family
5      * PPPoE --- PPP over Ethernet (RFC 2516)
6      *
7      *
8      * Version:    0.6.8
9      *
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
18      *		in pppoe_release.
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
25      *		locked. (DaveM)
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
31      *		a memory leak.
32      *
33      * Author:	Michal Ostrowski <mostrows@speakeasy.net>
34      * Contributors:
35      * 		Arnaldo Carvalho de Melo <acme@xconectiva.com.br>
36      *		David S. Miller (davem@redhat.com)
37      *
38      * License:
39      *		This program is free software; you can redistribute it and/or
40      *		modify it under the terms of the GNU General Public License
41      *		as published by the Free Software Foundation; either version
42      *		2 of the License, or (at your option) any later version.
43      *
44      */
45     
46     #include <linux/string.h>
47     #include <linux/module.h>
48     
49     #include <asm/uaccess.h>
50     
51     #include <linux/kernel.h>
52     #include <linux/sched.h>
53     #include <linux/slab.h>
54     #include <linux/errno.h>
55     
56     #include <linux/netdevice.h>
57     #include <linux/net.h>
58     #include <linux/inetdevice.h>
59     #include <linux/etherdevice.h>
60     #include <linux/skbuff.h>
61     #include <linux/init.h>
62     #include <linux/if_ether.h>
63     #include <linux/if_pppox.h>
64     #include <net/sock.h>
65     #include <linux/ppp_channel.h>
66     #include <linux/ppp_defs.h>
67     #include <linux/if_ppp.h>
68     #include <linux/if_pppvar.h>
69     #include <linux/notifier.h>
70     #include <linux/file.h>
71     #include <linux/proc_fs.h>
72     
73     
74     
75     static int __attribute__((unused)) pppoe_debug = 7;
76     #define PPPOE_HASH_BITS 4
77     #define PPPOE_HASH_SIZE (1<<PPPOE_HASH_BITS)
78     
79     int pppoe_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
80     int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb);
81     int __pppoe_xmit(struct sock *sk, struct sk_buff *skb);
82     
83     struct proto_ops pppoe_ops;
84     
85     
86     #if 0
87     #define CHECKPTR(x,y) { if (!(x) && pppoe_debug &7 ){ printk(KERN_CRIT "PPPoE Invalid pointer : %s , %p\n",#x,(x)); error=-EINVAL; goto y; }}
88     #define DEBUG(s,args...) if( pppoe_debug & (s) ) printk(KERN_CRIT args );
89     #else
90     #define CHECKPTR(x,y) do {} while (0);
91     #define DEBUG(s,args...) do { } while (0);
92     #endif
93     
94     
95     
96     static rwlock_t pppoe_hash_lock = RW_LOCK_UNLOCKED;
97     
98     
99     static inline int cmp_2_addr(struct pppoe_addr *a, struct pppoe_addr *b)
100     {
101     	return (a->sid == b->sid &&
102     		(memcmp(a->remote, b->remote, ETH_ALEN) == 0));
103     }
104     
105     static inline int cmp_addr(struct pppoe_addr *a, unsigned long sid, char *addr)
106     {
107     	return (a->sid == sid &&
108     		(memcmp(a->remote,addr,ETH_ALEN) == 0));
109     }
110     
111     static int hash_item(unsigned long sid, unsigned char *addr)
112     {
113     	char hash = 0;
114     	int i, j;
115     
116     	for (i = 0; i < ETH_ALEN ; ++i) {
117     		for (j = 0; j < 8/PPPOE_HASH_BITS ; ++j) {
118     			hash ^= addr[i] >> ( j * PPPOE_HASH_BITS );
119     		}
120     	}
121     
122     	for (i = 0; i < (sizeof(unsigned long)*8) / PPPOE_HASH_BITS ; ++i)
123     		hash ^= sid >> (i*PPPOE_HASH_BITS);
124     
125     	return hash & ( PPPOE_HASH_SIZE - 1 );
126     }
127     
128     static struct pppox_opt *item_hash_table[PPPOE_HASH_SIZE] = { 0, };
129     
130     /**********************************************************************
131      *
132      *  Set/get/delete/rehash items  (internal versions)
133      *
134      **********************************************************************/
135     static struct pppox_opt *__get_item(unsigned long sid, unsigned char *addr)
136     {
137     	int hash = hash_item(sid, addr);
138     	struct pppox_opt *ret;
139     
140     	ret = item_hash_table[hash];
141     
142     	while (ret && !cmp_addr(&ret->pppoe_pa, sid, addr))
143     		ret = ret->next;
144     
145     	return ret;
146     }
147     
148     static int __set_item(struct pppox_opt *po)
149     {
150     	int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
151     	struct pppox_opt *ret;
152     
153     	ret = item_hash_table[hash];
154     	while (ret) {
155     		if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa))
156     			return -EALREADY;
157     
158     		ret = ret->next;
159     	}
160     
161     	if (!ret) {
162     		po->next = item_hash_table[hash];
163     		item_hash_table[hash] = po;
164     	}
165     
166     	return 0;
167     }
168     
169     static struct pppox_opt *__delete_item(unsigned long sid, char *addr)
170     {
171     	int hash = hash_item(sid, addr);
172     	struct pppox_opt *ret, **src;
173     
174     	ret = item_hash_table[hash];
175     	src = &item_hash_table[hash];
176     
177     	while (ret) {
178     		if (cmp_addr(&ret->pppoe_pa, sid, addr)) {
179     			*src = ret->next;
180     			break;
181     		}
182     
183     		src = &ret->next;
184     		ret = ret->next;
185     	}
186     
187     	return ret;
188     }
189     
190     /**********************************************************************
191      *
192      *  Set/get/delete/rehash items
193      *
194      **********************************************************************/
195     static inline struct pppox_opt *get_item(unsigned long sid,
196     					 unsigned char *addr)
197     {
198     	struct pppox_opt *po;
199     
200     	read_lock_bh(&pppoe_hash_lock);
201     	po = __get_item(sid, addr);
202     	if (po)
203     		sock_hold(po->sk);
204     	read_unlock_bh(&pppoe_hash_lock);
205     
206     	return po;
207     }
208     
209     static inline struct pppox_opt *get_item_by_addr(struct sockaddr_pppox *sp)
210     {
211     	return get_item(sp->sa_addr.pppoe.sid, sp->sa_addr.pppoe.remote);
212     }
213     
214     static inline int set_item(struct pppox_opt *po)
215     {
216     	int i;
217     
218     	if (!po)
219     		return -EINVAL;
220     
221     	write_lock_bh(&pppoe_hash_lock);
222     	i = __set_item(po);
223     	write_unlock_bh(&pppoe_hash_lock);
224     
225     	return i;
226     }
227     
228     static inline struct pppox_opt *delete_item(unsigned long sid, char *addr)
229     {
230     	struct pppox_opt *ret;
231     
232     	write_lock_bh(&pppoe_hash_lock);
233     	ret = __delete_item(sid, addr);
234     	write_unlock_bh(&pppoe_hash_lock);
235     
236     	return ret;
237     }
238     
239     
240     
241     /***************************************************************************
242      *
243      *  Handler for device events.
244      *  Certain device events require that sockets be unconnected.
245      *
246      **************************************************************************/
247     
248     static void pppoe_flush_dev(struct net_device *dev)
249     {
250     	int hash;
251     
252     	if (dev == NULL)
253     		BUG();
254     
255     	read_lock_bh(&pppoe_hash_lock);
256     	for (hash = 0; hash < PPPOE_HASH_SIZE; hash++) {
257     		struct pppox_opt *po = item_hash_table[hash];
258     
259     		while (po != NULL) {
260     			if (po->pppoe_dev == dev) {
261     				struct sock *sk = po->sk;
262     
263     				sock_hold(sk);
264     				po->pppoe_dev = NULL;
265     
266     				/* We hold a reference to SK, now drop the
267     				 * hash table lock so that we may attempt
268     				 * to lock the socket (which can sleep).
269     				 */
270     				read_unlock_bh(&pppoe_hash_lock);
271     
272     				lock_sock(sk);
273     
274     				if (sk->state & (PPPOX_CONNECTED | PPPOX_BOUND)) {
275     					pppox_unbind_sock(sk);
276     					dev_put(dev);
277     					sk->state = PPPOX_DEAD;
278     					sk->state_change(sk);
279     				}
280     
281     				release_sock(sk);
282     
283     				sock_put(sk);
284     
285     				read_lock_bh(&pppoe_hash_lock);
286     
287     				/* Now restart from the beginning of this
288     				 * hash chain.  We always NULL out pppoe_dev
289     				 * so we are guarenteed to make forward
290     				 * progress.
291     				 */
292     				po = item_hash_table[hash];
293     				continue;
294     			}
295     			po = po->next;
296     		}
297     	}
298     	read_unlock_bh(&pppoe_hash_lock);
299     }
300     
301     static int pppoe_device_event(struct notifier_block *this,
302     			      unsigned long event, void *ptr)
303     {
304     	struct net_device *dev = (struct net_device *) ptr;
305     
306     	/* Only look at sockets that are using this specific device. */
307     	switch (event) {
308     	case NETDEV_CHANGEMTU:
309     		/* A change in mtu is a bad thing, requiring
310     		 * LCP re-negotiation.
311     		 */
312     
313     	case NETDEV_GOING_DOWN:
314     	case NETDEV_DOWN:
315     		/* Find every socket on this device and kill it. */
316     		pppoe_flush_dev(dev);
317     		break;
318     
319     	default:
320     		break;
321     	};
322     
323     	return NOTIFY_DONE;
324     }
325     
326     
327     static struct notifier_block pppoe_notifier = {
328     	notifier_call: pppoe_device_event,
329     };
330     
331     
332     
333     
334     /************************************************************************
335      *
336      * Do the real work of receiving a PPPoE Session frame.
337      *
338      ***********************************************************************/
339     int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb)
340     {
341     	struct pppox_opt *po = sk->protinfo.pppox;
342     	struct pppox_opt *relay_po = NULL;
343     
344     	if (sk->state & PPPOX_BOUND) {
345     		skb_pull(skb, sizeof(struct pppoe_hdr));
346     		ppp_input(&po->chan, skb);
347     	} else if (sk->state & PPPOX_RELAY) {
348     		relay_po = get_item_by_addr(&po->pppoe_relay);
349     
350     		if (relay_po == NULL)
351     			goto abort_kfree;
352     			
353     		if ((relay_po->sk->state & PPPOX_CONNECTED) == 0)
354     			goto abort_put;
355     
356     		skb_pull(skb, sizeof(struct pppoe_hdr));
357     		if (!__pppoe_xmit( relay_po->sk , skb))
358     			goto abort_put;
359     	} else {
360     		sock_queue_rcv_skb(sk, skb);
361     	}
362     
363     	return NET_RX_SUCCESS;
364     
365     abort_put:
366     	sock_put(relay_po->sk);
367     
368     abort_kfree:
369     	kfree_skb(skb);
370     	return NET_RX_DROP;
371     }
372     
373     /************************************************************************
374      *
375      * Receive wrapper called in BH context.
376      *
377      ***********************************************************************/
378     static int pppoe_rcv(struct sk_buff *skb,
379     		      struct net_device *dev,
380     		      struct packet_type *pt)
381     
382     {
383     	struct pppoe_hdr *ph = (struct pppoe_hdr *) skb->nh.raw;
384     	struct pppox_opt *po;
385     	struct sock *sk ;
386     	int ret;
387     
388     	po = get_item((unsigned long) ph->sid, skb->mac.ethernet->h_source);
389     
390     	if (!po) {
391     		kfree_skb(skb);
392     		return NET_RX_DROP;
393     	}
394     
395     	sk = po->sk;
396             bh_lock_sock(sk);
397     
398     	/* Socket state is unknown, must put skb into backlog. */
399     	if (sk->lock.users != 0) {
400     		sk_add_backlog(sk, skb);
401     		ret = NET_RX_SUCCESS;
402     	} else {
403     		ret = pppoe_rcv_core(sk, skb);
404     	}
405     
406     	bh_unlock_sock(sk);
407     	sock_put(sk);
408     
409     	return ret;
410     }
411     
412     /************************************************************************
413      *
414      * Receive a PPPoE Discovery frame.
415      * This is solely for detection of PADT frames
416      *
417      ***********************************************************************/
418     static int pppoe_disc_rcv(struct sk_buff *skb,
419     			  struct net_device *dev,
420     			  struct packet_type *pt)
421     
422     {
423     	struct pppoe_hdr *ph = (struct pppoe_hdr *) skb->nh.raw;
424     	struct pppox_opt *po;
425     
426     	if (ph->code != PADT_CODE)
427     		goto abort;
428     
429     	po = get_item((unsigned long) ph->sid, skb->mac.ethernet->h_source);
430     	if (po) {
431     		struct sock *sk = po->sk;
432     
433     		bh_lock_sock(sk);
434     
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.
439     		 */
440     		if (sk->lock.users == 0)
441     			pppox_unbind_sock(sk);
442     
443     		bh_unlock_sock(sk);
444     		sock_put(sk);
445     	}
446     
447     abort:
448     	kfree_skb(skb);
449     	return NET_RX_SUCCESS; /* Lies... :-) */
450     }
451     
452     struct packet_type pppoes_ptype = {
453     	type:	__constant_htons(ETH_P_PPP_SES),
454     	func:	pppoe_rcv,
455     };
456     
457     struct packet_type pppoed_ptype = {
458     	type:	__constant_htons(ETH_P_PPP_DISC),
459     	func:	pppoe_disc_rcv,
460     };
461     
462     /***********************************************************************
463      *
464      * Really kill the socket. (Called from sock_put if refcnt == 0.)
465      *
466      **********************************************************************/
467     void pppoe_sock_destruct(struct sock *sk)
468     {
469     	if (sk->protinfo.destruct_hook)
470     		kfree(sk->protinfo.destruct_hook);
471     	MOD_DEC_USE_COUNT;
472     }
473     
474     
475     /***********************************************************************
476      *
477      * Initialize a new struct sock.
478      *
479      **********************************************************************/
480     static int pppoe_create(struct socket *sock)
481     {
482     	int error = 0;
483     	struct sock *sk;
484     
485     	MOD_INC_USE_COUNT;
486     
487     	sk = sk_alloc(PF_PPPOX, GFP_KERNEL, 1);
488     	if (!sk)
489     		return -ENOMEM;
490     
491     	sock_init_data(sock, sk);
492     
493     	sock->state = SS_UNCONNECTED;
494     	sock->ops   = &pppoe_ops;
495     
496     	sk->protocol = PX_PROTO_OE;
497     	sk->family = PF_PPPOX;
498     
499     	sk->backlog_rcv = pppoe_rcv_core;
500     	sk->next = NULL;
501     	sk->pprev = NULL;
502     	sk->state = PPPOX_NONE;
503     	sk->type = SOCK_STREAM;
504     	sk->destruct = pppoe_sock_destruct;
505     
506     	sk->protinfo.pppox = kmalloc(sizeof(struct pppox_opt), GFP_KERNEL);
507     	if (!sk->protinfo.pppox) {
508     		error = -ENOMEM;
509     		goto free_sk;
510     	}
511     
512     	memset((void *) sk->protinfo.pppox, 0, sizeof(struct pppox_opt));
513     	sk->protinfo.pppox->sk = sk;
514     
515     	/* Delete the protinfo when it is time to do so. */
516     	sk->protinfo.destruct_hook = sk->protinfo.pppox;
517     	sock->sk = sk;
518     
519     	return 0;
520     
521     free_sk:
522     	sk_free(sk);
523     	return error;
524     }
525     
526     int pppoe_release(struct socket *sock)
527     {
528     	struct sock *sk = sock->sk;
529     	struct pppox_opt *po;
530     	int error = 0;
531     
532     	if (!sk)
533     		return 0;
534     
535     	if (sk->dead != 0)
536     		return -EBADF;
537     
538     	pppox_unbind_sock(sk);
539     
540     	/* Signal the death of the socket. */
541     	sk->state = PPPOX_DEAD;
542     
543     	po = sk->protinfo.pppox;
544     	if (po->pppoe_pa.sid)
545     		delete_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
546     
547     	if (po->pppoe_dev)
548     	    dev_put(po->pppoe_dev);
549     
550     	sock_orphan(sk);
551     	sock->sk = NULL;
552     
553     	skb_queue_purge(&sk->receive_queue);
554     	sock_put(sk);
555     
556     	return error;
557     }
558     
559     
560     int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
561     		  int sockaddr_len, int flags)
562     {
563     	struct sock *sk = sock->sk;
564     	struct net_device *dev = NULL;
565     	struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
566     	struct pppox_opt *po = sk->protinfo.pppox;
567     	int error;
568     
569     	lock_sock(sk);
570     
571     	error = -EINVAL;
572     	if (sp->sa_protocol != PX_PROTO_OE)
573     		goto end;
574     
575     	/* Check for already bound sockets */
576     	error = -EBUSY;
577     	if ((sk->state & PPPOX_CONNECTED) && sp->sa_addr.pppoe.sid)
578     		goto end;
579     
580     	/* Check for already disconnected sockets,
581     	   on attempts to disconnect */
582     	error = -EALREADY;
583     	if((sk->state & PPPOX_DEAD) && !sp->sa_addr.pppoe.sid )
584     		goto end;
585     
586     	error = 0;
587     	if (po->pppoe_pa.sid) {
588     		pppox_unbind_sock(sk);
589     
590     		/* Delete the old binding */
591     		delete_item(po->pppoe_pa.sid,po->pppoe_pa.remote);
592     
593     		dev_put(po->pppoe_dev);
594     
595     		memset(po, 0, sizeof(struct pppox_opt));
596     		po->sk = sk;
597     
598     		sk->state = PPPOX_NONE;
599     	}
600     
601     	/* Don't re-bind if sid==0 */
602     	if (sp->sa_addr.pppoe.sid != 0) {
603     		dev = dev_get_by_name(sp->sa_addr.pppoe.dev);
604     
605     		error = -ENODEV;
606     		if (!dev)
607     			goto end;
608     
609     		po->pppoe_dev = dev;
610     
611     		if (!(dev->flags & IFF_UP))
612     			goto err_put;
613     
614     		memcpy(&po->pppoe_pa,
615     		       &sp->sa_addr.pppoe,
616     		       sizeof(struct pppoe_addr));
617     
618     		error = set_item(po);
619     		if (error < 0)
620     			goto err_put;
621     
622     		po->chan.hdrlen = (sizeof(struct pppoe_hdr) +
623     				   dev->hard_header_len);
624     
625     		po->chan.private = sk;
626     		po->chan.ops = &pppoe_chan_ops;
627     
628     		error = ppp_register_channel(&po->chan);
629     		if (error)
630     			goto err_put;
631     
632     		sk->state = PPPOX_CONNECTED;
633     	}
634     
635     	sk->num = sp->sa_addr.pppoe.sid;
636     
637      end:
638     	release_sock(sk);
639     	return error;
640     err_put:
641     	dev_put(po->pppoe_dev);
642     	po->pppoe_dev = NULL;
643     	goto end;
644     }
645     
646     
647     int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
648     		  int *usockaddr_len, int peer)
649     {
650     	int len = sizeof(struct sockaddr_pppox);
651     	struct sockaddr_pppox sp;
652     
653     	sp.sa_family	= AF_PPPOX;
654     	sp.sa_protocol	= PX_PROTO_OE;
655     	memcpy(&sp.sa_addr.pppoe, &sock->sk->protinfo.pppox->pppoe_pa,
656     	       sizeof(struct pppoe_addr));
657     
658     	memcpy(uaddr, &sp, len);
659     
660     	*usockaddr_len = len;
661     
662     	return 0;
663     }
664     
665     
666     int pppoe_ioctl(struct socket *sock, unsigned int cmd,
667     		unsigned long arg)
668     {
669     	struct sock *sk = sock->sk;
670     	struct pppox_opt *po;
671     	int val = 0;
672     	int err = 0;
673     
674     	po = sk->protinfo.pppox;
675     	switch (cmd) {
676     	case PPPIOCGMRU:
677     		err = -ENXIO;
678     
679     		if (!(sk->state & PPPOX_CONNECTED))
680     			break;
681     
682     		err = -EFAULT;
683     		if (put_user(po->pppoe_dev->mtu -
684     			     sizeof(struct pppoe_hdr) -
685     			     PPP_HDRLEN,
686     			     (int *) arg))
687     			break;
688     		err = 0;
689     		break;
690     
691     	case PPPIOCSMRU:
692     		err = -ENXIO;
693     		if (!(sk->state & PPPOX_CONNECTED))
694     			break;
695     
696     		err = -EFAULT;
697     		if (get_user(val,(int *) arg))
698     			break;
699     
700     		if (val < (po->pppoe_dev->mtu
701     			   - sizeof(struct pppoe_hdr)
702     			   - PPP_HDRLEN))
703     			err = 0;
704     		else
705     			err = -EINVAL;
706     		break;
707     
708     	case PPPIOCSFLAGS:
709     		err = -EFAULT;
710     		if (get_user(val, (int *) arg))
711     			break;
712     		err = 0;
713     		break;
714     
715     	case PPPOEIOCSFWD:
716     	{
717     		struct pppox_opt *relay_po;
718     
719     		err = -EBUSY;
720     		if (sk->state & PPPOX_BOUND)
721     			break;
722     
723     		err = -ENOTCONN;
724     		if (!(sk->state & PPPOX_CONNECTED))
725     			break;
726     
727     		/* PPPoE address from the user specifies an outbound
728     		   PPPoE address to which frames are forwarded to */
729     		err = -EFAULT;
730     		if (copy_from_user(&po->pppoe_relay,
731     				   (void*)arg,
732     				   sizeof(struct sockaddr_pppox)))
733     			break;
734     
735     		err = -EINVAL;
736     		if (po->pppoe_relay.sa_family != AF_PPPOX ||
737     		    po->pppoe_relay.sa_protocol!= PX_PROTO_OE)
738     			break;
739     
740     		/* Check that the socket referenced by the address
741     		   actually exists. */
742     		relay_po = get_item_by_addr(&po->pppoe_relay);
743     
744     		if (!relay_po)
745     			break;
746     
747     		sock_put(relay_po->sk);
748     		sk->state |= PPPOX_RELAY;
749     		err = 0;
750     		break;
751     	}
752     
753     	case PPPOEIOCDFWD:
754     		err = -EALREADY;
755     		if (!(sk->state & PPPOX_RELAY))
756     			break;
757     
758     		sk->state &= ~PPPOX_RELAY;
759     		err = 0;
760     		break;
761     
762     	default:;
763     	};
764     
765     	return err;
766     }
767     
768     
769     int pppoe_sendmsg(struct socket *sock, struct msghdr *m,
770     		  int total_len, struct scm_cookie *scm)
771     {
772     	struct sk_buff *skb = NULL;
773     	struct sock *sk = sock->sk;
774     	int error = 0;
775     	struct pppoe_hdr hdr;
776     	struct pppoe_hdr *ph;
777     	struct net_device *dev;
778     	char *start;
779     
780     	if (sk->dead || !(sk->state & PPPOX_CONNECTED)) {
781     		error = -ENOTCONN;
782     		goto end;
783     	}
784     
785     	hdr.ver = 1;
786     	hdr.type = 1;
787     	hdr.code = 0;
788     	hdr.sid = sk->num;
789     
790     	lock_sock(sk);
791     
792     	dev = sk->protinfo.pppox->pppoe_dev;
793     
794     	error = -EMSGSIZE;
795      	if (total_len > (dev->mtu + dev->hard_header_len))
796     		goto end;
797     
798     
799     	skb = sock_wmalloc(sk, total_len + dev->hard_header_len + 32,
800     			   0, GFP_KERNEL);
801     	if (!skb) {
802     		error = -ENOMEM;
803     		goto end;
804     	}
805     
806     	/* Reserve space for headers. */
807     	skb_reserve(skb, dev->hard_header_len);
808     	skb->nh.raw = skb->data;
809     
810     	skb->dev = dev;
811     
812     	skb->priority = sk->priority;
813     	skb->protocol = __constant_htons(ETH_P_PPP_SES);
814     
815     	ph = (struct pppoe_hdr *) skb_put(skb, total_len + sizeof(struct pppoe_hdr));
816     	start = (char *) &ph->tag[0];
817     
818     	error = memcpy_fromiovec(start, m->msg_iov, total_len);
819     
820     	if (error < 0) {
821     		kfree_skb(skb);
822     		goto end;
823     	}
824     
825     	error = total_len;
826     	dev->hard_header(skb, dev, ETH_P_PPP_SES,
827     			 sk->protinfo.pppox->pppoe_pa.remote,
828     			 NULL, total_len);
829     
830     	memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
831     
832     	ph->length = htons(total_len);
833     
834     	dev_queue_xmit(skb);
835     
836     end:
837     	release_sock(sk);
838     	return error;
839     }
840     
841     
842     /************************************************************************
843      *
844      * xmit function for internal use.
845      *
846      ***********************************************************************/
847     int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
848     {
849     	struct net_device *dev = sk->protinfo.pppox->pppoe_dev;
850     	struct pppoe_hdr hdr;
851     	struct pppoe_hdr *ph;
852     	int headroom = skb_headroom(skb);
853     	int data_len = skb->len;
854     	struct sk_buff *skb2;
855     
856     	if (sk->dead  || !(sk->state & PPPOX_CONNECTED))
857     		goto abort;
858     
859     	hdr.ver	= 1;
860     	hdr.type = 1;
861     	hdr.code = 0;
862     	hdr.sid	= sk->num;
863     	hdr.length = htons(skb->len);
864     
865     	if (!dev)
866     		goto abort;
867     
868     	/* Copy the skb if there is no space for the header. */
869     	if (headroom < (sizeof(struct pppoe_hdr) + dev->hard_header_len)) {
870     		skb2 = dev_alloc_skb(32+skb->len +
871     				     sizeof(struct pppoe_hdr) +
872     				     dev->hard_header_len);
873     
874     		if (skb2 == NULL)
875     			goto abort;
876     
877     		skb_reserve(skb2, dev->hard_header_len + sizeof(struct pppoe_hdr));
878     		memcpy(skb_put(skb2, skb->len), skb->data, skb->len);
879     	} else {
880     		/* Make a clone so as to not disturb the original skb,
881     		 * give dev_queue_xmit something it can free.
882     		 */
883     		skb2 = skb_clone(skb, GFP_ATOMIC);
884     	}
885     
886     	ph = (struct pppoe_hdr *) skb_push(skb2, sizeof(struct pppoe_hdr));
887     	memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
888     	skb2->protocol = __constant_htons(ETH_P_PPP_SES);
889     
890     	skb2->nh.raw = skb2->data;
891     
892     	skb2->dev = dev;
893     
894     	dev->hard_header(skb2, dev, ETH_P_PPP_SES,
895     			 sk->protinfo.pppox->pppoe_pa.remote,
896     			 NULL, data_len);
897     
898     	/* We're transmitting skb2, and assuming that dev_queue_xmit
899     	 * will free it.  The generic ppp layer however, is expecting
900     	 * that we give back 'skb' (not 'skb2') in case of failure,
901     	 * but free it in case of success.
902     	 */
903     
904     	if (dev_queue_xmit(skb2) < 0)
905     		goto abort;
906     
907     	kfree_skb(skb);
908     	return 1;
909     
910     abort:
911     	return 0;
912     }
913     
914     
915     /************************************************************************
916      *
917      * xmit function called by generic PPP driver
918      * sends PPP frame over PPPoE socket
919      *
920      ***********************************************************************/
921     int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
922     {
923     	struct sock *sk = (struct sock *) chan->private;
924     	return __pppoe_xmit(sk, skb);
925     }
926     
927     
928     struct ppp_channel_ops pppoe_chan_ops = { pppoe_xmit , NULL };
929     
930     int pppoe_rcvmsg(struct socket *sock, struct msghdr *m, int total_len, int flags, struct scm_cookie *scm)
931     {
932     	struct sock *sk = sock->sk;
933     	struct sk_buff *skb = NULL;
934     	int error = 0;
935     	int len;
936     	struct pppoe_hdr *ph = NULL;
937     
938     	if (sk->state & PPPOX_BOUND) {
939     		error = -EIO;
940     		goto end;
941     	}
942     
943     	skb = skb_recv_datagram(sk, flags, 0, &error);
944     
945     	if (error < 0) {
946     		goto end;
947     	}
948     
949     	m->msg_namelen = 0;
950     
951     	if (skb) {
952     		error = 0;
953     		ph = (struct pppoe_hdr *) skb->nh.raw;
954     		len = ntohs(ph->length);
955     
956     		error = memcpy_toiovec(m->msg_iov, (unsigned char *) &ph->tag[0], len);
957     		if (error < 0)
958     			goto do_skb_free;
959     		error = len;
960     	}
961     
962     do_skb_free:
963     	if (skb)
964     		kfree_skb(skb);
965     end:
966     	return error;
967     }
968     
969     int pppoe_proc_info(char *buffer, char **start, off_t offset, int length)
970     {
971     	struct pppox_opt *po;
972     	int len = 0;
973     	off_t pos = 0;
974     	off_t begin = 0;
975     	int size;
976     	int i;
977     
978     	len += sprintf(buffer,
979     		       "Id       Address              Device\n");
980     	pos = len;
981     
982     	write_lock_bh(&pppoe_hash_lock);
983     
984     	for (i = 0; i < PPPOE_HASH_SIZE; i++) {
985     		po = item_hash_table[i];
986     		while (po) {
987     			char *dev = po->pppoe_pa.dev;
988     
989     			size = sprintf(buffer + len,
990     				       "%08X %02X:%02X:%02X:%02X:%02X:%02X %8s\n",
991     				       po->pppoe_pa.sid,
992     				       po->pppoe_pa.remote[0],
993     				       po->pppoe_pa.remote[1],
994     				       po->pppoe_pa.remote[2],
995     				       po->pppoe_pa.remote[3],
996     				       po->pppoe_pa.remote[4],
997     				       po->pppoe_pa.remote[5],
998     				       dev);
999     			len += size;
1000     			pos += size;
1001     			if (pos < offset) {
1002     				len = 0;
1003     				begin = pos;
1004     			}
1005     
1006     			if (pos > offset + length)
1007     				break;
1008     
1009     			po = po->next;
1010     		}
1011     
1012     		if (po)
1013     			break;
1014       	}
1015     	write_unlock_bh(&pppoe_hash_lock);
1016     
1017       	*start = buffer + (offset - begin);
1018       	len -= (offset - begin);
1019       	if (len > length)
1020       		len = length;
1021     	if (len < 0)
1022     		len = 0;
1023       	return len;
1024     }
1025     
1026     
1027     struct proto_ops pppoe_ops = {
1028         family:		AF_PPPOX,
1029         release:		pppoe_release,
1030         bind:		sock_no_bind,
1031         connect:		pppoe_connect,
1032         socketpair:		sock_no_socketpair,
1033         accept:		sock_no_accept,
1034         getname:		pppoe_getname,
1035         poll:		datagram_poll,
1036         ioctl:		pppoe_ioctl,
1037         listen:		sock_no_listen,
1038         shutdown:		sock_no_shutdown,
1039         setsockopt:		sock_no_setsockopt,
1040         getsockopt:		sock_no_getsockopt,
1041         sendmsg:		pppoe_sendmsg,
1042         recvmsg:		pppoe_rcvmsg,
1043         mmap:		sock_no_mmap
1044     };
1045     
1046     struct pppox_proto pppoe_proto = {
1047         create:	pppoe_create,
1048         ioctl:	pppoe_ioctl
1049     };
1050     
1051     
1052     int __init pppoe_init(void)
1053     {
1054      	int err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
1055     
1056     	if (err == 0) {
1057     		dev_add_pack(&pppoes_ptype);
1058     		dev_add_pack(&pppoed_ptype);
1059     		register_netdevice_notifier(&pppoe_notifier);
1060     		proc_net_create("pppoe", 0, pppoe_proc_info);
1061     	}
1062     	return err;
1063     }
1064     
1065     void __exit pppoe_exit(void)
1066     {
1067     	unregister_pppox_proto(PX_PROTO_OE);
1068     	dev_remove_pack(&pppoes_ptype);
1069     	dev_remove_pack(&pppoed_ptype);
1070     	unregister_netdevice_notifier(&pppoe_notifier);
1071     	proc_net_remove("pppoe");
1072     }
1073     
1074     module_init(pppoe_init);
1075     module_exit(pppoe_exit);
1076