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