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 |
|
|
static const char *net_if_name = NULL; // TUN/TAP device name |
85 |
|
|
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 |
|
|
args[1] = (char *)net_if_name; |
146 |
|
|
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 |
|
|
// Open sheep_net or ethertap device |
186 |
|
|
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 |
|
|
sprintf(dev_name, "/dev/net/tun", name); |
193 |
|
|
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 |
|
|
net_if_name = ifr.ifr_name; |
230 |
|
|
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 |
gbeauche |
1.9 |
if (net_if_type == NET_IF_TUNTAP) |
275 |
|
|
execute_network_script("down"); |
276 |
|
|
|
277 |
cebix |
1.1 |
if (fd > 0) { |
278 |
|
|
close(fd); |
279 |
|
|
fd = -1; |
280 |
|
|
} |
281 |
cebix |
1.2 |
return false; |
282 |
cebix |
1.1 |
} |
283 |
|
|
|
284 |
|
|
|
285 |
|
|
/* |
286 |
|
|
* Deinitialization |
287 |
|
|
*/ |
288 |
|
|
|
289 |
cebix |
1.2 |
void ether_exit(void) |
290 |
cebix |
1.1 |
{ |
291 |
|
|
// Stop reception thread |
292 |
|
|
if (thread_active) { |
293 |
|
|
pthread_cancel(ether_thread); |
294 |
|
|
pthread_join(ether_thread, NULL); |
295 |
|
|
sem_destroy(&int_ack); |
296 |
|
|
thread_active = false; |
297 |
|
|
} |
298 |
|
|
|
299 |
|
|
// Close sheep_net device |
300 |
|
|
if (fd > 0) |
301 |
|
|
close(fd); |
302 |
|
|
} |
303 |
|
|
|
304 |
|
|
|
305 |
|
|
/* |
306 |
|
|
* Reset |
307 |
|
|
*/ |
308 |
|
|
|
309 |
cebix |
1.3 |
void ether_reset(void) |
310 |
cebix |
1.1 |
{ |
311 |
cebix |
1.3 |
net_protocols.clear(); |
312 |
cebix |
1.1 |
} |
313 |
|
|
|
314 |
|
|
|
315 |
|
|
/* |
316 |
|
|
* Add multicast address |
317 |
|
|
*/ |
318 |
|
|
|
319 |
|
|
int16 ether_add_multicast(uint32 pb) |
320 |
|
|
{ |
321 |
gbeauche |
1.9 |
if (net_if_type != NET_IF_TUNTAP && ioctl(fd, SIOCADDMULTI, Mac2HostAddr(pb + eMultiAddr)) < 0) { |
322 |
cebix |
1.1 |
D(bug("WARNING: Couldn't enable multicast address\n")); |
323 |
gbeauche |
1.9 |
if (net_if_type == NET_IF_ETHERTAP) |
324 |
cebix |
1.1 |
return noErr; |
325 |
|
|
else |
326 |
|
|
return eMultiErr; |
327 |
|
|
} else |
328 |
|
|
return noErr; |
329 |
|
|
} |
330 |
|
|
|
331 |
|
|
|
332 |
|
|
/* |
333 |
|
|
* Delete multicast address |
334 |
|
|
*/ |
335 |
|
|
|
336 |
|
|
int16 ether_del_multicast(uint32 pb) |
337 |
|
|
{ |
338 |
gbeauche |
1.9 |
if (net_if_type != NET_IF_TUNTAP && ioctl(fd, SIOCDELMULTI, Mac2HostAddr(pb + eMultiAddr)) < 0) { |
339 |
cebix |
1.1 |
D(bug("WARNING: Couldn't disable multicast address\n")); |
340 |
|
|
return eMultiErr; |
341 |
|
|
} else |
342 |
|
|
return noErr; |
343 |
|
|
} |
344 |
|
|
|
345 |
|
|
|
346 |
|
|
/* |
347 |
|
|
* Attach protocol handler |
348 |
|
|
*/ |
349 |
|
|
|
350 |
|
|
int16 ether_attach_ph(uint16 type, uint32 handler) |
351 |
|
|
{ |
352 |
cebix |
1.3 |
if (net_protocols.find(type) != net_protocols.end()) |
353 |
cebix |
1.1 |
return lapProtErr; |
354 |
cebix |
1.3 |
net_protocols[type] = handler; |
355 |
|
|
return noErr; |
356 |
cebix |
1.1 |
} |
357 |
|
|
|
358 |
|
|
|
359 |
|
|
/* |
360 |
|
|
* Detach protocol handler |
361 |
|
|
*/ |
362 |
|
|
|
363 |
|
|
int16 ether_detach_ph(uint16 type) |
364 |
|
|
{ |
365 |
cebix |
1.3 |
if (net_protocols.erase(type) == 0) |
366 |
cebix |
1.1 |
return lapProtErr; |
367 |
cebix |
1.3 |
return noErr; |
368 |
cebix |
1.1 |
} |
369 |
|
|
|
370 |
|
|
|
371 |
|
|
/* |
372 |
|
|
* Transmit raw ethernet packet |
373 |
|
|
*/ |
374 |
|
|
|
375 |
|
|
int16 ether_write(uint32 wds) |
376 |
|
|
{ |
377 |
|
|
// Copy packet to buffer |
378 |
|
|
uint8 packet[1516], *p = packet; |
379 |
|
|
int len = 0; |
380 |
|
|
#if defined(__linux__) |
381 |
gbeauche |
1.9 |
if (net_if_type == NET_IF_ETHERTAP) { |
382 |
cebix |
1.1 |
*p++ = 0; // Linux ethertap discards the first 2 bytes |
383 |
|
|
*p++ = 0; |
384 |
|
|
len += 2; |
385 |
|
|
} |
386 |
|
|
#endif |
387 |
cebix |
1.2 |
len += ether_wds_to_buffer(wds, p); |
388 |
cebix |
1.1 |
|
389 |
|
|
#if MONITOR |
390 |
|
|
bug("Sending Ethernet packet:\n"); |
391 |
|
|
for (int i=0; i<len; i++) { |
392 |
|
|
bug("%02x ", packet[i]); |
393 |
|
|
} |
394 |
|
|
bug("\n"); |
395 |
|
|
#endif |
396 |
|
|
|
397 |
|
|
// Transmit packet |
398 |
|
|
if (write(fd, packet, len) < 0) { |
399 |
|
|
D(bug("WARNING: Couldn't transmit packet\n")); |
400 |
|
|
return excessCollsns; |
401 |
|
|
} else |
402 |
|
|
return noErr; |
403 |
|
|
} |
404 |
|
|
|
405 |
|
|
|
406 |
|
|
/* |
407 |
cebix |
1.2 |
* Start UDP packet reception thread |
408 |
|
|
*/ |
409 |
|
|
|
410 |
|
|
bool ether_start_udp_thread(int socket_fd) |
411 |
|
|
{ |
412 |
|
|
fd = socket_fd; |
413 |
|
|
udp_tunnel = true; |
414 |
|
|
return start_thread(); |
415 |
|
|
} |
416 |
|
|
|
417 |
|
|
|
418 |
|
|
/* |
419 |
|
|
* Stop UDP packet reception thread |
420 |
|
|
*/ |
421 |
|
|
|
422 |
|
|
void ether_stop_udp_thread(void) |
423 |
|
|
{ |
424 |
|
|
stop_thread(); |
425 |
|
|
fd = -1; |
426 |
|
|
} |
427 |
|
|
|
428 |
|
|
|
429 |
|
|
/* |
430 |
cebix |
1.1 |
* Packet reception thread |
431 |
|
|
*/ |
432 |
|
|
|
433 |
|
|
static void *receive_func(void *arg) |
434 |
|
|
{ |
435 |
|
|
for (;;) { |
436 |
|
|
|
437 |
|
|
// Wait for packets to arrive |
438 |
|
|
struct pollfd pf = {fd, POLLIN, 0}; |
439 |
|
|
int res = poll(&pf, 1, -1); |
440 |
|
|
if (res <= 0) |
441 |
|
|
break; |
442 |
|
|
|
443 |
|
|
// Trigger Ethernet interrupt |
444 |
|
|
D(bug(" packet received, triggering Ethernet interrupt\n")); |
445 |
|
|
SetInterruptFlag(INTFLAG_ETHER); |
446 |
|
|
TriggerInterrupt(); |
447 |
|
|
|
448 |
|
|
// Wait for interrupt acknowledge by EtherInterrupt() |
449 |
|
|
sem_wait(&int_ack); |
450 |
|
|
} |
451 |
|
|
return NULL; |
452 |
|
|
} |
453 |
|
|
|
454 |
|
|
|
455 |
|
|
/* |
456 |
|
|
* Ethernet interrupt - activate deferred tasks to call IODone or protocol handlers |
457 |
|
|
*/ |
458 |
|
|
|
459 |
|
|
void EtherInterrupt(void) |
460 |
|
|
{ |
461 |
|
|
D(bug("EtherIRQ\n")); |
462 |
|
|
|
463 |
|
|
// Call protocol handler for received packets |
464 |
|
|
uint8 packet[1516]; |
465 |
cebix |
1.2 |
ssize_t length; |
466 |
cebix |
1.1 |
for (;;) { |
467 |
|
|
|
468 |
cebix |
1.2 |
if (udp_tunnel) { |
469 |
|
|
|
470 |
|
|
// Read packet from socket |
471 |
|
|
struct sockaddr_in from; |
472 |
|
|
socklen_t from_len = sizeof(from); |
473 |
|
|
length = recvfrom(fd, packet, 1514, 0, (struct sockaddr *)&from, &from_len); |
474 |
|
|
if (length < 14) |
475 |
|
|
break; |
476 |
|
|
ether_udp_read(packet, length, &from); |
477 |
|
|
|
478 |
|
|
} else { |
479 |
|
|
|
480 |
|
|
// Read packet from sheep_net device |
481 |
cebix |
1.1 |
#if defined(__linux__) |
482 |
gbeauche |
1.9 |
length = read(fd, packet, net_if_type == NET_IF_ETHERTAP ? 1516 : 1514); |
483 |
cebix |
1.1 |
#else |
484 |
cebix |
1.2 |
length = read(fd, packet, 1514); |
485 |
cebix |
1.1 |
#endif |
486 |
cebix |
1.2 |
if (length < 14) |
487 |
|
|
break; |
488 |
cebix |
1.1 |
|
489 |
|
|
#if MONITOR |
490 |
cebix |
1.2 |
bug("Receiving Ethernet packet:\n"); |
491 |
|
|
for (int i=0; i<length; i++) { |
492 |
|
|
bug("%02x ", packet[i]); |
493 |
|
|
} |
494 |
|
|
bug("\n"); |
495 |
cebix |
1.1 |
#endif |
496 |
|
|
|
497 |
cebix |
1.2 |
// Pointer to packet data (Ethernet header) |
498 |
|
|
uint8 *p = packet; |
499 |
cebix |
1.1 |
#if defined(__linux__) |
500 |
gbeauche |
1.9 |
if (net_if_type == NET_IF_ETHERTAP) { |
501 |
cebix |
1.2 |
p += 2; // Linux ethertap has two random bytes before the packet |
502 |
|
|
length -= 2; |
503 |
|
|
} |
504 |
cebix |
1.1 |
#endif |
505 |
|
|
|
506 |
cebix |
1.2 |
// Get packet type |
507 |
|
|
uint16 type = (p[12] << 8) | p[13]; |
508 |
cebix |
1.1 |
|
509 |
cebix |
1.2 |
// Look for protocol |
510 |
cebix |
1.3 |
uint16 search_type = (type <= 1500 ? 0 : type); |
511 |
|
|
if (net_protocols.find(search_type) == net_protocols.end()) |
512 |
cebix |
1.2 |
continue; |
513 |
cebix |
1.3 |
uint32 handler = net_protocols[search_type]; |
514 |
cebix |
1.2 |
|
515 |
|
|
// No default handler |
516 |
cebix |
1.3 |
if (handler == 0) |
517 |
cebix |
1.2 |
continue; |
518 |
|
|
|
519 |
|
|
// Copy header to RHA |
520 |
|
|
Host2Mac_memcpy(ether_data + ed_RHA, p, 14); |
521 |
|
|
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))); |
522 |
|
|
|
523 |
|
|
// Call protocol handler |
524 |
|
|
M68kRegisters r; |
525 |
|
|
r.d[0] = type; // Packet type |
526 |
|
|
r.d[1] = length - 14; // Remaining packet length (without header, for ReadPacket) |
527 |
|
|
r.a[0] = (uint32)p + 14; // Pointer to packet (host address, for ReadPacket) |
528 |
|
|
r.a[3] = ether_data + ed_RHA + 14; // Pointer behind header in RHA |
529 |
|
|
r.a[4] = ether_data + ed_ReadPacket; // Pointer to ReadPacket/ReadRest routines |
530 |
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])); |
531 |
|
|
Execute68k(handler, &r); |
532 |
cebix |
1.2 |
} |
533 |
cebix |
1.1 |
} |
534 |
|
|
|
535 |
|
|
// Acknowledge interrupt to reception thread |
536 |
|
|
D(bug(" EtherIRQ done\n")); |
537 |
|
|
sem_post(&int_ack); |
538 |
|
|
} |