~ubuntu-branches/ubuntu/vivid/basilisk2/vivid

« back to all changes in this revision

Viewing changes to src/Unix/Linux/NetDriver/sheep_net.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-03-06 19:33:01 UTC
  • mfrom: (2.1.4 gutsy)
  • Revision ID: james.westby@ubuntu.com-20080306193301-cc2ofn705nfsq3y0
Tags: 0.9.20070407-4
* Update copyright-check cdbs snippet to parse licensecheck using perl:
  + No longer randomly drops newlines
  + More compact hint file (and ordered more like wiki-proposed new copyright
    syntax).
  + No longer ignore files without copyright.
* Update copyright_hints.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *  sheep_net.c - Linux driver for SheepShaver/Basilisk II networking (access to raw Ethernet packets)
3
3
 *
4
 
 *  sheep_net (C) 1999-2002 Mar"c" Hellwig and Christian Bauer
 
4
 *  sheep_net (C) 1999-2004 Mar"c" Hellwig and Christian Bauer
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
45
45
#include <linux/init.h>
46
46
#include <net/sock.h>
47
47
#include <asm/uaccess.h>
 
48
#include <asm/ioctls.h>
48
49
#include <net/arp.h>
49
50
#include <net/ip.h>
50
51
#include <linux/in.h>
54
55
MODULE_DESCRIPTION("Pseudo ethernet device for emulators");
55
56
 
56
57
/* Compatibility glue */
 
58
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
 
59
#define LINUX_26
 
60
#endif
57
61
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
58
62
#define LINUX_24
59
63
#else
61
65
typedef struct wait_queue *wait_queue_head_t;
62
66
#define init_waitqueue_head(x) *(x)=NULL
63
67
#endif
 
68
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,9)
 
69
#define eth_hdr(skb) (skb)->mac.ethernet
 
70
#endif
 
71
 
 
72
#ifdef LINUX_26
 
73
#define compat_sk_alloc(a,b,c)  sk_alloc( (a), (b), (c), NULL )
 
74
#define skt_set_dead(skt)               do {} while(0)
 
75
#define wmem_alloc                              sk_wmem_alloc
 
76
#else
 
77
#define compat_sk_alloc                 sk_alloc
 
78
#define skt_set_dead(skt)               (skt)->dead = 1
 
79
#endif
64
80
 
65
81
#define DEBUG 0
66
82
 
98
114
 */
99
115
 
100
116
struct SheepVars {
 
117
        /* IMPORTANT: the packet_type struct must go first. It no longer
 
118
           (2.6) contains * a data field so we typecast to get the SheepVars
 
119
           struct */
 
120
        struct packet_type pt;          /* Receiver packet type */
101
121
        struct net_device *ether;       /* The Ethernet device we're attached to */
102
122
        struct sock *skt;                       /* Socket for communication with Ethernet card */
103
123
        struct sk_buff_head queue;      /* Receiver packet queue */
104
 
        struct packet_type pt;          /* Receiver packet type */
105
124
        wait_queue_head_t wait;         /* Wait queue for blocking read operations */
106
125
        u32 ipfilter;                           /* Only receive IP packets destined for this address (host byte order) */
107
126
        char eth_addr[6];                       /* Hardware address of the Ethernet card */
115
134
 */
116
135
 
117
136
static struct file_operations sheep_net_fops = {
118
 
        read:           sheep_net_read,
119
 
        write:          sheep_net_write,
120
 
        poll:           sheep_net_poll,
121
 
        ioctl:          sheep_net_ioctl,
122
 
        open:           sheep_net_open,
123
 
        release:        sheep_net_release,
 
137
        .owner          = THIS_MODULE,
 
138
        .read           = sheep_net_read,
 
139
        .write          = sheep_net_write,
 
140
        .poll           = sheep_net_poll,
 
141
        .ioctl          = sheep_net_ioctl,
 
142
        .open           = sheep_net_open,
 
143
        .release        = sheep_net_release,
124
144
};
125
145
 
126
146
 
129
149
 */
130
150
 
131
151
static struct miscdevice sheep_net_device = {
132
 
        SHEEP_NET_MINOR,        /* minor number */
133
 
        "sheep_net",            /* name */
134
 
        &sheep_net_fops,
135
 
        NULL,
136
 
        NULL
 
152
        .minor          = SHEEP_NET_MINOR,      /* minor number */
 
153
        .name           = "sheep_net",          /* name */
 
154
        .fops           = &sheep_net_fops
137
155
};
138
156
 
139
157
 
156
174
 *  Deinitialize module
157
175
 */
158
176
 
159
 
int cleanup_module(void)
 
177
void cleanup_module(void)
160
178
{
161
 
        int ret;
162
 
 
163
179
        /* Unregister driver */
164
 
        ret = misc_deregister(&sheep_net_device);
 
180
        misc_deregister(&sheep_net_device);
165
181
        D(bug("Sheep net driver removed\n"));
166
 
        return ret;
167
182
}
168
183
 
169
184
 
181
196
                return -EPERM;
182
197
 
183
198
        /* Allocate private variables */
184
 
        v = (struct SheepVars *)f->private_data = kmalloc(sizeof(struct SheepVars), GFP_USER);
 
199
        v = (struct SheepVars *)(f->private_data = kmalloc(sizeof(struct SheepVars), GFP_USER));
185
200
        if (v == NULL)
186
201
                return -ENOMEM;
187
202
        memset(v, 0, sizeof(struct SheepVars));
195
210
        v->fake_addr[5] = 0xef;
196
211
 
197
212
        /* Yes, we're open */
 
213
#ifndef LINUX_26
198
214
        MOD_INC_USE_COUNT;
 
215
#endif
199
216
        return 0;
200
217
}
201
218
 
229
246
        kfree(v);
230
247
 
231
248
        /* Sorry, we're closed */
 
249
#ifndef LINUX_26
232
250
        MOD_DEC_USE_COUNT;
 
251
#endif
233
252
        return 0;
234
253
}
235
254
 
382
401
        skb->mac.raw = skb->data;
383
402
 
384
403
        /* Base the IP-filtering on the IP address in any outgoing ARP packets */
385
 
        if (skb->mac.ethernet->h_proto == htons(ETH_P_ARP)) {
 
404
        if (eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) {
386
405
                u8 *p = &skb->data[14+14];      /* source IP address */
387
406
                u32 ip = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
388
407
                if (ip != v->ipfilter) {
485
504
                        memcpy(v->eth_addr, v->ether->dev_addr, 6);
486
505
 
487
506
                        /* Allocate socket */
488
 
                        v->skt = sk_alloc(0, GFP_USER, 1);
 
507
                        v->skt = compat_sk_alloc(0, GFP_USER, 1);
489
508
                        if (v->skt == NULL) {
490
509
                                err = -ENOMEM;
491
510
                                goto error;
492
511
                        }
493
 
                        v->skt->dead = 1;
 
512
                        skt_set_dead(v->skt);
494
513
 
495
514
                        /* Attach packet handler */
496
515
                        v->pt.type = htons(ETH_P_ALL);
497
516
                        v->pt.dev = v->ether;
498
517
                        v->pt.func = sheep_net_receiver;
499
 
                        v->pt.data = v;
500
518
                        dev_add_pack(&v->pt);
501
519
#ifndef LINUX_24
502
520
                        dev_unlock_list();
555
573
                        int count = 0;
556
574
                        struct sk_buff *skb;
557
575
#ifdef LINUX_24
558
 
                        long flags;
 
576
                        unsigned long flags;
559
577
                        spin_lock_irqsave(&v->queue.lock, flags);
560
578
#else
561
579
                        cli();
590
608
 
591
609
static int sheep_net_receiver(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt)
592
610
{
593
 
        struct SheepVars *v = (struct SheepVars *)pt->data;
 
611
        struct SheepVars *v = (struct SheepVars *)pt;
594
612
        struct sk_buff *skb2;
595
613
        int fake;
596
614
        int multicast;
597
615
        D(bug("sheep_net: packet received\n"));
598
616
 
599
 
        multicast = (skb->mac.ethernet->h_dest[0] & ETH_ADDR_MULTICAST);
600
 
        fake = is_fake_addr(v, &skb->mac.ethernet->h_dest);
 
617
        multicast = (eth_hdr(skb)->h_dest[0] & ETH_ADDR_MULTICAST);
 
618
        fake = is_fake_addr(v, &eth_hdr(skb)->h_dest);
601
619
 
602
620
        /* Packet sent by us? Then discard */
603
 
        if (is_fake_addr(v, &skb->mac.ethernet->h_source) || skb->protocol == PROT_MAGIC)
 
621
        if (is_fake_addr(v, &eth_hdr(skb)->h_source) || skb->protocol == PROT_MAGIC)
604
622
                goto drop;
605
623
 
606
624
        /* If the packet is not meant for this host, discard it */
607
 
        if (!is_local_addr(v, &skb->mac.ethernet->h_dest) && !multicast && !fake)
 
625
        if (!is_local_addr(v, &eth_hdr(skb)->h_dest) && !multicast && !fake)
608
626
                goto drop;
609
627
 
610
628
        /* Discard packets if queue gets too full */