]> The Tcpdump Group git mirrors - libpcap/blob - pcap-libdlpi.c
Get rid of an unused variable, and stop using an unset variable and then
[libpcap] / pcap-libdlpi.c
1 /*
2 * Copyright (c) 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
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
16 * written permission.
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.
20 *
21 * This code contributed by Sagun Shakya (sagun.shakya@sun.com)
22 */
23 /*
24 * Packet capture routines for DLPI using libdlpi under SunOS 5.11.
25 */
26
27 #ifndef lint
28 static const char rcsid[] _U_ =
29 "@(#) $Header: /tcpdump/master/libpcap/pcap-libdlpi.c,v 1.1.2.3 2008-03-15 04:26:29 guy Exp $ (LBL)";
30 #endif
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 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 *);
56
57 /*
58 * list_interfaces() will list all the network links that are
59 * available on a system.
60 */
61 static boolean_t list_interfaces(const char *, void *);
62
63 typedef struct linknamelist {
64 char linkname[DLPI_LINKNAME_MAX];
65 struct linknamelist *lnl_next;
66 } linknamelist_t;
67
68 typedef struct linkwalk {
69 linknamelist_t *lw_list;
70 int lw_err;
71 } linkwalk_t;
72
73 /*
74 * The caller of this function should free the memory allocated
75 * for each linknamelist_t "entry" allocated.
76 */
77 static boolean_t
78 list_interfaces(const char *linkname, void *arg)
79 {
80 linkwalk_t *lwp = arg;
81 linknamelist_t *entry;
82
83 if ((entry = calloc(1, sizeof(linknamelist_t))) == NULL) {
84 lwp->lw_err = ENOMEM;
85 return (B_TRUE);
86 }
87 (void) strlcpy(entry->linkname, linkname, DLPI_LINKNAME_MAX);
88
89 if (lwp->lw_list == NULL) {
90 lwp->lw_list = entry;
91 } else {
92 entry->lnl_next = lwp->lw_list;
93 lwp->lw_list = entry;
94 }
95
96 return (B_FALSE);
97 }
98
99 pcap_t *
100 pcap_open_live(const char *device, int snaplen, int promisc, int to_ms,
101 char *ebuf)
102 {
103 int retv;
104 pcap_t *p;
105 dlpi_handle_t dh;
106 dlpi_info_t dlinfo;
107
108 if ((p = (pcap_t *)malloc(sizeof(*p))) == NULL) {
109 strlcpy(ebuf, pcap_strerror(errno), PCAP_ERRBUF_SIZE);
110 return (NULL);
111 }
112 memset(p, 0, sizeof(*p));
113 p->fd = -1; /* indicate that it hasn't been opened yet */
114 p->send_fd = -1;
115
116 /*
117 * Enable Solaris raw and passive DLPI extensions;
118 * dlpi_open() will not fail if the underlying link does not support
119 * passive mode. See dlpi(7P) for details.
120 */
121 retv = dlpi_open(device, &dh, DLPI_RAW|DLPI_PASSIVE);
122 if (retv != DLPI_SUCCESS) {
123 pcap_libdlpi_err(device, "dlpi_open", retv, ebuf);
124 goto bad;
125 }
126 p->dlpi_hd = dh;
127
128 p->snapshot = snaplen;
129
130 /* Bind with DLPI_ANY_SAP. */
131 if ((retv = dlpi_bind(p->dlpi_hd, DLPI_ANY_SAP, 0)) != DLPI_SUCCESS) {
132 pcap_libdlpi_err(device, "dlpi_bind", retv, ebuf);
133 goto bad;
134 }
135
136 /* Enable promiscuous mode. */
137 if (promisc) {
138 retv = dlpi_promiscon(p->dlpi_hd, DL_PROMISC_PHYS);
139 if (retv != DLPI_SUCCESS) {
140 pcap_libdlpi_err(device, "dlpi_promisc(PHYSICAL)",
141 retv, ebuf);
142 goto bad;
143 }
144 } else {
145 /* Try to enable multicast. */
146 retv = dlpi_promiscon(p->dlpi_hd, DL_PROMISC_MULTI);
147 if (retv != DLPI_SUCCESS) {
148 pcap_libdlpi_err(device, "dlpi_promisc(MULTI)",
149 retv, ebuf);
150 goto bad;
151 }
152 }
153
154 /* Try to enable SAP promiscuity. */
155 if ((retv = dlpi_promiscon(p->dlpi_hd, DL_PROMISC_SAP)) != DLPI_SUCCESS) {
156 if (!promisc) {
157 pcap_libdlpi_err(device, "dlpi_promisc(SAP)",
158 retv, ebuf);
159 goto bad;
160 }
161
162 /* Not fatal, since the DL_PROMISC_PHYS mode worked. */
163 fprintf(stderr, "WARNING: dlpi_promisc(SAP) failed on"
164 " %s:(%s)\n", device, dlpi_strerror(retv));
165 }
166
167 /* Determine link type. */
168 if ((retv = dlpi_info(p->dlpi_hd, &dlinfo, 0)) != DLPI_SUCCESS) {
169 pcap_libdlpi_err(device, "dlpi_info", retv, ebuf);
170 goto bad;
171 }
172
173 if (pcap_process_mactype(p, dlinfo.di_mactype, ebuf) != 0)
174 goto bad;
175
176 p->fd = dlpi_fd(p->dlpi_hd);
177
178 /* Push and configure bufmod. */
179 if (pcap_conf_bufmod(p, snaplen, to_ms, ebuf) != 0)
180 goto bad;
181
182 /*
183 * Flush the read side.
184 */
185 if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
186 snprintf(ebuf, PCAP_ERRBUF_SIZE, "FLUSHR: %s",
187 pcap_strerror(errno));
188 goto bad;
189 }
190
191 /* Allocate data buffer. */
192 if (pcap_alloc_databuf(p, ebuf) != 0)
193 goto bad;
194
195 /*
196 * "p->fd" is a FD for a STREAMS device, so "select()" and
197 * "poll()" should work on it.
198 */
199 p->selectable_fd = p->fd;
200
201 p->read_op = pcap_read_libdlpi;
202 p->inject_op = pcap_inject_libdlpi;
203 p->setfilter_op = install_bpf_program; /* No kernel filtering */
204 p->setdirection_op = NULL; /* Not implemented */
205 p->set_datalink_op = NULL; /* Can't change data link type */
206 p->getnonblock_op = pcap_getnonblock_fd;
207 p->setnonblock_op = pcap_setnonblock_fd;
208 p->stats_op = pcap_stats_dlpi;
209 p->close_op = pcap_close_libdlpi;
210
211 return (p);
212 bad:
213 /* Get rid of any link-layer type list we allocated. */
214 if (p->dlt_list != NULL)
215 free(p->dlt_list);
216 pcap_close_libdlpi(p);
217 free(p);
218 return (NULL);
219 }
220
221 /*
222 * In Solaris, the "standard" mechanism" i.e SIOCGLIFCONF will only find
223 * network links that are plumbed and are up. dlpi_walk(3DLPI) will find
224 * additional network links present in the system.
225 */
226 int
227 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
228 {
229 int retv = 0;
230
231 linknamelist_t *entry, *next;
232 linkwalk_t lw = {NULL, 0};
233 int save_errno;
234
235 /* dlpi_walk() for loopback will be added here. */
236
237 dlpi_walk(list_interfaces, &lw, 0);
238
239 if (lw.lw_err != 0) {
240 snprintf(errbuf, PCAP_ERRBUF_SIZE,
241 "dlpi_walk: %s", pcap_strerror(lw.lw_err));
242 retv = -1;
243 goto done;
244 }
245
246 /* Add linkname if it does not exist on the list. */
247 for (entry = lw.lw_list; entry != NULL; entry = entry->lnl_next) {
248 if (pcap_add_if(alldevsp, entry->linkname, 0, NULL, errbuf) < 0)
249 retv = -1;
250 }
251 done:
252 save_errno = errno;
253 for (entry = lw.lw_list; entry != NULL; entry = next) {
254 next = entry->lnl_next;
255 free(entry);
256 }
257 errno = save_errno;
258
259 return (retv);
260 }
261
262 /*
263 * Read data received on DLPI handle. Returns -2 if told to terminate, else
264 * returns the number of packets read.
265 */
266 static int
267 pcap_read_libdlpi(pcap_t *p, int count, pcap_handler callback, u_char *user)
268 {
269 int len;
270 u_char *bufp;
271 size_t msglen;
272 int retv;
273
274 len = p->cc;
275 if (len != 0) {
276 bufp = p->bp;
277 goto process_pkts;
278 }
279 do {
280 /* Has "pcap_breakloop()" been called? */
281 if (p->break_loop) {
282 /*
283 * Yes - clear the flag that indicates that it has,
284 * and return -2 to indicate that we were told to
285 * break out of the loop.
286 */
287 p->break_loop = 0;
288 return (-2);
289 }
290
291 msglen = p->bufsize;
292 bufp = p->buffer + p->offset;
293
294 retv = dlpi_recv(p->dlpi_hd, NULL, NULL, bufp,
295 &msglen, -1, NULL);
296 if (retv != DLPI_SUCCESS) {
297 /*
298 * This is most likely a call to terminate out of the
299 * loop. So, do not return an error message, instead
300 * check if "pcap_breakloop()" has been called above.
301 */
302 if (retv == DL_SYSERR && errno == EINTR) {
303 len = 0;
304 continue;
305 }
306 pcap_libdlpi_err(dlpi_linkname(p->dlpi_hd),
307 "dlpi_recv", retv, p->errbuf);
308 return (-1);
309 }
310 len = msglen;
311 } while (len == 0);
312
313 process_pkts:
314 return (pcap_process_pkts(p, callback, user, count, bufp, len));
315 }
316
317 static int
318 pcap_inject_libdlpi(pcap_t *p, const void *buf, size_t size)
319 {
320 int retv;
321
322 retv = dlpi_send(p->dlpi_hd, NULL, 0, buf, size, NULL);
323 if (retv != DLPI_SUCCESS) {
324 pcap_libdlpi_err(dlpi_linkname(p->dlpi_hd), "dlpi_send", retv,
325 p->errbuf);
326 return (-1);
327 }
328 /*
329 * dlpi_send(3DLPI) does not provide a way to return the number of
330 * bytes sent on the wire. Based on the fact that DLPI_SUCCESS was
331 * returned we are assuming 'size' bytes were sent.
332 */
333 return (size);
334 }
335
336 /*
337 * Close dlpi handle and deallocate data buffer.
338 */
339 static void
340 pcap_close_libdlpi(pcap_t *p)
341 {
342 dlpi_close(p->dlpi_hd);
343 free(p->buffer);
344 }
345
346 /*
347 * Write error message to buffer.
348 */
349 static void
350 pcap_libdlpi_err(const char *linkname, const char *func, int err, char *errbuf)
351 {
352 snprintf(errbuf, PCAP_ERRBUF_SIZE, "libpcap: %s failed on %s: %s",
353 func, linkname, dlpi_strerror(err));
354 }