├── bpf_bpfeb.o ├── bpf_bpfel.o ├── proxy_server ├── go.mod ├── headers ├── update.sh └── bpf │ ├── LICENSE.BSD-2-Clause │ ├── bpf_endian.h │ ├── bpf_helpers.h │ ├── bpf_tracing.h │ └── bpf_helper_defs.h ├── README.md ├── go.sum ├── main.go ├── proxy_server.c ├── bpf_bpfeb.go ├── bpf_bpfel.go └── main.go.bak /bpf_bpfeb.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinhjaxt/eBPF-sockmap-proxy/HEAD/bpf_bpfeb.o -------------------------------------------------------------------------------- /bpf_bpfel.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinhjaxt/eBPF-sockmap-proxy/HEAD/bpf_bpfel.o -------------------------------------------------------------------------------- /proxy_server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vinhjaxt/eBPF-sockmap-proxy/HEAD/proxy_server -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module proxy_server 2 | 3 | go 1.16 4 | 5 | require ( 6 | github.com/cilium/ebpf v0.8.2-0.20220302142225-d4100a1f430f 7 | golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34 8 | ) 9 | -------------------------------------------------------------------------------- /headers/update.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Version of libbpf to fetch headers from 4 | LIBBPF_VERSION=0.6.1 5 | 6 | # The headers we want 7 | prefix=libbpf-"$LIBBPF_VERSION" 8 | headers=( 9 | "$prefix"/LICENSE.BSD-2-Clause 10 | "$prefix"/src/bpf_endian.h 11 | "$prefix"/src/bpf_helper_defs.h 12 | "$prefix"/src/bpf_helpers.h 13 | "$prefix"/src/bpf_tracing.h 14 | ) 15 | 16 | # Fetch libbpf release and extract the desired headers 17 | curl -sL "https://github.com/libbpf/libbpf/archive/refs/tags/v${LIBBPF_VERSION}.tar.gz" | \ 18 | tar -xz --xform='s#.*/#bpf/#' "${headers[@]}" 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Proxy Server based on eBPF 2 | This forward any connection to Go server using eBPF sockmap 3 | 4 | # Commands 5 | 6 | ## Terminal 1 7 | ``` 8 | go get github.com/cilium/ebpf/cmd/bpf2go@master 9 | go generate && go build && sudo ./proxy_server 10 | ``` 11 | 12 | ## Terminal 2 13 | ``` 14 | sudo cat /sys/kernel/debug/tracing/trace_pipe 15 | ``` 16 | 17 | ## Terminal 3 18 | ``` 19 | nc -v 192.168.1.6 777 20 | ``` 21 | # CC 22 | * [https://github.com/jsitnicki/ebpf-summit-2020](https://github.com/jsitnicki/ebpf-summit-2020) 23 | * [https://github.com/intel/istio-tcpip-bypass/blob/main/main.go](https://github.com/intel/istio-tcpip-bypass/blob/main/main.go) 24 | # Docs 25 | [https://man7.org/linux/man-pages/man7/bpf-helpers.7.html](https://man7.org/linux/man-pages/man7/bpf-helpers.7.html) 26 | -------------------------------------------------------------------------------- /headers/bpf/LICENSE.BSD-2-Clause: -------------------------------------------------------------------------------- 1 | Valid-License-Identifier: BSD-2-Clause 2 | SPDX-URL: https://spdx.org/licenses/BSD-2-Clause.html 3 | Usage-Guide: 4 | To use the BSD 2-clause "Simplified" License put the following SPDX 5 | tag/value pair into a comment according to the placement guidelines in 6 | the licensing rules documentation: 7 | SPDX-License-Identifier: BSD-2-Clause 8 | License-Text: 9 | 10 | Copyright (c) . All rights reserved. 11 | 12 | Redistribution and use in source and binary forms, with or without 13 | modification, are permitted provided that the following conditions are met: 14 | 15 | 1. Redistributions of source code must retain the above copyright notice, 16 | this list of conditions and the following disclaimer. 17 | 18 | 2. Redistributions in binary form must reproduce the above copyright 19 | notice, this list of conditions and the following disclaimer in the 20 | documentation and/or other materials provided with the distribution. 21 | 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 23 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 26 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 | POSSIBILITY OF SUCH DAMAGE. 33 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/cilium/ebpf v0.8.2-0.20220302142225-d4100a1f430f h1:DzeZi0dxa9N76IQPBI82hZxPd3vSylJEwOnryncfkz4= 2 | github.com/cilium/ebpf v0.8.2-0.20220302142225-d4100a1f430f/go.mod h1:f5zLIM0FSNuAkSyLAN7X+Hy6yznlF1mNiWUMfxMtrgk= 3 | github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= 4 | github.com/frankban/quicktest v1.14.0 h1:+cqqvzZV87b4adx/5ayVOaYZ2CrvM4ejQvUdBzPPUss= 5 | github.com/frankban/quicktest v1.14.0/go.mod h1:NeW+ay9A/U67EYXNFA1nPE8e/tnQv/09mUdL/ijj8og= 6 | github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= 7 | github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 8 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 9 | github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= 10 | github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= 11 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 12 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 13 | github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= 14 | github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= 15 | github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k= 16 | github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= 17 | golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34 h1:GkvMjFtXUmahfDtashnc1mnrCtuBVcwse5QV2lUk/tI= 18 | golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 19 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= 20 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 21 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 22 | gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= 23 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | //go:build linux 2 | // +build linux 3 | // go get github.com/cilium/ebpf/cmd/bpf2go@master 4 | //go:generate bpf2go -cc clang-11 -cflags "-g -O2 -Wall -Wextra" bpf proxy_server.c -- -I./headers 5 | 6 | package main 7 | 8 | import ( 9 | "io" 10 | "log" 11 | "net" 12 | "os" 13 | "os/signal" 14 | "syscall" 15 | 16 | "github.com/cilium/ebpf/link" 17 | "github.com/cilium/ebpf/rlimit" 18 | ) 19 | 20 | // sudo cat /sys/kernel/debug/tracing/trace_pipe 21 | func main() { 22 | stopper := make(chan os.Signal, 1) 23 | signal.Notify(stopper, os.Interrupt, syscall.SIGTERM) 24 | 25 | // Allow the current process to lock memory for eBPF resources. 26 | if err := rlimit.RemoveMemlock(); err != nil { 27 | log.Panicln(err) 28 | } 29 | 30 | // Load pre-compiled programs and maps into the kernel. 31 | objs := bpfObjects{} 32 | err := loadBpfObjects(&objs, nil) 33 | if err != nil { 34 | log.Panicln(err) 35 | } 36 | defer objs.Close() 37 | 38 | // Attach program 39 | netns, err := os.Open("/proc/self/ns/net") 40 | if err != nil { 41 | log.Panicln(err) 42 | } 43 | defer netns.Close() 44 | 45 | lnk, err := link.AttachNetNs(int(netns.Fd()), objs.bpfPrograms.ProxyDispatch) 46 | defer lnk.Close() 47 | if err != nil { 48 | log.Panicln(err) 49 | } 50 | 51 | // Create server 52 | ln, err := net.Listen("tcp", "127.0.0.1:12295") 53 | if err != nil { 54 | log.Panicln(err) 55 | } 56 | 57 | go func() { 58 | <-stopper 59 | log.Println("Stoping..") 60 | os.Exit(0) 61 | }() 62 | 63 | // Put server fd into map 64 | lnTCP := ln.(*net.TCPListener) 65 | f, err := lnTCP.File() 66 | if err != nil { 67 | log.Panicln(err) 68 | } 69 | 70 | err = objs.bpfMaps.ServerSocket.Put(uint32(0), uint64(f.Fd())) 71 | if err != nil { 72 | log.Panicln(err) 73 | } 74 | 75 | // Put ports 76 | err = objs.bpfMaps.EchoPorts.Put(uint16(0x0309), uint8(0)) 77 | if err != nil { 78 | log.Panicln(err) 79 | } 80 | 81 | log.Println("Listening..") 82 | for { 83 | conn, err := ln.Accept() 84 | if err != nil { 85 | log.Println(err) 86 | continue 87 | } 88 | log.Println("New connection", conn.RemoteAddr()) 89 | go handleEcho(conn) 90 | } 91 | 92 | } 93 | 94 | // Handle 95 | func handleEcho(conn net.Conn) { 96 | buf := make([]byte, 1024) 97 | for { 98 | n, err := conn.Read(buf) 99 | if err != nil { 100 | if err != io.EOF { 101 | log.Println(err) 102 | } 103 | return 104 | } 105 | log.Println(string(buf[:n])) 106 | conn.Write(buf[:n]) 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /proxy_server.c: -------------------------------------------------------------------------------- 1 | // +build ignore 2 | 3 | // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 4 | /* CC: 2020 Cloudflare */ 5 | 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | /* Declare BPF maps */ 13 | 14 | /* List of open echo service ports. Key is the port number. */ 15 | struct bpf_map_def SEC("maps") echo_ports = { 16 | .type = BPF_MAP_TYPE_HASH, 17 | .max_entries = 1024, 18 | .key_size = sizeof(__u16), 19 | .value_size = sizeof(__u8), 20 | }; 21 | 22 | /* Server socket */ 23 | struct bpf_map_def SEC("maps") server_socket = { 24 | .type = BPF_MAP_TYPE_SOCKMAP, 25 | .max_entries = 1, 26 | .key_size = sizeof(__u32), 27 | .value_size = sizeof(__u64), 28 | }; 29 | 30 | /* Dispatcher program for the proxy service */ 31 | SEC("sk_lookup/proxy_dispatch") 32 | int proxy_dispatch(struct bpf_sk_lookup *ctx) 33 | { 34 | const __u32 zero = 0; 35 | struct bpf_sock *sk; 36 | __u16 port; 37 | __u8 *open; 38 | long err; 39 | 40 | /* Is echo service enabled on packets destination port? */ 41 | port = ctx->local_port; 42 | open = bpf_map_lookup_elem(&echo_ports, &port); 43 | if (!open) 44 | return SK_PASS; 45 | 46 | /* Get echo server socket */ 47 | sk = bpf_map_lookup_elem(&server_socket, &zero); 48 | if (!sk) 49 | return SK_DROP; 50 | 51 | /* Dispatch the packet to echo server socket */ 52 | err = bpf_sk_assign(ctx, sk, 0); 53 | bpf_sk_release(sk); 54 | 55 | // https://man7.org/linux/man-pages/man7/bpf-helpers.7.html 56 | if (err == -EINVAL) 57 | { 58 | bpf_printk("bpf_sk_assign: -EINVAL: specified flags are not supported.", err); 59 | } 60 | else if (err == -ENOENT) 61 | { 62 | bpf_printk("bpf_sk_assign: -ENOENT: the socket is unavailable for assignment.", err); 63 | } 64 | else if (err == -ENETUNREACH) 65 | { 66 | bpf_printk("bpf_sk_assign: -ENETUNREACH: the socket is unreachable (wrong netns).", err); 67 | } 68 | else if (err == -EOPNOTSUPP) 69 | { 70 | bpf_printk("bpf_sk_assign: -EOPNOTSUPP: the operation is not supported, for example a call from outside of TC ingress.", err); 71 | } 72 | else if (err == -ESOCKTNOSUPPORT) 73 | { 74 | bpf_printk("bpf_sk_assign: -ESOCKTNOSUPPORT if the socket type is not supported (reuseport).", err); 75 | } 76 | 77 | // add to key 78 | 79 | return err ? SK_DROP : SK_PASS; 80 | } 81 | 82 | char LICENSE[] SEC("license") = "Dual MIT/GPL"; 83 | // This number will be interpreted by elf-loader to set the current running kernel version 84 | __u32 _version SEC("version") = 0xFFFFFFFE; // NOLINT(bugprone-reserved-identifier) 85 | -------------------------------------------------------------------------------- /bpf_bpfeb.go: -------------------------------------------------------------------------------- 1 | // Code generated by bpf2go; DO NOT EDIT. 2 | // +build arm64be armbe mips mips64 mips64p32 ppc64 s390 s390x sparc sparc64 3 | 4 | package main 5 | 6 | import ( 7 | "bytes" 8 | _ "embed" 9 | "fmt" 10 | "io" 11 | 12 | "github.com/cilium/ebpf" 13 | ) 14 | 15 | // loadBpf returns the embedded CollectionSpec for bpf. 16 | func loadBpf() (*ebpf.CollectionSpec, error) { 17 | reader := bytes.NewReader(_BpfBytes) 18 | spec, err := ebpf.LoadCollectionSpecFromReader(reader) 19 | if err != nil { 20 | return nil, fmt.Errorf("can't load bpf: %w", err) 21 | } 22 | 23 | return spec, err 24 | } 25 | 26 | // loadBpfObjects loads bpf and converts it into a struct. 27 | // 28 | // The following types are suitable as obj argument: 29 | // 30 | // *bpfObjects 31 | // *bpfPrograms 32 | // *bpfMaps 33 | // 34 | // See ebpf.CollectionSpec.LoadAndAssign documentation for details. 35 | func loadBpfObjects(obj interface{}, opts *ebpf.CollectionOptions) error { 36 | spec, err := loadBpf() 37 | if err != nil { 38 | return err 39 | } 40 | 41 | return spec.LoadAndAssign(obj, opts) 42 | } 43 | 44 | // bpfSpecs contains maps and programs before they are loaded into the kernel. 45 | // 46 | // It can be passed ebpf.CollectionSpec.Assign. 47 | type bpfSpecs struct { 48 | bpfProgramSpecs 49 | bpfMapSpecs 50 | } 51 | 52 | // bpfSpecs contains programs before they are loaded into the kernel. 53 | // 54 | // It can be passed ebpf.CollectionSpec.Assign. 55 | type bpfProgramSpecs struct { 56 | ProxyDispatch *ebpf.ProgramSpec `ebpf:"proxy_dispatch"` 57 | } 58 | 59 | // bpfMapSpecs contains maps before they are loaded into the kernel. 60 | // 61 | // It can be passed ebpf.CollectionSpec.Assign. 62 | type bpfMapSpecs struct { 63 | EchoPorts *ebpf.MapSpec `ebpf:"echo_ports"` 64 | ServerSocket *ebpf.MapSpec `ebpf:"server_socket"` 65 | } 66 | 67 | // bpfObjects contains all objects after they have been loaded into the kernel. 68 | // 69 | // It can be passed to loadBpfObjects or ebpf.CollectionSpec.LoadAndAssign. 70 | type bpfObjects struct { 71 | bpfPrograms 72 | bpfMaps 73 | } 74 | 75 | func (o *bpfObjects) Close() error { 76 | return _BpfClose( 77 | &o.bpfPrograms, 78 | &o.bpfMaps, 79 | ) 80 | } 81 | 82 | // bpfMaps contains all maps after they have been loaded into the kernel. 83 | // 84 | // It can be passed to loadBpfObjects or ebpf.CollectionSpec.LoadAndAssign. 85 | type bpfMaps struct { 86 | EchoPorts *ebpf.Map `ebpf:"echo_ports"` 87 | ServerSocket *ebpf.Map `ebpf:"server_socket"` 88 | } 89 | 90 | func (m *bpfMaps) Close() error { 91 | return _BpfClose( 92 | m.EchoPorts, 93 | m.ServerSocket, 94 | ) 95 | } 96 | 97 | // bpfPrograms contains all programs after they have been loaded into the kernel. 98 | // 99 | // It can be passed to loadBpfObjects or ebpf.CollectionSpec.LoadAndAssign. 100 | type bpfPrograms struct { 101 | ProxyDispatch *ebpf.Program `ebpf:"proxy_dispatch"` 102 | } 103 | 104 | func (p *bpfPrograms) Close() error { 105 | return _BpfClose( 106 | p.ProxyDispatch, 107 | ) 108 | } 109 | 110 | func _BpfClose(closers ...io.Closer) error { 111 | for _, closer := range closers { 112 | if err := closer.Close(); err != nil { 113 | return err 114 | } 115 | } 116 | return nil 117 | } 118 | 119 | // Do not access this directly. 120 | //go:embed bpf_bpfeb.o 121 | var _BpfBytes []byte 122 | -------------------------------------------------------------------------------- /bpf_bpfel.go: -------------------------------------------------------------------------------- 1 | // Code generated by bpf2go; DO NOT EDIT. 2 | // +build 386 amd64 amd64p32 arm arm64 mips64le mips64p32le mipsle ppc64le riscv64 3 | 4 | package main 5 | 6 | import ( 7 | "bytes" 8 | _ "embed" 9 | "fmt" 10 | "io" 11 | 12 | "github.com/cilium/ebpf" 13 | ) 14 | 15 | // loadBpf returns the embedded CollectionSpec for bpf. 16 | func loadBpf() (*ebpf.CollectionSpec, error) { 17 | reader := bytes.NewReader(_BpfBytes) 18 | spec, err := ebpf.LoadCollectionSpecFromReader(reader) 19 | if err != nil { 20 | return nil, fmt.Errorf("can't load bpf: %w", err) 21 | } 22 | 23 | return spec, err 24 | } 25 | 26 | // loadBpfObjects loads bpf and converts it into a struct. 27 | // 28 | // The following types are suitable as obj argument: 29 | // 30 | // *bpfObjects 31 | // *bpfPrograms 32 | // *bpfMaps 33 | // 34 | // See ebpf.CollectionSpec.LoadAndAssign documentation for details. 35 | func loadBpfObjects(obj interface{}, opts *ebpf.CollectionOptions) error { 36 | spec, err := loadBpf() 37 | if err != nil { 38 | return err 39 | } 40 | 41 | return spec.LoadAndAssign(obj, opts) 42 | } 43 | 44 | // bpfSpecs contains maps and programs before they are loaded into the kernel. 45 | // 46 | // It can be passed ebpf.CollectionSpec.Assign. 47 | type bpfSpecs struct { 48 | bpfProgramSpecs 49 | bpfMapSpecs 50 | } 51 | 52 | // bpfSpecs contains programs before they are loaded into the kernel. 53 | // 54 | // It can be passed ebpf.CollectionSpec.Assign. 55 | type bpfProgramSpecs struct { 56 | ProxyDispatch *ebpf.ProgramSpec `ebpf:"proxy_dispatch"` 57 | } 58 | 59 | // bpfMapSpecs contains maps before they are loaded into the kernel. 60 | // 61 | // It can be passed ebpf.CollectionSpec.Assign. 62 | type bpfMapSpecs struct { 63 | EchoPorts *ebpf.MapSpec `ebpf:"echo_ports"` 64 | ServerSocket *ebpf.MapSpec `ebpf:"server_socket"` 65 | } 66 | 67 | // bpfObjects contains all objects after they have been loaded into the kernel. 68 | // 69 | // It can be passed to loadBpfObjects or ebpf.CollectionSpec.LoadAndAssign. 70 | type bpfObjects struct { 71 | bpfPrograms 72 | bpfMaps 73 | } 74 | 75 | func (o *bpfObjects) Close() error { 76 | return _BpfClose( 77 | &o.bpfPrograms, 78 | &o.bpfMaps, 79 | ) 80 | } 81 | 82 | // bpfMaps contains all maps after they have been loaded into the kernel. 83 | // 84 | // It can be passed to loadBpfObjects or ebpf.CollectionSpec.LoadAndAssign. 85 | type bpfMaps struct { 86 | EchoPorts *ebpf.Map `ebpf:"echo_ports"` 87 | ServerSocket *ebpf.Map `ebpf:"server_socket"` 88 | } 89 | 90 | func (m *bpfMaps) Close() error { 91 | return _BpfClose( 92 | m.EchoPorts, 93 | m.ServerSocket, 94 | ) 95 | } 96 | 97 | // bpfPrograms contains all programs after they have been loaded into the kernel. 98 | // 99 | // It can be passed to loadBpfObjects or ebpf.CollectionSpec.LoadAndAssign. 100 | type bpfPrograms struct { 101 | ProxyDispatch *ebpf.Program `ebpf:"proxy_dispatch"` 102 | } 103 | 104 | func (p *bpfPrograms) Close() error { 105 | return _BpfClose( 106 | p.ProxyDispatch, 107 | ) 108 | } 109 | 110 | func _BpfClose(closers ...io.Closer) error { 111 | for _, closer := range closers { 112 | if err := closer.Close(); err != nil { 113 | return err 114 | } 115 | } 116 | return nil 117 | } 118 | 119 | // Do not access this directly. 120 | //go:embed bpf_bpfel.o 121 | var _BpfBytes []byte 122 | -------------------------------------------------------------------------------- /headers/bpf/bpf_endian.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ 2 | #ifndef __BPF_ENDIAN__ 3 | #define __BPF_ENDIAN__ 4 | 5 | /* 6 | * Isolate byte #n and put it into byte #m, for __u##b type. 7 | * E.g., moving byte #6 (nnnnnnnn) into byte #1 (mmmmmmmm) for __u64: 8 | * 1) xxxxxxxx nnnnnnnn xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx mmmmmmmm xxxxxxxx 9 | * 2) nnnnnnnn xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx mmmmmmmm xxxxxxxx 00000000 10 | * 3) 00000000 00000000 00000000 00000000 00000000 00000000 00000000 nnnnnnnn 11 | * 4) 00000000 00000000 00000000 00000000 00000000 00000000 nnnnnnnn 00000000 12 | */ 13 | #define ___bpf_mvb(x, b, n, m) ((__u##b)(x) << (b-(n+1)*8) >> (b-8) << (m*8)) 14 | 15 | #define ___bpf_swab16(x) ((__u16)( \ 16 | ___bpf_mvb(x, 16, 0, 1) | \ 17 | ___bpf_mvb(x, 16, 1, 0))) 18 | 19 | #define ___bpf_swab32(x) ((__u32)( \ 20 | ___bpf_mvb(x, 32, 0, 3) | \ 21 | ___bpf_mvb(x, 32, 1, 2) | \ 22 | ___bpf_mvb(x, 32, 2, 1) | \ 23 | ___bpf_mvb(x, 32, 3, 0))) 24 | 25 | #define ___bpf_swab64(x) ((__u64)( \ 26 | ___bpf_mvb(x, 64, 0, 7) | \ 27 | ___bpf_mvb(x, 64, 1, 6) | \ 28 | ___bpf_mvb(x, 64, 2, 5) | \ 29 | ___bpf_mvb(x, 64, 3, 4) | \ 30 | ___bpf_mvb(x, 64, 4, 3) | \ 31 | ___bpf_mvb(x, 64, 5, 2) | \ 32 | ___bpf_mvb(x, 64, 6, 1) | \ 33 | ___bpf_mvb(x, 64, 7, 0))) 34 | 35 | /* LLVM's BPF target selects the endianness of the CPU 36 | * it compiles on, or the user specifies (bpfel/bpfeb), 37 | * respectively. The used __BYTE_ORDER__ is defined by 38 | * the compiler, we cannot rely on __BYTE_ORDER from 39 | * libc headers, since it doesn't reflect the actual 40 | * requested byte order. 41 | * 42 | * Note, LLVM's BPF target has different __builtin_bswapX() 43 | * semantics. It does map to BPF_ALU | BPF_END | BPF_TO_BE 44 | * in bpfel and bpfeb case, which means below, that we map 45 | * to cpu_to_be16(). We could use it unconditionally in BPF 46 | * case, but better not rely on it, so that this header here 47 | * can be used from application and BPF program side, which 48 | * use different targets. 49 | */ 50 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 51 | # define __bpf_ntohs(x) __builtin_bswap16(x) 52 | # define __bpf_htons(x) __builtin_bswap16(x) 53 | # define __bpf_constant_ntohs(x) ___bpf_swab16(x) 54 | # define __bpf_constant_htons(x) ___bpf_swab16(x) 55 | # define __bpf_ntohl(x) __builtin_bswap32(x) 56 | # define __bpf_htonl(x) __builtin_bswap32(x) 57 | # define __bpf_constant_ntohl(x) ___bpf_swab32(x) 58 | # define __bpf_constant_htonl(x) ___bpf_swab32(x) 59 | # define __bpf_be64_to_cpu(x) __builtin_bswap64(x) 60 | # define __bpf_cpu_to_be64(x) __builtin_bswap64(x) 61 | # define __bpf_constant_be64_to_cpu(x) ___bpf_swab64(x) 62 | # define __bpf_constant_cpu_to_be64(x) ___bpf_swab64(x) 63 | #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 64 | # define __bpf_ntohs(x) (x) 65 | # define __bpf_htons(x) (x) 66 | # define __bpf_constant_ntohs(x) (x) 67 | # define __bpf_constant_htons(x) (x) 68 | # define __bpf_ntohl(x) (x) 69 | # define __bpf_htonl(x) (x) 70 | # define __bpf_constant_ntohl(x) (x) 71 | # define __bpf_constant_htonl(x) (x) 72 | # define __bpf_be64_to_cpu(x) (x) 73 | # define __bpf_cpu_to_be64(x) (x) 74 | # define __bpf_constant_be64_to_cpu(x) (x) 75 | # define __bpf_constant_cpu_to_be64(x) (x) 76 | #else 77 | # error "Fix your compiler's __BYTE_ORDER__?!" 78 | #endif 79 | 80 | #define bpf_htons(x) \ 81 | (__builtin_constant_p(x) ? \ 82 | __bpf_constant_htons(x) : __bpf_htons(x)) 83 | #define bpf_ntohs(x) \ 84 | (__builtin_constant_p(x) ? \ 85 | __bpf_constant_ntohs(x) : __bpf_ntohs(x)) 86 | #define bpf_htonl(x) \ 87 | (__builtin_constant_p(x) ? \ 88 | __bpf_constant_htonl(x) : __bpf_htonl(x)) 89 | #define bpf_ntohl(x) \ 90 | (__builtin_constant_p(x) ? \ 91 | __bpf_constant_ntohl(x) : __bpf_ntohl(x)) 92 | #define bpf_cpu_to_be64(x) \ 93 | (__builtin_constant_p(x) ? \ 94 | __bpf_constant_cpu_to_be64(x) : __bpf_cpu_to_be64(x)) 95 | #define bpf_be64_to_cpu(x) \ 96 | (__builtin_constant_p(x) ? \ 97 | __bpf_constant_be64_to_cpu(x) : __bpf_be64_to_cpu(x)) 98 | 99 | #endif /* __BPF_ENDIAN__ */ 100 | -------------------------------------------------------------------------------- /main.go.bak: -------------------------------------------------------------------------------- 1 | //go:build linux 2 | // +build linux 3 | // go get github.com/cilium/ebpf/cmd/bpf2go@master 4 | //go:generate bpf2go -cc clang-11 -cflags "-g -O2 -Wall -Wextra" bpf fentry.c ./linux-5.9.1/tools/lib/bpf/libbpf.a -- -I./linux-5.9.1/usr/include -I./linux-5.9.1/tools/lib -I./headers 5 | 6 | package main 7 | 8 | import ( 9 | "log" 10 | "net" 11 | "os" 12 | "os/signal" 13 | "syscall" 14 | 15 | "github.com/cilium/ebpf/link" 16 | "github.com/cilium/ebpf/rlimit" 17 | "golang.org/x/sys/unix" 18 | ) 19 | 20 | const ( 21 | MapsPinpath = "/sys/fs/bpf/echo_dispatch_prog" 22 | ) 23 | const ServerAddr = "/tmp/echo_server" 24 | 25 | // sudo cat /sys/kernel/debug/tracing/trace_pipe 26 | func main() { 27 | stopper := make(chan os.Signal, 1) 28 | signal.Notify(stopper, os.Interrupt, syscall.SIGTERM) 29 | 30 | // Allow the current process to lock memory for eBPF resources. 31 | if err := rlimit.RemoveMemlock(); err != nil { 32 | log.Panicln(err) 33 | } 34 | 35 | /* 36 | // Create sockmap 37 | var sockMap *ebpf.Map 38 | sockMap, err := ebpf.NewMap(&ebpf.MapSpec{ 39 | Type: ebpf.MapType(15), // BPF_MAP_TYPE_SOCKMAP 40 | KeySize: 4, 41 | ValueSize: 4, 42 | MaxEntries: 2, 43 | }) 44 | if err != nil { 45 | log.Panicln(err) 46 | } 47 | defer sockMap.Close() 48 | // */ 49 | 50 | // var options ebpf.CollectionOptions 51 | 52 | // os.Mkdir(MapsPinpath, os.ModePerm) 53 | 54 | // options.Maps.PinPath = MapsPinpath 55 | // objs.bpfMaps.EchoSocket.FD() 56 | 57 | // Load pre-compiled programs and maps into the kernel. 58 | objs := bpfObjects{} 59 | // err := loadBpfObjects(&objs, &options) 60 | err := loadBpfObjects(&objs, nil) 61 | if err != nil { 62 | log.Panicln(err) 63 | } 64 | defer objs.Close() 65 | 66 | // Attach program 67 | netns, err := os.Open("/proc/self/ns/net") 68 | if err != nil { 69 | log.Panicln(err) 70 | } 71 | defer netns.Close() 72 | 73 | lnk, err := link.AttachNetNs(int(netns.Fd()), objs.bpfPrograms.EchoDispatch) 74 | defer lnk.Close() 75 | 76 | // err = link.RawAttachProgram(link.RawAttachProgramOptions{ 77 | // Target: int(netns.Fd()), 78 | // Program: objs.bpfPrograms.EchoDispatch, 79 | // Attach: ebpf.AttachSkLookup, 80 | // Flags: 0, 81 | // }) 82 | if err != nil { 83 | log.Panicln(err) 84 | } 85 | 86 | // Create server 87 | 88 | os.Remove(ServerAddr) 89 | 90 | ln, err := net.Listen("tcp", "127.0.0.1:8081") 91 | if err != nil { 92 | log.Panicln(err) 93 | } 94 | 95 | go func() { 96 | <-stopper 97 | log.Println("Stoping..") 98 | os.Exit(0) 99 | }() 100 | 101 | lnUnix := ln.(*net.TCPListener) 102 | f, err := lnUnix.File() 103 | if err != nil { 104 | log.Panicln(err) 105 | } 106 | 107 | log.Println("Fd:", f.Fd()) 108 | 109 | log.Println(unix.SetsockoptInt(int(f.Fd()), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)) 110 | 111 | err = objs.bpfMaps.EchoSocket.Put(uint32(0), uint64(f.Fd())) 112 | if err != nil { 113 | log.Panicln(err) 114 | } 115 | log.Println("Put done") 116 | 117 | mapIter := objs.bpfMaps.EchoSocket.Iterate() 118 | var keyOut1 uint32 119 | var valOut1 uint64 120 | log.Println(mapIter.Next(&keyOut1, &valOut1)) 121 | log.Println(keyOut1, valOut1) 122 | 123 | err = objs.bpfMaps.EchoPorts.Put(uint16(0x0309), uint8(0)) 124 | if err != nil { 125 | log.Panicln(err) 126 | } 127 | log.Println("Put done") 128 | 129 | mapIter = objs.bpfMaps.EchoPorts.Iterate() 130 | var keyOut uint16 131 | var valOut uint8 132 | log.Println(mapIter.Next(&keyOut, &valOut)) 133 | log.Println(keyOut, valOut) 134 | 135 | // sysConn, err := lnUnix.SyscallConn() 136 | // if err != nil { 137 | // log.Panicln(err) 138 | // } 139 | 140 | // log.Println(sysConn) 141 | 142 | log.Println("Accepting..") 143 | for { 144 | conn, err := ln.Accept() 145 | if err != nil { 146 | log.Println(err) 147 | continue 148 | } 149 | log.Println("New conn") 150 | go handleEcho(conn) 151 | } 152 | 153 | } 154 | 155 | func handleEcho(conn net.Conn) { 156 | buf := make([]byte, 1024) 157 | for { 158 | n, err := conn.Read(buf) 159 | if err != nil { 160 | log.Println(err) 161 | return 162 | } 163 | log.Println(string(buf[:n])) 164 | conn.Write(buf[:n]) 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /headers/bpf/bpf_helpers.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ 2 | #ifndef __BPF_HELPERS__ 3 | #define __BPF_HELPERS__ 4 | 5 | /* 6 | * Note that bpf programs need to include either 7 | * vmlinux.h (auto-generated from BTF) or linux/types.h 8 | * in advance since bpf_helper_defs.h uses such types 9 | * as __u64. 10 | */ 11 | #include "bpf_helper_defs.h" 12 | 13 | #define __uint(name, val) int (*name)[val] 14 | #define __type(name, val) typeof(val) *name 15 | #define __array(name, val) typeof(val) *name[] 16 | 17 | /* 18 | * Helper macro to place programs, maps, license in 19 | * different sections in elf_bpf file. Section names 20 | * are interpreted by libbpf depending on the context (BPF programs, BPF maps, 21 | * extern variables, etc). 22 | * To allow use of SEC() with externs (e.g., for extern .maps declarations), 23 | * make sure __attribute__((unused)) doesn't trigger compilation warning. 24 | */ 25 | #define SEC(name) \ 26 | _Pragma("GCC diagnostic push") \ 27 | _Pragma("GCC diagnostic ignored \"-Wignored-attributes\"") \ 28 | __attribute__((section(name), used)) \ 29 | _Pragma("GCC diagnostic pop") \ 30 | 31 | /* Avoid 'linux/stddef.h' definition of '__always_inline'. */ 32 | #undef __always_inline 33 | #define __always_inline inline __attribute__((always_inline)) 34 | 35 | #ifndef __noinline 36 | #define __noinline __attribute__((noinline)) 37 | #endif 38 | #ifndef __weak 39 | #define __weak __attribute__((weak)) 40 | #endif 41 | 42 | /* 43 | * Use __hidden attribute to mark a non-static BPF subprogram effectively 44 | * static for BPF verifier's verification algorithm purposes, allowing more 45 | * extensive and permissive BPF verification process, taking into account 46 | * subprogram's caller context. 47 | */ 48 | #define __hidden __attribute__((visibility("hidden"))) 49 | 50 | /* When utilizing vmlinux.h with BPF CO-RE, user BPF programs can't include 51 | * any system-level headers (such as stddef.h, linux/version.h, etc), and 52 | * commonly-used macros like NULL and KERNEL_VERSION aren't available through 53 | * vmlinux.h. This just adds unnecessary hurdles and forces users to re-define 54 | * them on their own. So as a convenience, provide such definitions here. 55 | */ 56 | #ifndef NULL 57 | #define NULL ((void *)0) 58 | #endif 59 | 60 | #ifndef KERNEL_VERSION 61 | #define KERNEL_VERSION(a, b, c) (((a) << 16) + ((b) << 8) + ((c) > 255 ? 255 : (c))) 62 | #endif 63 | 64 | /* 65 | * Helper macros to manipulate data structures 66 | */ 67 | #ifndef offsetof 68 | #define offsetof(TYPE, MEMBER) ((unsigned long)&((TYPE *)0)->MEMBER) 69 | #endif 70 | #ifndef container_of 71 | #define container_of(ptr, type, member) \ 72 | ({ \ 73 | void *__mptr = (void *)(ptr); \ 74 | ((type *)(__mptr - offsetof(type, member))); \ 75 | }) 76 | #endif 77 | 78 | /* 79 | * Helper macro to throw a compilation error if __bpf_unreachable() gets 80 | * built into the resulting code. This works given BPF back end does not 81 | * implement __builtin_trap(). This is useful to assert that certain paths 82 | * of the program code are never used and hence eliminated by the compiler. 83 | * 84 | * For example, consider a switch statement that covers known cases used by 85 | * the program. __bpf_unreachable() can then reside in the default case. If 86 | * the program gets extended such that a case is not covered in the switch 87 | * statement, then it will throw a build error due to the default case not 88 | * being compiled out. 89 | */ 90 | #ifndef __bpf_unreachable 91 | # define __bpf_unreachable() __builtin_trap() 92 | #endif 93 | 94 | /* 95 | * Helper function to perform a tail call with a constant/immediate map slot. 96 | */ 97 | #if __clang_major__ >= 8 && defined(__bpf__) 98 | static __always_inline void 99 | bpf_tail_call_static(void *ctx, const void *map, const __u32 slot) 100 | { 101 | if (!__builtin_constant_p(slot)) 102 | __bpf_unreachable(); 103 | 104 | /* 105 | * Provide a hard guarantee that LLVM won't optimize setting r2 (map 106 | * pointer) and r3 (constant map index) from _different paths_ ending 107 | * up at the _same_ call insn as otherwise we won't be able to use the 108 | * jmpq/nopl retpoline-free patching by the x86-64 JIT in the kernel 109 | * given they mismatch. See also d2e4c1e6c294 ("bpf: Constant map key 110 | * tracking for prog array pokes") for details on verifier tracking. 111 | * 112 | * Note on clobber list: we need to stay in-line with BPF calling 113 | * convention, so even if we don't end up using r0, r4, r5, we need 114 | * to mark them as clobber so that LLVM doesn't end up using them 115 | * before / after the call. 116 | */ 117 | asm volatile("r1 = %[ctx]\n\t" 118 | "r2 = %[map]\n\t" 119 | "r3 = %[slot]\n\t" 120 | "call 12" 121 | :: [ctx]"r"(ctx), [map]"r"(map), [slot]"i"(slot) 122 | : "r0", "r1", "r2", "r3", "r4", "r5"); 123 | } 124 | #endif 125 | 126 | /* 127 | * Helper structure used by eBPF C program 128 | * to describe BPF map attributes to libbpf loader 129 | */ 130 | struct bpf_map_def { 131 | unsigned int type; 132 | unsigned int key_size; 133 | unsigned int value_size; 134 | unsigned int max_entries; 135 | unsigned int map_flags; 136 | }; 137 | 138 | enum libbpf_pin_type { 139 | LIBBPF_PIN_NONE, 140 | /* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */ 141 | LIBBPF_PIN_BY_NAME, 142 | }; 143 | 144 | enum libbpf_tristate { 145 | TRI_NO = 0, 146 | TRI_YES = 1, 147 | TRI_MODULE = 2, 148 | }; 149 | 150 | #define __kconfig __attribute__((section(".kconfig"))) 151 | #define __ksym __attribute__((section(".ksyms"))) 152 | 153 | #ifndef ___bpf_concat 154 | #define ___bpf_concat(a, b) a ## b 155 | #endif 156 | #ifndef ___bpf_apply 157 | #define ___bpf_apply(fn, n) ___bpf_concat(fn, n) 158 | #endif 159 | #ifndef ___bpf_nth 160 | #define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N 161 | #endif 162 | #ifndef ___bpf_narg 163 | #define ___bpf_narg(...) \ 164 | ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) 165 | #endif 166 | 167 | #define ___bpf_fill0(arr, p, x) do {} while (0) 168 | #define ___bpf_fill1(arr, p, x) arr[p] = x 169 | #define ___bpf_fill2(arr, p, x, args...) arr[p] = x; ___bpf_fill1(arr, p + 1, args) 170 | #define ___bpf_fill3(arr, p, x, args...) arr[p] = x; ___bpf_fill2(arr, p + 1, args) 171 | #define ___bpf_fill4(arr, p, x, args...) arr[p] = x; ___bpf_fill3(arr, p + 1, args) 172 | #define ___bpf_fill5(arr, p, x, args...) arr[p] = x; ___bpf_fill4(arr, p + 1, args) 173 | #define ___bpf_fill6(arr, p, x, args...) arr[p] = x; ___bpf_fill5(arr, p + 1, args) 174 | #define ___bpf_fill7(arr, p, x, args...) arr[p] = x; ___bpf_fill6(arr, p + 1, args) 175 | #define ___bpf_fill8(arr, p, x, args...) arr[p] = x; ___bpf_fill7(arr, p + 1, args) 176 | #define ___bpf_fill9(arr, p, x, args...) arr[p] = x; ___bpf_fill8(arr, p + 1, args) 177 | #define ___bpf_fill10(arr, p, x, args...) arr[p] = x; ___bpf_fill9(arr, p + 1, args) 178 | #define ___bpf_fill11(arr, p, x, args...) arr[p] = x; ___bpf_fill10(arr, p + 1, args) 179 | #define ___bpf_fill12(arr, p, x, args...) arr[p] = x; ___bpf_fill11(arr, p + 1, args) 180 | #define ___bpf_fill(arr, args...) \ 181 | ___bpf_apply(___bpf_fill, ___bpf_narg(args))(arr, 0, args) 182 | 183 | /* 184 | * BPF_SEQ_PRINTF to wrap bpf_seq_printf to-be-printed values 185 | * in a structure. 186 | */ 187 | #define BPF_SEQ_PRINTF(seq, fmt, args...) \ 188 | ({ \ 189 | static const char ___fmt[] = fmt; \ 190 | unsigned long long ___param[___bpf_narg(args)]; \ 191 | \ 192 | _Pragma("GCC diagnostic push") \ 193 | _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 194 | ___bpf_fill(___param, args); \ 195 | _Pragma("GCC diagnostic pop") \ 196 | \ 197 | bpf_seq_printf(seq, ___fmt, sizeof(___fmt), \ 198 | ___param, sizeof(___param)); \ 199 | }) 200 | 201 | /* 202 | * BPF_SNPRINTF wraps the bpf_snprintf helper with variadic arguments instead of 203 | * an array of u64. 204 | */ 205 | #define BPF_SNPRINTF(out, out_size, fmt, args...) \ 206 | ({ \ 207 | static const char ___fmt[] = fmt; \ 208 | unsigned long long ___param[___bpf_narg(args)]; \ 209 | \ 210 | _Pragma("GCC diagnostic push") \ 211 | _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 212 | ___bpf_fill(___param, args); \ 213 | _Pragma("GCC diagnostic pop") \ 214 | \ 215 | bpf_snprintf(out, out_size, ___fmt, \ 216 | ___param, sizeof(___param)); \ 217 | }) 218 | 219 | #ifdef BPF_NO_GLOBAL_DATA 220 | #define BPF_PRINTK_FMT_MOD 221 | #else 222 | #define BPF_PRINTK_FMT_MOD static const 223 | #endif 224 | 225 | #define __bpf_printk(fmt, ...) \ 226 | ({ \ 227 | BPF_PRINTK_FMT_MOD char ____fmt[] = fmt; \ 228 | bpf_trace_printk(____fmt, sizeof(____fmt), \ 229 | ##__VA_ARGS__); \ 230 | }) 231 | 232 | /* 233 | * __bpf_vprintk wraps the bpf_trace_vprintk helper with variadic arguments 234 | * instead of an array of u64. 235 | */ 236 | #define __bpf_vprintk(fmt, args...) \ 237 | ({ \ 238 | static const char ___fmt[] = fmt; \ 239 | unsigned long long ___param[___bpf_narg(args)]; \ 240 | \ 241 | _Pragma("GCC diagnostic push") \ 242 | _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 243 | ___bpf_fill(___param, args); \ 244 | _Pragma("GCC diagnostic pop") \ 245 | \ 246 | bpf_trace_vprintk(___fmt, sizeof(___fmt), \ 247 | ___param, sizeof(___param)); \ 248 | }) 249 | 250 | /* Use __bpf_printk when bpf_printk call has 3 or fewer fmt args 251 | * Otherwise use __bpf_vprintk 252 | */ 253 | #define ___bpf_pick_printk(...) \ 254 | ___bpf_nth(_, ##__VA_ARGS__, __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, \ 255 | __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, __bpf_vprintk, \ 256 | __bpf_vprintk, __bpf_vprintk, __bpf_printk /*3*/, __bpf_printk /*2*/,\ 257 | __bpf_printk /*1*/, __bpf_printk /*0*/) 258 | 259 | /* Helper macro to print out debug messages */ 260 | #define bpf_printk(fmt, args...) ___bpf_pick_printk(args)(fmt, ##args) 261 | 262 | #endif 263 | -------------------------------------------------------------------------------- /headers/bpf/bpf_tracing.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ 2 | #ifndef __BPF_TRACING_H__ 3 | #define __BPF_TRACING_H__ 4 | 5 | /* Scan the ARCH passed in from ARCH env variable (see Makefile) */ 6 | #if defined(__TARGET_ARCH_x86) 7 | #define bpf_target_x86 8 | #define bpf_target_defined 9 | #elif defined(__TARGET_ARCH_s390) 10 | #define bpf_target_s390 11 | #define bpf_target_defined 12 | #elif defined(__TARGET_ARCH_arm) 13 | #define bpf_target_arm 14 | #define bpf_target_defined 15 | #elif defined(__TARGET_ARCH_arm64) 16 | #define bpf_target_arm64 17 | #define bpf_target_defined 18 | #elif defined(__TARGET_ARCH_mips) 19 | #define bpf_target_mips 20 | #define bpf_target_defined 21 | #elif defined(__TARGET_ARCH_powerpc) 22 | #define bpf_target_powerpc 23 | #define bpf_target_defined 24 | #elif defined(__TARGET_ARCH_sparc) 25 | #define bpf_target_sparc 26 | #define bpf_target_defined 27 | #elif defined(__TARGET_ARCH_riscv) 28 | #define bpf_target_riscv 29 | #define bpf_target_defined 30 | #else 31 | 32 | /* Fall back to what the compiler says */ 33 | #if defined(__x86_64__) 34 | #define bpf_target_x86 35 | #define bpf_target_defined 36 | #elif defined(__s390__) 37 | #define bpf_target_s390 38 | #define bpf_target_defined 39 | #elif defined(__arm__) 40 | #define bpf_target_arm 41 | #define bpf_target_defined 42 | #elif defined(__aarch64__) 43 | #define bpf_target_arm64 44 | #define bpf_target_defined 45 | #elif defined(__mips__) 46 | #define bpf_target_mips 47 | #define bpf_target_defined 48 | #elif defined(__powerpc__) 49 | #define bpf_target_powerpc 50 | #define bpf_target_defined 51 | #elif defined(__sparc__) 52 | #define bpf_target_sparc 53 | #define bpf_target_defined 54 | #elif defined(__riscv) && __riscv_xlen == 64 55 | #define bpf_target_riscv 56 | #define bpf_target_defined 57 | #endif /* no compiler target */ 58 | 59 | #endif 60 | 61 | #ifndef __BPF_TARGET_MISSING 62 | #define __BPF_TARGET_MISSING "GCC error \"Must specify a BPF target arch via __TARGET_ARCH_xxx\"" 63 | #endif 64 | 65 | #if defined(bpf_target_x86) 66 | 67 | #if defined(__KERNEL__) || defined(__VMLINUX_H__) 68 | 69 | #define PT_REGS_PARM1(x) ((x)->di) 70 | #define PT_REGS_PARM2(x) ((x)->si) 71 | #define PT_REGS_PARM3(x) ((x)->dx) 72 | #define PT_REGS_PARM4(x) ((x)->cx) 73 | #define PT_REGS_PARM5(x) ((x)->r8) 74 | #define PT_REGS_RET(x) ((x)->sp) 75 | #define PT_REGS_FP(x) ((x)->bp) 76 | #define PT_REGS_RC(x) ((x)->ax) 77 | #define PT_REGS_SP(x) ((x)->sp) 78 | #define PT_REGS_IP(x) ((x)->ip) 79 | 80 | #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), di) 81 | #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), si) 82 | #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), dx) 83 | #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((x), cx) 84 | #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((x), r8) 85 | #define PT_REGS_RET_CORE(x) BPF_CORE_READ((x), sp) 86 | #define PT_REGS_FP_CORE(x) BPF_CORE_READ((x), bp) 87 | #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), ax) 88 | #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), sp) 89 | #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), ip) 90 | 91 | #else 92 | 93 | #ifdef __i386__ 94 | /* i386 kernel is built with -mregparm=3 */ 95 | #define PT_REGS_PARM1(x) ((x)->eax) 96 | #define PT_REGS_PARM2(x) ((x)->edx) 97 | #define PT_REGS_PARM3(x) ((x)->ecx) 98 | #define PT_REGS_PARM4(x) 0 99 | #define PT_REGS_PARM5(x) 0 100 | #define PT_REGS_RET(x) ((x)->esp) 101 | #define PT_REGS_FP(x) ((x)->ebp) 102 | #define PT_REGS_RC(x) ((x)->eax) 103 | #define PT_REGS_SP(x) ((x)->esp) 104 | #define PT_REGS_IP(x) ((x)->eip) 105 | 106 | #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), eax) 107 | #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), edx) 108 | #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), ecx) 109 | #define PT_REGS_PARM4_CORE(x) 0 110 | #define PT_REGS_PARM5_CORE(x) 0 111 | #define PT_REGS_RET_CORE(x) BPF_CORE_READ((x), esp) 112 | #define PT_REGS_FP_CORE(x) BPF_CORE_READ((x), ebp) 113 | #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), eax) 114 | #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), esp) 115 | #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), eip) 116 | 117 | #else 118 | 119 | #define PT_REGS_PARM1(x) ((x)->rdi) 120 | #define PT_REGS_PARM2(x) ((x)->rsi) 121 | #define PT_REGS_PARM3(x) ((x)->rdx) 122 | #define PT_REGS_PARM4(x) ((x)->rcx) 123 | #define PT_REGS_PARM5(x) ((x)->r8) 124 | #define PT_REGS_RET(x) ((x)->rsp) 125 | #define PT_REGS_FP(x) ((x)->rbp) 126 | #define PT_REGS_RC(x) ((x)->rax) 127 | #define PT_REGS_SP(x) ((x)->rsp) 128 | #define PT_REGS_IP(x) ((x)->rip) 129 | 130 | #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), rdi) 131 | #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), rsi) 132 | #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), rdx) 133 | #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((x), rcx) 134 | #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((x), r8) 135 | #define PT_REGS_RET_CORE(x) BPF_CORE_READ((x), rsp) 136 | #define PT_REGS_FP_CORE(x) BPF_CORE_READ((x), rbp) 137 | #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), rax) 138 | #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), rsp) 139 | #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), rip) 140 | 141 | #endif 142 | #endif 143 | 144 | #elif defined(bpf_target_s390) 145 | 146 | /* s390 provides user_pt_regs instead of struct pt_regs to userspace */ 147 | struct pt_regs; 148 | #define PT_REGS_S390 const volatile user_pt_regs 149 | #define PT_REGS_PARM1(x) (((PT_REGS_S390 *)(x))->gprs[2]) 150 | #define PT_REGS_PARM2(x) (((PT_REGS_S390 *)(x))->gprs[3]) 151 | #define PT_REGS_PARM3(x) (((PT_REGS_S390 *)(x))->gprs[4]) 152 | #define PT_REGS_PARM4(x) (((PT_REGS_S390 *)(x))->gprs[5]) 153 | #define PT_REGS_PARM5(x) (((PT_REGS_S390 *)(x))->gprs[6]) 154 | #define PT_REGS_RET(x) (((PT_REGS_S390 *)(x))->gprs[14]) 155 | /* Works only with CONFIG_FRAME_POINTER */ 156 | #define PT_REGS_FP(x) (((PT_REGS_S390 *)(x))->gprs[11]) 157 | #define PT_REGS_RC(x) (((PT_REGS_S390 *)(x))->gprs[2]) 158 | #define PT_REGS_SP(x) (((PT_REGS_S390 *)(x))->gprs[15]) 159 | #define PT_REGS_IP(x) (((PT_REGS_S390 *)(x))->psw.addr) 160 | 161 | #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[2]) 162 | #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[3]) 163 | #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[4]) 164 | #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[5]) 165 | #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[6]) 166 | #define PT_REGS_RET_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[14]) 167 | #define PT_REGS_FP_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[11]) 168 | #define PT_REGS_RC_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[2]) 169 | #define PT_REGS_SP_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), gprs[15]) 170 | #define PT_REGS_IP_CORE(x) BPF_CORE_READ((PT_REGS_S390 *)(x), psw.addr) 171 | 172 | #elif defined(bpf_target_arm) 173 | 174 | #define PT_REGS_PARM1(x) ((x)->uregs[0]) 175 | #define PT_REGS_PARM2(x) ((x)->uregs[1]) 176 | #define PT_REGS_PARM3(x) ((x)->uregs[2]) 177 | #define PT_REGS_PARM4(x) ((x)->uregs[3]) 178 | #define PT_REGS_PARM5(x) ((x)->uregs[4]) 179 | #define PT_REGS_RET(x) ((x)->uregs[14]) 180 | #define PT_REGS_FP(x) ((x)->uregs[11]) /* Works only with CONFIG_FRAME_POINTER */ 181 | #define PT_REGS_RC(x) ((x)->uregs[0]) 182 | #define PT_REGS_SP(x) ((x)->uregs[13]) 183 | #define PT_REGS_IP(x) ((x)->uregs[12]) 184 | 185 | #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), uregs[0]) 186 | #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), uregs[1]) 187 | #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), uregs[2]) 188 | #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((x), uregs[3]) 189 | #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((x), uregs[4]) 190 | #define PT_REGS_RET_CORE(x) BPF_CORE_READ((x), uregs[14]) 191 | #define PT_REGS_FP_CORE(x) BPF_CORE_READ((x), uregs[11]) 192 | #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), uregs[0]) 193 | #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), uregs[13]) 194 | #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), uregs[12]) 195 | 196 | #elif defined(bpf_target_arm64) 197 | 198 | /* arm64 provides struct user_pt_regs instead of struct pt_regs to userspace */ 199 | struct pt_regs; 200 | #define PT_REGS_ARM64 const volatile struct user_pt_regs 201 | #define PT_REGS_PARM1(x) (((PT_REGS_ARM64 *)(x))->regs[0]) 202 | #define PT_REGS_PARM2(x) (((PT_REGS_ARM64 *)(x))->regs[1]) 203 | #define PT_REGS_PARM3(x) (((PT_REGS_ARM64 *)(x))->regs[2]) 204 | #define PT_REGS_PARM4(x) (((PT_REGS_ARM64 *)(x))->regs[3]) 205 | #define PT_REGS_PARM5(x) (((PT_REGS_ARM64 *)(x))->regs[4]) 206 | #define PT_REGS_RET(x) (((PT_REGS_ARM64 *)(x))->regs[30]) 207 | /* Works only with CONFIG_FRAME_POINTER */ 208 | #define PT_REGS_FP(x) (((PT_REGS_ARM64 *)(x))->regs[29]) 209 | #define PT_REGS_RC(x) (((PT_REGS_ARM64 *)(x))->regs[0]) 210 | #define PT_REGS_SP(x) (((PT_REGS_ARM64 *)(x))->sp) 211 | #define PT_REGS_IP(x) (((PT_REGS_ARM64 *)(x))->pc) 212 | 213 | #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[0]) 214 | #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[1]) 215 | #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[2]) 216 | #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[3]) 217 | #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[4]) 218 | #define PT_REGS_RET_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[30]) 219 | #define PT_REGS_FP_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[29]) 220 | #define PT_REGS_RC_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), regs[0]) 221 | #define PT_REGS_SP_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), sp) 222 | #define PT_REGS_IP_CORE(x) BPF_CORE_READ((PT_REGS_ARM64 *)(x), pc) 223 | 224 | #elif defined(bpf_target_mips) 225 | 226 | #define PT_REGS_PARM1(x) ((x)->regs[4]) 227 | #define PT_REGS_PARM2(x) ((x)->regs[5]) 228 | #define PT_REGS_PARM3(x) ((x)->regs[6]) 229 | #define PT_REGS_PARM4(x) ((x)->regs[7]) 230 | #define PT_REGS_PARM5(x) ((x)->regs[8]) 231 | #define PT_REGS_RET(x) ((x)->regs[31]) 232 | #define PT_REGS_FP(x) ((x)->regs[30]) /* Works only with CONFIG_FRAME_POINTER */ 233 | #define PT_REGS_RC(x) ((x)->regs[2]) 234 | #define PT_REGS_SP(x) ((x)->regs[29]) 235 | #define PT_REGS_IP(x) ((x)->cp0_epc) 236 | 237 | #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), regs[4]) 238 | #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), regs[5]) 239 | #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), regs[6]) 240 | #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((x), regs[7]) 241 | #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((x), regs[8]) 242 | #define PT_REGS_RET_CORE(x) BPF_CORE_READ((x), regs[31]) 243 | #define PT_REGS_FP_CORE(x) BPF_CORE_READ((x), regs[30]) 244 | #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), regs[2]) 245 | #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), regs[29]) 246 | #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), cp0_epc) 247 | 248 | #elif defined(bpf_target_powerpc) 249 | 250 | #define PT_REGS_PARM1(x) ((x)->gpr[3]) 251 | #define PT_REGS_PARM2(x) ((x)->gpr[4]) 252 | #define PT_REGS_PARM3(x) ((x)->gpr[5]) 253 | #define PT_REGS_PARM4(x) ((x)->gpr[6]) 254 | #define PT_REGS_PARM5(x) ((x)->gpr[7]) 255 | #define PT_REGS_RC(x) ((x)->gpr[3]) 256 | #define PT_REGS_SP(x) ((x)->sp) 257 | #define PT_REGS_IP(x) ((x)->nip) 258 | 259 | #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), gpr[3]) 260 | #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), gpr[4]) 261 | #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), gpr[5]) 262 | #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((x), gpr[6]) 263 | #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((x), gpr[7]) 264 | #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), gpr[3]) 265 | #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), sp) 266 | #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), nip) 267 | 268 | #elif defined(bpf_target_sparc) 269 | 270 | #define PT_REGS_PARM1(x) ((x)->u_regs[UREG_I0]) 271 | #define PT_REGS_PARM2(x) ((x)->u_regs[UREG_I1]) 272 | #define PT_REGS_PARM3(x) ((x)->u_regs[UREG_I2]) 273 | #define PT_REGS_PARM4(x) ((x)->u_regs[UREG_I3]) 274 | #define PT_REGS_PARM5(x) ((x)->u_regs[UREG_I4]) 275 | #define PT_REGS_RET(x) ((x)->u_regs[UREG_I7]) 276 | #define PT_REGS_RC(x) ((x)->u_regs[UREG_I0]) 277 | #define PT_REGS_SP(x) ((x)->u_regs[UREG_FP]) 278 | 279 | #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I0]) 280 | #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I1]) 281 | #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I2]) 282 | #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I3]) 283 | #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I4]) 284 | #define PT_REGS_RET_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I7]) 285 | #define PT_REGS_RC_CORE(x) BPF_CORE_READ((x), u_regs[UREG_I0]) 286 | #define PT_REGS_SP_CORE(x) BPF_CORE_READ((x), u_regs[UREG_FP]) 287 | 288 | /* Should this also be a bpf_target check for the sparc case? */ 289 | #if defined(__arch64__) 290 | #define PT_REGS_IP(x) ((x)->tpc) 291 | #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), tpc) 292 | #else 293 | #define PT_REGS_IP(x) ((x)->pc) 294 | #define PT_REGS_IP_CORE(x) BPF_CORE_READ((x), pc) 295 | #endif 296 | 297 | #elif defined(bpf_target_riscv) 298 | 299 | struct pt_regs; 300 | #define PT_REGS_RV const volatile struct user_regs_struct 301 | #define PT_REGS_PARM1(x) (((PT_REGS_RV *)(x))->a0) 302 | #define PT_REGS_PARM2(x) (((PT_REGS_RV *)(x))->a1) 303 | #define PT_REGS_PARM3(x) (((PT_REGS_RV *)(x))->a2) 304 | #define PT_REGS_PARM4(x) (((PT_REGS_RV *)(x))->a3) 305 | #define PT_REGS_PARM5(x) (((PT_REGS_RV *)(x))->a4) 306 | #define PT_REGS_RET(x) (((PT_REGS_RV *)(x))->ra) 307 | #define PT_REGS_FP(x) (((PT_REGS_RV *)(x))->s5) 308 | #define PT_REGS_RC(x) (((PT_REGS_RV *)(x))->a5) 309 | #define PT_REGS_SP(x) (((PT_REGS_RV *)(x))->sp) 310 | #define PT_REGS_IP(x) (((PT_REGS_RV *)(x))->epc) 311 | 312 | #define PT_REGS_PARM1_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), a0) 313 | #define PT_REGS_PARM2_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), a1) 314 | #define PT_REGS_PARM3_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), a2) 315 | #define PT_REGS_PARM4_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), a3) 316 | #define PT_REGS_PARM5_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), a4) 317 | #define PT_REGS_RET_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), ra) 318 | #define PT_REGS_FP_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), fp) 319 | #define PT_REGS_RC_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), a5) 320 | #define PT_REGS_SP_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), sp) 321 | #define PT_REGS_IP_CORE(x) BPF_CORE_READ((PT_REGS_RV *)(x), epc) 322 | 323 | #endif 324 | 325 | #if defined(bpf_target_powerpc) 326 | #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; }) 327 | #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP 328 | #elif defined(bpf_target_sparc) 329 | #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = PT_REGS_RET(ctx); }) 330 | #define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP 331 | #elif defined(bpf_target_defined) 332 | #define BPF_KPROBE_READ_RET_IP(ip, ctx) \ 333 | ({ bpf_probe_read_kernel(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); }) 334 | #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) \ 335 | ({ bpf_probe_read_kernel(&(ip), sizeof(ip), \ 336 | (void *)(PT_REGS_FP(ctx) + sizeof(ip))); }) 337 | #endif 338 | 339 | #if !defined(bpf_target_defined) 340 | 341 | #define PT_REGS_PARM1(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 342 | #define PT_REGS_PARM2(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 343 | #define PT_REGS_PARM3(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 344 | #define PT_REGS_PARM4(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 345 | #define PT_REGS_PARM5(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 346 | #define PT_REGS_RET(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 347 | #define PT_REGS_FP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 348 | #define PT_REGS_RC(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 349 | #define PT_REGS_SP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 350 | #define PT_REGS_IP(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 351 | 352 | #define PT_REGS_PARM1_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 353 | #define PT_REGS_PARM2_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 354 | #define PT_REGS_PARM3_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 355 | #define PT_REGS_PARM4_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 356 | #define PT_REGS_PARM5_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 357 | #define PT_REGS_RET_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 358 | #define PT_REGS_FP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 359 | #define PT_REGS_RC_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 360 | #define PT_REGS_SP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 361 | #define PT_REGS_IP_CORE(x) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 362 | 363 | #define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 364 | #define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ _Pragma(__BPF_TARGET_MISSING); 0l; }) 365 | 366 | #endif /* !defined(bpf_target_defined) */ 367 | 368 | #ifndef ___bpf_concat 369 | #define ___bpf_concat(a, b) a ## b 370 | #endif 371 | #ifndef ___bpf_apply 372 | #define ___bpf_apply(fn, n) ___bpf_concat(fn, n) 373 | #endif 374 | #ifndef ___bpf_nth 375 | #define ___bpf_nth(_, _1, _2, _3, _4, _5, _6, _7, _8, _9, _a, _b, _c, N, ...) N 376 | #endif 377 | #ifndef ___bpf_narg 378 | #define ___bpf_narg(...) \ 379 | ___bpf_nth(_, ##__VA_ARGS__, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) 380 | #endif 381 | 382 | #define ___bpf_ctx_cast0() ctx 383 | #define ___bpf_ctx_cast1(x) ___bpf_ctx_cast0(), (void *)ctx[0] 384 | #define ___bpf_ctx_cast2(x, args...) ___bpf_ctx_cast1(args), (void *)ctx[1] 385 | #define ___bpf_ctx_cast3(x, args...) ___bpf_ctx_cast2(args), (void *)ctx[2] 386 | #define ___bpf_ctx_cast4(x, args...) ___bpf_ctx_cast3(args), (void *)ctx[3] 387 | #define ___bpf_ctx_cast5(x, args...) ___bpf_ctx_cast4(args), (void *)ctx[4] 388 | #define ___bpf_ctx_cast6(x, args...) ___bpf_ctx_cast5(args), (void *)ctx[5] 389 | #define ___bpf_ctx_cast7(x, args...) ___bpf_ctx_cast6(args), (void *)ctx[6] 390 | #define ___bpf_ctx_cast8(x, args...) ___bpf_ctx_cast7(args), (void *)ctx[7] 391 | #define ___bpf_ctx_cast9(x, args...) ___bpf_ctx_cast8(args), (void *)ctx[8] 392 | #define ___bpf_ctx_cast10(x, args...) ___bpf_ctx_cast9(args), (void *)ctx[9] 393 | #define ___bpf_ctx_cast11(x, args...) ___bpf_ctx_cast10(args), (void *)ctx[10] 394 | #define ___bpf_ctx_cast12(x, args...) ___bpf_ctx_cast11(args), (void *)ctx[11] 395 | #define ___bpf_ctx_cast(args...) \ 396 | ___bpf_apply(___bpf_ctx_cast, ___bpf_narg(args))(args) 397 | 398 | /* 399 | * BPF_PROG is a convenience wrapper for generic tp_btf/fentry/fexit and 400 | * similar kinds of BPF programs, that accept input arguments as a single 401 | * pointer to untyped u64 array, where each u64 can actually be a typed 402 | * pointer or integer of different size. Instead of requring user to write 403 | * manual casts and work with array elements by index, BPF_PROG macro 404 | * allows user to declare a list of named and typed input arguments in the 405 | * same syntax as for normal C function. All the casting is hidden and 406 | * performed transparently, while user code can just assume working with 407 | * function arguments of specified type and name. 408 | * 409 | * Original raw context argument is preserved as well as 'ctx' argument. 410 | * This is useful when using BPF helpers that expect original context 411 | * as one of the parameters (e.g., for bpf_perf_event_output()). 412 | */ 413 | #define BPF_PROG(name, args...) \ 414 | name(unsigned long long *ctx); \ 415 | static __attribute__((always_inline)) typeof(name(0)) \ 416 | ____##name(unsigned long long *ctx, ##args); \ 417 | typeof(name(0)) name(unsigned long long *ctx) \ 418 | { \ 419 | _Pragma("GCC diagnostic push") \ 420 | _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 421 | return ____##name(___bpf_ctx_cast(args)); \ 422 | _Pragma("GCC diagnostic pop") \ 423 | } \ 424 | static __attribute__((always_inline)) typeof(name(0)) \ 425 | ____##name(unsigned long long *ctx, ##args) 426 | 427 | struct pt_regs; 428 | 429 | #define ___bpf_kprobe_args0() ctx 430 | #define ___bpf_kprobe_args1(x) \ 431 | ___bpf_kprobe_args0(), (void *)PT_REGS_PARM1(ctx) 432 | #define ___bpf_kprobe_args2(x, args...) \ 433 | ___bpf_kprobe_args1(args), (void *)PT_REGS_PARM2(ctx) 434 | #define ___bpf_kprobe_args3(x, args...) \ 435 | ___bpf_kprobe_args2(args), (void *)PT_REGS_PARM3(ctx) 436 | #define ___bpf_kprobe_args4(x, args...) \ 437 | ___bpf_kprobe_args3(args), (void *)PT_REGS_PARM4(ctx) 438 | #define ___bpf_kprobe_args5(x, args...) \ 439 | ___bpf_kprobe_args4(args), (void *)PT_REGS_PARM5(ctx) 440 | #define ___bpf_kprobe_args(args...) \ 441 | ___bpf_apply(___bpf_kprobe_args, ___bpf_narg(args))(args) 442 | 443 | /* 444 | * BPF_KPROBE serves the same purpose for kprobes as BPF_PROG for 445 | * tp_btf/fentry/fexit BPF programs. It hides the underlying platform-specific 446 | * low-level way of getting kprobe input arguments from struct pt_regs, and 447 | * provides a familiar typed and named function arguments syntax and 448 | * semantics of accessing kprobe input paremeters. 449 | * 450 | * Original struct pt_regs* context is preserved as 'ctx' argument. This might 451 | * be necessary when using BPF helpers like bpf_perf_event_output(). 452 | */ 453 | #define BPF_KPROBE(name, args...) \ 454 | name(struct pt_regs *ctx); \ 455 | static __attribute__((always_inline)) typeof(name(0)) \ 456 | ____##name(struct pt_regs *ctx, ##args); \ 457 | typeof(name(0)) name(struct pt_regs *ctx) \ 458 | { \ 459 | _Pragma("GCC diagnostic push") \ 460 | _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 461 | return ____##name(___bpf_kprobe_args(args)); \ 462 | _Pragma("GCC diagnostic pop") \ 463 | } \ 464 | static __attribute__((always_inline)) typeof(name(0)) \ 465 | ____##name(struct pt_regs *ctx, ##args) 466 | 467 | #define ___bpf_kretprobe_args0() ctx 468 | #define ___bpf_kretprobe_args1(x) \ 469 | ___bpf_kretprobe_args0(), (void *)PT_REGS_RC(ctx) 470 | #define ___bpf_kretprobe_args(args...) \ 471 | ___bpf_apply(___bpf_kretprobe_args, ___bpf_narg(args))(args) 472 | 473 | /* 474 | * BPF_KRETPROBE is similar to BPF_KPROBE, except, it only provides optional 475 | * return value (in addition to `struct pt_regs *ctx`), but no input 476 | * arguments, because they will be clobbered by the time probed function 477 | * returns. 478 | */ 479 | #define BPF_KRETPROBE(name, args...) \ 480 | name(struct pt_regs *ctx); \ 481 | static __attribute__((always_inline)) typeof(name(0)) \ 482 | ____##name(struct pt_regs *ctx, ##args); \ 483 | typeof(name(0)) name(struct pt_regs *ctx) \ 484 | { \ 485 | _Pragma("GCC diagnostic push") \ 486 | _Pragma("GCC diagnostic ignored \"-Wint-conversion\"") \ 487 | return ____##name(___bpf_kretprobe_args(args)); \ 488 | _Pragma("GCC diagnostic pop") \ 489 | } \ 490 | static __always_inline typeof(name(0)) ____##name(struct pt_regs *ctx, ##args) 491 | 492 | #endif 493 | -------------------------------------------------------------------------------- /headers/bpf/bpf_helper_defs.h: -------------------------------------------------------------------------------- 1 | /* This is auto-generated file. See bpf_doc.py for details. */ 2 | 3 | /* Forward declarations of BPF structs */ 4 | struct bpf_fib_lookup; 5 | struct bpf_sk_lookup; 6 | struct bpf_perf_event_data; 7 | struct bpf_perf_event_value; 8 | struct bpf_pidns_info; 9 | struct bpf_redir_neigh; 10 | struct bpf_sock; 11 | struct bpf_sock_addr; 12 | struct bpf_sock_ops; 13 | struct bpf_sock_tuple; 14 | struct bpf_spin_lock; 15 | struct bpf_sysctl; 16 | struct bpf_tcp_sock; 17 | struct bpf_tunnel_key; 18 | struct bpf_xfrm_state; 19 | struct linux_binprm; 20 | struct pt_regs; 21 | struct sk_reuseport_md; 22 | struct sockaddr; 23 | struct tcphdr; 24 | struct seq_file; 25 | struct tcp6_sock; 26 | struct tcp_sock; 27 | struct tcp_timewait_sock; 28 | struct tcp_request_sock; 29 | struct udp6_sock; 30 | struct unix_sock; 31 | struct task_struct; 32 | struct __sk_buff; 33 | struct sk_msg_md; 34 | struct xdp_md; 35 | struct path; 36 | struct btf_ptr; 37 | struct inode; 38 | struct socket; 39 | struct file; 40 | struct bpf_timer; 41 | 42 | /* 43 | * bpf_map_lookup_elem 44 | * 45 | * Perform a lookup in *map* for an entry associated to *key*. 46 | * 47 | * Returns 48 | * Map value associated to *key*, or **NULL** if no entry was 49 | * found. 50 | */ 51 | static void *(*bpf_map_lookup_elem)(void *map, const void *key) = (void *) 1; 52 | 53 | /* 54 | * bpf_map_update_elem 55 | * 56 | * Add or update the value of the entry associated to *key* in 57 | * *map* with *value*. *flags* is one of: 58 | * 59 | * **BPF_NOEXIST** 60 | * The entry for *key* must not exist in the map. 61 | * **BPF_EXIST** 62 | * The entry for *key* must already exist in the map. 63 | * **BPF_ANY** 64 | * No condition on the existence of the entry for *key*. 65 | * 66 | * Flag value **BPF_NOEXIST** cannot be used for maps of types 67 | * **BPF_MAP_TYPE_ARRAY** or **BPF_MAP_TYPE_PERCPU_ARRAY** (all 68 | * elements always exist), the helper would return an error. 69 | * 70 | * Returns 71 | * 0 on success, or a negative error in case of failure. 72 | */ 73 | static long (*bpf_map_update_elem)(void *map, const void *key, const void *value, __u64 flags) = (void *) 2; 74 | 75 | /* 76 | * bpf_map_delete_elem 77 | * 78 | * Delete entry with *key* from *map*. 79 | * 80 | * Returns 81 | * 0 on success, or a negative error in case of failure. 82 | */ 83 | static long (*bpf_map_delete_elem)(void *map, const void *key) = (void *) 3; 84 | 85 | /* 86 | * bpf_probe_read 87 | * 88 | * For tracing programs, safely attempt to read *size* bytes from 89 | * kernel space address *unsafe_ptr* and store the data in *dst*. 90 | * 91 | * Generally, use **bpf_probe_read_user**\ () or 92 | * **bpf_probe_read_kernel**\ () instead. 93 | * 94 | * Returns 95 | * 0 on success, or a negative error in case of failure. 96 | */ 97 | static long (*bpf_probe_read)(void *dst, __u32 size, const void *unsafe_ptr) = (void *) 4; 98 | 99 | /* 100 | * bpf_ktime_get_ns 101 | * 102 | * Return the time elapsed since system boot, in nanoseconds. 103 | * Does not include time the system was suspended. 104 | * See: **clock_gettime**\ (**CLOCK_MONOTONIC**) 105 | * 106 | * Returns 107 | * Current *ktime*. 108 | */ 109 | static __u64 (*bpf_ktime_get_ns)(void) = (void *) 5; 110 | 111 | /* 112 | * bpf_trace_printk 113 | * 114 | * This helper is a "printk()-like" facility for debugging. It 115 | * prints a message defined by format *fmt* (of size *fmt_size*) 116 | * to file *\/sys/kernel/debug/tracing/trace* from DebugFS, if 117 | * available. It can take up to three additional **u64** 118 | * arguments (as an eBPF helpers, the total number of arguments is 119 | * limited to five). 120 | * 121 | * Each time the helper is called, it appends a line to the trace. 122 | * Lines are discarded while *\/sys/kernel/debug/tracing/trace* is 123 | * open, use *\/sys/kernel/debug/tracing/trace_pipe* to avoid this. 124 | * The format of the trace is customizable, and the exact output 125 | * one will get depends on the options set in 126 | * *\/sys/kernel/debug/tracing/trace_options* (see also the 127 | * *README* file under the same directory). However, it usually 128 | * defaults to something like: 129 | * 130 | * :: 131 | * 132 | * telnet-470 [001] .N.. 419421.045894: 0x00000001: 133 | * 134 | * In the above: 135 | * 136 | * * ``telnet`` is the name of the current task. 137 | * * ``470`` is the PID of the current task. 138 | * * ``001`` is the CPU number on which the task is 139 | * running. 140 | * * In ``.N..``, each character refers to a set of 141 | * options (whether irqs are enabled, scheduling 142 | * options, whether hard/softirqs are running, level of 143 | * preempt_disabled respectively). **N** means that 144 | * **TIF_NEED_RESCHED** and **PREEMPT_NEED_RESCHED** 145 | * are set. 146 | * * ``419421.045894`` is a timestamp. 147 | * * ``0x00000001`` is a fake value used by BPF for the 148 | * instruction pointer register. 149 | * * ```` is the message formatted with 150 | * *fmt*. 151 | * 152 | * The conversion specifiers supported by *fmt* are similar, but 153 | * more limited than for printk(). They are **%d**, **%i**, 154 | * **%u**, **%x**, **%ld**, **%li**, **%lu**, **%lx**, **%lld**, 155 | * **%lli**, **%llu**, **%llx**, **%p**, **%s**. No modifier (size 156 | * of field, padding with zeroes, etc.) is available, and the 157 | * helper will return **-EINVAL** (but print nothing) if it 158 | * encounters an unknown specifier. 159 | * 160 | * Also, note that **bpf_trace_printk**\ () is slow, and should 161 | * only be used for debugging purposes. For this reason, a notice 162 | * block (spanning several lines) is printed to kernel logs and 163 | * states that the helper should not be used "for production use" 164 | * the first time this helper is used (or more precisely, when 165 | * **trace_printk**\ () buffers are allocated). For passing values 166 | * to user space, perf events should be preferred. 167 | * 168 | * Returns 169 | * The number of bytes written to the buffer, or a negative error 170 | * in case of failure. 171 | */ 172 | static long (*bpf_trace_printk)(const char *fmt, __u32 fmt_size, ...) = (void *) 6; 173 | 174 | /* 175 | * bpf_get_prandom_u32 176 | * 177 | * Get a pseudo-random number. 178 | * 179 | * From a security point of view, this helper uses its own 180 | * pseudo-random internal state, and cannot be used to infer the 181 | * seed of other random functions in the kernel. However, it is 182 | * essential to note that the generator used by the helper is not 183 | * cryptographically secure. 184 | * 185 | * Returns 186 | * A random 32-bit unsigned value. 187 | */ 188 | static __u32 (*bpf_get_prandom_u32)(void) = (void *) 7; 189 | 190 | /* 191 | * bpf_get_smp_processor_id 192 | * 193 | * Get the SMP (symmetric multiprocessing) processor id. Note that 194 | * all programs run with migration disabled, which means that the 195 | * SMP processor id is stable during all the execution of the 196 | * program. 197 | * 198 | * Returns 199 | * The SMP id of the processor running the program. 200 | */ 201 | static __u32 (*bpf_get_smp_processor_id)(void) = (void *) 8; 202 | 203 | /* 204 | * bpf_skb_store_bytes 205 | * 206 | * Store *len* bytes from address *from* into the packet 207 | * associated to *skb*, at *offset*. *flags* are a combination of 208 | * **BPF_F_RECOMPUTE_CSUM** (automatically recompute the 209 | * checksum for the packet after storing the bytes) and 210 | * **BPF_F_INVALIDATE_HASH** (set *skb*\ **->hash**, *skb*\ 211 | * **->swhash** and *skb*\ **->l4hash** to 0). 212 | * 213 | * A call to this helper is susceptible to change the underlying 214 | * packet buffer. Therefore, at load time, all checks on pointers 215 | * previously done by the verifier are invalidated and must be 216 | * performed again, if the helper is used in combination with 217 | * direct packet access. 218 | * 219 | * Returns 220 | * 0 on success, or a negative error in case of failure. 221 | */ 222 | static long (*bpf_skb_store_bytes)(struct __sk_buff *skb, __u32 offset, const void *from, __u32 len, __u64 flags) = (void *) 9; 223 | 224 | /* 225 | * bpf_l3_csum_replace 226 | * 227 | * Recompute the layer 3 (e.g. IP) checksum for the packet 228 | * associated to *skb*. Computation is incremental, so the helper 229 | * must know the former value of the header field that was 230 | * modified (*from*), the new value of this field (*to*), and the 231 | * number of bytes (2 or 4) for this field, stored in *size*. 232 | * Alternatively, it is possible to store the difference between 233 | * the previous and the new values of the header field in *to*, by 234 | * setting *from* and *size* to 0. For both methods, *offset* 235 | * indicates the location of the IP checksum within the packet. 236 | * 237 | * This helper works in combination with **bpf_csum_diff**\ (), 238 | * which does not update the checksum in-place, but offers more 239 | * flexibility and can handle sizes larger than 2 or 4 for the 240 | * checksum to update. 241 | * 242 | * A call to this helper is susceptible to change the underlying 243 | * packet buffer. Therefore, at load time, all checks on pointers 244 | * previously done by the verifier are invalidated and must be 245 | * performed again, if the helper is used in combination with 246 | * direct packet access. 247 | * 248 | * Returns 249 | * 0 on success, or a negative error in case of failure. 250 | */ 251 | static long (*bpf_l3_csum_replace)(struct __sk_buff *skb, __u32 offset, __u64 from, __u64 to, __u64 size) = (void *) 10; 252 | 253 | /* 254 | * bpf_l4_csum_replace 255 | * 256 | * Recompute the layer 4 (e.g. TCP, UDP or ICMP) checksum for the 257 | * packet associated to *skb*. Computation is incremental, so the 258 | * helper must know the former value of the header field that was 259 | * modified (*from*), the new value of this field (*to*), and the 260 | * number of bytes (2 or 4) for this field, stored on the lowest 261 | * four bits of *flags*. Alternatively, it is possible to store 262 | * the difference between the previous and the new values of the 263 | * header field in *to*, by setting *from* and the four lowest 264 | * bits of *flags* to 0. For both methods, *offset* indicates the 265 | * location of the IP checksum within the packet. In addition to 266 | * the size of the field, *flags* can be added (bitwise OR) actual 267 | * flags. With **BPF_F_MARK_MANGLED_0**, a null checksum is left 268 | * untouched (unless **BPF_F_MARK_ENFORCE** is added as well), and 269 | * for updates resulting in a null checksum the value is set to 270 | * **CSUM_MANGLED_0** instead. Flag **BPF_F_PSEUDO_HDR** indicates 271 | * the checksum is to be computed against a pseudo-header. 272 | * 273 | * This helper works in combination with **bpf_csum_diff**\ (), 274 | * which does not update the checksum in-place, but offers more 275 | * flexibility and can handle sizes larger than 2 or 4 for the 276 | * checksum to update. 277 | * 278 | * A call to this helper is susceptible to change the underlying 279 | * packet buffer. Therefore, at load time, all checks on pointers 280 | * previously done by the verifier are invalidated and must be 281 | * performed again, if the helper is used in combination with 282 | * direct packet access. 283 | * 284 | * Returns 285 | * 0 on success, or a negative error in case of failure. 286 | */ 287 | static long (*bpf_l4_csum_replace)(struct __sk_buff *skb, __u32 offset, __u64 from, __u64 to, __u64 flags) = (void *) 11; 288 | 289 | /* 290 | * bpf_tail_call 291 | * 292 | * This special helper is used to trigger a "tail call", or in 293 | * other words, to jump into another eBPF program. The same stack 294 | * frame is used (but values on stack and in registers for the 295 | * caller are not accessible to the callee). This mechanism allows 296 | * for program chaining, either for raising the maximum number of 297 | * available eBPF instructions, or to execute given programs in 298 | * conditional blocks. For security reasons, there is an upper 299 | * limit to the number of successive tail calls that can be 300 | * performed. 301 | * 302 | * Upon call of this helper, the program attempts to jump into a 303 | * program referenced at index *index* in *prog_array_map*, a 304 | * special map of type **BPF_MAP_TYPE_PROG_ARRAY**, and passes 305 | * *ctx*, a pointer to the context. 306 | * 307 | * If the call succeeds, the kernel immediately runs the first 308 | * instruction of the new program. This is not a function call, 309 | * and it never returns to the previous program. If the call 310 | * fails, then the helper has no effect, and the caller continues 311 | * to run its subsequent instructions. A call can fail if the 312 | * destination program for the jump does not exist (i.e. *index* 313 | * is superior to the number of entries in *prog_array_map*), or 314 | * if the maximum number of tail calls has been reached for this 315 | * chain of programs. This limit is defined in the kernel by the 316 | * macro **MAX_TAIL_CALL_CNT** (not accessible to user space), 317 | * which is currently set to 33. 318 | * 319 | * Returns 320 | * 0 on success, or a negative error in case of failure. 321 | */ 322 | static long (*bpf_tail_call)(void *ctx, void *prog_array_map, __u32 index) = (void *) 12; 323 | 324 | /* 325 | * bpf_clone_redirect 326 | * 327 | * Clone and redirect the packet associated to *skb* to another 328 | * net device of index *ifindex*. Both ingress and egress 329 | * interfaces can be used for redirection. The **BPF_F_INGRESS** 330 | * value in *flags* is used to make the distinction (ingress path 331 | * is selected if the flag is present, egress path otherwise). 332 | * This is the only flag supported for now. 333 | * 334 | * In comparison with **bpf_redirect**\ () helper, 335 | * **bpf_clone_redirect**\ () has the associated cost of 336 | * duplicating the packet buffer, but this can be executed out of 337 | * the eBPF program. Conversely, **bpf_redirect**\ () is more 338 | * efficient, but it is handled through an action code where the 339 | * redirection happens only after the eBPF program has returned. 340 | * 341 | * A call to this helper is susceptible to change the underlying 342 | * packet buffer. Therefore, at load time, all checks on pointers 343 | * previously done by the verifier are invalidated and must be 344 | * performed again, if the helper is used in combination with 345 | * direct packet access. 346 | * 347 | * Returns 348 | * 0 on success, or a negative error in case of failure. 349 | */ 350 | static long (*bpf_clone_redirect)(struct __sk_buff *skb, __u32 ifindex, __u64 flags) = (void *) 13; 351 | 352 | /* 353 | * bpf_get_current_pid_tgid 354 | * 355 | * 356 | * Returns 357 | * A 64-bit integer containing the current tgid and pid, and 358 | * created as such: 359 | * *current_task*\ **->tgid << 32 \|** 360 | * *current_task*\ **->pid**. 361 | */ 362 | static __u64 (*bpf_get_current_pid_tgid)(void) = (void *) 14; 363 | 364 | /* 365 | * bpf_get_current_uid_gid 366 | * 367 | * 368 | * Returns 369 | * A 64-bit integer containing the current GID and UID, and 370 | * created as such: *current_gid* **<< 32 \|** *current_uid*. 371 | */ 372 | static __u64 (*bpf_get_current_uid_gid)(void) = (void *) 15; 373 | 374 | /* 375 | * bpf_get_current_comm 376 | * 377 | * Copy the **comm** attribute of the current task into *buf* of 378 | * *size_of_buf*. The **comm** attribute contains the name of 379 | * the executable (excluding the path) for the current task. The 380 | * *size_of_buf* must be strictly positive. On success, the 381 | * helper makes sure that the *buf* is NUL-terminated. On failure, 382 | * it is filled with zeroes. 383 | * 384 | * Returns 385 | * 0 on success, or a negative error in case of failure. 386 | */ 387 | static long (*bpf_get_current_comm)(void *buf, __u32 size_of_buf) = (void *) 16; 388 | 389 | /* 390 | * bpf_get_cgroup_classid 391 | * 392 | * Retrieve the classid for the current task, i.e. for the net_cls 393 | * cgroup to which *skb* belongs. 394 | * 395 | * This helper can be used on TC egress path, but not on ingress. 396 | * 397 | * The net_cls cgroup provides an interface to tag network packets 398 | * based on a user-provided identifier for all traffic coming from 399 | * the tasks belonging to the related cgroup. See also the related 400 | * kernel documentation, available from the Linux sources in file 401 | * *Documentation/admin-guide/cgroup-v1/net_cls.rst*. 402 | * 403 | * The Linux kernel has two versions for cgroups: there are 404 | * cgroups v1 and cgroups v2. Both are available to users, who can 405 | * use a mixture of them, but note that the net_cls cgroup is for 406 | * cgroup v1 only. This makes it incompatible with BPF programs 407 | * run on cgroups, which is a cgroup-v2-only feature (a socket can 408 | * only hold data for one version of cgroups at a time). 409 | * 410 | * This helper is only available is the kernel was compiled with 411 | * the **CONFIG_CGROUP_NET_CLASSID** configuration option set to 412 | * "**y**" or to "**m**". 413 | * 414 | * Returns 415 | * The classid, or 0 for the default unconfigured classid. 416 | */ 417 | static __u32 (*bpf_get_cgroup_classid)(struct __sk_buff *skb) = (void *) 17; 418 | 419 | /* 420 | * bpf_skb_vlan_push 421 | * 422 | * Push a *vlan_tci* (VLAN tag control information) of protocol 423 | * *vlan_proto* to the packet associated to *skb*, then update 424 | * the checksum. Note that if *vlan_proto* is different from 425 | * **ETH_P_8021Q** and **ETH_P_8021AD**, it is considered to 426 | * be **ETH_P_8021Q**. 427 | * 428 | * A call to this helper is susceptible to change the underlying 429 | * packet buffer. Therefore, at load time, all checks on pointers 430 | * previously done by the verifier are invalidated and must be 431 | * performed again, if the helper is used in combination with 432 | * direct packet access. 433 | * 434 | * Returns 435 | * 0 on success, or a negative error in case of failure. 436 | */ 437 | static long (*bpf_skb_vlan_push)(struct __sk_buff *skb, __be16 vlan_proto, __u16 vlan_tci) = (void *) 18; 438 | 439 | /* 440 | * bpf_skb_vlan_pop 441 | * 442 | * Pop a VLAN header from the packet associated to *skb*. 443 | * 444 | * A call to this helper is susceptible to change the underlying 445 | * packet buffer. Therefore, at load time, all checks on pointers 446 | * previously done by the verifier are invalidated and must be 447 | * performed again, if the helper is used in combination with 448 | * direct packet access. 449 | * 450 | * Returns 451 | * 0 on success, or a negative error in case of failure. 452 | */ 453 | static long (*bpf_skb_vlan_pop)(struct __sk_buff *skb) = (void *) 19; 454 | 455 | /* 456 | * bpf_skb_get_tunnel_key 457 | * 458 | * Get tunnel metadata. This helper takes a pointer *key* to an 459 | * empty **struct bpf_tunnel_key** of **size**, that will be 460 | * filled with tunnel metadata for the packet associated to *skb*. 461 | * The *flags* can be set to **BPF_F_TUNINFO_IPV6**, which 462 | * indicates that the tunnel is based on IPv6 protocol instead of 463 | * IPv4. 464 | * 465 | * The **struct bpf_tunnel_key** is an object that generalizes the 466 | * principal parameters used by various tunneling protocols into a 467 | * single struct. This way, it can be used to easily make a 468 | * decision based on the contents of the encapsulation header, 469 | * "summarized" in this struct. In particular, it holds the IP 470 | * address of the remote end (IPv4 or IPv6, depending on the case) 471 | * in *key*\ **->remote_ipv4** or *key*\ **->remote_ipv6**. Also, 472 | * this struct exposes the *key*\ **->tunnel_id**, which is 473 | * generally mapped to a VNI (Virtual Network Identifier), making 474 | * it programmable together with the **bpf_skb_set_tunnel_key**\ 475 | * () helper. 476 | * 477 | * Let's imagine that the following code is part of a program 478 | * attached to the TC ingress interface, on one end of a GRE 479 | * tunnel, and is supposed to filter out all messages coming from 480 | * remote ends with IPv4 address other than 10.0.0.1: 481 | * 482 | * :: 483 | * 484 | * int ret; 485 | * struct bpf_tunnel_key key = {}; 486 | * 487 | * ret = bpf_skb_get_tunnel_key(skb, &key, sizeof(key), 0); 488 | * if (ret < 0) 489 | * return TC_ACT_SHOT; // drop packet 490 | * 491 | * if (key.remote_ipv4 != 0x0a000001) 492 | * return TC_ACT_SHOT; // drop packet 493 | * 494 | * return TC_ACT_OK; // accept packet 495 | * 496 | * This interface can also be used with all encapsulation devices 497 | * that can operate in "collect metadata" mode: instead of having 498 | * one network device per specific configuration, the "collect 499 | * metadata" mode only requires a single device where the 500 | * configuration can be extracted from this helper. 501 | * 502 | * This can be used together with various tunnels such as VXLan, 503 | * Geneve, GRE or IP in IP (IPIP). 504 | * 505 | * Returns 506 | * 0 on success, or a negative error in case of failure. 507 | */ 508 | static long (*bpf_skb_get_tunnel_key)(struct __sk_buff *skb, struct bpf_tunnel_key *key, __u32 size, __u64 flags) = (void *) 20; 509 | 510 | /* 511 | * bpf_skb_set_tunnel_key 512 | * 513 | * Populate tunnel metadata for packet associated to *skb.* The 514 | * tunnel metadata is set to the contents of *key*, of *size*. The 515 | * *flags* can be set to a combination of the following values: 516 | * 517 | * **BPF_F_TUNINFO_IPV6** 518 | * Indicate that the tunnel is based on IPv6 protocol 519 | * instead of IPv4. 520 | * **BPF_F_ZERO_CSUM_TX** 521 | * For IPv4 packets, add a flag to tunnel metadata 522 | * indicating that checksum computation should be skipped 523 | * and checksum set to zeroes. 524 | * **BPF_F_DONT_FRAGMENT** 525 | * Add a flag to tunnel metadata indicating that the 526 | * packet should not be fragmented. 527 | * **BPF_F_SEQ_NUMBER** 528 | * Add a flag to tunnel metadata indicating that a 529 | * sequence number should be added to tunnel header before 530 | * sending the packet. This flag was added for GRE 531 | * encapsulation, but might be used with other protocols 532 | * as well in the future. 533 | * 534 | * Here is a typical usage on the transmit path: 535 | * 536 | * :: 537 | * 538 | * struct bpf_tunnel_key key; 539 | * populate key ... 540 | * bpf_skb_set_tunnel_key(skb, &key, sizeof(key), 0); 541 | * bpf_clone_redirect(skb, vxlan_dev_ifindex, 0); 542 | * 543 | * See also the description of the **bpf_skb_get_tunnel_key**\ () 544 | * helper for additional information. 545 | * 546 | * Returns 547 | * 0 on success, or a negative error in case of failure. 548 | */ 549 | static long (*bpf_skb_set_tunnel_key)(struct __sk_buff *skb, struct bpf_tunnel_key *key, __u32 size, __u64 flags) = (void *) 21; 550 | 551 | /* 552 | * bpf_perf_event_read 553 | * 554 | * Read the value of a perf event counter. This helper relies on a 555 | * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. The nature of 556 | * the perf event counter is selected when *map* is updated with 557 | * perf event file descriptors. The *map* is an array whose size 558 | * is the number of available CPUs, and each cell contains a value 559 | * relative to one CPU. The value to retrieve is indicated by 560 | * *flags*, that contains the index of the CPU to look up, masked 561 | * with **BPF_F_INDEX_MASK**. Alternatively, *flags* can be set to 562 | * **BPF_F_CURRENT_CPU** to indicate that the value for the 563 | * current CPU should be retrieved. 564 | * 565 | * Note that before Linux 4.13, only hardware perf event can be 566 | * retrieved. 567 | * 568 | * Also, be aware that the newer helper 569 | * **bpf_perf_event_read_value**\ () is recommended over 570 | * **bpf_perf_event_read**\ () in general. The latter has some ABI 571 | * quirks where error and counter value are used as a return code 572 | * (which is wrong to do since ranges may overlap). This issue is 573 | * fixed with **bpf_perf_event_read_value**\ (), which at the same 574 | * time provides more features over the **bpf_perf_event_read**\ 575 | * () interface. Please refer to the description of 576 | * **bpf_perf_event_read_value**\ () for details. 577 | * 578 | * Returns 579 | * The value of the perf event counter read from the map, or a 580 | * negative error code in case of failure. 581 | */ 582 | static __u64 (*bpf_perf_event_read)(void *map, __u64 flags) = (void *) 22; 583 | 584 | /* 585 | * bpf_redirect 586 | * 587 | * Redirect the packet to another net device of index *ifindex*. 588 | * This helper is somewhat similar to **bpf_clone_redirect**\ 589 | * (), except that the packet is not cloned, which provides 590 | * increased performance. 591 | * 592 | * Except for XDP, both ingress and egress interfaces can be used 593 | * for redirection. The **BPF_F_INGRESS** value in *flags* is used 594 | * to make the distinction (ingress path is selected if the flag 595 | * is present, egress path otherwise). Currently, XDP only 596 | * supports redirection to the egress interface, and accepts no 597 | * flag at all. 598 | * 599 | * The same effect can also be attained with the more generic 600 | * **bpf_redirect_map**\ (), which uses a BPF map to store the 601 | * redirect target instead of providing it directly to the helper. 602 | * 603 | * Returns 604 | * For XDP, the helper returns **XDP_REDIRECT** on success or 605 | * **XDP_ABORTED** on error. For other program types, the values 606 | * are **TC_ACT_REDIRECT** on success or **TC_ACT_SHOT** on 607 | * error. 608 | */ 609 | static long (*bpf_redirect)(__u32 ifindex, __u64 flags) = (void *) 23; 610 | 611 | /* 612 | * bpf_get_route_realm 613 | * 614 | * Retrieve the realm or the route, that is to say the 615 | * **tclassid** field of the destination for the *skb*. The 616 | * identifier retrieved is a user-provided tag, similar to the 617 | * one used with the net_cls cgroup (see description for 618 | * **bpf_get_cgroup_classid**\ () helper), but here this tag is 619 | * held by a route (a destination entry), not by a task. 620 | * 621 | * Retrieving this identifier works with the clsact TC egress hook 622 | * (see also **tc-bpf(8)**), or alternatively on conventional 623 | * classful egress qdiscs, but not on TC ingress path. In case of 624 | * clsact TC egress hook, this has the advantage that, internally, 625 | * the destination entry has not been dropped yet in the transmit 626 | * path. Therefore, the destination entry does not need to be 627 | * artificially held via **netif_keep_dst**\ () for a classful 628 | * qdisc until the *skb* is freed. 629 | * 630 | * This helper is available only if the kernel was compiled with 631 | * **CONFIG_IP_ROUTE_CLASSID** configuration option. 632 | * 633 | * Returns 634 | * The realm of the route for the packet associated to *skb*, or 0 635 | * if none was found. 636 | */ 637 | static __u32 (*bpf_get_route_realm)(struct __sk_buff *skb) = (void *) 24; 638 | 639 | /* 640 | * bpf_perf_event_output 641 | * 642 | * Write raw *data* blob into a special BPF perf event held by 643 | * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. This perf 644 | * event must have the following attributes: **PERF_SAMPLE_RAW** 645 | * as **sample_type**, **PERF_TYPE_SOFTWARE** as **type**, and 646 | * **PERF_COUNT_SW_BPF_OUTPUT** as **config**. 647 | * 648 | * The *flags* are used to indicate the index in *map* for which 649 | * the value must be put, masked with **BPF_F_INDEX_MASK**. 650 | * Alternatively, *flags* can be set to **BPF_F_CURRENT_CPU** 651 | * to indicate that the index of the current CPU core should be 652 | * used. 653 | * 654 | * The value to write, of *size*, is passed through eBPF stack and 655 | * pointed by *data*. 656 | * 657 | * The context of the program *ctx* needs also be passed to the 658 | * helper. 659 | * 660 | * On user space, a program willing to read the values needs to 661 | * call **perf_event_open**\ () on the perf event (either for 662 | * one or for all CPUs) and to store the file descriptor into the 663 | * *map*. This must be done before the eBPF program can send data 664 | * into it. An example is available in file 665 | * *samples/bpf/trace_output_user.c* in the Linux kernel source 666 | * tree (the eBPF program counterpart is in 667 | * *samples/bpf/trace_output_kern.c*). 668 | * 669 | * **bpf_perf_event_output**\ () achieves better performance 670 | * than **bpf_trace_printk**\ () for sharing data with user 671 | * space, and is much better suitable for streaming data from eBPF 672 | * programs. 673 | * 674 | * Note that this helper is not restricted to tracing use cases 675 | * and can be used with programs attached to TC or XDP as well, 676 | * where it allows for passing data to user space listeners. Data 677 | * can be: 678 | * 679 | * * Only custom structs, 680 | * * Only the packet payload, or 681 | * * A combination of both. 682 | * 683 | * Returns 684 | * 0 on success, or a negative error in case of failure. 685 | */ 686 | static long (*bpf_perf_event_output)(void *ctx, void *map, __u64 flags, void *data, __u64 size) = (void *) 25; 687 | 688 | /* 689 | * bpf_skb_load_bytes 690 | * 691 | * This helper was provided as an easy way to load data from a 692 | * packet. It can be used to load *len* bytes from *offset* from 693 | * the packet associated to *skb*, into the buffer pointed by 694 | * *to*. 695 | * 696 | * Since Linux 4.7, usage of this helper has mostly been replaced 697 | * by "direct packet access", enabling packet data to be 698 | * manipulated with *skb*\ **->data** and *skb*\ **->data_end** 699 | * pointing respectively to the first byte of packet data and to 700 | * the byte after the last byte of packet data. However, it 701 | * remains useful if one wishes to read large quantities of data 702 | * at once from a packet into the eBPF stack. 703 | * 704 | * Returns 705 | * 0 on success, or a negative error in case of failure. 706 | */ 707 | static long (*bpf_skb_load_bytes)(const void *skb, __u32 offset, void *to, __u32 len) = (void *) 26; 708 | 709 | /* 710 | * bpf_get_stackid 711 | * 712 | * Walk a user or a kernel stack and return its id. To achieve 713 | * this, the helper needs *ctx*, which is a pointer to the context 714 | * on which the tracing program is executed, and a pointer to a 715 | * *map* of type **BPF_MAP_TYPE_STACK_TRACE**. 716 | * 717 | * The last argument, *flags*, holds the number of stack frames to 718 | * skip (from 0 to 255), masked with 719 | * **BPF_F_SKIP_FIELD_MASK**. The next bits can be used to set 720 | * a combination of the following flags: 721 | * 722 | * **BPF_F_USER_STACK** 723 | * Collect a user space stack instead of a kernel stack. 724 | * **BPF_F_FAST_STACK_CMP** 725 | * Compare stacks by hash only. 726 | * **BPF_F_REUSE_STACKID** 727 | * If two different stacks hash into the same *stackid*, 728 | * discard the old one. 729 | * 730 | * The stack id retrieved is a 32 bit long integer handle which 731 | * can be further combined with other data (including other stack 732 | * ids) and used as a key into maps. This can be useful for 733 | * generating a variety of graphs (such as flame graphs or off-cpu 734 | * graphs). 735 | * 736 | * For walking a stack, this helper is an improvement over 737 | * **bpf_probe_read**\ (), which can be used with unrolled loops 738 | * but is not efficient and consumes a lot of eBPF instructions. 739 | * Instead, **bpf_get_stackid**\ () can collect up to 740 | * **PERF_MAX_STACK_DEPTH** both kernel and user frames. Note that 741 | * this limit can be controlled with the **sysctl** program, and 742 | * that it should be manually increased in order to profile long 743 | * user stacks (such as stacks for Java programs). To do so, use: 744 | * 745 | * :: 746 | * 747 | * # sysctl kernel.perf_event_max_stack= 748 | * 749 | * Returns 750 | * The positive or null stack id on success, or a negative error 751 | * in case of failure. 752 | */ 753 | static long (*bpf_get_stackid)(void *ctx, void *map, __u64 flags) = (void *) 27; 754 | 755 | /* 756 | * bpf_csum_diff 757 | * 758 | * Compute a checksum difference, from the raw buffer pointed by 759 | * *from*, of length *from_size* (that must be a multiple of 4), 760 | * towards the raw buffer pointed by *to*, of size *to_size* 761 | * (same remark). An optional *seed* can be added to the value 762 | * (this can be cascaded, the seed may come from a previous call 763 | * to the helper). 764 | * 765 | * This is flexible enough to be used in several ways: 766 | * 767 | * * With *from_size* == 0, *to_size* > 0 and *seed* set to 768 | * checksum, it can be used when pushing new data. 769 | * * With *from_size* > 0, *to_size* == 0 and *seed* set to 770 | * checksum, it can be used when removing data from a packet. 771 | * * With *from_size* > 0, *to_size* > 0 and *seed* set to 0, it 772 | * can be used to compute a diff. Note that *from_size* and 773 | * *to_size* do not need to be equal. 774 | * 775 | * This helper can be used in combination with 776 | * **bpf_l3_csum_replace**\ () and **bpf_l4_csum_replace**\ (), to 777 | * which one can feed in the difference computed with 778 | * **bpf_csum_diff**\ (). 779 | * 780 | * Returns 781 | * The checksum result, or a negative error code in case of 782 | * failure. 783 | */ 784 | static __s64 (*bpf_csum_diff)(__be32 *from, __u32 from_size, __be32 *to, __u32 to_size, __wsum seed) = (void *) 28; 785 | 786 | /* 787 | * bpf_skb_get_tunnel_opt 788 | * 789 | * Retrieve tunnel options metadata for the packet associated to 790 | * *skb*, and store the raw tunnel option data to the buffer *opt* 791 | * of *size*. 792 | * 793 | * This helper can be used with encapsulation devices that can 794 | * operate in "collect metadata" mode (please refer to the related 795 | * note in the description of **bpf_skb_get_tunnel_key**\ () for 796 | * more details). A particular example where this can be used is 797 | * in combination with the Geneve encapsulation protocol, where it 798 | * allows for pushing (with **bpf_skb_get_tunnel_opt**\ () helper) 799 | * and retrieving arbitrary TLVs (Type-Length-Value headers) from 800 | * the eBPF program. This allows for full customization of these 801 | * headers. 802 | * 803 | * Returns 804 | * The size of the option data retrieved. 805 | */ 806 | static long (*bpf_skb_get_tunnel_opt)(struct __sk_buff *skb, void *opt, __u32 size) = (void *) 29; 807 | 808 | /* 809 | * bpf_skb_set_tunnel_opt 810 | * 811 | * Set tunnel options metadata for the packet associated to *skb* 812 | * to the option data contained in the raw buffer *opt* of *size*. 813 | * 814 | * See also the description of the **bpf_skb_get_tunnel_opt**\ () 815 | * helper for additional information. 816 | * 817 | * Returns 818 | * 0 on success, or a negative error in case of failure. 819 | */ 820 | static long (*bpf_skb_set_tunnel_opt)(struct __sk_buff *skb, void *opt, __u32 size) = (void *) 30; 821 | 822 | /* 823 | * bpf_skb_change_proto 824 | * 825 | * Change the protocol of the *skb* to *proto*. Currently 826 | * supported are transition from IPv4 to IPv6, and from IPv6 to 827 | * IPv4. The helper takes care of the groundwork for the 828 | * transition, including resizing the socket buffer. The eBPF 829 | * program is expected to fill the new headers, if any, via 830 | * **skb_store_bytes**\ () and to recompute the checksums with 831 | * **bpf_l3_csum_replace**\ () and **bpf_l4_csum_replace**\ 832 | * (). The main case for this helper is to perform NAT64 833 | * operations out of an eBPF program. 834 | * 835 | * Internally, the GSO type is marked as dodgy so that headers are 836 | * checked and segments are recalculated by the GSO/GRO engine. 837 | * The size for GSO target is adapted as well. 838 | * 839 | * All values for *flags* are reserved for future usage, and must 840 | * be left at zero. 841 | * 842 | * A call to this helper is susceptible to change the underlying 843 | * packet buffer. Therefore, at load time, all checks on pointers 844 | * previously done by the verifier are invalidated and must be 845 | * performed again, if the helper is used in combination with 846 | * direct packet access. 847 | * 848 | * Returns 849 | * 0 on success, or a negative error in case of failure. 850 | */ 851 | static long (*bpf_skb_change_proto)(struct __sk_buff *skb, __be16 proto, __u64 flags) = (void *) 31; 852 | 853 | /* 854 | * bpf_skb_change_type 855 | * 856 | * Change the packet type for the packet associated to *skb*. This 857 | * comes down to setting *skb*\ **->pkt_type** to *type*, except 858 | * the eBPF program does not have a write access to *skb*\ 859 | * **->pkt_type** beside this helper. Using a helper here allows 860 | * for graceful handling of errors. 861 | * 862 | * The major use case is to change incoming *skb*s to 863 | * **PACKET_HOST** in a programmatic way instead of having to 864 | * recirculate via **redirect**\ (..., **BPF_F_INGRESS**), for 865 | * example. 866 | * 867 | * Note that *type* only allows certain values. At this time, they 868 | * are: 869 | * 870 | * **PACKET_HOST** 871 | * Packet is for us. 872 | * **PACKET_BROADCAST** 873 | * Send packet to all. 874 | * **PACKET_MULTICAST** 875 | * Send packet to group. 876 | * **PACKET_OTHERHOST** 877 | * Send packet to someone else. 878 | * 879 | * Returns 880 | * 0 on success, or a negative error in case of failure. 881 | */ 882 | static long (*bpf_skb_change_type)(struct __sk_buff *skb, __u32 type) = (void *) 32; 883 | 884 | /* 885 | * bpf_skb_under_cgroup 886 | * 887 | * Check whether *skb* is a descendant of the cgroup2 held by 888 | * *map* of type **BPF_MAP_TYPE_CGROUP_ARRAY**, at *index*. 889 | * 890 | * Returns 891 | * The return value depends on the result of the test, and can be: 892 | * 893 | * * 0, if the *skb* failed the cgroup2 descendant test. 894 | * * 1, if the *skb* succeeded the cgroup2 descendant test. 895 | * * A negative error code, if an error occurred. 896 | */ 897 | static long (*bpf_skb_under_cgroup)(struct __sk_buff *skb, void *map, __u32 index) = (void *) 33; 898 | 899 | /* 900 | * bpf_get_hash_recalc 901 | * 902 | * Retrieve the hash of the packet, *skb*\ **->hash**. If it is 903 | * not set, in particular if the hash was cleared due to mangling, 904 | * recompute this hash. Later accesses to the hash can be done 905 | * directly with *skb*\ **->hash**. 906 | * 907 | * Calling **bpf_set_hash_invalid**\ (), changing a packet 908 | * prototype with **bpf_skb_change_proto**\ (), or calling 909 | * **bpf_skb_store_bytes**\ () with the 910 | * **BPF_F_INVALIDATE_HASH** are actions susceptible to clear 911 | * the hash and to trigger a new computation for the next call to 912 | * **bpf_get_hash_recalc**\ (). 913 | * 914 | * Returns 915 | * The 32-bit hash. 916 | */ 917 | static __u32 (*bpf_get_hash_recalc)(struct __sk_buff *skb) = (void *) 34; 918 | 919 | /* 920 | * bpf_get_current_task 921 | * 922 | * 923 | * Returns 924 | * A pointer to the current task struct. 925 | */ 926 | static __u64 (*bpf_get_current_task)(void) = (void *) 35; 927 | 928 | /* 929 | * bpf_probe_write_user 930 | * 931 | * Attempt in a safe way to write *len* bytes from the buffer 932 | * *src* to *dst* in memory. It only works for threads that are in 933 | * user context, and *dst* must be a valid user space address. 934 | * 935 | * This helper should not be used to implement any kind of 936 | * security mechanism because of TOC-TOU attacks, but rather to 937 | * debug, divert, and manipulate execution of semi-cooperative 938 | * processes. 939 | * 940 | * Keep in mind that this feature is meant for experiments, and it 941 | * has a risk of crashing the system and running programs. 942 | * Therefore, when an eBPF program using this helper is attached, 943 | * a warning including PID and process name is printed to kernel 944 | * logs. 945 | * 946 | * Returns 947 | * 0 on success, or a negative error in case of failure. 948 | */ 949 | static long (*bpf_probe_write_user)(void *dst, const void *src, __u32 len) = (void *) 36; 950 | 951 | /* 952 | * bpf_current_task_under_cgroup 953 | * 954 | * Check whether the probe is being run is the context of a given 955 | * subset of the cgroup2 hierarchy. The cgroup2 to test is held by 956 | * *map* of type **BPF_MAP_TYPE_CGROUP_ARRAY**, at *index*. 957 | * 958 | * Returns 959 | * The return value depends on the result of the test, and can be: 960 | * 961 | * * 0, if current task belongs to the cgroup2. 962 | * * 1, if current task does not belong to the cgroup2. 963 | * * A negative error code, if an error occurred. 964 | */ 965 | static long (*bpf_current_task_under_cgroup)(void *map, __u32 index) = (void *) 37; 966 | 967 | /* 968 | * bpf_skb_change_tail 969 | * 970 | * Resize (trim or grow) the packet associated to *skb* to the 971 | * new *len*. The *flags* are reserved for future usage, and must 972 | * be left at zero. 973 | * 974 | * The basic idea is that the helper performs the needed work to 975 | * change the size of the packet, then the eBPF program rewrites 976 | * the rest via helpers like **bpf_skb_store_bytes**\ (), 977 | * **bpf_l3_csum_replace**\ (), **bpf_l3_csum_replace**\ () 978 | * and others. This helper is a slow path utility intended for 979 | * replies with control messages. And because it is targeted for 980 | * slow path, the helper itself can afford to be slow: it 981 | * implicitly linearizes, unclones and drops offloads from the 982 | * *skb*. 983 | * 984 | * A call to this helper is susceptible to change the underlying 985 | * packet buffer. Therefore, at load time, all checks on pointers 986 | * previously done by the verifier are invalidated and must be 987 | * performed again, if the helper is used in combination with 988 | * direct packet access. 989 | * 990 | * Returns 991 | * 0 on success, or a negative error in case of failure. 992 | */ 993 | static long (*bpf_skb_change_tail)(struct __sk_buff *skb, __u32 len, __u64 flags) = (void *) 38; 994 | 995 | /* 996 | * bpf_skb_pull_data 997 | * 998 | * Pull in non-linear data in case the *skb* is non-linear and not 999 | * all of *len* are part of the linear section. Make *len* bytes 1000 | * from *skb* readable and writable. If a zero value is passed for 1001 | * *len*, then the whole length of the *skb* is pulled. 1002 | * 1003 | * This helper is only needed for reading and writing with direct 1004 | * packet access. 1005 | * 1006 | * For direct packet access, testing that offsets to access 1007 | * are within packet boundaries (test on *skb*\ **->data_end**) is 1008 | * susceptible to fail if offsets are invalid, or if the requested 1009 | * data is in non-linear parts of the *skb*. On failure the 1010 | * program can just bail out, or in the case of a non-linear 1011 | * buffer, use a helper to make the data available. The 1012 | * **bpf_skb_load_bytes**\ () helper is a first solution to access 1013 | * the data. Another one consists in using **bpf_skb_pull_data** 1014 | * to pull in once the non-linear parts, then retesting and 1015 | * eventually access the data. 1016 | * 1017 | * At the same time, this also makes sure the *skb* is uncloned, 1018 | * which is a necessary condition for direct write. As this needs 1019 | * to be an invariant for the write part only, the verifier 1020 | * detects writes and adds a prologue that is calling 1021 | * **bpf_skb_pull_data()** to effectively unclone the *skb* from 1022 | * the very beginning in case it is indeed cloned. 1023 | * 1024 | * A call to this helper is susceptible to change the underlying 1025 | * packet buffer. Therefore, at load time, all checks on pointers 1026 | * previously done by the verifier are invalidated and must be 1027 | * performed again, if the helper is used in combination with 1028 | * direct packet access. 1029 | * 1030 | * Returns 1031 | * 0 on success, or a negative error in case of failure. 1032 | */ 1033 | static long (*bpf_skb_pull_data)(struct __sk_buff *skb, __u32 len) = (void *) 39; 1034 | 1035 | /* 1036 | * bpf_csum_update 1037 | * 1038 | * Add the checksum *csum* into *skb*\ **->csum** in case the 1039 | * driver has supplied a checksum for the entire packet into that 1040 | * field. Return an error otherwise. This helper is intended to be 1041 | * used in combination with **bpf_csum_diff**\ (), in particular 1042 | * when the checksum needs to be updated after data has been 1043 | * written into the packet through direct packet access. 1044 | * 1045 | * Returns 1046 | * The checksum on success, or a negative error code in case of 1047 | * failure. 1048 | */ 1049 | static __s64 (*bpf_csum_update)(struct __sk_buff *skb, __wsum csum) = (void *) 40; 1050 | 1051 | /* 1052 | * bpf_set_hash_invalid 1053 | * 1054 | * Invalidate the current *skb*\ **->hash**. It can be used after 1055 | * mangling on headers through direct packet access, in order to 1056 | * indicate that the hash is outdated and to trigger a 1057 | * recalculation the next time the kernel tries to access this 1058 | * hash or when the **bpf_get_hash_recalc**\ () helper is called. 1059 | * 1060 | */ 1061 | static void (*bpf_set_hash_invalid)(struct __sk_buff *skb) = (void *) 41; 1062 | 1063 | /* 1064 | * bpf_get_numa_node_id 1065 | * 1066 | * Return the id of the current NUMA node. The primary use case 1067 | * for this helper is the selection of sockets for the local NUMA 1068 | * node, when the program is attached to sockets using the 1069 | * **SO_ATTACH_REUSEPORT_EBPF** option (see also **socket(7)**), 1070 | * but the helper is also available to other eBPF program types, 1071 | * similarly to **bpf_get_smp_processor_id**\ (). 1072 | * 1073 | * Returns 1074 | * The id of current NUMA node. 1075 | */ 1076 | static long (*bpf_get_numa_node_id)(void) = (void *) 42; 1077 | 1078 | /* 1079 | * bpf_skb_change_head 1080 | * 1081 | * Grows headroom of packet associated to *skb* and adjusts the 1082 | * offset of the MAC header accordingly, adding *len* bytes of 1083 | * space. It automatically extends and reallocates memory as 1084 | * required. 1085 | * 1086 | * This helper can be used on a layer 3 *skb* to push a MAC header 1087 | * for redirection into a layer 2 device. 1088 | * 1089 | * All values for *flags* are reserved for future usage, and must 1090 | * be left at zero. 1091 | * 1092 | * A call to this helper is susceptible to change the underlying 1093 | * packet buffer. Therefore, at load time, all checks on pointers 1094 | * previously done by the verifier are invalidated and must be 1095 | * performed again, if the helper is used in combination with 1096 | * direct packet access. 1097 | * 1098 | * Returns 1099 | * 0 on success, or a negative error in case of failure. 1100 | */ 1101 | static long (*bpf_skb_change_head)(struct __sk_buff *skb, __u32 len, __u64 flags) = (void *) 43; 1102 | 1103 | /* 1104 | * bpf_xdp_adjust_head 1105 | * 1106 | * Adjust (move) *xdp_md*\ **->data** by *delta* bytes. Note that 1107 | * it is possible to use a negative value for *delta*. This helper 1108 | * can be used to prepare the packet for pushing or popping 1109 | * headers. 1110 | * 1111 | * A call to this helper is susceptible to change the underlying 1112 | * packet buffer. Therefore, at load time, all checks on pointers 1113 | * previously done by the verifier are invalidated and must be 1114 | * performed again, if the helper is used in combination with 1115 | * direct packet access. 1116 | * 1117 | * Returns 1118 | * 0 on success, or a negative error in case of failure. 1119 | */ 1120 | static long (*bpf_xdp_adjust_head)(struct xdp_md *xdp_md, int delta) = (void *) 44; 1121 | 1122 | /* 1123 | * bpf_probe_read_str 1124 | * 1125 | * Copy a NUL terminated string from an unsafe kernel address 1126 | * *unsafe_ptr* to *dst*. See **bpf_probe_read_kernel_str**\ () for 1127 | * more details. 1128 | * 1129 | * Generally, use **bpf_probe_read_user_str**\ () or 1130 | * **bpf_probe_read_kernel_str**\ () instead. 1131 | * 1132 | * Returns 1133 | * On success, the strictly positive length of the string, 1134 | * including the trailing NUL character. On error, a negative 1135 | * value. 1136 | */ 1137 | static long (*bpf_probe_read_str)(void *dst, __u32 size, const void *unsafe_ptr) = (void *) 45; 1138 | 1139 | /* 1140 | * bpf_get_socket_cookie 1141 | * 1142 | * If the **struct sk_buff** pointed by *skb* has a known socket, 1143 | * retrieve the cookie (generated by the kernel) of this socket. 1144 | * If no cookie has been set yet, generate a new cookie. Once 1145 | * generated, the socket cookie remains stable for the life of the 1146 | * socket. This helper can be useful for monitoring per socket 1147 | * networking traffic statistics as it provides a global socket 1148 | * identifier that can be assumed unique. 1149 | * 1150 | * Returns 1151 | * A 8-byte long unique number on success, or 0 if the socket 1152 | * field is missing inside *skb*. 1153 | */ 1154 | static __u64 (*bpf_get_socket_cookie)(void *ctx) = (void *) 46; 1155 | 1156 | /* 1157 | * bpf_get_socket_uid 1158 | * 1159 | * 1160 | * Returns 1161 | * The owner UID of the socket associated to *skb*. If the socket 1162 | * is **NULL**, or if it is not a full socket (i.e. if it is a 1163 | * time-wait or a request socket instead), **overflowuid** value 1164 | * is returned (note that **overflowuid** might also be the actual 1165 | * UID value for the socket). 1166 | */ 1167 | static __u32 (*bpf_get_socket_uid)(struct __sk_buff *skb) = (void *) 47; 1168 | 1169 | /* 1170 | * bpf_set_hash 1171 | * 1172 | * Set the full hash for *skb* (set the field *skb*\ **->hash**) 1173 | * to value *hash*. 1174 | * 1175 | * Returns 1176 | * 0 1177 | */ 1178 | static long (*bpf_set_hash)(struct __sk_buff *skb, __u32 hash) = (void *) 48; 1179 | 1180 | /* 1181 | * bpf_setsockopt 1182 | * 1183 | * Emulate a call to **setsockopt()** on the socket associated to 1184 | * *bpf_socket*, which must be a full socket. The *level* at 1185 | * which the option resides and the name *optname* of the option 1186 | * must be specified, see **setsockopt(2)** for more information. 1187 | * The option value of length *optlen* is pointed by *optval*. 1188 | * 1189 | * *bpf_socket* should be one of the following: 1190 | * 1191 | * * **struct bpf_sock_ops** for **BPF_PROG_TYPE_SOCK_OPS**. 1192 | * * **struct bpf_sock_addr** for **BPF_CGROUP_INET4_CONNECT** 1193 | * and **BPF_CGROUP_INET6_CONNECT**. 1194 | * 1195 | * This helper actually implements a subset of **setsockopt()**. 1196 | * It supports the following *level*\ s: 1197 | * 1198 | * * **SOL_SOCKET**, which supports the following *optname*\ s: 1199 | * **SO_RCVBUF**, **SO_SNDBUF**, **SO_MAX_PACING_RATE**, 1200 | * **SO_PRIORITY**, **SO_RCVLOWAT**, **SO_MARK**, 1201 | * **SO_BINDTODEVICE**, **SO_KEEPALIVE**. 1202 | * * **IPPROTO_TCP**, which supports the following *optname*\ s: 1203 | * **TCP_CONGESTION**, **TCP_BPF_IW**, 1204 | * **TCP_BPF_SNDCWND_CLAMP**, **TCP_SAVE_SYN**, 1205 | * **TCP_KEEPIDLE**, **TCP_KEEPINTVL**, **TCP_KEEPCNT**, 1206 | * **TCP_SYNCNT**, **TCP_USER_TIMEOUT**, **TCP_NOTSENT_LOWAT**. 1207 | * * **IPPROTO_IP**, which supports *optname* **IP_TOS**. 1208 | * * **IPPROTO_IPV6**, which supports *optname* **IPV6_TCLASS**. 1209 | * 1210 | * Returns 1211 | * 0 on success, or a negative error in case of failure. 1212 | */ 1213 | static long (*bpf_setsockopt)(void *bpf_socket, int level, int optname, void *optval, int optlen) = (void *) 49; 1214 | 1215 | /* 1216 | * bpf_skb_adjust_room 1217 | * 1218 | * Grow or shrink the room for data in the packet associated to 1219 | * *skb* by *len_diff*, and according to the selected *mode*. 1220 | * 1221 | * By default, the helper will reset any offloaded checksum 1222 | * indicator of the skb to CHECKSUM_NONE. This can be avoided 1223 | * by the following flag: 1224 | * 1225 | * * **BPF_F_ADJ_ROOM_NO_CSUM_RESET**: Do not reset offloaded 1226 | * checksum data of the skb to CHECKSUM_NONE. 1227 | * 1228 | * There are two supported modes at this time: 1229 | * 1230 | * * **BPF_ADJ_ROOM_MAC**: Adjust room at the mac layer 1231 | * (room space is added or removed below the layer 2 header). 1232 | * 1233 | * * **BPF_ADJ_ROOM_NET**: Adjust room at the network layer 1234 | * (room space is added or removed below the layer 3 header). 1235 | * 1236 | * The following flags are supported at this time: 1237 | * 1238 | * * **BPF_F_ADJ_ROOM_FIXED_GSO**: Do not adjust gso_size. 1239 | * Adjusting mss in this way is not allowed for datagrams. 1240 | * 1241 | * * **BPF_F_ADJ_ROOM_ENCAP_L3_IPV4**, 1242 | * **BPF_F_ADJ_ROOM_ENCAP_L3_IPV6**: 1243 | * Any new space is reserved to hold a tunnel header. 1244 | * Configure skb offsets and other fields accordingly. 1245 | * 1246 | * * **BPF_F_ADJ_ROOM_ENCAP_L4_GRE**, 1247 | * **BPF_F_ADJ_ROOM_ENCAP_L4_UDP**: 1248 | * Use with ENCAP_L3 flags to further specify the tunnel type. 1249 | * 1250 | * * **BPF_F_ADJ_ROOM_ENCAP_L2**\ (*len*): 1251 | * Use with ENCAP_L3/L4 flags to further specify the tunnel 1252 | * type; *len* is the length of the inner MAC header. 1253 | * 1254 | * * **BPF_F_ADJ_ROOM_ENCAP_L2_ETH**: 1255 | * Use with BPF_F_ADJ_ROOM_ENCAP_L2 flag to further specify the 1256 | * L2 type as Ethernet. 1257 | * 1258 | * A call to this helper is susceptible to change the underlying 1259 | * packet buffer. Therefore, at load time, all checks on pointers 1260 | * previously done by the verifier are invalidated and must be 1261 | * performed again, if the helper is used in combination with 1262 | * direct packet access. 1263 | * 1264 | * Returns 1265 | * 0 on success, or a negative error in case of failure. 1266 | */ 1267 | static long (*bpf_skb_adjust_room)(struct __sk_buff *skb, __s32 len_diff, __u32 mode, __u64 flags) = (void *) 50; 1268 | 1269 | /* 1270 | * bpf_redirect_map 1271 | * 1272 | * Redirect the packet to the endpoint referenced by *map* at 1273 | * index *key*. Depending on its type, this *map* can contain 1274 | * references to net devices (for forwarding packets through other 1275 | * ports), or to CPUs (for redirecting XDP frames to another CPU; 1276 | * but this is only implemented for native XDP (with driver 1277 | * support) as of this writing). 1278 | * 1279 | * The lower two bits of *flags* are used as the return code if 1280 | * the map lookup fails. This is so that the return value can be 1281 | * one of the XDP program return codes up to **XDP_TX**, as chosen 1282 | * by the caller. The higher bits of *flags* can be set to 1283 | * BPF_F_BROADCAST or BPF_F_EXCLUDE_INGRESS as defined below. 1284 | * 1285 | * With BPF_F_BROADCAST the packet will be broadcasted to all the 1286 | * interfaces in the map, with BPF_F_EXCLUDE_INGRESS the ingress 1287 | * interface will be excluded when do broadcasting. 1288 | * 1289 | * See also **bpf_redirect**\ (), which only supports redirecting 1290 | * to an ifindex, but doesn't require a map to do so. 1291 | * 1292 | * Returns 1293 | * **XDP_REDIRECT** on success, or the value of the two lower bits 1294 | * of the *flags* argument on error. 1295 | */ 1296 | static long (*bpf_redirect_map)(void *map, __u32 key, __u64 flags) = (void *) 51; 1297 | 1298 | /* 1299 | * bpf_sk_redirect_map 1300 | * 1301 | * Redirect the packet to the socket referenced by *map* (of type 1302 | * **BPF_MAP_TYPE_SOCKMAP**) at index *key*. Both ingress and 1303 | * egress interfaces can be used for redirection. The 1304 | * **BPF_F_INGRESS** value in *flags* is used to make the 1305 | * distinction (ingress path is selected if the flag is present, 1306 | * egress path otherwise). This is the only flag supported for now. 1307 | * 1308 | * Returns 1309 | * **SK_PASS** on success, or **SK_DROP** on error. 1310 | */ 1311 | static long (*bpf_sk_redirect_map)(struct __sk_buff *skb, void *map, __u32 key, __u64 flags) = (void *) 52; 1312 | 1313 | /* 1314 | * bpf_sock_map_update 1315 | * 1316 | * Add an entry to, or update a *map* referencing sockets. The 1317 | * *skops* is used as a new value for the entry associated to 1318 | * *key*. *flags* is one of: 1319 | * 1320 | * **BPF_NOEXIST** 1321 | * The entry for *key* must not exist in the map. 1322 | * **BPF_EXIST** 1323 | * The entry for *key* must already exist in the map. 1324 | * **BPF_ANY** 1325 | * No condition on the existence of the entry for *key*. 1326 | * 1327 | * If the *map* has eBPF programs (parser and verdict), those will 1328 | * be inherited by the socket being added. If the socket is 1329 | * already attached to eBPF programs, this results in an error. 1330 | * 1331 | * Returns 1332 | * 0 on success, or a negative error in case of failure. 1333 | */ 1334 | static long (*bpf_sock_map_update)(struct bpf_sock_ops *skops, void *map, void *key, __u64 flags) = (void *) 53; 1335 | 1336 | /* 1337 | * bpf_xdp_adjust_meta 1338 | * 1339 | * Adjust the address pointed by *xdp_md*\ **->data_meta** by 1340 | * *delta* (which can be positive or negative). Note that this 1341 | * operation modifies the address stored in *xdp_md*\ **->data**, 1342 | * so the latter must be loaded only after the helper has been 1343 | * called. 1344 | * 1345 | * The use of *xdp_md*\ **->data_meta** is optional and programs 1346 | * are not required to use it. The rationale is that when the 1347 | * packet is processed with XDP (e.g. as DoS filter), it is 1348 | * possible to push further meta data along with it before passing 1349 | * to the stack, and to give the guarantee that an ingress eBPF 1350 | * program attached as a TC classifier on the same device can pick 1351 | * this up for further post-processing. Since TC works with socket 1352 | * buffers, it remains possible to set from XDP the **mark** or 1353 | * **priority** pointers, or other pointers for the socket buffer. 1354 | * Having this scratch space generic and programmable allows for 1355 | * more flexibility as the user is free to store whatever meta 1356 | * data they need. 1357 | * 1358 | * A call to this helper is susceptible to change the underlying 1359 | * packet buffer. Therefore, at load time, all checks on pointers 1360 | * previously done by the verifier are invalidated and must be 1361 | * performed again, if the helper is used in combination with 1362 | * direct packet access. 1363 | * 1364 | * Returns 1365 | * 0 on success, or a negative error in case of failure. 1366 | */ 1367 | static long (*bpf_xdp_adjust_meta)(struct xdp_md *xdp_md, int delta) = (void *) 54; 1368 | 1369 | /* 1370 | * bpf_perf_event_read_value 1371 | * 1372 | * Read the value of a perf event counter, and store it into *buf* 1373 | * of size *buf_size*. This helper relies on a *map* of type 1374 | * **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. The nature of the perf event 1375 | * counter is selected when *map* is updated with perf event file 1376 | * descriptors. The *map* is an array whose size is the number of 1377 | * available CPUs, and each cell contains a value relative to one 1378 | * CPU. The value to retrieve is indicated by *flags*, that 1379 | * contains the index of the CPU to look up, masked with 1380 | * **BPF_F_INDEX_MASK**. Alternatively, *flags* can be set to 1381 | * **BPF_F_CURRENT_CPU** to indicate that the value for the 1382 | * current CPU should be retrieved. 1383 | * 1384 | * This helper behaves in a way close to 1385 | * **bpf_perf_event_read**\ () helper, save that instead of 1386 | * just returning the value observed, it fills the *buf* 1387 | * structure. This allows for additional data to be retrieved: in 1388 | * particular, the enabled and running times (in *buf*\ 1389 | * **->enabled** and *buf*\ **->running**, respectively) are 1390 | * copied. In general, **bpf_perf_event_read_value**\ () is 1391 | * recommended over **bpf_perf_event_read**\ (), which has some 1392 | * ABI issues and provides fewer functionalities. 1393 | * 1394 | * These values are interesting, because hardware PMU (Performance 1395 | * Monitoring Unit) counters are limited resources. When there are 1396 | * more PMU based perf events opened than available counters, 1397 | * kernel will multiplex these events so each event gets certain 1398 | * percentage (but not all) of the PMU time. In case that 1399 | * multiplexing happens, the number of samples or counter value 1400 | * will not reflect the case compared to when no multiplexing 1401 | * occurs. This makes comparison between different runs difficult. 1402 | * Typically, the counter value should be normalized before 1403 | * comparing to other experiments. The usual normalization is done 1404 | * as follows. 1405 | * 1406 | * :: 1407 | * 1408 | * normalized_counter = counter * t_enabled / t_running 1409 | * 1410 | * Where t_enabled is the time enabled for event and t_running is 1411 | * the time running for event since last normalization. The 1412 | * enabled and running times are accumulated since the perf event 1413 | * open. To achieve scaling factor between two invocations of an 1414 | * eBPF program, users can use CPU id as the key (which is 1415 | * typical for perf array usage model) to remember the previous 1416 | * value and do the calculation inside the eBPF program. 1417 | * 1418 | * Returns 1419 | * 0 on success, or a negative error in case of failure. 1420 | */ 1421 | static long (*bpf_perf_event_read_value)(void *map, __u64 flags, struct bpf_perf_event_value *buf, __u32 buf_size) = (void *) 55; 1422 | 1423 | /* 1424 | * bpf_perf_prog_read_value 1425 | * 1426 | * For en eBPF program attached to a perf event, retrieve the 1427 | * value of the event counter associated to *ctx* and store it in 1428 | * the structure pointed by *buf* and of size *buf_size*. Enabled 1429 | * and running times are also stored in the structure (see 1430 | * description of helper **bpf_perf_event_read_value**\ () for 1431 | * more details). 1432 | * 1433 | * Returns 1434 | * 0 on success, or a negative error in case of failure. 1435 | */ 1436 | static long (*bpf_perf_prog_read_value)(struct bpf_perf_event_data *ctx, struct bpf_perf_event_value *buf, __u32 buf_size) = (void *) 56; 1437 | 1438 | /* 1439 | * bpf_getsockopt 1440 | * 1441 | * Emulate a call to **getsockopt()** on the socket associated to 1442 | * *bpf_socket*, which must be a full socket. The *level* at 1443 | * which the option resides and the name *optname* of the option 1444 | * must be specified, see **getsockopt(2)** for more information. 1445 | * The retrieved value is stored in the structure pointed by 1446 | * *opval* and of length *optlen*. 1447 | * 1448 | * *bpf_socket* should be one of the following: 1449 | * 1450 | * * **struct bpf_sock_ops** for **BPF_PROG_TYPE_SOCK_OPS**. 1451 | * * **struct bpf_sock_addr** for **BPF_CGROUP_INET4_CONNECT** 1452 | * and **BPF_CGROUP_INET6_CONNECT**. 1453 | * 1454 | * This helper actually implements a subset of **getsockopt()**. 1455 | * It supports the following *level*\ s: 1456 | * 1457 | * * **IPPROTO_TCP**, which supports *optname* 1458 | * **TCP_CONGESTION**. 1459 | * * **IPPROTO_IP**, which supports *optname* **IP_TOS**. 1460 | * * **IPPROTO_IPV6**, which supports *optname* **IPV6_TCLASS**. 1461 | * 1462 | * Returns 1463 | * 0 on success, or a negative error in case of failure. 1464 | */ 1465 | static long (*bpf_getsockopt)(void *bpf_socket, int level, int optname, void *optval, int optlen) = (void *) 57; 1466 | 1467 | /* 1468 | * bpf_override_return 1469 | * 1470 | * Used for error injection, this helper uses kprobes to override 1471 | * the return value of the probed function, and to set it to *rc*. 1472 | * The first argument is the context *regs* on which the kprobe 1473 | * works. 1474 | * 1475 | * This helper works by setting the PC (program counter) 1476 | * to an override function which is run in place of the original 1477 | * probed function. This means the probed function is not run at 1478 | * all. The replacement function just returns with the required 1479 | * value. 1480 | * 1481 | * This helper has security implications, and thus is subject to 1482 | * restrictions. It is only available if the kernel was compiled 1483 | * with the **CONFIG_BPF_KPROBE_OVERRIDE** configuration 1484 | * option, and in this case it only works on functions tagged with 1485 | * **ALLOW_ERROR_INJECTION** in the kernel code. 1486 | * 1487 | * Also, the helper is only available for the architectures having 1488 | * the CONFIG_FUNCTION_ERROR_INJECTION option. As of this writing, 1489 | * x86 architecture is the only one to support this feature. 1490 | * 1491 | * Returns 1492 | * 0 1493 | */ 1494 | static long (*bpf_override_return)(struct pt_regs *regs, __u64 rc) = (void *) 58; 1495 | 1496 | /* 1497 | * bpf_sock_ops_cb_flags_set 1498 | * 1499 | * Attempt to set the value of the **bpf_sock_ops_cb_flags** field 1500 | * for the full TCP socket associated to *bpf_sock_ops* to 1501 | * *argval*. 1502 | * 1503 | * The primary use of this field is to determine if there should 1504 | * be calls to eBPF programs of type 1505 | * **BPF_PROG_TYPE_SOCK_OPS** at various points in the TCP 1506 | * code. A program of the same type can change its value, per 1507 | * connection and as necessary, when the connection is 1508 | * established. This field is directly accessible for reading, but 1509 | * this helper must be used for updates in order to return an 1510 | * error if an eBPF program tries to set a callback that is not 1511 | * supported in the current kernel. 1512 | * 1513 | * *argval* is a flag array which can combine these flags: 1514 | * 1515 | * * **BPF_SOCK_OPS_RTO_CB_FLAG** (retransmission time out) 1516 | * * **BPF_SOCK_OPS_RETRANS_CB_FLAG** (retransmission) 1517 | * * **BPF_SOCK_OPS_STATE_CB_FLAG** (TCP state change) 1518 | * * **BPF_SOCK_OPS_RTT_CB_FLAG** (every RTT) 1519 | * 1520 | * Therefore, this function can be used to clear a callback flag by 1521 | * setting the appropriate bit to zero. e.g. to disable the RTO 1522 | * callback: 1523 | * 1524 | * **bpf_sock_ops_cb_flags_set(bpf_sock,** 1525 | * **bpf_sock->bpf_sock_ops_cb_flags & ~BPF_SOCK_OPS_RTO_CB_FLAG)** 1526 | * 1527 | * Here are some examples of where one could call such eBPF 1528 | * program: 1529 | * 1530 | * * When RTO fires. 1531 | * * When a packet is retransmitted. 1532 | * * When the connection terminates. 1533 | * * When a packet is sent. 1534 | * * When a packet is received. 1535 | * 1536 | * Returns 1537 | * Code **-EINVAL** if the socket is not a full TCP socket; 1538 | * otherwise, a positive number containing the bits that could not 1539 | * be set is returned (which comes down to 0 if all bits were set 1540 | * as required). 1541 | */ 1542 | static long (*bpf_sock_ops_cb_flags_set)(struct bpf_sock_ops *bpf_sock, int argval) = (void *) 59; 1543 | 1544 | /* 1545 | * bpf_msg_redirect_map 1546 | * 1547 | * This helper is used in programs implementing policies at the 1548 | * socket level. If the message *msg* is allowed to pass (i.e. if 1549 | * the verdict eBPF program returns **SK_PASS**), redirect it to 1550 | * the socket referenced by *map* (of type 1551 | * **BPF_MAP_TYPE_SOCKMAP**) at index *key*. Both ingress and 1552 | * egress interfaces can be used for redirection. The 1553 | * **BPF_F_INGRESS** value in *flags* is used to make the 1554 | * distinction (ingress path is selected if the flag is present, 1555 | * egress path otherwise). This is the only flag supported for now. 1556 | * 1557 | * Returns 1558 | * **SK_PASS** on success, or **SK_DROP** on error. 1559 | */ 1560 | static long (*bpf_msg_redirect_map)(struct sk_msg_md *msg, void *map, __u32 key, __u64 flags) = (void *) 60; 1561 | 1562 | /* 1563 | * bpf_msg_apply_bytes 1564 | * 1565 | * For socket policies, apply the verdict of the eBPF program to 1566 | * the next *bytes* (number of bytes) of message *msg*. 1567 | * 1568 | * For example, this helper can be used in the following cases: 1569 | * 1570 | * * A single **sendmsg**\ () or **sendfile**\ () system call 1571 | * contains multiple logical messages that the eBPF program is 1572 | * supposed to read and for which it should apply a verdict. 1573 | * * An eBPF program only cares to read the first *bytes* of a 1574 | * *msg*. If the message has a large payload, then setting up 1575 | * and calling the eBPF program repeatedly for all bytes, even 1576 | * though the verdict is already known, would create unnecessary 1577 | * overhead. 1578 | * 1579 | * When called from within an eBPF program, the helper sets a 1580 | * counter internal to the BPF infrastructure, that is used to 1581 | * apply the last verdict to the next *bytes*. If *bytes* is 1582 | * smaller than the current data being processed from a 1583 | * **sendmsg**\ () or **sendfile**\ () system call, the first 1584 | * *bytes* will be sent and the eBPF program will be re-run with 1585 | * the pointer for start of data pointing to byte number *bytes* 1586 | * **+ 1**. If *bytes* is larger than the current data being 1587 | * processed, then the eBPF verdict will be applied to multiple 1588 | * **sendmsg**\ () or **sendfile**\ () calls until *bytes* are 1589 | * consumed. 1590 | * 1591 | * Note that if a socket closes with the internal counter holding 1592 | * a non-zero value, this is not a problem because data is not 1593 | * being buffered for *bytes* and is sent as it is received. 1594 | * 1595 | * Returns 1596 | * 0 1597 | */ 1598 | static long (*bpf_msg_apply_bytes)(struct sk_msg_md *msg, __u32 bytes) = (void *) 61; 1599 | 1600 | /* 1601 | * bpf_msg_cork_bytes 1602 | * 1603 | * For socket policies, prevent the execution of the verdict eBPF 1604 | * program for message *msg* until *bytes* (byte number) have been 1605 | * accumulated. 1606 | * 1607 | * This can be used when one needs a specific number of bytes 1608 | * before a verdict can be assigned, even if the data spans 1609 | * multiple **sendmsg**\ () or **sendfile**\ () calls. The extreme 1610 | * case would be a user calling **sendmsg**\ () repeatedly with 1611 | * 1-byte long message segments. Obviously, this is bad for 1612 | * performance, but it is still valid. If the eBPF program needs 1613 | * *bytes* bytes to validate a header, this helper can be used to 1614 | * prevent the eBPF program to be called again until *bytes* have 1615 | * been accumulated. 1616 | * 1617 | * Returns 1618 | * 0 1619 | */ 1620 | static long (*bpf_msg_cork_bytes)(struct sk_msg_md *msg, __u32 bytes) = (void *) 62; 1621 | 1622 | /* 1623 | * bpf_msg_pull_data 1624 | * 1625 | * For socket policies, pull in non-linear data from user space 1626 | * for *msg* and set pointers *msg*\ **->data** and *msg*\ 1627 | * **->data_end** to *start* and *end* bytes offsets into *msg*, 1628 | * respectively. 1629 | * 1630 | * If a program of type **BPF_PROG_TYPE_SK_MSG** is run on a 1631 | * *msg* it can only parse data that the (**data**, **data_end**) 1632 | * pointers have already consumed. For **sendmsg**\ () hooks this 1633 | * is likely the first scatterlist element. But for calls relying 1634 | * on the **sendpage** handler (e.g. **sendfile**\ ()) this will 1635 | * be the range (**0**, **0**) because the data is shared with 1636 | * user space and by default the objective is to avoid allowing 1637 | * user space to modify data while (or after) eBPF verdict is 1638 | * being decided. This helper can be used to pull in data and to 1639 | * set the start and end pointer to given values. Data will be 1640 | * copied if necessary (i.e. if data was not linear and if start 1641 | * and end pointers do not point to the same chunk). 1642 | * 1643 | * A call to this helper is susceptible to change the underlying 1644 | * packet buffer. Therefore, at load time, all checks on pointers 1645 | * previously done by the verifier are invalidated and must be 1646 | * performed again, if the helper is used in combination with 1647 | * direct packet access. 1648 | * 1649 | * All values for *flags* are reserved for future usage, and must 1650 | * be left at zero. 1651 | * 1652 | * Returns 1653 | * 0 on success, or a negative error in case of failure. 1654 | */ 1655 | static long (*bpf_msg_pull_data)(struct sk_msg_md *msg, __u32 start, __u32 end, __u64 flags) = (void *) 63; 1656 | 1657 | /* 1658 | * bpf_bind 1659 | * 1660 | * Bind the socket associated to *ctx* to the address pointed by 1661 | * *addr*, of length *addr_len*. This allows for making outgoing 1662 | * connection from the desired IP address, which can be useful for 1663 | * example when all processes inside a cgroup should use one 1664 | * single IP address on a host that has multiple IP configured. 1665 | * 1666 | * This helper works for IPv4 and IPv6, TCP and UDP sockets. The 1667 | * domain (*addr*\ **->sa_family**) must be **AF_INET** (or 1668 | * **AF_INET6**). It's advised to pass zero port (**sin_port** 1669 | * or **sin6_port**) which triggers IP_BIND_ADDRESS_NO_PORT-like 1670 | * behavior and lets the kernel efficiently pick up an unused 1671 | * port as long as 4-tuple is unique. Passing non-zero port might 1672 | * lead to degraded performance. 1673 | * 1674 | * Returns 1675 | * 0 on success, or a negative error in case of failure. 1676 | */ 1677 | static long (*bpf_bind)(struct bpf_sock_addr *ctx, struct sockaddr *addr, int addr_len) = (void *) 64; 1678 | 1679 | /* 1680 | * bpf_xdp_adjust_tail 1681 | * 1682 | * Adjust (move) *xdp_md*\ **->data_end** by *delta* bytes. It is 1683 | * possible to both shrink and grow the packet tail. 1684 | * Shrink done via *delta* being a negative integer. 1685 | * 1686 | * A call to this helper is susceptible to change the underlying 1687 | * packet buffer. Therefore, at load time, all checks on pointers 1688 | * previously done by the verifier are invalidated and must be 1689 | * performed again, if the helper is used in combination with 1690 | * direct packet access. 1691 | * 1692 | * Returns 1693 | * 0 on success, or a negative error in case of failure. 1694 | */ 1695 | static long (*bpf_xdp_adjust_tail)(struct xdp_md *xdp_md, int delta) = (void *) 65; 1696 | 1697 | /* 1698 | * bpf_skb_get_xfrm_state 1699 | * 1700 | * Retrieve the XFRM state (IP transform framework, see also 1701 | * **ip-xfrm(8)**) at *index* in XFRM "security path" for *skb*. 1702 | * 1703 | * The retrieved value is stored in the **struct bpf_xfrm_state** 1704 | * pointed by *xfrm_state* and of length *size*. 1705 | * 1706 | * All values for *flags* are reserved for future usage, and must 1707 | * be left at zero. 1708 | * 1709 | * This helper is available only if the kernel was compiled with 1710 | * **CONFIG_XFRM** configuration option. 1711 | * 1712 | * Returns 1713 | * 0 on success, or a negative error in case of failure. 1714 | */ 1715 | static long (*bpf_skb_get_xfrm_state)(struct __sk_buff *skb, __u32 index, struct bpf_xfrm_state *xfrm_state, __u32 size, __u64 flags) = (void *) 66; 1716 | 1717 | /* 1718 | * bpf_get_stack 1719 | * 1720 | * Return a user or a kernel stack in bpf program provided buffer. 1721 | * To achieve this, the helper needs *ctx*, which is a pointer 1722 | * to the context on which the tracing program is executed. 1723 | * To store the stacktrace, the bpf program provides *buf* with 1724 | * a nonnegative *size*. 1725 | * 1726 | * The last argument, *flags*, holds the number of stack frames to 1727 | * skip (from 0 to 255), masked with 1728 | * **BPF_F_SKIP_FIELD_MASK**. The next bits can be used to set 1729 | * the following flags: 1730 | * 1731 | * **BPF_F_USER_STACK** 1732 | * Collect a user space stack instead of a kernel stack. 1733 | * **BPF_F_USER_BUILD_ID** 1734 | * Collect buildid+offset instead of ips for user stack, 1735 | * only valid if **BPF_F_USER_STACK** is also specified. 1736 | * 1737 | * **bpf_get_stack**\ () can collect up to 1738 | * **PERF_MAX_STACK_DEPTH** both kernel and user frames, subject 1739 | * to sufficient large buffer size. Note that 1740 | * this limit can be controlled with the **sysctl** program, and 1741 | * that it should be manually increased in order to profile long 1742 | * user stacks (such as stacks for Java programs). To do so, use: 1743 | * 1744 | * :: 1745 | * 1746 | * # sysctl kernel.perf_event_max_stack= 1747 | * 1748 | * Returns 1749 | * A non-negative value equal to or less than *size* on success, 1750 | * or a negative error in case of failure. 1751 | */ 1752 | static long (*bpf_get_stack)(void *ctx, void *buf, __u32 size, __u64 flags) = (void *) 67; 1753 | 1754 | /* 1755 | * bpf_skb_load_bytes_relative 1756 | * 1757 | * This helper is similar to **bpf_skb_load_bytes**\ () in that 1758 | * it provides an easy way to load *len* bytes from *offset* 1759 | * from the packet associated to *skb*, into the buffer pointed 1760 | * by *to*. The difference to **bpf_skb_load_bytes**\ () is that 1761 | * a fifth argument *start_header* exists in order to select a 1762 | * base offset to start from. *start_header* can be one of: 1763 | * 1764 | * **BPF_HDR_START_MAC** 1765 | * Base offset to load data from is *skb*'s mac header. 1766 | * **BPF_HDR_START_NET** 1767 | * Base offset to load data from is *skb*'s network header. 1768 | * 1769 | * In general, "direct packet access" is the preferred method to 1770 | * access packet data, however, this helper is in particular useful 1771 | * in socket filters where *skb*\ **->data** does not always point 1772 | * to the start of the mac header and where "direct packet access" 1773 | * is not available. 1774 | * 1775 | * Returns 1776 | * 0 on success, or a negative error in case of failure. 1777 | */ 1778 | static long (*bpf_skb_load_bytes_relative)(const void *skb, __u32 offset, void *to, __u32 len, __u32 start_header) = (void *) 68; 1779 | 1780 | /* 1781 | * bpf_fib_lookup 1782 | * 1783 | * Do FIB lookup in kernel tables using parameters in *params*. 1784 | * If lookup is successful and result shows packet is to be 1785 | * forwarded, the neighbor tables are searched for the nexthop. 1786 | * If successful (ie., FIB lookup shows forwarding and nexthop 1787 | * is resolved), the nexthop address is returned in ipv4_dst 1788 | * or ipv6_dst based on family, smac is set to mac address of 1789 | * egress device, dmac is set to nexthop mac address, rt_metric 1790 | * is set to metric from route (IPv4/IPv6 only), and ifindex 1791 | * is set to the device index of the nexthop from the FIB lookup. 1792 | * 1793 | * *plen* argument is the size of the passed in struct. 1794 | * *flags* argument can be a combination of one or more of the 1795 | * following values: 1796 | * 1797 | * **BPF_FIB_LOOKUP_DIRECT** 1798 | * Do a direct table lookup vs full lookup using FIB 1799 | * rules. 1800 | * **BPF_FIB_LOOKUP_OUTPUT** 1801 | * Perform lookup from an egress perspective (default is 1802 | * ingress). 1803 | * 1804 | * *ctx* is either **struct xdp_md** for XDP programs or 1805 | * **struct sk_buff** tc cls_act programs. 1806 | * 1807 | * Returns 1808 | * * < 0 if any input argument is invalid 1809 | * * 0 on success (packet is forwarded, nexthop neighbor exists) 1810 | * * > 0 one of **BPF_FIB_LKUP_RET_** codes explaining why the 1811 | * packet is not forwarded or needs assist from full stack 1812 | * 1813 | * If lookup fails with BPF_FIB_LKUP_RET_FRAG_NEEDED, then the MTU 1814 | * was exceeded and output params->mtu_result contains the MTU. 1815 | */ 1816 | static long (*bpf_fib_lookup)(void *ctx, struct bpf_fib_lookup *params, int plen, __u32 flags) = (void *) 69; 1817 | 1818 | /* 1819 | * bpf_sock_hash_update 1820 | * 1821 | * Add an entry to, or update a sockhash *map* referencing sockets. 1822 | * The *skops* is used as a new value for the entry associated to 1823 | * *key*. *flags* is one of: 1824 | * 1825 | * **BPF_NOEXIST** 1826 | * The entry for *key* must not exist in the map. 1827 | * **BPF_EXIST** 1828 | * The entry for *key* must already exist in the map. 1829 | * **BPF_ANY** 1830 | * No condition on the existence of the entry for *key*. 1831 | * 1832 | * If the *map* has eBPF programs (parser and verdict), those will 1833 | * be inherited by the socket being added. If the socket is 1834 | * already attached to eBPF programs, this results in an error. 1835 | * 1836 | * Returns 1837 | * 0 on success, or a negative error in case of failure. 1838 | */ 1839 | static long (*bpf_sock_hash_update)(struct bpf_sock_ops *skops, void *map, void *key, __u64 flags) = (void *) 70; 1840 | 1841 | /* 1842 | * bpf_msg_redirect_hash 1843 | * 1844 | * This helper is used in programs implementing policies at the 1845 | * socket level. If the message *msg* is allowed to pass (i.e. if 1846 | * the verdict eBPF program returns **SK_PASS**), redirect it to 1847 | * the socket referenced by *map* (of type 1848 | * **BPF_MAP_TYPE_SOCKHASH**) using hash *key*. Both ingress and 1849 | * egress interfaces can be used for redirection. The 1850 | * **BPF_F_INGRESS** value in *flags* is used to make the 1851 | * distinction (ingress path is selected if the flag is present, 1852 | * egress path otherwise). This is the only flag supported for now. 1853 | * 1854 | * Returns 1855 | * **SK_PASS** on success, or **SK_DROP** on error. 1856 | */ 1857 | static long (*bpf_msg_redirect_hash)(struct sk_msg_md *msg, void *map, void *key, __u64 flags) = (void *) 71; 1858 | 1859 | /* 1860 | * bpf_sk_redirect_hash 1861 | * 1862 | * This helper is used in programs implementing policies at the 1863 | * skb socket level. If the sk_buff *skb* is allowed to pass (i.e. 1864 | * if the verdict eBPF program returns **SK_PASS**), redirect it 1865 | * to the socket referenced by *map* (of type 1866 | * **BPF_MAP_TYPE_SOCKHASH**) using hash *key*. Both ingress and 1867 | * egress interfaces can be used for redirection. The 1868 | * **BPF_F_INGRESS** value in *flags* is used to make the 1869 | * distinction (ingress path is selected if the flag is present, 1870 | * egress otherwise). This is the only flag supported for now. 1871 | * 1872 | * Returns 1873 | * **SK_PASS** on success, or **SK_DROP** on error. 1874 | */ 1875 | static long (*bpf_sk_redirect_hash)(struct __sk_buff *skb, void *map, void *key, __u64 flags) = (void *) 72; 1876 | 1877 | /* 1878 | * bpf_lwt_push_encap 1879 | * 1880 | * Encapsulate the packet associated to *skb* within a Layer 3 1881 | * protocol header. This header is provided in the buffer at 1882 | * address *hdr*, with *len* its size in bytes. *type* indicates 1883 | * the protocol of the header and can be one of: 1884 | * 1885 | * **BPF_LWT_ENCAP_SEG6** 1886 | * IPv6 encapsulation with Segment Routing Header 1887 | * (**struct ipv6_sr_hdr**). *hdr* only contains the SRH, 1888 | * the IPv6 header is computed by the kernel. 1889 | * **BPF_LWT_ENCAP_SEG6_INLINE** 1890 | * Only works if *skb* contains an IPv6 packet. Insert a 1891 | * Segment Routing Header (**struct ipv6_sr_hdr**) inside 1892 | * the IPv6 header. 1893 | * **BPF_LWT_ENCAP_IP** 1894 | * IP encapsulation (GRE/GUE/IPIP/etc). The outer header 1895 | * must be IPv4 or IPv6, followed by zero or more 1896 | * additional headers, up to **LWT_BPF_MAX_HEADROOM** 1897 | * total bytes in all prepended headers. Please note that 1898 | * if **skb_is_gso**\ (*skb*) is true, no more than two 1899 | * headers can be prepended, and the inner header, if 1900 | * present, should be either GRE or UDP/GUE. 1901 | * 1902 | * **BPF_LWT_ENCAP_SEG6**\ \* types can be called by BPF programs 1903 | * of type **BPF_PROG_TYPE_LWT_IN**; **BPF_LWT_ENCAP_IP** type can 1904 | * be called by bpf programs of types **BPF_PROG_TYPE_LWT_IN** and 1905 | * **BPF_PROG_TYPE_LWT_XMIT**. 1906 | * 1907 | * A call to this helper is susceptible to change the underlying 1908 | * packet buffer. Therefore, at load time, all checks on pointers 1909 | * previously done by the verifier are invalidated and must be 1910 | * performed again, if the helper is used in combination with 1911 | * direct packet access. 1912 | * 1913 | * Returns 1914 | * 0 on success, or a negative error in case of failure. 1915 | */ 1916 | static long (*bpf_lwt_push_encap)(struct __sk_buff *skb, __u32 type, void *hdr, __u32 len) = (void *) 73; 1917 | 1918 | /* 1919 | * bpf_lwt_seg6_store_bytes 1920 | * 1921 | * Store *len* bytes from address *from* into the packet 1922 | * associated to *skb*, at *offset*. Only the flags, tag and TLVs 1923 | * inside the outermost IPv6 Segment Routing Header can be 1924 | * modified through this helper. 1925 | * 1926 | * A call to this helper is susceptible to change the underlying 1927 | * packet buffer. Therefore, at load time, all checks on pointers 1928 | * previously done by the verifier are invalidated and must be 1929 | * performed again, if the helper is used in combination with 1930 | * direct packet access. 1931 | * 1932 | * Returns 1933 | * 0 on success, or a negative error in case of failure. 1934 | */ 1935 | static long (*bpf_lwt_seg6_store_bytes)(struct __sk_buff *skb, __u32 offset, const void *from, __u32 len) = (void *) 74; 1936 | 1937 | /* 1938 | * bpf_lwt_seg6_adjust_srh 1939 | * 1940 | * Adjust the size allocated to TLVs in the outermost IPv6 1941 | * Segment Routing Header contained in the packet associated to 1942 | * *skb*, at position *offset* by *delta* bytes. Only offsets 1943 | * after the segments are accepted. *delta* can be as well 1944 | * positive (growing) as negative (shrinking). 1945 | * 1946 | * A call to this helper is susceptible to change the underlying 1947 | * packet buffer. Therefore, at load time, all checks on pointers 1948 | * previously done by the verifier are invalidated and must be 1949 | * performed again, if the helper is used in combination with 1950 | * direct packet access. 1951 | * 1952 | * Returns 1953 | * 0 on success, or a negative error in case of failure. 1954 | */ 1955 | static long (*bpf_lwt_seg6_adjust_srh)(struct __sk_buff *skb, __u32 offset, __s32 delta) = (void *) 75; 1956 | 1957 | /* 1958 | * bpf_lwt_seg6_action 1959 | * 1960 | * Apply an IPv6 Segment Routing action of type *action* to the 1961 | * packet associated to *skb*. Each action takes a parameter 1962 | * contained at address *param*, and of length *param_len* bytes. 1963 | * *action* can be one of: 1964 | * 1965 | * **SEG6_LOCAL_ACTION_END_X** 1966 | * End.X action: Endpoint with Layer-3 cross-connect. 1967 | * Type of *param*: **struct in6_addr**. 1968 | * **SEG6_LOCAL_ACTION_END_T** 1969 | * End.T action: Endpoint with specific IPv6 table lookup. 1970 | * Type of *param*: **int**. 1971 | * **SEG6_LOCAL_ACTION_END_B6** 1972 | * End.B6 action: Endpoint bound to an SRv6 policy. 1973 | * Type of *param*: **struct ipv6_sr_hdr**. 1974 | * **SEG6_LOCAL_ACTION_END_B6_ENCAP** 1975 | * End.B6.Encap action: Endpoint bound to an SRv6 1976 | * encapsulation policy. 1977 | * Type of *param*: **struct ipv6_sr_hdr**. 1978 | * 1979 | * A call to this helper is susceptible to change the underlying 1980 | * packet buffer. Therefore, at load time, all checks on pointers 1981 | * previously done by the verifier are invalidated and must be 1982 | * performed again, if the helper is used in combination with 1983 | * direct packet access. 1984 | * 1985 | * Returns 1986 | * 0 on success, or a negative error in case of failure. 1987 | */ 1988 | static long (*bpf_lwt_seg6_action)(struct __sk_buff *skb, __u32 action, void *param, __u32 param_len) = (void *) 76; 1989 | 1990 | /* 1991 | * bpf_rc_repeat 1992 | * 1993 | * This helper is used in programs implementing IR decoding, to 1994 | * report a successfully decoded repeat key message. This delays 1995 | * the generation of a key up event for previously generated 1996 | * key down event. 1997 | * 1998 | * Some IR protocols like NEC have a special IR message for 1999 | * repeating last button, for when a button is held down. 2000 | * 2001 | * The *ctx* should point to the lirc sample as passed into 2002 | * the program. 2003 | * 2004 | * This helper is only available is the kernel was compiled with 2005 | * the **CONFIG_BPF_LIRC_MODE2** configuration option set to 2006 | * "**y**". 2007 | * 2008 | * Returns 2009 | * 0 2010 | */ 2011 | static long (*bpf_rc_repeat)(void *ctx) = (void *) 77; 2012 | 2013 | /* 2014 | * bpf_rc_keydown 2015 | * 2016 | * This helper is used in programs implementing IR decoding, to 2017 | * report a successfully decoded key press with *scancode*, 2018 | * *toggle* value in the given *protocol*. The scancode will be 2019 | * translated to a keycode using the rc keymap, and reported as 2020 | * an input key down event. After a period a key up event is 2021 | * generated. This period can be extended by calling either 2022 | * **bpf_rc_keydown**\ () again with the same values, or calling 2023 | * **bpf_rc_repeat**\ (). 2024 | * 2025 | * Some protocols include a toggle bit, in case the button was 2026 | * released and pressed again between consecutive scancodes. 2027 | * 2028 | * The *ctx* should point to the lirc sample as passed into 2029 | * the program. 2030 | * 2031 | * The *protocol* is the decoded protocol number (see 2032 | * **enum rc_proto** for some predefined values). 2033 | * 2034 | * This helper is only available is the kernel was compiled with 2035 | * the **CONFIG_BPF_LIRC_MODE2** configuration option set to 2036 | * "**y**". 2037 | * 2038 | * Returns 2039 | * 0 2040 | */ 2041 | static long (*bpf_rc_keydown)(void *ctx, __u32 protocol, __u64 scancode, __u32 toggle) = (void *) 78; 2042 | 2043 | /* 2044 | * bpf_skb_cgroup_id 2045 | * 2046 | * Return the cgroup v2 id of the socket associated with the *skb*. 2047 | * This is roughly similar to the **bpf_get_cgroup_classid**\ () 2048 | * helper for cgroup v1 by providing a tag resp. identifier that 2049 | * can be matched on or used for map lookups e.g. to implement 2050 | * policy. The cgroup v2 id of a given path in the hierarchy is 2051 | * exposed in user space through the f_handle API in order to get 2052 | * to the same 64-bit id. 2053 | * 2054 | * This helper can be used on TC egress path, but not on ingress, 2055 | * and is available only if the kernel was compiled with the 2056 | * **CONFIG_SOCK_CGROUP_DATA** configuration option. 2057 | * 2058 | * Returns 2059 | * The id is returned or 0 in case the id could not be retrieved. 2060 | */ 2061 | static __u64 (*bpf_skb_cgroup_id)(struct __sk_buff *skb) = (void *) 79; 2062 | 2063 | /* 2064 | * bpf_get_current_cgroup_id 2065 | * 2066 | * 2067 | * Returns 2068 | * A 64-bit integer containing the current cgroup id based 2069 | * on the cgroup within which the current task is running. 2070 | */ 2071 | static __u64 (*bpf_get_current_cgroup_id)(void) = (void *) 80; 2072 | 2073 | /* 2074 | * bpf_get_local_storage 2075 | * 2076 | * Get the pointer to the local storage area. 2077 | * The type and the size of the local storage is defined 2078 | * by the *map* argument. 2079 | * The *flags* meaning is specific for each map type, 2080 | * and has to be 0 for cgroup local storage. 2081 | * 2082 | * Depending on the BPF program type, a local storage area 2083 | * can be shared between multiple instances of the BPF program, 2084 | * running simultaneously. 2085 | * 2086 | * A user should care about the synchronization by himself. 2087 | * For example, by using the **BPF_ATOMIC** instructions to alter 2088 | * the shared data. 2089 | * 2090 | * Returns 2091 | * A pointer to the local storage area. 2092 | */ 2093 | static void *(*bpf_get_local_storage)(void *map, __u64 flags) = (void *) 81; 2094 | 2095 | /* 2096 | * bpf_sk_select_reuseport 2097 | * 2098 | * Select a **SO_REUSEPORT** socket from a 2099 | * **BPF_MAP_TYPE_REUSEPORT_SOCKARRAY** *map*. 2100 | * It checks the selected socket is matching the incoming 2101 | * request in the socket buffer. 2102 | * 2103 | * Returns 2104 | * 0 on success, or a negative error in case of failure. 2105 | */ 2106 | static long (*bpf_sk_select_reuseport)(struct sk_reuseport_md *reuse, void *map, void *key, __u64 flags) = (void *) 82; 2107 | 2108 | /* 2109 | * bpf_skb_ancestor_cgroup_id 2110 | * 2111 | * Return id of cgroup v2 that is ancestor of cgroup associated 2112 | * with the *skb* at the *ancestor_level*. The root cgroup is at 2113 | * *ancestor_level* zero and each step down the hierarchy 2114 | * increments the level. If *ancestor_level* == level of cgroup 2115 | * associated with *skb*, then return value will be same as that 2116 | * of **bpf_skb_cgroup_id**\ (). 2117 | * 2118 | * The helper is useful to implement policies based on cgroups 2119 | * that are upper in hierarchy than immediate cgroup associated 2120 | * with *skb*. 2121 | * 2122 | * The format of returned id and helper limitations are same as in 2123 | * **bpf_skb_cgroup_id**\ (). 2124 | * 2125 | * Returns 2126 | * The id is returned or 0 in case the id could not be retrieved. 2127 | */ 2128 | static __u64 (*bpf_skb_ancestor_cgroup_id)(struct __sk_buff *skb, int ancestor_level) = (void *) 83; 2129 | 2130 | /* 2131 | * bpf_sk_lookup_tcp 2132 | * 2133 | * Look for TCP socket matching *tuple*, optionally in a child 2134 | * network namespace *netns*. The return value must be checked, 2135 | * and if non-**NULL**, released via **bpf_sk_release**\ (). 2136 | * 2137 | * The *ctx* should point to the context of the program, such as 2138 | * the skb or socket (depending on the hook in use). This is used 2139 | * to determine the base network namespace for the lookup. 2140 | * 2141 | * *tuple_size* must be one of: 2142 | * 2143 | * **sizeof**\ (*tuple*\ **->ipv4**) 2144 | * Look for an IPv4 socket. 2145 | * **sizeof**\ (*tuple*\ **->ipv6**) 2146 | * Look for an IPv6 socket. 2147 | * 2148 | * If the *netns* is a negative signed 32-bit integer, then the 2149 | * socket lookup table in the netns associated with the *ctx* 2150 | * will be used. For the TC hooks, this is the netns of the device 2151 | * in the skb. For socket hooks, this is the netns of the socket. 2152 | * If *netns* is any other signed 32-bit value greater than or 2153 | * equal to zero then it specifies the ID of the netns relative to 2154 | * the netns associated with the *ctx*. *netns* values beyond the 2155 | * range of 32-bit integers are reserved for future use. 2156 | * 2157 | * All values for *flags* are reserved for future usage, and must 2158 | * be left at zero. 2159 | * 2160 | * This helper is available only if the kernel was compiled with 2161 | * **CONFIG_NET** configuration option. 2162 | * 2163 | * Returns 2164 | * Pointer to **struct bpf_sock**, or **NULL** in case of failure. 2165 | * For sockets with reuseport option, the **struct bpf_sock** 2166 | * result is from *reuse*\ **->socks**\ [] using the hash of the 2167 | * tuple. 2168 | */ 2169 | static struct bpf_sock *(*bpf_sk_lookup_tcp)(void *ctx, struct bpf_sock_tuple *tuple, __u32 tuple_size, __u64 netns, __u64 flags) = (void *) 84; 2170 | 2171 | /* 2172 | * bpf_sk_lookup_udp 2173 | * 2174 | * Look for UDP socket matching *tuple*, optionally in a child 2175 | * network namespace *netns*. The return value must be checked, 2176 | * and if non-**NULL**, released via **bpf_sk_release**\ (). 2177 | * 2178 | * The *ctx* should point to the context of the program, such as 2179 | * the skb or socket (depending on the hook in use). This is used 2180 | * to determine the base network namespace for the lookup. 2181 | * 2182 | * *tuple_size* must be one of: 2183 | * 2184 | * **sizeof**\ (*tuple*\ **->ipv4**) 2185 | * Look for an IPv4 socket. 2186 | * **sizeof**\ (*tuple*\ **->ipv6**) 2187 | * Look for an IPv6 socket. 2188 | * 2189 | * If the *netns* is a negative signed 32-bit integer, then the 2190 | * socket lookup table in the netns associated with the *ctx* 2191 | * will be used. For the TC hooks, this is the netns of the device 2192 | * in the skb. For socket hooks, this is the netns of the socket. 2193 | * If *netns* is any other signed 32-bit value greater than or 2194 | * equal to zero then it specifies the ID of the netns relative to 2195 | * the netns associated with the *ctx*. *netns* values beyond the 2196 | * range of 32-bit integers are reserved for future use. 2197 | * 2198 | * All values for *flags* are reserved for future usage, and must 2199 | * be left at zero. 2200 | * 2201 | * This helper is available only if the kernel was compiled with 2202 | * **CONFIG_NET** configuration option. 2203 | * 2204 | * Returns 2205 | * Pointer to **struct bpf_sock**, or **NULL** in case of failure. 2206 | * For sockets with reuseport option, the **struct bpf_sock** 2207 | * result is from *reuse*\ **->socks**\ [] using the hash of the 2208 | * tuple. 2209 | */ 2210 | static struct bpf_sock *(*bpf_sk_lookup_udp)(void *ctx, struct bpf_sock_tuple *tuple, __u32 tuple_size, __u64 netns, __u64 flags) = (void *) 85; 2211 | 2212 | /* 2213 | * bpf_sk_release 2214 | * 2215 | * Release the reference held by *sock*. *sock* must be a 2216 | * non-**NULL** pointer that was returned from 2217 | * **bpf_sk_lookup_xxx**\ (). 2218 | * 2219 | * Returns 2220 | * 0 on success, or a negative error in case of failure. 2221 | */ 2222 | static long (*bpf_sk_release)(void *sock) = (void *) 86; 2223 | 2224 | /* 2225 | * bpf_map_push_elem 2226 | * 2227 | * Push an element *value* in *map*. *flags* is one of: 2228 | * 2229 | * **BPF_EXIST** 2230 | * If the queue/stack is full, the oldest element is 2231 | * removed to make room for this. 2232 | * 2233 | * Returns 2234 | * 0 on success, or a negative error in case of failure. 2235 | */ 2236 | static long (*bpf_map_push_elem)(void *map, const void *value, __u64 flags) = (void *) 87; 2237 | 2238 | /* 2239 | * bpf_map_pop_elem 2240 | * 2241 | * Pop an element from *map*. 2242 | * 2243 | * Returns 2244 | * 0 on success, or a negative error in case of failure. 2245 | */ 2246 | static long (*bpf_map_pop_elem)(void *map, void *value) = (void *) 88; 2247 | 2248 | /* 2249 | * bpf_map_peek_elem 2250 | * 2251 | * Get an element from *map* without removing it. 2252 | * 2253 | * Returns 2254 | * 0 on success, or a negative error in case of failure. 2255 | */ 2256 | static long (*bpf_map_peek_elem)(void *map, void *value) = (void *) 89; 2257 | 2258 | /* 2259 | * bpf_msg_push_data 2260 | * 2261 | * For socket policies, insert *len* bytes into *msg* at offset 2262 | * *start*. 2263 | * 2264 | * If a program of type **BPF_PROG_TYPE_SK_MSG** is run on a 2265 | * *msg* it may want to insert metadata or options into the *msg*. 2266 | * This can later be read and used by any of the lower layer BPF 2267 | * hooks. 2268 | * 2269 | * This helper may fail if under memory pressure (a malloc 2270 | * fails) in these cases BPF programs will get an appropriate 2271 | * error and BPF programs will need to handle them. 2272 | * 2273 | * Returns 2274 | * 0 on success, or a negative error in case of failure. 2275 | */ 2276 | static long (*bpf_msg_push_data)(struct sk_msg_md *msg, __u32 start, __u32 len, __u64 flags) = (void *) 90; 2277 | 2278 | /* 2279 | * bpf_msg_pop_data 2280 | * 2281 | * Will remove *len* bytes from a *msg* starting at byte *start*. 2282 | * This may result in **ENOMEM** errors under certain situations if 2283 | * an allocation and copy are required due to a full ring buffer. 2284 | * However, the helper will try to avoid doing the allocation 2285 | * if possible. Other errors can occur if input parameters are 2286 | * invalid either due to *start* byte not being valid part of *msg* 2287 | * payload and/or *pop* value being to large. 2288 | * 2289 | * Returns 2290 | * 0 on success, or a negative error in case of failure. 2291 | */ 2292 | static long (*bpf_msg_pop_data)(struct sk_msg_md *msg, __u32 start, __u32 len, __u64 flags) = (void *) 91; 2293 | 2294 | /* 2295 | * bpf_rc_pointer_rel 2296 | * 2297 | * This helper is used in programs implementing IR decoding, to 2298 | * report a successfully decoded pointer movement. 2299 | * 2300 | * The *ctx* should point to the lirc sample as passed into 2301 | * the program. 2302 | * 2303 | * This helper is only available is the kernel was compiled with 2304 | * the **CONFIG_BPF_LIRC_MODE2** configuration option set to 2305 | * "**y**". 2306 | * 2307 | * Returns 2308 | * 0 2309 | */ 2310 | static long (*bpf_rc_pointer_rel)(void *ctx, __s32 rel_x, __s32 rel_y) = (void *) 92; 2311 | 2312 | /* 2313 | * bpf_spin_lock 2314 | * 2315 | * Acquire a spinlock represented by the pointer *lock*, which is 2316 | * stored as part of a value of a map. Taking the lock allows to 2317 | * safely update the rest of the fields in that value. The 2318 | * spinlock can (and must) later be released with a call to 2319 | * **bpf_spin_unlock**\ (\ *lock*\ ). 2320 | * 2321 | * Spinlocks in BPF programs come with a number of restrictions 2322 | * and constraints: 2323 | * 2324 | * * **bpf_spin_lock** objects are only allowed inside maps of 2325 | * types **BPF_MAP_TYPE_HASH** and **BPF_MAP_TYPE_ARRAY** (this 2326 | * list could be extended in the future). 2327 | * * BTF description of the map is mandatory. 2328 | * * The BPF program can take ONE lock at a time, since taking two 2329 | * or more could cause dead locks. 2330 | * * Only one **struct bpf_spin_lock** is allowed per map element. 2331 | * * When the lock is taken, calls (either BPF to BPF or helpers) 2332 | * are not allowed. 2333 | * * The **BPF_LD_ABS** and **BPF_LD_IND** instructions are not 2334 | * allowed inside a spinlock-ed region. 2335 | * * The BPF program MUST call **bpf_spin_unlock**\ () to release 2336 | * the lock, on all execution paths, before it returns. 2337 | * * The BPF program can access **struct bpf_spin_lock** only via 2338 | * the **bpf_spin_lock**\ () and **bpf_spin_unlock**\ () 2339 | * helpers. Loading or storing data into the **struct 2340 | * bpf_spin_lock** *lock*\ **;** field of a map is not allowed. 2341 | * * To use the **bpf_spin_lock**\ () helper, the BTF description 2342 | * of the map value must be a struct and have **struct 2343 | * bpf_spin_lock** *anyname*\ **;** field at the top level. 2344 | * Nested lock inside another struct is not allowed. 2345 | * * The **struct bpf_spin_lock** *lock* field in a map value must 2346 | * be aligned on a multiple of 4 bytes in that value. 2347 | * * Syscall with command **BPF_MAP_LOOKUP_ELEM** does not copy 2348 | * the **bpf_spin_lock** field to user space. 2349 | * * Syscall with command **BPF_MAP_UPDATE_ELEM**, or update from 2350 | * a BPF program, do not update the **bpf_spin_lock** field. 2351 | * * **bpf_spin_lock** cannot be on the stack or inside a 2352 | * networking packet (it can only be inside of a map values). 2353 | * * **bpf_spin_lock** is available to root only. 2354 | * * Tracing programs and socket filter programs cannot use 2355 | * **bpf_spin_lock**\ () due to insufficient preemption checks 2356 | * (but this may change in the future). 2357 | * * **bpf_spin_lock** is not allowed in inner maps of map-in-map. 2358 | * 2359 | * Returns 2360 | * 0 2361 | */ 2362 | static long (*bpf_spin_lock)(struct bpf_spin_lock *lock) = (void *) 93; 2363 | 2364 | /* 2365 | * bpf_spin_unlock 2366 | * 2367 | * Release the *lock* previously locked by a call to 2368 | * **bpf_spin_lock**\ (\ *lock*\ ). 2369 | * 2370 | * Returns 2371 | * 0 2372 | */ 2373 | static long (*bpf_spin_unlock)(struct bpf_spin_lock *lock) = (void *) 94; 2374 | 2375 | /* 2376 | * bpf_sk_fullsock 2377 | * 2378 | * This helper gets a **struct bpf_sock** pointer such 2379 | * that all the fields in this **bpf_sock** can be accessed. 2380 | * 2381 | * Returns 2382 | * A **struct bpf_sock** pointer on success, or **NULL** in 2383 | * case of failure. 2384 | */ 2385 | static struct bpf_sock *(*bpf_sk_fullsock)(struct bpf_sock *sk) = (void *) 95; 2386 | 2387 | /* 2388 | * bpf_tcp_sock 2389 | * 2390 | * This helper gets a **struct bpf_tcp_sock** pointer from a 2391 | * **struct bpf_sock** pointer. 2392 | * 2393 | * Returns 2394 | * A **struct bpf_tcp_sock** pointer on success, or **NULL** in 2395 | * case of failure. 2396 | */ 2397 | static struct bpf_tcp_sock *(*bpf_tcp_sock)(struct bpf_sock *sk) = (void *) 96; 2398 | 2399 | /* 2400 | * bpf_skb_ecn_set_ce 2401 | * 2402 | * Set ECN (Explicit Congestion Notification) field of IP header 2403 | * to **CE** (Congestion Encountered) if current value is **ECT** 2404 | * (ECN Capable Transport). Otherwise, do nothing. Works with IPv6 2405 | * and IPv4. 2406 | * 2407 | * Returns 2408 | * 1 if the **CE** flag is set (either by the current helper call 2409 | * or because it was already present), 0 if it is not set. 2410 | */ 2411 | static long (*bpf_skb_ecn_set_ce)(struct __sk_buff *skb) = (void *) 97; 2412 | 2413 | /* 2414 | * bpf_get_listener_sock 2415 | * 2416 | * Return a **struct bpf_sock** pointer in **TCP_LISTEN** state. 2417 | * **bpf_sk_release**\ () is unnecessary and not allowed. 2418 | * 2419 | * Returns 2420 | * A **struct bpf_sock** pointer on success, or **NULL** in 2421 | * case of failure. 2422 | */ 2423 | static struct bpf_sock *(*bpf_get_listener_sock)(struct bpf_sock *sk) = (void *) 98; 2424 | 2425 | /* 2426 | * bpf_skc_lookup_tcp 2427 | * 2428 | * Look for TCP socket matching *tuple*, optionally in a child 2429 | * network namespace *netns*. The return value must be checked, 2430 | * and if non-**NULL**, released via **bpf_sk_release**\ (). 2431 | * 2432 | * This function is identical to **bpf_sk_lookup_tcp**\ (), except 2433 | * that it also returns timewait or request sockets. Use 2434 | * **bpf_sk_fullsock**\ () or **bpf_tcp_sock**\ () to access the 2435 | * full structure. 2436 | * 2437 | * This helper is available only if the kernel was compiled with 2438 | * **CONFIG_NET** configuration option. 2439 | * 2440 | * Returns 2441 | * Pointer to **struct bpf_sock**, or **NULL** in case of failure. 2442 | * For sockets with reuseport option, the **struct bpf_sock** 2443 | * result is from *reuse*\ **->socks**\ [] using the hash of the 2444 | * tuple. 2445 | */ 2446 | static struct bpf_sock *(*bpf_skc_lookup_tcp)(void *ctx, struct bpf_sock_tuple *tuple, __u32 tuple_size, __u64 netns, __u64 flags) = (void *) 99; 2447 | 2448 | /* 2449 | * bpf_tcp_check_syncookie 2450 | * 2451 | * Check whether *iph* and *th* contain a valid SYN cookie ACK for 2452 | * the listening socket in *sk*. 2453 | * 2454 | * *iph* points to the start of the IPv4 or IPv6 header, while 2455 | * *iph_len* contains **sizeof**\ (**struct iphdr**) or 2456 | * **sizeof**\ (**struct ip6hdr**). 2457 | * 2458 | * *th* points to the start of the TCP header, while *th_len* 2459 | * contains **sizeof**\ (**struct tcphdr**). 2460 | * 2461 | * Returns 2462 | * 0 if *iph* and *th* are a valid SYN cookie ACK, or a negative 2463 | * error otherwise. 2464 | */ 2465 | static long (*bpf_tcp_check_syncookie)(void *sk, void *iph, __u32 iph_len, struct tcphdr *th, __u32 th_len) = (void *) 100; 2466 | 2467 | /* 2468 | * bpf_sysctl_get_name 2469 | * 2470 | * Get name of sysctl in /proc/sys/ and copy it into provided by 2471 | * program buffer *buf* of size *buf_len*. 2472 | * 2473 | * The buffer is always NUL terminated, unless it's zero-sized. 2474 | * 2475 | * If *flags* is zero, full name (e.g. "net/ipv4/tcp_mem") is 2476 | * copied. Use **BPF_F_SYSCTL_BASE_NAME** flag to copy base name 2477 | * only (e.g. "tcp_mem"). 2478 | * 2479 | * Returns 2480 | * Number of character copied (not including the trailing NUL). 2481 | * 2482 | * **-E2BIG** if the buffer wasn't big enough (*buf* will contain 2483 | * truncated name in this case). 2484 | */ 2485 | static long (*bpf_sysctl_get_name)(struct bpf_sysctl *ctx, char *buf, unsigned long buf_len, __u64 flags) = (void *) 101; 2486 | 2487 | /* 2488 | * bpf_sysctl_get_current_value 2489 | * 2490 | * Get current value of sysctl as it is presented in /proc/sys 2491 | * (incl. newline, etc), and copy it as a string into provided 2492 | * by program buffer *buf* of size *buf_len*. 2493 | * 2494 | * The whole value is copied, no matter what file position user 2495 | * space issued e.g. sys_read at. 2496 | * 2497 | * The buffer is always NUL terminated, unless it's zero-sized. 2498 | * 2499 | * Returns 2500 | * Number of character copied (not including the trailing NUL). 2501 | * 2502 | * **-E2BIG** if the buffer wasn't big enough (*buf* will contain 2503 | * truncated name in this case). 2504 | * 2505 | * **-EINVAL** if current value was unavailable, e.g. because 2506 | * sysctl is uninitialized and read returns -EIO for it. 2507 | */ 2508 | static long (*bpf_sysctl_get_current_value)(struct bpf_sysctl *ctx, char *buf, unsigned long buf_len) = (void *) 102; 2509 | 2510 | /* 2511 | * bpf_sysctl_get_new_value 2512 | * 2513 | * Get new value being written by user space to sysctl (before 2514 | * the actual write happens) and copy it as a string into 2515 | * provided by program buffer *buf* of size *buf_len*. 2516 | * 2517 | * User space may write new value at file position > 0. 2518 | * 2519 | * The buffer is always NUL terminated, unless it's zero-sized. 2520 | * 2521 | * Returns 2522 | * Number of character copied (not including the trailing NUL). 2523 | * 2524 | * **-E2BIG** if the buffer wasn't big enough (*buf* will contain 2525 | * truncated name in this case). 2526 | * 2527 | * **-EINVAL** if sysctl is being read. 2528 | */ 2529 | static long (*bpf_sysctl_get_new_value)(struct bpf_sysctl *ctx, char *buf, unsigned long buf_len) = (void *) 103; 2530 | 2531 | /* 2532 | * bpf_sysctl_set_new_value 2533 | * 2534 | * Override new value being written by user space to sysctl with 2535 | * value provided by program in buffer *buf* of size *buf_len*. 2536 | * 2537 | * *buf* should contain a string in same form as provided by user 2538 | * space on sysctl write. 2539 | * 2540 | * User space may write new value at file position > 0. To override 2541 | * the whole sysctl value file position should be set to zero. 2542 | * 2543 | * Returns 2544 | * 0 on success. 2545 | * 2546 | * **-E2BIG** if the *buf_len* is too big. 2547 | * 2548 | * **-EINVAL** if sysctl is being read. 2549 | */ 2550 | static long (*bpf_sysctl_set_new_value)(struct bpf_sysctl *ctx, const char *buf, unsigned long buf_len) = (void *) 104; 2551 | 2552 | /* 2553 | * bpf_strtol 2554 | * 2555 | * Convert the initial part of the string from buffer *buf* of 2556 | * size *buf_len* to a long integer according to the given base 2557 | * and save the result in *res*. 2558 | * 2559 | * The string may begin with an arbitrary amount of white space 2560 | * (as determined by **isspace**\ (3)) followed by a single 2561 | * optional '**-**' sign. 2562 | * 2563 | * Five least significant bits of *flags* encode base, other bits 2564 | * are currently unused. 2565 | * 2566 | * Base must be either 8, 10, 16 or 0 to detect it automatically 2567 | * similar to user space **strtol**\ (3). 2568 | * 2569 | * Returns 2570 | * Number of characters consumed on success. Must be positive but 2571 | * no more than *buf_len*. 2572 | * 2573 | * **-EINVAL** if no valid digits were found or unsupported base 2574 | * was provided. 2575 | * 2576 | * **-ERANGE** if resulting value was out of range. 2577 | */ 2578 | static long (*bpf_strtol)(const char *buf, unsigned long buf_len, __u64 flags, long *res) = (void *) 105; 2579 | 2580 | /* 2581 | * bpf_strtoul 2582 | * 2583 | * Convert the initial part of the string from buffer *buf* of 2584 | * size *buf_len* to an unsigned long integer according to the 2585 | * given base and save the result in *res*. 2586 | * 2587 | * The string may begin with an arbitrary amount of white space 2588 | * (as determined by **isspace**\ (3)). 2589 | * 2590 | * Five least significant bits of *flags* encode base, other bits 2591 | * are currently unused. 2592 | * 2593 | * Base must be either 8, 10, 16 or 0 to detect it automatically 2594 | * similar to user space **strtoul**\ (3). 2595 | * 2596 | * Returns 2597 | * Number of characters consumed on success. Must be positive but 2598 | * no more than *buf_len*. 2599 | * 2600 | * **-EINVAL** if no valid digits were found or unsupported base 2601 | * was provided. 2602 | * 2603 | * **-ERANGE** if resulting value was out of range. 2604 | */ 2605 | static long (*bpf_strtoul)(const char *buf, unsigned long buf_len, __u64 flags, unsigned long *res) = (void *) 106; 2606 | 2607 | /* 2608 | * bpf_sk_storage_get 2609 | * 2610 | * Get a bpf-local-storage from a *sk*. 2611 | * 2612 | * Logically, it could be thought of getting the value from 2613 | * a *map* with *sk* as the **key**. From this 2614 | * perspective, the usage is not much different from 2615 | * **bpf_map_lookup_elem**\ (*map*, **&**\ *sk*) except this 2616 | * helper enforces the key must be a full socket and the map must 2617 | * be a **BPF_MAP_TYPE_SK_STORAGE** also. 2618 | * 2619 | * Underneath, the value is stored locally at *sk* instead of 2620 | * the *map*. The *map* is used as the bpf-local-storage 2621 | * "type". The bpf-local-storage "type" (i.e. the *map*) is 2622 | * searched against all bpf-local-storages residing at *sk*. 2623 | * 2624 | * *sk* is a kernel **struct sock** pointer for LSM program. 2625 | * *sk* is a **struct bpf_sock** pointer for other program types. 2626 | * 2627 | * An optional *flags* (**BPF_SK_STORAGE_GET_F_CREATE**) can be 2628 | * used such that a new bpf-local-storage will be 2629 | * created if one does not exist. *value* can be used 2630 | * together with **BPF_SK_STORAGE_GET_F_CREATE** to specify 2631 | * the initial value of a bpf-local-storage. If *value* is 2632 | * **NULL**, the new bpf-local-storage will be zero initialized. 2633 | * 2634 | * Returns 2635 | * A bpf-local-storage pointer is returned on success. 2636 | * 2637 | * **NULL** if not found or there was an error in adding 2638 | * a new bpf-local-storage. 2639 | */ 2640 | static void *(*bpf_sk_storage_get)(void *map, void *sk, void *value, __u64 flags) = (void *) 107; 2641 | 2642 | /* 2643 | * bpf_sk_storage_delete 2644 | * 2645 | * Delete a bpf-local-storage from a *sk*. 2646 | * 2647 | * Returns 2648 | * 0 on success. 2649 | * 2650 | * **-ENOENT** if the bpf-local-storage cannot be found. 2651 | * **-EINVAL** if sk is not a fullsock (e.g. a request_sock). 2652 | */ 2653 | static long (*bpf_sk_storage_delete)(void *map, void *sk) = (void *) 108; 2654 | 2655 | /* 2656 | * bpf_send_signal 2657 | * 2658 | * Send signal *sig* to the process of the current task. 2659 | * The signal may be delivered to any of this process's threads. 2660 | * 2661 | * Returns 2662 | * 0 on success or successfully queued. 2663 | * 2664 | * **-EBUSY** if work queue under nmi is full. 2665 | * 2666 | * **-EINVAL** if *sig* is invalid. 2667 | * 2668 | * **-EPERM** if no permission to send the *sig*. 2669 | * 2670 | * **-EAGAIN** if bpf program can try again. 2671 | */ 2672 | static long (*bpf_send_signal)(__u32 sig) = (void *) 109; 2673 | 2674 | /* 2675 | * bpf_tcp_gen_syncookie 2676 | * 2677 | * Try to issue a SYN cookie for the packet with corresponding 2678 | * IP/TCP headers, *iph* and *th*, on the listening socket in *sk*. 2679 | * 2680 | * *iph* points to the start of the IPv4 or IPv6 header, while 2681 | * *iph_len* contains **sizeof**\ (**struct iphdr**) or 2682 | * **sizeof**\ (**struct ip6hdr**). 2683 | * 2684 | * *th* points to the start of the TCP header, while *th_len* 2685 | * contains the length of the TCP header. 2686 | * 2687 | * Returns 2688 | * On success, lower 32 bits hold the generated SYN cookie in 2689 | * followed by 16 bits which hold the MSS value for that cookie, 2690 | * and the top 16 bits are unused. 2691 | * 2692 | * On failure, the returned value is one of the following: 2693 | * 2694 | * **-EINVAL** SYN cookie cannot be issued due to error 2695 | * 2696 | * **-ENOENT** SYN cookie should not be issued (no SYN flood) 2697 | * 2698 | * **-EOPNOTSUPP** kernel configuration does not enable SYN cookies 2699 | * 2700 | * **-EPROTONOSUPPORT** IP packet version is not 4 or 6 2701 | */ 2702 | static __s64 (*bpf_tcp_gen_syncookie)(void *sk, void *iph, __u32 iph_len, struct tcphdr *th, __u32 th_len) = (void *) 110; 2703 | 2704 | /* 2705 | * bpf_skb_output 2706 | * 2707 | * Write raw *data* blob into a special BPF perf event held by 2708 | * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. This perf 2709 | * event must have the following attributes: **PERF_SAMPLE_RAW** 2710 | * as **sample_type**, **PERF_TYPE_SOFTWARE** as **type**, and 2711 | * **PERF_COUNT_SW_BPF_OUTPUT** as **config**. 2712 | * 2713 | * The *flags* are used to indicate the index in *map* for which 2714 | * the value must be put, masked with **BPF_F_INDEX_MASK**. 2715 | * Alternatively, *flags* can be set to **BPF_F_CURRENT_CPU** 2716 | * to indicate that the index of the current CPU core should be 2717 | * used. 2718 | * 2719 | * The value to write, of *size*, is passed through eBPF stack and 2720 | * pointed by *data*. 2721 | * 2722 | * *ctx* is a pointer to in-kernel struct sk_buff. 2723 | * 2724 | * This helper is similar to **bpf_perf_event_output**\ () but 2725 | * restricted to raw_tracepoint bpf programs. 2726 | * 2727 | * Returns 2728 | * 0 on success, or a negative error in case of failure. 2729 | */ 2730 | static long (*bpf_skb_output)(void *ctx, void *map, __u64 flags, void *data, __u64 size) = (void *) 111; 2731 | 2732 | /* 2733 | * bpf_probe_read_user 2734 | * 2735 | * Safely attempt to read *size* bytes from user space address 2736 | * *unsafe_ptr* and store the data in *dst*. 2737 | * 2738 | * Returns 2739 | * 0 on success, or a negative error in case of failure. 2740 | */ 2741 | static long (*bpf_probe_read_user)(void *dst, __u32 size, const void *unsafe_ptr) = (void *) 112; 2742 | 2743 | /* 2744 | * bpf_probe_read_kernel 2745 | * 2746 | * Safely attempt to read *size* bytes from kernel space address 2747 | * *unsafe_ptr* and store the data in *dst*. 2748 | * 2749 | * Returns 2750 | * 0 on success, or a negative error in case of failure. 2751 | */ 2752 | static long (*bpf_probe_read_kernel)(void *dst, __u32 size, const void *unsafe_ptr) = (void *) 113; 2753 | 2754 | /* 2755 | * bpf_probe_read_user_str 2756 | * 2757 | * Copy a NUL terminated string from an unsafe user address 2758 | * *unsafe_ptr* to *dst*. The *size* should include the 2759 | * terminating NUL byte. In case the string length is smaller than 2760 | * *size*, the target is not padded with further NUL bytes. If the 2761 | * string length is larger than *size*, just *size*-1 bytes are 2762 | * copied and the last byte is set to NUL. 2763 | * 2764 | * On success, returns the number of bytes that were written, 2765 | * including the terminal NUL. This makes this helper useful in 2766 | * tracing programs for reading strings, and more importantly to 2767 | * get its length at runtime. See the following snippet: 2768 | * 2769 | * :: 2770 | * 2771 | * SEC("kprobe/sys_open") 2772 | * void bpf_sys_open(struct pt_regs *ctx) 2773 | * { 2774 | * char buf[PATHLEN]; // PATHLEN is defined to 256 2775 | * int res = bpf_probe_read_user_str(buf, sizeof(buf), 2776 | * ctx->di); 2777 | * 2778 | * // Consume buf, for example push it to 2779 | * // userspace via bpf_perf_event_output(); we 2780 | * // can use res (the string length) as event 2781 | * // size, after checking its boundaries. 2782 | * } 2783 | * 2784 | * In comparison, using **bpf_probe_read_user**\ () helper here 2785 | * instead to read the string would require to estimate the length 2786 | * at compile time, and would often result in copying more memory 2787 | * than necessary. 2788 | * 2789 | * Another useful use case is when parsing individual process 2790 | * arguments or individual environment variables navigating 2791 | * *current*\ **->mm->arg_start** and *current*\ 2792 | * **->mm->env_start**: using this helper and the return value, 2793 | * one can quickly iterate at the right offset of the memory area. 2794 | * 2795 | * Returns 2796 | * On success, the strictly positive length of the output string, 2797 | * including the trailing NUL character. On error, a negative 2798 | * value. 2799 | */ 2800 | static long (*bpf_probe_read_user_str)(void *dst, __u32 size, const void *unsafe_ptr) = (void *) 114; 2801 | 2802 | /* 2803 | * bpf_probe_read_kernel_str 2804 | * 2805 | * Copy a NUL terminated string from an unsafe kernel address *unsafe_ptr* 2806 | * to *dst*. Same semantics as with **bpf_probe_read_user_str**\ () apply. 2807 | * 2808 | * Returns 2809 | * On success, the strictly positive length of the string, including 2810 | * the trailing NUL character. On error, a negative value. 2811 | */ 2812 | static long (*bpf_probe_read_kernel_str)(void *dst, __u32 size, const void *unsafe_ptr) = (void *) 115; 2813 | 2814 | /* 2815 | * bpf_tcp_send_ack 2816 | * 2817 | * Send out a tcp-ack. *tp* is the in-kernel struct **tcp_sock**. 2818 | * *rcv_nxt* is the ack_seq to be sent out. 2819 | * 2820 | * Returns 2821 | * 0 on success, or a negative error in case of failure. 2822 | */ 2823 | static long (*bpf_tcp_send_ack)(void *tp, __u32 rcv_nxt) = (void *) 116; 2824 | 2825 | /* 2826 | * bpf_send_signal_thread 2827 | * 2828 | * Send signal *sig* to the thread corresponding to the current task. 2829 | * 2830 | * Returns 2831 | * 0 on success or successfully queued. 2832 | * 2833 | * **-EBUSY** if work queue under nmi is full. 2834 | * 2835 | * **-EINVAL** if *sig* is invalid. 2836 | * 2837 | * **-EPERM** if no permission to send the *sig*. 2838 | * 2839 | * **-EAGAIN** if bpf program can try again. 2840 | */ 2841 | static long (*bpf_send_signal_thread)(__u32 sig) = (void *) 117; 2842 | 2843 | /* 2844 | * bpf_jiffies64 2845 | * 2846 | * Obtain the 64bit jiffies 2847 | * 2848 | * Returns 2849 | * The 64 bit jiffies 2850 | */ 2851 | static __u64 (*bpf_jiffies64)(void) = (void *) 118; 2852 | 2853 | /* 2854 | * bpf_read_branch_records 2855 | * 2856 | * For an eBPF program attached to a perf event, retrieve the 2857 | * branch records (**struct perf_branch_entry**) associated to *ctx* 2858 | * and store it in the buffer pointed by *buf* up to size 2859 | * *size* bytes. 2860 | * 2861 | * Returns 2862 | * On success, number of bytes written to *buf*. On error, a 2863 | * negative value. 2864 | * 2865 | * The *flags* can be set to **BPF_F_GET_BRANCH_RECORDS_SIZE** to 2866 | * instead return the number of bytes required to store all the 2867 | * branch entries. If this flag is set, *buf* may be NULL. 2868 | * 2869 | * **-EINVAL** if arguments invalid or **size** not a multiple 2870 | * of **sizeof**\ (**struct perf_branch_entry**\ ). 2871 | * 2872 | * **-ENOENT** if architecture does not support branch records. 2873 | */ 2874 | static long (*bpf_read_branch_records)(struct bpf_perf_event_data *ctx, void *buf, __u32 size, __u64 flags) = (void *) 119; 2875 | 2876 | /* 2877 | * bpf_get_ns_current_pid_tgid 2878 | * 2879 | * Returns 0 on success, values for *pid* and *tgid* as seen from the current 2880 | * *namespace* will be returned in *nsdata*. 2881 | * 2882 | * Returns 2883 | * 0 on success, or one of the following in case of failure: 2884 | * 2885 | * **-EINVAL** if dev and inum supplied don't match dev_t and inode number 2886 | * with nsfs of current task, or if dev conversion to dev_t lost high bits. 2887 | * 2888 | * **-ENOENT** if pidns does not exists for the current task. 2889 | */ 2890 | static long (*bpf_get_ns_current_pid_tgid)(__u64 dev, __u64 ino, struct bpf_pidns_info *nsdata, __u32 size) = (void *) 120; 2891 | 2892 | /* 2893 | * bpf_xdp_output 2894 | * 2895 | * Write raw *data* blob into a special BPF perf event held by 2896 | * *map* of type **BPF_MAP_TYPE_PERF_EVENT_ARRAY**. This perf 2897 | * event must have the following attributes: **PERF_SAMPLE_RAW** 2898 | * as **sample_type**, **PERF_TYPE_SOFTWARE** as **type**, and 2899 | * **PERF_COUNT_SW_BPF_OUTPUT** as **config**. 2900 | * 2901 | * The *flags* are used to indicate the index in *map* for which 2902 | * the value must be put, masked with **BPF_F_INDEX_MASK**. 2903 | * Alternatively, *flags* can be set to **BPF_F_CURRENT_CPU** 2904 | * to indicate that the index of the current CPU core should be 2905 | * used. 2906 | * 2907 | * The value to write, of *size*, is passed through eBPF stack and 2908 | * pointed by *data*. 2909 | * 2910 | * *ctx* is a pointer to in-kernel struct xdp_buff. 2911 | * 2912 | * This helper is similar to **bpf_perf_eventoutput**\ () but 2913 | * restricted to raw_tracepoint bpf programs. 2914 | * 2915 | * Returns 2916 | * 0 on success, or a negative error in case of failure. 2917 | */ 2918 | static long (*bpf_xdp_output)(void *ctx, void *map, __u64 flags, void *data, __u64 size) = (void *) 121; 2919 | 2920 | /* 2921 | * bpf_get_netns_cookie 2922 | * 2923 | * Retrieve the cookie (generated by the kernel) of the network 2924 | * namespace the input *ctx* is associated with. The network 2925 | * namespace cookie remains stable for its lifetime and provides 2926 | * a global identifier that can be assumed unique. If *ctx* is 2927 | * NULL, then the helper returns the cookie for the initial 2928 | * network namespace. The cookie itself is very similar to that 2929 | * of **bpf_get_socket_cookie**\ () helper, but for network 2930 | * namespaces instead of sockets. 2931 | * 2932 | * Returns 2933 | * A 8-byte long opaque number. 2934 | */ 2935 | static __u64 (*bpf_get_netns_cookie)(void *ctx) = (void *) 122; 2936 | 2937 | /* 2938 | * bpf_get_current_ancestor_cgroup_id 2939 | * 2940 | * Return id of cgroup v2 that is ancestor of the cgroup associated 2941 | * with the current task at the *ancestor_level*. The root cgroup 2942 | * is at *ancestor_level* zero and each step down the hierarchy 2943 | * increments the level. If *ancestor_level* == level of cgroup 2944 | * associated with the current task, then return value will be the 2945 | * same as that of **bpf_get_current_cgroup_id**\ (). 2946 | * 2947 | * The helper is useful to implement policies based on cgroups 2948 | * that are upper in hierarchy than immediate cgroup associated 2949 | * with the current task. 2950 | * 2951 | * The format of returned id and helper limitations are same as in 2952 | * **bpf_get_current_cgroup_id**\ (). 2953 | * 2954 | * Returns 2955 | * The id is returned or 0 in case the id could not be retrieved. 2956 | */ 2957 | static __u64 (*bpf_get_current_ancestor_cgroup_id)(int ancestor_level) = (void *) 123; 2958 | 2959 | /* 2960 | * bpf_sk_assign 2961 | * 2962 | * Helper is overloaded depending on BPF program type. This 2963 | * description applies to **BPF_PROG_TYPE_SCHED_CLS** and 2964 | * **BPF_PROG_TYPE_SCHED_ACT** programs. 2965 | * 2966 | * Assign the *sk* to the *skb*. When combined with appropriate 2967 | * routing configuration to receive the packet towards the socket, 2968 | * will cause *skb* to be delivered to the specified socket. 2969 | * Subsequent redirection of *skb* via **bpf_redirect**\ (), 2970 | * **bpf_clone_redirect**\ () or other methods outside of BPF may 2971 | * interfere with successful delivery to the socket. 2972 | * 2973 | * This operation is only valid from TC ingress path. 2974 | * 2975 | * The *flags* argument must be zero. 2976 | * 2977 | * Returns 2978 | * 0 on success, or a negative error in case of failure: 2979 | * 2980 | * **-EINVAL** if specified *flags* are not supported. 2981 | * 2982 | * **-ENOENT** if the socket is unavailable for assignment. 2983 | * 2984 | * **-ENETUNREACH** if the socket is unreachable (wrong netns). 2985 | * 2986 | * **-EOPNOTSUPP** if the operation is not supported, for example 2987 | * a call from outside of TC ingress. 2988 | * 2989 | * **-ESOCKTNOSUPPORT** if the socket type is not supported 2990 | * (reuseport). 2991 | */ 2992 | static long (*bpf_sk_assign)(void *ctx, void *sk, __u64 flags) = (void *) 124; 2993 | 2994 | /* 2995 | * bpf_ktime_get_boot_ns 2996 | * 2997 | * Return the time elapsed since system boot, in nanoseconds. 2998 | * Does include the time the system was suspended. 2999 | * See: **clock_gettime**\ (**CLOCK_BOOTTIME**) 3000 | * 3001 | * Returns 3002 | * Current *ktime*. 3003 | */ 3004 | static __u64 (*bpf_ktime_get_boot_ns)(void) = (void *) 125; 3005 | 3006 | /* 3007 | * bpf_seq_printf 3008 | * 3009 | * **bpf_seq_printf**\ () uses seq_file **seq_printf**\ () to print 3010 | * out the format string. 3011 | * The *m* represents the seq_file. The *fmt* and *fmt_size* are for 3012 | * the format string itself. The *data* and *data_len* are format string 3013 | * arguments. The *data* are a **u64** array and corresponding format string 3014 | * values are stored in the array. For strings and pointers where pointees 3015 | * are accessed, only the pointer values are stored in the *data* array. 3016 | * The *data_len* is the size of *data* in bytes - must be a multiple of 8. 3017 | * 3018 | * Formats **%s**, **%p{i,I}{4,6}** requires to read kernel memory. 3019 | * Reading kernel memory may fail due to either invalid address or 3020 | * valid address but requiring a major memory fault. If reading kernel memory 3021 | * fails, the string for **%s** will be an empty string, and the ip 3022 | * address for **%p{i,I}{4,6}** will be 0. Not returning error to 3023 | * bpf program is consistent with what **bpf_trace_printk**\ () does for now. 3024 | * 3025 | * Returns 3026 | * 0 on success, or a negative error in case of failure: 3027 | * 3028 | * **-EBUSY** if per-CPU memory copy buffer is busy, can try again 3029 | * by returning 1 from bpf program. 3030 | * 3031 | * **-EINVAL** if arguments are invalid, or if *fmt* is invalid/unsupported. 3032 | * 3033 | * **-E2BIG** if *fmt* contains too many format specifiers. 3034 | * 3035 | * **-EOVERFLOW** if an overflow happened: The same object will be tried again. 3036 | */ 3037 | static long (*bpf_seq_printf)(struct seq_file *m, const char *fmt, __u32 fmt_size, const void *data, __u32 data_len) = (void *) 126; 3038 | 3039 | /* 3040 | * bpf_seq_write 3041 | * 3042 | * **bpf_seq_write**\ () uses seq_file **seq_write**\ () to write the data. 3043 | * The *m* represents the seq_file. The *data* and *len* represent the 3044 | * data to write in bytes. 3045 | * 3046 | * Returns 3047 | * 0 on success, or a negative error in case of failure: 3048 | * 3049 | * **-EOVERFLOW** if an overflow happened: The same object will be tried again. 3050 | */ 3051 | static long (*bpf_seq_write)(struct seq_file *m, const void *data, __u32 len) = (void *) 127; 3052 | 3053 | /* 3054 | * bpf_sk_cgroup_id 3055 | * 3056 | * Return the cgroup v2 id of the socket *sk*. 3057 | * 3058 | * *sk* must be a non-**NULL** pointer to a socket, e.g. one 3059 | * returned from **bpf_sk_lookup_xxx**\ (), 3060 | * **bpf_sk_fullsock**\ (), etc. The format of returned id is 3061 | * same as in **bpf_skb_cgroup_id**\ (). 3062 | * 3063 | * This helper is available only if the kernel was compiled with 3064 | * the **CONFIG_SOCK_CGROUP_DATA** configuration option. 3065 | * 3066 | * Returns 3067 | * The id is returned or 0 in case the id could not be retrieved. 3068 | */ 3069 | static __u64 (*bpf_sk_cgroup_id)(void *sk) = (void *) 128; 3070 | 3071 | /* 3072 | * bpf_sk_ancestor_cgroup_id 3073 | * 3074 | * Return id of cgroup v2 that is ancestor of cgroup associated 3075 | * with the *sk* at the *ancestor_level*. The root cgroup is at 3076 | * *ancestor_level* zero and each step down the hierarchy 3077 | * increments the level. If *ancestor_level* == level of cgroup 3078 | * associated with *sk*, then return value will be same as that 3079 | * of **bpf_sk_cgroup_id**\ (). 3080 | * 3081 | * The helper is useful to implement policies based on cgroups 3082 | * that are upper in hierarchy than immediate cgroup associated 3083 | * with *sk*. 3084 | * 3085 | * The format of returned id and helper limitations are same as in 3086 | * **bpf_sk_cgroup_id**\ (). 3087 | * 3088 | * Returns 3089 | * The id is returned or 0 in case the id could not be retrieved. 3090 | */ 3091 | static __u64 (*bpf_sk_ancestor_cgroup_id)(void *sk, int ancestor_level) = (void *) 129; 3092 | 3093 | /* 3094 | * bpf_ringbuf_output 3095 | * 3096 | * Copy *size* bytes from *data* into a ring buffer *ringbuf*. 3097 | * If **BPF_RB_NO_WAKEUP** is specified in *flags*, no notification 3098 | * of new data availability is sent. 3099 | * If **BPF_RB_FORCE_WAKEUP** is specified in *flags*, notification 3100 | * of new data availability is sent unconditionally. 3101 | * If **0** is specified in *flags*, an adaptive notification 3102 | * of new data availability is sent. 3103 | * 3104 | * An adaptive notification is a notification sent whenever the user-space 3105 | * process has caught up and consumed all available payloads. In case the user-space 3106 | * process is still processing a previous payload, then no notification is needed 3107 | * as it will process the newly added payload automatically. 3108 | * 3109 | * Returns 3110 | * 0 on success, or a negative error in case of failure. 3111 | */ 3112 | static long (*bpf_ringbuf_output)(void *ringbuf, void *data, __u64 size, __u64 flags) = (void *) 130; 3113 | 3114 | /* 3115 | * bpf_ringbuf_reserve 3116 | * 3117 | * Reserve *size* bytes of payload in a ring buffer *ringbuf*. 3118 | * *flags* must be 0. 3119 | * 3120 | * Returns 3121 | * Valid pointer with *size* bytes of memory available; NULL, 3122 | * otherwise. 3123 | */ 3124 | static void *(*bpf_ringbuf_reserve)(void *ringbuf, __u64 size, __u64 flags) = (void *) 131; 3125 | 3126 | /* 3127 | * bpf_ringbuf_submit 3128 | * 3129 | * Submit reserved ring buffer sample, pointed to by *data*. 3130 | * If **BPF_RB_NO_WAKEUP** is specified in *flags*, no notification 3131 | * of new data availability is sent. 3132 | * If **BPF_RB_FORCE_WAKEUP** is specified in *flags*, notification 3133 | * of new data availability is sent unconditionally. 3134 | * If **0** is specified in *flags*, an adaptive notification 3135 | * of new data availability is sent. 3136 | * 3137 | * See 'bpf_ringbuf_output()' for the definition of adaptive notification. 3138 | * 3139 | * Returns 3140 | * Nothing. Always succeeds. 3141 | */ 3142 | static void (*bpf_ringbuf_submit)(void *data, __u64 flags) = (void *) 132; 3143 | 3144 | /* 3145 | * bpf_ringbuf_discard 3146 | * 3147 | * Discard reserved ring buffer sample, pointed to by *data*. 3148 | * If **BPF_RB_NO_WAKEUP** is specified in *flags*, no notification 3149 | * of new data availability is sent. 3150 | * If **BPF_RB_FORCE_WAKEUP** is specified in *flags*, notification 3151 | * of new data availability is sent unconditionally. 3152 | * If **0** is specified in *flags*, an adaptive notification 3153 | * of new data availability is sent. 3154 | * 3155 | * See 'bpf_ringbuf_output()' for the definition of adaptive notification. 3156 | * 3157 | * Returns 3158 | * Nothing. Always succeeds. 3159 | */ 3160 | static void (*bpf_ringbuf_discard)(void *data, __u64 flags) = (void *) 133; 3161 | 3162 | /* 3163 | * bpf_ringbuf_query 3164 | * 3165 | * Query various characteristics of provided ring buffer. What 3166 | * exactly is queries is determined by *flags*: 3167 | * 3168 | * * **BPF_RB_AVAIL_DATA**: Amount of data not yet consumed. 3169 | * * **BPF_RB_RING_SIZE**: The size of ring buffer. 3170 | * * **BPF_RB_CONS_POS**: Consumer position (can wrap around). 3171 | * * **BPF_RB_PROD_POS**: Producer(s) position (can wrap around). 3172 | * 3173 | * Data returned is just a momentary snapshot of actual values 3174 | * and could be inaccurate, so this facility should be used to 3175 | * power heuristics and for reporting, not to make 100% correct 3176 | * calculation. 3177 | * 3178 | * Returns 3179 | * Requested value, or 0, if *flags* are not recognized. 3180 | */ 3181 | static __u64 (*bpf_ringbuf_query)(void *ringbuf, __u64 flags) = (void *) 134; 3182 | 3183 | /* 3184 | * bpf_csum_level 3185 | * 3186 | * Change the skbs checksum level by one layer up or down, or 3187 | * reset it entirely to none in order to have the stack perform 3188 | * checksum validation. The level is applicable to the following 3189 | * protocols: TCP, UDP, GRE, SCTP, FCOE. For example, a decap of 3190 | * | ETH | IP | UDP | GUE | IP | TCP | into | ETH | IP | TCP | 3191 | * through **bpf_skb_adjust_room**\ () helper with passing in 3192 | * **BPF_F_ADJ_ROOM_NO_CSUM_RESET** flag would require one call 3193 | * to **bpf_csum_level**\ () with **BPF_CSUM_LEVEL_DEC** since 3194 | * the UDP header is removed. Similarly, an encap of the latter 3195 | * into the former could be accompanied by a helper call to 3196 | * **bpf_csum_level**\ () with **BPF_CSUM_LEVEL_INC** if the 3197 | * skb is still intended to be processed in higher layers of the 3198 | * stack instead of just egressing at tc. 3199 | * 3200 | * There are three supported level settings at this time: 3201 | * 3202 | * * **BPF_CSUM_LEVEL_INC**: Increases skb->csum_level for skbs 3203 | * with CHECKSUM_UNNECESSARY. 3204 | * * **BPF_CSUM_LEVEL_DEC**: Decreases skb->csum_level for skbs 3205 | * with CHECKSUM_UNNECESSARY. 3206 | * * **BPF_CSUM_LEVEL_RESET**: Resets skb->csum_level to 0 and 3207 | * sets CHECKSUM_NONE to force checksum validation by the stack. 3208 | * * **BPF_CSUM_LEVEL_QUERY**: No-op, returns the current 3209 | * skb->csum_level. 3210 | * 3211 | * Returns 3212 | * 0 on success, or a negative error in case of failure. In the 3213 | * case of **BPF_CSUM_LEVEL_QUERY**, the current skb->csum_level 3214 | * is returned or the error code -EACCES in case the skb is not 3215 | * subject to CHECKSUM_UNNECESSARY. 3216 | */ 3217 | static long (*bpf_csum_level)(struct __sk_buff *skb, __u64 level) = (void *) 135; 3218 | 3219 | /* 3220 | * bpf_skc_to_tcp6_sock 3221 | * 3222 | * Dynamically cast a *sk* pointer to a *tcp6_sock* pointer. 3223 | * 3224 | * Returns 3225 | * *sk* if casting is valid, or **NULL** otherwise. 3226 | */ 3227 | static struct tcp6_sock *(*bpf_skc_to_tcp6_sock)(void *sk) = (void *) 136; 3228 | 3229 | /* 3230 | * bpf_skc_to_tcp_sock 3231 | * 3232 | * Dynamically cast a *sk* pointer to a *tcp_sock* pointer. 3233 | * 3234 | * Returns 3235 | * *sk* if casting is valid, or **NULL** otherwise. 3236 | */ 3237 | static struct tcp_sock *(*bpf_skc_to_tcp_sock)(void *sk) = (void *) 137; 3238 | 3239 | /* 3240 | * bpf_skc_to_tcp_timewait_sock 3241 | * 3242 | * Dynamically cast a *sk* pointer to a *tcp_timewait_sock* pointer. 3243 | * 3244 | * Returns 3245 | * *sk* if casting is valid, or **NULL** otherwise. 3246 | */ 3247 | static struct tcp_timewait_sock *(*bpf_skc_to_tcp_timewait_sock)(void *sk) = (void *) 138; 3248 | 3249 | /* 3250 | * bpf_skc_to_tcp_request_sock 3251 | * 3252 | * Dynamically cast a *sk* pointer to a *tcp_request_sock* pointer. 3253 | * 3254 | * Returns 3255 | * *sk* if casting is valid, or **NULL** otherwise. 3256 | */ 3257 | static struct tcp_request_sock *(*bpf_skc_to_tcp_request_sock)(void *sk) = (void *) 139; 3258 | 3259 | /* 3260 | * bpf_skc_to_udp6_sock 3261 | * 3262 | * Dynamically cast a *sk* pointer to a *udp6_sock* pointer. 3263 | * 3264 | * Returns 3265 | * *sk* if casting is valid, or **NULL** otherwise. 3266 | */ 3267 | static struct udp6_sock *(*bpf_skc_to_udp6_sock)(void *sk) = (void *) 140; 3268 | 3269 | /* 3270 | * bpf_get_task_stack 3271 | * 3272 | * Return a user or a kernel stack in bpf program provided buffer. 3273 | * To achieve this, the helper needs *task*, which is a valid 3274 | * pointer to **struct task_struct**. To store the stacktrace, the 3275 | * bpf program provides *buf* with a nonnegative *size*. 3276 | * 3277 | * The last argument, *flags*, holds the number of stack frames to 3278 | * skip (from 0 to 255), masked with 3279 | * **BPF_F_SKIP_FIELD_MASK**. The next bits can be used to set 3280 | * the following flags: 3281 | * 3282 | * **BPF_F_USER_STACK** 3283 | * Collect a user space stack instead of a kernel stack. 3284 | * **BPF_F_USER_BUILD_ID** 3285 | * Collect buildid+offset instead of ips for user stack, 3286 | * only valid if **BPF_F_USER_STACK** is also specified. 3287 | * 3288 | * **bpf_get_task_stack**\ () can collect up to 3289 | * **PERF_MAX_STACK_DEPTH** both kernel and user frames, subject 3290 | * to sufficient large buffer size. Note that 3291 | * this limit can be controlled with the **sysctl** program, and 3292 | * that it should be manually increased in order to profile long 3293 | * user stacks (such as stacks for Java programs). To do so, use: 3294 | * 3295 | * :: 3296 | * 3297 | * # sysctl kernel.perf_event_max_stack= 3298 | * 3299 | * Returns 3300 | * A non-negative value equal to or less than *size* on success, 3301 | * or a negative error in case of failure. 3302 | */ 3303 | static long (*bpf_get_task_stack)(struct task_struct *task, void *buf, __u32 size, __u64 flags) = (void *) 141; 3304 | 3305 | /* 3306 | * bpf_load_hdr_opt 3307 | * 3308 | * Load header option. Support reading a particular TCP header 3309 | * option for bpf program (**BPF_PROG_TYPE_SOCK_OPS**). 3310 | * 3311 | * If *flags* is 0, it will search the option from the 3312 | * *skops*\ **->skb_data**. The comment in **struct bpf_sock_ops** 3313 | * has details on what skb_data contains under different 3314 | * *skops*\ **->op**. 3315 | * 3316 | * The first byte of the *searchby_res* specifies the 3317 | * kind that it wants to search. 3318 | * 3319 | * If the searching kind is an experimental kind 3320 | * (i.e. 253 or 254 according to RFC6994). It also 3321 | * needs to specify the "magic" which is either 3322 | * 2 bytes or 4 bytes. It then also needs to 3323 | * specify the size of the magic by using 3324 | * the 2nd byte which is "kind-length" of a TCP 3325 | * header option and the "kind-length" also 3326 | * includes the first 2 bytes "kind" and "kind-length" 3327 | * itself as a normal TCP header option also does. 3328 | * 3329 | * For example, to search experimental kind 254 with 3330 | * 2 byte magic 0xeB9F, the searchby_res should be 3331 | * [ 254, 4, 0xeB, 0x9F, 0, 0, .... 0 ]. 3332 | * 3333 | * To search for the standard window scale option (3), 3334 | * the *searchby_res* should be [ 3, 0, 0, .... 0 ]. 3335 | * Note, kind-length must be 0 for regular option. 3336 | * 3337 | * Searching for No-Op (0) and End-of-Option-List (1) are 3338 | * not supported. 3339 | * 3340 | * *len* must be at least 2 bytes which is the minimal size 3341 | * of a header option. 3342 | * 3343 | * Supported flags: 3344 | * 3345 | * * **BPF_LOAD_HDR_OPT_TCP_SYN** to search from the 3346 | * saved_syn packet or the just-received syn packet. 3347 | * 3348 | * 3349 | * Returns 3350 | * > 0 when found, the header option is copied to *searchby_res*. 3351 | * The return value is the total length copied. On failure, a 3352 | * negative error code is returned: 3353 | * 3354 | * **-EINVAL** if a parameter is invalid. 3355 | * 3356 | * **-ENOMSG** if the option is not found. 3357 | * 3358 | * **-ENOENT** if no syn packet is available when 3359 | * **BPF_LOAD_HDR_OPT_TCP_SYN** is used. 3360 | * 3361 | * **-ENOSPC** if there is not enough space. Only *len* number of 3362 | * bytes are copied. 3363 | * 3364 | * **-EFAULT** on failure to parse the header options in the 3365 | * packet. 3366 | * 3367 | * **-EPERM** if the helper cannot be used under the current 3368 | * *skops*\ **->op**. 3369 | */ 3370 | static long (*bpf_load_hdr_opt)(struct bpf_sock_ops *skops, void *searchby_res, __u32 len, __u64 flags) = (void *) 142; 3371 | 3372 | /* 3373 | * bpf_store_hdr_opt 3374 | * 3375 | * Store header option. The data will be copied 3376 | * from buffer *from* with length *len* to the TCP header. 3377 | * 3378 | * The buffer *from* should have the whole option that 3379 | * includes the kind, kind-length, and the actual 3380 | * option data. The *len* must be at least kind-length 3381 | * long. The kind-length does not have to be 4 byte 3382 | * aligned. The kernel will take care of the padding 3383 | * and setting the 4 bytes aligned value to th->doff. 3384 | * 3385 | * This helper will check for duplicated option 3386 | * by searching the same option in the outgoing skb. 3387 | * 3388 | * This helper can only be called during 3389 | * **BPF_SOCK_OPS_WRITE_HDR_OPT_CB**. 3390 | * 3391 | * 3392 | * Returns 3393 | * 0 on success, or negative error in case of failure: 3394 | * 3395 | * **-EINVAL** If param is invalid. 3396 | * 3397 | * **-ENOSPC** if there is not enough space in the header. 3398 | * Nothing has been written 3399 | * 3400 | * **-EEXIST** if the option already exists. 3401 | * 3402 | * **-EFAULT** on failrue to parse the existing header options. 3403 | * 3404 | * **-EPERM** if the helper cannot be used under the current 3405 | * *skops*\ **->op**. 3406 | */ 3407 | static long (*bpf_store_hdr_opt)(struct bpf_sock_ops *skops, const void *from, __u32 len, __u64 flags) = (void *) 143; 3408 | 3409 | /* 3410 | * bpf_reserve_hdr_opt 3411 | * 3412 | * Reserve *len* bytes for the bpf header option. The 3413 | * space will be used by **bpf_store_hdr_opt**\ () later in 3414 | * **BPF_SOCK_OPS_WRITE_HDR_OPT_CB**. 3415 | * 3416 | * If **bpf_reserve_hdr_opt**\ () is called multiple times, 3417 | * the total number of bytes will be reserved. 3418 | * 3419 | * This helper can only be called during 3420 | * **BPF_SOCK_OPS_HDR_OPT_LEN_CB**. 3421 | * 3422 | * 3423 | * Returns 3424 | * 0 on success, or negative error in case of failure: 3425 | * 3426 | * **-EINVAL** if a parameter is invalid. 3427 | * 3428 | * **-ENOSPC** if there is not enough space in the header. 3429 | * 3430 | * **-EPERM** if the helper cannot be used under the current 3431 | * *skops*\ **->op**. 3432 | */ 3433 | static long (*bpf_reserve_hdr_opt)(struct bpf_sock_ops *skops, __u32 len, __u64 flags) = (void *) 144; 3434 | 3435 | /* 3436 | * bpf_inode_storage_get 3437 | * 3438 | * Get a bpf_local_storage from an *inode*. 3439 | * 3440 | * Logically, it could be thought of as getting the value from 3441 | * a *map* with *inode* as the **key**. From this 3442 | * perspective, the usage is not much different from 3443 | * **bpf_map_lookup_elem**\ (*map*, **&**\ *inode*) except this 3444 | * helper enforces the key must be an inode and the map must also 3445 | * be a **BPF_MAP_TYPE_INODE_STORAGE**. 3446 | * 3447 | * Underneath, the value is stored locally at *inode* instead of 3448 | * the *map*. The *map* is used as the bpf-local-storage 3449 | * "type". The bpf-local-storage "type" (i.e. the *map*) is 3450 | * searched against all bpf_local_storage residing at *inode*. 3451 | * 3452 | * An optional *flags* (**BPF_LOCAL_STORAGE_GET_F_CREATE**) can be 3453 | * used such that a new bpf_local_storage will be 3454 | * created if one does not exist. *value* can be used 3455 | * together with **BPF_LOCAL_STORAGE_GET_F_CREATE** to specify 3456 | * the initial value of a bpf_local_storage. If *value* is 3457 | * **NULL**, the new bpf_local_storage will be zero initialized. 3458 | * 3459 | * Returns 3460 | * A bpf_local_storage pointer is returned on success. 3461 | * 3462 | * **NULL** if not found or there was an error in adding 3463 | * a new bpf_local_storage. 3464 | */ 3465 | static void *(*bpf_inode_storage_get)(void *map, void *inode, void *value, __u64 flags) = (void *) 145; 3466 | 3467 | /* 3468 | * bpf_inode_storage_delete 3469 | * 3470 | * Delete a bpf_local_storage from an *inode*. 3471 | * 3472 | * Returns 3473 | * 0 on success. 3474 | * 3475 | * **-ENOENT** if the bpf_local_storage cannot be found. 3476 | */ 3477 | static int (*bpf_inode_storage_delete)(void *map, void *inode) = (void *) 146; 3478 | 3479 | /* 3480 | * bpf_d_path 3481 | * 3482 | * Return full path for given **struct path** object, which 3483 | * needs to be the kernel BTF *path* object. The path is 3484 | * returned in the provided buffer *buf* of size *sz* and 3485 | * is zero terminated. 3486 | * 3487 | * 3488 | * Returns 3489 | * On success, the strictly positive length of the string, 3490 | * including the trailing NUL character. On error, a negative 3491 | * value. 3492 | */ 3493 | static long (*bpf_d_path)(struct path *path, char *buf, __u32 sz) = (void *) 147; 3494 | 3495 | /* 3496 | * bpf_copy_from_user 3497 | * 3498 | * Read *size* bytes from user space address *user_ptr* and store 3499 | * the data in *dst*. This is a wrapper of **copy_from_user**\ (). 3500 | * 3501 | * Returns 3502 | * 0 on success, or a negative error in case of failure. 3503 | */ 3504 | static long (*bpf_copy_from_user)(void *dst, __u32 size, const void *user_ptr) = (void *) 148; 3505 | 3506 | /* 3507 | * bpf_snprintf_btf 3508 | * 3509 | * Use BTF to store a string representation of *ptr*->ptr in *str*, 3510 | * using *ptr*->type_id. This value should specify the type 3511 | * that *ptr*->ptr points to. LLVM __builtin_btf_type_id(type, 1) 3512 | * can be used to look up vmlinux BTF type ids. Traversing the 3513 | * data structure using BTF, the type information and values are 3514 | * stored in the first *str_size* - 1 bytes of *str*. Safe copy of 3515 | * the pointer data is carried out to avoid kernel crashes during 3516 | * operation. Smaller types can use string space on the stack; 3517 | * larger programs can use map data to store the string 3518 | * representation. 3519 | * 3520 | * The string can be subsequently shared with userspace via 3521 | * bpf_perf_event_output() or ring buffer interfaces. 3522 | * bpf_trace_printk() is to be avoided as it places too small 3523 | * a limit on string size to be useful. 3524 | * 3525 | * *flags* is a combination of 3526 | * 3527 | * **BTF_F_COMPACT** 3528 | * no formatting around type information 3529 | * **BTF_F_NONAME** 3530 | * no struct/union member names/types 3531 | * **BTF_F_PTR_RAW** 3532 | * show raw (unobfuscated) pointer values; 3533 | * equivalent to printk specifier %px. 3534 | * **BTF_F_ZERO** 3535 | * show zero-valued struct/union members; they 3536 | * are not displayed by default 3537 | * 3538 | * 3539 | * Returns 3540 | * The number of bytes that were written (or would have been 3541 | * written if output had to be truncated due to string size), 3542 | * or a negative error in cases of failure. 3543 | */ 3544 | static long (*bpf_snprintf_btf)(char *str, __u32 str_size, struct btf_ptr *ptr, __u32 btf_ptr_size, __u64 flags) = (void *) 149; 3545 | 3546 | /* 3547 | * bpf_seq_printf_btf 3548 | * 3549 | * Use BTF to write to seq_write a string representation of 3550 | * *ptr*->ptr, using *ptr*->type_id as per bpf_snprintf_btf(). 3551 | * *flags* are identical to those used for bpf_snprintf_btf. 3552 | * 3553 | * Returns 3554 | * 0 on success or a negative error in case of failure. 3555 | */ 3556 | static long (*bpf_seq_printf_btf)(struct seq_file *m, struct btf_ptr *ptr, __u32 ptr_size, __u64 flags) = (void *) 150; 3557 | 3558 | /* 3559 | * bpf_skb_cgroup_classid 3560 | * 3561 | * See **bpf_get_cgroup_classid**\ () for the main description. 3562 | * This helper differs from **bpf_get_cgroup_classid**\ () in that 3563 | * the cgroup v1 net_cls class is retrieved only from the *skb*'s 3564 | * associated socket instead of the current process. 3565 | * 3566 | * Returns 3567 | * The id is returned or 0 in case the id could not be retrieved. 3568 | */ 3569 | static __u64 (*bpf_skb_cgroup_classid)(struct __sk_buff *skb) = (void *) 151; 3570 | 3571 | /* 3572 | * bpf_redirect_neigh 3573 | * 3574 | * Redirect the packet to another net device of index *ifindex* 3575 | * and fill in L2 addresses from neighboring subsystem. This helper 3576 | * is somewhat similar to **bpf_redirect**\ (), except that it 3577 | * populates L2 addresses as well, meaning, internally, the helper 3578 | * relies on the neighbor lookup for the L2 address of the nexthop. 3579 | * 3580 | * The helper will perform a FIB lookup based on the skb's 3581 | * networking header to get the address of the next hop, unless 3582 | * this is supplied by the caller in the *params* argument. The 3583 | * *plen* argument indicates the len of *params* and should be set 3584 | * to 0 if *params* is NULL. 3585 | * 3586 | * The *flags* argument is reserved and must be 0. The helper is 3587 | * currently only supported for tc BPF program types, and enabled 3588 | * for IPv4 and IPv6 protocols. 3589 | * 3590 | * Returns 3591 | * The helper returns **TC_ACT_REDIRECT** on success or 3592 | * **TC_ACT_SHOT** on error. 3593 | */ 3594 | static long (*bpf_redirect_neigh)(__u32 ifindex, struct bpf_redir_neigh *params, int plen, __u64 flags) = (void *) 152; 3595 | 3596 | /* 3597 | * bpf_per_cpu_ptr 3598 | * 3599 | * Take a pointer to a percpu ksym, *percpu_ptr*, and return a 3600 | * pointer to the percpu kernel variable on *cpu*. A ksym is an 3601 | * extern variable decorated with '__ksym'. For ksym, there is a 3602 | * global var (either static or global) defined of the same name 3603 | * in the kernel. The ksym is percpu if the global var is percpu. 3604 | * The returned pointer points to the global percpu var on *cpu*. 3605 | * 3606 | * bpf_per_cpu_ptr() has the same semantic as per_cpu_ptr() in the 3607 | * kernel, except that bpf_per_cpu_ptr() may return NULL. This 3608 | * happens if *cpu* is larger than nr_cpu_ids. The caller of 3609 | * bpf_per_cpu_ptr() must check the returned value. 3610 | * 3611 | * Returns 3612 | * A pointer pointing to the kernel percpu variable on *cpu*, or 3613 | * NULL, if *cpu* is invalid. 3614 | */ 3615 | static void *(*bpf_per_cpu_ptr)(const void *percpu_ptr, __u32 cpu) = (void *) 153; 3616 | 3617 | /* 3618 | * bpf_this_cpu_ptr 3619 | * 3620 | * Take a pointer to a percpu ksym, *percpu_ptr*, and return a 3621 | * pointer to the percpu kernel variable on this cpu. See the 3622 | * description of 'ksym' in **bpf_per_cpu_ptr**\ (). 3623 | * 3624 | * bpf_this_cpu_ptr() has the same semantic as this_cpu_ptr() in 3625 | * the kernel. Different from **bpf_per_cpu_ptr**\ (), it would 3626 | * never return NULL. 3627 | * 3628 | * Returns 3629 | * A pointer pointing to the kernel percpu variable on this cpu. 3630 | */ 3631 | static void *(*bpf_this_cpu_ptr)(const void *percpu_ptr) = (void *) 154; 3632 | 3633 | /* 3634 | * bpf_redirect_peer 3635 | * 3636 | * Redirect the packet to another net device of index *ifindex*. 3637 | * This helper is somewhat similar to **bpf_redirect**\ (), except 3638 | * that the redirection happens to the *ifindex*' peer device and 3639 | * the netns switch takes place from ingress to ingress without 3640 | * going through the CPU's backlog queue. 3641 | * 3642 | * The *flags* argument is reserved and must be 0. The helper is 3643 | * currently only supported for tc BPF program types at the ingress 3644 | * hook and for veth device types. The peer device must reside in a 3645 | * different network namespace. 3646 | * 3647 | * Returns 3648 | * The helper returns **TC_ACT_REDIRECT** on success or 3649 | * **TC_ACT_SHOT** on error. 3650 | */ 3651 | static long (*bpf_redirect_peer)(__u32 ifindex, __u64 flags) = (void *) 155; 3652 | 3653 | /* 3654 | * bpf_task_storage_get 3655 | * 3656 | * Get a bpf_local_storage from the *task*. 3657 | * 3658 | * Logically, it could be thought of as getting the value from 3659 | * a *map* with *task* as the **key**. From this 3660 | * perspective, the usage is not much different from 3661 | * **bpf_map_lookup_elem**\ (*map*, **&**\ *task*) except this 3662 | * helper enforces the key must be an task_struct and the map must also 3663 | * be a **BPF_MAP_TYPE_TASK_STORAGE**. 3664 | * 3665 | * Underneath, the value is stored locally at *task* instead of 3666 | * the *map*. The *map* is used as the bpf-local-storage 3667 | * "type". The bpf-local-storage "type" (i.e. the *map*) is 3668 | * searched against all bpf_local_storage residing at *task*. 3669 | * 3670 | * An optional *flags* (**BPF_LOCAL_STORAGE_GET_F_CREATE**) can be 3671 | * used such that a new bpf_local_storage will be 3672 | * created if one does not exist. *value* can be used 3673 | * together with **BPF_LOCAL_STORAGE_GET_F_CREATE** to specify 3674 | * the initial value of a bpf_local_storage. If *value* is 3675 | * **NULL**, the new bpf_local_storage will be zero initialized. 3676 | * 3677 | * Returns 3678 | * A bpf_local_storage pointer is returned on success. 3679 | * 3680 | * **NULL** if not found or there was an error in adding 3681 | * a new bpf_local_storage. 3682 | */ 3683 | static void *(*bpf_task_storage_get)(void *map, struct task_struct *task, void *value, __u64 flags) = (void *) 156; 3684 | 3685 | /* 3686 | * bpf_task_storage_delete 3687 | * 3688 | * Delete a bpf_local_storage from a *task*. 3689 | * 3690 | * Returns 3691 | * 0 on success. 3692 | * 3693 | * **-ENOENT** if the bpf_local_storage cannot be found. 3694 | */ 3695 | static long (*bpf_task_storage_delete)(void *map, struct task_struct *task) = (void *) 157; 3696 | 3697 | /* 3698 | * bpf_get_current_task_btf 3699 | * 3700 | * Return a BTF pointer to the "current" task. 3701 | * This pointer can also be used in helpers that accept an 3702 | * *ARG_PTR_TO_BTF_ID* of type *task_struct*. 3703 | * 3704 | * Returns 3705 | * Pointer to the current task. 3706 | */ 3707 | static struct task_struct *(*bpf_get_current_task_btf)(void) = (void *) 158; 3708 | 3709 | /* 3710 | * bpf_bprm_opts_set 3711 | * 3712 | * Set or clear certain options on *bprm*: 3713 | * 3714 | * **BPF_F_BPRM_SECUREEXEC** Set the secureexec bit 3715 | * which sets the **AT_SECURE** auxv for glibc. The bit 3716 | * is cleared if the flag is not specified. 3717 | * 3718 | * Returns 3719 | * **-EINVAL** if invalid *flags* are passed, zero otherwise. 3720 | */ 3721 | static long (*bpf_bprm_opts_set)(struct linux_binprm *bprm, __u64 flags) = (void *) 159; 3722 | 3723 | /* 3724 | * bpf_ktime_get_coarse_ns 3725 | * 3726 | * Return a coarse-grained version of the time elapsed since 3727 | * system boot, in nanoseconds. Does not include time the system 3728 | * was suspended. 3729 | * 3730 | * See: **clock_gettime**\ (**CLOCK_MONOTONIC_COARSE**) 3731 | * 3732 | * Returns 3733 | * Current *ktime*. 3734 | */ 3735 | static __u64 (*bpf_ktime_get_coarse_ns)(void) = (void *) 160; 3736 | 3737 | /* 3738 | * bpf_ima_inode_hash 3739 | * 3740 | * Returns the stored IMA hash of the *inode* (if it's avaialable). 3741 | * If the hash is larger than *size*, then only *size* 3742 | * bytes will be copied to *dst* 3743 | * 3744 | * Returns 3745 | * The **hash_algo** is returned on success, 3746 | * **-EOPNOTSUP** if IMA is disabled or **-EINVAL** if 3747 | * invalid arguments are passed. 3748 | */ 3749 | static long (*bpf_ima_inode_hash)(struct inode *inode, void *dst, __u32 size) = (void *) 161; 3750 | 3751 | /* 3752 | * bpf_sock_from_file 3753 | * 3754 | * If the given file represents a socket, returns the associated 3755 | * socket. 3756 | * 3757 | * Returns 3758 | * A pointer to a struct socket on success or NULL if the file is 3759 | * not a socket. 3760 | */ 3761 | static struct socket *(*bpf_sock_from_file)(struct file *file) = (void *) 162; 3762 | 3763 | /* 3764 | * bpf_check_mtu 3765 | * 3766 | * Check packet size against exceeding MTU of net device (based 3767 | * on *ifindex*). This helper will likely be used in combination 3768 | * with helpers that adjust/change the packet size. 3769 | * 3770 | * The argument *len_diff* can be used for querying with a planned 3771 | * size change. This allows to check MTU prior to changing packet 3772 | * ctx. Providing an *len_diff* adjustment that is larger than the 3773 | * actual packet size (resulting in negative packet size) will in 3774 | * principle not exceed the MTU, why it is not considered a 3775 | * failure. Other BPF-helpers are needed for performing the 3776 | * planned size change, why the responsability for catch a negative 3777 | * packet size belong in those helpers. 3778 | * 3779 | * Specifying *ifindex* zero means the MTU check is performed 3780 | * against the current net device. This is practical if this isn't 3781 | * used prior to redirect. 3782 | * 3783 | * On input *mtu_len* must be a valid pointer, else verifier will 3784 | * reject BPF program. If the value *mtu_len* is initialized to 3785 | * zero then the ctx packet size is use. When value *mtu_len* is 3786 | * provided as input this specify the L3 length that the MTU check 3787 | * is done against. Remember XDP and TC length operate at L2, but 3788 | * this value is L3 as this correlate to MTU and IP-header tot_len 3789 | * values which are L3 (similar behavior as bpf_fib_lookup). 3790 | * 3791 | * The Linux kernel route table can configure MTUs on a more 3792 | * specific per route level, which is not provided by this helper. 3793 | * For route level MTU checks use the **bpf_fib_lookup**\ () 3794 | * helper. 3795 | * 3796 | * *ctx* is either **struct xdp_md** for XDP programs or 3797 | * **struct sk_buff** for tc cls_act programs. 3798 | * 3799 | * The *flags* argument can be a combination of one or more of the 3800 | * following values: 3801 | * 3802 | * **BPF_MTU_CHK_SEGS** 3803 | * This flag will only works for *ctx* **struct sk_buff**. 3804 | * If packet context contains extra packet segment buffers 3805 | * (often knows as GSO skb), then MTU check is harder to 3806 | * check at this point, because in transmit path it is 3807 | * possible for the skb packet to get re-segmented 3808 | * (depending on net device features). This could still be 3809 | * a MTU violation, so this flag enables performing MTU 3810 | * check against segments, with a different violation 3811 | * return code to tell it apart. Check cannot use len_diff. 3812 | * 3813 | * On return *mtu_len* pointer contains the MTU value of the net 3814 | * device. Remember the net device configured MTU is the L3 size, 3815 | * which is returned here and XDP and TC length operate at L2. 3816 | * Helper take this into account for you, but remember when using 3817 | * MTU value in your BPF-code. 3818 | * 3819 | * 3820 | * Returns 3821 | * * 0 on success, and populate MTU value in *mtu_len* pointer. 3822 | * 3823 | * * < 0 if any input argument is invalid (*mtu_len* not updated) 3824 | * 3825 | * MTU violations return positive values, but also populate MTU 3826 | * value in *mtu_len* pointer, as this can be needed for 3827 | * implementing PMTU handing: 3828 | * 3829 | * * **BPF_MTU_CHK_RET_FRAG_NEEDED** 3830 | * * **BPF_MTU_CHK_RET_SEGS_TOOBIG** 3831 | */ 3832 | static long (*bpf_check_mtu)(void *ctx, __u32 ifindex, __u32 *mtu_len, __s32 len_diff, __u64 flags) = (void *) 163; 3833 | 3834 | /* 3835 | * bpf_for_each_map_elem 3836 | * 3837 | * For each element in **map**, call **callback_fn** function with 3838 | * **map**, **callback_ctx** and other map-specific parameters. 3839 | * The **callback_fn** should be a static function and 3840 | * the **callback_ctx** should be a pointer to the stack. 3841 | * The **flags** is used to control certain aspects of the helper. 3842 | * Currently, the **flags** must be 0. 3843 | * 3844 | * The following are a list of supported map types and their 3845 | * respective expected callback signatures: 3846 | * 3847 | * BPF_MAP_TYPE_HASH, BPF_MAP_TYPE_PERCPU_HASH, 3848 | * BPF_MAP_TYPE_LRU_HASH, BPF_MAP_TYPE_LRU_PERCPU_HASH, 3849 | * BPF_MAP_TYPE_ARRAY, BPF_MAP_TYPE_PERCPU_ARRAY 3850 | * 3851 | * long (\*callback_fn)(struct bpf_map \*map, const void \*key, void \*value, void \*ctx); 3852 | * 3853 | * For per_cpu maps, the map_value is the value on the cpu where the 3854 | * bpf_prog is running. 3855 | * 3856 | * If **callback_fn** return 0, the helper will continue to the next 3857 | * element. If return value is 1, the helper will skip the rest of 3858 | * elements and return. Other return values are not used now. 3859 | * 3860 | * 3861 | * Returns 3862 | * The number of traversed map elements for success, **-EINVAL** for 3863 | * invalid **flags**. 3864 | */ 3865 | static long (*bpf_for_each_map_elem)(void *map, void *callback_fn, void *callback_ctx, __u64 flags) = (void *) 164; 3866 | 3867 | /* 3868 | * bpf_snprintf 3869 | * 3870 | * Outputs a string into the **str** buffer of size **str_size** 3871 | * based on a format string stored in a read-only map pointed by 3872 | * **fmt**. 3873 | * 3874 | * Each format specifier in **fmt** corresponds to one u64 element 3875 | * in the **data** array. For strings and pointers where pointees 3876 | * are accessed, only the pointer values are stored in the *data* 3877 | * array. The *data_len* is the size of *data* in bytes - must be 3878 | * a multiple of 8. 3879 | * 3880 | * Formats **%s** and **%p{i,I}{4,6}** require to read kernel 3881 | * memory. Reading kernel memory may fail due to either invalid 3882 | * address or valid address but requiring a major memory fault. If 3883 | * reading kernel memory fails, the string for **%s** will be an 3884 | * empty string, and the ip address for **%p{i,I}{4,6}** will be 0. 3885 | * Not returning error to bpf program is consistent with what 3886 | * **bpf_trace_printk**\ () does for now. 3887 | * 3888 | * 3889 | * Returns 3890 | * The strictly positive length of the formatted string, including 3891 | * the trailing zero character. If the return value is greater than 3892 | * **str_size**, **str** contains a truncated string, guaranteed to 3893 | * be zero-terminated except when **str_size** is 0. 3894 | * 3895 | * Or **-EBUSY** if the per-CPU memory copy buffer is busy. 3896 | */ 3897 | static long (*bpf_snprintf)(char *str, __u32 str_size, const char *fmt, __u64 *data, __u32 data_len) = (void *) 165; 3898 | 3899 | /* 3900 | * bpf_sys_bpf 3901 | * 3902 | * Execute bpf syscall with given arguments. 3903 | * 3904 | * Returns 3905 | * A syscall result. 3906 | */ 3907 | static long (*bpf_sys_bpf)(__u32 cmd, void *attr, __u32 attr_size) = (void *) 166; 3908 | 3909 | /* 3910 | * bpf_btf_find_by_name_kind 3911 | * 3912 | * Find BTF type with given name and kind in vmlinux BTF or in module's BTFs. 3913 | * 3914 | * Returns 3915 | * Returns btf_id and btf_obj_fd in lower and upper 32 bits. 3916 | */ 3917 | static long (*bpf_btf_find_by_name_kind)(char *name, int name_sz, __u32 kind, int flags) = (void *) 167; 3918 | 3919 | /* 3920 | * bpf_sys_close 3921 | * 3922 | * Execute close syscall for given FD. 3923 | * 3924 | * Returns 3925 | * A syscall result. 3926 | */ 3927 | static long (*bpf_sys_close)(__u32 fd) = (void *) 168; 3928 | 3929 | /* 3930 | * bpf_timer_init 3931 | * 3932 | * Initialize the timer. 3933 | * First 4 bits of *flags* specify clockid. 3934 | * Only CLOCK_MONOTONIC, CLOCK_REALTIME, CLOCK_BOOTTIME are allowed. 3935 | * All other bits of *flags* are reserved. 3936 | * The verifier will reject the program if *timer* is not from 3937 | * the same *map*. 3938 | * 3939 | * Returns 3940 | * 0 on success. 3941 | * **-EBUSY** if *timer* is already initialized. 3942 | * **-EINVAL** if invalid *flags* are passed. 3943 | * **-EPERM** if *timer* is in a map that doesn't have any user references. 3944 | * The user space should either hold a file descriptor to a map with timers 3945 | * or pin such map in bpffs. When map is unpinned or file descriptor is 3946 | * closed all timers in the map will be cancelled and freed. 3947 | */ 3948 | static long (*bpf_timer_init)(struct bpf_timer *timer, void *map, __u64 flags) = (void *) 169; 3949 | 3950 | /* 3951 | * bpf_timer_set_callback 3952 | * 3953 | * Configure the timer to call *callback_fn* static function. 3954 | * 3955 | * Returns 3956 | * 0 on success. 3957 | * **-EINVAL** if *timer* was not initialized with bpf_timer_init() earlier. 3958 | * **-EPERM** if *timer* is in a map that doesn't have any user references. 3959 | * The user space should either hold a file descriptor to a map with timers 3960 | * or pin such map in bpffs. When map is unpinned or file descriptor is 3961 | * closed all timers in the map will be cancelled and freed. 3962 | */ 3963 | static long (*bpf_timer_set_callback)(struct bpf_timer *timer, void *callback_fn) = (void *) 170; 3964 | 3965 | /* 3966 | * bpf_timer_start 3967 | * 3968 | * Set timer expiration N nanoseconds from the current time. The 3969 | * configured callback will be invoked in soft irq context on some cpu 3970 | * and will not repeat unless another bpf_timer_start() is made. 3971 | * In such case the next invocation can migrate to a different cpu. 3972 | * Since struct bpf_timer is a field inside map element the map 3973 | * owns the timer. The bpf_timer_set_callback() will increment refcnt 3974 | * of BPF program to make sure that callback_fn code stays valid. 3975 | * When user space reference to a map reaches zero all timers 3976 | * in a map are cancelled and corresponding program's refcnts are 3977 | * decremented. This is done to make sure that Ctrl-C of a user 3978 | * process doesn't leave any timers running. If map is pinned in 3979 | * bpffs the callback_fn can re-arm itself indefinitely. 3980 | * bpf_map_update/delete_elem() helpers and user space sys_bpf commands 3981 | * cancel and free the timer in the given map element. 3982 | * The map can contain timers that invoke callback_fn-s from different 3983 | * programs. The same callback_fn can serve different timers from 3984 | * different maps if key/value layout matches across maps. 3985 | * Every bpf_timer_set_callback() can have different callback_fn. 3986 | * 3987 | * 3988 | * Returns 3989 | * 0 on success. 3990 | * **-EINVAL** if *timer* was not initialized with bpf_timer_init() earlier 3991 | * or invalid *flags* are passed. 3992 | */ 3993 | static long (*bpf_timer_start)(struct bpf_timer *timer, __u64 nsecs, __u64 flags) = (void *) 171; 3994 | 3995 | /* 3996 | * bpf_timer_cancel 3997 | * 3998 | * Cancel the timer and wait for callback_fn to finish if it was running. 3999 | * 4000 | * Returns 4001 | * 0 if the timer was not active. 4002 | * 1 if the timer was active. 4003 | * **-EINVAL** if *timer* was not initialized with bpf_timer_init() earlier. 4004 | * **-EDEADLK** if callback_fn tried to call bpf_timer_cancel() on its 4005 | * own timer which would have led to a deadlock otherwise. 4006 | */ 4007 | static long (*bpf_timer_cancel)(struct bpf_timer *timer) = (void *) 172; 4008 | 4009 | /* 4010 | * bpf_get_func_ip 4011 | * 4012 | * Get address of the traced function (for tracing and kprobe programs). 4013 | * 4014 | * Returns 4015 | * Address of the traced function. 4016 | */ 4017 | static __u64 (*bpf_get_func_ip)(void *ctx) = (void *) 173; 4018 | 4019 | /* 4020 | * bpf_get_attach_cookie 4021 | * 4022 | * Get bpf_cookie value provided (optionally) during the program 4023 | * attachment. It might be different for each individual 4024 | * attachment, even if BPF program itself is the same. 4025 | * Expects BPF program context *ctx* as a first argument. 4026 | * 4027 | * Supported for the following program types: 4028 | * - kprobe/uprobe; 4029 | * - tracepoint; 4030 | * - perf_event. 4031 | * 4032 | * Returns 4033 | * Value specified by user at BPF link creation/attachment time 4034 | * or 0, if it was not specified. 4035 | */ 4036 | static __u64 (*bpf_get_attach_cookie)(void *ctx) = (void *) 174; 4037 | 4038 | /* 4039 | * bpf_task_pt_regs 4040 | * 4041 | * Get the struct pt_regs associated with **task**. 4042 | * 4043 | * Returns 4044 | * A pointer to struct pt_regs. 4045 | */ 4046 | static long (*bpf_task_pt_regs)(struct task_struct *task) = (void *) 175; 4047 | 4048 | /* 4049 | * bpf_get_branch_snapshot 4050 | * 4051 | * Get branch trace from hardware engines like Intel LBR. The 4052 | * hardware engine is stopped shortly after the helper is 4053 | * called. Therefore, the user need to filter branch entries 4054 | * based on the actual use case. To capture branch trace 4055 | * before the trigger point of the BPF program, the helper 4056 | * should be called at the beginning of the BPF program. 4057 | * 4058 | * The data is stored as struct perf_branch_entry into output 4059 | * buffer *entries*. *size* is the size of *entries* in bytes. 4060 | * *flags* is reserved for now and must be zero. 4061 | * 4062 | * 4063 | * Returns 4064 | * On success, number of bytes written to *buf*. On error, a 4065 | * negative value. 4066 | * 4067 | * **-EINVAL** if *flags* is not zero. 4068 | * 4069 | * **-ENOENT** if architecture does not support branch records. 4070 | */ 4071 | static long (*bpf_get_branch_snapshot)(void *entries, __u32 size, __u64 flags) = (void *) 176; 4072 | 4073 | /* 4074 | * bpf_trace_vprintk 4075 | * 4076 | * Behaves like **bpf_trace_printk**\ () helper, but takes an array of u64 4077 | * to format and can handle more format args as a result. 4078 | * 4079 | * Arguments are to be used as in **bpf_seq_printf**\ () helper. 4080 | * 4081 | * Returns 4082 | * The number of bytes written to the buffer, or a negative error 4083 | * in case of failure. 4084 | */ 4085 | static long (*bpf_trace_vprintk)(const char *fmt, __u32 fmt_size, const void *data, __u32 data_len) = (void *) 177; 4086 | 4087 | /* 4088 | * bpf_skc_to_unix_sock 4089 | * 4090 | * Dynamically cast a *sk* pointer to a *unix_sock* pointer. 4091 | * 4092 | * Returns 4093 | * *sk* if casting is valid, or **NULL** otherwise. 4094 | */ 4095 | static struct unix_sock *(*bpf_skc_to_unix_sock)(void *sk) = (void *) 178; 4096 | 4097 | /* 4098 | * bpf_kallsyms_lookup_name 4099 | * 4100 | * Get the address of a kernel symbol, returned in *res*. *res* is 4101 | * set to 0 if the symbol is not found. 4102 | * 4103 | * Returns 4104 | * On success, zero. On error, a negative value. 4105 | * 4106 | * **-EINVAL** if *flags* is not zero. 4107 | * 4108 | * **-EINVAL** if string *name* is not the same size as *name_sz*. 4109 | * 4110 | * **-ENOENT** if symbol is not found. 4111 | * 4112 | * **-EPERM** if caller does not have permission to obtain kernel address. 4113 | */ 4114 | static long (*bpf_kallsyms_lookup_name)(const char *name, int name_sz, int flags, __u64 *res) = (void *) 179; 4115 | 4116 | /* 4117 | * bpf_find_vma 4118 | * 4119 | * Find vma of *task* that contains *addr*, call *callback_fn* 4120 | * function with *task*, *vma*, and *callback_ctx*. 4121 | * The *callback_fn* should be a static function and 4122 | * the *callback_ctx* should be a pointer to the stack. 4123 | * The *flags* is used to control certain aspects of the helper. 4124 | * Currently, the *flags* must be 0. 4125 | * 4126 | * The expected callback signature is 4127 | * 4128 | * long (\*callback_fn)(struct task_struct \*task, struct vm_area_struct \*vma, void \*callback_ctx); 4129 | * 4130 | * 4131 | * Returns 4132 | * 0 on success. 4133 | * **-ENOENT** if *task->mm* is NULL, or no vma contains *addr*. 4134 | * **-EBUSY** if failed to try lock mmap_lock. 4135 | * **-EINVAL** for invalid **flags**. 4136 | */ 4137 | static long (*bpf_find_vma)(struct task_struct *task, __u64 addr, void *callback_fn, void *callback_ctx, __u64 flags) = (void *) 180; 4138 | 4139 | 4140 | --------------------------------------------------------------------------------