]> The Tcpdump Group git mirrors - libpcap/blob - pcap-libdlpi.c
permit --disable-usb to turn off support for sniffing USB interfaces
[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 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <sys/types.h>
32 #include <sys/time.h>
33 #include <sys/bufmod.h>
34 #include <sys/stream.h>
35 #include <libdlpi.h>
36 #include <errno.h>
37 #include <memory.h>
38 #include <stropts.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include "pcap-int.h"
44 #include "dlpisubs.h"
45
46 /* Forwards. */
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 *);
52
53 /*
54 * list_interfaces() will list all the network links that are
55 * available on a system.
56 */
57 static boolean_t list_interfaces(const char *, void *);
58
59 typedef struct linknamelist {
60 char linkname[DLPI_LINKNAME_MAX];
61 struct linknamelist *lnl_next;
62 } linknamelist_t;
63
64 typedef struct linkwalk {
65 linknamelist_t *lw_list;
66 int lw_err;
67 } linkwalk_t;
68
69 /*
70 * The caller of this function should free the memory allocated
71 * for each linknamelist_t "entry" allocated.
72 */
73 static boolean_t
74 list_interfaces(const char *linkname, void *arg)
75 {
76 linkwalk_t *lwp = arg;
77 linknamelist_t *entry;
78
79 if ((entry = calloc(1, sizeof(linknamelist_t))) == NULL) {
80 lwp->lw_err = ENOMEM;
81 return (B_TRUE);
82 }
83 (void) strlcpy(entry->linkname, linkname, DLPI_LINKNAME_MAX);
84
85 if (lwp->lw_list == NULL) {
86 lwp->lw_list = entry;
87 } else {
88 entry->lnl_next = lwp->lw_list;
89 lwp->lw_list = entry;
90 }
91
92 return (B_FALSE);
93 }
94
95 static int
96 pcap_activate_libdlpi(pcap_t *p)
97 {
98 struct pcap_dlpi *pd = p->priv;
99 int retv;
100 dlpi_handle_t dh;
101 dlpi_info_t dlinfo;
102 int err = PCAP_ERROR;
103
104 /*
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.
108 */
109 retv = dlpi_open(p->opt.source, &dh, DLPI_RAW|DLPI_PASSIVE);
110 if (retv != DLPI_SUCCESS) {
111 if (retv == DLPI_ELINKNAMEINVAL || retv == DLPI_ENOLINK)
112 err = PCAP_ERROR_NO_SUCH_DEVICE;
113 else if (retv == DL_SYSERR &&
114 (errno == EPERM || errno == EACCES))
115 err = PCAP_ERROR_PERM_DENIED;
116 pcap_libdlpi_err(p->opt.source, "dlpi_open", retv,
117 p->errbuf);
118 return (err);
119 }
120 pd->dlpi_hd = dh;
121
122 if (p->opt.rfmon) {
123 /*
124 * This device exists, but we don't support monitor mode
125 * any platforms that support DLPI.
126 */
127 err = PCAP_ERROR_RFMON_NOTSUP;
128 goto bad;
129 }
130
131 /* Bind with DLPI_ANY_SAP. */
132 if ((retv = dlpi_bind(pd->dlpi_hd, DLPI_ANY_SAP, 0)) != DLPI_SUCCESS) {
133 pcap_libdlpi_err(p->opt.source, "dlpi_bind", retv, p->errbuf);
134 goto bad;
135 }
136
137 /* Enable promiscuous mode. */
138 if (p->opt.promisc) {
139 err = dlpromiscon(p, DL_PROMISC_PHYS);
140 if (err < 0) {
141 /*
142 * "You don't have permission to capture on
143 * this device" and "you don't have permission
144 * to capture in promiscuous mode on this
145 * device" are different; let the user know,
146 * so if they can't get permission to
147 * capture in promiscuous mode, they can at
148 * least try to capture in non-promiscuous
149 * mode.
150 *
151 * XXX - you might have to capture in
152 * promiscuous mode to see outgoing packets.
153 */
154 if (err == PCAP_ERROR_PERM_DENIED)
155 err = PCAP_ERROR_PROMISC_PERM_DENIED;
156 goto bad;
157 }
158 } else {
159 /* Try to enable multicast. */
160 err = dlpromiscon(p, DL_PROMISC_MULTI);
161 if (err < 0)
162 goto bad;
163 }
164
165 /* Try to enable SAP promiscuity. */
166 err = dlpromiscon(p, DL_PROMISC_SAP);
167 if (err < 0) {
168 /*
169 * Not fatal, since the DL_PROMISC_PHYS mode worked.
170 * Report it as a warning, however.
171 */
172 if (p->opt.promisc)
173 err = PCAP_WARNING;
174 else
175 goto bad;
176 }
177
178 /* Determine link type. */
179 if ((retv = dlpi_info(pd->dlpi_hd, &dlinfo, 0)) != DLPI_SUCCESS) {
180 pcap_libdlpi_err(p->opt.source, "dlpi_info", retv, p->errbuf);
181 goto bad;
182 }
183
184 if (pcap_process_mactype(p, dlinfo.di_mactype) != 0)
185 goto bad;
186
187 p->fd = dlpi_fd(pd->dlpi_hd);
188
189 /* Push and configure bufmod. */
190 if (pcap_conf_bufmod(p, p->snapshot) != 0)
191 goto bad;
192
193 /*
194 * Flush the read side.
195 */
196 if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
197 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "FLUSHR: %s",
198 pcap_strerror(errno));
199 goto bad;
200 }
201
202 /* Allocate data buffer. */
203 if (pcap_alloc_databuf(p) != 0)
204 goto bad;
205
206 /*
207 * "p->fd" is a FD for a STREAMS device, so "select()" and
208 * "poll()" should work on it.
209 */
210 p->selectable_fd = p->fd;
211
212 p->read_op = pcap_read_libdlpi;
213 p->inject_op = pcap_inject_libdlpi;
214 p->setfilter_op = install_bpf_program; /* No kernel filtering */
215 p->setdirection_op = NULL; /* Not implemented */
216 p->set_datalink_op = NULL; /* Can't change data link type */
217 p->getnonblock_op = pcap_getnonblock_fd;
218 p->setnonblock_op = pcap_setnonblock_fd;
219 p->stats_op = pcap_stats_dlpi;
220 p->cleanup_op = pcap_cleanup_libdlpi;
221
222 return (0);
223 bad:
224 pcap_cleanup_libdlpi(p);
225 return (err);
226 }
227
228 #define STRINGIFY(n) #n
229
230 static int
231 dlpromiscon(pcap_t *p, bpf_u_int32 level)
232 {
233 struct pcap_dlpi *pd = p->priv;
234 int retv;
235 int err;
236
237 retv = dlpi_promiscon(pd->dlpi_hd, level);
238 if (retv != DLPI_SUCCESS) {
239 if (retv == DL_SYSERR &&
240 (errno == EPERM || errno == EACCES))
241 err = PCAP_ERROR_PERM_DENIED;
242 else
243 err = PCAP_ERROR;
244 pcap_libdlpi_err(p->opt.source, "dlpi_promiscon" STRINGIFY(level),
245 retv, p->errbuf);
246 return (err);
247 }
248 return (0);
249 }
250
251 /*
252 * In Solaris, the "standard" mechanism" i.e SIOCGLIFCONF will only find
253 * network links that are plumbed and are up. dlpi_walk(3DLPI) will find
254 * additional network links present in the system.
255 */
256 int
257 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
258 {
259 int retv = 0;
260
261 linknamelist_t *entry, *next;
262 linkwalk_t lw = {NULL, 0};
263 int save_errno;
264
265 /* dlpi_walk() for loopback will be added here. */
266
267 dlpi_walk(list_interfaces, &lw, 0);
268
269 if (lw.lw_err != 0) {
270 snprintf(errbuf, PCAP_ERRBUF_SIZE,
271 "dlpi_walk: %s", pcap_strerror(lw.lw_err));
272 retv = -1;
273 goto done;
274 }
275
276 /* Add linkname if it does not exist on the list. */
277 for (entry = lw.lw_list; entry != NULL; entry = entry->lnl_next) {
278 if (pcap_add_if(alldevsp, entry->linkname, 0, NULL, errbuf) < 0)
279 retv = -1;
280 }
281 done:
282 save_errno = errno;
283 for (entry = lw.lw_list; entry != NULL; entry = next) {
284 next = entry->lnl_next;
285 free(entry);
286 }
287 errno = save_errno;
288
289 return (retv);
290 }
291
292 /*
293 * Read data received on DLPI handle. Returns -2 if told to terminate, else
294 * returns the number of packets read.
295 */
296 static int
297 pcap_read_libdlpi(pcap_t *p, int count, pcap_handler callback, u_char *user)
298 {
299 struct pcap_dlpi *pd = p->priv;
300 int len;
301 u_char *bufp;
302 size_t msglen;
303 int retv;
304
305 len = p->cc;
306 if (len != 0) {
307 bufp = p->bp;
308 goto process_pkts;
309 }
310 do {
311 /* Has "pcap_breakloop()" been called? */
312 if (p->break_loop) {
313 /*
314 * Yes - clear the flag that indicates that it has,
315 * and return -2 to indicate that we were told to
316 * break out of the loop.
317 */
318 p->break_loop = 0;
319 return (-2);
320 }
321
322 msglen = p->bufsize;
323 bufp = p->buffer + p->offset;
324
325 retv = dlpi_recv(pd->dlpi_hd, NULL, NULL, bufp,
326 &msglen, -1, NULL);
327 if (retv != DLPI_SUCCESS) {
328 /*
329 * This is most likely a call to terminate out of the
330 * loop. So, do not return an error message, instead
331 * check if "pcap_breakloop()" has been called above.
332 */
333 if (retv == DL_SYSERR && errno == EINTR) {
334 len = 0;
335 continue;
336 }
337 pcap_libdlpi_err(dlpi_linkname(pd->dlpi_hd),
338 "dlpi_recv", retv, p->errbuf);
339 return (-1);
340 }
341 len = msglen;
342 } while (len == 0);
343
344 process_pkts:
345 return (pcap_process_pkts(p, callback, user, count, bufp, len));
346 }
347
348 static int
349 pcap_inject_libdlpi(pcap_t *p, const void *buf, size_t size)
350 {
351 struct pcap_dlpi *pd = p->priv;
352 int retv;
353
354 retv = dlpi_send(pd->dlpi_hd, NULL, 0, buf, size, NULL);
355 if (retv != DLPI_SUCCESS) {
356 pcap_libdlpi_err(dlpi_linkname(pd->dlpi_hd), "dlpi_send", retv,
357 p->errbuf);
358 return (-1);
359 }
360 /*
361 * dlpi_send(3DLPI) does not provide a way to return the number of
362 * bytes sent on the wire. Based on the fact that DLPI_SUCCESS was
363 * returned we are assuming 'size' bytes were sent.
364 */
365 return (size);
366 }
367
368 /*
369 * Close dlpi handle.
370 */
371 static void
372 pcap_cleanup_libdlpi(pcap_t *p)
373 {
374 struct pcap_dlpi *pd = p->priv;
375
376 if (pd->dlpi_hd != NULL) {
377 dlpi_close(pd->dlpi_hd);
378 pd->dlpi_hd = NULL;
379 p->fd = -1;
380 }
381 pcap_cleanup_live_common(p);
382 }
383
384 /*
385 * Write error message to buffer.
386 */
387 static void
388 pcap_libdlpi_err(const char *linkname, const char *func, int err, char *errbuf)
389 {
390 snprintf(errbuf, PCAP_ERRBUF_SIZE, "libpcap: %s failed on %s: %s",
391 func, linkname, dlpi_strerror(err));
392 }
393
394 pcap_t *
395 pcap_create_interface(const char *device, char *ebuf)
396 {
397 pcap_t *p;
398
399 p = pcap_create_common(device, ebuf, sizeof (struct pcap_dlpi));
400 if (p == NULL)
401 return (NULL);
402
403 p->activate_op = pcap_activate_libdlpi;
404 return (p);
405 }