OSDN Git Service

inet: Run SK_LOOKUP BPF program on socket lookup
authorJakub Sitnicki <jakub@cloudflare.com>
Fri, 17 Jul 2020 10:35:25 +0000 (12:35 +0200)
committerAlexei Starovoitov <ast@kernel.org>
Sat, 18 Jul 2020 03:18:16 +0000 (20:18 -0700)
commit1559b4aa1db443096af493c7d621dc156054babe
treeecbdcacb6be084e01d4e8fe05b39a2789d540e63
parent80b373f74f9e28b0093930a6b95c929732f02512
inet: Run SK_LOOKUP BPF program on socket lookup

Run a BPF program before looking up a listening socket on the receive path.
Program selects a listening socket to yield as result of socket lookup by
calling bpf_sk_assign() helper and returning SK_PASS code. Program can
revert its decision by assigning a NULL socket with bpf_sk_assign().

Alternatively, BPF program can also fail the lookup by returning with
SK_DROP, or let the lookup continue as usual with SK_PASS on return, when
no socket has been selected with bpf_sk_assign().

This lets the user match packets with listening sockets freely at the last
possible point on the receive path, where we know that packets are destined
for local delivery after undergoing policing, filtering, and routing.

With BPF code selecting the socket, directing packets destined to an IP
range or to a port range to a single socket becomes possible.

In case multiple programs are attached, they are run in series in the order
in which they were attached. The end result is determined from return codes
of all the programs according to following rules:

 1. If any program returned SK_PASS and selected a valid socket, the socket
    is used as result of socket lookup.
 2. If more than one program returned SK_PASS and selected a socket,
    last selection takes effect.
 3. If any program returned SK_DROP, and no program returned SK_PASS and
    selected a socket, socket lookup fails with -ECONNREFUSED.
 4. If all programs returned SK_PASS and none of them selected a socket,
    socket lookup continues to htable-based lookup.

Suggested-by: Marek Majkowski <marek@cloudflare.com>
Signed-off-by: Jakub Sitnicki <jakub@cloudflare.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200717103536.397595-5-jakub@cloudflare.com
include/linux/filter.h
kernel/bpf/net_namespace.c
net/core/filter.c
net/ipv4/inet_hashtables.c