Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 20 Jun 1995 22:13:34 +0900
From:      Inoguchi <inoguti@osa.dec.com>
Message-ID:  <9506201313.AA25735@osou15.osa.dec.com>

next in thread | raw e-mail | index | archive | help
I forgot to put with sample code.
Thanks / Kinichiro Inoguchi
-----------------------------------------------------------------------

Hello, I have a question about BSD packet filter.
I'm using RELEASE 2.0.5, now.

I want to read/write from/to bpf.
I could read from bpf, but can't write !

I tested again and again, but can't put packet to Ethernet.
write() system call didn't return error code until buffer was full.

It seems that everything I wrote to bpf stayed in system buffer.

Please tell me !

Kinichiro Inoguchi

------------------------------------------------------------------------
#include <stdio.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/bpf.h>

#define ETHERTYPE_DLI	0x6006

	struct bpf_insn insns[] = {
	    BPF_STMT(BPF_LD+BPF_H+BPF_ABS, 12),
	    BPF_JUMP(BPF_JMP+BPF_JEQ+BPF_K, ETHERTYPE_DLI, 0, 1),
	    BPF_STMT(BPF_RET+BPF_K, (u_int)-1),
	    BPF_STMT(BPF_RET+BPF_K, 0),
	};


int main() {

	int ists;
	int fd;
	struct bpf_program bpfprog;
	unsigned short sval;
	struct ifreq ifReq;
	u_int flag;
	int bufsiz;
	char *buf;
	unsigned char sim_addr[6] = {0xaa, 0x00, 0x04, 0x00, 0x96, 0xff};
	unsigned char osoi_addr[6] = {0x08, 0x00, 0x2b, 0x3c, 0xfd, 0x6d};

	/* open device */
	if((fd = open("/dev/bpf0", O_WRONLY)) < 0) {
	    perror("open");
	    exit(0);
	}

	/* binding */
	memset(&ifReq, 0, sizeof(struct ifreq));
	strcpy(ifReq.ifr_name, "le0");
	if(ioctl(fd, BIOCSETIF, &ifReq)) {
	    perror("setif");
	    exit(0);
	}

	flag = 1;
	if(ioctl(fd, BIOCIMMEDIATE, &flag) < 0) {
		perror("immediate");
		exit(0);
	}

	if(ioctl(fd, BIOCPROMISC) < 0) {
		perror("promisc");
		exit(0);
	}

	/* filter settings */
	bpfprog.bf_len = sizeof(insns) / sizeof(struct bpf_insn);
	bpfprog.bf_insns = insns;
	if(ioctl(fd, BIOCSETF, &bpfprog) < 0) {
		perror("SETF");
		exit(0);
	}

	if(ioctl(fd, BIOCGBLEN, &bufsiz) < 0) {
		perror("gblen");
		exit(0);
	}

	buf = (char *) malloc(bufsiz);
	memset(buf, 0, bufsiz);

	/* create packet to send */
	memmove(&buf[0], sim_addr, 6);
	memmove(&buf[6], osoi_addr, 6);
	sval = htons(0x6006);
	memmove(&buf[12], &sval, 2);
	sval = 2;
	memmove(&buf[14], &sval, 2);

	/* send packet */
	ists = write(fd, buf, 18);
	if(ists < 0) {
		perror("write");
	}
}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?9506201313.AA25735>