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