18cf6c252SPaul Traina /* 2a4b5b39fSBill Fenner * Copyright (c) 1993, 1994, 1995, 1996, 1997 38cf6c252SPaul Traina * The Regents of the University of California. All rights reserved. 48cf6c252SPaul Traina * 58cf6c252SPaul Traina * Redistribution and use in source and binary forms, with or without 68cf6c252SPaul Traina * modification, are permitted provided that: (1) source code distributions 78cf6c252SPaul Traina * retain the above copyright notice and this paragraph in its entirety, (2) 88cf6c252SPaul Traina * distributions including binary code include the above copyright notice and 98cf6c252SPaul Traina * this paragraph in its entirety in the documentation or other materials 108cf6c252SPaul Traina * provided with the distribution, and (3) all advertising materials mentioning 118cf6c252SPaul Traina * features or use of this software display the following acknowledgement: 128cf6c252SPaul Traina * ``This product includes software developed by the University of California, 138cf6c252SPaul Traina * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 148cf6c252SPaul Traina * the University nor the names of its contributors may be used to endorse 158cf6c252SPaul Traina * or promote products derived from this software without specific prior 168cf6c252SPaul Traina * written permission. 178cf6c252SPaul Traina * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 188cf6c252SPaul Traina * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 198cf6c252SPaul Traina * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 208cf6c252SPaul Traina * 218cf6c252SPaul Traina * This code contributed by Atanu Ghosh (atanu@cs.ucl.ac.uk), 2204fb2745SSam Leffler * University College London, and subsequently modified by 23ee2dd488SSam Leffler * Guy Harris (guy@alum.mit.edu), Mark Pizzolato 24ee2dd488SSam Leffler * <List-tcpdump-workers@subscriptions.pizzolato.net>, 25ee2dd488SSam Leffler * and Mark C. Brown (mbrown@hp.com). 268cf6c252SPaul Traina */ 278cf6c252SPaul Traina 288cf6c252SPaul Traina /* 29feb4ecdbSBruce M Simpson * Packet capture routine for DLPI under SunOS 5, HP-UX 9/10/11, and AIX. 308cf6c252SPaul Traina * 318cf6c252SPaul Traina * Notes: 328cf6c252SPaul Traina * 3304fb2745SSam Leffler * - The DLIOCRAW ioctl() is specific to SunOS. 348cf6c252SPaul Traina * 358cf6c252SPaul Traina * - There is a bug in bufmod(7) such that setting the snapshot 368cf6c252SPaul Traina * length results in data being left of the front of the packet. 378cf6c252SPaul Traina * 388cf6c252SPaul Traina * - It might be desirable to use pfmod(7) to filter packets in the 39feb4ecdbSBruce M Simpson * kernel when possible. 4004fb2745SSam Leffler * 41ee2dd488SSam Leffler * - An older version of the HP-UX DLPI Programmer's Guide, which 42ee2dd488SSam Leffler * I think was advertised as the 10.20 version, used to be available 4304fb2745SSam Leffler * at 4404fb2745SSam Leffler * 4504fb2745SSam Leffler * http://docs.hp.com/hpux/onlinedocs/B2355-90093/B2355-90093.html 4604fb2745SSam Leffler * 4704fb2745SSam Leffler * but is no longer available; it can still be found at 4804fb2745SSam Leffler * 4904fb2745SSam Leffler * http://h21007.www2.hp.com/dspp/files/unprotected/Drivers/Docs/Refs/B2355-90093.pdf 5004fb2745SSam Leffler * 5104fb2745SSam Leffler * in PDF form. 5204fb2745SSam Leffler * 53ee2dd488SSam Leffler * - The HP-UX 10.x, 11.0, and 11i v1.6 version of the HP-UX DLPI 54ee2dd488SSam Leffler * Programmer's Guide, which I think was once advertised as the 55ee2dd488SSam Leffler * 11.00 version is available at 5604fb2745SSam Leffler * 57ee2dd488SSam Leffler * http://docs.hp.com/en/B2355-90139/index.html 5804fb2745SSam Leffler * 59ee2dd488SSam Leffler * - The HP-UX 11i v2 version of the HP-UX DLPI Programmer's Guide 60ee2dd488SSam Leffler * is available at 6104fb2745SSam Leffler * 62ee2dd488SSam Leffler * http://docs.hp.com/en/B2355-90871/index.html 6304fb2745SSam Leffler * 64ee2dd488SSam Leffler * - All of the HP documents describe raw-mode services, which are 65ee2dd488SSam Leffler * what we use if DL_HP_RAWDLS is defined. XXX - we use __hpux 66ee2dd488SSam Leffler * in some places to test for HP-UX, but use DL_HP_RAWDLS in 67ee2dd488SSam Leffler * other places; do we support any versions of HP-UX without 68ee2dd488SSam Leffler * DL_HP_RAWDLS? 698cf6c252SPaul Traina */ 708cf6c252SPaul Traina 713052b236SBill Fenner #ifndef lint 72feb4ecdbSBruce M Simpson static const char rcsid[] _U_ = 73ee2dd488SSam Leffler "@(#) $Header: /tcpdump/master/libpcap/pcap-dlpi.c,v 1.108.2.5 2005/05/03 18:54:35 guy Exp $ (LBL)"; 74dc2c7305SBill Fenner #endif 75dc2c7305SBill Fenner 76dc2c7305SBill Fenner #ifdef HAVE_CONFIG_H 77dc2c7305SBill Fenner #include "config.h" 783052b236SBill Fenner #endif 793052b236SBill Fenner 808cf6c252SPaul Traina #include <sys/types.h> 818cf6c252SPaul Traina #include <sys/time.h> 828cf6c252SPaul Traina #ifdef HAVE_SYS_BUFMOD_H 838cf6c252SPaul Traina #include <sys/bufmod.h> 848cf6c252SPaul Traina #endif 858cf6c252SPaul Traina #include <sys/dlpi.h> 868cf6c252SPaul Traina #ifdef HAVE_SYS_DLPI_EXT_H 878cf6c252SPaul Traina #include <sys/dlpi_ext.h> 888cf6c252SPaul Traina #endif 898cf6c252SPaul Traina #ifdef HAVE_HPUX9 908cf6c252SPaul Traina #include <sys/socket.h> 918cf6c252SPaul Traina #endif 9204fb2745SSam Leffler #ifdef DL_HP_PPA_REQ 938cf6c252SPaul Traina #include <sys/stat.h> 948cf6c252SPaul Traina #endif 958cf6c252SPaul Traina #include <sys/stream.h> 968cf6c252SPaul Traina #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H) 978cf6c252SPaul Traina #include <sys/systeminfo.h> 988cf6c252SPaul Traina #endif 998cf6c252SPaul Traina 1008cf6c252SPaul Traina #ifdef HAVE_HPUX9 1018cf6c252SPaul Traina #include <net/if.h> 1028cf6c252SPaul Traina #endif 1038cf6c252SPaul Traina 1048cf6c252SPaul Traina #include <ctype.h> 1058cf6c252SPaul Traina #ifdef HAVE_HPUX9 1068cf6c252SPaul Traina #include <nlist.h> 1078cf6c252SPaul Traina #endif 1088cf6c252SPaul Traina #include <errno.h> 1098cf6c252SPaul Traina #include <fcntl.h> 1108cf6c252SPaul Traina #include <memory.h> 1118cf6c252SPaul Traina #include <stdio.h> 1128cf6c252SPaul Traina #include <stdlib.h> 1138cf6c252SPaul Traina #include <string.h> 1148cf6c252SPaul Traina #include <stropts.h> 1158cf6c252SPaul Traina #include <unistd.h> 1168cf6c252SPaul Traina 11704fb2745SSam Leffler #ifdef HAVE_LIMITS_H 11804fb2745SSam Leffler #include <limits.h> 11904fb2745SSam Leffler #else 12004fb2745SSam Leffler #define INT_MAX 2147483647 12104fb2745SSam Leffler #endif 12204fb2745SSam Leffler 1238cf6c252SPaul Traina #include "pcap-int.h" 1248cf6c252SPaul Traina 1258cf6c252SPaul Traina #ifdef HAVE_OS_PROTO_H 1268cf6c252SPaul Traina #include "os-proto.h" 1278cf6c252SPaul Traina #endif 1288cf6c252SPaul Traina 1298cf6c252SPaul Traina #ifndef PCAP_DEV_PREFIX 1300a94d38fSBill Fenner #ifdef _AIX 1310a94d38fSBill Fenner #define PCAP_DEV_PREFIX "/dev/dlpi" 1320a94d38fSBill Fenner #else 1338cf6c252SPaul Traina #define PCAP_DEV_PREFIX "/dev" 1348cf6c252SPaul Traina #endif 1350a94d38fSBill Fenner #endif 1368cf6c252SPaul Traina 1378cf6c252SPaul Traina #define MAXDLBUF 8192 1388cf6c252SPaul Traina 139feb4ecdbSBruce M Simpson #ifdef HAVE_SYS_BUFMOD_H 140feb4ecdbSBruce M Simpson 141feb4ecdbSBruce M Simpson /* 142feb4ecdbSBruce M Simpson * Size of a bufmod chunk to pass upstream; that appears to be the biggest 143feb4ecdbSBruce M Simpson * value to which you can set it, and setting it to that value (which 144feb4ecdbSBruce M Simpson * is bigger than what appears to be the Solaris default of 8192) 145feb4ecdbSBruce M Simpson * reduces the number of packet drops. 146feb4ecdbSBruce M Simpson */ 147feb4ecdbSBruce M Simpson #define CHUNKSIZE 65536 148feb4ecdbSBruce M Simpson 149feb4ecdbSBruce M Simpson /* 150feb4ecdbSBruce M Simpson * Size of the buffer to allocate for packet data we read; it must be 151feb4ecdbSBruce M Simpson * large enough to hold a chunk. 152feb4ecdbSBruce M Simpson */ 153feb4ecdbSBruce M Simpson #define PKTBUFSIZE CHUNKSIZE 154feb4ecdbSBruce M Simpson 155feb4ecdbSBruce M Simpson #else /* HAVE_SYS_BUFMOD_H */ 156feb4ecdbSBruce M Simpson 157feb4ecdbSBruce M Simpson /* 158feb4ecdbSBruce M Simpson * Size of the buffer to allocate for packet data we read; this is 159feb4ecdbSBruce M Simpson * what the value used to be - there's no particular reason why it 160feb4ecdbSBruce M Simpson * should be tied to MAXDLBUF, but we'll leave it as this for now. 161feb4ecdbSBruce M Simpson */ 162feb4ecdbSBruce M Simpson #define PKTBUFSIZE (MAXDLBUF * sizeof(bpf_u_int32)) 163feb4ecdbSBruce M Simpson 164feb4ecdbSBruce M Simpson #endif 165feb4ecdbSBruce M Simpson 1668cf6c252SPaul Traina /* Forwards */ 1670a94d38fSBill Fenner static char *split_dname(char *, int *, char *); 16804fb2745SSam Leffler static int dl_doattach(int, int, char *); 169ee2dd488SSam Leffler #ifdef DL_HP_RAWDLS 170ee2dd488SSam Leffler static int dl_dohpuxbind(int, char *); 171ee2dd488SSam Leffler #endif 1728cf6c252SPaul Traina static int dlattachreq(int, bpf_u_int32, char *); 1738cf6c252SPaul Traina static int dlbindreq(int, bpf_u_int32, char *); 174ee2dd488SSam Leffler static int dlbindack(int, char *, char *, int *); 17504fb2745SSam Leffler static int dlpromisconreq(int, bpf_u_int32, char *); 1768cf6c252SPaul Traina static int dlokack(int, const char *, char *, char *); 17704fb2745SSam Leffler static int dlinforeq(int, char *); 17804fb2745SSam Leffler static int dlinfoack(int, char *, char *); 17904fb2745SSam Leffler #ifdef DL_HP_RAWDLS 18004fb2745SSam Leffler static int dlrawdatareq(int, const u_char *, int); 18104fb2745SSam Leffler #endif 182ee2dd488SSam Leffler static int recv_ack(int, int, const char *, char *, char *, int *); 1830a94d38fSBill Fenner static char *dlstrerror(bpf_u_int32); 1840a94d38fSBill Fenner static char *dlprim(bpf_u_int32); 1858cf6c252SPaul Traina #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H) 1868cf6c252SPaul Traina static char *get_release(bpf_u_int32 *, bpf_u_int32 *, bpf_u_int32 *); 1878cf6c252SPaul Traina #endif 1888cf6c252SPaul Traina static int send_request(int, char *, int, char *, char *); 1898cf6c252SPaul Traina #ifdef HAVE_SYS_BUFMOD_H 1908cf6c252SPaul Traina static int strioctl(int, int, int, char *); 1918cf6c252SPaul Traina #endif 1928cf6c252SPaul Traina #ifdef HAVE_HPUX9 1938cf6c252SPaul Traina static int dlpi_kread(int, off_t, void *, u_int, char *); 1948cf6c252SPaul Traina #endif 1958cf6c252SPaul Traina #ifdef HAVE_DEV_DLPI 1968cf6c252SPaul Traina static int get_dlpi_ppa(int, const char *, int, char *); 1978cf6c252SPaul Traina #endif 1988cf6c252SPaul Traina 199feb4ecdbSBruce M Simpson static int 200feb4ecdbSBruce M Simpson pcap_stats_dlpi(pcap_t *p, struct pcap_stat *ps) 2018cf6c252SPaul Traina { 2028cf6c252SPaul Traina 2030a94d38fSBill Fenner /* 2040a94d38fSBill Fenner * "ps_recv" counts packets handed to the filter, not packets 2050a94d38fSBill Fenner * that passed the filter. As filtering is done in userland, 20604fb2745SSam Leffler * this would not include packets dropped because we ran out 20704fb2745SSam Leffler * of buffer space; in order to make this more like other 20804fb2745SSam Leffler * platforms (Linux 2.4 and later, BSDs with BPF), where the 20904fb2745SSam Leffler * "packets received" count includes packets received but dropped 21004fb2745SSam Leffler * due to running out of buffer space, and to keep from confusing 21104fb2745SSam Leffler * applications that, for example, compute packet drop percentages, 21204fb2745SSam Leffler * we also make it count packets dropped by "bufmod" (otherwise we 21304fb2745SSam Leffler * might run the risk of the packet drop count being bigger than 21404fb2745SSam Leffler * the received-packet count). 2150a94d38fSBill Fenner * 21604fb2745SSam Leffler * "ps_drop" counts packets dropped by "bufmod" because of 21704fb2745SSam Leffler * flow control requirements or resource exhaustion; it doesn't 21804fb2745SSam Leffler * count packets dropped by the interface driver, or packets 21904fb2745SSam Leffler * dropped upstream. As filtering is done in userland, it counts 22004fb2745SSam Leffler * packets regardless of whether they would've passed the filter. 2210a94d38fSBill Fenner * 2220a94d38fSBill Fenner * These statistics don't include packets not yet read from 2230a94d38fSBill Fenner * the kernel by libpcap, but they may include packets not 2240a94d38fSBill Fenner * yet read from libpcap by the application. 2250a94d38fSBill Fenner */ 2268cf6c252SPaul Traina *ps = p->md.stat; 22704fb2745SSam Leffler 22804fb2745SSam Leffler /* 22904fb2745SSam Leffler * Add in the drop count, as per the above comment. 23004fb2745SSam Leffler */ 23104fb2745SSam Leffler ps->ps_recv += ps->ps_drop; 2328cf6c252SPaul Traina return (0); 2338cf6c252SPaul Traina } 2348cf6c252SPaul Traina 2358cf6c252SPaul Traina /* XXX Needed by HP-UX (at least) */ 2368cf6c252SPaul Traina static bpf_u_int32 ctlbuf[MAXDLBUF]; 2378cf6c252SPaul Traina static struct strbuf ctl = { 2388cf6c252SPaul Traina MAXDLBUF, 2398cf6c252SPaul Traina 0, 2408cf6c252SPaul Traina (char *)ctlbuf 2418cf6c252SPaul Traina }; 2428cf6c252SPaul Traina 243feb4ecdbSBruce M Simpson static int 244feb4ecdbSBruce M Simpson pcap_read_dlpi(pcap_t *p, int cnt, pcap_handler callback, u_char *user) 2458cf6c252SPaul Traina { 2468cf6c252SPaul Traina register int cc, n, caplen, origlen; 2478cf6c252SPaul Traina register u_char *bp, *ep, *pk; 2488cf6c252SPaul Traina register struct bpf_insn *fcode; 2498cf6c252SPaul Traina #ifdef HAVE_SYS_BUFMOD_H 2508cf6c252SPaul Traina register struct sb_hdr *sbp; 2518cf6c252SPaul Traina #ifdef LBL_ALIGN 2528cf6c252SPaul Traina struct sb_hdr sbhdr; 2538cf6c252SPaul Traina #endif 2548cf6c252SPaul Traina #endif 2558cf6c252SPaul Traina int flags; 2568cf6c252SPaul Traina struct strbuf data; 2578cf6c252SPaul Traina struct pcap_pkthdr pkthdr; 2588cf6c252SPaul Traina 2598cf6c252SPaul Traina flags = 0; 2608cf6c252SPaul Traina cc = p->cc; 2618cf6c252SPaul Traina if (cc == 0) { 2628cf6c252SPaul Traina data.buf = (char *)p->buffer + p->offset; 263feb4ecdbSBruce M Simpson data.maxlen = p->bufsize; 2648cf6c252SPaul Traina data.len = 0; 2658cf6c252SPaul Traina do { 266feb4ecdbSBruce M Simpson /* 267feb4ecdbSBruce M Simpson * Has "pcap_breakloop()" been called? 268feb4ecdbSBruce M Simpson */ 269feb4ecdbSBruce M Simpson if (p->break_loop) { 270feb4ecdbSBruce M Simpson /* 271feb4ecdbSBruce M Simpson * Yes - clear the flag that indicates 272feb4ecdbSBruce M Simpson * that it has, and return -2 to 273feb4ecdbSBruce M Simpson * indicate that we were told to 274feb4ecdbSBruce M Simpson * break out of the loop. 275feb4ecdbSBruce M Simpson */ 276feb4ecdbSBruce M Simpson p->break_loop = 0; 277feb4ecdbSBruce M Simpson return (-2); 278feb4ecdbSBruce M Simpson } 27904fb2745SSam Leffler /* 28004fb2745SSam Leffler * XXX - check for the DLPI primitive, which 28104fb2745SSam Leffler * would be DL_HP_RAWDATA_IND on HP-UX 28204fb2745SSam Leffler * if we're in raw mode? 28304fb2745SSam Leffler */ 2848cf6c252SPaul Traina if (getmsg(p->fd, &ctl, &data, &flags) < 0) { 2858cf6c252SPaul Traina /* Don't choke when we get ptraced */ 28604fb2745SSam Leffler switch (errno) { 28704fb2745SSam Leffler 28804fb2745SSam Leffler case EINTR: 2898cf6c252SPaul Traina cc = 0; 2908cf6c252SPaul Traina continue; 29104fb2745SSam Leffler 29204fb2745SSam Leffler case EAGAIN: 29304fb2745SSam Leffler return (0); 2948cf6c252SPaul Traina } 295dc2c7305SBill Fenner strlcpy(p->errbuf, pcap_strerror(errno), 296dc2c7305SBill Fenner sizeof(p->errbuf)); 2978cf6c252SPaul Traina return (-1); 2988cf6c252SPaul Traina } 2998cf6c252SPaul Traina cc = data.len; 3008cf6c252SPaul Traina } while (cc == 0); 3018cf6c252SPaul Traina bp = p->buffer + p->offset; 3028cf6c252SPaul Traina } else 3038cf6c252SPaul Traina bp = p->bp; 3048cf6c252SPaul Traina 3058cf6c252SPaul Traina /* Loop through packets */ 3068cf6c252SPaul Traina fcode = p->fcode.bf_insns; 3078cf6c252SPaul Traina ep = bp + cc; 3088cf6c252SPaul Traina n = 0; 3098cf6c252SPaul Traina #ifdef HAVE_SYS_BUFMOD_H 3108cf6c252SPaul Traina while (bp < ep) { 311feb4ecdbSBruce M Simpson /* 312feb4ecdbSBruce M Simpson * Has "pcap_breakloop()" been called? 313feb4ecdbSBruce M Simpson * If so, return immediately - if we haven't read any 314feb4ecdbSBruce M Simpson * packets, clear the flag and return -2 to indicate 315feb4ecdbSBruce M Simpson * that we were told to break out of the loop, otherwise 316feb4ecdbSBruce M Simpson * leave the flag set, so that the *next* call will break 317feb4ecdbSBruce M Simpson * out of the loop without having read any packets, and 318feb4ecdbSBruce M Simpson * return the number of packets we've processed so far. 319feb4ecdbSBruce M Simpson */ 320feb4ecdbSBruce M Simpson if (p->break_loop) { 321feb4ecdbSBruce M Simpson if (n == 0) { 322feb4ecdbSBruce M Simpson p->break_loop = 0; 323feb4ecdbSBruce M Simpson return (-2); 324feb4ecdbSBruce M Simpson } else { 325feb4ecdbSBruce M Simpson p->bp = bp; 326feb4ecdbSBruce M Simpson p->cc = ep - bp; 327feb4ecdbSBruce M Simpson return (n); 328feb4ecdbSBruce M Simpson } 329feb4ecdbSBruce M Simpson } 3308cf6c252SPaul Traina #ifdef LBL_ALIGN 3318cf6c252SPaul Traina if ((long)bp & 3) { 3328cf6c252SPaul Traina sbp = &sbhdr; 3338cf6c252SPaul Traina memcpy(sbp, bp, sizeof(*sbp)); 3348cf6c252SPaul Traina } else 3358cf6c252SPaul Traina #endif 3368cf6c252SPaul Traina sbp = (struct sb_hdr *)bp; 337feb4ecdbSBruce M Simpson p->md.stat.ps_drop = sbp->sbh_drops; 3388cf6c252SPaul Traina pk = bp + sizeof(*sbp); 3398cf6c252SPaul Traina bp += sbp->sbh_totlen; 3408cf6c252SPaul Traina origlen = sbp->sbh_origlen; 3418cf6c252SPaul Traina caplen = sbp->sbh_msglen; 3428cf6c252SPaul Traina #else 3438cf6c252SPaul Traina origlen = cc; 3448cf6c252SPaul Traina caplen = min(p->snapshot, cc); 3458cf6c252SPaul Traina pk = bp; 3468cf6c252SPaul Traina bp += caplen; 3478cf6c252SPaul Traina #endif 3488cf6c252SPaul Traina ++p->md.stat.ps_recv; 3498cf6c252SPaul Traina if (bpf_filter(fcode, pk, origlen, caplen)) { 3508cf6c252SPaul Traina #ifdef HAVE_SYS_BUFMOD_H 351feb4ecdbSBruce M Simpson pkthdr.ts.tv_sec = sbp->sbh_timestamp.tv_sec; 352feb4ecdbSBruce M Simpson pkthdr.ts.tv_usec = sbp->sbh_timestamp.tv_usec; 3538cf6c252SPaul Traina #else 3548cf6c252SPaul Traina (void)gettimeofday(&pkthdr.ts, NULL); 3558cf6c252SPaul Traina #endif 3568cf6c252SPaul Traina pkthdr.len = origlen; 3578cf6c252SPaul Traina pkthdr.caplen = caplen; 3588cf6c252SPaul Traina /* Insure caplen does not exceed snapshot */ 3598cf6c252SPaul Traina if (pkthdr.caplen > p->snapshot) 3608cf6c252SPaul Traina pkthdr.caplen = p->snapshot; 3618cf6c252SPaul Traina (*callback)(user, &pkthdr, pk); 3628cf6c252SPaul Traina if (++n >= cnt && cnt >= 0) { 3638cf6c252SPaul Traina p->cc = ep - bp; 3648cf6c252SPaul Traina p->bp = bp; 3658cf6c252SPaul Traina return (n); 3668cf6c252SPaul Traina } 3678cf6c252SPaul Traina } 3688cf6c252SPaul Traina #ifdef HAVE_SYS_BUFMOD_H 3698cf6c252SPaul Traina } 3708cf6c252SPaul Traina #endif 3718cf6c252SPaul Traina p->cc = 0; 3728cf6c252SPaul Traina return (n); 3738cf6c252SPaul Traina } 3748cf6c252SPaul Traina 37504fb2745SSam Leffler static int 37604fb2745SSam Leffler pcap_inject_dlpi(pcap_t *p, const void *buf, size_t size) 37704fb2745SSam Leffler { 37804fb2745SSam Leffler int ret; 37904fb2745SSam Leffler 38004fb2745SSam Leffler #if defined(DLIOCRAW) 38104fb2745SSam Leffler ret = write(p->fd, buf, size); 38204fb2745SSam Leffler if (ret == -1) { 38304fb2745SSam Leffler snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", 38404fb2745SSam Leffler pcap_strerror(errno)); 38504fb2745SSam Leffler return (-1); 38604fb2745SSam Leffler } 38704fb2745SSam Leffler #elif defined(DL_HP_RAWDLS) 38804fb2745SSam Leffler if (p->send_fd < 0) { 38904fb2745SSam Leffler snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 39004fb2745SSam Leffler "send: Output FD couldn't be opened"); 39104fb2745SSam Leffler return (-1); 39204fb2745SSam Leffler } 39304fb2745SSam Leffler ret = dlrawdatareq(p->send_fd, buf, size); 39404fb2745SSam Leffler if (ret == -1) { 39504fb2745SSam Leffler snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", 39604fb2745SSam Leffler pcap_strerror(errno)); 39704fb2745SSam Leffler return (-1); 39804fb2745SSam Leffler } 39904fb2745SSam Leffler #else /* no raw mode */ 40004fb2745SSam Leffler /* 40104fb2745SSam Leffler * XXX - this is a pain, because you might have to extract 40204fb2745SSam Leffler * the address from the packet and use it in a DL_UNITDATA_REQ 40304fb2745SSam Leffler * request. That would be dependent on the link-layer type. 40404fb2745SSam Leffler * 40504fb2745SSam Leffler * I also don't know what SAP you'd have to bind the descriptor 40604fb2745SSam Leffler * to, or whether you'd need separate "receive" and "send" FDs, 40704fb2745SSam Leffler * nor do I know whether you'd need different bindings for 40804fb2745SSam Leffler * D/I/X Ethernet and 802.3, or for {FDDI,Token Ring} plus 40904fb2745SSam Leffler * 802.2 and {FDDI,Token Ring} plus 802.2 plus SNAP. 41004fb2745SSam Leffler * 41104fb2745SSam Leffler * So, for now, we just return a "you can't send" indication, 41204fb2745SSam Leffler * and leave it up to somebody with a DLPI-based system lacking 41304fb2745SSam Leffler * both DLIOCRAW and DL_HP_RAWDLS to supply code to implement 41404fb2745SSam Leffler * packet transmission on that system. If they do, they should 41504fb2745SSam Leffler * send it to us - but should not send us code that assumes 41604fb2745SSam Leffler * Ethernet; if the code doesn't work on non-Ethernet interfaces, 41704fb2745SSam Leffler * it should check "p->linktype" and reject the send request if 41804fb2745SSam Leffler * it's anything other than DLT_EN10MB. 41904fb2745SSam Leffler */ 42004fb2745SSam Leffler strlcpy(p->errbuf, "send: Not supported on this version of this OS", 42104fb2745SSam Leffler PCAP_ERRBUF_SIZE); 42204fb2745SSam Leffler ret = -1; 42304fb2745SSam Leffler #endif /* raw mode */ 42404fb2745SSam Leffler return (ret); 42504fb2745SSam Leffler } 42604fb2745SSam Leffler 427feb4ecdbSBruce M Simpson #ifndef DL_IPATM 428feb4ecdbSBruce M Simpson #define DL_IPATM 0x12 /* ATM Classical IP interface */ 429feb4ecdbSBruce M Simpson #endif 430feb4ecdbSBruce M Simpson 431feb4ecdbSBruce M Simpson #ifdef HAVE_SOLARIS 432feb4ecdbSBruce M Simpson /* 433feb4ecdbSBruce M Simpson * For SunATM. 434feb4ecdbSBruce M Simpson */ 435feb4ecdbSBruce M Simpson #ifndef A_GET_UNITS 436feb4ecdbSBruce M Simpson #define A_GET_UNITS (('A'<<8)|118) 437feb4ecdbSBruce M Simpson #endif /* A_GET_UNITS */ 438feb4ecdbSBruce M Simpson #ifndef A_PROMISCON_REQ 439feb4ecdbSBruce M Simpson #define A_PROMISCON_REQ (('A'<<8)|121) 440feb4ecdbSBruce M Simpson #endif /* A_PROMISCON_REQ */ 441feb4ecdbSBruce M Simpson #endif /* HAVE_SOLARIS */ 442feb4ecdbSBruce M Simpson 443feb4ecdbSBruce M Simpson static void 444feb4ecdbSBruce M Simpson pcap_close_dlpi(pcap_t *p) 445feb4ecdbSBruce M Simpson { 44604fb2745SSam Leffler pcap_close_common(p); 44704fb2745SSam Leffler if (p->send_fd >= 0) 44804fb2745SSam Leffler close(p->send_fd); 449feb4ecdbSBruce M Simpson } 450feb4ecdbSBruce M Simpson 4518cf6c252SPaul Traina pcap_t * 452feb4ecdbSBruce M Simpson pcap_open_live(const char *device, int snaplen, int promisc, int to_ms, 453feb4ecdbSBruce M Simpson char *ebuf) 4548cf6c252SPaul Traina { 4558cf6c252SPaul Traina register char *cp; 4568cf6c252SPaul Traina register pcap_t *p; 4570a94d38fSBill Fenner int ppa; 458feb4ecdbSBruce M Simpson #ifdef HAVE_SOLARIS 459feb4ecdbSBruce M Simpson int isatm = 0; 460feb4ecdbSBruce M Simpson #endif 4618cf6c252SPaul Traina register dl_info_ack_t *infop; 4628cf6c252SPaul Traina #ifdef HAVE_SYS_BUFMOD_H 463feb4ecdbSBruce M Simpson bpf_u_int32 ss, chunksize; 4648cf6c252SPaul Traina #ifdef HAVE_SOLARIS 4658cf6c252SPaul Traina register char *release; 4668cf6c252SPaul Traina bpf_u_int32 osmajor, osminor, osmicro; 4678cf6c252SPaul Traina #endif 4688cf6c252SPaul Traina #endif 4698cf6c252SPaul Traina bpf_u_int32 buf[MAXDLBUF]; 4708cf6c252SPaul Traina char dname[100]; 4718cf6c252SPaul Traina #ifndef HAVE_DEV_DLPI 4728cf6c252SPaul Traina char dname2[100]; 4738cf6c252SPaul Traina #endif 4748cf6c252SPaul Traina 4758cf6c252SPaul Traina p = (pcap_t *)malloc(sizeof(*p)); 4768cf6c252SPaul Traina if (p == NULL) { 477dc2c7305SBill Fenner strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); 4788cf6c252SPaul Traina return (NULL); 4798cf6c252SPaul Traina } 4808cf6c252SPaul Traina memset(p, 0, sizeof(*p)); 4810a94d38fSBill Fenner p->fd = -1; /* indicate that it hasn't been opened yet */ 48204fb2745SSam Leffler p->send_fd = -1; 4838cf6c252SPaul Traina 484dc2c7305SBill Fenner #ifdef HAVE_DEV_DLPI 485dc2c7305SBill Fenner /* 486dc2c7305SBill Fenner ** Remove any "/dev/" on the front of the device. 487dc2c7305SBill Fenner */ 488dc2c7305SBill Fenner cp = strrchr(device, '/'); 489dc2c7305SBill Fenner if (cp == NULL) 490feb4ecdbSBruce M Simpson strlcpy(dname, device, sizeof(dname)); 491dc2c7305SBill Fenner else 492feb4ecdbSBruce M Simpson strlcpy(dname, cp + 1, sizeof(dname)); 493dc2c7305SBill Fenner 494dc2c7305SBill Fenner /* 4950a94d38fSBill Fenner * Split the device name into a device type name and a unit number; 4960a94d38fSBill Fenner * chop off the unit number, so "dname" is just a device type name. 497dc2c7305SBill Fenner */ 4980a94d38fSBill Fenner cp = split_dname(dname, &ppa, ebuf); 4990a94d38fSBill Fenner if (cp == NULL) 500dc2c7305SBill Fenner goto bad; 501dc2c7305SBill Fenner *cp = '\0'; 502dc2c7305SBill Fenner 503dc2c7305SBill Fenner /* 504dc2c7305SBill Fenner * Use "/dev/dlpi" as the device. 505dc2c7305SBill Fenner * 506dc2c7305SBill Fenner * XXX - HP's DLPI Programmer's Guide for HP-UX 11.00 says that 507dc2c7305SBill Fenner * the "dl_mjr_num" field is for the "major number of interface 508dc2c7305SBill Fenner * driver"; that's the major of "/dev/dlpi" on the system on 509dc2c7305SBill Fenner * which I tried this, but there may be DLPI devices that 510dc2c7305SBill Fenner * use a different driver, in which case we may need to 511dc2c7305SBill Fenner * search "/dev" for the appropriate device with that major 512dc2c7305SBill Fenner * device number, rather than hardwiring "/dev/dlpi". 513dc2c7305SBill Fenner */ 514dc2c7305SBill Fenner cp = "/dev/dlpi"; 515dc2c7305SBill Fenner if ((p->fd = open(cp, O_RDWR)) < 0) { 516dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, 517dc2c7305SBill Fenner "%s: %s", cp, pcap_strerror(errno)); 518dc2c7305SBill Fenner goto bad; 519dc2c7305SBill Fenner } 520dc2c7305SBill Fenner 52104fb2745SSam Leffler #ifdef DL_HP_RAWDLS 52204fb2745SSam Leffler /* 52304fb2745SSam Leffler * XXX - HP-UX 10.20 and 11.xx don't appear to support sending and 524ee2dd488SSam Leffler * receiving packets on the same descriptor - you need separate 525ee2dd488SSam Leffler * descriptors for sending and receiving, bound to different SAPs. 52604fb2745SSam Leffler * 52704fb2745SSam Leffler * If the open fails, we just leave -1 in "p->send_fd" and reject 52804fb2745SSam Leffler * attempts to send packets, just as if, in pcap-bpf.c, we fail 52904fb2745SSam Leffler * to open the BPF device for reading and writing, we just try 53004fb2745SSam Leffler * to open it for reading only and, if that succeeds, just let 53104fb2745SSam Leffler * the send attempts fail. 53204fb2745SSam Leffler */ 53304fb2745SSam Leffler p->send_fd = open(cp, O_RDWR); 53404fb2745SSam Leffler #endif 53504fb2745SSam Leffler 536dc2c7305SBill Fenner /* 537dc2c7305SBill Fenner * Get a table of all PPAs for that device, and search that 538dc2c7305SBill Fenner * table for the specified device type name and unit number. 539dc2c7305SBill Fenner */ 540dc2c7305SBill Fenner ppa = get_dlpi_ppa(p->fd, dname, ppa, ebuf); 541dc2c7305SBill Fenner if (ppa < 0) 542dc2c7305SBill Fenner goto bad; 543dc2c7305SBill Fenner #else 5448cf6c252SPaul Traina /* 5450a94d38fSBill Fenner * If the device name begins with "/", assume it begins with 5460a94d38fSBill Fenner * the pathname of the directory containing the device to open; 5470a94d38fSBill Fenner * otherwise, concatenate the device directory name and the 5480a94d38fSBill Fenner * device name. 5490a94d38fSBill Fenner */ 5508cf6c252SPaul Traina if (*device == '/') 551dc2c7305SBill Fenner strlcpy(dname, device, sizeof(dname)); 5528cf6c252SPaul Traina else 553dc2c7305SBill Fenner snprintf(dname, sizeof(dname), "%s/%s", PCAP_DEV_PREFIX, 554dc2c7305SBill Fenner device); 555dc2c7305SBill Fenner 5560a94d38fSBill Fenner /* 557feb4ecdbSBruce M Simpson * Get the unit number, and a pointer to the end of the device 558feb4ecdbSBruce M Simpson * type name. 559feb4ecdbSBruce M Simpson */ 560feb4ecdbSBruce M Simpson cp = split_dname(dname, &ppa, ebuf); 561feb4ecdbSBruce M Simpson if (cp == NULL) 562feb4ecdbSBruce M Simpson goto bad; 563feb4ecdbSBruce M Simpson 564feb4ecdbSBruce M Simpson /* 5650a94d38fSBill Fenner * Make a copy of the device pathname, and then remove the unit 5660a94d38fSBill Fenner * number from the device pathname. 5670a94d38fSBill Fenner */ 5680a94d38fSBill Fenner strlcpy(dname2, dname, sizeof(dname)); 569feb4ecdbSBruce M Simpson *cp = '\0'; 5700a94d38fSBill Fenner 5718cf6c252SPaul Traina /* Try device without unit number */ 5728cf6c252SPaul Traina if ((p->fd = open(dname, O_RDWR)) < 0) { 5738cf6c252SPaul Traina if (errno != ENOENT) { 574dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s", dname, 575dc2c7305SBill Fenner pcap_strerror(errno)); 5768cf6c252SPaul Traina goto bad; 5778cf6c252SPaul Traina } 5788cf6c252SPaul Traina 5798cf6c252SPaul Traina /* Try again with unit number */ 5808cf6c252SPaul Traina if ((p->fd = open(dname2, O_RDWR)) < 0) { 58104fb2745SSam Leffler if (errno == ENOENT) { 58204fb2745SSam Leffler /* 58304fb2745SSam Leffler * We just report "No DLPI device found" 58404fb2745SSam Leffler * with the device name, so people don't 58504fb2745SSam Leffler * get confused and think, for example, 58604fb2745SSam Leffler * that if they can't capture on "lo0" 58704fb2745SSam Leffler * on Solaris the fix is to change libpcap 58804fb2745SSam Leffler * (or the application that uses it) to 58904fb2745SSam Leffler * look for something other than "/dev/lo0", 59004fb2745SSam Leffler * as the fix is to look for an operating 59104fb2745SSam Leffler * system other than Solaris - you just 59204fb2745SSam Leffler * *can't* capture on a loopback interface 59304fb2745SSam Leffler * on Solaris, the lack of a DLPI device 59404fb2745SSam Leffler * for the loopback interface is just a 59504fb2745SSam Leffler * symptom of that inability. 59604fb2745SSam Leffler */ 59704fb2745SSam Leffler snprintf(ebuf, PCAP_ERRBUF_SIZE, 59804fb2745SSam Leffler "%s: No DLPI device found", device); 59904fb2745SSam Leffler } else { 60004fb2745SSam Leffler snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s: %s", 60104fb2745SSam Leffler dname2, pcap_strerror(errno)); 60204fb2745SSam Leffler } 6038cf6c252SPaul Traina goto bad; 6048cf6c252SPaul Traina } 6058cf6c252SPaul Traina /* XXX Assume unit zero */ 6068cf6c252SPaul Traina ppa = 0; 6078cf6c252SPaul Traina } 6088cf6c252SPaul Traina #endif 6098cf6c252SPaul Traina 6108cf6c252SPaul Traina p->snapshot = snaplen; 6118cf6c252SPaul Traina 6128cf6c252SPaul Traina /* 6138cf6c252SPaul Traina ** Attach if "style 2" provider 6148cf6c252SPaul Traina */ 6158cf6c252SPaul Traina if (dlinforeq(p->fd, ebuf) < 0 || 6168cf6c252SPaul Traina dlinfoack(p->fd, (char *)buf, ebuf) < 0) 6178cf6c252SPaul Traina goto bad; 6188cf6c252SPaul Traina infop = &((union DL_primitives *)buf)->info_ack; 619feb4ecdbSBruce M Simpson #ifdef HAVE_SOLARIS 620feb4ecdbSBruce M Simpson if (infop->dl_mac_type == DL_IPATM) 621feb4ecdbSBruce M Simpson isatm = 1; 622feb4ecdbSBruce M Simpson #endif 62304fb2745SSam Leffler if (infop->dl_provider_style == DL_STYLE2) { 62404fb2745SSam Leffler if (dl_doattach(p->fd, ppa, ebuf) < 0) 6258cf6c252SPaul Traina goto bad; 62604fb2745SSam Leffler #ifdef DL_HP_RAWDLS 62704fb2745SSam Leffler if (p->send_fd >= 0) { 62804fb2745SSam Leffler if (dl_doattach(p->send_fd, ppa, ebuf) < 0) 62904fb2745SSam Leffler goto bad; 63004fb2745SSam Leffler } 63104fb2745SSam Leffler #endif 63204fb2745SSam Leffler } 63304fb2745SSam Leffler 6348cf6c252SPaul Traina /* 635ee2dd488SSam Leffler ** Bind (defer if using HP-UX 9 or HP-UX 10.20 or later, totally 636ee2dd488SSam Leffler ** skip if using SINIX) 6378cf6c252SPaul Traina */ 638ee2dd488SSam Leffler #if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20_OR_LATER) && !defined(sinix) 639dc2c7305SBill Fenner #ifdef _AIX 640ee2dd488SSam Leffler /* 641ee2dd488SSam Leffler ** AIX. 642ee2dd488SSam Leffler ** According to IBM's AIX Support Line, the dl_sap value 6430a94d38fSBill Fenner ** should not be less than 0x600 (1536) for standard Ethernet. 6440a94d38fSBill Fenner ** However, we seem to get DL_BADADDR - "DLSAP addr in improper 6450a94d38fSBill Fenner ** format or invalid" - errors if we use 1537 on the "tr0" 6460a94d38fSBill Fenner ** device, which, given that its name starts with "tr" and that 6470a94d38fSBill Fenner ** it's IBM, probably means a Token Ring device. (Perhaps we 6480a94d38fSBill Fenner ** need to use 1537 on "/dev/dlpi/en" because that device is for 6490a94d38fSBill Fenner ** D/I/X Ethernet, the "SAP" is actually an Ethernet type, and 6500a94d38fSBill Fenner ** it rejects invalid Ethernet types.) 6510a94d38fSBill Fenner ** 6520a94d38fSBill Fenner ** So if 1537 fails, we try 2, as Hyung Sik Yoon of IBM Korea 6530a94d38fSBill Fenner ** says that works on Token Ring (he says that 0 does *not* 6540a94d38fSBill Fenner ** work; perhaps that's considered an invalid LLC SAP value - I 6550a94d38fSBill Fenner ** assume the SAP value in a DLPI bind is an LLC SAP for network 6560a94d38fSBill Fenner ** types that use 802.2 LLC). 657dc2c7305SBill Fenner */ 6580a94d38fSBill Fenner if ((dlbindreq(p->fd, 1537, ebuf) < 0 && 6590a94d38fSBill Fenner dlbindreq(p->fd, 2, ebuf) < 0) || 660ee2dd488SSam Leffler dlbindack(p->fd, (char *)buf, ebuf, NULL) < 0) 6618cf6c252SPaul Traina goto bad; 66204fb2745SSam Leffler #elif defined(DL_HP_RAWDLS) 66304fb2745SSam Leffler /* 664ee2dd488SSam Leffler ** HP-UX 10.0x and 10.1x. 66504fb2745SSam Leffler */ 666ee2dd488SSam Leffler if (dl_dohpuxbind(p->fd, ebuf) < 0) 66704fb2745SSam Leffler goto bad; 66804fb2745SSam Leffler if (p->send_fd >= 0) { 66904fb2745SSam Leffler /* 670ee2dd488SSam Leffler ** XXX - if this fails, just close send_fd and 671ee2dd488SSam Leffler ** set it to -1, so that you can't send but can 672ee2dd488SSam Leffler ** still receive? 67304fb2745SSam Leffler */ 674ee2dd488SSam Leffler if (dl_dohpuxbind(p->send_fd, ebuf) < 0) 67504fb2745SSam Leffler goto bad; 67604fb2745SSam Leffler } 67704fb2745SSam Leffler #else /* neither AIX nor HP-UX */ 678ee2dd488SSam Leffler /* 679ee2dd488SSam Leffler ** Not Sinix, and neither AIX nor HP-UX - Solaris, and any other 680ee2dd488SSam Leffler ** OS using DLPI. 681ee2dd488SSam Leffler **/ 68204fb2745SSam Leffler if (dlbindreq(p->fd, 0, ebuf) < 0 || 683ee2dd488SSam Leffler dlbindack(p->fd, (char *)buf, ebuf, NULL) < 0) 68404fb2745SSam Leffler goto bad; 685ee2dd488SSam Leffler #endif /* AIX vs. HP-UX vs. other */ 686ee2dd488SSam Leffler #endif /* !HP-UX 9 and !HP-UX 10.20 or later and !SINIX */ 6878cf6c252SPaul Traina 688feb4ecdbSBruce M Simpson #ifdef HAVE_SOLARIS 689feb4ecdbSBruce M Simpson if (isatm) { 690feb4ecdbSBruce M Simpson /* 691feb4ecdbSBruce M Simpson ** Have to turn on some special ATM promiscuous mode 692feb4ecdbSBruce M Simpson ** for SunATM. 693feb4ecdbSBruce M Simpson ** Do *NOT* turn regular promiscuous mode on; it doesn't 694feb4ecdbSBruce M Simpson ** help, and may break things. 695feb4ecdbSBruce M Simpson */ 696feb4ecdbSBruce M Simpson if (strioctl(p->fd, A_PROMISCON_REQ, 0, NULL) < 0) { 697feb4ecdbSBruce M Simpson snprintf(ebuf, PCAP_ERRBUF_SIZE, "A_PROMISCON_REQ: %s", 698feb4ecdbSBruce M Simpson pcap_strerror(errno)); 699feb4ecdbSBruce M Simpson goto bad; 700feb4ecdbSBruce M Simpson } 701feb4ecdbSBruce M Simpson } else 702feb4ecdbSBruce M Simpson #endif 7038cf6c252SPaul Traina if (promisc) { 7048cf6c252SPaul Traina /* 70504fb2745SSam Leffler ** Enable promiscuous (not necessary on send FD) 7068cf6c252SPaul Traina */ 7078cf6c252SPaul Traina if (dlpromisconreq(p->fd, DL_PROMISC_PHYS, ebuf) < 0 || 7088cf6c252SPaul Traina dlokack(p->fd, "promisc_phys", (char *)buf, ebuf) < 0) 7098cf6c252SPaul Traina goto bad; 7108cf6c252SPaul Traina 7118cf6c252SPaul Traina /* 7128cf6c252SPaul Traina ** Try to enable multicast (you would have thought 713a4b5b39fSBill Fenner ** promiscuous would be sufficient). (Skip if using 71404fb2745SSam Leffler ** HP-UX or SINIX) (Not necessary on send FD) 7158cf6c252SPaul Traina */ 716a4b5b39fSBill Fenner #if !defined(__hpux) && !defined(sinix) 7178cf6c252SPaul Traina if (dlpromisconreq(p->fd, DL_PROMISC_MULTI, ebuf) < 0 || 7188cf6c252SPaul Traina dlokack(p->fd, "promisc_multi", (char *)buf, ebuf) < 0) 7198cf6c252SPaul Traina fprintf(stderr, 7208cf6c252SPaul Traina "WARNING: DL_PROMISC_MULTI failed (%s)\n", ebuf); 7218cf6c252SPaul Traina #endif 7228cf6c252SPaul Traina } 7238cf6c252SPaul Traina /* 724ee2dd488SSam Leffler ** Try to enable SAP promiscuity (when not in promiscuous mode 725ee2dd488SSam Leffler ** when using HP-UX, when not doing SunATM on Solaris, and never 72604fb2745SSam Leffler ** under SINIX) (Not necessary on send FD) 7278cf6c252SPaul Traina */ 7288cf6c252SPaul Traina #ifndef sinix 729a4b5b39fSBill Fenner if ( 730a4b5b39fSBill Fenner #ifdef __hpux 731a4b5b39fSBill Fenner !promisc && 732a4b5b39fSBill Fenner #endif 733feb4ecdbSBruce M Simpson #ifdef HAVE_SOLARIS 734feb4ecdbSBruce M Simpson !isatm && 735feb4ecdbSBruce M Simpson #endif 736a4b5b39fSBill Fenner (dlpromisconreq(p->fd, DL_PROMISC_SAP, ebuf) < 0 || 737a4b5b39fSBill Fenner dlokack(p->fd, "promisc_sap", (char *)buf, ebuf) < 0)) { 7388cf6c252SPaul Traina /* Not fatal if promisc since the DL_PROMISC_PHYS worked */ 7398cf6c252SPaul Traina if (promisc) 7408cf6c252SPaul Traina fprintf(stderr, 7418cf6c252SPaul Traina "WARNING: DL_PROMISC_SAP failed (%s)\n", ebuf); 7428cf6c252SPaul Traina else 7438cf6c252SPaul Traina goto bad; 7448cf6c252SPaul Traina } 745ee2dd488SSam Leffler #endif /* sinix */ 7468cf6c252SPaul Traina 7478cf6c252SPaul Traina /* 748ee2dd488SSam Leffler ** HP-UX 9, and HP-UX 10.20 or later, must bind after setting 749ee2dd488SSam Leffler ** promiscuous options. 7508cf6c252SPaul Traina */ 751ee2dd488SSam Leffler #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20_OR_LATER) 752ee2dd488SSam Leffler if (dl_dohpuxbind(p->fd, ebuf) < 0) 7538cf6c252SPaul Traina goto bad; 754ee2dd488SSam Leffler /* 755ee2dd488SSam Leffler ** We don't set promiscuous mode on the send FD, but we'll defer 756ee2dd488SSam Leffler ** binding it anyway, just to keep the HP-UX 9/10.20 or later 757ee2dd488SSam Leffler ** code together. 758ee2dd488SSam Leffler */ 759ee2dd488SSam Leffler if (p->send_fd >= 0) { 760ee2dd488SSam Leffler /* 761ee2dd488SSam Leffler ** XXX - if this fails, just close send_fd and 762ee2dd488SSam Leffler ** set it to -1, so that you can't send but can 763ee2dd488SSam Leffler ** still receive? 764ee2dd488SSam Leffler */ 765ee2dd488SSam Leffler if (dl_dohpuxbind(p->send_fd, ebuf) < 0) 766ee2dd488SSam Leffler goto bad; 767ee2dd488SSam Leffler } 7688cf6c252SPaul Traina #endif 7698cf6c252SPaul Traina 7708cf6c252SPaul Traina /* 7718cf6c252SPaul Traina ** Determine link type 77204fb2745SSam Leffler ** XXX - get SAP length and address length as well, for use 77304fb2745SSam Leffler ** when sending packets. 7748cf6c252SPaul Traina */ 7758cf6c252SPaul Traina if (dlinforeq(p->fd, ebuf) < 0 || 7768cf6c252SPaul Traina dlinfoack(p->fd, (char *)buf, ebuf) < 0) 7778cf6c252SPaul Traina goto bad; 7788cf6c252SPaul Traina 7798cf6c252SPaul Traina infop = &((union DL_primitives *)buf)->info_ack; 7808cf6c252SPaul Traina switch (infop->dl_mac_type) { 7818cf6c252SPaul Traina 7828cf6c252SPaul Traina case DL_CSMACD: 7838cf6c252SPaul Traina case DL_ETHER: 7848cf6c252SPaul Traina p->linktype = DLT_EN10MB; 7858cf6c252SPaul Traina p->offset = 2; 78604fb2745SSam Leffler /* 78704fb2745SSam Leffler * This is (presumably) a real Ethernet capture; give it a 78804fb2745SSam Leffler * link-layer-type list with DLT_EN10MB and DLT_DOCSIS, so 78904fb2745SSam Leffler * that an application can let you choose it, in case you're 79004fb2745SSam Leffler * capturing DOCSIS traffic that a Cisco Cable Modem 79104fb2745SSam Leffler * Termination System is putting out onto an Ethernet (it 79204fb2745SSam Leffler * doesn't put an Ethernet header onto the wire, it puts raw 79304fb2745SSam Leffler * DOCSIS frames out on the wire inside the low-level 79404fb2745SSam Leffler * Ethernet framing). 79504fb2745SSam Leffler */ 79604fb2745SSam Leffler p->dlt_list = (u_int *) malloc(sizeof(u_int) * 2); 79704fb2745SSam Leffler /* 79804fb2745SSam Leffler * If that fails, just leave the list empty. 79904fb2745SSam Leffler */ 80004fb2745SSam Leffler if (p->dlt_list != NULL) { 80104fb2745SSam Leffler p->dlt_list[0] = DLT_EN10MB; 80204fb2745SSam Leffler p->dlt_list[1] = DLT_DOCSIS; 80304fb2745SSam Leffler p->dlt_count = 2; 80404fb2745SSam Leffler } 8058cf6c252SPaul Traina break; 8068cf6c252SPaul Traina 8078cf6c252SPaul Traina case DL_FDDI: 8088cf6c252SPaul Traina p->linktype = DLT_FDDI; 809a4b5b39fSBill Fenner p->offset = 3; 8108cf6c252SPaul Traina break; 8118cf6c252SPaul Traina 8120a94d38fSBill Fenner case DL_TPR: 81304fb2745SSam Leffler /* 81404fb2745SSam Leffler * XXX - what about DL_TPB? Is that Token Bus? 81504fb2745SSam Leffler */ 8160a94d38fSBill Fenner p->linktype = DLT_IEEE802; 8170a94d38fSBill Fenner p->offset = 2; 8180a94d38fSBill Fenner break; 8190a94d38fSBill Fenner 820feb4ecdbSBruce M Simpson #ifdef HAVE_SOLARIS 821feb4ecdbSBruce M Simpson case DL_IPATM: 822feb4ecdbSBruce M Simpson p->linktype = DLT_SUNATM; 823feb4ecdbSBruce M Simpson p->offset = 0; /* works for LANE and LLC encapsulation */ 824feb4ecdbSBruce M Simpson break; 825feb4ecdbSBruce M Simpson #endif 826feb4ecdbSBruce M Simpson 8278cf6c252SPaul Traina default: 828dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, "unknown mac type %lu", 829feb4ecdbSBruce M Simpson (unsigned long)infop->dl_mac_type); 8308cf6c252SPaul Traina goto bad; 8318cf6c252SPaul Traina } 8328cf6c252SPaul Traina 8338cf6c252SPaul Traina #ifdef DLIOCRAW 8348cf6c252SPaul Traina /* 835feb4ecdbSBruce M Simpson ** This is a non standard SunOS hack to get the full raw link-layer 836feb4ecdbSBruce M Simpson ** header. 8378cf6c252SPaul Traina */ 8388cf6c252SPaul Traina if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) { 839dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, "DLIOCRAW: %s", 840dc2c7305SBill Fenner pcap_strerror(errno)); 8418cf6c252SPaul Traina goto bad; 8428cf6c252SPaul Traina } 8438cf6c252SPaul Traina #endif 8448cf6c252SPaul Traina 8458cf6c252SPaul Traina #ifdef HAVE_SYS_BUFMOD_H 8468cf6c252SPaul Traina /* 8478cf6c252SPaul Traina ** Another non standard call to get the data nicely buffered 8488cf6c252SPaul Traina */ 8498cf6c252SPaul Traina if (ioctl(p->fd, I_PUSH, "bufmod") != 0) { 850dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, "I_PUSH bufmod: %s", 851dc2c7305SBill Fenner pcap_strerror(errno)); 8528cf6c252SPaul Traina goto bad; 8538cf6c252SPaul Traina } 8548cf6c252SPaul Traina 8558cf6c252SPaul Traina /* 8568cf6c252SPaul Traina ** Now that the bufmod is pushed lets configure it. 8578cf6c252SPaul Traina ** 8588cf6c252SPaul Traina ** There is a bug in bufmod(7). When dealing with messages of 8598cf6c252SPaul Traina ** less than snaplen size it strips data from the beginning not 8608cf6c252SPaul Traina ** the end. 8618cf6c252SPaul Traina ** 8628cf6c252SPaul Traina ** This bug is supposed to be fixed in 5.3.2. Also, there is a 8638cf6c252SPaul Traina ** patch available. Ask for bugid 1149065. 8648cf6c252SPaul Traina */ 8658cf6c252SPaul Traina ss = snaplen; 8668cf6c252SPaul Traina #ifdef HAVE_SOLARIS 8678cf6c252SPaul Traina release = get_release(&osmajor, &osminor, &osmicro); 8688cf6c252SPaul Traina if (osmajor == 5 && (osminor <= 2 || (osminor == 3 && osmicro < 2)) && 8698cf6c252SPaul Traina getenv("BUFMOD_FIXED") == NULL) { 8708cf6c252SPaul Traina fprintf(stderr, 8718cf6c252SPaul Traina "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.\n", 8728cf6c252SPaul Traina release); 8738cf6c252SPaul Traina ss = 0; 8748cf6c252SPaul Traina } 8758cf6c252SPaul Traina #endif 8768cf6c252SPaul Traina if (ss > 0 && 8778cf6c252SPaul Traina strioctl(p->fd, SBIOCSSNAP, sizeof(ss), (char *)&ss) != 0) { 878dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, "SBIOCSSNAP: %s", 879dc2c7305SBill Fenner pcap_strerror(errno)); 8808cf6c252SPaul Traina goto bad; 8818cf6c252SPaul Traina } 8828cf6c252SPaul Traina 8838cf6c252SPaul Traina /* 8848cf6c252SPaul Traina ** Set up the bufmod timeout 8858cf6c252SPaul Traina */ 8868cf6c252SPaul Traina if (to_ms != 0) { 8878cf6c252SPaul Traina struct timeval to; 8888cf6c252SPaul Traina 8898cf6c252SPaul Traina to.tv_sec = to_ms / 1000; 8908cf6c252SPaul Traina to.tv_usec = (to_ms * 1000) % 1000000; 8918cf6c252SPaul Traina if (strioctl(p->fd, SBIOCSTIME, sizeof(to), (char *)&to) != 0) { 892dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, "SBIOCSTIME: %s", 893dc2c7305SBill Fenner pcap_strerror(errno)); 8948cf6c252SPaul Traina goto bad; 8958cf6c252SPaul Traina } 8968cf6c252SPaul Traina } 897feb4ecdbSBruce M Simpson 898feb4ecdbSBruce M Simpson /* 899feb4ecdbSBruce M Simpson ** Set the chunk length. 900feb4ecdbSBruce M Simpson */ 901feb4ecdbSBruce M Simpson chunksize = CHUNKSIZE; 902feb4ecdbSBruce M Simpson if (strioctl(p->fd, SBIOCSCHUNK, sizeof(chunksize), (char *)&chunksize) 903feb4ecdbSBruce M Simpson != 0) { 904feb4ecdbSBruce M Simpson snprintf(ebuf, PCAP_ERRBUF_SIZE, "SBIOCSCHUNKP: %s", 905feb4ecdbSBruce M Simpson pcap_strerror(errno)); 906feb4ecdbSBruce M Simpson goto bad; 907feb4ecdbSBruce M Simpson } 9088cf6c252SPaul Traina #endif 9098cf6c252SPaul Traina 9108cf6c252SPaul Traina /* 9118cf6c252SPaul Traina ** As the last operation flush the read side. 9128cf6c252SPaul Traina */ 9138cf6c252SPaul Traina if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) { 914dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, "FLUSHR: %s", 915dc2c7305SBill Fenner pcap_strerror(errno)); 9168cf6c252SPaul Traina goto bad; 9178cf6c252SPaul Traina } 918feb4ecdbSBruce M Simpson 9198cf6c252SPaul Traina /* Allocate data buffer */ 920feb4ecdbSBruce M Simpson p->bufsize = PKTBUFSIZE; 9218cf6c252SPaul Traina p->buffer = (u_char *)malloc(p->bufsize + p->offset); 922feb4ecdbSBruce M Simpson if (p->buffer == NULL) { 923feb4ecdbSBruce M Simpson strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE); 924feb4ecdbSBruce M Simpson goto bad; 925feb4ecdbSBruce M Simpson } 926feb4ecdbSBruce M Simpson 927feb4ecdbSBruce M Simpson /* 928feb4ecdbSBruce M Simpson * "p->fd" is an FD for a STREAMS device, so "select()" and 929feb4ecdbSBruce M Simpson * "poll()" should work on it. 930feb4ecdbSBruce M Simpson */ 931feb4ecdbSBruce M Simpson p->selectable_fd = p->fd; 932feb4ecdbSBruce M Simpson 933feb4ecdbSBruce M Simpson p->read_op = pcap_read_dlpi; 93404fb2745SSam Leffler p->inject_op = pcap_inject_dlpi; 935feb4ecdbSBruce M Simpson p->setfilter_op = install_bpf_program; /* no kernel filtering */ 936ee2dd488SSam Leffler p->setdirection_op = NULL; /* Not implemented.*/ 937feb4ecdbSBruce M Simpson p->set_datalink_op = NULL; /* can't change data link type */ 938feb4ecdbSBruce M Simpson p->getnonblock_op = pcap_getnonblock_fd; 939feb4ecdbSBruce M Simpson p->setnonblock_op = pcap_setnonblock_fd; 940feb4ecdbSBruce M Simpson p->stats_op = pcap_stats_dlpi; 941feb4ecdbSBruce M Simpson p->close_op = pcap_close_dlpi; 9428cf6c252SPaul Traina 9438cf6c252SPaul Traina return (p); 9448cf6c252SPaul Traina bad: 9450a94d38fSBill Fenner if (p->fd >= 0) 9460a94d38fSBill Fenner close(p->fd); 94704fb2745SSam Leffler if (p->send_fd >= 0) 94804fb2745SSam Leffler close(p->send_fd); 94904fb2745SSam Leffler /* 95004fb2745SSam Leffler * Get rid of any link-layer type list we allocated. 95104fb2745SSam Leffler */ 95204fb2745SSam Leffler if (p->dlt_list != NULL) 95304fb2745SSam Leffler free(p->dlt_list); 9548cf6c252SPaul Traina free(p); 9558cf6c252SPaul Traina return (NULL); 9568cf6c252SPaul Traina } 9578cf6c252SPaul Traina 9580a94d38fSBill Fenner /* 9590a94d38fSBill Fenner * Split a device name into a device type name and a unit number; 9600a94d38fSBill Fenner * return the a pointer to the beginning of the unit number, which 9610a94d38fSBill Fenner * is the end of the device type name, and set "*unitp" to the unit 9620a94d38fSBill Fenner * number. 9630a94d38fSBill Fenner * 9640a94d38fSBill Fenner * Returns NULL on error, and fills "ebuf" with an error message. 9650a94d38fSBill Fenner */ 9660a94d38fSBill Fenner static char * 9670a94d38fSBill Fenner split_dname(char *device, int *unitp, char *ebuf) 9680a94d38fSBill Fenner { 9690a94d38fSBill Fenner char *cp; 9700a94d38fSBill Fenner char *eos; 97104fb2745SSam Leffler long unit; 9720a94d38fSBill Fenner 9730a94d38fSBill Fenner /* 9740a94d38fSBill Fenner * Look for a number at the end of the device name string. 9750a94d38fSBill Fenner */ 9760a94d38fSBill Fenner cp = device + strlen(device) - 1; 9770a94d38fSBill Fenner if (*cp < '0' || *cp > '9') { 9780a94d38fSBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s missing unit number", 9790a94d38fSBill Fenner device); 9800a94d38fSBill Fenner return (NULL); 9810a94d38fSBill Fenner } 9820a94d38fSBill Fenner 9830a94d38fSBill Fenner /* Digits at end of string are unit number */ 9840a94d38fSBill Fenner while (cp-1 >= device && *(cp-1) >= '0' && *(cp-1) <= '9') 9850a94d38fSBill Fenner cp--; 9860a94d38fSBill Fenner 98704fb2745SSam Leffler errno = 0; 9880a94d38fSBill Fenner unit = strtol(cp, &eos, 10); 9890a94d38fSBill Fenner if (*eos != '\0') { 9900a94d38fSBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s bad unit number", device); 9910a94d38fSBill Fenner return (NULL); 9920a94d38fSBill Fenner } 99304fb2745SSam Leffler if (errno == ERANGE || unit > INT_MAX) { 99404fb2745SSam Leffler snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number too large", 99504fb2745SSam Leffler device); 99604fb2745SSam Leffler return (NULL); 99704fb2745SSam Leffler } 99804fb2745SSam Leffler if (unit < 0) { 99904fb2745SSam Leffler snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number is negative", 100004fb2745SSam Leffler device); 100104fb2745SSam Leffler return (NULL); 100204fb2745SSam Leffler } 100304fb2745SSam Leffler *unitp = (int)unit; 10040a94d38fSBill Fenner return (cp); 10050a94d38fSBill Fenner } 10060a94d38fSBill Fenner 100704fb2745SSam Leffler static int 100804fb2745SSam Leffler dl_doattach(int fd, int ppa, char *ebuf) 100904fb2745SSam Leffler { 101004fb2745SSam Leffler bpf_u_int32 buf[MAXDLBUF]; 101104fb2745SSam Leffler 101204fb2745SSam Leffler if (dlattachreq(fd, ppa, ebuf) < 0 || 101304fb2745SSam Leffler dlokack(fd, "attach", (char *)buf, ebuf) < 0) 101404fb2745SSam Leffler return (-1); 101504fb2745SSam Leffler return (0); 101604fb2745SSam Leffler } 101704fb2745SSam Leffler 1018ee2dd488SSam Leffler #ifdef DL_HP_RAWDLS 1019ee2dd488SSam Leffler static int 1020ee2dd488SSam Leffler dl_dohpuxbind(int fd, char *ebuf) 1021ee2dd488SSam Leffler { 1022ee2dd488SSam Leffler int hpsap; 1023ee2dd488SSam Leffler int uerror; 1024ee2dd488SSam Leffler bpf_u_int32 buf[MAXDLBUF]; 1025ee2dd488SSam Leffler 1026ee2dd488SSam Leffler /* 1027ee2dd488SSam Leffler * XXX - we start at 22 because we used to use only 22, but 1028ee2dd488SSam Leffler * that was just because that was the value used in some 1029ee2dd488SSam Leffler * sample code from HP. With what value *should* we start? 1030ee2dd488SSam Leffler * Does it matter, given that we're enabling SAP promiscuity 1031ee2dd488SSam Leffler * on the input FD? 1032ee2dd488SSam Leffler */ 1033ee2dd488SSam Leffler hpsap = 22; 1034ee2dd488SSam Leffler for (;;) { 1035ee2dd488SSam Leffler if (dlbindreq(fd, hpsap, ebuf) < 0) 1036ee2dd488SSam Leffler return (-1); 1037ee2dd488SSam Leffler if (dlbindack(fd, (char *)buf, ebuf, &uerror) >= 0) 1038ee2dd488SSam Leffler break; 1039ee2dd488SSam Leffler /* 1040ee2dd488SSam Leffler * For any error other than a UNIX EBUSY, give up. 1041ee2dd488SSam Leffler */ 1042ee2dd488SSam Leffler if (uerror != EBUSY) 1043ee2dd488SSam Leffler return (-1); 1044ee2dd488SSam Leffler 1045ee2dd488SSam Leffler /* 1046ee2dd488SSam Leffler * For EBUSY, try the next SAP value; that means that 1047ee2dd488SSam Leffler * somebody else is using that SAP. Clear ebuf so 1048ee2dd488SSam Leffler * that application doesn't report the "Device busy" 1049ee2dd488SSam Leffler * error as a warning. 1050ee2dd488SSam Leffler */ 1051ee2dd488SSam Leffler *ebuf = '\0'; 1052ee2dd488SSam Leffler hpsap++; 1053ee2dd488SSam Leffler if (hpsap > 100) 1054ee2dd488SSam Leffler return (-1); 1055ee2dd488SSam Leffler } 1056ee2dd488SSam Leffler } 1057ee2dd488SSam Leffler #endif 1058ee2dd488SSam Leffler 10598cf6c252SPaul Traina int 1060feb4ecdbSBruce M Simpson pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) 10618cf6c252SPaul Traina { 1062feb4ecdbSBruce M Simpson #ifdef HAVE_SOLARIS 1063feb4ecdbSBruce M Simpson int fd; 1064feb4ecdbSBruce M Simpson union { 1065feb4ecdbSBruce M Simpson u_int nunits; 1066feb4ecdbSBruce M Simpson char pad[516]; /* XXX - must be at least 513; is 516 1067feb4ecdbSBruce M Simpson in "atmgetunits" */ 1068feb4ecdbSBruce M Simpson } buf; 1069feb4ecdbSBruce M Simpson char baname[2+1+1]; 1070feb4ecdbSBruce M Simpson u_int i; 10718cf6c252SPaul Traina 1072feb4ecdbSBruce M Simpson /* 1073feb4ecdbSBruce M Simpson * We may have to do special magic to get ATM devices. 1074feb4ecdbSBruce M Simpson */ 1075feb4ecdbSBruce M Simpson if ((fd = open("/dev/ba", O_RDWR)) < 0) { 1076feb4ecdbSBruce M Simpson /* 1077feb4ecdbSBruce M Simpson * We couldn't open the "ba" device. 1078feb4ecdbSBruce M Simpson * For now, just give up; perhaps we should 1079feb4ecdbSBruce M Simpson * return an error if the problem is neither 1080feb4ecdbSBruce M Simpson * a "that device doesn't exist" error (ENOENT, 1081feb4ecdbSBruce M Simpson * ENXIO, etc.) or a "you're not allowed to do 1082feb4ecdbSBruce M Simpson * that" error (EPERM, EACCES). 1083feb4ecdbSBruce M Simpson */ 1084feb4ecdbSBruce M Simpson return (0); 1085feb4ecdbSBruce M Simpson } 1086feb4ecdbSBruce M Simpson 1087feb4ecdbSBruce M Simpson if (strioctl(fd, A_GET_UNITS, sizeof(buf), (char *)&buf) < 0) { 1088feb4ecdbSBruce M Simpson snprintf(errbuf, PCAP_ERRBUF_SIZE, "A_GET_UNITS: %s", 1089feb4ecdbSBruce M Simpson pcap_strerror(errno)); 1090dc2c7305SBill Fenner return (-1); 1091feb4ecdbSBruce M Simpson } 1092feb4ecdbSBruce M Simpson for (i = 0; i < buf.nunits; i++) { 1093feb4ecdbSBruce M Simpson snprintf(baname, sizeof baname, "ba%u", i); 1094feb4ecdbSBruce M Simpson if (pcap_add_if(alldevsp, baname, 0, NULL, errbuf) < 0) 1095feb4ecdbSBruce M Simpson return (-1); 1096feb4ecdbSBruce M Simpson } 1097feb4ecdbSBruce M Simpson #endif 1098feb4ecdbSBruce M Simpson 10998cf6c252SPaul Traina return (0); 11008cf6c252SPaul Traina } 11018cf6c252SPaul Traina 11028cf6c252SPaul Traina static int 11038cf6c252SPaul Traina send_request(int fd, char *ptr, int len, char *what, char *ebuf) 11048cf6c252SPaul Traina { 11058cf6c252SPaul Traina struct strbuf ctl; 11068cf6c252SPaul Traina int flags; 11078cf6c252SPaul Traina 11088cf6c252SPaul Traina ctl.maxlen = 0; 11098cf6c252SPaul Traina ctl.len = len; 11108cf6c252SPaul Traina ctl.buf = ptr; 11118cf6c252SPaul Traina 11128cf6c252SPaul Traina flags = 0; 11138cf6c252SPaul Traina if (putmsg(fd, &ctl, (struct strbuf *) NULL, flags) < 0) { 1114dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, 1115dc2c7305SBill Fenner "send_request: putmsg \"%s\": %s", 11168cf6c252SPaul Traina what, pcap_strerror(errno)); 11178cf6c252SPaul Traina return (-1); 11188cf6c252SPaul Traina } 11198cf6c252SPaul Traina return (0); 11208cf6c252SPaul Traina } 11218cf6c252SPaul Traina 11228cf6c252SPaul Traina static int 1123ee2dd488SSam Leffler recv_ack(int fd, int size, const char *what, char *bufp, char *ebuf, int *uerror) 11248cf6c252SPaul Traina { 11258cf6c252SPaul Traina union DL_primitives *dlp; 11268cf6c252SPaul Traina struct strbuf ctl; 11278cf6c252SPaul Traina int flags; 11288cf6c252SPaul Traina 11298cf6c252SPaul Traina ctl.maxlen = MAXDLBUF; 11308cf6c252SPaul Traina ctl.len = 0; 11318cf6c252SPaul Traina ctl.buf = bufp; 11328cf6c252SPaul Traina 11338cf6c252SPaul Traina flags = 0; 11348cf6c252SPaul Traina if (getmsg(fd, &ctl, (struct strbuf*)NULL, &flags) < 0) { 1135dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, "recv_ack: %s getmsg: %s", 11368cf6c252SPaul Traina what, pcap_strerror(errno)); 11378cf6c252SPaul Traina return (-1); 11388cf6c252SPaul Traina } 11398cf6c252SPaul Traina 11408cf6c252SPaul Traina dlp = (union DL_primitives *) ctl.buf; 11418cf6c252SPaul Traina switch (dlp->dl_primitive) { 11428cf6c252SPaul Traina 11438cf6c252SPaul Traina case DL_INFO_ACK: 11448cf6c252SPaul Traina case DL_BIND_ACK: 11458cf6c252SPaul Traina case DL_OK_ACK: 11468cf6c252SPaul Traina #ifdef DL_HP_PPA_ACK 11478cf6c252SPaul Traina case DL_HP_PPA_ACK: 11488cf6c252SPaul Traina #endif 11498cf6c252SPaul Traina /* These are OK */ 11508cf6c252SPaul Traina break; 11518cf6c252SPaul Traina 11528cf6c252SPaul Traina case DL_ERROR_ACK: 11538cf6c252SPaul Traina switch (dlp->error_ack.dl_errno) { 11548cf6c252SPaul Traina 11558cf6c252SPaul Traina case DL_SYSERR: 1156ee2dd488SSam Leffler if (uerror != NULL) 1157ee2dd488SSam Leffler *uerror = dlp->error_ack.dl_unix_errno; 11580a94d38fSBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, 11590a94d38fSBill Fenner "recv_ack: %s: UNIX error - %s", 11608cf6c252SPaul Traina what, pcap_strerror(dlp->error_ack.dl_unix_errno)); 11618cf6c252SPaul Traina break; 11628cf6c252SPaul Traina 11638cf6c252SPaul Traina default: 1164ee2dd488SSam Leffler if (uerror != NULL) 1165ee2dd488SSam Leffler *uerror = 0; 11660a94d38fSBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, "recv_ack: %s: %s", 11670a94d38fSBill Fenner what, dlstrerror(dlp->error_ack.dl_errno)); 11688cf6c252SPaul Traina break; 11698cf6c252SPaul Traina } 11708cf6c252SPaul Traina return (-1); 11718cf6c252SPaul Traina 11728cf6c252SPaul Traina default: 1173ee2dd488SSam Leffler if (uerror != NULL) 1174ee2dd488SSam Leffler *uerror = 0; 1175dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, 11760a94d38fSBill Fenner "recv_ack: %s: Unexpected primitive ack %s", 11770a94d38fSBill Fenner what, dlprim(dlp->dl_primitive)); 11788cf6c252SPaul Traina return (-1); 11798cf6c252SPaul Traina } 11808cf6c252SPaul Traina 11818cf6c252SPaul Traina if (ctl.len < size) { 1182ee2dd488SSam Leffler if (uerror != NULL) 1183ee2dd488SSam Leffler *uerror = 0; 1184dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, 11850a94d38fSBill Fenner "recv_ack: %s: Ack too small (%d < %d)", 11868cf6c252SPaul Traina what, ctl.len, size); 11878cf6c252SPaul Traina return (-1); 11888cf6c252SPaul Traina } 11898cf6c252SPaul Traina return (ctl.len); 11908cf6c252SPaul Traina } 11918cf6c252SPaul Traina 11920a94d38fSBill Fenner static char * 11930a94d38fSBill Fenner dlstrerror(bpf_u_int32 dl_errno) 11940a94d38fSBill Fenner { 11950a94d38fSBill Fenner static char errstring[6+2+8+1]; 11960a94d38fSBill Fenner 11970a94d38fSBill Fenner switch (dl_errno) { 11980a94d38fSBill Fenner 11990a94d38fSBill Fenner case DL_ACCESS: 12000a94d38fSBill Fenner return ("Improper permissions for request"); 12010a94d38fSBill Fenner 12020a94d38fSBill Fenner case DL_BADADDR: 12030a94d38fSBill Fenner return ("DLSAP addr in improper format or invalid"); 12040a94d38fSBill Fenner 12050a94d38fSBill Fenner case DL_BADCORR: 12060a94d38fSBill Fenner return ("Seq number not from outstand DL_CONN_IND"); 12070a94d38fSBill Fenner 12080a94d38fSBill Fenner case DL_BADDATA: 12090a94d38fSBill Fenner return ("User data exceeded provider limit"); 12100a94d38fSBill Fenner 12110a94d38fSBill Fenner case DL_BADPPA: 12120a94d38fSBill Fenner #ifdef HAVE_DEV_DLPI 12130a94d38fSBill Fenner /* 12140a94d38fSBill Fenner * With a single "/dev/dlpi" device used for all 12150a94d38fSBill Fenner * DLPI providers, PPAs have nothing to do with 12160a94d38fSBill Fenner * unit numbers. 12170a94d38fSBill Fenner */ 12180a94d38fSBill Fenner return ("Specified PPA was invalid"); 12190a94d38fSBill Fenner #else 12200a94d38fSBill Fenner /* 12210a94d38fSBill Fenner * We have separate devices for separate devices; 12220a94d38fSBill Fenner * the PPA is just the unit number. 12230a94d38fSBill Fenner */ 12240a94d38fSBill Fenner return ("Specified PPA (device unit) was invalid"); 12250a94d38fSBill Fenner #endif 12260a94d38fSBill Fenner 12270a94d38fSBill Fenner case DL_BADPRIM: 12280a94d38fSBill Fenner return ("Primitive received not known by provider"); 12290a94d38fSBill Fenner 12300a94d38fSBill Fenner case DL_BADQOSPARAM: 12310a94d38fSBill Fenner return ("QOS parameters contained invalid values"); 12320a94d38fSBill Fenner 12330a94d38fSBill Fenner case DL_BADQOSTYPE: 12340a94d38fSBill Fenner return ("QOS structure type is unknown/unsupported"); 12350a94d38fSBill Fenner 12360a94d38fSBill Fenner case DL_BADSAP: 12370a94d38fSBill Fenner return ("Bad LSAP selector"); 12380a94d38fSBill Fenner 12390a94d38fSBill Fenner case DL_BADTOKEN: 12400a94d38fSBill Fenner return ("Token used not an active stream"); 12410a94d38fSBill Fenner 12420a94d38fSBill Fenner case DL_BOUND: 12430a94d38fSBill Fenner return ("Attempted second bind with dl_max_conind"); 12440a94d38fSBill Fenner 12450a94d38fSBill Fenner case DL_INITFAILED: 12460a94d38fSBill Fenner return ("Physical link initialization failed"); 12470a94d38fSBill Fenner 12480a94d38fSBill Fenner case DL_NOADDR: 12490a94d38fSBill Fenner return ("Provider couldn't allocate alternate address"); 12500a94d38fSBill Fenner 12510a94d38fSBill Fenner case DL_NOTINIT: 12520a94d38fSBill Fenner return ("Physical link not initialized"); 12530a94d38fSBill Fenner 12540a94d38fSBill Fenner case DL_OUTSTATE: 12550a94d38fSBill Fenner return ("Primitive issued in improper state"); 12560a94d38fSBill Fenner 12570a94d38fSBill Fenner case DL_SYSERR: 12580a94d38fSBill Fenner return ("UNIX system error occurred"); 12590a94d38fSBill Fenner 12600a94d38fSBill Fenner case DL_UNSUPPORTED: 12610a94d38fSBill Fenner return ("Requested service not supplied by provider"); 12620a94d38fSBill Fenner 12630a94d38fSBill Fenner case DL_UNDELIVERABLE: 12640a94d38fSBill Fenner return ("Previous data unit could not be delivered"); 12650a94d38fSBill Fenner 12660a94d38fSBill Fenner case DL_NOTSUPPORTED: 12670a94d38fSBill Fenner return ("Primitive is known but not supported"); 12680a94d38fSBill Fenner 12690a94d38fSBill Fenner case DL_TOOMANY: 12700a94d38fSBill Fenner return ("Limit exceeded"); 12710a94d38fSBill Fenner 12720a94d38fSBill Fenner case DL_NOTENAB: 12730a94d38fSBill Fenner return ("Promiscuous mode not enabled"); 12740a94d38fSBill Fenner 12750a94d38fSBill Fenner case DL_BUSY: 12760a94d38fSBill Fenner return ("Other streams for PPA in post-attached"); 12770a94d38fSBill Fenner 12780a94d38fSBill Fenner case DL_NOAUTO: 12790a94d38fSBill Fenner return ("Automatic handling XID&TEST not supported"); 12800a94d38fSBill Fenner 12810a94d38fSBill Fenner case DL_NOXIDAUTO: 12820a94d38fSBill Fenner return ("Automatic handling of XID not supported"); 12830a94d38fSBill Fenner 12840a94d38fSBill Fenner case DL_NOTESTAUTO: 12850a94d38fSBill Fenner return ("Automatic handling of TEST not supported"); 12860a94d38fSBill Fenner 12870a94d38fSBill Fenner case DL_XIDAUTO: 12880a94d38fSBill Fenner return ("Automatic handling of XID response"); 12890a94d38fSBill Fenner 12900a94d38fSBill Fenner case DL_TESTAUTO: 12910a94d38fSBill Fenner return ("Automatic handling of TEST response"); 12920a94d38fSBill Fenner 12930a94d38fSBill Fenner case DL_PENDING: 12940a94d38fSBill Fenner return ("Pending outstanding connect indications"); 12950a94d38fSBill Fenner 12960a94d38fSBill Fenner default: 12970a94d38fSBill Fenner sprintf(errstring, "Error %02x", dl_errno); 12980a94d38fSBill Fenner return (errstring); 12990a94d38fSBill Fenner } 13000a94d38fSBill Fenner } 13010a94d38fSBill Fenner 13020a94d38fSBill Fenner static char * 13030a94d38fSBill Fenner dlprim(bpf_u_int32 prim) 13040a94d38fSBill Fenner { 13050a94d38fSBill Fenner static char primbuf[80]; 13060a94d38fSBill Fenner 13070a94d38fSBill Fenner switch (prim) { 13080a94d38fSBill Fenner 13090a94d38fSBill Fenner case DL_INFO_REQ: 13100a94d38fSBill Fenner return ("DL_INFO_REQ"); 13110a94d38fSBill Fenner 13120a94d38fSBill Fenner case DL_INFO_ACK: 13130a94d38fSBill Fenner return ("DL_INFO_ACK"); 13140a94d38fSBill Fenner 13150a94d38fSBill Fenner case DL_ATTACH_REQ: 13160a94d38fSBill Fenner return ("DL_ATTACH_REQ"); 13170a94d38fSBill Fenner 13180a94d38fSBill Fenner case DL_DETACH_REQ: 13190a94d38fSBill Fenner return ("DL_DETACH_REQ"); 13200a94d38fSBill Fenner 13210a94d38fSBill Fenner case DL_BIND_REQ: 13220a94d38fSBill Fenner return ("DL_BIND_REQ"); 13230a94d38fSBill Fenner 13240a94d38fSBill Fenner case DL_BIND_ACK: 13250a94d38fSBill Fenner return ("DL_BIND_ACK"); 13260a94d38fSBill Fenner 13270a94d38fSBill Fenner case DL_UNBIND_REQ: 13280a94d38fSBill Fenner return ("DL_UNBIND_REQ"); 13290a94d38fSBill Fenner 13300a94d38fSBill Fenner case DL_OK_ACK: 13310a94d38fSBill Fenner return ("DL_OK_ACK"); 13320a94d38fSBill Fenner 13330a94d38fSBill Fenner case DL_ERROR_ACK: 13340a94d38fSBill Fenner return ("DL_ERROR_ACK"); 13350a94d38fSBill Fenner 13360a94d38fSBill Fenner case DL_SUBS_BIND_REQ: 13370a94d38fSBill Fenner return ("DL_SUBS_BIND_REQ"); 13380a94d38fSBill Fenner 13390a94d38fSBill Fenner case DL_SUBS_BIND_ACK: 13400a94d38fSBill Fenner return ("DL_SUBS_BIND_ACK"); 13410a94d38fSBill Fenner 13420a94d38fSBill Fenner case DL_UNITDATA_REQ: 13430a94d38fSBill Fenner return ("DL_UNITDATA_REQ"); 13440a94d38fSBill Fenner 13450a94d38fSBill Fenner case DL_UNITDATA_IND: 13460a94d38fSBill Fenner return ("DL_UNITDATA_IND"); 13470a94d38fSBill Fenner 13480a94d38fSBill Fenner case DL_UDERROR_IND: 13490a94d38fSBill Fenner return ("DL_UDERROR_IND"); 13500a94d38fSBill Fenner 13510a94d38fSBill Fenner case DL_UDQOS_REQ: 13520a94d38fSBill Fenner return ("DL_UDQOS_REQ"); 13530a94d38fSBill Fenner 13540a94d38fSBill Fenner case DL_CONNECT_REQ: 13550a94d38fSBill Fenner return ("DL_CONNECT_REQ"); 13560a94d38fSBill Fenner 13570a94d38fSBill Fenner case DL_CONNECT_IND: 13580a94d38fSBill Fenner return ("DL_CONNECT_IND"); 13590a94d38fSBill Fenner 13600a94d38fSBill Fenner case DL_CONNECT_RES: 13610a94d38fSBill Fenner return ("DL_CONNECT_RES"); 13620a94d38fSBill Fenner 13630a94d38fSBill Fenner case DL_CONNECT_CON: 13640a94d38fSBill Fenner return ("DL_CONNECT_CON"); 13650a94d38fSBill Fenner 13660a94d38fSBill Fenner case DL_TOKEN_REQ: 13670a94d38fSBill Fenner return ("DL_TOKEN_REQ"); 13680a94d38fSBill Fenner 13690a94d38fSBill Fenner case DL_TOKEN_ACK: 13700a94d38fSBill Fenner return ("DL_TOKEN_ACK"); 13710a94d38fSBill Fenner 13720a94d38fSBill Fenner case DL_DISCONNECT_REQ: 13730a94d38fSBill Fenner return ("DL_DISCONNECT_REQ"); 13740a94d38fSBill Fenner 13750a94d38fSBill Fenner case DL_DISCONNECT_IND: 13760a94d38fSBill Fenner return ("DL_DISCONNECT_IND"); 13770a94d38fSBill Fenner 13780a94d38fSBill Fenner case DL_RESET_REQ: 13790a94d38fSBill Fenner return ("DL_RESET_REQ"); 13800a94d38fSBill Fenner 13810a94d38fSBill Fenner case DL_RESET_IND: 13820a94d38fSBill Fenner return ("DL_RESET_IND"); 13830a94d38fSBill Fenner 13840a94d38fSBill Fenner case DL_RESET_RES: 13850a94d38fSBill Fenner return ("DL_RESET_RES"); 13860a94d38fSBill Fenner 13870a94d38fSBill Fenner case DL_RESET_CON: 13880a94d38fSBill Fenner return ("DL_RESET_CON"); 13890a94d38fSBill Fenner 13900a94d38fSBill Fenner default: 13910a94d38fSBill Fenner (void) sprintf(primbuf, "unknown primitive 0x%x", prim); 13920a94d38fSBill Fenner return (primbuf); 13930a94d38fSBill Fenner } 13940a94d38fSBill Fenner } 13950a94d38fSBill Fenner 13968cf6c252SPaul Traina static int 13978cf6c252SPaul Traina dlattachreq(int fd, bpf_u_int32 ppa, char *ebuf) 13988cf6c252SPaul Traina { 13998cf6c252SPaul Traina dl_attach_req_t req; 14008cf6c252SPaul Traina 14018cf6c252SPaul Traina req.dl_primitive = DL_ATTACH_REQ; 14028cf6c252SPaul Traina req.dl_ppa = ppa; 14038cf6c252SPaul Traina 14048cf6c252SPaul Traina return (send_request(fd, (char *)&req, sizeof(req), "attach", ebuf)); 14058cf6c252SPaul Traina } 14068cf6c252SPaul Traina 14078cf6c252SPaul Traina static int 14088cf6c252SPaul Traina dlbindreq(int fd, bpf_u_int32 sap, char *ebuf) 14098cf6c252SPaul Traina { 14108cf6c252SPaul Traina 14118cf6c252SPaul Traina dl_bind_req_t req; 14128cf6c252SPaul Traina 14138cf6c252SPaul Traina memset((char *)&req, 0, sizeof(req)); 14148cf6c252SPaul Traina req.dl_primitive = DL_BIND_REQ; 1415ee2dd488SSam Leffler /* XXX - what if neither of these are defined? */ 1416ee2dd488SSam Leffler #if defined(DL_HP_RAWDLS) 14178cf6c252SPaul Traina req.dl_max_conind = 1; /* XXX magic number */ 14188cf6c252SPaul Traina req.dl_service_mode = DL_HP_RAWDLS; 1419ee2dd488SSam Leffler #elif defined(DL_CLDLS) 14203052b236SBill Fenner req.dl_service_mode = DL_CLDLS; 14213052b236SBill Fenner #endif 1422ee2dd488SSam Leffler req.dl_sap = sap; 14238cf6c252SPaul Traina 14248cf6c252SPaul Traina return (send_request(fd, (char *)&req, sizeof(req), "bind", ebuf)); 14258cf6c252SPaul Traina } 14268cf6c252SPaul Traina 14278cf6c252SPaul Traina static int 1428ee2dd488SSam Leffler dlbindack(int fd, char *bufp, char *ebuf, int *uerror) 14298cf6c252SPaul Traina { 14308cf6c252SPaul Traina 1431ee2dd488SSam Leffler return (recv_ack(fd, DL_BIND_ACK_SIZE, "bind", bufp, ebuf, uerror)); 14328cf6c252SPaul Traina } 14338cf6c252SPaul Traina 14348cf6c252SPaul Traina static int 14358cf6c252SPaul Traina dlpromisconreq(int fd, bpf_u_int32 level, char *ebuf) 14368cf6c252SPaul Traina { 14378cf6c252SPaul Traina dl_promiscon_req_t req; 14388cf6c252SPaul Traina 14398cf6c252SPaul Traina req.dl_primitive = DL_PROMISCON_REQ; 14408cf6c252SPaul Traina req.dl_level = level; 14418cf6c252SPaul Traina 14428cf6c252SPaul Traina return (send_request(fd, (char *)&req, sizeof(req), "promiscon", ebuf)); 14438cf6c252SPaul Traina } 14448cf6c252SPaul Traina 14458cf6c252SPaul Traina static int 14468cf6c252SPaul Traina dlokack(int fd, const char *what, char *bufp, char *ebuf) 14478cf6c252SPaul Traina { 14488cf6c252SPaul Traina 1449ee2dd488SSam Leffler return (recv_ack(fd, DL_OK_ACK_SIZE, what, bufp, ebuf, NULL)); 14508cf6c252SPaul Traina } 14518cf6c252SPaul Traina 14528cf6c252SPaul Traina 14538cf6c252SPaul Traina static int 14548cf6c252SPaul Traina dlinforeq(int fd, char *ebuf) 14558cf6c252SPaul Traina { 14568cf6c252SPaul Traina dl_info_req_t req; 14578cf6c252SPaul Traina 14588cf6c252SPaul Traina req.dl_primitive = DL_INFO_REQ; 14598cf6c252SPaul Traina 14608cf6c252SPaul Traina return (send_request(fd, (char *)&req, sizeof(req), "info", ebuf)); 14618cf6c252SPaul Traina } 14628cf6c252SPaul Traina 14638cf6c252SPaul Traina static int 14648cf6c252SPaul Traina dlinfoack(int fd, char *bufp, char *ebuf) 14658cf6c252SPaul Traina { 14668cf6c252SPaul Traina 1467ee2dd488SSam Leffler return (recv_ack(fd, DL_INFO_ACK_SIZE, "info", bufp, ebuf, NULL)); 14688cf6c252SPaul Traina } 14698cf6c252SPaul Traina 147004fb2745SSam Leffler #ifdef DL_HP_RAWDLS 147104fb2745SSam Leffler /* 147204fb2745SSam Leffler * There's an ack *if* there's an error. 147304fb2745SSam Leffler */ 147404fb2745SSam Leffler static int 147504fb2745SSam Leffler dlrawdatareq(int fd, const u_char *datap, int datalen) 147604fb2745SSam Leffler { 147704fb2745SSam Leffler struct strbuf ctl, data; 147804fb2745SSam Leffler long buf[MAXDLBUF]; /* XXX - char? */ 147904fb2745SSam Leffler union DL_primitives *dlp; 148004fb2745SSam Leffler int dlen; 148104fb2745SSam Leffler 148204fb2745SSam Leffler dlp = (union DL_primitives*) buf; 148304fb2745SSam Leffler 148404fb2745SSam Leffler dlp->dl_primitive = DL_HP_RAWDATA_REQ; 148504fb2745SSam Leffler dlen = DL_HP_RAWDATA_REQ_SIZE; 148604fb2745SSam Leffler 148704fb2745SSam Leffler /* 148804fb2745SSam Leffler * HP's documentation doesn't appear to show us supplying any 148904fb2745SSam Leffler * address pointed to by the control part of the message. 149004fb2745SSam Leffler * I think that's what raw mode means - you just send the raw 149104fb2745SSam Leffler * packet, you don't specify where to send it to, as that's 149204fb2745SSam Leffler * implied by the destination address. 149304fb2745SSam Leffler */ 149404fb2745SSam Leffler ctl.maxlen = 0; 149504fb2745SSam Leffler ctl.len = dlen; 149604fb2745SSam Leffler ctl.buf = (void *)buf; 149704fb2745SSam Leffler 149804fb2745SSam Leffler data.maxlen = 0; 149904fb2745SSam Leffler data.len = datalen; 150004fb2745SSam Leffler data.buf = (void *)datap; 150104fb2745SSam Leffler 150204fb2745SSam Leffler return (putmsg(fd, &ctl, &data, 0)); 150304fb2745SSam Leffler } 150404fb2745SSam Leffler #endif /* DL_HP_RAWDLS */ 150504fb2745SSam Leffler 15068cf6c252SPaul Traina #ifdef HAVE_SYS_BUFMOD_H 15078cf6c252SPaul Traina static int 15088cf6c252SPaul Traina strioctl(int fd, int cmd, int len, char *dp) 15098cf6c252SPaul Traina { 15108cf6c252SPaul Traina struct strioctl str; 15118cf6c252SPaul Traina int rc; 15128cf6c252SPaul Traina 15138cf6c252SPaul Traina str.ic_cmd = cmd; 15148cf6c252SPaul Traina str.ic_timout = -1; 15158cf6c252SPaul Traina str.ic_len = len; 15168cf6c252SPaul Traina str.ic_dp = dp; 15178cf6c252SPaul Traina rc = ioctl(fd, I_STR, &str); 15188cf6c252SPaul Traina 15198cf6c252SPaul Traina if (rc < 0) 15208cf6c252SPaul Traina return (rc); 15218cf6c252SPaul Traina else 15228cf6c252SPaul Traina return (str.ic_len); 15238cf6c252SPaul Traina } 15248cf6c252SPaul Traina #endif 15258cf6c252SPaul Traina 15268cf6c252SPaul Traina #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H) 15278cf6c252SPaul Traina static char * 15288cf6c252SPaul Traina get_release(bpf_u_int32 *majorp, bpf_u_int32 *minorp, bpf_u_int32 *microp) 15298cf6c252SPaul Traina { 15308cf6c252SPaul Traina char *cp; 15318cf6c252SPaul Traina static char buf[32]; 15328cf6c252SPaul Traina 15338cf6c252SPaul Traina *majorp = 0; 15348cf6c252SPaul Traina *minorp = 0; 15358cf6c252SPaul Traina *microp = 0; 15368cf6c252SPaul Traina if (sysinfo(SI_RELEASE, buf, sizeof(buf)) < 0) 15378cf6c252SPaul Traina return ("?"); 15388cf6c252SPaul Traina cp = buf; 15390a94d38fSBill Fenner if (!isdigit((unsigned char)*cp)) 15408cf6c252SPaul Traina return (buf); 15418cf6c252SPaul Traina *majorp = strtol(cp, &cp, 10); 15428cf6c252SPaul Traina if (*cp++ != '.') 15438cf6c252SPaul Traina return (buf); 15448cf6c252SPaul Traina *minorp = strtol(cp, &cp, 10); 15458cf6c252SPaul Traina if (*cp++ != '.') 15468cf6c252SPaul Traina return (buf); 15478cf6c252SPaul Traina *microp = strtol(cp, &cp, 10); 15488cf6c252SPaul Traina return (buf); 15498cf6c252SPaul Traina } 15508cf6c252SPaul Traina #endif 15518cf6c252SPaul Traina 155204fb2745SSam Leffler #ifdef DL_HP_PPA_REQ 15538cf6c252SPaul Traina /* 1554dc2c7305SBill Fenner * Under HP-UX 10 and HP-UX 11, we can ask for the ppa 15558cf6c252SPaul Traina */ 15568cf6c252SPaul Traina 15578cf6c252SPaul Traina 1558dc2c7305SBill Fenner /* 1559dc2c7305SBill Fenner * Determine ppa number that specifies ifname. 1560dc2c7305SBill Fenner * 1561dc2c7305SBill Fenner * If the "dl_hp_ppa_info_t" doesn't have a "dl_module_id_1" member, 1562dc2c7305SBill Fenner * the code that's used here is the old code for HP-UX 10.x. 1563dc2c7305SBill Fenner * 1564dc2c7305SBill Fenner * However, HP-UX 10.20, at least, appears to have such a member 1565dc2c7305SBill Fenner * in its "dl_hp_ppa_info_t" structure, so the new code is used. 1566dc2c7305SBill Fenner * The new code didn't work on an old 10.20 system on which Rick 1567dc2c7305SBill Fenner * Jones of HP tried it, but with later patches installed, it 1568dc2c7305SBill Fenner * worked - it appears that the older system had those members but 1569dc2c7305SBill Fenner * didn't put anything in them, so, if the search by name fails, we 1570dc2c7305SBill Fenner * do the old search. 1571dc2c7305SBill Fenner * 1572dc2c7305SBill Fenner * Rick suggests that making sure your system is "up on the latest 1573dc2c7305SBill Fenner * lancommon/DLPI/driver patches" is probably a good idea; it'd fix 1574dc2c7305SBill Fenner * that problem, as well as allowing libpcap to see packets sent 1575dc2c7305SBill Fenner * from the system on which the libpcap application is being run. 1576dc2c7305SBill Fenner * (On 10.20, in addition to getting the latest patches, you need 1577dc2c7305SBill Fenner * to turn the kernel "lanc_outbound_promisc_flag" flag on with ADB; 1578dc2c7305SBill Fenner * a posting to "comp.sys.hp.hpux" at 1579dc2c7305SBill Fenner * 1580dc2c7305SBill Fenner * http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=558092266 1581dc2c7305SBill Fenner * 1582dc2c7305SBill Fenner * says that, to see the machine's outgoing traffic, you'd need to 1583dc2c7305SBill Fenner * apply the right patches to your system, and also set that variable 1584dc2c7305SBill Fenner * with: 1585dc2c7305SBill Fenner 1586dc2c7305SBill Fenner echo 'lanc_outbound_promisc_flag/W1' | /usr/bin/adb -w /stand/vmunix /dev/kmem 1587dc2c7305SBill Fenner 1588dc2c7305SBill Fenner * which could be put in, for example, "/sbin/init.d/lan". 1589dc2c7305SBill Fenner * 1590dc2c7305SBill Fenner * Setting the variable is not necessary on HP-UX 11.x. 1591dc2c7305SBill Fenner */ 15928cf6c252SPaul Traina static int 15938cf6c252SPaul Traina get_dlpi_ppa(register int fd, register const char *device, register int unit, 15948cf6c252SPaul Traina register char *ebuf) 15958cf6c252SPaul Traina { 15968cf6c252SPaul Traina register dl_hp_ppa_ack_t *ap; 1597dc2c7305SBill Fenner register dl_hp_ppa_info_t *ipstart, *ip; 15988cf6c252SPaul Traina register int i; 1599dc2c7305SBill Fenner char dname[100]; 16008cf6c252SPaul Traina register u_long majdev; 16018cf6c252SPaul Traina struct stat statbuf; 1602dc2c7305SBill Fenner dl_hp_ppa_req_t req; 16030a94d38fSBill Fenner char buf[MAXDLBUF]; 16040a94d38fSBill Fenner char *ppa_data_buf; 16050a94d38fSBill Fenner dl_hp_ppa_ack_t *dlp; 16060a94d38fSBill Fenner struct strbuf ctl; 16070a94d38fSBill Fenner int flags; 16080a94d38fSBill Fenner int ppa; 16098cf6c252SPaul Traina 16108cf6c252SPaul Traina memset((char *)&req, 0, sizeof(req)); 16118cf6c252SPaul Traina req.dl_primitive = DL_HP_PPA_REQ; 16128cf6c252SPaul Traina 16138cf6c252SPaul Traina memset((char *)buf, 0, sizeof(buf)); 16140a94d38fSBill Fenner if (send_request(fd, (char *)&req, sizeof(req), "hpppa", ebuf) < 0) 16158cf6c252SPaul Traina return (-1); 16168cf6c252SPaul Traina 16170a94d38fSBill Fenner ctl.maxlen = DL_HP_PPA_ACK_SIZE; 16180a94d38fSBill Fenner ctl.len = 0; 16190a94d38fSBill Fenner ctl.buf = (char *)buf; 16200a94d38fSBill Fenner 16210a94d38fSBill Fenner flags = 0; 16220a94d38fSBill Fenner /* 16230a94d38fSBill Fenner * DLPI may return a big chunk of data for a DL_HP_PPA_REQ. The normal 16240a94d38fSBill Fenner * recv_ack will fail because it set the maxlen to MAXDLBUF (8192) 16250a94d38fSBill Fenner * which is NOT big enough for a DL_HP_PPA_REQ. 16260a94d38fSBill Fenner * 16270a94d38fSBill Fenner * This causes libpcap applications to fail on a system with HP-APA 16280a94d38fSBill Fenner * installed. 16290a94d38fSBill Fenner * 16300a94d38fSBill Fenner * To figure out how big the returned data is, we first call getmsg 16310a94d38fSBill Fenner * to get the small head and peek at the head to get the actual data 16320a94d38fSBill Fenner * length, and then issue another getmsg to get the actual PPA data. 16330a94d38fSBill Fenner */ 16340a94d38fSBill Fenner /* get the head first */ 16350a94d38fSBill Fenner if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) { 16360a94d38fSBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, 16370a94d38fSBill Fenner "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno)); 16380a94d38fSBill Fenner return (-1); 16390a94d38fSBill Fenner } 16400a94d38fSBill Fenner 16410a94d38fSBill Fenner dlp = (dl_hp_ppa_ack_t *)ctl.buf; 16420a94d38fSBill Fenner if (dlp->dl_primitive != DL_HP_PPA_ACK) { 16430a94d38fSBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, 16440a94d38fSBill Fenner "get_dlpi_ppa: hpppa unexpected primitive ack 0x%x", 16450a94d38fSBill Fenner (bpf_u_int32)dlp->dl_primitive); 16460a94d38fSBill Fenner return (-1); 16470a94d38fSBill Fenner } 16480a94d38fSBill Fenner 16490a94d38fSBill Fenner if (ctl.len < DL_HP_PPA_ACK_SIZE) { 16500a94d38fSBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, 1651feb4ecdbSBruce M Simpson "get_dlpi_ppa: hpppa ack too small (%d < %lu)", 1652feb4ecdbSBruce M Simpson ctl.len, (unsigned long)DL_HP_PPA_ACK_SIZE); 16530a94d38fSBill Fenner return (-1); 16540a94d38fSBill Fenner } 16550a94d38fSBill Fenner 16560a94d38fSBill Fenner /* allocate buffer */ 16570a94d38fSBill Fenner if ((ppa_data_buf = (char *)malloc(dlp->dl_length)) == NULL) { 16580a94d38fSBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, 16590a94d38fSBill Fenner "get_dlpi_ppa: hpppa malloc: %s", pcap_strerror(errno)); 16600a94d38fSBill Fenner return (-1); 16610a94d38fSBill Fenner } 16620a94d38fSBill Fenner ctl.maxlen = dlp->dl_length; 16630a94d38fSBill Fenner ctl.len = 0; 16640a94d38fSBill Fenner ctl.buf = (char *)ppa_data_buf; 16650a94d38fSBill Fenner /* get the data */ 16660a94d38fSBill Fenner if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) { 16670a94d38fSBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, 16680a94d38fSBill Fenner "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno)); 16690a94d38fSBill Fenner free(ppa_data_buf); 16700a94d38fSBill Fenner return (-1); 16710a94d38fSBill Fenner } 16720a94d38fSBill Fenner if (ctl.len < dlp->dl_length) { 16730a94d38fSBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, 16740a94d38fSBill Fenner "get_dlpi_ppa: hpppa ack too small (%d < %d)", 16750a94d38fSBill Fenner ctl.len, dlp->dl_length); 16760a94d38fSBill Fenner free(ppa_data_buf); 16770a94d38fSBill Fenner return (-1); 16780a94d38fSBill Fenner } 16790a94d38fSBill Fenner 16808cf6c252SPaul Traina ap = (dl_hp_ppa_ack_t *)buf; 16810a94d38fSBill Fenner ipstart = (dl_hp_ppa_info_t *)ppa_data_buf; 1682dc2c7305SBill Fenner ip = ipstart; 16838cf6c252SPaul Traina 1684dc2c7305SBill Fenner #ifdef HAVE_HP_PPA_INFO_T_DL_MODULE_ID_1 1685dc2c7305SBill Fenner /* 1686dc2c7305SBill Fenner * The "dl_hp_ppa_info_t" structure has a "dl_module_id_1" 1687dc2c7305SBill Fenner * member that should, in theory, contain the part of the 1688dc2c7305SBill Fenner * name for the device that comes before the unit number, 1689dc2c7305SBill Fenner * and should also have a "dl_module_id_2" member that may 1690dc2c7305SBill Fenner * contain an alternate name (e.g., I think Ethernet devices 1691dc2c7305SBill Fenner * have both "lan", for "lanN", and "snap", for "snapN", with 1692dc2c7305SBill Fenner * the former being for Ethernet packets and the latter being 1693dc2c7305SBill Fenner * for 802.3/802.2 packets). 1694dc2c7305SBill Fenner * 1695dc2c7305SBill Fenner * Search for the device that has the specified name and 1696dc2c7305SBill Fenner * instance number. 1697dc2c7305SBill Fenner */ 16988cf6c252SPaul Traina for (i = 0; i < ap->dl_count; i++) { 16990a94d38fSBill Fenner if ((strcmp((const char *)ip->dl_module_id_1, device) == 0 || 17000a94d38fSBill Fenner strcmp((const char *)ip->dl_module_id_2, device) == 0) && 1701dc2c7305SBill Fenner ip->dl_instance_num == unit) 17028cf6c252SPaul Traina break; 17038cf6c252SPaul Traina 1704dc2c7305SBill Fenner ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset); 1705dc2c7305SBill Fenner } 1706dc2c7305SBill Fenner #else 1707dc2c7305SBill Fenner /* 1708dc2c7305SBill Fenner * We don't have that member, so the search is impossible; make it 1709dc2c7305SBill Fenner * look as if the search failed. 1710dc2c7305SBill Fenner */ 1711dc2c7305SBill Fenner i = ap->dl_count; 1712dc2c7305SBill Fenner #endif 1713dc2c7305SBill Fenner 1714dc2c7305SBill Fenner if (i == ap->dl_count) { 1715dc2c7305SBill Fenner /* 1716dc2c7305SBill Fenner * Well, we didn't, or can't, find the device by name. 1717dc2c7305SBill Fenner * 1718dc2c7305SBill Fenner * HP-UX 10.20, whilst it has "dl_module_id_1" and 1719dc2c7305SBill Fenner * "dl_module_id_2" fields in the "dl_hp_ppa_info_t", 1720dc2c7305SBill Fenner * doesn't seem to fill them in unless the system is 1721dc2c7305SBill Fenner * at a reasonably up-to-date patch level. 1722dc2c7305SBill Fenner * 1723dc2c7305SBill Fenner * Older HP-UX 10.x systems might not have those fields 1724dc2c7305SBill Fenner * at all. 1725dc2c7305SBill Fenner * 1726dc2c7305SBill Fenner * Therefore, we'll search for the entry with the major 1727dc2c7305SBill Fenner * device number of a device with the name "/dev/<dev><unit>", 1728dc2c7305SBill Fenner * if such a device exists, as the old code did. 1729dc2c7305SBill Fenner */ 1730dc2c7305SBill Fenner snprintf(dname, sizeof(dname), "/dev/%s%d", device, unit); 1731dc2c7305SBill Fenner if (stat(dname, &statbuf) < 0) { 1732dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, "stat: %s: %s", 1733dc2c7305SBill Fenner dname, pcap_strerror(errno)); 1734dc2c7305SBill Fenner return (-1); 1735dc2c7305SBill Fenner } 1736dc2c7305SBill Fenner majdev = major(statbuf.st_rdev); 1737dc2c7305SBill Fenner 1738dc2c7305SBill Fenner ip = ipstart; 1739dc2c7305SBill Fenner 1740dc2c7305SBill Fenner for (i = 0; i < ap->dl_count; i++) { 1741dc2c7305SBill Fenner if (ip->dl_mjr_num == majdev && 1742dc2c7305SBill Fenner ip->dl_instance_num == unit) 1743dc2c7305SBill Fenner break; 1744dc2c7305SBill Fenner 1745dc2c7305SBill Fenner ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset); 1746dc2c7305SBill Fenner } 17478cf6c252SPaul Traina } 17488cf6c252SPaul Traina if (i == ap->dl_count) { 1749dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, 1750dc2c7305SBill Fenner "can't find /dev/dlpi PPA for %s%d", device, unit); 17518cf6c252SPaul Traina return (-1); 17528cf6c252SPaul Traina } 17538cf6c252SPaul Traina if (ip->dl_hdw_state == HDW_DEAD) { 1754dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, 1755dc2c7305SBill Fenner "%s%d: hardware state: DOWN\n", device, unit); 17560a94d38fSBill Fenner free(ppa_data_buf); 17578cf6c252SPaul Traina return (-1); 17588cf6c252SPaul Traina } 17590a94d38fSBill Fenner ppa = ip->dl_ppa; 17600a94d38fSBill Fenner free(ppa_data_buf); 17610a94d38fSBill Fenner return (ppa); 17628cf6c252SPaul Traina } 17638cf6c252SPaul Traina #endif 17648cf6c252SPaul Traina 17658cf6c252SPaul Traina #ifdef HAVE_HPUX9 17668cf6c252SPaul Traina /* 17678cf6c252SPaul Traina * Under HP-UX 9, there is no good way to determine the ppa. 17688cf6c252SPaul Traina * So punt and read it from /dev/kmem. 17698cf6c252SPaul Traina */ 17708cf6c252SPaul Traina static struct nlist nl[] = { 17718cf6c252SPaul Traina #define NL_IFNET 0 17728cf6c252SPaul Traina { "ifnet" }, 17738cf6c252SPaul Traina { "" } 17748cf6c252SPaul Traina }; 17758cf6c252SPaul Traina 17768cf6c252SPaul Traina static char path_vmunix[] = "/hp-ux"; 17778cf6c252SPaul Traina 17788cf6c252SPaul Traina /* Determine ppa number that specifies ifname */ 17798cf6c252SPaul Traina static int 17808cf6c252SPaul Traina get_dlpi_ppa(register int fd, register const char *ifname, register int unit, 17818cf6c252SPaul Traina register char *ebuf) 17828cf6c252SPaul Traina { 17838cf6c252SPaul Traina register const char *cp; 17848cf6c252SPaul Traina register int kd; 17858cf6c252SPaul Traina void *addr; 17868cf6c252SPaul Traina struct ifnet ifnet; 1787dc2c7305SBill Fenner char if_name[sizeof(ifnet.if_name) + 1]; 17888cf6c252SPaul Traina 17898cf6c252SPaul Traina cp = strrchr(ifname, '/'); 17908cf6c252SPaul Traina if (cp != NULL) 17918cf6c252SPaul Traina ifname = cp + 1; 17928cf6c252SPaul Traina if (nlist(path_vmunix, &nl) < 0) { 1793dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, "nlist %s failed", 1794dc2c7305SBill Fenner path_vmunix); 17958cf6c252SPaul Traina return (-1); 17968cf6c252SPaul Traina } 17978cf6c252SPaul Traina if (nl[NL_IFNET].n_value == 0) { 1798dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, 1799dc2c7305SBill Fenner "could't find %s kernel symbol", 18008cf6c252SPaul Traina nl[NL_IFNET].n_name); 18018cf6c252SPaul Traina return (-1); 18028cf6c252SPaul Traina } 18038cf6c252SPaul Traina kd = open("/dev/kmem", O_RDONLY); 18048cf6c252SPaul Traina if (kd < 0) { 1805dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, "kmem open: %s", 1806dc2c7305SBill Fenner pcap_strerror(errno)); 18078cf6c252SPaul Traina return (-1); 18088cf6c252SPaul Traina } 18098cf6c252SPaul Traina if (dlpi_kread(kd, nl[NL_IFNET].n_value, 18108cf6c252SPaul Traina &addr, sizeof(addr), ebuf) < 0) { 18118cf6c252SPaul Traina close(kd); 18128cf6c252SPaul Traina return (-1); 18138cf6c252SPaul Traina } 18148cf6c252SPaul Traina for (; addr != NULL; addr = ifnet.if_next) { 18158cf6c252SPaul Traina if (dlpi_kread(kd, (off_t)addr, 18168cf6c252SPaul Traina &ifnet, sizeof(ifnet), ebuf) < 0 || 18178cf6c252SPaul Traina dlpi_kread(kd, (off_t)ifnet.if_name, 1818dc2c7305SBill Fenner if_name, sizeof(ifnet.if_name), ebuf) < 0) { 18198cf6c252SPaul Traina (void)close(kd); 18208cf6c252SPaul Traina return (-1); 18218cf6c252SPaul Traina } 1822dc2c7305SBill Fenner if_name[sizeof(ifnet.if_name)] = '\0'; 1823dc2c7305SBill Fenner if (strcmp(if_name, ifname) == 0 && ifnet.if_unit == unit) 18248cf6c252SPaul Traina return (ifnet.if_index); 18258cf6c252SPaul Traina } 18268cf6c252SPaul Traina 1827dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, "Can't find %s", ifname); 18288cf6c252SPaul Traina return (-1); 18298cf6c252SPaul Traina } 18308cf6c252SPaul Traina 18318cf6c252SPaul Traina static int 18328cf6c252SPaul Traina dlpi_kread(register int fd, register off_t addr, 18338cf6c252SPaul Traina register void *buf, register u_int len, register char *ebuf) 18348cf6c252SPaul Traina { 18358cf6c252SPaul Traina register int cc; 18368cf6c252SPaul Traina 1837a4b5b39fSBill Fenner if (lseek(fd, addr, SEEK_SET) < 0) { 1838dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, "lseek: %s", 1839dc2c7305SBill Fenner pcap_strerror(errno)); 18408cf6c252SPaul Traina return (-1); 18418cf6c252SPaul Traina } 18428cf6c252SPaul Traina cc = read(fd, buf, len); 18438cf6c252SPaul Traina if (cc < 0) { 1844dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, "read: %s", 1845dc2c7305SBill Fenner pcap_strerror(errno)); 18468cf6c252SPaul Traina return (-1); 18478cf6c252SPaul Traina } else if (cc != len) { 1848dc2c7305SBill Fenner snprintf(ebuf, PCAP_ERRBUF_SIZE, "short read (%d != %d)", cc, 1849dc2c7305SBill Fenner len); 18508cf6c252SPaul Traina return (-1); 18518cf6c252SPaul Traina } 18528cf6c252SPaul Traina return (cc); 18538cf6c252SPaul Traina } 18548cf6c252SPaul Traina #endif 1855