1 |
cebix |
1.1 |
/* |
2 |
|
|
* ether_unix.cpp - Ethernet device driver, Unix specific stuff (Linux and FreeBSD) |
3 |
|
|
* |
4 |
gbeauche |
1.13 |
* Basilisk II (C) 1997-2005 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 |
|
|
#include "sysdeps.h" |
22 |
|
|
|
23 |
gbeauche |
1.25 |
/* |
24 |
|
|
* NOTES concerning MacOS X issues: |
25 |
|
|
* - poll() does not exist in 10.2.8, but is available in 10.4.4 |
26 |
|
|
* - select(), and very likely poll(), are not cancellation points. So |
27 |
|
|
* the ethernet thread doesn't stop on exit. An explicit check is |
28 |
|
|
* performed to workaround this problem. |
29 |
|
|
*/ |
30 |
|
|
#if (defined __APPLE__ && defined __MACH__) || ! defined HAVE_POLL |
31 |
|
|
#define USE_POLL 0 |
32 |
|
|
#else |
33 |
|
|
#define USE_POLL 1 |
34 |
|
|
#endif |
35 |
|
|
|
36 |
gbeauche |
1.17 |
#ifdef HAVE_SYS_POLL_H |
37 |
|
|
#include <sys/poll.h> |
38 |
|
|
#endif |
39 |
cebix |
1.1 |
#include <sys/ioctl.h> |
40 |
cebix |
1.3 |
#include <sys/socket.h> |
41 |
gbeauche |
1.9 |
#include <sys/wait.h> |
42 |
cebix |
1.3 |
#include <netinet/in.h> |
43 |
cebix |
1.1 |
#include <pthread.h> |
44 |
|
|
#include <semaphore.h> |
45 |
|
|
#include <errno.h> |
46 |
|
|
#include <stdio.h> |
47 |
cebix |
1.3 |
#include <map> |
48 |
cebix |
1.2 |
|
49 |
gbeauche |
1.17 |
#if defined(__FreeBSD__) || defined(sgi) || (defined(__APPLE__) && defined(__MACH__)) |
50 |
cebix |
1.1 |
#include <net/if.h> |
51 |
|
|
#endif |
52 |
|
|
|
53 |
gbeauche |
1.9 |
#if defined(HAVE_LINUX_IF_H) && defined(HAVE_LINUX_IF_TUN_H) |
54 |
|
|
#include <linux/if.h> |
55 |
|
|
#include <linux/if_tun.h> |
56 |
|
|
#endif |
57 |
|
|
|
58 |
|
|
#if defined(HAVE_NET_IF_H) && defined(HAVE_NET_IF_TUN_H) |
59 |
|
|
#include <net/if.h> |
60 |
|
|
#include <net/if_tun.h> |
61 |
|
|
#endif |
62 |
|
|
|
63 |
gbeauche |
1.18 |
#ifdef HAVE_SLIRP |
64 |
gbeauche |
1.16 |
#include "libslirp.h" |
65 |
gbeauche |
1.18 |
#endif |
66 |
gbeauche |
1.15 |
|
67 |
cebix |
1.1 |
#include "cpu_emulation.h" |
68 |
|
|
#include "main.h" |
69 |
|
|
#include "macos_util.h" |
70 |
|
|
#include "prefs.h" |
71 |
|
|
#include "user_strings.h" |
72 |
|
|
#include "ether.h" |
73 |
|
|
#include "ether_defs.h" |
74 |
|
|
|
75 |
cebix |
1.3 |
#ifndef NO_STD_NAMESPACE |
76 |
|
|
using std::map; |
77 |
|
|
#endif |
78 |
|
|
|
79 |
cebix |
1.1 |
#define DEBUG 0 |
80 |
|
|
#include "debug.h" |
81 |
|
|
|
82 |
gbeauche |
1.23 |
#define STATISTICS 0 |
83 |
cebix |
1.1 |
#define MONITOR 0 |
84 |
|
|
|
85 |
|
|
|
86 |
gbeauche |
1.9 |
// Ethernet device types |
87 |
|
|
enum { |
88 |
|
|
NET_IF_SHEEPNET, |
89 |
|
|
NET_IF_ETHERTAP, |
90 |
|
|
NET_IF_TUNTAP, |
91 |
gbeauche |
1.15 |
NET_IF_SLIRP |
92 |
gbeauche |
1.9 |
}; |
93 |
|
|
|
94 |
|
|
// Constants |
95 |
|
|
static const char ETHERCONFIG_FILE_NAME[] = DATADIR "/tunconfig"; |
96 |
|
|
|
97 |
cebix |
1.1 |
// Global variables |
98 |
|
|
static int fd = -1; // fd of sheep_net device |
99 |
|
|
static pthread_t ether_thread; // Packet reception thread |
100 |
|
|
static pthread_attr_t ether_thread_attr; // Packet reception thread attributes |
101 |
|
|
static bool thread_active = false; // Flag: Packet reception thread installed |
102 |
|
|
static sem_t int_ack; // Interrupt acknowledge semaphore |
103 |
cebix |
1.2 |
static bool udp_tunnel; // Flag: UDP tunnelling active, fd is the socket descriptor |
104 |
gbeauche |
1.9 |
static int net_if_type = -1; // Ethernet device type |
105 |
gbeauche |
1.10 |
static char *net_if_name = NULL; // TUN/TAP device name |
106 |
gbeauche |
1.9 |
static const char *net_if_script = NULL; // Network config script |
107 |
gbeauche |
1.15 |
static pthread_t slirp_thread; // Slirp reception thread |
108 |
|
|
static bool slirp_thread_active = false; // Flag: Slirp reception threadinstalled |
109 |
|
|
static int slirp_output_fd = -1; // fd of slirp output pipe |
110 |
gbeauche |
1.21 |
static int slirp_input_fds[2] = { -1, -1 }; // fds of slirp input pipe |
111 |
gbeauche |
1.23 |
#ifdef SHEEPSHAVER |
112 |
|
|
static bool net_open = false; // Flag: initialization succeeded, network device open |
113 |
|
|
static uint8 ether_addr[6]; // Our Ethernet address |
114 |
|
|
#else |
115 |
|
|
const bool ether_driver_opened = true; // Flag: is the MacOS driver opened? |
116 |
|
|
#endif |
117 |
cebix |
1.1 |
|
118 |
cebix |
1.3 |
// Attached network protocols, maps protocol type to MacOS handler address |
119 |
|
|
static map<uint16, uint32> net_protocols; |
120 |
|
|
|
121 |
cebix |
1.1 |
// Prototypes |
122 |
|
|
static void *receive_func(void *arg); |
123 |
gbeauche |
1.15 |
static void *slirp_receive_func(void *arg); |
124 |
gbeauche |
1.17 |
static int poll_fd(int fd); |
125 |
gbeauche |
1.23 |
static int16 ether_do_add_multicast(uint8 *addr); |
126 |
|
|
static int16 ether_do_del_multicast(uint8 *addr); |
127 |
|
|
static int16 ether_do_write(uint32 arg); |
128 |
|
|
static void ether_do_interrupt(void); |
129 |
cebix |
1.1 |
|
130 |
|
|
|
131 |
|
|
/* |
132 |
cebix |
1.2 |
* Start packet reception thread |
133 |
|
|
*/ |
134 |
|
|
|
135 |
|
|
static bool start_thread(void) |
136 |
|
|
{ |
137 |
|
|
if (sem_init(&int_ack, 0, 0) < 0) { |
138 |
|
|
printf("WARNING: Cannot init semaphore"); |
139 |
|
|
return false; |
140 |
|
|
} |
141 |
|
|
|
142 |
cebix |
1.7 |
Set_pthread_attr(ðer_thread_attr, 1); |
143 |
cebix |
1.2 |
thread_active = (pthread_create(ðer_thread, ðer_thread_attr, receive_func, NULL) == 0); |
144 |
|
|
if (!thread_active) { |
145 |
|
|
printf("WARNING: Cannot start Ethernet thread"); |
146 |
|
|
return false; |
147 |
|
|
} |
148 |
|
|
|
149 |
gbeauche |
1.15 |
#ifdef HAVE_SLIRP |
150 |
|
|
if (net_if_type == NET_IF_SLIRP) { |
151 |
|
|
slirp_thread_active = (pthread_create(&slirp_thread, NULL, slirp_receive_func, NULL) == 0); |
152 |
|
|
if (!slirp_thread_active) { |
153 |
|
|
printf("WARNING: Cannot start slirp reception thread\n"); |
154 |
|
|
return false; |
155 |
|
|
} |
156 |
|
|
} |
157 |
|
|
#endif |
158 |
|
|
|
159 |
cebix |
1.2 |
return true; |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
|
163 |
|
|
/* |
164 |
|
|
* Stop packet reception thread |
165 |
|
|
*/ |
166 |
|
|
|
167 |
|
|
static void stop_thread(void) |
168 |
|
|
{ |
169 |
gbeauche |
1.15 |
#ifdef HAVE_SLIRP |
170 |
|
|
if (slirp_thread_active) { |
171 |
gbeauche |
1.20 |
#ifdef HAVE_PTHREAD_CANCEL |
172 |
gbeauche |
1.15 |
pthread_cancel(slirp_thread); |
173 |
gbeauche |
1.20 |
#endif |
174 |
gbeauche |
1.15 |
pthread_join(slirp_thread, NULL); |
175 |
|
|
slirp_thread_active = false; |
176 |
|
|
} |
177 |
|
|
#endif |
178 |
|
|
|
179 |
cebix |
1.2 |
if (thread_active) { |
180 |
gbeauche |
1.20 |
#ifdef HAVE_PTHREAD_CANCEL |
181 |
cebix |
1.2 |
pthread_cancel(ether_thread); |
182 |
gbeauche |
1.20 |
#endif |
183 |
nigel |
1.24 |
#ifdef AQUA |
184 |
|
|
// This call, which waits for ether_thread to terminate, |
185 |
|
|
// never returns when used in Nigel's OS X port. |
186 |
|
|
#else |
187 |
cebix |
1.2 |
pthread_join(ether_thread, NULL); |
188 |
nigel |
1.24 |
#endif |
189 |
cebix |
1.2 |
sem_destroy(&int_ack); |
190 |
|
|
thread_active = false; |
191 |
|
|
} |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
|
195 |
|
|
/* |
196 |
gbeauche |
1.9 |
* Execute network script up|down |
197 |
|
|
*/ |
198 |
|
|
|
199 |
|
|
static bool execute_network_script(const char *action) |
200 |
|
|
{ |
201 |
|
|
if (net_if_script == NULL || net_if_name == NULL) |
202 |
|
|
return false; |
203 |
|
|
|
204 |
|
|
int pid = fork(); |
205 |
|
|
if (pid >= 0) { |
206 |
|
|
if (pid == 0) { |
207 |
|
|
char *args[4]; |
208 |
|
|
args[0] = (char *)net_if_script; |
209 |
gbeauche |
1.10 |
args[1] = net_if_name; |
210 |
gbeauche |
1.9 |
args[2] = (char *)action; |
211 |
|
|
args[3] = NULL; |
212 |
|
|
execv(net_if_script, args); |
213 |
|
|
exit(1); |
214 |
|
|
} |
215 |
|
|
int status; |
216 |
|
|
while (waitpid(pid, &status, 0) != pid); |
217 |
|
|
return WIFEXITED(status) && WEXITSTATUS(status) == 0; |
218 |
|
|
} |
219 |
|
|
|
220 |
|
|
return false; |
221 |
|
|
} |
222 |
|
|
|
223 |
|
|
|
224 |
|
|
/* |
225 |
cebix |
1.1 |
* Initialization |
226 |
|
|
*/ |
227 |
|
|
|
228 |
cebix |
1.2 |
bool ether_init(void) |
229 |
cebix |
1.1 |
{ |
230 |
|
|
int nonblock = 1; |
231 |
|
|
char str[256]; |
232 |
|
|
|
233 |
|
|
// Do nothing if no Ethernet device specified |
234 |
|
|
const char *name = PrefsFindString("ether"); |
235 |
|
|
if (name == NULL) |
236 |
cebix |
1.2 |
return false; |
237 |
cebix |
1.1 |
|
238 |
gbeauche |
1.9 |
// Determine Ethernet device type |
239 |
|
|
net_if_type = -1; |
240 |
|
|
if (strncmp(name, "tap", 3) == 0) |
241 |
|
|
net_if_type = NET_IF_ETHERTAP; |
242 |
|
|
#if ENABLE_TUNTAP |
243 |
|
|
else if (strcmp(name, "tun") == 0) |
244 |
|
|
net_if_type = NET_IF_TUNTAP; |
245 |
|
|
#endif |
246 |
gbeauche |
1.15 |
#ifdef HAVE_SLIRP |
247 |
|
|
else if (strcmp(name, "slirp") == 0) |
248 |
|
|
net_if_type = NET_IF_SLIRP; |
249 |
|
|
#endif |
250 |
gbeauche |
1.9 |
else |
251 |
|
|
net_if_type = NET_IF_SHEEPNET; |
252 |
cebix |
1.1 |
|
253 |
gbeauche |
1.15 |
#ifdef HAVE_SLIRP |
254 |
|
|
// Initialize slirp library |
255 |
|
|
if (net_if_type == NET_IF_SLIRP) { |
256 |
gbeauche |
1.22 |
if (slirp_init() < 0) { |
257 |
|
|
sprintf(str, GetString(STR_SLIRP_NO_DNS_FOUND_WARN)); |
258 |
|
|
WarningAlert(str); |
259 |
|
|
return false; |
260 |
|
|
} |
261 |
gbeauche |
1.15 |
|
262 |
|
|
// Open slirp output pipe |
263 |
|
|
int fds[2]; |
264 |
|
|
if (pipe(fds) < 0) |
265 |
|
|
return false; |
266 |
|
|
fd = fds[0]; |
267 |
|
|
slirp_output_fd = fds[1]; |
268 |
gbeauche |
1.21 |
|
269 |
|
|
// Open slirp input pipe |
270 |
|
|
if (pipe(slirp_input_fds) < 0) |
271 |
|
|
return false; |
272 |
gbeauche |
1.15 |
} |
273 |
|
|
#endif |
274 |
|
|
|
275 |
gbeauche |
1.11 |
// Open sheep_net or ethertap or TUN/TAP device |
276 |
cebix |
1.1 |
char dev_name[16]; |
277 |
gbeauche |
1.9 |
switch (net_if_type) { |
278 |
|
|
case NET_IF_ETHERTAP: |
279 |
cebix |
1.1 |
sprintf(dev_name, "/dev/%s", name); |
280 |
gbeauche |
1.9 |
break; |
281 |
|
|
case NET_IF_TUNTAP: |
282 |
gbeauche |
1.11 |
strcpy(dev_name, "/dev/net/tun"); |
283 |
gbeauche |
1.9 |
break; |
284 |
|
|
case NET_IF_SHEEPNET: |
285 |
cebix |
1.1 |
strcpy(dev_name, "/dev/sheep_net"); |
286 |
gbeauche |
1.9 |
break; |
287 |
|
|
} |
288 |
gbeauche |
1.15 |
if (net_if_type != NET_IF_SLIRP) { |
289 |
|
|
fd = open(dev_name, O_RDWR); |
290 |
|
|
if (fd < 0) { |
291 |
|
|
sprintf(str, GetString(STR_NO_SHEEP_NET_DRIVER_WARN), dev_name, strerror(errno)); |
292 |
|
|
WarningAlert(str); |
293 |
|
|
goto open_error; |
294 |
|
|
} |
295 |
cebix |
1.1 |
} |
296 |
|
|
|
297 |
gbeauche |
1.9 |
#if ENABLE_TUNTAP |
298 |
|
|
// Open TUN/TAP interface |
299 |
|
|
if (net_if_type == NET_IF_TUNTAP) { |
300 |
|
|
struct ifreq ifr; |
301 |
|
|
memset(&ifr, 0, sizeof(ifr)); |
302 |
|
|
ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
303 |
|
|
strcpy(ifr.ifr_name, "tun%d"); |
304 |
|
|
if (ioctl(fd, TUNSETIFF, (void *) &ifr) != 0) { |
305 |
|
|
sprintf(str, GetString(STR_SHEEP_NET_ATTACH_WARN), strerror(errno)); |
306 |
|
|
WarningAlert(str); |
307 |
|
|
goto open_error; |
308 |
|
|
} |
309 |
|
|
|
310 |
|
|
// Get network config script file path |
311 |
|
|
net_if_script = PrefsFindString("etherconfig"); |
312 |
|
|
if (net_if_script == NULL) |
313 |
|
|
net_if_script = ETHERCONFIG_FILE_NAME; |
314 |
|
|
|
315 |
|
|
// Start network script up |
316 |
|
|
if (net_if_script == NULL) { |
317 |
|
|
sprintf(str, GetString(STR_TUN_TAP_CONFIG_WARN), "script not found"); |
318 |
|
|
WarningAlert(str); |
319 |
|
|
goto open_error; |
320 |
|
|
} |
321 |
gbeauche |
1.10 |
net_if_name = strdup(ifr.ifr_name); |
322 |
gbeauche |
1.9 |
if (!execute_network_script("up")) { |
323 |
|
|
sprintf(str, GetString(STR_TUN_TAP_CONFIG_WARN), "script execute error"); |
324 |
|
|
WarningAlert(str); |
325 |
|
|
goto open_error; |
326 |
|
|
} |
327 |
|
|
D(bug("Connected to host network interface: %s\n", net_if_name)); |
328 |
|
|
} |
329 |
|
|
#endif |
330 |
|
|
|
331 |
cebix |
1.1 |
#if defined(__linux__) |
332 |
|
|
// Attach sheep_net to selected Ethernet card |
333 |
gbeauche |
1.9 |
if (net_if_type == NET_IF_SHEEPNET && ioctl(fd, SIOCSIFLINK, name) < 0) { |
334 |
cebix |
1.1 |
sprintf(str, GetString(STR_SHEEP_NET_ATTACH_WARN), strerror(errno)); |
335 |
|
|
WarningAlert(str); |
336 |
|
|
goto open_error; |
337 |
|
|
} |
338 |
|
|
#endif |
339 |
|
|
|
340 |
|
|
// Set nonblocking I/O |
341 |
|
|
ioctl(fd, FIONBIO, &nonblock); |
342 |
|
|
|
343 |
|
|
// Get Ethernet address |
344 |
gbeauche |
1.9 |
if (net_if_type == NET_IF_ETHERTAP) { |
345 |
cebix |
1.1 |
pid_t p = getpid(); // If configured for multicast, ethertap requires that the lower 32 bit of the Ethernet address are our PID |
346 |
|
|
ether_addr[0] = 0xfe; |
347 |
|
|
ether_addr[1] = 0xfd; |
348 |
|
|
ether_addr[2] = p >> 24; |
349 |
|
|
ether_addr[3] = p >> 16; |
350 |
|
|
ether_addr[4] = p >> 8; |
351 |
|
|
ether_addr[5] = p; |
352 |
gbeauche |
1.15 |
#ifdef HAVE_SLIRP |
353 |
|
|
} else if (net_if_type == NET_IF_SLIRP) { |
354 |
|
|
ether_addr[0] = 0x52; |
355 |
|
|
ether_addr[1] = 0x54; |
356 |
|
|
ether_addr[2] = 0x00; |
357 |
|
|
ether_addr[3] = 0x12; |
358 |
|
|
ether_addr[4] = 0x34; |
359 |
|
|
ether_addr[5] = 0x56; |
360 |
|
|
#endif |
361 |
cebix |
1.1 |
} else |
362 |
|
|
ioctl(fd, SIOCGIFADDR, ether_addr); |
363 |
|
|
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])); |
364 |
|
|
|
365 |
|
|
// Start packet reception thread |
366 |
cebix |
1.2 |
if (!start_thread()) |
367 |
cebix |
1.1 |
goto open_error; |
368 |
|
|
|
369 |
|
|
// Everything OK |
370 |
cebix |
1.2 |
return true; |
371 |
cebix |
1.1 |
|
372 |
|
|
open_error: |
373 |
cebix |
1.2 |
stop_thread(); |
374 |
|
|
|
375 |
cebix |
1.1 |
if (fd > 0) { |
376 |
|
|
close(fd); |
377 |
|
|
fd = -1; |
378 |
|
|
} |
379 |
gbeauche |
1.21 |
if (slirp_input_fds[0] >= 0) { |
380 |
|
|
close(slirp_input_fds[0]); |
381 |
|
|
slirp_input_fds[0] = -1; |
382 |
|
|
} |
383 |
|
|
if (slirp_input_fds[1] >= 0) { |
384 |
|
|
close(slirp_input_fds[1]); |
385 |
|
|
slirp_input_fds[1] = -1; |
386 |
|
|
} |
387 |
gbeauche |
1.15 |
if (slirp_output_fd >= 0) { |
388 |
|
|
close(slirp_output_fd); |
389 |
|
|
slirp_output_fd = -1; |
390 |
|
|
} |
391 |
cebix |
1.2 |
return false; |
392 |
cebix |
1.1 |
} |
393 |
|
|
|
394 |
|
|
|
395 |
|
|
/* |
396 |
|
|
* Deinitialization |
397 |
|
|
*/ |
398 |
|
|
|
399 |
cebix |
1.2 |
void ether_exit(void) |
400 |
cebix |
1.1 |
{ |
401 |
gbeauche |
1.20 |
// Stop reception threads |
402 |
|
|
stop_thread(); |
403 |
cebix |
1.1 |
|
404 |
gbeauche |
1.10 |
// Shut down TUN/TAP interface |
405 |
|
|
if (net_if_type == NET_IF_TUNTAP) |
406 |
|
|
execute_network_script("down"); |
407 |
|
|
|
408 |
|
|
// Free TUN/TAP device name |
409 |
|
|
if (net_if_name) |
410 |
|
|
free(net_if_name); |
411 |
|
|
|
412 |
cebix |
1.1 |
// Close sheep_net device |
413 |
|
|
if (fd > 0) |
414 |
|
|
close(fd); |
415 |
gbeauche |
1.15 |
|
416 |
gbeauche |
1.21 |
// Close slirp input buffer |
417 |
|
|
if (slirp_input_fds[0] >= 0) |
418 |
|
|
close(slirp_input_fds[0]); |
419 |
|
|
if (slirp_input_fds[1] >= 0) |
420 |
|
|
close(slirp_input_fds[1]); |
421 |
|
|
|
422 |
gbeauche |
1.15 |
// Close slirp output buffer |
423 |
|
|
if (slirp_output_fd > 0) |
424 |
|
|
close(slirp_output_fd); |
425 |
gbeauche |
1.23 |
|
426 |
|
|
#if STATISTICS |
427 |
|
|
// Show statistics |
428 |
|
|
printf("%ld messages put on write queue\n", num_wput); |
429 |
|
|
printf("%ld error acks\n", num_error_acks); |
430 |
|
|
printf("%ld packets transmitted (%ld raw, %ld normal)\n", num_tx_packets, num_tx_raw_packets, num_tx_normal_packets); |
431 |
|
|
printf("%ld tx packets dropped because buffer full\n", num_tx_buffer_full); |
432 |
|
|
printf("%ld packets received\n", num_rx_packets); |
433 |
|
|
printf("%ld packets passed upstream (%ld Fast Path, %ld normal)\n", num_rx_fastpath + num_unitdata_ind, num_rx_fastpath, num_unitdata_ind); |
434 |
|
|
printf("EtherIRQ called %ld times\n", num_ether_irq); |
435 |
|
|
printf("%ld rx packets dropped due to low memory\n", num_rx_no_mem); |
436 |
|
|
printf("%ld rx packets dropped because no stream found\n", num_rx_dropped); |
437 |
|
|
printf("%ld rx packets dropped because stream not ready\n", num_rx_stream_not_ready); |
438 |
|
|
printf("%ld rx packets dropped because no memory for unitdata_ind\n", num_rx_no_unitdata_mem); |
439 |
|
|
#endif |
440 |
cebix |
1.1 |
} |
441 |
|
|
|
442 |
|
|
|
443 |
|
|
/* |
444 |
gbeauche |
1.23 |
* Glue around low-level implementation |
445 |
|
|
*/ |
446 |
|
|
|
447 |
|
|
#ifdef SHEEPSHAVER |
448 |
|
|
// Error codes |
449 |
|
|
enum { |
450 |
|
|
eMultiErr = -91, |
451 |
|
|
eLenErr = -92, |
452 |
|
|
lapProtErr = -94, |
453 |
|
|
excessCollsns = -95 |
454 |
|
|
}; |
455 |
|
|
|
456 |
|
|
// Initialize ethernet |
457 |
|
|
void EtherInit(void) |
458 |
|
|
{ |
459 |
|
|
net_open = false; |
460 |
|
|
|
461 |
|
|
// Do nothing if the user disabled the network |
462 |
|
|
if (PrefsFindBool("nonet")) |
463 |
|
|
return; |
464 |
|
|
|
465 |
|
|
net_open = ether_init(); |
466 |
|
|
} |
467 |
|
|
|
468 |
|
|
// Exit ethernet |
469 |
|
|
void EtherExit(void) |
470 |
|
|
{ |
471 |
|
|
ether_exit(); |
472 |
|
|
net_open = false; |
473 |
|
|
} |
474 |
|
|
|
475 |
|
|
// Get ethernet hardware address |
476 |
|
|
void AO_get_ethernet_address(uint32 arg) |
477 |
|
|
{ |
478 |
|
|
uint8 *addr = Mac2HostAddr(arg); |
479 |
|
|
if (net_open) |
480 |
|
|
OTCopy48BitAddress(ether_addr, addr); |
481 |
|
|
else { |
482 |
|
|
addr[0] = 0x12; |
483 |
|
|
addr[1] = 0x34; |
484 |
|
|
addr[2] = 0x56; |
485 |
|
|
addr[3] = 0x78; |
486 |
|
|
addr[4] = 0x9a; |
487 |
|
|
addr[5] = 0xbc; |
488 |
|
|
} |
489 |
|
|
D(bug("AO_get_ethernet_address: got address %02x%02x%02x%02x%02x%02x\n", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5])); |
490 |
|
|
} |
491 |
|
|
|
492 |
|
|
// Add multicast address |
493 |
|
|
void AO_enable_multicast(uint32 addr) |
494 |
|
|
{ |
495 |
|
|
if (net_open) |
496 |
|
|
ether_do_add_multicast(Mac2HostAddr(addr)); |
497 |
|
|
} |
498 |
|
|
|
499 |
|
|
// Disable multicast address |
500 |
|
|
void AO_disable_multicast(uint32 addr) |
501 |
|
|
{ |
502 |
|
|
if (net_open) |
503 |
|
|
ether_do_del_multicast(Mac2HostAddr(addr)); |
504 |
|
|
} |
505 |
|
|
|
506 |
|
|
// Transmit one packet |
507 |
|
|
void AO_transmit_packet(uint32 mp) |
508 |
|
|
{ |
509 |
|
|
if (net_open) { |
510 |
|
|
switch (ether_do_write(mp)) { |
511 |
|
|
case noErr: |
512 |
|
|
num_tx_packets++; |
513 |
|
|
break; |
514 |
|
|
case excessCollsns: |
515 |
|
|
num_tx_buffer_full++; |
516 |
|
|
break; |
517 |
|
|
} |
518 |
|
|
} |
519 |
|
|
} |
520 |
|
|
|
521 |
|
|
// Copy packet data from message block to linear buffer |
522 |
|
|
static inline int ether_arg_to_buffer(uint32 mp, uint8 *p) |
523 |
|
|
{ |
524 |
|
|
return ether_msgb_to_buffer(mp, p); |
525 |
|
|
} |
526 |
|
|
|
527 |
|
|
// Ethernet interrupt |
528 |
|
|
void EtherIRQ(void) |
529 |
|
|
{ |
530 |
|
|
D(bug("EtherIRQ\n")); |
531 |
|
|
num_ether_irq++; |
532 |
|
|
|
533 |
|
|
OTEnterInterrupt(); |
534 |
|
|
ether_do_interrupt(); |
535 |
|
|
OTLeaveInterrupt(); |
536 |
|
|
|
537 |
|
|
// Acknowledge interrupt to reception thread |
538 |
|
|
D(bug(" EtherIRQ done\n")); |
539 |
|
|
sem_post(&int_ack); |
540 |
|
|
} |
541 |
|
|
#else |
542 |
|
|
// Add multicast address |
543 |
|
|
int16 ether_add_multicast(uint32 pb) |
544 |
|
|
{ |
545 |
|
|
return ether_do_add_multicast(Mac2HostAddr(pb + eMultiAddr)); |
546 |
|
|
} |
547 |
|
|
|
548 |
|
|
// Disable multicast address |
549 |
|
|
int16 ether_del_multicast(uint32 pb) |
550 |
|
|
{ |
551 |
|
|
return ether_do_del_multicast(Mac2HostAddr(pb + eMultiAddr)); |
552 |
|
|
} |
553 |
|
|
|
554 |
|
|
// Transmit one packet |
555 |
|
|
int16 ether_write(uint32 wds) |
556 |
|
|
{ |
557 |
|
|
return ether_do_write(wds); |
558 |
|
|
} |
559 |
|
|
|
560 |
|
|
// Copy packet data from WDS to linear buffer |
561 |
|
|
static inline int ether_arg_to_buffer(uint32 wds, uint8 *p) |
562 |
|
|
{ |
563 |
|
|
return ether_wds_to_buffer(wds, p); |
564 |
|
|
} |
565 |
|
|
|
566 |
|
|
// Dispatch packet to protocol handler |
567 |
|
|
static void ether_dispatch_packet(uint32 p, uint32 length) |
568 |
|
|
{ |
569 |
|
|
// Get packet type |
570 |
|
|
uint16 type = ReadMacInt16(p + 12); |
571 |
|
|
|
572 |
|
|
// Look for protocol |
573 |
|
|
uint16 search_type = (type <= 1500 ? 0 : type); |
574 |
|
|
if (net_protocols.find(search_type) == net_protocols.end()) |
575 |
|
|
return; |
576 |
|
|
uint32 handler = net_protocols[search_type]; |
577 |
|
|
|
578 |
|
|
// No default handler |
579 |
|
|
if (handler == 0) |
580 |
|
|
return; |
581 |
|
|
|
582 |
|
|
// Copy header to RHA |
583 |
|
|
Mac2Mac_memcpy(ether_data + ed_RHA, p, 14); |
584 |
|
|
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))); |
585 |
|
|
|
586 |
|
|
// Call protocol handler |
587 |
|
|
M68kRegisters r; |
588 |
|
|
r.d[0] = type; // Packet type |
589 |
|
|
r.d[1] = length - 14; // Remaining packet length (without header, for ReadPacket) |
590 |
|
|
r.a[0] = p + 14; // Pointer to packet (Mac address, for ReadPacket) |
591 |
|
|
r.a[3] = ether_data + ed_RHA + 14; // Pointer behind header in RHA |
592 |
|
|
r.a[4] = ether_data + ed_ReadPacket; // Pointer to ReadPacket/ReadRest routines |
593 |
|
|
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])); |
594 |
|
|
Execute68k(handler, &r); |
595 |
|
|
} |
596 |
|
|
|
597 |
|
|
// Ethernet interrupt |
598 |
|
|
void EtherInterrupt(void) |
599 |
|
|
{ |
600 |
|
|
D(bug("EtherIRQ\n")); |
601 |
|
|
ether_do_interrupt(); |
602 |
|
|
|
603 |
|
|
// Acknowledge interrupt to reception thread |
604 |
|
|
D(bug(" EtherIRQ done\n")); |
605 |
|
|
sem_post(&int_ack); |
606 |
|
|
} |
607 |
|
|
#endif |
608 |
|
|
|
609 |
|
|
|
610 |
|
|
/* |
611 |
cebix |
1.1 |
* Reset |
612 |
|
|
*/ |
613 |
|
|
|
614 |
cebix |
1.3 |
void ether_reset(void) |
615 |
cebix |
1.1 |
{ |
616 |
cebix |
1.3 |
net_protocols.clear(); |
617 |
cebix |
1.1 |
} |
618 |
|
|
|
619 |
|
|
|
620 |
|
|
/* |
621 |
|
|
* Add multicast address |
622 |
|
|
*/ |
623 |
|
|
|
624 |
gbeauche |
1.23 |
static int16 ether_do_add_multicast(uint8 *addr) |
625 |
cebix |
1.1 |
{ |
626 |
gbeauche |
1.15 |
switch (net_if_type) { |
627 |
|
|
case NET_IF_ETHERTAP: |
628 |
|
|
case NET_IF_SHEEPNET: |
629 |
gbeauche |
1.23 |
if (ioctl(fd, SIOCADDMULTI, addr) < 0) { |
630 |
gbeauche |
1.15 |
D(bug("WARNING: Couldn't enable multicast address\n")); |
631 |
|
|
if (net_if_type == NET_IF_ETHERTAP) |
632 |
|
|
return noErr; |
633 |
|
|
else |
634 |
|
|
return eMultiErr; |
635 |
|
|
} |
636 |
|
|
default: |
637 |
cebix |
1.1 |
return noErr; |
638 |
gbeauche |
1.15 |
} |
639 |
cebix |
1.1 |
} |
640 |
|
|
|
641 |
|
|
|
642 |
|
|
/* |
643 |
|
|
* Delete multicast address |
644 |
|
|
*/ |
645 |
|
|
|
646 |
gbeauche |
1.23 |
static int16 ether_do_del_multicast(uint8 *addr) |
647 |
cebix |
1.1 |
{ |
648 |
gbeauche |
1.15 |
switch (net_if_type) { |
649 |
|
|
case NET_IF_ETHERTAP: |
650 |
|
|
case NET_IF_SHEEPNET: |
651 |
gbeauche |
1.23 |
if (ioctl(fd, SIOCDELMULTI, addr) < 0) { |
652 |
gbeauche |
1.15 |
D(bug("WARNING: Couldn't disable multicast address\n")); |
653 |
|
|
return eMultiErr; |
654 |
|
|
} |
655 |
|
|
default: |
656 |
cebix |
1.1 |
return noErr; |
657 |
gbeauche |
1.15 |
} |
658 |
cebix |
1.1 |
} |
659 |
|
|
|
660 |
|
|
|
661 |
|
|
/* |
662 |
|
|
* Attach protocol handler |
663 |
|
|
*/ |
664 |
|
|
|
665 |
|
|
int16 ether_attach_ph(uint16 type, uint32 handler) |
666 |
|
|
{ |
667 |
cebix |
1.3 |
if (net_protocols.find(type) != net_protocols.end()) |
668 |
cebix |
1.1 |
return lapProtErr; |
669 |
cebix |
1.3 |
net_protocols[type] = handler; |
670 |
|
|
return noErr; |
671 |
cebix |
1.1 |
} |
672 |
|
|
|
673 |
|
|
|
674 |
|
|
/* |
675 |
|
|
* Detach protocol handler |
676 |
|
|
*/ |
677 |
|
|
|
678 |
|
|
int16 ether_detach_ph(uint16 type) |
679 |
|
|
{ |
680 |
cebix |
1.3 |
if (net_protocols.erase(type) == 0) |
681 |
cebix |
1.1 |
return lapProtErr; |
682 |
cebix |
1.3 |
return noErr; |
683 |
cebix |
1.1 |
} |
684 |
|
|
|
685 |
|
|
|
686 |
|
|
/* |
687 |
|
|
* Transmit raw ethernet packet |
688 |
|
|
*/ |
689 |
|
|
|
690 |
gbeauche |
1.23 |
static int16 ether_do_write(uint32 arg) |
691 |
cebix |
1.1 |
{ |
692 |
|
|
// Copy packet to buffer |
693 |
|
|
uint8 packet[1516], *p = packet; |
694 |
|
|
int len = 0; |
695 |
|
|
#if defined(__linux__) |
696 |
gbeauche |
1.9 |
if (net_if_type == NET_IF_ETHERTAP) { |
697 |
cebix |
1.1 |
*p++ = 0; // Linux ethertap discards the first 2 bytes |
698 |
|
|
*p++ = 0; |
699 |
|
|
len += 2; |
700 |
|
|
} |
701 |
|
|
#endif |
702 |
gbeauche |
1.23 |
len += ether_arg_to_buffer(arg, p); |
703 |
cebix |
1.1 |
|
704 |
|
|
#if MONITOR |
705 |
|
|
bug("Sending Ethernet packet:\n"); |
706 |
|
|
for (int i=0; i<len; i++) { |
707 |
|
|
bug("%02x ", packet[i]); |
708 |
|
|
} |
709 |
|
|
bug("\n"); |
710 |
|
|
#endif |
711 |
|
|
|
712 |
|
|
// Transmit packet |
713 |
gbeauche |
1.15 |
#ifdef HAVE_SLIRP |
714 |
|
|
if (net_if_type == NET_IF_SLIRP) { |
715 |
gbeauche |
1.21 |
const int slirp_input_fd = slirp_input_fds[1]; |
716 |
|
|
write(slirp_input_fd, &len, sizeof(len)); |
717 |
|
|
write(slirp_input_fd, packet, len); |
718 |
gbeauche |
1.15 |
return noErr; |
719 |
|
|
} else |
720 |
|
|
#endif |
721 |
cebix |
1.1 |
if (write(fd, packet, len) < 0) { |
722 |
|
|
D(bug("WARNING: Couldn't transmit packet\n")); |
723 |
|
|
return excessCollsns; |
724 |
|
|
} else |
725 |
|
|
return noErr; |
726 |
|
|
} |
727 |
|
|
|
728 |
|
|
|
729 |
|
|
/* |
730 |
cebix |
1.2 |
* Start UDP packet reception thread |
731 |
|
|
*/ |
732 |
|
|
|
733 |
|
|
bool ether_start_udp_thread(int socket_fd) |
734 |
|
|
{ |
735 |
|
|
fd = socket_fd; |
736 |
|
|
udp_tunnel = true; |
737 |
|
|
return start_thread(); |
738 |
|
|
} |
739 |
|
|
|
740 |
|
|
|
741 |
|
|
/* |
742 |
|
|
* Stop UDP packet reception thread |
743 |
|
|
*/ |
744 |
|
|
|
745 |
|
|
void ether_stop_udp_thread(void) |
746 |
|
|
{ |
747 |
|
|
stop_thread(); |
748 |
|
|
fd = -1; |
749 |
|
|
} |
750 |
|
|
|
751 |
|
|
|
752 |
|
|
/* |
753 |
gbeauche |
1.15 |
* SLIRP output buffer glue |
754 |
|
|
*/ |
755 |
|
|
|
756 |
|
|
#ifdef HAVE_SLIRP |
757 |
|
|
int slirp_can_output(void) |
758 |
|
|
{ |
759 |
|
|
return 1; |
760 |
|
|
} |
761 |
|
|
|
762 |
|
|
void slirp_output(const uint8 *packet, int len) |
763 |
|
|
{ |
764 |
|
|
write(slirp_output_fd, packet, len); |
765 |
|
|
} |
766 |
|
|
|
767 |
|
|
void *slirp_receive_func(void *arg) |
768 |
|
|
{ |
769 |
gbeauche |
1.21 |
const int slirp_input_fd = slirp_input_fds[0]; |
770 |
|
|
|
771 |
gbeauche |
1.15 |
for (;;) { |
772 |
|
|
// Wait for packets to arrive |
773 |
|
|
fd_set rfds, wfds, xfds; |
774 |
|
|
int nfds; |
775 |
|
|
struct timeval tv; |
776 |
|
|
|
777 |
gbeauche |
1.21 |
// ... in the input queue |
778 |
|
|
FD_ZERO(&rfds); |
779 |
|
|
FD_SET(slirp_input_fd, &rfds); |
780 |
|
|
tv.tv_sec = 0; |
781 |
|
|
tv.tv_usec = 0; |
782 |
|
|
if (select(slirp_input_fd + 1, &rfds, NULL, NULL, &tv) > 0) { |
783 |
|
|
int len; |
784 |
|
|
read(slirp_input_fd, &len, sizeof(len)); |
785 |
|
|
uint8 packet[1516]; |
786 |
|
|
assert(len <= sizeof(packet)); |
787 |
|
|
read(slirp_input_fd, packet, len); |
788 |
|
|
slirp_input(packet, len); |
789 |
|
|
} |
790 |
|
|
|
791 |
|
|
// ... in the output queue |
792 |
gbeauche |
1.15 |
nfds = -1; |
793 |
|
|
FD_ZERO(&rfds); |
794 |
|
|
FD_ZERO(&wfds); |
795 |
|
|
FD_ZERO(&xfds); |
796 |
|
|
slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
797 |
|
|
tv.tv_sec = 0; |
798 |
gbeauche |
1.21 |
tv.tv_usec = 10000; |
799 |
gbeauche |
1.15 |
if (select(nfds + 1, &rfds, &wfds, &xfds, &tv) >= 0) |
800 |
|
|
slirp_select_poll(&rfds, &wfds, &xfds); |
801 |
gbeauche |
1.20 |
|
802 |
|
|
#ifdef HAVE_PTHREAD_TESTCANCEL |
803 |
|
|
// Explicit cancellation point if select() was not covered |
804 |
|
|
// This seems to be the case on MacOS X 10.2 |
805 |
|
|
pthread_testcancel(); |
806 |
|
|
#endif |
807 |
gbeauche |
1.15 |
} |
808 |
|
|
return NULL; |
809 |
|
|
} |
810 |
|
|
#else |
811 |
|
|
int slirp_can_output(void) |
812 |
|
|
{ |
813 |
|
|
return 0; |
814 |
|
|
} |
815 |
|
|
|
816 |
|
|
void slirp_output(const uint8 *packet, int len) |
817 |
|
|
{ |
818 |
|
|
} |
819 |
|
|
#endif |
820 |
|
|
|
821 |
|
|
|
822 |
|
|
/* |
823 |
cebix |
1.1 |
* Packet reception thread |
824 |
|
|
*/ |
825 |
|
|
|
826 |
|
|
static void *receive_func(void *arg) |
827 |
|
|
{ |
828 |
|
|
for (;;) { |
829 |
|
|
|
830 |
|
|
// Wait for packets to arrive |
831 |
gbeauche |
1.25 |
#if USE_POLL |
832 |
gbeauche |
1.20 |
struct pollfd pf = {fd, POLLIN, 0}; |
833 |
|
|
int res = poll(&pf, 1, -1); |
834 |
|
|
#else |
835 |
|
|
fd_set rfds; |
836 |
|
|
FD_ZERO(&rfds); |
837 |
|
|
FD_SET(fd, &rfds); |
838 |
|
|
// A NULL timeout could cause select() to block indefinitely, |
839 |
|
|
// even if it is supposed to be a cancellation point [MacOS X] |
840 |
|
|
struct timeval tv = { 0, 20000 }; |
841 |
|
|
int res = select(fd + 1, &rfds, NULL, NULL, &tv); |
842 |
|
|
#ifdef HAVE_PTHREAD_TESTCANCEL |
843 |
|
|
pthread_testcancel(); |
844 |
|
|
#endif |
845 |
|
|
if (res == 0 || (res == -1 && errno == EINTR)) |
846 |
|
|
continue; |
847 |
|
|
#endif |
848 |
cebix |
1.1 |
if (res <= 0) |
849 |
|
|
break; |
850 |
|
|
|
851 |
gbeauche |
1.23 |
if (ether_driver_opened) { |
852 |
|
|
// Trigger Ethernet interrupt |
853 |
|
|
D(bug(" packet received, triggering Ethernet interrupt\n")); |
854 |
|
|
SetInterruptFlag(INTFLAG_ETHER); |
855 |
|
|
TriggerInterrupt(); |
856 |
|
|
|
857 |
|
|
// Wait for interrupt acknowledge by EtherInterrupt() |
858 |
|
|
sem_wait(&int_ack); |
859 |
|
|
} else |
860 |
|
|
usleep(20000); |
861 |
cebix |
1.1 |
} |
862 |
|
|
return NULL; |
863 |
|
|
} |
864 |
|
|
|
865 |
|
|
|
866 |
|
|
/* |
867 |
|
|
* Ethernet interrupt - activate deferred tasks to call IODone or protocol handlers |
868 |
|
|
*/ |
869 |
|
|
|
870 |
gbeauche |
1.23 |
void ether_do_interrupt(void) |
871 |
cebix |
1.1 |
{ |
872 |
|
|
// Call protocol handler for received packets |
873 |
gbeauche |
1.14 |
EthernetPacket ether_packet; |
874 |
|
|
uint32 packet = ether_packet.addr(); |
875 |
cebix |
1.2 |
ssize_t length; |
876 |
cebix |
1.1 |
for (;;) { |
877 |
|
|
|
878 |
gbeauche |
1.23 |
#ifndef SHEEPSHAVER |
879 |
cebix |
1.2 |
if (udp_tunnel) { |
880 |
|
|
|
881 |
|
|
// Read packet from socket |
882 |
|
|
struct sockaddr_in from; |
883 |
|
|
socklen_t from_len = sizeof(from); |
884 |
gbeauche |
1.14 |
length = recvfrom(fd, Mac2HostAddr(packet), 1514, 0, (struct sockaddr *)&from, &from_len); |
885 |
cebix |
1.2 |
if (length < 14) |
886 |
|
|
break; |
887 |
|
|
ether_udp_read(packet, length, &from); |
888 |
|
|
|
889 |
gbeauche |
1.23 |
} else |
890 |
|
|
#endif |
891 |
|
|
{ |
892 |
cebix |
1.2 |
|
893 |
|
|
// Read packet from sheep_net device |
894 |
cebix |
1.1 |
#if defined(__linux__) |
895 |
gbeauche |
1.14 |
length = read(fd, Mac2HostAddr(packet), net_if_type == NET_IF_ETHERTAP ? 1516 : 1514); |
896 |
cebix |
1.1 |
#else |
897 |
gbeauche |
1.14 |
length = read(fd, Mac2HostAddr(packet), 1514); |
898 |
cebix |
1.1 |
#endif |
899 |
cebix |
1.2 |
if (length < 14) |
900 |
|
|
break; |
901 |
cebix |
1.1 |
|
902 |
|
|
#if MONITOR |
903 |
cebix |
1.2 |
bug("Receiving Ethernet packet:\n"); |
904 |
|
|
for (int i=0; i<length; i++) { |
905 |
gbeauche |
1.14 |
bug("%02x ", ReadMacInt8(packet + i)); |
906 |
cebix |
1.2 |
} |
907 |
|
|
bug("\n"); |
908 |
cebix |
1.1 |
#endif |
909 |
|
|
|
910 |
cebix |
1.2 |
// Pointer to packet data (Ethernet header) |
911 |
gbeauche |
1.14 |
uint32 p = packet; |
912 |
cebix |
1.1 |
#if defined(__linux__) |
913 |
gbeauche |
1.9 |
if (net_if_type == NET_IF_ETHERTAP) { |
914 |
cebix |
1.2 |
p += 2; // Linux ethertap has two random bytes before the packet |
915 |
|
|
length -= 2; |
916 |
|
|
} |
917 |
cebix |
1.1 |
#endif |
918 |
|
|
|
919 |
gbeauche |
1.23 |
// Dispatch packet |
920 |
|
|
ether_dispatch_packet(p, length); |
921 |
cebix |
1.2 |
} |
922 |
cebix |
1.1 |
} |
923 |
|
|
} |