xref: /freebsd/contrib/libpcap/pcap-libdlpi.c (revision b00ab7548b418624b6719ab8a2e8aaeade767a70)
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
28*b00ab754SHans 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 *);
49a8e07101SRui Paulo static int pcap_inject_libdlpi(pcap_t *, const void *, size_t);
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 	}
83a8e07101SRui Paulo 	(void) 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) {
111a8e07101SRui Paulo 		if (retv == DLPI_ELINKNAMEINVAL || retv == DLPI_ENOLINK)
112681ed54cSXin LI 			status = PCAP_ERROR_NO_SUCH_DEVICE;
113d1e87331SXin LI 		else if (retv == DL_SYSERR &&
114d1e87331SXin LI 		    (errno == EPERM || errno == EACCES))
115681ed54cSXin LI 			status = PCAP_ERROR_PERM_DENIED;
116681ed54cSXin LI 		else
117681ed54cSXin LI 			status = PCAP_ERROR;
118ada6f083SXin LI 		pcap_libdlpi_err(p->opt.device, "dlpi_open", retv,
119a8e07101SRui Paulo 		    p->errbuf);
120681ed54cSXin LI 		return (status);
121a8e07101SRui Paulo 	}
122681ed54cSXin LI 	pd->dlpi_hd = dh;
123a8e07101SRui Paulo 
124a8e07101SRui Paulo 	if (p->opt.rfmon) {
125a8e07101SRui Paulo 		/*
126a8e07101SRui Paulo 		 * This device exists, but we don't support monitor mode
127a8e07101SRui Paulo 		 * any platforms that support DLPI.
128a8e07101SRui Paulo 		 */
129681ed54cSXin LI 		status = PCAP_ERROR_RFMON_NOTSUP;
130a8e07101SRui Paulo 		goto bad;
131a8e07101SRui Paulo 	}
132a8e07101SRui Paulo 
133a8e07101SRui Paulo 	/* Bind with DLPI_ANY_SAP. */
134681ed54cSXin LI 	if ((retv = dlpi_bind(pd->dlpi_hd, DLPI_ANY_SAP, 0)) != DLPI_SUCCESS) {
135681ed54cSXin LI 		status = PCAP_ERROR;
136ada6f083SXin LI 		pcap_libdlpi_err(p->opt.device, "dlpi_bind", retv, p->errbuf);
137a8e07101SRui Paulo 		goto bad;
138a8e07101SRui Paulo 	}
139a8e07101SRui Paulo 
140*b00ab754SHans Petter Selasky 	/*
141*b00ab754SHans Petter Selasky 	 * Turn a negative snapshot value (invalid), a snapshot value of
142*b00ab754SHans Petter Selasky 	 * 0 (unspecified), or a value bigger than the normal maximum
143*b00ab754SHans Petter Selasky 	 * value, into the maximum allowed value.
144*b00ab754SHans Petter Selasky 	 *
145*b00ab754SHans Petter Selasky 	 * If some application really *needs* a bigger snapshot
146*b00ab754SHans Petter Selasky 	 * length, we should just increase MAXIMUM_SNAPLEN.
147*b00ab754SHans Petter Selasky 	 */
148*b00ab754SHans Petter Selasky 	if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
149*b00ab754SHans Petter Selasky 		p->snapshot = MAXIMUM_SNAPLEN;
150*b00ab754SHans Petter Selasky 
151a8e07101SRui Paulo 	/* Enable promiscuous mode. */
152a8e07101SRui Paulo 	if (p->opt.promisc) {
153681ed54cSXin LI 		retv = dlpromiscon(p, DL_PROMISC_PHYS);
154681ed54cSXin LI 		if (retv < 0) {
155d1e87331SXin LI 			/*
156d1e87331SXin LI 			 * "You don't have permission to capture on
157d1e87331SXin LI 			 * this device" and "you don't have permission
158d1e87331SXin LI 			 * to capture in promiscuous mode on this
159d1e87331SXin LI 			 * device" are different; let the user know,
160d1e87331SXin LI 			 * so if they can't get permission to
161d1e87331SXin LI 			 * capture in promiscuous mode, they can at
162d1e87331SXin LI 			 * least try to capture in non-promiscuous
163d1e87331SXin LI 			 * mode.
164d1e87331SXin LI 			 *
165d1e87331SXin LI 			 * XXX - you might have to capture in
166d1e87331SXin LI 			 * promiscuous mode to see outgoing packets.
167d1e87331SXin LI 			 */
168681ed54cSXin LI 			if (retv == PCAP_ERROR_PERM_DENIED)
169681ed54cSXin LI 				status = PCAP_ERROR_PROMISC_PERM_DENIED;
170681ed54cSXin LI 			else
171681ed54cSXin LI 				status = retv;
172a8e07101SRui Paulo 			goto bad;
173a8e07101SRui Paulo 		}
174a8e07101SRui Paulo 	} else {
175a8e07101SRui Paulo 		/* Try to enable multicast. */
176681ed54cSXin LI 		retv = dlpromiscon(p, DL_PROMISC_MULTI);
177681ed54cSXin LI 		if (retv < 0) {
178681ed54cSXin LI 			status = retv;
179a8e07101SRui Paulo 			goto bad;
180a8e07101SRui Paulo 		}
181681ed54cSXin LI 	}
182a8e07101SRui Paulo 
183a8e07101SRui Paulo 	/* Try to enable SAP promiscuity. */
184681ed54cSXin LI 	retv = dlpromiscon(p, DL_PROMISC_SAP);
185681ed54cSXin LI 	if (retv < 0) {
186d1e87331SXin LI 		/*
187d1e87331SXin LI 		 * Not fatal, since the DL_PROMISC_PHYS mode worked.
188d1e87331SXin LI 		 * Report it as a warning, however.
189d1e87331SXin LI 		 */
190d1e87331SXin LI 		if (p->opt.promisc)
191681ed54cSXin LI 			status = PCAP_WARNING;
192681ed54cSXin LI 		else {
193681ed54cSXin LI 			status = retv;
194a8e07101SRui Paulo 			goto bad;
195a8e07101SRui Paulo 		}
196681ed54cSXin LI 	}
197a8e07101SRui Paulo 
198a8e07101SRui Paulo 	/* Determine link type.  */
199681ed54cSXin LI 	if ((retv = dlpi_info(pd->dlpi_hd, &dlinfo, 0)) != DLPI_SUCCESS) {
200681ed54cSXin LI 		status = PCAP_ERROR;
201ada6f083SXin LI 		pcap_libdlpi_err(p->opt.device, "dlpi_info", retv, p->errbuf);
202a8e07101SRui Paulo 		goto bad;
203a8e07101SRui Paulo 	}
204a8e07101SRui Paulo 
205681ed54cSXin LI 	if (pcap_process_mactype(p, dlinfo.di_mactype) != 0) {
206681ed54cSXin LI 		status = PCAP_ERROR;
207a8e07101SRui Paulo 		goto bad;
208681ed54cSXin LI 	}
209a8e07101SRui Paulo 
210681ed54cSXin LI 	p->fd = dlpi_fd(pd->dlpi_hd);
211a8e07101SRui Paulo 
212a8e07101SRui Paulo 	/* Push and configure bufmod. */
213681ed54cSXin LI 	if (pcap_conf_bufmod(p, p->snapshot) != 0) {
214681ed54cSXin LI 		status = PCAP_ERROR;
215a8e07101SRui Paulo 		goto bad;
216681ed54cSXin LI 	}
217a8e07101SRui Paulo 
218a8e07101SRui Paulo 	/*
219a8e07101SRui Paulo 	 * Flush the read side.
220a8e07101SRui Paulo 	 */
221a8e07101SRui Paulo 	if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
222681ed54cSXin LI 		status = PCAP_ERROR;
223*b00ab754SHans Petter Selasky 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
224*b00ab754SHans Petter Selasky 		    errno, "FLUSHR");
225a8e07101SRui Paulo 		goto bad;
226a8e07101SRui Paulo 	}
227a8e07101SRui Paulo 
228a8e07101SRui Paulo 	/* Allocate data buffer. */
229681ed54cSXin LI 	if (pcap_alloc_databuf(p) != 0) {
230681ed54cSXin LI 		status = PCAP_ERROR;
231a8e07101SRui Paulo 		goto bad;
232681ed54cSXin LI 	}
233a8e07101SRui Paulo 
234a8e07101SRui Paulo 	/*
235a8e07101SRui Paulo 	 * "p->fd" is a FD for a STREAMS device, so "select()" and
236a8e07101SRui Paulo 	 * "poll()" should work on it.
237a8e07101SRui Paulo 	 */
238a8e07101SRui Paulo 	p->selectable_fd = p->fd;
239a8e07101SRui Paulo 
240a8e07101SRui Paulo 	p->read_op = pcap_read_libdlpi;
241a8e07101SRui Paulo 	p->inject_op = pcap_inject_libdlpi;
242a8e07101SRui Paulo 	p->setfilter_op = install_bpf_program;	/* No kernel filtering */
243a8e07101SRui Paulo 	p->setdirection_op = NULL;	/* Not implemented */
244a8e07101SRui Paulo 	p->set_datalink_op = NULL;	/* Can't change data link type */
245a8e07101SRui Paulo 	p->getnonblock_op = pcap_getnonblock_fd;
246a8e07101SRui Paulo 	p->setnonblock_op = pcap_setnonblock_fd;
247a8e07101SRui Paulo 	p->stats_op = pcap_stats_dlpi;
248a8e07101SRui Paulo 	p->cleanup_op = pcap_cleanup_libdlpi;
249a8e07101SRui Paulo 
250681ed54cSXin LI 	return (status);
251a8e07101SRui Paulo bad:
252a8e07101SRui Paulo 	pcap_cleanup_libdlpi(p);
253681ed54cSXin LI 	return (status);
254a8e07101SRui Paulo }
255a8e07101SRui Paulo 
256d1e87331SXin LI #define STRINGIFY(n)	#n
257d1e87331SXin LI 
258d1e87331SXin LI static int
259d1e87331SXin LI dlpromiscon(pcap_t *p, bpf_u_int32 level)
260d1e87331SXin LI {
261681ed54cSXin LI 	struct pcap_dlpi *pd = p->priv;
262681ed54cSXin LI 	int retv;
263d1e87331SXin LI 	int err;
264d1e87331SXin LI 
265681ed54cSXin LI 	retv = dlpi_promiscon(pd->dlpi_hd, level);
266d1e87331SXin LI 	if (retv != DLPI_SUCCESS) {
267d1e87331SXin LI 		if (retv == DL_SYSERR &&
268d1e87331SXin LI 		    (errno == EPERM || errno == EACCES))
269d1e87331SXin LI 			err = PCAP_ERROR_PERM_DENIED;
270d1e87331SXin LI 		else
271d1e87331SXin LI 			err = PCAP_ERROR;
272ada6f083SXin LI 		pcap_libdlpi_err(p->opt.device, "dlpi_promiscon" STRINGIFY(level),
273d1e87331SXin LI 		    retv, p->errbuf);
274d1e87331SXin LI 		return (err);
275d1e87331SXin LI 	}
276d1e87331SXin LI 	return (0);
277d1e87331SXin LI }
278d1e87331SXin LI 
279a8e07101SRui Paulo /*
280ada6f083SXin LI  * Presumably everything returned by dlpi_walk() is a DLPI device,
281ada6f083SXin LI  * so there's no work to be done here to check whether name refers
282ada6f083SXin LI  * to a DLPI device.
283ada6f083SXin LI  */
284ada6f083SXin LI static int
285ada6f083SXin LI is_dlpi_interface(const char *name _U_)
286ada6f083SXin LI {
287ada6f083SXin LI 	return (1);
288ada6f083SXin LI }
289ada6f083SXin LI 
290*b00ab754SHans Petter Selasky static int
291*b00ab754SHans Petter Selasky get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
292*b00ab754SHans Petter Selasky {
293*b00ab754SHans Petter Selasky 	/*
294*b00ab754SHans Petter Selasky 	 * Nothing we can do other than mark loopback devices as "the
295*b00ab754SHans Petter Selasky 	 * connected/disconnected status doesn't apply".
296*b00ab754SHans Petter Selasky 	 *
297*b00ab754SHans Petter Selasky 	 * XXX - on Solaris, can we do what the dladm command does,
298*b00ab754SHans Petter Selasky 	 * i.e. get a connected/disconnected indication from a kstat?
299*b00ab754SHans Petter Selasky 	 * (Note that you can also get the link speed, and possibly
300*b00ab754SHans Petter Selasky 	 * other information, from a kstat as well.)
301*b00ab754SHans Petter Selasky 	 */
302*b00ab754SHans Petter Selasky 	if (*flags & PCAP_IF_LOOPBACK) {
303*b00ab754SHans Petter Selasky 		/*
304*b00ab754SHans Petter Selasky 		 * Loopback devices aren't wireless, and "connected"/
305*b00ab754SHans Petter Selasky 		 * "disconnected" doesn't apply to them.
306*b00ab754SHans Petter Selasky 		 */
307*b00ab754SHans Petter Selasky 		*flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
308*b00ab754SHans Petter Selasky 		return (0);
309*b00ab754SHans Petter Selasky 	}
310*b00ab754SHans Petter Selasky 	return (0);
311*b00ab754SHans Petter Selasky }
312*b00ab754SHans Petter Selasky 
313ada6f083SXin LI /*
314a8e07101SRui Paulo  * In Solaris, the "standard" mechanism" i.e SIOCGLIFCONF will only find
315a8e07101SRui Paulo  * network links that are plumbed and are up. dlpi_walk(3DLPI) will find
316a8e07101SRui Paulo  * additional network links present in the system.
317a8e07101SRui Paulo  */
318a8e07101SRui Paulo int
319*b00ab754SHans Petter Selasky pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
320a8e07101SRui Paulo {
321a8e07101SRui Paulo 	int retv = 0;
322a8e07101SRui Paulo 
323a8e07101SRui Paulo 	linknamelist_t	*entry, *next;
324a8e07101SRui Paulo 	linkwalk_t	lw = {NULL, 0};
325a8e07101SRui Paulo 	int 		save_errno;
326a8e07101SRui Paulo 
327ada6f083SXin LI 	/*
328ada6f083SXin LI 	 * Get the list of regular interfaces first.
329ada6f083SXin LI 	 */
330*b00ab754SHans Petter Selasky 	if (pcap_findalldevs_interfaces(devlistp, errbuf,
331*b00ab754SHans Petter Selasky 	    is_dlpi_interface, get_if_flags) == -1)
332ada6f083SXin LI 		return (-1);	/* failure */
333ada6f083SXin LI 
334a8e07101SRui Paulo 	/* dlpi_walk() for loopback will be added here. */
335a8e07101SRui Paulo 
336*b00ab754SHans Petter Selasky 	/*
337*b00ab754SHans Petter Selasky 	 * Find all DLPI devices in the current zone.
338*b00ab754SHans Petter Selasky 	 *
339*b00ab754SHans Petter Selasky 	 * XXX - will pcap_findalldevs_interfaces() find any devices
340*b00ab754SHans Petter Selasky 	 * outside the current zone?  If not, the only reason to call
341*b00ab754SHans Petter Selasky 	 * it would be to get the interface addresses.
342*b00ab754SHans Petter Selasky 	 */
343a8e07101SRui Paulo 	dlpi_walk(list_interfaces, &lw, 0);
344a8e07101SRui Paulo 
345a8e07101SRui Paulo 	if (lw.lw_err != 0) {
346*b00ab754SHans Petter Selasky 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
347*b00ab754SHans Petter Selasky 		    lw.lw_err, "dlpi_walk");
348a8e07101SRui Paulo 		retv = -1;
349a8e07101SRui Paulo 		goto done;
350a8e07101SRui Paulo 	}
351a8e07101SRui Paulo 
352a8e07101SRui Paulo 	/* Add linkname if it does not exist on the list. */
353a8e07101SRui Paulo 	for (entry = lw.lw_list; entry != NULL; entry = entry->lnl_next) {
354*b00ab754SHans Petter Selasky 		/*
355*b00ab754SHans Petter Selasky 		 * If it isn't already in the list of devices, try to
356*b00ab754SHans Petter Selasky 		 * add it.
357*b00ab754SHans Petter Selasky 		 */
358*b00ab754SHans Petter Selasky 		if (find_or_add_dev(devlistp, entry->linkname, 0, get_if_flags,
359*b00ab754SHans Petter Selasky 		    NULL, errbuf) == NULL)
360a8e07101SRui Paulo 			retv = -1;
361a8e07101SRui Paulo 	}
362a8e07101SRui Paulo done:
363a8e07101SRui Paulo 	save_errno = errno;
364a8e07101SRui Paulo 	for (entry = lw.lw_list; entry != NULL; entry = next) {
365a8e07101SRui Paulo 		next = entry->lnl_next;
366a8e07101SRui Paulo 		free(entry);
367a8e07101SRui Paulo 	}
368a8e07101SRui Paulo 	errno = save_errno;
369a8e07101SRui Paulo 
370a8e07101SRui Paulo 	return (retv);
371a8e07101SRui Paulo }
372a8e07101SRui Paulo 
373a8e07101SRui Paulo /*
374a8e07101SRui Paulo  * Read data received on DLPI handle. Returns -2 if told to terminate, else
375a8e07101SRui Paulo  * returns the number of packets read.
376a8e07101SRui Paulo  */
377a8e07101SRui Paulo static int
378a8e07101SRui Paulo pcap_read_libdlpi(pcap_t *p, int count, pcap_handler callback, u_char *user)
379a8e07101SRui Paulo {
380681ed54cSXin LI 	struct pcap_dlpi *pd = p->priv;
381a8e07101SRui Paulo 	int len;
382a8e07101SRui Paulo 	u_char *bufp;
383a8e07101SRui Paulo 	size_t msglen;
384a8e07101SRui Paulo 	int retv;
385a8e07101SRui Paulo 
386a8e07101SRui Paulo 	len = p->cc;
387a8e07101SRui Paulo 	if (len != 0) {
388a8e07101SRui Paulo 		bufp = p->bp;
389a8e07101SRui Paulo 		goto process_pkts;
390a8e07101SRui Paulo 	}
391a8e07101SRui Paulo 	do {
392a8e07101SRui Paulo 		/* Has "pcap_breakloop()" been called? */
393a8e07101SRui Paulo 		if (p->break_loop) {
394a8e07101SRui Paulo 			/*
395a8e07101SRui Paulo 			 * Yes - clear the flag that indicates that it has,
396a8e07101SRui Paulo 			 * and return -2 to indicate that we were told to
397a8e07101SRui Paulo 			 * break out of the loop.
398a8e07101SRui Paulo 			 */
399a8e07101SRui Paulo 			p->break_loop = 0;
400a8e07101SRui Paulo 			return (-2);
401a8e07101SRui Paulo 		}
402a8e07101SRui Paulo 
403a8e07101SRui Paulo 		msglen = p->bufsize;
404ada6f083SXin LI 		bufp = (u_char *)p->buffer + p->offset;
405a8e07101SRui Paulo 
406681ed54cSXin LI 		retv = dlpi_recv(pd->dlpi_hd, NULL, NULL, bufp,
407a8e07101SRui Paulo 		    &msglen, -1, NULL);
408a8e07101SRui Paulo 		if (retv != DLPI_SUCCESS) {
409a8e07101SRui Paulo 			/*
410a8e07101SRui Paulo 			 * This is most likely a call to terminate out of the
411a8e07101SRui Paulo 			 * loop. So, do not return an error message, instead
412a8e07101SRui Paulo 			 * check if "pcap_breakloop()" has been called above.
413a8e07101SRui Paulo 			 */
414a8e07101SRui Paulo 			if (retv == DL_SYSERR && errno == EINTR) {
415a8e07101SRui Paulo 				len = 0;
416a8e07101SRui Paulo 				continue;
417a8e07101SRui Paulo 			}
418681ed54cSXin LI 			pcap_libdlpi_err(dlpi_linkname(pd->dlpi_hd),
419a8e07101SRui Paulo 			    "dlpi_recv", retv, p->errbuf);
420a8e07101SRui Paulo 			return (-1);
421a8e07101SRui Paulo 		}
422a8e07101SRui Paulo 		len = msglen;
423a8e07101SRui Paulo 	} while (len == 0);
424a8e07101SRui Paulo 
425a8e07101SRui Paulo process_pkts:
426a8e07101SRui Paulo 	return (pcap_process_pkts(p, callback, user, count, bufp, len));
427a8e07101SRui Paulo }
428a8e07101SRui Paulo 
429a8e07101SRui Paulo static int
430a8e07101SRui Paulo pcap_inject_libdlpi(pcap_t *p, const void *buf, size_t size)
431a8e07101SRui Paulo {
432681ed54cSXin LI 	struct pcap_dlpi *pd = p->priv;
433a8e07101SRui Paulo 	int retv;
434a8e07101SRui Paulo 
435681ed54cSXin LI 	retv = dlpi_send(pd->dlpi_hd, NULL, 0, buf, size, NULL);
436a8e07101SRui Paulo 	if (retv != DLPI_SUCCESS) {
437681ed54cSXin LI 		pcap_libdlpi_err(dlpi_linkname(pd->dlpi_hd), "dlpi_send", retv,
438a8e07101SRui Paulo 		    p->errbuf);
439a8e07101SRui Paulo 		return (-1);
440a8e07101SRui Paulo 	}
441a8e07101SRui Paulo 	/*
442a8e07101SRui Paulo 	 * dlpi_send(3DLPI) does not provide a way to return the number of
443a8e07101SRui Paulo 	 * bytes sent on the wire. Based on the fact that DLPI_SUCCESS was
444a8e07101SRui Paulo 	 * returned we are assuming 'size' bytes were sent.
445a8e07101SRui Paulo 	 */
446a8e07101SRui Paulo 	return (size);
447a8e07101SRui Paulo }
448a8e07101SRui Paulo 
449a8e07101SRui Paulo /*
450a8e07101SRui Paulo  * Close dlpi handle.
451a8e07101SRui Paulo  */
452a8e07101SRui Paulo static void
453a8e07101SRui Paulo pcap_cleanup_libdlpi(pcap_t *p)
454a8e07101SRui Paulo {
455681ed54cSXin LI 	struct pcap_dlpi *pd = p->priv;
456681ed54cSXin LI 
457681ed54cSXin LI 	if (pd->dlpi_hd != NULL) {
458681ed54cSXin LI 		dlpi_close(pd->dlpi_hd);
459681ed54cSXin LI 		pd->dlpi_hd = NULL;
460a8e07101SRui Paulo 		p->fd = -1;
461a8e07101SRui Paulo 	}
462a8e07101SRui Paulo 	pcap_cleanup_live_common(p);
463a8e07101SRui Paulo }
464a8e07101SRui Paulo 
465a8e07101SRui Paulo /*
466a8e07101SRui Paulo  * Write error message to buffer.
467a8e07101SRui Paulo  */
468a8e07101SRui Paulo static void
469a8e07101SRui Paulo pcap_libdlpi_err(const char *linkname, const char *func, int err, char *errbuf)
470a8e07101SRui Paulo {
471ada6f083SXin LI 	pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "libpcap: %s failed on %s: %s",
472a8e07101SRui Paulo 	    func, linkname, dlpi_strerror(err));
473a8e07101SRui Paulo }
474a8e07101SRui Paulo 
475a8e07101SRui Paulo pcap_t *
476ada6f083SXin LI pcap_create_interface(const char *device _U_, char *ebuf)
477a8e07101SRui Paulo {
478a8e07101SRui Paulo 	pcap_t *p;
479a8e07101SRui Paulo 
480ada6f083SXin LI 	p = pcap_create_common(ebuf, sizeof (struct pcap_dlpi));
481a8e07101SRui Paulo 	if (p == NULL)
482a8e07101SRui Paulo 		return (NULL);
483a8e07101SRui Paulo 
484a8e07101SRui Paulo 	p->activate_op = pcap_activate_libdlpi;
485a8e07101SRui Paulo 	return (p);
486a8e07101SRui Paulo }
487*b00ab754SHans Petter Selasky 
488*b00ab754SHans Petter Selasky /*
489*b00ab754SHans Petter Selasky  * Libpcap version string.
490*b00ab754SHans Petter Selasky  */
491*b00ab754SHans Petter Selasky const char *
492*b00ab754SHans Petter Selasky pcap_lib_version(void)
493*b00ab754SHans Petter Selasky {
494*b00ab754SHans Petter Selasky 	return (PCAP_VERSION_STRING);
495*b00ab754SHans Petter Selasky }
496