Skip to content

Commit e076953

Browse files
author
Afshin Paydar
committed
open a non-existent interface should always return 'no such interface'
Signed-off-by: Afshin Paydar <[email protected]>
1 parent 5fc0c26 commit e076953

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

pcap-bpf.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,58 @@ bpf_open(char *errbuf)
626626
#define BPF_BIND_SUCCEEDED 0
627627
#define BPF_BIND_BUFFER_TOO_BIG 1
628628

629+
/*
630+
* Check if an interface exists without requiring special privileges.
631+
* Returns 0 if the interface exists, PCAP_ERROR_NO_SUCH_DEVICE if it doesn't,
632+
* or another negative error code on other failures.
633+
*/
634+
static int
635+
check_interface_exists(const char *name, char *errbuf)
636+
{
637+
#ifndef _WIN32
638+
int fd;
639+
struct ifreq ifr;
640+
641+
if (strlen(name) >= sizeof(ifr.ifr_name)) {
642+
/* The name is too long, so it can't possibly exist. */
643+
errbuf[0] = '\0';
644+
return PCAP_ERROR_NO_SUCH_DEVICE;
645+
}
646+
647+
fd = socket(AF_INET, SOCK_DGRAM, 0);
648+
if (fd < 0) {
649+
pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
650+
errno, "socket");
651+
return PCAP_ERROR;
652+
}
653+
654+
memset(&ifr, 0, sizeof(ifr));
655+
pcapint_strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
656+
657+
if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
658+
int save_errno = errno;
659+
close(fd);
660+
661+
if (save_errno == ENXIO || save_errno == ENODEV) {
662+
/* Interface doesn't exist */
663+
errbuf[0] = '\0';
664+
return PCAP_ERROR_NO_SUCH_DEVICE;
665+
} else {
666+
/* Some other error occurred */
667+
pcapint_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
668+
save_errno, "SIOCGIFFLAGS on %s", name);
669+
return PCAP_ERROR;
670+
}
671+
}
672+
673+
close(fd);
674+
return 0;
675+
#else
676+
/* On Windows, skip the check for now */
677+
return 0;
678+
#endif
679+
}
680+
629681
static int
630682
bpf_bind(int fd, const char *name, char *errbuf)
631683
{
@@ -1923,6 +1975,16 @@ pcap_activate_bpf(pcap_t *p)
19231975
int flags = MAP_ANON;
19241976
#endif
19251977

1978+
/*
1979+
* Check if the interface exists before trying to open BPF device.
1980+
* This avoids reporting permission errors when the real issue is
1981+
* that the interface doesn't exist.
1982+
*/
1983+
status = check_interface_exists(p->opt.device, p->errbuf);
1984+
if (status != 0) {
1985+
goto bad;
1986+
}
1987+
19261988
fd = bpf_open(p->errbuf);
19271989
if (fd < 0) {
19281990
status = fd;

pcap-linux.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5164,13 +5164,13 @@ iface_get_ts_types(const char *device, pcap_t *handle, char *ebuf)
51645164

51655165
case ENODEV:
51665166
/*
5167-
* OK, no such device.
5168-
* The user will find that out when they try to
5169-
* activate the device; just return an empty
5170-
* list of time stamp types.
5167+
* No such device.
5168+
*
5169+
* There's nothing more to say, so clear the
5170+
* error message.
51715171
*/
5172-
handle->tstamp_type_list = NULL;
5173-
return 0;
5172+
ebuf[0] = '\0';
5173+
return PCAP_ERROR_NO_SUCH_DEVICE;
51745174

51755175
default:
51765176
/*

0 commit comments

Comments
 (0)