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 #include <config.h> 28 29 #include <sys/types.h> 30 #include <sys/time.h> 31 #include <sys/bufmod.h> 32 #include <sys/stream.h> 33 #include <libdlpi.h> 34 #include <errno.h> 35 #include <memory.h> 36 #include <stropts.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 41 #include "pcap-int.h" 42 #include "dlpisubs.h" 43 44 /* Forwards. */ 45 static int dlpromiscon(pcap_t *, bpf_u_int32); 46 static int pcap_read_libdlpi(pcap_t *, int, pcap_handler, u_char *); 47 static int pcap_inject_libdlpi(pcap_t *, const void *, int); 48 static void pcap_libdlpi_err(const char *, const char *, int, char *); 49 static void pcap_cleanup_libdlpi(pcap_t *); 50 51 /* 52 * list_interfaces() will list all the network links that are 53 * available on a system. 54 */ 55 static boolean_t list_interfaces(const char *, void *); 56 57 typedef struct linknamelist { 58 char linkname[DLPI_LINKNAME_MAX]; 59 struct linknamelist *lnl_next; 60 } linknamelist_t; 61 62 typedef struct linkwalk { 63 linknamelist_t *lw_list; 64 int lw_err; 65 } linkwalk_t; 66 67 /* 68 * The caller of this function should free the memory allocated 69 * for each linknamelist_t "entry" allocated. 70 */ 71 static boolean_t 72 list_interfaces(const char *linkname, void *arg) 73 { 74 linkwalk_t *lwp = arg; 75 linknamelist_t *entry; 76 77 if ((entry = calloc(1, sizeof(linknamelist_t))) == NULL) { 78 lwp->lw_err = ENOMEM; 79 return (B_TRUE); 80 } 81 (void) pcapint_strlcpy(entry->linkname, linkname, DLPI_LINKNAME_MAX); 82 83 if (lwp->lw_list == NULL) { 84 lwp->lw_list = entry; 85 } else { 86 entry->lnl_next = lwp->lw_list; 87 lwp->lw_list = entry; 88 } 89 90 return (B_FALSE); 91 } 92 93 static int 94 pcap_activate_libdlpi(pcap_t *p) 95 { 96 struct pcap_dlpi *pd = p->priv; 97 int status = 0; 98 int retv; 99 dlpi_handle_t dh; 100 dlpi_info_t dlinfo; 101 102 /* 103 * Enable Solaris raw and passive DLPI extensions; 104 * dlpi_open() will not fail if the underlying link does not support 105 * passive mode. See dlpi(7P) for details. 106 */ 107 retv = dlpi_open(p->opt.device, &dh, DLPI_RAW|DLPI_PASSIVE); 108 if (retv != DLPI_SUCCESS) { 109 if (retv == DLPI_ELINKNAMEINVAL || retv == DLPI_ENOLINK) { 110 /* 111 * There's nothing more to say, so clear the 112 * error message. 113 */ 114 status = PCAP_ERROR_NO_SUCH_DEVICE; 115 p->errbuf[0] = '\0'; 116 } else if (retv == DL_SYSERR && 117 (errno == EPERM || errno == EACCES)) { 118 status = PCAP_ERROR_PERM_DENIED; 119 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 120 "Attempt to open DLPI device failed with %s - root privilege may be required", 121 (errno == EPERM) ? "EPERM" : "EACCES"); 122 } else { 123 status = PCAP_ERROR; 124 pcap_libdlpi_err(p->opt.device, "dlpi_open", retv, 125 p->errbuf); 126 } 127 return (status); 128 } 129 pd->dlpi_hd = dh; 130 131 if (p->opt.rfmon) { 132 /* 133 * This device exists, but we don't support monitor mode 134 * any platforms that support DLPI. 135 */ 136 status = PCAP_ERROR_RFMON_NOTSUP; 137 goto bad; 138 } 139 140 /* Bind with DLPI_ANY_SAP. */ 141 if ((retv = dlpi_bind(pd->dlpi_hd, DLPI_ANY_SAP, 0)) != DLPI_SUCCESS) { 142 status = PCAP_ERROR; 143 pcap_libdlpi_err(p->opt.device, "dlpi_bind", retv, p->errbuf); 144 goto bad; 145 } 146 147 /* 148 * Turn a negative snapshot value (invalid), a snapshot value of 149 * 0 (unspecified), or a value bigger than the normal maximum 150 * value, into the maximum allowed value. 151 * 152 * If some application really *needs* a bigger snapshot 153 * length, we should just increase MAXIMUM_SNAPLEN. 154 */ 155 if (p->snapshot <= 0 || p->snapshot > MAXIMUM_SNAPLEN) 156 p->snapshot = MAXIMUM_SNAPLEN; 157 158 /* Enable promiscuous mode. */ 159 if (p->opt.promisc) { 160 retv = dlpromiscon(p, DL_PROMISC_PHYS); 161 if (retv < 0) { 162 /* 163 * "You don't have permission to capture on 164 * this device" and "you don't have permission 165 * to capture in promiscuous mode on this 166 * device" are different; let the user know, 167 * so if they can't get permission to 168 * capture in promiscuous mode, they can at 169 * least try to capture in non-promiscuous 170 * mode. 171 * 172 * XXX - you might have to capture in 173 * promiscuous mode to see outgoing packets. 174 */ 175 if (retv == PCAP_ERROR_PERM_DENIED) 176 status = PCAP_ERROR_PROMISC_PERM_DENIED; 177 else 178 status = retv; 179 goto bad; 180 } 181 } else { 182 /* Try to enable multicast. */ 183 retv = dlpromiscon(p, DL_PROMISC_MULTI); 184 if (retv < 0) { 185 status = retv; 186 goto bad; 187 } 188 } 189 190 /* Try to enable SAP promiscuity. */ 191 retv = dlpromiscon(p, DL_PROMISC_SAP); 192 if (retv < 0) { 193 /* 194 * Not fatal, since the DL_PROMISC_PHYS mode worked. 195 * Report it as a warning, however. 196 */ 197 if (p->opt.promisc) 198 status = PCAP_WARNING; 199 else { 200 status = retv; 201 goto bad; 202 } 203 } 204 205 /* Determine link type. */ 206 if ((retv = dlpi_info(pd->dlpi_hd, &dlinfo, 0)) != DLPI_SUCCESS) { 207 status = PCAP_ERROR; 208 pcap_libdlpi_err(p->opt.device, "dlpi_info", retv, p->errbuf); 209 goto bad; 210 } 211 212 if (pcap_process_mactype(p, dlinfo.di_mactype) != 0) { 213 status = PCAP_ERROR; 214 goto bad; 215 } 216 217 p->fd = dlpi_fd(pd->dlpi_hd); 218 219 /* Push and configure bufmod. */ 220 if (pcap_conf_bufmod(p, p->snapshot) != 0) { 221 status = PCAP_ERROR; 222 goto bad; 223 } 224 225 /* 226 * Flush the read side. 227 */ 228 if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) { 229 status = PCAP_ERROR; 230 pcapint_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, 231 errno, "FLUSHR"); 232 goto bad; 233 } 234 235 /* Allocate data buffer. */ 236 if (pcap_alloc_databuf(p) != 0) { 237 status = PCAP_ERROR; 238 goto bad; 239 } 240 241 /* 242 * "p->fd" is a FD for a STREAMS device, so "select()" and 243 * "poll()" should work on it. 244 */ 245 p->selectable_fd = p->fd; 246 247 p->read_op = pcap_read_libdlpi; 248 p->inject_op = pcap_inject_libdlpi; 249 p->setfilter_op = pcapint_install_bpf_program; /* No kernel filtering */ 250 p->setdirection_op = NULL; /* Not implemented */ 251 p->set_datalink_op = NULL; /* Can't change data link type */ 252 p->getnonblock_op = pcapint_getnonblock_fd; 253 p->setnonblock_op = pcapint_setnonblock_fd; 254 p->stats_op = pcap_stats_dlpi; 255 p->cleanup_op = pcap_cleanup_libdlpi; 256 257 return (status); 258 bad: 259 pcap_cleanup_libdlpi(p); 260 return (status); 261 } 262 263 #define STRINGIFY(n) #n 264 265 static int 266 dlpromiscon(pcap_t *p, bpf_u_int32 level) 267 { 268 struct pcap_dlpi *pd = p->priv; 269 int retv; 270 int err; 271 272 retv = dlpi_promiscon(pd->dlpi_hd, level); 273 if (retv != DLPI_SUCCESS) { 274 if (retv == DL_SYSERR && 275 (errno == EPERM || errno == EACCES)) { 276 if (level == DL_PROMISC_PHYS) { 277 err = PCAP_ERROR_PROMISC_PERM_DENIED; 278 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 279 "Attempt to set promiscuous mode failed with %s - root privilege may be required", 280 (errno == EPERM) ? "EPERM" : "EACCES"); 281 } else { 282 err = PCAP_ERROR_PERM_DENIED; 283 snprintf(p->errbuf, PCAP_ERRBUF_SIZE, 284 "Attempt to set %s mode failed with %s - root privilege may be required", 285 (level == DL_PROMISC_MULTI) ? "multicast" : "SAP promiscuous", 286 (errno == EPERM) ? "EPERM" : "EACCES"); 287 } 288 } else { 289 err = PCAP_ERROR; 290 pcap_libdlpi_err(p->opt.device, 291 "dlpi_promiscon" STRINGIFY(level), 292 retv, p->errbuf); 293 } 294 return (err); 295 } 296 return (0); 297 } 298 299 /* 300 * Presumably everything returned by dlpi_walk() is a DLPI device, 301 * so there's no work to be done here to check whether name refers 302 * to a DLPI device. 303 */ 304 static int 305 is_dlpi_interface(const char *name _U_) 306 { 307 return (1); 308 } 309 310 static int 311 get_if_flags(const char *name _U_, bpf_u_int32 *flags _U_, char *errbuf _U_) 312 { 313 /* 314 * Nothing we can do other than mark loopback devices as "the 315 * connected/disconnected status doesn't apply". 316 * 317 * XXX - on Solaris, can we do what the dladm command does, 318 * i.e. get a connected/disconnected indication from a kstat? 319 * (Note that you can also get the link speed, and possibly 320 * other information, from a kstat as well.) 321 */ 322 if (*flags & PCAP_IF_LOOPBACK) { 323 /* 324 * Loopback devices aren't wireless, and "connected"/ 325 * "disconnected" doesn't apply to them. 326 */ 327 *flags |= PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE; 328 return (0); 329 } 330 return (0); 331 } 332 333 /* 334 * In Solaris, the "standard" mechanism" i.e SIOCGLIFCONF will only find 335 * network links that are plumbed and are up. dlpi_walk(3DLPI) will find 336 * additional network links present in the system. 337 */ 338 int 339 pcapint_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) 340 { 341 int retv = 0; 342 343 linknamelist_t *entry, *next; 344 linkwalk_t lw = {NULL, 0}; 345 int save_errno; 346 347 /* 348 * Get the list of regular interfaces first. 349 */ 350 if (pcapint_findalldevs_interfaces(devlistp, errbuf, 351 is_dlpi_interface, get_if_flags) == -1) 352 return (-1); /* failure */ 353 354 /* dlpi_walk() for loopback will be added here. */ 355 356 /* 357 * Find all DLPI devices in the current zone. 358 * 359 * XXX - will pcapint_findalldevs_interfaces() find any devices 360 * outside the current zone? If not, the only reason to call 361 * it would be to get the interface addresses. 362 */ 363 dlpi_walk(list_interfaces, &lw, 0); 364 365 if (lw.lw_err != 0) { 366 pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, 367 lw.lw_err, "dlpi_walk"); 368 retv = -1; 369 goto done; 370 } 371 372 /* Add linkname if it does not exist on the list. */ 373 for (entry = lw.lw_list; entry != NULL; entry = entry->lnl_next) { 374 /* 375 * If it isn't already in the list of devices, try to 376 * add it. 377 */ 378 if (pcapint_find_or_add_dev(devlistp, entry->linkname, 0, get_if_flags, 379 NULL, errbuf) == NULL) 380 retv = -1; 381 } 382 done: 383 save_errno = errno; 384 for (entry = lw.lw_list; entry != NULL; entry = next) { 385 next = entry->lnl_next; 386 free(entry); 387 } 388 errno = save_errno; 389 390 return (retv); 391 } 392 393 /* 394 * Read data received on DLPI handle. Returns -2 if told to terminate, else 395 * returns the number of packets read. 396 */ 397 static int 398 pcap_read_libdlpi(pcap_t *p, int count, pcap_handler callback, u_char *user) 399 { 400 struct pcap_dlpi *pd = p->priv; 401 int len; 402 u_char *bufp; 403 size_t msglen; 404 int retv; 405 406 len = p->cc; 407 if (len != 0) { 408 bufp = p->bp; 409 goto process_pkts; 410 } 411 do { 412 /* Has "pcap_breakloop()" been called? */ 413 if (p->break_loop) { 414 /* 415 * Yes - clear the flag that indicates that it has, 416 * and return -2 to indicate that we were told to 417 * break out of the loop. 418 */ 419 p->break_loop = 0; 420 return (-2); 421 } 422 423 msglen = p->bufsize; 424 bufp = (u_char *)p->buffer + p->offset; 425 426 retv = dlpi_recv(pd->dlpi_hd, NULL, NULL, bufp, 427 &msglen, -1, NULL); 428 if (retv != DLPI_SUCCESS) { 429 /* 430 * This is most likely a call to terminate out of the 431 * loop. So, do not return an error message, instead 432 * check if "pcap_breakloop()" has been called above. 433 */ 434 if (retv == DL_SYSERR && errno == EINTR) { 435 len = 0; 436 continue; 437 } 438 pcap_libdlpi_err(dlpi_linkname(pd->dlpi_hd), 439 "dlpi_recv", retv, p->errbuf); 440 return (-1); 441 } 442 len = msglen; 443 } while (len == 0); 444 445 process_pkts: 446 return (pcap_process_pkts(p, callback, user, count, bufp, len)); 447 } 448 449 static int 450 pcap_inject_libdlpi(pcap_t *p, const void *buf, int size) 451 { 452 struct pcap_dlpi *pd = p->priv; 453 int retv; 454 455 retv = dlpi_send(pd->dlpi_hd, NULL, 0, buf, size, NULL); 456 if (retv != DLPI_SUCCESS) { 457 pcap_libdlpi_err(dlpi_linkname(pd->dlpi_hd), "dlpi_send", retv, 458 p->errbuf); 459 return (-1); 460 } 461 /* 462 * dlpi_send(3DLPI) does not provide a way to return the number of 463 * bytes sent on the wire. Based on the fact that DLPI_SUCCESS was 464 * returned we are assuming 'size' bytes were sent. 465 */ 466 return (size); 467 } 468 469 /* 470 * Close dlpi handle. 471 */ 472 static void 473 pcap_cleanup_libdlpi(pcap_t *p) 474 { 475 struct pcap_dlpi *pd = p->priv; 476 477 if (pd->dlpi_hd != NULL) { 478 dlpi_close(pd->dlpi_hd); 479 pd->dlpi_hd = NULL; 480 p->fd = -1; 481 } 482 pcapint_cleanup_live_common(p); 483 } 484 485 /* 486 * Write error message to buffer. 487 */ 488 static void 489 pcap_libdlpi_err(const char *linkname, const char *func, int err, char *errbuf) 490 { 491 snprintf(errbuf, PCAP_ERRBUF_SIZE, "libpcap: %s failed on %s: %s", 492 func, linkname, dlpi_strerror(err)); 493 } 494 495 pcap_t * 496 pcapint_create_interface(const char *device _U_, char *ebuf) 497 { 498 pcap_t *p; 499 500 p = PCAP_CREATE_COMMON(ebuf, struct pcap_dlpi); 501 if (p == NULL) 502 return (NULL); 503 504 p->activate_op = pcap_activate_libdlpi; 505 return (p); 506 } 507 508 /* 509 * Libpcap version string. 510 */ 511 const char * 512 pcap_lib_version(void) 513 { 514 return (PCAP_VERSION_STRING); 515 } 516