Skip to content

Commit 233577a

Browse files
strssndktndavem330
authored andcommitted
net: filter: constify detection of pkt_type_offset
Currently we have 2 pkt_type_offset functions doing the same thing and spread across the architecture files. Remove those and replace them with a PKT_TYPE_OFFSET macro helper which gets the constant value from a zero sized sk_buff member right in front of the bitfield with offsetof. This new offset marker does not change size of struct sk_buff. Cc: Eric Dumazet <eric.dumazet@gmail.com> Cc: Markos Chandras <markos.chandras@imgtec.com> Cc: Martin Schwidefsky <schwidefsky@de.ibm.com> Cc: Daniel Borkmann <dborkman@redhat.com> Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com> Signed-off-by: Denis Kirjanov <kda@linux-powerpc.org> Signed-off-by: Hannes Frederic Sowa <hannes@stressinduktion.org> Acked-by: Alexei Starovoitov <ast@plumgrid.com> Acked-by: Daniel Borkmann <dborkman@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent ac7a04c commit 233577a

File tree

4 files changed

+14
-89
lines changed

4 files changed

+14
-89
lines changed

arch/mips/net/bpf_jit.c

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -765,27 +765,6 @@ static u64 jit_get_skb_w(struct sk_buff *skb, unsigned offset)
765765
return (u64)err << 32 | ntohl(ret);
766766
}
767767

768-
#ifdef __BIG_ENDIAN_BITFIELD
769-
#define PKT_TYPE_MAX (7 << 5)
770-
#else
771-
#define PKT_TYPE_MAX 7
772-
#endif
773-
static int pkt_type_offset(void)
774-
{
775-
struct sk_buff skb_probe = {
776-
.pkt_type = ~0,
777-
};
778-
u8 *ct = (u8 *)&skb_probe;
779-
unsigned int off;
780-
781-
for (off = 0; off < sizeof(struct sk_buff); off++) {
782-
if (ct[off] == PKT_TYPE_MAX)
783-
return off;
784-
}
785-
pr_err_once("Please fix pkt_type_offset(), as pkt_type couldn't be found\n");
786-
return -1;
787-
}
788-
789768
static int build_body(struct jit_ctx *ctx)
790769
{
791770
void *load_func[] = {jit_get_skb_b, jit_get_skb_h, jit_get_skb_w};
@@ -1332,11 +1311,7 @@ static int build_body(struct jit_ctx *ctx)
13321311
case BPF_ANC | SKF_AD_PKTTYPE:
13331312
ctx->flags |= SEEN_SKB;
13341313

1335-
off = pkt_type_offset();
1336-
1337-
if (off < 0)
1338-
return -1;
1339-
emit_load_byte(r_tmp, r_skb, off, ctx);
1314+
emit_load_byte(r_tmp, r_skb, PKT_TYPE_OFFSET(), ctx);
13401315
/* Keep only the last 3 bits */
13411316
emit_andi(r_A, r_tmp, PKT_TYPE_MAX, ctx);
13421317
#ifdef __BIG_ENDIAN_BITFIELD

arch/s390/net/bpf_jit_comp.c

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -227,37 +227,6 @@ static void bpf_jit_epilogue(struct bpf_jit *jit)
227227
EMIT2(0x07fe);
228228
}
229229

230-
/* Helper to find the offset of pkt_type in sk_buff
231-
* Make sure its still a 3bit field starting at the MSBs within a byte.
232-
*/
233-
#define PKT_TYPE_MAX 0xe0
234-
static int pkt_type_offset;
235-
236-
static int __init bpf_pkt_type_offset_init(void)
237-
{
238-
struct sk_buff skb_probe = {
239-
.pkt_type = ~0,
240-
};
241-
char *ct = (char *)&skb_probe;
242-
int off;
243-
244-
pkt_type_offset = -1;
245-
for (off = 0; off < sizeof(struct sk_buff); off++) {
246-
if (!ct[off])
247-
continue;
248-
if (ct[off] == PKT_TYPE_MAX)
249-
pkt_type_offset = off;
250-
else {
251-
/* Found non matching bit pattern, fix needed. */
252-
WARN_ON_ONCE(1);
253-
pkt_type_offset = -1;
254-
return -1;
255-
}
256-
}
257-
return 0;
258-
}
259-
device_initcall(bpf_pkt_type_offset_init);
260-
261230
/*
262231
* make sure we dont leak kernel information to user
263232
*/
@@ -757,12 +726,10 @@ load_abs: if ((int) K < 0)
757726
}
758727
break;
759728
case BPF_ANC | SKF_AD_PKTTYPE:
760-
if (pkt_type_offset < 0)
761-
goto out;
762729
/* lhi %r5,0 */
763730
EMIT4(0xa7580000);
764731
/* ic %r5,<d(pkt_type_offset)>(%r2) */
765-
EMIT4_DISP(0x43502000, pkt_type_offset);
732+
EMIT4_DISP(0x43502000, PKT_TYPE_OFFSET());
766733
/* srl %r5,5 */
767734
EMIT4_DISP(0x88500000, 5);
768735
break;

include/linux/skbuff.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,16 @@ struct sk_buff {
548548
ip_summed:2,
549549
nohdr:1,
550550
nfctinfo:3;
551+
552+
/* if you move pkt_type around you also must adapt those constants */
553+
#ifdef __BIG_ENDIAN_BITFIELD
554+
#define PKT_TYPE_MAX (7 << 5)
555+
#else
556+
#define PKT_TYPE_MAX 7
557+
#endif
558+
#define PKT_TYPE_OFFSET() offsetof(struct sk_buff, __pkt_type_offset)
559+
560+
__u8 __pkt_type_offset[0];
551561
__u8 pkt_type:3,
552562
fclone:2,
553563
ipvs_property:1,

net/core/filter.c

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -87,30 +87,6 @@ int sk_filter(struct sock *sk, struct sk_buff *skb)
8787
}
8888
EXPORT_SYMBOL(sk_filter);
8989

90-
/* Helper to find the offset of pkt_type in sk_buff structure. We want
91-
* to make sure its still a 3bit field starting at a byte boundary;
92-
* taken from arch/x86/net/bpf_jit_comp.c.
93-
*/
94-
#ifdef __BIG_ENDIAN_BITFIELD
95-
#define PKT_TYPE_MAX (7 << 5)
96-
#else
97-
#define PKT_TYPE_MAX 7
98-
#endif
99-
static unsigned int pkt_type_offset(void)
100-
{
101-
struct sk_buff skb_probe = { .pkt_type = ~0, };
102-
u8 *ct = (u8 *) &skb_probe;
103-
unsigned int off;
104-
105-
for (off = 0; off < sizeof(struct sk_buff); off++) {
106-
if (ct[off] == PKT_TYPE_MAX)
107-
return off;
108-
}
109-
110-
pr_err_once("Please fix %s, as pkt_type couldn't be found!\n", __func__);
111-
return -1;
112-
}
113-
11490
static u64 __skb_get_pay_offset(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
11591
{
11692
return skb_get_poff((struct sk_buff *)(unsigned long) ctx);
@@ -190,11 +166,8 @@ static bool convert_bpf_extensions(struct sock_filter *fp,
190166
break;
191167

192168
case SKF_AD_OFF + SKF_AD_PKTTYPE:
193-
*insn = BPF_LDX_MEM(BPF_B, BPF_REG_A, BPF_REG_CTX,
194-
pkt_type_offset());
195-
if (insn->off < 0)
196-
return false;
197-
insn++;
169+
*insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_A, BPF_REG_CTX,
170+
PKT_TYPE_OFFSET());
198171
*insn = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, PKT_TYPE_MAX);
199172
#ifdef __BIG_ENDIAN_BITFIELD
200173
insn++;

0 commit comments

Comments
 (0)