aboutsummaryrefslogtreecommitdiffstats
path: root/net/core
diff options
context:
space:
mode:
authorDaniel Borkmann <daniel@iogearbox.net>2015-03-19 14:38:27 -0400
committerDavid S. Miller <davem@davemloft.net>2015-03-20 15:24:26 -0400
commit0b8c707ddf37171413fe67350263e5b6ffeedf7c (patch)
treee2f6a1849cc78ec2cc41846718ad3076e84de0cd /net/core
parentc4a6853d8fb2b122686bc6a7c472956e87090f4e (diff)
ebpf, filter: do not convert skb->protocol to host endianess during runtime
Commit c24973957975 ("bpf: allow BPF programs access 'protocol' and 'vlan_tci' fields") has added support for accessing protocol, vlan_present and vlan_tci into the skb offset map. As referenced in the below discussion, accessing skb->protocol from an eBPF program should be converted without handling endianess. The reason for this is that an eBPF program could simply do a check more naturally, by f.e. testing skb->protocol == htons(ETH_P_IP), where the LLVM compiler resolves htons() against a constant automatically during compilation time, as opposed to an otherwise needed run time conversion. After all, the way of programming both from a user perspective differs quite a lot, i.e. bpf_asm ["ld proto"] versus a C subset/LLVM. Reference: https://patchwork.ozlabs.org/patch/450819/ Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@plumgrid.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/core')
-rw-r--r--net/core/filter.c29
1 files changed, 14 insertions, 15 deletions
diff --git a/net/core/filter.c b/net/core/filter.c
index b95ae7fe7e4f..bdaac5895def 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -178,16 +178,6 @@ static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
178 offsetof(struct sk_buff, queue_mapping)); 178 offsetof(struct sk_buff, queue_mapping));
179 break; 179 break;
180 180
181 case SKF_AD_PROTOCOL:
182 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
183
184 /* dst_reg = *(u16 *) (src_reg + offsetof(protocol)) */
185 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
186 offsetof(struct sk_buff, protocol));
187 /* dst_reg = ntohs(dst_reg) [emitting a nop or swap16] */
188 *insn++ = BPF_ENDIAN(BPF_FROM_BE, dst_reg, 16);
189 break;
190
191 case SKF_AD_VLAN_TAG: 181 case SKF_AD_VLAN_TAG:
192 case SKF_AD_VLAN_TAG_PRESENT: 182 case SKF_AD_VLAN_TAG_PRESENT:
193 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2); 183 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
@@ -219,8 +209,13 @@ static bool convert_bpf_extensions(struct sock_filter *fp,
219 209
220 switch (fp->k) { 210 switch (fp->k) {
221 case SKF_AD_OFF + SKF_AD_PROTOCOL: 211 case SKF_AD_OFF + SKF_AD_PROTOCOL:
222 cnt = convert_skb_access(SKF_AD_PROTOCOL, BPF_REG_A, BPF_REG_CTX, insn); 212 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
223 insn += cnt - 1; 213
214 /* A = *(u16 *) (CTX + offsetof(protocol)) */
215 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
216 offsetof(struct sk_buff, protocol));
217 /* A = ntohs(A) [emitting a nop or swap16] */
218 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
224 break; 219 break;
225 220
226 case SKF_AD_OFF + SKF_AD_PKTTYPE: 221 case SKF_AD_OFF + SKF_AD_PKTTYPE:
@@ -1224,6 +1219,13 @@ static u32 sk_filter_convert_ctx_access(int dst_reg, int src_reg, int ctx_off,
1224 offsetof(struct sk_buff, len)); 1219 offsetof(struct sk_buff, len));
1225 break; 1220 break;
1226 1221
1222 case offsetof(struct __sk_buff, protocol):
1223 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
1224
1225 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1226 offsetof(struct sk_buff, protocol));
1227 break;
1228
1227 case offsetof(struct __sk_buff, mark): 1229 case offsetof(struct __sk_buff, mark):
1228 return convert_skb_access(SKF_AD_MARK, dst_reg, src_reg, insn); 1230 return convert_skb_access(SKF_AD_MARK, dst_reg, src_reg, insn);
1229 1231
@@ -1233,9 +1235,6 @@ static u32 sk_filter_convert_ctx_access(int dst_reg, int src_reg, int ctx_off,
1233 case offsetof(struct __sk_buff, queue_mapping): 1235 case offsetof(struct __sk_buff, queue_mapping):
1234 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn); 1236 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
1235 1237
1236 case offsetof(struct __sk_buff, protocol):
1237 return convert_skb_access(SKF_AD_PROTOCOL, dst_reg, src_reg, insn);
1238
1239 case offsetof(struct __sk_buff, vlan_present): 1238 case offsetof(struct __sk_buff, vlan_present):
1240 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT, 1239 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
1241 dst_reg, src_reg, insn); 1240 dst_reg, src_reg, insn);