/*
 * DF-0401 reachability probe.
 *
 * The full PoC (poc.c) requires the netmap userland headers installed at
 * /usr/include/net/netmap{,_user}.h plus a live /dev/netmap device node
 * backed by a loaded netmap.ko. On DragonFlyBSD master DEV (commit
 * 6cc80ee9) NONE of these exist:
 *   - struct ifnet no longer has if_unused7, so netmap_kern.h:747's WNA
 *     macro fails to compile -> netmap.ko cannot be built (see
 *     netmap_build_attempt.log).
 *   - No netmap.ko is shipped in /boot/kernel/.
 *   - kldload netmap fails ("No such file or directory").
 *   - /dev/netmap device node is never created.
 *   - The userland UAPI headers are not installed in /usr/include.
 *
 * This probe makes the unreachability direct and self-evident: it just
 * tries to open /dev/netmap. ENOENT proves the entire bug path is dead
 * on this kernel.
 *
 * Build:  cc -O2 -Wall -o reachability_probe reachability_probe.c
 * Run:    ./reachability_probe
 */
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>

int main(void)
{
    struct stat st;
    int fd;

    printf("DF-0401 reachability probe\n");
    printf("--------------------------\n");

    if (stat("/dev/netmap", &st) != 0) {
        printf("[UNREACHABLE] /dev/netmap does not exist: %s\n",
               strerror(errno));
        printf("[UNREACHABLE] netmap subsystem is not loaded/available.\n");
        printf("[UNREACHABLE] The VALE forwarding path (nm_bdg_preflush/\n");
        printf("              nm_bdg_flush) cannot be entered live.\n");
        return 2;
    }
    printf("[INFO] /dev/netmap exists (mode 0%o, uid %d, gid %d)\n",
           st.st_mode & 0777, st.st_uid, st.st_gid);

    fd = open("/dev/netmap", O_RDWR);
    if (fd < 0) {
        printf("[BLOCKED] open /dev/netmap failed: %s\n", strerror(errno));
        printf("[BLOCKED] unprivileged user cannot access netmap -> bug\n");
        printf("          path reachable only as root/wheel.\n");
        return 3;
    }
    printf("[REACHABLE] /dev/netmap opened as unprivileged user.\n");
    printf("            Run ./poc to exercise the VALE overflow.\n");
    close(fd);
    return 0;
}
