2 * This file is part of DOS-libpcap
3 * Ported to DOS/DOSX by G. Vanem <giva@bgnett.no>
5 * pcap-dos.c: Interface to PKTDRVR, NDIS2 and 32-bit pmode
8 * @(#) $Header: /tcpdump/master/libpcap/pcap-dos.c,v 1.2.2.3 2008-04-14 20:41:51 guy Exp $ (LBL)
19 #if defined(USE_32BIT_DRIVERS)
20 #include "msdos/pm_drvr/pmdrvr.h"
21 #include "msdos/pm_drvr/pci.h"
22 #include "msdos/pm_drvr/bios32.h"
23 #include "msdos/pm_drvr/module.h"
24 #include "msdos/pm_drvr/3c501.h"
25 #include "msdos/pm_drvr/3c503.h"
26 #include "msdos/pm_drvr/3c509.h"
27 #include "msdos/pm_drvr/3c59x.h"
28 #include "msdos/pm_drvr/3c515.h"
29 #include "msdos/pm_drvr/3c90x.h"
30 #include "msdos/pm_drvr/3c575_cb.h"
31 #include "msdos/pm_drvr/ne.h"
32 #include "msdos/pm_drvr/wd.h"
33 #include "msdos/pm_drvr/accton.h"
34 #include "msdos/pm_drvr/cs89x0.h"
35 #include "msdos/pm_drvr/rtl8139.h"
36 #include "msdos/pm_drvr/ne2k-pci.h"
42 #include "msdos/pktdrvr.h"
45 #include "msdos/ndis2.h"
48 #include <arpa/inet.h>
50 #include <net/if_arp.h>
51 #include <net/if_ether.h>
52 #include <net/if_packe.h>
55 #if defined(USE_32BIT_DRIVERS)
56 #define FLUSHK() do { _printk_safe = 1; _printk_flush(); } while (0)
57 #define NDIS_NEXT_DEV &rtl8139_dev
59 static char *rx_pool
= NULL
;
60 static void init_32bit (void);
62 static int pktq_init (struct rx_ringbuf
*q
, int size
, int num
, char *pool
);
63 static int pktq_check (struct rx_ringbuf
*q
);
64 static int pktq_inc_out (struct rx_ringbuf
*q
);
65 static int pktq_in_index (struct rx_ringbuf
*q
) LOCKED_FUNC
;
66 static void pktq_clear (struct rx_ringbuf
*q
) LOCKED_FUNC
;
68 static struct rx_elem
*pktq_in_elem (struct rx_ringbuf
*q
) LOCKED_FUNC
;
69 static struct rx_elem
*pktq_out_elem (struct rx_ringbuf
*q
);
72 #define FLUSHK() ((void)0)
73 #define NDIS_NEXT_DEV NULL
77 * Internal variables/functions in Watt-32
79 extern WORD _pktdevclass
;
80 extern BOOL _eth_is_init
;
81 extern int _w32_dynamic_host
;
82 extern int _watt_do_exit
;
83 extern int _watt_is_init
;
84 extern int _w32__bootp_on
, _w32__dhcp_on
, _w32__rarp_on
, _w32__do_mask_req
;
85 extern void (*_w32_usr_post_init
) (void);
86 extern void (*_w32_print_hook
)();
88 extern void dbug_write (const char *); /* Watt-32 lib, pcdbug.c */
89 extern int pkt_get_mtu (void);
91 static int ref_count
= 0;
93 static u_long mac_count
= 0;
94 static u_long filter_count
= 0;
96 static volatile BOOL exc_occured
= 0;
98 static struct device
*handle_to_device
[20];
100 static void pcap_activate_dos (pcap_t
*p
);
101 static int pcap_read_dos (pcap_t
*p
, int cnt
, pcap_handler callback
,
103 static void pcap_close_dos (pcap_t
*p
);
104 static int pcap_stats_dos (pcap_t
*p
, struct pcap_stat
*ps
);
105 static int pcap_sendpacket_dos (pcap_t
*p
, const void *buf
, size_t len
);
106 static int pcap_setfilter_dos (pcap_t
*p
, struct bpf_program
*fp
);
108 static int ndis_probe (struct device
*dev
);
109 static int pkt_probe (struct device
*dev
);
111 static void close_driver (void);
112 static int init_watt32 (struct pcap
*pcap
, const char *dev_name
, char *err_buf
);
113 static int first_init (const char *name
, char *ebuf
, int promisc
);
115 static void watt32_recv_hook (u_char
*dummy
, const struct pcap_pkthdr
*pcap
,
119 * These are the device we always support
121 static struct device ndis_dev
= {
126 NDIS_NEXT_DEV
, /* NULL or a 32-bit device */
130 static struct device pkt_dev
= {
139 static struct device
*get_device (int fd
)
141 if (fd
<= 0 || fd
>= sizeof(handle_to_device
)/sizeof(handle_to_device
[0]))
143 return handle_to_device
[fd
-1];
146 pcap_t
*pcap_create (const char *device
, char *ebuf
)
150 p
= pcap_create_common(device
, ebuf
);
154 p
->activate_op
= pcap_activate_dos
;
159 * Open MAC-driver with name 'device_name' for live capture of
162 static int pcap_activate_dos (pcap_t
*pcap
)
168 * No monitor mode on DOS.
170 return (PCAP_ERROR_RFMON_NOTSUP
);
173 if (pcap
->snapshot
< ETH_MIN
+8)
174 pcap
->snapshot
= ETH_MIN
+8;
176 if (pcap
->snapshot
> ETH_MAX
) /* silently accept and truncate large MTUs */
177 pcap
->snapshot
= ETH_MAX
;
179 pcap
->linktype
= DLT_EN10MB
; /* !! */
180 pcap
->cleanup_op
= pcap_cleanup_dos
;
181 pcap
->read_op
= pcap_read_dos
;
182 pcap
->stats_op
= pcap_stats_dos
;
183 pcap
->inject_op
= pcap_sendpacket_dos
;
184 pcap
->setfilter_op
= pcap_setfilter_dos
;
185 pcap
->setdirection_op
= NULL
; /* Not implemented.*/
186 pcap
->fd
= ++ref_count
;
188 if (pcap
->fd
== 1) /* first time we're called */
190 if (!init_watt32(pcap
, pcap
->md
.device
, pcap
->errbuf
) ||
191 !first_init(pcap
->md
.device
, pcap
->errbuf
, pcap
->opt
.promisc
))
196 atexit (close_driver
);
198 else if (stricmp(active_dev
->name
,pcap
->md
.device
))
200 snprintf (pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
201 "Cannot use different devices simultaneously "
202 "(`%s' vs. `%s')", active_dev
->name
, pcap
->md
.device
);
206 handle_to_device
[pcap
->fd
-1] = active_dev
;
211 * Poll the receiver queue and call the pcap callback-handler
215 pcap_read_one (pcap_t
*p
, pcap_handler callback
, u_char
*data
)
217 struct pcap_pkthdr pcap
;
218 struct timeval now
, expiry
;
222 if (p
->md
.timeout
> 0)
224 gettimeofday2 (&now
, NULL
);
225 expiry
.tv_usec
= now
.tv_usec
+ 1000UL * p
->md
.timeout
;
226 expiry
.tv_sec
= now
.tv_sec
;
227 while (expiry
.tv_usec
>= 1000000L)
229 expiry
.tv_usec
-= 1000000L;
236 volatile struct device
*dev
; /* might be reset by sig_handler */
238 dev
= get_device (p
->fd
);
242 PCAP_ASSERT (dev
->copy_rx_buf
|| dev
->peek_rx_buf
);
245 /* If driver has a zero-copy receive facility, peek at the queue,
246 * filter it, do the callback and release the buffer.
248 if (dev
->peek_rx_buf
)
250 PCAP_ASSERT (dev
->release_rx_buf
);
251 rx_len
= (*dev
->peek_rx_buf
) (&rx_buf
);
255 BYTE buf
[ETH_MAX
+100]; /* add some margin */
256 rx_len
= (*dev
->copy_rx_buf
) (buf
, p
->snapshot
);
260 if (rx_len
> 0) /* got a packet */
266 pcap
.caplen
= min (rx_len
, p
->snapshot
);
270 (!p
->fcode
.bf_insns
|| bpf_filter(p
->fcode
.bf_insns
, rx_buf
, pcap
.len
, pcap
.caplen
)))
274 /* Fix-me!! Should be time of arrival. Not time of
277 gettimeofday2 (&pcap
.ts
, NULL
);
278 (*callback
) (data
, &pcap
, rx_buf
);
281 if (dev
->release_rx_buf
)
282 (*dev
->release_rx_buf
) (rx_buf
);
284 if (pcap_pkt_debug
> 0)
286 if (callback
== watt32_recv_hook
)
287 dbug_write ("pcap_recv_hook\n");
288 else dbug_write ("pcap_read_op\n");
294 /* If not to wait for a packet or pcap_close() called from
295 * e.g. SIGINT handler, exit loop now.
297 if (p
->md
.timeout
<= 0 || (volatile int)p
->fd
<= 0)
300 gettimeofday2 (&now
, NULL
);
302 if (timercmp(&now
, &expiry
, >))
306 kbhit(); /* a real CPU hog */
310 (*p
->wait_proc
)(); /* call yield func */
313 if (rx_len
< 0) /* receive error */
315 p
->md
.stat
.ps_drop
++;
316 #ifdef USE_32BIT_DRIVERS
317 if (pcap_pkt_debug
> 1)
318 printk ("pkt-err %s\n", pktInfo
.error
);
326 pcap_read_dos (pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*data
)
330 while (num
<= cnt
|| (cnt
< 0))
334 rc
= pcap_read_one (p
, callback
, data
);
339 _w32_os_yield(); /* allow SIGINT generation, yield to Win95/NT */
345 * Return network statistics
347 static int pcap_stats_dos (pcap_t
*p
, struct pcap_stat
*ps
)
349 struct net_device_stats
*stats
;
350 struct device
*dev
= p
? get_device(p
->fd
) : NULL
;
354 strcpy (p
->errbuf
, "illegal pcap handle");
358 if (!dev
->get_stats
|| (stats
= (*dev
->get_stats
)(dev
)) == NULL
)
360 strcpy (p
->errbuf
, "device statistics not available");
366 p
->md
.stat
.ps_recv
= stats
->rx_packets
;
367 p
->md
.stat
.ps_drop
+= stats
->rx_missed_errors
;
368 p
->md
.stat
.ps_ifdrop
= stats
->rx_dropped
+ /* queue full */
369 stats
->rx_errors
; /* HW errors */
377 * Return detailed network/device statistics.
378 * May be called after 'dev->close' is called.
380 int pcap_stats_ex (pcap_t
*p
, struct pcap_stat_ex
*se
)
382 struct device
*dev
= p
? get_device (p
->fd
) : NULL
;
384 if (!dev
|| !dev
->get_stats
)
386 strlcpy (p
->errbuf
, "detailed device statistics not available",
391 if (!strnicmp(dev
->name
,"pkt",3))
393 strlcpy (p
->errbuf
, "pktdrvr doesn't have detailed statistics",
397 memcpy (se
, (*dev
->get_stats
)(dev
), sizeof(*se
));
402 * Simply store the filter-code for the pcap_read_dos() callback
403 * Some day the filter-code could be handed down to the active
404 * device (pkt_rx1.s or 32-bit device interrupt handler).
406 static int pcap_setfilter_dos (pcap_t
*p
, struct bpf_program
*fp
)
415 * Return # of packets received in pcap_read_dos()
417 u_long
pcap_mac_packets (void)
423 * Return # of packets passed through filter in pcap_read_dos()
425 u_long
pcap_filter_packets (void)
427 return (filter_count
);
431 * Close pcap device. Not called for offline captures.
433 static void pcap_cleanup_dos (pcap_t
*p
)
435 if (p
&& !exc_occured
)
437 if (pcap_stats(p
,NULL
) < 0)
438 p
->md
.stat
.ps_drop
= 0;
439 if (!get_device(p
->fd
))
442 handle_to_device
[p
->fd
-1] = NULL
;
453 * Return the name of the 1st network interface,
454 * or NULL if none can be found.
456 char *pcap_lookupdev (char *ebuf
)
460 #ifdef USE_32BIT_DRIVERS
464 for (dev
= (struct device
*)dev_base
; dev
; dev
= dev
->next
)
466 PCAP_ASSERT (dev
->probe
);
468 if ((*dev
->probe
)(dev
))
471 probed_dev
= (struct device
*) dev
; /* remember last probed device */
472 return (char*) dev
->name
;
477 strcpy (ebuf
, "No driver found");
482 * Gets localnet & netmask from Watt-32.
484 int pcap_lookupnet (const char *device
, bpf_u_int32
*localnet
,
485 bpf_u_int32
*netmask
, char *errbuf
)
489 strcpy (errbuf
, "pcap_open_offline() or pcap_activate() must be "
494 *netmask
= _w32_sin_mask
;
495 *localnet
= my_ip_addr
& *netmask
;
498 if (IN_CLASSA(*netmask
))
499 *localnet
= IN_CLASSA_NET
;
500 else if (IN_CLASSB(*netmask
))
501 *localnet
= IN_CLASSB_NET
;
502 else if (IN_CLASSC(*netmask
))
503 *localnet
= IN_CLASSC_NET
;
506 sprintf (errbuf
, "inet class for 0x%lx unknown", *netmask
);
515 * Get a list of all interfaces that are present and that we probe okay.
516 * Returns -1 on error, 0 otherwise.
517 * The list, as returned through "alldevsp", may be null if no interfaces
518 * were up and could be opened.
520 int pcap_findalldevs (pcap_if_t
**alldevsp
, char *errbuf
)
523 struct sockaddr_ll sa_ll_1
, sa_ll_2
;
524 struct sockaddr
*addr
, *netmask
, *broadaddr
, *dstaddr
;
525 pcap_if_t
*devlist
= NULL
;
527 size_t addr_size
= sizeof(struct sockaddr_ll
);
529 for (dev
= (struct device
*)dev_base
; dev
; dev
= dev
->next
)
531 PCAP_ASSERT (dev
->probe
);
533 if (!(*dev
->probe
)(dev
))
536 PCAP_ASSERT (dev
->close
); /* set by probe routine */
540 memset (&sa_ll_1
, 0, sizeof(sa_ll_1
));
541 memset (&sa_ll_2
, 0, sizeof(sa_ll_2
));
542 sa_ll_1
.sll_family
= AF_PACKET
;
543 sa_ll_2
.sll_family
= AF_PACKET
;
545 addr
= (struct sockaddr
*) &sa_ll_1
;
546 netmask
= (struct sockaddr
*) &sa_ll_1
;
547 dstaddr
= (struct sockaddr
*) &sa_ll_1
;
548 broadaddr
= (struct sockaddr
*) &sa_ll_2
;
549 memset (&sa_ll_2
.sll_addr
, 0xFF, sizeof(sa_ll_2
.sll_addr
));
551 if (pcap_add_if(&devlist
, dev
->name
, dev
->flags
,
552 dev
->long_name
, errbuf
) < 0)
557 if (add_addr_to_iflist(&devlist
,dev
->name
, dev
->flags
, addr
, addr_size
,
558 netmask
, addr_size
, broadaddr
, addr_size
,
559 dstaddr
, addr_size
, errbuf
) < 0)
566 if (devlist
&& ret
< 0)
568 pcap_freealldevs (devlist
);
573 strcpy (errbuf
, "No drivers found");
580 * pcap_assert() is mainly used for debugging
582 void pcap_assert (const char *what
, const char *file
, unsigned line
)
585 fprintf (stderr
, "%s (%u): Assertion \"%s\" failed\n",
592 * For pcap_offline_read(): wait and yield between printing packets
593 * to simulate the pace packets where actually recorded.
595 void pcap_set_wait (pcap_t
*p
, void (*yield
)(void), int wait
)
599 p
->wait_proc
= yield
;
600 p
->md
.timeout
= wait
;
605 * Initialise a named network device.
607 static struct device
*
608 open_driver (const char *dev_name
, char *ebuf
, int promisc
)
612 for (dev
= (struct device
*)dev_base
; dev
; dev
= dev
->next
)
614 PCAP_ASSERT (dev
->name
);
616 if (strcmp (dev_name
,dev
->name
))
619 if (!probed_dev
) /* user didn't call pcap_lookupdev() first */
621 PCAP_ASSERT (dev
->probe
);
623 if (!(*dev
->probe
)(dev
)) /* call the xx_probe() function */
625 sprintf (ebuf
, "failed to detect device `%s'", dev_name
);
628 probed_dev
= dev
; /* device is probed okay and may be used */
630 else if (dev
!= probed_dev
)
637 /* Select what traffic to receive
640 dev
->flags
|= (IFF_ALLMULTI
| IFF_PROMISC
);
641 else dev
->flags
&= ~(IFF_ALLMULTI
| IFF_PROMISC
);
643 PCAP_ASSERT (dev
->open
);
645 if (!(*dev
->open
)(dev
))
647 sprintf (ebuf
, "failed to activate device `%s'", dev_name
);
648 if (pktInfo
.error
&& !strncmp(dev
->name
,"pkt",3))
651 strcat (ebuf
, pktInfo
.error
);
656 /* Some devices need this to operate in promiscous mode
658 if (promisc
&& dev
->set_multicast_list
)
659 (*dev
->set_multicast_list
) (dev
);
661 active_dev
= dev
; /* remember our active device */
665 /* 'dev_name' not matched in 'dev_base' list.
669 sprintf (ebuf
, "device `%s' not supported", dev_name
);
676 sprintf (ebuf
, "device `%s' not probed", dev_name
);
683 * Deinitialise MAC driver.
684 * Set receive mode back to default mode.
686 static void close_driver (void)
688 /* !!todo: loop over all 'handle_to_device[]' ? */
689 struct device
*dev
= active_dev
;
691 if (dev
&& dev
->close
)
699 #ifdef USE_32BIT_DRIVERS
712 static void setup_signals (void (*handler
)(int))
714 signal (SIGSEGV
,handler
);
715 signal (SIGILL
, handler
);
716 signal (SIGFPE
, handler
);
719 static void exc_handler (int sig
)
721 #ifdef USE_32BIT_DRIVERS
722 if (active_dev
->irq
> 0) /* excludes IRQ 0 */
724 disable_irq (active_dev
->irq
);
725 irq_eoi_cmd (active_dev
->irq
);
733 fputs ("Catching SIGSEGV.\n", stderr
);
736 fputs ("Catching SIGILL.\n", stderr
);
740 fputs ("Catching SIGFPE.\n", stderr
);
743 fprintf (stderr
, "Catching signal %d.\n", sig
);
746 pcap_close_dos (NULL
);
748 #endif /* __DJGPP__ */
752 * Open the pcap device for the first client calling pcap_activate()
754 static int first_init (const char *name
, char *ebuf
, int promisc
)
758 #ifdef USE_32BIT_DRIVERS
759 rx_pool
= k_calloc (RECEIVE_BUF_SIZE
, RECEIVE_QUEUE_SIZE
);
762 strcpy (ebuf
, "Not enough memory (Rx pool)");
768 setup_signals (exc_handler
);
771 #ifdef USE_32BIT_DRIVERS
775 dev
= open_driver (name
, ebuf
, promisc
);
778 #ifdef USE_32BIT_DRIVERS
784 setup_signals (SIG_DFL
);
789 #ifdef USE_32BIT_DRIVERS
791 * If driver is NOT a 16-bit "pkt/ndis" driver (having a 'copy_rx_buf'
792 * set in it's probe handler), initialise near-memory ring-buffer for
795 if (dev
->copy_rx_buf
== NULL
)
797 dev
->get_rx_buf
= get_rxbuf
;
798 dev
->peek_rx_buf
= peek_rxbuf
;
799 dev
->release_rx_buf
= release_rxbuf
;
800 pktq_init (&dev
->queue
, RECEIVE_BUF_SIZE
, RECEIVE_QUEUE_SIZE
, rx_pool
);
806 #ifdef USE_32BIT_DRIVERS
807 static void init_32bit (void)
809 static int init_pci
= 0;
812 _printk_init (64*1024, NULL
); /* calls atexit(printk_exit) */
815 (void)pci_init(); /* init BIOS32+PCI interface */
822 * Hook functions for using Watt-32 together with pcap
824 static char rxbuf
[ETH_MAX
+100]; /* rx-buffer with some margin */
826 static pcap_t pcap_save
;
828 static void watt32_recv_hook (u_char
*dummy
, const struct pcap_pkthdr
*pcap
,
831 /* Fix me: assumes Ethernet II only */
832 struct ether_header
*ep
= (struct ether_header
*) buf
;
834 memcpy (rxbuf
, buf
, pcap
->caplen
);
835 etype
= ep
->ether_type
;
839 #if (WATTCP_VER >= 0x0224)
841 * This function is used by Watt-32 to poll for a packet.
842 * i.e. it's set to bypass _eth_arrived()
844 static void *pcap_recv_hook (WORD
*type
)
846 int len
= pcap_read_dos (&pcap_save
, 1, watt32_recv_hook
, NULL
);
852 return (void*) &rxbuf
;
856 * This function is called by Watt-32 (via _eth_xmit_hook).
857 * If dbug_init() was called, we should trace packets sent.
859 static int pcap_xmit_hook (const void *buf
, unsigned len
)
863 if (pcap_pkt_debug
> 0)
864 dbug_write ("pcap_xmit_hook: ");
866 if (active_dev
&& active_dev
->xmit
)
867 if ((*active_dev
->xmit
) (active_dev
, buf
, len
) > 0)
870 if (pcap_pkt_debug
> 0)
871 dbug_write (rc
? "ok\n" : "fail\n");
876 static int pcap_sendpacket_dos (pcap_t
*p
, const void *buf
, size_t len
)
878 struct device
*dev
= p
? get_device(p
->fd
) : NULL
;
880 if (!dev
|| !dev
->xmit
)
882 return (*dev
->xmit
) (dev
, buf
, len
);
886 * This function is called by Watt-32 in tcp_post_init().
887 * We should prevent Watt-32 from using BOOTP/DHCP/RARP etc.
889 static void (*prev_post_hook
) (void);
891 static void pcap_init_hook (void)
893 _w32__bootp_on
= _w32__dhcp_on
= _w32__rarp_on
= 0;
894 _w32__do_mask_req
= 0;
895 _w32_dynamic_host
= 0;
901 * Supress PRINT message from Watt-32's sock_init()
903 static void null_print (void) {}
906 * To use features of Watt-32 (netdb functions and socket etc.)
907 * we must call sock_init(). But we set various hooks to prevent
908 * using normal PKTDRVR functions in pcpkt.c. This should hopefully
909 * make Watt-32 and pcap co-operate.
911 static int init_watt32 (struct pcap
*pcap
, const char *dev_name
, char *err_buf
)
914 int rc
, MTU
, has_ip_addr
;
915 int using_pktdrv
= 1;
917 /* If user called sock_init() first, we need to reinit in
918 * order to open debug/trace-file properly
923 env
= getenv ("PCAP_DEBUG");
924 if (env
&& atoi(env
) > 0 &&
925 pcap_pkt_debug
< 0) /* if not already set */
928 pcap_pkt_debug
= atoi (env
);
931 _watt_do_exit
= 0; /* prevent sock_init() calling exit() */
932 prev_post_hook
= _w32_usr_post_init
;
933 _w32_usr_post_init
= pcap_init_hook
;
934 _w32_print_hook
= null_print
;
936 if (dev_name
&& strncmp(dev_name
,"pkt",3))
937 using_pktdrv
= FALSE
;
940 has_ip_addr
= (rc
!= 8); /* IP-address assignment failed */
942 /* if pcap is using a 32-bit driver w/o a pktdrvr loaded, we
943 * just pretend Watt-32 is initialised okay.
945 * !! fix-me: The Watt-32 config isn't done if no pktdrvr
946 * was found. In that case my_ip_addr + sin_mask
947 * have default values. Should be taken from another
948 * ini-file/environment in any case (ref. tcpdump.ini)
952 if (!using_pktdrv
|| !has_ip_addr
) /* for now .... */
954 static const char myip
[] = "192.168.0.1";
955 static const char mask
[] = "255.255.255.0";
957 printf ("Just guessing, using IP %s and netmask %s\n", myip
, mask
);
958 my_ip_addr
= aton (myip
);
959 _w32_sin_mask
= aton (mask
);
961 else if (rc
&& using_pktdrv
)
963 sprintf (err_buf
, "sock_init() failed, code %d", rc
);
967 /* Set recv-hook for peeking in _eth_arrived().
969 #if (WATTCP_VER >= 0x0224)
970 _eth_recv_hook
= pcap_recv_hook
;
971 _eth_xmit_hook
= pcap_xmit_hook
;
974 /* Free the pkt-drvr handle allocated in pkt_init().
975 * The above hooks should thus use the handle reopened in open_driver()
980 /* _eth_is_init = 1; */ /* hack to get Rx/Tx-hooks in Watt-32 working */
983 memcpy (&pcap_save
, pcap
, sizeof(pcap_save
));
985 pcap_save
.fcode
.bf_insns
= NULL
;
986 pcap_save
.linktype
= _eth_get_hwtype (NULL
, NULL
);
987 pcap_save
.snapshot
= MTU
> 0 ? MTU
: ETH_MAX
; /* assume 1514 */
990 /* prevent use of resolve() and resolve_ip()
997 int EISA_bus
= 0; /* Where is natural place for this? */
1000 * Application config hooks to set various driver parameters.
1003 static struct config_table debug_tab
[] = {
1004 { "PKT.DEBUG", ARG_ATOI
, &pcap_pkt_debug
},
1005 { "PKT.VECTOR", ARG_ATOX_W
, NULL
},
1006 { "NDIS.DEBUG", ARG_ATOI
, NULL
},
1007 #ifdef USE_32BIT_DRIVERS
1008 { "3C503.DEBUG", ARG_ATOI
, &ei_debug
},
1009 { "3C503.IO_BASE", ARG_ATOX_W
, &el2_dev
.base_addr
},
1010 { "3C503.MEMORY", ARG_ATOX_W
, &el2_dev
.mem_start
},
1011 { "3C503.IRQ", ARG_ATOI
, &el2_dev
.irq
},
1012 { "3C505.DEBUG", ARG_ATOI
, NULL
},
1013 { "3C505.BASE", ARG_ATOX_W
, NULL
},
1014 { "3C507.DEBUG", ARG_ATOI
, NULL
},
1015 { "3C509.DEBUG", ARG_ATOI
, &el3_debug
},
1016 { "3C509.ILOOP", ARG_ATOI
, &el3_max_loop
},
1017 { "3C529.DEBUG", ARG_ATOI
, NULL
},
1018 { "3C575.DEBUG", ARG_ATOI
, &debug_3c575
},
1019 { "3C59X.DEBUG", ARG_ATOI
, &vortex_debug
},
1020 { "3C59X.IFACE0", ARG_ATOI
, &vortex_options
[0] },
1021 { "3C59X.IFACE1", ARG_ATOI
, &vortex_options
[1] },
1022 { "3C59X.IFACE2", ARG_ATOI
, &vortex_options
[2] },
1023 { "3C59X.IFACE3", ARG_ATOI
, &vortex_options
[3] },
1024 { "3C90X.DEBUG", ARG_ATOX_W
, &tc90xbc_debug
},
1025 { "ACCT.DEBUG", ARG_ATOI
, ðpk_debug
},
1026 { "CS89.DEBUG", ARG_ATOI
, &cs89_debug
},
1027 { "RTL8139.DEBUG", ARG_ATOI
, &rtl8139_debug
},
1028 /* { "RTL8139.FDUPLEX", ARG_ATOI, &rtl8139_options }, */
1029 { "SMC.DEBUG", ARG_ATOI
, &ei_debug
},
1030 /* { "E100.DEBUG", ARG_ATOI, &e100_debug }, */
1031 { "PCI.DEBUG", ARG_ATOI
, &pci_debug
},
1032 { "BIOS32.DEBUG", ARG_ATOI
, &bios32_debug
},
1033 { "IRQ.DEBUG", ARG_ATOI
, &irq_debug
},
1034 { "TIMER.IRQ", ARG_ATOI
, &timer_irq
},
1040 * pcap_config_hook() is an extension to application's config
1041 * handling. Uses Watt-32's config-table function.
1043 int pcap_config_hook (const char *name
, const char *value
)
1045 return parse_config_table (debug_tab
, NULL
, name
, value
);
1049 * Linked list of supported devices
1051 struct device
*active_dev
= NULL
; /* the device we have opened */
1052 struct device
*probed_dev
= NULL
; /* the device we have probed */
1053 const struct device
*dev_base
= &pkt_dev
; /* list of network devices */
1056 * PKTDRVR device functions
1058 int pcap_pkt_debug
= -1;
1060 static void pkt_close (struct device
*dev
)
1062 BOOL okay
= PktExitDriver();
1064 if (pcap_pkt_debug
> 1)
1065 fprintf (stderr
, "pkt_close(): %d\n", okay
);
1072 static int pkt_open (struct device
*dev
)
1076 if (dev
->flags
& IFF_PROMISC
)
1077 mode
= PDRX_ALL_PACKETS
;
1078 else mode
= PDRX_BROADCAST
;
1080 if (!PktInitDriver(mode
))
1083 PktResetStatistics (pktInfo
.handle
);
1084 PktQueueBusy (FALSE
);
1088 static int pkt_xmit (struct device
*dev
, const void *buf
, int len
)
1090 struct net_device_stats
*stats
= (struct net_device_stats
*) dev
->priv
;
1092 if (pcap_pkt_debug
> 0)
1093 dbug_write ("pcap_xmit\n");
1095 if (!PktTransmit(buf
,len
))
1103 static void *pkt_stats (struct device
*dev
)
1105 struct net_device_stats
*stats
= (struct net_device_stats
*) dev
->priv
;
1107 if (!stats
|| !PktSessStatistics(pktInfo
.handle
))
1110 stats
->rx_packets
= pktStat
.inPackets
;
1111 stats
->rx_errors
= pktStat
.lost
;
1112 stats
->rx_missed_errors
= PktRxDropped();
1116 static int pkt_probe (struct device
*dev
)
1118 if (!PktSearchDriver())
1121 dev
->open
= pkt_open
;
1122 dev
->xmit
= pkt_xmit
;
1123 dev
->close
= pkt_close
;
1124 dev
->get_stats
= pkt_stats
;
1125 dev
->copy_rx_buf
= PktReceive
; /* farmem peek and copy routine */
1126 dev
->get_rx_buf
= NULL
;
1127 dev
->peek_rx_buf
= NULL
;
1128 dev
->release_rx_buf
= NULL
;
1129 dev
->priv
= calloc (sizeof(struct net_device_stats
), 1);
1136 * NDIS device functions
1138 static void ndis_close (struct device
*dev
)
1146 static int ndis_open (struct device
*dev
)
1148 int promis
= (dev
->flags
& IFF_PROMISC
);
1151 if (!NdisInit(promis
))
1160 static void *ndis_stats (struct device
*dev
)
1162 static struct net_device_stats stats
;
1169 static int ndis_probe (struct device
*dev
)
1176 dev
->open
= ndis_open
;
1178 dev
->close
= ndis_close
;
1179 dev
->get_stats
= ndis_stats
;
1180 dev
->copy_rx_buf
= NULL
; /* to-do */
1181 dev
->get_rx_buf
= NULL
; /* upcall is from rmode driver */
1182 dev
->peek_rx_buf
= NULL
;
1183 dev
->release_rx_buf
= NULL
;
1188 * Search & probe for supported 32-bit (pmode) pcap devices
1190 #if defined(USE_32BIT_DRIVERS)
1192 struct device el2_dev LOCKED_VAR
= {
1201 struct device el3_dev LOCKED_VAR
= {
1210 struct device tc515_dev LOCKED_VAR
= {
1219 struct device tc59_dev LOCKED_VAR
= {
1228 struct device tc90xbc_dev LOCKED_VAR
= {
1237 struct device wd_dev LOCKED_VAR
= {
1246 struct device ne_dev LOCKED_VAR
= {
1255 struct device acct_dev LOCKED_VAR
= {
1257 "Accton EtherPocket",
1264 struct device cs89_dev LOCKED_VAR
= {
1266 "Crystal Semiconductor",
1273 struct device rtl8139_dev LOCKED_VAR
= {
1279 rtl8139_probe
/* dev->probe routine */
1283 * Dequeue routine is called by polling.
1284 * NOTE: the queue-element is not copied, only a pointer is
1285 * returned at '*buf'
1287 int peek_rxbuf (BYTE
**buf
)
1289 struct rx_elem
*tail
, *head
;
1291 PCAP_ASSERT (pktq_check (&active_dev
->queue
));
1294 tail
= pktq_out_elem (&active_dev
->queue
);
1295 head
= pktq_in_elem (&active_dev
->queue
);
1300 PCAP_ASSERT (tail
->size
< active_dev
->queue
.elem_size
-4-2);
1302 *buf
= &tail
->data
[0];
1303 return (tail
->size
);
1310 * Release buffer we peeked at above.
1312 int release_rxbuf (BYTE
*buf
)
1315 struct rx_elem
*tail
= pktq_out_elem (&active_dev
->queue
);
1317 PCAP_ASSERT (&tail
->data
[0] == buf
);
1321 pktq_inc_out (&active_dev
->queue
);
1326 * get_rxbuf() routine (in locked code) is called from IRQ handler
1327 * to request a buffer. Interrupts are disabled and we have a 32kB stack.
1329 BYTE
*get_rxbuf (int len
)
1333 if (len
< ETH_MIN
|| len
> ETH_MAX
)
1336 idx
= pktq_in_index (&active_dev
->queue
);
1340 static int fan_idx LOCKED_VAR
= 0;
1341 writew ("-\\|/"[fan_idx
++] | (15 << 8), /* white on black colour */
1342 0xB8000 + 2*79); /* upper-right corner, 80-col colour screen */
1345 /* writew (idx + '0' + 0x0F00, 0xB8000 + 2*78); */
1348 if (idx
!= active_dev
->queue
.out_index
)
1350 struct rx_elem
*head
= pktq_in_elem (&active_dev
->queue
);
1353 active_dev
->queue
.in_index
= idx
;
1354 return (&head
->data
[0]);
1357 /* !!to-do: drop 25% of the oldest element
1359 pktq_clear (&active_dev
->queue
);
1364 * Simple ring-buffer queue handler for reception of packets
1365 * from network driver.
1367 #define PKTQ_MARKER 0xDEADBEEF
1369 static int pktq_check (struct rx_ringbuf
*q
)
1376 if (!q
|| !q
->num_elem
|| !q
->buf_start
)
1382 for (i
= 0; i
< q
->num_elem
; i
++)
1384 buf
+= q
->elem_size
;
1385 if (*(DWORD
*)(buf
- sizeof(DWORD
)) != PKTQ_MARKER
)
1392 static int pktq_init (struct rx_ringbuf
*q
, int size
, int num
, char *pool
)
1396 q
->elem_size
= size
;
1398 q
->buf_start
= pool
;
1402 PCAP_ASSERT (size
>= sizeof(struct rx_elem
) + sizeof(DWORD
));
1406 for (i
= 0; i
< num
; i
++)
1409 struct rx_elem
*elem
= (struct rx_elem
*) pool
;
1411 /* assert dword aligned elements
1413 PCAP_ASSERT (((unsigned)(&elem
->data
[0]) & 3) == 0);
1416 *(DWORD
*) (pool
- sizeof(DWORD
)) = PKTQ_MARKER
;
1422 * Increment the queue 'out_index' (tail).
1425 static int pktq_inc_out (struct rx_ringbuf
*q
)
1428 if (q
->out_index
>= q
->num_elem
)
1430 return (q
->out_index
);
1434 * Return the queue's next 'in_index' (head).
1437 static int pktq_in_index (struct rx_ringbuf
*q
)
1439 volatile int index
= q
->in_index
+ 1;
1441 if (index
>= q
->num_elem
)
1447 * Return the queue's head-buffer.
1449 static struct rx_elem
*pktq_in_elem (struct rx_ringbuf
*q
)
1451 return (struct rx_elem
*) (q
->buf_start
+ (q
->elem_size
* q
->in_index
));
1455 * Return the queue's tail-buffer.
1457 static struct rx_elem
*pktq_out_elem (struct rx_ringbuf
*q
)
1459 return (struct rx_elem
*) (q
->buf_start
+ (q
->elem_size
* q
->out_index
));
1463 * Clear the queue ring-buffer by setting head=tail.
1465 static void pktq_clear (struct rx_ringbuf
*q
)
1467 q
->in_index
= q
->out_index
;
1471 * Symbols that must be linkable for "gcc -O0"
1479 #include "msdos/pm_drvr/ioport.h"
1480 #include "msdos/pm_drvr/dma.h"
1482 #endif /* USE_32BIT_DRIVERS */