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