xref: /freebsd/contrib/libpcap/pcap-libdlpi.c (revision 6f9cba8f8b5efd16249633e52483ea351876b67b)
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 
27a8e07101SRui Paulo #ifdef HAVE_CONFIG_H
28b00ab754SHans Petter Selasky #include <config.h>
29a8e07101SRui Paulo #endif
30a8e07101SRui Paulo 
31a8e07101SRui Paulo #include <sys/types.h>
32a8e07101SRui Paulo #include <sys/time.h>
33a8e07101SRui Paulo #include <sys/bufmod.h>
34a8e07101SRui Paulo #include <sys/stream.h>
35a8e07101SRui Paulo #include <libdlpi.h>
36a8e07101SRui Paulo #include <errno.h>
37a8e07101SRui Paulo #include <memory.h>
38a8e07101SRui Paulo #include <stropts.h>
39a8e07101SRui Paulo #include <stdio.h>
40a8e07101SRui Paulo #include <stdlib.h>
41a8e07101SRui Paulo #include <string.h>
42a8e07101SRui Paulo 
43a8e07101SRui Paulo #include "pcap-int.h"
44a8e07101SRui Paulo #include "dlpisubs.h"
45a8e07101SRui Paulo 
46a8e07101SRui Paulo /* Forwards. */
47d1e87331SXin LI static int dlpromiscon(pcap_t *, bpf_u_int32);
48a8e07101SRui Paulo static int pcap_read_libdlpi(pcap_t *, int, pcap_handler, u_char *);
49*6f9cba8fSJoseph Mingrone static int pcap_inject_libdlpi(pcap_t *, const void *, int);
50a8e07101SRui Paulo static void pcap_libdlpi_err(const char *, const char *, int, char *);
51a0ee43a1SRui Paulo static void pcap_cleanup_libdlpi(pcap_t *);
52a8e07101SRui Paulo 
53a8e07101SRui Paulo /*
54a8e07101SRui Paulo  * list_interfaces() will list all the network links that are
55a8e07101SRui Paulo  * available on a system.
56a8e07101SRui Paulo  */
57a8e07101SRui Paulo static boolean_t list_interfaces(const char *, void *);
58a8e07101SRui Paulo 
59a8e07101SRui Paulo typedef struct linknamelist {
60a8e07101SRui Paulo 	char	linkname[DLPI_LINKNAME_MAX];
61a8e07101SRui Paulo 	struct linknamelist *lnl_next;
62a8e07101SRui Paulo } linknamelist_t;
63a8e07101SRui Paulo 
64a8e07101SRui Paulo typedef struct linkwalk {
65a8e07101SRui Paulo 	linknamelist_t	*lw_list;
66a8e07101SRui Paulo 	int		lw_err;
67a8e07101SRui Paulo } linkwalk_t;
68a8e07101SRui Paulo 
69a8e07101SRui Paulo /*
70a8e07101SRui Paulo  * The caller of this function should free the memory allocated
71a8e07101SRui Paulo  * for each linknamelist_t "entry" allocated.
72a8e07101SRui Paulo  */
73a8e07101SRui Paulo static boolean_t
74a8e07101SRui Paulo list_interfaces(const char *linkname, void *arg)
75a8e07101SRui Paulo {
76a8e07101SRui Paulo 	linkwalk_t	*lwp = arg;
77a8e07101SRui Paulo 	linknamelist_t	*entry;
78a8e07101SRui Paulo 
79a8e07101SRui Paulo 	if ((entry = calloc(1, sizeof(linknamelist_t))) == NULL) {
80a8e07101SRui Paulo 		lwp->lw_err = ENOMEM;
81a8e07101SRui Paulo 		return (B_TRUE);
82a8e07101SRui Paulo 	}
8357e22627SCy Schubert 	(void) pcap_strlcpy(entry->linkname, linkname, DLPI_LINKNAME_MAX);
84a8e07101SRui Paulo 
85a8e07101SRui Paulo 	if (lwp->lw_list == NULL) {
86a8e07101SRui Paulo 		lwp->lw_list = entry;
87a8e07101SRui Paulo 	} else {
88a8e07101SRui Paulo 		entry->lnl_next = lwp->lw_list;
89a8e07101SRui Paulo 		lwp->lw_list = entry;
90a8e07101SRui Paulo 	}
91a8e07101SRui Paulo 
92a8e07101SRui Paulo 	return (B_FALSE);
93a8e07101SRui Paulo }
94a8e07101SRui Paulo 
95a8e07101SRui Paulo static int
96a8e07101SRui Paulo pcap_activate_libdlpi(pcap_t *p)
97a8e07101SRui Paulo {
98681ed54cSXin LI 	struct pcap_dlpi *pd = p->priv;
99681ed54cSXin LI 	int status = 0;
100a8e07101SRui Paulo 	int retv;
101a8e07101SRui Paulo 	dlpi_handle_t dh;
102a8e07101SRui Paulo 	dlpi_info_t dlinfo;
103a8e07101SRui Paulo 
104a8e07101SRui Paulo 	/*
105a8e07101SRui Paulo 	 * Enable Solaris raw and passive DLPI extensions;
106a8e07101SRui Paulo 	 * dlpi_open() will not fail if the underlying link does not support
107a8e07101SRui Paulo 	 * passive mode. See dlpi(7P) for details.
108a8e07101SRui Paulo 	 */
109ada6f083SXin LI 	retv = dlpi_open(p->opt.device, &dh, DLPI_RAW|DLPI_PASSIVE);
110a8e07101SRui Paulo 	if (retv != DLPI_SUCCESS) {
111*6f9cba8fSJoseph Mingrone 		if (retv == DLPI_ELINKNAMEINVAL || retv == DLPI_ENOLINK) {
112*6f9cba8fSJoseph Mingrone 			/*
113*6f9cba8fSJoseph Mingrone 			 * There's nothing more to say, so clear the
114*6f9cba8fSJoseph Mingrone 			 * error message.
115*6f9cba8fSJoseph Mingrone 			 */
116681ed54cSXin LI 			status = PCAP_ERROR_NO_SUCH_DEVICE;
117*6f9cba8fSJoseph Mingrone 			p->errbuf[0] = '\0';
118*6f9cba8fSJoseph Mingrone 		} else if (retv == DL_SYSERR &&
119*6f9cba8fSJoseph Mingrone 		    (errno == EPERM || errno == EACCES)) {
120681ed54cSXin LI 			status = PCAP_ERROR_PERM_DENIED;
121*6f9cba8fSJoseph Mingrone 			snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
122*6f9cba8fSJoseph Mingrone 			    "Attempt to open DLPI device failed with %s - root privilege may be required",
123*6f9cba8fSJoseph Mingrone 			    (errno == EPERM) ? "EPERM" : "EACCES");
124*6f9cba8fSJoseph Mingrone 		} else {
125681ed54cSXin LI 			status = PCAP_ERROR;
126ada6f083SXin LI 			pcap_libdlpi_err(p->opt.device, "dlpi_open", retv,
127a8e07101SRui Paulo 			    p->errbuf);
128*6f9cba8fSJoseph Mingrone 		}
129681ed54cSXin LI 		return (status);
130a8e07101SRui Paulo 	}
131681ed54cSXin LI 	pd->dlpi_hd = dh;
132a8e07101SRui Paulo 
133a8e07101SRui Paulo 	if (p->opt.rfmon) {
134a8e07101SRui Paulo 		/*
135a8e07101SRui Paulo 		 * This device exists, but we don't support monitor mode
136a8e07101SRui Paulo 		 * any platforms that support DLPI.
137a8e07101SRui Paulo 		 */
138681ed54cSXin LI 		status = PCAP_ERROR_RFMON_NOTSUP;
139a8e07101SRui Paulo 		goto bad;
140a8e07101SRui Paulo 	}
141a8e07101SRui Paulo 
142a8e07101SRui Paulo 	/* Bind with DLPI_ANY_SAP. */
143681ed54cSXin LI 	if ((retv = dlpi_bind(pd->dlpi_hd, DLPI_ANY_SAP, 0)) != DLPI_SUCCESS) {
144681ed54cSXin LI 		status = PCAP_ERROR;
145ada6f083SXin LI 		pcap_libdlpi_err(p->opt.device, "dlpi_bind", retv, p->errbuf);
146a8e07101SRui Paulo 		goto bad;
147a8e07101SRui Paulo 	}
148a8e07101SRui Paulo 
149b00ab754SHans Petter Selasky 	/*
150b00ab754SHans Petter Selasky 	 * Turn a negative snapshot value (invalid), a snapshot value of
151b00ab754SHans Petter Selasky 	 * 0 (unspecified), or a value bigger than the normal maximum
152b00ab754SHans Petter Selasky 	 * value, into the maximum allowed value.
153b00ab754SHans Petter Selasky 	 *
154b00ab754SHans Petter Selasky 	 * If some application really *needs* a bigger snapshot
155b00ab754SHans Petter Selasky 	 * length, we should just increase MAXIMUM_SNAPLEN.
156b00ab754SHans Petter Selasky 	 */
157b00ab754SHans Petter Selasky 	if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
158b00ab754SHans Petter Selasky 		p->snapshot = MAXIMUM_SNAPLEN;
159b00ab754SHans Petter Selasky 
160a8e07101SRui Paulo 	/* Enable promiscuous mode. */
161a8e07101SRui Paulo 	if (p->opt.promisc) {
162681ed54cSXin LI 		retv = dlpromiscon(p, DL_PROMISC_PHYS);
163681ed54cSXin LI 		if (retv < 0) {
164d1e87331SXin LI 			/*
165d1e87331SXin LI 			 * "You don't have permission to capture on
166d1e87331SXin LI 			 * this device" and "you don't have permission
167d1e87331SXin LI 			 * to capture in promiscuous mode on this
168d1e87331SXin LI 			 * device" are different; let the user know,
169d1e87331SXin LI 			 * so if they can't get permission to
170d1e87331SXin LI 			 * capture in promiscuous mode, they can at
171d1e87331SXin LI 			 * least try to capture in non-promiscuous
172d1e87331SXin LI 			 * mode.
173d1e87331SXin LI 			 *
174d1e87331SXin LI 			 * XXX - you might have to capture in
175d1e87331SXin LI 			 * promiscuous mode to see outgoing packets.
176d1e87331SXin LI 			 */
177681ed54cSXin LI 			if (retv == PCAP_ERROR_PERM_DENIED)
178681ed54cSXin LI 				status = PCAP_ERROR_PROMISC_PERM_DENIED;
179681ed54cSXin LI 			else
180681ed54cSXin LI 				status = retv;
181a8e07101SRui Paulo 			goto bad;
182a8e07101SRui Paulo 		}
183a8e07101SRui Paulo 	} else {
184a8e07101SRui Paulo 		/* Try to enable multicast. */
185681ed54cSXin LI 		retv = dlpromiscon(p, DL_PROMISC_MULTI);
186681ed54cSXin LI 		if (retv < 0) {
187681ed54cSXin LI 			status = retv;
188a8e07101SRui Paulo 			goto bad;
189a8e07101SRui Paulo 		}
190681ed54cSXin LI 	}
191a8e07101SRui Paulo 
192a8e07101SRui Paulo 	/* Try to enable SAP promiscuity. */
193681ed54cSXin LI 	retv = dlpromiscon(p, DL_PROMISC_SAP);
194681ed54cSXin LI 	if (retv < 0) {
195d1e87331SXin LI 		/*
196d1e87331SXin LI 		 * Not fatal, since the DL_PROMISC_PHYS mode worked.
197d1e87331SXin LI 		 * Report it as a warning, however.
198d1e87331SXin LI 		 */
199d1e87331SXin LI 		if (p->opt.promisc)
200681ed54cSXin LI 			status = PCAP_WARNING;
201681ed54cSXin LI 		else {
202681ed54cSXin LI 			status = retv;
203a8e07101SRui Paulo 			goto bad;
204a8e07101SRui Paulo 		}
205681ed54cSXin LI 	}
206a8e07101SRui Paulo 
207a8e07101SRui Paulo 	/* Determine link type.  */
208681ed54cSXin LI 	if ((retv = dlpi_info(pd->dlpi_hd, &dlinfo, 0)) != DLPI_SUCCESS) {
209681ed54cSXin LI 		status = PCAP_ERROR;
210ada6f083SXin LI 		pcap_libdlpi_err(p->opt.device, "dlpi_info", retv, p->errbuf);
211a8e07101SRui Paulo 		goto bad;
212a8e07101SRui Paulo 	}
213a8e07101SRui Paulo 
214681ed54cSXin LI 	if (pcap_process_mactype(p, dlinfo.di_mactype) != 0) {
215681ed54cSXin LI 		status = PCAP_ERROR;
216a8e07101SRui Paulo 		goto bad;
217681ed54cSXin LI 	}
218a8e07101SRui Paulo 
219681ed54cSXin LI 	p->fd = dlpi_fd(pd->dlpi_hd);
220a8e07101SRui Paulo 
221a8e07101SRui Paulo 	/* Push and configure bufmod. */
222681ed54cSXin LI 	if (pcap_conf_bufmod(p, p->snapshot) != 0) {
223681ed54cSXin LI 		status = PCAP_ERROR;
224a8e07101SRui Paulo 		goto bad;
225681ed54cSXin LI 	}
226a8e07101SRui Paulo 
227a8e07101SRui Paulo 	/*
228a8e07101SRui Paulo 	 * Flush the read side.
229a8e07101SRui Paulo 	 */
230a8e07101SRui Paulo 	if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
231681ed54cSXin LI 		status = PCAP_ERROR;
232b00ab754SHans Petter Selasky 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
233b00ab754SHans Petter Selasky 		    errno, "FLUSHR");
234a8e07101SRui Paulo 		goto bad;
235a8e07101SRui Paulo 	}
236a8e07101SRui Paulo 
237a8e07101SRui Paulo 	/* Allocate data buffer. */
238681ed54cSXin LI 	if (pcap_alloc_databuf(p) != 0) {
239681ed54cSXin LI 		status = PCAP_ERROR;
240a8e07101SRui Paulo 		goto bad;
241681ed54cSXin LI 	}
242a8e07101SRui Paulo 
243a8e07101SRui Paulo 	/*
244a8e07101SRui Paulo 	 * "p->fd" is a FD for a STREAMS device, so "select()" and
245a8e07101SRui Paulo 	 * "poll()" should work on it.
246a8e07101SRui Paulo 	 */
247a8e07101SRui Paulo 	p->selectable_fd = p->fd;
248a8e07101SRui Paulo 
249a8e07101SRui Paulo 	p->read_op = pcap_read_libdlpi;
250a8e07101SRui Paulo 	p->inject_op = pcap_inject_libdlpi;
251a8e07101SRui Paulo 	p->setfilter_op = install_bpf_program;	/* No kernel filtering */
252a8e07101SRui Paulo 	p->setdirection_op = NULL;	/* Not implemented */
253a8e07101SRui Paulo 	p->set_datalink_op = NULL;	/* Can't change data link type */
254a8e07101SRui Paulo 	p->getnonblock_op = pcap_getnonblock_fd;
255a8e07101SRui Paulo 	p->setnonblock_op = pcap_setnonblock_fd;
256a8e07101SRui Paulo 	p->stats_op = pcap_stats_dlpi;
257a8e07101SRui Paulo 	p->cleanup_op = pcap_cleanup_libdlpi;
258a8e07101SRui Paulo 
259681ed54cSXin LI 	return (status);
260a8e07101SRui Paulo bad:
261a8e07101SRui Paulo 	pcap_cleanup_libdlpi(p);
262681ed54cSXin LI 	return (status);
263a8e07101SRui Paulo }
264a8e07101SRui Paulo 
265d1e87331SXin LI #define STRINGIFY(n)	#n
266d1e87331SXin LI 
267d1e87331SXin LI static int
268d1e87331SXin LI dlpromiscon(pcap_t *p, bpf_u_int32 level)
269d1e87331SXin LI {
270681ed54cSXin LI 	struct pcap_dlpi *pd = p->priv;
271681ed54cSXin LI 	int retv;
272d1e87331SXin LI 	int err;
273d1e87331SXin LI 
274681ed54cSXin LI 	retv = dlpi_promiscon(pd->dlpi_hd, level);
275d1e87331SXin LI 	if (retv != DLPI_SUCCESS) {
276d1e87331SXin LI 		if (retv == DL_SYSERR &&
277*6f9cba8fSJoseph Mingrone 		    (errno == EPERM || errno == EACCES)) {
278*6f9cba8fSJoseph Mingrone 			if (level == DL_PROMISC_PHYS) {
279*6f9cba8fSJoseph Mingrone 				err = PCAP_ERROR_PROMISC_PERM_DENIED;
280*6f9cba8fSJoseph Mingrone 				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
281*6f9cba8fSJoseph Mingrone 				    "Attempt to set promiscuous mode failed with %s - root privilege may be required",
282*6f9cba8fSJoseph Mingrone 				    (errno == EPERM) ? "EPERM" : "EACCES");
283*6f9cba8fSJoseph Mingrone 			} else {
284d1e87331SXin LI 				err = PCAP_ERROR_PERM_DENIED;
285*6f9cba8fSJoseph Mingrone 				snprintf(p->errbuf, PCAP_ERRBUF_SIZE,
286*6f9cba8fSJoseph Mingrone 				    "Attempt to set %s mode failed with %s - root privilege may be required",
287*6f9cba8fSJoseph Mingrone 				    (level == DL_PROMISC_MULTI) ? "multicast" : "SAP promiscuous",
288*6f9cba8fSJoseph Mingrone 				    (errno == EPERM) ? "EPERM" : "EACCES");
289*6f9cba8fSJoseph Mingrone 			}
290*6f9cba8fSJoseph Mingrone 		} else {
291d1e87331SXin LI 			err = PCAP_ERROR;
292*6f9cba8fSJoseph Mingrone 			pcap_libdlpi_err(p->opt.device,
293*6f9cba8fSJoseph Mingrone 			    "dlpi_promiscon" STRINGIFY(level),
294d1e87331SXin LI 			    retv, p->errbuf);
295*6f9cba8fSJoseph Mingrone 		}
296d1e87331SXin LI 		return (err);
297d1e87331SXin LI 	}
298d1e87331SXin LI 	return (0);
299d1e87331SXin LI }
300d1e87331SXin LI 
301a8e07101SRui Paulo /*
302ada6f083SXin LI  * Presumably everything returned by dlpi_walk() is a DLPI device,
303ada6f083SXin LI  * so there's no work to be done here to check whether name refers
304ada6f083SXin LI  * to a DLPI device.
305ada6f083SXin LI  */
306ada6f083SXin LI static int
307ada6f083SXin LI is_dlpi_interface(const char *name _U_)
308ada6f083SXin LI {
309ada6f083SXin LI 	return (1);
310ada6f083SXin LI }
311ada6f083SXin LI 
312b00ab754SHans Petter Selasky static int
313b00ab754SHans Petter Selasky get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
314b00ab754SHans Petter Selasky {
315b00ab754SHans Petter Selasky 	/*
316b00ab754SHans Petter Selasky 	 * Nothing we can do other than mark loopback devices as "the
317b00ab754SHans Petter Selasky 	 * connected/disconnected status doesn't apply".
318b00ab754SHans Petter Selasky 	 *
319b00ab754SHans Petter Selasky 	 * XXX - on Solaris, can we do what the dladm command does,
320b00ab754SHans Petter Selasky 	 * i.e. get a connected/disconnected indication from a kstat?
321b00ab754SHans Petter Selasky 	 * (Note that you can also get the link speed, and possibly
322b00ab754SHans Petter Selasky 	 * other information, from a kstat as well.)
323b00ab754SHans Petter Selasky 	 */
324b00ab754SHans Petter Selasky 	if (*flags & PCAP_IF_LOOPBACK) {
325b00ab754SHans Petter Selasky 		/*
326b00ab754SHans Petter Selasky 		 * Loopback devices aren't wireless, and "connected"/
327b00ab754SHans Petter Selasky 		 * "disconnected" doesn't apply to them.
328b00ab754SHans Petter Selasky 		 */
329b00ab754SHans Petter Selasky 		*flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
330b00ab754SHans Petter Selasky 		return (0);
331b00ab754SHans Petter Selasky 	}
332b00ab754SHans Petter Selasky 	return (0);
333b00ab754SHans Petter Selasky }
334b00ab754SHans Petter Selasky 
335ada6f083SXin LI /*
336a8e07101SRui Paulo  * In Solaris, the "standard" mechanism" i.e SIOCGLIFCONF will only find
337a8e07101SRui Paulo  * network links that are plumbed and are up. dlpi_walk(3DLPI) will find
338a8e07101SRui Paulo  * additional network links present in the system.
339a8e07101SRui Paulo  */
340a8e07101SRui Paulo int
341b00ab754SHans Petter Selasky pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
342a8e07101SRui Paulo {
343a8e07101SRui Paulo 	int retv = 0;
344a8e07101SRui Paulo 
345a8e07101SRui Paulo 	linknamelist_t	*entry, *next;
346a8e07101SRui Paulo 	linkwalk_t	lw = {NULL, 0};
347a8e07101SRui Paulo 	int 		save_errno;
348a8e07101SRui Paulo 
349ada6f083SXin LI 	/*
350ada6f083SXin LI 	 * Get the list of regular interfaces first.
351ada6f083SXin LI 	 */
352b00ab754SHans Petter Selasky 	if (pcap_findalldevs_interfaces(devlistp, errbuf,
353b00ab754SHans Petter Selasky 	    is_dlpi_interface, get_if_flags) == -1)
354ada6f083SXin LI 		return (-1);	/* failure */
355ada6f083SXin LI 
356a8e07101SRui Paulo 	/* dlpi_walk() for loopback will be added here. */
357a8e07101SRui Paulo 
358b00ab754SHans Petter Selasky 	/*
359b00ab754SHans Petter Selasky 	 * Find all DLPI devices in the current zone.
360b00ab754SHans Petter Selasky 	 *
361b00ab754SHans Petter Selasky 	 * XXX - will pcap_findalldevs_interfaces() find any devices
362b00ab754SHans Petter Selasky 	 * outside the current zone?  If not, the only reason to call
363b00ab754SHans Petter Selasky 	 * it would be to get the interface addresses.
364b00ab754SHans Petter Selasky 	 */
365a8e07101SRui Paulo 	dlpi_walk(list_interfaces, &lw, 0);
366a8e07101SRui Paulo 
367a8e07101SRui Paulo 	if (lw.lw_err != 0) {
368b00ab754SHans Petter Selasky 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
369b00ab754SHans Petter Selasky 		    lw.lw_err, "dlpi_walk");
370a8e07101SRui Paulo 		retv = -1;
371a8e07101SRui Paulo 		goto done;
372a8e07101SRui Paulo 	}
373a8e07101SRui Paulo 
374a8e07101SRui Paulo 	/* Add linkname if it does not exist on the list. */
375a8e07101SRui Paulo 	for (entry = lw.lw_list; entry != NULL; entry = entry->lnl_next) {
376b00ab754SHans Petter Selasky 		/*
377b00ab754SHans Petter Selasky 		 * If it isn't already in the list of devices, try to
378b00ab754SHans Petter Selasky 		 * add it.
379b00ab754SHans Petter Selasky 		 */
380b00ab754SHans Petter Selasky 		if (find_or_add_dev(devlistp, entry->linkname, 0, get_if_flags,
381b00ab754SHans Petter Selasky 		    NULL, errbuf) == NULL)
382a8e07101SRui Paulo 			retv = -1;
383a8e07101SRui Paulo 	}
384a8e07101SRui Paulo done:
385a8e07101SRui Paulo 	save_errno = errno;
386a8e07101SRui Paulo 	for (entry = lw.lw_list; entry != NULL; entry = next) {
387a8e07101SRui Paulo 		next = entry->lnl_next;
388a8e07101SRui Paulo 		free(entry);
389a8e07101SRui Paulo 	}
390a8e07101SRui Paulo 	errno = save_errno;
391a8e07101SRui Paulo 
392a8e07101SRui Paulo 	return (retv);
393a8e07101SRui Paulo }
394a8e07101SRui Paulo 
395a8e07101SRui Paulo /*
396a8e07101SRui Paulo  * Read data received on DLPI handle. Returns -2 if told to terminate, else
397a8e07101SRui Paulo  * returns the number of packets read.
398a8e07101SRui Paulo  */
399a8e07101SRui Paulo static int
400a8e07101SRui Paulo pcap_read_libdlpi(pcap_t *p, int count, pcap_handler callback, u_char *user)
401a8e07101SRui Paulo {
402681ed54cSXin LI 	struct pcap_dlpi *pd = p->priv;
403a8e07101SRui Paulo 	int len;
404a8e07101SRui Paulo 	u_char *bufp;
405a8e07101SRui Paulo 	size_t msglen;
406a8e07101SRui Paulo 	int retv;
407a8e07101SRui Paulo 
408a8e07101SRui Paulo 	len = p->cc;
409a8e07101SRui Paulo 	if (len != 0) {
410a8e07101SRui Paulo 		bufp = p->bp;
411a8e07101SRui Paulo 		goto process_pkts;
412a8e07101SRui Paulo 	}
413a8e07101SRui Paulo 	do {
414a8e07101SRui Paulo 		/* Has "pcap_breakloop()" been called? */
415a8e07101SRui Paulo 		if (p->break_loop) {
416a8e07101SRui Paulo 			/*
417a8e07101SRui Paulo 			 * Yes - clear the flag that indicates that it has,
418a8e07101SRui Paulo 			 * and return -2 to indicate that we were told to
419a8e07101SRui Paulo 			 * break out of the loop.
420a8e07101SRui Paulo 			 */
421a8e07101SRui Paulo 			p->break_loop = 0;
422a8e07101SRui Paulo 			return (-2);
423a8e07101SRui Paulo 		}
424a8e07101SRui Paulo 
425a8e07101SRui Paulo 		msglen = p->bufsize;
426ada6f083SXin LI 		bufp = (u_char *)p->buffer + p->offset;
427a8e07101SRui Paulo 
428681ed54cSXin LI 		retv = dlpi_recv(pd->dlpi_hd, NULL, NULL, bufp,
429a8e07101SRui Paulo 		    &msglen, -1, NULL);
430a8e07101SRui Paulo 		if (retv != DLPI_SUCCESS) {
431a8e07101SRui Paulo 			/*
432a8e07101SRui Paulo 			 * This is most likely a call to terminate out of the
433a8e07101SRui Paulo 			 * loop. So, do not return an error message, instead
434a8e07101SRui Paulo 			 * check if "pcap_breakloop()" has been called above.
435a8e07101SRui Paulo 			 */
436a8e07101SRui Paulo 			if (retv == DL_SYSERR && errno == EINTR) {
437a8e07101SRui Paulo 				len = 0;
438a8e07101SRui Paulo 				continue;
439a8e07101SRui Paulo 			}
440681ed54cSXin LI 			pcap_libdlpi_err(dlpi_linkname(pd->dlpi_hd),
441a8e07101SRui Paulo 			    "dlpi_recv", retv, p->errbuf);
442a8e07101SRui Paulo 			return (-1);
443a8e07101SRui Paulo 		}
444a8e07101SRui Paulo 		len = msglen;
445a8e07101SRui Paulo 	} while (len == 0);
446a8e07101SRui Paulo 
447a8e07101SRui Paulo process_pkts:
448a8e07101SRui Paulo 	return (pcap_process_pkts(p, callback, user, count, bufp, len));
449a8e07101SRui Paulo }
450a8e07101SRui Paulo 
451a8e07101SRui Paulo static int
452*6f9cba8fSJoseph Mingrone pcap_inject_libdlpi(pcap_t *p, const void *buf, int size)
453a8e07101SRui Paulo {
454681ed54cSXin LI 	struct pcap_dlpi *pd = p->priv;
455a8e07101SRui Paulo 	int retv;
456a8e07101SRui Paulo 
457681ed54cSXin LI 	retv = dlpi_send(pd->dlpi_hd, NULL, 0, buf, size, NULL);
458a8e07101SRui Paulo 	if (retv != DLPI_SUCCESS) {
459681ed54cSXin LI 		pcap_libdlpi_err(dlpi_linkname(pd->dlpi_hd), "dlpi_send", retv,
460a8e07101SRui Paulo 		    p->errbuf);
461a8e07101SRui Paulo 		return (-1);
462a8e07101SRui Paulo 	}
463a8e07101SRui Paulo 	/*
464a8e07101SRui Paulo 	 * dlpi_send(3DLPI) does not provide a way to return the number of
465a8e07101SRui Paulo 	 * bytes sent on the wire. Based on the fact that DLPI_SUCCESS was
466a8e07101SRui Paulo 	 * returned we are assuming 'size' bytes were sent.
467a8e07101SRui Paulo 	 */
468a8e07101SRui Paulo 	return (size);
469a8e07101SRui Paulo }
470a8e07101SRui Paulo 
471a8e07101SRui Paulo /*
472a8e07101SRui Paulo  * Close dlpi handle.
473a8e07101SRui Paulo  */
474a8e07101SRui Paulo static void
475a8e07101SRui Paulo pcap_cleanup_libdlpi(pcap_t *p)
476a8e07101SRui Paulo {
477681ed54cSXin LI 	struct pcap_dlpi *pd = p->priv;
478681ed54cSXin LI 
479681ed54cSXin LI 	if (pd->dlpi_hd != NULL) {
480681ed54cSXin LI 		dlpi_close(pd->dlpi_hd);
481681ed54cSXin LI 		pd->dlpi_hd = NULL;
482a8e07101SRui Paulo 		p->fd = -1;
483a8e07101SRui Paulo 	}
484a8e07101SRui Paulo 	pcap_cleanup_live_common(p);
485a8e07101SRui Paulo }
486a8e07101SRui Paulo 
487a8e07101SRui Paulo /*
488a8e07101SRui Paulo  * Write error message to buffer.
489a8e07101SRui Paulo  */
490a8e07101SRui Paulo static void
491a8e07101SRui Paulo pcap_libdlpi_err(const char *linkname, const char *func, int err, char *errbuf)
492a8e07101SRui Paulo {
493*6f9cba8fSJoseph Mingrone 	snprintf(errbuf, PCAP_ERRBUF_SIZE, "libpcap: %s failed on %s: %s",
494a8e07101SRui Paulo 	    func, linkname, dlpi_strerror(err));
495a8e07101SRui Paulo }
496a8e07101SRui Paulo 
497a8e07101SRui Paulo pcap_t *
498ada6f083SXin LI pcap_create_interface(const char *device _U_, char *ebuf)
499a8e07101SRui Paulo {
500a8e07101SRui Paulo 	pcap_t *p;
501a8e07101SRui Paulo 
502*6f9cba8fSJoseph Mingrone 	p = PCAP_CREATE_COMMON(ebuf, struct pcap_dlpi);
503a8e07101SRui Paulo 	if (p == NULL)
504a8e07101SRui Paulo 		return (NULL);
505a8e07101SRui Paulo 
506a8e07101SRui Paulo 	p->activate_op = pcap_activate_libdlpi;
507a8e07101SRui Paulo 	return (p);
508a8e07101SRui Paulo }
509b00ab754SHans Petter Selasky 
510b00ab754SHans Petter Selasky /*
511b00ab754SHans Petter Selasky  * Libpcap version string.
512b00ab754SHans Petter Selasky  */
513b00ab754SHans Petter Selasky const char *
514b00ab754SHans Petter Selasky pcap_lib_version(void)
515b00ab754SHans Petter Selasky {
516b00ab754SHans Petter Selasky 	return (PCAP_VERSION_STRING);
517b00ab754SHans Petter Selasky }
518