2 * This file is part of DOS-libpcap
3 * Ported to DOS/DOSX by G. Vanem <gvanem@broadpark.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 struct pcap_dos
*pcapd
= pcap
->priv
;
172 if (pcap
->opt
.rfmon
) {
174 * No monitor mode on DOS.
176 return (PCAP_ERROR_RFMON_NOTSUP
);
179 if (pcap
->snapshot
< ETH_MIN
+8)
180 pcap
->snapshot
= ETH_MIN
+8;
182 if (pcap
->snapshot
> ETH_MAX
) /* silently accept and truncate large MTUs */
183 pcap
->snapshot
= ETH_MAX
;
185 pcap
->linktype
= DLT_EN10MB
; /* !! */
186 pcap
->cleanup_op
= pcap_cleanup_dos
;
187 pcap
->read_op
= pcap_read_dos
;
188 pcap
->stats_op
= pcap_stats_dos
;
189 pcap
->inject_op
= pcap_sendpacket_dos
;
190 pcap
->setfilter_op
= pcap_setfilter_dos
;
191 pcap
->setdirection_op
= NULL
; /* Not implemented.*/
192 pcap
->fd
= ++ref_count
;
194 if (pcap
->fd
== 1) /* first time we're called */
196 if (!init_watt32(pcap
, pcap
->opt
.source
, pcap
->errbuf
) ||
197 !first_init(pcap
->opt
.source
, pcap
->errbuf
, pcap
->opt
.promisc
))
201 atexit (close_driver
);
203 else if (stricmp(active_dev
->name
,pcap
->opt
.source
))
205 snprintf (pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
206 "Cannot use different devices simultaneously "
207 "(`%s' vs. `%s')", active_dev
->name
, pcap
->opt
.source
);
210 handle_to_device
[pcap
->fd
-1] = active_dev
;
215 * Poll the receiver queue and call the pcap callback-handler
219 pcap_read_one (pcap_t
*p
, pcap_handler callback
, u_char
*data
)
221 struct pcap_dos
*pd
= p
->priv
;
222 struct pcap_pkthdr pcap
;
223 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
) (&rx_buf
);
260 BYTE buf
[ETH_MAX
+100]; /* add some margin */
261 rx_len
= (*dev
->copy_rx_buf
) (buf
, p
->snapshot
);
265 if (rx_len
> 0) /* got a packet */
271 pcap
.caplen
= min (rx_len
, p
->snapshot
);
275 (!p
->fcode
.bf_insns
|| bpf_filter(p
->fcode
.bf_insns
, rx_buf
, pcap
.len
, pcap
.caplen
)))
279 /* Fix-me!! Should be time of arrival. Not time of
282 gettimeofday2 (&pcap
.ts
, NULL
);
283 (*callback
) (data
, &pcap
, rx_buf
);
286 if (dev
->release_rx_buf
)
287 (*dev
->release_rx_buf
) (rx_buf
);
289 if (pcap_pkt_debug
> 0)
291 if (callback
== watt32_recv_hook
)
292 dbug_write ("pcap_recv_hook\n");
293 else dbug_write ("pcap_read_op\n");
299 /* If not to wait for a packet or pcap_cleanup_dos() called from
300 * e.g. SIGINT handler, exit loop now.
302 if (p
->opt
.timeout
<= 0 || (volatile int)p
->fd
<= 0)
305 gettimeofday2 (&now
, NULL
);
307 if (timercmp(&now
, &expiry
, >))
311 kbhit(); /* a real CPU hog */
315 (*p
->wait_proc
)(); /* call yield func */
318 if (rx_len
< 0) /* receive error */
321 #ifdef USE_32BIT_DRIVERS
322 if (pcap_pkt_debug
> 1)
323 printk ("pkt-err %s\n", pktInfo
.error
);
331 pcap_read_dos (pcap_t
*p
, int cnt
, pcap_handler callback
, u_char
*data
)
333 struct pcap_dos
*pd
= p
->priv
;
336 while (num
<= cnt
|| PACKET_COUNT_IS_UNLIMITED(cnt
))
340 rc
= pcap_read_one (p
, callback
, data
);
345 _w32_os_yield(); /* allow SIGINT generation, yield to Win95/NT */
351 * Return network statistics
353 static int pcap_stats_dos (pcap_t
*p
, struct pcap_stat
*ps
)
355 struct net_device_stats
*stats
;
357 struct device
*dev
= p
? get_device(p
->fd
) : NULL
;
361 strcpy (p
->errbuf
, "illegal pcap handle");
365 if (!dev
->get_stats
|| (stats
= (*dev
->get_stats
)(dev
)) == NULL
)
367 strcpy (p
->errbuf
, "device statistics not available");
374 pd
->stat
.ps_recv
= stats
->rx_packets
;
375 pd
->stat
.ps_drop
+= stats
->rx_missed_errors
;
376 pd
->stat
.ps_ifdrop
= stats
->rx_dropped
+ /* queue full */
377 stats
->rx_errors
; /* HW errors */
385 * Return detailed network/device statistics.
386 * May be called after 'dev->close' is called.
388 int pcap_stats_ex (pcap_t
*p
, struct pcap_stat_ex
*se
)
390 struct device
*dev
= p
? get_device (p
->fd
) : NULL
;
392 if (!dev
|| !dev
->get_stats
)
394 strlcpy (p
->errbuf
, "detailed device statistics not available",
399 if (!strnicmp(dev
->name
,"pkt",3))
401 strlcpy (p
->errbuf
, "pktdrvr doesn't have detailed statistics",
405 memcpy (se
, (*dev
->get_stats
)(dev
), sizeof(*se
));
410 * Simply store the filter-code for the pcap_read_dos() callback
411 * Some day the filter-code could be handed down to the active
412 * device (pkt_rx1.s or 32-bit device interrupt handler).
414 static int pcap_setfilter_dos (pcap_t
*p
, struct bpf_program
*fp
)
423 * Return # of packets received in pcap_read_dos()
425 u_long
pcap_mac_packets (void)
431 * Return # of packets passed through filter in pcap_read_dos()
433 u_long
pcap_filter_packets (void)
435 return (filter_count
);
439 * Close pcap device. Not called for offline captures.
441 static void pcap_cleanup_dos (pcap_t
*p
)
445 if (p
&& !exc_occured
)
448 if (pcap_stats(p
,NULL
) < 0)
449 pd
->stat
.ps_drop
= 0;
450 if (!get_device(p
->fd
))
453 handle_to_device
[p
->fd
-1] = NULL
;
464 * Return the name of the 1st network interface,
465 * or NULL if none can be found.
467 char *pcap_lookupdev (char *ebuf
)
471 #ifdef USE_32BIT_DRIVERS
475 for (dev
= (struct device
*)dev_base
; dev
; dev
= dev
->next
)
477 PCAP_ASSERT (dev
->probe
);
479 if ((*dev
->probe
)(dev
))
482 probed_dev
= (struct device
*) dev
; /* remember last probed device */
483 return (char*) dev
->name
;
488 strcpy (ebuf
, "No driver found");
493 * Gets localnet & netmask from Watt-32.
495 int pcap_lookupnet (const char *device
, bpf_u_int32
*localnet
,
496 bpf_u_int32
*netmask
, char *errbuf
)
500 strcpy (errbuf
, "pcap_open_offline() or pcap_activate() must be "
505 *netmask
= _w32_sin_mask
;
506 *localnet
= my_ip_addr
& *netmask
;
509 if (IN_CLASSA(*netmask
))
510 *localnet
= IN_CLASSA_NET
;
511 else if (IN_CLASSB(*netmask
))
512 *localnet
= IN_CLASSB_NET
;
513 else if (IN_CLASSC(*netmask
))
514 *localnet
= IN_CLASSC_NET
;
517 sprintf (errbuf
, "inet class for 0x%lx unknown", *netmask
);
526 * Get a list of all interfaces that are present and that we probe okay.
527 * Returns -1 on error, 0 otherwise.
528 * The list, as returned through "alldevsp", may be null if no interfaces
529 * were up and could be opened.
531 int pcap_findalldevs (pcap_if_t
**alldevsp
, char *errbuf
)
534 struct sockaddr_ll sa_ll_1
, sa_ll_2
;
535 struct sockaddr
*addr
, *netmask
, *broadaddr
, *dstaddr
;
536 pcap_if_t
*devlist
= NULL
;
538 size_t addr_size
= sizeof(struct sockaddr_ll
);
540 for (dev
= (struct device
*)dev_base
; dev
; dev
= dev
->next
)
542 PCAP_ASSERT (dev
->probe
);
544 if (!(*dev
->probe
)(dev
))
547 PCAP_ASSERT (dev
->close
); /* set by probe routine */
551 memset (&sa_ll_1
, 0, sizeof(sa_ll_1
));
552 memset (&sa_ll_2
, 0, sizeof(sa_ll_2
));
553 sa_ll_1
.sll_family
= AF_PACKET
;
554 sa_ll_2
.sll_family
= AF_PACKET
;
556 addr
= (struct sockaddr
*) &sa_ll_1
;
557 netmask
= (struct sockaddr
*) &sa_ll_1
;
558 dstaddr
= (struct sockaddr
*) &sa_ll_1
;
559 broadaddr
= (struct sockaddr
*) &sa_ll_2
;
560 memset (&sa_ll_2
.sll_addr
, 0xFF, sizeof(sa_ll_2
.sll_addr
));
562 if (pcap_add_if(&devlist
, dev
->name
, dev
->flags
,
563 dev
->long_name
, errbuf
) < 0)
568 if (add_addr_to_iflist(&devlist
,dev
->name
, dev
->flags
, addr
, addr_size
,
569 netmask
, addr_size
, broadaddr
, addr_size
,
570 dstaddr
, addr_size
, errbuf
) < 0)
577 if (devlist
&& ret
< 0)
579 pcap_freealldevs (devlist
);
584 strcpy (errbuf
, "No drivers found");
591 * pcap_assert() is mainly used for debugging
593 void pcap_assert (const char *what
, const char *file
, unsigned line
)
596 fprintf (stderr
, "%s (%u): Assertion \"%s\" failed\n",
603 * For pcap_offline_read(): wait and yield between printing packets
604 * to simulate the pace packets where actually recorded.
606 void pcap_set_wait (pcap_t
*p
, void (*yield
)(void), int wait
)
612 pd
->wait_proc
= yield
;
613 p
->opt
.timeout
= wait
;
618 * Initialise a named network device.
620 static struct device
*
621 open_driver (const char *dev_name
, char *ebuf
, int promisc
)
625 for (dev
= (struct device
*)dev_base
; dev
; dev
= dev
->next
)
627 PCAP_ASSERT (dev
->name
);
629 if (strcmp (dev_name
,dev
->name
))
632 if (!probed_dev
) /* user didn't call pcap_lookupdev() first */
634 PCAP_ASSERT (dev
->probe
);
636 if (!(*dev
->probe
)(dev
)) /* call the xx_probe() function */
638 sprintf (ebuf
, "failed to detect device `%s'", dev_name
);
641 probed_dev
= dev
; /* device is probed okay and may be used */
643 else if (dev
!= probed_dev
)
650 /* Select what traffic to receive
653 dev
->flags
|= (IFF_ALLMULTI
| IFF_PROMISC
);
654 else dev
->flags
&= ~(IFF_ALLMULTI
| IFF_PROMISC
);
656 PCAP_ASSERT (dev
->open
);
658 if (!(*dev
->open
)(dev
))
660 sprintf (ebuf
, "failed to activate device `%s'", dev_name
);
661 if (pktInfo
.error
&& !strncmp(dev
->name
,"pkt",3))
664 strcat (ebuf
, pktInfo
.error
);
669 /* Some devices need this to operate in promiscous mode
671 if (promisc
&& dev
->set_multicast_list
)
672 (*dev
->set_multicast_list
) (dev
);
674 active_dev
= dev
; /* remember our active device */
678 /* 'dev_name' not matched in 'dev_base' list.
682 sprintf (ebuf
, "device `%s' not supported", dev_name
);
689 sprintf (ebuf
, "device `%s' not probed", dev_name
);
696 * Deinitialise MAC driver.
697 * Set receive mode back to default mode.
699 static void close_driver (void)
701 /* !!todo: loop over all 'handle_to_device[]' ? */
702 struct device
*dev
= active_dev
;
704 if (dev
&& dev
->close
)
712 #ifdef USE_32BIT_DRIVERS
725 static void setup_signals (void (*handler
)(int))
727 signal (SIGSEGV
,handler
);
728 signal (SIGILL
, handler
);
729 signal (SIGFPE
, handler
);
732 static void exc_handler (int sig
)
734 #ifdef USE_32BIT_DRIVERS
735 if (active_dev
->irq
> 0) /* excludes IRQ 0 */
737 disable_irq (active_dev
->irq
);
738 irq_eoi_cmd (active_dev
->irq
);
746 fputs ("Catching SIGSEGV.\n", stderr
);
749 fputs ("Catching SIGILL.\n", stderr
);
753 fputs ("Catching SIGFPE.\n", stderr
);
756 fprintf (stderr
, "Catching signal %d.\n", sig
);
759 pcap_cleanup_dos (NULL
);
761 #endif /* __DJGPP__ */
765 * Open the pcap device for the first client calling pcap_activate()
767 static int first_init (const char *name
, char *ebuf
, int promisc
)
771 #ifdef USE_32BIT_DRIVERS
772 rx_pool
= k_calloc (RECEIVE_BUF_SIZE
, RECEIVE_QUEUE_SIZE
);
775 strcpy (ebuf
, "Not enough memory (Rx pool)");
781 setup_signals (exc_handler
);
784 #ifdef USE_32BIT_DRIVERS
788 dev
= open_driver (name
, ebuf
, promisc
);
791 #ifdef USE_32BIT_DRIVERS
797 setup_signals (SIG_DFL
);
802 #ifdef USE_32BIT_DRIVERS
804 * If driver is NOT a 16-bit "pkt/ndis" driver (having a 'copy_rx_buf'
805 * set in it's probe handler), initialise near-memory ring-buffer for
808 if (dev
->copy_rx_buf
== NULL
)
810 dev
->get_rx_buf
= get_rxbuf
;
811 dev
->peek_rx_buf
= peek_rxbuf
;
812 dev
->release_rx_buf
= release_rxbuf
;
813 pktq_init (&dev
->queue
, RECEIVE_BUF_SIZE
, RECEIVE_QUEUE_SIZE
, rx_pool
);
819 #ifdef USE_32BIT_DRIVERS
820 static void init_32bit (void)
822 static int init_pci
= 0;
825 _printk_init (64*1024, NULL
); /* calls atexit(printk_exit) */
828 (void)pci_init(); /* init BIOS32+PCI interface */
835 * Hook functions for using Watt-32 together with pcap
837 static char rxbuf
[ETH_MAX
+100]; /* rx-buffer with some margin */
839 static pcap_t pcap_save
;
841 static void watt32_recv_hook (u_char
*dummy
, const struct pcap_pkthdr
*pcap
,
844 /* Fix me: assumes Ethernet II only */
845 struct ether_header
*ep
= (struct ether_header
*) buf
;
847 memcpy (rxbuf
, buf
, pcap
->caplen
);
848 etype
= ep
->ether_type
;
852 #if (WATTCP_VER >= 0x0224)
854 * This function is used by Watt-32 to poll for a packet.
855 * i.e. it's set to bypass _eth_arrived()
857 static void *pcap_recv_hook (WORD
*type
)
859 int len
= pcap_read_dos (&pcap_save
, 1, watt32_recv_hook
, NULL
);
865 return (void*) &rxbuf
;
869 * This function is called by Watt-32 (via _eth_xmit_hook).
870 * If dbug_init() was called, we should trace packets sent.
872 static int pcap_xmit_hook (const void *buf
, unsigned len
)
876 if (pcap_pkt_debug
> 0)
877 dbug_write ("pcap_xmit_hook: ");
879 if (active_dev
&& active_dev
->xmit
)
880 if ((*active_dev
->xmit
) (active_dev
, buf
, len
) > 0)
883 if (pcap_pkt_debug
> 0)
884 dbug_write (rc
? "ok\n" : "fail\n");
889 static int pcap_sendpacket_dos (pcap_t
*p
, const void *buf
, size_t len
)
891 struct device
*dev
= p
? get_device(p
->fd
) : NULL
;
893 if (!dev
|| !dev
->xmit
)
895 return (*dev
->xmit
) (dev
, buf
, len
);
899 * This function is called by Watt-32 in tcp_post_init().
900 * We should prevent Watt-32 from using BOOTP/DHCP/RARP etc.
902 static void (*prev_post_hook
) (void);
904 static void pcap_init_hook (void)
906 _w32__bootp_on
= _w32__dhcp_on
= _w32__rarp_on
= 0;
907 _w32__do_mask_req
= 0;
908 _w32_dynamic_host
= 0;
914 * Supress PRINT message from Watt-32's sock_init()
916 static void null_print (void) {}
919 * To use features of Watt-32 (netdb functions and socket etc.)
920 * we must call sock_init(). But we set various hooks to prevent
921 * using normal PKTDRVR functions in pcpkt.c. This should hopefully
922 * make Watt-32 and pcap co-operate.
924 static int init_watt32 (struct pcap
*pcap
, const char *dev_name
, char *err_buf
)
927 int rc
, MTU
, has_ip_addr
;
928 int using_pktdrv
= 1;
930 /* If user called sock_init() first, we need to reinit in
931 * order to open debug/trace-file properly
936 env
= getenv ("PCAP_DEBUG");
937 if (env
&& atoi(env
) > 0 &&
938 pcap_pkt_debug
< 0) /* if not already set */
941 pcap_pkt_debug
= atoi (env
);
944 _watt_do_exit
= 0; /* prevent sock_init() calling exit() */
945 prev_post_hook
= _w32_usr_post_init
;
946 _w32_usr_post_init
= pcap_init_hook
;
947 _w32_print_hook
= null_print
;
949 if (dev_name
&& strncmp(dev_name
,"pkt",3))
950 using_pktdrv
= FALSE
;
953 has_ip_addr
= (rc
!= 8); /* IP-address assignment failed */
955 /* if pcap is using a 32-bit driver w/o a pktdrvr loaded, we
956 * just pretend Watt-32 is initialised okay.
958 * !! fix-me: The Watt-32 config isn't done if no pktdrvr
959 * was found. In that case my_ip_addr + sin_mask
960 * have default values. Should be taken from another
961 * ini-file/environment in any case (ref. tcpdump.ini)
965 if (!using_pktdrv
|| !has_ip_addr
) /* for now .... */
967 static const char myip
[] = "192.168.0.1";
968 static const char mask
[] = "255.255.255.0";
970 printf ("Just guessing, using IP %s and netmask %s\n", myip
, mask
);
971 my_ip_addr
= aton (myip
);
972 _w32_sin_mask
= aton (mask
);
974 else if (rc
&& using_pktdrv
)
976 sprintf (err_buf
, "sock_init() failed, code %d", rc
);
980 /* Set recv-hook for peeking in _eth_arrived().
982 #if (WATTCP_VER >= 0x0224)
983 _eth_recv_hook
= pcap_recv_hook
;
984 _eth_xmit_hook
= pcap_xmit_hook
;
987 /* Free the pkt-drvr handle allocated in pkt_init().
988 * The above hooks should thus use the handle reopened in open_driver()
993 /* _eth_is_init = 1; */ /* hack to get Rx/Tx-hooks in Watt-32 working */
996 memcpy (&pcap_save
, pcap
, sizeof(pcap_save
));
998 pcap_save
.fcode
.bf_insns
= NULL
;
999 pcap_save
.linktype
= _eth_get_hwtype (NULL
, NULL
);
1000 pcap_save
.snapshot
= MTU
> 0 ? MTU
: ETH_MAX
; /* assume 1514 */
1003 /* prevent use of resolve() and resolve_ip()
1005 last_nameserver
= 0;
1010 int EISA_bus
= 0; /* Where is natural place for this? */
1013 * Application config hooks to set various driver parameters.
1016 static const struct config_table debug_tab
[] = {
1017 { "PKT.DEBUG", ARG_ATOI
, &pcap_pkt_debug
},
1018 { "PKT.VECTOR", ARG_ATOX_W
, NULL
},
1019 { "NDIS.DEBUG", ARG_ATOI
, NULL
},
1020 #ifdef USE_32BIT_DRIVERS
1021 { "3C503.DEBUG", ARG_ATOI
, &ei_debug
},
1022 { "3C503.IO_BASE", ARG_ATOX_W
, &el2_dev
.base_addr
},
1023 { "3C503.MEMORY", ARG_ATOX_W
, &el2_dev
.mem_start
},
1024 { "3C503.IRQ", ARG_ATOI
, &el2_dev
.irq
},
1025 { "3C505.DEBUG", ARG_ATOI
, NULL
},
1026 { "3C505.BASE", ARG_ATOX_W
, NULL
},
1027 { "3C507.DEBUG", ARG_ATOI
, NULL
},
1028 { "3C509.DEBUG", ARG_ATOI
, &el3_debug
},
1029 { "3C509.ILOOP", ARG_ATOI
, &el3_max_loop
},
1030 { "3C529.DEBUG", ARG_ATOI
, NULL
},
1031 { "3C575.DEBUG", ARG_ATOI
, &debug_3c575
},
1032 { "3C59X.DEBUG", ARG_ATOI
, &vortex_debug
},
1033 { "3C59X.IFACE0", ARG_ATOI
, &vortex_options
[0] },
1034 { "3C59X.IFACE1", ARG_ATOI
, &vortex_options
[1] },
1035 { "3C59X.IFACE2", ARG_ATOI
, &vortex_options
[2] },
1036 { "3C59X.IFACE3", ARG_ATOI
, &vortex_options
[3] },
1037 { "3C90X.DEBUG", ARG_ATOX_W
, &tc90xbc_debug
},
1038 { "ACCT.DEBUG", ARG_ATOI
, ðpk_debug
},
1039 { "CS89.DEBUG", ARG_ATOI
, &cs89_debug
},
1040 { "RTL8139.DEBUG", ARG_ATOI
, &rtl8139_debug
},
1041 /* { "RTL8139.FDUPLEX", ARG_ATOI, &rtl8139_options }, */
1042 { "SMC.DEBUG", ARG_ATOI
, &ei_debug
},
1043 /* { "E100.DEBUG", ARG_ATOI, &e100_debug }, */
1044 { "PCI.DEBUG", ARG_ATOI
, &pci_debug
},
1045 { "BIOS32.DEBUG", ARG_ATOI
, &bios32_debug
},
1046 { "IRQ.DEBUG", ARG_ATOI
, &irq_debug
},
1047 { "TIMER.IRQ", ARG_ATOI
, &timer_irq
},
1053 * pcap_config_hook() is an extension to application's config
1054 * handling. Uses Watt-32's config-table function.
1056 int pcap_config_hook (const char *name
, const char *value
)
1058 return parse_config_table (debug_tab
, NULL
, name
, value
);
1062 * Linked list of supported devices
1064 struct device
*active_dev
= NULL
; /* the device we have opened */
1065 struct device
*probed_dev
= NULL
; /* the device we have probed */
1066 const struct device
*dev_base
= &pkt_dev
; /* list of network devices */
1069 * PKTDRVR device functions
1071 int pcap_pkt_debug
= -1;
1073 static void pkt_close (struct device
*dev
)
1075 BOOL okay
= PktExitDriver();
1077 if (pcap_pkt_debug
> 1)
1078 fprintf (stderr
, "pkt_close(): %d\n", okay
);
1085 static int pkt_open (struct device
*dev
)
1089 if (dev
->flags
& IFF_PROMISC
)
1090 mode
= PDRX_ALL_PACKETS
;
1091 else mode
= PDRX_BROADCAST
;
1093 if (!PktInitDriver(mode
))
1096 PktResetStatistics (pktInfo
.handle
);
1097 PktQueueBusy (FALSE
);
1101 static int pkt_xmit (struct device
*dev
, const void *buf
, int len
)
1103 struct net_device_stats
*stats
= (struct net_device_stats
*) dev
->priv
;
1105 if (pcap_pkt_debug
> 0)
1106 dbug_write ("pcap_xmit\n");
1108 if (!PktTransmit(buf
,len
))
1116 static void *pkt_stats (struct device
*dev
)
1118 struct net_device_stats
*stats
= (struct net_device_stats
*) dev
->priv
;
1120 if (!stats
|| !PktSessStatistics(pktInfo
.handle
))
1123 stats
->rx_packets
= pktStat
.inPackets
;
1124 stats
->rx_errors
= pktStat
.lost
;
1125 stats
->rx_missed_errors
= PktRxDropped();
1129 static int pkt_probe (struct device
*dev
)
1131 if (!PktSearchDriver())
1134 dev
->open
= pkt_open
;
1135 dev
->xmit
= pkt_xmit
;
1136 dev
->close
= pkt_close
;
1137 dev
->get_stats
= pkt_stats
;
1138 dev
->copy_rx_buf
= PktReceive
; /* farmem peek and copy routine */
1139 dev
->get_rx_buf
= NULL
;
1140 dev
->peek_rx_buf
= NULL
;
1141 dev
->release_rx_buf
= NULL
;
1142 dev
->priv
= calloc (sizeof(struct net_device_stats
), 1);
1149 * NDIS device functions
1151 static void ndis_close (struct device
*dev
)
1159 static int ndis_open (struct device
*dev
)
1161 int promis
= (dev
->flags
& IFF_PROMISC
);
1164 if (!NdisInit(promis
))
1173 static void *ndis_stats (struct device
*dev
)
1175 static struct net_device_stats stats
;
1182 static int ndis_probe (struct device
*dev
)
1189 dev
->open
= ndis_open
;
1191 dev
->close
= ndis_close
;
1192 dev
->get_stats
= ndis_stats
;
1193 dev
->copy_rx_buf
= NULL
; /* to-do */
1194 dev
->get_rx_buf
= NULL
; /* upcall is from rmode driver */
1195 dev
->peek_rx_buf
= NULL
;
1196 dev
->release_rx_buf
= NULL
;
1201 * Search & probe for supported 32-bit (pmode) pcap devices
1203 #if defined(USE_32BIT_DRIVERS)
1205 struct device el2_dev LOCKED_VAR
= {
1214 struct device el3_dev LOCKED_VAR
= {
1223 struct device tc515_dev LOCKED_VAR
= {
1232 struct device tc59_dev LOCKED_VAR
= {
1241 struct device tc90xbc_dev LOCKED_VAR
= {
1250 struct device wd_dev LOCKED_VAR
= {
1259 struct device ne_dev LOCKED_VAR
= {
1268 struct device acct_dev LOCKED_VAR
= {
1270 "Accton EtherPocket",
1277 struct device cs89_dev LOCKED_VAR
= {
1279 "Crystal Semiconductor",
1286 struct device rtl8139_dev LOCKED_VAR
= {
1292 rtl8139_probe
/* dev->probe routine */
1296 * Dequeue routine is called by polling.
1297 * NOTE: the queue-element is not copied, only a pointer is
1298 * returned at '*buf'
1300 int peek_rxbuf (BYTE
**buf
)
1302 struct rx_elem
*tail
, *head
;
1304 PCAP_ASSERT (pktq_check (&active_dev
->queue
));
1307 tail
= pktq_out_elem (&active_dev
->queue
);
1308 head
= pktq_in_elem (&active_dev
->queue
);
1313 PCAP_ASSERT (tail
->size
< active_dev
->queue
.elem_size
-4-2);
1315 *buf
= &tail
->data
[0];
1316 return (tail
->size
);
1323 * Release buffer we peeked at above.
1325 int release_rxbuf (BYTE
*buf
)
1328 struct rx_elem
*tail
= pktq_out_elem (&active_dev
->queue
);
1330 PCAP_ASSERT (&tail
->data
[0] == buf
);
1334 pktq_inc_out (&active_dev
->queue
);
1339 * get_rxbuf() routine (in locked code) is called from IRQ handler
1340 * to request a buffer. Interrupts are disabled and we have a 32kB stack.
1342 BYTE
*get_rxbuf (int len
)
1346 if (len
< ETH_MIN
|| len
> ETH_MAX
)
1349 idx
= pktq_in_index (&active_dev
->queue
);
1353 static int fan_idx LOCKED_VAR
= 0;
1354 writew ("-\\|/"[fan_idx
++] | (15 << 8), /* white on black colour */
1355 0xB8000 + 2*79); /* upper-right corner, 80-col colour screen */
1358 /* writew (idx + '0' + 0x0F00, 0xB8000 + 2*78); */
1361 if (idx
!= active_dev
->queue
.out_index
)
1363 struct rx_elem
*head
= pktq_in_elem (&active_dev
->queue
);
1366 active_dev
->queue
.in_index
= idx
;
1367 return (&head
->data
[0]);
1370 /* !!to-do: drop 25% of the oldest element
1372 pktq_clear (&active_dev
->queue
);
1377 * Simple ring-buffer queue handler for reception of packets
1378 * from network driver.
1380 #define PKTQ_MARKER 0xDEADBEEF
1382 static int pktq_check (struct rx_ringbuf
*q
)
1389 if (!q
|| !q
->num_elem
|| !q
->buf_start
)
1395 for (i
= 0; i
< q
->num_elem
; i
++)
1397 buf
+= q
->elem_size
;
1398 if (*(DWORD
*)(buf
- sizeof(DWORD
)) != PKTQ_MARKER
)
1405 static int pktq_init (struct rx_ringbuf
*q
, int size
, int num
, char *pool
)
1409 q
->elem_size
= size
;
1411 q
->buf_start
= pool
;
1415 PCAP_ASSERT (size
>= sizeof(struct rx_elem
) + sizeof(DWORD
));
1419 for (i
= 0; i
< num
; i
++)
1422 struct rx_elem
*elem
= (struct rx_elem
*) pool
;
1424 /* assert dword aligned elements
1426 PCAP_ASSERT (((unsigned)(&elem
->data
[0]) & 3) == 0);
1429 *(DWORD
*) (pool
- sizeof(DWORD
)) = PKTQ_MARKER
;
1435 * Increment the queue 'out_index' (tail).
1438 static int pktq_inc_out (struct rx_ringbuf
*q
)
1441 if (q
->out_index
>= q
->num_elem
)
1443 return (q
->out_index
);
1447 * Return the queue's next 'in_index' (head).
1450 static int pktq_in_index (struct rx_ringbuf
*q
)
1452 volatile int index
= q
->in_index
+ 1;
1454 if (index
>= q
->num_elem
)
1460 * Return the queue's head-buffer.
1462 static struct rx_elem
*pktq_in_elem (struct rx_ringbuf
*q
)
1464 return (struct rx_elem
*) (q
->buf_start
+ (q
->elem_size
* q
->in_index
));
1468 * Return the queue's tail-buffer.
1470 static struct rx_elem
*pktq_out_elem (struct rx_ringbuf
*q
)
1472 return (struct rx_elem
*) (q
->buf_start
+ (q
->elem_size
* q
->out_index
));
1476 * Clear the queue ring-buffer by setting head=tail.
1478 static void pktq_clear (struct rx_ringbuf
*q
)
1480 q
->in_index
= q
->out_index
;
1484 * Symbols that must be linkable for "gcc -O0"
1492 #include "msdos/pm_drvr/ioport.h"
1493 #include "msdos/pm_drvr/dma.h"
1495 #endif /* USE_32BIT_DRIVERS */