home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / E-zine / Magazines / crh / freebsd / rootkit / sniffit.0.3.5 / libpcap-0.3 / pcap-dlpi.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-27  |  18.7 KB  |  840 lines

  1. /*
  2.  * Copyright (c) 1993, 1994, 1995, 1996
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  *
  21.  * This code contributed by Atanu Ghosh (atanu@cs.ucl.ac.uk),
  22.  * University College London.
  23.  */
  24.  
  25. /*
  26.  * Packet capture routine for dlpi under SunOS 5
  27.  *
  28.  * Notes:
  29.  *
  30.  *    - Apparently the DLIOCRAW ioctl() is specific to SunOS.
  31.  *
  32.  *    - There is a bug in bufmod(7) such that setting the snapshot
  33.  *      length results in data being left of the front of the packet.
  34.  *
  35.  *    - It might be desirable to use pfmod(7) to filter packets in the
  36.  *      kernel.
  37.  */
  38.  
  39. #ifndef lint
  40. static const char rcsid[] =
  41.     "@(#) $Header: pcap-dlpi.c,v 1.47 96/12/10 23:15:00 leres Exp $ (LBL)";
  42. #endif
  43.  
  44. #include <sys/types.h>
  45. #include <sys/time.h>
  46. #ifdef HAVE_SYS_BUFMOD_H
  47. #include <sys/bufmod.h>
  48. #endif
  49. #include <sys/dlpi.h>
  50. #ifdef HAVE_SYS_DLPI_EXT_H
  51. #include <sys/dlpi_ext.h>
  52. #endif
  53. #ifdef HAVE_HPUX9
  54. #include <sys/socket.h>
  55. #endif
  56. #ifdef DL_HP_PPA_ACK_OBS
  57. #include <sys/stat.h>
  58. #endif
  59. #include <sys/stream.h>
  60. #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
  61. #include <sys/systeminfo.h>
  62. #endif
  63.  
  64. #ifdef HAVE_HPUX9
  65. #include <net/if.h>
  66. #endif
  67.  
  68. #include <ctype.h>
  69. #ifdef HAVE_HPUX9
  70. #include <nlist.h>
  71. #endif
  72. #include <errno.h>
  73. #include <fcntl.h>
  74. #include <memory.h>
  75. #include <stdio.h>
  76. #include <stdlib.h>
  77. #include <string.h>
  78. #include <stropts.h>
  79. #include <unistd.h>
  80.  
  81. #include "pcap-int.h"
  82.  
  83. #include "gnuc.h"
  84. #ifdef HAVE_OS_PROTO_H
  85. #include "os-proto.h"
  86. #endif
  87.  
  88. #ifndef PCAP_DEV_PREFIX
  89. #define PCAP_DEV_PREFIX "/dev"
  90. #endif
  91.  
  92. #define    MAXDLBUF    8192
  93.  
  94. /* Forwards */
  95. static int dlattachreq(int, bpf_u_int32, char *);
  96. static int dlbindack(int, char *, char *);
  97. static int dlbindreq(int, bpf_u_int32, char *);
  98. static int dlinfoack(int, char *, char *);
  99. static int dlinforeq(int, char *);
  100. static int dlokack(int, const char *, char *, char *);
  101. static int recv_ack(int, int, const char *, char *, char *);
  102. static int dlpromisconreq(int, bpf_u_int32, char *);
  103. #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
  104. static char *get_release(bpf_u_int32 *, bpf_u_int32 *, bpf_u_int32 *);
  105. #endif
  106. static int send_request(int, char *, int, char *, char *);
  107. #ifdef HAVE_SYS_BUFMOD_H
  108. static int strioctl(int, int, int, char *);
  109. #endif
  110. #ifdef HAVE_HPUX9
  111. static int dlpi_kread(int, off_t, void *, u_int, char *);
  112. #endif
  113. #ifdef HAVE_DEV_DLPI
  114. static int get_dlpi_ppa(int, const char *, int, char *);
  115. #endif
  116.  
  117. int
  118. pcap_stats(pcap_t *p, struct pcap_stat *ps)
  119. {
  120.  
  121.     *ps = p->md.stat;
  122.     return (0);
  123. }
  124.  
  125. /* XXX Needed by HP-UX (at least) */
  126. static bpf_u_int32 ctlbuf[MAXDLBUF];
  127. static struct strbuf ctl = {
  128.     MAXDLBUF,
  129.     0,
  130.     (char *)ctlbuf
  131. };
  132.  
  133. int
  134. pcap_read(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
  135. {
  136.     register int cc, n, caplen, origlen;
  137.     register u_char *bp, *ep, *pk;
  138.     register struct bpf_insn *fcode;
  139. #ifdef HAVE_SYS_BUFMOD_H
  140.     register struct sb_hdr *sbp;
  141. #ifdef LBL_ALIGN
  142.     struct sb_hdr sbhdr;
  143. #endif
  144. #endif
  145.     int flags;
  146.     struct strbuf data;
  147.     struct pcap_pkthdr pkthdr;
  148.  
  149.     flags = 0;
  150.     cc = p->cc;
  151.     if (cc == 0) {
  152.         data.buf = (char *)p->buffer + p->offset;
  153.         data.maxlen = MAXDLBUF;
  154.         data.len = 0;
  155.         do {
  156.             if (getmsg(p->fd, &ctl, &data, &flags) < 0) {
  157.                 /* Don't choke when we get ptraced */
  158.                 if (errno == EINTR) {
  159.                     cc = 0;
  160.                     continue;
  161.                 }
  162.                 strcpy(p->errbuf, pcap_strerror(errno));
  163.                 return (-1);
  164.             }
  165.             cc = data.len;
  166.         } while (cc == 0);
  167.         bp = p->buffer + p->offset;
  168.     } else
  169.         bp = p->bp;
  170.  
  171.     /* Loop through packets */
  172.     fcode = p->fcode.bf_insns;
  173.     ep = bp + cc;
  174.     n = 0;
  175. #ifdef HAVE_SYS_BUFMOD_H
  176.     while (bp < ep) {
  177. #ifdef LBL_ALIGN
  178.         if ((long)bp & 3) {
  179.             sbp = &sbhdr;
  180.             memcpy(sbp, bp, sizeof(*sbp));
  181.         } else
  182. #endif
  183.             sbp = (struct sb_hdr *)bp;
  184.         p->md.stat.ps_drop += sbp->sbh_drops;
  185.         pk = bp + sizeof(*sbp);
  186.         bp += sbp->sbh_totlen;
  187.         origlen = sbp->sbh_origlen;
  188.         caplen = sbp->sbh_msglen;
  189. #else
  190.         origlen = cc;
  191.         caplen = min(p->snapshot, cc);
  192.         pk = bp;
  193.         bp += caplen;
  194. #endif
  195.         ++p->md.stat.ps_recv;
  196.         if (bpf_filter(fcode, pk, origlen, caplen)) {
  197. #ifdef HAVE_SYS_BUFMOD_H
  198.             pkthdr.ts = sbp->sbh_timestamp;
  199. #else
  200.             (void)gettimeofday(&pkthdr.ts, NULL);
  201. #endif
  202.             pkthdr.len = origlen;
  203.             pkthdr.caplen = caplen;
  204.             /* Insure caplen does not exceed snapshot */
  205.             if (pkthdr.caplen > p->snapshot)
  206.                 pkthdr.caplen = p->snapshot;
  207.             (*callback)(user, &pkthdr, pk);
  208.             if (++n >= cnt && cnt >= 0) {
  209.                 p->cc = ep - bp;
  210.                 p->bp = bp;
  211.                 return (n);
  212.             }
  213.         }
  214. #ifdef HAVE_SYS_BUFMOD_H
  215.     }
  216. #endif
  217.     p->cc = 0;
  218.     return (n);
  219. }
  220.  
  221. pcap_t *
  222. pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
  223. {
  224.     register char *cp;
  225.     char *eos;
  226.     register pcap_t *p;
  227.     register int ppa;
  228.     register dl_info_ack_t *infop;
  229. #ifdef HAVE_SYS_BUFMOD_H
  230.     bpf_u_int32 ss, flag;
  231. #ifdef HAVE_SOLARIS
  232.     register char *release;
  233.     bpf_u_int32 osmajor, osminor, osmicro;
  234. #endif
  235. #endif
  236.     bpf_u_int32 buf[MAXDLBUF];
  237.     char dname[100];
  238. #ifndef HAVE_DEV_DLPI
  239.     char dname2[100];
  240. #endif
  241.  
  242.     p = (pcap_t *)malloc(sizeof(*p));
  243.     if (p == NULL) {
  244.         strcpy(ebuf, pcap_strerror(errno));
  245.         return (NULL);
  246.     }
  247.     memset(p, 0, sizeof(*p));
  248.  
  249.     /*
  250.     ** Determine device and ppa
  251.     */
  252.     cp = strpbrk(device, "0123456789");
  253.     if (cp == NULL) {
  254.         sprintf(ebuf, "%s missing unit number", device);
  255.         goto bad;
  256.     }
  257.     ppa = strtol(cp, &eos, 10);
  258.     if (*eos != '\0') {
  259.         sprintf(ebuf, "%s bad unit number", device);
  260.         goto bad;
  261.     }
  262.  
  263.     if (*device == '/')
  264.         strcpy(dname, device);
  265.     else
  266.         sprintf(dname, "%s/%s", PCAP_DEV_PREFIX, device);
  267. #ifdef HAVE_DEV_DLPI
  268.     /* Map network device to /dev/dlpi unit */
  269.     cp = "/dev/dlpi";
  270.     if ((p->fd = open(cp, O_RDWR)) < 0) {
  271.         sprintf(ebuf, "%s: %s", cp, pcap_strerror(errno));
  272.         goto bad;
  273.     }
  274.     /* Map network interface to /dev/dlpi unit */
  275.     ppa = get_dlpi_ppa(p->fd, dname, ppa, ebuf);
  276.     if (ppa < 0)
  277.         goto bad;
  278. #else
  279.     /* Try device without unit number */
  280.     strcpy(dname2, dname);
  281.     cp = strchr(dname, *cp);
  282.     *cp = '\0';
  283.     if ((p->fd = open(dname, O_RDWR)) < 0) {
  284.         if (errno != ENOENT) {
  285.             sprintf(ebuf, "%s: %s", dname, pcap_strerror(errno));
  286.             goto bad;
  287.         }
  288.  
  289.         /* Try again with unit number */
  290.         if ((p->fd = open(dname2, O_RDWR)) < 0) {
  291.             sprintf(ebuf, "%s: %s", dname2, pcap_strerror(errno));
  292.             goto bad;
  293.         }
  294.         /* XXX Assume unit zero */
  295.         ppa = 0;
  296.     }
  297. #endif
  298.  
  299.     p->snapshot = snaplen;
  300.  
  301.     /*
  302.     ** Attach if "style 2" provider
  303.     */
  304.     if (dlinforeq(p->fd, ebuf) < 0 ||
  305.         dlinfoack(p->fd, (char *)buf, ebuf) < 0)
  306.         goto bad;
  307.     infop = &((union DL_primitives *)buf)->info_ack;
  308.     if (infop->dl_provider_style == DL_STYLE2 &&
  309.         (dlattachreq(p->fd, ppa, ebuf) < 0 ||
  310.         dlokack(p->fd, "attach", (char *)buf, ebuf) < 0))
  311.         goto bad;
  312.     /*
  313.     ** Bind (defer if using HP-UX 9, totally skip if using SINIX)
  314.     */
  315. #if !defined(HAVE_HPUX9) && !defined(sinix)
  316.     if (dlbindreq(p->fd, 0, ebuf) < 0 ||
  317.         dlbindack(p->fd, (char *)buf, ebuf) < 0)
  318.         goto bad;
  319. #endif
  320.  
  321.     if (promisc) {
  322.         /*
  323.         ** Enable promiscuous
  324.         */
  325.         if (dlpromisconreq(p->fd, DL_PROMISC_PHYS, ebuf) < 0 ||
  326.             dlokack(p->fd, "promisc_phys", (char *)buf, ebuf) < 0)
  327.             goto bad;
  328.  
  329.         /*
  330.         ** Try to enable multicast (you would have thought
  331.         ** promiscuous would be sufficient). Skip if SINIX.
  332.         */
  333. #ifndef sinix
  334.         if (dlpromisconreq(p->fd, DL_PROMISC_MULTI, ebuf) < 0 ||
  335.             dlokack(p->fd, "promisc_multi", (char *)buf, ebuf) < 0)
  336.             fprintf(stderr,
  337.                 "WARNING: DL_PROMISC_MULTI failed (%s)\n", ebuf);
  338. #endif
  339.     }
  340.     /*
  341.     ** Always try to enable sap (except if SINIX)
  342.     */
  343. #ifndef sinix
  344.     if (dlpromisconreq(p->fd, DL_PROMISC_SAP, ebuf) < 0 ||
  345.         dlokack(p->fd, "promisc_sap", (char *)buf, ebuf) < 0) {
  346.         /* Not fatal if promisc since the DL_PROMISC_PHYS worked */
  347.         if (promisc)
  348.             fprintf(stderr,
  349.                 "WARNING: DL_PROMISC_SAP failed (%s)\n", ebuf);
  350.         else
  351.             goto bad;
  352.     }
  353. #endif
  354.  
  355.     /*
  356.     ** Bind (HP-UX 9 must bind after setting promiscuous options)
  357.     */
  358. #ifdef HAVE_HPUX9
  359.     if (dlbindreq(p->fd, 0, ebuf) < 0 ||
  360.         dlbindack(p->fd, (char *)buf, ebuf) < 0)
  361.         goto bad;
  362. #endif
  363.  
  364.     /*
  365.     ** Determine link type
  366.     */
  367.     if (dlinforeq(p->fd, ebuf) < 0 ||
  368.         dlinfoack(p->fd, (char *)buf, ebuf) < 0)
  369.         goto bad;
  370.  
  371.     infop = &((union DL_primitives *)buf)->info_ack;
  372.     switch (infop->dl_mac_type) {
  373.  
  374.     case DL_CSMACD:
  375.     case DL_ETHER:
  376.         p->linktype = DLT_EN10MB;
  377.         p->offset = 2;
  378.         break;
  379.  
  380.     case DL_FDDI:
  381.         p->linktype = DLT_FDDI;
  382.         break;
  383.  
  384.     default:
  385.         sprintf(ebuf, "unknown mac type 0x%lu", infop->dl_mac_type);
  386.         goto bad;
  387.     }
  388.  
  389. #ifdef    DLIOCRAW
  390.     /*
  391.     ** This is a non standard SunOS hack to get the ethernet header.
  392.     */
  393.     if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) {
  394.         sprintf(ebuf, "DLIOCRAW: %s", pcap_strerror(errno));
  395.         goto bad;
  396.     }
  397. #endif
  398.  
  399. #ifdef HAVE_SYS_BUFMOD_H
  400.     /*
  401.     ** Another non standard call to get the data nicely buffered
  402.     */
  403.     if (ioctl(p->fd, I_PUSH, "bufmod") != 0) {
  404.         sprintf(ebuf, "I_PUSH bufmod: %s", pcap_strerror(errno));
  405.         goto bad;
  406.     }
  407.  
  408.     /*
  409.     ** Now that the bufmod is pushed lets configure it.
  410.     **
  411.     ** There is a bug in bufmod(7). When dealing with messages of
  412.     ** less than snaplen size it strips data from the beginning not
  413.     ** the end.
  414.     **
  415.     ** This bug is supposed to be fixed in 5.3.2. Also, there is a
  416.     ** patch available. Ask for bugid 1149065.
  417.     */
  418.     ss = snaplen;
  419. #ifdef HAVE_SOLARIS
  420.     release = get_release(&osmajor, &osminor, &osmicro);
  421.     if (osmajor == 5 && (osminor <= 2 || (osminor == 3 && osmicro < 2)) &&
  422.         getenv("BUFMOD_FIXED") == NULL) {
  423.         fprintf(stderr,
  424.         "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.\n",
  425.             release);
  426.         ss = 0;
  427.     }
  428. #endif
  429.     if (ss > 0 &&
  430.         strioctl(p->fd, SBIOCSSNAP, sizeof(ss), (char *)&ss) != 0) {
  431.         sprintf(ebuf, "SBIOCSSNAP: %s", pcap_strerror(errno));
  432.         goto bad;
  433.     }
  434.  
  435.     /*
  436.     ** Set up the bufmod flags
  437.     */
  438.     if (strioctl(p->fd, SBIOCGFLAGS, sizeof(flag), (char *)&flag) < 0) {
  439.         sprintf(ebuf, "SBIOCGFLAGS: %s", pcap_strerror(errno));
  440.         goto bad;
  441.     }
  442.     flag |= SB_NO_DROPS;
  443.     if (strioctl(p->fd, SBIOCSFLAGS, sizeof(flag), (char *)&flag) != 0) {
  444.         sprintf(ebuf, "SBIOCSFLAGS: %s", pcap_strerror(errno));
  445.         goto bad;
  446.     }
  447.     /*
  448.     ** Set up the bufmod timeout
  449.     */
  450.     if (to_ms != 0) {
  451.         struct timeval to;
  452.  
  453.         to.tv_sec = to_ms / 1000;
  454.         to.tv_usec = (to_ms * 1000) % 1000000;
  455.         if (strioctl(p->fd, SBIOCSTIME, sizeof(to), (char *)&to) != 0) {
  456.             sprintf(ebuf, "SBIOCSTIME: %s", pcap_strerror(errno));
  457.             goto bad;
  458.         }
  459.     }
  460. #endif
  461.  
  462.     /*
  463.     ** As the last operation flush the read side.
  464.     */
  465.     if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
  466.         sprintf(ebuf, "FLUSHR: %s", pcap_strerror(errno));
  467.         goto bad;
  468.     }
  469.     /* Allocate data buffer */
  470.     p->bufsize = MAXDLBUF * sizeof(bpf_u_int32);
  471.     p->buffer = (u_char *)malloc(p->bufsize + p->offset);
  472.  
  473.     return (p);
  474. bad:
  475.     free(p);
  476.     return (NULL);
  477. }
  478.  
  479. int
  480. pcap_setfilter(pcap_t *p, struct bpf_program *fp)
  481. {
  482.  
  483.     p->fcode = *fp;
  484.     return (0);
  485. }
  486.  
  487. static int
  488. send_request(int fd, char *ptr, int len, char *what, char *ebuf)
  489. {
  490.     struct    strbuf    ctl;
  491.     int    flags;
  492.  
  493.     ctl.maxlen = 0;
  494.     ctl.len = len;
  495.     ctl.buf = ptr;
  496.  
  497.     flags = 0;
  498.     if (putmsg(fd, &ctl, (struct strbuf *) NULL, flags) < 0) {
  499.         sprintf(ebuf, "send_request: putmsg \"%s\": %s",
  500.             what, pcap_strerror(errno));
  501.         return (-1);
  502.     }
  503.     return (0);
  504. }
  505.  
  506. static int
  507. recv_ack(int fd, int size, const char *what, char *bufp, char *ebuf)
  508. {
  509.     union    DL_primitives    *dlp;
  510.     struct    strbuf    ctl;
  511.     int    flags;
  512.  
  513.     ctl.maxlen = MAXDLBUF;
  514.     ctl.len = 0;
  515.     ctl.buf = bufp;
  516.  
  517.     flags = 0;
  518.     if (getmsg(fd, &ctl, (struct strbuf*)NULL, &flags) < 0) {
  519.         sprintf(ebuf, "recv_ack: %s getmsg: %s",
  520.             what, pcap_strerror(errno));
  521.         return (-1);
  522.     }
  523.  
  524.     dlp = (union DL_primitives *) ctl.buf;
  525.     switch (dlp->dl_primitive) {
  526.  
  527.     case DL_INFO_ACK:
  528.     case DL_BIND_ACK:
  529.     case DL_OK_ACK:
  530. #ifdef DL_HP_PPA_ACK
  531.     case DL_HP_PPA_ACK:
  532. #endif
  533.  
  534.         /* These are OK */
  535.         break;
  536.  
  537.     case DL_ERROR_ACK:
  538.         switch (dlp->error_ack.dl_errno) {
  539.  
  540.         case DL_BADPPA:
  541.             sprintf(ebuf, "recv_ack: %s bad ppa (device unit)",
  542.                 what);
  543.             break;
  544.  
  545.  
  546.         case DL_SYSERR:
  547.             sprintf(ebuf, "recv_ack: %s: %s",
  548.                 what, pcap_strerror(dlp->error_ack.dl_unix_errno));
  549.             break;
  550.  
  551.         case DL_UNSUPPORTED:
  552.             sprintf(ebuf,
  553.                 "recv_ack: %s: Service not supplied by provider",
  554.                 what);
  555.             break;
  556.  
  557.         default:
  558.             sprintf(ebuf, "recv_ack: %s error 0x%x",
  559.                 what, (bpf_u_int32)dlp->error_ack.dl_errno);
  560.             break;
  561.         }
  562.         return (-1);
  563.  
  564.     default:
  565.         sprintf(ebuf, "recv_ack: %s unexpected primitive ack 0x%x ",
  566.             what, (bpf_u_int32)dlp->dl_primitive);
  567.         return (-1);
  568.     }
  569.  
  570.     if (ctl.len < size) {
  571.         sprintf(ebuf, "recv_ack: %s ack too small (%d < %d)",
  572.             what, ctl.len, size);
  573.         return (-1);
  574.     }
  575.     return (ctl.len);
  576. }
  577.  
  578. static int
  579. dlattachreq(int fd, bpf_u_int32 ppa, char *ebuf)
  580. {
  581.     dl_attach_req_t    req;
  582.  
  583.     req.dl_primitive = DL_ATTACH_REQ;
  584.     req.dl_ppa = ppa;
  585.  
  586.     return (send_request(fd, (char *)&req, sizeof(req), "attach", ebuf));
  587. }
  588.  
  589. static int
  590. dlbindreq(int fd, bpf_u_int32 sap, char *ebuf)
  591. {
  592.  
  593.     dl_bind_req_t    req;
  594.  
  595.     memset((char *)&req, 0, sizeof(req));
  596.     req.dl_primitive = DL_BIND_REQ;
  597. #ifdef DL_HP_RAWDLS
  598.     req.dl_max_conind = 1;            /* XXX magic number */
  599.     /* 22 is INSAP as per the HP-UX DLPI Programmer's Guide */
  600.     req.dl_sap = 22;
  601.     req.dl_service_mode = DL_HP_RAWDLS;
  602. #else
  603.     req.dl_sap = sap;
  604. #ifdef DL_CLDLS
  605.     req.dl_service_mode = DL_CLDLS;
  606. #endif
  607. #endif
  608.  
  609.     return (send_request(fd, (char *)&req, sizeof(req), "bind", ebuf));
  610. }
  611.  
  612. static int
  613. dlbindack(int fd, char *bufp, char *ebuf)
  614. {
  615.  
  616.     return (recv_ack(fd, DL_BIND_ACK_SIZE, "bind", bufp, ebuf));
  617. }
  618.  
  619. static int
  620. dlpromisconreq(int fd, bpf_u_int32 level, char *ebuf)
  621. {
  622.     dl_promiscon_req_t req;
  623.  
  624.     req.dl_primitive = DL_PROMISCON_REQ;
  625.     req.dl_level = level;
  626.  
  627.     return (send_request(fd, (char *)&req, sizeof(req), "promiscon", ebuf));
  628. }
  629.  
  630. static int
  631. dlokack(int fd, const char *what, char *bufp, char *ebuf)
  632. {
  633.  
  634.     return (recv_ack(fd, DL_OK_ACK_SIZE, what, bufp, ebuf));
  635. }
  636.  
  637.  
  638. static int
  639. dlinforeq(int fd, char *ebuf)
  640. {
  641.     dl_info_req_t req;
  642.  
  643.     req.dl_primitive = DL_INFO_REQ;
  644.  
  645.     return (send_request(fd, (char *)&req, sizeof(req), "info", ebuf));
  646. }
  647.  
  648. static int
  649. dlinfoack(int fd, char *bufp, char *ebuf)
  650. {
  651.  
  652.     return (recv_ack(fd, DL_INFO_ACK_SIZE, "info", bufp, ebuf));
  653. }
  654.  
  655. #ifdef HAVE_SYS_BUFMOD_H
  656. static int
  657. strioctl(int fd, int cmd, int len, char *dp)
  658. {
  659.     struct strioctl str;
  660.     int rc;
  661.  
  662.     str.ic_cmd = cmd;
  663.     str.ic_timout = -1;
  664.     str.ic_len = len;
  665.     str.ic_dp = dp;
  666.     rc = ioctl(fd, I_STR, &str);
  667.  
  668.     if (rc < 0)
  669.         return (rc);
  670.     else
  671.         return (str.ic_len);
  672. }
  673. #endif
  674.  
  675. #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
  676. static char *
  677. get_release(bpf_u_int32 *majorp, bpf_u_int32 *minorp, bpf_u_int32 *microp)
  678. {
  679.     char *cp;
  680.     static char buf[32];
  681.  
  682.     *majorp = 0;
  683.     *minorp = 0;
  684.     *microp = 0;
  685.     if (sysinfo(SI_RELEASE, buf, sizeof(buf)) < 0)
  686.         return ("?");
  687.     cp = buf;
  688.     if (!isdigit(*cp))
  689.         return (buf);
  690.     *majorp = strtol(cp, &cp, 10);
  691.     if (*cp++ != '.')
  692.         return (buf);
  693.     *minorp =  strtol(cp, &cp, 10);
  694.     if (*cp++ != '.')
  695.         return (buf);
  696.     *microp =  strtol(cp, &cp, 10);
  697.     return (buf);
  698. }
  699. #endif
  700.  
  701. #ifdef DL_HP_PPA_ACK_OBS
  702. /*
  703.  * Under HP-UX 10, we can ask for the ppa
  704.  */
  705.  
  706.  
  707. /* Determine ppa number that specifies ifname */
  708. static int
  709. get_dlpi_ppa(register int fd, register const char *device, register int unit,
  710.     register char *ebuf)
  711. {
  712.     register dl_hp_ppa_ack_t *ap;
  713.     register dl_hp_ppa_info_t *ip;
  714.     register int i;
  715.     register u_long majdev;
  716.     dl_hp_ppa_req_t    req;
  717.     struct stat statbuf;
  718.     bpf_u_int32 buf[MAXDLBUF];
  719.  
  720.     if (stat(device, &statbuf) < 0) {
  721.         sprintf(ebuf, "stat: %s: %s", device, pcap_strerror(errno));
  722.         return (-1);
  723.     }
  724.     majdev = major(statbuf.st_rdev);
  725.  
  726.     memset((char *)&req, 0, sizeof(req));
  727.     req.dl_primitive = DL_HP_PPA_REQ;
  728.  
  729.     memset((char *)buf, 0, sizeof(buf));
  730.     if (send_request(fd, (char *)&req, sizeof(req), "hpppa", ebuf) < 0 ||
  731.         recv_ack(fd, DL_HP_PPA_ACK_SIZE, "hpppa", (char *)buf, ebuf) < 0)
  732.         return (-1);
  733.  
  734.     ap = (dl_hp_ppa_ack_t *)buf;
  735.     ip = (dl_hp_ppa_info_t *)((u_char *)ap + ap->dl_offset);
  736.  
  737.         for(i = 0; i < ap->dl_count; i++) {
  738.                 if (ip->dl_mjr_num == majdev && ip->dl_instance_num == unit)
  739.                         break;
  740.  
  741.                 ip = (dl_hp_ppa_info_t *)((u_char *)ip + ip->dl_next_offset);
  742.         }
  743.         if (i == ap->dl_count) {
  744.                 sprintf(ebuf, "can't find PPA for %s", device);
  745.         return (-1);
  746.         }
  747.         if (ip->dl_hdw_state == HDW_DEAD) {
  748.                 sprintf(ebuf, "%s: hardware state: DOWN\n", device);
  749.         return (-1);
  750.         }
  751.         return ((int)ip->dl_ppa);
  752. }
  753. #endif
  754.  
  755. #ifdef HAVE_HPUX9
  756. /*
  757.  * Under HP-UX 9, there is no good way to determine the ppa.
  758.  * So punt and read it from /dev/kmem.
  759.  */
  760. static struct nlist nl[] = {
  761. #define NL_IFNET 0
  762.     { "ifnet" },
  763.     { "" }
  764. };
  765.  
  766. static char path_vmunix[] = "/hp-ux";
  767.  
  768. /* Determine ppa number that specifies ifname */
  769. static int
  770. get_dlpi_ppa(register int fd, register const char *ifname, register int unit,
  771.     register char *ebuf)
  772. {
  773.     register const char *cp;
  774.     register int kd;
  775.     void *addr;
  776.     struct ifnet ifnet;
  777.     char if_name[sizeof(ifnet.if_name)], tifname[32];
  778.  
  779.     cp = strrchr(ifname, '/');
  780.     if (cp != NULL)
  781.         ifname = cp + 1;
  782.     if (nlist(path_vmunix, &nl) < 0) {
  783.         sprintf(ebuf, "nlist %s failed", path_vmunix);
  784.         return (-1);
  785.     }
  786.     if (nl[NL_IFNET].n_value == 0) {
  787.         sprintf(ebuf, "could't find %s kernel symbol",
  788.             nl[NL_IFNET].n_name);
  789.         return (-1);
  790.     }
  791.     kd = open("/dev/kmem", O_RDONLY);
  792.     if (kd < 0) {
  793.         sprintf(ebuf, "kmem open: %s", pcap_strerror(errno));
  794.         return (-1);
  795.     }
  796.     if (dlpi_kread(kd, nl[NL_IFNET].n_value,
  797.         &addr, sizeof(addr), ebuf) < 0) {
  798.         close(kd);
  799.         return (-1);
  800.     }
  801.     for (; addr != NULL; addr = ifnet.if_next) {
  802.         if (dlpi_kread(kd, (off_t)addr,
  803.             &ifnet, sizeof(ifnet), ebuf) < 0 ||
  804.             dlpi_kread(kd, (off_t)ifnet.if_name,
  805.             if_name, sizeof(if_name), ebuf) < 0) {
  806.             (void)close(kd);
  807.             return (-1);
  808.         }
  809.         sprintf(tifname, "%.*s%d",
  810.             (int)sizeof(if_name), if_name, ifnet.if_unit);
  811.         if (strcmp(tifname, ifname) == 0)
  812.             return (ifnet.if_index);
  813.     }
  814.  
  815.     sprintf(ebuf, "Can't find %s", ifname);
  816.     return (-1);
  817. }
  818.  
  819. static int
  820. dlpi_kread(register int fd, register off_t addr,
  821.     register void *buf, register u_int len, register char *ebuf)
  822. {
  823.     register int cc;
  824.  
  825.     if (lseek(fd, addr, L_SET) < 0) {
  826.         sprintf(ebuf, "lseek: %s", pcap_strerror(errno));
  827.         return (-1);
  828.     }
  829.     cc = read(fd, buf, len);
  830.     if (cc < 0) {
  831.         sprintf(ebuf, "read: %s", pcap_strerror(errno));
  832.         return (-1);
  833.     } else if (cc != len) {
  834.         sprintf(ebuf, "short read (%d != %d)", cc, len);
  835.         return (-1);
  836.     }
  837.     return (cc);
  838. }
  839. #endif
  840.