blob: 0d101a18026a31b250a010bba4be28be45cab441 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/** -*- 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
82static struct ppp_channel_ops pppoe_chan_ops;
83
84static int pppoe_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
85static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb);
86static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb);
87
David S. Miller17ba15f2005-12-27 20:57:40 -080088static const struct proto_ops pppoe_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static DEFINE_RWLOCK(pppoe_hash_lock);
90
91static struct ppp_channel_ops pppoe_chan_ops;
92
93static 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
99static 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
105static 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 */
123static struct pppox_sock *item_hash_table[PPPOE_HASH_SIZE];
124
125/**********************************************************************
126 *
127 * Set/get/delete/rehash items (internal versions)
128 *
129 **********************************************************************/
130static 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
143static 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
164static 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 **********************************************************************/
190static 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
204static 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
209static 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
223static 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
243static 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
296static 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
322static 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 ***********************************************************************/
332static 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);
Herbert Xucbb042f2006-03-20 22:43:56 -0800340 skb_pull_rcsum(skb, sizeof(struct pppoe_hdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (pskb_trim_rcsum(skb, len))
342 goto abort_kfree;
343
344 ppp_input(&po->chan, skb);
345 } else if (sk->sk_state & PPPOX_RELAY) {
346 relay_po = get_item_by_addr(&po->pppoe_relay);
347
348 if (relay_po == NULL)
349 goto abort_kfree;
350
351 if ((sk_pppox(relay_po)->sk_state & PPPOX_CONNECTED) == 0)
352 goto abort_put;
353
354 skb_pull(skb, sizeof(struct pppoe_hdr));
355 if (!__pppoe_xmit(sk_pppox(relay_po), skb))
356 goto abort_put;
357 } else {
358 if (sock_queue_rcv_skb(sk, skb))
359 goto abort_kfree;
360 }
361
362 return NET_RX_SUCCESS;
363
364abort_put:
365 sock_put(sk_pppox(relay_po));
366
367abort_kfree:
368 kfree_skb(skb);
369 return NET_RX_DROP;
370}
371
372/************************************************************************
373 *
374 * Receive wrapper called in BH context.
375 *
376 ***********************************************************************/
377static int pppoe_rcv(struct sk_buff *skb,
378 struct net_device *dev,
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700379 struct packet_type *pt,
380 struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382{
383 struct pppoe_hdr *ph;
384 struct pppox_sock *po;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
387 goto drop;
388
389 if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
390 goto out;
391
392 ph = (struct pppoe_hdr *) skb->nh.raw;
393
394 po = get_item((unsigned long) ph->sid, eth_hdr(skb)->h_source);
Arnaldo Carvalho de Melo25995ff2005-12-27 02:42:22 -0200395 if (po != NULL)
396 return sk_receive_skb(sk_pppox(po), skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397drop:
398 kfree_skb(skb);
399out:
400 return NET_RX_DROP;
401}
402
403/************************************************************************
404 *
405 * Receive a PPPoE Discovery frame.
406 * This is solely for detection of PADT frames
407 *
408 ***********************************************************************/
409static int pppoe_disc_rcv(struct sk_buff *skb,
410 struct net_device *dev,
David S. Millerf2ccd8f2005-08-09 19:34:12 -0700411 struct packet_type *pt,
412 struct net_device *orig_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414{
415 struct pppoe_hdr *ph;
416 struct pppox_sock *po;
417
418 if (!pskb_may_pull(skb, sizeof(struct pppoe_hdr)))
419 goto abort;
420
421 if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
422 goto out;
423
424 ph = (struct pppoe_hdr *) skb->nh.raw;
425 if (ph->code != PADT_CODE)
426 goto abort;
427
428 po = get_item((unsigned long) ph->sid, eth_hdr(skb)->h_source);
429 if (po) {
430 struct sock *sk = sk_pppox(po);
431
432 bh_lock_sock(sk);
433
434 /* If the user has locked the socket, just ignore
435 * the packet. With the way two rcv protocols hook into
436 * one socket family type, we cannot (easily) distinguish
437 * what kind of SKB it is during backlog rcv.
438 */
439 if (sock_owned_by_user(sk) == 0) {
440 /* We're no longer connect at the PPPOE layer,
441 * and must wait for ppp channel to disconnect us.
442 */
443 sk->sk_state = PPPOX_ZOMBIE;
444 }
445
446 bh_unlock_sock(sk);
447 sock_put(sk);
448 }
449
450abort:
451 kfree_skb(skb);
452out:
453 return NET_RX_SUCCESS; /* Lies... :-) */
454}
455
456static struct packet_type pppoes_ptype = {
457 .type = __constant_htons(ETH_P_PPP_SES),
458 .func = pppoe_rcv,
459};
460
461static struct packet_type pppoed_ptype = {
462 .type = __constant_htons(ETH_P_PPP_DISC),
463 .func = pppoe_disc_rcv,
464};
465
466static struct proto pppoe_sk_proto = {
467 .name = "PPPOE",
468 .owner = THIS_MODULE,
469 .obj_size = sizeof(struct pppox_sock),
470};
471
472/***********************************************************************
473 *
474 * Initialize a new struct sock.
475 *
476 **********************************************************************/
477static int pppoe_create(struct socket *sock)
478{
479 int error = -ENOMEM;
480 struct sock *sk;
481
482 sk = sk_alloc(PF_PPPOX, GFP_KERNEL, &pppoe_sk_proto, 1);
483 if (!sk)
484 goto out;
485
486 sock_init_data(sock, sk);
487
488 sock->state = SS_UNCONNECTED;
489 sock->ops = &pppoe_ops;
490
491 sk->sk_backlog_rcv = pppoe_rcv_core;
492 sk->sk_state = PPPOX_NONE;
493 sk->sk_type = SOCK_STREAM;
494 sk->sk_family = PF_PPPOX;
495 sk->sk_protocol = PX_PROTO_OE;
496
497 error = 0;
498out: return error;
499}
500
501static int pppoe_release(struct socket *sock)
502{
503 struct sock *sk = sock->sk;
504 struct pppox_sock *po;
505 int error = 0;
506
507 if (!sk)
508 return 0;
509
510 if (sock_flag(sk, SOCK_DEAD))
511 return -EBADF;
512
513 pppox_unbind_sock(sk);
514
515 /* Signal the death of the socket. */
516 sk->sk_state = PPPOX_DEAD;
517
518 po = pppox_sk(sk);
519 if (po->pppoe_pa.sid) {
520 delete_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
521 }
522
523 if (po->pppoe_dev)
524 dev_put(po->pppoe_dev);
525
526 po->pppoe_dev = NULL;
527
528 sock_orphan(sk);
529 sock->sk = NULL;
530
531 skb_queue_purge(&sk->sk_receive_queue);
532 sock_put(sk);
533
534 return error;
535}
536
537
538static int pppoe_connect(struct socket *sock, struct sockaddr *uservaddr,
539 int sockaddr_len, int flags)
540{
541 struct sock *sk = sock->sk;
542 struct net_device *dev = NULL;
543 struct sockaddr_pppox *sp = (struct sockaddr_pppox *) uservaddr;
544 struct pppox_sock *po = pppox_sk(sk);
545 int error;
546
547 lock_sock(sk);
548
549 error = -EINVAL;
550 if (sp->sa_protocol != PX_PROTO_OE)
551 goto end;
552
553 /* Check for already bound sockets */
554 error = -EBUSY;
555 if ((sk->sk_state & PPPOX_CONNECTED) && sp->sa_addr.pppoe.sid)
556 goto end;
557
558 /* Check for already disconnected sockets, on attempts to disconnect */
559 error = -EALREADY;
560 if ((sk->sk_state & PPPOX_DEAD) && !sp->sa_addr.pppoe.sid )
561 goto end;
562
563 error = 0;
564 if (po->pppoe_pa.sid) {
565 pppox_unbind_sock(sk);
566
567 /* Delete the old binding */
568 delete_item(po->pppoe_pa.sid,po->pppoe_pa.remote);
569
570 if(po->pppoe_dev)
571 dev_put(po->pppoe_dev);
572
573 memset(sk_pppox(po) + 1, 0,
574 sizeof(struct pppox_sock) - sizeof(struct sock));
575
576 sk->sk_state = PPPOX_NONE;
577 }
578
579 /* Don't re-bind if sid==0 */
580 if (sp->sa_addr.pppoe.sid != 0) {
581 dev = dev_get_by_name(sp->sa_addr.pppoe.dev);
582
583 error = -ENODEV;
584 if (!dev)
585 goto end;
586
587 po->pppoe_dev = dev;
588
589 if (!(dev->flags & IFF_UP))
590 goto err_put;
591
592 memcpy(&po->pppoe_pa,
593 &sp->sa_addr.pppoe,
594 sizeof(struct pppoe_addr));
595
596 error = set_item(po);
597 if (error < 0)
598 goto err_put;
599
600 po->chan.hdrlen = (sizeof(struct pppoe_hdr) +
601 dev->hard_header_len);
602
603 po->chan.private = sk;
604 po->chan.ops = &pppoe_chan_ops;
605
606 error = ppp_register_channel(&po->chan);
607 if (error)
608 goto err_put;
609
610 sk->sk_state = PPPOX_CONNECTED;
611 }
612
613 po->num = sp->sa_addr.pppoe.sid;
614
615 end:
616 release_sock(sk);
617 return error;
618err_put:
619 if (po->pppoe_dev) {
620 dev_put(po->pppoe_dev);
621 po->pppoe_dev = NULL;
622 }
623 goto end;
624}
625
626
627static int pppoe_getname(struct socket *sock, struct sockaddr *uaddr,
628 int *usockaddr_len, int peer)
629{
630 int len = sizeof(struct sockaddr_pppox);
631 struct sockaddr_pppox sp;
632
633 sp.sa_family = AF_PPPOX;
634 sp.sa_protocol = PX_PROTO_OE;
635 memcpy(&sp.sa_addr.pppoe, &pppox_sk(sock->sk)->pppoe_pa,
636 sizeof(struct pppoe_addr));
637
638 memcpy(uaddr, &sp, len);
639
640 *usockaddr_len = len;
641
642 return 0;
643}
644
645
646static int pppoe_ioctl(struct socket *sock, unsigned int cmd,
647 unsigned long arg)
648{
649 struct sock *sk = sock->sk;
650 struct pppox_sock *po = pppox_sk(sk);
651 int val = 0;
652 int err = 0;
653
654 switch (cmd) {
655 case PPPIOCGMRU:
656 err = -ENXIO;
657
658 if (!(sk->sk_state & PPPOX_CONNECTED))
659 break;
660
661 err = -EFAULT;
662 if (put_user(po->pppoe_dev->mtu -
663 sizeof(struct pppoe_hdr) -
664 PPP_HDRLEN,
665 (int __user *) arg))
666 break;
667 err = 0;
668 break;
669
670 case PPPIOCSMRU:
671 err = -ENXIO;
672 if (!(sk->sk_state & PPPOX_CONNECTED))
673 break;
674
675 err = -EFAULT;
676 if (get_user(val,(int __user *) arg))
677 break;
678
679 if (val < (po->pppoe_dev->mtu
680 - sizeof(struct pppoe_hdr)
681 - PPP_HDRLEN))
682 err = 0;
683 else
684 err = -EINVAL;
685 break;
686
687 case PPPIOCSFLAGS:
688 err = -EFAULT;
689 if (get_user(val, (int __user *) arg))
690 break;
691 err = 0;
692 break;
693
694 case PPPOEIOCSFWD:
695 {
696 struct pppox_sock *relay_po;
697
698 err = -EBUSY;
699 if (sk->sk_state & (PPPOX_BOUND | PPPOX_ZOMBIE | PPPOX_DEAD))
700 break;
701
702 err = -ENOTCONN;
703 if (!(sk->sk_state & PPPOX_CONNECTED))
704 break;
705
706 /* PPPoE address from the user specifies an outbound
707 PPPoE address to which frames are forwarded to */
708 err = -EFAULT;
709 if (copy_from_user(&po->pppoe_relay,
710 (void __user *)arg,
711 sizeof(struct sockaddr_pppox)))
712 break;
713
714 err = -EINVAL;
715 if (po->pppoe_relay.sa_family != AF_PPPOX ||
716 po->pppoe_relay.sa_protocol!= PX_PROTO_OE)
717 break;
718
719 /* Check that the socket referenced by the address
720 actually exists. */
721 relay_po = get_item_by_addr(&po->pppoe_relay);
722
723 if (!relay_po)
724 break;
725
726 sock_put(sk_pppox(relay_po));
727 sk->sk_state |= PPPOX_RELAY;
728 err = 0;
729 break;
730 }
731
732 case PPPOEIOCDFWD:
733 err = -EALREADY;
734 if (!(sk->sk_state & PPPOX_RELAY))
735 break;
736
737 sk->sk_state &= ~PPPOX_RELAY;
738 err = 0;
739 break;
740
741 default:;
742 };
743
744 return err;
745}
746
747
748static int pppoe_sendmsg(struct kiocb *iocb, struct socket *sock,
749 struct msghdr *m, size_t total_len)
750{
751 struct sk_buff *skb = NULL;
752 struct sock *sk = sock->sk;
753 struct pppox_sock *po = pppox_sk(sk);
754 int error = 0;
755 struct pppoe_hdr hdr;
756 struct pppoe_hdr *ph;
757 struct net_device *dev;
758 char *start;
759
760 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED)) {
761 error = -ENOTCONN;
762 goto end;
763 }
764
765 hdr.ver = 1;
766 hdr.type = 1;
767 hdr.code = 0;
768 hdr.sid = po->num;
769
770 lock_sock(sk);
771
772 dev = po->pppoe_dev;
773
774 error = -EMSGSIZE;
775 if (total_len > (dev->mtu + dev->hard_header_len))
776 goto end;
777
778
779 skb = sock_wmalloc(sk, total_len + dev->hard_header_len + 32,
780 0, GFP_KERNEL);
781 if (!skb) {
782 error = -ENOMEM;
783 goto end;
784 }
785
786 /* Reserve space for headers. */
787 skb_reserve(skb, dev->hard_header_len);
788 skb->nh.raw = skb->data;
789
790 skb->dev = dev;
791
792 skb->priority = sk->sk_priority;
793 skb->protocol = __constant_htons(ETH_P_PPP_SES);
794
795 ph = (struct pppoe_hdr *) skb_put(skb, total_len + sizeof(struct pppoe_hdr));
796 start = (char *) &ph->tag[0];
797
798 error = memcpy_fromiovec(start, m->msg_iov, total_len);
799
800 if (error < 0) {
801 kfree_skb(skb);
802 goto end;
803 }
804
805 error = total_len;
806 dev->hard_header(skb, dev, ETH_P_PPP_SES,
807 po->pppoe_pa.remote, NULL, total_len);
808
809 memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
810
811 ph->length = htons(total_len);
812
813 dev_queue_xmit(skb);
814
815end:
816 release_sock(sk);
817 return error;
818}
819
820
821/************************************************************************
822 *
823 * xmit function for internal use.
824 *
825 ***********************************************************************/
826static int __pppoe_xmit(struct sock *sk, struct sk_buff *skb)
827{
828 struct pppox_sock *po = pppox_sk(sk);
829 struct net_device *dev = po->pppoe_dev;
830 struct pppoe_hdr hdr;
831 struct pppoe_hdr *ph;
832 int headroom = skb_headroom(skb);
833 int data_len = skb->len;
834 struct sk_buff *skb2;
835
836 if (sock_flag(sk, SOCK_DEAD) || !(sk->sk_state & PPPOX_CONNECTED))
837 goto abort;
838
839 hdr.ver = 1;
840 hdr.type = 1;
841 hdr.code = 0;
842 hdr.sid = po->num;
843 hdr.length = htons(skb->len);
844
845 if (!dev)
846 goto abort;
847
848 /* Copy the skb if there is no space for the header. */
849 if (headroom < (sizeof(struct pppoe_hdr) + dev->hard_header_len)) {
850 skb2 = dev_alloc_skb(32+skb->len +
851 sizeof(struct pppoe_hdr) +
852 dev->hard_header_len);
853
854 if (skb2 == NULL)
855 goto abort;
856
857 skb_reserve(skb2, dev->hard_header_len + sizeof(struct pppoe_hdr));
858 memcpy(skb_put(skb2, skb->len), skb->data, skb->len);
859 } else {
860 /* Make a clone so as to not disturb the original skb,
861 * give dev_queue_xmit something it can free.
862 */
863 skb2 = skb_clone(skb, GFP_ATOMIC);
Florin Malita9bc18092006-06-05 15:34:33 -0700864
865 if (skb2 == NULL)
866 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868
869 ph = (struct pppoe_hdr *) skb_push(skb2, sizeof(struct pppoe_hdr));
870 memcpy(ph, &hdr, sizeof(struct pppoe_hdr));
871 skb2->protocol = __constant_htons(ETH_P_PPP_SES);
872
873 skb2->nh.raw = skb2->data;
874
875 skb2->dev = dev;
876
877 dev->hard_header(skb2, dev, ETH_P_PPP_SES,
878 po->pppoe_pa.remote, NULL, data_len);
879
880 /* We're transmitting skb2, and assuming that dev_queue_xmit
881 * will free it. The generic ppp layer however, is expecting
882 * that we give back 'skb' (not 'skb2') in case of failure,
883 * but free it in case of success.
884 */
885
886 if (dev_queue_xmit(skb2) < 0)
887 goto abort;
888
889 kfree_skb(skb);
890 return 1;
891
892abort:
893 return 0;
894}
895
896
897/************************************************************************
898 *
899 * xmit function called by generic PPP driver
900 * sends PPP frame over PPPoE socket
901 *
902 ***********************************************************************/
903static int pppoe_xmit(struct ppp_channel *chan, struct sk_buff *skb)
904{
905 struct sock *sk = (struct sock *) chan->private;
906 return __pppoe_xmit(sk, skb);
907}
908
909
910static struct ppp_channel_ops pppoe_chan_ops = {
911 .start_xmit = pppoe_xmit,
912};
913
914static int pppoe_recvmsg(struct kiocb *iocb, struct socket *sock,
915 struct msghdr *m, size_t total_len, int flags)
916{
917 struct sock *sk = sock->sk;
918 struct sk_buff *skb = NULL;
919 int error = 0;
920 int len;
921 struct pppoe_hdr *ph = NULL;
922
923 if (sk->sk_state & PPPOX_BOUND) {
924 error = -EIO;
925 goto end;
926 }
927
928 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
929 flags & MSG_DONTWAIT, &error);
930
931 if (error < 0) {
932 goto end;
933 }
934
935 m->msg_namelen = 0;
936
937 if (skb) {
938 error = 0;
939 ph = (struct pppoe_hdr *) skb->nh.raw;
940 len = ntohs(ph->length);
941
942 error = memcpy_toiovec(m->msg_iov, (unsigned char *) &ph->tag[0], len);
943 if (error < 0)
944 goto do_skb_free;
945 error = len;
946 }
947
948do_skb_free:
949 if (skb)
950 kfree_skb(skb);
951end:
952 return error;
953}
954
955#ifdef CONFIG_PROC_FS
956static int pppoe_seq_show(struct seq_file *seq, void *v)
957{
958 struct pppox_sock *po;
959 char *dev_name;
960
961 if (v == SEQ_START_TOKEN) {
962 seq_puts(seq, "Id Address Device\n");
963 goto out;
964 }
965
966 po = v;
967 dev_name = po->pppoe_pa.dev;
968
969 seq_printf(seq, "%08X %02X:%02X:%02X:%02X:%02X:%02X %8s\n",
970 po->pppoe_pa.sid,
971 po->pppoe_pa.remote[0], po->pppoe_pa.remote[1],
972 po->pppoe_pa.remote[2], po->pppoe_pa.remote[3],
973 po->pppoe_pa.remote[4], po->pppoe_pa.remote[5], dev_name);
974out:
975 return 0;
976}
977
978static __inline__ struct pppox_sock *pppoe_get_idx(loff_t pos)
979{
980 struct pppox_sock *po = NULL;
981 int i = 0;
982
983 for (; i < PPPOE_HASH_SIZE; i++) {
984 po = item_hash_table[i];
985 while (po) {
986 if (!pos--)
987 goto out;
988 po = po->next;
989 }
990 }
991out:
992 return po;
993}
994
995static void *pppoe_seq_start(struct seq_file *seq, loff_t *pos)
996{
997 loff_t l = *pos;
998
999 read_lock_bh(&pppoe_hash_lock);
1000 return l ? pppoe_get_idx(--l) : SEQ_START_TOKEN;
1001}
1002
1003static void *pppoe_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1004{
1005 struct pppox_sock *po;
1006
1007 ++*pos;
1008 if (v == SEQ_START_TOKEN) {
1009 po = pppoe_get_idx(0);
1010 goto out;
1011 }
1012 po = v;
1013 if (po->next)
1014 po = po->next;
1015 else {
1016 int hash = hash_item(po->pppoe_pa.sid, po->pppoe_pa.remote);
1017
1018 while (++hash < PPPOE_HASH_SIZE) {
1019 po = item_hash_table[hash];
1020 if (po)
1021 break;
1022 }
1023 }
1024out:
1025 return po;
1026}
1027
1028static void pppoe_seq_stop(struct seq_file *seq, void *v)
1029{
1030 read_unlock_bh(&pppoe_hash_lock);
1031}
1032
1033static struct seq_operations pppoe_seq_ops = {
1034 .start = pppoe_seq_start,
1035 .next = pppoe_seq_next,
1036 .stop = pppoe_seq_stop,
1037 .show = pppoe_seq_show,
1038};
1039
1040static int pppoe_seq_open(struct inode *inode, struct file *file)
1041{
1042 return seq_open(file, &pppoe_seq_ops);
1043}
1044
1045static struct file_operations pppoe_seq_fops = {
1046 .owner = THIS_MODULE,
1047 .open = pppoe_seq_open,
1048 .read = seq_read,
1049 .llseek = seq_lseek,
1050 .release = seq_release,
1051};
1052
1053static int __init pppoe_proc_init(void)
1054{
1055 struct proc_dir_entry *p;
1056
Al Viro66600222005-09-28 22:32:57 +01001057 p = create_proc_entry("net/pppoe", S_IRUGO, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (!p)
1059 return -ENOMEM;
1060
1061 p->proc_fops = &pppoe_seq_fops;
1062 return 0;
1063}
1064#else /* CONFIG_PROC_FS */
1065static inline int pppoe_proc_init(void) { return 0; }
1066#endif /* CONFIG_PROC_FS */
1067
David S. Miller17ba15f2005-12-27 20:57:40 -08001068static const struct proto_ops pppoe_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 .family = AF_PPPOX,
1070 .owner = THIS_MODULE,
1071 .release = pppoe_release,
1072 .bind = sock_no_bind,
1073 .connect = pppoe_connect,
1074 .socketpair = sock_no_socketpair,
1075 .accept = sock_no_accept,
1076 .getname = pppoe_getname,
1077 .poll = datagram_poll,
1078 .listen = sock_no_listen,
1079 .shutdown = sock_no_shutdown,
1080 .setsockopt = sock_no_setsockopt,
1081 .getsockopt = sock_no_getsockopt,
1082 .sendmsg = pppoe_sendmsg,
1083 .recvmsg = pppoe_recvmsg,
David S. Miller17ba15f2005-12-27 20:57:40 -08001084 .mmap = sock_no_mmap,
1085 .ioctl = pppox_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086};
1087
1088static struct pppox_proto pppoe_proto = {
1089 .create = pppoe_create,
1090 .ioctl = pppoe_ioctl,
1091 .owner = THIS_MODULE,
1092};
1093
1094
1095static int __init pppoe_init(void)
1096{
1097 int err = proto_register(&pppoe_sk_proto, 0);
1098
1099 if (err)
1100 goto out;
1101
1102 err = register_pppox_proto(PX_PROTO_OE, &pppoe_proto);
1103 if (err)
1104 goto out_unregister_pppoe_proto;
1105
1106 err = pppoe_proc_init();
1107 if (err)
1108 goto out_unregister_pppox_proto;
1109
1110 dev_add_pack(&pppoes_ptype);
1111 dev_add_pack(&pppoed_ptype);
1112 register_netdevice_notifier(&pppoe_notifier);
1113out:
1114 return err;
1115out_unregister_pppox_proto:
1116 unregister_pppox_proto(PX_PROTO_OE);
1117out_unregister_pppoe_proto:
1118 proto_unregister(&pppoe_sk_proto);
1119 goto out;
1120}
1121
1122static void __exit pppoe_exit(void)
1123{
1124 unregister_pppox_proto(PX_PROTO_OE);
1125 dev_remove_pack(&pppoes_ptype);
1126 dev_remove_pack(&pppoed_ptype);
1127 unregister_netdevice_notifier(&pppoe_notifier);
Al Viro66600222005-09-28 22:32:57 +01001128 remove_proc_entry("net/pppoe", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 proto_unregister(&pppoe_sk_proto);
1130}
1131
1132module_init(pppoe_init);
1133module_exit(pppoe_exit);
1134
1135MODULE_AUTHOR("Michal Ostrowski <mostrows@speakeasy.net>");
1136MODULE_DESCRIPTION("PPP over Ethernet driver");
1137MODULE_LICENSE("GPL");
1138MODULE_ALIAS_NETPROTO(PF_PPPOX);