xref: /netbsd-src/external/bsd/libpcap/dist/pcap-libdlpi.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 /*	$NetBSD: pcap-libdlpi.c,v 1.5 2019/10/01 16:02:12 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.5 2019/10/01 16:02:12 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) pcap_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 	/*
146 	 * Turn a negative snapshot value (invalid), a snapshot value of
147 	 * 0 (unspecified), or a value bigger than the normal maximum
148 	 * value, into the maximum allowed value.
149 	 *
150 	 * If some application really *needs* a bigger snapshot
151 	 * length, we should just increase MAXIMUM_SNAPLEN.
152 	 */
153 	if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN)
154 		p->snapshot = MAXIMUM_SNAPLEN;
155 
156 	/* Enable promiscuous mode. */
157 	if (p->opt.promisc) {
158 		retv = dlpromiscon(p, DL_PROMISC_PHYS);
159 		if (retv < 0) {
160 			/*
161 			 * "You don't have permission to capture on
162 			 * this device" and "you don't have permission
163 			 * to capture in promiscuous mode on this
164 			 * device" are different; let the user know,
165 			 * so if they can't get permission to
166 			 * capture in promiscuous mode, they can at
167 			 * least try to capture in non-promiscuous
168 			 * mode.
169 			 *
170 			 * XXX - you might have to capture in
171 			 * promiscuous mode to see outgoing packets.
172 			 */
173 			if (retv == PCAP_ERROR_PERM_DENIED)
174 				status = PCAP_ERROR_PROMISC_PERM_DENIED;
175 			else
176 				status = retv;
177 			goto bad;
178 		}
179 	} else {
180 		/* Try to enable multicast. */
181 		retv = dlpromiscon(p, DL_PROMISC_MULTI);
182 		if (retv < 0) {
183 			status = retv;
184 			goto bad;
185 		}
186 	}
187 
188 	/* Try to enable SAP promiscuity. */
189 	retv = dlpromiscon(p, DL_PROMISC_SAP);
190 	if (retv < 0) {
191 		/*
192 		 * Not fatal, since the DL_PROMISC_PHYS mode worked.
193 		 * Report it as a warning, however.
194 		 */
195 		if (p->opt.promisc)
196 			status = PCAP_WARNING;
197 		else {
198 			status = retv;
199 			goto bad;
200 		}
201 	}
202 
203 	/* Determine link type.  */
204 	if ((retv = dlpi_info(pd->dlpi_hd, &dlinfo, 0)) != DLPI_SUCCESS) {
205 		status = PCAP_ERROR;
206 		pcap_libdlpi_err(p->opt.device, "dlpi_info", retv, p->errbuf);
207 		goto bad;
208 	}
209 
210 	if (pcap_process_mactype(p, dlinfo.di_mactype) != 0) {
211 		status = PCAP_ERROR;
212 		goto bad;
213 	}
214 
215 	p->fd = dlpi_fd(pd->dlpi_hd);
216 
217 	/* Push and configure bufmod. */
218 	if (pcap_conf_bufmod(p, p->snapshot) != 0) {
219 		status = PCAP_ERROR;
220 		goto bad;
221 	}
222 
223 	/*
224 	 * Flush the read side.
225 	 */
226 	if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
227 		status = PCAP_ERROR;
228 		pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
229 		    errno, "FLUSHR");
230 		goto bad;
231 	}
232 
233 	/* Allocate data buffer. */
234 	if (pcap_alloc_databuf(p) != 0) {
235 		status = PCAP_ERROR;
236 		goto bad;
237 	}
238 
239 	/*
240 	 * "p->fd" is a FD for a STREAMS device, so "select()" and
241 	 * "poll()" should work on it.
242 	 */
243 	p->selectable_fd = p->fd;
244 
245 	p->read_op = pcap_read_libdlpi;
246 	p->inject_op = pcap_inject_libdlpi;
247 	p->setfilter_op = install_bpf_program;	/* No kernel filtering */
248 	p->setdirection_op = NULL;	/* Not implemented */
249 	p->set_datalink_op = NULL;	/* Can't change data link type */
250 	p->getnonblock_op = pcap_getnonblock_fd;
251 	p->setnonblock_op = pcap_setnonblock_fd;
252 	p->stats_op = pcap_stats_dlpi;
253 	p->cleanup_op = pcap_cleanup_libdlpi;
254 
255 	return (status);
256 bad:
257 	pcap_cleanup_libdlpi(p);
258 	return (status);
259 }
260 
261 #define STRINGIFY(n)	#n
262 
263 static int
264 dlpromiscon(pcap_t *p, bpf_u_int32 level)
265 {
266 	struct pcap_dlpi *pd = p->priv;
267 	int retv;
268 	int err;
269 
270 	retv = dlpi_promiscon(pd->dlpi_hd, level);
271 	if (retv != DLPI_SUCCESS) {
272 		if (retv == DL_SYSERR &&
273 		    (errno == EPERM || errno == EACCES))
274 			err = PCAP_ERROR_PERM_DENIED;
275 		else
276 			err = PCAP_ERROR;
277 		pcap_libdlpi_err(p->opt.device, "dlpi_promiscon" STRINGIFY(level),
278 		    retv, p->errbuf);
279 		return (err);
280 	}
281 	return (0);
282 }
283 
284 /*
285  * Presumably everything returned by dlpi_walk() is a DLPI device,
286  * so there's no work to be done here to check whether name refers
287  * to a DLPI device.
288  */
289 static int
290 is_dlpi_interface(const char *name _U_)
291 {
292 	return (1);
293 }
294 
295 static int
296 get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_)
297 {
298 	/*
299 	 * Nothing we can do other than mark loopback devices as "the
300 	 * connected/disconnected status doesn't apply".
301 	 *
302 	 * XXX - on Solaris, can we do what the dladm command does,
303 	 * i.e. get a connected/disconnected indication from a kstat?
304 	 * (Note that you can also get the link speed, and possibly
305 	 * other information, from a kstat as well.)
306 	 */
307 	if (*flags & PCAP_IF_LOOPBACK) {
308 		/*
309 		 * Loopback devices aren't wireless, and "connected"/
310 		 * "disconnected" doesn't apply to them.
311 		 */
312 		*flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE;
313 		return (0);
314 	}
315 	return (0);
316 }
317 
318 /*
319  * In Solaris, the "standard" mechanism" i.e SIOCGLIFCONF will only find
320  * network links that are plumbed and are up. dlpi_walk(3DLPI) will find
321  * additional network links present in the system.
322  */
323 int
324 pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf)
325 {
326 	int retv = 0;
327 
328 	linknamelist_t	*entry, *next;
329 	linkwalk_t	lw = {NULL, 0};
330 	int 		save_errno;
331 
332 	/*
333 	 * Get the list of regular interfaces first.
334 	 */
335 	if (pcap_findalldevs_interfaces(devlistp, errbuf,
336 	    is_dlpi_interface, get_if_flags) == -1)
337 		return (-1);	/* failure */
338 
339 	/* dlpi_walk() for loopback will be added here. */
340 
341 	/*
342 	 * Find all DLPI devices in the current zone.
343 	 *
344 	 * XXX - will pcap_findalldevs_interfaces() find any devices
345 	 * outside the current zone?  If not, the only reason to call
346 	 * it would be to get the interface addresses.
347 	 */
348 	dlpi_walk(list_interfaces, &lw, 0);
349 
350 	if (lw.lw_err != 0) {
351 		pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
352 		    lw.lw_err, "dlpi_walk");
353 		retv = -1;
354 		goto done;
355 	}
356 
357 	/* Add linkname if it does not exist on the list. */
358 	for (entry = lw.lw_list; entry != NULL; entry = entry->lnl_next) {
359 		/*
360 		 * If it isn't already in the list of devices, try to
361 		 * add it.
362 		 */
363 		if (find_or_add_dev(devlistp, entry->linkname, 0, get_if_flags,
364 		    NULL, errbuf) == NULL)
365 			retv = -1;
366 	}
367 done:
368 	save_errno = errno;
369 	for (entry = lw.lw_list; entry != NULL; entry = next) {
370 		next = entry->lnl_next;
371 		free(entry);
372 	}
373 	errno = save_errno;
374 
375 	return (retv);
376 }
377 
378 /*
379  * Read data received on DLPI handle. Returns -2 if told to terminate, else
380  * returns the number of packets read.
381  */
382 static int
383 pcap_read_libdlpi(pcap_t *p, int count, pcap_handler callback, u_char *user)
384 {
385 	struct pcap_dlpi *pd = p->priv;
386 	int len;
387 	u_char *bufp;
388 	size_t msglen;
389 	int retv;
390 
391 	len = p->cc;
392 	if (len != 0) {
393 		bufp = p->bp;
394 		goto process_pkts;
395 	}
396 	do {
397 		/* Has "pcap_breakloop()" been called? */
398 		if (p->break_loop) {
399 			/*
400 			 * Yes - clear the flag that indicates that it has,
401 			 * and return -2 to indicate that we were told to
402 			 * break out of the loop.
403 			 */
404 			p->break_loop = 0;
405 			return (-2);
406 		}
407 
408 		msglen = p->bufsize;
409 		bufp = (u_char *)p->buffer + p->offset;
410 
411 		retv = dlpi_recv(pd->dlpi_hd, NULL, NULL, bufp,
412 		    &msglen, -1, NULL);
413 		if (retv != DLPI_SUCCESS) {
414 			/*
415 			 * This is most likely a call to terminate out of the
416 			 * loop. So, do not return an error message, instead
417 			 * check if "pcap_breakloop()" has been called above.
418 			 */
419 			if (retv == DL_SYSERR && errno == EINTR) {
420 				len = 0;
421 				continue;
422 			}
423 			pcap_libdlpi_err(dlpi_linkname(pd->dlpi_hd),
424 			    "dlpi_recv", retv, p->errbuf);
425 			return (-1);
426 		}
427 		len = msglen;
428 	} while (len == 0);
429 
430 process_pkts:
431 	return (pcap_process_pkts(p, callback, user, count, bufp, len));
432 }
433 
434 static int
435 pcap_inject_libdlpi(pcap_t *p, const void *buf, size_t size)
436 {
437 	struct pcap_dlpi *pd = p->priv;
438 	int retv;
439 
440 	retv = dlpi_send(pd->dlpi_hd, NULL, 0, buf, size, NULL);
441 	if (retv != DLPI_SUCCESS) {
442 		pcap_libdlpi_err(dlpi_linkname(pd->dlpi_hd), "dlpi_send", retv,
443 		    p->errbuf);
444 		return (-1);
445 	}
446 	/*
447 	 * dlpi_send(3DLPI) does not provide a way to return the number of
448 	 * bytes sent on the wire. Based on the fact that DLPI_SUCCESS was
449 	 * returned we are assuming 'size' bytes were sent.
450 	 */
451 	return (size);
452 }
453 
454 /*
455  * Close dlpi handle.
456  */
457 static void
458 pcap_cleanup_libdlpi(pcap_t *p)
459 {
460 	struct pcap_dlpi *pd = p->priv;
461 
462 	if (pd->dlpi_hd != NULL) {
463 		dlpi_close(pd->dlpi_hd);
464 		pd->dlpi_hd = NULL;
465 		p->fd = -1;
466 	}
467 	pcap_cleanup_live_common(p);
468 }
469 
470 /*
471  * Write error message to buffer.
472  */
473 static void
474 pcap_libdlpi_err(const char *linkname, const char *func, int err, char *errbuf)
475 {
476 	pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "libpcap: %s failed on %s: %s",
477 	    func, linkname, dlpi_strerror(err));
478 }
479 
480 pcap_t *
481 pcap_create_interface(const char *device _U_, char *ebuf)
482 {
483 	pcap_t *p;
484 
485 	p = pcap_create_common(ebuf, sizeof (struct pcap_dlpi));
486 	if (p == NULL)
487 		return (NULL);
488 
489 	p->activate_op = pcap_activate_libdlpi;
490 	return (p);
491 }
492 
493 /*
494  * Libpcap version string.
495  */
496 const char *
497 pcap_lib_version(void)
498 {
499 	return (PCAP_VERSION_STRING);
500 }
501