2 * Copyright (c) 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
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
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.
21 * This code contributed by Sagun Shakya (sagun.shakya@sun.com)
24 * Packet capture routines for DLPI using libdlpi under SunOS 5.11.
31 #include <sys/types.h>
33 #include <sys/bufmod.h>
34 #include <sys/stream.h>
47 static int dlpromiscon(pcap_t
*, bpf_u_int32
);
48 static int pcap_read_libdlpi(pcap_t
*, int, pcap_handler
, u_char
*);
49 static int pcap_inject_libdlpi(pcap_t
*, const void *, size_t);
50 static void pcap_libdlpi_err(const char *, const char *, int, char *);
51 static void pcap_cleanup_libdlpi(pcap_t
*);
54 * list_interfaces() will list all the network links that are
55 * available on a system.
57 static boolean_t
list_interfaces(const char *, void *);
59 typedef struct linknamelist
{
60 char linkname
[DLPI_LINKNAME_MAX
];
61 struct linknamelist
*lnl_next
;
64 typedef struct linkwalk
{
65 linknamelist_t
*lw_list
;
70 * The caller of this function should free the memory allocated
71 * for each linknamelist_t "entry" allocated.
74 list_interfaces(const char *linkname
, void *arg
)
76 linkwalk_t
*lwp
= arg
;
77 linknamelist_t
*entry
;
79 if ((entry
= calloc(1, sizeof(linknamelist_t
))) == NULL
) {
83 (void) strlcpy(entry
->linkname
, linkname
, DLPI_LINKNAME_MAX
);
85 if (lwp
->lw_list
== NULL
) {
88 entry
->lnl_next
= lwp
->lw_list
;
96 pcap_activate_libdlpi(pcap_t
*p
)
98 struct pcap_dlpi
*pd
= p
->priv
;
105 * Enable Solaris raw and passive DLPI extensions;
106 * dlpi_open() will not fail if the underlying link does not support
107 * passive mode. See dlpi(7P) for details.
109 retv
= dlpi_open(p
->opt
.device
, &dh
, DLPI_RAW
|DLPI_PASSIVE
);
110 if (retv
!= DLPI_SUCCESS
) {
111 if (retv
== DLPI_ELINKNAMEINVAL
|| retv
== DLPI_ENOLINK
)
112 status
= PCAP_ERROR_NO_SUCH_DEVICE
;
113 else if (retv
== DL_SYSERR
&&
114 (errno
== EPERM
|| errno
== EACCES
))
115 status
= PCAP_ERROR_PERM_DENIED
;
118 pcap_libdlpi_err(p
->opt
.device
, "dlpi_open", retv
,
126 * This device exists, but we don't support monitor mode
127 * any platforms that support DLPI.
129 status
= PCAP_ERROR_RFMON_NOTSUP
;
133 /* Bind with DLPI_ANY_SAP. */
134 if ((retv
= dlpi_bind(pd
->dlpi_hd
, DLPI_ANY_SAP
, 0)) != DLPI_SUCCESS
) {
136 pcap_libdlpi_err(p
->opt
.device
, "dlpi_bind", retv
, p
->errbuf
);
141 * Turn a negative snapshot value (invalid), a snapshot value of
142 * 0 (unspecified), or a value bigger than the normal maximum
143 * value, into the maximum allowed value.
145 * If some application really *needs* a bigger snapshot
146 * length, we should just increase MAXIMUM_SNAPLEN.
148 if (p
->snapshot
<= 0 || p
->snapshot
> MAXIMUM_SNAPLEN
)
149 p
->snapshot
= MAXIMUM_SNAPLEN
;
151 /* Enable promiscuous mode. */
152 if (p
->opt
.promisc
) {
153 retv
= dlpromiscon(p
, DL_PROMISC_PHYS
);
156 * "You don't have permission to capture on
157 * this device" and "you don't have permission
158 * to capture in promiscuous mode on this
159 * device" are different; let the user know,
160 * so if they can't get permission to
161 * capture in promiscuous mode, they can at
162 * least try to capture in non-promiscuous
165 * XXX - you might have to capture in
166 * promiscuous mode to see outgoing packets.
168 if (retv
== PCAP_ERROR_PERM_DENIED
)
169 status
= PCAP_ERROR_PROMISC_PERM_DENIED
;
175 /* Try to enable multicast. */
176 retv
= dlpromiscon(p
, DL_PROMISC_MULTI
);
183 /* Try to enable SAP promiscuity. */
184 retv
= dlpromiscon(p
, DL_PROMISC_SAP
);
187 * Not fatal, since the DL_PROMISC_PHYS mode worked.
188 * Report it as a warning, however.
191 status
= PCAP_WARNING
;
198 /* Determine link type. */
199 if ((retv
= dlpi_info(pd
->dlpi_hd
, &dlinfo
, 0)) != DLPI_SUCCESS
) {
201 pcap_libdlpi_err(p
->opt
.device
, "dlpi_info", retv
, p
->errbuf
);
205 if (pcap_process_mactype(p
, dlinfo
.di_mactype
) != 0) {
210 p
->fd
= dlpi_fd(pd
->dlpi_hd
);
212 /* Push and configure bufmod. */
213 if (pcap_conf_bufmod(p
, p
->snapshot
) != 0) {
219 * Flush the read side.
221 if (ioctl(p
->fd
, I_FLUSH
, FLUSHR
) != 0) {
223 pcap_snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
, "FLUSHR: %s",
224 pcap_strerror(errno
));
228 /* Allocate data buffer. */
229 if (pcap_alloc_databuf(p
) != 0) {
235 * "p->fd" is a FD for a STREAMS device, so "select()" and
236 * "poll()" should work on it.
238 p
->selectable_fd
= p
->fd
;
240 p
->read_op
= pcap_read_libdlpi
;
241 p
->inject_op
= pcap_inject_libdlpi
;
242 p
->setfilter_op
= install_bpf_program
; /* No kernel filtering */
243 p
->setdirection_op
= NULL
; /* Not implemented */
244 p
->set_datalink_op
= NULL
; /* Can't change data link type */
245 p
->getnonblock_op
= pcap_getnonblock_fd
;
246 p
->setnonblock_op
= pcap_setnonblock_fd
;
247 p
->stats_op
= pcap_stats_dlpi
;
248 p
->cleanup_op
= pcap_cleanup_libdlpi
;
252 pcap_cleanup_libdlpi(p
);
256 #define STRINGIFY(n) #n
259 dlpromiscon(pcap_t
*p
, bpf_u_int32 level
)
261 struct pcap_dlpi
*pd
= p
->priv
;
265 retv
= dlpi_promiscon(pd
->dlpi_hd
, level
);
266 if (retv
!= DLPI_SUCCESS
) {
267 if (retv
== DL_SYSERR
&&
268 (errno
== EPERM
|| errno
== EACCES
))
269 err
= PCAP_ERROR_PERM_DENIED
;
272 pcap_libdlpi_err(p
->opt
.device
, "dlpi_promiscon" STRINGIFY(level
),
280 * Presumably everything returned by dlpi_walk() is a DLPI device,
281 * so there's no work to be done here to check whether name refers
285 is_dlpi_interface(const char *name _U_
)
291 * In Solaris, the "standard" mechanism" i.e SIOCGLIFCONF will only find
292 * network links that are plumbed and are up. dlpi_walk(3DLPI) will find
293 * additional network links present in the system.
296 pcap_platform_finddevs(pcap_if_list_t
*devlistp
, char *errbuf
)
300 linknamelist_t
*entry
, *next
;
301 linkwalk_t lw
= {NULL
, 0};
305 * Get the list of regular interfaces first.
307 if (pcap_findalldevs_interfaces(devlistp
, errbuf
,
308 is_dlpi_interface
) == -1)
309 return (-1); /* failure */
311 /* dlpi_walk() for loopback will be added here. */
314 * Find all DLPI devices in the current zone.
316 * XXX - will pcap_findalldevs_interfaces() find any devices
317 * outside the current zone? If not, the only reason to call
318 * it would be to get the interface addresses.
320 dlpi_walk(list_interfaces
, &lw
, 0);
322 if (lw
.lw_err
!= 0) {
323 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
324 "dlpi_walk: %s", pcap_strerror(lw
.lw_err
));
329 /* Add linkname if it does not exist on the list. */
330 for (entry
= lw
.lw_list
; entry
!= NULL
; entry
= entry
->lnl_next
) {
332 * If it isn't already in the list of devices, try to
335 if (find_or_add_dev(devlistp
, entry
->linkname
, 0, NULL
,
341 for (entry
= lw
.lw_list
; entry
!= NULL
; entry
= next
) {
342 next
= entry
->lnl_next
;
351 * Read data received on DLPI handle. Returns -2 if told to terminate, else
352 * returns the number of packets read.
355 pcap_read_libdlpi(pcap_t
*p
, int count
, pcap_handler callback
, u_char
*user
)
357 struct pcap_dlpi
*pd
= p
->priv
;
369 /* Has "pcap_breakloop()" been called? */
372 * Yes - clear the flag that indicates that it has,
373 * and return -2 to indicate that we were told to
374 * break out of the loop.
381 bufp
= (u_char
*)p
->buffer
+ p
->offset
;
383 retv
= dlpi_recv(pd
->dlpi_hd
, NULL
, NULL
, bufp
,
385 if (retv
!= DLPI_SUCCESS
) {
387 * This is most likely a call to terminate out of the
388 * loop. So, do not return an error message, instead
389 * check if "pcap_breakloop()" has been called above.
391 if (retv
== DL_SYSERR
&& errno
== EINTR
) {
395 pcap_libdlpi_err(dlpi_linkname(pd
->dlpi_hd
),
396 "dlpi_recv", retv
, p
->errbuf
);
403 return (pcap_process_pkts(p
, callback
, user
, count
, bufp
, len
));
407 pcap_inject_libdlpi(pcap_t
*p
, const void *buf
, size_t size
)
409 struct pcap_dlpi
*pd
= p
->priv
;
412 retv
= dlpi_send(pd
->dlpi_hd
, NULL
, 0, buf
, size
, NULL
);
413 if (retv
!= DLPI_SUCCESS
) {
414 pcap_libdlpi_err(dlpi_linkname(pd
->dlpi_hd
), "dlpi_send", retv
,
419 * dlpi_send(3DLPI) does not provide a way to return the number of
420 * bytes sent on the wire. Based on the fact that DLPI_SUCCESS was
421 * returned we are assuming 'size' bytes were sent.
430 pcap_cleanup_libdlpi(pcap_t
*p
)
432 struct pcap_dlpi
*pd
= p
->priv
;
434 if (pd
->dlpi_hd
!= NULL
) {
435 dlpi_close(pd
->dlpi_hd
);
439 pcap_cleanup_live_common(p
);
443 * Write error message to buffer.
446 pcap_libdlpi_err(const char *linkname
, const char *func
, int err
, char *errbuf
)
448 pcap_snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "libpcap: %s failed on %s: %s",
449 func
, linkname
, dlpi_strerror(err
));
453 pcap_create_interface(const char *device _U_
, char *ebuf
)
457 p
= pcap_create_common(ebuf
, sizeof (struct pcap_dlpi
));
461 p
->activate_op
= pcap_activate_libdlpi
;
466 * No platform-specific information.
469 pcap_platform_lib_version(void)