2024-08-16



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <bpf/bpf.h>
#include <bpf/libbpf.h>
 
#define MAX_IFACES 16
#define BUF_SIZE 65535
 
int main(int argc, char **argv) {
    char buf[BUF_SIZE];
    struct bpf_prog_load_attr prog_load_attr = {
        .file = "mini_tcpdump.o",
        .prog_type = BPF_PROG_TYPE_SOCKET_FILTER,
    };
    struct bpf_object *obj;
    struct bpf_prog_info info = {};
    bpf_u_int32 netmask;
    char iface[IFNAMSIZ];
    int fd, err, i, ifindex;
    unsigned int iface_count = 0;
    struct ifreq ifr;
    struct sockaddr_ll sll;
 
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <interface>\n", argv[0]);
        return 1;
    }
 
    strncpy(iface, argv[1], IFNAMSIZ - 1);
 
    if ((err = bpf_prog_load_xattr(&prog_load_attr, &obj, &fd))) {
        fprintf(stderr, "bpf_prog_load_xattr: %s\n", strerror(err));
        return 1;
    }
 
    if (bpf_obj_get_info(bpf_program__fd(prog_load_attr.prog), &info, sizeof(info))) {
        fprintf(stderr, "bpf_obj_get_info failed\n");
        return 1;
    }
 
    ifindex = if_nametoindex(iface);
    if (!ifindex) {
        perror("if_nametoindex");
        return 1;
    }
 
    memset(&ifr, 0, sizeof(ifr));
    strncpy(ifr.ifr_name, iface, IFNAMSIZ - 1);
 
    if (ioctl(fd, BIOCSETIF, &ifr) < 0) {
        perror("ioctl(BIOCSETIF)");
        return 1;
    }
 
    netmask = (bpf_u_int32)-1;
    if (ioctl(fd, BIOCSETWF, &netmask) < 0) {
        perror("ioctl(BIOCSETWF)");
        return 1;
    }
 
    memset(&sll, 0, sizeof(sll));
    sll.sll_family = AF_PACKET;
    sll.sll_ifindex = ifindex;
    sll.sll_protocol = htons(ETH_P_IP);
 
    if (bind(fd, (struct sockaddr *)&sll, sizeof(sll)) < 0) {
        perror("bind");
        return 1;
    }
 
    while (1) {
        int len = read(fd, buf, sizeof(buf));
        if (len < 0) {
            if (errno == EAGAIN)
                continue;
            perror("read");
            break;
        }
 
        // 处理读取到的数据包
        // ...
    }
 
    close(fd);
    return 0;
}

这段代码示例展示了如何加载一个BPF程序,并将其绑定到一个网络接口上,然后读取并处理数据包。它使用了BPF系统调用和socket编程接口,并且提供了一个简单的错误处理框架。这是一个基于Linux BPF JIT的实际应用示例,它可以作为开发