bpf: pass sk to helper functions
Subsystems (4)
PATCH 1/1 bpf: pass sk to helper functions
From: Willem de Bruijn <[email protected]>
BPF helper functions access socket fields through skb->sk. This is not set in ingress cgroup and socket filters. The association is only made in skb_set_owner_r once the filter has accepted the packet. Sk is available as socket lookup has taken place.
Temporarily set skb->sk to sk in these cases.
--- kernel/bpf/cgroup.c | 4 +++- net/core/filter.c | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-)
kernel/bpf/cgroup.c +3 -1
@@ -154,7 +154,7 @@ int __cgroup_bpf_update(struct cgroup *cgrp, struct cgroup *parent,/** * __cgroup_bpf_run_filter_skb() - Run a program for packet filtering -* @sk: The socken sending or receiving traffic +* @sk: The socket sending or receiving traffic * @skb: The skb that is being sent or received * @type: The type of program to be exectuted *
@@ -190,9 +190,11 @@ int __cgroup_bpf_run_filter_skb(struct sock *sk,if (prog) {unsigned int offset = skb->data -skb_network_header (skb); +swap (skb->sk, sk);__skb_push (skb, offset); ret =bpf_prog_run_save_cb (prog, skb) ==1 ?0 : -EPERM ;__skb_pull (skb, offset); +swap (skb->sk, sk); }rcu_read_unlock ();
net/core/filter.c +5 -1
@@ -92,8 +92,12 @@ int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap)rcu_read_lock (); filter =rcu_dereference (sk->sk_filter);if (filter) { -unsigned int pkt_len =bpf_prog_run_save_cb (filter->prog, skb); +unsigned int pkt_len; + +swap (skb->sk, sk); + pkt_len =bpf_prog_run_save_cb (filter->prog, skb); err = pkt_len ?pskb_trim (skb,max (cap, pkt_len)) : -EPERM ; +swap (skb->sk, sk); }rcu_read_unlock ();
Signature · 1 line
2.12.2.715.g7642488e1d-goog
Quoted 29 lines · Willem de Bruijn
From: Willem de Bruijn <[email protected]>
BPF helper functions access socket fields through
skb->sk. This is not set in ingress cgroup and socket filters. The association is only made inskb_set_owner_ronce the filter has accepted the packet. Sk is available as socket lookup has taken place.Temporarily set
skb->skto sk in these cases.Signed-off-by Willem de Bruijn <[email protected]>--- kernel/bpf/cgroup.c | 4 +++- net/core/filter.c | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-)kernel/bpf/cgroup.c +1
-1@@ -154,7 +154,7 @@ int __cgroup_bpf_update(struct cgroup *cgrp, struct cgroup *parent,/** * __cgroup_bpf_run_filter_skb() - Run a program for packet filtering -* @sk: The socken sending or receiving traffic +* @sk: The socket sending or receiving traffic * @skb: The skb that is being sent or received * @type: The type of program to be exectuted *
kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_skb(struct sock *sk, · line 192
Quoted 3 lines
kernel/bpf/cgroup.c +0
-0@@ -190,9 +190,11 @@ int __cgroup_bpf_run_filter_skb(struct sock *sk,if (prog) {unsigned int offset = skb->data -skb_network_header (skb);
It would be nice to not touch sk. swap() is a bit overkill here I think.
struct sock *save_sk = skb->sk; skb->sk = sk;
kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_skb(struct sock *sk, · line 197
Quoted 5 lines
kernel/bpf/cgroup.c +2
-0+swap (skb->sk, sk);__skb_push (skb, offset); ret =bpf_prog_run_save_cb (prog, skb) ==1 ?0 : -EPERM ;__skb_pull (skb, offset); +swap (skb->sk, sk);
skb->sk = save_sk;
Quoted 2 lines
kernel/bpf/cgroup.c +0
-0}
Quoted 41 lines · Eric Dumazet
Quoted 29 lines · Willem de Bruijn
From: Willem de Bruijn <[email protected]>
BPF helper functions access socket fields through
skb->sk. This is not set in ingress cgroup and socket filters. The association is only made inskb_set_owner_ronce the filter has accepted the packet. Sk is available as socket lookup has taken place.Temporarily set
skb->skto sk in these cases.Signed-off-by Willem de Bruijn <[email protected]>--- kernel/bpf/cgroup.c | 4 +++- net/core/filter.c | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-)kernel/bpf/cgroup.c +1
-1@@ -154,7 +154,7 @@ int __cgroup_bpf_update(struct cgroup *cgrp, struct cgroup *parent,/** * __cgroup_bpf_run_filter_skb() - Run a program for packet filtering -* @sk: The socken sending or receiving traffic +* @sk: The socket sending or receiving traffic * @skb: The skb that is being sent or received * @type: The type of program to be exectuted * kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_skb(struct sock *sk, · line 192
Quoted 3 lines
kernel/bpf/cgroup.c +0
-0@@ -190,9 +190,11 @@ int __cgroup_bpf_run_filter_skb(struct sock *sk,if (prog) {unsigned int offset = skb->data -skb_network_header (skb);It would be nice to not touch sk.
swap()is a bit overkill here I think.struct sock *save_sk = skb->sk; skb->sk = sk;
Quoted 9 lines
kernel/bpf/cgroup.c int __cgroup_bpf_run_filter_skb(struct sock *sk, · line 197
Quoted 5 lines
kernel/bpf/cgroup.c +2
-0+swap (skb->sk, sk);__skb_push (skb, offset); ret =bpf_prog_run_save_cb (prog, skb) ==1 ?0 : -EPERM ;__skb_pull (skb, offset); +swap (skb->sk, sk);skb->sk = save_sk;Quoted 1 line
kernel/bpf/cgroup.c +0
-0}
Thanks. I'll send a v2.
Quoted 4 lines
Quoted 1 line