1 |
|
/* |
2 |
|
* ether_unix.cpp - Ethernet device driver, Unix specific stuff (Linux and FreeBSD) |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-2001 Christian Bauer |
4 |
> |
* Basilisk II (C) 1997-2004 Christian Bauer |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
23 |
|
#include <sys/ioctl.h> |
24 |
|
#include <sys/poll.h> |
25 |
|
#include <sys/socket.h> |
26 |
+ |
#include <sys/wait.h> |
27 |
|
#include <netinet/in.h> |
28 |
|
#include <pthread.h> |
29 |
|
#include <semaphore.h> |
35 |
|
#include <net/if.h> |
36 |
|
#endif |
37 |
|
|
38 |
+ |
#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 |
|
#include "cpu_emulation.h" |
49 |
|
#include "main.h" |
50 |
|
#include "macos_util.h" |
63 |
|
#define MONITOR 0 |
64 |
|
|
65 |
|
|
66 |
+ |
// 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 |
|
// 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 |
61 |
– |
static bool is_ethertap; // Flag: Ethernet device is ethertap |
82 |
|
static bool udp_tunnel; // Flag: UDP tunnelling active, fd is the socket descriptor |
83 |
+ |
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 |
|
|
87 |
|
// Attached network protocols, maps protocol type to MacOS handler address |
88 |
|
static map<uint16, uint32> net_protocols; |
102 |
|
return false; |
103 |
|
} |
104 |
|
|
105 |
< |
pthread_attr_init(ðer_thread_attr); |
83 |
< |
#if defined(_POSIX_THREAD_PRIORITY_SCHEDULING) |
84 |
< |
if (geteuid() == 0) { |
85 |
< |
pthread_attr_setinheritsched(ðer_thread_attr, PTHREAD_EXPLICIT_SCHED); |
86 |
< |
pthread_attr_setschedpolicy(ðer_thread_attr, SCHED_FIFO); |
87 |
< |
struct sched_param fifo_param; |
88 |
< |
fifo_param.sched_priority = (sched_get_priority_min(SCHED_FIFO) + sched_get_priority_max(SCHED_FIFO)) / 2 + 1; |
89 |
< |
pthread_attr_setschedparam(ðer_thread_attr, &fifo_param); |
90 |
< |
} |
91 |
< |
#endif |
92 |
< |
|
105 |
> |
Set_pthread_attr(ðer_thread_attr, 1); |
106 |
|
thread_active = (pthread_create(ðer_thread, ðer_thread_attr, receive_func, NULL) == 0); |
107 |
|
if (!thread_active) { |
108 |
|
printf("WARNING: Cannot start Ethernet thread"); |
129 |
|
|
130 |
|
|
131 |
|
/* |
132 |
+ |
* 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 |
|
* Initialization |
162 |
|
*/ |
163 |
|
|
171 |
|
if (name == NULL) |
172 |
|
return false; |
173 |
|
|
174 |
< |
// Is it Ethertap? |
175 |
< |
is_ethertap = (strncmp(name, "tap", 3) == 0); |
174 |
> |
// 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 |
|
|
185 |
|
// Open sheep_net or ethertap device |
186 |
|
char dev_name[16]; |
187 |
< |
if (is_ethertap) |
187 |
> |
switch (net_if_type) { |
188 |
> |
case NET_IF_ETHERTAP: |
189 |
|
sprintf(dev_name, "/dev/%s", name); |
190 |
< |
else |
190 |
> |
break; |
191 |
> |
case NET_IF_TUNTAP: |
192 |
> |
sprintf(dev_name, "/dev/net/tun", name); |
193 |
> |
break; |
194 |
> |
case NET_IF_SHEEPNET: |
195 |
|
strcpy(dev_name, "/dev/sheep_net"); |
196 |
+ |
break; |
197 |
+ |
} |
198 |
|
fd = open(dev_name, O_RDWR); |
199 |
|
if (fd < 0) { |
200 |
|
sprintf(str, GetString(STR_NO_SHEEP_NET_DRIVER_WARN), dev_name, strerror(errno)); |
202 |
|
goto open_error; |
203 |
|
} |
204 |
|
|
205 |
+ |
#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 |
|
#if defined(__linux__) |
240 |
|
// Attach sheep_net to selected Ethernet card |
241 |
< |
if (!is_ethertap && ioctl(fd, SIOCSIFLINK, name) < 0) { |
241 |
> |
if (net_if_type == NET_IF_SHEEPNET && ioctl(fd, SIOCSIFLINK, name) < 0) { |
242 |
|
sprintf(str, GetString(STR_SHEEP_NET_ATTACH_WARN), strerror(errno)); |
243 |
|
WarningAlert(str); |
244 |
|
goto open_error; |
249 |
|
ioctl(fd, FIONBIO, &nonblock); |
250 |
|
|
251 |
|
// Get Ethernet address |
252 |
< |
if (is_ethertap) { |
252 |
> |
if (net_if_type == NET_IF_ETHERTAP) { |
253 |
|
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; |
271 |
|
open_error: |
272 |
|
stop_thread(); |
273 |
|
|
274 |
+ |
if (net_if_type == NET_IF_TUNTAP) |
275 |
+ |
execute_network_script("down"); |
276 |
+ |
|
277 |
|
if (fd > 0) { |
278 |
|
close(fd); |
279 |
|
fd = -1; |
318 |
|
|
319 |
|
int16 ether_add_multicast(uint32 pb) |
320 |
|
{ |
321 |
< |
if (ioctl(fd, SIOCADDMULTI, Mac2HostAddr(pb + eMultiAddr)) < 0) { |
321 |
> |
if (net_if_type != NET_IF_TUNTAP && ioctl(fd, SIOCADDMULTI, Mac2HostAddr(pb + eMultiAddr)) < 0) { |
322 |
|
D(bug("WARNING: Couldn't enable multicast address\n")); |
323 |
< |
if (is_ethertap) |
323 |
> |
if (net_if_type == NET_IF_ETHERTAP) |
324 |
|
return noErr; |
325 |
|
else |
326 |
|
return eMultiErr; |
335 |
|
|
336 |
|
int16 ether_del_multicast(uint32 pb) |
337 |
|
{ |
338 |
< |
if (ioctl(fd, SIOCDELMULTI, Mac2HostAddr(pb + eMultiAddr)) < 0) { |
338 |
> |
if (net_if_type != NET_IF_TUNTAP && ioctl(fd, SIOCDELMULTI, Mac2HostAddr(pb + eMultiAddr)) < 0) { |
339 |
|
D(bug("WARNING: Couldn't disable multicast address\n")); |
340 |
|
return eMultiErr; |
341 |
|
} else |
378 |
|
uint8 packet[1516], *p = packet; |
379 |
|
int len = 0; |
380 |
|
#if defined(__linux__) |
381 |
< |
if (is_ethertap) { |
381 |
> |
if (net_if_type == NET_IF_ETHERTAP) { |
382 |
|
*p++ = 0; // Linux ethertap discards the first 2 bytes |
383 |
|
*p++ = 0; |
384 |
|
len += 2; |
479 |
|
|
480 |
|
// Read packet from sheep_net device |
481 |
|
#if defined(__linux__) |
482 |
< |
length = read(fd, packet, is_ethertap ? 1516 : 1514); |
482 |
> |
length = read(fd, packet, net_if_type == NET_IF_ETHERTAP ? 1516 : 1514); |
483 |
|
#else |
484 |
|
length = read(fd, packet, 1514); |
485 |
|
#endif |
497 |
|
// Pointer to packet data (Ethernet header) |
498 |
|
uint8 *p = packet; |
499 |
|
#if defined(__linux__) |
500 |
< |
if (is_ethertap) { |
500 |
> |
if (net_if_type == NET_IF_ETHERTAP) { |
501 |
|
p += 2; // Linux ethertap has two random bytes before the packet |
502 |
|
length -= 2; |
503 |
|
} |