1 /* $NetBSD: pcap-libdlpi.c,v 1.1.1.3 2013/04/06 15:57:44 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_close_libdlpi(pcap_t *); 58 static void pcap_libdlpi_err(const char *, const char *, int, char *); 59 static void pcap_cleanup_libdlpi(pcap_t *); 60 61 /* 62 * list_interfaces() will list all the network links that are 63 * available on a system. 64 */ 65 static boolean_t list_interfaces(const char *, void *); 66 67 typedef struct linknamelist { 68 char linkname[DLPI_LINKNAME_MAX]; 69 struct linknamelist *lnl_next; 70 } linknamelist_t; 71 72 typedef struct linkwalk { 73 linknamelist_t *lw_list; 74 int lw_err; 75 } linkwalk_t; 76 77 /* 78 * The caller of this function should free the memory allocated 79 * for each linknamelist_t "entry" allocated. 80 */ 81 static boolean_t 82 list_interfaces(const char *linkname, void *arg) 83 { 84 linkwalk_t *lwp = arg; 85 linknamelist_t *entry; 86 87 if ((entry = calloc(1, sizeof(linknamelist_t))) == NULL) { 88 lwp->lw_err = ENOMEM; 89 return (B_TRUE); 90 } 91 (void) strlcpy(entry->linkname, linkname, DLPI_LINKNAME_MAX); 92 93 if (lwp->lw_list == NULL) { 94 lwp->lw_list = entry; 95 } else { 96 entry->lnl_next = lwp->lw_list; 97 lwp->lw_list = entry; 98 } 99 100 return (B_FALSE); 101 } 102 103 static int 104 pcap_activate_libdlpi(pcap_t *p) 105 { 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 p->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(p->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(p->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(p->dlpi_hd); 195 196 /* Push and configure bufmod. */ 197 if (pcap_conf_bufmod(p, p->snapshot, p->md.timeout) != 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 int err; 241 242 retv = dlpi_promiscon(p->hd, level); 243 if (retv != DLPI_SUCCESS) { 244 if (retv == DL_SYSERR && 245 (errno == EPERM || errno == EACCES)) 246 err = PCAP_ERROR_PERM_DENIED; 247 else 248 err = PCAP_ERROR; 249 pcap_libdlpi_err(p->opt.source, "dlpi_promiscon" STRINGIFY(level), 250 retv, p->errbuf); 251 return (err); 252 } 253 return (0); 254 } 255 256 /* 257 * In Solaris, the "standard" mechanism" i.e SIOCGLIFCONF will only find 258 * network links that are plumbed and are up. dlpi_walk(3DLPI) will find 259 * additional network links present in the system. 260 */ 261 int 262 pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf) 263 { 264 int retv = 0; 265 266 linknamelist_t *entry, *next; 267 linkwalk_t lw = {NULL, 0}; 268 int save_errno; 269 270 /* dlpi_walk() for loopback will be added here. */ 271 272 dlpi_walk(list_interfaces, &lw, 0); 273 274 if (lw.lw_err != 0) { 275 snprintf(errbuf, PCAP_ERRBUF_SIZE, 276 "dlpi_walk: %s", pcap_strerror(lw.lw_err)); 277 retv = -1; 278 goto done; 279 } 280 281 /* Add linkname if it does not exist on the list. */ 282 for (entry = lw.lw_list; entry != NULL; entry = entry->lnl_next) { 283 if (pcap_add_if(alldevsp, entry->linkname, 0, NULL, errbuf) < 0) 284 retv = -1; 285 } 286 done: 287 save_errno = errno; 288 for (entry = lw.lw_list; entry != NULL; entry = next) { 289 next = entry->lnl_next; 290 free(entry); 291 } 292 errno = save_errno; 293 294 return (retv); 295 } 296 297 /* 298 * Read data received on DLPI handle. Returns -2 if told to terminate, else 299 * returns the number of packets read. 300 */ 301 static int 302 pcap_read_libdlpi(pcap_t *p, int count, pcap_handler callback, u_char *user) 303 { 304 int len; 305 u_char *bufp; 306 size_t msglen; 307 int retv; 308 309 len = p->cc; 310 if (len != 0) { 311 bufp = p->bp; 312 goto process_pkts; 313 } 314 do { 315 /* Has "pcap_breakloop()" been called? */ 316 if (p->break_loop) { 317 /* 318 * Yes - clear the flag that indicates that it has, 319 * and return -2 to indicate that we were told to 320 * break out of the loop. 321 */ 322 p->break_loop = 0; 323 return (-2); 324 } 325 326 msglen = p->bufsize; 327 bufp = p->buffer + p->offset; 328 329 retv = dlpi_recv(p->dlpi_hd, NULL, NULL, bufp, 330 &msglen, -1, NULL); 331 if (retv != DLPI_SUCCESS) { 332 /* 333 * This is most likely a call to terminate out of the 334 * loop. So, do not return an error message, instead 335 * check if "pcap_breakloop()" has been called above. 336 */ 337 if (retv == DL_SYSERR && errno == EINTR) { 338 len = 0; 339 continue; 340 } 341 pcap_libdlpi_err(dlpi_linkname(p->dlpi_hd), 342 "dlpi_recv", retv, p->errbuf); 343 return (-1); 344 } 345 len = msglen; 346 } while (len == 0); 347 348 process_pkts: 349 return (pcap_process_pkts(p, callback, user, count, bufp, len)); 350 } 351 352 static int 353 pcap_inject_libdlpi(pcap_t *p, const void *buf, size_t size) 354 { 355 int retv; 356 357 retv = dlpi_send(p->dlpi_hd, NULL, 0, buf, size, NULL); 358 if (retv != DLPI_SUCCESS) { 359 pcap_libdlpi_err(dlpi_linkname(p->dlpi_hd), "dlpi_send", retv, 360 p->errbuf); 361 return (-1); 362 } 363 /* 364 * dlpi_send(3DLPI) does not provide a way to return the number of 365 * bytes sent on the wire. Based on the fact that DLPI_SUCCESS was 366 * returned we are assuming 'size' bytes were sent. 367 */ 368 return (size); 369 } 370 371 /* 372 * Close dlpi handle. 373 */ 374 static void 375 pcap_cleanup_libdlpi(pcap_t *p) 376 { 377 if (p->dlpi_hd != NULL) { 378 dlpi_close(p->dlpi_hd); 379 p->dlpi_hd = NULL; 380 p->fd = -1; 381 } 382 pcap_cleanup_live_common(p); 383 } 384 385 /* 386 * Write error message to buffer. 387 */ 388 static void 389 pcap_libdlpi_err(const char *linkname, const char *func, int err, char *errbuf) 390 { 391 snprintf(errbuf, PCAP_ERRBUF_SIZE, "libpcap: %s failed on %s: %s", 392 func, linkname, dlpi_strerror(err)); 393 } 394 395 pcap_t * 396 pcap_create(const char *device, char *ebuf) 397 { 398 pcap_t *p; 399 400 p = pcap_create_common(device, ebuf); 401 if (p == NULL) 402 return (NULL); 403 404 p->activate_op = pcap_activate_libdlpi; 405 return (p); 406 } 407