ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/ether_unix.cpp
Revision: 1.27
Committed: 2006-01-24T23:46:19Z (18 years, 5 months ago) by gbeauche
Branch: MAIN
Changes since 1.26: +14 -1 lines
Log Message:
Use the most portable POSIX-style non-blocking I/O (O_NONBLOCK) instead of
BSD-style through FIONBIO. It turns out Tru64 and probably IRIX don't support
the latter when fd is a pipe (slirp case).

File Contents

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