/* Returns an error code on failure (always negative), and an FD for the now-bound BPF device on success (always non-negative).  */
static int bpf_open_and_bind(const char *name, char *errbuf)
/* Returns an error code on failure (always negative), and an FD for the now-bound BPF device on success (always non-negative).  */
static int bpf_open_and_bind(const char *name, char *errbuf)
{
	int fd;
	struct ifreq ifr;
	fd = bpf_open(errbuf);
	if (fd < 0)
		return (fd);
	(void)strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
	if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) {
		switch (errno) {
		case ENXIO:
			close(fd);
			return (PCAP_ERROR_NO_SUCH_DEVICE);
		case ENETDOWN:
			close(fd);
			return (PCAP_ERROR_IFACE_NOT_UP);
		default:
			pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE,
			    errno, "BIOCSETIF: %s", name);
			close(fd);
			return (PCAP_ERROR);
		}
	}
	return (fd);
}