1 |
cebix |
1.1 |
/* |
2 |
|
|
* ether_unix.cpp - Ethernet device driver, Unix specific stuff (Linux and FreeBSD) |
3 |
|
|
* |
4 |
cebix |
1.8 |
* Basilisk II (C) 1997-2004 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 |
|
|
#include <sys/ioctl.h> |
24 |
|
|
#include <sys/poll.h> |
25 |
cebix |
1.3 |
#include <sys/socket.h> |
26 |
gbeauche |
1.9 |
#include <sys/wait.h> |
27 |
cebix |
1.3 |
#include <netinet/in.h> |
28 |
cebix |
1.1 |
#include <pthread.h> |
29 |
|
|
#include <semaphore.h> |
30 |
|
|
#include <errno.h> |
31 |
|
|
#include <stdio.h> |
32 |
cebix |
1.3 |
#include <map> |
33 |
cebix |
1.2 |
|
34 |
cebix |
1.5 |
#if defined(__FreeBSD__) || defined(sgi) |
35 |
cebix |
1.1 |
#include <net/if.h> |
36 |
|
|
#endif |
37 |
|
|
|
38 |
gbeauche |
1.9 |
#if defined(HAVE_LINUX_IF_H) && defined(HAVE_LINUX_IF_TUN_H) |
39 |
|
|
#include <linux/if.h> |
40 |
|
|
#include <linux/if_tun.h> |
41 |
|
|
#endif |
42 |
|
|
|
43 |
|
|
#if defined(HAVE_NET_IF_H) && defined(HAVE_NET_IF_TUN_H) |
44 |
|
|
#include <net/if.h> |
45 |
|
|
#include <net/if_tun.h> |
46 |
|
|
#endif |
47 |
|
|
|
48 |
cebix |
1.1 |
#include "cpu_emulation.h" |
49 |
|
|
#include "main.h" |
50 |
|
|
#include "macos_util.h" |
51 |
|
|
#include "prefs.h" |
52 |
|
|
#include "user_strings.h" |
53 |
|
|
#include "ether.h" |
54 |
|
|
#include "ether_defs.h" |
55 |
|
|
|
56 |
cebix |
1.3 |
#ifndef NO_STD_NAMESPACE |
57 |
|
|
using std::map; |
58 |
|
|
#endif |
59 |
|
|
|
60 |
cebix |
1.1 |
#define DEBUG 0 |
61 |
|
|
#include "debug.h" |
62 |
|
|
|
63 |
|
|
#define MONITOR 0 |
64 |
|
|
|
65 |
|
|
|
66 |
gbeauche |
1.9 |
// Ethernet device types |
67 |
|
|
enum { |
68 |
|
|
NET_IF_SHEEPNET, |
69 |
|
|
NET_IF_ETHERTAP, |
70 |
|
|
NET_IF_TUNTAP, |
71 |
|
|
}; |
72 |
|
|
|
73 |
|
|
// Constants |
74 |
|
|
static const char ETHERCONFIG_FILE_NAME[] = DATADIR "/tunconfig"; |
75 |
|
|
|
76 |
cebix |
1.1 |
// Global variables |
77 |
|
|
static int fd = -1; // fd of sheep_net device |
78 |
|
|
static pthread_t ether_thread; // Packet reception thread |
79 |
|
|
static pthread_attr_t ether_thread_attr; // Packet reception thread attributes |
80 |
|
|
static bool thread_active = false; // Flag: Packet reception thread installed |
81 |
|
|
static sem_t int_ack; // Interrupt acknowledge semaphore |
82 |
cebix |
1.2 |
static bool udp_tunnel; // Flag: UDP tunnelling active, fd is the socket descriptor |
83 |
gbeauche |
1.9 |
static int net_if_type = -1; // Ethernet device type |
84 |
gbeauche |
1.10 |
static char *net_if_name = NULL; // TUN/TAP device name |
85 |
gbeauche |
1.9 |
static const char *net_if_script = NULL; // Network config script |
86 |
cebix |
1.1 |
|
87 |
cebix |
1.3 |
// Attached network protocols, maps protocol type to MacOS handler address |
88 |
|
|
static map<uint16, uint32> net_protocols; |
89 |
|
|
|
90 |
cebix |
1.1 |
// Prototypes |
91 |
|
|
static void *receive_func(void *arg); |
92 |
|
|
|
93 |
|
|
|
94 |
|
|
/* |
95 |
cebix |
1.2 |
* Start packet reception thread |
96 |
|
|
*/ |
97 |
|
|
|
98 |
|
|
static bool start_thread(void) |
99 |
|
|
{ |
100 |
|
|
if (sem_init(&int_ack, 0, 0) < 0) { |
101 |
|
|
printf("WARNING: Cannot init semaphore"); |
102 |
|
|
return false; |
103 |
|
|
} |
104 |
|
|
|
105 |
cebix |
1.7 |
Set_pthread_attr(ðer_thread_attr, 1); |
106 |
cebix |
1.2 |
thread_active = (pthread_create(ðer_thread, ðer_thread_attr, receive_func, NULL) == 0); |
107 |
|
|
if (!thread_active) { |
108 |
|
|
printf("WARNING: Cannot start Ethernet thread"); |
109 |
|
|
return false; |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
return true; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
|
116 |
|
|
/* |
117 |
|
|
* Stop packet reception thread |
118 |
|
|
*/ |
119 |
|
|
|
120 |
|
|
static void stop_thread(void) |
121 |
|
|
{ |
122 |
|
|
if (thread_active) { |
123 |
|
|
pthread_cancel(ether_thread); |
124 |
|
|
pthread_join(ether_thread, NULL); |
125 |
|
|
sem_destroy(&int_ack); |
126 |
|
|
thread_active = false; |
127 |
|
|
} |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
|
131 |
|
|
/* |
132 |
gbeauche |
1.9 |
* Execute network script up|down |
133 |
|
|
*/ |
134 |
|
|
|
135 |
|
|
static bool execute_network_script(const char *action) |
136 |
|
|
{ |
137 |
|
|
if (net_if_script == NULL || net_if_name == NULL) |
138 |
|
|
return false; |
139 |
|
|
|
140 |
|
|
int pid = fork(); |
141 |
|
|
if (pid >= 0) { |
142 |
|
|
if (pid == 0) { |
143 |
|
|
char *args[4]; |
144 |
|
|
args[0] = (char *)net_if_script; |
145 |
gbeauche |
1.10 |
args[1] = net_if_name; |
146 |
gbeauche |
1.9 |
args[2] = (char *)action; |
147 |
|
|
args[3] = NULL; |
148 |
|
|
execv(net_if_script, args); |
149 |
|
|
exit(1); |
150 |
|
|
} |
151 |
|
|
int status; |
152 |
|
|
while (waitpid(pid, &status, 0) != pid); |
153 |
|
|
return WIFEXITED(status) && WEXITSTATUS(status) == 0; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
return false; |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
|
160 |
|
|
/* |
161 |
cebix |
1.1 |
* Initialization |
162 |
|
|
*/ |
163 |
|
|
|
164 |
cebix |
1.2 |
bool ether_init(void) |
165 |
cebix |
1.1 |
{ |
166 |
|
|
int nonblock = 1; |
167 |
|
|
char str[256]; |
168 |
|
|
|
169 |
|
|
// Do nothing if no Ethernet device specified |
170 |
|
|
const char *name = PrefsFindString("ether"); |
171 |
|
|
if (name == NULL) |
172 |
cebix |
1.2 |
return false; |
173 |
cebix |
1.1 |
|
174 |
gbeauche |
1.9 |
// Determine Ethernet device type |
175 |
|
|
net_if_type = -1; |
176 |
|
|
if (strncmp(name, "tap", 3) == 0) |
177 |
|
|
net_if_type = NET_IF_ETHERTAP; |
178 |
|
|
#if ENABLE_TUNTAP |
179 |
|
|
else if (strcmp(name, "tun") == 0) |
180 |
|
|
net_if_type = NET_IF_TUNTAP; |
181 |
|
|
#endif |
182 |
|
|
else |
183 |
|
|
net_if_type = NET_IF_SHEEPNET; |
184 |
cebix |
1.1 |
|
185 |
gbeauche |
1.11 |
// Open sheep_net or ethertap or TUN/TAP device |
186 |
cebix |
1.1 |
char dev_name[16]; |
187 |
gbeauche |
1.9 |
switch (net_if_type) { |
188 |
|
|
case NET_IF_ETHERTAP: |
189 |
cebix |
1.1 |
sprintf(dev_name, "/dev/%s", name); |
190 |
gbeauche |
1.9 |
break; |
191 |
|
|
case NET_IF_TUNTAP: |
192 |
gbeauche |
1.11 |
strcpy(dev_name, "/dev/net/tun"); |
193 |
gbeauche |
1.9 |
break; |
194 |
|
|
case NET_IF_SHEEPNET: |
195 |
cebix |
1.1 |
strcpy(dev_name, "/dev/sheep_net"); |
196 |
gbeauche |
1.9 |
break; |
197 |
|
|
} |
198 |
cebix |
1.1 |
fd = open(dev_name, O_RDWR); |
199 |
|
|
if (fd < 0) { |
200 |
|
|
sprintf(str, GetString(STR_NO_SHEEP_NET_DRIVER_WARN), dev_name, strerror(errno)); |
201 |
|
|
WarningAlert(str); |
202 |
|
|
goto open_error; |
203 |
|
|
} |
204 |
|
|
|
205 |
gbeauche |
1.9 |
#if ENABLE_TUNTAP |
206 |
|
|
// Open TUN/TAP interface |
207 |
|
|
if (net_if_type == NET_IF_TUNTAP) { |
208 |
|
|
struct ifreq ifr; |
209 |
|
|
memset(&ifr, 0, sizeof(ifr)); |
210 |
|
|
ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
211 |
|
|
strcpy(ifr.ifr_name, "tun%d"); |
212 |
|
|
if (ioctl(fd, TUNSETIFF, (void *) &ifr) != 0) { |
213 |
|
|
sprintf(str, GetString(STR_SHEEP_NET_ATTACH_WARN), strerror(errno)); |
214 |
|
|
WarningAlert(str); |
215 |
|
|
goto open_error; |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
// Get network config script file path |
219 |
|
|
net_if_script = PrefsFindString("etherconfig"); |
220 |
|
|
if (net_if_script == NULL) |
221 |
|
|
net_if_script = ETHERCONFIG_FILE_NAME; |
222 |
|
|
|
223 |
|
|
// Start network script up |
224 |
|
|
if (net_if_script == NULL) { |
225 |
|
|
sprintf(str, GetString(STR_TUN_TAP_CONFIG_WARN), "script not found"); |
226 |
|
|
WarningAlert(str); |
227 |
|
|
goto open_error; |
228 |
|
|
} |
229 |
gbeauche |
1.10 |
net_if_name = strdup(ifr.ifr_name); |
230 |
gbeauche |
1.9 |
if (!execute_network_script("up")) { |
231 |
|
|
sprintf(str, GetString(STR_TUN_TAP_CONFIG_WARN), "script execute error"); |
232 |
|
|
WarningAlert(str); |
233 |
|
|
goto open_error; |
234 |
|
|
} |
235 |
|
|
D(bug("Connected to host network interface: %s\n", net_if_name)); |
236 |
|
|
} |
237 |
|
|
#endif |
238 |
|
|
|
239 |
cebix |
1.1 |
#if defined(__linux__) |
240 |
|
|
// Attach sheep_net to selected Ethernet card |
241 |
gbeauche |
1.9 |
if (net_if_type == NET_IF_SHEEPNET && ioctl(fd, SIOCSIFLINK, name) < 0) { |
242 |
cebix |
1.1 |
sprintf(str, GetString(STR_SHEEP_NET_ATTACH_WARN), strerror(errno)); |
243 |
|
|
WarningAlert(str); |
244 |
|
|
goto open_error; |
245 |
|
|
} |
246 |
|
|
#endif |
247 |
|
|
|
248 |
|
|
// Set nonblocking I/O |
249 |
|
|
ioctl(fd, FIONBIO, &nonblock); |
250 |
|
|
|
251 |
|
|
// Get Ethernet address |
252 |
gbeauche |
1.9 |
if (net_if_type == NET_IF_ETHERTAP) { |
253 |
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 |
254 |
|
|
ether_addr[0] = 0xfe; |
255 |
|
|
ether_addr[1] = 0xfd; |
256 |
|
|
ether_addr[2] = p >> 24; |
257 |
|
|
ether_addr[3] = p >> 16; |
258 |
|
|
ether_addr[4] = p >> 8; |
259 |
|
|
ether_addr[5] = p; |
260 |
|
|
} else |
261 |
|
|
ioctl(fd, SIOCGIFADDR, ether_addr); |
262 |
|
|
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])); |
263 |
|
|
|
264 |
|
|
// Start packet reception thread |
265 |
cebix |
1.2 |
if (!start_thread()) |
266 |
cebix |
1.1 |
goto open_error; |
267 |
|
|
|
268 |
|
|
// Everything OK |
269 |
cebix |
1.2 |
return true; |
270 |
cebix |
1.1 |
|
271 |
|
|
open_error: |
272 |
cebix |
1.2 |
stop_thread(); |
273 |
|
|
|
274 |
cebix |
1.1 |
if (fd > 0) { |
275 |
|
|
close(fd); |
276 |
|
|
fd = -1; |
277 |
|
|
} |
278 |
cebix |
1.2 |
return false; |
279 |
cebix |
1.1 |
} |
280 |
|
|
|
281 |
|
|
|
282 |
|
|
/* |
283 |
|
|
* Deinitialization |
284 |
|
|
*/ |
285 |
|
|
|
286 |
cebix |
1.2 |
void ether_exit(void) |
287 |
cebix |
1.1 |
{ |
288 |
|
|
// Stop reception thread |
289 |
|
|
if (thread_active) { |
290 |
|
|
pthread_cancel(ether_thread); |
291 |
|
|
pthread_join(ether_thread, NULL); |
292 |
|
|
sem_destroy(&int_ack); |
293 |
|
|
thread_active = false; |
294 |
|
|
} |
295 |
|
|
|
296 |
gbeauche |
1.10 |
// Shut down TUN/TAP interface |
297 |
|
|
if (net_if_type == NET_IF_TUNTAP) |
298 |
|
|
execute_network_script("down"); |
299 |
|
|
|
300 |
|
|
// Free TUN/TAP device name |
301 |
|
|
if (net_if_name) |
302 |
|
|
free(net_if_name); |
303 |
|
|
|
304 |
cebix |
1.1 |
// Close sheep_net device |
305 |
|
|
if (fd > 0) |
306 |
|
|
close(fd); |
307 |
|
|
} |
308 |
|
|
|
309 |
|
|
|
310 |
|
|
/* |
311 |
|
|
* Reset |
312 |
|
|
*/ |
313 |
|
|
|
314 |
cebix |
1.3 |
void ether_reset(void) |
315 |
cebix |
1.1 |
{ |
316 |
cebix |
1.3 |
net_protocols.clear(); |
317 |
cebix |
1.1 |
} |
318 |
|
|
|
319 |
|
|
|
320 |
|
|
/* |
321 |
|
|
* Add multicast address |
322 |
|
|
*/ |
323 |
|
|
|
324 |
|
|
int16 ether_add_multicast(uint32 pb) |
325 |
|
|
{ |
326 |
gbeauche |
1.9 |
if (net_if_type != NET_IF_TUNTAP && ioctl(fd, SIOCADDMULTI, Mac2HostAddr(pb + eMultiAddr)) < 0) { |
327 |
cebix |
1.1 |
D(bug("WARNING: Couldn't enable multicast address\n")); |
328 |
gbeauche |
1.9 |
if (net_if_type == NET_IF_ETHERTAP) |
329 |
cebix |
1.1 |
return noErr; |
330 |
|
|
else |
331 |
|
|
return eMultiErr; |
332 |
|
|
} else |
333 |
|
|
return noErr; |
334 |
|
|
} |
335 |
|
|
|
336 |
|
|
|
337 |
|
|
/* |
338 |
|
|
* Delete multicast address |
339 |
|
|
*/ |
340 |
|
|
|
341 |
|
|
int16 ether_del_multicast(uint32 pb) |
342 |
|
|
{ |
343 |
gbeauche |
1.9 |
if (net_if_type != NET_IF_TUNTAP && ioctl(fd, SIOCDELMULTI, Mac2HostAddr(pb + eMultiAddr)) < 0) { |
344 |
cebix |
1.1 |
D(bug("WARNING: Couldn't disable multicast address\n")); |
345 |
|
|
return eMultiErr; |
346 |
|
|
} else |
347 |
|
|
return noErr; |
348 |
|
|
} |
349 |
|
|
|
350 |
|
|
|
351 |
|
|
/* |
352 |
|
|
* Attach protocol handler |
353 |
|
|
*/ |
354 |
|
|
|
355 |
|
|
int16 ether_attach_ph(uint16 type, uint32 handler) |
356 |
|
|
{ |
357 |
cebix |
1.3 |
if (net_protocols.find(type) != net_protocols.end()) |
358 |
cebix |
1.1 |
return lapProtErr; |
359 |
cebix |
1.3 |
net_protocols[type] = handler; |
360 |
|
|
return noErr; |
361 |
cebix |
1.1 |
} |
362 |
|
|
|
363 |
|
|
|
364 |
|
|
/* |
365 |
|
|
* Detach protocol handler |
366 |
|
|
*/ |
367 |
|
|
|
368 |
|
|
int16 ether_detach_ph(uint16 type) |
369 |
|
|
{ |
370 |
cebix |
1.3 |
if (net_protocols.erase(type) == 0) |
371 |
cebix |
1.1 |
return lapProtErr; |
372 |
cebix |
1.3 |
return noErr; |
373 |
cebix |
1.1 |
} |
374 |
|
|
|
375 |
|
|
|
376 |
|
|
/* |
377 |
|
|
* Transmit raw ethernet packet |
378 |
|
|
*/ |
379 |
|
|
|
380 |
|
|
int16 ether_write(uint32 wds) |
381 |
|
|
{ |
382 |
|
|
// Copy packet to buffer |
383 |
|
|
uint8 packet[1516], *p = packet; |
384 |
|
|
int len = 0; |
385 |
|
|
#if defined(__linux__) |
386 |
gbeauche |
1.9 |
if (net_if_type == NET_IF_ETHERTAP) { |
387 |
cebix |
1.1 |
*p++ = 0; // Linux ethertap discards the first 2 bytes |
388 |
|
|
*p++ = 0; |
389 |
|
|
len += 2; |
390 |
|
|
} |
391 |
|
|
#endif |
392 |
cebix |
1.2 |
len += ether_wds_to_buffer(wds, p); |
393 |
cebix |
1.1 |
|
394 |
|
|
#if MONITOR |
395 |
|
|
bug("Sending Ethernet packet:\n"); |
396 |
|
|
for (int i=0; i<len; i++) { |
397 |
|
|
bug("%02x ", packet[i]); |
398 |
|
|
} |
399 |
|
|
bug("\n"); |
400 |
|
|
#endif |
401 |
|
|
|
402 |
|
|
// Transmit packet |
403 |
|
|
if (write(fd, packet, len) < 0) { |
404 |
|
|
D(bug("WARNING: Couldn't transmit packet\n")); |
405 |
|
|
return excessCollsns; |
406 |
|
|
} else |
407 |
|
|
return noErr; |
408 |
|
|
} |
409 |
|
|
|
410 |
|
|
|
411 |
|
|
/* |
412 |
cebix |
1.2 |
* Start UDP packet reception thread |
413 |
|
|
*/ |
414 |
|
|
|
415 |
|
|
bool ether_start_udp_thread(int socket_fd) |
416 |
|
|
{ |
417 |
|
|
fd = socket_fd; |
418 |
|
|
udp_tunnel = true; |
419 |
|
|
return start_thread(); |
420 |
|
|
} |
421 |
|
|
|
422 |
|
|
|
423 |
|
|
/* |
424 |
|
|
* Stop UDP packet reception thread |
425 |
|
|
*/ |
426 |
|
|
|
427 |
|
|
void ether_stop_udp_thread(void) |
428 |
|
|
{ |
429 |
|
|
stop_thread(); |
430 |
|
|
fd = -1; |
431 |
|
|
} |
432 |
|
|
|
433 |
|
|
|
434 |
|
|
/* |
435 |
cebix |
1.1 |
* Packet reception thread |
436 |
|
|
*/ |
437 |
|
|
|
438 |
|
|
static void *receive_func(void *arg) |
439 |
|
|
{ |
440 |
|
|
for (;;) { |
441 |
|
|
|
442 |
|
|
// Wait for packets to arrive |
443 |
|
|
struct pollfd pf = {fd, POLLIN, 0}; |
444 |
|
|
int res = poll(&pf, 1, -1); |
445 |
|
|
if (res <= 0) |
446 |
|
|
break; |
447 |
|
|
|
448 |
|
|
// Trigger Ethernet interrupt |
449 |
|
|
D(bug(" packet received, triggering Ethernet interrupt\n")); |
450 |
|
|
SetInterruptFlag(INTFLAG_ETHER); |
451 |
|
|
TriggerInterrupt(); |
452 |
|
|
|
453 |
|
|
// Wait for interrupt acknowledge by EtherInterrupt() |
454 |
|
|
sem_wait(&int_ack); |
455 |
|
|
} |
456 |
|
|
return NULL; |
457 |
|
|
} |
458 |
|
|
|
459 |
|
|
|
460 |
|
|
/* |
461 |
|
|
* Ethernet interrupt - activate deferred tasks to call IODone or protocol handlers |
462 |
|
|
*/ |
463 |
|
|
|
464 |
|
|
void EtherInterrupt(void) |
465 |
|
|
{ |
466 |
|
|
D(bug("EtherIRQ\n")); |
467 |
|
|
|
468 |
|
|
// Call protocol handler for received packets |
469 |
gbeauche |
1.12 |
// NOTE: "static" so that packet[] has a 32-bit address (.data section, not stack) |
470 |
|
|
static uint8 packet[1516]; |
471 |
cebix |
1.2 |
ssize_t length; |
472 |
cebix |
1.1 |
for (;;) { |
473 |
|
|
|
474 |
cebix |
1.2 |
if (udp_tunnel) { |
475 |
|
|
|
476 |
|
|
// Read packet from socket |
477 |
|
|
struct sockaddr_in from; |
478 |
|
|
socklen_t from_len = sizeof(from); |
479 |
|
|
length = recvfrom(fd, packet, 1514, 0, (struct sockaddr *)&from, &from_len); |
480 |
|
|
if (length < 14) |
481 |
|
|
break; |
482 |
|
|
ether_udp_read(packet, length, &from); |
483 |
|
|
|
484 |
|
|
} else { |
485 |
|
|
|
486 |
|
|
// Read packet from sheep_net device |
487 |
cebix |
1.1 |
#if defined(__linux__) |
488 |
gbeauche |
1.9 |
length = read(fd, packet, net_if_type == NET_IF_ETHERTAP ? 1516 : 1514); |
489 |
cebix |
1.1 |
#else |
490 |
cebix |
1.2 |
length = read(fd, packet, 1514); |
491 |
cebix |
1.1 |
#endif |
492 |
cebix |
1.2 |
if (length < 14) |
493 |
|
|
break; |
494 |
cebix |
1.1 |
|
495 |
|
|
#if MONITOR |
496 |
cebix |
1.2 |
bug("Receiving Ethernet packet:\n"); |
497 |
|
|
for (int i=0; i<length; i++) { |
498 |
|
|
bug("%02x ", packet[i]); |
499 |
|
|
} |
500 |
|
|
bug("\n"); |
501 |
cebix |
1.1 |
#endif |
502 |
|
|
|
503 |
cebix |
1.2 |
// Pointer to packet data (Ethernet header) |
504 |
|
|
uint8 *p = packet; |
505 |
cebix |
1.1 |
#if defined(__linux__) |
506 |
gbeauche |
1.9 |
if (net_if_type == NET_IF_ETHERTAP) { |
507 |
cebix |
1.2 |
p += 2; // Linux ethertap has two random bytes before the packet |
508 |
|
|
length -= 2; |
509 |
|
|
} |
510 |
cebix |
1.1 |
#endif |
511 |
|
|
|
512 |
cebix |
1.2 |
// Get packet type |
513 |
|
|
uint16 type = (p[12] << 8) | p[13]; |
514 |
cebix |
1.1 |
|
515 |
cebix |
1.2 |
// Look for protocol |
516 |
cebix |
1.3 |
uint16 search_type = (type <= 1500 ? 0 : type); |
517 |
|
|
if (net_protocols.find(search_type) == net_protocols.end()) |
518 |
cebix |
1.2 |
continue; |
519 |
cebix |
1.3 |
uint32 handler = net_protocols[search_type]; |
520 |
cebix |
1.2 |
|
521 |
|
|
// No default handler |
522 |
cebix |
1.3 |
if (handler == 0) |
523 |
cebix |
1.2 |
continue; |
524 |
|
|
|
525 |
|
|
// Copy header to RHA |
526 |
|
|
Host2Mac_memcpy(ether_data + ed_RHA, p, 14); |
527 |
|
|
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))); |
528 |
|
|
|
529 |
|
|
// Call protocol handler |
530 |
|
|
M68kRegisters r; |
531 |
|
|
r.d[0] = type; // Packet type |
532 |
|
|
r.d[1] = length - 14; // Remaining packet length (without header, for ReadPacket) |
533 |
|
|
r.a[0] = (uint32)p + 14; // Pointer to packet (host address, for ReadPacket) |
534 |
|
|
r.a[3] = ether_data + ed_RHA + 14; // Pointer behind header in RHA |
535 |
|
|
r.a[4] = ether_data + ed_ReadPacket; // Pointer to ReadPacket/ReadRest routines |
536 |
cebix |
1.3 |
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])); |
537 |
|
|
Execute68k(handler, &r); |
538 |
cebix |
1.2 |
} |
539 |
cebix |
1.1 |
} |
540 |
|
|
|
541 |
|
|
// Acknowledge interrupt to reception thread |
542 |
|
|
D(bug(" EtherIRQ done\n")); |
543 |
|
|
sem_post(&int_ack); |
544 |
|
|
} |