Skip to content

Commit fecef4c

Browse files
Sebastian Andrzej Siewiorkuba-moo
authored andcommitted
tun: Assign missing bpf_net_context.
During the introduction of struct bpf_net_context handling for XDP-redirect, the tun driver has been missed. Jakub also pointed out that there is another call chain to do_xdp_generic() originating from netif_receive_skb() and drivers may use it outside from the NAPI context. Set the bpf_net_context before invoking BPF XDP program within the TUN driver. Set the bpf_net_context also in do_xdp_generic() if a xdp program is available. Reported-by: [email protected] Reported-by: [email protected] Reported-by: [email protected] Fixes: 401cb7d ("net: Reference bpf_redirect_info via task_struct on PREEMPT_RT.") Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 3b2aef9 commit fecef4c

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

drivers/net/tun.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,6 +1661,7 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun,
16611661
int len, int *skb_xdp)
16621662
{
16631663
struct page_frag *alloc_frag = &current->task_frag;
1664+
struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
16641665
struct bpf_prog *xdp_prog;
16651666
int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
16661667
char *buf;
@@ -1700,6 +1701,7 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun,
17001701

17011702
local_bh_disable();
17021703
rcu_read_lock();
1704+
bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
17031705
xdp_prog = rcu_dereference(tun->xdp_prog);
17041706
if (xdp_prog) {
17051707
struct xdp_buff xdp;
@@ -1728,12 +1730,14 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun,
17281730
pad = xdp.data - xdp.data_hard_start;
17291731
len = xdp.data_end - xdp.data;
17301732
}
1733+
bpf_net_ctx_clear(bpf_net_ctx);
17311734
rcu_read_unlock();
17321735
local_bh_enable();
17331736

17341737
return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, pad);
17351738

17361739
out:
1740+
bpf_net_ctx_clear(bpf_net_ctx);
17371741
rcu_read_unlock();
17381742
local_bh_enable();
17391743
return NULL;
@@ -2566,6 +2570,7 @@ static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
25662570

25672571
if (m->msg_controllen == sizeof(struct tun_msg_ctl) &&
25682572
ctl && ctl->type == TUN_MSG_PTR) {
2573+
struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
25692574
struct tun_page tpage;
25702575
int n = ctl->num;
25712576
int flush = 0, queued = 0;
@@ -2574,6 +2579,7 @@ static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
25742579

25752580
local_bh_disable();
25762581
rcu_read_lock();
2582+
bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
25772583

25782584
for (i = 0; i < n; i++) {
25792585
xdp = &((struct xdp_buff *)ctl->ptr)[i];
@@ -2588,6 +2594,7 @@ static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
25882594
if (tfile->napi_enabled && queued > 0)
25892595
napi_schedule(&tfile->napi);
25902596

2597+
bpf_net_ctx_clear(bpf_net_ctx);
25912598
rcu_read_unlock();
25922599
local_bh_enable();
25932600

net/core/dev.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5126,11 +5126,14 @@ static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key);
51265126

51275127
int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff **pskb)
51285128
{
5129+
struct bpf_net_context __bpf_net_ctx, *bpf_net_ctx;
5130+
51295131
if (xdp_prog) {
51305132
struct xdp_buff xdp;
51315133
u32 act;
51325134
int err;
51335135

5136+
bpf_net_ctx = bpf_net_ctx_set(&__bpf_net_ctx);
51345137
act = netif_receive_generic_xdp(pskb, &xdp, xdp_prog);
51355138
if (act != XDP_PASS) {
51365139
switch (act) {
@@ -5144,11 +5147,13 @@ int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff **pskb)
51445147
generic_xdp_tx(*pskb, xdp_prog);
51455148
break;
51465149
}
5150+
bpf_net_ctx_clear(bpf_net_ctx);
51475151
return XDP_DROP;
51485152
}
51495153
}
51505154
return XDP_PASS;
51515155
out_redir:
5156+
bpf_net_ctx_clear(bpf_net_ctx);
51525157
kfree_skb_reason(*pskb, SKB_DROP_REASON_XDP);
51535158
return XDP_DROP;
51545159
}

0 commit comments

Comments
 (0)