summaryrefslogtreecommitdiff
path: root/net/bpf
diff options
context:
space:
mode:
authorDmitry Yakunin <zeil@yandex-team.ru>2020-08-03 12:05:44 +0300
committerDaniel Borkmann <daniel@iogearbox.net>2020-08-03 23:31:59 +0200
commitfa5cb548ced61b9d3095f32f8a7e427a248c65ee (patch)
tree04e21e7068cb009498667e619e213bb90ebea164 /net/bpf
parentcfa3eb65a7d6da5664d4c9275fbb568a2446d6d9 (diff)
bpf: Setup socket family and addresses in bpf_prog_test_run_skb
Now it's impossible to test all branches of cgroup_skb bpf program which accesses skb->family and skb->{local,remote}_ip{4,6} fields because they are zeroed during socket allocation. This commit fills socket family and addresses from related fields in constructed skb. Signed-off-by: Dmitry Yakunin <zeil@yandex-team.ru> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/bpf/20200803090545.82046-2-zeil@yandex-team.ru
Diffstat (limited to 'net/bpf')
-rw-r--r--net/bpf/test_run.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
index b03c469cd01f..736a5964fa95 100644
--- a/net/bpf/test_run.c
+++ b/net/bpf/test_run.c
@@ -449,6 +449,27 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
skb->protocol = eth_type_trans(skb, current->nsproxy->net_ns->loopback_dev);
skb_reset_network_header(skb);
+ switch (skb->protocol) {
+ case htons(ETH_P_IP):
+ sk->sk_family = AF_INET;
+ if (sizeof(struct iphdr) <= skb_headlen(skb)) {
+ sk->sk_rcv_saddr = ip_hdr(skb)->saddr;
+ sk->sk_daddr = ip_hdr(skb)->daddr;
+ }
+ break;
+#if IS_ENABLED(CONFIG_IPV6)
+ case htons(ETH_P_IPV6):
+ sk->sk_family = AF_INET6;
+ if (sizeof(struct ipv6hdr) <= skb_headlen(skb)) {
+ sk->sk_v6_rcv_saddr = ipv6_hdr(skb)->saddr;
+ sk->sk_v6_daddr = ipv6_hdr(skb)->daddr;
+ }
+ break;
+#endif
+ default:
+ break;
+ }
+
if (is_l2)
__skb_push(skb, hh_len);
if (is_direct_pkt_access)