xref: /freebsd/contrib/libpcap/pcap-dlpi.c (revision afdbf109c6a661a729938f68211054a0a50d38ac)
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>,
25a8e07101SRui Paulo  * Mark C. Brown (mbrown@hp.com), and Sagun Shakya <Sagun.Shakya@Sun.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 
71b00ab754SHans Petter Selasky #include <config.h>
723052b236SBill Fenner 
738cf6c252SPaul Traina #include <sys/types.h>
748cf6c252SPaul Traina #include <sys/time.h>
758cf6c252SPaul Traina #ifdef HAVE_SYS_BUFMOD_H
768cf6c252SPaul Traina #include <sys/bufmod.h>
778cf6c252SPaul Traina #endif
788cf6c252SPaul Traina #include <sys/dlpi.h>
798cf6c252SPaul Traina #ifdef HAVE_SYS_DLPI_EXT_H
808cf6c252SPaul Traina #include <sys/dlpi_ext.h>
818cf6c252SPaul Traina #endif
828cf6c252SPaul Traina #ifdef HAVE_HPUX9
838cf6c252SPaul Traina #include <sys/socket.h>
848cf6c252SPaul Traina #endif
8504fb2745SSam Leffler #ifdef DL_HP_PPA_REQ
868cf6c252SPaul Traina #include <sys/stat.h>
878cf6c252SPaul Traina #endif
888cf6c252SPaul Traina #include <sys/stream.h>
898cf6c252SPaul Traina #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
908cf6c252SPaul Traina #include <sys/systeminfo.h>
918cf6c252SPaul Traina #endif
928cf6c252SPaul Traina 
938cf6c252SPaul Traina #ifdef HAVE_HPUX9
948cf6c252SPaul Traina #include <net/if.h>
958cf6c252SPaul Traina #endif
968cf6c252SPaul Traina 
978cf6c252SPaul Traina #ifdef HAVE_HPUX9
988cf6c252SPaul Traina #include <nlist.h>
998cf6c252SPaul Traina #endif
1008cf6c252SPaul Traina #include <errno.h>
1018cf6c252SPaul Traina #include <fcntl.h>
1028cf6c252SPaul Traina #include <memory.h>
1038cf6c252SPaul Traina #include <stdio.h>
1048cf6c252SPaul Traina #include <stdlib.h>
1058cf6c252SPaul Traina #include <string.h>
1068cf6c252SPaul Traina #include <stropts.h>
1078cf6c252SPaul Traina #include <unistd.h>
10804fb2745SSam Leffler #include <limits.h>
10904fb2745SSam Leffler 
1108cf6c252SPaul Traina #include "pcap-int.h"
111a8e07101SRui Paulo #include "dlpisubs.h"
1128cf6c252SPaul Traina 
1138cf6c252SPaul Traina #ifdef HAVE_OS_PROTO_H
1148cf6c252SPaul Traina #include "os-proto.h"
1158cf6c252SPaul Traina #endif
1168cf6c252SPaul Traina 
117b00ab754SHans Petter Selasky #if defined(__hpux)
118b00ab754SHans Petter Selasky   /*
119b00ab754SHans Petter Selasky    * HP-UX has a /dev/dlpi device; you open it and set the PPA of the actual
120b00ab754SHans Petter Selasky    * network device you want.
121b00ab754SHans Petter Selasky    */
122b00ab754SHans Petter Selasky   #define HAVE_DEV_DLPI
123b00ab754SHans Petter Selasky #elif defined(_AIX)
124b00ab754SHans Petter Selasky   /*
125b00ab754SHans Petter Selasky    * AIX has a /dev/dlpi directory, with devices named after the interfaces
126b00ab754SHans Petter Selasky    * underneath it.
127b00ab754SHans Petter Selasky    */
1280a94d38fSBill Fenner   #define PCAP_DEV_PREFIX "/dev/dlpi"
129b00ab754SHans Petter Selasky #elif defined(HAVE_SOLARIS)
130b00ab754SHans Petter Selasky   /*
131b00ab754SHans Petter Selasky    * Solaris has devices named after the interfaces underneath /dev.
132b00ab754SHans Petter Selasky    */
1338cf6c252SPaul Traina   #define PCAP_DEV_PREFIX "/dev"
1348cf6c252SPaul Traina #endif
1358cf6c252SPaul Traina 
1368cf6c252SPaul Traina #define	MAXDLBUF	8192
1378cf6c252SPaul Traina 
1388cf6c252SPaul Traina /* Forwards */
139b00ab754SHans Petter Selasky static char *split_dname(char *, u_int *, char *);
14004fb2745SSam Leffler static int dl_doattach(int, int, char *);
141ee2dd488SSam Leffler #ifdef DL_HP_RAWDLS
142ee2dd488SSam Leffler static int dl_dohpuxbind(int, char *);
143ee2dd488SSam Leffler #endif
144d1e87331SXin LI static int dlpromiscon(pcap_t *, bpf_u_int32);
1458cf6c252SPaul Traina static int dlbindreq(int, bpf_u_int32, char *);
146ee2dd488SSam Leffler static int dlbindack(int, char *, char *, int *);
1476f9cba8fSJoseph Mingrone static int dlokack(int, const char *, char *, char *, int *);
14804fb2745SSam Leffler static int dlinforeq(int, char *);
14904fb2745SSam Leffler static int dlinfoack(int, char *, char *);
150a8e07101SRui Paulo 
151b00ab754SHans Petter Selasky #ifdef HAVE_DL_PASSIVE_REQ_T
152a8e07101SRui Paulo static void dlpassive(int, char *);
153a8e07101SRui Paulo #endif
154a8e07101SRui Paulo 
15504fb2745SSam Leffler #ifdef DL_HP_RAWDLS
15604fb2745SSam Leffler static int dlrawdatareq(int, const u_char *, int);
15704fb2745SSam Leffler #endif
158ee2dd488SSam Leffler static int recv_ack(int, int, const char *, char *, char *, int *);
159ada6f083SXin LI static char *dlstrerror(char *, size_t, bpf_u_int32);
160ada6f083SXin LI static char *dlprim(char *, size_t, bpf_u_int32);
1618cf6c252SPaul Traina #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
162ada6f083SXin LI #define GET_RELEASE_BUFSIZE	32
163ada6f083SXin LI static void get_release(char *, size_t, bpf_u_int32 *, bpf_u_int32 *,
164ada6f083SXin LI     bpf_u_int32 *);
1658cf6c252SPaul Traina #endif
1668cf6c252SPaul Traina static int send_request(int, char *, int, char *, char *);
1678cf6c252SPaul Traina #ifdef HAVE_HPUX9
1688cf6c252SPaul Traina static int dlpi_kread(int, off_t, void *, u_int, char *);
1698cf6c252SPaul Traina #endif
1708cf6c252SPaul Traina #ifdef HAVE_DEV_DLPI
171b00ab754SHans Petter Selasky static int get_dlpi_ppa(int, const char *, u_int, u_int *, char *);
1728cf6c252SPaul Traina #endif
1738cf6c252SPaul Traina 
174a0ee43a1SRui Paulo /*
175a0ee43a1SRui Paulo  * Cast a buffer to "union DL_primitives" without provoking warnings
176a0ee43a1SRui Paulo  * from the compiler.
177a0ee43a1SRui Paulo  */
178a0ee43a1SRui Paulo #define MAKE_DL_PRIMITIVES(ptr)	((union DL_primitives *)(void *)(ptr))
179a0ee43a1SRui Paulo 
180feb4ecdbSBruce M Simpson static int
pcap_read_dlpi(pcap_t * p,int cnt,pcap_handler callback,u_char * user)181feb4ecdbSBruce M Simpson pcap_read_dlpi(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
1828cf6c252SPaul Traina {
183a8e07101SRui Paulo 	int cc;
184a8e07101SRui Paulo 	u_char *bp;
1858cf6c252SPaul Traina 	int flags;
186ada6f083SXin LI 	bpf_u_int32 ctlbuf[MAXDLBUF];
187ada6f083SXin LI 	struct strbuf ctl = {
188ada6f083SXin LI 		MAXDLBUF,
189ada6f083SXin LI 		0,
190ada6f083SXin LI 		(char *)ctlbuf
191ada6f083SXin LI 	};
1928cf6c252SPaul Traina 	struct strbuf data;
1938cf6c252SPaul Traina 
1948cf6c252SPaul Traina 	flags = 0;
1958cf6c252SPaul Traina 	cc = p->cc;
1968cf6c252SPaul Traina 	if (cc == 0) {
1978cf6c252SPaul Traina 		data.buf = (char *)p->buffer + p->offset;
198feb4ecdbSBruce M Simpson 		data.maxlen = p->bufsize;
1998cf6c252SPaul Traina 		data.len = 0;
2008cf6c252SPaul Traina 		do {
201feb4ecdbSBruce M Simpson 			/*
202feb4ecdbSBruce M Simpson 			 * Has "pcap_breakloop()" been called?
203feb4ecdbSBruce M Simpson 			 */
204feb4ecdbSBruce M Simpson 			if (p->break_loop) {
205feb4ecdbSBruce M Simpson 				/*
206feb4ecdbSBruce M Simpson 				 * Yes - clear the flag that indicates
207feb4ecdbSBruce M Simpson 				 * that it has, and return -2 to
208feb4ecdbSBruce M Simpson 				 * indicate that we were told to
209feb4ecdbSBruce M Simpson 				 * break out of the loop.
210feb4ecdbSBruce M Simpson 				 */
211feb4ecdbSBruce M Simpson 				p->break_loop = 0;
212feb4ecdbSBruce M Simpson 				return (-2);
213feb4ecdbSBruce M Simpson 			}
21404fb2745SSam Leffler 			/*
21504fb2745SSam Leffler 			 * XXX - check for the DLPI primitive, which
21604fb2745SSam Leffler 			 * would be DL_HP_RAWDATA_IND on HP-UX
21704fb2745SSam Leffler 			 * if we're in raw mode?
21804fb2745SSam Leffler 			 */
219ada6f083SXin LI 			ctl.buf = (char *)ctlbuf;
220ada6f083SXin LI 			ctl.maxlen = MAXDLBUF;
221ada6f083SXin LI 			ctl.len = 0;
2228cf6c252SPaul Traina 			if (getmsg(p->fd, &ctl, &data, &flags) < 0) {
2238cf6c252SPaul Traina 				/* Don't choke when we get ptraced */
22404fb2745SSam Leffler 				switch (errno) {
22504fb2745SSam Leffler 
22604fb2745SSam Leffler 				case EINTR:
2278cf6c252SPaul Traina 					cc = 0;
2288cf6c252SPaul Traina 					continue;
22904fb2745SSam Leffler 
23004fb2745SSam Leffler 				case EAGAIN:
23104fb2745SSam Leffler 					return (0);
2328cf6c252SPaul Traina 				}
233*afdbf109SJoseph Mingrone 				pcapint_fmt_errmsg_for_errno(p->errbuf,
234b00ab754SHans Petter Selasky 				    sizeof(p->errbuf), errno, "getmsg");
2358cf6c252SPaul Traina 				return (-1);
2368cf6c252SPaul Traina 			}
2378cf6c252SPaul Traina 			cc = data.len;
2388cf6c252SPaul Traina 		} while (cc == 0);
239ada6f083SXin LI 		bp = (u_char *)p->buffer + p->offset;
2408cf6c252SPaul Traina 	} else
2418cf6c252SPaul Traina 		bp = p->bp;
2428cf6c252SPaul Traina 
243a8e07101SRui Paulo 	return (pcap_process_pkts(p, callback, user, cnt, bp, cc));
2448cf6c252SPaul Traina }
2458cf6c252SPaul Traina 
24604fb2745SSam Leffler static int
pcap_inject_dlpi(pcap_t * p,const void * buf,int size)2476f9cba8fSJoseph Mingrone pcap_inject_dlpi(pcap_t *p, const void *buf, int size)
24804fb2745SSam Leffler {
249681ed54cSXin LI #ifdef DL_HP_RAWDLS
250681ed54cSXin LI 	struct pcap_dlpi *pd = p->priv;
251681ed54cSXin LI #endif
25204fb2745SSam Leffler 	int ret;
25304fb2745SSam Leffler 
25404fb2745SSam Leffler #if defined(DLIOCRAW)
25504fb2745SSam Leffler 	ret = write(p->fd, buf, size);
25604fb2745SSam Leffler 	if (ret == -1) {
257*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
258b00ab754SHans Petter Selasky 		    errno, "send");
25904fb2745SSam Leffler 		return (-1);
26004fb2745SSam Leffler 	}
26104fb2745SSam Leffler #elif defined(DL_HP_RAWDLS)
262681ed54cSXin LI 	if (pd->send_fd < 0) {
2636f9cba8fSJoseph Mingrone 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
26404fb2745SSam Leffler 		    "send: Output FD couldn't be opened");
26504fb2745SSam Leffler 		return (-1);
26604fb2745SSam Leffler 	}
267681ed54cSXin LI 	ret = dlrawdatareq(pd->send_fd, buf, size);
26804fb2745SSam Leffler 	if (ret == -1) {
269*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
270b00ab754SHans Petter Selasky 		    errno, "send");
27104fb2745SSam Leffler 		return (-1);
27204fb2745SSam Leffler 	}
273ef96d74fSMax Laier 	/*
274ef96d74fSMax Laier 	 * putmsg() returns either 0 or -1; it doesn't indicate how
275ef96d74fSMax Laier 	 * many bytes were written (presumably they were all written
276ef96d74fSMax Laier 	 * or none of them were written).  OpenBSD's pcap_inject()
277ef96d74fSMax Laier 	 * returns the number of bytes written, so, for API compatibility,
278ef96d74fSMax Laier 	 * we return the number of bytes we were told to write.
279ef96d74fSMax Laier 	 */
280ef96d74fSMax Laier 	ret = size;
28104fb2745SSam Leffler #else /* no raw mode */
28204fb2745SSam Leffler 	/*
28304fb2745SSam Leffler 	 * XXX - this is a pain, because you might have to extract
28404fb2745SSam Leffler 	 * the address from the packet and use it in a DL_UNITDATA_REQ
28504fb2745SSam Leffler 	 * request.  That would be dependent on the link-layer type.
28604fb2745SSam Leffler 	 *
28704fb2745SSam Leffler 	 * I also don't know what SAP you'd have to bind the descriptor
28804fb2745SSam Leffler 	 * to, or whether you'd need separate "receive" and "send" FDs,
28904fb2745SSam Leffler 	 * nor do I know whether you'd need different bindings for
29004fb2745SSam Leffler 	 * D/I/X Ethernet and 802.3, or for {FDDI,Token Ring} plus
29104fb2745SSam Leffler 	 * 802.2 and {FDDI,Token Ring} plus 802.2 plus SNAP.
29204fb2745SSam Leffler 	 *
29304fb2745SSam Leffler 	 * So, for now, we just return a "you can't send" indication,
29404fb2745SSam Leffler 	 * and leave it up to somebody with a DLPI-based system lacking
29504fb2745SSam Leffler 	 * both DLIOCRAW and DL_HP_RAWDLS to supply code to implement
29604fb2745SSam Leffler 	 * packet transmission on that system.  If they do, they should
29704fb2745SSam Leffler 	 * send it to us - but should not send us code that assumes
29804fb2745SSam Leffler 	 * Ethernet; if the code doesn't work on non-Ethernet interfaces,
29904fb2745SSam Leffler 	 * it should check "p->linktype" and reject the send request if
30004fb2745SSam Leffler 	 * it's anything other than DLT_EN10MB.
30104fb2745SSam Leffler 	 */
302*afdbf109SJoseph Mingrone 	pcapint_strlcpy(p->errbuf, "send: Not supported on this version of this OS",
30304fb2745SSam Leffler 	    PCAP_ERRBUF_SIZE);
30404fb2745SSam Leffler 	ret = -1;
30504fb2745SSam Leffler #endif /* raw mode */
30604fb2745SSam Leffler 	return (ret);
30704fb2745SSam Leffler }
30804fb2745SSam Leffler 
309feb4ecdbSBruce M Simpson #ifndef DL_IPATM
310feb4ecdbSBruce M Simpson #define DL_IPATM	0x12	/* ATM Classical IP interface */
311feb4ecdbSBruce M Simpson #endif
312feb4ecdbSBruce M Simpson 
313feb4ecdbSBruce M Simpson #ifdef HAVE_SOLARIS
314feb4ecdbSBruce M Simpson /*
315feb4ecdbSBruce M Simpson  * For SunATM.
316feb4ecdbSBruce M Simpson  */
317feb4ecdbSBruce M Simpson #ifndef A_GET_UNITS
318feb4ecdbSBruce M Simpson #define A_GET_UNITS	(('A'<<8)|118)
319feb4ecdbSBruce M Simpson #endif /* A_GET_UNITS */
320feb4ecdbSBruce M Simpson #ifndef A_PROMISCON_REQ
321feb4ecdbSBruce M Simpson #define A_PROMISCON_REQ	(('A'<<8)|121)
322feb4ecdbSBruce M Simpson #endif /* A_PROMISCON_REQ */
323feb4ecdbSBruce M Simpson #endif /* HAVE_SOLARIS */
324feb4ecdbSBruce M Simpson 
325feb4ecdbSBruce M Simpson static void
pcap_cleanup_dlpi(pcap_t * p)326a8e07101SRui Paulo pcap_cleanup_dlpi(pcap_t *p)
327feb4ecdbSBruce M Simpson {
328681ed54cSXin LI #ifdef DL_HP_RAWDLS
329681ed54cSXin LI 	struct pcap_dlpi *pd = p->priv;
330681ed54cSXin LI 
331681ed54cSXin LI 	if (pd->send_fd >= 0) {
332681ed54cSXin LI 		close(pd->send_fd);
333681ed54cSXin LI 		pd->send_fd = -1;
334a8e07101SRui Paulo 	}
335681ed54cSXin LI #endif
336*afdbf109SJoseph Mingrone 	pcapint_cleanup_live_common(p);
337feb4ecdbSBruce M Simpson }
338feb4ecdbSBruce M Simpson 
339a8e07101SRui Paulo static int
open_dlpi_device(const char * name,u_int * ppa,char * errbuf)340b00ab754SHans Petter Selasky open_dlpi_device(const char *name, u_int *ppa, char *errbuf)
3418cf6c252SPaul Traina {
342ada6f083SXin LI 	int status;
3438cf6c252SPaul Traina 	char dname[100];
344ada6f083SXin LI 	char *cp;
345ada6f083SXin LI 	int fd;
346b00ab754SHans Petter Selasky #ifdef HAVE_DEV_DLPI
347b00ab754SHans Petter Selasky 	u_int unit;
348b00ab754SHans Petter Selasky #else
3498cf6c252SPaul Traina 	char dname2[100];
3508cf6c252SPaul Traina #endif
3518cf6c252SPaul Traina 
352dc2c7305SBill Fenner #ifdef HAVE_DEV_DLPI
353dc2c7305SBill Fenner 	/*
354dc2c7305SBill Fenner 	** Remove any "/dev/" on the front of the device.
355dc2c7305SBill Fenner 	*/
356ada6f083SXin LI 	cp = strrchr(name, '/');
357dc2c7305SBill Fenner 	if (cp == NULL)
358*afdbf109SJoseph Mingrone 		pcapint_strlcpy(dname, name, sizeof(dname));
359dc2c7305SBill Fenner 	else
360*afdbf109SJoseph Mingrone 		pcapint_strlcpy(dname, cp + 1, sizeof(dname));
361dc2c7305SBill Fenner 
362dc2c7305SBill Fenner 	/*
3630a94d38fSBill Fenner 	 * Split the device name into a device type name and a unit number;
3640a94d38fSBill Fenner 	 * chop off the unit number, so "dname" is just a device type name.
365dc2c7305SBill Fenner 	 */
366b00ab754SHans Petter Selasky 	cp = split_dname(dname, &unit, errbuf);
3676f9cba8fSJoseph Mingrone 	if (cp == NULL) {
3686f9cba8fSJoseph Mingrone 		/*
3696f9cba8fSJoseph Mingrone 		 * split_dname() has filled in the error message.
3706f9cba8fSJoseph Mingrone 		 */
371ada6f083SXin LI 		return (PCAP_ERROR_NO_SUCH_DEVICE);
3726f9cba8fSJoseph Mingrone 	}
373dc2c7305SBill Fenner 	*cp = '\0';
374dc2c7305SBill Fenner 
375dc2c7305SBill Fenner 	/*
376dc2c7305SBill Fenner 	 * Use "/dev/dlpi" as the device.
377dc2c7305SBill Fenner 	 *
378dc2c7305SBill Fenner 	 * XXX - HP's DLPI Programmer's Guide for HP-UX 11.00 says that
379dc2c7305SBill Fenner 	 * the "dl_mjr_num" field is for the "major number of interface
380dc2c7305SBill Fenner 	 * driver"; that's the major of "/dev/dlpi" on the system on
381dc2c7305SBill Fenner 	 * which I tried this, but there may be DLPI devices that
382dc2c7305SBill Fenner 	 * use a different driver, in which case we may need to
383dc2c7305SBill Fenner 	 * search "/dev" for the appropriate device with that major
384dc2c7305SBill Fenner 	 * device number, rather than hardwiring "/dev/dlpi".
385dc2c7305SBill Fenner 	 */
386dc2c7305SBill Fenner 	cp = "/dev/dlpi";
387ada6f083SXin LI 	if ((fd = open(cp, O_RDWR)) < 0) {
3886f9cba8fSJoseph Mingrone 		if (errno == EPERM || errno == EACCES) {
389a8e07101SRui Paulo 			status = PCAP_ERROR_PERM_DENIED;
3906f9cba8fSJoseph Mingrone 			snprintf(errbuf, PCAP_ERRBUF_SIZE,
3916f9cba8fSJoseph Mingrone 			    "Attempt to open %s failed with %s - root privilege may be required",
3926f9cba8fSJoseph Mingrone 			    cp, (errno == EPERM) ? "EPERM" : "EACCES");
3936f9cba8fSJoseph Mingrone 		} else {
394681ed54cSXin LI 			status = PCAP_ERROR;
395*afdbf109SJoseph Mingrone 			pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
3966f9cba8fSJoseph Mingrone 			    errno, "Attempt to open %s failed", cp);
3976f9cba8fSJoseph Mingrone 		}
398ada6f083SXin LI 		return (status);
399ada6f083SXin LI 	}
400ada6f083SXin LI 
401ada6f083SXin LI 	/*
402ada6f083SXin LI 	 * Get a table of all PPAs for that device, and search that
403ada6f083SXin LI 	 * table for the specified device type name and unit number.
404ada6f083SXin LI 	 */
405b00ab754SHans Petter Selasky 	status = get_dlpi_ppa(fd, dname, unit, ppa, errbuf);
406b00ab754SHans Petter Selasky 	if (status < 0) {
407ada6f083SXin LI 		close(fd);
408b00ab754SHans Petter Selasky 		return (status);
409ada6f083SXin LI 	}
410ada6f083SXin LI #else
411ada6f083SXin LI 	/*
412ada6f083SXin LI 	 * If the device name begins with "/", assume it begins with
413ada6f083SXin LI 	 * the pathname of the directory containing the device to open;
414ada6f083SXin LI 	 * otherwise, concatenate the device directory name and the
415ada6f083SXin LI 	 * device name.
416ada6f083SXin LI 	 */
417ada6f083SXin LI 	if (*name == '/')
418*afdbf109SJoseph Mingrone 		pcapint_strlcpy(dname, name, sizeof(dname));
419ada6f083SXin LI 	else
4206f9cba8fSJoseph Mingrone 		snprintf(dname, sizeof(dname), "%s/%s", PCAP_DEV_PREFIX,
421ada6f083SXin LI 		    name);
422ada6f083SXin LI 
423ada6f083SXin LI 	/*
424ada6f083SXin LI 	 * Get the unit number, and a pointer to the end of the device
425ada6f083SXin LI 	 * type name.
426ada6f083SXin LI 	 */
427ada6f083SXin LI 	cp = split_dname(dname, ppa, errbuf);
4286f9cba8fSJoseph Mingrone 	if (cp == NULL) {
4296f9cba8fSJoseph Mingrone 		/*
4306f9cba8fSJoseph Mingrone 		 * split_dname() has filled in the error message.
4316f9cba8fSJoseph Mingrone 		 */
432ada6f083SXin LI 		return (PCAP_ERROR_NO_SUCH_DEVICE);
4336f9cba8fSJoseph Mingrone 	}
434ada6f083SXin LI 
435ada6f083SXin LI 	/*
436ada6f083SXin LI 	 * Make a copy of the device pathname, and then remove the unit
437ada6f083SXin LI 	 * number from the device pathname.
438ada6f083SXin LI 	 */
439*afdbf109SJoseph Mingrone 	pcapint_strlcpy(dname2, dname, sizeof(dname));
440ada6f083SXin LI 	*cp = '\0';
441ada6f083SXin LI 
442ada6f083SXin LI 	/* Try device without unit number */
443ada6f083SXin LI 	if ((fd = open(dname, O_RDWR)) < 0) {
444ada6f083SXin LI 		if (errno != ENOENT) {
4456f9cba8fSJoseph Mingrone 			if (errno == EPERM || errno == EACCES) {
446ada6f083SXin LI 				status = PCAP_ERROR_PERM_DENIED;
4476f9cba8fSJoseph Mingrone 				snprintf(errbuf, PCAP_ERRBUF_SIZE,
4486f9cba8fSJoseph Mingrone 				    "Attempt to open %s failed with %s - root privilege may be required",
4496f9cba8fSJoseph Mingrone 				    dname,
4506f9cba8fSJoseph Mingrone 				    (errno == EPERM) ? "EPERM" : "EACCES");
4516f9cba8fSJoseph Mingrone 			} else {
452ada6f083SXin LI 				status = PCAP_ERROR;
453*afdbf109SJoseph Mingrone 				pcapint_fmt_errmsg_for_errno(errbuf,
4546f9cba8fSJoseph Mingrone 				    PCAP_ERRBUF_SIZE, errno,
4556f9cba8fSJoseph Mingrone 				    "Attempt to open %s failed", dname);
4566f9cba8fSJoseph Mingrone 			}
457ada6f083SXin LI 			return (status);
458ada6f083SXin LI 		}
459ada6f083SXin LI 
460ada6f083SXin LI 		/* Try again with unit number */
461ada6f083SXin LI 		if ((fd = open(dname2, O_RDWR)) < 0) {
462ada6f083SXin LI 			if (errno == ENOENT) {
463ada6f083SXin LI 				status = PCAP_ERROR_NO_SUCH_DEVICE;
464ada6f083SXin LI 
465ada6f083SXin LI 				/*
466ada6f083SXin LI 				 * We provide an error message even
467ada6f083SXin LI 				 * for this error, for diagnostic
468ada6f083SXin LI 				 * purposes (so that, for example,
469ada6f083SXin LI 				 * the app can show the message if the
470ada6f083SXin LI 				 * user requests it).
471ada6f083SXin LI 				 *
472ada6f083SXin LI 				 * In it, we just report "No DLPI device
473ada6f083SXin LI 				 * found" with the device name, so people
474ada6f083SXin LI 				 * don't get confused and think, for example,
475ada6f083SXin LI 				 * that if they can't capture on "lo0"
476ada6f083SXin LI 				 * on Solaris prior to Solaris 11 the fix
477ada6f083SXin LI 				 * is to change libpcap (or the application
478ada6f083SXin LI 				 * that uses it) to look for something other
479ada6f083SXin LI 				 * than "/dev/lo0", as the fix is to use
480ada6f083SXin LI 				 * Solaris 11 or some operating system
481ada6f083SXin LI 				 * other than Solaris - you just *can't*
482ada6f083SXin LI 				 * capture on a loopback interface
483ada6f083SXin LI 				 * on Solaris prior to Solaris 11, the lack
484ada6f083SXin LI 				 * of a DLPI device for the loopback
485ada6f083SXin LI 				 * interface is just a symptom of that
486ada6f083SXin LI 				 * inability.
487ada6f083SXin LI 				 */
4886f9cba8fSJoseph Mingrone 				snprintf(errbuf, PCAP_ERRBUF_SIZE,
489ada6f083SXin LI 				    "%s: No DLPI device found", name);
490ada6f083SXin LI 			} else {
4916f9cba8fSJoseph Mingrone 				if (errno == EPERM || errno == EACCES) {
492ada6f083SXin LI 					status = PCAP_ERROR_PERM_DENIED;
4936f9cba8fSJoseph Mingrone 					snprintf(errbuf, PCAP_ERRBUF_SIZE,
4946f9cba8fSJoseph Mingrone 					    "Attempt to open %s failed with %s - root privilege may be required",
4956f9cba8fSJoseph Mingrone 					    dname2,
4966f9cba8fSJoseph Mingrone 					    (errno == EPERM) ? "EPERM" : "EACCES");
4976f9cba8fSJoseph Mingrone 				} else {
498ada6f083SXin LI 					status = PCAP_ERROR;
499*afdbf109SJoseph Mingrone 					pcapint_fmt_errmsg_for_errno(errbuf,
5006f9cba8fSJoseph Mingrone 					    PCAP_ERRBUF_SIZE, errno,
5016f9cba8fSJoseph Mingrone 					    "Attempt to open %s failed",
5026f9cba8fSJoseph Mingrone 					    dname2);
5036f9cba8fSJoseph Mingrone 				}
504ada6f083SXin LI 			}
505ada6f083SXin LI 			return (status);
506ada6f083SXin LI 		}
507ada6f083SXin LI 		/* XXX Assume unit zero */
508ada6f083SXin LI 		*ppa = 0;
509ada6f083SXin LI 	}
510ada6f083SXin LI #endif
511ada6f083SXin LI 	return (fd);
512ada6f083SXin LI }
513ada6f083SXin LI 
514ada6f083SXin LI static int
pcap_activate_dlpi(pcap_t * p)515ada6f083SXin LI pcap_activate_dlpi(pcap_t *p)
516ada6f083SXin LI {
517ada6f083SXin LI #ifdef DL_HP_RAWDLS
518ada6f083SXin LI 	struct pcap_dlpi *pd = p->priv;
519ada6f083SXin LI #endif
520ada6f083SXin LI 	int status = 0;
521ada6f083SXin LI 	int retv;
522b00ab754SHans Petter Selasky 	u_int ppa;
523ada6f083SXin LI #ifdef HAVE_SOLARIS
524ada6f083SXin LI 	int isatm = 0;
525ada6f083SXin LI #endif
526ada6f083SXin LI 	register dl_info_ack_t *infop;
527ada6f083SXin LI #ifdef HAVE_SYS_BUFMOD_H
528ada6f083SXin LI 	bpf_u_int32 ss;
529ada6f083SXin LI #ifdef HAVE_SOLARIS
530ada6f083SXin LI 	char release[GET_RELEASE_BUFSIZE];
531ada6f083SXin LI 	bpf_u_int32 osmajor, osminor, osmicro;
532ada6f083SXin LI #endif
533ada6f083SXin LI #endif
534ada6f083SXin LI 	bpf_u_int32 buf[MAXDLBUF];
535ada6f083SXin LI 
536ada6f083SXin LI 	p->fd = open_dlpi_device(p->opt.device, &ppa, p->errbuf);
537ada6f083SXin LI 	if (p->fd < 0) {
538ada6f083SXin LI 		status = p->fd;
539dc2c7305SBill Fenner 		goto bad;
540dc2c7305SBill Fenner 	}
541dc2c7305SBill Fenner 
54204fb2745SSam Leffler #ifdef DL_HP_RAWDLS
54304fb2745SSam Leffler 	/*
54404fb2745SSam Leffler 	 * XXX - HP-UX 10.20 and 11.xx don't appear to support sending and
545ee2dd488SSam Leffler 	 * receiving packets on the same descriptor - you need separate
546ee2dd488SSam Leffler 	 * descriptors for sending and receiving, bound to different SAPs.
54704fb2745SSam Leffler 	 *
548681ed54cSXin LI 	 * If the open fails, we just leave -1 in "pd->send_fd" and reject
54904fb2745SSam Leffler 	 * attempts to send packets, just as if, in pcap-bpf.c, we fail
55004fb2745SSam Leffler 	 * to open the BPF device for reading and writing, we just try
55104fb2745SSam Leffler 	 * to open it for reading only and, if that succeeds, just let
55204fb2745SSam Leffler 	 * the send attempts fail.
55304fb2745SSam Leffler 	 */
554ada6f083SXin LI 	pd->send_fd = open("/dev/dlpi", O_RDWR);
5558cf6c252SPaul Traina #endif
5568cf6c252SPaul Traina 
5578cf6c252SPaul Traina 	/*
5588cf6c252SPaul Traina 	** Attach if "style 2" provider
5598cf6c252SPaul Traina 	*/
560a8e07101SRui Paulo 	if (dlinforeq(p->fd, p->errbuf) < 0 ||
561681ed54cSXin LI 	    dlinfoack(p->fd, (char *)buf, p->errbuf) < 0) {
562681ed54cSXin LI 		status = PCAP_ERROR;
5638cf6c252SPaul Traina 		goto bad;
564681ed54cSXin LI 	}
565a0ee43a1SRui Paulo 	infop = &(MAKE_DL_PRIMITIVES(buf))->info_ack;
566feb4ecdbSBruce M Simpson #ifdef HAVE_SOLARIS
567feb4ecdbSBruce M Simpson 	if (infop->dl_mac_type == DL_IPATM)
568feb4ecdbSBruce M Simpson 		isatm = 1;
569feb4ecdbSBruce M Simpson #endif
57004fb2745SSam Leffler 	if (infop->dl_provider_style == DL_STYLE2) {
571681ed54cSXin LI 		retv = dl_doattach(p->fd, ppa, p->errbuf);
572681ed54cSXin LI 		if (retv < 0) {
573681ed54cSXin LI 			status = retv;
5748cf6c252SPaul Traina 			goto bad;
575681ed54cSXin LI 		}
57604fb2745SSam Leffler #ifdef DL_HP_RAWDLS
577681ed54cSXin LI 		if (pd->send_fd >= 0) {
578681ed54cSXin LI 			retv = dl_doattach(pd->send_fd, ppa, p->errbuf);
579681ed54cSXin LI 			if (retv < 0) {
580681ed54cSXin LI 				status = retv;
58104fb2745SSam Leffler 				goto bad;
58204fb2745SSam Leffler 			}
583681ed54cSXin LI 		}
58404fb2745SSam Leffler #endif
58504fb2745SSam Leffler 	}
58604fb2745SSam Leffler 
587a8e07101SRui Paulo 	if (p->opt.rfmon) {
588a8e07101SRui Paulo 		/*
589a8e07101SRui Paulo 		 * This device exists, but we don't support monitor mode
590a8e07101SRui Paulo 		 * any platforms that support DLPI.
591a8e07101SRui Paulo 		 */
592a8e07101SRui Paulo 		status = PCAP_ERROR_RFMON_NOTSUP;
593a8e07101SRui Paulo 		goto bad;
594a8e07101SRui Paulo 	}
595a8e07101SRui Paulo 
596b00ab754SHans Petter Selasky #ifdef HAVE_DL_PASSIVE_REQ_T
597a8e07101SRui Paulo 	/*
598a8e07101SRui Paulo 	 * Enable Passive mode to be able to capture on aggregated link.
599a8e07101SRui Paulo 	 * Not supported in all Solaris versions.
600a8e07101SRui Paulo 	 */
601a8e07101SRui Paulo 	dlpassive(p->fd, p->errbuf);
602a8e07101SRui Paulo #endif
6038cf6c252SPaul Traina 	/*
604ee2dd488SSam Leffler 	** Bind (defer if using HP-UX 9 or HP-UX 10.20 or later, totally
605ee2dd488SSam Leffler 	** skip if using SINIX)
6068cf6c252SPaul Traina 	*/
607ee2dd488SSam Leffler #if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20_OR_LATER) && !defined(sinix)
608dc2c7305SBill Fenner #ifdef _AIX
609ee2dd488SSam Leffler 	/*
610ee2dd488SSam Leffler 	** AIX.
611ee2dd488SSam Leffler 	** According to IBM's AIX Support Line, the dl_sap value
6120a94d38fSBill Fenner 	** should not be less than 0x600 (1536) for standard Ethernet.
6130a94d38fSBill Fenner 	** However, we seem to get DL_BADADDR - "DLSAP addr in improper
6140a94d38fSBill Fenner 	** format or invalid" - errors if we use 1537 on the "tr0"
6150a94d38fSBill Fenner 	** device, which, given that its name starts with "tr" and that
6160a94d38fSBill Fenner 	** it's IBM, probably means a Token Ring device.  (Perhaps we
6170a94d38fSBill Fenner 	** need to use 1537 on "/dev/dlpi/en" because that device is for
6180a94d38fSBill Fenner 	** D/I/X Ethernet, the "SAP" is actually an Ethernet type, and
6190a94d38fSBill Fenner 	** it rejects invalid Ethernet types.)
6200a94d38fSBill Fenner 	**
6210a94d38fSBill Fenner 	** So if 1537 fails, we try 2, as Hyung Sik Yoon of IBM Korea
6220a94d38fSBill Fenner 	** says that works on Token Ring (he says that 0 does *not*
6230a94d38fSBill Fenner 	** work; perhaps that's considered an invalid LLC SAP value - I
6240a94d38fSBill Fenner 	** assume the SAP value in a DLPI bind is an LLC SAP for network
6250a94d38fSBill Fenner 	** types that use 802.2 LLC).
626dc2c7305SBill Fenner 	*/
627a8e07101SRui Paulo 	if ((dlbindreq(p->fd, 1537, p->errbuf) < 0 &&
628a8e07101SRui Paulo 	     dlbindreq(p->fd, 2, p->errbuf) < 0) ||
629681ed54cSXin LI 	     dlbindack(p->fd, (char *)buf, p->errbuf, NULL) < 0) {
630681ed54cSXin LI 		status = PCAP_ERROR;
6318cf6c252SPaul Traina 		goto bad;
632681ed54cSXin LI 	}
63304fb2745SSam Leffler #elif defined(DL_HP_RAWDLS)
63404fb2745SSam Leffler 	/*
635ee2dd488SSam Leffler 	** HP-UX 10.0x and 10.1x.
63604fb2745SSam Leffler 	*/
637681ed54cSXin LI 	if (dl_dohpuxbind(p->fd, p->errbuf) < 0) {
638681ed54cSXin LI 		status = PCAP_ERROR;
63904fb2745SSam Leffler 		goto bad;
640681ed54cSXin LI 	}
641681ed54cSXin LI 	if (pd->send_fd >= 0) {
64204fb2745SSam Leffler 		/*
643ee2dd488SSam Leffler 		** XXX - if this fails, just close send_fd and
644ee2dd488SSam Leffler 		** set it to -1, so that you can't send but can
645ee2dd488SSam Leffler 		** still receive?
64604fb2745SSam Leffler 		*/
647681ed54cSXin LI 		if (dl_dohpuxbind(pd->send_fd, p->errbuf) < 0) {
648681ed54cSXin LI 			status = PCAP_ERROR;
64904fb2745SSam Leffler 			goto bad;
65004fb2745SSam Leffler 		}
651681ed54cSXin LI 	}
65204fb2745SSam Leffler #else /* neither AIX nor HP-UX */
653ee2dd488SSam Leffler 	/*
654ee2dd488SSam Leffler 	** Not Sinix, and neither AIX nor HP-UX - Solaris, and any other
655ee2dd488SSam Leffler 	** OS using DLPI.
656ee2dd488SSam Leffler 	**/
657a8e07101SRui Paulo 	if (dlbindreq(p->fd, 0, p->errbuf) < 0 ||
658681ed54cSXin LI 	    dlbindack(p->fd, (char *)buf, p->errbuf, NULL) < 0) {
659681ed54cSXin LI 		status = PCAP_ERROR;
66004fb2745SSam Leffler 		goto bad;
661681ed54cSXin LI 	}
662ee2dd488SSam Leffler #endif /* AIX vs. HP-UX vs. other */
663ee2dd488SSam Leffler #endif /* !HP-UX 9 and !HP-UX 10.20 or later and !SINIX */
6648cf6c252SPaul Traina 
665b00ab754SHans Petter Selasky 	/*
666b00ab754SHans Petter Selasky 	 * Turn a negative snapshot value (invalid), a snapshot value of
667b00ab754SHans Petter Selasky 	 * 0 (unspecified), or a value bigger than the normal maximum
668b00ab754SHans Petter Selasky 	 * value, into the maximum allowed value.
669b00ab754SHans Petter Selasky 	 *
670b00ab754SHans Petter Selasky 	 * If some application really *needs* a bigger snapshot
671b00ab754SHans Petter Selasky 	 * length, we should just increase MAXIMUM_SNAPLEN.
672b00ab754SHans Petter Selasky 	 */
673b00ab754SHans Petter Selasky 	if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
674b00ab754SHans Petter Selasky 		p->snapshot = MAXIMUM_SNAPLEN;
675b00ab754SHans Petter Selasky 
676feb4ecdbSBruce M Simpson #ifdef HAVE_SOLARIS
677feb4ecdbSBruce M Simpson 	if (isatm) {
678feb4ecdbSBruce M Simpson 		/*
679feb4ecdbSBruce M Simpson 		** Have to turn on some special ATM promiscuous mode
680feb4ecdbSBruce M Simpson 		** for SunATM.
681feb4ecdbSBruce M Simpson 		** Do *NOT* turn regular promiscuous mode on; it doesn't
682feb4ecdbSBruce M Simpson 		** help, and may break things.
683feb4ecdbSBruce M Simpson 		*/
684feb4ecdbSBruce M Simpson 		if (strioctl(p->fd, A_PROMISCON_REQ, 0, NULL) < 0) {
685681ed54cSXin LI 			status = PCAP_ERROR;
686*afdbf109SJoseph Mingrone 			pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
687b00ab754SHans Petter Selasky 			    errno, "A_PROMISCON_REQ");
688feb4ecdbSBruce M Simpson 			goto bad;
689feb4ecdbSBruce M Simpson 		}
690feb4ecdbSBruce M Simpson 	} else
691feb4ecdbSBruce M Simpson #endif
692a8e07101SRui Paulo 	if (p->opt.promisc) {
6938cf6c252SPaul Traina 		/*
69404fb2745SSam Leffler 		** Enable promiscuous (not necessary on send FD)
6958cf6c252SPaul Traina 		*/
696681ed54cSXin LI 		retv = dlpromiscon(p, DL_PROMISC_PHYS);
697681ed54cSXin LI 		if (retv < 0) {
698681ed54cSXin LI 			if (retv == PCAP_ERROR_PERM_DENIED)
699d1e87331SXin LI 				status = PCAP_ERROR_PROMISC_PERM_DENIED;
700681ed54cSXin LI 			else
701681ed54cSXin LI 				status = retv;
7028cf6c252SPaul Traina 			goto bad;
703d1e87331SXin LI 		}
7048cf6c252SPaul Traina 
7058cf6c252SPaul Traina 		/*
7068cf6c252SPaul Traina 		** Try to enable multicast (you would have thought
707a4b5b39fSBill Fenner 		** promiscuous would be sufficient). (Skip if using
70804fb2745SSam Leffler 		** HP-UX or SINIX) (Not necessary on send FD)
7098cf6c252SPaul Traina 		*/
710a4b5b39fSBill Fenner #if !defined(__hpux) && !defined(sinix)
711681ed54cSXin LI 		retv = dlpromiscon(p, DL_PROMISC_MULTI);
712681ed54cSXin LI 		if (retv < 0)
713a8e07101SRui Paulo 			status = PCAP_WARNING;
7148cf6c252SPaul Traina #endif
7158cf6c252SPaul Traina 	}
7168cf6c252SPaul Traina 	/*
717ee2dd488SSam Leffler 	** Try to enable SAP promiscuity (when not in promiscuous mode
718ee2dd488SSam Leffler 	** when using HP-UX, when not doing SunATM on Solaris, and never
71904fb2745SSam Leffler 	** under SINIX) (Not necessary on send FD)
7208cf6c252SPaul Traina 	*/
7218cf6c252SPaul Traina #ifndef sinix
722d1e87331SXin LI #if defined(__hpux)
723d1e87331SXin LI 	/* HP-UX - only do this when not in promiscuous mode */
724d1e87331SXin LI 	if (!p->opt.promisc) {
725d1e87331SXin LI #elif defined(HAVE_SOLARIS)
726d1e87331SXin LI 	/* Solaris - don't do this on SunATM devices */
727d1e87331SXin LI 	if (!isatm) {
728d1e87331SXin LI #else
729d1e87331SXin LI 	/* Everything else (except for SINIX) - always do this */
730d1e87331SXin LI 	{
731a4b5b39fSBill Fenner #endif
732681ed54cSXin LI 		retv = dlpromiscon(p, DL_PROMISC_SAP);
733681ed54cSXin LI 		if (retv < 0) {
734681ed54cSXin LI 			if (p->opt.promisc) {
735d1e87331SXin LI 				/*
736681ed54cSXin LI 				 * Not fatal, since the DL_PROMISC_PHYS mode
737681ed54cSXin LI 				 * worked.
738681ed54cSXin LI 				 *
739d1e87331SXin LI 				 * Report it as a warning, however.
740d1e87331SXin LI 				 */
741a8e07101SRui Paulo 				status = PCAP_WARNING;
742681ed54cSXin LI 			} else {
743681ed54cSXin LI 				/*
744681ed54cSXin LI 				 * Fatal.
745681ed54cSXin LI 				 */
746681ed54cSXin LI 				status = retv;
7478cf6c252SPaul Traina 				goto bad;
7488cf6c252SPaul Traina 			}
749d1e87331SXin LI 		}
750681ed54cSXin LI 	}
751ee2dd488SSam Leffler #endif /* sinix */
7528cf6c252SPaul Traina 
7538cf6c252SPaul Traina 	/*
754ee2dd488SSam Leffler 	** HP-UX 9, and HP-UX 10.20 or later, must bind after setting
755ee2dd488SSam Leffler 	** promiscuous options.
7568cf6c252SPaul Traina 	*/
757ee2dd488SSam Leffler #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20_OR_LATER)
758681ed54cSXin LI 	if (dl_dohpuxbind(p->fd, p->errbuf) < 0) {
759681ed54cSXin LI 		status = PCAP_ERROR;
7608cf6c252SPaul Traina 		goto bad;
761681ed54cSXin LI 	}
762ee2dd488SSam Leffler 	/*
763ee2dd488SSam Leffler 	** We don't set promiscuous mode on the send FD, but we'll defer
764ee2dd488SSam Leffler 	** binding it anyway, just to keep the HP-UX 9/10.20 or later
765ee2dd488SSam Leffler 	** code together.
766ee2dd488SSam Leffler 	*/
767681ed54cSXin LI 	if (pd->send_fd >= 0) {
768ee2dd488SSam Leffler 		/*
769ee2dd488SSam Leffler 		** XXX - if this fails, just close send_fd and
770ee2dd488SSam Leffler 		** set it to -1, so that you can't send but can
771ee2dd488SSam Leffler 		** still receive?
772ee2dd488SSam Leffler 		*/
773681ed54cSXin LI 		if (dl_dohpuxbind(pd->send_fd, p->errbuf) < 0) {
774681ed54cSXin LI 			status = PCAP_ERROR;
775ee2dd488SSam Leffler 			goto bad;
776ee2dd488SSam Leffler 		}
777681ed54cSXin LI 	}
7788cf6c252SPaul Traina #endif
7798cf6c252SPaul Traina 
7808cf6c252SPaul Traina 	/*
7818cf6c252SPaul Traina 	** Determine link type
78204fb2745SSam Leffler 	** XXX - get SAP length and address length as well, for use
78304fb2745SSam Leffler 	** when sending packets.
7848cf6c252SPaul Traina 	*/
785a8e07101SRui Paulo 	if (dlinforeq(p->fd, p->errbuf) < 0 ||
786681ed54cSXin LI 	    dlinfoack(p->fd, (char *)buf, p->errbuf) < 0) {
787681ed54cSXin LI 		status = PCAP_ERROR;
7888cf6c252SPaul Traina 		goto bad;
789681ed54cSXin LI 	}
7908cf6c252SPaul Traina 
791a0ee43a1SRui Paulo 	infop = &(MAKE_DL_PRIMITIVES(buf))->info_ack;
792681ed54cSXin LI 	if (pcap_process_mactype(p, infop->dl_mac_type) != 0) {
793681ed54cSXin LI 		status = PCAP_ERROR;
7948cf6c252SPaul Traina 		goto bad;
795681ed54cSXin LI 	}
7968cf6c252SPaul Traina 
7978cf6c252SPaul Traina #ifdef	DLIOCRAW
7988cf6c252SPaul Traina 	/*
799feb4ecdbSBruce M Simpson 	** This is a non standard SunOS hack to get the full raw link-layer
800feb4ecdbSBruce M Simpson 	** header.
8018cf6c252SPaul Traina 	*/
8028cf6c252SPaul Traina 	if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) {
803681ed54cSXin LI 		status = PCAP_ERROR;
804*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
805b00ab754SHans Petter Selasky 		    errno, "DLIOCRAW");
8068cf6c252SPaul Traina 		goto bad;
8078cf6c252SPaul Traina 	}
8088cf6c252SPaul Traina #endif
8098cf6c252SPaul Traina 
810a0ee43a1SRui Paulo #ifdef HAVE_SYS_BUFMOD_H
811a8e07101SRui Paulo 	ss = p->snapshot;
8128cf6c252SPaul Traina 
8138cf6c252SPaul Traina 	/*
8148cf6c252SPaul Traina 	** There is a bug in bufmod(7). When dealing with messages of
8158cf6c252SPaul Traina 	** less than snaplen size it strips data from the beginning not
8168cf6c252SPaul Traina 	** the end.
8178cf6c252SPaul Traina 	**
818a8e07101SRui Paulo 	** This bug is fixed in 5.3.2. Also, there is a patch available.
819a8e07101SRui Paulo 	** Ask for bugid 1149065.
8208cf6c252SPaul Traina 	*/
8218cf6c252SPaul Traina #ifdef HAVE_SOLARIS
822ada6f083SXin LI 	get_release(release, sizeof (release), &osmajor, &osminor, &osmicro);
8238cf6c252SPaul Traina 	if (osmajor == 5 && (osminor <= 2 || (osminor == 3 && osmicro < 2)) &&
8248cf6c252SPaul Traina 	    getenv("BUFMOD_FIXED") == NULL) {
8256f9cba8fSJoseph Mingrone 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
826a8e07101SRui Paulo 		"WARNING: bufmod is broken in SunOS %s; ignoring snaplen.",
8278cf6c252SPaul Traina 		    release);
8288cf6c252SPaul Traina 		ss = 0;
829a8e07101SRui Paulo 		status = PCAP_WARNING;
8308cf6c252SPaul Traina 	}
8318cf6c252SPaul Traina #endif
8328cf6c252SPaul Traina 
833a8e07101SRui Paulo 	/* Push and configure bufmod. */
834681ed54cSXin LI 	if (pcap_conf_bufmod(p, ss) != 0) {
835681ed54cSXin LI 		status = PCAP_ERROR;
8368cf6c252SPaul Traina 		goto bad;
837681ed54cSXin LI 	}
8388cf6c252SPaul Traina #endif
8398cf6c252SPaul Traina 
8408cf6c252SPaul Traina 	/*
8418cf6c252SPaul Traina 	** As the last operation flush the read side.
8428cf6c252SPaul Traina 	*/
8438cf6c252SPaul Traina 	if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
844681ed54cSXin LI 		status = PCAP_ERROR;
845*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
846b00ab754SHans Petter Selasky 		    errno, "FLUSHR");
8478cf6c252SPaul Traina 		goto bad;
8488cf6c252SPaul Traina 	}
849feb4ecdbSBruce M Simpson 
850a8e07101SRui Paulo 	/* Allocate data buffer. */
851681ed54cSXin LI 	if (pcap_alloc_databuf(p) != 0) {
852681ed54cSXin LI 		status = PCAP_ERROR;
853feb4ecdbSBruce M Simpson 		goto bad;
854681ed54cSXin LI 	}
855feb4ecdbSBruce M Simpson 
856feb4ecdbSBruce M Simpson 	/*
857681ed54cSXin LI 	 * Success.
858681ed54cSXin LI 	 *
859feb4ecdbSBruce M Simpson 	 * "p->fd" is an FD for a STREAMS device, so "select()" and
860feb4ecdbSBruce M Simpson 	 * "poll()" should work on it.
861feb4ecdbSBruce M Simpson 	 */
862feb4ecdbSBruce M Simpson 	p->selectable_fd = p->fd;
863feb4ecdbSBruce M Simpson 
864feb4ecdbSBruce M Simpson 	p->read_op = pcap_read_dlpi;
86504fb2745SSam Leffler 	p->inject_op = pcap_inject_dlpi;
866*afdbf109SJoseph Mingrone 	p->setfilter_op = pcapint_install_bpf_program;	/* no kernel filtering */
867ee2dd488SSam Leffler 	p->setdirection_op = NULL;	/* Not implemented.*/
868feb4ecdbSBruce M Simpson 	p->set_datalink_op = NULL;	/* can't change data link type */
869*afdbf109SJoseph Mingrone 	p->getnonblock_op = pcapint_getnonblock_fd;
870*afdbf109SJoseph Mingrone 	p->setnonblock_op = pcapint_setnonblock_fd;
871feb4ecdbSBruce M Simpson 	p->stats_op = pcap_stats_dlpi;
872a8e07101SRui Paulo 	p->cleanup_op = pcap_cleanup_dlpi;
8738cf6c252SPaul Traina 
874a8e07101SRui Paulo 	return (status);
8758cf6c252SPaul Traina bad:
876a8e07101SRui Paulo 	pcap_cleanup_dlpi(p);
877a8e07101SRui Paulo 	return (status);
8788cf6c252SPaul Traina }
8798cf6c252SPaul Traina 
8800a94d38fSBill Fenner /*
8810a94d38fSBill Fenner  * Split a device name into a device type name and a unit number;
8820a94d38fSBill Fenner  * return the a pointer to the beginning of the unit number, which
8830a94d38fSBill Fenner  * is the end of the device type name, and set "*unitp" to the unit
8840a94d38fSBill Fenner  * number.
8850a94d38fSBill Fenner  *
8860a94d38fSBill Fenner  * Returns NULL on error, and fills "ebuf" with an error message.
8870a94d38fSBill Fenner  */
8880a94d38fSBill Fenner static char *
889b00ab754SHans Petter Selasky split_dname(char *device, u_int *unitp, char *ebuf)
8900a94d38fSBill Fenner {
8910a94d38fSBill Fenner 	char *cp;
8920a94d38fSBill Fenner 	char *eos;
89304fb2745SSam Leffler 	long unit;
8940a94d38fSBill Fenner 
8950a94d38fSBill Fenner 	/*
8960a94d38fSBill Fenner 	 * Look for a number at the end of the device name string.
8970a94d38fSBill Fenner 	 */
8980a94d38fSBill Fenner 	cp = device + strlen(device) - 1;
8990a94d38fSBill Fenner 	if (*cp < '0' || *cp > '9') {
9006f9cba8fSJoseph Mingrone 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s missing unit number",
9010a94d38fSBill Fenner 		    device);
9020a94d38fSBill Fenner 		return (NULL);
9030a94d38fSBill Fenner 	}
9040a94d38fSBill Fenner 
9050a94d38fSBill Fenner 	/* Digits at end of string are unit number */
9060a94d38fSBill Fenner 	while (cp-1 >= device && *(cp-1) >= '0' && *(cp-1) <= '9')
9070a94d38fSBill Fenner 		cp--;
9080a94d38fSBill Fenner 
90904fb2745SSam Leffler 	errno = 0;
9100a94d38fSBill Fenner 	unit = strtol(cp, &eos, 10);
9110a94d38fSBill Fenner 	if (*eos != '\0') {
9126f9cba8fSJoseph Mingrone 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s bad unit number", device);
9130a94d38fSBill Fenner 		return (NULL);
9140a94d38fSBill Fenner 	}
91504fb2745SSam Leffler 	if (errno == ERANGE || unit > INT_MAX) {
9166f9cba8fSJoseph Mingrone 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number too large",
91704fb2745SSam Leffler 		    device);
91804fb2745SSam Leffler 		return (NULL);
91904fb2745SSam Leffler 	}
92004fb2745SSam Leffler 	if (unit < 0) {
9216f9cba8fSJoseph Mingrone 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number is negative",
92204fb2745SSam Leffler 		    device);
92304fb2745SSam Leffler 		return (NULL);
92404fb2745SSam Leffler 	}
925b00ab754SHans Petter Selasky 	*unitp = (u_int)unit;
9260a94d38fSBill Fenner 	return (cp);
9270a94d38fSBill Fenner }
9280a94d38fSBill Fenner 
92904fb2745SSam Leffler static int
93004fb2745SSam Leffler dl_doattach(int fd, int ppa, char *ebuf)
93104fb2745SSam Leffler {
932d1e87331SXin LI 	dl_attach_req_t	req;
93304fb2745SSam Leffler 	bpf_u_int32 buf[MAXDLBUF];
934a8e07101SRui Paulo 	int err;
93504fb2745SSam Leffler 
936d1e87331SXin LI 	req.dl_primitive = DL_ATTACH_REQ;
937d1e87331SXin LI 	req.dl_ppa = ppa;
938d1e87331SXin LI 	if (send_request(fd, (char *)&req, sizeof(req), "attach", ebuf) < 0)
939a8e07101SRui Paulo 		return (PCAP_ERROR);
940d1e87331SXin LI 
9416f9cba8fSJoseph Mingrone 	err = dlokack(fd, "attach", (char *)buf, ebuf, NULL);
942a8e07101SRui Paulo 	if (err < 0)
943a8e07101SRui Paulo 		return (err);
94404fb2745SSam Leffler 	return (0);
94504fb2745SSam Leffler }
94604fb2745SSam Leffler 
947ee2dd488SSam Leffler #ifdef DL_HP_RAWDLS
948ee2dd488SSam Leffler static int
949ee2dd488SSam Leffler dl_dohpuxbind(int fd, char *ebuf)
950ee2dd488SSam Leffler {
951ee2dd488SSam Leffler 	int hpsap;
952ee2dd488SSam Leffler 	int uerror;
953ee2dd488SSam Leffler 	bpf_u_int32 buf[MAXDLBUF];
954ee2dd488SSam Leffler 
955ee2dd488SSam Leffler 	/*
956ee2dd488SSam Leffler 	 * XXX - we start at 22 because we used to use only 22, but
957ee2dd488SSam Leffler 	 * that was just because that was the value used in some
958ee2dd488SSam Leffler 	 * sample code from HP.  With what value *should* we start?
959ee2dd488SSam Leffler 	 * Does it matter, given that we're enabling SAP promiscuity
960ee2dd488SSam Leffler 	 * on the input FD?
961ee2dd488SSam Leffler 	 */
962ee2dd488SSam Leffler 	hpsap = 22;
963ee2dd488SSam Leffler 	for (;;) {
964ee2dd488SSam Leffler 		if (dlbindreq(fd, hpsap, ebuf) < 0)
965ee2dd488SSam Leffler 			return (-1);
966ee2dd488SSam Leffler 		if (dlbindack(fd, (char *)buf, ebuf, &uerror) >= 0)
967ee2dd488SSam Leffler 			break;
968ee2dd488SSam Leffler 		/*
969ee2dd488SSam Leffler 		 * For any error other than a UNIX EBUSY, give up.
970ee2dd488SSam Leffler 		 */
9715d18909fSSam Leffler 		if (uerror != EBUSY) {
9725d18909fSSam Leffler 			/*
9735d18909fSSam Leffler 			 * dlbindack() has already filled in ebuf for
9745d18909fSSam Leffler 			 * this error.
9755d18909fSSam Leffler 			 */
976ee2dd488SSam Leffler 			return (-1);
9775d18909fSSam Leffler 		}
978ee2dd488SSam Leffler 
979ee2dd488SSam Leffler 		/*
980ee2dd488SSam Leffler 		 * For EBUSY, try the next SAP value; that means that
981ee2dd488SSam Leffler 		 * somebody else is using that SAP.  Clear ebuf so
982ee2dd488SSam Leffler 		 * that application doesn't report the "Device busy"
983ee2dd488SSam Leffler 		 * error as a warning.
984ee2dd488SSam Leffler 		 */
985ee2dd488SSam Leffler 		*ebuf = '\0';
986ee2dd488SSam Leffler 		hpsap++;
9875d18909fSSam Leffler 		if (hpsap > 100) {
988*afdbf109SJoseph Mingrone 			pcapint_strlcpy(ebuf,
9895d18909fSSam Leffler 			    "All SAPs from 22 through 100 are in use",
9905d18909fSSam Leffler 			    PCAP_ERRBUF_SIZE);
991ee2dd488SSam Leffler 			return (-1);
992ee2dd488SSam Leffler 		}
993ee2dd488SSam Leffler 	}
9945d18909fSSam Leffler 	return (0);
9955d18909fSSam Leffler }
996ee2dd488SSam Leffler #endif
997ee2dd488SSam Leffler 
998d1e87331SXin LI #define STRINGIFY(n)	#n
999d1e87331SXin LI 
1000d1e87331SXin LI static int
1001d1e87331SXin LI dlpromiscon(pcap_t *p, bpf_u_int32 level)
1002d1e87331SXin LI {
1003d1e87331SXin LI 	dl_promiscon_req_t req;
1004d1e87331SXin LI 	bpf_u_int32 buf[MAXDLBUF];
1005d1e87331SXin LI 	int err;
10066f9cba8fSJoseph Mingrone 	int uerror;
1007d1e87331SXin LI 
1008d1e87331SXin LI 	req.dl_primitive = DL_PROMISCON_REQ;
1009d1e87331SXin LI 	req.dl_level = level;
1010d1e87331SXin LI 	if (send_request(p->fd, (char *)&req, sizeof(req), "promiscon",
1011d1e87331SXin LI 	    p->errbuf) < 0)
1012d1e87331SXin LI 		return (PCAP_ERROR);
1013d1e87331SXin LI 	err = dlokack(p->fd, "promiscon" STRINGIFY(level), (char *)buf,
10146f9cba8fSJoseph Mingrone 	    p->errbuf, &uerror);
10156f9cba8fSJoseph Mingrone 	if (err < 0) {
10166f9cba8fSJoseph Mingrone 		if (err == PCAP_ERROR_PERM_DENIED) {
10176f9cba8fSJoseph Mingrone 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
10186f9cba8fSJoseph Mingrone 			    "Attempt to set promiscuous mode failed with %s - root privilege may be required",
10196f9cba8fSJoseph Mingrone 			    (uerror == EPERM) ? "EPERM" : "EACCES");
10206f9cba8fSJoseph Mingrone 			err = PCAP_ERROR_PROMISC_PERM_DENIED;
10216f9cba8fSJoseph Mingrone 		}
1022d1e87331SXin LI 		return (err);
10236f9cba8fSJoseph Mingrone 	}
1024d1e87331SXin LI 	return (0);
1025d1e87331SXin LI }
1026d1e87331SXin LI 
1027ada6f083SXin LI /*
1028ada6f083SXin LI  * Not all interfaces are DLPI interfaces, and thus not all interfaces
1029ada6f083SXin LI  * can be opened with DLPI (for example, the loopback interface is not
1030ada6f083SXin LI  * a DLPI interface on Solaris prior to Solaris 11), so try to open
1031ada6f083SXin LI  * the specified interface; return 0 if we fail with PCAP_ERROR_NO_SUCH_DEVICE
1032ada6f083SXin LI  * and 1 otherwise.
1033ada6f083SXin LI  */
1034ada6f083SXin LI static int
1035ada6f083SXin LI is_dlpi_interface(const char *name)
1036ada6f083SXin LI {
1037ada6f083SXin LI 	int fd;
1038b00ab754SHans Petter Selasky 	u_int ppa;
1039ada6f083SXin LI 	char errbuf[PCAP_ERRBUF_SIZE];
1040ada6f083SXin LI 
1041ada6f083SXin LI 	fd = open_dlpi_device(name, &ppa, errbuf);
1042ada6f083SXin LI 	if (fd < 0) {
1043ada6f083SXin LI 		/*
1044ada6f083SXin LI 		 * Error - was it PCAP_ERROR_NO_SUCH_DEVICE?
1045ada6f083SXin LI 		 */
1046ada6f083SXin LI 		if (fd == PCAP_ERROR_NO_SUCH_DEVICE) {
1047ada6f083SXin LI 			/*
1048ada6f083SXin LI 			 * Yes, so we can't open this because it's
1049ada6f083SXin LI 			 * not a DLPI interface.
1050ada6f083SXin LI 			 */
1051ada6f083SXin LI 			return (0);
1052ada6f083SXin LI 		}
1053ada6f083SXin LI 		/*
1054ada6f083SXin LI 		 * No, so, in the case where there's a single DLPI
1055ada6f083SXin LI 		 * device for all interfaces of this type ("style
1056ada6f083SXin LI 		 * 2" providers?), we don't know whether it's a DLPI
1057ada6f083SXin LI 		 * interface or not, as we didn't try an attach.
1058ada6f083SXin LI 		 * Say it is a DLPI device, so that the user can at
1059ada6f083SXin LI 		 * least try to open it and report the error (which
1060ada6f083SXin LI 		 * is probably "you don't have permission to open that
1061ada6f083SXin LI 		 * DLPI device"; reporting those interfaces means
1062ada6f083SXin LI 		 * users will ask "why am I getting a permissions error
1063ada6f083SXin LI 		 * when I try to capture" rather than "why am I not
1064ada6f083SXin LI 		 * seeing any interfaces", making the underlying problem
1065ada6f083SXin LI 		 * clearer).
1066ada6f083SXin LI 		 */
1067ada6f083SXin LI 		return (1);
1068ada6f083SXin LI 	}
1069ada6f083SXin LI 
1070ada6f083SXin LI 	/*
1071ada6f083SXin LI 	 * Success.
1072ada6f083SXin LI 	 */
1073ada6f083SXin LI 	close(fd);
1074ada6f083SXin LI 	return (1);
1075ada6f083SXin LI }
1076ada6f083SXin LI 
1077b00ab754SHans Petter Selasky static int
1078b00ab754SHans Petter Selasky get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
1079b00ab754SHans Petter Selasky {
1080b00ab754SHans Petter Selasky 	/*
1081b00ab754SHans Petter Selasky 	 * Nothing we can do other than mark loopback devices as "the
1082b00ab754SHans Petter Selasky 	 * connected/disconnected status doesn't apply".
1083b00ab754SHans Petter Selasky 	 *
1084b00ab754SHans Petter Selasky 	 * XXX - on Solaris, can we do what the dladm command does,
1085b00ab754SHans Petter Selasky 	 * i.e. get a connected/disconnected indication from a kstat?
1086b00ab754SHans Petter Selasky 	 * (Note that you can also get the link speed, and possibly
1087b00ab754SHans Petter Selasky 	 * other information, from a kstat as well.)
1088b00ab754SHans Petter Selasky 	 */
1089b00ab754SHans Petter Selasky 	if (*flags & PCAP_IF_LOOPBACK) {
1090b00ab754SHans Petter Selasky 		/*
1091b00ab754SHans Petter Selasky 		 * Loopback devices aren't wireless, and "connected"/
1092b00ab754SHans Petter Selasky 		 * "disconnected" doesn't apply to them.
1093b00ab754SHans Petter Selasky 		 */
1094b00ab754SHans Petter Selasky 		*flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
1095b00ab754SHans Petter Selasky 		return (0);
1096b00ab754SHans Petter Selasky 	}
1097b00ab754SHans Petter Selasky 	return (0);
1098b00ab754SHans Petter Selasky }
1099b00ab754SHans Petter Selasky 
11008cf6c252SPaul Traina int
1101*afdbf109SJoseph Mingrone pcapint_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
11028cf6c252SPaul Traina {
1103feb4ecdbSBruce M Simpson #ifdef HAVE_SOLARIS
1104feb4ecdbSBruce M Simpson 	int fd;
1105feb4ecdbSBruce M Simpson 	union {
1106feb4ecdbSBruce M Simpson 		u_int nunits;
1107feb4ecdbSBruce M Simpson 		char pad[516];	/* XXX - must be at least 513; is 516
1108feb4ecdbSBruce M Simpson 				   in "atmgetunits" */
1109feb4ecdbSBruce M Simpson 	} buf;
1110feb4ecdbSBruce M Simpson 	char baname[2+1+1];
1111feb4ecdbSBruce M Simpson 	u_int i;
1112ada6f083SXin LI #endif
11138cf6c252SPaul Traina 
1114feb4ecdbSBruce M Simpson 	/*
1115ada6f083SXin LI 	 * Get the list of regular interfaces first.
1116ada6f083SXin LI 	 */
1117*afdbf109SJoseph Mingrone 	if (pcapint_findalldevs_interfaces(devlistp, errbuf, is_dlpi_interface,
1118b00ab754SHans Petter Selasky 	    get_if_flags) == -1)
1119ada6f083SXin LI 		return (-1);	/* failure */
1120ada6f083SXin LI 
1121ada6f083SXin LI #ifdef HAVE_SOLARIS
1122ada6f083SXin LI 	/*
1123feb4ecdbSBruce M Simpson 	 * We may have to do special magic to get ATM devices.
1124feb4ecdbSBruce M Simpson 	 */
1125feb4ecdbSBruce M Simpson 	if ((fd = open("/dev/ba", O_RDWR)) < 0) {
1126feb4ecdbSBruce M Simpson 		/*
1127feb4ecdbSBruce M Simpson 		 * We couldn't open the "ba" device.
1128feb4ecdbSBruce M Simpson 		 * For now, just give up; perhaps we should
1129feb4ecdbSBruce M Simpson 		 * return an error if the problem is neither
1130feb4ecdbSBruce M Simpson 		 * a "that device doesn't exist" error (ENOENT,
1131feb4ecdbSBruce M Simpson 		 * ENXIO, etc.) or a "you're not allowed to do
1132feb4ecdbSBruce M Simpson 		 * that" error (EPERM, EACCES).
1133feb4ecdbSBruce M Simpson 		 */
1134feb4ecdbSBruce M Simpson 		return (0);
1135feb4ecdbSBruce M Simpson 	}
1136feb4ecdbSBruce M Simpson 
1137feb4ecdbSBruce M Simpson 	if (strioctl(fd, A_GET_UNITS, sizeof(buf), (char *)&buf) < 0) {
1138*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
1139b00ab754SHans Petter Selasky 		    errno, "A_GET_UNITS");
1140dc2c7305SBill Fenner 		return (-1);
1141feb4ecdbSBruce M Simpson 	}
1142feb4ecdbSBruce M Simpson 	for (i = 0; i < buf.nunits; i++) {
11436f9cba8fSJoseph Mingrone 		snprintf(baname, sizeof baname, "ba%u", i);
1144b00ab754SHans Petter Selasky 		/*
1145b00ab754SHans Petter Selasky 		 * XXX - is there a notion of "up" and "running"?
1146b00ab754SHans Petter Selasky 		 * And is there a way to determine whether the
1147b00ab754SHans Petter Selasky 		 * interface is plugged into a network?
1148b00ab754SHans Petter Selasky 		 */
1149*afdbf109SJoseph Mingrone 		if (pcapint_add_dev(devlistp, baname, 0, NULL, errbuf) == NULL)
1150feb4ecdbSBruce M Simpson 			return (-1);
1151feb4ecdbSBruce M Simpson 	}
1152feb4ecdbSBruce M Simpson #endif
1153feb4ecdbSBruce M Simpson 
11548cf6c252SPaul Traina 	return (0);
11558cf6c252SPaul Traina }
11568cf6c252SPaul Traina 
11578cf6c252SPaul Traina static int
11588cf6c252SPaul Traina send_request(int fd, char *ptr, int len, char *what, char *ebuf)
11598cf6c252SPaul Traina {
11608cf6c252SPaul Traina 	struct	strbuf	ctl;
11618cf6c252SPaul Traina 	int	flags;
11628cf6c252SPaul Traina 
11638cf6c252SPaul Traina 	ctl.maxlen = 0;
11648cf6c252SPaul Traina 	ctl.len = len;
11658cf6c252SPaul Traina 	ctl.buf = ptr;
11668cf6c252SPaul Traina 
11678cf6c252SPaul Traina 	flags = 0;
11688cf6c252SPaul Traina 	if (putmsg(fd, &ctl, (struct strbuf *) NULL, flags) < 0) {
1169*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1170b00ab754SHans Petter Selasky 		    errno, "send_request: putmsg \"%s\"", what);
11718cf6c252SPaul Traina 		return (-1);
11728cf6c252SPaul Traina 	}
11738cf6c252SPaul Traina 	return (0);
11748cf6c252SPaul Traina }
11758cf6c252SPaul Traina 
11768cf6c252SPaul Traina static int
1177ee2dd488SSam Leffler recv_ack(int fd, int size, const char *what, char *bufp, char *ebuf, int *uerror)
11788cf6c252SPaul Traina {
11798cf6c252SPaul Traina 	union	DL_primitives	*dlp;
11808cf6c252SPaul Traina 	struct	strbuf	ctl;
11818cf6c252SPaul Traina 	int	flags;
1182ada6f083SXin LI 	char	errmsgbuf[PCAP_ERRBUF_SIZE];
1183ada6f083SXin LI 	char	dlprimbuf[64];
11848cf6c252SPaul Traina 
11855d18909fSSam Leffler 	/*
11865d18909fSSam Leffler 	 * Clear out "*uerror", so it's only set for DL_ERROR_ACK/DL_SYSERR,
11875d18909fSSam Leffler 	 * making that the only place where EBUSY is treated specially.
11885d18909fSSam Leffler 	 */
11895d18909fSSam Leffler 	if (uerror != NULL)
11905d18909fSSam Leffler 		*uerror = 0;
11915d18909fSSam Leffler 
11928cf6c252SPaul Traina 	ctl.maxlen = MAXDLBUF;
11938cf6c252SPaul Traina 	ctl.len = 0;
11948cf6c252SPaul Traina 	ctl.buf = bufp;
11958cf6c252SPaul Traina 
11968cf6c252SPaul Traina 	flags = 0;
11978cf6c252SPaul Traina 	if (getmsg(fd, &ctl, (struct strbuf*)NULL, &flags) < 0) {
1198*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1199b00ab754SHans Petter Selasky 		    errno, "recv_ack: %s getmsg", what);
1200a8e07101SRui Paulo 		return (PCAP_ERROR);
12018cf6c252SPaul Traina 	}
12028cf6c252SPaul Traina 
1203a0ee43a1SRui Paulo 	dlp = MAKE_DL_PRIMITIVES(ctl.buf);
12048cf6c252SPaul Traina 	switch (dlp->dl_primitive) {
12058cf6c252SPaul Traina 
12068cf6c252SPaul Traina 	case DL_INFO_ACK:
12078cf6c252SPaul Traina 	case DL_BIND_ACK:
12088cf6c252SPaul Traina 	case DL_OK_ACK:
12098cf6c252SPaul Traina #ifdef DL_HP_PPA_ACK
12108cf6c252SPaul Traina 	case DL_HP_PPA_ACK:
12118cf6c252SPaul Traina #endif
12128cf6c252SPaul Traina 		/* These are OK */
12138cf6c252SPaul Traina 		break;
12148cf6c252SPaul Traina 
12158cf6c252SPaul Traina 	case DL_ERROR_ACK:
12168cf6c252SPaul Traina 		switch (dlp->error_ack.dl_errno) {
12178cf6c252SPaul Traina 
12188cf6c252SPaul Traina 		case DL_SYSERR:
1219ee2dd488SSam Leffler 			if (uerror != NULL)
1220ee2dd488SSam Leffler 				*uerror = dlp->error_ack.dl_unix_errno;
1221*afdbf109SJoseph Mingrone 			pcapint_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1222b00ab754SHans Petter Selasky 			    dlp->error_ack.dl_unix_errno,
1223b00ab754SHans Petter Selasky 			    "recv_ack: %s: UNIX error", what);
1224d1e87331SXin LI 			if (dlp->error_ack.dl_unix_errno == EPERM ||
1225d1e87331SXin LI 			    dlp->error_ack.dl_unix_errno == EACCES)
1226a8e07101SRui Paulo 				return (PCAP_ERROR_PERM_DENIED);
12278cf6c252SPaul Traina 			break;
12288cf6c252SPaul Traina 
12298cf6c252SPaul Traina 		default:
12306f9cba8fSJoseph Mingrone 			/*
12316f9cba8fSJoseph Mingrone 			 * Neither EPERM nor EACCES.
12326f9cba8fSJoseph Mingrone 			 */
12336f9cba8fSJoseph Mingrone 			snprintf(ebuf, PCAP_ERRBUF_SIZE,
1234ada6f083SXin LI 			    "recv_ack: %s: %s", what,
1235ada6f083SXin LI 			    dlstrerror(errmsgbuf, sizeof (errmsgbuf), dlp->error_ack.dl_errno));
1236a8e07101SRui Paulo 			if (dlp->error_ack.dl_errno == DL_BADPPA)
1237a8e07101SRui Paulo 				return (PCAP_ERROR_NO_SUCH_DEVICE);
1238a8e07101SRui Paulo 			else if (dlp->error_ack.dl_errno == DL_ACCESS)
1239a8e07101SRui Paulo 				return (PCAP_ERROR_PERM_DENIED);
12408cf6c252SPaul Traina 			break;
12418cf6c252SPaul Traina 		}
1242a8e07101SRui Paulo 		return (PCAP_ERROR);
12438cf6c252SPaul Traina 
12448cf6c252SPaul Traina 	default:
12456f9cba8fSJoseph Mingrone 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
12460a94d38fSBill Fenner 		    "recv_ack: %s: Unexpected primitive ack %s",
1247ada6f083SXin LI 		    what, dlprim(dlprimbuf, sizeof (dlprimbuf), dlp->dl_primitive));
1248a8e07101SRui Paulo 		return (PCAP_ERROR);
12498cf6c252SPaul Traina 	}
12508cf6c252SPaul Traina 
12518cf6c252SPaul Traina 	if (ctl.len < size) {
12526f9cba8fSJoseph Mingrone 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
12530a94d38fSBill Fenner 		    "recv_ack: %s: Ack too small (%d < %d)",
12548cf6c252SPaul Traina 		    what, ctl.len, size);
1255a8e07101SRui Paulo 		return (PCAP_ERROR);
12568cf6c252SPaul Traina 	}
12578cf6c252SPaul Traina 	return (ctl.len);
12588cf6c252SPaul Traina }
12598cf6c252SPaul Traina 
12600a94d38fSBill Fenner static char *
1261ada6f083SXin LI dlstrerror(char *errbuf, size_t errbufsize, bpf_u_int32 dl_errno)
12620a94d38fSBill Fenner {
12630a94d38fSBill Fenner 	switch (dl_errno) {
12640a94d38fSBill Fenner 
12650a94d38fSBill Fenner 	case DL_ACCESS:
12660a94d38fSBill Fenner 		return ("Improper permissions for request");
12670a94d38fSBill Fenner 
12680a94d38fSBill Fenner 	case DL_BADADDR:
12690a94d38fSBill Fenner 		return ("DLSAP addr in improper format or invalid");
12700a94d38fSBill Fenner 
12710a94d38fSBill Fenner 	case DL_BADCORR:
12720a94d38fSBill Fenner 		return ("Seq number not from outstand DL_CONN_IND");
12730a94d38fSBill Fenner 
12740a94d38fSBill Fenner 	case DL_BADDATA:
12750a94d38fSBill Fenner 		return ("User data exceeded provider limit");
12760a94d38fSBill Fenner 
12770a94d38fSBill Fenner 	case DL_BADPPA:
12780a94d38fSBill Fenner #ifdef HAVE_DEV_DLPI
12790a94d38fSBill Fenner 		/*
12800a94d38fSBill Fenner 		 * With a single "/dev/dlpi" device used for all
12810a94d38fSBill Fenner 		 * DLPI providers, PPAs have nothing to do with
12820a94d38fSBill Fenner 		 * unit numbers.
12830a94d38fSBill Fenner 		 */
12840a94d38fSBill Fenner 		return ("Specified PPA was invalid");
12850a94d38fSBill Fenner #else
12860a94d38fSBill Fenner 		/*
12870a94d38fSBill Fenner 		 * We have separate devices for separate devices;
12880a94d38fSBill Fenner 		 * the PPA is just the unit number.
12890a94d38fSBill Fenner 		 */
12900a94d38fSBill Fenner 		return ("Specified PPA (device unit) was invalid");
12910a94d38fSBill Fenner #endif
12920a94d38fSBill Fenner 
12930a94d38fSBill Fenner 	case DL_BADPRIM:
12940a94d38fSBill Fenner 		return ("Primitive received not known by provider");
12950a94d38fSBill Fenner 
12960a94d38fSBill Fenner 	case DL_BADQOSPARAM:
12970a94d38fSBill Fenner 		return ("QOS parameters contained invalid values");
12980a94d38fSBill Fenner 
12990a94d38fSBill Fenner 	case DL_BADQOSTYPE:
13000a94d38fSBill Fenner 		return ("QOS structure type is unknown/unsupported");
13010a94d38fSBill Fenner 
13020a94d38fSBill Fenner 	case DL_BADSAP:
13030a94d38fSBill Fenner 		return ("Bad LSAP selector");
13040a94d38fSBill Fenner 
13050a94d38fSBill Fenner 	case DL_BADTOKEN:
13060a94d38fSBill Fenner 		return ("Token used not an active stream");
13070a94d38fSBill Fenner 
13080a94d38fSBill Fenner 	case DL_BOUND:
13090a94d38fSBill Fenner 		return ("Attempted second bind with dl_max_conind");
13100a94d38fSBill Fenner 
13110a94d38fSBill Fenner 	case DL_INITFAILED:
13120a94d38fSBill Fenner 		return ("Physical link initialization failed");
13130a94d38fSBill Fenner 
13140a94d38fSBill Fenner 	case DL_NOADDR:
13150a94d38fSBill Fenner 		return ("Provider couldn't allocate alternate address");
13160a94d38fSBill Fenner 
13170a94d38fSBill Fenner 	case DL_NOTINIT:
13180a94d38fSBill Fenner 		return ("Physical link not initialized");
13190a94d38fSBill Fenner 
13200a94d38fSBill Fenner 	case DL_OUTSTATE:
13210a94d38fSBill Fenner 		return ("Primitive issued in improper state");
13220a94d38fSBill Fenner 
13230a94d38fSBill Fenner 	case DL_SYSERR:
13240a94d38fSBill Fenner 		return ("UNIX system error occurred");
13250a94d38fSBill Fenner 
13260a94d38fSBill Fenner 	case DL_UNSUPPORTED:
13270a94d38fSBill Fenner 		return ("Requested service not supplied by provider");
13280a94d38fSBill Fenner 
13290a94d38fSBill Fenner 	case DL_UNDELIVERABLE:
13300a94d38fSBill Fenner 		return ("Previous data unit could not be delivered");
13310a94d38fSBill Fenner 
13320a94d38fSBill Fenner 	case DL_NOTSUPPORTED:
13330a94d38fSBill Fenner 		return ("Primitive is known but not supported");
13340a94d38fSBill Fenner 
13350a94d38fSBill Fenner 	case DL_TOOMANY:
13360a94d38fSBill Fenner 		return ("Limit exceeded");
13370a94d38fSBill Fenner 
13380a94d38fSBill Fenner 	case DL_NOTENAB:
13390a94d38fSBill Fenner 		return ("Promiscuous mode not enabled");
13400a94d38fSBill Fenner 
13410a94d38fSBill Fenner 	case DL_BUSY:
13420a94d38fSBill Fenner 		return ("Other streams for PPA in post-attached");
13430a94d38fSBill Fenner 
13440a94d38fSBill Fenner 	case DL_NOAUTO:
13450a94d38fSBill Fenner 		return ("Automatic handling XID&TEST not supported");
13460a94d38fSBill Fenner 
13470a94d38fSBill Fenner 	case DL_NOXIDAUTO:
13480a94d38fSBill Fenner 		return ("Automatic handling of XID not supported");
13490a94d38fSBill Fenner 
13500a94d38fSBill Fenner 	case DL_NOTESTAUTO:
13510a94d38fSBill Fenner 		return ("Automatic handling of TEST not supported");
13520a94d38fSBill Fenner 
13530a94d38fSBill Fenner 	case DL_XIDAUTO:
13540a94d38fSBill Fenner 		return ("Automatic handling of XID response");
13550a94d38fSBill Fenner 
13560a94d38fSBill Fenner 	case DL_TESTAUTO:
13570a94d38fSBill Fenner 		return ("Automatic handling of TEST response");
13580a94d38fSBill Fenner 
13590a94d38fSBill Fenner 	case DL_PENDING:
13600a94d38fSBill Fenner 		return ("Pending outstanding connect indications");
13610a94d38fSBill Fenner 
13620a94d38fSBill Fenner 	default:
13636f9cba8fSJoseph Mingrone 		snprintf(errbuf, errbufsize, "Error %02x", dl_errno);
1364ada6f083SXin LI 		return (errbuf);
13650a94d38fSBill Fenner 	}
13660a94d38fSBill Fenner }
13670a94d38fSBill Fenner 
13680a94d38fSBill Fenner static char *
1369ada6f083SXin LI dlprim(char *primbuf, size_t primbufsize, bpf_u_int32 prim)
13700a94d38fSBill Fenner {
13710a94d38fSBill Fenner 	switch (prim) {
13720a94d38fSBill Fenner 
13730a94d38fSBill Fenner 	case DL_INFO_REQ:
13740a94d38fSBill Fenner 		return ("DL_INFO_REQ");
13750a94d38fSBill Fenner 
13760a94d38fSBill Fenner 	case DL_INFO_ACK:
13770a94d38fSBill Fenner 		return ("DL_INFO_ACK");
13780a94d38fSBill Fenner 
13790a94d38fSBill Fenner 	case DL_ATTACH_REQ:
13800a94d38fSBill Fenner 		return ("DL_ATTACH_REQ");
13810a94d38fSBill Fenner 
13820a94d38fSBill Fenner 	case DL_DETACH_REQ:
13830a94d38fSBill Fenner 		return ("DL_DETACH_REQ");
13840a94d38fSBill Fenner 
13850a94d38fSBill Fenner 	case DL_BIND_REQ:
13860a94d38fSBill Fenner 		return ("DL_BIND_REQ");
13870a94d38fSBill Fenner 
13880a94d38fSBill Fenner 	case DL_BIND_ACK:
13890a94d38fSBill Fenner 		return ("DL_BIND_ACK");
13900a94d38fSBill Fenner 
13910a94d38fSBill Fenner 	case DL_UNBIND_REQ:
13920a94d38fSBill Fenner 		return ("DL_UNBIND_REQ");
13930a94d38fSBill Fenner 
13940a94d38fSBill Fenner 	case DL_OK_ACK:
13950a94d38fSBill Fenner 		return ("DL_OK_ACK");
13960a94d38fSBill Fenner 
13970a94d38fSBill Fenner 	case DL_ERROR_ACK:
13980a94d38fSBill Fenner 		return ("DL_ERROR_ACK");
13990a94d38fSBill Fenner 
14000a94d38fSBill Fenner 	case DL_SUBS_BIND_REQ:
14010a94d38fSBill Fenner 		return ("DL_SUBS_BIND_REQ");
14020a94d38fSBill Fenner 
14030a94d38fSBill Fenner 	case DL_SUBS_BIND_ACK:
14040a94d38fSBill Fenner 		return ("DL_SUBS_BIND_ACK");
14050a94d38fSBill Fenner 
14060a94d38fSBill Fenner 	case DL_UNITDATA_REQ:
14070a94d38fSBill Fenner 		return ("DL_UNITDATA_REQ");
14080a94d38fSBill Fenner 
14090a94d38fSBill Fenner 	case DL_UNITDATA_IND:
14100a94d38fSBill Fenner 		return ("DL_UNITDATA_IND");
14110a94d38fSBill Fenner 
14120a94d38fSBill Fenner 	case DL_UDERROR_IND:
14130a94d38fSBill Fenner 		return ("DL_UDERROR_IND");
14140a94d38fSBill Fenner 
14150a94d38fSBill Fenner 	case DL_UDQOS_REQ:
14160a94d38fSBill Fenner 		return ("DL_UDQOS_REQ");
14170a94d38fSBill Fenner 
14180a94d38fSBill Fenner 	case DL_CONNECT_REQ:
14190a94d38fSBill Fenner 		return ("DL_CONNECT_REQ");
14200a94d38fSBill Fenner 
14210a94d38fSBill Fenner 	case DL_CONNECT_IND:
14220a94d38fSBill Fenner 		return ("DL_CONNECT_IND");
14230a94d38fSBill Fenner 
14240a94d38fSBill Fenner 	case DL_CONNECT_RES:
14250a94d38fSBill Fenner 		return ("DL_CONNECT_RES");
14260a94d38fSBill Fenner 
14270a94d38fSBill Fenner 	case DL_CONNECT_CON:
14280a94d38fSBill Fenner 		return ("DL_CONNECT_CON");
14290a94d38fSBill Fenner 
14300a94d38fSBill Fenner 	case DL_TOKEN_REQ:
14310a94d38fSBill Fenner 		return ("DL_TOKEN_REQ");
14320a94d38fSBill Fenner 
14330a94d38fSBill Fenner 	case DL_TOKEN_ACK:
14340a94d38fSBill Fenner 		return ("DL_TOKEN_ACK");
14350a94d38fSBill Fenner 
14360a94d38fSBill Fenner 	case DL_DISCONNECT_REQ:
14370a94d38fSBill Fenner 		return ("DL_DISCONNECT_REQ");
14380a94d38fSBill Fenner 
14390a94d38fSBill Fenner 	case DL_DISCONNECT_IND:
14400a94d38fSBill Fenner 		return ("DL_DISCONNECT_IND");
14410a94d38fSBill Fenner 
14420a94d38fSBill Fenner 	case DL_RESET_REQ:
14430a94d38fSBill Fenner 		return ("DL_RESET_REQ");
14440a94d38fSBill Fenner 
14450a94d38fSBill Fenner 	case DL_RESET_IND:
14460a94d38fSBill Fenner 		return ("DL_RESET_IND");
14470a94d38fSBill Fenner 
14480a94d38fSBill Fenner 	case DL_RESET_RES:
14490a94d38fSBill Fenner 		return ("DL_RESET_RES");
14500a94d38fSBill Fenner 
14510a94d38fSBill Fenner 	case DL_RESET_CON:
14520a94d38fSBill Fenner 		return ("DL_RESET_CON");
14530a94d38fSBill Fenner 
14540a94d38fSBill Fenner 	default:
14556f9cba8fSJoseph Mingrone 		snprintf(primbuf, primbufsize, "unknown primitive 0x%x",
1456ada6f083SXin LI 		    prim);
14570a94d38fSBill Fenner 		return (primbuf);
14580a94d38fSBill Fenner 	}
14590a94d38fSBill Fenner }
14600a94d38fSBill Fenner 
14618cf6c252SPaul Traina static int
14628cf6c252SPaul Traina dlbindreq(int fd, bpf_u_int32 sap, char *ebuf)
14638cf6c252SPaul Traina {
14648cf6c252SPaul Traina 
14658cf6c252SPaul Traina 	dl_bind_req_t	req;
14668cf6c252SPaul Traina 
14678cf6c252SPaul Traina 	memset((char *)&req, 0, sizeof(req));
14688cf6c252SPaul Traina 	req.dl_primitive = DL_BIND_REQ;
1469ee2dd488SSam Leffler 	/* XXX - what if neither of these are defined? */
1470ee2dd488SSam Leffler #if defined(DL_HP_RAWDLS)
14718cf6c252SPaul Traina 	req.dl_max_conind = 1;			/* XXX magic number */
14728cf6c252SPaul Traina 	req.dl_service_mode = DL_HP_RAWDLS;
1473ee2dd488SSam Leffler #elif defined(DL_CLDLS)
14743052b236SBill Fenner 	req.dl_service_mode = DL_CLDLS;
14753052b236SBill Fenner #endif
1476ee2dd488SSam Leffler 	req.dl_sap = sap;
14778cf6c252SPaul Traina 
14788cf6c252SPaul Traina 	return (send_request(fd, (char *)&req, sizeof(req), "bind", ebuf));
14798cf6c252SPaul Traina }
14808cf6c252SPaul Traina 
14818cf6c252SPaul Traina static int
1482ee2dd488SSam Leffler dlbindack(int fd, char *bufp, char *ebuf, int *uerror)
14838cf6c252SPaul Traina {
14848cf6c252SPaul Traina 
1485ee2dd488SSam Leffler 	return (recv_ack(fd, DL_BIND_ACK_SIZE, "bind", bufp, ebuf, uerror));
14868cf6c252SPaul Traina }
14878cf6c252SPaul Traina 
14888cf6c252SPaul Traina static int
14896f9cba8fSJoseph Mingrone dlokack(int fd, const char *what, char *bufp, char *ebuf, int *uerror)
14908cf6c252SPaul Traina {
14918cf6c252SPaul Traina 
14926f9cba8fSJoseph Mingrone 	return (recv_ack(fd, DL_OK_ACK_SIZE, what, bufp, ebuf, uerror));
14938cf6c252SPaul Traina }
14948cf6c252SPaul Traina 
14958cf6c252SPaul Traina 
14968cf6c252SPaul Traina static int
14978cf6c252SPaul Traina dlinforeq(int fd, char *ebuf)
14988cf6c252SPaul Traina {
14998cf6c252SPaul Traina 	dl_info_req_t req;
15008cf6c252SPaul Traina 
15018cf6c252SPaul Traina 	req.dl_primitive = DL_INFO_REQ;
15028cf6c252SPaul Traina 
15038cf6c252SPaul Traina 	return (send_request(fd, (char *)&req, sizeof(req), "info", ebuf));
15048cf6c252SPaul Traina }
15058cf6c252SPaul Traina 
15068cf6c252SPaul Traina static int
15078cf6c252SPaul Traina dlinfoack(int fd, char *bufp, char *ebuf)
15088cf6c252SPaul Traina {
15098cf6c252SPaul Traina 
1510ee2dd488SSam Leffler 	return (recv_ack(fd, DL_INFO_ACK_SIZE, "info", bufp, ebuf, NULL));
15118cf6c252SPaul Traina }
15128cf6c252SPaul Traina 
1513b00ab754SHans Petter Selasky #ifdef HAVE_DL_PASSIVE_REQ_T
1514a8e07101SRui Paulo /*
1515a8e07101SRui Paulo  * Enable DLPI passive mode. We do not care if this request fails, as this
1516a8e07101SRui Paulo  * indicates the underlying DLPI device does not support link aggregation.
1517a8e07101SRui Paulo  */
1518a8e07101SRui Paulo static void
1519a8e07101SRui Paulo dlpassive(int fd, char *ebuf)
1520a8e07101SRui Paulo {
1521a8e07101SRui Paulo 	dl_passive_req_t req;
1522a8e07101SRui Paulo 	bpf_u_int32 buf[MAXDLBUF];
1523a8e07101SRui Paulo 
1524a8e07101SRui Paulo 	req.dl_primitive = DL_PASSIVE_REQ;
1525a8e07101SRui Paulo 
1526a8e07101SRui Paulo 	if (send_request(fd, (char *)&req, sizeof(req), "dlpassive", ebuf) == 0)
15276f9cba8fSJoseph Mingrone 	    (void) dlokack(fd, "dlpassive", (char *)buf, ebuf, NULL);
1528a8e07101SRui Paulo }
1529a8e07101SRui Paulo #endif
1530a8e07101SRui Paulo 
153104fb2745SSam Leffler #ifdef DL_HP_RAWDLS
153204fb2745SSam Leffler /*
153304fb2745SSam Leffler  * There's an ack *if* there's an error.
153404fb2745SSam Leffler  */
153504fb2745SSam Leffler static int
153604fb2745SSam Leffler dlrawdatareq(int fd, const u_char *datap, int datalen)
153704fb2745SSam Leffler {
153804fb2745SSam Leffler 	struct strbuf ctl, data;
153904fb2745SSam Leffler 	long buf[MAXDLBUF];	/* XXX - char? */
154004fb2745SSam Leffler 	union DL_primitives *dlp;
154104fb2745SSam Leffler 	int dlen;
154204fb2745SSam Leffler 
1543a0ee43a1SRui Paulo 	dlp = MAKE_DL_PRIMITIVES(buf);
154404fb2745SSam Leffler 
154504fb2745SSam Leffler 	dlp->dl_primitive = DL_HP_RAWDATA_REQ;
154604fb2745SSam Leffler 	dlen = DL_HP_RAWDATA_REQ_SIZE;
154704fb2745SSam Leffler 
154804fb2745SSam Leffler 	/*
154904fb2745SSam Leffler 	 * HP's documentation doesn't appear to show us supplying any
155004fb2745SSam Leffler 	 * address pointed to by the control part of the message.
155104fb2745SSam Leffler 	 * I think that's what raw mode means - you just send the raw
155204fb2745SSam Leffler 	 * packet, you don't specify where to send it to, as that's
155304fb2745SSam Leffler 	 * implied by the destination address.
155404fb2745SSam Leffler 	 */
155504fb2745SSam Leffler 	ctl.maxlen = 0;
155604fb2745SSam Leffler 	ctl.len = dlen;
155704fb2745SSam Leffler 	ctl.buf = (void *)buf;
155804fb2745SSam Leffler 
155904fb2745SSam Leffler 	data.maxlen = 0;
156004fb2745SSam Leffler 	data.len = datalen;
156104fb2745SSam Leffler 	data.buf = (void *)datap;
156204fb2745SSam Leffler 
156304fb2745SSam Leffler 	return (putmsg(fd, &ctl, &data, 0));
156404fb2745SSam Leffler }
156504fb2745SSam Leffler #endif /* DL_HP_RAWDLS */
156604fb2745SSam Leffler 
15678cf6c252SPaul Traina #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
1568ada6f083SXin LI static void
1569ada6f083SXin LI get_release(char *buf, size_t bufsize, bpf_u_int32 *majorp,
1570ada6f083SXin LI     bpf_u_int32 *minorp, bpf_u_int32 *microp)
15718cf6c252SPaul Traina {
15728cf6c252SPaul Traina 	char *cp;
15738cf6c252SPaul Traina 
15748cf6c252SPaul Traina 	*majorp = 0;
15758cf6c252SPaul Traina 	*minorp = 0;
15768cf6c252SPaul Traina 	*microp = 0;
1577ada6f083SXin LI 	if (sysinfo(SI_RELEASE, buf, bufsize) < 0) {
1578*afdbf109SJoseph Mingrone 		pcapint_strlcpy(buf, "?", bufsize);
1579ada6f083SXin LI 		return;
1580ada6f083SXin LI 	}
15818cf6c252SPaul Traina 	cp = buf;
15826f9cba8fSJoseph Mingrone 	if (!PCAP_ISDIGIT((unsigned char)*cp))
1583ada6f083SXin LI 		return;
15848cf6c252SPaul Traina 	*majorp = strtol(cp, &cp, 10);
15858cf6c252SPaul Traina 	if (*cp++ != '.')
1586ada6f083SXin LI 		return;
15878cf6c252SPaul Traina 	*minorp =  strtol(cp, &cp, 10);
15888cf6c252SPaul Traina 	if (*cp++ != '.')
1589ada6f083SXin LI 		return;
15908cf6c252SPaul Traina 	*microp =  strtol(cp, &cp, 10);
15918cf6c252SPaul Traina }
15928cf6c252SPaul Traina #endif
15938cf6c252SPaul Traina 
159404fb2745SSam Leffler #ifdef DL_HP_PPA_REQ
15958cf6c252SPaul Traina /*
1596dc2c7305SBill Fenner  * Under HP-UX 10 and HP-UX 11, we can ask for the ppa
15978cf6c252SPaul Traina  */
15988cf6c252SPaul Traina 
15998cf6c252SPaul Traina 
1600dc2c7305SBill Fenner /*
1601dc2c7305SBill Fenner  * Determine ppa number that specifies ifname.
1602dc2c7305SBill Fenner  *
1603dc2c7305SBill Fenner  * If the "dl_hp_ppa_info_t" doesn't have a "dl_module_id_1" member,
1604dc2c7305SBill Fenner  * the code that's used here is the old code for HP-UX 10.x.
1605dc2c7305SBill Fenner  *
1606dc2c7305SBill Fenner  * However, HP-UX 10.20, at least, appears to have such a member
1607dc2c7305SBill Fenner  * in its "dl_hp_ppa_info_t" structure, so the new code is used.
1608dc2c7305SBill Fenner  * The new code didn't work on an old 10.20 system on which Rick
1609dc2c7305SBill Fenner  * Jones of HP tried it, but with later patches installed, it
1610dc2c7305SBill Fenner  * worked - it appears that the older system had those members but
1611dc2c7305SBill Fenner  * didn't put anything in them, so, if the search by name fails, we
1612dc2c7305SBill Fenner  * do the old search.
1613dc2c7305SBill Fenner  *
1614dc2c7305SBill Fenner  * Rick suggests that making sure your system is "up on the latest
1615dc2c7305SBill Fenner  * lancommon/DLPI/driver patches" is probably a good idea; it'd fix
1616dc2c7305SBill Fenner  * that problem, as well as allowing libpcap to see packets sent
1617dc2c7305SBill Fenner  * from the system on which the libpcap application is being run.
1618dc2c7305SBill Fenner  * (On 10.20, in addition to getting the latest patches, you need
1619dc2c7305SBill Fenner  * to turn the kernel "lanc_outbound_promisc_flag" flag on with ADB;
1620dc2c7305SBill Fenner  * a posting to "comp.sys.hp.hpux" at
1621dc2c7305SBill Fenner  *
1622dc2c7305SBill Fenner  *	http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=558092266
1623dc2c7305SBill Fenner  *
1624dc2c7305SBill Fenner  * says that, to see the machine's outgoing traffic, you'd need to
1625dc2c7305SBill Fenner  * apply the right patches to your system, and also set that variable
1626dc2c7305SBill Fenner  * with:
1627dc2c7305SBill Fenner 
1628dc2c7305SBill Fenner echo 'lanc_outbound_promisc_flag/W1' | /usr/bin/adb -w /stand/vmunix /dev/kmem
1629dc2c7305SBill Fenner 
1630dc2c7305SBill Fenner  * which could be put in, for example, "/sbin/init.d/lan".
1631dc2c7305SBill Fenner  *
1632dc2c7305SBill Fenner  * Setting the variable is not necessary on HP-UX 11.x.
1633dc2c7305SBill Fenner  */
16348cf6c252SPaul Traina static int
1635b00ab754SHans Petter Selasky get_dlpi_ppa(register int fd, register const char *device, register u_int unit,
1636b00ab754SHans Petter Selasky     u_int *ppa, register char *ebuf)
16378cf6c252SPaul Traina {
16388cf6c252SPaul Traina 	register dl_hp_ppa_ack_t *ap;
1639dc2c7305SBill Fenner 	register dl_hp_ppa_info_t *ipstart, *ip;
1640b00ab754SHans Petter Selasky 	register u_int i;
1641dc2c7305SBill Fenner 	char dname[100];
16428cf6c252SPaul Traina 	register u_long majdev;
16438cf6c252SPaul Traina 	struct stat statbuf;
1644dc2c7305SBill Fenner 	dl_hp_ppa_req_t	req;
16450a94d38fSBill Fenner 	char buf[MAXDLBUF];
16460a94d38fSBill Fenner 	char *ppa_data_buf;
16470a94d38fSBill Fenner 	dl_hp_ppa_ack_t	*dlp;
16480a94d38fSBill Fenner 	struct strbuf ctl;
16490a94d38fSBill Fenner 	int flags;
16508cf6c252SPaul Traina 
16518cf6c252SPaul Traina 	memset((char *)&req, 0, sizeof(req));
16528cf6c252SPaul Traina 	req.dl_primitive = DL_HP_PPA_REQ;
16538cf6c252SPaul Traina 
16548cf6c252SPaul Traina 	memset((char *)buf, 0, sizeof(buf));
16550a94d38fSBill Fenner 	if (send_request(fd, (char *)&req, sizeof(req), "hpppa", ebuf) < 0)
1656a8e07101SRui Paulo 		return (PCAP_ERROR);
16578cf6c252SPaul Traina 
16580a94d38fSBill Fenner 	ctl.maxlen = DL_HP_PPA_ACK_SIZE;
16590a94d38fSBill Fenner 	ctl.len = 0;
16600a94d38fSBill Fenner 	ctl.buf = (char *)buf;
16610a94d38fSBill Fenner 
16620a94d38fSBill Fenner 	flags = 0;
16630a94d38fSBill Fenner 	/*
16640a94d38fSBill Fenner 	 * DLPI may return a big chunk of data for a DL_HP_PPA_REQ. The normal
16650a94d38fSBill Fenner 	 * recv_ack will fail because it set the maxlen to MAXDLBUF (8192)
16660a94d38fSBill Fenner 	 * which is NOT big enough for a DL_HP_PPA_REQ.
16670a94d38fSBill Fenner 	 *
16680a94d38fSBill Fenner 	 * This causes libpcap applications to fail on a system with HP-APA
16690a94d38fSBill Fenner 	 * installed.
16700a94d38fSBill Fenner 	 *
16710a94d38fSBill Fenner 	 * To figure out how big the returned data is, we first call getmsg
16720a94d38fSBill Fenner 	 * to get the small head and peek at the head to get the actual data
16730a94d38fSBill Fenner 	 * length, and  then issue another getmsg to get the actual PPA data.
16740a94d38fSBill Fenner 	 */
16750a94d38fSBill Fenner 	/* get the head first */
16760a94d38fSBill Fenner 	if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) {
1677*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1678b00ab754SHans Petter Selasky 		    errno, "get_dlpi_ppa: hpppa getmsg");
1679b00ab754SHans Petter Selasky 		return (PCAP_ERROR);
1680b00ab754SHans Petter Selasky 	}
1681b00ab754SHans Petter Selasky 	if (ctl.len == -1) {
16826f9cba8fSJoseph Mingrone 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
1683b00ab754SHans Petter Selasky 		    "get_dlpi_ppa: hpppa getmsg: control buffer has no data");
1684a8e07101SRui Paulo 		return (PCAP_ERROR);
16850a94d38fSBill Fenner 	}
16860a94d38fSBill Fenner 
16870a94d38fSBill Fenner 	dlp = (dl_hp_ppa_ack_t *)ctl.buf;
16880a94d38fSBill Fenner 	if (dlp->dl_primitive != DL_HP_PPA_ACK) {
16896f9cba8fSJoseph Mingrone 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
16900a94d38fSBill Fenner 		    "get_dlpi_ppa: hpppa unexpected primitive ack 0x%x",
16910a94d38fSBill Fenner 		    (bpf_u_int32)dlp->dl_primitive);
1692a8e07101SRui Paulo 		return (PCAP_ERROR);
16930a94d38fSBill Fenner 	}
16940a94d38fSBill Fenner 
1695b00ab754SHans Petter Selasky 	if ((size_t)ctl.len < DL_HP_PPA_ACK_SIZE) {
16966f9cba8fSJoseph Mingrone 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
1697feb4ecdbSBruce M Simpson 		    "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
1698feb4ecdbSBruce M Simpson 		     ctl.len, (unsigned long)DL_HP_PPA_ACK_SIZE);
1699a8e07101SRui Paulo 		return (PCAP_ERROR);
17000a94d38fSBill Fenner 	}
17010a94d38fSBill Fenner 
17020a94d38fSBill Fenner 	/* allocate buffer */
17030a94d38fSBill Fenner 	if ((ppa_data_buf = (char *)malloc(dlp->dl_length)) == NULL) {
1704*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1705b00ab754SHans Petter Selasky 		    errno, "get_dlpi_ppa: hpppa malloc");
1706a8e07101SRui Paulo 		return (PCAP_ERROR);
17070a94d38fSBill Fenner 	}
17080a94d38fSBill Fenner 	ctl.maxlen = dlp->dl_length;
17090a94d38fSBill Fenner 	ctl.len = 0;
17100a94d38fSBill Fenner 	ctl.buf = (char *)ppa_data_buf;
17110a94d38fSBill Fenner 	/* get the data */
17120a94d38fSBill Fenner 	if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) {
1713*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1714b00ab754SHans Petter Selasky 		    errno, "get_dlpi_ppa: hpppa getmsg");
17150a94d38fSBill Fenner 		free(ppa_data_buf);
1716a8e07101SRui Paulo 		return (PCAP_ERROR);
17170a94d38fSBill Fenner 	}
1718b00ab754SHans Petter Selasky 	if (ctl.len == -1) {
17196f9cba8fSJoseph Mingrone 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
1720b00ab754SHans Petter Selasky 		    "get_dlpi_ppa: hpppa getmsg: control buffer has no data");
1721b00ab754SHans Petter Selasky 		return (PCAP_ERROR);
1722b00ab754SHans Petter Selasky 	}
1723b00ab754SHans Petter Selasky 	if ((u_int)ctl.len < dlp->dl_length) {
17246f9cba8fSJoseph Mingrone 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
1725a0ee43a1SRui Paulo 		    "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
1726a0ee43a1SRui Paulo 		    ctl.len, (unsigned long)dlp->dl_length);
17270a94d38fSBill Fenner 		free(ppa_data_buf);
1728a8e07101SRui Paulo 		return (PCAP_ERROR);
17290a94d38fSBill Fenner 	}
17300a94d38fSBill Fenner 
17318cf6c252SPaul Traina 	ap = (dl_hp_ppa_ack_t *)buf;
17320a94d38fSBill Fenner 	ipstart = (dl_hp_ppa_info_t *)ppa_data_buf;
1733dc2c7305SBill Fenner 	ip = ipstart;
17348cf6c252SPaul Traina 
1735b00ab754SHans Petter Selasky #ifdef HAVE_DL_HP_PPA_INFO_T_DL_MODULE_ID_1
1736dc2c7305SBill Fenner 	/*
1737dc2c7305SBill Fenner 	 * The "dl_hp_ppa_info_t" structure has a "dl_module_id_1"
1738dc2c7305SBill Fenner 	 * member that should, in theory, contain the part of the
1739dc2c7305SBill Fenner 	 * name for the device that comes before the unit number,
1740dc2c7305SBill Fenner 	 * and should also have a "dl_module_id_2" member that may
1741dc2c7305SBill Fenner 	 * contain an alternate name (e.g., I think Ethernet devices
1742dc2c7305SBill Fenner 	 * have both "lan", for "lanN", and "snap", for "snapN", with
1743dc2c7305SBill Fenner 	 * the former being for Ethernet packets and the latter being
1744dc2c7305SBill Fenner 	 * for 802.3/802.2 packets).
1745dc2c7305SBill Fenner 	 *
1746dc2c7305SBill Fenner 	 * Search for the device that has the specified name and
1747dc2c7305SBill Fenner 	 * instance number.
1748dc2c7305SBill Fenner 	 */
17498cf6c252SPaul Traina 	for (i = 0; i < ap->dl_count; i++) {
17500a94d38fSBill Fenner 		if ((strcmp((const char *)ip->dl_module_id_1, device) == 0 ||
17510a94d38fSBill Fenner 		     strcmp((const char *)ip->dl_module_id_2, device) == 0) &&
1752dc2c7305SBill Fenner 		    ip->dl_instance_num == unit)
17538cf6c252SPaul Traina 			break;
17548cf6c252SPaul Traina 
1755dc2c7305SBill Fenner 		ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset);
1756dc2c7305SBill Fenner 	}
1757dc2c7305SBill Fenner #else
1758dc2c7305SBill Fenner 	/*
1759dc2c7305SBill Fenner 	 * We don't have that member, so the search is impossible; make it
1760dc2c7305SBill Fenner 	 * look as if the search failed.
1761dc2c7305SBill Fenner 	 */
1762dc2c7305SBill Fenner 	i = ap->dl_count;
1763dc2c7305SBill Fenner #endif
1764dc2c7305SBill Fenner 
1765dc2c7305SBill Fenner 	if (i == ap->dl_count) {
1766dc2c7305SBill Fenner 		/*
1767dc2c7305SBill Fenner 		 * Well, we didn't, or can't, find the device by name.
1768dc2c7305SBill Fenner 		 *
1769dc2c7305SBill Fenner 		 * HP-UX 10.20, whilst it has "dl_module_id_1" and
1770dc2c7305SBill Fenner 		 * "dl_module_id_2" fields in the "dl_hp_ppa_info_t",
1771dc2c7305SBill Fenner 		 * doesn't seem to fill them in unless the system is
1772dc2c7305SBill Fenner 		 * at a reasonably up-to-date patch level.
1773dc2c7305SBill Fenner 		 *
1774dc2c7305SBill Fenner 		 * Older HP-UX 10.x systems might not have those fields
1775dc2c7305SBill Fenner 		 * at all.
1776dc2c7305SBill Fenner 		 *
1777dc2c7305SBill Fenner 		 * Therefore, we'll search for the entry with the major
1778dc2c7305SBill Fenner 		 * device number of a device with the name "/dev/<dev><unit>",
1779dc2c7305SBill Fenner 		 * if such a device exists, as the old code did.
1780dc2c7305SBill Fenner 		 */
17816f9cba8fSJoseph Mingrone 		snprintf(dname, sizeof(dname), "/dev/%s%u", device, unit);
1782dc2c7305SBill Fenner 		if (stat(dname, &statbuf) < 0) {
1783*afdbf109SJoseph Mingrone 			pcapint_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1784b00ab754SHans Petter Selasky 			    errno, "stat: %s", dname);
1785a8e07101SRui Paulo 			return (PCAP_ERROR);
1786dc2c7305SBill Fenner 		}
1787dc2c7305SBill Fenner 		majdev = major(statbuf.st_rdev);
1788dc2c7305SBill Fenner 
1789dc2c7305SBill Fenner 		ip = ipstart;
1790dc2c7305SBill Fenner 
1791dc2c7305SBill Fenner 		for (i = 0; i < ap->dl_count; i++) {
1792dc2c7305SBill Fenner 			if (ip->dl_mjr_num == majdev &&
1793dc2c7305SBill Fenner 			    ip->dl_instance_num == unit)
1794dc2c7305SBill Fenner 				break;
1795dc2c7305SBill Fenner 
1796dc2c7305SBill Fenner 			ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset);
1797dc2c7305SBill Fenner 		}
17988cf6c252SPaul Traina 	}
17998cf6c252SPaul Traina 	if (i == ap->dl_count) {
18006f9cba8fSJoseph Mingrone 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
1801b00ab754SHans Petter Selasky 		    "can't find /dev/dlpi PPA for %s%u", device, unit);
1802a8e07101SRui Paulo 		return (PCAP_ERROR_NO_SUCH_DEVICE);
18038cf6c252SPaul Traina 	}
18048cf6c252SPaul Traina 	if (ip->dl_hdw_state == HDW_DEAD) {
18056f9cba8fSJoseph Mingrone 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
1806dc2c7305SBill Fenner 		    "%s%d: hardware state: DOWN\n", device, unit);
18070a94d38fSBill Fenner 		free(ppa_data_buf);
1808a8e07101SRui Paulo 		return (PCAP_ERROR);
18098cf6c252SPaul Traina 	}
1810b00ab754SHans Petter Selasky 	*ppa = ip->dl_ppa;
18110a94d38fSBill Fenner 	free(ppa_data_buf);
1812b00ab754SHans Petter Selasky 	return (0);
18138cf6c252SPaul Traina }
18148cf6c252SPaul Traina #endif
18158cf6c252SPaul Traina 
18168cf6c252SPaul Traina #ifdef HAVE_HPUX9
18178cf6c252SPaul Traina /*
18188cf6c252SPaul Traina  * Under HP-UX 9, there is no good way to determine the ppa.
18198cf6c252SPaul Traina  * So punt and read it from /dev/kmem.
18208cf6c252SPaul Traina  */
18218cf6c252SPaul Traina static struct nlist nl[] = {
18228cf6c252SPaul Traina #define NL_IFNET 0
18238cf6c252SPaul Traina 	{ "ifnet" },
18248cf6c252SPaul Traina 	{ "" }
18258cf6c252SPaul Traina };
18268cf6c252SPaul Traina 
18278cf6c252SPaul Traina static char path_vmunix[] = "/hp-ux";
18288cf6c252SPaul Traina 
18298cf6c252SPaul Traina /* Determine ppa number that specifies ifname */
18308cf6c252SPaul Traina static int
1831b00ab754SHans Petter Selasky get_dlpi_ppa(register int fd, register const char *ifname, register u_int unit,
1832b00ab754SHans Petter Selasky     u_int *ppa, register char *ebuf)
18338cf6c252SPaul Traina {
18348cf6c252SPaul Traina 	register const char *cp;
18358cf6c252SPaul Traina 	register int kd;
18368cf6c252SPaul Traina 	void *addr;
18378cf6c252SPaul Traina 	struct ifnet ifnet;
1838dc2c7305SBill Fenner 	char if_name[sizeof(ifnet.if_name) + 1];
18398cf6c252SPaul Traina 
18408cf6c252SPaul Traina 	cp = strrchr(ifname, '/');
18418cf6c252SPaul Traina 	if (cp != NULL)
18428cf6c252SPaul Traina 		ifname = cp + 1;
18438cf6c252SPaul Traina 	if (nlist(path_vmunix, &nl) < 0) {
18446f9cba8fSJoseph Mingrone 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "nlist %s failed",
1845dc2c7305SBill Fenner 		    path_vmunix);
1846b00ab754SHans Petter Selasky 		return (PCAP_ERROR);
18478cf6c252SPaul Traina 	}
18488cf6c252SPaul Traina 	if (nl[NL_IFNET].n_value == 0) {
18496f9cba8fSJoseph Mingrone 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
18506f9cba8fSJoseph Mingrone 		    "couldn't find %s kernel symbol",
18518cf6c252SPaul Traina 		    nl[NL_IFNET].n_name);
1852b00ab754SHans Petter Selasky 		return (PCAP_ERROR);
18538cf6c252SPaul Traina 	}
18548cf6c252SPaul Traina 	kd = open("/dev/kmem", O_RDONLY);
18558cf6c252SPaul Traina 	if (kd < 0) {
1856*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1857b00ab754SHans Petter Selasky 		    errno, "kmem open");
1858b00ab754SHans Petter Selasky 		return (PCAP_ERROR);
18598cf6c252SPaul Traina 	}
18608cf6c252SPaul Traina 	if (dlpi_kread(kd, nl[NL_IFNET].n_value,
18618cf6c252SPaul Traina 	    &addr, sizeof(addr), ebuf) < 0) {
18628cf6c252SPaul Traina 		close(kd);
1863b00ab754SHans Petter Selasky 		return (PCAP_ERROR);
18648cf6c252SPaul Traina 	}
18658cf6c252SPaul Traina 	for (; addr != NULL; addr = ifnet.if_next) {
18668cf6c252SPaul Traina 		if (dlpi_kread(kd, (off_t)addr,
18678cf6c252SPaul Traina 		    &ifnet, sizeof(ifnet), ebuf) < 0 ||
18688cf6c252SPaul Traina 		    dlpi_kread(kd, (off_t)ifnet.if_name,
1869dc2c7305SBill Fenner 		    if_name, sizeof(ifnet.if_name), ebuf) < 0) {
18708cf6c252SPaul Traina 			(void)close(kd);
1871b00ab754SHans Petter Selasky 			return (PCAP_ERROR);
18728cf6c252SPaul Traina 		}
1873dc2c7305SBill Fenner 		if_name[sizeof(ifnet.if_name)] = '\0';
1874b00ab754SHans Petter Selasky 		if (strcmp(if_name, ifname) == 0 && ifnet.if_unit == unit) {
1875b00ab754SHans Petter Selasky 			*ppa = ifnet.if_index;
1876b00ab754SHans Petter Selasky 			return (0);
1877b00ab754SHans Petter Selasky 		}
18788cf6c252SPaul Traina 	}
18798cf6c252SPaul Traina 
18806f9cba8fSJoseph Mingrone 	snprintf(ebuf, PCAP_ERRBUF_SIZE, "Can't find %s", ifname);
1881b00ab754SHans Petter Selasky 	return (PCAP_ERROR_NO_SUCH_DEVICE);
18828cf6c252SPaul Traina }
18838cf6c252SPaul Traina 
18848cf6c252SPaul Traina static int
18858cf6c252SPaul Traina dlpi_kread(register int fd, register off_t addr,
18868cf6c252SPaul Traina     register void *buf, register u_int len, register char *ebuf)
18878cf6c252SPaul Traina {
18888cf6c252SPaul Traina 	register int cc;
18898cf6c252SPaul Traina 
1890a4b5b39fSBill Fenner 	if (lseek(fd, addr, SEEK_SET) < 0) {
1891*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1892b00ab754SHans Petter Selasky 		    errno, "lseek");
18938cf6c252SPaul Traina 		return (-1);
18948cf6c252SPaul Traina 	}
18958cf6c252SPaul Traina 	cc = read(fd, buf, len);
18968cf6c252SPaul Traina 	if (cc < 0) {
1897*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE,
1898b00ab754SHans Petter Selasky 		    errno, "read");
18998cf6c252SPaul Traina 		return (-1);
19008cf6c252SPaul Traina 	} else if (cc != len) {
19016f9cba8fSJoseph Mingrone 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "short read (%d != %d)", cc,
1902dc2c7305SBill Fenner 		    len);
19038cf6c252SPaul Traina 		return (-1);
19048cf6c252SPaul Traina 	}
19058cf6c252SPaul Traina 	return (cc);
19068cf6c252SPaul Traina }
19078cf6c252SPaul Traina #endif
1908a8e07101SRui Paulo 
1909a8e07101SRui Paulo pcap_t *
1910*afdbf109SJoseph Mingrone pcapint_create_interface(const char *device _U_, char *ebuf)
1911a8e07101SRui Paulo {
1912a8e07101SRui Paulo 	pcap_t *p;
1913681ed54cSXin LI #ifdef DL_HP_RAWDLS
1914681ed54cSXin LI 	struct pcap_dlpi *pd;
1915681ed54cSXin LI #endif
1916a8e07101SRui Paulo 
19176f9cba8fSJoseph Mingrone 	p = PCAP_CREATE_COMMON(ebuf, struct pcap_dlpi);
1918a8e07101SRui Paulo 	if (p == NULL)
1919a8e07101SRui Paulo 		return (NULL);
1920a8e07101SRui Paulo 
1921681ed54cSXin LI #ifdef DL_HP_RAWDLS
1922681ed54cSXin LI 	pd = p->priv;
1923681ed54cSXin LI 	pd->send_fd = -1;	/* it hasn't been opened yet */
1924681ed54cSXin LI #endif
1925a8e07101SRui Paulo 
1926a8e07101SRui Paulo 	p->activate_op = pcap_activate_dlpi;
1927a8e07101SRui Paulo 	return (p);
1928a8e07101SRui Paulo }
1929b00ab754SHans Petter Selasky 
1930b00ab754SHans Petter Selasky /*
1931b00ab754SHans Petter Selasky  * Libpcap version string.
1932b00ab754SHans Petter Selasky  */
1933b00ab754SHans Petter Selasky const char *
1934b00ab754SHans Petter Selasky pcap_lib_version(void)
1935b00ab754SHans Petter Selasky {
1936b00ab754SHans Petter Selasky 	return (PCAP_VERSION_STRING);
1937b00ab754SHans Petter Selasky }
1938