]>
The Tcpdump Group git mirrors - libpcap/blob - pcap-libdlpi.c
aeb94be4dd7b1fc8c8ca65202fbae8c7f0d4e553
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.
28 static const char rcsid
[] _U_
=
29 "@(#) $Header: /tcpdump/master/libpcap/pcap-libdlpi.c,v 1.1.2.2 2008-03-15 04:15:46 guy Exp $ (LBL)";
36 #include <sys/types.h>
38 #include <sys/bufmod.h>
39 #include <sys/stream.h>
52 static int pcap_read_libdlpi(pcap_t
*, int, pcap_handler
, u_char
*);
53 static int pcap_inject_libdlpi(pcap_t
*, const void *, size_t);
54 static void pcap_close_libdlpi(pcap_t
*);
55 static void pcap_libdlpi_err(const char *, const char *, int, char *);
58 * list_interfaces() will list all the network links that are
59 * available on a system.
61 static boolean_t
list_interfaces(const char *, void *);
63 typedef struct linknamelist
{
64 char linkname
[DLPI_LINKNAME_MAX
];
65 struct linknamelist
*lnl_next
;
68 typedef struct linkwalk
{
69 linknamelist_t
*lw_list
;
74 * The caller of this function should free the memory allocated
75 * for each linknamelist_t "entry" allocated.
78 list_interfaces(const char *linkname
, void *arg
)
80 linkwalk_t
*lwp
= arg
;
81 linknamelist_t
*entry
;
83 if ((entry
= calloc(1, sizeof(linknamelist_t
))) == NULL
) {
87 (void) strlcpy(entry
->linkname
, linkname
, DLPI_LINKNAME_MAX
);
89 if (lwp
->lw_list
== NULL
) {
92 entry
->lnl_next
= lwp
->lw_list
;
100 pcap_open_live(const char *device
, int snaplen
, int promisc
, int to_ms
,
107 bpf_u_int32 ss
, chunksize
;
109 if ((p
= (pcap_t
*)malloc(sizeof(*p
))) == NULL
) {
110 strlcpy(ebuf
, pcap_strerror(errno
), PCAP_ERRBUF_SIZE
);
113 memset(p
, 0, sizeof(*p
));
114 p
->fd
= -1; /* indicate that it hasn't been opened yet */
118 * Enable Solaris raw and passive DLPI extensions;
119 * dlpi_open() will not fail if the underlying link does not support
120 * passive mode. See dlpi(7P) for details.
122 retv
= dlpi_open(device
, &dh
, DLPI_RAW
|DLPI_PASSIVE
);
123 if (retv
!= DLPI_SUCCESS
) {
124 pcap_libdlpi_err(device
, "dlpi_open", retv
, ebuf
);
129 p
->snapshot
= snaplen
;
131 /* Bind with DLPI_ANY_SAP. */
132 if ((retv
= dlpi_bind(p
->dlpi_hd
, DLPI_ANY_SAP
, 0)) != DLPI_SUCCESS
) {
133 pcap_libdlpi_err(device
, "dlpi_bind", retv
, ebuf
);
137 /* Enable promiscuous mode. */
139 retv
= dlpi_promiscon(p
->dlpi_hd
, DL_PROMISC_PHYS
);
140 if (retv
!= DLPI_SUCCESS
) {
141 pcap_libdlpi_err(device
, "dlpi_promisc(PHYSICAL)",
146 /* Try to enable multicast. */
147 retv
= dlpi_promiscon(p
->dlpi_hd
, DL_PROMISC_MULTI
);
148 if (retv
!= DLPI_SUCCESS
) {
149 pcap_libdlpi_err(device
, "dlpi_promisc(MULTI)",
155 /* Try to enable SAP promiscuity. */
156 if ((retv
= dlpi_promiscon(p
->dlpi_hd
, DL_PROMISC_SAP
)) != DLPI_SUCCESS
) {
158 pcap_libdlpi_err(device
, "dlpi_promisc(SAP)",
163 /* Not fatal, since the DL_PROMISC_PHYS mode worked. */
164 fprintf(stderr
, "WARNING: dlpi_promisc(SAP) failed on"
165 " %s:(%s)\n", device
, dlpi_strerror(retv
));
168 /* Determine link type. */
169 if ((retv
= dlpi_info(p
->dlpi_hd
, &dlinfo
, 0)) != DLPI_SUCCESS
) {
170 pcap_libdlpi_err(device
, "dlpi_info", retv
, ebuf
);
174 if (pcap_process_mactype(p
, dlinfo
.di_mactype
, ebuf
) != 0)
177 p
->fd
= dlpi_fd(p
->dlpi_hd
);
179 /* Push and configure bufmod. */
180 if (pcap_conf_bufmod(p
, ss
, to_ms
, ebuf
) != 0)
184 * Flush the read side.
186 if (ioctl(p
->fd
, I_FLUSH
, FLUSHR
) != 0) {
187 snprintf(ebuf
, PCAP_ERRBUF_SIZE
, "FLUSHR: %s",
188 pcap_strerror(errno
));
192 /* Allocate data buffer. */
193 if (pcap_alloc_databuf(p
, ebuf
) != 0)
197 * "p->fd" is a FD for a STREAMS device, so "select()" and
198 * "poll()" should work on it.
200 p
->selectable_fd
= p
->fd
;
202 p
->read_op
= pcap_read_libdlpi
;
203 p
->inject_op
= pcap_inject_libdlpi
;
204 p
->setfilter_op
= install_bpf_program
; /* No kernel filtering */
205 p
->setdirection_op
= NULL
; /* Not implemented */
206 p
->set_datalink_op
= NULL
; /* Can't change data link type */
207 p
->getnonblock_op
= pcap_getnonblock_fd
;
208 p
->setnonblock_op
= pcap_setnonblock_fd
;
209 p
->stats_op
= pcap_stats_dlpi
;
210 p
->close_op
= pcap_close_libdlpi
;
214 /* Get rid of any link-layer type list we allocated. */
215 if (p
->dlt_list
!= NULL
)
217 pcap_close_libdlpi(p
);
223 * In Solaris, the "standard" mechanism" i.e SIOCGLIFCONF will only find
224 * network links that are plumbed and are up. dlpi_walk(3DLPI) will find
225 * additional network links present in the system.
228 pcap_platform_finddevs(pcap_if_t
**alldevsp
, char *errbuf
)
232 linknamelist_t
*entry
, *next
;
233 linkwalk_t lw
= {NULL
, 0};
236 /* dlpi_walk() for loopback will be added here. */
238 dlpi_walk(list_interfaces
, &lw
, 0);
240 if (lw
.lw_err
!= 0) {
241 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
242 "dlpi_walk: %s", pcap_strerror(lw
.lw_err
));
247 /* Add linkname if it does not exist on the list. */
248 for (entry
= lw
.lw_list
; entry
!= NULL
; entry
= entry
->lnl_next
) {
249 if (pcap_add_if(alldevsp
, entry
->linkname
, 0, NULL
, errbuf
) < 0)
254 for (entry
= lw
.lw_list
; entry
!= NULL
; entry
= next
) {
255 next
= entry
->lnl_next
;
264 * Read data received on DLPI handle. Returns -2 if told to terminate, else
265 * returns the number of packets read.
268 pcap_read_libdlpi(pcap_t
*p
, int count
, pcap_handler callback
, u_char
*user
)
281 /* Has "pcap_breakloop()" been called? */
284 * Yes - clear the flag that indicates that it has,
285 * and return -2 to indicate that we were told to
286 * break out of the loop.
293 bufp
= p
->buffer
+ p
->offset
;
295 retv
= dlpi_recv(p
->dlpi_hd
, NULL
, NULL
, bufp
,
297 if (retv
!= DLPI_SUCCESS
) {
299 * This is most likely a call to terminate out of the
300 * loop. So, do not return an error message, instead
301 * check if "pcap_breakloop()" has been called above.
303 if (retv
== DL_SYSERR
&& errno
== EINTR
) {
307 pcap_libdlpi_err(dlpi_linkname(p
->dlpi_hd
),
308 "dlpi_recv", retv
, p
->errbuf
);
315 return (pcap_process_pkts(p
, callback
, user
, count
, bufp
, len
));
319 pcap_inject_libdlpi(pcap_t
*p
, const void *buf
, size_t size
)
323 retv
= dlpi_send(p
->dlpi_hd
, NULL
, 0, buf
, size
, NULL
);
324 if (retv
!= DLPI_SUCCESS
) {
325 pcap_libdlpi_err(dlpi_linkname(p
->dlpi_hd
), "dlpi_send", retv
,
330 * dlpi_send(3DLPI) does not provide a way to return the number of
331 * bytes sent on the wire. Based on the fact that DLPI_SUCCESS was
332 * returned we are assuming 'size' bytes were sent.
338 * Close dlpi handle and deallocate data buffer.
341 pcap_close_libdlpi(pcap_t
*p
)
343 dlpi_close(p
->dlpi_hd
);
348 * Write error message to buffer.
351 pcap_libdlpi_err(const char *linkname
, const char *func
, int err
, char *errbuf
)
353 snprintf(errbuf
, PCAP_ERRBUF_SIZE
, "libpcap: %s failed on %s: %s",
354 func
, linkname
, dlpi_strerror(err
));