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