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
.source
, pcap
->errbuf
) ||
198 !first_init(pcap
->opt
.source
, pcap
->errbuf
, pcap
->opt
.promisc
))
202 atexit (close_driver
);
204 else if (stricmp(active_dev
->name
,pcap
->opt
.source
))
206 pcap_snprintf (pcap
->errbuf
, PCAP_ERRBUF_SIZE
,
207 "Cannot use different devices simultaneously "
208 "(`%s' vs. `%s')", active_dev
->name
, pcap
->opt
.source
);
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_findalldevs_interfaces (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 * platform-dependent routine to add devices or interfaces.
608 * We don't have any, so just return 0.
610 int pcap_platform_finddevs (pcap_if_t
**alldevsp
, char *errbuf
)
618 * pcap_assert() is mainly used for debugging
620 void pcap_assert (const char *what
, const char *file
, unsigned line
)
623 fprintf (stderr
, "%s (%u): Assertion \"%s\" failed\n",
630 * For pcap_offline_read(): wait and yield between printing packets
631 * to simulate the pace packets where actually recorded.
633 void pcap_set_wait (pcap_t
*p
, void (*yield
)(void), int wait
)
637 struct pcap_dos
*pd
= p
->priv
;
639 pd
->wait_proc
= yield
;
640 p
->opt
.timeout
= wait
;
645 * Initialise a named network device.
647 static struct device
*
648 open_driver (const char *dev_name
, char *ebuf
, int promisc
)
652 for (dev
= (struct device
*)dev_base
; dev
; dev
= dev
->next
)
654 PCAP_ASSERT (dev
->name
);
656 if (strcmp (dev_name
,dev
->name
))
659 if (!probed_dev
) /* user didn't call pcap_lookupdev() first */
661 PCAP_ASSERT (dev
->probe
);
663 if (!(*dev
->probe
)(dev
)) /* call the xx_probe() function */
665 pcap_snprintf (ebuf
, PCAP_ERRBUF_SIZE
, "failed to detect device `%s'", dev_name
);
668 probed_dev
= dev
; /* device is probed okay and may be used */
670 else if (dev
!= probed_dev
)
677 /* Select what traffic to receive
680 dev
->flags
|= (IFF_ALLMULTI
| IFF_PROMISC
);
681 else dev
->flags
&= ~(IFF_ALLMULTI
| IFF_PROMISC
);
683 PCAP_ASSERT (dev
->open
);
685 if (!(*dev
->open
)(dev
))
687 pcap_snprintf (ebuf
, PCAP_ERRBUF_SIZE
, "failed to activate device `%s'", dev_name
);
688 if (pktInfo
.error
&& !strncmp(dev
->name
,"pkt",3))
691 strcat (ebuf
, pktInfo
.error
);
696 /* Some devices need this to operate in promiscous mode
698 if (promisc
&& dev
->set_multicast_list
)
699 (*dev
->set_multicast_list
) (dev
);
701 active_dev
= dev
; /* remember our active device */
705 /* 'dev_name' not matched in 'dev_base' list.
709 pcap_snprintf (ebuf
, PCAP_ERRBUF_SIZE
, "device `%s' not supported", dev_name
);
716 pcap_snprintf (ebuf
, PCAP_ERRBUF_SIZE
, "device `%s' not probed", dev_name
);
723 * Deinitialise MAC driver.
724 * Set receive mode back to default mode.
726 static void close_driver (void)
728 /* !!todo: loop over all 'handle_to_device[]' ? */
729 struct device
*dev
= active_dev
;
731 if (dev
&& dev
->close
)
739 #ifdef USE_32BIT_DRIVERS
752 static void setup_signals (void (*handler
)(int))
754 signal (SIGSEGV
,handler
);
755 signal (SIGILL
, handler
);
756 signal (SIGFPE
, handler
);
759 static void exc_handler (int sig
)
761 #ifdef USE_32BIT_DRIVERS
762 if (active_dev
->irq
> 0) /* excludes IRQ 0 */
764 disable_irq (active_dev
->irq
);
765 irq_eoi_cmd (active_dev
->irq
);
773 fputs ("Catching SIGSEGV.\n", stderr
);
776 fputs ("Catching SIGILL.\n", stderr
);
780 fputs ("Catching SIGFPE.\n", stderr
);
783 fprintf (stderr
, "Catching signal %d.\n", sig
);
788 #endif /* __DJGPP__ */
792 * Open the pcap device for the first client calling pcap_activate()
794 static int first_init (const char *name
, char *ebuf
, int promisc
)
798 #ifdef USE_32BIT_DRIVERS
799 rx_pool
= k_calloc (RECEIVE_BUF_SIZE
, RECEIVE_QUEUE_SIZE
);
802 strcpy (ebuf
, "Not enough memory (Rx pool)");
808 setup_signals (exc_handler
);
811 #ifdef USE_32BIT_DRIVERS
815 dev
= open_driver (name
, ebuf
, promisc
);
818 #ifdef USE_32BIT_DRIVERS
824 setup_signals (SIG_DFL
);
829 #ifdef USE_32BIT_DRIVERS
831 * If driver is NOT a 16-bit "pkt/ndis" driver (having a 'copy_rx_buf'
832 * set in it's probe handler), initialise near-memory ring-buffer for
835 if (dev
->copy_rx_buf
== NULL
)
837 dev
->get_rx_buf
= get_rxbuf
;
838 dev
->peek_rx_buf
= peek_rxbuf
;
839 dev
->release_rx_buf
= release_rxbuf
;
840 pktq_init (&dev
->queue
, RECEIVE_BUF_SIZE
, RECEIVE_QUEUE_SIZE
, rx_pool
);
846 #ifdef USE_32BIT_DRIVERS
847 static void init_32bit (void)
849 static int init_pci
= 0;
852 _printk_init (64*1024, NULL
); /* calls atexit(printk_exit) */
855 (void)pci_init(); /* init BIOS32+PCI interface */
862 * Hook functions for using Watt-32 together with pcap
864 static char rxbuf
[ETH_MAX
+100]; /* rx-buffer with some margin */
866 static pcap_t pcap_save
;
868 static void watt32_recv_hook (u_char
*dummy
, const struct pcap_pkthdr
*pcap
,
871 /* Fix me: assumes Ethernet II only */
872 struct ether_header
*ep
= (struct ether_header
*) buf
;
874 memcpy (rxbuf
, buf
, pcap
->caplen
);
875 etype
= ep
->ether_type
;
879 #if (WATTCP_VER >= 0x0224)
881 * This function is used by Watt-32 to poll for a packet.
882 * i.e. it's set to bypass _eth_arrived()
884 static void *pcap_recv_hook (WORD
*type
)
886 int len
= pcap_read_dos (&pcap_save
, 1, watt32_recv_hook
, NULL
);
892 return (void*) &rxbuf
;
896 * This function is called by Watt-32 (via _eth_xmit_hook).
897 * If dbug_init() was called, we should trace packets sent.
899 static int pcap_xmit_hook (const void *buf
, unsigned len
)
903 if (pcap_pkt_debug
> 0)
904 dbug_write ("pcap_xmit_hook: ");
906 if (active_dev
&& active_dev
->xmit
)
907 if ((*active_dev
->xmit
) (active_dev
, buf
, len
) > 0)
910 if (pcap_pkt_debug
> 0)
911 dbug_write (rc
? "ok\n" : "fail\n");
916 static int pcap_sendpacket_dos (pcap_t
*p
, const void *buf
, size_t len
)
918 struct device
*dev
= p
? get_device(p
->fd
) : NULL
;
920 if (!dev
|| !dev
->xmit
)
922 return (*dev
->xmit
) (dev
, buf
, len
);
926 * This function is called by Watt-32 in tcp_post_init().
927 * We should prevent Watt-32 from using BOOTP/DHCP/RARP etc.
929 static void (*prev_post_hook
) (void);
931 static void pcap_init_hook (void)
933 _w32__bootp_on
= _w32__dhcp_on
= _w32__rarp_on
= 0;
934 _w32__do_mask_req
= 0;
935 _w32_dynamic_host
= 0;
941 * Supress PRINT message from Watt-32's sock_init()
943 static void null_print (void) {}
946 * To use features of Watt-32 (netdb functions and socket etc.)
947 * we must call sock_init(). But we set various hooks to prevent
948 * using normal PKTDRVR functions in pcpkt.c. This should hopefully
949 * make Watt-32 and pcap co-operate.
951 static int init_watt32 (struct pcap
*pcap
, const char *dev_name
, char *err_buf
)
954 int rc
, MTU
, has_ip_addr
;
955 int using_pktdrv
= 1;
957 /* If user called sock_init() first, we need to reinit in
958 * order to open debug/trace-file properly
963 env
= getenv ("PCAP_TRACE");
964 if (env
&& atoi(env
) > 0 &&
965 pcap_pkt_debug
< 0) /* if not already set */
968 pcap_pkt_debug
= atoi (env
);
971 _watt_do_exit
= 0; /* prevent sock_init() calling exit() */
972 prev_post_hook
= _w32_usr_post_init
;
973 _w32_usr_post_init
= pcap_init_hook
;
974 _w32_print_hook
= null_print
;
976 if (dev_name
&& strncmp(dev_name
,"pkt",3))
977 using_pktdrv
= FALSE
;
980 has_ip_addr
= (rc
!= 8); /* IP-address assignment failed */
982 /* if pcap is using a 32-bit driver w/o a pktdrvr loaded, we
983 * just pretend Watt-32 is initialised okay.
985 * !! fix-me: The Watt-32 config isn't done if no pktdrvr
986 * was found. In that case my_ip_addr + sin_mask
987 * have default values. Should be taken from another
988 * ini-file/environment in any case (ref. tcpdump.ini)
992 if (!using_pktdrv
|| !has_ip_addr
) /* for now .... */
994 static const char myip
[] = "192.168.0.1";
995 static const char mask
[] = "255.255.255.0";
997 printf ("Just guessing, using IP %s and netmask %s\n", myip
, mask
);
998 my_ip_addr
= aton (myip
);
999 _w32_sin_mask
= aton (mask
);
1001 else if (rc
&& using_pktdrv
)
1003 pcap_snprintf (err_buf
, PCAP_ERRBUF_SIZE
, "sock_init() failed, code %d", rc
);
1007 /* Set recv-hook for peeking in _eth_arrived().
1009 #if (WATTCP_VER >= 0x0224)
1010 _eth_recv_hook
= pcap_recv_hook
;
1011 _eth_xmit_hook
= pcap_xmit_hook
;
1014 /* Free the pkt-drvr handle allocated in pkt_init().
1015 * The above hooks should thus use the handle reopened in open_driver()
1020 /* _eth_is_init = 1; */ /* hack to get Rx/Tx-hooks in Watt-32 working */
1023 memcpy (&pcap_save
, pcap
, sizeof(pcap_save
));
1024 MTU
= pkt_get_mtu();
1025 pcap_save
.fcode
.bf_insns
= NULL
;
1026 pcap_save
.linktype
= _eth_get_hwtype (NULL
, NULL
);
1027 pcap_save
.snapshot
= MTU
> 0 ? MTU
: ETH_MAX
; /* assume 1514 */
1030 /* prevent use of resolve() and resolve_ip()
1032 last_nameserver
= 0;
1037 int EISA_bus
= 0; /* Where is natural place for this? */
1040 * Application config hooks to set various driver parameters.
1043 static const struct config_table debug_tab
[] = {
1044 { "PKT.DEBUG", ARG_ATOI
, &pcap_pkt_debug
},
1045 { "PKT.VECTOR", ARG_ATOX_W
, NULL
},
1046 { "NDIS.DEBUG", ARG_ATOI
, NULL
},
1047 #ifdef USE_32BIT_DRIVERS
1048 { "3C503.DEBUG", ARG_ATOI
, &ei_debug
},
1049 { "3C503.IO_BASE", ARG_ATOX_W
, &el2_dev
.base_addr
},
1050 { "3C503.MEMORY", ARG_ATOX_W
, &el2_dev
.mem_start
},
1051 { "3C503.IRQ", ARG_ATOI
, &el2_dev
.irq
},
1052 { "3C505.DEBUG", ARG_ATOI
, NULL
},
1053 { "3C505.BASE", ARG_ATOX_W
, NULL
},
1054 { "3C507.DEBUG", ARG_ATOI
, NULL
},
1055 { "3C509.DEBUG", ARG_ATOI
, &el3_debug
},
1056 { "3C509.ILOOP", ARG_ATOI
, &el3_max_loop
},
1057 { "3C529.DEBUG", ARG_ATOI
, NULL
},
1058 { "3C575.DEBUG", ARG_ATOI
, &debug_3c575
},
1059 { "3C59X.DEBUG", ARG_ATOI
, &vortex_debug
},
1060 { "3C59X.IFACE0", ARG_ATOI
, &vortex_options
[0] },
1061 { "3C59X.IFACE1", ARG_ATOI
, &vortex_options
[1] },
1062 { "3C59X.IFACE2", ARG_ATOI
, &vortex_options
[2] },
1063 { "3C59X.IFACE3", ARG_ATOI
, &vortex_options
[3] },
1064 { "3C90X.DEBUG", ARG_ATOX_W
, &tc90xbc_debug
},
1065 { "ACCT.DEBUG", ARG_ATOI
, ðpk_debug
},
1066 { "CS89.DEBUG", ARG_ATOI
, &cs89_debug
},
1067 { "RTL8139.DEBUG", ARG_ATOI
, &rtl8139_debug
},
1068 /* { "RTL8139.FDUPLEX", ARG_ATOI, &rtl8139_options }, */
1069 { "SMC.DEBUG", ARG_ATOI
, &ei_debug
},
1070 /* { "E100.DEBUG", ARG_ATOI, &e100_debug }, */
1071 { "PCI.DEBUG", ARG_ATOI
, &pci_debug
},
1072 { "BIOS32.DEBUG", ARG_ATOI
, &bios32_debug
},
1073 { "IRQ.DEBUG", ARG_ATOI
, &irq_debug
},
1074 { "TIMER.IRQ", ARG_ATOI
, &timer_irq
},
1080 * pcap_config_hook() is an extension to application's config
1081 * handling. Uses Watt-32's config-table function.
1083 int pcap_config_hook (const char *keyword
, const char *value
)
1085 return parse_config_table (debug_tab
, NULL
, keyword
, value
);
1089 * Linked list of supported devices
1091 struct device
*active_dev
= NULL
; /* the device we have opened */
1092 struct device
*probed_dev
= NULL
; /* the device we have probed */
1093 const struct device
*dev_base
= &pkt_dev
; /* list of network devices */
1096 * PKTDRVR device functions
1098 int pcap_pkt_debug
= -1;
1100 static void pkt_close (struct device
*dev
)
1102 BOOL okay
= PktExitDriver();
1104 if (pcap_pkt_debug
> 1)
1105 fprintf (stderr
, "pkt_close(): %d\n", okay
);
1112 static int pkt_open (struct device
*dev
)
1116 if (dev
->flags
& IFF_PROMISC
)
1117 mode
= PDRX_ALL_PACKETS
;
1118 else mode
= PDRX_BROADCAST
;
1120 if (!PktInitDriver(mode
))
1123 PktResetStatistics (pktInfo
.handle
);
1124 PktQueueBusy (FALSE
);
1128 static int pkt_xmit (struct device
*dev
, const void *buf
, int len
)
1130 struct net_device_stats
*stats
= (struct net_device_stats
*) dev
->priv
;
1132 if (pcap_pkt_debug
> 0)
1133 dbug_write ("pcap_xmit\n");
1135 if (!PktTransmit(buf
,len
))
1143 static void *pkt_stats (struct device
*dev
)
1145 struct net_device_stats
*stats
= (struct net_device_stats
*) dev
->priv
;
1147 if (!stats
|| !PktSessStatistics(pktInfo
.handle
))
1150 stats
->rx_packets
= pktStat
.inPackets
;
1151 stats
->rx_errors
= pktStat
.lost
;
1152 stats
->rx_missed_errors
= PktRxDropped();
1156 static int pkt_probe (struct device
*dev
)
1158 if (!PktSearchDriver())
1161 dev
->open
= pkt_open
;
1162 dev
->xmit
= pkt_xmit
;
1163 dev
->close
= pkt_close
;
1164 dev
->get_stats
= pkt_stats
;
1165 dev
->copy_rx_buf
= PktReceive
; /* farmem peek and copy routine */
1166 dev
->get_rx_buf
= NULL
;
1167 dev
->peek_rx_buf
= NULL
;
1168 dev
->release_rx_buf
= NULL
;
1169 dev
->priv
= calloc (sizeof(struct net_device_stats
), 1);
1176 * NDIS device functions
1178 static void ndis_close (struct device
*dev
)
1186 static int ndis_open (struct device
*dev
)
1188 int promis
= (dev
->flags
& IFF_PROMISC
);
1191 if (!NdisInit(promis
))
1200 static void *ndis_stats (struct device
*dev
)
1202 static struct net_device_stats stats
;
1209 static int ndis_probe (struct device
*dev
)
1216 dev
->open
= ndis_open
;
1218 dev
->close
= ndis_close
;
1219 dev
->get_stats
= ndis_stats
;
1220 dev
->copy_rx_buf
= NULL
; /* to-do */
1221 dev
->get_rx_buf
= NULL
; /* upcall is from rmode driver */
1222 dev
->peek_rx_buf
= NULL
;
1223 dev
->release_rx_buf
= NULL
;
1228 * Search & probe for supported 32-bit (pmode) pcap devices
1230 #if defined(USE_32BIT_DRIVERS)
1232 struct device el2_dev LOCKED_VAR
= {
1241 struct device el3_dev LOCKED_VAR
= {
1250 struct device tc515_dev LOCKED_VAR
= {
1259 struct device tc59_dev LOCKED_VAR
= {
1268 struct device tc90xbc_dev LOCKED_VAR
= {
1277 struct device wd_dev LOCKED_VAR
= {
1286 struct device ne_dev LOCKED_VAR
= {
1295 struct device acct_dev LOCKED_VAR
= {
1297 "Accton EtherPocket",
1304 struct device cs89_dev LOCKED_VAR
= {
1306 "Crystal Semiconductor",
1313 struct device rtl8139_dev LOCKED_VAR
= {
1319 rtl8139_probe
/* dev->probe routine */
1323 * Dequeue routine is called by polling.
1324 * NOTE: the queue-element is not copied, only a pointer is
1325 * returned at '*buf'
1327 int peek_rxbuf (BYTE
**buf
)
1329 struct rx_elem
*tail
, *head
;
1331 PCAP_ASSERT (pktq_check (&active_dev
->queue
));
1334 tail
= pktq_out_elem (&active_dev
->queue
);
1335 head
= pktq_in_elem (&active_dev
->queue
);
1340 PCAP_ASSERT (tail
->size
< active_dev
->queue
.elem_size
-4-2);
1342 *buf
= &tail
->data
[0];
1343 return (tail
->size
);
1350 * Release buffer we peeked at above.
1352 int release_rxbuf (BYTE
*buf
)
1355 struct rx_elem
*tail
= pktq_out_elem (&active_dev
->queue
);
1357 PCAP_ASSERT (&tail
->data
[0] == buf
);
1361 pktq_inc_out (&active_dev
->queue
);
1366 * get_rxbuf() routine (in locked code) is called from IRQ handler
1367 * to request a buffer. Interrupts are disabled and we have a 32kB stack.
1369 BYTE
*get_rxbuf (int len
)
1373 if (len
< ETH_MIN
|| len
> ETH_MAX
)
1376 idx
= pktq_in_index (&active_dev
->queue
);
1380 static int fan_idx LOCKED_VAR
= 0;
1381 writew ("-\\|/"[fan_idx
++] | (15 << 8), /* white on black colour */
1382 0xB8000 + 2*79); /* upper-right corner, 80-col colour screen */
1385 /* writew (idx + '0' + 0x0F00, 0xB8000 + 2*78); */
1388 if (idx
!= active_dev
->queue
.out_index
)
1390 struct rx_elem
*head
= pktq_in_elem (&active_dev
->queue
);
1393 active_dev
->queue
.in_index
= idx
;
1394 return (&head
->data
[0]);
1397 /* !!to-do: drop 25% of the oldest element
1399 pktq_clear (&active_dev
->queue
);
1404 * Simple ring-buffer queue handler for reception of packets
1405 * from network driver.
1407 #define PKTQ_MARKER 0xDEADBEEF
1409 static int pktq_check (struct rx_ringbuf
*q
)
1416 if (!q
|| !q
->num_elem
|| !q
->buf_start
)
1422 for (i
= 0; i
< q
->num_elem
; i
++)
1424 buf
+= q
->elem_size
;
1425 if (*(DWORD
*)(buf
- sizeof(DWORD
)) != PKTQ_MARKER
)
1432 static int pktq_init (struct rx_ringbuf
*q
, int size
, int num
, char *pool
)
1436 q
->elem_size
= size
;
1438 q
->buf_start
= pool
;
1442 PCAP_ASSERT (size
>= sizeof(struct rx_elem
) + sizeof(DWORD
));
1446 for (i
= 0; i
< num
; i
++)
1449 struct rx_elem
*elem
= (struct rx_elem
*) pool
;
1451 /* assert dword aligned elements
1453 PCAP_ASSERT (((unsigned)(&elem
->data
[0]) & 3) == 0);
1456 *(DWORD
*) (pool
- sizeof(DWORD
)) = PKTQ_MARKER
;
1462 * Increment the queue 'out_index' (tail).
1465 static int pktq_inc_out (struct rx_ringbuf
*q
)
1468 if (q
->out_index
>= q
->num_elem
)
1470 return (q
->out_index
);
1474 * Return the queue's next 'in_index' (head).
1477 static int pktq_in_index (struct rx_ringbuf
*q
)
1479 volatile int index
= q
->in_index
+ 1;
1481 if (index
>= q
->num_elem
)
1487 * Return the queue's head-buffer.
1489 static struct rx_elem
*pktq_in_elem (struct rx_ringbuf
*q
)
1491 return (struct rx_elem
*) (q
->buf_start
+ (q
->elem_size
* q
->in_index
));
1495 * Return the queue's tail-buffer.
1497 static struct rx_elem
*pktq_out_elem (struct rx_ringbuf
*q
)
1499 return (struct rx_elem
*) (q
->buf_start
+ (q
->elem_size
* q
->out_index
));
1503 * Clear the queue ring-buffer by setting head=tail.
1505 static void pktq_clear (struct rx_ringbuf
*q
)
1507 q
->in_index
= q
->out_index
;
1511 * Symbols that must be linkable for "gcc -O0"
1519 #include "msdos/pm_drvr/ioport.h"
1520 #include "msdos/pm_drvr/dma.h"
1522 #endif /* USE_32BIT_DRIVERS */