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