Skip to content

Commit fe94cc2

Browse files
Zashasborkmann
authored andcommitted
bpf: Add IPv6 Segment Routing helpers
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) - 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). Signed-off-by: Mathieu Xhonneux <[email protected]> Acked-by: David Lebrun <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 1c1e761 commit fe94cc2

File tree

5 files changed

+372
-24
lines changed

5 files changed

+372
-24
lines changed

Diff for: include/net/seg6_local.h

+8
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,18 @@
1515
#ifndef _NET_SEG6_LOCAL_H
1616
#define _NET_SEG6_LOCAL_H
1717

18+
#include <linux/percpu.h>
1819
#include <linux/net.h>
1920
#include <linux/ipv6.h>
2021

2122
extern int seg6_lookup_nexthop(struct sk_buff *skb, struct in6_addr *nhaddr,
2223
u32 tbl_id);
2324

25+
struct seg6_bpf_srh_state {
26+
bool valid;
27+
u16 hdrlen;
28+
};
29+
30+
DECLARE_PER_CPU(struct seg6_bpf_srh_state, seg6_bpf_srh_states);
31+
2432
#endif

Diff for: include/uapi/linux/bpf.h

+95-1
Original file line numberDiff line numberDiff line change
@@ -1902,6 +1902,90 @@ union bpf_attr {
19021902
* egress otherwise). This is the only flag supported for now.
19031903
* Return
19041904
* **SK_PASS** on success, or **SK_DROP** on error.
1905+
*
1906+
* int bpf_lwt_push_encap(struct sk_buff *skb, u32 type, void *hdr, u32 len)
1907+
* Description
1908+
* Encapsulate the packet associated to *skb* within a Layer 3
1909+
* protocol header. This header is provided in the buffer at
1910+
* address *hdr*, with *len* its size in bytes. *type* indicates
1911+
* the protocol of the header and can be one of:
1912+
*
1913+
* **BPF_LWT_ENCAP_SEG6**
1914+
* IPv6 encapsulation with Segment Routing Header
1915+
* (**struct ipv6_sr_hdr**). *hdr* only contains the SRH,
1916+
* the IPv6 header is computed by the kernel.
1917+
* **BPF_LWT_ENCAP_SEG6_INLINE**
1918+
* Only works if *skb* contains an IPv6 packet. Insert a
1919+
* Segment Routing Header (**struct ipv6_sr_hdr**) inside
1920+
* the IPv6 header.
1921+
*
1922+
* A call to this helper is susceptible to change the underlaying
1923+
* packet buffer. Therefore, at load time, all checks on pointers
1924+
* previously done by the verifier are invalidated and must be
1925+
* performed again, if the helper is used in combination with
1926+
* direct packet access.
1927+
* Return
1928+
* 0 on success, or a negative error in case of failure.
1929+
*
1930+
* int bpf_lwt_seg6_store_bytes(struct sk_buff *skb, u32 offset, const void *from, u32 len)
1931+
* Description
1932+
* Store *len* bytes from address *from* into the packet
1933+
* associated to *skb*, at *offset*. Only the flags, tag and TLVs
1934+
* inside the outermost IPv6 Segment Routing Header can be
1935+
* modified through this helper.
1936+
*
1937+
* A call to this helper is susceptible to change the underlaying
1938+
* packet buffer. Therefore, at load time, all checks on pointers
1939+
* previously done by the verifier are invalidated and must be
1940+
* performed again, if the helper is used in combination with
1941+
* direct packet access.
1942+
* Return
1943+
* 0 on success, or a negative error in case of failure.
1944+
*
1945+
* int bpf_lwt_seg6_adjust_srh(struct sk_buff *skb, u32 offset, s32 delta)
1946+
* Description
1947+
* Adjust the size allocated to TLVs in the outermost IPv6
1948+
* Segment Routing Header contained in the packet associated to
1949+
* *skb*, at position *offset* by *delta* bytes. Only offsets
1950+
* after the segments are accepted. *delta* can be as well
1951+
* positive (growing) as negative (shrinking).
1952+
*
1953+
* A call to this helper is susceptible to change the underlaying
1954+
* packet buffer. Therefore, at load time, all checks on pointers
1955+
* previously done by the verifier are invalidated and must be
1956+
* performed again, if the helper is used in combination with
1957+
* direct packet access.
1958+
* Return
1959+
* 0 on success, or a negative error in case of failure.
1960+
*
1961+
* int bpf_lwt_seg6_action(struct sk_buff *skb, u32 action, void *param, u32 param_len)
1962+
* Description
1963+
* Apply an IPv6 Segment Routing action of type *action* to the
1964+
* packet associated to *skb*. Each action takes a parameter
1965+
* contained at address *param*, and of length *param_len* bytes.
1966+
* *action* can be one of:
1967+
*
1968+
* **SEG6_LOCAL_ACTION_END_X**
1969+
* End.X action: Endpoint with Layer-3 cross-connect.
1970+
* Type of *param*: **struct in6_addr**.
1971+
* **SEG6_LOCAL_ACTION_END_T**
1972+
* End.T action: Endpoint with specific IPv6 table lookup.
1973+
* Type of *param*: **int**.
1974+
* **SEG6_LOCAL_ACTION_END_B6**
1975+
* End.B6 action: Endpoint bound to an SRv6 policy.
1976+
* Type of param: **struct ipv6_sr_hdr**.
1977+
* **SEG6_LOCAL_ACTION_END_B6_ENCAP**
1978+
* End.B6.Encap action: Endpoint bound to an SRv6
1979+
* encapsulation policy.
1980+
* Type of param: **struct ipv6_sr_hdr**.
1981+
*
1982+
* A call to this helper is susceptible to change the underlaying
1983+
* packet buffer. Therefore, at load time, all checks on pointers
1984+
* previously done by the verifier are invalidated and must be
1985+
* performed again, if the helper is used in combination with
1986+
* direct packet access.
1987+
* Return
1988+
* 0 on success, or a negative error in case of failure.
19051989
*/
19061990
#define __BPF_FUNC_MAPPER(FN) \
19071991
FN(unspec), \
@@ -1976,7 +2060,11 @@ union bpf_attr {
19762060
FN(fib_lookup), \
19772061
FN(sock_hash_update), \
19782062
FN(msg_redirect_hash), \
1979-
FN(sk_redirect_hash),
2063+
FN(sk_redirect_hash), \
2064+
FN(lwt_push_encap), \
2065+
FN(lwt_seg6_store_bytes), \
2066+
FN(lwt_seg6_adjust_srh), \
2067+
FN(lwt_seg6_action),
19802068

19812069
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
19822070
* function eBPF program intends to call
@@ -2043,6 +2131,12 @@ enum bpf_hdr_start_off {
20432131
BPF_HDR_START_NET,
20442132
};
20452133

2134+
/* Encapsulation type for BPF_FUNC_lwt_push_encap helper. */
2135+
enum bpf_lwt_encap_mode {
2136+
BPF_LWT_ENCAP_SEG6,
2137+
BPF_LWT_ENCAP_SEG6_INLINE
2138+
};
2139+
20462140
/* user accessible mirror of in-kernel sk_buff.
20472141
* new fields can only be added to the end of this structure
20482142
*/

0 commit comments

Comments
 (0)