1/*
2 * Copyright (c) 1993, 1994, 1995, 1996, 1997
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * This code contributed by Sagun Shakya (sagun.shakya@sun.com)
22 */
23/*
24 * Packet capture routines for DLPI using libdlpi under SunOS 5.11.
25 */
26
27#ifndef lint
28static const char rcsid[] _U_ =
29	"@(#) $Header: /tcpdump/master/libpcap/pcap-libdlpi.c,v 1.6 2008-04-14 20:40:58 guy Exp $ (LBL)";
30#endif
31
32#ifdef HAVE_CONFIG_H
33#include "config.h"
34#endif
35
36#include <sys/types.h>
37#include <sys/time.h>
38#include <sys/bufmod.h>
39#include <sys/stream.h>
40#include <libdlpi.h>
41#include <errno.h>
42#include <memory.h>
43#include <stropts.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47
48#include "pcap-int.h"
49#include "dlpisubs.h"
50
51/* Forwards. */
52static int dlpromiscon(pcap_t *, bpf_u_int32);
53static int pcap_read_libdlpi(pcap_t *, int, pcap_handler, u_char *);
54static int pcap_inject_libdlpi(pcap_t *, const void *, size_t);
55static void pcap_libdlpi_err(const char *, const char *, int, char *);
56static void pcap_cleanup_libdlpi(pcap_t *);
57
58/*
59 * list_interfaces() will list all the network links that are
60 * available on a system.
61 */
62static boolean_t list_interfaces(const char *, void *);
63
64typedef struct linknamelist {
65	char	linkname[DLPI_LINKNAME_MAX];
66	struct linknamelist *lnl_next;
67} linknamelist_t;
68
69typedef struct linkwalk {
70	linknamelist_t	*lw_list;
71	int		lw_err;
72} linkwalk_t;
73
74/*
75 * The caller of this function should free the memory allocated
76 * for each linknamelist_t "entry" allocated.
77 */
78static boolean_t
79list_interfaces(const char *linkname, void *arg)
80{
81	linkwalk_t	*lwp = arg;
82	linknamelist_t	*entry;
83
84	if ((entry = calloc(1, sizeof(linknamelist_t))) == NULL) {
85		lwp->lw_err = ENOMEM;
86		return (B_TRUE);
87	}
88	(void) strlcpy(entry->linkname, linkname, DLPI_LINKNAME_MAX);
89
90	if (lwp->lw_list == NULL) {
91		lwp->lw_list = entry;
92	} else {
93		entry->lnl_next = lwp->lw_list;
94		lwp->lw_list = entry;
95	}
96
97	return (B_FALSE);
98}
99
100static int
101pcap_activate_libdlpi(pcap_t *p)
102{
103	struct pcap_dlpi *pd = p->priv;
104	int retv;
105	dlpi_handle_t dh;
106	dlpi_info_t dlinfo;
107	int err = PCAP_ERROR;
108
109	/*
110	 * Enable Solaris raw and passive DLPI extensions;
111	 * dlpi_open() will not fail if the underlying link does not support
112	 * passive mode. See dlpi(7P) for details.
113	 */
114	retv = dlpi_open(p->opt.source, &dh, DLPI_RAW|DLPI_PASSIVE);
115	if (retv != DLPI_SUCCESS) {
116		if (retv == DLPI_ELINKNAMEINVAL || retv == DLPI_ENOLINK)
117			err = PCAP_ERROR_NO_SUCH_DEVICE;
118		else if (retv == DL_SYSERR &&
119		    (errno == EPERM || errno == EACCES))
120			err = PCAP_ERROR_PERM_DENIED;
121		pcap_libdlpi_err(p->opt.source, "dlpi_open", retv,
122		    p->errbuf);
123		return (err);
124	}
125	pd->dlpi_hd = dh;
126
127	if (p->opt.rfmon) {
128		/*
129		 * This device exists, but we don't support monitor mode
130		 * any platforms that support DLPI.
131		 */
132		err = PCAP_ERROR_RFMON_NOTSUP;
133		goto bad;
134	}
135
136	/* Bind with DLPI_ANY_SAP. */
137	if ((retv = dlpi_bind(pd->dlpi_hd, DLPI_ANY_SAP, 0)) != DLPI_SUCCESS) {
138		pcap_libdlpi_err(p->opt.source, "dlpi_bind", retv, p->errbuf);
139		goto bad;
140	}
141
142	/* Enable promiscuous mode. */
143	if (p->opt.promisc) {
144		err = dlpromiscon(p, DL_PROMISC_PHYS);
145		if (err < 0) {
146			/*
147			 * "You don't have permission to capture on
148			 * this device" and "you don't have permission
149			 * to capture in promiscuous mode on this
150			 * device" are different; let the user know,
151			 * so if they can't get permission to
152			 * capture in promiscuous mode, they can at
153			 * least try to capture in non-promiscuous
154			 * mode.
155			 *
156			 * XXX - you might have to capture in
157			 * promiscuous mode to see outgoing packets.
158			 */
159			if (err == PCAP_ERROR_PERM_DENIED)
160				err = PCAP_ERROR_PROMISC_PERM_DENIED;
161			goto bad;
162		}
163	} else {
164		/* Try to enable multicast. */
165		err = dlpromiscon(p, DL_PROMISC_MULTI);
166		if (err < 0)
167			goto bad;
168	}
169
170	/* Try to enable SAP promiscuity. */
171	err = dlpromiscon(p, DL_PROMISC_SAP);
172	if (err < 0) {
173		/*
174		 * Not fatal, since the DL_PROMISC_PHYS mode worked.
175		 * Report it as a warning, however.
176		 */
177		if (p->opt.promisc)
178			err = PCAP_WARNING;
179		else
180			goto bad;
181	}
182
183	/* Determine link type.  */
184	if ((retv = dlpi_info(pd->dlpi_hd, &dlinfo, 0)) != DLPI_SUCCESS) {
185		pcap_libdlpi_err(p->opt.source, "dlpi_info", retv, p->errbuf);
186		goto bad;
187	}
188
189	if (pcap_process_mactype(p, dlinfo.di_mactype) != 0)
190		goto bad;
191
192	p->fd = dlpi_fd(pd->dlpi_hd);
193
194	/* Push and configure bufmod. */
195	if (pcap_conf_bufmod(p, p->snapshot) != 0)
196		goto bad;
197
198	/*
199	 * Flush the read side.
200	 */
201	if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
202		snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "FLUSHR: %s",
203		    pcap_strerror(errno));
204		goto bad;
205	}
206
207	/* Allocate data buffer. */
208	if (pcap_alloc_databuf(p) != 0)
209		goto bad;
210
211	/*
212	 * "p->fd" is a FD for a STREAMS device, so "select()" and
213	 * "poll()" should work on it.
214	 */
215	p->selectable_fd = p->fd;
216
217	p->read_op = pcap_read_libdlpi;
218	p->inject_op = pcap_inject_libdlpi;
219	p->setfilter_op = install_bpf_program;	/* No kernel filtering */
220	p->setdirection_op = NULL;	/* Not implemented */
221	p->set_datalink_op = NULL;	/* Can't change data link type */
222	p->getnonblock_op = pcap_getnonblock_fd;
223	p->setnonblock_op = pcap_setnonblock_fd;
224	p->stats_op = pcap_stats_dlpi;
225	p->cleanup_op = pcap_cleanup_libdlpi;
226
227	return (0);
228bad:
229	pcap_cleanup_libdlpi(p);
230	return (err);
231}
232
233#define STRINGIFY(n)	#n
234
235static int
236dlpromiscon(pcap_t *p, bpf_u_int32 level)
237{
238	struct pcap_dlpi *pd = p->priv;
239	int retv;
240	int err;
241
242	retv = dlpi_promiscon(pd->dlpi_hd, level);
243	if (retv != DLPI_SUCCESS) {
244		if (retv == DL_SYSERR &&
245		    (errno == EPERM || errno == EACCES))
246			err = PCAP_ERROR_PERM_DENIED;
247		else
248			err = PCAP_ERROR;
249		pcap_libdlpi_err(p->opt.source, "dlpi_promiscon" STRINGIFY(level),
250		    retv, p->errbuf);
251		return (err);
252	}
253	return (0);
254}
255
256/*
257 * In Solaris, the "standard" mechanism" i.e SIOCGLIFCONF will only find
258 * network links that are plumbed and are up. dlpi_walk(3DLPI) will find
259 * additional network links present in the system.
260 */
261int
262pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
263{
264	int retv = 0;
265
266	linknamelist_t	*entry, *next;
267	linkwalk_t	lw = {NULL, 0};
268	int 		save_errno;
269
270	/* dlpi_walk() for loopback will be added here. */
271
272	dlpi_walk(list_interfaces, &lw, 0);
273
274	if (lw.lw_err != 0) {
275		snprintf(errbuf, PCAP_ERRBUF_SIZE,
276		    "dlpi_walk: %s", pcap_strerror(lw.lw_err));
277		retv = -1;
278		goto done;
279	}
280
281	/* Add linkname if it does not exist on the list. */
282	for (entry = lw.lw_list; entry != NULL; entry = entry->lnl_next) {
283		if (pcap_add_if(alldevsp, entry->linkname, 0, NULL, errbuf) < 0)
284			retv = -1;
285	}
286done:
287	save_errno = errno;
288	for (entry = lw.lw_list; entry != NULL; entry = next) {
289		next = entry->lnl_next;
290		free(entry);
291	}
292	errno = save_errno;
293
294	return (retv);
295}
296
297/*
298 * Read data received on DLPI handle. Returns -2 if told to terminate, else
299 * returns the number of packets read.
300 */
301static int
302pcap_read_libdlpi(pcap_t *p, int count, pcap_handler callback, u_char *user)
303{
304	struct pcap_dlpi *pd = p->priv;
305	int len;
306	u_char *bufp;
307	size_t msglen;
308	int retv;
309
310	len = p->cc;
311	if (len != 0) {
312		bufp = p->bp;
313		goto process_pkts;
314	}
315	do {
316		/* Has "pcap_breakloop()" been called? */
317		if (p->break_loop) {
318			/*
319			 * Yes - clear the flag that indicates that it has,
320			 * and return -2 to indicate that we were told to
321			 * break out of the loop.
322			 */
323			p->break_loop = 0;
324			return (-2);
325		}
326
327		msglen = p->bufsize;
328		bufp = p->buffer + p->offset;
329
330		retv = dlpi_recv(pd->dlpi_hd, NULL, NULL, bufp,
331		    &msglen, -1, NULL);
332		if (retv != DLPI_SUCCESS) {
333			/*
334			 * This is most likely a call to terminate out of the
335			 * loop. So, do not return an error message, instead
336			 * check if "pcap_breakloop()" has been called above.
337			 */
338			if (retv == DL_SYSERR && errno == EINTR) {
339				len = 0;
340				continue;
341			}
342			pcap_libdlpi_err(dlpi_linkname(pd->dlpi_hd),
343			    "dlpi_recv", retv, p->errbuf);
344			return (-1);
345		}
346		len = msglen;
347	} while (len == 0);
348
349process_pkts:
350	return (pcap_process_pkts(p, callback, user, count, bufp, len));
351}
352
353static int
354pcap_inject_libdlpi(pcap_t *p, const void *buf, size_t size)
355{
356	struct pcap_dlpi *pd = p->priv;
357	int retv;
358
359	retv = dlpi_send(pd->dlpi_hd, NULL, 0, buf, size, NULL);
360	if (retv != DLPI_SUCCESS) {
361		pcap_libdlpi_err(dlpi_linkname(pd->dlpi_hd), "dlpi_send", retv,
362		    p->errbuf);
363		return (-1);
364	}
365	/*
366	 * dlpi_send(3DLPI) does not provide a way to return the number of
367	 * bytes sent on the wire. Based on the fact that DLPI_SUCCESS was
368	 * returned we are assuming 'size' bytes were sent.
369	 */
370	return (size);
371}
372
373/*
374 * Close dlpi handle.
375 */
376static void
377pcap_cleanup_libdlpi(pcap_t *p)
378{
379	struct pcap_dlpi *pd = p->priv;
380
381	if (pd->dlpi_hd != NULL) {
382		dlpi_close(pd->dlpi_hd);
383		pd->dlpi_hd = NULL;
384		p->fd = -1;
385	}
386	pcap_cleanup_live_common(p);
387}
388
389/*
390 * Write error message to buffer.
391 */
392static void
393pcap_libdlpi_err(const char *linkname, const char *func, int err, char *errbuf)
394{
395	snprintf(errbuf, PCAP_ERRBUF_SIZE, "libpcap: %s failed on %s: %s",
396	    func, linkname, dlpi_strerror(err));
397}
398
399pcap_t *
400pcap_create_interface(const char *device, char *ebuf)
401{
402	pcap_t *p;
403
404	p = pcap_create_common(device, ebuf, sizeof (struct pcap_dlpi));
405	if (p == NULL)
406		return (NULL);
407
408	p->activate_op = pcap_activate_libdlpi;
409	return (p);
410}
411