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