1 /* $NetBSD: pcap-libdlpi.c,v 1.2 2014/11/19 19:33:30 christos Exp $ */
4 * Copyright (c) 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
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
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.
23 * This code contributed by Sagun Shakya (sagun.shakya@sun.com)
26 * Packet capture routines for DLPI using libdlpi under SunOS 5.11.
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: pcap-libdlpi.c,v 1.2 2014/11/19 19:33:30 christos Exp $");
36 #include <sys/types.h>
38 #include <sys/bufmod.h>
39 #include <sys/stream.h>
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
*);
59 * list_interfaces() will list all the network links that are
60 * available on a system.
62 static boolean_t
list_interfaces(const char *, void *);
64 typedef struct linknamelist
{
65 char linkname
[DLPI_LINKNAME_MAX
];
66 struct linknamelist
*lnl_next
;
69 typedef struct linkwalk
{
70 linknamelist_t
*lw_list
;
75 * The caller of this function should free the memory allocated
76 * for each linknamelist_t "entry" allocated.
79 list_interfaces(const char *linkname
, void *arg
)
81 linkwalk_t
*lwp
= arg
;
82 linknamelist_t
*entry
;
84 if ((entry
= calloc(1, sizeof(linknamelist_t
))) == NULL
) {
88 (void) strlcpy(entry
->linkname
, linkname
, DLPI_LINKNAME_MAX
);
90 if (lwp
->lw_list
== NULL
) {
93 entry
->lnl_next
= lwp
->lw_list
;
101 pcap_activate_libdlpi(pcap_t
*p
)
103 struct pcap_dlpi
*pd
= p
->priv
;
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.
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 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
118 else if (retv
== DL_SYSERR
&&
119 (errno
== EPERM
|| errno
== EACCES
))
120 status
= PCAP_ERROR_PERM_DENIED
;
123 pcap_libdlpi_err(p
->opt
.source
, "dlpi_open", retv
,
131 * This device exists, but we don't support monitor mode
132 * any platforms that support DLPI.
134 status
= PCAP_ERROR_RFMON_NOTSUP
;
138 /* Bind with DLPI_ANY_SAP. */
139 if ((retv
= dlpi_bind(pd
->dlpi_hd
, DLPI_ANY_SAP
, 0)) != DLPI_SUCCESS
) {
141 pcap_libdlpi_err(p
->opt
.source
, "dlpi_bind", retv
, p
->errbuf
);
145 /* Enable promiscuous mode. */
146 if (p
->opt
.promisc
) {
147 retv
= dlpromiscon(p
, DL_PROMISC_PHYS
);
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
159 * XXX - you might have to capture in
160 * promiscuous mode to see outgoing packets.
162 if (retv
== PCAP_ERROR_PERM_DENIED
)
163 status
= PCAP_ERROR_PROMISC_PERM_DENIED
;
169 /* Try to enable multicast. */
170 retv
= dlpromiscon(p
, DL_PROMISC_MULTI
);
177 /* Try to enable SAP promiscuity. */
178 retv
= dlpromiscon(p
, DL_PROMISC_SAP
);
181 * Not fatal, since the DL_PROMISC_PHYS mode worked.
182 * Report it as a warning, however.
185 status
= PCAP_WARNING
;
192 /* Determine link type. */
193 if ((retv
= dlpi_info(pd
->dlpi_hd
, &dlinfo
, 0)) != DLPI_SUCCESS
) {
195 pcap_libdlpi_err(p
->opt
.source
, "dlpi_info", retv
, p
->errbuf
);
199 if (pcap_process_mactype(p
, dlinfo
.di_mactype
) != 0) {
204 p
->fd
= dlpi_fd(pd
->dlpi_hd
);
206 /* Push and configure bufmod. */
207 if (pcap_conf_bufmod(p
, p
->snapshot
) != 0) {
213 * Flush the read side.
215 if (ioctl(p
->fd
, I_FLUSH
, FLUSHR
) != 0) {
217 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "FLUSHR: %s",
218 pcap_strerror(errno
));
222 /* Allocate data buffer. */
223 if (pcap_alloc_databuf(p
) != 0) {
229 * "p->fd" is a FD for a STREAMS device, so "select()" and
230 * "poll()" should work on it.
232 p
->selectable_fd
= p
->fd
;
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
;
246 pcap_cleanup_libdlpi(p
);
250 #define STRINGIFY(n) #n
253 dlpromiscon(pcap_t
*p
, bpf_u_int32 level
)
255 struct pcap_dlpi
*pd
= p
->priv
;
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
;
266 pcap_libdlpi_err(p
->opt
.source
, "dlpi_promiscon" STRINGIFY(level
),
274 * In Solaris, the "standard" mechanism" i.e SIOCGLIFCONF will only find
275 * network links that are plumbed and are up. dlpi_walk(3DLPI) will find
276 * additional network links present in the system.
279 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
283 linknamelist_t
*entry
, *next
;
284 linkwalk_t lw
= {NULL
, 0};
287 /* dlpi_walk() for loopback will be added here. */
289 dlpi_walk(list_interfaces
, &lw
, 0);
291 if (lw
.lw_err
!= 0) {
292 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
293 "dlpi_walk: %s", pcap_strerror(lw
.lw_err
));
298 /* Add linkname if it does not exist on the list. */
299 for (entry
= lw
.lw_list
; entry
!= NULL
; entry
= entry
->lnl_next
) {
300 if (pcap_add_if(alldevsp
, entry
->linkname
, 0, NULL
, errbuf
) < 0)
305 for (entry
= lw
.lw_list
; entry
!= NULL
; entry
= next
) {
306 next
= entry
->lnl_next
;
315 * Read data received on DLPI handle. Returns -2 if told to terminate, else
316 * returns the number of packets read.
319 pcap_read_libdlpi(pcap_t
*p
, int count
, pcap_handler callback
, u_char
*user
)
321 struct pcap_dlpi
*pd
= p
->priv
;
333 /* Has "pcap_breakloop()" been called? */
336 * Yes - clear the flag that indicates that it has,
337 * and return -2 to indicate that we were told to
338 * break out of the loop.
345 bufp
= p
->buffer
+ p
->offset
;
347 retv
= dlpi_recv(pd
->dlpi_hd
, NULL
, NULL
, bufp
,
349 if (retv
!= DLPI_SUCCESS
) {
351 * This is most likely a call to terminate out of the
352 * loop. So, do not return an error message, instead
353 * check if "pcap_breakloop()" has been called above.
355 if (retv
== DL_SYSERR
&& errno
== EINTR
) {
359 pcap_libdlpi_err(dlpi_linkname(pd
->dlpi_hd
),
360 "dlpi_recv", retv
, p
->errbuf
);
367 return (pcap_process_pkts(p
, callback
, user
, count
, bufp
, len
));
371 pcap_inject_libdlpi(pcap_t
*p
, const void *buf
, size_t size
)
373 struct pcap_dlpi
*pd
= p
->priv
;
376 retv
= dlpi_send(pd
->dlpi_hd
, NULL
, 0, buf
, size
, NULL
);
377 if (retv
!= DLPI_SUCCESS
) {
378 pcap_libdlpi_err(dlpi_linkname(pd
->dlpi_hd
), "dlpi_send", retv
,
383 * dlpi_send(3DLPI) does not provide a way to return the number of
384 * bytes sent on the wire. Based on the fact that DLPI_SUCCESS was
385 * returned we are assuming 'size' bytes were sent.
394 pcap_cleanup_libdlpi(pcap_t
*p
)
396 struct pcap_dlpi
*pd
= p
->priv
;
398 if (pd
->dlpi_hd
!= NULL
) {
399 dlpi_close(pd
->dlpi_hd
);
403 pcap_cleanup_live_common(p
);
407 * Write error message to buffer.
410 pcap_libdlpi_err(const char *linkname
, const char *func
, int err
, char *errbuf
)
412 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "libpcap: %s failed on %s: %s",
413 func
, linkname
, dlpi_strerror(err
));
417 pcap_create_interface(const char *device
, char *ebuf
)
421 p
= pcap_create_common(device
, ebuf
, sizeof (struct pcap_dlpi
));
425 p
->activate_op
= pcap_activate_libdlpi
;