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