xref: /freebsd/contrib/libpcap/pcap-dlpi.c (revision 681ed54caa769e19ddacdf0658fcc5ac5ea7cf3f)
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 
71dc2c7305SBill Fenner #ifdef HAVE_CONFIG_H
72dc2c7305SBill Fenner #include "config.h"
733052b236SBill Fenner #endif
743052b236SBill Fenner 
758cf6c252SPaul Traina #include <sys/types.h>
768cf6c252SPaul Traina #include <sys/time.h>
778cf6c252SPaul Traina #ifdef HAVE_SYS_BUFMOD_H
788cf6c252SPaul Traina #include <sys/bufmod.h>
798cf6c252SPaul Traina #endif
808cf6c252SPaul Traina #include <sys/dlpi.h>
818cf6c252SPaul Traina #ifdef HAVE_SYS_DLPI_EXT_H
828cf6c252SPaul Traina #include <sys/dlpi_ext.h>
838cf6c252SPaul Traina #endif
848cf6c252SPaul Traina #ifdef HAVE_HPUX9
858cf6c252SPaul Traina #include <sys/socket.h>
868cf6c252SPaul Traina #endif
8704fb2745SSam Leffler #ifdef DL_HP_PPA_REQ
888cf6c252SPaul Traina #include <sys/stat.h>
898cf6c252SPaul Traina #endif
908cf6c252SPaul Traina #include <sys/stream.h>
918cf6c252SPaul Traina #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
928cf6c252SPaul Traina #include <sys/systeminfo.h>
938cf6c252SPaul Traina #endif
948cf6c252SPaul Traina 
958cf6c252SPaul Traina #ifdef HAVE_HPUX9
968cf6c252SPaul Traina #include <net/if.h>
978cf6c252SPaul Traina #endif
988cf6c252SPaul Traina 
998cf6c252SPaul Traina #include <ctype.h>
1008cf6c252SPaul Traina #ifdef HAVE_HPUX9
1018cf6c252SPaul Traina #include <nlist.h>
1028cf6c252SPaul Traina #endif
1038cf6c252SPaul Traina #include <errno.h>
1048cf6c252SPaul Traina #include <fcntl.h>
1058cf6c252SPaul Traina #include <memory.h>
1068cf6c252SPaul Traina #include <stdio.h>
1078cf6c252SPaul Traina #include <stdlib.h>
1088cf6c252SPaul Traina #include <string.h>
1098cf6c252SPaul Traina #include <stropts.h>
1108cf6c252SPaul Traina #include <unistd.h>
1118cf6c252SPaul Traina 
11204fb2745SSam Leffler #ifdef HAVE_LIMITS_H
11304fb2745SSam Leffler #include <limits.h>
11404fb2745SSam Leffler #else
11504fb2745SSam Leffler #define INT_MAX		2147483647
11604fb2745SSam Leffler #endif
11704fb2745SSam Leffler 
1188cf6c252SPaul Traina #include "pcap-int.h"
119a8e07101SRui Paulo #include "dlpisubs.h"
1208cf6c252SPaul Traina 
1218cf6c252SPaul Traina #ifdef HAVE_OS_PROTO_H
1228cf6c252SPaul Traina #include "os-proto.h"
1238cf6c252SPaul Traina #endif
1248cf6c252SPaul Traina 
1258cf6c252SPaul Traina #ifndef PCAP_DEV_PREFIX
1260a94d38fSBill Fenner #ifdef _AIX
1270a94d38fSBill Fenner #define PCAP_DEV_PREFIX "/dev/dlpi"
1280a94d38fSBill Fenner #else
1298cf6c252SPaul Traina #define PCAP_DEV_PREFIX "/dev"
1308cf6c252SPaul Traina #endif
1310a94d38fSBill Fenner #endif
1328cf6c252SPaul Traina 
1338cf6c252SPaul Traina #define	MAXDLBUF	8192
1348cf6c252SPaul Traina 
1358cf6c252SPaul Traina /* Forwards */
1360a94d38fSBill Fenner static char *split_dname(char *, int *, char *);
13704fb2745SSam Leffler static int dl_doattach(int, int, char *);
138ee2dd488SSam Leffler #ifdef DL_HP_RAWDLS
139ee2dd488SSam Leffler static int dl_dohpuxbind(int, char *);
140ee2dd488SSam Leffler #endif
141d1e87331SXin LI static int dlpromiscon(pcap_t *, bpf_u_int32);
1428cf6c252SPaul Traina static int dlbindreq(int, bpf_u_int32, char *);
143ee2dd488SSam Leffler static int dlbindack(int, char *, char *, int *);
1448cf6c252SPaul Traina static int dlokack(int, const char *, char *, char *);
14504fb2745SSam Leffler static int dlinforeq(int, char *);
14604fb2745SSam Leffler static int dlinfoack(int, char *, char *);
147a8e07101SRui Paulo 
148a8e07101SRui Paulo #ifdef HAVE_DLPI_PASSIVE
149a8e07101SRui Paulo static void dlpassive(int, char *);
150a8e07101SRui Paulo #endif
151a8e07101SRui Paulo 
15204fb2745SSam Leffler #ifdef DL_HP_RAWDLS
15304fb2745SSam Leffler static int dlrawdatareq(int, const u_char *, int);
15404fb2745SSam Leffler #endif
155ee2dd488SSam Leffler static int recv_ack(int, int, const char *, char *, char *, int *);
1560a94d38fSBill Fenner static char *dlstrerror(bpf_u_int32);
1570a94d38fSBill Fenner static char *dlprim(bpf_u_int32);
1588cf6c252SPaul Traina #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
1598cf6c252SPaul Traina static char *get_release(bpf_u_int32 *, bpf_u_int32 *, bpf_u_int32 *);
1608cf6c252SPaul Traina #endif
1618cf6c252SPaul Traina static int send_request(int, char *, int, char *, char *);
1628cf6c252SPaul Traina #ifdef HAVE_HPUX9
1638cf6c252SPaul Traina static int dlpi_kread(int, off_t, void *, u_int, char *);
1648cf6c252SPaul Traina #endif
1658cf6c252SPaul Traina #ifdef HAVE_DEV_DLPI
1668cf6c252SPaul Traina static int get_dlpi_ppa(int, const char *, int, char *);
1678cf6c252SPaul Traina #endif
1688cf6c252SPaul Traina 
1698cf6c252SPaul Traina /* XXX Needed by HP-UX (at least) */
1708cf6c252SPaul Traina static bpf_u_int32 ctlbuf[MAXDLBUF];
1718cf6c252SPaul Traina static struct strbuf ctl = {
1728cf6c252SPaul Traina 	MAXDLBUF,
1738cf6c252SPaul Traina 	0,
1748cf6c252SPaul Traina 	(char *)ctlbuf
1758cf6c252SPaul Traina };
1768cf6c252SPaul Traina 
177a0ee43a1SRui Paulo /*
178a0ee43a1SRui Paulo  * Cast a buffer to "union DL_primitives" without provoking warnings
179a0ee43a1SRui Paulo  * from the compiler.
180a0ee43a1SRui Paulo  */
181a0ee43a1SRui Paulo #define MAKE_DL_PRIMITIVES(ptr)	((union DL_primitives *)(void *)(ptr))
182a0ee43a1SRui Paulo 
183feb4ecdbSBruce M Simpson static int
184feb4ecdbSBruce M Simpson pcap_read_dlpi(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
1858cf6c252SPaul Traina {
186a8e07101SRui Paulo 	int cc;
187a8e07101SRui Paulo 	u_char *bp;
1888cf6c252SPaul Traina 	int flags;
1898cf6c252SPaul Traina 	struct strbuf data;
1908cf6c252SPaul Traina 
1918cf6c252SPaul Traina 	flags = 0;
1928cf6c252SPaul Traina 	cc = p->cc;
1938cf6c252SPaul Traina 	if (cc == 0) {
1948cf6c252SPaul Traina 		data.buf = (char *)p->buffer + p->offset;
195feb4ecdbSBruce M Simpson 		data.maxlen = p->bufsize;
1968cf6c252SPaul Traina 		data.len = 0;
1978cf6c252SPaul Traina 		do {
198feb4ecdbSBruce M Simpson 			/*
199feb4ecdbSBruce M Simpson 			 * Has "pcap_breakloop()" been called?
200feb4ecdbSBruce M Simpson 			 */
201feb4ecdbSBruce M Simpson 			if (p->break_loop) {
202feb4ecdbSBruce M Simpson 				/*
203feb4ecdbSBruce M Simpson 				 * Yes - clear the flag that indicates
204feb4ecdbSBruce M Simpson 				 * that it has, and return -2 to
205feb4ecdbSBruce M Simpson 				 * indicate that we were told to
206feb4ecdbSBruce M Simpson 				 * break out of the loop.
207feb4ecdbSBruce M Simpson 				 */
208feb4ecdbSBruce M Simpson 				p->break_loop = 0;
209feb4ecdbSBruce M Simpson 				return (-2);
210feb4ecdbSBruce M Simpson 			}
21104fb2745SSam Leffler 			/*
21204fb2745SSam Leffler 			 * XXX - check for the DLPI primitive, which
21304fb2745SSam Leffler 			 * would be DL_HP_RAWDATA_IND on HP-UX
21404fb2745SSam Leffler 			 * if we're in raw mode?
21504fb2745SSam Leffler 			 */
2168cf6c252SPaul Traina 			if (getmsg(p->fd, &ctl, &data, &flags) < 0) {
2178cf6c252SPaul Traina 				/* Don't choke when we get ptraced */
21804fb2745SSam Leffler 				switch (errno) {
21904fb2745SSam Leffler 
22004fb2745SSam Leffler 				case EINTR:
2218cf6c252SPaul Traina 					cc = 0;
2228cf6c252SPaul Traina 					continue;
22304fb2745SSam Leffler 
22404fb2745SSam Leffler 				case EAGAIN:
22504fb2745SSam Leffler 					return (0);
2268cf6c252SPaul Traina 				}
227dc2c7305SBill Fenner 				strlcpy(p->errbuf, pcap_strerror(errno),
228dc2c7305SBill Fenner 				    sizeof(p->errbuf));
2298cf6c252SPaul Traina 				return (-1);
2308cf6c252SPaul Traina 			}
2318cf6c252SPaul Traina 			cc = data.len;
2328cf6c252SPaul Traina 		} while (cc == 0);
2338cf6c252SPaul Traina 		bp = p->buffer + p->offset;
2348cf6c252SPaul Traina 	} else
2358cf6c252SPaul Traina 		bp = p->bp;
2368cf6c252SPaul Traina 
237a8e07101SRui Paulo 	return (pcap_process_pkts(p, callback, user, cnt, bp, cc));
2388cf6c252SPaul Traina }
2398cf6c252SPaul Traina 
24004fb2745SSam Leffler static int
24104fb2745SSam Leffler pcap_inject_dlpi(pcap_t *p, const void *buf, size_t size)
24204fb2745SSam Leffler {
243*681ed54cSXin LI #ifdef DL_HP_RAWDLS
244*681ed54cSXin LI 	struct pcap_dlpi *pd = p->priv;
245*681ed54cSXin LI #endif
24604fb2745SSam Leffler 	int ret;
24704fb2745SSam Leffler 
24804fb2745SSam Leffler #if defined(DLIOCRAW)
24904fb2745SSam Leffler 	ret = write(p->fd, buf, size);
25004fb2745SSam Leffler 	if (ret == -1) {
25104fb2745SSam Leffler 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
25204fb2745SSam Leffler 		    pcap_strerror(errno));
25304fb2745SSam Leffler 		return (-1);
25404fb2745SSam Leffler 	}
25504fb2745SSam Leffler #elif defined(DL_HP_RAWDLS)
256*681ed54cSXin LI 	if (pd->send_fd < 0) {
25704fb2745SSam Leffler 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
25804fb2745SSam Leffler 		    "send: Output FD couldn't be opened");
25904fb2745SSam Leffler 		return (-1);
26004fb2745SSam Leffler 	}
261*681ed54cSXin LI 	ret = dlrawdatareq(pd->send_fd, buf, size);
26204fb2745SSam Leffler 	if (ret == -1) {
26304fb2745SSam Leffler 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s",
26404fb2745SSam Leffler 		    pcap_strerror(errno));
26504fb2745SSam Leffler 		return (-1);
26604fb2745SSam Leffler 	}
267ef96d74fSMax Laier 	/*
268ef96d74fSMax Laier 	 * putmsg() returns either 0 or -1; it doesn't indicate how
269ef96d74fSMax Laier 	 * many bytes were written (presumably they were all written
270ef96d74fSMax Laier 	 * or none of them were written).  OpenBSD's pcap_inject()
271ef96d74fSMax Laier 	 * returns the number of bytes written, so, for API compatibility,
272ef96d74fSMax Laier 	 * we return the number of bytes we were told to write.
273ef96d74fSMax Laier 	 */
274ef96d74fSMax Laier 	ret = size;
27504fb2745SSam Leffler #else /* no raw mode */
27604fb2745SSam Leffler 	/*
27704fb2745SSam Leffler 	 * XXX - this is a pain, because you might have to extract
27804fb2745SSam Leffler 	 * the address from the packet and use it in a DL_UNITDATA_REQ
27904fb2745SSam Leffler 	 * request.  That would be dependent on the link-layer type.
28004fb2745SSam Leffler 	 *
28104fb2745SSam Leffler 	 * I also don't know what SAP you'd have to bind the descriptor
28204fb2745SSam Leffler 	 * to, or whether you'd need separate "receive" and "send" FDs,
28304fb2745SSam Leffler 	 * nor do I know whether you'd need different bindings for
28404fb2745SSam Leffler 	 * D/I/X Ethernet and 802.3, or for {FDDI,Token Ring} plus
28504fb2745SSam Leffler 	 * 802.2 and {FDDI,Token Ring} plus 802.2 plus SNAP.
28604fb2745SSam Leffler 	 *
28704fb2745SSam Leffler 	 * So, for now, we just return a "you can't send" indication,
28804fb2745SSam Leffler 	 * and leave it up to somebody with a DLPI-based system lacking
28904fb2745SSam Leffler 	 * both DLIOCRAW and DL_HP_RAWDLS to supply code to implement
29004fb2745SSam Leffler 	 * packet transmission on that system.  If they do, they should
29104fb2745SSam Leffler 	 * send it to us - but should not send us code that assumes
29204fb2745SSam Leffler 	 * Ethernet; if the code doesn't work on non-Ethernet interfaces,
29304fb2745SSam Leffler 	 * it should check "p->linktype" and reject the send request if
29404fb2745SSam Leffler 	 * it's anything other than DLT_EN10MB.
29504fb2745SSam Leffler 	 */
29604fb2745SSam Leffler 	strlcpy(p->errbuf, "send: Not supported on this version of this OS",
29704fb2745SSam Leffler 	    PCAP_ERRBUF_SIZE);
29804fb2745SSam Leffler 	ret = -1;
29904fb2745SSam Leffler #endif /* raw mode */
30004fb2745SSam Leffler 	return (ret);
30104fb2745SSam Leffler }
30204fb2745SSam Leffler 
303feb4ecdbSBruce M Simpson #ifndef DL_IPATM
304feb4ecdbSBruce M Simpson #define DL_IPATM	0x12	/* ATM Classical IP interface */
305feb4ecdbSBruce M Simpson #endif
306feb4ecdbSBruce M Simpson 
307feb4ecdbSBruce M Simpson #ifdef HAVE_SOLARIS
308feb4ecdbSBruce M Simpson /*
309feb4ecdbSBruce M Simpson  * For SunATM.
310feb4ecdbSBruce M Simpson  */
311feb4ecdbSBruce M Simpson #ifndef A_GET_UNITS
312feb4ecdbSBruce M Simpson #define A_GET_UNITS	(('A'<<8)|118)
313feb4ecdbSBruce M Simpson #endif /* A_GET_UNITS */
314feb4ecdbSBruce M Simpson #ifndef A_PROMISCON_REQ
315feb4ecdbSBruce M Simpson #define A_PROMISCON_REQ	(('A'<<8)|121)
316feb4ecdbSBruce M Simpson #endif /* A_PROMISCON_REQ */
317feb4ecdbSBruce M Simpson #endif /* HAVE_SOLARIS */
318feb4ecdbSBruce M Simpson 
319feb4ecdbSBruce M Simpson static void
320a8e07101SRui Paulo pcap_cleanup_dlpi(pcap_t *p)
321feb4ecdbSBruce M Simpson {
322*681ed54cSXin LI #ifdef DL_HP_RAWDLS
323*681ed54cSXin LI 	struct pcap_dlpi *pd = p->priv;
324*681ed54cSXin LI 
325*681ed54cSXin LI 	if (pd->send_fd >= 0) {
326*681ed54cSXin LI 		close(pd->send_fd);
327*681ed54cSXin LI 		pd->send_fd = -1;
328a8e07101SRui Paulo 	}
329*681ed54cSXin LI #endif
330a8e07101SRui Paulo 	pcap_cleanup_live_common(p);
331feb4ecdbSBruce M Simpson }
332feb4ecdbSBruce M Simpson 
333a8e07101SRui Paulo static int
334a8e07101SRui Paulo pcap_activate_dlpi(pcap_t *p)
3358cf6c252SPaul Traina {
336*681ed54cSXin LI #ifdef DL_HP_RAWDLS
337*681ed54cSXin LI 	struct pcap_dlpi *pd = p->priv;
338*681ed54cSXin LI #endif
339*681ed54cSXin LI 	int status = 0;
340*681ed54cSXin LI 	int retv;
3418cf6c252SPaul Traina 	register char *cp;
3420a94d38fSBill Fenner 	int ppa;
343feb4ecdbSBruce M Simpson #ifdef HAVE_SOLARIS
344feb4ecdbSBruce M Simpson 	int isatm = 0;
345feb4ecdbSBruce M Simpson #endif
3468cf6c252SPaul Traina 	register dl_info_ack_t *infop;
3478cf6c252SPaul Traina #ifdef HAVE_SYS_BUFMOD_H
348a8e07101SRui Paulo 	bpf_u_int32 ss;
3498cf6c252SPaul Traina #ifdef HAVE_SOLARIS
3508cf6c252SPaul Traina 	register char *release;
3518cf6c252SPaul Traina 	bpf_u_int32 osmajor, osminor, osmicro;
3528cf6c252SPaul Traina #endif
3538cf6c252SPaul Traina #endif
3548cf6c252SPaul Traina 	bpf_u_int32 buf[MAXDLBUF];
3558cf6c252SPaul Traina 	char dname[100];
3568cf6c252SPaul Traina #ifndef HAVE_DEV_DLPI
3578cf6c252SPaul Traina 	char dname2[100];
3588cf6c252SPaul Traina #endif
3598cf6c252SPaul Traina 
360dc2c7305SBill Fenner #ifdef HAVE_DEV_DLPI
361dc2c7305SBill Fenner 	/*
362dc2c7305SBill Fenner 	** Remove any "/dev/" on the front of the device.
363dc2c7305SBill Fenner 	*/
364a8e07101SRui Paulo 	cp = strrchr(p->opt.source, '/');
365dc2c7305SBill Fenner 	if (cp == NULL)
366a8e07101SRui Paulo 		strlcpy(dname, p->opt.source, sizeof(dname));
367dc2c7305SBill Fenner 	else
368feb4ecdbSBruce M Simpson 		strlcpy(dname, cp + 1, sizeof(dname));
369dc2c7305SBill Fenner 
370dc2c7305SBill Fenner 	/*
3710a94d38fSBill Fenner 	 * Split the device name into a device type name and a unit number;
3720a94d38fSBill Fenner 	 * chop off the unit number, so "dname" is just a device type name.
373dc2c7305SBill Fenner 	 */
374a8e07101SRui Paulo 	cp = split_dname(dname, &ppa, p->errbuf);
375a8e07101SRui Paulo 	if (cp == NULL) {
376a8e07101SRui Paulo 		status = PCAP_ERROR_NO_SUCH_DEVICE;
377dc2c7305SBill Fenner 		goto bad;
378a8e07101SRui Paulo 	}
379dc2c7305SBill Fenner 	*cp = '\0';
380dc2c7305SBill Fenner 
381dc2c7305SBill Fenner 	/*
382dc2c7305SBill Fenner 	 * Use "/dev/dlpi" as the device.
383dc2c7305SBill Fenner 	 *
384dc2c7305SBill Fenner 	 * XXX - HP's DLPI Programmer's Guide for HP-UX 11.00 says that
385dc2c7305SBill Fenner 	 * the "dl_mjr_num" field is for the "major number of interface
386dc2c7305SBill Fenner 	 * driver"; that's the major of "/dev/dlpi" on the system on
387dc2c7305SBill Fenner 	 * which I tried this, but there may be DLPI devices that
388dc2c7305SBill Fenner 	 * use a different driver, in which case we may need to
389dc2c7305SBill Fenner 	 * search "/dev" for the appropriate device with that major
390dc2c7305SBill Fenner 	 * device number, rather than hardwiring "/dev/dlpi".
391dc2c7305SBill Fenner 	 */
392dc2c7305SBill Fenner 	cp = "/dev/dlpi";
393dc2c7305SBill Fenner 	if ((p->fd = open(cp, O_RDWR)) < 0) {
394a8e07101SRui Paulo 		if (errno == EPERM || errno == EACCES)
395a8e07101SRui Paulo 			status = PCAP_ERROR_PERM_DENIED;
396*681ed54cSXin LI 		else
397*681ed54cSXin LI 			status = PCAP_ERROR;
398a8e07101SRui Paulo 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
399dc2c7305SBill Fenner 		    "%s: %s", cp, pcap_strerror(errno));
400dc2c7305SBill Fenner 		goto bad;
401dc2c7305SBill Fenner 	}
402dc2c7305SBill Fenner 
40304fb2745SSam Leffler #ifdef DL_HP_RAWDLS
40404fb2745SSam Leffler 	/*
40504fb2745SSam Leffler 	 * XXX - HP-UX 10.20 and 11.xx don't appear to support sending and
406ee2dd488SSam Leffler 	 * receiving packets on the same descriptor - you need separate
407ee2dd488SSam Leffler 	 * descriptors for sending and receiving, bound to different SAPs.
40804fb2745SSam Leffler 	 *
409*681ed54cSXin LI 	 * If the open fails, we just leave -1 in "pd->send_fd" and reject
41004fb2745SSam Leffler 	 * attempts to send packets, just as if, in pcap-bpf.c, we fail
41104fb2745SSam Leffler 	 * to open the BPF device for reading and writing, we just try
41204fb2745SSam Leffler 	 * to open it for reading only and, if that succeeds, just let
41304fb2745SSam Leffler 	 * the send attempts fail.
41404fb2745SSam Leffler 	 */
415*681ed54cSXin LI 	pd->send_fd = open(cp, O_RDWR);
41604fb2745SSam Leffler #endif
41704fb2745SSam Leffler 
418dc2c7305SBill Fenner 	/*
419dc2c7305SBill Fenner 	 * Get a table of all PPAs for that device, and search that
420dc2c7305SBill Fenner 	 * table for the specified device type name and unit number.
421dc2c7305SBill Fenner 	 */
422a8e07101SRui Paulo 	ppa = get_dlpi_ppa(p->fd, dname, ppa, p->errbuf);
423a8e07101SRui Paulo 	if (ppa < 0) {
424a8e07101SRui Paulo 		status = ppa;
425dc2c7305SBill Fenner 		goto bad;
426a8e07101SRui Paulo 	}
427dc2c7305SBill Fenner #else
4288cf6c252SPaul Traina 	/*
4290a94d38fSBill Fenner 	 * If the device name begins with "/", assume it begins with
4300a94d38fSBill Fenner 	 * the pathname of the directory containing the device to open;
4310a94d38fSBill Fenner 	 * otherwise, concatenate the device directory name and the
4320a94d38fSBill Fenner 	 * device name.
4330a94d38fSBill Fenner 	 */
434a8e07101SRui Paulo 	if (*p->opt.source == '/')
435a8e07101SRui Paulo 		strlcpy(dname, p->opt.source, sizeof(dname));
4368cf6c252SPaul Traina 	else
437dc2c7305SBill Fenner 		snprintf(dname, sizeof(dname), "%s/%s", PCAP_DEV_PREFIX,
438a8e07101SRui Paulo 		    p->opt.source);
439dc2c7305SBill Fenner 
4400a94d38fSBill Fenner 	/*
441feb4ecdbSBruce M Simpson 	 * Get the unit number, and a pointer to the end of the device
442feb4ecdbSBruce M Simpson 	 * type name.
443feb4ecdbSBruce M Simpson 	 */
444a8e07101SRui Paulo 	cp = split_dname(dname, &ppa, p->errbuf);
445a8e07101SRui Paulo 	if (cp == NULL) {
446a8e07101SRui Paulo 		status = PCAP_ERROR_NO_SUCH_DEVICE;
447feb4ecdbSBruce M Simpson 		goto bad;
448a8e07101SRui Paulo 	}
449feb4ecdbSBruce M Simpson 
450feb4ecdbSBruce M Simpson 	/*
4510a94d38fSBill Fenner 	 * Make a copy of the device pathname, and then remove the unit
4520a94d38fSBill Fenner 	 * number from the device pathname.
4530a94d38fSBill Fenner 	 */
4540a94d38fSBill Fenner 	strlcpy(dname2, dname, sizeof(dname));
455feb4ecdbSBruce M Simpson 	*cp = '\0';
4560a94d38fSBill Fenner 
4578cf6c252SPaul Traina 	/* Try device without unit number */
4588cf6c252SPaul Traina 	if ((p->fd = open(dname, O_RDWR)) < 0) {
4598cf6c252SPaul Traina 		if (errno != ENOENT) {
460d1e87331SXin LI 			if (errno == EPERM || errno == EACCES)
461a8e07101SRui Paulo 				status = PCAP_ERROR_PERM_DENIED;
462*681ed54cSXin LI 			else
463*681ed54cSXin LI 				status = PCAP_ERROR;
464a8e07101SRui Paulo 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s", dname,
465dc2c7305SBill Fenner 			    pcap_strerror(errno));
4668cf6c252SPaul Traina 			goto bad;
4678cf6c252SPaul Traina 		}
4688cf6c252SPaul Traina 
4698cf6c252SPaul Traina 		/* Try again with unit number */
4708cf6c252SPaul Traina 		if ((p->fd = open(dname2, O_RDWR)) < 0) {
47104fb2745SSam Leffler 			if (errno == ENOENT) {
472a8e07101SRui Paulo 				status = PCAP_ERROR_NO_SUCH_DEVICE;
473a8e07101SRui Paulo 
47404fb2745SSam Leffler 				/*
475a8e07101SRui Paulo 				 * We provide an error message even
476a8e07101SRui Paulo 				 * for this error, for diagnostic
477a8e07101SRui Paulo 				 * purposes (so that, for example,
478a8e07101SRui Paulo 				 * the app can show the message if the
479a8e07101SRui Paulo 				 * user requests it).
480a8e07101SRui Paulo 				 *
481a8e07101SRui Paulo 				 * In it, we just report "No DLPI device
482a8e07101SRui Paulo 				 * found" with the device name, so people
483a8e07101SRui Paulo 				 * don't get confused and think, for example,
48404fb2745SSam Leffler 				 * that if they can't capture on "lo0"
48504fb2745SSam Leffler 				 * on Solaris the fix is to change libpcap
48604fb2745SSam Leffler 				 * (or the application that uses it) to
48704fb2745SSam Leffler 				 * look for something other than "/dev/lo0",
48804fb2745SSam Leffler 				 * as the fix is to look for an operating
48904fb2745SSam Leffler 				 * system other than Solaris - you just
49004fb2745SSam Leffler 				 * *can't* capture on a loopback interface
49104fb2745SSam Leffler 				 * on Solaris, the lack of a DLPI device
49204fb2745SSam Leffler 				 * for the loopback interface is just a
49304fb2745SSam Leffler 				 * symptom of that inability.
49404fb2745SSam Leffler 				 */
495a8e07101SRui Paulo 				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
496a8e07101SRui Paulo 				    "%s: No DLPI device found", p->opt.source);
49704fb2745SSam Leffler 			} else {
498d1e87331SXin LI 				if (errno == EPERM || errno == EACCES)
499a8e07101SRui Paulo 					status = PCAP_ERROR_PERM_DENIED;
500*681ed54cSXin LI 				else
501*681ed54cSXin LI 					status = PCAP_ERROR;
502a8e07101SRui Paulo 				snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
50304fb2745SSam Leffler 				    dname2, pcap_strerror(errno));
50404fb2745SSam Leffler 			}
5058cf6c252SPaul Traina 			goto bad;
5068cf6c252SPaul Traina 		}
5078cf6c252SPaul Traina 		/* XXX Assume unit zero */
5088cf6c252SPaul Traina 		ppa = 0;
5098cf6c252SPaul Traina 	}
5108cf6c252SPaul Traina #endif
5118cf6c252SPaul Traina 
5128cf6c252SPaul Traina 	/*
5138cf6c252SPaul Traina 	** Attach if "style 2" provider
5148cf6c252SPaul Traina 	*/
515a8e07101SRui Paulo 	if (dlinforeq(p->fd, p->errbuf) < 0 ||
516*681ed54cSXin LI 	    dlinfoack(p->fd, (char *)buf, p->errbuf) < 0) {
517*681ed54cSXin LI 		status = PCAP_ERROR;
5188cf6c252SPaul Traina 		goto bad;
519*681ed54cSXin LI 	}
520a0ee43a1SRui Paulo 	infop = &(MAKE_DL_PRIMITIVES(buf))->info_ack;
521feb4ecdbSBruce M Simpson #ifdef HAVE_SOLARIS
522feb4ecdbSBruce M Simpson 	if (infop->dl_mac_type == DL_IPATM)
523feb4ecdbSBruce M Simpson 		isatm = 1;
524feb4ecdbSBruce M Simpson #endif
52504fb2745SSam Leffler 	if (infop->dl_provider_style == DL_STYLE2) {
526*681ed54cSXin LI 		retv = dl_doattach(p->fd, ppa, p->errbuf);
527*681ed54cSXin LI 		if (retv < 0) {
528*681ed54cSXin LI 			status = retv;
5298cf6c252SPaul Traina 			goto bad;
530*681ed54cSXin LI 		}
53104fb2745SSam Leffler #ifdef DL_HP_RAWDLS
532*681ed54cSXin LI 		if (pd->send_fd >= 0) {
533*681ed54cSXin LI 			retv = dl_doattach(pd->send_fd, ppa, p->errbuf);
534*681ed54cSXin LI 			if (retv < 0) {
535*681ed54cSXin LI 				status = retv;
53604fb2745SSam Leffler 				goto bad;
53704fb2745SSam Leffler 			}
538*681ed54cSXin LI 		}
53904fb2745SSam Leffler #endif
54004fb2745SSam Leffler 	}
54104fb2745SSam Leffler 
542a8e07101SRui Paulo 	if (p->opt.rfmon) {
543a8e07101SRui Paulo 		/*
544a8e07101SRui Paulo 		 * This device exists, but we don't support monitor mode
545a8e07101SRui Paulo 		 * any platforms that support DLPI.
546a8e07101SRui Paulo 		 */
547a8e07101SRui Paulo 		status = PCAP_ERROR_RFMON_NOTSUP;
548a8e07101SRui Paulo 		goto bad;
549a8e07101SRui Paulo 	}
550a8e07101SRui Paulo 
551a8e07101SRui Paulo #ifdef HAVE_DLPI_PASSIVE
552a8e07101SRui Paulo 	/*
553a8e07101SRui Paulo 	 * Enable Passive mode to be able to capture on aggregated link.
554a8e07101SRui Paulo 	 * Not supported in all Solaris versions.
555a8e07101SRui Paulo 	 */
556a8e07101SRui Paulo 	dlpassive(p->fd, p->errbuf);
557a8e07101SRui Paulo #endif
5588cf6c252SPaul Traina 	/*
559ee2dd488SSam Leffler 	** Bind (defer if using HP-UX 9 or HP-UX 10.20 or later, totally
560ee2dd488SSam Leffler 	** skip if using SINIX)
5618cf6c252SPaul Traina 	*/
562ee2dd488SSam Leffler #if !defined(HAVE_HPUX9) && !defined(HAVE_HPUX10_20_OR_LATER) && !defined(sinix)
563dc2c7305SBill Fenner #ifdef _AIX
564ee2dd488SSam Leffler 	/*
565ee2dd488SSam Leffler 	** AIX.
566ee2dd488SSam Leffler 	** According to IBM's AIX Support Line, the dl_sap value
5670a94d38fSBill Fenner 	** should not be less than 0x600 (1536) for standard Ethernet.
5680a94d38fSBill Fenner 	** However, we seem to get DL_BADADDR - "DLSAP addr in improper
5690a94d38fSBill Fenner 	** format or invalid" - errors if we use 1537 on the "tr0"
5700a94d38fSBill Fenner 	** device, which, given that its name starts with "tr" and that
5710a94d38fSBill Fenner 	** it's IBM, probably means a Token Ring device.  (Perhaps we
5720a94d38fSBill Fenner 	** need to use 1537 on "/dev/dlpi/en" because that device is for
5730a94d38fSBill Fenner 	** D/I/X Ethernet, the "SAP" is actually an Ethernet type, and
5740a94d38fSBill Fenner 	** it rejects invalid Ethernet types.)
5750a94d38fSBill Fenner 	**
5760a94d38fSBill Fenner 	** So if 1537 fails, we try 2, as Hyung Sik Yoon of IBM Korea
5770a94d38fSBill Fenner 	** says that works on Token Ring (he says that 0 does *not*
5780a94d38fSBill Fenner 	** work; perhaps that's considered an invalid LLC SAP value - I
5790a94d38fSBill Fenner 	** assume the SAP value in a DLPI bind is an LLC SAP for network
5800a94d38fSBill Fenner 	** types that use 802.2 LLC).
581dc2c7305SBill Fenner 	*/
582a8e07101SRui Paulo 	if ((dlbindreq(p->fd, 1537, p->errbuf) < 0 &&
583a8e07101SRui Paulo 	     dlbindreq(p->fd, 2, p->errbuf) < 0) ||
584*681ed54cSXin LI 	     dlbindack(p->fd, (char *)buf, p->errbuf, NULL) < 0) {
585*681ed54cSXin LI 		status = PCAP_ERROR;
5868cf6c252SPaul Traina 		goto bad;
587*681ed54cSXin LI 	}
58804fb2745SSam Leffler #elif defined(DL_HP_RAWDLS)
58904fb2745SSam Leffler 	/*
590ee2dd488SSam Leffler 	** HP-UX 10.0x and 10.1x.
59104fb2745SSam Leffler 	*/
592*681ed54cSXin LI 	if (dl_dohpuxbind(p->fd, p->errbuf) < 0) {
593*681ed54cSXin LI 		status = PCAP_ERROR;
59404fb2745SSam Leffler 		goto bad;
595*681ed54cSXin LI 	}
596*681ed54cSXin LI 	if (pd->send_fd >= 0) {
59704fb2745SSam Leffler 		/*
598ee2dd488SSam Leffler 		** XXX - if this fails, just close send_fd and
599ee2dd488SSam Leffler 		** set it to -1, so that you can't send but can
600ee2dd488SSam Leffler 		** still receive?
60104fb2745SSam Leffler 		*/
602*681ed54cSXin LI 		if (dl_dohpuxbind(pd->send_fd, p->errbuf) < 0) {
603*681ed54cSXin LI 			status = PCAP_ERROR;
60404fb2745SSam Leffler 			goto bad;
60504fb2745SSam Leffler 		}
606*681ed54cSXin LI 	}
60704fb2745SSam Leffler #else /* neither AIX nor HP-UX */
608ee2dd488SSam Leffler 	/*
609ee2dd488SSam Leffler 	** Not Sinix, and neither AIX nor HP-UX - Solaris, and any other
610ee2dd488SSam Leffler 	** OS using DLPI.
611ee2dd488SSam Leffler 	**/
612a8e07101SRui Paulo 	if (dlbindreq(p->fd, 0, p->errbuf) < 0 ||
613*681ed54cSXin LI 	    dlbindack(p->fd, (char *)buf, p->errbuf, NULL) < 0) {
614*681ed54cSXin LI 	    	status = PCAP_ERROR;
61504fb2745SSam Leffler 		goto bad;
616*681ed54cSXin LI 	}
617ee2dd488SSam Leffler #endif /* AIX vs. HP-UX vs. other */
618ee2dd488SSam Leffler #endif /* !HP-UX 9 and !HP-UX 10.20 or later and !SINIX */
6198cf6c252SPaul Traina 
620feb4ecdbSBruce M Simpson #ifdef HAVE_SOLARIS
621feb4ecdbSBruce M Simpson 	if (isatm) {
622feb4ecdbSBruce M Simpson 		/*
623feb4ecdbSBruce M Simpson 		** Have to turn on some special ATM promiscuous mode
624feb4ecdbSBruce M Simpson 		** for SunATM.
625feb4ecdbSBruce M Simpson 		** Do *NOT* turn regular promiscuous mode on; it doesn't
626feb4ecdbSBruce M Simpson 		** help, and may break things.
627feb4ecdbSBruce M Simpson 		*/
628feb4ecdbSBruce M Simpson 		if (strioctl(p->fd, A_PROMISCON_REQ, 0, NULL) < 0) {
629*681ed54cSXin LI 			status = PCAP_ERROR;
630a8e07101SRui Paulo 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
631a8e07101SRui Paulo 			    "A_PROMISCON_REQ: %s", pcap_strerror(errno));
632feb4ecdbSBruce M Simpson 			goto bad;
633feb4ecdbSBruce M Simpson 		}
634feb4ecdbSBruce M Simpson 	} else
635feb4ecdbSBruce M Simpson #endif
636a8e07101SRui Paulo 	if (p->opt.promisc) {
6378cf6c252SPaul Traina 		/*
63804fb2745SSam Leffler 		** Enable promiscuous (not necessary on send FD)
6398cf6c252SPaul Traina 		*/
640*681ed54cSXin LI 		retv = dlpromiscon(p, DL_PROMISC_PHYS);
641*681ed54cSXin LI 		if (retv < 0) {
642*681ed54cSXin LI 			if (retv == PCAP_ERROR_PERM_DENIED)
643d1e87331SXin LI 				status = PCAP_ERROR_PROMISC_PERM_DENIED;
644*681ed54cSXin LI 			else
645*681ed54cSXin LI 				status = retv;
6468cf6c252SPaul Traina 			goto bad;
647d1e87331SXin LI 		}
6488cf6c252SPaul Traina 
6498cf6c252SPaul Traina 		/*
6508cf6c252SPaul Traina 		** Try to enable multicast (you would have thought
651a4b5b39fSBill Fenner 		** promiscuous would be sufficient). (Skip if using
65204fb2745SSam Leffler 		** HP-UX or SINIX) (Not necessary on send FD)
6538cf6c252SPaul Traina 		*/
654a4b5b39fSBill Fenner #if !defined(__hpux) && !defined(sinix)
655*681ed54cSXin LI 		retv = dlpromiscon(p, DL_PROMISC_MULTI);
656*681ed54cSXin LI 		if (retv < 0)
657a8e07101SRui Paulo 			status = PCAP_WARNING;
6588cf6c252SPaul Traina #endif
6598cf6c252SPaul Traina 	}
6608cf6c252SPaul Traina 	/*
661ee2dd488SSam Leffler 	** Try to enable SAP promiscuity (when not in promiscuous mode
662ee2dd488SSam Leffler 	** when using HP-UX, when not doing SunATM on Solaris, and never
66304fb2745SSam Leffler 	** under SINIX) (Not necessary on send FD)
6648cf6c252SPaul Traina 	*/
6658cf6c252SPaul Traina #ifndef sinix
666d1e87331SXin LI #if defined(__hpux)
667d1e87331SXin LI 	/* HP-UX - only do this when not in promiscuous mode */
668d1e87331SXin LI 	if (!p->opt.promisc) {
669d1e87331SXin LI #elif defined(HAVE_SOLARIS)
670d1e87331SXin LI 	/* Solaris - don't do this on SunATM devices */
671d1e87331SXin LI 	if (!isatm) {
672d1e87331SXin LI #else
673d1e87331SXin LI 	/* Everything else (except for SINIX) - always do this */
674d1e87331SXin LI 	{
675a4b5b39fSBill Fenner #endif
676*681ed54cSXin LI 		retv = dlpromiscon(p, DL_PROMISC_SAP);
677*681ed54cSXin LI 		if (retv < 0) {
678*681ed54cSXin LI 			if (p->opt.promisc) {
679d1e87331SXin LI 				/*
680*681ed54cSXin LI 				 * Not fatal, since the DL_PROMISC_PHYS mode
681*681ed54cSXin LI 				 * worked.
682*681ed54cSXin LI 				 *
683d1e87331SXin LI 				 * Report it as a warning, however.
684d1e87331SXin LI 				 */
685a8e07101SRui Paulo 				status = PCAP_WARNING;
686*681ed54cSXin LI 			} else {
687*681ed54cSXin LI 				/*
688*681ed54cSXin LI 				 * Fatal.
689*681ed54cSXin LI 				 */
690*681ed54cSXin LI 				status = retv;
6918cf6c252SPaul Traina 				goto bad;
6928cf6c252SPaul Traina 			}
693d1e87331SXin LI 		}
694*681ed54cSXin LI 	}
695ee2dd488SSam Leffler #endif /* sinix */
6968cf6c252SPaul Traina 
6978cf6c252SPaul Traina 	/*
698ee2dd488SSam Leffler 	** HP-UX 9, and HP-UX 10.20 or later, must bind after setting
699ee2dd488SSam Leffler 	** promiscuous options.
7008cf6c252SPaul Traina 	*/
701ee2dd488SSam Leffler #if defined(HAVE_HPUX9) || defined(HAVE_HPUX10_20_OR_LATER)
702*681ed54cSXin LI 	if (dl_dohpuxbind(p->fd, p->errbuf) < 0) {
703*681ed54cSXin LI 		status = PCAP_ERROR;
7048cf6c252SPaul Traina 		goto bad;
705*681ed54cSXin LI 	}
706ee2dd488SSam Leffler 	/*
707ee2dd488SSam Leffler 	** We don't set promiscuous mode on the send FD, but we'll defer
708ee2dd488SSam Leffler 	** binding it anyway, just to keep the HP-UX 9/10.20 or later
709ee2dd488SSam Leffler 	** code together.
710ee2dd488SSam Leffler 	*/
711*681ed54cSXin LI 	if (pd->send_fd >= 0) {
712ee2dd488SSam Leffler 		/*
713ee2dd488SSam Leffler 		** XXX - if this fails, just close send_fd and
714ee2dd488SSam Leffler 		** set it to -1, so that you can't send but can
715ee2dd488SSam Leffler 		** still receive?
716ee2dd488SSam Leffler 		*/
717*681ed54cSXin LI 		if (dl_dohpuxbind(pd->send_fd, p->errbuf) < 0) {
718*681ed54cSXin LI 			status = PCAP_ERROR;
719ee2dd488SSam Leffler 			goto bad;
720ee2dd488SSam Leffler 		}
721*681ed54cSXin LI 	}
7228cf6c252SPaul Traina #endif
7238cf6c252SPaul Traina 
7248cf6c252SPaul Traina 	/*
7258cf6c252SPaul Traina 	** Determine link type
72604fb2745SSam Leffler 	** XXX - get SAP length and address length as well, for use
72704fb2745SSam Leffler 	** when sending packets.
7288cf6c252SPaul Traina 	*/
729a8e07101SRui Paulo 	if (dlinforeq(p->fd, p->errbuf) < 0 ||
730*681ed54cSXin LI 	    dlinfoack(p->fd, (char *)buf, p->errbuf) < 0) {
731*681ed54cSXin LI 	    	status = PCAP_ERROR;
7328cf6c252SPaul Traina 		goto bad;
733*681ed54cSXin LI 	}
7348cf6c252SPaul Traina 
735a0ee43a1SRui Paulo 	infop = &(MAKE_DL_PRIMITIVES(buf))->info_ack;
736*681ed54cSXin LI 	if (pcap_process_mactype(p, infop->dl_mac_type) != 0) {
737*681ed54cSXin LI 		status = PCAP_ERROR;
7388cf6c252SPaul Traina 		goto bad;
739*681ed54cSXin LI 	}
7408cf6c252SPaul Traina 
7418cf6c252SPaul Traina #ifdef	DLIOCRAW
7428cf6c252SPaul Traina 	/*
743feb4ecdbSBruce M Simpson 	** This is a non standard SunOS hack to get the full raw link-layer
744feb4ecdbSBruce M Simpson 	** header.
7458cf6c252SPaul Traina 	*/
7468cf6c252SPaul Traina 	if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) {
747*681ed54cSXin LI 		status = PCAP_ERROR;
748a8e07101SRui Paulo 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "DLIOCRAW: %s",
749dc2c7305SBill Fenner 		    pcap_strerror(errno));
7508cf6c252SPaul Traina 		goto bad;
7518cf6c252SPaul Traina 	}
7528cf6c252SPaul Traina #endif
7538cf6c252SPaul Traina 
754a0ee43a1SRui Paulo #ifdef HAVE_SYS_BUFMOD_H
755a8e07101SRui Paulo 	ss = p->snapshot;
7568cf6c252SPaul Traina 
7578cf6c252SPaul Traina 	/*
7588cf6c252SPaul Traina 	** There is a bug in bufmod(7). When dealing with messages of
7598cf6c252SPaul Traina 	** less than snaplen size it strips data from the beginning not
7608cf6c252SPaul Traina 	** the end.
7618cf6c252SPaul Traina 	**
762a8e07101SRui Paulo 	** This bug is fixed in 5.3.2. Also, there is a patch available.
763a8e07101SRui Paulo 	** Ask for bugid 1149065.
7648cf6c252SPaul Traina 	*/
7658cf6c252SPaul Traina #ifdef HAVE_SOLARIS
7668cf6c252SPaul Traina 	release = get_release(&osmajor, &osminor, &osmicro);
7678cf6c252SPaul Traina 	if (osmajor == 5 && (osminor <= 2 || (osminor == 3 && osmicro < 2)) &&
7688cf6c252SPaul Traina 	    getenv("BUFMOD_FIXED") == NULL) {
769a8e07101SRui Paulo 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
770a8e07101SRui Paulo 		"WARNING: bufmod is broken in SunOS %s; ignoring snaplen.",
7718cf6c252SPaul Traina 		    release);
7728cf6c252SPaul Traina 		ss = 0;
773a8e07101SRui Paulo 		status = PCAP_WARNING;
7748cf6c252SPaul Traina 	}
7758cf6c252SPaul Traina #endif
7768cf6c252SPaul Traina 
777a8e07101SRui Paulo 	/* Push and configure bufmod. */
778*681ed54cSXin LI 	if (pcap_conf_bufmod(p, ss) != 0) {
779*681ed54cSXin LI 		status = PCAP_ERROR;
7808cf6c252SPaul Traina 		goto bad;
781*681ed54cSXin LI 	}
7828cf6c252SPaul Traina #endif
7838cf6c252SPaul Traina 
7848cf6c252SPaul Traina 	/*
7858cf6c252SPaul Traina 	** As the last operation flush the read side.
7868cf6c252SPaul Traina 	*/
7878cf6c252SPaul Traina 	if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
788*681ed54cSXin LI 		status = PCAP_ERROR;
789a8e07101SRui Paulo 		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "FLUSHR: %s",
790dc2c7305SBill Fenner 		    pcap_strerror(errno));
7918cf6c252SPaul Traina 		goto bad;
7928cf6c252SPaul Traina 	}
793feb4ecdbSBruce M Simpson 
794a8e07101SRui Paulo 	/* Allocate data buffer. */
795*681ed54cSXin LI 	if (pcap_alloc_databuf(p) != 0) {
796*681ed54cSXin LI 		status = PCAP_ERROR;
797feb4ecdbSBruce M Simpson 		goto bad;
798*681ed54cSXin LI 	}
799feb4ecdbSBruce M Simpson 
800feb4ecdbSBruce M Simpson 	/*
801*681ed54cSXin LI 	 * Success.
802*681ed54cSXin LI 	 *
803feb4ecdbSBruce M Simpson 	 * "p->fd" is an FD for a STREAMS device, so "select()" and
804feb4ecdbSBruce M Simpson 	 * "poll()" should work on it.
805feb4ecdbSBruce M Simpson 	 */
806feb4ecdbSBruce M Simpson 	p->selectable_fd = p->fd;
807feb4ecdbSBruce M Simpson 
808feb4ecdbSBruce M Simpson 	p->read_op = pcap_read_dlpi;
80904fb2745SSam Leffler 	p->inject_op = pcap_inject_dlpi;
810feb4ecdbSBruce M Simpson 	p->setfilter_op = install_bpf_program;	/* no kernel filtering */
811ee2dd488SSam Leffler 	p->setdirection_op = NULL;	/* Not implemented.*/
812feb4ecdbSBruce M Simpson 	p->set_datalink_op = NULL;	/* can't change data link type */
813feb4ecdbSBruce M Simpson 	p->getnonblock_op = pcap_getnonblock_fd;
814feb4ecdbSBruce M Simpson 	p->setnonblock_op = pcap_setnonblock_fd;
815feb4ecdbSBruce M Simpson 	p->stats_op = pcap_stats_dlpi;
816a8e07101SRui Paulo 	p->cleanup_op = pcap_cleanup_dlpi;
8178cf6c252SPaul Traina 
818a8e07101SRui Paulo 	return (status);
8198cf6c252SPaul Traina bad:
820a8e07101SRui Paulo 	pcap_cleanup_dlpi(p);
821a8e07101SRui Paulo 	return (status);
8228cf6c252SPaul Traina }
8238cf6c252SPaul Traina 
8240a94d38fSBill Fenner /*
8250a94d38fSBill Fenner  * Split a device name into a device type name and a unit number;
8260a94d38fSBill Fenner  * return the a pointer to the beginning of the unit number, which
8270a94d38fSBill Fenner  * is the end of the device type name, and set "*unitp" to the unit
8280a94d38fSBill Fenner  * number.
8290a94d38fSBill Fenner  *
8300a94d38fSBill Fenner  * Returns NULL on error, and fills "ebuf" with an error message.
8310a94d38fSBill Fenner  */
8320a94d38fSBill Fenner static char *
8330a94d38fSBill Fenner split_dname(char *device, int *unitp, char *ebuf)
8340a94d38fSBill Fenner {
8350a94d38fSBill Fenner 	char *cp;
8360a94d38fSBill Fenner 	char *eos;
83704fb2745SSam Leffler 	long unit;
8380a94d38fSBill Fenner 
8390a94d38fSBill Fenner 	/*
8400a94d38fSBill Fenner 	 * Look for a number at the end of the device name string.
8410a94d38fSBill Fenner 	 */
8420a94d38fSBill Fenner 	cp = device + strlen(device) - 1;
8430a94d38fSBill Fenner 	if (*cp < '0' || *cp > '9') {
8440a94d38fSBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s missing unit number",
8450a94d38fSBill Fenner 		    device);
8460a94d38fSBill Fenner 		return (NULL);
8470a94d38fSBill Fenner 	}
8480a94d38fSBill Fenner 
8490a94d38fSBill Fenner 	/* Digits at end of string are unit number */
8500a94d38fSBill Fenner 	while (cp-1 >= device && *(cp-1) >= '0' && *(cp-1) <= '9')
8510a94d38fSBill Fenner 		cp--;
8520a94d38fSBill Fenner 
85304fb2745SSam Leffler 	errno = 0;
8540a94d38fSBill Fenner 	unit = strtol(cp, &eos, 10);
8550a94d38fSBill Fenner 	if (*eos != '\0') {
8560a94d38fSBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s bad unit number", device);
8570a94d38fSBill Fenner 		return (NULL);
8580a94d38fSBill Fenner 	}
85904fb2745SSam Leffler 	if (errno == ERANGE || unit > INT_MAX) {
86004fb2745SSam Leffler 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number too large",
86104fb2745SSam Leffler 		    device);
86204fb2745SSam Leffler 		return (NULL);
86304fb2745SSam Leffler 	}
86404fb2745SSam Leffler 	if (unit < 0) {
86504fb2745SSam Leffler 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "%s unit number is negative",
86604fb2745SSam Leffler 		    device);
86704fb2745SSam Leffler 		return (NULL);
86804fb2745SSam Leffler 	}
86904fb2745SSam Leffler 	*unitp = (int)unit;
8700a94d38fSBill Fenner 	return (cp);
8710a94d38fSBill Fenner }
8720a94d38fSBill Fenner 
87304fb2745SSam Leffler static int
87404fb2745SSam Leffler dl_doattach(int fd, int ppa, char *ebuf)
87504fb2745SSam Leffler {
876d1e87331SXin LI 	dl_attach_req_t	req;
87704fb2745SSam Leffler 	bpf_u_int32 buf[MAXDLBUF];
878a8e07101SRui Paulo 	int err;
87904fb2745SSam Leffler 
880d1e87331SXin LI 	req.dl_primitive = DL_ATTACH_REQ;
881d1e87331SXin LI 	req.dl_ppa = ppa;
882d1e87331SXin LI 	if (send_request(fd, (char *)&req, sizeof(req), "attach", ebuf) < 0)
883a8e07101SRui Paulo 		return (PCAP_ERROR);
884d1e87331SXin LI 
885a8e07101SRui Paulo 	err = dlokack(fd, "attach", (char *)buf, ebuf);
886a8e07101SRui Paulo 	if (err < 0)
887a8e07101SRui Paulo 		return (err);
88804fb2745SSam Leffler 	return (0);
88904fb2745SSam Leffler }
89004fb2745SSam Leffler 
891ee2dd488SSam Leffler #ifdef DL_HP_RAWDLS
892ee2dd488SSam Leffler static int
893ee2dd488SSam Leffler dl_dohpuxbind(int fd, char *ebuf)
894ee2dd488SSam Leffler {
895ee2dd488SSam Leffler 	int hpsap;
896ee2dd488SSam Leffler 	int uerror;
897ee2dd488SSam Leffler 	bpf_u_int32 buf[MAXDLBUF];
898ee2dd488SSam Leffler 
899ee2dd488SSam Leffler 	/*
900ee2dd488SSam Leffler 	 * XXX - we start at 22 because we used to use only 22, but
901ee2dd488SSam Leffler 	 * that was just because that was the value used in some
902ee2dd488SSam Leffler 	 * sample code from HP.  With what value *should* we start?
903ee2dd488SSam Leffler 	 * Does it matter, given that we're enabling SAP promiscuity
904ee2dd488SSam Leffler 	 * on the input FD?
905ee2dd488SSam Leffler 	 */
906ee2dd488SSam Leffler 	hpsap = 22;
907ee2dd488SSam Leffler 	for (;;) {
908ee2dd488SSam Leffler 		if (dlbindreq(fd, hpsap, ebuf) < 0)
909ee2dd488SSam Leffler 			return (-1);
910ee2dd488SSam Leffler 		if (dlbindack(fd, (char *)buf, ebuf, &uerror) >= 0)
911ee2dd488SSam Leffler 			break;
912ee2dd488SSam Leffler 		/*
913ee2dd488SSam Leffler 		 * For any error other than a UNIX EBUSY, give up.
914ee2dd488SSam Leffler 		 */
9155d18909fSSam Leffler 		if (uerror != EBUSY) {
9165d18909fSSam Leffler 			/*
9175d18909fSSam Leffler 			 * dlbindack() has already filled in ebuf for
9185d18909fSSam Leffler 			 * this error.
9195d18909fSSam Leffler 			 */
920ee2dd488SSam Leffler 			return (-1);
9215d18909fSSam Leffler 		}
922ee2dd488SSam Leffler 
923ee2dd488SSam Leffler 		/*
924ee2dd488SSam Leffler 		 * For EBUSY, try the next SAP value; that means that
925ee2dd488SSam Leffler 		 * somebody else is using that SAP.  Clear ebuf so
926ee2dd488SSam Leffler 		 * that application doesn't report the "Device busy"
927ee2dd488SSam Leffler 		 * error as a warning.
928ee2dd488SSam Leffler 		 */
929ee2dd488SSam Leffler 		*ebuf = '\0';
930ee2dd488SSam Leffler 		hpsap++;
9315d18909fSSam Leffler 		if (hpsap > 100) {
9325d18909fSSam Leffler 			strlcpy(ebuf,
9335d18909fSSam Leffler 			    "All SAPs from 22 through 100 are in use",
9345d18909fSSam Leffler 			    PCAP_ERRBUF_SIZE);
935ee2dd488SSam Leffler 			return (-1);
936ee2dd488SSam Leffler 		}
937ee2dd488SSam Leffler 	}
9385d18909fSSam Leffler 	return (0);
9395d18909fSSam Leffler }
940ee2dd488SSam Leffler #endif
941ee2dd488SSam Leffler 
942d1e87331SXin LI #define STRINGIFY(n)	#n
943d1e87331SXin LI 
944d1e87331SXin LI static int
945d1e87331SXin LI dlpromiscon(pcap_t *p, bpf_u_int32 level)
946d1e87331SXin LI {
947d1e87331SXin LI 	dl_promiscon_req_t req;
948d1e87331SXin LI 	bpf_u_int32 buf[MAXDLBUF];
949d1e87331SXin LI 	int err;
950d1e87331SXin LI 
951d1e87331SXin LI 	req.dl_primitive = DL_PROMISCON_REQ;
952d1e87331SXin LI 	req.dl_level = level;
953d1e87331SXin LI 	if (send_request(p->fd, (char *)&req, sizeof(req), "promiscon",
954d1e87331SXin LI 	    p->errbuf) < 0)
955d1e87331SXin LI 		return (PCAP_ERROR);
956d1e87331SXin LI 	err = dlokack(p->fd, "promiscon" STRINGIFY(level), (char *)buf,
957d1e87331SXin LI 	    p->errbuf);
958d1e87331SXin LI 	if (err < 0)
959d1e87331SXin LI 		return (err);
960d1e87331SXin LI 	return (0);
961d1e87331SXin LI }
962d1e87331SXin LI 
9638cf6c252SPaul Traina int
964feb4ecdbSBruce M Simpson pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
9658cf6c252SPaul Traina {
966feb4ecdbSBruce M Simpson #ifdef HAVE_SOLARIS
967feb4ecdbSBruce M Simpson 	int fd;
968feb4ecdbSBruce M Simpson 	union {
969feb4ecdbSBruce M Simpson 		u_int nunits;
970feb4ecdbSBruce M Simpson 		char pad[516];	/* XXX - must be at least 513; is 516
971feb4ecdbSBruce M Simpson 				   in "atmgetunits" */
972feb4ecdbSBruce M Simpson 	} buf;
973feb4ecdbSBruce M Simpson 	char baname[2+1+1];
974feb4ecdbSBruce M Simpson 	u_int i;
9758cf6c252SPaul Traina 
976feb4ecdbSBruce M Simpson 	/*
977feb4ecdbSBruce M Simpson 	 * We may have to do special magic to get ATM devices.
978feb4ecdbSBruce M Simpson 	 */
979feb4ecdbSBruce M Simpson 	if ((fd = open("/dev/ba", O_RDWR)) < 0) {
980feb4ecdbSBruce M Simpson 		/*
981feb4ecdbSBruce M Simpson 		 * We couldn't open the "ba" device.
982feb4ecdbSBruce M Simpson 		 * For now, just give up; perhaps we should
983feb4ecdbSBruce M Simpson 		 * return an error if the problem is neither
984feb4ecdbSBruce M Simpson 		 * a "that device doesn't exist" error (ENOENT,
985feb4ecdbSBruce M Simpson 		 * ENXIO, etc.) or a "you're not allowed to do
986feb4ecdbSBruce M Simpson 		 * that" error (EPERM, EACCES).
987feb4ecdbSBruce M Simpson 		 */
988feb4ecdbSBruce M Simpson 		return (0);
989feb4ecdbSBruce M Simpson 	}
990feb4ecdbSBruce M Simpson 
991feb4ecdbSBruce M Simpson 	if (strioctl(fd, A_GET_UNITS, sizeof(buf), (char *)&buf) < 0) {
992feb4ecdbSBruce M Simpson 		snprintf(errbuf, PCAP_ERRBUF_SIZE, "A_GET_UNITS: %s",
993feb4ecdbSBruce M Simpson 		    pcap_strerror(errno));
994dc2c7305SBill Fenner 		return (-1);
995feb4ecdbSBruce M Simpson 	}
996feb4ecdbSBruce M Simpson 	for (i = 0; i < buf.nunits; i++) {
997feb4ecdbSBruce M Simpson 		snprintf(baname, sizeof baname, "ba%u", i);
998feb4ecdbSBruce M Simpson 		if (pcap_add_if(alldevsp, baname, 0, NULL, errbuf) < 0)
999feb4ecdbSBruce M Simpson 			return (-1);
1000feb4ecdbSBruce M Simpson 	}
1001feb4ecdbSBruce M Simpson #endif
1002feb4ecdbSBruce M Simpson 
10038cf6c252SPaul Traina 	return (0);
10048cf6c252SPaul Traina }
10058cf6c252SPaul Traina 
10068cf6c252SPaul Traina static int
10078cf6c252SPaul Traina send_request(int fd, char *ptr, int len, char *what, char *ebuf)
10088cf6c252SPaul Traina {
10098cf6c252SPaul Traina 	struct	strbuf	ctl;
10108cf6c252SPaul Traina 	int	flags;
10118cf6c252SPaul Traina 
10128cf6c252SPaul Traina 	ctl.maxlen = 0;
10138cf6c252SPaul Traina 	ctl.len = len;
10148cf6c252SPaul Traina 	ctl.buf = ptr;
10158cf6c252SPaul Traina 
10168cf6c252SPaul Traina 	flags = 0;
10178cf6c252SPaul Traina 	if (putmsg(fd, &ctl, (struct strbuf *) NULL, flags) < 0) {
1018dc2c7305SBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
1019dc2c7305SBill Fenner 		    "send_request: putmsg \"%s\": %s",
10208cf6c252SPaul Traina 		    what, pcap_strerror(errno));
10218cf6c252SPaul Traina 		return (-1);
10228cf6c252SPaul Traina 	}
10238cf6c252SPaul Traina 	return (0);
10248cf6c252SPaul Traina }
10258cf6c252SPaul Traina 
10268cf6c252SPaul Traina static int
1027ee2dd488SSam Leffler recv_ack(int fd, int size, const char *what, char *bufp, char *ebuf, int *uerror)
10288cf6c252SPaul Traina {
10298cf6c252SPaul Traina 	union	DL_primitives	*dlp;
10308cf6c252SPaul Traina 	struct	strbuf	ctl;
10318cf6c252SPaul Traina 	int	flags;
10328cf6c252SPaul Traina 
10335d18909fSSam Leffler 	/*
10345d18909fSSam Leffler 	 * Clear out "*uerror", so it's only set for DL_ERROR_ACK/DL_SYSERR,
10355d18909fSSam Leffler 	 * making that the only place where EBUSY is treated specially.
10365d18909fSSam Leffler 	 */
10375d18909fSSam Leffler 	if (uerror != NULL)
10385d18909fSSam Leffler 		*uerror = 0;
10395d18909fSSam Leffler 
10408cf6c252SPaul Traina 	ctl.maxlen = MAXDLBUF;
10418cf6c252SPaul Traina 	ctl.len = 0;
10428cf6c252SPaul Traina 	ctl.buf = bufp;
10438cf6c252SPaul Traina 
10448cf6c252SPaul Traina 	flags = 0;
10458cf6c252SPaul Traina 	if (getmsg(fd, &ctl, (struct strbuf*)NULL, &flags) < 0) {
1046dc2c7305SBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "recv_ack: %s getmsg: %s",
10478cf6c252SPaul Traina 		    what, pcap_strerror(errno));
1048a8e07101SRui Paulo 		return (PCAP_ERROR);
10498cf6c252SPaul Traina 	}
10508cf6c252SPaul Traina 
1051a0ee43a1SRui Paulo 	dlp = MAKE_DL_PRIMITIVES(ctl.buf);
10528cf6c252SPaul Traina 	switch (dlp->dl_primitive) {
10538cf6c252SPaul Traina 
10548cf6c252SPaul Traina 	case DL_INFO_ACK:
10558cf6c252SPaul Traina 	case DL_BIND_ACK:
10568cf6c252SPaul Traina 	case DL_OK_ACK:
10578cf6c252SPaul Traina #ifdef DL_HP_PPA_ACK
10588cf6c252SPaul Traina 	case DL_HP_PPA_ACK:
10598cf6c252SPaul Traina #endif
10608cf6c252SPaul Traina 		/* These are OK */
10618cf6c252SPaul Traina 		break;
10628cf6c252SPaul Traina 
10638cf6c252SPaul Traina 	case DL_ERROR_ACK:
10648cf6c252SPaul Traina 		switch (dlp->error_ack.dl_errno) {
10658cf6c252SPaul Traina 
10668cf6c252SPaul Traina 		case DL_SYSERR:
1067ee2dd488SSam Leffler 			if (uerror != NULL)
1068ee2dd488SSam Leffler 				*uerror = dlp->error_ack.dl_unix_errno;
10690a94d38fSBill Fenner 			snprintf(ebuf, PCAP_ERRBUF_SIZE,
10700a94d38fSBill Fenner 			    "recv_ack: %s: UNIX error - %s",
10718cf6c252SPaul Traina 			    what, pcap_strerror(dlp->error_ack.dl_unix_errno));
1072d1e87331SXin LI 			if (dlp->error_ack.dl_unix_errno == EPERM ||
1073d1e87331SXin LI 			    dlp->error_ack.dl_unix_errno == EACCES)
1074a8e07101SRui Paulo 				return (PCAP_ERROR_PERM_DENIED);
10758cf6c252SPaul Traina 			break;
10768cf6c252SPaul Traina 
10778cf6c252SPaul Traina 		default:
10780a94d38fSBill Fenner 			snprintf(ebuf, PCAP_ERRBUF_SIZE, "recv_ack: %s: %s",
10790a94d38fSBill Fenner 			    what, dlstrerror(dlp->error_ack.dl_errno));
1080a8e07101SRui Paulo 			if (dlp->error_ack.dl_errno == DL_BADPPA)
1081a8e07101SRui Paulo 				return (PCAP_ERROR_NO_SUCH_DEVICE);
1082a8e07101SRui Paulo 			else if (dlp->error_ack.dl_errno == DL_ACCESS)
1083a8e07101SRui Paulo 				return (PCAP_ERROR_PERM_DENIED);
10848cf6c252SPaul Traina 			break;
10858cf6c252SPaul Traina 		}
1086a8e07101SRui Paulo 		return (PCAP_ERROR);
10878cf6c252SPaul Traina 
10888cf6c252SPaul Traina 	default:
1089dc2c7305SBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
10900a94d38fSBill Fenner 		    "recv_ack: %s: Unexpected primitive ack %s",
10910a94d38fSBill Fenner 		    what, dlprim(dlp->dl_primitive));
1092a8e07101SRui Paulo 		return (PCAP_ERROR);
10938cf6c252SPaul Traina 	}
10948cf6c252SPaul Traina 
10958cf6c252SPaul Traina 	if (ctl.len < size) {
1096dc2c7305SBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
10970a94d38fSBill Fenner 		    "recv_ack: %s: Ack too small (%d < %d)",
10988cf6c252SPaul Traina 		    what, ctl.len, size);
1099a8e07101SRui Paulo 		return (PCAP_ERROR);
11008cf6c252SPaul Traina 	}
11018cf6c252SPaul Traina 	return (ctl.len);
11028cf6c252SPaul Traina }
11038cf6c252SPaul Traina 
11040a94d38fSBill Fenner static char *
11050a94d38fSBill Fenner dlstrerror(bpf_u_int32 dl_errno)
11060a94d38fSBill Fenner {
11070a94d38fSBill Fenner 	static char errstring[6+2+8+1];
11080a94d38fSBill Fenner 
11090a94d38fSBill Fenner 	switch (dl_errno) {
11100a94d38fSBill Fenner 
11110a94d38fSBill Fenner 	case DL_ACCESS:
11120a94d38fSBill Fenner 		return ("Improper permissions for request");
11130a94d38fSBill Fenner 
11140a94d38fSBill Fenner 	case DL_BADADDR:
11150a94d38fSBill Fenner 		return ("DLSAP addr in improper format or invalid");
11160a94d38fSBill Fenner 
11170a94d38fSBill Fenner 	case DL_BADCORR:
11180a94d38fSBill Fenner 		return ("Seq number not from outstand DL_CONN_IND");
11190a94d38fSBill Fenner 
11200a94d38fSBill Fenner 	case DL_BADDATA:
11210a94d38fSBill Fenner 		return ("User data exceeded provider limit");
11220a94d38fSBill Fenner 
11230a94d38fSBill Fenner 	case DL_BADPPA:
11240a94d38fSBill Fenner #ifdef HAVE_DEV_DLPI
11250a94d38fSBill Fenner 		/*
11260a94d38fSBill Fenner 		 * With a single "/dev/dlpi" device used for all
11270a94d38fSBill Fenner 		 * DLPI providers, PPAs have nothing to do with
11280a94d38fSBill Fenner 		 * unit numbers.
11290a94d38fSBill Fenner 		 */
11300a94d38fSBill Fenner 		return ("Specified PPA was invalid");
11310a94d38fSBill Fenner #else
11320a94d38fSBill Fenner 		/*
11330a94d38fSBill Fenner 		 * We have separate devices for separate devices;
11340a94d38fSBill Fenner 		 * the PPA is just the unit number.
11350a94d38fSBill Fenner 		 */
11360a94d38fSBill Fenner 		return ("Specified PPA (device unit) was invalid");
11370a94d38fSBill Fenner #endif
11380a94d38fSBill Fenner 
11390a94d38fSBill Fenner 	case DL_BADPRIM:
11400a94d38fSBill Fenner 		return ("Primitive received not known by provider");
11410a94d38fSBill Fenner 
11420a94d38fSBill Fenner 	case DL_BADQOSPARAM:
11430a94d38fSBill Fenner 		return ("QOS parameters contained invalid values");
11440a94d38fSBill Fenner 
11450a94d38fSBill Fenner 	case DL_BADQOSTYPE:
11460a94d38fSBill Fenner 		return ("QOS structure type is unknown/unsupported");
11470a94d38fSBill Fenner 
11480a94d38fSBill Fenner 	case DL_BADSAP:
11490a94d38fSBill Fenner 		return ("Bad LSAP selector");
11500a94d38fSBill Fenner 
11510a94d38fSBill Fenner 	case DL_BADTOKEN:
11520a94d38fSBill Fenner 		return ("Token used not an active stream");
11530a94d38fSBill Fenner 
11540a94d38fSBill Fenner 	case DL_BOUND:
11550a94d38fSBill Fenner 		return ("Attempted second bind with dl_max_conind");
11560a94d38fSBill Fenner 
11570a94d38fSBill Fenner 	case DL_INITFAILED:
11580a94d38fSBill Fenner 		return ("Physical link initialization failed");
11590a94d38fSBill Fenner 
11600a94d38fSBill Fenner 	case DL_NOADDR:
11610a94d38fSBill Fenner 		return ("Provider couldn't allocate alternate address");
11620a94d38fSBill Fenner 
11630a94d38fSBill Fenner 	case DL_NOTINIT:
11640a94d38fSBill Fenner 		return ("Physical link not initialized");
11650a94d38fSBill Fenner 
11660a94d38fSBill Fenner 	case DL_OUTSTATE:
11670a94d38fSBill Fenner 		return ("Primitive issued in improper state");
11680a94d38fSBill Fenner 
11690a94d38fSBill Fenner 	case DL_SYSERR:
11700a94d38fSBill Fenner 		return ("UNIX system error occurred");
11710a94d38fSBill Fenner 
11720a94d38fSBill Fenner 	case DL_UNSUPPORTED:
11730a94d38fSBill Fenner 		return ("Requested service not supplied by provider");
11740a94d38fSBill Fenner 
11750a94d38fSBill Fenner 	case DL_UNDELIVERABLE:
11760a94d38fSBill Fenner 		return ("Previous data unit could not be delivered");
11770a94d38fSBill Fenner 
11780a94d38fSBill Fenner 	case DL_NOTSUPPORTED:
11790a94d38fSBill Fenner 		return ("Primitive is known but not supported");
11800a94d38fSBill Fenner 
11810a94d38fSBill Fenner 	case DL_TOOMANY:
11820a94d38fSBill Fenner 		return ("Limit exceeded");
11830a94d38fSBill Fenner 
11840a94d38fSBill Fenner 	case DL_NOTENAB:
11850a94d38fSBill Fenner 		return ("Promiscuous mode not enabled");
11860a94d38fSBill Fenner 
11870a94d38fSBill Fenner 	case DL_BUSY:
11880a94d38fSBill Fenner 		return ("Other streams for PPA in post-attached");
11890a94d38fSBill Fenner 
11900a94d38fSBill Fenner 	case DL_NOAUTO:
11910a94d38fSBill Fenner 		return ("Automatic handling XID&TEST not supported");
11920a94d38fSBill Fenner 
11930a94d38fSBill Fenner 	case DL_NOXIDAUTO:
11940a94d38fSBill Fenner 		return ("Automatic handling of XID not supported");
11950a94d38fSBill Fenner 
11960a94d38fSBill Fenner 	case DL_NOTESTAUTO:
11970a94d38fSBill Fenner 		return ("Automatic handling of TEST not supported");
11980a94d38fSBill Fenner 
11990a94d38fSBill Fenner 	case DL_XIDAUTO:
12000a94d38fSBill Fenner 		return ("Automatic handling of XID response");
12010a94d38fSBill Fenner 
12020a94d38fSBill Fenner 	case DL_TESTAUTO:
12030a94d38fSBill Fenner 		return ("Automatic handling of TEST response");
12040a94d38fSBill Fenner 
12050a94d38fSBill Fenner 	case DL_PENDING:
12060a94d38fSBill Fenner 		return ("Pending outstanding connect indications");
12070a94d38fSBill Fenner 
12080a94d38fSBill Fenner 	default:
12090a94d38fSBill Fenner 		sprintf(errstring, "Error %02x", dl_errno);
12100a94d38fSBill Fenner 		return (errstring);
12110a94d38fSBill Fenner 	}
12120a94d38fSBill Fenner }
12130a94d38fSBill Fenner 
12140a94d38fSBill Fenner static char *
12150a94d38fSBill Fenner dlprim(bpf_u_int32 prim)
12160a94d38fSBill Fenner {
12170a94d38fSBill Fenner 	static char primbuf[80];
12180a94d38fSBill Fenner 
12190a94d38fSBill Fenner 	switch (prim) {
12200a94d38fSBill Fenner 
12210a94d38fSBill Fenner 	case DL_INFO_REQ:
12220a94d38fSBill Fenner 		return ("DL_INFO_REQ");
12230a94d38fSBill Fenner 
12240a94d38fSBill Fenner 	case DL_INFO_ACK:
12250a94d38fSBill Fenner 		return ("DL_INFO_ACK");
12260a94d38fSBill Fenner 
12270a94d38fSBill Fenner 	case DL_ATTACH_REQ:
12280a94d38fSBill Fenner 		return ("DL_ATTACH_REQ");
12290a94d38fSBill Fenner 
12300a94d38fSBill Fenner 	case DL_DETACH_REQ:
12310a94d38fSBill Fenner 		return ("DL_DETACH_REQ");
12320a94d38fSBill Fenner 
12330a94d38fSBill Fenner 	case DL_BIND_REQ:
12340a94d38fSBill Fenner 		return ("DL_BIND_REQ");
12350a94d38fSBill Fenner 
12360a94d38fSBill Fenner 	case DL_BIND_ACK:
12370a94d38fSBill Fenner 		return ("DL_BIND_ACK");
12380a94d38fSBill Fenner 
12390a94d38fSBill Fenner 	case DL_UNBIND_REQ:
12400a94d38fSBill Fenner 		return ("DL_UNBIND_REQ");
12410a94d38fSBill Fenner 
12420a94d38fSBill Fenner 	case DL_OK_ACK:
12430a94d38fSBill Fenner 		return ("DL_OK_ACK");
12440a94d38fSBill Fenner 
12450a94d38fSBill Fenner 	case DL_ERROR_ACK:
12460a94d38fSBill Fenner 		return ("DL_ERROR_ACK");
12470a94d38fSBill Fenner 
12480a94d38fSBill Fenner 	case DL_SUBS_BIND_REQ:
12490a94d38fSBill Fenner 		return ("DL_SUBS_BIND_REQ");
12500a94d38fSBill Fenner 
12510a94d38fSBill Fenner 	case DL_SUBS_BIND_ACK:
12520a94d38fSBill Fenner 		return ("DL_SUBS_BIND_ACK");
12530a94d38fSBill Fenner 
12540a94d38fSBill Fenner 	case DL_UNITDATA_REQ:
12550a94d38fSBill Fenner 		return ("DL_UNITDATA_REQ");
12560a94d38fSBill Fenner 
12570a94d38fSBill Fenner 	case DL_UNITDATA_IND:
12580a94d38fSBill Fenner 		return ("DL_UNITDATA_IND");
12590a94d38fSBill Fenner 
12600a94d38fSBill Fenner 	case DL_UDERROR_IND:
12610a94d38fSBill Fenner 		return ("DL_UDERROR_IND");
12620a94d38fSBill Fenner 
12630a94d38fSBill Fenner 	case DL_UDQOS_REQ:
12640a94d38fSBill Fenner 		return ("DL_UDQOS_REQ");
12650a94d38fSBill Fenner 
12660a94d38fSBill Fenner 	case DL_CONNECT_REQ:
12670a94d38fSBill Fenner 		return ("DL_CONNECT_REQ");
12680a94d38fSBill Fenner 
12690a94d38fSBill Fenner 	case DL_CONNECT_IND:
12700a94d38fSBill Fenner 		return ("DL_CONNECT_IND");
12710a94d38fSBill Fenner 
12720a94d38fSBill Fenner 	case DL_CONNECT_RES:
12730a94d38fSBill Fenner 		return ("DL_CONNECT_RES");
12740a94d38fSBill Fenner 
12750a94d38fSBill Fenner 	case DL_CONNECT_CON:
12760a94d38fSBill Fenner 		return ("DL_CONNECT_CON");
12770a94d38fSBill Fenner 
12780a94d38fSBill Fenner 	case DL_TOKEN_REQ:
12790a94d38fSBill Fenner 		return ("DL_TOKEN_REQ");
12800a94d38fSBill Fenner 
12810a94d38fSBill Fenner 	case DL_TOKEN_ACK:
12820a94d38fSBill Fenner 		return ("DL_TOKEN_ACK");
12830a94d38fSBill Fenner 
12840a94d38fSBill Fenner 	case DL_DISCONNECT_REQ:
12850a94d38fSBill Fenner 		return ("DL_DISCONNECT_REQ");
12860a94d38fSBill Fenner 
12870a94d38fSBill Fenner 	case DL_DISCONNECT_IND:
12880a94d38fSBill Fenner 		return ("DL_DISCONNECT_IND");
12890a94d38fSBill Fenner 
12900a94d38fSBill Fenner 	case DL_RESET_REQ:
12910a94d38fSBill Fenner 		return ("DL_RESET_REQ");
12920a94d38fSBill Fenner 
12930a94d38fSBill Fenner 	case DL_RESET_IND:
12940a94d38fSBill Fenner 		return ("DL_RESET_IND");
12950a94d38fSBill Fenner 
12960a94d38fSBill Fenner 	case DL_RESET_RES:
12970a94d38fSBill Fenner 		return ("DL_RESET_RES");
12980a94d38fSBill Fenner 
12990a94d38fSBill Fenner 	case DL_RESET_CON:
13000a94d38fSBill Fenner 		return ("DL_RESET_CON");
13010a94d38fSBill Fenner 
13020a94d38fSBill Fenner 	default:
13030a94d38fSBill Fenner 		(void) sprintf(primbuf, "unknown primitive 0x%x", prim);
13040a94d38fSBill Fenner 		return (primbuf);
13050a94d38fSBill Fenner 	}
13060a94d38fSBill Fenner }
13070a94d38fSBill Fenner 
13088cf6c252SPaul Traina static int
13098cf6c252SPaul Traina dlbindreq(int fd, bpf_u_int32 sap, char *ebuf)
13108cf6c252SPaul Traina {
13118cf6c252SPaul Traina 
13128cf6c252SPaul Traina 	dl_bind_req_t	req;
13138cf6c252SPaul Traina 
13148cf6c252SPaul Traina 	memset((char *)&req, 0, sizeof(req));
13158cf6c252SPaul Traina 	req.dl_primitive = DL_BIND_REQ;
1316ee2dd488SSam Leffler 	/* XXX - what if neither of these are defined? */
1317ee2dd488SSam Leffler #if defined(DL_HP_RAWDLS)
13188cf6c252SPaul Traina 	req.dl_max_conind = 1;			/* XXX magic number */
13198cf6c252SPaul Traina 	req.dl_service_mode = DL_HP_RAWDLS;
1320ee2dd488SSam Leffler #elif defined(DL_CLDLS)
13213052b236SBill Fenner 	req.dl_service_mode = DL_CLDLS;
13223052b236SBill Fenner #endif
1323ee2dd488SSam Leffler 	req.dl_sap = sap;
13248cf6c252SPaul Traina 
13258cf6c252SPaul Traina 	return (send_request(fd, (char *)&req, sizeof(req), "bind", ebuf));
13268cf6c252SPaul Traina }
13278cf6c252SPaul Traina 
13288cf6c252SPaul Traina static int
1329ee2dd488SSam Leffler dlbindack(int fd, char *bufp, char *ebuf, int *uerror)
13308cf6c252SPaul Traina {
13318cf6c252SPaul Traina 
1332ee2dd488SSam Leffler 	return (recv_ack(fd, DL_BIND_ACK_SIZE, "bind", bufp, ebuf, uerror));
13338cf6c252SPaul Traina }
13348cf6c252SPaul Traina 
13358cf6c252SPaul Traina static int
13368cf6c252SPaul Traina dlokack(int fd, const char *what, char *bufp, char *ebuf)
13378cf6c252SPaul Traina {
13388cf6c252SPaul Traina 
1339ee2dd488SSam Leffler 	return (recv_ack(fd, DL_OK_ACK_SIZE, what, bufp, ebuf, NULL));
13408cf6c252SPaul Traina }
13418cf6c252SPaul Traina 
13428cf6c252SPaul Traina 
13438cf6c252SPaul Traina static int
13448cf6c252SPaul Traina dlinforeq(int fd, char *ebuf)
13458cf6c252SPaul Traina {
13468cf6c252SPaul Traina 	dl_info_req_t req;
13478cf6c252SPaul Traina 
13488cf6c252SPaul Traina 	req.dl_primitive = DL_INFO_REQ;
13498cf6c252SPaul Traina 
13508cf6c252SPaul Traina 	return (send_request(fd, (char *)&req, sizeof(req), "info", ebuf));
13518cf6c252SPaul Traina }
13528cf6c252SPaul Traina 
13538cf6c252SPaul Traina static int
13548cf6c252SPaul Traina dlinfoack(int fd, char *bufp, char *ebuf)
13558cf6c252SPaul Traina {
13568cf6c252SPaul Traina 
1357ee2dd488SSam Leffler 	return (recv_ack(fd, DL_INFO_ACK_SIZE, "info", bufp, ebuf, NULL));
13588cf6c252SPaul Traina }
13598cf6c252SPaul Traina 
1360a8e07101SRui Paulo #ifdef HAVE_DLPI_PASSIVE
1361a8e07101SRui Paulo /*
1362a8e07101SRui Paulo  * Enable DLPI passive mode. We do not care if this request fails, as this
1363a8e07101SRui Paulo  * indicates the underlying DLPI device does not support link aggregation.
1364a8e07101SRui Paulo  */
1365a8e07101SRui Paulo static void
1366a8e07101SRui Paulo dlpassive(int fd, char *ebuf)
1367a8e07101SRui Paulo {
1368a8e07101SRui Paulo 	dl_passive_req_t req;
1369a8e07101SRui Paulo 	bpf_u_int32 buf[MAXDLBUF];
1370a8e07101SRui Paulo 
1371a8e07101SRui Paulo 	req.dl_primitive = DL_PASSIVE_REQ;
1372a8e07101SRui Paulo 
1373a8e07101SRui Paulo 	if (send_request(fd, (char *)&req, sizeof(req), "dlpassive", ebuf) == 0)
1374a8e07101SRui Paulo 	    (void) dlokack(fd, "dlpassive", (char *)buf, ebuf);
1375a8e07101SRui Paulo }
1376a8e07101SRui Paulo #endif
1377a8e07101SRui Paulo 
137804fb2745SSam Leffler #ifdef DL_HP_RAWDLS
137904fb2745SSam Leffler /*
138004fb2745SSam Leffler  * There's an ack *if* there's an error.
138104fb2745SSam Leffler  */
138204fb2745SSam Leffler static int
138304fb2745SSam Leffler dlrawdatareq(int fd, const u_char *datap, int datalen)
138404fb2745SSam Leffler {
138504fb2745SSam Leffler 	struct strbuf ctl, data;
138604fb2745SSam Leffler 	long buf[MAXDLBUF];	/* XXX - char? */
138704fb2745SSam Leffler 	union DL_primitives *dlp;
138804fb2745SSam Leffler 	int dlen;
138904fb2745SSam Leffler 
1390a0ee43a1SRui Paulo 	dlp = MAKE_DL_PRIMITIVES(buf);
139104fb2745SSam Leffler 
139204fb2745SSam Leffler 	dlp->dl_primitive = DL_HP_RAWDATA_REQ;
139304fb2745SSam Leffler 	dlen = DL_HP_RAWDATA_REQ_SIZE;
139404fb2745SSam Leffler 
139504fb2745SSam Leffler 	/*
139604fb2745SSam Leffler 	 * HP's documentation doesn't appear to show us supplying any
139704fb2745SSam Leffler 	 * address pointed to by the control part of the message.
139804fb2745SSam Leffler 	 * I think that's what raw mode means - you just send the raw
139904fb2745SSam Leffler 	 * packet, you don't specify where to send it to, as that's
140004fb2745SSam Leffler 	 * implied by the destination address.
140104fb2745SSam Leffler 	 */
140204fb2745SSam Leffler 	ctl.maxlen = 0;
140304fb2745SSam Leffler 	ctl.len = dlen;
140404fb2745SSam Leffler 	ctl.buf = (void *)buf;
140504fb2745SSam Leffler 
140604fb2745SSam Leffler 	data.maxlen = 0;
140704fb2745SSam Leffler 	data.len = datalen;
140804fb2745SSam Leffler 	data.buf = (void *)datap;
140904fb2745SSam Leffler 
141004fb2745SSam Leffler 	return (putmsg(fd, &ctl, &data, 0));
141104fb2745SSam Leffler }
141204fb2745SSam Leffler #endif /* DL_HP_RAWDLS */
141304fb2745SSam Leffler 
14148cf6c252SPaul Traina #if defined(HAVE_SOLARIS) && defined(HAVE_SYS_BUFMOD_H)
14158cf6c252SPaul Traina static char *
14168cf6c252SPaul Traina get_release(bpf_u_int32 *majorp, bpf_u_int32 *minorp, bpf_u_int32 *microp)
14178cf6c252SPaul Traina {
14188cf6c252SPaul Traina 	char *cp;
14198cf6c252SPaul Traina 	static char buf[32];
14208cf6c252SPaul Traina 
14218cf6c252SPaul Traina 	*majorp = 0;
14228cf6c252SPaul Traina 	*minorp = 0;
14238cf6c252SPaul Traina 	*microp = 0;
14248cf6c252SPaul Traina 	if (sysinfo(SI_RELEASE, buf, sizeof(buf)) < 0)
14258cf6c252SPaul Traina 		return ("?");
14268cf6c252SPaul Traina 	cp = buf;
14270a94d38fSBill Fenner 	if (!isdigit((unsigned char)*cp))
14288cf6c252SPaul Traina 		return (buf);
14298cf6c252SPaul Traina 	*majorp = strtol(cp, &cp, 10);
14308cf6c252SPaul Traina 	if (*cp++ != '.')
14318cf6c252SPaul Traina 		return (buf);
14328cf6c252SPaul Traina 	*minorp =  strtol(cp, &cp, 10);
14338cf6c252SPaul Traina 	if (*cp++ != '.')
14348cf6c252SPaul Traina 		return (buf);
14358cf6c252SPaul Traina 	*microp =  strtol(cp, &cp, 10);
14368cf6c252SPaul Traina 	return (buf);
14378cf6c252SPaul Traina }
14388cf6c252SPaul Traina #endif
14398cf6c252SPaul Traina 
144004fb2745SSam Leffler #ifdef DL_HP_PPA_REQ
14418cf6c252SPaul Traina /*
1442dc2c7305SBill Fenner  * Under HP-UX 10 and HP-UX 11, we can ask for the ppa
14438cf6c252SPaul Traina  */
14448cf6c252SPaul Traina 
14458cf6c252SPaul Traina 
1446dc2c7305SBill Fenner /*
1447dc2c7305SBill Fenner  * Determine ppa number that specifies ifname.
1448dc2c7305SBill Fenner  *
1449dc2c7305SBill Fenner  * If the "dl_hp_ppa_info_t" doesn't have a "dl_module_id_1" member,
1450dc2c7305SBill Fenner  * the code that's used here is the old code for HP-UX 10.x.
1451dc2c7305SBill Fenner  *
1452dc2c7305SBill Fenner  * However, HP-UX 10.20, at least, appears to have such a member
1453dc2c7305SBill Fenner  * in its "dl_hp_ppa_info_t" structure, so the new code is used.
1454dc2c7305SBill Fenner  * The new code didn't work on an old 10.20 system on which Rick
1455dc2c7305SBill Fenner  * Jones of HP tried it, but with later patches installed, it
1456dc2c7305SBill Fenner  * worked - it appears that the older system had those members but
1457dc2c7305SBill Fenner  * didn't put anything in them, so, if the search by name fails, we
1458dc2c7305SBill Fenner  * do the old search.
1459dc2c7305SBill Fenner  *
1460dc2c7305SBill Fenner  * Rick suggests that making sure your system is "up on the latest
1461dc2c7305SBill Fenner  * lancommon/DLPI/driver patches" is probably a good idea; it'd fix
1462dc2c7305SBill Fenner  * that problem, as well as allowing libpcap to see packets sent
1463dc2c7305SBill Fenner  * from the system on which the libpcap application is being run.
1464dc2c7305SBill Fenner  * (On 10.20, in addition to getting the latest patches, you need
1465dc2c7305SBill Fenner  * to turn the kernel "lanc_outbound_promisc_flag" flag on with ADB;
1466dc2c7305SBill Fenner  * a posting to "comp.sys.hp.hpux" at
1467dc2c7305SBill Fenner  *
1468dc2c7305SBill Fenner  *	http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=558092266
1469dc2c7305SBill Fenner  *
1470dc2c7305SBill Fenner  * says that, to see the machine's outgoing traffic, you'd need to
1471dc2c7305SBill Fenner  * apply the right patches to your system, and also set that variable
1472dc2c7305SBill Fenner  * with:
1473dc2c7305SBill Fenner 
1474dc2c7305SBill Fenner echo 'lanc_outbound_promisc_flag/W1' | /usr/bin/adb -w /stand/vmunix /dev/kmem
1475dc2c7305SBill Fenner 
1476dc2c7305SBill Fenner  * which could be put in, for example, "/sbin/init.d/lan".
1477dc2c7305SBill Fenner  *
1478dc2c7305SBill Fenner  * Setting the variable is not necessary on HP-UX 11.x.
1479dc2c7305SBill Fenner  */
14808cf6c252SPaul Traina static int
14818cf6c252SPaul Traina get_dlpi_ppa(register int fd, register const char *device, register int unit,
14828cf6c252SPaul Traina     register char *ebuf)
14838cf6c252SPaul Traina {
14848cf6c252SPaul Traina 	register dl_hp_ppa_ack_t *ap;
1485dc2c7305SBill Fenner 	register dl_hp_ppa_info_t *ipstart, *ip;
14868cf6c252SPaul Traina 	register int i;
1487dc2c7305SBill Fenner 	char dname[100];
14888cf6c252SPaul Traina 	register u_long majdev;
14898cf6c252SPaul Traina 	struct stat statbuf;
1490dc2c7305SBill Fenner 	dl_hp_ppa_req_t	req;
14910a94d38fSBill Fenner 	char buf[MAXDLBUF];
14920a94d38fSBill Fenner 	char *ppa_data_buf;
14930a94d38fSBill Fenner 	dl_hp_ppa_ack_t	*dlp;
14940a94d38fSBill Fenner 	struct strbuf ctl;
14950a94d38fSBill Fenner 	int flags;
14960a94d38fSBill Fenner 	int ppa;
14978cf6c252SPaul Traina 
14988cf6c252SPaul Traina 	memset((char *)&req, 0, sizeof(req));
14998cf6c252SPaul Traina 	req.dl_primitive = DL_HP_PPA_REQ;
15008cf6c252SPaul Traina 
15018cf6c252SPaul Traina 	memset((char *)buf, 0, sizeof(buf));
15020a94d38fSBill Fenner 	if (send_request(fd, (char *)&req, sizeof(req), "hpppa", ebuf) < 0)
1503a8e07101SRui Paulo 		return (PCAP_ERROR);
15048cf6c252SPaul Traina 
15050a94d38fSBill Fenner 	ctl.maxlen = DL_HP_PPA_ACK_SIZE;
15060a94d38fSBill Fenner 	ctl.len = 0;
15070a94d38fSBill Fenner 	ctl.buf = (char *)buf;
15080a94d38fSBill Fenner 
15090a94d38fSBill Fenner 	flags = 0;
15100a94d38fSBill Fenner 	/*
15110a94d38fSBill Fenner 	 * DLPI may return a big chunk of data for a DL_HP_PPA_REQ. The normal
15120a94d38fSBill Fenner 	 * recv_ack will fail because it set the maxlen to MAXDLBUF (8192)
15130a94d38fSBill Fenner 	 * which is NOT big enough for a DL_HP_PPA_REQ.
15140a94d38fSBill Fenner 	 *
15150a94d38fSBill Fenner 	 * This causes libpcap applications to fail on a system with HP-APA
15160a94d38fSBill Fenner 	 * installed.
15170a94d38fSBill Fenner 	 *
15180a94d38fSBill Fenner 	 * To figure out how big the returned data is, we first call getmsg
15190a94d38fSBill Fenner 	 * to get the small head and peek at the head to get the actual data
15200a94d38fSBill Fenner 	 * length, and  then issue another getmsg to get the actual PPA data.
15210a94d38fSBill Fenner 	 */
15220a94d38fSBill Fenner 	/* get the head first */
15230a94d38fSBill Fenner 	if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) {
15240a94d38fSBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
15250a94d38fSBill Fenner 		    "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno));
1526a8e07101SRui Paulo 		return (PCAP_ERROR);
15270a94d38fSBill Fenner 	}
15280a94d38fSBill Fenner 
15290a94d38fSBill Fenner 	dlp = (dl_hp_ppa_ack_t *)ctl.buf;
15300a94d38fSBill Fenner 	if (dlp->dl_primitive != DL_HP_PPA_ACK) {
15310a94d38fSBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
15320a94d38fSBill Fenner 		    "get_dlpi_ppa: hpppa unexpected primitive ack 0x%x",
15330a94d38fSBill Fenner 		    (bpf_u_int32)dlp->dl_primitive);
1534a8e07101SRui Paulo 		return (PCAP_ERROR);
15350a94d38fSBill Fenner 	}
15360a94d38fSBill Fenner 
15370a94d38fSBill Fenner 	if (ctl.len < DL_HP_PPA_ACK_SIZE) {
15380a94d38fSBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
1539feb4ecdbSBruce M Simpson 		    "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
1540feb4ecdbSBruce M Simpson 		     ctl.len, (unsigned long)DL_HP_PPA_ACK_SIZE);
1541a8e07101SRui Paulo 		return (PCAP_ERROR);
15420a94d38fSBill Fenner 	}
15430a94d38fSBill Fenner 
15440a94d38fSBill Fenner 	/* allocate buffer */
15450a94d38fSBill Fenner 	if ((ppa_data_buf = (char *)malloc(dlp->dl_length)) == NULL) {
15460a94d38fSBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
15470a94d38fSBill Fenner 		    "get_dlpi_ppa: hpppa malloc: %s", pcap_strerror(errno));
1548a8e07101SRui Paulo 		return (PCAP_ERROR);
15490a94d38fSBill Fenner 	}
15500a94d38fSBill Fenner 	ctl.maxlen = dlp->dl_length;
15510a94d38fSBill Fenner 	ctl.len = 0;
15520a94d38fSBill Fenner 	ctl.buf = (char *)ppa_data_buf;
15530a94d38fSBill Fenner 	/* get the data */
15540a94d38fSBill Fenner 	if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) {
15550a94d38fSBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
15560a94d38fSBill Fenner 		    "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno));
15570a94d38fSBill Fenner 		free(ppa_data_buf);
1558a8e07101SRui Paulo 		return (PCAP_ERROR);
15590a94d38fSBill Fenner 	}
15600a94d38fSBill Fenner 	if (ctl.len < dlp->dl_length) {
15610a94d38fSBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
1562a0ee43a1SRui Paulo 		    "get_dlpi_ppa: hpppa ack too small (%d < %lu)",
1563a0ee43a1SRui Paulo 		    ctl.len, (unsigned long)dlp->dl_length);
15640a94d38fSBill Fenner 		free(ppa_data_buf);
1565a8e07101SRui Paulo 		return (PCAP_ERROR);
15660a94d38fSBill Fenner 	}
15670a94d38fSBill Fenner 
15688cf6c252SPaul Traina 	ap = (dl_hp_ppa_ack_t *)buf;
15690a94d38fSBill Fenner 	ipstart = (dl_hp_ppa_info_t *)ppa_data_buf;
1570dc2c7305SBill Fenner 	ip = ipstart;
15718cf6c252SPaul Traina 
1572dc2c7305SBill Fenner #ifdef HAVE_HP_PPA_INFO_T_DL_MODULE_ID_1
1573dc2c7305SBill Fenner 	/*
1574dc2c7305SBill Fenner 	 * The "dl_hp_ppa_info_t" structure has a "dl_module_id_1"
1575dc2c7305SBill Fenner 	 * member that should, in theory, contain the part of the
1576dc2c7305SBill Fenner 	 * name for the device that comes before the unit number,
1577dc2c7305SBill Fenner 	 * and should also have a "dl_module_id_2" member that may
1578dc2c7305SBill Fenner 	 * contain an alternate name (e.g., I think Ethernet devices
1579dc2c7305SBill Fenner 	 * have both "lan", for "lanN", and "snap", for "snapN", with
1580dc2c7305SBill Fenner 	 * the former being for Ethernet packets and the latter being
1581dc2c7305SBill Fenner 	 * for 802.3/802.2 packets).
1582dc2c7305SBill Fenner 	 *
1583dc2c7305SBill Fenner 	 * Search for the device that has the specified name and
1584dc2c7305SBill Fenner 	 * instance number.
1585dc2c7305SBill Fenner 	 */
15868cf6c252SPaul Traina 	for (i = 0; i < ap->dl_count; i++) {
15870a94d38fSBill Fenner 		if ((strcmp((const char *)ip->dl_module_id_1, device) == 0 ||
15880a94d38fSBill Fenner 		     strcmp((const char *)ip->dl_module_id_2, device) == 0) &&
1589dc2c7305SBill Fenner 		    ip->dl_instance_num == unit)
15908cf6c252SPaul Traina 			break;
15918cf6c252SPaul Traina 
1592dc2c7305SBill Fenner 		ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset);
1593dc2c7305SBill Fenner 	}
1594dc2c7305SBill Fenner #else
1595dc2c7305SBill Fenner 	/*
1596dc2c7305SBill Fenner 	 * We don't have that member, so the search is impossible; make it
1597dc2c7305SBill Fenner 	 * look as if the search failed.
1598dc2c7305SBill Fenner 	 */
1599dc2c7305SBill Fenner 	i = ap->dl_count;
1600dc2c7305SBill Fenner #endif
1601dc2c7305SBill Fenner 
1602dc2c7305SBill Fenner 	if (i == ap->dl_count) {
1603dc2c7305SBill Fenner 		/*
1604dc2c7305SBill Fenner 		 * Well, we didn't, or can't, find the device by name.
1605dc2c7305SBill Fenner 		 *
1606dc2c7305SBill Fenner 		 * HP-UX 10.20, whilst it has "dl_module_id_1" and
1607dc2c7305SBill Fenner 		 * "dl_module_id_2" fields in the "dl_hp_ppa_info_t",
1608dc2c7305SBill Fenner 		 * doesn't seem to fill them in unless the system is
1609dc2c7305SBill Fenner 		 * at a reasonably up-to-date patch level.
1610dc2c7305SBill Fenner 		 *
1611dc2c7305SBill Fenner 		 * Older HP-UX 10.x systems might not have those fields
1612dc2c7305SBill Fenner 		 * at all.
1613dc2c7305SBill Fenner 		 *
1614dc2c7305SBill Fenner 		 * Therefore, we'll search for the entry with the major
1615dc2c7305SBill Fenner 		 * device number of a device with the name "/dev/<dev><unit>",
1616dc2c7305SBill Fenner 		 * if such a device exists, as the old code did.
1617dc2c7305SBill Fenner 		 */
1618dc2c7305SBill Fenner 		snprintf(dname, sizeof(dname), "/dev/%s%d", device, unit);
1619dc2c7305SBill Fenner 		if (stat(dname, &statbuf) < 0) {
1620dc2c7305SBill Fenner 			snprintf(ebuf, PCAP_ERRBUF_SIZE, "stat: %s: %s",
1621dc2c7305SBill Fenner 			    dname, pcap_strerror(errno));
1622a8e07101SRui Paulo 			return (PCAP_ERROR);
1623dc2c7305SBill Fenner 		}
1624dc2c7305SBill Fenner 		majdev = major(statbuf.st_rdev);
1625dc2c7305SBill Fenner 
1626dc2c7305SBill Fenner 		ip = ipstart;
1627dc2c7305SBill Fenner 
1628dc2c7305SBill Fenner 		for (i = 0; i < ap->dl_count; i++) {
1629dc2c7305SBill Fenner 			if (ip->dl_mjr_num == majdev &&
1630dc2c7305SBill Fenner 			    ip->dl_instance_num == unit)
1631dc2c7305SBill Fenner 				break;
1632dc2c7305SBill Fenner 
1633dc2c7305SBill Fenner 			ip = (dl_hp_ppa_info_t *)((u_char *)ipstart + ip->dl_next_offset);
1634dc2c7305SBill Fenner 		}
16358cf6c252SPaul Traina 	}
16368cf6c252SPaul Traina 	if (i == ap->dl_count) {
1637dc2c7305SBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
1638dc2c7305SBill Fenner 		    "can't find /dev/dlpi PPA for %s%d", device, unit);
1639a8e07101SRui Paulo 		return (PCAP_ERROR_NO_SUCH_DEVICE);
16408cf6c252SPaul Traina 	}
16418cf6c252SPaul Traina 	if (ip->dl_hdw_state == HDW_DEAD) {
1642dc2c7305SBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
1643dc2c7305SBill Fenner 		    "%s%d: hardware state: DOWN\n", device, unit);
16440a94d38fSBill Fenner 		free(ppa_data_buf);
1645a8e07101SRui Paulo 		return (PCAP_ERROR);
16468cf6c252SPaul Traina 	}
16470a94d38fSBill Fenner 	ppa = ip->dl_ppa;
16480a94d38fSBill Fenner 	free(ppa_data_buf);
16490a94d38fSBill Fenner 	return (ppa);
16508cf6c252SPaul Traina }
16518cf6c252SPaul Traina #endif
16528cf6c252SPaul Traina 
16538cf6c252SPaul Traina #ifdef HAVE_HPUX9
16548cf6c252SPaul Traina /*
16558cf6c252SPaul Traina  * Under HP-UX 9, there is no good way to determine the ppa.
16568cf6c252SPaul Traina  * So punt and read it from /dev/kmem.
16578cf6c252SPaul Traina  */
16588cf6c252SPaul Traina static struct nlist nl[] = {
16598cf6c252SPaul Traina #define NL_IFNET 0
16608cf6c252SPaul Traina 	{ "ifnet" },
16618cf6c252SPaul Traina 	{ "" }
16628cf6c252SPaul Traina };
16638cf6c252SPaul Traina 
16648cf6c252SPaul Traina static char path_vmunix[] = "/hp-ux";
16658cf6c252SPaul Traina 
16668cf6c252SPaul Traina /* Determine ppa number that specifies ifname */
16678cf6c252SPaul Traina static int
16688cf6c252SPaul Traina get_dlpi_ppa(register int fd, register const char *ifname, register int unit,
16698cf6c252SPaul Traina     register char *ebuf)
16708cf6c252SPaul Traina {
16718cf6c252SPaul Traina 	register const char *cp;
16728cf6c252SPaul Traina 	register int kd;
16738cf6c252SPaul Traina 	void *addr;
16748cf6c252SPaul Traina 	struct ifnet ifnet;
1675dc2c7305SBill Fenner 	char if_name[sizeof(ifnet.if_name) + 1];
16768cf6c252SPaul Traina 
16778cf6c252SPaul Traina 	cp = strrchr(ifname, '/');
16788cf6c252SPaul Traina 	if (cp != NULL)
16798cf6c252SPaul Traina 		ifname = cp + 1;
16808cf6c252SPaul Traina 	if (nlist(path_vmunix, &nl) < 0) {
1681dc2c7305SBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "nlist %s failed",
1682dc2c7305SBill Fenner 		    path_vmunix);
16838cf6c252SPaul Traina 		return (-1);
16848cf6c252SPaul Traina 	}
16858cf6c252SPaul Traina 	if (nl[NL_IFNET].n_value == 0) {
1686dc2c7305SBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE,
1687dc2c7305SBill Fenner 		    "could't find %s kernel symbol",
16888cf6c252SPaul Traina 		    nl[NL_IFNET].n_name);
16898cf6c252SPaul Traina 		return (-1);
16908cf6c252SPaul Traina 	}
16918cf6c252SPaul Traina 	kd = open("/dev/kmem", O_RDONLY);
16928cf6c252SPaul Traina 	if (kd < 0) {
1693dc2c7305SBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "kmem open: %s",
1694dc2c7305SBill Fenner 		    pcap_strerror(errno));
16958cf6c252SPaul Traina 		return (-1);
16968cf6c252SPaul Traina 	}
16978cf6c252SPaul Traina 	if (dlpi_kread(kd, nl[NL_IFNET].n_value,
16988cf6c252SPaul Traina 	    &addr, sizeof(addr), ebuf) < 0) {
16998cf6c252SPaul Traina 		close(kd);
17008cf6c252SPaul Traina 		return (-1);
17018cf6c252SPaul Traina 	}
17028cf6c252SPaul Traina 	for (; addr != NULL; addr = ifnet.if_next) {
17038cf6c252SPaul Traina 		if (dlpi_kread(kd, (off_t)addr,
17048cf6c252SPaul Traina 		    &ifnet, sizeof(ifnet), ebuf) < 0 ||
17058cf6c252SPaul Traina 		    dlpi_kread(kd, (off_t)ifnet.if_name,
1706dc2c7305SBill Fenner 		    if_name, sizeof(ifnet.if_name), ebuf) < 0) {
17078cf6c252SPaul Traina 			(void)close(kd);
17088cf6c252SPaul Traina 			return (-1);
17098cf6c252SPaul Traina 		}
1710dc2c7305SBill Fenner 		if_name[sizeof(ifnet.if_name)] = '\0';
1711dc2c7305SBill Fenner 		if (strcmp(if_name, ifname) == 0 && ifnet.if_unit == unit)
17128cf6c252SPaul Traina 			return (ifnet.if_index);
17138cf6c252SPaul Traina 	}
17148cf6c252SPaul Traina 
1715dc2c7305SBill Fenner 	snprintf(ebuf, PCAP_ERRBUF_SIZE, "Can't find %s", ifname);
17168cf6c252SPaul Traina 	return (-1);
17178cf6c252SPaul Traina }
17188cf6c252SPaul Traina 
17198cf6c252SPaul Traina static int
17208cf6c252SPaul Traina dlpi_kread(register int fd, register off_t addr,
17218cf6c252SPaul Traina     register void *buf, register u_int len, register char *ebuf)
17228cf6c252SPaul Traina {
17238cf6c252SPaul Traina 	register int cc;
17248cf6c252SPaul Traina 
1725a4b5b39fSBill Fenner 	if (lseek(fd, addr, SEEK_SET) < 0) {
1726dc2c7305SBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "lseek: %s",
1727dc2c7305SBill Fenner 		    pcap_strerror(errno));
17288cf6c252SPaul Traina 		return (-1);
17298cf6c252SPaul Traina 	}
17308cf6c252SPaul Traina 	cc = read(fd, buf, len);
17318cf6c252SPaul Traina 	if (cc < 0) {
1732dc2c7305SBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "read: %s",
1733dc2c7305SBill Fenner 		    pcap_strerror(errno));
17348cf6c252SPaul Traina 		return (-1);
17358cf6c252SPaul Traina 	} else if (cc != len) {
1736dc2c7305SBill Fenner 		snprintf(ebuf, PCAP_ERRBUF_SIZE, "short read (%d != %d)", cc,
1737dc2c7305SBill Fenner 		    len);
17388cf6c252SPaul Traina 		return (-1);
17398cf6c252SPaul Traina 	}
17408cf6c252SPaul Traina 	return (cc);
17418cf6c252SPaul Traina }
17428cf6c252SPaul Traina #endif
1743a8e07101SRui Paulo 
1744a8e07101SRui Paulo pcap_t *
1745edc89b24SXin LI pcap_create_interface(const char *device, char *ebuf)
1746a8e07101SRui Paulo {
1747a8e07101SRui Paulo 	pcap_t *p;
1748*681ed54cSXin LI #ifdef DL_HP_RAWDLS
1749*681ed54cSXin LI 	struct pcap_dlpi *pd;
1750*681ed54cSXin LI #endif
1751a8e07101SRui Paulo 
1752*681ed54cSXin LI 	p = pcap_create_common(device, ebuf, sizeof (struct pcap_dlpi));
1753a8e07101SRui Paulo 	if (p == NULL)
1754a8e07101SRui Paulo 		return (NULL);
1755a8e07101SRui Paulo 
1756*681ed54cSXin LI #ifdef DL_HP_RAWDLS
1757*681ed54cSXin LI 	pd = p->priv;
1758*681ed54cSXin LI 	pd->send_fd = -1;	/* it hasn't been opened yet */
1759*681ed54cSXin LI #endif
1760a8e07101SRui Paulo 
1761a8e07101SRui Paulo 	p->activate_op = pcap_activate_dlpi;
1762a8e07101SRui Paulo 	return (p);
1763a8e07101SRui Paulo }
1764