#include <sys/cdefs.h>
#if o
static const char _ndbootd_bpf_c_rcsid[] = "<<Id: ndbootd-bpf.c,v 1.4 2001/05/23 02:35:49 fredette Exp >>";
#else
__RCSID("$NetBSD: ndbootd-bpf.c,v 1.8 2004/12/01 23:18:20 christos Exp $");
#endif
#include <sys/poll.h>
#include <net/bpf.h>
#include <paths.h>
struct _ndbootd_interface_bpf {
size_t _ndbootd_interface_bpf_buffer_size;
char *_ndbootd_interface_bpf_buffer;
size_t _ndbootd_interface_bpf_buffer_offset;
size_t _ndbootd_interface_bpf_buffer_end;
};
static struct bpf_insn ndboot_bpf_filter[] = {
BPF_STMT(BPF_LD + BPF_H + BPF_ABS, NDBOOTD_OFFSETOF(struct ether_header, ether_type)),
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 0, 9),
BPF_STMT(BPF_LD + BPF_B + BPF_ABS, sizeof(struct ether_header) + NDBOOTD_OFFSETOF(struct ip, ip_p)),
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, IPPROTO_ND, 0, 7),
BPF_STMT(BPF_LD + BPF_H + BPF_ABS, sizeof(struct ether_header) + NDBOOTD_OFFSETOF(struct ip, ip_off)),
BPF_JUMP(BPF_JMP + BPF_JSET + BPF_K, 0x3fff, 5, 0),
BPF_STMT(BPF_LD + BPF_H + BPF_ABS, sizeof(struct ether_header) + NDBOOTD_OFFSETOF(struct ip, ip_len)),
BPF_STMT(BPF_LDX + BPF_B + BPF_MSH, sizeof(struct ether_header)),
BPF_STMT(BPF_ALU + BPF_SUB + BPF_X, 0),
BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, sizeof(struct ndboot_packet), 0, 1),
BPF_STMT(BPF_RET + BPF_K, (u_int) -1),
BPF_STMT(BPF_RET + BPF_K, 0),
};
int
ndbootd_raw_open(struct ndbootd_interface * interface)
{
int network_fd;
int saved_errno;
u_int bufsize;
u_int bpf_opt;
struct bpf_version version;
u_int packet_buffer_size;
struct bpf_program program;
struct _ndbootd_interface_bpf *interface_bpf;
const char *dev_bpf_filename = _PATH_BPF;
if ((network_fd = open(dev_bpf_filename, O_RDWR)) < 0) {
_NDBOOTD_DEBUG((fp, "bpf: failed to open %s: %s", dev_bpf_filename, strerror(errno)));
return (-1);
}
_NDBOOTD_DEBUG((fp, "bpf: opened %s", dev_bpf_filename));
#define _NDBOOTD_RAW_OPEN_ERROR(x) saved_errno = errno; x; errno = saved_errno
if (ioctl(network_fd, BIOCVERSION, &version) < 0) {
_NDBOOTD_DEBUG((fp, "bpf: failed to get the BPF version on %s: %s",
dev_bpf_filename, strerror(errno)));
_NDBOOTD_RAW_OPEN_ERROR(close(network_fd));
return (-1);
}
if (version.bv_major != BPF_MAJOR_VERSION
|| version.bv_minor < BPF_MINOR_VERSION) {
_NDBOOTD_DEBUG((fp, "bpf: kernel BPF version is %d.%d, my BPF version is %d.%d",
version.bv_major, version.bv_minor,
BPF_MAJOR_VERSION, BPF_MINOR_VERSION));
close(network_fd);
errno = ENXIO;
return (-1);
}
bpf_opt = TRUE;
if (ioctl(network_fd, BIOCIMMEDIATE, &bpf_opt) < 0) {
_NDBOOTD_DEBUG((fp, "bpf: failed to put %s into immediate mode: %s",
dev_bpf_filename, strerror(errno)));
_NDBOOTD_RAW_OPEN_ERROR(close(network_fd));
return (-1);
}
bufsize = 32768;
if (ioctl(network_fd, BIOCSBLEN, &bufsize) < 0) {
_NDBOOTD_DEBUG((fp, "bpf: failed set buffer size to %d: %s",
bufsize, strerror(errno)));
}
bpf_opt = TRUE;
if (ioctl(network_fd, BIOCSHDRCMPLT, &bpf_opt) < 0) {
_NDBOOTD_DEBUG((fp, "bpf: failed to put %s into complete-headers mode: %s",
dev_bpf_filename, strerror(errno)));
_NDBOOTD_RAW_OPEN_ERROR(close(network_fd));
return (-1);
}
if (ioctl(network_fd, BIOCSETIF, interface->ndbootd_interface_ifreq) < 0) {
_NDBOOTD_DEBUG((fp, "bpf: failed to point BPF socket at %s: %s",
interface->ndbootd_interface_ifreq->ifr_name, strerror(errno)));
saved_errno = errno;
close(network_fd);
errno = saved_errno;
return (-1);
}
program.bf_len = sizeof(ndboot_bpf_filter) / sizeof(ndboot_bpf_filter[0]);
program.bf_insns = ndboot_bpf_filter;
if (ioctl(network_fd, BIOCSETF, &program) < 0) {
_NDBOOTD_DEBUG((fp, "bpf: failed to set the filter on %s: %s",
dev_bpf_filename, strerror(errno)));
_NDBOOTD_RAW_OPEN_ERROR(close(network_fd));
return (-1);
}
if (ioctl(network_fd, BIOCGBLEN, &packet_buffer_size) < 0) {
_NDBOOTD_DEBUG((fp, "bpf: failed to read the buffer size for %s: %s",
dev_bpf_filename, strerror(errno)));
_NDBOOTD_RAW_OPEN_ERROR(close(network_fd));
return (-1);
}
_NDBOOTD_DEBUG((fp, "bpf: buffer size for %s is %u",
dev_bpf_filename, packet_buffer_size));
interface->ndbootd_interface_fd = network_fd;
interface_bpf = ndbootd_new0(struct _ndbootd_interface_bpf, 1);
interface_bpf->_ndbootd_interface_bpf_buffer_size = packet_buffer_size;
interface_bpf->_ndbootd_interface_bpf_buffer = ndbootd_new(char, packet_buffer_size);
interface->_ndbootd_interface_raw_private = interface_bpf;
return (0);
#undef _NDBOOTD_RAW_OPEN_ERROR
}
int
ndbootd_raw_read(struct ndbootd_interface * interface, void *packet_buffer, size_t packet_buffer_size)
{
struct _ndbootd_interface_bpf *interface_bpf;
ssize_t buffer_end;
struct bpf_hdr the_bpf_header;
struct pollfd set[1];
interface_bpf = (struct _ndbootd_interface_bpf *) interface->_ndbootd_interface_raw_private;
set[0].fd = interface->ndbootd_interface_fd;
set[0].events = POLLIN;
for (;;) {
if (interface_bpf->_ndbootd_interface_bpf_buffer_offset
>= interface_bpf->_ndbootd_interface_bpf_buffer_end) {
_NDBOOTD_DEBUG((fp, "bpf: calling poll"));
switch (poll(set, 1, INFTIM)) {
case 0:
_NDBOOTD_DEBUG((fp, "bpf: poll returned zero"));
continue;
case 1:
break;
default:
if (errno == EINTR) {
_NDBOOTD_DEBUG((fp, "bpf: poll got EINTR"));
continue;
}
_NDBOOTD_DEBUG((fp, "bpf: poll failed: %s", strerror(errno)));
return (-1);
}
assert(set[0].revents & POLLIN);
_NDBOOTD_DEBUG((fp, "bpf: calling read"));
buffer_end = read(interface->ndbootd_interface_fd,
interface_bpf->_ndbootd_interface_bpf_buffer,
interface_bpf->_ndbootd_interface_bpf_buffer_size);
if (buffer_end <= 0) {
_NDBOOTD_DEBUG((fp, "bpf: failed to read packets: %s", strerror(errno)));
return (-1);
}
_NDBOOTD_DEBUG((fp, "bpf: read %ld bytes of packets", (long) buffer_end));
interface_bpf->_ndbootd_interface_bpf_buffer_offset = 0;
interface_bpf->_ndbootd_interface_bpf_buffer_end = buffer_end;
}
if ((interface_bpf->_ndbootd_interface_bpf_buffer_offset
+ sizeof(the_bpf_header))
> interface_bpf->_ndbootd_interface_bpf_buffer_end) {
_NDBOOTD_DEBUG((fp, "bpf: flushed garbage BPF header bytes"));
interface_bpf->_ndbootd_interface_bpf_buffer_end = 0;
continue;
}
memcpy(&the_bpf_header,
interface_bpf->_ndbootd_interface_bpf_buffer
+ interface_bpf->_ndbootd_interface_bpf_buffer_offset,
sizeof(the_bpf_header));
interface_bpf->_ndbootd_interface_bpf_buffer_offset += the_bpf_header.bh_hdrlen;
if (the_bpf_header.bh_caplen != the_bpf_header.bh_datalen
|| ((interface_bpf->_ndbootd_interface_bpf_buffer_offset + the_bpf_header.bh_datalen)
> interface_bpf->_ndbootd_interface_bpf_buffer_end)) {
_NDBOOTD_DEBUG((fp, "bpf: flushed truncated BPF packet"));
interface_bpf->_ndbootd_interface_bpf_buffer_offset += the_bpf_header.bh_datalen;
continue;
}
if (the_bpf_header.bh_datalen < sizeof(struct ether_header)
|| !memcmp(((struct ether_header *)
(interface_bpf->_ndbootd_interface_bpf_buffer
+ interface_bpf->_ndbootd_interface_bpf_buffer_offset))->ether_shost,
interface->ndbootd_interface_ether,
ETHER_ADDR_LEN)) {
interface_bpf->_ndbootd_interface_bpf_buffer_offset += the_bpf_header.bh_datalen;
continue;
}
if (packet_buffer_size < the_bpf_header.bh_datalen) {
errno = EIO;
interface_bpf->_ndbootd_interface_bpf_buffer_offset += the_bpf_header.bh_datalen;
return (-1);
}
memcpy(packet_buffer,
interface_bpf->_ndbootd_interface_bpf_buffer
+ interface_bpf->_ndbootd_interface_bpf_buffer_offset,
the_bpf_header.bh_datalen);
interface_bpf->_ndbootd_interface_bpf_buffer_offset += the_bpf_header.bh_datalen;
return (the_bpf_header.bh_datalen);
}
}
int
ndbootd_raw_write(struct ndbootd_interface * interface, void *packet_buffer, size_t packet_buffer_size)
{
return (write(interface->ndbootd_interface_fd, packet_buffer, packet_buffer_size));
}