VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / 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.7.0
9  *
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)
45  *
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.
51  *
52  */
53
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>
74
75 #include <net/sock.h>
76
77 #include <asm/uaccess.h>
78
79 #define PPPOE_HASH_BITS 4
80 #define PPPOE_HASH_SIZE (1<<PPPOE_HASH_BITS)
81
82 static struct ppp_channel_ops pppoe_chan_ops;
83
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);
87
88 static struct proto_ops pppoe_ops;
89 static rwlock_t pppoe_hash_lock = RW_LOCK_UNLOCKED;
90
91 static struct ppp_channel_ops pppoe_chan_ops;
92
93 static inline int cmp_2_addr(struct pppoe_addr *a, struct pppoe_addr *b)
94 {
95         return (a->sid == b->sid &&
96                 (memcmp(a->remote, b->remote, ETH_ALEN) == 0));
97 }
98
99 static inline int cmp_addr(struct pppoe_addr *a, unsigned long sid, char *addr)
100 {
101         return (a->sid == sid &&
102                 (memcmp(a->remote,addr,ETH_ALEN) == 0));
103 }
104
105 static int hash_item(unsigned long sid, unsigned char *addr)
106 {
107         char hash = 0;
108         int i, j;
109
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 );
113                 }
114         }
115
116         for (i = 0; i < (sizeof(unsigned long)*8) / PPPOE_HASH_BITS ; ++i)
117                 hash ^= sid >> (i*PPPOE_HASH_BITS);
118
119         return hash & ( PPPOE_HASH_SIZE - 1 );
120 }
121
122 /* zeroed because its in .bss */
123 static struct pppox_opt *item_hash_table[PPPOE_HASH_SIZE];
124
125 /**********************************************************************
126  *
127  *  Set/get/delete/rehash items  (internal versions)
128  *
129  **********************************************************************/
130 static struct pppox_opt *__get_item(unsigned long sid, unsigned char *addr)
131 {
132         int hash = hash_item(sid, addr);
133         struct pppox_opt *ret;
134
135         ret = item_hash_table[hash];
136
137         while (ret && !cmp_addr(&ret->pppoe_pa, sid, addr))
138                 ret = ret->next;
139
140         return ret;
141 }
142
143 static int __set_item(struct pppox_opt *po)
144 {
145         int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
146         struct pppox_opt *ret;
147
148         ret = item_hash_table[hash];
149         while (ret) {
150                 if (cmp_2_addr(&ret->pppoe_pa, &po->pppoe_pa))
151                         return -EALREADY;
152
153                 ret = ret->next;
154         }
155
156         if (!ret) {
157                 po->next = item_hash_table[hash];
158                 item_hash_table[hash] = po;
159         }
160
161         return 0;
162 }
163
164 static struct pppox_opt *__delete_item(unsigned long sid, char *addr)
165 {
166         int hash = hash_item(sid, addr);
167         struct pppox_opt *ret, **src;
168
169         ret = item_hash_table[hash];
170         src = &item_hash_table[hash];
171
172         while (ret) {
173                 if (cmp_addr(&ret->pppoe_pa, sid, addr)) {
174                         *src = ret->next;
175                         break;
176                 }
177
178                 src = &ret->next;
179                 ret = ret->next;
180         }
181
182         return ret;
183 }
184
185 /**********************************************************************
186  *
187  *  Set/get/delete/rehash items
188  *
189  **********************************************************************/
190 static inline struct pppox_opt *get_item(unsigned long sid,
191                                          unsigned char *addr)
192 {
193         struct pppox_opt *po;
194
195         read_lock_bh(&pppoe_hash_lock);
196         po = __get_item(sid, addr);
197         if (po)
198                 sock_hold(po->sk);
199         read_unlock_bh(&pppoe_hash_lock);
200
201         return po;
202 }
203
204 static inline struct pppox_opt *get_item_by_addr(struct sockaddr_pppox *sp)
205 {
206         return get_item(sp->sa_addr.pppoe.sid, sp->sa_addr.pppoe.remote);
207 }
208
209 static inline int set_item(struct pppox_opt *po)
210 {
211         int i;
212
213         if (!po)
214                 return -EINVAL;
215
216         write_lock_bh(&pppoe_hash_lock);
217         i = __set_item(po);
218         write_unlock_bh(&pppoe_hash_lock);
219
220         return i;
221 }
222
223 static inline struct pppox_opt *delete_item(unsigned long sid, char *addr)
224 {
225         struct pppox_opt *ret;
226
227         write_lock_bh(&pppoe_hash_lock);
228         ret = __delete_item(sid, addr);
229         write_unlock_bh(&pppoe_hash_lock);
230
231         return ret;
232 }
233
234
235
236 /***************************************************************************
237  *
238  *  Handler for device events.
239  *  Certain device events require that sockets be unconnected.
240  *
241  **************************************************************************/
242
243 static void pppoe_flush_dev(struct net_device *dev)
244 {
245         int hash;
246
247         BUG_ON(dev == NULL);
248
249         read_lock_bh(&pppoe_hash_lock);
250         for (hash = 0; hash < PPPOE_HASH_SIZE; hash++) {
251                 struct pppox_opt *po = item_hash_table[hash];
252
253                 while (po != NULL) {
254                         if (po->pppoe_dev == dev) {
255                                 struct sock *sk = po->sk;
256
257                                 sock_hold(sk);
258                                 po->pppoe_dev = NULL;
259
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).
263                                  */
264                                 read_unlock_bh(&pppoe_hash_lock);
265
266                                 lock_sock(sk);
267
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);
274                                 }
275
276                                 release_sock(sk);
277
278                                 sock_put(sk);
279
280                                 read_lock_bh(&pppoe_hash_lock);
281
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.
286                                  */
287                                 po = item_hash_table[hash];
288                                 continue;
289                         }
290                         po = po->next;
291                 }
292         }
293         read_unlock_bh(&pppoe_hash_lock);
294 }
295
296 static int pppoe_device_event(struct notifier_block *this,
297                               unsigned long event, void *ptr)
298 {
299         struct net_device *dev = (struct net_device *) ptr;
300
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.
306                  */
307
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;
313
314         default:
315                 break;
316         };
317
318         return NOTIFY_DONE;
319 }
320
321
322 static struct notifier_block pppoe_notifier = {
323         .notifier_call = pppoe_device_event,
324 };
325
326
327 /************************************************************************
328  *
329  * Do the real work of receiving a PPPoE Session frame.
330  *
331  ***********************************************************************/
332 static int pppoe_rcv_core(struct sock *sk, struct sk_buff *skb)
333 {
334         struct pppox_opt *po = pppox_sk(sk);
335         struct pppox_opt *relay_po = NULL;
336
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_trim(skb, len);
342
343                 ppp_input(&po->chan, skb);
344         } else if (sk->sk_state & PPPOX_RELAY) {
345                 relay_po = get_item_by_addr(&po->pppoe_relay);
346
347                 if (relay_po == NULL)
348                         goto abort_kfree;
349
350                 if ((relay_po->sk->sk_state & PPPOX_CONNECTED) == 0)
351                         goto abort_put;
352
353                 skb_pull(skb, sizeof(struct pppoe_hdr));
354                 if (!__pppoe_xmit( relay_po->sk, skb))
355                         goto abort_put;
356         } else {
357                 if (sock_queue_rcv_skb(sk, skb))
358                         goto abort_kfree;
359         }
360
361         return NET_RX_SUCCESS;
362
363 abort_put:
364         sock_put(relay_po->sk);
365
366 abort_kfree:
367         kfree_skb(skb);
368         return NET_RX_DROP;
369 }
370
371 /************************************************************************
372  *
373  * Receive wrapper called in BH context.
374  *
375  ***********************************************************************/
376 static int pppoe_rcv(struct sk_buff *skb,
377                      struct net_device *dev,
378                      struct packet_type *pt)
379
380 {
381         struct pppoe_hdr *ph;
382         struct pppox_opt *po;
383         struct sock *sk;
384         int ret;
385
386         if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
387                 goto drop;
388
389         if (!(skb = skb_share_check(skb, GFP_ATOMIC))) 
390                 goto out;
391
392         ph = (struct pppoe_hdr *) skb->nh.raw;
393
394         po = get_item((unsigned long) ph->sid, skb->mac.ethernet->h_source);
395         if (!po) 
396                 goto drop;
397
398         sk = po->sk;
399         bh_lock_sock(sk);
400
401         /* Socket state is unknown, must put skb into backlog. */
402         if (sock_owned_by_user(sk) != 0) {
403                 sk_add_backlog(sk, skb);
404                 ret = NET_RX_SUCCESS;
405         } else {
406                 ret = pppoe_rcv_core(sk, skb);
407         }
408
409         bh_unlock_sock(sk);
410         sock_put(sk);
411
412         return ret;
413 drop:
414         kfree_skb(skb);
415 out:
416         return NET_RX_DROP;
417 }
418
419 /************************************************************************
420  *
421  * Receive a PPPoE Discovery frame.
422  * This is solely for detection of PADT frames
423  *
424  ***********************************************************************/
425 static int pppoe_disc_rcv(struct sk_buff *skb,
426                           struct net_device *dev,
427                           struct packet_type *pt)
428
429 {
430         struct pppoe_hdr *ph;
431         struct pppox_opt *po;
432
433         if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
434                 goto abort;
435
436         if (!(skb = skb_share_check(skb, GFP_ATOMIC))) 
437                 goto out;
438
439         ph = (struct pppoe_hdr *) skb->nh.raw;
440         if (ph->code != PADT_CODE)
441                 goto abort;
442
443         po = get_item((unsigned long) ph->sid, skb->mac.ethernet->h_source);
444         if (po) {
445                 struct sock *sk = po->sk;
446
447                 bh_lock_sock(sk);
448
449                 /* If the user has locked the socket, just ignore
450                  * the packet.  With the way two rcv protocols hook into
451                  * one socket family type, we cannot (easily) distinguish
452                  * what kind of SKB it is during backlog rcv.
453                  */
454                 if (sock_owned_by_user(sk) == 0) {
455                         /* We're no longer connect at the PPPOE layer,
456                          * and must wait for ppp channel to disconnect us.
457                          */
458                         sk->sk_state = PPPOX_ZOMBIE;
459                 }
460
461                 bh_unlock_sock(sk);
462                 sock_put(sk);
463         }
464
465 abort:
466         kfree_skb(skb);
467 out:
468         return NET_RX_SUCCESS; /* Lies... :-) */
469 }
470
471 static struct packet_type pppoes_ptype = {
472         .type   = __constant_htons(ETH_P_PPP_SES),
473         .func   = pppoe_rcv,
474 };
475
476 static struct packet_type pppoed_ptype = {
477         .type   = __constant_htons(ETH_P_PPP_DISC),
478         .func   = pppoe_disc_rcv,
479 };
480
481 /***********************************************************************
482  *
483  * Really kill the socket. (Called from pppox_sk_free if refcnt == 0.)
484  *
485  **********************************************************************/
486 static void pppoe_sk_free(struct sock *sk)
487 {
488         struct pppox_opt *po = pppox_sk(sk);
489
490         if (po)
491                 kfree(po);
492 }
493
494
495 /***********************************************************************
496  *
497  * Initialize a new struct sock.
498  *
499  **********************************************************************/
500 static int pppoe_create(struct socket *sock)
501 {
502         int error = -ENOMEM;
503         struct sock *sk;
504         struct pppox_opt *po;
505
506         sk = sk_alloc(PF_PPPOX, GFP_KERNEL, 1, NULL);
507         if (!sk)
508                 goto out;
509
510         sock_init_data(sock, sk);
511         sk_set_owner(sk, THIS_MODULE);
512         sock->state = SS_UNCONNECTED;
513         sock->ops   = &pppoe_ops;
514
515         sk->sk_backlog_rcv = pppoe_rcv_core;
516         sk->sk_state       = PPPOX_NONE;
517         sk->sk_type        = SOCK_STREAM;
518         sk->sk_family      = PF_PPPOX;
519         sk->sk_protocol    = PX_PROTO_OE;
520         sk->sk_destruct    = pppoe_sk_free;
521
522         po = sk->sk_protinfo = kmalloc(sizeof(*po), GFP_KERNEL);
523         if (!po)
524                 goto frees;
525         memset(po, 0, sizeof(*po));
526         po->sk = sk;
527         error = 0;
528 out:    return error;
529 frees:  sk_free(sk);
530         goto out;
531 }
532
533 static int pppoe_release(struct socket *sock)
534 {
535         struct sock *sk = sock->sk;
536         struct pppox_opt *po;
537         int error = 0;
538
539         if (!sk)
540                 return 0;
541
542         if (sock_flag(sk, SOCK_DEAD))
543                 return -EBADF;
544
545         pppox_unbind_sock(sk);
546
547         /* Signal the death of the socket. */
548         sk->sk_state = PPPOX_DEAD;
549
550         po = pppox_sk(sk);
551         if (po->pppoe_pa.sid) {
552                 delete_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
553         }
554
555         if (po->pppoe_dev)
556                 dev_put(po->pppoe_dev);
557
558         po->pppoe_dev = NULL;
559
560         sock_orphan(sk);
561         sock->sk = NULL;
562
563         skb_queue_purge(&sk->sk_receive_queue);
564         sock_put(sk);
565
566         return error;
567 }
568
569
570 static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
571                   int sockaddr_len, int flags)
572 {
573         struct sock *sk = sock->sk;
574         struct net_device *dev = NULL;
575         struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
576         struct pppox_opt *po = pppox_sk(sk);
577         int error;
578
579         lock_sock(sk);
580
581         error = -EINVAL;
582         if (sp->sa_protocol != PX_PROTO_OE)
583                 goto end;
584
585         /* Check for already bound sockets */
586         error = -EBUSY;
587         if ((sk->sk_state & PPPOX_CONNECTED) && sp->sa_addr.pppoe.sid)
588                 goto end;
589
590         /* Check for already disconnected sockets, on attempts to disconnect */
591         error = -EALREADY;
592         if ((sk->sk_state & PPPOX_DEAD) && !sp->sa_addr.pppoe.sid )
593                 goto end;
594
595         error = 0;
596         if (po->pppoe_pa.sid) {
597                 pppox_unbind_sock(sk);
598
599                 /* Delete the old binding */
600                 delete_item(po->pppoe_pa.sid,po->pppoe_pa.remote);
601
602                 if(po->pppoe_dev)
603                         dev_put(po->pppoe_dev);
604
605                 memset(po, 0, sizeof(struct pppox_opt));
606                 po->sk = sk;
607
608                 sk->sk_state = PPPOX_NONE;
609         }
610
611         /* Don't re-bind if sid==0 */
612         if (sp->sa_addr.pppoe.sid != 0) {
613                 dev = dev_get_by_name(sp->sa_addr.pppoe.dev);
614
615                 error = -ENODEV;
616                 if (!dev)
617                         goto end;
618
619                 po->pppoe_dev = dev;
620
621                 if (!(dev->flags & IFF_UP))
622                         goto err_put;
623
624                 memcpy(&po->pppoe_pa,
625                        &sp->sa_addr.pppoe,
626                        sizeof(struct pppoe_addr));
627
628                 error = set_item(po);
629                 if (error < 0)
630                         goto err_put;
631
632                 po->chan.hdrlen = (sizeof(struct pppoe_hdr) +
633                                    dev->hard_header_len);
634
635                 po->chan.private = sk;
636                 po->chan.ops = &pppoe_chan_ops;
637
638                 error = ppp_register_channel(&po->chan);
639                 if (error)
640                         goto err_put;
641
642                 sk->sk_state = PPPOX_CONNECTED;
643         }
644
645         po->num = sp->sa_addr.pppoe.sid;
646
647  end:
648         release_sock(sk);
649         return error;
650 err_put:
651         if (po->pppoe_dev) {
652                 dev_put(po->pppoe_dev);
653                 po->pppoe_dev = NULL;
654         }
655         goto end;
656 }
657
658
659 static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
660                   int *usockaddr_len, int peer)
661 {
662         int len = sizeof(struct sockaddr_pppox);
663         struct sockaddr_pppox sp;
664
665         sp.sa_family    = AF_PPPOX;
666         sp.sa_protocol  = PX_PROTO_OE;
667         memcpy(&sp.sa_addr.pppoe, &pppox_sk(sock->sk)->pppoe_pa,
668                sizeof(struct pppoe_addr));
669
670         memcpy(uaddr, &sp, len);
671
672         *usockaddr_len = len;
673
674         return 0;
675 }
676
677
678 static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
679                 unsigned long arg)
680 {
681         struct sock *sk = sock->sk;
682         struct pppox_opt *po = pppox_sk(sk);
683         int val = 0;
684         int err = 0;
685
686         switch (cmd) {
687         case PPPIOCGMRU:
688                 err = -ENXIO;
689
690                 if (!(sk->sk_state & PPPOX_CONNECTED))
691                         break;
692
693                 err = -EFAULT;
694                 if (put_user(po->pppoe_dev->mtu -
695                              sizeof(struct pppoe_hdr) -
696                              PPP_HDRLEN,
697                              (int __user *) arg))
698                         break;
699                 err = 0;
700                 break;
701
702         case PPPIOCSMRU:
703                 err = -ENXIO;
704                 if (!(sk->sk_state & PPPOX_CONNECTED))
705                         break;
706
707                 err = -EFAULT;
708                 if (get_user(val,(int __user *) arg))
709                         break;
710
711                 if (val < (po->pppoe_dev->mtu
712                            - sizeof(struct pppoe_hdr)
713                            - PPP_HDRLEN))
714                         err = 0;
715                 else
716                         err = -EINVAL;
717                 break;
718
719         case PPPIOCSFLAGS:
720                 err = -EFAULT;
721                 if (get_user(val, (int __user *) arg))
722                         break;
723                 err = 0;
724                 break;
725
726         case PPPOEIOCSFWD:
727         {
728                 struct pppox_opt *relay_po;
729
730                 err = -EBUSY;
731                 if (sk->sk_state & (PPPOX_BOUND | PPPOX_ZOMBIE | PPPOX_DEAD))
732                         break;
733
734                 err = -ENOTCONN;
735                 if (!(sk->sk_state & PPPOX_CONNECTED))
736                         break;
737
738                 /* PPPoE address from the user specifies an outbound
739                    PPPoE address to which frames are forwarded to */
740                 err = -EFAULT;
741                 if (copy_from_user(&po->pppoe_relay,
742                                    (void __user *)arg,
743                                    sizeof(struct sockaddr_pppox)))
744                         break;
745
746                 err = -EINVAL;
747                 if (po->pppoe_relay.sa_family != AF_PPPOX ||
748                     po->pppoe_relay.sa_protocol!= PX_PROTO_OE)
749                         break;
750
751                 /* Check that the socket referenced by the address
752                    actually exists. */
753                 relay_po = get_item_by_addr(&po->pppoe_relay);
754
755                 if (!relay_po)
756                         break;
757
758                 sock_put(relay_po->sk);
759                 sk->sk_state |= PPPOX_RELAY;
760                 err = 0;
761                 break;
762         }
763
764         case PPPOEIOCDFWD:
765                 err = -EALREADY;
766                 if (!(sk->sk_state & PPPOX_RELAY))
767                         break;
768
769                 sk->sk_state &= ~PPPOX_RELAY;
770                 err = 0;
771                 break;
772
773         default:;
774         };
775
776         return err;
777 }
778
779
780 static int pppoe_sendmsg(struct kiocb *iocb, struct socket *sock, 
781                   struct msghdr *m, size_t total_len)
782 {
783         struct sk_buff *skb = NULL;
784         struct sock *sk = sock->sk;
785         struct pppox_opt *po = pppox_sk(sk);
786         int error = 0;
787         struct pppoe_hdr hdr;
788         struct pppoe_hdr *ph;
789         struct net_device *dev;
790         char *start;
791
792         if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
793                 error = -ENOTCONN;
794                 goto end;
795         }
796
797         hdr.ver = 1;
798         hdr.type = 1;
799         hdr.code = 0;
800         hdr.sid = po->num;
801
802         lock_sock(sk);
803
804         dev = po->pppoe_dev;
805
806         error = -EMSGSIZE;
807         if (total_len > (dev->mtu + dev->hard_header_len))
808                 goto end;
809
810
811         skb = sock_wmalloc(sk, total_len + dev->hard_header_len + 32,
812                            0, GFP_KERNEL);
813         if (!skb) {
814                 error = -ENOMEM;
815                 goto end;
816         }
817
818         /* Reserve space for headers. */
819         skb_reserve(skb, dev->hard_header_len);
820         skb->nh.raw = skb->data;
821
822         skb->dev = dev;
823
824         skb->priority = sk->sk_priority;
825         skb->protocol = __constant_htons(ETH_P_PPP_SES);
826
827         ph = (struct pppoe_hdr *) skb_put(skb, total_len + sizeof(struct pppoe_hdr));
828         start = (char *) &ph->tag[0];
829
830         error = memcpy_fromiovec(start, m->msg_iov, total_len);
831
832         if (error < 0) {
833                 kfree_skb(skb);
834                 goto end;
835         }
836
837         error = total_len;
838         dev->hard_header(skb, dev, ETH_P_PPP_SES,
839                          po->pppoe_pa.remote, NULL, total_len);
840
841         memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
842
843         ph->length = htons(total_len);
844
845         dev_queue_xmit(skb);
846
847 end:
848         release_sock(sk);
849         return error;
850 }
851
852
853 /************************************************************************
854  *
855  * xmit function for internal use.
856  *
857  ***********************************************************************/
858 static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
859 {
860         struct pppox_opt *po = pppox_sk(sk);
861         struct net_device *dev = po->pppoe_dev;
862         struct pppoe_hdr hdr;
863         struct pppoe_hdr *ph;
864         int headroom = skb_headroom(skb);
865         int data_len = skb->len;
866         struct sk_buff *skb2;
867
868         if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
869                 goto abort;
870
871         hdr.ver = 1;
872         hdr.type = 1;
873         hdr.code = 0;
874         hdr.sid = po->num;
875         hdr.length = htons(skb->len);
876
877         if (!dev)
878                 goto abort;
879
880         /* Copy the skb if there is no space for the header. */
881         if (headroom < (sizeof(struct pppoe_hdr) + dev->hard_header_len)) {
882                 skb2 = dev_alloc_skb(32+skb->len +
883                                      sizeof(struct pppoe_hdr) +
884                                      dev->hard_header_len);
885
886                 if (skb2 == NULL)
887                         goto abort;
888
889                 skb_reserve(skb2, dev->hard_header_len + sizeof(struct pppoe_hdr));
890                 memcpy(skb_put(skb2, skb->len), skb->data, skb->len);
891         } else {
892                 /* Make a clone so as to not disturb the original skb,
893                  * give dev_queue_xmit something it can free.
894                  */
895                 skb2 = skb_clone(skb, GFP_ATOMIC);
896         }
897
898         ph = (struct pppoe_hdr *) skb_push(skb2, sizeof(struct pppoe_hdr));
899         memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
900         skb2->protocol = __constant_htons(ETH_P_PPP_SES);
901
902         skb2->nh.raw = skb2->data;
903
904         skb2->dev = dev;
905
906         dev->hard_header(skb2, dev, ETH_P_PPP_SES,
907                          po->pppoe_pa.remote, NULL, data_len);
908
909         /* We're transmitting skb2, and assuming that dev_queue_xmit
910          * will free it.  The generic ppp layer however, is expecting
911          * that we give back 'skb' (not 'skb2') in case of failure,
912          * but free it in case of success.
913          */
914
915         if (dev_queue_xmit(skb2) < 0)
916                 goto abort;
917
918         kfree_skb(skb);
919         return 1;
920
921 abort:
922         return 0;
923 }
924
925
926 /************************************************************************
927  *
928  * xmit function called by generic PPP driver
929  * sends PPP frame over PPPoE socket
930  *
931  ***********************************************************************/
932 static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
933 {
934         struct sock *sk = (struct sock *) chan->private;
935         return __pppoe_xmit(sk, skb);
936 }
937
938
939 static struct ppp_channel_ops pppoe_chan_ops = { 
940         .start_xmit = pppoe_xmit, 
941 };
942
943 static int pppoe_recvmsg(struct kiocb *iocb, struct socket *sock,
944                   struct msghdr *m, size_t total_len, int flags)
945 {
946         struct sock *sk = sock->sk;
947         struct sk_buff *skb = NULL;
948         int error = 0;
949         int len;
950         struct pppoe_hdr *ph = NULL;
951
952         if (sk->sk_state & PPPOX_BOUND) {
953                 error = -EIO;
954                 goto end;
955         }
956
957         skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
958                                 flags & MSG_DONTWAIT, &error);
959
960         if (error < 0) {
961                 goto end;
962         }
963
964         m->msg_namelen = 0;
965
966         if (skb) {
967                 error = 0;
968                 ph = (struct pppoe_hdr *) skb->nh.raw;
969                 len = ntohs(ph->length);
970
971                 error = memcpy_toiovec(m->msg_iov, (unsigned char *) &ph->tag[0], len);
972                 if (error < 0)
973                         goto do_skb_free;
974                 error = len;
975         }
976
977 do_skb_free:
978         if (skb)
979                 kfree_skb(skb);
980 end:
981         return error;
982 }
983
984 #ifdef CONFIG_PROC_FS
985 static int pppoe_seq_show(struct seq_file *seq, void *v)
986 {
987         struct pppox_opt *po;
988         char *dev_name;
989
990         if (v == SEQ_START_TOKEN) {
991                 seq_puts(seq, "Id       Address              Device\n");
992                 goto out;
993         }
994
995         po = v;
996         dev_name = po->pppoe_pa.dev;
997
998         seq_printf(seq, "%08X %02X:%02X:%02X:%02X:%02X:%02X %8s\n",
999                    po->pppoe_pa.sid,
1000                    po->pppoe_pa.remote[0], po->pppoe_pa.remote[1],
1001                    po->pppoe_pa.remote[2], po->pppoe_pa.remote[3],
1002                    po->pppoe_pa.remote[4], po->pppoe_pa.remote[5], dev_name);
1003 out:
1004         return 0;
1005 }
1006
1007 static __inline__ struct pppox_opt *pppoe_get_idx(loff_t pos)
1008 {
1009         struct pppox_opt *po = NULL;
1010         int i = 0;
1011
1012         for (; i < PPPOE_HASH_SIZE; i++) {
1013                 po = item_hash_table[i];
1014                 while (po) {
1015                         if (!pos--)
1016                                 goto out;
1017                         po = po->next;
1018                 }
1019         }
1020 out:
1021         return po;
1022 }
1023
1024 static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos)
1025 {
1026         loff_t l = *pos;
1027
1028         read_lock_bh(&pppoe_hash_lock);
1029         return l ? pppoe_get_idx(--l) : SEQ_START_TOKEN;
1030 }
1031
1032 static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1033 {
1034         struct pppox_opt *po;
1035
1036         ++*pos;
1037         if (v == SEQ_START_TOKEN) {
1038                 po = pppoe_get_idx(0);
1039                 goto out;
1040         }
1041         po = v;
1042         if (po->next) 
1043                 po = po->next;
1044         else {
1045                 int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
1046
1047                 while (++hash < PPPOE_HASH_SIZE) {
1048                         po = item_hash_table[hash];
1049                         if (po)
1050                                 break;
1051                 }
1052         }
1053 out:
1054         return po;
1055 }
1056
1057 static void pppoe_seq_stop(struct seq_file *seq, void *v)
1058 {
1059         read_unlock_bh(&pppoe_hash_lock);
1060 }
1061
1062 struct seq_operations pppoe_seq_ops = {
1063         .start          = pppoe_seq_start,
1064         .next           = pppoe_seq_next,
1065         .stop           = pppoe_seq_stop,
1066         .show           = pppoe_seq_show,
1067 };
1068
1069 static int pppoe_seq_open(struct inode *inode, struct file *file)
1070 {
1071         return seq_open(file, &pppoe_seq_ops);
1072 }
1073
1074 static struct file_operations pppoe_seq_fops = {
1075         .owner          = THIS_MODULE,
1076         .open           = pppoe_seq_open,
1077         .read           = seq_read,
1078         .llseek         = seq_lseek,
1079         .release        = seq_release,
1080 };
1081
1082 static int __init pppoe_proc_init(void)
1083 {
1084         struct proc_dir_entry *p;
1085
1086         p = create_proc_entry("pppoe", S_IRUGO, proc_net);
1087         if (!p)
1088                 return -ENOMEM;
1089
1090         p->proc_fops = &pppoe_seq_fops;
1091         return 0;
1092 }
1093 #else /* CONFIG_PROC_FS */
1094 static inline int pppoe_proc_init(void) { return 0; }
1095 #endif /* CONFIG_PROC_FS */
1096
1097 /* ->ioctl are set at pppox_create */
1098
1099 static struct proto_ops pppoe_ops = {
1100     .family             = AF_PPPOX,
1101     .owner              = THIS_MODULE,
1102     .release            = pppoe_release,
1103     .bind               = sock_no_bind,
1104     .connect            = pppoe_connect,
1105     .socketpair         = sock_no_socketpair,
1106     .accept             = sock_no_accept,
1107     .getname            = pppoe_getname,
1108     .poll               = datagram_poll,
1109     .listen             = sock_no_listen,
1110     .shutdown           = sock_no_shutdown,
1111     .setsockopt         = sock_no_setsockopt,
1112     .getsockopt         = sock_no_getsockopt,
1113     .sendmsg            = pppoe_sendmsg,
1114     .recvmsg            = pppoe_recvmsg,
1115     .mmap               = sock_no_mmap
1116 };
1117
1118 static struct pppox_proto pppoe_proto = {
1119     .create     = pppoe_create,
1120     .ioctl      = pppoe_ioctl,
1121     .owner      = THIS_MODULE,
1122 };
1123
1124
1125 static int __init pppoe_init(void)
1126 {
1127         int err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
1128
1129         if (err)
1130                 goto out;
1131
1132         err = pppoe_proc_init();
1133         if (err) {
1134                 unregister_pppox_proto(PX_PROTO_OE);
1135                 goto out;
1136         }
1137         
1138         dev_add_pack(&pppoes_ptype);
1139         dev_add_pack(&pppoed_ptype);
1140         register_netdevice_notifier(&pppoe_notifier);
1141 out:
1142         return err;
1143 }
1144
1145 static void __exit pppoe_exit(void)
1146 {
1147         unregister_pppox_proto(PX_PROTO_OE);
1148         dev_remove_pack(&pppoes_ptype);
1149         dev_remove_pack(&pppoed_ptype);
1150         unregister_netdevice_notifier(&pppoe_notifier);
1151         remove_proc_entry("pppoe", proc_net);
1152 }
1153
1154 module_init(pppoe_init);
1155 module_exit(pppoe_exit);
1156
1157 MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
1158 MODULE_DESCRIPTION("PPP over Ethernet driver");
1159 MODULE_LICENSE("GPL");
1160 MODULE_ALIAS_NETPROTO(PF_PPPOX);