ipv6: sr: introduce seg6local End.BPF action
Subsystems (9)
v5 cover ipv6: sr: introduce seg6local End.BPF action
As of Linux 4.14, it is possible to define advanced local processing for IPv6 packets with a Segment Routing Header through the seg6local LWT infrastructure. This LWT implements the network programming principles defined in the IETF “SRv6 Network Programming” draft.
The implemented operations are generic, and it would be very interesting to be able to implement user-specific seg6local actions, without having to modify the kernel directly. To do so, this patchset adds an End.BPF action to seg6local, powered by some specific Segment Routing-related helpers, which provide SR functionalities that can be applied on the packet. This BPF hook would then allow to implement specific actions at native kernel speed such as OAM features, advanced SR SDN policies, SRv6 actions like Segment Routing Header (SRH) encapsulation depending on the content of the packet, etc.
This patchset is divided in 6 patches, whose main features are :
- A new seg6local action End.BPF with the corresponding new BPF program type BPF_PROG_TYPE_LWT_SEG6LOCAL. Such attached BPF program can be passed to the LWT seg6local through netlink, the same way as the LWT BPF hook operates.
- 3 new BPF helpers for the seg6local BPF hook, allowing to edit/grow/ shrink a SRH and apply on a packet some of the generic SRv6 actions.
- 1 new BPF helper for the LWT BPF IN hook, allowing to add a SRH through encapsulation (via IPv6 encapsulation or inlining if the packet contains already an IPv6 header).
As this patchset adds a new LWT BPF hook, I took into account the result of the discussions when the LWT BPF infrastructure got merged. Hence, the seg6local BPF hook doesn’t allow write access to skb->data directly, only the SRH can be modified through specific helpers, which ensures that the integrity of the packet is maintained.
More details are available in the related patches messages.
The performances of this BPF hook have been assessed with the BPF JIT enabled on a Intel Xeon X3440 processors with 4 cores and 8 threads clocked at 2.53 GHz. No throughput losses are noted with the seg6local BPF hook when the BPF program does nothing (440kpps). Adding a 8-bytes TLV (1 call each to bpf_lwt_seg6_adjust_srh and bpf_lwt_seg6_store_bytes) drops the throughput to 410kpps, and inlining a SRH via bpf_lwt_seg6_action drops the throughput to 420kpps.
All throughputs are stable.
-------
v2: move the SRH integrity state from skb->cb to a per-cpu buffer
v3: - document helpers in man-page style
- fix kbuild bugs
- un-break BPF LWT out hook
- bpf_push_seg6_encap is now static
- preempt_enable is now called when the packet is dropped in input_action_end_bpf
v4: fix kbuild bugs when CONFIG_IPV6=m
v5: fix kbuild sparse warnings when CONFIG_IPV6=m
Thanks.
Mathieu Xhonneux (6):
ipv6: sr: make seg6.h includable without IPv6
ipv6: sr: export function lookup_nexthop
bpf: Add IPv6 Segment Routing helpers
bpf: Split lwt inout verifier structures
ipv6: sr: Add seg6local action End.BPF selftests/bpf: test for seg6local End.BPF action
include/linux/bpf_types.h | 5 +- include/net/seg6.h | 7 +- include/net/seg6_local.h | 32 ++ include/uapi/linux/bpf.h | 98 ++++- include/uapi/linux/seg6_local.h | 3 + kernel/bpf/verifier.c | 1 + net/core/filter.c | 390 ++++++++++++++++--- net/ipv6/Kconfig | 5 + net/ipv6/seg6_local.c | 180 ++++++++- tools/include/uapi/linux/bpf.h | 98 ++++- tools/lib/bpf/libbpf.c | 1 + tools/testing/selftests/bpf/Makefile | 5 +- tools/testing/selftests/bpf/bpf_helpers.h | 12 + tools/testing/selftests/bpf/test_lwt_seg6local.c | 438 ++++++++++++++++++++++ tools/testing/selftests/bpf/test_lwt_seg6local.sh | 140 +++++++ 15 files changed, 1340 insertions(+), 75 deletions(-) create mode 100644 include/net/seg6_local.h create mode 100644 tools/testing/selftests/bpf/test_lwt_seg6local.c create mode 100755 tools/testing/selftests/bpf/test_lwt_seg6local.sh
Signature · 1 line
2.16.1
PATCH v5 1/6 ipv6: sr: make seg6.h includable without IPv6
include/net/seg6.h cannot be included in a source file if CONFIG_IPV6 is not enabled:
include/net/seg6.h: In function 'seg6_pernet':
Quoted 1 line
Quoted 1 line
include/net/seg6.h:52:14: error: 'struct net' has no member named
'ipv6'; did you mean 'ipv4'?
return net->ipv6.seg6_data;
^~~~
ipv4This commit makes seg6_pernet return NULL if IPv6 is not compiled, hence allowing seg6.h to be included regardless of the configuration.
--- include/net/seg6.h | 4 ++++ 1 file changed, 4 insertions(+)
include/net/seg6.h +4 -0
@@ -49,7 +49,11 @@ struct seg6_pernet_data {static inline struct seg6_pernet_data *seg6_pernet (struct net *net) { +#if IS_ENABLED (CONFIG_IPV6 )return net->ipv6.seg6_data; +#else +return NULL ; +#endif }extern int seg6_init (void );
Signature · 1 line
2.16.1
PATCH v5 2/6 ipv6: sr: export function lookup_nexthop
In reply to Mathieu XhonneuxThe function lookup_nexthop is essential to implement most of the seg6local actions. As we want to provide a BPF helper allowing to apply some of these actions on the packet being processed, the helper should be able to call this function, hence the need to make it public.
Moreover, if one argument is incorrect or if the next hop can not be found, an error should be returned by the BPF helper so the BPF program can adapt its processing of the packet (return an error, properly force the drop, ...). This patch hence makes this function return dst->error to indicate a possible error.
--- include/net/seg6.h | 3 ++- include/net/seg6_local.h | 24 ++++++++++++++++++++++++ net/ipv6/seg6_local.c | 20 +++++++++++--------- 3 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 include/net/seg6_local.h
include/net/seg6.h +2 -1
@@ -67,5 +67,6 @@ extern bool seg6_validate_srh(struct ipv6_sr_hdr *srh, int len);extern int seg6_do_srh_encap (struct sk_buff *skb,struct ipv6_sr_hdr *osrh,int proto);extern int seg6_do_srh_inline (struct sk_buff *skb,struct ipv6_sr_hdr *osrh); - +extern int seg6_lookup_nexthop (struct sk_buff *skb,struct in6_addr *nhaddr, +u32 tbl_id);#endif
include/net/seg6_local.h new file +24 -0
@@ -0,0 +1,24 @@ +/* +* SR-IPv6 implementation +* +* Authors: +* David Lebrun <[email protected]> +* eBPF support: Mathieu Xhonneux <[email protected]> +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version +* 2 of the License, or (at your option) any later version. +*/ + +#ifndef _NET_SEG6_LOCAL_H +#define _NET_SEG6_LOCAL_H + +#include <linux/net.h> +#include <linux/ipv6.h> + +extern int seg6_lookup_nexthop (struct sk_buff *skb,struct in6_addr *nhaddr, +u32 tbl_id); + +#endif
net/ipv6/seg6_local.c +11 -9
@@ -30,6 +30,7 @@#ifdef CONFIG_IPV6_SEG6_HMAC #include <net/seg6_hmac.h> #endif +#include <net/seg6_local.h> #include <linux/etherdevice.h> struct seg6_local_lwt ;
@@ -140,8 +141,8 @@ static void advance_nextseg(struct ipv6_sr_hdr *srh, struct in6_addr *daddr) *daddr = *addr; } -static void lookup_nexthop (struct sk_buff *skb,struct in6_addr *nhaddr, -u32 tbl_id) +int seg6_lookup_nexthop (struct sk_buff *skb,struct in6_addr *nhaddr, +u32 tbl_id) {struct net *net =dev_net (skb->dev);struct ipv6hdr *hdr =ipv6_hdr (skb);
@@ -187,6 +188,7 @@ static void lookup_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,skb_dst_drop (skb);skb_dst_set (skb, dst); +return dst->error; }/* regular endpoint function */
@@ -200,7 +202,7 @@ static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt)advance_nextseg (srh, &ipv6_hdr (skb)->daddr); -lookup_nexthop (skb,NULL ,0 ); +seg6_lookup_nexthop (skb,NULL ,0 );return dst_input (skb);
@@ -220,7 +222,7 @@ static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt)advance_nextseg (srh, &ipv6_hdr (skb)->daddr); -lookup_nexthop (skb, &slwt->nh6,0 ); +seg6_lookup_nexthop (skb, &slwt->nh6,0 );return dst_input (skb);
@@ -239,7 +241,7 @@ static int input_action_end_t(struct sk_buff *skb, struct seg6_local_lwt *slwt)advance_nextseg (srh, &ipv6_hdr (skb)->daddr); -lookup_nexthop (skb,NULL , slwt->table); +seg6_lookup_nexthop (skb,NULL , slwt->table);return dst_input (skb);
@@ -331,7 +333,7 @@ static int input_action_end_dx6(struct sk_buff *skb,if (!ipv6_addr_any (&slwt->nh6)) nhaddr = &slwt->nh6; -lookup_nexthop (skb, nhaddr,0 ); +seg6_lookup_nexthop (skb, nhaddr,0 );return dst_input (skb); drop:
@@ -380,7 +382,7 @@ static int input_action_end_dt6(struct sk_buff *skb,if (!pskb_may_pull (skb,sizeof (struct ipv6hdr ))) goto drop; -lookup_nexthop (skb,NULL , slwt->table); +seg6_lookup_nexthop (skb,NULL , slwt->table);return dst_input (skb);
@@ -406,7 +408,7 @@ static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt)ipv6_hdr (skb)->payload_len =htons (skb->len -sizeof (struct ipv6hdr ));skb_set_transport_header (skb,sizeof (struct ipv6hdr )); -lookup_nexthop (skb,NULL ,0 ); +seg6_lookup_nexthop (skb,NULL ,0 );return dst_input (skb);
@@ -438,7 +440,7 @@ static int input_action_end_b6_encap(struct sk_buff *skb,ipv6_hdr (skb)->payload_len =htons (skb->len -sizeof (struct ipv6hdr ));skb_set_transport_header (skb,sizeof (struct ipv6hdr )); -lookup_nexthop (skb,NULL ,0 ); +seg6_lookup_nexthop (skb,NULL ,0 );return dst_input (skb);
Signature · 1 line
2.16.1
PATCH v5 3/6 bpf: Add IPv6 Segment Routing helpers
In reply to Mathieu XhonneuxThe BPF seg6local hook should be powerful enough to enable users to implement most of the use-cases one could think of. After some thinking, we figured out that the following actions should be possible on a SRv6 packet, requiring 3 specific helpers :
- bpf_lwt_seg6_store_bytes: Modify non-sensitive fields of the SRH
- bpf_lwt_seg6_adjust_srh: Allow to grow or shrink a SRH (to add/delete TLVs)
- bpf_lwt_seg6_action: Apply some SRv6 network programming actions (specifically End.X, End.T, End.B6 and End.B6.Encap)
The specifications of these helpers are provided in the patch (see include/uapi/linux/bpf.h).
The non-sensitive fields of the SRH are the following : flags, tag and TLVs. The other fields can not be modified, to maintain the SRH integrity. Flags, tag and TLVs can easily be modified as their validity can be checked afterwards via seg6_validate_srh. It is not allowed to modify the segments directly. If one wants to add segments on the path, he should stack a new SRH using the End.B6 action via bpf_lwt_seg6_action.
Growing, shrinking or editing TLVs via the helpers will flag the SRH as invalid, and it will have to be re-validated before re-entering the IPv6 layer. This flag is stored in a per-CPU buffer, along with the current header length in bytes.
Storing the SRH len in bytes in the control block is mandatory when using bpf_lwt_seg6_adjust_srh. The Header Ext. Length field contains the SRH len rounded to 8 bytes (a padding TLV can be inserted to ensure the 8-bytes boundary). When adding/deleting TLVs within the BPF program, the SRH may temporary be in an invalid state where its length cannot be rounded to 8 bytes without remainder, hence the need to store the length in bytes separately. The caller of the BPF program can then ensure that the SRH's final length is valid using this value. Again, a final SRH modified by a BPF program which doesn’t respect the 8-bytes boundary will be discarded as it will be considered as invalid.
Finally, a fourth helper is provided, bpf_lwt_push_encap, which is available from the LWT BPF IN hook, but not from the seg6local BPF one. This helper allows to encapsulate a Segment Routing Header (either with a new outer IPv6 header, or by inlining it directly in the existing IPv6 header) into a non-SRv6 packet. This helper is required if we want to offer the possibility to dynamically encapsulate a SRH for non-SRv6 packet, as the BPF seg6local hook only works on traffic already containing a SRH. This is the BPF equivalent of the seg6 LWT infrastructure, which achieves the same purpose but with a static SRH per route.
These helpers require CONFIG_IPV6=y (and not =m).
--- include/net/seg6_local.h | 8 ++ include/uapi/linux/bpf.h | 97 +++++++++++++++- net/core/filter.c | 282 +++++++++++++++++++++++++++++++++++++++++++---- net/ipv6/Kconfig | 5 + net/ipv6/seg6_local.c | 2 + 5 files changed, 369 insertions(+), 25 deletions(-)
include/net/seg6_local.h +8 -0
@@ -15,10 +15,18 @@#ifndef _NET_SEG6_LOCAL_H#define _NET_SEG6_LOCAL_H +#include <linux/percpu.h> #include <linux/net.h> #include <linux/ipv6.h> extern int seg6_lookup_nexthop (struct sk_buff *skb,struct in6_addr *nhaddr,u32 tbl_id); +struct seg6_bpf_srh_state { +bool valid; +u16 hdrlen; +}; + +DECLARE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states); +#endif
include/uapi/linux/bpf.h +95 -2
121 lines not on this page · raw message
net/core/filter.c +259 -23
306 lines not on this page · raw message
net/ipv6/Kconfig +5 -0
@@ -330,4 +330,9 @@ config IPV6_SEG6_HMAC If unsure, say N. +config IPV6_SEG6_BPF + def_bool y + depends on IPV6_SEG6_LWTUNNEL + depends on IPV6 = y + endif # IPV6
net/ipv6/seg6_local.c +2 -0
@@ -449,6 +449,8 @@ static int input_action_end_b6_encap(struct sk_buff *skb,return err; } +DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states); +static struct seg6_action_desc seg6_action_table[] = { { .action =SEG6_LOCAL_ACTION_END ,
Signature · 1 line
2.16.1
Daniel Borkmann
maintainerRe: [PATCH bpf-next v5 3/6] bpf: Add IPv6 Segment Routing helpers
On 05/12/2018 07:25 PM, Mathieu Xhonneux wrote:
[...]
Quoted 6 lines
net/core/filter.c +6
-0+BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset, + const void *, from, u32, len) +{ +#if IS_ENABLED (CONFIG_IPV6_SEG6_BPF ) +struct seg6_bpf_srh_state *srh_state = +this_cpu_ptr (&seg6_bpf_srh_states);
net/core/filter.c static const struct bpf_func_proto bpf_skb_fib_lookup_proto = { · line 4353
Quoted 10 lines
net/core/filter.c +10
-0+void *srh_tlvs, *srh_end, *ptr; +struct ipv6_sr_hdr *srh; +int srhoff =0 ; + +if (ipv6_find_hdr (skb, &srhoff,IPPROTO_ROUTING ,NULL ,NULL ) <0 ) +return -EINVAL ; + + srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); + srh_tlvs = (void *)((char *)srh + ((srh->first_segment +1 ) <<4 )); + srh_end = (void *)((char *)srh +sizeof (*srh) + srh_state->hdrlen);
Do we need to check that this cannot go out of bounds wrt skb data?
net/core/filter.c static const struct bpf_func_proto bpf_skb_fib_lookup_proto = { · line 4365
Quoted 11 lines
net/core/filter.c +11
-0+ ptr = skb->data + offset; +if (ptr >= srh_tlvs && ptr + len <= srh_end) + srh_state->valid =0 ; +else if (ptr < (void *)&srh->flags || + ptr + len > (void *)&srh->segments) +return -EFAULT ; + +if (unlikely (bpf_try_make_writable (skb, offset + len))) +return -EFAULT ; + +memcpy (ptr, from, len);
You have a use after free here. bpf_try_make_writable() is potentially changing underlying skb->data (e.g. see pskb_expand_head()). Therefore memcpy()'ing into cached ptr is invalid.
Quoted 110 lines
net/core/filter.c +110
-0+return 0 ; +#else /* CONFIG_IPV6_SEG6_BPF */ +return -EOPNOTSUPP ; +#endif +} + +static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = { + .func = bpf_lwt_seg6_store_bytes, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , + .arg2_type =ARG_ANYTHING , + .arg3_type =ARG_PTR_TO_MEM , + .arg4_type =ARG_CONST_SIZE +}; + +BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb, + u32, action, void *, param, u32, param_len) +{ +#if IS_ENABLED (CONFIG_IPV6_SEG6_BPF ) +struct seg6_bpf_srh_state *srh_state = +this_cpu_ptr (&seg6_bpf_srh_states); +struct ipv6_sr_hdr *srh; +int srhoff =0 ; +int err; + +if (ipv6_find_hdr (skb, &srhoff,IPPROTO_ROUTING ,NULL ,NULL ) <0 ) +return -EINVAL ; + srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); + +if (!srh_state->valid) { +if (unlikely ((srh_state->hdrlen &7 ) !=0 )) +return -EBADMSG ; + + srh->hdrlen = (u8)(srh_state->hdrlen >>3 ); +if (unlikely (!seg6_validate_srh (srh, (srh->hdrlen +1 ) <<3 ))) +return -EBADMSG ; + + srh_state->valid =1 ; + } + +switch (action) { +case SEG6_LOCAL_ACTION_END_X : +if (param_len !=sizeof (struct in6_addr )) +return -EINVAL ; +return seg6_lookup_nexthop (skb, (struct in6_addr *)param,0 ); +case SEG6_LOCAL_ACTION_END_T : +if (param_len !=sizeof (int )) +return -EINVAL ; +return seg6_lookup_nexthop (skb,NULL , *(int *)param); +case SEG6_LOCAL_ACTION_END_B6 : + err =bpf_push_seg6_encap (skb,BPF_LWT_ENCAP_SEG6_INLINE , + param, param_len); +if (!err) + srh_state->hdrlen = + ((struct ipv6_sr_hdr *)param)->hdrlen <<3 ; +return err; +case SEG6_LOCAL_ACTION_END_B6_ENCAP : + err =bpf_push_seg6_encap (skb,BPF_LWT_ENCAP_SEG6 , + param, param_len); +if (!err) + srh_state->hdrlen = + ((struct ipv6_sr_hdr *)param)->hdrlen <<3 ; +return err; +default : +return -EINVAL ; + } +#else /* CONFIG_IPV6_SEG6_BPF */ +return -EOPNOTSUPP ; +#endif +} + +static const struct bpf_func_proto bpf_lwt_seg6_action_proto = { + .func = bpf_lwt_seg6_action, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , + .arg2_type =ARG_ANYTHING , + .arg3_type =ARG_PTR_TO_MEM , + .arg4_type =ARG_CONST_SIZE +}; + +BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset, + s32, len) +{ +#if IS_ENABLED (CONFIG_IPV6_SEG6_BPF ) +struct seg6_bpf_srh_state *srh_state = +this_cpu_ptr (&seg6_bpf_srh_states); +void *srh_end, *srh_tlvs, *ptr; +struct ipv6_sr_hdr *srh; +struct ipv6hdr *hdr; +int srhoff =0 ; +int ret; + +if (ipv6_find_hdr (skb, &srhoff,IPPROTO_ROUTING ,NULL ,NULL ) <0 ) +return -EINVAL ; + srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); + + srh_tlvs = (void *)((unsigned char *)srh +sizeof (*srh) + + ((srh->first_segment +1 ) <<4 )); + srh_end = (void *)((unsigned char *)srh +sizeof (*srh) + + srh_state->hdrlen); + ptr = skb->data + offset; + +if (unlikely (ptr < srh_tlvs || ptr > srh_end)) +return -EFAULT ; +if (unlikely (len <0 && (void *)((char *)ptr - len) > srh_end)) +return -EFAULT ; + +if (len >0 ) {
net/core/filter.c static const struct bpf_func_proto bpf_skb_fib_lookup_proto = { · line 4485
Quoted 10 lines
net/core/filter.c +10
-0+ ret =skb_cow_head (skb, len); +if (unlikely (ret <0 )) +return ret; + + ret =bpf_skb_net_hdr_push (skb, offset, len); + }else { + ret =bpf_skb_net_hdr_pop (skb, offset,-1 * len); + } +if (unlikely (ret <0 )) +return ret;
And here as well. You changed underlying pointers via skb_cow_head(), but in the error path you leave the cached pointers that now point to already freed buffer. Thus, you'd now be able to access the new skb data out of bounds since cb->data_end is still the old one due to missing bpf_compute_data_pointers(skb). Please fix and audit your whole series carefully against these types of subtle bugs.
Quoted 87 lines
net/core/filter.c +48
-1+ hdr = (struct ipv6hdr *)skb->data; + hdr->payload_len =htons (skb->len -sizeof (struct ipv6hdr )); + +bpf_compute_data_pointers (skb); + srh_state->hdrlen += len; + srh_state->valid =0 ; +return 0 ; +#else /* CONFIG_IPV6_SEG6_BPF */ +return -EOPNOTSUPP ; +#endif +} + +static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = { + .func = bpf_lwt_seg6_adjust_srh, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , + .arg2_type =ARG_ANYTHING , + .arg3_type =ARG_ANYTHING , +}; + +bool bpf_helper_changes_pkt_data (void *func) +{ +if (func == bpf_skb_vlan_push || + func == bpf_skb_vlan_pop || + func == bpf_skb_store_bytes || + func == bpf_skb_change_proto || + func == bpf_skb_change_head || + func == bpf_skb_change_tail || + func == bpf_skb_adjust_room || + func == bpf_skb_pull_data || + func == bpf_clone_redirect || + func == bpf_l3_csum_replace || + func == bpf_l4_csum_replace || + func == bpf_xdp_adjust_head || + func == bpf_xdp_adjust_meta || + func == bpf_msg_pull_data || + func == bpf_xdp_adjust_tail || + func == bpf_lwt_push_encap || + func == bpf_lwt_seg6_store_bytes || + func == bpf_lwt_seg6_adjust_srh || + func == bpf_lwt_seg6_action + ) +return true; + +return false; +} +static const struct bpf_func_proto *bpf_base_func_proto (enum bpf_func_id func_id) {@@ -4703,7 +4940,6 @@ static bool lwt_is_valid_access(int off, int size,return bpf_skb_is_valid_access (off, size, type, prog, info); } -/* Attach type specific accesses */ static bool __sock_filter_check_attach_type(int off, enum bpf_access_type access_type,net/ipv6/Kconfig +5
-0@@ -330,4 +330,9 @@ config IPV6_SEG6_HMAC If unsure, say N. +config IPV6_SEG6_BPF + def_bool y + depends on IPV6_SEG6_LWTUNNEL + depends on IPV6 = y + endif # IPV6net/ipv6/seg6_local.c +2
-0@@ -449,6 +449,8 @@ static int input_action_end_b6_encap(struct sk_buff *skb,return err; } +DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states); +static struct seg6_action_desc seg6_action_table[] = { { .action =SEG6_LOCAL_ACTION_END ,
Re: [PATCH bpf-next v5 3/6] bpf: Add IPv6 Segment Routing helpers
2018-05-14 23:40 GMT+01:00 Daniel Borkmann <[email protected]>:
Quoted 12 lines · Mathieu Xhonneux
[...]
Quoted 10 lines
net/core/filter.c +10
-0+BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset, + const void *, from, u32, len) +{ +#if IS_ENABLED (CONFIG_IPV6_SEG6_BPF ) +struct seg6_bpf_srh_state *srh_state = +this_cpu_ptr (&seg6_bpf_srh_states); +void *srh_tlvs, *srh_end, *ptr; +struct ipv6_sr_hdr *srh; +int srhoff =0 ; +
Quoted 8 lines
net/core/filter.c static const struct bpf_func_proto bpf_skb_fib_lookup_proto = { · line 4353
Quoted 6 lines
net/core/filter.c +6
-0+if (ipv6_find_hdr (skb, &srhoff,IPPROTO_ROUTING ,NULL ,NULL ) <0 ) +return -EINVAL ; + + srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); + srh_tlvs = (void *)((char *)srh + ((srh->first_segment +1 ) <<4 )); + srh_end = (void *)((char *)srh +sizeof (*srh) + srh_state->hdrlen);Do we need to check that this cannot go out of bounds wrt skb data?
input_action_bpf_end (which calls the BPF program) already verifies using get_srh() that the whole SRH is accessible and is not out of bounds.
The seg6 helpers (e.g. bpf_lwt_seg6_adjust_srh) then modify srh_state->hdrlen following the evolution of the SRH in size. I don't think that a check on srh_end is needed here as the SRH is already verified once, and srh_state->hdrlen is then updated to keep this bound correct.
Quoted 7 lines
Quoted 7 lines
net/core/filter.c +7
-0+ ptr = skb->data + offset; +if (ptr >= srh_tlvs && ptr + len <= srh_end) + srh_state->valid =0 ; +else if (ptr < (void *)&srh->flags || + ptr + len > (void *)&srh->segments) +return -EFAULT ; +
Quoted 9 lines
net/core/filter.c static const struct bpf_func_proto bpf_skb_fib_lookup_proto = { · line 4365
Quoted 4 lines
net/core/filter.c +4
-0+if (unlikely (bpf_try_make_writable (skb, offset + len))) +return -EFAULT ; + +memcpy (ptr, from, len);You have a use after free here.
bpf_try_make_writable()is potentially changing underlyingskb->data(e.g. seepskb_expand_head()). Therefore memcpy()'ing into cached ptr is invalid.
OK.
Quoted 12 lines
net/core/filter.c static const struct bpf_func_proto bpf_skb_fib_lookup_proto = { · line 4485
Quoted 11 lines
net/core/filter.c +11
-0+if (len >0 ) { + ret =skb_cow_head (skb, len); +if (unlikely (ret <0 )) +return ret; + + ret =bpf_skb_net_hdr_push (skb, offset, len); + }else { + ret =bpf_skb_net_hdr_pop (skb, offset,-1 * len); + } +if (unlikely (ret <0 )) +return ret;
Quoted 6 lines
And here as well. You changed underlying pointers via
skb_cow_head(), but in the error path you leave the cached pointers that now point to already freed buffer. Thus, you'd now be able to access the new skb data out of bounds sincecb->data_endis still the old one due to missingbpf_compute_data_pointers(skb). Please fix and audit your whole series carefully against these types of subtle bugs.
Right.
I went through the whole series again. I found a similar mistake in bpf_push_seg6_encap, and added also a bpf_compute_data_pointers(skb) there. I didn't find anything else, so I hope that we're covered here (bpf_lwt_seg6_store_bytes, bpf_lwt_seg6_adjust_srh and bpf_push_seg6_encap are the only functions modifying the packet in this series).
Thanks. I'll submit a v6 ASAP.
Re: [PATCH bpf-next v5 3/6] bpf: Add IPv6 Segment Routing helpers
In reply to Mathieu XhonneuxQuoted 8 lines · Mathieu Xhonneux
The BPF seg6local hook should be powerful enough to enable users to implement most of the use-cases one could think of. After some thinking, we figured out that the following actions should be possible on a SRv6 packet, requiring 3 specific helpers :
-bpf_lwt_seg6_store_bytes: Modify non-sensitive fields of the SRH
-bpf_lwt_seg6_adjust_srh: Allow to grow or shrink a SRH (to add/delete TLVs)
Note that the current draft of SRH does not allow TLVs to be added or deleted in flight (draft-ietf-6man-segment-routing-header-19). Neither is it permissible to change the size of an extension header of a packet in flight. Is this code enabling that?
Tom
Quoted 549 lines
-
bpf_lwt_seg6_action: Apply some SRv6 network programming actions (specifically End.X, End.T, End.B6 and End.B6.Encap)The specifications of these helpers are provided in the patch (see
include/uapi/linux/bpf.h).The non-sensitive fields of the SRH are the following : flags, tag and TLVs. The other fields can not be modified, to maintain the SRH integrity. Flags, tag and TLVs can easily be modified as their validity can be checked afterwards via
seg6_validate_srh. It is not allowed to modify the segments directly. If one wants to add segments on the path, he should stack a new SRH using the End.B6 action viabpf_lwt_seg6_action.Growing, shrinking or editing TLVs via the helpers will flag the SRH as invalid, and it will have to be re-validated before re-entering the IPv6 layer. This flag is stored in a per-CPU buffer, along with the current header length in bytes.
Storing the SRH len in bytes in the control block is mandatory when using
bpf_lwt_seg6_adjust_srh. The Header Ext. Length field contains the SRH len rounded to 8 bytes (a padding TLV can be inserted to ensure the 8-bytes boundary). When adding/deleting TLVs within the BPF program, the SRH may temporary be in an invalid state where its length cannot be rounded to 8 bytes without remainder, hence the need to store the length in bytes separately. The caller of the BPF program can then ensure that the SRH's final length is valid using this value. Again, a final SRH modified by a BPF program which doesn’t respect the 8-bytes boundary will be discarded as it will be considered as invalid.Finally, a fourth helper is provided,
bpf_lwt_push_encap, which is available from the LWT BPF IN hook, but not from the seg6local BPF one. This helper allows to encapsulate a Segment Routing Header (either with a new outer IPv6 header, or by inlining it directly in the existing IPv6 header) into a non-SRv6 packet. This helper is required if we want to offer the possibility to dynamically encapsulate a SRH for non-SRv6 packet, as the BPF seg6local hook only works on traffic already containing a SRH. This is the BPF equivalent of the seg6 LWT infrastructure, which achieves the same purpose but with a static SRH per route.These helpers require
CONFIG_IPV6=y(and not =m).Signed-off-by Mathieu Xhonneux <[email protected]>Acked-by David Lebrun <[email protected]>--- include/net/seg6_local.h | 8 ++ include/uapi/linux/bpf.h | 97 +++++++++++++++- net/core/filter.c | 282 +++++++++++++++++++++++++++++++++++++++++++---- net/ipv6/Kconfig | 5 + net/ipv6/seg6_local.c | 2 + 5 files changed, 369 insertions(+), 25 deletions(-)include/net/seg6_local.h +8
-0@@ -15,10 +15,18 @@#ifndef _NET_SEG6_LOCAL_H#define _NET_SEG6_LOCAL_H +#include <linux/percpu.h> #include <linux/net.h> #include <linux/ipv6.h> extern int seg6_lookup_nexthop (struct sk_buff *skb,struct in6_addr *nhaddr,u32 tbl_id); +struct seg6_bpf_srh_state { +bool valid; +u16 hdrlen; +}; + +DECLARE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states); +#endif include/uapi/linux/bpf.h +95
-2@@ -1828,7 +1828,6 @@ union bpf_attr {* Return * 0 on success, or a negative error in case of failure. * -* * int bpf_fib_lookup(void *ctx, struct bpf_fib_lookup *params, int plen, u32 flags) * Description * Do FIB lookup in kernel tables using parameters in *params*. @@ -1855,6 +1854,90 @@ union bpf_attr {* Egress device index on success, 0 if packet needs to continue * up the stack for further processing or a negative error in case * of failure. +* +* int bpf_lwt_push_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len) +* Description +* Encapsulate the packet associated to *skb* within a Layer 3 +* protocol header. This header is provided in the buffer at +* address *hdr*, with *len* its size in bytes. *type* indicates +* the protocol of the header and can be one of: +* +* **BPF_LWT_ENCAP_SEG6** +* IPv6 encapsulation with Segment Routing Header +* (**struct ipv6_sr_hdr**). *hdr* only contains the SRH, +* the IPv6 header is computed by the kernel. +* **BPF_LWT_ENCAP_SEG6_INLINE** +* Only works if *skb* contains an IPv6 packet. Insert a +* Segment Routing Header (**struct ipv6_sr_hdr**) inside +* the IPv6 header. +* +* A call to this helper is susceptible to change the underlaying +* packet buffer. Therefore, at load time, all checks on pointers +* previously done by the verifier are invalidated and must be +* performed again, if the helper is used in combination with +* direct packet access. +* Return +* 0 on success, or a negative error in case of failure. +* +* int bpf_lwt_seg6_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len) +* Description +* Store *len* bytes from address *from* into the packet +* associated to *skb*, at *offset*. Only the flags, tag and TLVs +* inside the outermost IPv6 Segment Routing Header can be +* modified through this helper. +* +* A call to this helper is susceptible to change the underlaying +* packet buffer. Therefore, at load time, all checks on pointers +* previously done by the verifier are invalidated and must be +* performed again, if the helper is used in combination with +* direct packet access. +* Return +* 0 on success, or a negative error in case of failure. +* +* int bpf_lwt_seg6_adjust_srh(struct sk_buff *skb, u32 offset, s32 delta) +* Description +* Adjust the size allocated to TLVs in the outermost IPv6 +* Segment Routing Header contained in the packet associated to +* *skb*, at position *offset* by *delta* bytes. Only offsets +* after the segments are accepted. *delta* can be as well +* positive (growing) as negative (shrinking). +* +* A call to this helper is susceptible to change the underlaying +* packet buffer. Therefore, at load time, all checks on pointers +* previously done by the verifier are invalidated and must be +* performed again, if the helper is used in combination with +* direct packet access. +* Return +* 0 on success, or a negative error in case of failure. +* +* int bpf_lwt_seg6_action(struct sk_buff *skb, u32 action, void *param, u32 param_len) +* Description +* Apply an IPv6 Segment Routing action of type *action* to the +* packet associated to *skb*. Each action takes a parameter +* contained at address *param*, and of length *param_len* bytes. +* *action* can be one of: +* +* **SEG6_LOCAL_ACTION_END_X** +* End.X action: Endpoint with Layer-3 cross-connect. +* Type of *param*: **struct in6_addr**. +* **SEG6_LOCAL_ACTION_END_T** +* End.T action: Endpoint with specific IPv6 table lookup. +* Type of *param*: **int**. +* **SEG6_LOCAL_ACTION_END_B6** +* End.B6 action: Endpoint bound to an SRv6 policy. +* Type of param: **struct ipv6_sr_hdr**. +* **SEG6_LOCAL_ACTION_END_B6_ENCAP** +* End.B6.Encap action: Endpoint bound to an SRv6 +* encapsulation policy. +* Type of param: **struct ipv6_sr_hdr**. +* +* A call to this helper is susceptible to change the underlaying +* packet buffer. Therefore, at load time, all checks on pointers +* previously done by the verifier are invalidated and must be +* performed again, if the helper is used in combination with +* direct packet access. +* Return +* 0 on success, or a negative error in case of failure. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \@@ -1926,7 +2009,11 @@ union bpf_attr {FN (skb_get_xfrm_state), \FN (get_stack), \FN (skb_load_bytes_relative), \ -FN (fib_lookup), +FN (fib_lookup), \ +FN (lwt_push_encap), \ +FN (lwt_seg6_store_bytes), \ +FN (lwt_seg6_adjust_srh), \ +FN (lwt_seg6_action),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call @@ -1993,6 +2080,12 @@ enum bpf_hdr_start_off { BPF_HDR_START_NET, }; +/* Encapsulation type for BPF_FUNC_lwt_push_encap helper. */ +enum bpf_lwt_encap_mode { +BPF_LWT_ENCAP_SEG6 , +BPF_LWT_ENCAP_SEG6_INLINE +}; +/* user accessible mirror of in-kernel sk_buff. * new fields can only be added to the end of this structure */ net/core/filter.c +259
-23@@ -64,6 +64,10 @@#include <net/ip_fib.h> #include <net/flow.h> #include <net/arp.h> +#include <net/ipv6.h> +#include <linux/seg6_local.h> +#include <net/seg6.h> +#include <net/seg6_local.h> /** * sk_filter_trim_cap - run a packet through a socket filter @@ -3326,28 +3330,6 @@ static const struct bpf_func_proto bpf_xdp_redirect_map_proto = { .arg3_type = ARG_ANYTHING, }; -bool bpf_helper_changes_pkt_data (void *func) -{ -if (func == bpf_skb_vlan_push || - func == bpf_skb_vlan_pop || - func == bpf_skb_store_bytes || - func == bpf_skb_change_proto || - func == bpf_skb_change_head || - func == bpf_skb_change_tail || - func == bpf_skb_adjust_room || - func == bpf_skb_pull_data || - func == bpf_clone_redirect || - func == bpf_l3_csum_replace || - func == bpf_l4_csum_replace || - func == bpf_xdp_adjust_head || - func == bpf_xdp_adjust_meta || - func == bpf_msg_pull_data || - func == bpf_xdp_adjust_tail) -return true; - -return false; -} -static unsigned long bpf_skb_copy (void *dst_buff,const void *skb,unsigned long off,unsigned long len) {@@ -4295,6 +4277,261 @@ static const struct bpf_func_proto bpf_skb_fib_lookup_proto = { .arg4_type = ARG_ANYTHING, }; +#if IS_ENABLED (CONFIG_IPV6_SEG6_BPF ) +static int bpf_push_seg6_encap (struct sk_buff *skb,u32 type,void *hdr,u32 len) +{ +int err; +struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)hdr; + +if (!seg6_validate_srh (srh, len)) +return -EINVAL ; + +switch (type) { +case BPF_LWT_ENCAP_SEG6_INLINE : +if (skb->protocol !=htons (ETH_P_IPV6 )) +return -EBADMSG ; + + err =seg6_do_srh_inline (skb, srh); +break ; +case BPF_LWT_ENCAP_SEG6 : +skb_reset_inner_headers (skb); + skb->encapsulation =1 ; + err =seg6_do_srh_encap (skb, srh,IPPROTO_IPV6 ); +break ; +default : +return -EINVAL ; + } +if (err) +return err; + +ipv6_hdr (skb)->payload_len =htons (skb->len -sizeof (struct ipv6hdr )); +skb_set_transport_header (skb,sizeof (struct ipv6hdr )); + +return seg6_lookup_nexthop (skb,NULL ,0 ); +} +#endif /* CONFIG_IPV6_SEG6_BPF */ + +BPF_CALL_4(bpf_lwt_push_encap, struct sk_buff *, skb, u32, type, void *, hdr, + u32, len) +{ +switch (type) { +#if IS_ENABLED (CONFIG_IPV6_SEG6_BPF ) +case BPF_LWT_ENCAP_SEG6 : +case BPF_LWT_ENCAP_SEG6_INLINE : +return bpf_push_seg6_encap (skb, type, hdr, len); +#endif +default : +return -EINVAL ; + } +} + +static const struct bpf_func_proto bpf_lwt_push_encap_proto = { + .func = bpf_lwt_push_encap, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , + .arg2_type =ARG_ANYTHING , + .arg3_type =ARG_PTR_TO_MEM , + .arg4_type =ARG_CONST_SIZE +}; + +BPF_CALL_4(bpf_lwt_seg6_store_bytes, struct sk_buff *, skb, u32, offset, + const void *, from, u32, len) +{ +#if IS_ENABLED (CONFIG_IPV6_SEG6_BPF ) +struct seg6_bpf_srh_state *srh_state = +this_cpu_ptr (&seg6_bpf_srh_states); +void *srh_tlvs, *srh_end, *ptr; +struct ipv6_sr_hdr *srh; +int srhoff =0 ; + +if (ipv6_find_hdr (skb, &srhoff,IPPROTO_ROUTING ,NULL ,NULL ) <0 ) +return -EINVAL ; + + srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); + srh_tlvs = (void *)((char *)srh + ((srh->first_segment +1 ) <<4 )); + srh_end = (void *)((char *)srh +sizeof (*srh) + srh_state->hdrlen); + + ptr = skb->data + offset; +if (ptr >= srh_tlvs && ptr + len <= srh_end) + srh_state->valid =0 ; +else if (ptr < (void *)&srh->flags || + ptr + len > (void *)&srh->segments) +return -EFAULT ; + +if (unlikely (bpf_try_make_writable (skb, offset + len))) +return -EFAULT ; + +memcpy (ptr, from, len); +return 0 ; +#else /* CONFIG_IPV6_SEG6_BPF */ +return -EOPNOTSUPP ; +#endif +} + +static const struct bpf_func_proto bpf_lwt_seg6_store_bytes_proto = { + .func = bpf_lwt_seg6_store_bytes, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , + .arg2_type =ARG_ANYTHING , + .arg3_type =ARG_PTR_TO_MEM , + .arg4_type =ARG_CONST_SIZE +}; + +BPF_CALL_4(bpf_lwt_seg6_action, struct sk_buff *, skb, + u32, action, void *, param, u32, param_len) +{ +#if IS_ENABLED (CONFIG_IPV6_SEG6_BPF ) +struct seg6_bpf_srh_state *srh_state = +this_cpu_ptr (&seg6_bpf_srh_states); +struct ipv6_sr_hdr *srh; +int srhoff =0 ; +int err; + +if (ipv6_find_hdr (skb, &srhoff,IPPROTO_ROUTING ,NULL ,NULL ) <0 ) +return -EINVAL ; + srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); + +if (!srh_state->valid) { +if (unlikely ((srh_state->hdrlen &7 ) !=0 )) +return -EBADMSG ; + + srh->hdrlen = (u8)(srh_state->hdrlen >>3 ); +if (unlikely (!seg6_validate_srh (srh, (srh->hdrlen +1 ) <<3 ))) +return -EBADMSG ; + + srh_state->valid =1 ; + } + +switch (action) { +case SEG6_LOCAL_ACTION_END_X : +if (param_len !=sizeof (struct in6_addr )) +return -EINVAL ; +return seg6_lookup_nexthop (skb, (struct in6_addr *)param,0 ); +case SEG6_LOCAL_ACTION_END_T : +if (param_len !=sizeof (int )) +return -EINVAL ; +return seg6_lookup_nexthop (skb,NULL , *(int *)param); +case SEG6_LOCAL_ACTION_END_B6 : + err =bpf_push_seg6_encap (skb,BPF_LWT_ENCAP_SEG6_INLINE , + param, param_len); +if (!err) + srh_state->hdrlen = + ((struct ipv6_sr_hdr *)param)->hdrlen <<3 ; +return err; +case SEG6_LOCAL_ACTION_END_B6_ENCAP : + err =bpf_push_seg6_encap (skb,BPF_LWT_ENCAP_SEG6 , + param, param_len); +if (!err) + srh_state->hdrlen = + ((struct ipv6_sr_hdr *)param)->hdrlen <<3 ; +return err; +default : +return -EINVAL ; + } +#else /* CONFIG_IPV6_SEG6_BPF */ +return -EOPNOTSUPP ; +#endif +} + +static const struct bpf_func_proto bpf_lwt_seg6_action_proto = { + .func = bpf_lwt_seg6_action, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , + .arg2_type =ARG_ANYTHING , + .arg3_type =ARG_PTR_TO_MEM , + .arg4_type =ARG_CONST_SIZE +}; + +BPF_CALL_3(bpf_lwt_seg6_adjust_srh, struct sk_buff *, skb, u32, offset, + s32, len) +{ +#if IS_ENABLED (CONFIG_IPV6_SEG6_BPF ) +struct seg6_bpf_srh_state *srh_state = +this_cpu_ptr (&seg6_bpf_srh_states); +void *srh_end, *srh_tlvs, *ptr; +struct ipv6_sr_hdr *srh; +struct ipv6hdr *hdr; +int srhoff =0 ; +int ret; + +if (ipv6_find_hdr (skb, &srhoff,IPPROTO_ROUTING ,NULL ,NULL ) <0 ) +return -EINVAL ; + srh = (struct ipv6_sr_hdr *)(skb->data + srhoff); + + srh_tlvs = (void *)((unsigned char *)srh +sizeof (*srh) + + ((srh->first_segment +1 ) <<4 )); + srh_end = (void *)((unsigned char *)srh +sizeof (*srh) + + srh_state->hdrlen); + ptr = skb->data + offset; + +if (unlikely (ptr < srh_tlvs || ptr > srh_end)) +return -EFAULT ; +if (unlikely (len <0 && (void *)((char *)ptr - len) > srh_end)) +return -EFAULT ; + +if (len >0 ) { + ret =skb_cow_head (skb, len); +if (unlikely (ret <0 )) +return ret; + + ret =bpf_skb_net_hdr_push (skb, offset, len); + }else { + ret =bpf_skb_net_hdr_pop (skb, offset,-1 * len); + } +if (unlikely (ret <0 )) +return ret; + + hdr = (struct ipv6hdr *)skb->data; + hdr->payload_len =htons (skb->len -sizeof (struct ipv6hdr )); + +bpf_compute_data_pointers (skb); + srh_state->hdrlen += len; + srh_state->valid =0 ; +return 0 ; +#else /* CONFIG_IPV6_SEG6_BPF */ +return -EOPNOTSUPP ; +#endif +} + +static const struct bpf_func_proto bpf_lwt_seg6_adjust_srh_proto = { + .func = bpf_lwt_seg6_adjust_srh, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_PTR_TO_CTX , + .arg2_type =ARG_ANYTHING , + .arg3_type =ARG_ANYTHING , +}; + +bool bpf_helper_changes_pkt_data (void *func) +{ +if (func == bpf_skb_vlan_push || + func == bpf_skb_vlan_pop || + func == bpf_skb_store_bytes || + func == bpf_skb_change_proto || + func == bpf_skb_change_head || + func == bpf_skb_change_tail || + func == bpf_skb_adjust_room || + func == bpf_skb_pull_data || + func == bpf_clone_redirect || + func == bpf_l3_csum_replace || + func == bpf_l4_csum_replace || + func == bpf_xdp_adjust_head || + func == bpf_xdp_adjust_meta || + func == bpf_msg_pull_data || + func == bpf_xdp_adjust_tail || + func == bpf_lwt_push_encap || + func == bpf_lwt_seg6_store_bytes || + func == bpf_lwt_seg6_adjust_srh || + func == bpf_lwt_seg6_action + ) +return true; + +return false; +} +static const struct bpf_func_proto *bpf_base_func_proto (enum bpf_func_id func_id) {@@ -4703,7 +4940,6 @@ static bool lwt_is_valid_access(int off, int size,return bpf_skb_is_valid_access (off, size, type, prog, info); } -/* Attach type specific accesses */ static bool __sock_filter_check_attach_type(int off, enum bpf_access_type access_type,net/ipv6/Kconfig +5
-0@@ -330,4 +330,9 @@ config IPV6_SEG6_HMAC If unsure, say N. +config IPV6_SEG6_BPF + def_bool y + depends on IPV6_SEG6_LWTUNNEL + depends on IPV6 = y + endif # IPV6net/ipv6/seg6_local.c +2
-1@@ -449,6 +449,8 @@ static int input_action_end_b6_encap(struct sk_buff *skb,return err; } +DEFINE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states); +static struct seg6_action_desc seg6_action_table[] = { { .action =SEG6_LOCAL_ACTION_END , --2.16.1
PATCH v5 4/6 bpf: Split lwt inout verifier structures
In reply to Mathieu XhonneuxThe new bpf_lwt_push_encap helper should only be accessible within the LWT BPF IN hook, and not the OUT one, as this may lead to a skb under panic.
At the moment, both LWT BPF IN and OUT share the same list of helpers, whose calls are authorized by the verifier. This patch separates the verifier ops for the IN and OUT hooks, and allows the IN hook to call the bpf_lwt_push_encap helper.
This patch is also the occasion to put all lwt_*_func_proto functions together for clarity. At the moment, socks_op_func_proto is in the middle of lwt_inout_func_proto and lwt_xmit_func_proto.
--- include/linux/bpf_types.h | 4 +-- net/core/filter.c | 83 +++++++++++++++++++++++++++++------------------ 2 files changed, 54 insertions(+), 33 deletions(-)
include/linux/bpf_types.h +2 -2
@@ -9,8 +9,8 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_XDP, xdp) BPF_PROG_TYPE(BPF_PROG_TYPE_CGROUP_SKB, cg_skb) BPF_PROG_TYPE(BPF_PROG_TYPE_CGROUP_SOCK, cg_sock) BPF_PROG_TYPE(BPF_PROG_TYPE_CGROUP_SOCK_ADDR, cg_sock_addr) -BPF_PROG_TYPE(BPF_PROG_TYPE_LWT_IN, lwt_inout) -BPF_PROG_TYPE(BPF_PROG_TYPE_LWT_OUT, lwt_inout) +BPF_PROG_TYPE(BPF_PROG_TYPE_LWT_IN, lwt_in) +BPF_PROG_TYPE(BPF_PROG_TYPE_LWT_OUT, lwt_out) BPF_PROG_TYPE(BPF_PROG_TYPE_LWT_XMIT, lwt_xmit) BPF_PROG_TYPE(BPF_PROG_TYPE_SOCK_OPS, sock_ops)BPF_PROG_TYPE (BPF_PROG_TYPE_SK_SKB , sk_skb)
net/core/filter.c +52 -31
111 lines not on this page · raw message
Signature · 1 line
2.16.1
PATCH v5 5/6 ipv6: sr: Add seg6local action End.BPF
In reply to Mathieu XhonneuxThis patch adds the End.BPF action to the LWT seg6local infrastructure. This action works like any other seg6local End action, meaning that an IPv6 header with SRH is needed, whose DA has to be equal to the SID of the action. It will also advance the SRH to the next segment, the BPF program does not have to take care of this.
Since the BPF program may not be a source of instability in the kernel, it is important to ensure that the integrity of the packet is maintained before yielding it back to the IPv6 layer. The hook hence keeps track if the SRH has been altered through the helpers, and re-validates its content if needed with seg6_validate_srh. The state kept for validation is stored in a per-CPU buffer. The BPF program is not allowed to directly write into the packet, and only some fields of the SRH can be altered through the helper bpf_lwt_seg6_store_bytes.
Performances profiling has shown that the SRH re-validation does not induce a significant overhead. If the altered SRH is deemed as invalid, the packet is dropped.
This validation is also done before executing any action through bpf_lwt_seg6_action, and will not be performed again if the SRH is not modified after calling the action.
The BPF program may return 3 types of return codes:
- BPF_OK: the End.BPF action will look up the next destination through seg6_lookup_nexthop.
- BPF_REDIRECT: if an action has been executed through the bpf_lwt_seg6_action helper, the BPF program should return this value, as the skb's destination is already set and the default lookup should not be performed.
- BPF_DROP : the packet will be dropped.
--- include/linux/bpf_types.h | 1 + include/uapi/linux/bpf.h | 1 + include/uapi/linux/seg6_local.h | 3 + kernel/bpf/verifier.c | 1 + net/core/filter.c | 25 +++++++ net/ipv6/seg6_local.c | 158 +++++++++++++++++++++++++++++++++++++++- tools/lib/bpf/libbpf.c | 1 + 7 files changed, 187 insertions(+), 3 deletions(-)
include/linux/bpf_types.h +1 -0
@@ -12,6 +12,7 @@ BPF_PROG_TYPE(BPF_PROG_TYPE_CGROUP_SOCK_ADDR, cg_sock_addr) BPF_PROG_TYPE(BPF_PROG_TYPE_LWT_IN, lwt_in) BPF_PROG_TYPE(BPF_PROG_TYPE_LWT_OUT, lwt_out) BPF_PROG_TYPE(BPF_PROG_TYPE_LWT_XMIT, lwt_xmit) +BPF_PROG_TYPE(BPF_PROG_TYPE_LWT_SEG6LOCAL, lwt_seg6local) BPF_PROG_TYPE(BPF_PROG_TYPE_SOCK_OPS, sock_ops) BPF_PROG_TYPE(BPF_PROG_TYPE_SK_SKB, sk_skb)BPF_PROG_TYPE (BPF_PROG_TYPE_SK_MSG , sk_msg)
include/uapi/linux/bpf.h +1 -0
@@ -140,6 +140,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_SK_MSG ,BPF_PROG_TYPE_RAW_TRACEPOINT ,BPF_PROG_TYPE_CGROUP_SOCK_ADDR , + BPF_PROG_TYPE_LWT_SEG6LOCAL, }; enum bpf_attach_type {
include/uapi/linux/seg6_local.h +3 -0
@@ -25,6 +25,7 @@ enum {SEG6_LOCAL_NH6 ,SEG6_LOCAL_IIF ,SEG6_LOCAL_OIF , +SEG6_LOCAL_BPF , __SEG6_LOCAL_MAX, };#define SEG6_LOCAL_MAX (__SEG6_LOCAL_MAX - 1)
@@ -59,6 +60,8 @@ enum {SEG6_LOCAL_ACTION_END_AS =13 ,/* forward to SR-unaware VNF with masquerading */ SEG6_LOCAL_ACTION_END_AM =14 , +/* custom BPF action */ +SEG6_LOCAL_ACTION_END_BPF =15 , __SEG6_LOCAL_ACTION_MAX, };
kernel/bpf/verifier.c +1 -0
@@ -1262,6 +1262,7 @@ static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,switch (env->prog->type) {case BPF_PROG_TYPE_LWT_IN :case BPF_PROG_TYPE_LWT_OUT : +case BPF_PROG_TYPE_LWT_SEG6LOCAL :/* dst_input() and dst_output() can't write for now */ if (t ==BPF_WRITE )return false;
net/core/filter.c +25 -0
@@ -4847,6 +4847,21 @@ lwt_xmit_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog) } } +static const struct bpf_func_proto * +lwt_seg6local_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) +{ +switch (func_id) { +case BPF_FUNC_lwt_seg6_store_bytes: +return &bpf_lwt_seg6_store_bytes_proto; +case BPF_FUNC_lwt_seg6_action: +return &bpf_lwt_seg6_action_proto; +case BPF_FUNC_lwt_seg6_adjust_srh: +return &bpf_lwt_seg6_adjust_srh_proto; +default : +return lwt_out_func_proto (func_id, prog); + } +} +static bool bpf_skb_is_valid_access (int off,int size,enum bpf_access_type type,const struct bpf_prog *prog,struct bpf_insn_access_aux *info)
@@ -6447,6 +6462,16 @@ const struct bpf_prog_ops lwt_xmit_prog_ops = { .test_run = bpf_prog_test_run_skb, }; +const struct bpf_verifier_ops lwt_seg6local_verifier_ops = { + .get_func_proto = lwt_seg6local_func_proto, + .is_valid_access = lwt_is_valid_access, + .convert_ctx_access = bpf_convert_ctx_access, +}; + +const struct bpf_prog_ops lwt_seg6local_prog_ops = { + .test_run = bpf_prog_test_run_skb, +}; +const struct bpf_verifier_ops cg_sock_verifier_ops = { .get_func_proto = sock_filter_func_proto, .is_valid_access = sock_filter_is_valid_access,
net/ipv6/seg6_local.c +155 -3
225 lines not on this page · raw message
tools/lib/bpf/libbpf.c +1 -0
@@ -1450,6 +1450,7 @@ static bool bpf_prog_type__needs_kver(enum bpf_prog_type type)case BPF_PROG_TYPE_LWT_IN :case BPF_PROG_TYPE_LWT_OUT :case BPF_PROG_TYPE_LWT_XMIT : +case BPF_PROG_TYPE_LWT_SEG6LOCAL :case BPF_PROG_TYPE_SOCK_OPS :case BPF_PROG_TYPE_SK_SKB :case BPF_PROG_TYPE_CGROUP_DEVICE :
Signature · 1 line
2.16.1
PATCH v5 6/6 selftests/bpf: test for seg6local End.BPF action
In reply to Mathieu XhonneuxAdd a new test for the seg6local End.BPF action. The following helpers are also tested :
- bpf_lwt_push_encap within the LWT BPF IN hook
- bpf_lwt_seg6_action
- bpf_lwt_seg6_adjust_srh
- bpf_lwt_seg6_store_bytes
A chain of End.BPF actions is built. The SRH is injected through a LWT BPF IN hook before the chain. Each End.BPF action validates the previous one, otherwise the packet is dropped.
The test succeeds if the last node in the chain receives the packet and the UDP datagram contained can be retrieved from userspace.
--- tools/include/uapi/linux/bpf.h | 98 ++++- tools/testing/selftests/bpf/Makefile | 5 +- tools/testing/selftests/bpf/bpf_helpers.h | 12 + tools/testing/selftests/bpf/test_lwt_seg6local.c | 438 ++++++++++++++++++++++ tools/testing/selftests/bpf/test_lwt_seg6local.sh | 140 +++++++ 5 files changed, 689 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/bpf/test_lwt_seg6local.c create mode 100755 tools/testing/selftests/bpf/test_lwt_seg6local.sh
tools/include/uapi/linux/bpf.h +96 -2
@@ -140,6 +140,7 @@ enum bpf_prog_type {BPF_PROG_TYPE_SK_MSG ,BPF_PROG_TYPE_RAW_TRACEPOINT ,BPF_PROG_TYPE_CGROUP_SOCK_ADDR , + BPF_PROG_TYPE_LWT_SEG6LOCAL, }; enum bpf_attach_type {
@@ -1828,7 +1829,6 @@ union bpf_attr {* Return * 0 on success, or a negative error in case of failure. * -* * int bpf_fib_lookup(void *ctx, struct bpf_fib_lookup *params, int plen, u32 flags) * Description * Do FIB lookup in kernel tables using parameters in *params*.
@@ -1855,6 +1855,90 @@ union bpf_attr {* Egress device index on success, 0 if packet needs to continue * up the stack for further processing or a negative error in case * of failure. +* +* int bpf_lwt_push_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len) +* Description +* Encapsulate the packet associated to *skb* within a Layer 3 +* protocol header. This header is provided in the buffer at +* address *hdr*, with *len* its size in bytes. *type* indicates +* the protocol of the header and can be one of: +* +* **BPF_LWT_ENCAP_SEG6** +* IPv6 encapsulation with Segment Routing Header +* (**struct ipv6_sr_hdr**). *hdr* only contains the SRH, +* the IPv6 header is computed by the kernel. +* **BPF_LWT_ENCAP_SEG6_INLINE** +* Only works if *skb* contains an IPv6 packet. Insert a +* Segment Routing Header (**struct ipv6_sr_hdr**) inside +* the IPv6 header. +* +* A call to this helper is susceptible to change the underlaying +* packet buffer. Therefore, at load time, all checks on pointers +* previously done by the verifier are invalidated and must be +* performed again, if the helper is used in combination with +* direct packet access. +* Return +* 0 on success, or a negative error in case of failure. +* +* int bpf_lwt_seg6_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len) +* Description +* Store *len* bytes from address *from* into the packet +* associated to *skb*, at *offset*. Only the flags, tag and TLVs +* inside the outermost IPv6 Segment Routing Header can be +* modified through this helper. +* +* A call to this helper is susceptible to change the underlaying +* packet buffer. Therefore, at load time, all checks on pointers +* previously done by the verifier are invalidated and must be +* performed again, if the helper is used in combination with +* direct packet access. +* Return +* 0 on success, or a negative error in case of failure. +* +* int bpf_lwt_seg6_adjust_srh(struct sk_buff *skb, u32 offset, s32 delta) +* Description +* Adjust the size allocated to TLVs in the outermost IPv6 +* Segment Routing Header contained in the packet associated to +* *skb*, at position *offset* by *delta* bytes. Only offsets +* after the segments are accepted. *delta* can be as well +* positive (growing) as negative (shrinking). +* +* A call to this helper is susceptible to change the underlaying +* packet buffer. Therefore, at load time, all checks on pointers +* previously done by the verifier are invalidated and must be +* performed again, if the helper is used in combination with +* direct packet access. +* Return +* 0 on success, or a negative error in case of failure. +* +* int bpf_lwt_seg6_action(struct sk_buff *skb, u32 action, void *param, u32 param_len) +* Description +* Apply an IPv6 Segment Routing action of type *action* to the +* packet associated to *skb*. Each action takes a parameter +* contained at address *param*, and of length *param_len* bytes. +* *action* can be one of: +* +* **SEG6_LOCAL_ACTION_END_X** +* End.X action: Endpoint with Layer-3 cross-connect. +* Type of *param*: **struct in6_addr**. +* **SEG6_LOCAL_ACTION_END_T** +* End.T action: Endpoint with specific IPv6 table lookup. +* Type of *param*: **int**. +* **SEG6_LOCAL_ACTION_END_B6** +* End.B6 action: Endpoint bound to an SRv6 policy. +* Type of param: **struct ipv6_sr_hdr**. +* **SEG6_LOCAL_ACTION_END_B6_ENCAP** +* End.B6.Encap action: Endpoint bound to an SRv6 +* encapsulation policy. +* Type of param: **struct ipv6_sr_hdr**. +* +* A call to this helper is susceptible to change the underlaying +* packet buffer. Therefore, at load time, all checks on pointers +* previously done by the verifier are invalidated and must be +* performed again, if the helper is used in combination with +* direct packet access. +* Return +* 0 on success, or a negative error in case of failure. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -1926,7 +2010,11 @@ union bpf_attr {FN (skb_get_xfrm_state), \FN (get_stack), \FN (skb_load_bytes_relative), \ -FN (fib_lookup), +FN (fib_lookup), \ +FN (lwt_push_encap), \ +FN (lwt_seg6_store_bytes), \ +FN (lwt_seg6_adjust_srh), \ +FN (lwt_seg6_action),/* integer value in 'imm' field of BPF_CALL instruction selects which helper * function eBPF program intends to call
@@ -1993,6 +2081,12 @@ enum bpf_hdr_start_off { BPF_HDR_START_NET, }; +/* Encapsulation type for BPF_FUNC_lwt_push_encap helper. */ +enum bpf_lwt_encap_mode { +BPF_LWT_ENCAP_SEG6 , +BPF_LWT_ENCAP_SEG6_INLINE +}; +/* user accessible mirror of in-kernel sk_buff. * new fields can only be added to the end of this structure */
tools/testing/selftests/bpf/Makefile +3 -2
@@ -33,7 +33,7 @@ TEST_GEN_FILES = test_pkt_access.o test_xdp.o test_l4lb.o test_tcp_estats.o test sample_map_ret0.o test_tcpbpf_kern.o test_stacktrace_build_id.o \ sockmap_tcp_msg_prog.o connect4_prog.o connect6_prog.o test_adjust_tail.o \ test_btf_haskv.o test_btf_nokv.o test_sockmap_kern.o test_tunnel_kern.o \ - test_get_stack_rawtp.o + test_get_stack_rawtp.o test_lwt_seg6local.o # Order correspond to 'make run_tests' order TEST_PROGS := test_kmod.sh \
@@ -42,7 +42,8 @@ TEST_PROGS := test_kmod.sh \ test_xdp_meta.sh \ test_offload.py \ test_sock_addr.sh \ - test_tunnel.sh + test_tunnel.sh \ + test_lwt_seg6local.sh # Compile but not part of 'make run_tests' TEST_GEN_PROGS_EXTENDED = test_libbpf_open test_sock_addr
tools/testing/selftests/bpf/bpf_helpers.h +12 -0
@@ -106,6 +106,18 @@ static int (*bpf_get_stack)(void *ctx, void *buf, int size, int flags) =static int (*bpf_fib_lookup)(void *ctx,struct bpf_fib_lookup *params,int plen,__u32 flags) = (void *) BPF_FUNC_fib_lookup; +static int (*bpf_lwt_push_encap)(void *ctx,unsigned int type,void *hdr, +unsigned int len) = + (void *) BPF_FUNC_lwt_push_encap; +static int (*bpf_lwt_seg6_store_bytes)(void *ctx,unsigned int offset, +void *from,unsigned int len) = + (void *) BPF_FUNC_lwt_seg6_store_bytes; +static int (*bpf_lwt_seg6_action)(void *ctx,unsigned int action,void *param, +unsigned int param_len) = + (void *) BPF_FUNC_lwt_seg6_action; +static int (*bpf_lwt_seg6_adjust_srh)(void *ctx,unsigned int offset, +unsigned int len) = + (void *) BPF_FUNC_lwt_seg6_adjust_srh;/* llvm builtin functions that eBPF C program may use to * emit BPF_LD_ABS and BPF_LD_IND instructions
tools/testing/selftests/bpf/test_lwt_seg6local.c new file +438 -0
438 lines not on this page · raw message
tools/testing/selftests/bpf/test_lwt_seg6local.sh new file +140 -0
140 lines not on this page · raw message
Signature · 1 line
2.16.1
Sorry for the v4 still throwing warnings from the kbuild bot, this version should be OK.
2018-05-12 18:25 GMT+01:00 Mathieu Xhonneux <[email protected]>:
Quoted 88 lines
As of Linux 4.14, it is possible to define advanced local processing for IPv6 packets with a Segment Routing Header through the seg6local LWT infrastructure. This LWT implements the network programming principles defined in the IETF “SRv6 Network Programming” draft.
The implemented operations are generic, and it would be very interesting to be able to implement user-specific seg6local actions, without having to modify the kernel directly. To do so, this patchset adds an End.BPF action to seg6local, powered by some specific Segment Routing-related helpers, which provide SR functionalities that can be applied on the packet. This BPF hook would then allow to implement specific actions at native kernel speed such as OAM features, advanced SR SDN policies, SRv6 actions like Segment Routing Header (SRH) encapsulation depending on the content of the packet, etc.
This patchset is divided in 6 patches, whose main features are :
- A new seg6local action End.BPF with the corresponding new BPF program type
BPF_PROG_TYPE_LWT_SEG6LOCAL. Such attached BPF program can be passed to the LWT seg6local through netlink, the same way as the LWT BPF hook operates.
- 3 new BPF helpers for the seg6local BPF hook, allowing to edit/grow/ shrink a SRH and apply on a packet some of the generic SRv6 actions.
- 1 new BPF helper for the LWT BPF IN hook, allowing to add a SRH through encapsulation (via IPv6 encapsulation or inlining if the packet contains already an IPv6 header).As this patchset adds a new LWT BPF hook, I took into account the result of the discussions when the LWT BPF infrastructure got merged. Hence, the seg6local BPF hook doesn’t allow write access to
skb->datadirectly, only the SRH can be modified through specific helpers, which ensures that the integrity of the packet is maintained.
More details are available in the related patches messages.The performances of this BPF hook have been assessed with the BPF JIT enabled on a Intel Xeon X3440 processors with 4 cores and 8 threads clocked at 2.53 GHz. No throughput losses are noted with the seg6local BPF hook when the BPF program does nothing (440kpps). Adding a 8-bytes TLV (1 call each to
bpf_lwt_seg6_adjust_srhandbpf_lwt_seg6_store_bytes) drops the throughput to 410kpps, and inlining a SRH viabpf_lwt_seg6_actiondrops the throughput to 420kpps.
All throughputs are stable.-------
v2: move the SRH integrity state fromskb->cbto a per-cpu buffer
v3: - document helpers in man-page style
- fix kbuild bugs
- un-break BPF LWT out hook
-bpf_push_seg6_encapis now static
-preempt_enableis now called when the packet is dropped ininput_action_end_bpf
v4: fix kbuild bugs whenCONFIG_IPV6=m
v5: fix kbuild sparse warnings whenCONFIG_IPV6=mThanks.
Mathieu Xhonneux (6):
ipv6: sr: makeseg6.hincludable without IPv6
ipv6: sr: export functionlookup_nexthop
bpf: Add IPv6 Segment Routing helpers
bpf: Split lwt inout verifier structures
ipv6: sr: Add seg6local action End.BPF selftests/bpf: test for seg6local End.BPF actioninclude/linux/bpf_types.h | 5 +- include/net/seg6.h | 7 +- include/net/seg6_local.h | 32 ++ include/uapi/linux/bpf.h | 98 ++++- include/uapi/linux/seg6_local.h | 3 + kernel/bpf/verifier.c | 1 + net/core/filter.c | 390 ++++++++++++++++--- net/ipv6/Kconfig | 5 + net/ipv6/seg6_local.c | 180 ++++++++- tools/include/uapi/linux/bpf.h | 98 ++++- tools/lib/bpf/libbpf.c | 1 + tools/testing/selftests/bpf/Makefile | 5 +- tools/testing/selftests/bpf/bpf_helpers.h | 12 + tools/testing/selftests/bpf/test_lwt_seg6local.c | 438 ++++++++++++++++++++++ tools/testing/selftests/bpf/test_lwt_seg6local.sh | 140 +++++++ 15 files changed, 1340 insertions(+), 75 deletions(-) create mode 100644 include/net/seg6_local.h create mode 100644 tools/testing/selftests/bpf/test_lwt_seg6local.c create mode 100755 tools/testing/selftests/bpf/test_lwt_seg6local.sh--
2.16.1