xref: /freebsd/contrib/libpcap/pcap-libdlpi.c (revision afdbf109c6a661a729938f68211054a0a50d38ac)
1a8e07101SRui Paulo /*
2a8e07101SRui Paulo  * Copyright (c) 1993, 1994, 1995, 1996, 1997
3a8e07101SRui Paulo  *	The Regents of the University of California.  All rights reserved.
4a8e07101SRui Paulo  *
5a8e07101SRui Paulo  * Redistribution and use in source and binary forms, with or without
6a8e07101SRui Paulo  * modification, are permitted provided that: (1) source code distributions
7a8e07101SRui Paulo  * retain the above copyright notice and this paragraph in its entirety, (2)
8a8e07101SRui Paulo  * distributions including binary code include the above copyright notice and
9a8e07101SRui Paulo  * this paragraph in its entirety in the documentation or other materials
10a8e07101SRui Paulo  * provided with the distribution, and (3) all advertising materials mentioning
11a8e07101SRui Paulo  * features or use of this software display the following acknowledgement:
12a8e07101SRui Paulo  * ``This product includes software developed by the University of California,
13a8e07101SRui Paulo  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14a8e07101SRui Paulo  * the University nor the names of its contributors may be used to endorse
15a8e07101SRui Paulo  * or promote products derived from this software without specific prior
16a8e07101SRui Paulo  * written permission.
17a8e07101SRui Paulo  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18a8e07101SRui Paulo  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19a8e07101SRui Paulo  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20a8e07101SRui Paulo  *
21a8e07101SRui Paulo  * This code contributed by Sagun Shakya (sagun.shakya@sun.com)
22a8e07101SRui Paulo  */
23a8e07101SRui Paulo /*
24a8e07101SRui Paulo  * Packet capture routines for DLPI using libdlpi under SunOS 5.11.
25a8e07101SRui Paulo  */
26a8e07101SRui Paulo 
27b00ab754SHans Petter Selasky #include <config.h>
28a8e07101SRui Paulo 
29a8e07101SRui Paulo #include <sys/types.h>
30a8e07101SRui Paulo #include <sys/time.h>
31a8e07101SRui Paulo #include <sys/bufmod.h>
32a8e07101SRui Paulo #include <sys/stream.h>
33a8e07101SRui Paulo #include <libdlpi.h>
34a8e07101SRui Paulo #include <errno.h>
35a8e07101SRui Paulo #include <memory.h>
36a8e07101SRui Paulo #include <stropts.h>
37a8e07101SRui Paulo #include <stdio.h>
38a8e07101SRui Paulo #include <stdlib.h>
39a8e07101SRui Paulo #include <string.h>
40a8e07101SRui Paulo 
41a8e07101SRui Paulo #include "pcap-int.h"
42a8e07101SRui Paulo #include "dlpisubs.h"
43a8e07101SRui Paulo 
44a8e07101SRui Paulo /* Forwards. */
45d1e87331SXin LI static int dlpromiscon(pcap_t *, bpf_u_int32);
46a8e07101SRui Paulo static int pcap_read_libdlpi(pcap_t *, int, pcap_handler, u_char *);
476f9cba8fSJoseph Mingrone static int pcap_inject_libdlpi(pcap_t *, const void *, int);
48a8e07101SRui Paulo static void pcap_libdlpi_err(const char *, const char *, int, char *);
49a0ee43a1SRui Paulo static void pcap_cleanup_libdlpi(pcap_t *);
50a8e07101SRui Paulo 
51a8e07101SRui Paulo /*
52a8e07101SRui Paulo  * list_interfaces() will list all the network links that are
53a8e07101SRui Paulo  * available on a system.
54a8e07101SRui Paulo  */
55a8e07101SRui Paulo static boolean_t list_interfaces(const char *, void *);
56a8e07101SRui Paulo 
57a8e07101SRui Paulo typedef struct linknamelist {
58a8e07101SRui Paulo 	char	linkname[DLPI_LINKNAME_MAX];
59a8e07101SRui Paulo 	struct linknamelist *lnl_next;
60a8e07101SRui Paulo } linknamelist_t;
61a8e07101SRui Paulo 
62a8e07101SRui Paulo typedef struct linkwalk {
63a8e07101SRui Paulo 	linknamelist_t	*lw_list;
64a8e07101SRui Paulo 	int		lw_err;
65a8e07101SRui Paulo } linkwalk_t;
66a8e07101SRui Paulo 
67a8e07101SRui Paulo /*
68a8e07101SRui Paulo  * The caller of this function should free the memory allocated
69a8e07101SRui Paulo  * for each linknamelist_t "entry" allocated.
70a8e07101SRui Paulo  */
71a8e07101SRui Paulo static boolean_t
list_interfaces(const char * linkname,void * arg)72a8e07101SRui Paulo list_interfaces(const char *linkname, void *arg)
73a8e07101SRui Paulo {
74a8e07101SRui Paulo 	linkwalk_t	*lwp = arg;
75a8e07101SRui Paulo 	linknamelist_t	*entry;
76a8e07101SRui Paulo 
77a8e07101SRui Paulo 	if ((entry = calloc(1, sizeof(linknamelist_t))) == NULL) {
78a8e07101SRui Paulo 		lwp->lw_err = ENOMEM;
79a8e07101SRui Paulo 		return (B_TRUE);
80a8e07101SRui Paulo 	}
81*afdbf109SJoseph Mingrone 	(void) pcapint_strlcpy(entry->linkname, linkname, DLPI_LINKNAME_MAX);
82a8e07101SRui Paulo 
83a8e07101SRui Paulo 	if (lwp->lw_list == NULL) {
84a8e07101SRui Paulo 		lwp->lw_list = entry;
85a8e07101SRui Paulo 	} else {
86a8e07101SRui Paulo 		entry->lnl_next = lwp->lw_list;
87a8e07101SRui Paulo 		lwp->lw_list = entry;
88a8e07101SRui Paulo 	}
89a8e07101SRui Paulo 
90a8e07101SRui Paulo 	return (B_FALSE);
91a8e07101SRui Paulo }
92a8e07101SRui Paulo 
93a8e07101SRui Paulo static int
pcap_activate_libdlpi(pcap_t * p)94a8e07101SRui Paulo pcap_activate_libdlpi(pcap_t *p)
95a8e07101SRui Paulo {
96681ed54cSXin LI 	struct pcap_dlpi *pd = p->priv;
97681ed54cSXin LI 	int status = 0;
98a8e07101SRui Paulo 	int retv;
99a8e07101SRui Paulo 	dlpi_handle_t dh;
100a8e07101SRui Paulo 	dlpi_info_t dlinfo;
101a8e07101SRui Paulo 
102a8e07101SRui Paulo 	/*
103a8e07101SRui Paulo 	 * Enable Solaris raw and passive DLPI extensions;
104a8e07101SRui Paulo 	 * dlpi_open() will not fail if the underlying link does not support
105a8e07101SRui Paulo 	 * passive mode. See dlpi(7P) for details.
106a8e07101SRui Paulo 	 */
107ada6f083SXin LI 	retv = dlpi_open(p->opt.device, &dh, DLPI_RAW|DLPI_PASSIVE);
108a8e07101SRui Paulo 	if (retv != DLPI_SUCCESS) {
1096f9cba8fSJoseph Mingrone 		if (retv == DLPI_ELINKNAMEINVAL || retv == DLPI_ENOLINK) {
1106f9cba8fSJoseph Mingrone 			/*
1116f9cba8fSJoseph Mingrone 			 * There's nothing more to say, so clear the
1126f9cba8fSJoseph Mingrone 			 * error message.
1136f9cba8fSJoseph Mingrone 			 */
114681ed54cSXin LI 			status = PCAP_ERROR_NO_SUCH_DEVICE;
1156f9cba8fSJoseph Mingrone 			p->errbuf[0] = '\0';
1166f9cba8fSJoseph Mingrone 		} else if (retv == DL_SYSERR &&
1176f9cba8fSJoseph Mingrone 		    (errno == EPERM || errno == EACCES)) {
118681ed54cSXin LI 			status = PCAP_ERROR_PERM_DENIED;
1196f9cba8fSJoseph Mingrone 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
1206f9cba8fSJoseph Mingrone 			    "Attempt to open DLPI device failed with %s - root privilege may be required",
1216f9cba8fSJoseph Mingrone 			    (errno == EPERM) ? "EPERM" : "EACCES");
1226f9cba8fSJoseph Mingrone 		} else {
123681ed54cSXin LI 			status = PCAP_ERROR;
124ada6f083SXin LI 			pcap_libdlpi_err(p->opt.device, "dlpi_open", retv,
125a8e07101SRui Paulo 			    p->errbuf);
1266f9cba8fSJoseph Mingrone 		}
127681ed54cSXin LI 		return (status);
128a8e07101SRui Paulo 	}
129681ed54cSXin LI 	pd->dlpi_hd = dh;
130a8e07101SRui Paulo 
131a8e07101SRui Paulo 	if (p->opt.rfmon) {
132a8e07101SRui Paulo 		/*
133a8e07101SRui Paulo 		 * This device exists, but we don't support monitor mode
134a8e07101SRui Paulo 		 * any platforms that support DLPI.
135a8e07101SRui Paulo 		 */
136681ed54cSXin LI 		status = PCAP_ERROR_RFMON_NOTSUP;
137a8e07101SRui Paulo 		goto bad;
138a8e07101SRui Paulo 	}
139a8e07101SRui Paulo 
140a8e07101SRui Paulo 	/* Bind with DLPI_ANY_SAP. */
141681ed54cSXin LI 	if ((retv = dlpi_bind(pd->dlpi_hd, DLPI_ANY_SAP, 0)) != DLPI_SUCCESS) {
142681ed54cSXin LI 		status = PCAP_ERROR;
143ada6f083SXin LI 		pcap_libdlpi_err(p->opt.device, "dlpi_bind", retv, p->errbuf);
144a8e07101SRui Paulo 		goto bad;
145a8e07101SRui Paulo 	}
146a8e07101SRui Paulo 
147b00ab754SHans Petter Selasky 	/*
148b00ab754SHans Petter Selasky 	 * Turn a negative snapshot value (invalid), a snapshot value of
149b00ab754SHans Petter Selasky 	 * 0 (unspecified), or a value bigger than the normal maximum
150b00ab754SHans Petter Selasky 	 * value, into the maximum allowed value.
151b00ab754SHans Petter Selasky 	 *
152b00ab754SHans Petter Selasky 	 * If some application really *needs* a bigger snapshot
153b00ab754SHans Petter Selasky 	 * length, we should just increase MAXIMUM_SNAPLEN.
154b00ab754SHans Petter Selasky 	 */
155b00ab754SHans Petter Selasky 	if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
156b00ab754SHans Petter Selasky 		p->snapshot = MAXIMUM_SNAPLEN;
157b00ab754SHans Petter Selasky 
158a8e07101SRui Paulo 	/* Enable promiscuous mode. */
159a8e07101SRui Paulo 	if (p->opt.promisc) {
160681ed54cSXin LI 		retv = dlpromiscon(p, DL_PROMISC_PHYS);
161681ed54cSXin LI 		if (retv < 0) {
162d1e87331SXin LI 			/*
163d1e87331SXin LI 			 * "You don't have permission to capture on
164d1e87331SXin LI 			 * this device" and "you don't have permission
165d1e87331SXin LI 			 * to capture in promiscuous mode on this
166d1e87331SXin LI 			 * device" are different; let the user know,
167d1e87331SXin LI 			 * so if they can't get permission to
168d1e87331SXin LI 			 * capture in promiscuous mode, they can at
169d1e87331SXin LI 			 * least try to capture in non-promiscuous
170d1e87331SXin LI 			 * mode.
171d1e87331SXin LI 			 *
172d1e87331SXin LI 			 * XXX - you might have to capture in
173d1e87331SXin LI 			 * promiscuous mode to see outgoing packets.
174d1e87331SXin LI 			 */
175681ed54cSXin LI 			if (retv == PCAP_ERROR_PERM_DENIED)
176681ed54cSXin LI 				status = PCAP_ERROR_PROMISC_PERM_DENIED;
177681ed54cSXin LI 			else
178681ed54cSXin LI 				status = retv;
179a8e07101SRui Paulo 			goto bad;
180a8e07101SRui Paulo 		}
181a8e07101SRui Paulo 	} else {
182a8e07101SRui Paulo 		/* Try to enable multicast. */
183681ed54cSXin LI 		retv = dlpromiscon(p, DL_PROMISC_MULTI);
184681ed54cSXin LI 		if (retv < 0) {
185681ed54cSXin LI 			status = retv;
186a8e07101SRui Paulo 			goto bad;
187a8e07101SRui Paulo 		}
188681ed54cSXin LI 	}
189a8e07101SRui Paulo 
190a8e07101SRui Paulo 	/* Try to enable SAP promiscuity. */
191681ed54cSXin LI 	retv = dlpromiscon(p, DL_PROMISC_SAP);
192681ed54cSXin LI 	if (retv < 0) {
193d1e87331SXin LI 		/*
194d1e87331SXin LI 		 * Not fatal, since the DL_PROMISC_PHYS mode worked.
195d1e87331SXin LI 		 * Report it as a warning, however.
196d1e87331SXin LI 		 */
197d1e87331SXin LI 		if (p->opt.promisc)
198681ed54cSXin LI 			status = PCAP_WARNING;
199681ed54cSXin LI 		else {
200681ed54cSXin LI 			status = retv;
201a8e07101SRui Paulo 			goto bad;
202a8e07101SRui Paulo 		}
203681ed54cSXin LI 	}
204a8e07101SRui Paulo 
205a8e07101SRui Paulo 	/* Determine link type.  */
206681ed54cSXin LI 	if ((retv = dlpi_info(pd->dlpi_hd, &dlinfo, 0)) != DLPI_SUCCESS) {
207681ed54cSXin LI 		status = PCAP_ERROR;
208ada6f083SXin LI 		pcap_libdlpi_err(p->opt.device, "dlpi_info", retv, p->errbuf);
209a8e07101SRui Paulo 		goto bad;
210a8e07101SRui Paulo 	}
211a8e07101SRui Paulo 
212681ed54cSXin LI 	if (pcap_process_mactype(p, dlinfo.di_mactype) != 0) {
213681ed54cSXin LI 		status = PCAP_ERROR;
214a8e07101SRui Paulo 		goto bad;
215681ed54cSXin LI 	}
216a8e07101SRui Paulo 
217681ed54cSXin LI 	p->fd = dlpi_fd(pd->dlpi_hd);
218a8e07101SRui Paulo 
219a8e07101SRui Paulo 	/* Push and configure bufmod. */
220681ed54cSXin LI 	if (pcap_conf_bufmod(p, p->snapshot) != 0) {
221681ed54cSXin LI 		status = PCAP_ERROR;
222a8e07101SRui Paulo 		goto bad;
223681ed54cSXin LI 	}
224a8e07101SRui Paulo 
225a8e07101SRui Paulo 	/*
226a8e07101SRui Paulo 	 * Flush the read side.
227a8e07101SRui Paulo 	 */
228a8e07101SRui Paulo 	if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
229681ed54cSXin LI 		status = PCAP_ERROR;
230*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
231b00ab754SHans Petter Selasky 		    errno, "FLUSHR");
232a8e07101SRui Paulo 		goto bad;
233a8e07101SRui Paulo 	}
234a8e07101SRui Paulo 
235a8e07101SRui Paulo 	/* Allocate data buffer. */
236681ed54cSXin LI 	if (pcap_alloc_databuf(p) != 0) {
237681ed54cSXin LI 		status = PCAP_ERROR;
238a8e07101SRui Paulo 		goto bad;
239681ed54cSXin LI 	}
240a8e07101SRui Paulo 
241a8e07101SRui Paulo 	/*
242a8e07101SRui Paulo 	 * "p->fd" is a FD for a STREAMS device, so "select()" and
243a8e07101SRui Paulo 	 * "poll()" should work on it.
244a8e07101SRui Paulo 	 */
245a8e07101SRui Paulo 	p->selectable_fd = p->fd;
246a8e07101SRui Paulo 
247a8e07101SRui Paulo 	p->read_op = pcap_read_libdlpi;
248a8e07101SRui Paulo 	p->inject_op = pcap_inject_libdlpi;
249*afdbf109SJoseph Mingrone 	p->setfilter_op = pcapint_install_bpf_program;	/* No kernel filtering */
250a8e07101SRui Paulo 	p->setdirection_op = NULL;	/* Not implemented */
251a8e07101SRui Paulo 	p->set_datalink_op = NULL;	/* Can't change data link type */
252*afdbf109SJoseph Mingrone 	p->getnonblock_op = pcapint_getnonblock_fd;
253*afdbf109SJoseph Mingrone 	p->setnonblock_op = pcapint_setnonblock_fd;
254a8e07101SRui Paulo 	p->stats_op = pcap_stats_dlpi;
255a8e07101SRui Paulo 	p->cleanup_op = pcap_cleanup_libdlpi;
256a8e07101SRui Paulo 
257681ed54cSXin LI 	return (status);
258a8e07101SRui Paulo bad:
259a8e07101SRui Paulo 	pcap_cleanup_libdlpi(p);
260681ed54cSXin LI 	return (status);
261a8e07101SRui Paulo }
262a8e07101SRui Paulo 
263d1e87331SXin LI #define STRINGIFY(n)	#n
264d1e87331SXin LI 
265d1e87331SXin LI static int
dlpromiscon(pcap_t * p,bpf_u_int32 level)266d1e87331SXin LI dlpromiscon(pcap_t *p, bpf_u_int32 level)
267d1e87331SXin LI {
268681ed54cSXin LI 	struct pcap_dlpi *pd = p->priv;
269681ed54cSXin LI 	int retv;
270d1e87331SXin LI 	int err;
271d1e87331SXin LI 
272681ed54cSXin LI 	retv = dlpi_promiscon(pd->dlpi_hd, level);
273d1e87331SXin LI 	if (retv != DLPI_SUCCESS) {
274d1e87331SXin LI 		if (retv == DL_SYSERR &&
2756f9cba8fSJoseph Mingrone 		    (errno == EPERM || errno == EACCES)) {
2766f9cba8fSJoseph Mingrone 			if (level == DL_PROMISC_PHYS) {
2776f9cba8fSJoseph Mingrone 				err = PCAP_ERROR_PROMISC_PERM_DENIED;
2786f9cba8fSJoseph Mingrone 				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2796f9cba8fSJoseph Mingrone 				    "Attempt to set promiscuous mode failed with %s - root privilege may be required",
2806f9cba8fSJoseph Mingrone 				    (errno == EPERM) ? "EPERM" : "EACCES");
2816f9cba8fSJoseph Mingrone 			} else {
282d1e87331SXin LI 				err = PCAP_ERROR_PERM_DENIED;
2836f9cba8fSJoseph Mingrone 				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
2846f9cba8fSJoseph Mingrone 				    "Attempt to set %s mode failed with %s - root privilege may be required",
2856f9cba8fSJoseph Mingrone 				    (level == DL_PROMISC_MULTI) ? "multicast" : "SAP promiscuous",
2866f9cba8fSJoseph Mingrone 				    (errno == EPERM) ? "EPERM" : "EACCES");
2876f9cba8fSJoseph Mingrone 			}
2886f9cba8fSJoseph Mingrone 		} else {
289d1e87331SXin LI 			err = PCAP_ERROR;
2906f9cba8fSJoseph Mingrone 			pcap_libdlpi_err(p->opt.device,
2916f9cba8fSJoseph Mingrone 			    "dlpi_promiscon" STRINGIFY(level),
292d1e87331SXin LI 			    retv, p->errbuf);
2936f9cba8fSJoseph Mingrone 		}
294d1e87331SXin LI 		return (err);
295d1e87331SXin LI 	}
296d1e87331SXin LI 	return (0);
297d1e87331SXin LI }
298d1e87331SXin LI 
299a8e07101SRui Paulo /*
300ada6f083SXin LI  * Presumably everything returned by dlpi_walk() is a DLPI device,
301ada6f083SXin LI  * so there's no work to be done here to check whether name refers
302ada6f083SXin LI  * to a DLPI device.
303ada6f083SXin LI  */
304ada6f083SXin LI static int
is_dlpi_interface(const char * name _U_)305ada6f083SXin LI is_dlpi_interface(const char *name _U_)
306ada6f083SXin LI {
307ada6f083SXin LI 	return (1);
308ada6f083SXin LI }
309ada6f083SXin LI 
310b00ab754SHans Petter Selasky static int
get_if_flags(const char * name _U_,bpf_u_int32 * flags _U_,char * errbuf _U_)311b00ab754SHans Petter Selasky get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
312b00ab754SHans Petter Selasky {
313b00ab754SHans Petter Selasky 	/*
314b00ab754SHans Petter Selasky 	 * Nothing we can do other than mark loopback devices as "the
315b00ab754SHans Petter Selasky 	 * connected/disconnected status doesn't apply".
316b00ab754SHans Petter Selasky 	 *
317b00ab754SHans Petter Selasky 	 * XXX - on Solaris, can we do what the dladm command does,
318b00ab754SHans Petter Selasky 	 * i.e. get a connected/disconnected indication from a kstat?
319b00ab754SHans Petter Selasky 	 * (Note that you can also get the link speed, and possibly
320b00ab754SHans Petter Selasky 	 * other information, from a kstat as well.)
321b00ab754SHans Petter Selasky 	 */
322b00ab754SHans Petter Selasky 	if (*flags & PCAP_IF_LOOPBACK) {
323b00ab754SHans Petter Selasky 		/*
324b00ab754SHans Petter Selasky 		 * Loopback devices aren't wireless, and "connected"/
325b00ab754SHans Petter Selasky 		 * "disconnected" doesn't apply to them.
326b00ab754SHans Petter Selasky 		 */
327b00ab754SHans Petter Selasky 		*flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
328b00ab754SHans Petter Selasky 		return (0);
329b00ab754SHans Petter Selasky 	}
330b00ab754SHans Petter Selasky 	return (0);
331b00ab754SHans Petter Selasky }
332b00ab754SHans Petter Selasky 
333ada6f083SXin LI /*
334a8e07101SRui Paulo  * In Solaris, the "standard" mechanism" i.e SIOCGLIFCONF will only find
335a8e07101SRui Paulo  * network links that are plumbed and are up. dlpi_walk(3DLPI) will find
336a8e07101SRui Paulo  * additional network links present in the system.
337a8e07101SRui Paulo  */
338a8e07101SRui Paulo int
pcapint_platform_finddevs(pcap_if_list_t * devlistp,char * errbuf)339*afdbf109SJoseph Mingrone pcapint_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
340a8e07101SRui Paulo {
341a8e07101SRui Paulo 	int retv = 0;
342a8e07101SRui Paulo 
343a8e07101SRui Paulo 	linknamelist_t	*entry, *next;
344a8e07101SRui Paulo 	linkwalk_t	lw = {NULL, 0};
345a8e07101SRui Paulo 	int		save_errno;
346a8e07101SRui Paulo 
347ada6f083SXin LI 	/*
348ada6f083SXin LI 	 * Get the list of regular interfaces first.
349ada6f083SXin LI 	 */
350*afdbf109SJoseph Mingrone 	if (pcapint_findalldevs_interfaces(devlistp, errbuf,
351b00ab754SHans Petter Selasky 	    is_dlpi_interface, get_if_flags) == -1)
352ada6f083SXin LI 		return (-1);	/* failure */
353ada6f083SXin LI 
354a8e07101SRui Paulo 	/* dlpi_walk() for loopback will be added here. */
355a8e07101SRui Paulo 
356b00ab754SHans Petter Selasky 	/*
357b00ab754SHans Petter Selasky 	 * Find all DLPI devices in the current zone.
358b00ab754SHans Petter Selasky 	 *
359*afdbf109SJoseph Mingrone 	 * XXX - will pcapint_findalldevs_interfaces() find any devices
360b00ab754SHans Petter Selasky 	 * outside the current zone?  If not, the only reason to call
361b00ab754SHans Petter Selasky 	 * it would be to get the interface addresses.
362b00ab754SHans Petter Selasky 	 */
363a8e07101SRui Paulo 	dlpi_walk(list_interfaces, &lw, 0);
364a8e07101SRui Paulo 
365a8e07101SRui Paulo 	if (lw.lw_err != 0) {
366*afdbf109SJoseph Mingrone 		pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
367b00ab754SHans Petter Selasky 		    lw.lw_err, "dlpi_walk");
368a8e07101SRui Paulo 		retv = -1;
369a8e07101SRui Paulo 		goto done;
370a8e07101SRui Paulo 	}
371a8e07101SRui Paulo 
372a8e07101SRui Paulo 	/* Add linkname if it does not exist on the list. */
373a8e07101SRui Paulo 	for (entry = lw.lw_list; entry != NULL; entry = entry->lnl_next) {
374b00ab754SHans Petter Selasky 		/*
375b00ab754SHans Petter Selasky 		 * If it isn't already in the list of devices, try to
376b00ab754SHans Petter Selasky 		 * add it.
377b00ab754SHans Petter Selasky 		 */
378*afdbf109SJoseph Mingrone 		if (pcapint_find_or_add_dev(devlistp, entry->linkname, 0, get_if_flags,
379b00ab754SHans Petter Selasky 		    NULL, errbuf) == NULL)
380a8e07101SRui Paulo 			retv = -1;
381a8e07101SRui Paulo 	}
382a8e07101SRui Paulo done:
383a8e07101SRui Paulo 	save_errno = errno;
384a8e07101SRui Paulo 	for (entry = lw.lw_list; entry != NULL; entry = next) {
385a8e07101SRui Paulo 		next = entry->lnl_next;
386a8e07101SRui Paulo 		free(entry);
387a8e07101SRui Paulo 	}
388a8e07101SRui Paulo 	errno = save_errno;
389a8e07101SRui Paulo 
390a8e07101SRui Paulo 	return (retv);
391a8e07101SRui Paulo }
392a8e07101SRui Paulo 
393a8e07101SRui Paulo /*
394a8e07101SRui Paulo  * Read data received on DLPI handle. Returns -2 if told to terminate, else
395a8e07101SRui Paulo  * returns the number of packets read.
396a8e07101SRui Paulo  */
397a8e07101SRui Paulo static int
pcap_read_libdlpi(pcap_t * p,int count,pcap_handler callback,u_char * user)398a8e07101SRui Paulo pcap_read_libdlpi(pcap_t *p, int count, pcap_handler callback, u_char *user)
399a8e07101SRui Paulo {
400681ed54cSXin LI 	struct pcap_dlpi *pd = p->priv;
401a8e07101SRui Paulo 	int len;
402a8e07101SRui Paulo 	u_char *bufp;
403a8e07101SRui Paulo 	size_t msglen;
404a8e07101SRui Paulo 	int retv;
405a8e07101SRui Paulo 
406a8e07101SRui Paulo 	len = p->cc;
407a8e07101SRui Paulo 	if (len != 0) {
408a8e07101SRui Paulo 		bufp = p->bp;
409a8e07101SRui Paulo 		goto process_pkts;
410a8e07101SRui Paulo 	}
411a8e07101SRui Paulo 	do {
412a8e07101SRui Paulo 		/* Has "pcap_breakloop()" been called? */
413a8e07101SRui Paulo 		if (p->break_loop) {
414a8e07101SRui Paulo 			/*
415a8e07101SRui Paulo 			 * Yes - clear the flag that indicates that it has,
416a8e07101SRui Paulo 			 * and return -2 to indicate that we were told to
417a8e07101SRui Paulo 			 * break out of the loop.
418a8e07101SRui Paulo 			 */
419a8e07101SRui Paulo 			p->break_loop = 0;
420a8e07101SRui Paulo 			return (-2);
421a8e07101SRui Paulo 		}
422a8e07101SRui Paulo 
423a8e07101SRui Paulo 		msglen = p->bufsize;
424ada6f083SXin LI 		bufp = (u_char *)p->buffer + p->offset;
425a8e07101SRui Paulo 
426681ed54cSXin LI 		retv = dlpi_recv(pd->dlpi_hd, NULL, NULL, bufp,
427a8e07101SRui Paulo 		    &msglen, -1, NULL);
428a8e07101SRui Paulo 		if (retv != DLPI_SUCCESS) {
429a8e07101SRui Paulo 			/*
430a8e07101SRui Paulo 			 * This is most likely a call to terminate out of the
431a8e07101SRui Paulo 			 * loop. So, do not return an error message, instead
432a8e07101SRui Paulo 			 * check if "pcap_breakloop()" has been called above.
433a8e07101SRui Paulo 			 */
434a8e07101SRui Paulo 			if (retv == DL_SYSERR && errno == EINTR) {
435a8e07101SRui Paulo 				len = 0;
436a8e07101SRui Paulo 				continue;
437a8e07101SRui Paulo 			}
438681ed54cSXin LI 			pcap_libdlpi_err(dlpi_linkname(pd->dlpi_hd),
439a8e07101SRui Paulo 			    "dlpi_recv", retv, p->errbuf);
440a8e07101SRui Paulo 			return (-1);
441a8e07101SRui Paulo 		}
442a8e07101SRui Paulo 		len = msglen;
443a8e07101SRui Paulo 	} while (len == 0);
444a8e07101SRui Paulo 
445a8e07101SRui Paulo process_pkts:
446a8e07101SRui Paulo 	return (pcap_process_pkts(p, callback, user, count, bufp, len));
447a8e07101SRui Paulo }
448a8e07101SRui Paulo 
449a8e07101SRui Paulo static int
pcap_inject_libdlpi(pcap_t * p,const void * buf,int size)4506f9cba8fSJoseph Mingrone pcap_inject_libdlpi(pcap_t *p, const void *buf, int size)
451a8e07101SRui Paulo {
452681ed54cSXin LI 	struct pcap_dlpi *pd = p->priv;
453a8e07101SRui Paulo 	int retv;
454a8e07101SRui Paulo 
455681ed54cSXin LI 	retv = dlpi_send(pd->dlpi_hd, NULL, 0, buf, size, NULL);
456a8e07101SRui Paulo 	if (retv != DLPI_SUCCESS) {
457681ed54cSXin LI 		pcap_libdlpi_err(dlpi_linkname(pd->dlpi_hd), "dlpi_send", retv,
458a8e07101SRui Paulo 		    p->errbuf);
459a8e07101SRui Paulo 		return (-1);
460a8e07101SRui Paulo 	}
461a8e07101SRui Paulo 	/*
462a8e07101SRui Paulo 	 * dlpi_send(3DLPI) does not provide a way to return the number of
463a8e07101SRui Paulo 	 * bytes sent on the wire. Based on the fact that DLPI_SUCCESS was
464a8e07101SRui Paulo 	 * returned we are assuming 'size' bytes were sent.
465a8e07101SRui Paulo 	 */
466a8e07101SRui Paulo 	return (size);
467a8e07101SRui Paulo }
468a8e07101SRui Paulo 
469a8e07101SRui Paulo /*
470a8e07101SRui Paulo  * Close dlpi handle.
471a8e07101SRui Paulo  */
472a8e07101SRui Paulo static void
pcap_cleanup_libdlpi(pcap_t * p)473a8e07101SRui Paulo pcap_cleanup_libdlpi(pcap_t *p)
474a8e07101SRui Paulo {
475681ed54cSXin LI 	struct pcap_dlpi *pd = p->priv;
476681ed54cSXin LI 
477681ed54cSXin LI 	if (pd->dlpi_hd != NULL) {
478681ed54cSXin LI 		dlpi_close(pd->dlpi_hd);
479681ed54cSXin LI 		pd->dlpi_hd = NULL;
480a8e07101SRui Paulo 		p->fd = -1;
481a8e07101SRui Paulo 	}
482*afdbf109SJoseph Mingrone 	pcapint_cleanup_live_common(p);
483a8e07101SRui Paulo }
484a8e07101SRui Paulo 
485a8e07101SRui Paulo /*
486a8e07101SRui Paulo  * Write error message to buffer.
487a8e07101SRui Paulo  */
488a8e07101SRui Paulo static void
pcap_libdlpi_err(const char * linkname,const char * func,int err,char * errbuf)489a8e07101SRui Paulo pcap_libdlpi_err(const char *linkname, const char *func, int err, char *errbuf)
490a8e07101SRui Paulo {
4916f9cba8fSJoseph Mingrone 	snprintf(errbuf, PCAP_ERRBUF_SIZE, "libpcap: %s failed on %s: %s",
492a8e07101SRui Paulo 	    func, linkname, dlpi_strerror(err));
493a8e07101SRui Paulo }
494a8e07101SRui Paulo 
495a8e07101SRui Paulo pcap_t *
pcapint_create_interface(const char * device _U_,char * ebuf)496*afdbf109SJoseph Mingrone pcapint_create_interface(const char *device _U_, char *ebuf)
497a8e07101SRui Paulo {
498a8e07101SRui Paulo 	pcap_t *p;
499a8e07101SRui Paulo 
5006f9cba8fSJoseph Mingrone 	p = PCAP_CREATE_COMMON(ebuf, struct pcap_dlpi);
501a8e07101SRui Paulo 	if (p == NULL)
502a8e07101SRui Paulo 		return (NULL);
503a8e07101SRui Paulo 
504a8e07101SRui Paulo 	p->activate_op = pcap_activate_libdlpi;
505a8e07101SRui Paulo 	return (p);
506a8e07101SRui Paulo }
507b00ab754SHans Petter Selasky 
508b00ab754SHans Petter Selasky /*
509b00ab754SHans Petter Selasky  * Libpcap version string.
510b00ab754SHans Petter Selasky  */
511b00ab754SHans Petter Selasky const char *
pcap_lib_version(void)512b00ab754SHans Petter Selasky pcap_lib_version(void)
513b00ab754SHans Petter Selasky {
514b00ab754SHans Petter Selasky 	return (PCAP_VERSION_STRING);
515b00ab754SHans Petter Selasky }
516