DragonFlyBSD Kernel Audit
DF-0428 / setup_pfsync.c
← back to finding ↓ download raw
/*
 * setup_pfsync.c — configure pfsync0 with a syncdev / syncpeer / maxupdates
 * via the SIOCSETPFSYNC ioctl (DragonFly's ifconfig does not expose it).
 *
 * Usage:  setup_pfsync <syncdev> [syncpeer-addr] [maxupdates]
 *   syncpeer-addr defaults to the pfsync multicast group 224.0.0.240
 *
 * Build: cc -o setup_pfsync setup_pfsync.c
 * Run:   ./setup_pfsync vtnet0            # multicast peer (default)
 *        ./setup_pfsync vtnet0 10.0.2.20  # unicast peer
 */
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/pf/if_pfsync.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <err.h>

int
main(int argc, char **argv)
{
    struct pfsyncreq preq;
    struct ifreq ifr;
    int s;

    if (argc < 2 || argc > 4) {
        fprintf(stderr, "usage: %s <syncdev> [syncpeer-addr] [maxupdates]\n",
                argv[0]);
        return 2;
    }

    memset(&preq, 0, sizeof(preq));
    strlcpy(preq.pfsyncr_syncdev, argv[1], sizeof(preq.pfsyncr_syncdev));

    /* default peer = pfsync multicast group 224.0.0.240 */
    if (inet_pton(AF_INET,
                  (argc >= 3) ? argv[2] : "224.0.0.240",
                  &preq.pfsyncr_syncpeer) != 1)
        errx(1, "inet_pton peer");
    preq.pfsyncr_maxupdates = (argc >= 4) ? atoi(argv[3]) : 128;

    s = socket(AF_INET, SOCK_DGRAM, 0);
    if (s < 0) err(1, "socket");

    memset(&ifr, 0, sizeof(ifr));
    strlcpy(ifr.ifr_name, "pfsync0", sizeof(ifr.ifr_name));
    ifr.ifr_data = (caddr_t)&preq;

    if (ioctl(s, SIOCSETPFSYNC, &ifr) < 0)
        err(1, "SIOCSETPFSYNC");

    /* bring it up */
    if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) err(1, "SIOCGIFFLAGS");
    ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
    if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) err(1, "SIOCSIFFLAGS");

    close(s);
    printf("pfsync0 configured: syncdev=%s peer=%s maxupd=%d\n",
           preq.pfsyncr_syncdev,
           (argc >= 3) ? argv[2] : "224.0.0.240",
           preq.pfsyncr_maxupdates);
    return 0;
}