Skip to content

Commit fa5cb54

Browse files
zeilborkmann
authored andcommitted
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 <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent cfa3eb6 commit fa5cb54

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

net/bpf/test_run.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,27 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
449449
skb->protocol = eth_type_trans(skb, current->nsproxy->net_ns->loopback_dev);
450450
skb_reset_network_header(skb);
451451

452+
switch (skb->protocol) {
453+
case htons(ETH_P_IP):
454+
sk->sk_family = AF_INET;
455+
if (sizeof(struct iphdr) <= skb_headlen(skb)) {
456+
sk->sk_rcv_saddr = ip_hdr(skb)->saddr;
457+
sk->sk_daddr = ip_hdr(skb)->daddr;
458+
}
459+
break;
460+
#if IS_ENABLED(CONFIG_IPV6)
461+
case htons(ETH_P_IPV6):
462+
sk->sk_family = AF_INET6;
463+
if (sizeof(struct ipv6hdr) <= skb_headlen(skb)) {
464+
sk->sk_v6_rcv_saddr = ipv6_hdr(skb)->saddr;
465+
sk->sk_v6_daddr = ipv6_hdr(skb)->daddr;
466+
}
467+
break;
468+
#endif
469+
default:
470+
break;
471+
}
472+
452473
if (is_l2)
453474
__skb_push(skb, hh_len);
454475
if (is_direct_pkt_access)

0 commit comments

Comments
 (0)