summaryrefslogtreecommitdiffstats
path: root/kernel/bpf
diff options
context:
space:
mode:
authorThomas Graf <tgraf@suug.ch>2016-11-30 11:10:10 -0500
committerDavid S. Miller <davem@davemloft.net>2016-12-02 10:51:49 -0500
commit3a0af8fd61f90920f6fa04e4f1e9a6a73c1b4fd2 (patch)
treed416e9fd84ccb9c7d66ac9d58e1ca9cd7000bd33 /kernel/bpf
parentefd857008142017ff1b4ff70d98f4a5f6003cbea (diff)
bpf: BPF for lightweight tunnel infrastructure
Registers new BPF program types which correspond to the LWT hooks: - BPF_PROG_TYPE_LWT_IN => dst_input() - BPF_PROG_TYPE_LWT_OUT => dst_output() - BPF_PROG_TYPE_LWT_XMIT => lwtunnel_xmit() The separate program types are required to differentiate between the capabilities each LWT hook allows: * Programs attached to dst_input() or dst_output() are restricted and may only read the data of an skb. This prevent modification and possible invalidation of already validated packet headers on receive and the construction of illegal headers while the IP headers are still being assembled. * Programs attached to lwtunnel_xmit() are allowed to modify packet content as well as prepending an L2 header via a newly introduced helper bpf_skb_change_head(). This is safe as lwtunnel_xmit() is invoked after the IP header has been assembled completely. All BPF programs receive an skb with L3 headers attached and may return one of the following error codes: BPF_OK - Continue routing as per nexthop BPF_DROP - Drop skb and return EPERM BPF_REDIRECT - Redirect skb to device as per redirect() helper. (Only valid in lwtunnel_xmit() context) The return codes are binary compatible with their TC_ACT_ relatives to ease compatibility. Signed-off-by: Thomas Graf <tgraf@suug.ch> Acked-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Daniel Borkmann <daniel@iogearbox.net> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'kernel/bpf')
-rw-r--r--kernel/bpf/verifier.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 8740c5fa02fc..8135cb1077ee 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -633,12 +633,19 @@ static int check_map_access(struct bpf_verifier_env *env, u32 regno, int off,
633#define MAX_PACKET_OFF 0xffff 633#define MAX_PACKET_OFF 0xffff
634 634
635static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, 635static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
636 const struct bpf_call_arg_meta *meta) 636 const struct bpf_call_arg_meta *meta,
637 enum bpf_access_type t)
637{ 638{
638 switch (env->prog->type) { 639 switch (env->prog->type) {
640 case BPF_PROG_TYPE_LWT_IN:
641 case BPF_PROG_TYPE_LWT_OUT:
642 /* dst_input() and dst_output() can't write for now */
643 if (t == BPF_WRITE)
644 return false;
639 case BPF_PROG_TYPE_SCHED_CLS: 645 case BPF_PROG_TYPE_SCHED_CLS:
640 case BPF_PROG_TYPE_SCHED_ACT: 646 case BPF_PROG_TYPE_SCHED_ACT:
641 case BPF_PROG_TYPE_XDP: 647 case BPF_PROG_TYPE_XDP:
648 case BPF_PROG_TYPE_LWT_XMIT:
642 if (meta) 649 if (meta)
643 return meta->pkt_access; 650 return meta->pkt_access;
644 651
@@ -837,7 +844,7 @@ static int check_mem_access(struct bpf_verifier_env *env, u32 regno, int off,
837 err = check_stack_read(state, off, size, value_regno); 844 err = check_stack_read(state, off, size, value_regno);
838 } 845 }
839 } else if (state->regs[regno].type == PTR_TO_PACKET) { 846 } else if (state->regs[regno].type == PTR_TO_PACKET) {
840 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL)) { 847 if (t == BPF_WRITE && !may_access_direct_pkt_data(env, NULL, t)) {
841 verbose("cannot write into packet\n"); 848 verbose("cannot write into packet\n");
842 return -EACCES; 849 return -EACCES;
843 } 850 }
@@ -970,7 +977,8 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 regno,
970 return 0; 977 return 0;
971 } 978 }
972 979
973 if (type == PTR_TO_PACKET && !may_access_direct_pkt_data(env, meta)) { 980 if (type == PTR_TO_PACKET &&
981 !may_access_direct_pkt_data(env, meta, BPF_READ)) {
974 verbose("helper access to the packet is not allowed\n"); 982 verbose("helper access to the packet is not allowed\n");
975 return -EACCES; 983 return -EACCES;
976 } 984 }