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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * INET         An implementation of the TCP/IP protocol suite for the LINUX
3
 *              operating system.  INET is implemented using the  BSD Socket
4
 *              interface as the means of communication with the user level.
5
 *
6
 *              PACKET - implements raw packet sockets.
7
 *
8
 * Version:     $Id: af_packet.c,v 1.1.1.1 2004-04-15 01:16:52 phoenix Exp $
9
 *
10
 * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
11
 *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12
 *              Alan Cox, <gw4pts@gw4pts.ampr.org>
13
 *
14
 * Fixes:
15
 *              Alan Cox        :       verify_area() now used correctly
16
 *              Alan Cox        :       new skbuff lists, look ma no backlogs!
17
 *              Alan Cox        :       tidied skbuff lists.
18
 *              Alan Cox        :       Now uses generic datagram routines I
19
 *                                      added. Also fixed the peek/read crash
20
 *                                      from all old Linux datagram code.
21
 *              Alan Cox        :       Uses the improved datagram code.
22
 *              Alan Cox        :       Added NULL's for socket options.
23
 *              Alan Cox        :       Re-commented the code.
24
 *              Alan Cox        :       Use new kernel side addressing
25
 *              Rob Janssen     :       Correct MTU usage.
26
 *              Dave Platt      :       Counter leaks caused by incorrect
27
 *                                      interrupt locking and some slightly
28
 *                                      dubious gcc output. Can you read
29
 *                                      compiler: it said _VOLATILE_
30
 *      Richard Kooijman        :       Timestamp fixes.
31
 *              Alan Cox        :       New buffers. Use sk->mac.raw.
32
 *              Alan Cox        :       sendmsg/recvmsg support.
33
 *              Alan Cox        :       Protocol setting support
34
 *      Alexey Kuznetsov        :       Untied from IPv4 stack.
35
 *      Cyrus Durgin            :       Fixed kerneld for kmod.
36
 *      Michal Ostrowski        :       Module initialization cleanup.
37
 *         Ulises Alonso        :       Frame number limit removal and
38
 *                                      packet_set_ring memory leak.
39
 *
40
 *              This program is free software; you can redistribute it and/or
41
 *              modify it under the terms of the GNU General Public License
42
 *              as published by the Free Software Foundation; either version
43
 *              2 of the License, or (at your option) any later version.
44
 *
45
 */
46
 
47
#include <linux/config.h>
48
#include <linux/types.h>
49
#include <linux/sched.h>
50
#include <linux/mm.h>
51
#include <linux/fcntl.h>
52
#include <linux/socket.h>
53
#include <linux/in.h>
54
#include <linux/inet.h>
55
#include <linux/netdevice.h>
56
#include <linux/if_packet.h>
57
#include <linux/wireless.h>
58
#include <linux/kmod.h>
59
#include <net/ip.h>
60
#include <net/protocol.h>
61
#include <linux/skbuff.h>
62
#include <net/sock.h>
63
#include <linux/errno.h>
64
#include <linux/timer.h>
65
#include <asm/system.h>
66
#include <asm/uaccess.h>
67
#include <asm/ioctls.h>
68
#include <linux/proc_fs.h>
69
#include <linux/poll.h>
70
#include <linux/module.h>
71
#include <linux/init.h>
72
#include <linux/if_bridge.h>
73
 
74
#ifdef CONFIG_NET_DIVERT
75
#include <linux/divert.h>
76
#endif /* CONFIG_NET_DIVERT */
77
 
78
#ifdef CONFIG_INET
79
#include <net/inet_common.h>
80
#endif
81
 
82
#ifdef CONFIG_DLCI
83
extern int dlci_ioctl(unsigned int, void*);
84
#endif
85
 
86
#define CONFIG_SOCK_PACKET      1
87
 
88
/*
89
   Proposed replacement for SIOC{ADD,DEL}MULTI and
90
   IFF_PROMISC, IFF_ALLMULTI flags.
91
 
92
   It is more expensive, but I believe,
93
   it is really correct solution: reentereble, safe and fault tolerant.
94
 
95
   IFF_PROMISC/IFF_ALLMULTI/SIOC{ADD/DEL}MULTI are faked by keeping
96
   reference count and global flag, so that real status is
97
   (gflag|(count != 0)), so that we can use obsolete faulty interface
98
   not harming clever users.
99
 */
100
#define CONFIG_PACKET_MULTICAST 1
101
 
102
/*
103
   Assumptions:
104
   - if device has no dev->hard_header routine, it adds and removes ll header
105
     inside itself. In this case ll header is invisible outside of device,
106
     but higher levels still should reserve dev->hard_header_len.
107
     Some devices are enough clever to reallocate skb, when header
108
     will not fit to reserved space (tunnel), another ones are silly
109
     (PPP).
110
   - packet socket receives packets with pulled ll header,
111
     so that SOCK_RAW should push it back.
112
 
113
On receive:
114
-----------
115
 
116
Incoming, dev->hard_header!=NULL
117
   mac.raw -> ll header
118
   data    -> data
119
 
120
Outgoing, dev->hard_header!=NULL
121
   mac.raw -> ll header
122
   data    -> ll header
123
 
124
Incoming, dev->hard_header==NULL
125
   mac.raw -> UNKNOWN position. It is very likely, that it points to ll header.
126
              PPP makes it, that is wrong, because introduce assymetry
127
              between rx and tx paths.
128
   data    -> data
129
 
130
Outgoing, dev->hard_header==NULL
131
   mac.raw -> data. ll header is still not built!
132
   data    -> data
133
 
134
Resume
135
  If dev->hard_header==NULL we are unlikely to restore sensible ll header.
136
 
137
 
138
On transmit:
139
------------
140
 
141
dev->hard_header != NULL
142
   mac.raw -> ll header
143
   data    -> ll header
144
 
145
dev->hard_header == NULL (ll header is added by device, we cannot control it)
146
   mac.raw -> data
147
   data -> data
148
 
149
   We should set nh.raw on output to correct posistion,
150
   packet classifier depends on it.
151
 */
152
 
153
/* List of all packet sockets. */
154
static struct sock * packet_sklist;
155
static rwlock_t packet_sklist_lock = RW_LOCK_UNLOCKED;
156
 
157
atomic_t packet_socks_nr;
158
 
159
 
160
/* Private packet socket structures. */
161
 
162
#ifdef CONFIG_PACKET_MULTICAST
163
struct packet_mclist
164
{
165
        struct packet_mclist    *next;
166
        int                     ifindex;
167
        int                     count;
168
        unsigned short          type;
169
        unsigned short          alen;
170
        unsigned char           addr[8];
171
};
172
#endif
173
#ifdef CONFIG_PACKET_MMAP
174
static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing);
175
#endif
176
 
177
static void packet_flush_mclist(struct sock *sk);
178
 
179
struct packet_opt
180
{
181
        struct tpacket_stats    stats;
182
#ifdef CONFIG_PACKET_MMAP
183
        unsigned long           *pg_vec;
184
        unsigned int            head;
185
        unsigned int            frames_per_block;
186
        unsigned int            frame_size;
187
        unsigned int            frame_max;
188
        int                     copy_thresh;
189
#endif
190
        struct packet_type      prot_hook;
191
        spinlock_t              bind_lock;
192
        char                    running;        /* prot_hook is attached*/
193
        int                     ifindex;        /* bound device         */
194
#ifdef CONFIG_PACKET_MULTICAST
195
        struct packet_mclist    *mclist;
196
#endif
197
#ifdef CONFIG_PACKET_MMAP
198
        atomic_t                mapped;
199
        unsigned int            pg_vec_order;
200
        unsigned int            pg_vec_pages;
201
        unsigned int            pg_vec_len;
202
#endif
203
};
204
 
205
#ifdef CONFIG_PACKET_MMAP
206
 
207
static inline unsigned long packet_lookup_frame(struct packet_opt *po, unsigned int position)
208
{
209
        unsigned int pg_vec_pos, frame_offset;
210
        unsigned long frame;
211
 
212
        pg_vec_pos = position / po->frames_per_block;
213
        frame_offset = position % po->frames_per_block;
214
 
215
        frame = (unsigned long) (po->pg_vec[pg_vec_pos] + (frame_offset * po->frame_size));
216
 
217
        return frame;
218
}
219
#endif
220
 
221
void packet_sock_destruct(struct sock *sk)
222
{
223
        BUG_TRAP(atomic_read(&sk->rmem_alloc)==0);
224
        BUG_TRAP(atomic_read(&sk->wmem_alloc)==0);
225
 
226
        if (!sk->dead) {
227
                printk("Attempt to release alive packet socket: %p\n", sk);
228
                return;
229
        }
230
 
231
        if (sk->protinfo.destruct_hook)
232
                kfree(sk->protinfo.destruct_hook);
233
        atomic_dec(&packet_socks_nr);
234
#ifdef PACKET_REFCNT_DEBUG
235
        printk(KERN_DEBUG "PACKET socket %p is free, %d are alive\n", sk, atomic_read(&packet_socks_nr));
236
#endif
237
        MOD_DEC_USE_COUNT;
238
}
239
 
240
 
241
extern struct proto_ops packet_ops;
242
 
243
#ifdef CONFIG_SOCK_PACKET
244
extern struct proto_ops packet_ops_spkt;
245
 
246
static int packet_rcv_spkt(struct sk_buff *skb, struct net_device *dev,  struct packet_type *pt)
247
{
248
        struct sock *sk;
249
        struct sockaddr_pkt *spkt;
250
 
251
        /*
252
         *      When we registered the protocol we saved the socket in the data
253
         *      field for just this event.
254
         */
255
 
256
        sk = (struct sock *) pt->data;
257
 
258
        /*
259
         *      Yank back the headers [hope the device set this
260
         *      right or kerboom...]
261
         *
262
         *      Incoming packets have ll header pulled,
263
         *      push it back.
264
         *
265
         *      For outgoing ones skb->data == skb->mac.raw
266
         *      so that this procedure is noop.
267
         */
268
 
269
        if (skb->pkt_type == PACKET_LOOPBACK)
270
                goto out;
271
 
272
        if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
273
                goto oom;
274
 
275
        spkt = (struct sockaddr_pkt*)skb->cb;
276
 
277
        skb_push(skb, skb->data-skb->mac.raw);
278
 
279
        /*
280
         *      The SOCK_PACKET socket receives _all_ frames.
281
         */
282
 
283
        spkt->spkt_family = dev->type;
284
        strncpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device));
285
        spkt->spkt_protocol = skb->protocol;
286
 
287
        /*
288
         *      Charge the memory to the socket. This is done specifically
289
         *      to prevent sockets using all the memory up.
290
         */
291
 
292
        if (sock_queue_rcv_skb(sk,skb) == 0)
293
                return 0;
294
 
295
out:
296
        kfree_skb(skb);
297
oom:
298
        return 0;
299
}
300
 
301
 
302
/*
303
 *      Output a raw packet to a device layer. This bypasses all the other
304
 *      protocol layers and you must therefore supply it with a complete frame
305
 */
306
 
307
static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg, int len,
308
                               struct scm_cookie *scm)
309
{
310
        struct sock *sk = sock->sk;
311
        struct sockaddr_pkt *saddr=(struct sockaddr_pkt *)msg->msg_name;
312
        struct sk_buff *skb;
313
        struct net_device *dev;
314
        unsigned short proto=0;
315
        int err;
316
 
317
        /*
318
         *      Get and verify the address.
319
         */
320
 
321
        if (saddr)
322
        {
323
                if (msg->msg_namelen < sizeof(struct sockaddr))
324
                        return(-EINVAL);
325
                if (msg->msg_namelen==sizeof(struct sockaddr_pkt))
326
                        proto=saddr->spkt_protocol;
327
        }
328
        else
329
                return(-ENOTCONN);      /* SOCK_PACKET must be sent giving an address */
330
 
331
        /*
332
         *      Find the device first to size check it
333
         */
334
 
335
        saddr->spkt_device[13] = 0;
336
        dev = dev_get_by_name(saddr->spkt_device);
337
        err = -ENODEV;
338
        if (dev == NULL)
339
                goto out_unlock;
340
 
341
        /*
342
         *      You may not queue a frame bigger than the mtu. This is the lowest level
343
         *      raw protocol and you must do your own fragmentation at this level.
344
         */
345
 
346
        err = -EMSGSIZE;
347
        if(len>dev->mtu+dev->hard_header_len)
348
                goto out_unlock;
349
 
350
        err = -ENOBUFS;
351
        skb = sock_wmalloc(sk, len+dev->hard_header_len+15, 0, GFP_KERNEL);
352
 
353
        /*
354
         *      If the write buffer is full, then tough. At this level the user gets to
355
         *      deal with the problem - do your own algorithmic backoffs. That's far
356
         *      more flexible.
357
         */
358
 
359
        if (skb == NULL)
360
                goto out_unlock;
361
 
362
        /*
363
         *      Fill it in
364
         */
365
 
366
        /* FIXME: Save some space for broken drivers that write a
367
         * hard header at transmission time by themselves. PPP is the
368
         * notable one here. This should really be fixed at the driver level.
369
         */
370
        skb_reserve(skb,(dev->hard_header_len+15)&~15);
371
        skb->nh.raw = skb->data;
372
 
373
        /* Try to align data part correctly */
374
        if (dev->hard_header) {
375
                skb->data -= dev->hard_header_len;
376
                skb->tail -= dev->hard_header_len;
377
                if (len < dev->hard_header_len)
378
                        skb->nh.raw = skb->data;
379
        }
380
 
381
        /* Returns -EFAULT on error */
382
        err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
383
        skb->protocol = proto;
384
        skb->dev = dev;
385
        skb->priority = sk->priority;
386
        if (err)
387
                goto out_free;
388
 
389
        err = -ENETDOWN;
390
        if (!(dev->flags & IFF_UP))
391
                goto out_free;
392
 
393
        /*
394
         *      Now send it
395
         */
396
 
397
        dev_queue_xmit(skb);
398
        dev_put(dev);
399
        return(len);
400
 
401
out_free:
402
        kfree_skb(skb);
403
out_unlock:
404
        if (dev)
405
                dev_put(dev);
406
        return err;
407
}
408
#endif
409
 
410
/*
411
   This function makes lazy skb cloning in hope that most of packets
412
   are discarded by BPF.
413
 
414
   Note tricky part: we DO mangle shared skb! skb->data, skb->len
415
   and skb->cb are mangled. It works because (and until) packets
416
   falling here are owned by current CPU. Output packets are cloned
417
   by dev_queue_xmit_nit(), input packets are processed by net_bh
418
   sequencially, so that if we return skb to original state on exit,
419
   we will not harm anyone.
420
 */
421
 
422
static int packet_rcv(struct sk_buff *skb, struct net_device *dev,  struct packet_type *pt)
423
{
424
        struct sock *sk;
425
        struct sockaddr_ll *sll;
426
        struct packet_opt *po;
427
        u8 * skb_head = skb->data;
428
        int skb_len = skb->len;
429
#ifdef CONFIG_FILTER
430
        unsigned snaplen;
431
#endif
432
 
433
        if (skb->pkt_type == PACKET_LOOPBACK)
434
                goto drop;
435
 
436
        sk = (struct sock *) pt->data;
437
        po = sk->protinfo.af_packet;
438
 
439
        skb->dev = dev;
440
 
441
        if (dev->hard_header) {
442
                /* The device has an explicit notion of ll header,
443
                   exported to higher levels.
444
 
445
                   Otherwise, the device hides datails of it frame
446
                   structure, so that corresponding packet head
447
                   never delivered to user.
448
                 */
449
                if (sk->type != SOCK_DGRAM)
450
                        skb_push(skb, skb->data - skb->mac.raw);
451
                else if (skb->pkt_type == PACKET_OUTGOING) {
452
                        /* Special case: outgoing packets have ll header at head */
453
                        skb_pull(skb, skb->nh.raw - skb->data);
454
                }
455
        }
456
 
457
#ifdef CONFIG_FILTER
458
        snaplen = skb->len;
459
 
460
        if (sk->filter) {
461
                unsigned res = snaplen;
462
                struct sk_filter *filter;
463
 
464
                bh_lock_sock(sk);
465
                if ((filter = sk->filter) != NULL)
466
                        res = sk_run_filter(skb, sk->filter->insns, sk->filter->len);
467
                bh_unlock_sock(sk);
468
 
469
                if (res == 0)
470
                        goto drop_n_restore;
471
                if (snaplen > res)
472
                        snaplen = res;
473
        }
474
#endif /* CONFIG_FILTER */
475
 
476
        if (atomic_read(&sk->rmem_alloc) + skb->truesize >= (unsigned)sk->rcvbuf)
477
                goto drop_n_acct;
478
 
479
        if (skb_shared(skb)) {
480
                struct sk_buff *nskb = skb_clone(skb, GFP_ATOMIC);
481
                if (nskb == NULL)
482
                        goto drop_n_acct;
483
 
484
                if (skb_head != skb->data) {
485
                        skb->data = skb_head;
486
                        skb->len = skb_len;
487
                }
488
                kfree_skb(skb);
489
                skb = nskb;
490
        }
491
 
492
        sll = (struct sockaddr_ll*)skb->cb;
493
        sll->sll_family = AF_PACKET;
494
        sll->sll_hatype = dev->type;
495
        sll->sll_protocol = skb->protocol;
496
        sll->sll_pkttype = skb->pkt_type;
497
        sll->sll_ifindex = dev->ifindex;
498
        sll->sll_halen = 0;
499
 
500
        if (dev->hard_header_parse)
501
                sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
502
 
503
#ifdef CONFIG_FILTER
504
        if (pskb_trim(skb, snaplen))
505
                goto drop_n_acct;
506
#endif
507
 
508
        skb_set_owner_r(skb, sk);
509
        skb->dev = NULL;
510
        spin_lock(&sk->receive_queue.lock);
511
        po->stats.tp_packets++;
512
        __skb_queue_tail(&sk->receive_queue, skb);
513
        spin_unlock(&sk->receive_queue.lock);
514
        sk->data_ready(sk,skb->len);
515
        return 0;
516
 
517
drop_n_acct:
518
        spin_lock(&sk->receive_queue.lock);
519
        po->stats.tp_drops++;
520
        spin_unlock(&sk->receive_queue.lock);
521
 
522
#ifdef CONFIG_FILTER
523
drop_n_restore:
524
#endif
525
        if (skb_head != skb->data && skb_shared(skb)) {
526
                skb->data = skb_head;
527
                skb->len = skb_len;
528
        }
529
drop:
530
        kfree_skb(skb);
531
        return 0;
532
}
533
 
534
#ifdef CONFIG_PACKET_MMAP
535
static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,  struct packet_type *pt)
536
{
537
        struct sock *sk;
538
        struct packet_opt *po;
539
        struct sockaddr_ll *sll;
540
        struct tpacket_hdr *h;
541
        u8 * skb_head = skb->data;
542
        int skb_len = skb->len;
543
        unsigned snaplen;
544
        unsigned long status = TP_STATUS_LOSING|TP_STATUS_USER;
545
        unsigned short macoff, netoff;
546
        struct sk_buff *copy_skb = NULL;
547
 
548
        if (skb->pkt_type == PACKET_LOOPBACK)
549
                goto drop;
550
 
551
        sk = (struct sock *) pt->data;
552
        po = sk->protinfo.af_packet;
553
 
554
        if (dev->hard_header) {
555
                if (sk->type != SOCK_DGRAM)
556
                        skb_push(skb, skb->data - skb->mac.raw);
557
                else if (skb->pkt_type == PACKET_OUTGOING) {
558
                        /* Special case: outgoing packets have ll header at head */
559
                        skb_pull(skb, skb->nh.raw - skb->data);
560
                        if (skb->ip_summed == CHECKSUM_HW)
561
                                status |= TP_STATUS_CSUMNOTREADY;
562
                }
563
        }
564
 
565
        snaplen = skb->len;
566
 
567
#ifdef CONFIG_FILTER
568
        if (sk->filter) {
569
                unsigned res = snaplen;
570
                struct sk_filter *filter;
571
 
572
                bh_lock_sock(sk);
573
                if ((filter = sk->filter) != NULL)
574
                        res = sk_run_filter(skb, sk->filter->insns, sk->filter->len);
575
                bh_unlock_sock(sk);
576
 
577
                if (res == 0)
578
                        goto drop_n_restore;
579
                if (snaplen > res)
580
                        snaplen = res;
581
        }
582
#endif
583
 
584
        if (sk->type == SOCK_DGRAM) {
585
                macoff = netoff = TPACKET_ALIGN(TPACKET_HDRLEN) + 16;
586
        } else {
587
                unsigned maclen = skb->nh.raw - skb->data;
588
                netoff = TPACKET_ALIGN(TPACKET_HDRLEN + (maclen < 16 ? 16 : maclen));
589
                macoff = netoff - maclen;
590
        }
591
 
592
        if (macoff + snaplen > po->frame_size) {
593
                if (po->copy_thresh &&
594
                    atomic_read(&sk->rmem_alloc) + skb->truesize < (unsigned)sk->rcvbuf) {
595
                        if (skb_shared(skb)) {
596
                                copy_skb = skb_clone(skb, GFP_ATOMIC);
597
                        } else {
598
                                copy_skb = skb_get(skb);
599
                                skb_head = skb->data;
600
                        }
601
                        if (copy_skb)
602
                                skb_set_owner_r(copy_skb, sk);
603
                }
604
                snaplen = po->frame_size - macoff;
605
                if ((int)snaplen < 0)
606
                        snaplen = 0;
607
        }
608
        if (snaplen > skb->len-skb->data_len)
609
                snaplen = skb->len-skb->data_len;
610
 
611
        spin_lock(&sk->receive_queue.lock);
612
        h = (struct tpacket_hdr *)packet_lookup_frame(po, po->head);
613
 
614
        if (h->tp_status)
615
                goto ring_is_full;
616
        po->head = po->head != po->frame_max ? po->head+1 : 0;
617
        po->stats.tp_packets++;
618
        if (copy_skb) {
619
                status |= TP_STATUS_COPY;
620
                __skb_queue_tail(&sk->receive_queue, copy_skb);
621
        }
622
        if (!po->stats.tp_drops)
623
                status &= ~TP_STATUS_LOSING;
624
        spin_unlock(&sk->receive_queue.lock);
625
 
626
        memcpy((u8*)h + macoff, skb->data, snaplen);
627
 
628
        h->tp_len = skb->len;
629
        h->tp_snaplen = snaplen;
630
        h->tp_mac = macoff;
631
        h->tp_net = netoff;
632
        h->tp_sec = skb->stamp.tv_sec;
633
        h->tp_usec = skb->stamp.tv_usec;
634
 
635
        sll = (struct sockaddr_ll*)((u8*)h + TPACKET_ALIGN(sizeof(*h)));
636
        sll->sll_halen = 0;
637
        if (dev->hard_header_parse)
638
                sll->sll_halen = dev->hard_header_parse(skb, sll->sll_addr);
639
        sll->sll_family = AF_PACKET;
640
        sll->sll_hatype = dev->type;
641
        sll->sll_protocol = skb->protocol;
642
        sll->sll_pkttype = skb->pkt_type;
643
        sll->sll_ifindex = dev->ifindex;
644
 
645
        h->tp_status = status;
646
        mb();
647
 
648
        {
649
                struct page *p_start, *p_end;
650
                u8 *h_end = (u8 *)h + macoff + snaplen - 1;
651
 
652
                p_start = virt_to_page(h);
653
                p_end = virt_to_page(h_end);
654
                while (p_start <= p_end) {
655
                        flush_dcache_page(p_start);
656
                        p_start++;
657
                }
658
        }
659
 
660
        sk->data_ready(sk, 0);
661
 
662
drop_n_restore:
663
        if (skb_head != skb->data && skb_shared(skb)) {
664
                skb->data = skb_head;
665
                skb->len = skb_len;
666
        }
667
drop:
668
        kfree_skb(skb);
669
        return 0;
670
 
671
ring_is_full:
672
        po->stats.tp_drops++;
673
        spin_unlock(&sk->receive_queue.lock);
674
 
675
        sk->data_ready(sk, 0);
676
        if (copy_skb)
677
                kfree_skb(copy_skb);
678
        goto drop_n_restore;
679
}
680
 
681
#endif
682
 
683
 
684
static int packet_sendmsg(struct socket *sock, struct msghdr *msg, int len,
685
                          struct scm_cookie *scm)
686
{
687
        struct sock *sk = sock->sk;
688
        struct sockaddr_ll *saddr=(struct sockaddr_ll *)msg->msg_name;
689
        struct sk_buff *skb;
690
        struct net_device *dev;
691
        unsigned short proto;
692
        unsigned char *addr;
693
        int ifindex, err, reserve = 0;
694
 
695
        /*
696
         *      Get and verify the address.
697
         */
698
 
699
        if (saddr == NULL) {
700
                ifindex = sk->protinfo.af_packet->ifindex;
701
                proto   = sk->num;
702
                addr    = NULL;
703
        } else {
704
                err = -EINVAL;
705
                if (msg->msg_namelen < sizeof(struct sockaddr_ll))
706
                        goto out;
707
                ifindex = saddr->sll_ifindex;
708
                proto   = saddr->sll_protocol;
709
                addr    = saddr->sll_addr;
710
        }
711
 
712
 
713
        dev = dev_get_by_index(ifindex);
714
        err = -ENXIO;
715
        if (dev == NULL)
716
                goto out_unlock;
717
        if (sock->type == SOCK_RAW)
718
                reserve = dev->hard_header_len;
719
 
720
        err = -EMSGSIZE;
721
        if (len > dev->mtu+reserve)
722
                goto out_unlock;
723
 
724
        skb = sock_alloc_send_skb(sk, len+dev->hard_header_len+15,
725
                                msg->msg_flags & MSG_DONTWAIT, &err);
726
        if (skb==NULL)
727
                goto out_unlock;
728
 
729
        skb_reserve(skb, (dev->hard_header_len+15)&~15);
730
        skb->nh.raw = skb->data;
731
 
732
        if (dev->hard_header) {
733
                int res;
734
                err = -EINVAL;
735
                res = dev->hard_header(skb, dev, ntohs(proto), addr, NULL, len);
736
                if (sock->type != SOCK_DGRAM) {
737
                        skb->tail = skb->data;
738
                        skb->len = 0;
739
                } else if (res < 0)
740
                        goto out_free;
741
        }
742
 
743
        /* Returns -EFAULT on error */
744
        err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len);
745
        if (err)
746
                goto out_free;
747
 
748
        skb->protocol = proto;
749
        skb->dev = dev;
750
        skb->priority = sk->priority;
751
 
752
        err = -ENETDOWN;
753
        if (!(dev->flags & IFF_UP))
754
                goto out_free;
755
 
756
        /*
757
         *      Now send it
758
         */
759
 
760
        err = dev_queue_xmit(skb);
761
        if (err > 0 && (err = net_xmit_errno(err)) != 0)
762
                goto out_unlock;
763
 
764
        dev_put(dev);
765
 
766
        return(len);
767
 
768
out_free:
769
        kfree_skb(skb);
770
out_unlock:
771
        if (dev)
772
                dev_put(dev);
773
out:
774
        return err;
775
}
776
 
777
/*
778
 *      Close a PACKET socket. This is fairly simple. We immediately go
779
 *      to 'closed' state and remove our protocol entry in the device list.
780
 */
781
 
782
static int packet_release(struct socket *sock)
783
{
784
        struct sock *sk = sock->sk;
785
        struct sock **skp;
786
 
787
        if (!sk)
788
                return 0;
789
 
790
        write_lock_bh(&packet_sklist_lock);
791
        for (skp = &packet_sklist; *skp; skp = &(*skp)->next) {
792
                if (*skp == sk) {
793
                        *skp = sk->next;
794
                        __sock_put(sk);
795
                        break;
796
                }
797
        }
798
        write_unlock_bh(&packet_sklist_lock);
799
 
800
        /*
801
         *      Unhook packet receive handler.
802
         */
803
 
804
        if (sk->protinfo.af_packet->running) {
805
                /*
806
                 *      Remove the protocol hook
807
                 */
808
                dev_remove_pack(&sk->protinfo.af_packet->prot_hook);
809
                sk->protinfo.af_packet->running = 0;
810
                __sock_put(sk);
811
        }
812
 
813
#ifdef CONFIG_PACKET_MULTICAST
814
        packet_flush_mclist(sk);
815
#endif
816
 
817
#ifdef CONFIG_PACKET_MMAP
818
        if (sk->protinfo.af_packet->pg_vec) {
819
                struct tpacket_req req;
820
                memset(&req, 0, sizeof(req));
821
                packet_set_ring(sk, &req, 1);
822
        }
823
#endif
824
 
825
        /*
826
         *      Now the socket is dead. No more input will appear.
827
         */
828
 
829
        sock_orphan(sk);
830
        sock->sk = NULL;
831
 
832
        /* Purge queues */
833
 
834
        skb_queue_purge(&sk->receive_queue);
835
 
836
        sock_put(sk);
837
        return 0;
838
}
839
 
840
/*
841
 *      Attach a packet hook.
842
 */
843
 
844
static int packet_do_bind(struct sock *sk, struct net_device *dev, int protocol)
845
{
846
        /*
847
         *      Detach an existing hook if present.
848
         */
849
 
850
        lock_sock(sk);
851
 
852
        spin_lock(&sk->protinfo.af_packet->bind_lock);
853
        if (sk->protinfo.af_packet->running) {
854
                dev_remove_pack(&sk->protinfo.af_packet->prot_hook);
855
                __sock_put(sk);
856
                sk->protinfo.af_packet->running = 0;
857
        }
858
 
859
        sk->num = protocol;
860
        sk->protinfo.af_packet->prot_hook.type = protocol;
861
        sk->protinfo.af_packet->prot_hook.dev = dev;
862
 
863
        sk->protinfo.af_packet->ifindex = dev ? dev->ifindex : 0;
864
 
865
        if (protocol == 0)
866
                goto out_unlock;
867
 
868
        if (dev) {
869
                if (dev->flags&IFF_UP) {
870
                        dev_add_pack(&sk->protinfo.af_packet->prot_hook);
871
                        sock_hold(sk);
872
                        sk->protinfo.af_packet->running = 1;
873
                } else {
874
                        sk->err = ENETDOWN;
875
                        if (!sk->dead)
876
                                sk->error_report(sk);
877
                }
878
        } else {
879
                dev_add_pack(&sk->protinfo.af_packet->prot_hook);
880
                sock_hold(sk);
881
                sk->protinfo.af_packet->running = 1;
882
        }
883
 
884
out_unlock:
885
        spin_unlock(&sk->protinfo.af_packet->bind_lock);
886
        release_sock(sk);
887
        return 0;
888
}
889
 
890
/*
891
 *      Bind a packet socket to a device
892
 */
893
 
894
#ifdef CONFIG_SOCK_PACKET
895
 
896
static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr, int addr_len)
897
{
898
        struct sock *sk=sock->sk;
899
        char name[15];
900
        struct net_device *dev;
901
        int err = -ENODEV;
902
 
903
        /*
904
         *      Check legality
905
         */
906
 
907
        if(addr_len!=sizeof(struct sockaddr))
908
                return -EINVAL;
909
        strncpy(name,uaddr->sa_data,14);
910
        name[14]=0;
911
 
912
        dev = dev_get_by_name(name);
913
        if (dev) {
914
                err = packet_do_bind(sk, dev, sk->num);
915
                dev_put(dev);
916
        }
917
        return err;
918
}
919
#endif
920
 
921
static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
922
{
923
        struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
924
        struct sock *sk=sock->sk;
925
        struct net_device *dev = NULL;
926
        int err;
927
 
928
 
929
        /*
930
         *      Check legality
931
         */
932
 
933
        if (addr_len < sizeof(struct sockaddr_ll))
934
                return -EINVAL;
935
        if (sll->sll_family != AF_PACKET)
936
                return -EINVAL;
937
 
938
        if (sll->sll_ifindex) {
939
                err = -ENODEV;
940
                dev = dev_get_by_index(sll->sll_ifindex);
941
                if (dev == NULL)
942
                        goto out;
943
        }
944
        err = packet_do_bind(sk, dev, sll->sll_protocol ? : sk->num);
945
        if (dev)
946
                dev_put(dev);
947
 
948
out:
949
        return err;
950
}
951
 
952
 
953
/*
954
 *      Create a packet of type SOCK_PACKET.
955
 */
956
 
957
static int packet_create(struct socket *sock, int protocol)
958
{
959
        struct sock *sk;
960
        int err;
961
 
962
        if (!capable(CAP_NET_RAW))
963
                return -EPERM;
964
        if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW
965
#ifdef CONFIG_SOCK_PACKET
966
            && sock->type != SOCK_PACKET
967
#endif
968
            )
969
                return -ESOCKTNOSUPPORT;
970
 
971
        sock->state = SS_UNCONNECTED;
972
        MOD_INC_USE_COUNT;
973
 
974
        err = -ENOBUFS;
975
        sk = sk_alloc(PF_PACKET, GFP_KERNEL, 1);
976
        if (sk == NULL)
977
                goto out;
978
 
979
        sock->ops = &packet_ops;
980
#ifdef CONFIG_SOCK_PACKET
981
        if (sock->type == SOCK_PACKET)
982
                sock->ops = &packet_ops_spkt;
983
#endif
984
        sock_init_data(sock,sk);
985
 
986
        sk->protinfo.af_packet = kmalloc(sizeof(struct packet_opt), GFP_KERNEL);
987
        if (sk->protinfo.af_packet == NULL)
988
                goto out_free;
989
        memset(sk->protinfo.af_packet, 0, sizeof(struct packet_opt));
990
        sk->family = PF_PACKET;
991
        sk->num = protocol;
992
 
993
        sk->destruct = packet_sock_destruct;
994
        atomic_inc(&packet_socks_nr);
995
 
996
        /*
997
         *      Attach a protocol block
998
         */
999
 
1000
        spin_lock_init(&sk->protinfo.af_packet->bind_lock);
1001
        sk->protinfo.af_packet->prot_hook.func = packet_rcv;
1002
#ifdef CONFIG_SOCK_PACKET
1003
        if (sock->type == SOCK_PACKET)
1004
                sk->protinfo.af_packet->prot_hook.func = packet_rcv_spkt;
1005
#endif
1006
        sk->protinfo.af_packet->prot_hook.data = (void *)sk;
1007
 
1008
        if (protocol) {
1009
                sk->protinfo.af_packet->prot_hook.type = protocol;
1010
                dev_add_pack(&sk->protinfo.af_packet->prot_hook);
1011
                sock_hold(sk);
1012
                sk->protinfo.af_packet->running = 1;
1013
        }
1014
 
1015
        write_lock_bh(&packet_sklist_lock);
1016
        sk->next = packet_sklist;
1017
        packet_sklist = sk;
1018
        sock_hold(sk);
1019
        write_unlock_bh(&packet_sklist_lock);
1020
        return(0);
1021
 
1022
out_free:
1023
        sk_free(sk);
1024
out:
1025
        MOD_DEC_USE_COUNT;
1026
        return err;
1027
}
1028
 
1029
/*
1030
 *      Pull a packet from our receive queue and hand it to the user.
1031
 *      If necessary we block.
1032
 */
1033
 
1034
static int packet_recvmsg(struct socket *sock, struct msghdr *msg, int len,
1035
                          int flags, struct scm_cookie *scm)
1036
{
1037
        struct sock *sk = sock->sk;
1038
        struct sk_buff *skb;
1039
        int copied, err;
1040
 
1041
        err = -EINVAL;
1042
        if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC))
1043
                goto out;
1044
 
1045
#if 0
1046
        /* What error should we return now? EUNATTACH? */
1047
        if (sk->protinfo.af_packet->ifindex < 0)
1048
                return -ENODEV;
1049
#endif
1050
 
1051
        /*
1052
         *      If the address length field is there to be filled in, we fill
1053
         *      it in now.
1054
         */
1055
 
1056
        if (sock->type == SOCK_PACKET)
1057
                msg->msg_namelen = sizeof(struct sockaddr_pkt);
1058
        else
1059
                msg->msg_namelen = sizeof(struct sockaddr_ll);
1060
 
1061
        /*
1062
         *      Call the generic datagram receiver. This handles all sorts
1063
         *      of horrible races and re-entrancy so we can forget about it
1064
         *      in the protocol layers.
1065
         *
1066
         *      Now it will return ENETDOWN, if device have just gone down,
1067
         *      but then it will block.
1068
         */
1069
 
1070
        skb=skb_recv_datagram(sk,flags,flags&MSG_DONTWAIT,&err);
1071
 
1072
        /*
1073
         *      An error occurred so return it. Because skb_recv_datagram()
1074
         *      handles the blocking we don't see and worry about blocking
1075
         *      retries.
1076
         */
1077
 
1078
        if(skb==NULL)
1079
                goto out;
1080
 
1081
        /*
1082
         *      You lose any data beyond the buffer you gave. If it worries a
1083
         *      user program they can ask the device for its MTU anyway.
1084
         */
1085
 
1086
        copied = skb->len;
1087
        if (copied > len)
1088
        {
1089
                copied=len;
1090
                msg->msg_flags|=MSG_TRUNC;
1091
        }
1092
 
1093
        err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1094
        if (err)
1095
                goto out_free;
1096
 
1097
        sock_recv_timestamp(msg, sk, skb);
1098
 
1099
        if (msg->msg_name)
1100
                memcpy(msg->msg_name, skb->cb, msg->msg_namelen);
1101
 
1102
        /*
1103
         *      Free or return the buffer as appropriate. Again this
1104
         *      hides all the races and re-entrancy issues from us.
1105
         */
1106
        err = (flags&MSG_TRUNC) ? skb->len : copied;
1107
 
1108
out_free:
1109
        skb_free_datagram(sk, skb);
1110
out:
1111
        return err;
1112
}
1113
 
1114
#ifdef CONFIG_SOCK_PACKET
1115
static int packet_getname_spkt(struct socket *sock, struct sockaddr *uaddr,
1116
                               int *uaddr_len, int peer)
1117
{
1118
        struct net_device *dev;
1119
        struct sock *sk = sock->sk;
1120
 
1121
        if (peer)
1122
                return -EOPNOTSUPP;
1123
 
1124
        uaddr->sa_family = AF_PACKET;
1125
        dev = dev_get_by_index(sk->protinfo.af_packet->ifindex);
1126
        if (dev) {
1127
                strncpy(uaddr->sa_data, dev->name, 15);
1128
                dev_put(dev);
1129
        } else
1130
                memset(uaddr->sa_data, 0, 14);
1131
        *uaddr_len = sizeof(*uaddr);
1132
 
1133
        return 0;
1134
}
1135
#endif
1136
 
1137
static int packet_getname(struct socket *sock, struct sockaddr *uaddr,
1138
                          int *uaddr_len, int peer)
1139
{
1140
        struct net_device *dev;
1141
        struct sock *sk = sock->sk;
1142
        struct sockaddr_ll *sll = (struct sockaddr_ll*)uaddr;
1143
 
1144
        if (peer)
1145
                return -EOPNOTSUPP;
1146
 
1147
        sll->sll_family = AF_PACKET;
1148
        sll->sll_ifindex = sk->protinfo.af_packet->ifindex;
1149
        sll->sll_protocol = sk->num;
1150
        dev = dev_get_by_index(sk->protinfo.af_packet->ifindex);
1151
        if (dev) {
1152
                sll->sll_hatype = dev->type;
1153
                sll->sll_halen = dev->addr_len;
1154
                memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len);
1155
                dev_put(dev);
1156
        } else {
1157
                sll->sll_hatype = 0;     /* Bad: we have no ARPHRD_UNSPEC */
1158
                sll->sll_halen = 0;
1159
        }
1160
        *uaddr_len = sizeof(*sll);
1161
 
1162
        return 0;
1163
}
1164
 
1165
#ifdef CONFIG_PACKET_MULTICAST
1166
static void packet_dev_mc(struct net_device *dev, struct packet_mclist *i, int what)
1167
{
1168
        switch (i->type) {
1169
        case PACKET_MR_MULTICAST:
1170
                if (what > 0)
1171
                        dev_mc_add(dev, i->addr, i->alen, 0);
1172
                else
1173
                        dev_mc_delete(dev, i->addr, i->alen, 0);
1174
                break;
1175
        case PACKET_MR_PROMISC:
1176
                dev_set_promiscuity(dev, what);
1177
                break;
1178
        case PACKET_MR_ALLMULTI:
1179
                dev_set_allmulti(dev, what);
1180
                break;
1181
        default:;
1182
        }
1183
}
1184
 
1185
static void packet_dev_mclist(struct net_device *dev, struct packet_mclist *i, int what)
1186
{
1187
        for ( ; i; i=i->next) {
1188
                if (i->ifindex == dev->ifindex)
1189
                        packet_dev_mc(dev, i, what);
1190
        }
1191
}
1192
 
1193
static int packet_mc_add(struct sock *sk, struct packet_mreq *mreq)
1194
{
1195
        struct packet_mclist *ml, *i;
1196
        struct net_device *dev;
1197
        int err;
1198
 
1199
        rtnl_lock();
1200
 
1201
        err = -ENODEV;
1202
        dev = __dev_get_by_index(mreq->mr_ifindex);
1203
        if (!dev)
1204
                goto done;
1205
 
1206
        err = -EINVAL;
1207
        if (mreq->mr_alen > dev->addr_len)
1208
                goto done;
1209
 
1210
        err = -ENOBUFS;
1211
        i = (struct packet_mclist *)kmalloc(sizeof(*i), GFP_KERNEL);
1212
        if (i == NULL)
1213
                goto done;
1214
 
1215
        err = 0;
1216
        for (ml=sk->protinfo.af_packet->mclist; ml; ml=ml->next) {
1217
                if (ml->ifindex == mreq->mr_ifindex &&
1218
                    ml->type == mreq->mr_type &&
1219
                    ml->alen == mreq->mr_alen &&
1220
                    memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1221
                        ml->count++;
1222
                        /* Free the new element ... */
1223
                        kfree(i);
1224
                        goto done;
1225
                }
1226
        }
1227
 
1228
        i->type = mreq->mr_type;
1229
        i->ifindex = mreq->mr_ifindex;
1230
        i->alen = mreq->mr_alen;
1231
        memcpy(i->addr, mreq->mr_address, i->alen);
1232
        i->count = 1;
1233
        i->next = sk->protinfo.af_packet->mclist;
1234
        sk->protinfo.af_packet->mclist = i;
1235
        packet_dev_mc(dev, i, +1);
1236
 
1237
done:
1238
        rtnl_unlock();
1239
        return err;
1240
}
1241
 
1242
static int packet_mc_drop(struct sock *sk, struct packet_mreq *mreq)
1243
{
1244
        struct packet_mclist *ml, **mlp;
1245
 
1246
        rtnl_lock();
1247
 
1248
        for (mlp=&sk->protinfo.af_packet->mclist; (ml=*mlp)!=NULL; mlp=&ml->next) {
1249
                if (ml->ifindex == mreq->mr_ifindex &&
1250
                    ml->type == mreq->mr_type &&
1251
                    ml->alen == mreq->mr_alen &&
1252
                    memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) {
1253
                        if (--ml->count == 0) {
1254
                                struct net_device *dev;
1255
                                *mlp = ml->next;
1256
                                dev = dev_get_by_index(ml->ifindex);
1257
                                if (dev) {
1258
                                        packet_dev_mc(dev, ml, -1);
1259
                                        dev_put(dev);
1260
                                }
1261
                                kfree(ml);
1262
                        }
1263
                        rtnl_unlock();
1264
                        return 0;
1265
                }
1266
        }
1267
        rtnl_unlock();
1268
        return -EADDRNOTAVAIL;
1269
}
1270
 
1271
static void packet_flush_mclist(struct sock *sk)
1272
{
1273
        struct packet_mclist *ml;
1274
 
1275
        if (sk->protinfo.af_packet->mclist == NULL)
1276
                return;
1277
 
1278
        rtnl_lock();
1279
        while ((ml=sk->protinfo.af_packet->mclist) != NULL) {
1280
                struct net_device *dev;
1281
                sk->protinfo.af_packet->mclist = ml->next;
1282
                if ((dev = dev_get_by_index(ml->ifindex)) != NULL) {
1283
                        packet_dev_mc(dev, ml, -1);
1284
                        dev_put(dev);
1285
                }
1286
                kfree(ml);
1287
        }
1288
        rtnl_unlock();
1289
}
1290
#endif
1291
 
1292
static int
1293
packet_setsockopt(struct socket *sock, int level, int optname, char *optval, int optlen)
1294
{
1295
        struct sock *sk = sock->sk;
1296
        int ret;
1297
 
1298
        if (level != SOL_PACKET)
1299
                return -ENOPROTOOPT;
1300
 
1301
        switch(optname) {
1302
#ifdef CONFIG_PACKET_MULTICAST
1303
        case PACKET_ADD_MEMBERSHIP:
1304
        case PACKET_DROP_MEMBERSHIP:
1305
        {
1306
                struct packet_mreq mreq;
1307
                if (optlen<sizeof(mreq))
1308
                        return -EINVAL;
1309
                if (copy_from_user(&mreq,optval,sizeof(mreq)))
1310
                        return -EFAULT;
1311
                if (optname == PACKET_ADD_MEMBERSHIP)
1312
                        ret = packet_mc_add(sk, &mreq);
1313
                else
1314
                        ret = packet_mc_drop(sk, &mreq);
1315
                return ret;
1316
        }
1317
#endif
1318
#ifdef CONFIG_PACKET_MMAP
1319
        case PACKET_RX_RING:
1320
        {
1321
                struct tpacket_req req;
1322
 
1323
                if (optlen<sizeof(req))
1324
                        return -EINVAL;
1325
                if (copy_from_user(&req,optval,sizeof(req)))
1326
                        return -EFAULT;
1327
                return packet_set_ring(sk, &req, 0);
1328
        }
1329
        case PACKET_COPY_THRESH:
1330
        {
1331
                int val;
1332
 
1333
                if (optlen!=sizeof(val))
1334
                        return -EINVAL;
1335
                if (copy_from_user(&val,optval,sizeof(val)))
1336
                        return -EFAULT;
1337
 
1338
                sk->protinfo.af_packet->copy_thresh = val;
1339
                return 0;
1340
        }
1341
#endif
1342
        default:
1343
                return -ENOPROTOOPT;
1344
        }
1345
}
1346
 
1347
int packet_getsockopt(struct socket *sock, int level, int optname,
1348
                      char *optval, int *optlen)
1349
{
1350
        int len;
1351
        struct sock *sk = sock->sk;
1352
 
1353
        if (level != SOL_PACKET)
1354
                return -ENOPROTOOPT;
1355
 
1356
        if (get_user(len,optlen))
1357
                return -EFAULT;
1358
 
1359
        if (len < 0)
1360
                return -EINVAL;
1361
 
1362
        switch(optname) {
1363
        case PACKET_STATISTICS:
1364
        {
1365
                struct tpacket_stats st;
1366
 
1367
                if (len > sizeof(struct tpacket_stats))
1368
                        len = sizeof(struct tpacket_stats);
1369
                spin_lock_bh(&sk->receive_queue.lock);
1370
                st = sk->protinfo.af_packet->stats;
1371
                memset(&sk->protinfo.af_packet->stats, 0, sizeof(st));
1372
                spin_unlock_bh(&sk->receive_queue.lock);
1373
                st.tp_packets += st.tp_drops;
1374
 
1375
                if (copy_to_user(optval, &st, len))
1376
                        return -EFAULT;
1377
                break;
1378
        }
1379
        default:
1380
                return -ENOPROTOOPT;
1381
        }
1382
 
1383
        if (put_user(len, optlen))
1384
                return -EFAULT;
1385
        return 0;
1386
}
1387
 
1388
 
1389
static int packet_notifier(struct notifier_block *this, unsigned long msg, void *data)
1390
{
1391
        struct sock *sk;
1392
        struct packet_opt *po;
1393
        struct net_device *dev = (struct net_device*)data;
1394
 
1395
        read_lock(&packet_sklist_lock);
1396
        for (sk = packet_sklist; sk; sk = sk->next) {
1397
                po = sk->protinfo.af_packet;
1398
 
1399
                switch (msg) {
1400
                case NETDEV_UNREGISTER:
1401
#ifdef CONFIG_PACKET_MULTICAST
1402
                        if (po->mclist)
1403
                                packet_dev_mclist(dev, po->mclist, -1);
1404
                        // fallthrough
1405
#endif
1406
                case NETDEV_DOWN:
1407
                        if (dev->ifindex == po->ifindex) {
1408
                                spin_lock(&po->bind_lock);
1409
                                if (po->running) {
1410
                                        dev_remove_pack(&po->prot_hook);
1411
                                        __sock_put(sk);
1412
                                        po->running = 0;
1413
                                        sk->err = ENETDOWN;
1414
                                        if (!sk->dead)
1415
                                                sk->error_report(sk);
1416
                                }
1417
                                if (msg == NETDEV_UNREGISTER) {
1418
                                        po->ifindex = -1;
1419
                                        po->prot_hook.dev = NULL;
1420
                                }
1421
                                spin_unlock(&po->bind_lock);
1422
                        }
1423
                        break;
1424
                case NETDEV_UP:
1425
                        spin_lock(&po->bind_lock);
1426
                        if (dev->ifindex == po->ifindex && sk->num && po->running==0) {
1427
                                dev_add_pack(&po->prot_hook);
1428
                                sock_hold(sk);
1429
                                po->running = 1;
1430
                        }
1431
                        spin_unlock(&po->bind_lock);
1432
                        break;
1433
                }
1434
        }
1435
        read_unlock(&packet_sklist_lock);
1436
        return NOTIFY_DONE;
1437
}
1438
 
1439
 
1440
static int packet_ioctl(struct socket *sock, unsigned int cmd,
1441
                        unsigned long arg)
1442
{
1443
        struct sock *sk = sock->sk;
1444
 
1445
        switch(cmd)
1446
        {
1447
                case SIOCOUTQ:
1448
                {
1449
                        int amount = atomic_read(&sk->wmem_alloc);
1450
                        return put_user(amount, (int *)arg);
1451
                }
1452
                case SIOCINQ:
1453
                {
1454
                        struct sk_buff *skb;
1455
                        int amount = 0;
1456
 
1457
                        spin_lock_bh(&sk->receive_queue.lock);
1458
                        skb = skb_peek(&sk->receive_queue);
1459
                        if (skb)
1460
                                amount = skb->len;
1461
                        spin_unlock_bh(&sk->receive_queue.lock);
1462
                        return put_user(amount, (int *)arg);
1463
                }
1464
                case FIOSETOWN:
1465
                case SIOCSPGRP: {
1466
                        int pid;
1467
                        if (get_user(pid, (int *) arg))
1468
                                return -EFAULT;
1469
                        if (current->pid != pid && current->pgrp != -pid &&
1470
                            !capable(CAP_NET_ADMIN))
1471
                                return -EPERM;
1472
                        sk->proc = pid;
1473
                        break;
1474
                }
1475
                case FIOGETOWN:
1476
                case SIOCGPGRP:
1477
                        return put_user(sk->proc, (int *)arg);
1478
                case SIOCGSTAMP:
1479
                        if(sk->stamp.tv_sec==0)
1480
                                return -ENOENT;
1481
                        if (copy_to_user((void *)arg, &sk->stamp,
1482
                                         sizeof(struct timeval)))
1483
                                return -EFAULT;
1484
                        break;
1485
                case SIOCGIFFLAGS:
1486
#ifndef CONFIG_INET
1487
                case SIOCSIFFLAGS:
1488
#endif
1489
                case SIOCGIFCONF:
1490
                case SIOCGIFMETRIC:
1491
                case SIOCSIFMETRIC:
1492
                case SIOCGIFMEM:
1493
                case SIOCSIFMEM:
1494
                case SIOCGIFMTU:
1495
                case SIOCSIFMTU:
1496
                case SIOCSIFLINK:
1497
                case SIOCGIFHWADDR:
1498
                case SIOCSIFHWADDR:
1499
                case SIOCSIFMAP:
1500
                case SIOCGIFMAP:
1501
                case SIOCSIFSLAVE:
1502
                case SIOCGIFSLAVE:
1503
                case SIOCGIFINDEX:
1504
                case SIOCGIFNAME:
1505
                case SIOCGIFCOUNT:
1506
                case SIOCSIFHWBROADCAST:
1507
                        return(dev_ioctl(cmd,(void *) arg));
1508
 
1509
                case SIOCGIFBR:
1510
                case SIOCSIFBR:
1511
#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
1512
#ifdef CONFIG_INET
1513
#ifdef CONFIG_KMOD
1514
                        if (br_ioctl_hook == NULL)
1515
                                request_module("bridge");
1516
#endif
1517
                        if (br_ioctl_hook != NULL)
1518
                                return br_ioctl_hook(arg);
1519
#endif
1520
#endif                          
1521
                        return -ENOPKG;
1522
 
1523
                case SIOCGIFDIVERT:
1524
                case SIOCSIFDIVERT:
1525
#ifdef CONFIG_NET_DIVERT
1526
                        return divert_ioctl(cmd, (struct divert_cf *) arg);
1527
#else
1528
                        return -ENOPKG;
1529
#endif /* CONFIG_NET_DIVERT */
1530
 
1531
#ifdef CONFIG_INET
1532
                case SIOCADDRT:
1533
                case SIOCDELRT:
1534
                case SIOCDARP:
1535
                case SIOCGARP:
1536
                case SIOCSARP:
1537
                case SIOCGIFADDR:
1538
                case SIOCSIFADDR:
1539
                case SIOCGIFBRDADDR:
1540
                case SIOCSIFBRDADDR:
1541
                case SIOCGIFNETMASK:
1542
                case SIOCSIFNETMASK:
1543
                case SIOCGIFDSTADDR:
1544
                case SIOCSIFDSTADDR:
1545
                case SIOCSIFFLAGS:
1546
                case SIOCADDDLCI:
1547
                case SIOCDELDLCI:
1548
                        return inet_dgram_ops.ioctl(sock, cmd, arg);
1549
#endif
1550
 
1551
                default:
1552
                        if ((cmd >= SIOCDEVPRIVATE) &&
1553
                            (cmd <= (SIOCDEVPRIVATE + 15)))
1554
                                return(dev_ioctl(cmd,(void *) arg));
1555
 
1556
#ifdef CONFIG_NET_RADIO
1557
                        if((cmd >= SIOCIWFIRST) && (cmd <= SIOCIWLAST))
1558
                                return(dev_ioctl(cmd,(void *) arg));
1559
#endif
1560
                        return -EOPNOTSUPP;
1561
        }
1562
        return 0;
1563
}
1564
 
1565
#ifndef CONFIG_PACKET_MMAP
1566
#define packet_mmap sock_no_mmap
1567
#define packet_poll datagram_poll
1568
#else
1569
 
1570
unsigned int packet_poll(struct file * file, struct socket *sock, poll_table *wait)
1571
{
1572
        struct sock *sk = sock->sk;
1573
        struct packet_opt *po = sk->protinfo.af_packet;
1574
        unsigned int mask = datagram_poll(file, sock, wait);
1575
 
1576
        spin_lock_bh(&sk->receive_queue.lock);
1577
        if (po->pg_vec) {
1578
                unsigned last = po->head ? po->head-1 : po->frame_max;
1579
                struct tpacket_hdr *h;
1580
 
1581
                h = (struct tpacket_hdr *)packet_lookup_frame(po, last);
1582
 
1583
                if (h->tp_status)
1584
                        mask |= POLLIN | POLLRDNORM;
1585
        }
1586
        spin_unlock_bh(&sk->receive_queue.lock);
1587
        return mask;
1588
}
1589
 
1590
 
1591
/* Dirty? Well, I still did not learn better way to account
1592
 * for user mmaps.
1593
 */
1594
 
1595
static void packet_mm_open(struct vm_area_struct *vma)
1596
{
1597
        struct file *file = vma->vm_file;
1598
        struct inode *inode = file->f_dentry->d_inode;
1599
        struct socket * sock = &inode->u.socket_i;
1600
        struct sock *sk = sock->sk;
1601
 
1602
        if (sk)
1603
                atomic_inc(&sk->protinfo.af_packet->mapped);
1604
}
1605
 
1606
static void packet_mm_close(struct vm_area_struct *vma)
1607
{
1608
        struct file *file = vma->vm_file;
1609
        struct inode *inode = file->f_dentry->d_inode;
1610
        struct socket * sock = &inode->u.socket_i;
1611
        struct sock *sk = sock->sk;
1612
 
1613
        if (sk)
1614
                atomic_dec(&sk->protinfo.af_packet->mapped);
1615
}
1616
 
1617
static struct vm_operations_struct packet_mmap_ops = {
1618
        open:   packet_mm_open,
1619
        close:  packet_mm_close,
1620
};
1621
 
1622
static void free_pg_vec(unsigned long *pg_vec, unsigned order, unsigned len)
1623
{
1624
        int i;
1625
 
1626
        for (i=0; i<len; i++) {
1627
                if (pg_vec[i]) {
1628
                        struct page *page, *pend;
1629
 
1630
                        pend = virt_to_page(pg_vec[i] + (PAGE_SIZE << order) - 1);
1631
                        for (page = virt_to_page(pg_vec[i]); page <= pend; page++)
1632
                                ClearPageReserved(page);
1633
                        free_pages(pg_vec[i], order);
1634
                }
1635
        }
1636
        kfree(pg_vec);
1637
}
1638
 
1639
 
1640
static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing)
1641
{
1642
        unsigned long *pg_vec = NULL;
1643
        struct packet_opt *po = sk->protinfo.af_packet;
1644
        int order = 0;
1645
        int err = 0;
1646
 
1647
        if (req->tp_block_nr) {
1648
                int i, l;
1649
 
1650
                /* Sanity tests and some calculations */
1651
 
1652
                if (po->pg_vec)
1653
                        return -EBUSY;
1654
 
1655
                if ((int)req->tp_block_size <= 0)
1656
                        return -EINVAL;
1657
                if (req->tp_block_size&(PAGE_SIZE-1))
1658
                        return -EINVAL;
1659
                if (req->tp_frame_size < TPACKET_HDRLEN)
1660
                        return -EINVAL;
1661
                if (req->tp_frame_size&(TPACKET_ALIGNMENT-1))
1662
                        return -EINVAL;
1663
 
1664
                po->frames_per_block = req->tp_block_size/req->tp_frame_size;
1665
                if (po->frames_per_block <= 0)
1666
                        return -EINVAL;
1667
                if (po->frames_per_block*req->tp_block_nr != req->tp_frame_nr)
1668
                        return -EINVAL;
1669
                /* OK! */
1670
 
1671
                /* Allocate page vector */
1672
                while ((PAGE_SIZE<<order) < req->tp_block_size)
1673
                        order++;
1674
 
1675
                err = -ENOMEM;
1676
 
1677
                pg_vec = kmalloc(req->tp_block_nr*sizeof(unsigned long*), GFP_KERNEL);
1678
                if (pg_vec == NULL)
1679
                        goto out;
1680
                memset(pg_vec, 0, req->tp_block_nr*sizeof(unsigned long*));
1681
 
1682
                for (i=0; i<req->tp_block_nr; i++) {
1683
                        struct page *page, *pend;
1684
                        pg_vec[i] = __get_free_pages(GFP_KERNEL, order);
1685
                        if (!pg_vec[i])
1686
                                goto out_free_pgvec;
1687
                        memset((void *)(pg_vec[i]), 0, PAGE_SIZE << order);
1688
                        pend = virt_to_page(pg_vec[i] + (PAGE_SIZE << order) - 1);
1689
                        for (page = virt_to_page(pg_vec[i]); page <= pend; page++)
1690
                                SetPageReserved(page);
1691
                }
1692
                /* Page vector is allocated */
1693
 
1694
                l = 0;
1695
                for (i=0; i<req->tp_block_nr; i++) {
1696
                        unsigned long ptr = pg_vec[i];
1697
                        struct tpacket_hdr *header;
1698
                        int k;
1699
 
1700
                        for (k=0; k<po->frames_per_block; k++) {
1701
 
1702
                                header = (struct tpacket_hdr*)ptr;
1703
                                header->tp_status = TP_STATUS_KERNEL;
1704
                                ptr += req->tp_frame_size;
1705
                        }
1706
                }
1707
                /* Done */
1708
        } else {
1709
                if (req->tp_frame_nr)
1710
                        return -EINVAL;
1711
        }
1712
 
1713
        lock_sock(sk);
1714
 
1715
        /* Detach socket from network */
1716
        spin_lock(&po->bind_lock);
1717
        if (po->running)
1718
                dev_remove_pack(&po->prot_hook);
1719
        spin_unlock(&po->bind_lock);
1720
 
1721
        err = -EBUSY;
1722
        if (closing || atomic_read(&po->mapped) == 0) {
1723
                err = 0;
1724
#define XC(a, b) ({ __typeof__ ((a)) __t; __t = (a); (a) = (b); __t; })
1725
 
1726
                spin_lock_bh(&sk->receive_queue.lock);
1727
                pg_vec = XC(po->pg_vec, pg_vec);
1728
                po->frame_max = req->tp_frame_nr-1;
1729
                po->head = 0;
1730
                po->frame_size = req->tp_frame_size;
1731
                spin_unlock_bh(&sk->receive_queue.lock);
1732
 
1733
                order = XC(po->pg_vec_order, order);
1734
                req->tp_block_nr = XC(po->pg_vec_len, req->tp_block_nr);
1735
 
1736
                po->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
1737
                po->prot_hook.func = po->pg_vec ? tpacket_rcv : packet_rcv;
1738
                skb_queue_purge(&sk->receive_queue);
1739
#undef XC
1740
                if (atomic_read(&po->mapped))
1741
                        printk(KERN_DEBUG "packet_mmap: vma is busy: %d\n", atomic_read(&po->mapped));
1742
        }
1743
 
1744
        spin_lock(&po->bind_lock);
1745
        if (po->running)
1746
                dev_add_pack(&po->prot_hook);
1747
        spin_unlock(&po->bind_lock);
1748
 
1749
        release_sock(sk);
1750
 
1751
out_free_pgvec:
1752
        if (pg_vec)
1753
                free_pg_vec(pg_vec, order, req->tp_block_nr);
1754
out:
1755
        return err;
1756
}
1757
 
1758
static int packet_mmap(struct file *file, struct socket *sock, struct vm_area_struct *vma)
1759
{
1760
        struct sock *sk = sock->sk;
1761
        struct packet_opt *po = sk->protinfo.af_packet;
1762
        unsigned long size;
1763
        unsigned long start;
1764
        int err = -EINVAL;
1765
        int i;
1766
 
1767
        if (vma->vm_pgoff)
1768
                return -EINVAL;
1769
 
1770
        size = vma->vm_end - vma->vm_start;
1771
 
1772
        lock_sock(sk);
1773
        if (po->pg_vec == NULL)
1774
                goto out;
1775
        if (size != po->pg_vec_len*po->pg_vec_pages*PAGE_SIZE)
1776
                goto out;
1777
 
1778
        atomic_inc(&po->mapped);
1779
        start = vma->vm_start;
1780
        err = -EAGAIN;
1781
        for (i=0; i<po->pg_vec_len; i++) {
1782
                if (remap_page_range(start, __pa(po->pg_vec[i]),
1783
                                     po->pg_vec_pages*PAGE_SIZE,
1784
                                     vma->vm_page_prot))
1785
                        goto out;
1786
                start += po->pg_vec_pages*PAGE_SIZE;
1787
        }
1788
        vma->vm_ops = &packet_mmap_ops;
1789
        err = 0;
1790
 
1791
out:
1792
        release_sock(sk);
1793
        return err;
1794
}
1795
#endif
1796
 
1797
 
1798
#ifdef CONFIG_SOCK_PACKET
1799
struct proto_ops packet_ops_spkt = {
1800
        family:         PF_PACKET,
1801
 
1802
        release:        packet_release,
1803
        bind:           packet_bind_spkt,
1804
        connect:        sock_no_connect,
1805
        socketpair:     sock_no_socketpair,
1806
        accept:         sock_no_accept,
1807
        getname:        packet_getname_spkt,
1808
        poll:           datagram_poll,
1809
        ioctl:          packet_ioctl,
1810
        listen:         sock_no_listen,
1811
        shutdown:       sock_no_shutdown,
1812
        setsockopt:     sock_no_setsockopt,
1813
        getsockopt:     sock_no_getsockopt,
1814
        sendmsg:        packet_sendmsg_spkt,
1815
        recvmsg:        packet_recvmsg,
1816
        mmap:           sock_no_mmap,
1817
        sendpage:       sock_no_sendpage,
1818
};
1819
#endif
1820
 
1821
struct proto_ops packet_ops = {
1822
        family:         PF_PACKET,
1823
 
1824
        release:        packet_release,
1825
        bind:           packet_bind,
1826
        connect:        sock_no_connect,
1827
        socketpair:     sock_no_socketpair,
1828
        accept:         sock_no_accept,
1829
        getname:        packet_getname,
1830
        poll:           packet_poll,
1831
        ioctl:          packet_ioctl,
1832
        listen:         sock_no_listen,
1833
        shutdown:       sock_no_shutdown,
1834
        setsockopt:     packet_setsockopt,
1835
        getsockopt:     packet_getsockopt,
1836
        sendmsg:        packet_sendmsg,
1837
        recvmsg:        packet_recvmsg,
1838
        mmap:           packet_mmap,
1839
        sendpage:       sock_no_sendpage,
1840
};
1841
 
1842
static struct net_proto_family packet_family_ops = {
1843
        family:         PF_PACKET,
1844
        create:         packet_create,
1845
};
1846
 
1847
static struct notifier_block packet_netdev_notifier = {
1848
        notifier_call:  packet_notifier,
1849
};
1850
 
1851
#ifdef CONFIG_PROC_FS
1852
static int packet_read_proc(char *buffer, char **start, off_t offset,
1853
                             int length, int *eof, void *data)
1854
{
1855
        off_t pos=0;
1856
        off_t begin=0;
1857
        int len=0;
1858
        struct sock *s;
1859
 
1860
        len+= sprintf(buffer,"sk       RefCnt Type Proto  Iface R Rmem   User   Inode\n");
1861
 
1862
        read_lock(&packet_sklist_lock);
1863
 
1864
        for (s = packet_sklist; s; s = s->next) {
1865
                len+=sprintf(buffer+len,"%p %-6d %-4d %04x   %-5d %1d %-6u %-6u %-6lu",
1866
                             s,
1867
                             atomic_read(&s->refcnt),
1868
                             s->type,
1869
                             ntohs(s->num),
1870
                             s->protinfo.af_packet->ifindex,
1871
                             s->protinfo.af_packet->running,
1872
                             atomic_read(&s->rmem_alloc),
1873
                             sock_i_uid(s),
1874
                             sock_i_ino(s)
1875
                             );
1876
 
1877
                buffer[len++]='\n';
1878
 
1879
                pos=begin+len;
1880
                if(pos<offset) {
1881
                        len=0;
1882
                        begin=pos;
1883
                }
1884
                if(pos>offset+length)
1885
                        goto done;
1886
        }
1887
        *eof = 1;
1888
 
1889
done:
1890
        read_unlock(&packet_sklist_lock);
1891
        *start=buffer+(offset-begin);
1892
        len-=(offset-begin);
1893
        if(len>length)
1894
                len=length;
1895
        if(len<0)
1896
                len=0;
1897
        return len;
1898
}
1899
#endif
1900
 
1901
static void __exit packet_exit(void)
1902
{
1903
        remove_proc_entry("net/packet", 0);
1904
        unregister_netdevice_notifier(&packet_netdev_notifier);
1905
        sock_unregister(PF_PACKET);
1906
        return;
1907
}
1908
 
1909
static int __init packet_init(void)
1910
{
1911
        sock_register(&packet_family_ops);
1912
        register_netdevice_notifier(&packet_netdev_notifier);
1913
#ifdef CONFIG_PROC_FS
1914
        create_proc_read_entry("net/packet", 0, 0, packet_read_proc, NULL);
1915
#endif
1916
        return 0;
1917
}
1918
 
1919
module_init(packet_init);
1920
module_exit(packet_exit);
1921
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

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