Why I don't receive any packets using BPF with udp socket?

1

GOAL: write a BPF filter which allow just UDP packets from a specific src address and attach it to and UDP socket.

PROBLEM: if I execute the program and I try to send udp packets from a VM which has the correct src IP I don't receive none of them

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>

#include <arpa/inet.h>
#include <linux/filter.h>

/* udp and src 192.168.56.101 */
struct sock_filter bpfcode[] = {
    { 0x28, 0, 0, 0x0000000c },
    { 0x15, 6, 0, 0x000086dd },
    { 0x15, 0, 5, 0x00000800 },
    { 0x30, 0, 0, 0x00000017 },
    { 0x15, 0, 3, 0x00000011 },
    { 0x20, 0, 0, 0x0000001a },
    { 0x15, 0, 1, 0xc0a83865 },
    { 0x6,  0, 0, 0x00040000 },
    { 0x6,  0, 0, 0x00000000 },
};

int main(void)
{
    struct sock_fprog bpf = {
        sizeof(bpfcode) / sizeof(struct sock_filter),
        bpfcode
    };
    struct sockaddr_in src = {
        .sin_family = AF_INET,
        .sin_addr.s_addr = INADDR_ANY,
        .sin_port = htons(1025)
    };
    char buf[1024];
    ssize_t res;
    int fd, ret;

    fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (fd < 0) {
        printf("error: socket\n");
        exit(EXIT_FAILURE);
    }

    ret = setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf));
    if (ret < 0) {
        perror("error: setsockopt\n");
        close(fd);
        exit(EXIT_FAILURE);
    }

    ret = bind(fd, (struct sockaddr *)&src, sizeof(src));
    if (ret < 0) {
        printf("error: bind\n");
        close(fd);
        exit(EXIT_FAILURE);
    }

    res = recvfrom(fd, buf, sizeof(buf), 0, NULL, 0);
    printf("res = %zi\n", res);

    close(fd);

    return 0;
}
c
sockets
udp
bpf
asked on Stack Overflow Jul 25, 2019 by Maicake • edited Jul 25, 2019 by red0ct

1 Answer

2

Summary from the discussion in the comments:

First, the socket's type is SOCK_DGRAM, so the data you get starts at L4 (UDP), not at L2 as your filter expects. Use a SOCK_RAW instead.

Then, using SOCK_RAW will give you access to L3, not L2 (you'd need to change the socket domain for that). So you need to adapt your filter somewhat:

    { 0x28, 0, 0, 0x0000000c }, // load Ethertype
    { 0x15, 6, 0, 0x000086dd }, // If IPv6 goto drop
    { 0x15, 0, 5, 0x00000800 }, // If not IPv4 (and not IPv6) goto drop
    { 0x30, 0, 0, 0x00000017 }, // Load IP protocol
    { 0x15, 0, 3, 0x00000011 }, // If not UDP goto drop
    { 0x20, 0, 0, 0x0000001a }, // Load src address
    { 0x15, 0, 1, 0xc0a83865 }, // If not 192.168.56.1.1 goto drop
    { 0x6,  0, 0, 0x00040000 }, // Pass packet
    { 0x6,  0, 0, 0x00000000 }, // Drop

Should become (credits to OP who fixed it on their own :) ):

    // UDP check is harmless but useless
    // { 0x30, 0, 0, 0x00000009 }, // Note the offset update
    // { 0x15, 0, 3, 0x00000011 },
    { 0x20, 0, 0, 0x0000000c }, // Note the offset update
    { 0x15, 0, 1, 0xc0a83865 },
    { 0x6,  0, 0, 0x00040000 },
    { 0x6,  0, 0, 0x00000000 },
answered on Stack Overflow Jul 25, 2019 by Qeole

User contributions licensed under CC BY-SA 3.0