ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/ether_unix.cpp
Revision: 1.10
Committed: 2004-05-09T16:32:12Z (20 years, 6 months ago) by gbeauche
Branch: MAIN
Changes since 1.9: +11 -6 lines
Log Message:
Move and fix tun interface shutdown call

File Contents

# User Rev Content
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(&ether_thread_attr, 1);
106 cebix 1.2 thread_active = (pthread_create(&ether_thread, &ether_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     // 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 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     uint8 packet[1516];
470 cebix 1.2 ssize_t length;
471 cebix 1.1 for (;;) {
472    
473 cebix 1.2 if (udp_tunnel) {
474    
475     // Read packet from socket
476     struct sockaddr_in from;
477     socklen_t from_len = sizeof(from);
478     length = recvfrom(fd, packet, 1514, 0, (struct sockaddr *)&from, &from_len);
479     if (length < 14)
480     break;
481     ether_udp_read(packet, length, &from);
482    
483     } else {
484    
485     // Read packet from sheep_net device
486 cebix 1.1 #if defined(__linux__)
487 gbeauche 1.9 length = read(fd, packet, net_if_type == NET_IF_ETHERTAP ? 1516 : 1514);
488 cebix 1.1 #else
489 cebix 1.2 length = read(fd, packet, 1514);
490 cebix 1.1 #endif
491 cebix 1.2 if (length < 14)
492     break;
493 cebix 1.1
494     #if MONITOR
495 cebix 1.2 bug("Receiving Ethernet packet:\n");
496     for (int i=0; i<length; i++) {
497     bug("%02x ", packet[i]);
498     }
499     bug("\n");
500 cebix 1.1 #endif
501    
502 cebix 1.2 // Pointer to packet data (Ethernet header)
503     uint8 *p = packet;
504 cebix 1.1 #if defined(__linux__)
505 gbeauche 1.9 if (net_if_type == NET_IF_ETHERTAP) {
506 cebix 1.2 p += 2; // Linux ethertap has two random bytes before the packet
507     length -= 2;
508     }
509 cebix 1.1 #endif
510    
511 cebix 1.2 // Get packet type
512     uint16 type = (p[12] << 8) | p[13];
513 cebix 1.1
514 cebix 1.2 // Look for protocol
515 cebix 1.3 uint16 search_type = (type <= 1500 ? 0 : type);
516     if (net_protocols.find(search_type) == net_protocols.end())
517 cebix 1.2 continue;
518 cebix 1.3 uint32 handler = net_protocols[search_type];
519 cebix 1.2
520     // No default handler
521 cebix 1.3 if (handler == 0)
522 cebix 1.2 continue;
523    
524     // Copy header to RHA
525     Host2Mac_memcpy(ether_data + ed_RHA, p, 14);
526     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)));
527    
528     // Call protocol handler
529     M68kRegisters r;
530     r.d[0] = type; // Packet type
531     r.d[1] = length - 14; // Remaining packet length (without header, for ReadPacket)
532     r.a[0] = (uint32)p + 14; // Pointer to packet (host address, for ReadPacket)
533     r.a[3] = ether_data + ed_RHA + 14; // Pointer behind header in RHA
534     r.a[4] = ether_data + ed_ReadPacket; // Pointer to ReadPacket/ReadRest routines
535 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]));
536     Execute68k(handler, &r);
537 cebix 1.2 }
538 cebix 1.1 }
539    
540     // Acknowledge interrupt to reception thread
541     D(bug(" EtherIRQ done\n"));
542     sem_post(&int_ack);
543     }