1 |
|
/* |
2 |
|
* ether_unix.cpp - Ethernet device driver, Unix specific stuff (Linux and FreeBSD) |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-2001 Christian Bauer |
4 |
> |
* Basilisk II (C) 1997-2005 Christian Bauer |
5 |
|
* |
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 |
20 |
|
|
21 |
|
#include "sysdeps.h" |
22 |
|
|
23 |
< |
#include <sys/ioctl.h> |
23 |
> |
#ifdef HAVE_SYS_POLL_H |
24 |
|
#include <sys/poll.h> |
25 |
+ |
#endif |
26 |
+ |
#include <sys/ioctl.h> |
27 |
+ |
#include <sys/socket.h> |
28 |
+ |
#include <sys/wait.h> |
29 |
+ |
#include <netinet/in.h> |
30 |
|
#include <pthread.h> |
31 |
|
#include <semaphore.h> |
32 |
|
#include <errno.h> |
33 |
|
#include <stdio.h> |
34 |
+ |
#include <map> |
35 |
|
|
36 |
< |
#include <netinet/in.h> |
37 |
< |
#include <sys/socket.h> |
36 |
> |
#if defined(__FreeBSD__) || defined(sgi) || (defined(__APPLE__) && defined(__MACH__)) |
37 |
> |
#include <net/if.h> |
38 |
> |
#endif |
39 |
|
|
40 |
< |
#if defined(__FreeBSD__) |
40 |
> |
#if defined(HAVE_LINUX_IF_H) && defined(HAVE_LINUX_IF_TUN_H) |
41 |
> |
#include <linux/if.h> |
42 |
> |
#include <linux/if_tun.h> |
43 |
> |
#endif |
44 |
> |
|
45 |
> |
#if defined(HAVE_NET_IF_H) && defined(HAVE_NET_IF_TUN_H) |
46 |
|
#include <net/if.h> |
47 |
+ |
#include <net/if_tun.h> |
48 |
+ |
#endif |
49 |
+ |
|
50 |
+ |
#ifdef HAVE_SLIRP |
51 |
+ |
#include "libslirp.h" |
52 |
|
#endif |
53 |
|
|
54 |
|
#include "cpu_emulation.h" |
59 |
|
#include "ether.h" |
60 |
|
#include "ether_defs.h" |
61 |
|
|
62 |
+ |
#ifndef NO_STD_NAMESPACE |
63 |
+ |
using std::map; |
64 |
+ |
#endif |
65 |
+ |
|
66 |
|
#define DEBUG 0 |
67 |
|
#include "debug.h" |
68 |
|
|
69 |
|
#define MONITOR 0 |
70 |
|
|
71 |
|
|
72 |
< |
// List of attached protocols |
73 |
< |
struct NetProtocol { |
74 |
< |
NetProtocol *next; |
75 |
< |
uint16 type; |
76 |
< |
uint32 handler; |
72 |
> |
// Ethernet device types |
73 |
> |
enum { |
74 |
> |
NET_IF_SHEEPNET, |
75 |
> |
NET_IF_ETHERTAP, |
76 |
> |
NET_IF_TUNTAP, |
77 |
> |
NET_IF_SLIRP |
78 |
|
}; |
79 |
|
|
80 |
< |
static NetProtocol *prot_list = NULL; |
81 |
< |
|
80 |
> |
// Constants |
81 |
> |
static const char ETHERCONFIG_FILE_NAME[] = DATADIR "/tunconfig"; |
82 |
|
|
83 |
|
// Global variables |
84 |
|
static int fd = -1; // fd of sheep_net device |
86 |
|
static pthread_attr_t ether_thread_attr; // Packet reception thread attributes |
87 |
|
static bool thread_active = false; // Flag: Packet reception thread installed |
88 |
|
static sem_t int_ack; // Interrupt acknowledge semaphore |
67 |
– |
static bool is_ethertap; // Flag: Ethernet device is ethertap |
89 |
|
static bool udp_tunnel; // Flag: UDP tunnelling active, fd is the socket descriptor |
90 |
+ |
static int net_if_type = -1; // Ethernet device type |
91 |
+ |
static char *net_if_name = NULL; // TUN/TAP device name |
92 |
+ |
static const char *net_if_script = NULL; // Network config script |
93 |
+ |
static pthread_t slirp_thread; // Slirp reception thread |
94 |
+ |
static bool slirp_thread_active = false; // Flag: Slirp reception threadinstalled |
95 |
+ |
static int slirp_output_fd = -1; // fd of slirp output pipe |
96 |
+ |
static int slirp_input_fds[2] = { -1, -1 }; // fds of slirp input pipe |
97 |
+ |
|
98 |
+ |
// Attached network protocols, maps protocol type to MacOS handler address |
99 |
+ |
static map<uint16, uint32> net_protocols; |
100 |
|
|
101 |
|
// Prototypes |
102 |
|
static void *receive_func(void *arg); |
103 |
< |
|
104 |
< |
|
74 |
< |
/* |
75 |
< |
* Find protocol in list |
76 |
< |
*/ |
77 |
< |
|
78 |
< |
static NetProtocol *find_protocol(uint16 type) |
79 |
< |
{ |
80 |
< |
// All 802.2 types are the same |
81 |
< |
if (type <= 1500) |
82 |
< |
type = 0; |
83 |
< |
|
84 |
< |
// Search list (we could use hashing here but there are usually only three |
85 |
< |
// handlers installed: 0x0000 for AppleTalk and 0x0800/0x0806 for TCP/IP) |
86 |
< |
NetProtocol *p = prot_list; |
87 |
< |
while (p) { |
88 |
< |
if (p->type == type) |
89 |
< |
return p; |
90 |
< |
p = p->next; |
91 |
< |
} |
92 |
< |
return NULL; |
93 |
< |
} |
94 |
< |
|
95 |
< |
|
96 |
< |
/* |
97 |
< |
* Remove all protocols |
98 |
< |
*/ |
99 |
< |
|
100 |
< |
static void remove_all_protocols(void) |
101 |
< |
{ |
102 |
< |
NetProtocol *p = prot_list; |
103 |
< |
while (p) { |
104 |
< |
NetProtocol *next = p->next; |
105 |
< |
delete p; |
106 |
< |
p = next; |
107 |
< |
} |
108 |
< |
prot_list = NULL; |
109 |
< |
} |
103 |
> |
static void *slirp_receive_func(void *arg); |
104 |
> |
static int poll_fd(int fd); |
105 |
|
|
106 |
|
|
107 |
|
/* |
115 |
|
return false; |
116 |
|
} |
117 |
|
|
118 |
< |
pthread_attr_init(ðer_thread_attr); |
124 |
< |
#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) |
125 |
< |
if (geteuid() == 0) { |
126 |
< |
pthread_attr_setinheritsched(ðer_thread_attr, PTHREAD_EXPLICIT_SCHED); |
127 |
< |
pthread_attr_setschedpolicy(ðer_thread_attr, SCHED_FIFO); |
128 |
< |
struct sched_param fifo_param; |
129 |
< |
fifo_param.sched_priority = (sched_get_priority_min(SCHED_FIFO) + sched_get_priority_max(SCHED_FIFO)) / 2 + 1; |
130 |
< |
pthread_attr_setschedparam(ðer_thread_attr, &fifo_param); |
131 |
< |
} |
132 |
< |
#endif |
133 |
< |
|
118 |
> |
Set_pthread_attr(ðer_thread_attr, 1); |
119 |
|
thread_active = (pthread_create(ðer_thread, ðer_thread_attr, receive_func, NULL) == 0); |
120 |
|
if (!thread_active) { |
121 |
|
printf("WARNING: Cannot start Ethernet thread"); |
122 |
|
return false; |
123 |
|
} |
124 |
|
|
125 |
+ |
#ifdef HAVE_SLIRP |
126 |
+ |
if (net_if_type == NET_IF_SLIRP) { |
127 |
+ |
slirp_thread_active = (pthread_create(&slirp_thread, NULL, slirp_receive_func, NULL) == 0); |
128 |
+ |
if (!slirp_thread_active) { |
129 |
+ |
printf("WARNING: Cannot start slirp reception thread\n"); |
130 |
+ |
return false; |
131 |
+ |
} |
132 |
+ |
} |
133 |
+ |
#endif |
134 |
+ |
|
135 |
|
return true; |
136 |
|
} |
137 |
|
|
142 |
|
|
143 |
|
static void stop_thread(void) |
144 |
|
{ |
145 |
+ |
#ifdef HAVE_SLIRP |
146 |
+ |
if (slirp_thread_active) { |
147 |
+ |
#ifdef HAVE_PTHREAD_CANCEL |
148 |
+ |
pthread_cancel(slirp_thread); |
149 |
+ |
#endif |
150 |
+ |
pthread_join(slirp_thread, NULL); |
151 |
+ |
slirp_thread_active = false; |
152 |
+ |
} |
153 |
+ |
#endif |
154 |
+ |
|
155 |
|
if (thread_active) { |
156 |
+ |
#ifdef HAVE_PTHREAD_CANCEL |
157 |
|
pthread_cancel(ether_thread); |
158 |
+ |
#endif |
159 |
|
pthread_join(ether_thread, NULL); |
160 |
|
sem_destroy(&int_ack); |
161 |
|
thread_active = false; |
164 |
|
|
165 |
|
|
166 |
|
/* |
167 |
+ |
* Execute network script up|down |
168 |
+ |
*/ |
169 |
+ |
|
170 |
+ |
static bool execute_network_script(const char *action) |
171 |
+ |
{ |
172 |
+ |
if (net_if_script == NULL || net_if_name == NULL) |
173 |
+ |
return false; |
174 |
+ |
|
175 |
+ |
int pid = fork(); |
176 |
+ |
if (pid >= 0) { |
177 |
+ |
if (pid == 0) { |
178 |
+ |
char *args[4]; |
179 |
+ |
args[0] = (char *)net_if_script; |
180 |
+ |
args[1] = net_if_name; |
181 |
+ |
args[2] = (char *)action; |
182 |
+ |
args[3] = NULL; |
183 |
+ |
execv(net_if_script, args); |
184 |
+ |
exit(1); |
185 |
+ |
} |
186 |
+ |
int status; |
187 |
+ |
while (waitpid(pid, &status, 0) != pid); |
188 |
+ |
return WIFEXITED(status) && WEXITSTATUS(status) == 0; |
189 |
+ |
} |
190 |
+ |
|
191 |
+ |
return false; |
192 |
+ |
} |
193 |
+ |
|
194 |
+ |
|
195 |
+ |
/* |
196 |
|
* Initialization |
197 |
|
*/ |
198 |
|
|
206 |
|
if (name == NULL) |
207 |
|
return false; |
208 |
|
|
209 |
< |
// Is it Ethertap? |
210 |
< |
is_ethertap = (strncmp(name, "tap", 3) == 0); |
209 |
> |
// Determine Ethernet device type |
210 |
> |
net_if_type = -1; |
211 |
> |
if (strncmp(name, "tap", 3) == 0) |
212 |
> |
net_if_type = NET_IF_ETHERTAP; |
213 |
> |
#if ENABLE_TUNTAP |
214 |
> |
else if (strcmp(name, "tun") == 0) |
215 |
> |
net_if_type = NET_IF_TUNTAP; |
216 |
> |
#endif |
217 |
> |
#ifdef HAVE_SLIRP |
218 |
> |
else if (strcmp(name, "slirp") == 0) |
219 |
> |
net_if_type = NET_IF_SLIRP; |
220 |
> |
#endif |
221 |
> |
else |
222 |
> |
net_if_type = NET_IF_SHEEPNET; |
223 |
|
|
224 |
< |
// Open sheep_net or ethertap device |
224 |
> |
#ifdef HAVE_SLIRP |
225 |
> |
// Initialize slirp library |
226 |
> |
if (net_if_type == NET_IF_SLIRP) { |
227 |
> |
slirp_init(); |
228 |
> |
|
229 |
> |
// Open slirp output pipe |
230 |
> |
int fds[2]; |
231 |
> |
if (pipe(fds) < 0) |
232 |
> |
return false; |
233 |
> |
fd = fds[0]; |
234 |
> |
slirp_output_fd = fds[1]; |
235 |
> |
|
236 |
> |
// Open slirp input pipe |
237 |
> |
if (pipe(slirp_input_fds) < 0) |
238 |
> |
return false; |
239 |
> |
} |
240 |
> |
#endif |
241 |
> |
|
242 |
> |
// Open sheep_net or ethertap or TUN/TAP device |
243 |
|
char dev_name[16]; |
244 |
< |
if (is_ethertap) |
244 |
> |
switch (net_if_type) { |
245 |
> |
case NET_IF_ETHERTAP: |
246 |
|
sprintf(dev_name, "/dev/%s", name); |
247 |
< |
else |
247 |
> |
break; |
248 |
> |
case NET_IF_TUNTAP: |
249 |
> |
strcpy(dev_name, "/dev/net/tun"); |
250 |
> |
break; |
251 |
> |
case NET_IF_SHEEPNET: |
252 |
|
strcpy(dev_name, "/dev/sheep_net"); |
253 |
< |
fd = open(dev_name, O_RDWR); |
254 |
< |
if (fd < 0) { |
255 |
< |
sprintf(str, GetString(STR_NO_SHEEP_NET_DRIVER_WARN), dev_name, strerror(errno)); |
256 |
< |
WarningAlert(str); |
257 |
< |
goto open_error; |
253 |
> |
break; |
254 |
> |
} |
255 |
> |
if (net_if_type != NET_IF_SLIRP) { |
256 |
> |
fd = open(dev_name, O_RDWR); |
257 |
> |
if (fd < 0) { |
258 |
> |
sprintf(str, GetString(STR_NO_SHEEP_NET_DRIVER_WARN), dev_name, strerror(errno)); |
259 |
> |
WarningAlert(str); |
260 |
> |
goto open_error; |
261 |
> |
} |
262 |
|
} |
263 |
|
|
264 |
+ |
#if ENABLE_TUNTAP |
265 |
+ |
// Open TUN/TAP interface |
266 |
+ |
if (net_if_type == NET_IF_TUNTAP) { |
267 |
+ |
struct ifreq ifr; |
268 |
+ |
memset(&ifr, 0, sizeof(ifr)); |
269 |
+ |
ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
270 |
+ |
strcpy(ifr.ifr_name, "tun%d"); |
271 |
+ |
if (ioctl(fd, TUNSETIFF, (void *) &ifr) != 0) { |
272 |
+ |
sprintf(str, GetString(STR_SHEEP_NET_ATTACH_WARN), strerror(errno)); |
273 |
+ |
WarningAlert(str); |
274 |
+ |
goto open_error; |
275 |
+ |
} |
276 |
+ |
|
277 |
+ |
// Get network config script file path |
278 |
+ |
net_if_script = PrefsFindString("etherconfig"); |
279 |
+ |
if (net_if_script == NULL) |
280 |
+ |
net_if_script = ETHERCONFIG_FILE_NAME; |
281 |
+ |
|
282 |
+ |
// Start network script up |
283 |
+ |
if (net_if_script == NULL) { |
284 |
+ |
sprintf(str, GetString(STR_TUN_TAP_CONFIG_WARN), "script not found"); |
285 |
+ |
WarningAlert(str); |
286 |
+ |
goto open_error; |
287 |
+ |
} |
288 |
+ |
net_if_name = strdup(ifr.ifr_name); |
289 |
+ |
if (!execute_network_script("up")) { |
290 |
+ |
sprintf(str, GetString(STR_TUN_TAP_CONFIG_WARN), "script execute error"); |
291 |
+ |
WarningAlert(str); |
292 |
+ |
goto open_error; |
293 |
+ |
} |
294 |
+ |
D(bug("Connected to host network interface: %s\n", net_if_name)); |
295 |
+ |
} |
296 |
+ |
#endif |
297 |
+ |
|
298 |
|
#if defined(__linux__) |
299 |
|
// Attach sheep_net to selected Ethernet card |
300 |
< |
if (!is_ethertap && ioctl(fd, SIOCSIFLINK, name) < 0) { |
300 |
> |
if (net_if_type == NET_IF_SHEEPNET && ioctl(fd, SIOCSIFLINK, name) < 0) { |
301 |
|
sprintf(str, GetString(STR_SHEEP_NET_ATTACH_WARN), strerror(errno)); |
302 |
|
WarningAlert(str); |
303 |
|
goto open_error; |
308 |
|
ioctl(fd, FIONBIO, &nonblock); |
309 |
|
|
310 |
|
// Get Ethernet address |
311 |
< |
if (is_ethertap) { |
311 |
> |
if (net_if_type == NET_IF_ETHERTAP) { |
312 |
|
pid_t p = getpid(); // If configured for multicast, ethertap requires that the lower 32 bit of the Ethernet address are our PID |
313 |
|
ether_addr[0] = 0xfe; |
314 |
|
ether_addr[1] = 0xfd; |
316 |
|
ether_addr[3] = p >> 16; |
317 |
|
ether_addr[4] = p >> 8; |
318 |
|
ether_addr[5] = p; |
319 |
+ |
#ifdef HAVE_SLIRP |
320 |
+ |
} else if (net_if_type == NET_IF_SLIRP) { |
321 |
+ |
ether_addr[0] = 0x52; |
322 |
+ |
ether_addr[1] = 0x54; |
323 |
+ |
ether_addr[2] = 0x00; |
324 |
+ |
ether_addr[3] = 0x12; |
325 |
+ |
ether_addr[4] = 0x34; |
326 |
+ |
ether_addr[5] = 0x56; |
327 |
+ |
#endif |
328 |
|
} else |
329 |
|
ioctl(fd, SIOCGIFADDR, ether_addr); |
330 |
|
D(bug("Ethernet address %02x %02x %02x %02x %02x %02x\n", ether_addr[0], ether_addr[1], ether_addr[2], ether_addr[3], ether_addr[4], ether_addr[5])); |
343 |
|
close(fd); |
344 |
|
fd = -1; |
345 |
|
} |
346 |
+ |
if (slirp_input_fds[0] >= 0) { |
347 |
+ |
close(slirp_input_fds[0]); |
348 |
+ |
slirp_input_fds[0] = -1; |
349 |
+ |
} |
350 |
+ |
if (slirp_input_fds[1] >= 0) { |
351 |
+ |
close(slirp_input_fds[1]); |
352 |
+ |
slirp_input_fds[1] = -1; |
353 |
+ |
} |
354 |
+ |
if (slirp_output_fd >= 0) { |
355 |
+ |
close(slirp_output_fd); |
356 |
+ |
slirp_output_fd = -1; |
357 |
+ |
} |
358 |
|
return false; |
359 |
|
} |
360 |
|
|
365 |
|
|
366 |
|
void ether_exit(void) |
367 |
|
{ |
368 |
< |
// Stop reception thread |
369 |
< |
if (thread_active) { |
370 |
< |
pthread_cancel(ether_thread); |
371 |
< |
pthread_join(ether_thread, NULL); |
372 |
< |
sem_destroy(&int_ack); |
373 |
< |
thread_active = false; |
374 |
< |
} |
368 |
> |
// Stop reception threads |
369 |
> |
stop_thread(); |
370 |
> |
|
371 |
> |
// Shut down TUN/TAP interface |
372 |
> |
if (net_if_type == NET_IF_TUNTAP) |
373 |
> |
execute_network_script("down"); |
374 |
> |
|
375 |
> |
// Free TUN/TAP device name |
376 |
> |
if (net_if_name) |
377 |
> |
free(net_if_name); |
378 |
|
|
379 |
|
// Close sheep_net device |
380 |
|
if (fd > 0) |
381 |
|
close(fd); |
382 |
|
|
383 |
< |
// Remove all protocols |
384 |
< |
remove_all_protocols(); |
383 |
> |
// Close slirp input buffer |
384 |
> |
if (slirp_input_fds[0] >= 0) |
385 |
> |
close(slirp_input_fds[0]); |
386 |
> |
if (slirp_input_fds[1] >= 0) |
387 |
> |
close(slirp_input_fds[1]); |
388 |
> |
|
389 |
> |
// Close slirp output buffer |
390 |
> |
if (slirp_output_fd > 0) |
391 |
> |
close(slirp_output_fd); |
392 |
|
} |
393 |
|
|
394 |
|
|
396 |
|
* Reset |
397 |
|
*/ |
398 |
|
|
399 |
< |
void EtherReset(void) |
399 |
> |
void ether_reset(void) |
400 |
|
{ |
401 |
< |
remove_all_protocols(); |
401 |
> |
net_protocols.clear(); |
402 |
|
} |
403 |
|
|
404 |
|
|
408 |
|
|
409 |
|
int16 ether_add_multicast(uint32 pb) |
410 |
|
{ |
411 |
< |
if (ioctl(fd, SIOCADDMULTI, Mac2HostAddr(pb + eMultiAddr)) < 0) { |
412 |
< |
D(bug("WARNING: Couldn't enable multicast address\n")); |
413 |
< |
if (is_ethertap) |
414 |
< |
return noErr; |
415 |
< |
else |
416 |
< |
return eMultiErr; |
417 |
< |
} else |
411 |
> |
switch (net_if_type) { |
412 |
> |
case NET_IF_ETHERTAP: |
413 |
> |
case NET_IF_SHEEPNET: |
414 |
> |
if (ioctl(fd, SIOCADDMULTI, Mac2HostAddr(pb + eMultiAddr)) < 0) { |
415 |
> |
D(bug("WARNING: Couldn't enable multicast address\n")); |
416 |
> |
if (net_if_type == NET_IF_ETHERTAP) |
417 |
> |
return noErr; |
418 |
> |
else |
419 |
> |
return eMultiErr; |
420 |
> |
} |
421 |
> |
default: |
422 |
|
return noErr; |
423 |
+ |
} |
424 |
|
} |
425 |
|
|
426 |
|
|
430 |
|
|
431 |
|
int16 ether_del_multicast(uint32 pb) |
432 |
|
{ |
433 |
< |
if (ioctl(fd, SIOCDELMULTI, Mac2HostAddr(pb + eMultiAddr)) < 0) { |
434 |
< |
D(bug("WARNING: Couldn't disable multicast address\n")); |
435 |
< |
return eMultiErr; |
436 |
< |
} else |
433 |
> |
switch (net_if_type) { |
434 |
> |
case NET_IF_ETHERTAP: |
435 |
> |
case NET_IF_SHEEPNET: |
436 |
> |
if (ioctl(fd, SIOCDELMULTI, Mac2HostAddr(pb + eMultiAddr)) < 0) { |
437 |
> |
D(bug("WARNING: Couldn't disable multicast address\n")); |
438 |
> |
return eMultiErr; |
439 |
> |
} |
440 |
> |
default: |
441 |
|
return noErr; |
442 |
+ |
} |
443 |
|
} |
444 |
|
|
445 |
|
|
449 |
|
|
450 |
|
int16 ether_attach_ph(uint16 type, uint32 handler) |
451 |
|
{ |
452 |
< |
// Already attached? |
303 |
< |
NetProtocol *p = find_protocol(type); |
304 |
< |
if (p != NULL) |
452 |
> |
if (net_protocols.find(type) != net_protocols.end()) |
453 |
|
return lapProtErr; |
454 |
< |
else { |
455 |
< |
// No, create and attach |
308 |
< |
p = new NetProtocol; |
309 |
< |
p->next = prot_list; |
310 |
< |
p->type = type; |
311 |
< |
p->handler = handler; |
312 |
< |
prot_list = p; |
313 |
< |
return noErr; |
314 |
< |
} |
454 |
> |
net_protocols[type] = handler; |
455 |
> |
return noErr; |
456 |
|
} |
457 |
|
|
458 |
|
|
462 |
|
|
463 |
|
int16 ether_detach_ph(uint16 type) |
464 |
|
{ |
465 |
< |
NetProtocol *p = find_protocol(type); |
325 |
< |
if (p != NULL) { |
326 |
< |
NetProtocol *q = prot_list; |
327 |
< |
if (p == q) { |
328 |
< |
prot_list = p->next; |
329 |
< |
delete p; |
330 |
< |
return noErr; |
331 |
< |
} |
332 |
< |
while (q) { |
333 |
< |
if (q->next == p) { |
334 |
< |
q->next = p->next; |
335 |
< |
delete p; |
336 |
< |
return noErr; |
337 |
< |
} |
338 |
< |
q = q->next; |
339 |
< |
} |
340 |
< |
return lapProtErr; |
341 |
< |
} else |
465 |
> |
if (net_protocols.erase(type) == 0) |
466 |
|
return lapProtErr; |
467 |
+ |
return noErr; |
468 |
|
} |
469 |
|
|
470 |
|
|
474 |
|
|
475 |
|
int16 ether_write(uint32 wds) |
476 |
|
{ |
352 |
– |
// Set source address |
353 |
– |
uint32 hdr = ReadMacInt32(wds + 2); |
354 |
– |
Host2Mac_memcpy(hdr + 6, ether_addr, 6); |
355 |
– |
|
477 |
|
// Copy packet to buffer |
478 |
|
uint8 packet[1516], *p = packet; |
479 |
|
int len = 0; |
480 |
|
#if defined(__linux__) |
481 |
< |
if (is_ethertap) { |
481 |
> |
if (net_if_type == NET_IF_ETHERTAP) { |
482 |
|
*p++ = 0; // Linux ethertap discards the first 2 bytes |
483 |
|
*p++ = 0; |
484 |
|
len += 2; |
495 |
|
#endif |
496 |
|
|
497 |
|
// Transmit packet |
498 |
+ |
#ifdef HAVE_SLIRP |
499 |
+ |
if (net_if_type == NET_IF_SLIRP) { |
500 |
+ |
const int slirp_input_fd = slirp_input_fds[1]; |
501 |
+ |
write(slirp_input_fd, &len, sizeof(len)); |
502 |
+ |
write(slirp_input_fd, packet, len); |
503 |
+ |
return noErr; |
504 |
+ |
} else |
505 |
+ |
#endif |
506 |
|
if (write(fd, packet, len) < 0) { |
507 |
|
D(bug("WARNING: Couldn't transmit packet\n")); |
508 |
|
return excessCollsns; |
535 |
|
|
536 |
|
|
537 |
|
/* |
538 |
+ |
* SLIRP output buffer glue |
539 |
+ |
*/ |
540 |
+ |
|
541 |
+ |
#ifdef HAVE_SLIRP |
542 |
+ |
int slirp_can_output(void) |
543 |
+ |
{ |
544 |
+ |
return 1; |
545 |
+ |
} |
546 |
+ |
|
547 |
+ |
void slirp_output(const uint8 *packet, int len) |
548 |
+ |
{ |
549 |
+ |
write(slirp_output_fd, packet, len); |
550 |
+ |
} |
551 |
+ |
|
552 |
+ |
void *slirp_receive_func(void *arg) |
553 |
+ |
{ |
554 |
+ |
const int slirp_input_fd = slirp_input_fds[0]; |
555 |
+ |
|
556 |
+ |
for (;;) { |
557 |
+ |
// Wait for packets to arrive |
558 |
+ |
fd_set rfds, wfds, xfds; |
559 |
+ |
int nfds; |
560 |
+ |
struct timeval tv; |
561 |
+ |
|
562 |
+ |
// ... in the input queue |
563 |
+ |
FD_ZERO(&rfds); |
564 |
+ |
FD_SET(slirp_input_fd, &rfds); |
565 |
+ |
tv.tv_sec = 0; |
566 |
+ |
tv.tv_usec = 0; |
567 |
+ |
if (select(slirp_input_fd + 1, &rfds, NULL, NULL, &tv) > 0) { |
568 |
+ |
int len; |
569 |
+ |
read(slirp_input_fd, &len, sizeof(len)); |
570 |
+ |
uint8 packet[1516]; |
571 |
+ |
assert(len <= sizeof(packet)); |
572 |
+ |
read(slirp_input_fd, packet, len); |
573 |
+ |
slirp_input(packet, len); |
574 |
+ |
} |
575 |
+ |
|
576 |
+ |
// ... in the output queue |
577 |
+ |
nfds = -1; |
578 |
+ |
FD_ZERO(&rfds); |
579 |
+ |
FD_ZERO(&wfds); |
580 |
+ |
FD_ZERO(&xfds); |
581 |
+ |
slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
582 |
+ |
tv.tv_sec = 0; |
583 |
+ |
tv.tv_usec = 10000; |
584 |
+ |
if (select(nfds + 1, &rfds, &wfds, &xfds, &tv) >= 0) |
585 |
+ |
slirp_select_poll(&rfds, &wfds, &xfds); |
586 |
+ |
|
587 |
+ |
#ifdef HAVE_PTHREAD_TESTCANCEL |
588 |
+ |
// Explicit cancellation point if select() was not covered |
589 |
+ |
// This seems to be the case on MacOS X 10.2 |
590 |
+ |
pthread_testcancel(); |
591 |
+ |
#endif |
592 |
+ |
} |
593 |
+ |
return NULL; |
594 |
+ |
} |
595 |
+ |
#else |
596 |
+ |
int slirp_can_output(void) |
597 |
+ |
{ |
598 |
+ |
return 0; |
599 |
+ |
} |
600 |
+ |
|
601 |
+ |
void slirp_output(const uint8 *packet, int len) |
602 |
+ |
{ |
603 |
+ |
} |
604 |
+ |
#endif |
605 |
+ |
|
606 |
+ |
|
607 |
+ |
/* |
608 |
|
* Packet reception thread |
609 |
|
*/ |
610 |
|
|
613 |
|
for (;;) { |
614 |
|
|
615 |
|
// Wait for packets to arrive |
616 |
+ |
#if HAVE_POLL |
617 |
|
struct pollfd pf = {fd, POLLIN, 0}; |
618 |
|
int res = poll(&pf, 1, -1); |
619 |
+ |
#else |
620 |
+ |
fd_set rfds; |
621 |
+ |
FD_ZERO(&rfds); |
622 |
+ |
FD_SET(fd, &rfds); |
623 |
+ |
// A NULL timeout could cause select() to block indefinitely, |
624 |
+ |
// even if it is supposed to be a cancellation point [MacOS X] |
625 |
+ |
struct timeval tv = { 0, 20000 }; |
626 |
+ |
int res = select(fd + 1, &rfds, NULL, NULL, &tv); |
627 |
+ |
#ifdef HAVE_PTHREAD_TESTCANCEL |
628 |
+ |
pthread_testcancel(); |
629 |
+ |
#endif |
630 |
+ |
if (res == 0 || (res == -1 && errno == EINTR)) |
631 |
+ |
continue; |
632 |
+ |
#endif |
633 |
|
if (res <= 0) |
634 |
|
break; |
635 |
|
|
654 |
|
D(bug("EtherIRQ\n")); |
655 |
|
|
656 |
|
// Call protocol handler for received packets |
657 |
< |
uint8 packet[1516]; |
657 |
> |
EthernetPacket ether_packet; |
658 |
> |
uint32 packet = ether_packet.addr(); |
659 |
|
ssize_t length; |
660 |
|
for (;;) { |
661 |
|
|
664 |
|
// Read packet from socket |
665 |
|
struct sockaddr_in from; |
666 |
|
socklen_t from_len = sizeof(from); |
667 |
< |
length = recvfrom(fd, packet, 1514, 0, (struct sockaddr *)&from, &from_len); |
667 |
> |
length = recvfrom(fd, Mac2HostAddr(packet), 1514, 0, (struct sockaddr *)&from, &from_len); |
668 |
|
if (length < 14) |
669 |
|
break; |
670 |
|
ether_udp_read(packet, length, &from); |
673 |
|
|
674 |
|
// Read packet from sheep_net device |
675 |
|
#if defined(__linux__) |
676 |
< |
length = read(fd, packet, is_ethertap ? 1516 : 1514); |
676 |
> |
length = read(fd, Mac2HostAddr(packet), net_if_type == NET_IF_ETHERTAP ? 1516 : 1514); |
677 |
|
#else |
678 |
< |
length = read(fd, packet, 1514); |
678 |
> |
length = read(fd, Mac2HostAddr(packet), 1514); |
679 |
|
#endif |
680 |
|
if (length < 14) |
681 |
|
break; |
683 |
|
#if MONITOR |
684 |
|
bug("Receiving Ethernet packet:\n"); |
685 |
|
for (int i=0; i<length; i++) { |
686 |
< |
bug("%02x ", packet[i]); |
686 |
> |
bug("%02x ", ReadMacInt8(packet + i)); |
687 |
|
} |
688 |
|
bug("\n"); |
689 |
|
#endif |
690 |
|
|
691 |
|
// Pointer to packet data (Ethernet header) |
692 |
< |
uint8 *p = packet; |
692 |
> |
uint32 p = packet; |
693 |
|
#if defined(__linux__) |
694 |
< |
if (is_ethertap) { |
694 |
> |
if (net_if_type == NET_IF_ETHERTAP) { |
695 |
|
p += 2; // Linux ethertap has two random bytes before the packet |
696 |
|
length -= 2; |
697 |
|
} |
698 |
|
#endif |
699 |
|
|
700 |
|
// Get packet type |
701 |
< |
uint16 type = (p[12] << 8) | p[13]; |
701 |
> |
uint16 type = ReadMacInt16(p + 12); |
702 |
|
|
703 |
|
// Look for protocol |
704 |
< |
NetProtocol *prot = find_protocol(type); |
705 |
< |
if (prot == NULL) |
704 |
> |
uint16 search_type = (type <= 1500 ? 0 : type); |
705 |
> |
if (net_protocols.find(search_type) == net_protocols.end()) |
706 |
|
continue; |
707 |
+ |
uint32 handler = net_protocols[search_type]; |
708 |
|
|
709 |
|
// No default handler |
710 |
< |
if (prot->handler == 0) |
710 |
> |
if (handler == 0) |
711 |
|
continue; |
712 |
|
|
713 |
|
// Copy header to RHA |
714 |
< |
Host2Mac_memcpy(ether_data + ed_RHA, p, 14); |
714 |
> |
Mac2Mac_memcpy(ether_data + ed_RHA, p, 14); |
715 |
|
D(bug(" header %08x%04x %08x%04x %04x\n", ReadMacInt32(ether_data + ed_RHA), ReadMacInt16(ether_data + ed_RHA + 4), ReadMacInt32(ether_data + ed_RHA + 6), ReadMacInt16(ether_data + ed_RHA + 10), ReadMacInt16(ether_data + ed_RHA + 12))); |
716 |
|
|
717 |
|
// Call protocol handler |
718 |
|
M68kRegisters r; |
719 |
|
r.d[0] = type; // Packet type |
720 |
|
r.d[1] = length - 14; // Remaining packet length (without header, for ReadPacket) |
721 |
< |
r.a[0] = (uint32)p + 14; // Pointer to packet (host address, for ReadPacket) |
721 |
> |
r.a[0] = p + 14; // Pointer to packet (Mac address, for ReadPacket) |
722 |
|
r.a[3] = ether_data + ed_RHA + 14; // Pointer behind header in RHA |
723 |
|
r.a[4] = ether_data + ed_ReadPacket; // Pointer to ReadPacket/ReadRest routines |
724 |
< |
D(bug(" calling protocol handler %08x, type %08x, length %08x, data %08x, rha %08x, read_packet %08x\n", prot->handler, r.d[0], r.d[1], r.a[0], r.a[3], r.a[4])); |
725 |
< |
Execute68k(prot->handler, &r); |
724 |
> |
D(bug(" calling protocol handler %08x, type %08x, length %08x, data %08x, rha %08x, read_packet %08x\n", handler, r.d[0], r.d[1], r.a[0], r.a[3], r.a[4])); |
725 |
> |
Execute68k(handler, &r); |
726 |
|
} |
727 |
|
} |
728 |
|
|