Skip to content

Commit 151516f

Browse files
committed
Merge branch 'sockmap-fixes'
John Fastabend says: ==================== net: sockmap fixes Last two fixes (as far as I know) for sockmap code this round. First, we are using the qdisc cb structure when making the data end calculation. This is really just wrong so, store it with the other metadata in the correct tcp_skb_cb sturct to avoid breaking things. Next, with recent work to attach multiple programs to a cgroup a specific enumeration of return codes was agreed upon. However, I wrote the sk_skb program types before seeing this work and used a different convention. Patch 2 in the series aligns the return codes to avoid breaking with this infrastructure and also aligns with other programming conventions to avoid being the odd duck out forcing programs to remember SK_SKB programs are different. Pusing to net because its a user visible change. With this SK_SKB program return codes are the same as other cgroup program types. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents dea6e19 + bfa6407 commit 151516f

5 files changed

Lines changed: 47 additions & 11 deletions

File tree

include/net/tcp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,7 @@ struct tcp_skb_cb {
844844
__u32 key;
845845
__u32 flags;
846846
struct bpf_map *map;
847+
void *data_end;
847848
} bpf;
848849
};
849850
};

include/uapi/linux/bpf.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ union bpf_attr {
575575
* @map: pointer to sockmap
576576
* @key: key to lookup sock in map
577577
* @flags: reserved for future use
578-
* Return: SK_REDIRECT
578+
* Return: SK_PASS
579579
*
580580
* int bpf_sock_map_update(skops, map, key, flags)
581581
* @skops: pointer to bpf_sock_ops
@@ -786,8 +786,8 @@ struct xdp_md {
786786
};
787787

788788
enum sk_action {
789-
SK_ABORTED = 0,
790-
SK_DROP,
789+
SK_DROP = 0,
790+
SK_PASS,
791791
SK_REDIRECT,
792792
};
793793

kernel/bpf/sockmap.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ static inline struct smap_psock *smap_psock_sk(const struct sock *sk)
9393
return rcu_dereference_sk_user_data(sk);
9494
}
9595

96+
/* compute the linear packet data range [data, data_end) for skb when
97+
* sk_skb type programs are in use.
98+
*/
99+
static inline void bpf_compute_data_end_sk_skb(struct sk_buff *skb)
100+
{
101+
TCP_SKB_CB(skb)->bpf.data_end = skb->data + skb_headlen(skb);
102+
}
103+
96104
static int smap_verdict_func(struct smap_psock *psock, struct sk_buff *skb)
97105
{
98106
struct bpf_prog *prog = READ_ONCE(psock->bpf_verdict);
@@ -108,13 +116,14 @@ static int smap_verdict_func(struct smap_psock *psock, struct sk_buff *skb)
108116
*/
109117
TCP_SKB_CB(skb)->bpf.map = NULL;
110118
skb->sk = psock->sock;
111-
bpf_compute_data_end(skb);
119+
bpf_compute_data_end_sk_skb(skb);
112120
preempt_disable();
113121
rc = (*prog->bpf_func)(skb, prog->insnsi);
114122
preempt_enable();
115123
skb->sk = NULL;
116124

117-
return rc;
125+
return rc == SK_PASS ?
126+
(TCP_SKB_CB(skb)->bpf.map ? SK_REDIRECT : SK_PASS) : SK_DROP;
118127
}
119128

120129
static void smap_do_verdict(struct smap_psock *psock, struct sk_buff *skb)
@@ -368,7 +377,7 @@ static int smap_parse_func_strparser(struct strparser *strp,
368377
* any socket yet.
369378
*/
370379
skb->sk = psock->sock;
371-
bpf_compute_data_end(skb);
380+
bpf_compute_data_end_sk_skb(skb);
372381
rc = (*prog->bpf_func)(skb, prog->insnsi);
373382
skb->sk = NULL;
374383
rcu_read_unlock();

net/core/filter.c

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,14 +1844,15 @@ BPF_CALL_4(bpf_sk_redirect_map, struct sk_buff *, skb,
18441844
{
18451845
struct tcp_skb_cb *tcb = TCP_SKB_CB(skb);
18461846

1847+
/* If user passes invalid input drop the packet. */
18471848
if (unlikely(flags))
1848-
return SK_ABORTED;
1849+
return SK_DROP;
18491850

18501851
tcb->bpf.key = key;
18511852
tcb->bpf.flags = flags;
18521853
tcb->bpf.map = map;
18531854

1854-
return SK_REDIRECT;
1855+
return SK_PASS;
18551856
}
18561857

18571858
struct sock *do_sk_redirect_map(struct sk_buff *skb)
@@ -4243,6 +4244,31 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,
42434244
return insn - insn_buf;
42444245
}
42454246

4247+
static u32 sk_skb_convert_ctx_access(enum bpf_access_type type,
4248+
const struct bpf_insn *si,
4249+
struct bpf_insn *insn_buf,
4250+
struct bpf_prog *prog, u32 *target_size)
4251+
{
4252+
struct bpf_insn *insn = insn_buf;
4253+
int off;
4254+
4255+
switch (si->off) {
4256+
case offsetof(struct __sk_buff, data_end):
4257+
off = si->off;
4258+
off -= offsetof(struct __sk_buff, data_end);
4259+
off += offsetof(struct sk_buff, cb);
4260+
off += offsetof(struct tcp_skb_cb, bpf.data_end);
4261+
*insn++ = BPF_LDX_MEM(BPF_SIZEOF(void *), si->dst_reg,
4262+
si->src_reg, off);
4263+
break;
4264+
default:
4265+
return bpf_convert_ctx_access(type, si, insn_buf, prog,
4266+
target_size);
4267+
}
4268+
4269+
return insn - insn_buf;
4270+
}
4271+
42464272
const struct bpf_verifier_ops sk_filter_prog_ops = {
42474273
.get_func_proto = sk_filter_func_proto,
42484274
.is_valid_access = sk_filter_is_valid_access,
@@ -4301,7 +4327,7 @@ const struct bpf_verifier_ops sock_ops_prog_ops = {
43014327
const struct bpf_verifier_ops sk_skb_prog_ops = {
43024328
.get_func_proto = sk_skb_func_proto,
43034329
.is_valid_access = sk_skb_is_valid_access,
4304-
.convert_ctx_access = bpf_convert_ctx_access,
4330+
.convert_ctx_access = sk_skb_convert_ctx_access,
43054331
.gen_prologue = sk_skb_prologue,
43064332
};
43074333

tools/include/uapi/linux/bpf.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -787,8 +787,8 @@ struct xdp_md {
787787
};
788788

789789
enum sk_action {
790-
SK_ABORTED = 0,
791-
SK_DROP,
790+
SK_DROP = 0,
791+
SK_PASS,
792792
SK_REDIRECT,
793793
};
794794

0 commit comments

Comments
 (0)