#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)bpf.c 8.1 (Berkeley) 6/4/93";
#else
__RCSID("$NetBSD: bpf.c,v 1.22 2022/09/03 07:45:08 tsutsui Exp $");
#endif
#endif
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/bpf.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <paths.h>
#include <unistd.h>
#include <ifaddrs.h>
#include "defs.h"
#include "pathnames.h"
static int BpfFd = -1;
static unsigned BpfLen = 0;
static u_int8_t *BpfPkt = NULL;
int
BpfOpen(void)
{
struct ifreq ifr;
u_int bufsize = 32768;
int n;
BpfFd = open(_PATH_BPF, O_RDWR);
if (BpfFd < 0) {
syslog(LOG_ERR, "bpf: no available devices: %m");
Exit(0);
}
if (ioctl(BpfFd, BIOCSBLEN, &bufsize) < 0) {
syslog(LOG_ERR, "bpf: ioctl(BIOCSBLEN,%d): %m", bufsize);
}
(void) strncpy(ifr.ifr_name, IntfName, sizeof(ifr.ifr_name));
if (ioctl(BpfFd, BIOCSETIF, (caddr_t)&ifr) < 0) {
syslog(LOG_ERR, "bpf: ioctl(BIOCSETIF,%s): %m", IntfName);
Exit(0);
}
if (ioctl(BpfFd, BIOCGDLT, (caddr_t)&n) < 0) {
syslog(LOG_ERR, "bpf: ioctl(BIOCGDLT): %m");
Exit(0);
}
if (n != DLT_EN10MB) {
syslog(LOG_ERR,"bpf: %s: data-link type %d unsupported",
IntfName, n);
Exit(0);
}
n = 1;
if (ioctl(BpfFd, BIOCIMMEDIATE, (caddr_t)&n) < 0) {
syslog(LOG_ERR, "bpf: ioctl(BIOCIMMEDIATE): %m");
Exit(0);
}
#ifdef MSG_EOR
ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2;
#endif
ifr.ifr_addr.sa_family = AF_UNSPEC;
memmove((char *)&ifr.ifr_addr.sa_data[0], &RmpMcastAddr[0],
RMP_ADDRLEN);
if (ioctl(BpfFd, BIOCPROMISC, (caddr_t)0) < 0) {
syslog(LOG_ERR, "bpf: can't set promiscuous mode: %m");
Exit(0);
}
if (ioctl(BpfFd, BIOCGBLEN, (caddr_t)&BpfLen) < 0) {
syslog(LOG_ERR, "bpf: ioctl(BIOCGBLEN): %m");
Exit(0);
}
if (BpfPkt == NULL)
BpfPkt = (u_int8_t *)malloc(BpfLen);
if (BpfPkt == NULL) {
syslog(LOG_ERR, "bpf: out of memory (%u bytes for bpfpkt)",
BpfLen);
Exit(0);
}
{
#define RMP ((struct rmp_packet *)0)
static struct bpf_insn bpf_insn[] = {
{ BPF_LD|BPF_B|BPF_ABS, 0, 0, (long)&RMP->hp_llc.dsap },
{ BPF_JMP|BPF_JEQ|BPF_K, 0, 5, IEEE_DSAP_HP },
{ BPF_LD|BPF_H|BPF_ABS, 0, 0, (long)&RMP->hp_llc.cntrl },
{ BPF_JMP|BPF_JEQ|BPF_K, 0, 3, IEEE_CNTL_HP },
{ BPF_LD|BPF_H|BPF_ABS, 0, 0, (long)&RMP->hp_llc.dxsap },
{ BPF_JMP|BPF_JEQ|BPF_K, 0, 1, HPEXT_DXSAP },
{ BPF_RET|BPF_K, 0, 0, RMP_MAX_PACKET },
{ BPF_RET|BPF_K, 0, 0, 0x0 }
};
#undef RMP
static struct bpf_program bpf_pgm = {
sizeof(bpf_insn)/sizeof(bpf_insn[0]), bpf_insn
};
if (ioctl(BpfFd, BIOCSETF, (caddr_t)&bpf_pgm) < 0) {
syslog(LOG_ERR, "bpf: ioctl(BIOCSETF): %m");
Exit(0);
}
}
return(BpfFd);
}
char *
BpfGetIntfName(char **errmsg)
{
struct ifaddrs *ifap, *ifa, *p;
int minunit, n;
char *cp;
static char device[IFNAMSIZ + 1];
static char errbuf[128] = "No Error!";
if (errmsg != NULL)
*errmsg = errbuf;
if (getifaddrs(&ifap) != 0) {
(void) strlcpy(errbuf, "bpf: getifaddrs: %m", sizeof(errbuf));
return(NULL);
}
p = NULL;
minunit = 666;
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
if ((ifa->ifa_flags & IFF_UP) == 0 ||
#ifdef IFF_LOOPBACK
(ifa->ifa_flags & IFF_LOOPBACK))
#else
(strcmp(ifa->ifa_name, "lo0") == 0))
#endif
continue;
for (cp = ifa->ifa_name; !isdigit((unsigned char)*cp); ++cp)
;
n = atoi(cp);
if (n < minunit) {
minunit = n;
p = ifa;
}
}
if (p == NULL) {
(void) strlcpy(errbuf, "bpf: no interfaces found",
sizeof(errbuf));
freeifaddrs(ifap);
return(NULL);
}
(void) strlcpy(device, p->ifa_name, sizeof(device));
freeifaddrs(ifap);
return(device);
}
int
BpfRead(RMPCONN *rconn, int doread)
{
int datlen, caplen, hdrlen;
static u_int8_t *bp = NULL, *ep = NULL;
int cc;
if (doread) {
if ((cc = read(BpfFd, (char *)BpfPkt, (int)BpfLen)) < 0) {
syslog(LOG_ERR, "bpf: read: %m");
return(0);
} else {
bp = BpfPkt;
ep = BpfPkt + cc;
}
}
#define bhp ((struct bpf_hdr *)bp)
if (bp < ep) {
datlen = bhp->bh_datalen;
caplen = bhp->bh_caplen;
hdrlen = bhp->bh_hdrlen;
if (caplen != datlen)
syslog(LOG_ERR,
"bpf: short packet dropped (%d of %d bytes)",
caplen, datlen);
else if (caplen > (int)sizeof(struct rmp_packet))
syslog(LOG_ERR, "bpf: large packet dropped (%d bytes)",
caplen);
else {
rconn->rmplen = caplen;
memmove((char *)&rconn->tstamp, (char *)&bhp->bh_tstamp,
sizeof(struct timeval));
memmove((char *)&rconn->rmp, (char *)bp + hdrlen,
caplen);
}
bp += BPF_WORDALIGN(caplen + hdrlen);
return(1);
}
#undef bhp
return(0);
}
int
BpfWrite(RMPCONN *rconn)
{
if (write(BpfFd, (char *)&rconn->rmp, rconn->rmplen) < 0) {
syslog(LOG_ERR, "write: %s: %m", EnetStr(rconn));
return(0);
}
return(1);
}
void
BpfClose(void)
{
struct ifreq ifr;
if (BpfPkt != NULL) {
free((char *)BpfPkt);
BpfPkt = NULL;
}
if (BpfFd == -1)
return;
#ifdef MSG_EOR
ifr.ifr_addr.sa_len = RMP_ADDRLEN + 2;
#endif
ifr.ifr_addr.sa_family = AF_UNSPEC;
memmove((char *)&ifr.ifr_addr.sa_data[0], &RmpMcastAddr[0],
RMP_ADDRLEN);
if (ioctl(BpfFd, SIOCDELMULTI, (caddr_t)&ifr) < 0)
(void) ioctl(BpfFd, BIOCPROMISC, (caddr_t)0);
(void) close(BpfFd);
BpfFd = -1;
}