OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [net/] [pppoe.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.