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