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