• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  This file is part of DOS-libpcap
3  *  Ported to DOS/DOSX by G. Vanem <gvanem@yahoo.no>
4  *
5  *  pcap-dos.c: Interface to PKTDRVR, NDIS2 and 32-bit pmode
6  *              network drivers.
7  */
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <signal.h>
13 #include <float.h>
14 #include <fcntl.h>
15 #include <io.h>
16 
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"
35 #endif
36 
37 #include "pcap.h"
38 #include "pcap-dos.h"
39 #include "pcap-int.h"
40 #include "msdos/pktdrvr.h"
41 
42 #ifdef USE_NDIS2
43 #include "msdos/ndis2.h"
44 #endif
45 
46 #include <arpa/inet.h>
47 #include <net/if.h>
48 #include <net/if_arp.h>
49 #include <net/if_ether.h>
50 #include <net/if_packe.h>
51 #include <tcp.h>
52 
53 #if defined(USE_32BIT_DRIVERS)
54   #define FLUSHK()       do { _printk_safe = 1; _printk_flush(); } while (0)
55   #define NDIS_NEXT_DEV  &rtl8139_dev
56 
57   static char *rx_pool = NULL;
58   static void init_32bit (void);
59 
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;
65 
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);
68 
69 #else
70   #define FLUSHK()      ((void)0)
71   #define NDIS_NEXT_DEV  NULL
72 #endif
73 
74 /*
75  * Internal variables/functions in Watt-32
76  */
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)();
85 
86 extern void dbug_write (const char *);  /* Watt-32 lib, pcdbug.c */
87 extern int  pkt_get_mtu (void);
88 
89 static int ref_count = 0;
90 
91 static u_long mac_count    = 0;
92 static u_long filter_count = 0;
93 
94 static volatile BOOL exc_occured = 0;
95 
96 static struct device *handle_to_device [20];
97 
98 static int  pcap_activate_dos (pcap_t *p);
99 static int  pcap_read_dos (pcap_t *p, int cnt, pcap_handler callback,
100                            u_char *data);
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);
105 
106 static int  ndis_probe (struct device *dev);
107 static int  pkt_probe  (struct device *dev);
108 
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);
112 
113 static void watt32_recv_hook (u_char *dummy, const struct pcap_pkthdr *pcap,
114                               const u_char *buf);
115 
116 /*
117  * These are the device we always support
118  */
119 static struct device ndis_dev = {
120               "ndis",
121               "NDIS2 LanManager",
122               0,
123               0,0,0,0,0,0,
124               NDIS_NEXT_DEV,  /* NULL or a 32-bit device */
125               ndis_probe
126             };
127 
128 static struct device pkt_dev = {
129               "pkt",
130               "Packet-Driver",
131               0,
132               0,0,0,0,0,0,
133               &ndis_dev,
134               pkt_probe
135             };
136 
get_device(int fd)137 static struct device *get_device (int fd)
138 {
139   if (fd <= 0 || fd >= sizeof(handle_to_device)/sizeof(handle_to_device[0]))
140      return (NULL);
141   return handle_to_device [fd-1];
142 }
143 
144 /*
145  * Private data for capturing on MS-DOS.
146  */
147 struct pcap_dos {
148 	void (*wait_proc)(void); /* call proc while waiting */
149 	struct pcap_stat stat;
150 };
151 
pcap_create_interface(const char * device _U_,char * ebuf)152 pcap_t *pcap_create_interface (const char *device _U_, char *ebuf)
153 {
154 	pcap_t *p;
155 
156 	p = PCAP_CREATE_COMMON(ebuf, struct pcap_dos);
157 	if (p == NULL)
158 		return (NULL);
159 
160 	p->activate_op = pcap_activate_dos;
161 	return (p);
162 }
163 
164 /*
165  * Open MAC-driver with name 'device_name' for live capture of
166  * network packets.
167  */
pcap_activate_dos(pcap_t * pcap)168 static int pcap_activate_dos (pcap_t *pcap)
169 {
170   if (pcap->opt.rfmon) {
171     /*
172      * No monitor mode on DOS.
173      */
174     return (PCAP_ERROR_RFMON_NOTSUP);
175   }
176 
177   /*
178    * Turn a negative snapshot value (invalid), a snapshot value of
179    * 0 (unspecified), or a value bigger than the normal maximum
180    * value, into the maximum allowed value.
181    *
182    * If some application really *needs* a bigger snapshot
183    * length, we should just increase MAXIMUM_SNAPLEN.
184    */
185   if (pcap->snapshot <= 0 || pcap->snapshot > MAXIMUM_SNAPLEN)
186     pcap->snapshot = MAXIMUM_SNAPLEN;
187 
188   if (pcap->snapshot < ETH_MIN+8)
189       pcap->snapshot = ETH_MIN+8;
190 
191   if (pcap->snapshot > ETH_MAX)   /* silently accept and truncate large MTUs */
192       pcap->snapshot = ETH_MAX;
193 
194   pcap->linktype          = DLT_EN10MB;  /* !! */
195   pcap->cleanup_op        = pcap_cleanup_dos;
196   pcap->read_op           = pcap_read_dos;
197   pcap->stats_op          = pcap_stats_dos;
198   pcap->inject_op         = pcap_sendpacket_dos;
199   pcap->setfilter_op      = pcap_setfilter_dos;
200   pcap->setdirection_op   = NULL;  /* Not implemented.*/
201   pcap->fd                = ++ref_count;
202 
203   pcap->bufsize = ETH_MAX+100;     /* add some margin */
204   pcap->buffer = calloc (pcap->bufsize, 1);
205 
206   if (pcap->fd == 1)  /* first time we're called */
207   {
208     if (!init_watt32(pcap, pcap->opt.device, pcap->errbuf) ||
209         !first_init(pcap->opt.device, pcap->errbuf, pcap->opt.promisc))
210     {
211       /* XXX - free pcap->buffer? */
212       return (PCAP_ERROR);
213     }
214     atexit (close_driver);
215   }
216   else if (stricmp(active_dev->name,pcap->opt.device))
217   {
218     snprintf (pcap->errbuf, PCAP_ERRBUF_SIZE,
219                    "Cannot use different devices simultaneously "
220                    "(`%s' vs. `%s')", active_dev->name, pcap->opt.device);
221     /* XXX - free pcap->buffer? */
222     return (PCAP_ERROR);
223   }
224   handle_to_device [pcap->fd-1] = active_dev;
225   return (0);
226 }
227 
228 /*
229  * Poll the receiver queue and call the pcap callback-handler
230  * with the packet.
231  */
232 static int
pcap_read_one(pcap_t * p,pcap_handler callback,u_char * data)233 pcap_read_one (pcap_t *p, pcap_handler callback, u_char *data)
234 {
235   struct pcap_dos *pd = p->priv;
236   struct pcap_pkthdr pcap;
237   struct timeval     now, expiry = { 0,0 };
238   int    rx_len = 0;
239 
240   if (p->opt.timeout > 0)
241   {
242     gettimeofday2 (&now, NULL);
243     expiry.tv_usec = now.tv_usec + 1000UL * p->opt.timeout;
244     expiry.tv_sec  = now.tv_sec;
245     while (expiry.tv_usec >= 1000000L)
246     {
247       expiry.tv_usec -= 1000000L;
248       expiry.tv_sec++;
249     }
250   }
251 
252   while (!exc_occured)
253   {
254     volatile struct device *dev; /* might be reset by sig_handler */
255 
256     dev = get_device (p->fd);
257     if (!dev)
258        break;
259 
260     PCAP_ASSERT (dev->copy_rx_buf || dev->peek_rx_buf);
261     FLUSHK();
262 
263     /* If driver has a zero-copy receive facility, peek at the queue,
264      * filter it, do the callback and release the buffer.
265      */
266     if (dev->peek_rx_buf)
267     {
268       PCAP_ASSERT (dev->release_rx_buf);
269       rx_len = (*dev->peek_rx_buf) (&p->buffer);
270     }
271     else
272     {
273       rx_len = (*dev->copy_rx_buf) (p->buffer, p->snapshot);
274     }
275 
276     if (rx_len > 0)  /* got a packet */
277     {
278       mac_count++;
279 
280       FLUSHK();
281 
282       pcap.caplen = min (rx_len, p->snapshot);
283       pcap.len    = rx_len;
284 
285       if (callback &&
286           (!p->fcode.bf_insns || pcap_filter(p->fcode.bf_insns, p->buffer, pcap.len, pcap.caplen)))
287       {
288         filter_count++;
289 
290         /* Fix-me!! Should be time of arrival. Not time of
291          * capture.
292          */
293         gettimeofday2 (&pcap.ts, NULL);
294         (*callback) (data, &pcap, p->buffer);
295       }
296 
297       if (dev->release_rx_buf)
298         (*dev->release_rx_buf) (p->buffer);
299 
300       if (pcap_pkt_debug > 0)
301       {
302         if (callback == watt32_recv_hook)
303              dbug_write ("pcap_recv_hook\n");
304         else dbug_write ("pcap_read_op\n");
305       }
306       FLUSHK();
307       return (1);
308     }
309 
310     /* Has "pcap_breakloop()" been called?
311      */
312     if (p->break_loop) {
313       /*
314        * Yes - clear the flag that indicates that it
315        * has, and return -2 to indicate that we were
316        * told to break out of the loop.
317        */
318       p->break_loop = 0;
319       return (-2);
320     }
321 
322     /* If not to wait for a packet or pcap_cleanup_dos() called from
323      * e.g. SIGINT handler, exit loop now.
324      */
325     if (p->opt.timeout <= 0 || (volatile int)p->fd <= 0)
326        break;
327 
328     gettimeofday2 (&now, NULL);
329 
330     if (timercmp(&now, &expiry, >))
331        break;
332 
333 #ifndef DJGPP
334     kbhit();    /* a real CPU hog */
335 #endif
336 
337     if (pd->wait_proc)
338       (*pd->wait_proc)();     /* call yield func */
339   }
340 
341   if (rx_len < 0)            /* receive error */
342   {
343     pd->stat.ps_drop++;
344 #ifdef USE_32BIT_DRIVERS
345     if (pcap_pkt_debug > 1)
346        printk ("pkt-err %s\n", pktInfo.error);
347 #endif
348     return (-1);
349   }
350   return (0);
351 }
352 
353 static int
pcap_read_dos(pcap_t * p,int cnt,pcap_handler callback,u_char * data)354 pcap_read_dos (pcap_t *p, int cnt, pcap_handler callback, u_char *data)
355 {
356   int rc, num = 0;
357 
358   while (num <= cnt || PACKET_COUNT_IS_UNLIMITED(cnt))
359   {
360     if (p->fd <= 0)
361        return (-1);
362     rc = pcap_read_one (p, callback, data);
363     if (rc > 0)
364        num++;
365     if (rc < 0)
366        break;
367     _w32_os_yield();  /* allow SIGINT generation, yield to Win95/NT */
368   }
369   return (num);
370 }
371 
372 /*
373  * Return network statistics
374  */
pcap_stats_dos(pcap_t * p,struct pcap_stat * ps)375 static int pcap_stats_dos (pcap_t *p, struct pcap_stat *ps)
376 {
377   struct net_device_stats *stats;
378   struct pcap_dos         *pd;
379   struct device           *dev = p ? get_device(p->fd) : NULL;
380 
381   if (!dev)
382   {
383     strcpy (p->errbuf, "illegal pcap handle");
384     return (-1);
385   }
386 
387   if (!dev->get_stats || (stats = (*dev->get_stats)(dev)) == NULL)
388   {
389     strcpy (p->errbuf, "device statistics not available");
390     return (-1);
391   }
392 
393   FLUSHK();
394 
395   pd = p->priv;
396   pd->stat.ps_recv   = stats->rx_packets;
397   pd->stat.ps_drop  += stats->rx_missed_errors;
398   pd->stat.ps_ifdrop = stats->rx_dropped +  /* queue full */
399                          stats->rx_errors;    /* HW errors */
400   if (ps)
401      *ps = pd->stat;
402 
403   return (0);
404 }
405 
406 /*
407  * Return detailed network/device statistics.
408  * May be called after 'dev->close' is called.
409  */
pcap_stats_ex(pcap_t * p,struct pcap_stat_ex * se)410 int pcap_stats_ex (pcap_t *p, struct pcap_stat_ex *se)
411 {
412   struct device *dev = p ? get_device (p->fd) : NULL;
413 
414   if (!dev || !dev->get_stats)
415   {
416     pcap_strlcpy (p->errbuf, "detailed device statistics not available",
417              PCAP_ERRBUF_SIZE);
418     return (-1);
419   }
420 
421   if (!strnicmp(dev->name,"pkt",3))
422   {
423     pcap_strlcpy (p->errbuf, "pktdrvr doesn't have detailed statistics",
424              PCAP_ERRBUF_SIZE);
425     return (-1);
426   }
427   memcpy (se, (*dev->get_stats)(dev), sizeof(*se));
428   return (0);
429 }
430 
431 /*
432  * Simply store the filter-code for the pcap_read_dos() callback
433  * Some day the filter-code could be handed down to the active
434  * device (pkt_rx1.s or 32-bit device interrupt handler).
435  */
pcap_setfilter_dos(pcap_t * p,struct bpf_program * fp)436 static int pcap_setfilter_dos (pcap_t *p, struct bpf_program *fp)
437 {
438   if (!p)
439      return (-1);
440   p->fcode = *fp;
441   return (0);
442 }
443 
444 /*
445  * Return # of packets received in pcap_read_dos()
446  */
pcap_mac_packets(void)447 u_long pcap_mac_packets (void)
448 {
449   return (mac_count);
450 }
451 
452 /*
453  * Return # of packets passed through filter in pcap_read_dos()
454  */
pcap_filter_packets(void)455 u_long pcap_filter_packets (void)
456 {
457   return (filter_count);
458 }
459 
460 /*
461  * Close pcap device. Not called for offline captures.
462  */
pcap_cleanup_dos(pcap_t * p)463 static void pcap_cleanup_dos (pcap_t *p)
464 {
465   struct pcap_dos *pd;
466 
467   if (!exc_occured)
468   {
469     pd = p->priv;
470     if (pcap_stats(p,NULL) < 0)
471        pd->stat.ps_drop = 0;
472     if (!get_device(p->fd))
473        return;
474 
475     handle_to_device [p->fd-1] = NULL;
476     p->fd = 0;
477     if (ref_count > 0)
478         ref_count--;
479     if (ref_count > 0)
480        return;
481   }
482   close_driver();
483   /* XXX - call pcap_cleanup_live_common? */
484 }
485 
486 /*
487  * Return the name of the 1st network interface,
488  * or NULL if none can be found.
489  */
pcap_lookupdev(char * ebuf)490 char *pcap_lookupdev (char *ebuf)
491 {
492   struct device *dev;
493 
494 #ifdef USE_32BIT_DRIVERS
495   init_32bit();
496 #endif
497 
498   for (dev = (struct device*)dev_base; dev; dev = dev->next)
499   {
500     PCAP_ASSERT (dev->probe);
501 
502     if ((*dev->probe)(dev))
503     {
504       FLUSHK();
505       probed_dev = (struct device*) dev; /* remember last probed device */
506       return (char*) dev->name;
507     }
508   }
509 
510   if (ebuf)
511      strcpy (ebuf, "No driver found");
512   return (NULL);
513 }
514 
515 /*
516  * Gets localnet & netmask from Watt-32.
517  */
pcap_lookupnet(const char * device,bpf_u_int32 * localnet,bpf_u_int32 * netmask,char * errbuf)518 int pcap_lookupnet (const char *device, bpf_u_int32 *localnet,
519                     bpf_u_int32 *netmask, char *errbuf)
520 {
521   DWORD mask, net;
522 
523   if (!_watt_is_init)
524   {
525     strcpy (errbuf, "pcap_open_offline() or pcap_activate() must be "
526                     "called first");
527     return (-1);
528   }
529 
530   mask  = _w32_sin_mask;
531   net = my_ip_addr & mask;
532   if (net == 0)
533   {
534     if (IN_CLASSA(*netmask))
535        net = IN_CLASSA_NET;
536     else if (IN_CLASSB(*netmask))
537        net = IN_CLASSB_NET;
538     else if (IN_CLASSC(*netmask))
539        net = IN_CLASSC_NET;
540     else
541     {
542       snprintf (errbuf, PCAP_ERRBUF_SIZE, "inet class for 0x%lx unknown", mask);
543       return (-1);
544     }
545   }
546   *localnet = htonl (net);
547   *netmask = htonl (mask);
548 
549   ARGSUSED (device);
550   return (0);
551 }
552 
553 /*
554  * Get a list of all interfaces that are present and that we probe okay.
555  * Returns -1 on error, 0 otherwise.
556  * The list may be NULL epty if no interfaces were up and could be opened.
557  */
pcap_platform_finddevs(pcap_if_list_t * devlistp,char * errbuf)558 int pcap_platform_finddevs  (pcap_if_list_t *devlistp, char *errbuf)
559 {
560   struct device     *dev;
561   pcap_if_t *curdev;
562 #if 0   /* Pkt drivers should have no addresses */
563   struct sockaddr_in sa_ll_1, sa_ll_2;
564   struct sockaddr   *addr, *netmask, *broadaddr, *dstaddr;
565 #endif
566   int       ret = 0;
567   int       found = 0;
568 
569   for (dev = (struct device*)dev_base; dev; dev = dev->next)
570   {
571     PCAP_ASSERT (dev->probe);
572 
573     if (!(*dev->probe)(dev))
574        continue;
575 
576     PCAP_ASSERT (dev->close);  /* set by probe routine */
577     FLUSHK();
578     (*dev->close) (dev);
579 
580     /*
581      * XXX - find out whether it's up or running?  Does that apply here?
582      * Can we find out if anything's plugged into the adapter, if it's
583      * a wired device, and set PCAP_IF_CONNECTION_STATUS_CONNECTED
584      * or PCAP_IF_CONNECTION_STATUS_DISCONNECTED?
585      */
586     if ((curdev = add_dev(devlistp, dev->name, 0,
587                 dev->long_name, errbuf)) == NULL)
588     {
589       ret = -1;
590       break;
591     }
592     found = 1;
593 #if 0   /* Pkt drivers should have no addresses */
594     memset (&sa_ll_1, 0, sizeof(sa_ll_1));
595     memset (&sa_ll_2, 0, sizeof(sa_ll_2));
596     sa_ll_1.sin_family = AF_INET;
597     sa_ll_2.sin_family = AF_INET;
598 
599     addr      = (struct sockaddr*) &sa_ll_1;
600     netmask   = (struct sockaddr*) &sa_ll_1;
601     dstaddr   = (struct sockaddr*) &sa_ll_1;
602     broadaddr = (struct sockaddr*) &sa_ll_2;
603     memset (&sa_ll_2.sin_addr, 0xFF, sizeof(sa_ll_2.sin_addr));
604 
605     if (add_addr_to_dev(curdev, addr, sizeof(*addr),
606                         netmask, sizeof(*netmask),
607                         broadaddr, sizeof(*broadaddr),
608                         dstaddr, sizeof(*dstaddr), errbuf) < 0)
609     {
610       ret = -1;
611       break;
612     }
613 #endif
614   }
615 
616   if (ret == 0 && !found)
617      strcpy (errbuf, "No drivers found");
618 
619   return (ret);
620 }
621 
622 /*
623  * pcap_assert() is mainly used for debugging
624  */
pcap_assert(const char * what,const char * file,unsigned line)625 void pcap_assert (const char *what, const char *file, unsigned line)
626 {
627   FLUSHK();
628   fprintf (stderr, "%s (%u): Assertion \"%s\" failed\n",
629            file, line, what);
630   close_driver();
631   _exit (-1);
632 }
633 
634 /*
635  * For pcap_offline_read(): wait and yield between printing packets
636  * to simulate the pace packets where actually recorded.
637  */
pcap_set_wait(pcap_t * p,void (* yield)(void),int wait)638 void pcap_set_wait (pcap_t *p, void (*yield)(void), int wait)
639 {
640   if (p)
641   {
642     struct pcap_dos *pd = p->priv;
643 
644     pd->wait_proc  = yield;
645     p->opt.timeout = wait;
646   }
647 }
648 
649 /*
650  * Initialise a named network device.
651  */
652 static struct device *
open_driver(const char * dev_name,char * ebuf,int promisc)653 open_driver (const char *dev_name, char *ebuf, int promisc)
654 {
655   struct device *dev;
656 
657   for (dev = (struct device*)dev_base; dev; dev = dev->next)
658   {
659     PCAP_ASSERT (dev->name);
660 
661     if (strcmp (dev_name,dev->name))
662        continue;
663 
664     if (!probed_dev)   /* user didn't call pcap_lookupdev() first */
665     {
666       PCAP_ASSERT (dev->probe);
667 
668       if (!(*dev->probe)(dev))    /* call the xx_probe() function */
669       {
670         snprintf (ebuf, PCAP_ERRBUF_SIZE, "failed to detect device `%s'", dev_name);
671         return (NULL);
672       }
673       probed_dev = dev;  /* device is probed okay and may be used */
674     }
675     else if (dev != probed_dev)
676     {
677       goto not_probed;
678     }
679 
680     FLUSHK();
681 
682     /* Select what traffic to receive
683      */
684     if (promisc)
685          dev->flags |=  (IFF_ALLMULTI | IFF_PROMISC);
686     else dev->flags &= ~(IFF_ALLMULTI | IFF_PROMISC);
687 
688     PCAP_ASSERT (dev->open);
689 
690     if (!(*dev->open)(dev))
691     {
692       snprintf (ebuf, PCAP_ERRBUF_SIZE, "failed to activate device `%s'", dev_name);
693       if (pktInfo.error && !strncmp(dev->name,"pkt",3))
694       {
695         strcat (ebuf, ": ");
696         strcat (ebuf, pktInfo.error);
697       }
698       return (NULL);
699     }
700 
701     /* Some devices need this to operate in promiscuous mode
702      */
703     if (promisc && dev->set_multicast_list)
704        (*dev->set_multicast_list) (dev);
705 
706     active_dev = dev;   /* remember our active device */
707     break;
708   }
709 
710   /* 'dev_name' not matched in 'dev_base' list.
711    */
712   if (!dev)
713   {
714     snprintf (ebuf, PCAP_ERRBUF_SIZE, "device `%s' not supported", dev_name);
715     return (NULL);
716   }
717 
718 not_probed:
719   if (!probed_dev)
720   {
721     snprintf (ebuf, PCAP_ERRBUF_SIZE, "device `%s' not probed", dev_name);
722     return (NULL);
723   }
724   return (dev);
725 }
726 
727 /*
728  * Deinitialise MAC driver.
729  * Set receive mode back to default mode.
730  */
close_driver(void)731 static void close_driver (void)
732 {
733   /* !!todo: loop over all 'handle_to_device[]' ? */
734   struct device *dev = active_dev;
735 
736   if (dev && dev->close)
737   {
738     (*dev->close) (dev);
739     FLUSHK();
740   }
741 
742   active_dev = NULL;
743 
744 #ifdef USE_32BIT_DRIVERS
745   if (rx_pool)
746   {
747     k_free (rx_pool);
748     rx_pool = NULL;
749   }
750   if (dev)
751      pcibios_exit();
752 #endif
753 }
754 
755 
756 #ifdef __DJGPP__
setup_signals(void (* handler)(int))757 static void setup_signals (void (*handler)(int))
758 {
759   signal (SIGSEGV,handler);
760   signal (SIGILL, handler);
761   signal (SIGFPE, handler);
762 }
763 
exc_handler(int sig)764 static void exc_handler (int sig)
765 {
766 #ifdef USE_32BIT_DRIVERS
767   if (active_dev->irq > 0)    /* excludes IRQ 0 */
768   {
769     disable_irq (active_dev->irq);
770     irq_eoi_cmd (active_dev->irq);
771     _printk_safe = 1;
772   }
773 #endif
774 
775   switch (sig)
776   {
777     case SIGSEGV:
778          fputs ("Catching SIGSEGV.\n", stderr);
779          break;
780     case SIGILL:
781          fputs ("Catching SIGILL.\n", stderr);
782          break;
783     case SIGFPE:
784          _fpreset();
785          fputs ("Catching SIGFPE.\n", stderr);
786          break;
787     default:
788          fprintf (stderr, "Catching signal %d.\n", sig);
789   }
790   exc_occured = 1;
791   close_driver();
792 }
793 #endif  /* __DJGPP__ */
794 
795 
796 /*
797  * Open the pcap device for the first client calling pcap_activate()
798  */
first_init(const char * name,char * ebuf,int promisc)799 static int first_init (const char *name, char *ebuf, int promisc)
800 {
801   struct device *dev;
802 
803 #ifdef USE_32BIT_DRIVERS
804   rx_pool = k_calloc (RECEIVE_BUF_SIZE, RECEIVE_QUEUE_SIZE);
805   if (!rx_pool)
806   {
807     strcpy (ebuf, "Not enough memory (Rx pool)");
808     return (0);
809   }
810 #endif
811 
812 #ifdef __DJGPP__
813   setup_signals (exc_handler);
814 #endif
815 
816 #ifdef USE_32BIT_DRIVERS
817   init_32bit();
818 #endif
819 
820   dev = open_driver (name, ebuf, promisc);
821   if (!dev)
822   {
823 #ifdef USE_32BIT_DRIVERS
824     k_free (rx_pool);
825     rx_pool = NULL;
826 #endif
827 
828 #ifdef __DJGPP__
829     setup_signals (SIG_DFL);
830 #endif
831     return (0);
832   }
833 
834 #ifdef USE_32BIT_DRIVERS
835   /*
836    * If driver is NOT a 16-bit "pkt/ndis" driver (having a 'copy_rx_buf'
837    * set in it's probe handler), initialise near-memory ring-buffer for
838    * the 32-bit device.
839    */
840   if (dev->copy_rx_buf == NULL)
841   {
842     dev->get_rx_buf     = get_rxbuf;
843     dev->peek_rx_buf    = peek_rxbuf;
844     dev->release_rx_buf = release_rxbuf;
845     pktq_init (&dev->queue, RECEIVE_BUF_SIZE, RECEIVE_QUEUE_SIZE, rx_pool);
846   }
847 #endif
848   return (1);
849 }
850 
851 #ifdef USE_32BIT_DRIVERS
init_32bit(void)852 static void init_32bit (void)
853 {
854   static int init_pci = 0;
855 
856   if (!_printk_file)
857      _printk_init (64*1024, NULL); /* calls atexit(printk_exit) */
858 
859   if (!init_pci)
860      (void)pci_init();             /* init BIOS32+PCI interface */
861   init_pci = 1;
862 }
863 #endif
864 
865 
866 /*
867  * Hook functions for using Watt-32 together with pcap
868  */
869 static char rxbuf [ETH_MAX+100]; /* rx-buffer with some margin */
870 static WORD etype;
871 static pcap_t pcap_save;
872 
watt32_recv_hook(u_char * dummy,const struct pcap_pkthdr * pcap,const u_char * buf)873 static void watt32_recv_hook (u_char *dummy, const struct pcap_pkthdr *pcap,
874                               const u_char *buf)
875 {
876   /* Fix me: assumes Ethernet II only */
877   struct ether_header *ep = (struct ether_header*) buf;
878 
879   memcpy (rxbuf, buf, pcap->caplen);
880   etype = ep->ether_type;
881   ARGSUSED (dummy);
882 }
883 
884 #if (WATTCP_VER >= 0x0224)
885 /*
886  * This function is used by Watt-32 to poll for a packet.
887  * i.e. it's set to bypass _eth_arrived()
888  */
pcap_recv_hook(WORD * type)889 static void *pcap_recv_hook (WORD *type)
890 {
891   int len = pcap_read_dos (&pcap_save, 1, watt32_recv_hook, NULL);
892 
893   if (len < 0)
894      return (NULL);
895 
896   *type = etype;
897   return (void*) &rxbuf;
898 }
899 
900 /*
901  * This function is called by Watt-32 (via _eth_xmit_hook).
902  * If dbug_init() was called, we should trace packets sent.
903  */
pcap_xmit_hook(const void * buf,unsigned len)904 static int pcap_xmit_hook (const void *buf, unsigned len)
905 {
906   int rc = 0;
907 
908   if (pcap_pkt_debug > 0)
909      dbug_write ("pcap_xmit_hook: ");
910 
911   if (active_dev && active_dev->xmit)
912      if ((*active_dev->xmit) (active_dev, buf, len) > 0)
913         rc = len;
914 
915   if (pcap_pkt_debug > 0)
916      dbug_write (rc ? "ok\n" : "fail\n");
917   return (rc);
918 }
919 #endif
920 
pcap_sendpacket_dos(pcap_t * p,const void * buf,size_t len)921 static int pcap_sendpacket_dos (pcap_t *p, const void *buf, size_t len)
922 {
923   struct device *dev = p ? get_device(p->fd) : NULL;
924 
925   if (!dev || !dev->xmit)
926      return (-1);
927   return (*dev->xmit) (dev, buf, len);
928 }
929 
930 /*
931  * This function is called by Watt-32 in tcp_post_init().
932  * We should prevent Watt-32 from using BOOTP/DHCP/RARP etc.
933  */
934 static void (*prev_post_hook) (void);
935 
pcap_init_hook(void)936 static void pcap_init_hook (void)
937 {
938   _w32__bootp_on = _w32__dhcp_on = _w32__rarp_on = 0;
939   _w32__do_mask_req = 0;
940   _w32_dynamic_host = 0;
941   if (prev_post_hook)
942     (*prev_post_hook)();
943 }
944 
945 /*
946  * Suppress PRINT message from Watt-32's sock_init()
947  */
null_print(void)948 static void null_print (void) {}
949 
950 /*
951  * To use features of Watt-32 (netdb functions and socket etc.)
952  * we must call sock_init(). But we set various hooks to prevent
953  * using normal PKTDRVR functions in pcpkt.c. This should hopefully
954  * make Watt-32 and pcap co-operate.
955  */
init_watt32(struct pcap * pcap,const char * dev_name,char * err_buf)956 static int init_watt32 (struct pcap *pcap, const char *dev_name, char *err_buf)
957 {
958   char *env;
959   int   rc, MTU, has_ip_addr;
960   int   using_pktdrv = 1;
961 
962   /* If user called sock_init() first, we need to reinit in
963    * order to open debug/trace-file properly
964    */
965   if (_watt_is_init)
966      sock_exit();
967 
968   env = getenv ("PCAP_TRACE");
969   if (env && atoi(env) > 0 &&
970       pcap_pkt_debug < 0)   /* if not already set */
971   {
972     dbug_init();
973     pcap_pkt_debug = atoi (env);
974   }
975 
976   _watt_do_exit      = 0;    /* prevent sock_init() calling exit() */
977   prev_post_hook     = _w32_usr_post_init;
978   _w32_usr_post_init = pcap_init_hook;
979   _w32_print_hook    = null_print;
980 
981   if (dev_name && strncmp(dev_name,"pkt",3))
982      using_pktdrv = FALSE;
983 
984   rc = sock_init();
985   has_ip_addr = (rc != 8);  /* IP-address assignment failed */
986 
987   /* if pcap is using a 32-bit driver w/o a pktdrvr loaded, we
988    * just pretend Watt-32 is initialised okay.
989    *
990    * !! fix-me: The Watt-32 config isn't done if no pktdrvr
991    *            was found. In that case my_ip_addr + sin_mask
992    *            have default values. Should be taken from another
993    *            ini-file/environment in any case (ref. tcpdump.ini)
994    */
995   _watt_is_init = 1;
996 
997   if (!using_pktdrv || !has_ip_addr)  /* for now .... */
998   {
999     static const char myip[] = "192.168.0.1";
1000     static const char mask[] = "255.255.255.0";
1001 
1002     printf ("Just guessing, using IP %s and netmask %s\n", myip, mask);
1003     my_ip_addr    = aton (myip);
1004     _w32_sin_mask = aton (mask);
1005   }
1006   else if (rc && using_pktdrv)
1007   {
1008     snprintf (err_buf, PCAP_ERRBUF_SIZE, "sock_init() failed, code %d", rc);
1009     return (0);
1010   }
1011 
1012   /* Set recv-hook for peeking in _eth_arrived().
1013    */
1014 #if (WATTCP_VER >= 0x0224)
1015   _eth_recv_hook = pcap_recv_hook;
1016   _eth_xmit_hook = pcap_xmit_hook;
1017 #endif
1018 
1019   /* Free the pkt-drvr handle allocated in pkt_init().
1020    * The above hooks should thus use the handle reopened in open_driver()
1021    */
1022   if (using_pktdrv)
1023   {
1024     _eth_release();
1025 /*  _eth_is_init = 1; */  /* hack to get Rx/Tx-hooks in Watt-32 working */
1026   }
1027 
1028   memcpy (&pcap_save, pcap, sizeof(pcap_save));
1029   MTU = pkt_get_mtu();
1030   pcap_save.fcode.bf_insns = NULL;
1031   pcap_save.linktype       = _eth_get_hwtype (NULL, NULL);
1032   pcap_save.snapshot       = MTU > 0 ? MTU : ETH_MAX; /* assume 1514 */
1033 
1034   /* prevent use of resolve() and resolve_ip()
1035    */
1036   last_nameserver = 0;
1037   return (1);
1038 }
1039 
1040 int EISA_bus = 0;  /* Where is natural place for this? */
1041 
1042 /*
1043  * Application config hooks to set various driver parameters.
1044  */
1045 
1046 static const struct config_table debug_tab[] = {
1047             { "PKT.DEBUG",       ARG_ATOI,   &pcap_pkt_debug    },
1048             { "PKT.VECTOR",      ARG_ATOX_W, NULL               },
1049             { "NDIS.DEBUG",      ARG_ATOI,   NULL               },
1050 #ifdef USE_32BIT_DRIVERS
1051             { "3C503.DEBUG",     ARG_ATOI,   &ei_debug          },
1052             { "3C503.IO_BASE",   ARG_ATOX_W, &el2_dev.base_addr },
1053             { "3C503.MEMORY",    ARG_ATOX_W, &el2_dev.mem_start },
1054             { "3C503.IRQ",       ARG_ATOI,   &el2_dev.irq       },
1055             { "3C505.DEBUG",     ARG_ATOI,   NULL               },
1056             { "3C505.BASE",      ARG_ATOX_W, NULL               },
1057             { "3C507.DEBUG",     ARG_ATOI,   NULL               },
1058             { "3C509.DEBUG",     ARG_ATOI,   &el3_debug         },
1059             { "3C509.ILOOP",     ARG_ATOI,   &el3_max_loop      },
1060             { "3C529.DEBUG",     ARG_ATOI,   NULL               },
1061             { "3C575.DEBUG",     ARG_ATOI,   &debug_3c575       },
1062             { "3C59X.DEBUG",     ARG_ATOI,   &vortex_debug      },
1063             { "3C59X.IFACE0",    ARG_ATOI,   &vortex_options[0] },
1064             { "3C59X.IFACE1",    ARG_ATOI,   &vortex_options[1] },
1065             { "3C59X.IFACE2",    ARG_ATOI,   &vortex_options[2] },
1066             { "3C59X.IFACE3",    ARG_ATOI,   &vortex_options[3] },
1067             { "3C90X.DEBUG",     ARG_ATOX_W, &tc90xbc_debug     },
1068             { "ACCT.DEBUG",      ARG_ATOI,   &ethpk_debug       },
1069             { "CS89.DEBUG",      ARG_ATOI,   &cs89_debug        },
1070             { "RTL8139.DEBUG",   ARG_ATOI,   &rtl8139_debug     },
1071         /*  { "RTL8139.FDUPLEX", ARG_ATOI,   &rtl8139_options   }, */
1072             { "SMC.DEBUG",       ARG_ATOI,   &ei_debug          },
1073         /*  { "E100.DEBUG",      ARG_ATOI,   &e100_debug        }, */
1074             { "PCI.DEBUG",       ARG_ATOI,   &pci_debug         },
1075             { "BIOS32.DEBUG",    ARG_ATOI,   &bios32_debug      },
1076             { "IRQ.DEBUG",       ARG_ATOI,   &irq_debug         },
1077             { "TIMER.IRQ",       ARG_ATOI,   &timer_irq         },
1078 #endif
1079             { NULL }
1080           };
1081 
1082 /*
1083  * pcap_config_hook() is an extension to application's config
1084  * handling. Uses Watt-32's config-table function.
1085  */
pcap_config_hook(const char * keyword,const char * value)1086 int pcap_config_hook (const char *keyword, const char *value)
1087 {
1088   return parse_config_table (debug_tab, NULL, keyword, value);
1089 }
1090 
1091 /*
1092  * Linked list of supported devices
1093  */
1094 struct device       *active_dev = NULL;      /* the device we have opened */
1095 struct device       *probed_dev = NULL;      /* the device we have probed */
1096 const struct device *dev_base   = &pkt_dev;  /* list of network devices */
1097 
1098 /*
1099  * PKTDRVR device functions
1100  */
1101 int pcap_pkt_debug = -1;
1102 
pkt_close(struct device * dev)1103 static void pkt_close (struct device *dev)
1104 {
1105   BOOL okay = PktExitDriver();
1106 
1107   if (pcap_pkt_debug > 1)
1108      fprintf (stderr, "pkt_close(): %d\n", okay);
1109 
1110   if (dev->priv)
1111      free (dev->priv);
1112   dev->priv = NULL;
1113 }
1114 
pkt_open(struct device * dev)1115 static int pkt_open (struct device *dev)
1116 {
1117   PKT_RX_MODE mode;
1118 
1119   if (dev->flags & IFF_PROMISC)
1120        mode = PDRX_ALL_PACKETS;
1121   else mode = PDRX_BROADCAST;
1122 
1123   if (!PktInitDriver(mode))
1124      return (0);
1125 
1126   PktResetStatistics (pktInfo.handle);
1127   PktQueueBusy (FALSE);
1128   return (1);
1129 }
1130 
pkt_xmit(struct device * dev,const void * buf,int len)1131 static int pkt_xmit (struct device *dev, const void *buf, int len)
1132 {
1133   struct net_device_stats *stats = (struct net_device_stats*) dev->priv;
1134 
1135   if (pcap_pkt_debug > 0)
1136      dbug_write ("pcap_xmit\n");
1137 
1138   if (!PktTransmit(buf,len))
1139   {
1140     stats->tx_errors++;
1141     return (0);
1142   }
1143   return (len);
1144 }
1145 
pkt_stats(struct device * dev)1146 static void *pkt_stats (struct device *dev)
1147 {
1148   struct net_device_stats *stats = (struct net_device_stats*) dev->priv;
1149 
1150   if (!stats || !PktSessStatistics(pktInfo.handle))
1151      return (NULL);
1152 
1153   stats->rx_packets       = pktStat.inPackets;
1154   stats->rx_errors        = pktStat.lost;
1155   stats->rx_missed_errors = PktRxDropped();
1156   return (stats);
1157 }
1158 
pkt_probe(struct device * dev)1159 static int pkt_probe (struct device *dev)
1160 {
1161   if (!PktSearchDriver())
1162      return (0);
1163 
1164   dev->open           = pkt_open;
1165   dev->xmit           = pkt_xmit;
1166   dev->close          = pkt_close;
1167   dev->get_stats      = pkt_stats;
1168   dev->copy_rx_buf    = PktReceive;  /* farmem peek and copy routine */
1169   dev->get_rx_buf     = NULL;
1170   dev->peek_rx_buf    = NULL;
1171   dev->release_rx_buf = NULL;
1172   dev->priv           = calloc (sizeof(struct net_device_stats), 1);
1173   if (!dev->priv)
1174      return (0);
1175   return (1);
1176 }
1177 
1178 /*
1179  * NDIS device functions
1180  */
ndis_close(struct device * dev)1181 static void ndis_close (struct device *dev)
1182 {
1183 #ifdef USE_NDIS2
1184   NdisShutdown();
1185 #endif
1186   ARGSUSED (dev);
1187 }
1188 
ndis_open(struct device * dev)1189 static int ndis_open (struct device *dev)
1190 {
1191   int promis = (dev->flags & IFF_PROMISC);
1192 
1193 #ifdef USE_NDIS2
1194   if (!NdisInit(promis))
1195      return (0);
1196   return (1);
1197 #else
1198   ARGSUSED (promis);
1199   return (0);
1200 #endif
1201 }
1202 
ndis_stats(struct device * dev)1203 static void *ndis_stats (struct device *dev)
1204 {
1205   static struct net_device_stats stats;
1206 
1207   /* to-do */
1208   ARGSUSED (dev);
1209   return (&stats);
1210 }
1211 
ndis_probe(struct device * dev)1212 static int ndis_probe (struct device *dev)
1213 {
1214 #ifdef USE_NDIS2
1215   if (!NdisOpen())
1216      return (0);
1217 #endif
1218 
1219   dev->open           = ndis_open;
1220   dev->xmit           = NULL;
1221   dev->close          = ndis_close;
1222   dev->get_stats      = ndis_stats;
1223   dev->copy_rx_buf    = NULL;       /* to-do */
1224   dev->get_rx_buf     = NULL;       /* upcall is from rmode driver */
1225   dev->peek_rx_buf    = NULL;
1226   dev->release_rx_buf = NULL;
1227   return (0);
1228 }
1229 
1230 /*
1231  * Search & probe for supported 32-bit (pmode) pcap devices
1232  */
1233 #if defined(USE_32BIT_DRIVERS)
1234 
1235 struct device el2_dev LOCKED_VAR = {
1236               "3c503",
1237               "EtherLink II",
1238               0,
1239               0,0,0,0,0,0,
1240               NULL,
1241               el2_probe
1242             };
1243 
1244 struct device el3_dev LOCKED_VAR = {
1245               "3c509",
1246               "EtherLink III",
1247               0,
1248               0,0,0,0,0,0,
1249               &el2_dev,
1250               el3_probe
1251             };
1252 
1253 struct device tc515_dev LOCKED_VAR = {
1254               "3c515",
1255               "EtherLink PCI",
1256               0,
1257               0,0,0,0,0,0,
1258               &el3_dev,
1259               tc515_probe
1260             };
1261 
1262 struct device tc59_dev LOCKED_VAR = {
1263               "3c59x",
1264               "EtherLink PCI",
1265               0,
1266               0,0,0,0,0,0,
1267               &tc515_dev,
1268               tc59x_probe
1269             };
1270 
1271 struct device tc90xbc_dev LOCKED_VAR = {
1272               "3c90x",
1273               "EtherLink 90X",
1274               0,
1275               0,0,0,0,0,0,
1276               &tc59_dev,
1277               tc90xbc_probe
1278             };
1279 
1280 struct device wd_dev LOCKED_VAR = {
1281               "wd",
1282               "Westen Digital",
1283               0,
1284               0,0,0,0,0,0,
1285               &tc90xbc_dev,
1286               wd_probe
1287             };
1288 
1289 struct device ne_dev LOCKED_VAR = {
1290               "ne",
1291               "NEx000",
1292               0,
1293               0,0,0,0,0,0,
1294               &wd_dev,
1295               ne_probe
1296             };
1297 
1298 struct device acct_dev LOCKED_VAR = {
1299               "acct",
1300               "Accton EtherPocket",
1301               0,
1302               0,0,0,0,0,0,
1303               &ne_dev,
1304               ethpk_probe
1305             };
1306 
1307 struct device cs89_dev LOCKED_VAR = {
1308               "cs89",
1309               "Crystal Semiconductor",
1310               0,
1311               0,0,0,0,0,0,
1312               &acct_dev,
1313               cs89x0_probe
1314             };
1315 
1316 struct device rtl8139_dev LOCKED_VAR = {
1317               "rtl8139",
1318               "RealTek PCI",
1319               0,
1320               0,0,0,0,0,0,
1321               &cs89_dev,
1322               rtl8139_probe     /* dev->probe routine */
1323             };
1324 
1325 /*
1326  * Dequeue routine is called by polling.
1327  * NOTE: the queue-element is not copied, only a pointer is
1328  * returned at '*buf'
1329  */
peek_rxbuf(BYTE ** buf)1330 int peek_rxbuf (BYTE **buf)
1331 {
1332   struct rx_elem *tail, *head;
1333 
1334   PCAP_ASSERT (pktq_check (&active_dev->queue));
1335 
1336   DISABLE();
1337   tail = pktq_out_elem (&active_dev->queue);
1338   head = pktq_in_elem (&active_dev->queue);
1339   ENABLE();
1340 
1341   if (head != tail)
1342   {
1343     PCAP_ASSERT (tail->size < active_dev->queue.elem_size-4-2);
1344 
1345     *buf = &tail->data[0];
1346     return (tail->size);
1347   }
1348   *buf = NULL;
1349   return (0);
1350 }
1351 
1352 /*
1353  * Release buffer we peeked at above.
1354  */
release_rxbuf(BYTE * buf)1355 int release_rxbuf (BYTE *buf)
1356 {
1357 #ifndef NDEBUG
1358   struct rx_elem *tail = pktq_out_elem (&active_dev->queue);
1359 
1360   PCAP_ASSERT (&tail->data[0] == buf);
1361 #else
1362   ARGSUSED (buf);
1363 #endif
1364   pktq_inc_out (&active_dev->queue);
1365   return (1);
1366 }
1367 
1368 /*
1369  * get_rxbuf() routine (in locked code) is called from IRQ handler
1370  * to request a buffer. Interrupts are disabled and we have a 32kB stack.
1371  */
get_rxbuf(int len)1372 BYTE *get_rxbuf (int len)
1373 {
1374   int idx;
1375 
1376   if (len < ETH_MIN || len > ETH_MAX)
1377      return (NULL);
1378 
1379   idx = pktq_in_index (&active_dev->queue);
1380 
1381 #ifdef DEBUG
1382   {
1383     static int fan_idx LOCKED_VAR = 0;
1384     writew ("-\\|/"[fan_idx++] | (15 << 8),      /* white on black colour */
1385             0xB8000 + 2*79);  /* upper-right corner, 80-col colour screen */
1386     fan_idx &= 3;
1387   }
1388 /* writew (idx + '0' + 0x0F00, 0xB8000 + 2*78); */
1389 #endif
1390 
1391   if (idx != active_dev->queue.out_index)
1392   {
1393     struct rx_elem *head = pktq_in_elem (&active_dev->queue);
1394 
1395     head->size = len;
1396     active_dev->queue.in_index = idx;
1397     return (&head->data[0]);
1398   }
1399 
1400   /* !!to-do: drop 25% of the oldest element
1401    */
1402   pktq_clear (&active_dev->queue);
1403   return (NULL);
1404 }
1405 
1406 /*
1407  *  Simple ring-buffer queue handler for reception of packets
1408  *  from network driver.
1409  */
1410 #define PKTQ_MARKER  0xDEADBEEF
1411 
pktq_check(struct rx_ringbuf * q)1412 static int pktq_check (struct rx_ringbuf *q)
1413 {
1414 #ifndef NDEBUG
1415   int   i;
1416   char *buf;
1417 #endif
1418 
1419   if (!q || !q->num_elem || !q->buf_start)
1420      return (0);
1421 
1422 #ifndef NDEBUG
1423   buf = q->buf_start;
1424 
1425   for (i = 0; i < q->num_elem; i++)
1426   {
1427     buf += q->elem_size;
1428     if (*(DWORD*)(buf - sizeof(DWORD)) != PKTQ_MARKER)
1429        return (0);
1430   }
1431 #endif
1432   return (1);
1433 }
1434 
pktq_init(struct rx_ringbuf * q,int size,int num,char * pool)1435 static int pktq_init (struct rx_ringbuf *q, int size, int num, char *pool)
1436 {
1437   int i;
1438 
1439   q->elem_size = size;
1440   q->num_elem  = num;
1441   q->buf_start = pool;
1442   q->in_index  = 0;
1443   q->out_index = 0;
1444 
1445   PCAP_ASSERT (size >= sizeof(struct rx_elem) + sizeof(DWORD));
1446   PCAP_ASSERT (num);
1447   PCAP_ASSERT (pool);
1448 
1449   for (i = 0; i < num; i++)
1450   {
1451 #if 0
1452     struct rx_elem *elem = (struct rx_elem*) pool;
1453 
1454     /* assert dword aligned elements
1455      */
1456     PCAP_ASSERT (((unsigned)(&elem->data[0]) & 3) == 0);
1457 #endif
1458     pool += size;
1459     *(DWORD*) (pool - sizeof(DWORD)) = PKTQ_MARKER;
1460   }
1461   return (1);
1462 }
1463 
1464 /*
1465  * Increment the queue 'out_index' (tail).
1466  * Check for wraps.
1467  */
pktq_inc_out(struct rx_ringbuf * q)1468 static int pktq_inc_out (struct rx_ringbuf *q)
1469 {
1470   q->out_index++;
1471   if (q->out_index >= q->num_elem)
1472       q->out_index = 0;
1473   return (q->out_index);
1474 }
1475 
1476 /*
1477  * Return the queue's next 'in_index' (head).
1478  * Check for wraps.
1479  */
pktq_in_index(struct rx_ringbuf * q)1480 static int pktq_in_index (struct rx_ringbuf *q)
1481 {
1482   volatile int index = q->in_index + 1;
1483 
1484   if (index >= q->num_elem)
1485       index = 0;
1486   return (index);
1487 }
1488 
1489 /*
1490  * Return the queue's head-buffer.
1491  */
pktq_in_elem(struct rx_ringbuf * q)1492 static struct rx_elem *pktq_in_elem (struct rx_ringbuf *q)
1493 {
1494   return (struct rx_elem*) (q->buf_start + (q->elem_size * q->in_index));
1495 }
1496 
1497 /*
1498  * Return the queue's tail-buffer.
1499  */
pktq_out_elem(struct rx_ringbuf * q)1500 static struct rx_elem *pktq_out_elem (struct rx_ringbuf *q)
1501 {
1502   return (struct rx_elem*) (q->buf_start + (q->elem_size * q->out_index));
1503 }
1504 
1505 /*
1506  * Clear the queue ring-buffer by setting head=tail.
1507  */
pktq_clear(struct rx_ringbuf * q)1508 static void pktq_clear (struct rx_ringbuf *q)
1509 {
1510   q->in_index = q->out_index;
1511 }
1512 
1513 /*
1514  * Symbols that must be linkable for "gcc -O0"
1515  */
1516 #undef __IOPORT_H
1517 #undef __DMA_H
1518 
1519 #define extern
1520 #define __inline__
1521 
1522 #include "msdos/pm_drvr/ioport.h"
1523 #include "msdos/pm_drvr/dma.h"
1524 
1525 #endif /* USE_32BIT_DRIVERS */
1526 
1527 /*
1528  * Libpcap version string.
1529  */
1530 const char *
pcap_lib_version(void)1531 pcap_lib_version(void)
1532 {
1533   return ("DOS-" PCAP_VERSION_STRING);
1534 }
1535