bpf, testing: Add get{peer, sock}name selftests to test_progs
Extend the existing connect_force_port test to assert get{peer,sock}name programs as well. The workflow for e.g. IPv4 is as follows: i) server binds to concrete port, ii) client calls getsockname() on server fd which exposes 1.2.3.4:60000 to client, iii) client connects to service address 1.2.3.4:60000 binds to concrete local address (127.0.0.1:22222) and remaps service address to a concrete backend address (127.0.0.1:60123), iv) client then calls getsockname() on its own fd to verify local address (127.0.0.1:22222) and getpeername() on its own fd which then publishes service address (1.2.3.4:60000) instead of actual backend. Same workflow is done for IPv6 just with different address/port tuples. # ./test_progs -t connect_force_port #14 connect_force_port:OK Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Andrii Nakryiko <andriin@fb.com> Acked-by: Andrey Ignatov <rdna@fb.com> Link: https://lore.kernel.org/bpf/3343da6ad08df81af715a95d61a84fb4a960f2bf.1589841594.git.daniel@iogearbox.net
This commit is contained in:

committed by
Alexei Starovoitov

parent
05ee19c18c
commit
566fc3f5d1
@@ -1,5 +1,6 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <linux/bpf.h>
|
||||
#include <linux/in.h>
|
||||
@@ -12,17 +13,71 @@
|
||||
char _license[] SEC("license") = "GPL";
|
||||
int _version SEC("version") = 1;
|
||||
|
||||
struct svc_addr {
|
||||
__be32 addr;
|
||||
__be16 port;
|
||||
};
|
||||
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_SK_STORAGE);
|
||||
__uint(map_flags, BPF_F_NO_PREALLOC);
|
||||
__type(key, int);
|
||||
__type(value, struct svc_addr);
|
||||
} service_mapping SEC(".maps");
|
||||
|
||||
SEC("cgroup/connect4")
|
||||
int _connect4(struct bpf_sock_addr *ctx)
|
||||
int connect4(struct bpf_sock_addr *ctx)
|
||||
{
|
||||
struct sockaddr_in sa = {};
|
||||
struct svc_addr *orig;
|
||||
|
||||
/* Force local address to 127.0.0.1:22222. */
|
||||
sa.sin_family = AF_INET;
|
||||
sa.sin_port = bpf_htons(22222);
|
||||
sa.sin_addr.s_addr = bpf_htonl(0x7f000001); /* 127.0.0.1 */
|
||||
sa.sin_addr.s_addr = bpf_htonl(0x7f000001);
|
||||
|
||||
if (bpf_bind(ctx, (struct sockaddr *)&sa, sizeof(sa)) != 0)
|
||||
return 0;
|
||||
|
||||
/* Rewire service 1.2.3.4:60000 to backend 127.0.0.1:60123. */
|
||||
if (ctx->user_port == bpf_htons(60000)) {
|
||||
orig = bpf_sk_storage_get(&service_mapping, ctx->sk, 0,
|
||||
BPF_SK_STORAGE_GET_F_CREATE);
|
||||
if (!orig)
|
||||
return 0;
|
||||
|
||||
orig->addr = ctx->user_ip4;
|
||||
orig->port = ctx->user_port;
|
||||
|
||||
ctx->user_ip4 = bpf_htonl(0x7f000001);
|
||||
ctx->user_port = bpf_htons(60123);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
SEC("cgroup/getsockname4")
|
||||
int getsockname4(struct bpf_sock_addr *ctx)
|
||||
{
|
||||
/* Expose local server as 1.2.3.4:60000 to client. */
|
||||
if (ctx->user_port == bpf_htons(60123)) {
|
||||
ctx->user_ip4 = bpf_htonl(0x01020304);
|
||||
ctx->user_port = bpf_htons(60000);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
SEC("cgroup/getpeername4")
|
||||
int getpeername4(struct bpf_sock_addr *ctx)
|
||||
{
|
||||
struct svc_addr *orig;
|
||||
|
||||
/* Expose service 1.2.3.4:60000 as peer instead of backend. */
|
||||
if (ctx->user_port == bpf_htons(60123)) {
|
||||
orig = bpf_sk_storage_get(&service_mapping, ctx->sk, 0, 0);
|
||||
if (orig) {
|
||||
ctx->user_ip4 = orig->addr;
|
||||
ctx->user_port = orig->port;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
@@ -12,17 +12,83 @@
|
||||
char _license[] SEC("license") = "GPL";
|
||||
int _version SEC("version") = 1;
|
||||
|
||||
struct svc_addr {
|
||||
__be32 addr[4];
|
||||
__be16 port;
|
||||
};
|
||||
|
||||
struct {
|
||||
__uint(type, BPF_MAP_TYPE_SK_STORAGE);
|
||||
__uint(map_flags, BPF_F_NO_PREALLOC);
|
||||
__type(key, int);
|
||||
__type(value, struct svc_addr);
|
||||
} service_mapping SEC(".maps");
|
||||
|
||||
SEC("cgroup/connect6")
|
||||
int _connect6(struct bpf_sock_addr *ctx)
|
||||
int connect6(struct bpf_sock_addr *ctx)
|
||||
{
|
||||
struct sockaddr_in6 sa = {};
|
||||
struct svc_addr *orig;
|
||||
|
||||
/* Force local address to [::1]:22223. */
|
||||
sa.sin6_family = AF_INET6;
|
||||
sa.sin6_port = bpf_htons(22223);
|
||||
sa.sin6_addr.s6_addr32[3] = bpf_htonl(1); /* ::1 */
|
||||
sa.sin6_addr.s6_addr32[3] = bpf_htonl(1);
|
||||
|
||||
if (bpf_bind(ctx, (struct sockaddr *)&sa, sizeof(sa)) != 0)
|
||||
return 0;
|
||||
|
||||
/* Rewire service [fc00::1]:60000 to backend [::1]:60124. */
|
||||
if (ctx->user_port == bpf_htons(60000)) {
|
||||
orig = bpf_sk_storage_get(&service_mapping, ctx->sk, 0,
|
||||
BPF_SK_STORAGE_GET_F_CREATE);
|
||||
if (!orig)
|
||||
return 0;
|
||||
|
||||
orig->addr[0] = ctx->user_ip6[0];
|
||||
orig->addr[1] = ctx->user_ip6[1];
|
||||
orig->addr[2] = ctx->user_ip6[2];
|
||||
orig->addr[3] = ctx->user_ip6[3];
|
||||
orig->port = ctx->user_port;
|
||||
|
||||
ctx->user_ip6[0] = 0;
|
||||
ctx->user_ip6[1] = 0;
|
||||
ctx->user_ip6[2] = 0;
|
||||
ctx->user_ip6[3] = bpf_htonl(1);
|
||||
ctx->user_port = bpf_htons(60124);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
SEC("cgroup/getsockname6")
|
||||
int getsockname6(struct bpf_sock_addr *ctx)
|
||||
{
|
||||
/* Expose local server as [fc00::1]:60000 to client. */
|
||||
if (ctx->user_port == bpf_htons(60124)) {
|
||||
ctx->user_ip6[0] = bpf_htonl(0xfc000000);
|
||||
ctx->user_ip6[1] = 0;
|
||||
ctx->user_ip6[2] = 0;
|
||||
ctx->user_ip6[3] = bpf_htonl(1);
|
||||
ctx->user_port = bpf_htons(60000);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
SEC("cgroup/getpeername6")
|
||||
int getpeername6(struct bpf_sock_addr *ctx)
|
||||
{
|
||||
struct svc_addr *orig;
|
||||
|
||||
/* Expose service [fc00::1]:60000 as peer instead of backend. */
|
||||
if (ctx->user_port == bpf_htons(60124)) {
|
||||
orig = bpf_sk_storage_get(&service_mapping, ctx->sk, 0, 0);
|
||||
if (orig) {
|
||||
ctx->user_ip6[0] = orig->addr[0];
|
||||
ctx->user_ip6[1] = orig->addr[1];
|
||||
ctx->user_ip6[2] = orig->addr[2];
|
||||
ctx->user_ip6[3] = orig->addr[3];
|
||||
ctx->user_port = orig->port;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
Reference in New Issue
Block a user