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