Skip to content

Libbpf eBPF macro bpf_ntohs

v0.0.6

The bpf_ntohs macro is used to convert a 16-bit number from network byte order to host byte order.

Definition

#define ___bpf_mvb(x, b, n, m) ((__u##b)(x) << (b-(n+1)*8) >> (b-8) << (m*8))

#define ___bpf_swab16(x) ((__u16)(          \
              ___bpf_mvb(x, 16, 0, 1) | \
              ___bpf_mvb(x, 16, 1, 0)))

#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
# define __bpf_ntohs(x)         __builtin_bswap16(x)
# define __bpf_constant_ntohs(x)    ___bpf_swab16(x)
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
# define __bpf_ntohs(x)         (x)
# define __bpf_constant_ntohs(x)    (x)
#else
# error "Fix your compiler's __BYTE_ORDER__?!"
#endif

#define bpf_ntohs(x)                \
    (__builtin_constant_p(x) ?      \
     __bpf_constant_ntohs(x) : __bpf_ntohs(x))

Usage

This macro implements the analog of the ntons function from the standard C library. ntons being short for "network to host short", converts a 16-bit number (a short) from network byte order to host byte order.

The implementation checks the endianness of the host system and if the number is a compile time constant or not. If the endianness of the system we are compiling on is already in network order, the macro simply returns the number as is. Otherwise if conversion is needed, and the number is a compile time constant, the conversion is done at compile time. If the number is not a compile time constant, a compiler builtin is used to emit byte swap instructions.

Example

Only allow a socket to bind to 192.168.1.254 and port 4040.

#define SERV4_IP        0xc0a801feU /* 192.168.1.254 */
#define SERV4_PORT      4040

SEC("cgroup/bind4")
int bind_v4_prog(struct bpf_sock_addr *ctx)
{
    struct bpf_sock *sk;

    sk = ctx->sk;
    if (!sk)
        return 0;

    if (sk->family != AF_INET)
        return 0;

    if (bpf_ntohl(ctx->user_ip4) != SERV4_IP ||
        bpf_ntohs(ctx->user_port) != SERV4_PORT)
        return 0;

    return 1;
}