2 * This file is part of DOS-libpcap
3 * Ported to DOS/DOSX by G. Vanem <gvanem@yahoo.no>
5 * pcap-dos.c: Interface to PKTDRVR, NDIS2 and 32-bit pmode
17 #if defined(USE_32BIT_DRIVERS)
18 #include "msdos/pm_drvr/pmdrvr.h"
19 #include "msdos/pm_drvr/pci.h"
20 #include "msdos/pm_drvr/bios32.h"
21 #include "msdos/pm_drvr/module.h"
22 #include "msdos/pm_drvr/3c501.h"
23 #include "msdos/pm_drvr/3c503.h"
24 #include "msdos/pm_drvr/3c509.h"
25 #include "msdos/pm_drvr/3c59x.h"
26 #include "msdos/pm_drvr/3c515.h"
27 #include "msdos/pm_drvr/3c90x.h"
28 #include "msdos/pm_drvr/3c575_cb.h"
29 #include "msdos/pm_drvr/ne.h"
30 #include "msdos/pm_drvr/wd.h"
31 #include "msdos/pm_drvr/accton.h"
32 #include "msdos/pm_drvr/cs89x0.h"
33 #include "msdos/pm_drvr/rtl8139.h"
34 #include "msdos/pm_drvr/ne2k-pci.h"
40 #include "msdos/pktdrvr.h"
43 #include "msdos/ndis2.h"
46 #include <arpa/inet.h>
48 #include <net/if_arp.h>
49 #include <net/if_ether.h>
50 #include <net/if_packe.h>
53 #if defined(USE_32BIT_DRIVERS)
54 #define FLUSHK() do { _printk_safe = 1; _printk_flush(); } while (0)
55 #define NDIS_NEXT_DEV &rtl8139_dev
57 static char *rx_pool
= NULL
;
58 static void init_32bit (void);
60 static int pktq_init (struct rx_ringbuf
*q
, int size
, int num
, char *pool
);
61 static int pktq_check (struct rx_ringbuf
*q
);
62 static int pktq_inc_out (struct rx_ringbuf
*q
);
63 static int pktq_in_index (struct rx_ringbuf
*q
) LOCKED_FUNC
;
64 static void pktq_clear (struct rx_ringbuf
*q
) LOCKED_FUNC
;
66 static struct rx_elem
*pktq_in_elem (struct rx_ringbuf
*q
) LOCKED_FUNC
;
67 static struct rx_elem
*pktq_out_elem (struct rx_ringbuf
*q
);
70 #define FLUSHK() ((void)0)
71 #define NDIS_NEXT_DEV NULL
75 * Internal variables/functions in Watt-32
77 extern WORD _pktdevclass
;
78 extern BOOL _eth_is_init
;
79 extern int _w32_dynamic_host
;
80 extern int _watt_do_exit
;
81 extern int _watt_is_init
;
82 extern int _w32__bootp_on
, _w32__dhcp_on
, _w32__rarp_on
, _w32__do_mask_req
;
83 extern void (*_w32_usr_post_init
) (void);
84 extern void (*_w32_print_hook
)();
86 extern void dbug_write (const char *); /* Watt-32 lib, pcdbug.c */
87 extern int pkt_get_mtu (void);
89 static int ref_count
= 0;
91 static u_long mac_count
= 0;
92 static u_long filter_count
= 0;
94 static volatile BOOL exc_occured
= 0;
96 static struct device
*handle_to_device
[20];
98 static int pcap_activate_dos (pcap_t
*p
);
99 static int pcap_read_dos (pcap_t
*p
, int cnt
, pcap_handler callback
,
101 static void pcap_cleanup_dos (pcap_t
*p
);
102 static int pcap_stats_dos (pcap_t
*p
, struct pcap_stat
*ps
);
103 static int pcap_sendpacket_dos (pcap_t
*p
, const void *buf
, size_t len
);
104 static int pcap_setfilter_dos (pcap_t
*p
, struct bpf_program
*fp
);
106 static int ndis_probe (struct device
*dev
);
107 static int pkt_probe (struct device
*dev
);
109 static void close_driver (void);
110 static int init_watt32 (struct pcap
*pcap
, const char *dev_name
, char *err_buf
);
111 static int first_init (const char *name
, char *ebuf
, int promisc
);
113 static void watt32_recv_hook (u_char
*dummy
, const struct pcap_pkthdr
*pcap
,
117 * These are the device we always support
119 static struct device ndis_dev
= {
124 NDIS_NEXT_DEV
, /* NULL or a 32-bit device */
128 static struct device pkt_dev
= {
137 static struct device
*get_device (int fd
)
139 if (fd
<= 0 || fd
>= sizeof(handle_to_device
)/sizeof(handle_to_device
[0]))
141 return handle_to_device
[fd
-1];
145 * Private data for capturing on MS-DOS.
148 void (*wait_proc
)(void); /* call proc while waiting */
149 struct pcap_stat stat
;
152 pcap_t
*pcap_create_interface (const char *device
, char *ebuf
)
156 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_dos
));
160 p
->activate_op
= pcap_activate_dos
;
165 * Open MAC-driver with name 'device_name' for live capture of
168 static int pcap_activate_dos (pcap_t
*pcap
)
170 if (pcap
->opt
.rfmon
) {
172 * No monitor mode on DOS.
174 return (PCAP_ERROR_RFMON_NOTSUP
);
177 if (pcap
->snapshot
< ETH_MIN
+8)
178 pcap
->snapshot
= ETH_MIN
+8;
180 if (pcap
->snapshot
> ETH_MAX
) /* silently accept and truncate large MTUs */
181 pcap
->snapshot
= ETH_MAX
;
183 pcap
->linktype
= DLT_EN10MB
; /* !! */
184 pcap
->cleanup_op
= pcap_cleanup_dos
;
185 pcap
->read_op
= pcap_read_dos
;
186 pcap
->stats_op
= pcap_stats_dos
;
187 pcap
->inject_op
= pcap_sendpacket_dos
;
188 pcap
->setfilter_op
= pcap_setfilter_dos
;
189 pcap
->setdirection_op
= NULL
; /* Not implemented.*/
190 pcap
->fd
= ++ref_count
;
192 pcap
->bufsize
= ETH_MAX
+100; /* add some margin */
193 pcap
->buffer
= calloc (pcap
->bufsize
, 1);
195 if (pcap
->fd
== 1) /* first time we're called */
197 if (!init_watt32(pcap
, pcap
->opt
.device
, pcap
->errbuf
) ||
198 !first_init(pcap
->opt
.device
, pcap
->errbuf
, pcap
->opt
.promisc
))
202 atexit (close_driver
);
204 else if (stricmp(active_dev
->name
,pcap
->opt
.device
))
206 pcap_snprintf (pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
207 "Cannot use different devices simultaneously "
208 "(`%s' vs. `%s')", active_dev
->name
, pcap
->opt
.device
);
211 handle_to_device
[pcap
->fd
-1] = active_dev
;
216 * Poll the receiver queue and call the pcap callback-handler
220 pcap_read_one (pcap_t
*p
, pcap_handler callback
, u_char
*data
)
222 struct pcap_dos
*pd
= p
->priv
;
223 struct pcap_pkthdr pcap
;
224 struct timeval now
, expiry
= { 0,0 };
227 if (p
->opt
.timeout
> 0)
229 gettimeofday2 (&now
, NULL
);
230 expiry
.tv_usec
= now
.tv_usec
+ 1000UL * p
->opt
.timeout
;
231 expiry
.tv_sec
= now
.tv_sec
;
232 while (expiry
.tv_usec
>= 1000000L)
234 expiry
.tv_usec
-= 1000000L;
241 volatile struct device
*dev
; /* might be reset by sig_handler */
243 dev
= get_device (p
->fd
);
247 PCAP_ASSERT (dev
->copy_rx_buf
|| dev
->peek_rx_buf
);
250 /* If driver has a zero-copy receive facility, peek at the queue,
251 * filter it, do the callback and release the buffer.
253 if (dev
->peek_rx_buf
)
255 PCAP_ASSERT (dev
->release_rx_buf
);
256 rx_len
= (*dev
->peek_rx_buf
) (&p
->buffer
);
260 rx_len
= (*dev
->copy_rx_buf
) (p
->buffer
, p
->snapshot
);
263 if (rx_len
> 0) /* got a packet */
269 pcap
.caplen
= min (rx_len
, p
->snapshot
);
273 (!p
->fcode
.bf_insns
|| bpf_filter(p
->fcode
.bf_insns
, p
->buffer
, pcap
.len
, pcap
.caplen
)))
277 /* Fix-me!! Should be time of arrival. Not time of
280 gettimeofday2 (&pcap
.ts
, NULL
);
281 (*callback
) (data
, &pcap
, p
->buffer
);
284 if (dev
->release_rx_buf
)
285 (*dev
->release_rx_buf
) (p
->buffer
);
287 if (pcap_pkt_debug
> 0)
289 if (callback
== watt32_recv_hook
)
290 dbug_write ("pcap_recv_hook\n");
291 else dbug_write ("pcap_read_op\n");
297 /* Has "pcap_breakloop()" been called?
301 * Yes - clear the flag that indicates that it
302 * has, and return -2 to indicate that we were
303 * told to break out of the loop.
309 /* If not to wait for a packet or pcap_cleanup_dos() called from
310 * e.g. SIGINT handler, exit loop now.
312 if (p
->opt
.timeout
<= 0 || (volatile int)p
->fd
<= 0)
315 gettimeofday2 (&now
, NULL
);
317 if (timercmp(&now
, &expiry
, >))
321 kbhit(); /* a real CPU hog */
325 (*pd
->wait_proc
)(); /* call yield func */
328 if (rx_len
< 0) /* receive error */
331 #ifdef USE_32BIT_DRIVERS
332 if (pcap_pkt_debug
> 1)
333 printk ("pkt-err %s\n", pktInfo
.error
);
341 pcap_read_dos (pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*data
)
345 while (num
<= cnt
|| PACKET_COUNT_IS_UNLIMITED(cnt
))
349 rc
= pcap_read_one (p
, callback
, data
);
354 _w32_os_yield(); /* allow SIGINT generation, yield to Win95/NT */
360 * Return network statistics
362 static int pcap_stats_dos (pcap_t
*p
, struct pcap_stat
*ps
)
364 struct net_device_stats
*stats
;
366 struct device
*dev
= p
? get_device(p
->fd
) : NULL
;
370 strcpy (p
->errbuf
, "illegal pcap handle");
374 if (!dev
->get_stats
|| (stats
= (*dev
->get_stats
)(dev
)) == NULL
)
376 strcpy (p
->errbuf
, "device statistics not available");
383 pd
->stat
.ps_recv
= stats
->rx_packets
;
384 pd
->stat
.ps_drop
+= stats
->rx_missed_errors
;
385 pd
->stat
.ps_ifdrop
= stats
->rx_dropped
+ /* queue full */
386 stats
->rx_errors
; /* HW errors */
394 * Return detailed network/device statistics.
395 * May be called after 'dev->close' is called.
397 int pcap_stats_ex (pcap_t
*p
, struct pcap_stat_ex
*se
)
399 struct device
*dev
= p
? get_device (p
->fd
) : NULL
;
401 if (!dev
|| !dev
->get_stats
)
403 strlcpy (p
->errbuf
, "detailed device statistics not available",
408 if (!strnicmp(dev
->name
,"pkt",3))
410 strlcpy (p
->errbuf
, "pktdrvr doesn't have detailed statistics",
414 memcpy (se
, (*dev
->get_stats
)(dev
), sizeof(*se
));
419 * Simply store the filter-code for the pcap_read_dos() callback
420 * Some day the filter-code could be handed down to the active
421 * device (pkt_rx1.s or 32-bit device interrupt handler).
423 static int pcap_setfilter_dos (pcap_t
*p
, struct bpf_program
*fp
)
432 * Return # of packets received in pcap_read_dos()
434 u_long
pcap_mac_packets (void)
440 * Return # of packets passed through filter in pcap_read_dos()
442 u_long
pcap_filter_packets (void)
444 return (filter_count
);
448 * Close pcap device. Not called for offline captures.
450 static void pcap_cleanup_dos (pcap_t
*p
)
457 if (pcap_stats(p
,NULL
) < 0)
458 pd
->stat
.ps_drop
= 0;
459 if (!get_device(p
->fd
))
462 handle_to_device
[p
->fd
-1] = NULL
;
473 * Return the name of the 1st network interface,
474 * or NULL if none can be found.
476 char *pcap_lookupdev (char *ebuf
)
480 #ifdef USE_32BIT_DRIVERS
484 for (dev
= (struct device
*)dev_base
; dev
; dev
= dev
->next
)
486 PCAP_ASSERT (dev
->probe
);
488 if ((*dev
->probe
)(dev
))
491 probed_dev
= (struct device
*) dev
; /* remember last probed device */
492 return (char*) dev
->name
;
497 strcpy (ebuf
, "No driver found");
502 * Gets localnet & netmask from Watt-32.
504 int pcap_lookupnet (const char *device
, bpf_u_int32
*localnet
,
505 bpf_u_int32
*netmask
, char *errbuf
)
511 strcpy (errbuf
, "pcap_open_offline() or pcap_activate() must be "
516 mask
= _w32_sin_mask
;
517 net
= my_ip_addr
& mask
;
520 if (IN_CLASSA(*netmask
))
522 else if (IN_CLASSB(*netmask
))
524 else if (IN_CLASSC(*netmask
))
528 pcap_snprintf (errbuf
, PCAP_ERRBUF_SIZE
, "inet class for 0x%lx unknown", mask
);
532 *localnet
= htonl (net
);
533 *netmask
= htonl (mask
);
540 * Get a list of all interfaces that are present and that we probe okay.
541 * Returns -1 on error, 0 otherwise.
542 * The list, as returned through "alldevsp", may be NULL if no interfaces
543 * were up and could be opened.
545 int pcap_platform_finddevs (pcap_if_t
**alldevsp
, char *errbuf
)
548 struct sockaddr_in sa_ll_1
, sa_ll_2
;
549 struct sockaddr
*addr
, *netmask
, *broadaddr
, *dstaddr
;
550 pcap_if_t
*devlist
= NULL
;
552 size_t addr_size
= sizeof(*addr
);
554 for (dev
= (struct device
*)dev_base
; dev
; dev
= dev
->next
)
556 PCAP_ASSERT (dev
->probe
);
558 if (!(*dev
->probe
)(dev
))
561 PCAP_ASSERT (dev
->close
); /* set by probe routine */
565 memset (&sa_ll_1
, 0, sizeof(sa_ll_1
));
566 memset (&sa_ll_2
, 0, sizeof(sa_ll_2
));
567 sa_ll_1
.sin_family
= AF_INET
;
568 sa_ll_2
.sin_family
= AF_INET
;
570 addr
= (struct sockaddr
*) &sa_ll_1
;
571 netmask
= (struct sockaddr
*) &sa_ll_1
;
572 dstaddr
= (struct sockaddr
*) &sa_ll_1
;
573 broadaddr
= (struct sockaddr
*) &sa_ll_2
;
574 memset (&sa_ll_2
.sin_addr
, 0xFF, sizeof(sa_ll_2
.sin_addr
));
576 if (pcap_add_if(&devlist
, dev
->name
, dev
->flags
,
577 dev
->long_name
, errbuf
) < 0)
582 #if 0 /* Pkt drivers should have no addresses */
583 if (add_addr_to_iflist(&devlist
, dev
->name
, dev
->flags
, addr
, addr_size
,
584 netmask
, addr_size
, broadaddr
, addr_size
,
585 dstaddr
, addr_size
, errbuf
) < 0)
593 if (devlist
&& ret
< 0)
595 pcap_freealldevs (devlist
);
600 strcpy (errbuf
, "No drivers found");
607 * pcap_assert() is mainly used for debugging
609 void pcap_assert (const char *what
, const char *file
, unsigned line
)
612 fprintf (stderr
, "%s (%u): Assertion \"%s\" failed\n",
619 * For pcap_offline_read(): wait and yield between printing packets
620 * to simulate the pace packets where actually recorded.
622 void pcap_set_wait (pcap_t
*p
, void (*yield
)(void), int wait
)
626 struct pcap_dos
*pd
= p
->priv
;
628 pd
->wait_proc
= yield
;
629 p
->opt
.timeout
= wait
;
634 * Initialise a named network device.
636 static struct device
*
637 open_driver (const char *dev_name
, char *ebuf
, int promisc
)
641 for (dev
= (struct device
*)dev_base
; dev
; dev
= dev
->next
)
643 PCAP_ASSERT (dev
->name
);
645 if (strcmp (dev_name
,dev
->name
))
648 if (!probed_dev
) /* user didn't call pcap_lookupdev() first */
650 PCAP_ASSERT (dev
->probe
);
652 if (!(*dev
->probe
)(dev
)) /* call the xx_probe() function */
654 pcap_snprintf (ebuf
, PCAP_ERRBUF_SIZE
, "failed to detect device `%s'", dev_name
);
657 probed_dev
= dev
; /* device is probed okay and may be used */
659 else if (dev
!= probed_dev
)
666 /* Select what traffic to receive
669 dev
->flags
|= (IFF_ALLMULTI
| IFF_PROMISC
);
670 else dev
->flags
&= ~(IFF_ALLMULTI
| IFF_PROMISC
);
672 PCAP_ASSERT (dev
->open
);
674 if (!(*dev
->open
)(dev
))
676 pcap_snprintf (ebuf
, PCAP_ERRBUF_SIZE
, "failed to activate device `%s'", dev_name
);
677 if (pktInfo
.error
&& !strncmp(dev
->name
,"pkt",3))
680 strcat (ebuf
, pktInfo
.error
);
685 /* Some devices need this to operate in promiscous mode
687 if (promisc
&& dev
->set_multicast_list
)
688 (*dev
->set_multicast_list
) (dev
);
690 active_dev
= dev
; /* remember our active device */
694 /* 'dev_name' not matched in 'dev_base' list.
698 pcap_snprintf (ebuf
, PCAP_ERRBUF_SIZE
, "device `%s' not supported", dev_name
);
705 pcap_snprintf (ebuf
, PCAP_ERRBUF_SIZE
, "device `%s' not probed", dev_name
);
712 * Deinitialise MAC driver.
713 * Set receive mode back to default mode.
715 static void close_driver (void)
717 /* !!todo: loop over all 'handle_to_device[]' ? */
718 struct device
*dev
= active_dev
;
720 if (dev
&& dev
->close
)
728 #ifdef USE_32BIT_DRIVERS
741 static void setup_signals (void (*handler
)(int))
743 signal (SIGSEGV
,handler
);
744 signal (SIGILL
, handler
);
745 signal (SIGFPE
, handler
);
748 static void exc_handler (int sig
)
750 #ifdef USE_32BIT_DRIVERS
751 if (active_dev
->irq
> 0) /* excludes IRQ 0 */
753 disable_irq (active_dev
->irq
);
754 irq_eoi_cmd (active_dev
->irq
);
762 fputs ("Catching SIGSEGV.\n", stderr
);
765 fputs ("Catching SIGILL.\n", stderr
);
769 fputs ("Catching SIGFPE.\n", stderr
);
772 fprintf (stderr
, "Catching signal %d.\n", sig
);
777 #endif /* __DJGPP__ */
781 * Open the pcap device for the first client calling pcap_activate()
783 static int first_init (const char *name
, char *ebuf
, int promisc
)
787 #ifdef USE_32BIT_DRIVERS
788 rx_pool
= k_calloc (RECEIVE_BUF_SIZE
, RECEIVE_QUEUE_SIZE
);
791 strcpy (ebuf
, "Not enough memory (Rx pool)");
797 setup_signals (exc_handler
);
800 #ifdef USE_32BIT_DRIVERS
804 dev
= open_driver (name
, ebuf
, promisc
);
807 #ifdef USE_32BIT_DRIVERS
813 setup_signals (SIG_DFL
);
818 #ifdef USE_32BIT_DRIVERS
820 * If driver is NOT a 16-bit "pkt/ndis" driver (having a 'copy_rx_buf'
821 * set in it's probe handler), initialise near-memory ring-buffer for
824 if (dev
->copy_rx_buf
== NULL
)
826 dev
->get_rx_buf
= get_rxbuf
;
827 dev
->peek_rx_buf
= peek_rxbuf
;
828 dev
->release_rx_buf
= release_rxbuf
;
829 pktq_init (&dev
->queue
, RECEIVE_BUF_SIZE
, RECEIVE_QUEUE_SIZE
, rx_pool
);
835 #ifdef USE_32BIT_DRIVERS
836 static void init_32bit (void)
838 static int init_pci
= 0;
841 _printk_init (64*1024, NULL
); /* calls atexit(printk_exit) */
844 (void)pci_init(); /* init BIOS32+PCI interface */
851 * Hook functions for using Watt-32 together with pcap
853 static char rxbuf
[ETH_MAX
+100]; /* rx-buffer with some margin */
855 static pcap_t pcap_save
;
857 static void watt32_recv_hook (u_char
*dummy
, const struct pcap_pkthdr
*pcap
,
860 /* Fix me: assumes Ethernet II only */
861 struct ether_header
*ep
= (struct ether_header
*) buf
;
863 memcpy (rxbuf
, buf
, pcap
->caplen
);
864 etype
= ep
->ether_type
;
868 #if (WATTCP_VER >= 0x0224)
870 * This function is used by Watt-32 to poll for a packet.
871 * i.e. it's set to bypass _eth_arrived()
873 static void *pcap_recv_hook (WORD
*type
)
875 int len
= pcap_read_dos (&pcap_save
, 1, watt32_recv_hook
, NULL
);
881 return (void*) &rxbuf
;
885 * This function is called by Watt-32 (via _eth_xmit_hook).
886 * If dbug_init() was called, we should trace packets sent.
888 static int pcap_xmit_hook (const void *buf
, unsigned len
)
892 if (pcap_pkt_debug
> 0)
893 dbug_write ("pcap_xmit_hook: ");
895 if (active_dev
&& active_dev
->xmit
)
896 if ((*active_dev
->xmit
) (active_dev
, buf
, len
) > 0)
899 if (pcap_pkt_debug
> 0)
900 dbug_write (rc
? "ok\n" : "fail\n");
905 static int pcap_sendpacket_dos (pcap_t
*p
, const void *buf
, size_t len
)
907 struct device
*dev
= p
? get_device(p
->fd
) : NULL
;
909 if (!dev
|| !dev
->xmit
)
911 return (*dev
->xmit
) (dev
, buf
, len
);
915 * This function is called by Watt-32 in tcp_post_init().
916 * We should prevent Watt-32 from using BOOTP/DHCP/RARP etc.
918 static void (*prev_post_hook
) (void);
920 static void pcap_init_hook (void)
922 _w32__bootp_on
= _w32__dhcp_on
= _w32__rarp_on
= 0;
923 _w32__do_mask_req
= 0;
924 _w32_dynamic_host
= 0;
930 * Supress PRINT message from Watt-32's sock_init()
932 static void null_print (void) {}
935 * To use features of Watt-32 (netdb functions and socket etc.)
936 * we must call sock_init(). But we set various hooks to prevent
937 * using normal PKTDRVR functions in pcpkt.c. This should hopefully
938 * make Watt-32 and pcap co-operate.
940 static int init_watt32 (struct pcap
*pcap
, const char *dev_name
, char *err_buf
)
943 int rc
, MTU
, has_ip_addr
;
944 int using_pktdrv
= 1;
946 /* If user called sock_init() first, we need to reinit in
947 * order to open debug/trace-file properly
952 env
= getenv ("PCAP_TRACE");
953 if (env
&& atoi(env
) > 0 &&
954 pcap_pkt_debug
< 0) /* if not already set */
957 pcap_pkt_debug
= atoi (env
);
960 _watt_do_exit
= 0; /* prevent sock_init() calling exit() */
961 prev_post_hook
= _w32_usr_post_init
;
962 _w32_usr_post_init
= pcap_init_hook
;
963 _w32_print_hook
= null_print
;
965 if (dev_name
&& strncmp(dev_name
,"pkt",3))
966 using_pktdrv
= FALSE
;
969 has_ip_addr
= (rc
!= 8); /* IP-address assignment failed */
971 /* if pcap is using a 32-bit driver w/o a pktdrvr loaded, we
972 * just pretend Watt-32 is initialised okay.
974 * !! fix-me: The Watt-32 config isn't done if no pktdrvr
975 * was found. In that case my_ip_addr + sin_mask
976 * have default values. Should be taken from another
977 * ini-file/environment in any case (ref. tcpdump.ini)
981 if (!using_pktdrv
|| !has_ip_addr
) /* for now .... */
983 static const char myip
[] = "192.168.0.1";
984 static const char mask
[] = "255.255.255.0";
986 printf ("Just guessing, using IP %s and netmask %s\n", myip
, mask
);
987 my_ip_addr
= aton (myip
);
988 _w32_sin_mask
= aton (mask
);
990 else if (rc
&& using_pktdrv
)
992 pcap_snprintf (err_buf
, PCAP_ERRBUF_SIZE
, "sock_init() failed, code %d", rc
);
996 /* Set recv-hook for peeking in _eth_arrived().
998 #if (WATTCP_VER >= 0x0224)
999 _eth_recv_hook
= pcap_recv_hook
;
1000 _eth_xmit_hook
= pcap_xmit_hook
;
1003 /* Free the pkt-drvr handle allocated in pkt_init().
1004 * The above hooks should thus use the handle reopened in open_driver()
1009 /* _eth_is_init = 1; */ /* hack to get Rx/Tx-hooks in Watt-32 working */
1012 memcpy (&pcap_save
, pcap
, sizeof(pcap_save
));
1013 MTU
= pkt_get_mtu();
1014 pcap_save
.fcode
.bf_insns
= NULL
;
1015 pcap_save
.linktype
= _eth_get_hwtype (NULL
, NULL
);
1016 pcap_save
.snapshot
= MTU
> 0 ? MTU
: ETH_MAX
; /* assume 1514 */
1019 /* prevent use of resolve() and resolve_ip()
1021 last_nameserver
= 0;
1026 int EISA_bus
= 0; /* Where is natural place for this? */
1029 * Application config hooks to set various driver parameters.
1032 static const struct config_table debug_tab
[] = {
1033 { "PKT.DEBUG", ARG_ATOI
, &pcap_pkt_debug
},
1034 { "PKT.VECTOR", ARG_ATOX_W
, NULL
},
1035 { "NDIS.DEBUG", ARG_ATOI
, NULL
},
1036 #ifdef USE_32BIT_DRIVERS
1037 { "3C503.DEBUG", ARG_ATOI
, &ei_debug
},
1038 { "3C503.IO_BASE", ARG_ATOX_W
, &el2_dev
.base_addr
},
1039 { "3C503.MEMORY", ARG_ATOX_W
, &el2_dev
.mem_start
},
1040 { "3C503.IRQ", ARG_ATOI
, &el2_dev
.irq
},
1041 { "3C505.DEBUG", ARG_ATOI
, NULL
},
1042 { "3C505.BASE", ARG_ATOX_W
, NULL
},
1043 { "3C507.DEBUG", ARG_ATOI
, NULL
},
1044 { "3C509.DEBUG", ARG_ATOI
, &el3_debug
},
1045 { "3C509.ILOOP", ARG_ATOI
, &el3_max_loop
},
1046 { "3C529.DEBUG", ARG_ATOI
, NULL
},
1047 { "3C575.DEBUG", ARG_ATOI
, &debug_3c575
},
1048 { "3C59X.DEBUG", ARG_ATOI
, &vortex_debug
},
1049 { "3C59X.IFACE0", ARG_ATOI
, &vortex_options
[0] },
1050 { "3C59X.IFACE1", ARG_ATOI
, &vortex_options
[1] },
1051 { "3C59X.IFACE2", ARG_ATOI
, &vortex_options
[2] },
1052 { "3C59X.IFACE3", ARG_ATOI
, &vortex_options
[3] },
1053 { "3C90X.DEBUG", ARG_ATOX_W
, &tc90xbc_debug
},
1054 { "ACCT.DEBUG", ARG_ATOI
, ðpk_debug
},
1055 { "CS89.DEBUG", ARG_ATOI
, &cs89_debug
},
1056 { "RTL8139.DEBUG", ARG_ATOI
, &rtl8139_debug
},
1057 /* { "RTL8139.FDUPLEX", ARG_ATOI, &rtl8139_options }, */
1058 { "SMC.DEBUG", ARG_ATOI
, &ei_debug
},
1059 /* { "E100.DEBUG", ARG_ATOI, &e100_debug }, */
1060 { "PCI.DEBUG", ARG_ATOI
, &pci_debug
},
1061 { "BIOS32.DEBUG", ARG_ATOI
, &bios32_debug
},
1062 { "IRQ.DEBUG", ARG_ATOI
, &irq_debug
},
1063 { "TIMER.IRQ", ARG_ATOI
, &timer_irq
},
1069 * pcap_config_hook() is an extension to application's config
1070 * handling. Uses Watt-32's config-table function.
1072 int pcap_config_hook (const char *keyword
, const char *value
)
1074 return parse_config_table (debug_tab
, NULL
, keyword
, value
);
1078 * Linked list of supported devices
1080 struct device
*active_dev
= NULL
; /* the device we have opened */
1081 struct device
*probed_dev
= NULL
; /* the device we have probed */
1082 const struct device
*dev_base
= &pkt_dev
; /* list of network devices */
1085 * PKTDRVR device functions
1087 int pcap_pkt_debug
= -1;
1089 static void pkt_close (struct device
*dev
)
1091 BOOL okay
= PktExitDriver();
1093 if (pcap_pkt_debug
> 1)
1094 fprintf (stderr
, "pkt_close(): %d\n", okay
);
1101 static int pkt_open (struct device
*dev
)
1105 if (dev
->flags
& IFF_PROMISC
)
1106 mode
= PDRX_ALL_PACKETS
;
1107 else mode
= PDRX_BROADCAST
;
1109 if (!PktInitDriver(mode
))
1112 PktResetStatistics (pktInfo
.handle
);
1113 PktQueueBusy (FALSE
);
1117 static int pkt_xmit (struct device
*dev
, const void *buf
, int len
)
1119 struct net_device_stats
*stats
= (struct net_device_stats
*) dev
->priv
;
1121 if (pcap_pkt_debug
> 0)
1122 dbug_write ("pcap_xmit\n");
1124 if (!PktTransmit(buf
,len
))
1132 static void *pkt_stats (struct device
*dev
)
1134 struct net_device_stats
*stats
= (struct net_device_stats
*) dev
->priv
;
1136 if (!stats
|| !PktSessStatistics(pktInfo
.handle
))
1139 stats
->rx_packets
= pktStat
.inPackets
;
1140 stats
->rx_errors
= pktStat
.lost
;
1141 stats
->rx_missed_errors
= PktRxDropped();
1145 static int pkt_probe (struct device
*dev
)
1147 if (!PktSearchDriver())
1150 dev
->open
= pkt_open
;
1151 dev
->xmit
= pkt_xmit
;
1152 dev
->close
= pkt_close
;
1153 dev
->get_stats
= pkt_stats
;
1154 dev
->copy_rx_buf
= PktReceive
; /* farmem peek and copy routine */
1155 dev
->get_rx_buf
= NULL
;
1156 dev
->peek_rx_buf
= NULL
;
1157 dev
->release_rx_buf
= NULL
;
1158 dev
->priv
= calloc (sizeof(struct net_device_stats
), 1);
1165 * NDIS device functions
1167 static void ndis_close (struct device
*dev
)
1175 static int ndis_open (struct device
*dev
)
1177 int promis
= (dev
->flags
& IFF_PROMISC
);
1180 if (!NdisInit(promis
))
1189 static void *ndis_stats (struct device
*dev
)
1191 static struct net_device_stats stats
;
1198 static int ndis_probe (struct device
*dev
)
1205 dev
->open
= ndis_open
;
1207 dev
->close
= ndis_close
;
1208 dev
->get_stats
= ndis_stats
;
1209 dev
->copy_rx_buf
= NULL
; /* to-do */
1210 dev
->get_rx_buf
= NULL
; /* upcall is from rmode driver */
1211 dev
->peek_rx_buf
= NULL
;
1212 dev
->release_rx_buf
= NULL
;
1217 * Search & probe for supported 32-bit (pmode) pcap devices
1219 #if defined(USE_32BIT_DRIVERS)
1221 struct device el2_dev LOCKED_VAR
= {
1230 struct device el3_dev LOCKED_VAR
= {
1239 struct device tc515_dev LOCKED_VAR
= {
1248 struct device tc59_dev LOCKED_VAR
= {
1257 struct device tc90xbc_dev LOCKED_VAR
= {
1266 struct device wd_dev LOCKED_VAR
= {
1275 struct device ne_dev LOCKED_VAR
= {
1284 struct device acct_dev LOCKED_VAR
= {
1286 "Accton EtherPocket",
1293 struct device cs89_dev LOCKED_VAR
= {
1295 "Crystal Semiconductor",
1302 struct device rtl8139_dev LOCKED_VAR
= {
1308 rtl8139_probe
/* dev->probe routine */
1312 * Dequeue routine is called by polling.
1313 * NOTE: the queue-element is not copied, only a pointer is
1314 * returned at '*buf'
1316 int peek_rxbuf (BYTE
**buf
)
1318 struct rx_elem
*tail
, *head
;
1320 PCAP_ASSERT (pktq_check (&active_dev
->queue
));
1323 tail
= pktq_out_elem (&active_dev
->queue
);
1324 head
= pktq_in_elem (&active_dev
->queue
);
1329 PCAP_ASSERT (tail
->size
< active_dev
->queue
.elem_size
-4-2);
1331 *buf
= &tail
->data
[0];
1332 return (tail
->size
);
1339 * Release buffer we peeked at above.
1341 int release_rxbuf (BYTE
*buf
)
1344 struct rx_elem
*tail
= pktq_out_elem (&active_dev
->queue
);
1346 PCAP_ASSERT (&tail
->data
[0] == buf
);
1350 pktq_inc_out (&active_dev
->queue
);
1355 * get_rxbuf() routine (in locked code) is called from IRQ handler
1356 * to request a buffer. Interrupts are disabled and we have a 32kB stack.
1358 BYTE
*get_rxbuf (int len
)
1362 if (len
< ETH_MIN
|| len
> ETH_MAX
)
1365 idx
= pktq_in_index (&active_dev
->queue
);
1369 static int fan_idx LOCKED_VAR
= 0;
1370 writew ("-\\|/"[fan_idx
++] | (15 << 8), /* white on black colour */
1371 0xB8000 + 2*79); /* upper-right corner, 80-col colour screen */
1374 /* writew (idx + '0' + 0x0F00, 0xB8000 + 2*78); */
1377 if (idx
!= active_dev
->queue
.out_index
)
1379 struct rx_elem
*head
= pktq_in_elem (&active_dev
->queue
);
1382 active_dev
->queue
.in_index
= idx
;
1383 return (&head
->data
[0]);
1386 /* !!to-do: drop 25% of the oldest element
1388 pktq_clear (&active_dev
->queue
);
1393 * Simple ring-buffer queue handler for reception of packets
1394 * from network driver.
1396 #define PKTQ_MARKER 0xDEADBEEF
1398 static int pktq_check (struct rx_ringbuf
*q
)
1405 if (!q
|| !q
->num_elem
|| !q
->buf_start
)
1411 for (i
= 0; i
< q
->num_elem
; i
++)
1413 buf
+= q
->elem_size
;
1414 if (*(DWORD
*)(buf
- sizeof(DWORD
)) != PKTQ_MARKER
)
1421 static int pktq_init (struct rx_ringbuf
*q
, int size
, int num
, char *pool
)
1425 q
->elem_size
= size
;
1427 q
->buf_start
= pool
;
1431 PCAP_ASSERT (size
>= sizeof(struct rx_elem
) + sizeof(DWORD
));
1435 for (i
= 0; i
< num
; i
++)
1438 struct rx_elem
*elem
= (struct rx_elem
*) pool
;
1440 /* assert dword aligned elements
1442 PCAP_ASSERT (((unsigned)(&elem
->data
[0]) & 3) == 0);
1445 *(DWORD
*) (pool
- sizeof(DWORD
)) = PKTQ_MARKER
;
1451 * Increment the queue 'out_index' (tail).
1454 static int pktq_inc_out (struct rx_ringbuf
*q
)
1457 if (q
->out_index
>= q
->num_elem
)
1459 return (q
->out_index
);
1463 * Return the queue's next 'in_index' (head).
1466 static int pktq_in_index (struct rx_ringbuf
*q
)
1468 volatile int index
= q
->in_index
+ 1;
1470 if (index
>= q
->num_elem
)
1476 * Return the queue's head-buffer.
1478 static struct rx_elem
*pktq_in_elem (struct rx_ringbuf
*q
)
1480 return (struct rx_elem
*) (q
->buf_start
+ (q
->elem_size
* q
->in_index
));
1484 * Return the queue's tail-buffer.
1486 static struct rx_elem
*pktq_out_elem (struct rx_ringbuf
*q
)
1488 return (struct rx_elem
*) (q
->buf_start
+ (q
->elem_size
* q
->out_index
));
1492 * Clear the queue ring-buffer by setting head=tail.
1494 static void pktq_clear (struct rx_ringbuf
*q
)
1496 q
->in_index
= q
->out_index
;
1500 * Symbols that must be linkable for "gcc -O0"
1508 #include "msdos/pm_drvr/ioport.h"
1509 #include "msdos/pm_drvr/dma.h"
1511 #endif /* USE_32BIT_DRIVERS */