|
1 | | -/* $NetBSD: dlpisubs.c,v 1.6 2024/09/02 15:33:36 christos Exp $ */ |
| 1 | +/* $NetBSD: dlpisubs.c,v 1.7 2026/03/18 23:43:20 christos Exp $ */ |
2 | 2 |
|
3 | 3 | /* |
4 | 4 | * This code is derived from code formerly in pcap-dlpi.c, originally |
|
14 | 14 | */ |
15 | 15 |
|
16 | 16 | #include <sys/cdefs.h> |
17 | | -__RCSID("$NetBSD: dlpisubs.c,v 1.6 2024/09/02 15:33:36 christos Exp $"); |
| 17 | +__RCSID("$NetBSD: dlpisubs.c,v 1.7 2026/03/18 23:43:20 christos Exp $"); |
18 | 18 |
|
19 | 19 | #include <config.h> |
20 | 20 |
|
@@ -65,10 +65,20 @@ __RCSID("$NetBSD: dlpisubs.c,v 1.6 2024/09/02 15:33:36 christos Exp $"); |
65 | 65 | #include <stropts.h> |
66 | 66 | #include <unistd.h> |
67 | 67 |
|
| 68 | +#include <sys/ioctl.h> |
| 69 | +#include <sys/socket.h> |
| 70 | +#include <sys/sockio.h> |
| 71 | + |
| 72 | +#include <net/if.h> |
| 73 | + |
68 | 74 | #ifdef HAVE_LIBDLPI |
69 | 75 | #include <libdlpi.h> |
70 | 76 | #endif |
71 | 77 |
|
| 78 | +#ifdef HAVE_ZONE_H |
| 79 | +#include <zone.h> |
| 80 | +#endif |
| 81 | + |
72 | 82 | #include "pcap-int.h" |
73 | 83 | #include "dlpisubs.h" |
74 | 84 |
|
@@ -418,3 +428,117 @@ pcap_stream_err(const char *func, int err, char *errbuf) |
418 | 428 | pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, err, "%s", func); |
419 | 429 | } |
420 | 430 | #endif |
| 431 | + |
| 432 | +int |
| 433 | +handle_nonexistent_dlpi_device(const char *ifname, char *errbuf) |
| 434 | +{ |
| 435 | + int fd; |
| 436 | + int status; |
| 437 | +#ifdef LIFNAMSIZ |
| 438 | + struct lifreq lifr; |
| 439 | +#else |
| 440 | + struct ifreq ifr; |
| 441 | +#endif |
| 442 | + |
| 443 | +#ifdef LIFNAMSIZ |
| 444 | + if (strlen(ifname) >= sizeof(lifr.lifr_name)) { |
| 445 | +#else |
| 446 | + if (strlen(ifname) >= sizeof(ifr.ifr_name)) { |
| 447 | +#endif |
| 448 | + /* |
| 449 | + * The name is too long, so it can't possibly exist. |
| 450 | + */ |
| 451 | + strlcpy(errbuf, "", PCAP_ERRBUF_SIZE); |
| 452 | + return (PCAP_ERROR_NO_SUCH_DEVICE); |
| 453 | + } |
| 454 | + |
| 455 | + /* |
| 456 | + * Try to get a socket on which to do an ioctl to get the |
| 457 | + * interface's flags. |
| 458 | + */ |
| 459 | + fd = socket(AF_INET, SOCK_DGRAM, 0); |
| 460 | + if (fd < 0) { |
| 461 | + /* That failed; report that as the error. */ |
| 462 | + pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, |
| 463 | + errno, "Can't open socket to get interface flags"); |
| 464 | + return (PCAP_ERROR); |
| 465 | + } |
| 466 | + |
| 467 | +#ifdef LIFNAMSIZ |
| 468 | + pcapint_strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name)); |
| 469 | + status = ioctl(fd, SIOCGLIFFLAGS, (char *)&lifr); |
| 470 | +#else |
| 471 | + pcapint_strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| 472 | + status = ioctl(fd, SIOCGIFFLAGS, (char *)&ifr); |
| 473 | +#endif |
| 474 | + |
| 475 | + if (status < 0) { |
| 476 | + if (errno == ENXIO || errno == EINVAL) { |
| 477 | + /* |
| 478 | + * macOS, *BSD, and Solaris return one of |
| 479 | + * those two errors if the device doesn't exist. |
| 480 | + */ |
| 481 | + strlcpy(errbuf, "", PCAP_ERRBUF_SIZE); |
| 482 | + close(fd); |
| 483 | + return (PCAP_ERROR_NO_SUCH_DEVICE); |
| 484 | + } |
| 485 | + |
| 486 | + /* |
| 487 | + * Some other error. |
| 488 | + */ |
| 489 | + pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, errno, |
| 490 | + "Can't get interface flags"); |
| 491 | + close(fd); |
| 492 | + return (PCAP_ERROR); |
| 493 | + } |
| 494 | + |
| 495 | + /* |
| 496 | + * The interface exists. |
| 497 | + */ |
| 498 | + close(fd); |
| 499 | + |
| 500 | +#ifdef HAVE_ZONE_H |
| 501 | + /* |
| 502 | + * If we're not in a global zone, the problem is probably |
| 503 | + * that the zone we're in doesn't happen to have any |
| 504 | + * DLPI devices. Exclusive-IP non-global zones have |
| 505 | + * their own interfaces and have DLPI devices for them: |
| 506 | + * |
| 507 | + * https://docs.oracle.com/cd/E37838_01/html/E61040/z.config.ov-6.html#VLZCRgekkb |
| 508 | + * |
| 509 | + * https://docs.oracle.com/cd/E19044-01/sol.containers/817-1592/geprv/index.html |
| 510 | + * |
| 511 | + * but shared-IP non-global zones don't: |
| 512 | + * |
| 513 | + * https://docs.oracle.com/cd/E37838_01/html/E61040/z.config.ov-6.html#VLZCRgekku |
| 514 | + */ |
| 515 | + if (getzoneid() != GLOBAL_ZONEID) { |
| 516 | + /* |
| 517 | + * Not a global zone; note that capturing on network |
| 518 | + * interfaces is only supported for interfaces |
| 519 | + * in an exclusive-IP zone. |
| 520 | + */ |
| 521 | + snprintf(errbuf, PCAP_ERRBUF_SIZE, |
| 522 | + "Capturing on interfaces in a non-global zone is supported only for interfaces in exclusive-IP zones"); |
| 523 | + return (PCAP_ERROR_CAPTURE_NOTSUP); |
| 524 | + } |
| 525 | +#endif /* HAVE_ZONE_H */ |
| 526 | + |
| 527 | + /* |
| 528 | + * Some other problem; just report it as not having |
| 529 | + * a DLPI device. We don't report the DLPI device |
| 530 | + * name, so people don't get confused and think, for |
| 531 | + * example, that if they can't capture on that interface |
| 532 | + * on Solaris prior to Solaris 11 the fix is to change |
| 533 | + * libpcap (or the application that uses it) to look |
| 534 | + * for something other than "/dev/{ifname}", as the fix |
| 535 | + * is to use Solaris 11 or some operating system other |
| 536 | + * than Solaris - you just *can't* capture on that |
| 537 | + * interface on Solaris prior to Solaris 11, the lack |
| 538 | + * of a DLPI device for the loopback interface is just |
| 539 | + * a symptom of that inability. |
| 540 | + */ |
| 541 | + snprintf(errbuf, PCAP_ERRBUF_SIZE, |
| 542 | + "Capturing on that interface is not supported - no DLPI device found"); |
| 543 | + return (PCAP_ERROR_CAPTURE_NOTSUP); |
| 544 | +} |
0 commit comments