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