104fb2745SSam Leffler /* 204fb2745SSam Leffler * This file is part of DOS-libpcap 3a8e07101SRui Paulo * Ported to DOS/DOSX by G. Vanem <gvanem@broadpark.no> 404fb2745SSam Leffler * 504fb2745SSam Leffler * pcap-dos.c: Interface to PKTDRVR, NDIS2 and 32-bit pmode 604fb2745SSam Leffler * network drivers. 704fb2745SSam Leffler * 8a8e07101SRui Paulo * @(#) $Header: /tcpdump/master/libpcap/pcap-dos.c,v 1.2.2.5 2008-04-22 17:16:49 guy Exp $ (LBL) 904fb2745SSam Leffler */ 1004fb2745SSam Leffler 1104fb2745SSam Leffler #include <stdio.h> 1204fb2745SSam Leffler #include <stdlib.h> 1304fb2745SSam Leffler #include <string.h> 1404fb2745SSam Leffler #include <signal.h> 1504fb2745SSam Leffler #include <float.h> 1604fb2745SSam Leffler #include <fcntl.h> 1704fb2745SSam Leffler #include <io.h> 1804fb2745SSam Leffler 1904fb2745SSam Leffler #if defined(USE_32BIT_DRIVERS) 2004fb2745SSam Leffler #include "msdos/pm_drvr/pmdrvr.h" 2104fb2745SSam Leffler #include "msdos/pm_drvr/pci.h" 2204fb2745SSam Leffler #include "msdos/pm_drvr/bios32.h" 2304fb2745SSam Leffler #include "msdos/pm_drvr/module.h" 2404fb2745SSam Leffler #include "msdos/pm_drvr/3c501.h" 2504fb2745SSam Leffler #include "msdos/pm_drvr/3c503.h" 2604fb2745SSam Leffler #include "msdos/pm_drvr/3c509.h" 2704fb2745SSam Leffler #include "msdos/pm_drvr/3c59x.h" 2804fb2745SSam Leffler #include "msdos/pm_drvr/3c515.h" 2904fb2745SSam Leffler #include "msdos/pm_drvr/3c90x.h" 3004fb2745SSam Leffler #include "msdos/pm_drvr/3c575_cb.h" 3104fb2745SSam Leffler #include "msdos/pm_drvr/ne.h" 3204fb2745SSam Leffler #include "msdos/pm_drvr/wd.h" 3304fb2745SSam Leffler #include "msdos/pm_drvr/accton.h" 3404fb2745SSam Leffler #include "msdos/pm_drvr/cs89x0.h" 3504fb2745SSam Leffler #include "msdos/pm_drvr/rtl8139.h" 3604fb2745SSam Leffler #include "msdos/pm_drvr/ne2k-pci.h" 3704fb2745SSam Leffler #endif 3804fb2745SSam Leffler 3904fb2745SSam Leffler #include "pcap.h" 4004fb2745SSam Leffler #include "pcap-dos.h" 4104fb2745SSam Leffler #include "pcap-int.h" 4204fb2745SSam Leffler #include "msdos/pktdrvr.h" 4304fb2745SSam Leffler 4404fb2745SSam Leffler #ifdef USE_NDIS2 4504fb2745SSam Leffler #include "msdos/ndis2.h" 4604fb2745SSam Leffler #endif 4704fb2745SSam Leffler 4804fb2745SSam Leffler #include <arpa/inet.h> 4904fb2745SSam Leffler #include <net/if.h> 5004fb2745SSam Leffler #include <net/if_arp.h> 5104fb2745SSam Leffler #include <net/if_ether.h> 5204fb2745SSam Leffler #include <net/if_packe.h> 5304fb2745SSam Leffler #include <tcp.h> 5404fb2745SSam Leffler 5504fb2745SSam Leffler #if defined(USE_32BIT_DRIVERS) 5604fb2745SSam Leffler #define FLUSHK() do { _printk_safe = 1; _printk_flush(); } while (0) 5704fb2745SSam Leffler #define NDIS_NEXT_DEV &rtl8139_dev 5804fb2745SSam Leffler 5904fb2745SSam Leffler static char *rx_pool = NULL; 6004fb2745SSam Leffler static void init_32bit (void); 6104fb2745SSam Leffler 6204fb2745SSam Leffler static int pktq_init (struct rx_ringbuf *q, int size, int num, char *pool); 6304fb2745SSam Leffler static int pktq_check (struct rx_ringbuf *q); 6404fb2745SSam Leffler static int pktq_inc_out (struct rx_ringbuf *q); 6504fb2745SSam Leffler static int pktq_in_index (struct rx_ringbuf *q) LOCKED_FUNC; 6604fb2745SSam Leffler static void pktq_clear (struct rx_ringbuf *q) LOCKED_FUNC; 6704fb2745SSam Leffler 6804fb2745SSam Leffler static struct rx_elem *pktq_in_elem (struct rx_ringbuf *q) LOCKED_FUNC; 6904fb2745SSam Leffler static struct rx_elem *pktq_out_elem (struct rx_ringbuf *q); 7004fb2745SSam Leffler 7104fb2745SSam Leffler #else 7204fb2745SSam Leffler #define FLUSHK() ((void)0) 7304fb2745SSam Leffler #define NDIS_NEXT_DEV NULL 7404fb2745SSam Leffler #endif 7504fb2745SSam Leffler 7604fb2745SSam Leffler /* 7704fb2745SSam Leffler * Internal variables/functions in Watt-32 7804fb2745SSam Leffler */ 7904fb2745SSam Leffler extern WORD _pktdevclass; 8004fb2745SSam Leffler extern BOOL _eth_is_init; 8104fb2745SSam Leffler extern int _w32_dynamic_host; 8204fb2745SSam Leffler extern int _watt_do_exit; 8304fb2745SSam Leffler extern int _watt_is_init; 8404fb2745SSam Leffler extern int _w32__bootp_on, _w32__dhcp_on, _w32__rarp_on, _w32__do_mask_req; 8504fb2745SSam Leffler extern void (*_w32_usr_post_init) (void); 8604fb2745SSam Leffler extern void (*_w32_print_hook)(); 8704fb2745SSam Leffler 8804fb2745SSam Leffler extern void dbug_write (const char *); /* Watt-32 lib, pcdbug.c */ 8904fb2745SSam Leffler extern int pkt_get_mtu (void); 9004fb2745SSam Leffler 9104fb2745SSam Leffler static int ref_count = 0; 9204fb2745SSam Leffler 9304fb2745SSam Leffler static u_long mac_count = 0; 9404fb2745SSam Leffler static u_long filter_count = 0; 9504fb2745SSam Leffler 9604fb2745SSam Leffler static volatile BOOL exc_occured = 0; 9704fb2745SSam Leffler 9804fb2745SSam Leffler static struct device *handle_to_device [20]; 9904fb2745SSam Leffler 100a8e07101SRui Paulo static int pcap_activate_dos (pcap_t *p); 10104fb2745SSam Leffler static int pcap_read_dos (pcap_t *p, int cnt, pcap_handler callback, 10204fb2745SSam Leffler u_char *data); 103a8e07101SRui Paulo static void pcap_cleanup_dos (pcap_t *p); 10404fb2745SSam Leffler static int pcap_stats_dos (pcap_t *p, struct pcap_stat *ps); 10504fb2745SSam Leffler static int pcap_sendpacket_dos (pcap_t *p, const void *buf, size_t len); 10604fb2745SSam Leffler static int pcap_setfilter_dos (pcap_t *p, struct bpf_program *fp); 10704fb2745SSam Leffler 10804fb2745SSam Leffler static int ndis_probe (struct device *dev); 10904fb2745SSam Leffler static int pkt_probe (struct device *dev); 11004fb2745SSam Leffler 11104fb2745SSam Leffler static void close_driver (void); 11204fb2745SSam Leffler static int init_watt32 (struct pcap *pcap, const char *dev_name, char *err_buf); 11304fb2745SSam Leffler static int first_init (const char *name, char *ebuf, int promisc); 11404fb2745SSam Leffler 11504fb2745SSam Leffler static void watt32_recv_hook (u_char *dummy, const struct pcap_pkthdr *pcap, 11604fb2745SSam Leffler const u_char *buf); 11704fb2745SSam Leffler 11804fb2745SSam Leffler /* 11904fb2745SSam Leffler * These are the device we always support 12004fb2745SSam Leffler */ 12104fb2745SSam Leffler static struct device ndis_dev = { 12204fb2745SSam Leffler "ndis", 12304fb2745SSam Leffler "NDIS2 LanManager", 12404fb2745SSam Leffler 0, 12504fb2745SSam Leffler 0,0,0,0,0,0, 12604fb2745SSam Leffler NDIS_NEXT_DEV, /* NULL or a 32-bit device */ 12704fb2745SSam Leffler ndis_probe 12804fb2745SSam Leffler }; 12904fb2745SSam Leffler 13004fb2745SSam Leffler static struct device pkt_dev = { 13104fb2745SSam Leffler "pkt", 13204fb2745SSam Leffler "Packet-Driver", 13304fb2745SSam Leffler 0, 13404fb2745SSam Leffler 0,0,0,0,0,0, 13504fb2745SSam Leffler &ndis_dev, 13604fb2745SSam Leffler pkt_probe 13704fb2745SSam Leffler }; 13804fb2745SSam Leffler 13904fb2745SSam Leffler static struct device *get_device (int fd) 14004fb2745SSam Leffler { 14104fb2745SSam Leffler if (fd <= 0 || fd >= sizeof(handle_to_device)/sizeof(handle_to_device[0])) 14204fb2745SSam Leffler return (NULL); 14304fb2745SSam Leffler return handle_to_device [fd-1]; 14404fb2745SSam Leffler } 14504fb2745SSam Leffler 146a8e07101SRui Paulo pcap_t *pcap_create (const char *device, char *ebuf) 147a8e07101SRui Paulo { 148a8e07101SRui Paulo pcap_t *p; 149a8e07101SRui Paulo 150a8e07101SRui Paulo p = pcap_create_common(device, ebuf); 151a8e07101SRui Paulo if (p == NULL) 152a8e07101SRui Paulo return (NULL); 153a8e07101SRui Paulo 154a8e07101SRui Paulo p->activate_op = pcap_activate_dos; 155a8e07101SRui Paulo return (p); 156a8e07101SRui Paulo } 157a8e07101SRui Paulo 15804fb2745SSam Leffler /* 15904fb2745SSam Leffler * Open MAC-driver with name 'device_name' for live capture of 16004fb2745SSam Leffler * network packets. 16104fb2745SSam Leffler */ 162a8e07101SRui Paulo static int pcap_activate_dos (pcap_t *pcap) 16304fb2745SSam Leffler { 164a8e07101SRui Paulo if (pcap->opt.rfmon) { 165a8e07101SRui Paulo /* 166a8e07101SRui Paulo * No monitor mode on DOS. 167a8e07101SRui Paulo */ 168a8e07101SRui Paulo return (PCAP_ERROR_RFMON_NOTSUP); 16904fb2745SSam Leffler } 17004fb2745SSam Leffler 171a8e07101SRui Paulo if (pcap->snapshot < ETH_MIN+8) 172a8e07101SRui Paulo pcap->snapshot = ETH_MIN+8; 173a8e07101SRui Paulo 174a8e07101SRui Paulo if (pcap->snapshot > ETH_MAX) /* silently accept and truncate large MTUs */ 175a8e07101SRui Paulo pcap->snapshot = ETH_MAX; 176a8e07101SRui Paulo 17704fb2745SSam Leffler pcap->linktype = DLT_EN10MB; /* !! */ 178a8e07101SRui Paulo pcap->cleanup_op = pcap_cleanup_dos; 17904fb2745SSam Leffler pcap->read_op = pcap_read_dos; 18004fb2745SSam Leffler pcap->stats_op = pcap_stats_dos; 18104fb2745SSam Leffler pcap->inject_op = pcap_sendpacket_dos; 18204fb2745SSam Leffler pcap->setfilter_op = pcap_setfilter_dos; 183ee2dd488SSam Leffler pcap->setdirection_op = NULL; /* Not implemented.*/ 18404fb2745SSam Leffler pcap->fd = ++ref_count; 18504fb2745SSam Leffler 18604fb2745SSam Leffler if (pcap->fd == 1) /* first time we're called */ 18704fb2745SSam Leffler { 188a8e07101SRui Paulo if (!init_watt32(pcap, pcap->opt.source, pcap->errbuf) || 189a8e07101SRui Paulo !first_init(pcap->opt.source, pcap->errbuf, pcap->opt.promisc)) 19004fb2745SSam Leffler { 191a8e07101SRui Paulo return (PCAP_ERROR); 19204fb2745SSam Leffler } 19304fb2745SSam Leffler atexit (close_driver); 19404fb2745SSam Leffler } 195a8e07101SRui Paulo else if (stricmp(active_dev->name,pcap->opt.source)) 19604fb2745SSam Leffler { 197a8e07101SRui Paulo snprintf (pcap->errbuf, PCAP_ERRBUF_SIZE, 19804fb2745SSam Leffler "Cannot use different devices simultaneously " 199a8e07101SRui Paulo "(`%s' vs. `%s')", active_dev->name, pcap->opt.source); 200a8e07101SRui Paulo return (PCAP_ERROR); 20104fb2745SSam Leffler } 20204fb2745SSam Leffler handle_to_device [pcap->fd-1] = active_dev; 203a8e07101SRui Paulo return (0); 20404fb2745SSam Leffler } 20504fb2745SSam Leffler 20604fb2745SSam Leffler /* 20704fb2745SSam Leffler * Poll the receiver queue and call the pcap callback-handler 20804fb2745SSam Leffler * with the packet. 20904fb2745SSam Leffler */ 21004fb2745SSam Leffler static int 21104fb2745SSam Leffler pcap_read_one (pcap_t *p, pcap_handler callback, u_char *data) 21204fb2745SSam Leffler { 21304fb2745SSam Leffler struct pcap_pkthdr pcap; 21404fb2745SSam Leffler struct timeval now, expiry; 21504fb2745SSam Leffler BYTE *rx_buf; 21604fb2745SSam Leffler int rx_len = 0; 21704fb2745SSam Leffler 218a8e07101SRui Paulo if (p->md.timeout > 0) 21904fb2745SSam Leffler { 22004fb2745SSam Leffler gettimeofday2 (&now, NULL); 221a8e07101SRui Paulo expiry.tv_usec = now.tv_usec + 1000UL * p->md.timeout; 22204fb2745SSam Leffler expiry.tv_sec = now.tv_sec; 22304fb2745SSam Leffler while (expiry.tv_usec >= 1000000L) 22404fb2745SSam Leffler { 22504fb2745SSam Leffler expiry.tv_usec -= 1000000L; 22604fb2745SSam Leffler expiry.tv_sec++; 22704fb2745SSam Leffler } 22804fb2745SSam Leffler } 22904fb2745SSam Leffler 23004fb2745SSam Leffler while (!exc_occured) 23104fb2745SSam Leffler { 23204fb2745SSam Leffler volatile struct device *dev; /* might be reset by sig_handler */ 23304fb2745SSam Leffler 23404fb2745SSam Leffler dev = get_device (p->fd); 23504fb2745SSam Leffler if (!dev) 23604fb2745SSam Leffler break; 23704fb2745SSam Leffler 23804fb2745SSam Leffler PCAP_ASSERT (dev->copy_rx_buf || dev->peek_rx_buf); 23904fb2745SSam Leffler FLUSHK(); 24004fb2745SSam Leffler 24104fb2745SSam Leffler /* If driver has a zero-copy receive facility, peek at the queue, 24204fb2745SSam Leffler * filter it, do the callback and release the buffer. 24304fb2745SSam Leffler */ 24404fb2745SSam Leffler if (dev->peek_rx_buf) 24504fb2745SSam Leffler { 24604fb2745SSam Leffler PCAP_ASSERT (dev->release_rx_buf); 24704fb2745SSam Leffler rx_len = (*dev->peek_rx_buf) (&rx_buf); 24804fb2745SSam Leffler } 24904fb2745SSam Leffler else 25004fb2745SSam Leffler { 25104fb2745SSam Leffler BYTE buf [ETH_MAX+100]; /* add some margin */ 25204fb2745SSam Leffler rx_len = (*dev->copy_rx_buf) (buf, p->snapshot); 25304fb2745SSam Leffler rx_buf = buf; 25404fb2745SSam Leffler } 25504fb2745SSam Leffler 25604fb2745SSam Leffler if (rx_len > 0) /* got a packet */ 25704fb2745SSam Leffler { 25804fb2745SSam Leffler mac_count++; 25904fb2745SSam Leffler 26004fb2745SSam Leffler FLUSHK(); 26104fb2745SSam Leffler 26204fb2745SSam Leffler pcap.caplen = min (rx_len, p->snapshot); 26304fb2745SSam Leffler pcap.len = rx_len; 26404fb2745SSam Leffler 26504fb2745SSam Leffler if (callback && 266a8e07101SRui Paulo (!p->fcode.bf_insns || bpf_filter(p->fcode.bf_insns, rx_buf, pcap.len, pcap.caplen))) 26704fb2745SSam Leffler { 26804fb2745SSam Leffler filter_count++; 26904fb2745SSam Leffler 27004fb2745SSam Leffler /* Fix-me!! Should be time of arrival. Not time of 27104fb2745SSam Leffler * capture. 27204fb2745SSam Leffler */ 27304fb2745SSam Leffler gettimeofday2 (&pcap.ts, NULL); 27404fb2745SSam Leffler (*callback) (data, &pcap, rx_buf); 27504fb2745SSam Leffler } 27604fb2745SSam Leffler 27704fb2745SSam Leffler if (dev->release_rx_buf) 27804fb2745SSam Leffler (*dev->release_rx_buf) (rx_buf); 27904fb2745SSam Leffler 28004fb2745SSam Leffler if (pcap_pkt_debug > 0) 28104fb2745SSam Leffler { 28204fb2745SSam Leffler if (callback == watt32_recv_hook) 28304fb2745SSam Leffler dbug_write ("pcap_recv_hook\n"); 28404fb2745SSam Leffler else dbug_write ("pcap_read_op\n"); 28504fb2745SSam Leffler } 28604fb2745SSam Leffler FLUSHK(); 28704fb2745SSam Leffler return (1); 28804fb2745SSam Leffler } 28904fb2745SSam Leffler 29004fb2745SSam Leffler /* If not to wait for a packet or pcap_close() called from 29104fb2745SSam Leffler * e.g. SIGINT handler, exit loop now. 29204fb2745SSam Leffler */ 293a8e07101SRui Paulo if (p->md.timeout <= 0 || (volatile int)p->fd <= 0) 29404fb2745SSam Leffler break; 29504fb2745SSam Leffler 29604fb2745SSam Leffler gettimeofday2 (&now, NULL); 29704fb2745SSam Leffler 29804fb2745SSam Leffler if (timercmp(&now, &expiry, >)) 29904fb2745SSam Leffler break; 30004fb2745SSam Leffler 30104fb2745SSam Leffler #ifndef DJGPP 30204fb2745SSam Leffler kbhit(); /* a real CPU hog */ 30304fb2745SSam Leffler #endif 30404fb2745SSam Leffler 30504fb2745SSam Leffler if (p->wait_proc) 30604fb2745SSam Leffler (*p->wait_proc)(); /* call yield func */ 30704fb2745SSam Leffler } 30804fb2745SSam Leffler 30904fb2745SSam Leffler if (rx_len < 0) /* receive error */ 31004fb2745SSam Leffler { 31104fb2745SSam Leffler p->md.stat.ps_drop++; 31204fb2745SSam Leffler #ifdef USE_32BIT_DRIVERS 31304fb2745SSam Leffler if (pcap_pkt_debug > 1) 31404fb2745SSam Leffler printk ("pkt-err %s\n", pktInfo.error); 31504fb2745SSam Leffler #endif 31604fb2745SSam Leffler return (-1); 31704fb2745SSam Leffler } 31804fb2745SSam Leffler return (0); 31904fb2745SSam Leffler } 32004fb2745SSam Leffler 32104fb2745SSam Leffler static int 32204fb2745SSam Leffler pcap_read_dos (pcap_t *p, int cnt, pcap_handler callback, u_char *data) 32304fb2745SSam Leffler { 32404fb2745SSam Leffler int rc, num = 0; 32504fb2745SSam Leffler 32604fb2745SSam Leffler while (num <= cnt || (cnt < 0)) 32704fb2745SSam Leffler { 32804fb2745SSam Leffler if (p->fd <= 0) 32904fb2745SSam Leffler return (-1); 33004fb2745SSam Leffler rc = pcap_read_one (p, callback, data); 33104fb2745SSam Leffler if (rc > 0) 33204fb2745SSam Leffler num++; 33304fb2745SSam Leffler if (rc < 0) 33404fb2745SSam Leffler break; 33504fb2745SSam Leffler _w32_os_yield(); /* allow SIGINT generation, yield to Win95/NT */ 33604fb2745SSam Leffler } 33704fb2745SSam Leffler return (num); 33804fb2745SSam Leffler } 33904fb2745SSam Leffler 34004fb2745SSam Leffler /* 34104fb2745SSam Leffler * Return network statistics 34204fb2745SSam Leffler */ 34304fb2745SSam Leffler static int pcap_stats_dos (pcap_t *p, struct pcap_stat *ps) 34404fb2745SSam Leffler { 34504fb2745SSam Leffler struct net_device_stats *stats; 34604fb2745SSam Leffler struct device *dev = p ? get_device(p->fd) : NULL; 34704fb2745SSam Leffler 34804fb2745SSam Leffler if (!dev) 34904fb2745SSam Leffler { 35004fb2745SSam Leffler strcpy (p->errbuf, "illegal pcap handle"); 35104fb2745SSam Leffler return (-1); 35204fb2745SSam Leffler } 35304fb2745SSam Leffler 35404fb2745SSam Leffler if (!dev->get_stats || (stats = (*dev->get_stats)(dev)) == NULL) 35504fb2745SSam Leffler { 35604fb2745SSam Leffler strcpy (p->errbuf, "device statistics not available"); 35704fb2745SSam Leffler return (-1); 35804fb2745SSam Leffler } 35904fb2745SSam Leffler 36004fb2745SSam Leffler FLUSHK(); 36104fb2745SSam Leffler 36204fb2745SSam Leffler p->md.stat.ps_recv = stats->rx_packets; 36304fb2745SSam Leffler p->md.stat.ps_drop += stats->rx_missed_errors; 36404fb2745SSam Leffler p->md.stat.ps_ifdrop = stats->rx_dropped + /* queue full */ 36504fb2745SSam Leffler stats->rx_errors; /* HW errors */ 36604fb2745SSam Leffler if (ps) 36704fb2745SSam Leffler *ps = p->md.stat; 36804fb2745SSam Leffler 36904fb2745SSam Leffler return (0); 37004fb2745SSam Leffler } 37104fb2745SSam Leffler 37204fb2745SSam Leffler /* 37304fb2745SSam Leffler * Return detailed network/device statistics. 37404fb2745SSam Leffler * May be called after 'dev->close' is called. 37504fb2745SSam Leffler */ 37604fb2745SSam Leffler int pcap_stats_ex (pcap_t *p, struct pcap_stat_ex *se) 37704fb2745SSam Leffler { 37804fb2745SSam Leffler struct device *dev = p ? get_device (p->fd) : NULL; 37904fb2745SSam Leffler 38004fb2745SSam Leffler if (!dev || !dev->get_stats) 38104fb2745SSam Leffler { 38204fb2745SSam Leffler strlcpy (p->errbuf, "detailed device statistics not available", 38304fb2745SSam Leffler PCAP_ERRBUF_SIZE); 38404fb2745SSam Leffler return (-1); 38504fb2745SSam Leffler } 38604fb2745SSam Leffler 38704fb2745SSam Leffler if (!strnicmp(dev->name,"pkt",3)) 38804fb2745SSam Leffler { 38904fb2745SSam Leffler strlcpy (p->errbuf, "pktdrvr doesn't have detailed statistics", 39004fb2745SSam Leffler PCAP_ERRBUF_SIZE); 39104fb2745SSam Leffler return (-1); 39204fb2745SSam Leffler } 39304fb2745SSam Leffler memcpy (se, (*dev->get_stats)(dev), sizeof(*se)); 39404fb2745SSam Leffler return (0); 39504fb2745SSam Leffler } 39604fb2745SSam Leffler 39704fb2745SSam Leffler /* 39804fb2745SSam Leffler * Simply store the filter-code for the pcap_read_dos() callback 39904fb2745SSam Leffler * Some day the filter-code could be handed down to the active 40004fb2745SSam Leffler * device (pkt_rx1.s or 32-bit device interrupt handler). 40104fb2745SSam Leffler */ 40204fb2745SSam Leffler static int pcap_setfilter_dos (pcap_t *p, struct bpf_program *fp) 40304fb2745SSam Leffler { 40404fb2745SSam Leffler if (!p) 40504fb2745SSam Leffler return (-1); 40604fb2745SSam Leffler p->fcode = *fp; 40704fb2745SSam Leffler return (0); 40804fb2745SSam Leffler } 40904fb2745SSam Leffler 41004fb2745SSam Leffler /* 41104fb2745SSam Leffler * Return # of packets received in pcap_read_dos() 41204fb2745SSam Leffler */ 41304fb2745SSam Leffler u_long pcap_mac_packets (void) 41404fb2745SSam Leffler { 41504fb2745SSam Leffler return (mac_count); 41604fb2745SSam Leffler } 41704fb2745SSam Leffler 41804fb2745SSam Leffler /* 41904fb2745SSam Leffler * Return # of packets passed through filter in pcap_read_dos() 42004fb2745SSam Leffler */ 42104fb2745SSam Leffler u_long pcap_filter_packets (void) 42204fb2745SSam Leffler { 42304fb2745SSam Leffler return (filter_count); 42404fb2745SSam Leffler } 42504fb2745SSam Leffler 42604fb2745SSam Leffler /* 42704fb2745SSam Leffler * Close pcap device. Not called for offline captures. 42804fb2745SSam Leffler */ 429a8e07101SRui Paulo static void pcap_cleanup_dos (pcap_t *p) 43004fb2745SSam Leffler { 43104fb2745SSam Leffler if (p && !exc_occured) 43204fb2745SSam Leffler { 43304fb2745SSam Leffler if (pcap_stats(p,NULL) < 0) 43404fb2745SSam Leffler p->md.stat.ps_drop = 0; 43504fb2745SSam Leffler if (!get_device(p->fd)) 43604fb2745SSam Leffler return; 43704fb2745SSam Leffler 43804fb2745SSam Leffler handle_to_device [p->fd-1] = NULL; 43904fb2745SSam Leffler p->fd = 0; 44004fb2745SSam Leffler if (ref_count > 0) 44104fb2745SSam Leffler ref_count--; 44204fb2745SSam Leffler if (ref_count > 0) 44304fb2745SSam Leffler return; 44404fb2745SSam Leffler } 44504fb2745SSam Leffler close_driver(); 44604fb2745SSam Leffler } 44704fb2745SSam Leffler 44804fb2745SSam Leffler /* 44904fb2745SSam Leffler * Return the name of the 1st network interface, 45004fb2745SSam Leffler * or NULL if none can be found. 45104fb2745SSam Leffler */ 45204fb2745SSam Leffler char *pcap_lookupdev (char *ebuf) 45304fb2745SSam Leffler { 45404fb2745SSam Leffler struct device *dev; 45504fb2745SSam Leffler 45604fb2745SSam Leffler #ifdef USE_32BIT_DRIVERS 45704fb2745SSam Leffler init_32bit(); 45804fb2745SSam Leffler #endif 45904fb2745SSam Leffler 46004fb2745SSam Leffler for (dev = (struct device*)dev_base; dev; dev = dev->next) 46104fb2745SSam Leffler { 46204fb2745SSam Leffler PCAP_ASSERT (dev->probe); 46304fb2745SSam Leffler 46404fb2745SSam Leffler if ((*dev->probe)(dev)) 46504fb2745SSam Leffler { 46604fb2745SSam Leffler FLUSHK(); 46704fb2745SSam Leffler probed_dev = (struct device*) dev; /* remember last probed device */ 46804fb2745SSam Leffler return (char*) dev->name; 46904fb2745SSam Leffler } 47004fb2745SSam Leffler } 47104fb2745SSam Leffler 47204fb2745SSam Leffler if (ebuf) 47304fb2745SSam Leffler strcpy (ebuf, "No driver found"); 47404fb2745SSam Leffler return (NULL); 47504fb2745SSam Leffler } 47604fb2745SSam Leffler 47704fb2745SSam Leffler /* 47804fb2745SSam Leffler * Gets localnet & netmask from Watt-32. 47904fb2745SSam Leffler */ 48004fb2745SSam Leffler int pcap_lookupnet (const char *device, bpf_u_int32 *localnet, 48104fb2745SSam Leffler bpf_u_int32 *netmask, char *errbuf) 48204fb2745SSam Leffler { 48304fb2745SSam Leffler if (!_watt_is_init) 48404fb2745SSam Leffler { 485a8e07101SRui Paulo strcpy (errbuf, "pcap_open_offline() or pcap_activate() must be " 48604fb2745SSam Leffler "called first"); 48704fb2745SSam Leffler return (-1); 48804fb2745SSam Leffler } 48904fb2745SSam Leffler 49004fb2745SSam Leffler *netmask = _w32_sin_mask; 49104fb2745SSam Leffler *localnet = my_ip_addr & *netmask; 49204fb2745SSam Leffler if (*localnet == 0) 49304fb2745SSam Leffler { 49404fb2745SSam Leffler if (IN_CLASSA(*netmask)) 49504fb2745SSam Leffler *localnet = IN_CLASSA_NET; 49604fb2745SSam Leffler else if (IN_CLASSB(*netmask)) 49704fb2745SSam Leffler *localnet = IN_CLASSB_NET; 49804fb2745SSam Leffler else if (IN_CLASSC(*netmask)) 49904fb2745SSam Leffler *localnet = IN_CLASSC_NET; 50004fb2745SSam Leffler else 50104fb2745SSam Leffler { 50204fb2745SSam Leffler sprintf (errbuf, "inet class for 0x%lx unknown", *netmask); 50304fb2745SSam Leffler return (-1); 50404fb2745SSam Leffler } 50504fb2745SSam Leffler } 50604fb2745SSam Leffler ARGSUSED (device); 50704fb2745SSam Leffler return (0); 50804fb2745SSam Leffler } 50904fb2745SSam Leffler 51004fb2745SSam Leffler /* 51104fb2745SSam Leffler * Get a list of all interfaces that are present and that we probe okay. 51204fb2745SSam Leffler * Returns -1 on error, 0 otherwise. 51304fb2745SSam Leffler * The list, as returned through "alldevsp", may be null if no interfaces 51404fb2745SSam Leffler * were up and could be opened. 51504fb2745SSam Leffler */ 51604fb2745SSam Leffler int pcap_findalldevs (pcap_if_t **alldevsp, char *errbuf) 51704fb2745SSam Leffler { 51804fb2745SSam Leffler struct device *dev; 51904fb2745SSam Leffler struct sockaddr_ll sa_ll_1, sa_ll_2; 52004fb2745SSam Leffler struct sockaddr *addr, *netmask, *broadaddr, *dstaddr; 52104fb2745SSam Leffler pcap_if_t *devlist = NULL; 52204fb2745SSam Leffler int ret = 0; 52304fb2745SSam Leffler size_t addr_size = sizeof(struct sockaddr_ll); 52404fb2745SSam Leffler 52504fb2745SSam Leffler for (dev = (struct device*)dev_base; dev; dev = dev->next) 52604fb2745SSam Leffler { 52704fb2745SSam Leffler PCAP_ASSERT (dev->probe); 52804fb2745SSam Leffler 52904fb2745SSam Leffler if (!(*dev->probe)(dev)) 53004fb2745SSam Leffler continue; 53104fb2745SSam Leffler 53204fb2745SSam Leffler PCAP_ASSERT (dev->close); /* set by probe routine */ 53304fb2745SSam Leffler FLUSHK(); 53404fb2745SSam Leffler (*dev->close) (dev); 53504fb2745SSam Leffler 53604fb2745SSam Leffler memset (&sa_ll_1, 0, sizeof(sa_ll_1)); 53704fb2745SSam Leffler memset (&sa_ll_2, 0, sizeof(sa_ll_2)); 53804fb2745SSam Leffler sa_ll_1.sll_family = AF_PACKET; 53904fb2745SSam Leffler sa_ll_2.sll_family = AF_PACKET; 54004fb2745SSam Leffler 54104fb2745SSam Leffler addr = (struct sockaddr*) &sa_ll_1; 54204fb2745SSam Leffler netmask = (struct sockaddr*) &sa_ll_1; 54304fb2745SSam Leffler dstaddr = (struct sockaddr*) &sa_ll_1; 54404fb2745SSam Leffler broadaddr = (struct sockaddr*) &sa_ll_2; 54504fb2745SSam Leffler memset (&sa_ll_2.sll_addr, 0xFF, sizeof(sa_ll_2.sll_addr)); 54604fb2745SSam Leffler 54704fb2745SSam Leffler if (pcap_add_if(&devlist, dev->name, dev->flags, 54804fb2745SSam Leffler dev->long_name, errbuf) < 0) 54904fb2745SSam Leffler { 55004fb2745SSam Leffler ret = -1; 55104fb2745SSam Leffler break; 55204fb2745SSam Leffler } 55304fb2745SSam Leffler if (add_addr_to_iflist(&devlist,dev->name, dev->flags, addr, addr_size, 55404fb2745SSam Leffler netmask, addr_size, broadaddr, addr_size, 55504fb2745SSam Leffler dstaddr, addr_size, errbuf) < 0) 55604fb2745SSam Leffler { 55704fb2745SSam Leffler ret = -1; 55804fb2745SSam Leffler break; 55904fb2745SSam Leffler } 56004fb2745SSam Leffler } 56104fb2745SSam Leffler 56204fb2745SSam Leffler if (devlist && ret < 0) 56304fb2745SSam Leffler { 56404fb2745SSam Leffler pcap_freealldevs (devlist); 56504fb2745SSam Leffler devlist = NULL; 56604fb2745SSam Leffler } 56704fb2745SSam Leffler else 56804fb2745SSam Leffler if (!devlist) 56904fb2745SSam Leffler strcpy (errbuf, "No drivers found"); 57004fb2745SSam Leffler 57104fb2745SSam Leffler *alldevsp = devlist; 57204fb2745SSam Leffler return (ret); 57304fb2745SSam Leffler } 57404fb2745SSam Leffler 57504fb2745SSam Leffler /* 57604fb2745SSam Leffler * pcap_assert() is mainly used for debugging 57704fb2745SSam Leffler */ 57804fb2745SSam Leffler void pcap_assert (const char *what, const char *file, unsigned line) 57904fb2745SSam Leffler { 58004fb2745SSam Leffler FLUSHK(); 58104fb2745SSam Leffler fprintf (stderr, "%s (%u): Assertion \"%s\" failed\n", 58204fb2745SSam Leffler file, line, what); 58304fb2745SSam Leffler close_driver(); 58404fb2745SSam Leffler _exit (-1); 58504fb2745SSam Leffler } 58604fb2745SSam Leffler 58704fb2745SSam Leffler /* 58804fb2745SSam Leffler * For pcap_offline_read(): wait and yield between printing packets 58904fb2745SSam Leffler * to simulate the pace packets where actually recorded. 59004fb2745SSam Leffler */ 59104fb2745SSam Leffler void pcap_set_wait (pcap_t *p, void (*yield)(void), int wait) 59204fb2745SSam Leffler { 59304fb2745SSam Leffler if (p) 59404fb2745SSam Leffler { 59504fb2745SSam Leffler p->wait_proc = yield; 596a8e07101SRui Paulo p->md.timeout = wait; 59704fb2745SSam Leffler } 59804fb2745SSam Leffler } 59904fb2745SSam Leffler 60004fb2745SSam Leffler /* 60104fb2745SSam Leffler * Initialise a named network device. 60204fb2745SSam Leffler */ 60304fb2745SSam Leffler static struct device * 60404fb2745SSam Leffler open_driver (const char *dev_name, char *ebuf, int promisc) 60504fb2745SSam Leffler { 60604fb2745SSam Leffler struct device *dev; 60704fb2745SSam Leffler 60804fb2745SSam Leffler for (dev = (struct device*)dev_base; dev; dev = dev->next) 60904fb2745SSam Leffler { 61004fb2745SSam Leffler PCAP_ASSERT (dev->name); 61104fb2745SSam Leffler 61204fb2745SSam Leffler if (strcmp (dev_name,dev->name)) 61304fb2745SSam Leffler continue; 61404fb2745SSam Leffler 61504fb2745SSam Leffler if (!probed_dev) /* user didn't call pcap_lookupdev() first */ 61604fb2745SSam Leffler { 61704fb2745SSam Leffler PCAP_ASSERT (dev->probe); 61804fb2745SSam Leffler 61904fb2745SSam Leffler if (!(*dev->probe)(dev)) /* call the xx_probe() function */ 62004fb2745SSam Leffler { 62104fb2745SSam Leffler sprintf (ebuf, "failed to detect device `%s'", dev_name); 62204fb2745SSam Leffler return (NULL); 62304fb2745SSam Leffler } 62404fb2745SSam Leffler probed_dev = dev; /* device is probed okay and may be used */ 62504fb2745SSam Leffler } 62604fb2745SSam Leffler else if (dev != probed_dev) 62704fb2745SSam Leffler { 62804fb2745SSam Leffler goto not_probed; 62904fb2745SSam Leffler } 63004fb2745SSam Leffler 63104fb2745SSam Leffler FLUSHK(); 63204fb2745SSam Leffler 63304fb2745SSam Leffler /* Select what traffic to receive 63404fb2745SSam Leffler */ 63504fb2745SSam Leffler if (promisc) 63604fb2745SSam Leffler dev->flags |= (IFF_ALLMULTI | IFF_PROMISC); 63704fb2745SSam Leffler else dev->flags &= ~(IFF_ALLMULTI | IFF_PROMISC); 63804fb2745SSam Leffler 63904fb2745SSam Leffler PCAP_ASSERT (dev->open); 64004fb2745SSam Leffler 64104fb2745SSam Leffler if (!(*dev->open)(dev)) 64204fb2745SSam Leffler { 64304fb2745SSam Leffler sprintf (ebuf, "failed to activate device `%s'", dev_name); 64404fb2745SSam Leffler if (pktInfo.error && !strncmp(dev->name,"pkt",3)) 64504fb2745SSam Leffler { 64604fb2745SSam Leffler strcat (ebuf, ": "); 64704fb2745SSam Leffler strcat (ebuf, pktInfo.error); 64804fb2745SSam Leffler } 64904fb2745SSam Leffler return (NULL); 65004fb2745SSam Leffler } 65104fb2745SSam Leffler 65204fb2745SSam Leffler /* Some devices need this to operate in promiscous mode 65304fb2745SSam Leffler */ 65404fb2745SSam Leffler if (promisc && dev->set_multicast_list) 65504fb2745SSam Leffler (*dev->set_multicast_list) (dev); 65604fb2745SSam Leffler 65704fb2745SSam Leffler active_dev = dev; /* remember our active device */ 65804fb2745SSam Leffler break; 65904fb2745SSam Leffler } 66004fb2745SSam Leffler 66104fb2745SSam Leffler /* 'dev_name' not matched in 'dev_base' list. 66204fb2745SSam Leffler */ 66304fb2745SSam Leffler if (!dev) 66404fb2745SSam Leffler { 66504fb2745SSam Leffler sprintf (ebuf, "device `%s' not supported", dev_name); 66604fb2745SSam Leffler return (NULL); 66704fb2745SSam Leffler } 66804fb2745SSam Leffler 66904fb2745SSam Leffler not_probed: 67004fb2745SSam Leffler if (!probed_dev) 67104fb2745SSam Leffler { 67204fb2745SSam Leffler sprintf (ebuf, "device `%s' not probed", dev_name); 67304fb2745SSam Leffler return (NULL); 67404fb2745SSam Leffler } 67504fb2745SSam Leffler return (dev); 67604fb2745SSam Leffler } 67704fb2745SSam Leffler 67804fb2745SSam Leffler /* 67904fb2745SSam Leffler * Deinitialise MAC driver. 68004fb2745SSam Leffler * Set receive mode back to default mode. 68104fb2745SSam Leffler */ 68204fb2745SSam Leffler static void close_driver (void) 68304fb2745SSam Leffler { 68404fb2745SSam Leffler /* !!todo: loop over all 'handle_to_device[]' ? */ 68504fb2745SSam Leffler struct device *dev = active_dev; 68604fb2745SSam Leffler 68704fb2745SSam Leffler if (dev && dev->close) 68804fb2745SSam Leffler { 68904fb2745SSam Leffler (*dev->close) (dev); 69004fb2745SSam Leffler FLUSHK(); 69104fb2745SSam Leffler } 69204fb2745SSam Leffler 69304fb2745SSam Leffler active_dev = NULL; 69404fb2745SSam Leffler 69504fb2745SSam Leffler #ifdef USE_32BIT_DRIVERS 69604fb2745SSam Leffler if (rx_pool) 69704fb2745SSam Leffler { 69804fb2745SSam Leffler k_free (rx_pool); 69904fb2745SSam Leffler rx_pool = NULL; 70004fb2745SSam Leffler } 70104fb2745SSam Leffler if (dev) 70204fb2745SSam Leffler pcibios_exit(); 70304fb2745SSam Leffler #endif 70404fb2745SSam Leffler } 70504fb2745SSam Leffler 70604fb2745SSam Leffler 70704fb2745SSam Leffler #ifdef __DJGPP__ 70804fb2745SSam Leffler static void setup_signals (void (*handler)(int)) 70904fb2745SSam Leffler { 71004fb2745SSam Leffler signal (SIGSEGV,handler); 71104fb2745SSam Leffler signal (SIGILL, handler); 71204fb2745SSam Leffler signal (SIGFPE, handler); 71304fb2745SSam Leffler } 71404fb2745SSam Leffler 71504fb2745SSam Leffler static void exc_handler (int sig) 71604fb2745SSam Leffler { 71704fb2745SSam Leffler #ifdef USE_32BIT_DRIVERS 71804fb2745SSam Leffler if (active_dev->irq > 0) /* excludes IRQ 0 */ 71904fb2745SSam Leffler { 72004fb2745SSam Leffler disable_irq (active_dev->irq); 72104fb2745SSam Leffler irq_eoi_cmd (active_dev->irq); 72204fb2745SSam Leffler _printk_safe = 1; 72304fb2745SSam Leffler } 72404fb2745SSam Leffler #endif 72504fb2745SSam Leffler 72604fb2745SSam Leffler switch (sig) 72704fb2745SSam Leffler { 72804fb2745SSam Leffler case SIGSEGV: 72904fb2745SSam Leffler fputs ("Catching SIGSEGV.\n", stderr); 73004fb2745SSam Leffler break; 73104fb2745SSam Leffler case SIGILL: 73204fb2745SSam Leffler fputs ("Catching SIGILL.\n", stderr); 73304fb2745SSam Leffler break; 73404fb2745SSam Leffler case SIGFPE: 73504fb2745SSam Leffler _fpreset(); 73604fb2745SSam Leffler fputs ("Catching SIGFPE.\n", stderr); 73704fb2745SSam Leffler break; 73804fb2745SSam Leffler default: 73904fb2745SSam Leffler fprintf (stderr, "Catching signal %d.\n", sig); 74004fb2745SSam Leffler } 74104fb2745SSam Leffler exc_occured = 1; 742a8e07101SRui Paulo pcap_cleanup_dos (NULL); 74304fb2745SSam Leffler } 74404fb2745SSam Leffler #endif /* __DJGPP__ */ 74504fb2745SSam Leffler 74604fb2745SSam Leffler 74704fb2745SSam Leffler /* 748a8e07101SRui Paulo * Open the pcap device for the first client calling pcap_activate() 74904fb2745SSam Leffler */ 75004fb2745SSam Leffler static int first_init (const char *name, char *ebuf, int promisc) 75104fb2745SSam Leffler { 75204fb2745SSam Leffler struct device *dev; 75304fb2745SSam Leffler 75404fb2745SSam Leffler #ifdef USE_32BIT_DRIVERS 75504fb2745SSam Leffler rx_pool = k_calloc (RECEIVE_BUF_SIZE, RECEIVE_QUEUE_SIZE); 75604fb2745SSam Leffler if (!rx_pool) 75704fb2745SSam Leffler { 75804fb2745SSam Leffler strcpy (ebuf, "Not enough memory (Rx pool)"); 75904fb2745SSam Leffler return (0); 76004fb2745SSam Leffler } 76104fb2745SSam Leffler #endif 76204fb2745SSam Leffler 76304fb2745SSam Leffler #ifdef __DJGPP__ 76404fb2745SSam Leffler setup_signals (exc_handler); 76504fb2745SSam Leffler #endif 76604fb2745SSam Leffler 76704fb2745SSam Leffler #ifdef USE_32BIT_DRIVERS 76804fb2745SSam Leffler init_32bit(); 76904fb2745SSam Leffler #endif 77004fb2745SSam Leffler 77104fb2745SSam Leffler dev = open_driver (name, ebuf, promisc); 77204fb2745SSam Leffler if (!dev) 77304fb2745SSam Leffler { 77404fb2745SSam Leffler #ifdef USE_32BIT_DRIVERS 77504fb2745SSam Leffler k_free (rx_pool); 77604fb2745SSam Leffler rx_pool = NULL; 77704fb2745SSam Leffler #endif 77804fb2745SSam Leffler 77904fb2745SSam Leffler #ifdef __DJGPP__ 78004fb2745SSam Leffler setup_signals (SIG_DFL); 78104fb2745SSam Leffler #endif 78204fb2745SSam Leffler return (0); 78304fb2745SSam Leffler } 78404fb2745SSam Leffler 78504fb2745SSam Leffler #ifdef USE_32BIT_DRIVERS 78604fb2745SSam Leffler /* 78704fb2745SSam Leffler * If driver is NOT a 16-bit "pkt/ndis" driver (having a 'copy_rx_buf' 78804fb2745SSam Leffler * set in it's probe handler), initialise near-memory ring-buffer for 78904fb2745SSam Leffler * the 32-bit device. 79004fb2745SSam Leffler */ 79104fb2745SSam Leffler if (dev->copy_rx_buf == NULL) 79204fb2745SSam Leffler { 79304fb2745SSam Leffler dev->get_rx_buf = get_rxbuf; 79404fb2745SSam Leffler dev->peek_rx_buf = peek_rxbuf; 79504fb2745SSam Leffler dev->release_rx_buf = release_rxbuf; 79604fb2745SSam Leffler pktq_init (&dev->queue, RECEIVE_BUF_SIZE, RECEIVE_QUEUE_SIZE, rx_pool); 79704fb2745SSam Leffler } 79804fb2745SSam Leffler #endif 79904fb2745SSam Leffler return (1); 80004fb2745SSam Leffler } 80104fb2745SSam Leffler 80204fb2745SSam Leffler #ifdef USE_32BIT_DRIVERS 80304fb2745SSam Leffler static void init_32bit (void) 80404fb2745SSam Leffler { 80504fb2745SSam Leffler static int init_pci = 0; 80604fb2745SSam Leffler 80704fb2745SSam Leffler if (!_printk_file) 80804fb2745SSam Leffler _printk_init (64*1024, NULL); /* calls atexit(printk_exit) */ 80904fb2745SSam Leffler 81004fb2745SSam Leffler if (!init_pci) 81104fb2745SSam Leffler (void)pci_init(); /* init BIOS32+PCI interface */ 81204fb2745SSam Leffler init_pci = 1; 81304fb2745SSam Leffler } 81404fb2745SSam Leffler #endif 81504fb2745SSam Leffler 81604fb2745SSam Leffler 81704fb2745SSam Leffler /* 81804fb2745SSam Leffler * Hook functions for using Watt-32 together with pcap 81904fb2745SSam Leffler */ 82004fb2745SSam Leffler static char rxbuf [ETH_MAX+100]; /* rx-buffer with some margin */ 82104fb2745SSam Leffler static WORD etype; 82204fb2745SSam Leffler static pcap_t pcap_save; 82304fb2745SSam Leffler 82404fb2745SSam Leffler static void watt32_recv_hook (u_char *dummy, const struct pcap_pkthdr *pcap, 82504fb2745SSam Leffler const u_char *buf) 82604fb2745SSam Leffler { 82704fb2745SSam Leffler /* Fix me: assumes Ethernet II only */ 82804fb2745SSam Leffler struct ether_header *ep = (struct ether_header*) buf; 82904fb2745SSam Leffler 83004fb2745SSam Leffler memcpy (rxbuf, buf, pcap->caplen); 83104fb2745SSam Leffler etype = ep->ether_type; 83204fb2745SSam Leffler ARGSUSED (dummy); 83304fb2745SSam Leffler } 83404fb2745SSam Leffler 83504fb2745SSam Leffler #if (WATTCP_VER >= 0x0224) 83604fb2745SSam Leffler /* 83704fb2745SSam Leffler * This function is used by Watt-32 to poll for a packet. 83804fb2745SSam Leffler * i.e. it's set to bypass _eth_arrived() 83904fb2745SSam Leffler */ 84004fb2745SSam Leffler static void *pcap_recv_hook (WORD *type) 84104fb2745SSam Leffler { 84204fb2745SSam Leffler int len = pcap_read_dos (&pcap_save, 1, watt32_recv_hook, NULL); 84304fb2745SSam Leffler 84404fb2745SSam Leffler if (len < 0) 84504fb2745SSam Leffler return (NULL); 84604fb2745SSam Leffler 84704fb2745SSam Leffler *type = etype; 84804fb2745SSam Leffler return (void*) &rxbuf; 84904fb2745SSam Leffler } 85004fb2745SSam Leffler 85104fb2745SSam Leffler /* 85204fb2745SSam Leffler * This function is called by Watt-32 (via _eth_xmit_hook). 85304fb2745SSam Leffler * If dbug_init() was called, we should trace packets sent. 85404fb2745SSam Leffler */ 85504fb2745SSam Leffler static int pcap_xmit_hook (const void *buf, unsigned len) 85604fb2745SSam Leffler { 85704fb2745SSam Leffler int rc = 0; 85804fb2745SSam Leffler 85904fb2745SSam Leffler if (pcap_pkt_debug > 0) 86004fb2745SSam Leffler dbug_write ("pcap_xmit_hook: "); 86104fb2745SSam Leffler 86204fb2745SSam Leffler if (active_dev && active_dev->xmit) 86304fb2745SSam Leffler if ((*active_dev->xmit) (active_dev, buf, len) > 0) 86404fb2745SSam Leffler rc = len; 86504fb2745SSam Leffler 86604fb2745SSam Leffler if (pcap_pkt_debug > 0) 86704fb2745SSam Leffler dbug_write (rc ? "ok\n" : "fail\n"); 86804fb2745SSam Leffler return (rc); 86904fb2745SSam Leffler } 87004fb2745SSam Leffler #endif 87104fb2745SSam Leffler 87204fb2745SSam Leffler static int pcap_sendpacket_dos (pcap_t *p, const void *buf, size_t len) 87304fb2745SSam Leffler { 87404fb2745SSam Leffler struct device *dev = p ? get_device(p->fd) : NULL; 87504fb2745SSam Leffler 87604fb2745SSam Leffler if (!dev || !dev->xmit) 87704fb2745SSam Leffler return (-1); 87804fb2745SSam Leffler return (*dev->xmit) (dev, buf, len); 87904fb2745SSam Leffler } 88004fb2745SSam Leffler 88104fb2745SSam Leffler /* 88204fb2745SSam Leffler * This function is called by Watt-32 in tcp_post_init(). 88304fb2745SSam Leffler * We should prevent Watt-32 from using BOOTP/DHCP/RARP etc. 88404fb2745SSam Leffler */ 88504fb2745SSam Leffler static void (*prev_post_hook) (void); 88604fb2745SSam Leffler 88704fb2745SSam Leffler static void pcap_init_hook (void) 88804fb2745SSam Leffler { 88904fb2745SSam Leffler _w32__bootp_on = _w32__dhcp_on = _w32__rarp_on = 0; 89004fb2745SSam Leffler _w32__do_mask_req = 0; 89104fb2745SSam Leffler _w32_dynamic_host = 0; 89204fb2745SSam Leffler if (prev_post_hook) 89304fb2745SSam Leffler (*prev_post_hook)(); 89404fb2745SSam Leffler } 89504fb2745SSam Leffler 89604fb2745SSam Leffler /* 89704fb2745SSam Leffler * Supress PRINT message from Watt-32's sock_init() 89804fb2745SSam Leffler */ 89904fb2745SSam Leffler static void null_print (void) {} 90004fb2745SSam Leffler 90104fb2745SSam Leffler /* 90204fb2745SSam Leffler * To use features of Watt-32 (netdb functions and socket etc.) 90304fb2745SSam Leffler * we must call sock_init(). But we set various hooks to prevent 90404fb2745SSam Leffler * using normal PKTDRVR functions in pcpkt.c. This should hopefully 90504fb2745SSam Leffler * make Watt-32 and pcap co-operate. 90604fb2745SSam Leffler */ 90704fb2745SSam Leffler static int init_watt32 (struct pcap *pcap, const char *dev_name, char *err_buf) 90804fb2745SSam Leffler { 90904fb2745SSam Leffler char *env; 91004fb2745SSam Leffler int rc, MTU, has_ip_addr; 91104fb2745SSam Leffler int using_pktdrv = 1; 91204fb2745SSam Leffler 91304fb2745SSam Leffler /* If user called sock_init() first, we need to reinit in 91404fb2745SSam Leffler * order to open debug/trace-file properly 91504fb2745SSam Leffler */ 91604fb2745SSam Leffler if (_watt_is_init) 91704fb2745SSam Leffler sock_exit(); 91804fb2745SSam Leffler 91904fb2745SSam Leffler env = getenv ("PCAP_DEBUG"); 92004fb2745SSam Leffler if (env && atoi(env) > 0 && 92104fb2745SSam Leffler pcap_pkt_debug < 0) /* if not already set */ 92204fb2745SSam Leffler { 92304fb2745SSam Leffler dbug_init(); 92404fb2745SSam Leffler pcap_pkt_debug = atoi (env); 92504fb2745SSam Leffler } 92604fb2745SSam Leffler 92704fb2745SSam Leffler _watt_do_exit = 0; /* prevent sock_init() calling exit() */ 92804fb2745SSam Leffler prev_post_hook = _w32_usr_post_init; 92904fb2745SSam Leffler _w32_usr_post_init = pcap_init_hook; 93004fb2745SSam Leffler _w32_print_hook = null_print; 93104fb2745SSam Leffler 93204fb2745SSam Leffler if (dev_name && strncmp(dev_name,"pkt",3)) 93304fb2745SSam Leffler using_pktdrv = FALSE; 93404fb2745SSam Leffler 93504fb2745SSam Leffler rc = sock_init(); 93604fb2745SSam Leffler has_ip_addr = (rc != 8); /* IP-address assignment failed */ 93704fb2745SSam Leffler 93804fb2745SSam Leffler /* if pcap is using a 32-bit driver w/o a pktdrvr loaded, we 93904fb2745SSam Leffler * just pretend Watt-32 is initialised okay. 94004fb2745SSam Leffler * 94104fb2745SSam Leffler * !! fix-me: The Watt-32 config isn't done if no pktdrvr 94204fb2745SSam Leffler * was found. In that case my_ip_addr + sin_mask 94304fb2745SSam Leffler * have default values. Should be taken from another 94404fb2745SSam Leffler * ini-file/environment in any case (ref. tcpdump.ini) 94504fb2745SSam Leffler */ 94604fb2745SSam Leffler _watt_is_init = 1; 94704fb2745SSam Leffler 94804fb2745SSam Leffler if (!using_pktdrv || !has_ip_addr) /* for now .... */ 94904fb2745SSam Leffler { 95004fb2745SSam Leffler static const char myip[] = "192.168.0.1"; 95104fb2745SSam Leffler static const char mask[] = "255.255.255.0"; 95204fb2745SSam Leffler 95304fb2745SSam Leffler printf ("Just guessing, using IP %s and netmask %s\n", myip, mask); 95404fb2745SSam Leffler my_ip_addr = aton (myip); 95504fb2745SSam Leffler _w32_sin_mask = aton (mask); 95604fb2745SSam Leffler } 95704fb2745SSam Leffler else if (rc && using_pktdrv) 95804fb2745SSam Leffler { 95904fb2745SSam Leffler sprintf (err_buf, "sock_init() failed, code %d", rc); 96004fb2745SSam Leffler return (0); 96104fb2745SSam Leffler } 96204fb2745SSam Leffler 96304fb2745SSam Leffler /* Set recv-hook for peeking in _eth_arrived(). 96404fb2745SSam Leffler */ 96504fb2745SSam Leffler #if (WATTCP_VER >= 0x0224) 96604fb2745SSam Leffler _eth_recv_hook = pcap_recv_hook; 96704fb2745SSam Leffler _eth_xmit_hook = pcap_xmit_hook; 96804fb2745SSam Leffler #endif 96904fb2745SSam Leffler 97004fb2745SSam Leffler /* Free the pkt-drvr handle allocated in pkt_init(). 97104fb2745SSam Leffler * The above hooks should thus use the handle reopened in open_driver() 97204fb2745SSam Leffler */ 97304fb2745SSam Leffler if (using_pktdrv) 97404fb2745SSam Leffler { 97504fb2745SSam Leffler _eth_release(); 97604fb2745SSam Leffler /* _eth_is_init = 1; */ /* hack to get Rx/Tx-hooks in Watt-32 working */ 97704fb2745SSam Leffler } 97804fb2745SSam Leffler 97904fb2745SSam Leffler memcpy (&pcap_save, pcap, sizeof(pcap_save)); 98004fb2745SSam Leffler MTU = pkt_get_mtu(); 98104fb2745SSam Leffler pcap_save.fcode.bf_insns = NULL; 98204fb2745SSam Leffler pcap_save.linktype = _eth_get_hwtype (NULL, NULL); 98304fb2745SSam Leffler pcap_save.snapshot = MTU > 0 ? MTU : ETH_MAX; /* assume 1514 */ 98404fb2745SSam Leffler 98504fb2745SSam Leffler #if 1 98604fb2745SSam Leffler /* prevent use of resolve() and resolve_ip() 98704fb2745SSam Leffler */ 98804fb2745SSam Leffler last_nameserver = 0; 98904fb2745SSam Leffler #endif 99004fb2745SSam Leffler return (1); 99104fb2745SSam Leffler } 99204fb2745SSam Leffler 99304fb2745SSam Leffler int EISA_bus = 0; /* Where is natural place for this? */ 99404fb2745SSam Leffler 99504fb2745SSam Leffler /* 99604fb2745SSam Leffler * Application config hooks to set various driver parameters. 99704fb2745SSam Leffler */ 99804fb2745SSam Leffler 999a8e07101SRui Paulo static const struct config_table debug_tab[] = { 100004fb2745SSam Leffler { "PKT.DEBUG", ARG_ATOI, &pcap_pkt_debug }, 100104fb2745SSam Leffler { "PKT.VECTOR", ARG_ATOX_W, NULL }, 100204fb2745SSam Leffler { "NDIS.DEBUG", ARG_ATOI, NULL }, 100304fb2745SSam Leffler #ifdef USE_32BIT_DRIVERS 100404fb2745SSam Leffler { "3C503.DEBUG", ARG_ATOI, &ei_debug }, 100504fb2745SSam Leffler { "3C503.IO_BASE", ARG_ATOX_W, &el2_dev.base_addr }, 100604fb2745SSam Leffler { "3C503.MEMORY", ARG_ATOX_W, &el2_dev.mem_start }, 100704fb2745SSam Leffler { "3C503.IRQ", ARG_ATOI, &el2_dev.irq }, 100804fb2745SSam Leffler { "3C505.DEBUG", ARG_ATOI, NULL }, 100904fb2745SSam Leffler { "3C505.BASE", ARG_ATOX_W, NULL }, 101004fb2745SSam Leffler { "3C507.DEBUG", ARG_ATOI, NULL }, 101104fb2745SSam Leffler { "3C509.DEBUG", ARG_ATOI, &el3_debug }, 101204fb2745SSam Leffler { "3C509.ILOOP", ARG_ATOI, &el3_max_loop }, 101304fb2745SSam Leffler { "3C529.DEBUG", ARG_ATOI, NULL }, 101404fb2745SSam Leffler { "3C575.DEBUG", ARG_ATOI, &debug_3c575 }, 101504fb2745SSam Leffler { "3C59X.DEBUG", ARG_ATOI, &vortex_debug }, 101604fb2745SSam Leffler { "3C59X.IFACE0", ARG_ATOI, &vortex_options[0] }, 101704fb2745SSam Leffler { "3C59X.IFACE1", ARG_ATOI, &vortex_options[1] }, 101804fb2745SSam Leffler { "3C59X.IFACE2", ARG_ATOI, &vortex_options[2] }, 101904fb2745SSam Leffler { "3C59X.IFACE3", ARG_ATOI, &vortex_options[3] }, 102004fb2745SSam Leffler { "3C90X.DEBUG", ARG_ATOX_W, &tc90xbc_debug }, 102104fb2745SSam Leffler { "ACCT.DEBUG", ARG_ATOI, ðpk_debug }, 102204fb2745SSam Leffler { "CS89.DEBUG", ARG_ATOI, &cs89_debug }, 102304fb2745SSam Leffler { "RTL8139.DEBUG", ARG_ATOI, &rtl8139_debug }, 102404fb2745SSam Leffler /* { "RTL8139.FDUPLEX", ARG_ATOI, &rtl8139_options }, */ 102504fb2745SSam Leffler { "SMC.DEBUG", ARG_ATOI, &ei_debug }, 102604fb2745SSam Leffler /* { "E100.DEBUG", ARG_ATOI, &e100_debug }, */ 102704fb2745SSam Leffler { "PCI.DEBUG", ARG_ATOI, &pci_debug }, 102804fb2745SSam Leffler { "BIOS32.DEBUG", ARG_ATOI, &bios32_debug }, 102904fb2745SSam Leffler { "IRQ.DEBUG", ARG_ATOI, &irq_debug }, 103004fb2745SSam Leffler { "TIMER.IRQ", ARG_ATOI, &timer_irq }, 103104fb2745SSam Leffler #endif 103204fb2745SSam Leffler { NULL } 103304fb2745SSam Leffler }; 103404fb2745SSam Leffler 103504fb2745SSam Leffler /* 103604fb2745SSam Leffler * pcap_config_hook() is an extension to application's config 103704fb2745SSam Leffler * handling. Uses Watt-32's config-table function. 103804fb2745SSam Leffler */ 103904fb2745SSam Leffler int pcap_config_hook (const char *name, const char *value) 104004fb2745SSam Leffler { 104104fb2745SSam Leffler return parse_config_table (debug_tab, NULL, name, value); 104204fb2745SSam Leffler } 104304fb2745SSam Leffler 104404fb2745SSam Leffler /* 104504fb2745SSam Leffler * Linked list of supported devices 104604fb2745SSam Leffler */ 104704fb2745SSam Leffler struct device *active_dev = NULL; /* the device we have opened */ 104804fb2745SSam Leffler struct device *probed_dev = NULL; /* the device we have probed */ 104904fb2745SSam Leffler const struct device *dev_base = &pkt_dev; /* list of network devices */ 105004fb2745SSam Leffler 105104fb2745SSam Leffler /* 105204fb2745SSam Leffler * PKTDRVR device functions 105304fb2745SSam Leffler */ 105404fb2745SSam Leffler int pcap_pkt_debug = -1; 105504fb2745SSam Leffler 105604fb2745SSam Leffler static void pkt_close (struct device *dev) 105704fb2745SSam Leffler { 105804fb2745SSam Leffler BOOL okay = PktExitDriver(); 105904fb2745SSam Leffler 106004fb2745SSam Leffler if (pcap_pkt_debug > 1) 106104fb2745SSam Leffler fprintf (stderr, "pkt_close(): %d\n", okay); 106204fb2745SSam Leffler 106304fb2745SSam Leffler if (dev->priv) 106404fb2745SSam Leffler free (dev->priv); 106504fb2745SSam Leffler dev->priv = NULL; 106604fb2745SSam Leffler } 106704fb2745SSam Leffler 106804fb2745SSam Leffler static int pkt_open (struct device *dev) 106904fb2745SSam Leffler { 107004fb2745SSam Leffler PKT_RX_MODE mode; 107104fb2745SSam Leffler 107204fb2745SSam Leffler if (dev->flags & IFF_PROMISC) 107304fb2745SSam Leffler mode = PDRX_ALL_PACKETS; 107404fb2745SSam Leffler else mode = PDRX_BROADCAST; 107504fb2745SSam Leffler 107604fb2745SSam Leffler if (!PktInitDriver(mode)) 107704fb2745SSam Leffler return (0); 107804fb2745SSam Leffler 107904fb2745SSam Leffler PktResetStatistics (pktInfo.handle); 108004fb2745SSam Leffler PktQueueBusy (FALSE); 108104fb2745SSam Leffler return (1); 108204fb2745SSam Leffler } 108304fb2745SSam Leffler 108404fb2745SSam Leffler static int pkt_xmit (struct device *dev, const void *buf, int len) 108504fb2745SSam Leffler { 108604fb2745SSam Leffler struct net_device_stats *stats = (struct net_device_stats*) dev->priv; 108704fb2745SSam Leffler 108804fb2745SSam Leffler if (pcap_pkt_debug > 0) 108904fb2745SSam Leffler dbug_write ("pcap_xmit\n"); 109004fb2745SSam Leffler 109104fb2745SSam Leffler if (!PktTransmit(buf,len)) 109204fb2745SSam Leffler { 109304fb2745SSam Leffler stats->tx_errors++; 109404fb2745SSam Leffler return (0); 109504fb2745SSam Leffler } 109604fb2745SSam Leffler return (len); 109704fb2745SSam Leffler } 109804fb2745SSam Leffler 109904fb2745SSam Leffler static void *pkt_stats (struct device *dev) 110004fb2745SSam Leffler { 110104fb2745SSam Leffler struct net_device_stats *stats = (struct net_device_stats*) dev->priv; 110204fb2745SSam Leffler 110304fb2745SSam Leffler if (!stats || !PktSessStatistics(pktInfo.handle)) 110404fb2745SSam Leffler return (NULL); 110504fb2745SSam Leffler 110604fb2745SSam Leffler stats->rx_packets = pktStat.inPackets; 110704fb2745SSam Leffler stats->rx_errors = pktStat.lost; 110804fb2745SSam Leffler stats->rx_missed_errors = PktRxDropped(); 110904fb2745SSam Leffler return (stats); 111004fb2745SSam Leffler } 111104fb2745SSam Leffler 111204fb2745SSam Leffler static int pkt_probe (struct device *dev) 111304fb2745SSam Leffler { 111404fb2745SSam Leffler if (!PktSearchDriver()) 111504fb2745SSam Leffler return (0); 111604fb2745SSam Leffler 111704fb2745SSam Leffler dev->open = pkt_open; 111804fb2745SSam Leffler dev->xmit = pkt_xmit; 111904fb2745SSam Leffler dev->close = pkt_close; 112004fb2745SSam Leffler dev->get_stats = pkt_stats; 112104fb2745SSam Leffler dev->copy_rx_buf = PktReceive; /* farmem peek and copy routine */ 112204fb2745SSam Leffler dev->get_rx_buf = NULL; 112304fb2745SSam Leffler dev->peek_rx_buf = NULL; 112404fb2745SSam Leffler dev->release_rx_buf = NULL; 112504fb2745SSam Leffler dev->priv = calloc (sizeof(struct net_device_stats), 1); 112604fb2745SSam Leffler if (!dev->priv) 112704fb2745SSam Leffler return (0); 112804fb2745SSam Leffler return (1); 112904fb2745SSam Leffler } 113004fb2745SSam Leffler 113104fb2745SSam Leffler /* 113204fb2745SSam Leffler * NDIS device functions 113304fb2745SSam Leffler */ 113404fb2745SSam Leffler static void ndis_close (struct device *dev) 113504fb2745SSam Leffler { 113604fb2745SSam Leffler #ifdef USE_NDIS2 113704fb2745SSam Leffler NdisShutdown(); 113804fb2745SSam Leffler #endif 113904fb2745SSam Leffler ARGSUSED (dev); 114004fb2745SSam Leffler } 114104fb2745SSam Leffler 114204fb2745SSam Leffler static int ndis_open (struct device *dev) 114304fb2745SSam Leffler { 114404fb2745SSam Leffler int promis = (dev->flags & IFF_PROMISC); 114504fb2745SSam Leffler 114604fb2745SSam Leffler #ifdef USE_NDIS2 114704fb2745SSam Leffler if (!NdisInit(promis)) 114804fb2745SSam Leffler return (0); 114904fb2745SSam Leffler return (1); 115004fb2745SSam Leffler #else 115104fb2745SSam Leffler ARGSUSED (promis); 115204fb2745SSam Leffler return (0); 115304fb2745SSam Leffler #endif 115404fb2745SSam Leffler } 115504fb2745SSam Leffler 115604fb2745SSam Leffler static void *ndis_stats (struct device *dev) 115704fb2745SSam Leffler { 115804fb2745SSam Leffler static struct net_device_stats stats; 115904fb2745SSam Leffler 116004fb2745SSam Leffler /* to-do */ 116104fb2745SSam Leffler ARGSUSED (dev); 116204fb2745SSam Leffler return (&stats); 116304fb2745SSam Leffler } 116404fb2745SSam Leffler 116504fb2745SSam Leffler static int ndis_probe (struct device *dev) 116604fb2745SSam Leffler { 116704fb2745SSam Leffler #ifdef USE_NDIS2 116804fb2745SSam Leffler if (!NdisOpen()) 116904fb2745SSam Leffler return (0); 117004fb2745SSam Leffler #endif 117104fb2745SSam Leffler 117204fb2745SSam Leffler dev->open = ndis_open; 117304fb2745SSam Leffler dev->xmit = NULL; 117404fb2745SSam Leffler dev->close = ndis_close; 117504fb2745SSam Leffler dev->get_stats = ndis_stats; 117604fb2745SSam Leffler dev->copy_rx_buf = NULL; /* to-do */ 117704fb2745SSam Leffler dev->get_rx_buf = NULL; /* upcall is from rmode driver */ 117804fb2745SSam Leffler dev->peek_rx_buf = NULL; 117904fb2745SSam Leffler dev->release_rx_buf = NULL; 118004fb2745SSam Leffler return (0); 118104fb2745SSam Leffler } 118204fb2745SSam Leffler 118304fb2745SSam Leffler /* 118404fb2745SSam Leffler * Search & probe for supported 32-bit (pmode) pcap devices 118504fb2745SSam Leffler */ 118604fb2745SSam Leffler #if defined(USE_32BIT_DRIVERS) 118704fb2745SSam Leffler 118804fb2745SSam Leffler struct device el2_dev LOCKED_VAR = { 118904fb2745SSam Leffler "3c503", 119004fb2745SSam Leffler "EtherLink II", 119104fb2745SSam Leffler 0, 119204fb2745SSam Leffler 0,0,0,0,0,0, 119304fb2745SSam Leffler NULL, 119404fb2745SSam Leffler el2_probe 119504fb2745SSam Leffler }; 119604fb2745SSam Leffler 119704fb2745SSam Leffler struct device el3_dev LOCKED_VAR = { 119804fb2745SSam Leffler "3c509", 119904fb2745SSam Leffler "EtherLink III", 120004fb2745SSam Leffler 0, 120104fb2745SSam Leffler 0,0,0,0,0,0, 120204fb2745SSam Leffler &el2_dev, 120304fb2745SSam Leffler el3_probe 120404fb2745SSam Leffler }; 120504fb2745SSam Leffler 120604fb2745SSam Leffler struct device tc515_dev LOCKED_VAR = { 120704fb2745SSam Leffler "3c515", 120804fb2745SSam Leffler "EtherLink PCI", 120904fb2745SSam Leffler 0, 121004fb2745SSam Leffler 0,0,0,0,0,0, 121104fb2745SSam Leffler &el3_dev, 121204fb2745SSam Leffler tc515_probe 121304fb2745SSam Leffler }; 121404fb2745SSam Leffler 121504fb2745SSam Leffler struct device tc59_dev LOCKED_VAR = { 121604fb2745SSam Leffler "3c59x", 121704fb2745SSam Leffler "EtherLink PCI", 121804fb2745SSam Leffler 0, 121904fb2745SSam Leffler 0,0,0,0,0,0, 122004fb2745SSam Leffler &tc515_dev, 122104fb2745SSam Leffler tc59x_probe 122204fb2745SSam Leffler }; 122304fb2745SSam Leffler 122404fb2745SSam Leffler struct device tc90xbc_dev LOCKED_VAR = { 122504fb2745SSam Leffler "3c90x", 122604fb2745SSam Leffler "EtherLink 90X", 122704fb2745SSam Leffler 0, 122804fb2745SSam Leffler 0,0,0,0,0,0, 122904fb2745SSam Leffler &tc59_dev, 123004fb2745SSam Leffler tc90xbc_probe 123104fb2745SSam Leffler }; 123204fb2745SSam Leffler 123304fb2745SSam Leffler struct device wd_dev LOCKED_VAR = { 123404fb2745SSam Leffler "wd", 123504fb2745SSam Leffler "Westen Digital", 123604fb2745SSam Leffler 0, 123704fb2745SSam Leffler 0,0,0,0,0,0, 123804fb2745SSam Leffler &tc90xbc_dev, 123904fb2745SSam Leffler wd_probe 124004fb2745SSam Leffler }; 124104fb2745SSam Leffler 124204fb2745SSam Leffler struct device ne_dev LOCKED_VAR = { 124304fb2745SSam Leffler "ne", 124404fb2745SSam Leffler "NEx000", 124504fb2745SSam Leffler 0, 124604fb2745SSam Leffler 0,0,0,0,0,0, 124704fb2745SSam Leffler &wd_dev, 124804fb2745SSam Leffler ne_probe 124904fb2745SSam Leffler }; 125004fb2745SSam Leffler 125104fb2745SSam Leffler struct device acct_dev LOCKED_VAR = { 125204fb2745SSam Leffler "acct", 125304fb2745SSam Leffler "Accton EtherPocket", 125404fb2745SSam Leffler 0, 125504fb2745SSam Leffler 0,0,0,0,0,0, 125604fb2745SSam Leffler &ne_dev, 125704fb2745SSam Leffler ethpk_probe 125804fb2745SSam Leffler }; 125904fb2745SSam Leffler 126004fb2745SSam Leffler struct device cs89_dev LOCKED_VAR = { 126104fb2745SSam Leffler "cs89", 126204fb2745SSam Leffler "Crystal Semiconductor", 126304fb2745SSam Leffler 0, 126404fb2745SSam Leffler 0,0,0,0,0,0, 126504fb2745SSam Leffler &acct_dev, 126604fb2745SSam Leffler cs89x0_probe 126704fb2745SSam Leffler }; 126804fb2745SSam Leffler 126904fb2745SSam Leffler struct device rtl8139_dev LOCKED_VAR = { 127004fb2745SSam Leffler "rtl8139", 127104fb2745SSam Leffler "RealTek PCI", 127204fb2745SSam Leffler 0, 127304fb2745SSam Leffler 0,0,0,0,0,0, 127404fb2745SSam Leffler &cs89_dev, 127504fb2745SSam Leffler rtl8139_probe /* dev->probe routine */ 127604fb2745SSam Leffler }; 127704fb2745SSam Leffler 127804fb2745SSam Leffler /* 127904fb2745SSam Leffler * Dequeue routine is called by polling. 128004fb2745SSam Leffler * NOTE: the queue-element is not copied, only a pointer is 128104fb2745SSam Leffler * returned at '*buf' 128204fb2745SSam Leffler */ 128304fb2745SSam Leffler int peek_rxbuf (BYTE **buf) 128404fb2745SSam Leffler { 128504fb2745SSam Leffler struct rx_elem *tail, *head; 128604fb2745SSam Leffler 128704fb2745SSam Leffler PCAP_ASSERT (pktq_check (&active_dev->queue)); 128804fb2745SSam Leffler 128904fb2745SSam Leffler DISABLE(); 129004fb2745SSam Leffler tail = pktq_out_elem (&active_dev->queue); 129104fb2745SSam Leffler head = pktq_in_elem (&active_dev->queue); 129204fb2745SSam Leffler ENABLE(); 129304fb2745SSam Leffler 129404fb2745SSam Leffler if (head != tail) 129504fb2745SSam Leffler { 129604fb2745SSam Leffler PCAP_ASSERT (tail->size < active_dev->queue.elem_size-4-2); 129704fb2745SSam Leffler 129804fb2745SSam Leffler *buf = &tail->data[0]; 129904fb2745SSam Leffler return (tail->size); 130004fb2745SSam Leffler } 130104fb2745SSam Leffler *buf = NULL; 130204fb2745SSam Leffler return (0); 130304fb2745SSam Leffler } 130404fb2745SSam Leffler 130504fb2745SSam Leffler /* 130604fb2745SSam Leffler * Release buffer we peeked at above. 130704fb2745SSam Leffler */ 130804fb2745SSam Leffler int release_rxbuf (BYTE *buf) 130904fb2745SSam Leffler { 131004fb2745SSam Leffler #ifndef NDEBUG 131104fb2745SSam Leffler struct rx_elem *tail = pktq_out_elem (&active_dev->queue); 131204fb2745SSam Leffler 131304fb2745SSam Leffler PCAP_ASSERT (&tail->data[0] == buf); 131404fb2745SSam Leffler #else 131504fb2745SSam Leffler ARGSUSED (buf); 131604fb2745SSam Leffler #endif 131704fb2745SSam Leffler pktq_inc_out (&active_dev->queue); 131804fb2745SSam Leffler return (1); 131904fb2745SSam Leffler } 132004fb2745SSam Leffler 132104fb2745SSam Leffler /* 132204fb2745SSam Leffler * get_rxbuf() routine (in locked code) is called from IRQ handler 132304fb2745SSam Leffler * to request a buffer. Interrupts are disabled and we have a 32kB stack. 132404fb2745SSam Leffler */ 132504fb2745SSam Leffler BYTE *get_rxbuf (int len) 132604fb2745SSam Leffler { 132704fb2745SSam Leffler int idx; 132804fb2745SSam Leffler 132904fb2745SSam Leffler if (len < ETH_MIN || len > ETH_MAX) 133004fb2745SSam Leffler return (NULL); 133104fb2745SSam Leffler 133204fb2745SSam Leffler idx = pktq_in_index (&active_dev->queue); 133304fb2745SSam Leffler 133404fb2745SSam Leffler #ifdef DEBUG 133504fb2745SSam Leffler { 133604fb2745SSam Leffler static int fan_idx LOCKED_VAR = 0; 133704fb2745SSam Leffler writew ("-\\|/"[fan_idx++] | (15 << 8), /* white on black colour */ 133804fb2745SSam Leffler 0xB8000 + 2*79); /* upper-right corner, 80-col colour screen */ 133904fb2745SSam Leffler fan_idx &= 3; 134004fb2745SSam Leffler } 134104fb2745SSam Leffler /* writew (idx + '0' + 0x0F00, 0xB8000 + 2*78); */ 134204fb2745SSam Leffler #endif 134304fb2745SSam Leffler 134404fb2745SSam Leffler if (idx != active_dev->queue.out_index) 134504fb2745SSam Leffler { 134604fb2745SSam Leffler struct rx_elem *head = pktq_in_elem (&active_dev->queue); 134704fb2745SSam Leffler 134804fb2745SSam Leffler head->size = len; 134904fb2745SSam Leffler active_dev->queue.in_index = idx; 135004fb2745SSam Leffler return (&head->data[0]); 135104fb2745SSam Leffler } 135204fb2745SSam Leffler 135304fb2745SSam Leffler /* !!to-do: drop 25% of the oldest element 135404fb2745SSam Leffler */ 135504fb2745SSam Leffler pktq_clear (&active_dev->queue); 135604fb2745SSam Leffler return (NULL); 135704fb2745SSam Leffler } 135804fb2745SSam Leffler 135904fb2745SSam Leffler /* 136004fb2745SSam Leffler * Simple ring-buffer queue handler for reception of packets 136104fb2745SSam Leffler * from network driver. 136204fb2745SSam Leffler */ 136304fb2745SSam Leffler #define PKTQ_MARKER 0xDEADBEEF 136404fb2745SSam Leffler 136504fb2745SSam Leffler static int pktq_check (struct rx_ringbuf *q) 136604fb2745SSam Leffler { 136704fb2745SSam Leffler #ifndef NDEBUG 136804fb2745SSam Leffler int i; 136904fb2745SSam Leffler char *buf; 137004fb2745SSam Leffler #endif 137104fb2745SSam Leffler 137204fb2745SSam Leffler if (!q || !q->num_elem || !q->buf_start) 137304fb2745SSam Leffler return (0); 137404fb2745SSam Leffler 137504fb2745SSam Leffler #ifndef NDEBUG 137604fb2745SSam Leffler buf = q->buf_start; 137704fb2745SSam Leffler 137804fb2745SSam Leffler for (i = 0; i < q->num_elem; i++) 137904fb2745SSam Leffler { 138004fb2745SSam Leffler buf += q->elem_size; 138104fb2745SSam Leffler if (*(DWORD*)(buf - sizeof(DWORD)) != PKTQ_MARKER) 138204fb2745SSam Leffler return (0); 138304fb2745SSam Leffler } 138404fb2745SSam Leffler #endif 138504fb2745SSam Leffler return (1); 138604fb2745SSam Leffler } 138704fb2745SSam Leffler 138804fb2745SSam Leffler static int pktq_init (struct rx_ringbuf *q, int size, int num, char *pool) 138904fb2745SSam Leffler { 139004fb2745SSam Leffler int i; 139104fb2745SSam Leffler 139204fb2745SSam Leffler q->elem_size = size; 139304fb2745SSam Leffler q->num_elem = num; 139404fb2745SSam Leffler q->buf_start = pool; 139504fb2745SSam Leffler q->in_index = 0; 139604fb2745SSam Leffler q->out_index = 0; 139704fb2745SSam Leffler 139804fb2745SSam Leffler PCAP_ASSERT (size >= sizeof(struct rx_elem) + sizeof(DWORD)); 139904fb2745SSam Leffler PCAP_ASSERT (num); 140004fb2745SSam Leffler PCAP_ASSERT (pool); 140104fb2745SSam Leffler 140204fb2745SSam Leffler for (i = 0; i < num; i++) 140304fb2745SSam Leffler { 140404fb2745SSam Leffler #if 0 140504fb2745SSam Leffler struct rx_elem *elem = (struct rx_elem*) pool; 140604fb2745SSam Leffler 140704fb2745SSam Leffler /* assert dword aligned elements 140804fb2745SSam Leffler */ 140904fb2745SSam Leffler PCAP_ASSERT (((unsigned)(&elem->data[0]) & 3) == 0); 141004fb2745SSam Leffler #endif 141104fb2745SSam Leffler pool += size; 141204fb2745SSam Leffler *(DWORD*) (pool - sizeof(DWORD)) = PKTQ_MARKER; 141304fb2745SSam Leffler } 141404fb2745SSam Leffler return (1); 141504fb2745SSam Leffler } 141604fb2745SSam Leffler 141704fb2745SSam Leffler /* 141804fb2745SSam Leffler * Increment the queue 'out_index' (tail). 141904fb2745SSam Leffler * Check for wraps. 142004fb2745SSam Leffler */ 142104fb2745SSam Leffler static int pktq_inc_out (struct rx_ringbuf *q) 142204fb2745SSam Leffler { 142304fb2745SSam Leffler q->out_index++; 142404fb2745SSam Leffler if (q->out_index >= q->num_elem) 142504fb2745SSam Leffler q->out_index = 0; 142604fb2745SSam Leffler return (q->out_index); 142704fb2745SSam Leffler } 142804fb2745SSam Leffler 142904fb2745SSam Leffler /* 143004fb2745SSam Leffler * Return the queue's next 'in_index' (head). 143104fb2745SSam Leffler * Check for wraps. 143204fb2745SSam Leffler */ 143304fb2745SSam Leffler static int pktq_in_index (struct rx_ringbuf *q) 143404fb2745SSam Leffler { 143504fb2745SSam Leffler volatile int index = q->in_index + 1; 143604fb2745SSam Leffler 143704fb2745SSam Leffler if (index >= q->num_elem) 143804fb2745SSam Leffler index = 0; 143904fb2745SSam Leffler return (index); 144004fb2745SSam Leffler } 144104fb2745SSam Leffler 144204fb2745SSam Leffler /* 144304fb2745SSam Leffler * Return the queue's head-buffer. 144404fb2745SSam Leffler */ 144504fb2745SSam Leffler static struct rx_elem *pktq_in_elem (struct rx_ringbuf *q) 144604fb2745SSam Leffler { 144704fb2745SSam Leffler return (struct rx_elem*) (q->buf_start + (q->elem_size * q->in_index)); 144804fb2745SSam Leffler } 144904fb2745SSam Leffler 145004fb2745SSam Leffler /* 145104fb2745SSam Leffler * Return the queue's tail-buffer. 145204fb2745SSam Leffler */ 145304fb2745SSam Leffler static struct rx_elem *pktq_out_elem (struct rx_ringbuf *q) 145404fb2745SSam Leffler { 145504fb2745SSam Leffler return (struct rx_elem*) (q->buf_start + (q->elem_size * q->out_index)); 145604fb2745SSam Leffler } 145704fb2745SSam Leffler 145804fb2745SSam Leffler /* 145904fb2745SSam Leffler * Clear the queue ring-buffer by setting head=tail. 146004fb2745SSam Leffler */ 146104fb2745SSam Leffler static void pktq_clear (struct rx_ringbuf *q) 146204fb2745SSam Leffler { 146304fb2745SSam Leffler q->in_index = q->out_index; 146404fb2745SSam Leffler } 146504fb2745SSam Leffler 146604fb2745SSam Leffler /* 146704fb2745SSam Leffler * Symbols that must be linkable for "gcc -O0" 146804fb2745SSam Leffler */ 146904fb2745SSam Leffler #undef __IOPORT_H 147004fb2745SSam Leffler #undef __DMA_H 147104fb2745SSam Leffler 147204fb2745SSam Leffler #define extern 147304fb2745SSam Leffler #define __inline__ 147404fb2745SSam Leffler 147504fb2745SSam Leffler #include "msdos/pm_drvr/ioport.h" 147604fb2745SSam Leffler #include "msdos/pm_drvr/dma.h" 147704fb2745SSam Leffler 147804fb2745SSam Leffler #endif /* USE_32BIT_DRIVERS */ 147904fb2745SSam Leffler 1480