diff options
author | Daniel Borkmann <daniel@iogearbox.net> | 2016-06-28 06:18:28 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2016-06-30 05:54:40 -0400 |
commit | d2485c4242a826fdf493fd3a27b8b792965b9b9e (patch) | |
tree | d02f10ddd1ad1f4e5d280ab8e285f7ab450312fc | |
parent | 6578171a7ff0c31dc73258f93da7407510abf085 (diff) |
bpf: add bpf_skb_change_type helper
This work adds a helper for changing skb->pkt_type in a controlled way.
We only allow a subset of possible values and can extend that in future
should other use cases come up. Doing this as a helper has the advantage
that errors can be handeled gracefully and thus helper kept extensible.
It's a write counterpart to pkt_type member we can already read from
struct __sk_buff context. Major use case is to change incoming skbs to
PACKET_HOST in a programmatic way instead of having to recirculate via
redirect(..., BPF_F_INGRESS), for example.
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r-- | include/uapi/linux/bpf.h | 9 | ||||
-rw-r--r-- | net/core/filter.c | 24 |
2 files changed, 33 insertions, 0 deletions
diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 66cd738a937a..be6ac1291680 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h | |||
@@ -327,6 +327,15 @@ enum bpf_func_id { | |||
327 | */ | 327 | */ |
328 | BPF_FUNC_skb_change_proto, | 328 | BPF_FUNC_skb_change_proto, |
329 | 329 | ||
330 | /** | ||
331 | * bpf_skb_change_type(skb, type) | ||
332 | * Change packet type of skb. | ||
333 | * @skb: pointer to skb | ||
334 | * @type: new skb->pkt_type type | ||
335 | * Return: 0 on success or negative error | ||
336 | */ | ||
337 | BPF_FUNC_skb_change_type, | ||
338 | |||
330 | __BPF_FUNC_MAX_ID, | 339 | __BPF_FUNC_MAX_ID, |
331 | }; | 340 | }; |
332 | 341 | ||
diff --git a/net/core/filter.c b/net/core/filter.c index d983e765787a..76f9a4938be4 100644 --- a/net/core/filter.c +++ b/net/core/filter.c | |||
@@ -1979,6 +1979,28 @@ static const struct bpf_func_proto bpf_skb_change_proto_proto = { | |||
1979 | .arg3_type = ARG_ANYTHING, | 1979 | .arg3_type = ARG_ANYTHING, |
1980 | }; | 1980 | }; |
1981 | 1981 | ||
1982 | static u64 bpf_skb_change_type(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5) | ||
1983 | { | ||
1984 | struct sk_buff *skb = (struct sk_buff *) (long) r1; | ||
1985 | u32 pkt_type = r2; | ||
1986 | |||
1987 | /* We only allow a restricted subset to be changed for now. */ | ||
1988 | if (unlikely(skb->pkt_type > PACKET_OTHERHOST || | ||
1989 | pkt_type > PACKET_OTHERHOST)) | ||
1990 | return -EINVAL; | ||
1991 | |||
1992 | skb->pkt_type = pkt_type; | ||
1993 | return 0; | ||
1994 | } | ||
1995 | |||
1996 | static const struct bpf_func_proto bpf_skb_change_type_proto = { | ||
1997 | .func = bpf_skb_change_type, | ||
1998 | .gpl_only = false, | ||
1999 | .ret_type = RET_INTEGER, | ||
2000 | .arg1_type = ARG_PTR_TO_CTX, | ||
2001 | .arg2_type = ARG_ANYTHING, | ||
2002 | }; | ||
2003 | |||
1982 | bool bpf_helper_changes_skb_data(void *func) | 2004 | bool bpf_helper_changes_skb_data(void *func) |
1983 | { | 2005 | { |
1984 | if (func == bpf_skb_vlan_push) | 2006 | if (func == bpf_skb_vlan_push) |
@@ -2278,6 +2300,8 @@ tc_cls_act_func_proto(enum bpf_func_id func_id) | |||
2278 | return &bpf_skb_vlan_pop_proto; | 2300 | return &bpf_skb_vlan_pop_proto; |
2279 | case BPF_FUNC_skb_change_proto: | 2301 | case BPF_FUNC_skb_change_proto: |
2280 | return &bpf_skb_change_proto_proto; | 2302 | return &bpf_skb_change_proto_proto; |
2303 | case BPF_FUNC_skb_change_type: | ||
2304 | return &bpf_skb_change_type_proto; | ||
2281 | case BPF_FUNC_skb_get_tunnel_key: | 2305 | case BPF_FUNC_skb_get_tunnel_key: |
2282 | return &bpf_skb_get_tunnel_key_proto; | 2306 | return &bpf_skb_get_tunnel_key_proto; |
2283 | case BPF_FUNC_skb_set_tunnel_key: | 2307 | case BPF_FUNC_skb_set_tunnel_key: |