aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHannes Frederic Sowa <hannes@stressinduktion.org>2014-09-12 08:04:43 -0400
committerDavid S. Miller <davem@davemloft.net>2014-09-13 17:07:21 -0400
commit233577a22089facf5271ab5e845b2262047c971f (patch)
treec93ec43dbf3adb316b3af67a3d315984b3cc5f10
parentac7a04c33dd7f8e429df4b929ba3a3e8e729cc89 (diff)
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>
-rw-r--r--arch/mips/net/bpf_jit.c27
-rw-r--r--arch/s390/net/bpf_jit_comp.c35
-rw-r--r--include/linux/skbuff.h10
-rw-r--r--net/core/filter.c31
4 files changed, 14 insertions, 89 deletions
diff --git a/arch/mips/net/bpf_jit.c b/arch/mips/net/bpf_jit.c
index 0e97ccd29fe3..7edc08398c4a 100644
--- a/arch/mips/net/bpf_jit.c
+++ b/arch/mips/net/bpf_jit.c
@@ -765,27 +765,6 @@ static u64 jit_get_skb_w(struct sk_buff *skb, unsigned offset)
765 return (u64)err << 32 | ntohl(ret); 765 return (u64)err << 32 | ntohl(ret);
766} 766}
767 767
768#ifdef __BIG_ENDIAN_BITFIELD
769#define PKT_TYPE_MAX (7 << 5)
770#else
771#define PKT_TYPE_MAX 7
772#endif
773static 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
789static int build_body(struct jit_ctx *ctx) 768static int build_body(struct jit_ctx *ctx)
790{ 769{
791 void *load_func[] = {jit_get_skb_b, jit_get_skb_h, jit_get_skb_w}; 770 void *load_func[] = {jit_get_skb_b, jit_get_skb_h, jit_get_skb_w};
@@ -1332,11 +1311,7 @@ jmp_cmp:
1332 case BPF_ANC | SKF_AD_PKTTYPE: 1311 case BPF_ANC | SKF_AD_PKTTYPE:
1333 ctx->flags |= SEEN_SKB; 1312 ctx->flags |= SEEN_SKB;
1334 1313
1335 off = pkt_type_offset(); 1314 emit_load_byte(r_tmp, r_skb, PKT_TYPE_OFFSET(), ctx);
1336
1337 if (off < 0)
1338 return -1;
1339 emit_load_byte(r_tmp, r_skb, off, ctx);
1340 /* Keep only the last 3 bits */ 1315 /* Keep only the last 3 bits */
1341 emit_andi(r_A, r_tmp, PKT_TYPE_MAX, ctx); 1316 emit_andi(r_A, r_tmp, PKT_TYPE_MAX, ctx);
1342#ifdef __BIG_ENDIAN_BITFIELD 1317#ifdef __BIG_ENDIAN_BITFIELD
diff --git a/arch/s390/net/bpf_jit_comp.c b/arch/s390/net/bpf_jit_comp.c
index 555f5c7e83ab..c52ac77408ca 100644
--- a/arch/s390/net/bpf_jit_comp.c
+++ b/arch/s390/net/bpf_jit_comp.c
@@ -227,37 +227,6 @@ static void bpf_jit_epilogue(struct bpf_jit *jit)
227 EMIT2(0x07fe); 227 EMIT2(0x07fe);
228} 228}
229 229
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
234static int pkt_type_offset;
235
236static 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}
259device_initcall(bpf_pkt_type_offset_init);
260
261/* 230/*
262 * make sure we dont leak kernel information to user 231 * make sure we dont leak kernel information to user
263 */ 232 */
@@ -757,12 +726,10 @@ call_fn: /* lg %r1,<d(function)>(%r13) */
757 } 726 }
758 break; 727 break;
759 case BPF_ANC | SKF_AD_PKTTYPE: 728 case BPF_ANC | SKF_AD_PKTTYPE:
760 if (pkt_type_offset < 0)
761 goto out;
762 /* lhi %r5,0 */ 729 /* lhi %r5,0 */
763 EMIT4(0xa7580000); 730 EMIT4(0xa7580000);
764 /* ic %r5,<d(pkt_type_offset)>(%r2) */ 731 /* ic %r5,<d(pkt_type_offset)>(%r2) */
765 EMIT4_DISP(0x43502000, pkt_type_offset); 732 EMIT4_DISP(0x43502000, PKT_TYPE_OFFSET());
766 /* srl %r5,5 */ 733 /* srl %r5,5 */
767 EMIT4_DISP(0x88500000, 5); 734 EMIT4_DISP(0x88500000, 5);
768 break; 735 break;
diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
index 07c9fdd0c126..756e3d057e84 100644
--- a/include/linux/skbuff.h
+++ b/include/linux/skbuff.h
@@ -548,6 +548,16 @@ struct sk_buff {
548 ip_summed:2, 548 ip_summed:2,
549 nohdr:1, 549 nohdr:1,
550 nfctinfo:3; 550 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];
551 __u8 pkt_type:3, 561 __u8 pkt_type:3,
552 fclone:2, 562 fclone:2,
553 ipvs_property:1, 563 ipvs_property:1,
diff --git a/net/core/filter.c b/net/core/filter.c
index dfc716ffa44b..601f28de7311 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -87,30 +87,6 @@ int sk_filter(struct sock *sk, struct sk_buff *skb)
87} 87}
88EXPORT_SYMBOL(sk_filter); 88EXPORT_SYMBOL(sk_filter);
89 89
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
99static 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
114static u64 __skb_get_pay_offset(u64 ctx, u64 a, u64 x, u64 r4, u64 r5) 90static u64 __skb_get_pay_offset(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
115{ 91{
116 return skb_get_poff((struct sk_buff *)(unsigned long) ctx); 92 return skb_get_poff((struct sk_buff *)(unsigned long) ctx);
@@ -190,11 +166,8 @@ static bool convert_bpf_extensions(struct sock_filter *fp,
190 break; 166 break;
191 167
192 case SKF_AD_OFF + SKF_AD_PKTTYPE: 168 case SKF_AD_OFF + SKF_AD_PKTTYPE:
193 *insn = BPF_LDX_MEM(BPF_B, BPF_REG_A, BPF_REG_CTX, 169 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_A, BPF_REG_CTX,
194 pkt_type_offset()); 170 PKT_TYPE_OFFSET());
195 if (insn->off < 0)
196 return false;
197 insn++;
198 *insn = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, PKT_TYPE_MAX); 171 *insn = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, PKT_TYPE_MAX);
199#ifdef __BIG_ENDIAN_BITFIELD 172#ifdef __BIG_ENDIAN_BITFIELD
200 insn++; 173 insn++;