1 |
cebix |
1.1 |
/* |
2 |
|
|
* sheep_net.c - Linux driver for SheepShaver/Basilisk II networking (access to raw Ethernet packets) |
3 |
|
|
* |
4 |
cebix |
1.11 |
* sheep_net (C) 1999-2004 Mar"c" Hellwig and Christian Bauer |
5 |
cebix |
1.1 |
* |
6 |
|
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
cebix |
1.10 |
/* determine whether to use checksummed versions of kernel symbols */ |
22 |
|
|
#include <linux/config.h> |
23 |
cebix |
1.9 |
|
24 |
cebix |
1.10 |
#if defined(CONFIG_MODVERSIONS) && !defined(MODVERSIONS) |
25 |
|
|
#define MODVERSIONS |
26 |
cebix |
1.9 |
#endif |
27 |
|
|
|
28 |
cebix |
1.10 |
#if defined(MODVERSIONS) |
29 |
cebix |
1.9 |
#include <linux/modversions.h> |
30 |
|
|
#endif |
31 |
|
|
|
32 |
|
|
/* modversions.h redefines kernel symbols. Now include other headers */ |
33 |
|
|
#include <linux/kernel.h> |
34 |
|
|
#include <linux/module.h> |
35 |
cebix |
1.10 |
#include <linux/version.h> |
36 |
cebix |
1.9 |
|
37 |
cebix |
1.10 |
#include <linux/types.h> |
38 |
cebix |
1.1 |
#include <linux/miscdevice.h> |
39 |
|
|
#include <linux/netdevice.h> |
40 |
|
|
#include <linux/etherdevice.h> |
41 |
|
|
#include <linux/if_ether.h> |
42 |
|
|
#include <linux/if_arp.h> |
43 |
cebix |
1.7 |
#include <linux/fs.h> |
44 |
cebix |
1.1 |
#include <linux/poll.h> |
45 |
|
|
#include <linux/init.h> |
46 |
|
|
#include <net/sock.h> |
47 |
cebix |
1.7 |
#include <asm/uaccess.h> |
48 |
gbeauche |
1.12 |
#include <asm/ioctls.h> |
49 |
cebix |
1.4 |
#include <net/arp.h> |
50 |
|
|
#include <net/ip.h> |
51 |
cebix |
1.7 |
#include <linux/in.h> |
52 |
|
|
#include <linux/wait.h> |
53 |
cebix |
1.1 |
|
54 |
cebix |
1.5 |
MODULE_AUTHOR("Christian Bauer"); |
55 |
|
|
MODULE_DESCRIPTION("Pseudo ethernet device for emulators"); |
56 |
|
|
|
57 |
cebix |
1.4 |
/* Compatibility glue */ |
58 |
gbeauche |
1.12 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0) |
59 |
|
|
#define LINUX_26 |
60 |
|
|
#endif |
61 |
cebix |
1.4 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0) |
62 |
|
|
#define LINUX_24 |
63 |
|
|
#else |
64 |
|
|
#define net_device device |
65 |
|
|
typedef struct wait_queue *wait_queue_head_t; |
66 |
|
|
#define init_waitqueue_head(x) *(x)=NULL |
67 |
|
|
#endif |
68 |
|
|
|
69 |
gbeauche |
1.12 |
#ifdef LINUX_26 |
70 |
|
|
#define compat_sk_alloc(a,b,c) sk_alloc( (a), (b), (c), NULL ) |
71 |
|
|
#define skt_set_dead(skt) do {} while(0) |
72 |
|
|
#define wmem_alloc sk_wmem_alloc |
73 |
|
|
#else |
74 |
|
|
#define compat_sk_alloc sk_alloc |
75 |
|
|
#define skt_set_dead(skt) (skt)->dead = 1 |
76 |
|
|
#endif |
77 |
|
|
|
78 |
cebix |
1.1 |
#define DEBUG 0 |
79 |
|
|
|
80 |
|
|
#define bug printk |
81 |
|
|
#if DEBUG |
82 |
|
|
#define D(x) (x); |
83 |
|
|
#else |
84 |
|
|
#define D(x) ; |
85 |
|
|
#endif |
86 |
|
|
|
87 |
|
|
|
88 |
cebix |
1.4 |
/* Constants */ |
89 |
|
|
#define SHEEP_NET_MINOR 198 /* Driver minor number */ |
90 |
|
|
#define MAX_QUEUE 32 /* Maximum number of packets in queue */ |
91 |
|
|
#define PROT_MAGIC 1520 /* Our "magic" protocol type */ |
92 |
cebix |
1.1 |
|
93 |
cebix |
1.4 |
#define ETH_ADDR_MULTICAST 0x1 |
94 |
cebix |
1.7 |
#define ETH_ADDR_LOCALLY_DEFINED 0x02 |
95 |
cebix |
1.4 |
|
96 |
|
|
#define SIOC_MOL_GET_IPFILTER SIOCDEVPRIVATE |
97 |
|
|
#define SIOC_MOL_SET_IPFILTER (SIOCDEVPRIVATE + 1) |
98 |
|
|
|
99 |
|
|
/* Prototypes */ |
100 |
cebix |
1.1 |
static int sheep_net_open(struct inode *inode, struct file *f); |
101 |
|
|
static int sheep_net_release(struct inode *inode, struct file *f); |
102 |
|
|
static ssize_t sheep_net_read(struct file *f, char *buf, size_t count, loff_t *off); |
103 |
|
|
static ssize_t sheep_net_write(struct file *f, const char *buf, size_t count, loff_t *off); |
104 |
|
|
static unsigned int sheep_net_poll(struct file *f, struct poll_table_struct *wait); |
105 |
|
|
static int sheep_net_ioctl(struct inode *inode, struct file *f, unsigned int code, unsigned long arg); |
106 |
cebix |
1.4 |
static int sheep_net_receiver(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt); |
107 |
cebix |
1.1 |
|
108 |
|
|
|
109 |
|
|
/* |
110 |
|
|
* Driver private variables |
111 |
|
|
*/ |
112 |
|
|
|
113 |
|
|
struct SheepVars { |
114 |
gbeauche |
1.12 |
/* IMPORTANT: the packet_type struct must go first. It no longer |
115 |
|
|
(2.6) contains * a data field so we typecast to get the SheepVars |
116 |
|
|
struct */ |
117 |
|
|
struct packet_type pt; /* Receiver packet type */ |
118 |
cebix |
1.4 |
struct net_device *ether; /* The Ethernet device we're attached to */ |
119 |
|
|
struct sock *skt; /* Socket for communication with Ethernet card */ |
120 |
|
|
struct sk_buff_head queue; /* Receiver packet queue */ |
121 |
|
|
wait_queue_head_t wait; /* Wait queue for blocking read operations */ |
122 |
|
|
u32 ipfilter; /* Only receive IP packets destined for this address (host byte order) */ |
123 |
|
|
char eth_addr[6]; /* Hardware address of the Ethernet card */ |
124 |
|
|
char fake_addr[6]; /* Local faked hardware address (what SheepShaver sees) */ |
125 |
cebix |
1.1 |
}; |
126 |
|
|
|
127 |
|
|
|
128 |
|
|
/* |
129 |
|
|
* file_operations structure - has function pointers to the |
130 |
|
|
* various entry points for device operations |
131 |
|
|
*/ |
132 |
|
|
|
133 |
|
|
static struct file_operations sheep_net_fops = { |
134 |
gbeauche |
1.12 |
.owner = THIS_MODULE, |
135 |
|
|
.read = sheep_net_read, |
136 |
|
|
.write = sheep_net_write, |
137 |
|
|
.poll = sheep_net_poll, |
138 |
|
|
.ioctl = sheep_net_ioctl, |
139 |
|
|
.open = sheep_net_open, |
140 |
|
|
.release = sheep_net_release, |
141 |
cebix |
1.1 |
}; |
142 |
|
|
|
143 |
|
|
|
144 |
|
|
/* |
145 |
|
|
* miscdevice structure for driver initialization |
146 |
|
|
*/ |
147 |
|
|
|
148 |
|
|
static struct miscdevice sheep_net_device = { |
149 |
gbeauche |
1.12 |
.minor = SHEEP_NET_MINOR, /* minor number */ |
150 |
|
|
.name = "sheep_net", /* name */ |
151 |
|
|
.fops = &sheep_net_fops |
152 |
cebix |
1.1 |
}; |
153 |
|
|
|
154 |
|
|
|
155 |
|
|
/* |
156 |
|
|
* Initialize module |
157 |
|
|
*/ |
158 |
|
|
|
159 |
|
|
int init_module(void) |
160 |
|
|
{ |
161 |
|
|
int ret; |
162 |
|
|
|
163 |
cebix |
1.4 |
/* Register driver */ |
164 |
cebix |
1.1 |
ret = misc_register(&sheep_net_device); |
165 |
|
|
D(bug("Sheep net driver installed\n")); |
166 |
|
|
return ret; |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
|
170 |
|
|
/* |
171 |
|
|
* Deinitialize module |
172 |
|
|
*/ |
173 |
|
|
|
174 |
gbeauche |
1.12 |
void cleanup_module(void) |
175 |
cebix |
1.1 |
{ |
176 |
cebix |
1.4 |
/* Unregister driver */ |
177 |
gbeauche |
1.12 |
misc_deregister(&sheep_net_device); |
178 |
cebix |
1.1 |
D(bug("Sheep net driver removed\n")); |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
|
182 |
|
|
/* |
183 |
|
|
* Driver open() function |
184 |
|
|
*/ |
185 |
|
|
|
186 |
|
|
static int sheep_net_open(struct inode *inode, struct file *f) |
187 |
|
|
{ |
188 |
|
|
struct SheepVars *v; |
189 |
|
|
D(bug("sheep_net: open\n")); |
190 |
|
|
|
191 |
cebix |
1.4 |
/* Must be opened with read permissions */ |
192 |
cebix |
1.1 |
if ((f->f_flags & O_ACCMODE) == O_WRONLY) |
193 |
|
|
return -EPERM; |
194 |
|
|
|
195 |
cebix |
1.4 |
/* Allocate private variables */ |
196 |
gbeauche |
1.12 |
v = (struct SheepVars *)(f->private_data = kmalloc(sizeof(struct SheepVars), GFP_USER)); |
197 |
cebix |
1.1 |
if (v == NULL) |
198 |
|
|
return -ENOMEM; |
199 |
|
|
memset(v, 0, sizeof(struct SheepVars)); |
200 |
|
|
skb_queue_head_init(&v->queue); |
201 |
cebix |
1.4 |
init_waitqueue_head(&v->wait); |
202 |
|
|
v->fake_addr[0] = 0xfe; |
203 |
|
|
v->fake_addr[1] = 0xfd; |
204 |
|
|
v->fake_addr[2] = 0xde; |
205 |
|
|
v->fake_addr[3] = 0xad; |
206 |
|
|
v->fake_addr[4] = 0xbe; |
207 |
|
|
v->fake_addr[5] = 0xef; |
208 |
cebix |
1.1 |
|
209 |
cebix |
1.4 |
/* Yes, we're open */ |
210 |
gbeauche |
1.12 |
#ifndef LINUX_26 |
211 |
cebix |
1.1 |
MOD_INC_USE_COUNT; |
212 |
gbeauche |
1.12 |
#endif |
213 |
cebix |
1.1 |
return 0; |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
|
217 |
|
|
/* |
218 |
|
|
* Driver release() function |
219 |
|
|
*/ |
220 |
|
|
|
221 |
|
|
static int sheep_net_release(struct inode *inode, struct file *f) |
222 |
|
|
{ |
223 |
|
|
struct SheepVars *v = (struct SheepVars *)f->private_data; |
224 |
|
|
struct sk_buff *skb; |
225 |
|
|
D(bug("sheep_net: close\n")); |
226 |
|
|
|
227 |
cebix |
1.4 |
/* Detach from Ethernet card */ |
228 |
cebix |
1.1 |
if (v->ether) { |
229 |
|
|
dev_remove_pack(&v->pt); |
230 |
|
|
sk_free(v->skt); |
231 |
|
|
v->skt = NULL; |
232 |
cebix |
1.4 |
#ifdef LINUX_24 |
233 |
|
|
dev_put( v->ether ); |
234 |
|
|
#endif |
235 |
cebix |
1.1 |
v->ether = NULL; |
236 |
|
|
} |
237 |
|
|
|
238 |
cebix |
1.4 |
/* Empty packet queue */ |
239 |
cebix |
1.1 |
while ((skb = skb_dequeue(&v->queue)) != NULL) |
240 |
|
|
dev_kfree_skb(skb); |
241 |
|
|
|
242 |
cebix |
1.4 |
/* Free private variables */ |
243 |
cebix |
1.1 |
kfree(v); |
244 |
|
|
|
245 |
cebix |
1.4 |
/* Sorry, we're closed */ |
246 |
gbeauche |
1.12 |
#ifndef LINUX_26 |
247 |
cebix |
1.1 |
MOD_DEC_USE_COUNT; |
248 |
gbeauche |
1.12 |
#endif |
249 |
cebix |
1.1 |
return 0; |
250 |
|
|
} |
251 |
|
|
|
252 |
|
|
|
253 |
|
|
/* |
254 |
cebix |
1.4 |
* Check whether an Ethernet address is the local (attached) one or |
255 |
|
|
* the fake one |
256 |
|
|
*/ |
257 |
|
|
|
258 |
|
|
static inline int is_local_addr(struct SheepVars *v, void *a) |
259 |
|
|
{ |
260 |
|
|
return memcmp(a, v->eth_addr, 6) == 0; |
261 |
|
|
} |
262 |
|
|
|
263 |
|
|
static inline int is_fake_addr(struct SheepVars *v, void *a) |
264 |
|
|
{ |
265 |
|
|
return memcmp(a, v->fake_addr, 6) == 0; |
266 |
|
|
} |
267 |
|
|
|
268 |
|
|
|
269 |
|
|
/* |
270 |
|
|
* Outgoing packet. Replace the fake enet addr with the real local one. |
271 |
|
|
*/ |
272 |
|
|
|
273 |
|
|
static inline void do_demasq(struct SheepVars *v, u8 *p) |
274 |
|
|
{ |
275 |
|
|
memcpy(p, v->eth_addr, 6); |
276 |
|
|
} |
277 |
|
|
|
278 |
|
|
static void demasquerade(struct SheepVars *v, struct sk_buff *skb) |
279 |
|
|
{ |
280 |
|
|
u8 *p = skb->mac.raw; |
281 |
|
|
int proto = (p[12] << 8) | p[13]; |
282 |
|
|
|
283 |
|
|
do_demasq(v, p + 6); /* source address */ |
284 |
|
|
|
285 |
|
|
/* Need to fix ARP packets */ |
286 |
|
|
if (proto == ETH_P_ARP) { |
287 |
|
|
if (is_fake_addr(v, p + 14 + 8)) /* sender HW-addr */ |
288 |
|
|
do_demasq(v, p + 14 + 8); |
289 |
|
|
} |
290 |
|
|
|
291 |
|
|
/* ...and AARPs (snap code: 0x00,0x00,0x00,0x80,0xF3) */ |
292 |
|
|
if (p[17] == 0 && p[18] == 0 && p[19] == 0 && p[20] == 0x80 && p[21] == 0xf3) { |
293 |
|
|
/* XXX: we should perhaps look for the 802 frame too */ |
294 |
|
|
if (is_fake_addr(v, p + 30)) |
295 |
|
|
do_demasq(v, p + 30); /* sender HW-addr */ |
296 |
|
|
} |
297 |
|
|
} |
298 |
|
|
|
299 |
|
|
|
300 |
|
|
/* |
301 |
|
|
* Incoming packet. Replace the local enet addr with the fake one. |
302 |
|
|
*/ |
303 |
|
|
|
304 |
|
|
static inline void do_masq(struct SheepVars *v, u8 *p) |
305 |
|
|
{ |
306 |
|
|
memcpy(p, v->fake_addr, 6); |
307 |
|
|
} |
308 |
|
|
|
309 |
|
|
static void masquerade(struct SheepVars *v, struct sk_buff *skb) |
310 |
|
|
{ |
311 |
|
|
u8 *p = skb->mac.raw; |
312 |
|
|
if (!(p[0] & ETH_ADDR_MULTICAST)) |
313 |
|
|
do_masq(v, p); /* destination address */ |
314 |
|
|
|
315 |
|
|
/* XXX: reverse ARP might need to be fixed */ |
316 |
|
|
} |
317 |
|
|
|
318 |
|
|
|
319 |
|
|
/* |
320 |
cebix |
1.1 |
* Driver read() function |
321 |
|
|
*/ |
322 |
|
|
|
323 |
|
|
static ssize_t sheep_net_read(struct file *f, char *buf, size_t count, loff_t *off) |
324 |
|
|
{ |
325 |
|
|
struct SheepVars *v = (struct SheepVars *)f->private_data; |
326 |
|
|
struct sk_buff *skb; |
327 |
cebix |
1.4 |
|
328 |
cebix |
1.1 |
D(bug("sheep_net: read\n")); |
329 |
|
|
|
330 |
|
|
for (;;) { |
331 |
|
|
|
332 |
cebix |
1.4 |
/* Get next packet from queue */ |
333 |
cebix |
1.1 |
skb = skb_dequeue(&v->queue); |
334 |
|
|
if (skb != NULL || (f->f_flags & O_NONBLOCK)) |
335 |
|
|
break; |
336 |
|
|
|
337 |
cebix |
1.4 |
/* No packet in queue and in blocking mode, so block */ |
338 |
cebix |
1.1 |
interruptible_sleep_on(&v->wait); |
339 |
|
|
|
340 |
cebix |
1.4 |
/* Signal received? Then bail out */ |
341 |
|
|
if (signal_pending(current)) |
342 |
|
|
return -EINTR; |
343 |
cebix |
1.1 |
} |
344 |
|
|
if (skb == NULL) |
345 |
|
|
return -EAGAIN; |
346 |
|
|
|
347 |
cebix |
1.4 |
/* Pass packet to caller */ |
348 |
cebix |
1.1 |
if (count > skb->len) |
349 |
|
|
count = skb->len; |
350 |
|
|
if (copy_to_user(buf, skb->data, count)) |
351 |
|
|
count = -EFAULT; |
352 |
|
|
dev_kfree_skb(skb); |
353 |
|
|
return count; |
354 |
|
|
} |
355 |
|
|
|
356 |
|
|
|
357 |
|
|
/* |
358 |
|
|
* Driver write() function |
359 |
|
|
*/ |
360 |
|
|
|
361 |
|
|
static ssize_t sheep_net_write(struct file *f, const char *buf, size_t count, loff_t *off) |
362 |
|
|
{ |
363 |
|
|
struct SheepVars *v = (struct SheepVars *)f->private_data; |
364 |
|
|
struct sk_buff *skb; |
365 |
|
|
char *p; |
366 |
|
|
D(bug("sheep_net: write\n")); |
367 |
|
|
|
368 |
cebix |
1.4 |
/* Check packet size */ |
369 |
cebix |
1.1 |
if (count < sizeof(struct ethhdr)) |
370 |
|
|
return -EINVAL; |
371 |
cebix |
1.7 |
if (count > 1514) { |
372 |
|
|
printk("sheep_net_write: packet size > 1514\n"); |
373 |
cebix |
1.1 |
count = 1514; |
374 |
cebix |
1.7 |
} |
375 |
cebix |
1.1 |
|
376 |
cebix |
1.4 |
/* Interface active? */ |
377 |
cebix |
1.1 |
if (v->ether == NULL) |
378 |
|
|
return count; |
379 |
|
|
|
380 |
cebix |
1.4 |
/* Allocate buffer for packet */ |
381 |
cebix |
1.1 |
skb = dev_alloc_skb(count); |
382 |
|
|
if (skb == NULL) |
383 |
|
|
return -ENOBUFS; |
384 |
|
|
|
385 |
cebix |
1.4 |
/* Stuff packet in buffer */ |
386 |
cebix |
1.1 |
p = skb_put(skb, count); |
387 |
|
|
if (copy_from_user(p, buf, count)) { |
388 |
|
|
kfree_skb(skb); |
389 |
|
|
return -EFAULT; |
390 |
|
|
} |
391 |
|
|
|
392 |
cebix |
1.4 |
/* Transmit packet */ |
393 |
cebix |
1.1 |
atomic_add(skb->truesize, &v->skt->wmem_alloc); |
394 |
|
|
skb->sk = v->skt; |
395 |
|
|
skb->dev = v->ether; |
396 |
|
|
skb->priority = 0; |
397 |
|
|
skb->nh.raw = skb->h.raw = skb->data + v->ether->hard_header_len; |
398 |
cebix |
1.4 |
skb->mac.raw = skb->data; |
399 |
|
|
|
400 |
|
|
/* Base the IP-filtering on the IP address in any outgoing ARP packets */ |
401 |
|
|
if (skb->mac.ethernet->h_proto == htons(ETH_P_ARP)) { |
402 |
|
|
u8 *p = &skb->data[14+14]; /* source IP address */ |
403 |
|
|
u32 ip = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; |
404 |
|
|
if (ip != v->ipfilter) { |
405 |
|
|
v->ipfilter = ip; |
406 |
|
|
printk("sheep_net: ipfilter set to %d.%d.%d.%d\n", (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff); |
407 |
|
|
} |
408 |
|
|
} |
409 |
|
|
|
410 |
|
|
/* Is this packet addressed solely to the local host? */ |
411 |
|
|
if (is_local_addr(v, skb->data) && !(skb->data[0] & ETH_ADDR_MULTICAST)) { |
412 |
|
|
skb->protocol = eth_type_trans(skb, v->ether); |
413 |
|
|
netif_rx(skb); |
414 |
|
|
return count; |
415 |
|
|
} |
416 |
|
|
if (skb->data[0] & ETH_ADDR_MULTICAST) { |
417 |
|
|
/* We can't clone the skb since we will manipulate the data below */ |
418 |
|
|
struct sk_buff *lskb = skb_copy(skb, GFP_ATOMIC); |
419 |
|
|
if (lskb) { |
420 |
|
|
lskb->protocol = eth_type_trans(lskb, v->ether); |
421 |
|
|
netif_rx(lskb); |
422 |
|
|
} |
423 |
|
|
} |
424 |
|
|
|
425 |
|
|
/* Outgoing packet (will be on the net) */ |
426 |
|
|
demasquerade(v, skb); |
427 |
|
|
|
428 |
|
|
skb->protocol = PROT_MAGIC; /* Magic value (we can recognize the packet in sheep_net_receiver) */ |
429 |
cebix |
1.1 |
dev_queue_xmit(skb); |
430 |
|
|
return count; |
431 |
|
|
} |
432 |
|
|
|
433 |
|
|
|
434 |
|
|
/* |
435 |
|
|
* Driver poll() function |
436 |
|
|
*/ |
437 |
|
|
|
438 |
|
|
static unsigned int sheep_net_poll(struct file *f, struct poll_table_struct *wait) |
439 |
|
|
{ |
440 |
|
|
struct SheepVars *v = (struct SheepVars *)f->private_data; |
441 |
|
|
D(bug("sheep_net: poll\n")); |
442 |
|
|
|
443 |
cebix |
1.4 |
/* Packets in queue? Then return */ |
444 |
|
|
if (!skb_queue_empty(&v->queue)) |
445 |
cebix |
1.1 |
return POLLIN | POLLRDNORM; |
446 |
|
|
|
447 |
cebix |
1.4 |
/* Otherwise wait for packet */ |
448 |
cebix |
1.1 |
poll_wait(f, &v->wait, wait); |
449 |
cebix |
1.4 |
if (!skb_queue_empty(&v->queue)) |
450 |
cebix |
1.1 |
return POLLIN | POLLRDNORM; |
451 |
cebix |
1.4 |
else |
452 |
cebix |
1.1 |
return 0; |
453 |
|
|
} |
454 |
|
|
|
455 |
|
|
|
456 |
|
|
/* |
457 |
|
|
* Driver ioctl() function |
458 |
|
|
*/ |
459 |
|
|
|
460 |
|
|
static int sheep_net_ioctl(struct inode *inode, struct file *f, unsigned int code, unsigned long arg) |
461 |
|
|
{ |
462 |
|
|
struct SheepVars *v = (struct SheepVars *)f->private_data; |
463 |
|
|
D(bug("sheep_net: ioctl %04x\n", code)); |
464 |
|
|
|
465 |
|
|
switch (code) { |
466 |
|
|
|
467 |
cebix |
1.4 |
/* Attach to Ethernet card |
468 |
cebix |
1.7 |
arg: pointer to name of Ethernet device (char[20]) */ |
469 |
cebix |
1.1 |
case SIOCSIFLINK: { |
470 |
cebix |
1.7 |
char name[20]; |
471 |
cebix |
1.4 |
int err; |
472 |
cebix |
1.1 |
|
473 |
cebix |
1.4 |
/* Already attached? */ |
474 |
cebix |
1.1 |
if (v->ether) |
475 |
|
|
return -EBUSY; |
476 |
|
|
|
477 |
cebix |
1.4 |
/* Get Ethernet card name */ |
478 |
cebix |
1.7 |
if (copy_from_user(name, (void *)arg, 20)) |
479 |
cebix |
1.1 |
return -EFAULT; |
480 |
cebix |
1.7 |
name[19] = 0; |
481 |
cebix |
1.1 |
|
482 |
cebix |
1.4 |
/* Find card */ |
483 |
|
|
#ifdef LINUX_24 |
484 |
|
|
v->ether = dev_get_by_name(name); |
485 |
|
|
#else |
486 |
cebix |
1.1 |
dev_lock_list(); |
487 |
|
|
v->ether = dev_get(name); |
488 |
cebix |
1.4 |
#endif |
489 |
cebix |
1.1 |
if (v->ether == NULL) { |
490 |
cebix |
1.4 |
err = -ENODEV; |
491 |
|
|
goto error; |
492 |
cebix |
1.1 |
} |
493 |
|
|
|
494 |
cebix |
1.4 |
/* Is it Ethernet? */ |
495 |
cebix |
1.1 |
if (v->ether->type != ARPHRD_ETHER) { |
496 |
cebix |
1.4 |
err = -EINVAL; |
497 |
|
|
goto error; |
498 |
cebix |
1.1 |
} |
499 |
|
|
|
500 |
cebix |
1.4 |
/* Remember the card's hardware address */ |
501 |
|
|
memcpy(v->eth_addr, v->ether->dev_addr, 6); |
502 |
|
|
|
503 |
|
|
/* Allocate socket */ |
504 |
gbeauche |
1.12 |
v->skt = compat_sk_alloc(0, GFP_USER, 1); |
505 |
cebix |
1.1 |
if (v->skt == NULL) { |
506 |
cebix |
1.4 |
err = -ENOMEM; |
507 |
|
|
goto error; |
508 |
cebix |
1.1 |
} |
509 |
gbeauche |
1.12 |
skt_set_dead(v->skt->dead); |
510 |
cebix |
1.1 |
|
511 |
cebix |
1.4 |
/* Attach packet handler */ |
512 |
cebix |
1.1 |
v->pt.type = htons(ETH_P_ALL); |
513 |
|
|
v->pt.dev = v->ether; |
514 |
|
|
v->pt.func = sheep_net_receiver; |
515 |
|
|
dev_add_pack(&v->pt); |
516 |
cebix |
1.4 |
#ifndef LINUX_24 |
517 |
cebix |
1.1 |
dev_unlock_list(); |
518 |
cebix |
1.4 |
#endif |
519 |
cebix |
1.1 |
return 0; |
520 |
cebix |
1.4 |
|
521 |
|
|
error: |
522 |
|
|
#ifdef LINUX_24 |
523 |
|
|
if (v->ether) |
524 |
|
|
dev_put(v->ether); |
525 |
|
|
#else |
526 |
|
|
dev_unlock_list(); |
527 |
|
|
#endif |
528 |
|
|
v->ether = NULL; |
529 |
|
|
return err; |
530 |
cebix |
1.1 |
} |
531 |
|
|
|
532 |
cebix |
1.4 |
/* Get hardware address of the sheep_net module |
533 |
|
|
arg: pointer to buffer (6 bytes) to store address */ |
534 |
cebix |
1.1 |
case SIOCGIFADDR: |
535 |
cebix |
1.4 |
if (copy_to_user((void *)arg, v->fake_addr, 6)) |
536 |
|
|
return -EFAULT; |
537 |
|
|
return 0; |
538 |
|
|
|
539 |
|
|
/* Set the hardware address of the sheep_net module |
540 |
|
|
arg: pointer to new address (6 bytes) */ |
541 |
|
|
case SIOCSIFADDR: |
542 |
|
|
if (copy_from_user(v->fake_addr, (void*)arg, 6)) |
543 |
cebix |
1.1 |
return -EFAULT; |
544 |
|
|
return 0; |
545 |
|
|
|
546 |
cebix |
1.4 |
/* Add multicast address |
547 |
|
|
arg: pointer to address (6 bytes) */ |
548 |
cebix |
1.1 |
case SIOCADDMULTI: { |
549 |
|
|
char addr[6]; |
550 |
|
|
if (v->ether == NULL) |
551 |
|
|
return -ENODEV; |
552 |
|
|
if (copy_from_user(addr, (void *)arg, 6)) |
553 |
|
|
return -EFAULT; |
554 |
|
|
return dev_mc_add(v->ether, addr, 6, 0); |
555 |
|
|
} |
556 |
|
|
|
557 |
cebix |
1.4 |
/* Remove multicast address |
558 |
|
|
arg: pointer to address (6 bytes) */ |
559 |
cebix |
1.1 |
case SIOCDELMULTI: { |
560 |
|
|
char addr[6]; |
561 |
|
|
if (v->ether == NULL) |
562 |
|
|
return -ENODEV; |
563 |
|
|
if (copy_from_user(addr, (void *)arg, 6)) |
564 |
|
|
return -EFAULT; |
565 |
|
|
return dev_mc_delete(v->ether, addr, 6, 0); |
566 |
|
|
} |
567 |
|
|
|
568 |
cebix |
1.4 |
/* Return size of first packet in queue */ |
569 |
cebix |
1.1 |
case FIONREAD: { |
570 |
|
|
int count = 0; |
571 |
|
|
struct sk_buff *skb; |
572 |
cebix |
1.4 |
#ifdef LINUX_24 |
573 |
gbeauche |
1.12 |
unsigned long flags; |
574 |
cebix |
1.4 |
spin_lock_irqsave(&v->queue.lock, flags); |
575 |
|
|
#else |
576 |
|
|
cli(); |
577 |
|
|
#endif |
578 |
cebix |
1.1 |
skb = skb_peek(&v->queue); |
579 |
|
|
if (skb) |
580 |
|
|
count = skb->len; |
581 |
cebix |
1.4 |
#ifdef LINUX_24 |
582 |
|
|
spin_unlock_irqrestore(&v->queue.lock, flags); |
583 |
|
|
#else |
584 |
|
|
sti(); |
585 |
|
|
#endif |
586 |
cebix |
1.1 |
return put_user(count, (int *)arg); |
587 |
|
|
} |
588 |
|
|
|
589 |
cebix |
1.4 |
case SIOC_MOL_GET_IPFILTER: |
590 |
|
|
return put_user(v->ipfilter, (int *)arg); |
591 |
|
|
|
592 |
|
|
case SIOC_MOL_SET_IPFILTER: |
593 |
|
|
v->ipfilter = arg; |
594 |
|
|
return 0; |
595 |
|
|
|
596 |
cebix |
1.1 |
default: |
597 |
|
|
return -ENOIOCTLCMD; |
598 |
|
|
} |
599 |
|
|
} |
600 |
|
|
|
601 |
|
|
|
602 |
|
|
/* |
603 |
|
|
* Packet receiver function |
604 |
|
|
*/ |
605 |
|
|
|
606 |
cebix |
1.4 |
static int sheep_net_receiver(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt) |
607 |
cebix |
1.1 |
{ |
608 |
gbeauche |
1.12 |
struct SheepVars *v = (struct SheepVars *)pt; |
609 |
cebix |
1.4 |
struct sk_buff *skb2; |
610 |
|
|
int fake; |
611 |
|
|
int multicast; |
612 |
cebix |
1.1 |
D(bug("sheep_net: packet received\n")); |
613 |
|
|
|
614 |
cebix |
1.4 |
multicast = (skb->mac.ethernet->h_dest[0] & ETH_ADDR_MULTICAST); |
615 |
|
|
fake = is_fake_addr(v, &skb->mac.ethernet->h_dest); |
616 |
|
|
|
617 |
|
|
/* Packet sent by us? Then discard */ |
618 |
|
|
if (is_fake_addr(v, &skb->mac.ethernet->h_source) || skb->protocol == PROT_MAGIC) |
619 |
|
|
goto drop; |
620 |
|
|
|
621 |
|
|
/* If the packet is not meant for this host, discard it */ |
622 |
|
|
if (!is_local_addr(v, &skb->mac.ethernet->h_dest) && !multicast && !fake) |
623 |
|
|
goto drop; |
624 |
|
|
|
625 |
|
|
/* Discard packets if queue gets too full */ |
626 |
|
|
if (skb_queue_len(&v->queue) > MAX_QUEUE) |
627 |
|
|
goto drop; |
628 |
|
|
|
629 |
|
|
/* Apply any filters here (if fake is true, then we *know* we want this packet) */ |
630 |
|
|
if (!fake) { |
631 |
|
|
if ((skb->protocol == htons(ETH_P_IP)) |
632 |
|
|
&& (!v->ipfilter || (ntohl(skb->h.ipiph->daddr) != v->ipfilter && !multicast))) |
633 |
|
|
goto drop; |
634 |
cebix |
1.1 |
} |
635 |
|
|
|
636 |
cebix |
1.4 |
/* Masquerade (we are typically a clone - best to make a real copy) */ |
637 |
|
|
skb2 = skb_copy(skb, GFP_ATOMIC); |
638 |
|
|
if (!skb2) |
639 |
|
|
goto drop; |
640 |
|
|
kfree_skb(skb); |
641 |
|
|
skb = skb2; |
642 |
|
|
masquerade(v, skb); |
643 |
cebix |
1.1 |
|
644 |
cebix |
1.4 |
/* We also want the Ethernet header */ |
645 |
cebix |
1.1 |
skb_push(skb, skb->data - skb->mac.raw); |
646 |
|
|
|
647 |
cebix |
1.4 |
/* Enqueue packet */ |
648 |
cebix |
1.1 |
skb_queue_tail(&v->queue, skb); |
649 |
|
|
|
650 |
cebix |
1.4 |
/* Unblock blocked read */ |
651 |
cebix |
1.1 |
wake_up(&v->wait); |
652 |
cebix |
1.4 |
return 0; |
653 |
|
|
|
654 |
|
|
drop: |
655 |
|
|
kfree_skb(skb); |
656 |
cebix |
1.1 |
return 0; |
657 |
|
|
} |
658 |
cebix |
1.9 |
|
659 |
|
|
MODULE_LICENSE("GPL"); |